base.c 53 KB

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