base.c 51 KB

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