root/fs/proc/array.c

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

DEFINITIONS

This source file includes following definitions.
  1. read_core
  2. get_loadavg
  3. get_kstat
  4. get_uptime
  5. get_meminfo
  6. get_version
  7. get_task
  8. get_phys_addr
  9. get_array
  10. get_env
  11. get_arg
  12. get_wchan
  13. get_stat
  14. get_statm
  15. get_maps
  16. get_root_array
  17. get_process_array
  18. fill_array
  19. 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  * 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@calvin.iaf.nl>
  16  */
  17 
  18 #include <linux/types.h>
  19 #include <linux/errno.h>
  20 #include <linux/sched.h>
  21 #include <linux/kernel.h>
  22 #include <linux/kernel_stat.h>
  23 #include <linux/tty.h>
  24 #include <linux/user.h>
  25 #include <linux/a.out.h>
  26 #include <linux/string.h>
  27 #include <linux/mman.h>
  28 #include <linux/proc_fs.h>
  29 
  30 #include <asm/segment.h>
  31 #include <asm/io.h>
  32 
  33 #define LOAD_INT(x) ((x) >> FSHIFT)
  34 #define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)
  35 
  36 #ifdef CONFIG_DEBUG_MALLOC
  37 int get_malloc(char * buffer);
  38 #endif
  39 
  40 static int read_core(struct inode * inode, struct file * file,char * buf, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
  41 {
  42         unsigned long p = file->f_pos;
  43         int read;
  44         int count1;
  45         char * pnt;
  46         struct user dump;
  47 
  48         memset(&dump, 0, sizeof(struct user));
  49         dump.magic = CMAGIC;
  50         dump.u_dsize = high_memory >> 12;
  51 
  52         if (count < 0)
  53                 return -EINVAL;
  54         if (p >= high_memory + PAGE_SIZE)
  55                 return 0;
  56         if (count > high_memory + PAGE_SIZE - p)
  57                 count = high_memory + PAGE_SIZE - p;
  58         read = 0;
  59 
  60         if (p < sizeof(struct user) && count > 0) {
  61                 count1 = count;
  62                 if (p + count1 > sizeof(struct user))
  63                         count1 = sizeof(struct user)-p;
  64                 pnt = (char *) &dump + p;
  65                 memcpy_tofs(buf,(void *) pnt, count1);
  66                 buf += count1;
  67                 p += count1;
  68                 count -= count1;
  69                 read += count1;
  70         }
  71 
  72         while (p < 2*PAGE_SIZE && count > 0) {
  73                 put_fs_byte(0,buf);
  74                 buf++;
  75                 p++;
  76                 count--;
  77                 read++;
  78         }
  79         memcpy_tofs(buf,(void *) (p - PAGE_SIZE),count);
  80         read += count;
  81         file->f_pos += read;
  82         return read;
  83 }
  84 
  85 static struct file_operations proc_kcore_operations = {
  86         NULL,           /* lseek */
  87         read_core,
  88 };
  89 
  90 struct inode_operations proc_kcore_inode_operations = {
  91         &proc_kcore_operations, 
  92 };
  93 
  94 static int get_loadavg(char * buffer)
     /* [previous][next][first][last][top][bottom][index][help] */
  95 {
  96         int a, b, c;
  97 
  98         a = avenrun[0] + (FIXED_1/200);
  99         b = avenrun[1] + (FIXED_1/200);
 100         c = avenrun[2] + (FIXED_1/200);
 101         return sprintf(buffer,"%d.%02d %d.%02d %d.%02d\n",
 102                 LOAD_INT(a), LOAD_FRAC(a),
 103                 LOAD_INT(b), LOAD_FRAC(b),
 104                 LOAD_INT(c), LOAD_FRAC(c));
 105 }
 106 
 107 static int get_kstat(char * buffer)
     /* [previous][next][first][last][top][bottom][index][help] */
 108 {
 109         int i, len;
 110         unsigned sum = 0;
 111 
 112         for (i = 0 ; i < 16 ; i++)
 113                 sum += kstat.interrupts[i];
 114         len = sprintf(buffer,
 115                 "cpu  %u %u %u %lu\n"
 116                 "disk %u %u %u %u\n"
 117                 "page %u %u\n"
 118                 "swap %u %u\n"
 119                 "intr %u",
 120                 kstat.cpu_user,
 121                 kstat.cpu_nice,
 122                 kstat.cpu_system,
 123                 jiffies - (kstat.cpu_user + kstat.cpu_nice + kstat.cpu_system),
 124                 kstat.dk_drive[0],
 125                 kstat.dk_drive[1],
 126                 kstat.dk_drive[2],
 127                 kstat.dk_drive[3],
 128                 kstat.pgpgin,
 129                 kstat.pgpgout,
 130                 kstat.pswpin,
 131                 kstat.pswpout,
 132                 sum);
 133         for (i = 0 ; i < 16 ; i++)
 134                 len += sprintf(buffer + len, " %u", kstat.interrupts[i]);
 135         len += sprintf(buffer + len,
 136                 "\nctxt %u\n"
 137                 "btime %lu\n",
 138                 kstat.context_swtch,
 139                 xtime.tv_sec - jiffies / HZ);
 140         return len;
 141 }
 142 
 143 
 144 static int get_uptime(char * buffer)
     /* [previous][next][first][last][top][bottom][index][help] */
 145 {
 146         unsigned long uptime;
 147         unsigned long idle;
 148 
 149         uptime = jiffies;
 150         idle = task[0]->utime + task[0]->stime;
 151         return sprintf(buffer,"%lu.%02lu %lu.%02lu\n",
 152                 uptime / HZ,
 153                 uptime % HZ,
 154                 idle / HZ,
 155                 idle % HZ);
 156 }
 157 
 158 static int get_meminfo(char * buffer)
     /* [previous][next][first][last][top][bottom][index][help] */
 159 {
 160         struct sysinfo i;
 161 
 162         si_meminfo(&i);
 163         si_swapinfo(&i);
 164         return sprintf(buffer, "        total:   used:    free:   shared:  buffers:\n"
 165                 "Mem:  %8lu %8lu %8lu %8lu %8lu\n"
 166                 "Swap: %8lu %8lu %8lu\n",
 167                 i.totalram, i.totalram-i.freeram, i.freeram, i.sharedram, i.bufferram,
 168                 i.totalswap, i.totalswap-i.freeswap, i.freeswap);
 169 }
 170 
 171 static int get_version(char * buffer)
     /* [previous][next][first][last][top][bottom][index][help] */
 172 {
 173         extern char *linux_banner;
 174 
 175         strcpy(buffer, linux_banner);
 176         return strlen(buffer);
 177 }
 178 
 179 static struct task_struct ** get_task(pid_t pid)
     /* [previous][next][first][last][top][bottom][index][help] */
 180 {
 181         struct task_struct ** p;
 182 
 183         p = task;
 184         while (++p < task+NR_TASKS) {
 185                 if (*p && (*p)->pid == pid)
 186                         return p;
 187         }
 188         return NULL;
 189 }
 190 
 191 static unsigned long get_phys_addr(struct task_struct ** p, unsigned long ptr)
     /* [previous][next][first][last][top][bottom][index][help] */
 192 {
 193         unsigned long page;
 194 
 195         if (!p || !*p || ptr >= TASK_SIZE)
 196                 return 0;
 197         page = *PAGE_DIR_OFFSET((*p)->tss.cr3,ptr);
 198         if (!(page & PAGE_PRESENT))
 199                 return 0;
 200         page &= PAGE_MASK;
 201         page += PAGE_PTR(ptr);
 202         page = *(unsigned long *) page;
 203         if (!(page & PAGE_PRESENT))
 204                 return 0;
 205         page &= PAGE_MASK;
 206         page += ptr & ~PAGE_MASK;
 207         return page;
 208 }
 209 
 210 static int get_array(struct task_struct ** p, unsigned long start, unsigned long end, char * buffer)
     /* [previous][next][first][last][top][bottom][index][help] */
 211 {
 212         unsigned long addr;
 213         int size = 0, result = 0;
 214         char c;
 215 
 216         if (start >= end)
 217                 return result;
 218         for (;;) {
 219                 addr = get_phys_addr(p, start);
 220                 if (!addr)
 221                         goto ready;
 222                 do {
 223                         c = *(char *) addr;
 224                         if (!c)
 225                                 result = size;
 226                         if (size < PAGE_SIZE)
 227                                 buffer[size++] = c;
 228                         else
 229                                 goto ready;
 230                         addr++;
 231                         start++;
 232                         if (!c && start >= end)
 233                                 goto ready;
 234                 } while (addr & ~PAGE_MASK);
 235         }
 236 ready:
 237         /* remove the trailing blanks, used to fill out argv,envp space */
 238         while (result>0 && buffer[result-1]==' ')
 239                 result--;
 240         return result;
 241 }
 242 
 243 static int get_env(int pid, char * buffer)
     /* [previous][next][first][last][top][bottom][index][help] */
 244 {
 245         struct task_struct ** p = get_task(pid);
 246 
 247         if (!p || !*p)
 248                 return 0;
 249         return get_array(p, (*p)->mm->env_start, (*p)->mm->env_end, buffer);
 250 }
 251 
 252 static int get_arg(int pid, char * buffer)
     /* [previous][next][first][last][top][bottom][index][help] */
 253 {
 254         struct task_struct ** p = get_task(pid);
 255 
 256         if (!p || !*p)
 257                 return 0;
 258         return get_array(p, (*p)->mm->arg_start, (*p)->mm->arg_end, buffer);
 259 }
 260 
 261 static unsigned long get_wchan(struct task_struct *p)
     /* [previous][next][first][last][top][bottom][index][help] */
 262 {
 263         unsigned long ebp, eip;
 264         unsigned long stack_page;
 265         int count = 0;
 266 
 267         if (!p || p == current || p->state == TASK_RUNNING)
 268                 return 0;
 269         stack_page = p->kernel_stack_page;
 270         if (!stack_page)
 271                 return 0;
 272         ebp = p->tss.ebp;
 273         do {
 274                 if (ebp < stack_page || ebp >= 4092+stack_page)
 275                         return 0;
 276                 eip = *(unsigned long *) (ebp+4);
 277                 if ((void *)eip != sleep_on &&
 278                     (void *)eip != interruptible_sleep_on)
 279                         return eip;
 280                 ebp = *(unsigned long *) ebp;
 281         } while (count++ < 16);
 282         return 0;
 283 }
 284 
 285 #define KSTK_EIP(stack) (((unsigned long *)stack)[1019])
 286 #define KSTK_ESP(stack) (((unsigned long *)stack)[1022])
 287 
 288 static int get_stat(int pid, char * buffer)
     /* [previous][next][first][last][top][bottom][index][help] */
 289 {
 290         struct task_struct ** p = get_task(pid);
 291         unsigned long sigignore=0, sigcatch=0, bit=1, wchan;
 292         unsigned long vsize, eip, esp;
 293         int i,tty_pgrp;
 294         char state;
 295 
 296         if (!p || !*p)
 297                 return 0;
 298         if ((*p)->state < 0 || (*p)->state > 5)
 299                 state = '.';
 300         else
 301                 state = "RSDZTD"[(*p)->state];
 302         eip = esp = 0;
 303         vsize = (*p)->kernel_stack_page;
 304         if (vsize) {
 305                 eip = KSTK_EIP(vsize);
 306                 esp = KSTK_ESP(vsize);
 307                 vsize = (*p)->mm->brk - (*p)->mm->start_code + PAGE_SIZE-1;
 308                 if (esp)
 309                         vsize += TASK_SIZE - esp;
 310         }
 311         wchan = get_wchan(*p);
 312         for(i=0; i<32; ++i) {
 313                 switch((int) (*p)->sigaction[i].sa_handler) {
 314                 case 1: sigignore |= bit; break;
 315                 case 0: break;
 316                 default: sigcatch |= bit;
 317                 } bit <<= 1;
 318         }
 319         if ((*p)->tty)
 320                 tty_pgrp = (*p)->tty->pgrp;
 321         else
 322                 tty_pgrp = -1;
 323         return sprintf(buffer,"%d (%s) %c %d %d %d %d %d %lu %lu \
 324 %lu %lu %lu %ld %ld %ld %ld %ld %ld %lu %lu %ld %lu %lu %u %lu %lu %lu %lu %lu %lu \
 325 %lu %lu %lu %lu\n",
 326                 pid,
 327                 (*p)->comm,
 328                 state,
 329                 (*p)->p_pptr->pid,
 330                 (*p)->pgrp,
 331                 (*p)->session,
 332                 (*p)->tty ? (*p)->tty->device : 0,
 333                 tty_pgrp,
 334                 (*p)->flags,
 335                 (*p)->mm->min_flt,
 336                 (*p)->mm->cmin_flt,
 337                 (*p)->mm->maj_flt,
 338                 (*p)->mm->cmaj_flt,
 339                 (*p)->utime,
 340                 (*p)->stime,
 341                 (*p)->cutime,
 342                 (*p)->cstime,
 343                 (*p)->counter,  /* this is the kernel priority ---
 344                                    subtract 30 in your user-level program. */
 345                 (*p)->priority, /* this is the nice value ---
 346                                    subtract 15 in your user-level program. */
 347                 (*p)->timeout,
 348                 (*p)->it_real_value,
 349                 (*p)->start_time,
 350                 vsize,
 351                 (*p)->mm->rss, /* you might want to shift this left 3 */
 352                 (*p)->rlim[RLIMIT_RSS].rlim_cur,
 353                 (*p)->mm->start_code,
 354                 (*p)->mm->end_code,
 355                 (*p)->mm->start_stack,
 356                 esp,
 357                 eip,
 358                 (*p)->signal,
 359                 (*p)->blocked,
 360                 sigignore,
 361                 sigcatch,
 362                 wchan);
 363 }
 364 
 365 static int get_statm(int pid, char * buffer)
     /* [previous][next][first][last][top][bottom][index][help] */
 366 {
 367         struct task_struct ** p = get_task(pid);
 368         int i, tpag;
 369         int size=0, resident=0, share=0, trs=0, lrs=0, drs=0, dt=0;
 370         unsigned long ptbl, *buf, *pte, *pagedir, map_nr;
 371 
 372         if (!p || !*p)
 373                 return 0;
 374         tpag = (*p)->mm->end_code / PAGE_SIZE;
 375         if ((*p)->state != TASK_ZOMBIE) {
 376           pagedir = (unsigned long *) (*p)->tss.cr3;
 377           for (i = 0; i < 0x300; ++i) {
 378             if ((ptbl = pagedir[i]) == 0) {
 379               tpag -= PTRS_PER_PAGE;
 380               continue;
 381             }
 382             buf = (unsigned long *)(ptbl & PAGE_MASK);
 383             for (pte = buf; pte < (buf + PTRS_PER_PAGE); ++pte) {
 384               if (*pte != 0) {
 385                 ++size;
 386                 if (*pte & 1) {
 387                   ++resident;
 388                   if (tpag > 0)
 389                     ++trs;
 390                   else
 391                     ++drs;
 392                   if (i >= 15 && i < 0x2f0) {
 393                     ++lrs;
 394                     if (*pte & 0x40)
 395                       ++dt;
 396                     else
 397                       --drs;
 398                   }
 399                   map_nr = MAP_NR(*pte);
 400                   if (map_nr < (high_memory / PAGE_SIZE) && mem_map[map_nr] > 1)
 401                     ++share;
 402                 }
 403               }
 404               --tpag;
 405             }
 406           }
 407         }
 408         return sprintf(buffer,"%d %d %d %d %d %d %d\n",
 409                        size, resident, share, trs, lrs, drs, dt);
 410 }
 411 
 412 static int get_maps(int pid, char *buf)
     /* [previous][next][first][last][top][bottom][index][help] */
 413 {
 414         int sz = 0;
 415         struct task_struct **p = get_task(pid);
 416         struct vm_area_struct *map;
 417 
 418         if (!p || !*p)
 419                 return 0;
 420 
 421         for(map = (*p)->mm->mmap; map != NULL; map = map->vm_next) {
 422                 char str[7], *cp = str;
 423                 int flags;
 424                 int end = sz + 80;      /* Length of line */
 425                 dev_t dev;
 426                 unsigned long ino;
 427 
 428                 flags = map->vm_flags;
 429 
 430                 *cp++ = flags & VM_READ ? 'r' : '-';
 431                 *cp++ = flags & VM_WRITE ? 'w' : '-';
 432                 *cp++ = flags & VM_EXEC ? 'x' : '-';
 433                 *cp++ = flags & VM_SHARED ? 's' : 'p';
 434                 *cp++ = 0;
 435                 
 436                 if (end >= PAGE_SIZE) {
 437                         sprintf(buf+sz, "...\n");
 438                         break;
 439                 }
 440                 
 441                 if (map->vm_inode != NULL) {
 442                         dev = map->vm_inode->i_dev;
 443                         ino = map->vm_inode->i_ino;
 444                 } else {
 445                         dev = 0;
 446                         ino = 0;
 447                 }
 448 
 449                 sz += sprintf(buf+sz, "%08lx-%08lx %s %08lx %02x:%02x %lu\n",
 450                               map->vm_start, map->vm_end, str, map->vm_offset,
 451                               MAJOR(dev),MINOR(dev), ino);
 452                 if (sz > end) {
 453                         printk("get_maps: end(%d) < sz(%d)\n", end, sz);
 454                         break;
 455                 }
 456         }
 457         
 458         return sz;
 459 }
 460 
 461 extern int get_module_list(char *);
 462 extern int get_device_list(char *);
 463 extern int get_filesystem_list(char *);
 464 extern int get_ksyms_list(char *);
 465 extern int get_irq_list(char *);
 466 extern int get_dma_list(char *);
 467 
 468 static int get_root_array(char * page, int type)
     /* [previous][next][first][last][top][bottom][index][help] */
 469 {
 470         switch (type) {
 471                 case PROC_LOADAVG:
 472                         return get_loadavg(page);
 473 
 474                 case PROC_UPTIME:
 475                         return get_uptime(page);
 476 
 477                 case PROC_MEMINFO:
 478                         return get_meminfo(page);
 479 
 480                 case PROC_VERSION:
 481                         return get_version(page);
 482 
 483 #ifdef CONFIG_DEBUG_MALLOC
 484                 case PROC_MALLOC:
 485                         return get_malloc(page);
 486 #endif
 487 
 488                 case PROC_MODULES:
 489                         return get_module_list(page);
 490 
 491                 case PROC_STAT:
 492                         return get_kstat(page);
 493 
 494                 case PROC_DEVICES:
 495                         return get_device_list(page);
 496 
 497                 case PROC_INTERRUPTS:
 498                         return get_irq_list(page);
 499 
 500                 case PROC_FILESYSTEMS:
 501                         return get_filesystem_list(page);
 502 
 503                 case PROC_KSYMS:
 504                         return get_ksyms_list(page);
 505 
 506                 case PROC_DMA:
 507                         return get_dma_list(page);
 508         }
 509         return -EBADF;
 510 }
 511 
 512 static int get_process_array(char * page, int pid, int type)
     /* [previous][next][first][last][top][bottom][index][help] */
 513 {
 514         switch (type) {
 515                 case PROC_PID_ENVIRON:
 516                         return get_env(pid, page);
 517                 case PROC_PID_CMDLINE:
 518                         return get_arg(pid, page);
 519                 case PROC_PID_STAT:
 520                         return get_stat(pid, page);
 521                 case PROC_PID_STATM:
 522                         return get_statm(pid, page);
 523                 case PROC_PID_MAPS:
 524                         return get_maps(pid, page);
 525         }
 526         return -EBADF;
 527 }
 528 
 529 
 530 static inline int fill_array(char * page, int pid, int type)
     /* [previous][next][first][last][top][bottom][index][help] */
 531 {
 532         if (pid)
 533                 return get_process_array(page, pid, type);
 534         return get_root_array(page, type);
 535 }
 536 
 537 static int array_read(struct inode * inode, struct file * file,char * buf, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
 538 {
 539         unsigned long page;
 540         int length;
 541         int end;
 542         unsigned int type, pid;
 543 
 544         if (count < 0)
 545                 return -EINVAL;
 546         if (!(page = __get_free_page(GFP_KERNEL)))
 547                 return -ENOMEM;
 548         type = inode->i_ino;
 549         pid = type >> 16;
 550         type &= 0x0000ffff;
 551         length = fill_array((char *) page, pid, type);
 552         if (length < 0) {
 553                 free_page(page);
 554                 return length;
 555         }
 556         if (file->f_pos >= length) {
 557                 free_page(page);
 558                 return 0;
 559         }
 560         if (count + file->f_pos > length)
 561                 count = length - file->f_pos;
 562         end = count + file->f_pos;
 563         memcpy_tofs(buf, (char *) page + file->f_pos, count);
 564         free_page(page);
 565         file->f_pos = end;
 566         return count;
 567 }
 568 
 569 static struct file_operations proc_array_operations = {
 570         NULL,           /* array_lseek */
 571         array_read,
 572         NULL,           /* array_write */
 573         NULL,           /* array_readdir */
 574         NULL,           /* array_select */
 575         NULL,           /* array_ioctl */
 576         NULL,           /* mmap */
 577         NULL,           /* no special open code */
 578         NULL,           /* no special release code */
 579         NULL            /* can't fsync */
 580 };
 581 
 582 struct inode_operations proc_array_inode_operations = {
 583         &proc_array_operations, /* default base directory file-ops */
 584         NULL,                   /* create */
 585         NULL,                   /* lookup */
 586         NULL,                   /* link */
 587         NULL,                   /* unlink */
 588         NULL,                   /* symlink */
 589         NULL,                   /* mkdir */
 590         NULL,                   /* rmdir */
 591         NULL,                   /* mknod */
 592         NULL,                   /* rename */
 593         NULL,                   /* readlink */
 594         NULL,                   /* follow_link */
 595         NULL,                   /* bmap */
 596         NULL,                   /* truncate */
 597         NULL                    /* permission */
 598 };

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