base.c 81 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454
  1. /*
  2. * linux/fs/proc/base.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. *
  6. * proc base directory handling functions
  7. *
  8. * 1999, Al Viro. Rewritten. Now it covers the whole per-process part.
  9. * Instead of using magical inumbers to determine the kind of object
  10. * we allocate and fill in-core inodes upon lookup. They don't even
  11. * go into icache. We cache the reference to task_struct upon lookup too.
  12. * Eventually it should become a filesystem in its own. We don't use the
  13. * rest of procfs anymore.
  14. *
  15. *
  16. * Changelog:
  17. * 17-Jan-2005
  18. * Allan Bezerra
  19. * Bruna Moreira <bruna.moreira@indt.org.br>
  20. * Edjard Mota <edjard.mota@indt.org.br>
  21. * Ilias Biris <ilias.biris@indt.org.br>
  22. * Mauricio Lin <mauricio.lin@indt.org.br>
  23. *
  24. * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
  25. *
  26. * A new process specific entry (smaps) included in /proc. It shows the
  27. * size of rss for each memory area. The maps entry lacks information
  28. * about physical memory size (rss) for each mapped file, i.e.,
  29. * rss information for executables and library files.
  30. * This additional information is useful for any tools that need to know
  31. * about physical memory consumption for a process specific library.
  32. *
  33. * Changelog:
  34. * 21-Feb-2005
  35. * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
  36. * Pud inclusion in the page table walking.
  37. *
  38. * ChangeLog:
  39. * 10-Mar-2005
  40. * 10LE Instituto Nokia de Tecnologia - INdT:
  41. * A better way to walks through the page table as suggested by Hugh Dickins.
  42. *
  43. * Simo Piiroinen <simo.piiroinen@nokia.com>:
  44. * Smaps information related to shared, private, clean and dirty pages.
  45. *
  46. * Paul Mundt <paul.mundt@nokia.com>:
  47. * Overall revision about smaps.
  48. */
  49. #include <asm/uaccess.h>
  50. #include <linux/errno.h>
  51. #include <linux/time.h>
  52. #include <linux/proc_fs.h>
  53. #include <linux/stat.h>
  54. #include <linux/task_io_accounting_ops.h>
  55. #include <linux/init.h>
  56. #include <linux/capability.h>
  57. #include <linux/file.h>
  58. #include <linux/fdtable.h>
  59. #include <linux/string.h>
  60. #include <linux/seq_file.h>
  61. #include <linux/namei.h>
  62. #include <linux/mnt_namespace.h>
  63. #include <linux/mm.h>
  64. #include <linux/swap.h>
  65. #include <linux/rcupdate.h>
  66. #include <linux/kallsyms.h>
  67. #include <linux/stacktrace.h>
  68. #include <linux/resource.h>
  69. #include <linux/module.h>
  70. #include <linux/mount.h>
  71. #include <linux/security.h>
  72. #include <linux/ptrace.h>
  73. #include <linux/tracehook.h>
  74. #include <linux/cgroup.h>
  75. #include <linux/cpuset.h>
  76. #include <linux/audit.h>
  77. #include <linux/poll.h>
  78. #include <linux/nsproxy.h>
  79. #include <linux/oom.h>
  80. #include <linux/elf.h>
  81. #include <linux/pid_namespace.h>
  82. #include <linux/fs_struct.h>
  83. #include <linux/slab.h>
  84. #include "internal.h"
  85. /* NOTE:
  86. * Implementing inode permission operations in /proc is almost
  87. * certainly an error. Permission checks need to happen during
  88. * each system call not at open time. The reason is that most of
  89. * what we wish to check for permissions in /proc varies at runtime.
  90. *
  91. * The classic example of a problem is opening file descriptors
  92. * in /proc for a task before it execs a suid executable.
  93. */
  94. struct pid_entry {
  95. char *name;
  96. int len;
  97. mode_t mode;
  98. const struct inode_operations *iop;
  99. const struct file_operations *fop;
  100. union proc_op op;
  101. };
  102. #define NOD(NAME, MODE, IOP, FOP, OP) { \
  103. .name = (NAME), \
  104. .len = sizeof(NAME) - 1, \
  105. .mode = MODE, \
  106. .iop = IOP, \
  107. .fop = FOP, \
  108. .op = OP, \
  109. }
  110. #define DIR(NAME, MODE, iops, fops) \
  111. NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
  112. #define LNK(NAME, get_link) \
  113. NOD(NAME, (S_IFLNK|S_IRWXUGO), \
  114. &proc_pid_link_inode_operations, NULL, \
  115. { .proc_get_link = get_link } )
  116. #define REG(NAME, MODE, fops) \
  117. NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
  118. #define INF(NAME, MODE, read) \
  119. NOD(NAME, (S_IFREG|(MODE)), \
  120. NULL, &proc_info_file_operations, \
  121. { .proc_read = read } )
  122. #define ONE(NAME, MODE, show) \
  123. NOD(NAME, (S_IFREG|(MODE)), \
  124. NULL, &proc_single_file_operations, \
  125. { .proc_show = show } )
  126. /*
  127. * Count the number of hardlinks for the pid_entry table, excluding the .
  128. * and .. links.
  129. */
  130. static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
  131. unsigned int n)
  132. {
  133. unsigned int i;
  134. unsigned int count;
  135. count = 0;
  136. for (i = 0; i < n; ++i) {
  137. if (S_ISDIR(entries[i].mode))
  138. ++count;
  139. }
  140. return count;
  141. }
  142. static int get_task_root(struct task_struct *task, struct path *root)
  143. {
  144. int result = -ENOENT;
  145. task_lock(task);
  146. if (task->fs) {
  147. get_fs_root(task->fs, root);
  148. result = 0;
  149. }
  150. task_unlock(task);
  151. return result;
  152. }
  153. static int proc_cwd_link(struct inode *inode, struct path *path)
  154. {
  155. struct task_struct *task = get_proc_task(inode);
  156. int result = -ENOENT;
  157. if (task) {
  158. task_lock(task);
  159. if (task->fs) {
  160. get_fs_pwd(task->fs, path);
  161. result = 0;
  162. }
  163. task_unlock(task);
  164. put_task_struct(task);
  165. }
  166. return result;
  167. }
  168. static int proc_root_link(struct inode *inode, struct path *path)
  169. {
  170. struct task_struct *task = get_proc_task(inode);
  171. int result = -ENOENT;
  172. if (task) {
  173. result = get_task_root(task, path);
  174. put_task_struct(task);
  175. }
  176. return result;
  177. }
  178. static struct mm_struct *__check_mem_permission(struct task_struct *task)
  179. {
  180. struct mm_struct *mm;
  181. mm = get_task_mm(task);
  182. if (!mm)
  183. return ERR_PTR(-EINVAL);
  184. /*
  185. * A task can always look at itself, in case it chooses
  186. * to use system calls instead of load instructions.
  187. */
  188. if (task == current)
  189. return mm;
  190. /*
  191. * If current is actively ptrace'ing, and would also be
  192. * permitted to freshly attach with ptrace now, permit it.
  193. */
  194. if (task_is_stopped_or_traced(task)) {
  195. int match;
  196. rcu_read_lock();
  197. match = (tracehook_tracer_task(task) == current);
  198. rcu_read_unlock();
  199. if (match && ptrace_may_access(task, PTRACE_MODE_ATTACH))
  200. return mm;
  201. }
  202. /*
  203. * Noone else is allowed.
  204. */
  205. mmput(mm);
  206. return ERR_PTR(-EPERM);
  207. }
  208. /*
  209. * If current may access user memory in @task return a reference to the
  210. * corresponding mm, otherwise ERR_PTR.
  211. */
  212. static struct mm_struct *check_mem_permission(struct task_struct *task)
  213. {
  214. struct mm_struct *mm;
  215. int err;
  216. /*
  217. * Avoid racing if task exec's as we might get a new mm but validate
  218. * against old credentials.
  219. */
  220. err = mutex_lock_killable(&task->signal->cred_guard_mutex);
  221. if (err)
  222. return ERR_PTR(err);
  223. mm = __check_mem_permission(task);
  224. mutex_unlock(&task->signal->cred_guard_mutex);
  225. return mm;
  226. }
  227. struct mm_struct *mm_for_maps(struct task_struct *task)
  228. {
  229. struct mm_struct *mm;
  230. int err;
  231. err = mutex_lock_killable(&task->signal->cred_guard_mutex);
  232. if (err)
  233. return ERR_PTR(err);
  234. mm = get_task_mm(task);
  235. if (mm && mm != current->mm &&
  236. !ptrace_may_access(task, PTRACE_MODE_READ)) {
  237. mmput(mm);
  238. mm = ERR_PTR(-EACCES);
  239. }
  240. mutex_unlock(&task->signal->cred_guard_mutex);
  241. return mm;
  242. }
  243. static int proc_pid_cmdline(struct task_struct *task, char * buffer)
  244. {
  245. int res = 0;
  246. unsigned int len;
  247. struct mm_struct *mm = get_task_mm(task);
  248. if (!mm)
  249. goto out;
  250. if (!mm->arg_end)
  251. goto out_mm; /* Shh! No looking before we're done */
  252. len = mm->arg_end - mm->arg_start;
  253. if (len > PAGE_SIZE)
  254. len = PAGE_SIZE;
  255. res = access_process_vm(task, mm->arg_start, buffer, len, 0);
  256. // If the nul at the end of args has been overwritten, then
  257. // assume application is using setproctitle(3).
  258. if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
  259. len = strnlen(buffer, res);
  260. if (len < res) {
  261. res = len;
  262. } else {
  263. len = mm->env_end - mm->env_start;
  264. if (len > PAGE_SIZE - res)
  265. len = PAGE_SIZE - res;
  266. res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
  267. res = strnlen(buffer, res);
  268. }
  269. }
  270. out_mm:
  271. mmput(mm);
  272. out:
  273. return res;
  274. }
  275. static int proc_pid_auxv(struct task_struct *task, char *buffer)
  276. {
  277. struct mm_struct *mm = mm_for_maps(task);
  278. int res = PTR_ERR(mm);
  279. if (mm && !IS_ERR(mm)) {
  280. unsigned int nwords = 0;
  281. do {
  282. nwords += 2;
  283. } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
  284. res = nwords * sizeof(mm->saved_auxv[0]);
  285. if (res > PAGE_SIZE)
  286. res = PAGE_SIZE;
  287. memcpy(buffer, mm->saved_auxv, res);
  288. mmput(mm);
  289. }
  290. return res;
  291. }
  292. #ifdef CONFIG_KALLSYMS
  293. /*
  294. * Provides a wchan file via kallsyms in a proper one-value-per-file format.
  295. * Returns the resolved symbol. If that fails, simply return the address.
  296. */
  297. static int proc_pid_wchan(struct task_struct *task, char *buffer)
  298. {
  299. unsigned long wchan;
  300. char symname[KSYM_NAME_LEN];
  301. wchan = get_wchan(task);
  302. if (lookup_symbol_name(wchan, symname) < 0)
  303. if (!ptrace_may_access(task, PTRACE_MODE_READ))
  304. return 0;
  305. else
  306. return sprintf(buffer, "%lu", wchan);
  307. else
  308. return sprintf(buffer, "%s", symname);
  309. }
  310. #endif /* CONFIG_KALLSYMS */
  311. #ifdef CONFIG_STACKTRACE
  312. #define MAX_STACK_TRACE_DEPTH 64
  313. static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
  314. struct pid *pid, struct task_struct *task)
  315. {
  316. struct stack_trace trace;
  317. unsigned long *entries;
  318. int i;
  319. entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
  320. if (!entries)
  321. return -ENOMEM;
  322. trace.nr_entries = 0;
  323. trace.max_entries = MAX_STACK_TRACE_DEPTH;
  324. trace.entries = entries;
  325. trace.skip = 0;
  326. save_stack_trace_tsk(task, &trace);
  327. for (i = 0; i < trace.nr_entries; i++) {
  328. seq_printf(m, "[<%p>] %pS\n",
  329. (void *)entries[i], (void *)entries[i]);
  330. }
  331. kfree(entries);
  332. return 0;
  333. }
  334. #endif
  335. #ifdef CONFIG_SCHEDSTATS
  336. /*
  337. * Provides /proc/PID/schedstat
  338. */
  339. static int proc_pid_schedstat(struct task_struct *task, char *buffer)
  340. {
  341. return sprintf(buffer, "%llu %llu %lu\n",
  342. (unsigned long long)task->se.sum_exec_runtime,
  343. (unsigned long long)task->sched_info.run_delay,
  344. task->sched_info.pcount);
  345. }
  346. #endif
  347. #ifdef CONFIG_LATENCYTOP
  348. static int lstats_show_proc(struct seq_file *m, void *v)
  349. {
  350. int i;
  351. struct inode *inode = m->private;
  352. struct task_struct *task = get_proc_task(inode);
  353. if (!task)
  354. return -ESRCH;
  355. seq_puts(m, "Latency Top version : v0.1\n");
  356. for (i = 0; i < 32; i++) {
  357. struct latency_record *lr = &task->latency_record[i];
  358. if (lr->backtrace[0]) {
  359. int q;
  360. seq_printf(m, "%i %li %li",
  361. lr->count, lr->time, lr->max);
  362. for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
  363. unsigned long bt = lr->backtrace[q];
  364. if (!bt)
  365. break;
  366. if (bt == ULONG_MAX)
  367. break;
  368. seq_printf(m, " %ps", (void *)bt);
  369. }
  370. seq_putc(m, '\n');
  371. }
  372. }
  373. put_task_struct(task);
  374. return 0;
  375. }
  376. static int lstats_open(struct inode *inode, struct file *file)
  377. {
  378. return single_open(file, lstats_show_proc, inode);
  379. }
  380. static ssize_t lstats_write(struct file *file, const char __user *buf,
  381. size_t count, loff_t *offs)
  382. {
  383. struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
  384. if (!task)
  385. return -ESRCH;
  386. clear_all_latency_tracing(task);
  387. put_task_struct(task);
  388. return count;
  389. }
  390. static const struct file_operations proc_lstats_operations = {
  391. .open = lstats_open,
  392. .read = seq_read,
  393. .write = lstats_write,
  394. .llseek = seq_lseek,
  395. .release = single_release,
  396. };
  397. #endif
  398. static int proc_oom_score(struct task_struct *task, char *buffer)
  399. {
  400. unsigned long points = 0;
  401. read_lock(&tasklist_lock);
  402. if (pid_alive(task))
  403. points = oom_badness(task, NULL, NULL,
  404. totalram_pages + total_swap_pages);
  405. read_unlock(&tasklist_lock);
  406. return sprintf(buffer, "%lu\n", points);
  407. }
  408. struct limit_names {
  409. char *name;
  410. char *unit;
  411. };
  412. static const struct limit_names lnames[RLIM_NLIMITS] = {
  413. [RLIMIT_CPU] = {"Max cpu time", "seconds"},
  414. [RLIMIT_FSIZE] = {"Max file size", "bytes"},
  415. [RLIMIT_DATA] = {"Max data size", "bytes"},
  416. [RLIMIT_STACK] = {"Max stack size", "bytes"},
  417. [RLIMIT_CORE] = {"Max core file size", "bytes"},
  418. [RLIMIT_RSS] = {"Max resident set", "bytes"},
  419. [RLIMIT_NPROC] = {"Max processes", "processes"},
  420. [RLIMIT_NOFILE] = {"Max open files", "files"},
  421. [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
  422. [RLIMIT_AS] = {"Max address space", "bytes"},
  423. [RLIMIT_LOCKS] = {"Max file locks", "locks"},
  424. [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
  425. [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
  426. [RLIMIT_NICE] = {"Max nice priority", NULL},
  427. [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
  428. [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
  429. };
  430. /* Display limits for a process */
  431. static int proc_pid_limits(struct task_struct *task, char *buffer)
  432. {
  433. unsigned int i;
  434. int count = 0;
  435. unsigned long flags;
  436. char *bufptr = buffer;
  437. struct rlimit rlim[RLIM_NLIMITS];
  438. if (!lock_task_sighand(task, &flags))
  439. return 0;
  440. memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
  441. unlock_task_sighand(task, &flags);
  442. /*
  443. * print the file header
  444. */
  445. count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
  446. "Limit", "Soft Limit", "Hard Limit", "Units");
  447. for (i = 0; i < RLIM_NLIMITS; i++) {
  448. if (rlim[i].rlim_cur == RLIM_INFINITY)
  449. count += sprintf(&bufptr[count], "%-25s %-20s ",
  450. lnames[i].name, "unlimited");
  451. else
  452. count += sprintf(&bufptr[count], "%-25s %-20lu ",
  453. lnames[i].name, rlim[i].rlim_cur);
  454. if (rlim[i].rlim_max == RLIM_INFINITY)
  455. count += sprintf(&bufptr[count], "%-20s ", "unlimited");
  456. else
  457. count += sprintf(&bufptr[count], "%-20lu ",
  458. rlim[i].rlim_max);
  459. if (lnames[i].unit)
  460. count += sprintf(&bufptr[count], "%-10s\n",
  461. lnames[i].unit);
  462. else
  463. count += sprintf(&bufptr[count], "\n");
  464. }
  465. return count;
  466. }
  467. #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
  468. static int proc_pid_syscall(struct task_struct *task, char *buffer)
  469. {
  470. long nr;
  471. unsigned long args[6], sp, pc;
  472. if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
  473. return sprintf(buffer, "running\n");
  474. if (nr < 0)
  475. return sprintf(buffer, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
  476. return sprintf(buffer,
  477. "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
  478. nr,
  479. args[0], args[1], args[2], args[3], args[4], args[5],
  480. sp, pc);
  481. }
  482. #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
  483. /************************************************************************/
  484. /* Here the fs part begins */
  485. /************************************************************************/
  486. /* permission checks */
  487. static int proc_fd_access_allowed(struct inode *inode)
  488. {
  489. struct task_struct *task;
  490. int allowed = 0;
  491. /* Allow access to a task's file descriptors if it is us or we
  492. * may use ptrace attach to the process and find out that
  493. * information.
  494. */
  495. task = get_proc_task(inode);
  496. if (task) {
  497. allowed = ptrace_may_access(task, PTRACE_MODE_READ);
  498. put_task_struct(task);
  499. }
  500. return allowed;
  501. }
  502. static int proc_setattr(struct dentry *dentry, struct iattr *attr)
  503. {
  504. int error;
  505. struct inode *inode = dentry->d_inode;
  506. if (attr->ia_valid & ATTR_MODE)
  507. return -EPERM;
  508. error = inode_change_ok(inode, attr);
  509. if (error)
  510. return error;
  511. if ((attr->ia_valid & ATTR_SIZE) &&
  512. attr->ia_size != i_size_read(inode)) {
  513. error = vmtruncate(inode, attr->ia_size);
  514. if (error)
  515. return error;
  516. }
  517. setattr_copy(inode, attr);
  518. mark_inode_dirty(inode);
  519. return 0;
  520. }
  521. static const struct inode_operations proc_def_inode_operations = {
  522. .setattr = proc_setattr,
  523. };
  524. static int mounts_open_common(struct inode *inode, struct file *file,
  525. const struct seq_operations *op)
  526. {
  527. struct task_struct *task = get_proc_task(inode);
  528. struct nsproxy *nsp;
  529. struct mnt_namespace *ns = NULL;
  530. struct path root;
  531. struct proc_mounts *p;
  532. int ret = -EINVAL;
  533. if (task) {
  534. rcu_read_lock();
  535. nsp = task_nsproxy(task);
  536. if (nsp) {
  537. ns = nsp->mnt_ns;
  538. if (ns)
  539. get_mnt_ns(ns);
  540. }
  541. rcu_read_unlock();
  542. if (ns && get_task_root(task, &root) == 0)
  543. ret = 0;
  544. put_task_struct(task);
  545. }
  546. if (!ns)
  547. goto err;
  548. if (ret)
  549. goto err_put_ns;
  550. ret = -ENOMEM;
  551. p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
  552. if (!p)
  553. goto err_put_path;
  554. file->private_data = &p->m;
  555. ret = seq_open(file, op);
  556. if (ret)
  557. goto err_free;
  558. p->m.private = p;
  559. p->ns = ns;
  560. p->root = root;
  561. p->event = ns->event;
  562. return 0;
  563. err_free:
  564. kfree(p);
  565. err_put_path:
  566. path_put(&root);
  567. err_put_ns:
  568. put_mnt_ns(ns);
  569. err:
  570. return ret;
  571. }
  572. static int mounts_release(struct inode *inode, struct file *file)
  573. {
  574. struct proc_mounts *p = file->private_data;
  575. path_put(&p->root);
  576. put_mnt_ns(p->ns);
  577. return seq_release(inode, file);
  578. }
  579. static unsigned mounts_poll(struct file *file, poll_table *wait)
  580. {
  581. struct proc_mounts *p = file->private_data;
  582. unsigned res = POLLIN | POLLRDNORM;
  583. poll_wait(file, &p->ns->poll, wait);
  584. if (mnt_had_events(p))
  585. res |= POLLERR | POLLPRI;
  586. return res;
  587. }
  588. static int mounts_open(struct inode *inode, struct file *file)
  589. {
  590. return mounts_open_common(inode, file, &mounts_op);
  591. }
  592. static const struct file_operations proc_mounts_operations = {
  593. .open = mounts_open,
  594. .read = seq_read,
  595. .llseek = seq_lseek,
  596. .release = mounts_release,
  597. .poll = mounts_poll,
  598. };
  599. static int mountinfo_open(struct inode *inode, struct file *file)
  600. {
  601. return mounts_open_common(inode, file, &mountinfo_op);
  602. }
  603. static const struct file_operations proc_mountinfo_operations = {
  604. .open = mountinfo_open,
  605. .read = seq_read,
  606. .llseek = seq_lseek,
  607. .release = mounts_release,
  608. .poll = mounts_poll,
  609. };
  610. static int mountstats_open(struct inode *inode, struct file *file)
  611. {
  612. return mounts_open_common(inode, file, &mountstats_op);
  613. }
  614. static const struct file_operations proc_mountstats_operations = {
  615. .open = mountstats_open,
  616. .read = seq_read,
  617. .llseek = seq_lseek,
  618. .release = mounts_release,
  619. };
  620. #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
  621. static ssize_t proc_info_read(struct file * file, char __user * buf,
  622. size_t count, loff_t *ppos)
  623. {
  624. struct inode * inode = file->f_path.dentry->d_inode;
  625. unsigned long page;
  626. ssize_t length;
  627. struct task_struct *task = get_proc_task(inode);
  628. length = -ESRCH;
  629. if (!task)
  630. goto out_no_task;
  631. if (count > PROC_BLOCK_SIZE)
  632. count = PROC_BLOCK_SIZE;
  633. length = -ENOMEM;
  634. if (!(page = __get_free_page(GFP_TEMPORARY)))
  635. goto out;
  636. length = PROC_I(inode)->op.proc_read(task, (char*)page);
  637. if (length >= 0)
  638. length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
  639. free_page(page);
  640. out:
  641. put_task_struct(task);
  642. out_no_task:
  643. return length;
  644. }
  645. static const struct file_operations proc_info_file_operations = {
  646. .read = proc_info_read,
  647. .llseek = generic_file_llseek,
  648. };
  649. static int proc_single_show(struct seq_file *m, void *v)
  650. {
  651. struct inode *inode = m->private;
  652. struct pid_namespace *ns;
  653. struct pid *pid;
  654. struct task_struct *task;
  655. int ret;
  656. ns = inode->i_sb->s_fs_info;
  657. pid = proc_pid(inode);
  658. task = get_pid_task(pid, PIDTYPE_PID);
  659. if (!task)
  660. return -ESRCH;
  661. ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
  662. put_task_struct(task);
  663. return ret;
  664. }
  665. static int proc_single_open(struct inode *inode, struct file *filp)
  666. {
  667. return single_open(filp, proc_single_show, inode);
  668. }
  669. static const struct file_operations proc_single_file_operations = {
  670. .open = proc_single_open,
  671. .read = seq_read,
  672. .llseek = seq_lseek,
  673. .release = single_release,
  674. };
  675. static int mem_open(struct inode* inode, struct file* file)
  676. {
  677. file->private_data = (void*)((long)current->self_exec_id);
  678. /* OK to pass negative loff_t, we can catch out-of-range */
  679. file->f_mode |= FMODE_UNSIGNED_OFFSET;
  680. return 0;
  681. }
  682. static ssize_t mem_read(struct file * file, char __user * buf,
  683. size_t count, loff_t *ppos)
  684. {
  685. struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
  686. char *page;
  687. unsigned long src = *ppos;
  688. int ret = -ESRCH;
  689. struct mm_struct *mm;
  690. if (!task)
  691. goto out_no_task;
  692. ret = -ENOMEM;
  693. page = (char *)__get_free_page(GFP_TEMPORARY);
  694. if (!page)
  695. goto out;
  696. mm = check_mem_permission(task);
  697. ret = PTR_ERR(mm);
  698. if (IS_ERR(mm))
  699. goto out_free;
  700. ret = -EIO;
  701. if (file->private_data != (void*)((long)current->self_exec_id))
  702. goto out_put;
  703. ret = 0;
  704. while (count > 0) {
  705. int this_len, retval;
  706. this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
  707. retval = access_remote_vm(mm, src, page, this_len, 0);
  708. if (!retval) {
  709. if (!ret)
  710. ret = -EIO;
  711. break;
  712. }
  713. if (copy_to_user(buf, page, retval)) {
  714. ret = -EFAULT;
  715. break;
  716. }
  717. ret += retval;
  718. src += retval;
  719. buf += retval;
  720. count -= retval;
  721. }
  722. *ppos = src;
  723. out_put:
  724. mmput(mm);
  725. out_free:
  726. free_page((unsigned long) page);
  727. out:
  728. put_task_struct(task);
  729. out_no_task:
  730. return ret;
  731. }
  732. static ssize_t mem_write(struct file * file, const char __user *buf,
  733. size_t count, loff_t *ppos)
  734. {
  735. int copied;
  736. char *page;
  737. struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
  738. unsigned long dst = *ppos;
  739. struct mm_struct *mm;
  740. copied = -ESRCH;
  741. if (!task)
  742. goto out_no_task;
  743. mm = check_mem_permission(task);
  744. copied = PTR_ERR(mm);
  745. if (IS_ERR(mm))
  746. goto out_task;
  747. copied = -EIO;
  748. if (file->private_data != (void *)((long)current->self_exec_id))
  749. goto out_mm;
  750. copied = -ENOMEM;
  751. page = (char *)__get_free_page(GFP_TEMPORARY);
  752. if (!page)
  753. goto out_mm;
  754. copied = 0;
  755. while (count > 0) {
  756. int this_len, retval;
  757. this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
  758. if (copy_from_user(page, buf, this_len)) {
  759. copied = -EFAULT;
  760. break;
  761. }
  762. retval = access_remote_vm(mm, dst, page, this_len, 1);
  763. if (!retval) {
  764. if (!copied)
  765. copied = -EIO;
  766. break;
  767. }
  768. copied += retval;
  769. buf += retval;
  770. dst += retval;
  771. count -= retval;
  772. }
  773. *ppos = dst;
  774. free_page((unsigned long) page);
  775. out_mm:
  776. mmput(mm);
  777. out_task:
  778. put_task_struct(task);
  779. out_no_task:
  780. return copied;
  781. }
  782. loff_t mem_lseek(struct file *file, loff_t offset, int orig)
  783. {
  784. switch (orig) {
  785. case 0:
  786. file->f_pos = offset;
  787. break;
  788. case 1:
  789. file->f_pos += offset;
  790. break;
  791. default:
  792. return -EINVAL;
  793. }
  794. force_successful_syscall_return();
  795. return file->f_pos;
  796. }
  797. static const struct file_operations proc_mem_operations = {
  798. .llseek = mem_lseek,
  799. .read = mem_read,
  800. .write = mem_write,
  801. .open = mem_open,
  802. };
  803. static ssize_t environ_read(struct file *file, char __user *buf,
  804. size_t count, loff_t *ppos)
  805. {
  806. struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
  807. char *page;
  808. unsigned long src = *ppos;
  809. int ret = -ESRCH;
  810. struct mm_struct *mm;
  811. if (!task)
  812. goto out_no_task;
  813. ret = -ENOMEM;
  814. page = (char *)__get_free_page(GFP_TEMPORARY);
  815. if (!page)
  816. goto out;
  817. mm = mm_for_maps(task);
  818. ret = PTR_ERR(mm);
  819. if (!mm || IS_ERR(mm))
  820. goto out_free;
  821. ret = 0;
  822. while (count > 0) {
  823. int this_len, retval, max_len;
  824. this_len = mm->env_end - (mm->env_start + src);
  825. if (this_len <= 0)
  826. break;
  827. max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
  828. this_len = (this_len > max_len) ? max_len : this_len;
  829. retval = access_process_vm(task, (mm->env_start + src),
  830. page, this_len, 0);
  831. if (retval <= 0) {
  832. ret = retval;
  833. break;
  834. }
  835. if (copy_to_user(buf, page, retval)) {
  836. ret = -EFAULT;
  837. break;
  838. }
  839. ret += retval;
  840. src += retval;
  841. buf += retval;
  842. count -= retval;
  843. }
  844. *ppos = src;
  845. mmput(mm);
  846. out_free:
  847. free_page((unsigned long) page);
  848. out:
  849. put_task_struct(task);
  850. out_no_task:
  851. return ret;
  852. }
  853. static const struct file_operations proc_environ_operations = {
  854. .read = environ_read,
  855. .llseek = generic_file_llseek,
  856. };
  857. static ssize_t oom_adjust_read(struct file *file, char __user *buf,
  858. size_t count, loff_t *ppos)
  859. {
  860. struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
  861. char buffer[PROC_NUMBUF];
  862. size_t len;
  863. int oom_adjust = OOM_DISABLE;
  864. unsigned long flags;
  865. if (!task)
  866. return -ESRCH;
  867. if (lock_task_sighand(task, &flags)) {
  868. oom_adjust = task->signal->oom_adj;
  869. unlock_task_sighand(task, &flags);
  870. }
  871. put_task_struct(task);
  872. len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
  873. return simple_read_from_buffer(buf, count, ppos, buffer, len);
  874. }
  875. static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
  876. size_t count, loff_t *ppos)
  877. {
  878. struct task_struct *task;
  879. char buffer[PROC_NUMBUF];
  880. long oom_adjust;
  881. unsigned long flags;
  882. int err;
  883. memset(buffer, 0, sizeof(buffer));
  884. if (count > sizeof(buffer) - 1)
  885. count = sizeof(buffer) - 1;
  886. if (copy_from_user(buffer, buf, count)) {
  887. err = -EFAULT;
  888. goto out;
  889. }
  890. err = strict_strtol(strstrip(buffer), 0, &oom_adjust);
  891. if (err)
  892. goto out;
  893. if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
  894. oom_adjust != OOM_DISABLE) {
  895. err = -EINVAL;
  896. goto out;
  897. }
  898. task = get_proc_task(file->f_path.dentry->d_inode);
  899. if (!task) {
  900. err = -ESRCH;
  901. goto out;
  902. }
  903. task_lock(task);
  904. if (!task->mm) {
  905. err = -EINVAL;
  906. goto err_task_lock;
  907. }
  908. if (!lock_task_sighand(task, &flags)) {
  909. err = -ESRCH;
  910. goto err_task_lock;
  911. }
  912. if (oom_adjust < task->signal->oom_adj && !capable(CAP_SYS_RESOURCE)) {
  913. err = -EACCES;
  914. goto err_sighand;
  915. }
  916. if (oom_adjust != task->signal->oom_adj) {
  917. if (oom_adjust == OOM_DISABLE)
  918. atomic_inc(&task->mm->oom_disable_count);
  919. if (task->signal->oom_adj == OOM_DISABLE)
  920. atomic_dec(&task->mm->oom_disable_count);
  921. }
  922. /*
  923. * Warn that /proc/pid/oom_adj is deprecated, see
  924. * Documentation/feature-removal-schedule.txt.
  925. */
  926. printk_once(KERN_WARNING "%s (%d): /proc/%d/oom_adj is deprecated, "
  927. "please use /proc/%d/oom_score_adj instead.\n",
  928. current->comm, task_pid_nr(current),
  929. task_pid_nr(task), task_pid_nr(task));
  930. task->signal->oom_adj = oom_adjust;
  931. /*
  932. * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
  933. * value is always attainable.
  934. */
  935. if (task->signal->oom_adj == OOM_ADJUST_MAX)
  936. task->signal->oom_score_adj = OOM_SCORE_ADJ_MAX;
  937. else
  938. task->signal->oom_score_adj = (oom_adjust * OOM_SCORE_ADJ_MAX) /
  939. -OOM_DISABLE;
  940. err_sighand:
  941. unlock_task_sighand(task, &flags);
  942. err_task_lock:
  943. task_unlock(task);
  944. put_task_struct(task);
  945. out:
  946. return err < 0 ? err : count;
  947. }
  948. static const struct file_operations proc_oom_adjust_operations = {
  949. .read = oom_adjust_read,
  950. .write = oom_adjust_write,
  951. .llseek = generic_file_llseek,
  952. };
  953. static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
  954. size_t count, loff_t *ppos)
  955. {
  956. struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
  957. char buffer[PROC_NUMBUF];
  958. int oom_score_adj = OOM_SCORE_ADJ_MIN;
  959. unsigned long flags;
  960. size_t len;
  961. if (!task)
  962. return -ESRCH;
  963. if (lock_task_sighand(task, &flags)) {
  964. oom_score_adj = task->signal->oom_score_adj;
  965. unlock_task_sighand(task, &flags);
  966. }
  967. put_task_struct(task);
  968. len = snprintf(buffer, sizeof(buffer), "%d\n", oom_score_adj);
  969. return simple_read_from_buffer(buf, count, ppos, buffer, len);
  970. }
  971. static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
  972. size_t count, loff_t *ppos)
  973. {
  974. struct task_struct *task;
  975. char buffer[PROC_NUMBUF];
  976. unsigned long flags;
  977. long oom_score_adj;
  978. int err;
  979. memset(buffer, 0, sizeof(buffer));
  980. if (count > sizeof(buffer) - 1)
  981. count = sizeof(buffer) - 1;
  982. if (copy_from_user(buffer, buf, count)) {
  983. err = -EFAULT;
  984. goto out;
  985. }
  986. err = strict_strtol(strstrip(buffer), 0, &oom_score_adj);
  987. if (err)
  988. goto out;
  989. if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
  990. oom_score_adj > OOM_SCORE_ADJ_MAX) {
  991. err = -EINVAL;
  992. goto out;
  993. }
  994. task = get_proc_task(file->f_path.dentry->d_inode);
  995. if (!task) {
  996. err = -ESRCH;
  997. goto out;
  998. }
  999. task_lock(task);
  1000. if (!task->mm) {
  1001. err = -EINVAL;
  1002. goto err_task_lock;
  1003. }
  1004. if (!lock_task_sighand(task, &flags)) {
  1005. err = -ESRCH;
  1006. goto err_task_lock;
  1007. }
  1008. if (oom_score_adj < task->signal->oom_score_adj_min &&
  1009. !capable(CAP_SYS_RESOURCE)) {
  1010. err = -EACCES;
  1011. goto err_sighand;
  1012. }
  1013. if (oom_score_adj != task->signal->oom_score_adj) {
  1014. if (oom_score_adj == OOM_SCORE_ADJ_MIN)
  1015. atomic_inc(&task->mm->oom_disable_count);
  1016. if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MIN)
  1017. atomic_dec(&task->mm->oom_disable_count);
  1018. }
  1019. task->signal->oom_score_adj = oom_score_adj;
  1020. if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
  1021. task->signal->oom_score_adj_min = oom_score_adj;
  1022. /*
  1023. * Scale /proc/pid/oom_adj appropriately ensuring that OOM_DISABLE is
  1024. * always attainable.
  1025. */
  1026. if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MIN)
  1027. task->signal->oom_adj = OOM_DISABLE;
  1028. else
  1029. task->signal->oom_adj = (oom_score_adj * OOM_ADJUST_MAX) /
  1030. OOM_SCORE_ADJ_MAX;
  1031. err_sighand:
  1032. unlock_task_sighand(task, &flags);
  1033. err_task_lock:
  1034. task_unlock(task);
  1035. put_task_struct(task);
  1036. out:
  1037. return err < 0 ? err : count;
  1038. }
  1039. static const struct file_operations proc_oom_score_adj_operations = {
  1040. .read = oom_score_adj_read,
  1041. .write = oom_score_adj_write,
  1042. .llseek = default_llseek,
  1043. };
  1044. #ifdef CONFIG_AUDITSYSCALL
  1045. #define TMPBUFLEN 21
  1046. static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
  1047. size_t count, loff_t *ppos)
  1048. {
  1049. struct inode * inode = file->f_path.dentry->d_inode;
  1050. struct task_struct *task = get_proc_task(inode);
  1051. ssize_t length;
  1052. char tmpbuf[TMPBUFLEN];
  1053. if (!task)
  1054. return -ESRCH;
  1055. length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
  1056. audit_get_loginuid(task));
  1057. put_task_struct(task);
  1058. return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
  1059. }
  1060. static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
  1061. size_t count, loff_t *ppos)
  1062. {
  1063. struct inode * inode = file->f_path.dentry->d_inode;
  1064. char *page, *tmp;
  1065. ssize_t length;
  1066. uid_t loginuid;
  1067. if (!capable(CAP_AUDIT_CONTROL))
  1068. return -EPERM;
  1069. rcu_read_lock();
  1070. if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
  1071. rcu_read_unlock();
  1072. return -EPERM;
  1073. }
  1074. rcu_read_unlock();
  1075. if (count >= PAGE_SIZE)
  1076. count = PAGE_SIZE - 1;
  1077. if (*ppos != 0) {
  1078. /* No partial writes. */
  1079. return -EINVAL;
  1080. }
  1081. page = (char*)__get_free_page(GFP_TEMPORARY);
  1082. if (!page)
  1083. return -ENOMEM;
  1084. length = -EFAULT;
  1085. if (copy_from_user(page, buf, count))
  1086. goto out_free_page;
  1087. page[count] = '\0';
  1088. loginuid = simple_strtoul(page, &tmp, 10);
  1089. if (tmp == page) {
  1090. length = -EINVAL;
  1091. goto out_free_page;
  1092. }
  1093. length = audit_set_loginuid(current, loginuid);
  1094. if (likely(length == 0))
  1095. length = count;
  1096. out_free_page:
  1097. free_page((unsigned long) page);
  1098. return length;
  1099. }
  1100. static const struct file_operations proc_loginuid_operations = {
  1101. .read = proc_loginuid_read,
  1102. .write = proc_loginuid_write,
  1103. .llseek = generic_file_llseek,
  1104. };
  1105. static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
  1106. size_t count, loff_t *ppos)
  1107. {
  1108. struct inode * inode = file->f_path.dentry->d_inode;
  1109. struct task_struct *task = get_proc_task(inode);
  1110. ssize_t length;
  1111. char tmpbuf[TMPBUFLEN];
  1112. if (!task)
  1113. return -ESRCH;
  1114. length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
  1115. audit_get_sessionid(task));
  1116. put_task_struct(task);
  1117. return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
  1118. }
  1119. static const struct file_operations proc_sessionid_operations = {
  1120. .read = proc_sessionid_read,
  1121. .llseek = generic_file_llseek,
  1122. };
  1123. #endif
  1124. #ifdef CONFIG_FAULT_INJECTION
  1125. static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
  1126. size_t count, loff_t *ppos)
  1127. {
  1128. struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
  1129. char buffer[PROC_NUMBUF];
  1130. size_t len;
  1131. int make_it_fail;
  1132. if (!task)
  1133. return -ESRCH;
  1134. make_it_fail = task->make_it_fail;
  1135. put_task_struct(task);
  1136. len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
  1137. return simple_read_from_buffer(buf, count, ppos, buffer, len);
  1138. }
  1139. static ssize_t proc_fault_inject_write(struct file * file,
  1140. const char __user * buf, size_t count, loff_t *ppos)
  1141. {
  1142. struct task_struct *task;
  1143. char buffer[PROC_NUMBUF], *end;
  1144. int make_it_fail;
  1145. if (!capable(CAP_SYS_RESOURCE))
  1146. return -EPERM;
  1147. memset(buffer, 0, sizeof(buffer));
  1148. if (count > sizeof(buffer) - 1)
  1149. count = sizeof(buffer) - 1;
  1150. if (copy_from_user(buffer, buf, count))
  1151. return -EFAULT;
  1152. make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
  1153. if (*end)
  1154. return -EINVAL;
  1155. task = get_proc_task(file->f_dentry->d_inode);
  1156. if (!task)
  1157. return -ESRCH;
  1158. task->make_it_fail = make_it_fail;
  1159. put_task_struct(task);
  1160. return count;
  1161. }
  1162. static const struct file_operations proc_fault_inject_operations = {
  1163. .read = proc_fault_inject_read,
  1164. .write = proc_fault_inject_write,
  1165. .llseek = generic_file_llseek,
  1166. };
  1167. #endif
  1168. #ifdef CONFIG_SCHED_DEBUG
  1169. /*
  1170. * Print out various scheduling related per-task fields:
  1171. */
  1172. static int sched_show(struct seq_file *m, void *v)
  1173. {
  1174. struct inode *inode = m->private;
  1175. struct task_struct *p;
  1176. p = get_proc_task(inode);
  1177. if (!p)
  1178. return -ESRCH;
  1179. proc_sched_show_task(p, m);
  1180. put_task_struct(p);
  1181. return 0;
  1182. }
  1183. static ssize_t
  1184. sched_write(struct file *file, const char __user *buf,
  1185. size_t count, loff_t *offset)
  1186. {
  1187. struct inode *inode = file->f_path.dentry->d_inode;
  1188. struct task_struct *p;
  1189. p = get_proc_task(inode);
  1190. if (!p)
  1191. return -ESRCH;
  1192. proc_sched_set_task(p);
  1193. put_task_struct(p);
  1194. return count;
  1195. }
  1196. static int sched_open(struct inode *inode, struct file *filp)
  1197. {
  1198. return single_open(filp, sched_show, inode);
  1199. }
  1200. static const struct file_operations proc_pid_sched_operations = {
  1201. .open = sched_open,
  1202. .read = seq_read,
  1203. .write = sched_write,
  1204. .llseek = seq_lseek,
  1205. .release = single_release,
  1206. };
  1207. #endif
  1208. #ifdef CONFIG_SCHED_AUTOGROUP
  1209. /*
  1210. * Print out autogroup related information:
  1211. */
  1212. static int sched_autogroup_show(struct seq_file *m, void *v)
  1213. {
  1214. struct inode *inode = m->private;
  1215. struct task_struct *p;
  1216. p = get_proc_task(inode);
  1217. if (!p)
  1218. return -ESRCH;
  1219. proc_sched_autogroup_show_task(p, m);
  1220. put_task_struct(p);
  1221. return 0;
  1222. }
  1223. static ssize_t
  1224. sched_autogroup_write(struct file *file, const char __user *buf,
  1225. size_t count, loff_t *offset)
  1226. {
  1227. struct inode *inode = file->f_path.dentry->d_inode;
  1228. struct task_struct *p;
  1229. char buffer[PROC_NUMBUF];
  1230. long nice;
  1231. int err;
  1232. memset(buffer, 0, sizeof(buffer));
  1233. if (count > sizeof(buffer) - 1)
  1234. count = sizeof(buffer) - 1;
  1235. if (copy_from_user(buffer, buf, count))
  1236. return -EFAULT;
  1237. err = strict_strtol(strstrip(buffer), 0, &nice);
  1238. if (err)
  1239. return -EINVAL;
  1240. p = get_proc_task(inode);
  1241. if (!p)
  1242. return -ESRCH;
  1243. err = nice;
  1244. err = proc_sched_autogroup_set_nice(p, &err);
  1245. if (err)
  1246. count = err;
  1247. put_task_struct(p);
  1248. return count;
  1249. }
  1250. static int sched_autogroup_open(struct inode *inode, struct file *filp)
  1251. {
  1252. int ret;
  1253. ret = single_open(filp, sched_autogroup_show, NULL);
  1254. if (!ret) {
  1255. struct seq_file *m = filp->private_data;
  1256. m->private = inode;
  1257. }
  1258. return ret;
  1259. }
  1260. static const struct file_operations proc_pid_sched_autogroup_operations = {
  1261. .open = sched_autogroup_open,
  1262. .read = seq_read,
  1263. .write = sched_autogroup_write,
  1264. .llseek = seq_lseek,
  1265. .release = single_release,
  1266. };
  1267. #endif /* CONFIG_SCHED_AUTOGROUP */
  1268. static ssize_t comm_write(struct file *file, const char __user *buf,
  1269. size_t count, loff_t *offset)
  1270. {
  1271. struct inode *inode = file->f_path.dentry->d_inode;
  1272. struct task_struct *p;
  1273. char buffer[TASK_COMM_LEN];
  1274. memset(buffer, 0, sizeof(buffer));
  1275. if (count > sizeof(buffer) - 1)
  1276. count = sizeof(buffer) - 1;
  1277. if (copy_from_user(buffer, buf, count))
  1278. return -EFAULT;
  1279. p = get_proc_task(inode);
  1280. if (!p)
  1281. return -ESRCH;
  1282. if (same_thread_group(current, p))
  1283. set_task_comm(p, buffer);
  1284. else
  1285. count = -EINVAL;
  1286. put_task_struct(p);
  1287. return count;
  1288. }
  1289. static int comm_show(struct seq_file *m, void *v)
  1290. {
  1291. struct inode *inode = m->private;
  1292. struct task_struct *p;
  1293. p = get_proc_task(inode);
  1294. if (!p)
  1295. return -ESRCH;
  1296. task_lock(p);
  1297. seq_printf(m, "%s\n", p->comm);
  1298. task_unlock(p);
  1299. put_task_struct(p);
  1300. return 0;
  1301. }
  1302. static int comm_open(struct inode *inode, struct file *filp)
  1303. {
  1304. return single_open(filp, comm_show, inode);
  1305. }
  1306. static const struct file_operations proc_pid_set_comm_operations = {
  1307. .open = comm_open,
  1308. .read = seq_read,
  1309. .write = comm_write,
  1310. .llseek = seq_lseek,
  1311. .release = single_release,
  1312. };
  1313. /*
  1314. * We added or removed a vma mapping the executable. The vmas are only mapped
  1315. * during exec and are not mapped with the mmap system call.
  1316. * Callers must hold down_write() on the mm's mmap_sem for these
  1317. */
  1318. void added_exe_file_vma(struct mm_struct *mm)
  1319. {
  1320. mm->num_exe_file_vmas++;
  1321. }
  1322. void removed_exe_file_vma(struct mm_struct *mm)
  1323. {
  1324. mm->num_exe_file_vmas--;
  1325. if ((mm->num_exe_file_vmas == 0) && mm->exe_file){
  1326. fput(mm->exe_file);
  1327. mm->exe_file = NULL;
  1328. }
  1329. }
  1330. void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file)
  1331. {
  1332. if (new_exe_file)
  1333. get_file(new_exe_file);
  1334. if (mm->exe_file)
  1335. fput(mm->exe_file);
  1336. mm->exe_file = new_exe_file;
  1337. mm->num_exe_file_vmas = 0;
  1338. }
  1339. struct file *get_mm_exe_file(struct mm_struct *mm)
  1340. {
  1341. struct file *exe_file;
  1342. /* We need mmap_sem to protect against races with removal of
  1343. * VM_EXECUTABLE vmas */
  1344. down_read(&mm->mmap_sem);
  1345. exe_file = mm->exe_file;
  1346. if (exe_file)
  1347. get_file(exe_file);
  1348. up_read(&mm->mmap_sem);
  1349. return exe_file;
  1350. }
  1351. void dup_mm_exe_file(struct mm_struct *oldmm, struct mm_struct *newmm)
  1352. {
  1353. /* It's safe to write the exe_file pointer without exe_file_lock because
  1354. * this is called during fork when the task is not yet in /proc */
  1355. newmm->exe_file = get_mm_exe_file(oldmm);
  1356. }
  1357. static int proc_exe_link(struct inode *inode, struct path *exe_path)
  1358. {
  1359. struct task_struct *task;
  1360. struct mm_struct *mm;
  1361. struct file *exe_file;
  1362. task = get_proc_task(inode);
  1363. if (!task)
  1364. return -ENOENT;
  1365. mm = get_task_mm(task);
  1366. put_task_struct(task);
  1367. if (!mm)
  1368. return -ENOENT;
  1369. exe_file = get_mm_exe_file(mm);
  1370. mmput(mm);
  1371. if (exe_file) {
  1372. *exe_path = exe_file->f_path;
  1373. path_get(&exe_file->f_path);
  1374. fput(exe_file);
  1375. return 0;
  1376. } else
  1377. return -ENOENT;
  1378. }
  1379. static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
  1380. {
  1381. struct inode *inode = dentry->d_inode;
  1382. int error = -EACCES;
  1383. /* We don't need a base pointer in the /proc filesystem */
  1384. path_put(&nd->path);
  1385. /* Are we allowed to snoop on the tasks file descriptors? */
  1386. if (!proc_fd_access_allowed(inode))
  1387. goto out;
  1388. error = PROC_I(inode)->op.proc_get_link(inode, &nd->path);
  1389. out:
  1390. return ERR_PTR(error);
  1391. }
  1392. static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
  1393. {
  1394. char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
  1395. char *pathname;
  1396. int len;
  1397. if (!tmp)
  1398. return -ENOMEM;
  1399. pathname = d_path(path, tmp, PAGE_SIZE);
  1400. len = PTR_ERR(pathname);
  1401. if (IS_ERR(pathname))
  1402. goto out;
  1403. len = tmp + PAGE_SIZE - 1 - pathname;
  1404. if (len > buflen)
  1405. len = buflen;
  1406. if (copy_to_user(buffer, pathname, len))
  1407. len = -EFAULT;
  1408. out:
  1409. free_page((unsigned long)tmp);
  1410. return len;
  1411. }
  1412. static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
  1413. {
  1414. int error = -EACCES;
  1415. struct inode *inode = dentry->d_inode;
  1416. struct path path;
  1417. /* Are we allowed to snoop on the tasks file descriptors? */
  1418. if (!proc_fd_access_allowed(inode))
  1419. goto out;
  1420. error = PROC_I(inode)->op.proc_get_link(inode, &path);
  1421. if (error)
  1422. goto out;
  1423. error = do_proc_readlink(&path, buffer, buflen);
  1424. path_put(&path);
  1425. out:
  1426. return error;
  1427. }
  1428. static const struct inode_operations proc_pid_link_inode_operations = {
  1429. .readlink = proc_pid_readlink,
  1430. .follow_link = proc_pid_follow_link,
  1431. .setattr = proc_setattr,
  1432. };
  1433. /* building an inode */
  1434. static int task_dumpable(struct task_struct *task)
  1435. {
  1436. int dumpable = 0;
  1437. struct mm_struct *mm;
  1438. task_lock(task);
  1439. mm = task->mm;
  1440. if (mm)
  1441. dumpable = get_dumpable(mm);
  1442. task_unlock(task);
  1443. if(dumpable == 1)
  1444. return 1;
  1445. return 0;
  1446. }
  1447. static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
  1448. {
  1449. struct inode * inode;
  1450. struct proc_inode *ei;
  1451. const struct cred *cred;
  1452. /* We need a new inode */
  1453. inode = new_inode(sb);
  1454. if (!inode)
  1455. goto out;
  1456. /* Common stuff */
  1457. ei = PROC_I(inode);
  1458. inode->i_ino = get_next_ino();
  1459. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  1460. inode->i_op = &proc_def_inode_operations;
  1461. /*
  1462. * grab the reference to task.
  1463. */
  1464. ei->pid = get_task_pid(task, PIDTYPE_PID);
  1465. if (!ei->pid)
  1466. goto out_unlock;
  1467. if (task_dumpable(task)) {
  1468. rcu_read_lock();
  1469. cred = __task_cred(task);
  1470. inode->i_uid = cred->euid;
  1471. inode->i_gid = cred->egid;
  1472. rcu_read_unlock();
  1473. }
  1474. security_task_to_inode(task, inode);
  1475. out:
  1476. return inode;
  1477. out_unlock:
  1478. iput(inode);
  1479. return NULL;
  1480. }
  1481. static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
  1482. {
  1483. struct inode *inode = dentry->d_inode;
  1484. struct task_struct *task;
  1485. const struct cred *cred;
  1486. generic_fillattr(inode, stat);
  1487. rcu_read_lock();
  1488. stat->uid = 0;
  1489. stat->gid = 0;
  1490. task = pid_task(proc_pid(inode), PIDTYPE_PID);
  1491. if (task) {
  1492. if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
  1493. task_dumpable(task)) {
  1494. cred = __task_cred(task);
  1495. stat->uid = cred->euid;
  1496. stat->gid = cred->egid;
  1497. }
  1498. }
  1499. rcu_read_unlock();
  1500. return 0;
  1501. }
  1502. /* dentry stuff */
  1503. /*
  1504. * Exceptional case: normally we are not allowed to unhash a busy
  1505. * directory. In this case, however, we can do it - no aliasing problems
  1506. * due to the way we treat inodes.
  1507. *
  1508. * Rewrite the inode's ownerships here because the owning task may have
  1509. * performed a setuid(), etc.
  1510. *
  1511. * Before the /proc/pid/status file was created the only way to read
  1512. * the effective uid of a /process was to stat /proc/pid. Reading
  1513. * /proc/pid/status is slow enough that procps and other packages
  1514. * kept stating /proc/pid. To keep the rules in /proc simple I have
  1515. * made this apply to all per process world readable and executable
  1516. * directories.
  1517. */
  1518. static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
  1519. {
  1520. struct inode *inode;
  1521. struct task_struct *task;
  1522. const struct cred *cred;
  1523. if (nd && nd->flags & LOOKUP_RCU)
  1524. return -ECHILD;
  1525. inode = dentry->d_inode;
  1526. task = get_proc_task(inode);
  1527. if (task) {
  1528. if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
  1529. task_dumpable(task)) {
  1530. rcu_read_lock();
  1531. cred = __task_cred(task);
  1532. inode->i_uid = cred->euid;
  1533. inode->i_gid = cred->egid;
  1534. rcu_read_unlock();
  1535. } else {
  1536. inode->i_uid = 0;
  1537. inode->i_gid = 0;
  1538. }
  1539. inode->i_mode &= ~(S_ISUID | S_ISGID);
  1540. security_task_to_inode(task, inode);
  1541. put_task_struct(task);
  1542. return 1;
  1543. }
  1544. d_drop(dentry);
  1545. return 0;
  1546. }
  1547. static int pid_delete_dentry(const struct dentry * dentry)
  1548. {
  1549. /* Is the task we represent dead?
  1550. * If so, then don't put the dentry on the lru list,
  1551. * kill it immediately.
  1552. */
  1553. return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
  1554. }
  1555. static const struct dentry_operations pid_dentry_operations =
  1556. {
  1557. .d_revalidate = pid_revalidate,
  1558. .d_delete = pid_delete_dentry,
  1559. };
  1560. /* Lookups */
  1561. typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
  1562. struct task_struct *, const void *);
  1563. /*
  1564. * Fill a directory entry.
  1565. *
  1566. * If possible create the dcache entry and derive our inode number and
  1567. * file type from dcache entry.
  1568. *
  1569. * Since all of the proc inode numbers are dynamically generated, the inode
  1570. * numbers do not exist until the inode is cache. This means creating the
  1571. * the dcache entry in readdir is necessary to keep the inode numbers
  1572. * reported by readdir in sync with the inode numbers reported
  1573. * by stat.
  1574. */
  1575. static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
  1576. char *name, int len,
  1577. instantiate_t instantiate, struct task_struct *task, const void *ptr)
  1578. {
  1579. struct dentry *child, *dir = filp->f_path.dentry;
  1580. struct inode *inode;
  1581. struct qstr qname;
  1582. ino_t ino = 0;
  1583. unsigned type = DT_UNKNOWN;
  1584. qname.name = name;
  1585. qname.len = len;
  1586. qname.hash = full_name_hash(name, len);
  1587. child = d_lookup(dir, &qname);
  1588. if (!child) {
  1589. struct dentry *new;
  1590. new = d_alloc(dir, &qname);
  1591. if (new) {
  1592. child = instantiate(dir->d_inode, new, task, ptr);
  1593. if (child)
  1594. dput(new);
  1595. else
  1596. child = new;
  1597. }
  1598. }
  1599. if (!child || IS_ERR(child) || !child->d_inode)
  1600. goto end_instantiate;
  1601. inode = child->d_inode;
  1602. if (inode) {
  1603. ino = inode->i_ino;
  1604. type = inode->i_mode >> 12;
  1605. }
  1606. dput(child);
  1607. end_instantiate:
  1608. if (!ino)
  1609. ino = find_inode_number(dir, &qname);
  1610. if (!ino)
  1611. ino = 1;
  1612. return filldir(dirent, name, len, filp->f_pos, ino, type);
  1613. }
  1614. static unsigned name_to_int(struct dentry *dentry)
  1615. {
  1616. const char *name = dentry->d_name.name;
  1617. int len = dentry->d_name.len;
  1618. unsigned n = 0;
  1619. if (len > 1 && *name == '0')
  1620. goto out;
  1621. while (len-- > 0) {
  1622. unsigned c = *name++ - '0';
  1623. if (c > 9)
  1624. goto out;
  1625. if (n >= (~0U-9)/10)
  1626. goto out;
  1627. n *= 10;
  1628. n += c;
  1629. }
  1630. return n;
  1631. out:
  1632. return ~0U;
  1633. }
  1634. #define PROC_FDINFO_MAX 64
  1635. static int proc_fd_info(struct inode *inode, struct path *path, char *info)
  1636. {
  1637. struct task_struct *task = get_proc_task(inode);
  1638. struct files_struct *files = NULL;
  1639. struct file *file;
  1640. int fd = proc_fd(inode);
  1641. if (task) {
  1642. files = get_files_struct(task);
  1643. put_task_struct(task);
  1644. }
  1645. if (files) {
  1646. /*
  1647. * We are not taking a ref to the file structure, so we must
  1648. * hold ->file_lock.
  1649. */
  1650. spin_lock(&files->file_lock);
  1651. file = fcheck_files(files, fd);
  1652. if (file) {
  1653. if (path) {
  1654. *path = file->f_path;
  1655. path_get(&file->f_path);
  1656. }
  1657. if (info)
  1658. snprintf(info, PROC_FDINFO_MAX,
  1659. "pos:\t%lli\n"
  1660. "flags:\t0%o\n",
  1661. (long long) file->f_pos,
  1662. file->f_flags);
  1663. spin_unlock(&files->file_lock);
  1664. put_files_struct(files);
  1665. return 0;
  1666. }
  1667. spin_unlock(&files->file_lock);
  1668. put_files_struct(files);
  1669. }
  1670. return -ENOENT;
  1671. }
  1672. static int proc_fd_link(struct inode *inode, struct path *path)
  1673. {
  1674. return proc_fd_info(inode, path, NULL);
  1675. }
  1676. static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
  1677. {
  1678. struct inode *inode;
  1679. struct task_struct *task;
  1680. int fd;
  1681. struct files_struct *files;
  1682. const struct cred *cred;
  1683. if (nd && nd->flags & LOOKUP_RCU)
  1684. return -ECHILD;
  1685. inode = dentry->d_inode;
  1686. task = get_proc_task(inode);
  1687. fd = proc_fd(inode);
  1688. if (task) {
  1689. files = get_files_struct(task);
  1690. if (files) {
  1691. rcu_read_lock();
  1692. if (fcheck_files(files, fd)) {
  1693. rcu_read_unlock();
  1694. put_files_struct(files);
  1695. if (task_dumpable(task)) {
  1696. rcu_read_lock();
  1697. cred = __task_cred(task);
  1698. inode->i_uid = cred->euid;
  1699. inode->i_gid = cred->egid;
  1700. rcu_read_unlock();
  1701. } else {
  1702. inode->i_uid = 0;
  1703. inode->i_gid = 0;
  1704. }
  1705. inode->i_mode &= ~(S_ISUID | S_ISGID);
  1706. security_task_to_inode(task, inode);
  1707. put_task_struct(task);
  1708. return 1;
  1709. }
  1710. rcu_read_unlock();
  1711. put_files_struct(files);
  1712. }
  1713. put_task_struct(task);
  1714. }
  1715. d_drop(dentry);
  1716. return 0;
  1717. }
  1718. static const struct dentry_operations tid_fd_dentry_operations =
  1719. {
  1720. .d_revalidate = tid_fd_revalidate,
  1721. .d_delete = pid_delete_dentry,
  1722. };
  1723. static struct dentry *proc_fd_instantiate(struct inode *dir,
  1724. struct dentry *dentry, struct task_struct *task, const void *ptr)
  1725. {
  1726. unsigned fd = *(const unsigned *)ptr;
  1727. struct file *file;
  1728. struct files_struct *files;
  1729. struct inode *inode;
  1730. struct proc_inode *ei;
  1731. struct dentry *error = ERR_PTR(-ENOENT);
  1732. inode = proc_pid_make_inode(dir->i_sb, task);
  1733. if (!inode)
  1734. goto out;
  1735. ei = PROC_I(inode);
  1736. ei->fd = fd;
  1737. files = get_files_struct(task);
  1738. if (!files)
  1739. goto out_iput;
  1740. inode->i_mode = S_IFLNK;
  1741. /*
  1742. * We are not taking a ref to the file structure, so we must
  1743. * hold ->file_lock.
  1744. */
  1745. spin_lock(&files->file_lock);
  1746. file = fcheck_files(files, fd);
  1747. if (!file)
  1748. goto out_unlock;
  1749. if (file->f_mode & FMODE_READ)
  1750. inode->i_mode |= S_IRUSR | S_IXUSR;
  1751. if (file->f_mode & FMODE_WRITE)
  1752. inode->i_mode |= S_IWUSR | S_IXUSR;
  1753. spin_unlock(&files->file_lock);
  1754. put_files_struct(files);
  1755. inode->i_op = &proc_pid_link_inode_operations;
  1756. inode->i_size = 64;
  1757. ei->op.proc_get_link = proc_fd_link;
  1758. d_set_d_op(dentry, &tid_fd_dentry_operations);
  1759. d_add(dentry, inode);
  1760. /* Close the race of the process dying before we return the dentry */
  1761. if (tid_fd_revalidate(dentry, NULL))
  1762. error = NULL;
  1763. out:
  1764. return error;
  1765. out_unlock:
  1766. spin_unlock(&files->file_lock);
  1767. put_files_struct(files);
  1768. out_iput:
  1769. iput(inode);
  1770. goto out;
  1771. }
  1772. static struct dentry *proc_lookupfd_common(struct inode *dir,
  1773. struct dentry *dentry,
  1774. instantiate_t instantiate)
  1775. {
  1776. struct task_struct *task = get_proc_task(dir);
  1777. unsigned fd = name_to_int(dentry);
  1778. struct dentry *result = ERR_PTR(-ENOENT);
  1779. if (!task)
  1780. goto out_no_task;
  1781. if (fd == ~0U)
  1782. goto out;
  1783. result = instantiate(dir, dentry, task, &fd);
  1784. out:
  1785. put_task_struct(task);
  1786. out_no_task:
  1787. return result;
  1788. }
  1789. static int proc_readfd_common(struct file * filp, void * dirent,
  1790. filldir_t filldir, instantiate_t instantiate)
  1791. {
  1792. struct dentry *dentry = filp->f_path.dentry;
  1793. struct inode *inode = dentry->d_inode;
  1794. struct task_struct *p = get_proc_task(inode);
  1795. unsigned int fd, ino;
  1796. int retval;
  1797. struct files_struct * files;
  1798. retval = -ENOENT;
  1799. if (!p)
  1800. goto out_no_task;
  1801. retval = 0;
  1802. fd = filp->f_pos;
  1803. switch (fd) {
  1804. case 0:
  1805. if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
  1806. goto out;
  1807. filp->f_pos++;
  1808. case 1:
  1809. ino = parent_ino(dentry);
  1810. if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
  1811. goto out;
  1812. filp->f_pos++;
  1813. default:
  1814. files = get_files_struct(p);
  1815. if (!files)
  1816. goto out;
  1817. rcu_read_lock();
  1818. for (fd = filp->f_pos-2;
  1819. fd < files_fdtable(files)->max_fds;
  1820. fd++, filp->f_pos++) {
  1821. char name[PROC_NUMBUF];
  1822. int len;
  1823. if (!fcheck_files(files, fd))
  1824. continue;
  1825. rcu_read_unlock();
  1826. len = snprintf(name, sizeof(name), "%d", fd);
  1827. if (proc_fill_cache(filp, dirent, filldir,
  1828. name, len, instantiate,
  1829. p, &fd) < 0) {
  1830. rcu_read_lock();
  1831. break;
  1832. }
  1833. rcu_read_lock();
  1834. }
  1835. rcu_read_unlock();
  1836. put_files_struct(files);
  1837. }
  1838. out:
  1839. put_task_struct(p);
  1840. out_no_task:
  1841. return retval;
  1842. }
  1843. static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
  1844. struct nameidata *nd)
  1845. {
  1846. return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
  1847. }
  1848. static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
  1849. {
  1850. return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
  1851. }
  1852. static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
  1853. size_t len, loff_t *ppos)
  1854. {
  1855. char tmp[PROC_FDINFO_MAX];
  1856. int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
  1857. if (!err)
  1858. err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
  1859. return err;
  1860. }
  1861. static const struct file_operations proc_fdinfo_file_operations = {
  1862. .open = nonseekable_open,
  1863. .read = proc_fdinfo_read,
  1864. .llseek = no_llseek,
  1865. };
  1866. static const struct file_operations proc_fd_operations = {
  1867. .read = generic_read_dir,
  1868. .readdir = proc_readfd,
  1869. .llseek = default_llseek,
  1870. };
  1871. /*
  1872. * /proc/pid/fd needs a special permission handler so that a process can still
  1873. * access /proc/self/fd after it has executed a setuid().
  1874. */
  1875. static int proc_fd_permission(struct inode *inode, int mask, unsigned int flags)
  1876. {
  1877. int rv;
  1878. if (flags & IPERM_FLAG_RCU)
  1879. return -ECHILD;
  1880. rv = generic_permission(inode, mask, flags, NULL);
  1881. if (rv == 0)
  1882. return 0;
  1883. if (task_pid(current) == proc_pid(inode))
  1884. rv = 0;
  1885. return rv;
  1886. }
  1887. /*
  1888. * proc directories can do almost nothing..
  1889. */
  1890. static const struct inode_operations proc_fd_inode_operations = {
  1891. .lookup = proc_lookupfd,
  1892. .permission = proc_fd_permission,
  1893. .setattr = proc_setattr,
  1894. };
  1895. static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
  1896. struct dentry *dentry, struct task_struct *task, const void *ptr)
  1897. {
  1898. unsigned fd = *(unsigned *)ptr;
  1899. struct inode *inode;
  1900. struct proc_inode *ei;
  1901. struct dentry *error = ERR_PTR(-ENOENT);
  1902. inode = proc_pid_make_inode(dir->i_sb, task);
  1903. if (!inode)
  1904. goto out;
  1905. ei = PROC_I(inode);
  1906. ei->fd = fd;
  1907. inode->i_mode = S_IFREG | S_IRUSR;
  1908. inode->i_fop = &proc_fdinfo_file_operations;
  1909. d_set_d_op(dentry, &tid_fd_dentry_operations);
  1910. d_add(dentry, inode);
  1911. /* Close the race of the process dying before we return the dentry */
  1912. if (tid_fd_revalidate(dentry, NULL))
  1913. error = NULL;
  1914. out:
  1915. return error;
  1916. }
  1917. static struct dentry *proc_lookupfdinfo(struct inode *dir,
  1918. struct dentry *dentry,
  1919. struct nameidata *nd)
  1920. {
  1921. return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
  1922. }
  1923. static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
  1924. {
  1925. return proc_readfd_common(filp, dirent, filldir,
  1926. proc_fdinfo_instantiate);
  1927. }
  1928. static const struct file_operations proc_fdinfo_operations = {
  1929. .read = generic_read_dir,
  1930. .readdir = proc_readfdinfo,
  1931. .llseek = default_llseek,
  1932. };
  1933. /*
  1934. * proc directories can do almost nothing..
  1935. */
  1936. static const struct inode_operations proc_fdinfo_inode_operations = {
  1937. .lookup = proc_lookupfdinfo,
  1938. .setattr = proc_setattr,
  1939. };
  1940. static struct dentry *proc_pident_instantiate(struct inode *dir,
  1941. struct dentry *dentry, struct task_struct *task, const void *ptr)
  1942. {
  1943. const struct pid_entry *p = ptr;
  1944. struct inode *inode;
  1945. struct proc_inode *ei;
  1946. struct dentry *error = ERR_PTR(-ENOENT);
  1947. inode = proc_pid_make_inode(dir->i_sb, task);
  1948. if (!inode)
  1949. goto out;
  1950. ei = PROC_I(inode);
  1951. inode->i_mode = p->mode;
  1952. if (S_ISDIR(inode->i_mode))
  1953. inode->i_nlink = 2; /* Use getattr to fix if necessary */
  1954. if (p->iop)
  1955. inode->i_op = p->iop;
  1956. if (p->fop)
  1957. inode->i_fop = p->fop;
  1958. ei->op = p->op;
  1959. d_set_d_op(dentry, &pid_dentry_operations);
  1960. d_add(dentry, inode);
  1961. /* Close the race of the process dying before we return the dentry */
  1962. if (pid_revalidate(dentry, NULL))
  1963. error = NULL;
  1964. out:
  1965. return error;
  1966. }
  1967. static struct dentry *proc_pident_lookup(struct inode *dir,
  1968. struct dentry *dentry,
  1969. const struct pid_entry *ents,
  1970. unsigned int nents)
  1971. {
  1972. struct dentry *error;
  1973. struct task_struct *task = get_proc_task(dir);
  1974. const struct pid_entry *p, *last;
  1975. error = ERR_PTR(-ENOENT);
  1976. if (!task)
  1977. goto out_no_task;
  1978. /*
  1979. * Yes, it does not scale. And it should not. Don't add
  1980. * new entries into /proc/<tgid>/ without very good reasons.
  1981. */
  1982. last = &ents[nents - 1];
  1983. for (p = ents; p <= last; p++) {
  1984. if (p->len != dentry->d_name.len)
  1985. continue;
  1986. if (!memcmp(dentry->d_name.name, p->name, p->len))
  1987. break;
  1988. }
  1989. if (p > last)
  1990. goto out;
  1991. error = proc_pident_instantiate(dir, dentry, task, p);
  1992. out:
  1993. put_task_struct(task);
  1994. out_no_task:
  1995. return error;
  1996. }
  1997. static int proc_pident_fill_cache(struct file *filp, void *dirent,
  1998. filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
  1999. {
  2000. return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
  2001. proc_pident_instantiate, task, p);
  2002. }
  2003. static int proc_pident_readdir(struct file *filp,
  2004. void *dirent, filldir_t filldir,
  2005. const struct pid_entry *ents, unsigned int nents)
  2006. {
  2007. int i;
  2008. struct dentry *dentry = filp->f_path.dentry;
  2009. struct inode *inode = dentry->d_inode;
  2010. struct task_struct *task = get_proc_task(inode);
  2011. const struct pid_entry *p, *last;
  2012. ino_t ino;
  2013. int ret;
  2014. ret = -ENOENT;
  2015. if (!task)
  2016. goto out_no_task;
  2017. ret = 0;
  2018. i = filp->f_pos;
  2019. switch (i) {
  2020. case 0:
  2021. ino = inode->i_ino;
  2022. if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
  2023. goto out;
  2024. i++;
  2025. filp->f_pos++;
  2026. /* fall through */
  2027. case 1:
  2028. ino = parent_ino(dentry);
  2029. if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
  2030. goto out;
  2031. i++;
  2032. filp->f_pos++;
  2033. /* fall through */
  2034. default:
  2035. i -= 2;
  2036. if (i >= nents) {
  2037. ret = 1;
  2038. goto out;
  2039. }
  2040. p = ents + i;
  2041. last = &ents[nents - 1];
  2042. while (p <= last) {
  2043. if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
  2044. goto out;
  2045. filp->f_pos++;
  2046. p++;
  2047. }
  2048. }
  2049. ret = 1;
  2050. out:
  2051. put_task_struct(task);
  2052. out_no_task:
  2053. return ret;
  2054. }
  2055. #ifdef CONFIG_SECURITY
  2056. static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
  2057. size_t count, loff_t *ppos)
  2058. {
  2059. struct inode * inode = file->f_path.dentry->d_inode;
  2060. char *p = NULL;
  2061. ssize_t length;
  2062. struct task_struct *task = get_proc_task(inode);
  2063. if (!task)
  2064. return -ESRCH;
  2065. length = security_getprocattr(task,
  2066. (char*)file->f_path.dentry->d_name.name,
  2067. &p);
  2068. put_task_struct(task);
  2069. if (length > 0)
  2070. length = simple_read_from_buffer(buf, count, ppos, p, length);
  2071. kfree(p);
  2072. return length;
  2073. }
  2074. static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
  2075. size_t count, loff_t *ppos)
  2076. {
  2077. struct inode * inode = file->f_path.dentry->d_inode;
  2078. char *page;
  2079. ssize_t length;
  2080. struct task_struct *task = get_proc_task(inode);
  2081. length = -ESRCH;
  2082. if (!task)
  2083. goto out_no_task;
  2084. if (count > PAGE_SIZE)
  2085. count = PAGE_SIZE;
  2086. /* No partial writes. */
  2087. length = -EINVAL;
  2088. if (*ppos != 0)
  2089. goto out;
  2090. length = -ENOMEM;
  2091. page = (char*)__get_free_page(GFP_TEMPORARY);
  2092. if (!page)
  2093. goto out;
  2094. length = -EFAULT;
  2095. if (copy_from_user(page, buf, count))
  2096. goto out_free;
  2097. /* Guard against adverse ptrace interaction */
  2098. length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
  2099. if (length < 0)
  2100. goto out_free;
  2101. length = security_setprocattr(task,
  2102. (char*)file->f_path.dentry->d_name.name,
  2103. (void*)page, count);
  2104. mutex_unlock(&task->signal->cred_guard_mutex);
  2105. out_free:
  2106. free_page((unsigned long) page);
  2107. out:
  2108. put_task_struct(task);
  2109. out_no_task:
  2110. return length;
  2111. }
  2112. static const struct file_operations proc_pid_attr_operations = {
  2113. .read = proc_pid_attr_read,
  2114. .write = proc_pid_attr_write,
  2115. .llseek = generic_file_llseek,
  2116. };
  2117. static const struct pid_entry attr_dir_stuff[] = {
  2118. REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
  2119. REG("prev", S_IRUGO, proc_pid_attr_operations),
  2120. REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
  2121. REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
  2122. REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
  2123. REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
  2124. };
  2125. static int proc_attr_dir_readdir(struct file * filp,
  2126. void * dirent, filldir_t filldir)
  2127. {
  2128. return proc_pident_readdir(filp,dirent,filldir,
  2129. attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
  2130. }
  2131. static const struct file_operations proc_attr_dir_operations = {
  2132. .read = generic_read_dir,
  2133. .readdir = proc_attr_dir_readdir,
  2134. .llseek = default_llseek,
  2135. };
  2136. static struct dentry *proc_attr_dir_lookup(struct inode *dir,
  2137. struct dentry *dentry, struct nameidata *nd)
  2138. {
  2139. return proc_pident_lookup(dir, dentry,
  2140. attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
  2141. }
  2142. static const struct inode_operations proc_attr_dir_inode_operations = {
  2143. .lookup = proc_attr_dir_lookup,
  2144. .getattr = pid_getattr,
  2145. .setattr = proc_setattr,
  2146. };
  2147. #endif
  2148. #ifdef CONFIG_ELF_CORE
  2149. static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
  2150. size_t count, loff_t *ppos)
  2151. {
  2152. struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
  2153. struct mm_struct *mm;
  2154. char buffer[PROC_NUMBUF];
  2155. size_t len;
  2156. int ret;
  2157. if (!task)
  2158. return -ESRCH;
  2159. ret = 0;
  2160. mm = get_task_mm(task);
  2161. if (mm) {
  2162. len = snprintf(buffer, sizeof(buffer), "%08lx\n",
  2163. ((mm->flags & MMF_DUMP_FILTER_MASK) >>
  2164. MMF_DUMP_FILTER_SHIFT));
  2165. mmput(mm);
  2166. ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
  2167. }
  2168. put_task_struct(task);
  2169. return ret;
  2170. }
  2171. static ssize_t proc_coredump_filter_write(struct file *file,
  2172. const char __user *buf,
  2173. size_t count,
  2174. loff_t *ppos)
  2175. {
  2176. struct task_struct *task;
  2177. struct mm_struct *mm;
  2178. char buffer[PROC_NUMBUF], *end;
  2179. unsigned int val;
  2180. int ret;
  2181. int i;
  2182. unsigned long mask;
  2183. ret = -EFAULT;
  2184. memset(buffer, 0, sizeof(buffer));
  2185. if (count > sizeof(buffer) - 1)
  2186. count = sizeof(buffer) - 1;
  2187. if (copy_from_user(buffer, buf, count))
  2188. goto out_no_task;
  2189. ret = -EINVAL;
  2190. val = (unsigned int)simple_strtoul(buffer, &end, 0);
  2191. if (*end == '\n')
  2192. end++;
  2193. if (end - buffer == 0)
  2194. goto out_no_task;
  2195. ret = -ESRCH;
  2196. task = get_proc_task(file->f_dentry->d_inode);
  2197. if (!task)
  2198. goto out_no_task;
  2199. ret = end - buffer;
  2200. mm = get_task_mm(task);
  2201. if (!mm)
  2202. goto out_no_mm;
  2203. for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
  2204. if (val & mask)
  2205. set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
  2206. else
  2207. clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
  2208. }
  2209. mmput(mm);
  2210. out_no_mm:
  2211. put_task_struct(task);
  2212. out_no_task:
  2213. return ret;
  2214. }
  2215. static const struct file_operations proc_coredump_filter_operations = {
  2216. .read = proc_coredump_filter_read,
  2217. .write = proc_coredump_filter_write,
  2218. .llseek = generic_file_llseek,
  2219. };
  2220. #endif
  2221. /*
  2222. * /proc/self:
  2223. */
  2224. static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
  2225. int buflen)
  2226. {
  2227. struct pid_namespace *ns = dentry->d_sb->s_fs_info;
  2228. pid_t tgid = task_tgid_nr_ns(current, ns);
  2229. char tmp[PROC_NUMBUF];
  2230. if (!tgid)
  2231. return -ENOENT;
  2232. sprintf(tmp, "%d", tgid);
  2233. return vfs_readlink(dentry,buffer,buflen,tmp);
  2234. }
  2235. static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
  2236. {
  2237. struct pid_namespace *ns = dentry->d_sb->s_fs_info;
  2238. pid_t tgid = task_tgid_nr_ns(current, ns);
  2239. char *name = ERR_PTR(-ENOENT);
  2240. if (tgid) {
  2241. name = __getname();
  2242. if (!name)
  2243. name = ERR_PTR(-ENOMEM);
  2244. else
  2245. sprintf(name, "%d", tgid);
  2246. }
  2247. nd_set_link(nd, name);
  2248. return NULL;
  2249. }
  2250. static void proc_self_put_link(struct dentry *dentry, struct nameidata *nd,
  2251. void *cookie)
  2252. {
  2253. char *s = nd_get_link(nd);
  2254. if (!IS_ERR(s))
  2255. __putname(s);
  2256. }
  2257. static const struct inode_operations proc_self_inode_operations = {
  2258. .readlink = proc_self_readlink,
  2259. .follow_link = proc_self_follow_link,
  2260. .put_link = proc_self_put_link,
  2261. };
  2262. /*
  2263. * proc base
  2264. *
  2265. * These are the directory entries in the root directory of /proc
  2266. * that properly belong to the /proc filesystem, as they describe
  2267. * describe something that is process related.
  2268. */
  2269. static const struct pid_entry proc_base_stuff[] = {
  2270. NOD("self", S_IFLNK|S_IRWXUGO,
  2271. &proc_self_inode_operations, NULL, {}),
  2272. };
  2273. static struct dentry *proc_base_instantiate(struct inode *dir,
  2274. struct dentry *dentry, struct task_struct *task, const void *ptr)
  2275. {
  2276. const struct pid_entry *p = ptr;
  2277. struct inode *inode;
  2278. struct proc_inode *ei;
  2279. struct dentry *error;
  2280. /* Allocate the inode */
  2281. error = ERR_PTR(-ENOMEM);
  2282. inode = new_inode(dir->i_sb);
  2283. if (!inode)
  2284. goto out;
  2285. /* Initialize the inode */
  2286. ei = PROC_I(inode);
  2287. inode->i_ino = get_next_ino();
  2288. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  2289. /*
  2290. * grab the reference to the task.
  2291. */
  2292. ei->pid = get_task_pid(task, PIDTYPE_PID);
  2293. if (!ei->pid)
  2294. goto out_iput;
  2295. inode->i_mode = p->mode;
  2296. if (S_ISDIR(inode->i_mode))
  2297. inode->i_nlink = 2;
  2298. if (S_ISLNK(inode->i_mode))
  2299. inode->i_size = 64;
  2300. if (p->iop)
  2301. inode->i_op = p->iop;
  2302. if (p->fop)
  2303. inode->i_fop = p->fop;
  2304. ei->op = p->op;
  2305. d_add(dentry, inode);
  2306. error = NULL;
  2307. out:
  2308. return error;
  2309. out_iput:
  2310. iput(inode);
  2311. goto out;
  2312. }
  2313. static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
  2314. {
  2315. struct dentry *error;
  2316. struct task_struct *task = get_proc_task(dir);
  2317. const struct pid_entry *p, *last;
  2318. error = ERR_PTR(-ENOENT);
  2319. if (!task)
  2320. goto out_no_task;
  2321. /* Lookup the directory entry */
  2322. last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
  2323. for (p = proc_base_stuff; p <= last; p++) {
  2324. if (p->len != dentry->d_name.len)
  2325. continue;
  2326. if (!memcmp(dentry->d_name.name, p->name, p->len))
  2327. break;
  2328. }
  2329. if (p > last)
  2330. goto out;
  2331. error = proc_base_instantiate(dir, dentry, task, p);
  2332. out:
  2333. put_task_struct(task);
  2334. out_no_task:
  2335. return error;
  2336. }
  2337. static int proc_base_fill_cache(struct file *filp, void *dirent,
  2338. filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
  2339. {
  2340. return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
  2341. proc_base_instantiate, task, p);
  2342. }
  2343. #ifdef CONFIG_TASK_IO_ACCOUNTING
  2344. static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
  2345. {
  2346. struct task_io_accounting acct = task->ioac;
  2347. unsigned long flags;
  2348. if (whole && lock_task_sighand(task, &flags)) {
  2349. struct task_struct *t = task;
  2350. task_io_accounting_add(&acct, &task->signal->ioac);
  2351. while_each_thread(task, t)
  2352. task_io_accounting_add(&acct, &t->ioac);
  2353. unlock_task_sighand(task, &flags);
  2354. }
  2355. return sprintf(buffer,
  2356. "rchar: %llu\n"
  2357. "wchar: %llu\n"
  2358. "syscr: %llu\n"
  2359. "syscw: %llu\n"
  2360. "read_bytes: %llu\n"
  2361. "write_bytes: %llu\n"
  2362. "cancelled_write_bytes: %llu\n",
  2363. (unsigned long long)acct.rchar,
  2364. (unsigned long long)acct.wchar,
  2365. (unsigned long long)acct.syscr,
  2366. (unsigned long long)acct.syscw,
  2367. (unsigned long long)acct.read_bytes,
  2368. (unsigned long long)acct.write_bytes,
  2369. (unsigned long long)acct.cancelled_write_bytes);
  2370. }
  2371. static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
  2372. {
  2373. return do_io_accounting(task, buffer, 0);
  2374. }
  2375. static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
  2376. {
  2377. return do_io_accounting(task, buffer, 1);
  2378. }
  2379. #endif /* CONFIG_TASK_IO_ACCOUNTING */
  2380. static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
  2381. struct pid *pid, struct task_struct *task)
  2382. {
  2383. seq_printf(m, "%08x\n", task->personality);
  2384. return 0;
  2385. }
  2386. /*
  2387. * Thread groups
  2388. */
  2389. static const struct file_operations proc_task_operations;
  2390. static const struct inode_operations proc_task_inode_operations;
  2391. static const struct pid_entry tgid_base_stuff[] = {
  2392. DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
  2393. DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
  2394. DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
  2395. #ifdef CONFIG_NET
  2396. DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
  2397. #endif
  2398. REG("environ", S_IRUSR, proc_environ_operations),
  2399. INF("auxv", S_IRUSR, proc_pid_auxv),
  2400. ONE("status", S_IRUGO, proc_pid_status),
  2401. ONE("personality", S_IRUSR, proc_pid_personality),
  2402. INF("limits", S_IRUGO, proc_pid_limits),
  2403. #ifdef CONFIG_SCHED_DEBUG
  2404. REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
  2405. #endif
  2406. #ifdef CONFIG_SCHED_AUTOGROUP
  2407. REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
  2408. #endif
  2409. REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
  2410. #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
  2411. INF("syscall", S_IRUSR, proc_pid_syscall),
  2412. #endif
  2413. INF("cmdline", S_IRUGO, proc_pid_cmdline),
  2414. ONE("stat", S_IRUGO, proc_tgid_stat),
  2415. ONE("statm", S_IRUGO, proc_pid_statm),
  2416. REG("maps", S_IRUGO, proc_maps_operations),
  2417. #ifdef CONFIG_NUMA
  2418. REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
  2419. #endif
  2420. REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
  2421. LNK("cwd", proc_cwd_link),
  2422. LNK("root", proc_root_link),
  2423. LNK("exe", proc_exe_link),
  2424. REG("mounts", S_IRUGO, proc_mounts_operations),
  2425. REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
  2426. REG("mountstats", S_IRUSR, proc_mountstats_operations),
  2427. #ifdef CONFIG_PROC_PAGE_MONITOR
  2428. REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
  2429. REG("smaps", S_IRUGO, proc_smaps_operations),
  2430. REG("pagemap", S_IRUGO, proc_pagemap_operations),
  2431. #endif
  2432. #ifdef CONFIG_SECURITY
  2433. DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
  2434. #endif
  2435. #ifdef CONFIG_KALLSYMS
  2436. INF("wchan", S_IRUGO, proc_pid_wchan),
  2437. #endif
  2438. #ifdef CONFIG_STACKTRACE
  2439. ONE("stack", S_IRUSR, proc_pid_stack),
  2440. #endif
  2441. #ifdef CONFIG_SCHEDSTATS
  2442. INF("schedstat", S_IRUGO, proc_pid_schedstat),
  2443. #endif
  2444. #ifdef CONFIG_LATENCYTOP
  2445. REG("latency", S_IRUGO, proc_lstats_operations),
  2446. #endif
  2447. #ifdef CONFIG_PROC_PID_CPUSET
  2448. REG("cpuset", S_IRUGO, proc_cpuset_operations),
  2449. #endif
  2450. #ifdef CONFIG_CGROUPS
  2451. REG("cgroup", S_IRUGO, proc_cgroup_operations),
  2452. #endif
  2453. INF("oom_score", S_IRUGO, proc_oom_score),
  2454. REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
  2455. REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
  2456. #ifdef CONFIG_AUDITSYSCALL
  2457. REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
  2458. REG("sessionid", S_IRUGO, proc_sessionid_operations),
  2459. #endif
  2460. #ifdef CONFIG_FAULT_INJECTION
  2461. REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
  2462. #endif
  2463. #ifdef CONFIG_ELF_CORE
  2464. REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
  2465. #endif
  2466. #ifdef CONFIG_TASK_IO_ACCOUNTING
  2467. INF("io", S_IRUGO, proc_tgid_io_accounting),
  2468. #endif
  2469. };
  2470. static int proc_tgid_base_readdir(struct file * filp,
  2471. void * dirent, filldir_t filldir)
  2472. {
  2473. return proc_pident_readdir(filp,dirent,filldir,
  2474. tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
  2475. }
  2476. static const struct file_operations proc_tgid_base_operations = {
  2477. .read = generic_read_dir,
  2478. .readdir = proc_tgid_base_readdir,
  2479. .llseek = default_llseek,
  2480. };
  2481. static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
  2482. return proc_pident_lookup(dir, dentry,
  2483. tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
  2484. }
  2485. static const struct inode_operations proc_tgid_base_inode_operations = {
  2486. .lookup = proc_tgid_base_lookup,
  2487. .getattr = pid_getattr,
  2488. .setattr = proc_setattr,
  2489. };
  2490. static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
  2491. {
  2492. struct dentry *dentry, *leader, *dir;
  2493. char buf[PROC_NUMBUF];
  2494. struct qstr name;
  2495. name.name = buf;
  2496. name.len = snprintf(buf, sizeof(buf), "%d", pid);
  2497. dentry = d_hash_and_lookup(mnt->mnt_root, &name);
  2498. if (dentry) {
  2499. shrink_dcache_parent(dentry);
  2500. d_drop(dentry);
  2501. dput(dentry);
  2502. }
  2503. name.name = buf;
  2504. name.len = snprintf(buf, sizeof(buf), "%d", tgid);
  2505. leader = d_hash_and_lookup(mnt->mnt_root, &name);
  2506. if (!leader)
  2507. goto out;
  2508. name.name = "task";
  2509. name.len = strlen(name.name);
  2510. dir = d_hash_and_lookup(leader, &name);
  2511. if (!dir)
  2512. goto out_put_leader;
  2513. name.name = buf;
  2514. name.len = snprintf(buf, sizeof(buf), "%d", pid);
  2515. dentry = d_hash_and_lookup(dir, &name);
  2516. if (dentry) {
  2517. shrink_dcache_parent(dentry);
  2518. d_drop(dentry);
  2519. dput(dentry);
  2520. }
  2521. dput(dir);
  2522. out_put_leader:
  2523. dput(leader);
  2524. out:
  2525. return;
  2526. }
  2527. /**
  2528. * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
  2529. * @task: task that should be flushed.
  2530. *
  2531. * When flushing dentries from proc, one needs to flush them from global
  2532. * proc (proc_mnt) and from all the namespaces' procs this task was seen
  2533. * in. This call is supposed to do all of this job.
  2534. *
  2535. * Looks in the dcache for
  2536. * /proc/@pid
  2537. * /proc/@tgid/task/@pid
  2538. * if either directory is present flushes it and all of it'ts children
  2539. * from the dcache.
  2540. *
  2541. * It is safe and reasonable to cache /proc entries for a task until
  2542. * that task exits. After that they just clog up the dcache with
  2543. * useless entries, possibly causing useful dcache entries to be
  2544. * flushed instead. This routine is proved to flush those useless
  2545. * dcache entries at process exit time.
  2546. *
  2547. * NOTE: This routine is just an optimization so it does not guarantee
  2548. * that no dcache entries will exist at process exit time it
  2549. * just makes it very unlikely that any will persist.
  2550. */
  2551. void proc_flush_task(struct task_struct *task)
  2552. {
  2553. int i;
  2554. struct pid *pid, *tgid;
  2555. struct upid *upid;
  2556. pid = task_pid(task);
  2557. tgid = task_tgid(task);
  2558. for (i = 0; i <= pid->level; i++) {
  2559. upid = &pid->numbers[i];
  2560. proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
  2561. tgid->numbers[i].nr);
  2562. }
  2563. upid = &pid->numbers[pid->level];
  2564. if (upid->nr == 1)
  2565. pid_ns_release_proc(upid->ns);
  2566. }
  2567. static struct dentry *proc_pid_instantiate(struct inode *dir,
  2568. struct dentry * dentry,
  2569. struct task_struct *task, const void *ptr)
  2570. {
  2571. struct dentry *error = ERR_PTR(-ENOENT);
  2572. struct inode *inode;
  2573. inode = proc_pid_make_inode(dir->i_sb, task);
  2574. if (!inode)
  2575. goto out;
  2576. inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
  2577. inode->i_op = &proc_tgid_base_inode_operations;
  2578. inode->i_fop = &proc_tgid_base_operations;
  2579. inode->i_flags|=S_IMMUTABLE;
  2580. inode->i_nlink = 2 + pid_entry_count_dirs(tgid_base_stuff,
  2581. ARRAY_SIZE(tgid_base_stuff));
  2582. d_set_d_op(dentry, &pid_dentry_operations);
  2583. d_add(dentry, inode);
  2584. /* Close the race of the process dying before we return the dentry */
  2585. if (pid_revalidate(dentry, NULL))
  2586. error = NULL;
  2587. out:
  2588. return error;
  2589. }
  2590. struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
  2591. {
  2592. struct dentry *result;
  2593. struct task_struct *task;
  2594. unsigned tgid;
  2595. struct pid_namespace *ns;
  2596. result = proc_base_lookup(dir, dentry);
  2597. if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
  2598. goto out;
  2599. tgid = name_to_int(dentry);
  2600. if (tgid == ~0U)
  2601. goto out;
  2602. ns = dentry->d_sb->s_fs_info;
  2603. rcu_read_lock();
  2604. task = find_task_by_pid_ns(tgid, ns);
  2605. if (task)
  2606. get_task_struct(task);
  2607. rcu_read_unlock();
  2608. if (!task)
  2609. goto out;
  2610. result = proc_pid_instantiate(dir, dentry, task, NULL);
  2611. put_task_struct(task);
  2612. out:
  2613. return result;
  2614. }
  2615. /*
  2616. * Find the first task with tgid >= tgid
  2617. *
  2618. */
  2619. struct tgid_iter {
  2620. unsigned int tgid;
  2621. struct task_struct *task;
  2622. };
  2623. static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
  2624. {
  2625. struct pid *pid;
  2626. if (iter.task)
  2627. put_task_struct(iter.task);
  2628. rcu_read_lock();
  2629. retry:
  2630. iter.task = NULL;
  2631. pid = find_ge_pid(iter.tgid, ns);
  2632. if (pid) {
  2633. iter.tgid = pid_nr_ns(pid, ns);
  2634. iter.task = pid_task(pid, PIDTYPE_PID);
  2635. /* What we to know is if the pid we have find is the
  2636. * pid of a thread_group_leader. Testing for task
  2637. * being a thread_group_leader is the obvious thing
  2638. * todo but there is a window when it fails, due to
  2639. * the pid transfer logic in de_thread.
  2640. *
  2641. * So we perform the straight forward test of seeing
  2642. * if the pid we have found is the pid of a thread
  2643. * group leader, and don't worry if the task we have
  2644. * found doesn't happen to be a thread group leader.
  2645. * As we don't care in the case of readdir.
  2646. */
  2647. if (!iter.task || !has_group_leader_pid(iter.task)) {
  2648. iter.tgid += 1;
  2649. goto retry;
  2650. }
  2651. get_task_struct(iter.task);
  2652. }
  2653. rcu_read_unlock();
  2654. return iter;
  2655. }
  2656. #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
  2657. static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
  2658. struct tgid_iter iter)
  2659. {
  2660. char name[PROC_NUMBUF];
  2661. int len = snprintf(name, sizeof(name), "%d", iter.tgid);
  2662. return proc_fill_cache(filp, dirent, filldir, name, len,
  2663. proc_pid_instantiate, iter.task, NULL);
  2664. }
  2665. /* for the /proc/ directory itself, after non-process stuff has been done */
  2666. int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
  2667. {
  2668. unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
  2669. struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
  2670. struct tgid_iter iter;
  2671. struct pid_namespace *ns;
  2672. if (!reaper)
  2673. goto out_no_task;
  2674. for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
  2675. const struct pid_entry *p = &proc_base_stuff[nr];
  2676. if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
  2677. goto out;
  2678. }
  2679. ns = filp->f_dentry->d_sb->s_fs_info;
  2680. iter.task = NULL;
  2681. iter.tgid = filp->f_pos - TGID_OFFSET;
  2682. for (iter = next_tgid(ns, iter);
  2683. iter.task;
  2684. iter.tgid += 1, iter = next_tgid(ns, iter)) {
  2685. filp->f_pos = iter.tgid + TGID_OFFSET;
  2686. if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) {
  2687. put_task_struct(iter.task);
  2688. goto out;
  2689. }
  2690. }
  2691. filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
  2692. out:
  2693. put_task_struct(reaper);
  2694. out_no_task:
  2695. return 0;
  2696. }
  2697. /*
  2698. * Tasks
  2699. */
  2700. static const struct pid_entry tid_base_stuff[] = {
  2701. DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
  2702. DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
  2703. REG("environ", S_IRUSR, proc_environ_operations),
  2704. INF("auxv", S_IRUSR, proc_pid_auxv),
  2705. ONE("status", S_IRUGO, proc_pid_status),
  2706. ONE("personality", S_IRUSR, proc_pid_personality),
  2707. INF("limits", S_IRUGO, proc_pid_limits),
  2708. #ifdef CONFIG_SCHED_DEBUG
  2709. REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
  2710. #endif
  2711. REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
  2712. #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
  2713. INF("syscall", S_IRUSR, proc_pid_syscall),
  2714. #endif
  2715. INF("cmdline", S_IRUGO, proc_pid_cmdline),
  2716. ONE("stat", S_IRUGO, proc_tid_stat),
  2717. ONE("statm", S_IRUGO, proc_pid_statm),
  2718. REG("maps", S_IRUGO, proc_maps_operations),
  2719. #ifdef CONFIG_NUMA
  2720. REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
  2721. #endif
  2722. REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
  2723. LNK("cwd", proc_cwd_link),
  2724. LNK("root", proc_root_link),
  2725. LNK("exe", proc_exe_link),
  2726. REG("mounts", S_IRUGO, proc_mounts_operations),
  2727. REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
  2728. #ifdef CONFIG_PROC_PAGE_MONITOR
  2729. REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
  2730. REG("smaps", S_IRUGO, proc_smaps_operations),
  2731. REG("pagemap", S_IRUGO, proc_pagemap_operations),
  2732. #endif
  2733. #ifdef CONFIG_SECURITY
  2734. DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
  2735. #endif
  2736. #ifdef CONFIG_KALLSYMS
  2737. INF("wchan", S_IRUGO, proc_pid_wchan),
  2738. #endif
  2739. #ifdef CONFIG_STACKTRACE
  2740. ONE("stack", S_IRUSR, proc_pid_stack),
  2741. #endif
  2742. #ifdef CONFIG_SCHEDSTATS
  2743. INF("schedstat", S_IRUGO, proc_pid_schedstat),
  2744. #endif
  2745. #ifdef CONFIG_LATENCYTOP
  2746. REG("latency", S_IRUGO, proc_lstats_operations),
  2747. #endif
  2748. #ifdef CONFIG_PROC_PID_CPUSET
  2749. REG("cpuset", S_IRUGO, proc_cpuset_operations),
  2750. #endif
  2751. #ifdef CONFIG_CGROUPS
  2752. REG("cgroup", S_IRUGO, proc_cgroup_operations),
  2753. #endif
  2754. INF("oom_score", S_IRUGO, proc_oom_score),
  2755. REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
  2756. REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
  2757. #ifdef CONFIG_AUDITSYSCALL
  2758. REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
  2759. REG("sessionid", S_IRUGO, proc_sessionid_operations),
  2760. #endif
  2761. #ifdef CONFIG_FAULT_INJECTION
  2762. REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
  2763. #endif
  2764. #ifdef CONFIG_TASK_IO_ACCOUNTING
  2765. INF("io", S_IRUGO, proc_tid_io_accounting),
  2766. #endif
  2767. };
  2768. static int proc_tid_base_readdir(struct file * filp,
  2769. void * dirent, filldir_t filldir)
  2770. {
  2771. return proc_pident_readdir(filp,dirent,filldir,
  2772. tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
  2773. }
  2774. static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
  2775. return proc_pident_lookup(dir, dentry,
  2776. tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
  2777. }
  2778. static const struct file_operations proc_tid_base_operations = {
  2779. .read = generic_read_dir,
  2780. .readdir = proc_tid_base_readdir,
  2781. .llseek = default_llseek,
  2782. };
  2783. static const struct inode_operations proc_tid_base_inode_operations = {
  2784. .lookup = proc_tid_base_lookup,
  2785. .getattr = pid_getattr,
  2786. .setattr = proc_setattr,
  2787. };
  2788. static struct dentry *proc_task_instantiate(struct inode *dir,
  2789. struct dentry *dentry, struct task_struct *task, const void *ptr)
  2790. {
  2791. struct dentry *error = ERR_PTR(-ENOENT);
  2792. struct inode *inode;
  2793. inode = proc_pid_make_inode(dir->i_sb, task);
  2794. if (!inode)
  2795. goto out;
  2796. inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
  2797. inode->i_op = &proc_tid_base_inode_operations;
  2798. inode->i_fop = &proc_tid_base_operations;
  2799. inode->i_flags|=S_IMMUTABLE;
  2800. inode->i_nlink = 2 + pid_entry_count_dirs(tid_base_stuff,
  2801. ARRAY_SIZE(tid_base_stuff));
  2802. d_set_d_op(dentry, &pid_dentry_operations);
  2803. d_add(dentry, inode);
  2804. /* Close the race of the process dying before we return the dentry */
  2805. if (pid_revalidate(dentry, NULL))
  2806. error = NULL;
  2807. out:
  2808. return error;
  2809. }
  2810. static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
  2811. {
  2812. struct dentry *result = ERR_PTR(-ENOENT);
  2813. struct task_struct *task;
  2814. struct task_struct *leader = get_proc_task(dir);
  2815. unsigned tid;
  2816. struct pid_namespace *ns;
  2817. if (!leader)
  2818. goto out_no_task;
  2819. tid = name_to_int(dentry);
  2820. if (tid == ~0U)
  2821. goto out;
  2822. ns = dentry->d_sb->s_fs_info;
  2823. rcu_read_lock();
  2824. task = find_task_by_pid_ns(tid, ns);
  2825. if (task)
  2826. get_task_struct(task);
  2827. rcu_read_unlock();
  2828. if (!task)
  2829. goto out;
  2830. if (!same_thread_group(leader, task))
  2831. goto out_drop_task;
  2832. result = proc_task_instantiate(dir, dentry, task, NULL);
  2833. out_drop_task:
  2834. put_task_struct(task);
  2835. out:
  2836. put_task_struct(leader);
  2837. out_no_task:
  2838. return result;
  2839. }
  2840. /*
  2841. * Find the first tid of a thread group to return to user space.
  2842. *
  2843. * Usually this is just the thread group leader, but if the users
  2844. * buffer was too small or there was a seek into the middle of the
  2845. * directory we have more work todo.
  2846. *
  2847. * In the case of a short read we start with find_task_by_pid.
  2848. *
  2849. * In the case of a seek we start with the leader and walk nr
  2850. * threads past it.
  2851. */
  2852. static struct task_struct *first_tid(struct task_struct *leader,
  2853. int tid, int nr, struct pid_namespace *ns)
  2854. {
  2855. struct task_struct *pos;
  2856. rcu_read_lock();
  2857. /* Attempt to start with the pid of a thread */
  2858. if (tid && (nr > 0)) {
  2859. pos = find_task_by_pid_ns(tid, ns);
  2860. if (pos && (pos->group_leader == leader))
  2861. goto found;
  2862. }
  2863. /* If nr exceeds the number of threads there is nothing todo */
  2864. pos = NULL;
  2865. if (nr && nr >= get_nr_threads(leader))
  2866. goto out;
  2867. /* If we haven't found our starting place yet start
  2868. * with the leader and walk nr threads forward.
  2869. */
  2870. for (pos = leader; nr > 0; --nr) {
  2871. pos = next_thread(pos);
  2872. if (pos == leader) {
  2873. pos = NULL;
  2874. goto out;
  2875. }
  2876. }
  2877. found:
  2878. get_task_struct(pos);
  2879. out:
  2880. rcu_read_unlock();
  2881. return pos;
  2882. }
  2883. /*
  2884. * Find the next thread in the thread list.
  2885. * Return NULL if there is an error or no next thread.
  2886. *
  2887. * The reference to the input task_struct is released.
  2888. */
  2889. static struct task_struct *next_tid(struct task_struct *start)
  2890. {
  2891. struct task_struct *pos = NULL;
  2892. rcu_read_lock();
  2893. if (pid_alive(start)) {
  2894. pos = next_thread(start);
  2895. if (thread_group_leader(pos))
  2896. pos = NULL;
  2897. else
  2898. get_task_struct(pos);
  2899. }
  2900. rcu_read_unlock();
  2901. put_task_struct(start);
  2902. return pos;
  2903. }
  2904. static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
  2905. struct task_struct *task, int tid)
  2906. {
  2907. char name[PROC_NUMBUF];
  2908. int len = snprintf(name, sizeof(name), "%d", tid);
  2909. return proc_fill_cache(filp, dirent, filldir, name, len,
  2910. proc_task_instantiate, task, NULL);
  2911. }
  2912. /* for the /proc/TGID/task/ directories */
  2913. static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
  2914. {
  2915. struct dentry *dentry = filp->f_path.dentry;
  2916. struct inode *inode = dentry->d_inode;
  2917. struct task_struct *leader = NULL;
  2918. struct task_struct *task;
  2919. int retval = -ENOENT;
  2920. ino_t ino;
  2921. int tid;
  2922. struct pid_namespace *ns;
  2923. task = get_proc_task(inode);
  2924. if (!task)
  2925. goto out_no_task;
  2926. rcu_read_lock();
  2927. if (pid_alive(task)) {
  2928. leader = task->group_leader;
  2929. get_task_struct(leader);
  2930. }
  2931. rcu_read_unlock();
  2932. put_task_struct(task);
  2933. if (!leader)
  2934. goto out_no_task;
  2935. retval = 0;
  2936. switch ((unsigned long)filp->f_pos) {
  2937. case 0:
  2938. ino = inode->i_ino;
  2939. if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) < 0)
  2940. goto out;
  2941. filp->f_pos++;
  2942. /* fall through */
  2943. case 1:
  2944. ino = parent_ino(dentry);
  2945. if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) < 0)
  2946. goto out;
  2947. filp->f_pos++;
  2948. /* fall through */
  2949. }
  2950. /* f_version caches the tgid value that the last readdir call couldn't
  2951. * return. lseek aka telldir automagically resets f_version to 0.
  2952. */
  2953. ns = filp->f_dentry->d_sb->s_fs_info;
  2954. tid = (int)filp->f_version;
  2955. filp->f_version = 0;
  2956. for (task = first_tid(leader, tid, filp->f_pos - 2, ns);
  2957. task;
  2958. task = next_tid(task), filp->f_pos++) {
  2959. tid = task_pid_nr_ns(task, ns);
  2960. if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
  2961. /* returning this tgid failed, save it as the first
  2962. * pid for the next readir call */
  2963. filp->f_version = (u64)tid;
  2964. put_task_struct(task);
  2965. break;
  2966. }
  2967. }
  2968. out:
  2969. put_task_struct(leader);
  2970. out_no_task:
  2971. return retval;
  2972. }
  2973. static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
  2974. {
  2975. struct inode *inode = dentry->d_inode;
  2976. struct task_struct *p = get_proc_task(inode);
  2977. generic_fillattr(inode, stat);
  2978. if (p) {
  2979. stat->nlink += get_nr_threads(p);
  2980. put_task_struct(p);
  2981. }
  2982. return 0;
  2983. }
  2984. static const struct inode_operations proc_task_inode_operations = {
  2985. .lookup = proc_task_lookup,
  2986. .getattr = proc_task_getattr,
  2987. .setattr = proc_setattr,
  2988. };
  2989. static const struct file_operations proc_task_operations = {
  2990. .read = generic_read_dir,
  2991. .readdir = proc_task_readdir,
  2992. .llseek = default_llseek,
  2993. };