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

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