array.c 19 KB

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