base.c 54 KB

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