base.c 48 KB

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