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
);
778 /* OK to pass negative loff_t, we can catch out-of-range */
779 file
->f_mode
|= FMODE_UNSIGNED_OFFSET
;
780 file
->private_data
= mm
;
785 static ssize_t
mem_read(struct file
* file
, char __user
* buf
,
786 size_t count
, loff_t
*ppos
)
790 unsigned long src
= *ppos
;
791 struct mm_struct
*mm
= file
->private_data
;
796 page
= (char *)__get_free_page(GFP_TEMPORARY
);
803 int this_len
, retval
;
805 this_len
= (count
> PAGE_SIZE
) ? PAGE_SIZE
: count
;
806 retval
= access_remote_vm(mm
, src
, page
, this_len
, 0);
813 if (copy_to_user(buf
, page
, retval
)) {
825 free_page((unsigned long) page
);
829 static ssize_t
mem_write(struct file
* file
, const char __user
*buf
,
830 size_t count
, loff_t
*ppos
)
834 unsigned long dst
= *ppos
;
835 struct mm_struct
*mm
= file
->private_data
;
840 page
= (char *)__get_free_page(GFP_TEMPORARY
);
846 int this_len
, retval
;
848 this_len
= (count
> PAGE_SIZE
) ? PAGE_SIZE
: count
;
849 if (copy_from_user(page
, buf
, this_len
)) {
853 retval
= access_remote_vm(mm
, dst
, page
, this_len
, 1);
866 free_page((unsigned long) page
);
870 loff_t
mem_lseek(struct file
*file
, loff_t offset
, int orig
)
874 file
->f_pos
= offset
;
877 file
->f_pos
+= offset
;
882 force_successful_syscall_return();
886 static int mem_release(struct inode
*inode
, struct file
*file
)
888 struct mm_struct
*mm
= file
->private_data
;
894 static const struct file_operations proc_mem_operations
= {
899 .release
= mem_release
,
902 static ssize_t
environ_read(struct file
*file
, char __user
*buf
,
903 size_t count
, loff_t
*ppos
)
905 struct task_struct
*task
= get_proc_task(file
->f_dentry
->d_inode
);
907 unsigned long src
= *ppos
;
909 struct mm_struct
*mm
;
915 page
= (char *)__get_free_page(GFP_TEMPORARY
);
920 mm
= mm_for_maps(task
);
922 if (!mm
|| IS_ERR(mm
))
927 int this_len
, retval
, max_len
;
929 this_len
= mm
->env_end
- (mm
->env_start
+ src
);
934 max_len
= (count
> PAGE_SIZE
) ? PAGE_SIZE
: count
;
935 this_len
= (this_len
> max_len
) ? max_len
: this_len
;
937 retval
= access_process_vm(task
, (mm
->env_start
+ src
),
945 if (copy_to_user(buf
, page
, retval
)) {
959 free_page((unsigned long) page
);
961 put_task_struct(task
);
966 static const struct file_operations proc_environ_operations
= {
967 .read
= environ_read
,
968 .llseek
= generic_file_llseek
,
971 static ssize_t
oom_adjust_read(struct file
*file
, char __user
*buf
,
972 size_t count
, loff_t
*ppos
)
974 struct task_struct
*task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
975 char buffer
[PROC_NUMBUF
];
977 int oom_adjust
= OOM_DISABLE
;
983 if (lock_task_sighand(task
, &flags
)) {
984 oom_adjust
= task
->signal
->oom_adj
;
985 unlock_task_sighand(task
, &flags
);
988 put_task_struct(task
);
990 len
= snprintf(buffer
, sizeof(buffer
), "%i\n", oom_adjust
);
992 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
995 static ssize_t
oom_adjust_write(struct file
*file
, const char __user
*buf
,
996 size_t count
, loff_t
*ppos
)
998 struct task_struct
*task
;
999 char buffer
[PROC_NUMBUF
];
1001 unsigned long flags
;
1004 memset(buffer
, 0, sizeof(buffer
));
1005 if (count
> sizeof(buffer
) - 1)
1006 count
= sizeof(buffer
) - 1;
1007 if (copy_from_user(buffer
, buf
, count
)) {
1012 err
= kstrtoint(strstrip(buffer
), 0, &oom_adjust
);
1015 if ((oom_adjust
< OOM_ADJUST_MIN
|| oom_adjust
> OOM_ADJUST_MAX
) &&
1016 oom_adjust
!= OOM_DISABLE
) {
1021 task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
1033 if (!lock_task_sighand(task
, &flags
)) {
1038 if (oom_adjust
< task
->signal
->oom_adj
&& !capable(CAP_SYS_RESOURCE
)) {
1043 if (oom_adjust
!= task
->signal
->oom_adj
) {
1044 if (oom_adjust
== OOM_DISABLE
)
1045 atomic_inc(&task
->mm
->oom_disable_count
);
1046 if (task
->signal
->oom_adj
== OOM_DISABLE
)
1047 atomic_dec(&task
->mm
->oom_disable_count
);
1051 * Warn that /proc/pid/oom_adj is deprecated, see
1052 * Documentation/feature-removal-schedule.txt.
1054 printk_once(KERN_WARNING
"%s (%d): /proc/%d/oom_adj is deprecated, "
1055 "please use /proc/%d/oom_score_adj instead.\n",
1056 current
->comm
, task_pid_nr(current
),
1057 task_pid_nr(task
), task_pid_nr(task
));
1058 task
->signal
->oom_adj
= oom_adjust
;
1060 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1061 * value is always attainable.
1063 if (task
->signal
->oom_adj
== OOM_ADJUST_MAX
)
1064 task
->signal
->oom_score_adj
= OOM_SCORE_ADJ_MAX
;
1066 task
->signal
->oom_score_adj
= (oom_adjust
* OOM_SCORE_ADJ_MAX
) /
1069 unlock_task_sighand(task
, &flags
);
1072 put_task_struct(task
);
1074 return err
< 0 ? err
: count
;
1077 static const struct file_operations proc_oom_adjust_operations
= {
1078 .read
= oom_adjust_read
,
1079 .write
= oom_adjust_write
,
1080 .llseek
= generic_file_llseek
,
1083 static ssize_t
oom_score_adj_read(struct file
*file
, char __user
*buf
,
1084 size_t count
, loff_t
*ppos
)
1086 struct task_struct
*task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
1087 char buffer
[PROC_NUMBUF
];
1088 int oom_score_adj
= OOM_SCORE_ADJ_MIN
;
1089 unsigned long flags
;
1094 if (lock_task_sighand(task
, &flags
)) {
1095 oom_score_adj
= task
->signal
->oom_score_adj
;
1096 unlock_task_sighand(task
, &flags
);
1098 put_task_struct(task
);
1099 len
= snprintf(buffer
, sizeof(buffer
), "%d\n", oom_score_adj
);
1100 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
1103 static ssize_t
oom_score_adj_write(struct file
*file
, const char __user
*buf
,
1104 size_t count
, loff_t
*ppos
)
1106 struct task_struct
*task
;
1107 char buffer
[PROC_NUMBUF
];
1108 unsigned long flags
;
1112 memset(buffer
, 0, sizeof(buffer
));
1113 if (count
> sizeof(buffer
) - 1)
1114 count
= sizeof(buffer
) - 1;
1115 if (copy_from_user(buffer
, buf
, count
)) {
1120 err
= kstrtoint(strstrip(buffer
), 0, &oom_score_adj
);
1123 if (oom_score_adj
< OOM_SCORE_ADJ_MIN
||
1124 oom_score_adj
> OOM_SCORE_ADJ_MAX
) {
1129 task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
1141 if (!lock_task_sighand(task
, &flags
)) {
1146 if (oom_score_adj
< task
->signal
->oom_score_adj_min
&&
1147 !capable(CAP_SYS_RESOURCE
)) {
1152 if (oom_score_adj
!= task
->signal
->oom_score_adj
) {
1153 if (oom_score_adj
== OOM_SCORE_ADJ_MIN
)
1154 atomic_inc(&task
->mm
->oom_disable_count
);
1155 if (task
->signal
->oom_score_adj
== OOM_SCORE_ADJ_MIN
)
1156 atomic_dec(&task
->mm
->oom_disable_count
);
1158 task
->signal
->oom_score_adj
= oom_score_adj
;
1159 if (has_capability_noaudit(current
, CAP_SYS_RESOURCE
))
1160 task
->signal
->oom_score_adj_min
= oom_score_adj
;
1162 * Scale /proc/pid/oom_adj appropriately ensuring that OOM_DISABLE is
1163 * always attainable.
1165 if (task
->signal
->oom_score_adj
== OOM_SCORE_ADJ_MIN
)
1166 task
->signal
->oom_adj
= OOM_DISABLE
;
1168 task
->signal
->oom_adj
= (oom_score_adj
* OOM_ADJUST_MAX
) /
1171 unlock_task_sighand(task
, &flags
);
1174 put_task_struct(task
);
1176 return err
< 0 ? err
: count
;
1179 static const struct file_operations proc_oom_score_adj_operations
= {
1180 .read
= oom_score_adj_read
,
1181 .write
= oom_score_adj_write
,
1182 .llseek
= default_llseek
,
1185 #ifdef CONFIG_AUDITSYSCALL
1186 #define TMPBUFLEN 21
1187 static ssize_t
proc_loginuid_read(struct file
* file
, char __user
* buf
,
1188 size_t count
, loff_t
*ppos
)
1190 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
1191 struct task_struct
*task
= get_proc_task(inode
);
1193 char tmpbuf
[TMPBUFLEN
];
1197 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
1198 audit_get_loginuid(task
));
1199 put_task_struct(task
);
1200 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1203 static ssize_t
proc_loginuid_write(struct file
* file
, const char __user
* buf
,
1204 size_t count
, loff_t
*ppos
)
1206 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
1211 if (!capable(CAP_AUDIT_CONTROL
))
1215 if (current
!= pid_task(proc_pid(inode
), PIDTYPE_PID
)) {
1221 if (count
>= PAGE_SIZE
)
1222 count
= PAGE_SIZE
- 1;
1225 /* No partial writes. */
1228 page
= (char*)__get_free_page(GFP_TEMPORARY
);
1232 if (copy_from_user(page
, buf
, count
))
1236 loginuid
= simple_strtoul(page
, &tmp
, 10);
1242 length
= audit_set_loginuid(current
, loginuid
);
1243 if (likely(length
== 0))
1247 free_page((unsigned long) page
);
1251 static const struct file_operations proc_loginuid_operations
= {
1252 .read
= proc_loginuid_read
,
1253 .write
= proc_loginuid_write
,
1254 .llseek
= generic_file_llseek
,
1257 static ssize_t
proc_sessionid_read(struct file
* file
, char __user
* buf
,
1258 size_t count
, loff_t
*ppos
)
1260 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
1261 struct task_struct
*task
= get_proc_task(inode
);
1263 char tmpbuf
[TMPBUFLEN
];
1267 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
1268 audit_get_sessionid(task
));
1269 put_task_struct(task
);
1270 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1273 static const struct file_operations proc_sessionid_operations
= {
1274 .read
= proc_sessionid_read
,
1275 .llseek
= generic_file_llseek
,
1279 #ifdef CONFIG_FAULT_INJECTION
1280 static ssize_t
proc_fault_inject_read(struct file
* file
, char __user
* buf
,
1281 size_t count
, loff_t
*ppos
)
1283 struct task_struct
*task
= get_proc_task(file
->f_dentry
->d_inode
);
1284 char buffer
[PROC_NUMBUF
];
1290 make_it_fail
= task
->make_it_fail
;
1291 put_task_struct(task
);
1293 len
= snprintf(buffer
, sizeof(buffer
), "%i\n", make_it_fail
);
1295 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
1298 static ssize_t
proc_fault_inject_write(struct file
* file
,
1299 const char __user
* buf
, size_t count
, loff_t
*ppos
)
1301 struct task_struct
*task
;
1302 char buffer
[PROC_NUMBUF
], *end
;
1305 if (!capable(CAP_SYS_RESOURCE
))
1307 memset(buffer
, 0, sizeof(buffer
));
1308 if (count
> sizeof(buffer
) - 1)
1309 count
= sizeof(buffer
) - 1;
1310 if (copy_from_user(buffer
, buf
, count
))
1312 make_it_fail
= simple_strtol(strstrip(buffer
), &end
, 0);
1315 task
= get_proc_task(file
->f_dentry
->d_inode
);
1318 task
->make_it_fail
= make_it_fail
;
1319 put_task_struct(task
);
1324 static const struct file_operations proc_fault_inject_operations
= {
1325 .read
= proc_fault_inject_read
,
1326 .write
= proc_fault_inject_write
,
1327 .llseek
= generic_file_llseek
,
1332 #ifdef CONFIG_SCHED_DEBUG
1334 * Print out various scheduling related per-task fields:
1336 static int sched_show(struct seq_file
*m
, void *v
)
1338 struct inode
*inode
= m
->private;
1339 struct task_struct
*p
;
1341 p
= get_proc_task(inode
);
1344 proc_sched_show_task(p
, m
);
1352 sched_write(struct file
*file
, const char __user
*buf
,
1353 size_t count
, loff_t
*offset
)
1355 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1356 struct task_struct
*p
;
1358 p
= get_proc_task(inode
);
1361 proc_sched_set_task(p
);
1368 static int sched_open(struct inode
*inode
, struct file
*filp
)
1370 return single_open(filp
, sched_show
, inode
);
1373 static const struct file_operations proc_pid_sched_operations
= {
1376 .write
= sched_write
,
1377 .llseek
= seq_lseek
,
1378 .release
= single_release
,
1383 #ifdef CONFIG_SCHED_AUTOGROUP
1385 * Print out autogroup related information:
1387 static int sched_autogroup_show(struct seq_file
*m
, void *v
)
1389 struct inode
*inode
= m
->private;
1390 struct task_struct
*p
;
1392 p
= get_proc_task(inode
);
1395 proc_sched_autogroup_show_task(p
, m
);
1403 sched_autogroup_write(struct file
*file
, const char __user
*buf
,
1404 size_t count
, loff_t
*offset
)
1406 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1407 struct task_struct
*p
;
1408 char buffer
[PROC_NUMBUF
];
1412 memset(buffer
, 0, sizeof(buffer
));
1413 if (count
> sizeof(buffer
) - 1)
1414 count
= sizeof(buffer
) - 1;
1415 if (copy_from_user(buffer
, buf
, count
))
1418 err
= kstrtoint(strstrip(buffer
), 0, &nice
);
1422 p
= get_proc_task(inode
);
1427 err
= proc_sched_autogroup_set_nice(p
, &err
);
1436 static int sched_autogroup_open(struct inode
*inode
, struct file
*filp
)
1440 ret
= single_open(filp
, sched_autogroup_show
, NULL
);
1442 struct seq_file
*m
= filp
->private_data
;
1449 static const struct file_operations proc_pid_sched_autogroup_operations
= {
1450 .open
= sched_autogroup_open
,
1452 .write
= sched_autogroup_write
,
1453 .llseek
= seq_lseek
,
1454 .release
= single_release
,
1457 #endif /* CONFIG_SCHED_AUTOGROUP */
1459 static ssize_t
comm_write(struct file
*file
, const char __user
*buf
,
1460 size_t count
, loff_t
*offset
)
1462 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1463 struct task_struct
*p
;
1464 char buffer
[TASK_COMM_LEN
];
1466 memset(buffer
, 0, sizeof(buffer
));
1467 if (count
> sizeof(buffer
) - 1)
1468 count
= sizeof(buffer
) - 1;
1469 if (copy_from_user(buffer
, buf
, count
))
1472 p
= get_proc_task(inode
);
1476 if (same_thread_group(current
, p
))
1477 set_task_comm(p
, buffer
);
1486 static int comm_show(struct seq_file
*m
, void *v
)
1488 struct inode
*inode
= m
->private;
1489 struct task_struct
*p
;
1491 p
= get_proc_task(inode
);
1496 seq_printf(m
, "%s\n", p
->comm
);
1504 static int comm_open(struct inode
*inode
, struct file
*filp
)
1506 return single_open(filp
, comm_show
, inode
);
1509 static const struct file_operations proc_pid_set_comm_operations
= {
1512 .write
= comm_write
,
1513 .llseek
= seq_lseek
,
1514 .release
= single_release
,
1517 static int proc_exe_link(struct inode
*inode
, struct path
*exe_path
)
1519 struct task_struct
*task
;
1520 struct mm_struct
*mm
;
1521 struct file
*exe_file
;
1523 task
= get_proc_task(inode
);
1526 mm
= get_task_mm(task
);
1527 put_task_struct(task
);
1530 exe_file
= get_mm_exe_file(mm
);
1533 *exe_path
= exe_file
->f_path
;
1534 path_get(&exe_file
->f_path
);
1541 static void *proc_pid_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
1543 struct inode
*inode
= dentry
->d_inode
;
1544 int error
= -EACCES
;
1546 /* We don't need a base pointer in the /proc filesystem */
1547 path_put(&nd
->path
);
1549 /* Are we allowed to snoop on the tasks file descriptors? */
1550 if (!proc_fd_access_allowed(inode
))
1553 error
= PROC_I(inode
)->op
.proc_get_link(inode
, &nd
->path
);
1555 return ERR_PTR(error
);
1558 static int do_proc_readlink(struct path
*path
, char __user
*buffer
, int buflen
)
1560 char *tmp
= (char*)__get_free_page(GFP_TEMPORARY
);
1567 pathname
= d_path(path
, tmp
, PAGE_SIZE
);
1568 len
= PTR_ERR(pathname
);
1569 if (IS_ERR(pathname
))
1571 len
= tmp
+ PAGE_SIZE
- 1 - pathname
;
1575 if (copy_to_user(buffer
, pathname
, len
))
1578 free_page((unsigned long)tmp
);
1582 static int proc_pid_readlink(struct dentry
* dentry
, char __user
* buffer
, int buflen
)
1584 int error
= -EACCES
;
1585 struct inode
*inode
= dentry
->d_inode
;
1588 /* Are we allowed to snoop on the tasks file descriptors? */
1589 if (!proc_fd_access_allowed(inode
))
1592 error
= PROC_I(inode
)->op
.proc_get_link(inode
, &path
);
1596 error
= do_proc_readlink(&path
, buffer
, buflen
);
1602 static const struct inode_operations proc_pid_link_inode_operations
= {
1603 .readlink
= proc_pid_readlink
,
1604 .follow_link
= proc_pid_follow_link
,
1605 .setattr
= proc_setattr
,
1609 /* building an inode */
1611 static int task_dumpable(struct task_struct
*task
)
1614 struct mm_struct
*mm
;
1619 dumpable
= get_dumpable(mm
);
1626 struct inode
*proc_pid_make_inode(struct super_block
* sb
, struct task_struct
*task
)
1628 struct inode
* inode
;
1629 struct proc_inode
*ei
;
1630 const struct cred
*cred
;
1632 /* We need a new inode */
1634 inode
= new_inode(sb
);
1640 inode
->i_ino
= get_next_ino();
1641 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1642 inode
->i_op
= &proc_def_inode_operations
;
1645 * grab the reference to task.
1647 ei
->pid
= get_task_pid(task
, PIDTYPE_PID
);
1651 if (task_dumpable(task
)) {
1653 cred
= __task_cred(task
);
1654 inode
->i_uid
= cred
->euid
;
1655 inode
->i_gid
= cred
->egid
;
1658 security_task_to_inode(task
, inode
);
1668 int pid_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
1670 struct inode
*inode
= dentry
->d_inode
;
1671 struct task_struct
*task
;
1672 const struct cred
*cred
;
1674 generic_fillattr(inode
, stat
);
1679 task
= pid_task(proc_pid(inode
), PIDTYPE_PID
);
1681 if ((inode
->i_mode
== (S_IFDIR
|S_IRUGO
|S_IXUGO
)) ||
1682 task_dumpable(task
)) {
1683 cred
= __task_cred(task
);
1684 stat
->uid
= cred
->euid
;
1685 stat
->gid
= cred
->egid
;
1695 * Exceptional case: normally we are not allowed to unhash a busy
1696 * directory. In this case, however, we can do it - no aliasing problems
1697 * due to the way we treat inodes.
1699 * Rewrite the inode's ownerships here because the owning task may have
1700 * performed a setuid(), etc.
1702 * Before the /proc/pid/status file was created the only way to read
1703 * the effective uid of a /process was to stat /proc/pid. Reading
1704 * /proc/pid/status is slow enough that procps and other packages
1705 * kept stating /proc/pid. To keep the rules in /proc simple I have
1706 * made this apply to all per process world readable and executable
1709 int pid_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1711 struct inode
*inode
;
1712 struct task_struct
*task
;
1713 const struct cred
*cred
;
1715 if (nd
&& nd
->flags
& LOOKUP_RCU
)
1718 inode
= dentry
->d_inode
;
1719 task
= get_proc_task(inode
);
1722 if ((inode
->i_mode
== (S_IFDIR
|S_IRUGO
|S_IXUGO
)) ||
1723 task_dumpable(task
)) {
1725 cred
= __task_cred(task
);
1726 inode
->i_uid
= cred
->euid
;
1727 inode
->i_gid
= cred
->egid
;
1733 inode
->i_mode
&= ~(S_ISUID
| S_ISGID
);
1734 security_task_to_inode(task
, inode
);
1735 put_task_struct(task
);
1742 static int pid_delete_dentry(const struct dentry
* dentry
)
1744 /* Is the task we represent dead?
1745 * If so, then don't put the dentry on the lru list,
1746 * kill it immediately.
1748 return !proc_pid(dentry
->d_inode
)->tasks
[PIDTYPE_PID
].first
;
1751 const struct dentry_operations pid_dentry_operations
=
1753 .d_revalidate
= pid_revalidate
,
1754 .d_delete
= pid_delete_dentry
,
1760 * Fill a directory entry.
1762 * If possible create the dcache entry and derive our inode number and
1763 * file type from dcache entry.
1765 * Since all of the proc inode numbers are dynamically generated, the inode
1766 * numbers do not exist until the inode is cache. This means creating the
1767 * the dcache entry in readdir is necessary to keep the inode numbers
1768 * reported by readdir in sync with the inode numbers reported
1771 int proc_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
1772 const char *name
, int len
,
1773 instantiate_t instantiate
, struct task_struct
*task
, const void *ptr
)
1775 struct dentry
*child
, *dir
= filp
->f_path
.dentry
;
1776 struct inode
*inode
;
1779 unsigned type
= DT_UNKNOWN
;
1783 qname
.hash
= full_name_hash(name
, len
);
1785 child
= d_lookup(dir
, &qname
);
1788 new = d_alloc(dir
, &qname
);
1790 child
= instantiate(dir
->d_inode
, new, task
, ptr
);
1797 if (!child
|| IS_ERR(child
) || !child
->d_inode
)
1798 goto end_instantiate
;
1799 inode
= child
->d_inode
;
1802 type
= inode
->i_mode
>> 12;
1807 ino
= find_inode_number(dir
, &qname
);
1810 return filldir(dirent
, name
, len
, filp
->f_pos
, ino
, type
);
1813 static unsigned name_to_int(struct dentry
*dentry
)
1815 const char *name
= dentry
->d_name
.name
;
1816 int len
= dentry
->d_name
.len
;
1819 if (len
> 1 && *name
== '0')
1822 unsigned c
= *name
++ - '0';
1825 if (n
>= (~0U-9)/10)
1835 #define PROC_FDINFO_MAX 64
1837 static int proc_fd_info(struct inode
*inode
, struct path
*path
, char *info
)
1839 struct task_struct
*task
= get_proc_task(inode
);
1840 struct files_struct
*files
= NULL
;
1842 int fd
= proc_fd(inode
);
1845 files
= get_files_struct(task
);
1846 put_task_struct(task
);
1850 * We are not taking a ref to the file structure, so we must
1853 spin_lock(&files
->file_lock
);
1854 file
= fcheck_files(files
, fd
);
1856 unsigned int f_flags
;
1857 struct fdtable
*fdt
;
1859 fdt
= files_fdtable(files
);
1860 f_flags
= file
->f_flags
& ~O_CLOEXEC
;
1861 if (FD_ISSET(fd
, fdt
->close_on_exec
))
1862 f_flags
|= O_CLOEXEC
;
1865 *path
= file
->f_path
;
1866 path_get(&file
->f_path
);
1869 snprintf(info
, PROC_FDINFO_MAX
,
1872 (long long) file
->f_pos
,
1874 spin_unlock(&files
->file_lock
);
1875 put_files_struct(files
);
1878 spin_unlock(&files
->file_lock
);
1879 put_files_struct(files
);
1884 static int proc_fd_link(struct inode
*inode
, struct path
*path
)
1886 return proc_fd_info(inode
, path
, NULL
);
1889 static int tid_fd_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1891 struct inode
*inode
;
1892 struct task_struct
*task
;
1894 struct files_struct
*files
;
1895 const struct cred
*cred
;
1897 if (nd
&& nd
->flags
& LOOKUP_RCU
)
1900 inode
= dentry
->d_inode
;
1901 task
= get_proc_task(inode
);
1902 fd
= proc_fd(inode
);
1905 files
= get_files_struct(task
);
1908 if (fcheck_files(files
, fd
)) {
1910 put_files_struct(files
);
1911 if (task_dumpable(task
)) {
1913 cred
= __task_cred(task
);
1914 inode
->i_uid
= cred
->euid
;
1915 inode
->i_gid
= cred
->egid
;
1921 inode
->i_mode
&= ~(S_ISUID
| S_ISGID
);
1922 security_task_to_inode(task
, inode
);
1923 put_task_struct(task
);
1927 put_files_struct(files
);
1929 put_task_struct(task
);
1935 static const struct dentry_operations tid_fd_dentry_operations
=
1937 .d_revalidate
= tid_fd_revalidate
,
1938 .d_delete
= pid_delete_dentry
,
1941 static struct dentry
*proc_fd_instantiate(struct inode
*dir
,
1942 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
1944 unsigned fd
= *(const unsigned *)ptr
;
1946 struct files_struct
*files
;
1947 struct inode
*inode
;
1948 struct proc_inode
*ei
;
1949 struct dentry
*error
= ERR_PTR(-ENOENT
);
1951 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
1956 files
= get_files_struct(task
);
1959 inode
->i_mode
= S_IFLNK
;
1962 * We are not taking a ref to the file structure, so we must
1965 spin_lock(&files
->file_lock
);
1966 file
= fcheck_files(files
, fd
);
1969 if (file
->f_mode
& FMODE_READ
)
1970 inode
->i_mode
|= S_IRUSR
| S_IXUSR
;
1971 if (file
->f_mode
& FMODE_WRITE
)
1972 inode
->i_mode
|= S_IWUSR
| S_IXUSR
;
1973 spin_unlock(&files
->file_lock
);
1974 put_files_struct(files
);
1976 inode
->i_op
= &proc_pid_link_inode_operations
;
1978 ei
->op
.proc_get_link
= proc_fd_link
;
1979 d_set_d_op(dentry
, &tid_fd_dentry_operations
);
1980 d_add(dentry
, inode
);
1981 /* Close the race of the process dying before we return the dentry */
1982 if (tid_fd_revalidate(dentry
, NULL
))
1988 spin_unlock(&files
->file_lock
);
1989 put_files_struct(files
);
1995 static struct dentry
*proc_lookupfd_common(struct inode
*dir
,
1996 struct dentry
*dentry
,
1997 instantiate_t instantiate
)
1999 struct task_struct
*task
= get_proc_task(dir
);
2000 unsigned fd
= name_to_int(dentry
);
2001 struct dentry
*result
= ERR_PTR(-ENOENT
);
2008 result
= instantiate(dir
, dentry
, task
, &fd
);
2010 put_task_struct(task
);
2015 static int proc_readfd_common(struct file
* filp
, void * dirent
,
2016 filldir_t filldir
, instantiate_t instantiate
)
2018 struct dentry
*dentry
= filp
->f_path
.dentry
;
2019 struct inode
*inode
= dentry
->d_inode
;
2020 struct task_struct
*p
= get_proc_task(inode
);
2021 unsigned int fd
, ino
;
2023 struct files_struct
* files
;
2033 if (filldir(dirent
, ".", 1, 0, inode
->i_ino
, DT_DIR
) < 0)
2037 ino
= parent_ino(dentry
);
2038 if (filldir(dirent
, "..", 2, 1, ino
, DT_DIR
) < 0)
2042 files
= get_files_struct(p
);
2046 for (fd
= filp
->f_pos
-2;
2047 fd
< files_fdtable(files
)->max_fds
;
2048 fd
++, filp
->f_pos
++) {
2049 char name
[PROC_NUMBUF
];
2052 if (!fcheck_files(files
, fd
))
2056 len
= snprintf(name
, sizeof(name
), "%d", fd
);
2057 if (proc_fill_cache(filp
, dirent
, filldir
,
2058 name
, len
, instantiate
,
2066 put_files_struct(files
);
2074 static struct dentry
*proc_lookupfd(struct inode
*dir
, struct dentry
*dentry
,
2075 struct nameidata
*nd
)
2077 return proc_lookupfd_common(dir
, dentry
, proc_fd_instantiate
);
2080 static int proc_readfd(struct file
*filp
, void *dirent
, filldir_t filldir
)
2082 return proc_readfd_common(filp
, dirent
, filldir
, proc_fd_instantiate
);
2085 static ssize_t
proc_fdinfo_read(struct file
*file
, char __user
*buf
,
2086 size_t len
, loff_t
*ppos
)
2088 char tmp
[PROC_FDINFO_MAX
];
2089 int err
= proc_fd_info(file
->f_path
.dentry
->d_inode
, NULL
, tmp
);
2091 err
= simple_read_from_buffer(buf
, len
, ppos
, tmp
, strlen(tmp
));
2095 static const struct file_operations proc_fdinfo_file_operations
= {
2096 .open
= nonseekable_open
,
2097 .read
= proc_fdinfo_read
,
2098 .llseek
= no_llseek
,
2101 static const struct file_operations proc_fd_operations
= {
2102 .read
= generic_read_dir
,
2103 .readdir
= proc_readfd
,
2104 .llseek
= default_llseek
,
2108 * /proc/pid/fd needs a special permission handler so that a process can still
2109 * access /proc/self/fd after it has executed a setuid().
2111 static int proc_fd_permission(struct inode
*inode
, int mask
, unsigned int flags
)
2113 int rv
= generic_permission(inode
, mask
, flags
, NULL
);
2116 if (task_pid(current
) == proc_pid(inode
))
2122 * proc directories can do almost nothing..
2124 static const struct inode_operations proc_fd_inode_operations
= {
2125 .lookup
= proc_lookupfd
,
2126 .permission
= proc_fd_permission
,
2127 .setattr
= proc_setattr
,
2130 static struct dentry
*proc_fdinfo_instantiate(struct inode
*dir
,
2131 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
2133 unsigned fd
= *(unsigned *)ptr
;
2134 struct inode
*inode
;
2135 struct proc_inode
*ei
;
2136 struct dentry
*error
= ERR_PTR(-ENOENT
);
2138 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
2143 inode
->i_mode
= S_IFREG
| S_IRUSR
;
2144 inode
->i_fop
= &proc_fdinfo_file_operations
;
2145 d_set_d_op(dentry
, &tid_fd_dentry_operations
);
2146 d_add(dentry
, inode
);
2147 /* Close the race of the process dying before we return the dentry */
2148 if (tid_fd_revalidate(dentry
, NULL
))
2155 static struct dentry
*proc_lookupfdinfo(struct inode
*dir
,
2156 struct dentry
*dentry
,
2157 struct nameidata
*nd
)
2159 return proc_lookupfd_common(dir
, dentry
, proc_fdinfo_instantiate
);
2162 static int proc_readfdinfo(struct file
*filp
, void *dirent
, filldir_t filldir
)
2164 return proc_readfd_common(filp
, dirent
, filldir
,
2165 proc_fdinfo_instantiate
);
2168 static const struct file_operations proc_fdinfo_operations
= {
2169 .read
= generic_read_dir
,
2170 .readdir
= proc_readfdinfo
,
2171 .llseek
= default_llseek
,
2175 * proc directories can do almost nothing..
2177 static const struct inode_operations proc_fdinfo_inode_operations
= {
2178 .lookup
= proc_lookupfdinfo
,
2179 .setattr
= proc_setattr
,
2183 static struct dentry
*proc_pident_instantiate(struct inode
*dir
,
2184 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
2186 const struct pid_entry
*p
= ptr
;
2187 struct inode
*inode
;
2188 struct proc_inode
*ei
;
2189 struct dentry
*error
= ERR_PTR(-ENOENT
);
2191 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
2196 inode
->i_mode
= p
->mode
;
2197 if (S_ISDIR(inode
->i_mode
))
2198 inode
->i_nlink
= 2; /* Use getattr to fix if necessary */
2200 inode
->i_op
= p
->iop
;
2202 inode
->i_fop
= p
->fop
;
2204 d_set_d_op(dentry
, &pid_dentry_operations
);
2205 d_add(dentry
, inode
);
2206 /* Close the race of the process dying before we return the dentry */
2207 if (pid_revalidate(dentry
, NULL
))
2213 static struct dentry
*proc_pident_lookup(struct inode
*dir
,
2214 struct dentry
*dentry
,
2215 const struct pid_entry
*ents
,
2218 struct dentry
*error
;
2219 struct task_struct
*task
= get_proc_task(dir
);
2220 const struct pid_entry
*p
, *last
;
2222 error
= ERR_PTR(-ENOENT
);
2228 * Yes, it does not scale. And it should not. Don't add
2229 * new entries into /proc/<tgid>/ without very good reasons.
2231 last
= &ents
[nents
- 1];
2232 for (p
= ents
; p
<= last
; p
++) {
2233 if (p
->len
!= dentry
->d_name
.len
)
2235 if (!memcmp(dentry
->d_name
.name
, p
->name
, p
->len
))
2241 error
= proc_pident_instantiate(dir
, dentry
, task
, p
);
2243 put_task_struct(task
);
2248 static int proc_pident_fill_cache(struct file
*filp
, void *dirent
,
2249 filldir_t filldir
, struct task_struct
*task
, const struct pid_entry
*p
)
2251 return proc_fill_cache(filp
, dirent
, filldir
, p
->name
, p
->len
,
2252 proc_pident_instantiate
, task
, p
);
2255 static int proc_pident_readdir(struct file
*filp
,
2256 void *dirent
, filldir_t filldir
,
2257 const struct pid_entry
*ents
, unsigned int nents
)
2260 struct dentry
*dentry
= filp
->f_path
.dentry
;
2261 struct inode
*inode
= dentry
->d_inode
;
2262 struct task_struct
*task
= get_proc_task(inode
);
2263 const struct pid_entry
*p
, *last
;
2276 if (filldir(dirent
, ".", 1, i
, ino
, DT_DIR
) < 0)
2282 ino
= parent_ino(dentry
);
2283 if (filldir(dirent
, "..", 2, i
, ino
, DT_DIR
) < 0)
2295 last
= &ents
[nents
- 1];
2297 if (proc_pident_fill_cache(filp
, dirent
, filldir
, task
, p
) < 0)
2306 put_task_struct(task
);
2311 #ifdef CONFIG_SECURITY
2312 static ssize_t
proc_pid_attr_read(struct file
* file
, char __user
* buf
,
2313 size_t count
, loff_t
*ppos
)
2315 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
2318 struct task_struct
*task
= get_proc_task(inode
);
2323 length
= security_getprocattr(task
,
2324 (char*)file
->f_path
.dentry
->d_name
.name
,
2326 put_task_struct(task
);
2328 length
= simple_read_from_buffer(buf
, count
, ppos
, p
, length
);
2333 static ssize_t
proc_pid_attr_write(struct file
* file
, const char __user
* buf
,
2334 size_t count
, loff_t
*ppos
)
2336 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
2339 struct task_struct
*task
= get_proc_task(inode
);
2344 if (count
> PAGE_SIZE
)
2347 /* No partial writes. */
2353 page
= (char*)__get_free_page(GFP_TEMPORARY
);
2358 if (copy_from_user(page
, buf
, count
))
2361 /* Guard against adverse ptrace interaction */
2362 length
= mutex_lock_interruptible(&task
->signal
->cred_guard_mutex
);
2366 length
= security_setprocattr(task
,
2367 (char*)file
->f_path
.dentry
->d_name
.name
,
2368 (void*)page
, count
);
2369 mutex_unlock(&task
->signal
->cred_guard_mutex
);
2371 free_page((unsigned long) page
);
2373 put_task_struct(task
);
2378 static const struct file_operations proc_pid_attr_operations
= {
2379 .read
= proc_pid_attr_read
,
2380 .write
= proc_pid_attr_write
,
2381 .llseek
= generic_file_llseek
,
2384 static const struct pid_entry attr_dir_stuff
[] = {
2385 REG("current", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2386 REG("prev", S_IRUGO
, proc_pid_attr_operations
),
2387 REG("exec", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2388 REG("fscreate", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2389 REG("keycreate", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2390 REG("sockcreate", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2393 static int proc_attr_dir_readdir(struct file
* filp
,
2394 void * dirent
, filldir_t filldir
)
2396 return proc_pident_readdir(filp
,dirent
,filldir
,
2397 attr_dir_stuff
,ARRAY_SIZE(attr_dir_stuff
));
2400 static const struct file_operations proc_attr_dir_operations
= {
2401 .read
= generic_read_dir
,
2402 .readdir
= proc_attr_dir_readdir
,
2403 .llseek
= default_llseek
,
2406 static struct dentry
*proc_attr_dir_lookup(struct inode
*dir
,
2407 struct dentry
*dentry
, struct nameidata
*nd
)
2409 return proc_pident_lookup(dir
, dentry
,
2410 attr_dir_stuff
, ARRAY_SIZE(attr_dir_stuff
));
2413 static const struct inode_operations proc_attr_dir_inode_operations
= {
2414 .lookup
= proc_attr_dir_lookup
,
2415 .getattr
= pid_getattr
,
2416 .setattr
= proc_setattr
,
2421 #ifdef CONFIG_ELF_CORE
2422 static ssize_t
proc_coredump_filter_read(struct file
*file
, char __user
*buf
,
2423 size_t count
, loff_t
*ppos
)
2425 struct task_struct
*task
= get_proc_task(file
->f_dentry
->d_inode
);
2426 struct mm_struct
*mm
;
2427 char buffer
[PROC_NUMBUF
];
2435 mm
= get_task_mm(task
);
2437 len
= snprintf(buffer
, sizeof(buffer
), "%08lx\n",
2438 ((mm
->flags
& MMF_DUMP_FILTER_MASK
) >>
2439 MMF_DUMP_FILTER_SHIFT
));
2441 ret
= simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
2444 put_task_struct(task
);
2449 static ssize_t
proc_coredump_filter_write(struct file
*file
,
2450 const char __user
*buf
,
2454 struct task_struct
*task
;
2455 struct mm_struct
*mm
;
2456 char buffer
[PROC_NUMBUF
], *end
;
2463 memset(buffer
, 0, sizeof(buffer
));
2464 if (count
> sizeof(buffer
) - 1)
2465 count
= sizeof(buffer
) - 1;
2466 if (copy_from_user(buffer
, buf
, count
))
2470 val
= (unsigned int)simple_strtoul(buffer
, &end
, 0);
2473 if (end
- buffer
== 0)
2477 task
= get_proc_task(file
->f_dentry
->d_inode
);
2482 mm
= get_task_mm(task
);
2486 for (i
= 0, mask
= 1; i
< MMF_DUMP_FILTER_BITS
; i
++, mask
<<= 1) {
2488 set_bit(i
+ MMF_DUMP_FILTER_SHIFT
, &mm
->flags
);
2490 clear_bit(i
+ MMF_DUMP_FILTER_SHIFT
, &mm
->flags
);
2495 put_task_struct(task
);
2500 static const struct file_operations proc_coredump_filter_operations
= {
2501 .read
= proc_coredump_filter_read
,
2502 .write
= proc_coredump_filter_write
,
2503 .llseek
= generic_file_llseek
,
2510 static int proc_self_readlink(struct dentry
*dentry
, char __user
*buffer
,
2513 struct pid_namespace
*ns
= dentry
->d_sb
->s_fs_info
;
2514 pid_t tgid
= task_tgid_nr_ns(current
, ns
);
2515 char tmp
[PROC_NUMBUF
];
2518 sprintf(tmp
, "%d", tgid
);
2519 return vfs_readlink(dentry
,buffer
,buflen
,tmp
);
2522 static void *proc_self_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
2524 struct pid_namespace
*ns
= dentry
->d_sb
->s_fs_info
;
2525 pid_t tgid
= task_tgid_nr_ns(current
, ns
);
2526 char *name
= ERR_PTR(-ENOENT
);
2530 name
= ERR_PTR(-ENOMEM
);
2532 sprintf(name
, "%d", tgid
);
2534 nd_set_link(nd
, name
);
2538 static void proc_self_put_link(struct dentry
*dentry
, struct nameidata
*nd
,
2541 char *s
= nd_get_link(nd
);
2546 static const struct inode_operations proc_self_inode_operations
= {
2547 .readlink
= proc_self_readlink
,
2548 .follow_link
= proc_self_follow_link
,
2549 .put_link
= proc_self_put_link
,
2555 * These are the directory entries in the root directory of /proc
2556 * that properly belong to the /proc filesystem, as they describe
2557 * describe something that is process related.
2559 static const struct pid_entry proc_base_stuff
[] = {
2560 NOD("self", S_IFLNK
|S_IRWXUGO
,
2561 &proc_self_inode_operations
, NULL
, {}),
2564 static struct dentry
*proc_base_instantiate(struct inode
*dir
,
2565 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
2567 const struct pid_entry
*p
= ptr
;
2568 struct inode
*inode
;
2569 struct proc_inode
*ei
;
2570 struct dentry
*error
;
2572 /* Allocate the inode */
2573 error
= ERR_PTR(-ENOMEM
);
2574 inode
= new_inode(dir
->i_sb
);
2578 /* Initialize the inode */
2580 inode
->i_ino
= get_next_ino();
2581 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
2584 * grab the reference to the task.
2586 ei
->pid
= get_task_pid(task
, PIDTYPE_PID
);
2590 inode
->i_mode
= p
->mode
;
2591 if (S_ISDIR(inode
->i_mode
))
2593 if (S_ISLNK(inode
->i_mode
))
2596 inode
->i_op
= p
->iop
;
2598 inode
->i_fop
= p
->fop
;
2600 d_add(dentry
, inode
);
2609 static struct dentry
*proc_base_lookup(struct inode
*dir
, struct dentry
*dentry
)
2611 struct dentry
*error
;
2612 struct task_struct
*task
= get_proc_task(dir
);
2613 const struct pid_entry
*p
, *last
;
2615 error
= ERR_PTR(-ENOENT
);
2620 /* Lookup the directory entry */
2621 last
= &proc_base_stuff
[ARRAY_SIZE(proc_base_stuff
) - 1];
2622 for (p
= proc_base_stuff
; p
<= last
; p
++) {
2623 if (p
->len
!= dentry
->d_name
.len
)
2625 if (!memcmp(dentry
->d_name
.name
, p
->name
, p
->len
))
2631 error
= proc_base_instantiate(dir
, dentry
, task
, p
);
2634 put_task_struct(task
);
2639 static int proc_base_fill_cache(struct file
*filp
, void *dirent
,
2640 filldir_t filldir
, struct task_struct
*task
, const struct pid_entry
*p
)
2642 return proc_fill_cache(filp
, dirent
, filldir
, p
->name
, p
->len
,
2643 proc_base_instantiate
, task
, p
);
2646 #ifdef CONFIG_TASK_IO_ACCOUNTING
2647 static int do_io_accounting(struct task_struct
*task
, char *buffer
, int whole
)
2649 struct task_io_accounting acct
= task
->ioac
;
2650 unsigned long flags
;
2653 result
= mutex_lock_killable(&task
->signal
->cred_guard_mutex
);
2657 if (!ptrace_may_access(task
, PTRACE_MODE_READ
)) {
2662 if (whole
&& lock_task_sighand(task
, &flags
)) {
2663 struct task_struct
*t
= task
;
2665 task_io_accounting_add(&acct
, &task
->signal
->ioac
);
2666 while_each_thread(task
, t
)
2667 task_io_accounting_add(&acct
, &t
->ioac
);
2669 unlock_task_sighand(task
, &flags
);
2671 result
= sprintf(buffer
,
2676 "read_bytes: %llu\n"
2677 "write_bytes: %llu\n"
2678 "cancelled_write_bytes: %llu\n",
2679 (unsigned long long)acct
.rchar
,
2680 (unsigned long long)acct
.wchar
,
2681 (unsigned long long)acct
.syscr
,
2682 (unsigned long long)acct
.syscw
,
2683 (unsigned long long)acct
.read_bytes
,
2684 (unsigned long long)acct
.write_bytes
,
2685 (unsigned long long)acct
.cancelled_write_bytes
);
2687 mutex_unlock(&task
->signal
->cred_guard_mutex
);
2691 static int proc_tid_io_accounting(struct task_struct
*task
, char *buffer
)
2693 return do_io_accounting(task
, buffer
, 0);
2696 static int proc_tgid_io_accounting(struct task_struct
*task
, char *buffer
)
2698 return do_io_accounting(task
, buffer
, 1);
2700 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2702 static int proc_pid_personality(struct seq_file
*m
, struct pid_namespace
*ns
,
2703 struct pid
*pid
, struct task_struct
*task
)
2705 int err
= lock_trace(task
);
2707 seq_printf(m
, "%08x\n", task
->personality
);
2716 static const struct file_operations proc_task_operations
;
2717 static const struct inode_operations proc_task_inode_operations
;
2719 static const struct pid_entry tgid_base_stuff
[] = {
2720 DIR("task", S_IRUGO
|S_IXUGO
, proc_task_inode_operations
, proc_task_operations
),
2721 DIR("fd", S_IRUSR
|S_IXUSR
, proc_fd_inode_operations
, proc_fd_operations
),
2722 DIR("fdinfo", S_IRUSR
|S_IXUSR
, proc_fdinfo_inode_operations
, proc_fdinfo_operations
),
2723 DIR("ns", S_IRUSR
|S_IXUGO
, proc_ns_dir_inode_operations
, proc_ns_dir_operations
),
2725 DIR("net", S_IRUGO
|S_IXUGO
, proc_net_inode_operations
, proc_net_operations
),
2727 REG("environ", S_IRUSR
, proc_environ_operations
),
2728 INF("auxv", S_IRUSR
, proc_pid_auxv
),
2729 ONE("status", S_IRUGO
, proc_pid_status
),
2730 ONE("personality", S_IRUGO
, proc_pid_personality
),
2731 INF("limits", S_IRUGO
, proc_pid_limits
),
2732 #ifdef CONFIG_SCHED_DEBUG
2733 REG("sched", S_IRUGO
|S_IWUSR
, proc_pid_sched_operations
),
2735 #ifdef CONFIG_SCHED_AUTOGROUP
2736 REG("autogroup", S_IRUGO
|S_IWUSR
, proc_pid_sched_autogroup_operations
),
2738 REG("comm", S_IRUGO
|S_IWUSR
, proc_pid_set_comm_operations
),
2739 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2740 INF("syscall", S_IRUGO
, proc_pid_syscall
),
2742 INF("cmdline", S_IRUGO
, proc_pid_cmdline
),
2743 ONE("stat", S_IRUGO
, proc_tgid_stat
),
2744 ONE("statm", S_IRUGO
, proc_pid_statm
),
2745 REG("maps", S_IRUGO
, proc_maps_operations
),
2747 REG("numa_maps", S_IRUGO
, proc_numa_maps_operations
),
2749 REG("mem", S_IRUSR
|S_IWUSR
, proc_mem_operations
),
2750 LNK("cwd", proc_cwd_link
),
2751 LNK("root", proc_root_link
),
2752 LNK("exe", proc_exe_link
),
2753 REG("mounts", S_IRUGO
, proc_mounts_operations
),
2754 REG("mountinfo", S_IRUGO
, proc_mountinfo_operations
),
2755 REG("mountstats", S_IRUSR
, proc_mountstats_operations
),
2756 #ifdef CONFIG_PROC_PAGE_MONITOR
2757 REG("clear_refs", S_IWUSR
, proc_clear_refs_operations
),
2758 REG("smaps", S_IRUGO
, proc_smaps_operations
),
2759 REG("pagemap", S_IRUGO
, proc_pagemap_operations
),
2761 #ifdef CONFIG_SECURITY
2762 DIR("attr", S_IRUGO
|S_IXUGO
, proc_attr_dir_inode_operations
, proc_attr_dir_operations
),
2764 #ifdef CONFIG_KALLSYMS
2765 INF("wchan", S_IRUGO
, proc_pid_wchan
),
2767 #ifdef CONFIG_STACKTRACE
2768 ONE("stack", S_IRUGO
, proc_pid_stack
),
2770 #ifdef CONFIG_SCHEDSTATS
2771 INF("schedstat", S_IRUGO
, proc_pid_schedstat
),
2773 #ifdef CONFIG_LATENCYTOP
2774 REG("latency", S_IRUGO
, proc_lstats_operations
),
2776 #ifdef CONFIG_PROC_PID_CPUSET
2777 REG("cpuset", S_IRUGO
, proc_cpuset_operations
),
2779 #ifdef CONFIG_CGROUPS
2780 REG("cgroup", S_IRUGO
, proc_cgroup_operations
),
2782 INF("oom_score", S_IRUGO
, proc_oom_score
),
2783 REG("oom_adj", S_IRUGO
|S_IWUSR
, proc_oom_adjust_operations
),
2784 REG("oom_score_adj", S_IRUGO
|S_IWUSR
, proc_oom_score_adj_operations
),
2785 #ifdef CONFIG_AUDITSYSCALL
2786 REG("loginuid", S_IWUSR
|S_IRUGO
, proc_loginuid_operations
),
2787 REG("sessionid", S_IRUGO
, proc_sessionid_operations
),
2789 #ifdef CONFIG_FAULT_INJECTION
2790 REG("make-it-fail", S_IRUGO
|S_IWUSR
, proc_fault_inject_operations
),
2792 #ifdef CONFIG_ELF_CORE
2793 REG("coredump_filter", S_IRUGO
|S_IWUSR
, proc_coredump_filter_operations
),
2795 #ifdef CONFIG_TASK_IO_ACCOUNTING
2796 INF("io", S_IRUSR
, proc_tgid_io_accounting
),
2798 #ifdef CONFIG_HARDWALL
2799 INF("hardwall", S_IRUGO
, proc_pid_hardwall
),
2803 static int proc_tgid_base_readdir(struct file
* filp
,
2804 void * dirent
, filldir_t filldir
)
2806 return proc_pident_readdir(filp
,dirent
,filldir
,
2807 tgid_base_stuff
,ARRAY_SIZE(tgid_base_stuff
));
2810 static const struct file_operations proc_tgid_base_operations
= {
2811 .read
= generic_read_dir
,
2812 .readdir
= proc_tgid_base_readdir
,
2813 .llseek
= default_llseek
,
2816 static struct dentry
*proc_tgid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
){
2817 return proc_pident_lookup(dir
, dentry
,
2818 tgid_base_stuff
, ARRAY_SIZE(tgid_base_stuff
));
2821 static const struct inode_operations proc_tgid_base_inode_operations
= {
2822 .lookup
= proc_tgid_base_lookup
,
2823 .getattr
= pid_getattr
,
2824 .setattr
= proc_setattr
,
2827 static void proc_flush_task_mnt(struct vfsmount
*mnt
, pid_t pid
, pid_t tgid
)
2829 struct dentry
*dentry
, *leader
, *dir
;
2830 char buf
[PROC_NUMBUF
];
2834 name
.len
= snprintf(buf
, sizeof(buf
), "%d", pid
);
2835 dentry
= d_hash_and_lookup(mnt
->mnt_root
, &name
);
2837 shrink_dcache_parent(dentry
);
2843 name
.len
= snprintf(buf
, sizeof(buf
), "%d", tgid
);
2844 leader
= d_hash_and_lookup(mnt
->mnt_root
, &name
);
2849 name
.len
= strlen(name
.name
);
2850 dir
= d_hash_and_lookup(leader
, &name
);
2852 goto out_put_leader
;
2855 name
.len
= snprintf(buf
, sizeof(buf
), "%d", pid
);
2856 dentry
= d_hash_and_lookup(dir
, &name
);
2858 shrink_dcache_parent(dentry
);
2871 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2872 * @task: task that should be flushed.
2874 * When flushing dentries from proc, one needs to flush them from global
2875 * proc (proc_mnt) and from all the namespaces' procs this task was seen
2876 * in. This call is supposed to do all of this job.
2878 * Looks in the dcache for
2880 * /proc/@tgid/task/@pid
2881 * if either directory is present flushes it and all of it'ts children
2884 * It is safe and reasonable to cache /proc entries for a task until
2885 * that task exits. After that they just clog up the dcache with
2886 * useless entries, possibly causing useful dcache entries to be
2887 * flushed instead. This routine is proved to flush those useless
2888 * dcache entries at process exit time.
2890 * NOTE: This routine is just an optimization so it does not guarantee
2891 * that no dcache entries will exist at process exit time it
2892 * just makes it very unlikely that any will persist.
2895 void proc_flush_task(struct task_struct
*task
)
2898 struct pid
*pid
, *tgid
;
2901 pid
= task_pid(task
);
2902 tgid
= task_tgid(task
);
2904 for (i
= 0; i
<= pid
->level
; i
++) {
2905 upid
= &pid
->numbers
[i
];
2906 proc_flush_task_mnt(upid
->ns
->proc_mnt
, upid
->nr
,
2907 tgid
->numbers
[i
].nr
);
2910 upid
= &pid
->numbers
[pid
->level
];
2912 pid_ns_release_proc(upid
->ns
);
2915 static struct dentry
*proc_pid_instantiate(struct inode
*dir
,
2916 struct dentry
* dentry
,
2917 struct task_struct
*task
, const void *ptr
)
2919 struct dentry
*error
= ERR_PTR(-ENOENT
);
2920 struct inode
*inode
;
2922 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
2926 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
2927 inode
->i_op
= &proc_tgid_base_inode_operations
;
2928 inode
->i_fop
= &proc_tgid_base_operations
;
2929 inode
->i_flags
|=S_IMMUTABLE
;
2931 inode
->i_nlink
= 2 + pid_entry_count_dirs(tgid_base_stuff
,
2932 ARRAY_SIZE(tgid_base_stuff
));
2934 d_set_d_op(dentry
, &pid_dentry_operations
);
2936 d_add(dentry
, inode
);
2937 /* Close the race of the process dying before we return the dentry */
2938 if (pid_revalidate(dentry
, NULL
))
2944 struct dentry
*proc_pid_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
)
2946 struct dentry
*result
;
2947 struct task_struct
*task
;
2949 struct pid_namespace
*ns
;
2951 result
= proc_base_lookup(dir
, dentry
);
2952 if (!IS_ERR(result
) || PTR_ERR(result
) != -ENOENT
)
2955 tgid
= name_to_int(dentry
);
2959 ns
= dentry
->d_sb
->s_fs_info
;
2961 task
= find_task_by_pid_ns(tgid
, ns
);
2963 get_task_struct(task
);
2968 result
= proc_pid_instantiate(dir
, dentry
, task
, NULL
);
2969 put_task_struct(task
);
2975 * Find the first task with tgid >= tgid
2980 struct task_struct
*task
;
2982 static struct tgid_iter
next_tgid(struct pid_namespace
*ns
, struct tgid_iter iter
)
2987 put_task_struct(iter
.task
);
2991 pid
= find_ge_pid(iter
.tgid
, ns
);
2993 iter
.tgid
= pid_nr_ns(pid
, ns
);
2994 iter
.task
= pid_task(pid
, PIDTYPE_PID
);
2995 /* What we to know is if the pid we have find is the
2996 * pid of a thread_group_leader. Testing for task
2997 * being a thread_group_leader is the obvious thing
2998 * todo but there is a window when it fails, due to
2999 * the pid transfer logic in de_thread.
3001 * So we perform the straight forward test of seeing
3002 * if the pid we have found is the pid of a thread
3003 * group leader, and don't worry if the task we have
3004 * found doesn't happen to be a thread group leader.
3005 * As we don't care in the case of readdir.
3007 if (!iter
.task
|| !has_group_leader_pid(iter
.task
)) {
3011 get_task_struct(iter
.task
);
3017 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
3019 static int proc_pid_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
3020 struct tgid_iter iter
)
3022 char name
[PROC_NUMBUF
];
3023 int len
= snprintf(name
, sizeof(name
), "%d", iter
.tgid
);
3024 return proc_fill_cache(filp
, dirent
, filldir
, name
, len
,
3025 proc_pid_instantiate
, iter
.task
, NULL
);
3028 /* for the /proc/ directory itself, after non-process stuff has been done */
3029 int proc_pid_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
3032 struct task_struct
*reaper
;
3033 struct tgid_iter iter
;
3034 struct pid_namespace
*ns
;
3036 if (filp
->f_pos
>= PID_MAX_LIMIT
+ TGID_OFFSET
)
3038 nr
= filp
->f_pos
- FIRST_PROCESS_ENTRY
;
3040 reaper
= get_proc_task(filp
->f_path
.dentry
->d_inode
);
3044 for (; nr
< ARRAY_SIZE(proc_base_stuff
); filp
->f_pos
++, nr
++) {
3045 const struct pid_entry
*p
= &proc_base_stuff
[nr
];
3046 if (proc_base_fill_cache(filp
, dirent
, filldir
, reaper
, p
) < 0)
3050 ns
= filp
->f_dentry
->d_sb
->s_fs_info
;
3052 iter
.tgid
= filp
->f_pos
- TGID_OFFSET
;
3053 for (iter
= next_tgid(ns
, iter
);
3055 iter
.tgid
+= 1, iter
= next_tgid(ns
, iter
)) {
3056 filp
->f_pos
= iter
.tgid
+ TGID_OFFSET
;
3057 if (proc_pid_fill_cache(filp
, dirent
, filldir
, iter
) < 0) {
3058 put_task_struct(iter
.task
);
3062 filp
->f_pos
= PID_MAX_LIMIT
+ TGID_OFFSET
;
3064 put_task_struct(reaper
);
3072 static const struct pid_entry tid_base_stuff
[] = {
3073 DIR("fd", S_IRUSR
|S_IXUSR
, proc_fd_inode_operations
, proc_fd_operations
),
3074 DIR("fdinfo", S_IRUSR
|S_IXUSR
, proc_fdinfo_inode_operations
, proc_fdinfo_operations
),
3075 DIR("ns", S_IRUSR
|S_IXUGO
, proc_ns_dir_inode_operations
, proc_ns_dir_operations
),
3076 REG("environ", S_IRUSR
, proc_environ_operations
),
3077 INF("auxv", S_IRUSR
, proc_pid_auxv
),
3078 ONE("status", S_IRUGO
, proc_pid_status
),
3079 ONE("personality", S_IRUGO
, proc_pid_personality
),
3080 INF("limits", S_IRUGO
, proc_pid_limits
),
3081 #ifdef CONFIG_SCHED_DEBUG
3082 REG("sched", S_IRUGO
|S_IWUSR
, proc_pid_sched_operations
),
3084 REG("comm", S_IRUGO
|S_IWUSR
, proc_pid_set_comm_operations
),
3085 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3086 INF("syscall", S_IRUGO
, proc_pid_syscall
),
3088 INF("cmdline", S_IRUGO
, proc_pid_cmdline
),
3089 ONE("stat", S_IRUGO
, proc_tid_stat
),
3090 ONE("statm", S_IRUGO
, proc_pid_statm
),
3091 REG("maps", S_IRUGO
, proc_maps_operations
),
3093 REG("numa_maps", S_IRUGO
, proc_numa_maps_operations
),
3095 REG("mem", S_IRUSR
|S_IWUSR
, proc_mem_operations
),
3096 LNK("cwd", proc_cwd_link
),
3097 LNK("root", proc_root_link
),
3098 LNK("exe", proc_exe_link
),
3099 REG("mounts", S_IRUGO
, proc_mounts_operations
),
3100 REG("mountinfo", S_IRUGO
, proc_mountinfo_operations
),
3101 #ifdef CONFIG_PROC_PAGE_MONITOR
3102 REG("clear_refs", S_IWUSR
, proc_clear_refs_operations
),
3103 REG("smaps", S_IRUGO
, proc_smaps_operations
),
3104 REG("pagemap", S_IRUGO
, proc_pagemap_operations
),
3106 #ifdef CONFIG_SECURITY
3107 DIR("attr", S_IRUGO
|S_IXUGO
, proc_attr_dir_inode_operations
, proc_attr_dir_operations
),
3109 #ifdef CONFIG_KALLSYMS
3110 INF("wchan", S_IRUGO
, proc_pid_wchan
),
3112 #ifdef CONFIG_STACKTRACE
3113 ONE("stack", S_IRUGO
, proc_pid_stack
),
3115 #ifdef CONFIG_SCHEDSTATS
3116 INF("schedstat", S_IRUGO
, proc_pid_schedstat
),
3118 #ifdef CONFIG_LATENCYTOP
3119 REG("latency", S_IRUGO
, proc_lstats_operations
),
3121 #ifdef CONFIG_PROC_PID_CPUSET
3122 REG("cpuset", S_IRUGO
, proc_cpuset_operations
),
3124 #ifdef CONFIG_CGROUPS
3125 REG("cgroup", S_IRUGO
, proc_cgroup_operations
),
3127 INF("oom_score", S_IRUGO
, proc_oom_score
),
3128 REG("oom_adj", S_IRUGO
|S_IWUSR
, proc_oom_adjust_operations
),
3129 REG("oom_score_adj", S_IRUGO
|S_IWUSR
, proc_oom_score_adj_operations
),
3130 #ifdef CONFIG_AUDITSYSCALL
3131 REG("loginuid", S_IWUSR
|S_IRUGO
, proc_loginuid_operations
),
3132 REG("sessionid", S_IRUGO
, proc_sessionid_operations
),
3134 #ifdef CONFIG_FAULT_INJECTION
3135 REG("make-it-fail", S_IRUGO
|S_IWUSR
, proc_fault_inject_operations
),
3137 #ifdef CONFIG_TASK_IO_ACCOUNTING
3138 INF("io", S_IRUSR
, proc_tid_io_accounting
),
3140 #ifdef CONFIG_HARDWALL
3141 INF("hardwall", S_IRUGO
, proc_pid_hardwall
),
3145 static int proc_tid_base_readdir(struct file
* filp
,
3146 void * dirent
, filldir_t filldir
)
3148 return proc_pident_readdir(filp
,dirent
,filldir
,
3149 tid_base_stuff
,ARRAY_SIZE(tid_base_stuff
));
3152 static struct dentry
*proc_tid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
){
3153 return proc_pident_lookup(dir
, dentry
,
3154 tid_base_stuff
, ARRAY_SIZE(tid_base_stuff
));
3157 static const struct file_operations proc_tid_base_operations
= {
3158 .read
= generic_read_dir
,
3159 .readdir
= proc_tid_base_readdir
,
3160 .llseek
= default_llseek
,
3163 static const struct inode_operations proc_tid_base_inode_operations
= {
3164 .lookup
= proc_tid_base_lookup
,
3165 .getattr
= pid_getattr
,
3166 .setattr
= proc_setattr
,
3169 static struct dentry
*proc_task_instantiate(struct inode
*dir
,
3170 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
3172 struct dentry
*error
= ERR_PTR(-ENOENT
);
3173 struct inode
*inode
;
3174 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
3178 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
3179 inode
->i_op
= &proc_tid_base_inode_operations
;
3180 inode
->i_fop
= &proc_tid_base_operations
;
3181 inode
->i_flags
|=S_IMMUTABLE
;
3183 inode
->i_nlink
= 2 + pid_entry_count_dirs(tid_base_stuff
,
3184 ARRAY_SIZE(tid_base_stuff
));
3186 d_set_d_op(dentry
, &pid_dentry_operations
);
3188 d_add(dentry
, inode
);
3189 /* Close the race of the process dying before we return the dentry */
3190 if (pid_revalidate(dentry
, NULL
))
3196 static struct dentry
*proc_task_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
)
3198 struct dentry
*result
= ERR_PTR(-ENOENT
);
3199 struct task_struct
*task
;
3200 struct task_struct
*leader
= get_proc_task(dir
);
3202 struct pid_namespace
*ns
;
3207 tid
= name_to_int(dentry
);
3211 ns
= dentry
->d_sb
->s_fs_info
;
3213 task
= find_task_by_pid_ns(tid
, ns
);
3215 get_task_struct(task
);
3219 if (!same_thread_group(leader
, task
))
3222 result
= proc_task_instantiate(dir
, dentry
, task
, NULL
);
3224 put_task_struct(task
);
3226 put_task_struct(leader
);
3232 * Find the first tid of a thread group to return to user space.
3234 * Usually this is just the thread group leader, but if the users
3235 * buffer was too small or there was a seek into the middle of the
3236 * directory we have more work todo.
3238 * In the case of a short read we start with find_task_by_pid.
3240 * In the case of a seek we start with the leader and walk nr
3243 static struct task_struct
*first_tid(struct task_struct
*leader
,
3244 int tid
, int nr
, struct pid_namespace
*ns
)
3246 struct task_struct
*pos
;
3249 /* Attempt to start with the pid of a thread */
3250 if (tid
&& (nr
> 0)) {
3251 pos
= find_task_by_pid_ns(tid
, ns
);
3252 if (pos
&& (pos
->group_leader
== leader
))
3256 /* If nr exceeds the number of threads there is nothing todo */
3258 if (nr
&& nr
>= get_nr_threads(leader
))
3261 /* If we haven't found our starting place yet start
3262 * with the leader and walk nr threads forward.
3264 for (pos
= leader
; nr
> 0; --nr
) {
3265 pos
= next_thread(pos
);
3266 if (pos
== leader
) {
3272 get_task_struct(pos
);
3279 * Find the next thread in the thread list.
3280 * Return NULL if there is an error or no next thread.
3282 * The reference to the input task_struct is released.
3284 static struct task_struct
*next_tid(struct task_struct
*start
)
3286 struct task_struct
*pos
= NULL
;
3288 if (pid_alive(start
)) {
3289 pos
= next_thread(start
);
3290 if (thread_group_leader(pos
))
3293 get_task_struct(pos
);
3296 put_task_struct(start
);
3300 static int proc_task_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
3301 struct task_struct
*task
, int tid
)
3303 char name
[PROC_NUMBUF
];
3304 int len
= snprintf(name
, sizeof(name
), "%d", tid
);
3305 return proc_fill_cache(filp
, dirent
, filldir
, name
, len
,
3306 proc_task_instantiate
, task
, NULL
);
3309 /* for the /proc/TGID/task/ directories */
3310 static int proc_task_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
3312 struct dentry
*dentry
= filp
->f_path
.dentry
;
3313 struct inode
*inode
= dentry
->d_inode
;
3314 struct task_struct
*leader
= NULL
;
3315 struct task_struct
*task
;
3316 int retval
= -ENOENT
;
3319 struct pid_namespace
*ns
;
3321 task
= get_proc_task(inode
);
3325 if (pid_alive(task
)) {
3326 leader
= task
->group_leader
;
3327 get_task_struct(leader
);
3330 put_task_struct(task
);
3335 switch ((unsigned long)filp
->f_pos
) {
3338 if (filldir(dirent
, ".", 1, filp
->f_pos
, ino
, DT_DIR
) < 0)
3343 ino
= parent_ino(dentry
);
3344 if (filldir(dirent
, "..", 2, filp
->f_pos
, ino
, DT_DIR
) < 0)
3350 /* f_version caches the tgid value that the last readdir call couldn't
3351 * return. lseek aka telldir automagically resets f_version to 0.
3353 ns
= filp
->f_dentry
->d_sb
->s_fs_info
;
3354 tid
= (int)filp
->f_version
;
3355 filp
->f_version
= 0;
3356 for (task
= first_tid(leader
, tid
, filp
->f_pos
- 2, ns
);
3358 task
= next_tid(task
), filp
->f_pos
++) {
3359 tid
= task_pid_nr_ns(task
, ns
);
3360 if (proc_task_fill_cache(filp
, dirent
, filldir
, task
, tid
) < 0) {
3361 /* returning this tgid failed, save it as the first
3362 * pid for the next readir call */
3363 filp
->f_version
= (u64
)tid
;
3364 put_task_struct(task
);
3369 put_task_struct(leader
);
3374 static int proc_task_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
3376 struct inode
*inode
= dentry
->d_inode
;
3377 struct task_struct
*p
= get_proc_task(inode
);
3378 generic_fillattr(inode
, stat
);
3381 stat
->nlink
+= get_nr_threads(p
);
3388 static const struct inode_operations proc_task_inode_operations
= {
3389 .lookup
= proc_task_lookup
,
3390 .getattr
= proc_task_getattr
,
3391 .setattr
= proc_setattr
,
3394 static const struct file_operations proc_task_operations
= {
3395 .read
= generic_read_dir
,
3396 .readdir
= proc_task_readdir
,
3397 .llseek
= default_llseek
,