base.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394
  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. static int proc_setattr(struct dentry *dentry, struct iattr *attr)
  515. {
  516. int error;
  517. struct inode *inode = dentry->d_inode;
  518. if (attr->ia_valid & ATTR_MODE)
  519. return -EPERM;
  520. error = inode_change_ok(inode, attr);
  521. if (!error) {
  522. error = security_inode_setattr(dentry, attr);
  523. if (!error)
  524. error = inode_setattr(inode, attr);
  525. }
  526. return error;
  527. }
  528. static struct inode_operations proc_def_inode_operations = {
  529. .setattr = proc_setattr,
  530. };
  531. extern struct seq_operations mounts_op;
  532. struct proc_mounts {
  533. struct seq_file m;
  534. int event;
  535. };
  536. static int mounts_open(struct inode *inode, struct file *file)
  537. {
  538. struct task_struct *task = get_proc_task(inode);
  539. struct namespace *namespace = NULL;
  540. struct proc_mounts *p;
  541. int ret = -EINVAL;
  542. if (task) {
  543. task_lock(task);
  544. namespace = task->namespace;
  545. if (namespace)
  546. get_namespace(namespace);
  547. task_unlock(task);
  548. put_task_struct(task);
  549. }
  550. if (namespace) {
  551. ret = -ENOMEM;
  552. p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
  553. if (p) {
  554. file->private_data = &p->m;
  555. ret = seq_open(file, &mounts_op);
  556. if (!ret) {
  557. p->m.private = namespace;
  558. p->event = namespace->event;
  559. return 0;
  560. }
  561. kfree(p);
  562. }
  563. put_namespace(namespace);
  564. }
  565. return ret;
  566. }
  567. static int mounts_release(struct inode *inode, struct file *file)
  568. {
  569. struct seq_file *m = file->private_data;
  570. struct namespace *namespace = m->private;
  571. put_namespace(namespace);
  572. return seq_release(inode, file);
  573. }
  574. static unsigned mounts_poll(struct file *file, poll_table *wait)
  575. {
  576. struct proc_mounts *p = file->private_data;
  577. struct namespace *ns = p->m.private;
  578. unsigned res = 0;
  579. poll_wait(file, &ns->poll, wait);
  580. spin_lock(&vfsmount_lock);
  581. if (p->event != ns->event) {
  582. p->event = ns->event;
  583. res = POLLERR;
  584. }
  585. spin_unlock(&vfsmount_lock);
  586. return res;
  587. }
  588. static struct file_operations proc_mounts_operations = {
  589. .open = mounts_open,
  590. .read = seq_read,
  591. .llseek = seq_lseek,
  592. .release = mounts_release,
  593. .poll = mounts_poll,
  594. };
  595. extern struct seq_operations mountstats_op;
  596. static int mountstats_open(struct inode *inode, struct file *file)
  597. {
  598. int ret = seq_open(file, &mountstats_op);
  599. if (!ret) {
  600. struct seq_file *m = file->private_data;
  601. struct namespace *namespace = NULL;
  602. struct task_struct *task = get_proc_task(inode);
  603. if (task) {
  604. task_lock(task);
  605. namespace = task->namespace;
  606. if (namespace)
  607. get_namespace(namespace);
  608. task_unlock(task);
  609. put_task_struct(task);
  610. }
  611. if (namespace)
  612. m->private = namespace;
  613. else {
  614. seq_release(inode, file);
  615. ret = -EINVAL;
  616. }
  617. }
  618. return ret;
  619. }
  620. static struct file_operations proc_mountstats_operations = {
  621. .open = mountstats_open,
  622. .read = seq_read,
  623. .llseek = seq_lseek,
  624. .release = mounts_release,
  625. };
  626. #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
  627. static ssize_t proc_info_read(struct file * file, char __user * buf,
  628. size_t count, loff_t *ppos)
  629. {
  630. struct inode * inode = file->f_dentry->d_inode;
  631. unsigned long page;
  632. ssize_t length;
  633. struct task_struct *task = get_proc_task(inode);
  634. length = -ESRCH;
  635. if (!task)
  636. goto out_no_task;
  637. if (count > PROC_BLOCK_SIZE)
  638. count = PROC_BLOCK_SIZE;
  639. length = -ENOMEM;
  640. if (!(page = __get_free_page(GFP_KERNEL)))
  641. goto out;
  642. length = PROC_I(inode)->op.proc_read(task, (char*)page);
  643. if (length >= 0)
  644. length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
  645. free_page(page);
  646. out:
  647. put_task_struct(task);
  648. out_no_task:
  649. return length;
  650. }
  651. static struct file_operations proc_info_file_operations = {
  652. .read = proc_info_read,
  653. };
  654. static int mem_open(struct inode* inode, struct file* file)
  655. {
  656. file->private_data = (void*)((long)current->self_exec_id);
  657. return 0;
  658. }
  659. static ssize_t mem_read(struct file * file, char __user * buf,
  660. size_t count, loff_t *ppos)
  661. {
  662. struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
  663. char *page;
  664. unsigned long src = *ppos;
  665. int ret = -ESRCH;
  666. struct mm_struct *mm;
  667. if (!task)
  668. goto out_no_task;
  669. if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
  670. goto out;
  671. ret = -ENOMEM;
  672. page = (char *)__get_free_page(GFP_USER);
  673. if (!page)
  674. goto out;
  675. ret = 0;
  676. mm = get_task_mm(task);
  677. if (!mm)
  678. goto out_free;
  679. ret = -EIO;
  680. if (file->private_data != (void*)((long)current->self_exec_id))
  681. goto out_put;
  682. ret = 0;
  683. while (count > 0) {
  684. int this_len, retval;
  685. this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
  686. retval = access_process_vm(task, src, page, this_len, 0);
  687. if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
  688. if (!ret)
  689. ret = -EIO;
  690. break;
  691. }
  692. if (copy_to_user(buf, page, retval)) {
  693. ret = -EFAULT;
  694. break;
  695. }
  696. ret += retval;
  697. src += retval;
  698. buf += retval;
  699. count -= retval;
  700. }
  701. *ppos = src;
  702. out_put:
  703. mmput(mm);
  704. out_free:
  705. free_page((unsigned long) page);
  706. out:
  707. put_task_struct(task);
  708. out_no_task:
  709. return ret;
  710. }
  711. #define mem_write NULL
  712. #ifndef mem_write
  713. /* This is a security hazard */
  714. static ssize_t mem_write(struct file * file, const char * buf,
  715. size_t count, loff_t *ppos)
  716. {
  717. int copied;
  718. char *page;
  719. struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
  720. unsigned long dst = *ppos;
  721. copied = -ESRCH;
  722. if (!task)
  723. goto out_no_task;
  724. if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
  725. goto out;
  726. copied = -ENOMEM;
  727. page = (char *)__get_free_page(GFP_USER);
  728. if (!page)
  729. goto out;
  730. copied = 0;
  731. while (count > 0) {
  732. int this_len, retval;
  733. this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
  734. if (copy_from_user(page, buf, this_len)) {
  735. copied = -EFAULT;
  736. break;
  737. }
  738. retval = access_process_vm(task, dst, page, this_len, 1);
  739. if (!retval) {
  740. if (!copied)
  741. copied = -EIO;
  742. break;
  743. }
  744. copied += retval;
  745. buf += retval;
  746. dst += retval;
  747. count -= retval;
  748. }
  749. *ppos = dst;
  750. free_page((unsigned long) page);
  751. out:
  752. put_task_struct(task);
  753. out_no_task:
  754. return copied;
  755. }
  756. #endif
  757. static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
  758. {
  759. switch (orig) {
  760. case 0:
  761. file->f_pos = offset;
  762. break;
  763. case 1:
  764. file->f_pos += offset;
  765. break;
  766. default:
  767. return -EINVAL;
  768. }
  769. force_successful_syscall_return();
  770. return file->f_pos;
  771. }
  772. static struct file_operations proc_mem_operations = {
  773. .llseek = mem_lseek,
  774. .read = mem_read,
  775. .write = mem_write,
  776. .open = mem_open,
  777. };
  778. static ssize_t oom_adjust_read(struct file *file, char __user *buf,
  779. size_t count, loff_t *ppos)
  780. {
  781. struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
  782. char buffer[PROC_NUMBUF];
  783. size_t len;
  784. int oom_adjust;
  785. loff_t __ppos = *ppos;
  786. if (!task)
  787. return -ESRCH;
  788. oom_adjust = task->oomkilladj;
  789. put_task_struct(task);
  790. len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
  791. if (__ppos >= len)
  792. return 0;
  793. if (count > len-__ppos)
  794. count = len-__ppos;
  795. if (copy_to_user(buf, buffer + __ppos, count))
  796. return -EFAULT;
  797. *ppos = __ppos + count;
  798. return count;
  799. }
  800. static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
  801. size_t count, loff_t *ppos)
  802. {
  803. struct task_struct *task;
  804. char buffer[PROC_NUMBUF], *end;
  805. int oom_adjust;
  806. if (!capable(CAP_SYS_RESOURCE))
  807. return -EPERM;
  808. memset(buffer, 0, sizeof(buffer));
  809. if (count > sizeof(buffer) - 1)
  810. count = sizeof(buffer) - 1;
  811. if (copy_from_user(buffer, buf, count))
  812. return -EFAULT;
  813. oom_adjust = simple_strtol(buffer, &end, 0);
  814. if ((oom_adjust < -16 || oom_adjust > 15) && oom_adjust != OOM_DISABLE)
  815. return -EINVAL;
  816. if (*end == '\n')
  817. end++;
  818. task = get_proc_task(file->f_dentry->d_inode);
  819. if (!task)
  820. return -ESRCH;
  821. task->oomkilladj = oom_adjust;
  822. put_task_struct(task);
  823. if (end - buffer == 0)
  824. return -EIO;
  825. return end - buffer;
  826. }
  827. static struct file_operations proc_oom_adjust_operations = {
  828. .read = oom_adjust_read,
  829. .write = oom_adjust_write,
  830. };
  831. #ifdef CONFIG_AUDITSYSCALL
  832. #define TMPBUFLEN 21
  833. static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
  834. size_t count, loff_t *ppos)
  835. {
  836. struct inode * inode = file->f_dentry->d_inode;
  837. struct task_struct *task = get_proc_task(inode);
  838. ssize_t length;
  839. char tmpbuf[TMPBUFLEN];
  840. if (!task)
  841. return -ESRCH;
  842. length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
  843. audit_get_loginuid(task->audit_context));
  844. put_task_struct(task);
  845. return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
  846. }
  847. static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
  848. size_t count, loff_t *ppos)
  849. {
  850. struct inode * inode = file->f_dentry->d_inode;
  851. char *page, *tmp;
  852. ssize_t length;
  853. uid_t loginuid;
  854. if (!capable(CAP_AUDIT_CONTROL))
  855. return -EPERM;
  856. if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
  857. return -EPERM;
  858. if (count >= PAGE_SIZE)
  859. count = PAGE_SIZE - 1;
  860. if (*ppos != 0) {
  861. /* No partial writes. */
  862. return -EINVAL;
  863. }
  864. page = (char*)__get_free_page(GFP_USER);
  865. if (!page)
  866. return -ENOMEM;
  867. length = -EFAULT;
  868. if (copy_from_user(page, buf, count))
  869. goto out_free_page;
  870. page[count] = '\0';
  871. loginuid = simple_strtoul(page, &tmp, 10);
  872. if (tmp == page) {
  873. length = -EINVAL;
  874. goto out_free_page;
  875. }
  876. length = audit_set_loginuid(current, loginuid);
  877. if (likely(length == 0))
  878. length = count;
  879. out_free_page:
  880. free_page((unsigned long) page);
  881. return length;
  882. }
  883. static struct file_operations proc_loginuid_operations = {
  884. .read = proc_loginuid_read,
  885. .write = proc_loginuid_write,
  886. };
  887. #endif
  888. #ifdef CONFIG_SECCOMP
  889. static ssize_t seccomp_read(struct file *file, char __user *buf,
  890. size_t count, loff_t *ppos)
  891. {
  892. struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
  893. char __buf[20];
  894. loff_t __ppos = *ppos;
  895. size_t len;
  896. if (!tsk)
  897. return -ESRCH;
  898. /* no need to print the trailing zero, so use only len */
  899. len = sprintf(__buf, "%u\n", tsk->seccomp.mode);
  900. put_task_struct(tsk);
  901. if (__ppos >= len)
  902. return 0;
  903. if (count > len - __ppos)
  904. count = len - __ppos;
  905. if (copy_to_user(buf, __buf + __ppos, count))
  906. return -EFAULT;
  907. *ppos = __ppos + count;
  908. return count;
  909. }
  910. static ssize_t seccomp_write(struct file *file, const char __user *buf,
  911. size_t count, loff_t *ppos)
  912. {
  913. struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
  914. char __buf[20], *end;
  915. unsigned int seccomp_mode;
  916. ssize_t result;
  917. result = -ESRCH;
  918. if (!tsk)
  919. goto out_no_task;
  920. /* can set it only once to be even more secure */
  921. result = -EPERM;
  922. if (unlikely(tsk->seccomp.mode))
  923. goto out;
  924. result = -EFAULT;
  925. memset(__buf, 0, sizeof(__buf));
  926. count = min(count, sizeof(__buf) - 1);
  927. if (copy_from_user(__buf, buf, count))
  928. goto out;
  929. seccomp_mode = simple_strtoul(__buf, &end, 0);
  930. if (*end == '\n')
  931. end++;
  932. result = -EINVAL;
  933. if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) {
  934. tsk->seccomp.mode = seccomp_mode;
  935. set_tsk_thread_flag(tsk, TIF_SECCOMP);
  936. } else
  937. goto out;
  938. result = -EIO;
  939. if (unlikely(!(end - __buf)))
  940. goto out;
  941. result = end - __buf;
  942. out:
  943. put_task_struct(tsk);
  944. out_no_task:
  945. return result;
  946. }
  947. static struct file_operations proc_seccomp_operations = {
  948. .read = seccomp_read,
  949. .write = seccomp_write,
  950. };
  951. #endif /* CONFIG_SECCOMP */
  952. static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
  953. {
  954. struct inode *inode = dentry->d_inode;
  955. int error = -EACCES;
  956. /* We don't need a base pointer in the /proc filesystem */
  957. path_release(nd);
  958. /* Are we allowed to snoop on the tasks file descriptors? */
  959. if (!proc_fd_access_allowed(inode))
  960. goto out;
  961. error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
  962. nd->last_type = LAST_BIND;
  963. out:
  964. return ERR_PTR(error);
  965. }
  966. static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
  967. char __user *buffer, int buflen)
  968. {
  969. struct inode * inode;
  970. char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
  971. int len;
  972. if (!tmp)
  973. return -ENOMEM;
  974. inode = dentry->d_inode;
  975. path = d_path(dentry, mnt, tmp, PAGE_SIZE);
  976. len = PTR_ERR(path);
  977. if (IS_ERR(path))
  978. goto out;
  979. len = tmp + PAGE_SIZE - 1 - path;
  980. if (len > buflen)
  981. len = buflen;
  982. if (copy_to_user(buffer, path, len))
  983. len = -EFAULT;
  984. out:
  985. free_page((unsigned long)tmp);
  986. return len;
  987. }
  988. static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
  989. {
  990. int error = -EACCES;
  991. struct inode *inode = dentry->d_inode;
  992. struct dentry *de;
  993. struct vfsmount *mnt = NULL;
  994. /* Are we allowed to snoop on the tasks file descriptors? */
  995. if (!proc_fd_access_allowed(inode))
  996. goto out;
  997. error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
  998. if (error)
  999. goto out;
  1000. error = do_proc_readlink(de, mnt, buffer, buflen);
  1001. dput(de);
  1002. mntput(mnt);
  1003. out:
  1004. return error;
  1005. }
  1006. static struct inode_operations proc_pid_link_inode_operations = {
  1007. .readlink = proc_pid_readlink,
  1008. .follow_link = proc_pid_follow_link,
  1009. .setattr = proc_setattr,
  1010. };
  1011. static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
  1012. {
  1013. struct dentry *dentry = filp->f_dentry;
  1014. struct inode *inode = dentry->d_inode;
  1015. struct task_struct *p = get_proc_task(inode);
  1016. unsigned int fd, tid, ino;
  1017. int retval;
  1018. char buf[PROC_NUMBUF];
  1019. struct files_struct * files;
  1020. struct fdtable *fdt;
  1021. retval = -ENOENT;
  1022. if (!p)
  1023. goto out_no_task;
  1024. retval = 0;
  1025. tid = p->pid;
  1026. fd = filp->f_pos;
  1027. switch (fd) {
  1028. case 0:
  1029. if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
  1030. goto out;
  1031. filp->f_pos++;
  1032. case 1:
  1033. ino = parent_ino(dentry);
  1034. if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
  1035. goto out;
  1036. filp->f_pos++;
  1037. default:
  1038. files = get_files_struct(p);
  1039. if (!files)
  1040. goto out;
  1041. rcu_read_lock();
  1042. fdt = files_fdtable(files);
  1043. for (fd = filp->f_pos-2;
  1044. fd < fdt->max_fds;
  1045. fd++, filp->f_pos++) {
  1046. unsigned int i,j;
  1047. if (!fcheck_files(files, fd))
  1048. continue;
  1049. rcu_read_unlock();
  1050. j = PROC_NUMBUF;
  1051. i = fd;
  1052. do {
  1053. j--;
  1054. buf[j] = '0' + (i % 10);
  1055. i /= 10;
  1056. } while (i);
  1057. ino = fake_ino(tid, PROC_TID_FD_DIR + fd);
  1058. if (filldir(dirent, buf+j, PROC_NUMBUF-j, fd+2, ino, DT_LNK) < 0) {
  1059. rcu_read_lock();
  1060. break;
  1061. }
  1062. rcu_read_lock();
  1063. }
  1064. rcu_read_unlock();
  1065. put_files_struct(files);
  1066. }
  1067. out:
  1068. put_task_struct(p);
  1069. out_no_task:
  1070. return retval;
  1071. }
  1072. static int proc_pident_readdir(struct file *filp,
  1073. void *dirent, filldir_t filldir,
  1074. struct pid_entry *ents, unsigned int nents)
  1075. {
  1076. int i;
  1077. int pid;
  1078. struct dentry *dentry = filp->f_dentry;
  1079. struct inode *inode = dentry->d_inode;
  1080. struct task_struct *task = get_proc_task(inode);
  1081. struct pid_entry *p;
  1082. ino_t ino;
  1083. int ret;
  1084. ret = -ENOENT;
  1085. if (!task)
  1086. goto out;
  1087. ret = 0;
  1088. pid = task->pid;
  1089. put_task_struct(task);
  1090. i = filp->f_pos;
  1091. switch (i) {
  1092. case 0:
  1093. ino = inode->i_ino;
  1094. if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
  1095. goto out;
  1096. i++;
  1097. filp->f_pos++;
  1098. /* fall through */
  1099. case 1:
  1100. ino = parent_ino(dentry);
  1101. if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
  1102. goto out;
  1103. i++;
  1104. filp->f_pos++;
  1105. /* fall through */
  1106. default:
  1107. i -= 2;
  1108. if (i >= nents) {
  1109. ret = 1;
  1110. goto out;
  1111. }
  1112. p = ents + i;
  1113. while (p->name) {
  1114. if (filldir(dirent, p->name, p->len, filp->f_pos,
  1115. fake_ino(pid, p->type), p->mode >> 12) < 0)
  1116. goto out;
  1117. filp->f_pos++;
  1118. p++;
  1119. }
  1120. }
  1121. ret = 1;
  1122. out:
  1123. return ret;
  1124. }
  1125. static int proc_tgid_base_readdir(struct file * filp,
  1126. void * dirent, filldir_t filldir)
  1127. {
  1128. return proc_pident_readdir(filp,dirent,filldir,
  1129. tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
  1130. }
  1131. static int proc_tid_base_readdir(struct file * filp,
  1132. void * dirent, filldir_t filldir)
  1133. {
  1134. return proc_pident_readdir(filp,dirent,filldir,
  1135. tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
  1136. }
  1137. /* building an inode */
  1138. static int task_dumpable(struct task_struct *task)
  1139. {
  1140. int dumpable = 0;
  1141. struct mm_struct *mm;
  1142. task_lock(task);
  1143. mm = task->mm;
  1144. if (mm)
  1145. dumpable = mm->dumpable;
  1146. task_unlock(task);
  1147. if(dumpable == 1)
  1148. return 1;
  1149. return 0;
  1150. }
  1151. static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task, int ino)
  1152. {
  1153. struct inode * inode;
  1154. struct proc_inode *ei;
  1155. /* We need a new inode */
  1156. inode = new_inode(sb);
  1157. if (!inode)
  1158. goto out;
  1159. /* Common stuff */
  1160. ei = PROC_I(inode);
  1161. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  1162. inode->i_ino = fake_ino(task->pid, ino);
  1163. inode->i_op = &proc_def_inode_operations;
  1164. /*
  1165. * grab the reference to task.
  1166. */
  1167. ei->pid = get_pid(task->pids[PIDTYPE_PID].pid);
  1168. if (!ei->pid)
  1169. goto out_unlock;
  1170. inode->i_uid = 0;
  1171. inode->i_gid = 0;
  1172. if (task_dumpable(task)) {
  1173. inode->i_uid = task->euid;
  1174. inode->i_gid = task->egid;
  1175. }
  1176. security_task_to_inode(task, inode);
  1177. out:
  1178. return inode;
  1179. out_unlock:
  1180. iput(inode);
  1181. return NULL;
  1182. }
  1183. /* dentry stuff */
  1184. /*
  1185. * Exceptional case: normally we are not allowed to unhash a busy
  1186. * directory. In this case, however, we can do it - no aliasing problems
  1187. * due to the way we treat inodes.
  1188. *
  1189. * Rewrite the inode's ownerships here because the owning task may have
  1190. * performed a setuid(), etc.
  1191. *
  1192. * Before the /proc/pid/status file was created the only way to read
  1193. * the effective uid of a /process was to stat /proc/pid. Reading
  1194. * /proc/pid/status is slow enough that procps and other packages
  1195. * kept stating /proc/pid. To keep the rules in /proc simple I have
  1196. * made this apply to all per process world readable and executable
  1197. * directories.
  1198. */
  1199. static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
  1200. {
  1201. struct inode *inode = dentry->d_inode;
  1202. struct task_struct *task = get_proc_task(inode);
  1203. if (task) {
  1204. if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
  1205. task_dumpable(task)) {
  1206. inode->i_uid = task->euid;
  1207. inode->i_gid = task->egid;
  1208. } else {
  1209. inode->i_uid = 0;
  1210. inode->i_gid = 0;
  1211. }
  1212. inode->i_mode &= ~(S_ISUID | S_ISGID);
  1213. security_task_to_inode(task, inode);
  1214. put_task_struct(task);
  1215. return 1;
  1216. }
  1217. d_drop(dentry);
  1218. return 0;
  1219. }
  1220. static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
  1221. {
  1222. struct inode *inode = dentry->d_inode;
  1223. struct task_struct *task;
  1224. generic_fillattr(inode, stat);
  1225. rcu_read_lock();
  1226. stat->uid = 0;
  1227. stat->gid = 0;
  1228. task = pid_task(proc_pid(inode), PIDTYPE_PID);
  1229. if (task) {
  1230. if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
  1231. task_dumpable(task)) {
  1232. stat->uid = task->euid;
  1233. stat->gid = task->egid;
  1234. }
  1235. }
  1236. rcu_read_unlock();
  1237. return 0;
  1238. }
  1239. static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
  1240. {
  1241. struct inode *inode = dentry->d_inode;
  1242. struct task_struct *task = get_proc_task(inode);
  1243. int fd = proc_fd(inode);
  1244. struct files_struct *files;
  1245. if (task) {
  1246. files = get_files_struct(task);
  1247. if (files) {
  1248. rcu_read_lock();
  1249. if (fcheck_files(files, fd)) {
  1250. rcu_read_unlock();
  1251. put_files_struct(files);
  1252. if (task_dumpable(task)) {
  1253. inode->i_uid = task->euid;
  1254. inode->i_gid = task->egid;
  1255. } else {
  1256. inode->i_uid = 0;
  1257. inode->i_gid = 0;
  1258. }
  1259. inode->i_mode &= ~(S_ISUID | S_ISGID);
  1260. security_task_to_inode(task, inode);
  1261. put_task_struct(task);
  1262. return 1;
  1263. }
  1264. rcu_read_unlock();
  1265. put_files_struct(files);
  1266. }
  1267. put_task_struct(task);
  1268. }
  1269. d_drop(dentry);
  1270. return 0;
  1271. }
  1272. static int pid_delete_dentry(struct dentry * dentry)
  1273. {
  1274. /* Is the task we represent dead?
  1275. * If so, then don't put the dentry on the lru list,
  1276. * kill it immediately.
  1277. */
  1278. return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
  1279. }
  1280. static struct dentry_operations tid_fd_dentry_operations =
  1281. {
  1282. .d_revalidate = tid_fd_revalidate,
  1283. .d_delete = pid_delete_dentry,
  1284. };
  1285. static struct dentry_operations pid_dentry_operations =
  1286. {
  1287. .d_revalidate = pid_revalidate,
  1288. .d_delete = pid_delete_dentry,
  1289. };
  1290. /* Lookups */
  1291. static unsigned name_to_int(struct dentry *dentry)
  1292. {
  1293. const char *name = dentry->d_name.name;
  1294. int len = dentry->d_name.len;
  1295. unsigned n = 0;
  1296. if (len > 1 && *name == '0')
  1297. goto out;
  1298. while (len-- > 0) {
  1299. unsigned c = *name++ - '0';
  1300. if (c > 9)
  1301. goto out;
  1302. if (n >= (~0U-9)/10)
  1303. goto out;
  1304. n *= 10;
  1305. n += c;
  1306. }
  1307. return n;
  1308. out:
  1309. return ~0U;
  1310. }
  1311. /* SMP-safe */
  1312. static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
  1313. {
  1314. struct task_struct *task = get_proc_task(dir);
  1315. unsigned fd = name_to_int(dentry);
  1316. struct dentry *result = ERR_PTR(-ENOENT);
  1317. struct file * file;
  1318. struct files_struct * files;
  1319. struct inode *inode;
  1320. struct proc_inode *ei;
  1321. if (!task)
  1322. goto out_no_task;
  1323. if (fd == ~0U)
  1324. goto out;
  1325. inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_FD_DIR+fd);
  1326. if (!inode)
  1327. goto out;
  1328. ei = PROC_I(inode);
  1329. ei->fd = fd;
  1330. files = get_files_struct(task);
  1331. if (!files)
  1332. goto out_unlock;
  1333. inode->i_mode = S_IFLNK;
  1334. /*
  1335. * We are not taking a ref to the file structure, so we must
  1336. * hold ->file_lock.
  1337. */
  1338. spin_lock(&files->file_lock);
  1339. file = fcheck_files(files, fd);
  1340. if (!file)
  1341. goto out_unlock2;
  1342. if (file->f_mode & 1)
  1343. inode->i_mode |= S_IRUSR | S_IXUSR;
  1344. if (file->f_mode & 2)
  1345. inode->i_mode |= S_IWUSR | S_IXUSR;
  1346. spin_unlock(&files->file_lock);
  1347. put_files_struct(files);
  1348. inode->i_op = &proc_pid_link_inode_operations;
  1349. inode->i_size = 64;
  1350. ei->op.proc_get_link = proc_fd_link;
  1351. dentry->d_op = &tid_fd_dentry_operations;
  1352. d_add(dentry, inode);
  1353. /* Close the race of the process dying before we return the dentry */
  1354. if (tid_fd_revalidate(dentry, NULL))
  1355. result = NULL;
  1356. out:
  1357. put_task_struct(task);
  1358. out_no_task:
  1359. return result;
  1360. out_unlock2:
  1361. spin_unlock(&files->file_lock);
  1362. put_files_struct(files);
  1363. out_unlock:
  1364. iput(inode);
  1365. goto out;
  1366. }
  1367. static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir);
  1368. static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd);
  1369. static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat);
  1370. static struct file_operations proc_fd_operations = {
  1371. .read = generic_read_dir,
  1372. .readdir = proc_readfd,
  1373. };
  1374. static struct file_operations proc_task_operations = {
  1375. .read = generic_read_dir,
  1376. .readdir = proc_task_readdir,
  1377. };
  1378. /*
  1379. * proc directories can do almost nothing..
  1380. */
  1381. static struct inode_operations proc_fd_inode_operations = {
  1382. .lookup = proc_lookupfd,
  1383. .setattr = proc_setattr,
  1384. };
  1385. static struct inode_operations proc_task_inode_operations = {
  1386. .lookup = proc_task_lookup,
  1387. .getattr = proc_task_getattr,
  1388. .setattr = proc_setattr,
  1389. };
  1390. #ifdef CONFIG_SECURITY
  1391. static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
  1392. size_t count, loff_t *ppos)
  1393. {
  1394. struct inode * inode = file->f_dentry->d_inode;
  1395. unsigned long page;
  1396. ssize_t length;
  1397. struct task_struct *task = get_proc_task(inode);
  1398. length = -ESRCH;
  1399. if (!task)
  1400. goto out_no_task;
  1401. if (count > PAGE_SIZE)
  1402. count = PAGE_SIZE;
  1403. length = -ENOMEM;
  1404. if (!(page = __get_free_page(GFP_KERNEL)))
  1405. goto out;
  1406. length = security_getprocattr(task,
  1407. (char*)file->f_dentry->d_name.name,
  1408. (void*)page, count);
  1409. if (length >= 0)
  1410. length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
  1411. free_page(page);
  1412. out:
  1413. put_task_struct(task);
  1414. out_no_task:
  1415. return length;
  1416. }
  1417. static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
  1418. size_t count, loff_t *ppos)
  1419. {
  1420. struct inode * inode = file->f_dentry->d_inode;
  1421. char *page;
  1422. ssize_t length;
  1423. struct task_struct *task = get_proc_task(inode);
  1424. length = -ESRCH;
  1425. if (!task)
  1426. goto out_no_task;
  1427. if (count > PAGE_SIZE)
  1428. count = PAGE_SIZE;
  1429. /* No partial writes. */
  1430. length = -EINVAL;
  1431. if (*ppos != 0)
  1432. goto out;
  1433. length = -ENOMEM;
  1434. page = (char*)__get_free_page(GFP_USER);
  1435. if (!page)
  1436. goto out;
  1437. length = -EFAULT;
  1438. if (copy_from_user(page, buf, count))
  1439. goto out_free;
  1440. length = security_setprocattr(task,
  1441. (char*)file->f_dentry->d_name.name,
  1442. (void*)page, count);
  1443. out_free:
  1444. free_page((unsigned long) page);
  1445. out:
  1446. put_task_struct(task);
  1447. out_no_task:
  1448. return length;
  1449. }
  1450. static struct file_operations proc_pid_attr_operations = {
  1451. .read = proc_pid_attr_read,
  1452. .write = proc_pid_attr_write,
  1453. };
  1454. static struct file_operations proc_tid_attr_operations;
  1455. static struct inode_operations proc_tid_attr_inode_operations;
  1456. static struct file_operations proc_tgid_attr_operations;
  1457. static struct inode_operations proc_tgid_attr_inode_operations;
  1458. #endif
  1459. /* SMP-safe */
  1460. static struct dentry *proc_pident_lookup(struct inode *dir,
  1461. struct dentry *dentry,
  1462. struct pid_entry *ents)
  1463. {
  1464. struct inode *inode;
  1465. struct dentry *error;
  1466. struct task_struct *task = get_proc_task(dir);
  1467. struct pid_entry *p;
  1468. struct proc_inode *ei;
  1469. error = ERR_PTR(-ENOENT);
  1470. inode = NULL;
  1471. if (!task)
  1472. goto out_no_task;
  1473. for (p = ents; p->name; p++) {
  1474. if (p->len != dentry->d_name.len)
  1475. continue;
  1476. if (!memcmp(dentry->d_name.name, p->name, p->len))
  1477. break;
  1478. }
  1479. if (!p->name)
  1480. goto out;
  1481. error = ERR_PTR(-EINVAL);
  1482. inode = proc_pid_make_inode(dir->i_sb, task, p->type);
  1483. if (!inode)
  1484. goto out;
  1485. ei = PROC_I(inode);
  1486. inode->i_mode = p->mode;
  1487. /*
  1488. * Yes, it does not scale. And it should not. Don't add
  1489. * new entries into /proc/<tgid>/ without very good reasons.
  1490. */
  1491. switch(p->type) {
  1492. case PROC_TGID_TASK:
  1493. inode->i_nlink = 2;
  1494. inode->i_op = &proc_task_inode_operations;
  1495. inode->i_fop = &proc_task_operations;
  1496. break;
  1497. case PROC_TID_FD:
  1498. case PROC_TGID_FD:
  1499. inode->i_nlink = 2;
  1500. inode->i_op = &proc_fd_inode_operations;
  1501. inode->i_fop = &proc_fd_operations;
  1502. break;
  1503. case PROC_TID_EXE:
  1504. case PROC_TGID_EXE:
  1505. inode->i_op = &proc_pid_link_inode_operations;
  1506. ei->op.proc_get_link = proc_exe_link;
  1507. break;
  1508. case PROC_TID_CWD:
  1509. case PROC_TGID_CWD:
  1510. inode->i_op = &proc_pid_link_inode_operations;
  1511. ei->op.proc_get_link = proc_cwd_link;
  1512. break;
  1513. case PROC_TID_ROOT:
  1514. case PROC_TGID_ROOT:
  1515. inode->i_op = &proc_pid_link_inode_operations;
  1516. ei->op.proc_get_link = proc_root_link;
  1517. break;
  1518. case PROC_TID_ENVIRON:
  1519. case PROC_TGID_ENVIRON:
  1520. inode->i_fop = &proc_info_file_operations;
  1521. ei->op.proc_read = proc_pid_environ;
  1522. break;
  1523. case PROC_TID_AUXV:
  1524. case PROC_TGID_AUXV:
  1525. inode->i_fop = &proc_info_file_operations;
  1526. ei->op.proc_read = proc_pid_auxv;
  1527. break;
  1528. case PROC_TID_STATUS:
  1529. case PROC_TGID_STATUS:
  1530. inode->i_fop = &proc_info_file_operations;
  1531. ei->op.proc_read = proc_pid_status;
  1532. break;
  1533. case PROC_TID_STAT:
  1534. inode->i_fop = &proc_info_file_operations;
  1535. ei->op.proc_read = proc_tid_stat;
  1536. break;
  1537. case PROC_TGID_STAT:
  1538. inode->i_fop = &proc_info_file_operations;
  1539. ei->op.proc_read = proc_tgid_stat;
  1540. break;
  1541. case PROC_TID_CMDLINE:
  1542. case PROC_TGID_CMDLINE:
  1543. inode->i_fop = &proc_info_file_operations;
  1544. ei->op.proc_read = proc_pid_cmdline;
  1545. break;
  1546. case PROC_TID_STATM:
  1547. case PROC_TGID_STATM:
  1548. inode->i_fop = &proc_info_file_operations;
  1549. ei->op.proc_read = proc_pid_statm;
  1550. break;
  1551. case PROC_TID_MAPS:
  1552. case PROC_TGID_MAPS:
  1553. inode->i_fop = &proc_maps_operations;
  1554. break;
  1555. #ifdef CONFIG_NUMA
  1556. case PROC_TID_NUMA_MAPS:
  1557. case PROC_TGID_NUMA_MAPS:
  1558. inode->i_fop = &proc_numa_maps_operations;
  1559. break;
  1560. #endif
  1561. case PROC_TID_MEM:
  1562. case PROC_TGID_MEM:
  1563. inode->i_fop = &proc_mem_operations;
  1564. break;
  1565. #ifdef CONFIG_SECCOMP
  1566. case PROC_TID_SECCOMP:
  1567. case PROC_TGID_SECCOMP:
  1568. inode->i_fop = &proc_seccomp_operations;
  1569. break;
  1570. #endif /* CONFIG_SECCOMP */
  1571. case PROC_TID_MOUNTS:
  1572. case PROC_TGID_MOUNTS:
  1573. inode->i_fop = &proc_mounts_operations;
  1574. break;
  1575. #ifdef CONFIG_MMU
  1576. case PROC_TID_SMAPS:
  1577. case PROC_TGID_SMAPS:
  1578. inode->i_fop = &proc_smaps_operations;
  1579. break;
  1580. #endif
  1581. case PROC_TID_MOUNTSTATS:
  1582. case PROC_TGID_MOUNTSTATS:
  1583. inode->i_fop = &proc_mountstats_operations;
  1584. break;
  1585. #ifdef CONFIG_SECURITY
  1586. case PROC_TID_ATTR:
  1587. inode->i_nlink = 2;
  1588. inode->i_op = &proc_tid_attr_inode_operations;
  1589. inode->i_fop = &proc_tid_attr_operations;
  1590. break;
  1591. case PROC_TGID_ATTR:
  1592. inode->i_nlink = 2;
  1593. inode->i_op = &proc_tgid_attr_inode_operations;
  1594. inode->i_fop = &proc_tgid_attr_operations;
  1595. break;
  1596. case PROC_TID_ATTR_CURRENT:
  1597. case PROC_TGID_ATTR_CURRENT:
  1598. case PROC_TID_ATTR_PREV:
  1599. case PROC_TGID_ATTR_PREV:
  1600. case PROC_TID_ATTR_EXEC:
  1601. case PROC_TGID_ATTR_EXEC:
  1602. case PROC_TID_ATTR_FSCREATE:
  1603. case PROC_TGID_ATTR_FSCREATE:
  1604. case PROC_TID_ATTR_KEYCREATE:
  1605. case PROC_TGID_ATTR_KEYCREATE:
  1606. case PROC_TID_ATTR_SOCKCREATE:
  1607. case PROC_TGID_ATTR_SOCKCREATE:
  1608. inode->i_fop = &proc_pid_attr_operations;
  1609. break;
  1610. #endif
  1611. #ifdef CONFIG_KALLSYMS
  1612. case PROC_TID_WCHAN:
  1613. case PROC_TGID_WCHAN:
  1614. inode->i_fop = &proc_info_file_operations;
  1615. ei->op.proc_read = proc_pid_wchan;
  1616. break;
  1617. #endif
  1618. #ifdef CONFIG_SCHEDSTATS
  1619. case PROC_TID_SCHEDSTAT:
  1620. case PROC_TGID_SCHEDSTAT:
  1621. inode->i_fop = &proc_info_file_operations;
  1622. ei->op.proc_read = proc_pid_schedstat;
  1623. break;
  1624. #endif
  1625. #ifdef CONFIG_CPUSETS
  1626. case PROC_TID_CPUSET:
  1627. case PROC_TGID_CPUSET:
  1628. inode->i_fop = &proc_cpuset_operations;
  1629. break;
  1630. #endif
  1631. case PROC_TID_OOM_SCORE:
  1632. case PROC_TGID_OOM_SCORE:
  1633. inode->i_fop = &proc_info_file_operations;
  1634. ei->op.proc_read = proc_oom_score;
  1635. break;
  1636. case PROC_TID_OOM_ADJUST:
  1637. case PROC_TGID_OOM_ADJUST:
  1638. inode->i_fop = &proc_oom_adjust_operations;
  1639. break;
  1640. #ifdef CONFIG_AUDITSYSCALL
  1641. case PROC_TID_LOGINUID:
  1642. case PROC_TGID_LOGINUID:
  1643. inode->i_fop = &proc_loginuid_operations;
  1644. break;
  1645. #endif
  1646. default:
  1647. printk("procfs: impossible type (%d)",p->type);
  1648. iput(inode);
  1649. error = ERR_PTR(-EINVAL);
  1650. goto out;
  1651. }
  1652. dentry->d_op = &pid_dentry_operations;
  1653. d_add(dentry, inode);
  1654. /* Close the race of the process dying before we return the dentry */
  1655. if (pid_revalidate(dentry, NULL))
  1656. error = NULL;
  1657. out:
  1658. put_task_struct(task);
  1659. out_no_task:
  1660. return error;
  1661. }
  1662. static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
  1663. return proc_pident_lookup(dir, dentry, tgid_base_stuff);
  1664. }
  1665. static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
  1666. return proc_pident_lookup(dir, dentry, tid_base_stuff);
  1667. }
  1668. static struct file_operations proc_tgid_base_operations = {
  1669. .read = generic_read_dir,
  1670. .readdir = proc_tgid_base_readdir,
  1671. };
  1672. static struct file_operations proc_tid_base_operations = {
  1673. .read = generic_read_dir,
  1674. .readdir = proc_tid_base_readdir,
  1675. };
  1676. static struct inode_operations proc_tgid_base_inode_operations = {
  1677. .lookup = proc_tgid_base_lookup,
  1678. .getattr = pid_getattr,
  1679. .setattr = proc_setattr,
  1680. };
  1681. static struct inode_operations proc_tid_base_inode_operations = {
  1682. .lookup = proc_tid_base_lookup,
  1683. .getattr = pid_getattr,
  1684. .setattr = proc_setattr,
  1685. };
  1686. #ifdef CONFIG_SECURITY
  1687. static int proc_tgid_attr_readdir(struct file * filp,
  1688. void * dirent, filldir_t filldir)
  1689. {
  1690. return proc_pident_readdir(filp,dirent,filldir,
  1691. tgid_attr_stuff,ARRAY_SIZE(tgid_attr_stuff));
  1692. }
  1693. static int proc_tid_attr_readdir(struct file * filp,
  1694. void * dirent, filldir_t filldir)
  1695. {
  1696. return proc_pident_readdir(filp,dirent,filldir,
  1697. tid_attr_stuff,ARRAY_SIZE(tid_attr_stuff));
  1698. }
  1699. static struct file_operations proc_tgid_attr_operations = {
  1700. .read = generic_read_dir,
  1701. .readdir = proc_tgid_attr_readdir,
  1702. };
  1703. static struct file_operations proc_tid_attr_operations = {
  1704. .read = generic_read_dir,
  1705. .readdir = proc_tid_attr_readdir,
  1706. };
  1707. static struct dentry *proc_tgid_attr_lookup(struct inode *dir,
  1708. struct dentry *dentry, struct nameidata *nd)
  1709. {
  1710. return proc_pident_lookup(dir, dentry, tgid_attr_stuff);
  1711. }
  1712. static struct dentry *proc_tid_attr_lookup(struct inode *dir,
  1713. struct dentry *dentry, struct nameidata *nd)
  1714. {
  1715. return proc_pident_lookup(dir, dentry, tid_attr_stuff);
  1716. }
  1717. static struct inode_operations proc_tgid_attr_inode_operations = {
  1718. .lookup = proc_tgid_attr_lookup,
  1719. .getattr = pid_getattr,
  1720. .setattr = proc_setattr,
  1721. };
  1722. static struct inode_operations proc_tid_attr_inode_operations = {
  1723. .lookup = proc_tid_attr_lookup,
  1724. .getattr = pid_getattr,
  1725. .setattr = proc_setattr,
  1726. };
  1727. #endif
  1728. /*
  1729. * /proc/self:
  1730. */
  1731. static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
  1732. int buflen)
  1733. {
  1734. char tmp[PROC_NUMBUF];
  1735. sprintf(tmp, "%d", current->tgid);
  1736. return vfs_readlink(dentry,buffer,buflen,tmp);
  1737. }
  1738. static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
  1739. {
  1740. char tmp[PROC_NUMBUF];
  1741. sprintf(tmp, "%d", current->tgid);
  1742. return ERR_PTR(vfs_follow_link(nd,tmp));
  1743. }
  1744. static struct inode_operations proc_self_inode_operations = {
  1745. .readlink = proc_self_readlink,
  1746. .follow_link = proc_self_follow_link,
  1747. };
  1748. /**
  1749. * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
  1750. *
  1751. * @task: task that should be flushed.
  1752. *
  1753. * Looks in the dcache for
  1754. * /proc/@pid
  1755. * /proc/@tgid/task/@pid
  1756. * if either directory is present flushes it and all of it'ts children
  1757. * from the dcache.
  1758. *
  1759. * It is safe and reasonable to cache /proc entries for a task until
  1760. * that task exits. After that they just clog up the dcache with
  1761. * useless entries, possibly causing useful dcache entries to be
  1762. * flushed instead. This routine is proved to flush those useless
  1763. * dcache entries at process exit time.
  1764. *
  1765. * NOTE: This routine is just an optimization so it does not guarantee
  1766. * that no dcache entries will exist at process exit time it
  1767. * just makes it very unlikely that any will persist.
  1768. */
  1769. void proc_flush_task(struct task_struct *task)
  1770. {
  1771. struct dentry *dentry, *leader, *dir;
  1772. char buf[PROC_NUMBUF];
  1773. struct qstr name;
  1774. name.name = buf;
  1775. name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
  1776. dentry = d_hash_and_lookup(proc_mnt->mnt_root, &name);
  1777. if (dentry) {
  1778. shrink_dcache_parent(dentry);
  1779. d_drop(dentry);
  1780. dput(dentry);
  1781. }
  1782. if (thread_group_leader(task))
  1783. goto out;
  1784. name.name = buf;
  1785. name.len = snprintf(buf, sizeof(buf), "%d", task->tgid);
  1786. leader = d_hash_and_lookup(proc_mnt->mnt_root, &name);
  1787. if (!leader)
  1788. goto out;
  1789. name.name = "task";
  1790. name.len = strlen(name.name);
  1791. dir = d_hash_and_lookup(leader, &name);
  1792. if (!dir)
  1793. goto out_put_leader;
  1794. name.name = buf;
  1795. name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
  1796. dentry = d_hash_and_lookup(dir, &name);
  1797. if (dentry) {
  1798. shrink_dcache_parent(dentry);
  1799. d_drop(dentry);
  1800. dput(dentry);
  1801. }
  1802. dput(dir);
  1803. out_put_leader:
  1804. dput(leader);
  1805. out:
  1806. return;
  1807. }
  1808. /* SMP-safe */
  1809. struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
  1810. {
  1811. struct dentry *result = ERR_PTR(-ENOENT);
  1812. struct task_struct *task;
  1813. struct inode *inode;
  1814. struct proc_inode *ei;
  1815. unsigned tgid;
  1816. if (dentry->d_name.len == 4 && !memcmp(dentry->d_name.name,"self",4)) {
  1817. inode = new_inode(dir->i_sb);
  1818. if (!inode)
  1819. return ERR_PTR(-ENOMEM);
  1820. ei = PROC_I(inode);
  1821. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  1822. inode->i_ino = fake_ino(0, PROC_TGID_INO);
  1823. ei->pde = NULL;
  1824. inode->i_mode = S_IFLNK|S_IRWXUGO;
  1825. inode->i_uid = inode->i_gid = 0;
  1826. inode->i_size = 64;
  1827. inode->i_op = &proc_self_inode_operations;
  1828. d_add(dentry, inode);
  1829. return NULL;
  1830. }
  1831. tgid = name_to_int(dentry);
  1832. if (tgid == ~0U)
  1833. goto out;
  1834. rcu_read_lock();
  1835. task = find_task_by_pid(tgid);
  1836. if (task)
  1837. get_task_struct(task);
  1838. rcu_read_unlock();
  1839. if (!task)
  1840. goto out;
  1841. inode = proc_pid_make_inode(dir->i_sb, task, PROC_TGID_INO);
  1842. if (!inode)
  1843. goto out_put_task;
  1844. inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
  1845. inode->i_op = &proc_tgid_base_inode_operations;
  1846. inode->i_fop = &proc_tgid_base_operations;
  1847. inode->i_flags|=S_IMMUTABLE;
  1848. #ifdef CONFIG_SECURITY
  1849. inode->i_nlink = 5;
  1850. #else
  1851. inode->i_nlink = 4;
  1852. #endif
  1853. dentry->d_op = &pid_dentry_operations;
  1854. d_add(dentry, inode);
  1855. /* Close the race of the process dying before we return the dentry */
  1856. if (pid_revalidate(dentry, NULL))
  1857. result = NULL;
  1858. out_put_task:
  1859. put_task_struct(task);
  1860. out:
  1861. return result;
  1862. }
  1863. /* SMP-safe */
  1864. static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
  1865. {
  1866. struct dentry *result = ERR_PTR(-ENOENT);
  1867. struct task_struct *task;
  1868. struct task_struct *leader = get_proc_task(dir);
  1869. struct inode *inode;
  1870. unsigned tid;
  1871. if (!leader)
  1872. goto out_no_task;
  1873. tid = name_to_int(dentry);
  1874. if (tid == ~0U)
  1875. goto out;
  1876. rcu_read_lock();
  1877. task = find_task_by_pid(tid);
  1878. if (task)
  1879. get_task_struct(task);
  1880. rcu_read_unlock();
  1881. if (!task)
  1882. goto out;
  1883. if (leader->tgid != task->tgid)
  1884. goto out_drop_task;
  1885. inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_INO);
  1886. if (!inode)
  1887. goto out_drop_task;
  1888. inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
  1889. inode->i_op = &proc_tid_base_inode_operations;
  1890. inode->i_fop = &proc_tid_base_operations;
  1891. inode->i_flags|=S_IMMUTABLE;
  1892. #ifdef CONFIG_SECURITY
  1893. inode->i_nlink = 4;
  1894. #else
  1895. inode->i_nlink = 3;
  1896. #endif
  1897. dentry->d_op = &pid_dentry_operations;
  1898. d_add(dentry, inode);
  1899. /* Close the race of the process dying before we return the dentry */
  1900. if (pid_revalidate(dentry, NULL))
  1901. result = NULL;
  1902. out_drop_task:
  1903. put_task_struct(task);
  1904. out:
  1905. put_task_struct(leader);
  1906. out_no_task:
  1907. return result;
  1908. }
  1909. /*
  1910. * Find the first tgid to return to user space.
  1911. *
  1912. * Usually this is just whatever follows &init_task, but if the users
  1913. * buffer was too small to hold the full list or there was a seek into
  1914. * the middle of the directory we have more work to do.
  1915. *
  1916. * In the case of a short read we start with find_task_by_pid.
  1917. *
  1918. * In the case of a seek we start with &init_task and walk nr
  1919. * threads past it.
  1920. */
  1921. static struct task_struct *first_tgid(int tgid, unsigned int nr)
  1922. {
  1923. struct task_struct *pos;
  1924. rcu_read_lock();
  1925. if (tgid && nr) {
  1926. pos = find_task_by_pid(tgid);
  1927. if (pos && thread_group_leader(pos))
  1928. goto found;
  1929. }
  1930. /* If nr exceeds the number of processes get out quickly */
  1931. pos = NULL;
  1932. if (nr && nr >= nr_processes())
  1933. goto done;
  1934. /* If we haven't found our starting place yet start with
  1935. * the init_task and walk nr tasks forward.
  1936. */
  1937. for (pos = next_task(&init_task); nr > 0; --nr) {
  1938. pos = next_task(pos);
  1939. if (pos == &init_task) {
  1940. pos = NULL;
  1941. goto done;
  1942. }
  1943. }
  1944. found:
  1945. get_task_struct(pos);
  1946. done:
  1947. rcu_read_unlock();
  1948. return pos;
  1949. }
  1950. /*
  1951. * Find the next task in the task list.
  1952. * Return NULL if we loop or there is any error.
  1953. *
  1954. * The reference to the input task_struct is released.
  1955. */
  1956. static struct task_struct *next_tgid(struct task_struct *start)
  1957. {
  1958. struct task_struct *pos;
  1959. rcu_read_lock();
  1960. pos = start;
  1961. if (pid_alive(start))
  1962. pos = next_task(start);
  1963. if (pid_alive(pos) && (pos != &init_task)) {
  1964. get_task_struct(pos);
  1965. goto done;
  1966. }
  1967. pos = NULL;
  1968. done:
  1969. rcu_read_unlock();
  1970. put_task_struct(start);
  1971. return pos;
  1972. }
  1973. /* for the /proc/ directory itself, after non-process stuff has been done */
  1974. int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
  1975. {
  1976. char buf[PROC_NUMBUF];
  1977. unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
  1978. struct task_struct *task;
  1979. int tgid;
  1980. if (!nr) {
  1981. ino_t ino = fake_ino(0,PROC_TGID_INO);
  1982. if (filldir(dirent, "self", 4, filp->f_pos, ino, DT_LNK) < 0)
  1983. return 0;
  1984. filp->f_pos++;
  1985. nr++;
  1986. }
  1987. nr -= 1;
  1988. /* f_version caches the tgid value that the last readdir call couldn't
  1989. * return. lseek aka telldir automagically resets f_version to 0.
  1990. */
  1991. tgid = filp->f_version;
  1992. filp->f_version = 0;
  1993. for (task = first_tgid(tgid, nr);
  1994. task;
  1995. task = next_tgid(task), filp->f_pos++) {
  1996. int len;
  1997. ino_t ino;
  1998. tgid = task->pid;
  1999. len = snprintf(buf, sizeof(buf), "%d", tgid);
  2000. ino = fake_ino(tgid, PROC_TGID_INO);
  2001. if (filldir(dirent, buf, len, filp->f_pos, ino, DT_DIR) < 0) {
  2002. /* returning this tgid failed, save it as the first
  2003. * pid for the next readir call */
  2004. filp->f_version = tgid;
  2005. put_task_struct(task);
  2006. break;
  2007. }
  2008. }
  2009. return 0;
  2010. }
  2011. /*
  2012. * Find the first tid of a thread group to return to user space.
  2013. *
  2014. * Usually this is just the thread group leader, but if the users
  2015. * buffer was too small or there was a seek into the middle of the
  2016. * directory we have more work todo.
  2017. *
  2018. * In the case of a short read we start with find_task_by_pid.
  2019. *
  2020. * In the case of a seek we start with the leader and walk nr
  2021. * threads past it.
  2022. */
  2023. static struct task_struct *first_tid(struct task_struct *leader,
  2024. int tid, int nr)
  2025. {
  2026. struct task_struct *pos;
  2027. rcu_read_lock();
  2028. /* Attempt to start with the pid of a thread */
  2029. if (tid && (nr > 0)) {
  2030. pos = find_task_by_pid(tid);
  2031. if (pos && (pos->group_leader == leader))
  2032. goto found;
  2033. }
  2034. /* If nr exceeds the number of threads there is nothing todo */
  2035. pos = NULL;
  2036. if (nr && nr >= get_nr_threads(leader))
  2037. goto out;
  2038. /* If we haven't found our starting place yet start
  2039. * with the leader and walk nr threads forward.
  2040. */
  2041. for (pos = leader; nr > 0; --nr) {
  2042. pos = next_thread(pos);
  2043. if (pos == leader) {
  2044. pos = NULL;
  2045. goto out;
  2046. }
  2047. }
  2048. found:
  2049. get_task_struct(pos);
  2050. out:
  2051. rcu_read_unlock();
  2052. return pos;
  2053. }
  2054. /*
  2055. * Find the next thread in the thread list.
  2056. * Return NULL if there is an error or no next thread.
  2057. *
  2058. * The reference to the input task_struct is released.
  2059. */
  2060. static struct task_struct *next_tid(struct task_struct *start)
  2061. {
  2062. struct task_struct *pos = NULL;
  2063. rcu_read_lock();
  2064. if (pid_alive(start)) {
  2065. pos = next_thread(start);
  2066. if (thread_group_leader(pos))
  2067. pos = NULL;
  2068. else
  2069. get_task_struct(pos);
  2070. }
  2071. rcu_read_unlock();
  2072. put_task_struct(start);
  2073. return pos;
  2074. }
  2075. /* for the /proc/TGID/task/ directories */
  2076. static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
  2077. {
  2078. char buf[PROC_NUMBUF];
  2079. struct dentry *dentry = filp->f_dentry;
  2080. struct inode *inode = dentry->d_inode;
  2081. struct task_struct *leader = get_proc_task(inode);
  2082. struct task_struct *task;
  2083. int retval = -ENOENT;
  2084. ino_t ino;
  2085. int tid;
  2086. unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
  2087. if (!leader)
  2088. goto out_no_task;
  2089. retval = 0;
  2090. switch (pos) {
  2091. case 0:
  2092. ino = inode->i_ino;
  2093. if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
  2094. goto out;
  2095. pos++;
  2096. /* fall through */
  2097. case 1:
  2098. ino = parent_ino(dentry);
  2099. if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
  2100. goto out;
  2101. pos++;
  2102. /* fall through */
  2103. }
  2104. /* f_version caches the tgid value that the last readdir call couldn't
  2105. * return. lseek aka telldir automagically resets f_version to 0.
  2106. */
  2107. tid = filp->f_version;
  2108. filp->f_version = 0;
  2109. for (task = first_tid(leader, tid, pos - 2);
  2110. task;
  2111. task = next_tid(task), pos++) {
  2112. int len;
  2113. tid = task->pid;
  2114. len = snprintf(buf, sizeof(buf), "%d", tid);
  2115. ino = fake_ino(tid, PROC_TID_INO);
  2116. if (filldir(dirent, buf, len, pos, ino, DT_DIR < 0)) {
  2117. /* returning this tgid failed, save it as the first
  2118. * pid for the next readir call */
  2119. filp->f_version = tid;
  2120. put_task_struct(task);
  2121. break;
  2122. }
  2123. }
  2124. out:
  2125. filp->f_pos = pos;
  2126. put_task_struct(leader);
  2127. out_no_task:
  2128. return retval;
  2129. }
  2130. static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
  2131. {
  2132. struct inode *inode = dentry->d_inode;
  2133. struct task_struct *p = get_proc_task(inode);
  2134. generic_fillattr(inode, stat);
  2135. if (p) {
  2136. rcu_read_lock();
  2137. stat->nlink += get_nr_threads(p);
  2138. rcu_read_unlock();
  2139. put_task_struct(p);
  2140. }
  2141. return 0;
  2142. }