| 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 |
* <[email protected]> |
| 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 |
* <[email protected]> |
| 17 |
* |
| 18 |
* Danny ter Haar : added cpuinfo |
| 19 |
* <[email protected]> |
| 20 |
* |
| 21 |
* Alessandro Rubini : profile extension. |
| 22 |
* <[email protected]> |
| 23 |
* |
| 24 |
* Jeff Tranter : added BogoMips field to cpuinfo |
| 25 |
* <[email protected]> |
| 26 |
* |
| 27 |
* Bruno Haible : remove 4K limit for the maps file |
| 28 |
* <[email protected]> |
| 29 |
* |
| 30 |
* Yves Arrouye : remove removal of trailing spaces in get_array. |
| 31 |
* <[email protected]> |
| 32 |
* |
| 33 |
* Jerome Forissier : added per-CPU time information to /proc/stat |
| 34 |
* and /proc/<pid>/cpu extension |
| 35 |
* <[email protected]> |
| 36 |
* - Incorporation and non-SMP safe operation |
| 37 |
* of forissier patch in 2.1.78 by |
| 38 |
* Hans Marcus <[email protected]> |
| 39 |
* |
| 40 |
* [email protected] : /proc/partitions |
| 41 |
* |
| 42 |
* |
| 43 |
* Alan Cox : security fixes. |
| 44 |
* <[email protected]> |
| 45 |
* |
| 46 |
* Al Viro : safe handling of mm_struct |
| 47 |
* |
| 48 |
* Gerhard Wichert : added BIGMEM support |
| 49 |
* Siemens AG <[email protected]> |
| 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 |
|
| 56 |
#include <linux/types.h> |
| 57 |
#include <linux/errno.h> |
| 58 |
#include <linux/hex.h> |
| 59 |
#include <linux/time.h> |
| 60 |
#include <linux/time_namespace.h> |
| 61 |
#include <linux/kernel.h> |
| 62 |
#include <linux/kernel_stat.h> |
| 63 |
#include <linux/tty.h> |
| 64 |
#include <linux/string.h> |
| 65 |
#include <linux/mman.h> |
| 66 |
#include <linux/sched/mm.h> |
| 67 |
#include <linux/sched/numa_balancing.h> |
| 68 |
#include <linux/sched/task_stack.h> |
| 69 |
#include <linux/sched/task.h> |
| 70 |
#include <linux/sched/cputime.h> |
| 71 |
#include <linux/proc_fs.h> |
| 72 |
#include <linux/ioport.h> |
| 73 |
#include <linux/io.h> |
| 74 |
#include <linux/mm.h> |
| 75 |
#include <linux/hugetlb.h> |
| 76 |
#include <linux/pagemap.h> |
| 77 |
#include <linux/swap.h> |
| 78 |
#include <linux/smp.h> |
| 79 |
#include <linux/signal.h> |
| 80 |
#include <linux/highmem.h> |
| 81 |
#include <linux/file.h> |
| 82 |
#include <linux/fdtable.h> |
| 83 |
#include <linux/times.h> |
| 84 |
#include <linux/cpuset.h> |
| 85 |
#include <linux/rcupdate.h> |
| 86 |
#include <linux/delayacct.h> |
| 87 |
#include <linux/seq_file.h> |
| 88 |
#include <linux/pid_namespace.h> |
| 89 |
#include <linux/prctl.h> |
| 90 |
#include <linux/ptrace.h> |
| 91 |
#include <linux/string_helpers.h> |
| 92 |
#include <linux/user_namespace.h> |
| 93 |
#include <linux/fs_struct.h> |
| 94 |
#include <linux/kthread.h> |
| 95 |
#include <linux/mmu_context.h> |
| 96 |
|
| 97 |
#include <asm/processor.h> |
| 98 |
#include "internal.h" |
| 99 |
|
| 100 |
void proc_task_name(struct seq_file *m, struct task_struct *p, bool escape) |
| 101 |
{ |
| 102 |
char tcomm[64]; |
| 103 |
|
| 104 |
/* |
| 105 |
* Test before PF_KTHREAD because all workqueue worker threads are |
| 106 |
* kernel threads. |
| 107 |
*/ |
| 108 |
if (p->flags & PF_WQ_WORKER) |
| 109 |
wq_worker_comm(tcomm, sizeof(tcomm), p); |
| 110 |
else if (p->flags & PF_KTHREAD) |
| 111 |
get_kthread_comm(tcomm, sizeof(tcomm), p); |
| 112 |
else |
| 113 |
get_task_comm(tcomm, p); |
| 114 |
|
| 115 |
if (escape) |
| 116 |
seq_escape_str(m, tcomm, ESCAPE_SPACE | ESCAPE_SPECIAL, "\n\\"); |
| 117 |
else |
| 118 |
seq_printf(m, "%.64s", tcomm); |
| 119 |
} |
| 120 |
|
| 121 |
/* |
| 122 |
* The task state array is a strange "bitmap" of |
| 123 |
* reasons to sleep. Thus "running" is zero, and |
| 124 |
* you can test for combinations of others with |
| 125 |
* simple bit tests. |
| 126 |
*/ |
| 127 |
static const char * const task_state_array[] = { |
| 128 |
|
| 129 |
/* states in TASK_REPORT: */ |
| 130 |
"R (running)", /* 0x00 */ |
| 131 |
"S (sleeping)", /* 0x01 */ |
| 132 |
"D (disk sleep)", /* 0x02 */ |
| 133 |
"T (stopped)", /* 0x04 */ |
| 134 |
"t (tracing stop)", /* 0x08 */ |
| 135 |
"X (dead)", /* 0x10 */ |
| 136 |
"Z (zombie)", /* 0x20 */ |
| 137 |
"P (parked)", /* 0x40 */ |
| 138 |
|
| 139 |
/* states beyond TASK_REPORT: */ |
| 140 |
"I (idle)", /* 0x80 */ |
| 141 |
}; |
| 142 |
|
| 143 |
static inline const char *get_task_state(struct task_struct *tsk) |
| 144 |
{ |
| 145 |
BUILD_BUG_ON(1 + ilog2(TASK_REPORT_MAX) != ARRAY_SIZE(task_state_array)); |
| 146 |
return task_state_array[task_state_index(tsk)]; |
| 147 |
} |
| 148 |
|
| 149 |
static inline void task_state(struct seq_file *m, struct pid_namespace *ns, |
| 150 |
struct pid *pid, struct task_struct *p) |
| 151 |
{ |
| 152 |
struct user_namespace *user_ns = seq_user_ns(m); |
| 153 |
struct group_info *group_info; |
| 154 |
int g, umask = -1; |
| 155 |
struct task_struct *tracer; |
| 156 |
const struct cred *cred; |
| 157 |
pid_t ppid, tpid = 0, tgid, ngid; |
| 158 |
unsigned int max_fds = 0; |
| 159 |
|
| 160 |
rcu_read_lock(); |
| 161 |
tracer = ptrace_parent(p); |
| 162 |
if (tracer) |
| 163 |
tpid = task_pid_nr_ns(tracer, ns); |
| 164 |
|
| 165 |
ppid = task_ppid_nr_ns(p, ns); |
| 166 |
tgid = task_tgid_nr_ns(p, ns); |
| 167 |
ngid = task_numa_group_id(p); |
| 168 |
cred = get_task_cred(p); |
| 169 |
|
| 170 |
task_lock(p); |
| 171 |
if (p->fs) |
| 172 |
umask = p->fs->umask; |
| 173 |
if (p->files) |
| 174 |
max_fds = files_fdtable(p->files)->max_fds; |
| 175 |
task_unlock(p); |
| 176 |
rcu_read_unlock(); |
| 177 |
|
| 178 |
if (umask >= 0) |
| 179 |
seq_printf(m, "Umask:\t%#04o\n", umask); |
| 180 |
seq_puts(m, "State:\t"); |
| 181 |
seq_puts(m, get_task_state(p)); |
| 182 |
|
| 183 |
seq_put_decimal_ull(m, "\nTgid:\t", tgid); |
| 184 |
seq_put_decimal_ull(m, "\nNgid:\t", ngid); |
| 185 |
seq_put_decimal_ull(m, "\nPid:\t", pid_nr_ns(pid, ns)); |
| 186 |
seq_put_decimal_ull(m, "\nPPid:\t", ppid); |
| 187 |
seq_put_decimal_ull(m, "\nTracerPid:\t", tpid); |
| 188 |
seq_put_decimal_ull(m, "\nUid:\t", from_kuid_munged(user_ns, cred->uid)); |
| 189 |
seq_put_decimal_ull(m, "\t", from_kuid_munged(user_ns, cred->euid)); |
| 190 |
seq_put_decimal_ull(m, "\t", from_kuid_munged(user_ns, cred->suid)); |
| 191 |
seq_put_decimal_ull(m, "\t", from_kuid_munged(user_ns, cred->fsuid)); |
| 192 |
seq_put_decimal_ull(m, "\nGid:\t", from_kgid_munged(user_ns, cred->gid)); |
| 193 |
seq_put_decimal_ull(m, "\t", from_kgid_munged(user_ns, cred->egid)); |
| 194 |
seq_put_decimal_ull(m, "\t", from_kgid_munged(user_ns, cred->sgid)); |
| 195 |
seq_put_decimal_ull(m, "\t", from_kgid_munged(user_ns, cred->fsgid)); |
| 196 |
seq_put_decimal_ull(m, "\nFDSize:\t", max_fds); |
| 197 |
|
| 198 |
seq_puts(m, "\nGroups:\t"); |
| 199 |
group_info = cred->group_info; |
| 200 |
for (g = 0; g < group_info->ngroups; g++) |
| 201 |
seq_put_decimal_ull(m, g ? " " : "", |
| 202 |
from_kgid_munged(user_ns, group_info->gid[g])); |
| 203 |
put_cred(cred); |
| 204 |
/* Trailing space shouldn't have been added in the first place. */ |
| 205 |
seq_putc(m, ' '); |
| 206 |
|
| 207 |
#ifdef CONFIG_PID_NS |
| 208 |
seq_puts(m, "\nNStgid:"); |
| 209 |
for (g = ns->level; g <= pid->level; g++) |
| 210 |
seq_put_decimal_ull(m, "\t", task_tgid_nr_ns(p, pid->numbers[g].ns)); |
| 211 |
seq_puts(m, "\nNSpid:"); |
| 212 |
for (g = ns->level; g <= pid->level; g++) |
| 213 |
seq_put_decimal_ull(m, "\t", task_pid_nr_ns(p, pid->numbers[g].ns)); |
| 214 |
seq_puts(m, "\nNSpgid:"); |
| 215 |
for (g = ns->level; g <= pid->level; g++) |
| 216 |
seq_put_decimal_ull(m, "\t", task_pgrp_nr_ns(p, pid->numbers[g].ns)); |
| 217 |
seq_puts(m, "\nNSsid:"); |
| 218 |
for (g = ns->level; g <= pid->level; g++) |
| 219 |
seq_put_decimal_ull(m, "\t", task_session_nr_ns(p, pid->numbers[g].ns)); |
| 220 |
#endif |
| 221 |
seq_putc(m, '\n'); |
| 222 |
|
| 223 |
seq_printf(m, "Kthread:\t%c\n", p->flags & PF_KTHREAD ? '1' : '0'); |
| 224 |
} |
| 225 |
|
| 226 |
void render_sigset_t(struct seq_file *m, const char *header, |
| 227 |
sigset_t *set) |
| 228 |
{ |
| 229 |
int i; |
| 230 |
|
| 231 |
seq_puts(m, header); |
| 232 |
|
| 233 |
i = _NSIG; |
| 234 |
do { |
| 235 |
int x = 0; |
| 236 |
|
| 237 |
i -= 4; |
| 238 |
if (sigismember(set, i+1)) x |= 1; |
| 239 |
if (sigismember(set, i+2)) x |= 2; |
| 240 |
if (sigismember(set, i+3)) x |= 4; |
| 241 |
if (sigismember(set, i+4)) x |= 8; |
| 242 |
seq_putc(m, hex_asc[x]); |
| 243 |
} while (i >= 4); |
| 244 |
|
| 245 |
seq_putc(m, '\n'); |
| 246 |
} |
| 247 |
|
| 248 |
static void collect_sigign_sigcatch(struct task_struct *p, sigset_t *sigign, |
| 249 |
sigset_t *sigcatch) |
| 250 |
{ |
| 251 |
struct k_sigaction *k; |
| 252 |
int i; |
| 253 |
|
| 254 |
k = p->sighand->action; |
| 255 |
for (i = 1; i <= _NSIG; ++i, ++k) { |
| 256 |
if (k->sa.sa_handler == SIG_IGN) |
| 257 |
sigaddset(sigign, i); |
| 258 |
else if (k->sa.sa_handler != SIG_DFL) |
| 259 |
sigaddset(sigcatch, i); |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
static inline void task_sig(struct seq_file *m, struct task_struct *p) |
| 264 |
{ |
| 265 |
unsigned long flags; |
| 266 |
sigset_t pending, shpending, blocked, ignored, caught; |
| 267 |
int num_threads = 0; |
| 268 |
unsigned int qsize = 0; |
| 269 |
unsigned long qlim = 0; |
| 270 |
|
| 271 |
sigemptyset(&pending); |
| 272 |
sigemptyset(&shpending); |
| 273 |
sigemptyset(&blocked); |
| 274 |
sigemptyset(&ignored); |
| 275 |
sigemptyset(&caught); |
| 276 |
|
| 277 |
if (lock_task_sighand(p, &flags)) { |
| 278 |
pending = p->pending.signal; |
| 279 |
shpending = p->signal->shared_pending.signal; |
| 280 |
blocked = p->blocked; |
| 281 |
collect_sigign_sigcatch(p, &ignored, &caught); |
| 282 |
num_threads = get_nr_threads(p); |
| 283 |
rcu_read_lock(); /* FIXME: is this correct? */ |
| 284 |
qsize = get_rlimit_value(task_ucounts(p), UCOUNT_RLIMIT_SIGPENDING); |
| 285 |
rcu_read_unlock(); |
| 286 |
qlim = task_rlimit(p, RLIMIT_SIGPENDING); |
| 287 |
unlock_task_sighand(p, &flags); |
| 288 |
} |
| 289 |
|
| 290 |
seq_put_decimal_ull(m, "Threads:\t", num_threads); |
| 291 |
seq_put_decimal_ull(m, "\nSigQ:\t", qsize); |
| 292 |
seq_put_decimal_ull(m, "/", qlim); |
| 293 |
|
| 294 |
/* render them all */ |
| 295 |
render_sigset_t(m, "\nSigPnd:\t", &pending); |
| 296 |
render_sigset_t(m, "ShdPnd:\t", &shpending); |
| 297 |
render_sigset_t(m, "SigBlk:\t", &blocked); |
| 298 |
render_sigset_t(m, "SigIgn:\t", &ignored); |
| 299 |
render_sigset_t(m, "SigCgt:\t", &caught); |
| 300 |
} |
| 301 |
|
| 302 |
static void render_cap_t(struct seq_file *m, const char *header, |
| 303 |
kernel_cap_t *a) |
| 304 |
{ |
| 305 |
seq_puts(m, header); |
| 306 |
seq_put_hex_ll(m, NULL, a->val, 16); |
| 307 |
seq_putc(m, '\n'); |
| 308 |
} |
| 309 |
|
| 310 |
static inline void task_cap(struct seq_file *m, struct task_struct *p) |
| 311 |
{ |
| 312 |
const struct cred *cred; |
| 313 |
kernel_cap_t cap_inheritable, cap_permitted, cap_effective, |
| 314 |
cap_bset, cap_ambient; |
| 315 |
|
| 316 |
rcu_read_lock(); |
| 317 |
cred = __task_cred(p); |
| 318 |
cap_inheritable = cred->cap_inheritable; |
| 319 |
cap_permitted = cred->cap_permitted; |
| 320 |
cap_effective = cred->cap_effective; |
| 321 |
cap_bset = cred->cap_bset; |
| 322 |
cap_ambient = cred->cap_ambient; |
| 323 |
rcu_read_unlock(); |
| 324 |
|
| 325 |
render_cap_t(m, "CapInh:\t", &cap_inheritable); |
| 326 |
render_cap_t(m, "CapPrm:\t", &cap_permitted); |
| 327 |
render_cap_t(m, "CapEff:\t", &cap_effective); |
| 328 |
render_cap_t(m, "CapBnd:\t", &cap_bset); |
| 329 |
render_cap_t(m, "CapAmb:\t", &cap_ambient); |
| 330 |
} |
| 331 |
|
| 332 |
static inline void task_seccomp(struct seq_file *m, struct task_struct *p) |
| 333 |
{ |
| 334 |
seq_put_decimal_ull(m, "NoNewPrivs:\t", task_no_new_privs(p)); |
| 335 |
#ifdef CONFIG_SECCOMP |
| 336 |
seq_put_decimal_ull(m, "\nSeccomp:\t", p->seccomp.mode); |
| 337 |
#ifdef CONFIG_SECCOMP_FILTER |
| 338 |
seq_put_decimal_ull(m, "\nSeccomp_filters:\t", |
| 339 |
atomic_read(&p->seccomp.filter_count)); |
| 340 |
#endif |
| 341 |
#endif |
| 342 |
seq_puts(m, "\nSpeculation_Store_Bypass:\t"); |
| 343 |
switch (arch_prctl_spec_ctrl_get(p, PR_SPEC_STORE_BYPASS)) { |
| 344 |
case -EINVAL: |
| 345 |
seq_puts(m, "unknown"); |
| 346 |
break; |
| 347 |
case PR_SPEC_NOT_AFFECTED: |
| 348 |
seq_puts(m, "not vulnerable"); |
| 349 |
break; |
| 350 |
case PR_SPEC_PRCTL | PR_SPEC_FORCE_DISABLE: |
| 351 |
seq_puts(m, "thread force mitigated"); |
| 352 |
break; |
| 353 |
case PR_SPEC_PRCTL | PR_SPEC_DISABLE: |
| 354 |
seq_puts(m, "thread mitigated"); |
| 355 |
break; |
| 356 |
case PR_SPEC_PRCTL | PR_SPEC_ENABLE: |
| 357 |
seq_puts(m, "thread vulnerable"); |
| 358 |
break; |
| 359 |
case PR_SPEC_DISABLE: |
| 360 |
seq_puts(m, "globally mitigated"); |
| 361 |
break; |
| 362 |
default: |
| 363 |
seq_puts(m, "vulnerable"); |
| 364 |
break; |
| 365 |
} |
| 366 |
|
| 367 |
seq_puts(m, "\nSpeculationIndirectBranch:\t"); |
| 368 |
switch (arch_prctl_spec_ctrl_get(p, PR_SPEC_INDIRECT_BRANCH)) { |
| 369 |
case -EINVAL: |
| 370 |
seq_puts(m, "unsupported"); |
| 371 |
break; |
| 372 |
case PR_SPEC_NOT_AFFECTED: |
| 373 |
seq_puts(m, "not affected"); |
| 374 |
break; |
| 375 |
case PR_SPEC_PRCTL | PR_SPEC_FORCE_DISABLE: |
| 376 |
seq_puts(m, "conditional force disabled"); |
| 377 |
break; |
| 378 |
case PR_SPEC_PRCTL | PR_SPEC_DISABLE: |
| 379 |
seq_puts(m, "conditional disabled"); |
| 380 |
break; |
| 381 |
case PR_SPEC_PRCTL | PR_SPEC_ENABLE: |
| 382 |
seq_puts(m, "conditional enabled"); |
| 383 |
break; |
| 384 |
case PR_SPEC_ENABLE: |
| 385 |
seq_puts(m, "always enabled"); |
| 386 |
break; |
| 387 |
case PR_SPEC_DISABLE: |
| 388 |
seq_puts(m, "always disabled"); |
| 389 |
break; |
| 390 |
default: |
| 391 |
seq_puts(m, "unknown"); |
| 392 |
break; |
| 393 |
} |
| 394 |
seq_putc(m, '\n'); |
| 395 |
} |
| 396 |
|
| 397 |
static inline void task_context_switch_counts(struct seq_file *m, |
| 398 |
struct task_struct *p) |
| 399 |
{ |
| 400 |
seq_put_decimal_ull(m, "voluntary_ctxt_switches:\t", p->nvcsw); |
| 401 |
seq_put_decimal_ull(m, "\nnonvoluntary_ctxt_switches:\t", p->nivcsw); |
| 402 |
seq_putc(m, '\n'); |
| 403 |
} |
| 404 |
|
| 405 |
static void task_cpus_allowed(struct seq_file *m, struct task_struct *task) |
| 406 |
{ |
| 407 |
seq_printf(m, "Cpus_allowed:\t%*pb\n", |
| 408 |
cpumask_pr_args(&task->cpus_mask)); |
| 409 |
seq_printf(m, "Cpus_allowed_list:\t%*pbl\n", |
| 410 |
cpumask_pr_args(&task->cpus_mask)); |
| 411 |
} |
| 412 |
|
| 413 |
static inline void task_core_dumping(struct seq_file *m, struct task_struct *task) |
| 414 |
{ |
| 415 |
seq_put_decimal_ull(m, "CoreDumping:\t", !!task->signal->core_state); |
| 416 |
seq_putc(m, '\n'); |
| 417 |
} |
| 418 |
|
| 419 |
static inline void task_thp_status(struct seq_file *m, struct mm_struct *mm) |
| 420 |
{ |
| 421 |
bool thp_enabled = IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE); |
| 422 |
|
| 423 |
if (thp_enabled) |
| 424 |
thp_enabled = !mm_flags_test(MMF_DISABLE_THP_COMPLETELY, mm); |
| 425 |
seq_printf(m, "THP_enabled:\t%d\n", thp_enabled); |
| 426 |
} |
| 427 |
|
| 428 |
static inline void task_untag_mask(struct seq_file *m, struct mm_struct *mm) |
| 429 |
{ |
| 430 |
seq_printf(m, "untag_mask:\t%#lx\n", mm_untag_mask(mm)); |
| 431 |
} |
| 432 |
|
| 433 |
__weak void arch_proc_pid_thread_features(struct seq_file *m, |
| 434 |
struct task_struct *task) |
| 435 |
{ |
| 436 |
} |
| 437 |
|
| 438 |
int proc_pid_status(struct seq_file *m, struct pid_namespace *ns, |
| 439 |
struct pid *pid, struct task_struct *task) |
| 440 |
{ |
| 441 |
struct mm_struct *mm = get_task_mm(task); |
| 442 |
|
| 443 |
seq_puts(m, "Name:\t"); |
| 444 |
proc_task_name(m, task, true); |
| 445 |
seq_putc(m, '\n'); |
| 446 |
|
| 447 |
task_state(m, ns, pid, task); |
| 448 |
|
| 449 |
if (mm) { |
| 450 |
task_mem(m, mm); |
| 451 |
task_core_dumping(m, task); |
| 452 |
task_thp_status(m, mm); |
| 453 |
task_untag_mask(m, mm); |
| 454 |
mmput(mm); |
| 455 |
} |
| 456 |
task_sig(m, task); |
| 457 |
task_cap(m, task); |
| 458 |
task_seccomp(m, task); |
| 459 |
task_cpus_allowed(m, task); |
| 460 |
cpuset_task_status_allowed(m, task); |
| 461 |
task_context_switch_counts(m, task); |
| 462 |
arch_proc_pid_thread_features(m, task); |
| 463 |
return 0; |
| 464 |
} |
| 465 |
|
| 466 |
static int do_task_stat(struct seq_file *m, struct pid_namespace *ns, |
| 467 |
struct pid *pid, struct task_struct *task, int whole) |
| 468 |
{ |
| 469 |
unsigned long vsize, eip, esp, wchan = 0; |
| 470 |
int priority, nice; |
| 471 |
int tty_pgrp = -1, tty_nr = 0; |
| 472 |
sigset_t sigign, sigcatch; |
| 473 |
char state; |
| 474 |
pid_t ppid = 0, pgid = -1, sid = -1; |
| 475 |
int num_threads = 0; |
| 476 |
int permitted; |
| 477 |
struct mm_struct *mm; |
| 478 |
unsigned long long start_time; |
| 479 |
unsigned long cmin_flt, cmaj_flt, min_flt, maj_flt; |
| 480 |
u64 cutime, cstime, cgtime, utime, stime, gtime; |
| 481 |
unsigned long rsslim = 0; |
| 482 |
unsigned long flags; |
| 483 |
int exit_code = task->exit_code; |
| 484 |
struct signal_struct *sig = task->signal; |
| 485 |
|
| 486 |
state = *get_task_state(task); |
| 487 |
vsize = eip = esp = 0; |
| 488 |
permitted = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS | PTRACE_MODE_NOAUDIT); |
| 489 |
mm = get_task_mm(task); |
| 490 |
if (mm) { |
| 491 |
vsize = task_vsize(mm); |
| 492 |
/* |
| 493 |
* esp and eip are intentionally zeroed out. There is no |
| 494 |
* non-racy way to read them without freezing the task. |
| 495 |
* Programs that need reliable values can use ptrace(2). |
| 496 |
* |
| 497 |
* The only exception is if the task is core dumping because |
| 498 |
* a program is not able to use ptrace(2) in that case. It is |
| 499 |
* safe because the task has stopped executing permanently. |
| 500 |
*/ |
| 501 |
if (permitted && (task->flags & (PF_EXITING|PF_DUMPCORE|PF_POSTCOREDUMP))) { |
| 502 |
if (try_get_task_stack(task)) { |
| 503 |
eip = KSTK_EIP(task); |
| 504 |
esp = KSTK_ESP(task); |
| 505 |
put_task_stack(task); |
| 506 |
} |
| 507 |
} |
| 508 |
} |
| 509 |
|
| 510 |
sigemptyset(&sigign); |
| 511 |
sigemptyset(&sigcatch); |
| 512 |
|
| 513 |
if (lock_task_sighand(task, &flags)) { |
| 514 |
if (sig->tty) { |
| 515 |
struct pid *pgrp = tty_get_pgrp(sig->tty); |
| 516 |
tty_pgrp = pid_nr_ns(pgrp, ns); |
| 517 |
put_pid(pgrp); |
| 518 |
tty_nr = new_encode_dev(tty_devnum(sig->tty)); |
| 519 |
} |
| 520 |
|
| 521 |
num_threads = get_nr_threads(task); |
| 522 |
collect_sigign_sigcatch(task, &sigign, &sigcatch); |
| 523 |
|
| 524 |
rsslim = READ_ONCE(sig->rlim[RLIMIT_RSS].rlim_cur); |
| 525 |
|
| 526 |
if (whole) { |
| 527 |
if (sig->flags & (SIGNAL_GROUP_EXIT | SIGNAL_STOP_STOPPED)) |
| 528 |
exit_code = sig->group_exit_code; |
| 529 |
} |
| 530 |
|
| 531 |
sid = task_session_nr_ns(task, ns); |
| 532 |
ppid = task_ppid_nr_ns(task, ns); |
| 533 |
pgid = task_pgrp_nr_ns(task, ns); |
| 534 |
|
| 535 |
unlock_task_sighand(task, &flags); |
| 536 |
} |
| 537 |
|
| 538 |
if (permitted && (!whole || num_threads < 2)) |
| 539 |
wchan = !task_is_running(task); |
| 540 |
|
| 541 |
scoped_guard(rcu) { |
| 542 |
scoped_seqlock_read (&sig->stats_lock, ss_lock_irqsave) { |
| 543 |
cmin_flt = sig->cmin_flt; |
| 544 |
cmaj_flt = sig->cmaj_flt; |
| 545 |
cutime = sig->cutime; |
| 546 |
cstime = sig->cstime; |
| 547 |
cgtime = sig->cgtime; |
| 548 |
|
| 549 |
if (whole) { |
| 550 |
struct task_struct *t; |
| 551 |
|
| 552 |
min_flt = sig->min_flt; |
| 553 |
maj_flt = sig->maj_flt; |
| 554 |
gtime = sig->gtime; |
| 555 |
|
| 556 |
__for_each_thread(sig, t) { |
| 557 |
min_flt += t->min_flt; |
| 558 |
maj_flt += t->maj_flt; |
| 559 |
gtime += task_gtime(t); |
| 560 |
} |
| 561 |
} |
| 562 |
} |
| 563 |
} |
| 564 |
|
| 565 |
if (whole) { |
| 566 |
thread_group_cputime_adjusted(task, &utime, &stime); |
| 567 |
} else { |
| 568 |
task_cputime_adjusted(task, &utime, &stime); |
| 569 |
min_flt = task->min_flt; |
| 570 |
maj_flt = task->maj_flt; |
| 571 |
gtime = task_gtime(task); |
| 572 |
} |
| 573 |
|
| 574 |
/* scale priority and nice values from timeslices to -20..20 */ |
| 575 |
/* to make it look like a "normal" Unix priority/nice value */ |
| 576 |
priority = task_prio(task); |
| 577 |
nice = task_nice(task); |
| 578 |
|
| 579 |
/* apply timens offset for boottime and convert nsec -> ticks */ |
| 580 |
start_time = |
| 581 |
nsec_to_clock_t(timens_add_boottime_ns(task->start_boottime)); |
| 582 |
|
| 583 |
seq_put_decimal_ull(m, "", pid_nr_ns(pid, ns)); |
| 584 |
seq_puts(m, " ("); |
| 585 |
proc_task_name(m, task, false); |
| 586 |
seq_puts(m, ") "); |
| 587 |
seq_putc(m, state); |
| 588 |
seq_put_decimal_ll(m, " ", ppid); |
| 589 |
seq_put_decimal_ll(m, " ", pgid); |
| 590 |
seq_put_decimal_ll(m, " ", sid); |
| 591 |
seq_put_decimal_ll(m, " ", tty_nr); |
| 592 |
seq_put_decimal_ll(m, " ", tty_pgrp); |
| 593 |
seq_put_decimal_ull(m, " ", task->flags); |
| 594 |
seq_put_decimal_ull(m, " ", min_flt); |
| 595 |
seq_put_decimal_ull(m, " ", cmin_flt); |
| 596 |
seq_put_decimal_ull(m, " ", maj_flt); |
| 597 |
seq_put_decimal_ull(m, " ", cmaj_flt); |
| 598 |
seq_put_decimal_ull(m, " ", nsec_to_clock_t(utime)); |
| 599 |
seq_put_decimal_ull(m, " ", nsec_to_clock_t(stime)); |
| 600 |
seq_put_decimal_ll(m, " ", nsec_to_clock_t(cutime)); |
| 601 |
seq_put_decimal_ll(m, " ", nsec_to_clock_t(cstime)); |
| 602 |
seq_put_decimal_ll(m, " ", priority); |
| 603 |
seq_put_decimal_ll(m, " ", nice); |
| 604 |
seq_put_decimal_ll(m, " ", num_threads); |
| 605 |
seq_put_decimal_ull(m, " ", 0); |
| 606 |
seq_put_decimal_ull(m, " ", start_time); |
| 607 |
seq_put_decimal_ull(m, " ", vsize); |
| 608 |
seq_put_decimal_ull(m, " ", mm ? get_mm_rss(mm) : 0); |
| 609 |
seq_put_decimal_ull(m, " ", rsslim); |
| 610 |
seq_put_decimal_ull(m, " ", mm ? (permitted ? mm->start_code : 1) : 0); |
| 611 |
seq_put_decimal_ull(m, " ", mm ? (permitted ? mm->end_code : 1) : 0); |
| 612 |
seq_put_decimal_ull(m, " ", (permitted && mm) ? mm->start_stack : 0); |
| 613 |
seq_put_decimal_ull(m, " ", esp); |
| 614 |
seq_put_decimal_ull(m, " ", eip); |
| 615 |
/* The signal information here is obsolete. |
| 616 |
* It must be decimal for Linux 2.0 compatibility. |
| 617 |
* Use /proc/#/status for real-time signals. |
| 618 |
*/ |
| 619 |
seq_put_decimal_ull(m, " ", task->pending.signal.sig[0] & 0x7fffffffUL); |
| 620 |
seq_put_decimal_ull(m, " ", task->blocked.sig[0] & 0x7fffffffUL); |
| 621 |
seq_put_decimal_ull(m, " ", sigign.sig[0] & 0x7fffffffUL); |
| 622 |
seq_put_decimal_ull(m, " ", sigcatch.sig[0] & 0x7fffffffUL); |
| 623 |
|
| 624 |
/* |
| 625 |
* We used to output the absolute kernel address, but that's an |
| 626 |
* information leak - so instead we show a 0/1 flag here, to signal |
| 627 |
* to user-space whether there's a wchan field in /proc/PID/wchan. |
| 628 |
* |
| 629 |
* This works with older implementations of procps as well. |
| 630 |
*/ |
| 631 |
seq_put_decimal_ull(m, " ", wchan); |
| 632 |
|
| 633 |
seq_put_decimal_ull(m, " ", 0); |
| 634 |
seq_put_decimal_ull(m, " ", 0); |
| 635 |
seq_put_decimal_ll(m, " ", task->exit_signal); |
| 636 |
seq_put_decimal_ll(m, " ", task_cpu(task)); |
| 637 |
seq_put_decimal_ull(m, " ", task->rt_priority); |
| 638 |
seq_put_decimal_ull(m, " ", task->policy); |
| 639 |
seq_put_decimal_ull(m, " ", delayacct_blkio_ticks(task)); |
| 640 |
seq_put_decimal_ull(m, " ", nsec_to_clock_t(gtime)); |
| 641 |
seq_put_decimal_ll(m, " ", nsec_to_clock_t(cgtime)); |
| 642 |
|
| 643 |
if (mm && permitted) { |
| 644 |
seq_put_decimal_ull(m, " ", mm->start_data); |
| 645 |
seq_put_decimal_ull(m, " ", mm->end_data); |
| 646 |
seq_put_decimal_ull(m, " ", mm->start_brk); |
| 647 |
seq_put_decimal_ull(m, " ", mm->arg_start); |
| 648 |
seq_put_decimal_ull(m, " ", mm->arg_end); |
| 649 |
seq_put_decimal_ull(m, " ", mm->env_start); |
| 650 |
seq_put_decimal_ull(m, " ", mm->env_end); |
| 651 |
} else |
| 652 |
seq_puts(m, " 0 0 0 0 0 0 0"); |
| 653 |
|
| 654 |
if (permitted) |
| 655 |
seq_put_decimal_ll(m, " ", exit_code); |
| 656 |
else |
| 657 |
seq_puts(m, " 0"); |
| 658 |
|
| 659 |
seq_putc(m, '\n'); |
| 660 |
if (mm) |
| 661 |
mmput(mm); |
| 662 |
return 0; |
| 663 |
} |
| 664 |
|
| 665 |
int proc_tid_stat(struct seq_file *m, struct pid_namespace *ns, |
| 666 |
struct pid *pid, struct task_struct *task) |
| 667 |
{ |
| 668 |
return do_task_stat(m, ns, pid, task, 0); |
| 669 |
} |
| 670 |
|
| 671 |
int proc_tgid_stat(struct seq_file *m, struct pid_namespace *ns, |
| 672 |
struct pid *pid, struct task_struct *task) |
| 673 |
{ |
| 674 |
return do_task_stat(m, ns, pid, task, 1); |
| 675 |
} |
| 676 |
|
| 677 |
int proc_pid_statm(struct seq_file *m, struct pid_namespace *ns, |
| 678 |
struct pid *pid, struct task_struct *task) |
| 679 |
{ |
| 680 |
struct mm_struct *mm = get_task_mm(task); |
| 681 |
|
| 682 |
if (mm) { |
| 683 |
unsigned long size; |
| 684 |
unsigned long resident = 0; |
| 685 |
unsigned long shared = 0; |
| 686 |
unsigned long text = 0; |
| 687 |
unsigned long data = 0; |
| 688 |
|
| 689 |
size = task_statm(mm, &shared, &text, &data, &resident); |
| 690 |
mmput(mm); |
| 691 |
|
| 692 |
/* |
| 693 |
* For quick read, open code by putting numbers directly |
| 694 |
* expected format is |
| 695 |
* seq_printf(m, "%lu %lu %lu %lu 0 %lu 0\n", |
| 696 |
* size, resident, shared, text, data); |
| 697 |
*/ |
| 698 |
seq_put_decimal_ull(m, "", size); |
| 699 |
seq_put_decimal_ull(m, " ", resident); |
| 700 |
seq_put_decimal_ull(m, " ", shared); |
| 701 |
seq_put_decimal_ull(m, " ", text); |
| 702 |
seq_put_decimal_ull(m, " ", 0); |
| 703 |
seq_put_decimal_ull(m, " ", data); |
| 704 |
seq_put_decimal_ull(m, " ", 0); |
| 705 |
seq_putc(m, '\n'); |
| 706 |
} else { |
| 707 |
seq_write(m, "0 0 0 0 0 0 0\n", 14); |
| 708 |
} |
| 709 |
return 0; |
| 710 |
} |
| 711 |
|
| 712 |
#ifdef CONFIG_PROC_CHILDREN |
| 713 |
static struct pid * |
| 714 |
get_children_pid(struct inode *inode, struct pid *pid_prev, loff_t pos) |
| 715 |
{ |
| 716 |
struct task_struct *start, *task; |
| 717 |
struct pid *pid = NULL; |
| 718 |
|
| 719 |
read_lock(&tasklist_lock); |
| 720 |
|
| 721 |
start = pid_task(proc_pid(inode), PIDTYPE_PID); |
| 722 |
if (!start) |
| 723 |
goto out; |
| 724 |
|
| 725 |
/* |
| 726 |
* Lets try to continue searching first, this gives |
| 727 |
* us significant speedup on children-rich processes. |
| 728 |
*/ |
| 729 |
if (pid_prev) { |
| 730 |
task = pid_task(pid_prev, PIDTYPE_PID); |
| 731 |
if (task && task->real_parent == start && |
| 732 |
!(list_empty(&task->sibling))) { |
| 733 |
if (list_is_last(&task->sibling, &start->children)) |
| 734 |
goto out; |
| 735 |
task = list_first_entry(&task->sibling, |
| 736 |
struct task_struct, sibling); |
| 737 |
pid = get_pid(task_pid(task)); |
| 738 |
goto out; |
| 739 |
} |
| 740 |
} |
| 741 |
|
| 742 |
/* |
| 743 |
* Slow search case. |
| 744 |
* |
| 745 |
* We might miss some children here if children |
| 746 |
* are exited while we were not holding the lock, |
| 747 |
* but it was never promised to be accurate that |
| 748 |
* much. |
| 749 |
* |
| 750 |
* "Just suppose that the parent sleeps, but N children |
| 751 |
* exit after we printed their tids. Now the slow paths |
| 752 |
* skips N extra children, we miss N tasks." (c) |
| 753 |
* |
| 754 |
* So one need to stop or freeze the leader and all |
| 755 |
* its children to get a precise result. |
| 756 |
*/ |
| 757 |
list_for_each_entry(task, &start->children, sibling) { |
| 758 |
if (pos-- == 0) { |
| 759 |
pid = get_pid(task_pid(task)); |
| 760 |
break; |
| 761 |
} |
| 762 |
} |
| 763 |
|
| 764 |
out: |
| 765 |
read_unlock(&tasklist_lock); |
| 766 |
return pid; |
| 767 |
} |
| 768 |
|
| 769 |
static int children_seq_show(struct seq_file *seq, void *v) |
| 770 |
{ |
| 771 |
struct inode *inode = file_inode(seq->file); |
| 772 |
|
| 773 |
seq_printf(seq, "%d ", pid_nr_ns(v, proc_pid_ns(inode->i_sb))); |
| 774 |
return 0; |
| 775 |
} |
| 776 |
|
| 777 |
static void *children_seq_start(struct seq_file *seq, loff_t *pos) |
| 778 |
{ |
| 779 |
return get_children_pid(file_inode(seq->file), NULL, *pos); |
| 780 |
} |
| 781 |
|
| 782 |
static void *children_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 783 |
{ |
| 784 |
struct pid *pid; |
| 785 |
|
| 786 |
pid = get_children_pid(file_inode(seq->file), v, *pos + 1); |
| 787 |
put_pid(v); |
| 788 |
|
| 789 |
++*pos; |
| 790 |
return pid; |
| 791 |
} |
| 792 |
|
| 793 |
static void children_seq_stop(struct seq_file *seq, void *v) |
| 794 |
{ |
| 795 |
put_pid(v); |
| 796 |
} |
| 797 |
|
| 798 |
static const struct seq_operations children_seq_ops = { |
| 799 |
.start = children_seq_start, |
| 800 |
.next = children_seq_next, |
| 801 |
.stop = children_seq_stop, |
| 802 |
.show = children_seq_show, |
| 803 |
}; |
| 804 |
|
| 805 |
static int children_seq_open(struct inode *inode, struct file *file) |
| 806 |
{ |
| 807 |
return seq_open(file, &children_seq_ops); |
| 808 |
} |
| 809 |
|
| 810 |
const struct file_operations proc_tid_children_operations = { |
| 811 |
.open = children_seq_open, |
| 812 |
.read = seq_read, |
| 813 |
.llseek = seq_lseek, |
| 814 |
.release = seq_release, |
| 815 |
}; |
| 816 |
#endif /* CONFIG_PROC_CHILDREN */ |