array.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  1. /*
  2. * linux/fs/proc/array.c
  3. *
  4. * Copyright (C) 1992 by Linus Torvalds
  5. * based on ideas by Darren Senn
  6. *
  7. * Fixes:
  8. * Michael. K. Johnson: stat,statm extensions.
  9. * <johnsonm@stolaf.edu>
  10. *
  11. * Pauline Middelink : Made cmdline,envline only break at '\0's, to
  12. * make sure SET_PROCTITLE works. Also removed
  13. * bad '!' which forced address recalculation for
  14. * EVERY character on the current page.
  15. * <middelin@polyware.iaf.nl>
  16. *
  17. * Danny ter Haar : added cpuinfo
  18. * <dth@cistron.nl>
  19. *
  20. * Alessandro Rubini : profile extension.
  21. * <rubini@ipvvis.unipv.it>
  22. *
  23. * Jeff Tranter : added BogoMips field to cpuinfo
  24. * <Jeff_Tranter@Mitel.COM>
  25. *
  26. * Bruno Haible : remove 4K limit for the maps file
  27. * <haible@ma2s2.mathematik.uni-karlsruhe.de>
  28. *
  29. * Yves Arrouye : remove removal of trailing spaces in get_array.
  30. * <Yves.Arrouye@marin.fdn.fr>
  31. *
  32. * Jerome Forissier : added per-CPU time information to /proc/stat
  33. * and /proc/<pid>/cpu extension
  34. * <forissier@isia.cma.fr>
  35. * - Incorporation and non-SMP safe operation
  36. * of forissier patch in 2.1.78 by
  37. * Hans Marcus <crowbar@concepts.nl>
  38. *
  39. * aeb@cwi.nl : /proc/partitions
  40. *
  41. *
  42. * Alan Cox : security fixes.
  43. * <alan@lxorguk.ukuu.org.uk>
  44. *
  45. * Al Viro : safe handling of mm_struct
  46. *
  47. * Gerhard Wichert : added BIGMEM support
  48. * Siemens AG <Gerhard.Wichert@pdb.siemens.de>
  49. *
  50. * Al Viro & Jeff Garzik : moved most of the thing into base.c and
  51. * : proc_misc.c. The rest may eventually go into
  52. * : base.c too.
  53. */
  54. #include <linux/types.h>
  55. #include <linux/errno.h>
  56. #include <linux/time.h>
  57. #include <linux/kernel.h>
  58. #include <linux/kernel_stat.h>
  59. #include <linux/tty.h>
  60. #include <linux/string.h>
  61. #include <linux/mman.h>
  62. #include <linux/proc_fs.h>
  63. #include <linux/ioport.h>
  64. #include <linux/uaccess.h>
  65. #include <linux/io.h>
  66. #include <linux/mm.h>
  67. #include <linux/hugetlb.h>
  68. #include <linux/pagemap.h>
  69. #include <linux/swap.h>
  70. #include <linux/smp.h>
  71. #include <linux/signal.h>
  72. #include <linux/highmem.h>
  73. #include <linux/file.h>
  74. #include <linux/fdtable.h>
  75. #include <linux/times.h>
  76. #include <linux/cpuset.h>
  77. #include <linux/rcupdate.h>
  78. #include <linux/delayacct.h>
  79. #include <linux/seq_file.h>
  80. #include <linux/pid_namespace.h>
  81. #include <linux/prctl.h>
  82. #include <linux/ptrace.h>
  83. #include <linux/tracehook.h>
  84. #include <linux/string_helpers.h>
  85. #include <linux/user_namespace.h>
  86. #include <asm/pgtable.h>
  87. #include <asm/processor.h>
  88. #include "internal.h"
  89. static inline void task_name(struct seq_file *m, struct task_struct *p)
  90. {
  91. char *buf;
  92. size_t size;
  93. char tcomm[sizeof(p->comm)];
  94. int ret;
  95. get_task_comm(tcomm, p);
  96. seq_puts(m, "Name:\t");
  97. size = seq_get_buf(m, &buf);
  98. ret = string_escape_str(tcomm, buf, size, ESCAPE_SPACE | ESCAPE_SPECIAL, "\n\\");
  99. seq_commit(m, ret < size ? ret : -1);
  100. seq_putc(m, '\n');
  101. }
  102. /*
  103. * The task state array is a strange "bitmap" of
  104. * reasons to sleep. Thus "running" is zero, and
  105. * you can test for combinations of others with
  106. * simple bit tests.
  107. */
  108. static const char * const task_state_array[] = {
  109. "R (running)", /* 0 */
  110. "S (sleeping)", /* 1 */
  111. "D (disk sleep)", /* 2 */
  112. "T (stopped)", /* 4 */
  113. "t (tracing stop)", /* 8 */
  114. "X (dead)", /* 16 */
  115. "Z (zombie)", /* 32 */
  116. };
  117. static inline const char *get_task_state(struct task_struct *tsk)
  118. {
  119. unsigned int state = (tsk->state | tsk->exit_state) & TASK_REPORT;
  120. /*
  121. * Parked tasks do not run; they sit in __kthread_parkme().
  122. * Without this check, we would report them as running, which is
  123. * clearly wrong, so we report them as sleeping instead.
  124. */
  125. if (tsk->state == TASK_PARKED)
  126. state = TASK_INTERRUPTIBLE;
  127. BUILD_BUG_ON(1 + ilog2(TASK_REPORT) != ARRAY_SIZE(task_state_array)-1);
  128. return task_state_array[fls(state)];
  129. }
  130. static inline void task_state(struct seq_file *m, struct pid_namespace *ns,
  131. struct pid *pid, struct task_struct *p)
  132. {
  133. struct user_namespace *user_ns = seq_user_ns(m);
  134. struct group_info *group_info;
  135. int g;
  136. struct task_struct *tracer;
  137. const struct cred *cred;
  138. pid_t ppid, tpid = 0, tgid, ngid;
  139. unsigned int max_fds = 0;
  140. rcu_read_lock();
  141. ppid = pid_alive(p) ?
  142. task_tgid_nr_ns(rcu_dereference(p->real_parent), ns) : 0;
  143. tracer = ptrace_parent(p);
  144. if (tracer)
  145. tpid = task_pid_nr_ns(tracer, ns);
  146. tgid = task_tgid_nr_ns(p, ns);
  147. ngid = task_numa_group_id(p);
  148. cred = get_task_cred(p);
  149. task_lock(p);
  150. if (p->files)
  151. max_fds = files_fdtable(p->files)->max_fds;
  152. task_unlock(p);
  153. rcu_read_unlock();
  154. seq_printf(m,
  155. "State:\t%s\n"
  156. "Tgid:\t%d\n"
  157. "Ngid:\t%d\n"
  158. "Pid:\t%d\n"
  159. "PPid:\t%d\n"
  160. "TracerPid:\t%d\n"
  161. "Uid:\t%d\t%d\t%d\t%d\n"
  162. "Gid:\t%d\t%d\t%d\t%d\n"
  163. "FDSize:\t%d\nGroups:\t",
  164. get_task_state(p),
  165. tgid, ngid, pid_nr_ns(pid, ns), ppid, tpid,
  166. from_kuid_munged(user_ns, cred->uid),
  167. from_kuid_munged(user_ns, cred->euid),
  168. from_kuid_munged(user_ns, cred->suid),
  169. from_kuid_munged(user_ns, cred->fsuid),
  170. from_kgid_munged(user_ns, cred->gid),
  171. from_kgid_munged(user_ns, cred->egid),
  172. from_kgid_munged(user_ns, cred->sgid),
  173. from_kgid_munged(user_ns, cred->fsgid),
  174. max_fds);
  175. group_info = cred->group_info;
  176. for (g = 0; g < group_info->ngroups; g++)
  177. seq_printf(m, "%d ",
  178. from_kgid_munged(user_ns, GROUP_AT(group_info, g)));
  179. put_cred(cred);
  180. #ifdef CONFIG_PID_NS
  181. seq_puts(m, "\nNStgid:");
  182. for (g = ns->level; g <= pid->level; g++)
  183. seq_printf(m, "\t%d",
  184. task_tgid_nr_ns(p, pid->numbers[g].ns));
  185. seq_puts(m, "\nNSpid:");
  186. for (g = ns->level; g <= pid->level; g++)
  187. seq_printf(m, "\t%d",
  188. task_pid_nr_ns(p, pid->numbers[g].ns));
  189. seq_puts(m, "\nNSpgid:");
  190. for (g = ns->level; g <= pid->level; g++)
  191. seq_printf(m, "\t%d",
  192. task_pgrp_nr_ns(p, pid->numbers[g].ns));
  193. seq_puts(m, "\nNSsid:");
  194. for (g = ns->level; g <= pid->level; g++)
  195. seq_printf(m, "\t%d",
  196. task_session_nr_ns(p, pid->numbers[g].ns));
  197. #endif
  198. seq_putc(m, '\n');
  199. }
  200. void render_sigset_t(struct seq_file *m, const char *header,
  201. sigset_t *set)
  202. {
  203. int i;
  204. seq_puts(m, header);
  205. i = _NSIG;
  206. do {
  207. int x = 0;
  208. i -= 4;
  209. if (sigismember(set, i+1)) x |= 1;
  210. if (sigismember(set, i+2)) x |= 2;
  211. if (sigismember(set, i+3)) x |= 4;
  212. if (sigismember(set, i+4)) x |= 8;
  213. seq_printf(m, "%x", x);
  214. } while (i >= 4);
  215. seq_putc(m, '\n');
  216. }
  217. static void collect_sigign_sigcatch(struct task_struct *p, sigset_t *ign,
  218. sigset_t *catch)
  219. {
  220. struct k_sigaction *k;
  221. int i;
  222. k = p->sighand->action;
  223. for (i = 1; i <= _NSIG; ++i, ++k) {
  224. if (k->sa.sa_handler == SIG_IGN)
  225. sigaddset(ign, i);
  226. else if (k->sa.sa_handler != SIG_DFL)
  227. sigaddset(catch, i);
  228. }
  229. }
  230. static inline void task_sig(struct seq_file *m, struct task_struct *p)
  231. {
  232. unsigned long flags;
  233. sigset_t pending, shpending, blocked, ignored, caught;
  234. int num_threads = 0;
  235. unsigned long qsize = 0;
  236. unsigned long qlim = 0;
  237. sigemptyset(&pending);
  238. sigemptyset(&shpending);
  239. sigemptyset(&blocked);
  240. sigemptyset(&ignored);
  241. sigemptyset(&caught);
  242. if (lock_task_sighand(p, &flags)) {
  243. pending = p->pending.signal;
  244. shpending = p->signal->shared_pending.signal;
  245. blocked = p->blocked;
  246. collect_sigign_sigcatch(p, &ignored, &caught);
  247. num_threads = get_nr_threads(p);
  248. rcu_read_lock(); /* FIXME: is this correct? */
  249. qsize = atomic_read(&__task_cred(p)->user->sigpending);
  250. rcu_read_unlock();
  251. qlim = task_rlimit(p, RLIMIT_SIGPENDING);
  252. unlock_task_sighand(p, &flags);
  253. }
  254. seq_printf(m, "Threads:\t%d\n", num_threads);
  255. seq_printf(m, "SigQ:\t%lu/%lu\n", qsize, qlim);
  256. /* render them all */
  257. render_sigset_t(m, "SigPnd:\t", &pending);
  258. render_sigset_t(m, "ShdPnd:\t", &shpending);
  259. render_sigset_t(m, "SigBlk:\t", &blocked);
  260. render_sigset_t(m, "SigIgn:\t", &ignored);
  261. render_sigset_t(m, "SigCgt:\t", &caught);
  262. }
  263. static void render_cap_t(struct seq_file *m, const char *header,
  264. kernel_cap_t *a)
  265. {
  266. unsigned __capi;
  267. seq_puts(m, header);
  268. CAP_FOR_EACH_U32(__capi) {
  269. seq_printf(m, "%08x",
  270. a->cap[CAP_LAST_U32 - __capi]);
  271. }
  272. seq_putc(m, '\n');
  273. }
  274. static inline void task_cap(struct seq_file *m, struct task_struct *p)
  275. {
  276. const struct cred *cred;
  277. kernel_cap_t cap_inheritable, cap_permitted, cap_effective,
  278. cap_bset, cap_ambient;
  279. rcu_read_lock();
  280. cred = __task_cred(p);
  281. cap_inheritable = cred->cap_inheritable;
  282. cap_permitted = cred->cap_permitted;
  283. cap_effective = cred->cap_effective;
  284. cap_bset = cred->cap_bset;
  285. cap_ambient = cred->cap_ambient;
  286. rcu_read_unlock();
  287. render_cap_t(m, "CapInh:\t", &cap_inheritable);
  288. render_cap_t(m, "CapPrm:\t", &cap_permitted);
  289. render_cap_t(m, "CapEff:\t", &cap_effective);
  290. render_cap_t(m, "CapBnd:\t", &cap_bset);
  291. render_cap_t(m, "CapAmb:\t", &cap_ambient);
  292. }
  293. static inline void task_seccomp(struct seq_file *m, struct task_struct *p)
  294. {
  295. #ifdef CONFIG_SECCOMP
  296. seq_printf(m, "Seccomp:\t%d\n", p->seccomp.mode);
  297. #endif
  298. seq_printf(m, "Speculation_Store_Bypass:\t");
  299. switch (arch_prctl_spec_ctrl_get(p, PR_SPEC_STORE_BYPASS)) {
  300. case -EINVAL:
  301. seq_printf(m, "unknown");
  302. break;
  303. case PR_SPEC_NOT_AFFECTED:
  304. seq_printf(m, "not vulnerable");
  305. break;
  306. case PR_SPEC_PRCTL | PR_SPEC_FORCE_DISABLE:
  307. seq_printf(m, "thread force mitigated");
  308. break;
  309. case PR_SPEC_PRCTL | PR_SPEC_DISABLE:
  310. seq_printf(m, "thread mitigated");
  311. break;
  312. case PR_SPEC_PRCTL | PR_SPEC_ENABLE:
  313. seq_printf(m, "thread vulnerable");
  314. break;
  315. case PR_SPEC_DISABLE:
  316. seq_printf(m, "globally mitigated");
  317. break;
  318. default:
  319. seq_printf(m, "vulnerable");
  320. break;
  321. }
  322. seq_putc(m, '\n');
  323. }
  324. static inline void task_context_switch_counts(struct seq_file *m,
  325. struct task_struct *p)
  326. {
  327. seq_printf(m, "voluntary_ctxt_switches:\t%lu\n"
  328. "nonvoluntary_ctxt_switches:\t%lu\n",
  329. p->nvcsw,
  330. p->nivcsw);
  331. }
  332. static void task_cpus_allowed(struct seq_file *m, struct task_struct *task)
  333. {
  334. seq_printf(m, "Cpus_allowed:\t%*pb\n",
  335. cpumask_pr_args(&task->cpus_allowed));
  336. seq_printf(m, "Cpus_allowed_list:\t%*pbl\n",
  337. cpumask_pr_args(&task->cpus_allowed));
  338. }
  339. int proc_pid_status(struct seq_file *m, struct pid_namespace *ns,
  340. struct pid *pid, struct task_struct *task)
  341. {
  342. struct mm_struct *mm = get_task_mm(task);
  343. task_name(m, task);
  344. task_state(m, ns, pid, task);
  345. if (mm) {
  346. task_mem(m, mm);
  347. mmput(mm);
  348. }
  349. task_sig(m, task);
  350. task_cap(m, task);
  351. task_seccomp(m, task);
  352. task_cpus_allowed(m, task);
  353. cpuset_task_status_allowed(m, task);
  354. task_context_switch_counts(m, task);
  355. return 0;
  356. }
  357. static int do_task_stat(struct seq_file *m, struct pid_namespace *ns,
  358. struct pid *pid, struct task_struct *task, int whole)
  359. {
  360. unsigned long vsize, eip, esp, wchan = 0;
  361. int priority, nice;
  362. int tty_pgrp = -1, tty_nr = 0;
  363. sigset_t sigign, sigcatch;
  364. char state;
  365. pid_t ppid = 0, pgid = -1, sid = -1;
  366. int num_threads = 0;
  367. int permitted;
  368. struct mm_struct *mm;
  369. unsigned long long start_time;
  370. unsigned long cmin_flt = 0, cmaj_flt = 0;
  371. unsigned long min_flt = 0, maj_flt = 0;
  372. cputime_t cutime, cstime, utime, stime;
  373. cputime_t cgtime, gtime;
  374. unsigned long rsslim = 0;
  375. char tcomm[sizeof(task->comm)];
  376. unsigned long flags;
  377. state = *get_task_state(task);
  378. vsize = eip = esp = 0;
  379. permitted = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS | PTRACE_MODE_NOAUDIT);
  380. mm = get_task_mm(task);
  381. if (mm) {
  382. vsize = task_vsize(mm);
  383. if (permitted) {
  384. eip = KSTK_EIP(task);
  385. esp = KSTK_ESP(task);
  386. }
  387. }
  388. get_task_comm(tcomm, task);
  389. sigemptyset(&sigign);
  390. sigemptyset(&sigcatch);
  391. cutime = cstime = utime = stime = 0;
  392. cgtime = gtime = 0;
  393. if (lock_task_sighand(task, &flags)) {
  394. struct signal_struct *sig = task->signal;
  395. if (sig->tty) {
  396. struct pid *pgrp = tty_get_pgrp(sig->tty);
  397. tty_pgrp = pid_nr_ns(pgrp, ns);
  398. put_pid(pgrp);
  399. tty_nr = new_encode_dev(tty_devnum(sig->tty));
  400. }
  401. num_threads = get_nr_threads(task);
  402. collect_sigign_sigcatch(task, &sigign, &sigcatch);
  403. cmin_flt = sig->cmin_flt;
  404. cmaj_flt = sig->cmaj_flt;
  405. cutime = sig->cutime;
  406. cstime = sig->cstime;
  407. cgtime = sig->cgtime;
  408. rsslim = ACCESS_ONCE(sig->rlim[RLIMIT_RSS].rlim_cur);
  409. /* add up live thread stats at the group level */
  410. if (whole) {
  411. struct task_struct *t = task;
  412. do {
  413. min_flt += t->min_flt;
  414. maj_flt += t->maj_flt;
  415. gtime += task_gtime(t);
  416. } while_each_thread(task, t);
  417. min_flt += sig->min_flt;
  418. maj_flt += sig->maj_flt;
  419. thread_group_cputime_adjusted(task, &utime, &stime);
  420. gtime += sig->gtime;
  421. }
  422. sid = task_session_nr_ns(task, ns);
  423. ppid = task_tgid_nr_ns(task->real_parent, ns);
  424. pgid = task_pgrp_nr_ns(task, ns);
  425. unlock_task_sighand(task, &flags);
  426. }
  427. if (permitted && (!whole || num_threads < 2))
  428. wchan = get_wchan(task);
  429. if (!whole) {
  430. min_flt = task->min_flt;
  431. maj_flt = task->maj_flt;
  432. task_cputime_adjusted(task, &utime, &stime);
  433. gtime = task_gtime(task);
  434. }
  435. /* scale priority and nice values from timeslices to -20..20 */
  436. /* to make it look like a "normal" Unix priority/nice value */
  437. priority = task_prio(task);
  438. nice = task_nice(task);
  439. /* convert nsec -> ticks */
  440. start_time = nsec_to_clock_t(task->real_start_time);
  441. seq_printf(m, "%d (%s) %c", pid_nr_ns(pid, ns), tcomm, state);
  442. seq_put_decimal_ll(m, ' ', ppid);
  443. seq_put_decimal_ll(m, ' ', pgid);
  444. seq_put_decimal_ll(m, ' ', sid);
  445. seq_put_decimal_ll(m, ' ', tty_nr);
  446. seq_put_decimal_ll(m, ' ', tty_pgrp);
  447. seq_put_decimal_ull(m, ' ', task->flags);
  448. seq_put_decimal_ull(m, ' ', min_flt);
  449. seq_put_decimal_ull(m, ' ', cmin_flt);
  450. seq_put_decimal_ull(m, ' ', maj_flt);
  451. seq_put_decimal_ull(m, ' ', cmaj_flt);
  452. seq_put_decimal_ull(m, ' ', cputime_to_clock_t(utime));
  453. seq_put_decimal_ull(m, ' ', cputime_to_clock_t(stime));
  454. seq_put_decimal_ll(m, ' ', cputime_to_clock_t(cutime));
  455. seq_put_decimal_ll(m, ' ', cputime_to_clock_t(cstime));
  456. seq_put_decimal_ll(m, ' ', priority);
  457. seq_put_decimal_ll(m, ' ', nice);
  458. seq_put_decimal_ll(m, ' ', num_threads);
  459. seq_put_decimal_ull(m, ' ', 0);
  460. seq_put_decimal_ull(m, ' ', start_time);
  461. seq_put_decimal_ull(m, ' ', vsize);
  462. seq_put_decimal_ull(m, ' ', mm ? get_mm_rss(mm) : 0);
  463. seq_put_decimal_ull(m, ' ', rsslim);
  464. seq_put_decimal_ull(m, ' ', mm ? (permitted ? mm->start_code : 1) : 0);
  465. seq_put_decimal_ull(m, ' ', mm ? (permitted ? mm->end_code : 1) : 0);
  466. seq_put_decimal_ull(m, ' ', (permitted && mm) ? mm->start_stack : 0);
  467. seq_put_decimal_ull(m, ' ', esp);
  468. seq_put_decimal_ull(m, ' ', eip);
  469. /* The signal information here is obsolete.
  470. * It must be decimal for Linux 2.0 compatibility.
  471. * Use /proc/#/status for real-time signals.
  472. */
  473. seq_put_decimal_ull(m, ' ', task->pending.signal.sig[0] & 0x7fffffffUL);
  474. seq_put_decimal_ull(m, ' ', task->blocked.sig[0] & 0x7fffffffUL);
  475. seq_put_decimal_ull(m, ' ', sigign.sig[0] & 0x7fffffffUL);
  476. seq_put_decimal_ull(m, ' ', sigcatch.sig[0] & 0x7fffffffUL);
  477. /*
  478. * We used to output the absolute kernel address, but that's an
  479. * information leak - so instead we show a 0/1 flag here, to signal
  480. * to user-space whether there's a wchan field in /proc/PID/wchan.
  481. *
  482. * This works with older implementations of procps as well.
  483. */
  484. if (wchan)
  485. seq_puts(m, " 1");
  486. else
  487. seq_puts(m, " 0");
  488. seq_put_decimal_ull(m, ' ', 0);
  489. seq_put_decimal_ull(m, ' ', 0);
  490. seq_put_decimal_ll(m, ' ', task->exit_signal);
  491. seq_put_decimal_ll(m, ' ', task_cpu(task));
  492. seq_put_decimal_ull(m, ' ', task->rt_priority);
  493. seq_put_decimal_ull(m, ' ', task->policy);
  494. seq_put_decimal_ull(m, ' ', delayacct_blkio_ticks(task));
  495. seq_put_decimal_ull(m, ' ', cputime_to_clock_t(gtime));
  496. seq_put_decimal_ll(m, ' ', cputime_to_clock_t(cgtime));
  497. if (mm && permitted) {
  498. seq_put_decimal_ull(m, ' ', mm->start_data);
  499. seq_put_decimal_ull(m, ' ', mm->end_data);
  500. seq_put_decimal_ull(m, ' ', mm->start_brk);
  501. seq_put_decimal_ull(m, ' ', mm->arg_start);
  502. seq_put_decimal_ull(m, ' ', mm->arg_end);
  503. seq_put_decimal_ull(m, ' ', mm->env_start);
  504. seq_put_decimal_ull(m, ' ', mm->env_end);
  505. } else
  506. seq_printf(m, " 0 0 0 0 0 0 0");
  507. if (permitted)
  508. seq_put_decimal_ll(m, ' ', task->exit_code);
  509. else
  510. seq_put_decimal_ll(m, ' ', 0);
  511. seq_putc(m, '\n');
  512. if (mm)
  513. mmput(mm);
  514. return 0;
  515. }
  516. int proc_tid_stat(struct seq_file *m, struct pid_namespace *ns,
  517. struct pid *pid, struct task_struct *task)
  518. {
  519. return do_task_stat(m, ns, pid, task, 0);
  520. }
  521. int proc_tgid_stat(struct seq_file *m, struct pid_namespace *ns,
  522. struct pid *pid, struct task_struct *task)
  523. {
  524. return do_task_stat(m, ns, pid, task, 1);
  525. }
  526. int proc_pid_statm(struct seq_file *m, struct pid_namespace *ns,
  527. struct pid *pid, struct task_struct *task)
  528. {
  529. unsigned long size = 0, resident = 0, shared = 0, text = 0, data = 0;
  530. struct mm_struct *mm = get_task_mm(task);
  531. if (mm) {
  532. size = task_statm(mm, &shared, &text, &data, &resident);
  533. mmput(mm);
  534. }
  535. /*
  536. * For quick read, open code by putting numbers directly
  537. * expected format is
  538. * seq_printf(m, "%lu %lu %lu %lu 0 %lu 0\n",
  539. * size, resident, shared, text, data);
  540. */
  541. seq_put_decimal_ull(m, 0, size);
  542. seq_put_decimal_ull(m, ' ', resident);
  543. seq_put_decimal_ull(m, ' ', shared);
  544. seq_put_decimal_ull(m, ' ', text);
  545. seq_put_decimal_ull(m, ' ', 0);
  546. seq_put_decimal_ull(m, ' ', data);
  547. seq_put_decimal_ull(m, ' ', 0);
  548. seq_putc(m, '\n');
  549. return 0;
  550. }
  551. #ifdef CONFIG_PROC_CHILDREN
  552. static struct pid *
  553. get_children_pid(struct inode *inode, struct pid *pid_prev, loff_t pos)
  554. {
  555. struct task_struct *start, *task;
  556. struct pid *pid = NULL;
  557. read_lock(&tasklist_lock);
  558. start = pid_task(proc_pid(inode), PIDTYPE_PID);
  559. if (!start)
  560. goto out;
  561. /*
  562. * Lets try to continue searching first, this gives
  563. * us significant speedup on children-rich processes.
  564. */
  565. if (pid_prev) {
  566. task = pid_task(pid_prev, PIDTYPE_PID);
  567. if (task && task->real_parent == start &&
  568. !(list_empty(&task->sibling))) {
  569. if (list_is_last(&task->sibling, &start->children))
  570. goto out;
  571. task = list_first_entry(&task->sibling,
  572. struct task_struct, sibling);
  573. pid = get_pid(task_pid(task));
  574. goto out;
  575. }
  576. }
  577. /*
  578. * Slow search case.
  579. *
  580. * We might miss some children here if children
  581. * are exited while we were not holding the lock,
  582. * but it was never promised to be accurate that
  583. * much.
  584. *
  585. * "Just suppose that the parent sleeps, but N children
  586. * exit after we printed their tids. Now the slow paths
  587. * skips N extra children, we miss N tasks." (c)
  588. *
  589. * So one need to stop or freeze the leader and all
  590. * its children to get a precise result.
  591. */
  592. list_for_each_entry(task, &start->children, sibling) {
  593. if (pos-- == 0) {
  594. pid = get_pid(task_pid(task));
  595. break;
  596. }
  597. }
  598. out:
  599. read_unlock(&tasklist_lock);
  600. return pid;
  601. }
  602. static int children_seq_show(struct seq_file *seq, void *v)
  603. {
  604. struct inode *inode = seq->private;
  605. pid_t pid;
  606. pid = pid_nr_ns(v, inode->i_sb->s_fs_info);
  607. seq_printf(seq, "%d ", pid);
  608. return 0;
  609. }
  610. static void *children_seq_start(struct seq_file *seq, loff_t *pos)
  611. {
  612. return get_children_pid(seq->private, NULL, *pos);
  613. }
  614. static void *children_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  615. {
  616. struct pid *pid;
  617. pid = get_children_pid(seq->private, v, *pos + 1);
  618. put_pid(v);
  619. ++*pos;
  620. return pid;
  621. }
  622. static void children_seq_stop(struct seq_file *seq, void *v)
  623. {
  624. put_pid(v);
  625. }
  626. static const struct seq_operations children_seq_ops = {
  627. .start = children_seq_start,
  628. .next = children_seq_next,
  629. .stop = children_seq_stop,
  630. .show = children_seq_show,
  631. };
  632. static int children_seq_open(struct inode *inode, struct file *file)
  633. {
  634. struct seq_file *m;
  635. int ret;
  636. ret = seq_open(file, &children_seq_ops);
  637. if (ret)
  638. return ret;
  639. m = file->private_data;
  640. m->private = inode;
  641. return ret;
  642. }
  643. int children_seq_release(struct inode *inode, struct file *file)
  644. {
  645. seq_release(inode, file);
  646. return 0;
  647. }
  648. const struct file_operations proc_tid_children_operations = {
  649. .open = children_seq_open,
  650. .read = seq_read,
  651. .llseek = seq_lseek,
  652. .release = children_seq_release,
  653. };
  654. #endif /* CONFIG_PROC_CHILDREN */