base.c 54 KB

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