base.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316
  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. if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
  1038. goto out;
  1039. error = proc_check_root(inode);
  1040. if (error)
  1041. goto out;
  1042. error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
  1043. if (error)
  1044. goto out;
  1045. error = do_proc_readlink(de, mnt, buffer, buflen);
  1046. dput(de);
  1047. mntput(mnt);
  1048. out:
  1049. return error;
  1050. }
  1051. static struct inode_operations proc_pid_link_inode_operations = {
  1052. .readlink = proc_pid_readlink,
  1053. .follow_link = proc_pid_follow_link
  1054. };
  1055. #define NUMBUF 10
  1056. static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
  1057. {
  1058. struct dentry *dentry = filp->f_dentry;
  1059. struct inode *inode = dentry->d_inode;
  1060. struct task_struct *p = proc_task(inode);
  1061. unsigned int fd, tid, ino;
  1062. int retval;
  1063. char buf[NUMBUF];
  1064. struct files_struct * files;
  1065. struct fdtable *fdt;
  1066. retval = -ENOENT;
  1067. if (!pid_alive(p))
  1068. goto out;
  1069. retval = 0;
  1070. tid = p->pid;
  1071. fd = filp->f_pos;
  1072. switch (fd) {
  1073. case 0:
  1074. if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
  1075. goto out;
  1076. filp->f_pos++;
  1077. case 1:
  1078. ino = parent_ino(dentry);
  1079. if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
  1080. goto out;
  1081. filp->f_pos++;
  1082. default:
  1083. files = get_files_struct(p);
  1084. if (!files)
  1085. goto out;
  1086. rcu_read_lock();
  1087. fdt = files_fdtable(files);
  1088. for (fd = filp->f_pos-2;
  1089. fd < fdt->max_fds;
  1090. fd++, filp->f_pos++) {
  1091. unsigned int i,j;
  1092. if (!fcheck_files(files, fd))
  1093. continue;
  1094. rcu_read_unlock();
  1095. j = NUMBUF;
  1096. i = fd;
  1097. do {
  1098. j--;
  1099. buf[j] = '0' + (i % 10);
  1100. i /= 10;
  1101. } while (i);
  1102. ino = fake_ino(tid, PROC_TID_FD_DIR + fd);
  1103. if (filldir(dirent, buf+j, NUMBUF-j, fd+2, ino, DT_LNK) < 0) {
  1104. rcu_read_lock();
  1105. break;
  1106. }
  1107. rcu_read_lock();
  1108. }
  1109. rcu_read_unlock();
  1110. put_files_struct(files);
  1111. }
  1112. out:
  1113. return retval;
  1114. }
  1115. static int proc_pident_readdir(struct file *filp,
  1116. void *dirent, filldir_t filldir,
  1117. struct pid_entry *ents, unsigned int nents)
  1118. {
  1119. int i;
  1120. int pid;
  1121. struct dentry *dentry = filp->f_dentry;
  1122. struct inode *inode = dentry->d_inode;
  1123. struct pid_entry *p;
  1124. ino_t ino;
  1125. int ret;
  1126. ret = -ENOENT;
  1127. if (!pid_alive(proc_task(inode)))
  1128. goto out;
  1129. ret = 0;
  1130. pid = proc_task(inode)->pid;
  1131. i = filp->f_pos;
  1132. switch (i) {
  1133. case 0:
  1134. ino = inode->i_ino;
  1135. if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
  1136. goto out;
  1137. i++;
  1138. filp->f_pos++;
  1139. /* fall through */
  1140. case 1:
  1141. ino = parent_ino(dentry);
  1142. if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
  1143. goto out;
  1144. i++;
  1145. filp->f_pos++;
  1146. /* fall through */
  1147. default:
  1148. i -= 2;
  1149. if (i >= nents) {
  1150. ret = 1;
  1151. goto out;
  1152. }
  1153. p = ents + i;
  1154. while (p->name) {
  1155. if (filldir(dirent, p->name, p->len, filp->f_pos,
  1156. fake_ino(pid, p->type), p->mode >> 12) < 0)
  1157. goto out;
  1158. filp->f_pos++;
  1159. p++;
  1160. }
  1161. }
  1162. ret = 1;
  1163. out:
  1164. return ret;
  1165. }
  1166. static int proc_tgid_base_readdir(struct file * filp,
  1167. void * dirent, filldir_t filldir)
  1168. {
  1169. return proc_pident_readdir(filp,dirent,filldir,
  1170. tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
  1171. }
  1172. static int proc_tid_base_readdir(struct file * filp,
  1173. void * dirent, filldir_t filldir)
  1174. {
  1175. return proc_pident_readdir(filp,dirent,filldir,
  1176. tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
  1177. }
  1178. /* building an inode */
  1179. static int task_dumpable(struct task_struct *task)
  1180. {
  1181. int dumpable = 0;
  1182. struct mm_struct *mm;
  1183. task_lock(task);
  1184. mm = task->mm;
  1185. if (mm)
  1186. dumpable = mm->dumpable;
  1187. task_unlock(task);
  1188. if(dumpable == 1)
  1189. return 1;
  1190. return 0;
  1191. }
  1192. static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task, int ino)
  1193. {
  1194. struct inode * inode;
  1195. struct proc_inode *ei;
  1196. /* We need a new inode */
  1197. inode = new_inode(sb);
  1198. if (!inode)
  1199. goto out;
  1200. /* Common stuff */
  1201. ei = PROC_I(inode);
  1202. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  1203. inode->i_ino = fake_ino(task->pid, ino);
  1204. if (!pid_alive(task))
  1205. goto out_unlock;
  1206. /*
  1207. * grab the reference to task.
  1208. */
  1209. get_task_struct(task);
  1210. ei->task = task;
  1211. ei->type = ino;
  1212. inode->i_uid = 0;
  1213. inode->i_gid = 0;
  1214. if (task_dumpable(task)) {
  1215. inode->i_uid = task->euid;
  1216. inode->i_gid = task->egid;
  1217. }
  1218. security_task_to_inode(task, inode);
  1219. out:
  1220. return inode;
  1221. out_unlock:
  1222. iput(inode);
  1223. return NULL;
  1224. }
  1225. /* dentry stuff */
  1226. /*
  1227. * Exceptional case: normally we are not allowed to unhash a busy
  1228. * directory. In this case, however, we can do it - no aliasing problems
  1229. * due to the way we treat inodes.
  1230. *
  1231. * Rewrite the inode's ownerships here because the owning task may have
  1232. * performed a setuid(), etc.
  1233. */
  1234. static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
  1235. {
  1236. struct inode *inode = dentry->d_inode;
  1237. struct task_struct *task = proc_task(inode);
  1238. if (pid_alive(task)) {
  1239. if (task_dumpable(task)) {
  1240. inode->i_uid = task->euid;
  1241. inode->i_gid = task->egid;
  1242. } else {
  1243. inode->i_uid = 0;
  1244. inode->i_gid = 0;
  1245. }
  1246. security_task_to_inode(task, inode);
  1247. return 1;
  1248. }
  1249. d_drop(dentry);
  1250. return 0;
  1251. }
  1252. static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
  1253. {
  1254. struct inode *inode = dentry->d_inode;
  1255. struct task_struct *task = proc_task(inode);
  1256. int fd = proc_type(inode) - PROC_TID_FD_DIR;
  1257. struct files_struct *files;
  1258. files = get_files_struct(task);
  1259. if (files) {
  1260. rcu_read_lock();
  1261. if (fcheck_files(files, fd)) {
  1262. rcu_read_unlock();
  1263. put_files_struct(files);
  1264. if (task_dumpable(task)) {
  1265. inode->i_uid = task->euid;
  1266. inode->i_gid = task->egid;
  1267. } else {
  1268. inode->i_uid = 0;
  1269. inode->i_gid = 0;
  1270. }
  1271. security_task_to_inode(task, inode);
  1272. return 1;
  1273. }
  1274. rcu_read_unlock();
  1275. put_files_struct(files);
  1276. }
  1277. d_drop(dentry);
  1278. return 0;
  1279. }
  1280. static void pid_base_iput(struct dentry *dentry, struct inode *inode)
  1281. {
  1282. struct task_struct *task = proc_task(inode);
  1283. spin_lock(&task->proc_lock);
  1284. if (task->proc_dentry == dentry)
  1285. task->proc_dentry = NULL;
  1286. spin_unlock(&task->proc_lock);
  1287. iput(inode);
  1288. }
  1289. static int pid_delete_dentry(struct dentry * dentry)
  1290. {
  1291. /* Is the task we represent dead?
  1292. * If so, then don't put the dentry on the lru list,
  1293. * kill it immediately.
  1294. */
  1295. return !pid_alive(proc_task(dentry->d_inode));
  1296. }
  1297. static struct dentry_operations tid_fd_dentry_operations =
  1298. {
  1299. .d_revalidate = tid_fd_revalidate,
  1300. .d_delete = pid_delete_dentry,
  1301. };
  1302. static struct dentry_operations pid_dentry_operations =
  1303. {
  1304. .d_revalidate = pid_revalidate,
  1305. .d_delete = pid_delete_dentry,
  1306. };
  1307. static struct dentry_operations pid_base_dentry_operations =
  1308. {
  1309. .d_revalidate = pid_revalidate,
  1310. .d_iput = pid_base_iput,
  1311. .d_delete = pid_delete_dentry,
  1312. };
  1313. /* Lookups */
  1314. static unsigned name_to_int(struct dentry *dentry)
  1315. {
  1316. const char *name = dentry->d_name.name;
  1317. int len = dentry->d_name.len;
  1318. unsigned n = 0;
  1319. if (len > 1 && *name == '0')
  1320. goto out;
  1321. while (len-- > 0) {
  1322. unsigned c = *name++ - '0';
  1323. if (c > 9)
  1324. goto out;
  1325. if (n >= (~0U-9)/10)
  1326. goto out;
  1327. n *= 10;
  1328. n += c;
  1329. }
  1330. return n;
  1331. out:
  1332. return ~0U;
  1333. }
  1334. /* SMP-safe */
  1335. static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
  1336. {
  1337. struct task_struct *task = proc_task(dir);
  1338. unsigned fd = name_to_int(dentry);
  1339. struct file * file;
  1340. struct files_struct * files;
  1341. struct inode *inode;
  1342. struct proc_inode *ei;
  1343. if (fd == ~0U)
  1344. goto out;
  1345. if (!pid_alive(task))
  1346. goto out;
  1347. inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_FD_DIR+fd);
  1348. if (!inode)
  1349. goto out;
  1350. ei = PROC_I(inode);
  1351. files = get_files_struct(task);
  1352. if (!files)
  1353. goto out_unlock;
  1354. inode->i_mode = S_IFLNK;
  1355. /*
  1356. * We are not taking a ref to the file structure, so we must
  1357. * hold ->file_lock.
  1358. */
  1359. spin_lock(&files->file_lock);
  1360. file = fcheck_files(files, fd);
  1361. if (!file)
  1362. goto out_unlock2;
  1363. if (file->f_mode & 1)
  1364. inode->i_mode |= S_IRUSR | S_IXUSR;
  1365. if (file->f_mode & 2)
  1366. inode->i_mode |= S_IWUSR | S_IXUSR;
  1367. spin_unlock(&files->file_lock);
  1368. put_files_struct(files);
  1369. inode->i_op = &proc_pid_link_inode_operations;
  1370. inode->i_size = 64;
  1371. ei->op.proc_get_link = proc_fd_link;
  1372. dentry->d_op = &tid_fd_dentry_operations;
  1373. d_add(dentry, inode);
  1374. return NULL;
  1375. out_unlock2:
  1376. spin_unlock(&files->file_lock);
  1377. put_files_struct(files);
  1378. out_unlock:
  1379. iput(inode);
  1380. out:
  1381. return ERR_PTR(-ENOENT);
  1382. }
  1383. static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir);
  1384. static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd);
  1385. static struct file_operations proc_fd_operations = {
  1386. .read = generic_read_dir,
  1387. .readdir = proc_readfd,
  1388. };
  1389. static struct file_operations proc_task_operations = {
  1390. .read = generic_read_dir,
  1391. .readdir = proc_task_readdir,
  1392. };
  1393. /*
  1394. * proc directories can do almost nothing..
  1395. */
  1396. static struct inode_operations proc_fd_inode_operations = {
  1397. .lookup = proc_lookupfd,
  1398. .permission = proc_permission,
  1399. };
  1400. static struct inode_operations proc_task_inode_operations = {
  1401. .lookup = proc_task_lookup,
  1402. .permission = proc_task_permission,
  1403. };
  1404. #ifdef CONFIG_SECURITY
  1405. static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
  1406. size_t count, loff_t *ppos)
  1407. {
  1408. struct inode * inode = file->f_dentry->d_inode;
  1409. unsigned long page;
  1410. ssize_t length;
  1411. struct task_struct *task = proc_task(inode);
  1412. if (count > PAGE_SIZE)
  1413. count = PAGE_SIZE;
  1414. if (!(page = __get_free_page(GFP_KERNEL)))
  1415. return -ENOMEM;
  1416. length = security_getprocattr(task,
  1417. (char*)file->f_dentry->d_name.name,
  1418. (void*)page, count);
  1419. if (length >= 0)
  1420. length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
  1421. free_page(page);
  1422. return length;
  1423. }
  1424. static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
  1425. size_t count, loff_t *ppos)
  1426. {
  1427. struct inode * inode = file->f_dentry->d_inode;
  1428. char *page;
  1429. ssize_t length;
  1430. struct task_struct *task = proc_task(inode);
  1431. if (count > PAGE_SIZE)
  1432. count = PAGE_SIZE;
  1433. if (*ppos != 0) {
  1434. /* No partial writes. */
  1435. return -EINVAL;
  1436. }
  1437. page = (char*)__get_free_page(GFP_USER);
  1438. if (!page)
  1439. return -ENOMEM;
  1440. length = -EFAULT;
  1441. if (copy_from_user(page, buf, count))
  1442. goto out;
  1443. length = security_setprocattr(task,
  1444. (char*)file->f_dentry->d_name.name,
  1445. (void*)page, count);
  1446. out:
  1447. free_page((unsigned long) page);
  1448. return length;
  1449. }
  1450. static struct file_operations proc_pid_attr_operations = {
  1451. .read = proc_pid_attr_read,
  1452. .write = proc_pid_attr_write,
  1453. };
  1454. static struct file_operations proc_tid_attr_operations;
  1455. static struct inode_operations proc_tid_attr_inode_operations;
  1456. static struct file_operations proc_tgid_attr_operations;
  1457. static struct inode_operations proc_tgid_attr_inode_operations;
  1458. #endif
  1459. static int get_tid_list(int index, unsigned int *tids, struct inode *dir);
  1460. /* SMP-safe */
  1461. static struct dentry *proc_pident_lookup(struct inode *dir,
  1462. struct dentry *dentry,
  1463. struct pid_entry *ents)
  1464. {
  1465. struct inode *inode;
  1466. int error;
  1467. struct task_struct *task = proc_task(dir);
  1468. struct pid_entry *p;
  1469. struct proc_inode *ei;
  1470. error = -ENOENT;
  1471. inode = NULL;
  1472. if (!pid_alive(task))
  1473. goto out;
  1474. for (p = ents; p->name; p++) {
  1475. if (p->len != dentry->d_name.len)
  1476. continue;
  1477. if (!memcmp(dentry->d_name.name, p->name, p->len))
  1478. break;
  1479. }
  1480. if (!p->name)
  1481. goto out;
  1482. error = -EINVAL;
  1483. inode = proc_pid_make_inode(dir->i_sb, task, p->type);
  1484. if (!inode)
  1485. goto out;
  1486. ei = PROC_I(inode);
  1487. inode->i_mode = p->mode;
  1488. /*
  1489. * Yes, it does not scale. And it should not. Don't add
  1490. * new entries into /proc/<tgid>/ without very good reasons.
  1491. */
  1492. switch(p->type) {
  1493. case PROC_TGID_TASK:
  1494. inode->i_nlink = 2 + get_tid_list(2, NULL, dir);
  1495. inode->i_op = &proc_task_inode_operations;
  1496. inode->i_fop = &proc_task_operations;
  1497. break;
  1498. case PROC_TID_FD:
  1499. case PROC_TGID_FD:
  1500. inode->i_nlink = 2;
  1501. inode->i_op = &proc_fd_inode_operations;
  1502. inode->i_fop = &proc_fd_operations;
  1503. break;
  1504. case PROC_TID_EXE:
  1505. case PROC_TGID_EXE:
  1506. inode->i_op = &proc_pid_link_inode_operations;
  1507. ei->op.proc_get_link = proc_exe_link;
  1508. break;
  1509. case PROC_TID_CWD:
  1510. case PROC_TGID_CWD:
  1511. inode->i_op = &proc_pid_link_inode_operations;
  1512. ei->op.proc_get_link = proc_cwd_link;
  1513. break;
  1514. case PROC_TID_ROOT:
  1515. case PROC_TGID_ROOT:
  1516. inode->i_op = &proc_pid_link_inode_operations;
  1517. ei->op.proc_get_link = proc_root_link;
  1518. break;
  1519. case PROC_TID_ENVIRON:
  1520. case PROC_TGID_ENVIRON:
  1521. inode->i_fop = &proc_info_file_operations;
  1522. ei->op.proc_read = proc_pid_environ;
  1523. break;
  1524. case PROC_TID_AUXV:
  1525. case PROC_TGID_AUXV:
  1526. inode->i_fop = &proc_info_file_operations;
  1527. ei->op.proc_read = proc_pid_auxv;
  1528. break;
  1529. case PROC_TID_STATUS:
  1530. case PROC_TGID_STATUS:
  1531. inode->i_fop = &proc_info_file_operations;
  1532. ei->op.proc_read = proc_pid_status;
  1533. break;
  1534. case PROC_TID_STAT:
  1535. inode->i_fop = &proc_info_file_operations;
  1536. ei->op.proc_read = proc_tid_stat;
  1537. break;
  1538. case PROC_TGID_STAT:
  1539. inode->i_fop = &proc_info_file_operations;
  1540. ei->op.proc_read = proc_tgid_stat;
  1541. break;
  1542. case PROC_TID_CMDLINE:
  1543. case PROC_TGID_CMDLINE:
  1544. inode->i_fop = &proc_info_file_operations;
  1545. ei->op.proc_read = proc_pid_cmdline;
  1546. break;
  1547. case PROC_TID_STATM:
  1548. case PROC_TGID_STATM:
  1549. inode->i_fop = &proc_info_file_operations;
  1550. ei->op.proc_read = proc_pid_statm;
  1551. break;
  1552. case PROC_TID_MAPS:
  1553. case PROC_TGID_MAPS:
  1554. inode->i_fop = &proc_maps_operations;
  1555. break;
  1556. #ifdef CONFIG_NUMA
  1557. case PROC_TID_NUMA_MAPS:
  1558. case PROC_TGID_NUMA_MAPS:
  1559. inode->i_fop = &proc_numa_maps_operations;
  1560. break;
  1561. #endif
  1562. case PROC_TID_MEM:
  1563. case PROC_TGID_MEM:
  1564. inode->i_op = &proc_mem_inode_operations;
  1565. inode->i_fop = &proc_mem_operations;
  1566. break;
  1567. #ifdef CONFIG_SECCOMP
  1568. case PROC_TID_SECCOMP:
  1569. case PROC_TGID_SECCOMP:
  1570. inode->i_fop = &proc_seccomp_operations;
  1571. break;
  1572. #endif /* CONFIG_SECCOMP */
  1573. case PROC_TID_MOUNTS:
  1574. case PROC_TGID_MOUNTS:
  1575. inode->i_fop = &proc_mounts_operations;
  1576. break;
  1577. #ifdef CONFIG_MMU
  1578. case PROC_TID_SMAPS:
  1579. case PROC_TGID_SMAPS:
  1580. inode->i_fop = &proc_smaps_operations;
  1581. break;
  1582. #endif
  1583. case PROC_TID_MOUNTSTATS:
  1584. case PROC_TGID_MOUNTSTATS:
  1585. inode->i_fop = &proc_mountstats_operations;
  1586. break;
  1587. #ifdef CONFIG_SECURITY
  1588. case PROC_TID_ATTR:
  1589. inode->i_nlink = 2;
  1590. inode->i_op = &proc_tid_attr_inode_operations;
  1591. inode->i_fop = &proc_tid_attr_operations;
  1592. break;
  1593. case PROC_TGID_ATTR:
  1594. inode->i_nlink = 2;
  1595. inode->i_op = &proc_tgid_attr_inode_operations;
  1596. inode->i_fop = &proc_tgid_attr_operations;
  1597. break;
  1598. case PROC_TID_ATTR_CURRENT:
  1599. case PROC_TGID_ATTR_CURRENT:
  1600. case PROC_TID_ATTR_PREV:
  1601. case PROC_TGID_ATTR_PREV:
  1602. case PROC_TID_ATTR_EXEC:
  1603. case PROC_TGID_ATTR_EXEC:
  1604. case PROC_TID_ATTR_FSCREATE:
  1605. case PROC_TGID_ATTR_FSCREATE:
  1606. case PROC_TID_ATTR_KEYCREATE:
  1607. case PROC_TGID_ATTR_KEYCREATE:
  1608. inode->i_fop = &proc_pid_attr_operations;
  1609. break;
  1610. #endif
  1611. #ifdef CONFIG_KALLSYMS
  1612. case PROC_TID_WCHAN:
  1613. case PROC_TGID_WCHAN:
  1614. inode->i_fop = &proc_info_file_operations;
  1615. ei->op.proc_read = proc_pid_wchan;
  1616. break;
  1617. #endif
  1618. #ifdef CONFIG_SCHEDSTATS
  1619. case PROC_TID_SCHEDSTAT:
  1620. case PROC_TGID_SCHEDSTAT:
  1621. inode->i_fop = &proc_info_file_operations;
  1622. ei->op.proc_read = proc_pid_schedstat;
  1623. break;
  1624. #endif
  1625. #ifdef CONFIG_CPUSETS
  1626. case PROC_TID_CPUSET:
  1627. case PROC_TGID_CPUSET:
  1628. inode->i_fop = &proc_cpuset_operations;
  1629. break;
  1630. #endif
  1631. case PROC_TID_OOM_SCORE:
  1632. case PROC_TGID_OOM_SCORE:
  1633. inode->i_fop = &proc_info_file_operations;
  1634. ei->op.proc_read = proc_oom_score;
  1635. break;
  1636. case PROC_TID_OOM_ADJUST:
  1637. case PROC_TGID_OOM_ADJUST:
  1638. inode->i_fop = &proc_oom_adjust_operations;
  1639. break;
  1640. #ifdef CONFIG_AUDITSYSCALL
  1641. case PROC_TID_LOGINUID:
  1642. case PROC_TGID_LOGINUID:
  1643. inode->i_fop = &proc_loginuid_operations;
  1644. break;
  1645. #endif
  1646. default:
  1647. printk("procfs: impossible type (%d)",p->type);
  1648. iput(inode);
  1649. return ERR_PTR(-EINVAL);
  1650. }
  1651. dentry->d_op = &pid_dentry_operations;
  1652. d_add(dentry, inode);
  1653. return NULL;
  1654. out:
  1655. return ERR_PTR(error);
  1656. }
  1657. static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
  1658. return proc_pident_lookup(dir, dentry, tgid_base_stuff);
  1659. }
  1660. static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
  1661. return proc_pident_lookup(dir, dentry, tid_base_stuff);
  1662. }
  1663. static struct file_operations proc_tgid_base_operations = {
  1664. .read = generic_read_dir,
  1665. .readdir = proc_tgid_base_readdir,
  1666. };
  1667. static struct file_operations proc_tid_base_operations = {
  1668. .read = generic_read_dir,
  1669. .readdir = proc_tid_base_readdir,
  1670. };
  1671. static struct inode_operations proc_tgid_base_inode_operations = {
  1672. .lookup = proc_tgid_base_lookup,
  1673. };
  1674. static struct inode_operations proc_tid_base_inode_operations = {
  1675. .lookup = proc_tid_base_lookup,
  1676. };
  1677. #ifdef CONFIG_SECURITY
  1678. static int proc_tgid_attr_readdir(struct file * filp,
  1679. void * dirent, filldir_t filldir)
  1680. {
  1681. return proc_pident_readdir(filp,dirent,filldir,
  1682. tgid_attr_stuff,ARRAY_SIZE(tgid_attr_stuff));
  1683. }
  1684. static int proc_tid_attr_readdir(struct file * filp,
  1685. void * dirent, filldir_t filldir)
  1686. {
  1687. return proc_pident_readdir(filp,dirent,filldir,
  1688. tid_attr_stuff,ARRAY_SIZE(tid_attr_stuff));
  1689. }
  1690. static struct file_operations proc_tgid_attr_operations = {
  1691. .read = generic_read_dir,
  1692. .readdir = proc_tgid_attr_readdir,
  1693. };
  1694. static struct file_operations proc_tid_attr_operations = {
  1695. .read = generic_read_dir,
  1696. .readdir = proc_tid_attr_readdir,
  1697. };
  1698. static struct dentry *proc_tgid_attr_lookup(struct inode *dir,
  1699. struct dentry *dentry, struct nameidata *nd)
  1700. {
  1701. return proc_pident_lookup(dir, dentry, tgid_attr_stuff);
  1702. }
  1703. static struct dentry *proc_tid_attr_lookup(struct inode *dir,
  1704. struct dentry *dentry, struct nameidata *nd)
  1705. {
  1706. return proc_pident_lookup(dir, dentry, tid_attr_stuff);
  1707. }
  1708. static struct inode_operations proc_tgid_attr_inode_operations = {
  1709. .lookup = proc_tgid_attr_lookup,
  1710. };
  1711. static struct inode_operations proc_tid_attr_inode_operations = {
  1712. .lookup = proc_tid_attr_lookup,
  1713. };
  1714. #endif
  1715. /*
  1716. * /proc/self:
  1717. */
  1718. static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
  1719. int buflen)
  1720. {
  1721. char tmp[30];
  1722. sprintf(tmp, "%d", current->tgid);
  1723. return vfs_readlink(dentry,buffer,buflen,tmp);
  1724. }
  1725. static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
  1726. {
  1727. char tmp[30];
  1728. sprintf(tmp, "%d", current->tgid);
  1729. return ERR_PTR(vfs_follow_link(nd,tmp));
  1730. }
  1731. static struct inode_operations proc_self_inode_operations = {
  1732. .readlink = proc_self_readlink,
  1733. .follow_link = proc_self_follow_link,
  1734. };
  1735. /**
  1736. * proc_pid_unhash - Unhash /proc/@pid entry from the dcache.
  1737. * @p: task that should be flushed.
  1738. *
  1739. * Drops the /proc/@pid dcache entry from the hash chains.
  1740. *
  1741. * Dropping /proc/@pid entries and detach_pid must be synchroneous,
  1742. * otherwise e.g. /proc/@pid/exe might point to the wrong executable,
  1743. * if the pid value is immediately reused. This is enforced by
  1744. * - caller must acquire spin_lock(p->proc_lock)
  1745. * - must be called before detach_pid()
  1746. * - proc_pid_lookup acquires proc_lock, and checks that
  1747. * the target is not dead by looking at the attach count
  1748. * of PIDTYPE_PID.
  1749. */
  1750. struct dentry *proc_pid_unhash(struct task_struct *p)
  1751. {
  1752. struct dentry *proc_dentry;
  1753. proc_dentry = p->proc_dentry;
  1754. if (proc_dentry != NULL) {
  1755. spin_lock(&dcache_lock);
  1756. spin_lock(&proc_dentry->d_lock);
  1757. if (!d_unhashed(proc_dentry)) {
  1758. dget_locked(proc_dentry);
  1759. __d_drop(proc_dentry);
  1760. spin_unlock(&proc_dentry->d_lock);
  1761. } else {
  1762. spin_unlock(&proc_dentry->d_lock);
  1763. proc_dentry = NULL;
  1764. }
  1765. spin_unlock(&dcache_lock);
  1766. }
  1767. return proc_dentry;
  1768. }
  1769. /**
  1770. * proc_pid_flush - recover memory used by stale /proc/@pid/x entries
  1771. * @proc_dentry: directoy to prune.
  1772. *
  1773. * Shrink the /proc directory that was used by the just killed thread.
  1774. */
  1775. void proc_pid_flush(struct dentry *proc_dentry)
  1776. {
  1777. might_sleep();
  1778. if(proc_dentry != NULL) {
  1779. shrink_dcache_parent(proc_dentry);
  1780. dput(proc_dentry);
  1781. }
  1782. }
  1783. /* SMP-safe */
  1784. struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
  1785. {
  1786. struct task_struct *task;
  1787. struct inode *inode;
  1788. struct proc_inode *ei;
  1789. unsigned tgid;
  1790. int died;
  1791. if (dentry->d_name.len == 4 && !memcmp(dentry->d_name.name,"self",4)) {
  1792. inode = new_inode(dir->i_sb);
  1793. if (!inode)
  1794. return ERR_PTR(-ENOMEM);
  1795. ei = PROC_I(inode);
  1796. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  1797. inode->i_ino = fake_ino(0, PROC_TGID_INO);
  1798. ei->pde = NULL;
  1799. inode->i_mode = S_IFLNK|S_IRWXUGO;
  1800. inode->i_uid = inode->i_gid = 0;
  1801. inode->i_size = 64;
  1802. inode->i_op = &proc_self_inode_operations;
  1803. d_add(dentry, inode);
  1804. return NULL;
  1805. }
  1806. tgid = name_to_int(dentry);
  1807. if (tgid == ~0U)
  1808. goto out;
  1809. read_lock(&tasklist_lock);
  1810. task = find_task_by_pid(tgid);
  1811. if (task)
  1812. get_task_struct(task);
  1813. read_unlock(&tasklist_lock);
  1814. if (!task)
  1815. goto out;
  1816. inode = proc_pid_make_inode(dir->i_sb, task, PROC_TGID_INO);
  1817. if (!inode) {
  1818. put_task_struct(task);
  1819. goto out;
  1820. }
  1821. inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
  1822. inode->i_op = &proc_tgid_base_inode_operations;
  1823. inode->i_fop = &proc_tgid_base_operations;
  1824. inode->i_flags|=S_IMMUTABLE;
  1825. #ifdef CONFIG_SECURITY
  1826. inode->i_nlink = 5;
  1827. #else
  1828. inode->i_nlink = 4;
  1829. #endif
  1830. dentry->d_op = &pid_base_dentry_operations;
  1831. died = 0;
  1832. d_add(dentry, inode);
  1833. spin_lock(&task->proc_lock);
  1834. task->proc_dentry = dentry;
  1835. if (!pid_alive(task)) {
  1836. dentry = proc_pid_unhash(task);
  1837. died = 1;
  1838. }
  1839. spin_unlock(&task->proc_lock);
  1840. put_task_struct(task);
  1841. if (died) {
  1842. proc_pid_flush(dentry);
  1843. goto out;
  1844. }
  1845. return NULL;
  1846. out:
  1847. return ERR_PTR(-ENOENT);
  1848. }
  1849. /* SMP-safe */
  1850. static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
  1851. {
  1852. struct task_struct *task;
  1853. struct task_struct *leader = proc_task(dir);
  1854. struct inode *inode;
  1855. unsigned tid;
  1856. tid = name_to_int(dentry);
  1857. if (tid == ~0U)
  1858. goto out;
  1859. read_lock(&tasklist_lock);
  1860. task = find_task_by_pid(tid);
  1861. if (task)
  1862. get_task_struct(task);
  1863. read_unlock(&tasklist_lock);
  1864. if (!task)
  1865. goto out;
  1866. if (leader->tgid != task->tgid)
  1867. goto out_drop_task;
  1868. inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_INO);
  1869. if (!inode)
  1870. goto out_drop_task;
  1871. inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
  1872. inode->i_op = &proc_tid_base_inode_operations;
  1873. inode->i_fop = &proc_tid_base_operations;
  1874. inode->i_flags|=S_IMMUTABLE;
  1875. #ifdef CONFIG_SECURITY
  1876. inode->i_nlink = 4;
  1877. #else
  1878. inode->i_nlink = 3;
  1879. #endif
  1880. dentry->d_op = &pid_base_dentry_operations;
  1881. d_add(dentry, inode);
  1882. put_task_struct(task);
  1883. return NULL;
  1884. out_drop_task:
  1885. put_task_struct(task);
  1886. out:
  1887. return ERR_PTR(-ENOENT);
  1888. }
  1889. #define PROC_NUMBUF 10
  1890. #define PROC_MAXPIDS 20
  1891. /*
  1892. * Get a few tgid's to return for filldir - we need to hold the
  1893. * tasklist lock while doing this, and we must release it before
  1894. * we actually do the filldir itself, so we use a temp buffer..
  1895. */
  1896. static int get_tgid_list(int index, unsigned long version, unsigned int *tgids)
  1897. {
  1898. struct task_struct *p;
  1899. int nr_tgids = 0;
  1900. index--;
  1901. read_lock(&tasklist_lock);
  1902. p = NULL;
  1903. if (version) {
  1904. p = find_task_by_pid(version);
  1905. if (p && !thread_group_leader(p))
  1906. p = NULL;
  1907. }
  1908. if (p)
  1909. index = 0;
  1910. else
  1911. p = next_task(&init_task);
  1912. for ( ; p != &init_task; p = next_task(p)) {
  1913. int tgid = p->pid;
  1914. if (!pid_alive(p))
  1915. continue;
  1916. if (--index >= 0)
  1917. continue;
  1918. tgids[nr_tgids] = tgid;
  1919. nr_tgids++;
  1920. if (nr_tgids >= PROC_MAXPIDS)
  1921. break;
  1922. }
  1923. read_unlock(&tasklist_lock);
  1924. return nr_tgids;
  1925. }
  1926. /*
  1927. * Get a few tid's to return for filldir - we need to hold the
  1928. * tasklist lock while doing this, and we must release it before
  1929. * we actually do the filldir itself, so we use a temp buffer..
  1930. */
  1931. static int get_tid_list(int index, unsigned int *tids, struct inode *dir)
  1932. {
  1933. struct task_struct *leader_task = proc_task(dir);
  1934. struct task_struct *task = leader_task;
  1935. int nr_tids = 0;
  1936. index -= 2;
  1937. read_lock(&tasklist_lock);
  1938. /*
  1939. * The starting point task (leader_task) might be an already
  1940. * unlinked task, which cannot be used to access the task-list
  1941. * via next_thread().
  1942. */
  1943. if (pid_alive(task)) do {
  1944. int tid = task->pid;
  1945. if (--index >= 0)
  1946. continue;
  1947. if (tids != NULL)
  1948. tids[nr_tids] = tid;
  1949. nr_tids++;
  1950. if (nr_tids >= PROC_MAXPIDS)
  1951. break;
  1952. } while ((task = next_thread(task)) != leader_task);
  1953. read_unlock(&tasklist_lock);
  1954. return nr_tids;
  1955. }
  1956. /* for the /proc/ directory itself, after non-process stuff has been done */
  1957. int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
  1958. {
  1959. unsigned int tgid_array[PROC_MAXPIDS];
  1960. char buf[PROC_NUMBUF];
  1961. unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
  1962. unsigned int nr_tgids, i;
  1963. int next_tgid;
  1964. if (!nr) {
  1965. ino_t ino = fake_ino(0,PROC_TGID_INO);
  1966. if (filldir(dirent, "self", 4, filp->f_pos, ino, DT_LNK) < 0)
  1967. return 0;
  1968. filp->f_pos++;
  1969. nr++;
  1970. }
  1971. /* f_version caches the tgid value that the last readdir call couldn't
  1972. * return. lseek aka telldir automagically resets f_version to 0.
  1973. */
  1974. next_tgid = filp->f_version;
  1975. filp->f_version = 0;
  1976. for (;;) {
  1977. nr_tgids = get_tgid_list(nr, next_tgid, tgid_array);
  1978. if (!nr_tgids) {
  1979. /* no more entries ! */
  1980. break;
  1981. }
  1982. next_tgid = 0;
  1983. /* do not use the last found pid, reserve it for next_tgid */
  1984. if (nr_tgids == PROC_MAXPIDS) {
  1985. nr_tgids--;
  1986. next_tgid = tgid_array[nr_tgids];
  1987. }
  1988. for (i=0;i<nr_tgids;i++) {
  1989. int tgid = tgid_array[i];
  1990. ino_t ino = fake_ino(tgid,PROC_TGID_INO);
  1991. unsigned long j = PROC_NUMBUF;
  1992. do
  1993. buf[--j] = '0' + (tgid % 10);
  1994. while ((tgid /= 10) != 0);
  1995. if (filldir(dirent, buf+j, PROC_NUMBUF-j, filp->f_pos, ino, DT_DIR) < 0) {
  1996. /* returning this tgid failed, save it as the first
  1997. * pid for the next readir call */
  1998. filp->f_version = tgid_array[i];
  1999. goto out;
  2000. }
  2001. filp->f_pos++;
  2002. nr++;
  2003. }
  2004. }
  2005. out:
  2006. return 0;
  2007. }
  2008. /* for the /proc/TGID/task/ directories */
  2009. static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
  2010. {
  2011. unsigned int tid_array[PROC_MAXPIDS];
  2012. char buf[PROC_NUMBUF];
  2013. unsigned int nr_tids, i;
  2014. struct dentry *dentry = filp->f_dentry;
  2015. struct inode *inode = dentry->d_inode;
  2016. int retval = -ENOENT;
  2017. ino_t ino;
  2018. unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
  2019. if (!pid_alive(proc_task(inode)))
  2020. goto out;
  2021. retval = 0;
  2022. switch (pos) {
  2023. case 0:
  2024. ino = inode->i_ino;
  2025. if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
  2026. goto out;
  2027. pos++;
  2028. /* fall through */
  2029. case 1:
  2030. ino = parent_ino(dentry);
  2031. if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
  2032. goto out;
  2033. pos++;
  2034. /* fall through */
  2035. }
  2036. nr_tids = get_tid_list(pos, tid_array, inode);
  2037. inode->i_nlink = pos + nr_tids;
  2038. for (i = 0; i < nr_tids; i++) {
  2039. unsigned long j = PROC_NUMBUF;
  2040. int tid = tid_array[i];
  2041. ino = fake_ino(tid,PROC_TID_INO);
  2042. do
  2043. buf[--j] = '0' + (tid % 10);
  2044. while ((tid /= 10) != 0);
  2045. if (filldir(dirent, buf+j, PROC_NUMBUF-j, pos, ino, DT_DIR) < 0)
  2046. break;
  2047. pos++;
  2048. }
  2049. out:
  2050. filp->f_pos = pos;
  2051. return retval;
  2052. }