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 } )
136 /* ANDROID is for special files in /proc. */
137 #define ANDROID(NAME, MODE, OTYPE) \
138 NOD(NAME, (S_IFREG|(MODE)), \
139 &proc_##OTYPE##_inode_operations, \
140 &proc_##OTYPE##_operations, {})
143 * Count the number of hardlinks for the pid_entry table, excluding the .
146 static unsigned int pid_entry_count_dirs(const struct pid_entry
*entries
,
153 for (i
= 0; i
< n
; ++i
) {
154 if (S_ISDIR(entries
[i
].mode
))
161 static int get_task_root(struct task_struct
*task
, struct path
*root
)
163 int result
= -ENOENT
;
167 get_fs_root(task
->fs
, root
);
174 static int proc_cwd_link(struct inode
*inode
, struct path
*path
)
176 struct task_struct
*task
= get_proc_task(inode
);
177 int result
= -ENOENT
;
182 get_fs_pwd(task
->fs
, path
);
186 put_task_struct(task
);
191 static int proc_root_link(struct inode
*inode
, struct path
*path
)
193 struct task_struct
*task
= get_proc_task(inode
);
194 int result
= -ENOENT
;
197 result
= get_task_root(task
, path
);
198 put_task_struct(task
);
203 static struct mm_struct
*mm_access(struct task_struct
*task
, unsigned int mode
)
205 struct mm_struct
*mm
;
208 err
= mutex_lock_killable(&task
->signal
->cred_guard_mutex
);
212 mm
= get_task_mm(task
);
213 if (mm
&& mm
!= current
->mm
&&
214 !ptrace_may_access(task
, mode
) &&
215 !capable(CAP_SYS_RESOURCE
)) {
217 mm
= ERR_PTR(-EACCES
);
219 mutex_unlock(&task
->signal
->cred_guard_mutex
);
224 struct mm_struct
*mm_for_maps(struct task_struct
*task
)
226 return mm_access(task
, PTRACE_MODE_READ
);
229 static int proc_pid_cmdline(struct task_struct
*task
, char * buffer
)
233 struct mm_struct
*mm
= get_task_mm(task
);
237 goto out_mm
; /* Shh! No looking before we're done */
239 len
= mm
->arg_end
- mm
->arg_start
;
244 res
= access_process_vm(task
, mm
->arg_start
, buffer
, len
, 0);
246 // If the nul at the end of args has been overwritten, then
247 // assume application is using setproctitle(3).
248 if (res
> 0 && buffer
[res
-1] != '\0' && len
< PAGE_SIZE
) {
249 len
= strnlen(buffer
, res
);
253 len
= mm
->env_end
- mm
->env_start
;
254 if (len
> PAGE_SIZE
- res
)
255 len
= PAGE_SIZE
- res
;
256 res
+= access_process_vm(task
, mm
->env_start
, buffer
+res
, len
, 0);
257 res
= strnlen(buffer
, res
);
266 static int proc_pid_auxv(struct task_struct
*task
, char *buffer
)
268 struct mm_struct
*mm
= mm_for_maps(task
);
269 int res
= PTR_ERR(mm
);
270 if (mm
&& !IS_ERR(mm
)) {
271 unsigned int nwords
= 0;
274 } while (mm
->saved_auxv
[nwords
- 2] != 0); /* AT_NULL */
275 res
= nwords
* sizeof(mm
->saved_auxv
[0]);
278 memcpy(buffer
, mm
->saved_auxv
, res
);
285 #ifdef CONFIG_KALLSYMS
287 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
288 * Returns the resolved symbol. If that fails, simply return the address.
290 static int proc_pid_wchan(struct task_struct
*task
, char *buffer
)
293 char symname
[KSYM_NAME_LEN
];
295 wchan
= get_wchan(task
);
297 if (lookup_symbol_name(wchan
, symname
) < 0)
298 if (!ptrace_may_access(task
, PTRACE_MODE_READ
))
301 return sprintf(buffer
, "%lu", wchan
);
303 return sprintf(buffer
, "%s", symname
);
305 #endif /* CONFIG_KALLSYMS */
307 static int lock_trace(struct task_struct
*task
)
309 int err
= mutex_lock_killable(&task
->signal
->cred_guard_mutex
);
312 if (!ptrace_may_access(task
, PTRACE_MODE_ATTACH
)) {
313 mutex_unlock(&task
->signal
->cred_guard_mutex
);
319 static void unlock_trace(struct task_struct
*task
)
321 mutex_unlock(&task
->signal
->cred_guard_mutex
);
324 #ifdef CONFIG_STACKTRACE
326 #define MAX_STACK_TRACE_DEPTH 64
328 static int proc_pid_stack(struct seq_file
*m
, struct pid_namespace
*ns
,
329 struct pid
*pid
, struct task_struct
*task
)
331 struct stack_trace trace
;
332 unsigned long *entries
;
336 entries
= kmalloc(MAX_STACK_TRACE_DEPTH
* sizeof(*entries
), GFP_KERNEL
);
340 trace
.nr_entries
= 0;
341 trace
.max_entries
= MAX_STACK_TRACE_DEPTH
;
342 trace
.entries
= entries
;
345 err
= lock_trace(task
);
347 save_stack_trace_tsk(task
, &trace
);
349 for (i
= 0; i
< trace
.nr_entries
; i
++) {
350 seq_printf(m
, "[<%pK>] %pS\n",
351 (void *)entries
[i
], (void *)entries
[i
]);
361 #ifdef CONFIG_SCHEDSTATS
363 * Provides /proc/PID/schedstat
365 static int proc_pid_schedstat(struct task_struct
*task
, char *buffer
)
367 return sprintf(buffer
, "%llu %llu %lu\n",
368 (unsigned long long)task
->se
.sum_exec_runtime
,
369 (unsigned long long)task
->sched_info
.run_delay
,
370 task
->sched_info
.pcount
);
374 #ifdef CONFIG_LATENCYTOP
375 static int lstats_show_proc(struct seq_file
*m
, void *v
)
378 struct inode
*inode
= m
->private;
379 struct task_struct
*task
= get_proc_task(inode
);
383 seq_puts(m
, "Latency Top version : v0.1\n");
384 for (i
= 0; i
< 32; i
++) {
385 struct latency_record
*lr
= &task
->latency_record
[i
];
386 if (lr
->backtrace
[0]) {
388 seq_printf(m
, "%i %li %li",
389 lr
->count
, lr
->time
, lr
->max
);
390 for (q
= 0; q
< LT_BACKTRACEDEPTH
; q
++) {
391 unsigned long bt
= lr
->backtrace
[q
];
396 seq_printf(m
, " %ps", (void *)bt
);
402 put_task_struct(task
);
406 static int lstats_open(struct inode
*inode
, struct file
*file
)
408 return single_open(file
, lstats_show_proc
, inode
);
411 static ssize_t
lstats_write(struct file
*file
, const char __user
*buf
,
412 size_t count
, loff_t
*offs
)
414 struct task_struct
*task
= get_proc_task(file
->f_dentry
->d_inode
);
418 clear_all_latency_tracing(task
);
419 put_task_struct(task
);
424 static const struct file_operations proc_lstats_operations
= {
427 .write
= lstats_write
,
429 .release
= single_release
,
434 static int proc_oom_score(struct task_struct
*task
, char *buffer
)
436 unsigned long points
= 0;
438 read_lock(&tasklist_lock
);
440 points
= oom_badness(task
, NULL
, NULL
,
441 totalram_pages
+ total_swap_pages
);
442 read_unlock(&tasklist_lock
);
443 return sprintf(buffer
, "%lu\n", points
);
451 static const struct limit_names lnames
[RLIM_NLIMITS
] = {
452 [RLIMIT_CPU
] = {"Max cpu time", "seconds"},
453 [RLIMIT_FSIZE
] = {"Max file size", "bytes"},
454 [RLIMIT_DATA
] = {"Max data size", "bytes"},
455 [RLIMIT_STACK
] = {"Max stack size", "bytes"},
456 [RLIMIT_CORE
] = {"Max core file size", "bytes"},
457 [RLIMIT_RSS
] = {"Max resident set", "bytes"},
458 [RLIMIT_NPROC
] = {"Max processes", "processes"},
459 [RLIMIT_NOFILE
] = {"Max open files", "files"},
460 [RLIMIT_MEMLOCK
] = {"Max locked memory", "bytes"},
461 [RLIMIT_AS
] = {"Max address space", "bytes"},
462 [RLIMIT_LOCKS
] = {"Max file locks", "locks"},
463 [RLIMIT_SIGPENDING
] = {"Max pending signals", "signals"},
464 [RLIMIT_MSGQUEUE
] = {"Max msgqueue size", "bytes"},
465 [RLIMIT_NICE
] = {"Max nice priority", NULL
},
466 [RLIMIT_RTPRIO
] = {"Max realtime priority", NULL
},
467 [RLIMIT_RTTIME
] = {"Max realtime timeout", "us"},
470 /* Display limits for a process */
471 static int proc_pid_limits(struct task_struct
*task
, char *buffer
)
476 char *bufptr
= buffer
;
478 struct rlimit rlim
[RLIM_NLIMITS
];
480 if (!lock_task_sighand(task
, &flags
))
482 memcpy(rlim
, task
->signal
->rlim
, sizeof(struct rlimit
) * RLIM_NLIMITS
);
483 unlock_task_sighand(task
, &flags
);
486 * print the file header
488 count
+= sprintf(&bufptr
[count
], "%-25s %-20s %-20s %-10s\n",
489 "Limit", "Soft Limit", "Hard Limit", "Units");
491 for (i
= 0; i
< RLIM_NLIMITS
; i
++) {
492 if (rlim
[i
].rlim_cur
== RLIM_INFINITY
)
493 count
+= sprintf(&bufptr
[count
], "%-25s %-20s ",
494 lnames
[i
].name
, "unlimited");
496 count
+= sprintf(&bufptr
[count
], "%-25s %-20lu ",
497 lnames
[i
].name
, rlim
[i
].rlim_cur
);
499 if (rlim
[i
].rlim_max
== RLIM_INFINITY
)
500 count
+= sprintf(&bufptr
[count
], "%-20s ", "unlimited");
502 count
+= sprintf(&bufptr
[count
], "%-20lu ",
506 count
+= sprintf(&bufptr
[count
], "%-10s\n",
509 count
+= sprintf(&bufptr
[count
], "\n");
515 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
516 static int proc_pid_syscall(struct task_struct
*task
, char *buffer
)
519 unsigned long args
[6], sp
, pc
;
520 int res
= lock_trace(task
);
524 if (task_current_syscall(task
, &nr
, args
, 6, &sp
, &pc
))
525 res
= sprintf(buffer
, "running\n");
527 res
= sprintf(buffer
, "%ld 0x%lx 0x%lx\n", nr
, sp
, pc
);
529 res
= sprintf(buffer
,
530 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
532 args
[0], args
[1], args
[2], args
[3], args
[4], args
[5],
537 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
539 /************************************************************************/
540 /* Here the fs part begins */
541 /************************************************************************/
543 /* permission checks */
544 static int proc_fd_access_allowed(struct inode
*inode
)
546 struct task_struct
*task
;
548 /* Allow access to a task's file descriptors if it is us or we
549 * may use ptrace attach to the process and find out that
552 task
= get_proc_task(inode
);
554 allowed
= ptrace_may_access(task
, PTRACE_MODE_READ
);
555 put_task_struct(task
);
560 int proc_setattr(struct dentry
*dentry
, struct iattr
*attr
)
563 struct inode
*inode
= dentry
->d_inode
;
565 if (attr
->ia_valid
& ATTR_MODE
)
568 error
= inode_change_ok(inode
, attr
);
572 if ((attr
->ia_valid
& ATTR_SIZE
) &&
573 attr
->ia_size
!= i_size_read(inode
)) {
574 error
= vmtruncate(inode
, attr
->ia_size
);
579 setattr_copy(inode
, attr
);
580 mark_inode_dirty(inode
);
584 static const struct inode_operations proc_def_inode_operations
= {
585 .setattr
= proc_setattr
,
588 static int mounts_open_common(struct inode
*inode
, struct file
*file
,
589 const struct seq_operations
*op
)
591 struct task_struct
*task
= get_proc_task(inode
);
593 struct mnt_namespace
*ns
= NULL
;
595 struct proc_mounts
*p
;
600 nsp
= task_nsproxy(task
);
607 if (ns
&& get_task_root(task
, &root
) == 0)
609 put_task_struct(task
);
618 p
= kmalloc(sizeof(struct proc_mounts
), GFP_KERNEL
);
622 file
->private_data
= &p
->m
;
623 ret
= seq_open(file
, op
);
630 p
->event
= ns
->event
;
644 static int mounts_release(struct inode
*inode
, struct file
*file
)
646 struct proc_mounts
*p
= file
->private_data
;
649 return seq_release(inode
, file
);
652 static unsigned mounts_poll(struct file
*file
, poll_table
*wait
)
654 struct proc_mounts
*p
= file
->private_data
;
655 unsigned res
= POLLIN
| POLLRDNORM
;
657 poll_wait(file
, &p
->ns
->poll
, wait
);
658 if (mnt_had_events(p
))
659 res
|= POLLERR
| POLLPRI
;
664 static int mounts_open(struct inode
*inode
, struct file
*file
)
666 return mounts_open_common(inode
, file
, &mounts_op
);
669 static const struct file_operations proc_mounts_operations
= {
673 .release
= mounts_release
,
677 static int mountinfo_open(struct inode
*inode
, struct file
*file
)
679 return mounts_open_common(inode
, file
, &mountinfo_op
);
682 static const struct file_operations proc_mountinfo_operations
= {
683 .open
= mountinfo_open
,
686 .release
= mounts_release
,
690 static int mountstats_open(struct inode
*inode
, struct file
*file
)
692 return mounts_open_common(inode
, file
, &mountstats_op
);
695 static const struct file_operations proc_mountstats_operations
= {
696 .open
= mountstats_open
,
699 .release
= mounts_release
,
702 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
704 static ssize_t
proc_info_read(struct file
* file
, char __user
* buf
,
705 size_t count
, loff_t
*ppos
)
707 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
710 struct task_struct
*task
= get_proc_task(inode
);
716 if (count
> PROC_BLOCK_SIZE
)
717 count
= PROC_BLOCK_SIZE
;
720 if (!(page
= __get_free_page(GFP_TEMPORARY
)))
723 length
= PROC_I(inode
)->op
.proc_read(task
, (char*)page
);
726 length
= simple_read_from_buffer(buf
, count
, ppos
, (char *)page
, length
);
729 put_task_struct(task
);
734 static const struct file_operations proc_info_file_operations
= {
735 .read
= proc_info_read
,
736 .llseek
= generic_file_llseek
,
739 static int proc_single_show(struct seq_file
*m
, void *v
)
741 struct inode
*inode
= m
->private;
742 struct pid_namespace
*ns
;
744 struct task_struct
*task
;
747 ns
= inode
->i_sb
->s_fs_info
;
748 pid
= proc_pid(inode
);
749 task
= get_pid_task(pid
, PIDTYPE_PID
);
753 ret
= PROC_I(inode
)->op
.proc_show(m
, ns
, pid
, task
);
755 put_task_struct(task
);
759 static int proc_single_open(struct inode
*inode
, struct file
*filp
)
761 return single_open(filp
, proc_single_show
, inode
);
764 static const struct file_operations proc_single_file_operations
= {
765 .open
= proc_single_open
,
768 .release
= single_release
,
771 static int mem_open(struct inode
* inode
, struct file
* file
)
773 struct task_struct
*task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
774 struct mm_struct
*mm
;
779 mm
= mm_access(task
, PTRACE_MODE_ATTACH
);
780 put_task_struct(task
);
786 /* ensure this mm_struct can't be freed */
787 atomic_inc(&mm
->mm_count
);
788 /* but do not pin its memory */
792 /* OK to pass negative loff_t, we can catch out-of-range */
793 file
->f_mode
|= FMODE_UNSIGNED_OFFSET
;
794 file
->private_data
= mm
;
799 static ssize_t
mem_rw(struct file
*file
, char __user
*buf
,
800 size_t count
, loff_t
*ppos
, int write
)
802 struct mm_struct
*mm
= file
->private_data
;
803 unsigned long addr
= *ppos
;
810 page
= (char *)__get_free_page(GFP_TEMPORARY
);
815 if (!atomic_inc_not_zero(&mm
->mm_users
))
819 int this_len
= min_t(int, count
, PAGE_SIZE
);
821 if (write
&& copy_from_user(page
, buf
, this_len
)) {
826 this_len
= access_remote_vm(mm
, addr
, page
, this_len
, write
);
833 if (!write
&& copy_to_user(buf
, page
, this_len
)) {
847 free_page((unsigned long) page
);
851 static ssize_t
mem_read(struct file
*file
, char __user
*buf
,
852 size_t count
, loff_t
*ppos
)
854 return mem_rw(file
, buf
, count
, ppos
, 0);
857 #define mem_write NULL
860 /* This is a security hazard */
861 static ssize_t
mem_write(struct file
*file
, const char __user
*buf
,
862 size_t count
, loff_t
*ppos
)
864 return mem_rw(file
, (char __user
*)buf
, count
, ppos
, 1);
868 loff_t
mem_lseek(struct file
*file
, loff_t offset
, int orig
)
872 file
->f_pos
= offset
;
875 file
->f_pos
+= offset
;
880 force_successful_syscall_return();
884 static int mem_release(struct inode
*inode
, struct file
*file
)
886 struct mm_struct
*mm
= file
->private_data
;
892 static const struct file_operations proc_mem_operations
= {
897 .release
= mem_release
,
900 static ssize_t
environ_read(struct file
*file
, char __user
*buf
,
901 size_t count
, loff_t
*ppos
)
903 struct task_struct
*task
= get_proc_task(file
->f_dentry
->d_inode
);
905 unsigned long src
= *ppos
;
907 struct mm_struct
*mm
;
913 page
= (char *)__get_free_page(GFP_TEMPORARY
);
918 mm
= mm_for_maps(task
);
920 if (!mm
|| IS_ERR(mm
))
925 int this_len
, retval
, max_len
;
927 this_len
= mm
->env_end
- (mm
->env_start
+ src
);
932 max_len
= (count
> PAGE_SIZE
) ? PAGE_SIZE
: count
;
933 this_len
= (this_len
> max_len
) ? max_len
: this_len
;
935 retval
= access_process_vm(task
, (mm
->env_start
+ src
),
943 if (copy_to_user(buf
, page
, retval
)) {
957 free_page((unsigned long) page
);
959 put_task_struct(task
);
964 static const struct file_operations proc_environ_operations
= {
965 .read
= environ_read
,
966 .llseek
= generic_file_llseek
,
969 static ssize_t
oom_adjust_read(struct file
*file
, char __user
*buf
,
970 size_t count
, loff_t
*ppos
)
972 struct task_struct
*task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
973 char buffer
[PROC_NUMBUF
];
975 int oom_adjust
= OOM_DISABLE
;
981 if (lock_task_sighand(task
, &flags
)) {
982 oom_adjust
= task
->signal
->oom_adj
;
983 unlock_task_sighand(task
, &flags
);
986 put_task_struct(task
);
988 len
= snprintf(buffer
, sizeof(buffer
), "%i\n", oom_adjust
);
990 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
993 static ssize_t
oom_adjust_write(struct file
*file
, const char __user
*buf
,
994 size_t count
, loff_t
*ppos
)
996 struct task_struct
*task
;
997 char buffer
[PROC_NUMBUF
];
1002 memset(buffer
, 0, sizeof(buffer
));
1003 if (count
> sizeof(buffer
) - 1)
1004 count
= sizeof(buffer
) - 1;
1005 if (copy_from_user(buffer
, buf
, count
)) {
1010 err
= kstrtoint(strstrip(buffer
), 0, &oom_adjust
);
1013 if ((oom_adjust
< OOM_ADJUST_MIN
|| oom_adjust
> OOM_ADJUST_MAX
) &&
1014 oom_adjust
!= OOM_DISABLE
) {
1019 task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
1031 if (!lock_task_sighand(task
, &flags
)) {
1036 if (oom_adjust
< task
->signal
->oom_adj
&& !capable(CAP_SYS_RESOURCE
)) {
1041 if (oom_adjust
!= task
->signal
->oom_adj
) {
1042 if (oom_adjust
== OOM_DISABLE
)
1043 atomic_inc(&task
->mm
->oom_disable_count
);
1044 if (task
->signal
->oom_adj
== OOM_DISABLE
)
1045 atomic_dec(&task
->mm
->oom_disable_count
);
1049 * Warn that /proc/pid/oom_adj is deprecated, see
1050 * Documentation/feature-removal-schedule.txt.
1052 printk_once(KERN_WARNING
"%s (%d): /proc/%d/oom_adj is deprecated, "
1053 "please use /proc/%d/oom_score_adj instead.\n",
1054 current
->comm
, task_pid_nr(current
),
1055 task_pid_nr(task
), task_pid_nr(task
));
1056 task
->signal
->oom_adj
= oom_adjust
;
1058 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1059 * value is always attainable.
1061 if (task
->signal
->oom_adj
== OOM_ADJUST_MAX
)
1062 task
->signal
->oom_score_adj
= OOM_SCORE_ADJ_MAX
;
1064 task
->signal
->oom_score_adj
= (oom_adjust
* OOM_SCORE_ADJ_MAX
) /
1067 unlock_task_sighand(task
, &flags
);
1070 put_task_struct(task
);
1072 return err
< 0 ? err
: count
;
1075 static int oom_adjust_permission(struct inode
*inode
, int mask
,
1079 struct task_struct
*p
;
1081 if (flags
& IPERM_FLAG_RCU
)
1084 p
= get_proc_task(inode
);
1091 * System Server (uid == 1000) is granted access to oom_adj of all
1092 * android applications (uid > 10000) as and services (uid >= 1000)
1094 if (p
&& (current_fsuid() == 1000) && (uid
>= 1000)) {
1095 if (inode
->i_mode
>> 6 & mask
) {
1100 /* Fall back to default. */
1101 return generic_permission(inode
, mask
, flags
, NULL
);
1104 static const struct inode_operations proc_oom_adjust_inode_operations
= {
1105 .permission
= oom_adjust_permission
,
1108 static const struct file_operations proc_oom_adjust_operations
= {
1109 .read
= oom_adjust_read
,
1110 .write
= oom_adjust_write
,
1111 .llseek
= generic_file_llseek
,
1114 static ssize_t
oom_score_adj_read(struct file
*file
, char __user
*buf
,
1115 size_t count
, loff_t
*ppos
)
1117 struct task_struct
*task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
1118 char buffer
[PROC_NUMBUF
];
1119 int oom_score_adj
= OOM_SCORE_ADJ_MIN
;
1120 unsigned long flags
;
1125 if (lock_task_sighand(task
, &flags
)) {
1126 oom_score_adj
= task
->signal
->oom_score_adj
;
1127 unlock_task_sighand(task
, &flags
);
1129 put_task_struct(task
);
1130 len
= snprintf(buffer
, sizeof(buffer
), "%d\n", oom_score_adj
);
1131 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
1134 static ssize_t
oom_score_adj_write(struct file
*file
, const char __user
*buf
,
1135 size_t count
, loff_t
*ppos
)
1137 struct task_struct
*task
;
1138 char buffer
[PROC_NUMBUF
];
1139 unsigned long flags
;
1143 memset(buffer
, 0, sizeof(buffer
));
1144 if (count
> sizeof(buffer
) - 1)
1145 count
= sizeof(buffer
) - 1;
1146 if (copy_from_user(buffer
, buf
, count
)) {
1151 err
= kstrtoint(strstrip(buffer
), 0, &oom_score_adj
);
1154 if (oom_score_adj
< OOM_SCORE_ADJ_MIN
||
1155 oom_score_adj
> OOM_SCORE_ADJ_MAX
) {
1160 task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
1172 if (!lock_task_sighand(task
, &flags
)) {
1177 if (oom_score_adj
< task
->signal
->oom_score_adj_min
&&
1178 !capable(CAP_SYS_RESOURCE
)) {
1183 if (oom_score_adj
!= task
->signal
->oom_score_adj
) {
1184 if (oom_score_adj
== OOM_SCORE_ADJ_MIN
)
1185 atomic_inc(&task
->mm
->oom_disable_count
);
1186 if (task
->signal
->oom_score_adj
== OOM_SCORE_ADJ_MIN
)
1187 atomic_dec(&task
->mm
->oom_disable_count
);
1189 task
->signal
->oom_score_adj
= oom_score_adj
;
1190 if (has_capability_noaudit(current
, CAP_SYS_RESOURCE
))
1191 task
->signal
->oom_score_adj_min
= oom_score_adj
;
1193 * Scale /proc/pid/oom_adj appropriately ensuring that OOM_DISABLE is
1194 * always attainable.
1196 if (task
->signal
->oom_score_adj
== OOM_SCORE_ADJ_MIN
)
1197 task
->signal
->oom_adj
= OOM_DISABLE
;
1199 task
->signal
->oom_adj
= (oom_score_adj
* OOM_ADJUST_MAX
) /
1202 unlock_task_sighand(task
, &flags
);
1205 put_task_struct(task
);
1207 return err
< 0 ? err
: count
;
1210 static const struct file_operations proc_oom_score_adj_operations
= {
1211 .read
= oom_score_adj_read
,
1212 .write
= oom_score_adj_write
,
1213 .llseek
= default_llseek
,
1216 #ifdef CONFIG_AUDITSYSCALL
1217 #define TMPBUFLEN 21
1218 static ssize_t
proc_loginuid_read(struct file
* file
, char __user
* buf
,
1219 size_t count
, loff_t
*ppos
)
1221 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
1222 struct task_struct
*task
= get_proc_task(inode
);
1224 char tmpbuf
[TMPBUFLEN
];
1228 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
1229 audit_get_loginuid(task
));
1230 put_task_struct(task
);
1231 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1234 static ssize_t
proc_loginuid_write(struct file
* file
, const char __user
* buf
,
1235 size_t count
, loff_t
*ppos
)
1237 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
1242 if (!capable(CAP_AUDIT_CONTROL
))
1246 if (current
!= pid_task(proc_pid(inode
), PIDTYPE_PID
)) {
1252 if (count
>= PAGE_SIZE
)
1253 count
= PAGE_SIZE
- 1;
1256 /* No partial writes. */
1259 page
= (char*)__get_free_page(GFP_TEMPORARY
);
1263 if (copy_from_user(page
, buf
, count
))
1267 loginuid
= simple_strtoul(page
, &tmp
, 10);
1273 length
= audit_set_loginuid(current
, loginuid
);
1274 if (likely(length
== 0))
1278 free_page((unsigned long) page
);
1282 static const struct file_operations proc_loginuid_operations
= {
1283 .read
= proc_loginuid_read
,
1284 .write
= proc_loginuid_write
,
1285 .llseek
= generic_file_llseek
,
1288 static ssize_t
proc_sessionid_read(struct file
* file
, char __user
* buf
,
1289 size_t count
, loff_t
*ppos
)
1291 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
1292 struct task_struct
*task
= get_proc_task(inode
);
1294 char tmpbuf
[TMPBUFLEN
];
1298 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
1299 audit_get_sessionid(task
));
1300 put_task_struct(task
);
1301 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1304 static const struct file_operations proc_sessionid_operations
= {
1305 .read
= proc_sessionid_read
,
1306 .llseek
= generic_file_llseek
,
1310 #ifdef CONFIG_FAULT_INJECTION
1311 static ssize_t
proc_fault_inject_read(struct file
* file
, char __user
* buf
,
1312 size_t count
, loff_t
*ppos
)
1314 struct task_struct
*task
= get_proc_task(file
->f_dentry
->d_inode
);
1315 char buffer
[PROC_NUMBUF
];
1321 make_it_fail
= task
->make_it_fail
;
1322 put_task_struct(task
);
1324 len
= snprintf(buffer
, sizeof(buffer
), "%i\n", make_it_fail
);
1326 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
1329 static ssize_t
proc_fault_inject_write(struct file
* file
,
1330 const char __user
* buf
, size_t count
, loff_t
*ppos
)
1332 struct task_struct
*task
;
1333 char buffer
[PROC_NUMBUF
], *end
;
1336 if (!capable(CAP_SYS_RESOURCE
))
1338 memset(buffer
, 0, sizeof(buffer
));
1339 if (count
> sizeof(buffer
) - 1)
1340 count
= sizeof(buffer
) - 1;
1341 if (copy_from_user(buffer
, buf
, count
))
1343 make_it_fail
= simple_strtol(strstrip(buffer
), &end
, 0);
1346 task
= get_proc_task(file
->f_dentry
->d_inode
);
1349 task
->make_it_fail
= make_it_fail
;
1350 put_task_struct(task
);
1355 static const struct file_operations proc_fault_inject_operations
= {
1356 .read
= proc_fault_inject_read
,
1357 .write
= proc_fault_inject_write
,
1358 .llseek
= generic_file_llseek
,
1363 #ifdef CONFIG_SCHED_DEBUG
1365 * Print out various scheduling related per-task fields:
1367 static int sched_show(struct seq_file
*m
, void *v
)
1369 struct inode
*inode
= m
->private;
1370 struct task_struct
*p
;
1372 p
= get_proc_task(inode
);
1375 proc_sched_show_task(p
, m
);
1383 sched_write(struct file
*file
, const char __user
*buf
,
1384 size_t count
, loff_t
*offset
)
1386 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1387 struct task_struct
*p
;
1389 p
= get_proc_task(inode
);
1392 proc_sched_set_task(p
);
1399 static int sched_open(struct inode
*inode
, struct file
*filp
)
1401 return single_open(filp
, sched_show
, inode
);
1404 static const struct file_operations proc_pid_sched_operations
= {
1407 .write
= sched_write
,
1408 .llseek
= seq_lseek
,
1409 .release
= single_release
,
1414 #ifdef CONFIG_SCHED_AUTOGROUP
1416 * Print out autogroup related information:
1418 static int sched_autogroup_show(struct seq_file
*m
, void *v
)
1420 struct inode
*inode
= m
->private;
1421 struct task_struct
*p
;
1423 p
= get_proc_task(inode
);
1426 proc_sched_autogroup_show_task(p
, m
);
1434 sched_autogroup_write(struct file
*file
, const char __user
*buf
,
1435 size_t count
, loff_t
*offset
)
1437 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1438 struct task_struct
*p
;
1439 char buffer
[PROC_NUMBUF
];
1443 memset(buffer
, 0, sizeof(buffer
));
1444 if (count
> sizeof(buffer
) - 1)
1445 count
= sizeof(buffer
) - 1;
1446 if (copy_from_user(buffer
, buf
, count
))
1449 err
= kstrtoint(strstrip(buffer
), 0, &nice
);
1453 p
= get_proc_task(inode
);
1458 err
= proc_sched_autogroup_set_nice(p
, &err
);
1467 static int sched_autogroup_open(struct inode
*inode
, struct file
*filp
)
1471 ret
= single_open(filp
, sched_autogroup_show
, NULL
);
1473 struct seq_file
*m
= filp
->private_data
;
1480 static const struct file_operations proc_pid_sched_autogroup_operations
= {
1481 .open
= sched_autogroup_open
,
1483 .write
= sched_autogroup_write
,
1484 .llseek
= seq_lseek
,
1485 .release
= single_release
,
1488 #endif /* CONFIG_SCHED_AUTOGROUP */
1490 static ssize_t
comm_write(struct file
*file
, const char __user
*buf
,
1491 size_t count
, loff_t
*offset
)
1493 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1494 struct task_struct
*p
;
1495 char buffer
[TASK_COMM_LEN
];
1497 memset(buffer
, 0, sizeof(buffer
));
1498 if (count
> sizeof(buffer
) - 1)
1499 count
= sizeof(buffer
) - 1;
1500 if (copy_from_user(buffer
, buf
, count
))
1503 p
= get_proc_task(inode
);
1507 if (same_thread_group(current
, p
))
1508 set_task_comm(p
, buffer
);
1517 static int comm_show(struct seq_file
*m
, void *v
)
1519 struct inode
*inode
= m
->private;
1520 struct task_struct
*p
;
1522 p
= get_proc_task(inode
);
1527 seq_printf(m
, "%s\n", p
->comm
);
1535 static int comm_open(struct inode
*inode
, struct file
*filp
)
1537 return single_open(filp
, comm_show
, inode
);
1540 static const struct file_operations proc_pid_set_comm_operations
= {
1543 .write
= comm_write
,
1544 .llseek
= seq_lseek
,
1545 .release
= single_release
,
1548 static int proc_exe_link(struct inode
*inode
, struct path
*exe_path
)
1550 struct task_struct
*task
;
1551 struct mm_struct
*mm
;
1552 struct file
*exe_file
;
1554 task
= get_proc_task(inode
);
1557 mm
= get_task_mm(task
);
1558 put_task_struct(task
);
1561 exe_file
= get_mm_exe_file(mm
);
1564 *exe_path
= exe_file
->f_path
;
1565 path_get(&exe_file
->f_path
);
1572 static void *proc_pid_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
1574 struct inode
*inode
= dentry
->d_inode
;
1575 int error
= -EACCES
;
1577 /* We don't need a base pointer in the /proc filesystem */
1578 path_put(&nd
->path
);
1580 /* Are we allowed to snoop on the tasks file descriptors? */
1581 if (!proc_fd_access_allowed(inode
))
1584 error
= PROC_I(inode
)->op
.proc_get_link(inode
, &nd
->path
);
1586 return ERR_PTR(error
);
1589 static int do_proc_readlink(struct path
*path
, char __user
*buffer
, int buflen
)
1591 char *tmp
= (char*)__get_free_page(GFP_TEMPORARY
);
1598 pathname
= d_path(path
, tmp
, PAGE_SIZE
);
1599 len
= PTR_ERR(pathname
);
1600 if (IS_ERR(pathname
))
1602 len
= tmp
+ PAGE_SIZE
- 1 - pathname
;
1606 if (copy_to_user(buffer
, pathname
, len
))
1609 free_page((unsigned long)tmp
);
1613 static int proc_pid_readlink(struct dentry
* dentry
, char __user
* buffer
, int buflen
)
1615 int error
= -EACCES
;
1616 struct inode
*inode
= dentry
->d_inode
;
1619 /* Are we allowed to snoop on the tasks file descriptors? */
1620 if (!proc_fd_access_allowed(inode
))
1623 error
= PROC_I(inode
)->op
.proc_get_link(inode
, &path
);
1627 error
= do_proc_readlink(&path
, buffer
, buflen
);
1633 static const struct inode_operations proc_pid_link_inode_operations
= {
1634 .readlink
= proc_pid_readlink
,
1635 .follow_link
= proc_pid_follow_link
,
1636 .setattr
= proc_setattr
,
1640 /* building an inode */
1642 static int task_dumpable(struct task_struct
*task
)
1645 struct mm_struct
*mm
;
1650 dumpable
= get_dumpable(mm
);
1657 struct inode
*proc_pid_make_inode(struct super_block
* sb
, struct task_struct
*task
)
1659 struct inode
* inode
;
1660 struct proc_inode
*ei
;
1661 const struct cred
*cred
;
1663 /* We need a new inode */
1665 inode
= new_inode(sb
);
1671 inode
->i_ino
= get_next_ino();
1672 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1673 inode
->i_op
= &proc_def_inode_operations
;
1676 * grab the reference to task.
1678 ei
->pid
= get_task_pid(task
, PIDTYPE_PID
);
1682 if (task_dumpable(task
)) {
1684 cred
= __task_cred(task
);
1685 inode
->i_uid
= cred
->euid
;
1686 inode
->i_gid
= cred
->egid
;
1689 security_task_to_inode(task
, inode
);
1699 int pid_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
1701 struct inode
*inode
= dentry
->d_inode
;
1702 struct task_struct
*task
;
1703 const struct cred
*cred
;
1705 generic_fillattr(inode
, stat
);
1710 task
= pid_task(proc_pid(inode
), PIDTYPE_PID
);
1712 if ((inode
->i_mode
== (S_IFDIR
|S_IRUGO
|S_IXUGO
)) ||
1713 task_dumpable(task
)) {
1714 cred
= __task_cred(task
);
1715 stat
->uid
= cred
->euid
;
1716 stat
->gid
= cred
->egid
;
1726 * Exceptional case: normally we are not allowed to unhash a busy
1727 * directory. In this case, however, we can do it - no aliasing problems
1728 * due to the way we treat inodes.
1730 * Rewrite the inode's ownerships here because the owning task may have
1731 * performed a setuid(), etc.
1733 * Before the /proc/pid/status file was created the only way to read
1734 * the effective uid of a /process was to stat /proc/pid. Reading
1735 * /proc/pid/status is slow enough that procps and other packages
1736 * kept stating /proc/pid. To keep the rules in /proc simple I have
1737 * made this apply to all per process world readable and executable
1740 int pid_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1742 struct inode
*inode
;
1743 struct task_struct
*task
;
1744 const struct cred
*cred
;
1746 if (nd
&& nd
->flags
& LOOKUP_RCU
)
1749 inode
= dentry
->d_inode
;
1750 task
= get_proc_task(inode
);
1753 if ((inode
->i_mode
== (S_IFDIR
|S_IRUGO
|S_IXUGO
)) ||
1754 task_dumpable(task
)) {
1756 cred
= __task_cred(task
);
1757 inode
->i_uid
= cred
->euid
;
1758 inode
->i_gid
= cred
->egid
;
1764 inode
->i_mode
&= ~(S_ISUID
| S_ISGID
);
1765 security_task_to_inode(task
, inode
);
1766 put_task_struct(task
);
1773 static int pid_delete_dentry(const struct dentry
* dentry
)
1775 /* Is the task we represent dead?
1776 * If so, then don't put the dentry on the lru list,
1777 * kill it immediately.
1779 return !proc_pid(dentry
->d_inode
)->tasks
[PIDTYPE_PID
].first
;
1782 const struct dentry_operations pid_dentry_operations
=
1784 .d_revalidate
= pid_revalidate
,
1785 .d_delete
= pid_delete_dentry
,
1791 * Fill a directory entry.
1793 * If possible create the dcache entry and derive our inode number and
1794 * file type from dcache entry.
1796 * Since all of the proc inode numbers are dynamically generated, the inode
1797 * numbers do not exist until the inode is cache. This means creating the
1798 * the dcache entry in readdir is necessary to keep the inode numbers
1799 * reported by readdir in sync with the inode numbers reported
1802 int proc_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
1803 const char *name
, int len
,
1804 instantiate_t instantiate
, struct task_struct
*task
, const void *ptr
)
1806 struct dentry
*child
, *dir
= filp
->f_path
.dentry
;
1807 struct inode
*inode
;
1810 unsigned type
= DT_UNKNOWN
;
1814 qname
.hash
= full_name_hash(name
, len
);
1816 child
= d_lookup(dir
, &qname
);
1819 new = d_alloc(dir
, &qname
);
1821 child
= instantiate(dir
->d_inode
, new, task
, ptr
);
1828 if (!child
|| IS_ERR(child
) || !child
->d_inode
)
1829 goto end_instantiate
;
1830 inode
= child
->d_inode
;
1833 type
= inode
->i_mode
>> 12;
1838 ino
= find_inode_number(dir
, &qname
);
1841 return filldir(dirent
, name
, len
, filp
->f_pos
, ino
, type
);
1844 static unsigned name_to_int(struct dentry
*dentry
)
1846 const char *name
= dentry
->d_name
.name
;
1847 int len
= dentry
->d_name
.len
;
1850 if (len
> 1 && *name
== '0')
1853 unsigned c
= *name
++ - '0';
1856 if (n
>= (~0U-9)/10)
1866 #define PROC_FDINFO_MAX 64
1868 static int proc_fd_info(struct inode
*inode
, struct path
*path
, char *info
)
1870 struct task_struct
*task
= get_proc_task(inode
);
1871 struct files_struct
*files
= NULL
;
1873 int fd
= proc_fd(inode
);
1876 files
= get_files_struct(task
);
1877 put_task_struct(task
);
1881 * We are not taking a ref to the file structure, so we must
1884 spin_lock(&files
->file_lock
);
1885 file
= fcheck_files(files
, fd
);
1887 unsigned int f_flags
;
1888 struct fdtable
*fdt
;
1890 fdt
= files_fdtable(files
);
1891 f_flags
= file
->f_flags
& ~O_CLOEXEC
;
1892 if (FD_ISSET(fd
, fdt
->close_on_exec
))
1893 f_flags
|= O_CLOEXEC
;
1896 *path
= file
->f_path
;
1897 path_get(&file
->f_path
);
1900 snprintf(info
, PROC_FDINFO_MAX
,
1903 (long long) file
->f_pos
,
1905 spin_unlock(&files
->file_lock
);
1906 put_files_struct(files
);
1909 spin_unlock(&files
->file_lock
);
1910 put_files_struct(files
);
1915 static int proc_fd_link(struct inode
*inode
, struct path
*path
)
1917 return proc_fd_info(inode
, path
, NULL
);
1920 static int tid_fd_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1922 struct inode
*inode
;
1923 struct task_struct
*task
;
1925 struct files_struct
*files
;
1926 const struct cred
*cred
;
1928 if (nd
&& nd
->flags
& LOOKUP_RCU
)
1931 inode
= dentry
->d_inode
;
1932 task
= get_proc_task(inode
);
1933 fd
= proc_fd(inode
);
1936 files
= get_files_struct(task
);
1939 if (fcheck_files(files
, fd
)) {
1941 put_files_struct(files
);
1942 if (task_dumpable(task
)) {
1944 cred
= __task_cred(task
);
1945 inode
->i_uid
= cred
->euid
;
1946 inode
->i_gid
= cred
->egid
;
1952 inode
->i_mode
&= ~(S_ISUID
| S_ISGID
);
1953 security_task_to_inode(task
, inode
);
1954 put_task_struct(task
);
1958 put_files_struct(files
);
1960 put_task_struct(task
);
1966 static const struct dentry_operations tid_fd_dentry_operations
=
1968 .d_revalidate
= tid_fd_revalidate
,
1969 .d_delete
= pid_delete_dentry
,
1972 static struct dentry
*proc_fd_instantiate(struct inode
*dir
,
1973 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
1975 unsigned fd
= *(const unsigned *)ptr
;
1977 struct files_struct
*files
;
1978 struct inode
*inode
;
1979 struct proc_inode
*ei
;
1980 struct dentry
*error
= ERR_PTR(-ENOENT
);
1982 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
1987 files
= get_files_struct(task
);
1990 inode
->i_mode
= S_IFLNK
;
1993 * We are not taking a ref to the file structure, so we must
1996 spin_lock(&files
->file_lock
);
1997 file
= fcheck_files(files
, fd
);
2000 if (file
->f_mode
& FMODE_READ
)
2001 inode
->i_mode
|= S_IRUSR
| S_IXUSR
;
2002 if (file
->f_mode
& FMODE_WRITE
)
2003 inode
->i_mode
|= S_IWUSR
| S_IXUSR
;
2004 spin_unlock(&files
->file_lock
);
2005 put_files_struct(files
);
2007 inode
->i_op
= &proc_pid_link_inode_operations
;
2009 ei
->op
.proc_get_link
= proc_fd_link
;
2010 d_set_d_op(dentry
, &tid_fd_dentry_operations
);
2011 d_add(dentry
, inode
);
2012 /* Close the race of the process dying before we return the dentry */
2013 if (tid_fd_revalidate(dentry
, NULL
))
2019 spin_unlock(&files
->file_lock
);
2020 put_files_struct(files
);
2026 static struct dentry
*proc_lookupfd_common(struct inode
*dir
,
2027 struct dentry
*dentry
,
2028 instantiate_t instantiate
)
2030 struct task_struct
*task
= get_proc_task(dir
);
2031 unsigned fd
= name_to_int(dentry
);
2032 struct dentry
*result
= ERR_PTR(-ENOENT
);
2039 result
= instantiate(dir
, dentry
, task
, &fd
);
2041 put_task_struct(task
);
2046 static int proc_readfd_common(struct file
* filp
, void * dirent
,
2047 filldir_t filldir
, instantiate_t instantiate
)
2049 struct dentry
*dentry
= filp
->f_path
.dentry
;
2050 struct inode
*inode
= dentry
->d_inode
;
2051 struct task_struct
*p
= get_proc_task(inode
);
2052 unsigned int fd
, ino
;
2054 struct files_struct
* files
;
2064 if (filldir(dirent
, ".", 1, 0, inode
->i_ino
, DT_DIR
) < 0)
2068 ino
= parent_ino(dentry
);
2069 if (filldir(dirent
, "..", 2, 1, ino
, DT_DIR
) < 0)
2073 files
= get_files_struct(p
);
2077 for (fd
= filp
->f_pos
-2;
2078 fd
< files_fdtable(files
)->max_fds
;
2079 fd
++, filp
->f_pos
++) {
2080 char name
[PROC_NUMBUF
];
2083 if (!fcheck_files(files
, fd
))
2087 len
= snprintf(name
, sizeof(name
), "%d", fd
);
2088 if (proc_fill_cache(filp
, dirent
, filldir
,
2089 name
, len
, instantiate
,
2097 put_files_struct(files
);
2105 static struct dentry
*proc_lookupfd(struct inode
*dir
, struct dentry
*dentry
,
2106 struct nameidata
*nd
)
2108 return proc_lookupfd_common(dir
, dentry
, proc_fd_instantiate
);
2111 static int proc_readfd(struct file
*filp
, void *dirent
, filldir_t filldir
)
2113 return proc_readfd_common(filp
, dirent
, filldir
, proc_fd_instantiate
);
2116 static ssize_t
proc_fdinfo_read(struct file
*file
, char __user
*buf
,
2117 size_t len
, loff_t
*ppos
)
2119 char tmp
[PROC_FDINFO_MAX
];
2120 int err
= proc_fd_info(file
->f_path
.dentry
->d_inode
, NULL
, tmp
);
2122 err
= simple_read_from_buffer(buf
, len
, ppos
, tmp
, strlen(tmp
));
2126 static const struct file_operations proc_fdinfo_file_operations
= {
2127 .open
= nonseekable_open
,
2128 .read
= proc_fdinfo_read
,
2129 .llseek
= no_llseek
,
2132 static const struct file_operations proc_fd_operations
= {
2133 .read
= generic_read_dir
,
2134 .readdir
= proc_readfd
,
2135 .llseek
= default_llseek
,
2139 * /proc/pid/fd needs a special permission handler so that a process can still
2140 * access /proc/self/fd after it has executed a setuid().
2142 static int proc_fd_permission(struct inode
*inode
, int mask
, unsigned int flags
)
2144 int rv
= generic_permission(inode
, mask
, flags
, NULL
);
2147 if (task_pid(current
) == proc_pid(inode
))
2153 * proc directories can do almost nothing..
2155 static const struct inode_operations proc_fd_inode_operations
= {
2156 .lookup
= proc_lookupfd
,
2157 .permission
= proc_fd_permission
,
2158 .setattr
= proc_setattr
,
2161 static struct dentry
*proc_fdinfo_instantiate(struct inode
*dir
,
2162 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
2164 unsigned fd
= *(unsigned *)ptr
;
2165 struct inode
*inode
;
2166 struct proc_inode
*ei
;
2167 struct dentry
*error
= ERR_PTR(-ENOENT
);
2169 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
2174 inode
->i_mode
= S_IFREG
| S_IRUSR
;
2175 inode
->i_fop
= &proc_fdinfo_file_operations
;
2176 d_set_d_op(dentry
, &tid_fd_dentry_operations
);
2177 d_add(dentry
, inode
);
2178 /* Close the race of the process dying before we return the dentry */
2179 if (tid_fd_revalidate(dentry
, NULL
))
2186 static struct dentry
*proc_lookupfdinfo(struct inode
*dir
,
2187 struct dentry
*dentry
,
2188 struct nameidata
*nd
)
2190 return proc_lookupfd_common(dir
, dentry
, proc_fdinfo_instantiate
);
2193 static int proc_readfdinfo(struct file
*filp
, void *dirent
, filldir_t filldir
)
2195 return proc_readfd_common(filp
, dirent
, filldir
,
2196 proc_fdinfo_instantiate
);
2199 static const struct file_operations proc_fdinfo_operations
= {
2200 .read
= generic_read_dir
,
2201 .readdir
= proc_readfdinfo
,
2202 .llseek
= default_llseek
,
2206 * proc directories can do almost nothing..
2208 static const struct inode_operations proc_fdinfo_inode_operations
= {
2209 .lookup
= proc_lookupfdinfo
,
2210 .setattr
= proc_setattr
,
2214 static struct dentry
*proc_pident_instantiate(struct inode
*dir
,
2215 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
2217 const struct pid_entry
*p
= ptr
;
2218 struct inode
*inode
;
2219 struct proc_inode
*ei
;
2220 struct dentry
*error
= ERR_PTR(-ENOENT
);
2222 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
2227 inode
->i_mode
= p
->mode
;
2228 if (S_ISDIR(inode
->i_mode
))
2229 inode
->i_nlink
= 2; /* Use getattr to fix if necessary */
2231 inode
->i_op
= p
->iop
;
2233 inode
->i_fop
= p
->fop
;
2235 d_set_d_op(dentry
, &pid_dentry_operations
);
2236 d_add(dentry
, inode
);
2237 /* Close the race of the process dying before we return the dentry */
2238 if (pid_revalidate(dentry
, NULL
))
2244 static struct dentry
*proc_pident_lookup(struct inode
*dir
,
2245 struct dentry
*dentry
,
2246 const struct pid_entry
*ents
,
2249 struct dentry
*error
;
2250 struct task_struct
*task
= get_proc_task(dir
);
2251 const struct pid_entry
*p
, *last
;
2253 error
= ERR_PTR(-ENOENT
);
2259 * Yes, it does not scale. And it should not. Don't add
2260 * new entries into /proc/<tgid>/ without very good reasons.
2262 last
= &ents
[nents
- 1];
2263 for (p
= ents
; p
<= last
; p
++) {
2264 if (p
->len
!= dentry
->d_name
.len
)
2266 if (!memcmp(dentry
->d_name
.name
, p
->name
, p
->len
))
2272 error
= proc_pident_instantiate(dir
, dentry
, task
, p
);
2274 put_task_struct(task
);
2279 static int proc_pident_fill_cache(struct file
*filp
, void *dirent
,
2280 filldir_t filldir
, struct task_struct
*task
, const struct pid_entry
*p
)
2282 return proc_fill_cache(filp
, dirent
, filldir
, p
->name
, p
->len
,
2283 proc_pident_instantiate
, task
, p
);
2286 static int proc_pident_readdir(struct file
*filp
,
2287 void *dirent
, filldir_t filldir
,
2288 const struct pid_entry
*ents
, unsigned int nents
)
2291 struct dentry
*dentry
= filp
->f_path
.dentry
;
2292 struct inode
*inode
= dentry
->d_inode
;
2293 struct task_struct
*task
= get_proc_task(inode
);
2294 const struct pid_entry
*p
, *last
;
2307 if (filldir(dirent
, ".", 1, i
, ino
, DT_DIR
) < 0)
2313 ino
= parent_ino(dentry
);
2314 if (filldir(dirent
, "..", 2, i
, ino
, DT_DIR
) < 0)
2326 last
= &ents
[nents
- 1];
2328 if (proc_pident_fill_cache(filp
, dirent
, filldir
, task
, p
) < 0)
2337 put_task_struct(task
);
2342 #ifdef CONFIG_SECURITY
2343 static ssize_t
proc_pid_attr_read(struct file
* file
, char __user
* buf
,
2344 size_t count
, loff_t
*ppos
)
2346 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
2349 struct task_struct
*task
= get_proc_task(inode
);
2354 length
= security_getprocattr(task
,
2355 (char*)file
->f_path
.dentry
->d_name
.name
,
2357 put_task_struct(task
);
2359 length
= simple_read_from_buffer(buf
, count
, ppos
, p
, length
);
2364 static ssize_t
proc_pid_attr_write(struct file
* file
, const char __user
* buf
,
2365 size_t count
, loff_t
*ppos
)
2367 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
2370 struct task_struct
*task
= get_proc_task(inode
);
2375 if (count
> PAGE_SIZE
)
2378 /* No partial writes. */
2384 page
= (char*)__get_free_page(GFP_TEMPORARY
);
2389 if (copy_from_user(page
, buf
, count
))
2392 /* Guard against adverse ptrace interaction */
2393 length
= mutex_lock_interruptible(&task
->signal
->cred_guard_mutex
);
2397 length
= security_setprocattr(task
,
2398 (char*)file
->f_path
.dentry
->d_name
.name
,
2399 (void*)page
, count
);
2400 mutex_unlock(&task
->signal
->cred_guard_mutex
);
2402 free_page((unsigned long) page
);
2404 put_task_struct(task
);
2409 static const struct file_operations proc_pid_attr_operations
= {
2410 .read
= proc_pid_attr_read
,
2411 .write
= proc_pid_attr_write
,
2412 .llseek
= generic_file_llseek
,
2415 static const struct pid_entry attr_dir_stuff
[] = {
2416 REG("current", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2417 REG("prev", S_IRUGO
, proc_pid_attr_operations
),
2418 REG("exec", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2419 REG("fscreate", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2420 REG("keycreate", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2421 REG("sockcreate", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2424 static int proc_attr_dir_readdir(struct file
* filp
,
2425 void * dirent
, filldir_t filldir
)
2427 return proc_pident_readdir(filp
,dirent
,filldir
,
2428 attr_dir_stuff
,ARRAY_SIZE(attr_dir_stuff
));
2431 static const struct file_operations proc_attr_dir_operations
= {
2432 .read
= generic_read_dir
,
2433 .readdir
= proc_attr_dir_readdir
,
2434 .llseek
= default_llseek
,
2437 static struct dentry
*proc_attr_dir_lookup(struct inode
*dir
,
2438 struct dentry
*dentry
, struct nameidata
*nd
)
2440 return proc_pident_lookup(dir
, dentry
,
2441 attr_dir_stuff
, ARRAY_SIZE(attr_dir_stuff
));
2444 static const struct inode_operations proc_attr_dir_inode_operations
= {
2445 .lookup
= proc_attr_dir_lookup
,
2446 .getattr
= pid_getattr
,
2447 .setattr
= proc_setattr
,
2452 #ifdef CONFIG_ELF_CORE
2453 static ssize_t
proc_coredump_filter_read(struct file
*file
, char __user
*buf
,
2454 size_t count
, loff_t
*ppos
)
2456 struct task_struct
*task
= get_proc_task(file
->f_dentry
->d_inode
);
2457 struct mm_struct
*mm
;
2458 char buffer
[PROC_NUMBUF
];
2466 mm
= get_task_mm(task
);
2468 len
= snprintf(buffer
, sizeof(buffer
), "%08lx\n",
2469 ((mm
->flags
& MMF_DUMP_FILTER_MASK
) >>
2470 MMF_DUMP_FILTER_SHIFT
));
2472 ret
= simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
2475 put_task_struct(task
);
2480 static ssize_t
proc_coredump_filter_write(struct file
*file
,
2481 const char __user
*buf
,
2485 struct task_struct
*task
;
2486 struct mm_struct
*mm
;
2487 char buffer
[PROC_NUMBUF
], *end
;
2494 memset(buffer
, 0, sizeof(buffer
));
2495 if (count
> sizeof(buffer
) - 1)
2496 count
= sizeof(buffer
) - 1;
2497 if (copy_from_user(buffer
, buf
, count
))
2501 val
= (unsigned int)simple_strtoul(buffer
, &end
, 0);
2504 if (end
- buffer
== 0)
2508 task
= get_proc_task(file
->f_dentry
->d_inode
);
2513 mm
= get_task_mm(task
);
2517 for (i
= 0, mask
= 1; i
< MMF_DUMP_FILTER_BITS
; i
++, mask
<<= 1) {
2519 set_bit(i
+ MMF_DUMP_FILTER_SHIFT
, &mm
->flags
);
2521 clear_bit(i
+ MMF_DUMP_FILTER_SHIFT
, &mm
->flags
);
2526 put_task_struct(task
);
2531 static const struct file_operations proc_coredump_filter_operations
= {
2532 .read
= proc_coredump_filter_read
,
2533 .write
= proc_coredump_filter_write
,
2534 .llseek
= generic_file_llseek
,
2541 static int proc_self_readlink(struct dentry
*dentry
, char __user
*buffer
,
2544 struct pid_namespace
*ns
= dentry
->d_sb
->s_fs_info
;
2545 pid_t tgid
= task_tgid_nr_ns(current
, ns
);
2546 char tmp
[PROC_NUMBUF
];
2549 sprintf(tmp
, "%d", tgid
);
2550 return vfs_readlink(dentry
,buffer
,buflen
,tmp
);
2553 static void *proc_self_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
2555 struct pid_namespace
*ns
= dentry
->d_sb
->s_fs_info
;
2556 pid_t tgid
= task_tgid_nr_ns(current
, ns
);
2557 char *name
= ERR_PTR(-ENOENT
);
2561 name
= ERR_PTR(-ENOMEM
);
2563 sprintf(name
, "%d", tgid
);
2565 nd_set_link(nd
, name
);
2569 static void proc_self_put_link(struct dentry
*dentry
, struct nameidata
*nd
,
2572 char *s
= nd_get_link(nd
);
2577 static const struct inode_operations proc_self_inode_operations
= {
2578 .readlink
= proc_self_readlink
,
2579 .follow_link
= proc_self_follow_link
,
2580 .put_link
= proc_self_put_link
,
2586 * These are the directory entries in the root directory of /proc
2587 * that properly belong to the /proc filesystem, as they describe
2588 * describe something that is process related.
2590 static const struct pid_entry proc_base_stuff
[] = {
2591 NOD("self", S_IFLNK
|S_IRWXUGO
,
2592 &proc_self_inode_operations
, NULL
, {}),
2595 static struct dentry
*proc_base_instantiate(struct inode
*dir
,
2596 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
2598 const struct pid_entry
*p
= ptr
;
2599 struct inode
*inode
;
2600 struct proc_inode
*ei
;
2601 struct dentry
*error
;
2603 /* Allocate the inode */
2604 error
= ERR_PTR(-ENOMEM
);
2605 inode
= new_inode(dir
->i_sb
);
2609 /* Initialize the inode */
2611 inode
->i_ino
= get_next_ino();
2612 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
2615 * grab the reference to the task.
2617 ei
->pid
= get_task_pid(task
, PIDTYPE_PID
);
2621 inode
->i_mode
= p
->mode
;
2622 if (S_ISDIR(inode
->i_mode
))
2624 if (S_ISLNK(inode
->i_mode
))
2627 inode
->i_op
= p
->iop
;
2629 inode
->i_fop
= p
->fop
;
2631 d_add(dentry
, inode
);
2640 static struct dentry
*proc_base_lookup(struct inode
*dir
, struct dentry
*dentry
)
2642 struct dentry
*error
;
2643 struct task_struct
*task
= get_proc_task(dir
);
2644 const struct pid_entry
*p
, *last
;
2646 error
= ERR_PTR(-ENOENT
);
2651 /* Lookup the directory entry */
2652 last
= &proc_base_stuff
[ARRAY_SIZE(proc_base_stuff
) - 1];
2653 for (p
= proc_base_stuff
; p
<= last
; p
++) {
2654 if (p
->len
!= dentry
->d_name
.len
)
2656 if (!memcmp(dentry
->d_name
.name
, p
->name
, p
->len
))
2662 error
= proc_base_instantiate(dir
, dentry
, task
, p
);
2665 put_task_struct(task
);
2670 static int proc_base_fill_cache(struct file
*filp
, void *dirent
,
2671 filldir_t filldir
, struct task_struct
*task
, const struct pid_entry
*p
)
2673 return proc_fill_cache(filp
, dirent
, filldir
, p
->name
, p
->len
,
2674 proc_base_instantiate
, task
, p
);
2677 #ifdef CONFIG_TASK_IO_ACCOUNTING
2678 static int do_io_accounting(struct task_struct
*task
, char *buffer
, int whole
)
2680 struct task_io_accounting acct
= task
->ioac
;
2681 unsigned long flags
;
2684 result
= mutex_lock_killable(&task
->signal
->cred_guard_mutex
);
2688 if (!ptrace_may_access(task
, PTRACE_MODE_READ
)) {
2693 if (whole
&& lock_task_sighand(task
, &flags
)) {
2694 struct task_struct
*t
= task
;
2696 task_io_accounting_add(&acct
, &task
->signal
->ioac
);
2697 while_each_thread(task
, t
)
2698 task_io_accounting_add(&acct
, &t
->ioac
);
2700 unlock_task_sighand(task
, &flags
);
2702 result
= sprintf(buffer
,
2707 "read_bytes: %llu\n"
2708 "write_bytes: %llu\n"
2709 "cancelled_write_bytes: %llu\n",
2710 (unsigned long long)acct
.rchar
,
2711 (unsigned long long)acct
.wchar
,
2712 (unsigned long long)acct
.syscr
,
2713 (unsigned long long)acct
.syscw
,
2714 (unsigned long long)acct
.read_bytes
,
2715 (unsigned long long)acct
.write_bytes
,
2716 (unsigned long long)acct
.cancelled_write_bytes
);
2718 mutex_unlock(&task
->signal
->cred_guard_mutex
);
2722 static int proc_tid_io_accounting(struct task_struct
*task
, char *buffer
)
2724 return do_io_accounting(task
, buffer
, 0);
2727 static int proc_tgid_io_accounting(struct task_struct
*task
, char *buffer
)
2729 return do_io_accounting(task
, buffer
, 1);
2731 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2733 static int proc_pid_personality(struct seq_file
*m
, struct pid_namespace
*ns
,
2734 struct pid
*pid
, struct task_struct
*task
)
2736 int err
= lock_trace(task
);
2738 seq_printf(m
, "%08x\n", task
->personality
);
2747 static const struct file_operations proc_task_operations
;
2748 static const struct inode_operations proc_task_inode_operations
;
2750 static const struct pid_entry tgid_base_stuff
[] = {
2751 DIR("task", S_IRUGO
|S_IXUGO
, proc_task_inode_operations
, proc_task_operations
),
2752 DIR("fd", S_IRUSR
|S_IXUSR
, proc_fd_inode_operations
, proc_fd_operations
),
2753 DIR("fdinfo", S_IRUSR
|S_IXUSR
, proc_fdinfo_inode_operations
, proc_fdinfo_operations
),
2754 DIR("ns", S_IRUSR
|S_IXUGO
, proc_ns_dir_inode_operations
, proc_ns_dir_operations
),
2756 DIR("net", S_IRUGO
|S_IXUGO
, proc_net_inode_operations
, proc_net_operations
),
2758 REG("environ", S_IRUSR
, proc_environ_operations
),
2759 INF("auxv", S_IRUSR
, proc_pid_auxv
),
2760 ONE("status", S_IRUGO
, proc_pid_status
),
2761 ONE("personality", S_IRUGO
, proc_pid_personality
),
2762 INF("limits", S_IRUGO
, proc_pid_limits
),
2763 #ifdef CONFIG_SCHED_DEBUG
2764 REG("sched", S_IRUGO
|S_IWUSR
, proc_pid_sched_operations
),
2766 #ifdef CONFIG_SCHED_AUTOGROUP
2767 REG("autogroup", S_IRUGO
|S_IWUSR
, proc_pid_sched_autogroup_operations
),
2769 REG("comm", S_IRUGO
|S_IWUSR
, proc_pid_set_comm_operations
),
2770 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2771 INF("syscall", S_IRUGO
, proc_pid_syscall
),
2773 INF("cmdline", S_IRUGO
, proc_pid_cmdline
),
2774 ONE("stat", S_IRUGO
, proc_tgid_stat
),
2775 ONE("statm", S_IRUGO
, proc_pid_statm
),
2776 REG("maps", S_IRUGO
, proc_maps_operations
),
2778 REG("numa_maps", S_IRUGO
, proc_numa_maps_operations
),
2780 REG("mem", S_IRUSR
|S_IWUSR
, proc_mem_operations
),
2781 LNK("cwd", proc_cwd_link
),
2782 LNK("root", proc_root_link
),
2783 LNK("exe", proc_exe_link
),
2784 REG("mounts", S_IRUGO
, proc_mounts_operations
),
2785 REG("mountinfo", S_IRUGO
, proc_mountinfo_operations
),
2786 REG("mountstats", S_IRUSR
, proc_mountstats_operations
),
2787 #ifdef CONFIG_PROC_PAGE_MONITOR
2788 REG("clear_refs", S_IWUSR
, proc_clear_refs_operations
),
2789 REG("smaps", S_IRUGO
, proc_smaps_operations
),
2790 REG("pagemap", S_IRUGO
, proc_pagemap_operations
),
2792 #ifdef CONFIG_SECURITY
2793 DIR("attr", S_IRUGO
|S_IXUGO
, proc_attr_dir_inode_operations
, proc_attr_dir_operations
),
2795 #ifdef CONFIG_KALLSYMS
2796 INF("wchan", S_IRUGO
, proc_pid_wchan
),
2798 #ifdef CONFIG_STACKTRACE
2799 ONE("stack", S_IRUGO
, proc_pid_stack
),
2801 #ifdef CONFIG_SCHEDSTATS
2802 INF("schedstat", S_IRUGO
, proc_pid_schedstat
),
2804 #ifdef CONFIG_LATENCYTOP
2805 REG("latency", S_IRUGO
, proc_lstats_operations
),
2807 #ifdef CONFIG_PROC_PID_CPUSET
2808 REG("cpuset", S_IRUGO
, proc_cpuset_operations
),
2810 #ifdef CONFIG_CGROUPS
2811 REG("cgroup", S_IRUGO
, proc_cgroup_operations
),
2813 INF("oom_score", S_IRUGO
, proc_oom_score
),
2814 ANDROID("oom_adj",S_IRUGO
|S_IWUSR
, oom_adjust
),
2815 REG("oom_score_adj", S_IRUGO
|S_IWUSR
, proc_oom_score_adj_operations
),
2816 #ifdef CONFIG_AUDITSYSCALL
2817 REG("loginuid", S_IWUSR
|S_IRUGO
, proc_loginuid_operations
),
2818 REG("sessionid", S_IRUGO
, proc_sessionid_operations
),
2820 #ifdef CONFIG_FAULT_INJECTION
2821 REG("make-it-fail", S_IRUGO
|S_IWUSR
, proc_fault_inject_operations
),
2823 #ifdef CONFIG_ELF_CORE
2824 REG("coredump_filter", S_IRUGO
|S_IWUSR
, proc_coredump_filter_operations
),
2826 #ifdef CONFIG_TASK_IO_ACCOUNTING
2827 INF("io", S_IRUSR
, proc_tgid_io_accounting
),
2829 #ifdef CONFIG_HARDWALL
2830 INF("hardwall", S_IRUGO
, proc_pid_hardwall
),
2834 static int proc_tgid_base_readdir(struct file
* filp
,
2835 void * dirent
, filldir_t filldir
)
2837 return proc_pident_readdir(filp
,dirent
,filldir
,
2838 tgid_base_stuff
,ARRAY_SIZE(tgid_base_stuff
));
2841 static const struct file_operations proc_tgid_base_operations
= {
2842 .read
= generic_read_dir
,
2843 .readdir
= proc_tgid_base_readdir
,
2844 .llseek
= default_llseek
,
2847 static struct dentry
*proc_tgid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
){
2848 return proc_pident_lookup(dir
, dentry
,
2849 tgid_base_stuff
, ARRAY_SIZE(tgid_base_stuff
));
2852 static const struct inode_operations proc_tgid_base_inode_operations
= {
2853 .lookup
= proc_tgid_base_lookup
,
2854 .getattr
= pid_getattr
,
2855 .setattr
= proc_setattr
,
2858 static void proc_flush_task_mnt(struct vfsmount
*mnt
, pid_t pid
, pid_t tgid
)
2860 struct dentry
*dentry
, *leader
, *dir
;
2861 char buf
[PROC_NUMBUF
];
2865 name
.len
= snprintf(buf
, sizeof(buf
), "%d", pid
);
2866 dentry
= d_hash_and_lookup(mnt
->mnt_root
, &name
);
2868 shrink_dcache_parent(dentry
);
2874 name
.len
= snprintf(buf
, sizeof(buf
), "%d", tgid
);
2875 leader
= d_hash_and_lookup(mnt
->mnt_root
, &name
);
2880 name
.len
= strlen(name
.name
);
2881 dir
= d_hash_and_lookup(leader
, &name
);
2883 goto out_put_leader
;
2886 name
.len
= snprintf(buf
, sizeof(buf
), "%d", pid
);
2887 dentry
= d_hash_and_lookup(dir
, &name
);
2889 shrink_dcache_parent(dentry
);
2902 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2903 * @task: task that should be flushed.
2905 * When flushing dentries from proc, one needs to flush them from global
2906 * proc (proc_mnt) and from all the namespaces' procs this task was seen
2907 * in. This call is supposed to do all of this job.
2909 * Looks in the dcache for
2911 * /proc/@tgid/task/@pid
2912 * if either directory is present flushes it and all of it'ts children
2915 * It is safe and reasonable to cache /proc entries for a task until
2916 * that task exits. After that they just clog up the dcache with
2917 * useless entries, possibly causing useful dcache entries to be
2918 * flushed instead. This routine is proved to flush those useless
2919 * dcache entries at process exit time.
2921 * NOTE: This routine is just an optimization so it does not guarantee
2922 * that no dcache entries will exist at process exit time it
2923 * just makes it very unlikely that any will persist.
2926 void proc_flush_task(struct task_struct
*task
)
2929 struct pid
*pid
, *tgid
;
2932 pid
= task_pid(task
);
2933 tgid
= task_tgid(task
);
2935 for (i
= 0; i
<= pid
->level
; i
++) {
2936 upid
= &pid
->numbers
[i
];
2937 proc_flush_task_mnt(upid
->ns
->proc_mnt
, upid
->nr
,
2938 tgid
->numbers
[i
].nr
);
2941 upid
= &pid
->numbers
[pid
->level
];
2943 pid_ns_release_proc(upid
->ns
);
2946 static struct dentry
*proc_pid_instantiate(struct inode
*dir
,
2947 struct dentry
* dentry
,
2948 struct task_struct
*task
, const void *ptr
)
2950 struct dentry
*error
= ERR_PTR(-ENOENT
);
2951 struct inode
*inode
;
2953 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
2957 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
2958 inode
->i_op
= &proc_tgid_base_inode_operations
;
2959 inode
->i_fop
= &proc_tgid_base_operations
;
2960 inode
->i_flags
|=S_IMMUTABLE
;
2962 inode
->i_nlink
= 2 + pid_entry_count_dirs(tgid_base_stuff
,
2963 ARRAY_SIZE(tgid_base_stuff
));
2965 d_set_d_op(dentry
, &pid_dentry_operations
);
2967 d_add(dentry
, inode
);
2968 /* Close the race of the process dying before we return the dentry */
2969 if (pid_revalidate(dentry
, NULL
))
2975 struct dentry
*proc_pid_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
)
2977 struct dentry
*result
;
2978 struct task_struct
*task
;
2980 struct pid_namespace
*ns
;
2982 result
= proc_base_lookup(dir
, dentry
);
2983 if (!IS_ERR(result
) || PTR_ERR(result
) != -ENOENT
)
2986 tgid
= name_to_int(dentry
);
2990 ns
= dentry
->d_sb
->s_fs_info
;
2992 task
= find_task_by_pid_ns(tgid
, ns
);
2994 get_task_struct(task
);
2999 result
= proc_pid_instantiate(dir
, dentry
, task
, NULL
);
3000 put_task_struct(task
);
3006 * Find the first task with tgid >= tgid
3011 struct task_struct
*task
;
3013 static struct tgid_iter
next_tgid(struct pid_namespace
*ns
, struct tgid_iter iter
)
3018 put_task_struct(iter
.task
);
3022 pid
= find_ge_pid(iter
.tgid
, ns
);
3024 iter
.tgid
= pid_nr_ns(pid
, ns
);
3025 iter
.task
= pid_task(pid
, PIDTYPE_PID
);
3026 /* What we to know is if the pid we have find is the
3027 * pid of a thread_group_leader. Testing for task
3028 * being a thread_group_leader is the obvious thing
3029 * todo but there is a window when it fails, due to
3030 * the pid transfer logic in de_thread.
3032 * So we perform the straight forward test of seeing
3033 * if the pid we have found is the pid of a thread
3034 * group leader, and don't worry if the task we have
3035 * found doesn't happen to be a thread group leader.
3036 * As we don't care in the case of readdir.
3038 if (!iter
.task
|| !has_group_leader_pid(iter
.task
)) {
3042 get_task_struct(iter
.task
);
3048 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
3050 static int proc_pid_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
3051 struct tgid_iter iter
)
3053 char name
[PROC_NUMBUF
];
3054 int len
= snprintf(name
, sizeof(name
), "%d", iter
.tgid
);
3055 return proc_fill_cache(filp
, dirent
, filldir
, name
, len
,
3056 proc_pid_instantiate
, iter
.task
, NULL
);
3059 /* for the /proc/ directory itself, after non-process stuff has been done */
3060 int proc_pid_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
3063 struct task_struct
*reaper
;
3064 struct tgid_iter iter
;
3065 struct pid_namespace
*ns
;
3067 if (filp
->f_pos
>= PID_MAX_LIMIT
+ TGID_OFFSET
)
3069 nr
= filp
->f_pos
- FIRST_PROCESS_ENTRY
;
3071 reaper
= get_proc_task(filp
->f_path
.dentry
->d_inode
);
3075 for (; nr
< ARRAY_SIZE(proc_base_stuff
); filp
->f_pos
++, nr
++) {
3076 const struct pid_entry
*p
= &proc_base_stuff
[nr
];
3077 if (proc_base_fill_cache(filp
, dirent
, filldir
, reaper
, p
) < 0)
3081 ns
= filp
->f_dentry
->d_sb
->s_fs_info
;
3083 iter
.tgid
= filp
->f_pos
- TGID_OFFSET
;
3084 for (iter
= next_tgid(ns
, iter
);
3086 iter
.tgid
+= 1, iter
= next_tgid(ns
, iter
)) {
3087 filp
->f_pos
= iter
.tgid
+ TGID_OFFSET
;
3088 if (proc_pid_fill_cache(filp
, dirent
, filldir
, iter
) < 0) {
3089 put_task_struct(iter
.task
);
3093 filp
->f_pos
= PID_MAX_LIMIT
+ TGID_OFFSET
;
3095 put_task_struct(reaper
);
3103 static const struct pid_entry tid_base_stuff
[] = {
3104 DIR("fd", S_IRUSR
|S_IXUSR
, proc_fd_inode_operations
, proc_fd_operations
),
3105 DIR("fdinfo", S_IRUSR
|S_IXUSR
, proc_fdinfo_inode_operations
, proc_fdinfo_operations
),
3106 DIR("ns", S_IRUSR
|S_IXUGO
, proc_ns_dir_inode_operations
, proc_ns_dir_operations
),
3107 REG("environ", S_IRUSR
, proc_environ_operations
),
3108 INF("auxv", S_IRUSR
, proc_pid_auxv
),
3109 ONE("status", S_IRUGO
, proc_pid_status
),
3110 ONE("personality", S_IRUGO
, proc_pid_personality
),
3111 INF("limits", S_IRUGO
, proc_pid_limits
),
3112 #ifdef CONFIG_SCHED_DEBUG
3113 REG("sched", S_IRUGO
|S_IWUSR
, proc_pid_sched_operations
),
3115 REG("comm", S_IRUGO
|S_IWUSR
, proc_pid_set_comm_operations
),
3116 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3117 INF("syscall", S_IRUGO
, proc_pid_syscall
),
3119 INF("cmdline", S_IRUGO
, proc_pid_cmdline
),
3120 ONE("stat", S_IRUGO
, proc_tid_stat
),
3121 ONE("statm", S_IRUGO
, proc_pid_statm
),
3122 REG("maps", S_IRUGO
, proc_maps_operations
),
3124 REG("numa_maps", S_IRUGO
, proc_numa_maps_operations
),
3126 REG("mem", S_IRUSR
|S_IWUSR
, proc_mem_operations
),
3127 LNK("cwd", proc_cwd_link
),
3128 LNK("root", proc_root_link
),
3129 LNK("exe", proc_exe_link
),
3130 REG("mounts", S_IRUGO
, proc_mounts_operations
),
3131 REG("mountinfo", S_IRUGO
, proc_mountinfo_operations
),
3132 #ifdef CONFIG_PROC_PAGE_MONITOR
3133 REG("clear_refs", S_IWUSR
, proc_clear_refs_operations
),
3134 REG("smaps", S_IRUGO
, proc_smaps_operations
),
3135 REG("pagemap", S_IRUGO
, proc_pagemap_operations
),
3137 #ifdef CONFIG_SECURITY
3138 DIR("attr", S_IRUGO
|S_IXUGO
, proc_attr_dir_inode_operations
, proc_attr_dir_operations
),
3140 #ifdef CONFIG_KALLSYMS
3141 INF("wchan", S_IRUGO
, proc_pid_wchan
),
3143 #ifdef CONFIG_STACKTRACE
3144 ONE("stack", S_IRUGO
, proc_pid_stack
),
3146 #ifdef CONFIG_SCHEDSTATS
3147 INF("schedstat", S_IRUGO
, proc_pid_schedstat
),
3149 #ifdef CONFIG_LATENCYTOP
3150 REG("latency", S_IRUGO
, proc_lstats_operations
),
3152 #ifdef CONFIG_PROC_PID_CPUSET
3153 REG("cpuset", S_IRUGO
, proc_cpuset_operations
),
3155 #ifdef CONFIG_CGROUPS
3156 REG("cgroup", S_IRUGO
, proc_cgroup_operations
),
3158 INF("oom_score", S_IRUGO
, proc_oom_score
),
3159 REG("oom_adj", S_IRUGO
|S_IWUSR
, proc_oom_adjust_operations
),
3160 REG("oom_score_adj", S_IRUGO
|S_IWUSR
, proc_oom_score_adj_operations
),
3161 #ifdef CONFIG_AUDITSYSCALL
3162 REG("loginuid", S_IWUSR
|S_IRUGO
, proc_loginuid_operations
),
3163 REG("sessionid", S_IRUGO
, proc_sessionid_operations
),
3165 #ifdef CONFIG_FAULT_INJECTION
3166 REG("make-it-fail", S_IRUGO
|S_IWUSR
, proc_fault_inject_operations
),
3168 #ifdef CONFIG_TASK_IO_ACCOUNTING
3169 INF("io", S_IRUSR
, proc_tid_io_accounting
),
3171 #ifdef CONFIG_HARDWALL
3172 INF("hardwall", S_IRUGO
, proc_pid_hardwall
),
3176 static int proc_tid_base_readdir(struct file
* filp
,
3177 void * dirent
, filldir_t filldir
)
3179 return proc_pident_readdir(filp
,dirent
,filldir
,
3180 tid_base_stuff
,ARRAY_SIZE(tid_base_stuff
));
3183 static struct dentry
*proc_tid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
){
3184 return proc_pident_lookup(dir
, dentry
,
3185 tid_base_stuff
, ARRAY_SIZE(tid_base_stuff
));
3188 static const struct file_operations proc_tid_base_operations
= {
3189 .read
= generic_read_dir
,
3190 .readdir
= proc_tid_base_readdir
,
3191 .llseek
= default_llseek
,
3194 static const struct inode_operations proc_tid_base_inode_operations
= {
3195 .lookup
= proc_tid_base_lookup
,
3196 .getattr
= pid_getattr
,
3197 .setattr
= proc_setattr
,
3200 static struct dentry
*proc_task_instantiate(struct inode
*dir
,
3201 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
3203 struct dentry
*error
= ERR_PTR(-ENOENT
);
3204 struct inode
*inode
;
3205 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
3209 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
3210 inode
->i_op
= &proc_tid_base_inode_operations
;
3211 inode
->i_fop
= &proc_tid_base_operations
;
3212 inode
->i_flags
|=S_IMMUTABLE
;
3214 inode
->i_nlink
= 2 + pid_entry_count_dirs(tid_base_stuff
,
3215 ARRAY_SIZE(tid_base_stuff
));
3217 d_set_d_op(dentry
, &pid_dentry_operations
);
3219 d_add(dentry
, inode
);
3220 /* Close the race of the process dying before we return the dentry */
3221 if (pid_revalidate(dentry
, NULL
))
3227 static struct dentry
*proc_task_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
)
3229 struct dentry
*result
= ERR_PTR(-ENOENT
);
3230 struct task_struct
*task
;
3231 struct task_struct
*leader
= get_proc_task(dir
);
3233 struct pid_namespace
*ns
;
3238 tid
= name_to_int(dentry
);
3242 ns
= dentry
->d_sb
->s_fs_info
;
3244 task
= find_task_by_pid_ns(tid
, ns
);
3246 get_task_struct(task
);
3250 if (!same_thread_group(leader
, task
))
3253 result
= proc_task_instantiate(dir
, dentry
, task
, NULL
);
3255 put_task_struct(task
);
3257 put_task_struct(leader
);
3263 * Find the first tid of a thread group to return to user space.
3265 * Usually this is just the thread group leader, but if the users
3266 * buffer was too small or there was a seek into the middle of the
3267 * directory we have more work todo.
3269 * In the case of a short read we start with find_task_by_pid.
3271 * In the case of a seek we start with the leader and walk nr
3274 static struct task_struct
*first_tid(struct task_struct
*leader
,
3275 int tid
, int nr
, struct pid_namespace
*ns
)
3277 struct task_struct
*pos
;
3280 /* Attempt to start with the pid of a thread */
3281 if (tid
&& (nr
> 0)) {
3282 pos
= find_task_by_pid_ns(tid
, ns
);
3283 if (pos
&& (pos
->group_leader
== leader
))
3287 /* If nr exceeds the number of threads there is nothing todo */
3289 if (nr
&& nr
>= get_nr_threads(leader
))
3292 /* If we haven't found our starting place yet start
3293 * with the leader and walk nr threads forward.
3295 for (pos
= leader
; nr
> 0; --nr
) {
3296 pos
= next_thread(pos
);
3297 if (pos
== leader
) {
3303 get_task_struct(pos
);
3310 * Find the next thread in the thread list.
3311 * Return NULL if there is an error or no next thread.
3313 * The reference to the input task_struct is released.
3315 static struct task_struct
*next_tid(struct task_struct
*start
)
3317 struct task_struct
*pos
= NULL
;
3319 if (pid_alive(start
)) {
3320 pos
= next_thread(start
);
3321 if (thread_group_leader(pos
))
3324 get_task_struct(pos
);
3327 put_task_struct(start
);
3331 static int proc_task_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
3332 struct task_struct
*task
, int tid
)
3334 char name
[PROC_NUMBUF
];
3335 int len
= snprintf(name
, sizeof(name
), "%d", tid
);
3336 return proc_fill_cache(filp
, dirent
, filldir
, name
, len
,
3337 proc_task_instantiate
, task
, NULL
);
3340 /* for the /proc/TGID/task/ directories */
3341 static int proc_task_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
3343 struct dentry
*dentry
= filp
->f_path
.dentry
;
3344 struct inode
*inode
= dentry
->d_inode
;
3345 struct task_struct
*leader
= NULL
;
3346 struct task_struct
*task
;
3347 int retval
= -ENOENT
;
3350 struct pid_namespace
*ns
;
3352 task
= get_proc_task(inode
);
3356 if (pid_alive(task
)) {
3357 leader
= task
->group_leader
;
3358 get_task_struct(leader
);
3361 put_task_struct(task
);
3366 switch ((unsigned long)filp
->f_pos
) {
3369 if (filldir(dirent
, ".", 1, filp
->f_pos
, ino
, DT_DIR
) < 0)
3374 ino
= parent_ino(dentry
);
3375 if (filldir(dirent
, "..", 2, filp
->f_pos
, ino
, DT_DIR
) < 0)
3381 /* f_version caches the tgid value that the last readdir call couldn't
3382 * return. lseek aka telldir automagically resets f_version to 0.
3384 ns
= filp
->f_dentry
->d_sb
->s_fs_info
;
3385 tid
= (int)filp
->f_version
;
3386 filp
->f_version
= 0;
3387 for (task
= first_tid(leader
, tid
, filp
->f_pos
- 2, ns
);
3389 task
= next_tid(task
), filp
->f_pos
++) {
3390 tid
= task_pid_nr_ns(task
, ns
);
3391 if (proc_task_fill_cache(filp
, dirent
, filldir
, task
, tid
) < 0) {
3392 /* returning this tgid failed, save it as the first
3393 * pid for the next readir call */
3394 filp
->f_version
= (u64
)tid
;
3395 put_task_struct(task
);
3400 put_task_struct(leader
);
3405 static int proc_task_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
3407 struct inode
*inode
= dentry
->d_inode
;
3408 struct task_struct
*p
= get_proc_task(inode
);
3409 generic_fillattr(inode
, stat
);
3412 stat
->nlink
+= get_nr_threads(p
);
3419 static const struct inode_operations proc_task_inode_operations
= {
3420 .lookup
= proc_task_lookup
,
3421 .getattr
= proc_task_getattr
,
3422 .setattr
= proc_setattr
,
3425 static const struct file_operations proc_task_operations
= {
3426 .read
= generic_read_dir
,
3427 .readdir
= proc_task_readdir
,
3428 .llseek
= default_llseek
,