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 #include <linux/flex_array.h>
87 #ifdef CONFIG_HARDWALL
88 #include <asm/hardwall.h>
90 #include <trace/events/oom.h>
94 * Implementing inode permission operations in /proc is almost
95 * certainly an error. Permission checks need to happen during
96 * each system call not at open time. The reason is that most of
97 * what we wish to check for permissions in /proc varies at runtime.
99 * The classic example of a problem is opening file descriptors
100 * in /proc for a task before it execs a suid executable.
107 const struct inode_operations
*iop
;
108 const struct file_operations
*fop
;
112 #define NOD(NAME, MODE, IOP, FOP, OP) { \
114 .len = sizeof(NAME) - 1, \
121 #define DIR(NAME, MODE, iops, fops) \
122 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
123 #define LNK(NAME, get_link) \
124 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
125 &proc_pid_link_inode_operations, NULL, \
126 { .proc_get_link = get_link } )
127 #define REG(NAME, MODE, fops) \
128 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
129 #define INF(NAME, MODE, read) \
130 NOD(NAME, (S_IFREG|(MODE)), \
131 NULL, &proc_info_file_operations, \
132 { .proc_read = read } )
133 #define ONE(NAME, MODE, show) \
134 NOD(NAME, (S_IFREG|(MODE)), \
135 NULL, &proc_single_file_operations, \
136 { .proc_show = show } )
138 static int proc_fd_permission(struct inode
*inode
, int mask
);
141 * Count the number of hardlinks for the pid_entry table, excluding the .
144 static unsigned int pid_entry_count_dirs(const struct pid_entry
*entries
,
151 for (i
= 0; i
< n
; ++i
) {
152 if (S_ISDIR(entries
[i
].mode
))
159 static int get_task_root(struct task_struct
*task
, struct path
*root
)
161 int result
= -ENOENT
;
165 get_fs_root(task
->fs
, root
);
172 static int proc_cwd_link(struct dentry
*dentry
, struct path
*path
)
174 struct task_struct
*task
= get_proc_task(dentry
->d_inode
);
175 int result
= -ENOENT
;
180 get_fs_pwd(task
->fs
, path
);
184 put_task_struct(task
);
189 static int proc_root_link(struct dentry
*dentry
, struct path
*path
)
191 struct task_struct
*task
= get_proc_task(dentry
->d_inode
);
192 int result
= -ENOENT
;
195 result
= get_task_root(task
, path
);
196 put_task_struct(task
);
201 static struct mm_struct
*mm_access(struct task_struct
*task
, unsigned int mode
)
203 struct mm_struct
*mm
;
206 err
= mutex_lock_killable(&task
->signal
->cred_guard_mutex
);
210 mm
= get_task_mm(task
);
211 if (mm
&& mm
!= current
->mm
&&
212 !ptrace_may_access(task
, mode
)) {
214 mm
= ERR_PTR(-EACCES
);
216 mutex_unlock(&task
->signal
->cred_guard_mutex
);
221 struct mm_struct
*mm_for_maps(struct task_struct
*task
)
223 return mm_access(task
, PTRACE_MODE_READ
);
226 static int proc_pid_cmdline(struct task_struct
*task
, char * buffer
)
230 struct mm_struct
*mm
= get_task_mm(task
);
234 goto out_mm
; /* Shh! No looking before we're done */
236 len
= mm
->arg_end
- mm
->arg_start
;
241 res
= access_process_vm(task
, mm
->arg_start
, buffer
, len
, 0);
243 // If the nul at the end of args has been overwritten, then
244 // assume application is using setproctitle(3).
245 if (res
> 0 && buffer
[res
-1] != '\0' && len
< PAGE_SIZE
) {
246 len
= strnlen(buffer
, res
);
250 len
= mm
->env_end
- mm
->env_start
;
251 if (len
> PAGE_SIZE
- res
)
252 len
= PAGE_SIZE
- res
;
253 res
+= access_process_vm(task
, mm
->env_start
, buffer
+res
, len
, 0);
254 res
= strnlen(buffer
, res
);
263 static int proc_pid_auxv(struct task_struct
*task
, char *buffer
)
265 struct mm_struct
*mm
= mm_for_maps(task
);
266 int res
= PTR_ERR(mm
);
267 if (mm
&& !IS_ERR(mm
)) {
268 unsigned int nwords
= 0;
271 } while (mm
->saved_auxv
[nwords
- 2] != 0); /* AT_NULL */
272 res
= nwords
* sizeof(mm
->saved_auxv
[0]);
275 memcpy(buffer
, mm
->saved_auxv
, res
);
282 #ifdef CONFIG_KALLSYMS
284 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
285 * Returns the resolved symbol. If that fails, simply return the address.
287 static int proc_pid_wchan(struct task_struct
*task
, char *buffer
)
290 char symname
[KSYM_NAME_LEN
];
292 wchan
= get_wchan(task
);
294 if (lookup_symbol_name(wchan
, symname
) < 0)
295 if (!ptrace_may_access(task
, PTRACE_MODE_READ
))
298 return sprintf(buffer
, "%lu", wchan
);
300 return sprintf(buffer
, "%s", symname
);
302 #endif /* CONFIG_KALLSYMS */
304 static int lock_trace(struct task_struct
*task
)
306 int err
= mutex_lock_killable(&task
->signal
->cred_guard_mutex
);
309 if (!ptrace_may_access(task
, PTRACE_MODE_ATTACH
)) {
310 mutex_unlock(&task
->signal
->cred_guard_mutex
);
316 static void unlock_trace(struct task_struct
*task
)
318 mutex_unlock(&task
->signal
->cred_guard_mutex
);
321 #ifdef CONFIG_STACKTRACE
323 #define MAX_STACK_TRACE_DEPTH 64
325 static int proc_pid_stack(struct seq_file
*m
, struct pid_namespace
*ns
,
326 struct pid
*pid
, struct task_struct
*task
)
328 struct stack_trace trace
;
329 unsigned long *entries
;
333 entries
= kmalloc(MAX_STACK_TRACE_DEPTH
* sizeof(*entries
), GFP_KERNEL
);
337 trace
.nr_entries
= 0;
338 trace
.max_entries
= MAX_STACK_TRACE_DEPTH
;
339 trace
.entries
= entries
;
342 err
= lock_trace(task
);
344 save_stack_trace_tsk(task
, &trace
);
346 for (i
= 0; i
< trace
.nr_entries
; i
++) {
347 seq_printf(m
, "[<%pK>] %pS\n",
348 (void *)entries
[i
], (void *)entries
[i
]);
358 #ifdef CONFIG_SCHEDSTATS
360 * Provides /proc/PID/schedstat
362 static int proc_pid_schedstat(struct task_struct
*task
, char *buffer
)
364 return sprintf(buffer
, "%llu %llu %lu\n",
365 (unsigned long long)task
->se
.sum_exec_runtime
,
366 (unsigned long long)task
->sched_info
.run_delay
,
367 task
->sched_info
.pcount
);
371 #ifdef CONFIG_LATENCYTOP
372 static int lstats_show_proc(struct seq_file
*m
, void *v
)
375 struct inode
*inode
= m
->private;
376 struct task_struct
*task
= get_proc_task(inode
);
380 seq_puts(m
, "Latency Top version : v0.1\n");
381 for (i
= 0; i
< 32; i
++) {
382 struct latency_record
*lr
= &task
->latency_record
[i
];
383 if (lr
->backtrace
[0]) {
385 seq_printf(m
, "%i %li %li",
386 lr
->count
, lr
->time
, lr
->max
);
387 for (q
= 0; q
< LT_BACKTRACEDEPTH
; q
++) {
388 unsigned long bt
= lr
->backtrace
[q
];
393 seq_printf(m
, " %ps", (void *)bt
);
399 put_task_struct(task
);
403 static int lstats_open(struct inode
*inode
, struct file
*file
)
405 return single_open(file
, lstats_show_proc
, inode
);
408 static ssize_t
lstats_write(struct file
*file
, const char __user
*buf
,
409 size_t count
, loff_t
*offs
)
411 struct task_struct
*task
= get_proc_task(file
->f_dentry
->d_inode
);
415 clear_all_latency_tracing(task
);
416 put_task_struct(task
);
421 static const struct file_operations proc_lstats_operations
= {
424 .write
= lstats_write
,
426 .release
= single_release
,
431 static int proc_oom_score(struct task_struct
*task
, char *buffer
)
433 unsigned long points
= 0;
435 read_lock(&tasklist_lock
);
437 points
= oom_badness(task
, NULL
, NULL
,
438 totalram_pages
+ total_swap_pages
);
439 read_unlock(&tasklist_lock
);
440 return sprintf(buffer
, "%lu\n", points
);
448 static const struct limit_names lnames
[RLIM_NLIMITS
] = {
449 [RLIMIT_CPU
] = {"Max cpu time", "seconds"},
450 [RLIMIT_FSIZE
] = {"Max file size", "bytes"},
451 [RLIMIT_DATA
] = {"Max data size", "bytes"},
452 [RLIMIT_STACK
] = {"Max stack size", "bytes"},
453 [RLIMIT_CORE
] = {"Max core file size", "bytes"},
454 [RLIMIT_RSS
] = {"Max resident set", "bytes"},
455 [RLIMIT_NPROC
] = {"Max processes", "processes"},
456 [RLIMIT_NOFILE
] = {"Max open files", "files"},
457 [RLIMIT_MEMLOCK
] = {"Max locked memory", "bytes"},
458 [RLIMIT_AS
] = {"Max address space", "bytes"},
459 [RLIMIT_LOCKS
] = {"Max file locks", "locks"},
460 [RLIMIT_SIGPENDING
] = {"Max pending signals", "signals"},
461 [RLIMIT_MSGQUEUE
] = {"Max msgqueue size", "bytes"},
462 [RLIMIT_NICE
] = {"Max nice priority", NULL
},
463 [RLIMIT_RTPRIO
] = {"Max realtime priority", NULL
},
464 [RLIMIT_RTTIME
] = {"Max realtime timeout", "us"},
467 /* Display limits for a process */
468 static int proc_pid_limits(struct task_struct
*task
, char *buffer
)
473 char *bufptr
= buffer
;
475 struct rlimit rlim
[RLIM_NLIMITS
];
477 if (!lock_task_sighand(task
, &flags
))
479 memcpy(rlim
, task
->signal
->rlim
, sizeof(struct rlimit
) * RLIM_NLIMITS
);
480 unlock_task_sighand(task
, &flags
);
483 * print the file header
485 count
+= sprintf(&bufptr
[count
], "%-25s %-20s %-20s %-10s\n",
486 "Limit", "Soft Limit", "Hard Limit", "Units");
488 for (i
= 0; i
< RLIM_NLIMITS
; i
++) {
489 if (rlim
[i
].rlim_cur
== RLIM_INFINITY
)
490 count
+= sprintf(&bufptr
[count
], "%-25s %-20s ",
491 lnames
[i
].name
, "unlimited");
493 count
+= sprintf(&bufptr
[count
], "%-25s %-20lu ",
494 lnames
[i
].name
, rlim
[i
].rlim_cur
);
496 if (rlim
[i
].rlim_max
== RLIM_INFINITY
)
497 count
+= sprintf(&bufptr
[count
], "%-20s ", "unlimited");
499 count
+= sprintf(&bufptr
[count
], "%-20lu ",
503 count
+= sprintf(&bufptr
[count
], "%-10s\n",
506 count
+= sprintf(&bufptr
[count
], "\n");
512 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
513 static int proc_pid_syscall(struct task_struct
*task
, char *buffer
)
516 unsigned long args
[6], sp
, pc
;
517 int res
= lock_trace(task
);
521 if (task_current_syscall(task
, &nr
, args
, 6, &sp
, &pc
))
522 res
= sprintf(buffer
, "running\n");
524 res
= sprintf(buffer
, "%ld 0x%lx 0x%lx\n", nr
, sp
, pc
);
526 res
= sprintf(buffer
,
527 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
529 args
[0], args
[1], args
[2], args
[3], args
[4], args
[5],
534 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
536 /************************************************************************/
537 /* Here the fs part begins */
538 /************************************************************************/
540 /* permission checks */
541 static int proc_fd_access_allowed(struct inode
*inode
)
543 struct task_struct
*task
;
545 /* Allow access to a task's file descriptors if it is us or we
546 * may use ptrace attach to the process and find out that
549 task
= get_proc_task(inode
);
551 allowed
= ptrace_may_access(task
, PTRACE_MODE_READ
);
552 put_task_struct(task
);
557 int proc_setattr(struct dentry
*dentry
, struct iattr
*attr
)
560 struct inode
*inode
= dentry
->d_inode
;
562 if (attr
->ia_valid
& ATTR_MODE
)
565 error
= inode_change_ok(inode
, attr
);
569 if ((attr
->ia_valid
& ATTR_SIZE
) &&
570 attr
->ia_size
!= i_size_read(inode
)) {
571 error
= vmtruncate(inode
, attr
->ia_size
);
576 setattr_copy(inode
, attr
);
577 mark_inode_dirty(inode
);
582 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
583 * or euid/egid (for hide_pid_min=2)?
585 static bool has_pid_permissions(struct pid_namespace
*pid
,
586 struct task_struct
*task
,
589 if (pid
->hide_pid
< hide_pid_min
)
591 if (in_group_p(pid
->pid_gid
))
593 return ptrace_may_access(task
, PTRACE_MODE_READ
);
597 static int proc_pid_permission(struct inode
*inode
, int mask
)
599 struct pid_namespace
*pid
= inode
->i_sb
->s_fs_info
;
600 struct task_struct
*task
;
603 task
= get_proc_task(inode
);
606 has_perms
= has_pid_permissions(pid
, task
, 1);
607 put_task_struct(task
);
610 if (pid
->hide_pid
== 2) {
612 * Let's make getdents(), stat(), and open()
613 * consistent with each other. If a process
614 * may not stat() a file, it shouldn't be seen
622 return generic_permission(inode
, mask
);
627 static const struct inode_operations proc_def_inode_operations
= {
628 .setattr
= proc_setattr
,
631 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
633 static ssize_t
proc_info_read(struct file
* file
, char __user
* buf
,
634 size_t count
, loff_t
*ppos
)
636 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
639 struct task_struct
*task
= get_proc_task(inode
);
645 if (count
> PROC_BLOCK_SIZE
)
646 count
= PROC_BLOCK_SIZE
;
649 if (!(page
= __get_free_page(GFP_TEMPORARY
)))
652 length
= PROC_I(inode
)->op
.proc_read(task
, (char*)page
);
655 length
= simple_read_from_buffer(buf
, count
, ppos
, (char *)page
, length
);
658 put_task_struct(task
);
663 static const struct file_operations proc_info_file_operations
= {
664 .read
= proc_info_read
,
665 .llseek
= generic_file_llseek
,
668 static int proc_single_show(struct seq_file
*m
, void *v
)
670 struct inode
*inode
= m
->private;
671 struct pid_namespace
*ns
;
673 struct task_struct
*task
;
676 ns
= inode
->i_sb
->s_fs_info
;
677 pid
= proc_pid(inode
);
678 task
= get_pid_task(pid
, PIDTYPE_PID
);
682 ret
= PROC_I(inode
)->op
.proc_show(m
, ns
, pid
, task
);
684 put_task_struct(task
);
688 static int proc_single_open(struct inode
*inode
, struct file
*filp
)
690 return single_open(filp
, proc_single_show
, inode
);
693 static const struct file_operations proc_single_file_operations
= {
694 .open
= proc_single_open
,
697 .release
= single_release
,
700 static int mem_open(struct inode
* inode
, struct file
* file
)
702 struct task_struct
*task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
703 struct mm_struct
*mm
;
708 mm
= mm_access(task
, PTRACE_MODE_ATTACH
);
709 put_task_struct(task
);
714 /* OK to pass negative loff_t, we can catch out-of-range */
715 file
->f_mode
|= FMODE_UNSIGNED_OFFSET
;
716 file
->private_data
= mm
;
721 static ssize_t
mem_read(struct file
* file
, char __user
* buf
,
722 size_t count
, loff_t
*ppos
)
726 unsigned long src
= *ppos
;
727 struct mm_struct
*mm
= file
->private_data
;
732 page
= (char *)__get_free_page(GFP_TEMPORARY
);
739 int this_len
, retval
;
741 this_len
= (count
> PAGE_SIZE
) ? PAGE_SIZE
: count
;
742 retval
= access_remote_vm(mm
, src
, page
, this_len
, 0);
749 if (copy_to_user(buf
, page
, retval
)) {
761 free_page((unsigned long) page
);
765 static ssize_t
mem_write(struct file
* file
, const char __user
*buf
,
766 size_t count
, loff_t
*ppos
)
770 unsigned long dst
= *ppos
;
771 struct mm_struct
*mm
= file
->private_data
;
776 page
= (char *)__get_free_page(GFP_TEMPORARY
);
782 int this_len
, retval
;
784 this_len
= (count
> PAGE_SIZE
) ? PAGE_SIZE
: count
;
785 if (copy_from_user(page
, buf
, this_len
)) {
789 retval
= access_remote_vm(mm
, dst
, page
, this_len
, 1);
802 free_page((unsigned long) page
);
806 loff_t
mem_lseek(struct file
*file
, loff_t offset
, int orig
)
810 file
->f_pos
= offset
;
813 file
->f_pos
+= offset
;
818 force_successful_syscall_return();
822 static int mem_release(struct inode
*inode
, struct file
*file
)
824 struct mm_struct
*mm
= file
->private_data
;
830 static const struct file_operations proc_mem_operations
= {
835 .release
= mem_release
,
838 static ssize_t
environ_read(struct file
*file
, char __user
*buf
,
839 size_t count
, loff_t
*ppos
)
841 struct task_struct
*task
= get_proc_task(file
->f_dentry
->d_inode
);
843 unsigned long src
= *ppos
;
845 struct mm_struct
*mm
;
851 page
= (char *)__get_free_page(GFP_TEMPORARY
);
856 mm
= mm_for_maps(task
);
858 if (!mm
|| IS_ERR(mm
))
863 int this_len
, retval
, max_len
;
865 this_len
= mm
->env_end
- (mm
->env_start
+ src
);
870 max_len
= (count
> PAGE_SIZE
) ? PAGE_SIZE
: count
;
871 this_len
= (this_len
> max_len
) ? max_len
: this_len
;
873 retval
= access_process_vm(task
, (mm
->env_start
+ src
),
881 if (copy_to_user(buf
, page
, retval
)) {
895 free_page((unsigned long) page
);
897 put_task_struct(task
);
902 static const struct file_operations proc_environ_operations
= {
903 .read
= environ_read
,
904 .llseek
= generic_file_llseek
,
907 static ssize_t
oom_adjust_read(struct file
*file
, char __user
*buf
,
908 size_t count
, loff_t
*ppos
)
910 struct task_struct
*task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
911 char buffer
[PROC_NUMBUF
];
913 int oom_adjust
= OOM_DISABLE
;
919 if (lock_task_sighand(task
, &flags
)) {
920 oom_adjust
= task
->signal
->oom_adj
;
921 unlock_task_sighand(task
, &flags
);
924 put_task_struct(task
);
926 len
= snprintf(buffer
, sizeof(buffer
), "%i\n", oom_adjust
);
928 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
931 static ssize_t
oom_adjust_write(struct file
*file
, const char __user
*buf
,
932 size_t count
, loff_t
*ppos
)
934 struct task_struct
*task
;
935 char buffer
[PROC_NUMBUF
];
940 memset(buffer
, 0, sizeof(buffer
));
941 if (count
> sizeof(buffer
) - 1)
942 count
= sizeof(buffer
) - 1;
943 if (copy_from_user(buffer
, buf
, count
)) {
948 err
= kstrtoint(strstrip(buffer
), 0, &oom_adjust
);
951 if ((oom_adjust
< OOM_ADJUST_MIN
|| oom_adjust
> OOM_ADJUST_MAX
) &&
952 oom_adjust
!= OOM_DISABLE
) {
957 task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
969 if (!lock_task_sighand(task
, &flags
)) {
974 if (oom_adjust
< task
->signal
->oom_adj
&& !capable(CAP_SYS_RESOURCE
)) {
980 * Warn that /proc/pid/oom_adj is deprecated, see
981 * Documentation/feature-removal-schedule.txt.
983 printk_once(KERN_WARNING
"%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
984 current
->comm
, task_pid_nr(current
), task_pid_nr(task
),
986 task
->signal
->oom_adj
= oom_adjust
;
988 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
989 * value is always attainable.
991 if (task
->signal
->oom_adj
== OOM_ADJUST_MAX
)
992 task
->signal
->oom_score_adj
= OOM_SCORE_ADJ_MAX
;
994 task
->signal
->oom_score_adj
= (oom_adjust
* OOM_SCORE_ADJ_MAX
) /
996 trace_oom_score_adj_update(task
);
998 unlock_task_sighand(task
, &flags
);
1001 put_task_struct(task
);
1003 return err
< 0 ? err
: count
;
1006 static const struct file_operations proc_oom_adjust_operations
= {
1007 .read
= oom_adjust_read
,
1008 .write
= oom_adjust_write
,
1009 .llseek
= generic_file_llseek
,
1012 static ssize_t
oom_score_adj_read(struct file
*file
, char __user
*buf
,
1013 size_t count
, loff_t
*ppos
)
1015 struct task_struct
*task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
1016 char buffer
[PROC_NUMBUF
];
1017 int oom_score_adj
= OOM_SCORE_ADJ_MIN
;
1018 unsigned long flags
;
1023 if (lock_task_sighand(task
, &flags
)) {
1024 oom_score_adj
= task
->signal
->oom_score_adj
;
1025 unlock_task_sighand(task
, &flags
);
1027 put_task_struct(task
);
1028 len
= snprintf(buffer
, sizeof(buffer
), "%d\n", oom_score_adj
);
1029 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
1032 static ssize_t
oom_score_adj_write(struct file
*file
, const char __user
*buf
,
1033 size_t count
, loff_t
*ppos
)
1035 struct task_struct
*task
;
1036 char buffer
[PROC_NUMBUF
];
1037 unsigned long flags
;
1041 memset(buffer
, 0, sizeof(buffer
));
1042 if (count
> sizeof(buffer
) - 1)
1043 count
= sizeof(buffer
) - 1;
1044 if (copy_from_user(buffer
, buf
, count
)) {
1049 err
= kstrtoint(strstrip(buffer
), 0, &oom_score_adj
);
1052 if (oom_score_adj
< OOM_SCORE_ADJ_MIN
||
1053 oom_score_adj
> OOM_SCORE_ADJ_MAX
) {
1058 task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
1070 if (!lock_task_sighand(task
, &flags
)) {
1075 if (oom_score_adj
< task
->signal
->oom_score_adj_min
&&
1076 !capable(CAP_SYS_RESOURCE
)) {
1081 task
->signal
->oom_score_adj
= oom_score_adj
;
1082 if (has_capability_noaudit(current
, CAP_SYS_RESOURCE
))
1083 task
->signal
->oom_score_adj_min
= oom_score_adj
;
1084 trace_oom_score_adj_update(task
);
1086 * Scale /proc/pid/oom_adj appropriately ensuring that OOM_DISABLE is
1087 * always attainable.
1089 if (task
->signal
->oom_score_adj
== OOM_SCORE_ADJ_MIN
)
1090 task
->signal
->oom_adj
= OOM_DISABLE
;
1092 task
->signal
->oom_adj
= (oom_score_adj
* OOM_ADJUST_MAX
) /
1095 unlock_task_sighand(task
, &flags
);
1098 put_task_struct(task
);
1100 return err
< 0 ? err
: count
;
1103 static const struct file_operations proc_oom_score_adj_operations
= {
1104 .read
= oom_score_adj_read
,
1105 .write
= oom_score_adj_write
,
1106 .llseek
= default_llseek
,
1109 #ifdef CONFIG_AUDITSYSCALL
1110 #define TMPBUFLEN 21
1111 static ssize_t
proc_loginuid_read(struct file
* file
, char __user
* buf
,
1112 size_t count
, loff_t
*ppos
)
1114 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
1115 struct task_struct
*task
= get_proc_task(inode
);
1117 char tmpbuf
[TMPBUFLEN
];
1121 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
1122 audit_get_loginuid(task
));
1123 put_task_struct(task
);
1124 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1127 static ssize_t
proc_loginuid_write(struct file
* file
, const char __user
* buf
,
1128 size_t count
, loff_t
*ppos
)
1130 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
1135 if (!capable(CAP_AUDIT_CONTROL
))
1139 if (current
!= pid_task(proc_pid(inode
), PIDTYPE_PID
)) {
1145 if (count
>= PAGE_SIZE
)
1146 count
= PAGE_SIZE
- 1;
1149 /* No partial writes. */
1152 page
= (char*)__get_free_page(GFP_TEMPORARY
);
1156 if (copy_from_user(page
, buf
, count
))
1160 loginuid
= simple_strtoul(page
, &tmp
, 10);
1166 length
= audit_set_loginuid(current
, loginuid
);
1167 if (likely(length
== 0))
1171 free_page((unsigned long) page
);
1175 static const struct file_operations proc_loginuid_operations
= {
1176 .read
= proc_loginuid_read
,
1177 .write
= proc_loginuid_write
,
1178 .llseek
= generic_file_llseek
,
1181 static ssize_t
proc_sessionid_read(struct file
* file
, char __user
* buf
,
1182 size_t count
, loff_t
*ppos
)
1184 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
1185 struct task_struct
*task
= get_proc_task(inode
);
1187 char tmpbuf
[TMPBUFLEN
];
1191 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
1192 audit_get_sessionid(task
));
1193 put_task_struct(task
);
1194 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1197 static const struct file_operations proc_sessionid_operations
= {
1198 .read
= proc_sessionid_read
,
1199 .llseek
= generic_file_llseek
,
1203 #ifdef CONFIG_FAULT_INJECTION
1204 static ssize_t
proc_fault_inject_read(struct file
* file
, char __user
* buf
,
1205 size_t count
, loff_t
*ppos
)
1207 struct task_struct
*task
= get_proc_task(file
->f_dentry
->d_inode
);
1208 char buffer
[PROC_NUMBUF
];
1214 make_it_fail
= task
->make_it_fail
;
1215 put_task_struct(task
);
1217 len
= snprintf(buffer
, sizeof(buffer
), "%i\n", make_it_fail
);
1219 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
1222 static ssize_t
proc_fault_inject_write(struct file
* file
,
1223 const char __user
* buf
, size_t count
, loff_t
*ppos
)
1225 struct task_struct
*task
;
1226 char buffer
[PROC_NUMBUF
], *end
;
1229 if (!capable(CAP_SYS_RESOURCE
))
1231 memset(buffer
, 0, sizeof(buffer
));
1232 if (count
> sizeof(buffer
) - 1)
1233 count
= sizeof(buffer
) - 1;
1234 if (copy_from_user(buffer
, buf
, count
))
1236 make_it_fail
= simple_strtol(strstrip(buffer
), &end
, 0);
1239 task
= get_proc_task(file
->f_dentry
->d_inode
);
1242 task
->make_it_fail
= make_it_fail
;
1243 put_task_struct(task
);
1248 static const struct file_operations proc_fault_inject_operations
= {
1249 .read
= proc_fault_inject_read
,
1250 .write
= proc_fault_inject_write
,
1251 .llseek
= generic_file_llseek
,
1256 #ifdef CONFIG_SCHED_DEBUG
1258 * Print out various scheduling related per-task fields:
1260 static int sched_show(struct seq_file
*m
, void *v
)
1262 struct inode
*inode
= m
->private;
1263 struct task_struct
*p
;
1265 p
= get_proc_task(inode
);
1268 proc_sched_show_task(p
, m
);
1276 sched_write(struct file
*file
, const char __user
*buf
,
1277 size_t count
, loff_t
*offset
)
1279 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1280 struct task_struct
*p
;
1282 p
= get_proc_task(inode
);
1285 proc_sched_set_task(p
);
1292 static int sched_open(struct inode
*inode
, struct file
*filp
)
1294 return single_open(filp
, sched_show
, inode
);
1297 static const struct file_operations proc_pid_sched_operations
= {
1300 .write
= sched_write
,
1301 .llseek
= seq_lseek
,
1302 .release
= single_release
,
1307 #ifdef CONFIG_SCHED_AUTOGROUP
1309 * Print out autogroup related information:
1311 static int sched_autogroup_show(struct seq_file
*m
, void *v
)
1313 struct inode
*inode
= m
->private;
1314 struct task_struct
*p
;
1316 p
= get_proc_task(inode
);
1319 proc_sched_autogroup_show_task(p
, m
);
1327 sched_autogroup_write(struct file
*file
, const char __user
*buf
,
1328 size_t count
, loff_t
*offset
)
1330 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1331 struct task_struct
*p
;
1332 char buffer
[PROC_NUMBUF
];
1336 memset(buffer
, 0, sizeof(buffer
));
1337 if (count
> sizeof(buffer
) - 1)
1338 count
= sizeof(buffer
) - 1;
1339 if (copy_from_user(buffer
, buf
, count
))
1342 err
= kstrtoint(strstrip(buffer
), 0, &nice
);
1346 p
= get_proc_task(inode
);
1351 err
= proc_sched_autogroup_set_nice(p
, &err
);
1360 static int sched_autogroup_open(struct inode
*inode
, struct file
*filp
)
1364 ret
= single_open(filp
, sched_autogroup_show
, NULL
);
1366 struct seq_file
*m
= filp
->private_data
;
1373 static const struct file_operations proc_pid_sched_autogroup_operations
= {
1374 .open
= sched_autogroup_open
,
1376 .write
= sched_autogroup_write
,
1377 .llseek
= seq_lseek
,
1378 .release
= single_release
,
1381 #endif /* CONFIG_SCHED_AUTOGROUP */
1383 static ssize_t
comm_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
;
1388 char buffer
[TASK_COMM_LEN
];
1390 memset(buffer
, 0, sizeof(buffer
));
1391 if (count
> sizeof(buffer
) - 1)
1392 count
= sizeof(buffer
) - 1;
1393 if (copy_from_user(buffer
, buf
, count
))
1396 p
= get_proc_task(inode
);
1400 if (same_thread_group(current
, p
))
1401 set_task_comm(p
, buffer
);
1410 static int comm_show(struct seq_file
*m
, void *v
)
1412 struct inode
*inode
= m
->private;
1413 struct task_struct
*p
;
1415 p
= get_proc_task(inode
);
1420 seq_printf(m
, "%s\n", p
->comm
);
1428 static int comm_open(struct inode
*inode
, struct file
*filp
)
1430 return single_open(filp
, comm_show
, inode
);
1433 static const struct file_operations proc_pid_set_comm_operations
= {
1436 .write
= comm_write
,
1437 .llseek
= seq_lseek
,
1438 .release
= single_release
,
1441 static int proc_exe_link(struct dentry
*dentry
, struct path
*exe_path
)
1443 struct task_struct
*task
;
1444 struct mm_struct
*mm
;
1445 struct file
*exe_file
;
1447 task
= get_proc_task(dentry
->d_inode
);
1450 mm
= get_task_mm(task
);
1451 put_task_struct(task
);
1454 exe_file
= get_mm_exe_file(mm
);
1457 *exe_path
= exe_file
->f_path
;
1458 path_get(&exe_file
->f_path
);
1465 static void *proc_pid_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
1467 struct inode
*inode
= dentry
->d_inode
;
1468 int error
= -EACCES
;
1470 /* We don't need a base pointer in the /proc filesystem */
1471 path_put(&nd
->path
);
1473 /* Are we allowed to snoop on the tasks file descriptors? */
1474 if (!proc_fd_access_allowed(inode
))
1477 error
= PROC_I(inode
)->op
.proc_get_link(dentry
, &nd
->path
);
1479 return ERR_PTR(error
);
1482 static int do_proc_readlink(struct path
*path
, char __user
*buffer
, int buflen
)
1484 char *tmp
= (char*)__get_free_page(GFP_TEMPORARY
);
1491 pathname
= d_path(path
, tmp
, PAGE_SIZE
);
1492 len
= PTR_ERR(pathname
);
1493 if (IS_ERR(pathname
))
1495 len
= tmp
+ PAGE_SIZE
- 1 - pathname
;
1499 if (copy_to_user(buffer
, pathname
, len
))
1502 free_page((unsigned long)tmp
);
1506 static int proc_pid_readlink(struct dentry
* dentry
, char __user
* buffer
, int buflen
)
1508 int error
= -EACCES
;
1509 struct inode
*inode
= dentry
->d_inode
;
1512 /* Are we allowed to snoop on the tasks file descriptors? */
1513 if (!proc_fd_access_allowed(inode
))
1516 error
= PROC_I(inode
)->op
.proc_get_link(dentry
, &path
);
1520 error
= do_proc_readlink(&path
, buffer
, buflen
);
1526 static const struct inode_operations proc_pid_link_inode_operations
= {
1527 .readlink
= proc_pid_readlink
,
1528 .follow_link
= proc_pid_follow_link
,
1529 .setattr
= proc_setattr
,
1533 /* building an inode */
1535 static int task_dumpable(struct task_struct
*task
)
1538 struct mm_struct
*mm
;
1543 dumpable
= get_dumpable(mm
);
1550 struct inode
*proc_pid_make_inode(struct super_block
* sb
, struct task_struct
*task
)
1552 struct inode
* inode
;
1553 struct proc_inode
*ei
;
1554 const struct cred
*cred
;
1556 /* We need a new inode */
1558 inode
= new_inode(sb
);
1564 inode
->i_ino
= get_next_ino();
1565 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1566 inode
->i_op
= &proc_def_inode_operations
;
1569 * grab the reference to task.
1571 ei
->pid
= get_task_pid(task
, PIDTYPE_PID
);
1575 if (task_dumpable(task
)) {
1577 cred
= __task_cred(task
);
1578 inode
->i_uid
= cred
->euid
;
1579 inode
->i_gid
= cred
->egid
;
1582 security_task_to_inode(task
, inode
);
1592 int pid_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
1594 struct inode
*inode
= dentry
->d_inode
;
1595 struct task_struct
*task
;
1596 const struct cred
*cred
;
1597 struct pid_namespace
*pid
= dentry
->d_sb
->s_fs_info
;
1599 generic_fillattr(inode
, stat
);
1604 task
= pid_task(proc_pid(inode
), PIDTYPE_PID
);
1606 if (!has_pid_permissions(pid
, task
, 2)) {
1609 * This doesn't prevent learning whether PID exists,
1610 * it only makes getattr() consistent with readdir().
1614 if ((inode
->i_mode
== (S_IFDIR
|S_IRUGO
|S_IXUGO
)) ||
1615 task_dumpable(task
)) {
1616 cred
= __task_cred(task
);
1617 stat
->uid
= cred
->euid
;
1618 stat
->gid
= cred
->egid
;
1628 * Exceptional case: normally we are not allowed to unhash a busy
1629 * directory. In this case, however, we can do it - no aliasing problems
1630 * due to the way we treat inodes.
1632 * Rewrite the inode's ownerships here because the owning task may have
1633 * performed a setuid(), etc.
1635 * Before the /proc/pid/status file was created the only way to read
1636 * the effective uid of a /process was to stat /proc/pid. Reading
1637 * /proc/pid/status is slow enough that procps and other packages
1638 * kept stating /proc/pid. To keep the rules in /proc simple I have
1639 * made this apply to all per process world readable and executable
1642 int pid_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1644 struct inode
*inode
;
1645 struct task_struct
*task
;
1646 const struct cred
*cred
;
1648 if (nd
&& nd
->flags
& LOOKUP_RCU
)
1651 inode
= dentry
->d_inode
;
1652 task
= get_proc_task(inode
);
1655 if ((inode
->i_mode
== (S_IFDIR
|S_IRUGO
|S_IXUGO
)) ||
1656 task_dumpable(task
)) {
1658 cred
= __task_cred(task
);
1659 inode
->i_uid
= cred
->euid
;
1660 inode
->i_gid
= cred
->egid
;
1666 inode
->i_mode
&= ~(S_ISUID
| S_ISGID
);
1667 security_task_to_inode(task
, inode
);
1668 put_task_struct(task
);
1675 static int pid_delete_dentry(const struct dentry
* dentry
)
1677 /* Is the task we represent dead?
1678 * If so, then don't put the dentry on the lru list,
1679 * kill it immediately.
1681 return !proc_pid(dentry
->d_inode
)->tasks
[PIDTYPE_PID
].first
;
1684 const struct dentry_operations pid_dentry_operations
=
1686 .d_revalidate
= pid_revalidate
,
1687 .d_delete
= pid_delete_dentry
,
1693 * Fill a directory entry.
1695 * If possible create the dcache entry and derive our inode number and
1696 * file type from dcache entry.
1698 * Since all of the proc inode numbers are dynamically generated, the inode
1699 * numbers do not exist until the inode is cache. This means creating the
1700 * the dcache entry in readdir is necessary to keep the inode numbers
1701 * reported by readdir in sync with the inode numbers reported
1704 int proc_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
1705 const char *name
, int len
,
1706 instantiate_t instantiate
, struct task_struct
*task
, const void *ptr
)
1708 struct dentry
*child
, *dir
= filp
->f_path
.dentry
;
1709 struct inode
*inode
;
1712 unsigned type
= DT_UNKNOWN
;
1716 qname
.hash
= full_name_hash(name
, len
);
1718 child
= d_lookup(dir
, &qname
);
1721 new = d_alloc(dir
, &qname
);
1723 child
= instantiate(dir
->d_inode
, new, task
, ptr
);
1730 if (!child
|| IS_ERR(child
) || !child
->d_inode
)
1731 goto end_instantiate
;
1732 inode
= child
->d_inode
;
1735 type
= inode
->i_mode
>> 12;
1740 ino
= find_inode_number(dir
, &qname
);
1743 return filldir(dirent
, name
, len
, filp
->f_pos
, ino
, type
);
1746 static unsigned name_to_int(struct dentry
*dentry
)
1748 const char *name
= dentry
->d_name
.name
;
1749 int len
= dentry
->d_name
.len
;
1752 if (len
> 1 && *name
== '0')
1755 unsigned c
= *name
++ - '0';
1758 if (n
>= (~0U-9)/10)
1768 #define PROC_FDINFO_MAX 64
1770 static int proc_fd_info(struct inode
*inode
, struct path
*path
, char *info
)
1772 struct task_struct
*task
= get_proc_task(inode
);
1773 struct files_struct
*files
= NULL
;
1775 int fd
= proc_fd(inode
);
1778 files
= get_files_struct(task
);
1779 put_task_struct(task
);
1783 * We are not taking a ref to the file structure, so we must
1786 spin_lock(&files
->file_lock
);
1787 file
= fcheck_files(files
, fd
);
1789 unsigned int f_flags
;
1790 struct fdtable
*fdt
;
1792 fdt
= files_fdtable(files
);
1793 f_flags
= file
->f_flags
& ~O_CLOEXEC
;
1794 if (FD_ISSET(fd
, fdt
->close_on_exec
))
1795 f_flags
|= O_CLOEXEC
;
1798 *path
= file
->f_path
;
1799 path_get(&file
->f_path
);
1802 snprintf(info
, PROC_FDINFO_MAX
,
1805 (long long) file
->f_pos
,
1807 spin_unlock(&files
->file_lock
);
1808 put_files_struct(files
);
1811 spin_unlock(&files
->file_lock
);
1812 put_files_struct(files
);
1817 static int proc_fd_link(struct dentry
*dentry
, struct path
*path
)
1819 return proc_fd_info(dentry
->d_inode
, path
, NULL
);
1822 static int tid_fd_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1824 struct inode
*inode
;
1825 struct task_struct
*task
;
1827 struct files_struct
*files
;
1828 const struct cred
*cred
;
1830 if (nd
&& nd
->flags
& LOOKUP_RCU
)
1833 inode
= dentry
->d_inode
;
1834 task
= get_proc_task(inode
);
1835 fd
= proc_fd(inode
);
1838 files
= get_files_struct(task
);
1841 if (fcheck_files(files
, fd
)) {
1843 put_files_struct(files
);
1844 if (task_dumpable(task
)) {
1846 cred
= __task_cred(task
);
1847 inode
->i_uid
= cred
->euid
;
1848 inode
->i_gid
= cred
->egid
;
1854 inode
->i_mode
&= ~(S_ISUID
| S_ISGID
);
1855 security_task_to_inode(task
, inode
);
1856 put_task_struct(task
);
1860 put_files_struct(files
);
1862 put_task_struct(task
);
1868 static const struct dentry_operations tid_fd_dentry_operations
=
1870 .d_revalidate
= tid_fd_revalidate
,
1871 .d_delete
= pid_delete_dentry
,
1874 static struct dentry
*proc_fd_instantiate(struct inode
*dir
,
1875 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
1877 unsigned fd
= *(const unsigned *)ptr
;
1879 struct files_struct
*files
;
1880 struct inode
*inode
;
1881 struct proc_inode
*ei
;
1882 struct dentry
*error
= ERR_PTR(-ENOENT
);
1884 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
1889 files
= get_files_struct(task
);
1892 inode
->i_mode
= S_IFLNK
;
1895 * We are not taking a ref to the file structure, so we must
1898 spin_lock(&files
->file_lock
);
1899 file
= fcheck_files(files
, fd
);
1902 if (file
->f_mode
& FMODE_READ
)
1903 inode
->i_mode
|= S_IRUSR
| S_IXUSR
;
1904 if (file
->f_mode
& FMODE_WRITE
)
1905 inode
->i_mode
|= S_IWUSR
| S_IXUSR
;
1906 spin_unlock(&files
->file_lock
);
1907 put_files_struct(files
);
1909 inode
->i_op
= &proc_pid_link_inode_operations
;
1911 ei
->op
.proc_get_link
= proc_fd_link
;
1912 d_set_d_op(dentry
, &tid_fd_dentry_operations
);
1913 d_add(dentry
, inode
);
1914 /* Close the race of the process dying before we return the dentry */
1915 if (tid_fd_revalidate(dentry
, NULL
))
1921 spin_unlock(&files
->file_lock
);
1922 put_files_struct(files
);
1928 static struct dentry
*proc_lookupfd_common(struct inode
*dir
,
1929 struct dentry
*dentry
,
1930 instantiate_t instantiate
)
1932 struct task_struct
*task
= get_proc_task(dir
);
1933 unsigned fd
= name_to_int(dentry
);
1934 struct dentry
*result
= ERR_PTR(-ENOENT
);
1941 result
= instantiate(dir
, dentry
, task
, &fd
);
1943 put_task_struct(task
);
1948 static int proc_readfd_common(struct file
* filp
, void * dirent
,
1949 filldir_t filldir
, instantiate_t instantiate
)
1951 struct dentry
*dentry
= filp
->f_path
.dentry
;
1952 struct inode
*inode
= dentry
->d_inode
;
1953 struct task_struct
*p
= get_proc_task(inode
);
1954 unsigned int fd
, ino
;
1956 struct files_struct
* files
;
1966 if (filldir(dirent
, ".", 1, 0, inode
->i_ino
, DT_DIR
) < 0)
1970 ino
= parent_ino(dentry
);
1971 if (filldir(dirent
, "..", 2, 1, ino
, DT_DIR
) < 0)
1975 files
= get_files_struct(p
);
1979 for (fd
= filp
->f_pos
-2;
1980 fd
< files_fdtable(files
)->max_fds
;
1981 fd
++, filp
->f_pos
++) {
1982 char name
[PROC_NUMBUF
];
1985 if (!fcheck_files(files
, fd
))
1989 len
= snprintf(name
, sizeof(name
), "%d", fd
);
1990 if (proc_fill_cache(filp
, dirent
, filldir
,
1991 name
, len
, instantiate
,
1999 put_files_struct(files
);
2007 static struct dentry
*proc_lookupfd(struct inode
*dir
, struct dentry
*dentry
,
2008 struct nameidata
*nd
)
2010 return proc_lookupfd_common(dir
, dentry
, proc_fd_instantiate
);
2013 static int proc_readfd(struct file
*filp
, void *dirent
, filldir_t filldir
)
2015 return proc_readfd_common(filp
, dirent
, filldir
, proc_fd_instantiate
);
2018 static ssize_t
proc_fdinfo_read(struct file
*file
, char __user
*buf
,
2019 size_t len
, loff_t
*ppos
)
2021 char tmp
[PROC_FDINFO_MAX
];
2022 int err
= proc_fd_info(file
->f_path
.dentry
->d_inode
, NULL
, tmp
);
2024 err
= simple_read_from_buffer(buf
, len
, ppos
, tmp
, strlen(tmp
));
2028 static const struct file_operations proc_fdinfo_file_operations
= {
2029 .open
= nonseekable_open
,
2030 .read
= proc_fdinfo_read
,
2031 .llseek
= no_llseek
,
2034 static const struct file_operations proc_fd_operations
= {
2035 .read
= generic_read_dir
,
2036 .readdir
= proc_readfd
,
2037 .llseek
= default_llseek
,
2040 #ifdef CONFIG_CHECKPOINT_RESTORE
2043 * dname_to_vma_addr - maps a dentry name into two unsigned longs
2044 * which represent vma start and end addresses.
2046 static int dname_to_vma_addr(struct dentry
*dentry
,
2047 unsigned long *start
, unsigned long *end
)
2049 if (sscanf(dentry
->d_name
.name
, "%lx-%lx", start
, end
) != 2)
2055 static int map_files_d_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
2057 unsigned long vm_start
, vm_end
;
2058 bool exact_vma_exists
= false;
2059 struct mm_struct
*mm
= NULL
;
2060 struct task_struct
*task
;
2061 const struct cred
*cred
;
2062 struct inode
*inode
;
2065 if (nd
&& nd
->flags
& LOOKUP_RCU
)
2068 if (!capable(CAP_SYS_ADMIN
)) {
2073 inode
= dentry
->d_inode
;
2074 task
= get_proc_task(inode
);
2078 if (!ptrace_may_access(task
, PTRACE_MODE_READ
))
2081 mm
= get_task_mm(task
);
2085 if (!dname_to_vma_addr(dentry
, &vm_start
, &vm_end
)) {
2086 down_read(&mm
->mmap_sem
);
2087 exact_vma_exists
= !!find_exact_vma(mm
, vm_start
, vm_end
);
2088 up_read(&mm
->mmap_sem
);
2093 if (exact_vma_exists
) {
2094 if (task_dumpable(task
)) {
2096 cred
= __task_cred(task
);
2097 inode
->i_uid
= cred
->euid
;
2098 inode
->i_gid
= cred
->egid
;
2104 security_task_to_inode(task
, inode
);
2109 put_task_struct(task
);
2118 static const struct dentry_operations tid_map_files_dentry_operations
= {
2119 .d_revalidate
= map_files_d_revalidate
,
2120 .d_delete
= pid_delete_dentry
,
2123 static int proc_map_files_get_link(struct dentry
*dentry
, struct path
*path
)
2125 unsigned long vm_start
, vm_end
;
2126 struct vm_area_struct
*vma
;
2127 struct task_struct
*task
;
2128 struct mm_struct
*mm
;
2132 task
= get_proc_task(dentry
->d_inode
);
2136 mm
= get_task_mm(task
);
2137 put_task_struct(task
);
2141 rc
= dname_to_vma_addr(dentry
, &vm_start
, &vm_end
);
2145 down_read(&mm
->mmap_sem
);
2146 vma
= find_exact_vma(mm
, vm_start
, vm_end
);
2147 if (vma
&& vma
->vm_file
) {
2148 *path
= vma
->vm_file
->f_path
;
2152 up_read(&mm
->mmap_sem
);
2160 struct map_files_info
{
2163 unsigned char name
[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
2166 static struct dentry
*
2167 proc_map_files_instantiate(struct inode
*dir
, struct dentry
*dentry
,
2168 struct task_struct
*task
, const void *ptr
)
2170 const struct file
*file
= ptr
;
2171 struct proc_inode
*ei
;
2172 struct inode
*inode
;
2175 return ERR_PTR(-ENOENT
);
2177 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
2179 return ERR_PTR(-ENOENT
);
2182 ei
->op
.proc_get_link
= proc_map_files_get_link
;
2184 inode
->i_op
= &proc_pid_link_inode_operations
;
2186 inode
->i_mode
= S_IFLNK
;
2188 if (file
->f_mode
& FMODE_READ
)
2189 inode
->i_mode
|= S_IRUSR
;
2190 if (file
->f_mode
& FMODE_WRITE
)
2191 inode
->i_mode
|= S_IWUSR
;
2193 d_set_d_op(dentry
, &tid_map_files_dentry_operations
);
2194 d_add(dentry
, inode
);
2199 static struct dentry
*proc_map_files_lookup(struct inode
*dir
,
2200 struct dentry
*dentry
, struct nameidata
*nd
)
2202 unsigned long vm_start
, vm_end
;
2203 struct vm_area_struct
*vma
;
2204 struct task_struct
*task
;
2205 struct dentry
*result
;
2206 struct mm_struct
*mm
;
2208 result
= ERR_PTR(-EACCES
);
2209 if (!capable(CAP_SYS_ADMIN
))
2212 result
= ERR_PTR(-ENOENT
);
2213 task
= get_proc_task(dir
);
2217 result
= ERR_PTR(-EACCES
);
2218 if (lock_trace(task
))
2221 result
= ERR_PTR(-ENOENT
);
2222 if (dname_to_vma_addr(dentry
, &vm_start
, &vm_end
))
2225 mm
= get_task_mm(task
);
2229 down_read(&mm
->mmap_sem
);
2230 vma
= find_exact_vma(mm
, vm_start
, vm_end
);
2234 result
= proc_map_files_instantiate(dir
, dentry
, task
, vma
->vm_file
);
2237 up_read(&mm
->mmap_sem
);
2242 put_task_struct(task
);
2247 static const struct inode_operations proc_map_files_inode_operations
= {
2248 .lookup
= proc_map_files_lookup
,
2249 .permission
= proc_fd_permission
,
2250 .setattr
= proc_setattr
,
2254 proc_map_files_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
2256 struct dentry
*dentry
= filp
->f_path
.dentry
;
2257 struct inode
*inode
= dentry
->d_inode
;
2258 struct vm_area_struct
*vma
;
2259 struct task_struct
*task
;
2260 struct mm_struct
*mm
;
2265 if (!capable(CAP_SYS_ADMIN
))
2269 task
= get_proc_task(inode
);
2274 if (lock_trace(task
))
2278 switch (filp
->f_pos
) {
2281 if (filldir(dirent
, ".", 1, 0, ino
, DT_DIR
) < 0)
2285 ino
= parent_ino(dentry
);
2286 if (filldir(dirent
, "..", 2, 1, ino
, DT_DIR
) < 0)
2291 unsigned long nr_files
, pos
, i
;
2292 struct flex_array
*fa
= NULL
;
2293 struct map_files_info info
;
2294 struct map_files_info
*p
;
2296 mm
= get_task_mm(task
);
2299 down_read(&mm
->mmap_sem
);
2304 * We need two passes here:
2306 * 1) Collect vmas of mapped files with mmap_sem taken
2307 * 2) Release mmap_sem and instantiate entries
2309 * otherwise we get lockdep complained, since filldir()
2310 * routine might require mmap_sem taken in might_fault().
2313 for (vma
= mm
->mmap
, pos
= 2; vma
; vma
= vma
->vm_next
) {
2314 if (vma
->vm_file
&& ++pos
> filp
->f_pos
)
2319 fa
= flex_array_alloc(sizeof(info
), nr_files
,
2321 if (!fa
|| flex_array_prealloc(fa
, 0, nr_files
,
2325 flex_array_free(fa
);
2326 up_read(&mm
->mmap_sem
);
2330 for (i
= 0, vma
= mm
->mmap
, pos
= 2; vma
;
2331 vma
= vma
->vm_next
) {
2334 if (++pos
<= filp
->f_pos
)
2337 get_file(vma
->vm_file
);
2338 info
.file
= vma
->vm_file
;
2339 info
.len
= snprintf(info
.name
,
2340 sizeof(info
.name
), "%lx-%lx",
2341 vma
->vm_start
, vma
->vm_end
);
2342 if (flex_array_put(fa
, i
++, &info
, GFP_KERNEL
))
2346 up_read(&mm
->mmap_sem
);
2348 for (i
= 0; i
< nr_files
; i
++) {
2349 p
= flex_array_get(fa
, i
);
2350 ret
= proc_fill_cache(filp
, dirent
, filldir
,
2352 proc_map_files_instantiate
,
2359 for (; i
< nr_files
; i
++) {
2361 * In case of error don't forget
2362 * to put rest of file refs.
2364 p
= flex_array_get(fa
, i
);
2368 flex_array_free(fa
);
2376 put_task_struct(task
);
2381 static const struct file_operations proc_map_files_operations
= {
2382 .read
= generic_read_dir
,
2383 .readdir
= proc_map_files_readdir
,
2384 .llseek
= default_llseek
,
2387 #endif /* CONFIG_CHECKPOINT_RESTORE */
2390 * /proc/pid/fd needs a special permission handler so that a process can still
2391 * access /proc/self/fd after it has executed a setuid().
2393 static int proc_fd_permission(struct inode
*inode
, int mask
)
2395 int rv
= generic_permission(inode
, mask
);
2398 if (task_pid(current
) == proc_pid(inode
))
2404 * proc directories can do almost nothing..
2406 static const struct inode_operations proc_fd_inode_operations
= {
2407 .lookup
= proc_lookupfd
,
2408 .permission
= proc_fd_permission
,
2409 .setattr
= proc_setattr
,
2412 static struct dentry
*proc_fdinfo_instantiate(struct inode
*dir
,
2413 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
2415 unsigned fd
= *(unsigned *)ptr
;
2416 struct inode
*inode
;
2417 struct proc_inode
*ei
;
2418 struct dentry
*error
= ERR_PTR(-ENOENT
);
2420 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
2425 inode
->i_mode
= S_IFREG
| S_IRUSR
;
2426 inode
->i_fop
= &proc_fdinfo_file_operations
;
2427 d_set_d_op(dentry
, &tid_fd_dentry_operations
);
2428 d_add(dentry
, inode
);
2429 /* Close the race of the process dying before we return the dentry */
2430 if (tid_fd_revalidate(dentry
, NULL
))
2437 static struct dentry
*proc_lookupfdinfo(struct inode
*dir
,
2438 struct dentry
*dentry
,
2439 struct nameidata
*nd
)
2441 return proc_lookupfd_common(dir
, dentry
, proc_fdinfo_instantiate
);
2444 static int proc_readfdinfo(struct file
*filp
, void *dirent
, filldir_t filldir
)
2446 return proc_readfd_common(filp
, dirent
, filldir
,
2447 proc_fdinfo_instantiate
);
2450 static const struct file_operations proc_fdinfo_operations
= {
2451 .read
= generic_read_dir
,
2452 .readdir
= proc_readfdinfo
,
2453 .llseek
= default_llseek
,
2457 * proc directories can do almost nothing..
2459 static const struct inode_operations proc_fdinfo_inode_operations
= {
2460 .lookup
= proc_lookupfdinfo
,
2461 .setattr
= proc_setattr
,
2465 static struct dentry
*proc_pident_instantiate(struct inode
*dir
,
2466 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
2468 const struct pid_entry
*p
= ptr
;
2469 struct inode
*inode
;
2470 struct proc_inode
*ei
;
2471 struct dentry
*error
= ERR_PTR(-ENOENT
);
2473 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
2478 inode
->i_mode
= p
->mode
;
2479 if (S_ISDIR(inode
->i_mode
))
2480 set_nlink(inode
, 2); /* Use getattr to fix if necessary */
2482 inode
->i_op
= p
->iop
;
2484 inode
->i_fop
= p
->fop
;
2486 d_set_d_op(dentry
, &pid_dentry_operations
);
2487 d_add(dentry
, inode
);
2488 /* Close the race of the process dying before we return the dentry */
2489 if (pid_revalidate(dentry
, NULL
))
2495 static struct dentry
*proc_pident_lookup(struct inode
*dir
,
2496 struct dentry
*dentry
,
2497 const struct pid_entry
*ents
,
2500 struct dentry
*error
;
2501 struct task_struct
*task
= get_proc_task(dir
);
2502 const struct pid_entry
*p
, *last
;
2504 error
= ERR_PTR(-ENOENT
);
2510 * Yes, it does not scale. And it should not. Don't add
2511 * new entries into /proc/<tgid>/ without very good reasons.
2513 last
= &ents
[nents
- 1];
2514 for (p
= ents
; p
<= last
; p
++) {
2515 if (p
->len
!= dentry
->d_name
.len
)
2517 if (!memcmp(dentry
->d_name
.name
, p
->name
, p
->len
))
2523 error
= proc_pident_instantiate(dir
, dentry
, task
, p
);
2525 put_task_struct(task
);
2530 static int proc_pident_fill_cache(struct file
*filp
, void *dirent
,
2531 filldir_t filldir
, struct task_struct
*task
, const struct pid_entry
*p
)
2533 return proc_fill_cache(filp
, dirent
, filldir
, p
->name
, p
->len
,
2534 proc_pident_instantiate
, task
, p
);
2537 static int proc_pident_readdir(struct file
*filp
,
2538 void *dirent
, filldir_t filldir
,
2539 const struct pid_entry
*ents
, unsigned int nents
)
2542 struct dentry
*dentry
= filp
->f_path
.dentry
;
2543 struct inode
*inode
= dentry
->d_inode
;
2544 struct task_struct
*task
= get_proc_task(inode
);
2545 const struct pid_entry
*p
, *last
;
2558 if (filldir(dirent
, ".", 1, i
, ino
, DT_DIR
) < 0)
2564 ino
= parent_ino(dentry
);
2565 if (filldir(dirent
, "..", 2, i
, ino
, DT_DIR
) < 0)
2577 last
= &ents
[nents
- 1];
2579 if (proc_pident_fill_cache(filp
, dirent
, filldir
, task
, p
) < 0)
2588 put_task_struct(task
);
2593 #ifdef CONFIG_SECURITY
2594 static ssize_t
proc_pid_attr_read(struct file
* file
, char __user
* buf
,
2595 size_t count
, loff_t
*ppos
)
2597 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
2600 struct task_struct
*task
= get_proc_task(inode
);
2605 length
= security_getprocattr(task
,
2606 (char*)file
->f_path
.dentry
->d_name
.name
,
2608 put_task_struct(task
);
2610 length
= simple_read_from_buffer(buf
, count
, ppos
, p
, length
);
2615 static ssize_t
proc_pid_attr_write(struct file
* file
, const char __user
* buf
,
2616 size_t count
, loff_t
*ppos
)
2618 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
2621 struct task_struct
*task
= get_proc_task(inode
);
2626 if (count
> PAGE_SIZE
)
2629 /* No partial writes. */
2635 page
= (char*)__get_free_page(GFP_TEMPORARY
);
2640 if (copy_from_user(page
, buf
, count
))
2643 /* Guard against adverse ptrace interaction */
2644 length
= mutex_lock_interruptible(&task
->signal
->cred_guard_mutex
);
2648 length
= security_setprocattr(task
,
2649 (char*)file
->f_path
.dentry
->d_name
.name
,
2650 (void*)page
, count
);
2651 mutex_unlock(&task
->signal
->cred_guard_mutex
);
2653 free_page((unsigned long) page
);
2655 put_task_struct(task
);
2660 static const struct file_operations proc_pid_attr_operations
= {
2661 .read
= proc_pid_attr_read
,
2662 .write
= proc_pid_attr_write
,
2663 .llseek
= generic_file_llseek
,
2666 static const struct pid_entry attr_dir_stuff
[] = {
2667 REG("current", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2668 REG("prev", S_IRUGO
, proc_pid_attr_operations
),
2669 REG("exec", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2670 REG("fscreate", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2671 REG("keycreate", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2672 REG("sockcreate", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2675 static int proc_attr_dir_readdir(struct file
* filp
,
2676 void * dirent
, filldir_t filldir
)
2678 return proc_pident_readdir(filp
,dirent
,filldir
,
2679 attr_dir_stuff
,ARRAY_SIZE(attr_dir_stuff
));
2682 static const struct file_operations proc_attr_dir_operations
= {
2683 .read
= generic_read_dir
,
2684 .readdir
= proc_attr_dir_readdir
,
2685 .llseek
= default_llseek
,
2688 static struct dentry
*proc_attr_dir_lookup(struct inode
*dir
,
2689 struct dentry
*dentry
, struct nameidata
*nd
)
2691 return proc_pident_lookup(dir
, dentry
,
2692 attr_dir_stuff
, ARRAY_SIZE(attr_dir_stuff
));
2695 static const struct inode_operations proc_attr_dir_inode_operations
= {
2696 .lookup
= proc_attr_dir_lookup
,
2697 .getattr
= pid_getattr
,
2698 .setattr
= proc_setattr
,
2703 #ifdef CONFIG_ELF_CORE
2704 static ssize_t
proc_coredump_filter_read(struct file
*file
, char __user
*buf
,
2705 size_t count
, loff_t
*ppos
)
2707 struct task_struct
*task
= get_proc_task(file
->f_dentry
->d_inode
);
2708 struct mm_struct
*mm
;
2709 char buffer
[PROC_NUMBUF
];
2717 mm
= get_task_mm(task
);
2719 len
= snprintf(buffer
, sizeof(buffer
), "%08lx\n",
2720 ((mm
->flags
& MMF_DUMP_FILTER_MASK
) >>
2721 MMF_DUMP_FILTER_SHIFT
));
2723 ret
= simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
2726 put_task_struct(task
);
2731 static ssize_t
proc_coredump_filter_write(struct file
*file
,
2732 const char __user
*buf
,
2736 struct task_struct
*task
;
2737 struct mm_struct
*mm
;
2738 char buffer
[PROC_NUMBUF
], *end
;
2745 memset(buffer
, 0, sizeof(buffer
));
2746 if (count
> sizeof(buffer
) - 1)
2747 count
= sizeof(buffer
) - 1;
2748 if (copy_from_user(buffer
, buf
, count
))
2752 val
= (unsigned int)simple_strtoul(buffer
, &end
, 0);
2755 if (end
- buffer
== 0)
2759 task
= get_proc_task(file
->f_dentry
->d_inode
);
2764 mm
= get_task_mm(task
);
2768 for (i
= 0, mask
= 1; i
< MMF_DUMP_FILTER_BITS
; i
++, mask
<<= 1) {
2770 set_bit(i
+ MMF_DUMP_FILTER_SHIFT
, &mm
->flags
);
2772 clear_bit(i
+ MMF_DUMP_FILTER_SHIFT
, &mm
->flags
);
2777 put_task_struct(task
);
2782 static const struct file_operations proc_coredump_filter_operations
= {
2783 .read
= proc_coredump_filter_read
,
2784 .write
= proc_coredump_filter_write
,
2785 .llseek
= generic_file_llseek
,
2792 static int proc_self_readlink(struct dentry
*dentry
, char __user
*buffer
,
2795 struct pid_namespace
*ns
= dentry
->d_sb
->s_fs_info
;
2796 pid_t tgid
= task_tgid_nr_ns(current
, ns
);
2797 char tmp
[PROC_NUMBUF
];
2800 sprintf(tmp
, "%d", tgid
);
2801 return vfs_readlink(dentry
,buffer
,buflen
,tmp
);
2804 static void *proc_self_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
2806 struct pid_namespace
*ns
= dentry
->d_sb
->s_fs_info
;
2807 pid_t tgid
= task_tgid_nr_ns(current
, ns
);
2808 char *name
= ERR_PTR(-ENOENT
);
2812 name
= ERR_PTR(-ENOMEM
);
2814 sprintf(name
, "%d", tgid
);
2816 nd_set_link(nd
, name
);
2820 static void proc_self_put_link(struct dentry
*dentry
, struct nameidata
*nd
,
2823 char *s
= nd_get_link(nd
);
2828 static const struct inode_operations proc_self_inode_operations
= {
2829 .readlink
= proc_self_readlink
,
2830 .follow_link
= proc_self_follow_link
,
2831 .put_link
= proc_self_put_link
,
2837 * These are the directory entries in the root directory of /proc
2838 * that properly belong to the /proc filesystem, as they describe
2839 * describe something that is process related.
2841 static const struct pid_entry proc_base_stuff
[] = {
2842 NOD("self", S_IFLNK
|S_IRWXUGO
,
2843 &proc_self_inode_operations
, NULL
, {}),
2846 static struct dentry
*proc_base_instantiate(struct inode
*dir
,
2847 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
2849 const struct pid_entry
*p
= ptr
;
2850 struct inode
*inode
;
2851 struct proc_inode
*ei
;
2852 struct dentry
*error
;
2854 /* Allocate the inode */
2855 error
= ERR_PTR(-ENOMEM
);
2856 inode
= new_inode(dir
->i_sb
);
2860 /* Initialize the inode */
2862 inode
->i_ino
= get_next_ino();
2863 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
2866 * grab the reference to the task.
2868 ei
->pid
= get_task_pid(task
, PIDTYPE_PID
);
2872 inode
->i_mode
= p
->mode
;
2873 if (S_ISDIR(inode
->i_mode
))
2874 set_nlink(inode
, 2);
2875 if (S_ISLNK(inode
->i_mode
))
2878 inode
->i_op
= p
->iop
;
2880 inode
->i_fop
= p
->fop
;
2882 d_add(dentry
, inode
);
2891 static struct dentry
*proc_base_lookup(struct inode
*dir
, struct dentry
*dentry
)
2893 struct dentry
*error
;
2894 struct task_struct
*task
= get_proc_task(dir
);
2895 const struct pid_entry
*p
, *last
;
2897 error
= ERR_PTR(-ENOENT
);
2902 /* Lookup the directory entry */
2903 last
= &proc_base_stuff
[ARRAY_SIZE(proc_base_stuff
) - 1];
2904 for (p
= proc_base_stuff
; p
<= last
; p
++) {
2905 if (p
->len
!= dentry
->d_name
.len
)
2907 if (!memcmp(dentry
->d_name
.name
, p
->name
, p
->len
))
2913 error
= proc_base_instantiate(dir
, dentry
, task
, p
);
2916 put_task_struct(task
);
2921 static int proc_base_fill_cache(struct file
*filp
, void *dirent
,
2922 filldir_t filldir
, struct task_struct
*task
, const struct pid_entry
*p
)
2924 return proc_fill_cache(filp
, dirent
, filldir
, p
->name
, p
->len
,
2925 proc_base_instantiate
, task
, p
);
2928 #ifdef CONFIG_TASK_IO_ACCOUNTING
2929 static int do_io_accounting(struct task_struct
*task
, char *buffer
, int whole
)
2931 struct task_io_accounting acct
= task
->ioac
;
2932 unsigned long flags
;
2935 result
= mutex_lock_killable(&task
->signal
->cred_guard_mutex
);
2939 if (!ptrace_may_access(task
, PTRACE_MODE_READ
)) {
2944 if (whole
&& lock_task_sighand(task
, &flags
)) {
2945 struct task_struct
*t
= task
;
2947 task_io_accounting_add(&acct
, &task
->signal
->ioac
);
2948 while_each_thread(task
, t
)
2949 task_io_accounting_add(&acct
, &t
->ioac
);
2951 unlock_task_sighand(task
, &flags
);
2953 result
= sprintf(buffer
,
2958 "read_bytes: %llu\n"
2959 "write_bytes: %llu\n"
2960 "cancelled_write_bytes: %llu\n",
2961 (unsigned long long)acct
.rchar
,
2962 (unsigned long long)acct
.wchar
,
2963 (unsigned long long)acct
.syscr
,
2964 (unsigned long long)acct
.syscw
,
2965 (unsigned long long)acct
.read_bytes
,
2966 (unsigned long long)acct
.write_bytes
,
2967 (unsigned long long)acct
.cancelled_write_bytes
);
2969 mutex_unlock(&task
->signal
->cred_guard_mutex
);
2973 static int proc_tid_io_accounting(struct task_struct
*task
, char *buffer
)
2975 return do_io_accounting(task
, buffer
, 0);
2978 static int proc_tgid_io_accounting(struct task_struct
*task
, char *buffer
)
2980 return do_io_accounting(task
, buffer
, 1);
2982 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2984 static int proc_pid_personality(struct seq_file
*m
, struct pid_namespace
*ns
,
2985 struct pid
*pid
, struct task_struct
*task
)
2987 int err
= lock_trace(task
);
2989 seq_printf(m
, "%08x\n", task
->personality
);
2998 static const struct file_operations proc_task_operations
;
2999 static const struct inode_operations proc_task_inode_operations
;
3001 static const struct pid_entry tgid_base_stuff
[] = {
3002 DIR("task", S_IRUGO
|S_IXUGO
, proc_task_inode_operations
, proc_task_operations
),
3003 DIR("fd", S_IRUSR
|S_IXUSR
, proc_fd_inode_operations
, proc_fd_operations
),
3004 #ifdef CONFIG_CHECKPOINT_RESTORE
3005 DIR("map_files", S_IRUSR
|S_IXUSR
, proc_map_files_inode_operations
, proc_map_files_operations
),
3007 DIR("fdinfo", S_IRUSR
|S_IXUSR
, proc_fdinfo_inode_operations
, proc_fdinfo_operations
),
3008 DIR("ns", S_IRUSR
|S_IXUGO
, proc_ns_dir_inode_operations
, proc_ns_dir_operations
),
3010 DIR("net", S_IRUGO
|S_IXUGO
, proc_net_inode_operations
, proc_net_operations
),
3012 REG("environ", S_IRUSR
, proc_environ_operations
),
3013 INF("auxv", S_IRUSR
, proc_pid_auxv
),
3014 ONE("status", S_IRUGO
, proc_pid_status
),
3015 ONE("personality", S_IRUGO
, proc_pid_personality
),
3016 INF("limits", S_IRUGO
, proc_pid_limits
),
3017 #ifdef CONFIG_SCHED_DEBUG
3018 REG("sched", S_IRUGO
|S_IWUSR
, proc_pid_sched_operations
),
3020 #ifdef CONFIG_SCHED_AUTOGROUP
3021 REG("autogroup", S_IRUGO
|S_IWUSR
, proc_pid_sched_autogroup_operations
),
3023 REG("comm", S_IRUGO
|S_IWUSR
, proc_pid_set_comm_operations
),
3024 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3025 INF("syscall", S_IRUGO
, proc_pid_syscall
),
3027 INF("cmdline", S_IRUGO
, proc_pid_cmdline
),
3028 ONE("stat", S_IRUGO
, proc_tgid_stat
),
3029 ONE("statm", S_IRUGO
, proc_pid_statm
),
3030 REG("maps", S_IRUGO
, proc_maps_operations
),
3032 REG("numa_maps", S_IRUGO
, proc_numa_maps_operations
),
3034 REG("mem", S_IRUSR
|S_IWUSR
, proc_mem_operations
),
3035 LNK("cwd", proc_cwd_link
),
3036 LNK("root", proc_root_link
),
3037 LNK("exe", proc_exe_link
),
3038 REG("mounts", S_IRUGO
, proc_mounts_operations
),
3039 REG("mountinfo", S_IRUGO
, proc_mountinfo_operations
),
3040 REG("mountstats", S_IRUSR
, proc_mountstats_operations
),
3041 #ifdef CONFIG_PROC_PAGE_MONITOR
3042 REG("clear_refs", S_IWUSR
, proc_clear_refs_operations
),
3043 REG("smaps", S_IRUGO
, proc_smaps_operations
),
3044 REG("pagemap", S_IRUGO
, proc_pagemap_operations
),
3046 #ifdef CONFIG_SECURITY
3047 DIR("attr", S_IRUGO
|S_IXUGO
, proc_attr_dir_inode_operations
, proc_attr_dir_operations
),
3049 #ifdef CONFIG_KALLSYMS
3050 INF("wchan", S_IRUGO
, proc_pid_wchan
),
3052 #ifdef CONFIG_STACKTRACE
3053 ONE("stack", S_IRUGO
, proc_pid_stack
),
3055 #ifdef CONFIG_SCHEDSTATS
3056 INF("schedstat", S_IRUGO
, proc_pid_schedstat
),
3058 #ifdef CONFIG_LATENCYTOP
3059 REG("latency", S_IRUGO
, proc_lstats_operations
),
3061 #ifdef CONFIG_PROC_PID_CPUSET
3062 REG("cpuset", S_IRUGO
, proc_cpuset_operations
),
3064 #ifdef CONFIG_CGROUPS
3065 REG("cgroup", S_IRUGO
, proc_cgroup_operations
),
3067 INF("oom_score", S_IRUGO
, proc_oom_score
),
3068 REG("oom_adj", S_IRUGO
|S_IWUSR
, proc_oom_adjust_operations
),
3069 REG("oom_score_adj", S_IRUGO
|S_IWUSR
, proc_oom_score_adj_operations
),
3070 #ifdef CONFIG_AUDITSYSCALL
3071 REG("loginuid", S_IWUSR
|S_IRUGO
, proc_loginuid_operations
),
3072 REG("sessionid", S_IRUGO
, proc_sessionid_operations
),
3074 #ifdef CONFIG_FAULT_INJECTION
3075 REG("make-it-fail", S_IRUGO
|S_IWUSR
, proc_fault_inject_operations
),
3077 #ifdef CONFIG_ELF_CORE
3078 REG("coredump_filter", S_IRUGO
|S_IWUSR
, proc_coredump_filter_operations
),
3080 #ifdef CONFIG_TASK_IO_ACCOUNTING
3081 INF("io", S_IRUSR
, proc_tgid_io_accounting
),
3083 #ifdef CONFIG_HARDWALL
3084 INF("hardwall", S_IRUGO
, proc_pid_hardwall
),
3088 static int proc_tgid_base_readdir(struct file
* filp
,
3089 void * dirent
, filldir_t filldir
)
3091 return proc_pident_readdir(filp
,dirent
,filldir
,
3092 tgid_base_stuff
,ARRAY_SIZE(tgid_base_stuff
));
3095 static const struct file_operations proc_tgid_base_operations
= {
3096 .read
= generic_read_dir
,
3097 .readdir
= proc_tgid_base_readdir
,
3098 .llseek
= default_llseek
,
3101 static struct dentry
*proc_tgid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
){
3102 return proc_pident_lookup(dir
, dentry
,
3103 tgid_base_stuff
, ARRAY_SIZE(tgid_base_stuff
));
3106 static const struct inode_operations proc_tgid_base_inode_operations
= {
3107 .lookup
= proc_tgid_base_lookup
,
3108 .getattr
= pid_getattr
,
3109 .setattr
= proc_setattr
,
3110 .permission
= proc_pid_permission
,
3113 static void proc_flush_task_mnt(struct vfsmount
*mnt
, pid_t pid
, pid_t tgid
)
3115 struct dentry
*dentry
, *leader
, *dir
;
3116 char buf
[PROC_NUMBUF
];
3120 name
.len
= snprintf(buf
, sizeof(buf
), "%d", pid
);
3121 dentry
= d_hash_and_lookup(mnt
->mnt_root
, &name
);
3123 shrink_dcache_parent(dentry
);
3129 name
.len
= snprintf(buf
, sizeof(buf
), "%d", tgid
);
3130 leader
= d_hash_and_lookup(mnt
->mnt_root
, &name
);
3135 name
.len
= strlen(name
.name
);
3136 dir
= d_hash_and_lookup(leader
, &name
);
3138 goto out_put_leader
;
3141 name
.len
= snprintf(buf
, sizeof(buf
), "%d", pid
);
3142 dentry
= d_hash_and_lookup(dir
, &name
);
3144 shrink_dcache_parent(dentry
);
3157 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
3158 * @task: task that should be flushed.
3160 * When flushing dentries from proc, one needs to flush them from global
3161 * proc (proc_mnt) and from all the namespaces' procs this task was seen
3162 * in. This call is supposed to do all of this job.
3164 * Looks in the dcache for
3166 * /proc/@tgid/task/@pid
3167 * if either directory is present flushes it and all of it'ts children
3170 * It is safe and reasonable to cache /proc entries for a task until
3171 * that task exits. After that they just clog up the dcache with
3172 * useless entries, possibly causing useful dcache entries to be
3173 * flushed instead. This routine is proved to flush those useless
3174 * dcache entries at process exit time.
3176 * NOTE: This routine is just an optimization so it does not guarantee
3177 * that no dcache entries will exist at process exit time it
3178 * just makes it very unlikely that any will persist.
3181 void proc_flush_task(struct task_struct
*task
)
3184 struct pid
*pid
, *tgid
;
3187 pid
= task_pid(task
);
3188 tgid
= task_tgid(task
);
3190 for (i
= 0; i
<= pid
->level
; i
++) {
3191 upid
= &pid
->numbers
[i
];
3192 proc_flush_task_mnt(upid
->ns
->proc_mnt
, upid
->nr
,
3193 tgid
->numbers
[i
].nr
);
3196 upid
= &pid
->numbers
[pid
->level
];
3198 pid_ns_release_proc(upid
->ns
);
3201 static struct dentry
*proc_pid_instantiate(struct inode
*dir
,
3202 struct dentry
* dentry
,
3203 struct task_struct
*task
, const void *ptr
)
3205 struct dentry
*error
= ERR_PTR(-ENOENT
);
3206 struct inode
*inode
;
3208 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
3212 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
3213 inode
->i_op
= &proc_tgid_base_inode_operations
;
3214 inode
->i_fop
= &proc_tgid_base_operations
;
3215 inode
->i_flags
|=S_IMMUTABLE
;
3217 set_nlink(inode
, 2 + pid_entry_count_dirs(tgid_base_stuff
,
3218 ARRAY_SIZE(tgid_base_stuff
)));
3220 d_set_d_op(dentry
, &pid_dentry_operations
);
3222 d_add(dentry
, inode
);
3223 /* Close the race of the process dying before we return the dentry */
3224 if (pid_revalidate(dentry
, NULL
))
3230 struct dentry
*proc_pid_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
)
3232 struct dentry
*result
;
3233 struct task_struct
*task
;
3235 struct pid_namespace
*ns
;
3237 result
= proc_base_lookup(dir
, dentry
);
3238 if (!IS_ERR(result
) || PTR_ERR(result
) != -ENOENT
)
3241 tgid
= name_to_int(dentry
);
3245 ns
= dentry
->d_sb
->s_fs_info
;
3247 task
= find_task_by_pid_ns(tgid
, ns
);
3249 get_task_struct(task
);
3254 result
= proc_pid_instantiate(dir
, dentry
, task
, NULL
);
3255 put_task_struct(task
);
3261 * Find the first task with tgid >= tgid
3266 struct task_struct
*task
;
3268 static struct tgid_iter
next_tgid(struct pid_namespace
*ns
, struct tgid_iter iter
)
3273 put_task_struct(iter
.task
);
3277 pid
= find_ge_pid(iter
.tgid
, ns
);
3279 iter
.tgid
= pid_nr_ns(pid
, ns
);
3280 iter
.task
= pid_task(pid
, PIDTYPE_PID
);
3281 /* What we to know is if the pid we have find is the
3282 * pid of a thread_group_leader. Testing for task
3283 * being a thread_group_leader is the obvious thing
3284 * todo but there is a window when it fails, due to
3285 * the pid transfer logic in de_thread.
3287 * So we perform the straight forward test of seeing
3288 * if the pid we have found is the pid of a thread
3289 * group leader, and don't worry if the task we have
3290 * found doesn't happen to be a thread group leader.
3291 * As we don't care in the case of readdir.
3293 if (!iter
.task
|| !has_group_leader_pid(iter
.task
)) {
3297 get_task_struct(iter
.task
);
3303 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
3305 static int proc_pid_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
3306 struct tgid_iter iter
)
3308 char name
[PROC_NUMBUF
];
3309 int len
= snprintf(name
, sizeof(name
), "%d", iter
.tgid
);
3310 return proc_fill_cache(filp
, dirent
, filldir
, name
, len
,
3311 proc_pid_instantiate
, iter
.task
, NULL
);
3314 static int fake_filldir(void *buf
, const char *name
, int namelen
,
3315 loff_t offset
, u64 ino
, unsigned d_type
)
3320 /* for the /proc/ directory itself, after non-process stuff has been done */
3321 int proc_pid_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
3324 struct task_struct
*reaper
;
3325 struct tgid_iter iter
;
3326 struct pid_namespace
*ns
;
3327 filldir_t __filldir
;
3329 if (filp
->f_pos
>= PID_MAX_LIMIT
+ TGID_OFFSET
)
3331 nr
= filp
->f_pos
- FIRST_PROCESS_ENTRY
;
3333 reaper
= get_proc_task(filp
->f_path
.dentry
->d_inode
);
3337 for (; nr
< ARRAY_SIZE(proc_base_stuff
); filp
->f_pos
++, nr
++) {
3338 const struct pid_entry
*p
= &proc_base_stuff
[nr
];
3339 if (proc_base_fill_cache(filp
, dirent
, filldir
, reaper
, p
) < 0)
3343 ns
= filp
->f_dentry
->d_sb
->s_fs_info
;
3345 iter
.tgid
= filp
->f_pos
- TGID_OFFSET
;
3346 for (iter
= next_tgid(ns
, iter
);
3348 iter
.tgid
+= 1, iter
= next_tgid(ns
, iter
)) {
3349 if (has_pid_permissions(ns
, iter
.task
, 2))
3350 __filldir
= filldir
;
3352 __filldir
= fake_filldir
;
3354 filp
->f_pos
= iter
.tgid
+ TGID_OFFSET
;
3355 if (proc_pid_fill_cache(filp
, dirent
, __filldir
, iter
) < 0) {
3356 put_task_struct(iter
.task
);
3360 filp
->f_pos
= PID_MAX_LIMIT
+ TGID_OFFSET
;
3362 put_task_struct(reaper
);
3370 static const struct pid_entry tid_base_stuff
[] = {
3371 DIR("fd", S_IRUSR
|S_IXUSR
, proc_fd_inode_operations
, proc_fd_operations
),
3372 DIR("fdinfo", S_IRUSR
|S_IXUSR
, proc_fdinfo_inode_operations
, proc_fdinfo_operations
),
3373 DIR("ns", S_IRUSR
|S_IXUGO
, proc_ns_dir_inode_operations
, proc_ns_dir_operations
),
3374 REG("environ", S_IRUSR
, proc_environ_operations
),
3375 INF("auxv", S_IRUSR
, proc_pid_auxv
),
3376 ONE("status", S_IRUGO
, proc_pid_status
),
3377 ONE("personality", S_IRUGO
, proc_pid_personality
),
3378 INF("limits", S_IRUGO
, proc_pid_limits
),
3379 #ifdef CONFIG_SCHED_DEBUG
3380 REG("sched", S_IRUGO
|S_IWUSR
, proc_pid_sched_operations
),
3382 REG("comm", S_IRUGO
|S_IWUSR
, proc_pid_set_comm_operations
),
3383 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3384 INF("syscall", S_IRUGO
, proc_pid_syscall
),
3386 INF("cmdline", S_IRUGO
, proc_pid_cmdline
),
3387 ONE("stat", S_IRUGO
, proc_tid_stat
),
3388 ONE("statm", S_IRUGO
, proc_pid_statm
),
3389 REG("maps", S_IRUGO
, proc_maps_operations
),
3391 REG("numa_maps", S_IRUGO
, proc_numa_maps_operations
),
3393 REG("mem", S_IRUSR
|S_IWUSR
, proc_mem_operations
),
3394 LNK("cwd", proc_cwd_link
),
3395 LNK("root", proc_root_link
),
3396 LNK("exe", proc_exe_link
),
3397 REG("mounts", S_IRUGO
, proc_mounts_operations
),
3398 REG("mountinfo", S_IRUGO
, proc_mountinfo_operations
),
3399 #ifdef CONFIG_PROC_PAGE_MONITOR
3400 REG("clear_refs", S_IWUSR
, proc_clear_refs_operations
),
3401 REG("smaps", S_IRUGO
, proc_smaps_operations
),
3402 REG("pagemap", S_IRUGO
, proc_pagemap_operations
),
3404 #ifdef CONFIG_SECURITY
3405 DIR("attr", S_IRUGO
|S_IXUGO
, proc_attr_dir_inode_operations
, proc_attr_dir_operations
),
3407 #ifdef CONFIG_KALLSYMS
3408 INF("wchan", S_IRUGO
, proc_pid_wchan
),
3410 #ifdef CONFIG_STACKTRACE
3411 ONE("stack", S_IRUGO
, proc_pid_stack
),
3413 #ifdef CONFIG_SCHEDSTATS
3414 INF("schedstat", S_IRUGO
, proc_pid_schedstat
),
3416 #ifdef CONFIG_LATENCYTOP
3417 REG("latency", S_IRUGO
, proc_lstats_operations
),
3419 #ifdef CONFIG_PROC_PID_CPUSET
3420 REG("cpuset", S_IRUGO
, proc_cpuset_operations
),
3422 #ifdef CONFIG_CGROUPS
3423 REG("cgroup", S_IRUGO
, proc_cgroup_operations
),
3425 INF("oom_score", S_IRUGO
, proc_oom_score
),
3426 REG("oom_adj", S_IRUGO
|S_IWUSR
, proc_oom_adjust_operations
),
3427 REG("oom_score_adj", S_IRUGO
|S_IWUSR
, proc_oom_score_adj_operations
),
3428 #ifdef CONFIG_AUDITSYSCALL
3429 REG("loginuid", S_IWUSR
|S_IRUGO
, proc_loginuid_operations
),
3430 REG("sessionid", S_IRUGO
, proc_sessionid_operations
),
3432 #ifdef CONFIG_FAULT_INJECTION
3433 REG("make-it-fail", S_IRUGO
|S_IWUSR
, proc_fault_inject_operations
),
3435 #ifdef CONFIG_TASK_IO_ACCOUNTING
3436 INF("io", S_IRUSR
, proc_tid_io_accounting
),
3438 #ifdef CONFIG_HARDWALL
3439 INF("hardwall", S_IRUGO
, proc_pid_hardwall
),
3443 static int proc_tid_base_readdir(struct file
* filp
,
3444 void * dirent
, filldir_t filldir
)
3446 return proc_pident_readdir(filp
,dirent
,filldir
,
3447 tid_base_stuff
,ARRAY_SIZE(tid_base_stuff
));
3450 static struct dentry
*proc_tid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
){
3451 return proc_pident_lookup(dir
, dentry
,
3452 tid_base_stuff
, ARRAY_SIZE(tid_base_stuff
));
3455 static const struct file_operations proc_tid_base_operations
= {
3456 .read
= generic_read_dir
,
3457 .readdir
= proc_tid_base_readdir
,
3458 .llseek
= default_llseek
,
3461 static const struct inode_operations proc_tid_base_inode_operations
= {
3462 .lookup
= proc_tid_base_lookup
,
3463 .getattr
= pid_getattr
,
3464 .setattr
= proc_setattr
,
3467 static struct dentry
*proc_task_instantiate(struct inode
*dir
,
3468 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
3470 struct dentry
*error
= ERR_PTR(-ENOENT
);
3471 struct inode
*inode
;
3472 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
3476 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
3477 inode
->i_op
= &proc_tid_base_inode_operations
;
3478 inode
->i_fop
= &proc_tid_base_operations
;
3479 inode
->i_flags
|=S_IMMUTABLE
;
3481 set_nlink(inode
, 2 + pid_entry_count_dirs(tid_base_stuff
,
3482 ARRAY_SIZE(tid_base_stuff
)));
3484 d_set_d_op(dentry
, &pid_dentry_operations
);
3486 d_add(dentry
, inode
);
3487 /* Close the race of the process dying before we return the dentry */
3488 if (pid_revalidate(dentry
, NULL
))
3494 static struct dentry
*proc_task_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
)
3496 struct dentry
*result
= ERR_PTR(-ENOENT
);
3497 struct task_struct
*task
;
3498 struct task_struct
*leader
= get_proc_task(dir
);
3500 struct pid_namespace
*ns
;
3505 tid
= name_to_int(dentry
);
3509 ns
= dentry
->d_sb
->s_fs_info
;
3511 task
= find_task_by_pid_ns(tid
, ns
);
3513 get_task_struct(task
);
3517 if (!same_thread_group(leader
, task
))
3520 result
= proc_task_instantiate(dir
, dentry
, task
, NULL
);
3522 put_task_struct(task
);
3524 put_task_struct(leader
);
3530 * Find the first tid of a thread group to return to user space.
3532 * Usually this is just the thread group leader, but if the users
3533 * buffer was too small or there was a seek into the middle of the
3534 * directory we have more work todo.
3536 * In the case of a short read we start with find_task_by_pid.
3538 * In the case of a seek we start with the leader and walk nr
3541 static struct task_struct
*first_tid(struct task_struct
*leader
,
3542 int tid
, int nr
, struct pid_namespace
*ns
)
3544 struct task_struct
*pos
;
3547 /* Attempt to start with the pid of a thread */
3548 if (tid
&& (nr
> 0)) {
3549 pos
= find_task_by_pid_ns(tid
, ns
);
3550 if (pos
&& (pos
->group_leader
== leader
))
3554 /* If nr exceeds the number of threads there is nothing todo */
3556 if (nr
&& nr
>= get_nr_threads(leader
))
3559 /* If we haven't found our starting place yet start
3560 * with the leader and walk nr threads forward.
3562 for (pos
= leader
; nr
> 0; --nr
) {
3563 pos
= next_thread(pos
);
3564 if (pos
== leader
) {
3570 get_task_struct(pos
);
3577 * Find the next thread in the thread list.
3578 * Return NULL if there is an error or no next thread.
3580 * The reference to the input task_struct is released.
3582 static struct task_struct
*next_tid(struct task_struct
*start
)
3584 struct task_struct
*pos
= NULL
;
3586 if (pid_alive(start
)) {
3587 pos
= next_thread(start
);
3588 if (thread_group_leader(pos
))
3591 get_task_struct(pos
);
3594 put_task_struct(start
);
3598 static int proc_task_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
3599 struct task_struct
*task
, int tid
)
3601 char name
[PROC_NUMBUF
];
3602 int len
= snprintf(name
, sizeof(name
), "%d", tid
);
3603 return proc_fill_cache(filp
, dirent
, filldir
, name
, len
,
3604 proc_task_instantiate
, task
, NULL
);
3607 /* for the /proc/TGID/task/ directories */
3608 static int proc_task_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
3610 struct dentry
*dentry
= filp
->f_path
.dentry
;
3611 struct inode
*inode
= dentry
->d_inode
;
3612 struct task_struct
*leader
= NULL
;
3613 struct task_struct
*task
;
3614 int retval
= -ENOENT
;
3617 struct pid_namespace
*ns
;
3619 task
= get_proc_task(inode
);
3623 if (pid_alive(task
)) {
3624 leader
= task
->group_leader
;
3625 get_task_struct(leader
);
3628 put_task_struct(task
);
3633 switch ((unsigned long)filp
->f_pos
) {
3636 if (filldir(dirent
, ".", 1, filp
->f_pos
, ino
, DT_DIR
) < 0)
3641 ino
= parent_ino(dentry
);
3642 if (filldir(dirent
, "..", 2, filp
->f_pos
, ino
, DT_DIR
) < 0)
3648 /* f_version caches the tgid value that the last readdir call couldn't
3649 * return. lseek aka telldir automagically resets f_version to 0.
3651 ns
= filp
->f_dentry
->d_sb
->s_fs_info
;
3652 tid
= (int)filp
->f_version
;
3653 filp
->f_version
= 0;
3654 for (task
= first_tid(leader
, tid
, filp
->f_pos
- 2, ns
);
3656 task
= next_tid(task
), filp
->f_pos
++) {
3657 tid
= task_pid_nr_ns(task
, ns
);
3658 if (proc_task_fill_cache(filp
, dirent
, filldir
, task
, tid
) < 0) {
3659 /* returning this tgid failed, save it as the first
3660 * pid for the next readir call */
3661 filp
->f_version
= (u64
)tid
;
3662 put_task_struct(task
);
3667 put_task_struct(leader
);
3672 static int proc_task_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
3674 struct inode
*inode
= dentry
->d_inode
;
3675 struct task_struct
*p
= get_proc_task(inode
);
3676 generic_fillattr(inode
, stat
);
3679 stat
->nlink
+= get_nr_threads(p
);
3686 static const struct inode_operations proc_task_inode_operations
= {
3687 .lookup
= proc_task_lookup
,
3688 .getattr
= proc_task_getattr
,
3689 .setattr
= proc_setattr
,
3690 .permission
= proc_pid_permission
,
3693 static const struct file_operations proc_task_operations
= {
3694 .read
= generic_read_dir
,
3695 .readdir
= proc_task_readdir
,
3696 .llseek
= default_llseek
,