base.c 53 KB

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