base.c 52 KB

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