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