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 "Z (zombie)", /* 16 */
142 "N (noninteractive)", /* 64 */
143 "H (on hold)" /* 128 */
146 static inline const char * get_task_state(struct task_struct *tsk)
148 unsigned int state = (tsk->state & (TASK_RUNNING |
150 TASK_UNINTERRUPTIBLE |
154 (tsk->exit_state & (EXIT_ZOMBIE |
156 const char **p = &task_state_array[0];
165 static inline char * task_state(struct task_struct *p, char *buffer)
167 struct task_struct *tracer;
168 pid_t tracer_pid, pid, ptgid, tgid;
169 struct group_info *group_info;
171 struct fdtable *fdt = NULL;
174 tracer = tracehook_tracer_task(p);
175 tracer_pid = tracer == NULL ? 0 : vx_map_pid(tracer->pid);
178 read_lock(&tasklist_lock);
179 tgid = vx_map_tgid(p->tgid);
180 pid = vx_map_pid(p->pid);
181 ptgid = vx_map_pid(p->group_leader->parent->tgid);
182 buffer += sprintf(buffer,
189 "Uid:\t%d\t%d\t%d\t%d\n"
190 "Gid:\t%d\t%d\t%d\t%d\n",
192 (p->sleep_avg/1024)*100/(1020000000/1024),
193 tgid, pid, (pid > 1) ? ptgid : 0,
195 p->uid, p->euid, p->suid, p->fsuid,
196 p->gid, p->egid, p->sgid, p->fsgid);
197 read_unlock(&tasklist_lock);
201 fdt = files_fdtable(p->files);
202 buffer += sprintf(buffer,
205 fdt ? fdt->max_fds : 0);
208 group_info = p->group_info;
209 get_group_info(group_info);
212 for (g = 0; g < min(group_info->ngroups,NGROUPS_SMALL); g++)
213 buffer += sprintf(buffer, "%d ", GROUP_AT(group_info,g));
214 put_group_info(group_info);
216 buffer += sprintf(buffer, "\n");
220 static char * render_sigset_t(const char *header, sigset_t *set, char *buffer)
224 len = strlen(header);
225 memcpy(buffer, header, len);
233 if (sigismember(set, i+1)) x |= 1;
234 if (sigismember(set, i+2)) x |= 2;
235 if (sigismember(set, i+3)) x |= 4;
236 if (sigismember(set, i+4)) x |= 8;
237 *buffer++ = (x < 10 ? '0' : 'a' - 10) + x;
245 static void collect_sigign_sigcatch(struct task_struct *p, sigset_t *ign,
248 struct k_sigaction *k;
251 k = p->sighand->action;
252 for (i = 1; i <= _NSIG; ++i, ++k) {
253 if (k->sa.sa_handler == SIG_IGN)
255 else if (k->sa.sa_handler != SIG_DFL)
260 static inline char * task_sig(struct task_struct *p, char *buffer)
262 sigset_t pending, shpending, blocked, ignored, caught;
264 unsigned long qsize = 0;
265 unsigned long qlim = 0;
267 sigemptyset(&pending);
268 sigemptyset(&shpending);
269 sigemptyset(&blocked);
270 sigemptyset(&ignored);
271 sigemptyset(&caught);
273 /* Gather all the data with the appropriate locks held */
274 read_lock(&tasklist_lock);
276 spin_lock_irq(&p->sighand->siglock);
277 pending = p->pending.signal;
278 shpending = p->signal->shared_pending.signal;
279 blocked = p->blocked;
280 collect_sigign_sigcatch(p, &ignored, &caught);
281 num_threads = atomic_read(&p->signal->count);
282 qsize = atomic_read(&p->user->sigpending);
283 qlim = p->signal->rlim[RLIMIT_SIGPENDING].rlim_cur;
284 spin_unlock_irq(&p->sighand->siglock);
286 read_unlock(&tasklist_lock);
288 buffer += sprintf(buffer, "Threads:\t%d\n", num_threads);
289 buffer += sprintf(buffer, "SigQ:\t%lu/%lu\n", qsize, qlim);
291 /* render them all */
292 buffer = render_sigset_t("SigPnd:\t", &pending, buffer);
293 buffer = render_sigset_t("ShdPnd:\t", &shpending, buffer);
294 buffer = render_sigset_t("SigBlk:\t", &blocked, buffer);
295 buffer = render_sigset_t("SigIgn:\t", &ignored, buffer);
296 buffer = render_sigset_t("SigCgt:\t", &caught, buffer);
301 static inline char *task_cap(struct task_struct *p, char *buffer)
303 return buffer + sprintf(buffer, "CapInh:\t%016x\n"
306 cap_t(p->cap_inheritable),
307 cap_t(p->cap_permitted),
308 cap_t(p->cap_effective));
311 int proc_pid_status(struct task_struct *task, char * buffer)
313 char * orig = buffer;
314 #ifdef CONFIG_VSERVER_LEGACY
317 #ifdef CONFIG_VSERVER_LEGACYNET
320 struct mm_struct *mm = get_task_mm(task);
322 buffer = task_name(task, buffer);
323 buffer = task_state(task, buffer);
326 buffer = task_mem(mm, buffer);
329 buffer = task_sig(task, buffer);
330 buffer = task_cap(task, buffer);
331 buffer = cpuset_task_status_allowed(task, buffer);
333 if (task_vx_flags(task, VXF_INFO_HIDE, 0))
335 #ifdef CONFIG_VSERVER_LEGACY
336 buffer += sprintf (buffer,"s_context: %d\n", vx_task_xid(task));
337 vxi = task_get_vx_info(task);
339 buffer += sprintf (buffer,"ctxflags: %08llx\n"
340 ,(unsigned long long)vxi->vx_flags);
341 buffer += sprintf (buffer,"initpid: %d\n"
344 buffer += sprintf (buffer,"ctxflags: none\n");
345 buffer += sprintf (buffer,"initpid: none\n");
349 buffer += sprintf (buffer,"VxID: %d\n", vx_task_xid(task));
351 #ifdef CONFIG_VSERVER_LEGACYNET
352 nxi = task_get_nx_info(task);
356 buffer += sprintf (buffer,"ipv4root:");
357 for (i=0; i<nxi->nbipv4; i++){
358 buffer += sprintf (buffer," %08x/%08x"
363 buffer += sprintf (buffer,"ipv4root_bcast: %08x\n"
366 buffer += sprintf (buffer,"ipv4root: 0\n");
367 buffer += sprintf (buffer,"ipv4root_bcast: 0\n");
372 #if defined(CONFIG_S390)
373 buffer = task_show_regs(task, buffer);
375 return buffer - orig;
378 static int do_task_stat(struct task_struct *task, char * buffer, int whole)
380 unsigned long vsize, eip, esp, wchan = ~0UL;
382 int tty_pgrp = -1, tty_nr = 0;
383 sigset_t sigign, sigcatch;
386 pid_t pid, ppid, pgid = -1, sid = -1;
388 struct mm_struct *mm;
389 unsigned long long start_time;
390 unsigned long cmin_flt = 0, cmaj_flt = 0;
391 unsigned long min_flt = 0, maj_flt = 0;
392 cputime_t cutime, cstime, utime, stime;
393 unsigned long rsslim = 0;
394 struct task_struct *t;
395 char tcomm[sizeof(task->comm)];
397 state = *get_task_state(task);
398 vsize = eip = esp = 0;
399 mm = get_task_mm(task);
401 vsize = task_vsize(mm);
402 eip = KSTK_EIP(task);
403 esp = KSTK_ESP(task);
406 get_task_comm(tcomm, task);
408 sigemptyset(&sigign);
409 sigemptyset(&sigcatch);
410 cutime = cstime = utime = stime = cputime_zero;
412 mutex_lock(&tty_mutex);
413 read_lock(&tasklist_lock);
415 spin_lock_irq(&task->sighand->siglock);
416 num_threads = atomic_read(&task->signal->count);
417 collect_sigign_sigcatch(task, &sigign, &sigcatch);
419 /* add up live thread stats at the group level */
423 min_flt += t->min_flt;
424 maj_flt += t->maj_flt;
425 utime = cputime_add(utime, t->utime);
426 stime = cputime_add(stime, t->stime);
431 spin_unlock_irq(&task->sighand->siglock);
434 if (task->signal->tty) {
435 tty_pgrp = task->signal->tty->pgrp;
436 tty_nr = new_encode_dev(tty_devnum(task->signal->tty));
438 pgid = process_group(task);
439 sid = task->signal->session;
440 cmin_flt = task->signal->cmin_flt;
441 cmaj_flt = task->signal->cmaj_flt;
442 cutime = task->signal->cutime;
443 cstime = task->signal->cstime;
444 rsslim = task->signal->rlim[RLIMIT_RSS].rlim_cur;
446 min_flt += task->signal->min_flt;
447 maj_flt += task->signal->maj_flt;
448 utime = cputime_add(utime, task->signal->utime);
449 stime = cputime_add(stime, task->signal->stime);
452 pid = vx_info_map_pid(task->vx_info, pid_alive(task) ? task->pid : 0);
453 ppid = (!(pid > 1)) ? 0 : vx_info_map_tgid(task->vx_info,
454 task->group_leader->parent->tgid);
455 pgid = vx_info_map_pid(task->vx_info, pgid);
457 read_unlock(&tasklist_lock);
458 mutex_unlock(&tty_mutex);
460 if (!whole || num_threads<2) {
462 if (current->uid == task->uid || current->euid == task->uid ||
463 capable(CAP_SYS_NICE))
464 wchan = get_wchan(task);
467 min_flt = task->min_flt;
468 maj_flt = task->maj_flt;
473 /* scale priority and nice values from timeslices to -20..20 */
474 /* to make it look like a "normal" Unix priority/nice value */
475 priority = task_prio(task);
476 nice = task_nice(task);
478 /* Temporary variable needed for gcc-2.96 */
479 /* convert timespec -> nsec*/
480 start_time = (unsigned long long)task->start_time.tv_sec * NSEC_PER_SEC
481 + task->start_time.tv_nsec;
482 /* convert nsec -> ticks */
483 start_time = nsec_to_clock_t(start_time);
485 /* fixup start time for virt uptime */
486 if (vx_flags(VXF_VIRT_UPTIME, 0)) {
487 unsigned long long bias =
488 current->vx_info->cvirt.bias_clock;
490 if (start_time > bias)
496 res = sprintf(buffer,"%d (%s) %c %d %d %d %d %d %lu %lu \
497 %lu %lu %lu %lu %lu %ld %ld %ld %ld %d 0 %llu %lu %ld %lu %lu %lu %lu %lu \
498 %lu %lu %lu %lu %lu %lu %lu %lu %d %d %lu %lu %llu\n",
512 cputime_to_clock_t(utime),
513 cputime_to_clock_t(stime),
514 cputime_to_clock_t(cutime),
515 cputime_to_clock_t(cstime),
521 mm ? get_mm_rss(mm) : 0,
523 mm ? mm->start_code : 0,
524 mm ? mm->end_code : 0,
525 mm ? mm->start_stack : 0,
528 /* The signal information here is obsolete.
529 * It must be decimal for Linux 2.0 compatibility.
530 * Use /proc/#/status for real-time signals.
532 task->pending.signal.sig[0] & 0x7fffffffUL,
533 task->blocked.sig[0] & 0x7fffffffUL,
534 sigign .sig[0] & 0x7fffffffUL,
535 sigcatch .sig[0] & 0x7fffffffUL,
543 (unsigned long long)delayacct_blkio_ticks(task));
549 int proc_tid_stat(struct task_struct *task, char * buffer)
551 return do_task_stat(task, buffer, 0);
554 int proc_tgid_stat(struct task_struct *task, char * buffer)
556 return do_task_stat(task, buffer, 1);
559 int proc_pid_statm(struct task_struct *task, char *buffer)
561 int size = 0, resident = 0, shared = 0, text = 0, lib = 0, data = 0;
562 struct mm_struct *mm = get_task_mm(task);
565 size = task_statm(mm, &shared, &text, &data, &resident);
569 return sprintf(buffer,"%d %d %d %d %d %d %d\n",
570 size, resident, shared, text, lib, data, 0);