base.c 79 KB

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