base.c 54 KB

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