2 * linux/fs/proc/array.c
4 * Copyright (C) 1992 by Linus Torvalds
5 * based on ideas by Darren Senn
8 * Michael. K. Johnson: stat,statm extensions.
9 * <johnsonm@stolaf.edu>
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>
17 * Danny ter Haar : added cpuinfo
20 * Alessandro Rubini : profile extension.
21 * <rubini@ipvvis.unipv.it>
23 * Jeff Tranter : added BogoMips field to cpuinfo
24 * <Jeff_Tranter@Mitel.COM>
26 * Bruno Haible : remove 4K limit for the maps file
27 * <haible@ma2s2.mathematik.uni-karlsruhe.de>
29 * Yves Arrouye : remove removal of trailing spaces in get_array.
30 * <Yves.Arrouye@marin.fdn.fr>
32 * Jerome Forissier : added per-CPU time information to /proc/stat
33 * and /proc/<pid>/cpu extension
34 * <forissier@isia.cma.fr>
35 * - Incorporation and non-SMP safe operation
36 * of forissier patch in 2.1.78 by
37 * Hans Marcus <crowbar@concepts.nl>
39 * aeb@cwi.nl : /proc/partitions
42 * Alan Cox : security fixes.
43 * <Alan.Cox@linux.org>
45 * Andi Kleen : Race Fixes.
49 #include <linux/types.h>
50 #include <linux/errno.h>
51 #include <linux/sched.h>
52 #include <linux/kernel.h>
53 #include <linux/kernel_stat.h>
54 #include <linux/tty.h>
55 #include <linux/user.h>
56 #include <linux/a.out.h>
57 #include <linux/string.h>
58 #include <linux/mman.h>
59 #include <linux/proc_fs.h>
60 #include <linux/ioport.h>
61 #include <linux/config.h>
63 #include <linux/pagemap.h>
64 #include <linux/swap.h>
65 #include <linux/slab.h>
66 #include <linux/smp.h>
67 #include <linux/signal.h>
69 #include <asm/uaccess.h>
70 #include <asm/pgtable.h>
73 #define LOAD_INT(x) ((x) >> FSHIFT)
74 #define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)
76 #ifdef CONFIG_DEBUG_MALLOC
77 int get_malloc(char * buffer
);
81 static ssize_t
read_core(struct file
* file
, char * buf
,
82 size_t count
, loff_t
*ppos
)
84 unsigned long p
= *ppos
, memsize
;
89 #if defined (__i386__) || defined (__mc68000__)
90 # define FIRST_MAPPED PAGE_SIZE /* we don't have page 0 mapped on x86.. */
92 # define FIRST_MAPPED 0
95 memset(&dump
, 0, sizeof(struct user
));
97 dump
.u_dsize
= max_mapnr
;
99 dump
.start_data
= PAGE_OFFSET
;
102 memsize
= (max_mapnr
+ 1) << PAGE_SHIFT
;
105 if (count
> memsize
- p
)
109 if (p
< sizeof(struct user
) && count
> 0) {
111 if (p
+ count1
> sizeof(struct user
))
112 count1
= sizeof(struct user
)-p
;
113 pnt
= (char *) &dump
+ p
;
114 copy_to_user(buf
,(void *) pnt
, count1
);
121 if (count
> 0 && p
< PAGE_SIZE
+ FIRST_MAPPED
) {
122 count1
= PAGE_SIZE
+ FIRST_MAPPED
- p
;
125 clear_user(buf
, count1
);
132 copy_to_user(buf
, (void *) (PAGE_OFFSET
+p
-PAGE_SIZE
), count
);
139 static struct file_operations proc_kcore_operations
= {
144 struct inode_operations proc_kcore_inode_operations
= {
145 &proc_kcore_operations
,
149 * This function accesses profiling information. The returned data is
150 * binary: the sampling step and the actual contents of the profile
151 * buffer. Use of the program readprofile is recommended in order to
152 * get meaningful info out of these data.
154 static ssize_t
read_profile(struct file
*file
, char *buf
,
155 size_t count
, loff_t
*ppos
)
157 unsigned long p
= *ppos
;
160 unsigned int sample_step
= 1 << prof_shift
;
162 if (p
>= (prof_len
+1)*sizeof(unsigned int))
164 if (count
> (prof_len
+1)*sizeof(unsigned int) - p
)
165 count
= (prof_len
+1)*sizeof(unsigned int) - p
;
168 while (p
< sizeof(unsigned int) && count
> 0) {
169 put_user(*((char *)(&sample_step
)+p
),buf
);
170 buf
++; p
++; count
--; read
++;
172 pnt
= (char *)prof_buffer
+ p
- sizeof(unsigned int);
173 copy_to_user(buf
,(void *)pnt
,count
);
180 * Writing to /proc/profile resets the counters
182 * Writing a 'profiling multiplier' value into it also re-sets the profiling
183 * interrupt frequency, on architectures that support this.
185 static ssize_t
write_profile(struct file
* file
, const char * buf
,
186 size_t count
, loff_t
*ppos
)
189 extern int setup_profiling_timer (unsigned int multiplier
);
191 if (count
==sizeof(int)) {
192 unsigned int multiplier
;
194 if (copy_from_user(&multiplier
, buf
, sizeof(int)))
197 if (setup_profiling_timer(multiplier
))
202 memset(prof_buffer
, 0, prof_len
* sizeof(*prof_buffer
));
206 static struct file_operations proc_profile_operations
= {
212 struct inode_operations proc_profile_inode_operations
= {
213 &proc_profile_operations
,
217 static int get_loadavg(char * buffer
)
221 a
= avenrun
[0] + (FIXED_1
/200);
222 b
= avenrun
[1] + (FIXED_1
/200);
223 c
= avenrun
[2] + (FIXED_1
/200);
224 return sprintf(buffer
,"%d.%02d %d.%02d %d.%02d %d/%d %d\n",
225 LOAD_INT(a
), LOAD_FRAC(a
),
226 LOAD_INT(b
), LOAD_FRAC(b
),
227 LOAD_INT(c
), LOAD_FRAC(c
),
228 nr_running
, nr_tasks
, last_pid
);
231 static int get_kstat(char * buffer
)
235 extern unsigned long total_forks
;
238 ticks
= jiffies
* smp_num_cpus
;
239 for (i
= 0 ; i
< NR_IRQS
; i
++)
240 sum
+= kstat_irqs(i
);
243 len
= sprintf(buffer
,
244 "cpu %u %u %u %lu\n",
248 jiffies
*smp_num_cpus
- (kstat
.cpu_user
+ kstat
.cpu_nice
+ kstat
.cpu_system
));
249 for (i
= 0 ; i
< smp_num_cpus
; i
++)
250 len
+= sprintf(buffer
+ len
, "cpu%d %u %u %u %lu\n",
252 kstat
.per_cpu_user
[cpu_logical_map(i
)],
253 kstat
.per_cpu_nice
[cpu_logical_map(i
)],
254 kstat
.per_cpu_system
[cpu_logical_map(i
)],
255 jiffies
- ( kstat
.per_cpu_user
[cpu_logical_map(i
)] \
256 + kstat
.per_cpu_nice
[cpu_logical_map(i
)] \
257 + kstat
.per_cpu_system
[cpu_logical_map(i
)]));
258 len
+= sprintf(buffer
+ len
,
260 "disk_rio %u %u %u %u\n"
261 "disk_wio %u %u %u %u\n"
262 "disk_rblk %u %u %u %u\n"
263 "disk_wblk %u %u %u %u\n"
268 len
= sprintf(buffer
,
271 "disk_rio %u %u %u %u\n"
272 "disk_wio %u %u %u %u\n"
273 "disk_rblk %u %u %u %u\n"
274 "disk_wblk %u %u %u %u\n"
281 ticks
- (kstat
.cpu_user
+ kstat
.cpu_nice
+ kstat
.cpu_system
),
283 kstat
.dk_drive
[0], kstat
.dk_drive
[1],
284 kstat
.dk_drive
[2], kstat
.dk_drive
[3],
285 kstat
.dk_drive_rio
[0], kstat
.dk_drive_rio
[1],
286 kstat
.dk_drive_rio
[2], kstat
.dk_drive_rio
[3],
287 kstat
.dk_drive_wio
[0], kstat
.dk_drive_wio
[1],
288 kstat
.dk_drive_wio
[2], kstat
.dk_drive_wio
[3],
289 kstat
.dk_drive_rblk
[0], kstat
.dk_drive_rblk
[1],
290 kstat
.dk_drive_rblk
[2], kstat
.dk_drive_rblk
[3],
291 kstat
.dk_drive_wblk
[0], kstat
.dk_drive_wblk
[1],
292 kstat
.dk_drive_wblk
[2], kstat
.dk_drive_wblk
[3],
298 for (i
= 0 ; i
< NR_IRQS
; i
++)
299 len
+= sprintf(buffer
+ len
, " %u", kstat_irqs(i
));
300 len
+= sprintf(buffer
+ len
,
305 xtime
.tv_sec
- jiffies
/ HZ
,
311 static int get_uptime(char * buffer
)
313 unsigned long uptime
;
317 idle
= task
[0]->times
.tms_utime
+ task
[0]->times
.tms_stime
;
319 /* The formula for the fraction parts really is ((t * 100) / HZ) % 100, but
320 that would overflow about every five days at HZ == 100.
321 Therefore the identity a = (a / b) * b + a % b is used so that it is
322 calculated as (((t / HZ) * 100) + ((t % HZ) * 100) / HZ) % 100.
323 The part in front of the '+' always evaluates as 0 (mod 100). All divisions
324 in the above formulas are truncating. For HZ being a power of 10, the
325 calculations simplify to the version in the #else part (if the printf
326 format is adapted to the same number of digits as zeroes in HZ.
329 return sprintf(buffer
,"%lu.%02lu %lu.%02lu\n",
331 (((uptime
% HZ
) * 100) / HZ
) % 100,
333 (((idle
% HZ
) * 100) / HZ
) % 100);
335 return sprintf(buffer
,"%lu.%02lu %lu.%02lu\n",
343 static int get_meminfo(char * buffer
)
350 len
= sprintf(buffer
, " total: used: free: shared: buffers: cached:\n"
351 "Mem: %8lu %8lu %8lu %8lu %8lu %8lu\n"
352 "Swap: %8lu %8lu %8lu\n",
353 i
.totalram
, i
.totalram
-i
.freeram
, i
.freeram
, i
.sharedram
, i
.bufferram
, page_cache_size
*PAGE_SIZE
,
354 i
.totalswap
, i
.totalswap
-i
.freeswap
, i
.freeswap
);
356 * Tagged format, for easy grepping and expansion. The above will go away
357 * eventually, once the tools have been updated.
359 return len
+ sprintf(buffer
+len
,
360 "MemTotal: %8lu kB\n"
362 "MemShared: %8lu kB\n"
365 "SwapTotal: %8lu kB\n"
366 "SwapFree: %8lu kB\n",
371 page_cache_size
<< (PAGE_SHIFT
- 10),
376 static int get_version(char * buffer
)
378 extern char *linux_banner
;
380 strcpy(buffer
, linux_banner
);
381 return strlen(buffer
);
384 static int get_cmdline(char * buffer
)
386 extern char saved_command_line
[];
388 return sprintf(buffer
, "%s\n", saved_command_line
);
392 * Caller must release_mm the mm_struct later.
393 * You don't get any access to init_mm.
395 static struct mm_struct
*get_mm_and_lock(int pid
)
397 struct mm_struct
*mm
= NULL
;
398 struct task_struct
*tsk
;
400 read_lock(&tasklist_lock
);
401 tsk
= find_task_by_pid(pid
);
402 if (tsk
&& tsk
->mm
&& tsk
->mm
!= &init_mm
)
404 read_unlock(&tasklist_lock
);
410 static void release_mm(struct mm_struct
*mm
)
416 static unsigned long get_phys_addr(struct mm_struct
*mm
, unsigned long ptr
)
422 if (ptr
>= TASK_SIZE
)
424 /* Check for NULL pgd .. shouldn't happen! */
426 printk(KERN_DEBUG
"missing pgd for mm %p\n", mm
);
430 page_dir
= pgd_offset(mm
,ptr
);
431 if (pgd_none(*page_dir
))
433 if (pgd_bad(*page_dir
)) {
434 printk("bad page directory entry %08lx\n", pgd_val(*page_dir
));
438 page_middle
= pmd_offset(page_dir
,ptr
);
439 if (pmd_none(*page_middle
))
441 if (pmd_bad(*page_middle
)) {
442 printk("bad page middle entry %08lx\n", pmd_val(*page_middle
));
443 pmd_clear(page_middle
);
446 pte
= *pte_offset(page_middle
,ptr
);
447 if (!pte_present(pte
))
449 return pte_page(pte
) + (ptr
& ~PAGE_MASK
);
452 static int get_array(struct mm_struct
*mm
, unsigned long start
, unsigned long end
, char * buffer
)
455 int size
= 0, result
= 0;
461 addr
= get_phys_addr(mm
, start
);
468 if (size
< PAGE_SIZE
)
474 if (!c
&& start
>= end
)
476 } while (addr
& ~PAGE_MASK
);
481 static int get_env(int pid
, char * buffer
)
483 struct mm_struct
*mm
;
486 mm
= get_mm_and_lock(pid
);
488 res
= get_array(mm
, mm
->env_start
, mm
->env_end
, buffer
);
494 static int get_arg(int pid
, char * buffer
)
496 struct mm_struct
*mm
;
499 mm
= get_mm_and_lock(pid
);
501 res
= get_array(mm
, mm
->arg_start
, mm
->arg_end
, buffer
);
508 * These bracket the sleeping functions..
510 extern void scheduling_functions_start_here(void);
511 extern void scheduling_functions_end_here(void);
512 #define first_sched ((unsigned long) scheduling_functions_start_here)
513 #define last_sched ((unsigned long) scheduling_functions_end_here)
515 static unsigned long get_wchan(struct task_struct
*p
)
517 if (!p
|| p
== current
|| p
->state
== TASK_RUNNING
)
519 #if defined(__i386__)
521 unsigned long ebp
, esp
, eip
;
522 unsigned long stack_page
;
525 stack_page
= (unsigned long)p
;
527 if (!stack_page
|| esp
< stack_page
|| esp
>= 8188+stack_page
)
529 /* include/asm-i386/system.h:switch_to() pushes ebp last. */
530 ebp
= *(unsigned long *) esp
;
532 if (ebp
< stack_page
|| ebp
>= 8188+stack_page
)
534 eip
= *(unsigned long *) (ebp
+4);
535 if (eip
< first_sched
|| eip
>= last_sched
)
537 ebp
= *(unsigned long *) ebp
;
538 } while (count
++ < 16);
540 #elif defined(__alpha__)
542 * This one depends on the frame size of schedule(). Do a
543 * "disass schedule" in gdb to find the frame size. Also, the
544 * code assumes that sleep_on() follows immediately after
545 * interruptible_sleep_on() and that add_timer() follows
546 * immediately after interruptible_sleep(). Ugly, isn't it?
547 * Maybe adding a wchan field to task_struct would be better,
551 unsigned long schedule_frame
;
554 pc
= thread_saved_pc(&p
->tss
);
555 if (pc
>= first_sched
&& pc
< last_sched
) {
556 schedule_frame
= ((unsigned long *)p
->tss
.ksp
)[6];
557 return ((unsigned long *)schedule_frame
)[12];
561 #elif defined(__mc68000__)
563 unsigned long fp
, pc
;
564 unsigned long stack_page
;
567 stack_page
= (unsigned long)p
;
568 fp
= ((struct switch_stack
*)p
->tss
.ksp
)->a6
;
570 if (fp
< stack_page
+sizeof(struct task_struct
) ||
571 fp
>= 8184+stack_page
)
573 pc
= ((unsigned long *)fp
)[1];
574 /* FIXME: This depends on the order of these functions. */
575 if (pc
< first_sched
|| pc
>= last_sched
)
577 fp
= *(unsigned long *) fp
;
578 } while (count
++ < 16);
580 #elif defined(__powerpc__)
581 return (p
->tss
.wchan
);
582 #elif defined (CONFIG_ARM)
584 unsigned long fp
, lr
;
585 unsigned long stack_page
;
588 stack_page
= 4096 + (unsigned long)p
;
589 fp
= get_css_fp (&p
->tss
);
591 if (fp
< stack_page
|| fp
> 4092+stack_page
)
593 lr
= pc_pointer (((unsigned long *)fp
)[-1]);
594 if (lr
< first_sched
|| lr
> last_sched
)
596 fp
= *(unsigned long *) (fp
- 12);
597 } while (count
++ < 16);
599 #elif defined (__sparc__)
601 unsigned long pc
, fp
, bias
= 0;
602 unsigned long task_base
= (unsigned long) p
;
603 struct reg_window
*rw
;
609 fp
= p
->tss
.ksp
+ bias
;
611 /* Bogus frame pointer? */
612 if (fp
< (task_base
+ sizeof(struct task_struct
)) ||
613 fp
>= (task_base
+ (2 * PAGE_SIZE
)))
615 rw
= (struct reg_window
*) fp
;
617 if (pc
< first_sched
|| pc
>= last_sched
)
619 fp
= rw
->ins
[6] + bias
;
620 } while (++count
< 16);
626 #if defined(__i386__)
627 # define KSTK_EIP(tsk) (((unsigned long *)(4096+(unsigned long)(tsk)))[1019])
628 # define KSTK_ESP(tsk) (((unsigned long *)(4096+(unsigned long)(tsk)))[1022])
629 #elif defined(__alpha__)
631 * See arch/alpha/kernel/ptrace.c for details.
633 # define PT_REG(reg) (PAGE_SIZE - sizeof(struct pt_regs) \
634 + (long)&((struct pt_regs *)0)->reg)
635 # define KSTK_EIP(tsk) \
636 (*(unsigned long *)(PT_REG(pc) + PAGE_SIZE + (unsigned long)(tsk)))
637 # define KSTK_ESP(tsk) ((tsk) == current ? rdusp() : (tsk)->tss.usp)
638 #elif defined(CONFIG_ARM)
639 # define KSTK_EIP(tsk) (((unsigned long *)(4096+(unsigned long)(tsk)))[1022])
640 # define KSTK_ESP(tsk) (((unsigned long *)(4096+(unsigned long)(tsk)))[1020])
641 #elif defined(__mc68000__)
642 #define KSTK_EIP(tsk) \
644 unsigned long eip = 0; \
645 if ((tsk)->tss.esp0 > PAGE_SIZE && \
646 MAP_NR((tsk)->tss.esp0) < max_mapnr) \
647 eip = ((struct pt_regs *) (tsk)->tss.esp0)->pc; \
649 #define KSTK_ESP(tsk) ((tsk) == current ? rdusp() : (tsk)->tss.usp)
650 #elif defined(__powerpc__)
651 #define KSTK_EIP(tsk) ((tsk)->tss.regs->nip)
652 #define KSTK_ESP(tsk) ((tsk)->tss.regs->gpr[1])
653 #elif defined (__sparc_v9__)
654 # define KSTK_EIP(tsk) ((tsk)->tss.kregs->tpc)
655 # define KSTK_ESP(tsk) ((tsk)->tss.kregs->u_regs[UREG_FP])
656 #elif defined(__sparc__)
657 # define KSTK_EIP(tsk) ((tsk)->tss.kregs->pc)
658 # define KSTK_ESP(tsk) ((tsk)->tss.kregs->u_regs[UREG_FP])
661 /* Gcc optimizes away "strlen(x)" for constant x */
662 #define ADDBUF(buffer, string) \
663 do { memcpy(buffer, string, strlen(string)); \
664 buffer += strlen(string); } while (0)
666 static inline char * task_name(struct task_struct
*p
, char * buf
)
671 ADDBUF(buf
, "Name:\t");
675 unsigned char c
= *name
;
699 * The task state array is a strange "bitmap" of
700 * reasons to sleep. Thus "running" is zero, and
701 * you can test for combinations of others with
704 static const char *task_state_array
[] = {
705 "R (running)", /* 0 */
706 "S (sleeping)", /* 1 */
707 "D (disk sleep)", /* 2 */
708 "Z (zombie)", /* 4 */
709 "T (stopped)", /* 8 */
710 "W (paging)" /* 16 */
713 static inline const char * get_task_state(struct task_struct
*tsk
)
715 unsigned int state
= tsk
->state
& (TASK_RUNNING
|
717 TASK_UNINTERRUPTIBLE
|
721 const char **p
= &task_state_array
[0];
730 static inline char * task_state(struct task_struct
*p
, char *buffer
)
734 buffer
+= sprintf(buffer
,
738 "Uid:\t%d\t%d\t%d\t%d\n"
739 "Gid:\t%d\t%d\t%d\t%d\n"
742 p
->pid
, p
->p_pptr
->pid
,
743 p
->uid
, p
->euid
, p
->suid
, p
->fsuid
,
744 p
->gid
, p
->egid
, p
->sgid
, p
->fsgid
);
746 for (g
= 0; g
< p
->ngroups
; g
++)
747 buffer
+= sprintf(buffer
, "%d ", p
->groups
[g
]);
749 buffer
+= sprintf(buffer
, "\n");
753 static inline char * task_mem(struct task_struct
*p
, char *buffer
)
755 struct mm_struct
* mm
= p
->mm
;
759 if (mm
!= &init_mm
) {
760 struct vm_area_struct
* vma
;
761 unsigned long data
= 0, stack
= 0;
762 unsigned long exec
= 0, lib
= 0;
765 for (vma
= mm
->mmap
; vma
; vma
= vma
->vm_next
) {
766 unsigned long len
= (vma
->vm_end
- vma
->vm_start
) >> 10;
769 if (vma
->vm_flags
& VM_GROWSDOWN
)
773 if (vma
->vm_flags
& VM_WRITE
)
775 if (vma
->vm_flags
& VM_EXEC
) {
777 if (vma
->vm_flags
& VM_EXECUTABLE
)
783 buffer
+= sprintf(buffer
,
791 mm
->total_vm
<< (PAGE_SHIFT
-10),
792 mm
->locked_vm
<< (PAGE_SHIFT
-10),
793 mm
->rss
<< (PAGE_SHIFT
-10),
800 static void collect_sigign_sigcatch(struct task_struct
*p
, sigset_t
*ign
,
803 struct k_sigaction
*k
;
811 for (i
= 1; i
<= _NSIG
; ++i
, ++k
) {
812 if (k
->sa
.sa_handler
== SIG_IGN
)
814 else if (k
->sa
.sa_handler
!= SIG_DFL
)
820 static inline char * task_sig(struct task_struct
*p
, char *buffer
)
824 buffer
+= sprintf(buffer
, "SigPnd:\t");
825 buffer
= render_sigset_t(&p
->signal
, buffer
);
827 buffer
+= sprintf(buffer
, "SigBlk:\t");
828 buffer
= render_sigset_t(&p
->blocked
, buffer
);
831 collect_sigign_sigcatch(p
, &ign
, &catch);
832 buffer
+= sprintf(buffer
, "SigIgn:\t");
833 buffer
= render_sigset_t(&ign
, buffer
);
835 buffer
+= sprintf(buffer
, "SigCgt:\t"); /* Linux 2.0 uses "SigCgt" */
836 buffer
= render_sigset_t(&catch, buffer
);
842 extern inline char *task_cap(struct task_struct
*p
, char *buffer
)
844 return buffer
+ sprintf(buffer
, "CapInh:\t%016x\n"
847 cap_t(p
->cap_inheritable
),
848 cap_t(p
->cap_permitted
),
849 cap_t(p
->cap_effective
));
852 static struct task_struct
*grab_task(int pid
)
854 struct task_struct
*tsk
= current
;
855 if (pid
!= tsk
->pid
) {
856 read_lock(&tasklist_lock
);
857 tsk
= find_task_by_pid(pid
);
858 if (tsk
&& tsk
->mm
&& tsk
->mm
!= &init_mm
)
860 read_unlock(&tasklist_lock
);
865 static void release_task(struct task_struct
*tsk
)
867 if (tsk
!= current
&& tsk
->mm
&& tsk
->mm
!= &init_mm
)
871 static int get_status(int pid
, char * buffer
)
873 char * orig
= buffer
;
874 struct task_struct
*tsk
;
876 tsk
= grab_task(pid
);
879 buffer
= task_name(tsk
, buffer
);
880 buffer
= task_state(tsk
, buffer
);
881 buffer
= task_mem(tsk
, buffer
);
882 buffer
= task_sig(tsk
, buffer
);
883 buffer
= task_cap(tsk
, buffer
);
885 return buffer
- orig
;
888 static int get_stat(int pid
, char * buffer
)
890 struct task_struct
*tsk
;
891 unsigned long vsize
, eip
, esp
, wchan
;
894 sigset_t sigign
, sigcatch
;
898 tsk
= grab_task(pid
);
901 state
= *get_task_state(tsk
);
902 vsize
= eip
= esp
= 0;
903 if (tsk
->mm
&& tsk
->mm
!= &init_mm
) {
904 struct vm_area_struct
*vma
;
906 down(&tsk
->mm
->mmap_sem
);
907 for (vma
= tsk
->mm
->mmap
; vma
; vma
= vma
->vm_next
) {
908 vsize
+= vma
->vm_end
- vma
->vm_start
;
910 up(&tsk
->mm
->mmap_sem
);
916 wchan
= get_wchan(tsk
);
918 collect_sigign_sigcatch(tsk
, &sigign
, &sigcatch
);
921 tty_pgrp
= tsk
->tty
->pgrp
;
925 /* scale priority and nice values from timeslices to -20..20 */
926 /* to make it look like a "normal" Unix priority/nice value */
927 priority
= tsk
->counter
;
928 priority
= 20 - (priority
* 10 + DEF_PRIORITY
/ 2) / DEF_PRIORITY
;
929 nice
= tsk
->priority
;
930 nice
= 20 - (nice
* 20 + DEF_PRIORITY
/ 2) / DEF_PRIORITY
;
932 res
= sprintf(buffer
,"%d (%s) %c %d %d %d %d %d %lu %lu \
933 %lu %lu %lu %lu %lu %ld %ld %ld %ld %ld %ld %lu %lu %ld %lu %lu %lu %lu %lu \
934 %lu %lu %lu %lu %lu %lu %lu %lu %d\n",
941 tsk
->tty
? kdev_t_to_nr(tsk
->tty
->device
) : 0,
948 tsk
->times
.tms_utime
,
949 tsk
->times
.tms_stime
,
950 tsk
->times
.tms_cutime
,
951 tsk
->times
.tms_cstime
,
958 tsk
->mm
? tsk
->mm
->rss
: 0, /* you might want to shift this left 3 */
959 tsk
->rlim
? tsk
->rlim
[RLIMIT_RSS
].rlim_cur
: 0,
960 tsk
->mm
? tsk
->mm
->start_code
: 0,
961 tsk
->mm
? tsk
->mm
->end_code
: 0,
962 tsk
->mm
? tsk
->mm
->start_stack
: 0,
965 /* The signal information here is obsolete.
966 * It must be decimal for Linux 2.0 compatibility.
967 * Use /proc/#/status for real-time signals.
969 tsk
->signal
.sig
[0] & 0x7fffffffUL
,
970 tsk
->blocked
.sig
[0] & 0x7fffffffUL
,
971 sigign
.sig
[0] & 0x7fffffffUL
,
972 sigcatch
.sig
[0] & 0x7fffffffUL
,
982 static inline void statm_pte_range(pmd_t
* pmd
, unsigned long address
, unsigned long size
,
983 int * pages
, int * shared
, int * dirty
, int * total
)
991 printk("statm_pte_range: bad pmd (%08lx)\n", pmd_val(*pmd
));
995 pte
= pte_offset(pmd
, address
);
996 address
&= ~PMD_MASK
;
997 end
= address
+ size
;
1003 address
+= PAGE_SIZE
;
1008 if (!pte_present(page
))
1011 if (pte_dirty(page
))
1013 if (MAP_NR(pte_page(page
)) >= max_mapnr
)
1015 if (atomic_read(&mem_map
[MAP_NR(pte_page(page
))].count
) > 1)
1017 } while (address
< end
);
1020 static inline void statm_pmd_range(pgd_t
* pgd
, unsigned long address
, unsigned long size
,
1021 int * pages
, int * shared
, int * dirty
, int * total
)
1028 if (pgd_bad(*pgd
)) {
1029 printk("statm_pmd_range: bad pgd (%08lx)\n", pgd_val(*pgd
));
1033 pmd
= pmd_offset(pgd
, address
);
1034 address
&= ~PGDIR_MASK
;
1035 end
= address
+ size
;
1036 if (end
> PGDIR_SIZE
)
1039 statm_pte_range(pmd
, address
, end
- address
, pages
, shared
, dirty
, total
);
1040 address
= (address
+ PMD_SIZE
) & PMD_MASK
;
1042 } while (address
< end
);
1045 static void statm_pgd_range(pgd_t
* pgd
, unsigned long address
, unsigned long end
,
1046 int * pages
, int * shared
, int * dirty
, int * total
)
1048 while (address
< end
) {
1049 statm_pmd_range(pgd
, address
, end
- address
, pages
, shared
, dirty
, total
);
1050 address
= (address
+ PGDIR_SIZE
) & PGDIR_MASK
;
1055 static int get_statm(int pid
, char * buffer
)
1057 int size
=0, resident
=0, share
=0, trs
=0, lrs
=0, drs
=0, dt
=0;
1058 struct mm_struct
*mm
;
1060 mm
= get_mm_and_lock(pid
);
1062 struct vm_area_struct
* vma
= mm
->mmap
;
1065 pgd_t
*pgd
= pgd_offset(mm
, vma
->vm_start
);
1066 int pages
= 0, shared
= 0, dirty
= 0, total
= 0;
1068 statm_pgd_range(pgd
, vma
->vm_start
, vma
->vm_end
, &pages
, &shared
, &dirty
, &total
);
1073 if (vma
->vm_flags
& VM_EXECUTABLE
)
1074 trs
+= pages
; /* text */
1075 else if (vma
->vm_flags
& VM_GROWSDOWN
)
1076 drs
+= pages
; /* stack */
1077 else if (vma
->vm_end
> 0x60000000)
1078 lrs
+= pages
; /* library */
1085 return sprintf(buffer
,"%d %d %d %d %d %d %d\n",
1086 size
, resident
, share
, trs
, lrs
, drs
, dt
);
1090 * The way we support synthetic files > 4K
1091 * - without storing their contents in some buffer and
1092 * - without walking through the entire synthetic file until we reach the
1093 * position of the requested data
1094 * is to cleverly encode the current position in the file's f_pos field.
1095 * There is no requirement that a read() call which returns `count' bytes
1096 * of data increases f_pos by exactly `count'.
1098 * This idea is Linus' one. Bruno implemented it.
1102 * For the /proc/<pid>/maps file, we use fixed length records, each containing
1105 #define MAPS_LINE_LENGTH 4096
1106 #define MAPS_LINE_SHIFT 12
1108 * f_pos = (number of the vma in the task->mm->mmap list) * MAPS_LINE_LENGTH
1109 * + (index into the line)
1111 /* for systems with sizeof(void*) == 4: */
1112 #define MAPS_LINE_FORMAT4 "%08lx-%08lx %s %08lx %s %lu"
1113 #define MAPS_LINE_MAX4 49 /* sum of 8 1 8 1 4 1 8 1 5 1 10 1 */
1115 /* for systems with sizeof(void*) == 8: */
1116 #define MAPS_LINE_FORMAT8 "%016lx-%016lx %s %016lx %s %lu"
1117 #define MAPS_LINE_MAX8 73 /* sum of 16 1 16 1 4 1 16 1 5 1 10 1 */
1119 #define MAPS_LINE_MAX MAPS_LINE_MAX8
1121 /* FIXME: this does not do proper mm locking */
1122 static ssize_t
read_maps (int pid
, struct file
* file
, char * buf
,
1123 size_t count
, loff_t
*ppos
)
1125 struct task_struct
*p
;
1126 struct vm_area_struct
* map
, * next
;
1127 char * destptr
= buf
, * buffer
;
1134 * We might sleep getting the page, so get it first.
1137 buffer
= (char*)__get_free_page(GFP_KERNEL
);
1142 read_lock(&tasklist_lock
);
1143 p
= find_task_by_pid(pid
);
1144 read_unlock(&tasklist_lock
); /* FIXME!! This should be done after the last use */
1148 if (!p
->mm
|| p
->mm
== &init_mm
|| count
== 0)
1151 /* Check whether the mmaps could change if we sleep */
1152 volatile_task
= (p
!= current
|| atomic_read(&p
->mm
->count
) > 1);
1155 lineno
= *ppos
>> MAPS_LINE_SHIFT
;
1156 column
= *ppos
& (MAPS_LINE_LENGTH
-1);
1158 /* quickly go to line lineno */
1159 for (map
= p
->mm
->mmap
, i
= 0; map
&& (i
< lineno
); map
= map
->vm_next
, i
++)
1162 for ( ; map
; map
= next
) {
1163 /* produce the next line */
1165 char str
[5], *cp
= str
;
1169 int maxlen
= (sizeof(void*) == 4) ?
1170 MAPS_LINE_MAX4
: MAPS_LINE_MAX8
;
1174 * Get the next vma now (but it won't be used if we sleep).
1176 next
= map
->vm_next
;
1177 flags
= map
->vm_flags
;
1179 *cp
++ = flags
& VM_READ
? 'r' : '-';
1180 *cp
++ = flags
& VM_WRITE
? 'w' : '-';
1181 *cp
++ = flags
& VM_EXEC
? 'x' : '-';
1182 *cp
++ = flags
& VM_MAYSHARE
? 's' : 'p';
1187 if (map
->vm_file
!= NULL
) {
1188 dev
= map
->vm_file
->f_dentry
->d_inode
->i_dev
;
1189 ino
= map
->vm_file
->f_dentry
->d_inode
->i_ino
;
1190 line
= d_path(map
->vm_file
->f_dentry
, buffer
, PAGE_SIZE
);
1191 buffer
[PAGE_SIZE
-1] = '\n';
1199 sizeof(void*) == 4 ? MAPS_LINE_FORMAT4
: MAPS_LINE_FORMAT8
,
1200 map
->vm_start
, map
->vm_end
, str
, map
->vm_offset
,
1201 kdevname(dev
), ino
);
1204 for(i
= len
; i
< maxlen
; i
++)
1206 len
= buffer
+ PAGE_SIZE
- line
;
1209 if (column
>= len
) {
1210 column
= 0; /* continue with next line at column 0 */
1212 continue; /* we haven't slept */
1218 copy_to_user(destptr
, line
+column
, i
); /* may have slept */
1222 if (column
>= len
) {
1223 column
= 0; /* next time: next line at column 0 */
1231 /* By writing to user space, we might have slept.
1232 * Stop the loop, to avoid a race condition.
1239 *ppos
= (lineno
<< MAPS_LINE_SHIFT
) + column
;
1242 retval
= destptr
- buf
;
1245 free_page((unsigned long)buffer
);
1251 static int get_pidcpu(int pid
, char * buffer
)
1253 struct task_struct
* tsk
;
1256 tsk
= grab_task(pid
);
1260 len
= sprintf(buffer
,
1262 tsk
->times
.tms_utime
,
1263 tsk
->times
.tms_stime
);
1265 for (i
= 0 ; i
< smp_num_cpus
; i
++)
1266 len
+= sprintf(buffer
+ len
, "cpu%d %lu %lu\n",
1268 tsk
->per_cpu_utime
[cpu_logical_map(i
)],
1269 tsk
->per_cpu_stime
[cpu_logical_map(i
)]);
1276 #ifdef CONFIG_MODULES
1277 extern int get_module_list(char *);
1278 extern int get_ksyms_list(char *, char **, off_t
, int);
1280 extern int get_device_list(char *);
1281 extern int get_partition_list(char *);
1282 extern int get_filesystem_list(char *);
1283 extern int get_filesystem_info( char * );
1284 extern int get_irq_list(char *);
1285 extern int get_dma_list(char *);
1286 extern int get_cpuinfo(char *);
1287 extern int get_pci_list(char *);
1288 extern int get_md_status (char *);
1289 extern int get_rtc_status (char *);
1290 extern int get_locks_status (char *, char **, off_t
, int);
1291 extern int get_swaparea_info (char *);
1292 extern int get_hardware_list(char *);
1293 extern int get_stram_list(char *);
1295 static long get_root_array(char * page
, int type
, char **start
,
1296 off_t offset
, unsigned long length
)
1300 return get_loadavg(page
);
1303 return get_uptime(page
);
1306 return get_meminfo(page
);
1308 #ifdef CONFIG_PCI_OLD_PROC
1310 return get_pci_list(page
);
1315 return get_nubus_list(page
);
1319 return get_cpuinfo(page
);
1322 return get_version(page
);
1324 #ifdef CONFIG_DEBUG_MALLOC
1326 return get_malloc(page
);
1329 #ifdef CONFIG_MODULES
1331 return get_module_list(page
);
1334 return get_ksyms_list(page
, start
, offset
, length
);
1338 return get_kstat(page
);
1341 return get_slabinfo(page
);
1344 return get_device_list(page
);
1346 case PROC_PARTITIONS
:
1347 return get_partition_list(page
);
1349 case PROC_INTERRUPTS
:
1350 return get_irq_list(page
);
1352 case PROC_FILESYSTEMS
:
1353 return get_filesystem_list(page
);
1356 return get_dma_list(page
);
1359 return get_ioport_list(page
);
1360 #ifdef CONFIG_BLK_DEV_MD
1362 return get_md_status(page
);
1365 return get_cmdline(page
);
1368 return get_filesystem_info( page
);
1371 return get_swaparea_info(page
);
1374 return get_rtc_status(page
);
1377 return get_locks_status(page
, start
, offset
, length
);
1378 #ifdef CONFIG_PROC_HARDWARE
1380 return get_hardware_list(page
);
1382 #ifdef CONFIG_STRAM_PROC
1384 return get_stram_list(page
);
1390 static int process_unauthorized(int type
, int pid
)
1392 struct task_struct
*p
;
1393 uid_t euid
=0; /* Save the euid keep the lock short */
1395 read_lock(&tasklist_lock
);
1398 * Grab the lock, find the task, save the uid and
1399 * check it has an mm still (ie its not dead)
1401 p
= find_task_by_pid(pid
);
1405 if(!p
->mm
) /* Scooby scooby doo where are you ? */
1409 read_unlock(&tasklist_lock
);
1416 case PROC_PID_STATUS
:
1417 case PROC_PID_STATM
:
1420 case PROC_PID_CMDLINE
:
1424 if(capable(CAP_DAC_OVERRIDE
) || current
->fsuid
== euid
)
1430 static int get_process_array(char * page
, int pid
, int type
)
1433 case PROC_PID_STATUS
:
1434 return get_status(pid
, page
);
1435 case PROC_PID_ENVIRON
:
1436 return get_env(pid
, page
);
1437 case PROC_PID_CMDLINE
:
1438 return get_arg(pid
, page
);
1440 return get_stat(pid
, page
);
1441 case PROC_PID_STATM
:
1442 return get_statm(pid
, page
);
1445 return get_pidcpu(pid
, page
);
1452 static inline int fill_array(char * page
, int pid
, int type
, char **start
, off_t offset
, int length
)
1455 return get_process_array(page
, pid
, type
);
1456 return get_root_array(page
, type
, start
, offset
, length
);
1459 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
1461 static ssize_t
array_read(struct file
* file
, char * buf
,
1462 size_t count
, loff_t
*ppos
)
1464 struct inode
* inode
= file
->f_dentry
->d_inode
;
1469 unsigned int type
, pid
;
1470 struct proc_dir_entry
*dp
;
1473 if (count
> PROC_BLOCK_SIZE
)
1474 count
= PROC_BLOCK_SIZE
;
1475 if (!(page
= __get_free_page(GFP_KERNEL
)))
1477 type
= inode
->i_ino
;
1481 dp
= (struct proc_dir_entry
*) inode
->u
.generic_ip
;
1483 if (pid
&& process_unauthorized(type
, pid
))
1490 length
= dp
->get_info((char *)page
, &start
, *ppos
,
1493 length
= fill_array((char *) page
, pid
, type
,
1494 &start
, *ppos
, count
);
1499 if (start
!= NULL
) {
1502 /* We have had block-adjusting processing! */
1503 err
= copy_to_user(buf
, start
, length
);
1507 /* Static 4kB (or whatever) block capacity */
1508 if (*ppos
>= length
) {
1512 if (count
+ *ppos
> length
)
1513 count
= length
- *ppos
;
1514 end
= count
+ *ppos
;
1515 err
= copy_to_user(buf
, (char *) page
+ *ppos
, count
);
1519 return err
? -EFAULT
: count
;
1522 static struct file_operations proc_array_operations
= {
1523 NULL
, /* array_lseek */
1525 NULL
, /* array_write */
1526 NULL
, /* array_readdir */
1527 NULL
, /* array_poll */
1528 NULL
, /* array_ioctl */
1530 NULL
, /* no special open code */
1532 NULL
, /* no special release code */
1533 NULL
/* can't fsync */
1536 struct inode_operations proc_array_inode_operations
= {
1537 &proc_array_operations
, /* default base directory file-ops */
1547 NULL
, /* readlink */
1548 NULL
, /* follow_link */
1549 NULL
, /* readpage */
1550 NULL
, /* writepage */
1552 NULL
, /* truncate */
1553 NULL
/* permission */
1556 static ssize_t
arraylong_read(struct file
* file
, char * buf
,
1557 size_t count
, loff_t
*ppos
)
1559 struct inode
* inode
= file
->f_dentry
->d_inode
;
1560 unsigned int pid
= inode
->i_ino
>> 16;
1561 unsigned int type
= inode
->i_ino
& 0x0000ffff;
1565 return read_maps(pid
, file
, buf
, count
, ppos
);
1570 static struct file_operations proc_arraylong_operations
= {
1571 NULL
, /* array_lseek */
1573 NULL
, /* array_write */
1574 NULL
, /* array_readdir */
1575 NULL
, /* array_poll */
1576 NULL
, /* array_ioctl */
1578 NULL
, /* no special open code */
1580 NULL
, /* no special release code */
1581 NULL
/* can't fsync */
1584 struct inode_operations proc_arraylong_inode_operations
= {
1585 &proc_arraylong_operations
, /* default base directory file-ops */
1595 NULL
, /* readlink */
1596 NULL
, /* follow_link */
1597 NULL
, /* readpage */
1598 NULL
, /* writepage */
1600 NULL
, /* truncate */
1601 NULL
/* permission */