root/fs/proc/array.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. get_loadavg
  2. get_uptime
  3. get_meminfo
  4. get_version
  5. get_task
  6. get_phys_addr
  7. get_array
  8. get_env
  9. get_arg
  10. get_wchan
  11. get_stat
  12. get_statm
  13. array_read

   1 /*
   2  *  linux/fs/proc/array.c
   3  *
   4  *  Copyright (C) 1992  by Linus Torvalds
   5  *  based on ideas by Darren Senn
   6  *
   7  *  stat,statm extensions by Michael K. Johnson, johnsonm@stolaf.edu
   8  */
   9 
  10 #include <linux/types.h>
  11 #include <linux/errno.h>
  12 #include <linux/sched.h>
  13 #include <linux/kernel.h>
  14 #include <linux/tty.h>
  15 
  16 #include <asm/segment.h>
  17 #include <asm/io.h>
  18 
  19 #define LOAD_INT(x) ((x) >> FSHIFT)
  20 #define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)
  21 
  22 #define KSTK_EIP(stack) (((unsigned long *)stack)[1019])
  23 #define KSTK_ESP(stack) (((unsigned long *)stack)[1022])
  24 
  25 #define _SSIZE(stack)   (TASK_SIZE - KSTK_ESP(stack))
  26 #define SSIZE(stack)    (KSTK_ESP(stack) ? _SSIZE(stack) : 0)
  27 
  28 #define VSIZE(task,stack) ((task)->brk + 1023 + SSIZE(stack))
  29 
  30 
  31 static int get_loadavg(char * buffer)
     /* [previous][next][first][last][top][bottom][index][help] */
  32 {
  33         int a, b, c;
  34 
  35         a = avenrun[0] + (FIXED_1/200);
  36         b = avenrun[1] + (FIXED_1/200);
  37         c = avenrun[2] + (FIXED_1/200);
  38         return sprintf(buffer,"%d.%02d %d.%02d %d.%02d\n",
  39                 LOAD_INT(a), LOAD_FRAC(a),
  40                 LOAD_INT(b), LOAD_FRAC(b),
  41                 LOAD_INT(c), LOAD_FRAC(c));
  42 }
  43 
  44 static int get_uptime(char * buffer)
     /* [previous][next][first][last][top][bottom][index][help] */
  45 {
  46         unsigned long uptime;
  47         unsigned long idle;
  48 
  49         uptime = jiffies + jiffies_offset;
  50         idle = task[0]->utime + task[0]->stime;
  51         return sprintf(buffer,"%d.%02d %d.%02d\n",
  52                 uptime / HZ,
  53                 uptime % HZ,
  54                 idle / HZ,
  55                 idle % HZ);
  56 }
  57 
  58 static int get_meminfo(char * buffer)
     /* [previous][next][first][last][top][bottom][index][help] */
  59 {
  60         struct sysinfo i;
  61 
  62         si_meminfo(&i);
  63         si_swapinfo(&i);
  64         return sprintf(buffer, "        total:   used:    free:   shared:  buffers:\n"
  65                 "Mem:  %8d %8d %8d %8d %8d\n"
  66                 "Swap: %8d %8d %8d\n",
  67                 i.totalram, i.totalram-i.freeram, i.freeram, i.sharedram, i.bufferram,
  68                 i.totalswap, i.totalswap-i.freeswap, i.freeswap);
  69 }
  70 
  71 static int get_version(char * buffer)
     /* [previous][next][first][last][top][bottom][index][help] */
  72 {
  73         extern char *linux_banner;
  74 
  75         strcpy(buffer, linux_banner);
  76         return strlen(buffer);
  77 }
  78 
  79 static struct task_struct ** get_task(pid_t pid)
     /* [previous][next][first][last][top][bottom][index][help] */
  80 {
  81         struct task_struct ** p;
  82 
  83         p = task;
  84         while (++p < task+NR_TASKS) {
  85                 if (*p && (*p)->pid == pid)
  86                         return p;
  87         }
  88         return NULL;
  89 }
  90 
  91 static unsigned long get_phys_addr(struct task_struct ** p, unsigned long ptr)
     /* [previous][next][first][last][top][bottom][index][help] */
  92 {
  93         unsigned long page;
  94 
  95         if (!p || !*p || ptr >= TASK_SIZE)
  96                 return 0;
  97         page = (*p)->tss.cr3;
  98         page += (ptr >> 20) & 0xffc;
  99         page = *(unsigned long *) page;
 100         if (!(page & 1))
 101                 return 0;
 102         page &= 0xfffff000;
 103         page += (ptr >> 10) & 0xffc;
 104         page = *(unsigned long *) page;
 105         if (!(page & 1))
 106                 return 0;
 107         page &= 0xfffff000;
 108         page += ptr & 0xfff;
 109         return page;
 110 }
 111 
 112 static int get_array(struct task_struct ** p, unsigned long start, unsigned long end, char * buffer)
     /* [previous][next][first][last][top][bottom][index][help] */
 113 {
 114         unsigned long addr;
 115         int size = 0, result = 0;
 116         char c;
 117 
 118         if (start >= end)
 119                 return result;
 120         for (;;) {
 121                 addr = get_phys_addr(p, start);
 122                 if (!addr)
 123                         return result;
 124                 do {
 125                         c = *(char *) addr;
 126                         if (!c)
 127                                 result = size;
 128                         if (size < PAGE_SIZE)
 129                                 buffer[size++] = c;
 130                         else
 131                                 return result;
 132                         addr++;
 133                         start++;
 134                         if (start >= end)
 135                                 return result;
 136                 } while (!(addr & 0xfff));
 137         }
 138 }
 139 
 140 static int get_env(int pid, char * buffer)
     /* [previous][next][first][last][top][bottom][index][help] */
 141 {
 142         struct task_struct ** p = get_task(pid);
 143 
 144         if (!p || !*p)
 145                 return 0;
 146         return get_array(p, (*p)->env_start, (*p)->env_end, buffer);
 147 }
 148 
 149 static int get_arg(int pid, char * buffer)
     /* [previous][next][first][last][top][bottom][index][help] */
 150 {
 151         struct task_struct ** p = get_task(pid);
 152 
 153         if (!p || !*p)
 154                 return 0;
 155         return get_array(p, (*p)->arg_start, (*p)->arg_end, buffer);
 156 }
 157 
 158 static unsigned long get_wchan(struct task_struct *p)
     /* [previous][next][first][last][top][bottom][index][help] */
 159 {
 160         unsigned long ebp, eip;
 161         unsigned long stack_page;
 162         int count = 0;
 163 
 164         if (!p || p == current || p->state == TASK_RUNNING)
 165                 return 0;
 166         ebp = p->tss.ebp;
 167         stack_page = p->kernel_stack_page;
 168         do {
 169                 if (ebp < stack_page || ebp >= 4092+stack_page)
 170                         return 0;
 171                 eip = *(unsigned long *) (ebp+4);
 172                 if ((void *)eip != sleep_on &&
 173                     (void *)eip != interruptible_sleep_on)
 174                         return eip;
 175                 ebp = *(unsigned long *) ebp;
 176         } while (count++ < 16);
 177         return 0;
 178 }
 179 
 180 static int get_stat(int pid, char * buffer)
     /* [previous][next][first][last][top][bottom][index][help] */
 181 {
 182         struct task_struct ** p = get_task(pid);
 183         unsigned long sigignore=0, sigcatch=0, bit=1, wchan;
 184         int i,tty_pgrp;
 185         char state;
 186 
 187         if (!p || !*p)
 188                 return 0;
 189         if ((*p)->state < 0 || (*p)->state > 5)
 190                 state = '.';
 191         else
 192                 state = "RSDZTD"[(*p)->state];
 193         wchan = get_wchan(*p);
 194         for(i=0; i<32; ++i) {
 195                 switch((int) (*p)->sigaction[i].sa_handler) {
 196                 case 1: sigignore |= bit; break;
 197                 case 0: break;
 198                 default: sigcatch |= bit;
 199                 } bit <<= 1;
 200         }
 201         tty_pgrp = (*p)->tty;
 202         if (tty_pgrp > 0 && tty_table[tty_pgrp])
 203                 tty_pgrp = tty_table[tty_pgrp]->pgrp;
 204         else
 205                 tty_pgrp = -1;
 206         return sprintf(buffer,"%d (%s) %c %d %d %d %d %d %u %u \
 207 %u %u %u %d %d %d %d %d %d %u %u %d %u %u %u %u %u %u %u %u %d \
 208 %d %d %d %u\n",
 209                 pid,
 210                 (*p)->comm,
 211                 state,
 212                 (*p)->p_pptr->pid,
 213                 (*p)->pgrp,
 214                 (*p)->session,
 215                 (*p)->tty,
 216                 tty_pgrp,
 217                 (*p)->flags,
 218                 (*p)->min_flt,
 219                 (*p)->cmin_flt,
 220                 (*p)->maj_flt,
 221                 (*p)->cmaj_flt,
 222                 (*p)->utime,
 223                 (*p)->stime,
 224                 (*p)->cutime,
 225                 (*p)->cstime,
 226                 (*p)->counter,  /* this is the kernel priority ---
 227                                    subtract 30 in your user-level program. */
 228                 (*p)->priority, /* this is the nice value ---
 229                                    subtract 15 in your user-level program. */
 230                 (*p)->timeout,
 231                 (*p)->it_real_value,
 232                 (*p)->start_time,
 233                 VSIZE((*p),(*p)->kernel_stack_page),
 234                 (*p)->rss, /* you might want to shift this left 3 */
 235                 (*p)->rlim[RLIMIT_RSS].rlim_cur,
 236                 (*p)->start_code,
 237                 (*p)->end_code,
 238                 (*p)->start_stack,
 239                 KSTK_ESP((*p)->kernel_stack_page),
 240                 KSTK_EIP((*p)->kernel_stack_page),
 241                 (*p)->signal,
 242                 (*p)->blocked,
 243                 sigignore,
 244                 sigcatch,
 245                 wchan);
 246 }
 247 
 248 static int get_statm(int pid, char * buffer)
     /* [previous][next][first][last][top][bottom][index][help] */
 249 {
 250         struct task_struct ** p = get_task(pid);
 251         int i, tpag;
 252         int size=0, resident=0, share=0, trs=0, lrs=0, drs=0, dt=0;
 253         unsigned long ptbl, *buf, *pte, *pagedir, map_nr;
 254 
 255         if (!p || !*p)
 256                 return 0;
 257         tpag = (*p)->end_code / PAGE_SIZE;
 258         if ((*p)->state != TASK_ZOMBIE) {
 259           pagedir = (void *)((*p)->tss.cr3 + ((*p)->start_code >> 20));
 260           for (i = 0; i < 0x300; ++i) {
 261             if ((ptbl = pagedir[i]) == 0) {
 262               tpag -= 1024;
 263               continue;
 264             }
 265             buf = (void *)(ptbl & 0xfffff000);
 266             for (pte = buf; pte < (buf + 1024); ++pte) {
 267               if (*pte != 0) {
 268                 ++size;
 269                 if (*pte & 1) {
 270                   ++resident;
 271                   if (tpag > 0)
 272                     ++trs;
 273                   else
 274                     ++drs;
 275                   if (i >= 15 && i < 0x2f0) {
 276                     ++lrs;
 277                     if (*pte & 0x40)
 278                       ++dt;
 279                     else
 280                       --drs;
 281                   }
 282                   map_nr = MAP_NR(*pte);
 283                   if (map_nr < (high_memory / 4096) && mem_map[map_nr] > 1)
 284                     ++share;
 285                 }
 286               }
 287               --tpag;
 288             }
 289           }
 290         }
 291         return sprintf(buffer,"%d %d %d %d %d %d %d\n",
 292                        size, resident, share, trs, lrs, drs, dt);
 293 }
 294 
 295 static int array_read(struct inode * inode, struct file * file,char * buf, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
 296 {
 297         char * page;
 298         int length;
 299         int end;
 300         unsigned int type, pid;
 301 
 302         if (count < 0)
 303                 return -EINVAL;
 304         page = (char *) get_free_page(GFP_KERNEL);
 305         if (!page)
 306                 return -ENOMEM;
 307         type = inode->i_ino;
 308         pid = type >> 16;
 309         type &= 0x0000ffff;
 310         switch (type) {
 311                 case 2:
 312                         length = get_loadavg(page);
 313                         break;
 314                 case 3:
 315                         length = get_uptime(page);
 316                         break;
 317                 case 4:
 318                         length = get_meminfo(page);
 319                         break;
 320                 case 6:
 321                         length = get_version(page);
 322                         break;
 323                 case 9:
 324                         length = get_env(pid, page);
 325                         break;
 326                 case 10:
 327                         length = get_arg(pid, page);
 328                         break;
 329                 case 11:
 330                         length = get_stat(pid, page);
 331                         break;
 332                 case 12:
 333                         length = get_statm(pid, page);
 334                         break;
 335                 default:
 336                         free_page((unsigned long) page);
 337                         return -EBADF;
 338         }
 339         if (file->f_pos >= length) {
 340                 free_page((unsigned long) page);
 341                 return 0;
 342         }
 343         if (count + file->f_pos > length)
 344                 count = length - file->f_pos;
 345         end = count + file->f_pos;
 346         memcpy_tofs(buf, page + file->f_pos, count);
 347         free_page((unsigned long) page);
 348         file->f_pos = end;
 349         return count;
 350 }
 351 
 352 static struct file_operations proc_array_operations = {
 353         NULL,           /* array_lseek */
 354         array_read,
 355         NULL,           /* array_write */
 356         NULL,           /* array_readdir */
 357         NULL,           /* array_select */
 358         NULL,           /* array_ioctl */
 359         NULL,           /* mmap */
 360         NULL,           /* no special open code */
 361         NULL,           /* no special release code */
 362         NULL            /* can't fsync */
 363 };
 364 
 365 struct inode_operations proc_array_inode_operations = {
 366         &proc_array_operations, /* default base directory file-ops */
 367         NULL,                   /* create */
 368         NULL,                   /* lookup */
 369         NULL,                   /* link */
 370         NULL,                   /* unlink */
 371         NULL,                   /* symlink */
 372         NULL,                   /* mkdir */
 373         NULL,                   /* rmdir */
 374         NULL,                   /* mknod */
 375         NULL,                   /* rename */
 376         NULL,                   /* readlink */
 377         NULL,                   /* follow_link */
 378         NULL,                   /* bmap */
 379         NULL,                   /* truncate */
 380         NULL                    /* permission */
 381 };

/* [previous][next][first][last][top][bottom][index][help] */