base.c 55 KB

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