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
)
170 if (lock_task_sighand(tsk
, &flags
)) {
171 count
= atomic_read(&tsk
->signal
->count
);
172 unlock_task_sighand(tsk
, &flags
);
177 static int proc_cwd_link(struct inode
*inode
, struct path
*path
)
179 struct task_struct
*task
= get_proc_task(inode
);
180 struct fs_struct
*fs
= NULL
;
181 int result
= -ENOENT
;
184 fs
= get_fs_struct(task
);
185 put_task_struct(task
);
188 read_lock(&fs
->lock
);
191 read_unlock(&fs
->lock
);
198 static int proc_root_link(struct inode
*inode
, struct path
*path
)
200 struct task_struct
*task
= get_proc_task(inode
);
201 struct fs_struct
*fs
= NULL
;
202 int result
= -ENOENT
;
205 fs
= get_fs_struct(task
);
206 put_task_struct(task
);
209 read_lock(&fs
->lock
);
212 read_unlock(&fs
->lock
);
220 * Return zero if current may access user memory in @task, -error if not.
222 static int check_mem_permission(struct task_struct
*task
)
225 * A task can always look at itself, in case it chooses
226 * to use system calls instead of load instructions.
232 * If current is actively ptrace'ing, and would also be
233 * permitted to freshly attach with ptrace now, permit it.
235 if (task_is_stopped_or_traced(task
)) {
238 match
= (tracehook_tracer_task(task
) == current
);
240 if (match
&& ptrace_may_access(task
, PTRACE_MODE_ATTACH
))
245 * Noone else is allowed.
250 struct mm_struct
*mm_for_maps(struct task_struct
*task
)
252 struct mm_struct
*mm
= get_task_mm(task
);
255 down_read(&mm
->mmap_sem
);
259 if (task
->mm
!= current
->mm
&&
260 __ptrace_may_access(task
, PTRACE_MODE_READ
) < 0)
266 up_read(&mm
->mmap_sem
);
271 static int proc_pid_cmdline(struct task_struct
*task
, char * buffer
)
275 struct mm_struct
*mm
= get_task_mm(task
);
279 goto out_mm
; /* Shh! No looking before we're done */
281 len
= mm
->arg_end
- mm
->arg_start
;
286 res
= access_process_vm(task
, mm
->arg_start
, buffer
, len
, 0);
288 // If the nul at the end of args has been overwritten, then
289 // assume application is using setproctitle(3).
290 if (res
> 0 && buffer
[res
-1] != '\0' && len
< PAGE_SIZE
) {
291 len
= strnlen(buffer
, res
);
295 len
= mm
->env_end
- mm
->env_start
;
296 if (len
> PAGE_SIZE
- res
)
297 len
= PAGE_SIZE
- res
;
298 res
+= access_process_vm(task
, mm
->env_start
, buffer
+res
, len
, 0);
299 res
= strnlen(buffer
, res
);
308 static int proc_pid_auxv(struct task_struct
*task
, char *buffer
)
311 struct mm_struct
*mm
= get_task_mm(task
);
313 unsigned int nwords
= 0;
316 while (mm
->saved_auxv
[nwords
- 2] != 0); /* AT_NULL */
317 res
= nwords
* sizeof(mm
->saved_auxv
[0]);
320 memcpy(buffer
, mm
->saved_auxv
, res
);
327 #ifdef CONFIG_KALLSYMS
329 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
330 * Returns the resolved symbol. If that fails, simply return the address.
332 static int proc_pid_wchan(struct task_struct
*task
, char *buffer
)
335 char symname
[KSYM_NAME_LEN
];
337 wchan
= get_wchan(task
);
339 if (lookup_symbol_name(wchan
, symname
) < 0)
340 return sprintf(buffer
, "%lu", wchan
);
342 return sprintf(buffer
, "%s", symname
);
344 #endif /* CONFIG_KALLSYMS */
346 #ifdef CONFIG_SCHEDSTATS
348 * Provides /proc/PID/schedstat
350 static int proc_pid_schedstat(struct task_struct
*task
, char *buffer
)
352 return sprintf(buffer
, "%llu %llu %lu\n",
353 task
->sched_info
.cpu_time
,
354 task
->sched_info
.run_delay
,
355 task
->sched_info
.pcount
);
359 #ifdef CONFIG_LATENCYTOP
360 static int lstats_show_proc(struct seq_file
*m
, void *v
)
363 struct inode
*inode
= m
->private;
364 struct task_struct
*task
= get_proc_task(inode
);
368 seq_puts(m
, "Latency Top version : v0.1\n");
369 for (i
= 0; i
< 32; i
++) {
370 if (task
->latency_record
[i
].backtrace
[0]) {
372 seq_printf(m
, "%i %li %li ",
373 task
->latency_record
[i
].count
,
374 task
->latency_record
[i
].time
,
375 task
->latency_record
[i
].max
);
376 for (q
= 0; q
< LT_BACKTRACEDEPTH
; q
++) {
377 char sym
[KSYM_NAME_LEN
];
379 if (!task
->latency_record
[i
].backtrace
[q
])
381 if (task
->latency_record
[i
].backtrace
[q
] == ULONG_MAX
)
383 sprint_symbol(sym
, task
->latency_record
[i
].backtrace
[q
]);
384 c
= strchr(sym
, '+');
387 seq_printf(m
, "%s ", sym
);
393 put_task_struct(task
);
397 static int lstats_open(struct inode
*inode
, struct file
*file
)
399 return single_open(file
, lstats_show_proc
, inode
);
402 static ssize_t
lstats_write(struct file
*file
, const char __user
*buf
,
403 size_t count
, loff_t
*offs
)
405 struct task_struct
*task
= get_proc_task(file
->f_dentry
->d_inode
);
409 clear_all_latency_tracing(task
);
410 put_task_struct(task
);
415 static const struct file_operations proc_lstats_operations
= {
418 .write
= lstats_write
,
420 .release
= single_release
,
425 /* The badness from the OOM killer */
426 unsigned long badness(struct task_struct
*p
, unsigned long uptime
);
427 static int proc_oom_score(struct task_struct
*task
, char *buffer
)
429 unsigned long points
;
430 struct timespec uptime
;
432 do_posix_clock_monotonic_gettime(&uptime
);
433 read_lock(&tasklist_lock
);
434 points
= badness(task
, uptime
.tv_sec
);
435 read_unlock(&tasklist_lock
);
436 return sprintf(buffer
, "%lu\n", points
);
444 static const struct limit_names lnames
[RLIM_NLIMITS
] = {
445 [RLIMIT_CPU
] = {"Max cpu time", "ms"},
446 [RLIMIT_FSIZE
] = {"Max file size", "bytes"},
447 [RLIMIT_DATA
] = {"Max data size", "bytes"},
448 [RLIMIT_STACK
] = {"Max stack size", "bytes"},
449 [RLIMIT_CORE
] = {"Max core file size", "bytes"},
450 [RLIMIT_RSS
] = {"Max resident set", "bytes"},
451 [RLIMIT_NPROC
] = {"Max processes", "processes"},
452 [RLIMIT_NOFILE
] = {"Max open files", "files"},
453 [RLIMIT_MEMLOCK
] = {"Max locked memory", "bytes"},
454 [RLIMIT_AS
] = {"Max address space", "bytes"},
455 [RLIMIT_LOCKS
] = {"Max file locks", "locks"},
456 [RLIMIT_SIGPENDING
] = {"Max pending signals", "signals"},
457 [RLIMIT_MSGQUEUE
] = {"Max msgqueue size", "bytes"},
458 [RLIMIT_NICE
] = {"Max nice priority", NULL
},
459 [RLIMIT_RTPRIO
] = {"Max realtime priority", NULL
},
460 [RLIMIT_RTTIME
] = {"Max realtime timeout", "us"},
463 /* Display limits for a process */
464 static int proc_pid_limits(struct task_struct
*task
, char *buffer
)
469 char *bufptr
= buffer
;
471 struct rlimit rlim
[RLIM_NLIMITS
];
473 if (!lock_task_sighand(task
, &flags
))
475 memcpy(rlim
, task
->signal
->rlim
, sizeof(struct rlimit
) * RLIM_NLIMITS
);
476 unlock_task_sighand(task
, &flags
);
479 * print the file header
481 count
+= sprintf(&bufptr
[count
], "%-25s %-20s %-20s %-10s\n",
482 "Limit", "Soft Limit", "Hard Limit", "Units");
484 for (i
= 0; i
< RLIM_NLIMITS
; i
++) {
485 if (rlim
[i
].rlim_cur
== RLIM_INFINITY
)
486 count
+= sprintf(&bufptr
[count
], "%-25s %-20s ",
487 lnames
[i
].name
, "unlimited");
489 count
+= sprintf(&bufptr
[count
], "%-25s %-20lu ",
490 lnames
[i
].name
, rlim
[i
].rlim_cur
);
492 if (rlim
[i
].rlim_max
== RLIM_INFINITY
)
493 count
+= sprintf(&bufptr
[count
], "%-20s ", "unlimited");
495 count
+= sprintf(&bufptr
[count
], "%-20lu ",
499 count
+= sprintf(&bufptr
[count
], "%-10s\n",
502 count
+= sprintf(&bufptr
[count
], "\n");
508 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
509 static int proc_pid_syscall(struct task_struct
*task
, char *buffer
)
512 unsigned long args
[6], sp
, pc
;
514 if (task_current_syscall(task
, &nr
, args
, 6, &sp
, &pc
))
515 return sprintf(buffer
, "running\n");
518 return sprintf(buffer
, "%ld 0x%lx 0x%lx\n", nr
, sp
, pc
);
520 return sprintf(buffer
,
521 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
523 args
[0], args
[1], args
[2], args
[3], args
[4], args
[5],
526 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
528 /************************************************************************/
529 /* Here the fs part begins */
530 /************************************************************************/
532 /* permission checks */
533 static int proc_fd_access_allowed(struct inode
*inode
)
535 struct task_struct
*task
;
537 /* Allow access to a task's file descriptors if it is us or we
538 * may use ptrace attach to the process and find out that
541 task
= get_proc_task(inode
);
543 allowed
= ptrace_may_access(task
, PTRACE_MODE_READ
);
544 put_task_struct(task
);
549 static int proc_setattr(struct dentry
*dentry
, struct iattr
*attr
)
552 struct inode
*inode
= dentry
->d_inode
;
554 if (attr
->ia_valid
& ATTR_MODE
)
557 error
= inode_change_ok(inode
, attr
);
559 error
= inode_setattr(inode
, attr
);
563 static const struct inode_operations proc_def_inode_operations
= {
564 .setattr
= proc_setattr
,
567 static int mounts_open_common(struct inode
*inode
, struct file
*file
,
568 const struct seq_operations
*op
)
570 struct task_struct
*task
= get_proc_task(inode
);
572 struct mnt_namespace
*ns
= NULL
;
573 struct fs_struct
*fs
= NULL
;
575 struct proc_mounts
*p
;
580 nsp
= task_nsproxy(task
);
588 fs
= get_fs_struct(task
);
589 put_task_struct(task
);
597 read_lock(&fs
->lock
);
600 read_unlock(&fs
->lock
);
604 p
= kmalloc(sizeof(struct proc_mounts
), GFP_KERNEL
);
608 file
->private_data
= &p
->m
;
609 ret
= seq_open(file
, op
);
616 p
->event
= ns
->event
;
630 static int mounts_release(struct inode
*inode
, struct file
*file
)
632 struct proc_mounts
*p
= file
->private_data
;
635 return seq_release(inode
, file
);
638 static unsigned mounts_poll(struct file
*file
, poll_table
*wait
)
640 struct proc_mounts
*p
= file
->private_data
;
641 struct mnt_namespace
*ns
= p
->ns
;
644 poll_wait(file
, &ns
->poll
, wait
);
646 spin_lock(&vfsmount_lock
);
647 if (p
->event
!= ns
->event
) {
648 p
->event
= ns
->event
;
651 spin_unlock(&vfsmount_lock
);
656 static int mounts_open(struct inode
*inode
, struct file
*file
)
658 return mounts_open_common(inode
, file
, &mounts_op
);
661 static const struct file_operations proc_mounts_operations
= {
665 .release
= mounts_release
,
669 static int mountinfo_open(struct inode
*inode
, struct file
*file
)
671 return mounts_open_common(inode
, file
, &mountinfo_op
);
674 static const struct file_operations proc_mountinfo_operations
= {
675 .open
= mountinfo_open
,
678 .release
= mounts_release
,
682 static int mountstats_open(struct inode
*inode
, struct file
*file
)
684 return mounts_open_common(inode
, file
, &mountstats_op
);
687 static const struct file_operations proc_mountstats_operations
= {
688 .open
= mountstats_open
,
691 .release
= mounts_release
,
694 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
696 static ssize_t
proc_info_read(struct file
* file
, char __user
* buf
,
697 size_t count
, loff_t
*ppos
)
699 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
702 struct task_struct
*task
= get_proc_task(inode
);
708 if (count
> PROC_BLOCK_SIZE
)
709 count
= PROC_BLOCK_SIZE
;
712 if (!(page
= __get_free_page(GFP_TEMPORARY
)))
715 length
= PROC_I(inode
)->op
.proc_read(task
, (char*)page
);
718 length
= simple_read_from_buffer(buf
, count
, ppos
, (char *)page
, length
);
721 put_task_struct(task
);
726 static const struct file_operations proc_info_file_operations
= {
727 .read
= proc_info_read
,
730 static int proc_single_show(struct seq_file
*m
, void *v
)
732 struct inode
*inode
= m
->private;
733 struct pid_namespace
*ns
;
735 struct task_struct
*task
;
738 ns
= inode
->i_sb
->s_fs_info
;
739 pid
= proc_pid(inode
);
740 task
= get_pid_task(pid
, PIDTYPE_PID
);
744 ret
= PROC_I(inode
)->op
.proc_show(m
, ns
, pid
, task
);
746 put_task_struct(task
);
750 static int proc_single_open(struct inode
*inode
, struct file
*filp
)
753 ret
= single_open(filp
, proc_single_show
, NULL
);
755 struct seq_file
*m
= filp
->private_data
;
762 static const struct file_operations proc_single_file_operations
= {
763 .open
= proc_single_open
,
766 .release
= single_release
,
769 static int mem_open(struct inode
* inode
, struct file
* file
)
771 file
->private_data
= (void*)((long)current
->self_exec_id
);
775 static ssize_t
mem_read(struct file
* file
, char __user
* buf
,
776 size_t count
, loff_t
*ppos
)
778 struct task_struct
*task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
780 unsigned long src
= *ppos
;
782 struct mm_struct
*mm
;
787 if (check_mem_permission(task
))
791 page
= (char *)__get_free_page(GFP_TEMPORARY
);
797 mm
= get_task_mm(task
);
803 if (file
->private_data
!= (void*)((long)current
->self_exec_id
))
809 int this_len
, retval
;
811 this_len
= (count
> PAGE_SIZE
) ? PAGE_SIZE
: count
;
812 retval
= access_process_vm(task
, src
, page
, this_len
, 0);
813 if (!retval
|| check_mem_permission(task
)) {
819 if (copy_to_user(buf
, page
, retval
)) {
834 free_page((unsigned long) page
);
836 put_task_struct(task
);
841 #define mem_write NULL
844 /* This is a security hazard */
845 static ssize_t
mem_write(struct file
* file
, const char __user
*buf
,
846 size_t count
, loff_t
*ppos
)
850 struct task_struct
*task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
851 unsigned long dst
= *ppos
;
857 if (check_mem_permission(task
))
861 page
= (char *)__get_free_page(GFP_TEMPORARY
);
867 int this_len
, retval
;
869 this_len
= (count
> PAGE_SIZE
) ? PAGE_SIZE
: count
;
870 if (copy_from_user(page
, buf
, this_len
)) {
874 retval
= access_process_vm(task
, dst
, page
, this_len
, 1);
886 free_page((unsigned long) page
);
888 put_task_struct(task
);
894 loff_t
mem_lseek(struct file
*file
, loff_t offset
, int orig
)
898 file
->f_pos
= offset
;
901 file
->f_pos
+= offset
;
906 force_successful_syscall_return();
910 static const struct file_operations proc_mem_operations
= {
917 static ssize_t
environ_read(struct file
*file
, char __user
*buf
,
918 size_t count
, loff_t
*ppos
)
920 struct task_struct
*task
= get_proc_task(file
->f_dentry
->d_inode
);
922 unsigned long src
= *ppos
;
924 struct mm_struct
*mm
;
929 if (!ptrace_may_access(task
, PTRACE_MODE_READ
))
933 page
= (char *)__get_free_page(GFP_TEMPORARY
);
939 mm
= get_task_mm(task
);
944 int this_len
, retval
, max_len
;
946 this_len
= mm
->env_end
- (mm
->env_start
+ src
);
951 max_len
= (count
> PAGE_SIZE
) ? PAGE_SIZE
: count
;
952 this_len
= (this_len
> max_len
) ? max_len
: this_len
;
954 retval
= access_process_vm(task
, (mm
->env_start
+ src
),
962 if (copy_to_user(buf
, page
, retval
)) {
976 free_page((unsigned long) page
);
978 put_task_struct(task
);
983 static const struct file_operations proc_environ_operations
= {
984 .read
= environ_read
,
987 static ssize_t
oom_adjust_read(struct file
*file
, char __user
*buf
,
988 size_t count
, loff_t
*ppos
)
990 struct task_struct
*task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
991 char buffer
[PROC_NUMBUF
];
997 oom_adjust
= task
->oomkilladj
;
998 put_task_struct(task
);
1000 len
= snprintf(buffer
, sizeof(buffer
), "%i\n", oom_adjust
);
1002 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
1005 static ssize_t
oom_adjust_write(struct file
*file
, const char __user
*buf
,
1006 size_t count
, loff_t
*ppos
)
1008 struct task_struct
*task
;
1009 char buffer
[PROC_NUMBUF
], *end
;
1012 memset(buffer
, 0, sizeof(buffer
));
1013 if (count
> sizeof(buffer
) - 1)
1014 count
= sizeof(buffer
) - 1;
1015 if (copy_from_user(buffer
, buf
, count
))
1017 oom_adjust
= simple_strtol(buffer
, &end
, 0);
1018 if ((oom_adjust
< OOM_ADJUST_MIN
|| oom_adjust
> OOM_ADJUST_MAX
) &&
1019 oom_adjust
!= OOM_DISABLE
)
1023 task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
1026 if (oom_adjust
< task
->oomkilladj
&& !capable(CAP_SYS_RESOURCE
)) {
1027 put_task_struct(task
);
1030 task
->oomkilladj
= oom_adjust
;
1031 put_task_struct(task
);
1032 if (end
- buffer
== 0)
1034 return end
- buffer
;
1037 static const struct file_operations proc_oom_adjust_operations
= {
1038 .read
= oom_adjust_read
,
1039 .write
= oom_adjust_write
,
1042 #ifdef CONFIG_AUDITSYSCALL
1043 #define TMPBUFLEN 21
1044 static ssize_t
proc_loginuid_read(struct file
* file
, char __user
* buf
,
1045 size_t count
, loff_t
*ppos
)
1047 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
1048 struct task_struct
*task
= get_proc_task(inode
);
1050 char tmpbuf
[TMPBUFLEN
];
1054 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
1055 audit_get_loginuid(task
));
1056 put_task_struct(task
);
1057 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1060 static ssize_t
proc_loginuid_write(struct file
* file
, const char __user
* buf
,
1061 size_t count
, loff_t
*ppos
)
1063 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
1068 if (!capable(CAP_AUDIT_CONTROL
))
1071 if (current
!= pid_task(proc_pid(inode
), PIDTYPE_PID
))
1074 if (count
>= PAGE_SIZE
)
1075 count
= PAGE_SIZE
- 1;
1078 /* No partial writes. */
1081 page
= (char*)__get_free_page(GFP_TEMPORARY
);
1085 if (copy_from_user(page
, buf
, count
))
1089 loginuid
= simple_strtoul(page
, &tmp
, 10);
1095 length
= audit_set_loginuid(current
, loginuid
);
1096 if (likely(length
== 0))
1100 free_page((unsigned long) page
);
1104 static const struct file_operations proc_loginuid_operations
= {
1105 .read
= proc_loginuid_read
,
1106 .write
= proc_loginuid_write
,
1109 static ssize_t
proc_sessionid_read(struct file
* file
, char __user
* buf
,
1110 size_t count
, loff_t
*ppos
)
1112 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
1113 struct task_struct
*task
= get_proc_task(inode
);
1115 char tmpbuf
[TMPBUFLEN
];
1119 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
1120 audit_get_sessionid(task
));
1121 put_task_struct(task
);
1122 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1125 static const struct file_operations proc_sessionid_operations
= {
1126 .read
= proc_sessionid_read
,
1130 #ifdef CONFIG_FAULT_INJECTION
1131 static ssize_t
proc_fault_inject_read(struct file
* file
, char __user
* buf
,
1132 size_t count
, loff_t
*ppos
)
1134 struct task_struct
*task
= get_proc_task(file
->f_dentry
->d_inode
);
1135 char buffer
[PROC_NUMBUF
];
1141 make_it_fail
= task
->make_it_fail
;
1142 put_task_struct(task
);
1144 len
= snprintf(buffer
, sizeof(buffer
), "%i\n", make_it_fail
);
1146 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
1149 static ssize_t
proc_fault_inject_write(struct file
* file
,
1150 const char __user
* buf
, size_t count
, loff_t
*ppos
)
1152 struct task_struct
*task
;
1153 char buffer
[PROC_NUMBUF
], *end
;
1156 if (!capable(CAP_SYS_RESOURCE
))
1158 memset(buffer
, 0, sizeof(buffer
));
1159 if (count
> sizeof(buffer
) - 1)
1160 count
= sizeof(buffer
) - 1;
1161 if (copy_from_user(buffer
, buf
, count
))
1163 make_it_fail
= simple_strtol(buffer
, &end
, 0);
1166 task
= get_proc_task(file
->f_dentry
->d_inode
);
1169 task
->make_it_fail
= make_it_fail
;
1170 put_task_struct(task
);
1171 if (end
- buffer
== 0)
1173 return end
- buffer
;
1176 static const struct file_operations proc_fault_inject_operations
= {
1177 .read
= proc_fault_inject_read
,
1178 .write
= proc_fault_inject_write
,
1183 #ifdef CONFIG_SCHED_DEBUG
1185 * Print out various scheduling related per-task fields:
1187 static int sched_show(struct seq_file
*m
, void *v
)
1189 struct inode
*inode
= m
->private;
1190 struct task_struct
*p
;
1194 p
= get_proc_task(inode
);
1197 proc_sched_show_task(p
, m
);
1205 sched_write(struct file
*file
, const char __user
*buf
,
1206 size_t count
, loff_t
*offset
)
1208 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1209 struct task_struct
*p
;
1213 p
= get_proc_task(inode
);
1216 proc_sched_set_task(p
);
1223 static int sched_open(struct inode
*inode
, struct file
*filp
)
1227 ret
= single_open(filp
, sched_show
, NULL
);
1229 struct seq_file
*m
= filp
->private_data
;
1236 static const struct file_operations proc_pid_sched_operations
= {
1239 .write
= sched_write
,
1240 .llseek
= seq_lseek
,
1241 .release
= single_release
,
1247 * We added or removed a vma mapping the executable. The vmas are only mapped
1248 * during exec and are not mapped with the mmap system call.
1249 * Callers must hold down_write() on the mm's mmap_sem for these
1251 void added_exe_file_vma(struct mm_struct
*mm
)
1253 mm
->num_exe_file_vmas
++;
1256 void removed_exe_file_vma(struct mm_struct
*mm
)
1258 mm
->num_exe_file_vmas
--;
1259 if ((mm
->num_exe_file_vmas
== 0) && mm
->exe_file
){
1261 mm
->exe_file
= NULL
;
1266 void set_mm_exe_file(struct mm_struct
*mm
, struct file
*new_exe_file
)
1269 get_file(new_exe_file
);
1272 mm
->exe_file
= new_exe_file
;
1273 mm
->num_exe_file_vmas
= 0;
1276 struct file
*get_mm_exe_file(struct mm_struct
*mm
)
1278 struct file
*exe_file
;
1280 /* We need mmap_sem to protect against races with removal of
1281 * VM_EXECUTABLE vmas */
1282 down_read(&mm
->mmap_sem
);
1283 exe_file
= mm
->exe_file
;
1286 up_read(&mm
->mmap_sem
);
1290 void dup_mm_exe_file(struct mm_struct
*oldmm
, struct mm_struct
*newmm
)
1292 /* It's safe to write the exe_file pointer without exe_file_lock because
1293 * this is called during fork when the task is not yet in /proc */
1294 newmm
->exe_file
= get_mm_exe_file(oldmm
);
1297 static int proc_exe_link(struct inode
*inode
, struct path
*exe_path
)
1299 struct task_struct
*task
;
1300 struct mm_struct
*mm
;
1301 struct file
*exe_file
;
1303 task
= get_proc_task(inode
);
1306 mm
= get_task_mm(task
);
1307 put_task_struct(task
);
1310 exe_file
= get_mm_exe_file(mm
);
1313 *exe_path
= exe_file
->f_path
;
1314 path_get(&exe_file
->f_path
);
1321 static void *proc_pid_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
1323 struct inode
*inode
= dentry
->d_inode
;
1324 int error
= -EACCES
;
1326 /* We don't need a base pointer in the /proc filesystem */
1327 path_put(&nd
->path
);
1329 /* Are we allowed to snoop on the tasks file descriptors? */
1330 if (!proc_fd_access_allowed(inode
))
1333 error
= PROC_I(inode
)->op
.proc_get_link(inode
, &nd
->path
);
1334 nd
->last_type
= LAST_BIND
;
1336 return ERR_PTR(error
);
1339 static int do_proc_readlink(struct path
*path
, char __user
*buffer
, int buflen
)
1341 char *tmp
= (char*)__get_free_page(GFP_TEMPORARY
);
1348 pathname
= d_path(path
, tmp
, PAGE_SIZE
);
1349 len
= PTR_ERR(pathname
);
1350 if (IS_ERR(pathname
))
1352 len
= tmp
+ PAGE_SIZE
- 1 - pathname
;
1356 if (copy_to_user(buffer
, pathname
, len
))
1359 free_page((unsigned long)tmp
);
1363 static int proc_pid_readlink(struct dentry
* dentry
, char __user
* buffer
, int buflen
)
1365 int error
= -EACCES
;
1366 struct inode
*inode
= dentry
->d_inode
;
1369 /* Are we allowed to snoop on the tasks file descriptors? */
1370 if (!proc_fd_access_allowed(inode
))
1373 error
= PROC_I(inode
)->op
.proc_get_link(inode
, &path
);
1377 error
= do_proc_readlink(&path
, buffer
, buflen
);
1383 static const struct inode_operations proc_pid_link_inode_operations
= {
1384 .readlink
= proc_pid_readlink
,
1385 .follow_link
= proc_pid_follow_link
,
1386 .setattr
= proc_setattr
,
1390 /* building an inode */
1392 static int task_dumpable(struct task_struct
*task
)
1395 struct mm_struct
*mm
;
1400 dumpable
= get_dumpable(mm
);
1408 static struct inode
*proc_pid_make_inode(struct super_block
* sb
, struct task_struct
*task
)
1410 struct inode
* inode
;
1411 struct proc_inode
*ei
;
1413 /* We need a new inode */
1415 inode
= new_inode(sb
);
1421 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1422 inode
->i_op
= &proc_def_inode_operations
;
1425 * grab the reference to task.
1427 ei
->pid
= get_task_pid(task
, PIDTYPE_PID
);
1433 if (task_dumpable(task
)) {
1434 inode
->i_uid
= task
->euid
;
1435 inode
->i_gid
= task
->egid
;
1437 security_task_to_inode(task
, inode
);
1447 static int pid_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
1449 struct inode
*inode
= dentry
->d_inode
;
1450 struct task_struct
*task
;
1451 generic_fillattr(inode
, stat
);
1456 task
= pid_task(proc_pid(inode
), PIDTYPE_PID
);
1458 if ((inode
->i_mode
== (S_IFDIR
|S_IRUGO
|S_IXUGO
)) ||
1459 task_dumpable(task
)) {
1460 stat
->uid
= task
->euid
;
1461 stat
->gid
= task
->egid
;
1471 * Exceptional case: normally we are not allowed to unhash a busy
1472 * directory. In this case, however, we can do it - no aliasing problems
1473 * due to the way we treat inodes.
1475 * Rewrite the inode's ownerships here because the owning task may have
1476 * performed a setuid(), etc.
1478 * Before the /proc/pid/status file was created the only way to read
1479 * the effective uid of a /process was to stat /proc/pid. Reading
1480 * /proc/pid/status is slow enough that procps and other packages
1481 * kept stating /proc/pid. To keep the rules in /proc simple I have
1482 * made this apply to all per process world readable and executable
1485 static int pid_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1487 struct inode
*inode
= dentry
->d_inode
;
1488 struct task_struct
*task
= get_proc_task(inode
);
1490 if ((inode
->i_mode
== (S_IFDIR
|S_IRUGO
|S_IXUGO
)) ||
1491 task_dumpable(task
)) {
1492 inode
->i_uid
= task
->euid
;
1493 inode
->i_gid
= task
->egid
;
1498 inode
->i_mode
&= ~(S_ISUID
| S_ISGID
);
1499 security_task_to_inode(task
, inode
);
1500 put_task_struct(task
);
1507 static int pid_delete_dentry(struct dentry
* dentry
)
1509 /* Is the task we represent dead?
1510 * If so, then don't put the dentry on the lru list,
1511 * kill it immediately.
1513 return !proc_pid(dentry
->d_inode
)->tasks
[PIDTYPE_PID
].first
;
1516 static struct dentry_operations pid_dentry_operations
=
1518 .d_revalidate
= pid_revalidate
,
1519 .d_delete
= pid_delete_dentry
,
1524 typedef struct dentry
*instantiate_t(struct inode
*, struct dentry
*,
1525 struct task_struct
*, const void *);
1528 * Fill a directory entry.
1530 * If possible create the dcache entry and derive our inode number and
1531 * file type from dcache entry.
1533 * Since all of the proc inode numbers are dynamically generated, the inode
1534 * numbers do not exist until the inode is cache. This means creating the
1535 * the dcache entry in readdir is necessary to keep the inode numbers
1536 * reported by readdir in sync with the inode numbers reported
1539 static int proc_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
1540 char *name
, int len
,
1541 instantiate_t instantiate
, struct task_struct
*task
, const void *ptr
)
1543 struct dentry
*child
, *dir
= filp
->f_path
.dentry
;
1544 struct inode
*inode
;
1547 unsigned type
= DT_UNKNOWN
;
1551 qname
.hash
= full_name_hash(name
, len
);
1553 child
= d_lookup(dir
, &qname
);
1556 new = d_alloc(dir
, &qname
);
1558 child
= instantiate(dir
->d_inode
, new, task
, ptr
);
1565 if (!child
|| IS_ERR(child
) || !child
->d_inode
)
1566 goto end_instantiate
;
1567 inode
= child
->d_inode
;
1570 type
= inode
->i_mode
>> 12;
1575 ino
= find_inode_number(dir
, &qname
);
1578 return filldir(dirent
, name
, len
, filp
->f_pos
, ino
, type
);
1581 static unsigned name_to_int(struct dentry
*dentry
)
1583 const char *name
= dentry
->d_name
.name
;
1584 int len
= dentry
->d_name
.len
;
1587 if (len
> 1 && *name
== '0')
1590 unsigned c
= *name
++ - '0';
1593 if (n
>= (~0U-9)/10)
1603 #define PROC_FDINFO_MAX 64
1605 static int proc_fd_info(struct inode
*inode
, struct path
*path
, char *info
)
1607 struct task_struct
*task
= get_proc_task(inode
);
1608 struct files_struct
*files
= NULL
;
1610 int fd
= proc_fd(inode
);
1613 files
= get_files_struct(task
);
1614 put_task_struct(task
);
1618 * We are not taking a ref to the file structure, so we must
1621 spin_lock(&files
->file_lock
);
1622 file
= fcheck_files(files
, fd
);
1625 *path
= file
->f_path
;
1626 path_get(&file
->f_path
);
1629 snprintf(info
, PROC_FDINFO_MAX
,
1632 (long long) file
->f_pos
,
1634 spin_unlock(&files
->file_lock
);
1635 put_files_struct(files
);
1638 spin_unlock(&files
->file_lock
);
1639 put_files_struct(files
);
1644 static int proc_fd_link(struct inode
*inode
, struct path
*path
)
1646 return proc_fd_info(inode
, path
, NULL
);
1649 static int tid_fd_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1651 struct inode
*inode
= dentry
->d_inode
;
1652 struct task_struct
*task
= get_proc_task(inode
);
1653 int fd
= proc_fd(inode
);
1654 struct files_struct
*files
;
1657 files
= get_files_struct(task
);
1660 if (fcheck_files(files
, fd
)) {
1662 put_files_struct(files
);
1663 if (task_dumpable(task
)) {
1664 inode
->i_uid
= task
->euid
;
1665 inode
->i_gid
= task
->egid
;
1670 inode
->i_mode
&= ~(S_ISUID
| S_ISGID
);
1671 security_task_to_inode(task
, inode
);
1672 put_task_struct(task
);
1676 put_files_struct(files
);
1678 put_task_struct(task
);
1684 static struct dentry_operations tid_fd_dentry_operations
=
1686 .d_revalidate
= tid_fd_revalidate
,
1687 .d_delete
= pid_delete_dentry
,
1690 static struct dentry
*proc_fd_instantiate(struct inode
*dir
,
1691 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
1693 unsigned fd
= *(const unsigned *)ptr
;
1695 struct files_struct
*files
;
1696 struct inode
*inode
;
1697 struct proc_inode
*ei
;
1698 struct dentry
*error
= ERR_PTR(-ENOENT
);
1700 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
1705 files
= get_files_struct(task
);
1708 inode
->i_mode
= S_IFLNK
;
1711 * We are not taking a ref to the file structure, so we must
1714 spin_lock(&files
->file_lock
);
1715 file
= fcheck_files(files
, fd
);
1718 if (file
->f_mode
& 1)
1719 inode
->i_mode
|= S_IRUSR
| S_IXUSR
;
1720 if (file
->f_mode
& 2)
1721 inode
->i_mode
|= S_IWUSR
| S_IXUSR
;
1722 spin_unlock(&files
->file_lock
);
1723 put_files_struct(files
);
1725 inode
->i_op
= &proc_pid_link_inode_operations
;
1727 ei
->op
.proc_get_link
= proc_fd_link
;
1728 dentry
->d_op
= &tid_fd_dentry_operations
;
1729 d_add(dentry
, inode
);
1730 /* Close the race of the process dying before we return the dentry */
1731 if (tid_fd_revalidate(dentry
, NULL
))
1737 spin_unlock(&files
->file_lock
);
1738 put_files_struct(files
);
1744 static struct dentry
*proc_lookupfd_common(struct inode
*dir
,
1745 struct dentry
*dentry
,
1746 instantiate_t instantiate
)
1748 struct task_struct
*task
= get_proc_task(dir
);
1749 unsigned fd
= name_to_int(dentry
);
1750 struct dentry
*result
= ERR_PTR(-ENOENT
);
1757 result
= instantiate(dir
, dentry
, task
, &fd
);
1759 put_task_struct(task
);
1764 static int proc_readfd_common(struct file
* filp
, void * dirent
,
1765 filldir_t filldir
, instantiate_t instantiate
)
1767 struct dentry
*dentry
= filp
->f_path
.dentry
;
1768 struct inode
*inode
= dentry
->d_inode
;
1769 struct task_struct
*p
= get_proc_task(inode
);
1770 unsigned int fd
, ino
;
1772 struct files_struct
* files
;
1782 if (filldir(dirent
, ".", 1, 0, inode
->i_ino
, DT_DIR
) < 0)
1786 ino
= parent_ino(dentry
);
1787 if (filldir(dirent
, "..", 2, 1, ino
, DT_DIR
) < 0)
1791 files
= get_files_struct(p
);
1795 for (fd
= filp
->f_pos
-2;
1796 fd
< files_fdtable(files
)->max_fds
;
1797 fd
++, filp
->f_pos
++) {
1798 char name
[PROC_NUMBUF
];
1801 if (!fcheck_files(files
, fd
))
1805 len
= snprintf(name
, sizeof(name
), "%d", fd
);
1806 if (proc_fill_cache(filp
, dirent
, filldir
,
1807 name
, len
, instantiate
,
1815 put_files_struct(files
);
1823 static struct dentry
*proc_lookupfd(struct inode
*dir
, struct dentry
*dentry
,
1824 struct nameidata
*nd
)
1826 return proc_lookupfd_common(dir
, dentry
, proc_fd_instantiate
);
1829 static int proc_readfd(struct file
*filp
, void *dirent
, filldir_t filldir
)
1831 return proc_readfd_common(filp
, dirent
, filldir
, proc_fd_instantiate
);
1834 static ssize_t
proc_fdinfo_read(struct file
*file
, char __user
*buf
,
1835 size_t len
, loff_t
*ppos
)
1837 char tmp
[PROC_FDINFO_MAX
];
1838 int err
= proc_fd_info(file
->f_path
.dentry
->d_inode
, NULL
, tmp
);
1840 err
= simple_read_from_buffer(buf
, len
, ppos
, tmp
, strlen(tmp
));
1844 static const struct file_operations proc_fdinfo_file_operations
= {
1845 .open
= nonseekable_open
,
1846 .read
= proc_fdinfo_read
,
1849 static const struct file_operations proc_fd_operations
= {
1850 .read
= generic_read_dir
,
1851 .readdir
= proc_readfd
,
1855 * /proc/pid/fd needs a special permission handler so that a process can still
1856 * access /proc/self/fd after it has executed a setuid().
1858 static int proc_fd_permission(struct inode
*inode
, int mask
)
1862 rv
= generic_permission(inode
, mask
, NULL
);
1865 if (task_pid(current
) == proc_pid(inode
))
1871 * proc directories can do almost nothing..
1873 static const struct inode_operations proc_fd_inode_operations
= {
1874 .lookup
= proc_lookupfd
,
1875 .permission
= proc_fd_permission
,
1876 .setattr
= proc_setattr
,
1879 static struct dentry
*proc_fdinfo_instantiate(struct inode
*dir
,
1880 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
1882 unsigned fd
= *(unsigned *)ptr
;
1883 struct inode
*inode
;
1884 struct proc_inode
*ei
;
1885 struct dentry
*error
= ERR_PTR(-ENOENT
);
1887 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
1892 inode
->i_mode
= S_IFREG
| S_IRUSR
;
1893 inode
->i_fop
= &proc_fdinfo_file_operations
;
1894 dentry
->d_op
= &tid_fd_dentry_operations
;
1895 d_add(dentry
, inode
);
1896 /* Close the race of the process dying before we return the dentry */
1897 if (tid_fd_revalidate(dentry
, NULL
))
1904 static struct dentry
*proc_lookupfdinfo(struct inode
*dir
,
1905 struct dentry
*dentry
,
1906 struct nameidata
*nd
)
1908 return proc_lookupfd_common(dir
, dentry
, proc_fdinfo_instantiate
);
1911 static int proc_readfdinfo(struct file
*filp
, void *dirent
, filldir_t filldir
)
1913 return proc_readfd_common(filp
, dirent
, filldir
,
1914 proc_fdinfo_instantiate
);
1917 static const struct file_operations proc_fdinfo_operations
= {
1918 .read
= generic_read_dir
,
1919 .readdir
= proc_readfdinfo
,
1923 * proc directories can do almost nothing..
1925 static const struct inode_operations proc_fdinfo_inode_operations
= {
1926 .lookup
= proc_lookupfdinfo
,
1927 .setattr
= proc_setattr
,
1931 static struct dentry
*proc_pident_instantiate(struct inode
*dir
,
1932 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
1934 const struct pid_entry
*p
= ptr
;
1935 struct inode
*inode
;
1936 struct proc_inode
*ei
;
1937 struct dentry
*error
= ERR_PTR(-EINVAL
);
1939 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
1944 inode
->i_mode
= p
->mode
;
1945 if (S_ISDIR(inode
->i_mode
))
1946 inode
->i_nlink
= 2; /* Use getattr to fix if necessary */
1948 inode
->i_op
= p
->iop
;
1950 inode
->i_fop
= p
->fop
;
1952 dentry
->d_op
= &pid_dentry_operations
;
1953 d_add(dentry
, inode
);
1954 /* Close the race of the process dying before we return the dentry */
1955 if (pid_revalidate(dentry
, NULL
))
1961 static struct dentry
*proc_pident_lookup(struct inode
*dir
,
1962 struct dentry
*dentry
,
1963 const struct pid_entry
*ents
,
1966 struct inode
*inode
;
1967 struct dentry
*error
;
1968 struct task_struct
*task
= get_proc_task(dir
);
1969 const struct pid_entry
*p
, *last
;
1971 error
= ERR_PTR(-ENOENT
);
1978 * Yes, it does not scale. And it should not. Don't add
1979 * new entries into /proc/<tgid>/ without very good reasons.
1981 last
= &ents
[nents
- 1];
1982 for (p
= ents
; p
<= last
; p
++) {
1983 if (p
->len
!= dentry
->d_name
.len
)
1985 if (!memcmp(dentry
->d_name
.name
, p
->name
, p
->len
))
1991 error
= proc_pident_instantiate(dir
, dentry
, task
, p
);
1993 put_task_struct(task
);
1998 static int proc_pident_fill_cache(struct file
*filp
, void *dirent
,
1999 filldir_t filldir
, struct task_struct
*task
, const struct pid_entry
*p
)
2001 return proc_fill_cache(filp
, dirent
, filldir
, p
->name
, p
->len
,
2002 proc_pident_instantiate
, task
, p
);
2005 static int proc_pident_readdir(struct file
*filp
,
2006 void *dirent
, filldir_t filldir
,
2007 const struct pid_entry
*ents
, unsigned int nents
)
2010 struct dentry
*dentry
= filp
->f_path
.dentry
;
2011 struct inode
*inode
= dentry
->d_inode
;
2012 struct task_struct
*task
= get_proc_task(inode
);
2013 const struct pid_entry
*p
, *last
;
2026 if (filldir(dirent
, ".", 1, i
, ino
, DT_DIR
) < 0)
2032 ino
= parent_ino(dentry
);
2033 if (filldir(dirent
, "..", 2, i
, ino
, DT_DIR
) < 0)
2045 last
= &ents
[nents
- 1];
2047 if (proc_pident_fill_cache(filp
, dirent
, filldir
, task
, p
) < 0)
2056 put_task_struct(task
);
2061 #ifdef CONFIG_SECURITY
2062 static ssize_t
proc_pid_attr_read(struct file
* file
, char __user
* buf
,
2063 size_t count
, loff_t
*ppos
)
2065 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
2068 struct task_struct
*task
= get_proc_task(inode
);
2073 length
= security_getprocattr(task
,
2074 (char*)file
->f_path
.dentry
->d_name
.name
,
2076 put_task_struct(task
);
2078 length
= simple_read_from_buffer(buf
, count
, ppos
, p
, length
);
2083 static ssize_t
proc_pid_attr_write(struct file
* file
, const char __user
* buf
,
2084 size_t count
, loff_t
*ppos
)
2086 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
2089 struct task_struct
*task
= get_proc_task(inode
);
2094 if (count
> PAGE_SIZE
)
2097 /* No partial writes. */
2103 page
= (char*)__get_free_page(GFP_TEMPORARY
);
2108 if (copy_from_user(page
, buf
, count
))
2111 length
= security_setprocattr(task
,
2112 (char*)file
->f_path
.dentry
->d_name
.name
,
2113 (void*)page
, count
);
2115 free_page((unsigned long) page
);
2117 put_task_struct(task
);
2122 static const struct file_operations proc_pid_attr_operations
= {
2123 .read
= proc_pid_attr_read
,
2124 .write
= proc_pid_attr_write
,
2127 static const struct pid_entry attr_dir_stuff
[] = {
2128 REG("current", S_IRUGO
|S_IWUGO
, pid_attr
),
2129 REG("prev", S_IRUGO
, pid_attr
),
2130 REG("exec", S_IRUGO
|S_IWUGO
, pid_attr
),
2131 REG("fscreate", S_IRUGO
|S_IWUGO
, pid_attr
),
2132 REG("keycreate", S_IRUGO
|S_IWUGO
, pid_attr
),
2133 REG("sockcreate", S_IRUGO
|S_IWUGO
, pid_attr
),
2136 static int proc_attr_dir_readdir(struct file
* filp
,
2137 void * dirent
, filldir_t filldir
)
2139 return proc_pident_readdir(filp
,dirent
,filldir
,
2140 attr_dir_stuff
,ARRAY_SIZE(attr_dir_stuff
));
2143 static const struct file_operations proc_attr_dir_operations
= {
2144 .read
= generic_read_dir
,
2145 .readdir
= proc_attr_dir_readdir
,
2148 static struct dentry
*proc_attr_dir_lookup(struct inode
*dir
,
2149 struct dentry
*dentry
, struct nameidata
*nd
)
2151 return proc_pident_lookup(dir
, dentry
,
2152 attr_dir_stuff
, ARRAY_SIZE(attr_dir_stuff
));
2155 static const struct inode_operations proc_attr_dir_inode_operations
= {
2156 .lookup
= proc_attr_dir_lookup
,
2157 .getattr
= pid_getattr
,
2158 .setattr
= proc_setattr
,
2163 #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2164 static ssize_t
proc_coredump_filter_read(struct file
*file
, char __user
*buf
,
2165 size_t count
, loff_t
*ppos
)
2167 struct task_struct
*task
= get_proc_task(file
->f_dentry
->d_inode
);
2168 struct mm_struct
*mm
;
2169 char buffer
[PROC_NUMBUF
];
2177 mm
= get_task_mm(task
);
2179 len
= snprintf(buffer
, sizeof(buffer
), "%08lx\n",
2180 ((mm
->flags
& MMF_DUMP_FILTER_MASK
) >>
2181 MMF_DUMP_FILTER_SHIFT
));
2183 ret
= simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
2186 put_task_struct(task
);
2191 static ssize_t
proc_coredump_filter_write(struct file
*file
,
2192 const char __user
*buf
,
2196 struct task_struct
*task
;
2197 struct mm_struct
*mm
;
2198 char buffer
[PROC_NUMBUF
], *end
;
2205 memset(buffer
, 0, sizeof(buffer
));
2206 if (count
> sizeof(buffer
) - 1)
2207 count
= sizeof(buffer
) - 1;
2208 if (copy_from_user(buffer
, buf
, count
))
2212 val
= (unsigned int)simple_strtoul(buffer
, &end
, 0);
2215 if (end
- buffer
== 0)
2219 task
= get_proc_task(file
->f_dentry
->d_inode
);
2224 mm
= get_task_mm(task
);
2228 for (i
= 0, mask
= 1; i
< MMF_DUMP_FILTER_BITS
; i
++, mask
<<= 1) {
2230 set_bit(i
+ MMF_DUMP_FILTER_SHIFT
, &mm
->flags
);
2232 clear_bit(i
+ MMF_DUMP_FILTER_SHIFT
, &mm
->flags
);
2237 put_task_struct(task
);
2242 static const struct file_operations proc_coredump_filter_operations
= {
2243 .read
= proc_coredump_filter_read
,
2244 .write
= proc_coredump_filter_write
,
2251 static int proc_self_readlink(struct dentry
*dentry
, char __user
*buffer
,
2254 struct pid_namespace
*ns
= dentry
->d_sb
->s_fs_info
;
2255 pid_t tgid
= task_tgid_nr_ns(current
, ns
);
2256 char tmp
[PROC_NUMBUF
];
2259 sprintf(tmp
, "%d", tgid
);
2260 return vfs_readlink(dentry
,buffer
,buflen
,tmp
);
2263 static void *proc_self_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
2265 struct pid_namespace
*ns
= dentry
->d_sb
->s_fs_info
;
2266 pid_t tgid
= task_tgid_nr_ns(current
, ns
);
2267 char tmp
[PROC_NUMBUF
];
2269 return ERR_PTR(-ENOENT
);
2270 sprintf(tmp
, "%d", task_tgid_nr_ns(current
, ns
));
2271 return ERR_PTR(vfs_follow_link(nd
,tmp
));
2274 static const struct inode_operations proc_self_inode_operations
= {
2275 .readlink
= proc_self_readlink
,
2276 .follow_link
= proc_self_follow_link
,
2282 * These are the directory entries in the root directory of /proc
2283 * that properly belong to the /proc filesystem, as they describe
2284 * describe something that is process related.
2286 static const struct pid_entry proc_base_stuff
[] = {
2287 NOD("self", S_IFLNK
|S_IRWXUGO
,
2288 &proc_self_inode_operations
, NULL
, {}),
2292 * Exceptional case: normally we are not allowed to unhash a busy
2293 * directory. In this case, however, we can do it - no aliasing problems
2294 * due to the way we treat inodes.
2296 static int proc_base_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
2298 struct inode
*inode
= dentry
->d_inode
;
2299 struct task_struct
*task
= get_proc_task(inode
);
2301 put_task_struct(task
);
2308 static struct dentry_operations proc_base_dentry_operations
=
2310 .d_revalidate
= proc_base_revalidate
,
2311 .d_delete
= pid_delete_dentry
,
2314 static struct dentry
*proc_base_instantiate(struct inode
*dir
,
2315 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
2317 const struct pid_entry
*p
= ptr
;
2318 struct inode
*inode
;
2319 struct proc_inode
*ei
;
2320 struct dentry
*error
= ERR_PTR(-EINVAL
);
2322 /* Allocate the inode */
2323 error
= ERR_PTR(-ENOMEM
);
2324 inode
= new_inode(dir
->i_sb
);
2328 /* Initialize the inode */
2330 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
2333 * grab the reference to the task.
2335 ei
->pid
= get_task_pid(task
, PIDTYPE_PID
);
2341 inode
->i_mode
= p
->mode
;
2342 if (S_ISDIR(inode
->i_mode
))
2344 if (S_ISLNK(inode
->i_mode
))
2347 inode
->i_op
= p
->iop
;
2349 inode
->i_fop
= p
->fop
;
2351 dentry
->d_op
= &proc_base_dentry_operations
;
2352 d_add(dentry
, inode
);
2361 static struct dentry
*proc_base_lookup(struct inode
*dir
, struct dentry
*dentry
)
2363 struct dentry
*error
;
2364 struct task_struct
*task
= get_proc_task(dir
);
2365 const struct pid_entry
*p
, *last
;
2367 error
= ERR_PTR(-ENOENT
);
2372 /* Lookup the directory entry */
2373 last
= &proc_base_stuff
[ARRAY_SIZE(proc_base_stuff
) - 1];
2374 for (p
= proc_base_stuff
; p
<= last
; p
++) {
2375 if (p
->len
!= dentry
->d_name
.len
)
2377 if (!memcmp(dentry
->d_name
.name
, p
->name
, p
->len
))
2383 error
= proc_base_instantiate(dir
, dentry
, task
, p
);
2386 put_task_struct(task
);
2391 static int proc_base_fill_cache(struct file
*filp
, void *dirent
,
2392 filldir_t filldir
, struct task_struct
*task
, const struct pid_entry
*p
)
2394 return proc_fill_cache(filp
, dirent
, filldir
, p
->name
, p
->len
,
2395 proc_base_instantiate
, task
, p
);
2398 #ifdef CONFIG_TASK_IO_ACCOUNTING
2399 static int do_io_accounting(struct task_struct
*task
, char *buffer
, int whole
)
2401 struct task_io_accounting acct
= task
->ioac
;
2402 unsigned long flags
;
2404 if (whole
&& lock_task_sighand(task
, &flags
)) {
2405 struct task_struct
*t
= task
;
2407 task_io_accounting_add(&acct
, &task
->signal
->ioac
);
2408 while_each_thread(task
, t
)
2409 task_io_accounting_add(&acct
, &t
->ioac
);
2411 unlock_task_sighand(task
, &flags
);
2413 return sprintf(buffer
,
2418 "read_bytes: %llu\n"
2419 "write_bytes: %llu\n"
2420 "cancelled_write_bytes: %llu\n",
2421 (unsigned long long)acct
.rchar
,
2422 (unsigned long long)acct
.wchar
,
2423 (unsigned long long)acct
.syscr
,
2424 (unsigned long long)acct
.syscw
,
2425 (unsigned long long)acct
.read_bytes
,
2426 (unsigned long long)acct
.write_bytes
,
2427 (unsigned long long)acct
.cancelled_write_bytes
);
2430 static int proc_tid_io_accounting(struct task_struct
*task
, char *buffer
)
2432 return do_io_accounting(task
, buffer
, 0);
2435 static int proc_tgid_io_accounting(struct task_struct
*task
, char *buffer
)
2437 return do_io_accounting(task
, buffer
, 1);
2439 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2444 static const struct file_operations proc_task_operations
;
2445 static const struct inode_operations proc_task_inode_operations
;
2447 static const struct pid_entry tgid_base_stuff
[] = {
2448 DIR("task", S_IRUGO
|S_IXUGO
, task
),
2449 DIR("fd", S_IRUSR
|S_IXUSR
, fd
),
2450 DIR("fdinfo", S_IRUSR
|S_IXUSR
, fdinfo
),
2452 DIR("net", S_IRUGO
|S_IXUGO
, net
),
2454 REG("environ", S_IRUSR
, environ
),
2455 INF("auxv", S_IRUSR
, pid_auxv
),
2456 ONE("status", S_IRUGO
, pid_status
),
2457 INF("limits", S_IRUSR
, pid_limits
),
2458 #ifdef CONFIG_SCHED_DEBUG
2459 REG("sched", S_IRUGO
|S_IWUSR
, pid_sched
),
2461 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2462 INF("syscall", S_IRUSR
, pid_syscall
),
2464 INF("cmdline", S_IRUGO
, pid_cmdline
),
2465 ONE("stat", S_IRUGO
, tgid_stat
),
2466 ONE("statm", S_IRUGO
, pid_statm
),
2467 REG("maps", S_IRUGO
, maps
),
2469 REG("numa_maps", S_IRUGO
, numa_maps
),
2471 REG("mem", S_IRUSR
|S_IWUSR
, mem
),
2475 REG("mounts", S_IRUGO
, mounts
),
2476 REG("mountinfo", S_IRUGO
, mountinfo
),
2477 REG("mountstats", S_IRUSR
, mountstats
),
2478 #ifdef CONFIG_PROC_PAGE_MONITOR
2479 REG("clear_refs", S_IWUSR
, clear_refs
),
2480 REG("smaps", S_IRUGO
, smaps
),
2481 REG("pagemap", S_IRUSR
, pagemap
),
2483 #ifdef CONFIG_SECURITY
2484 DIR("attr", S_IRUGO
|S_IXUGO
, attr_dir
),
2486 #ifdef CONFIG_KALLSYMS
2487 INF("wchan", S_IRUGO
, pid_wchan
),
2489 #ifdef CONFIG_SCHEDSTATS
2490 INF("schedstat", S_IRUGO
, pid_schedstat
),
2492 #ifdef CONFIG_LATENCYTOP
2493 REG("latency", S_IRUGO
, lstats
),
2495 #ifdef CONFIG_PROC_PID_CPUSET
2496 REG("cpuset", S_IRUGO
, cpuset
),
2498 #ifdef CONFIG_CGROUPS
2499 REG("cgroup", S_IRUGO
, cgroup
),
2501 INF("oom_score", S_IRUGO
, oom_score
),
2502 REG("oom_adj", S_IRUGO
|S_IWUSR
, oom_adjust
),
2503 #ifdef CONFIG_AUDITSYSCALL
2504 REG("loginuid", S_IWUSR
|S_IRUGO
, loginuid
),
2505 REG("sessionid", S_IRUGO
, sessionid
),
2507 #ifdef CONFIG_FAULT_INJECTION
2508 REG("make-it-fail", S_IRUGO
|S_IWUSR
, fault_inject
),
2510 #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2511 REG("coredump_filter", S_IRUGO
|S_IWUSR
, coredump_filter
),
2513 #ifdef CONFIG_TASK_IO_ACCOUNTING
2514 INF("io", S_IRUGO
, tgid_io_accounting
),
2518 static int proc_tgid_base_readdir(struct file
* filp
,
2519 void * dirent
, filldir_t filldir
)
2521 return proc_pident_readdir(filp
,dirent
,filldir
,
2522 tgid_base_stuff
,ARRAY_SIZE(tgid_base_stuff
));
2525 static const struct file_operations proc_tgid_base_operations
= {
2526 .read
= generic_read_dir
,
2527 .readdir
= proc_tgid_base_readdir
,
2530 static struct dentry
*proc_tgid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
){
2531 return proc_pident_lookup(dir
, dentry
,
2532 tgid_base_stuff
, ARRAY_SIZE(tgid_base_stuff
));
2535 static const struct inode_operations proc_tgid_base_inode_operations
= {
2536 .lookup
= proc_tgid_base_lookup
,
2537 .getattr
= pid_getattr
,
2538 .setattr
= proc_setattr
,
2541 static void proc_flush_task_mnt(struct vfsmount
*mnt
, pid_t pid
, pid_t tgid
)
2543 struct dentry
*dentry
, *leader
, *dir
;
2544 char buf
[PROC_NUMBUF
];
2548 name
.len
= snprintf(buf
, sizeof(buf
), "%d", pid
);
2549 dentry
= d_hash_and_lookup(mnt
->mnt_root
, &name
);
2551 if (!(current
->flags
& PF_EXITING
))
2552 shrink_dcache_parent(dentry
);
2561 name
.len
= snprintf(buf
, sizeof(buf
), "%d", tgid
);
2562 leader
= d_hash_and_lookup(mnt
->mnt_root
, &name
);
2567 name
.len
= strlen(name
.name
);
2568 dir
= d_hash_and_lookup(leader
, &name
);
2570 goto out_put_leader
;
2573 name
.len
= snprintf(buf
, sizeof(buf
), "%d", pid
);
2574 dentry
= d_hash_and_lookup(dir
, &name
);
2576 shrink_dcache_parent(dentry
);
2589 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2590 * @task: task that should be flushed.
2592 * When flushing dentries from proc, one needs to flush them from global
2593 * proc (proc_mnt) and from all the namespaces' procs this task was seen
2594 * in. This call is supposed to do all of this job.
2596 * Looks in the dcache for
2598 * /proc/@tgid/task/@pid
2599 * if either directory is present flushes it and all of it'ts children
2602 * It is safe and reasonable to cache /proc entries for a task until
2603 * that task exits. After that they just clog up the dcache with
2604 * useless entries, possibly causing useful dcache entries to be
2605 * flushed instead. This routine is proved to flush those useless
2606 * dcache entries at process exit time.
2608 * NOTE: This routine is just an optimization so it does not guarantee
2609 * that no dcache entries will exist at process exit time it
2610 * just makes it very unlikely that any will persist.
2613 void proc_flush_task(struct task_struct
*task
)
2616 struct pid
*pid
, *tgid
= NULL
;
2619 pid
= task_pid(task
);
2620 if (thread_group_leader(task
))
2621 tgid
= task_tgid(task
);
2623 for (i
= 0; i
<= pid
->level
; i
++) {
2624 upid
= &pid
->numbers
[i
];
2625 proc_flush_task_mnt(upid
->ns
->proc_mnt
, upid
->nr
,
2626 tgid
? tgid
->numbers
[i
].nr
: 0);
2629 upid
= &pid
->numbers
[pid
->level
];
2631 pid_ns_release_proc(upid
->ns
);
2634 static struct dentry
*proc_pid_instantiate(struct inode
*dir
,
2635 struct dentry
* dentry
,
2636 struct task_struct
*task
, const void *ptr
)
2638 struct dentry
*error
= ERR_PTR(-ENOENT
);
2639 struct inode
*inode
;
2641 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
2645 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
2646 inode
->i_op
= &proc_tgid_base_inode_operations
;
2647 inode
->i_fop
= &proc_tgid_base_operations
;
2648 inode
->i_flags
|=S_IMMUTABLE
;
2650 inode
->i_nlink
= 2 + pid_entry_count_dirs(tgid_base_stuff
,
2651 ARRAY_SIZE(tgid_base_stuff
));
2653 dentry
->d_op
= &pid_dentry_operations
;
2655 d_add(dentry
, inode
);
2656 /* Close the race of the process dying before we return the dentry */
2657 if (pid_revalidate(dentry
, NULL
))
2663 struct dentry
*proc_pid_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
)
2665 struct dentry
*result
= ERR_PTR(-ENOENT
);
2666 struct task_struct
*task
;
2668 struct pid_namespace
*ns
;
2670 result
= proc_base_lookup(dir
, dentry
);
2671 if (!IS_ERR(result
) || PTR_ERR(result
) != -ENOENT
)
2674 tgid
= name_to_int(dentry
);
2678 ns
= dentry
->d_sb
->s_fs_info
;
2680 task
= find_task_by_pid_ns(tgid
, ns
);
2682 get_task_struct(task
);
2687 result
= proc_pid_instantiate(dir
, dentry
, task
, NULL
);
2688 put_task_struct(task
);
2694 * Find the first task with tgid >= tgid
2699 struct task_struct
*task
;
2701 static struct tgid_iter
next_tgid(struct pid_namespace
*ns
, struct tgid_iter iter
)
2706 put_task_struct(iter
.task
);
2710 pid
= find_ge_pid(iter
.tgid
, ns
);
2712 iter
.tgid
= pid_nr_ns(pid
, ns
);
2713 iter
.task
= pid_task(pid
, PIDTYPE_PID
);
2714 /* What we to know is if the pid we have find is the
2715 * pid of a thread_group_leader. Testing for task
2716 * being a thread_group_leader is the obvious thing
2717 * todo but there is a window when it fails, due to
2718 * the pid transfer logic in de_thread.
2720 * So we perform the straight forward test of seeing
2721 * if the pid we have found is the pid of a thread
2722 * group leader, and don't worry if the task we have
2723 * found doesn't happen to be a thread group leader.
2724 * As we don't care in the case of readdir.
2726 if (!iter
.task
|| !has_group_leader_pid(iter
.task
)) {
2730 get_task_struct(iter
.task
);
2736 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
2738 static int proc_pid_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
2739 struct tgid_iter iter
)
2741 char name
[PROC_NUMBUF
];
2742 int len
= snprintf(name
, sizeof(name
), "%d", iter
.tgid
);
2743 return proc_fill_cache(filp
, dirent
, filldir
, name
, len
,
2744 proc_pid_instantiate
, iter
.task
, NULL
);
2747 /* for the /proc/ directory itself, after non-process stuff has been done */
2748 int proc_pid_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
2750 unsigned int nr
= filp
->f_pos
- FIRST_PROCESS_ENTRY
;
2751 struct task_struct
*reaper
= get_proc_task(filp
->f_path
.dentry
->d_inode
);
2752 struct tgid_iter iter
;
2753 struct pid_namespace
*ns
;
2758 for (; nr
< ARRAY_SIZE(proc_base_stuff
); filp
->f_pos
++, nr
++) {
2759 const struct pid_entry
*p
= &proc_base_stuff
[nr
];
2760 if (proc_base_fill_cache(filp
, dirent
, filldir
, reaper
, p
) < 0)
2764 ns
= filp
->f_dentry
->d_sb
->s_fs_info
;
2766 iter
.tgid
= filp
->f_pos
- TGID_OFFSET
;
2767 for (iter
= next_tgid(ns
, iter
);
2769 iter
.tgid
+= 1, iter
= next_tgid(ns
, iter
)) {
2770 filp
->f_pos
= iter
.tgid
+ TGID_OFFSET
;
2771 if (proc_pid_fill_cache(filp
, dirent
, filldir
, iter
) < 0) {
2772 put_task_struct(iter
.task
);
2776 filp
->f_pos
= PID_MAX_LIMIT
+ TGID_OFFSET
;
2778 put_task_struct(reaper
);
2786 static const struct pid_entry tid_base_stuff
[] = {
2787 DIR("fd", S_IRUSR
|S_IXUSR
, fd
),
2788 DIR("fdinfo", S_IRUSR
|S_IXUSR
, fdinfo
),
2789 REG("environ", S_IRUSR
, environ
),
2790 INF("auxv", S_IRUSR
, pid_auxv
),
2791 ONE("status", S_IRUGO
, pid_status
),
2792 INF("limits", S_IRUSR
, pid_limits
),
2793 #ifdef CONFIG_SCHED_DEBUG
2794 REG("sched", S_IRUGO
|S_IWUSR
, pid_sched
),
2796 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2797 INF("syscall", S_IRUSR
, pid_syscall
),
2799 INF("cmdline", S_IRUGO
, pid_cmdline
),
2800 ONE("stat", S_IRUGO
, tid_stat
),
2801 ONE("statm", S_IRUGO
, pid_statm
),
2802 REG("maps", S_IRUGO
, maps
),
2804 REG("numa_maps", S_IRUGO
, numa_maps
),
2806 REG("mem", S_IRUSR
|S_IWUSR
, mem
),
2810 REG("mounts", S_IRUGO
, mounts
),
2811 REG("mountinfo", S_IRUGO
, mountinfo
),
2812 #ifdef CONFIG_PROC_PAGE_MONITOR
2813 REG("clear_refs", S_IWUSR
, clear_refs
),
2814 REG("smaps", S_IRUGO
, smaps
),
2815 REG("pagemap", S_IRUSR
, pagemap
),
2817 #ifdef CONFIG_SECURITY
2818 DIR("attr", S_IRUGO
|S_IXUGO
, attr_dir
),
2820 #ifdef CONFIG_KALLSYMS
2821 INF("wchan", S_IRUGO
, pid_wchan
),
2823 #ifdef CONFIG_SCHEDSTATS
2824 INF("schedstat", S_IRUGO
, pid_schedstat
),
2826 #ifdef CONFIG_LATENCYTOP
2827 REG("latency", S_IRUGO
, lstats
),
2829 #ifdef CONFIG_PROC_PID_CPUSET
2830 REG("cpuset", S_IRUGO
, cpuset
),
2832 #ifdef CONFIG_CGROUPS
2833 REG("cgroup", S_IRUGO
, cgroup
),
2835 INF("oom_score", S_IRUGO
, oom_score
),
2836 REG("oom_adj", S_IRUGO
|S_IWUSR
, oom_adjust
),
2837 #ifdef CONFIG_AUDITSYSCALL
2838 REG("loginuid", S_IWUSR
|S_IRUGO
, loginuid
),
2839 REG("sessionid", S_IRUSR
, sessionid
),
2841 #ifdef CONFIG_FAULT_INJECTION
2842 REG("make-it-fail", S_IRUGO
|S_IWUSR
, fault_inject
),
2844 #ifdef CONFIG_TASK_IO_ACCOUNTING
2845 INF("io", S_IRUGO
, tid_io_accounting
),
2849 static int proc_tid_base_readdir(struct file
* filp
,
2850 void * dirent
, filldir_t filldir
)
2852 return proc_pident_readdir(filp
,dirent
,filldir
,
2853 tid_base_stuff
,ARRAY_SIZE(tid_base_stuff
));
2856 static struct dentry
*proc_tid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
){
2857 return proc_pident_lookup(dir
, dentry
,
2858 tid_base_stuff
, ARRAY_SIZE(tid_base_stuff
));
2861 static const struct file_operations proc_tid_base_operations
= {
2862 .read
= generic_read_dir
,
2863 .readdir
= proc_tid_base_readdir
,
2866 static const struct inode_operations proc_tid_base_inode_operations
= {
2867 .lookup
= proc_tid_base_lookup
,
2868 .getattr
= pid_getattr
,
2869 .setattr
= proc_setattr
,
2872 static struct dentry
*proc_task_instantiate(struct inode
*dir
,
2873 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
2875 struct dentry
*error
= ERR_PTR(-ENOENT
);
2876 struct inode
*inode
;
2877 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
2881 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
2882 inode
->i_op
= &proc_tid_base_inode_operations
;
2883 inode
->i_fop
= &proc_tid_base_operations
;
2884 inode
->i_flags
|=S_IMMUTABLE
;
2886 inode
->i_nlink
= 2 + pid_entry_count_dirs(tid_base_stuff
,
2887 ARRAY_SIZE(tid_base_stuff
));
2889 dentry
->d_op
= &pid_dentry_operations
;
2891 d_add(dentry
, inode
);
2892 /* Close the race of the process dying before we return the dentry */
2893 if (pid_revalidate(dentry
, NULL
))
2899 static struct dentry
*proc_task_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
)
2901 struct dentry
*result
= ERR_PTR(-ENOENT
);
2902 struct task_struct
*task
;
2903 struct task_struct
*leader
= get_proc_task(dir
);
2905 struct pid_namespace
*ns
;
2910 tid
= name_to_int(dentry
);
2914 ns
= dentry
->d_sb
->s_fs_info
;
2916 task
= find_task_by_pid_ns(tid
, ns
);
2918 get_task_struct(task
);
2922 if (!same_thread_group(leader
, task
))
2925 result
= proc_task_instantiate(dir
, dentry
, task
, NULL
);
2927 put_task_struct(task
);
2929 put_task_struct(leader
);
2935 * Find the first tid of a thread group to return to user space.
2937 * Usually this is just the thread group leader, but if the users
2938 * buffer was too small or there was a seek into the middle of the
2939 * directory we have more work todo.
2941 * In the case of a short read we start with find_task_by_pid.
2943 * In the case of a seek we start with the leader and walk nr
2946 static struct task_struct
*first_tid(struct task_struct
*leader
,
2947 int tid
, int nr
, struct pid_namespace
*ns
)
2949 struct task_struct
*pos
;
2952 /* Attempt to start with the pid of a thread */
2953 if (tid
&& (nr
> 0)) {
2954 pos
= find_task_by_pid_ns(tid
, ns
);
2955 if (pos
&& (pos
->group_leader
== leader
))
2959 /* If nr exceeds the number of threads there is nothing todo */
2961 if (nr
&& nr
>= get_nr_threads(leader
))
2964 /* If we haven't found our starting place yet start
2965 * with the leader and walk nr threads forward.
2967 for (pos
= leader
; nr
> 0; --nr
) {
2968 pos
= next_thread(pos
);
2969 if (pos
== leader
) {
2975 get_task_struct(pos
);
2982 * Find the next thread in the thread list.
2983 * Return NULL if there is an error or no next thread.
2985 * The reference to the input task_struct is released.
2987 static struct task_struct
*next_tid(struct task_struct
*start
)
2989 struct task_struct
*pos
= NULL
;
2991 if (pid_alive(start
)) {
2992 pos
= next_thread(start
);
2993 if (thread_group_leader(pos
))
2996 get_task_struct(pos
);
2999 put_task_struct(start
);
3003 static int proc_task_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
3004 struct task_struct
*task
, int tid
)
3006 char name
[PROC_NUMBUF
];
3007 int len
= snprintf(name
, sizeof(name
), "%d", tid
);
3008 return proc_fill_cache(filp
, dirent
, filldir
, name
, len
,
3009 proc_task_instantiate
, task
, NULL
);
3012 /* for the /proc/TGID/task/ directories */
3013 static int proc_task_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
3015 struct dentry
*dentry
= filp
->f_path
.dentry
;
3016 struct inode
*inode
= dentry
->d_inode
;
3017 struct task_struct
*leader
= NULL
;
3018 struct task_struct
*task
;
3019 int retval
= -ENOENT
;
3022 unsigned long pos
= filp
->f_pos
; /* avoiding "long long" filp->f_pos */
3023 struct pid_namespace
*ns
;
3025 task
= get_proc_task(inode
);
3029 if (pid_alive(task
)) {
3030 leader
= task
->group_leader
;
3031 get_task_struct(leader
);
3034 put_task_struct(task
);
3042 if (filldir(dirent
, ".", 1, pos
, ino
, DT_DIR
) < 0)
3047 ino
= parent_ino(dentry
);
3048 if (filldir(dirent
, "..", 2, pos
, ino
, DT_DIR
) < 0)
3054 /* f_version caches the tgid value that the last readdir call couldn't
3055 * return. lseek aka telldir automagically resets f_version to 0.
3057 ns
= filp
->f_dentry
->d_sb
->s_fs_info
;
3058 tid
= (int)filp
->f_version
;
3059 filp
->f_version
= 0;
3060 for (task
= first_tid(leader
, tid
, pos
- 2, ns
);
3062 task
= next_tid(task
), pos
++) {
3063 tid
= task_pid_nr_ns(task
, ns
);
3064 if (proc_task_fill_cache(filp
, dirent
, filldir
, task
, tid
) < 0) {
3065 /* returning this tgid failed, save it as the first
3066 * pid for the next readir call */
3067 filp
->f_version
= (u64
)tid
;
3068 put_task_struct(task
);
3074 put_task_struct(leader
);
3079 static int proc_task_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
3081 struct inode
*inode
= dentry
->d_inode
;
3082 struct task_struct
*p
= get_proc_task(inode
);
3083 generic_fillattr(inode
, stat
);
3086 stat
->nlink
+= get_nr_threads(p
);
3093 static const struct inode_operations proc_task_inode_operations
= {
3094 .lookup
= proc_task_lookup
,
3095 .getattr
= proc_task_getattr
,
3096 .setattr
= proc_setattr
,
3099 static const struct file_operations proc_task_operations
= {
3100 .read
= generic_read_dir
,
3101 .readdir
= proc_task_readdir
,