base.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374
  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/config.h>
  51. #include <linux/errno.h>
  52. #include <linux/time.h>
  53. #include <linux/proc_fs.h>
  54. #include <linux/stat.h>
  55. #include <linux/init.h>
  56. #include <linux/capability.h>
  57. #include <linux/file.h>
  58. #include <linux/string.h>
  59. #include <linux/seq_file.h>
  60. #include <linux/namei.h>
  61. #include <linux/namespace.h>
  62. #include <linux/mm.h>
  63. #include <linux/smp_lock.h>
  64. #include <linux/rcupdate.h>
  65. #include <linux/kallsyms.h>
  66. #include <linux/mount.h>
  67. #include <linux/security.h>
  68. #include <linux/ptrace.h>
  69. #include <linux/seccomp.h>
  70. #include <linux/cpuset.h>
  71. #include <linux/audit.h>
  72. #include <linux/poll.h>
  73. #include "internal.h"
  74. /* NOTE:
  75. * Implementing inode permission operations in /proc is almost
  76. * certainly an error. Permission checks need to happen during
  77. * each system call not at open time. The reason is that most of
  78. * what we wish to check for permissions in /proc varies at runtime.
  79. *
  80. * The classic example of a problem is opening file descriptors
  81. * in /proc for a task before it execs a suid executable.
  82. */
  83. /*
  84. * For hysterical raisins we keep the same inumbers as in the old procfs.
  85. * Feel free to change the macro below - just keep the range distinct from
  86. * inumbers of the rest of procfs (currently those are in 0x0000--0xffff).
  87. * As soon as we'll get a separate superblock we will be able to forget
  88. * about magical ranges too.
  89. */
  90. #define fake_ino(pid,ino) (((pid)<<16)|(ino))
  91. enum pid_directory_inos {
  92. PROC_TGID_INO = 2,
  93. PROC_TGID_TASK,
  94. PROC_TGID_STATUS,
  95. PROC_TGID_MEM,
  96. #ifdef CONFIG_SECCOMP
  97. PROC_TGID_SECCOMP,
  98. #endif
  99. PROC_TGID_CWD,
  100. PROC_TGID_ROOT,
  101. PROC_TGID_EXE,
  102. PROC_TGID_FD,
  103. PROC_TGID_ENVIRON,
  104. PROC_TGID_AUXV,
  105. PROC_TGID_CMDLINE,
  106. PROC_TGID_STAT,
  107. PROC_TGID_STATM,
  108. PROC_TGID_MAPS,
  109. PROC_TGID_NUMA_MAPS,
  110. PROC_TGID_MOUNTS,
  111. PROC_TGID_MOUNTSTATS,
  112. PROC_TGID_WCHAN,
  113. #ifdef CONFIG_MMU
  114. PROC_TGID_SMAPS,
  115. #endif
  116. #ifdef CONFIG_SCHEDSTATS
  117. PROC_TGID_SCHEDSTAT,
  118. #endif
  119. #ifdef CONFIG_CPUSETS
  120. PROC_TGID_CPUSET,
  121. #endif
  122. #ifdef CONFIG_SECURITY
  123. PROC_TGID_ATTR,
  124. PROC_TGID_ATTR_CURRENT,
  125. PROC_TGID_ATTR_PREV,
  126. PROC_TGID_ATTR_EXEC,
  127. PROC_TGID_ATTR_FSCREATE,
  128. PROC_TGID_ATTR_KEYCREATE,
  129. #endif
  130. #ifdef CONFIG_AUDITSYSCALL
  131. PROC_TGID_LOGINUID,
  132. #endif
  133. PROC_TGID_OOM_SCORE,
  134. PROC_TGID_OOM_ADJUST,
  135. PROC_TID_INO,
  136. PROC_TID_STATUS,
  137. PROC_TID_MEM,
  138. #ifdef CONFIG_SECCOMP
  139. PROC_TID_SECCOMP,
  140. #endif
  141. PROC_TID_CWD,
  142. PROC_TID_ROOT,
  143. PROC_TID_EXE,
  144. PROC_TID_FD,
  145. PROC_TID_ENVIRON,
  146. PROC_TID_AUXV,
  147. PROC_TID_CMDLINE,
  148. PROC_TID_STAT,
  149. PROC_TID_STATM,
  150. PROC_TID_MAPS,
  151. PROC_TID_NUMA_MAPS,
  152. PROC_TID_MOUNTS,
  153. PROC_TID_MOUNTSTATS,
  154. PROC_TID_WCHAN,
  155. #ifdef CONFIG_MMU
  156. PROC_TID_SMAPS,
  157. #endif
  158. #ifdef CONFIG_SCHEDSTATS
  159. PROC_TID_SCHEDSTAT,
  160. #endif
  161. #ifdef CONFIG_CPUSETS
  162. PROC_TID_CPUSET,
  163. #endif
  164. #ifdef CONFIG_SECURITY
  165. PROC_TID_ATTR,
  166. PROC_TID_ATTR_CURRENT,
  167. PROC_TID_ATTR_PREV,
  168. PROC_TID_ATTR_EXEC,
  169. PROC_TID_ATTR_FSCREATE,
  170. PROC_TID_ATTR_KEYCREATE,
  171. #endif
  172. #ifdef CONFIG_AUDITSYSCALL
  173. PROC_TID_LOGINUID,
  174. #endif
  175. PROC_TID_OOM_SCORE,
  176. PROC_TID_OOM_ADJUST,
  177. /* Add new entries before this */
  178. PROC_TID_FD_DIR = 0x8000, /* 0x8000-0xffff */
  179. };
  180. /* Worst case buffer size needed for holding an integer. */
  181. #define PROC_NUMBUF 10
  182. struct pid_entry {
  183. int type;
  184. int len;
  185. char *name;
  186. mode_t mode;
  187. };
  188. #define E(type,name,mode) {(type),sizeof(name)-1,(name),(mode)}
  189. static struct pid_entry tgid_base_stuff[] = {
  190. E(PROC_TGID_TASK, "task", S_IFDIR|S_IRUGO|S_IXUGO),
  191. E(PROC_TGID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
  192. E(PROC_TGID_ENVIRON, "environ", S_IFREG|S_IRUSR),
  193. E(PROC_TGID_AUXV, "auxv", S_IFREG|S_IRUSR),
  194. E(PROC_TGID_STATUS, "status", S_IFREG|S_IRUGO),
  195. E(PROC_TGID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
  196. E(PROC_TGID_STAT, "stat", S_IFREG|S_IRUGO),
  197. E(PROC_TGID_STATM, "statm", S_IFREG|S_IRUGO),
  198. E(PROC_TGID_MAPS, "maps", S_IFREG|S_IRUGO),
  199. #ifdef CONFIG_NUMA
  200. E(PROC_TGID_NUMA_MAPS, "numa_maps", S_IFREG|S_IRUGO),
  201. #endif
  202. E(PROC_TGID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
  203. #ifdef CONFIG_SECCOMP
  204. E(PROC_TGID_SECCOMP, "seccomp", S_IFREG|S_IRUSR|S_IWUSR),
  205. #endif
  206. E(PROC_TGID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
  207. E(PROC_TGID_ROOT, "root", S_IFLNK|S_IRWXUGO),
  208. E(PROC_TGID_EXE, "exe", S_IFLNK|S_IRWXUGO),
  209. E(PROC_TGID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
  210. E(PROC_TGID_MOUNTSTATS, "mountstats", S_IFREG|S_IRUSR),
  211. #ifdef CONFIG_MMU
  212. E(PROC_TGID_SMAPS, "smaps", S_IFREG|S_IRUGO),
  213. #endif
  214. #ifdef CONFIG_SECURITY
  215. E(PROC_TGID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
  216. #endif
  217. #ifdef CONFIG_KALLSYMS
  218. E(PROC_TGID_WCHAN, "wchan", S_IFREG|S_IRUGO),
  219. #endif
  220. #ifdef CONFIG_SCHEDSTATS
  221. E(PROC_TGID_SCHEDSTAT, "schedstat", S_IFREG|S_IRUGO),
  222. #endif
  223. #ifdef CONFIG_CPUSETS
  224. E(PROC_TGID_CPUSET, "cpuset", S_IFREG|S_IRUGO),
  225. #endif
  226. E(PROC_TGID_OOM_SCORE, "oom_score",S_IFREG|S_IRUGO),
  227. E(PROC_TGID_OOM_ADJUST,"oom_adj", S_IFREG|S_IRUGO|S_IWUSR),
  228. #ifdef CONFIG_AUDITSYSCALL
  229. E(PROC_TGID_LOGINUID, "loginuid", S_IFREG|S_IWUSR|S_IRUGO),
  230. #endif
  231. {0,0,NULL,0}
  232. };
  233. static struct pid_entry tid_base_stuff[] = {
  234. E(PROC_TID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
  235. E(PROC_TID_ENVIRON, "environ", S_IFREG|S_IRUSR),
  236. E(PROC_TID_AUXV, "auxv", S_IFREG|S_IRUSR),
  237. E(PROC_TID_STATUS, "status", S_IFREG|S_IRUGO),
  238. E(PROC_TID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
  239. E(PROC_TID_STAT, "stat", S_IFREG|S_IRUGO),
  240. E(PROC_TID_STATM, "statm", S_IFREG|S_IRUGO),
  241. E(PROC_TID_MAPS, "maps", S_IFREG|S_IRUGO),
  242. #ifdef CONFIG_NUMA
  243. E(PROC_TID_NUMA_MAPS, "numa_maps", S_IFREG|S_IRUGO),
  244. #endif
  245. E(PROC_TID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
  246. #ifdef CONFIG_SECCOMP
  247. E(PROC_TID_SECCOMP, "seccomp", S_IFREG|S_IRUSR|S_IWUSR),
  248. #endif
  249. E(PROC_TID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
  250. E(PROC_TID_ROOT, "root", S_IFLNK|S_IRWXUGO),
  251. E(PROC_TID_EXE, "exe", S_IFLNK|S_IRWXUGO),
  252. E(PROC_TID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
  253. #ifdef CONFIG_MMU
  254. E(PROC_TID_SMAPS, "smaps", S_IFREG|S_IRUGO),
  255. #endif
  256. #ifdef CONFIG_SECURITY
  257. E(PROC_TID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
  258. #endif
  259. #ifdef CONFIG_KALLSYMS
  260. E(PROC_TID_WCHAN, "wchan", S_IFREG|S_IRUGO),
  261. #endif
  262. #ifdef CONFIG_SCHEDSTATS
  263. E(PROC_TID_SCHEDSTAT, "schedstat",S_IFREG|S_IRUGO),
  264. #endif
  265. #ifdef CONFIG_CPUSETS
  266. E(PROC_TID_CPUSET, "cpuset", S_IFREG|S_IRUGO),
  267. #endif
  268. E(PROC_TID_OOM_SCORE, "oom_score",S_IFREG|S_IRUGO),
  269. E(PROC_TID_OOM_ADJUST, "oom_adj", S_IFREG|S_IRUGO|S_IWUSR),
  270. #ifdef CONFIG_AUDITSYSCALL
  271. E(PROC_TID_LOGINUID, "loginuid", S_IFREG|S_IWUSR|S_IRUGO),
  272. #endif
  273. {0,0,NULL,0}
  274. };
  275. #ifdef CONFIG_SECURITY
  276. static struct pid_entry tgid_attr_stuff[] = {
  277. E(PROC_TGID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO),
  278. E(PROC_TGID_ATTR_PREV, "prev", S_IFREG|S_IRUGO),
  279. E(PROC_TGID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO),
  280. E(PROC_TGID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
  281. E(PROC_TGID_ATTR_KEYCREATE, "keycreate", S_IFREG|S_IRUGO|S_IWUGO),
  282. {0,0,NULL,0}
  283. };
  284. static struct pid_entry tid_attr_stuff[] = {
  285. E(PROC_TID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO),
  286. E(PROC_TID_ATTR_PREV, "prev", S_IFREG|S_IRUGO),
  287. E(PROC_TID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO),
  288. E(PROC_TID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
  289. E(PROC_TID_ATTR_KEYCREATE, "keycreate", S_IFREG|S_IRUGO|S_IWUGO),
  290. {0,0,NULL,0}
  291. };
  292. #endif
  293. #undef E
  294. static int proc_fd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
  295. {
  296. struct task_struct *task = get_proc_task(inode);
  297. struct files_struct *files = NULL;
  298. struct file *file;
  299. int fd = proc_fd(inode);
  300. if (task) {
  301. files = get_files_struct(task);
  302. put_task_struct(task);
  303. }
  304. if (files) {
  305. /*
  306. * We are not taking a ref to the file structure, so we must
  307. * hold ->file_lock.
  308. */
  309. spin_lock(&files->file_lock);
  310. file = fcheck_files(files, fd);
  311. if (file) {
  312. *mnt = mntget(file->f_vfsmnt);
  313. *dentry = dget(file->f_dentry);
  314. spin_unlock(&files->file_lock);
  315. put_files_struct(files);
  316. return 0;
  317. }
  318. spin_unlock(&files->file_lock);
  319. put_files_struct(files);
  320. }
  321. return -ENOENT;
  322. }
  323. static struct fs_struct *get_fs_struct(struct task_struct *task)
  324. {
  325. struct fs_struct *fs;
  326. task_lock(task);
  327. fs = task->fs;
  328. if(fs)
  329. atomic_inc(&fs->count);
  330. task_unlock(task);
  331. return fs;
  332. }
  333. static int get_nr_threads(struct task_struct *tsk)
  334. {
  335. /* Must be called with the rcu_read_lock held */
  336. unsigned long flags;
  337. int count = 0;
  338. if (lock_task_sighand(tsk, &flags)) {
  339. count = atomic_read(&tsk->signal->count);
  340. unlock_task_sighand(tsk, &flags);
  341. }
  342. return count;
  343. }
  344. static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
  345. {
  346. struct task_struct *task = get_proc_task(inode);
  347. struct fs_struct *fs = NULL;
  348. int result = -ENOENT;
  349. if (task) {
  350. fs = get_fs_struct(task);
  351. put_task_struct(task);
  352. }
  353. if (fs) {
  354. read_lock(&fs->lock);
  355. *mnt = mntget(fs->pwdmnt);
  356. *dentry = dget(fs->pwd);
  357. read_unlock(&fs->lock);
  358. result = 0;
  359. put_fs_struct(fs);
  360. }
  361. return result;
  362. }
  363. static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
  364. {
  365. struct task_struct *task = get_proc_task(inode);
  366. struct fs_struct *fs = NULL;
  367. int result = -ENOENT;
  368. if (task) {
  369. fs = get_fs_struct(task);
  370. put_task_struct(task);
  371. }
  372. if (fs) {
  373. read_lock(&fs->lock);
  374. *mnt = mntget(fs->rootmnt);
  375. *dentry = dget(fs->root);
  376. read_unlock(&fs->lock);
  377. result = 0;
  378. put_fs_struct(fs);
  379. }
  380. return result;
  381. }
  382. #define MAY_PTRACE(task) \
  383. (task == current || \
  384. (task->parent == current && \
  385. (task->ptrace & PT_PTRACED) && \
  386. (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
  387. security_ptrace(current,task) == 0))
  388. static int proc_pid_environ(struct task_struct *task, char * buffer)
  389. {
  390. int res = 0;
  391. struct mm_struct *mm = get_task_mm(task);
  392. if (mm) {
  393. unsigned int len = mm->env_end - mm->env_start;
  394. if (len > PAGE_SIZE)
  395. len = PAGE_SIZE;
  396. res = access_process_vm(task, mm->env_start, buffer, len, 0);
  397. if (!ptrace_may_attach(task))
  398. res = -ESRCH;
  399. mmput(mm);
  400. }
  401. return res;
  402. }
  403. static int proc_pid_cmdline(struct task_struct *task, char * buffer)
  404. {
  405. int res = 0;
  406. unsigned int len;
  407. struct mm_struct *mm = get_task_mm(task);
  408. if (!mm)
  409. goto out;
  410. if (!mm->arg_end)
  411. goto out_mm; /* Shh! No looking before we're done */
  412. len = mm->arg_end - mm->arg_start;
  413. if (len > PAGE_SIZE)
  414. len = PAGE_SIZE;
  415. res = access_process_vm(task, mm->arg_start, buffer, len, 0);
  416. // If the nul at the end of args has been overwritten, then
  417. // assume application is using setproctitle(3).
  418. if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
  419. len = strnlen(buffer, res);
  420. if (len < res) {
  421. res = len;
  422. } else {
  423. len = mm->env_end - mm->env_start;
  424. if (len > PAGE_SIZE - res)
  425. len = PAGE_SIZE - res;
  426. res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
  427. res = strnlen(buffer, res);
  428. }
  429. }
  430. out_mm:
  431. mmput(mm);
  432. out:
  433. return res;
  434. }
  435. static int proc_pid_auxv(struct task_struct *task, char *buffer)
  436. {
  437. int res = 0;
  438. struct mm_struct *mm = get_task_mm(task);
  439. if (mm) {
  440. unsigned int nwords = 0;
  441. do
  442. nwords += 2;
  443. while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
  444. res = nwords * sizeof(mm->saved_auxv[0]);
  445. if (res > PAGE_SIZE)
  446. res = PAGE_SIZE;
  447. memcpy(buffer, mm->saved_auxv, res);
  448. mmput(mm);
  449. }
  450. return res;
  451. }
  452. #ifdef CONFIG_KALLSYMS
  453. /*
  454. * Provides a wchan file via kallsyms in a proper one-value-per-file format.
  455. * Returns the resolved symbol. If that fails, simply return the address.
  456. */
  457. static int proc_pid_wchan(struct task_struct *task, char *buffer)
  458. {
  459. char *modname;
  460. const char *sym_name;
  461. unsigned long wchan, size, offset;
  462. char namebuf[KSYM_NAME_LEN+1];
  463. wchan = get_wchan(task);
  464. sym_name = kallsyms_lookup(wchan, &size, &offset, &modname, namebuf);
  465. if (sym_name)
  466. return sprintf(buffer, "%s", sym_name);
  467. return sprintf(buffer, "%lu", wchan);
  468. }
  469. #endif /* CONFIG_KALLSYMS */
  470. #ifdef CONFIG_SCHEDSTATS
  471. /*
  472. * Provides /proc/PID/schedstat
  473. */
  474. static int proc_pid_schedstat(struct task_struct *task, char *buffer)
  475. {
  476. return sprintf(buffer, "%lu %lu %lu\n",
  477. task->sched_info.cpu_time,
  478. task->sched_info.run_delay,
  479. task->sched_info.pcnt);
  480. }
  481. #endif
  482. /* The badness from the OOM killer */
  483. unsigned long badness(struct task_struct *p, unsigned long uptime);
  484. static int proc_oom_score(struct task_struct *task, char *buffer)
  485. {
  486. unsigned long points;
  487. struct timespec uptime;
  488. do_posix_clock_monotonic_gettime(&uptime);
  489. points = badness(task, uptime.tv_sec);
  490. return sprintf(buffer, "%lu\n", points);
  491. }
  492. /************************************************************************/
  493. /* Here the fs part begins */
  494. /************************************************************************/
  495. /* permission checks */
  496. static int proc_fd_access_allowed(struct inode *inode)
  497. {
  498. struct task_struct *task;
  499. int allowed = 0;
  500. /* Allow access to a task's file descriptors if either we may
  501. * use ptrace attach to the process and find out that
  502. * information, or if the task cannot possibly be ptraced
  503. * allow access if we have the proper capability.
  504. */
  505. task = get_proc_task(inode);
  506. if (task == current)
  507. allowed = 1;
  508. if (task && !allowed) {
  509. int alive;
  510. task_lock(task);
  511. alive = !!task->mm;
  512. task_unlock(task);
  513. if (alive)
  514. /* For a living task obey ptrace_may_attach */
  515. allowed = ptrace_may_attach(task);
  516. else
  517. /* For a special task simply check the capability */
  518. allowed = capable(CAP_SYS_PTRACE);
  519. }
  520. if (task)
  521. put_task_struct(task);
  522. return allowed;
  523. }
  524. extern struct seq_operations mounts_op;
  525. struct proc_mounts {
  526. struct seq_file m;
  527. int event;
  528. };
  529. static int mounts_open(struct inode *inode, struct file *file)
  530. {
  531. struct task_struct *task = get_proc_task(inode);
  532. struct namespace *namespace = NULL;
  533. struct proc_mounts *p;
  534. int ret = -EINVAL;
  535. if (task) {
  536. task_lock(task);
  537. namespace = task->namespace;
  538. if (namespace)
  539. get_namespace(namespace);
  540. task_unlock(task);
  541. put_task_struct(task);
  542. }
  543. if (namespace) {
  544. ret = -ENOMEM;
  545. p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
  546. if (p) {
  547. file->private_data = &p->m;
  548. ret = seq_open(file, &mounts_op);
  549. if (!ret) {
  550. p->m.private = namespace;
  551. p->event = namespace->event;
  552. return 0;
  553. }
  554. kfree(p);
  555. }
  556. put_namespace(namespace);
  557. }
  558. return ret;
  559. }
  560. static int mounts_release(struct inode *inode, struct file *file)
  561. {
  562. struct seq_file *m = file->private_data;
  563. struct namespace *namespace = m->private;
  564. put_namespace(namespace);
  565. return seq_release(inode, file);
  566. }
  567. static unsigned mounts_poll(struct file *file, poll_table *wait)
  568. {
  569. struct proc_mounts *p = file->private_data;
  570. struct namespace *ns = p->m.private;
  571. unsigned res = 0;
  572. poll_wait(file, &ns->poll, wait);
  573. spin_lock(&vfsmount_lock);
  574. if (p->event != ns->event) {
  575. p->event = ns->event;
  576. res = POLLERR;
  577. }
  578. spin_unlock(&vfsmount_lock);
  579. return res;
  580. }
  581. static struct file_operations proc_mounts_operations = {
  582. .open = mounts_open,
  583. .read = seq_read,
  584. .llseek = seq_lseek,
  585. .release = mounts_release,
  586. .poll = mounts_poll,
  587. };
  588. extern struct seq_operations mountstats_op;
  589. static int mountstats_open(struct inode *inode, struct file *file)
  590. {
  591. int ret = seq_open(file, &mountstats_op);
  592. if (!ret) {
  593. struct seq_file *m = file->private_data;
  594. struct namespace *namespace = NULL;
  595. struct task_struct *task = get_proc_task(inode);
  596. if (task) {
  597. task_lock(task);
  598. namespace = task->namespace;
  599. if (namespace)
  600. get_namespace(namespace);
  601. task_unlock(task);
  602. put_task_struct(task);
  603. }
  604. if (namespace)
  605. m->private = namespace;
  606. else {
  607. seq_release(inode, file);
  608. ret = -EINVAL;
  609. }
  610. }
  611. return ret;
  612. }
  613. static struct file_operations proc_mountstats_operations = {
  614. .open = mountstats_open,
  615. .read = seq_read,
  616. .llseek = seq_lseek,
  617. .release = mounts_release,
  618. };
  619. #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
  620. static ssize_t proc_info_read(struct file * file, char __user * buf,
  621. size_t count, loff_t *ppos)
  622. {
  623. struct inode * inode = file->f_dentry->d_inode;
  624. unsigned long page;
  625. ssize_t length;
  626. struct task_struct *task = get_proc_task(inode);
  627. length = -ESRCH;
  628. if (!task)
  629. goto out_no_task;
  630. if (count > PROC_BLOCK_SIZE)
  631. count = PROC_BLOCK_SIZE;
  632. length = -ENOMEM;
  633. if (!(page = __get_free_page(GFP_KERNEL)))
  634. goto out;
  635. length = PROC_I(inode)->op.proc_read(task, (char*)page);
  636. if (length >= 0)
  637. length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
  638. free_page(page);
  639. out:
  640. put_task_struct(task);
  641. out_no_task:
  642. return length;
  643. }
  644. static struct file_operations proc_info_file_operations = {
  645. .read = proc_info_read,
  646. };
  647. static int mem_open(struct inode* inode, struct file* file)
  648. {
  649. file->private_data = (void*)((long)current->self_exec_id);
  650. return 0;
  651. }
  652. static ssize_t mem_read(struct file * file, char __user * buf,
  653. size_t count, loff_t *ppos)
  654. {
  655. struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
  656. char *page;
  657. unsigned long src = *ppos;
  658. int ret = -ESRCH;
  659. struct mm_struct *mm;
  660. if (!task)
  661. goto out_no_task;
  662. if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
  663. goto out;
  664. ret = -ENOMEM;
  665. page = (char *)__get_free_page(GFP_USER);
  666. if (!page)
  667. goto out;
  668. ret = 0;
  669. mm = get_task_mm(task);
  670. if (!mm)
  671. goto out_free;
  672. ret = -EIO;
  673. if (file->private_data != (void*)((long)current->self_exec_id))
  674. goto out_put;
  675. ret = 0;
  676. while (count > 0) {
  677. int this_len, retval;
  678. this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
  679. retval = access_process_vm(task, src, page, this_len, 0);
  680. if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
  681. if (!ret)
  682. ret = -EIO;
  683. break;
  684. }
  685. if (copy_to_user(buf, page, retval)) {
  686. ret = -EFAULT;
  687. break;
  688. }
  689. ret += retval;
  690. src += retval;
  691. buf += retval;
  692. count -= retval;
  693. }
  694. *ppos = src;
  695. out_put:
  696. mmput(mm);
  697. out_free:
  698. free_page((unsigned long) page);
  699. out:
  700. put_task_struct(task);
  701. out_no_task:
  702. return ret;
  703. }
  704. #define mem_write NULL
  705. #ifndef mem_write
  706. /* This is a security hazard */
  707. static ssize_t mem_write(struct file * file, const char * buf,
  708. size_t count, loff_t *ppos)
  709. {
  710. int copied = 0;
  711. char *page;
  712. struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
  713. unsigned long dst = *ppos;
  714. copied = -ESRCH;
  715. if (!task)
  716. goto out_no_task;
  717. if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
  718. goto out;
  719. copied = -ENOMEM;
  720. page = (char *)__get_free_page(GFP_USER);
  721. if (!page)
  722. goto out;
  723. while (count > 0) {
  724. int this_len, retval;
  725. this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
  726. if (copy_from_user(page, buf, this_len)) {
  727. copied = -EFAULT;
  728. break;
  729. }
  730. retval = access_process_vm(task, dst, page, this_len, 1);
  731. if (!retval) {
  732. if (!copied)
  733. copied = -EIO;
  734. break;
  735. }
  736. copied += retval;
  737. buf += retval;
  738. dst += retval;
  739. count -= retval;
  740. }
  741. *ppos = dst;
  742. free_page((unsigned long) page);
  743. out:
  744. put_task_struct(task);
  745. out_no_task:
  746. return copied;
  747. }
  748. #endif
  749. static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
  750. {
  751. switch (orig) {
  752. case 0:
  753. file->f_pos = offset;
  754. break;
  755. case 1:
  756. file->f_pos += offset;
  757. break;
  758. default:
  759. return -EINVAL;
  760. }
  761. force_successful_syscall_return();
  762. return file->f_pos;
  763. }
  764. static struct file_operations proc_mem_operations = {
  765. .llseek = mem_lseek,
  766. .read = mem_read,
  767. .write = mem_write,
  768. .open = mem_open,
  769. };
  770. static ssize_t oom_adjust_read(struct file *file, char __user *buf,
  771. size_t count, loff_t *ppos)
  772. {
  773. struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
  774. char buffer[PROC_NUMBUF];
  775. size_t len;
  776. int oom_adjust;
  777. loff_t __ppos = *ppos;
  778. if (!task)
  779. return -ESRCH;
  780. oom_adjust = task->oomkilladj;
  781. put_task_struct(task);
  782. len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
  783. if (__ppos >= len)
  784. return 0;
  785. if (count > len-__ppos)
  786. count = len-__ppos;
  787. if (copy_to_user(buf, buffer + __ppos, count))
  788. return -EFAULT;
  789. *ppos = __ppos + count;
  790. return count;
  791. }
  792. static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
  793. size_t count, loff_t *ppos)
  794. {
  795. struct task_struct *task;
  796. char buffer[PROC_NUMBUF], *end;
  797. int oom_adjust;
  798. if (!capable(CAP_SYS_RESOURCE))
  799. return -EPERM;
  800. memset(buffer, 0, sizeof(buffer));
  801. if (count > sizeof(buffer) - 1)
  802. count = sizeof(buffer) - 1;
  803. if (copy_from_user(buffer, buf, count))
  804. return -EFAULT;
  805. oom_adjust = simple_strtol(buffer, &end, 0);
  806. if ((oom_adjust < -16 || oom_adjust > 15) && oom_adjust != OOM_DISABLE)
  807. return -EINVAL;
  808. if (*end == '\n')
  809. end++;
  810. task = get_proc_task(file->f_dentry->d_inode);
  811. if (!task)
  812. return -ESRCH;
  813. task->oomkilladj = oom_adjust;
  814. put_task_struct(task);
  815. if (end - buffer == 0)
  816. return -EIO;
  817. return end - buffer;
  818. }
  819. static struct file_operations proc_oom_adjust_operations = {
  820. .read = oom_adjust_read,
  821. .write = oom_adjust_write,
  822. };
  823. #ifdef CONFIG_AUDITSYSCALL
  824. #define TMPBUFLEN 21
  825. static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
  826. size_t count, loff_t *ppos)
  827. {
  828. struct inode * inode = file->f_dentry->d_inode;
  829. struct task_struct *task = get_proc_task(inode);
  830. ssize_t length;
  831. char tmpbuf[TMPBUFLEN];
  832. if (!task)
  833. return -ESRCH;
  834. length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
  835. audit_get_loginuid(task->audit_context));
  836. put_task_struct(task);
  837. return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
  838. }
  839. static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
  840. size_t count, loff_t *ppos)
  841. {
  842. struct inode * inode = file->f_dentry->d_inode;
  843. char *page, *tmp;
  844. ssize_t length;
  845. uid_t loginuid;
  846. if (!capable(CAP_AUDIT_CONTROL))
  847. return -EPERM;
  848. if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
  849. return -EPERM;
  850. if (count >= PAGE_SIZE)
  851. count = PAGE_SIZE - 1;
  852. if (*ppos != 0) {
  853. /* No partial writes. */
  854. return -EINVAL;
  855. }
  856. page = (char*)__get_free_page(GFP_USER);
  857. if (!page)
  858. return -ENOMEM;
  859. length = -EFAULT;
  860. if (copy_from_user(page, buf, count))
  861. goto out_free_page;
  862. page[count] = '\0';
  863. loginuid = simple_strtoul(page, &tmp, 10);
  864. if (tmp == page) {
  865. length = -EINVAL;
  866. goto out_free_page;
  867. }
  868. length = audit_set_loginuid(current, loginuid);
  869. if (likely(length == 0))
  870. length = count;
  871. out_free_page:
  872. free_page((unsigned long) page);
  873. return length;
  874. }
  875. static struct file_operations proc_loginuid_operations = {
  876. .read = proc_loginuid_read,
  877. .write = proc_loginuid_write,
  878. };
  879. #endif
  880. #ifdef CONFIG_SECCOMP
  881. static ssize_t seccomp_read(struct file *file, char __user *buf,
  882. size_t count, loff_t *ppos)
  883. {
  884. struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
  885. char __buf[20];
  886. loff_t __ppos = *ppos;
  887. size_t len;
  888. if (!tsk)
  889. return -ESRCH;
  890. /* no need to print the trailing zero, so use only len */
  891. len = sprintf(__buf, "%u\n", tsk->seccomp.mode);
  892. put_task_struct(tsk);
  893. if (__ppos >= len)
  894. return 0;
  895. if (count > len - __ppos)
  896. count = len - __ppos;
  897. if (copy_to_user(buf, __buf + __ppos, count))
  898. return -EFAULT;
  899. *ppos = __ppos + count;
  900. return count;
  901. }
  902. static ssize_t seccomp_write(struct file *file, const char __user *buf,
  903. size_t count, loff_t *ppos)
  904. {
  905. struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
  906. char __buf[20], *end;
  907. unsigned int seccomp_mode;
  908. ssize_t result;
  909. result = -ESRCH;
  910. if (!tsk)
  911. goto out_no_task;
  912. /* can set it only once to be even more secure */
  913. result = -EPERM;
  914. if (unlikely(tsk->seccomp.mode))
  915. goto out;
  916. result = -EFAULT;
  917. memset(__buf, 0, sizeof(__buf));
  918. count = min(count, sizeof(__buf) - 1);
  919. if (copy_from_user(__buf, buf, count))
  920. goto out;
  921. seccomp_mode = simple_strtoul(__buf, &end, 0);
  922. if (*end == '\n')
  923. end++;
  924. result = -EINVAL;
  925. if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) {
  926. tsk->seccomp.mode = seccomp_mode;
  927. set_tsk_thread_flag(tsk, TIF_SECCOMP);
  928. } else
  929. goto out;
  930. result = -EIO;
  931. if (unlikely(!(end - __buf)))
  932. goto out;
  933. result = end - __buf;
  934. out:
  935. put_task_struct(tsk);
  936. out_no_task:
  937. return result;
  938. }
  939. static struct file_operations proc_seccomp_operations = {
  940. .read = seccomp_read,
  941. .write = seccomp_write,
  942. };
  943. #endif /* CONFIG_SECCOMP */
  944. static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
  945. {
  946. struct inode *inode = dentry->d_inode;
  947. int error = -EACCES;
  948. /* We don't need a base pointer in the /proc filesystem */
  949. path_release(nd);
  950. /* Are we allowed to snoop on the tasks file descriptors? */
  951. if (!proc_fd_access_allowed(inode))
  952. goto out;
  953. error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
  954. nd->last_type = LAST_BIND;
  955. out:
  956. return ERR_PTR(error);
  957. }
  958. static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
  959. char __user *buffer, int buflen)
  960. {
  961. struct inode * inode;
  962. char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
  963. int len;
  964. if (!tmp)
  965. return -ENOMEM;
  966. inode = dentry->d_inode;
  967. path = d_path(dentry, mnt, tmp, PAGE_SIZE);
  968. len = PTR_ERR(path);
  969. if (IS_ERR(path))
  970. goto out;
  971. len = tmp + PAGE_SIZE - 1 - path;
  972. if (len > buflen)
  973. len = buflen;
  974. if (copy_to_user(buffer, path, len))
  975. len = -EFAULT;
  976. out:
  977. free_page((unsigned long)tmp);
  978. return len;
  979. }
  980. static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
  981. {
  982. int error = -EACCES;
  983. struct inode *inode = dentry->d_inode;
  984. struct dentry *de;
  985. struct vfsmount *mnt = NULL;
  986. /* Are we allowed to snoop on the tasks file descriptors? */
  987. if (!proc_fd_access_allowed(inode))
  988. goto out;
  989. error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
  990. if (error)
  991. goto out;
  992. error = do_proc_readlink(de, mnt, buffer, buflen);
  993. dput(de);
  994. mntput(mnt);
  995. out:
  996. return error;
  997. }
  998. static struct inode_operations proc_pid_link_inode_operations = {
  999. .readlink = proc_pid_readlink,
  1000. .follow_link = proc_pid_follow_link
  1001. };
  1002. static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
  1003. {
  1004. struct dentry *dentry = filp->f_dentry;
  1005. struct inode *inode = dentry->d_inode;
  1006. struct task_struct *p = get_proc_task(inode);
  1007. unsigned int fd, tid, ino;
  1008. int retval;
  1009. char buf[PROC_NUMBUF];
  1010. struct files_struct * files;
  1011. struct fdtable *fdt;
  1012. retval = -ENOENT;
  1013. if (!p)
  1014. goto out_no_task;
  1015. retval = 0;
  1016. tid = p->pid;
  1017. fd = filp->f_pos;
  1018. switch (fd) {
  1019. case 0:
  1020. if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
  1021. goto out;
  1022. filp->f_pos++;
  1023. case 1:
  1024. ino = parent_ino(dentry);
  1025. if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
  1026. goto out;
  1027. filp->f_pos++;
  1028. default:
  1029. files = get_files_struct(p);
  1030. if (!files)
  1031. goto out;
  1032. rcu_read_lock();
  1033. fdt = files_fdtable(files);
  1034. for (fd = filp->f_pos-2;
  1035. fd < fdt->max_fds;
  1036. fd++, filp->f_pos++) {
  1037. unsigned int i,j;
  1038. if (!fcheck_files(files, fd))
  1039. continue;
  1040. rcu_read_unlock();
  1041. j = PROC_NUMBUF;
  1042. i = fd;
  1043. do {
  1044. j--;
  1045. buf[j] = '0' + (i % 10);
  1046. i /= 10;
  1047. } while (i);
  1048. ino = fake_ino(tid, PROC_TID_FD_DIR + fd);
  1049. if (filldir(dirent, buf+j, PROC_NUMBUF-j, fd+2, ino, DT_LNK) < 0) {
  1050. rcu_read_lock();
  1051. break;
  1052. }
  1053. rcu_read_lock();
  1054. }
  1055. rcu_read_unlock();
  1056. put_files_struct(files);
  1057. }
  1058. out:
  1059. put_task_struct(p);
  1060. out_no_task:
  1061. return retval;
  1062. }
  1063. static int proc_pident_readdir(struct file *filp,
  1064. void *dirent, filldir_t filldir,
  1065. struct pid_entry *ents, unsigned int nents)
  1066. {
  1067. int i;
  1068. int pid;
  1069. struct dentry *dentry = filp->f_dentry;
  1070. struct inode *inode = dentry->d_inode;
  1071. struct task_struct *task = get_proc_task(inode);
  1072. struct pid_entry *p;
  1073. ino_t ino;
  1074. int ret;
  1075. ret = -ENOENT;
  1076. if (!task)
  1077. goto out;
  1078. ret = 0;
  1079. pid = task->pid;
  1080. put_task_struct(task);
  1081. i = filp->f_pos;
  1082. switch (i) {
  1083. case 0:
  1084. ino = inode->i_ino;
  1085. if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
  1086. goto out;
  1087. i++;
  1088. filp->f_pos++;
  1089. /* fall through */
  1090. case 1:
  1091. ino = parent_ino(dentry);
  1092. if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
  1093. goto out;
  1094. i++;
  1095. filp->f_pos++;
  1096. /* fall through */
  1097. default:
  1098. i -= 2;
  1099. if (i >= nents) {
  1100. ret = 1;
  1101. goto out;
  1102. }
  1103. p = ents + i;
  1104. while (p->name) {
  1105. if (filldir(dirent, p->name, p->len, filp->f_pos,
  1106. fake_ino(pid, p->type), p->mode >> 12) < 0)
  1107. goto out;
  1108. filp->f_pos++;
  1109. p++;
  1110. }
  1111. }
  1112. ret = 1;
  1113. out:
  1114. return ret;
  1115. }
  1116. static int proc_tgid_base_readdir(struct file * filp,
  1117. void * dirent, filldir_t filldir)
  1118. {
  1119. return proc_pident_readdir(filp,dirent,filldir,
  1120. tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
  1121. }
  1122. static int proc_tid_base_readdir(struct file * filp,
  1123. void * dirent, filldir_t filldir)
  1124. {
  1125. return proc_pident_readdir(filp,dirent,filldir,
  1126. tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
  1127. }
  1128. /* building an inode */
  1129. static int task_dumpable(struct task_struct *task)
  1130. {
  1131. int dumpable = 0;
  1132. struct mm_struct *mm;
  1133. task_lock(task);
  1134. mm = task->mm;
  1135. if (mm)
  1136. dumpable = mm->dumpable;
  1137. task_unlock(task);
  1138. if(dumpable == 1)
  1139. return 1;
  1140. return 0;
  1141. }
  1142. static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task, int ino)
  1143. {
  1144. struct inode * inode;
  1145. struct proc_inode *ei;
  1146. /* We need a new inode */
  1147. inode = new_inode(sb);
  1148. if (!inode)
  1149. goto out;
  1150. /* Common stuff */
  1151. ei = PROC_I(inode);
  1152. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  1153. inode->i_ino = fake_ino(task->pid, ino);
  1154. /*
  1155. * grab the reference to task.
  1156. */
  1157. ei->pid = get_pid(task->pids[PIDTYPE_PID].pid);
  1158. if (!ei->pid)
  1159. goto out_unlock;
  1160. inode->i_uid = 0;
  1161. inode->i_gid = 0;
  1162. if (task_dumpable(task)) {
  1163. inode->i_uid = task->euid;
  1164. inode->i_gid = task->egid;
  1165. }
  1166. security_task_to_inode(task, inode);
  1167. out:
  1168. return inode;
  1169. out_unlock:
  1170. iput(inode);
  1171. return NULL;
  1172. }
  1173. /* dentry stuff */
  1174. /*
  1175. * Exceptional case: normally we are not allowed to unhash a busy
  1176. * directory. In this case, however, we can do it - no aliasing problems
  1177. * due to the way we treat inodes.
  1178. *
  1179. * Rewrite the inode's ownerships here because the owning task may have
  1180. * performed a setuid(), etc.
  1181. *
  1182. * Before the /proc/pid/status file was created the only way to read
  1183. * the effective uid of a /process was to stat /proc/pid. Reading
  1184. * /proc/pid/status is slow enough that procps and other packages
  1185. * kept stating /proc/pid. To keep the rules in /proc simple I have
  1186. * made this apply to all per process world readable and executable
  1187. * directories.
  1188. */
  1189. static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
  1190. {
  1191. struct inode *inode = dentry->d_inode;
  1192. struct task_struct *task = get_proc_task(inode);
  1193. if (task) {
  1194. if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
  1195. task_dumpable(task)) {
  1196. inode->i_uid = task->euid;
  1197. inode->i_gid = task->egid;
  1198. } else {
  1199. inode->i_uid = 0;
  1200. inode->i_gid = 0;
  1201. }
  1202. security_task_to_inode(task, inode);
  1203. put_task_struct(task);
  1204. return 1;
  1205. }
  1206. d_drop(dentry);
  1207. return 0;
  1208. }
  1209. static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
  1210. {
  1211. struct inode *inode = dentry->d_inode;
  1212. struct task_struct *task;
  1213. generic_fillattr(inode, stat);
  1214. rcu_read_lock();
  1215. stat->uid = 0;
  1216. stat->gid = 0;
  1217. task = pid_task(proc_pid(inode), PIDTYPE_PID);
  1218. if (task) {
  1219. if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
  1220. task_dumpable(task)) {
  1221. stat->uid = task->euid;
  1222. stat->gid = task->egid;
  1223. }
  1224. }
  1225. rcu_read_unlock();
  1226. return 0;
  1227. }
  1228. static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
  1229. {
  1230. struct inode *inode = dentry->d_inode;
  1231. struct task_struct *task = get_proc_task(inode);
  1232. int fd = proc_fd(inode);
  1233. struct files_struct *files;
  1234. if (task) {
  1235. files = get_files_struct(task);
  1236. if (files) {
  1237. rcu_read_lock();
  1238. if (fcheck_files(files, fd)) {
  1239. rcu_read_unlock();
  1240. put_files_struct(files);
  1241. if (task_dumpable(task)) {
  1242. inode->i_uid = task->euid;
  1243. inode->i_gid = task->egid;
  1244. } else {
  1245. inode->i_uid = 0;
  1246. inode->i_gid = 0;
  1247. }
  1248. security_task_to_inode(task, inode);
  1249. put_task_struct(task);
  1250. return 1;
  1251. }
  1252. rcu_read_unlock();
  1253. put_files_struct(files);
  1254. }
  1255. put_task_struct(task);
  1256. }
  1257. d_drop(dentry);
  1258. return 0;
  1259. }
  1260. static int pid_delete_dentry(struct dentry * dentry)
  1261. {
  1262. /* Is the task we represent dead?
  1263. * If so, then don't put the dentry on the lru list,
  1264. * kill it immediately.
  1265. */
  1266. return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
  1267. }
  1268. static struct dentry_operations tid_fd_dentry_operations =
  1269. {
  1270. .d_revalidate = tid_fd_revalidate,
  1271. .d_delete = pid_delete_dentry,
  1272. };
  1273. static struct dentry_operations pid_dentry_operations =
  1274. {
  1275. .d_revalidate = pid_revalidate,
  1276. .d_delete = pid_delete_dentry,
  1277. };
  1278. /* Lookups */
  1279. static unsigned name_to_int(struct dentry *dentry)
  1280. {
  1281. const char *name = dentry->d_name.name;
  1282. int len = dentry->d_name.len;
  1283. unsigned n = 0;
  1284. if (len > 1 && *name == '0')
  1285. goto out;
  1286. while (len-- > 0) {
  1287. unsigned c = *name++ - '0';
  1288. if (c > 9)
  1289. goto out;
  1290. if (n >= (~0U-9)/10)
  1291. goto out;
  1292. n *= 10;
  1293. n += c;
  1294. }
  1295. return n;
  1296. out:
  1297. return ~0U;
  1298. }
  1299. /* SMP-safe */
  1300. static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
  1301. {
  1302. struct task_struct *task = get_proc_task(dir);
  1303. unsigned fd = name_to_int(dentry);
  1304. struct dentry *result = ERR_PTR(-ENOENT);
  1305. struct file * file;
  1306. struct files_struct * files;
  1307. struct inode *inode;
  1308. struct proc_inode *ei;
  1309. if (!task)
  1310. goto out_no_task;
  1311. if (fd == ~0U)
  1312. goto out;
  1313. inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_FD_DIR+fd);
  1314. if (!inode)
  1315. goto out;
  1316. ei = PROC_I(inode);
  1317. ei->fd = fd;
  1318. files = get_files_struct(task);
  1319. if (!files)
  1320. goto out_unlock;
  1321. inode->i_mode = S_IFLNK;
  1322. /*
  1323. * We are not taking a ref to the file structure, so we must
  1324. * hold ->file_lock.
  1325. */
  1326. spin_lock(&files->file_lock);
  1327. file = fcheck_files(files, fd);
  1328. if (!file)
  1329. goto out_unlock2;
  1330. if (file->f_mode & 1)
  1331. inode->i_mode |= S_IRUSR | S_IXUSR;
  1332. if (file->f_mode & 2)
  1333. inode->i_mode |= S_IWUSR | S_IXUSR;
  1334. spin_unlock(&files->file_lock);
  1335. put_files_struct(files);
  1336. inode->i_op = &proc_pid_link_inode_operations;
  1337. inode->i_size = 64;
  1338. ei->op.proc_get_link = proc_fd_link;
  1339. dentry->d_op = &tid_fd_dentry_operations;
  1340. d_add(dentry, inode);
  1341. /* Close the race of the process dying before we return the dentry */
  1342. if (tid_fd_revalidate(dentry, NULL))
  1343. result = NULL;
  1344. out:
  1345. put_task_struct(task);
  1346. out_no_task:
  1347. return result;
  1348. out_unlock2:
  1349. spin_unlock(&files->file_lock);
  1350. put_files_struct(files);
  1351. out_unlock:
  1352. iput(inode);
  1353. goto out;
  1354. }
  1355. static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir);
  1356. static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd);
  1357. static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat);
  1358. static struct file_operations proc_fd_operations = {
  1359. .read = generic_read_dir,
  1360. .readdir = proc_readfd,
  1361. };
  1362. static struct file_operations proc_task_operations = {
  1363. .read = generic_read_dir,
  1364. .readdir = proc_task_readdir,
  1365. };
  1366. /*
  1367. * proc directories can do almost nothing..
  1368. */
  1369. static struct inode_operations proc_fd_inode_operations = {
  1370. .lookup = proc_lookupfd,
  1371. };
  1372. static struct inode_operations proc_task_inode_operations = {
  1373. .lookup = proc_task_lookup,
  1374. .getattr = proc_task_getattr,
  1375. };
  1376. #ifdef CONFIG_SECURITY
  1377. static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
  1378. size_t count, loff_t *ppos)
  1379. {
  1380. struct inode * inode = file->f_dentry->d_inode;
  1381. unsigned long page;
  1382. ssize_t length;
  1383. struct task_struct *task = get_proc_task(inode);
  1384. length = -ESRCH;
  1385. if (!task)
  1386. goto out_no_task;
  1387. if (count > PAGE_SIZE)
  1388. count = PAGE_SIZE;
  1389. length = -ENOMEM;
  1390. if (!(page = __get_free_page(GFP_KERNEL)))
  1391. goto out;
  1392. length = security_getprocattr(task,
  1393. (char*)file->f_dentry->d_name.name,
  1394. (void*)page, count);
  1395. if (length >= 0)
  1396. length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
  1397. free_page(page);
  1398. out:
  1399. put_task_struct(task);
  1400. out_no_task:
  1401. return length;
  1402. }
  1403. static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
  1404. size_t count, loff_t *ppos)
  1405. {
  1406. struct inode * inode = file->f_dentry->d_inode;
  1407. char *page;
  1408. ssize_t length;
  1409. struct task_struct *task = get_proc_task(inode);
  1410. length = -ESRCH;
  1411. if (!task)
  1412. goto out_no_task;
  1413. if (count > PAGE_SIZE)
  1414. count = PAGE_SIZE;
  1415. /* No partial writes. */
  1416. length = -EINVAL;
  1417. if (*ppos != 0)
  1418. goto out;
  1419. length = -ENOMEM;
  1420. page = (char*)__get_free_page(GFP_USER);
  1421. if (!page)
  1422. goto out;
  1423. length = -EFAULT;
  1424. if (copy_from_user(page, buf, count))
  1425. goto out_free;
  1426. length = security_setprocattr(task,
  1427. (char*)file->f_dentry->d_name.name,
  1428. (void*)page, count);
  1429. out_free:
  1430. free_page((unsigned long) page);
  1431. out:
  1432. put_task_struct(task);
  1433. out_no_task:
  1434. return length;
  1435. }
  1436. static struct file_operations proc_pid_attr_operations = {
  1437. .read = proc_pid_attr_read,
  1438. .write = proc_pid_attr_write,
  1439. };
  1440. static struct file_operations proc_tid_attr_operations;
  1441. static struct inode_operations proc_tid_attr_inode_operations;
  1442. static struct file_operations proc_tgid_attr_operations;
  1443. static struct inode_operations proc_tgid_attr_inode_operations;
  1444. #endif
  1445. /* SMP-safe */
  1446. static struct dentry *proc_pident_lookup(struct inode *dir,
  1447. struct dentry *dentry,
  1448. struct pid_entry *ents)
  1449. {
  1450. struct inode *inode;
  1451. struct dentry *error;
  1452. struct task_struct *task = get_proc_task(dir);
  1453. struct pid_entry *p;
  1454. struct proc_inode *ei;
  1455. error = ERR_PTR(-ENOENT);
  1456. inode = NULL;
  1457. if (!task)
  1458. goto out_no_task;
  1459. for (p = ents; p->name; p++) {
  1460. if (p->len != dentry->d_name.len)
  1461. continue;
  1462. if (!memcmp(dentry->d_name.name, p->name, p->len))
  1463. break;
  1464. }
  1465. if (!p->name)
  1466. goto out;
  1467. error = ERR_PTR(-EINVAL);
  1468. inode = proc_pid_make_inode(dir->i_sb, task, p->type);
  1469. if (!inode)
  1470. goto out;
  1471. ei = PROC_I(inode);
  1472. inode->i_mode = p->mode;
  1473. /*
  1474. * Yes, it does not scale. And it should not. Don't add
  1475. * new entries into /proc/<tgid>/ without very good reasons.
  1476. */
  1477. switch(p->type) {
  1478. case PROC_TGID_TASK:
  1479. inode->i_nlink = 2;
  1480. inode->i_op = &proc_task_inode_operations;
  1481. inode->i_fop = &proc_task_operations;
  1482. break;
  1483. case PROC_TID_FD:
  1484. case PROC_TGID_FD:
  1485. inode->i_nlink = 2;
  1486. inode->i_op = &proc_fd_inode_operations;
  1487. inode->i_fop = &proc_fd_operations;
  1488. break;
  1489. case PROC_TID_EXE:
  1490. case PROC_TGID_EXE:
  1491. inode->i_op = &proc_pid_link_inode_operations;
  1492. ei->op.proc_get_link = proc_exe_link;
  1493. break;
  1494. case PROC_TID_CWD:
  1495. case PROC_TGID_CWD:
  1496. inode->i_op = &proc_pid_link_inode_operations;
  1497. ei->op.proc_get_link = proc_cwd_link;
  1498. break;
  1499. case PROC_TID_ROOT:
  1500. case PROC_TGID_ROOT:
  1501. inode->i_op = &proc_pid_link_inode_operations;
  1502. ei->op.proc_get_link = proc_root_link;
  1503. break;
  1504. case PROC_TID_ENVIRON:
  1505. case PROC_TGID_ENVIRON:
  1506. inode->i_fop = &proc_info_file_operations;
  1507. ei->op.proc_read = proc_pid_environ;
  1508. break;
  1509. case PROC_TID_AUXV:
  1510. case PROC_TGID_AUXV:
  1511. inode->i_fop = &proc_info_file_operations;
  1512. ei->op.proc_read = proc_pid_auxv;
  1513. break;
  1514. case PROC_TID_STATUS:
  1515. case PROC_TGID_STATUS:
  1516. inode->i_fop = &proc_info_file_operations;
  1517. ei->op.proc_read = proc_pid_status;
  1518. break;
  1519. case PROC_TID_STAT:
  1520. inode->i_fop = &proc_info_file_operations;
  1521. ei->op.proc_read = proc_tid_stat;
  1522. break;
  1523. case PROC_TGID_STAT:
  1524. inode->i_fop = &proc_info_file_operations;
  1525. ei->op.proc_read = proc_tgid_stat;
  1526. break;
  1527. case PROC_TID_CMDLINE:
  1528. case PROC_TGID_CMDLINE:
  1529. inode->i_fop = &proc_info_file_operations;
  1530. ei->op.proc_read = proc_pid_cmdline;
  1531. break;
  1532. case PROC_TID_STATM:
  1533. case PROC_TGID_STATM:
  1534. inode->i_fop = &proc_info_file_operations;
  1535. ei->op.proc_read = proc_pid_statm;
  1536. break;
  1537. case PROC_TID_MAPS:
  1538. case PROC_TGID_MAPS:
  1539. inode->i_fop = &proc_maps_operations;
  1540. break;
  1541. #ifdef CONFIG_NUMA
  1542. case PROC_TID_NUMA_MAPS:
  1543. case PROC_TGID_NUMA_MAPS:
  1544. inode->i_fop = &proc_numa_maps_operations;
  1545. break;
  1546. #endif
  1547. case PROC_TID_MEM:
  1548. case PROC_TGID_MEM:
  1549. inode->i_fop = &proc_mem_operations;
  1550. break;
  1551. #ifdef CONFIG_SECCOMP
  1552. case PROC_TID_SECCOMP:
  1553. case PROC_TGID_SECCOMP:
  1554. inode->i_fop = &proc_seccomp_operations;
  1555. break;
  1556. #endif /* CONFIG_SECCOMP */
  1557. case PROC_TID_MOUNTS:
  1558. case PROC_TGID_MOUNTS:
  1559. inode->i_fop = &proc_mounts_operations;
  1560. break;
  1561. #ifdef CONFIG_MMU
  1562. case PROC_TID_SMAPS:
  1563. case PROC_TGID_SMAPS:
  1564. inode->i_fop = &proc_smaps_operations;
  1565. break;
  1566. #endif
  1567. case PROC_TID_MOUNTSTATS:
  1568. case PROC_TGID_MOUNTSTATS:
  1569. inode->i_fop = &proc_mountstats_operations;
  1570. break;
  1571. #ifdef CONFIG_SECURITY
  1572. case PROC_TID_ATTR:
  1573. inode->i_nlink = 2;
  1574. inode->i_op = &proc_tid_attr_inode_operations;
  1575. inode->i_fop = &proc_tid_attr_operations;
  1576. break;
  1577. case PROC_TGID_ATTR:
  1578. inode->i_nlink = 2;
  1579. inode->i_op = &proc_tgid_attr_inode_operations;
  1580. inode->i_fop = &proc_tgid_attr_operations;
  1581. break;
  1582. case PROC_TID_ATTR_CURRENT:
  1583. case PROC_TGID_ATTR_CURRENT:
  1584. case PROC_TID_ATTR_PREV:
  1585. case PROC_TGID_ATTR_PREV:
  1586. case PROC_TID_ATTR_EXEC:
  1587. case PROC_TGID_ATTR_EXEC:
  1588. case PROC_TID_ATTR_FSCREATE:
  1589. case PROC_TGID_ATTR_FSCREATE:
  1590. case PROC_TID_ATTR_KEYCREATE:
  1591. case PROC_TGID_ATTR_KEYCREATE:
  1592. inode->i_fop = &proc_pid_attr_operations;
  1593. break;
  1594. #endif
  1595. #ifdef CONFIG_KALLSYMS
  1596. case PROC_TID_WCHAN:
  1597. case PROC_TGID_WCHAN:
  1598. inode->i_fop = &proc_info_file_operations;
  1599. ei->op.proc_read = proc_pid_wchan;
  1600. break;
  1601. #endif
  1602. #ifdef CONFIG_SCHEDSTATS
  1603. case PROC_TID_SCHEDSTAT:
  1604. case PROC_TGID_SCHEDSTAT:
  1605. inode->i_fop = &proc_info_file_operations;
  1606. ei->op.proc_read = proc_pid_schedstat;
  1607. break;
  1608. #endif
  1609. #ifdef CONFIG_CPUSETS
  1610. case PROC_TID_CPUSET:
  1611. case PROC_TGID_CPUSET:
  1612. inode->i_fop = &proc_cpuset_operations;
  1613. break;
  1614. #endif
  1615. case PROC_TID_OOM_SCORE:
  1616. case PROC_TGID_OOM_SCORE:
  1617. inode->i_fop = &proc_info_file_operations;
  1618. ei->op.proc_read = proc_oom_score;
  1619. break;
  1620. case PROC_TID_OOM_ADJUST:
  1621. case PROC_TGID_OOM_ADJUST:
  1622. inode->i_fop = &proc_oom_adjust_operations;
  1623. break;
  1624. #ifdef CONFIG_AUDITSYSCALL
  1625. case PROC_TID_LOGINUID:
  1626. case PROC_TGID_LOGINUID:
  1627. inode->i_fop = &proc_loginuid_operations;
  1628. break;
  1629. #endif
  1630. default:
  1631. printk("procfs: impossible type (%d)",p->type);
  1632. iput(inode);
  1633. error = ERR_PTR(-EINVAL);
  1634. goto out;
  1635. }
  1636. dentry->d_op = &pid_dentry_operations;
  1637. d_add(dentry, inode);
  1638. /* Close the race of the process dying before we return the dentry */
  1639. if (pid_revalidate(dentry, NULL))
  1640. error = NULL;
  1641. out:
  1642. put_task_struct(task);
  1643. out_no_task:
  1644. return error;
  1645. }
  1646. static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
  1647. return proc_pident_lookup(dir, dentry, tgid_base_stuff);
  1648. }
  1649. static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
  1650. return proc_pident_lookup(dir, dentry, tid_base_stuff);
  1651. }
  1652. static struct file_operations proc_tgid_base_operations = {
  1653. .read = generic_read_dir,
  1654. .readdir = proc_tgid_base_readdir,
  1655. };
  1656. static struct file_operations proc_tid_base_operations = {
  1657. .read = generic_read_dir,
  1658. .readdir = proc_tid_base_readdir,
  1659. };
  1660. static struct inode_operations proc_tgid_base_inode_operations = {
  1661. .lookup = proc_tgid_base_lookup,
  1662. .getattr = pid_getattr,
  1663. };
  1664. static struct inode_operations proc_tid_base_inode_operations = {
  1665. .lookup = proc_tid_base_lookup,
  1666. .getattr = pid_getattr,
  1667. };
  1668. #ifdef CONFIG_SECURITY
  1669. static int proc_tgid_attr_readdir(struct file * filp,
  1670. void * dirent, filldir_t filldir)
  1671. {
  1672. return proc_pident_readdir(filp,dirent,filldir,
  1673. tgid_attr_stuff,ARRAY_SIZE(tgid_attr_stuff));
  1674. }
  1675. static int proc_tid_attr_readdir(struct file * filp,
  1676. void * dirent, filldir_t filldir)
  1677. {
  1678. return proc_pident_readdir(filp,dirent,filldir,
  1679. tid_attr_stuff,ARRAY_SIZE(tid_attr_stuff));
  1680. }
  1681. static struct file_operations proc_tgid_attr_operations = {
  1682. .read = generic_read_dir,
  1683. .readdir = proc_tgid_attr_readdir,
  1684. };
  1685. static struct file_operations proc_tid_attr_operations = {
  1686. .read = generic_read_dir,
  1687. .readdir = proc_tid_attr_readdir,
  1688. };
  1689. static struct dentry *proc_tgid_attr_lookup(struct inode *dir,
  1690. struct dentry *dentry, struct nameidata *nd)
  1691. {
  1692. return proc_pident_lookup(dir, dentry, tgid_attr_stuff);
  1693. }
  1694. static struct dentry *proc_tid_attr_lookup(struct inode *dir,
  1695. struct dentry *dentry, struct nameidata *nd)
  1696. {
  1697. return proc_pident_lookup(dir, dentry, tid_attr_stuff);
  1698. }
  1699. static struct inode_operations proc_tgid_attr_inode_operations = {
  1700. .lookup = proc_tgid_attr_lookup,
  1701. .getattr = pid_getattr,
  1702. };
  1703. static struct inode_operations proc_tid_attr_inode_operations = {
  1704. .lookup = proc_tid_attr_lookup,
  1705. .getattr = pid_getattr,
  1706. };
  1707. #endif
  1708. /*
  1709. * /proc/self:
  1710. */
  1711. static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
  1712. int buflen)
  1713. {
  1714. char tmp[PROC_NUMBUF];
  1715. sprintf(tmp, "%d", current->tgid);
  1716. return vfs_readlink(dentry,buffer,buflen,tmp);
  1717. }
  1718. static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
  1719. {
  1720. char tmp[PROC_NUMBUF];
  1721. sprintf(tmp, "%d", current->tgid);
  1722. return ERR_PTR(vfs_follow_link(nd,tmp));
  1723. }
  1724. static struct inode_operations proc_self_inode_operations = {
  1725. .readlink = proc_self_readlink,
  1726. .follow_link = proc_self_follow_link,
  1727. };
  1728. /**
  1729. * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
  1730. *
  1731. * @task: task that should be flushed.
  1732. *
  1733. * Looks in the dcache for
  1734. * /proc/@pid
  1735. * /proc/@tgid/task/@pid
  1736. * if either directory is present flushes it and all of it'ts children
  1737. * from the dcache.
  1738. *
  1739. * It is safe and reasonable to cache /proc entries for a task until
  1740. * that task exits. After that they just clog up the dcache with
  1741. * useless entries, possibly causing useful dcache entries to be
  1742. * flushed instead. This routine is proved to flush those useless
  1743. * dcache entries at process exit time.
  1744. *
  1745. * NOTE: This routine is just an optimization so it does not guarantee
  1746. * that no dcache entries will exist at process exit time it
  1747. * just makes it very unlikely that any will persist.
  1748. */
  1749. void proc_flush_task(struct task_struct *task)
  1750. {
  1751. struct dentry *dentry, *leader, *dir;
  1752. char buf[PROC_NUMBUF];
  1753. struct qstr name;
  1754. name.name = buf;
  1755. name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
  1756. dentry = d_hash_and_lookup(proc_mnt->mnt_root, &name);
  1757. if (dentry) {
  1758. shrink_dcache_parent(dentry);
  1759. d_drop(dentry);
  1760. dput(dentry);
  1761. }
  1762. if (thread_group_leader(task))
  1763. goto out;
  1764. name.name = buf;
  1765. name.len = snprintf(buf, sizeof(buf), "%d", task->tgid);
  1766. leader = d_hash_and_lookup(proc_mnt->mnt_root, &name);
  1767. if (!leader)
  1768. goto out;
  1769. name.name = "task";
  1770. name.len = strlen(name.name);
  1771. dir = d_hash_and_lookup(leader, &name);
  1772. if (!dir)
  1773. goto out_put_leader;
  1774. name.name = buf;
  1775. name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
  1776. dentry = d_hash_and_lookup(dir, &name);
  1777. if (dentry) {
  1778. shrink_dcache_parent(dentry);
  1779. d_drop(dentry);
  1780. dput(dentry);
  1781. }
  1782. dput(dir);
  1783. out_put_leader:
  1784. dput(leader);
  1785. out:
  1786. return;
  1787. }
  1788. /* SMP-safe */
  1789. struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
  1790. {
  1791. struct dentry *result = ERR_PTR(-ENOENT);
  1792. struct task_struct *task;
  1793. struct inode *inode;
  1794. struct proc_inode *ei;
  1795. unsigned tgid;
  1796. if (dentry->d_name.len == 4 && !memcmp(dentry->d_name.name,"self",4)) {
  1797. inode = new_inode(dir->i_sb);
  1798. if (!inode)
  1799. return ERR_PTR(-ENOMEM);
  1800. ei = PROC_I(inode);
  1801. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  1802. inode->i_ino = fake_ino(0, PROC_TGID_INO);
  1803. ei->pde = NULL;
  1804. inode->i_mode = S_IFLNK|S_IRWXUGO;
  1805. inode->i_uid = inode->i_gid = 0;
  1806. inode->i_size = 64;
  1807. inode->i_op = &proc_self_inode_operations;
  1808. d_add(dentry, inode);
  1809. return NULL;
  1810. }
  1811. tgid = name_to_int(dentry);
  1812. if (tgid == ~0U)
  1813. goto out;
  1814. rcu_read_lock();
  1815. task = find_task_by_pid(tgid);
  1816. if (task)
  1817. get_task_struct(task);
  1818. rcu_read_unlock();
  1819. if (!task)
  1820. goto out;
  1821. inode = proc_pid_make_inode(dir->i_sb, task, PROC_TGID_INO);
  1822. if (!inode)
  1823. goto out_put_task;
  1824. inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
  1825. inode->i_op = &proc_tgid_base_inode_operations;
  1826. inode->i_fop = &proc_tgid_base_operations;
  1827. inode->i_flags|=S_IMMUTABLE;
  1828. #ifdef CONFIG_SECURITY
  1829. inode->i_nlink = 5;
  1830. #else
  1831. inode->i_nlink = 4;
  1832. #endif
  1833. dentry->d_op = &pid_dentry_operations;
  1834. d_add(dentry, inode);
  1835. /* Close the race of the process dying before we return the dentry */
  1836. if (pid_revalidate(dentry, NULL))
  1837. result = NULL;
  1838. out_put_task:
  1839. put_task_struct(task);
  1840. out:
  1841. return result;
  1842. }
  1843. /* SMP-safe */
  1844. static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
  1845. {
  1846. struct dentry *result = ERR_PTR(-ENOENT);
  1847. struct task_struct *task;
  1848. struct task_struct *leader = get_proc_task(dir);
  1849. struct inode *inode;
  1850. unsigned tid;
  1851. if (!leader)
  1852. goto out_no_task;
  1853. tid = name_to_int(dentry);
  1854. if (tid == ~0U)
  1855. goto out;
  1856. rcu_read_lock();
  1857. task = find_task_by_pid(tid);
  1858. if (task)
  1859. get_task_struct(task);
  1860. rcu_read_unlock();
  1861. if (!task)
  1862. goto out;
  1863. if (leader->tgid != task->tgid)
  1864. goto out_drop_task;
  1865. inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_INO);
  1866. if (!inode)
  1867. goto out_drop_task;
  1868. inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
  1869. inode->i_op = &proc_tid_base_inode_operations;
  1870. inode->i_fop = &proc_tid_base_operations;
  1871. inode->i_flags|=S_IMMUTABLE;
  1872. #ifdef CONFIG_SECURITY
  1873. inode->i_nlink = 4;
  1874. #else
  1875. inode->i_nlink = 3;
  1876. #endif
  1877. dentry->d_op = &pid_dentry_operations;
  1878. d_add(dentry, inode);
  1879. /* Close the race of the process dying before we return the dentry */
  1880. if (pid_revalidate(dentry, NULL))
  1881. result = NULL;
  1882. out_drop_task:
  1883. put_task_struct(task);
  1884. out:
  1885. put_task_struct(leader);
  1886. out_no_task:
  1887. return result;
  1888. }
  1889. /*
  1890. * Find the first tgid to return to user space.
  1891. *
  1892. * Usually this is just whatever follows &init_task, but if the users
  1893. * buffer was too small to hold the full list or there was a seek into
  1894. * the middle of the directory we have more work to do.
  1895. *
  1896. * In the case of a short read we start with find_task_by_pid.
  1897. *
  1898. * In the case of a seek we start with &init_task and walk nr
  1899. * threads past it.
  1900. */
  1901. static struct task_struct *first_tgid(int tgid, unsigned int nr)
  1902. {
  1903. struct task_struct *pos;
  1904. rcu_read_lock();
  1905. if (tgid && nr) {
  1906. pos = find_task_by_pid(tgid);
  1907. if (pos && thread_group_leader(pos))
  1908. goto found;
  1909. }
  1910. /* If nr exceeds the number of processes get out quickly */
  1911. pos = NULL;
  1912. if (nr && nr >= nr_processes())
  1913. goto done;
  1914. /* If we haven't found our starting place yet start with
  1915. * the init_task and walk nr tasks forward.
  1916. */
  1917. for (pos = next_task(&init_task); nr > 0; --nr) {
  1918. pos = next_task(pos);
  1919. if (pos == &init_task) {
  1920. pos = NULL;
  1921. goto done;
  1922. }
  1923. }
  1924. found:
  1925. get_task_struct(pos);
  1926. done:
  1927. rcu_read_unlock();
  1928. return pos;
  1929. }
  1930. /*
  1931. * Find the next task in the task list.
  1932. * Return NULL if we loop or there is any error.
  1933. *
  1934. * The reference to the input task_struct is released.
  1935. */
  1936. static struct task_struct *next_tgid(struct task_struct *start)
  1937. {
  1938. struct task_struct *pos;
  1939. rcu_read_lock();
  1940. pos = start;
  1941. if (pid_alive(start))
  1942. pos = next_task(start);
  1943. if (pid_alive(pos) && (pos != &init_task)) {
  1944. get_task_struct(pos);
  1945. goto done;
  1946. }
  1947. pos = NULL;
  1948. done:
  1949. rcu_read_unlock();
  1950. put_task_struct(start);
  1951. return pos;
  1952. }
  1953. /* for the /proc/ directory itself, after non-process stuff has been done */
  1954. int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
  1955. {
  1956. char buf[PROC_NUMBUF];
  1957. unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
  1958. struct task_struct *task;
  1959. int tgid;
  1960. if (!nr) {
  1961. ino_t ino = fake_ino(0,PROC_TGID_INO);
  1962. if (filldir(dirent, "self", 4, filp->f_pos, ino, DT_LNK) < 0)
  1963. return 0;
  1964. filp->f_pos++;
  1965. nr++;
  1966. }
  1967. nr -= 1;
  1968. /* f_version caches the tgid value that the last readdir call couldn't
  1969. * return. lseek aka telldir automagically resets f_version to 0.
  1970. */
  1971. tgid = filp->f_version;
  1972. filp->f_version = 0;
  1973. for (task = first_tgid(tgid, nr);
  1974. task;
  1975. task = next_tgid(task), filp->f_pos++) {
  1976. int len;
  1977. ino_t ino;
  1978. tgid = task->pid;
  1979. len = snprintf(buf, sizeof(buf), "%d", tgid);
  1980. ino = fake_ino(tgid, PROC_TGID_INO);
  1981. if (filldir(dirent, buf, len, filp->f_pos, ino, DT_DIR) < 0) {
  1982. /* returning this tgid failed, save it as the first
  1983. * pid for the next readir call */
  1984. filp->f_version = tgid;
  1985. put_task_struct(task);
  1986. break;
  1987. }
  1988. }
  1989. return 0;
  1990. }
  1991. /*
  1992. * Find the first tid of a thread group to return to user space.
  1993. *
  1994. * Usually this is just the thread group leader, but if the users
  1995. * buffer was too small or there was a seek into the middle of the
  1996. * directory we have more work todo.
  1997. *
  1998. * In the case of a short read we start with find_task_by_pid.
  1999. *
  2000. * In the case of a seek we start with the leader and walk nr
  2001. * threads past it.
  2002. */
  2003. static struct task_struct *first_tid(struct task_struct *leader, int tid, int nr)
  2004. {
  2005. struct task_struct *pos = NULL;
  2006. read_lock(&tasklist_lock);
  2007. /* Attempt to start with the pid of a thread */
  2008. if (tid && (nr > 0)) {
  2009. pos = find_task_by_pid(tid);
  2010. if (pos && (pos->group_leader != leader))
  2011. pos = NULL;
  2012. if (pos)
  2013. nr = 0;
  2014. }
  2015. /* If nr exceeds the number of threads there is nothing todo */
  2016. if (nr) {
  2017. if (nr >= get_nr_threads(leader))
  2018. goto done;
  2019. }
  2020. /* If we haven't found our starting place yet start with the
  2021. * leader and walk nr threads forward.
  2022. */
  2023. if (!pos && (nr >= 0))
  2024. pos = leader;
  2025. for (; pos && pid_alive(pos); pos = next_thread(pos)) {
  2026. if (--nr > 0)
  2027. continue;
  2028. get_task_struct(pos);
  2029. goto done;
  2030. }
  2031. pos = NULL;
  2032. done:
  2033. read_unlock(&tasklist_lock);
  2034. return pos;
  2035. }
  2036. /*
  2037. * Find the next thread in the thread list.
  2038. * Return NULL if there is an error or no next thread.
  2039. *
  2040. * The reference to the input task_struct is released.
  2041. */
  2042. static struct task_struct *next_tid(struct task_struct *start)
  2043. {
  2044. struct task_struct *pos;
  2045. read_lock(&tasklist_lock);
  2046. pos = start;
  2047. if (pid_alive(start))
  2048. pos = next_thread(start);
  2049. if (pid_alive(pos) && (pos != start->group_leader))
  2050. get_task_struct(pos);
  2051. else
  2052. pos = NULL;
  2053. read_unlock(&tasklist_lock);
  2054. put_task_struct(start);
  2055. return pos;
  2056. }
  2057. /* for the /proc/TGID/task/ directories */
  2058. static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
  2059. {
  2060. char buf[PROC_NUMBUF];
  2061. struct dentry *dentry = filp->f_dentry;
  2062. struct inode *inode = dentry->d_inode;
  2063. struct task_struct *leader = get_proc_task(inode);
  2064. struct task_struct *task;
  2065. int retval = -ENOENT;
  2066. ino_t ino;
  2067. int tid;
  2068. unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
  2069. if (!leader)
  2070. goto out_no_task;
  2071. retval = 0;
  2072. switch (pos) {
  2073. case 0:
  2074. ino = inode->i_ino;
  2075. if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
  2076. goto out;
  2077. pos++;
  2078. /* fall through */
  2079. case 1:
  2080. ino = parent_ino(dentry);
  2081. if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
  2082. goto out;
  2083. pos++;
  2084. /* fall through */
  2085. }
  2086. /* f_version caches the tgid value that the last readdir call couldn't
  2087. * return. lseek aka telldir automagically resets f_version to 0.
  2088. */
  2089. tid = filp->f_version;
  2090. filp->f_version = 0;
  2091. for (task = first_tid(leader, tid, pos - 2);
  2092. task;
  2093. task = next_tid(task), pos++) {
  2094. int len;
  2095. tid = task->pid;
  2096. len = snprintf(buf, sizeof(buf), "%d", tid);
  2097. ino = fake_ino(tid, PROC_TID_INO);
  2098. if (filldir(dirent, buf, len, pos, ino, DT_DIR < 0)) {
  2099. /* returning this tgid failed, save it as the first
  2100. * pid for the next readir call */
  2101. filp->f_version = tid;
  2102. put_task_struct(task);
  2103. break;
  2104. }
  2105. }
  2106. out:
  2107. filp->f_pos = pos;
  2108. put_task_struct(leader);
  2109. out_no_task:
  2110. return retval;
  2111. }
  2112. static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
  2113. {
  2114. struct inode *inode = dentry->d_inode;
  2115. struct task_struct *p = get_proc_task(inode);
  2116. generic_fillattr(inode, stat);
  2117. if (p) {
  2118. rcu_read_lock();
  2119. stat->nlink += get_nr_threads(p);
  2120. rcu_read_unlock();
  2121. put_task_struct(p);
  2122. }
  2123. return 0;
  2124. }