base.c 56 KB

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