Merge to Fedora kernel-2.6.18-1.2239_FC5 patched with stable patch-2.6.18.2-vs2.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         "Z (zombie)",           /* 16 */
141         "X (dead)",             /* 32 */
142         "N (noninteractive)",   /* 64 */
143         "H (on hold)"           /* 128 */
144 };
145
146 static inline const char * get_task_state(struct task_struct *tsk)
147 {
148         unsigned int state = (tsk->state & (TASK_RUNNING |
149                                             TASK_INTERRUPTIBLE |
150                                             TASK_UNINTERRUPTIBLE |
151                                             TASK_STOPPED |
152                                            TASK_TRACED |
153                                            TASK_ONHOLD)) |
154                         (tsk->exit_state & (EXIT_ZOMBIE |
155                                             EXIT_DEAD));
156         const char **p = &task_state_array[0];
157
158         while (state) {
159                 p++;
160                 state >>= 1;
161         }
162         return *p;
163 }
164
165 static inline char * task_state(struct task_struct *p, char *buffer)
166 {
167         struct task_struct *tracer;
168         pid_t tracer_pid, pid, ptgid, tgid;
169         struct group_info *group_info;
170         int g;
171         struct fdtable *fdt = NULL;
172
173         rcu_read_lock();
174         tracer = tracehook_tracer_task(p);
175         tracer_pid = tracer == NULL ? 0 : vx_map_pid(tracer->pid);
176         rcu_read_unlock();
177
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,
183                 "State:\t%s\n"
184                 "SleepAVG:\t%lu%%\n"
185                 "Tgid:\t%d\n"
186                 "Pid:\t%d\n"
187                 "PPid:\t%d\n"
188                 "TracerPid:\t%d\n"
189                 "Uid:\t%d\t%d\t%d\t%d\n"
190                 "Gid:\t%d\t%d\t%d\t%d\n",
191                 get_task_state(p),
192                 (p->sleep_avg/1024)*100/(1020000000/1024),
193                 tgid, pid, (pid > 1) ? ptgid : 0,
194                 tracer_pid,
195                 p->uid, p->euid, p->suid, p->fsuid,
196                 p->gid, p->egid, p->sgid, p->fsgid);
197         read_unlock(&tasklist_lock);
198         task_lock(p);
199         rcu_read_lock();
200         if (p->files)
201                 fdt = files_fdtable(p->files);
202         buffer += sprintf(buffer,
203                 "FDSize:\t%d\n"
204                 "Groups:\t",
205                 fdt ? fdt->max_fds : 0);
206         rcu_read_unlock();
207
208         group_info = p->group_info;
209         get_group_info(group_info);
210         task_unlock(p);
211
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);
215
216         buffer += sprintf(buffer, "\n");
217         return buffer;
218 }
219
220 static char * render_sigset_t(const char *header, sigset_t *set, char *buffer)
221 {
222         int i, len;
223
224         len = strlen(header);
225         memcpy(buffer, header, len);
226         buffer += len;
227
228         i = _NSIG;
229         do {
230                 int x = 0;
231
232                 i -= 4;
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;
238         } while (i >= 4);
239
240         *buffer++ = '\n';
241         *buffer = 0;
242         return buffer;
243 }
244
245 static void collect_sigign_sigcatch(struct task_struct *p, sigset_t *ign,
246                                     sigset_t *catch)
247 {
248         struct k_sigaction *k;
249         int i;
250
251         k = p->sighand->action;
252         for (i = 1; i <= _NSIG; ++i, ++k) {
253                 if (k->sa.sa_handler == SIG_IGN)
254                         sigaddset(ign, i);
255                 else if (k->sa.sa_handler != SIG_DFL)
256                         sigaddset(catch, i);
257         }
258 }
259
260 static inline char * task_sig(struct task_struct *p, char *buffer)
261 {
262         sigset_t pending, shpending, blocked, ignored, caught;
263         int num_threads = 0;
264         unsigned long qsize = 0;
265         unsigned long qlim = 0;
266
267         sigemptyset(&pending);
268         sigemptyset(&shpending);
269         sigemptyset(&blocked);
270         sigemptyset(&ignored);
271         sigemptyset(&caught);
272
273         /* Gather all the data with the appropriate locks held */
274         read_lock(&tasklist_lock);
275         if (p->sighand) {
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);
285         }
286         read_unlock(&tasklist_lock);
287
288         buffer += sprintf(buffer, "Threads:\t%d\n", num_threads);
289         buffer += sprintf(buffer, "SigQ:\t%lu/%lu\n", qsize, qlim);
290
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);
297
298         return buffer;
299 }
300
301 static inline char *task_cap(struct task_struct *p, char *buffer)
302 {
303     return buffer + sprintf(buffer, "CapInh:\t%016x\n"
304                             "CapPrm:\t%016x\n"
305                             "CapEff:\t%016x\n",
306                             cap_t(p->cap_inheritable),
307                             cap_t(p->cap_permitted),
308                             cap_t(p->cap_effective));
309 }
310
311 int proc_pid_status(struct task_struct *task, char * buffer)
312 {
313         char * orig = buffer;
314 #ifdef  CONFIG_VSERVER_LEGACY
315         struct vx_info *vxi;
316 #endif
317 #ifdef  CONFIG_VSERVER_LEGACYNET
318         struct nx_info *nxi;
319 #endif
320         struct mm_struct *mm = get_task_mm(task);
321
322         buffer = task_name(task, buffer);
323         buffer = task_state(task, buffer);
324  
325         if (mm) {
326                 buffer = task_mem(mm, buffer);
327                 mmput(mm);
328         }
329         buffer = task_sig(task, buffer);
330         buffer = task_cap(task, buffer);
331         buffer = cpuset_task_status_allowed(task, buffer);
332
333         if (task_vx_flags(task, VXF_INFO_HIDE, 0))
334                 goto skip;
335 #ifdef  CONFIG_VSERVER_LEGACY
336         buffer += sprintf (buffer,"s_context: %d\n", vx_task_xid(task));
337         vxi = task_get_vx_info(task);
338         if (vxi) {
339                 buffer += sprintf (buffer,"ctxflags: %08llx\n"
340                         ,(unsigned long long)vxi->vx_flags);
341                 buffer += sprintf (buffer,"initpid: %d\n"
342                         ,vxi->vx_initpid);
343         } else {
344                 buffer += sprintf (buffer,"ctxflags: none\n");
345                 buffer += sprintf (buffer,"initpid: none\n");
346         }
347         put_vx_info(vxi);
348 #else
349         buffer += sprintf (buffer,"VxID: %d\n", vx_task_xid(task));
350 #endif
351 #ifdef  CONFIG_VSERVER_LEGACYNET
352         nxi = task_get_nx_info(task);
353         if (nxi) {
354                 int i;
355
356                 buffer += sprintf (buffer,"ipv4root:");
357                 for (i=0; i<nxi->nbipv4; i++){
358                         buffer += sprintf (buffer," %08x/%08x"
359                                 ,nxi->ipv4[i]
360                                 ,nxi->mask[i]);
361                 }
362                 *buffer++ = '\n';
363                 buffer += sprintf (buffer,"ipv4root_bcast: %08x\n"
364                         ,nxi->v4_bcast);
365         } else {
366                 buffer += sprintf (buffer,"ipv4root: 0\n");
367                 buffer += sprintf (buffer,"ipv4root_bcast: 0\n");
368         }
369         put_nx_info(nxi);
370 #endif
371 skip:
372 #if defined(CONFIG_S390)
373         buffer = task_show_regs(task, buffer);
374 #endif
375         return buffer - orig;
376 }
377
378 static int do_task_stat(struct task_struct *task, char * buffer, int whole)
379 {
380         unsigned long vsize, eip, esp, wchan = ~0UL;
381         long priority, nice;
382         int tty_pgrp = -1, tty_nr = 0;
383         sigset_t sigign, sigcatch;
384         char state;
385         int res;
386         pid_t pid, ppid, pgid = -1, sid = -1;
387         int num_threads = 0;
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)];
396
397         state = *get_task_state(task);
398         vsize = eip = esp = 0;
399         mm = get_task_mm(task);
400         if (mm) {
401                 vsize = task_vsize(mm);
402                 eip = KSTK_EIP(task);
403                 esp = KSTK_ESP(task);
404         }
405
406         get_task_comm(tcomm, task);
407
408         sigemptyset(&sigign);
409         sigemptyset(&sigcatch);
410         cutime = cstime = utime = stime = cputime_zero;
411
412         mutex_lock(&tty_mutex);
413         read_lock(&tasklist_lock);
414         if (task->sighand) {
415                 spin_lock_irq(&task->sighand->siglock);
416                 num_threads = atomic_read(&task->signal->count);
417                 collect_sigign_sigcatch(task, &sigign, &sigcatch);
418
419                 /* add up live thread stats at the group level */
420                 if (whole) {
421                         t = task;
422                         do {
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);
427                                 t = next_thread(t);
428                         } while (t != task);
429                 }
430
431                 spin_unlock_irq(&task->sighand->siglock);
432         }
433         if (task->signal) {
434                 if (task->signal->tty) {
435                         tty_pgrp = task->signal->tty->pgrp;
436                         tty_nr = new_encode_dev(tty_devnum(task->signal->tty));
437                 }
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;
445                 if (whole) {
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);
450                 }
451         }
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);
456
457         read_unlock(&tasklist_lock);
458         mutex_unlock(&tty_mutex);
459
460         if (!whole || num_threads<2) {
461                 wchan = 0;
462                 if (current->uid == task->uid || current->euid == task->uid ||
463                                 capable(CAP_SYS_NICE))
464                         wchan = get_wchan(task);
465         }
466         if (!whole) {
467                 min_flt = task->min_flt;
468                 maj_flt = task->maj_flt;
469                 utime = task->utime;
470                 stime = task->stime;
471         }
472
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);
477
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);
484
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;
489
490                 if (start_time > bias)
491                         start_time -= bias;
492                 else
493                         start_time = 0;
494         }
495
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",
499                 pid,
500                 tcomm,
501                 state,
502                 ppid,
503                 pgid,
504                 sid,
505                 tty_nr,
506                 tty_pgrp,
507                 task->flags,
508                 min_flt,
509                 cmin_flt,
510                 maj_flt,
511                 cmaj_flt,
512                 cputime_to_clock_t(utime),
513                 cputime_to_clock_t(stime),
514                 cputime_to_clock_t(cutime),
515                 cputime_to_clock_t(cstime),
516                 priority,
517                 nice,
518                 num_threads,
519                 start_time,
520                 vsize,
521                 mm ? get_mm_rss(mm) : 0,
522                 rsslim,
523                 mm ? mm->start_code : 0,
524                 mm ? mm->end_code : 0,
525                 mm ? mm->start_stack : 0,
526                 esp,
527                 eip,
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.
531                  */
532                 task->pending.signal.sig[0] & 0x7fffffffUL,
533                 task->blocked.sig[0] & 0x7fffffffUL,
534                 sigign      .sig[0] & 0x7fffffffUL,
535                 sigcatch    .sig[0] & 0x7fffffffUL,
536                 wchan,
537                 0UL,
538                 0UL,
539                 task->exit_signal,
540                 task_cpu(task),
541                 task->rt_priority,
542                 task->policy,
543                 (unsigned long long)delayacct_blkio_ticks(task));
544         if(mm)
545                 mmput(mm);
546         return res;
547 }
548
549 int proc_tid_stat(struct task_struct *task, char * buffer)
550 {
551         return do_task_stat(task, buffer, 0);
552 }
553
554 int proc_tgid_stat(struct task_struct *task, char * buffer)
555 {
556         return do_task_stat(task, buffer, 1);
557 }
558
559 int proc_pid_statm(struct task_struct *task, char *buffer)
560 {
561         int size = 0, resident = 0, shared = 0, text = 0, lib = 0, data = 0;
562         struct mm_struct *mm = get_task_mm(task);
563         
564         if (mm) {
565                 size = task_statm(mm, &shared, &text, &data, &resident);
566                 mmput(mm);
567         }
568
569         return sprintf(buffer,"%d %d %d %d %d %d %d\n",
570                        size, resident, shared, text, lib, data, 0);
571 }