base.c 79 KB

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