root/fs/proc/array.c

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

DEFINITIONS

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

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