base.c 75 KB

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