array.c 18 KB

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