base.c 51 KB

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