base.c 73 KB

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