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

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