array.c 18 KB

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