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>
80 #include <asm/uaccess.h>
81 #include <asm/pgtable.h>
83 #include <asm/processor.h>
86 /* Gcc optimizes away "strlen(x)" for constant x */
87 #define ADDBUF(buffer, string) \
88 do { memcpy(buffer, string, strlen(string)); \
89 buffer += strlen(string); } while (0)
91 static inline char * task_name(struct task_struct *p, char * buf)
95 char tcomm[sizeof(p->comm)];
97 get_task_comm(tcomm, p);
99 ADDBUF(buf, "Name:\t");
103 unsigned char c = *name;
127 * The task state array is a strange "bitmap" of
128 * reasons to sleep. Thus "running" is zero, and
129 * you can test for combinations of others with
132 static const char *task_state_array[] = {
133 "R (running)", /* 0 */
134 "S (sleeping)", /* 1 */
135 "D (disk sleep)", /* 2 */
136 "T (stopped)", /* 4 */
137 "T (tracing stop)", /* 8 */
138 "Z (zombie)", /* 16 */
140 "N (noninteractive)", /* 64 */
141 "H (on hold)" /* 128 */
144 static inline const char * get_task_state(struct task_struct *tsk)
146 unsigned int state = (tsk->state & (TASK_RUNNING |
148 TASK_UNINTERRUPTIBLE |
152 (tsk->exit_state & (EXIT_ZOMBIE |
154 const char **p = &task_state_array[0];
163 static inline char * task_state(struct task_struct *p, char *buffer)
165 struct task_struct *tracer;
166 pid_t tracer_pid, pid, ptgid, tgid;
167 struct group_info *group_info;
169 struct fdtable *fdt = NULL;
172 tracer = tracehook_tracer_task(p);
173 tracer_pid = tracer == NULL ? 0 : vx_map_pid(tracer->pid);
176 read_lock(&tasklist_lock);
177 tgid = vx_map_tgid(p->tgid);
178 pid = vx_map_pid(p->pid);
179 ptgid = vx_map_pid(p->group_leader->parent->tgid);
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);
195 read_unlock(&tasklist_lock);
199 fdt = files_fdtable(p->files);
200 buffer += sprintf(buffer,
203 fdt ? fdt->max_fds : 0);
206 group_info = p->group_info;
207 get_group_info(group_info);
210 for (g = 0; g < min(group_info->ngroups,NGROUPS_SMALL); g++)
211 buffer += sprintf(buffer, "%d ", GROUP_AT(group_info,g));
212 put_group_info(group_info);
214 buffer += sprintf(buffer, "\n");
218 static char * render_sigset_t(const char *header, sigset_t *set, char *buffer)
222 len = strlen(header);
223 memcpy(buffer, header, len);
231 if (sigismember(set, i+1)) x |= 1;
232 if (sigismember(set, i+2)) x |= 2;
233 if (sigismember(set, i+3)) x |= 4;
234 if (sigismember(set, i+4)) x |= 8;
235 *buffer++ = (x < 10 ? '0' : 'a' - 10) + x;
243 static void collect_sigign_sigcatch(struct task_struct *p, sigset_t *ign,
246 struct k_sigaction *k;
249 k = p->sighand->action;
250 for (i = 1; i <= _NSIG; ++i, ++k) {
251 if (k->sa.sa_handler == SIG_IGN)
253 else if (k->sa.sa_handler != SIG_DFL)
258 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);
271 /* Gather all the data with the appropriate locks held */
272 read_lock(&tasklist_lock);
274 spin_lock_irq(&p->sighand->siglock);
275 pending = p->pending.signal;
276 shpending = p->signal->shared_pending.signal;
277 blocked = p->blocked;
278 collect_sigign_sigcatch(p, &ignored, &caught);
279 num_threads = atomic_read(&p->signal->count);
280 qsize = atomic_read(&p->user->sigpending);
281 qlim = p->signal->rlim[RLIMIT_SIGPENDING].rlim_cur;
282 spin_unlock_irq(&p->sighand->siglock);
284 read_unlock(&tasklist_lock);
286 buffer += sprintf(buffer, "Threads:\t%d\n", num_threads);
287 buffer += sprintf(buffer, "SigQ:\t%lu/%lu\n", qsize, qlim);
289 /* render them all */
290 buffer = render_sigset_t("SigPnd:\t", &pending, buffer);
291 buffer = render_sigset_t("ShdPnd:\t", &shpending, buffer);
292 buffer = render_sigset_t("SigBlk:\t", &blocked, buffer);
293 buffer = render_sigset_t("SigIgn:\t", &ignored, buffer);
294 buffer = render_sigset_t("SigCgt:\t", &caught, buffer);
299 static inline char *task_cap(struct task_struct *p, char *buffer)
301 return buffer + sprintf(buffer, "CapInh:\t%016x\n"
304 cap_t(p->cap_inheritable),
305 cap_t(p->cap_permitted),
306 cap_t(p->cap_effective));
309 int proc_pid_status(struct task_struct *task, char * buffer)
311 char * orig = buffer;
312 #ifdef CONFIG_VSERVER_LEGACY
315 #ifdef CONFIG_VSERVER_LEGACYNET
318 struct mm_struct *mm = get_task_mm(task);
320 buffer = task_name(task, buffer);
321 buffer = task_state(task, buffer);
324 buffer = task_mem(mm, buffer);
327 buffer = task_sig(task, buffer);
328 buffer = task_cap(task, buffer);
329 buffer = cpuset_task_status_allowed(task, buffer);
331 if (task_vx_flags(task, VXF_INFO_HIDE, 0))
333 #ifdef CONFIG_VSERVER_LEGACY
334 buffer += sprintf (buffer,"s_context: %d\n", vx_task_xid(task));
335 vxi = task_get_vx_info(task);
337 buffer += sprintf (buffer,"ctxflags: %08llx\n"
338 ,(unsigned long long)vxi->vx_flags);
339 buffer += sprintf (buffer,"initpid: %d\n"
342 buffer += sprintf (buffer,"ctxflags: none\n");
343 buffer += sprintf (buffer,"initpid: none\n");
347 buffer += sprintf (buffer,"VxID: %d\n", vx_task_xid(task));
349 #ifdef CONFIG_VSERVER_LEGACYNET
350 nxi = task_get_nx_info(task);
354 buffer += sprintf (buffer,"ipv4root:");
355 for (i=0; i<nxi->nbipv4; i++){
356 buffer += sprintf (buffer," %08x/%08x"
361 buffer += sprintf (buffer,"ipv4root_bcast: %08x\n"
364 buffer += sprintf (buffer,"ipv4root: 0\n");
365 buffer += sprintf (buffer,"ipv4root_bcast: 0\n");
370 #if defined(CONFIG_S390)
371 buffer = task_show_regs(task, buffer);
373 return buffer - orig;
376 static int do_task_stat(struct task_struct *task, char * buffer, int whole)
378 unsigned long vsize, eip, esp, wchan = ~0UL;
380 int tty_pgrp = -1, tty_nr = 0;
381 sigset_t sigign, sigcatch;
384 pid_t pid, ppid, pgid = -1, sid = -1;
386 struct mm_struct *mm;
387 unsigned long long start_time;
388 unsigned long cmin_flt = 0, cmaj_flt = 0;
389 unsigned long min_flt = 0, maj_flt = 0;
390 cputime_t cutime, cstime, utime, stime;
391 unsigned long rsslim = 0;
392 struct task_struct *t;
393 char tcomm[sizeof(task->comm)];
395 state = *get_task_state(task);
396 vsize = eip = esp = 0;
397 mm = get_task_mm(task);
399 vsize = task_vsize(mm);
400 eip = KSTK_EIP(task);
401 esp = KSTK_ESP(task);
404 get_task_comm(tcomm, task);
406 sigemptyset(&sigign);
407 sigemptyset(&sigcatch);
408 cutime = cstime = utime = stime = cputime_zero;
410 mutex_lock(&tty_mutex);
411 read_lock(&tasklist_lock);
413 spin_lock_irq(&task->sighand->siglock);
414 num_threads = atomic_read(&task->signal->count);
415 collect_sigign_sigcatch(task, &sigign, &sigcatch);
417 /* add up live thread stats at the group level */
421 min_flt += t->min_flt;
422 maj_flt += t->maj_flt;
423 utime = cputime_add(utime, t->utime);
424 stime = cputime_add(stime, t->stime);
429 spin_unlock_irq(&task->sighand->siglock);
432 if (task->signal->tty) {
433 tty_pgrp = task->signal->tty->pgrp;
434 tty_nr = new_encode_dev(tty_devnum(task->signal->tty));
436 pgid = process_group(task);
437 sid = task->signal->session;
438 cmin_flt = task->signal->cmin_flt;
439 cmaj_flt = task->signal->cmaj_flt;
440 cutime = task->signal->cutime;
441 cstime = task->signal->cstime;
442 rsslim = task->signal->rlim[RLIMIT_RSS].rlim_cur;
444 min_flt += task->signal->min_flt;
445 maj_flt += task->signal->maj_flt;
446 utime = cputime_add(utime, task->signal->utime);
447 stime = cputime_add(stime, task->signal->stime);
450 pid = vx_info_map_pid(task->vx_info, pid_alive(task) ? task->pid : 0);
451 ppid = (!(pid > 1)) ? 0 : vx_info_map_tgid(task->vx_info,
452 task->group_leader->parent->tgid);
453 pgid = vx_info_map_pid(task->vx_info, pgid);
455 read_unlock(&tasklist_lock);
456 mutex_unlock(&tty_mutex);
458 if (!whole || num_threads<2) {
460 if (current->uid == task->uid || current->euid == task->uid ||
461 capable(CAP_SYS_NICE))
462 wchan = get_wchan(task);
465 min_flt = task->min_flt;
466 maj_flt = task->maj_flt;
471 /* scale priority and nice values from timeslices to -20..20 */
472 /* to make it look like a "normal" Unix priority/nice value */
473 priority = task_prio(task);
474 nice = task_nice(task);
476 /* Temporary variable needed for gcc-2.96 */
477 /* convert timespec -> nsec*/
478 start_time = (unsigned long long)task->start_time.tv_sec * NSEC_PER_SEC
479 + task->start_time.tv_nsec;
480 /* convert nsec -> ticks */
481 start_time = nsec_to_clock_t(start_time);
483 /* fixup start time for virt uptime */
484 if (vx_flags(VXF_VIRT_UPTIME, 0)) {
485 unsigned long long bias =
486 current->vx_info->cvirt.bias_clock;
488 if (start_time > bias)
494 res = sprintf(buffer,"%d (%s) %c %d %d %d %d %d %lu %lu \
495 %lu %lu %lu %lu %lu %ld %ld %ld %ld %d 0 %llu %lu %ld %lu %lu %lu %lu %lu \
496 %lu %lu %lu %lu %lu %lu %lu %lu %d %d %lu %lu %llu\n",
510 cputime_to_clock_t(utime),
511 cputime_to_clock_t(stime),
512 cputime_to_clock_t(cutime),
513 cputime_to_clock_t(cstime),
519 mm ? get_mm_rss(mm) : 0,
521 mm ? mm->start_code : 0,
522 mm ? mm->end_code : 0,
523 mm ? mm->start_stack : 0,
526 /* The signal information here is obsolete.
527 * It must be decimal for Linux 2.0 compatibility.
528 * Use /proc/#/status for real-time signals.
530 task->pending.signal.sig[0] & 0x7fffffffUL,
531 task->blocked.sig[0] & 0x7fffffffUL,
532 sigign .sig[0] & 0x7fffffffUL,
533 sigcatch .sig[0] & 0x7fffffffUL,
541 (unsigned long long)delayacct_blkio_ticks(task));
547 int proc_tid_stat(struct task_struct *task, char * buffer)
549 return do_task_stat(task, buffer, 0);
552 int proc_tgid_stat(struct task_struct *task, char * buffer)
554 return do_task_stat(task, buffer, 1);
557 int proc_pid_statm(struct task_struct *task, char *buffer)
559 int size = 0, resident = 0, shared = 0, text = 0, lib = 0, data = 0;
560 struct mm_struct *mm = get_task_mm(task);
563 size = task_statm(mm, &shared, &text, &data, &resident);
567 return sprintf(buffer,"%d %d %d %d %d %d %d\n",
568 size, resident, shared, text, lib, data, 0);