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

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