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

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