2 * linux/fs/proc/array.c
4 * Copyright (C) 1992 by Linus Torvalds
5 * based on ideas by Darren Senn
8 * Michael. K. Johnson: stat,statm extensions.
9 * <johnsonm@stolaf.edu>
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>
17 * Danny ter Haar : added cpuinfo
20 * Alessandro Rubini : profile extension.
21 * <rubini@ipvvis.unipv.it>
23 * Jeff Tranter : added BogoMips field to cpuinfo
24 * <Jeff_Tranter@Mitel.COM>
26 * Bruno Haible : remove 4K limit for the maps file
27 * <haible@ma2s2.mathematik.uni-karlsruhe.de>
29 * Yves Arrouye : remove removal of trailing spaces in get_array.
30 * <Yves.Arrouye@marin.fdn.fr>
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>
39 * aeb@cwi.nl : /proc/partitions
42 * Alan Cox : security fixes.
43 * <Alan.Cox@linux.org>
45 * Al Viro : safe handling of mm_struct
47 * Gerhard Wichert : added BIGMEM support
48 * Siemens AG <Gerhard.Wichert@pdb.siemens.de>
50 * Al Viro & Jeff Garzik : moved most of the thing into base.c and
51 * : proc_misc.c. The rest may eventually go into
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/proc_fs.h>
64 #include <linux/ioport.h>
66 #include <linux/hugetlb.h>
67 #include <linux/pagemap.h>
68 #include <linux/swap.h>
69 #include <linux/slab.h>
70 #include <linux/smp.h>
71 #include <linux/signal.h>
72 #include <linux/highmem.h>
73 #include <linux/file.h>
74 #include <linux/times.h>
75 #include <linux/cpuset.h>
76 #include <linux/tracehook.h>
77 #include <linux/rcupdate.h>
78 #include <linux/delayacct.h>
79 #include <linux/vs_context.h>
80 #include <linux/vs_network.h>
82 #include <asm/uaccess.h>
83 #include <asm/pgtable.h>
85 #include <asm/processor.h>
88 /* Gcc optimizes away "strlen(x)" for constant x */
89 #define ADDBUF(buffer, string) \
90 do { memcpy(buffer, string, strlen(string)); \
91 buffer += strlen(string); } while (0)
93 static inline char * task_name(struct task_struct *p, char * buf)
97 char tcomm[sizeof(p->comm)];
99 get_task_comm(tcomm, p);
101 ADDBUF(buf, "Name:\t");
105 unsigned char c = *name;
129 * The task state array is a strange "bitmap" of
130 * reasons to sleep. Thus "running" is zero, and
131 * you can test for combinations of others with
134 static const char *task_state_array[] = {
135 "R (running)", /* 0 */
136 "S (sleeping)", /* 1 */
137 "D (disk sleep)", /* 2 */
138 "T (stopped)", /* 4 */
139 "T (tracing stop)", /* 8 */
140 "H (on hold)", /* 16 */
141 "Z (zombie)", /* 32 */
145 static inline const char * get_task_state(struct task_struct *tsk)
147 unsigned int state = (tsk->state & (TASK_RUNNING |
149 TASK_UNINTERRUPTIBLE |
153 (tsk->exit_state & (EXIT_ZOMBIE |
155 const char **p = &task_state_array[0];
164 static inline char * task_state(struct task_struct *p, char *buffer)
166 struct task_struct *tracer;
167 pid_t pid, ptgid, tracer_pid, tgid;
168 struct group_info *group_info;
170 struct fdtable *fdt = NULL;
173 tracer = tracehook_tracer_task(p);
174 tracer_pid = tracer == NULL ? 0 : tracer->pid;
175 tgid = vx_map_tgid(p->tgid);
176 pid = vx_map_pid(p->pid);
177 ptgid = vx_map_pid(pid_alive(p) ?
178 rcu_dereference(p->parent)->tgid : 0);
180 buffer += sprintf(buffer,
187 "Uid:\t%d\t%d\t%d\t%d\n"
188 "Gid:\t%d\t%d\t%d\t%d\n",
190 (p->sleep_avg/1024)*100/(1020000000/1024),
191 tgid, pid, (pid > 1) ? ptgid : 0,
193 p->uid, p->euid, p->suid, p->fsuid,
194 p->gid, p->egid, p->sgid, p->fsgid);
198 fdt = files_fdtable(p->files);
199 buffer += sprintf(buffer,
202 fdt ? fdt->max_fds : 0);
205 group_info = p->group_info;
206 get_group_info(group_info);
209 for (g = 0; g < min(group_info->ngroups,NGROUPS_SMALL); g++)
210 buffer += sprintf(buffer, "%d ", GROUP_AT(group_info,g));
211 put_group_info(group_info);
213 buffer += sprintf(buffer, "\n");
217 static char * render_sigset_t(const char *header, sigset_t *set, char *buffer)
221 len = strlen(header);
222 memcpy(buffer, header, len);
230 if (sigismember(set, i+1)) x |= 1;
231 if (sigismember(set, i+2)) x |= 2;
232 if (sigismember(set, i+3)) x |= 4;
233 if (sigismember(set, i+4)) x |= 8;
234 *buffer++ = (x < 10 ? '0' : 'a' - 10) + x;
242 static void collect_sigign_sigcatch(struct task_struct *p, sigset_t *ign,
245 struct k_sigaction *k;
248 k = p->sighand->action;
249 for (i = 1; i <= _NSIG; ++i, ++k) {
250 if (k->sa.sa_handler == SIG_IGN)
252 else if (k->sa.sa_handler != SIG_DFL)
257 static inline char * task_sig(struct task_struct *p, char *buffer)
260 sigset_t pending, shpending, blocked, ignored, caught;
262 unsigned long qsize = 0;
263 unsigned long qlim = 0;
265 sigemptyset(&pending);
266 sigemptyset(&shpending);
267 sigemptyset(&blocked);
268 sigemptyset(&ignored);
269 sigemptyset(&caught);
272 if (lock_task_sighand(p, &flags)) {
273 pending = p->pending.signal;
274 shpending = p->signal->shared_pending.signal;
275 blocked = p->blocked;
276 collect_sigign_sigcatch(p, &ignored, &caught);
277 num_threads = atomic_read(&p->signal->count);
278 qsize = atomic_read(&p->user->sigpending);
279 qlim = p->signal->rlim[RLIMIT_SIGPENDING].rlim_cur;
280 unlock_task_sighand(p, &flags);
284 buffer += sprintf(buffer, "Threads:\t%d\n", num_threads);
285 buffer += sprintf(buffer, "SigQ:\t%lu/%lu\n", qsize, qlim);
287 /* render them all */
288 buffer = render_sigset_t("SigPnd:\t", &pending, buffer);
289 buffer = render_sigset_t("ShdPnd:\t", &shpending, buffer);
290 buffer = render_sigset_t("SigBlk:\t", &blocked, buffer);
291 buffer = render_sigset_t("SigIgn:\t", &ignored, buffer);
292 buffer = render_sigset_t("SigCgt:\t", &caught, buffer);
297 static inline char *task_cap(struct task_struct *p, char *buffer)
299 struct vx_info *vxi = p->vx_info;
301 return buffer + sprintf(buffer,
305 (unsigned)vx_info_mbcap(vxi, p->cap_inheritable),
306 (unsigned)vx_info_mbcap(vxi, p->cap_permitted),
307 (unsigned)vx_info_mbcap(vxi, p->cap_effective));
310 int proc_pid_status(struct task_struct *task, char * buffer)
312 char * orig = buffer;
313 #ifdef CONFIG_VSERVER_LEGACY
316 #ifdef CONFIG_VSERVER_LEGACYNET
319 struct mm_struct *mm = get_task_mm(task);
321 buffer = task_name(task, buffer);
322 buffer = task_state(task, buffer);
325 buffer = task_mem(mm, buffer);
328 buffer = task_sig(task, buffer);
329 buffer = task_cap(task, buffer);
330 buffer = cpuset_task_status_allowed(task, buffer);
332 if (task_vx_flags(task, VXF_HIDE_VINFO, 0))
334 #ifdef CONFIG_VSERVER_LEGACY
335 buffer += sprintf (buffer,"s_context: %d\n", vx_task_xid(task));
336 vxi = task_get_vx_info(task);
338 buffer += sprintf (buffer,"ctxflags: %08llx\n"
339 ,(unsigned long long)vxi->vx_flags);
340 buffer += sprintf (buffer,"initpid: %d\n"
343 buffer += sprintf (buffer,"ctxflags: none\n");
344 buffer += sprintf (buffer,"initpid: none\n");
348 buffer += sprintf (buffer,"VxID: %d\n", vx_task_xid(task));
350 #ifdef CONFIG_VSERVER_LEGACYNET
351 nxi = task_get_nx_info(task);
355 buffer += sprintf (buffer,"ipv4root:");
356 for (i=0; i<nxi->nbipv4; i++){
357 buffer += sprintf (buffer," %08x/%08x"
362 buffer += sprintf (buffer,"ipv4root_bcast: %08x\n"
365 buffer += sprintf (buffer,"ipv4root: 0\n");
366 buffer += sprintf (buffer,"ipv4root_bcast: 0\n");
371 #if defined(CONFIG_S390)
372 buffer = task_show_regs(task, buffer);
374 return buffer - orig;
377 static int do_task_stat(struct task_struct *task, char * buffer, int whole)
379 unsigned long vsize, eip, esp, wchan = ~0UL;
381 int tty_pgrp = -1, tty_nr = 0;
382 sigset_t sigign, sigcatch;
385 pid_t pid = 0, ppid = 0, pgid = -1, sid = -1;
387 struct mm_struct *mm;
388 unsigned long long start_time;
389 unsigned long cmin_flt = 0, cmaj_flt = 0;
390 unsigned long min_flt = 0, maj_flt = 0;
391 cputime_t cutime, cstime, utime, stime;
392 unsigned long rsslim = 0;
393 char tcomm[sizeof(task->comm)];
396 state = *get_task_state(task);
397 vsize = eip = esp = 0;
398 mm = get_task_mm(task);
400 vsize = task_vsize(mm);
401 eip = KSTK_EIP(task);
402 esp = KSTK_ESP(task);
405 get_task_comm(tcomm, task);
407 sigemptyset(&sigign);
408 sigemptyset(&sigcatch);
409 cutime = cstime = utime = stime = cputime_zero;
412 if (lock_task_sighand(task, &flags)) {
413 struct signal_struct *sig = task->signal;
416 tty_pgrp = sig->tty->pgrp;
417 tty_nr = new_encode_dev(tty_devnum(sig->tty));
420 num_threads = atomic_read(&sig->count);
421 collect_sigign_sigcatch(task, &sigign, &sigcatch);
423 cmin_flt = sig->cmin_flt;
424 cmaj_flt = sig->cmaj_flt;
425 cutime = sig->cutime;
426 cstime = sig->cstime;
427 rsslim = sig->rlim[RLIMIT_RSS].rlim_cur;
429 /* add up live thread stats at the group level */
431 struct task_struct *t = task;
433 min_flt += t->min_flt;
434 maj_flt += t->maj_flt;
435 utime = cputime_add(utime, t->utime);
436 stime = cputime_add(stime, t->stime);
440 min_flt += sig->min_flt;
441 maj_flt += sig->maj_flt;
442 utime = cputime_add(utime, sig->utime);
443 stime = cputime_add(stime, sig->stime);
446 sid = signal_session(sig);
447 pid = vx_info_map_pid(task->vx_info, task->pid);
448 pgid = vx_info_map_pid(task->vx_info, process_group(task));
449 ppid = (pid > 1) ? vx_info_map_tgid(task->vx_info,
450 rcu_dereference(task->parent)->tgid) : 0;
452 unlock_task_sighand(task, &flags);
456 if (!whole || num_threads<2) {
458 if (current->uid == task->uid || current->euid == task->uid ||
459 capable(CAP_SYS_NICE))
460 wchan = get_wchan(task);
463 min_flt = task->min_flt;
464 maj_flt = task->maj_flt;
469 /* scale priority and nice values from timeslices to -20..20 */
470 /* to make it look like a "normal" Unix priority/nice value */
471 priority = task_prio(task);
472 nice = task_nice(task);
474 /* Temporary variable needed for gcc-2.96 */
475 /* convert timespec -> nsec*/
476 start_time = (unsigned long long)task->start_time.tv_sec * NSEC_PER_SEC
477 + task->start_time.tv_nsec;
478 /* convert nsec -> ticks */
479 start_time = nsec_to_clock_t(start_time);
481 /* fixup start time for virt uptime */
482 if (vx_flags(VXF_VIRT_UPTIME, 0)) {
483 unsigned long long bias =
484 current->vx_info->cvirt.bias_clock;
486 if (start_time > bias)
492 res = sprintf(buffer,"%d (%s) %c %d %d %d %d %d %lu %lu \
493 %lu %lu %lu %lu %lu %ld %ld %ld %ld %d 0 %llu %lu %ld %lu %lu %lu %lu %lu \
494 %lu %lu %lu %lu %lu %lu %lu %lu %d %d %lu %lu %llu\n",
508 cputime_to_clock_t(utime),
509 cputime_to_clock_t(stime),
510 cputime_to_clock_t(cutime),
511 cputime_to_clock_t(cstime),
517 mm ? get_mm_rss(mm) : 0,
519 mm ? mm->start_code : 0,
520 mm ? mm->end_code : 0,
521 mm ? mm->start_stack : 0,
524 /* The signal information here is obsolete.
525 * It must be decimal for Linux 2.0 compatibility.
526 * Use /proc/#/status for real-time signals.
528 task->pending.signal.sig[0] & 0x7fffffffUL,
529 task->blocked.sig[0] & 0x7fffffffUL,
530 sigign .sig[0] & 0x7fffffffUL,
531 sigcatch .sig[0] & 0x7fffffffUL,
539 (unsigned long long)delayacct_blkio_ticks(task));
545 int proc_tid_stat(struct task_struct *task, char * buffer)
547 return do_task_stat(task, buffer, 0);
550 int proc_tgid_stat(struct task_struct *task, char * buffer)
552 return do_task_stat(task, buffer, 1);
555 int proc_pid_statm(struct task_struct *task, char *buffer)
557 int size = 0, resident = 0, shared = 0, text = 0, lib = 0, data = 0;
558 struct mm_struct *mm = get_task_mm(task);
561 size = task_statm(mm, &shared, &text, &data, &resident);
565 return sprintf(buffer,"%d %d %d %d %d %d %d\n",
566 size, resident, shared, text, lib, data, 0);