base.c 54 KB

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