base.c 79 KB

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