base.c 74 KB

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