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