base.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364
  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. inode->i_mode &= ~(S_ISUID | S_ISGID);
  1193. security_task_to_inode(task, inode);
  1194. put_task_struct(task);
  1195. return 1;
  1196. }
  1197. d_drop(dentry);
  1198. return 0;
  1199. }
  1200. static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
  1201. {
  1202. struct inode *inode = dentry->d_inode;
  1203. struct task_struct *task;
  1204. generic_fillattr(inode, stat);
  1205. rcu_read_lock();
  1206. stat->uid = 0;
  1207. stat->gid = 0;
  1208. task = pid_task(proc_pid(inode), PIDTYPE_PID);
  1209. if (task) {
  1210. if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
  1211. task_dumpable(task)) {
  1212. stat->uid = task->euid;
  1213. stat->gid = task->egid;
  1214. }
  1215. }
  1216. rcu_read_unlock();
  1217. return 0;
  1218. }
  1219. static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
  1220. {
  1221. struct inode *inode = dentry->d_inode;
  1222. struct task_struct *task = get_proc_task(inode);
  1223. int fd = proc_fd(inode);
  1224. struct files_struct *files;
  1225. if (task) {
  1226. files = get_files_struct(task);
  1227. if (files) {
  1228. rcu_read_lock();
  1229. if (fcheck_files(files, fd)) {
  1230. rcu_read_unlock();
  1231. put_files_struct(files);
  1232. if (task_dumpable(task)) {
  1233. inode->i_uid = task->euid;
  1234. inode->i_gid = task->egid;
  1235. } else {
  1236. inode->i_uid = 0;
  1237. inode->i_gid = 0;
  1238. }
  1239. inode->i_mode &= ~(S_ISUID | S_ISGID);
  1240. security_task_to_inode(task, inode);
  1241. put_task_struct(task);
  1242. return 1;
  1243. }
  1244. rcu_read_unlock();
  1245. put_files_struct(files);
  1246. }
  1247. put_task_struct(task);
  1248. }
  1249. d_drop(dentry);
  1250. return 0;
  1251. }
  1252. static int pid_delete_dentry(struct dentry * dentry)
  1253. {
  1254. /* Is the task we represent dead?
  1255. * If so, then don't put the dentry on the lru list,
  1256. * kill it immediately.
  1257. */
  1258. return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
  1259. }
  1260. static struct dentry_operations tid_fd_dentry_operations =
  1261. {
  1262. .d_revalidate = tid_fd_revalidate,
  1263. .d_delete = pid_delete_dentry,
  1264. };
  1265. static struct dentry_operations pid_dentry_operations =
  1266. {
  1267. .d_revalidate = pid_revalidate,
  1268. .d_delete = pid_delete_dentry,
  1269. };
  1270. /* Lookups */
  1271. static unsigned name_to_int(struct dentry *dentry)
  1272. {
  1273. const char *name = dentry->d_name.name;
  1274. int len = dentry->d_name.len;
  1275. unsigned n = 0;
  1276. if (len > 1 && *name == '0')
  1277. goto out;
  1278. while (len-- > 0) {
  1279. unsigned c = *name++ - '0';
  1280. if (c > 9)
  1281. goto out;
  1282. if (n >= (~0U-9)/10)
  1283. goto out;
  1284. n *= 10;
  1285. n += c;
  1286. }
  1287. return n;
  1288. out:
  1289. return ~0U;
  1290. }
  1291. /* SMP-safe */
  1292. static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
  1293. {
  1294. struct task_struct *task = get_proc_task(dir);
  1295. unsigned fd = name_to_int(dentry);
  1296. struct dentry *result = ERR_PTR(-ENOENT);
  1297. struct file * file;
  1298. struct files_struct * files;
  1299. struct inode *inode;
  1300. struct proc_inode *ei;
  1301. if (!task)
  1302. goto out_no_task;
  1303. if (fd == ~0U)
  1304. goto out;
  1305. inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_FD_DIR+fd);
  1306. if (!inode)
  1307. goto out;
  1308. ei = PROC_I(inode);
  1309. ei->fd = fd;
  1310. files = get_files_struct(task);
  1311. if (!files)
  1312. goto out_unlock;
  1313. inode->i_mode = S_IFLNK;
  1314. /*
  1315. * We are not taking a ref to the file structure, so we must
  1316. * hold ->file_lock.
  1317. */
  1318. spin_lock(&files->file_lock);
  1319. file = fcheck_files(files, fd);
  1320. if (!file)
  1321. goto out_unlock2;
  1322. if (file->f_mode & 1)
  1323. inode->i_mode |= S_IRUSR | S_IXUSR;
  1324. if (file->f_mode & 2)
  1325. inode->i_mode |= S_IWUSR | S_IXUSR;
  1326. spin_unlock(&files->file_lock);
  1327. put_files_struct(files);
  1328. inode->i_op = &proc_pid_link_inode_operations;
  1329. inode->i_size = 64;
  1330. ei->op.proc_get_link = proc_fd_link;
  1331. dentry->d_op = &tid_fd_dentry_operations;
  1332. d_add(dentry, inode);
  1333. /* Close the race of the process dying before we return the dentry */
  1334. if (tid_fd_revalidate(dentry, NULL))
  1335. result = NULL;
  1336. out:
  1337. put_task_struct(task);
  1338. out_no_task:
  1339. return result;
  1340. out_unlock2:
  1341. spin_unlock(&files->file_lock);
  1342. put_files_struct(files);
  1343. out_unlock:
  1344. iput(inode);
  1345. goto out;
  1346. }
  1347. static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir);
  1348. static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd);
  1349. static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat);
  1350. static struct file_operations proc_fd_operations = {
  1351. .read = generic_read_dir,
  1352. .readdir = proc_readfd,
  1353. };
  1354. static struct file_operations proc_task_operations = {
  1355. .read = generic_read_dir,
  1356. .readdir = proc_task_readdir,
  1357. };
  1358. /*
  1359. * proc directories can do almost nothing..
  1360. */
  1361. static struct inode_operations proc_fd_inode_operations = {
  1362. .lookup = proc_lookupfd,
  1363. };
  1364. static struct inode_operations proc_task_inode_operations = {
  1365. .lookup = proc_task_lookup,
  1366. .getattr = proc_task_getattr,
  1367. };
  1368. #ifdef CONFIG_SECURITY
  1369. static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
  1370. size_t count, loff_t *ppos)
  1371. {
  1372. struct inode * inode = file->f_dentry->d_inode;
  1373. unsigned long page;
  1374. ssize_t length;
  1375. struct task_struct *task = get_proc_task(inode);
  1376. length = -ESRCH;
  1377. if (!task)
  1378. goto out_no_task;
  1379. if (count > PAGE_SIZE)
  1380. count = PAGE_SIZE;
  1381. length = -ENOMEM;
  1382. if (!(page = __get_free_page(GFP_KERNEL)))
  1383. goto out;
  1384. length = security_getprocattr(task,
  1385. (char*)file->f_dentry->d_name.name,
  1386. (void*)page, count);
  1387. if (length >= 0)
  1388. length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
  1389. free_page(page);
  1390. out:
  1391. put_task_struct(task);
  1392. out_no_task:
  1393. return length;
  1394. }
  1395. static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
  1396. size_t count, loff_t *ppos)
  1397. {
  1398. struct inode * inode = file->f_dentry->d_inode;
  1399. char *page;
  1400. ssize_t length;
  1401. struct task_struct *task = get_proc_task(inode);
  1402. length = -ESRCH;
  1403. if (!task)
  1404. goto out_no_task;
  1405. if (count > PAGE_SIZE)
  1406. count = PAGE_SIZE;
  1407. /* No partial writes. */
  1408. length = -EINVAL;
  1409. if (*ppos != 0)
  1410. goto out;
  1411. length = -ENOMEM;
  1412. page = (char*)__get_free_page(GFP_USER);
  1413. if (!page)
  1414. goto out;
  1415. length = -EFAULT;
  1416. if (copy_from_user(page, buf, count))
  1417. goto out_free;
  1418. length = security_setprocattr(task,
  1419. (char*)file->f_dentry->d_name.name,
  1420. (void*)page, count);
  1421. out_free:
  1422. free_page((unsigned long) page);
  1423. out:
  1424. put_task_struct(task);
  1425. out_no_task:
  1426. return length;
  1427. }
  1428. static struct file_operations proc_pid_attr_operations = {
  1429. .read = proc_pid_attr_read,
  1430. .write = proc_pid_attr_write,
  1431. };
  1432. static struct file_operations proc_tid_attr_operations;
  1433. static struct inode_operations proc_tid_attr_inode_operations;
  1434. static struct file_operations proc_tgid_attr_operations;
  1435. static struct inode_operations proc_tgid_attr_inode_operations;
  1436. #endif
  1437. /* SMP-safe */
  1438. static struct dentry *proc_pident_lookup(struct inode *dir,
  1439. struct dentry *dentry,
  1440. struct pid_entry *ents)
  1441. {
  1442. struct inode *inode;
  1443. struct dentry *error;
  1444. struct task_struct *task = get_proc_task(dir);
  1445. struct pid_entry *p;
  1446. struct proc_inode *ei;
  1447. error = ERR_PTR(-ENOENT);
  1448. inode = NULL;
  1449. if (!task)
  1450. goto out_no_task;
  1451. for (p = ents; p->name; p++) {
  1452. if (p->len != dentry->d_name.len)
  1453. continue;
  1454. if (!memcmp(dentry->d_name.name, p->name, p->len))
  1455. break;
  1456. }
  1457. if (!p->name)
  1458. goto out;
  1459. error = ERR_PTR(-EINVAL);
  1460. inode = proc_pid_make_inode(dir->i_sb, task, p->type);
  1461. if (!inode)
  1462. goto out;
  1463. ei = PROC_I(inode);
  1464. inode->i_mode = p->mode;
  1465. /*
  1466. * Yes, it does not scale. And it should not. Don't add
  1467. * new entries into /proc/<tgid>/ without very good reasons.
  1468. */
  1469. switch(p->type) {
  1470. case PROC_TGID_TASK:
  1471. inode->i_nlink = 2;
  1472. inode->i_op = &proc_task_inode_operations;
  1473. inode->i_fop = &proc_task_operations;
  1474. break;
  1475. case PROC_TID_FD:
  1476. case PROC_TGID_FD:
  1477. inode->i_nlink = 2;
  1478. inode->i_op = &proc_fd_inode_operations;
  1479. inode->i_fop = &proc_fd_operations;
  1480. break;
  1481. case PROC_TID_EXE:
  1482. case PROC_TGID_EXE:
  1483. inode->i_op = &proc_pid_link_inode_operations;
  1484. ei->op.proc_get_link = proc_exe_link;
  1485. break;
  1486. case PROC_TID_CWD:
  1487. case PROC_TGID_CWD:
  1488. inode->i_op = &proc_pid_link_inode_operations;
  1489. ei->op.proc_get_link = proc_cwd_link;
  1490. break;
  1491. case PROC_TID_ROOT:
  1492. case PROC_TGID_ROOT:
  1493. inode->i_op = &proc_pid_link_inode_operations;
  1494. ei->op.proc_get_link = proc_root_link;
  1495. break;
  1496. case PROC_TID_ENVIRON:
  1497. case PROC_TGID_ENVIRON:
  1498. inode->i_fop = &proc_info_file_operations;
  1499. ei->op.proc_read = proc_pid_environ;
  1500. break;
  1501. case PROC_TID_AUXV:
  1502. case PROC_TGID_AUXV:
  1503. inode->i_fop = &proc_info_file_operations;
  1504. ei->op.proc_read = proc_pid_auxv;
  1505. break;
  1506. case PROC_TID_STATUS:
  1507. case PROC_TGID_STATUS:
  1508. inode->i_fop = &proc_info_file_operations;
  1509. ei->op.proc_read = proc_pid_status;
  1510. break;
  1511. case PROC_TID_STAT:
  1512. inode->i_fop = &proc_info_file_operations;
  1513. ei->op.proc_read = proc_tid_stat;
  1514. break;
  1515. case PROC_TGID_STAT:
  1516. inode->i_fop = &proc_info_file_operations;
  1517. ei->op.proc_read = proc_tgid_stat;
  1518. break;
  1519. case PROC_TID_CMDLINE:
  1520. case PROC_TGID_CMDLINE:
  1521. inode->i_fop = &proc_info_file_operations;
  1522. ei->op.proc_read = proc_pid_cmdline;
  1523. break;
  1524. case PROC_TID_STATM:
  1525. case PROC_TGID_STATM:
  1526. inode->i_fop = &proc_info_file_operations;
  1527. ei->op.proc_read = proc_pid_statm;
  1528. break;
  1529. case PROC_TID_MAPS:
  1530. case PROC_TGID_MAPS:
  1531. inode->i_fop = &proc_maps_operations;
  1532. break;
  1533. #ifdef CONFIG_NUMA
  1534. case PROC_TID_NUMA_MAPS:
  1535. case PROC_TGID_NUMA_MAPS:
  1536. inode->i_fop = &proc_numa_maps_operations;
  1537. break;
  1538. #endif
  1539. case PROC_TID_MEM:
  1540. case PROC_TGID_MEM:
  1541. inode->i_fop = &proc_mem_operations;
  1542. break;
  1543. #ifdef CONFIG_SECCOMP
  1544. case PROC_TID_SECCOMP:
  1545. case PROC_TGID_SECCOMP:
  1546. inode->i_fop = &proc_seccomp_operations;
  1547. break;
  1548. #endif /* CONFIG_SECCOMP */
  1549. case PROC_TID_MOUNTS:
  1550. case PROC_TGID_MOUNTS:
  1551. inode->i_fop = &proc_mounts_operations;
  1552. break;
  1553. #ifdef CONFIG_MMU
  1554. case PROC_TID_SMAPS:
  1555. case PROC_TGID_SMAPS:
  1556. inode->i_fop = &proc_smaps_operations;
  1557. break;
  1558. #endif
  1559. case PROC_TID_MOUNTSTATS:
  1560. case PROC_TGID_MOUNTSTATS:
  1561. inode->i_fop = &proc_mountstats_operations;
  1562. break;
  1563. #ifdef CONFIG_SECURITY
  1564. case PROC_TID_ATTR:
  1565. inode->i_nlink = 2;
  1566. inode->i_op = &proc_tid_attr_inode_operations;
  1567. inode->i_fop = &proc_tid_attr_operations;
  1568. break;
  1569. case PROC_TGID_ATTR:
  1570. inode->i_nlink = 2;
  1571. inode->i_op = &proc_tgid_attr_inode_operations;
  1572. inode->i_fop = &proc_tgid_attr_operations;
  1573. break;
  1574. case PROC_TID_ATTR_CURRENT:
  1575. case PROC_TGID_ATTR_CURRENT:
  1576. case PROC_TID_ATTR_PREV:
  1577. case PROC_TGID_ATTR_PREV:
  1578. case PROC_TID_ATTR_EXEC:
  1579. case PROC_TGID_ATTR_EXEC:
  1580. case PROC_TID_ATTR_FSCREATE:
  1581. case PROC_TGID_ATTR_FSCREATE:
  1582. case PROC_TID_ATTR_KEYCREATE:
  1583. case PROC_TGID_ATTR_KEYCREATE:
  1584. case PROC_TID_ATTR_SOCKCREATE:
  1585. case PROC_TGID_ATTR_SOCKCREATE:
  1586. inode->i_fop = &proc_pid_attr_operations;
  1587. break;
  1588. #endif
  1589. #ifdef CONFIG_KALLSYMS
  1590. case PROC_TID_WCHAN:
  1591. case PROC_TGID_WCHAN:
  1592. inode->i_fop = &proc_info_file_operations;
  1593. ei->op.proc_read = proc_pid_wchan;
  1594. break;
  1595. #endif
  1596. #ifdef CONFIG_SCHEDSTATS
  1597. case PROC_TID_SCHEDSTAT:
  1598. case PROC_TGID_SCHEDSTAT:
  1599. inode->i_fop = &proc_info_file_operations;
  1600. ei->op.proc_read = proc_pid_schedstat;
  1601. break;
  1602. #endif
  1603. #ifdef CONFIG_CPUSETS
  1604. case PROC_TID_CPUSET:
  1605. case PROC_TGID_CPUSET:
  1606. inode->i_fop = &proc_cpuset_operations;
  1607. break;
  1608. #endif
  1609. case PROC_TID_OOM_SCORE:
  1610. case PROC_TGID_OOM_SCORE:
  1611. inode->i_fop = &proc_info_file_operations;
  1612. ei->op.proc_read = proc_oom_score;
  1613. break;
  1614. case PROC_TID_OOM_ADJUST:
  1615. case PROC_TGID_OOM_ADJUST:
  1616. inode->i_fop = &proc_oom_adjust_operations;
  1617. break;
  1618. #ifdef CONFIG_AUDITSYSCALL
  1619. case PROC_TID_LOGINUID:
  1620. case PROC_TGID_LOGINUID:
  1621. inode->i_fop = &proc_loginuid_operations;
  1622. break;
  1623. #endif
  1624. default:
  1625. printk("procfs: impossible type (%d)",p->type);
  1626. iput(inode);
  1627. error = ERR_PTR(-EINVAL);
  1628. goto out;
  1629. }
  1630. dentry->d_op = &pid_dentry_operations;
  1631. d_add(dentry, inode);
  1632. /* Close the race of the process dying before we return the dentry */
  1633. if (pid_revalidate(dentry, NULL))
  1634. error = NULL;
  1635. out:
  1636. put_task_struct(task);
  1637. out_no_task:
  1638. return error;
  1639. }
  1640. static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
  1641. return proc_pident_lookup(dir, dentry, tgid_base_stuff);
  1642. }
  1643. static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
  1644. return proc_pident_lookup(dir, dentry, tid_base_stuff);
  1645. }
  1646. static struct file_operations proc_tgid_base_operations = {
  1647. .read = generic_read_dir,
  1648. .readdir = proc_tgid_base_readdir,
  1649. };
  1650. static struct file_operations proc_tid_base_operations = {
  1651. .read = generic_read_dir,
  1652. .readdir = proc_tid_base_readdir,
  1653. };
  1654. static struct inode_operations proc_tgid_base_inode_operations = {
  1655. .lookup = proc_tgid_base_lookup,
  1656. .getattr = pid_getattr,
  1657. };
  1658. static struct inode_operations proc_tid_base_inode_operations = {
  1659. .lookup = proc_tid_base_lookup,
  1660. .getattr = pid_getattr,
  1661. };
  1662. #ifdef CONFIG_SECURITY
  1663. static int proc_tgid_attr_readdir(struct file * filp,
  1664. void * dirent, filldir_t filldir)
  1665. {
  1666. return proc_pident_readdir(filp,dirent,filldir,
  1667. tgid_attr_stuff,ARRAY_SIZE(tgid_attr_stuff));
  1668. }
  1669. static int proc_tid_attr_readdir(struct file * filp,
  1670. void * dirent, filldir_t filldir)
  1671. {
  1672. return proc_pident_readdir(filp,dirent,filldir,
  1673. tid_attr_stuff,ARRAY_SIZE(tid_attr_stuff));
  1674. }
  1675. static struct file_operations proc_tgid_attr_operations = {
  1676. .read = generic_read_dir,
  1677. .readdir = proc_tgid_attr_readdir,
  1678. };
  1679. static struct file_operations proc_tid_attr_operations = {
  1680. .read = generic_read_dir,
  1681. .readdir = proc_tid_attr_readdir,
  1682. };
  1683. static struct dentry *proc_tgid_attr_lookup(struct inode *dir,
  1684. struct dentry *dentry, struct nameidata *nd)
  1685. {
  1686. return proc_pident_lookup(dir, dentry, tgid_attr_stuff);
  1687. }
  1688. static struct dentry *proc_tid_attr_lookup(struct inode *dir,
  1689. struct dentry *dentry, struct nameidata *nd)
  1690. {
  1691. return proc_pident_lookup(dir, dentry, tid_attr_stuff);
  1692. }
  1693. static struct inode_operations proc_tgid_attr_inode_operations = {
  1694. .lookup = proc_tgid_attr_lookup,
  1695. .getattr = pid_getattr,
  1696. };
  1697. static struct inode_operations proc_tid_attr_inode_operations = {
  1698. .lookup = proc_tid_attr_lookup,
  1699. .getattr = pid_getattr,
  1700. };
  1701. #endif
  1702. /*
  1703. * /proc/self:
  1704. */
  1705. static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
  1706. int buflen)
  1707. {
  1708. char tmp[PROC_NUMBUF];
  1709. sprintf(tmp, "%d", current->tgid);
  1710. return vfs_readlink(dentry,buffer,buflen,tmp);
  1711. }
  1712. static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
  1713. {
  1714. char tmp[PROC_NUMBUF];
  1715. sprintf(tmp, "%d", current->tgid);
  1716. return ERR_PTR(vfs_follow_link(nd,tmp));
  1717. }
  1718. static struct inode_operations proc_self_inode_operations = {
  1719. .readlink = proc_self_readlink,
  1720. .follow_link = proc_self_follow_link,
  1721. };
  1722. /**
  1723. * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
  1724. *
  1725. * @task: task that should be flushed.
  1726. *
  1727. * Looks in the dcache for
  1728. * /proc/@pid
  1729. * /proc/@tgid/task/@pid
  1730. * if either directory is present flushes it and all of it'ts children
  1731. * from the dcache.
  1732. *
  1733. * It is safe and reasonable to cache /proc entries for a task until
  1734. * that task exits. After that they just clog up the dcache with
  1735. * useless entries, possibly causing useful dcache entries to be
  1736. * flushed instead. This routine is proved to flush those useless
  1737. * dcache entries at process exit time.
  1738. *
  1739. * NOTE: This routine is just an optimization so it does not guarantee
  1740. * that no dcache entries will exist at process exit time it
  1741. * just makes it very unlikely that any will persist.
  1742. */
  1743. void proc_flush_task(struct task_struct *task)
  1744. {
  1745. struct dentry *dentry, *leader, *dir;
  1746. char buf[PROC_NUMBUF];
  1747. struct qstr name;
  1748. name.name = buf;
  1749. name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
  1750. dentry = d_hash_and_lookup(proc_mnt->mnt_root, &name);
  1751. if (dentry) {
  1752. shrink_dcache_parent(dentry);
  1753. d_drop(dentry);
  1754. dput(dentry);
  1755. }
  1756. if (thread_group_leader(task))
  1757. goto out;
  1758. name.name = buf;
  1759. name.len = snprintf(buf, sizeof(buf), "%d", task->tgid);
  1760. leader = d_hash_and_lookup(proc_mnt->mnt_root, &name);
  1761. if (!leader)
  1762. goto out;
  1763. name.name = "task";
  1764. name.len = strlen(name.name);
  1765. dir = d_hash_and_lookup(leader, &name);
  1766. if (!dir)
  1767. goto out_put_leader;
  1768. name.name = buf;
  1769. name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
  1770. dentry = d_hash_and_lookup(dir, &name);
  1771. if (dentry) {
  1772. shrink_dcache_parent(dentry);
  1773. d_drop(dentry);
  1774. dput(dentry);
  1775. }
  1776. dput(dir);
  1777. out_put_leader:
  1778. dput(leader);
  1779. out:
  1780. return;
  1781. }
  1782. /* SMP-safe */
  1783. struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
  1784. {
  1785. struct dentry *result = ERR_PTR(-ENOENT);
  1786. struct task_struct *task;
  1787. struct inode *inode;
  1788. struct proc_inode *ei;
  1789. unsigned tgid;
  1790. if (dentry->d_name.len == 4 && !memcmp(dentry->d_name.name,"self",4)) {
  1791. inode = new_inode(dir->i_sb);
  1792. if (!inode)
  1793. return ERR_PTR(-ENOMEM);
  1794. ei = PROC_I(inode);
  1795. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  1796. inode->i_ino = fake_ino(0, PROC_TGID_INO);
  1797. ei->pde = NULL;
  1798. inode->i_mode = S_IFLNK|S_IRWXUGO;
  1799. inode->i_uid = inode->i_gid = 0;
  1800. inode->i_size = 64;
  1801. inode->i_op = &proc_self_inode_operations;
  1802. d_add(dentry, inode);
  1803. return NULL;
  1804. }
  1805. tgid = name_to_int(dentry);
  1806. if (tgid == ~0U)
  1807. goto out;
  1808. rcu_read_lock();
  1809. task = find_task_by_pid(tgid);
  1810. if (task)
  1811. get_task_struct(task);
  1812. rcu_read_unlock();
  1813. if (!task)
  1814. goto out;
  1815. inode = proc_pid_make_inode(dir->i_sb, task, PROC_TGID_INO);
  1816. if (!inode)
  1817. goto out_put_task;
  1818. inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
  1819. inode->i_op = &proc_tgid_base_inode_operations;
  1820. inode->i_fop = &proc_tgid_base_operations;
  1821. inode->i_flags|=S_IMMUTABLE;
  1822. #ifdef CONFIG_SECURITY
  1823. inode->i_nlink = 5;
  1824. #else
  1825. inode->i_nlink = 4;
  1826. #endif
  1827. dentry->d_op = &pid_dentry_operations;
  1828. d_add(dentry, inode);
  1829. /* Close the race of the process dying before we return the dentry */
  1830. if (pid_revalidate(dentry, NULL))
  1831. result = NULL;
  1832. out_put_task:
  1833. put_task_struct(task);
  1834. out:
  1835. return result;
  1836. }
  1837. /* SMP-safe */
  1838. static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
  1839. {
  1840. struct dentry *result = ERR_PTR(-ENOENT);
  1841. struct task_struct *task;
  1842. struct task_struct *leader = get_proc_task(dir);
  1843. struct inode *inode;
  1844. unsigned tid;
  1845. if (!leader)
  1846. goto out_no_task;
  1847. tid = name_to_int(dentry);
  1848. if (tid == ~0U)
  1849. goto out;
  1850. rcu_read_lock();
  1851. task = find_task_by_pid(tid);
  1852. if (task)
  1853. get_task_struct(task);
  1854. rcu_read_unlock();
  1855. if (!task)
  1856. goto out;
  1857. if (leader->tgid != task->tgid)
  1858. goto out_drop_task;
  1859. inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_INO);
  1860. if (!inode)
  1861. goto out_drop_task;
  1862. inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
  1863. inode->i_op = &proc_tid_base_inode_operations;
  1864. inode->i_fop = &proc_tid_base_operations;
  1865. inode->i_flags|=S_IMMUTABLE;
  1866. #ifdef CONFIG_SECURITY
  1867. inode->i_nlink = 4;
  1868. #else
  1869. inode->i_nlink = 3;
  1870. #endif
  1871. dentry->d_op = &pid_dentry_operations;
  1872. d_add(dentry, inode);
  1873. /* Close the race of the process dying before we return the dentry */
  1874. if (pid_revalidate(dentry, NULL))
  1875. result = NULL;
  1876. out_drop_task:
  1877. put_task_struct(task);
  1878. out:
  1879. put_task_struct(leader);
  1880. out_no_task:
  1881. return result;
  1882. }
  1883. /*
  1884. * Find the first tgid to return to user space.
  1885. *
  1886. * Usually this is just whatever follows &init_task, but if the users
  1887. * buffer was too small to hold the full list or there was a seek into
  1888. * the middle of the directory we have more work to do.
  1889. *
  1890. * In the case of a short read we start with find_task_by_pid.
  1891. *
  1892. * In the case of a seek we start with &init_task and walk nr
  1893. * threads past it.
  1894. */
  1895. static struct task_struct *first_tgid(int tgid, unsigned int nr)
  1896. {
  1897. struct task_struct *pos;
  1898. rcu_read_lock();
  1899. if (tgid && nr) {
  1900. pos = find_task_by_pid(tgid);
  1901. if (pos && thread_group_leader(pos))
  1902. goto found;
  1903. }
  1904. /* If nr exceeds the number of processes get out quickly */
  1905. pos = NULL;
  1906. if (nr && nr >= nr_processes())
  1907. goto done;
  1908. /* If we haven't found our starting place yet start with
  1909. * the init_task and walk nr tasks forward.
  1910. */
  1911. for (pos = next_task(&init_task); nr > 0; --nr) {
  1912. pos = next_task(pos);
  1913. if (pos == &init_task) {
  1914. pos = NULL;
  1915. goto done;
  1916. }
  1917. }
  1918. found:
  1919. get_task_struct(pos);
  1920. done:
  1921. rcu_read_unlock();
  1922. return pos;
  1923. }
  1924. /*
  1925. * Find the next task in the task list.
  1926. * Return NULL if we loop or there is any error.
  1927. *
  1928. * The reference to the input task_struct is released.
  1929. */
  1930. static struct task_struct *next_tgid(struct task_struct *start)
  1931. {
  1932. struct task_struct *pos;
  1933. rcu_read_lock();
  1934. pos = start;
  1935. if (pid_alive(start))
  1936. pos = next_task(start);
  1937. if (pid_alive(pos) && (pos != &init_task)) {
  1938. get_task_struct(pos);
  1939. goto done;
  1940. }
  1941. pos = NULL;
  1942. done:
  1943. rcu_read_unlock();
  1944. put_task_struct(start);
  1945. return pos;
  1946. }
  1947. /* for the /proc/ directory itself, after non-process stuff has been done */
  1948. int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
  1949. {
  1950. char buf[PROC_NUMBUF];
  1951. unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
  1952. struct task_struct *task;
  1953. int tgid;
  1954. if (!nr) {
  1955. ino_t ino = fake_ino(0,PROC_TGID_INO);
  1956. if (filldir(dirent, "self", 4, filp->f_pos, ino, DT_LNK) < 0)
  1957. return 0;
  1958. filp->f_pos++;
  1959. nr++;
  1960. }
  1961. nr -= 1;
  1962. /* f_version caches the tgid value that the last readdir call couldn't
  1963. * return. lseek aka telldir automagically resets f_version to 0.
  1964. */
  1965. tgid = filp->f_version;
  1966. filp->f_version = 0;
  1967. for (task = first_tgid(tgid, nr);
  1968. task;
  1969. task = next_tgid(task), filp->f_pos++) {
  1970. int len;
  1971. ino_t ino;
  1972. tgid = task->pid;
  1973. len = snprintf(buf, sizeof(buf), "%d", tgid);
  1974. ino = fake_ino(tgid, PROC_TGID_INO);
  1975. if (filldir(dirent, buf, len, filp->f_pos, ino, DT_DIR) < 0) {
  1976. /* returning this tgid failed, save it as the first
  1977. * pid for the next readir call */
  1978. filp->f_version = tgid;
  1979. put_task_struct(task);
  1980. break;
  1981. }
  1982. }
  1983. return 0;
  1984. }
  1985. /*
  1986. * Find the first tid of a thread group to return to user space.
  1987. *
  1988. * Usually this is just the thread group leader, but if the users
  1989. * buffer was too small or there was a seek into the middle of the
  1990. * directory we have more work todo.
  1991. *
  1992. * In the case of a short read we start with find_task_by_pid.
  1993. *
  1994. * In the case of a seek we start with the leader and walk nr
  1995. * threads past it.
  1996. */
  1997. static struct task_struct *first_tid(struct task_struct *leader,
  1998. int tid, int nr)
  1999. {
  2000. struct task_struct *pos;
  2001. rcu_read_lock();
  2002. /* Attempt to start with the pid of a thread */
  2003. if (tid && (nr > 0)) {
  2004. pos = find_task_by_pid(tid);
  2005. if (pos && (pos->group_leader == leader))
  2006. goto found;
  2007. }
  2008. /* If nr exceeds the number of threads there is nothing todo */
  2009. pos = NULL;
  2010. if (nr && nr >= get_nr_threads(leader))
  2011. goto out;
  2012. /* If we haven't found our starting place yet start
  2013. * with the leader and walk nr threads forward.
  2014. */
  2015. for (pos = leader; nr > 0; --nr) {
  2016. pos = next_thread(pos);
  2017. if (pos == leader) {
  2018. pos = NULL;
  2019. goto out;
  2020. }
  2021. }
  2022. found:
  2023. get_task_struct(pos);
  2024. out:
  2025. rcu_read_unlock();
  2026. return pos;
  2027. }
  2028. /*
  2029. * Find the next thread in the thread list.
  2030. * Return NULL if there is an error or no next thread.
  2031. *
  2032. * The reference to the input task_struct is released.
  2033. */
  2034. static struct task_struct *next_tid(struct task_struct *start)
  2035. {
  2036. struct task_struct *pos = NULL;
  2037. rcu_read_lock();
  2038. if (pid_alive(start)) {
  2039. pos = next_thread(start);
  2040. if (thread_group_leader(pos))
  2041. pos = NULL;
  2042. else
  2043. get_task_struct(pos);
  2044. }
  2045. rcu_read_unlock();
  2046. put_task_struct(start);
  2047. return pos;
  2048. }
  2049. /* for the /proc/TGID/task/ directories */
  2050. static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
  2051. {
  2052. char buf[PROC_NUMBUF];
  2053. struct dentry *dentry = filp->f_dentry;
  2054. struct inode *inode = dentry->d_inode;
  2055. struct task_struct *leader = get_proc_task(inode);
  2056. struct task_struct *task;
  2057. int retval = -ENOENT;
  2058. ino_t ino;
  2059. int tid;
  2060. unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
  2061. if (!leader)
  2062. goto out_no_task;
  2063. retval = 0;
  2064. switch (pos) {
  2065. case 0:
  2066. ino = inode->i_ino;
  2067. if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
  2068. goto out;
  2069. pos++;
  2070. /* fall through */
  2071. case 1:
  2072. ino = parent_ino(dentry);
  2073. if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
  2074. goto out;
  2075. pos++;
  2076. /* fall through */
  2077. }
  2078. /* f_version caches the tgid value that the last readdir call couldn't
  2079. * return. lseek aka telldir automagically resets f_version to 0.
  2080. */
  2081. tid = filp->f_version;
  2082. filp->f_version = 0;
  2083. for (task = first_tid(leader, tid, pos - 2);
  2084. task;
  2085. task = next_tid(task), pos++) {
  2086. int len;
  2087. tid = task->pid;
  2088. len = snprintf(buf, sizeof(buf), "%d", tid);
  2089. ino = fake_ino(tid, PROC_TID_INO);
  2090. if (filldir(dirent, buf, len, pos, ino, DT_DIR < 0)) {
  2091. /* returning this tgid failed, save it as the first
  2092. * pid for the next readir call */
  2093. filp->f_version = tid;
  2094. put_task_struct(task);
  2095. break;
  2096. }
  2097. }
  2098. out:
  2099. filp->f_pos = pos;
  2100. put_task_struct(leader);
  2101. out_no_task:
  2102. return retval;
  2103. }
  2104. static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
  2105. {
  2106. struct inode *inode = dentry->d_inode;
  2107. struct task_struct *p = get_proc_task(inode);
  2108. generic_fillattr(inode, stat);
  2109. if (p) {
  2110. rcu_read_lock();
  2111. stat->nlink += get_nr_threads(p);
  2112. rcu_read_unlock();
  2113. put_task_struct(p);
  2114. }
  2115. return 0;
  2116. }