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

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