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

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