base.c 54 KB

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