base.c 53 KB

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