base.c 50 KB

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