array.c 20 KB

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