base.c 74 KB

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