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

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