base.c 54 KB

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