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>
89 * Implementing inode permission operations in /proc is almost
90 * certainly an error. Permission checks need to happen during
91 * each system call not at open time. The reason is that most of
92 * what we wish to check for permissions in /proc varies at runtime.
94 * The classic example of a problem is opening file descriptors
95 * in /proc for a task before it execs a suid executable.
102 const struct inode_operations
*iop
;
103 const struct file_operations
*fop
;
107 #define NOD(NAME, MODE, IOP, FOP, OP) { \
109 .len = sizeof(NAME) - 1, \
116 #define DIR(NAME, MODE, iops, fops) \
117 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
118 #define LNK(NAME, get_link) \
119 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
120 &proc_pid_link_inode_operations, NULL, \
121 { .proc_get_link = get_link } )
122 #define REG(NAME, MODE, fops) \
123 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
124 #define INF(NAME, MODE, read) \
125 NOD(NAME, (S_IFREG|(MODE)), \
126 NULL, &proc_info_file_operations, \
127 { .proc_read = read } )
128 #define ONE(NAME, MODE, show) \
129 NOD(NAME, (S_IFREG|(MODE)), \
130 NULL, &proc_single_file_operations, \
131 { .proc_show = show } )
134 * Count the number of hardlinks for the pid_entry table, excluding the .
137 static unsigned int pid_entry_count_dirs(const struct pid_entry
*entries
,
144 for (i
= 0; i
< n
; ++i
) {
145 if (S_ISDIR(entries
[i
].mode
))
152 static int get_task_root(struct task_struct
*task
, struct path
*root
)
154 int result
= -ENOENT
;
158 get_fs_root(task
->fs
, root
);
165 static int proc_cwd_link(struct inode
*inode
, struct path
*path
)
167 struct task_struct
*task
= get_proc_task(inode
);
168 int result
= -ENOENT
;
173 get_fs_pwd(task
->fs
, path
);
177 put_task_struct(task
);
182 static int proc_root_link(struct inode
*inode
, struct path
*path
)
184 struct task_struct
*task
= get_proc_task(inode
);
185 int result
= -ENOENT
;
188 result
= get_task_root(task
, path
);
189 put_task_struct(task
);
194 static int __check_mem_permission(struct task_struct
*task
)
197 * A task can always look at itself, in case it chooses
198 * to use system calls instead of load instructions.
204 * If current is actively ptrace'ing, and would also be
205 * permitted to freshly attach with ptrace now, permit it.
207 if (task_is_stopped_or_traced(task
)) {
210 match
= (tracehook_tracer_task(task
) == current
);
212 if (match
&& ptrace_may_access(task
, PTRACE_MODE_ATTACH
))
217 * Noone else is allowed.
223 * Return zero if current may access user memory in @task, -error if not.
225 static int check_mem_permission(struct task_struct
*task
)
230 * Avoid racing if task exec's as we might get a new mm but validate
231 * against old credentials.
233 err
= mutex_lock_killable(&task
->signal
->cred_guard_mutex
);
237 err
= __check_mem_permission(task
);
238 mutex_unlock(&task
->signal
->cred_guard_mutex
);
243 struct mm_struct
*mm_for_maps(struct task_struct
*task
)
245 struct mm_struct
*mm
;
248 err
= mutex_lock_killable(&task
->signal
->cred_guard_mutex
);
252 mm
= get_task_mm(task
);
253 if (mm
&& mm
!= current
->mm
&&
254 !ptrace_may_access(task
, PTRACE_MODE_READ
)) {
256 mm
= ERR_PTR(-EACCES
);
258 mutex_unlock(&task
->signal
->cred_guard_mutex
);
263 static int proc_pid_cmdline(struct task_struct
*task
, char * buffer
)
267 struct mm_struct
*mm
= get_task_mm(task
);
271 goto out_mm
; /* Shh! No looking before we're done */
273 len
= mm
->arg_end
- mm
->arg_start
;
278 res
= access_process_vm(task
, mm
->arg_start
, buffer
, len
, 0);
280 // If the nul at the end of args has been overwritten, then
281 // assume application is using setproctitle(3).
282 if (res
> 0 && buffer
[res
-1] != '\0' && len
< PAGE_SIZE
) {
283 len
= strnlen(buffer
, res
);
287 len
= mm
->env_end
- mm
->env_start
;
288 if (len
> PAGE_SIZE
- res
)
289 len
= PAGE_SIZE
- res
;
290 res
+= access_process_vm(task
, mm
->env_start
, buffer
+res
, len
, 0);
291 res
= strnlen(buffer
, res
);
300 static int proc_pid_auxv(struct task_struct
*task
, char *buffer
)
302 struct mm_struct
*mm
= mm_for_maps(task
);
303 int res
= PTR_ERR(mm
);
304 if (mm
&& !IS_ERR(mm
)) {
305 unsigned int nwords
= 0;
308 } while (mm
->saved_auxv
[nwords
- 2] != 0); /* AT_NULL */
309 res
= nwords
* sizeof(mm
->saved_auxv
[0]);
312 memcpy(buffer
, mm
->saved_auxv
, res
);
319 #ifdef CONFIG_KALLSYMS
321 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
322 * Returns the resolved symbol. If that fails, simply return the address.
324 static int proc_pid_wchan(struct task_struct
*task
, char *buffer
)
327 char symname
[KSYM_NAME_LEN
];
329 wchan
= get_wchan(task
);
331 if (lookup_symbol_name(wchan
, symname
) < 0)
332 if (!ptrace_may_access(task
, PTRACE_MODE_READ
))
335 return sprintf(buffer
, "%lu", wchan
);
337 return sprintf(buffer
, "%s", symname
);
339 #endif /* CONFIG_KALLSYMS */
341 #ifdef CONFIG_STACKTRACE
343 #define MAX_STACK_TRACE_DEPTH 64
345 static int proc_pid_stack(struct seq_file
*m
, struct pid_namespace
*ns
,
346 struct pid
*pid
, struct task_struct
*task
)
348 struct stack_trace trace
;
349 unsigned long *entries
;
352 entries
= kmalloc(MAX_STACK_TRACE_DEPTH
* sizeof(*entries
), GFP_KERNEL
);
356 trace
.nr_entries
= 0;
357 trace
.max_entries
= MAX_STACK_TRACE_DEPTH
;
358 trace
.entries
= entries
;
360 save_stack_trace_tsk(task
, &trace
);
362 for (i
= 0; i
< trace
.nr_entries
; i
++) {
363 seq_printf(m
, "[<%p>] %pS\n",
364 (void *)entries
[i
], (void *)entries
[i
]);
372 #ifdef CONFIG_SCHEDSTATS
374 * Provides /proc/PID/schedstat
376 static int proc_pid_schedstat(struct task_struct
*task
, char *buffer
)
378 return sprintf(buffer
, "%llu %llu %lu\n",
379 (unsigned long long)task
->se
.sum_exec_runtime
,
380 (unsigned long long)task
->sched_info
.run_delay
,
381 task
->sched_info
.pcount
);
385 #ifdef CONFIG_LATENCYTOP
386 static int lstats_show_proc(struct seq_file
*m
, void *v
)
389 struct inode
*inode
= m
->private;
390 struct task_struct
*task
= get_proc_task(inode
);
394 seq_puts(m
, "Latency Top version : v0.1\n");
395 for (i
= 0; i
< 32; i
++) {
396 struct latency_record
*lr
= &task
->latency_record
[i
];
397 if (lr
->backtrace
[0]) {
399 seq_printf(m
, "%i %li %li",
400 lr
->count
, lr
->time
, lr
->max
);
401 for (q
= 0; q
< LT_BACKTRACEDEPTH
; q
++) {
402 unsigned long bt
= lr
->backtrace
[q
];
407 seq_printf(m
, " %ps", (void *)bt
);
413 put_task_struct(task
);
417 static int lstats_open(struct inode
*inode
, struct file
*file
)
419 return single_open(file
, lstats_show_proc
, inode
);
422 static ssize_t
lstats_write(struct file
*file
, const char __user
*buf
,
423 size_t count
, loff_t
*offs
)
425 struct task_struct
*task
= get_proc_task(file
->f_dentry
->d_inode
);
429 clear_all_latency_tracing(task
);
430 put_task_struct(task
);
435 static const struct file_operations proc_lstats_operations
= {
438 .write
= lstats_write
,
440 .release
= single_release
,
445 static int proc_oom_score(struct task_struct
*task
, char *buffer
)
447 unsigned long points
= 0;
449 read_lock(&tasklist_lock
);
451 points
= oom_badness(task
, NULL
, NULL
,
452 totalram_pages
+ total_swap_pages
);
453 read_unlock(&tasklist_lock
);
454 return sprintf(buffer
, "%lu\n", points
);
462 static const struct limit_names lnames
[RLIM_NLIMITS
] = {
463 [RLIMIT_CPU
] = {"Max cpu time", "seconds"},
464 [RLIMIT_FSIZE
] = {"Max file size", "bytes"},
465 [RLIMIT_DATA
] = {"Max data size", "bytes"},
466 [RLIMIT_STACK
] = {"Max stack size", "bytes"},
467 [RLIMIT_CORE
] = {"Max core file size", "bytes"},
468 [RLIMIT_RSS
] = {"Max resident set", "bytes"},
469 [RLIMIT_NPROC
] = {"Max processes", "processes"},
470 [RLIMIT_NOFILE
] = {"Max open files", "files"},
471 [RLIMIT_MEMLOCK
] = {"Max locked memory", "bytes"},
472 [RLIMIT_AS
] = {"Max address space", "bytes"},
473 [RLIMIT_LOCKS
] = {"Max file locks", "locks"},
474 [RLIMIT_SIGPENDING
] = {"Max pending signals", "signals"},
475 [RLIMIT_MSGQUEUE
] = {"Max msgqueue size", "bytes"},
476 [RLIMIT_NICE
] = {"Max nice priority", NULL
},
477 [RLIMIT_RTPRIO
] = {"Max realtime priority", NULL
},
478 [RLIMIT_RTTIME
] = {"Max realtime timeout", "us"},
481 /* Display limits for a process */
482 static int proc_pid_limits(struct task_struct
*task
, char *buffer
)
487 char *bufptr
= buffer
;
489 struct rlimit rlim
[RLIM_NLIMITS
];
491 if (!lock_task_sighand(task
, &flags
))
493 memcpy(rlim
, task
->signal
->rlim
, sizeof(struct rlimit
) * RLIM_NLIMITS
);
494 unlock_task_sighand(task
, &flags
);
497 * print the file header
499 count
+= sprintf(&bufptr
[count
], "%-25s %-20s %-20s %-10s\n",
500 "Limit", "Soft Limit", "Hard Limit", "Units");
502 for (i
= 0; i
< RLIM_NLIMITS
; i
++) {
503 if (rlim
[i
].rlim_cur
== RLIM_INFINITY
)
504 count
+= sprintf(&bufptr
[count
], "%-25s %-20s ",
505 lnames
[i
].name
, "unlimited");
507 count
+= sprintf(&bufptr
[count
], "%-25s %-20lu ",
508 lnames
[i
].name
, rlim
[i
].rlim_cur
);
510 if (rlim
[i
].rlim_max
== RLIM_INFINITY
)
511 count
+= sprintf(&bufptr
[count
], "%-20s ", "unlimited");
513 count
+= sprintf(&bufptr
[count
], "%-20lu ",
517 count
+= sprintf(&bufptr
[count
], "%-10s\n",
520 count
+= sprintf(&bufptr
[count
], "\n");
526 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
527 static int proc_pid_syscall(struct task_struct
*task
, char *buffer
)
530 unsigned long args
[6], sp
, pc
;
532 if (task_current_syscall(task
, &nr
, args
, 6, &sp
, &pc
))
533 return sprintf(buffer
, "running\n");
536 return sprintf(buffer
, "%ld 0x%lx 0x%lx\n", nr
, sp
, pc
);
538 return sprintf(buffer
,
539 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
541 args
[0], args
[1], args
[2], args
[3], args
[4], args
[5],
544 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
546 /************************************************************************/
547 /* Here the fs part begins */
548 /************************************************************************/
550 /* permission checks */
551 static int proc_fd_access_allowed(struct inode
*inode
)
553 struct task_struct
*task
;
555 /* Allow access to a task's file descriptors if it is us or we
556 * may use ptrace attach to the process and find out that
559 task
= get_proc_task(inode
);
561 allowed
= ptrace_may_access(task
, PTRACE_MODE_READ
);
562 put_task_struct(task
);
567 static int proc_setattr(struct dentry
*dentry
, struct iattr
*attr
)
570 struct inode
*inode
= dentry
->d_inode
;
572 if (attr
->ia_valid
& ATTR_MODE
)
575 error
= inode_change_ok(inode
, attr
);
579 if ((attr
->ia_valid
& ATTR_SIZE
) &&
580 attr
->ia_size
!= i_size_read(inode
)) {
581 error
= vmtruncate(inode
, attr
->ia_size
);
586 setattr_copy(inode
, attr
);
587 mark_inode_dirty(inode
);
591 static const struct inode_operations proc_def_inode_operations
= {
592 .setattr
= proc_setattr
,
595 static int mounts_open_common(struct inode
*inode
, struct file
*file
,
596 const struct seq_operations
*op
)
598 struct task_struct
*task
= get_proc_task(inode
);
600 struct mnt_namespace
*ns
= NULL
;
602 struct proc_mounts
*p
;
607 nsp
= task_nsproxy(task
);
614 if (ns
&& get_task_root(task
, &root
) == 0)
616 put_task_struct(task
);
625 p
= kmalloc(sizeof(struct proc_mounts
), GFP_KERNEL
);
629 file
->private_data
= &p
->m
;
630 ret
= seq_open(file
, op
);
637 p
->event
= ns
->event
;
651 static int mounts_release(struct inode
*inode
, struct file
*file
)
653 struct proc_mounts
*p
= file
->private_data
;
656 return seq_release(inode
, file
);
659 static unsigned mounts_poll(struct file
*file
, poll_table
*wait
)
661 struct proc_mounts
*p
= file
->private_data
;
662 unsigned res
= POLLIN
| POLLRDNORM
;
664 poll_wait(file
, &p
->ns
->poll
, wait
);
665 if (mnt_had_events(p
))
666 res
|= POLLERR
| POLLPRI
;
671 static int mounts_open(struct inode
*inode
, struct file
*file
)
673 return mounts_open_common(inode
, file
, &mounts_op
);
676 static const struct file_operations proc_mounts_operations
= {
680 .release
= mounts_release
,
684 static int mountinfo_open(struct inode
*inode
, struct file
*file
)
686 return mounts_open_common(inode
, file
, &mountinfo_op
);
689 static const struct file_operations proc_mountinfo_operations
= {
690 .open
= mountinfo_open
,
693 .release
= mounts_release
,
697 static int mountstats_open(struct inode
*inode
, struct file
*file
)
699 return mounts_open_common(inode
, file
, &mountstats_op
);
702 static const struct file_operations proc_mountstats_operations
= {
703 .open
= mountstats_open
,
706 .release
= mounts_release
,
709 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
711 static ssize_t
proc_info_read(struct file
* file
, char __user
* buf
,
712 size_t count
, loff_t
*ppos
)
714 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
717 struct task_struct
*task
= get_proc_task(inode
);
723 if (count
> PROC_BLOCK_SIZE
)
724 count
= PROC_BLOCK_SIZE
;
727 if (!(page
= __get_free_page(GFP_TEMPORARY
)))
730 length
= PROC_I(inode
)->op
.proc_read(task
, (char*)page
);
733 length
= simple_read_from_buffer(buf
, count
, ppos
, (char *)page
, length
);
736 put_task_struct(task
);
741 static const struct file_operations proc_info_file_operations
= {
742 .read
= proc_info_read
,
743 .llseek
= generic_file_llseek
,
746 static int proc_single_show(struct seq_file
*m
, void *v
)
748 struct inode
*inode
= m
->private;
749 struct pid_namespace
*ns
;
751 struct task_struct
*task
;
754 ns
= inode
->i_sb
->s_fs_info
;
755 pid
= proc_pid(inode
);
756 task
= get_pid_task(pid
, PIDTYPE_PID
);
760 ret
= PROC_I(inode
)->op
.proc_show(m
, ns
, pid
, task
);
762 put_task_struct(task
);
766 static int proc_single_open(struct inode
*inode
, struct file
*filp
)
768 return single_open(filp
, proc_single_show
, inode
);
771 static const struct file_operations proc_single_file_operations
= {
772 .open
= proc_single_open
,
775 .release
= single_release
,
778 static int mem_open(struct inode
* inode
, struct file
* file
)
780 file
->private_data
= (void*)((long)current
->self_exec_id
);
781 /* OK to pass negative loff_t, we can catch out-of-range */
782 file
->f_mode
|= FMODE_UNSIGNED_OFFSET
;
786 static ssize_t
mem_read(struct file
* file
, char __user
* buf
,
787 size_t count
, loff_t
*ppos
)
789 struct task_struct
*task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
791 unsigned long src
= *ppos
;
793 struct mm_struct
*mm
;
798 if (check_mem_permission(task
))
802 page
= (char *)__get_free_page(GFP_TEMPORARY
);
808 mm
= get_task_mm(task
);
814 if (file
->private_data
!= (void*)((long)current
->self_exec_id
))
820 int this_len
, retval
;
822 this_len
= (count
> PAGE_SIZE
) ? PAGE_SIZE
: count
;
823 retval
= access_process_vm(task
, src
, page
, this_len
, 0);
824 if (!retval
|| check_mem_permission(task
)) {
830 if (copy_to_user(buf
, page
, retval
)) {
845 free_page((unsigned long) page
);
847 put_task_struct(task
);
852 #define mem_write NULL
855 /* This is a security hazard */
856 static ssize_t
mem_write(struct file
* file
, const char __user
*buf
,
857 size_t count
, loff_t
*ppos
)
861 struct task_struct
*task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
862 unsigned long dst
= *ppos
;
868 if (check_mem_permission(task
))
872 if (file
->private_data
!= (void *)((long)current
->self_exec_id
))
876 page
= (char *)__get_free_page(GFP_TEMPORARY
);
882 int this_len
, retval
;
884 this_len
= (count
> PAGE_SIZE
) ? PAGE_SIZE
: count
;
885 if (copy_from_user(page
, buf
, this_len
)) {
889 retval
= access_process_vm(task
, dst
, page
, this_len
, 1);
901 free_page((unsigned long) page
);
903 put_task_struct(task
);
909 loff_t
mem_lseek(struct file
*file
, loff_t offset
, int orig
)
913 file
->f_pos
= offset
;
916 file
->f_pos
+= offset
;
921 force_successful_syscall_return();
925 static const struct file_operations proc_mem_operations
= {
932 static ssize_t
environ_read(struct file
*file
, char __user
*buf
,
933 size_t count
, loff_t
*ppos
)
935 struct task_struct
*task
= get_proc_task(file
->f_dentry
->d_inode
);
937 unsigned long src
= *ppos
;
939 struct mm_struct
*mm
;
945 page
= (char *)__get_free_page(GFP_TEMPORARY
);
950 mm
= mm_for_maps(task
);
952 if (!mm
|| IS_ERR(mm
))
957 int this_len
, retval
, max_len
;
959 this_len
= mm
->env_end
- (mm
->env_start
+ src
);
964 max_len
= (count
> PAGE_SIZE
) ? PAGE_SIZE
: count
;
965 this_len
= (this_len
> max_len
) ? max_len
: this_len
;
967 retval
= access_process_vm(task
, (mm
->env_start
+ src
),
975 if (copy_to_user(buf
, page
, retval
)) {
989 free_page((unsigned long) page
);
991 put_task_struct(task
);
996 static const struct file_operations proc_environ_operations
= {
997 .read
= environ_read
,
998 .llseek
= generic_file_llseek
,
1001 static ssize_t
oom_adjust_read(struct file
*file
, char __user
*buf
,
1002 size_t count
, loff_t
*ppos
)
1004 struct task_struct
*task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
1005 char buffer
[PROC_NUMBUF
];
1007 int oom_adjust
= OOM_DISABLE
;
1008 unsigned long flags
;
1013 if (lock_task_sighand(task
, &flags
)) {
1014 oom_adjust
= task
->signal
->oom_adj
;
1015 unlock_task_sighand(task
, &flags
);
1018 put_task_struct(task
);
1020 len
= snprintf(buffer
, sizeof(buffer
), "%i\n", oom_adjust
);
1022 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
1025 static ssize_t
oom_adjust_write(struct file
*file
, const char __user
*buf
,
1026 size_t count
, loff_t
*ppos
)
1028 struct task_struct
*task
;
1029 char buffer
[PROC_NUMBUF
];
1031 unsigned long flags
;
1034 memset(buffer
, 0, sizeof(buffer
));
1035 if (count
> sizeof(buffer
) - 1)
1036 count
= sizeof(buffer
) - 1;
1037 if (copy_from_user(buffer
, buf
, count
)) {
1042 err
= strict_strtol(strstrip(buffer
), 0, &oom_adjust
);
1045 if ((oom_adjust
< OOM_ADJUST_MIN
|| oom_adjust
> OOM_ADJUST_MAX
) &&
1046 oom_adjust
!= OOM_DISABLE
) {
1051 task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
1063 if (!lock_task_sighand(task
, &flags
)) {
1068 if (oom_adjust
< task
->signal
->oom_adj
&& !capable(CAP_SYS_RESOURCE
)) {
1073 if (oom_adjust
!= task
->signal
->oom_adj
) {
1074 if (oom_adjust
== OOM_DISABLE
)
1075 atomic_inc(&task
->mm
->oom_disable_count
);
1076 if (task
->signal
->oom_adj
== OOM_DISABLE
)
1077 atomic_dec(&task
->mm
->oom_disable_count
);
1081 * Warn that /proc/pid/oom_adj is deprecated, see
1082 * Documentation/feature-removal-schedule.txt.
1084 printk_once(KERN_WARNING
"%s (%d): /proc/%d/oom_adj is deprecated, "
1085 "please use /proc/%d/oom_score_adj instead.\n",
1086 current
->comm
, task_pid_nr(current
),
1087 task_pid_nr(task
), task_pid_nr(task
));
1088 task
->signal
->oom_adj
= oom_adjust
;
1090 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1091 * value is always attainable.
1093 if (task
->signal
->oom_adj
== OOM_ADJUST_MAX
)
1094 task
->signal
->oom_score_adj
= OOM_SCORE_ADJ_MAX
;
1096 task
->signal
->oom_score_adj
= (oom_adjust
* OOM_SCORE_ADJ_MAX
) /
1099 unlock_task_sighand(task
, &flags
);
1102 put_task_struct(task
);
1104 return err
< 0 ? err
: count
;
1107 static const struct file_operations proc_oom_adjust_operations
= {
1108 .read
= oom_adjust_read
,
1109 .write
= oom_adjust_write
,
1110 .llseek
= generic_file_llseek
,
1113 static ssize_t
oom_score_adj_read(struct file
*file
, char __user
*buf
,
1114 size_t count
, loff_t
*ppos
)
1116 struct task_struct
*task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
1117 char buffer
[PROC_NUMBUF
];
1118 int oom_score_adj
= OOM_SCORE_ADJ_MIN
;
1119 unsigned long flags
;
1124 if (lock_task_sighand(task
, &flags
)) {
1125 oom_score_adj
= task
->signal
->oom_score_adj
;
1126 unlock_task_sighand(task
, &flags
);
1128 put_task_struct(task
);
1129 len
= snprintf(buffer
, sizeof(buffer
), "%d\n", oom_score_adj
);
1130 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
1133 static ssize_t
oom_score_adj_write(struct file
*file
, const char __user
*buf
,
1134 size_t count
, loff_t
*ppos
)
1136 struct task_struct
*task
;
1137 char buffer
[PROC_NUMBUF
];
1138 unsigned long flags
;
1142 memset(buffer
, 0, sizeof(buffer
));
1143 if (count
> sizeof(buffer
) - 1)
1144 count
= sizeof(buffer
) - 1;
1145 if (copy_from_user(buffer
, buf
, count
)) {
1150 err
= strict_strtol(strstrip(buffer
), 0, &oom_score_adj
);
1153 if (oom_score_adj
< OOM_SCORE_ADJ_MIN
||
1154 oom_score_adj
> OOM_SCORE_ADJ_MAX
) {
1159 task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
1171 if (!lock_task_sighand(task
, &flags
)) {
1176 if (oom_score_adj
< task
->signal
->oom_score_adj_min
&&
1177 !capable(CAP_SYS_RESOURCE
)) {
1182 if (oom_score_adj
!= task
->signal
->oom_score_adj
) {
1183 if (oom_score_adj
== OOM_SCORE_ADJ_MIN
)
1184 atomic_inc(&task
->mm
->oom_disable_count
);
1185 if (task
->signal
->oom_score_adj
== OOM_SCORE_ADJ_MIN
)
1186 atomic_dec(&task
->mm
->oom_disable_count
);
1188 task
->signal
->oom_score_adj
= oom_score_adj
;
1189 if (has_capability_noaudit(current
, CAP_SYS_RESOURCE
))
1190 task
->signal
->oom_score_adj_min
= oom_score_adj
;
1192 * Scale /proc/pid/oom_adj appropriately ensuring that OOM_DISABLE is
1193 * always attainable.
1195 if (task
->signal
->oom_score_adj
== OOM_SCORE_ADJ_MIN
)
1196 task
->signal
->oom_adj
= OOM_DISABLE
;
1198 task
->signal
->oom_adj
= (oom_score_adj
* OOM_ADJUST_MAX
) /
1201 unlock_task_sighand(task
, &flags
);
1204 put_task_struct(task
);
1206 return err
< 0 ? err
: count
;
1209 static const struct file_operations proc_oom_score_adj_operations
= {
1210 .read
= oom_score_adj_read
,
1211 .write
= oom_score_adj_write
,
1212 .llseek
= default_llseek
,
1215 #ifdef CONFIG_AUDITSYSCALL
1216 #define TMPBUFLEN 21
1217 static ssize_t
proc_loginuid_read(struct file
* file
, char __user
* buf
,
1218 size_t count
, loff_t
*ppos
)
1220 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
1221 struct task_struct
*task
= get_proc_task(inode
);
1223 char tmpbuf
[TMPBUFLEN
];
1227 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
1228 audit_get_loginuid(task
));
1229 put_task_struct(task
);
1230 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1233 static ssize_t
proc_loginuid_write(struct file
* file
, const char __user
* buf
,
1234 size_t count
, loff_t
*ppos
)
1236 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
1241 if (!capable(CAP_AUDIT_CONTROL
))
1245 if (current
!= pid_task(proc_pid(inode
), PIDTYPE_PID
)) {
1251 if (count
>= PAGE_SIZE
)
1252 count
= PAGE_SIZE
- 1;
1255 /* No partial writes. */
1258 page
= (char*)__get_free_page(GFP_TEMPORARY
);
1262 if (copy_from_user(page
, buf
, count
))
1266 loginuid
= simple_strtoul(page
, &tmp
, 10);
1272 length
= audit_set_loginuid(current
, loginuid
);
1273 if (likely(length
== 0))
1277 free_page((unsigned long) page
);
1281 static const struct file_operations proc_loginuid_operations
= {
1282 .read
= proc_loginuid_read
,
1283 .write
= proc_loginuid_write
,
1284 .llseek
= generic_file_llseek
,
1287 static ssize_t
proc_sessionid_read(struct file
* file
, char __user
* buf
,
1288 size_t count
, loff_t
*ppos
)
1290 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
1291 struct task_struct
*task
= get_proc_task(inode
);
1293 char tmpbuf
[TMPBUFLEN
];
1297 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
1298 audit_get_sessionid(task
));
1299 put_task_struct(task
);
1300 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1303 static const struct file_operations proc_sessionid_operations
= {
1304 .read
= proc_sessionid_read
,
1305 .llseek
= generic_file_llseek
,
1309 #ifdef CONFIG_FAULT_INJECTION
1310 static ssize_t
proc_fault_inject_read(struct file
* file
, char __user
* buf
,
1311 size_t count
, loff_t
*ppos
)
1313 struct task_struct
*task
= get_proc_task(file
->f_dentry
->d_inode
);
1314 char buffer
[PROC_NUMBUF
];
1320 make_it_fail
= task
->make_it_fail
;
1321 put_task_struct(task
);
1323 len
= snprintf(buffer
, sizeof(buffer
), "%i\n", make_it_fail
);
1325 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
1328 static ssize_t
proc_fault_inject_write(struct file
* file
,
1329 const char __user
* buf
, size_t count
, loff_t
*ppos
)
1331 struct task_struct
*task
;
1332 char buffer
[PROC_NUMBUF
], *end
;
1335 if (!capable(CAP_SYS_RESOURCE
))
1337 memset(buffer
, 0, sizeof(buffer
));
1338 if (count
> sizeof(buffer
) - 1)
1339 count
= sizeof(buffer
) - 1;
1340 if (copy_from_user(buffer
, buf
, count
))
1342 make_it_fail
= simple_strtol(strstrip(buffer
), &end
, 0);
1345 task
= get_proc_task(file
->f_dentry
->d_inode
);
1348 task
->make_it_fail
= make_it_fail
;
1349 put_task_struct(task
);
1354 static const struct file_operations proc_fault_inject_operations
= {
1355 .read
= proc_fault_inject_read
,
1356 .write
= proc_fault_inject_write
,
1357 .llseek
= generic_file_llseek
,
1362 #ifdef CONFIG_SCHED_DEBUG
1364 * Print out various scheduling related per-task fields:
1366 static int sched_show(struct seq_file
*m
, void *v
)
1368 struct inode
*inode
= m
->private;
1369 struct task_struct
*p
;
1371 p
= get_proc_task(inode
);
1374 proc_sched_show_task(p
, m
);
1382 sched_write(struct file
*file
, const char __user
*buf
,
1383 size_t count
, loff_t
*offset
)
1385 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1386 struct task_struct
*p
;
1388 p
= get_proc_task(inode
);
1391 proc_sched_set_task(p
);
1398 static int sched_open(struct inode
*inode
, struct file
*filp
)
1400 return single_open(filp
, sched_show
, inode
);
1403 static const struct file_operations proc_pid_sched_operations
= {
1406 .write
= sched_write
,
1407 .llseek
= seq_lseek
,
1408 .release
= single_release
,
1413 #ifdef CONFIG_SCHED_AUTOGROUP
1415 * Print out autogroup related information:
1417 static int sched_autogroup_show(struct seq_file
*m
, void *v
)
1419 struct inode
*inode
= m
->private;
1420 struct task_struct
*p
;
1422 p
= get_proc_task(inode
);
1425 proc_sched_autogroup_show_task(p
, m
);
1433 sched_autogroup_write(struct file
*file
, const char __user
*buf
,
1434 size_t count
, loff_t
*offset
)
1436 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1437 struct task_struct
*p
;
1438 char buffer
[PROC_NUMBUF
];
1442 memset(buffer
, 0, sizeof(buffer
));
1443 if (count
> sizeof(buffer
) - 1)
1444 count
= sizeof(buffer
) - 1;
1445 if (copy_from_user(buffer
, buf
, count
))
1448 err
= strict_strtol(strstrip(buffer
), 0, &nice
);
1452 p
= get_proc_task(inode
);
1457 err
= proc_sched_autogroup_set_nice(p
, &err
);
1466 static int sched_autogroup_open(struct inode
*inode
, struct file
*filp
)
1470 ret
= single_open(filp
, sched_autogroup_show
, NULL
);
1472 struct seq_file
*m
= filp
->private_data
;
1479 static const struct file_operations proc_pid_sched_autogroup_operations
= {
1480 .open
= sched_autogroup_open
,
1482 .write
= sched_autogroup_write
,
1483 .llseek
= seq_lseek
,
1484 .release
= single_release
,
1487 #endif /* CONFIG_SCHED_AUTOGROUP */
1489 static ssize_t
comm_write(struct file
*file
, const char __user
*buf
,
1490 size_t count
, loff_t
*offset
)
1492 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1493 struct task_struct
*p
;
1494 char buffer
[TASK_COMM_LEN
];
1496 memset(buffer
, 0, sizeof(buffer
));
1497 if (count
> sizeof(buffer
) - 1)
1498 count
= sizeof(buffer
) - 1;
1499 if (copy_from_user(buffer
, buf
, count
))
1502 p
= get_proc_task(inode
);
1506 if (same_thread_group(current
, p
))
1507 set_task_comm(p
, buffer
);
1516 static int comm_show(struct seq_file
*m
, void *v
)
1518 struct inode
*inode
= m
->private;
1519 struct task_struct
*p
;
1521 p
= get_proc_task(inode
);
1526 seq_printf(m
, "%s\n", p
->comm
);
1534 static int comm_open(struct inode
*inode
, struct file
*filp
)
1536 return single_open(filp
, comm_show
, inode
);
1539 static const struct file_operations proc_pid_set_comm_operations
= {
1542 .write
= comm_write
,
1543 .llseek
= seq_lseek
,
1544 .release
= single_release
,
1548 * We added or removed a vma mapping the executable. The vmas are only mapped
1549 * during exec and are not mapped with the mmap system call.
1550 * Callers must hold down_write() on the mm's mmap_sem for these
1552 void added_exe_file_vma(struct mm_struct
*mm
)
1554 mm
->num_exe_file_vmas
++;
1557 void removed_exe_file_vma(struct mm_struct
*mm
)
1559 mm
->num_exe_file_vmas
--;
1560 if ((mm
->num_exe_file_vmas
== 0) && mm
->exe_file
){
1562 mm
->exe_file
= NULL
;
1567 void set_mm_exe_file(struct mm_struct
*mm
, struct file
*new_exe_file
)
1570 get_file(new_exe_file
);
1573 mm
->exe_file
= new_exe_file
;
1574 mm
->num_exe_file_vmas
= 0;
1577 struct file
*get_mm_exe_file(struct mm_struct
*mm
)
1579 struct file
*exe_file
;
1581 /* We need mmap_sem to protect against races with removal of
1582 * VM_EXECUTABLE vmas */
1583 down_read(&mm
->mmap_sem
);
1584 exe_file
= mm
->exe_file
;
1587 up_read(&mm
->mmap_sem
);
1591 void dup_mm_exe_file(struct mm_struct
*oldmm
, struct mm_struct
*newmm
)
1593 /* It's safe to write the exe_file pointer without exe_file_lock because
1594 * this is called during fork when the task is not yet in /proc */
1595 newmm
->exe_file
= get_mm_exe_file(oldmm
);
1598 static int proc_exe_link(struct inode
*inode
, struct path
*exe_path
)
1600 struct task_struct
*task
;
1601 struct mm_struct
*mm
;
1602 struct file
*exe_file
;
1604 task
= get_proc_task(inode
);
1607 mm
= get_task_mm(task
);
1608 put_task_struct(task
);
1611 exe_file
= get_mm_exe_file(mm
);
1614 *exe_path
= exe_file
->f_path
;
1615 path_get(&exe_file
->f_path
);
1622 static void *proc_pid_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
1624 struct inode
*inode
= dentry
->d_inode
;
1625 int error
= -EACCES
;
1627 /* We don't need a base pointer in the /proc filesystem */
1628 path_put(&nd
->path
);
1630 /* Are we allowed to snoop on the tasks file descriptors? */
1631 if (!proc_fd_access_allowed(inode
))
1634 error
= PROC_I(inode
)->op
.proc_get_link(inode
, &nd
->path
);
1636 return ERR_PTR(error
);
1639 static int do_proc_readlink(struct path
*path
, char __user
*buffer
, int buflen
)
1641 char *tmp
= (char*)__get_free_page(GFP_TEMPORARY
);
1648 pathname
= d_path(path
, tmp
, PAGE_SIZE
);
1649 len
= PTR_ERR(pathname
);
1650 if (IS_ERR(pathname
))
1652 len
= tmp
+ PAGE_SIZE
- 1 - pathname
;
1656 if (copy_to_user(buffer
, pathname
, len
))
1659 free_page((unsigned long)tmp
);
1663 static int proc_pid_readlink(struct dentry
* dentry
, char __user
* buffer
, int buflen
)
1665 int error
= -EACCES
;
1666 struct inode
*inode
= dentry
->d_inode
;
1669 /* Are we allowed to snoop on the tasks file descriptors? */
1670 if (!proc_fd_access_allowed(inode
))
1673 error
= PROC_I(inode
)->op
.proc_get_link(inode
, &path
);
1677 error
= do_proc_readlink(&path
, buffer
, buflen
);
1683 static const struct inode_operations proc_pid_link_inode_operations
= {
1684 .readlink
= proc_pid_readlink
,
1685 .follow_link
= proc_pid_follow_link
,
1686 .setattr
= proc_setattr
,
1690 /* building an inode */
1692 static int task_dumpable(struct task_struct
*task
)
1695 struct mm_struct
*mm
;
1700 dumpable
= get_dumpable(mm
);
1708 static struct inode
*proc_pid_make_inode(struct super_block
* sb
, struct task_struct
*task
)
1710 struct inode
* inode
;
1711 struct proc_inode
*ei
;
1712 const struct cred
*cred
;
1714 /* We need a new inode */
1716 inode
= new_inode(sb
);
1722 inode
->i_ino
= get_next_ino();
1723 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1724 inode
->i_op
= &proc_def_inode_operations
;
1727 * grab the reference to task.
1729 ei
->pid
= get_task_pid(task
, PIDTYPE_PID
);
1733 if (task_dumpable(task
)) {
1735 cred
= __task_cred(task
);
1736 inode
->i_uid
= cred
->euid
;
1737 inode
->i_gid
= cred
->egid
;
1740 security_task_to_inode(task
, inode
);
1750 static int pid_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
1752 struct inode
*inode
= dentry
->d_inode
;
1753 struct task_struct
*task
;
1754 const struct cred
*cred
;
1756 generic_fillattr(inode
, stat
);
1761 task
= pid_task(proc_pid(inode
), PIDTYPE_PID
);
1763 if ((inode
->i_mode
== (S_IFDIR
|S_IRUGO
|S_IXUGO
)) ||
1764 task_dumpable(task
)) {
1765 cred
= __task_cred(task
);
1766 stat
->uid
= cred
->euid
;
1767 stat
->gid
= cred
->egid
;
1777 * Exceptional case: normally we are not allowed to unhash a busy
1778 * directory. In this case, however, we can do it - no aliasing problems
1779 * due to the way we treat inodes.
1781 * Rewrite the inode's ownerships here because the owning task may have
1782 * performed a setuid(), etc.
1784 * Before the /proc/pid/status file was created the only way to read
1785 * the effective uid of a /process was to stat /proc/pid. Reading
1786 * /proc/pid/status is slow enough that procps and other packages
1787 * kept stating /proc/pid. To keep the rules in /proc simple I have
1788 * made this apply to all per process world readable and executable
1791 static int pid_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1793 struct inode
*inode
;
1794 struct task_struct
*task
;
1795 const struct cred
*cred
;
1797 if (nd
&& nd
->flags
& LOOKUP_RCU
)
1800 inode
= dentry
->d_inode
;
1801 task
= get_proc_task(inode
);
1804 if ((inode
->i_mode
== (S_IFDIR
|S_IRUGO
|S_IXUGO
)) ||
1805 task_dumpable(task
)) {
1807 cred
= __task_cred(task
);
1808 inode
->i_uid
= cred
->euid
;
1809 inode
->i_gid
= cred
->egid
;
1815 inode
->i_mode
&= ~(S_ISUID
| S_ISGID
);
1816 security_task_to_inode(task
, inode
);
1817 put_task_struct(task
);
1824 static int pid_delete_dentry(const struct dentry
* dentry
)
1826 /* Is the task we represent dead?
1827 * If so, then don't put the dentry on the lru list,
1828 * kill it immediately.
1830 return !proc_pid(dentry
->d_inode
)->tasks
[PIDTYPE_PID
].first
;
1833 static const struct dentry_operations pid_dentry_operations
=
1835 .d_revalidate
= pid_revalidate
,
1836 .d_delete
= pid_delete_dentry
,
1841 typedef struct dentry
*instantiate_t(struct inode
*, struct dentry
*,
1842 struct task_struct
*, const void *);
1845 * Fill a directory entry.
1847 * If possible create the dcache entry and derive our inode number and
1848 * file type from dcache entry.
1850 * Since all of the proc inode numbers are dynamically generated, the inode
1851 * numbers do not exist until the inode is cache. This means creating the
1852 * the dcache entry in readdir is necessary to keep the inode numbers
1853 * reported by readdir in sync with the inode numbers reported
1856 static int proc_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
1857 char *name
, int len
,
1858 instantiate_t instantiate
, struct task_struct
*task
, const void *ptr
)
1860 struct dentry
*child
, *dir
= filp
->f_path
.dentry
;
1861 struct inode
*inode
;
1864 unsigned type
= DT_UNKNOWN
;
1868 qname
.hash
= full_name_hash(name
, len
);
1870 child
= d_lookup(dir
, &qname
);
1873 new = d_alloc(dir
, &qname
);
1875 child
= instantiate(dir
->d_inode
, new, task
, ptr
);
1882 if (!child
|| IS_ERR(child
) || !child
->d_inode
)
1883 goto end_instantiate
;
1884 inode
= child
->d_inode
;
1887 type
= inode
->i_mode
>> 12;
1892 ino
= find_inode_number(dir
, &qname
);
1895 return filldir(dirent
, name
, len
, filp
->f_pos
, ino
, type
);
1898 static unsigned name_to_int(struct dentry
*dentry
)
1900 const char *name
= dentry
->d_name
.name
;
1901 int len
= dentry
->d_name
.len
;
1904 if (len
> 1 && *name
== '0')
1907 unsigned c
= *name
++ - '0';
1910 if (n
>= (~0U-9)/10)
1920 #define PROC_FDINFO_MAX 64
1922 static int proc_fd_info(struct inode
*inode
, struct path
*path
, char *info
)
1924 struct task_struct
*task
= get_proc_task(inode
);
1925 struct files_struct
*files
= NULL
;
1927 int fd
= proc_fd(inode
);
1930 files
= get_files_struct(task
);
1931 put_task_struct(task
);
1935 * We are not taking a ref to the file structure, so we must
1938 spin_lock(&files
->file_lock
);
1939 file
= fcheck_files(files
, fd
);
1942 *path
= file
->f_path
;
1943 path_get(&file
->f_path
);
1946 snprintf(info
, PROC_FDINFO_MAX
,
1949 (long long) file
->f_pos
,
1951 spin_unlock(&files
->file_lock
);
1952 put_files_struct(files
);
1955 spin_unlock(&files
->file_lock
);
1956 put_files_struct(files
);
1961 static int proc_fd_link(struct inode
*inode
, struct path
*path
)
1963 return proc_fd_info(inode
, path
, NULL
);
1966 static int tid_fd_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1968 struct inode
*inode
;
1969 struct task_struct
*task
;
1971 struct files_struct
*files
;
1972 const struct cred
*cred
;
1974 if (nd
&& nd
->flags
& LOOKUP_RCU
)
1977 inode
= dentry
->d_inode
;
1978 task
= get_proc_task(inode
);
1979 fd
= proc_fd(inode
);
1982 files
= get_files_struct(task
);
1985 if (fcheck_files(files
, fd
)) {
1987 put_files_struct(files
);
1988 if (task_dumpable(task
)) {
1990 cred
= __task_cred(task
);
1991 inode
->i_uid
= cred
->euid
;
1992 inode
->i_gid
= cred
->egid
;
1998 inode
->i_mode
&= ~(S_ISUID
| S_ISGID
);
1999 security_task_to_inode(task
, inode
);
2000 put_task_struct(task
);
2004 put_files_struct(files
);
2006 put_task_struct(task
);
2012 static const struct dentry_operations tid_fd_dentry_operations
=
2014 .d_revalidate
= tid_fd_revalidate
,
2015 .d_delete
= pid_delete_dentry
,
2018 static struct dentry
*proc_fd_instantiate(struct inode
*dir
,
2019 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
2021 unsigned fd
= *(const unsigned *)ptr
;
2023 struct files_struct
*files
;
2024 struct inode
*inode
;
2025 struct proc_inode
*ei
;
2026 struct dentry
*error
= ERR_PTR(-ENOENT
);
2028 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
2033 files
= get_files_struct(task
);
2036 inode
->i_mode
= S_IFLNK
;
2039 * We are not taking a ref to the file structure, so we must
2042 spin_lock(&files
->file_lock
);
2043 file
= fcheck_files(files
, fd
);
2046 if (file
->f_mode
& FMODE_READ
)
2047 inode
->i_mode
|= S_IRUSR
| S_IXUSR
;
2048 if (file
->f_mode
& FMODE_WRITE
)
2049 inode
->i_mode
|= S_IWUSR
| S_IXUSR
;
2050 spin_unlock(&files
->file_lock
);
2051 put_files_struct(files
);
2053 inode
->i_op
= &proc_pid_link_inode_operations
;
2055 ei
->op
.proc_get_link
= proc_fd_link
;
2056 d_set_d_op(dentry
, &tid_fd_dentry_operations
);
2057 d_add(dentry
, inode
);
2058 /* Close the race of the process dying before we return the dentry */
2059 if (tid_fd_revalidate(dentry
, NULL
))
2065 spin_unlock(&files
->file_lock
);
2066 put_files_struct(files
);
2072 static struct dentry
*proc_lookupfd_common(struct inode
*dir
,
2073 struct dentry
*dentry
,
2074 instantiate_t instantiate
)
2076 struct task_struct
*task
= get_proc_task(dir
);
2077 unsigned fd
= name_to_int(dentry
);
2078 struct dentry
*result
= ERR_PTR(-ENOENT
);
2085 result
= instantiate(dir
, dentry
, task
, &fd
);
2087 put_task_struct(task
);
2092 static int proc_readfd_common(struct file
* filp
, void * dirent
,
2093 filldir_t filldir
, instantiate_t instantiate
)
2095 struct dentry
*dentry
= filp
->f_path
.dentry
;
2096 struct inode
*inode
= dentry
->d_inode
;
2097 struct task_struct
*p
= get_proc_task(inode
);
2098 unsigned int fd
, ino
;
2100 struct files_struct
* files
;
2110 if (filldir(dirent
, ".", 1, 0, inode
->i_ino
, DT_DIR
) < 0)
2114 ino
= parent_ino(dentry
);
2115 if (filldir(dirent
, "..", 2, 1, ino
, DT_DIR
) < 0)
2119 files
= get_files_struct(p
);
2123 for (fd
= filp
->f_pos
-2;
2124 fd
< files_fdtable(files
)->max_fds
;
2125 fd
++, filp
->f_pos
++) {
2126 char name
[PROC_NUMBUF
];
2129 if (!fcheck_files(files
, fd
))
2133 len
= snprintf(name
, sizeof(name
), "%d", fd
);
2134 if (proc_fill_cache(filp
, dirent
, filldir
,
2135 name
, len
, instantiate
,
2143 put_files_struct(files
);
2151 static struct dentry
*proc_lookupfd(struct inode
*dir
, struct dentry
*dentry
,
2152 struct nameidata
*nd
)
2154 return proc_lookupfd_common(dir
, dentry
, proc_fd_instantiate
);
2157 static int proc_readfd(struct file
*filp
, void *dirent
, filldir_t filldir
)
2159 return proc_readfd_common(filp
, dirent
, filldir
, proc_fd_instantiate
);
2162 static ssize_t
proc_fdinfo_read(struct file
*file
, char __user
*buf
,
2163 size_t len
, loff_t
*ppos
)
2165 char tmp
[PROC_FDINFO_MAX
];
2166 int err
= proc_fd_info(file
->f_path
.dentry
->d_inode
, NULL
, tmp
);
2168 err
= simple_read_from_buffer(buf
, len
, ppos
, tmp
, strlen(tmp
));
2172 static const struct file_operations proc_fdinfo_file_operations
= {
2173 .open
= nonseekable_open
,
2174 .read
= proc_fdinfo_read
,
2175 .llseek
= no_llseek
,
2178 static const struct file_operations proc_fd_operations
= {
2179 .read
= generic_read_dir
,
2180 .readdir
= proc_readfd
,
2181 .llseek
= default_llseek
,
2185 * /proc/pid/fd needs a special permission handler so that a process can still
2186 * access /proc/self/fd after it has executed a setuid().
2188 static int proc_fd_permission(struct inode
*inode
, int mask
, unsigned int flags
)
2192 if (flags
& IPERM_FLAG_RCU
)
2194 rv
= generic_permission(inode
, mask
, flags
, NULL
);
2197 if (task_pid(current
) == proc_pid(inode
))
2203 * proc directories can do almost nothing..
2205 static const struct inode_operations proc_fd_inode_operations
= {
2206 .lookup
= proc_lookupfd
,
2207 .permission
= proc_fd_permission
,
2208 .setattr
= proc_setattr
,
2211 static struct dentry
*proc_fdinfo_instantiate(struct inode
*dir
,
2212 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
2214 unsigned fd
= *(unsigned *)ptr
;
2215 struct inode
*inode
;
2216 struct proc_inode
*ei
;
2217 struct dentry
*error
= ERR_PTR(-ENOENT
);
2219 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
2224 inode
->i_mode
= S_IFREG
| S_IRUSR
;
2225 inode
->i_fop
= &proc_fdinfo_file_operations
;
2226 d_set_d_op(dentry
, &tid_fd_dentry_operations
);
2227 d_add(dentry
, inode
);
2228 /* Close the race of the process dying before we return the dentry */
2229 if (tid_fd_revalidate(dentry
, NULL
))
2236 static struct dentry
*proc_lookupfdinfo(struct inode
*dir
,
2237 struct dentry
*dentry
,
2238 struct nameidata
*nd
)
2240 return proc_lookupfd_common(dir
, dentry
, proc_fdinfo_instantiate
);
2243 static int proc_readfdinfo(struct file
*filp
, void *dirent
, filldir_t filldir
)
2245 return proc_readfd_common(filp
, dirent
, filldir
,
2246 proc_fdinfo_instantiate
);
2249 static const struct file_operations proc_fdinfo_operations
= {
2250 .read
= generic_read_dir
,
2251 .readdir
= proc_readfdinfo
,
2252 .llseek
= default_llseek
,
2256 * proc directories can do almost nothing..
2258 static const struct inode_operations proc_fdinfo_inode_operations
= {
2259 .lookup
= proc_lookupfdinfo
,
2260 .setattr
= proc_setattr
,
2264 static struct dentry
*proc_pident_instantiate(struct inode
*dir
,
2265 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
2267 const struct pid_entry
*p
= ptr
;
2268 struct inode
*inode
;
2269 struct proc_inode
*ei
;
2270 struct dentry
*error
= ERR_PTR(-ENOENT
);
2272 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
2277 inode
->i_mode
= p
->mode
;
2278 if (S_ISDIR(inode
->i_mode
))
2279 inode
->i_nlink
= 2; /* Use getattr to fix if necessary */
2281 inode
->i_op
= p
->iop
;
2283 inode
->i_fop
= p
->fop
;
2285 d_set_d_op(dentry
, &pid_dentry_operations
);
2286 d_add(dentry
, inode
);
2287 /* Close the race of the process dying before we return the dentry */
2288 if (pid_revalidate(dentry
, NULL
))
2294 static struct dentry
*proc_pident_lookup(struct inode
*dir
,
2295 struct dentry
*dentry
,
2296 const struct pid_entry
*ents
,
2299 struct dentry
*error
;
2300 struct task_struct
*task
= get_proc_task(dir
);
2301 const struct pid_entry
*p
, *last
;
2303 error
= ERR_PTR(-ENOENT
);
2309 * Yes, it does not scale. And it should not. Don't add
2310 * new entries into /proc/<tgid>/ without very good reasons.
2312 last
= &ents
[nents
- 1];
2313 for (p
= ents
; p
<= last
; p
++) {
2314 if (p
->len
!= dentry
->d_name
.len
)
2316 if (!memcmp(dentry
->d_name
.name
, p
->name
, p
->len
))
2322 error
= proc_pident_instantiate(dir
, dentry
, task
, p
);
2324 put_task_struct(task
);
2329 static int proc_pident_fill_cache(struct file
*filp
, void *dirent
,
2330 filldir_t filldir
, struct task_struct
*task
, const struct pid_entry
*p
)
2332 return proc_fill_cache(filp
, dirent
, filldir
, p
->name
, p
->len
,
2333 proc_pident_instantiate
, task
, p
);
2336 static int proc_pident_readdir(struct file
*filp
,
2337 void *dirent
, filldir_t filldir
,
2338 const struct pid_entry
*ents
, unsigned int nents
)
2341 struct dentry
*dentry
= filp
->f_path
.dentry
;
2342 struct inode
*inode
= dentry
->d_inode
;
2343 struct task_struct
*task
= get_proc_task(inode
);
2344 const struct pid_entry
*p
, *last
;
2357 if (filldir(dirent
, ".", 1, i
, ino
, DT_DIR
) < 0)
2363 ino
= parent_ino(dentry
);
2364 if (filldir(dirent
, "..", 2, i
, ino
, DT_DIR
) < 0)
2376 last
= &ents
[nents
- 1];
2378 if (proc_pident_fill_cache(filp
, dirent
, filldir
, task
, p
) < 0)
2387 put_task_struct(task
);
2392 #ifdef CONFIG_SECURITY
2393 static ssize_t
proc_pid_attr_read(struct file
* file
, char __user
* buf
,
2394 size_t count
, loff_t
*ppos
)
2396 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
2399 struct task_struct
*task
= get_proc_task(inode
);
2404 length
= security_getprocattr(task
,
2405 (char*)file
->f_path
.dentry
->d_name
.name
,
2407 put_task_struct(task
);
2409 length
= simple_read_from_buffer(buf
, count
, ppos
, p
, length
);
2414 static ssize_t
proc_pid_attr_write(struct file
* file
, const char __user
* buf
,
2415 size_t count
, loff_t
*ppos
)
2417 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
2420 struct task_struct
*task
= get_proc_task(inode
);
2425 if (count
> PAGE_SIZE
)
2428 /* No partial writes. */
2434 page
= (char*)__get_free_page(GFP_TEMPORARY
);
2439 if (copy_from_user(page
, buf
, count
))
2442 /* Guard against adverse ptrace interaction */
2443 length
= mutex_lock_interruptible(&task
->signal
->cred_guard_mutex
);
2447 length
= security_setprocattr(task
,
2448 (char*)file
->f_path
.dentry
->d_name
.name
,
2449 (void*)page
, count
);
2450 mutex_unlock(&task
->signal
->cred_guard_mutex
);
2452 free_page((unsigned long) page
);
2454 put_task_struct(task
);
2459 static const struct file_operations proc_pid_attr_operations
= {
2460 .read
= proc_pid_attr_read
,
2461 .write
= proc_pid_attr_write
,
2462 .llseek
= generic_file_llseek
,
2465 static const struct pid_entry attr_dir_stuff
[] = {
2466 REG("current", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2467 REG("prev", S_IRUGO
, proc_pid_attr_operations
),
2468 REG("exec", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2469 REG("fscreate", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2470 REG("keycreate", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2471 REG("sockcreate", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2474 static int proc_attr_dir_readdir(struct file
* filp
,
2475 void * dirent
, filldir_t filldir
)
2477 return proc_pident_readdir(filp
,dirent
,filldir
,
2478 attr_dir_stuff
,ARRAY_SIZE(attr_dir_stuff
));
2481 static const struct file_operations proc_attr_dir_operations
= {
2482 .read
= generic_read_dir
,
2483 .readdir
= proc_attr_dir_readdir
,
2484 .llseek
= default_llseek
,
2487 static struct dentry
*proc_attr_dir_lookup(struct inode
*dir
,
2488 struct dentry
*dentry
, struct nameidata
*nd
)
2490 return proc_pident_lookup(dir
, dentry
,
2491 attr_dir_stuff
, ARRAY_SIZE(attr_dir_stuff
));
2494 static const struct inode_operations proc_attr_dir_inode_operations
= {
2495 .lookup
= proc_attr_dir_lookup
,
2496 .getattr
= pid_getattr
,
2497 .setattr
= proc_setattr
,
2502 #ifdef CONFIG_ELF_CORE
2503 static ssize_t
proc_coredump_filter_read(struct file
*file
, char __user
*buf
,
2504 size_t count
, loff_t
*ppos
)
2506 struct task_struct
*task
= get_proc_task(file
->f_dentry
->d_inode
);
2507 struct mm_struct
*mm
;
2508 char buffer
[PROC_NUMBUF
];
2516 mm
= get_task_mm(task
);
2518 len
= snprintf(buffer
, sizeof(buffer
), "%08lx\n",
2519 ((mm
->flags
& MMF_DUMP_FILTER_MASK
) >>
2520 MMF_DUMP_FILTER_SHIFT
));
2522 ret
= simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
2525 put_task_struct(task
);
2530 static ssize_t
proc_coredump_filter_write(struct file
*file
,
2531 const char __user
*buf
,
2535 struct task_struct
*task
;
2536 struct mm_struct
*mm
;
2537 char buffer
[PROC_NUMBUF
], *end
;
2544 memset(buffer
, 0, sizeof(buffer
));
2545 if (count
> sizeof(buffer
) - 1)
2546 count
= sizeof(buffer
) - 1;
2547 if (copy_from_user(buffer
, buf
, count
))
2551 val
= (unsigned int)simple_strtoul(buffer
, &end
, 0);
2554 if (end
- buffer
== 0)
2558 task
= get_proc_task(file
->f_dentry
->d_inode
);
2563 mm
= get_task_mm(task
);
2567 for (i
= 0, mask
= 1; i
< MMF_DUMP_FILTER_BITS
; i
++, mask
<<= 1) {
2569 set_bit(i
+ MMF_DUMP_FILTER_SHIFT
, &mm
->flags
);
2571 clear_bit(i
+ MMF_DUMP_FILTER_SHIFT
, &mm
->flags
);
2576 put_task_struct(task
);
2581 static const struct file_operations proc_coredump_filter_operations
= {
2582 .read
= proc_coredump_filter_read
,
2583 .write
= proc_coredump_filter_write
,
2584 .llseek
= generic_file_llseek
,
2591 static int proc_self_readlink(struct dentry
*dentry
, char __user
*buffer
,
2594 struct pid_namespace
*ns
= dentry
->d_sb
->s_fs_info
;
2595 pid_t tgid
= task_tgid_nr_ns(current
, ns
);
2596 char tmp
[PROC_NUMBUF
];
2599 sprintf(tmp
, "%d", tgid
);
2600 return vfs_readlink(dentry
,buffer
,buflen
,tmp
);
2603 static void *proc_self_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
2605 struct pid_namespace
*ns
= dentry
->d_sb
->s_fs_info
;
2606 pid_t tgid
= task_tgid_nr_ns(current
, ns
);
2607 char *name
= ERR_PTR(-ENOENT
);
2611 name
= ERR_PTR(-ENOMEM
);
2613 sprintf(name
, "%d", tgid
);
2615 nd_set_link(nd
, name
);
2619 static void proc_self_put_link(struct dentry
*dentry
, struct nameidata
*nd
,
2622 char *s
= nd_get_link(nd
);
2627 static const struct inode_operations proc_self_inode_operations
= {
2628 .readlink
= proc_self_readlink
,
2629 .follow_link
= proc_self_follow_link
,
2630 .put_link
= proc_self_put_link
,
2636 * These are the directory entries in the root directory of /proc
2637 * that properly belong to the /proc filesystem, as they describe
2638 * describe something that is process related.
2640 static const struct pid_entry proc_base_stuff
[] = {
2641 NOD("self", S_IFLNK
|S_IRWXUGO
,
2642 &proc_self_inode_operations
, NULL
, {}),
2645 static struct dentry
*proc_base_instantiate(struct inode
*dir
,
2646 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
2648 const struct pid_entry
*p
= ptr
;
2649 struct inode
*inode
;
2650 struct proc_inode
*ei
;
2651 struct dentry
*error
;
2653 /* Allocate the inode */
2654 error
= ERR_PTR(-ENOMEM
);
2655 inode
= new_inode(dir
->i_sb
);
2659 /* Initialize the inode */
2661 inode
->i_ino
= get_next_ino();
2662 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
2665 * grab the reference to the task.
2667 ei
->pid
= get_task_pid(task
, PIDTYPE_PID
);
2671 inode
->i_mode
= p
->mode
;
2672 if (S_ISDIR(inode
->i_mode
))
2674 if (S_ISLNK(inode
->i_mode
))
2677 inode
->i_op
= p
->iop
;
2679 inode
->i_fop
= p
->fop
;
2681 d_add(dentry
, inode
);
2690 static struct dentry
*proc_base_lookup(struct inode
*dir
, struct dentry
*dentry
)
2692 struct dentry
*error
;
2693 struct task_struct
*task
= get_proc_task(dir
);
2694 const struct pid_entry
*p
, *last
;
2696 error
= ERR_PTR(-ENOENT
);
2701 /* Lookup the directory entry */
2702 last
= &proc_base_stuff
[ARRAY_SIZE(proc_base_stuff
) - 1];
2703 for (p
= proc_base_stuff
; p
<= last
; p
++) {
2704 if (p
->len
!= dentry
->d_name
.len
)
2706 if (!memcmp(dentry
->d_name
.name
, p
->name
, p
->len
))
2712 error
= proc_base_instantiate(dir
, dentry
, task
, p
);
2715 put_task_struct(task
);
2720 static int proc_base_fill_cache(struct file
*filp
, void *dirent
,
2721 filldir_t filldir
, struct task_struct
*task
, const struct pid_entry
*p
)
2723 return proc_fill_cache(filp
, dirent
, filldir
, p
->name
, p
->len
,
2724 proc_base_instantiate
, task
, p
);
2727 #ifdef CONFIG_TASK_IO_ACCOUNTING
2728 static int do_io_accounting(struct task_struct
*task
, char *buffer
, int whole
)
2730 struct task_io_accounting acct
= task
->ioac
;
2731 unsigned long flags
;
2733 if (whole
&& lock_task_sighand(task
, &flags
)) {
2734 struct task_struct
*t
= task
;
2736 task_io_accounting_add(&acct
, &task
->signal
->ioac
);
2737 while_each_thread(task
, t
)
2738 task_io_accounting_add(&acct
, &t
->ioac
);
2740 unlock_task_sighand(task
, &flags
);
2742 return sprintf(buffer
,
2747 "read_bytes: %llu\n"
2748 "write_bytes: %llu\n"
2749 "cancelled_write_bytes: %llu\n",
2750 (unsigned long long)acct
.rchar
,
2751 (unsigned long long)acct
.wchar
,
2752 (unsigned long long)acct
.syscr
,
2753 (unsigned long long)acct
.syscw
,
2754 (unsigned long long)acct
.read_bytes
,
2755 (unsigned long long)acct
.write_bytes
,
2756 (unsigned long long)acct
.cancelled_write_bytes
);
2759 static int proc_tid_io_accounting(struct task_struct
*task
, char *buffer
)
2761 return do_io_accounting(task
, buffer
, 0);
2764 static int proc_tgid_io_accounting(struct task_struct
*task
, char *buffer
)
2766 return do_io_accounting(task
, buffer
, 1);
2768 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2770 static int proc_pid_personality(struct seq_file
*m
, struct pid_namespace
*ns
,
2771 struct pid
*pid
, struct task_struct
*task
)
2773 seq_printf(m
, "%08x\n", task
->personality
);
2780 static const struct file_operations proc_task_operations
;
2781 static const struct inode_operations proc_task_inode_operations
;
2783 static const struct pid_entry tgid_base_stuff
[] = {
2784 DIR("task", S_IRUGO
|S_IXUGO
, proc_task_inode_operations
, proc_task_operations
),
2785 DIR("fd", S_IRUSR
|S_IXUSR
, proc_fd_inode_operations
, proc_fd_operations
),
2786 DIR("fdinfo", S_IRUSR
|S_IXUSR
, proc_fdinfo_inode_operations
, proc_fdinfo_operations
),
2788 DIR("net", S_IRUGO
|S_IXUGO
, proc_net_inode_operations
, proc_net_operations
),
2790 REG("environ", S_IRUSR
, proc_environ_operations
),
2791 INF("auxv", S_IRUSR
, proc_pid_auxv
),
2792 ONE("status", S_IRUGO
, proc_pid_status
),
2793 ONE("personality", S_IRUSR
, proc_pid_personality
),
2794 INF("limits", S_IRUGO
, proc_pid_limits
),
2795 #ifdef CONFIG_SCHED_DEBUG
2796 REG("sched", S_IRUGO
|S_IWUSR
, proc_pid_sched_operations
),
2798 #ifdef CONFIG_SCHED_AUTOGROUP
2799 REG("autogroup", S_IRUGO
|S_IWUSR
, proc_pid_sched_autogroup_operations
),
2801 REG("comm", S_IRUGO
|S_IWUSR
, proc_pid_set_comm_operations
),
2802 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2803 INF("syscall", S_IRUSR
, proc_pid_syscall
),
2805 INF("cmdline", S_IRUGO
, proc_pid_cmdline
),
2806 ONE("stat", S_IRUGO
, proc_tgid_stat
),
2807 ONE("statm", S_IRUGO
, proc_pid_statm
),
2808 REG("maps", S_IRUGO
, proc_maps_operations
),
2810 REG("numa_maps", S_IRUGO
, proc_numa_maps_operations
),
2812 REG("mem", S_IRUSR
|S_IWUSR
, proc_mem_operations
),
2813 LNK("cwd", proc_cwd_link
),
2814 LNK("root", proc_root_link
),
2815 LNK("exe", proc_exe_link
),
2816 REG("mounts", S_IRUGO
, proc_mounts_operations
),
2817 REG("mountinfo", S_IRUGO
, proc_mountinfo_operations
),
2818 REG("mountstats", S_IRUSR
, proc_mountstats_operations
),
2819 #ifdef CONFIG_PROC_PAGE_MONITOR
2820 REG("clear_refs", S_IWUSR
, proc_clear_refs_operations
),
2821 REG("smaps", S_IRUGO
, proc_smaps_operations
),
2822 REG("pagemap", S_IRUGO
, proc_pagemap_operations
),
2824 #ifdef CONFIG_SECURITY
2825 DIR("attr", S_IRUGO
|S_IXUGO
, proc_attr_dir_inode_operations
, proc_attr_dir_operations
),
2827 #ifdef CONFIG_KALLSYMS
2828 INF("wchan", S_IRUGO
, proc_pid_wchan
),
2830 #ifdef CONFIG_STACKTRACE
2831 ONE("stack", S_IRUSR
, proc_pid_stack
),
2833 #ifdef CONFIG_SCHEDSTATS
2834 INF("schedstat", S_IRUGO
, proc_pid_schedstat
),
2836 #ifdef CONFIG_LATENCYTOP
2837 REG("latency", S_IRUGO
, proc_lstats_operations
),
2839 #ifdef CONFIG_PROC_PID_CPUSET
2840 REG("cpuset", S_IRUGO
, proc_cpuset_operations
),
2842 #ifdef CONFIG_CGROUPS
2843 REG("cgroup", S_IRUGO
, proc_cgroup_operations
),
2845 INF("oom_score", S_IRUGO
, proc_oom_score
),
2846 REG("oom_adj", S_IRUGO
|S_IWUSR
, proc_oom_adjust_operations
),
2847 REG("oom_score_adj", S_IRUGO
|S_IWUSR
, proc_oom_score_adj_operations
),
2848 #ifdef CONFIG_AUDITSYSCALL
2849 REG("loginuid", S_IWUSR
|S_IRUGO
, proc_loginuid_operations
),
2850 REG("sessionid", S_IRUGO
, proc_sessionid_operations
),
2852 #ifdef CONFIG_FAULT_INJECTION
2853 REG("make-it-fail", S_IRUGO
|S_IWUSR
, proc_fault_inject_operations
),
2855 #ifdef CONFIG_ELF_CORE
2856 REG("coredump_filter", S_IRUGO
|S_IWUSR
, proc_coredump_filter_operations
),
2858 #ifdef CONFIG_TASK_IO_ACCOUNTING
2859 INF("io", S_IRUGO
, proc_tgid_io_accounting
),
2863 static int proc_tgid_base_readdir(struct file
* filp
,
2864 void * dirent
, filldir_t filldir
)
2866 return proc_pident_readdir(filp
,dirent
,filldir
,
2867 tgid_base_stuff
,ARRAY_SIZE(tgid_base_stuff
));
2870 static const struct file_operations proc_tgid_base_operations
= {
2871 .read
= generic_read_dir
,
2872 .readdir
= proc_tgid_base_readdir
,
2873 .llseek
= default_llseek
,
2876 static struct dentry
*proc_tgid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
){
2877 return proc_pident_lookup(dir
, dentry
,
2878 tgid_base_stuff
, ARRAY_SIZE(tgid_base_stuff
));
2881 static const struct inode_operations proc_tgid_base_inode_operations
= {
2882 .lookup
= proc_tgid_base_lookup
,
2883 .getattr
= pid_getattr
,
2884 .setattr
= proc_setattr
,
2887 static void proc_flush_task_mnt(struct vfsmount
*mnt
, pid_t pid
, pid_t tgid
)
2889 struct dentry
*dentry
, *leader
, *dir
;
2890 char buf
[PROC_NUMBUF
];
2894 name
.len
= snprintf(buf
, sizeof(buf
), "%d", pid
);
2895 dentry
= d_hash_and_lookup(mnt
->mnt_root
, &name
);
2897 shrink_dcache_parent(dentry
);
2903 name
.len
= snprintf(buf
, sizeof(buf
), "%d", tgid
);
2904 leader
= d_hash_and_lookup(mnt
->mnt_root
, &name
);
2909 name
.len
= strlen(name
.name
);
2910 dir
= d_hash_and_lookup(leader
, &name
);
2912 goto out_put_leader
;
2915 name
.len
= snprintf(buf
, sizeof(buf
), "%d", pid
);
2916 dentry
= d_hash_and_lookup(dir
, &name
);
2918 shrink_dcache_parent(dentry
);
2931 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2932 * @task: task that should be flushed.
2934 * When flushing dentries from proc, one needs to flush them from global
2935 * proc (proc_mnt) and from all the namespaces' procs this task was seen
2936 * in. This call is supposed to do all of this job.
2938 * Looks in the dcache for
2940 * /proc/@tgid/task/@pid
2941 * if either directory is present flushes it and all of it'ts children
2944 * It is safe and reasonable to cache /proc entries for a task until
2945 * that task exits. After that they just clog up the dcache with
2946 * useless entries, possibly causing useful dcache entries to be
2947 * flushed instead. This routine is proved to flush those useless
2948 * dcache entries at process exit time.
2950 * NOTE: This routine is just an optimization so it does not guarantee
2951 * that no dcache entries will exist at process exit time it
2952 * just makes it very unlikely that any will persist.
2955 void proc_flush_task(struct task_struct
*task
)
2958 struct pid
*pid
, *tgid
;
2961 pid
= task_pid(task
);
2962 tgid
= task_tgid(task
);
2964 for (i
= 0; i
<= pid
->level
; i
++) {
2965 upid
= &pid
->numbers
[i
];
2966 proc_flush_task_mnt(upid
->ns
->proc_mnt
, upid
->nr
,
2967 tgid
->numbers
[i
].nr
);
2970 upid
= &pid
->numbers
[pid
->level
];
2972 pid_ns_release_proc(upid
->ns
);
2975 static struct dentry
*proc_pid_instantiate(struct inode
*dir
,
2976 struct dentry
* dentry
,
2977 struct task_struct
*task
, const void *ptr
)
2979 struct dentry
*error
= ERR_PTR(-ENOENT
);
2980 struct inode
*inode
;
2982 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
2986 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
2987 inode
->i_op
= &proc_tgid_base_inode_operations
;
2988 inode
->i_fop
= &proc_tgid_base_operations
;
2989 inode
->i_flags
|=S_IMMUTABLE
;
2991 inode
->i_nlink
= 2 + pid_entry_count_dirs(tgid_base_stuff
,
2992 ARRAY_SIZE(tgid_base_stuff
));
2994 d_set_d_op(dentry
, &pid_dentry_operations
);
2996 d_add(dentry
, inode
);
2997 /* Close the race of the process dying before we return the dentry */
2998 if (pid_revalidate(dentry
, NULL
))
3004 struct dentry
*proc_pid_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
)
3006 struct dentry
*result
;
3007 struct task_struct
*task
;
3009 struct pid_namespace
*ns
;
3011 result
= proc_base_lookup(dir
, dentry
);
3012 if (!IS_ERR(result
) || PTR_ERR(result
) != -ENOENT
)
3015 tgid
= name_to_int(dentry
);
3019 ns
= dentry
->d_sb
->s_fs_info
;
3021 task
= find_task_by_pid_ns(tgid
, ns
);
3023 get_task_struct(task
);
3028 result
= proc_pid_instantiate(dir
, dentry
, task
, NULL
);
3029 put_task_struct(task
);
3035 * Find the first task with tgid >= tgid
3040 struct task_struct
*task
;
3042 static struct tgid_iter
next_tgid(struct pid_namespace
*ns
, struct tgid_iter iter
)
3047 put_task_struct(iter
.task
);
3051 pid
= find_ge_pid(iter
.tgid
, ns
);
3053 iter
.tgid
= pid_nr_ns(pid
, ns
);
3054 iter
.task
= pid_task(pid
, PIDTYPE_PID
);
3055 /* What we to know is if the pid we have find is the
3056 * pid of a thread_group_leader. Testing for task
3057 * being a thread_group_leader is the obvious thing
3058 * todo but there is a window when it fails, due to
3059 * the pid transfer logic in de_thread.
3061 * So we perform the straight forward test of seeing
3062 * if the pid we have found is the pid of a thread
3063 * group leader, and don't worry if the task we have
3064 * found doesn't happen to be a thread group leader.
3065 * As we don't care in the case of readdir.
3067 if (!iter
.task
|| !has_group_leader_pid(iter
.task
)) {
3071 get_task_struct(iter
.task
);
3077 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
3079 static int proc_pid_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
3080 struct tgid_iter iter
)
3082 char name
[PROC_NUMBUF
];
3083 int len
= snprintf(name
, sizeof(name
), "%d", iter
.tgid
);
3084 return proc_fill_cache(filp
, dirent
, filldir
, name
, len
,
3085 proc_pid_instantiate
, iter
.task
, NULL
);
3088 /* for the /proc/ directory itself, after non-process stuff has been done */
3089 int proc_pid_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
3091 unsigned int nr
= filp
->f_pos
- FIRST_PROCESS_ENTRY
;
3092 struct task_struct
*reaper
= get_proc_task(filp
->f_path
.dentry
->d_inode
);
3093 struct tgid_iter iter
;
3094 struct pid_namespace
*ns
;
3099 for (; nr
< ARRAY_SIZE(proc_base_stuff
); filp
->f_pos
++, nr
++) {
3100 const struct pid_entry
*p
= &proc_base_stuff
[nr
];
3101 if (proc_base_fill_cache(filp
, dirent
, filldir
, reaper
, p
) < 0)
3105 ns
= filp
->f_dentry
->d_sb
->s_fs_info
;
3107 iter
.tgid
= filp
->f_pos
- TGID_OFFSET
;
3108 for (iter
= next_tgid(ns
, iter
);
3110 iter
.tgid
+= 1, iter
= next_tgid(ns
, iter
)) {
3111 filp
->f_pos
= iter
.tgid
+ TGID_OFFSET
;
3112 if (proc_pid_fill_cache(filp
, dirent
, filldir
, iter
) < 0) {
3113 put_task_struct(iter
.task
);
3117 filp
->f_pos
= PID_MAX_LIMIT
+ TGID_OFFSET
;
3119 put_task_struct(reaper
);
3127 static const struct pid_entry tid_base_stuff
[] = {
3128 DIR("fd", S_IRUSR
|S_IXUSR
, proc_fd_inode_operations
, proc_fd_operations
),
3129 DIR("fdinfo", S_IRUSR
|S_IXUSR
, proc_fdinfo_inode_operations
, proc_fdinfo_operations
),
3130 REG("environ", S_IRUSR
, proc_environ_operations
),
3131 INF("auxv", S_IRUSR
, proc_pid_auxv
),
3132 ONE("status", S_IRUGO
, proc_pid_status
),
3133 ONE("personality", S_IRUSR
, proc_pid_personality
),
3134 INF("limits", S_IRUGO
, proc_pid_limits
),
3135 #ifdef CONFIG_SCHED_DEBUG
3136 REG("sched", S_IRUGO
|S_IWUSR
, proc_pid_sched_operations
),
3138 REG("comm", S_IRUGO
|S_IWUSR
, proc_pid_set_comm_operations
),
3139 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3140 INF("syscall", S_IRUSR
, proc_pid_syscall
),
3142 INF("cmdline", S_IRUGO
, proc_pid_cmdline
),
3143 ONE("stat", S_IRUGO
, proc_tid_stat
),
3144 ONE("statm", S_IRUGO
, proc_pid_statm
),
3145 REG("maps", S_IRUGO
, proc_maps_operations
),
3147 REG("numa_maps", S_IRUGO
, proc_numa_maps_operations
),
3149 REG("mem", S_IRUSR
|S_IWUSR
, proc_mem_operations
),
3150 LNK("cwd", proc_cwd_link
),
3151 LNK("root", proc_root_link
),
3152 LNK("exe", proc_exe_link
),
3153 REG("mounts", S_IRUGO
, proc_mounts_operations
),
3154 REG("mountinfo", S_IRUGO
, proc_mountinfo_operations
),
3155 #ifdef CONFIG_PROC_PAGE_MONITOR
3156 REG("clear_refs", S_IWUSR
, proc_clear_refs_operations
),
3157 REG("smaps", S_IRUGO
, proc_smaps_operations
),
3158 REG("pagemap", S_IRUGO
, proc_pagemap_operations
),
3160 #ifdef CONFIG_SECURITY
3161 DIR("attr", S_IRUGO
|S_IXUGO
, proc_attr_dir_inode_operations
, proc_attr_dir_operations
),
3163 #ifdef CONFIG_KALLSYMS
3164 INF("wchan", S_IRUGO
, proc_pid_wchan
),
3166 #ifdef CONFIG_STACKTRACE
3167 ONE("stack", S_IRUSR
, proc_pid_stack
),
3169 #ifdef CONFIG_SCHEDSTATS
3170 INF("schedstat", S_IRUGO
, proc_pid_schedstat
),
3172 #ifdef CONFIG_LATENCYTOP
3173 REG("latency", S_IRUGO
, proc_lstats_operations
),
3175 #ifdef CONFIG_PROC_PID_CPUSET
3176 REG("cpuset", S_IRUGO
, proc_cpuset_operations
),
3178 #ifdef CONFIG_CGROUPS
3179 REG("cgroup", S_IRUGO
, proc_cgroup_operations
),
3181 INF("oom_score", S_IRUGO
, proc_oom_score
),
3182 REG("oom_adj", S_IRUGO
|S_IWUSR
, proc_oom_adjust_operations
),
3183 REG("oom_score_adj", S_IRUGO
|S_IWUSR
, proc_oom_score_adj_operations
),
3184 #ifdef CONFIG_AUDITSYSCALL
3185 REG("loginuid", S_IWUSR
|S_IRUGO
, proc_loginuid_operations
),
3186 REG("sessionid", S_IRUGO
, proc_sessionid_operations
),
3188 #ifdef CONFIG_FAULT_INJECTION
3189 REG("make-it-fail", S_IRUGO
|S_IWUSR
, proc_fault_inject_operations
),
3191 #ifdef CONFIG_TASK_IO_ACCOUNTING
3192 INF("io", S_IRUGO
, proc_tid_io_accounting
),
3196 static int proc_tid_base_readdir(struct file
* filp
,
3197 void * dirent
, filldir_t filldir
)
3199 return proc_pident_readdir(filp
,dirent
,filldir
,
3200 tid_base_stuff
,ARRAY_SIZE(tid_base_stuff
));
3203 static struct dentry
*proc_tid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
){
3204 return proc_pident_lookup(dir
, dentry
,
3205 tid_base_stuff
, ARRAY_SIZE(tid_base_stuff
));
3208 static const struct file_operations proc_tid_base_operations
= {
3209 .read
= generic_read_dir
,
3210 .readdir
= proc_tid_base_readdir
,
3211 .llseek
= default_llseek
,
3214 static const struct inode_operations proc_tid_base_inode_operations
= {
3215 .lookup
= proc_tid_base_lookup
,
3216 .getattr
= pid_getattr
,
3217 .setattr
= proc_setattr
,
3220 static struct dentry
*proc_task_instantiate(struct inode
*dir
,
3221 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
3223 struct dentry
*error
= ERR_PTR(-ENOENT
);
3224 struct inode
*inode
;
3225 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
3229 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
3230 inode
->i_op
= &proc_tid_base_inode_operations
;
3231 inode
->i_fop
= &proc_tid_base_operations
;
3232 inode
->i_flags
|=S_IMMUTABLE
;
3234 inode
->i_nlink
= 2 + pid_entry_count_dirs(tid_base_stuff
,
3235 ARRAY_SIZE(tid_base_stuff
));
3237 d_set_d_op(dentry
, &pid_dentry_operations
);
3239 d_add(dentry
, inode
);
3240 /* Close the race of the process dying before we return the dentry */
3241 if (pid_revalidate(dentry
, NULL
))
3247 static struct dentry
*proc_task_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
)
3249 struct dentry
*result
= ERR_PTR(-ENOENT
);
3250 struct task_struct
*task
;
3251 struct task_struct
*leader
= get_proc_task(dir
);
3253 struct pid_namespace
*ns
;
3258 tid
= name_to_int(dentry
);
3262 ns
= dentry
->d_sb
->s_fs_info
;
3264 task
= find_task_by_pid_ns(tid
, ns
);
3266 get_task_struct(task
);
3270 if (!same_thread_group(leader
, task
))
3273 result
= proc_task_instantiate(dir
, dentry
, task
, NULL
);
3275 put_task_struct(task
);
3277 put_task_struct(leader
);
3283 * Find the first tid of a thread group to return to user space.
3285 * Usually this is just the thread group leader, but if the users
3286 * buffer was too small or there was a seek into the middle of the
3287 * directory we have more work todo.
3289 * In the case of a short read we start with find_task_by_pid.
3291 * In the case of a seek we start with the leader and walk nr
3294 static struct task_struct
*first_tid(struct task_struct
*leader
,
3295 int tid
, int nr
, struct pid_namespace
*ns
)
3297 struct task_struct
*pos
;
3300 /* Attempt to start with the pid of a thread */
3301 if (tid
&& (nr
> 0)) {
3302 pos
= find_task_by_pid_ns(tid
, ns
);
3303 if (pos
&& (pos
->group_leader
== leader
))
3307 /* If nr exceeds the number of threads there is nothing todo */
3309 if (nr
&& nr
>= get_nr_threads(leader
))
3312 /* If we haven't found our starting place yet start
3313 * with the leader and walk nr threads forward.
3315 for (pos
= leader
; nr
> 0; --nr
) {
3316 pos
= next_thread(pos
);
3317 if (pos
== leader
) {
3323 get_task_struct(pos
);
3330 * Find the next thread in the thread list.
3331 * Return NULL if there is an error or no next thread.
3333 * The reference to the input task_struct is released.
3335 static struct task_struct
*next_tid(struct task_struct
*start
)
3337 struct task_struct
*pos
= NULL
;
3339 if (pid_alive(start
)) {
3340 pos
= next_thread(start
);
3341 if (thread_group_leader(pos
))
3344 get_task_struct(pos
);
3347 put_task_struct(start
);
3351 static int proc_task_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
3352 struct task_struct
*task
, int tid
)
3354 char name
[PROC_NUMBUF
];
3355 int len
= snprintf(name
, sizeof(name
), "%d", tid
);
3356 return proc_fill_cache(filp
, dirent
, filldir
, name
, len
,
3357 proc_task_instantiate
, task
, NULL
);
3360 /* for the /proc/TGID/task/ directories */
3361 static int proc_task_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
3363 struct dentry
*dentry
= filp
->f_path
.dentry
;
3364 struct inode
*inode
= dentry
->d_inode
;
3365 struct task_struct
*leader
= NULL
;
3366 struct task_struct
*task
;
3367 int retval
= -ENOENT
;
3370 struct pid_namespace
*ns
;
3372 task
= get_proc_task(inode
);
3376 if (pid_alive(task
)) {
3377 leader
= task
->group_leader
;
3378 get_task_struct(leader
);
3381 put_task_struct(task
);
3386 switch ((unsigned long)filp
->f_pos
) {
3389 if (filldir(dirent
, ".", 1, filp
->f_pos
, ino
, DT_DIR
) < 0)
3394 ino
= parent_ino(dentry
);
3395 if (filldir(dirent
, "..", 2, filp
->f_pos
, ino
, DT_DIR
) < 0)
3401 /* f_version caches the tgid value that the last readdir call couldn't
3402 * return. lseek aka telldir automagically resets f_version to 0.
3404 ns
= filp
->f_dentry
->d_sb
->s_fs_info
;
3405 tid
= (int)filp
->f_version
;
3406 filp
->f_version
= 0;
3407 for (task
= first_tid(leader
, tid
, filp
->f_pos
- 2, ns
);
3409 task
= next_tid(task
), filp
->f_pos
++) {
3410 tid
= task_pid_nr_ns(task
, ns
);
3411 if (proc_task_fill_cache(filp
, dirent
, filldir
, task
, tid
) < 0) {
3412 /* returning this tgid failed, save it as the first
3413 * pid for the next readir call */
3414 filp
->f_version
= (u64
)tid
;
3415 put_task_struct(task
);
3420 put_task_struct(leader
);
3425 static int proc_task_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
3427 struct inode
*inode
= dentry
->d_inode
;
3428 struct task_struct
*p
= get_proc_task(inode
);
3429 generic_fillattr(inode
, stat
);
3432 stat
->nlink
+= get_nr_threads(p
);
3439 static const struct inode_operations proc_task_inode_operations
= {
3440 .lookup
= proc_task_lookup
,
3441 .getattr
= proc_task_getattr
,
3442 .setattr
= proc_setattr
,
3445 static const struct file_operations proc_task_operations
= {
3446 .read
= generic_read_dir
,
3447 .readdir
= proc_task_readdir
,
3448 .llseek
= default_llseek
,