base.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363
  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. extern struct seq_operations mounts_op;
  515. struct proc_mounts {
  516. struct seq_file m;
  517. int event;
  518. };
  519. static int mounts_open(struct inode *inode, struct file *file)
  520. {
  521. struct task_struct *task = get_proc_task(inode);
  522. struct namespace *namespace = NULL;
  523. struct proc_mounts *p;
  524. int ret = -EINVAL;
  525. if (task) {
  526. task_lock(task);
  527. namespace = task->namespace;
  528. if (namespace)
  529. get_namespace(namespace);
  530. task_unlock(task);
  531. put_task_struct(task);
  532. }
  533. if (namespace) {
  534. ret = -ENOMEM;
  535. p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
  536. if (p) {
  537. file->private_data = &p->m;
  538. ret = seq_open(file, &mounts_op);
  539. if (!ret) {
  540. p->m.private = namespace;
  541. p->event = namespace->event;
  542. return 0;
  543. }
  544. kfree(p);
  545. }
  546. put_namespace(namespace);
  547. }
  548. return ret;
  549. }
  550. static int mounts_release(struct inode *inode, struct file *file)
  551. {
  552. struct seq_file *m = file->private_data;
  553. struct namespace *namespace = m->private;
  554. put_namespace(namespace);
  555. return seq_release(inode, file);
  556. }
  557. static unsigned mounts_poll(struct file *file, poll_table *wait)
  558. {
  559. struct proc_mounts *p = file->private_data;
  560. struct namespace *ns = p->m.private;
  561. unsigned res = 0;
  562. poll_wait(file, &ns->poll, wait);
  563. spin_lock(&vfsmount_lock);
  564. if (p->event != ns->event) {
  565. p->event = ns->event;
  566. res = POLLERR;
  567. }
  568. spin_unlock(&vfsmount_lock);
  569. return res;
  570. }
  571. static struct file_operations proc_mounts_operations = {
  572. .open = mounts_open,
  573. .read = seq_read,
  574. .llseek = seq_lseek,
  575. .release = mounts_release,
  576. .poll = mounts_poll,
  577. };
  578. extern struct seq_operations mountstats_op;
  579. static int mountstats_open(struct inode *inode, struct file *file)
  580. {
  581. int ret = seq_open(file, &mountstats_op);
  582. if (!ret) {
  583. struct seq_file *m = file->private_data;
  584. struct namespace *namespace = NULL;
  585. struct task_struct *task = get_proc_task(inode);
  586. if (task) {
  587. task_lock(task);
  588. namespace = task->namespace;
  589. if (namespace)
  590. get_namespace(namespace);
  591. task_unlock(task);
  592. put_task_struct(task);
  593. }
  594. if (namespace)
  595. m->private = namespace;
  596. else {
  597. seq_release(inode, file);
  598. ret = -EINVAL;
  599. }
  600. }
  601. return ret;
  602. }
  603. static struct file_operations proc_mountstats_operations = {
  604. .open = mountstats_open,
  605. .read = seq_read,
  606. .llseek = seq_lseek,
  607. .release = mounts_release,
  608. };
  609. #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
  610. static ssize_t proc_info_read(struct file * file, char __user * buf,
  611. size_t count, loff_t *ppos)
  612. {
  613. struct inode * inode = file->f_dentry->d_inode;
  614. unsigned long page;
  615. ssize_t length;
  616. struct task_struct *task = get_proc_task(inode);
  617. length = -ESRCH;
  618. if (!task)
  619. goto out_no_task;
  620. if (count > PROC_BLOCK_SIZE)
  621. count = PROC_BLOCK_SIZE;
  622. length = -ENOMEM;
  623. if (!(page = __get_free_page(GFP_KERNEL)))
  624. goto out;
  625. length = PROC_I(inode)->op.proc_read(task, (char*)page);
  626. if (length >= 0)
  627. length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
  628. free_page(page);
  629. out:
  630. put_task_struct(task);
  631. out_no_task:
  632. return length;
  633. }
  634. static struct file_operations proc_info_file_operations = {
  635. .read = proc_info_read,
  636. };
  637. static int mem_open(struct inode* inode, struct file* file)
  638. {
  639. file->private_data = (void*)((long)current->self_exec_id);
  640. return 0;
  641. }
  642. static ssize_t mem_read(struct file * file, char __user * buf,
  643. size_t count, loff_t *ppos)
  644. {
  645. struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
  646. char *page;
  647. unsigned long src = *ppos;
  648. int ret = -ESRCH;
  649. struct mm_struct *mm;
  650. if (!task)
  651. goto out_no_task;
  652. if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
  653. goto out;
  654. ret = -ENOMEM;
  655. page = (char *)__get_free_page(GFP_USER);
  656. if (!page)
  657. goto out;
  658. ret = 0;
  659. mm = get_task_mm(task);
  660. if (!mm)
  661. goto out_free;
  662. ret = -EIO;
  663. if (file->private_data != (void*)((long)current->self_exec_id))
  664. goto out_put;
  665. ret = 0;
  666. while (count > 0) {
  667. int this_len, retval;
  668. this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
  669. retval = access_process_vm(task, src, page, this_len, 0);
  670. if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
  671. if (!ret)
  672. ret = -EIO;
  673. break;
  674. }
  675. if (copy_to_user(buf, page, retval)) {
  676. ret = -EFAULT;
  677. break;
  678. }
  679. ret += retval;
  680. src += retval;
  681. buf += retval;
  682. count -= retval;
  683. }
  684. *ppos = src;
  685. out_put:
  686. mmput(mm);
  687. out_free:
  688. free_page((unsigned long) page);
  689. out:
  690. put_task_struct(task);
  691. out_no_task:
  692. return ret;
  693. }
  694. #define mem_write NULL
  695. #ifndef mem_write
  696. /* This is a security hazard */
  697. static ssize_t mem_write(struct file * file, const char * buf,
  698. size_t count, loff_t *ppos)
  699. {
  700. int copied = 0;
  701. char *page;
  702. struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
  703. unsigned long dst = *ppos;
  704. copied = -ESRCH;
  705. if (!task)
  706. goto out_no_task;
  707. if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
  708. goto out;
  709. copied = -ENOMEM;
  710. page = (char *)__get_free_page(GFP_USER);
  711. if (!page)
  712. goto out;
  713. while (count > 0) {
  714. int this_len, retval;
  715. this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
  716. if (copy_from_user(page, buf, this_len)) {
  717. copied = -EFAULT;
  718. break;
  719. }
  720. retval = access_process_vm(task, dst, page, this_len, 1);
  721. if (!retval) {
  722. if (!copied)
  723. copied = -EIO;
  724. break;
  725. }
  726. copied += retval;
  727. buf += retval;
  728. dst += retval;
  729. count -= retval;
  730. }
  731. *ppos = dst;
  732. free_page((unsigned long) page);
  733. out:
  734. put_task_struct(task);
  735. out_no_task:
  736. return copied;
  737. }
  738. #endif
  739. static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
  740. {
  741. switch (orig) {
  742. case 0:
  743. file->f_pos = offset;
  744. break;
  745. case 1:
  746. file->f_pos += offset;
  747. break;
  748. default:
  749. return -EINVAL;
  750. }
  751. force_successful_syscall_return();
  752. return file->f_pos;
  753. }
  754. static struct file_operations proc_mem_operations = {
  755. .llseek = mem_lseek,
  756. .read = mem_read,
  757. .write = mem_write,
  758. .open = mem_open,
  759. };
  760. static ssize_t oom_adjust_read(struct file *file, char __user *buf,
  761. size_t count, loff_t *ppos)
  762. {
  763. struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
  764. char buffer[PROC_NUMBUF];
  765. size_t len;
  766. int oom_adjust;
  767. loff_t __ppos = *ppos;
  768. if (!task)
  769. return -ESRCH;
  770. oom_adjust = task->oomkilladj;
  771. put_task_struct(task);
  772. len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
  773. if (__ppos >= len)
  774. return 0;
  775. if (count > len-__ppos)
  776. count = len-__ppos;
  777. if (copy_to_user(buf, buffer + __ppos, count))
  778. return -EFAULT;
  779. *ppos = __ppos + count;
  780. return count;
  781. }
  782. static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
  783. size_t count, loff_t *ppos)
  784. {
  785. struct task_struct *task;
  786. char buffer[PROC_NUMBUF], *end;
  787. int oom_adjust;
  788. if (!capable(CAP_SYS_RESOURCE))
  789. return -EPERM;
  790. memset(buffer, 0, sizeof(buffer));
  791. if (count > sizeof(buffer) - 1)
  792. count = sizeof(buffer) - 1;
  793. if (copy_from_user(buffer, buf, count))
  794. return -EFAULT;
  795. oom_adjust = simple_strtol(buffer, &end, 0);
  796. if ((oom_adjust < -16 || oom_adjust > 15) && oom_adjust != OOM_DISABLE)
  797. return -EINVAL;
  798. if (*end == '\n')
  799. end++;
  800. task = get_proc_task(file->f_dentry->d_inode);
  801. if (!task)
  802. return -ESRCH;
  803. task->oomkilladj = oom_adjust;
  804. put_task_struct(task);
  805. if (end - buffer == 0)
  806. return -EIO;
  807. return end - buffer;
  808. }
  809. static struct file_operations proc_oom_adjust_operations = {
  810. .read = oom_adjust_read,
  811. .write = oom_adjust_write,
  812. };
  813. #ifdef CONFIG_AUDITSYSCALL
  814. #define TMPBUFLEN 21
  815. static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
  816. size_t count, loff_t *ppos)
  817. {
  818. struct inode * inode = file->f_dentry->d_inode;
  819. struct task_struct *task = get_proc_task(inode);
  820. ssize_t length;
  821. char tmpbuf[TMPBUFLEN];
  822. if (!task)
  823. return -ESRCH;
  824. length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
  825. audit_get_loginuid(task->audit_context));
  826. put_task_struct(task);
  827. return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
  828. }
  829. static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
  830. size_t count, loff_t *ppos)
  831. {
  832. struct inode * inode = file->f_dentry->d_inode;
  833. char *page, *tmp;
  834. ssize_t length;
  835. uid_t loginuid;
  836. if (!capable(CAP_AUDIT_CONTROL))
  837. return -EPERM;
  838. if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
  839. return -EPERM;
  840. if (count >= PAGE_SIZE)
  841. count = PAGE_SIZE - 1;
  842. if (*ppos != 0) {
  843. /* No partial writes. */
  844. return -EINVAL;
  845. }
  846. page = (char*)__get_free_page(GFP_USER);
  847. if (!page)
  848. return -ENOMEM;
  849. length = -EFAULT;
  850. if (copy_from_user(page, buf, count))
  851. goto out_free_page;
  852. page[count] = '\0';
  853. loginuid = simple_strtoul(page, &tmp, 10);
  854. if (tmp == page) {
  855. length = -EINVAL;
  856. goto out_free_page;
  857. }
  858. length = audit_set_loginuid(current, loginuid);
  859. if (likely(length == 0))
  860. length = count;
  861. out_free_page:
  862. free_page((unsigned long) page);
  863. return length;
  864. }
  865. static struct file_operations proc_loginuid_operations = {
  866. .read = proc_loginuid_read,
  867. .write = proc_loginuid_write,
  868. };
  869. #endif
  870. #ifdef CONFIG_SECCOMP
  871. static ssize_t seccomp_read(struct file *file, char __user *buf,
  872. size_t count, loff_t *ppos)
  873. {
  874. struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
  875. char __buf[20];
  876. loff_t __ppos = *ppos;
  877. size_t len;
  878. if (!tsk)
  879. return -ESRCH;
  880. /* no need to print the trailing zero, so use only len */
  881. len = sprintf(__buf, "%u\n", tsk->seccomp.mode);
  882. put_task_struct(tsk);
  883. if (__ppos >= len)
  884. return 0;
  885. if (count > len - __ppos)
  886. count = len - __ppos;
  887. if (copy_to_user(buf, __buf + __ppos, count))
  888. return -EFAULT;
  889. *ppos = __ppos + count;
  890. return count;
  891. }
  892. static ssize_t seccomp_write(struct file *file, const char __user *buf,
  893. size_t count, loff_t *ppos)
  894. {
  895. struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
  896. char __buf[20], *end;
  897. unsigned int seccomp_mode;
  898. ssize_t result;
  899. result = -ESRCH;
  900. if (!tsk)
  901. goto out_no_task;
  902. /* can set it only once to be even more secure */
  903. result = -EPERM;
  904. if (unlikely(tsk->seccomp.mode))
  905. goto out;
  906. result = -EFAULT;
  907. memset(__buf, 0, sizeof(__buf));
  908. count = min(count, sizeof(__buf) - 1);
  909. if (copy_from_user(__buf, buf, count))
  910. goto out;
  911. seccomp_mode = simple_strtoul(__buf, &end, 0);
  912. if (*end == '\n')
  913. end++;
  914. result = -EINVAL;
  915. if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) {
  916. tsk->seccomp.mode = seccomp_mode;
  917. set_tsk_thread_flag(tsk, TIF_SECCOMP);
  918. } else
  919. goto out;
  920. result = -EIO;
  921. if (unlikely(!(end - __buf)))
  922. goto out;
  923. result = end - __buf;
  924. out:
  925. put_task_struct(tsk);
  926. out_no_task:
  927. return result;
  928. }
  929. static struct file_operations proc_seccomp_operations = {
  930. .read = seccomp_read,
  931. .write = seccomp_write,
  932. };
  933. #endif /* CONFIG_SECCOMP */
  934. static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
  935. {
  936. struct inode *inode = dentry->d_inode;
  937. int error = -EACCES;
  938. /* We don't need a base pointer in the /proc filesystem */
  939. path_release(nd);
  940. /* Are we allowed to snoop on the tasks file descriptors? */
  941. if (!proc_fd_access_allowed(inode))
  942. goto out;
  943. error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
  944. nd->last_type = LAST_BIND;
  945. out:
  946. return ERR_PTR(error);
  947. }
  948. static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
  949. char __user *buffer, int buflen)
  950. {
  951. struct inode * inode;
  952. char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
  953. int len;
  954. if (!tmp)
  955. return -ENOMEM;
  956. inode = dentry->d_inode;
  957. path = d_path(dentry, mnt, tmp, PAGE_SIZE);
  958. len = PTR_ERR(path);
  959. if (IS_ERR(path))
  960. goto out;
  961. len = tmp + PAGE_SIZE - 1 - path;
  962. if (len > buflen)
  963. len = buflen;
  964. if (copy_to_user(buffer, path, len))
  965. len = -EFAULT;
  966. out:
  967. free_page((unsigned long)tmp);
  968. return len;
  969. }
  970. static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
  971. {
  972. int error = -EACCES;
  973. struct inode *inode = dentry->d_inode;
  974. struct dentry *de;
  975. struct vfsmount *mnt = NULL;
  976. /* Are we allowed to snoop on the tasks file descriptors? */
  977. if (!proc_fd_access_allowed(inode))
  978. goto out;
  979. error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
  980. if (error)
  981. goto out;
  982. error = do_proc_readlink(de, mnt, buffer, buflen);
  983. dput(de);
  984. mntput(mnt);
  985. out:
  986. return error;
  987. }
  988. static struct inode_operations proc_pid_link_inode_operations = {
  989. .readlink = proc_pid_readlink,
  990. .follow_link = proc_pid_follow_link
  991. };
  992. static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
  993. {
  994. struct dentry *dentry = filp->f_dentry;
  995. struct inode *inode = dentry->d_inode;
  996. struct task_struct *p = get_proc_task(inode);
  997. unsigned int fd, tid, ino;
  998. int retval;
  999. char buf[PROC_NUMBUF];
  1000. struct files_struct * files;
  1001. struct fdtable *fdt;
  1002. retval = -ENOENT;
  1003. if (!p)
  1004. goto out_no_task;
  1005. retval = 0;
  1006. tid = p->pid;
  1007. fd = filp->f_pos;
  1008. switch (fd) {
  1009. case 0:
  1010. if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
  1011. goto out;
  1012. filp->f_pos++;
  1013. case 1:
  1014. ino = parent_ino(dentry);
  1015. if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
  1016. goto out;
  1017. filp->f_pos++;
  1018. default:
  1019. files = get_files_struct(p);
  1020. if (!files)
  1021. goto out;
  1022. rcu_read_lock();
  1023. fdt = files_fdtable(files);
  1024. for (fd = filp->f_pos-2;
  1025. fd < fdt->max_fds;
  1026. fd++, filp->f_pos++) {
  1027. unsigned int i,j;
  1028. if (!fcheck_files(files, fd))
  1029. continue;
  1030. rcu_read_unlock();
  1031. j = PROC_NUMBUF;
  1032. i = fd;
  1033. do {
  1034. j--;
  1035. buf[j] = '0' + (i % 10);
  1036. i /= 10;
  1037. } while (i);
  1038. ino = fake_ino(tid, PROC_TID_FD_DIR + fd);
  1039. if (filldir(dirent, buf+j, PROC_NUMBUF-j, fd+2, ino, DT_LNK) < 0) {
  1040. rcu_read_lock();
  1041. break;
  1042. }
  1043. rcu_read_lock();
  1044. }
  1045. rcu_read_unlock();
  1046. put_files_struct(files);
  1047. }
  1048. out:
  1049. put_task_struct(p);
  1050. out_no_task:
  1051. return retval;
  1052. }
  1053. static int proc_pident_readdir(struct file *filp,
  1054. void *dirent, filldir_t filldir,
  1055. struct pid_entry *ents, unsigned int nents)
  1056. {
  1057. int i;
  1058. int pid;
  1059. struct dentry *dentry = filp->f_dentry;
  1060. struct inode *inode = dentry->d_inode;
  1061. struct task_struct *task = get_proc_task(inode);
  1062. struct pid_entry *p;
  1063. ino_t ino;
  1064. int ret;
  1065. ret = -ENOENT;
  1066. if (!task)
  1067. goto out;
  1068. ret = 0;
  1069. pid = task->pid;
  1070. put_task_struct(task);
  1071. i = filp->f_pos;
  1072. switch (i) {
  1073. case 0:
  1074. ino = inode->i_ino;
  1075. if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
  1076. goto out;
  1077. i++;
  1078. filp->f_pos++;
  1079. /* fall through */
  1080. case 1:
  1081. ino = parent_ino(dentry);
  1082. if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
  1083. goto out;
  1084. i++;
  1085. filp->f_pos++;
  1086. /* fall through */
  1087. default:
  1088. i -= 2;
  1089. if (i >= nents) {
  1090. ret = 1;
  1091. goto out;
  1092. }
  1093. p = ents + i;
  1094. while (p->name) {
  1095. if (filldir(dirent, p->name, p->len, filp->f_pos,
  1096. fake_ino(pid, p->type), p->mode >> 12) < 0)
  1097. goto out;
  1098. filp->f_pos++;
  1099. p++;
  1100. }
  1101. }
  1102. ret = 1;
  1103. out:
  1104. return ret;
  1105. }
  1106. static int proc_tgid_base_readdir(struct file * filp,
  1107. void * dirent, filldir_t filldir)
  1108. {
  1109. return proc_pident_readdir(filp,dirent,filldir,
  1110. tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
  1111. }
  1112. static int proc_tid_base_readdir(struct file * filp,
  1113. void * dirent, filldir_t filldir)
  1114. {
  1115. return proc_pident_readdir(filp,dirent,filldir,
  1116. tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
  1117. }
  1118. /* building an inode */
  1119. static int task_dumpable(struct task_struct *task)
  1120. {
  1121. int dumpable = 0;
  1122. struct mm_struct *mm;
  1123. task_lock(task);
  1124. mm = task->mm;
  1125. if (mm)
  1126. dumpable = mm->dumpable;
  1127. task_unlock(task);
  1128. if(dumpable == 1)
  1129. return 1;
  1130. return 0;
  1131. }
  1132. static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task, int ino)
  1133. {
  1134. struct inode * inode;
  1135. struct proc_inode *ei;
  1136. /* We need a new inode */
  1137. inode = new_inode(sb);
  1138. if (!inode)
  1139. goto out;
  1140. /* Common stuff */
  1141. ei = PROC_I(inode);
  1142. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  1143. inode->i_ino = fake_ino(task->pid, ino);
  1144. /*
  1145. * grab the reference to task.
  1146. */
  1147. ei->pid = get_pid(task->pids[PIDTYPE_PID].pid);
  1148. if (!ei->pid)
  1149. goto out_unlock;
  1150. inode->i_uid = 0;
  1151. inode->i_gid = 0;
  1152. if (task_dumpable(task)) {
  1153. inode->i_uid = task->euid;
  1154. inode->i_gid = task->egid;
  1155. }
  1156. security_task_to_inode(task, inode);
  1157. out:
  1158. return inode;
  1159. out_unlock:
  1160. iput(inode);
  1161. return NULL;
  1162. }
  1163. /* dentry stuff */
  1164. /*
  1165. * Exceptional case: normally we are not allowed to unhash a busy
  1166. * directory. In this case, however, we can do it - no aliasing problems
  1167. * due to the way we treat inodes.
  1168. *
  1169. * Rewrite the inode's ownerships here because the owning task may have
  1170. * performed a setuid(), etc.
  1171. *
  1172. * Before the /proc/pid/status file was created the only way to read
  1173. * the effective uid of a /process was to stat /proc/pid. Reading
  1174. * /proc/pid/status is slow enough that procps and other packages
  1175. * kept stating /proc/pid. To keep the rules in /proc simple I have
  1176. * made this apply to all per process world readable and executable
  1177. * directories.
  1178. */
  1179. static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
  1180. {
  1181. struct inode *inode = dentry->d_inode;
  1182. struct task_struct *task = get_proc_task(inode);
  1183. if (task) {
  1184. if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
  1185. task_dumpable(task)) {
  1186. inode->i_uid = task->euid;
  1187. inode->i_gid = task->egid;
  1188. } else {
  1189. inode->i_uid = 0;
  1190. inode->i_gid = 0;
  1191. inode->i_mode = 0;
  1192. }
  1193. security_task_to_inode(task, inode);
  1194. put_task_struct(task);
  1195. return 1;
  1196. }
  1197. d_drop(dentry);
  1198. return 0;
  1199. }
  1200. static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
  1201. {
  1202. struct inode *inode = dentry->d_inode;
  1203. struct task_struct *task;
  1204. generic_fillattr(inode, stat);
  1205. rcu_read_lock();
  1206. stat->uid = 0;
  1207. stat->gid = 0;
  1208. task = pid_task(proc_pid(inode), PIDTYPE_PID);
  1209. if (task) {
  1210. if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
  1211. task_dumpable(task)) {
  1212. stat->uid = task->euid;
  1213. stat->gid = task->egid;
  1214. }
  1215. }
  1216. rcu_read_unlock();
  1217. return 0;
  1218. }
  1219. static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
  1220. {
  1221. struct inode *inode = dentry->d_inode;
  1222. struct task_struct *task = get_proc_task(inode);
  1223. int fd = proc_fd(inode);
  1224. struct files_struct *files;
  1225. if (task) {
  1226. files = get_files_struct(task);
  1227. if (files) {
  1228. rcu_read_lock();
  1229. if (fcheck_files(files, fd)) {
  1230. rcu_read_unlock();
  1231. put_files_struct(files);
  1232. if (task_dumpable(task)) {
  1233. inode->i_uid = task->euid;
  1234. inode->i_gid = task->egid;
  1235. } else {
  1236. inode->i_uid = 0;
  1237. inode->i_gid = 0;
  1238. }
  1239. security_task_to_inode(task, inode);
  1240. put_task_struct(task);
  1241. return 1;
  1242. }
  1243. rcu_read_unlock();
  1244. put_files_struct(files);
  1245. }
  1246. put_task_struct(task);
  1247. }
  1248. d_drop(dentry);
  1249. return 0;
  1250. }
  1251. static int pid_delete_dentry(struct dentry * dentry)
  1252. {
  1253. /* Is the task we represent dead?
  1254. * If so, then don't put the dentry on the lru list,
  1255. * kill it immediately.
  1256. */
  1257. return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
  1258. }
  1259. static struct dentry_operations tid_fd_dentry_operations =
  1260. {
  1261. .d_revalidate = tid_fd_revalidate,
  1262. .d_delete = pid_delete_dentry,
  1263. };
  1264. static struct dentry_operations pid_dentry_operations =
  1265. {
  1266. .d_revalidate = pid_revalidate,
  1267. .d_delete = pid_delete_dentry,
  1268. };
  1269. /* Lookups */
  1270. static unsigned name_to_int(struct dentry *dentry)
  1271. {
  1272. const char *name = dentry->d_name.name;
  1273. int len = dentry->d_name.len;
  1274. unsigned n = 0;
  1275. if (len > 1 && *name == '0')
  1276. goto out;
  1277. while (len-- > 0) {
  1278. unsigned c = *name++ - '0';
  1279. if (c > 9)
  1280. goto out;
  1281. if (n >= (~0U-9)/10)
  1282. goto out;
  1283. n *= 10;
  1284. n += c;
  1285. }
  1286. return n;
  1287. out:
  1288. return ~0U;
  1289. }
  1290. /* SMP-safe */
  1291. static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
  1292. {
  1293. struct task_struct *task = get_proc_task(dir);
  1294. unsigned fd = name_to_int(dentry);
  1295. struct dentry *result = ERR_PTR(-ENOENT);
  1296. struct file * file;
  1297. struct files_struct * files;
  1298. struct inode *inode;
  1299. struct proc_inode *ei;
  1300. if (!task)
  1301. goto out_no_task;
  1302. if (fd == ~0U)
  1303. goto out;
  1304. inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_FD_DIR+fd);
  1305. if (!inode)
  1306. goto out;
  1307. ei = PROC_I(inode);
  1308. ei->fd = fd;
  1309. files = get_files_struct(task);
  1310. if (!files)
  1311. goto out_unlock;
  1312. inode->i_mode = S_IFLNK;
  1313. /*
  1314. * We are not taking a ref to the file structure, so we must
  1315. * hold ->file_lock.
  1316. */
  1317. spin_lock(&files->file_lock);
  1318. file = fcheck_files(files, fd);
  1319. if (!file)
  1320. goto out_unlock2;
  1321. if (file->f_mode & 1)
  1322. inode->i_mode |= S_IRUSR | S_IXUSR;
  1323. if (file->f_mode & 2)
  1324. inode->i_mode |= S_IWUSR | S_IXUSR;
  1325. spin_unlock(&files->file_lock);
  1326. put_files_struct(files);
  1327. inode->i_op = &proc_pid_link_inode_operations;
  1328. inode->i_size = 64;
  1329. ei->op.proc_get_link = proc_fd_link;
  1330. dentry->d_op = &tid_fd_dentry_operations;
  1331. d_add(dentry, inode);
  1332. /* Close the race of the process dying before we return the dentry */
  1333. if (tid_fd_revalidate(dentry, NULL))
  1334. result = NULL;
  1335. out:
  1336. put_task_struct(task);
  1337. out_no_task:
  1338. return result;
  1339. out_unlock2:
  1340. spin_unlock(&files->file_lock);
  1341. put_files_struct(files);
  1342. out_unlock:
  1343. iput(inode);
  1344. goto out;
  1345. }
  1346. static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir);
  1347. static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd);
  1348. static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat);
  1349. static struct file_operations proc_fd_operations = {
  1350. .read = generic_read_dir,
  1351. .readdir = proc_readfd,
  1352. };
  1353. static struct file_operations proc_task_operations = {
  1354. .read = generic_read_dir,
  1355. .readdir = proc_task_readdir,
  1356. };
  1357. /*
  1358. * proc directories can do almost nothing..
  1359. */
  1360. static struct inode_operations proc_fd_inode_operations = {
  1361. .lookup = proc_lookupfd,
  1362. };
  1363. static struct inode_operations proc_task_inode_operations = {
  1364. .lookup = proc_task_lookup,
  1365. .getattr = proc_task_getattr,
  1366. };
  1367. #ifdef CONFIG_SECURITY
  1368. static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
  1369. size_t count, loff_t *ppos)
  1370. {
  1371. struct inode * inode = file->f_dentry->d_inode;
  1372. unsigned long page;
  1373. ssize_t length;
  1374. struct task_struct *task = get_proc_task(inode);
  1375. length = -ESRCH;
  1376. if (!task)
  1377. goto out_no_task;
  1378. if (count > PAGE_SIZE)
  1379. count = PAGE_SIZE;
  1380. length = -ENOMEM;
  1381. if (!(page = __get_free_page(GFP_KERNEL)))
  1382. goto out;
  1383. length = security_getprocattr(task,
  1384. (char*)file->f_dentry->d_name.name,
  1385. (void*)page, count);
  1386. if (length >= 0)
  1387. length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
  1388. free_page(page);
  1389. out:
  1390. put_task_struct(task);
  1391. out_no_task:
  1392. return length;
  1393. }
  1394. static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
  1395. size_t count, loff_t *ppos)
  1396. {
  1397. struct inode * inode = file->f_dentry->d_inode;
  1398. char *page;
  1399. ssize_t length;
  1400. struct task_struct *task = get_proc_task(inode);
  1401. length = -ESRCH;
  1402. if (!task)
  1403. goto out_no_task;
  1404. if (count > PAGE_SIZE)
  1405. count = PAGE_SIZE;
  1406. /* No partial writes. */
  1407. length = -EINVAL;
  1408. if (*ppos != 0)
  1409. goto out;
  1410. length = -ENOMEM;
  1411. page = (char*)__get_free_page(GFP_USER);
  1412. if (!page)
  1413. goto out;
  1414. length = -EFAULT;
  1415. if (copy_from_user(page, buf, count))
  1416. goto out_free;
  1417. length = security_setprocattr(task,
  1418. (char*)file->f_dentry->d_name.name,
  1419. (void*)page, count);
  1420. out_free:
  1421. free_page((unsigned long) page);
  1422. out:
  1423. put_task_struct(task);
  1424. out_no_task:
  1425. return length;
  1426. }
  1427. static struct file_operations proc_pid_attr_operations = {
  1428. .read = proc_pid_attr_read,
  1429. .write = proc_pid_attr_write,
  1430. };
  1431. static struct file_operations proc_tid_attr_operations;
  1432. static struct inode_operations proc_tid_attr_inode_operations;
  1433. static struct file_operations proc_tgid_attr_operations;
  1434. static struct inode_operations proc_tgid_attr_inode_operations;
  1435. #endif
  1436. /* SMP-safe */
  1437. static struct dentry *proc_pident_lookup(struct inode *dir,
  1438. struct dentry *dentry,
  1439. struct pid_entry *ents)
  1440. {
  1441. struct inode *inode;
  1442. struct dentry *error;
  1443. struct task_struct *task = get_proc_task(dir);
  1444. struct pid_entry *p;
  1445. struct proc_inode *ei;
  1446. error = ERR_PTR(-ENOENT);
  1447. inode = NULL;
  1448. if (!task)
  1449. goto out_no_task;
  1450. for (p = ents; p->name; p++) {
  1451. if (p->len != dentry->d_name.len)
  1452. continue;
  1453. if (!memcmp(dentry->d_name.name, p->name, p->len))
  1454. break;
  1455. }
  1456. if (!p->name)
  1457. goto out;
  1458. error = ERR_PTR(-EINVAL);
  1459. inode = proc_pid_make_inode(dir->i_sb, task, p->type);
  1460. if (!inode)
  1461. goto out;
  1462. ei = PROC_I(inode);
  1463. inode->i_mode = p->mode;
  1464. /*
  1465. * Yes, it does not scale. And it should not. Don't add
  1466. * new entries into /proc/<tgid>/ without very good reasons.
  1467. */
  1468. switch(p->type) {
  1469. case PROC_TGID_TASK:
  1470. inode->i_nlink = 2;
  1471. inode->i_op = &proc_task_inode_operations;
  1472. inode->i_fop = &proc_task_operations;
  1473. break;
  1474. case PROC_TID_FD:
  1475. case PROC_TGID_FD:
  1476. inode->i_nlink = 2;
  1477. inode->i_op = &proc_fd_inode_operations;
  1478. inode->i_fop = &proc_fd_operations;
  1479. break;
  1480. case PROC_TID_EXE:
  1481. case PROC_TGID_EXE:
  1482. inode->i_op = &proc_pid_link_inode_operations;
  1483. ei->op.proc_get_link = proc_exe_link;
  1484. break;
  1485. case PROC_TID_CWD:
  1486. case PROC_TGID_CWD:
  1487. inode->i_op = &proc_pid_link_inode_operations;
  1488. ei->op.proc_get_link = proc_cwd_link;
  1489. break;
  1490. case PROC_TID_ROOT:
  1491. case PROC_TGID_ROOT:
  1492. inode->i_op = &proc_pid_link_inode_operations;
  1493. ei->op.proc_get_link = proc_root_link;
  1494. break;
  1495. case PROC_TID_ENVIRON:
  1496. case PROC_TGID_ENVIRON:
  1497. inode->i_fop = &proc_info_file_operations;
  1498. ei->op.proc_read = proc_pid_environ;
  1499. break;
  1500. case PROC_TID_AUXV:
  1501. case PROC_TGID_AUXV:
  1502. inode->i_fop = &proc_info_file_operations;
  1503. ei->op.proc_read = proc_pid_auxv;
  1504. break;
  1505. case PROC_TID_STATUS:
  1506. case PROC_TGID_STATUS:
  1507. inode->i_fop = &proc_info_file_operations;
  1508. ei->op.proc_read = proc_pid_status;
  1509. break;
  1510. case PROC_TID_STAT:
  1511. inode->i_fop = &proc_info_file_operations;
  1512. ei->op.proc_read = proc_tid_stat;
  1513. break;
  1514. case PROC_TGID_STAT:
  1515. inode->i_fop = &proc_info_file_operations;
  1516. ei->op.proc_read = proc_tgid_stat;
  1517. break;
  1518. case PROC_TID_CMDLINE:
  1519. case PROC_TGID_CMDLINE:
  1520. inode->i_fop = &proc_info_file_operations;
  1521. ei->op.proc_read = proc_pid_cmdline;
  1522. break;
  1523. case PROC_TID_STATM:
  1524. case PROC_TGID_STATM:
  1525. inode->i_fop = &proc_info_file_operations;
  1526. ei->op.proc_read = proc_pid_statm;
  1527. break;
  1528. case PROC_TID_MAPS:
  1529. case PROC_TGID_MAPS:
  1530. inode->i_fop = &proc_maps_operations;
  1531. break;
  1532. #ifdef CONFIG_NUMA
  1533. case PROC_TID_NUMA_MAPS:
  1534. case PROC_TGID_NUMA_MAPS:
  1535. inode->i_fop = &proc_numa_maps_operations;
  1536. break;
  1537. #endif
  1538. case PROC_TID_MEM:
  1539. case PROC_TGID_MEM:
  1540. inode->i_fop = &proc_mem_operations;
  1541. break;
  1542. #ifdef CONFIG_SECCOMP
  1543. case PROC_TID_SECCOMP:
  1544. case PROC_TGID_SECCOMP:
  1545. inode->i_fop = &proc_seccomp_operations;
  1546. break;
  1547. #endif /* CONFIG_SECCOMP */
  1548. case PROC_TID_MOUNTS:
  1549. case PROC_TGID_MOUNTS:
  1550. inode->i_fop = &proc_mounts_operations;
  1551. break;
  1552. #ifdef CONFIG_MMU
  1553. case PROC_TID_SMAPS:
  1554. case PROC_TGID_SMAPS:
  1555. inode->i_fop = &proc_smaps_operations;
  1556. break;
  1557. #endif
  1558. case PROC_TID_MOUNTSTATS:
  1559. case PROC_TGID_MOUNTSTATS:
  1560. inode->i_fop = &proc_mountstats_operations;
  1561. break;
  1562. #ifdef CONFIG_SECURITY
  1563. case PROC_TID_ATTR:
  1564. inode->i_nlink = 2;
  1565. inode->i_op = &proc_tid_attr_inode_operations;
  1566. inode->i_fop = &proc_tid_attr_operations;
  1567. break;
  1568. case PROC_TGID_ATTR:
  1569. inode->i_nlink = 2;
  1570. inode->i_op = &proc_tgid_attr_inode_operations;
  1571. inode->i_fop = &proc_tgid_attr_operations;
  1572. break;
  1573. case PROC_TID_ATTR_CURRENT:
  1574. case PROC_TGID_ATTR_CURRENT:
  1575. case PROC_TID_ATTR_PREV:
  1576. case PROC_TGID_ATTR_PREV:
  1577. case PROC_TID_ATTR_EXEC:
  1578. case PROC_TGID_ATTR_EXEC:
  1579. case PROC_TID_ATTR_FSCREATE:
  1580. case PROC_TGID_ATTR_FSCREATE:
  1581. case PROC_TID_ATTR_KEYCREATE:
  1582. case PROC_TGID_ATTR_KEYCREATE:
  1583. case PROC_TID_ATTR_SOCKCREATE:
  1584. case PROC_TGID_ATTR_SOCKCREATE:
  1585. inode->i_fop = &proc_pid_attr_operations;
  1586. break;
  1587. #endif
  1588. #ifdef CONFIG_KALLSYMS
  1589. case PROC_TID_WCHAN:
  1590. case PROC_TGID_WCHAN:
  1591. inode->i_fop = &proc_info_file_operations;
  1592. ei->op.proc_read = proc_pid_wchan;
  1593. break;
  1594. #endif
  1595. #ifdef CONFIG_SCHEDSTATS
  1596. case PROC_TID_SCHEDSTAT:
  1597. case PROC_TGID_SCHEDSTAT:
  1598. inode->i_fop = &proc_info_file_operations;
  1599. ei->op.proc_read = proc_pid_schedstat;
  1600. break;
  1601. #endif
  1602. #ifdef CONFIG_CPUSETS
  1603. case PROC_TID_CPUSET:
  1604. case PROC_TGID_CPUSET:
  1605. inode->i_fop = &proc_cpuset_operations;
  1606. break;
  1607. #endif
  1608. case PROC_TID_OOM_SCORE:
  1609. case PROC_TGID_OOM_SCORE:
  1610. inode->i_fop = &proc_info_file_operations;
  1611. ei->op.proc_read = proc_oom_score;
  1612. break;
  1613. case PROC_TID_OOM_ADJUST:
  1614. case PROC_TGID_OOM_ADJUST:
  1615. inode->i_fop = &proc_oom_adjust_operations;
  1616. break;
  1617. #ifdef CONFIG_AUDITSYSCALL
  1618. case PROC_TID_LOGINUID:
  1619. case PROC_TGID_LOGINUID:
  1620. inode->i_fop = &proc_loginuid_operations;
  1621. break;
  1622. #endif
  1623. default:
  1624. printk("procfs: impossible type (%d)",p->type);
  1625. iput(inode);
  1626. error = ERR_PTR(-EINVAL);
  1627. goto out;
  1628. }
  1629. dentry->d_op = &pid_dentry_operations;
  1630. d_add(dentry, inode);
  1631. /* Close the race of the process dying before we return the dentry */
  1632. if (pid_revalidate(dentry, NULL))
  1633. error = NULL;
  1634. out:
  1635. put_task_struct(task);
  1636. out_no_task:
  1637. return error;
  1638. }
  1639. static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
  1640. return proc_pident_lookup(dir, dentry, tgid_base_stuff);
  1641. }
  1642. static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
  1643. return proc_pident_lookup(dir, dentry, tid_base_stuff);
  1644. }
  1645. static struct file_operations proc_tgid_base_operations = {
  1646. .read = generic_read_dir,
  1647. .readdir = proc_tgid_base_readdir,
  1648. };
  1649. static struct file_operations proc_tid_base_operations = {
  1650. .read = generic_read_dir,
  1651. .readdir = proc_tid_base_readdir,
  1652. };
  1653. static struct inode_operations proc_tgid_base_inode_operations = {
  1654. .lookup = proc_tgid_base_lookup,
  1655. .getattr = pid_getattr,
  1656. };
  1657. static struct inode_operations proc_tid_base_inode_operations = {
  1658. .lookup = proc_tid_base_lookup,
  1659. .getattr = pid_getattr,
  1660. };
  1661. #ifdef CONFIG_SECURITY
  1662. static int proc_tgid_attr_readdir(struct file * filp,
  1663. void * dirent, filldir_t filldir)
  1664. {
  1665. return proc_pident_readdir(filp,dirent,filldir,
  1666. tgid_attr_stuff,ARRAY_SIZE(tgid_attr_stuff));
  1667. }
  1668. static int proc_tid_attr_readdir(struct file * filp,
  1669. void * dirent, filldir_t filldir)
  1670. {
  1671. return proc_pident_readdir(filp,dirent,filldir,
  1672. tid_attr_stuff,ARRAY_SIZE(tid_attr_stuff));
  1673. }
  1674. static struct file_operations proc_tgid_attr_operations = {
  1675. .read = generic_read_dir,
  1676. .readdir = proc_tgid_attr_readdir,
  1677. };
  1678. static struct file_operations proc_tid_attr_operations = {
  1679. .read = generic_read_dir,
  1680. .readdir = proc_tid_attr_readdir,
  1681. };
  1682. static struct dentry *proc_tgid_attr_lookup(struct inode *dir,
  1683. struct dentry *dentry, struct nameidata *nd)
  1684. {
  1685. return proc_pident_lookup(dir, dentry, tgid_attr_stuff);
  1686. }
  1687. static struct dentry *proc_tid_attr_lookup(struct inode *dir,
  1688. struct dentry *dentry, struct nameidata *nd)
  1689. {
  1690. return proc_pident_lookup(dir, dentry, tid_attr_stuff);
  1691. }
  1692. static struct inode_operations proc_tgid_attr_inode_operations = {
  1693. .lookup = proc_tgid_attr_lookup,
  1694. .getattr = pid_getattr,
  1695. };
  1696. static struct inode_operations proc_tid_attr_inode_operations = {
  1697. .lookup = proc_tid_attr_lookup,
  1698. .getattr = pid_getattr,
  1699. };
  1700. #endif
  1701. /*
  1702. * /proc/self:
  1703. */
  1704. static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
  1705. int buflen)
  1706. {
  1707. char tmp[PROC_NUMBUF];
  1708. sprintf(tmp, "%d", current->tgid);
  1709. return vfs_readlink(dentry,buffer,buflen,tmp);
  1710. }
  1711. static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
  1712. {
  1713. char tmp[PROC_NUMBUF];
  1714. sprintf(tmp, "%d", current->tgid);
  1715. return ERR_PTR(vfs_follow_link(nd,tmp));
  1716. }
  1717. static struct inode_operations proc_self_inode_operations = {
  1718. .readlink = proc_self_readlink,
  1719. .follow_link = proc_self_follow_link,
  1720. };
  1721. /**
  1722. * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
  1723. *
  1724. * @task: task that should be flushed.
  1725. *
  1726. * Looks in the dcache for
  1727. * /proc/@pid
  1728. * /proc/@tgid/task/@pid
  1729. * if either directory is present flushes it and all of it'ts children
  1730. * from the dcache.
  1731. *
  1732. * It is safe and reasonable to cache /proc entries for a task until
  1733. * that task exits. After that they just clog up the dcache with
  1734. * useless entries, possibly causing useful dcache entries to be
  1735. * flushed instead. This routine is proved to flush those useless
  1736. * dcache entries at process exit time.
  1737. *
  1738. * NOTE: This routine is just an optimization so it does not guarantee
  1739. * that no dcache entries will exist at process exit time it
  1740. * just makes it very unlikely that any will persist.
  1741. */
  1742. void proc_flush_task(struct task_struct *task)
  1743. {
  1744. struct dentry *dentry, *leader, *dir;
  1745. char buf[PROC_NUMBUF];
  1746. struct qstr name;
  1747. name.name = buf;
  1748. name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
  1749. dentry = d_hash_and_lookup(proc_mnt->mnt_root, &name);
  1750. if (dentry) {
  1751. shrink_dcache_parent(dentry);
  1752. d_drop(dentry);
  1753. dput(dentry);
  1754. }
  1755. if (thread_group_leader(task))
  1756. goto out;
  1757. name.name = buf;
  1758. name.len = snprintf(buf, sizeof(buf), "%d", task->tgid);
  1759. leader = d_hash_and_lookup(proc_mnt->mnt_root, &name);
  1760. if (!leader)
  1761. goto out;
  1762. name.name = "task";
  1763. name.len = strlen(name.name);
  1764. dir = d_hash_and_lookup(leader, &name);
  1765. if (!dir)
  1766. goto out_put_leader;
  1767. name.name = buf;
  1768. name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
  1769. dentry = d_hash_and_lookup(dir, &name);
  1770. if (dentry) {
  1771. shrink_dcache_parent(dentry);
  1772. d_drop(dentry);
  1773. dput(dentry);
  1774. }
  1775. dput(dir);
  1776. out_put_leader:
  1777. dput(leader);
  1778. out:
  1779. return;
  1780. }
  1781. /* SMP-safe */
  1782. struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
  1783. {
  1784. struct dentry *result = ERR_PTR(-ENOENT);
  1785. struct task_struct *task;
  1786. struct inode *inode;
  1787. struct proc_inode *ei;
  1788. unsigned tgid;
  1789. if (dentry->d_name.len == 4 && !memcmp(dentry->d_name.name,"self",4)) {
  1790. inode = new_inode(dir->i_sb);
  1791. if (!inode)
  1792. return ERR_PTR(-ENOMEM);
  1793. ei = PROC_I(inode);
  1794. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  1795. inode->i_ino = fake_ino(0, PROC_TGID_INO);
  1796. ei->pde = NULL;
  1797. inode->i_mode = S_IFLNK|S_IRWXUGO;
  1798. inode->i_uid = inode->i_gid = 0;
  1799. inode->i_size = 64;
  1800. inode->i_op = &proc_self_inode_operations;
  1801. d_add(dentry, inode);
  1802. return NULL;
  1803. }
  1804. tgid = name_to_int(dentry);
  1805. if (tgid == ~0U)
  1806. goto out;
  1807. rcu_read_lock();
  1808. task = find_task_by_pid(tgid);
  1809. if (task)
  1810. get_task_struct(task);
  1811. rcu_read_unlock();
  1812. if (!task)
  1813. goto out;
  1814. inode = proc_pid_make_inode(dir->i_sb, task, PROC_TGID_INO);
  1815. if (!inode)
  1816. goto out_put_task;
  1817. inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
  1818. inode->i_op = &proc_tgid_base_inode_operations;
  1819. inode->i_fop = &proc_tgid_base_operations;
  1820. inode->i_flags|=S_IMMUTABLE;
  1821. #ifdef CONFIG_SECURITY
  1822. inode->i_nlink = 5;
  1823. #else
  1824. inode->i_nlink = 4;
  1825. #endif
  1826. dentry->d_op = &pid_dentry_operations;
  1827. d_add(dentry, inode);
  1828. /* Close the race of the process dying before we return the dentry */
  1829. if (pid_revalidate(dentry, NULL))
  1830. result = NULL;
  1831. out_put_task:
  1832. put_task_struct(task);
  1833. out:
  1834. return result;
  1835. }
  1836. /* SMP-safe */
  1837. static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
  1838. {
  1839. struct dentry *result = ERR_PTR(-ENOENT);
  1840. struct task_struct *task;
  1841. struct task_struct *leader = get_proc_task(dir);
  1842. struct inode *inode;
  1843. unsigned tid;
  1844. if (!leader)
  1845. goto out_no_task;
  1846. tid = name_to_int(dentry);
  1847. if (tid == ~0U)
  1848. goto out;
  1849. rcu_read_lock();
  1850. task = find_task_by_pid(tid);
  1851. if (task)
  1852. get_task_struct(task);
  1853. rcu_read_unlock();
  1854. if (!task)
  1855. goto out;
  1856. if (leader->tgid != task->tgid)
  1857. goto out_drop_task;
  1858. inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_INO);
  1859. if (!inode)
  1860. goto out_drop_task;
  1861. inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
  1862. inode->i_op = &proc_tid_base_inode_operations;
  1863. inode->i_fop = &proc_tid_base_operations;
  1864. inode->i_flags|=S_IMMUTABLE;
  1865. #ifdef CONFIG_SECURITY
  1866. inode->i_nlink = 4;
  1867. #else
  1868. inode->i_nlink = 3;
  1869. #endif
  1870. dentry->d_op = &pid_dentry_operations;
  1871. d_add(dentry, inode);
  1872. /* Close the race of the process dying before we return the dentry */
  1873. if (pid_revalidate(dentry, NULL))
  1874. result = NULL;
  1875. out_drop_task:
  1876. put_task_struct(task);
  1877. out:
  1878. put_task_struct(leader);
  1879. out_no_task:
  1880. return result;
  1881. }
  1882. /*
  1883. * Find the first tgid to return to user space.
  1884. *
  1885. * Usually this is just whatever follows &init_task, but if the users
  1886. * buffer was too small to hold the full list or there was a seek into
  1887. * the middle of the directory we have more work to do.
  1888. *
  1889. * In the case of a short read we start with find_task_by_pid.
  1890. *
  1891. * In the case of a seek we start with &init_task and walk nr
  1892. * threads past it.
  1893. */
  1894. static struct task_struct *first_tgid(int tgid, unsigned int nr)
  1895. {
  1896. struct task_struct *pos;
  1897. rcu_read_lock();
  1898. if (tgid && nr) {
  1899. pos = find_task_by_pid(tgid);
  1900. if (pos && thread_group_leader(pos))
  1901. goto found;
  1902. }
  1903. /* If nr exceeds the number of processes get out quickly */
  1904. pos = NULL;
  1905. if (nr && nr >= nr_processes())
  1906. goto done;
  1907. /* If we haven't found our starting place yet start with
  1908. * the init_task and walk nr tasks forward.
  1909. */
  1910. for (pos = next_task(&init_task); nr > 0; --nr) {
  1911. pos = next_task(pos);
  1912. if (pos == &init_task) {
  1913. pos = NULL;
  1914. goto done;
  1915. }
  1916. }
  1917. found:
  1918. get_task_struct(pos);
  1919. done:
  1920. rcu_read_unlock();
  1921. return pos;
  1922. }
  1923. /*
  1924. * Find the next task in the task list.
  1925. * Return NULL if we loop or there is any error.
  1926. *
  1927. * The reference to the input task_struct is released.
  1928. */
  1929. static struct task_struct *next_tgid(struct task_struct *start)
  1930. {
  1931. struct task_struct *pos;
  1932. rcu_read_lock();
  1933. pos = start;
  1934. if (pid_alive(start))
  1935. pos = next_task(start);
  1936. if (pid_alive(pos) && (pos != &init_task)) {
  1937. get_task_struct(pos);
  1938. goto done;
  1939. }
  1940. pos = NULL;
  1941. done:
  1942. rcu_read_unlock();
  1943. put_task_struct(start);
  1944. return pos;
  1945. }
  1946. /* for the /proc/ directory itself, after non-process stuff has been done */
  1947. int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
  1948. {
  1949. char buf[PROC_NUMBUF];
  1950. unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
  1951. struct task_struct *task;
  1952. int tgid;
  1953. if (!nr) {
  1954. ino_t ino = fake_ino(0,PROC_TGID_INO);
  1955. if (filldir(dirent, "self", 4, filp->f_pos, ino, DT_LNK) < 0)
  1956. return 0;
  1957. filp->f_pos++;
  1958. nr++;
  1959. }
  1960. nr -= 1;
  1961. /* f_version caches the tgid value that the last readdir call couldn't
  1962. * return. lseek aka telldir automagically resets f_version to 0.
  1963. */
  1964. tgid = filp->f_version;
  1965. filp->f_version = 0;
  1966. for (task = first_tgid(tgid, nr);
  1967. task;
  1968. task = next_tgid(task), filp->f_pos++) {
  1969. int len;
  1970. ino_t ino;
  1971. tgid = task->pid;
  1972. len = snprintf(buf, sizeof(buf), "%d", tgid);
  1973. ino = fake_ino(tgid, PROC_TGID_INO);
  1974. if (filldir(dirent, buf, len, filp->f_pos, ino, DT_DIR) < 0) {
  1975. /* returning this tgid failed, save it as the first
  1976. * pid for the next readir call */
  1977. filp->f_version = tgid;
  1978. put_task_struct(task);
  1979. break;
  1980. }
  1981. }
  1982. return 0;
  1983. }
  1984. /*
  1985. * Find the first tid of a thread group to return to user space.
  1986. *
  1987. * Usually this is just the thread group leader, but if the users
  1988. * buffer was too small or there was a seek into the middle of the
  1989. * directory we have more work todo.
  1990. *
  1991. * In the case of a short read we start with find_task_by_pid.
  1992. *
  1993. * In the case of a seek we start with the leader and walk nr
  1994. * threads past it.
  1995. */
  1996. static struct task_struct *first_tid(struct task_struct *leader,
  1997. int tid, int nr)
  1998. {
  1999. struct task_struct *pos;
  2000. rcu_read_lock();
  2001. /* Attempt to start with the pid of a thread */
  2002. if (tid && (nr > 0)) {
  2003. pos = find_task_by_pid(tid);
  2004. if (pos && (pos->group_leader == leader))
  2005. goto found;
  2006. }
  2007. /* If nr exceeds the number of threads there is nothing todo */
  2008. pos = NULL;
  2009. if (nr && nr >= get_nr_threads(leader))
  2010. goto out;
  2011. /* If we haven't found our starting place yet start
  2012. * with the leader and walk nr threads forward.
  2013. */
  2014. for (pos = leader; nr > 0; --nr) {
  2015. pos = next_thread(pos);
  2016. if (pos == leader) {
  2017. pos = NULL;
  2018. goto out;
  2019. }
  2020. }
  2021. found:
  2022. get_task_struct(pos);
  2023. out:
  2024. rcu_read_unlock();
  2025. return pos;
  2026. }
  2027. /*
  2028. * Find the next thread in the thread list.
  2029. * Return NULL if there is an error or no next thread.
  2030. *
  2031. * The reference to the input task_struct is released.
  2032. */
  2033. static struct task_struct *next_tid(struct task_struct *start)
  2034. {
  2035. struct task_struct *pos = NULL;
  2036. rcu_read_lock();
  2037. if (pid_alive(start)) {
  2038. pos = next_thread(start);
  2039. if (thread_group_leader(pos))
  2040. pos = NULL;
  2041. else
  2042. get_task_struct(pos);
  2043. }
  2044. rcu_read_unlock();
  2045. put_task_struct(start);
  2046. return pos;
  2047. }
  2048. /* for the /proc/TGID/task/ directories */
  2049. static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
  2050. {
  2051. char buf[PROC_NUMBUF];
  2052. struct dentry *dentry = filp->f_dentry;
  2053. struct inode *inode = dentry->d_inode;
  2054. struct task_struct *leader = get_proc_task(inode);
  2055. struct task_struct *task;
  2056. int retval = -ENOENT;
  2057. ino_t ino;
  2058. int tid;
  2059. unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
  2060. if (!leader)
  2061. goto out_no_task;
  2062. retval = 0;
  2063. switch (pos) {
  2064. case 0:
  2065. ino = inode->i_ino;
  2066. if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
  2067. goto out;
  2068. pos++;
  2069. /* fall through */
  2070. case 1:
  2071. ino = parent_ino(dentry);
  2072. if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
  2073. goto out;
  2074. pos++;
  2075. /* fall through */
  2076. }
  2077. /* f_version caches the tgid value that the last readdir call couldn't
  2078. * return. lseek aka telldir automagically resets f_version to 0.
  2079. */
  2080. tid = filp->f_version;
  2081. filp->f_version = 0;
  2082. for (task = first_tid(leader, tid, pos - 2);
  2083. task;
  2084. task = next_tid(task), pos++) {
  2085. int len;
  2086. tid = task->pid;
  2087. len = snprintf(buf, sizeof(buf), "%d", tid);
  2088. ino = fake_ino(tid, PROC_TID_INO);
  2089. if (filldir(dirent, buf, len, pos, ino, DT_DIR < 0)) {
  2090. /* returning this tgid failed, save it as the first
  2091. * pid for the next readir call */
  2092. filp->f_version = tid;
  2093. put_task_struct(task);
  2094. break;
  2095. }
  2096. }
  2097. out:
  2098. filp->f_pos = pos;
  2099. put_task_struct(leader);
  2100. out_no_task:
  2101. return retval;
  2102. }
  2103. static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
  2104. {
  2105. struct inode *inode = dentry->d_inode;
  2106. struct task_struct *p = get_proc_task(inode);
  2107. generic_fillattr(inode, stat);
  2108. if (p) {
  2109. rcu_read_lock();
  2110. stat->nlink += get_nr_threads(p);
  2111. rcu_read_unlock();
  2112. put_task_struct(p);
  2113. }
  2114. return 0;
  2115. }