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/rcupdate.h>
67 #include <linux/kallsyms.h>
68 #include <linux/resource.h>
69 #include <linux/module.h>
70 #include <linux/mount.h>
71 #include <linux/security.h>
72 #include <linux/ptrace.h>
73 #include <linux/tracehook.h>
74 #include <linux/cgroup.h>
75 #include <linux/cpuset.h>
76 #include <linux/audit.h>
77 #include <linux/poll.h>
78 #include <linux/nsproxy.h>
79 #include <linux/oom.h>
80 #include <linux/elf.h>
81 #include <linux/pid_namespace.h>
85 * Implementing inode permission operations in /proc is almost
86 * certainly an error. Permission checks need to happen during
87 * each system call not at open time. The reason is that most of
88 * what we wish to check for permissions in /proc varies at runtime.
90 * The classic example of a problem is opening file descriptors
91 * in /proc for a task before it execs a suid executable.
98 const struct inode_operations
*iop
;
99 const struct file_operations
*fop
;
103 #define NOD(NAME, MODE, IOP, FOP, OP) { \
105 .len = sizeof(NAME) - 1, \
112 #define DIR(NAME, MODE, OTYPE) \
113 NOD(NAME, (S_IFDIR|(MODE)), \
114 &proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations, \
116 #define LNK(NAME, OTYPE) \
117 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
118 &proc_pid_link_inode_operations, NULL, \
119 { .proc_get_link = &proc_##OTYPE##_link } )
120 #define REG(NAME, MODE, OTYPE) \
121 NOD(NAME, (S_IFREG|(MODE)), NULL, \
122 &proc_##OTYPE##_operations, {})
123 #define INF(NAME, MODE, OTYPE) \
124 NOD(NAME, (S_IFREG|(MODE)), \
125 NULL, &proc_info_file_operations, \
126 { .proc_read = &proc_##OTYPE } )
127 #define ONE(NAME, MODE, OTYPE) \
128 NOD(NAME, (S_IFREG|(MODE)), \
129 NULL, &proc_single_file_operations, \
130 { .proc_show = &proc_##OTYPE } )
133 * Count the number of hardlinks for the pid_entry table, excluding the .
136 static unsigned int pid_entry_count_dirs(const struct pid_entry
*entries
,
143 for (i
= 0; i
< n
; ++i
) {
144 if (S_ISDIR(entries
[i
].mode
))
152 EXPORT_SYMBOL(maps_protect
);
154 static struct fs_struct
*get_fs_struct(struct task_struct
*task
)
156 struct fs_struct
*fs
;
160 atomic_inc(&fs
->count
);
165 static int get_nr_threads(struct task_struct
*tsk
)
167 /* Must be called with the rcu_read_lock held */
171 if (lock_task_sighand(tsk
, &flags
)) {
172 count
= atomic_read(&tsk
->signal
->count
);
173 unlock_task_sighand(tsk
, &flags
);
178 static int proc_cwd_link(struct inode
*inode
, struct path
*path
)
180 struct task_struct
*task
= get_proc_task(inode
);
181 struct fs_struct
*fs
= NULL
;
182 int result
= -ENOENT
;
185 fs
= get_fs_struct(task
);
186 put_task_struct(task
);
189 read_lock(&fs
->lock
);
192 read_unlock(&fs
->lock
);
199 static int proc_root_link(struct inode
*inode
, struct path
*path
)
201 struct task_struct
*task
= get_proc_task(inode
);
202 struct fs_struct
*fs
= NULL
;
203 int result
= -ENOENT
;
206 fs
= get_fs_struct(task
);
207 put_task_struct(task
);
210 read_lock(&fs
->lock
);
213 read_unlock(&fs
->lock
);
221 * Return zero if current may access user memory in @task, -error if not.
223 static int check_mem_permission(struct task_struct
*task
)
226 * A task can always look at itself, in case it chooses
227 * to use system calls instead of load instructions.
233 * If current is actively ptrace'ing, and would also be
234 * permitted to freshly attach with ptrace now, permit it.
236 if (task_is_stopped_or_traced(task
)) {
239 match
= (tracehook_tracer_task(task
) == current
);
241 if (match
&& ptrace_may_access(task
, PTRACE_MODE_ATTACH
))
246 * Noone else is allowed.
251 struct mm_struct
*mm_for_maps(struct task_struct
*task
)
253 struct mm_struct
*mm
= get_task_mm(task
);
256 down_read(&mm
->mmap_sem
);
260 if (task
->mm
!= current
->mm
&&
261 __ptrace_may_access(task
, PTRACE_MODE_READ
) < 0)
267 up_read(&mm
->mmap_sem
);
272 static int proc_pid_cmdline(struct task_struct
*task
, char * buffer
)
276 struct mm_struct
*mm
= get_task_mm(task
);
280 goto out_mm
; /* Shh! No looking before we're done */
282 len
= mm
->arg_end
- mm
->arg_start
;
287 res
= access_process_vm(task
, mm
->arg_start
, buffer
, len
, 0);
289 // If the nul at the end of args has been overwritten, then
290 // assume application is using setproctitle(3).
291 if (res
> 0 && buffer
[res
-1] != '\0' && len
< PAGE_SIZE
) {
292 len
= strnlen(buffer
, res
);
296 len
= mm
->env_end
- mm
->env_start
;
297 if (len
> PAGE_SIZE
- res
)
298 len
= PAGE_SIZE
- res
;
299 res
+= access_process_vm(task
, mm
->env_start
, buffer
+res
, len
, 0);
300 res
= strnlen(buffer
, res
);
309 static int proc_pid_auxv(struct task_struct
*task
, char *buffer
)
312 struct mm_struct
*mm
= get_task_mm(task
);
314 unsigned int nwords
= 0;
317 while (mm
->saved_auxv
[nwords
- 2] != 0); /* AT_NULL */
318 res
= nwords
* sizeof(mm
->saved_auxv
[0]);
321 memcpy(buffer
, mm
->saved_auxv
, res
);
328 #ifdef CONFIG_KALLSYMS
330 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
331 * Returns the resolved symbol. If that fails, simply return the address.
333 static int proc_pid_wchan(struct task_struct
*task
, char *buffer
)
336 char symname
[KSYM_NAME_LEN
];
338 wchan
= get_wchan(task
);
340 if (lookup_symbol_name(wchan
, symname
) < 0)
341 return sprintf(buffer
, "%lu", wchan
);
343 return sprintf(buffer
, "%s", symname
);
345 #endif /* CONFIG_KALLSYMS */
347 #ifdef CONFIG_SCHEDSTATS
349 * Provides /proc/PID/schedstat
351 static int proc_pid_schedstat(struct task_struct
*task
, char *buffer
)
353 return sprintf(buffer
, "%llu %llu %lu\n",
354 task
->sched_info
.cpu_time
,
355 task
->sched_info
.run_delay
,
356 task
->sched_info
.pcount
);
360 #ifdef CONFIG_LATENCYTOP
361 static int lstats_show_proc(struct seq_file
*m
, void *v
)
364 struct inode
*inode
= m
->private;
365 struct task_struct
*task
= get_proc_task(inode
);
369 seq_puts(m
, "Latency Top version : v0.1\n");
370 for (i
= 0; i
< 32; i
++) {
371 if (task
->latency_record
[i
].backtrace
[0]) {
373 seq_printf(m
, "%i %li %li ",
374 task
->latency_record
[i
].count
,
375 task
->latency_record
[i
].time
,
376 task
->latency_record
[i
].max
);
377 for (q
= 0; q
< LT_BACKTRACEDEPTH
; q
++) {
378 char sym
[KSYM_NAME_LEN
];
380 if (!task
->latency_record
[i
].backtrace
[q
])
382 if (task
->latency_record
[i
].backtrace
[q
] == ULONG_MAX
)
384 sprint_symbol(sym
, task
->latency_record
[i
].backtrace
[q
]);
385 c
= strchr(sym
, '+');
388 seq_printf(m
, "%s ", sym
);
394 put_task_struct(task
);
398 static int lstats_open(struct inode
*inode
, struct file
*file
)
400 return single_open(file
, lstats_show_proc
, inode
);
403 static ssize_t
lstats_write(struct file
*file
, const char __user
*buf
,
404 size_t count
, loff_t
*offs
)
406 struct task_struct
*task
= get_proc_task(file
->f_dentry
->d_inode
);
410 clear_all_latency_tracing(task
);
411 put_task_struct(task
);
416 static const struct file_operations proc_lstats_operations
= {
419 .write
= lstats_write
,
421 .release
= single_release
,
426 /* The badness from the OOM killer */
427 unsigned long badness(struct task_struct
*p
, unsigned long uptime
);
428 static int proc_oom_score(struct task_struct
*task
, char *buffer
)
430 unsigned long points
;
431 struct timespec uptime
;
433 do_posix_clock_monotonic_gettime(&uptime
);
434 read_lock(&tasklist_lock
);
435 points
= badness(task
, uptime
.tv_sec
);
436 read_unlock(&tasklist_lock
);
437 return sprintf(buffer
, "%lu\n", points
);
445 static const struct limit_names lnames
[RLIM_NLIMITS
] = {
446 [RLIMIT_CPU
] = {"Max cpu time", "ms"},
447 [RLIMIT_FSIZE
] = {"Max file size", "bytes"},
448 [RLIMIT_DATA
] = {"Max data size", "bytes"},
449 [RLIMIT_STACK
] = {"Max stack size", "bytes"},
450 [RLIMIT_CORE
] = {"Max core file size", "bytes"},
451 [RLIMIT_RSS
] = {"Max resident set", "bytes"},
452 [RLIMIT_NPROC
] = {"Max processes", "processes"},
453 [RLIMIT_NOFILE
] = {"Max open files", "files"},
454 [RLIMIT_MEMLOCK
] = {"Max locked memory", "bytes"},
455 [RLIMIT_AS
] = {"Max address space", "bytes"},
456 [RLIMIT_LOCKS
] = {"Max file locks", "locks"},
457 [RLIMIT_SIGPENDING
] = {"Max pending signals", "signals"},
458 [RLIMIT_MSGQUEUE
] = {"Max msgqueue size", "bytes"},
459 [RLIMIT_NICE
] = {"Max nice priority", NULL
},
460 [RLIMIT_RTPRIO
] = {"Max realtime priority", NULL
},
461 [RLIMIT_RTTIME
] = {"Max realtime timeout", "us"},
464 /* Display limits for a process */
465 static int proc_pid_limits(struct task_struct
*task
, char *buffer
)
470 char *bufptr
= buffer
;
472 struct rlimit rlim
[RLIM_NLIMITS
];
475 if (!lock_task_sighand(task
,&flags
)) {
479 memcpy(rlim
, task
->signal
->rlim
, sizeof(struct rlimit
) * RLIM_NLIMITS
);
480 unlock_task_sighand(task
, &flags
);
484 * print the file header
486 count
+= sprintf(&bufptr
[count
], "%-25s %-20s %-20s %-10s\n",
487 "Limit", "Soft Limit", "Hard Limit", "Units");
489 for (i
= 0; i
< RLIM_NLIMITS
; i
++) {
490 if (rlim
[i
].rlim_cur
== RLIM_INFINITY
)
491 count
+= sprintf(&bufptr
[count
], "%-25s %-20s ",
492 lnames
[i
].name
, "unlimited");
494 count
+= sprintf(&bufptr
[count
], "%-25s %-20lu ",
495 lnames
[i
].name
, rlim
[i
].rlim_cur
);
497 if (rlim
[i
].rlim_max
== RLIM_INFINITY
)
498 count
+= sprintf(&bufptr
[count
], "%-20s ", "unlimited");
500 count
+= sprintf(&bufptr
[count
], "%-20lu ",
504 count
+= sprintf(&bufptr
[count
], "%-10s\n",
507 count
+= sprintf(&bufptr
[count
], "\n");
513 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
514 static int proc_pid_syscall(struct task_struct
*task
, char *buffer
)
517 unsigned long args
[6], sp
, pc
;
519 if (task_current_syscall(task
, &nr
, args
, 6, &sp
, &pc
))
520 return sprintf(buffer
, "running\n");
523 return sprintf(buffer
, "%ld 0x%lx 0x%lx\n", nr
, sp
, pc
);
525 return sprintf(buffer
,
526 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
528 args
[0], args
[1], args
[2], args
[3], args
[4], args
[5],
531 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
533 /************************************************************************/
534 /* Here the fs part begins */
535 /************************************************************************/
537 /* permission checks */
538 static int proc_fd_access_allowed(struct inode
*inode
)
540 struct task_struct
*task
;
542 /* Allow access to a task's file descriptors if it is us or we
543 * may use ptrace attach to the process and find out that
546 task
= get_proc_task(inode
);
548 allowed
= ptrace_may_access(task
, PTRACE_MODE_READ
);
549 put_task_struct(task
);
554 static int proc_setattr(struct dentry
*dentry
, struct iattr
*attr
)
557 struct inode
*inode
= dentry
->d_inode
;
559 if (attr
->ia_valid
& ATTR_MODE
)
562 error
= inode_change_ok(inode
, attr
);
564 error
= inode_setattr(inode
, attr
);
568 static const struct inode_operations proc_def_inode_operations
= {
569 .setattr
= proc_setattr
,
572 static int mounts_open_common(struct inode
*inode
, struct file
*file
,
573 const struct seq_operations
*op
)
575 struct task_struct
*task
= get_proc_task(inode
);
577 struct mnt_namespace
*ns
= NULL
;
578 struct fs_struct
*fs
= NULL
;
580 struct proc_mounts
*p
;
585 nsp
= task_nsproxy(task
);
593 fs
= get_fs_struct(task
);
594 put_task_struct(task
);
602 read_lock(&fs
->lock
);
605 read_unlock(&fs
->lock
);
609 p
= kmalloc(sizeof(struct proc_mounts
), GFP_KERNEL
);
613 file
->private_data
= &p
->m
;
614 ret
= seq_open(file
, op
);
621 p
->event
= ns
->event
;
635 static int mounts_release(struct inode
*inode
, struct file
*file
)
637 struct proc_mounts
*p
= file
->private_data
;
640 return seq_release(inode
, file
);
643 static unsigned mounts_poll(struct file
*file
, poll_table
*wait
)
645 struct proc_mounts
*p
= file
->private_data
;
646 struct mnt_namespace
*ns
= p
->ns
;
649 poll_wait(file
, &ns
->poll
, wait
);
651 spin_lock(&vfsmount_lock
);
652 if (p
->event
!= ns
->event
) {
653 p
->event
= ns
->event
;
656 spin_unlock(&vfsmount_lock
);
661 static int mounts_open(struct inode
*inode
, struct file
*file
)
663 return mounts_open_common(inode
, file
, &mounts_op
);
666 static const struct file_operations proc_mounts_operations
= {
670 .release
= mounts_release
,
674 static int mountinfo_open(struct inode
*inode
, struct file
*file
)
676 return mounts_open_common(inode
, file
, &mountinfo_op
);
679 static const struct file_operations proc_mountinfo_operations
= {
680 .open
= mountinfo_open
,
683 .release
= mounts_release
,
687 static int mountstats_open(struct inode
*inode
, struct file
*file
)
689 return mounts_open_common(inode
, file
, &mountstats_op
);
692 static const struct file_operations proc_mountstats_operations
= {
693 .open
= mountstats_open
,
696 .release
= mounts_release
,
699 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
701 static ssize_t
proc_info_read(struct file
* file
, char __user
* buf
,
702 size_t count
, loff_t
*ppos
)
704 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
707 struct task_struct
*task
= get_proc_task(inode
);
713 if (count
> PROC_BLOCK_SIZE
)
714 count
= PROC_BLOCK_SIZE
;
717 if (!(page
= __get_free_page(GFP_TEMPORARY
)))
720 length
= PROC_I(inode
)->op
.proc_read(task
, (char*)page
);
723 length
= simple_read_from_buffer(buf
, count
, ppos
, (char *)page
, length
);
726 put_task_struct(task
);
731 static const struct file_operations proc_info_file_operations
= {
732 .read
= proc_info_read
,
735 static int proc_single_show(struct seq_file
*m
, void *v
)
737 struct inode
*inode
= m
->private;
738 struct pid_namespace
*ns
;
740 struct task_struct
*task
;
743 ns
= inode
->i_sb
->s_fs_info
;
744 pid
= proc_pid(inode
);
745 task
= get_pid_task(pid
, PIDTYPE_PID
);
749 ret
= PROC_I(inode
)->op
.proc_show(m
, ns
, pid
, task
);
751 put_task_struct(task
);
755 static int proc_single_open(struct inode
*inode
, struct file
*filp
)
758 ret
= single_open(filp
, proc_single_show
, NULL
);
760 struct seq_file
*m
= filp
->private_data
;
767 static const struct file_operations proc_single_file_operations
= {
768 .open
= proc_single_open
,
771 .release
= single_release
,
774 static int mem_open(struct inode
* inode
, struct file
* file
)
776 file
->private_data
= (void*)((long)current
->self_exec_id
);
780 static ssize_t
mem_read(struct file
* file
, char __user
* buf
,
781 size_t count
, loff_t
*ppos
)
783 struct task_struct
*task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
785 unsigned long src
= *ppos
;
787 struct mm_struct
*mm
;
792 if (check_mem_permission(task
))
796 page
= (char *)__get_free_page(GFP_TEMPORARY
);
802 mm
= get_task_mm(task
);
808 if (file
->private_data
!= (void*)((long)current
->self_exec_id
))
814 int this_len
, retval
;
816 this_len
= (count
> PAGE_SIZE
) ? PAGE_SIZE
: count
;
817 retval
= access_process_vm(task
, src
, page
, this_len
, 0);
818 if (!retval
|| check_mem_permission(task
)) {
824 if (copy_to_user(buf
, page
, retval
)) {
839 free_page((unsigned long) page
);
841 put_task_struct(task
);
846 #define mem_write NULL
849 /* This is a security hazard */
850 static ssize_t
mem_write(struct file
* file
, const char __user
*buf
,
851 size_t count
, loff_t
*ppos
)
855 struct task_struct
*task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
856 unsigned long dst
= *ppos
;
862 if (check_mem_permission(task
))
866 page
= (char *)__get_free_page(GFP_TEMPORARY
);
872 int this_len
, retval
;
874 this_len
= (count
> PAGE_SIZE
) ? PAGE_SIZE
: count
;
875 if (copy_from_user(page
, buf
, this_len
)) {
879 retval
= access_process_vm(task
, dst
, page
, this_len
, 1);
891 free_page((unsigned long) page
);
893 put_task_struct(task
);
899 loff_t
mem_lseek(struct file
*file
, loff_t offset
, int orig
)
903 file
->f_pos
= offset
;
906 file
->f_pos
+= offset
;
911 force_successful_syscall_return();
915 static const struct file_operations proc_mem_operations
= {
922 static ssize_t
environ_read(struct file
*file
, char __user
*buf
,
923 size_t count
, loff_t
*ppos
)
925 struct task_struct
*task
= get_proc_task(file
->f_dentry
->d_inode
);
927 unsigned long src
= *ppos
;
929 struct mm_struct
*mm
;
934 if (!ptrace_may_access(task
, PTRACE_MODE_READ
))
938 page
= (char *)__get_free_page(GFP_TEMPORARY
);
944 mm
= get_task_mm(task
);
949 int this_len
, retval
, max_len
;
951 this_len
= mm
->env_end
- (mm
->env_start
+ src
);
956 max_len
= (count
> PAGE_SIZE
) ? PAGE_SIZE
: count
;
957 this_len
= (this_len
> max_len
) ? max_len
: this_len
;
959 retval
= access_process_vm(task
, (mm
->env_start
+ src
),
967 if (copy_to_user(buf
, page
, retval
)) {
981 free_page((unsigned long) page
);
983 put_task_struct(task
);
988 static const struct file_operations proc_environ_operations
= {
989 .read
= environ_read
,
992 static ssize_t
oom_adjust_read(struct file
*file
, char __user
*buf
,
993 size_t count
, loff_t
*ppos
)
995 struct task_struct
*task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
996 char buffer
[PROC_NUMBUF
];
1002 oom_adjust
= task
->oomkilladj
;
1003 put_task_struct(task
);
1005 len
= snprintf(buffer
, sizeof(buffer
), "%i\n", oom_adjust
);
1007 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
1010 static ssize_t
oom_adjust_write(struct file
*file
, const char __user
*buf
,
1011 size_t count
, loff_t
*ppos
)
1013 struct task_struct
*task
;
1014 char buffer
[PROC_NUMBUF
], *end
;
1017 memset(buffer
, 0, sizeof(buffer
));
1018 if (count
> sizeof(buffer
) - 1)
1019 count
= sizeof(buffer
) - 1;
1020 if (copy_from_user(buffer
, buf
, count
))
1022 oom_adjust
= simple_strtol(buffer
, &end
, 0);
1023 if ((oom_adjust
< OOM_ADJUST_MIN
|| oom_adjust
> OOM_ADJUST_MAX
) &&
1024 oom_adjust
!= OOM_DISABLE
)
1028 task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
1031 if (oom_adjust
< task
->oomkilladj
&& !capable(CAP_SYS_RESOURCE
)) {
1032 put_task_struct(task
);
1035 task
->oomkilladj
= oom_adjust
;
1036 put_task_struct(task
);
1037 if (end
- buffer
== 0)
1039 return end
- buffer
;
1042 static const struct file_operations proc_oom_adjust_operations
= {
1043 .read
= oom_adjust_read
,
1044 .write
= oom_adjust_write
,
1047 #ifdef CONFIG_AUDITSYSCALL
1048 #define TMPBUFLEN 21
1049 static ssize_t
proc_loginuid_read(struct file
* file
, char __user
* buf
,
1050 size_t count
, loff_t
*ppos
)
1052 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
1053 struct task_struct
*task
= get_proc_task(inode
);
1055 char tmpbuf
[TMPBUFLEN
];
1059 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
1060 audit_get_loginuid(task
));
1061 put_task_struct(task
);
1062 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1065 static ssize_t
proc_loginuid_write(struct file
* file
, const char __user
* buf
,
1066 size_t count
, loff_t
*ppos
)
1068 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
1073 if (!capable(CAP_AUDIT_CONTROL
))
1076 if (current
!= pid_task(proc_pid(inode
), PIDTYPE_PID
))
1079 if (count
>= PAGE_SIZE
)
1080 count
= PAGE_SIZE
- 1;
1083 /* No partial writes. */
1086 page
= (char*)__get_free_page(GFP_TEMPORARY
);
1090 if (copy_from_user(page
, buf
, count
))
1094 loginuid
= simple_strtoul(page
, &tmp
, 10);
1100 length
= audit_set_loginuid(current
, loginuid
);
1101 if (likely(length
== 0))
1105 free_page((unsigned long) page
);
1109 static const struct file_operations proc_loginuid_operations
= {
1110 .read
= proc_loginuid_read
,
1111 .write
= proc_loginuid_write
,
1114 static ssize_t
proc_sessionid_read(struct file
* file
, char __user
* buf
,
1115 size_t count
, loff_t
*ppos
)
1117 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
1118 struct task_struct
*task
= get_proc_task(inode
);
1120 char tmpbuf
[TMPBUFLEN
];
1124 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
1125 audit_get_sessionid(task
));
1126 put_task_struct(task
);
1127 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1130 static const struct file_operations proc_sessionid_operations
= {
1131 .read
= proc_sessionid_read
,
1135 #ifdef CONFIG_FAULT_INJECTION
1136 static ssize_t
proc_fault_inject_read(struct file
* file
, char __user
* buf
,
1137 size_t count
, loff_t
*ppos
)
1139 struct task_struct
*task
= get_proc_task(file
->f_dentry
->d_inode
);
1140 char buffer
[PROC_NUMBUF
];
1146 make_it_fail
= task
->make_it_fail
;
1147 put_task_struct(task
);
1149 len
= snprintf(buffer
, sizeof(buffer
), "%i\n", make_it_fail
);
1151 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
1154 static ssize_t
proc_fault_inject_write(struct file
* file
,
1155 const char __user
* buf
, size_t count
, loff_t
*ppos
)
1157 struct task_struct
*task
;
1158 char buffer
[PROC_NUMBUF
], *end
;
1161 if (!capable(CAP_SYS_RESOURCE
))
1163 memset(buffer
, 0, sizeof(buffer
));
1164 if (count
> sizeof(buffer
) - 1)
1165 count
= sizeof(buffer
) - 1;
1166 if (copy_from_user(buffer
, buf
, count
))
1168 make_it_fail
= simple_strtol(buffer
, &end
, 0);
1171 task
= get_proc_task(file
->f_dentry
->d_inode
);
1174 task
->make_it_fail
= make_it_fail
;
1175 put_task_struct(task
);
1176 if (end
- buffer
== 0)
1178 return end
- buffer
;
1181 static const struct file_operations proc_fault_inject_operations
= {
1182 .read
= proc_fault_inject_read
,
1183 .write
= proc_fault_inject_write
,
1188 #ifdef CONFIG_SCHED_DEBUG
1190 * Print out various scheduling related per-task fields:
1192 static int sched_show(struct seq_file
*m
, void *v
)
1194 struct inode
*inode
= m
->private;
1195 struct task_struct
*p
;
1199 p
= get_proc_task(inode
);
1202 proc_sched_show_task(p
, m
);
1210 sched_write(struct file
*file
, const char __user
*buf
,
1211 size_t count
, loff_t
*offset
)
1213 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1214 struct task_struct
*p
;
1218 p
= get_proc_task(inode
);
1221 proc_sched_set_task(p
);
1228 static int sched_open(struct inode
*inode
, struct file
*filp
)
1232 ret
= single_open(filp
, sched_show
, NULL
);
1234 struct seq_file
*m
= filp
->private_data
;
1241 static const struct file_operations proc_pid_sched_operations
= {
1244 .write
= sched_write
,
1245 .llseek
= seq_lseek
,
1246 .release
= single_release
,
1252 * We added or removed a vma mapping the executable. The vmas are only mapped
1253 * during exec and are not mapped with the mmap system call.
1254 * Callers must hold down_write() on the mm's mmap_sem for these
1256 void added_exe_file_vma(struct mm_struct
*mm
)
1258 mm
->num_exe_file_vmas
++;
1261 void removed_exe_file_vma(struct mm_struct
*mm
)
1263 mm
->num_exe_file_vmas
--;
1264 if ((mm
->num_exe_file_vmas
== 0) && mm
->exe_file
){
1266 mm
->exe_file
= NULL
;
1271 void set_mm_exe_file(struct mm_struct
*mm
, struct file
*new_exe_file
)
1274 get_file(new_exe_file
);
1277 mm
->exe_file
= new_exe_file
;
1278 mm
->num_exe_file_vmas
= 0;
1281 struct file
*get_mm_exe_file(struct mm_struct
*mm
)
1283 struct file
*exe_file
;
1285 /* We need mmap_sem to protect against races with removal of
1286 * VM_EXECUTABLE vmas */
1287 down_read(&mm
->mmap_sem
);
1288 exe_file
= mm
->exe_file
;
1291 up_read(&mm
->mmap_sem
);
1295 void dup_mm_exe_file(struct mm_struct
*oldmm
, struct mm_struct
*newmm
)
1297 /* It's safe to write the exe_file pointer without exe_file_lock because
1298 * this is called during fork when the task is not yet in /proc */
1299 newmm
->exe_file
= get_mm_exe_file(oldmm
);
1302 static int proc_exe_link(struct inode
*inode
, struct path
*exe_path
)
1304 struct task_struct
*task
;
1305 struct mm_struct
*mm
;
1306 struct file
*exe_file
;
1308 task
= get_proc_task(inode
);
1311 mm
= get_task_mm(task
);
1312 put_task_struct(task
);
1315 exe_file
= get_mm_exe_file(mm
);
1318 *exe_path
= exe_file
->f_path
;
1319 path_get(&exe_file
->f_path
);
1326 static void *proc_pid_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
1328 struct inode
*inode
= dentry
->d_inode
;
1329 int error
= -EACCES
;
1331 /* We don't need a base pointer in the /proc filesystem */
1332 path_put(&nd
->path
);
1334 /* Are we allowed to snoop on the tasks file descriptors? */
1335 if (!proc_fd_access_allowed(inode
))
1338 error
= PROC_I(inode
)->op
.proc_get_link(inode
, &nd
->path
);
1339 nd
->last_type
= LAST_BIND
;
1341 return ERR_PTR(error
);
1344 static int do_proc_readlink(struct path
*path
, char __user
*buffer
, int buflen
)
1346 char *tmp
= (char*)__get_free_page(GFP_TEMPORARY
);
1353 pathname
= d_path(path
, tmp
, PAGE_SIZE
);
1354 len
= PTR_ERR(pathname
);
1355 if (IS_ERR(pathname
))
1357 len
= tmp
+ PAGE_SIZE
- 1 - pathname
;
1361 if (copy_to_user(buffer
, pathname
, len
))
1364 free_page((unsigned long)tmp
);
1368 static int proc_pid_readlink(struct dentry
* dentry
, char __user
* buffer
, int buflen
)
1370 int error
= -EACCES
;
1371 struct inode
*inode
= dentry
->d_inode
;
1374 /* Are we allowed to snoop on the tasks file descriptors? */
1375 if (!proc_fd_access_allowed(inode
))
1378 error
= PROC_I(inode
)->op
.proc_get_link(inode
, &path
);
1382 error
= do_proc_readlink(&path
, buffer
, buflen
);
1388 static const struct inode_operations proc_pid_link_inode_operations
= {
1389 .readlink
= proc_pid_readlink
,
1390 .follow_link
= proc_pid_follow_link
,
1391 .setattr
= proc_setattr
,
1395 /* building an inode */
1397 static int task_dumpable(struct task_struct
*task
)
1400 struct mm_struct
*mm
;
1405 dumpable
= get_dumpable(mm
);
1413 static struct inode
*proc_pid_make_inode(struct super_block
* sb
, struct task_struct
*task
)
1415 struct inode
* inode
;
1416 struct proc_inode
*ei
;
1418 /* We need a new inode */
1420 inode
= new_inode(sb
);
1426 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1427 inode
->i_op
= &proc_def_inode_operations
;
1430 * grab the reference to task.
1432 ei
->pid
= get_task_pid(task
, PIDTYPE_PID
);
1438 if (task_dumpable(task
)) {
1439 inode
->i_uid
= task
->euid
;
1440 inode
->i_gid
= task
->egid
;
1442 security_task_to_inode(task
, inode
);
1452 static int pid_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
1454 struct inode
*inode
= dentry
->d_inode
;
1455 struct task_struct
*task
;
1456 generic_fillattr(inode
, stat
);
1461 task
= pid_task(proc_pid(inode
), PIDTYPE_PID
);
1463 if ((inode
->i_mode
== (S_IFDIR
|S_IRUGO
|S_IXUGO
)) ||
1464 task_dumpable(task
)) {
1465 stat
->uid
= task
->euid
;
1466 stat
->gid
= task
->egid
;
1476 * Exceptional case: normally we are not allowed to unhash a busy
1477 * directory. In this case, however, we can do it - no aliasing problems
1478 * due to the way we treat inodes.
1480 * Rewrite the inode's ownerships here because the owning task may have
1481 * performed a setuid(), etc.
1483 * Before the /proc/pid/status file was created the only way to read
1484 * the effective uid of a /process was to stat /proc/pid. Reading
1485 * /proc/pid/status is slow enough that procps and other packages
1486 * kept stating /proc/pid. To keep the rules in /proc simple I have
1487 * made this apply to all per process world readable and executable
1490 static int pid_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1492 struct inode
*inode
= dentry
->d_inode
;
1493 struct task_struct
*task
= get_proc_task(inode
);
1495 if ((inode
->i_mode
== (S_IFDIR
|S_IRUGO
|S_IXUGO
)) ||
1496 task_dumpable(task
)) {
1497 inode
->i_uid
= task
->euid
;
1498 inode
->i_gid
= task
->egid
;
1503 inode
->i_mode
&= ~(S_ISUID
| S_ISGID
);
1504 security_task_to_inode(task
, inode
);
1505 put_task_struct(task
);
1512 static int pid_delete_dentry(struct dentry
* dentry
)
1514 /* Is the task we represent dead?
1515 * If so, then don't put the dentry on the lru list,
1516 * kill it immediately.
1518 return !proc_pid(dentry
->d_inode
)->tasks
[PIDTYPE_PID
].first
;
1521 static struct dentry_operations pid_dentry_operations
=
1523 .d_revalidate
= pid_revalidate
,
1524 .d_delete
= pid_delete_dentry
,
1529 typedef struct dentry
*instantiate_t(struct inode
*, struct dentry
*,
1530 struct task_struct
*, const void *);
1533 * Fill a directory entry.
1535 * If possible create the dcache entry and derive our inode number and
1536 * file type from dcache entry.
1538 * Since all of the proc inode numbers are dynamically generated, the inode
1539 * numbers do not exist until the inode is cache. This means creating the
1540 * the dcache entry in readdir is necessary to keep the inode numbers
1541 * reported by readdir in sync with the inode numbers reported
1544 static int proc_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
1545 char *name
, int len
,
1546 instantiate_t instantiate
, struct task_struct
*task
, const void *ptr
)
1548 struct dentry
*child
, *dir
= filp
->f_path
.dentry
;
1549 struct inode
*inode
;
1552 unsigned type
= DT_UNKNOWN
;
1556 qname
.hash
= full_name_hash(name
, len
);
1558 child
= d_lookup(dir
, &qname
);
1561 new = d_alloc(dir
, &qname
);
1563 child
= instantiate(dir
->d_inode
, new, task
, ptr
);
1570 if (!child
|| IS_ERR(child
) || !child
->d_inode
)
1571 goto end_instantiate
;
1572 inode
= child
->d_inode
;
1575 type
= inode
->i_mode
>> 12;
1580 ino
= find_inode_number(dir
, &qname
);
1583 return filldir(dirent
, name
, len
, filp
->f_pos
, ino
, type
);
1586 static unsigned name_to_int(struct dentry
*dentry
)
1588 const char *name
= dentry
->d_name
.name
;
1589 int len
= dentry
->d_name
.len
;
1592 if (len
> 1 && *name
== '0')
1595 unsigned c
= *name
++ - '0';
1598 if (n
>= (~0U-9)/10)
1608 #define PROC_FDINFO_MAX 64
1610 static int proc_fd_info(struct inode
*inode
, struct path
*path
, char *info
)
1612 struct task_struct
*task
= get_proc_task(inode
);
1613 struct files_struct
*files
= NULL
;
1615 int fd
= proc_fd(inode
);
1618 files
= get_files_struct(task
);
1619 put_task_struct(task
);
1623 * We are not taking a ref to the file structure, so we must
1626 spin_lock(&files
->file_lock
);
1627 file
= fcheck_files(files
, fd
);
1630 *path
= file
->f_path
;
1631 path_get(&file
->f_path
);
1634 snprintf(info
, PROC_FDINFO_MAX
,
1637 (long long) file
->f_pos
,
1639 spin_unlock(&files
->file_lock
);
1640 put_files_struct(files
);
1643 spin_unlock(&files
->file_lock
);
1644 put_files_struct(files
);
1649 static int proc_fd_link(struct inode
*inode
, struct path
*path
)
1651 return proc_fd_info(inode
, path
, NULL
);
1654 static int tid_fd_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1656 struct inode
*inode
= dentry
->d_inode
;
1657 struct task_struct
*task
= get_proc_task(inode
);
1658 int fd
= proc_fd(inode
);
1659 struct files_struct
*files
;
1662 files
= get_files_struct(task
);
1665 if (fcheck_files(files
, fd
)) {
1667 put_files_struct(files
);
1668 if (task_dumpable(task
)) {
1669 inode
->i_uid
= task
->euid
;
1670 inode
->i_gid
= task
->egid
;
1675 inode
->i_mode
&= ~(S_ISUID
| S_ISGID
);
1676 security_task_to_inode(task
, inode
);
1677 put_task_struct(task
);
1681 put_files_struct(files
);
1683 put_task_struct(task
);
1689 static struct dentry_operations tid_fd_dentry_operations
=
1691 .d_revalidate
= tid_fd_revalidate
,
1692 .d_delete
= pid_delete_dentry
,
1695 static struct dentry
*proc_fd_instantiate(struct inode
*dir
,
1696 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
1698 unsigned fd
= *(const unsigned *)ptr
;
1700 struct files_struct
*files
;
1701 struct inode
*inode
;
1702 struct proc_inode
*ei
;
1703 struct dentry
*error
= ERR_PTR(-ENOENT
);
1705 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
1710 files
= get_files_struct(task
);
1713 inode
->i_mode
= S_IFLNK
;
1716 * We are not taking a ref to the file structure, so we must
1719 spin_lock(&files
->file_lock
);
1720 file
= fcheck_files(files
, fd
);
1723 if (file
->f_mode
& 1)
1724 inode
->i_mode
|= S_IRUSR
| S_IXUSR
;
1725 if (file
->f_mode
& 2)
1726 inode
->i_mode
|= S_IWUSR
| S_IXUSR
;
1727 spin_unlock(&files
->file_lock
);
1728 put_files_struct(files
);
1730 inode
->i_op
= &proc_pid_link_inode_operations
;
1732 ei
->op
.proc_get_link
= proc_fd_link
;
1733 dentry
->d_op
= &tid_fd_dentry_operations
;
1734 d_add(dentry
, inode
);
1735 /* Close the race of the process dying before we return the dentry */
1736 if (tid_fd_revalidate(dentry
, NULL
))
1742 spin_unlock(&files
->file_lock
);
1743 put_files_struct(files
);
1749 static struct dentry
*proc_lookupfd_common(struct inode
*dir
,
1750 struct dentry
*dentry
,
1751 instantiate_t instantiate
)
1753 struct task_struct
*task
= get_proc_task(dir
);
1754 unsigned fd
= name_to_int(dentry
);
1755 struct dentry
*result
= ERR_PTR(-ENOENT
);
1762 result
= instantiate(dir
, dentry
, task
, &fd
);
1764 put_task_struct(task
);
1769 static int proc_readfd_common(struct file
* filp
, void * dirent
,
1770 filldir_t filldir
, instantiate_t instantiate
)
1772 struct dentry
*dentry
= filp
->f_path
.dentry
;
1773 struct inode
*inode
= dentry
->d_inode
;
1774 struct task_struct
*p
= get_proc_task(inode
);
1775 unsigned int fd
, ino
;
1777 struct files_struct
* files
;
1787 if (filldir(dirent
, ".", 1, 0, inode
->i_ino
, DT_DIR
) < 0)
1791 ino
= parent_ino(dentry
);
1792 if (filldir(dirent
, "..", 2, 1, ino
, DT_DIR
) < 0)
1796 files
= get_files_struct(p
);
1800 for (fd
= filp
->f_pos
-2;
1801 fd
< files_fdtable(files
)->max_fds
;
1802 fd
++, filp
->f_pos
++) {
1803 char name
[PROC_NUMBUF
];
1806 if (!fcheck_files(files
, fd
))
1810 len
= snprintf(name
, sizeof(name
), "%d", fd
);
1811 if (proc_fill_cache(filp
, dirent
, filldir
,
1812 name
, len
, instantiate
,
1820 put_files_struct(files
);
1828 static struct dentry
*proc_lookupfd(struct inode
*dir
, struct dentry
*dentry
,
1829 struct nameidata
*nd
)
1831 return proc_lookupfd_common(dir
, dentry
, proc_fd_instantiate
);
1834 static int proc_readfd(struct file
*filp
, void *dirent
, filldir_t filldir
)
1836 return proc_readfd_common(filp
, dirent
, filldir
, proc_fd_instantiate
);
1839 static ssize_t
proc_fdinfo_read(struct file
*file
, char __user
*buf
,
1840 size_t len
, loff_t
*ppos
)
1842 char tmp
[PROC_FDINFO_MAX
];
1843 int err
= proc_fd_info(file
->f_path
.dentry
->d_inode
, NULL
, tmp
);
1845 err
= simple_read_from_buffer(buf
, len
, ppos
, tmp
, strlen(tmp
));
1849 static const struct file_operations proc_fdinfo_file_operations
= {
1850 .open
= nonseekable_open
,
1851 .read
= proc_fdinfo_read
,
1854 static const struct file_operations proc_fd_operations
= {
1855 .read
= generic_read_dir
,
1856 .readdir
= proc_readfd
,
1860 * /proc/pid/fd needs a special permission handler so that a process can still
1861 * access /proc/self/fd after it has executed a setuid().
1863 static int proc_fd_permission(struct inode
*inode
, int mask
)
1867 rv
= generic_permission(inode
, mask
, NULL
);
1870 if (task_pid(current
) == proc_pid(inode
))
1876 * proc directories can do almost nothing..
1878 static const struct inode_operations proc_fd_inode_operations
= {
1879 .lookup
= proc_lookupfd
,
1880 .permission
= proc_fd_permission
,
1881 .setattr
= proc_setattr
,
1884 static struct dentry
*proc_fdinfo_instantiate(struct inode
*dir
,
1885 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
1887 unsigned fd
= *(unsigned *)ptr
;
1888 struct inode
*inode
;
1889 struct proc_inode
*ei
;
1890 struct dentry
*error
= ERR_PTR(-ENOENT
);
1892 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
1897 inode
->i_mode
= S_IFREG
| S_IRUSR
;
1898 inode
->i_fop
= &proc_fdinfo_file_operations
;
1899 dentry
->d_op
= &tid_fd_dentry_operations
;
1900 d_add(dentry
, inode
);
1901 /* Close the race of the process dying before we return the dentry */
1902 if (tid_fd_revalidate(dentry
, NULL
))
1909 static struct dentry
*proc_lookupfdinfo(struct inode
*dir
,
1910 struct dentry
*dentry
,
1911 struct nameidata
*nd
)
1913 return proc_lookupfd_common(dir
, dentry
, proc_fdinfo_instantiate
);
1916 static int proc_readfdinfo(struct file
*filp
, void *dirent
, filldir_t filldir
)
1918 return proc_readfd_common(filp
, dirent
, filldir
,
1919 proc_fdinfo_instantiate
);
1922 static const struct file_operations proc_fdinfo_operations
= {
1923 .read
= generic_read_dir
,
1924 .readdir
= proc_readfdinfo
,
1928 * proc directories can do almost nothing..
1930 static const struct inode_operations proc_fdinfo_inode_operations
= {
1931 .lookup
= proc_lookupfdinfo
,
1932 .setattr
= proc_setattr
,
1936 static struct dentry
*proc_pident_instantiate(struct inode
*dir
,
1937 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
1939 const struct pid_entry
*p
= ptr
;
1940 struct inode
*inode
;
1941 struct proc_inode
*ei
;
1942 struct dentry
*error
= ERR_PTR(-EINVAL
);
1944 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
1949 inode
->i_mode
= p
->mode
;
1950 if (S_ISDIR(inode
->i_mode
))
1951 inode
->i_nlink
= 2; /* Use getattr to fix if necessary */
1953 inode
->i_op
= p
->iop
;
1955 inode
->i_fop
= p
->fop
;
1957 dentry
->d_op
= &pid_dentry_operations
;
1958 d_add(dentry
, inode
);
1959 /* Close the race of the process dying before we return the dentry */
1960 if (pid_revalidate(dentry
, NULL
))
1966 static struct dentry
*proc_pident_lookup(struct inode
*dir
,
1967 struct dentry
*dentry
,
1968 const struct pid_entry
*ents
,
1971 struct inode
*inode
;
1972 struct dentry
*error
;
1973 struct task_struct
*task
= get_proc_task(dir
);
1974 const struct pid_entry
*p
, *last
;
1976 error
= ERR_PTR(-ENOENT
);
1983 * Yes, it does not scale. And it should not. Don't add
1984 * new entries into /proc/<tgid>/ without very good reasons.
1986 last
= &ents
[nents
- 1];
1987 for (p
= ents
; p
<= last
; p
++) {
1988 if (p
->len
!= dentry
->d_name
.len
)
1990 if (!memcmp(dentry
->d_name
.name
, p
->name
, p
->len
))
1996 error
= proc_pident_instantiate(dir
, dentry
, task
, p
);
1998 put_task_struct(task
);
2003 static int proc_pident_fill_cache(struct file
*filp
, void *dirent
,
2004 filldir_t filldir
, struct task_struct
*task
, const struct pid_entry
*p
)
2006 return proc_fill_cache(filp
, dirent
, filldir
, p
->name
, p
->len
,
2007 proc_pident_instantiate
, task
, p
);
2010 static int proc_pident_readdir(struct file
*filp
,
2011 void *dirent
, filldir_t filldir
,
2012 const struct pid_entry
*ents
, unsigned int nents
)
2015 struct dentry
*dentry
= filp
->f_path
.dentry
;
2016 struct inode
*inode
= dentry
->d_inode
;
2017 struct task_struct
*task
= get_proc_task(inode
);
2018 const struct pid_entry
*p
, *last
;
2031 if (filldir(dirent
, ".", 1, i
, ino
, DT_DIR
) < 0)
2037 ino
= parent_ino(dentry
);
2038 if (filldir(dirent
, "..", 2, i
, ino
, DT_DIR
) < 0)
2050 last
= &ents
[nents
- 1];
2052 if (proc_pident_fill_cache(filp
, dirent
, filldir
, task
, p
) < 0)
2061 put_task_struct(task
);
2066 #ifdef CONFIG_SECURITY
2067 static ssize_t
proc_pid_attr_read(struct file
* file
, char __user
* buf
,
2068 size_t count
, loff_t
*ppos
)
2070 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
2073 struct task_struct
*task
= get_proc_task(inode
);
2078 length
= security_getprocattr(task
,
2079 (char*)file
->f_path
.dentry
->d_name
.name
,
2081 put_task_struct(task
);
2083 length
= simple_read_from_buffer(buf
, count
, ppos
, p
, length
);
2088 static ssize_t
proc_pid_attr_write(struct file
* file
, const char __user
* buf
,
2089 size_t count
, loff_t
*ppos
)
2091 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
2094 struct task_struct
*task
= get_proc_task(inode
);
2099 if (count
> PAGE_SIZE
)
2102 /* No partial writes. */
2108 page
= (char*)__get_free_page(GFP_TEMPORARY
);
2113 if (copy_from_user(page
, buf
, count
))
2116 length
= security_setprocattr(task
,
2117 (char*)file
->f_path
.dentry
->d_name
.name
,
2118 (void*)page
, count
);
2120 free_page((unsigned long) page
);
2122 put_task_struct(task
);
2127 static const struct file_operations proc_pid_attr_operations
= {
2128 .read
= proc_pid_attr_read
,
2129 .write
= proc_pid_attr_write
,
2132 static const struct pid_entry attr_dir_stuff
[] = {
2133 REG("current", S_IRUGO
|S_IWUGO
, pid_attr
),
2134 REG("prev", S_IRUGO
, pid_attr
),
2135 REG("exec", S_IRUGO
|S_IWUGO
, pid_attr
),
2136 REG("fscreate", S_IRUGO
|S_IWUGO
, pid_attr
),
2137 REG("keycreate", S_IRUGO
|S_IWUGO
, pid_attr
),
2138 REG("sockcreate", S_IRUGO
|S_IWUGO
, pid_attr
),
2141 static int proc_attr_dir_readdir(struct file
* filp
,
2142 void * dirent
, filldir_t filldir
)
2144 return proc_pident_readdir(filp
,dirent
,filldir
,
2145 attr_dir_stuff
,ARRAY_SIZE(attr_dir_stuff
));
2148 static const struct file_operations proc_attr_dir_operations
= {
2149 .read
= generic_read_dir
,
2150 .readdir
= proc_attr_dir_readdir
,
2153 static struct dentry
*proc_attr_dir_lookup(struct inode
*dir
,
2154 struct dentry
*dentry
, struct nameidata
*nd
)
2156 return proc_pident_lookup(dir
, dentry
,
2157 attr_dir_stuff
, ARRAY_SIZE(attr_dir_stuff
));
2160 static const struct inode_operations proc_attr_dir_inode_operations
= {
2161 .lookup
= proc_attr_dir_lookup
,
2162 .getattr
= pid_getattr
,
2163 .setattr
= proc_setattr
,
2168 #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2169 static ssize_t
proc_coredump_filter_read(struct file
*file
, char __user
*buf
,
2170 size_t count
, loff_t
*ppos
)
2172 struct task_struct
*task
= get_proc_task(file
->f_dentry
->d_inode
);
2173 struct mm_struct
*mm
;
2174 char buffer
[PROC_NUMBUF
];
2182 mm
= get_task_mm(task
);
2184 len
= snprintf(buffer
, sizeof(buffer
), "%08lx\n",
2185 ((mm
->flags
& MMF_DUMP_FILTER_MASK
) >>
2186 MMF_DUMP_FILTER_SHIFT
));
2188 ret
= simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
2191 put_task_struct(task
);
2196 static ssize_t
proc_coredump_filter_write(struct file
*file
,
2197 const char __user
*buf
,
2201 struct task_struct
*task
;
2202 struct mm_struct
*mm
;
2203 char buffer
[PROC_NUMBUF
], *end
;
2210 memset(buffer
, 0, sizeof(buffer
));
2211 if (count
> sizeof(buffer
) - 1)
2212 count
= sizeof(buffer
) - 1;
2213 if (copy_from_user(buffer
, buf
, count
))
2217 val
= (unsigned int)simple_strtoul(buffer
, &end
, 0);
2220 if (end
- buffer
== 0)
2224 task
= get_proc_task(file
->f_dentry
->d_inode
);
2229 mm
= get_task_mm(task
);
2233 for (i
= 0, mask
= 1; i
< MMF_DUMP_FILTER_BITS
; i
++, mask
<<= 1) {
2235 set_bit(i
+ MMF_DUMP_FILTER_SHIFT
, &mm
->flags
);
2237 clear_bit(i
+ MMF_DUMP_FILTER_SHIFT
, &mm
->flags
);
2242 put_task_struct(task
);
2247 static const struct file_operations proc_coredump_filter_operations
= {
2248 .read
= proc_coredump_filter_read
,
2249 .write
= proc_coredump_filter_write
,
2256 static int proc_self_readlink(struct dentry
*dentry
, char __user
*buffer
,
2259 struct pid_namespace
*ns
= dentry
->d_sb
->s_fs_info
;
2260 pid_t tgid
= task_tgid_nr_ns(current
, ns
);
2261 char tmp
[PROC_NUMBUF
];
2264 sprintf(tmp
, "%d", tgid
);
2265 return vfs_readlink(dentry
,buffer
,buflen
,tmp
);
2268 static void *proc_self_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
2270 struct pid_namespace
*ns
= dentry
->d_sb
->s_fs_info
;
2271 pid_t tgid
= task_tgid_nr_ns(current
, ns
);
2272 char tmp
[PROC_NUMBUF
];
2274 return ERR_PTR(-ENOENT
);
2275 sprintf(tmp
, "%d", task_tgid_nr_ns(current
, ns
));
2276 return ERR_PTR(vfs_follow_link(nd
,tmp
));
2279 static const struct inode_operations proc_self_inode_operations
= {
2280 .readlink
= proc_self_readlink
,
2281 .follow_link
= proc_self_follow_link
,
2287 * These are the directory entries in the root directory of /proc
2288 * that properly belong to the /proc filesystem, as they describe
2289 * describe something that is process related.
2291 static const struct pid_entry proc_base_stuff
[] = {
2292 NOD("self", S_IFLNK
|S_IRWXUGO
,
2293 &proc_self_inode_operations
, NULL
, {}),
2297 * Exceptional case: normally we are not allowed to unhash a busy
2298 * directory. In this case, however, we can do it - no aliasing problems
2299 * due to the way we treat inodes.
2301 static int proc_base_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
2303 struct inode
*inode
= dentry
->d_inode
;
2304 struct task_struct
*task
= get_proc_task(inode
);
2306 put_task_struct(task
);
2313 static struct dentry_operations proc_base_dentry_operations
=
2315 .d_revalidate
= proc_base_revalidate
,
2316 .d_delete
= pid_delete_dentry
,
2319 static struct dentry
*proc_base_instantiate(struct inode
*dir
,
2320 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
2322 const struct pid_entry
*p
= ptr
;
2323 struct inode
*inode
;
2324 struct proc_inode
*ei
;
2325 struct dentry
*error
= ERR_PTR(-EINVAL
);
2327 /* Allocate the inode */
2328 error
= ERR_PTR(-ENOMEM
);
2329 inode
= new_inode(dir
->i_sb
);
2333 /* Initialize the inode */
2335 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
2338 * grab the reference to the task.
2340 ei
->pid
= get_task_pid(task
, PIDTYPE_PID
);
2346 inode
->i_mode
= p
->mode
;
2347 if (S_ISDIR(inode
->i_mode
))
2349 if (S_ISLNK(inode
->i_mode
))
2352 inode
->i_op
= p
->iop
;
2354 inode
->i_fop
= p
->fop
;
2356 dentry
->d_op
= &proc_base_dentry_operations
;
2357 d_add(dentry
, inode
);
2366 static struct dentry
*proc_base_lookup(struct inode
*dir
, struct dentry
*dentry
)
2368 struct dentry
*error
;
2369 struct task_struct
*task
= get_proc_task(dir
);
2370 const struct pid_entry
*p
, *last
;
2372 error
= ERR_PTR(-ENOENT
);
2377 /* Lookup the directory entry */
2378 last
= &proc_base_stuff
[ARRAY_SIZE(proc_base_stuff
) - 1];
2379 for (p
= proc_base_stuff
; p
<= last
; p
++) {
2380 if (p
->len
!= dentry
->d_name
.len
)
2382 if (!memcmp(dentry
->d_name
.name
, p
->name
, p
->len
))
2388 error
= proc_base_instantiate(dir
, dentry
, task
, p
);
2391 put_task_struct(task
);
2396 static int proc_base_fill_cache(struct file
*filp
, void *dirent
,
2397 filldir_t filldir
, struct task_struct
*task
, const struct pid_entry
*p
)
2399 return proc_fill_cache(filp
, dirent
, filldir
, p
->name
, p
->len
,
2400 proc_base_instantiate
, task
, p
);
2403 #ifdef CONFIG_TASK_IO_ACCOUNTING
2404 static int do_io_accounting(struct task_struct
*task
, char *buffer
, int whole
)
2406 struct task_io_accounting acct
= task
->ioac
;
2407 unsigned long flags
;
2409 if (whole
&& lock_task_sighand(task
, &flags
)) {
2410 struct task_struct
*t
= task
;
2412 task_io_accounting_add(&acct
, &task
->signal
->ioac
);
2413 while_each_thread(task
, t
)
2414 task_io_accounting_add(&acct
, &t
->ioac
);
2416 unlock_task_sighand(task
, &flags
);
2418 return sprintf(buffer
,
2423 "read_bytes: %llu\n"
2424 "write_bytes: %llu\n"
2425 "cancelled_write_bytes: %llu\n",
2426 (unsigned long long)acct
.rchar
,
2427 (unsigned long long)acct
.wchar
,
2428 (unsigned long long)acct
.syscr
,
2429 (unsigned long long)acct
.syscw
,
2430 (unsigned long long)acct
.read_bytes
,
2431 (unsigned long long)acct
.write_bytes
,
2432 (unsigned long long)acct
.cancelled_write_bytes
);
2435 static int proc_tid_io_accounting(struct task_struct
*task
, char *buffer
)
2437 return do_io_accounting(task
, buffer
, 0);
2440 static int proc_tgid_io_accounting(struct task_struct
*task
, char *buffer
)
2442 return do_io_accounting(task
, buffer
, 1);
2444 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2449 static const struct file_operations proc_task_operations
;
2450 static const struct inode_operations proc_task_inode_operations
;
2452 static const struct pid_entry tgid_base_stuff
[] = {
2453 DIR("task", S_IRUGO
|S_IXUGO
, task
),
2454 DIR("fd", S_IRUSR
|S_IXUSR
, fd
),
2455 DIR("fdinfo", S_IRUSR
|S_IXUSR
, fdinfo
),
2457 DIR("net", S_IRUGO
|S_IXUGO
, net
),
2459 REG("environ", S_IRUSR
, environ
),
2460 INF("auxv", S_IRUSR
, pid_auxv
),
2461 ONE("status", S_IRUGO
, pid_status
),
2462 INF("limits", S_IRUSR
, pid_limits
),
2463 #ifdef CONFIG_SCHED_DEBUG
2464 REG("sched", S_IRUGO
|S_IWUSR
, pid_sched
),
2466 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2467 INF("syscall", S_IRUSR
, pid_syscall
),
2469 INF("cmdline", S_IRUGO
, pid_cmdline
),
2470 ONE("stat", S_IRUGO
, tgid_stat
),
2471 ONE("statm", S_IRUGO
, pid_statm
),
2472 REG("maps", S_IRUGO
, maps
),
2474 REG("numa_maps", S_IRUGO
, numa_maps
),
2476 REG("mem", S_IRUSR
|S_IWUSR
, mem
),
2480 REG("mounts", S_IRUGO
, mounts
),
2481 REG("mountinfo", S_IRUGO
, mountinfo
),
2482 REG("mountstats", S_IRUSR
, mountstats
),
2483 #ifdef CONFIG_PROC_PAGE_MONITOR
2484 REG("clear_refs", S_IWUSR
, clear_refs
),
2485 REG("smaps", S_IRUGO
, smaps
),
2486 REG("pagemap", S_IRUSR
, pagemap
),
2488 #ifdef CONFIG_SECURITY
2489 DIR("attr", S_IRUGO
|S_IXUGO
, attr_dir
),
2491 #ifdef CONFIG_KALLSYMS
2492 INF("wchan", S_IRUGO
, pid_wchan
),
2494 #ifdef CONFIG_SCHEDSTATS
2495 INF("schedstat", S_IRUGO
, pid_schedstat
),
2497 #ifdef CONFIG_LATENCYTOP
2498 REG("latency", S_IRUGO
, lstats
),
2500 #ifdef CONFIG_PROC_PID_CPUSET
2501 REG("cpuset", S_IRUGO
, cpuset
),
2503 #ifdef CONFIG_CGROUPS
2504 REG("cgroup", S_IRUGO
, cgroup
),
2506 INF("oom_score", S_IRUGO
, oom_score
),
2507 REG("oom_adj", S_IRUGO
|S_IWUSR
, oom_adjust
),
2508 #ifdef CONFIG_AUDITSYSCALL
2509 REG("loginuid", S_IWUSR
|S_IRUGO
, loginuid
),
2510 REG("sessionid", S_IRUGO
, sessionid
),
2512 #ifdef CONFIG_FAULT_INJECTION
2513 REG("make-it-fail", S_IRUGO
|S_IWUSR
, fault_inject
),
2515 #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2516 REG("coredump_filter", S_IRUGO
|S_IWUSR
, coredump_filter
),
2518 #ifdef CONFIG_TASK_IO_ACCOUNTING
2519 INF("io", S_IRUGO
, tgid_io_accounting
),
2523 static int proc_tgid_base_readdir(struct file
* filp
,
2524 void * dirent
, filldir_t filldir
)
2526 return proc_pident_readdir(filp
,dirent
,filldir
,
2527 tgid_base_stuff
,ARRAY_SIZE(tgid_base_stuff
));
2530 static const struct file_operations proc_tgid_base_operations
= {
2531 .read
= generic_read_dir
,
2532 .readdir
= proc_tgid_base_readdir
,
2535 static struct dentry
*proc_tgid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
){
2536 return proc_pident_lookup(dir
, dentry
,
2537 tgid_base_stuff
, ARRAY_SIZE(tgid_base_stuff
));
2540 static const struct inode_operations proc_tgid_base_inode_operations
= {
2541 .lookup
= proc_tgid_base_lookup
,
2542 .getattr
= pid_getattr
,
2543 .setattr
= proc_setattr
,
2546 static void proc_flush_task_mnt(struct vfsmount
*mnt
, pid_t pid
, pid_t tgid
)
2548 struct dentry
*dentry
, *leader
, *dir
;
2549 char buf
[PROC_NUMBUF
];
2553 name
.len
= snprintf(buf
, sizeof(buf
), "%d", pid
);
2554 dentry
= d_hash_and_lookup(mnt
->mnt_root
, &name
);
2556 if (!(current
->flags
& PF_EXITING
))
2557 shrink_dcache_parent(dentry
);
2566 name
.len
= snprintf(buf
, sizeof(buf
), "%d", tgid
);
2567 leader
= d_hash_and_lookup(mnt
->mnt_root
, &name
);
2572 name
.len
= strlen(name
.name
);
2573 dir
= d_hash_and_lookup(leader
, &name
);
2575 goto out_put_leader
;
2578 name
.len
= snprintf(buf
, sizeof(buf
), "%d", pid
);
2579 dentry
= d_hash_and_lookup(dir
, &name
);
2581 shrink_dcache_parent(dentry
);
2594 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2595 * @task: task that should be flushed.
2597 * When flushing dentries from proc, one needs to flush them from global
2598 * proc (proc_mnt) and from all the namespaces' procs this task was seen
2599 * in. This call is supposed to do all of this job.
2601 * Looks in the dcache for
2603 * /proc/@tgid/task/@pid
2604 * if either directory is present flushes it and all of it'ts children
2607 * It is safe and reasonable to cache /proc entries for a task until
2608 * that task exits. After that they just clog up the dcache with
2609 * useless entries, possibly causing useful dcache entries to be
2610 * flushed instead. This routine is proved to flush those useless
2611 * dcache entries at process exit time.
2613 * NOTE: This routine is just an optimization so it does not guarantee
2614 * that no dcache entries will exist at process exit time it
2615 * just makes it very unlikely that any will persist.
2618 void proc_flush_task(struct task_struct
*task
)
2621 struct pid
*pid
, *tgid
= NULL
;
2624 pid
= task_pid(task
);
2625 if (thread_group_leader(task
))
2626 tgid
= task_tgid(task
);
2628 for (i
= 0; i
<= pid
->level
; i
++) {
2629 upid
= &pid
->numbers
[i
];
2630 proc_flush_task_mnt(upid
->ns
->proc_mnt
, upid
->nr
,
2631 tgid
? tgid
->numbers
[i
].nr
: 0);
2634 upid
= &pid
->numbers
[pid
->level
];
2636 pid_ns_release_proc(upid
->ns
);
2639 static struct dentry
*proc_pid_instantiate(struct inode
*dir
,
2640 struct dentry
* dentry
,
2641 struct task_struct
*task
, const void *ptr
)
2643 struct dentry
*error
= ERR_PTR(-ENOENT
);
2644 struct inode
*inode
;
2646 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
2650 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
2651 inode
->i_op
= &proc_tgid_base_inode_operations
;
2652 inode
->i_fop
= &proc_tgid_base_operations
;
2653 inode
->i_flags
|=S_IMMUTABLE
;
2655 inode
->i_nlink
= 2 + pid_entry_count_dirs(tgid_base_stuff
,
2656 ARRAY_SIZE(tgid_base_stuff
));
2658 dentry
->d_op
= &pid_dentry_operations
;
2660 d_add(dentry
, inode
);
2661 /* Close the race of the process dying before we return the dentry */
2662 if (pid_revalidate(dentry
, NULL
))
2668 struct dentry
*proc_pid_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
)
2670 struct dentry
*result
= ERR_PTR(-ENOENT
);
2671 struct task_struct
*task
;
2673 struct pid_namespace
*ns
;
2675 result
= proc_base_lookup(dir
, dentry
);
2676 if (!IS_ERR(result
) || PTR_ERR(result
) != -ENOENT
)
2679 tgid
= name_to_int(dentry
);
2683 ns
= dentry
->d_sb
->s_fs_info
;
2685 task
= find_task_by_pid_ns(tgid
, ns
);
2687 get_task_struct(task
);
2692 result
= proc_pid_instantiate(dir
, dentry
, task
, NULL
);
2693 put_task_struct(task
);
2699 * Find the first task with tgid >= tgid
2704 struct task_struct
*task
;
2706 static struct tgid_iter
next_tgid(struct pid_namespace
*ns
, struct tgid_iter iter
)
2711 put_task_struct(iter
.task
);
2715 pid
= find_ge_pid(iter
.tgid
, ns
);
2717 iter
.tgid
= pid_nr_ns(pid
, ns
);
2718 iter
.task
= pid_task(pid
, PIDTYPE_PID
);
2719 /* What we to know is if the pid we have find is the
2720 * pid of a thread_group_leader. Testing for task
2721 * being a thread_group_leader is the obvious thing
2722 * todo but there is a window when it fails, due to
2723 * the pid transfer logic in de_thread.
2725 * So we perform the straight forward test of seeing
2726 * if the pid we have found is the pid of a thread
2727 * group leader, and don't worry if the task we have
2728 * found doesn't happen to be a thread group leader.
2729 * As we don't care in the case of readdir.
2731 if (!iter
.task
|| !has_group_leader_pid(iter
.task
)) {
2735 get_task_struct(iter
.task
);
2741 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
2743 static int proc_pid_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
2744 struct tgid_iter iter
)
2746 char name
[PROC_NUMBUF
];
2747 int len
= snprintf(name
, sizeof(name
), "%d", iter
.tgid
);
2748 return proc_fill_cache(filp
, dirent
, filldir
, name
, len
,
2749 proc_pid_instantiate
, iter
.task
, NULL
);
2752 /* for the /proc/ directory itself, after non-process stuff has been done */
2753 int proc_pid_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
2755 unsigned int nr
= filp
->f_pos
- FIRST_PROCESS_ENTRY
;
2756 struct task_struct
*reaper
= get_proc_task(filp
->f_path
.dentry
->d_inode
);
2757 struct tgid_iter iter
;
2758 struct pid_namespace
*ns
;
2763 for (; nr
< ARRAY_SIZE(proc_base_stuff
); filp
->f_pos
++, nr
++) {
2764 const struct pid_entry
*p
= &proc_base_stuff
[nr
];
2765 if (proc_base_fill_cache(filp
, dirent
, filldir
, reaper
, p
) < 0)
2769 ns
= filp
->f_dentry
->d_sb
->s_fs_info
;
2771 iter
.tgid
= filp
->f_pos
- TGID_OFFSET
;
2772 for (iter
= next_tgid(ns
, iter
);
2774 iter
.tgid
+= 1, iter
= next_tgid(ns
, iter
)) {
2775 filp
->f_pos
= iter
.tgid
+ TGID_OFFSET
;
2776 if (proc_pid_fill_cache(filp
, dirent
, filldir
, iter
) < 0) {
2777 put_task_struct(iter
.task
);
2781 filp
->f_pos
= PID_MAX_LIMIT
+ TGID_OFFSET
;
2783 put_task_struct(reaper
);
2791 static const struct pid_entry tid_base_stuff
[] = {
2792 DIR("fd", S_IRUSR
|S_IXUSR
, fd
),
2793 DIR("fdinfo", S_IRUSR
|S_IXUSR
, fdinfo
),
2794 REG("environ", S_IRUSR
, environ
),
2795 INF("auxv", S_IRUSR
, pid_auxv
),
2796 ONE("status", S_IRUGO
, pid_status
),
2797 INF("limits", S_IRUSR
, pid_limits
),
2798 #ifdef CONFIG_SCHED_DEBUG
2799 REG("sched", S_IRUGO
|S_IWUSR
, pid_sched
),
2801 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2802 INF("syscall", S_IRUSR
, pid_syscall
),
2804 INF("cmdline", S_IRUGO
, pid_cmdline
),
2805 ONE("stat", S_IRUGO
, tid_stat
),
2806 ONE("statm", S_IRUGO
, pid_statm
),
2807 REG("maps", S_IRUGO
, maps
),
2809 REG("numa_maps", S_IRUGO
, numa_maps
),
2811 REG("mem", S_IRUSR
|S_IWUSR
, mem
),
2815 REG("mounts", S_IRUGO
, mounts
),
2816 REG("mountinfo", S_IRUGO
, mountinfo
),
2817 #ifdef CONFIG_PROC_PAGE_MONITOR
2818 REG("clear_refs", S_IWUSR
, clear_refs
),
2819 REG("smaps", S_IRUGO
, smaps
),
2820 REG("pagemap", S_IRUSR
, pagemap
),
2822 #ifdef CONFIG_SECURITY
2823 DIR("attr", S_IRUGO
|S_IXUGO
, attr_dir
),
2825 #ifdef CONFIG_KALLSYMS
2826 INF("wchan", S_IRUGO
, pid_wchan
),
2828 #ifdef CONFIG_SCHEDSTATS
2829 INF("schedstat", S_IRUGO
, pid_schedstat
),
2831 #ifdef CONFIG_LATENCYTOP
2832 REG("latency", S_IRUGO
, lstats
),
2834 #ifdef CONFIG_PROC_PID_CPUSET
2835 REG("cpuset", S_IRUGO
, cpuset
),
2837 #ifdef CONFIG_CGROUPS
2838 REG("cgroup", S_IRUGO
, cgroup
),
2840 INF("oom_score", S_IRUGO
, oom_score
),
2841 REG("oom_adj", S_IRUGO
|S_IWUSR
, oom_adjust
),
2842 #ifdef CONFIG_AUDITSYSCALL
2843 REG("loginuid", S_IWUSR
|S_IRUGO
, loginuid
),
2844 REG("sessionid", S_IRUSR
, sessionid
),
2846 #ifdef CONFIG_FAULT_INJECTION
2847 REG("make-it-fail", S_IRUGO
|S_IWUSR
, fault_inject
),
2849 #ifdef CONFIG_TASK_IO_ACCOUNTING
2850 INF("io", S_IRUGO
, tid_io_accounting
),
2854 static int proc_tid_base_readdir(struct file
* filp
,
2855 void * dirent
, filldir_t filldir
)
2857 return proc_pident_readdir(filp
,dirent
,filldir
,
2858 tid_base_stuff
,ARRAY_SIZE(tid_base_stuff
));
2861 static struct dentry
*proc_tid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
){
2862 return proc_pident_lookup(dir
, dentry
,
2863 tid_base_stuff
, ARRAY_SIZE(tid_base_stuff
));
2866 static const struct file_operations proc_tid_base_operations
= {
2867 .read
= generic_read_dir
,
2868 .readdir
= proc_tid_base_readdir
,
2871 static const struct inode_operations proc_tid_base_inode_operations
= {
2872 .lookup
= proc_tid_base_lookup
,
2873 .getattr
= pid_getattr
,
2874 .setattr
= proc_setattr
,
2877 static struct dentry
*proc_task_instantiate(struct inode
*dir
,
2878 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
2880 struct dentry
*error
= ERR_PTR(-ENOENT
);
2881 struct inode
*inode
;
2882 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
2886 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
2887 inode
->i_op
= &proc_tid_base_inode_operations
;
2888 inode
->i_fop
= &proc_tid_base_operations
;
2889 inode
->i_flags
|=S_IMMUTABLE
;
2891 inode
->i_nlink
= 2 + pid_entry_count_dirs(tid_base_stuff
,
2892 ARRAY_SIZE(tid_base_stuff
));
2894 dentry
->d_op
= &pid_dentry_operations
;
2896 d_add(dentry
, inode
);
2897 /* Close the race of the process dying before we return the dentry */
2898 if (pid_revalidate(dentry
, NULL
))
2904 static struct dentry
*proc_task_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
)
2906 struct dentry
*result
= ERR_PTR(-ENOENT
);
2907 struct task_struct
*task
;
2908 struct task_struct
*leader
= get_proc_task(dir
);
2910 struct pid_namespace
*ns
;
2915 tid
= name_to_int(dentry
);
2919 ns
= dentry
->d_sb
->s_fs_info
;
2921 task
= find_task_by_pid_ns(tid
, ns
);
2923 get_task_struct(task
);
2927 if (!same_thread_group(leader
, task
))
2930 result
= proc_task_instantiate(dir
, dentry
, task
, NULL
);
2932 put_task_struct(task
);
2934 put_task_struct(leader
);
2940 * Find the first tid of a thread group to return to user space.
2942 * Usually this is just the thread group leader, but if the users
2943 * buffer was too small or there was a seek into the middle of the
2944 * directory we have more work todo.
2946 * In the case of a short read we start with find_task_by_pid.
2948 * In the case of a seek we start with the leader and walk nr
2951 static struct task_struct
*first_tid(struct task_struct
*leader
,
2952 int tid
, int nr
, struct pid_namespace
*ns
)
2954 struct task_struct
*pos
;
2957 /* Attempt to start with the pid of a thread */
2958 if (tid
&& (nr
> 0)) {
2959 pos
= find_task_by_pid_ns(tid
, ns
);
2960 if (pos
&& (pos
->group_leader
== leader
))
2964 /* If nr exceeds the number of threads there is nothing todo */
2966 if (nr
&& nr
>= get_nr_threads(leader
))
2969 /* If we haven't found our starting place yet start
2970 * with the leader and walk nr threads forward.
2972 for (pos
= leader
; nr
> 0; --nr
) {
2973 pos
= next_thread(pos
);
2974 if (pos
== leader
) {
2980 get_task_struct(pos
);
2987 * Find the next thread in the thread list.
2988 * Return NULL if there is an error or no next thread.
2990 * The reference to the input task_struct is released.
2992 static struct task_struct
*next_tid(struct task_struct
*start
)
2994 struct task_struct
*pos
= NULL
;
2996 if (pid_alive(start
)) {
2997 pos
= next_thread(start
);
2998 if (thread_group_leader(pos
))
3001 get_task_struct(pos
);
3004 put_task_struct(start
);
3008 static int proc_task_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
3009 struct task_struct
*task
, int tid
)
3011 char name
[PROC_NUMBUF
];
3012 int len
= snprintf(name
, sizeof(name
), "%d", tid
);
3013 return proc_fill_cache(filp
, dirent
, filldir
, name
, len
,
3014 proc_task_instantiate
, task
, NULL
);
3017 /* for the /proc/TGID/task/ directories */
3018 static int proc_task_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
3020 struct dentry
*dentry
= filp
->f_path
.dentry
;
3021 struct inode
*inode
= dentry
->d_inode
;
3022 struct task_struct
*leader
= NULL
;
3023 struct task_struct
*task
;
3024 int retval
= -ENOENT
;
3027 unsigned long pos
= filp
->f_pos
; /* avoiding "long long" filp->f_pos */
3028 struct pid_namespace
*ns
;
3030 task
= get_proc_task(inode
);
3034 if (pid_alive(task
)) {
3035 leader
= task
->group_leader
;
3036 get_task_struct(leader
);
3039 put_task_struct(task
);
3047 if (filldir(dirent
, ".", 1, pos
, ino
, DT_DIR
) < 0)
3052 ino
= parent_ino(dentry
);
3053 if (filldir(dirent
, "..", 2, pos
, ino
, DT_DIR
) < 0)
3059 /* f_version caches the tgid value that the last readdir call couldn't
3060 * return. lseek aka telldir automagically resets f_version to 0.
3062 ns
= filp
->f_dentry
->d_sb
->s_fs_info
;
3063 tid
= (int)filp
->f_version
;
3064 filp
->f_version
= 0;
3065 for (task
= first_tid(leader
, tid
, pos
- 2, ns
);
3067 task
= next_tid(task
), pos
++) {
3068 tid
= task_pid_nr_ns(task
, ns
);
3069 if (proc_task_fill_cache(filp
, dirent
, filldir
, task
, tid
) < 0) {
3070 /* returning this tgid failed, save it as the first
3071 * pid for the next readir call */
3072 filp
->f_version
= (u64
)tid
;
3073 put_task_struct(task
);
3079 put_task_struct(leader
);
3084 static int proc_task_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
3086 struct inode
*inode
= dentry
->d_inode
;
3087 struct task_struct
*p
= get_proc_task(inode
);
3088 generic_fillattr(inode
, stat
);
3092 stat
->nlink
+= get_nr_threads(p
);
3100 static const struct inode_operations proc_task_inode_operations
= {
3101 .lookup
= proc_task_lookup
,
3102 .getattr
= proc_task_getattr
,
3103 .setattr
= proc_setattr
,
3106 static const struct file_operations proc_task_operations
= {
3107 .read
= generic_read_dir
,
3108 .readdir
= proc_task_readdir
,