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

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