base.c 54 KB

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