base.c 81 KB

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