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/init.h>
57 #include <linux/capability.h>
58 #include <linux/file.h>
59 #include <linux/fdtable.h>
60 #include <linux/string.h>
61 #include <linux/seq_file.h>
62 #include <linux/namei.h>
63 #include <linux/mnt_namespace.h>
65 #include <linux/rcupdate.h>
66 #include <linux/kallsyms.h>
67 #include <linux/resource.h>
68 #include <linux/module.h>
69 #include <linux/mount.h>
70 #include <linux/security.h>
71 #include <linux/ptrace.h>
72 #include <linux/tracehook.h>
73 #include <linux/cgroup.h>
74 #include <linux/cpuset.h>
75 #include <linux/audit.h>
76 #include <linux/poll.h>
77 #include <linux/nsproxy.h>
78 #include <linux/oom.h>
79 #include <linux/elf.h>
80 #include <linux/pid_namespace.h>
84 * Implementing inode permission operations in /proc is almost
85 * certainly an error. Permission checks need to happen during
86 * each system call not at open time. The reason is that most of
87 * what we wish to check for permissions in /proc varies at runtime.
89 * The classic example of a problem is opening file descriptors
90 * in /proc for a task before it execs a suid executable.
97 const struct inode_operations
*iop
;
98 const struct file_operations
*fop
;
102 #define NOD(NAME, MODE, IOP, FOP, OP) { \
104 .len = sizeof(NAME) - 1, \
111 #define DIR(NAME, MODE, OTYPE) \
112 NOD(NAME, (S_IFDIR|(MODE)), \
113 &proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations, \
115 #define LNK(NAME, OTYPE) \
116 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
117 &proc_pid_link_inode_operations, NULL, \
118 { .proc_get_link = &proc_##OTYPE##_link } )
119 #define REG(NAME, MODE, OTYPE) \
120 NOD(NAME, (S_IFREG|(MODE)), NULL, \
121 &proc_##OTYPE##_operations, {})
122 #define INF(NAME, MODE, OTYPE) \
123 NOD(NAME, (S_IFREG|(MODE)), \
124 NULL, &proc_info_file_operations, \
125 { .proc_read = &proc_##OTYPE } )
126 #define ONE(NAME, MODE, OTYPE) \
127 NOD(NAME, (S_IFREG|(MODE)), \
128 NULL, &proc_single_file_operations, \
129 { .proc_show = &proc_##OTYPE } )
132 * Count the number of hardlinks for the pid_entry table, excluding the .
135 static unsigned int pid_entry_count_dirs(const struct pid_entry
*entries
,
142 for (i
= 0; i
< n
; ++i
) {
143 if (S_ISDIR(entries
[i
].mode
))
151 EXPORT_SYMBOL(maps_protect
);
153 static struct fs_struct
*get_fs_struct(struct task_struct
*task
)
155 struct fs_struct
*fs
;
159 atomic_inc(&fs
->count
);
164 static int get_nr_threads(struct task_struct
*tsk
)
166 /* Must be called with the rcu_read_lock held */
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
];
474 if (!lock_task_sighand(task
,&flags
)) {
478 memcpy(rlim
, task
->signal
->rlim
, sizeof(struct rlimit
) * RLIM_NLIMITS
);
479 unlock_task_sighand(task
, &flags
);
483 * print the file header
485 count
+= sprintf(&bufptr
[count
], "%-25s %-20s %-20s %-10s\n",
486 "Limit", "Soft Limit", "Hard Limit", "Units");
488 for (i
= 0; i
< RLIM_NLIMITS
; i
++) {
489 if (rlim
[i
].rlim_cur
== RLIM_INFINITY
)
490 count
+= sprintf(&bufptr
[count
], "%-25s %-20s ",
491 lnames
[i
].name
, "unlimited");
493 count
+= sprintf(&bufptr
[count
], "%-25s %-20lu ",
494 lnames
[i
].name
, rlim
[i
].rlim_cur
);
496 if (rlim
[i
].rlim_max
== RLIM_INFINITY
)
497 count
+= sprintf(&bufptr
[count
], "%-20s ", "unlimited");
499 count
+= sprintf(&bufptr
[count
], "%-20lu ",
503 count
+= sprintf(&bufptr
[count
], "%-10s\n",
506 count
+= sprintf(&bufptr
[count
], "\n");
512 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
513 static int proc_pid_syscall(struct task_struct
*task
, char *buffer
)
516 unsigned long args
[6], sp
, pc
;
518 if (task_current_syscall(task
, &nr
, args
, 6, &sp
, &pc
))
519 return sprintf(buffer
, "running\n");
522 return sprintf(buffer
, "%ld 0x%lx 0x%lx\n", nr
, sp
, pc
);
524 return sprintf(buffer
,
525 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
527 args
[0], args
[1], args
[2], args
[3], args
[4], args
[5],
530 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
532 /************************************************************************/
533 /* Here the fs part begins */
534 /************************************************************************/
536 /* permission checks */
537 static int proc_fd_access_allowed(struct inode
*inode
)
539 struct task_struct
*task
;
541 /* Allow access to a task's file descriptors if it is us or we
542 * may use ptrace attach to the process and find out that
545 task
= get_proc_task(inode
);
547 allowed
= ptrace_may_access(task
, PTRACE_MODE_READ
);
548 put_task_struct(task
);
553 static int proc_setattr(struct dentry
*dentry
, struct iattr
*attr
)
556 struct inode
*inode
= dentry
->d_inode
;
558 if (attr
->ia_valid
& ATTR_MODE
)
561 error
= inode_change_ok(inode
, attr
);
563 error
= inode_setattr(inode
, attr
);
567 static const struct inode_operations proc_def_inode_operations
= {
568 .setattr
= proc_setattr
,
571 static int mounts_open_common(struct inode
*inode
, struct file
*file
,
572 const struct seq_operations
*op
)
574 struct task_struct
*task
= get_proc_task(inode
);
576 struct mnt_namespace
*ns
= NULL
;
577 struct fs_struct
*fs
= NULL
;
579 struct proc_mounts
*p
;
584 nsp
= task_nsproxy(task
);
592 fs
= get_fs_struct(task
);
593 put_task_struct(task
);
601 read_lock(&fs
->lock
);
604 read_unlock(&fs
->lock
);
608 p
= kmalloc(sizeof(struct proc_mounts
), GFP_KERNEL
);
612 file
->private_data
= &p
->m
;
613 ret
= seq_open(file
, op
);
620 p
->event
= ns
->event
;
634 static int mounts_release(struct inode
*inode
, struct file
*file
)
636 struct proc_mounts
*p
= file
->private_data
;
639 return seq_release(inode
, file
);
642 static unsigned mounts_poll(struct file
*file
, poll_table
*wait
)
644 struct proc_mounts
*p
= file
->private_data
;
645 struct mnt_namespace
*ns
= p
->ns
;
648 poll_wait(file
, &ns
->poll
, wait
);
650 spin_lock(&vfsmount_lock
);
651 if (p
->event
!= ns
->event
) {
652 p
->event
= ns
->event
;
655 spin_unlock(&vfsmount_lock
);
660 static int mounts_open(struct inode
*inode
, struct file
*file
)
662 return mounts_open_common(inode
, file
, &mounts_op
);
665 static const struct file_operations proc_mounts_operations
= {
669 .release
= mounts_release
,
673 static int mountinfo_open(struct inode
*inode
, struct file
*file
)
675 return mounts_open_common(inode
, file
, &mountinfo_op
);
678 static const struct file_operations proc_mountinfo_operations
= {
679 .open
= mountinfo_open
,
682 .release
= mounts_release
,
686 static int mountstats_open(struct inode
*inode
, struct file
*file
)
688 return mounts_open_common(inode
, file
, &mountstats_op
);
691 static const struct file_operations proc_mountstats_operations
= {
692 .open
= mountstats_open
,
695 .release
= mounts_release
,
698 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
700 static ssize_t
proc_info_read(struct file
* file
, char __user
* buf
,
701 size_t count
, loff_t
*ppos
)
703 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
706 struct task_struct
*task
= get_proc_task(inode
);
712 if (count
> PROC_BLOCK_SIZE
)
713 count
= PROC_BLOCK_SIZE
;
716 if (!(page
= __get_free_page(GFP_TEMPORARY
)))
719 length
= PROC_I(inode
)->op
.proc_read(task
, (char*)page
);
722 length
= simple_read_from_buffer(buf
, count
, ppos
, (char *)page
, length
);
725 put_task_struct(task
);
730 static const struct file_operations proc_info_file_operations
= {
731 .read
= proc_info_read
,
734 static int proc_single_show(struct seq_file
*m
, void *v
)
736 struct inode
*inode
= m
->private;
737 struct pid_namespace
*ns
;
739 struct task_struct
*task
;
742 ns
= inode
->i_sb
->s_fs_info
;
743 pid
= proc_pid(inode
);
744 task
= get_pid_task(pid
, PIDTYPE_PID
);
748 ret
= PROC_I(inode
)->op
.proc_show(m
, ns
, pid
, task
);
750 put_task_struct(task
);
754 static int proc_single_open(struct inode
*inode
, struct file
*filp
)
757 ret
= single_open(filp
, proc_single_show
, NULL
);
759 struct seq_file
*m
= filp
->private_data
;
766 static const struct file_operations proc_single_file_operations
= {
767 .open
= proc_single_open
,
770 .release
= single_release
,
773 static int mem_open(struct inode
* inode
, struct file
* file
)
775 file
->private_data
= (void*)((long)current
->self_exec_id
);
779 static ssize_t
mem_read(struct file
* file
, char __user
* buf
,
780 size_t count
, loff_t
*ppos
)
782 struct task_struct
*task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
784 unsigned long src
= *ppos
;
786 struct mm_struct
*mm
;
791 if (check_mem_permission(task
))
795 page
= (char *)__get_free_page(GFP_TEMPORARY
);
801 mm
= get_task_mm(task
);
807 if (file
->private_data
!= (void*)((long)current
->self_exec_id
))
813 int this_len
, retval
;
815 this_len
= (count
> PAGE_SIZE
) ? PAGE_SIZE
: count
;
816 retval
= access_process_vm(task
, src
, page
, this_len
, 0);
817 if (!retval
|| check_mem_permission(task
)) {
823 if (copy_to_user(buf
, page
, retval
)) {
838 free_page((unsigned long) page
);
840 put_task_struct(task
);
845 #define mem_write NULL
848 /* This is a security hazard */
849 static ssize_t
mem_write(struct file
* file
, const char __user
*buf
,
850 size_t count
, loff_t
*ppos
)
854 struct task_struct
*task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
855 unsigned long dst
= *ppos
;
861 if (check_mem_permission(task
))
865 page
= (char *)__get_free_page(GFP_TEMPORARY
);
871 int this_len
, retval
;
873 this_len
= (count
> PAGE_SIZE
) ? PAGE_SIZE
: count
;
874 if (copy_from_user(page
, buf
, this_len
)) {
878 retval
= access_process_vm(task
, dst
, page
, this_len
, 1);
890 free_page((unsigned long) page
);
892 put_task_struct(task
);
898 loff_t
mem_lseek(struct file
*file
, loff_t offset
, int orig
)
902 file
->f_pos
= offset
;
905 file
->f_pos
+= offset
;
910 force_successful_syscall_return();
914 static const struct file_operations proc_mem_operations
= {
921 static ssize_t
environ_read(struct file
*file
, char __user
*buf
,
922 size_t count
, loff_t
*ppos
)
924 struct task_struct
*task
= get_proc_task(file
->f_dentry
->d_inode
);
926 unsigned long src
= *ppos
;
928 struct mm_struct
*mm
;
933 if (!ptrace_may_access(task
, PTRACE_MODE_READ
))
937 page
= (char *)__get_free_page(GFP_TEMPORARY
);
943 mm
= get_task_mm(task
);
948 int this_len
, retval
, max_len
;
950 this_len
= mm
->env_end
- (mm
->env_start
+ src
);
955 max_len
= (count
> PAGE_SIZE
) ? PAGE_SIZE
: count
;
956 this_len
= (this_len
> max_len
) ? max_len
: this_len
;
958 retval
= access_process_vm(task
, (mm
->env_start
+ src
),
966 if (copy_to_user(buf
, page
, retval
)) {
980 free_page((unsigned long) page
);
982 put_task_struct(task
);
987 static const struct file_operations proc_environ_operations
= {
988 .read
= environ_read
,
991 static ssize_t
oom_adjust_read(struct file
*file
, char __user
*buf
,
992 size_t count
, loff_t
*ppos
)
994 struct task_struct
*task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
995 char buffer
[PROC_NUMBUF
];
1001 oom_adjust
= task
->oomkilladj
;
1002 put_task_struct(task
);
1004 len
= snprintf(buffer
, sizeof(buffer
), "%i\n", oom_adjust
);
1006 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
1009 static ssize_t
oom_adjust_write(struct file
*file
, const char __user
*buf
,
1010 size_t count
, loff_t
*ppos
)
1012 struct task_struct
*task
;
1013 char buffer
[PROC_NUMBUF
], *end
;
1016 memset(buffer
, 0, sizeof(buffer
));
1017 if (count
> sizeof(buffer
) - 1)
1018 count
= sizeof(buffer
) - 1;
1019 if (copy_from_user(buffer
, buf
, count
))
1021 oom_adjust
= simple_strtol(buffer
, &end
, 0);
1022 if ((oom_adjust
< OOM_ADJUST_MIN
|| oom_adjust
> OOM_ADJUST_MAX
) &&
1023 oom_adjust
!= OOM_DISABLE
)
1027 task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
1030 if (oom_adjust
< task
->oomkilladj
&& !capable(CAP_SYS_RESOURCE
)) {
1031 put_task_struct(task
);
1034 task
->oomkilladj
= oom_adjust
;
1035 put_task_struct(task
);
1036 if (end
- buffer
== 0)
1038 return end
- buffer
;
1041 static const struct file_operations proc_oom_adjust_operations
= {
1042 .read
= oom_adjust_read
,
1043 .write
= oom_adjust_write
,
1046 #ifdef CONFIG_AUDITSYSCALL
1047 #define TMPBUFLEN 21
1048 static ssize_t
proc_loginuid_read(struct file
* file
, char __user
* buf
,
1049 size_t count
, loff_t
*ppos
)
1051 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
1052 struct task_struct
*task
= get_proc_task(inode
);
1054 char tmpbuf
[TMPBUFLEN
];
1058 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
1059 audit_get_loginuid(task
));
1060 put_task_struct(task
);
1061 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1064 static ssize_t
proc_loginuid_write(struct file
* file
, const char __user
* buf
,
1065 size_t count
, loff_t
*ppos
)
1067 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
1072 if (!capable(CAP_AUDIT_CONTROL
))
1075 if (current
!= pid_task(proc_pid(inode
), PIDTYPE_PID
))
1078 if (count
>= PAGE_SIZE
)
1079 count
= PAGE_SIZE
- 1;
1082 /* No partial writes. */
1085 page
= (char*)__get_free_page(GFP_TEMPORARY
);
1089 if (copy_from_user(page
, buf
, count
))
1093 loginuid
= simple_strtoul(page
, &tmp
, 10);
1099 length
= audit_set_loginuid(current
, loginuid
);
1100 if (likely(length
== 0))
1104 free_page((unsigned long) page
);
1108 static const struct file_operations proc_loginuid_operations
= {
1109 .read
= proc_loginuid_read
,
1110 .write
= proc_loginuid_write
,
1113 static ssize_t
proc_sessionid_read(struct file
* file
, char __user
* buf
,
1114 size_t count
, loff_t
*ppos
)
1116 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
1117 struct task_struct
*task
= get_proc_task(inode
);
1119 char tmpbuf
[TMPBUFLEN
];
1123 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
1124 audit_get_sessionid(task
));
1125 put_task_struct(task
);
1126 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1129 static const struct file_operations proc_sessionid_operations
= {
1130 .read
= proc_sessionid_read
,
1134 #ifdef CONFIG_FAULT_INJECTION
1135 static ssize_t
proc_fault_inject_read(struct file
* file
, char __user
* buf
,
1136 size_t count
, loff_t
*ppos
)
1138 struct task_struct
*task
= get_proc_task(file
->f_dentry
->d_inode
);
1139 char buffer
[PROC_NUMBUF
];
1145 make_it_fail
= task
->make_it_fail
;
1146 put_task_struct(task
);
1148 len
= snprintf(buffer
, sizeof(buffer
), "%i\n", make_it_fail
);
1150 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
1153 static ssize_t
proc_fault_inject_write(struct file
* file
,
1154 const char __user
* buf
, size_t count
, loff_t
*ppos
)
1156 struct task_struct
*task
;
1157 char buffer
[PROC_NUMBUF
], *end
;
1160 if (!capable(CAP_SYS_RESOURCE
))
1162 memset(buffer
, 0, sizeof(buffer
));
1163 if (count
> sizeof(buffer
) - 1)
1164 count
= sizeof(buffer
) - 1;
1165 if (copy_from_user(buffer
, buf
, count
))
1167 make_it_fail
= simple_strtol(buffer
, &end
, 0);
1170 task
= get_proc_task(file
->f_dentry
->d_inode
);
1173 task
->make_it_fail
= make_it_fail
;
1174 put_task_struct(task
);
1175 if (end
- buffer
== 0)
1177 return end
- buffer
;
1180 static const struct file_operations proc_fault_inject_operations
= {
1181 .read
= proc_fault_inject_read
,
1182 .write
= proc_fault_inject_write
,
1187 #ifdef CONFIG_SCHED_DEBUG
1189 * Print out various scheduling related per-task fields:
1191 static int sched_show(struct seq_file
*m
, void *v
)
1193 struct inode
*inode
= m
->private;
1194 struct task_struct
*p
;
1198 p
= get_proc_task(inode
);
1201 proc_sched_show_task(p
, m
);
1209 sched_write(struct file
*file
, const char __user
*buf
,
1210 size_t count
, loff_t
*offset
)
1212 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1213 struct task_struct
*p
;
1217 p
= get_proc_task(inode
);
1220 proc_sched_set_task(p
);
1227 static int sched_open(struct inode
*inode
, struct file
*filp
)
1231 ret
= single_open(filp
, sched_show
, NULL
);
1233 struct seq_file
*m
= filp
->private_data
;
1240 static const struct file_operations proc_pid_sched_operations
= {
1243 .write
= sched_write
,
1244 .llseek
= seq_lseek
,
1245 .release
= single_release
,
1251 * We added or removed a vma mapping the executable. The vmas are only mapped
1252 * during exec and are not mapped with the mmap system call.
1253 * Callers must hold down_write() on the mm's mmap_sem for these
1255 void added_exe_file_vma(struct mm_struct
*mm
)
1257 mm
->num_exe_file_vmas
++;
1260 void removed_exe_file_vma(struct mm_struct
*mm
)
1262 mm
->num_exe_file_vmas
--;
1263 if ((mm
->num_exe_file_vmas
== 0) && mm
->exe_file
){
1265 mm
->exe_file
= NULL
;
1270 void set_mm_exe_file(struct mm_struct
*mm
, struct file
*new_exe_file
)
1273 get_file(new_exe_file
);
1276 mm
->exe_file
= new_exe_file
;
1277 mm
->num_exe_file_vmas
= 0;
1280 struct file
*get_mm_exe_file(struct mm_struct
*mm
)
1282 struct file
*exe_file
;
1284 /* We need mmap_sem to protect against races with removal of
1285 * VM_EXECUTABLE vmas */
1286 down_read(&mm
->mmap_sem
);
1287 exe_file
= mm
->exe_file
;
1290 up_read(&mm
->mmap_sem
);
1294 void dup_mm_exe_file(struct mm_struct
*oldmm
, struct mm_struct
*newmm
)
1296 /* It's safe to write the exe_file pointer without exe_file_lock because
1297 * this is called during fork when the task is not yet in /proc */
1298 newmm
->exe_file
= get_mm_exe_file(oldmm
);
1301 static int proc_exe_link(struct inode
*inode
, struct path
*exe_path
)
1303 struct task_struct
*task
;
1304 struct mm_struct
*mm
;
1305 struct file
*exe_file
;
1307 task
= get_proc_task(inode
);
1310 mm
= get_task_mm(task
);
1311 put_task_struct(task
);
1314 exe_file
= get_mm_exe_file(mm
);
1317 *exe_path
= exe_file
->f_path
;
1318 path_get(&exe_file
->f_path
);
1325 static void *proc_pid_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
1327 struct inode
*inode
= dentry
->d_inode
;
1328 int error
= -EACCES
;
1330 /* We don't need a base pointer in the /proc filesystem */
1331 path_put(&nd
->path
);
1333 /* Are we allowed to snoop on the tasks file descriptors? */
1334 if (!proc_fd_access_allowed(inode
))
1337 error
= PROC_I(inode
)->op
.proc_get_link(inode
, &nd
->path
);
1338 nd
->last_type
= LAST_BIND
;
1340 return ERR_PTR(error
);
1343 static int do_proc_readlink(struct path
*path
, char __user
*buffer
, int buflen
)
1345 char *tmp
= (char*)__get_free_page(GFP_TEMPORARY
);
1352 pathname
= d_path(path
, tmp
, PAGE_SIZE
);
1353 len
= PTR_ERR(pathname
);
1354 if (IS_ERR(pathname
))
1356 len
= tmp
+ PAGE_SIZE
- 1 - pathname
;
1360 if (copy_to_user(buffer
, pathname
, len
))
1363 free_page((unsigned long)tmp
);
1367 static int proc_pid_readlink(struct dentry
* dentry
, char __user
* buffer
, int buflen
)
1369 int error
= -EACCES
;
1370 struct inode
*inode
= dentry
->d_inode
;
1373 /* Are we allowed to snoop on the tasks file descriptors? */
1374 if (!proc_fd_access_allowed(inode
))
1377 error
= PROC_I(inode
)->op
.proc_get_link(inode
, &path
);
1381 error
= do_proc_readlink(&path
, buffer
, buflen
);
1387 static const struct inode_operations proc_pid_link_inode_operations
= {
1388 .readlink
= proc_pid_readlink
,
1389 .follow_link
= proc_pid_follow_link
,
1390 .setattr
= proc_setattr
,
1394 /* building an inode */
1396 static int task_dumpable(struct task_struct
*task
)
1399 struct mm_struct
*mm
;
1404 dumpable
= get_dumpable(mm
);
1412 static struct inode
*proc_pid_make_inode(struct super_block
* sb
, struct task_struct
*task
)
1414 struct inode
* inode
;
1415 struct proc_inode
*ei
;
1417 /* We need a new inode */
1419 inode
= new_inode(sb
);
1425 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1426 inode
->i_op
= &proc_def_inode_operations
;
1429 * grab the reference to task.
1431 ei
->pid
= get_task_pid(task
, PIDTYPE_PID
);
1437 if (task_dumpable(task
)) {
1438 inode
->i_uid
= task
->euid
;
1439 inode
->i_gid
= task
->egid
;
1441 security_task_to_inode(task
, inode
);
1451 static int pid_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
1453 struct inode
*inode
= dentry
->d_inode
;
1454 struct task_struct
*task
;
1455 generic_fillattr(inode
, stat
);
1460 task
= pid_task(proc_pid(inode
), PIDTYPE_PID
);
1462 if ((inode
->i_mode
== (S_IFDIR
|S_IRUGO
|S_IXUGO
)) ||
1463 task_dumpable(task
)) {
1464 stat
->uid
= task
->euid
;
1465 stat
->gid
= task
->egid
;
1475 * Exceptional case: normally we are not allowed to unhash a busy
1476 * directory. In this case, however, we can do it - no aliasing problems
1477 * due to the way we treat inodes.
1479 * Rewrite the inode's ownerships here because the owning task may have
1480 * performed a setuid(), etc.
1482 * Before the /proc/pid/status file was created the only way to read
1483 * the effective uid of a /process was to stat /proc/pid. Reading
1484 * /proc/pid/status is slow enough that procps and other packages
1485 * kept stating /proc/pid. To keep the rules in /proc simple I have
1486 * made this apply to all per process world readable and executable
1489 static int pid_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1491 struct inode
*inode
= dentry
->d_inode
;
1492 struct task_struct
*task
= get_proc_task(inode
);
1494 if ((inode
->i_mode
== (S_IFDIR
|S_IRUGO
|S_IXUGO
)) ||
1495 task_dumpable(task
)) {
1496 inode
->i_uid
= task
->euid
;
1497 inode
->i_gid
= task
->egid
;
1502 inode
->i_mode
&= ~(S_ISUID
| S_ISGID
);
1503 security_task_to_inode(task
, inode
);
1504 put_task_struct(task
);
1511 static int pid_delete_dentry(struct dentry
* dentry
)
1513 /* Is the task we represent dead?
1514 * If so, then don't put the dentry on the lru list,
1515 * kill it immediately.
1517 return !proc_pid(dentry
->d_inode
)->tasks
[PIDTYPE_PID
].first
;
1520 static struct dentry_operations pid_dentry_operations
=
1522 .d_revalidate
= pid_revalidate
,
1523 .d_delete
= pid_delete_dentry
,
1528 typedef struct dentry
*instantiate_t(struct inode
*, struct dentry
*,
1529 struct task_struct
*, const void *);
1532 * Fill a directory entry.
1534 * If possible create the dcache entry and derive our inode number and
1535 * file type from dcache entry.
1537 * Since all of the proc inode numbers are dynamically generated, the inode
1538 * numbers do not exist until the inode is cache. This means creating the
1539 * the dcache entry in readdir is necessary to keep the inode numbers
1540 * reported by readdir in sync with the inode numbers reported
1543 static int proc_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
1544 char *name
, int len
,
1545 instantiate_t instantiate
, struct task_struct
*task
, const void *ptr
)
1547 struct dentry
*child
, *dir
= filp
->f_path
.dentry
;
1548 struct inode
*inode
;
1551 unsigned type
= DT_UNKNOWN
;
1555 qname
.hash
= full_name_hash(name
, len
);
1557 child
= d_lookup(dir
, &qname
);
1560 new = d_alloc(dir
, &qname
);
1562 child
= instantiate(dir
->d_inode
, new, task
, ptr
);
1569 if (!child
|| IS_ERR(child
) || !child
->d_inode
)
1570 goto end_instantiate
;
1571 inode
= child
->d_inode
;
1574 type
= inode
->i_mode
>> 12;
1579 ino
= find_inode_number(dir
, &qname
);
1582 return filldir(dirent
, name
, len
, filp
->f_pos
, ino
, type
);
1585 static unsigned name_to_int(struct dentry
*dentry
)
1587 const char *name
= dentry
->d_name
.name
;
1588 int len
= dentry
->d_name
.len
;
1591 if (len
> 1 && *name
== '0')
1594 unsigned c
= *name
++ - '0';
1597 if (n
>= (~0U-9)/10)
1607 #define PROC_FDINFO_MAX 64
1609 static int proc_fd_info(struct inode
*inode
, struct path
*path
, char *info
)
1611 struct task_struct
*task
= get_proc_task(inode
);
1612 struct files_struct
*files
= NULL
;
1614 int fd
= proc_fd(inode
);
1617 files
= get_files_struct(task
);
1618 put_task_struct(task
);
1622 * We are not taking a ref to the file structure, so we must
1625 spin_lock(&files
->file_lock
);
1626 file
= fcheck_files(files
, fd
);
1629 *path
= file
->f_path
;
1630 path_get(&file
->f_path
);
1633 snprintf(info
, PROC_FDINFO_MAX
,
1636 (long long) file
->f_pos
,
1638 spin_unlock(&files
->file_lock
);
1639 put_files_struct(files
);
1642 spin_unlock(&files
->file_lock
);
1643 put_files_struct(files
);
1648 static int proc_fd_link(struct inode
*inode
, struct path
*path
)
1650 return proc_fd_info(inode
, path
, NULL
);
1653 static int tid_fd_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1655 struct inode
*inode
= dentry
->d_inode
;
1656 struct task_struct
*task
= get_proc_task(inode
);
1657 int fd
= proc_fd(inode
);
1658 struct files_struct
*files
;
1661 files
= get_files_struct(task
);
1664 if (fcheck_files(files
, fd
)) {
1666 put_files_struct(files
);
1667 if (task_dumpable(task
)) {
1668 inode
->i_uid
= task
->euid
;
1669 inode
->i_gid
= task
->egid
;
1674 inode
->i_mode
&= ~(S_ISUID
| S_ISGID
);
1675 security_task_to_inode(task
, inode
);
1676 put_task_struct(task
);
1680 put_files_struct(files
);
1682 put_task_struct(task
);
1688 static struct dentry_operations tid_fd_dentry_operations
=
1690 .d_revalidate
= tid_fd_revalidate
,
1691 .d_delete
= pid_delete_dentry
,
1694 static struct dentry
*proc_fd_instantiate(struct inode
*dir
,
1695 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
1697 unsigned fd
= *(const unsigned *)ptr
;
1699 struct files_struct
*files
;
1700 struct inode
*inode
;
1701 struct proc_inode
*ei
;
1702 struct dentry
*error
= ERR_PTR(-ENOENT
);
1704 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
1709 files
= get_files_struct(task
);
1712 inode
->i_mode
= S_IFLNK
;
1715 * We are not taking a ref to the file structure, so we must
1718 spin_lock(&files
->file_lock
);
1719 file
= fcheck_files(files
, fd
);
1722 if (file
->f_mode
& 1)
1723 inode
->i_mode
|= S_IRUSR
| S_IXUSR
;
1724 if (file
->f_mode
& 2)
1725 inode
->i_mode
|= S_IWUSR
| S_IXUSR
;
1726 spin_unlock(&files
->file_lock
);
1727 put_files_struct(files
);
1729 inode
->i_op
= &proc_pid_link_inode_operations
;
1731 ei
->op
.proc_get_link
= proc_fd_link
;
1732 dentry
->d_op
= &tid_fd_dentry_operations
;
1733 d_add(dentry
, inode
);
1734 /* Close the race of the process dying before we return the dentry */
1735 if (tid_fd_revalidate(dentry
, NULL
))
1741 spin_unlock(&files
->file_lock
);
1742 put_files_struct(files
);
1748 static struct dentry
*proc_lookupfd_common(struct inode
*dir
,
1749 struct dentry
*dentry
,
1750 instantiate_t instantiate
)
1752 struct task_struct
*task
= get_proc_task(dir
);
1753 unsigned fd
= name_to_int(dentry
);
1754 struct dentry
*result
= ERR_PTR(-ENOENT
);
1761 result
= instantiate(dir
, dentry
, task
, &fd
);
1763 put_task_struct(task
);
1768 static int proc_readfd_common(struct file
* filp
, void * dirent
,
1769 filldir_t filldir
, instantiate_t instantiate
)
1771 struct dentry
*dentry
= filp
->f_path
.dentry
;
1772 struct inode
*inode
= dentry
->d_inode
;
1773 struct task_struct
*p
= get_proc_task(inode
);
1774 unsigned int fd
, ino
;
1776 struct files_struct
* files
;
1786 if (filldir(dirent
, ".", 1, 0, inode
->i_ino
, DT_DIR
) < 0)
1790 ino
= parent_ino(dentry
);
1791 if (filldir(dirent
, "..", 2, 1, ino
, DT_DIR
) < 0)
1795 files
= get_files_struct(p
);
1799 for (fd
= filp
->f_pos
-2;
1800 fd
< files_fdtable(files
)->max_fds
;
1801 fd
++, filp
->f_pos
++) {
1802 char name
[PROC_NUMBUF
];
1805 if (!fcheck_files(files
, fd
))
1809 len
= snprintf(name
, sizeof(name
), "%d", fd
);
1810 if (proc_fill_cache(filp
, dirent
, filldir
,
1811 name
, len
, instantiate
,
1819 put_files_struct(files
);
1827 static struct dentry
*proc_lookupfd(struct inode
*dir
, struct dentry
*dentry
,
1828 struct nameidata
*nd
)
1830 return proc_lookupfd_common(dir
, dentry
, proc_fd_instantiate
);
1833 static int proc_readfd(struct file
*filp
, void *dirent
, filldir_t filldir
)
1835 return proc_readfd_common(filp
, dirent
, filldir
, proc_fd_instantiate
);
1838 static ssize_t
proc_fdinfo_read(struct file
*file
, char __user
*buf
,
1839 size_t len
, loff_t
*ppos
)
1841 char tmp
[PROC_FDINFO_MAX
];
1842 int err
= proc_fd_info(file
->f_path
.dentry
->d_inode
, NULL
, tmp
);
1844 err
= simple_read_from_buffer(buf
, len
, ppos
, tmp
, strlen(tmp
));
1848 static const struct file_operations proc_fdinfo_file_operations
= {
1849 .open
= nonseekable_open
,
1850 .read
= proc_fdinfo_read
,
1853 static const struct file_operations proc_fd_operations
= {
1854 .read
= generic_read_dir
,
1855 .readdir
= proc_readfd
,
1859 * /proc/pid/fd needs a special permission handler so that a process can still
1860 * access /proc/self/fd after it has executed a setuid().
1862 static int proc_fd_permission(struct inode
*inode
, int mask
,
1863 struct nameidata
*nd
)
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 u64 rchar
, wchar
, syscr
, syscw
;
2407 struct task_io_accounting ioac
;
2410 rchar
= task
->rchar
;
2411 wchar
= task
->wchar
;
2412 syscr
= task
->syscr
;
2413 syscw
= task
->syscw
;
2414 memcpy(&ioac
, &task
->ioac
, sizeof(ioac
));
2416 unsigned long flags
;
2417 struct task_struct
*t
= task
;
2418 rchar
= wchar
= syscr
= syscw
= 0;
2419 memset(&ioac
, 0, sizeof(ioac
));
2428 ioac
.read_bytes
+= t
->ioac
.read_bytes
;
2429 ioac
.write_bytes
+= t
->ioac
.write_bytes
;
2430 ioac
.cancelled_write_bytes
+=
2431 t
->ioac
.cancelled_write_bytes
;
2433 } while (t
!= task
);
2436 if (lock_task_sighand(task
, &flags
)) {
2437 struct signal_struct
*sig
= task
->signal
;
2439 rchar
+= sig
->rchar
;
2440 wchar
+= sig
->wchar
;
2441 syscr
+= sig
->syscr
;
2442 syscw
+= sig
->syscw
;
2444 ioac
.read_bytes
+= sig
->ioac
.read_bytes
;
2445 ioac
.write_bytes
+= sig
->ioac
.write_bytes
;
2446 ioac
.cancelled_write_bytes
+=
2447 sig
->ioac
.cancelled_write_bytes
;
2449 unlock_task_sighand(task
, &flags
);
2453 return sprintf(buffer
,
2458 "read_bytes: %llu\n"
2459 "write_bytes: %llu\n"
2460 "cancelled_write_bytes: %llu\n",
2461 (unsigned long long)rchar
,
2462 (unsigned long long)wchar
,
2463 (unsigned long long)syscr
,
2464 (unsigned long long)syscw
,
2465 (unsigned long long)ioac
.read_bytes
,
2466 (unsigned long long)ioac
.write_bytes
,
2467 (unsigned long long)ioac
.cancelled_write_bytes
);
2470 static int proc_tid_io_accounting(struct task_struct
*task
, char *buffer
)
2472 return do_io_accounting(task
, buffer
, 0);
2475 static int proc_tgid_io_accounting(struct task_struct
*task
, char *buffer
)
2477 return do_io_accounting(task
, buffer
, 1);
2479 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2484 static const struct file_operations proc_task_operations
;
2485 static const struct inode_operations proc_task_inode_operations
;
2487 static const struct pid_entry tgid_base_stuff
[] = {
2488 DIR("task", S_IRUGO
|S_IXUGO
, task
),
2489 DIR("fd", S_IRUSR
|S_IXUSR
, fd
),
2490 DIR("fdinfo", S_IRUSR
|S_IXUSR
, fdinfo
),
2492 DIR("net", S_IRUGO
|S_IXUGO
, net
),
2494 REG("environ", S_IRUSR
, environ
),
2495 INF("auxv", S_IRUSR
, pid_auxv
),
2496 ONE("status", S_IRUGO
, pid_status
),
2497 INF("limits", S_IRUSR
, pid_limits
),
2498 #ifdef CONFIG_SCHED_DEBUG
2499 REG("sched", S_IRUGO
|S_IWUSR
, pid_sched
),
2501 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2502 INF("syscall", S_IRUSR
, pid_syscall
),
2504 INF("cmdline", S_IRUGO
, pid_cmdline
),
2505 ONE("stat", S_IRUGO
, tgid_stat
),
2506 ONE("statm", S_IRUGO
, pid_statm
),
2507 REG("maps", S_IRUGO
, maps
),
2509 REG("numa_maps", S_IRUGO
, numa_maps
),
2511 REG("mem", S_IRUSR
|S_IWUSR
, mem
),
2515 REG("mounts", S_IRUGO
, mounts
),
2516 REG("mountinfo", S_IRUGO
, mountinfo
),
2517 REG("mountstats", S_IRUSR
, mountstats
),
2518 #ifdef CONFIG_PROC_PAGE_MONITOR
2519 REG("clear_refs", S_IWUSR
, clear_refs
),
2520 REG("smaps", S_IRUGO
, smaps
),
2521 REG("pagemap", S_IRUSR
, pagemap
),
2523 #ifdef CONFIG_SECURITY
2524 DIR("attr", S_IRUGO
|S_IXUGO
, attr_dir
),
2526 #ifdef CONFIG_KALLSYMS
2527 INF("wchan", S_IRUGO
, pid_wchan
),
2529 #ifdef CONFIG_SCHEDSTATS
2530 INF("schedstat", S_IRUGO
, pid_schedstat
),
2532 #ifdef CONFIG_LATENCYTOP
2533 REG("latency", S_IRUGO
, lstats
),
2535 #ifdef CONFIG_PROC_PID_CPUSET
2536 REG("cpuset", S_IRUGO
, cpuset
),
2538 #ifdef CONFIG_CGROUPS
2539 REG("cgroup", S_IRUGO
, cgroup
),
2541 INF("oom_score", S_IRUGO
, oom_score
),
2542 REG("oom_adj", S_IRUGO
|S_IWUSR
, oom_adjust
),
2543 #ifdef CONFIG_AUDITSYSCALL
2544 REG("loginuid", S_IWUSR
|S_IRUGO
, loginuid
),
2545 REG("sessionid", S_IRUGO
, sessionid
),
2547 #ifdef CONFIG_FAULT_INJECTION
2548 REG("make-it-fail", S_IRUGO
|S_IWUSR
, fault_inject
),
2550 #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2551 REG("coredump_filter", S_IRUGO
|S_IWUSR
, coredump_filter
),
2553 #ifdef CONFIG_TASK_IO_ACCOUNTING
2554 INF("io", S_IRUGO
, tgid_io_accounting
),
2558 static int proc_tgid_base_readdir(struct file
* filp
,
2559 void * dirent
, filldir_t filldir
)
2561 return proc_pident_readdir(filp
,dirent
,filldir
,
2562 tgid_base_stuff
,ARRAY_SIZE(tgid_base_stuff
));
2565 static const struct file_operations proc_tgid_base_operations
= {
2566 .read
= generic_read_dir
,
2567 .readdir
= proc_tgid_base_readdir
,
2570 static struct dentry
*proc_tgid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
){
2571 return proc_pident_lookup(dir
, dentry
,
2572 tgid_base_stuff
, ARRAY_SIZE(tgid_base_stuff
));
2575 static const struct inode_operations proc_tgid_base_inode_operations
= {
2576 .lookup
= proc_tgid_base_lookup
,
2577 .getattr
= pid_getattr
,
2578 .setattr
= proc_setattr
,
2581 static void proc_flush_task_mnt(struct vfsmount
*mnt
, pid_t pid
, pid_t tgid
)
2583 struct dentry
*dentry
, *leader
, *dir
;
2584 char buf
[PROC_NUMBUF
];
2588 name
.len
= snprintf(buf
, sizeof(buf
), "%d", pid
);
2589 dentry
= d_hash_and_lookup(mnt
->mnt_root
, &name
);
2591 if (!(current
->flags
& PF_EXITING
))
2592 shrink_dcache_parent(dentry
);
2601 name
.len
= snprintf(buf
, sizeof(buf
), "%d", tgid
);
2602 leader
= d_hash_and_lookup(mnt
->mnt_root
, &name
);
2607 name
.len
= strlen(name
.name
);
2608 dir
= d_hash_and_lookup(leader
, &name
);
2610 goto out_put_leader
;
2613 name
.len
= snprintf(buf
, sizeof(buf
), "%d", pid
);
2614 dentry
= d_hash_and_lookup(dir
, &name
);
2616 shrink_dcache_parent(dentry
);
2629 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2630 * @task: task that should be flushed.
2632 * When flushing dentries from proc, one needs to flush them from global
2633 * proc (proc_mnt) and from all the namespaces' procs this task was seen
2634 * in. This call is supposed to do all of this job.
2636 * Looks in the dcache for
2638 * /proc/@tgid/task/@pid
2639 * if either directory is present flushes it and all of it'ts children
2642 * It is safe and reasonable to cache /proc entries for a task until
2643 * that task exits. After that they just clog up the dcache with
2644 * useless entries, possibly causing useful dcache entries to be
2645 * flushed instead. This routine is proved to flush those useless
2646 * dcache entries at process exit time.
2648 * NOTE: This routine is just an optimization so it does not guarantee
2649 * that no dcache entries will exist at process exit time it
2650 * just makes it very unlikely that any will persist.
2653 void proc_flush_task(struct task_struct
*task
)
2656 struct pid
*pid
, *tgid
= NULL
;
2659 pid
= task_pid(task
);
2660 if (thread_group_leader(task
))
2661 tgid
= task_tgid(task
);
2663 for (i
= 0; i
<= pid
->level
; i
++) {
2664 upid
= &pid
->numbers
[i
];
2665 proc_flush_task_mnt(upid
->ns
->proc_mnt
, upid
->nr
,
2666 tgid
? tgid
->numbers
[i
].nr
: 0);
2669 upid
= &pid
->numbers
[pid
->level
];
2671 pid_ns_release_proc(upid
->ns
);
2674 static struct dentry
*proc_pid_instantiate(struct inode
*dir
,
2675 struct dentry
* dentry
,
2676 struct task_struct
*task
, const void *ptr
)
2678 struct dentry
*error
= ERR_PTR(-ENOENT
);
2679 struct inode
*inode
;
2681 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
2685 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
2686 inode
->i_op
= &proc_tgid_base_inode_operations
;
2687 inode
->i_fop
= &proc_tgid_base_operations
;
2688 inode
->i_flags
|=S_IMMUTABLE
;
2690 inode
->i_nlink
= 2 + pid_entry_count_dirs(tgid_base_stuff
,
2691 ARRAY_SIZE(tgid_base_stuff
));
2693 dentry
->d_op
= &pid_dentry_operations
;
2695 d_add(dentry
, inode
);
2696 /* Close the race of the process dying before we return the dentry */
2697 if (pid_revalidate(dentry
, NULL
))
2703 struct dentry
*proc_pid_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
)
2705 struct dentry
*result
= ERR_PTR(-ENOENT
);
2706 struct task_struct
*task
;
2708 struct pid_namespace
*ns
;
2710 result
= proc_base_lookup(dir
, dentry
);
2711 if (!IS_ERR(result
) || PTR_ERR(result
) != -ENOENT
)
2714 tgid
= name_to_int(dentry
);
2718 ns
= dentry
->d_sb
->s_fs_info
;
2720 task
= find_task_by_pid_ns(tgid
, ns
);
2722 get_task_struct(task
);
2727 result
= proc_pid_instantiate(dir
, dentry
, task
, NULL
);
2728 put_task_struct(task
);
2734 * Find the first task with tgid >= tgid
2739 struct task_struct
*task
;
2741 static struct tgid_iter
next_tgid(struct pid_namespace
*ns
, struct tgid_iter iter
)
2746 put_task_struct(iter
.task
);
2750 pid
= find_ge_pid(iter
.tgid
, ns
);
2752 iter
.tgid
= pid_nr_ns(pid
, ns
);
2753 iter
.task
= pid_task(pid
, PIDTYPE_PID
);
2754 /* What we to know is if the pid we have find is the
2755 * pid of a thread_group_leader. Testing for task
2756 * being a thread_group_leader is the obvious thing
2757 * todo but there is a window when it fails, due to
2758 * the pid transfer logic in de_thread.
2760 * So we perform the straight forward test of seeing
2761 * if the pid we have found is the pid of a thread
2762 * group leader, and don't worry if the task we have
2763 * found doesn't happen to be a thread group leader.
2764 * As we don't care in the case of readdir.
2766 if (!iter
.task
|| !has_group_leader_pid(iter
.task
)) {
2770 get_task_struct(iter
.task
);
2776 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
2778 static int proc_pid_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
2779 struct tgid_iter iter
)
2781 char name
[PROC_NUMBUF
];
2782 int len
= snprintf(name
, sizeof(name
), "%d", iter
.tgid
);
2783 return proc_fill_cache(filp
, dirent
, filldir
, name
, len
,
2784 proc_pid_instantiate
, iter
.task
, NULL
);
2787 /* for the /proc/ directory itself, after non-process stuff has been done */
2788 int proc_pid_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
2790 unsigned int nr
= filp
->f_pos
- FIRST_PROCESS_ENTRY
;
2791 struct task_struct
*reaper
= get_proc_task(filp
->f_path
.dentry
->d_inode
);
2792 struct tgid_iter iter
;
2793 struct pid_namespace
*ns
;
2798 for (; nr
< ARRAY_SIZE(proc_base_stuff
); filp
->f_pos
++, nr
++) {
2799 const struct pid_entry
*p
= &proc_base_stuff
[nr
];
2800 if (proc_base_fill_cache(filp
, dirent
, filldir
, reaper
, p
) < 0)
2804 ns
= filp
->f_dentry
->d_sb
->s_fs_info
;
2806 iter
.tgid
= filp
->f_pos
- TGID_OFFSET
;
2807 for (iter
= next_tgid(ns
, iter
);
2809 iter
.tgid
+= 1, iter
= next_tgid(ns
, iter
)) {
2810 filp
->f_pos
= iter
.tgid
+ TGID_OFFSET
;
2811 if (proc_pid_fill_cache(filp
, dirent
, filldir
, iter
) < 0) {
2812 put_task_struct(iter
.task
);
2816 filp
->f_pos
= PID_MAX_LIMIT
+ TGID_OFFSET
;
2818 put_task_struct(reaper
);
2826 static const struct pid_entry tid_base_stuff
[] = {
2827 DIR("fd", S_IRUSR
|S_IXUSR
, fd
),
2828 DIR("fdinfo", S_IRUSR
|S_IXUSR
, fdinfo
),
2829 REG("environ", S_IRUSR
, environ
),
2830 INF("auxv", S_IRUSR
, pid_auxv
),
2831 ONE("status", S_IRUGO
, pid_status
),
2832 INF("limits", S_IRUSR
, pid_limits
),
2833 #ifdef CONFIG_SCHED_DEBUG
2834 REG("sched", S_IRUGO
|S_IWUSR
, pid_sched
),
2836 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2837 INF("syscall", S_IRUSR
, pid_syscall
),
2839 INF("cmdline", S_IRUGO
, pid_cmdline
),
2840 ONE("stat", S_IRUGO
, tid_stat
),
2841 ONE("statm", S_IRUGO
, pid_statm
),
2842 REG("maps", S_IRUGO
, maps
),
2844 REG("numa_maps", S_IRUGO
, numa_maps
),
2846 REG("mem", S_IRUSR
|S_IWUSR
, mem
),
2850 REG("mounts", S_IRUGO
, mounts
),
2851 REG("mountinfo", S_IRUGO
, mountinfo
),
2852 #ifdef CONFIG_PROC_PAGE_MONITOR
2853 REG("clear_refs", S_IWUSR
, clear_refs
),
2854 REG("smaps", S_IRUGO
, smaps
),
2855 REG("pagemap", S_IRUSR
, pagemap
),
2857 #ifdef CONFIG_SECURITY
2858 DIR("attr", S_IRUGO
|S_IXUGO
, attr_dir
),
2860 #ifdef CONFIG_KALLSYMS
2861 INF("wchan", S_IRUGO
, pid_wchan
),
2863 #ifdef CONFIG_SCHEDSTATS
2864 INF("schedstat", S_IRUGO
, pid_schedstat
),
2866 #ifdef CONFIG_LATENCYTOP
2867 REG("latency", S_IRUGO
, lstats
),
2869 #ifdef CONFIG_PROC_PID_CPUSET
2870 REG("cpuset", S_IRUGO
, cpuset
),
2872 #ifdef CONFIG_CGROUPS
2873 REG("cgroup", S_IRUGO
, cgroup
),
2875 INF("oom_score", S_IRUGO
, oom_score
),
2876 REG("oom_adj", S_IRUGO
|S_IWUSR
, oom_adjust
),
2877 #ifdef CONFIG_AUDITSYSCALL
2878 REG("loginuid", S_IWUSR
|S_IRUGO
, loginuid
),
2879 REG("sessionid", S_IRUSR
, sessionid
),
2881 #ifdef CONFIG_FAULT_INJECTION
2882 REG("make-it-fail", S_IRUGO
|S_IWUSR
, fault_inject
),
2884 #ifdef CONFIG_TASK_IO_ACCOUNTING
2885 INF("io", S_IRUGO
, tid_io_accounting
),
2889 static int proc_tid_base_readdir(struct file
* filp
,
2890 void * dirent
, filldir_t filldir
)
2892 return proc_pident_readdir(filp
,dirent
,filldir
,
2893 tid_base_stuff
,ARRAY_SIZE(tid_base_stuff
));
2896 static struct dentry
*proc_tid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
){
2897 return proc_pident_lookup(dir
, dentry
,
2898 tid_base_stuff
, ARRAY_SIZE(tid_base_stuff
));
2901 static const struct file_operations proc_tid_base_operations
= {
2902 .read
= generic_read_dir
,
2903 .readdir
= proc_tid_base_readdir
,
2906 static const struct inode_operations proc_tid_base_inode_operations
= {
2907 .lookup
= proc_tid_base_lookup
,
2908 .getattr
= pid_getattr
,
2909 .setattr
= proc_setattr
,
2912 static struct dentry
*proc_task_instantiate(struct inode
*dir
,
2913 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
2915 struct dentry
*error
= ERR_PTR(-ENOENT
);
2916 struct inode
*inode
;
2917 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
2921 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
2922 inode
->i_op
= &proc_tid_base_inode_operations
;
2923 inode
->i_fop
= &proc_tid_base_operations
;
2924 inode
->i_flags
|=S_IMMUTABLE
;
2926 inode
->i_nlink
= 2 + pid_entry_count_dirs(tid_base_stuff
,
2927 ARRAY_SIZE(tid_base_stuff
));
2929 dentry
->d_op
= &pid_dentry_operations
;
2931 d_add(dentry
, inode
);
2932 /* Close the race of the process dying before we return the dentry */
2933 if (pid_revalidate(dentry
, NULL
))
2939 static struct dentry
*proc_task_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
)
2941 struct dentry
*result
= ERR_PTR(-ENOENT
);
2942 struct task_struct
*task
;
2943 struct task_struct
*leader
= get_proc_task(dir
);
2945 struct pid_namespace
*ns
;
2950 tid
= name_to_int(dentry
);
2954 ns
= dentry
->d_sb
->s_fs_info
;
2956 task
= find_task_by_pid_ns(tid
, ns
);
2958 get_task_struct(task
);
2962 if (!same_thread_group(leader
, task
))
2965 result
= proc_task_instantiate(dir
, dentry
, task
, NULL
);
2967 put_task_struct(task
);
2969 put_task_struct(leader
);
2975 * Find the first tid of a thread group to return to user space.
2977 * Usually this is just the thread group leader, but if the users
2978 * buffer was too small or there was a seek into the middle of the
2979 * directory we have more work todo.
2981 * In the case of a short read we start with find_task_by_pid.
2983 * In the case of a seek we start with the leader and walk nr
2986 static struct task_struct
*first_tid(struct task_struct
*leader
,
2987 int tid
, int nr
, struct pid_namespace
*ns
)
2989 struct task_struct
*pos
;
2992 /* Attempt to start with the pid of a thread */
2993 if (tid
&& (nr
> 0)) {
2994 pos
= find_task_by_pid_ns(tid
, ns
);
2995 if (pos
&& (pos
->group_leader
== leader
))
2999 /* If nr exceeds the number of threads there is nothing todo */
3001 if (nr
&& nr
>= get_nr_threads(leader
))
3004 /* If we haven't found our starting place yet start
3005 * with the leader and walk nr threads forward.
3007 for (pos
= leader
; nr
> 0; --nr
) {
3008 pos
= next_thread(pos
);
3009 if (pos
== leader
) {
3015 get_task_struct(pos
);
3022 * Find the next thread in the thread list.
3023 * Return NULL if there is an error or no next thread.
3025 * The reference to the input task_struct is released.
3027 static struct task_struct
*next_tid(struct task_struct
*start
)
3029 struct task_struct
*pos
= NULL
;
3031 if (pid_alive(start
)) {
3032 pos
= next_thread(start
);
3033 if (thread_group_leader(pos
))
3036 get_task_struct(pos
);
3039 put_task_struct(start
);
3043 static int proc_task_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
3044 struct task_struct
*task
, int tid
)
3046 char name
[PROC_NUMBUF
];
3047 int len
= snprintf(name
, sizeof(name
), "%d", tid
);
3048 return proc_fill_cache(filp
, dirent
, filldir
, name
, len
,
3049 proc_task_instantiate
, task
, NULL
);
3052 /* for the /proc/TGID/task/ directories */
3053 static int proc_task_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
3055 struct dentry
*dentry
= filp
->f_path
.dentry
;
3056 struct inode
*inode
= dentry
->d_inode
;
3057 struct task_struct
*leader
= NULL
;
3058 struct task_struct
*task
;
3059 int retval
= -ENOENT
;
3062 unsigned long pos
= filp
->f_pos
; /* avoiding "long long" filp->f_pos */
3063 struct pid_namespace
*ns
;
3065 task
= get_proc_task(inode
);
3069 if (pid_alive(task
)) {
3070 leader
= task
->group_leader
;
3071 get_task_struct(leader
);
3074 put_task_struct(task
);
3082 if (filldir(dirent
, ".", 1, pos
, ino
, DT_DIR
) < 0)
3087 ino
= parent_ino(dentry
);
3088 if (filldir(dirent
, "..", 2, pos
, ino
, DT_DIR
) < 0)
3094 /* f_version caches the tgid value that the last readdir call couldn't
3095 * return. lseek aka telldir automagically resets f_version to 0.
3097 ns
= filp
->f_dentry
->d_sb
->s_fs_info
;
3098 tid
= (int)filp
->f_version
;
3099 filp
->f_version
= 0;
3100 for (task
= first_tid(leader
, tid
, pos
- 2, ns
);
3102 task
= next_tid(task
), pos
++) {
3103 tid
= task_pid_nr_ns(task
, ns
);
3104 if (proc_task_fill_cache(filp
, dirent
, filldir
, task
, tid
) < 0) {
3105 /* returning this tgid failed, save it as the first
3106 * pid for the next readir call */
3107 filp
->f_version
= (u64
)tid
;
3108 put_task_struct(task
);
3114 put_task_struct(leader
);
3119 static int proc_task_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
3121 struct inode
*inode
= dentry
->d_inode
;
3122 struct task_struct
*p
= get_proc_task(inode
);
3123 generic_fillattr(inode
, stat
);
3127 stat
->nlink
+= get_nr_threads(p
);
3135 static const struct inode_operations proc_task_inode_operations
= {
3136 .lookup
= proc_task_lookup
,
3137 .getattr
= proc_task_getattr
,
3138 .setattr
= proc_setattr
,
3141 static const struct file_operations proc_task_operations
= {
3142 .read
= generic_read_dir
,
3143 .readdir
= proc_task_readdir
,