root/fs/proc/array.c

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

DEFINITIONS

This source file includes following definitions.
  1. read_core
  2. read_profile
  3. write_profile
  4. get_loadavg
  5. get_kstat
  6. get_uptime
  7. get_meminfo
  8. get_version
  9. get_task
  10. get_phys_addr
  11. get_array
  12. get_env
  13. get_arg
  14. get_wchan
  15. get_stat
  16. statm_pte_range
  17. statm_pmd_range
  18. statm_pgd_range
  19. get_statm
  20. read_maps
  21. get_root_array
  22. get_process_array
  23. fill_array
  24. array_read
  25. arraylong_read

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

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