array.c 21 KB

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