base.c 79 KB

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