base.c 72 KB

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