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
*__check_mem_permission(struct task_struct
*task
)
203 struct mm_struct
*mm
;
205 mm
= get_task_mm(task
);
207 return ERR_PTR(-EINVAL
);
210 * A task can always look at itself, in case it chooses
211 * to use system calls instead of load instructions.
217 * If current is actively ptrace'ing, and would also be
218 * permitted to freshly attach with ptrace now, permit it.
220 if (task_is_stopped_or_traced(task
)) {
223 match
= (ptrace_parent(task
) == current
);
225 if (match
&& ptrace_may_access(task
, PTRACE_MODE_ATTACH
))
230 * No one else is allowed.
233 return ERR_PTR(-EPERM
);
237 * If current may access user memory in @task return a reference to the
238 * corresponding mm, otherwise ERR_PTR.
240 static struct mm_struct
*check_mem_permission(struct task_struct
*task
)
242 struct mm_struct
*mm
;
246 * Avoid racing if task exec's as we might get a new mm but validate
247 * against old credentials.
249 err
= mutex_lock_killable(&task
->signal
->cred_guard_mutex
);
253 mm
= __check_mem_permission(task
);
254 mutex_unlock(&task
->signal
->cred_guard_mutex
);
259 struct mm_struct
*mm_for_maps(struct task_struct
*task
)
261 struct mm_struct
*mm
;
264 err
= mutex_lock_killable(&task
->signal
->cred_guard_mutex
);
268 mm
= get_task_mm(task
);
269 if (mm
&& mm
!= current
->mm
&&
270 !ptrace_may_access(task
, PTRACE_MODE_READ
)) {
272 mm
= ERR_PTR(-EACCES
);
274 mutex_unlock(&task
->signal
->cred_guard_mutex
);
279 static int proc_pid_cmdline(struct task_struct
*task
, char * buffer
)
283 struct mm_struct
*mm
= get_task_mm(task
);
287 goto out_mm
; /* Shh! No looking before we're done */
289 len
= mm
->arg_end
- mm
->arg_start
;
294 res
= access_process_vm(task
, mm
->arg_start
, buffer
, len
, 0);
296 // If the nul at the end of args has been overwritten, then
297 // assume application is using setproctitle(3).
298 if (res
> 0 && buffer
[res
-1] != '\0' && len
< PAGE_SIZE
) {
299 len
= strnlen(buffer
, res
);
303 len
= mm
->env_end
- mm
->env_start
;
304 if (len
> PAGE_SIZE
- res
)
305 len
= PAGE_SIZE
- res
;
306 res
+= access_process_vm(task
, mm
->env_start
, buffer
+res
, len
, 0);
307 res
= strnlen(buffer
, res
);
316 static int proc_pid_auxv(struct task_struct
*task
, char *buffer
)
318 struct mm_struct
*mm
= mm_for_maps(task
);
319 int res
= PTR_ERR(mm
);
320 if (mm
&& !IS_ERR(mm
)) {
321 unsigned int nwords
= 0;
324 } while (mm
->saved_auxv
[nwords
- 2] != 0); /* AT_NULL */
325 res
= nwords
* sizeof(mm
->saved_auxv
[0]);
328 memcpy(buffer
, mm
->saved_auxv
, res
);
335 #ifdef CONFIG_KALLSYMS
337 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
338 * Returns the resolved symbol. If that fails, simply return the address.
340 static int proc_pid_wchan(struct task_struct
*task
, char *buffer
)
343 char symname
[KSYM_NAME_LEN
];
345 wchan
= get_wchan(task
);
347 if (lookup_symbol_name(wchan
, symname
) < 0)
348 if (!ptrace_may_access(task
, PTRACE_MODE_READ
))
351 return sprintf(buffer
, "%lu", wchan
);
353 return sprintf(buffer
, "%s", symname
);
355 #endif /* CONFIG_KALLSYMS */
357 static int lock_trace(struct task_struct
*task
)
359 int err
= mutex_lock_killable(&task
->signal
->cred_guard_mutex
);
362 if (!ptrace_may_access(task
, PTRACE_MODE_ATTACH
)) {
363 mutex_unlock(&task
->signal
->cred_guard_mutex
);
369 static void unlock_trace(struct task_struct
*task
)
371 mutex_unlock(&task
->signal
->cred_guard_mutex
);
374 #ifdef CONFIG_STACKTRACE
376 #define MAX_STACK_TRACE_DEPTH 64
378 static int proc_pid_stack(struct seq_file
*m
, struct pid_namespace
*ns
,
379 struct pid
*pid
, struct task_struct
*task
)
381 struct stack_trace trace
;
382 unsigned long *entries
;
386 entries
= kmalloc(MAX_STACK_TRACE_DEPTH
* sizeof(*entries
), GFP_KERNEL
);
390 trace
.nr_entries
= 0;
391 trace
.max_entries
= MAX_STACK_TRACE_DEPTH
;
392 trace
.entries
= entries
;
395 err
= lock_trace(task
);
397 save_stack_trace_tsk(task
, &trace
);
399 for (i
= 0; i
< trace
.nr_entries
; i
++) {
400 seq_printf(m
, "[<%pK>] %pS\n",
401 (void *)entries
[i
], (void *)entries
[i
]);
411 #ifdef CONFIG_SCHEDSTATS
413 * Provides /proc/PID/schedstat
415 static int proc_pid_schedstat(struct task_struct
*task
, char *buffer
)
417 return sprintf(buffer
, "%llu %llu %lu\n",
418 (unsigned long long)task
->se
.sum_exec_runtime
,
419 (unsigned long long)task
->sched_info
.run_delay
,
420 task
->sched_info
.pcount
);
424 #ifdef CONFIG_LATENCYTOP
425 static int lstats_show_proc(struct seq_file
*m
, void *v
)
428 struct inode
*inode
= m
->private;
429 struct task_struct
*task
= get_proc_task(inode
);
433 seq_puts(m
, "Latency Top version : v0.1\n");
434 for (i
= 0; i
< 32; i
++) {
435 struct latency_record
*lr
= &task
->latency_record
[i
];
436 if (lr
->backtrace
[0]) {
438 seq_printf(m
, "%i %li %li",
439 lr
->count
, lr
->time
, lr
->max
);
440 for (q
= 0; q
< LT_BACKTRACEDEPTH
; q
++) {
441 unsigned long bt
= lr
->backtrace
[q
];
446 seq_printf(m
, " %ps", (void *)bt
);
452 put_task_struct(task
);
456 static int lstats_open(struct inode
*inode
, struct file
*file
)
458 return single_open(file
, lstats_show_proc
, inode
);
461 static ssize_t
lstats_write(struct file
*file
, const char __user
*buf
,
462 size_t count
, loff_t
*offs
)
464 struct task_struct
*task
= get_proc_task(file
->f_dentry
->d_inode
);
468 clear_all_latency_tracing(task
);
469 put_task_struct(task
);
474 static const struct file_operations proc_lstats_operations
= {
477 .write
= lstats_write
,
479 .release
= single_release
,
484 static int proc_oom_score(struct task_struct
*task
, char *buffer
)
486 unsigned long points
= 0;
488 read_lock(&tasklist_lock
);
490 points
= oom_badness(task
, NULL
, NULL
,
491 totalram_pages
+ total_swap_pages
);
492 read_unlock(&tasklist_lock
);
493 return sprintf(buffer
, "%lu\n", points
);
501 static const struct limit_names lnames
[RLIM_NLIMITS
] = {
502 [RLIMIT_CPU
] = {"Max cpu time", "seconds"},
503 [RLIMIT_FSIZE
] = {"Max file size", "bytes"},
504 [RLIMIT_DATA
] = {"Max data size", "bytes"},
505 [RLIMIT_STACK
] = {"Max stack size", "bytes"},
506 [RLIMIT_CORE
] = {"Max core file size", "bytes"},
507 [RLIMIT_RSS
] = {"Max resident set", "bytes"},
508 [RLIMIT_NPROC
] = {"Max processes", "processes"},
509 [RLIMIT_NOFILE
] = {"Max open files", "files"},
510 [RLIMIT_MEMLOCK
] = {"Max locked memory", "bytes"},
511 [RLIMIT_AS
] = {"Max address space", "bytes"},
512 [RLIMIT_LOCKS
] = {"Max file locks", "locks"},
513 [RLIMIT_SIGPENDING
] = {"Max pending signals", "signals"},
514 [RLIMIT_MSGQUEUE
] = {"Max msgqueue size", "bytes"},
515 [RLIMIT_NICE
] = {"Max nice priority", NULL
},
516 [RLIMIT_RTPRIO
] = {"Max realtime priority", NULL
},
517 [RLIMIT_RTTIME
] = {"Max realtime timeout", "us"},
520 /* Display limits for a process */
521 static int proc_pid_limits(struct task_struct
*task
, char *buffer
)
526 char *bufptr
= buffer
;
528 struct rlimit rlim
[RLIM_NLIMITS
];
530 if (!lock_task_sighand(task
, &flags
))
532 memcpy(rlim
, task
->signal
->rlim
, sizeof(struct rlimit
) * RLIM_NLIMITS
);
533 unlock_task_sighand(task
, &flags
);
536 * print the file header
538 count
+= sprintf(&bufptr
[count
], "%-25s %-20s %-20s %-10s\n",
539 "Limit", "Soft Limit", "Hard Limit", "Units");
541 for (i
= 0; i
< RLIM_NLIMITS
; i
++) {
542 if (rlim
[i
].rlim_cur
== RLIM_INFINITY
)
543 count
+= sprintf(&bufptr
[count
], "%-25s %-20s ",
544 lnames
[i
].name
, "unlimited");
546 count
+= sprintf(&bufptr
[count
], "%-25s %-20lu ",
547 lnames
[i
].name
, rlim
[i
].rlim_cur
);
549 if (rlim
[i
].rlim_max
== RLIM_INFINITY
)
550 count
+= sprintf(&bufptr
[count
], "%-20s ", "unlimited");
552 count
+= sprintf(&bufptr
[count
], "%-20lu ",
556 count
+= sprintf(&bufptr
[count
], "%-10s\n",
559 count
+= sprintf(&bufptr
[count
], "\n");
565 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
566 static int proc_pid_syscall(struct task_struct
*task
, char *buffer
)
569 unsigned long args
[6], sp
, pc
;
570 int res
= lock_trace(task
);
574 if (task_current_syscall(task
, &nr
, args
, 6, &sp
, &pc
))
575 res
= sprintf(buffer
, "running\n");
577 res
= sprintf(buffer
, "%ld 0x%lx 0x%lx\n", nr
, sp
, pc
);
579 res
= sprintf(buffer
,
580 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
582 args
[0], args
[1], args
[2], args
[3], args
[4], args
[5],
587 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
589 /************************************************************************/
590 /* Here the fs part begins */
591 /************************************************************************/
593 /* permission checks */
594 static int proc_fd_access_allowed(struct inode
*inode
)
596 struct task_struct
*task
;
598 /* Allow access to a task's file descriptors if it is us or we
599 * may use ptrace attach to the process and find out that
602 task
= get_proc_task(inode
);
604 allowed
= ptrace_may_access(task
, PTRACE_MODE_READ
);
605 put_task_struct(task
);
610 int proc_setattr(struct dentry
*dentry
, struct iattr
*attr
)
613 struct inode
*inode
= dentry
->d_inode
;
615 if (attr
->ia_valid
& ATTR_MODE
)
618 error
= inode_change_ok(inode
, attr
);
622 if ((attr
->ia_valid
& ATTR_SIZE
) &&
623 attr
->ia_size
!= i_size_read(inode
)) {
624 error
= vmtruncate(inode
, attr
->ia_size
);
629 setattr_copy(inode
, attr
);
630 mark_inode_dirty(inode
);
635 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
636 * or euid/egid (for hide_pid_min=2)?
638 static bool has_pid_permissions(struct pid_namespace
*pid
,
639 struct task_struct
*task
,
642 if (pid
->hide_pid
< hide_pid_min
)
644 if (in_group_p(pid
->pid_gid
))
646 return ptrace_may_access(task
, PTRACE_MODE_READ
);
650 static int proc_pid_permission(struct inode
*inode
, int mask
)
652 struct pid_namespace
*pid
= inode
->i_sb
->s_fs_info
;
653 struct task_struct
*task
;
656 task
= get_proc_task(inode
);
659 has_perms
= has_pid_permissions(pid
, task
, 1);
660 put_task_struct(task
);
663 if (pid
->hide_pid
== 2) {
665 * Let's make getdents(), stat(), and open()
666 * consistent with each other. If a process
667 * may not stat() a file, it shouldn't be seen
675 return generic_permission(inode
, mask
);
680 static const struct inode_operations proc_def_inode_operations
= {
681 .setattr
= proc_setattr
,
684 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
686 static ssize_t
proc_info_read(struct file
* file
, char __user
* buf
,
687 size_t count
, loff_t
*ppos
)
689 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
692 struct task_struct
*task
= get_proc_task(inode
);
698 if (count
> PROC_BLOCK_SIZE
)
699 count
= PROC_BLOCK_SIZE
;
702 if (!(page
= __get_free_page(GFP_TEMPORARY
)))
705 length
= PROC_I(inode
)->op
.proc_read(task
, (char*)page
);
708 length
= simple_read_from_buffer(buf
, count
, ppos
, (char *)page
, length
);
711 put_task_struct(task
);
716 static const struct file_operations proc_info_file_operations
= {
717 .read
= proc_info_read
,
718 .llseek
= generic_file_llseek
,
721 static int proc_single_show(struct seq_file
*m
, void *v
)
723 struct inode
*inode
= m
->private;
724 struct pid_namespace
*ns
;
726 struct task_struct
*task
;
729 ns
= inode
->i_sb
->s_fs_info
;
730 pid
= proc_pid(inode
);
731 task
= get_pid_task(pid
, PIDTYPE_PID
);
735 ret
= PROC_I(inode
)->op
.proc_show(m
, ns
, pid
, task
);
737 put_task_struct(task
);
741 static int proc_single_open(struct inode
*inode
, struct file
*filp
)
743 return single_open(filp
, proc_single_show
, inode
);
746 static const struct file_operations proc_single_file_operations
= {
747 .open
= proc_single_open
,
750 .release
= single_release
,
753 static int mem_open(struct inode
* inode
, struct file
* file
)
755 file
->private_data
= (void*)((long)current
->self_exec_id
);
756 /* OK to pass negative loff_t, we can catch out-of-range */
757 file
->f_mode
|= FMODE_UNSIGNED_OFFSET
;
761 static ssize_t
mem_read(struct file
* file
, char __user
* buf
,
762 size_t count
, loff_t
*ppos
)
764 struct task_struct
*task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
766 unsigned long src
= *ppos
;
768 struct mm_struct
*mm
;
774 page
= (char *)__get_free_page(GFP_TEMPORARY
);
778 mm
= check_mem_permission(task
);
785 if (file
->private_data
!= (void*)((long)current
->self_exec_id
))
791 int this_len
, retval
;
793 this_len
= (count
> PAGE_SIZE
) ? PAGE_SIZE
: count
;
794 retval
= access_remote_vm(mm
, src
, page
, this_len
, 0);
801 if (copy_to_user(buf
, page
, retval
)) {
816 free_page((unsigned long) page
);
818 put_task_struct(task
);
823 static ssize_t
mem_write(struct file
* file
, const char __user
*buf
,
824 size_t count
, loff_t
*ppos
)
828 struct task_struct
*task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
829 unsigned long dst
= *ppos
;
830 struct mm_struct
*mm
;
837 page
= (char *)__get_free_page(GFP_TEMPORARY
);
841 mm
= check_mem_permission(task
);
842 copied
= PTR_ERR(mm
);
847 if (file
->private_data
!= (void *)((long)current
->self_exec_id
))
852 int this_len
, retval
;
854 this_len
= (count
> PAGE_SIZE
) ? PAGE_SIZE
: count
;
855 if (copy_from_user(page
, buf
, this_len
)) {
859 retval
= access_remote_vm(mm
, dst
, page
, this_len
, 1);
875 free_page((unsigned long) page
);
877 put_task_struct(task
);
882 loff_t
mem_lseek(struct file
*file
, loff_t offset
, int orig
)
886 file
->f_pos
= offset
;
889 file
->f_pos
+= offset
;
894 force_successful_syscall_return();
898 static const struct file_operations proc_mem_operations
= {
905 static ssize_t
environ_read(struct file
*file
, char __user
*buf
,
906 size_t count
, loff_t
*ppos
)
908 struct task_struct
*task
= get_proc_task(file
->f_dentry
->d_inode
);
910 unsigned long src
= *ppos
;
912 struct mm_struct
*mm
;
918 page
= (char *)__get_free_page(GFP_TEMPORARY
);
923 mm
= mm_for_maps(task
);
925 if (!mm
|| IS_ERR(mm
))
930 int this_len
, retval
, max_len
;
932 this_len
= mm
->env_end
- (mm
->env_start
+ src
);
937 max_len
= (count
> PAGE_SIZE
) ? PAGE_SIZE
: count
;
938 this_len
= (this_len
> max_len
) ? max_len
: this_len
;
940 retval
= access_process_vm(task
, (mm
->env_start
+ src
),
948 if (copy_to_user(buf
, page
, retval
)) {
962 free_page((unsigned long) page
);
964 put_task_struct(task
);
969 static const struct file_operations proc_environ_operations
= {
970 .read
= environ_read
,
971 .llseek
= generic_file_llseek
,
974 static ssize_t
oom_adjust_read(struct file
*file
, char __user
*buf
,
975 size_t count
, loff_t
*ppos
)
977 struct task_struct
*task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
978 char buffer
[PROC_NUMBUF
];
980 int oom_adjust
= OOM_DISABLE
;
986 if (lock_task_sighand(task
, &flags
)) {
987 oom_adjust
= task
->signal
->oom_adj
;
988 unlock_task_sighand(task
, &flags
);
991 put_task_struct(task
);
993 len
= snprintf(buffer
, sizeof(buffer
), "%i\n", oom_adjust
);
995 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
998 static ssize_t
oom_adjust_write(struct file
*file
, const char __user
*buf
,
999 size_t count
, loff_t
*ppos
)
1001 struct task_struct
*task
;
1002 char buffer
[PROC_NUMBUF
];
1004 unsigned long flags
;
1007 memset(buffer
, 0, sizeof(buffer
));
1008 if (count
> sizeof(buffer
) - 1)
1009 count
= sizeof(buffer
) - 1;
1010 if (copy_from_user(buffer
, buf
, count
)) {
1015 err
= kstrtoint(strstrip(buffer
), 0, &oom_adjust
);
1018 if ((oom_adjust
< OOM_ADJUST_MIN
|| oom_adjust
> OOM_ADJUST_MAX
) &&
1019 oom_adjust
!= OOM_DISABLE
) {
1024 task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
1036 if (!lock_task_sighand(task
, &flags
)) {
1041 if (oom_adjust
< task
->signal
->oom_adj
&& !capable(CAP_SYS_RESOURCE
)) {
1047 * Warn that /proc/pid/oom_adj is deprecated, see
1048 * Documentation/feature-removal-schedule.txt.
1050 printk_once(KERN_WARNING
"%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
1051 current
->comm
, task_pid_nr(current
), task_pid_nr(task
),
1053 task
->signal
->oom_adj
= oom_adjust
;
1055 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1056 * value is always attainable.
1058 if (task
->signal
->oom_adj
== OOM_ADJUST_MAX
)
1059 task
->signal
->oom_score_adj
= OOM_SCORE_ADJ_MAX
;
1061 task
->signal
->oom_score_adj
= (oom_adjust
* OOM_SCORE_ADJ_MAX
) /
1063 trace_oom_score_adj_update(task
);
1065 unlock_task_sighand(task
, &flags
);
1068 put_task_struct(task
);
1070 return err
< 0 ? err
: count
;
1073 static const struct file_operations proc_oom_adjust_operations
= {
1074 .read
= oom_adjust_read
,
1075 .write
= oom_adjust_write
,
1076 .llseek
= generic_file_llseek
,
1079 static ssize_t
oom_score_adj_read(struct file
*file
, char __user
*buf
,
1080 size_t count
, loff_t
*ppos
)
1082 struct task_struct
*task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
1083 char buffer
[PROC_NUMBUF
];
1084 int oom_score_adj
= OOM_SCORE_ADJ_MIN
;
1085 unsigned long flags
;
1090 if (lock_task_sighand(task
, &flags
)) {
1091 oom_score_adj
= task
->signal
->oom_score_adj
;
1092 unlock_task_sighand(task
, &flags
);
1094 put_task_struct(task
);
1095 len
= snprintf(buffer
, sizeof(buffer
), "%d\n", oom_score_adj
);
1096 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
1099 static ssize_t
oom_score_adj_write(struct file
*file
, const char __user
*buf
,
1100 size_t count
, loff_t
*ppos
)
1102 struct task_struct
*task
;
1103 char buffer
[PROC_NUMBUF
];
1104 unsigned long flags
;
1108 memset(buffer
, 0, sizeof(buffer
));
1109 if (count
> sizeof(buffer
) - 1)
1110 count
= sizeof(buffer
) - 1;
1111 if (copy_from_user(buffer
, buf
, count
)) {
1116 err
= kstrtoint(strstrip(buffer
), 0, &oom_score_adj
);
1119 if (oom_score_adj
< OOM_SCORE_ADJ_MIN
||
1120 oom_score_adj
> OOM_SCORE_ADJ_MAX
) {
1125 task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
1137 if (!lock_task_sighand(task
, &flags
)) {
1142 if (oom_score_adj
< task
->signal
->oom_score_adj_min
&&
1143 !capable(CAP_SYS_RESOURCE
)) {
1148 task
->signal
->oom_score_adj
= oom_score_adj
;
1149 if (has_capability_noaudit(current
, CAP_SYS_RESOURCE
))
1150 task
->signal
->oom_score_adj_min
= oom_score_adj
;
1151 trace_oom_score_adj_update(task
);
1153 * Scale /proc/pid/oom_adj appropriately ensuring that OOM_DISABLE is
1154 * always attainable.
1156 if (task
->signal
->oom_score_adj
== OOM_SCORE_ADJ_MIN
)
1157 task
->signal
->oom_adj
= OOM_DISABLE
;
1159 task
->signal
->oom_adj
= (oom_score_adj
* OOM_ADJUST_MAX
) /
1162 unlock_task_sighand(task
, &flags
);
1165 put_task_struct(task
);
1167 return err
< 0 ? err
: count
;
1170 static const struct file_operations proc_oom_score_adj_operations
= {
1171 .read
= oom_score_adj_read
,
1172 .write
= oom_score_adj_write
,
1173 .llseek
= default_llseek
,
1176 #ifdef CONFIG_AUDITSYSCALL
1177 #define TMPBUFLEN 21
1178 static ssize_t
proc_loginuid_read(struct file
* file
, char __user
* buf
,
1179 size_t count
, loff_t
*ppos
)
1181 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
1182 struct task_struct
*task
= get_proc_task(inode
);
1184 char tmpbuf
[TMPBUFLEN
];
1188 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
1189 audit_get_loginuid(task
));
1190 put_task_struct(task
);
1191 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1194 static ssize_t
proc_loginuid_write(struct file
* file
, const char __user
* buf
,
1195 size_t count
, loff_t
*ppos
)
1197 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
1202 if (!capable(CAP_AUDIT_CONTROL
))
1206 if (current
!= pid_task(proc_pid(inode
), PIDTYPE_PID
)) {
1212 if (count
>= PAGE_SIZE
)
1213 count
= PAGE_SIZE
- 1;
1216 /* No partial writes. */
1219 page
= (char*)__get_free_page(GFP_TEMPORARY
);
1223 if (copy_from_user(page
, buf
, count
))
1227 loginuid
= simple_strtoul(page
, &tmp
, 10);
1233 length
= audit_set_loginuid(current
, loginuid
);
1234 if (likely(length
== 0))
1238 free_page((unsigned long) page
);
1242 static const struct file_operations proc_loginuid_operations
= {
1243 .read
= proc_loginuid_read
,
1244 .write
= proc_loginuid_write
,
1245 .llseek
= generic_file_llseek
,
1248 static ssize_t
proc_sessionid_read(struct file
* file
, char __user
* buf
,
1249 size_t count
, loff_t
*ppos
)
1251 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
1252 struct task_struct
*task
= get_proc_task(inode
);
1254 char tmpbuf
[TMPBUFLEN
];
1258 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
1259 audit_get_sessionid(task
));
1260 put_task_struct(task
);
1261 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1264 static const struct file_operations proc_sessionid_operations
= {
1265 .read
= proc_sessionid_read
,
1266 .llseek
= generic_file_llseek
,
1270 #ifdef CONFIG_FAULT_INJECTION
1271 static ssize_t
proc_fault_inject_read(struct file
* file
, char __user
* buf
,
1272 size_t count
, loff_t
*ppos
)
1274 struct task_struct
*task
= get_proc_task(file
->f_dentry
->d_inode
);
1275 char buffer
[PROC_NUMBUF
];
1281 make_it_fail
= task
->make_it_fail
;
1282 put_task_struct(task
);
1284 len
= snprintf(buffer
, sizeof(buffer
), "%i\n", make_it_fail
);
1286 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
1289 static ssize_t
proc_fault_inject_write(struct file
* file
,
1290 const char __user
* buf
, size_t count
, loff_t
*ppos
)
1292 struct task_struct
*task
;
1293 char buffer
[PROC_NUMBUF
], *end
;
1296 if (!capable(CAP_SYS_RESOURCE
))
1298 memset(buffer
, 0, sizeof(buffer
));
1299 if (count
> sizeof(buffer
) - 1)
1300 count
= sizeof(buffer
) - 1;
1301 if (copy_from_user(buffer
, buf
, count
))
1303 make_it_fail
= simple_strtol(strstrip(buffer
), &end
, 0);
1306 task
= get_proc_task(file
->f_dentry
->d_inode
);
1309 task
->make_it_fail
= make_it_fail
;
1310 put_task_struct(task
);
1315 static const struct file_operations proc_fault_inject_operations
= {
1316 .read
= proc_fault_inject_read
,
1317 .write
= proc_fault_inject_write
,
1318 .llseek
= generic_file_llseek
,
1323 #ifdef CONFIG_SCHED_DEBUG
1325 * Print out various scheduling related per-task fields:
1327 static int sched_show(struct seq_file
*m
, void *v
)
1329 struct inode
*inode
= m
->private;
1330 struct task_struct
*p
;
1332 p
= get_proc_task(inode
);
1335 proc_sched_show_task(p
, m
);
1343 sched_write(struct file
*file
, const char __user
*buf
,
1344 size_t count
, loff_t
*offset
)
1346 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1347 struct task_struct
*p
;
1349 p
= get_proc_task(inode
);
1352 proc_sched_set_task(p
);
1359 static int sched_open(struct inode
*inode
, struct file
*filp
)
1361 return single_open(filp
, sched_show
, inode
);
1364 static const struct file_operations proc_pid_sched_operations
= {
1367 .write
= sched_write
,
1368 .llseek
= seq_lseek
,
1369 .release
= single_release
,
1374 #ifdef CONFIG_SCHED_AUTOGROUP
1376 * Print out autogroup related information:
1378 static int sched_autogroup_show(struct seq_file
*m
, void *v
)
1380 struct inode
*inode
= m
->private;
1381 struct task_struct
*p
;
1383 p
= get_proc_task(inode
);
1386 proc_sched_autogroup_show_task(p
, m
);
1394 sched_autogroup_write(struct file
*file
, const char __user
*buf
,
1395 size_t count
, loff_t
*offset
)
1397 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1398 struct task_struct
*p
;
1399 char buffer
[PROC_NUMBUF
];
1403 memset(buffer
, 0, sizeof(buffer
));
1404 if (count
> sizeof(buffer
) - 1)
1405 count
= sizeof(buffer
) - 1;
1406 if (copy_from_user(buffer
, buf
, count
))
1409 err
= kstrtoint(strstrip(buffer
), 0, &nice
);
1413 p
= get_proc_task(inode
);
1418 err
= proc_sched_autogroup_set_nice(p
, &err
);
1427 static int sched_autogroup_open(struct inode
*inode
, struct file
*filp
)
1431 ret
= single_open(filp
, sched_autogroup_show
, NULL
);
1433 struct seq_file
*m
= filp
->private_data
;
1440 static const struct file_operations proc_pid_sched_autogroup_operations
= {
1441 .open
= sched_autogroup_open
,
1443 .write
= sched_autogroup_write
,
1444 .llseek
= seq_lseek
,
1445 .release
= single_release
,
1448 #endif /* CONFIG_SCHED_AUTOGROUP */
1450 static ssize_t
comm_write(struct file
*file
, const char __user
*buf
,
1451 size_t count
, loff_t
*offset
)
1453 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1454 struct task_struct
*p
;
1455 char buffer
[TASK_COMM_LEN
];
1457 memset(buffer
, 0, sizeof(buffer
));
1458 if (count
> sizeof(buffer
) - 1)
1459 count
= sizeof(buffer
) - 1;
1460 if (copy_from_user(buffer
, buf
, count
))
1463 p
= get_proc_task(inode
);
1467 if (same_thread_group(current
, p
))
1468 set_task_comm(p
, buffer
);
1477 static int comm_show(struct seq_file
*m
, void *v
)
1479 struct inode
*inode
= m
->private;
1480 struct task_struct
*p
;
1482 p
= get_proc_task(inode
);
1487 seq_printf(m
, "%s\n", p
->comm
);
1495 static int comm_open(struct inode
*inode
, struct file
*filp
)
1497 return single_open(filp
, comm_show
, inode
);
1500 static const struct file_operations proc_pid_set_comm_operations
= {
1503 .write
= comm_write
,
1504 .llseek
= seq_lseek
,
1505 .release
= single_release
,
1508 static int proc_exe_link(struct dentry
*dentry
, struct path
*exe_path
)
1510 struct task_struct
*task
;
1511 struct mm_struct
*mm
;
1512 struct file
*exe_file
;
1514 task
= get_proc_task(dentry
->d_inode
);
1517 mm
= get_task_mm(task
);
1518 put_task_struct(task
);
1521 exe_file
= get_mm_exe_file(mm
);
1524 *exe_path
= exe_file
->f_path
;
1525 path_get(&exe_file
->f_path
);
1532 static void *proc_pid_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
1534 struct inode
*inode
= dentry
->d_inode
;
1535 int error
= -EACCES
;
1537 /* We don't need a base pointer in the /proc filesystem */
1538 path_put(&nd
->path
);
1540 /* Are we allowed to snoop on the tasks file descriptors? */
1541 if (!proc_fd_access_allowed(inode
))
1544 error
= PROC_I(inode
)->op
.proc_get_link(dentry
, &nd
->path
);
1546 return ERR_PTR(error
);
1549 static int do_proc_readlink(struct path
*path
, char __user
*buffer
, int buflen
)
1551 char *tmp
= (char*)__get_free_page(GFP_TEMPORARY
);
1558 pathname
= d_path(path
, tmp
, PAGE_SIZE
);
1559 len
= PTR_ERR(pathname
);
1560 if (IS_ERR(pathname
))
1562 len
= tmp
+ PAGE_SIZE
- 1 - pathname
;
1566 if (copy_to_user(buffer
, pathname
, len
))
1569 free_page((unsigned long)tmp
);
1573 static int proc_pid_readlink(struct dentry
* dentry
, char __user
* buffer
, int buflen
)
1575 int error
= -EACCES
;
1576 struct inode
*inode
= dentry
->d_inode
;
1579 /* Are we allowed to snoop on the tasks file descriptors? */
1580 if (!proc_fd_access_allowed(inode
))
1583 error
= PROC_I(inode
)->op
.proc_get_link(dentry
, &path
);
1587 error
= do_proc_readlink(&path
, buffer
, buflen
);
1593 static const struct inode_operations proc_pid_link_inode_operations
= {
1594 .readlink
= proc_pid_readlink
,
1595 .follow_link
= proc_pid_follow_link
,
1596 .setattr
= proc_setattr
,
1600 /* building an inode */
1602 static int task_dumpable(struct task_struct
*task
)
1605 struct mm_struct
*mm
;
1610 dumpable
= get_dumpable(mm
);
1617 struct inode
*proc_pid_make_inode(struct super_block
* sb
, struct task_struct
*task
)
1619 struct inode
* inode
;
1620 struct proc_inode
*ei
;
1621 const struct cred
*cred
;
1623 /* We need a new inode */
1625 inode
= new_inode(sb
);
1631 inode
->i_ino
= get_next_ino();
1632 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1633 inode
->i_op
= &proc_def_inode_operations
;
1636 * grab the reference to task.
1638 ei
->pid
= get_task_pid(task
, PIDTYPE_PID
);
1642 if (task_dumpable(task
)) {
1644 cred
= __task_cred(task
);
1645 inode
->i_uid
= cred
->euid
;
1646 inode
->i_gid
= cred
->egid
;
1649 security_task_to_inode(task
, inode
);
1659 int pid_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
1661 struct inode
*inode
= dentry
->d_inode
;
1662 struct task_struct
*task
;
1663 const struct cred
*cred
;
1664 struct pid_namespace
*pid
= dentry
->d_sb
->s_fs_info
;
1666 generic_fillattr(inode
, stat
);
1671 task
= pid_task(proc_pid(inode
), PIDTYPE_PID
);
1673 if (!has_pid_permissions(pid
, task
, 2)) {
1676 * This doesn't prevent learning whether PID exists,
1677 * it only makes getattr() consistent with readdir().
1681 if ((inode
->i_mode
== (S_IFDIR
|S_IRUGO
|S_IXUGO
)) ||
1682 task_dumpable(task
)) {
1683 cred
= __task_cred(task
);
1684 stat
->uid
= cred
->euid
;
1685 stat
->gid
= cred
->egid
;
1695 * Exceptional case: normally we are not allowed to unhash a busy
1696 * directory. In this case, however, we can do it - no aliasing problems
1697 * due to the way we treat inodes.
1699 * Rewrite the inode's ownerships here because the owning task may have
1700 * performed a setuid(), etc.
1702 * Before the /proc/pid/status file was created the only way to read
1703 * the effective uid of a /process was to stat /proc/pid. Reading
1704 * /proc/pid/status is slow enough that procps and other packages
1705 * kept stating /proc/pid. To keep the rules in /proc simple I have
1706 * made this apply to all per process world readable and executable
1709 int pid_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1711 struct inode
*inode
;
1712 struct task_struct
*task
;
1713 const struct cred
*cred
;
1715 if (nd
&& nd
->flags
& LOOKUP_RCU
)
1718 inode
= dentry
->d_inode
;
1719 task
= get_proc_task(inode
);
1722 if ((inode
->i_mode
== (S_IFDIR
|S_IRUGO
|S_IXUGO
)) ||
1723 task_dumpable(task
)) {
1725 cred
= __task_cred(task
);
1726 inode
->i_uid
= cred
->euid
;
1727 inode
->i_gid
= cred
->egid
;
1733 inode
->i_mode
&= ~(S_ISUID
| S_ISGID
);
1734 security_task_to_inode(task
, inode
);
1735 put_task_struct(task
);
1742 static int pid_delete_dentry(const struct dentry
* dentry
)
1744 /* Is the task we represent dead?
1745 * If so, then don't put the dentry on the lru list,
1746 * kill it immediately.
1748 return !proc_pid(dentry
->d_inode
)->tasks
[PIDTYPE_PID
].first
;
1751 const struct dentry_operations pid_dentry_operations
=
1753 .d_revalidate
= pid_revalidate
,
1754 .d_delete
= pid_delete_dentry
,
1760 * Fill a directory entry.
1762 * If possible create the dcache entry and derive our inode number and
1763 * file type from dcache entry.
1765 * Since all of the proc inode numbers are dynamically generated, the inode
1766 * numbers do not exist until the inode is cache. This means creating the
1767 * the dcache entry in readdir is necessary to keep the inode numbers
1768 * reported by readdir in sync with the inode numbers reported
1771 int proc_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
1772 const char *name
, int len
,
1773 instantiate_t instantiate
, struct task_struct
*task
, const void *ptr
)
1775 struct dentry
*child
, *dir
= filp
->f_path
.dentry
;
1776 struct inode
*inode
;
1779 unsigned type
= DT_UNKNOWN
;
1783 qname
.hash
= full_name_hash(name
, len
);
1785 child
= d_lookup(dir
, &qname
);
1788 new = d_alloc(dir
, &qname
);
1790 child
= instantiate(dir
->d_inode
, new, task
, ptr
);
1797 if (!child
|| IS_ERR(child
) || !child
->d_inode
)
1798 goto end_instantiate
;
1799 inode
= child
->d_inode
;
1802 type
= inode
->i_mode
>> 12;
1807 ino
= find_inode_number(dir
, &qname
);
1810 return filldir(dirent
, name
, len
, filp
->f_pos
, ino
, type
);
1813 static unsigned name_to_int(struct dentry
*dentry
)
1815 const char *name
= dentry
->d_name
.name
;
1816 int len
= dentry
->d_name
.len
;
1819 if (len
> 1 && *name
== '0')
1822 unsigned c
= *name
++ - '0';
1825 if (n
>= (~0U-9)/10)
1835 #define PROC_FDINFO_MAX 64
1837 static int proc_fd_info(struct inode
*inode
, struct path
*path
, char *info
)
1839 struct task_struct
*task
= get_proc_task(inode
);
1840 struct files_struct
*files
= NULL
;
1842 int fd
= proc_fd(inode
);
1845 files
= get_files_struct(task
);
1846 put_task_struct(task
);
1850 * We are not taking a ref to the file structure, so we must
1853 spin_lock(&files
->file_lock
);
1854 file
= fcheck_files(files
, fd
);
1856 unsigned int f_flags
;
1857 struct fdtable
*fdt
;
1859 fdt
= files_fdtable(files
);
1860 f_flags
= file
->f_flags
& ~O_CLOEXEC
;
1861 if (FD_ISSET(fd
, fdt
->close_on_exec
))
1862 f_flags
|= O_CLOEXEC
;
1865 *path
= file
->f_path
;
1866 path_get(&file
->f_path
);
1869 snprintf(info
, PROC_FDINFO_MAX
,
1872 (long long) file
->f_pos
,
1874 spin_unlock(&files
->file_lock
);
1875 put_files_struct(files
);
1878 spin_unlock(&files
->file_lock
);
1879 put_files_struct(files
);
1884 static int proc_fd_link(struct dentry
*dentry
, struct path
*path
)
1886 return proc_fd_info(dentry
->d_inode
, path
, NULL
);
1889 static int tid_fd_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1891 struct inode
*inode
;
1892 struct task_struct
*task
;
1894 struct files_struct
*files
;
1895 const struct cred
*cred
;
1897 if (nd
&& nd
->flags
& LOOKUP_RCU
)
1900 inode
= dentry
->d_inode
;
1901 task
= get_proc_task(inode
);
1902 fd
= proc_fd(inode
);
1905 files
= get_files_struct(task
);
1908 if (fcheck_files(files
, fd
)) {
1910 put_files_struct(files
);
1911 if (task_dumpable(task
)) {
1913 cred
= __task_cred(task
);
1914 inode
->i_uid
= cred
->euid
;
1915 inode
->i_gid
= cred
->egid
;
1921 inode
->i_mode
&= ~(S_ISUID
| S_ISGID
);
1922 security_task_to_inode(task
, inode
);
1923 put_task_struct(task
);
1927 put_files_struct(files
);
1929 put_task_struct(task
);
1935 static const struct dentry_operations tid_fd_dentry_operations
=
1937 .d_revalidate
= tid_fd_revalidate
,
1938 .d_delete
= pid_delete_dentry
,
1941 static struct dentry
*proc_fd_instantiate(struct inode
*dir
,
1942 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
1944 unsigned fd
= *(const unsigned *)ptr
;
1946 struct files_struct
*files
;
1947 struct inode
*inode
;
1948 struct proc_inode
*ei
;
1949 struct dentry
*error
= ERR_PTR(-ENOENT
);
1951 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
1956 files
= get_files_struct(task
);
1959 inode
->i_mode
= S_IFLNK
;
1962 * We are not taking a ref to the file structure, so we must
1965 spin_lock(&files
->file_lock
);
1966 file
= fcheck_files(files
, fd
);
1969 if (file
->f_mode
& FMODE_READ
)
1970 inode
->i_mode
|= S_IRUSR
| S_IXUSR
;
1971 if (file
->f_mode
& FMODE_WRITE
)
1972 inode
->i_mode
|= S_IWUSR
| S_IXUSR
;
1973 spin_unlock(&files
->file_lock
);
1974 put_files_struct(files
);
1976 inode
->i_op
= &proc_pid_link_inode_operations
;
1978 ei
->op
.proc_get_link
= proc_fd_link
;
1979 d_set_d_op(dentry
, &tid_fd_dentry_operations
);
1980 d_add(dentry
, inode
);
1981 /* Close the race of the process dying before we return the dentry */
1982 if (tid_fd_revalidate(dentry
, NULL
))
1988 spin_unlock(&files
->file_lock
);
1989 put_files_struct(files
);
1995 static struct dentry
*proc_lookupfd_common(struct inode
*dir
,
1996 struct dentry
*dentry
,
1997 instantiate_t instantiate
)
1999 struct task_struct
*task
= get_proc_task(dir
);
2000 unsigned fd
= name_to_int(dentry
);
2001 struct dentry
*result
= ERR_PTR(-ENOENT
);
2008 result
= instantiate(dir
, dentry
, task
, &fd
);
2010 put_task_struct(task
);
2015 static int proc_readfd_common(struct file
* filp
, void * dirent
,
2016 filldir_t filldir
, instantiate_t instantiate
)
2018 struct dentry
*dentry
= filp
->f_path
.dentry
;
2019 struct inode
*inode
= dentry
->d_inode
;
2020 struct task_struct
*p
= get_proc_task(inode
);
2021 unsigned int fd
, ino
;
2023 struct files_struct
* files
;
2033 if (filldir(dirent
, ".", 1, 0, inode
->i_ino
, DT_DIR
) < 0)
2037 ino
= parent_ino(dentry
);
2038 if (filldir(dirent
, "..", 2, 1, ino
, DT_DIR
) < 0)
2042 files
= get_files_struct(p
);
2046 for (fd
= filp
->f_pos
-2;
2047 fd
< files_fdtable(files
)->max_fds
;
2048 fd
++, filp
->f_pos
++) {
2049 char name
[PROC_NUMBUF
];
2052 if (!fcheck_files(files
, fd
))
2056 len
= snprintf(name
, sizeof(name
), "%d", fd
);
2057 if (proc_fill_cache(filp
, dirent
, filldir
,
2058 name
, len
, instantiate
,
2066 put_files_struct(files
);
2074 static struct dentry
*proc_lookupfd(struct inode
*dir
, struct dentry
*dentry
,
2075 struct nameidata
*nd
)
2077 return proc_lookupfd_common(dir
, dentry
, proc_fd_instantiate
);
2080 static int proc_readfd(struct file
*filp
, void *dirent
, filldir_t filldir
)
2082 return proc_readfd_common(filp
, dirent
, filldir
, proc_fd_instantiate
);
2085 static ssize_t
proc_fdinfo_read(struct file
*file
, char __user
*buf
,
2086 size_t len
, loff_t
*ppos
)
2088 char tmp
[PROC_FDINFO_MAX
];
2089 int err
= proc_fd_info(file
->f_path
.dentry
->d_inode
, NULL
, tmp
);
2091 err
= simple_read_from_buffer(buf
, len
, ppos
, tmp
, strlen(tmp
));
2095 static const struct file_operations proc_fdinfo_file_operations
= {
2096 .open
= nonseekable_open
,
2097 .read
= proc_fdinfo_read
,
2098 .llseek
= no_llseek
,
2101 static const struct file_operations proc_fd_operations
= {
2102 .read
= generic_read_dir
,
2103 .readdir
= proc_readfd
,
2104 .llseek
= default_llseek
,
2107 #ifdef CONFIG_CHECKPOINT_RESTORE
2110 * dname_to_vma_addr - maps a dentry name into two unsigned longs
2111 * which represent vma start and end addresses.
2113 static int dname_to_vma_addr(struct dentry
*dentry
,
2114 unsigned long *start
, unsigned long *end
)
2116 if (sscanf(dentry
->d_name
.name
, "%lx-%lx", start
, end
) != 2)
2122 static int map_files_d_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
2124 unsigned long vm_start
, vm_end
;
2125 bool exact_vma_exists
= false;
2126 struct mm_struct
*mm
= NULL
;
2127 struct task_struct
*task
;
2128 const struct cred
*cred
;
2129 struct inode
*inode
;
2132 if (nd
&& nd
->flags
& LOOKUP_RCU
)
2135 if (!capable(CAP_SYS_ADMIN
)) {
2140 inode
= dentry
->d_inode
;
2141 task
= get_proc_task(inode
);
2145 if (!ptrace_may_access(task
, PTRACE_MODE_READ
))
2148 mm
= get_task_mm(task
);
2152 if (!dname_to_vma_addr(dentry
, &vm_start
, &vm_end
)) {
2153 down_read(&mm
->mmap_sem
);
2154 exact_vma_exists
= !!find_exact_vma(mm
, vm_start
, vm_end
);
2155 up_read(&mm
->mmap_sem
);
2160 if (exact_vma_exists
) {
2161 if (task_dumpable(task
)) {
2163 cred
= __task_cred(task
);
2164 inode
->i_uid
= cred
->euid
;
2165 inode
->i_gid
= cred
->egid
;
2171 security_task_to_inode(task
, inode
);
2176 put_task_struct(task
);
2185 static const struct dentry_operations tid_map_files_dentry_operations
= {
2186 .d_revalidate
= map_files_d_revalidate
,
2187 .d_delete
= pid_delete_dentry
,
2190 static int proc_map_files_get_link(struct dentry
*dentry
, struct path
*path
)
2192 unsigned long vm_start
, vm_end
;
2193 struct vm_area_struct
*vma
;
2194 struct task_struct
*task
;
2195 struct mm_struct
*mm
;
2199 task
= get_proc_task(dentry
->d_inode
);
2203 mm
= get_task_mm(task
);
2204 put_task_struct(task
);
2208 rc
= dname_to_vma_addr(dentry
, &vm_start
, &vm_end
);
2212 down_read(&mm
->mmap_sem
);
2213 vma
= find_exact_vma(mm
, vm_start
, vm_end
);
2214 if (vma
&& vma
->vm_file
) {
2215 *path
= vma
->vm_file
->f_path
;
2219 up_read(&mm
->mmap_sem
);
2227 struct map_files_info
{
2230 unsigned char name
[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
2233 static struct dentry
*
2234 proc_map_files_instantiate(struct inode
*dir
, struct dentry
*dentry
,
2235 struct task_struct
*task
, const void *ptr
)
2237 const struct file
*file
= ptr
;
2238 struct proc_inode
*ei
;
2239 struct inode
*inode
;
2242 return ERR_PTR(-ENOENT
);
2244 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
2246 return ERR_PTR(-ENOENT
);
2249 ei
->op
.proc_get_link
= proc_map_files_get_link
;
2251 inode
->i_op
= &proc_pid_link_inode_operations
;
2253 inode
->i_mode
= S_IFLNK
;
2255 if (file
->f_mode
& FMODE_READ
)
2256 inode
->i_mode
|= S_IRUSR
;
2257 if (file
->f_mode
& FMODE_WRITE
)
2258 inode
->i_mode
|= S_IWUSR
;
2260 d_set_d_op(dentry
, &tid_map_files_dentry_operations
);
2261 d_add(dentry
, inode
);
2266 static struct dentry
*proc_map_files_lookup(struct inode
*dir
,
2267 struct dentry
*dentry
, struct nameidata
*nd
)
2269 unsigned long vm_start
, vm_end
;
2270 struct vm_area_struct
*vma
;
2271 struct task_struct
*task
;
2272 struct dentry
*result
;
2273 struct mm_struct
*mm
;
2275 result
= ERR_PTR(-EACCES
);
2276 if (!capable(CAP_SYS_ADMIN
))
2279 result
= ERR_PTR(-ENOENT
);
2280 task
= get_proc_task(dir
);
2284 result
= ERR_PTR(-EACCES
);
2285 if (lock_trace(task
))
2288 result
= ERR_PTR(-ENOENT
);
2289 if (dname_to_vma_addr(dentry
, &vm_start
, &vm_end
))
2292 mm
= get_task_mm(task
);
2296 down_read(&mm
->mmap_sem
);
2297 vma
= find_exact_vma(mm
, vm_start
, vm_end
);
2301 result
= proc_map_files_instantiate(dir
, dentry
, task
, vma
->vm_file
);
2304 up_read(&mm
->mmap_sem
);
2309 put_task_struct(task
);
2314 static const struct inode_operations proc_map_files_inode_operations
= {
2315 .lookup
= proc_map_files_lookup
,
2316 .permission
= proc_fd_permission
,
2317 .setattr
= proc_setattr
,
2321 proc_map_files_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
2323 struct dentry
*dentry
= filp
->f_path
.dentry
;
2324 struct inode
*inode
= dentry
->d_inode
;
2325 struct vm_area_struct
*vma
;
2326 struct task_struct
*task
;
2327 struct mm_struct
*mm
;
2332 if (!capable(CAP_SYS_ADMIN
))
2336 task
= get_proc_task(inode
);
2341 if (lock_trace(task
))
2345 switch (filp
->f_pos
) {
2348 if (filldir(dirent
, ".", 1, 0, ino
, DT_DIR
) < 0)
2352 ino
= parent_ino(dentry
);
2353 if (filldir(dirent
, "..", 2, 1, ino
, DT_DIR
) < 0)
2358 unsigned long nr_files
, pos
, i
;
2359 struct flex_array
*fa
= NULL
;
2360 struct map_files_info info
;
2361 struct map_files_info
*p
;
2363 mm
= get_task_mm(task
);
2366 down_read(&mm
->mmap_sem
);
2371 * We need two passes here:
2373 * 1) Collect vmas of mapped files with mmap_sem taken
2374 * 2) Release mmap_sem and instantiate entries
2376 * otherwise we get lockdep complained, since filldir()
2377 * routine might require mmap_sem taken in might_fault().
2380 for (vma
= mm
->mmap
, pos
= 2; vma
; vma
= vma
->vm_next
) {
2381 if (vma
->vm_file
&& ++pos
> filp
->f_pos
)
2386 fa
= flex_array_alloc(sizeof(info
), nr_files
,
2388 if (!fa
|| flex_array_prealloc(fa
, 0, nr_files
,
2392 flex_array_free(fa
);
2393 up_read(&mm
->mmap_sem
);
2397 for (i
= 0, vma
= mm
->mmap
, pos
= 2; vma
;
2398 vma
= vma
->vm_next
) {
2401 if (++pos
<= filp
->f_pos
)
2404 get_file(vma
->vm_file
);
2405 info
.file
= vma
->vm_file
;
2406 info
.len
= snprintf(info
.name
,
2407 sizeof(info
.name
), "%lx-%lx",
2408 vma
->vm_start
, vma
->vm_end
);
2409 if (flex_array_put(fa
, i
++, &info
, GFP_KERNEL
))
2413 up_read(&mm
->mmap_sem
);
2415 for (i
= 0; i
< nr_files
; i
++) {
2416 p
= flex_array_get(fa
, i
);
2417 ret
= proc_fill_cache(filp
, dirent
, filldir
,
2419 proc_map_files_instantiate
,
2426 for (; i
< nr_files
; i
++) {
2428 * In case of error don't forget
2429 * to put rest of file refs.
2431 p
= flex_array_get(fa
, i
);
2435 flex_array_free(fa
);
2443 put_task_struct(task
);
2448 static const struct file_operations proc_map_files_operations
= {
2449 .read
= generic_read_dir
,
2450 .readdir
= proc_map_files_readdir
,
2451 .llseek
= default_llseek
,
2454 #endif /* CONFIG_CHECKPOINT_RESTORE */
2457 * /proc/pid/fd needs a special permission handler so that a process can still
2458 * access /proc/self/fd after it has executed a setuid().
2460 static int proc_fd_permission(struct inode
*inode
, int mask
)
2462 int rv
= generic_permission(inode
, mask
);
2465 if (task_pid(current
) == proc_pid(inode
))
2471 * proc directories can do almost nothing..
2473 static const struct inode_operations proc_fd_inode_operations
= {
2474 .lookup
= proc_lookupfd
,
2475 .permission
= proc_fd_permission
,
2476 .setattr
= proc_setattr
,
2479 static struct dentry
*proc_fdinfo_instantiate(struct inode
*dir
,
2480 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
2482 unsigned fd
= *(unsigned *)ptr
;
2483 struct inode
*inode
;
2484 struct proc_inode
*ei
;
2485 struct dentry
*error
= ERR_PTR(-ENOENT
);
2487 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
2492 inode
->i_mode
= S_IFREG
| S_IRUSR
;
2493 inode
->i_fop
= &proc_fdinfo_file_operations
;
2494 d_set_d_op(dentry
, &tid_fd_dentry_operations
);
2495 d_add(dentry
, inode
);
2496 /* Close the race of the process dying before we return the dentry */
2497 if (tid_fd_revalidate(dentry
, NULL
))
2504 static struct dentry
*proc_lookupfdinfo(struct inode
*dir
,
2505 struct dentry
*dentry
,
2506 struct nameidata
*nd
)
2508 return proc_lookupfd_common(dir
, dentry
, proc_fdinfo_instantiate
);
2511 static int proc_readfdinfo(struct file
*filp
, void *dirent
, filldir_t filldir
)
2513 return proc_readfd_common(filp
, dirent
, filldir
,
2514 proc_fdinfo_instantiate
);
2517 static const struct file_operations proc_fdinfo_operations
= {
2518 .read
= generic_read_dir
,
2519 .readdir
= proc_readfdinfo
,
2520 .llseek
= default_llseek
,
2524 * proc directories can do almost nothing..
2526 static const struct inode_operations proc_fdinfo_inode_operations
= {
2527 .lookup
= proc_lookupfdinfo
,
2528 .setattr
= proc_setattr
,
2532 static struct dentry
*proc_pident_instantiate(struct inode
*dir
,
2533 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
2535 const struct pid_entry
*p
= ptr
;
2536 struct inode
*inode
;
2537 struct proc_inode
*ei
;
2538 struct dentry
*error
= ERR_PTR(-ENOENT
);
2540 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
2545 inode
->i_mode
= p
->mode
;
2546 if (S_ISDIR(inode
->i_mode
))
2547 set_nlink(inode
, 2); /* Use getattr to fix if necessary */
2549 inode
->i_op
= p
->iop
;
2551 inode
->i_fop
= p
->fop
;
2553 d_set_d_op(dentry
, &pid_dentry_operations
);
2554 d_add(dentry
, inode
);
2555 /* Close the race of the process dying before we return the dentry */
2556 if (pid_revalidate(dentry
, NULL
))
2562 static struct dentry
*proc_pident_lookup(struct inode
*dir
,
2563 struct dentry
*dentry
,
2564 const struct pid_entry
*ents
,
2567 struct dentry
*error
;
2568 struct task_struct
*task
= get_proc_task(dir
);
2569 const struct pid_entry
*p
, *last
;
2571 error
= ERR_PTR(-ENOENT
);
2577 * Yes, it does not scale. And it should not. Don't add
2578 * new entries into /proc/<tgid>/ without very good reasons.
2580 last
= &ents
[nents
- 1];
2581 for (p
= ents
; p
<= last
; p
++) {
2582 if (p
->len
!= dentry
->d_name
.len
)
2584 if (!memcmp(dentry
->d_name
.name
, p
->name
, p
->len
))
2590 error
= proc_pident_instantiate(dir
, dentry
, task
, p
);
2592 put_task_struct(task
);
2597 static int proc_pident_fill_cache(struct file
*filp
, void *dirent
,
2598 filldir_t filldir
, struct task_struct
*task
, const struct pid_entry
*p
)
2600 return proc_fill_cache(filp
, dirent
, filldir
, p
->name
, p
->len
,
2601 proc_pident_instantiate
, task
, p
);
2604 static int proc_pident_readdir(struct file
*filp
,
2605 void *dirent
, filldir_t filldir
,
2606 const struct pid_entry
*ents
, unsigned int nents
)
2609 struct dentry
*dentry
= filp
->f_path
.dentry
;
2610 struct inode
*inode
= dentry
->d_inode
;
2611 struct task_struct
*task
= get_proc_task(inode
);
2612 const struct pid_entry
*p
, *last
;
2625 if (filldir(dirent
, ".", 1, i
, ino
, DT_DIR
) < 0)
2631 ino
= parent_ino(dentry
);
2632 if (filldir(dirent
, "..", 2, i
, ino
, DT_DIR
) < 0)
2644 last
= &ents
[nents
- 1];
2646 if (proc_pident_fill_cache(filp
, dirent
, filldir
, task
, p
) < 0)
2655 put_task_struct(task
);
2660 #ifdef CONFIG_SECURITY
2661 static ssize_t
proc_pid_attr_read(struct file
* file
, char __user
* buf
,
2662 size_t count
, loff_t
*ppos
)
2664 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
2667 struct task_struct
*task
= get_proc_task(inode
);
2672 length
= security_getprocattr(task
,
2673 (char*)file
->f_path
.dentry
->d_name
.name
,
2675 put_task_struct(task
);
2677 length
= simple_read_from_buffer(buf
, count
, ppos
, p
, length
);
2682 static ssize_t
proc_pid_attr_write(struct file
* file
, const char __user
* buf
,
2683 size_t count
, loff_t
*ppos
)
2685 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
2688 struct task_struct
*task
= get_proc_task(inode
);
2693 if (count
> PAGE_SIZE
)
2696 /* No partial writes. */
2702 page
= (char*)__get_free_page(GFP_TEMPORARY
);
2707 if (copy_from_user(page
, buf
, count
))
2710 /* Guard against adverse ptrace interaction */
2711 length
= mutex_lock_interruptible(&task
->signal
->cred_guard_mutex
);
2715 length
= security_setprocattr(task
,
2716 (char*)file
->f_path
.dentry
->d_name
.name
,
2717 (void*)page
, count
);
2718 mutex_unlock(&task
->signal
->cred_guard_mutex
);
2720 free_page((unsigned long) page
);
2722 put_task_struct(task
);
2727 static const struct file_operations proc_pid_attr_operations
= {
2728 .read
= proc_pid_attr_read
,
2729 .write
= proc_pid_attr_write
,
2730 .llseek
= generic_file_llseek
,
2733 static const struct pid_entry attr_dir_stuff
[] = {
2734 REG("current", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2735 REG("prev", S_IRUGO
, proc_pid_attr_operations
),
2736 REG("exec", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2737 REG("fscreate", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2738 REG("keycreate", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2739 REG("sockcreate", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2742 static int proc_attr_dir_readdir(struct file
* filp
,
2743 void * dirent
, filldir_t filldir
)
2745 return proc_pident_readdir(filp
,dirent
,filldir
,
2746 attr_dir_stuff
,ARRAY_SIZE(attr_dir_stuff
));
2749 static const struct file_operations proc_attr_dir_operations
= {
2750 .read
= generic_read_dir
,
2751 .readdir
= proc_attr_dir_readdir
,
2752 .llseek
= default_llseek
,
2755 static struct dentry
*proc_attr_dir_lookup(struct inode
*dir
,
2756 struct dentry
*dentry
, struct nameidata
*nd
)
2758 return proc_pident_lookup(dir
, dentry
,
2759 attr_dir_stuff
, ARRAY_SIZE(attr_dir_stuff
));
2762 static const struct inode_operations proc_attr_dir_inode_operations
= {
2763 .lookup
= proc_attr_dir_lookup
,
2764 .getattr
= pid_getattr
,
2765 .setattr
= proc_setattr
,
2770 #ifdef CONFIG_ELF_CORE
2771 static ssize_t
proc_coredump_filter_read(struct file
*file
, char __user
*buf
,
2772 size_t count
, loff_t
*ppos
)
2774 struct task_struct
*task
= get_proc_task(file
->f_dentry
->d_inode
);
2775 struct mm_struct
*mm
;
2776 char buffer
[PROC_NUMBUF
];
2784 mm
= get_task_mm(task
);
2786 len
= snprintf(buffer
, sizeof(buffer
), "%08lx\n",
2787 ((mm
->flags
& MMF_DUMP_FILTER_MASK
) >>
2788 MMF_DUMP_FILTER_SHIFT
));
2790 ret
= simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
2793 put_task_struct(task
);
2798 static ssize_t
proc_coredump_filter_write(struct file
*file
,
2799 const char __user
*buf
,
2803 struct task_struct
*task
;
2804 struct mm_struct
*mm
;
2805 char buffer
[PROC_NUMBUF
], *end
;
2812 memset(buffer
, 0, sizeof(buffer
));
2813 if (count
> sizeof(buffer
) - 1)
2814 count
= sizeof(buffer
) - 1;
2815 if (copy_from_user(buffer
, buf
, count
))
2819 val
= (unsigned int)simple_strtoul(buffer
, &end
, 0);
2822 if (end
- buffer
== 0)
2826 task
= get_proc_task(file
->f_dentry
->d_inode
);
2831 mm
= get_task_mm(task
);
2835 for (i
= 0, mask
= 1; i
< MMF_DUMP_FILTER_BITS
; i
++, mask
<<= 1) {
2837 set_bit(i
+ MMF_DUMP_FILTER_SHIFT
, &mm
->flags
);
2839 clear_bit(i
+ MMF_DUMP_FILTER_SHIFT
, &mm
->flags
);
2844 put_task_struct(task
);
2849 static const struct file_operations proc_coredump_filter_operations
= {
2850 .read
= proc_coredump_filter_read
,
2851 .write
= proc_coredump_filter_write
,
2852 .llseek
= generic_file_llseek
,
2859 static int proc_self_readlink(struct dentry
*dentry
, char __user
*buffer
,
2862 struct pid_namespace
*ns
= dentry
->d_sb
->s_fs_info
;
2863 pid_t tgid
= task_tgid_nr_ns(current
, ns
);
2864 char tmp
[PROC_NUMBUF
];
2867 sprintf(tmp
, "%d", tgid
);
2868 return vfs_readlink(dentry
,buffer
,buflen
,tmp
);
2871 static void *proc_self_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
2873 struct pid_namespace
*ns
= dentry
->d_sb
->s_fs_info
;
2874 pid_t tgid
= task_tgid_nr_ns(current
, ns
);
2875 char *name
= ERR_PTR(-ENOENT
);
2879 name
= ERR_PTR(-ENOMEM
);
2881 sprintf(name
, "%d", tgid
);
2883 nd_set_link(nd
, name
);
2887 static void proc_self_put_link(struct dentry
*dentry
, struct nameidata
*nd
,
2890 char *s
= nd_get_link(nd
);
2895 static const struct inode_operations proc_self_inode_operations
= {
2896 .readlink
= proc_self_readlink
,
2897 .follow_link
= proc_self_follow_link
,
2898 .put_link
= proc_self_put_link
,
2904 * These are the directory entries in the root directory of /proc
2905 * that properly belong to the /proc filesystem, as they describe
2906 * describe something that is process related.
2908 static const struct pid_entry proc_base_stuff
[] = {
2909 NOD("self", S_IFLNK
|S_IRWXUGO
,
2910 &proc_self_inode_operations
, NULL
, {}),
2913 static struct dentry
*proc_base_instantiate(struct inode
*dir
,
2914 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
2916 const struct pid_entry
*p
= ptr
;
2917 struct inode
*inode
;
2918 struct proc_inode
*ei
;
2919 struct dentry
*error
;
2921 /* Allocate the inode */
2922 error
= ERR_PTR(-ENOMEM
);
2923 inode
= new_inode(dir
->i_sb
);
2927 /* Initialize the inode */
2929 inode
->i_ino
= get_next_ino();
2930 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
2933 * grab the reference to the task.
2935 ei
->pid
= get_task_pid(task
, PIDTYPE_PID
);
2939 inode
->i_mode
= p
->mode
;
2940 if (S_ISDIR(inode
->i_mode
))
2941 set_nlink(inode
, 2);
2942 if (S_ISLNK(inode
->i_mode
))
2945 inode
->i_op
= p
->iop
;
2947 inode
->i_fop
= p
->fop
;
2949 d_add(dentry
, inode
);
2958 static struct dentry
*proc_base_lookup(struct inode
*dir
, struct dentry
*dentry
)
2960 struct dentry
*error
;
2961 struct task_struct
*task
= get_proc_task(dir
);
2962 const struct pid_entry
*p
, *last
;
2964 error
= ERR_PTR(-ENOENT
);
2969 /* Lookup the directory entry */
2970 last
= &proc_base_stuff
[ARRAY_SIZE(proc_base_stuff
) - 1];
2971 for (p
= proc_base_stuff
; p
<= last
; p
++) {
2972 if (p
->len
!= dentry
->d_name
.len
)
2974 if (!memcmp(dentry
->d_name
.name
, p
->name
, p
->len
))
2980 error
= proc_base_instantiate(dir
, dentry
, task
, p
);
2983 put_task_struct(task
);
2988 static int proc_base_fill_cache(struct file
*filp
, void *dirent
,
2989 filldir_t filldir
, struct task_struct
*task
, const struct pid_entry
*p
)
2991 return proc_fill_cache(filp
, dirent
, filldir
, p
->name
, p
->len
,
2992 proc_base_instantiate
, task
, p
);
2995 #ifdef CONFIG_TASK_IO_ACCOUNTING
2996 static int do_io_accounting(struct task_struct
*task
, char *buffer
, int whole
)
2998 struct task_io_accounting acct
= task
->ioac
;
2999 unsigned long flags
;
3002 result
= mutex_lock_killable(&task
->signal
->cred_guard_mutex
);
3006 if (!ptrace_may_access(task
, PTRACE_MODE_READ
)) {
3011 if (whole
&& lock_task_sighand(task
, &flags
)) {
3012 struct task_struct
*t
= task
;
3014 task_io_accounting_add(&acct
, &task
->signal
->ioac
);
3015 while_each_thread(task
, t
)
3016 task_io_accounting_add(&acct
, &t
->ioac
);
3018 unlock_task_sighand(task
, &flags
);
3020 result
= sprintf(buffer
,
3025 "read_bytes: %llu\n"
3026 "write_bytes: %llu\n"
3027 "cancelled_write_bytes: %llu\n",
3028 (unsigned long long)acct
.rchar
,
3029 (unsigned long long)acct
.wchar
,
3030 (unsigned long long)acct
.syscr
,
3031 (unsigned long long)acct
.syscw
,
3032 (unsigned long long)acct
.read_bytes
,
3033 (unsigned long long)acct
.write_bytes
,
3034 (unsigned long long)acct
.cancelled_write_bytes
);
3036 mutex_unlock(&task
->signal
->cred_guard_mutex
);
3040 static int proc_tid_io_accounting(struct task_struct
*task
, char *buffer
)
3042 return do_io_accounting(task
, buffer
, 0);
3045 static int proc_tgid_io_accounting(struct task_struct
*task
, char *buffer
)
3047 return do_io_accounting(task
, buffer
, 1);
3049 #endif /* CONFIG_TASK_IO_ACCOUNTING */
3051 static int proc_pid_personality(struct seq_file
*m
, struct pid_namespace
*ns
,
3052 struct pid
*pid
, struct task_struct
*task
)
3054 int err
= lock_trace(task
);
3056 seq_printf(m
, "%08x\n", task
->personality
);
3065 static const struct file_operations proc_task_operations
;
3066 static const struct inode_operations proc_task_inode_operations
;
3068 static const struct pid_entry tgid_base_stuff
[] = {
3069 DIR("task", S_IRUGO
|S_IXUGO
, proc_task_inode_operations
, proc_task_operations
),
3070 DIR("fd", S_IRUSR
|S_IXUSR
, proc_fd_inode_operations
, proc_fd_operations
),
3071 #ifdef CONFIG_CHECKPOINT_RESTORE
3072 DIR("map_files", S_IRUSR
|S_IXUSR
, proc_map_files_inode_operations
, proc_map_files_operations
),
3074 DIR("fdinfo", S_IRUSR
|S_IXUSR
, proc_fdinfo_inode_operations
, proc_fdinfo_operations
),
3075 DIR("ns", S_IRUSR
|S_IXUGO
, proc_ns_dir_inode_operations
, proc_ns_dir_operations
),
3077 DIR("net", S_IRUGO
|S_IXUGO
, proc_net_inode_operations
, proc_net_operations
),
3079 REG("environ", S_IRUSR
, proc_environ_operations
),
3080 INF("auxv", S_IRUSR
, proc_pid_auxv
),
3081 ONE("status", S_IRUGO
, proc_pid_status
),
3082 ONE("personality", S_IRUGO
, proc_pid_personality
),
3083 INF("limits", S_IRUGO
, proc_pid_limits
),
3084 #ifdef CONFIG_SCHED_DEBUG
3085 REG("sched", S_IRUGO
|S_IWUSR
, proc_pid_sched_operations
),
3087 #ifdef CONFIG_SCHED_AUTOGROUP
3088 REG("autogroup", S_IRUGO
|S_IWUSR
, proc_pid_sched_autogroup_operations
),
3090 REG("comm", S_IRUGO
|S_IWUSR
, proc_pid_set_comm_operations
),
3091 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3092 INF("syscall", S_IRUGO
, proc_pid_syscall
),
3094 INF("cmdline", S_IRUGO
, proc_pid_cmdline
),
3095 ONE("stat", S_IRUGO
, proc_tgid_stat
),
3096 ONE("statm", S_IRUGO
, proc_pid_statm
),
3097 REG("maps", S_IRUGO
, proc_maps_operations
),
3099 REG("numa_maps", S_IRUGO
, proc_numa_maps_operations
),
3101 REG("mem", S_IRUSR
|S_IWUSR
, proc_mem_operations
),
3102 LNK("cwd", proc_cwd_link
),
3103 LNK("root", proc_root_link
),
3104 LNK("exe", proc_exe_link
),
3105 REG("mounts", S_IRUGO
, proc_mounts_operations
),
3106 REG("mountinfo", S_IRUGO
, proc_mountinfo_operations
),
3107 REG("mountstats", S_IRUSR
, proc_mountstats_operations
),
3108 #ifdef CONFIG_PROC_PAGE_MONITOR
3109 REG("clear_refs", S_IWUSR
, proc_clear_refs_operations
),
3110 REG("smaps", S_IRUGO
, proc_smaps_operations
),
3111 REG("pagemap", S_IRUGO
, proc_pagemap_operations
),
3113 #ifdef CONFIG_SECURITY
3114 DIR("attr", S_IRUGO
|S_IXUGO
, proc_attr_dir_inode_operations
, proc_attr_dir_operations
),
3116 #ifdef CONFIG_KALLSYMS
3117 INF("wchan", S_IRUGO
, proc_pid_wchan
),
3119 #ifdef CONFIG_STACKTRACE
3120 ONE("stack", S_IRUGO
, proc_pid_stack
),
3122 #ifdef CONFIG_SCHEDSTATS
3123 INF("schedstat", S_IRUGO
, proc_pid_schedstat
),
3125 #ifdef CONFIG_LATENCYTOP
3126 REG("latency", S_IRUGO
, proc_lstats_operations
),
3128 #ifdef CONFIG_PROC_PID_CPUSET
3129 REG("cpuset", S_IRUGO
, proc_cpuset_operations
),
3131 #ifdef CONFIG_CGROUPS
3132 REG("cgroup", S_IRUGO
, proc_cgroup_operations
),
3134 INF("oom_score", S_IRUGO
, proc_oom_score
),
3135 REG("oom_adj", S_IRUGO
|S_IWUSR
, proc_oom_adjust_operations
),
3136 REG("oom_score_adj", S_IRUGO
|S_IWUSR
, proc_oom_score_adj_operations
),
3137 #ifdef CONFIG_AUDITSYSCALL
3138 REG("loginuid", S_IWUSR
|S_IRUGO
, proc_loginuid_operations
),
3139 REG("sessionid", S_IRUGO
, proc_sessionid_operations
),
3141 #ifdef CONFIG_FAULT_INJECTION
3142 REG("make-it-fail", S_IRUGO
|S_IWUSR
, proc_fault_inject_operations
),
3144 #ifdef CONFIG_ELF_CORE
3145 REG("coredump_filter", S_IRUGO
|S_IWUSR
, proc_coredump_filter_operations
),
3147 #ifdef CONFIG_TASK_IO_ACCOUNTING
3148 INF("io", S_IRUSR
, proc_tgid_io_accounting
),
3150 #ifdef CONFIG_HARDWALL
3151 INF("hardwall", S_IRUGO
, proc_pid_hardwall
),
3155 static int proc_tgid_base_readdir(struct file
* filp
,
3156 void * dirent
, filldir_t filldir
)
3158 return proc_pident_readdir(filp
,dirent
,filldir
,
3159 tgid_base_stuff
,ARRAY_SIZE(tgid_base_stuff
));
3162 static const struct file_operations proc_tgid_base_operations
= {
3163 .read
= generic_read_dir
,
3164 .readdir
= proc_tgid_base_readdir
,
3165 .llseek
= default_llseek
,
3168 static struct dentry
*proc_tgid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
){
3169 return proc_pident_lookup(dir
, dentry
,
3170 tgid_base_stuff
, ARRAY_SIZE(tgid_base_stuff
));
3173 static const struct inode_operations proc_tgid_base_inode_operations
= {
3174 .lookup
= proc_tgid_base_lookup
,
3175 .getattr
= pid_getattr
,
3176 .setattr
= proc_setattr
,
3177 .permission
= proc_pid_permission
,
3180 static void proc_flush_task_mnt(struct vfsmount
*mnt
, pid_t pid
, pid_t tgid
)
3182 struct dentry
*dentry
, *leader
, *dir
;
3183 char buf
[PROC_NUMBUF
];
3187 name
.len
= snprintf(buf
, sizeof(buf
), "%d", pid
);
3188 dentry
= d_hash_and_lookup(mnt
->mnt_root
, &name
);
3190 shrink_dcache_parent(dentry
);
3196 name
.len
= snprintf(buf
, sizeof(buf
), "%d", tgid
);
3197 leader
= d_hash_and_lookup(mnt
->mnt_root
, &name
);
3202 name
.len
= strlen(name
.name
);
3203 dir
= d_hash_and_lookup(leader
, &name
);
3205 goto out_put_leader
;
3208 name
.len
= snprintf(buf
, sizeof(buf
), "%d", pid
);
3209 dentry
= d_hash_and_lookup(dir
, &name
);
3211 shrink_dcache_parent(dentry
);
3224 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
3225 * @task: task that should be flushed.
3227 * When flushing dentries from proc, one needs to flush them from global
3228 * proc (proc_mnt) and from all the namespaces' procs this task was seen
3229 * in. This call is supposed to do all of this job.
3231 * Looks in the dcache for
3233 * /proc/@tgid/task/@pid
3234 * if either directory is present flushes it and all of it'ts children
3237 * It is safe and reasonable to cache /proc entries for a task until
3238 * that task exits. After that they just clog up the dcache with
3239 * useless entries, possibly causing useful dcache entries to be
3240 * flushed instead. This routine is proved to flush those useless
3241 * dcache entries at process exit time.
3243 * NOTE: This routine is just an optimization so it does not guarantee
3244 * that no dcache entries will exist at process exit time it
3245 * just makes it very unlikely that any will persist.
3248 void proc_flush_task(struct task_struct
*task
)
3251 struct pid
*pid
, *tgid
;
3254 pid
= task_pid(task
);
3255 tgid
= task_tgid(task
);
3257 for (i
= 0; i
<= pid
->level
; i
++) {
3258 upid
= &pid
->numbers
[i
];
3259 proc_flush_task_mnt(upid
->ns
->proc_mnt
, upid
->nr
,
3260 tgid
->numbers
[i
].nr
);
3263 upid
= &pid
->numbers
[pid
->level
];
3265 pid_ns_release_proc(upid
->ns
);
3268 static struct dentry
*proc_pid_instantiate(struct inode
*dir
,
3269 struct dentry
* dentry
,
3270 struct task_struct
*task
, const void *ptr
)
3272 struct dentry
*error
= ERR_PTR(-ENOENT
);
3273 struct inode
*inode
;
3275 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
3279 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
3280 inode
->i_op
= &proc_tgid_base_inode_operations
;
3281 inode
->i_fop
= &proc_tgid_base_operations
;
3282 inode
->i_flags
|=S_IMMUTABLE
;
3284 set_nlink(inode
, 2 + pid_entry_count_dirs(tgid_base_stuff
,
3285 ARRAY_SIZE(tgid_base_stuff
)));
3287 d_set_d_op(dentry
, &pid_dentry_operations
);
3289 d_add(dentry
, inode
);
3290 /* Close the race of the process dying before we return the dentry */
3291 if (pid_revalidate(dentry
, NULL
))
3297 struct dentry
*proc_pid_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
)
3299 struct dentry
*result
;
3300 struct task_struct
*task
;
3302 struct pid_namespace
*ns
;
3304 result
= proc_base_lookup(dir
, dentry
);
3305 if (!IS_ERR(result
) || PTR_ERR(result
) != -ENOENT
)
3308 tgid
= name_to_int(dentry
);
3312 ns
= dentry
->d_sb
->s_fs_info
;
3314 task
= find_task_by_pid_ns(tgid
, ns
);
3316 get_task_struct(task
);
3321 result
= proc_pid_instantiate(dir
, dentry
, task
, NULL
);
3322 put_task_struct(task
);
3328 * Find the first task with tgid >= tgid
3333 struct task_struct
*task
;
3335 static struct tgid_iter
next_tgid(struct pid_namespace
*ns
, struct tgid_iter iter
)
3340 put_task_struct(iter
.task
);
3344 pid
= find_ge_pid(iter
.tgid
, ns
);
3346 iter
.tgid
= pid_nr_ns(pid
, ns
);
3347 iter
.task
= pid_task(pid
, PIDTYPE_PID
);
3348 /* What we to know is if the pid we have find is the
3349 * pid of a thread_group_leader. Testing for task
3350 * being a thread_group_leader is the obvious thing
3351 * todo but there is a window when it fails, due to
3352 * the pid transfer logic in de_thread.
3354 * So we perform the straight forward test of seeing
3355 * if the pid we have found is the pid of a thread
3356 * group leader, and don't worry if the task we have
3357 * found doesn't happen to be a thread group leader.
3358 * As we don't care in the case of readdir.
3360 if (!iter
.task
|| !has_group_leader_pid(iter
.task
)) {
3364 get_task_struct(iter
.task
);
3370 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
3372 static int proc_pid_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
3373 struct tgid_iter iter
)
3375 char name
[PROC_NUMBUF
];
3376 int len
= snprintf(name
, sizeof(name
), "%d", iter
.tgid
);
3377 return proc_fill_cache(filp
, dirent
, filldir
, name
, len
,
3378 proc_pid_instantiate
, iter
.task
, NULL
);
3381 static int fake_filldir(void *buf
, const char *name
, int namelen
,
3382 loff_t offset
, u64 ino
, unsigned d_type
)
3387 /* for the /proc/ directory itself, after non-process stuff has been done */
3388 int proc_pid_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
3391 struct task_struct
*reaper
;
3392 struct tgid_iter iter
;
3393 struct pid_namespace
*ns
;
3394 filldir_t __filldir
;
3396 if (filp
->f_pos
>= PID_MAX_LIMIT
+ TGID_OFFSET
)
3398 nr
= filp
->f_pos
- FIRST_PROCESS_ENTRY
;
3400 reaper
= get_proc_task(filp
->f_path
.dentry
->d_inode
);
3404 for (; nr
< ARRAY_SIZE(proc_base_stuff
); filp
->f_pos
++, nr
++) {
3405 const struct pid_entry
*p
= &proc_base_stuff
[nr
];
3406 if (proc_base_fill_cache(filp
, dirent
, filldir
, reaper
, p
) < 0)
3410 ns
= filp
->f_dentry
->d_sb
->s_fs_info
;
3412 iter
.tgid
= filp
->f_pos
- TGID_OFFSET
;
3413 for (iter
= next_tgid(ns
, iter
);
3415 iter
.tgid
+= 1, iter
= next_tgid(ns
, iter
)) {
3416 if (has_pid_permissions(ns
, iter
.task
, 2))
3417 __filldir
= filldir
;
3419 __filldir
= fake_filldir
;
3421 filp
->f_pos
= iter
.tgid
+ TGID_OFFSET
;
3422 if (proc_pid_fill_cache(filp
, dirent
, __filldir
, iter
) < 0) {
3423 put_task_struct(iter
.task
);
3427 filp
->f_pos
= PID_MAX_LIMIT
+ TGID_OFFSET
;
3429 put_task_struct(reaper
);
3437 static const struct pid_entry tid_base_stuff
[] = {
3438 DIR("fd", S_IRUSR
|S_IXUSR
, proc_fd_inode_operations
, proc_fd_operations
),
3439 DIR("fdinfo", S_IRUSR
|S_IXUSR
, proc_fdinfo_inode_operations
, proc_fdinfo_operations
),
3440 DIR("ns", S_IRUSR
|S_IXUGO
, proc_ns_dir_inode_operations
, proc_ns_dir_operations
),
3441 REG("environ", S_IRUSR
, proc_environ_operations
),
3442 INF("auxv", S_IRUSR
, proc_pid_auxv
),
3443 ONE("status", S_IRUGO
, proc_pid_status
),
3444 ONE("personality", S_IRUGO
, proc_pid_personality
),
3445 INF("limits", S_IRUGO
, proc_pid_limits
),
3446 #ifdef CONFIG_SCHED_DEBUG
3447 REG("sched", S_IRUGO
|S_IWUSR
, proc_pid_sched_operations
),
3449 REG("comm", S_IRUGO
|S_IWUSR
, proc_pid_set_comm_operations
),
3450 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3451 INF("syscall", S_IRUGO
, proc_pid_syscall
),
3453 INF("cmdline", S_IRUGO
, proc_pid_cmdline
),
3454 ONE("stat", S_IRUGO
, proc_tid_stat
),
3455 ONE("statm", S_IRUGO
, proc_pid_statm
),
3456 REG("maps", S_IRUGO
, proc_maps_operations
),
3458 REG("numa_maps", S_IRUGO
, proc_numa_maps_operations
),
3460 REG("mem", S_IRUSR
|S_IWUSR
, proc_mem_operations
),
3461 LNK("cwd", proc_cwd_link
),
3462 LNK("root", proc_root_link
),
3463 LNK("exe", proc_exe_link
),
3464 REG("mounts", S_IRUGO
, proc_mounts_operations
),
3465 REG("mountinfo", S_IRUGO
, proc_mountinfo_operations
),
3466 #ifdef CONFIG_PROC_PAGE_MONITOR
3467 REG("clear_refs", S_IWUSR
, proc_clear_refs_operations
),
3468 REG("smaps", S_IRUGO
, proc_smaps_operations
),
3469 REG("pagemap", S_IRUGO
, proc_pagemap_operations
),
3471 #ifdef CONFIG_SECURITY
3472 DIR("attr", S_IRUGO
|S_IXUGO
, proc_attr_dir_inode_operations
, proc_attr_dir_operations
),
3474 #ifdef CONFIG_KALLSYMS
3475 INF("wchan", S_IRUGO
, proc_pid_wchan
),
3477 #ifdef CONFIG_STACKTRACE
3478 ONE("stack", S_IRUGO
, proc_pid_stack
),
3480 #ifdef CONFIG_SCHEDSTATS
3481 INF("schedstat", S_IRUGO
, proc_pid_schedstat
),
3483 #ifdef CONFIG_LATENCYTOP
3484 REG("latency", S_IRUGO
, proc_lstats_operations
),
3486 #ifdef CONFIG_PROC_PID_CPUSET
3487 REG("cpuset", S_IRUGO
, proc_cpuset_operations
),
3489 #ifdef CONFIG_CGROUPS
3490 REG("cgroup", S_IRUGO
, proc_cgroup_operations
),
3492 INF("oom_score", S_IRUGO
, proc_oom_score
),
3493 REG("oom_adj", S_IRUGO
|S_IWUSR
, proc_oom_adjust_operations
),
3494 REG("oom_score_adj", S_IRUGO
|S_IWUSR
, proc_oom_score_adj_operations
),
3495 #ifdef CONFIG_AUDITSYSCALL
3496 REG("loginuid", S_IWUSR
|S_IRUGO
, proc_loginuid_operations
),
3497 REG("sessionid", S_IRUGO
, proc_sessionid_operations
),
3499 #ifdef CONFIG_FAULT_INJECTION
3500 REG("make-it-fail", S_IRUGO
|S_IWUSR
, proc_fault_inject_operations
),
3502 #ifdef CONFIG_TASK_IO_ACCOUNTING
3503 INF("io", S_IRUSR
, proc_tid_io_accounting
),
3505 #ifdef CONFIG_HARDWALL
3506 INF("hardwall", S_IRUGO
, proc_pid_hardwall
),
3510 static int proc_tid_base_readdir(struct file
* filp
,
3511 void * dirent
, filldir_t filldir
)
3513 return proc_pident_readdir(filp
,dirent
,filldir
,
3514 tid_base_stuff
,ARRAY_SIZE(tid_base_stuff
));
3517 static struct dentry
*proc_tid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
){
3518 return proc_pident_lookup(dir
, dentry
,
3519 tid_base_stuff
, ARRAY_SIZE(tid_base_stuff
));
3522 static const struct file_operations proc_tid_base_operations
= {
3523 .read
= generic_read_dir
,
3524 .readdir
= proc_tid_base_readdir
,
3525 .llseek
= default_llseek
,
3528 static const struct inode_operations proc_tid_base_inode_operations
= {
3529 .lookup
= proc_tid_base_lookup
,
3530 .getattr
= pid_getattr
,
3531 .setattr
= proc_setattr
,
3534 static struct dentry
*proc_task_instantiate(struct inode
*dir
,
3535 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
3537 struct dentry
*error
= ERR_PTR(-ENOENT
);
3538 struct inode
*inode
;
3539 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
3543 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
3544 inode
->i_op
= &proc_tid_base_inode_operations
;
3545 inode
->i_fop
= &proc_tid_base_operations
;
3546 inode
->i_flags
|=S_IMMUTABLE
;
3548 set_nlink(inode
, 2 + pid_entry_count_dirs(tid_base_stuff
,
3549 ARRAY_SIZE(tid_base_stuff
)));
3551 d_set_d_op(dentry
, &pid_dentry_operations
);
3553 d_add(dentry
, inode
);
3554 /* Close the race of the process dying before we return the dentry */
3555 if (pid_revalidate(dentry
, NULL
))
3561 static struct dentry
*proc_task_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
)
3563 struct dentry
*result
= ERR_PTR(-ENOENT
);
3564 struct task_struct
*task
;
3565 struct task_struct
*leader
= get_proc_task(dir
);
3567 struct pid_namespace
*ns
;
3572 tid
= name_to_int(dentry
);
3576 ns
= dentry
->d_sb
->s_fs_info
;
3578 task
= find_task_by_pid_ns(tid
, ns
);
3580 get_task_struct(task
);
3584 if (!same_thread_group(leader
, task
))
3587 result
= proc_task_instantiate(dir
, dentry
, task
, NULL
);
3589 put_task_struct(task
);
3591 put_task_struct(leader
);
3597 * Find the first tid of a thread group to return to user space.
3599 * Usually this is just the thread group leader, but if the users
3600 * buffer was too small or there was a seek into the middle of the
3601 * directory we have more work todo.
3603 * In the case of a short read we start with find_task_by_pid.
3605 * In the case of a seek we start with the leader and walk nr
3608 static struct task_struct
*first_tid(struct task_struct
*leader
,
3609 int tid
, int nr
, struct pid_namespace
*ns
)
3611 struct task_struct
*pos
;
3614 /* Attempt to start with the pid of a thread */
3615 if (tid
&& (nr
> 0)) {
3616 pos
= find_task_by_pid_ns(tid
, ns
);
3617 if (pos
&& (pos
->group_leader
== leader
))
3621 /* If nr exceeds the number of threads there is nothing todo */
3623 if (nr
&& nr
>= get_nr_threads(leader
))
3626 /* If we haven't found our starting place yet start
3627 * with the leader and walk nr threads forward.
3629 for (pos
= leader
; nr
> 0; --nr
) {
3630 pos
= next_thread(pos
);
3631 if (pos
== leader
) {
3637 get_task_struct(pos
);
3644 * Find the next thread in the thread list.
3645 * Return NULL if there is an error or no next thread.
3647 * The reference to the input task_struct is released.
3649 static struct task_struct
*next_tid(struct task_struct
*start
)
3651 struct task_struct
*pos
= NULL
;
3653 if (pid_alive(start
)) {
3654 pos
= next_thread(start
);
3655 if (thread_group_leader(pos
))
3658 get_task_struct(pos
);
3661 put_task_struct(start
);
3665 static int proc_task_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
3666 struct task_struct
*task
, int tid
)
3668 char name
[PROC_NUMBUF
];
3669 int len
= snprintf(name
, sizeof(name
), "%d", tid
);
3670 return proc_fill_cache(filp
, dirent
, filldir
, name
, len
,
3671 proc_task_instantiate
, task
, NULL
);
3674 /* for the /proc/TGID/task/ directories */
3675 static int proc_task_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
3677 struct dentry
*dentry
= filp
->f_path
.dentry
;
3678 struct inode
*inode
= dentry
->d_inode
;
3679 struct task_struct
*leader
= NULL
;
3680 struct task_struct
*task
;
3681 int retval
= -ENOENT
;
3684 struct pid_namespace
*ns
;
3686 task
= get_proc_task(inode
);
3690 if (pid_alive(task
)) {
3691 leader
= task
->group_leader
;
3692 get_task_struct(leader
);
3695 put_task_struct(task
);
3700 switch ((unsigned long)filp
->f_pos
) {
3703 if (filldir(dirent
, ".", 1, filp
->f_pos
, ino
, DT_DIR
) < 0)
3708 ino
= parent_ino(dentry
);
3709 if (filldir(dirent
, "..", 2, filp
->f_pos
, ino
, DT_DIR
) < 0)
3715 /* f_version caches the tgid value that the last readdir call couldn't
3716 * return. lseek aka telldir automagically resets f_version to 0.
3718 ns
= filp
->f_dentry
->d_sb
->s_fs_info
;
3719 tid
= (int)filp
->f_version
;
3720 filp
->f_version
= 0;
3721 for (task
= first_tid(leader
, tid
, filp
->f_pos
- 2, ns
);
3723 task
= next_tid(task
), filp
->f_pos
++) {
3724 tid
= task_pid_nr_ns(task
, ns
);
3725 if (proc_task_fill_cache(filp
, dirent
, filldir
, task
, tid
) < 0) {
3726 /* returning this tgid failed, save it as the first
3727 * pid for the next readir call */
3728 filp
->f_version
= (u64
)tid
;
3729 put_task_struct(task
);
3734 put_task_struct(leader
);
3739 static int proc_task_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
3741 struct inode
*inode
= dentry
->d_inode
;
3742 struct task_struct
*p
= get_proc_task(inode
);
3743 generic_fillattr(inode
, stat
);
3746 stat
->nlink
+= get_nr_threads(p
);
3753 static const struct inode_operations proc_task_inode_operations
= {
3754 .lookup
= proc_task_lookup
,
3755 .getattr
= proc_task_getattr
,
3756 .setattr
= proc_setattr
,
3757 .permission
= proc_pid_permission
,
3760 static const struct file_operations proc_task_operations
= {
3761 .read
= generic_read_dir
,
3762 .readdir
= proc_task_readdir
,
3763 .llseek
= default_llseek
,