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

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