4 * Copyright (C) 1991, 1992 Linus Torvalds
6 * proc base directory handling functions
8 * 1999, Al Viro. Rewritten. Now it covers the whole per-process part.
9 * Instead of using magical inumbers to determine the kind of object
10 * we allocate and fill in-core inodes upon lookup. They don't even
11 * go into icache. We cache the reference to task_struct upon lookup too.
12 * Eventually it should become a filesystem in its own. We don't use the
13 * rest of procfs anymore.
19 * Bruna Moreira <bruna.moreira@indt.org.br>
20 * Edjard Mota <edjard.mota@indt.org.br>
21 * Ilias Biris <ilias.biris@indt.org.br>
22 * Mauricio Lin <mauricio.lin@indt.org.br>
24 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
26 * A new process specific entry (smaps) included in /proc. It shows the
27 * size of rss for each memory area. The maps entry lacks information
28 * about physical memory size (rss) for each mapped file, i.e.,
29 * rss information for executables and library files.
30 * This additional information is useful for any tools that need to know
31 * about physical memory consumption for a process specific library.
35 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
36 * Pud inclusion in the page table walking.
40 * 10LE Instituto Nokia de Tecnologia - INdT:
41 * A better way to walks through the page table as suggested by Hugh Dickins.
43 * Simo Piiroinen <simo.piiroinen@nokia.com>:
44 * Smaps information related to shared, private, clean and dirty pages.
46 * Paul Mundt <paul.mundt@nokia.com>:
47 * Overall revision about smaps.
50 #include <asm/uaccess.h>
52 #include <linux/errno.h>
53 #include <linux/time.h>
54 #include <linux/proc_fs.h>
55 #include <linux/stat.h>
56 #include <linux/task_io_accounting_ops.h>
57 #include <linux/init.h>
58 #include <linux/capability.h>
59 #include <linux/file.h>
60 #include <linux/fdtable.h>
61 #include <linux/string.h>
62 #include <linux/seq_file.h>
63 #include <linux/namei.h>
64 #include <linux/mnt_namespace.h>
66 #include <linux/swap.h>
67 #include <linux/rcupdate.h>
68 #include <linux/kallsyms.h>
69 #include <linux/stacktrace.h>
70 #include <linux/resource.h>
71 #include <linux/module.h>
72 #include <linux/mount.h>
73 #include <linux/security.h>
74 #include <linux/ptrace.h>
75 #include <linux/tracehook.h>
76 #include <linux/cgroup.h>
77 #include <linux/cpuset.h>
78 #include <linux/audit.h>
79 #include <linux/poll.h>
80 #include <linux/nsproxy.h>
81 #include <linux/oom.h>
82 #include <linux/elf.h>
83 #include <linux/pid_namespace.h>
84 #include <linux/fs_struct.h>
85 #include <linux/slab.h>
86 #ifdef CONFIG_HARDWALL
87 #include <asm/hardwall.h>
92 * Implementing inode permission operations in /proc is almost
93 * certainly an error. Permission checks need to happen during
94 * each system call not at open time. The reason is that most of
95 * what we wish to check for permissions in /proc varies at runtime.
97 * The classic example of a problem is opening file descriptors
98 * in /proc for a task before it execs a suid executable.
105 const struct inode_operations
*iop
;
106 const struct file_operations
*fop
;
110 #define NOD(NAME, MODE, IOP, FOP, OP) { \
112 .len = sizeof(NAME) - 1, \
119 #define DIR(NAME, MODE, iops, fops) \
120 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
121 #define LNK(NAME, get_link) \
122 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
123 &proc_pid_link_inode_operations, NULL, \
124 { .proc_get_link = get_link } )
125 #define REG(NAME, MODE, fops) \
126 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
127 #define INF(NAME, MODE, read) \
128 NOD(NAME, (S_IFREG|(MODE)), \
129 NULL, &proc_info_file_operations, \
130 { .proc_read = read } )
131 #define ONE(NAME, MODE, show) \
132 NOD(NAME, (S_IFREG|(MODE)), \
133 NULL, &proc_single_file_operations, \
134 { .proc_show = show } )
137 * Count the number of hardlinks for the pid_entry table, excluding the .
140 static unsigned int pid_entry_count_dirs(const struct pid_entry
*entries
,
147 for (i
= 0; i
< n
; ++i
) {
148 if (S_ISDIR(entries
[i
].mode
))
155 static int get_task_root(struct task_struct
*task
, struct path
*root
)
157 int result
= -ENOENT
;
161 get_fs_root(task
->fs
, root
);
168 static int proc_cwd_link(struct inode
*inode
, struct path
*path
)
170 struct task_struct
*task
= get_proc_task(inode
);
171 int result
= -ENOENT
;
176 get_fs_pwd(task
->fs
, path
);
180 put_task_struct(task
);
185 static int proc_root_link(struct inode
*inode
, struct path
*path
)
187 struct task_struct
*task
= get_proc_task(inode
);
188 int result
= -ENOENT
;
191 result
= get_task_root(task
, path
);
192 put_task_struct(task
);
197 static struct mm_struct
*mm_access(struct task_struct
*task
, unsigned int mode
)
199 struct mm_struct
*mm
;
202 err
= mutex_lock_killable(&task
->signal
->cred_guard_mutex
);
206 mm
= get_task_mm(task
);
207 if (mm
&& mm
!= current
->mm
&&
208 !ptrace_may_access(task
, mode
)) {
210 mm
= ERR_PTR(-EACCES
);
212 mutex_unlock(&task
->signal
->cred_guard_mutex
);
217 struct mm_struct
*mm_for_maps(struct task_struct
*task
)
219 return mm_access(task
, PTRACE_MODE_READ
);
222 static int proc_pid_cmdline(struct task_struct
*task
, char * buffer
)
226 struct mm_struct
*mm
= get_task_mm(task
);
230 goto out_mm
; /* Shh! No looking before we're done */
232 len
= mm
->arg_end
- mm
->arg_start
;
237 res
= access_process_vm(task
, mm
->arg_start
, buffer
, len
, 0);
239 // If the nul at the end of args has been overwritten, then
240 // assume application is using setproctitle(3).
241 if (res
> 0 && buffer
[res
-1] != '\0' && len
< PAGE_SIZE
) {
242 len
= strnlen(buffer
, res
);
246 len
= mm
->env_end
- mm
->env_start
;
247 if (len
> PAGE_SIZE
- res
)
248 len
= PAGE_SIZE
- res
;
249 res
+= access_process_vm(task
, mm
->env_start
, buffer
+res
, len
, 0);
250 res
= strnlen(buffer
, res
);
259 static int proc_pid_auxv(struct task_struct
*task
, char *buffer
)
261 struct mm_struct
*mm
= mm_for_maps(task
);
262 int res
= PTR_ERR(mm
);
263 if (mm
&& !IS_ERR(mm
)) {
264 unsigned int nwords
= 0;
267 } while (mm
->saved_auxv
[nwords
- 2] != 0); /* AT_NULL */
268 res
= nwords
* sizeof(mm
->saved_auxv
[0]);
271 memcpy(buffer
, mm
->saved_auxv
, res
);
278 #ifdef CONFIG_KALLSYMS
280 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
281 * Returns the resolved symbol. If that fails, simply return the address.
283 static int proc_pid_wchan(struct task_struct
*task
, char *buffer
)
286 char symname
[KSYM_NAME_LEN
];
288 wchan
= get_wchan(task
);
290 if (lookup_symbol_name(wchan
, symname
) < 0)
291 if (!ptrace_may_access(task
, PTRACE_MODE_READ
))
294 return sprintf(buffer
, "%lu", wchan
);
296 return sprintf(buffer
, "%s", symname
);
298 #endif /* CONFIG_KALLSYMS */
300 static int lock_trace(struct task_struct
*task
)
302 int err
= mutex_lock_killable(&task
->signal
->cred_guard_mutex
);
305 if (!ptrace_may_access(task
, PTRACE_MODE_ATTACH
)) {
306 mutex_unlock(&task
->signal
->cred_guard_mutex
);
312 static void unlock_trace(struct task_struct
*task
)
314 mutex_unlock(&task
->signal
->cred_guard_mutex
);
317 #ifdef CONFIG_STACKTRACE
319 #define MAX_STACK_TRACE_DEPTH 64
321 static int proc_pid_stack(struct seq_file
*m
, struct pid_namespace
*ns
,
322 struct pid
*pid
, struct task_struct
*task
)
324 struct stack_trace trace
;
325 unsigned long *entries
;
329 entries
= kmalloc(MAX_STACK_TRACE_DEPTH
* sizeof(*entries
), GFP_KERNEL
);
333 trace
.nr_entries
= 0;
334 trace
.max_entries
= MAX_STACK_TRACE_DEPTH
;
335 trace
.entries
= entries
;
338 err
= lock_trace(task
);
340 save_stack_trace_tsk(task
, &trace
);
342 for (i
= 0; i
< trace
.nr_entries
; i
++) {
343 seq_printf(m
, "[<%pK>] %pS\n",
344 (void *)entries
[i
], (void *)entries
[i
]);
354 #ifdef CONFIG_SCHEDSTATS
356 * Provides /proc/PID/schedstat
358 static int proc_pid_schedstat(struct task_struct
*task
, char *buffer
)
360 return sprintf(buffer
, "%llu %llu %lu\n",
361 (unsigned long long)task
->se
.sum_exec_runtime
,
362 (unsigned long long)task
->sched_info
.run_delay
,
363 task
->sched_info
.pcount
);
367 #ifdef CONFIG_LATENCYTOP
368 static int lstats_show_proc(struct seq_file
*m
, void *v
)
371 struct inode
*inode
= m
->private;
372 struct task_struct
*task
= get_proc_task(inode
);
376 seq_puts(m
, "Latency Top version : v0.1\n");
377 for (i
= 0; i
< 32; i
++) {
378 struct latency_record
*lr
= &task
->latency_record
[i
];
379 if (lr
->backtrace
[0]) {
381 seq_printf(m
, "%i %li %li",
382 lr
->count
, lr
->time
, lr
->max
);
383 for (q
= 0; q
< LT_BACKTRACEDEPTH
; q
++) {
384 unsigned long bt
= lr
->backtrace
[q
];
389 seq_printf(m
, " %ps", (void *)bt
);
395 put_task_struct(task
);
399 static int lstats_open(struct inode
*inode
, struct file
*file
)
401 return single_open(file
, lstats_show_proc
, inode
);
404 static ssize_t
lstats_write(struct file
*file
, const char __user
*buf
,
405 size_t count
, loff_t
*offs
)
407 struct task_struct
*task
= get_proc_task(file
->f_dentry
->d_inode
);
411 clear_all_latency_tracing(task
);
412 put_task_struct(task
);
417 static const struct file_operations proc_lstats_operations
= {
420 .write
= lstats_write
,
422 .release
= single_release
,
427 static int proc_oom_score(struct task_struct
*task
, char *buffer
)
429 unsigned long points
= 0;
431 read_lock(&tasklist_lock
);
433 points
= oom_badness(task
, NULL
, NULL
,
434 totalram_pages
+ total_swap_pages
);
435 read_unlock(&tasklist_lock
);
436 return sprintf(buffer
, "%lu\n", points
);
444 static const struct limit_names lnames
[RLIM_NLIMITS
] = {
445 [RLIMIT_CPU
] = {"Max cpu time", "seconds"},
446 [RLIMIT_FSIZE
] = {"Max file size", "bytes"},
447 [RLIMIT_DATA
] = {"Max data size", "bytes"},
448 [RLIMIT_STACK
] = {"Max stack size", "bytes"},
449 [RLIMIT_CORE
] = {"Max core file size", "bytes"},
450 [RLIMIT_RSS
] = {"Max resident set", "bytes"},
451 [RLIMIT_NPROC
] = {"Max processes", "processes"},
452 [RLIMIT_NOFILE
] = {"Max open files", "files"},
453 [RLIMIT_MEMLOCK
] = {"Max locked memory", "bytes"},
454 [RLIMIT_AS
] = {"Max address space", "bytes"},
455 [RLIMIT_LOCKS
] = {"Max file locks", "locks"},
456 [RLIMIT_SIGPENDING
] = {"Max pending signals", "signals"},
457 [RLIMIT_MSGQUEUE
] = {"Max msgqueue size", "bytes"},
458 [RLIMIT_NICE
] = {"Max nice priority", NULL
},
459 [RLIMIT_RTPRIO
] = {"Max realtime priority", NULL
},
460 [RLIMIT_RTTIME
] = {"Max realtime timeout", "us"},
463 /* Display limits for a process */
464 static int proc_pid_limits(struct task_struct
*task
, char *buffer
)
469 char *bufptr
= buffer
;
471 struct rlimit rlim
[RLIM_NLIMITS
];
473 if (!lock_task_sighand(task
, &flags
))
475 memcpy(rlim
, task
->signal
->rlim
, sizeof(struct rlimit
) * RLIM_NLIMITS
);
476 unlock_task_sighand(task
, &flags
);
479 * print the file header
481 count
+= sprintf(&bufptr
[count
], "%-25s %-20s %-20s %-10s\n",
482 "Limit", "Soft Limit", "Hard Limit", "Units");
484 for (i
= 0; i
< RLIM_NLIMITS
; i
++) {
485 if (rlim
[i
].rlim_cur
== RLIM_INFINITY
)
486 count
+= sprintf(&bufptr
[count
], "%-25s %-20s ",
487 lnames
[i
].name
, "unlimited");
489 count
+= sprintf(&bufptr
[count
], "%-25s %-20lu ",
490 lnames
[i
].name
, rlim
[i
].rlim_cur
);
492 if (rlim
[i
].rlim_max
== RLIM_INFINITY
)
493 count
+= sprintf(&bufptr
[count
], "%-20s ", "unlimited");
495 count
+= sprintf(&bufptr
[count
], "%-20lu ",
499 count
+= sprintf(&bufptr
[count
], "%-10s\n",
502 count
+= sprintf(&bufptr
[count
], "\n");
508 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
509 static int proc_pid_syscall(struct task_struct
*task
, char *buffer
)
512 unsigned long args
[6], sp
, pc
;
513 int res
= lock_trace(task
);
517 if (task_current_syscall(task
, &nr
, args
, 6, &sp
, &pc
))
518 res
= sprintf(buffer
, "running\n");
520 res
= sprintf(buffer
, "%ld 0x%lx 0x%lx\n", nr
, sp
, pc
);
522 res
= sprintf(buffer
,
523 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
525 args
[0], args
[1], args
[2], args
[3], args
[4], args
[5],
530 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
532 /************************************************************************/
533 /* Here the fs part begins */
534 /************************************************************************/
536 /* permission checks */
537 static int proc_fd_access_allowed(struct inode
*inode
)
539 struct task_struct
*task
;
541 /* Allow access to a task's file descriptors if it is us or we
542 * may use ptrace attach to the process and find out that
545 task
= get_proc_task(inode
);
547 allowed
= ptrace_may_access(task
, PTRACE_MODE_READ
);
548 put_task_struct(task
);
553 int proc_setattr(struct dentry
*dentry
, struct iattr
*attr
)
556 struct inode
*inode
= dentry
->d_inode
;
558 if (attr
->ia_valid
& ATTR_MODE
)
561 error
= inode_change_ok(inode
, attr
);
565 if ((attr
->ia_valid
& ATTR_SIZE
) &&
566 attr
->ia_size
!= i_size_read(inode
)) {
567 error
= vmtruncate(inode
, attr
->ia_size
);
572 setattr_copy(inode
, attr
);
573 mark_inode_dirty(inode
);
577 static const struct inode_operations proc_def_inode_operations
= {
578 .setattr
= proc_setattr
,
581 static int mounts_open_common(struct inode
*inode
, struct file
*file
,
582 const struct seq_operations
*op
)
584 struct task_struct
*task
= get_proc_task(inode
);
586 struct mnt_namespace
*ns
= NULL
;
588 struct proc_mounts
*p
;
593 nsp
= task_nsproxy(task
);
600 if (ns
&& get_task_root(task
, &root
) == 0)
602 put_task_struct(task
);
611 p
= kmalloc(sizeof(struct proc_mounts
), GFP_KERNEL
);
615 file
->private_data
= &p
->m
;
616 ret
= seq_open(file
, op
);
623 p
->event
= ns
->event
;
637 static int mounts_release(struct inode
*inode
, struct file
*file
)
639 struct proc_mounts
*p
= file
->private_data
;
642 return seq_release(inode
, file
);
645 static unsigned mounts_poll(struct file
*file
, poll_table
*wait
)
647 struct proc_mounts
*p
= file
->private_data
;
648 unsigned res
= POLLIN
| POLLRDNORM
;
650 poll_wait(file
, &p
->ns
->poll
, wait
);
651 if (mnt_had_events(p
))
652 res
|= POLLERR
| POLLPRI
;
657 static int mounts_open(struct inode
*inode
, struct file
*file
)
659 return mounts_open_common(inode
, file
, &mounts_op
);
662 static const struct file_operations proc_mounts_operations
= {
666 .release
= mounts_release
,
670 static int mountinfo_open(struct inode
*inode
, struct file
*file
)
672 return mounts_open_common(inode
, file
, &mountinfo_op
);
675 static const struct file_operations proc_mountinfo_operations
= {
676 .open
= mountinfo_open
,
679 .release
= mounts_release
,
683 static int mountstats_open(struct inode
*inode
, struct file
*file
)
685 return mounts_open_common(inode
, file
, &mountstats_op
);
688 static const struct file_operations proc_mountstats_operations
= {
689 .open
= mountstats_open
,
692 .release
= mounts_release
,
695 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
697 static ssize_t
proc_info_read(struct file
* file
, char __user
* buf
,
698 size_t count
, loff_t
*ppos
)
700 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
703 struct task_struct
*task
= get_proc_task(inode
);
709 if (count
> PROC_BLOCK_SIZE
)
710 count
= PROC_BLOCK_SIZE
;
713 if (!(page
= __get_free_page(GFP_TEMPORARY
)))
716 length
= PROC_I(inode
)->op
.proc_read(task
, (char*)page
);
719 length
= simple_read_from_buffer(buf
, count
, ppos
, (char *)page
, length
);
722 put_task_struct(task
);
727 static const struct file_operations proc_info_file_operations
= {
728 .read
= proc_info_read
,
729 .llseek
= generic_file_llseek
,
732 static int proc_single_show(struct seq_file
*m
, void *v
)
734 struct inode
*inode
= m
->private;
735 struct pid_namespace
*ns
;
737 struct task_struct
*task
;
740 ns
= inode
->i_sb
->s_fs_info
;
741 pid
= proc_pid(inode
);
742 task
= get_pid_task(pid
, PIDTYPE_PID
);
746 ret
= PROC_I(inode
)->op
.proc_show(m
, ns
, pid
, task
);
748 put_task_struct(task
);
752 static int proc_single_open(struct inode
*inode
, struct file
*filp
)
754 return single_open(filp
, proc_single_show
, inode
);
757 static const struct file_operations proc_single_file_operations
= {
758 .open
= proc_single_open
,
761 .release
= single_release
,
764 static int mem_open(struct inode
* inode
, struct file
* file
)
766 struct task_struct
*task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
767 struct mm_struct
*mm
;
772 mm
= mm_access(task
, PTRACE_MODE_ATTACH
);
773 put_task_struct(task
);
779 /* ensure this mm_struct can't be freed */
780 atomic_inc(&mm
->mm_count
);
781 /* but do not pin its memory */
785 /* OK to pass negative loff_t, we can catch out-of-range */
786 file
->f_mode
|= FMODE_UNSIGNED_OFFSET
;
787 file
->private_data
= mm
;
792 static ssize_t
mem_rw(struct file
*file
, char __user
*buf
,
793 size_t count
, loff_t
*ppos
, int write
)
795 struct mm_struct
*mm
= file
->private_data
;
796 unsigned long addr
= *ppos
;
803 page
= (char *)__get_free_page(GFP_TEMPORARY
);
808 if (!atomic_inc_not_zero(&mm
->mm_users
))
812 int this_len
= min_t(int, count
, PAGE_SIZE
);
814 if (write
&& copy_from_user(page
, buf
, this_len
)) {
819 this_len
= access_remote_vm(mm
, addr
, page
, this_len
, write
);
826 if (!write
&& copy_to_user(buf
, page
, this_len
)) {
840 free_page((unsigned long) page
);
844 static ssize_t
mem_read(struct file
*file
, char __user
*buf
,
845 size_t count
, loff_t
*ppos
)
847 return mem_rw(file
, buf
, count
, ppos
, 0);
850 static ssize_t
mem_write(struct file
*file
, const char __user
*buf
,
851 size_t count
, loff_t
*ppos
)
853 return mem_rw(file
, (char __user
*)buf
, count
, ppos
, 1);
856 loff_t
mem_lseek(struct file
*file
, loff_t offset
, int orig
)
860 file
->f_pos
= offset
;
863 file
->f_pos
+= offset
;
868 force_successful_syscall_return();
872 static int mem_release(struct inode
*inode
, struct file
*file
)
874 struct mm_struct
*mm
= file
->private_data
;
880 static const struct file_operations proc_mem_operations
= {
885 .release
= mem_release
,
888 static ssize_t
environ_read(struct file
*file
, char __user
*buf
,
889 size_t count
, loff_t
*ppos
)
891 struct task_struct
*task
= get_proc_task(file
->f_dentry
->d_inode
);
893 unsigned long src
= *ppos
;
895 struct mm_struct
*mm
;
901 page
= (char *)__get_free_page(GFP_TEMPORARY
);
906 mm
= mm_for_maps(task
);
908 if (!mm
|| IS_ERR(mm
))
913 int this_len
, retval
, max_len
;
915 this_len
= mm
->env_end
- (mm
->env_start
+ src
);
920 max_len
= (count
> PAGE_SIZE
) ? PAGE_SIZE
: count
;
921 this_len
= (this_len
> max_len
) ? max_len
: this_len
;
923 retval
= access_process_vm(task
, (mm
->env_start
+ src
),
931 if (copy_to_user(buf
, page
, retval
)) {
945 free_page((unsigned long) page
);
947 put_task_struct(task
);
952 static const struct file_operations proc_environ_operations
= {
953 .read
= environ_read
,
954 .llseek
= generic_file_llseek
,
957 static ssize_t
oom_adjust_read(struct file
*file
, char __user
*buf
,
958 size_t count
, loff_t
*ppos
)
960 struct task_struct
*task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
961 char buffer
[PROC_NUMBUF
];
963 int oom_adjust
= OOM_DISABLE
;
969 if (lock_task_sighand(task
, &flags
)) {
970 oom_adjust
= task
->signal
->oom_adj
;
971 unlock_task_sighand(task
, &flags
);
974 put_task_struct(task
);
976 len
= snprintf(buffer
, sizeof(buffer
), "%i\n", oom_adjust
);
978 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
981 static ssize_t
oom_adjust_write(struct file
*file
, const char __user
*buf
,
982 size_t count
, loff_t
*ppos
)
984 struct task_struct
*task
;
985 char buffer
[PROC_NUMBUF
];
990 memset(buffer
, 0, sizeof(buffer
));
991 if (count
> sizeof(buffer
) - 1)
992 count
= sizeof(buffer
) - 1;
993 if (copy_from_user(buffer
, buf
, count
)) {
998 err
= kstrtoint(strstrip(buffer
), 0, &oom_adjust
);
1001 if ((oom_adjust
< OOM_ADJUST_MIN
|| oom_adjust
> OOM_ADJUST_MAX
) &&
1002 oom_adjust
!= OOM_DISABLE
) {
1007 task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
1019 if (!lock_task_sighand(task
, &flags
)) {
1024 if (oom_adjust
< task
->signal
->oom_adj
&& !capable(CAP_SYS_RESOURCE
)) {
1029 if (oom_adjust
!= task
->signal
->oom_adj
) {
1030 if (oom_adjust
== OOM_DISABLE
)
1031 atomic_inc(&task
->mm
->oom_disable_count
);
1032 if (task
->signal
->oom_adj
== OOM_DISABLE
)
1033 atomic_dec(&task
->mm
->oom_disable_count
);
1037 * Warn that /proc/pid/oom_adj is deprecated, see
1038 * Documentation/feature-removal-schedule.txt.
1040 printk_once(KERN_WARNING
"%s (%d): /proc/%d/oom_adj is deprecated, "
1041 "please use /proc/%d/oom_score_adj instead.\n",
1042 current
->comm
, task_pid_nr(current
),
1043 task_pid_nr(task
), task_pid_nr(task
));
1044 task
->signal
->oom_adj
= oom_adjust
;
1046 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1047 * value is always attainable.
1049 if (task
->signal
->oom_adj
== OOM_ADJUST_MAX
)
1050 task
->signal
->oom_score_adj
= OOM_SCORE_ADJ_MAX
;
1052 task
->signal
->oom_score_adj
= (oom_adjust
* OOM_SCORE_ADJ_MAX
) /
1055 unlock_task_sighand(task
, &flags
);
1058 put_task_struct(task
);
1060 return err
< 0 ? err
: count
;
1063 static const struct file_operations proc_oom_adjust_operations
= {
1064 .read
= oom_adjust_read
,
1065 .write
= oom_adjust_write
,
1066 .llseek
= generic_file_llseek
,
1069 static ssize_t
oom_score_adj_read(struct file
*file
, char __user
*buf
,
1070 size_t count
, loff_t
*ppos
)
1072 struct task_struct
*task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
1073 char buffer
[PROC_NUMBUF
];
1074 int oom_score_adj
= OOM_SCORE_ADJ_MIN
;
1075 unsigned long flags
;
1080 if (lock_task_sighand(task
, &flags
)) {
1081 oom_score_adj
= task
->signal
->oom_score_adj
;
1082 unlock_task_sighand(task
, &flags
);
1084 put_task_struct(task
);
1085 len
= snprintf(buffer
, sizeof(buffer
), "%d\n", oom_score_adj
);
1086 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
1089 static ssize_t
oom_score_adj_write(struct file
*file
, const char __user
*buf
,
1090 size_t count
, loff_t
*ppos
)
1092 struct task_struct
*task
;
1093 char buffer
[PROC_NUMBUF
];
1094 unsigned long flags
;
1098 memset(buffer
, 0, sizeof(buffer
));
1099 if (count
> sizeof(buffer
) - 1)
1100 count
= sizeof(buffer
) - 1;
1101 if (copy_from_user(buffer
, buf
, count
)) {
1106 err
= kstrtoint(strstrip(buffer
), 0, &oom_score_adj
);
1109 if (oom_score_adj
< OOM_SCORE_ADJ_MIN
||
1110 oom_score_adj
> OOM_SCORE_ADJ_MAX
) {
1115 task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
1127 if (!lock_task_sighand(task
, &flags
)) {
1132 if (oom_score_adj
< task
->signal
->oom_score_adj_min
&&
1133 !capable(CAP_SYS_RESOURCE
)) {
1138 if (oom_score_adj
!= task
->signal
->oom_score_adj
) {
1139 if (oom_score_adj
== OOM_SCORE_ADJ_MIN
)
1140 atomic_inc(&task
->mm
->oom_disable_count
);
1141 if (task
->signal
->oom_score_adj
== OOM_SCORE_ADJ_MIN
)
1142 atomic_dec(&task
->mm
->oom_disable_count
);
1144 task
->signal
->oom_score_adj
= oom_score_adj
;
1145 if (has_capability_noaudit(current
, CAP_SYS_RESOURCE
))
1146 task
->signal
->oom_score_adj_min
= oom_score_adj
;
1148 * Scale /proc/pid/oom_adj appropriately ensuring that OOM_DISABLE is
1149 * always attainable.
1151 if (task
->signal
->oom_score_adj
== OOM_SCORE_ADJ_MIN
)
1152 task
->signal
->oom_adj
= OOM_DISABLE
;
1154 task
->signal
->oom_adj
= (oom_score_adj
* OOM_ADJUST_MAX
) /
1157 unlock_task_sighand(task
, &flags
);
1160 put_task_struct(task
);
1162 return err
< 0 ? err
: count
;
1165 static const struct file_operations proc_oom_score_adj_operations
= {
1166 .read
= oom_score_adj_read
,
1167 .write
= oom_score_adj_write
,
1168 .llseek
= default_llseek
,
1171 #ifdef CONFIG_AUDITSYSCALL
1172 #define TMPBUFLEN 21
1173 static ssize_t
proc_loginuid_read(struct file
* file
, char __user
* buf
,
1174 size_t count
, loff_t
*ppos
)
1176 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
1177 struct task_struct
*task
= get_proc_task(inode
);
1179 char tmpbuf
[TMPBUFLEN
];
1183 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
1184 audit_get_loginuid(task
));
1185 put_task_struct(task
);
1186 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1189 static ssize_t
proc_loginuid_write(struct file
* file
, const char __user
* buf
,
1190 size_t count
, loff_t
*ppos
)
1192 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
1197 if (!capable(CAP_AUDIT_CONTROL
))
1201 if (current
!= pid_task(proc_pid(inode
), PIDTYPE_PID
)) {
1207 if (count
>= PAGE_SIZE
)
1208 count
= PAGE_SIZE
- 1;
1211 /* No partial writes. */
1214 page
= (char*)__get_free_page(GFP_TEMPORARY
);
1218 if (copy_from_user(page
, buf
, count
))
1222 loginuid
= simple_strtoul(page
, &tmp
, 10);
1228 length
= audit_set_loginuid(current
, loginuid
);
1229 if (likely(length
== 0))
1233 free_page((unsigned long) page
);
1237 static const struct file_operations proc_loginuid_operations
= {
1238 .read
= proc_loginuid_read
,
1239 .write
= proc_loginuid_write
,
1240 .llseek
= generic_file_llseek
,
1243 static ssize_t
proc_sessionid_read(struct file
* file
, char __user
* buf
,
1244 size_t count
, loff_t
*ppos
)
1246 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
1247 struct task_struct
*task
= get_proc_task(inode
);
1249 char tmpbuf
[TMPBUFLEN
];
1253 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
1254 audit_get_sessionid(task
));
1255 put_task_struct(task
);
1256 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1259 static const struct file_operations proc_sessionid_operations
= {
1260 .read
= proc_sessionid_read
,
1261 .llseek
= generic_file_llseek
,
1265 #ifdef CONFIG_FAULT_INJECTION
1266 static ssize_t
proc_fault_inject_read(struct file
* file
, char __user
* buf
,
1267 size_t count
, loff_t
*ppos
)
1269 struct task_struct
*task
= get_proc_task(file
->f_dentry
->d_inode
);
1270 char buffer
[PROC_NUMBUF
];
1276 make_it_fail
= task
->make_it_fail
;
1277 put_task_struct(task
);
1279 len
= snprintf(buffer
, sizeof(buffer
), "%i\n", make_it_fail
);
1281 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
1284 static ssize_t
proc_fault_inject_write(struct file
* file
,
1285 const char __user
* buf
, size_t count
, loff_t
*ppos
)
1287 struct task_struct
*task
;
1288 char buffer
[PROC_NUMBUF
], *end
;
1291 if (!capable(CAP_SYS_RESOURCE
))
1293 memset(buffer
, 0, sizeof(buffer
));
1294 if (count
> sizeof(buffer
) - 1)
1295 count
= sizeof(buffer
) - 1;
1296 if (copy_from_user(buffer
, buf
, count
))
1298 make_it_fail
= simple_strtol(strstrip(buffer
), &end
, 0);
1301 task
= get_proc_task(file
->f_dentry
->d_inode
);
1304 task
->make_it_fail
= make_it_fail
;
1305 put_task_struct(task
);
1310 static const struct file_operations proc_fault_inject_operations
= {
1311 .read
= proc_fault_inject_read
,
1312 .write
= proc_fault_inject_write
,
1313 .llseek
= generic_file_llseek
,
1318 #ifdef CONFIG_SCHED_DEBUG
1320 * Print out various scheduling related per-task fields:
1322 static int sched_show(struct seq_file
*m
, void *v
)
1324 struct inode
*inode
= m
->private;
1325 struct task_struct
*p
;
1327 p
= get_proc_task(inode
);
1330 proc_sched_show_task(p
, m
);
1338 sched_write(struct file
*file
, const char __user
*buf
,
1339 size_t count
, loff_t
*offset
)
1341 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1342 struct task_struct
*p
;
1344 p
= get_proc_task(inode
);
1347 proc_sched_set_task(p
);
1354 static int sched_open(struct inode
*inode
, struct file
*filp
)
1356 return single_open(filp
, sched_show
, inode
);
1359 static const struct file_operations proc_pid_sched_operations
= {
1362 .write
= sched_write
,
1363 .llseek
= seq_lseek
,
1364 .release
= single_release
,
1369 #ifdef CONFIG_SCHED_AUTOGROUP
1371 * Print out autogroup related information:
1373 static int sched_autogroup_show(struct seq_file
*m
, void *v
)
1375 struct inode
*inode
= m
->private;
1376 struct task_struct
*p
;
1378 p
= get_proc_task(inode
);
1381 proc_sched_autogroup_show_task(p
, m
);
1389 sched_autogroup_write(struct file
*file
, const char __user
*buf
,
1390 size_t count
, loff_t
*offset
)
1392 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1393 struct task_struct
*p
;
1394 char buffer
[PROC_NUMBUF
];
1398 memset(buffer
, 0, sizeof(buffer
));
1399 if (count
> sizeof(buffer
) - 1)
1400 count
= sizeof(buffer
) - 1;
1401 if (copy_from_user(buffer
, buf
, count
))
1404 err
= kstrtoint(strstrip(buffer
), 0, &nice
);
1408 p
= get_proc_task(inode
);
1413 err
= proc_sched_autogroup_set_nice(p
, &err
);
1422 static int sched_autogroup_open(struct inode
*inode
, struct file
*filp
)
1426 ret
= single_open(filp
, sched_autogroup_show
, NULL
);
1428 struct seq_file
*m
= filp
->private_data
;
1435 static const struct file_operations proc_pid_sched_autogroup_operations
= {
1436 .open
= sched_autogroup_open
,
1438 .write
= sched_autogroup_write
,
1439 .llseek
= seq_lseek
,
1440 .release
= single_release
,
1443 #endif /* CONFIG_SCHED_AUTOGROUP */
1445 static ssize_t
comm_write(struct file
*file
, const char __user
*buf
,
1446 size_t count
, loff_t
*offset
)
1448 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1449 struct task_struct
*p
;
1450 char buffer
[TASK_COMM_LEN
];
1452 memset(buffer
, 0, sizeof(buffer
));
1453 if (count
> sizeof(buffer
) - 1)
1454 count
= sizeof(buffer
) - 1;
1455 if (copy_from_user(buffer
, buf
, count
))
1458 p
= get_proc_task(inode
);
1462 if (same_thread_group(current
, p
))
1463 set_task_comm(p
, buffer
);
1472 static int comm_show(struct seq_file
*m
, void *v
)
1474 struct inode
*inode
= m
->private;
1475 struct task_struct
*p
;
1477 p
= get_proc_task(inode
);
1482 seq_printf(m
, "%s\n", p
->comm
);
1490 static int comm_open(struct inode
*inode
, struct file
*filp
)
1492 return single_open(filp
, comm_show
, inode
);
1495 static const struct file_operations proc_pid_set_comm_operations
= {
1498 .write
= comm_write
,
1499 .llseek
= seq_lseek
,
1500 .release
= single_release
,
1503 static int proc_exe_link(struct inode
*inode
, struct path
*exe_path
)
1505 struct task_struct
*task
;
1506 struct mm_struct
*mm
;
1507 struct file
*exe_file
;
1509 task
= get_proc_task(inode
);
1512 mm
= get_task_mm(task
);
1513 put_task_struct(task
);
1516 exe_file
= get_mm_exe_file(mm
);
1519 *exe_path
= exe_file
->f_path
;
1520 path_get(&exe_file
->f_path
);
1527 static void *proc_pid_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
1529 struct inode
*inode
= dentry
->d_inode
;
1530 int error
= -EACCES
;
1532 /* We don't need a base pointer in the /proc filesystem */
1533 path_put(&nd
->path
);
1535 /* Are we allowed to snoop on the tasks file descriptors? */
1536 if (!proc_fd_access_allowed(inode
))
1539 error
= PROC_I(inode
)->op
.proc_get_link(inode
, &nd
->path
);
1541 return ERR_PTR(error
);
1544 static int do_proc_readlink(struct path
*path
, char __user
*buffer
, int buflen
)
1546 char *tmp
= (char*)__get_free_page(GFP_TEMPORARY
);
1553 pathname
= d_path(path
, tmp
, PAGE_SIZE
);
1554 len
= PTR_ERR(pathname
);
1555 if (IS_ERR(pathname
))
1557 len
= tmp
+ PAGE_SIZE
- 1 - pathname
;
1561 if (copy_to_user(buffer
, pathname
, len
))
1564 free_page((unsigned long)tmp
);
1568 static int proc_pid_readlink(struct dentry
* dentry
, char __user
* buffer
, int buflen
)
1570 int error
= -EACCES
;
1571 struct inode
*inode
= dentry
->d_inode
;
1574 /* Are we allowed to snoop on the tasks file descriptors? */
1575 if (!proc_fd_access_allowed(inode
))
1578 error
= PROC_I(inode
)->op
.proc_get_link(inode
, &path
);
1582 error
= do_proc_readlink(&path
, buffer
, buflen
);
1588 static const struct inode_operations proc_pid_link_inode_operations
= {
1589 .readlink
= proc_pid_readlink
,
1590 .follow_link
= proc_pid_follow_link
,
1591 .setattr
= proc_setattr
,
1595 /* building an inode */
1597 static int task_dumpable(struct task_struct
*task
)
1600 struct mm_struct
*mm
;
1605 dumpable
= get_dumpable(mm
);
1612 struct inode
*proc_pid_make_inode(struct super_block
* sb
, struct task_struct
*task
)
1614 struct inode
* inode
;
1615 struct proc_inode
*ei
;
1616 const struct cred
*cred
;
1618 /* We need a new inode */
1620 inode
= new_inode(sb
);
1626 inode
->i_ino
= get_next_ino();
1627 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1628 inode
->i_op
= &proc_def_inode_operations
;
1631 * grab the reference to task.
1633 ei
->pid
= get_task_pid(task
, PIDTYPE_PID
);
1637 if (task_dumpable(task
)) {
1639 cred
= __task_cred(task
);
1640 inode
->i_uid
= cred
->euid
;
1641 inode
->i_gid
= cred
->egid
;
1644 security_task_to_inode(task
, inode
);
1654 int pid_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
1656 struct inode
*inode
= dentry
->d_inode
;
1657 struct task_struct
*task
;
1658 const struct cred
*cred
;
1660 generic_fillattr(inode
, stat
);
1665 task
= pid_task(proc_pid(inode
), PIDTYPE_PID
);
1667 if ((inode
->i_mode
== (S_IFDIR
|S_IRUGO
|S_IXUGO
)) ||
1668 task_dumpable(task
)) {
1669 cred
= __task_cred(task
);
1670 stat
->uid
= cred
->euid
;
1671 stat
->gid
= cred
->egid
;
1681 * Exceptional case: normally we are not allowed to unhash a busy
1682 * directory. In this case, however, we can do it - no aliasing problems
1683 * due to the way we treat inodes.
1685 * Rewrite the inode's ownerships here because the owning task may have
1686 * performed a setuid(), etc.
1688 * Before the /proc/pid/status file was created the only way to read
1689 * the effective uid of a /process was to stat /proc/pid. Reading
1690 * /proc/pid/status is slow enough that procps and other packages
1691 * kept stating /proc/pid. To keep the rules in /proc simple I have
1692 * made this apply to all per process world readable and executable
1695 int pid_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1697 struct inode
*inode
;
1698 struct task_struct
*task
;
1699 const struct cred
*cred
;
1701 if (nd
&& nd
->flags
& LOOKUP_RCU
)
1704 inode
= dentry
->d_inode
;
1705 task
= get_proc_task(inode
);
1708 if ((inode
->i_mode
== (S_IFDIR
|S_IRUGO
|S_IXUGO
)) ||
1709 task_dumpable(task
)) {
1711 cred
= __task_cred(task
);
1712 inode
->i_uid
= cred
->euid
;
1713 inode
->i_gid
= cred
->egid
;
1719 inode
->i_mode
&= ~(S_ISUID
| S_ISGID
);
1720 security_task_to_inode(task
, inode
);
1721 put_task_struct(task
);
1728 static int pid_delete_dentry(const struct dentry
* dentry
)
1730 /* Is the task we represent dead?
1731 * If so, then don't put the dentry on the lru list,
1732 * kill it immediately.
1734 return !proc_pid(dentry
->d_inode
)->tasks
[PIDTYPE_PID
].first
;
1737 const struct dentry_operations pid_dentry_operations
=
1739 .d_revalidate
= pid_revalidate
,
1740 .d_delete
= pid_delete_dentry
,
1746 * Fill a directory entry.
1748 * If possible create the dcache entry and derive our inode number and
1749 * file type from dcache entry.
1751 * Since all of the proc inode numbers are dynamically generated, the inode
1752 * numbers do not exist until the inode is cache. This means creating the
1753 * the dcache entry in readdir is necessary to keep the inode numbers
1754 * reported by readdir in sync with the inode numbers reported
1757 int proc_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
1758 const char *name
, int len
,
1759 instantiate_t instantiate
, struct task_struct
*task
, const void *ptr
)
1761 struct dentry
*child
, *dir
= filp
->f_path
.dentry
;
1762 struct inode
*inode
;
1765 unsigned type
= DT_UNKNOWN
;
1769 qname
.hash
= full_name_hash(name
, len
);
1771 child
= d_lookup(dir
, &qname
);
1774 new = d_alloc(dir
, &qname
);
1776 child
= instantiate(dir
->d_inode
, new, task
, ptr
);
1783 if (!child
|| IS_ERR(child
) || !child
->d_inode
)
1784 goto end_instantiate
;
1785 inode
= child
->d_inode
;
1788 type
= inode
->i_mode
>> 12;
1793 ino
= find_inode_number(dir
, &qname
);
1796 return filldir(dirent
, name
, len
, filp
->f_pos
, ino
, type
);
1799 static unsigned name_to_int(struct dentry
*dentry
)
1801 const char *name
= dentry
->d_name
.name
;
1802 int len
= dentry
->d_name
.len
;
1805 if (len
> 1 && *name
== '0')
1808 unsigned c
= *name
++ - '0';
1811 if (n
>= (~0U-9)/10)
1821 #define PROC_FDINFO_MAX 64
1823 static int proc_fd_info(struct inode
*inode
, struct path
*path
, char *info
)
1825 struct task_struct
*task
= get_proc_task(inode
);
1826 struct files_struct
*files
= NULL
;
1828 int fd
= proc_fd(inode
);
1831 files
= get_files_struct(task
);
1832 put_task_struct(task
);
1836 * We are not taking a ref to the file structure, so we must
1839 spin_lock(&files
->file_lock
);
1840 file
= fcheck_files(files
, fd
);
1842 unsigned int f_flags
;
1843 struct fdtable
*fdt
;
1845 fdt
= files_fdtable(files
);
1846 f_flags
= file
->f_flags
& ~O_CLOEXEC
;
1847 if (FD_ISSET(fd
, fdt
->close_on_exec
))
1848 f_flags
|= O_CLOEXEC
;
1851 *path
= file
->f_path
;
1852 path_get(&file
->f_path
);
1855 snprintf(info
, PROC_FDINFO_MAX
,
1858 (long long) file
->f_pos
,
1860 spin_unlock(&files
->file_lock
);
1861 put_files_struct(files
);
1864 spin_unlock(&files
->file_lock
);
1865 put_files_struct(files
);
1870 static int proc_fd_link(struct inode
*inode
, struct path
*path
)
1872 return proc_fd_info(inode
, path
, NULL
);
1875 static int tid_fd_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1877 struct inode
*inode
;
1878 struct task_struct
*task
;
1880 struct files_struct
*files
;
1881 const struct cred
*cred
;
1883 if (nd
&& nd
->flags
& LOOKUP_RCU
)
1886 inode
= dentry
->d_inode
;
1887 task
= get_proc_task(inode
);
1888 fd
= proc_fd(inode
);
1891 files
= get_files_struct(task
);
1894 if (fcheck_files(files
, fd
)) {
1896 put_files_struct(files
);
1897 if (task_dumpable(task
)) {
1899 cred
= __task_cred(task
);
1900 inode
->i_uid
= cred
->euid
;
1901 inode
->i_gid
= cred
->egid
;
1907 inode
->i_mode
&= ~(S_ISUID
| S_ISGID
);
1908 security_task_to_inode(task
, inode
);
1909 put_task_struct(task
);
1913 put_files_struct(files
);
1915 put_task_struct(task
);
1921 static const struct dentry_operations tid_fd_dentry_operations
=
1923 .d_revalidate
= tid_fd_revalidate
,
1924 .d_delete
= pid_delete_dentry
,
1927 static struct dentry
*proc_fd_instantiate(struct inode
*dir
,
1928 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
1930 unsigned fd
= *(const unsigned *)ptr
;
1932 struct files_struct
*files
;
1933 struct inode
*inode
;
1934 struct proc_inode
*ei
;
1935 struct dentry
*error
= ERR_PTR(-ENOENT
);
1937 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
1942 files
= get_files_struct(task
);
1945 inode
->i_mode
= S_IFLNK
;
1948 * We are not taking a ref to the file structure, so we must
1951 spin_lock(&files
->file_lock
);
1952 file
= fcheck_files(files
, fd
);
1955 if (file
->f_mode
& FMODE_READ
)
1956 inode
->i_mode
|= S_IRUSR
| S_IXUSR
;
1957 if (file
->f_mode
& FMODE_WRITE
)
1958 inode
->i_mode
|= S_IWUSR
| S_IXUSR
;
1959 spin_unlock(&files
->file_lock
);
1960 put_files_struct(files
);
1962 inode
->i_op
= &proc_pid_link_inode_operations
;
1964 ei
->op
.proc_get_link
= proc_fd_link
;
1965 d_set_d_op(dentry
, &tid_fd_dentry_operations
);
1966 d_add(dentry
, inode
);
1967 /* Close the race of the process dying before we return the dentry */
1968 if (tid_fd_revalidate(dentry
, NULL
))
1974 spin_unlock(&files
->file_lock
);
1975 put_files_struct(files
);
1981 static struct dentry
*proc_lookupfd_common(struct inode
*dir
,
1982 struct dentry
*dentry
,
1983 instantiate_t instantiate
)
1985 struct task_struct
*task
= get_proc_task(dir
);
1986 unsigned fd
= name_to_int(dentry
);
1987 struct dentry
*result
= ERR_PTR(-ENOENT
);
1994 result
= instantiate(dir
, dentry
, task
, &fd
);
1996 put_task_struct(task
);
2001 static int proc_readfd_common(struct file
* filp
, void * dirent
,
2002 filldir_t filldir
, instantiate_t instantiate
)
2004 struct dentry
*dentry
= filp
->f_path
.dentry
;
2005 struct inode
*inode
= dentry
->d_inode
;
2006 struct task_struct
*p
= get_proc_task(inode
);
2007 unsigned int fd
, ino
;
2009 struct files_struct
* files
;
2019 if (filldir(dirent
, ".", 1, 0, inode
->i_ino
, DT_DIR
) < 0)
2023 ino
= parent_ino(dentry
);
2024 if (filldir(dirent
, "..", 2, 1, ino
, DT_DIR
) < 0)
2028 files
= get_files_struct(p
);
2032 for (fd
= filp
->f_pos
-2;
2033 fd
< files_fdtable(files
)->max_fds
;
2034 fd
++, filp
->f_pos
++) {
2035 char name
[PROC_NUMBUF
];
2038 if (!fcheck_files(files
, fd
))
2042 len
= snprintf(name
, sizeof(name
), "%d", fd
);
2043 if (proc_fill_cache(filp
, dirent
, filldir
,
2044 name
, len
, instantiate
,
2052 put_files_struct(files
);
2060 static struct dentry
*proc_lookupfd(struct inode
*dir
, struct dentry
*dentry
,
2061 struct nameidata
*nd
)
2063 return proc_lookupfd_common(dir
, dentry
, proc_fd_instantiate
);
2066 static int proc_readfd(struct file
*filp
, void *dirent
, filldir_t filldir
)
2068 return proc_readfd_common(filp
, dirent
, filldir
, proc_fd_instantiate
);
2071 static ssize_t
proc_fdinfo_read(struct file
*file
, char __user
*buf
,
2072 size_t len
, loff_t
*ppos
)
2074 char tmp
[PROC_FDINFO_MAX
];
2075 int err
= proc_fd_info(file
->f_path
.dentry
->d_inode
, NULL
, tmp
);
2077 err
= simple_read_from_buffer(buf
, len
, ppos
, tmp
, strlen(tmp
));
2081 static const struct file_operations proc_fdinfo_file_operations
= {
2082 .open
= nonseekable_open
,
2083 .read
= proc_fdinfo_read
,
2084 .llseek
= no_llseek
,
2087 static const struct file_operations proc_fd_operations
= {
2088 .read
= generic_read_dir
,
2089 .readdir
= proc_readfd
,
2090 .llseek
= default_llseek
,
2094 * /proc/pid/fd needs a special permission handler so that a process can still
2095 * access /proc/self/fd after it has executed a setuid().
2097 static int proc_fd_permission(struct inode
*inode
, int mask
, unsigned int flags
)
2099 int rv
= generic_permission(inode
, mask
, flags
, NULL
);
2102 if (task_pid(current
) == proc_pid(inode
))
2108 * proc directories can do almost nothing..
2110 static const struct inode_operations proc_fd_inode_operations
= {
2111 .lookup
= proc_lookupfd
,
2112 .permission
= proc_fd_permission
,
2113 .setattr
= proc_setattr
,
2116 static struct dentry
*proc_fdinfo_instantiate(struct inode
*dir
,
2117 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
2119 unsigned fd
= *(unsigned *)ptr
;
2120 struct inode
*inode
;
2121 struct proc_inode
*ei
;
2122 struct dentry
*error
= ERR_PTR(-ENOENT
);
2124 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
2129 inode
->i_mode
= S_IFREG
| S_IRUSR
;
2130 inode
->i_fop
= &proc_fdinfo_file_operations
;
2131 d_set_d_op(dentry
, &tid_fd_dentry_operations
);
2132 d_add(dentry
, inode
);
2133 /* Close the race of the process dying before we return the dentry */
2134 if (tid_fd_revalidate(dentry
, NULL
))
2141 static struct dentry
*proc_lookupfdinfo(struct inode
*dir
,
2142 struct dentry
*dentry
,
2143 struct nameidata
*nd
)
2145 return proc_lookupfd_common(dir
, dentry
, proc_fdinfo_instantiate
);
2148 static int proc_readfdinfo(struct file
*filp
, void *dirent
, filldir_t filldir
)
2150 return proc_readfd_common(filp
, dirent
, filldir
,
2151 proc_fdinfo_instantiate
);
2154 static const struct file_operations proc_fdinfo_operations
= {
2155 .read
= generic_read_dir
,
2156 .readdir
= proc_readfdinfo
,
2157 .llseek
= default_llseek
,
2161 * proc directories can do almost nothing..
2163 static const struct inode_operations proc_fdinfo_inode_operations
= {
2164 .lookup
= proc_lookupfdinfo
,
2165 .setattr
= proc_setattr
,
2169 static struct dentry
*proc_pident_instantiate(struct inode
*dir
,
2170 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
2172 const struct pid_entry
*p
= ptr
;
2173 struct inode
*inode
;
2174 struct proc_inode
*ei
;
2175 struct dentry
*error
= ERR_PTR(-ENOENT
);
2177 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
2182 inode
->i_mode
= p
->mode
;
2183 if (S_ISDIR(inode
->i_mode
))
2184 inode
->i_nlink
= 2; /* Use getattr to fix if necessary */
2186 inode
->i_op
= p
->iop
;
2188 inode
->i_fop
= p
->fop
;
2190 d_set_d_op(dentry
, &pid_dentry_operations
);
2191 d_add(dentry
, inode
);
2192 /* Close the race of the process dying before we return the dentry */
2193 if (pid_revalidate(dentry
, NULL
))
2199 static struct dentry
*proc_pident_lookup(struct inode
*dir
,
2200 struct dentry
*dentry
,
2201 const struct pid_entry
*ents
,
2204 struct dentry
*error
;
2205 struct task_struct
*task
= get_proc_task(dir
);
2206 const struct pid_entry
*p
, *last
;
2208 error
= ERR_PTR(-ENOENT
);
2214 * Yes, it does not scale. And it should not. Don't add
2215 * new entries into /proc/<tgid>/ without very good reasons.
2217 last
= &ents
[nents
- 1];
2218 for (p
= ents
; p
<= last
; p
++) {
2219 if (p
->len
!= dentry
->d_name
.len
)
2221 if (!memcmp(dentry
->d_name
.name
, p
->name
, p
->len
))
2227 error
= proc_pident_instantiate(dir
, dentry
, task
, p
);
2229 put_task_struct(task
);
2234 static int proc_pident_fill_cache(struct file
*filp
, void *dirent
,
2235 filldir_t filldir
, struct task_struct
*task
, const struct pid_entry
*p
)
2237 return proc_fill_cache(filp
, dirent
, filldir
, p
->name
, p
->len
,
2238 proc_pident_instantiate
, task
, p
);
2241 static int proc_pident_readdir(struct file
*filp
,
2242 void *dirent
, filldir_t filldir
,
2243 const struct pid_entry
*ents
, unsigned int nents
)
2246 struct dentry
*dentry
= filp
->f_path
.dentry
;
2247 struct inode
*inode
= dentry
->d_inode
;
2248 struct task_struct
*task
= get_proc_task(inode
);
2249 const struct pid_entry
*p
, *last
;
2262 if (filldir(dirent
, ".", 1, i
, ino
, DT_DIR
) < 0)
2268 ino
= parent_ino(dentry
);
2269 if (filldir(dirent
, "..", 2, i
, ino
, DT_DIR
) < 0)
2281 last
= &ents
[nents
- 1];
2283 if (proc_pident_fill_cache(filp
, dirent
, filldir
, task
, p
) < 0)
2292 put_task_struct(task
);
2297 #ifdef CONFIG_SECURITY
2298 static ssize_t
proc_pid_attr_read(struct file
* file
, char __user
* buf
,
2299 size_t count
, loff_t
*ppos
)
2301 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
2304 struct task_struct
*task
= get_proc_task(inode
);
2309 length
= security_getprocattr(task
,
2310 (char*)file
->f_path
.dentry
->d_name
.name
,
2312 put_task_struct(task
);
2314 length
= simple_read_from_buffer(buf
, count
, ppos
, p
, length
);
2319 static ssize_t
proc_pid_attr_write(struct file
* file
, const char __user
* buf
,
2320 size_t count
, loff_t
*ppos
)
2322 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
2325 struct task_struct
*task
= get_proc_task(inode
);
2330 if (count
> PAGE_SIZE
)
2333 /* No partial writes. */
2339 page
= (char*)__get_free_page(GFP_TEMPORARY
);
2344 if (copy_from_user(page
, buf
, count
))
2347 /* Guard against adverse ptrace interaction */
2348 length
= mutex_lock_interruptible(&task
->signal
->cred_guard_mutex
);
2352 length
= security_setprocattr(task
,
2353 (char*)file
->f_path
.dentry
->d_name
.name
,
2354 (void*)page
, count
);
2355 mutex_unlock(&task
->signal
->cred_guard_mutex
);
2357 free_page((unsigned long) page
);
2359 put_task_struct(task
);
2364 static const struct file_operations proc_pid_attr_operations
= {
2365 .read
= proc_pid_attr_read
,
2366 .write
= proc_pid_attr_write
,
2367 .llseek
= generic_file_llseek
,
2370 static const struct pid_entry attr_dir_stuff
[] = {
2371 REG("current", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2372 REG("prev", S_IRUGO
, proc_pid_attr_operations
),
2373 REG("exec", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2374 REG("fscreate", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2375 REG("keycreate", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2376 REG("sockcreate", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2379 static int proc_attr_dir_readdir(struct file
* filp
,
2380 void * dirent
, filldir_t filldir
)
2382 return proc_pident_readdir(filp
,dirent
,filldir
,
2383 attr_dir_stuff
,ARRAY_SIZE(attr_dir_stuff
));
2386 static const struct file_operations proc_attr_dir_operations
= {
2387 .read
= generic_read_dir
,
2388 .readdir
= proc_attr_dir_readdir
,
2389 .llseek
= default_llseek
,
2392 static struct dentry
*proc_attr_dir_lookup(struct inode
*dir
,
2393 struct dentry
*dentry
, struct nameidata
*nd
)
2395 return proc_pident_lookup(dir
, dentry
,
2396 attr_dir_stuff
, ARRAY_SIZE(attr_dir_stuff
));
2399 static const struct inode_operations proc_attr_dir_inode_operations
= {
2400 .lookup
= proc_attr_dir_lookup
,
2401 .getattr
= pid_getattr
,
2402 .setattr
= proc_setattr
,
2407 #ifdef CONFIG_ELF_CORE
2408 static ssize_t
proc_coredump_filter_read(struct file
*file
, char __user
*buf
,
2409 size_t count
, loff_t
*ppos
)
2411 struct task_struct
*task
= get_proc_task(file
->f_dentry
->d_inode
);
2412 struct mm_struct
*mm
;
2413 char buffer
[PROC_NUMBUF
];
2421 mm
= get_task_mm(task
);
2423 len
= snprintf(buffer
, sizeof(buffer
), "%08lx\n",
2424 ((mm
->flags
& MMF_DUMP_FILTER_MASK
) >>
2425 MMF_DUMP_FILTER_SHIFT
));
2427 ret
= simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
2430 put_task_struct(task
);
2435 static ssize_t
proc_coredump_filter_write(struct file
*file
,
2436 const char __user
*buf
,
2440 struct task_struct
*task
;
2441 struct mm_struct
*mm
;
2442 char buffer
[PROC_NUMBUF
], *end
;
2449 memset(buffer
, 0, sizeof(buffer
));
2450 if (count
> sizeof(buffer
) - 1)
2451 count
= sizeof(buffer
) - 1;
2452 if (copy_from_user(buffer
, buf
, count
))
2456 val
= (unsigned int)simple_strtoul(buffer
, &end
, 0);
2459 if (end
- buffer
== 0)
2463 task
= get_proc_task(file
->f_dentry
->d_inode
);
2468 mm
= get_task_mm(task
);
2472 for (i
= 0, mask
= 1; i
< MMF_DUMP_FILTER_BITS
; i
++, mask
<<= 1) {
2474 set_bit(i
+ MMF_DUMP_FILTER_SHIFT
, &mm
->flags
);
2476 clear_bit(i
+ MMF_DUMP_FILTER_SHIFT
, &mm
->flags
);
2481 put_task_struct(task
);
2486 static const struct file_operations proc_coredump_filter_operations
= {
2487 .read
= proc_coredump_filter_read
,
2488 .write
= proc_coredump_filter_write
,
2489 .llseek
= generic_file_llseek
,
2496 static int proc_self_readlink(struct dentry
*dentry
, char __user
*buffer
,
2499 struct pid_namespace
*ns
= dentry
->d_sb
->s_fs_info
;
2500 pid_t tgid
= task_tgid_nr_ns(current
, ns
);
2501 char tmp
[PROC_NUMBUF
];
2504 sprintf(tmp
, "%d", tgid
);
2505 return vfs_readlink(dentry
,buffer
,buflen
,tmp
);
2508 static void *proc_self_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
2510 struct pid_namespace
*ns
= dentry
->d_sb
->s_fs_info
;
2511 pid_t tgid
= task_tgid_nr_ns(current
, ns
);
2512 char *name
= ERR_PTR(-ENOENT
);
2516 name
= ERR_PTR(-ENOMEM
);
2518 sprintf(name
, "%d", tgid
);
2520 nd_set_link(nd
, name
);
2524 static void proc_self_put_link(struct dentry
*dentry
, struct nameidata
*nd
,
2527 char *s
= nd_get_link(nd
);
2532 static const struct inode_operations proc_self_inode_operations
= {
2533 .readlink
= proc_self_readlink
,
2534 .follow_link
= proc_self_follow_link
,
2535 .put_link
= proc_self_put_link
,
2541 * These are the directory entries in the root directory of /proc
2542 * that properly belong to the /proc filesystem, as they describe
2543 * describe something that is process related.
2545 static const struct pid_entry proc_base_stuff
[] = {
2546 NOD("self", S_IFLNK
|S_IRWXUGO
,
2547 &proc_self_inode_operations
, NULL
, {}),
2550 static struct dentry
*proc_base_instantiate(struct inode
*dir
,
2551 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
2553 const struct pid_entry
*p
= ptr
;
2554 struct inode
*inode
;
2555 struct proc_inode
*ei
;
2556 struct dentry
*error
;
2558 /* Allocate the inode */
2559 error
= ERR_PTR(-ENOMEM
);
2560 inode
= new_inode(dir
->i_sb
);
2564 /* Initialize the inode */
2566 inode
->i_ino
= get_next_ino();
2567 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
2570 * grab the reference to the task.
2572 ei
->pid
= get_task_pid(task
, PIDTYPE_PID
);
2576 inode
->i_mode
= p
->mode
;
2577 if (S_ISDIR(inode
->i_mode
))
2579 if (S_ISLNK(inode
->i_mode
))
2582 inode
->i_op
= p
->iop
;
2584 inode
->i_fop
= p
->fop
;
2586 d_add(dentry
, inode
);
2595 static struct dentry
*proc_base_lookup(struct inode
*dir
, struct dentry
*dentry
)
2597 struct dentry
*error
;
2598 struct task_struct
*task
= get_proc_task(dir
);
2599 const struct pid_entry
*p
, *last
;
2601 error
= ERR_PTR(-ENOENT
);
2606 /* Lookup the directory entry */
2607 last
= &proc_base_stuff
[ARRAY_SIZE(proc_base_stuff
) - 1];
2608 for (p
= proc_base_stuff
; p
<= last
; p
++) {
2609 if (p
->len
!= dentry
->d_name
.len
)
2611 if (!memcmp(dentry
->d_name
.name
, p
->name
, p
->len
))
2617 error
= proc_base_instantiate(dir
, dentry
, task
, p
);
2620 put_task_struct(task
);
2625 static int proc_base_fill_cache(struct file
*filp
, void *dirent
,
2626 filldir_t filldir
, struct task_struct
*task
, const struct pid_entry
*p
)
2628 return proc_fill_cache(filp
, dirent
, filldir
, p
->name
, p
->len
,
2629 proc_base_instantiate
, task
, p
);
2632 #ifdef CONFIG_TASK_IO_ACCOUNTING
2633 static int do_io_accounting(struct task_struct
*task
, char *buffer
, int whole
)
2635 struct task_io_accounting acct
= task
->ioac
;
2636 unsigned long flags
;
2639 result
= mutex_lock_killable(&task
->signal
->cred_guard_mutex
);
2643 if (!ptrace_may_access(task
, PTRACE_MODE_READ
)) {
2648 if (whole
&& lock_task_sighand(task
, &flags
)) {
2649 struct task_struct
*t
= task
;
2651 task_io_accounting_add(&acct
, &task
->signal
->ioac
);
2652 while_each_thread(task
, t
)
2653 task_io_accounting_add(&acct
, &t
->ioac
);
2655 unlock_task_sighand(task
, &flags
);
2657 result
= sprintf(buffer
,
2662 "read_bytes: %llu\n"
2663 "write_bytes: %llu\n"
2664 "cancelled_write_bytes: %llu\n",
2665 (unsigned long long)acct
.rchar
,
2666 (unsigned long long)acct
.wchar
,
2667 (unsigned long long)acct
.syscr
,
2668 (unsigned long long)acct
.syscw
,
2669 (unsigned long long)acct
.read_bytes
,
2670 (unsigned long long)acct
.write_bytes
,
2671 (unsigned long long)acct
.cancelled_write_bytes
);
2673 mutex_unlock(&task
->signal
->cred_guard_mutex
);
2677 static int proc_tid_io_accounting(struct task_struct
*task
, char *buffer
)
2679 return do_io_accounting(task
, buffer
, 0);
2682 static int proc_tgid_io_accounting(struct task_struct
*task
, char *buffer
)
2684 return do_io_accounting(task
, buffer
, 1);
2686 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2688 static int proc_pid_personality(struct seq_file
*m
, struct pid_namespace
*ns
,
2689 struct pid
*pid
, struct task_struct
*task
)
2691 int err
= lock_trace(task
);
2693 seq_printf(m
, "%08x\n", task
->personality
);
2702 static const struct file_operations proc_task_operations
;
2703 static const struct inode_operations proc_task_inode_operations
;
2705 static const struct pid_entry tgid_base_stuff
[] = {
2706 DIR("task", S_IRUGO
|S_IXUGO
, proc_task_inode_operations
, proc_task_operations
),
2707 DIR("fd", S_IRUSR
|S_IXUSR
, proc_fd_inode_operations
, proc_fd_operations
),
2708 DIR("fdinfo", S_IRUSR
|S_IXUSR
, proc_fdinfo_inode_operations
, proc_fdinfo_operations
),
2709 DIR("ns", S_IRUSR
|S_IXUGO
, proc_ns_dir_inode_operations
, proc_ns_dir_operations
),
2711 DIR("net", S_IRUGO
|S_IXUGO
, proc_net_inode_operations
, proc_net_operations
),
2713 REG("environ", S_IRUSR
, proc_environ_operations
),
2714 INF("auxv", S_IRUSR
, proc_pid_auxv
),
2715 ONE("status", S_IRUGO
, proc_pid_status
),
2716 ONE("personality", S_IRUGO
, proc_pid_personality
),
2717 INF("limits", S_IRUGO
, proc_pid_limits
),
2718 #ifdef CONFIG_SCHED_DEBUG
2719 REG("sched", S_IRUGO
|S_IWUSR
, proc_pid_sched_operations
),
2721 #ifdef CONFIG_SCHED_AUTOGROUP
2722 REG("autogroup", S_IRUGO
|S_IWUSR
, proc_pid_sched_autogroup_operations
),
2724 REG("comm", S_IRUGO
|S_IWUSR
, proc_pid_set_comm_operations
),
2725 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2726 INF("syscall", S_IRUGO
, proc_pid_syscall
),
2728 INF("cmdline", S_IRUGO
, proc_pid_cmdline
),
2729 ONE("stat", S_IRUGO
, proc_tgid_stat
),
2730 ONE("statm", S_IRUGO
, proc_pid_statm
),
2731 REG("maps", S_IRUGO
, proc_maps_operations
),
2733 REG("numa_maps", S_IRUGO
, proc_numa_maps_operations
),
2735 REG("mem", S_IRUSR
|S_IWUSR
, proc_mem_operations
),
2736 LNK("cwd", proc_cwd_link
),
2737 LNK("root", proc_root_link
),
2738 LNK("exe", proc_exe_link
),
2739 REG("mounts", S_IRUGO
, proc_mounts_operations
),
2740 REG("mountinfo", S_IRUGO
, proc_mountinfo_operations
),
2741 REG("mountstats", S_IRUSR
, proc_mountstats_operations
),
2742 #ifdef CONFIG_PROC_PAGE_MONITOR
2743 REG("clear_refs", S_IWUSR
, proc_clear_refs_operations
),
2744 REG("smaps", S_IRUGO
, proc_smaps_operations
),
2745 REG("pagemap", S_IRUGO
, proc_pagemap_operations
),
2747 #ifdef CONFIG_SECURITY
2748 DIR("attr", S_IRUGO
|S_IXUGO
, proc_attr_dir_inode_operations
, proc_attr_dir_operations
),
2750 #ifdef CONFIG_KALLSYMS
2751 INF("wchan", S_IRUGO
, proc_pid_wchan
),
2753 #ifdef CONFIG_STACKTRACE
2754 ONE("stack", S_IRUGO
, proc_pid_stack
),
2756 #ifdef CONFIG_SCHEDSTATS
2757 INF("schedstat", S_IRUGO
, proc_pid_schedstat
),
2759 #ifdef CONFIG_LATENCYTOP
2760 REG("latency", S_IRUGO
, proc_lstats_operations
),
2762 #ifdef CONFIG_PROC_PID_CPUSET
2763 REG("cpuset", S_IRUGO
, proc_cpuset_operations
),
2765 #ifdef CONFIG_CGROUPS
2766 REG("cgroup", S_IRUGO
, proc_cgroup_operations
),
2768 INF("oom_score", S_IRUGO
, proc_oom_score
),
2769 REG("oom_adj", S_IRUGO
|S_IWUSR
, proc_oom_adjust_operations
),
2770 REG("oom_score_adj", S_IRUGO
|S_IWUSR
, proc_oom_score_adj_operations
),
2771 #ifdef CONFIG_AUDITSYSCALL
2772 REG("loginuid", S_IWUSR
|S_IRUGO
, proc_loginuid_operations
),
2773 REG("sessionid", S_IRUGO
, proc_sessionid_operations
),
2775 #ifdef CONFIG_FAULT_INJECTION
2776 REG("make-it-fail", S_IRUGO
|S_IWUSR
, proc_fault_inject_operations
),
2778 #ifdef CONFIG_ELF_CORE
2779 REG("coredump_filter", S_IRUGO
|S_IWUSR
, proc_coredump_filter_operations
),
2781 #ifdef CONFIG_TASK_IO_ACCOUNTING
2782 INF("io", S_IRUSR
, proc_tgid_io_accounting
),
2784 #ifdef CONFIG_HARDWALL
2785 INF("hardwall", S_IRUGO
, proc_pid_hardwall
),
2789 static int proc_tgid_base_readdir(struct file
* filp
,
2790 void * dirent
, filldir_t filldir
)
2792 return proc_pident_readdir(filp
,dirent
,filldir
,
2793 tgid_base_stuff
,ARRAY_SIZE(tgid_base_stuff
));
2796 static const struct file_operations proc_tgid_base_operations
= {
2797 .read
= generic_read_dir
,
2798 .readdir
= proc_tgid_base_readdir
,
2799 .llseek
= default_llseek
,
2802 static struct dentry
*proc_tgid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
){
2803 return proc_pident_lookup(dir
, dentry
,
2804 tgid_base_stuff
, ARRAY_SIZE(tgid_base_stuff
));
2807 static const struct inode_operations proc_tgid_base_inode_operations
= {
2808 .lookup
= proc_tgid_base_lookup
,
2809 .getattr
= pid_getattr
,
2810 .setattr
= proc_setattr
,
2813 static void proc_flush_task_mnt(struct vfsmount
*mnt
, pid_t pid
, pid_t tgid
)
2815 struct dentry
*dentry
, *leader
, *dir
;
2816 char buf
[PROC_NUMBUF
];
2820 name
.len
= snprintf(buf
, sizeof(buf
), "%d", pid
);
2821 dentry
= d_hash_and_lookup(mnt
->mnt_root
, &name
);
2823 shrink_dcache_parent(dentry
);
2829 name
.len
= snprintf(buf
, sizeof(buf
), "%d", tgid
);
2830 leader
= d_hash_and_lookup(mnt
->mnt_root
, &name
);
2835 name
.len
= strlen(name
.name
);
2836 dir
= d_hash_and_lookup(leader
, &name
);
2838 goto out_put_leader
;
2841 name
.len
= snprintf(buf
, sizeof(buf
), "%d", pid
);
2842 dentry
= d_hash_and_lookup(dir
, &name
);
2844 shrink_dcache_parent(dentry
);
2857 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2858 * @task: task that should be flushed.
2860 * When flushing dentries from proc, one needs to flush them from global
2861 * proc (proc_mnt) and from all the namespaces' procs this task was seen
2862 * in. This call is supposed to do all of this job.
2864 * Looks in the dcache for
2866 * /proc/@tgid/task/@pid
2867 * if either directory is present flushes it and all of it'ts children
2870 * It is safe and reasonable to cache /proc entries for a task until
2871 * that task exits. After that they just clog up the dcache with
2872 * useless entries, possibly causing useful dcache entries to be
2873 * flushed instead. This routine is proved to flush those useless
2874 * dcache entries at process exit time.
2876 * NOTE: This routine is just an optimization so it does not guarantee
2877 * that no dcache entries will exist at process exit time it
2878 * just makes it very unlikely that any will persist.
2881 void proc_flush_task(struct task_struct
*task
)
2884 struct pid
*pid
, *tgid
;
2887 pid
= task_pid(task
);
2888 tgid
= task_tgid(task
);
2890 for (i
= 0; i
<= pid
->level
; i
++) {
2891 upid
= &pid
->numbers
[i
];
2892 proc_flush_task_mnt(upid
->ns
->proc_mnt
, upid
->nr
,
2893 tgid
->numbers
[i
].nr
);
2896 upid
= &pid
->numbers
[pid
->level
];
2898 pid_ns_release_proc(upid
->ns
);
2901 static struct dentry
*proc_pid_instantiate(struct inode
*dir
,
2902 struct dentry
* dentry
,
2903 struct task_struct
*task
, const void *ptr
)
2905 struct dentry
*error
= ERR_PTR(-ENOENT
);
2906 struct inode
*inode
;
2908 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
2912 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
2913 inode
->i_op
= &proc_tgid_base_inode_operations
;
2914 inode
->i_fop
= &proc_tgid_base_operations
;
2915 inode
->i_flags
|=S_IMMUTABLE
;
2917 inode
->i_nlink
= 2 + pid_entry_count_dirs(tgid_base_stuff
,
2918 ARRAY_SIZE(tgid_base_stuff
));
2920 d_set_d_op(dentry
, &pid_dentry_operations
);
2922 d_add(dentry
, inode
);
2923 /* Close the race of the process dying before we return the dentry */
2924 if (pid_revalidate(dentry
, NULL
))
2930 struct dentry
*proc_pid_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
)
2932 struct dentry
*result
;
2933 struct task_struct
*task
;
2935 struct pid_namespace
*ns
;
2937 result
= proc_base_lookup(dir
, dentry
);
2938 if (!IS_ERR(result
) || PTR_ERR(result
) != -ENOENT
)
2941 tgid
= name_to_int(dentry
);
2945 ns
= dentry
->d_sb
->s_fs_info
;
2947 task
= find_task_by_pid_ns(tgid
, ns
);
2949 get_task_struct(task
);
2954 result
= proc_pid_instantiate(dir
, dentry
, task
, NULL
);
2955 put_task_struct(task
);
2961 * Find the first task with tgid >= tgid
2966 struct task_struct
*task
;
2968 static struct tgid_iter
next_tgid(struct pid_namespace
*ns
, struct tgid_iter iter
)
2973 put_task_struct(iter
.task
);
2977 pid
= find_ge_pid(iter
.tgid
, ns
);
2979 iter
.tgid
= pid_nr_ns(pid
, ns
);
2980 iter
.task
= pid_task(pid
, PIDTYPE_PID
);
2981 /* What we to know is if the pid we have find is the
2982 * pid of a thread_group_leader. Testing for task
2983 * being a thread_group_leader is the obvious thing
2984 * todo but there is a window when it fails, due to
2985 * the pid transfer logic in de_thread.
2987 * So we perform the straight forward test of seeing
2988 * if the pid we have found is the pid of a thread
2989 * group leader, and don't worry if the task we have
2990 * found doesn't happen to be a thread group leader.
2991 * As we don't care in the case of readdir.
2993 if (!iter
.task
|| !has_group_leader_pid(iter
.task
)) {
2997 get_task_struct(iter
.task
);
3003 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
3005 static int proc_pid_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
3006 struct tgid_iter iter
)
3008 char name
[PROC_NUMBUF
];
3009 int len
= snprintf(name
, sizeof(name
), "%d", iter
.tgid
);
3010 return proc_fill_cache(filp
, dirent
, filldir
, name
, len
,
3011 proc_pid_instantiate
, iter
.task
, NULL
);
3014 /* for the /proc/ directory itself, after non-process stuff has been done */
3015 int proc_pid_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
3018 struct task_struct
*reaper
;
3019 struct tgid_iter iter
;
3020 struct pid_namespace
*ns
;
3022 if (filp
->f_pos
>= PID_MAX_LIMIT
+ TGID_OFFSET
)
3024 nr
= filp
->f_pos
- FIRST_PROCESS_ENTRY
;
3026 reaper
= get_proc_task(filp
->f_path
.dentry
->d_inode
);
3030 for (; nr
< ARRAY_SIZE(proc_base_stuff
); filp
->f_pos
++, nr
++) {
3031 const struct pid_entry
*p
= &proc_base_stuff
[nr
];
3032 if (proc_base_fill_cache(filp
, dirent
, filldir
, reaper
, p
) < 0)
3036 ns
= filp
->f_dentry
->d_sb
->s_fs_info
;
3038 iter
.tgid
= filp
->f_pos
- TGID_OFFSET
;
3039 for (iter
= next_tgid(ns
, iter
);
3041 iter
.tgid
+= 1, iter
= next_tgid(ns
, iter
)) {
3042 filp
->f_pos
= iter
.tgid
+ TGID_OFFSET
;
3043 if (proc_pid_fill_cache(filp
, dirent
, filldir
, iter
) < 0) {
3044 put_task_struct(iter
.task
);
3048 filp
->f_pos
= PID_MAX_LIMIT
+ TGID_OFFSET
;
3050 put_task_struct(reaper
);
3058 static const struct pid_entry tid_base_stuff
[] = {
3059 DIR("fd", S_IRUSR
|S_IXUSR
, proc_fd_inode_operations
, proc_fd_operations
),
3060 DIR("fdinfo", S_IRUSR
|S_IXUSR
, proc_fdinfo_inode_operations
, proc_fdinfo_operations
),
3061 DIR("ns", S_IRUSR
|S_IXUGO
, proc_ns_dir_inode_operations
, proc_ns_dir_operations
),
3062 REG("environ", S_IRUSR
, proc_environ_operations
),
3063 INF("auxv", S_IRUSR
, proc_pid_auxv
),
3064 ONE("status", S_IRUGO
, proc_pid_status
),
3065 ONE("personality", S_IRUGO
, proc_pid_personality
),
3066 INF("limits", S_IRUGO
, proc_pid_limits
),
3067 #ifdef CONFIG_SCHED_DEBUG
3068 REG("sched", S_IRUGO
|S_IWUSR
, proc_pid_sched_operations
),
3070 REG("comm", S_IRUGO
|S_IWUSR
, proc_pid_set_comm_operations
),
3071 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3072 INF("syscall", S_IRUGO
, proc_pid_syscall
),
3074 INF("cmdline", S_IRUGO
, proc_pid_cmdline
),
3075 ONE("stat", S_IRUGO
, proc_tid_stat
),
3076 ONE("statm", S_IRUGO
, proc_pid_statm
),
3077 REG("maps", S_IRUGO
, proc_maps_operations
),
3079 REG("numa_maps", S_IRUGO
, proc_numa_maps_operations
),
3081 REG("mem", S_IRUSR
|S_IWUSR
, proc_mem_operations
),
3082 LNK("cwd", proc_cwd_link
),
3083 LNK("root", proc_root_link
),
3084 LNK("exe", proc_exe_link
),
3085 REG("mounts", S_IRUGO
, proc_mounts_operations
),
3086 REG("mountinfo", S_IRUGO
, proc_mountinfo_operations
),
3087 #ifdef CONFIG_PROC_PAGE_MONITOR
3088 REG("clear_refs", S_IWUSR
, proc_clear_refs_operations
),
3089 REG("smaps", S_IRUGO
, proc_smaps_operations
),
3090 REG("pagemap", S_IRUGO
, proc_pagemap_operations
),
3092 #ifdef CONFIG_SECURITY
3093 DIR("attr", S_IRUGO
|S_IXUGO
, proc_attr_dir_inode_operations
, proc_attr_dir_operations
),
3095 #ifdef CONFIG_KALLSYMS
3096 INF("wchan", S_IRUGO
, proc_pid_wchan
),
3098 #ifdef CONFIG_STACKTRACE
3099 ONE("stack", S_IRUGO
, proc_pid_stack
),
3101 #ifdef CONFIG_SCHEDSTATS
3102 INF("schedstat", S_IRUGO
, proc_pid_schedstat
),
3104 #ifdef CONFIG_LATENCYTOP
3105 REG("latency", S_IRUGO
, proc_lstats_operations
),
3107 #ifdef CONFIG_PROC_PID_CPUSET
3108 REG("cpuset", S_IRUGO
, proc_cpuset_operations
),
3110 #ifdef CONFIG_CGROUPS
3111 REG("cgroup", S_IRUGO
, proc_cgroup_operations
),
3113 INF("oom_score", S_IRUGO
, proc_oom_score
),
3114 REG("oom_adj", S_IRUGO
|S_IWUSR
, proc_oom_adjust_operations
),
3115 REG("oom_score_adj", S_IRUGO
|S_IWUSR
, proc_oom_score_adj_operations
),
3116 #ifdef CONFIG_AUDITSYSCALL
3117 REG("loginuid", S_IWUSR
|S_IRUGO
, proc_loginuid_operations
),
3118 REG("sessionid", S_IRUGO
, proc_sessionid_operations
),
3120 #ifdef CONFIG_FAULT_INJECTION
3121 REG("make-it-fail", S_IRUGO
|S_IWUSR
, proc_fault_inject_operations
),
3123 #ifdef CONFIG_TASK_IO_ACCOUNTING
3124 INF("io", S_IRUSR
, proc_tid_io_accounting
),
3126 #ifdef CONFIG_HARDWALL
3127 INF("hardwall", S_IRUGO
, proc_pid_hardwall
),
3131 static int proc_tid_base_readdir(struct file
* filp
,
3132 void * dirent
, filldir_t filldir
)
3134 return proc_pident_readdir(filp
,dirent
,filldir
,
3135 tid_base_stuff
,ARRAY_SIZE(tid_base_stuff
));
3138 static struct dentry
*proc_tid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
){
3139 return proc_pident_lookup(dir
, dentry
,
3140 tid_base_stuff
, ARRAY_SIZE(tid_base_stuff
));
3143 static const struct file_operations proc_tid_base_operations
= {
3144 .read
= generic_read_dir
,
3145 .readdir
= proc_tid_base_readdir
,
3146 .llseek
= default_llseek
,
3149 static const struct inode_operations proc_tid_base_inode_operations
= {
3150 .lookup
= proc_tid_base_lookup
,
3151 .getattr
= pid_getattr
,
3152 .setattr
= proc_setattr
,
3155 static struct dentry
*proc_task_instantiate(struct inode
*dir
,
3156 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
3158 struct dentry
*error
= ERR_PTR(-ENOENT
);
3159 struct inode
*inode
;
3160 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
3164 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
3165 inode
->i_op
= &proc_tid_base_inode_operations
;
3166 inode
->i_fop
= &proc_tid_base_operations
;
3167 inode
->i_flags
|=S_IMMUTABLE
;
3169 inode
->i_nlink
= 2 + pid_entry_count_dirs(tid_base_stuff
,
3170 ARRAY_SIZE(tid_base_stuff
));
3172 d_set_d_op(dentry
, &pid_dentry_operations
);
3174 d_add(dentry
, inode
);
3175 /* Close the race of the process dying before we return the dentry */
3176 if (pid_revalidate(dentry
, NULL
))
3182 static struct dentry
*proc_task_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
)
3184 struct dentry
*result
= ERR_PTR(-ENOENT
);
3185 struct task_struct
*task
;
3186 struct task_struct
*leader
= get_proc_task(dir
);
3188 struct pid_namespace
*ns
;
3193 tid
= name_to_int(dentry
);
3197 ns
= dentry
->d_sb
->s_fs_info
;
3199 task
= find_task_by_pid_ns(tid
, ns
);
3201 get_task_struct(task
);
3205 if (!same_thread_group(leader
, task
))
3208 result
= proc_task_instantiate(dir
, dentry
, task
, NULL
);
3210 put_task_struct(task
);
3212 put_task_struct(leader
);
3218 * Find the first tid of a thread group to return to user space.
3220 * Usually this is just the thread group leader, but if the users
3221 * buffer was too small or there was a seek into the middle of the
3222 * directory we have more work todo.
3224 * In the case of a short read we start with find_task_by_pid.
3226 * In the case of a seek we start with the leader and walk nr
3229 static struct task_struct
*first_tid(struct task_struct
*leader
,
3230 int tid
, int nr
, struct pid_namespace
*ns
)
3232 struct task_struct
*pos
;
3235 /* Attempt to start with the pid of a thread */
3236 if (tid
&& (nr
> 0)) {
3237 pos
= find_task_by_pid_ns(tid
, ns
);
3238 if (pos
&& (pos
->group_leader
== leader
))
3242 /* If nr exceeds the number of threads there is nothing todo */
3244 if (nr
&& nr
>= get_nr_threads(leader
))
3247 /* If we haven't found our starting place yet start
3248 * with the leader and walk nr threads forward.
3250 for (pos
= leader
; nr
> 0; --nr
) {
3251 pos
= next_thread(pos
);
3252 if (pos
== leader
) {
3258 get_task_struct(pos
);
3265 * Find the next thread in the thread list.
3266 * Return NULL if there is an error or no next thread.
3268 * The reference to the input task_struct is released.
3270 static struct task_struct
*next_tid(struct task_struct
*start
)
3272 struct task_struct
*pos
= NULL
;
3274 if (pid_alive(start
)) {
3275 pos
= next_thread(start
);
3276 if (thread_group_leader(pos
))
3279 get_task_struct(pos
);
3282 put_task_struct(start
);
3286 static int proc_task_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
3287 struct task_struct
*task
, int tid
)
3289 char name
[PROC_NUMBUF
];
3290 int len
= snprintf(name
, sizeof(name
), "%d", tid
);
3291 return proc_fill_cache(filp
, dirent
, filldir
, name
, len
,
3292 proc_task_instantiate
, task
, NULL
);
3295 /* for the /proc/TGID/task/ directories */
3296 static int proc_task_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
3298 struct dentry
*dentry
= filp
->f_path
.dentry
;
3299 struct inode
*inode
= dentry
->d_inode
;
3300 struct task_struct
*leader
= NULL
;
3301 struct task_struct
*task
;
3302 int retval
= -ENOENT
;
3305 struct pid_namespace
*ns
;
3307 task
= get_proc_task(inode
);
3311 if (pid_alive(task
)) {
3312 leader
= task
->group_leader
;
3313 get_task_struct(leader
);
3316 put_task_struct(task
);
3321 switch ((unsigned long)filp
->f_pos
) {
3324 if (filldir(dirent
, ".", 1, filp
->f_pos
, ino
, DT_DIR
) < 0)
3329 ino
= parent_ino(dentry
);
3330 if (filldir(dirent
, "..", 2, filp
->f_pos
, ino
, DT_DIR
) < 0)
3336 /* f_version caches the tgid value that the last readdir call couldn't
3337 * return. lseek aka telldir automagically resets f_version to 0.
3339 ns
= filp
->f_dentry
->d_sb
->s_fs_info
;
3340 tid
= (int)filp
->f_version
;
3341 filp
->f_version
= 0;
3342 for (task
= first_tid(leader
, tid
, filp
->f_pos
- 2, ns
);
3344 task
= next_tid(task
), filp
->f_pos
++) {
3345 tid
= task_pid_nr_ns(task
, ns
);
3346 if (proc_task_fill_cache(filp
, dirent
, filldir
, task
, tid
) < 0) {
3347 /* returning this tgid failed, save it as the first
3348 * pid for the next readir call */
3349 filp
->f_version
= (u64
)tid
;
3350 put_task_struct(task
);
3355 put_task_struct(leader
);
3360 static int proc_task_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
3362 struct inode
*inode
= dentry
->d_inode
;
3363 struct task_struct
*p
= get_proc_task(inode
);
3364 generic_fillattr(inode
, stat
);
3367 stat
->nlink
+= get_nr_threads(p
);
3374 static const struct inode_operations proc_task_inode_operations
= {
3375 .lookup
= proc_task_lookup
,
3376 .getattr
= proc_task_getattr
,
3377 .setattr
= proc_setattr
,
3380 static const struct file_operations proc_task_operations
= {
3381 .read
= generic_read_dir
,
3382 .readdir
= proc_task_readdir
,
3383 .llseek
= default_llseek
,