base.c 77 KB

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