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