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/config.h>
  51. #include <linux/errno.h>
  52. #include <linux/time.h>
  53. #include <linux/proc_fs.h>
  54. #include <linux/stat.h>
  55. #include <linux/init.h>
  56. #include <linux/capability.h>
  57. #include <linux/file.h>
  58. #include <linux/string.h>
  59. #include <linux/seq_file.h>
  60. #include <linux/namei.h>
  61. #include <linux/namespace.h>
  62. #include <linux/mm.h>
  63. #include <linux/smp_lock.h>
  64. #include <linux/rcupdate.h>
  65. #include <linux/kallsyms.h>
  66. #include <linux/mount.h>
  67. #include <linux/security.h>
  68. #include <linux/ptrace.h>
  69. #include <linux/seccomp.h>
  70. #include <linux/cpuset.h>
  71. #include <linux/audit.h>
  72. #include <linux/poll.h>
  73. #include "internal.h"
  74. /* NOTE:
  75. * Implementing inode permission operations in /proc is almost
  76. * certainly an error. Permission checks need to happen during
  77. * each system call not at open time. The reason is that most of
  78. * what we wish to check for permissions in /proc varies at runtime.
  79. *
  80. * The classic example of a problem is opening file descriptors
  81. * in /proc for a task before it execs a suid executable.
  82. */
  83. /*
  84. * For hysterical raisins we keep the same inumbers as in the old procfs.
  85. * Feel free to change the macro below - just keep the range distinct from
  86. * inumbers of the rest of procfs (currently those are in 0x0000--0xffff).
  87. * As soon as we'll get a separate superblock we will be able to forget
  88. * about magical ranges too.
  89. */
  90. #define fake_ino(pid,ino) (((pid)<<16)|(ino))
  91. enum pid_directory_inos {
  92. PROC_TGID_INO = 2,
  93. PROC_TGID_TASK,
  94. PROC_TGID_STATUS,
  95. PROC_TGID_MEM,
  96. #ifdef CONFIG_SECCOMP
  97. PROC_TGID_SECCOMP,
  98. #endif
  99. PROC_TGID_CWD,
  100. PROC_TGID_ROOT,
  101. PROC_TGID_EXE,
  102. PROC_TGID_FD,
  103. PROC_TGID_ENVIRON,
  104. PROC_TGID_AUXV,
  105. PROC_TGID_CMDLINE,
  106. PROC_TGID_STAT,
  107. PROC_TGID_STATM,
  108. PROC_TGID_MAPS,
  109. PROC_TGID_NUMA_MAPS,
  110. PROC_TGID_MOUNTS,
  111. PROC_TGID_MOUNTSTATS,
  112. PROC_TGID_WCHAN,
  113. #ifdef CONFIG_MMU
  114. PROC_TGID_SMAPS,
  115. #endif
  116. #ifdef CONFIG_SCHEDSTATS
  117. PROC_TGID_SCHEDSTAT,
  118. #endif
  119. #ifdef CONFIG_CPUSETS
  120. PROC_TGID_CPUSET,
  121. #endif
  122. #ifdef CONFIG_SECURITY
  123. PROC_TGID_ATTR,
  124. PROC_TGID_ATTR_CURRENT,
  125. PROC_TGID_ATTR_PREV,
  126. PROC_TGID_ATTR_EXEC,
  127. PROC_TGID_ATTR_FSCREATE,
  128. PROC_TGID_ATTR_KEYCREATE,
  129. PROC_TGID_ATTR_SOCKCREATE,
  130. #endif
  131. #ifdef CONFIG_AUDITSYSCALL
  132. PROC_TGID_LOGINUID,
  133. #endif
  134. PROC_TGID_OOM_SCORE,
  135. PROC_TGID_OOM_ADJUST,
  136. PROC_TID_INO,
  137. PROC_TID_STATUS,
  138. PROC_TID_MEM,
  139. #ifdef CONFIG_SECCOMP
  140. PROC_TID_SECCOMP,
  141. #endif
  142. PROC_TID_CWD,
  143. PROC_TID_ROOT,
  144. PROC_TID_EXE,
  145. PROC_TID_FD,
  146. PROC_TID_ENVIRON,
  147. PROC_TID_AUXV,
  148. PROC_TID_CMDLINE,
  149. PROC_TID_STAT,
  150. PROC_TID_STATM,
  151. PROC_TID_MAPS,
  152. PROC_TID_NUMA_MAPS,
  153. PROC_TID_MOUNTS,
  154. PROC_TID_MOUNTSTATS,
  155. PROC_TID_WCHAN,
  156. #ifdef CONFIG_MMU
  157. PROC_TID_SMAPS,
  158. #endif
  159. #ifdef CONFIG_SCHEDSTATS
  160. PROC_TID_SCHEDSTAT,
  161. #endif
  162. #ifdef CONFIG_CPUSETS
  163. PROC_TID_CPUSET,
  164. #endif
  165. #ifdef CONFIG_SECURITY
  166. PROC_TID_ATTR,
  167. PROC_TID_ATTR_CURRENT,
  168. PROC_TID_ATTR_PREV,
  169. PROC_TID_ATTR_EXEC,
  170. PROC_TID_ATTR_FSCREATE,
  171. PROC_TID_ATTR_KEYCREATE,
  172. PROC_TID_ATTR_SOCKCREATE,
  173. #endif
  174. #ifdef CONFIG_AUDITSYSCALL
  175. PROC_TID_LOGINUID,
  176. #endif
  177. PROC_TID_OOM_SCORE,
  178. PROC_TID_OOM_ADJUST,
  179. /* Add new entries before this */
  180. PROC_TID_FD_DIR = 0x8000, /* 0x8000-0xffff */
  181. };
  182. /* Worst case buffer size needed for holding an integer. */
  183. #define PROC_NUMBUF 10
  184. struct pid_entry {
  185. int type;
  186. int len;
  187. char *name;
  188. mode_t mode;
  189. };
  190. #define E(type,name,mode) {(type),sizeof(name)-1,(name),(mode)}
  191. static struct pid_entry tgid_base_stuff[] = {
  192. E(PROC_TGID_TASK, "task", S_IFDIR|S_IRUGO|S_IXUGO),
  193. E(PROC_TGID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
  194. E(PROC_TGID_ENVIRON, "environ", S_IFREG|S_IRUSR),
  195. E(PROC_TGID_AUXV, "auxv", S_IFREG|S_IRUSR),
  196. E(PROC_TGID_STATUS, "status", S_IFREG|S_IRUGO),
  197. E(PROC_TGID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
  198. E(PROC_TGID_STAT, "stat", S_IFREG|S_IRUGO),
  199. E(PROC_TGID_STATM, "statm", S_IFREG|S_IRUGO),
  200. E(PROC_TGID_MAPS, "maps", S_IFREG|S_IRUGO),
  201. #ifdef CONFIG_NUMA
  202. E(PROC_TGID_NUMA_MAPS, "numa_maps", S_IFREG|S_IRUGO),
  203. #endif
  204. E(PROC_TGID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
  205. #ifdef CONFIG_SECCOMP
  206. E(PROC_TGID_SECCOMP, "seccomp", S_IFREG|S_IRUSR|S_IWUSR),
  207. #endif
  208. E(PROC_TGID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
  209. E(PROC_TGID_ROOT, "root", S_IFLNK|S_IRWXUGO),
  210. E(PROC_TGID_EXE, "exe", S_IFLNK|S_IRWXUGO),
  211. E(PROC_TGID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
  212. E(PROC_TGID_MOUNTSTATS, "mountstats", S_IFREG|S_IRUSR),
  213. #ifdef CONFIG_MMU
  214. E(PROC_TGID_SMAPS, "smaps", S_IFREG|S_IRUGO),
  215. #endif
  216. #ifdef CONFIG_SECURITY
  217. E(PROC_TGID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
  218. #endif
  219. #ifdef CONFIG_KALLSYMS
  220. E(PROC_TGID_WCHAN, "wchan", S_IFREG|S_IRUGO),
  221. #endif
  222. #ifdef CONFIG_SCHEDSTATS
  223. E(PROC_TGID_SCHEDSTAT, "schedstat", S_IFREG|S_IRUGO),
  224. #endif
  225. #ifdef CONFIG_CPUSETS
  226. E(PROC_TGID_CPUSET, "cpuset", S_IFREG|S_IRUGO),
  227. #endif
  228. E(PROC_TGID_OOM_SCORE, "oom_score",S_IFREG|S_IRUGO),
  229. E(PROC_TGID_OOM_ADJUST,"oom_adj", S_IFREG|S_IRUGO|S_IWUSR),
  230. #ifdef CONFIG_AUDITSYSCALL
  231. E(PROC_TGID_LOGINUID, "loginuid", S_IFREG|S_IWUSR|S_IRUGO),
  232. #endif
  233. {0,0,NULL,0}
  234. };
  235. static struct pid_entry tid_base_stuff[] = {
  236. E(PROC_TID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
  237. E(PROC_TID_ENVIRON, "environ", S_IFREG|S_IRUSR),
  238. E(PROC_TID_AUXV, "auxv", S_IFREG|S_IRUSR),
  239. E(PROC_TID_STATUS, "status", S_IFREG|S_IRUGO),
  240. E(PROC_TID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
  241. E(PROC_TID_STAT, "stat", S_IFREG|S_IRUGO),
  242. E(PROC_TID_STATM, "statm", S_IFREG|S_IRUGO),
  243. E(PROC_TID_MAPS, "maps", S_IFREG|S_IRUGO),
  244. #ifdef CONFIG_NUMA
  245. E(PROC_TID_NUMA_MAPS, "numa_maps", S_IFREG|S_IRUGO),
  246. #endif
  247. E(PROC_TID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
  248. #ifdef CONFIG_SECCOMP
  249. E(PROC_TID_SECCOMP, "seccomp", S_IFREG|S_IRUSR|S_IWUSR),
  250. #endif
  251. E(PROC_TID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
  252. E(PROC_TID_ROOT, "root", S_IFLNK|S_IRWXUGO),
  253. E(PROC_TID_EXE, "exe", S_IFLNK|S_IRWXUGO),
  254. E(PROC_TID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
  255. #ifdef CONFIG_MMU
  256. E(PROC_TID_SMAPS, "smaps", S_IFREG|S_IRUGO),
  257. #endif
  258. #ifdef CONFIG_SECURITY
  259. E(PROC_TID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
  260. #endif
  261. #ifdef CONFIG_KALLSYMS
  262. E(PROC_TID_WCHAN, "wchan", S_IFREG|S_IRUGO),
  263. #endif
  264. #ifdef CONFIG_SCHEDSTATS
  265. E(PROC_TID_SCHEDSTAT, "schedstat",S_IFREG|S_IRUGO),
  266. #endif
  267. #ifdef CONFIG_CPUSETS
  268. E(PROC_TID_CPUSET, "cpuset", S_IFREG|S_IRUGO),
  269. #endif
  270. E(PROC_TID_OOM_SCORE, "oom_score",S_IFREG|S_IRUGO),
  271. E(PROC_TID_OOM_ADJUST, "oom_adj", S_IFREG|S_IRUGO|S_IWUSR),
  272. #ifdef CONFIG_AUDITSYSCALL
  273. E(PROC_TID_LOGINUID, "loginuid", S_IFREG|S_IWUSR|S_IRUGO),
  274. #endif
  275. {0,0,NULL,0}
  276. };
  277. #ifdef CONFIG_SECURITY
  278. static struct pid_entry tgid_attr_stuff[] = {
  279. E(PROC_TGID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO),
  280. E(PROC_TGID_ATTR_PREV, "prev", S_IFREG|S_IRUGO),
  281. E(PROC_TGID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO),
  282. E(PROC_TGID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
  283. E(PROC_TGID_ATTR_KEYCREATE, "keycreate", S_IFREG|S_IRUGO|S_IWUGO),
  284. E(PROC_TGID_ATTR_SOCKCREATE, "sockcreate", S_IFREG|S_IRUGO|S_IWUGO),
  285. {0,0,NULL,0}
  286. };
  287. static struct pid_entry tid_attr_stuff[] = {
  288. E(PROC_TID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO),
  289. E(PROC_TID_ATTR_PREV, "prev", S_IFREG|S_IRUGO),
  290. E(PROC_TID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO),
  291. E(PROC_TID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
  292. E(PROC_TID_ATTR_KEYCREATE, "keycreate", S_IFREG|S_IRUGO|S_IWUGO),
  293. E(PROC_TID_ATTR_SOCKCREATE, "sockcreate", S_IFREG|S_IRUGO|S_IWUGO),
  294. {0,0,NULL,0}
  295. };
  296. #endif
  297. #undef E
  298. static int proc_fd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
  299. {
  300. struct task_struct *task = get_proc_task(inode);
  301. struct files_struct *files = NULL;
  302. struct file *file;
  303. int fd = proc_fd(inode);
  304. if (task) {
  305. files = get_files_struct(task);
  306. put_task_struct(task);
  307. }
  308. if (files) {
  309. /*
  310. * We are not taking a ref to the file structure, so we must
  311. * hold ->file_lock.
  312. */
  313. spin_lock(&files->file_lock);
  314. file = fcheck_files(files, fd);
  315. if (file) {
  316. *mnt = mntget(file->f_vfsmnt);
  317. *dentry = dget(file->f_dentry);
  318. spin_unlock(&files->file_lock);
  319. put_files_struct(files);
  320. return 0;
  321. }
  322. spin_unlock(&files->file_lock);
  323. put_files_struct(files);
  324. }
  325. return -ENOENT;
  326. }
  327. static struct fs_struct *get_fs_struct(struct task_struct *task)
  328. {
  329. struct fs_struct *fs;
  330. task_lock(task);
  331. fs = task->fs;
  332. if(fs)
  333. atomic_inc(&fs->count);
  334. task_unlock(task);
  335. return fs;
  336. }
  337. static int get_nr_threads(struct task_struct *tsk)
  338. {
  339. /* Must be called with the rcu_read_lock held */
  340. unsigned long flags;
  341. int count = 0;
  342. if (lock_task_sighand(tsk, &flags)) {
  343. count = atomic_read(&tsk->signal->count);
  344. unlock_task_sighand(tsk, &flags);
  345. }
  346. return count;
  347. }
  348. static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
  349. {
  350. struct task_struct *task = get_proc_task(inode);
  351. struct fs_struct *fs = NULL;
  352. int result = -ENOENT;
  353. if (task) {
  354. fs = get_fs_struct(task);
  355. put_task_struct(task);
  356. }
  357. if (fs) {
  358. read_lock(&fs->lock);
  359. *mnt = mntget(fs->pwdmnt);
  360. *dentry = dget(fs->pwd);
  361. read_unlock(&fs->lock);
  362. result = 0;
  363. put_fs_struct(fs);
  364. }
  365. return result;
  366. }
  367. static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
  368. {
  369. struct task_struct *task = get_proc_task(inode);
  370. struct fs_struct *fs = NULL;
  371. int result = -ENOENT;
  372. if (task) {
  373. fs = get_fs_struct(task);
  374. put_task_struct(task);
  375. }
  376. if (fs) {
  377. read_lock(&fs->lock);
  378. *mnt = mntget(fs->rootmnt);
  379. *dentry = dget(fs->root);
  380. read_unlock(&fs->lock);
  381. result = 0;
  382. put_fs_struct(fs);
  383. }
  384. return result;
  385. }
  386. #define MAY_PTRACE(task) \
  387. (task == current || \
  388. (task->parent == current && \
  389. (task->ptrace & PT_PTRACED) && \
  390. (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
  391. security_ptrace(current,task) == 0))
  392. static int proc_pid_environ(struct task_struct *task, char * buffer)
  393. {
  394. int res = 0;
  395. struct mm_struct *mm = get_task_mm(task);
  396. if (mm) {
  397. unsigned int len = mm->env_end - mm->env_start;
  398. if (len > PAGE_SIZE)
  399. len = PAGE_SIZE;
  400. res = access_process_vm(task, mm->env_start, buffer, len, 0);
  401. if (!ptrace_may_attach(task))
  402. res = -ESRCH;
  403. mmput(mm);
  404. }
  405. return res;
  406. }
  407. static int proc_pid_cmdline(struct task_struct *task, char * buffer)
  408. {
  409. int res = 0;
  410. unsigned int len;
  411. struct mm_struct *mm = get_task_mm(task);
  412. if (!mm)
  413. goto out;
  414. if (!mm->arg_end)
  415. goto out_mm; /* Shh! No looking before we're done */
  416. len = mm->arg_end - mm->arg_start;
  417. if (len > PAGE_SIZE)
  418. len = PAGE_SIZE;
  419. res = access_process_vm(task, mm->arg_start, buffer, len, 0);
  420. // If the nul at the end of args has been overwritten, then
  421. // assume application is using setproctitle(3).
  422. if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
  423. len = strnlen(buffer, res);
  424. if (len < res) {
  425. res = len;
  426. } else {
  427. len = mm->env_end - mm->env_start;
  428. if (len > PAGE_SIZE - res)
  429. len = PAGE_SIZE - res;
  430. res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
  431. res = strnlen(buffer, res);
  432. }
  433. }
  434. out_mm:
  435. mmput(mm);
  436. out:
  437. return res;
  438. }
  439. static int proc_pid_auxv(struct task_struct *task, char *buffer)
  440. {
  441. int res = 0;
  442. struct mm_struct *mm = get_task_mm(task);
  443. if (mm) {
  444. unsigned int nwords = 0;
  445. do
  446. nwords += 2;
  447. while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
  448. res = nwords * sizeof(mm->saved_auxv[0]);
  449. if (res > PAGE_SIZE)
  450. res = PAGE_SIZE;
  451. memcpy(buffer, mm->saved_auxv, res);
  452. mmput(mm);
  453. }
  454. return res;
  455. }
  456. #ifdef CONFIG_KALLSYMS
  457. /*
  458. * Provides a wchan file via kallsyms in a proper one-value-per-file format.
  459. * Returns the resolved symbol. If that fails, simply return the address.
  460. */
  461. static int proc_pid_wchan(struct task_struct *task, char *buffer)
  462. {
  463. char *modname;
  464. const char *sym_name;
  465. unsigned long wchan, size, offset;
  466. char namebuf[KSYM_NAME_LEN+1];
  467. wchan = get_wchan(task);
  468. sym_name = kallsyms_lookup(wchan, &size, &offset, &modname, namebuf);
  469. if (sym_name)
  470. return sprintf(buffer, "%s", sym_name);
  471. return sprintf(buffer, "%lu", wchan);
  472. }
  473. #endif /* CONFIG_KALLSYMS */
  474. #ifdef CONFIG_SCHEDSTATS
  475. /*
  476. * Provides /proc/PID/schedstat
  477. */
  478. static int proc_pid_schedstat(struct task_struct *task, char *buffer)
  479. {
  480. return sprintf(buffer, "%lu %lu %lu\n",
  481. task->sched_info.cpu_time,
  482. task->sched_info.run_delay,
  483. task->sched_info.pcnt);
  484. }
  485. #endif
  486. /* The badness from the OOM killer */
  487. unsigned long badness(struct task_struct *p, unsigned long uptime);
  488. static int proc_oom_score(struct task_struct *task, char *buffer)
  489. {
  490. unsigned long points;
  491. struct timespec uptime;
  492. do_posix_clock_monotonic_gettime(&uptime);
  493. points = badness(task, uptime.tv_sec);
  494. return sprintf(buffer, "%lu\n", points);
  495. }
  496. /************************************************************************/
  497. /* Here the fs part begins */
  498. /************************************************************************/
  499. /* permission checks */
  500. static int proc_fd_access_allowed(struct inode *inode)
  501. {
  502. struct task_struct *task;
  503. int allowed = 0;
  504. /* Allow access to a task's file descriptors if it is us or we
  505. * may use ptrace attach to the process and find out that
  506. * information.
  507. */
  508. task = get_proc_task(inode);
  509. if (task) {
  510. allowed = ptrace_may_attach(task);
  511. put_task_struct(task);
  512. }
  513. return allowed;
  514. }
  515. extern struct seq_operations mounts_op;
  516. struct proc_mounts {
  517. struct seq_file m;
  518. int event;
  519. };
  520. static int mounts_open(struct inode *inode, struct file *file)
  521. {
  522. struct task_struct *task = get_proc_task(inode);
  523. struct namespace *namespace = NULL;
  524. struct proc_mounts *p;
  525. int ret = -EINVAL;
  526. if (task) {
  527. task_lock(task);
  528. namespace = task->namespace;
  529. if (namespace)
  530. get_namespace(namespace);
  531. task_unlock(task);
  532. put_task_struct(task);
  533. }
  534. if (namespace) {
  535. ret = -ENOMEM;
  536. p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
  537. if (p) {
  538. file->private_data = &p->m;
  539. ret = seq_open(file, &mounts_op);
  540. if (!ret) {
  541. p->m.private = namespace;
  542. p->event = namespace->event;
  543. return 0;
  544. }
  545. kfree(p);
  546. }
  547. put_namespace(namespace);
  548. }
  549. return ret;
  550. }
  551. static int mounts_release(struct inode *inode, struct file *file)
  552. {
  553. struct seq_file *m = file->private_data;
  554. struct namespace *namespace = m->private;
  555. put_namespace(namespace);
  556. return seq_release(inode, file);
  557. }
  558. static unsigned mounts_poll(struct file *file, poll_table *wait)
  559. {
  560. struct proc_mounts *p = file->private_data;
  561. struct namespace *ns = p->m.private;
  562. unsigned res = 0;
  563. poll_wait(file, &ns->poll, wait);
  564. spin_lock(&vfsmount_lock);
  565. if (p->event != ns->event) {
  566. p->event = ns->event;
  567. res = POLLERR;
  568. }
  569. spin_unlock(&vfsmount_lock);
  570. return res;
  571. }
  572. static struct file_operations proc_mounts_operations = {
  573. .open = mounts_open,
  574. .read = seq_read,
  575. .llseek = seq_lseek,
  576. .release = mounts_release,
  577. .poll = mounts_poll,
  578. };
  579. extern struct seq_operations mountstats_op;
  580. static int mountstats_open(struct inode *inode, struct file *file)
  581. {
  582. int ret = seq_open(file, &mountstats_op);
  583. if (!ret) {
  584. struct seq_file *m = file->private_data;
  585. struct namespace *namespace = NULL;
  586. struct task_struct *task = get_proc_task(inode);
  587. if (task) {
  588. task_lock(task);
  589. namespace = task->namespace;
  590. if (namespace)
  591. get_namespace(namespace);
  592. task_unlock(task);
  593. put_task_struct(task);
  594. }
  595. if (namespace)
  596. m->private = namespace;
  597. else {
  598. seq_release(inode, file);
  599. ret = -EINVAL;
  600. }
  601. }
  602. return ret;
  603. }
  604. static struct file_operations proc_mountstats_operations = {
  605. .open = mountstats_open,
  606. .read = seq_read,
  607. .llseek = seq_lseek,
  608. .release = mounts_release,
  609. };
  610. #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
  611. static ssize_t proc_info_read(struct file * file, char __user * buf,
  612. size_t count, loff_t *ppos)
  613. {
  614. struct inode * inode = file->f_dentry->d_inode;
  615. unsigned long page;
  616. ssize_t length;
  617. struct task_struct *task = get_proc_task(inode);
  618. length = -ESRCH;
  619. if (!task)
  620. goto out_no_task;
  621. if (count > PROC_BLOCK_SIZE)
  622. count = PROC_BLOCK_SIZE;
  623. length = -ENOMEM;
  624. if (!(page = __get_free_page(GFP_KERNEL)))
  625. goto out;
  626. length = PROC_I(inode)->op.proc_read(task, (char*)page);
  627. if (length >= 0)
  628. length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
  629. free_page(page);
  630. out:
  631. put_task_struct(task);
  632. out_no_task:
  633. return length;
  634. }
  635. static struct file_operations proc_info_file_operations = {
  636. .read = proc_info_read,
  637. };
  638. static int mem_open(struct inode* inode, struct file* file)
  639. {
  640. file->private_data = (void*)((long)current->self_exec_id);
  641. return 0;
  642. }
  643. static ssize_t mem_read(struct file * file, char __user * buf,
  644. size_t count, loff_t *ppos)
  645. {
  646. struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
  647. char *page;
  648. unsigned long src = *ppos;
  649. int ret = -ESRCH;
  650. struct mm_struct *mm;
  651. if (!task)
  652. goto out_no_task;
  653. if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
  654. goto out;
  655. ret = -ENOMEM;
  656. page = (char *)__get_free_page(GFP_USER);
  657. if (!page)
  658. goto out;
  659. ret = 0;
  660. mm = get_task_mm(task);
  661. if (!mm)
  662. goto out_free;
  663. ret = -EIO;
  664. if (file->private_data != (void*)((long)current->self_exec_id))
  665. goto out_put;
  666. ret = 0;
  667. while (count > 0) {
  668. int this_len, retval;
  669. this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
  670. retval = access_process_vm(task, src, page, this_len, 0);
  671. if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
  672. if (!ret)
  673. ret = -EIO;
  674. break;
  675. }
  676. if (copy_to_user(buf, page, retval)) {
  677. ret = -EFAULT;
  678. break;
  679. }
  680. ret += retval;
  681. src += retval;
  682. buf += retval;
  683. count -= retval;
  684. }
  685. *ppos = src;
  686. out_put:
  687. mmput(mm);
  688. out_free:
  689. free_page((unsigned long) page);
  690. out:
  691. put_task_struct(task);
  692. out_no_task:
  693. return ret;
  694. }
  695. #define mem_write NULL
  696. #ifndef mem_write
  697. /* This is a security hazard */
  698. static ssize_t mem_write(struct file * file, const char * buf,
  699. size_t count, loff_t *ppos)
  700. {
  701. int copied = 0;
  702. char *page;
  703. struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
  704. unsigned long dst = *ppos;
  705. copied = -ESRCH;
  706. if (!task)
  707. goto out_no_task;
  708. if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
  709. goto out;
  710. copied = -ENOMEM;
  711. page = (char *)__get_free_page(GFP_USER);
  712. if (!page)
  713. goto out;
  714. while (count > 0) {
  715. int this_len, retval;
  716. this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
  717. if (copy_from_user(page, buf, this_len)) {
  718. copied = -EFAULT;
  719. break;
  720. }
  721. retval = access_process_vm(task, dst, page, this_len, 1);
  722. if (!retval) {
  723. if (!copied)
  724. copied = -EIO;
  725. break;
  726. }
  727. copied += retval;
  728. buf += retval;
  729. dst += retval;
  730. count -= retval;
  731. }
  732. *ppos = dst;
  733. free_page((unsigned long) page);
  734. out:
  735. put_task_struct(task);
  736. out_no_task:
  737. return copied;
  738. }
  739. #endif
  740. static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
  741. {
  742. switch (orig) {
  743. case 0:
  744. file->f_pos = offset;
  745. break;
  746. case 1:
  747. file->f_pos += offset;
  748. break;
  749. default:
  750. return -EINVAL;
  751. }
  752. force_successful_syscall_return();
  753. return file->f_pos;
  754. }
  755. static struct file_operations proc_mem_operations = {
  756. .llseek = mem_lseek,
  757. .read = mem_read,
  758. .write = mem_write,
  759. .open = mem_open,
  760. };
  761. static ssize_t oom_adjust_read(struct file *file, char __user *buf,
  762. size_t count, loff_t *ppos)
  763. {
  764. struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
  765. char buffer[PROC_NUMBUF];
  766. size_t len;
  767. int oom_adjust;
  768. loff_t __ppos = *ppos;
  769. if (!task)
  770. return -ESRCH;
  771. oom_adjust = task->oomkilladj;
  772. put_task_struct(task);
  773. len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
  774. if (__ppos >= len)
  775. return 0;
  776. if (count > len-__ppos)
  777. count = len-__ppos;
  778. if (copy_to_user(buf, buffer + __ppos, count))
  779. return -EFAULT;
  780. *ppos = __ppos + count;
  781. return count;
  782. }
  783. static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
  784. size_t count, loff_t *ppos)
  785. {
  786. struct task_struct *task;
  787. char buffer[PROC_NUMBUF], *end;
  788. int oom_adjust;
  789. if (!capable(CAP_SYS_RESOURCE))
  790. return -EPERM;
  791. memset(buffer, 0, sizeof(buffer));
  792. if (count > sizeof(buffer) - 1)
  793. count = sizeof(buffer) - 1;
  794. if (copy_from_user(buffer, buf, count))
  795. return -EFAULT;
  796. oom_adjust = simple_strtol(buffer, &end, 0);
  797. if ((oom_adjust < -16 || oom_adjust > 15) && oom_adjust != OOM_DISABLE)
  798. return -EINVAL;
  799. if (*end == '\n')
  800. end++;
  801. task = get_proc_task(file->f_dentry->d_inode);
  802. if (!task)
  803. return -ESRCH;
  804. task->oomkilladj = oom_adjust;
  805. put_task_struct(task);
  806. if (end - buffer == 0)
  807. return -EIO;
  808. return end - buffer;
  809. }
  810. static struct file_operations proc_oom_adjust_operations = {
  811. .read = oom_adjust_read,
  812. .write = oom_adjust_write,
  813. };
  814. #ifdef CONFIG_AUDITSYSCALL
  815. #define TMPBUFLEN 21
  816. static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
  817. size_t count, loff_t *ppos)
  818. {
  819. struct inode * inode = file->f_dentry->d_inode;
  820. struct task_struct *task = get_proc_task(inode);
  821. ssize_t length;
  822. char tmpbuf[TMPBUFLEN];
  823. if (!task)
  824. return -ESRCH;
  825. length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
  826. audit_get_loginuid(task->audit_context));
  827. put_task_struct(task);
  828. return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
  829. }
  830. static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
  831. size_t count, loff_t *ppos)
  832. {
  833. struct inode * inode = file->f_dentry->d_inode;
  834. char *page, *tmp;
  835. ssize_t length;
  836. uid_t loginuid;
  837. if (!capable(CAP_AUDIT_CONTROL))
  838. return -EPERM;
  839. if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
  840. return -EPERM;
  841. if (count >= PAGE_SIZE)
  842. count = PAGE_SIZE - 1;
  843. if (*ppos != 0) {
  844. /* No partial writes. */
  845. return -EINVAL;
  846. }
  847. page = (char*)__get_free_page(GFP_USER);
  848. if (!page)
  849. return -ENOMEM;
  850. length = -EFAULT;
  851. if (copy_from_user(page, buf, count))
  852. goto out_free_page;
  853. page[count] = '\0';
  854. loginuid = simple_strtoul(page, &tmp, 10);
  855. if (tmp == page) {
  856. length = -EINVAL;
  857. goto out_free_page;
  858. }
  859. length = audit_set_loginuid(current, loginuid);
  860. if (likely(length == 0))
  861. length = count;
  862. out_free_page:
  863. free_page((unsigned long) page);
  864. return length;
  865. }
  866. static struct file_operations proc_loginuid_operations = {
  867. .read = proc_loginuid_read,
  868. .write = proc_loginuid_write,
  869. };
  870. #endif
  871. #ifdef CONFIG_SECCOMP
  872. static ssize_t seccomp_read(struct file *file, char __user *buf,
  873. size_t count, loff_t *ppos)
  874. {
  875. struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
  876. char __buf[20];
  877. loff_t __ppos = *ppos;
  878. size_t len;
  879. if (!tsk)
  880. return -ESRCH;
  881. /* no need to print the trailing zero, so use only len */
  882. len = sprintf(__buf, "%u\n", tsk->seccomp.mode);
  883. put_task_struct(tsk);
  884. if (__ppos >= len)
  885. return 0;
  886. if (count > len - __ppos)
  887. count = len - __ppos;
  888. if (copy_to_user(buf, __buf + __ppos, count))
  889. return -EFAULT;
  890. *ppos = __ppos + count;
  891. return count;
  892. }
  893. static ssize_t seccomp_write(struct file *file, const char __user *buf,
  894. size_t count, loff_t *ppos)
  895. {
  896. struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
  897. char __buf[20], *end;
  898. unsigned int seccomp_mode;
  899. ssize_t result;
  900. result = -ESRCH;
  901. if (!tsk)
  902. goto out_no_task;
  903. /* can set it only once to be even more secure */
  904. result = -EPERM;
  905. if (unlikely(tsk->seccomp.mode))
  906. goto out;
  907. result = -EFAULT;
  908. memset(__buf, 0, sizeof(__buf));
  909. count = min(count, sizeof(__buf) - 1);
  910. if (copy_from_user(__buf, buf, count))
  911. goto out;
  912. seccomp_mode = simple_strtoul(__buf, &end, 0);
  913. if (*end == '\n')
  914. end++;
  915. result = -EINVAL;
  916. if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) {
  917. tsk->seccomp.mode = seccomp_mode;
  918. set_tsk_thread_flag(tsk, TIF_SECCOMP);
  919. } else
  920. goto out;
  921. result = -EIO;
  922. if (unlikely(!(end - __buf)))
  923. goto out;
  924. result = end - __buf;
  925. out:
  926. put_task_struct(tsk);
  927. out_no_task:
  928. return result;
  929. }
  930. static struct file_operations proc_seccomp_operations = {
  931. .read = seccomp_read,
  932. .write = seccomp_write,
  933. };
  934. #endif /* CONFIG_SECCOMP */
  935. static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
  936. {
  937. struct inode *inode = dentry->d_inode;
  938. int error = -EACCES;
  939. /* We don't need a base pointer in the /proc filesystem */
  940. path_release(nd);
  941. /* Are we allowed to snoop on the tasks file descriptors? */
  942. if (!proc_fd_access_allowed(inode))
  943. goto out;
  944. error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
  945. nd->last_type = LAST_BIND;
  946. out:
  947. return ERR_PTR(error);
  948. }
  949. static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
  950. char __user *buffer, int buflen)
  951. {
  952. struct inode * inode;
  953. char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
  954. int len;
  955. if (!tmp)
  956. return -ENOMEM;
  957. inode = dentry->d_inode;
  958. path = d_path(dentry, mnt, tmp, PAGE_SIZE);
  959. len = PTR_ERR(path);
  960. if (IS_ERR(path))
  961. goto out;
  962. len = tmp + PAGE_SIZE - 1 - path;
  963. if (len > buflen)
  964. len = buflen;
  965. if (copy_to_user(buffer, path, len))
  966. len = -EFAULT;
  967. out:
  968. free_page((unsigned long)tmp);
  969. return len;
  970. }
  971. static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
  972. {
  973. int error = -EACCES;
  974. struct inode *inode = dentry->d_inode;
  975. struct dentry *de;
  976. struct vfsmount *mnt = NULL;
  977. /* Are we allowed to snoop on the tasks file descriptors? */
  978. if (!proc_fd_access_allowed(inode))
  979. goto out;
  980. error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
  981. if (error)
  982. goto out;
  983. error = do_proc_readlink(de, mnt, buffer, buflen);
  984. dput(de);
  985. mntput(mnt);
  986. out:
  987. return error;
  988. }
  989. static struct inode_operations proc_pid_link_inode_operations = {
  990. .readlink = proc_pid_readlink,
  991. .follow_link = proc_pid_follow_link
  992. };
  993. static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
  994. {
  995. struct dentry *dentry = filp->f_dentry;
  996. struct inode *inode = dentry->d_inode;
  997. struct task_struct *p = get_proc_task(inode);
  998. unsigned int fd, tid, ino;
  999. int retval;
  1000. char buf[PROC_NUMBUF];
  1001. struct files_struct * files;
  1002. struct fdtable *fdt;
  1003. retval = -ENOENT;
  1004. if (!p)
  1005. goto out_no_task;
  1006. retval = 0;
  1007. tid = p->pid;
  1008. fd = filp->f_pos;
  1009. switch (fd) {
  1010. case 0:
  1011. if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
  1012. goto out;
  1013. filp->f_pos++;
  1014. case 1:
  1015. ino = parent_ino(dentry);
  1016. if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
  1017. goto out;
  1018. filp->f_pos++;
  1019. default:
  1020. files = get_files_struct(p);
  1021. if (!files)
  1022. goto out;
  1023. rcu_read_lock();
  1024. fdt = files_fdtable(files);
  1025. for (fd = filp->f_pos-2;
  1026. fd < fdt->max_fds;
  1027. fd++, filp->f_pos++) {
  1028. unsigned int i,j;
  1029. if (!fcheck_files(files, fd))
  1030. continue;
  1031. rcu_read_unlock();
  1032. j = PROC_NUMBUF;
  1033. i = fd;
  1034. do {
  1035. j--;
  1036. buf[j] = '0' + (i % 10);
  1037. i /= 10;
  1038. } while (i);
  1039. ino = fake_ino(tid, PROC_TID_FD_DIR + fd);
  1040. if (filldir(dirent, buf+j, PROC_NUMBUF-j, fd+2, ino, DT_LNK) < 0) {
  1041. rcu_read_lock();
  1042. break;
  1043. }
  1044. rcu_read_lock();
  1045. }
  1046. rcu_read_unlock();
  1047. put_files_struct(files);
  1048. }
  1049. out:
  1050. put_task_struct(p);
  1051. out_no_task:
  1052. return retval;
  1053. }
  1054. static int proc_pident_readdir(struct file *filp,
  1055. void *dirent, filldir_t filldir,
  1056. struct pid_entry *ents, unsigned int nents)
  1057. {
  1058. int i;
  1059. int pid;
  1060. struct dentry *dentry = filp->f_dentry;
  1061. struct inode *inode = dentry->d_inode;
  1062. struct task_struct *task = get_proc_task(inode);
  1063. struct pid_entry *p;
  1064. ino_t ino;
  1065. int ret;
  1066. ret = -ENOENT;
  1067. if (!task)
  1068. goto out;
  1069. ret = 0;
  1070. pid = task->pid;
  1071. put_task_struct(task);
  1072. i = filp->f_pos;
  1073. switch (i) {
  1074. case 0:
  1075. ino = inode->i_ino;
  1076. if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
  1077. goto out;
  1078. i++;
  1079. filp->f_pos++;
  1080. /* fall through */
  1081. case 1:
  1082. ino = parent_ino(dentry);
  1083. if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
  1084. goto out;
  1085. i++;
  1086. filp->f_pos++;
  1087. /* fall through */
  1088. default:
  1089. i -= 2;
  1090. if (i >= nents) {
  1091. ret = 1;
  1092. goto out;
  1093. }
  1094. p = ents + i;
  1095. while (p->name) {
  1096. if (filldir(dirent, p->name, p->len, filp->f_pos,
  1097. fake_ino(pid, p->type), p->mode >> 12) < 0)
  1098. goto out;
  1099. filp->f_pos++;
  1100. p++;
  1101. }
  1102. }
  1103. ret = 1;
  1104. out:
  1105. return ret;
  1106. }
  1107. static int proc_tgid_base_readdir(struct file * filp,
  1108. void * dirent, filldir_t filldir)
  1109. {
  1110. return proc_pident_readdir(filp,dirent,filldir,
  1111. tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
  1112. }
  1113. static int proc_tid_base_readdir(struct file * filp,
  1114. void * dirent, filldir_t filldir)
  1115. {
  1116. return proc_pident_readdir(filp,dirent,filldir,
  1117. tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
  1118. }
  1119. /* building an inode */
  1120. static int task_dumpable(struct task_struct *task)
  1121. {
  1122. int dumpable = 0;
  1123. struct mm_struct *mm;
  1124. task_lock(task);
  1125. mm = task->mm;
  1126. if (mm)
  1127. dumpable = mm->dumpable;
  1128. task_unlock(task);
  1129. if(dumpable == 1)
  1130. return 1;
  1131. return 0;
  1132. }
  1133. static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task, int ino)
  1134. {
  1135. struct inode * inode;
  1136. struct proc_inode *ei;
  1137. /* We need a new inode */
  1138. inode = new_inode(sb);
  1139. if (!inode)
  1140. goto out;
  1141. /* Common stuff */
  1142. ei = PROC_I(inode);
  1143. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  1144. inode->i_ino = fake_ino(task->pid, ino);
  1145. /*
  1146. * grab the reference to task.
  1147. */
  1148. ei->pid = get_pid(task->pids[PIDTYPE_PID].pid);
  1149. if (!ei->pid)
  1150. goto out_unlock;
  1151. inode->i_uid = 0;
  1152. inode->i_gid = 0;
  1153. if (task_dumpable(task)) {
  1154. inode->i_uid = task->euid;
  1155. inode->i_gid = task->egid;
  1156. }
  1157. security_task_to_inode(task, inode);
  1158. out:
  1159. return inode;
  1160. out_unlock:
  1161. iput(inode);
  1162. return NULL;
  1163. }
  1164. /* dentry stuff */
  1165. /*
  1166. * Exceptional case: normally we are not allowed to unhash a busy
  1167. * directory. In this case, however, we can do it - no aliasing problems
  1168. * due to the way we treat inodes.
  1169. *
  1170. * Rewrite the inode's ownerships here because the owning task may have
  1171. * performed a setuid(), etc.
  1172. *
  1173. * Before the /proc/pid/status file was created the only way to read
  1174. * the effective uid of a /process was to stat /proc/pid. Reading
  1175. * /proc/pid/status is slow enough that procps and other packages
  1176. * kept stating /proc/pid. To keep the rules in /proc simple I have
  1177. * made this apply to all per process world readable and executable
  1178. * directories.
  1179. */
  1180. static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
  1181. {
  1182. struct inode *inode = dentry->d_inode;
  1183. struct task_struct *task = get_proc_task(inode);
  1184. if (task) {
  1185. if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
  1186. task_dumpable(task)) {
  1187. inode->i_uid = task->euid;
  1188. inode->i_gid = task->egid;
  1189. } else {
  1190. inode->i_uid = 0;
  1191. inode->i_gid = 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. }