array.c 20 KB

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