base.c 48 KB

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