base.c 52 KB

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