vserver 2.0 rc7
[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/config.h>
56 #include <linux/types.h>
57 #include <linux/errno.h>
58 #include <linux/time.h>
59 #include <linux/kernel.h>
60 #include <linux/kernel_stat.h>
61 #include <linux/tty.h>
62 #include <linux/string.h>
63 #include <linux/mman.h>
64 #include <linux/proc_fs.h>
65 #include <linux/ioport.h>
66 #include <linux/mm.h>
67 #include <linux/hugetlb.h>
68 #include <linux/pagemap.h>
69 #include <linux/swap.h>
70 #include <linux/slab.h>
71 #include <linux/smp.h>
72 #include <linux/signal.h>
73 #include <linux/highmem.h>
74 #include <linux/file.h>
75 #include <linux/times.h>
76 #include <linux/cpuset.h>
77 #include <linux/vs_context.h>
78 #include <linux/vs_network.h>
79 #include <linux/vs_cvirt.h>
80
81 #include <asm/uaccess.h>
82 #include <asm/pgtable.h>
83 #include <asm/io.h>
84 #include <asm/processor.h>
85 #include "internal.h"
86
87 /* Gcc optimizes away "strlen(x)" for constant x */
88 #define ADDBUF(buffer, string) \
89 do { memcpy(buffer, string, strlen(string)); \
90      buffer += strlen(string); } while (0)
91
92 static inline char * task_name(struct task_struct *p, char * buf)
93 {
94         int i;
95         char * name;
96         char tcomm[sizeof(p->comm)];
97
98         get_task_comm(tcomm, p);
99
100         ADDBUF(buf, "Name:\t");
101         name = tcomm;
102         i = sizeof(tcomm);
103         do {
104                 unsigned char c = *name;
105                 name++;
106                 i--;
107                 *buf = c;
108                 if (!c)
109                         break;
110                 if (c == '\\') {
111                         buf[1] = c;
112                         buf += 2;
113                         continue;
114                 }
115                 if (c == '\n') {
116                         buf[0] = '\\';
117                         buf[1] = 'n';
118                         buf += 2;
119                         continue;
120                 }
121                 buf++;
122         } while (i);
123         *buf = '\n';
124         return buf+1;
125 }
126
127 /*
128  * The task state array is a strange "bitmap" of
129  * reasons to sleep. Thus "running" is zero, and
130  * you can test for combinations of others with
131  * simple bit tests.
132  */
133 static const char *task_state_array[] = {
134         "R (running)",          /*  0 */
135         "S (sleeping)",         /*  1 */
136         "D (disk sleep)",       /*  2 */
137         "T (stopped)",          /*  4 */
138         "T (tracing stop)",     /*  8 */
139         "Z (zombie)",           /* 16 */
140         "X (dead)",             /* 32 */
141         "H (on hold)"           /* 64 */
142 };
143
144 static inline const char * get_task_state(struct task_struct *tsk)
145 {
146         unsigned int state = (tsk->state & (TASK_RUNNING |
147                                             TASK_INTERRUPTIBLE |
148                                             TASK_UNINTERRUPTIBLE |
149                                             TASK_STOPPED |
150                                            TASK_TRACED |
151                                            TASK_ONHOLD)) |
152                         (tsk->exit_state & (EXIT_ZOMBIE |
153                                             EXIT_DEAD));
154         const char **p = &task_state_array[0];
155
156         while (state) {
157                 p++;
158                 state >>= 1;
159         }
160         return *p;
161 }
162
163 static inline char * task_state(struct task_struct *p, char *buffer)
164 {
165         struct group_info *group_info;
166         int g;
167         pid_t pid, ptgid, tppid, tgid;
168
169         read_lock(&tasklist_lock);
170         tgid = vx_map_tgid(p->tgid);
171         pid = vx_map_pid(p->pid);
172         ptgid = vx_map_pid(p->group_leader->real_parent->tgid);
173         tppid = vx_map_pid(p->parent->pid);
174         buffer += sprintf(buffer,
175                 "State:\t%s\n"
176                 "SleepAVG:\t%lu%%\n"
177                 "Tgid:\t%d\n"
178                 "Pid:\t%d\n"
179                 "PPid:\t%d\n"
180                 "TracerPid:\t%d\n"
181                 "Uid:\t%d\t%d\t%d\t%d\n"
182                 "Gid:\t%d\t%d\t%d\t%d\n",
183                 get_task_state(p),
184                 (p->sleep_avg/1024)*100/(1020000000/1024),
185                 tgid, pid, (pid > 1) ? ptgid : 0,
186                 pid_alive(p) && p->ptrace ? tppid : 0,
187                 p->uid, p->euid, p->suid, p->fsuid,
188                 p->gid, p->egid, p->sgid, p->fsgid);
189         read_unlock(&tasklist_lock);
190         task_lock(p);
191         buffer += sprintf(buffer,
192                 "FDSize:\t%d\n"
193                 "Groups:\t",
194                 p->files ? p->files->max_fds : 0);
195
196         group_info = p->group_info;
197         get_group_info(group_info);
198         task_unlock(p);
199
200         for (g = 0; g < min(group_info->ngroups,NGROUPS_SMALL); g++)
201                 buffer += sprintf(buffer, "%d ", GROUP_AT(group_info,g));
202         put_group_info(group_info);
203
204         buffer += sprintf(buffer, "\n");
205         return buffer;
206 }
207
208 static char * render_sigset_t(const char *header, sigset_t *set, char *buffer)
209 {
210         int i, len;
211
212         len = strlen(header);
213         memcpy(buffer, header, len);
214         buffer += len;
215
216         i = _NSIG;
217         do {
218                 int x = 0;
219
220                 i -= 4;
221                 if (sigismember(set, i+1)) x |= 1;
222                 if (sigismember(set, i+2)) x |= 2;
223                 if (sigismember(set, i+3)) x |= 4;
224                 if (sigismember(set, i+4)) x |= 8;
225                 *buffer++ = (x < 10 ? '0' : 'a' - 10) + x;
226         } while (i >= 4);
227
228         *buffer++ = '\n';
229         *buffer = 0;
230         return buffer;
231 }
232
233 static void collect_sigign_sigcatch(struct task_struct *p, sigset_t *ign,
234                                     sigset_t *catch)
235 {
236         struct k_sigaction *k;
237         int i;
238
239         k = p->sighand->action;
240         for (i = 1; i <= _NSIG; ++i, ++k) {
241                 if (k->sa.sa_handler == SIG_IGN)
242                         sigaddset(ign, i);
243                 else if (k->sa.sa_handler != SIG_DFL)
244                         sigaddset(catch, i);
245         }
246 }
247
248 static inline char * task_sig(struct task_struct *p, char *buffer)
249 {
250         sigset_t pending, shpending, blocked, ignored, caught;
251         int num_threads = 0;
252         unsigned long qsize = 0;
253         unsigned long qlim = 0;
254
255         sigemptyset(&pending);
256         sigemptyset(&shpending);
257         sigemptyset(&blocked);
258         sigemptyset(&ignored);
259         sigemptyset(&caught);
260
261         /* Gather all the data with the appropriate locks held */
262         read_lock(&tasklist_lock);
263         if (p->sighand) {
264                 spin_lock_irq(&p->sighand->siglock);
265                 pending = p->pending.signal;
266                 shpending = p->signal->shared_pending.signal;
267                 blocked = p->blocked;
268                 collect_sigign_sigcatch(p, &ignored, &caught);
269                 num_threads = atomic_read(&p->signal->count);
270                 qsize = atomic_read(&p->user->sigpending);
271                 qlim = p->signal->rlim[RLIMIT_SIGPENDING].rlim_cur;
272                 spin_unlock_irq(&p->sighand->siglock);
273         }
274         read_unlock(&tasklist_lock);
275
276         buffer += sprintf(buffer, "Threads:\t%d\n", num_threads);
277         buffer += sprintf(buffer, "SigQ:\t%lu/%lu\n", qsize, qlim);
278
279         /* render them all */
280         buffer = render_sigset_t("SigPnd:\t", &pending, buffer);
281         buffer = render_sigset_t("ShdPnd:\t", &shpending, buffer);
282         buffer = render_sigset_t("SigBlk:\t", &blocked, buffer);
283         buffer = render_sigset_t("SigIgn:\t", &ignored, buffer);
284         buffer = render_sigset_t("SigCgt:\t", &caught, buffer);
285
286         return buffer;
287 }
288
289 static inline char *task_cap(struct task_struct *p, char *buffer)
290 {
291     return buffer + sprintf(buffer, "CapInh:\t%016x\n"
292                             "CapPrm:\t%016x\n"
293                             "CapEff:\t%016x\n",
294                             cap_t(p->cap_inheritable),
295                             cap_t(p->cap_permitted),
296                             cap_t(p->cap_effective));
297 }
298
299 int proc_pid_status(struct task_struct *task, char * buffer)
300 {
301         char * orig = buffer;
302 #ifdef  CONFIG_VSERVER_LEGACY
303         struct vx_info *vxi;
304 #endif
305 #ifdef  CONFIG_VSERVER_LEGACYNET
306         struct nx_info *nxi;
307 #endif
308         struct mm_struct *mm = get_task_mm(task);
309
310         buffer = task_name(task, buffer);
311         buffer = task_state(task, buffer);
312  
313         if (mm) {
314                 buffer = task_mem(mm, buffer);
315                 mmput(mm);
316         }
317         buffer = task_sig(task, buffer);
318         buffer = task_cap(task, buffer);
319         buffer = cpuset_task_status_allowed(task, buffer);
320
321         if (task_vx_flags(task, VXF_INFO_HIDE, 0))
322                 goto skip;
323 #ifdef  CONFIG_VSERVER_LEGACY
324         buffer += sprintf (buffer,"s_context: %d\n", vx_task_xid(task));
325         vxi = task_get_vx_info(task);
326         if (vxi) {
327                 buffer += sprintf (buffer,"ctxflags: %08llx\n"
328                         ,(unsigned long long)vxi->vx_flags);
329                 buffer += sprintf (buffer,"initpid: %d\n"
330                         ,vxi->vx_initpid);
331         } else {
332                 buffer += sprintf (buffer,"ctxflags: none\n");
333                 buffer += sprintf (buffer,"initpid: none\n");
334         }
335         put_vx_info(vxi);
336 #else
337         buffer += sprintf (buffer,"VxID: %d\n", vx_task_xid(task));
338 #endif
339 #ifdef  CONFIG_VSERVER_LEGACYNET
340         nxi = task_get_nx_info(task);
341         if (nxi) {
342                 int i;
343
344                 buffer += sprintf (buffer,"ipv4root:");
345                 for (i=0; i<nxi->nbipv4; i++){
346                         buffer += sprintf (buffer," %08x/%08x"
347                                 ,nxi->ipv4[i]
348                                 ,nxi->mask[i]);
349                 }
350                 *buffer++ = '\n';
351                 buffer += sprintf (buffer,"ipv4root_bcast: %08x\n"
352                         ,nxi->v4_bcast);
353         } else {
354                 buffer += sprintf (buffer,"ipv4root: 0\n");
355                 buffer += sprintf (buffer,"ipv4root_bcast: 0\n");
356         }
357         put_nx_info(nxi);
358 #endif
359 skip:
360 #if defined(CONFIG_ARCH_S390)
361         buffer = task_show_regs(task, buffer);
362 #endif
363         return buffer - orig;
364 }
365
366 static int do_task_stat(struct task_struct *task, char * buffer, int whole)
367 {
368         unsigned long vsize, eip, esp, wchan = ~0UL;
369         long priority, nice;
370         int tty_pgrp = -1, tty_nr = 0;
371         sigset_t sigign, sigcatch;
372         char state;
373         int res;
374         pid_t pid, ppid, pgid = -1, sid = -1;
375         int num_threads = 0;
376         struct mm_struct *mm;
377         unsigned long long start_time;
378         unsigned long cmin_flt = 0, cmaj_flt = 0;
379         unsigned long  min_flt = 0,  maj_flt = 0;
380         cputime_t cutime, cstime, utime, stime;
381         unsigned long rsslim = 0;
382         unsigned long it_real_value = 0;
383         struct task_struct *t;
384         char tcomm[sizeof(task->comm)];
385
386         state = *get_task_state(task);
387         vsize = eip = esp = 0;
388         mm = get_task_mm(task);
389         if (mm) {
390                 vsize = task_vsize(mm);
391                 eip = KSTK_EIP(task);
392                 esp = KSTK_ESP(task);
393         }
394
395         get_task_comm(tcomm, task);
396
397         sigemptyset(&sigign);
398         sigemptyset(&sigcatch);
399         cutime = cstime = utime = stime = cputime_zero;
400         read_lock(&tasklist_lock);
401         if (task->sighand) {
402                 spin_lock_irq(&task->sighand->siglock);
403                 num_threads = atomic_read(&task->signal->count);
404                 collect_sigign_sigcatch(task, &sigign, &sigcatch);
405
406                 /* add up live thread stats at the group level */
407                 if (whole) {
408                         t = task;
409                         do {
410                                 min_flt += t->min_flt;
411                                 maj_flt += t->maj_flt;
412                                 utime = cputime_add(utime, t->utime);
413                                 stime = cputime_add(stime, t->stime);
414                                 t = next_thread(t);
415                         } while (t != task);
416                 }
417
418                 spin_unlock_irq(&task->sighand->siglock);
419         }
420         if (task->signal) {
421                 if (task->signal->tty) {
422                         tty_pgrp = task->signal->tty->pgrp;
423                         tty_nr = new_encode_dev(tty_devnum(task->signal->tty));
424                 }
425                 pgid = process_group(task);
426                 sid = task->signal->session;
427                 cmin_flt = task->signal->cmin_flt;
428                 cmaj_flt = task->signal->cmaj_flt;
429                 cutime = task->signal->cutime;
430                 cstime = task->signal->cstime;
431                 rsslim = task->signal->rlim[RLIMIT_RSS].rlim_cur;
432                 if (whole) {
433                         min_flt += task->signal->min_flt;
434                         maj_flt += task->signal->maj_flt;
435                         utime = cputime_add(utime, task->signal->utime);
436                         stime = cputime_add(stime, task->signal->stime);
437                 }
438                 it_real_value = task->signal->it_real_value;
439         }
440         pid = vx_info_map_pid(task->vx_info, pid_alive(task) ? task->pid : 0);
441         ppid = (!(pid > 1)) ? 0 : vx_info_map_tgid(task->vx_info,
442                 task->group_leader->real_parent->tgid);
443         pgid = vx_info_map_pid(task->vx_info, pgid);
444
445         read_unlock(&tasklist_lock);
446
447         if (!whole || num_threads<2)
448                 wchan = get_wchan(task);
449         if (!whole) {
450                 min_flt = task->min_flt;
451                 maj_flt = task->maj_flt;
452                 utime = task->utime;
453                 stime = task->stime;
454         }
455
456         /* scale priority and nice values from timeslices to -20..20 */
457         /* to make it look like a "normal" Unix priority/nice value  */
458         priority = task_prio(task);
459         nice = task_nice(task);
460
461         /* Temporary variable needed for gcc-2.96 */
462         /* convert timespec -> nsec*/
463         start_time = (unsigned long long)task->start_time.tv_sec * NSEC_PER_SEC
464                                 + task->start_time.tv_nsec;
465
466         /* convert nsec -> ticks */
467         start_time = nsec_to_clock_t(start_time);
468
469         /* fixup start time for virt uptime */
470         if (vx_flags(VXF_VIRT_UPTIME, 0)) {
471                 unsigned long long bias =
472                         current->vx_info->cvirt.bias_clock;
473
474                 if (start_time > bias)
475                         start_time -= bias;
476                 else
477                         start_time = 0;
478         }
479
480         res = sprintf(buffer,"%d (%s) %c %d %d %d %d %d %lu %lu \
481 %lu %lu %lu %lu %lu %ld %ld %ld %ld %d %ld %llu %lu %ld %lu %lu %lu %lu %lu \
482 %lu %lu %lu %lu %lu %lu %lu %lu %d %d %lu %lu\n",
483                 pid,
484                 tcomm,
485                 state,
486                 ppid,
487                 pgid,
488                 sid,
489                 tty_nr,
490                 tty_pgrp,
491                 task->flags,
492                 min_flt,
493                 cmin_flt,
494                 maj_flt,
495                 cmaj_flt,
496                 cputime_to_clock_t(utime),
497                 cputime_to_clock_t(stime),
498                 cputime_to_clock_t(cutime),
499                 cputime_to_clock_t(cstime),
500                 priority,
501                 nice,
502                 num_threads,
503                 jiffies_to_clock_t(it_real_value),
504                 start_time,
505                 vsize,
506                 mm ? get_mm_counter(mm, rss) : 0, /* you might want to shift this left 3 */
507                 rsslim,
508                 mm ? mm->start_code : 0,
509                 mm ? mm->end_code : 0,
510                 mm ? mm->start_stack : 0,
511                 esp,
512                 eip,
513                 /* The signal information here is obsolete.
514                  * It must be decimal for Linux 2.0 compatibility.
515                  * Use /proc/#/status for real-time signals.
516                  */
517                 task->pending.signal.sig[0] & 0x7fffffffUL,
518                 task->blocked.sig[0] & 0x7fffffffUL,
519                 sigign      .sig[0] & 0x7fffffffUL,
520                 sigcatch    .sig[0] & 0x7fffffffUL,
521                 wchan,
522                 0UL,
523                 0UL,
524                 task->exit_signal,
525                 task_cpu(task),
526                 task->rt_priority,
527                 task->policy);
528         if(mm)
529                 mmput(mm);
530         return res;
531 }
532
533 int proc_tid_stat(struct task_struct *task, char * buffer)
534 {
535         return do_task_stat(task, buffer, 0);
536 }
537
538 int proc_tgid_stat(struct task_struct *task, char * buffer)
539 {
540         return do_task_stat(task, buffer, 1);
541 }
542
543 int proc_pid_statm(struct task_struct *task, char *buffer)
544 {
545         int size = 0, resident = 0, shared = 0, text = 0, lib = 0, data = 0;
546         struct mm_struct *mm = get_task_mm(task);
547         
548         if (mm) {
549                 size = task_statm(mm, &shared, &text, &data, &resident);
550                 mmput(mm);
551         }
552
553         return sprintf(buffer,"%d %d %d %d %d %d %d\n",
554                        size, resident, shared, text, lib, data, 0);
555 }