base.c 51 KB

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