base.c 56 KB

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