base.c 52 KB

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