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 55 #include <linux/types.h> 56 #include <linux/errno.h> 57 #include <linux/time.h> 58 #include <linux/kernel.h> 59 #include <linux/kernel_stat.h> 60 #include <linux/tty.h> 61 #include <linux/string.h> 62 #include <linux/mman.h> 63 #include <linux/sched/mm.h> 64 #include <linux/sched/numa_balancing.h> 65 #include <linux/proc_fs.h> 66 #include <linux/ioport.h> 67 #include <linux/uaccess.h> 68 #include <linux/io.h> 69 #include <linux/mm.h> 70 #include <linux/hugetlb.h> 71 #include <linux/pagemap.h> 72 #include <linux/swap.h> 73 #include <linux/smp.h> 74 #include <linux/signal.h> 75 #include <linux/highmem.h> 76 #include <linux/file.h> 77 #include <linux/fdtable.h> 78 #include <linux/times.h> 79 #include <linux/cpuset.h> 80 #include <linux/rcupdate.h> 81 #include <linux/delayacct.h> 82 #include <linux/seq_file.h> 83 #include <linux/pid_namespace.h> 84 #include <linux/ptrace.h> 85 #include <linux/tracehook.h> 86 #include <linux/string_helpers.h> 87 #include <linux/user_namespace.h> 88 #include <linux/fs_struct.h> 89 90 #include <asm/pgtable.h> 91 #include <asm/processor.h> 92 #include "internal.h" 93 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 101 get_task_comm(tcomm, p); 102 103 seq_puts(m, "Name:\t"); 104 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); 108 109 seq_putc(m, '\n'); 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 */ 118 static const char * const task_state_array[] = { 119 "R (running)", /* 0 */ 120 "S (sleeping)", /* 1 */ 121 "D (disk sleep)", /* 2 */ 122 "T (stopped)", /* 4 */ 123 "t (tracing stop)", /* 8 */ 124 "X (dead)", /* 16 */ 125 "Z (zombie)", /* 32 */ 126 }; 127 128 static inline const char *get_task_state(struct task_struct *tsk) 129 { 130 unsigned int state = (tsk->state | tsk->exit_state) & TASK_REPORT; 131 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 140 BUILD_BUG_ON(1 + ilog2(TASK_REPORT) != ARRAY_SIZE(task_state_array)-1); 141 142 return task_state_array[fls(state)]; 143 } 144 145 static 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 158 static inline void task_state(struct seq_file *m, struct pid_namespace *ns, 159 struct pid *pid, struct task_struct *p) 160 { 161 struct user_namespace *user_ns = seq_user_ns(m); 162 struct group_info *group_info; 163 int g, umask; 164 struct task_struct *tracer; 165 const struct cred *cred; 166 pid_t ppid, tpid = 0, tgid, ngid; 167 unsigned int max_fds = 0; 168 169 rcu_read_lock(); 170 ppid = pid_alive(p) ? 171 task_tgid_nr_ns(rcu_dereference(p->real_parent), ns) : 0; 172 173 tracer = ptrace_parent(p); 174 if (tracer) 175 tpid = task_pid_nr_ns(tracer, ns); 176 177 tgid = task_tgid_nr_ns(p, ns); 178 ngid = task_numa_group_id(p); 179 cred = get_task_cred(p); 180 181 umask = get_task_umask(p); 182 if (umask >= 0) 183 seq_printf(m, "Umask:\t%#04o\n", umask); 184 185 task_lock(p); 186 if (p->files) 187 max_fds = files_fdtable(p->files)->max_fds; 188 task_unlock(p); 189 rcu_read_unlock(); 190 191 seq_printf(m, "State:\t%s", get_task_state(p)); 192 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); 207 208 seq_puts(m, "\nGroups:\t"); 209 group_info = cred->group_info; 210 for (g = 0; g < group_info->ngroups; g++) 211 seq_put_decimal_ull(m, g ? " " : "", 212 from_kgid_munged(user_ns, group_info->gid[g])); 213 put_cred(cred); 214 /* Trailing space shouldn't have been added in the first place. */ 215 seq_putc(m, ' '); 216 217 #ifdef CONFIG_PID_NS 218 seq_puts(m, "\nNStgid:"); 219 for (g = ns->level; g <= pid->level; g++) 220 seq_put_decimal_ull(m, "\t", task_tgid_nr_ns(p, pid->numbers[g].ns)); 221 seq_puts(m, "\nNSpid:"); 222 for (g = ns->level; g <= pid->level; g++) 223 seq_put_decimal_ull(m, "\t", task_pid_nr_ns(p, pid->numbers[g].ns)); 224 seq_puts(m, "\nNSpgid:"); 225 for (g = ns->level; g <= pid->level; g++) 226 seq_put_decimal_ull(m, "\t", task_pgrp_nr_ns(p, pid->numbers[g].ns)); 227 seq_puts(m, "\nNSsid:"); 228 for (g = ns->level; g <= pid->level; g++) 229 seq_put_decimal_ull(m, "\t", task_session_nr_ns(p, pid->numbers[g].ns)); 230 #endif 231 seq_putc(m, '\n'); 232 } 233 234 void render_sigset_t(struct seq_file *m, const char *header, 235 sigset_t *set) 236 { 237 int i; 238 239 seq_puts(m, header); 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; 250 seq_putc(m, hex_asc[x]); 251 } while (i >= 4); 252 253 seq_putc(m, '\n'); 254 } 255 256 static 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 271 static inline void task_sig(struct seq_file *m, struct task_struct *p) 272 { 273 unsigned long flags; 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 285 if (lock_task_sighand(p, &flags)) { 286 pending = p->pending.signal; 287 shpending = p->signal->shared_pending.signal; 288 blocked = p->blocked; 289 collect_sigign_sigcatch(p, &ignored, &caught); 290 num_threads = get_nr_threads(p); 291 rcu_read_lock(); /* FIXME: is this correct? */ 292 qsize = atomic_read(&__task_cred(p)->user->sigpending); 293 rcu_read_unlock(); 294 qlim = task_rlimit(p, RLIMIT_SIGPENDING); 295 unlock_task_sighand(p, &flags); 296 } 297 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); 301 302 /* render them all */ 303 render_sigset_t(m, "\nSigPnd:\t", &pending); 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); 308 } 309 310 static void render_cap_t(struct seq_file *m, const char *header, 311 kernel_cap_t *a) 312 { 313 unsigned __capi; 314 315 seq_puts(m, header); 316 CAP_FOR_EACH_U32(__capi) { 317 seq_printf(m, "%08x", 318 a->cap[CAP_LAST_U32 - __capi]); 319 } 320 seq_putc(m, '\n'); 321 } 322 323 static inline void task_cap(struct seq_file *m, struct task_struct *p) 324 { 325 const struct cred *cred; 326 kernel_cap_t cap_inheritable, cap_permitted, cap_effective, 327 cap_bset, cap_ambient; 328 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; 335 cap_ambient = cred->cap_ambient; 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); 342 render_cap_t(m, "CapAmb:\t", &cap_ambient); 343 } 344 345 static inline void task_seccomp(struct seq_file *m, struct task_struct *p) 346 { 347 seq_put_decimal_ull(m, "NoNewPrivs:\t", task_no_new_privs(p)); 348 #ifdef CONFIG_SECCOMP 349 seq_put_decimal_ull(m, "\nSeccomp:\t", p->seccomp.mode); 350 #endif 351 seq_putc(m, '\n'); 352 } 353 354 static inline void task_context_switch_counts(struct seq_file *m, 355 struct task_struct *p) 356 { 357 seq_put_decimal_ull(m, "voluntary_ctxt_switches:\t", p->nvcsw); 358 seq_put_decimal_ull(m, "\nnonvoluntary_ctxt_switches:\t", p->nivcsw); 359 seq_putc(m, '\n'); 360 } 361 362 static void task_cpus_allowed(struct seq_file *m, struct task_struct *task) 363 { 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)); 368 } 369 370 int proc_pid_status(struct seq_file *m, struct pid_namespace *ns, 371 struct pid *pid, struct task_struct *task) 372 { 373 struct mm_struct *mm = get_task_mm(task); 374 375 task_name(m, task); 376 task_state(m, ns, pid, task); 377 378 if (mm) { 379 task_mem(m, mm); 380 mmput(mm); 381 } 382 task_sig(m, task); 383 task_cap(m, task); 384 task_seccomp(m, task); 385 task_cpus_allowed(m, task); 386 cpuset_task_status_allowed(m, task); 387 task_context_switch_counts(m, task); 388 return 0; 389 } 390 391 static int do_task_stat(struct seq_file *m, struct pid_namespace *ns, 392 struct pid *pid, struct task_struct *task, int whole) 393 { 394 unsigned long vsize, eip, esp, wchan = 0; 395 int priority, nice; 396 int tty_pgrp = -1, tty_nr = 0; 397 sigset_t sigign, sigcatch; 398 char state; 399 pid_t ppid = 0, pgid = -1, sid = -1; 400 int num_threads = 0; 401 int permitted; 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; 406 u64 cutime, cstime, utime, stime; 407 u64 cgtime, gtime; 408 unsigned long rsslim = 0; 409 char tcomm[sizeof(task->comm)]; 410 unsigned long flags; 411 412 state = *get_task_state(task); 413 vsize = eip = esp = 0; 414 permitted = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS | PTRACE_MODE_NOAUDIT); 415 mm = get_task_mm(task); 416 if (mm) { 417 vsize = task_vsize(mm); 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 */ 423 } 424 425 get_task_comm(tcomm, task); 426 427 sigemptyset(&sigign); 428 sigemptyset(&sigcatch); 429 cutime = cstime = utime = stime = 0; 430 cgtime = gtime = 0; 431 432 if (lock_task_sighand(task, &flags)) { 433 struct signal_struct *sig = task->signal; 434 435 if (sig->tty) { 436 struct pid *pgrp = tty_get_pgrp(sig->tty); 437 tty_pgrp = pid_nr_ns(pgrp, ns); 438 put_pid(pgrp); 439 tty_nr = new_encode_dev(tty_devnum(sig->tty)); 440 } 441 442 num_threads = get_nr_threads(task); 443 collect_sigign_sigcatch(task, &sigign, &sigcatch); 444 445 cmin_flt = sig->cmin_flt; 446 cmaj_flt = sig->cmaj_flt; 447 cutime = sig->cutime; 448 cstime = sig->cstime; 449 cgtime = sig->cgtime; 450 rsslim = ACCESS_ONCE(sig->rlim[RLIMIT_RSS].rlim_cur); 451 452 /* add up live thread stats at the group level */ 453 if (whole) { 454 struct task_struct *t = task; 455 do { 456 min_flt += t->min_flt; 457 maj_flt += t->maj_flt; 458 gtime += task_gtime(t); 459 } while_each_thread(task, t); 460 461 min_flt += sig->min_flt; 462 maj_flt += sig->maj_flt; 463 thread_group_cputime_adjusted(task, &utime, &stime); 464 gtime += sig->gtime; 465 } 466 467 sid = task_session_nr_ns(task, ns); 468 ppid = task_tgid_nr_ns(task->real_parent, ns); 469 pgid = task_pgrp_nr_ns(task, ns); 470 471 unlock_task_sighand(task, &flags); 472 } 473 474 if (permitted && (!whole || num_threads < 2)) 475 wchan = get_wchan(task); 476 if (!whole) { 477 min_flt = task->min_flt; 478 maj_flt = task->maj_flt; 479 task_cputime_adjusted(task, &utime, &stime); 480 gtime = task_gtime(task); 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 488 /* convert nsec -> ticks */ 489 start_time = nsec_to_clock_t(task->real_start_time); 490 491 seq_printf(m, "%d (%s) %c", pid_nr_ns(pid, ns), tcomm, state); 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); 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)); 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); 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 */ 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); 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 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)); 547 seq_put_decimal_ull(m, " ", nsec_to_clock_t(gtime)); 548 seq_put_decimal_ll(m, " ", nsec_to_clock_t(cgtime)); 549 550 if (mm && permitted) { 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); 558 } else 559 seq_puts(m, " 0 0 0 0 0 0 0"); 560 561 if (permitted) 562 seq_put_decimal_ll(m, " ", task->exit_code); 563 else 564 seq_puts(m, " 0"); 565 566 seq_putc(m, '\n'); 567 if (mm) 568 mmput(mm); 569 return 0; 570 } 571 572 int proc_tid_stat(struct seq_file *m, struct pid_namespace *ns, 573 struct pid *pid, struct task_struct *task) 574 { 575 return do_task_stat(m, ns, pid, task, 0); 576 } 577 578 int proc_tgid_stat(struct seq_file *m, struct pid_namespace *ns, 579 struct pid *pid, struct task_struct *task) 580 { 581 return do_task_stat(m, ns, pid, task, 1); 582 } 583 584 int proc_pid_statm(struct seq_file *m, struct pid_namespace *ns, 585 struct pid *pid, struct task_struct *task) 586 { 587 unsigned long size = 0, resident = 0, shared = 0, text = 0, data = 0; 588 struct mm_struct *mm = get_task_mm(task); 589 590 if (mm) { 591 size = task_statm(mm, &shared, &text, &data, &resident); 592 mmput(mm); 593 } 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 */ 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); 607 seq_putc(m, '\n'); 608 609 return 0; 610 } 611 612 #ifdef CONFIG_PROC_CHILDREN 613 static struct pid * 614 get_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 664 out: 665 read_unlock(&tasklist_lock); 666 return pid; 667 } 668 669 static 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); 675 seq_printf(seq, "%d ", pid); 676 677 return 0; 678 } 679 680 static void *children_seq_start(struct seq_file *seq, loff_t *pos) 681 { 682 return get_children_pid(seq->private, NULL, *pos); 683 } 684 685 static 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 696 static void children_seq_stop(struct seq_file *seq, void *v) 697 { 698 put_pid(v); 699 } 700 701 static 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 708 static 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 723 int children_seq_release(struct inode *inode, struct file *file) 724 { 725 seq_release(inode, file); 726 return 0; 727 } 728 729 const 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 }; 735 #endif /* CONFIG_PROC_CHILDREN */ 736