base.c 54 KB

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