Merge to Fedora kernel-2.6.18-1.2255_FC5-vs2.0.2.2-rc9 patched with stable patch...
[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 #include <linux/vs_context.h>
79 #include <linux/vs_network.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         "N (noninteractive)",   /* 64 */
142         "H (on hold)"           /* 128 */
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 group_info *group_info;
167         int g;
168         struct fdtable *fdt = NULL;
169         pid_t pid, ptgid, tppid, tgid;
170
171         read_lock(&tasklist_lock);
172         tgid = vx_map_tgid(p->tgid);
173         pid = vx_map_pid(p->pid);
174         ptgid = vx_map_pid(p->group_leader->real_parent->tgid);
175         tppid = vx_map_pid(p->parent->pid);
176         buffer += sprintf(buffer,
177                 "State:\t%s\n"
178                 "SleepAVG:\t%lu%%\n"
179                 "Tgid:\t%d\n"
180                 "Pid:\t%d\n"
181                 "PPid:\t%d\n"
182                 "TracerPid:\t%d\n"
183                 "Uid:\t%d\t%d\t%d\t%d\n"
184                 "Gid:\t%d\t%d\t%d\t%d\n",
185                 get_task_state(p),
186                 (p->sleep_avg/1024)*100/(1020000000/1024),
187                 tgid, pid, (pid > 1) ? ptgid : 0,
188                 pid_alive(p) && p->ptrace ? tppid : 0,
189                 p->uid, p->euid, p->suid, p->fsuid,
190                 p->gid, p->egid, p->sgid, p->fsgid);
191         read_unlock(&tasklist_lock);
192         task_lock(p);
193         rcu_read_lock();
194         if (p->files)
195                 fdt = files_fdtable(p->files);
196         buffer += sprintf(buffer,
197                 "FDSize:\t%d\n"
198                 "Groups:\t",
199                 fdt ? fdt->max_fds : 0);
200         rcu_read_unlock();
201
202         group_info = p->group_info;
203         get_group_info(group_info);
204         task_unlock(p);
205
206         for (g = 0; g < min(group_info->ngroups,NGROUPS_SMALL); g++)
207                 buffer += sprintf(buffer, "%d ", GROUP_AT(group_info,g));
208         put_group_info(group_info);
209
210         buffer += sprintf(buffer, "\n");
211         return buffer;
212 }
213
214 static char * render_sigset_t(const char *header, sigset_t *set, char *buffer)
215 {
216         int i, len;
217
218         len = strlen(header);
219         memcpy(buffer, header, len);
220         buffer += len;
221
222         i = _NSIG;
223         do {
224                 int x = 0;
225
226                 i -= 4;
227                 if (sigismember(set, i+1)) x |= 1;
228                 if (sigismember(set, i+2)) x |= 2;
229                 if (sigismember(set, i+3)) x |= 4;
230                 if (sigismember(set, i+4)) x |= 8;
231                 *buffer++ = (x < 10 ? '0' : 'a' - 10) + x;
232         } while (i >= 4);
233
234         *buffer++ = '\n';
235         *buffer = 0;
236         return buffer;
237 }
238
239 static void collect_sigign_sigcatch(struct task_struct *p, sigset_t *ign,
240                                     sigset_t *catch)
241 {
242         struct k_sigaction *k;
243         int i;
244
245         k = p->sighand->action;
246         for (i = 1; i <= _NSIG; ++i, ++k) {
247                 if (k->sa.sa_handler == SIG_IGN)
248                         sigaddset(ign, i);
249                 else if (k->sa.sa_handler != SIG_DFL)
250                         sigaddset(catch, i);
251         }
252 }
253
254 static inline char * task_sig(struct task_struct *p, char *buffer)
255 {
256         sigset_t pending, shpending, blocked, ignored, caught;
257         int num_threads = 0;
258         unsigned long qsize = 0;
259         unsigned long qlim = 0;
260
261         sigemptyset(&pending);
262         sigemptyset(&shpending);
263         sigemptyset(&blocked);
264         sigemptyset(&ignored);
265         sigemptyset(&caught);
266
267         /* Gather all the data with the appropriate locks held */
268         read_lock(&tasklist_lock);
269         if (p->sighand) {
270                 spin_lock_irq(&p->sighand->siglock);
271                 pending = p->pending.signal;
272                 shpending = p->signal->shared_pending.signal;
273                 blocked = p->blocked;
274                 collect_sigign_sigcatch(p, &ignored, &caught);
275                 num_threads = atomic_read(&p->signal->count);
276                 qsize = atomic_read(&p->user->sigpending);
277                 qlim = p->signal->rlim[RLIMIT_SIGPENDING].rlim_cur;
278                 spin_unlock_irq(&p->sighand->siglock);
279         }
280         read_unlock(&tasklist_lock);
281
282         buffer += sprintf(buffer, "Threads:\t%d\n", num_threads);
283         buffer += sprintf(buffer, "SigQ:\t%lu/%lu\n", qsize, qlim);
284
285         /* render them all */
286         buffer = render_sigset_t("SigPnd:\t", &pending, buffer);
287         buffer = render_sigset_t("ShdPnd:\t", &shpending, buffer);
288         buffer = render_sigset_t("SigBlk:\t", &blocked, buffer);
289         buffer = render_sigset_t("SigIgn:\t", &ignored, buffer);
290         buffer = render_sigset_t("SigCgt:\t", &caught, buffer);
291
292         return buffer;
293 }
294
295 static inline char *task_cap(struct task_struct *p, char *buffer)
296 {
297     return buffer + sprintf(buffer, "CapInh:\t%016x\n"
298                             "CapPrm:\t%016x\n"
299                             "CapEff:\t%016x\n",
300                             cap_t(p->cap_inheritable),
301                             cap_t(p->cap_permitted),
302                             cap_t(p->cap_effective));
303 }
304
305 int proc_pid_status(struct task_struct *task, char * buffer)
306 {
307         char * orig = buffer;
308 #ifdef  CONFIG_VSERVER_LEGACY
309         struct vx_info *vxi;
310 #endif
311 #ifdef  CONFIG_VSERVER_LEGACYNET
312         struct nx_info *nxi;
313 #endif
314         struct mm_struct *mm = get_task_mm(task);
315
316         buffer = task_name(task, buffer);
317         buffer = task_state(task, buffer);
318  
319         if (mm) {
320                 buffer = task_mem(mm, buffer);
321                 mmput(mm);
322         }
323         buffer = task_sig(task, buffer);
324         buffer = task_cap(task, buffer);
325         buffer = cpuset_task_status_allowed(task, buffer);
326
327         if (task_vx_flags(task, VXF_INFO_HIDE, 0))
328                 goto skip;
329 #ifdef  CONFIG_VSERVER_LEGACY
330         buffer += sprintf (buffer,"s_context: %d\n", vx_task_xid(task));
331         vxi = task_get_vx_info(task);
332         if (vxi) {
333                 buffer += sprintf (buffer,"ctxflags: %08llx\n"
334                         ,(unsigned long long)vxi->vx_flags);
335                 buffer += sprintf (buffer,"initpid: %d\n"
336                         ,vxi->vx_initpid);
337         } else {
338                 buffer += sprintf (buffer,"ctxflags: none\n");
339                 buffer += sprintf (buffer,"initpid: none\n");
340         }
341         put_vx_info(vxi);
342 #else
343         buffer += sprintf (buffer,"VxID: %d\n", vx_task_xid(task));
344 #endif
345 #ifdef  CONFIG_VSERVER_LEGACYNET
346         nxi = task_get_nx_info(task);
347         if (nxi) {
348                 int i;
349
350                 buffer += sprintf (buffer,"ipv4root:");
351                 for (i=0; i<nxi->nbipv4; i++){
352                         buffer += sprintf (buffer," %08x/%08x"
353                                 ,nxi->ipv4[i]
354                                 ,nxi->mask[i]);
355                 }
356                 *buffer++ = '\n';
357                 buffer += sprintf (buffer,"ipv4root_bcast: %08x\n"
358                         ,nxi->v4_bcast);
359         } else {
360                 buffer += sprintf (buffer,"ipv4root: 0\n");
361                 buffer += sprintf (buffer,"ipv4root_bcast: 0\n");
362         }
363         put_nx_info(nxi);
364 #endif
365 skip:
366 #if defined(CONFIG_S390)
367         buffer = task_show_regs(task, buffer);
368 #endif
369         return buffer - orig;
370 }
371
372 static int do_task_stat(struct task_struct *task, char * buffer, int whole)
373 {
374         unsigned long vsize, eip, esp, wchan = ~0UL;
375         long priority, nice;
376         int tty_pgrp = -1, tty_nr = 0;
377         sigset_t sigign, sigcatch;
378         char state;
379         int res;
380         pid_t pid, ppid, pgid = -1, sid = -1;
381         int num_threads = 0;
382         struct mm_struct *mm;
383         unsigned long long start_time;
384         unsigned long cmin_flt = 0, cmaj_flt = 0;
385         unsigned long  min_flt = 0,  maj_flt = 0;
386         cputime_t cutime, cstime, utime, stime;
387         unsigned long rsslim = 0;
388         struct task_struct *t;
389         char tcomm[sizeof(task->comm)];
390
391         state = *get_task_state(task);
392         vsize = eip = esp = 0;
393         mm = get_task_mm(task);
394         if (mm) {
395                 vsize = task_vsize(mm);
396                 eip = KSTK_EIP(task);
397                 esp = KSTK_ESP(task);
398         }
399
400         get_task_comm(tcomm, task);
401
402         sigemptyset(&sigign);
403         sigemptyset(&sigcatch);
404         cutime = cstime = utime = stime = cputime_zero;
405
406         mutex_lock(&tty_mutex);
407         read_lock(&tasklist_lock);
408         if (task->sighand) {
409                 spin_lock_irq(&task->sighand->siglock);
410                 num_threads = atomic_read(&task->signal->count);
411                 collect_sigign_sigcatch(task, &sigign, &sigcatch);
412
413                 /* add up live thread stats at the group level */
414                 if (whole) {
415                         t = task;
416                         do {
417                                 min_flt += t->min_flt;
418                                 maj_flt += t->maj_flt;
419                                 utime = cputime_add(utime, t->utime);
420                                 stime = cputime_add(stime, t->stime);
421                                 t = next_thread(t);
422                         } while (t != task);
423                 }
424
425                 spin_unlock_irq(&task->sighand->siglock);
426         }
427         if (task->signal) {
428                 if (task->signal->tty) {
429                         tty_pgrp = task->signal->tty->pgrp;
430                         tty_nr = new_encode_dev(tty_devnum(task->signal->tty));
431                 }
432                 pgid = process_group(task);
433                 sid = task->signal->session;
434                 cmin_flt = task->signal->cmin_flt;
435                 cmaj_flt = task->signal->cmaj_flt;
436                 cutime = task->signal->cutime;
437                 cstime = task->signal->cstime;
438                 rsslim = task->signal->rlim[RLIMIT_RSS].rlim_cur;
439                 if (whole) {
440                         min_flt += task->signal->min_flt;
441                         maj_flt += task->signal->maj_flt;
442                         utime = cputime_add(utime, task->signal->utime);
443                         stime = cputime_add(stime, task->signal->stime);
444                 }
445         }
446         pid = vx_info_map_pid(task->vx_info, pid_alive(task) ? task->pid : 0);
447         ppid = (!(pid > 1)) ? 0 : vx_info_map_tgid(task->vx_info,
448                 task->group_leader->real_parent->tgid);
449         pgid = vx_info_map_pid(task->vx_info, pgid);
450
451         read_unlock(&tasklist_lock);
452         mutex_unlock(&tty_mutex);
453
454         if (!whole || num_threads<2) {
455                 wchan = 0;
456                 if (current->uid == task->uid || current->euid == task->uid ||
457                                 capable(CAP_SYS_NICE))
458                         wchan = get_wchan(task);
459         }
460         if (!whole) {
461                 min_flt = task->min_flt;
462                 maj_flt = task->maj_flt;
463                 utime = task->utime;
464                 stime = task->stime;
465         }
466
467         /* scale priority and nice values from timeslices to -20..20 */
468         /* to make it look like a "normal" Unix priority/nice value  */
469         priority = task_prio(task);
470         nice = task_nice(task);
471
472         /* Temporary variable needed for gcc-2.96 */
473         /* convert timespec -> nsec*/
474         start_time = (unsigned long long)task->start_time.tv_sec * NSEC_PER_SEC
475                                 + task->start_time.tv_nsec;
476         /* convert nsec -> ticks */
477         start_time = nsec_to_clock_t(start_time);
478
479         /* fixup start time for virt uptime */
480         if (vx_flags(VXF_VIRT_UPTIME, 0)) {
481                 unsigned long long bias =
482                         current->vx_info->cvirt.bias_clock;
483
484                 if (start_time > bias)
485                         start_time -= bias;
486                 else
487                         start_time = 0;
488         }
489
490         res = sprintf(buffer,"%d (%s) %c %d %d %d %d %d %lu %lu \
491 %lu %lu %lu %lu %lu %ld %ld %ld %ld %d 0 %llu %lu %ld %lu %lu %lu %lu %lu \
492 %lu %lu %lu %lu %lu %lu %lu %lu %d %d %lu %lu %llu\n",
493                 pid,
494                 tcomm,
495                 state,
496                 ppid,
497                 pgid,
498                 sid,
499                 tty_nr,
500                 tty_pgrp,
501                 task->flags,
502                 min_flt,
503                 cmin_flt,
504                 maj_flt,
505                 cmaj_flt,
506                 cputime_to_clock_t(utime),
507                 cputime_to_clock_t(stime),
508                 cputime_to_clock_t(cutime),
509                 cputime_to_clock_t(cstime),
510                 priority,
511                 nice,
512                 num_threads,
513                 start_time,
514                 vsize,
515                 mm ? get_mm_rss(mm) : 0,
516                 rsslim,
517                 mm ? mm->start_code : 0,
518                 mm ? mm->end_code : 0,
519                 mm ? mm->start_stack : 0,
520                 esp,
521                 eip,
522                 /* The signal information here is obsolete.
523                  * It must be decimal for Linux 2.0 compatibility.
524                  * Use /proc/#/status for real-time signals.
525                  */
526                 task->pending.signal.sig[0] & 0x7fffffffUL,
527                 task->blocked.sig[0] & 0x7fffffffUL,
528                 sigign      .sig[0] & 0x7fffffffUL,
529                 sigcatch    .sig[0] & 0x7fffffffUL,
530                 wchan,
531                 0UL,
532                 0UL,
533                 task->exit_signal,
534                 task_cpu(task),
535                 task->rt_priority,
536                 task->policy,
537                 (unsigned long long)delayacct_blkio_ticks(task));
538         if(mm)
539                 mmput(mm);
540         return res;
541 }
542
543 int proc_tid_stat(struct task_struct *task, char * buffer)
544 {
545         return do_task_stat(task, buffer, 0);
546 }
547
548 int proc_tgid_stat(struct task_struct *task, char * buffer)
549 {
550         return do_task_stat(task, buffer, 1);
551 }
552
553 int proc_pid_statm(struct task_struct *task, char *buffer)
554 {
555         int size = 0, resident = 0, shared = 0, text = 0, lib = 0, data = 0;
556         struct mm_struct *mm = get_task_mm(task);
557         
558         if (mm) {
559                 size = task_statm(mm, &shared, &text, &data, &resident);
560                 mmput(mm);
561         }
562
563         return sprintf(buffer,"%d %d %d %d %d %d %d\n",
564                        size, resident, shared, text, lib, data, 0);
565 }