base.c 76 KB

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