fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / fs / proc / array.c
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.Cox@linux.org>
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/proc_fs.h>
64 #include <linux/ioport.h>
65 #include <linux/mm.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>
81
82 #include <asm/uaccess.h>
83 #include <asm/pgtable.h>
84 #include <asm/io.h>
85 #include <asm/processor.h>
86 #include "internal.h"
87
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)
92
93 static inline char * task_name(struct task_struct *p, char * buf)
94 {
95         int i;
96         char * name;
97         char tcomm[sizeof(p->comm)];
98
99         get_task_comm(tcomm, p);
100
101         ADDBUF(buf, "Name:\t");
102         name = tcomm;
103         i = sizeof(tcomm);
104         do {
105                 unsigned char c = *name;
106                 name++;
107                 i--;
108                 *buf = c;
109                 if (!c)
110                         break;
111                 if (c == '\\') {
112                         buf[1] = c;
113                         buf += 2;
114                         continue;
115                 }
116                 if (c == '\n') {
117                         buf[0] = '\\';
118                         buf[1] = 'n';
119                         buf += 2;
120                         continue;
121                 }
122                 buf++;
123         } while (i);
124         *buf = '\n';
125         return buf+1;
126 }
127
128 /*
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
132  * simple bit tests.
133  */
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 */
142         "X (dead)",             /* 64 */
143 };
144
145 static inline const char * get_task_state(struct task_struct *tsk)
146 {
147         unsigned int state = (tsk->state & (TASK_RUNNING |
148                                             TASK_INTERRUPTIBLE |
149                                             TASK_UNINTERRUPTIBLE |
150                                             TASK_STOPPED |
151                                            TASK_TRACED |
152                                            TASK_ONHOLD)) |
153                         (tsk->exit_state & (EXIT_ZOMBIE |
154                                             EXIT_DEAD));
155         const char **p = &task_state_array[0];
156
157         while (state) {
158                 p++;
159                 state >>= 1;
160         }
161         return *p;
162 }
163
164 static inline char * task_state(struct task_struct *p, char *buffer)
165 {
166         struct task_struct *tracer;
167         pid_t pid, ptgid, tracer_pid, tgid;
168         struct group_info *group_info;
169         int g;
170         struct fdtable *fdt = NULL;
171
172         rcu_read_lock();
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);
179
180         buffer += sprintf(buffer,
181                 "State:\t%s\n"
182                 "SleepAVG:\t%lu%%\n"
183                 "Tgid:\t%d\n"
184                 "Pid:\t%d\n"
185                 "PPid:\t%d\n"
186                 "TracerPid:\t%d\n"
187                 "Uid:\t%d\t%d\t%d\t%d\n"
188                 "Gid:\t%d\t%d\t%d\t%d\n",
189                 get_task_state(p),
190                 (p->sleep_avg/1024)*100/(1020000000/1024),
191                 tgid, pid, (pid > 1) ? ptgid : 0,
192                 tracer_pid,
193                 p->uid, p->euid, p->suid, p->fsuid,
194                 p->gid, p->egid, p->sgid, p->fsgid);
195
196         task_lock(p);
197         if (p->files)
198                 fdt = files_fdtable(p->files);
199         buffer += sprintf(buffer,
200                 "FDSize:\t%d\n"
201                 "Groups:\t",
202                 fdt ? fdt->max_fds : 0);
203         rcu_read_unlock();
204
205         group_info = p->group_info;
206         get_group_info(group_info);
207         task_unlock(p);
208
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);
212
213         buffer += sprintf(buffer, "\n");
214         return buffer;
215 }
216
217 static char * render_sigset_t(const char *header, sigset_t *set, char *buffer)
218 {
219         int i, len;
220
221         len = strlen(header);
222         memcpy(buffer, header, len);
223         buffer += len;
224
225         i = _NSIG;
226         do {
227                 int x = 0;
228
229                 i -= 4;
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;
235         } while (i >= 4);
236
237         *buffer++ = '\n';
238         *buffer = 0;
239         return buffer;
240 }
241
242 static void collect_sigign_sigcatch(struct task_struct *p, sigset_t *ign,
243                                     sigset_t *catch)
244 {
245         struct k_sigaction *k;
246         int i;
247
248         k = p->sighand->action;
249         for (i = 1; i <= _NSIG; ++i, ++k) {
250                 if (k->sa.sa_handler == SIG_IGN)
251                         sigaddset(ign, i);
252                 else if (k->sa.sa_handler != SIG_DFL)
253                         sigaddset(catch, i);
254         }
255 }
256
257 static inline char * task_sig(struct task_struct *p, char *buffer)
258 {
259         unsigned long flags;
260         sigset_t pending, shpending, blocked, ignored, caught;
261         int num_threads = 0;
262         unsigned long qsize = 0;
263         unsigned long qlim = 0;
264
265         sigemptyset(&pending);
266         sigemptyset(&shpending);
267         sigemptyset(&blocked);
268         sigemptyset(&ignored);
269         sigemptyset(&caught);
270
271         rcu_read_lock();
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);
281         }
282         rcu_read_unlock();
283
284         buffer += sprintf(buffer, "Threads:\t%d\n", num_threads);
285         buffer += sprintf(buffer, "SigQ:\t%lu/%lu\n", qsize, qlim);
286
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);
293
294         return buffer;
295 }
296
297 static inline char *task_cap(struct task_struct *p, char *buffer)
298 {
299         struct vx_info *vxi = p->vx_info;
300
301         return buffer + sprintf(buffer,
302                 "CapInh:\t%016x\n"
303                 "CapPrm:\t%016x\n"
304                 "CapEff:\t%016x\n",
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));
308 }
309
310 int proc_pid_status(struct task_struct *task, char * buffer)
311 {
312         char * orig = buffer;
313 #ifdef  CONFIG_VSERVER_LEGACY
314         struct vx_info *vxi;
315 #endif
316 #ifdef  CONFIG_VSERVER_LEGACYNET
317         struct nx_info *nxi;
318 #endif
319         struct mm_struct *mm = get_task_mm(task);
320
321         buffer = task_name(task, buffer);
322         buffer = task_state(task, buffer);
323  
324         if (mm) {
325                 buffer = task_mem(mm, buffer);
326                 mmput(mm);
327         }
328         buffer = task_sig(task, buffer);
329         buffer = task_cap(task, buffer);
330         buffer = cpuset_task_status_allowed(task, buffer);
331
332         if (task_vx_flags(task, VXF_HIDE_VINFO, 0))
333                 goto skip;
334 #ifdef  CONFIG_VSERVER_LEGACY
335         buffer += sprintf (buffer,"s_context: %d\n", vx_task_xid(task));
336         vxi = task_get_vx_info(task);
337         if (vxi) {
338                 buffer += sprintf (buffer,"ctxflags: %08llx\n"
339                         ,(unsigned long long)vxi->vx_flags);
340                 buffer += sprintf (buffer,"initpid: %d\n"
341                         ,vxi->vx_initpid);
342         } else {
343                 buffer += sprintf (buffer,"ctxflags: none\n");
344                 buffer += sprintf (buffer,"initpid: none\n");
345         }
346         put_vx_info(vxi);
347 #else
348         buffer += sprintf (buffer,"VxID: %d\n", vx_task_xid(task));
349 #endif
350 #ifdef  CONFIG_VSERVER_LEGACYNET
351         nxi = task_get_nx_info(task);
352         if (nxi) {
353                 int i;
354
355                 buffer += sprintf (buffer,"ipv4root:");
356                 for (i=0; i<nxi->nbipv4; i++){
357                         buffer += sprintf (buffer," %08x/%08x"
358                                 ,nxi->ipv4[i]
359                                 ,nxi->mask[i]);
360                 }
361                 *buffer++ = '\n';
362                 buffer += sprintf (buffer,"ipv4root_bcast: %08x\n"
363                         ,nxi->v4_bcast);
364         } else {
365                 buffer += sprintf (buffer,"ipv4root: 0\n");
366                 buffer += sprintf (buffer,"ipv4root_bcast: 0\n");
367         }
368         put_nx_info(nxi);
369 #endif
370 skip:
371 #if defined(CONFIG_S390)
372         buffer = task_show_regs(task, buffer);
373 #endif
374         return buffer - orig;
375 }
376
377 static int do_task_stat(struct task_struct *task, char * buffer, int whole)
378 {
379         unsigned long vsize, eip, esp, wchan = ~0UL;
380         long priority, nice;
381         int tty_pgrp = -1, tty_nr = 0;
382         sigset_t sigign, sigcatch;
383         char state;
384         int res;
385         pid_t pid = 0, ppid = 0, pgid = -1, sid = -1;
386         int num_threads = 0;
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)];
394         unsigned long flags;
395
396         state = *get_task_state(task);
397         vsize = eip = esp = 0;
398         mm = get_task_mm(task);
399         if (mm) {
400                 vsize = task_vsize(mm);
401                 eip = KSTK_EIP(task);
402                 esp = KSTK_ESP(task);
403         }
404
405         get_task_comm(tcomm, task);
406
407         sigemptyset(&sigign);
408         sigemptyset(&sigcatch);
409         cutime = cstime = utime = stime = cputime_zero;
410
411         rcu_read_lock();
412         if (lock_task_sighand(task, &flags)) {
413                 struct signal_struct *sig = task->signal;
414
415                 if (sig->tty) {
416                         tty_pgrp = sig->tty->pgrp;
417                         tty_nr = new_encode_dev(tty_devnum(sig->tty));
418                 }
419
420                 num_threads = atomic_read(&sig->count);
421                 collect_sigign_sigcatch(task, &sigign, &sigcatch);
422
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;
428
429                 /* add up live thread stats at the group level */
430                 if (whole) {
431                         struct task_struct *t = task;
432                         do {
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);
437                                 t = next_thread(t);
438                         } while (t != task);
439
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);
444                 }
445
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;
451
452                 unlock_task_sighand(task, &flags);
453         }
454         rcu_read_unlock();
455
456         if (!whole || num_threads<2) {
457                 wchan = 0;
458                 if (current->uid == task->uid || current->euid == task->uid ||
459                                 capable(CAP_SYS_NICE))
460                         wchan = get_wchan(task);
461         }
462         if (!whole) {
463                 min_flt = task->min_flt;
464                 maj_flt = task->maj_flt;
465                 utime = task->utime;
466                 stime = task->stime;
467         }
468
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);
473
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);
480
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;
485
486                 if (start_time > bias)
487                         start_time -= bias;
488                 else
489                         start_time = 0;
490         }
491
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",
495                 pid,
496                 tcomm,
497                 state,
498                 ppid,
499                 pgid,
500                 sid,
501                 tty_nr,
502                 tty_pgrp,
503                 task->flags,
504                 min_flt,
505                 cmin_flt,
506                 maj_flt,
507                 cmaj_flt,
508                 cputime_to_clock_t(utime),
509                 cputime_to_clock_t(stime),
510                 cputime_to_clock_t(cutime),
511                 cputime_to_clock_t(cstime),
512                 priority,
513                 nice,
514                 num_threads,
515                 start_time,
516                 vsize,
517                 mm ? get_mm_rss(mm) : 0,
518                 rsslim,
519                 mm ? mm->start_code : 0,
520                 mm ? mm->end_code : 0,
521                 mm ? mm->start_stack : 0,
522                 esp,
523                 eip,
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.
527                  */
528                 task->pending.signal.sig[0] & 0x7fffffffUL,
529                 task->blocked.sig[0] & 0x7fffffffUL,
530                 sigign      .sig[0] & 0x7fffffffUL,
531                 sigcatch    .sig[0] & 0x7fffffffUL,
532                 wchan,
533                 0UL,
534                 0UL,
535                 task->exit_signal,
536                 task_cpu(task),
537                 task->rt_priority,
538                 task->policy,
539                 (unsigned long long)delayacct_blkio_ticks(task));
540         if(mm)
541                 mmput(mm);
542         return res;
543 }
544
545 int proc_tid_stat(struct task_struct *task, char * buffer)
546 {
547         return do_task_stat(task, buffer, 0);
548 }
549
550 int proc_tgid_stat(struct task_struct *task, char * buffer)
551 {
552         return do_task_stat(task, buffer, 1);
553 }
554
555 int proc_pid_statm(struct task_struct *task, char *buffer)
556 {
557         int size = 0, resident = 0, shared = 0, text = 0, lib = 0, data = 0;
558         struct mm_struct *mm = get_task_mm(task);
559         
560         if (mm) {
561                 size = task_statm(mm, &shared, &text, &data, &resident);
562                 mmput(mm);
563         }
564
565         return sprintf(buffer,"%d %d %d %d %d %d %d\n",
566                        size, resident, shared, text, lib, data, 0);
567 }