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 * Al Viro : safe handling of mm_struct
47 * Gerhard Wichert : added BIGMEM support
48 * Siemens AG <Gerhard.Wichert@pdb.siemens.de>
51 #include <linux/types.h>
52 #include <linux/errno.h>
53 #include <linux/sched.h>
54 #include <linux/kernel.h>
55 #include <linux/kernel_stat.h>
56 #include <linux/tty.h>
57 #include <linux/user.h>
58 #include <linux/a.out.h>
59 #include <linux/string.h>
60 #include <linux/mman.h>
61 #include <linux/proc_fs.h>
62 #include <linux/ioport.h>
63 #include <linux/config.h>
65 #include <linux/pagemap.h>
66 #include <linux/swap.h>
67 #include <linux/slab.h>
68 #include <linux/smp.h>
69 #include <linux/signal.h>
71 #include <asm/uaccess.h>
72 #include <asm/pgtable.h>
76 static int open_kcore(struct inode
* inode
, struct file
* filp
)
78 return capable(CAP_SYS_RAWIO
) ? 0 : -EPERM
;
81 extern ssize_t
read_kcore(struct file
*, char *, size_t, loff_t
*);
83 static struct file_operations proc_kcore_operations
= {
94 struct inode_operations proc_kcore_inode_operations
= {
95 &proc_kcore_operations
,
99 * This function accesses profiling information. The returned data is
100 * binary: the sampling step and the actual contents of the profile
101 * buffer. Use of the program readprofile is recommended in order to
102 * get meaningful info out of these data.
104 static ssize_t
read_profile(struct file
*file
, char *buf
,
105 size_t count
, loff_t
*ppos
)
107 unsigned long p
= *ppos
;
110 unsigned int sample_step
= 1 << prof_shift
;
112 if (p
>= (prof_len
+1)*sizeof(unsigned int))
114 if (count
> (prof_len
+1)*sizeof(unsigned int) - p
)
115 count
= (prof_len
+1)*sizeof(unsigned int) - p
;
118 while (p
< sizeof(unsigned int) && count
> 0) {
119 put_user(*((char *)(&sample_step
)+p
),buf
);
120 buf
++; p
++; count
--; read
++;
122 pnt
= (char *)prof_buffer
+ p
- sizeof(unsigned int);
123 copy_to_user(buf
,(void *)pnt
,count
);
130 * Writing to /proc/profile resets the counters
132 * Writing a 'profiling multiplier' value into it also re-sets the profiling
133 * interrupt frequency, on architectures that support this.
135 static ssize_t
write_profile(struct file
* file
, const char * buf
,
136 size_t count
, loff_t
*ppos
)
139 extern int setup_profiling_timer (unsigned int multiplier
);
141 if (count
==sizeof(int)) {
142 unsigned int multiplier
;
144 if (copy_from_user(&multiplier
, buf
, sizeof(int)))
147 if (setup_profiling_timer(multiplier
))
152 memset(prof_buffer
, 0, prof_len
* sizeof(*prof_buffer
));
156 static struct file_operations proc_profile_operations
= {
162 struct inode_operations proc_profile_inode_operations
= {
163 &proc_profile_operations
,
166 static struct page
* get_phys_addr(struct mm_struct
* mm
, unsigned long ptr
)
172 if (ptr
>= TASK_SIZE
)
174 pgd
= pgd_offset(mm
,ptr
);
182 pmd
= pmd_offset(pgd
,ptr
);
190 pte
= *pte_offset(pmd
,ptr
);
191 if (!pte_present(pte
))
193 return pte_page(pte
);
196 static int get_array(struct mm_struct
*mm
, unsigned long start
, unsigned long end
, char * buffer
)
200 int size
= 0, result
= 0;
206 page
= get_phys_addr(mm
, start
);
209 kaddr
= kmap(page
, KM_READ
) + (start
& ~PAGE_MASK
);
214 if (size
< PAGE_SIZE
)
217 kunmap(kaddr
, KM_READ
);
222 if (!c
&& start
>= end
) {
223 kunmap(kaddr
, KM_READ
);
226 } while (kaddr
& ~PAGE_MASK
);
227 kunmap(kaddr
, KM_READ
);
232 static struct mm_struct
*get_mm(int pid
)
234 struct task_struct
*p
;
235 struct mm_struct
*mm
= NULL
;
237 read_lock(&tasklist_lock
);
238 p
= find_task_by_pid(pid
);
242 atomic_inc(&mm
->mm_users
);
243 read_unlock(&tasklist_lock
);
248 static int get_env(int pid
, char * buffer
)
250 struct mm_struct
*mm
= get_mm(pid
);
253 res
= get_array(mm
, mm
->env_start
, mm
->env_end
, buffer
);
259 static int get_arg(int pid
, char * buffer
)
261 struct mm_struct
*mm
= get_mm(pid
);
264 res
= get_array(mm
, mm
->arg_start
, mm
->arg_end
, buffer
);
271 * These bracket the sleeping functions..
273 extern void scheduling_functions_start_here(void);
274 extern void scheduling_functions_end_here(void);
275 #define first_sched ((unsigned long) scheduling_functions_start_here)
276 #define last_sched ((unsigned long) scheduling_functions_end_here)
278 static unsigned long get_wchan(struct task_struct
*p
)
280 if (!p
|| p
== current
|| p
->state
== TASK_RUNNING
)
282 #if defined(__i386__)
284 unsigned long ebp
, esp
, eip
;
285 unsigned long stack_page
;
288 stack_page
= (unsigned long)p
;
290 if (!stack_page
|| esp
< stack_page
|| esp
> 8188+stack_page
)
292 /* include/asm-i386/system.h:switch_to() pushes ebp last. */
293 ebp
= *(unsigned long *) esp
;
295 if (ebp
< stack_page
|| ebp
> 8184+stack_page
)
297 eip
= *(unsigned long *) (ebp
+4);
298 if (eip
< first_sched
|| eip
>= last_sched
)
300 ebp
= *(unsigned long *) ebp
;
301 } while (count
++ < 16);
303 #elif defined(__alpha__)
305 * This one depends on the frame size of schedule(). Do a
306 * "disass schedule" in gdb to find the frame size. Also, the
307 * code assumes that sleep_on() follows immediately after
308 * interruptible_sleep_on() and that add_timer() follows
309 * immediately after interruptible_sleep(). Ugly, isn't it?
310 * Maybe adding a wchan field to task_struct would be better,
314 unsigned long schedule_frame
;
317 pc
= thread_saved_pc(&p
->thread
);
318 if (pc
>= first_sched
&& pc
< last_sched
) {
319 schedule_frame
= ((unsigned long *)p
->thread
.ksp
)[6];
320 return ((unsigned long *)schedule_frame
)[12];
324 #elif defined(__mips__)
326 * The same comment as on the Alpha applies here, too ...
329 unsigned long schedule_frame
;
332 pc
= thread_saved_pc(&p
->tss
);
333 if (pc
>= (unsigned long) interruptible_sleep_on
&& pc
< (unsigned long) add_timer
) {
334 schedule_frame
= ((unsigned long *)(long)p
->tss
.reg30
)[16];
335 return (unsigned long)((unsigned long *)schedule_frame
)[11];
339 #elif defined(__mc68000__)
341 unsigned long fp
, pc
;
342 unsigned long stack_page
;
345 stack_page
= (unsigned long)p
;
346 fp
= ((struct switch_stack
*)p
->thread
.ksp
)->a6
;
348 if (fp
< stack_page
+sizeof(struct task_struct
) ||
349 fp
>= 8184+stack_page
)
351 pc
= ((unsigned long *)fp
)[1];
352 /* FIXME: This depends on the order of these functions. */
353 if (pc
< first_sched
|| pc
>= last_sched
)
355 fp
= *(unsigned long *) fp
;
356 } while (count
++ < 16);
358 #elif defined(__powerpc__)
360 unsigned long ip
, sp
;
361 unsigned long stack_page
= (unsigned long) p
;
366 sp
= *(unsigned long *)sp
;
367 if (sp
< stack_page
|| sp
>= stack_page
+ 8188)
370 ip
= *(unsigned long *)(sp
+ 4);
371 if (ip
< first_sched
|| ip
>= last_sched
)
374 } while (count
++ < 16);
376 #elif defined(__arm__)
378 unsigned long fp
, lr
;
379 unsigned long stack_page
;
382 stack_page
= 4096 + (unsigned long)p
;
383 fp
= get_css_fp(&p
->thread
);
385 if (fp
< stack_page
|| fp
> 4092+stack_page
)
387 lr
= pc_pointer (((unsigned long *)fp
)[-1]);
388 if (lr
< first_sched
|| lr
> last_sched
)
390 fp
= *(unsigned long *) (fp
- 12);
391 } while (count
++ < 16);
393 #elif defined (__sparc__)
395 unsigned long pc
, fp
, bias
= 0;
396 unsigned long task_base
= (unsigned long) p
;
397 struct reg_window
*rw
;
403 fp
= p
->thread
.ksp
+ bias
;
405 /* Bogus frame pointer? */
406 if (fp
< (task_base
+ sizeof(struct task_struct
)) ||
407 fp
>= (task_base
+ (2 * PAGE_SIZE
)))
409 rw
= (struct reg_window
*) fp
;
411 if (pc
< first_sched
|| pc
>= last_sched
)
413 fp
= rw
->ins
[6] + bias
;
414 } while (++count
< 16);
421 #if defined(__i386__)
422 # define KSTK_EIP(tsk) (((unsigned long *)(4096+(unsigned long)(tsk)))[1019])
423 # define KSTK_ESP(tsk) (((unsigned long *)(4096+(unsigned long)(tsk)))[1022])
424 #elif defined(__alpha__)
426 * See arch/alpha/kernel/ptrace.c for details.
428 # define PT_REG(reg) (PAGE_SIZE - sizeof(struct pt_regs) \
429 + (long)&((struct pt_regs *)0)->reg)
430 # define KSTK_EIP(tsk) \
431 (*(unsigned long *)(PT_REG(pc) + PAGE_SIZE + (unsigned long)(tsk)))
432 # define KSTK_ESP(tsk) ((tsk) == current ? rdusp() : (tsk)->thread.usp)
433 #elif defined(__arm__)
434 # ifdef CONFIG_CPU_26
435 # define KSTK_EIP(tsk) (((unsigned long *)(4096+(unsigned long)(tsk)))[1022])
436 # define KSTK_ESP(tsk) (((unsigned long *)(4096+(unsigned long)(tsk)))[1020])
438 # define KSTK_EIP(tsk) (((unsigned long *)(4096+(unsigned long)(tsk)))[1021])
439 # define KSTK_ESP(tsk) (((unsigned long *)(4096+(unsigned long)(tsk)))[1019])
441 #elif defined(__mc68000__)
442 #define KSTK_EIP(tsk) \
444 unsigned long eip = 0; \
445 if ((tsk)->thread.esp0 > PAGE_SIZE && \
446 MAP_NR((tsk)->thread.esp0) < max_mapnr) \
447 eip = ((struct pt_regs *) (tsk)->thread.esp0)->pc; \
449 #define KSTK_ESP(tsk) ((tsk) == current ? rdusp() : (tsk)->thread.usp)
450 #elif defined(__powerpc__)
451 #define KSTK_EIP(tsk) ((tsk)->thread.regs->nip)
452 #define KSTK_ESP(tsk) ((tsk)->thread.regs->gpr[1])
453 #elif defined (__sparc_v9__)
454 # define KSTK_EIP(tsk) ((tsk)->thread.kregs->tpc)
455 # define KSTK_ESP(tsk) ((tsk)->thread.kregs->u_regs[UREG_FP])
456 #elif defined(__sparc__)
457 # define KSTK_EIP(tsk) ((tsk)->thread.kregs->pc)
458 # define KSTK_ESP(tsk) ((tsk)->thread.kregs->u_regs[UREG_FP])
459 #elif defined(__mips__)
460 # define PT_REG(reg) ((long)&((struct pt_regs *)0)->reg \
461 - sizeof(struct pt_regs))
462 #define KSTK_TOS(tsk) ((unsigned long)(tsk) + KERNEL_STACK_SIZE - 32)
463 # define KSTK_EIP(tsk) (*(unsigned long *)(KSTK_TOS(tsk) + PT_REG(cp0_epc)))
464 # define KSTK_ESP(tsk) (*(unsigned long *)(KSTK_TOS(tsk) + PT_REG(regs[29])))
465 #elif defined(__sh__)
466 # define KSTK_EIP(tsk) ((tsk)->thread.pc)
467 # define KSTK_ESP(tsk) ((tsk)->thread.sp)
470 /* Gcc optimizes away "strlen(x)" for constant x */
471 #define ADDBUF(buffer, string) \
472 do { memcpy(buffer, string, strlen(string)); \
473 buffer += strlen(string); } while (0)
475 static inline char * task_name(struct task_struct
*p
, char * buf
)
480 ADDBUF(buf
, "Name:\t");
484 unsigned char c
= *name
;
508 * The task state array is a strange "bitmap" of
509 * reasons to sleep. Thus "running" is zero, and
510 * you can test for combinations of others with
513 static const char *task_state_array
[] = {
514 "R (running)", /* 0 */
515 "S (sleeping)", /* 1 */
516 "D (disk sleep)", /* 2 */
517 "Z (zombie)", /* 4 */
518 "T (stopped)", /* 8 */
519 "W (paging)" /* 16 */
522 static inline const char * get_task_state(struct task_struct
*tsk
)
524 unsigned int state
= tsk
->state
& (TASK_RUNNING
|
526 TASK_UNINTERRUPTIBLE
|
530 const char **p
= &task_state_array
[0];
539 static inline char * task_state(struct task_struct
*p
, char *buffer
)
543 buffer
+= sprintf(buffer
,
547 "Uid:\t%d\t%d\t%d\t%d\n"
548 "Gid:\t%d\t%d\t%d\t%d\n"
552 p
->pid
, p
->p_pptr
->pid
,
553 p
->uid
, p
->euid
, p
->suid
, p
->fsuid
,
554 p
->gid
, p
->egid
, p
->sgid
, p
->fsgid
,
555 p
->files
? p
->files
->max_fds
: 0);
557 for (g
= 0; g
< p
->ngroups
; g
++)
558 buffer
+= sprintf(buffer
, "%d ", p
->groups
[g
]);
560 buffer
+= sprintf(buffer
, "\n");
564 static inline char * task_mem(struct mm_struct
*mm
, char *buffer
)
566 struct vm_area_struct
* vma
;
567 unsigned long data
= 0, stack
= 0;
568 unsigned long exec
= 0, lib
= 0;
571 for (vma
= mm
->mmap
; vma
; vma
= vma
->vm_next
) {
572 unsigned long len
= (vma
->vm_end
- vma
->vm_start
) >> 10;
575 if (vma
->vm_flags
& VM_GROWSDOWN
)
579 if (vma
->vm_flags
& VM_WRITE
)
581 if (vma
->vm_flags
& VM_EXEC
) {
583 if (vma
->vm_flags
& VM_EXECUTABLE
)
588 buffer
+= sprintf(buffer
,
596 mm
->total_vm
<< (PAGE_SHIFT
-10),
597 mm
->locked_vm
<< (PAGE_SHIFT
-10),
598 mm
->rss
<< (PAGE_SHIFT
-10),
605 static void collect_sigign_sigcatch(struct task_struct
*p
, sigset_t
*ign
,
608 struct k_sigaction
*k
;
616 for (i
= 1; i
<= _NSIG
; ++i
, ++k
) {
617 if (k
->sa
.sa_handler
== SIG_IGN
)
619 else if (k
->sa
.sa_handler
!= SIG_DFL
)
625 static inline char * task_sig(struct task_struct
*p
, char *buffer
)
629 buffer
+= sprintf(buffer
, "SigPnd:\t");
630 buffer
= render_sigset_t(&p
->signal
, buffer
);
632 buffer
+= sprintf(buffer
, "SigBlk:\t");
633 buffer
= render_sigset_t(&p
->blocked
, buffer
);
636 collect_sigign_sigcatch(p
, &ign
, &catch);
637 buffer
+= sprintf(buffer
, "SigIgn:\t");
638 buffer
= render_sigset_t(&ign
, buffer
);
640 buffer
+= sprintf(buffer
, "SigCgt:\t"); /* Linux 2.0 uses "SigCgt" */
641 buffer
= render_sigset_t(&catch, buffer
);
647 extern inline char *task_cap(struct task_struct
*p
, char *buffer
)
649 return buffer
+ sprintf(buffer
, "CapInh:\t%016x\n"
652 cap_t(p
->cap_inheritable
),
653 cap_t(p
->cap_permitted
),
654 cap_t(p
->cap_effective
));
658 static int get_status(int pid
, char * buffer
)
660 char * orig
= buffer
;
661 struct task_struct
*tsk
;
662 struct mm_struct
*mm
= NULL
;
664 read_lock(&tasklist_lock
);
665 tsk
= find_task_by_pid(pid
);
669 atomic_inc(&mm
->mm_users
);
670 read_unlock(&tasklist_lock
); /* FIXME!! This should be done after the last use */
673 buffer
= task_name(tsk
, buffer
);
674 buffer
= task_state(tsk
, buffer
);
676 buffer
= task_mem(mm
, buffer
);
677 buffer
= task_sig(tsk
, buffer
);
678 buffer
= task_cap(tsk
, buffer
);
681 return buffer
- orig
;
684 static int get_stat(int pid
, char * buffer
)
686 struct task_struct
*tsk
;
687 struct mm_struct
*mm
= NULL
;
688 unsigned long vsize
, eip
, esp
, wchan
;
691 sigset_t sigign
, sigcatch
;
695 read_lock(&tasklist_lock
);
696 tsk
= find_task_by_pid(pid
);
700 atomic_inc(&mm
->mm_users
);
701 read_unlock(&tasklist_lock
); /* FIXME!! This should be done after the last use */
704 state
= *get_task_state(tsk
);
705 vsize
= eip
= esp
= 0;
707 struct vm_area_struct
*vma
;
711 vsize
+= vma
->vm_end
- vma
->vm_start
;
719 wchan
= get_wchan(tsk
);
721 collect_sigign_sigcatch(tsk
, &sigign
, &sigcatch
);
724 tty_pgrp
= tsk
->tty
->pgrp
;
728 /* scale priority and nice values from timeslices to -20..20 */
729 /* to make it look like a "normal" Unix priority/nice value */
730 priority
= tsk
->counter
;
731 priority
= 20 - (priority
* 10 + DEF_PRIORITY
/ 2) / DEF_PRIORITY
;
732 nice
= tsk
->priority
;
733 nice
= 20 - (nice
* 20 + DEF_PRIORITY
/ 2) / DEF_PRIORITY
;
735 res
= sprintf(buffer
,"%d (%s) %c %d %d %d %d %d %lu %lu \
736 %lu %lu %lu %lu %lu %ld %ld %ld %ld %ld %ld %lu %lu %ld %lu %lu %lu %lu %lu \
737 %lu %lu %lu %lu %lu %lu %lu %lu %d %d\n",
744 tsk
->tty
? kdev_t_to_nr(tsk
->tty
->device
) : 0,
751 tsk
->times
.tms_utime
,
752 tsk
->times
.tms_stime
,
753 tsk
->times
.tms_cutime
,
754 tsk
->times
.tms_cstime
,
761 mm
? mm
->rss
: 0, /* you might want to shift this left 3 */
762 tsk
->rlim
? tsk
->rlim
[RLIMIT_RSS
].rlim_cur
: 0,
763 mm
? mm
->start_code
: 0,
764 mm
? mm
->end_code
: 0,
765 mm
? mm
->start_stack
: 0,
768 /* The signal information here is obsolete.
769 * It must be decimal for Linux 2.0 compatibility.
770 * Use /proc/#/status for real-time signals.
772 tsk
->signal
.sig
[0] & 0x7fffffffUL
,
773 tsk
->blocked
.sig
[0] & 0x7fffffffUL
,
774 sigign
.sig
[0] & 0x7fffffffUL
,
775 sigcatch
.sig
[0] & 0x7fffffffUL
,
786 static inline void statm_pte_range(pmd_t
* pmd
, unsigned long address
, unsigned long size
,
787 int * pages
, int * shared
, int * dirty
, int * total
)
799 pte
= pte_offset(pmd
, address
);
800 address
&= ~PMD_MASK
;
801 end
= address
+ size
;
807 address
+= PAGE_SIZE
;
812 if (!pte_present(page
))
817 if (MAP_NR(pte_page(page
)) >= max_mapnr
)
819 if (page_count(mem_map
+ MAP_NR(pte_page(page
))) > 1)
821 } while (address
< end
);
824 static inline void statm_pmd_range(pgd_t
* pgd
, unsigned long address
, unsigned long size
,
825 int * pages
, int * shared
, int * dirty
, int * total
)
837 pmd
= pmd_offset(pgd
, address
);
838 address
&= ~PGDIR_MASK
;
839 end
= address
+ size
;
840 if (end
> PGDIR_SIZE
)
843 statm_pte_range(pmd
, address
, end
- address
, pages
, shared
, dirty
, total
);
844 address
= (address
+ PMD_SIZE
) & PMD_MASK
;
846 } while (address
< end
);
849 static void statm_pgd_range(pgd_t
* pgd
, unsigned long address
, unsigned long end
,
850 int * pages
, int * shared
, int * dirty
, int * total
)
852 while (address
< end
) {
853 statm_pmd_range(pgd
, address
, end
- address
, pages
, shared
, dirty
, total
);
854 address
= (address
+ PGDIR_SIZE
) & PGDIR_MASK
;
859 static int get_statm(int pid
, char * buffer
)
861 struct mm_struct
*mm
= get_mm(pid
);
862 int size
=0, resident
=0, share
=0, trs
=0, lrs
=0, drs
=0, dt
=0;
865 struct vm_area_struct
* vma
;
869 pgd_t
*pgd
= pgd_offset(mm
, vma
->vm_start
);
870 int pages
= 0, shared
= 0, dirty
= 0, total
= 0;
872 statm_pgd_range(pgd
, vma
->vm_start
, vma
->vm_end
, &pages
, &shared
, &dirty
, &total
);
877 if (vma
->vm_flags
& VM_EXECUTABLE
)
878 trs
+= pages
; /* text */
879 else if (vma
->vm_flags
& VM_GROWSDOWN
)
880 drs
+= pages
; /* stack */
881 else if (vma
->vm_end
> 0x60000000)
882 lrs
+= pages
; /* library */
890 return sprintf(buffer
,"%d %d %d %d %d %d %d\n",
891 size
, resident
, share
, trs
, lrs
, drs
, dt
);
895 * The way we support synthetic files > 4K
896 * - without storing their contents in some buffer and
897 * - without walking through the entire synthetic file until we reach the
898 * position of the requested data
899 * is to cleverly encode the current position in the file's f_pos field.
900 * There is no requirement that a read() call which returns `count' bytes
901 * of data increases f_pos by exactly `count'.
903 * This idea is Linus' one. Bruno implemented it.
907 * For the /proc/<pid>/maps file, we use fixed length records, each containing
910 #define MAPS_LINE_LENGTH 4096
911 #define MAPS_LINE_SHIFT 12
913 * f_pos = (number of the vma in the task->mm->mmap list) * MAPS_LINE_LENGTH
914 * + (index into the line)
916 /* for systems with sizeof(void*) == 4: */
917 #define MAPS_LINE_FORMAT4 "%08lx-%08lx %s %08lx %s %lu"
918 #define MAPS_LINE_MAX4 49 /* sum of 8 1 8 1 4 1 8 1 5 1 10 1 */
920 /* for systems with sizeof(void*) == 8: */
921 #define MAPS_LINE_FORMAT8 "%016lx-%016lx %s %016lx %s %lu"
922 #define MAPS_LINE_MAX8 73 /* sum of 16 1 16 1 4 1 16 1 5 1 10 1 */
924 #define MAPS_LINE_MAX MAPS_LINE_MAX8
927 static ssize_t
read_maps (int pid
, struct file
* file
, char * buf
,
928 size_t count
, loff_t
*ppos
)
930 struct task_struct
*p
;
931 struct vm_area_struct
* map
, * next
;
932 char * destptr
= buf
, * buffer
;
939 * We might sleep getting the page, so get it first.
942 buffer
= (char*)__get_free_page(GFP_KERNEL
);
947 read_lock(&tasklist_lock
);
948 p
= find_task_by_pid(pid
);
949 read_unlock(&tasklist_lock
); /* FIXME!! This should be done after the last use */
953 if (!p
->mm
|| count
== 0)
956 /* Check whether the mmaps could change if we sleep */
957 volatile_task
= (p
!= current
|| atomic_read(&p
->mm
->mm_users
) > 1);
960 lineno
= *ppos
>> MAPS_LINE_SHIFT
;
961 column
= *ppos
& (MAPS_LINE_LENGTH
-1);
963 /* quickly go to line lineno */
964 for (map
= p
->mm
->mmap
, i
= 0; map
&& (i
< lineno
); map
= map
->vm_next
, i
++)
967 for ( ; map
; map
= next
) {
968 /* produce the next line */
970 char str
[5], *cp
= str
;
974 int maxlen
= (sizeof(void*) == 4) ?
975 MAPS_LINE_MAX4
: MAPS_LINE_MAX8
;
979 * Get the next vma now (but it won't be used if we sleep).
982 flags
= map
->vm_flags
;
984 *cp
++ = flags
& VM_READ
? 'r' : '-';
985 *cp
++ = flags
& VM_WRITE
? 'w' : '-';
986 *cp
++ = flags
& VM_EXEC
? 'x' : '-';
987 *cp
++ = flags
& VM_MAYSHARE
? 's' : 'p';
992 if (map
->vm_file
!= NULL
) {
993 dev
= map
->vm_file
->f_dentry
->d_inode
->i_dev
;
994 ino
= map
->vm_file
->f_dentry
->d_inode
->i_ino
;
995 line
= d_path(map
->vm_file
->f_dentry
, buffer
, PAGE_SIZE
);
996 buffer
[PAGE_SIZE
-1] = '\n';
1004 sizeof(void*) == 4 ? MAPS_LINE_FORMAT4
: MAPS_LINE_FORMAT8
,
1005 map
->vm_start
, map
->vm_end
, str
, map
->vm_pgoff
<< PAGE_SHIFT
,
1006 kdevname(dev
), ino
);
1009 for(i
= len
; i
< maxlen
; i
++)
1011 len
= buffer
+ PAGE_SIZE
- line
;
1014 if (column
>= len
) {
1015 column
= 0; /* continue with next line at column 0 */
1017 continue; /* we haven't slept */
1023 copy_to_user(destptr
, line
+column
, i
); /* may have slept */
1027 if (column
>= len
) {
1028 column
= 0; /* next time: next line at column 0 */
1036 /* By writing to user space, we might have slept.
1037 * Stop the loop, to avoid a race condition.
1044 *ppos
= (lineno
<< MAPS_LINE_SHIFT
) + column
;
1047 retval
= destptr
- buf
;
1050 free_page((unsigned long)buffer
);
1056 static int get_pidcpu(int pid
, char * buffer
)
1058 struct task_struct
* tsk
= current
;
1061 read_lock(&tasklist_lock
);
1062 if (pid
!= tsk
->pid
)
1063 tsk
= find_task_by_pid(pid
);
1064 read_unlock(&tasklist_lock
); /* FIXME!! This should be done after the last use */
1069 len
= sprintf(buffer
,
1071 tsk
->times
.tms_utime
,
1072 tsk
->times
.tms_stime
);
1074 for (i
= 0 ; i
< smp_num_cpus
; i
++)
1075 len
+= sprintf(buffer
+ len
, "cpu%d %lu %lu\n",
1077 tsk
->per_cpu_utime
[cpu_logical_map(i
)],
1078 tsk
->per_cpu_stime
[cpu_logical_map(i
)]);
1084 static int process_unauthorized(int type
, int pid
)
1086 struct task_struct
*p
;
1087 uid_t euid
=0; /* Save the euid keep the lock short */
1090 read_lock(&tasklist_lock
);
1093 * Grab the lock, find the task, save the uid and
1094 * check it has an mm still (ie its not dead)
1097 p
= find_task_by_pid(pid
);
1101 if(!cap_issubset(p
->cap_permitted
, current
->cap_permitted
))
1105 read_unlock(&tasklist_lock
);
1111 case PROC_PID_STATUS
:
1112 case PROC_PID_STATM
:
1115 case PROC_PID_CMDLINE
:
1119 if(capable(CAP_DAC_OVERRIDE
) || (current
->fsuid
== euid
&& ok
))
1125 static inline int get_process_array(char * page
, int pid
, int type
)
1128 case PROC_PID_STATUS
:
1129 return get_status(pid
, page
);
1130 case PROC_PID_ENVIRON
:
1131 return get_env(pid
, page
);
1132 case PROC_PID_CMDLINE
:
1133 return get_arg(pid
, page
);
1135 return get_stat(pid
, page
);
1136 case PROC_PID_STATM
:
1137 return get_statm(pid
, page
);
1140 return get_pidcpu(pid
, page
);
1146 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
1148 static ssize_t
array_read(struct file
* file
, char * buf
,
1149 size_t count
, loff_t
*ppos
)
1151 struct inode
* inode
= file
->f_dentry
->d_inode
;
1156 unsigned int type
, pid
;
1157 struct proc_dir_entry
*dp
;
1159 if (count
> PROC_BLOCK_SIZE
)
1160 count
= PROC_BLOCK_SIZE
;
1161 if (!(page
= __get_free_page(GFP_KERNEL
)))
1163 type
= inode
->i_ino
;
1167 dp
= (struct proc_dir_entry
*) inode
->u
.generic_ip
;
1169 if (!pid
) { /* can't happen */
1174 if (process_unauthorized(type
, pid
)) {
1179 length
= get_process_array((char *) page
, pid
, type
);
1184 /* Static 4kB (or whatever) block capacity */
1185 if (*ppos
>= length
) {
1189 if (count
+ *ppos
> length
)
1190 count
= length
- *ppos
;
1191 end
= count
+ *ppos
;
1192 copy_to_user(buf
, (char *) page
+ *ppos
, count
);
1198 static struct file_operations proc_array_operations
= {
1199 NULL
, /* array_lseek */
1201 NULL
, /* array_write */
1202 NULL
, /* array_readdir */
1203 NULL
, /* array_poll */
1204 NULL
, /* array_ioctl */
1206 NULL
, /* no special open code */
1208 NULL
, /* no special release code */
1209 NULL
/* can't fsync */
1212 struct inode_operations proc_array_inode_operations
= {
1213 &proc_array_operations
, /* default base directory file-ops */
1223 NULL
, /* readlink */
1224 NULL
, /* follow_link */
1225 NULL
, /* get_block */
1226 NULL
, /* readpage */
1227 NULL
, /* writepage */
1228 NULL
, /* flushpage */
1229 NULL
, /* truncate */
1230 NULL
, /* permission */
1232 NULL
/* revalidate */
1235 static ssize_t
arraylong_read(struct file
* file
, char * buf
,
1236 size_t count
, loff_t
*ppos
)
1238 struct inode
* inode
= file
->f_dentry
->d_inode
;
1239 unsigned int pid
= inode
->i_ino
>> 16;
1240 unsigned int type
= inode
->i_ino
& 0x0000ffff;
1244 return read_maps(pid
, file
, buf
, count
, ppos
);
1249 static struct file_operations proc_arraylong_operations
= {
1250 NULL
, /* array_lseek */
1252 NULL
, /* array_write */
1253 NULL
, /* array_readdir */
1254 NULL
, /* array_poll */
1255 NULL
, /* array_ioctl */
1257 NULL
, /* no special open code */
1259 NULL
, /* no special release code */
1260 NULL
/* can't fsync */
1263 struct inode_operations proc_arraylong_inode_operations
= {
1264 &proc_arraylong_operations
, /* default base directory file-ops */
1274 NULL
, /* readlink */
1275 NULL
, /* follow_link */
1276 NULL
, /* get_block */
1277 NULL
, /* readpage */
1278 NULL
, /* writepage */
1279 NULL
, /* flushpage */
1280 NULL
, /* truncate */
1281 NULL
, /* permission */
1283 NULL
/* revalidate */