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

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