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