base.c 54 KB

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