base.c 55 KB

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