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_task
  10. get_phys_addr
  11. get_array
  12. get_env
  13. get_arg
  14. get_wchan
  15. get_stat
  16. statm_pte_range
  17. statm_pmd_range
  18. statm_pgd_range
  19. get_statm
  20. read_maps
  21. get_root_array
  22. get_process_array
  23. fill_array
  24. array_read
  25. 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/mm.h>
  44 
  45 #include <asm/segment.h>
  46 #include <asm/pgtable.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_user(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_user(*((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 %d/%d\n",
 179                 LOAD_INT(a), LOAD_FRAC(a),
 180                 LOAD_INT(b), LOAD_FRAC(b),
 181                 LOAD_INT(c), LOAD_FRAC(c),
 182                 nr_running, nr_tasks);
 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 struct task_struct ** get_task(pid_t pid)
     /* [previous][next][first][last][top][bottom][index][help] */
 276 {
 277         struct task_struct ** p;
 278 
 279         p = task;
 280         while (++p < task+NR_TASKS) {
 281                 if (*p && (*p)->pid == pid)
 282                         return p;
 283         }
 284         return NULL;
 285 }
 286 
 287 static unsigned long get_phys_addr(struct task_struct * p, unsigned long ptr)
     /* [previous][next][first][last][top][bottom][index][help] */
 288 {
 289         pgd_t *page_dir;
 290         pmd_t *page_middle;
 291         pte_t pte;
 292 
 293         if (!p || ptr >= TASK_SIZE)
 294                 return 0;
 295         page_dir = pgd_offset(p,ptr);
 296         if (pgd_none(*page_dir))
 297                 return 0;
 298         if (pgd_bad(*page_dir)) {
 299                 printk("bad page directory entry %08lx\n", pgd_val(*page_dir));
 300                 pgd_clear(page_dir);
 301                 return 0;
 302         }
 303         page_middle = pmd_offset(page_dir,ptr);
 304         if (pmd_none(*page_middle))
 305                 return 0;
 306         if (pmd_bad(*page_middle)) {
 307                 printk("bad page middle entry %08lx\n", pmd_val(*page_middle));
 308                 pmd_clear(page_middle);
 309                 return 0;
 310         }
 311         pte = *pte_offset(page_middle,ptr);
 312         if (!pte_present(pte))
 313                 return 0;
 314         return pte_page(pte) + (ptr & ~PAGE_MASK);
 315 }
 316 
 317 static int get_array(struct task_struct ** p, unsigned long start, unsigned long end, char * buffer)
     /* [previous][next][first][last][top][bottom][index][help] */
 318 {
 319         unsigned long addr;
 320         int size = 0, result = 0;
 321         char c;
 322 
 323         if (start >= end)
 324                 return result;
 325         for (;;) {
 326                 addr = get_phys_addr(*p, start);
 327                 if (!addr)
 328                         goto ready;
 329                 do {
 330                         c = *(char *) addr;
 331                         if (!c)
 332                                 result = size;
 333                         if (size < PAGE_SIZE)
 334                                 buffer[size++] = c;
 335                         else
 336                                 goto ready;
 337                         addr++;
 338                         start++;
 339                         if (!c && start >= end)
 340                                 goto ready;
 341                 } while (addr & ~PAGE_MASK);
 342         }
 343 ready:
 344         /* remove the trailing blanks, used to fill out argv,envp space */
 345         while (result>0 && buffer[result-1]==' ')
 346                 result--;
 347         return result;
 348 }
 349 
 350 static int get_env(int pid, char * buffer)
     /* [previous][next][first][last][top][bottom][index][help] */
 351 {
 352         struct task_struct ** p = get_task(pid);
 353 
 354         if (!p || !*p)
 355                 return 0;
 356         return get_array(p, (*p)->mm->env_start, (*p)->mm->env_end, buffer);
 357 }
 358 
 359 static int get_arg(int pid, char * buffer)
     /* [previous][next][first][last][top][bottom][index][help] */
 360 {
 361         struct task_struct ** p = get_task(pid);
 362 
 363         if (!p || !*p)
 364                 return 0;
 365         return get_array(p, (*p)->mm->arg_start, (*p)->mm->arg_end, buffer);
 366 }
 367 
 368 static unsigned long get_wchan(struct task_struct *p)
     /* [previous][next][first][last][top][bottom][index][help] */
 369 {
 370 #ifdef __i386__
 371         unsigned long ebp, eip;
 372         unsigned long stack_page;
 373         int count = 0;
 374 
 375         if (!p || p == current || p->state == TASK_RUNNING)
 376                 return 0;
 377         stack_page = p->kernel_stack_page;
 378         if (!stack_page)
 379                 return 0;
 380         ebp = p->tss.ebp;
 381         do {
 382                 if (ebp < stack_page || ebp >= 4092+stack_page)
 383                         return 0;
 384                 eip = *(unsigned long *) (ebp+4);
 385                 if ((void *)eip != sleep_on &&
 386                     (void *)eip != interruptible_sleep_on)
 387                         return eip;
 388                 ebp = *(unsigned long *) ebp;
 389         } while (count++ < 16);
 390 #endif
 391         return 0;
 392 }
 393 
 394 #define KSTK_EIP(stack) (((unsigned long *)stack)[1019])
 395 #define KSTK_ESP(stack) (((unsigned long *)stack)[1022])
 396 
 397 static int get_stat(int pid, char * buffer)
     /* [previous][next][first][last][top][bottom][index][help] */
 398 {
 399         struct task_struct ** p = get_task(pid);
 400         unsigned long sigignore=0, sigcatch=0, bit=1, wchan;
 401         unsigned long vsize, eip, esp;
 402         int i,tty_pgrp;
 403         char state;
 404 
 405         if (!p || !*p)
 406                 return 0;
 407         if ((*p)->state < 0 || (*p)->state > 5)
 408                 state = '.';
 409         else
 410                 state = "RSDZTD"[(*p)->state];
 411         eip = esp = 0;
 412         vsize = (*p)->kernel_stack_page;
 413         if (vsize) {
 414                 eip = KSTK_EIP(vsize);
 415                 esp = KSTK_ESP(vsize);
 416                 vsize = (*p)->mm->brk - (*p)->mm->start_code + PAGE_SIZE-1;
 417                 if (esp)
 418                         vsize += TASK_SIZE - esp;
 419         }
 420         wchan = get_wchan(*p);
 421         for(i=0; i<32; ++i) {
 422                 switch((unsigned long) (*p)->sigaction[i].sa_handler) {
 423                 case 1: sigignore |= bit; break;
 424                 case 0: break;
 425                 default: sigcatch |= bit;
 426                 } bit <<= 1;
 427         }
 428         if ((*p)->tty)
 429                 tty_pgrp = (*p)->tty->pgrp;
 430         else
 431                 tty_pgrp = -1;
 432         return sprintf(buffer,"%d (%s) %c %d %d %d %d %d %lu %lu \
 433 %lu %lu %lu %ld %ld %ld %ld %ld %ld %lu %lu %ld %lu %lu %lu %lu %lu %lu %lu %lu %lu \
 434 %lu %lu %lu %lu\n",
 435                 pid,
 436                 (*p)->comm,
 437                 state,
 438                 (*p)->p_pptr->pid,
 439                 (*p)->pgrp,
 440                 (*p)->session,
 441                 (*p)->tty ? (*p)->tty->device : 0,
 442                 tty_pgrp,
 443                 (*p)->flags,
 444                 (*p)->mm->min_flt,
 445                 (*p)->mm->cmin_flt,
 446                 (*p)->mm->maj_flt,
 447                 (*p)->mm->cmaj_flt,
 448                 (*p)->utime,
 449                 (*p)->stime,
 450                 (*p)->cutime,
 451                 (*p)->cstime,
 452                 (*p)->counter,  /* this is the kernel priority ---
 453                                    subtract 30 in your user-level program. */
 454                 (*p)->priority, /* this is the nice value ---
 455                                    subtract 15 in your user-level program. */
 456                 (*p)->timeout,
 457                 (*p)->it_real_value,
 458                 (*p)->start_time,
 459                 vsize,
 460                 (*p)->mm->rss, /* you might want to shift this left 3 */
 461                 (*p)->rlim[RLIMIT_RSS].rlim_cur,
 462                 (*p)->mm->start_code,
 463                 (*p)->mm->end_code,
 464                 (*p)->mm->start_stack,
 465                 esp,
 466                 eip,
 467                 (*p)->signal,
 468                 (*p)->blocked,
 469                 sigignore,
 470                 sigcatch,
 471                 wchan);
 472 }
 473                 
 474 static inline void statm_pte_range(pmd_t * pmd, unsigned long address, unsigned long size,
     /* [previous][next][first][last][top][bottom][index][help] */
 475         int * pages, int * shared, int * dirty, int * total)
 476 {
 477         pte_t * pte;
 478         unsigned long end;
 479 
 480         if (pmd_none(*pmd))
 481                 return;
 482         if (pmd_bad(*pmd)) {
 483                 printk("statm_pte_range: bad pmd (%08lx)\n", pmd_val(*pmd));
 484                 pmd_clear(pmd);
 485                 return;
 486         }
 487         pte = pte_offset(pmd, address);
 488         address &= ~PMD_MASK;
 489         end = address + size;
 490         if (end > PMD_SIZE)
 491                 end = PMD_SIZE;
 492         do {
 493                 pte_t page = *pte;
 494 
 495                 address += PAGE_SIZE;
 496                 pte++;
 497                 if (pte_none(page))
 498                         continue;
 499                 ++*total;
 500                 if (!pte_present(page))
 501                         continue;
 502                 ++*pages;
 503                 if (pte_dirty(page))
 504                         ++*dirty;
 505                 if (pte_page(page) >= high_memory)
 506                         continue;
 507                 if (mem_map[MAP_NR(pte_page(page))] > 1)
 508                         ++*shared;
 509         } while (address < end);
 510 }
 511 
 512 static inline void statm_pmd_range(pgd_t * pgd, unsigned long address, unsigned long size,
     /* [previous][next][first][last][top][bottom][index][help] */
 513         int * pages, int * shared, int * dirty, int * total)
 514 {
 515         pmd_t * pmd;
 516         unsigned long end;
 517 
 518         if (pgd_none(*pgd))
 519                 return;
 520         if (pgd_bad(*pgd)) {
 521                 printk("statm_pmd_range: bad pgd (%08lx)\n", pgd_val(*pgd));
 522                 pgd_clear(pgd);
 523                 return;
 524         }
 525         pmd = pmd_offset(pgd, address);
 526         address &= ~PGDIR_MASK;
 527         end = address + size;
 528         if (end > PGDIR_SIZE)
 529                 end = PGDIR_SIZE;
 530         do {
 531                 statm_pte_range(pmd, address, end - address, pages, shared, dirty, total);
 532                 address = (address + PMD_SIZE) & PMD_MASK;
 533                 pmd++;
 534         } while (address < end);
 535 }
 536 
 537 static void statm_pgd_range(pgd_t * pgd, unsigned long address, unsigned long end,
     /* [previous][next][first][last][top][bottom][index][help] */
 538         int * pages, int * shared, int * dirty, int * total)
 539 {
 540         while (address < end) {
 541                 statm_pmd_range(pgd, address, end - address, pages, shared, dirty, total);
 542                 address = (address + PGDIR_SIZE) & PGDIR_MASK;
 543                 pgd++;
 544         }
 545 }
 546 
 547 static int get_statm(int pid, char * buffer)
     /* [previous][next][first][last][top][bottom][index][help] */
 548 {
 549         struct task_struct ** p = get_task(pid);
 550         int size=0, resident=0, share=0, trs=0, lrs=0, drs=0, dt=0;
 551 
 552         if (!p || !*p)
 553                 return 0;
 554         if ((*p)->state != TASK_ZOMBIE) {
 555                 struct vm_area_struct * vma = (*p)->mm->mmap;
 556 
 557                 while (vma) {
 558                         pgd_t *pgd = pgd_offset(*p, vma->vm_start);
 559                         int pages = 0, shared = 0, dirty = 0, total = 0;
 560 
 561                         statm_pgd_range(pgd, vma->vm_start, vma->vm_end, &pages, &shared, &dirty, &total);
 562                         resident += pages;
 563                         share += shared;
 564                         dt += dirty;
 565                         size += total;
 566                         if (vma->vm_flags & VM_EXECUTABLE)
 567                                 trs += pages;   /* text */
 568                         else if (vma->vm_flags & VM_GROWSDOWN)
 569                                 drs += pages;   /* stack */
 570                         else if (vma->vm_end > 0x60000000)
 571                                 lrs += pages;   /* library */
 572                         else
 573                                 drs += pages;
 574                         vma = vma->vm_next;
 575                 }
 576         }
 577         return sprintf(buffer,"%d %d %d %d %d %d %d\n",
 578                        size, resident, share, trs, lrs, drs, dt);
 579 }
 580 
 581 /*
 582  * The way we support synthetic files > 4K
 583  * - without storing their contents in some buffer and
 584  * - without walking through the entire synthetic file until we reach the
 585  *   position of the requested data
 586  * is to cleverly encode the current position in the file's f_pos field.
 587  * There is no requirement that a read() call which returns `count' bytes
 588  * of data increases f_pos by exactly `count'.
 589  *
 590  * This idea is Linus' one. Bruno implemented it.
 591  */
 592 
 593 /*
 594  * For the /proc/<pid>/maps file, we use fixed length records, each containing
 595  * a single line.
 596  */
 597 #define MAPS_LINE_LENGTH        1024
 598 #define MAPS_LINE_SHIFT         10
 599 /*
 600  * f_pos = (number of the vma in the task->mm->mmap list) * MAPS_LINE_LENGTH
 601  *         + (index into the line)
 602  */
 603 #define MAPS_LINE_FORMAT          "%08lx-%08lx %s %08lx %02x:%02x %lu\n"
 604 #define MAPS_LINE_MAX   49 /* sum of 8  1  8  1 4 1 8  1  2 1  2 1 10 1 */
 605 
 606 static int read_maps (int pid, struct file * file, char * buf, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
 607 {
 608         struct task_struct ** p = get_task(pid);
 609         char * destptr;
 610         loff_t lineno;
 611         int column;
 612         struct vm_area_struct * map;
 613         int i;
 614 
 615         if (!p || !*p)
 616                 return -EINVAL;
 617 
 618         if (count == 0)
 619                 return 0;
 620 
 621         /* decode f_pos */
 622         lineno = file->f_pos >> MAPS_LINE_SHIFT;
 623         column = file->f_pos & (MAPS_LINE_LENGTH-1);
 624 
 625         /* quickly go to line lineno */
 626         for (map = (*p)->mm->mmap, i = 0; map && (i < lineno); map = map->vm_next, i++)
 627                 continue;
 628 
 629         destptr = buf;
 630 
 631         for ( ; map ; ) {
 632                 /* produce the next line */
 633                 char line[MAPS_LINE_MAX+1];
 634                 char str[5], *cp = str;
 635                 int flags;
 636                 dev_t dev;
 637                 unsigned long ino;
 638                 int len;
 639 
 640                 flags = map->vm_flags;
 641 
 642                 *cp++ = flags & VM_READ ? 'r' : '-';
 643                 *cp++ = flags & VM_WRITE ? 'w' : '-';
 644                 *cp++ = flags & VM_EXEC ? 'x' : '-';
 645                 *cp++ = flags & VM_MAYSHARE ? 's' : 'p';
 646                 *cp++ = 0;
 647 
 648                 if (map->vm_inode != NULL) {
 649                         dev = map->vm_inode->i_dev;
 650                         ino = map->vm_inode->i_ino;
 651                 } else {
 652                         dev = 0;
 653                         ino = 0;
 654                 }
 655 
 656                 len = sprintf(line, MAPS_LINE_FORMAT,
 657                               map->vm_start, map->vm_end, str, map->vm_offset,
 658                               MAJOR(dev),MINOR(dev), ino);
 659 
 660                 if (column >= len) {
 661                         column = 0; /* continue with next line at column 0 */
 662                         lineno++;
 663                         map = map->vm_next;
 664                         continue;
 665                 }
 666 
 667                 i = len-column;
 668                 if (i > count)
 669                         i = count;
 670                 memcpy_tofs(destptr, line+column, i);
 671                 destptr += i; count -= i;
 672                 column += i;
 673                 if (column >= len) {
 674                         column = 0; /* next time: next line at column 0 */
 675                         lineno++;
 676                         map = map->vm_next;
 677                 }
 678 
 679                 /* done? */
 680                 if (count == 0)
 681                         break;
 682 
 683                 /* By writing to user space, we might have slept.
 684                  * Stop the loop, to avoid a race condition.
 685                  */
 686                 if (*p != current)
 687                         break;
 688         }
 689 
 690         /* encode f_pos */
 691         file->f_pos = (lineno << MAPS_LINE_SHIFT) + column;
 692 
 693         return destptr-buf;
 694 }
 695 
 696 extern int get_module_list(char *);
 697 extern int get_device_list(char *);
 698 extern int get_filesystem_list(char *);
 699 extern int get_ksyms_list(char *);
 700 extern int get_irq_list(char *);
 701 extern int get_dma_list(char *);
 702 extern int get_cpuinfo(char *);
 703 extern int get_pci_list(char*);
 704 
 705 static int get_root_array(char * page, int type)
     /* [previous][next][first][last][top][bottom][index][help] */
 706 {
 707         switch (type) {
 708                 case PROC_LOADAVG:
 709                         return get_loadavg(page);
 710 
 711                 case PROC_UPTIME:
 712                         return get_uptime(page);
 713 
 714                 case PROC_MEMINFO:
 715                         return get_meminfo(page);
 716 
 717 #ifdef CONFIG_PCI
 718                 case PROC_PCI:
 719                         return get_pci_list(page);
 720 #endif
 721                         
 722                 case PROC_CPUINFO:
 723                         return get_cpuinfo(page);
 724 
 725                 case PROC_VERSION:
 726                         return get_version(page);
 727 
 728 #ifdef CONFIG_DEBUG_MALLOC
 729                 case PROC_MALLOC:
 730                         return get_malloc(page);
 731 #endif
 732 
 733                 case PROC_MODULES:
 734                         return get_module_list(page);
 735 
 736                 case PROC_STAT:
 737                         return get_kstat(page);
 738 
 739                 case PROC_DEVICES:
 740                         return get_device_list(page);
 741 
 742                 case PROC_INTERRUPTS:
 743                         return get_irq_list(page);
 744 
 745                 case PROC_FILESYSTEMS:
 746                         return get_filesystem_list(page);
 747 
 748                 case PROC_KSYMS:
 749                         return get_ksyms_list(page);
 750 
 751                 case PROC_DMA:
 752                         return get_dma_list(page);
 753 
 754                 case PROC_IOPORTS:
 755                         return get_ioport_list(page);
 756         }
 757         return -EBADF;
 758 }
 759 
 760 static int get_process_array(char * page, int pid, int type)
     /* [previous][next][first][last][top][bottom][index][help] */
 761 {
 762         switch (type) {
 763                 case PROC_PID_ENVIRON:
 764                         return get_env(pid, page);
 765                 case PROC_PID_CMDLINE:
 766                         return get_arg(pid, page);
 767                 case PROC_PID_STAT:
 768                         return get_stat(pid, page);
 769                 case PROC_PID_STATM:
 770                         return get_statm(pid, page);
 771         }
 772         return -EBADF;
 773 }
 774 
 775 
 776 static inline int fill_array(char * page, int pid, int type)
     /* [previous][next][first][last][top][bottom][index][help] */
 777 {
 778         if (pid)
 779                 return get_process_array(page, pid, type);
 780         return get_root_array(page, type);
 781 }
 782 
 783 static int array_read(struct inode * inode, struct file * file,char * buf, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
 784 {
 785         unsigned long page;
 786         int length;
 787         int end;
 788         unsigned int type, pid;
 789 
 790         if (count < 0)
 791                 return -EINVAL;
 792         if (!(page = __get_free_page(GFP_KERNEL)))
 793                 return -ENOMEM;
 794         type = inode->i_ino;
 795         pid = type >> 16;
 796         type &= 0x0000ffff;
 797         length = fill_array((char *) page, pid, type);
 798         if (length < 0) {
 799                 free_page(page);
 800                 return length;
 801         }
 802         if (file->f_pos >= length) {
 803                 free_page(page);
 804                 return 0;
 805         }
 806         if (count + file->f_pos > length)
 807                 count = length - file->f_pos;
 808         end = count + file->f_pos;
 809         memcpy_tofs(buf, (char *) page + file->f_pos, count);
 810         free_page(page);
 811         file->f_pos = end;
 812         return count;
 813 }
 814 
 815 static struct file_operations proc_array_operations = {
 816         NULL,           /* array_lseek */
 817         array_read,
 818         NULL,           /* array_write */
 819         NULL,           /* array_readdir */
 820         NULL,           /* array_select */
 821         NULL,           /* array_ioctl */
 822         NULL,           /* mmap */
 823         NULL,           /* no special open code */
 824         NULL,           /* no special release code */
 825         NULL            /* can't fsync */
 826 };
 827 
 828 struct inode_operations proc_array_inode_operations = {
 829         &proc_array_operations, /* default base directory file-ops */
 830         NULL,                   /* create */
 831         NULL,                   /* lookup */
 832         NULL,                   /* link */
 833         NULL,                   /* unlink */
 834         NULL,                   /* symlink */
 835         NULL,                   /* mkdir */
 836         NULL,                   /* rmdir */
 837         NULL,                   /* mknod */
 838         NULL,                   /* rename */
 839         NULL,                   /* readlink */
 840         NULL,                   /* follow_link */
 841         NULL,                   /* bmap */
 842         NULL,                   /* truncate */
 843         NULL                    /* permission */
 844 };
 845 
 846 static int arraylong_read (struct inode * inode, struct file * file, char * buf, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
 847 {
 848         unsigned int pid = inode->i_ino >> 16;
 849         unsigned int type = inode->i_ino & 0x0000ffff;
 850 
 851         if (count < 0)
 852                 return -EINVAL;
 853 
 854         switch (type) {
 855                 case PROC_PID_MAPS:
 856                         return read_maps(pid, file, buf, count);
 857         }
 858         return -EINVAL;
 859 }
 860 
 861 static struct file_operations proc_arraylong_operations = {
 862         NULL,           /* array_lseek */
 863         arraylong_read,
 864         NULL,           /* array_write */
 865         NULL,           /* array_readdir */
 866         NULL,           /* array_select */
 867         NULL,           /* array_ioctl */
 868         NULL,           /* mmap */
 869         NULL,           /* no special open code */
 870         NULL,           /* no special release code */
 871         NULL            /* can't fsync */
 872 };
 873 
 874 struct inode_operations proc_arraylong_inode_operations = {
 875         &proc_arraylong_operations,     /* default base directory file-ops */
 876         NULL,                   /* create */
 877         NULL,                   /* lookup */
 878         NULL,                   /* link */
 879         NULL,                   /* unlink */
 880         NULL,                   /* symlink */
 881         NULL,                   /* mkdir */
 882         NULL,                   /* rmdir */
 883         NULL,                   /* mknod */
 884         NULL,                   /* rename */
 885         NULL,                   /* readlink */
 886         NULL,                   /* follow_link */
 887         NULL,                   /* bmap */
 888         NULL,                   /* truncate */
 889         NULL                    /* permission */
 890 };

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