array.c 20 KB

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