array.c 20 KB

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