array.c 21 KB

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