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/cgroup.h>
73 #include <linux/cpuset.h>
74 #include <linux/audit.h>
75 #include <linux/poll.h>
76 #include <linux/nsproxy.h>
77 #include <linux/oom.h>
78 #include <linux/elf.h>
79 #include <linux/pid_namespace.h>
83 * Implementing inode permission operations in /proc is almost
84 * certainly an error. Permission checks need to happen during
85 * each system call not at open time. The reason is that most of
86 * what we wish to check for permissions in /proc varies at runtime.
88 * The classic example of a problem is opening file descriptors
89 * in /proc for a task before it execs a suid executable.
96 const struct inode_operations
*iop
;
97 const struct file_operations
*fop
;
101 #define NOD(NAME, MODE, IOP, FOP, OP) { \
103 .len = sizeof(NAME) - 1, \
110 #define DIR(NAME, MODE, OTYPE) \
111 NOD(NAME, (S_IFDIR|(MODE)), \
112 &proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations, \
114 #define LNK(NAME, OTYPE) \
115 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
116 &proc_pid_link_inode_operations, NULL, \
117 { .proc_get_link = &proc_##OTYPE##_link } )
118 #define REG(NAME, MODE, OTYPE) \
119 NOD(NAME, (S_IFREG|(MODE)), NULL, \
120 &proc_##OTYPE##_operations, {})
121 #define INF(NAME, MODE, OTYPE) \
122 NOD(NAME, (S_IFREG|(MODE)), \
123 NULL, &proc_info_file_operations, \
124 { .proc_read = &proc_##OTYPE } )
125 #define ONE(NAME, MODE, OTYPE) \
126 NOD(NAME, (S_IFREG|(MODE)), \
127 NULL, &proc_single_file_operations, \
128 { .proc_show = &proc_##OTYPE } )
131 * Count the number of hardlinks for the pid_entry table, excluding the .
134 static unsigned int pid_entry_count_dirs(const struct pid_entry
*entries
,
141 for (i
= 0; i
< n
; ++i
) {
142 if (S_ISDIR(entries
[i
].mode
))
150 EXPORT_SYMBOL(maps_protect
);
152 static struct fs_struct
*get_fs_struct(struct task_struct
*task
)
154 struct fs_struct
*fs
;
158 atomic_inc(&fs
->count
);
163 static int get_nr_threads(struct task_struct
*tsk
)
165 /* Must be called with the rcu_read_lock held */
169 if (lock_task_sighand(tsk
, &flags
)) {
170 count
= atomic_read(&tsk
->signal
->count
);
171 unlock_task_sighand(tsk
, &flags
);
176 static int proc_cwd_link(struct inode
*inode
, struct path
*path
)
178 struct task_struct
*task
= get_proc_task(inode
);
179 struct fs_struct
*fs
= NULL
;
180 int result
= -ENOENT
;
183 fs
= get_fs_struct(task
);
184 put_task_struct(task
);
187 read_lock(&fs
->lock
);
190 read_unlock(&fs
->lock
);
197 static int proc_root_link(struct inode
*inode
, struct path
*path
)
199 struct task_struct
*task
= get_proc_task(inode
);
200 struct fs_struct
*fs
= NULL
;
201 int result
= -ENOENT
;
204 fs
= get_fs_struct(task
);
205 put_task_struct(task
);
208 read_lock(&fs
->lock
);
211 read_unlock(&fs
->lock
);
219 * Return zero if current may access user memory in @task, -error if not.
221 static int check_mem_permission(struct task_struct
*task
)
224 * A task can always look at itself, in case it chooses
225 * to use system calls instead of load instructions.
231 * If current is actively ptrace'ing, and would also be
232 * permitted to freshly attach with ptrace now, permit it.
234 if (task
->parent
== current
&& (task
->ptrace
& PT_PTRACED
) &&
235 task_is_stopped_or_traced(task
) &&
236 ptrace_may_access(task
, PTRACE_MODE_ATTACH
))
240 * Noone else is allowed.
245 struct mm_struct
*mm_for_maps(struct task_struct
*task
)
247 struct mm_struct
*mm
= get_task_mm(task
);
250 down_read(&mm
->mmap_sem
);
254 if (task
->mm
!= current
->mm
&&
255 __ptrace_may_access(task
, PTRACE_MODE_READ
) < 0)
261 up_read(&mm
->mmap_sem
);
266 static int proc_pid_cmdline(struct task_struct
*task
, char * buffer
)
270 struct mm_struct
*mm
= get_task_mm(task
);
274 goto out_mm
; /* Shh! No looking before we're done */
276 len
= mm
->arg_end
- mm
->arg_start
;
281 res
= access_process_vm(task
, mm
->arg_start
, buffer
, len
, 0);
283 // If the nul at the end of args has been overwritten, then
284 // assume application is using setproctitle(3).
285 if (res
> 0 && buffer
[res
-1] != '\0' && len
< PAGE_SIZE
) {
286 len
= strnlen(buffer
, res
);
290 len
= mm
->env_end
- mm
->env_start
;
291 if (len
> PAGE_SIZE
- res
)
292 len
= PAGE_SIZE
- res
;
293 res
+= access_process_vm(task
, mm
->env_start
, buffer
+res
, len
, 0);
294 res
= strnlen(buffer
, res
);
303 static int proc_pid_auxv(struct task_struct
*task
, char *buffer
)
306 struct mm_struct
*mm
= get_task_mm(task
);
308 unsigned int nwords
= 0;
311 while (mm
->saved_auxv
[nwords
- 2] != 0); /* AT_NULL */
312 res
= nwords
* sizeof(mm
->saved_auxv
[0]);
315 memcpy(buffer
, mm
->saved_auxv
, res
);
322 #ifdef CONFIG_KALLSYMS
324 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
325 * Returns the resolved symbol. If that fails, simply return the address.
327 static int proc_pid_wchan(struct task_struct
*task
, char *buffer
)
330 char symname
[KSYM_NAME_LEN
];
332 wchan
= get_wchan(task
);
334 if (lookup_symbol_name(wchan
, symname
) < 0)
335 return sprintf(buffer
, "%lu", wchan
);
337 return sprintf(buffer
, "%s", symname
);
339 #endif /* CONFIG_KALLSYMS */
341 #ifdef CONFIG_SCHEDSTATS
343 * Provides /proc/PID/schedstat
345 static int proc_pid_schedstat(struct task_struct
*task
, char *buffer
)
347 return sprintf(buffer
, "%llu %llu %lu\n",
348 task
->sched_info
.cpu_time
,
349 task
->sched_info
.run_delay
,
350 task
->sched_info
.pcount
);
354 #ifdef CONFIG_LATENCYTOP
355 static int lstats_show_proc(struct seq_file
*m
, void *v
)
358 struct inode
*inode
= m
->private;
359 struct task_struct
*task
= get_proc_task(inode
);
363 seq_puts(m
, "Latency Top version : v0.1\n");
364 for (i
= 0; i
< 32; i
++) {
365 if (task
->latency_record
[i
].backtrace
[0]) {
367 seq_printf(m
, "%i %li %li ",
368 task
->latency_record
[i
].count
,
369 task
->latency_record
[i
].time
,
370 task
->latency_record
[i
].max
);
371 for (q
= 0; q
< LT_BACKTRACEDEPTH
; q
++) {
372 char sym
[KSYM_NAME_LEN
];
374 if (!task
->latency_record
[i
].backtrace
[q
])
376 if (task
->latency_record
[i
].backtrace
[q
] == ULONG_MAX
)
378 sprint_symbol(sym
, task
->latency_record
[i
].backtrace
[q
]);
379 c
= strchr(sym
, '+');
382 seq_printf(m
, "%s ", sym
);
388 put_task_struct(task
);
392 static int lstats_open(struct inode
*inode
, struct file
*file
)
394 return single_open(file
, lstats_show_proc
, inode
);
397 static ssize_t
lstats_write(struct file
*file
, const char __user
*buf
,
398 size_t count
, loff_t
*offs
)
400 struct task_struct
*task
= get_proc_task(file
->f_dentry
->d_inode
);
404 clear_all_latency_tracing(task
);
405 put_task_struct(task
);
410 static const struct file_operations proc_lstats_operations
= {
413 .write
= lstats_write
,
415 .release
= single_release
,
420 /* The badness from the OOM killer */
421 unsigned long badness(struct task_struct
*p
, unsigned long uptime
);
422 static int proc_oom_score(struct task_struct
*task
, char *buffer
)
424 unsigned long points
;
425 struct timespec uptime
;
427 do_posix_clock_monotonic_gettime(&uptime
);
428 read_lock(&tasklist_lock
);
429 points
= badness(task
, uptime
.tv_sec
);
430 read_unlock(&tasklist_lock
);
431 return sprintf(buffer
, "%lu\n", points
);
439 static const struct limit_names lnames
[RLIM_NLIMITS
] = {
440 [RLIMIT_CPU
] = {"Max cpu time", "ms"},
441 [RLIMIT_FSIZE
] = {"Max file size", "bytes"},
442 [RLIMIT_DATA
] = {"Max data size", "bytes"},
443 [RLIMIT_STACK
] = {"Max stack size", "bytes"},
444 [RLIMIT_CORE
] = {"Max core file size", "bytes"},
445 [RLIMIT_RSS
] = {"Max resident set", "bytes"},
446 [RLIMIT_NPROC
] = {"Max processes", "processes"},
447 [RLIMIT_NOFILE
] = {"Max open files", "files"},
448 [RLIMIT_MEMLOCK
] = {"Max locked memory", "bytes"},
449 [RLIMIT_AS
] = {"Max address space", "bytes"},
450 [RLIMIT_LOCKS
] = {"Max file locks", "locks"},
451 [RLIMIT_SIGPENDING
] = {"Max pending signals", "signals"},
452 [RLIMIT_MSGQUEUE
] = {"Max msgqueue size", "bytes"},
453 [RLIMIT_NICE
] = {"Max nice priority", NULL
},
454 [RLIMIT_RTPRIO
] = {"Max realtime priority", NULL
},
455 [RLIMIT_RTTIME
] = {"Max realtime timeout", "us"},
458 /* Display limits for a process */
459 static int proc_pid_limits(struct task_struct
*task
, char *buffer
)
464 char *bufptr
= buffer
;
466 struct rlimit rlim
[RLIM_NLIMITS
];
469 if (!lock_task_sighand(task
,&flags
)) {
473 memcpy(rlim
, task
->signal
->rlim
, sizeof(struct rlimit
) * RLIM_NLIMITS
);
474 unlock_task_sighand(task
, &flags
);
478 * print the file header
480 count
+= sprintf(&bufptr
[count
], "%-25s %-20s %-20s %-10s\n",
481 "Limit", "Soft Limit", "Hard Limit", "Units");
483 for (i
= 0; i
< RLIM_NLIMITS
; i
++) {
484 if (rlim
[i
].rlim_cur
== RLIM_INFINITY
)
485 count
+= sprintf(&bufptr
[count
], "%-25s %-20s ",
486 lnames
[i
].name
, "unlimited");
488 count
+= sprintf(&bufptr
[count
], "%-25s %-20lu ",
489 lnames
[i
].name
, rlim
[i
].rlim_cur
);
491 if (rlim
[i
].rlim_max
== RLIM_INFINITY
)
492 count
+= sprintf(&bufptr
[count
], "%-20s ", "unlimited");
494 count
+= sprintf(&bufptr
[count
], "%-20lu ",
498 count
+= sprintf(&bufptr
[count
], "%-10s\n",
501 count
+= sprintf(&bufptr
[count
], "\n");
507 /************************************************************************/
508 /* Here the fs part begins */
509 /************************************************************************/
511 /* permission checks */
512 static int proc_fd_access_allowed(struct inode
*inode
)
514 struct task_struct
*task
;
516 /* Allow access to a task's file descriptors if it is us or we
517 * may use ptrace attach to the process and find out that
520 task
= get_proc_task(inode
);
522 allowed
= ptrace_may_access(task
, PTRACE_MODE_READ
);
523 put_task_struct(task
);
528 static int proc_setattr(struct dentry
*dentry
, struct iattr
*attr
)
531 struct inode
*inode
= dentry
->d_inode
;
533 if (attr
->ia_valid
& ATTR_MODE
)
536 error
= inode_change_ok(inode
, attr
);
538 error
= inode_setattr(inode
, attr
);
542 static const struct inode_operations proc_def_inode_operations
= {
543 .setattr
= proc_setattr
,
546 static int mounts_open_common(struct inode
*inode
, struct file
*file
,
547 const struct seq_operations
*op
)
549 struct task_struct
*task
= get_proc_task(inode
);
551 struct mnt_namespace
*ns
= NULL
;
552 struct fs_struct
*fs
= NULL
;
554 struct proc_mounts
*p
;
559 nsp
= task_nsproxy(task
);
567 fs
= get_fs_struct(task
);
568 put_task_struct(task
);
576 read_lock(&fs
->lock
);
579 read_unlock(&fs
->lock
);
583 p
= kmalloc(sizeof(struct proc_mounts
), GFP_KERNEL
);
587 file
->private_data
= &p
->m
;
588 ret
= seq_open(file
, op
);
595 p
->event
= ns
->event
;
609 static int mounts_release(struct inode
*inode
, struct file
*file
)
611 struct proc_mounts
*p
= file
->private_data
;
614 return seq_release(inode
, file
);
617 static unsigned mounts_poll(struct file
*file
, poll_table
*wait
)
619 struct proc_mounts
*p
= file
->private_data
;
620 struct mnt_namespace
*ns
= p
->ns
;
623 poll_wait(file
, &ns
->poll
, wait
);
625 spin_lock(&vfsmount_lock
);
626 if (p
->event
!= ns
->event
) {
627 p
->event
= ns
->event
;
630 spin_unlock(&vfsmount_lock
);
635 static int mounts_open(struct inode
*inode
, struct file
*file
)
637 return mounts_open_common(inode
, file
, &mounts_op
);
640 static const struct file_operations proc_mounts_operations
= {
644 .release
= mounts_release
,
648 static int mountinfo_open(struct inode
*inode
, struct file
*file
)
650 return mounts_open_common(inode
, file
, &mountinfo_op
);
653 static const struct file_operations proc_mountinfo_operations
= {
654 .open
= mountinfo_open
,
657 .release
= mounts_release
,
661 static int mountstats_open(struct inode
*inode
, struct file
*file
)
663 return mounts_open_common(inode
, file
, &mountstats_op
);
666 static const struct file_operations proc_mountstats_operations
= {
667 .open
= mountstats_open
,
670 .release
= mounts_release
,
673 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
675 static ssize_t
proc_info_read(struct file
* file
, char __user
* buf
,
676 size_t count
, loff_t
*ppos
)
678 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
681 struct task_struct
*task
= get_proc_task(inode
);
687 if (count
> PROC_BLOCK_SIZE
)
688 count
= PROC_BLOCK_SIZE
;
691 if (!(page
= __get_free_page(GFP_TEMPORARY
)))
694 length
= PROC_I(inode
)->op
.proc_read(task
, (char*)page
);
697 length
= simple_read_from_buffer(buf
, count
, ppos
, (char *)page
, length
);
700 put_task_struct(task
);
705 static const struct file_operations proc_info_file_operations
= {
706 .read
= proc_info_read
,
709 static int proc_single_show(struct seq_file
*m
, void *v
)
711 struct inode
*inode
= m
->private;
712 struct pid_namespace
*ns
;
714 struct task_struct
*task
;
717 ns
= inode
->i_sb
->s_fs_info
;
718 pid
= proc_pid(inode
);
719 task
= get_pid_task(pid
, PIDTYPE_PID
);
723 ret
= PROC_I(inode
)->op
.proc_show(m
, ns
, pid
, task
);
725 put_task_struct(task
);
729 static int proc_single_open(struct inode
*inode
, struct file
*filp
)
732 ret
= single_open(filp
, proc_single_show
, NULL
);
734 struct seq_file
*m
= filp
->private_data
;
741 static const struct file_operations proc_single_file_operations
= {
742 .open
= proc_single_open
,
745 .release
= single_release
,
748 static int mem_open(struct inode
* inode
, struct file
* file
)
750 file
->private_data
= (void*)((long)current
->self_exec_id
);
754 static ssize_t
mem_read(struct file
* file
, char __user
* buf
,
755 size_t count
, loff_t
*ppos
)
757 struct task_struct
*task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
759 unsigned long src
= *ppos
;
761 struct mm_struct
*mm
;
766 if (check_mem_permission(task
))
770 page
= (char *)__get_free_page(GFP_TEMPORARY
);
776 mm
= get_task_mm(task
);
782 if (file
->private_data
!= (void*)((long)current
->self_exec_id
))
788 int this_len
, retval
;
790 this_len
= (count
> PAGE_SIZE
) ? PAGE_SIZE
: count
;
791 retval
= access_process_vm(task
, src
, page
, this_len
, 0);
792 if (!retval
|| check_mem_permission(task
)) {
798 if (copy_to_user(buf
, page
, retval
)) {
813 free_page((unsigned long) page
);
815 put_task_struct(task
);
820 #define mem_write NULL
823 /* This is a security hazard */
824 static ssize_t
mem_write(struct file
* file
, const char __user
*buf
,
825 size_t count
, loff_t
*ppos
)
829 struct task_struct
*task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
830 unsigned long dst
= *ppos
;
836 if (check_mem_permission(task
))
840 page
= (char *)__get_free_page(GFP_TEMPORARY
);
846 int this_len
, retval
;
848 this_len
= (count
> PAGE_SIZE
) ? PAGE_SIZE
: count
;
849 if (copy_from_user(page
, buf
, this_len
)) {
853 retval
= access_process_vm(task
, dst
, page
, this_len
, 1);
865 free_page((unsigned long) page
);
867 put_task_struct(task
);
873 loff_t
mem_lseek(struct file
*file
, loff_t offset
, int orig
)
877 file
->f_pos
= offset
;
880 file
->f_pos
+= offset
;
885 force_successful_syscall_return();
889 static const struct file_operations proc_mem_operations
= {
896 static ssize_t
environ_read(struct file
*file
, char __user
*buf
,
897 size_t count
, loff_t
*ppos
)
899 struct task_struct
*task
= get_proc_task(file
->f_dentry
->d_inode
);
901 unsigned long src
= *ppos
;
903 struct mm_struct
*mm
;
908 if (!ptrace_may_access(task
, PTRACE_MODE_READ
))
912 page
= (char *)__get_free_page(GFP_TEMPORARY
);
918 mm
= get_task_mm(task
);
923 int this_len
, retval
, max_len
;
925 this_len
= mm
->env_end
- (mm
->env_start
+ src
);
930 max_len
= (count
> PAGE_SIZE
) ? PAGE_SIZE
: count
;
931 this_len
= (this_len
> max_len
) ? max_len
: this_len
;
933 retval
= access_process_vm(task
, (mm
->env_start
+ src
),
941 if (copy_to_user(buf
, page
, retval
)) {
955 free_page((unsigned long) page
);
957 put_task_struct(task
);
962 static const struct file_operations proc_environ_operations
= {
963 .read
= environ_read
,
966 static ssize_t
oom_adjust_read(struct file
*file
, char __user
*buf
,
967 size_t count
, loff_t
*ppos
)
969 struct task_struct
*task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
970 char buffer
[PROC_NUMBUF
];
976 oom_adjust
= task
->oomkilladj
;
977 put_task_struct(task
);
979 len
= snprintf(buffer
, sizeof(buffer
), "%i\n", oom_adjust
);
981 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
984 static ssize_t
oom_adjust_write(struct file
*file
, const char __user
*buf
,
985 size_t count
, loff_t
*ppos
)
987 struct task_struct
*task
;
988 char buffer
[PROC_NUMBUF
], *end
;
991 memset(buffer
, 0, sizeof(buffer
));
992 if (count
> sizeof(buffer
) - 1)
993 count
= sizeof(buffer
) - 1;
994 if (copy_from_user(buffer
, buf
, count
))
996 oom_adjust
= simple_strtol(buffer
, &end
, 0);
997 if ((oom_adjust
< OOM_ADJUST_MIN
|| oom_adjust
> OOM_ADJUST_MAX
) &&
998 oom_adjust
!= OOM_DISABLE
)
1002 task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
1005 if (oom_adjust
< task
->oomkilladj
&& !capable(CAP_SYS_RESOURCE
)) {
1006 put_task_struct(task
);
1009 task
->oomkilladj
= oom_adjust
;
1010 put_task_struct(task
);
1011 if (end
- buffer
== 0)
1013 return end
- buffer
;
1016 static const struct file_operations proc_oom_adjust_operations
= {
1017 .read
= oom_adjust_read
,
1018 .write
= oom_adjust_write
,
1021 #ifdef CONFIG_AUDITSYSCALL
1022 #define TMPBUFLEN 21
1023 static ssize_t
proc_loginuid_read(struct file
* file
, char __user
* buf
,
1024 size_t count
, loff_t
*ppos
)
1026 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
1027 struct task_struct
*task
= get_proc_task(inode
);
1029 char tmpbuf
[TMPBUFLEN
];
1033 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
1034 audit_get_loginuid(task
));
1035 put_task_struct(task
);
1036 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1039 static ssize_t
proc_loginuid_write(struct file
* file
, const char __user
* buf
,
1040 size_t count
, loff_t
*ppos
)
1042 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
1047 if (!capable(CAP_AUDIT_CONTROL
))
1050 if (current
!= pid_task(proc_pid(inode
), PIDTYPE_PID
))
1053 if (count
>= PAGE_SIZE
)
1054 count
= PAGE_SIZE
- 1;
1057 /* No partial writes. */
1060 page
= (char*)__get_free_page(GFP_TEMPORARY
);
1064 if (copy_from_user(page
, buf
, count
))
1068 loginuid
= simple_strtoul(page
, &tmp
, 10);
1074 length
= audit_set_loginuid(current
, loginuid
);
1075 if (likely(length
== 0))
1079 free_page((unsigned long) page
);
1083 static const struct file_operations proc_loginuid_operations
= {
1084 .read
= proc_loginuid_read
,
1085 .write
= proc_loginuid_write
,
1088 static ssize_t
proc_sessionid_read(struct file
* file
, char __user
* buf
,
1089 size_t count
, loff_t
*ppos
)
1091 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
1092 struct task_struct
*task
= get_proc_task(inode
);
1094 char tmpbuf
[TMPBUFLEN
];
1098 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
1099 audit_get_sessionid(task
));
1100 put_task_struct(task
);
1101 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1104 static const struct file_operations proc_sessionid_operations
= {
1105 .read
= proc_sessionid_read
,
1109 #ifdef CONFIG_FAULT_INJECTION
1110 static ssize_t
proc_fault_inject_read(struct file
* file
, char __user
* buf
,
1111 size_t count
, loff_t
*ppos
)
1113 struct task_struct
*task
= get_proc_task(file
->f_dentry
->d_inode
);
1114 char buffer
[PROC_NUMBUF
];
1120 make_it_fail
= task
->make_it_fail
;
1121 put_task_struct(task
);
1123 len
= snprintf(buffer
, sizeof(buffer
), "%i\n", make_it_fail
);
1125 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
1128 static ssize_t
proc_fault_inject_write(struct file
* file
,
1129 const char __user
* buf
, size_t count
, loff_t
*ppos
)
1131 struct task_struct
*task
;
1132 char buffer
[PROC_NUMBUF
], *end
;
1135 if (!capable(CAP_SYS_RESOURCE
))
1137 memset(buffer
, 0, sizeof(buffer
));
1138 if (count
> sizeof(buffer
) - 1)
1139 count
= sizeof(buffer
) - 1;
1140 if (copy_from_user(buffer
, buf
, count
))
1142 make_it_fail
= simple_strtol(buffer
, &end
, 0);
1145 task
= get_proc_task(file
->f_dentry
->d_inode
);
1148 task
->make_it_fail
= make_it_fail
;
1149 put_task_struct(task
);
1150 if (end
- buffer
== 0)
1152 return end
- buffer
;
1155 static const struct file_operations proc_fault_inject_operations
= {
1156 .read
= proc_fault_inject_read
,
1157 .write
= proc_fault_inject_write
,
1162 #ifdef CONFIG_SCHED_DEBUG
1164 * Print out various scheduling related per-task fields:
1166 static int sched_show(struct seq_file
*m
, void *v
)
1168 struct inode
*inode
= m
->private;
1169 struct task_struct
*p
;
1173 p
= get_proc_task(inode
);
1176 proc_sched_show_task(p
, m
);
1184 sched_write(struct file
*file
, const char __user
*buf
,
1185 size_t count
, loff_t
*offset
)
1187 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1188 struct task_struct
*p
;
1192 p
= get_proc_task(inode
);
1195 proc_sched_set_task(p
);
1202 static int sched_open(struct inode
*inode
, struct file
*filp
)
1206 ret
= single_open(filp
, sched_show
, NULL
);
1208 struct seq_file
*m
= filp
->private_data
;
1215 static const struct file_operations proc_pid_sched_operations
= {
1218 .write
= sched_write
,
1219 .llseek
= seq_lseek
,
1220 .release
= single_release
,
1226 * We added or removed a vma mapping the executable. The vmas are only mapped
1227 * during exec and are not mapped with the mmap system call.
1228 * Callers must hold down_write() on the mm's mmap_sem for these
1230 void added_exe_file_vma(struct mm_struct
*mm
)
1232 mm
->num_exe_file_vmas
++;
1235 void removed_exe_file_vma(struct mm_struct
*mm
)
1237 mm
->num_exe_file_vmas
--;
1238 if ((mm
->num_exe_file_vmas
== 0) && mm
->exe_file
){
1240 mm
->exe_file
= NULL
;
1245 void set_mm_exe_file(struct mm_struct
*mm
, struct file
*new_exe_file
)
1248 get_file(new_exe_file
);
1251 mm
->exe_file
= new_exe_file
;
1252 mm
->num_exe_file_vmas
= 0;
1255 struct file
*get_mm_exe_file(struct mm_struct
*mm
)
1257 struct file
*exe_file
;
1259 /* We need mmap_sem to protect against races with removal of
1260 * VM_EXECUTABLE vmas */
1261 down_read(&mm
->mmap_sem
);
1262 exe_file
= mm
->exe_file
;
1265 up_read(&mm
->mmap_sem
);
1269 void dup_mm_exe_file(struct mm_struct
*oldmm
, struct mm_struct
*newmm
)
1271 /* It's safe to write the exe_file pointer without exe_file_lock because
1272 * this is called during fork when the task is not yet in /proc */
1273 newmm
->exe_file
= get_mm_exe_file(oldmm
);
1276 static int proc_exe_link(struct inode
*inode
, struct path
*exe_path
)
1278 struct task_struct
*task
;
1279 struct mm_struct
*mm
;
1280 struct file
*exe_file
;
1282 task
= get_proc_task(inode
);
1285 mm
= get_task_mm(task
);
1286 put_task_struct(task
);
1289 exe_file
= get_mm_exe_file(mm
);
1292 *exe_path
= exe_file
->f_path
;
1293 path_get(&exe_file
->f_path
);
1300 static void *proc_pid_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
1302 struct inode
*inode
= dentry
->d_inode
;
1303 int error
= -EACCES
;
1305 /* We don't need a base pointer in the /proc filesystem */
1306 path_put(&nd
->path
);
1308 /* Are we allowed to snoop on the tasks file descriptors? */
1309 if (!proc_fd_access_allowed(inode
))
1312 error
= PROC_I(inode
)->op
.proc_get_link(inode
, &nd
->path
);
1313 nd
->last_type
= LAST_BIND
;
1315 return ERR_PTR(error
);
1318 static int do_proc_readlink(struct path
*path
, char __user
*buffer
, int buflen
)
1320 char *tmp
= (char*)__get_free_page(GFP_TEMPORARY
);
1327 pathname
= d_path(path
, tmp
, PAGE_SIZE
);
1328 len
= PTR_ERR(pathname
);
1329 if (IS_ERR(pathname
))
1331 len
= tmp
+ PAGE_SIZE
- 1 - pathname
;
1335 if (copy_to_user(buffer
, pathname
, len
))
1338 free_page((unsigned long)tmp
);
1342 static int proc_pid_readlink(struct dentry
* dentry
, char __user
* buffer
, int buflen
)
1344 int error
= -EACCES
;
1345 struct inode
*inode
= dentry
->d_inode
;
1348 /* Are we allowed to snoop on the tasks file descriptors? */
1349 if (!proc_fd_access_allowed(inode
))
1352 error
= PROC_I(inode
)->op
.proc_get_link(inode
, &path
);
1356 error
= do_proc_readlink(&path
, buffer
, buflen
);
1362 static const struct inode_operations proc_pid_link_inode_operations
= {
1363 .readlink
= proc_pid_readlink
,
1364 .follow_link
= proc_pid_follow_link
,
1365 .setattr
= proc_setattr
,
1369 /* building an inode */
1371 static int task_dumpable(struct task_struct
*task
)
1374 struct mm_struct
*mm
;
1379 dumpable
= get_dumpable(mm
);
1387 static struct inode
*proc_pid_make_inode(struct super_block
* sb
, struct task_struct
*task
)
1389 struct inode
* inode
;
1390 struct proc_inode
*ei
;
1392 /* We need a new inode */
1394 inode
= new_inode(sb
);
1400 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1401 inode
->i_op
= &proc_def_inode_operations
;
1404 * grab the reference to task.
1406 ei
->pid
= get_task_pid(task
, PIDTYPE_PID
);
1412 if (task_dumpable(task
)) {
1413 inode
->i_uid
= task
->euid
;
1414 inode
->i_gid
= task
->egid
;
1416 security_task_to_inode(task
, inode
);
1426 static int pid_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
1428 struct inode
*inode
= dentry
->d_inode
;
1429 struct task_struct
*task
;
1430 generic_fillattr(inode
, stat
);
1435 task
= pid_task(proc_pid(inode
), PIDTYPE_PID
);
1437 if ((inode
->i_mode
== (S_IFDIR
|S_IRUGO
|S_IXUGO
)) ||
1438 task_dumpable(task
)) {
1439 stat
->uid
= task
->euid
;
1440 stat
->gid
= task
->egid
;
1450 * Exceptional case: normally we are not allowed to unhash a busy
1451 * directory. In this case, however, we can do it - no aliasing problems
1452 * due to the way we treat inodes.
1454 * Rewrite the inode's ownerships here because the owning task may have
1455 * performed a setuid(), etc.
1457 * Before the /proc/pid/status file was created the only way to read
1458 * the effective uid of a /process was to stat /proc/pid. Reading
1459 * /proc/pid/status is slow enough that procps and other packages
1460 * kept stating /proc/pid. To keep the rules in /proc simple I have
1461 * made this apply to all per process world readable and executable
1464 static int pid_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1466 struct inode
*inode
= dentry
->d_inode
;
1467 struct task_struct
*task
= get_proc_task(inode
);
1469 if ((inode
->i_mode
== (S_IFDIR
|S_IRUGO
|S_IXUGO
)) ||
1470 task_dumpable(task
)) {
1471 inode
->i_uid
= task
->euid
;
1472 inode
->i_gid
= task
->egid
;
1477 inode
->i_mode
&= ~(S_ISUID
| S_ISGID
);
1478 security_task_to_inode(task
, inode
);
1479 put_task_struct(task
);
1486 static int pid_delete_dentry(struct dentry
* dentry
)
1488 /* Is the task we represent dead?
1489 * If so, then don't put the dentry on the lru list,
1490 * kill it immediately.
1492 return !proc_pid(dentry
->d_inode
)->tasks
[PIDTYPE_PID
].first
;
1495 static struct dentry_operations pid_dentry_operations
=
1497 .d_revalidate
= pid_revalidate
,
1498 .d_delete
= pid_delete_dentry
,
1503 typedef struct dentry
*instantiate_t(struct inode
*, struct dentry
*,
1504 struct task_struct
*, const void *);
1507 * Fill a directory entry.
1509 * If possible create the dcache entry and derive our inode number and
1510 * file type from dcache entry.
1512 * Since all of the proc inode numbers are dynamically generated, the inode
1513 * numbers do not exist until the inode is cache. This means creating the
1514 * the dcache entry in readdir is necessary to keep the inode numbers
1515 * reported by readdir in sync with the inode numbers reported
1518 static int proc_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
1519 char *name
, int len
,
1520 instantiate_t instantiate
, struct task_struct
*task
, const void *ptr
)
1522 struct dentry
*child
, *dir
= filp
->f_path
.dentry
;
1523 struct inode
*inode
;
1526 unsigned type
= DT_UNKNOWN
;
1530 qname
.hash
= full_name_hash(name
, len
);
1532 child
= d_lookup(dir
, &qname
);
1535 new = d_alloc(dir
, &qname
);
1537 child
= instantiate(dir
->d_inode
, new, task
, ptr
);
1544 if (!child
|| IS_ERR(child
) || !child
->d_inode
)
1545 goto end_instantiate
;
1546 inode
= child
->d_inode
;
1549 type
= inode
->i_mode
>> 12;
1554 ino
= find_inode_number(dir
, &qname
);
1557 return filldir(dirent
, name
, len
, filp
->f_pos
, ino
, type
);
1560 static unsigned name_to_int(struct dentry
*dentry
)
1562 const char *name
= dentry
->d_name
.name
;
1563 int len
= dentry
->d_name
.len
;
1566 if (len
> 1 && *name
== '0')
1569 unsigned c
= *name
++ - '0';
1572 if (n
>= (~0U-9)/10)
1582 #define PROC_FDINFO_MAX 64
1584 static int proc_fd_info(struct inode
*inode
, struct path
*path
, char *info
)
1586 struct task_struct
*task
= get_proc_task(inode
);
1587 struct files_struct
*files
= NULL
;
1589 int fd
= proc_fd(inode
);
1592 files
= get_files_struct(task
);
1593 put_task_struct(task
);
1597 * We are not taking a ref to the file structure, so we must
1600 spin_lock(&files
->file_lock
);
1601 file
= fcheck_files(files
, fd
);
1604 *path
= file
->f_path
;
1605 path_get(&file
->f_path
);
1608 snprintf(info
, PROC_FDINFO_MAX
,
1611 (long long) file
->f_pos
,
1613 spin_unlock(&files
->file_lock
);
1614 put_files_struct(files
);
1617 spin_unlock(&files
->file_lock
);
1618 put_files_struct(files
);
1623 static int proc_fd_link(struct inode
*inode
, struct path
*path
)
1625 return proc_fd_info(inode
, path
, NULL
);
1628 static int tid_fd_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1630 struct inode
*inode
= dentry
->d_inode
;
1631 struct task_struct
*task
= get_proc_task(inode
);
1632 int fd
= proc_fd(inode
);
1633 struct files_struct
*files
;
1636 files
= get_files_struct(task
);
1639 if (fcheck_files(files
, fd
)) {
1641 put_files_struct(files
);
1642 if (task_dumpable(task
)) {
1643 inode
->i_uid
= task
->euid
;
1644 inode
->i_gid
= task
->egid
;
1649 inode
->i_mode
&= ~(S_ISUID
| S_ISGID
);
1650 security_task_to_inode(task
, inode
);
1651 put_task_struct(task
);
1655 put_files_struct(files
);
1657 put_task_struct(task
);
1663 static struct dentry_operations tid_fd_dentry_operations
=
1665 .d_revalidate
= tid_fd_revalidate
,
1666 .d_delete
= pid_delete_dentry
,
1669 static struct dentry
*proc_fd_instantiate(struct inode
*dir
,
1670 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
1672 unsigned fd
= *(const unsigned *)ptr
;
1674 struct files_struct
*files
;
1675 struct inode
*inode
;
1676 struct proc_inode
*ei
;
1677 struct dentry
*error
= ERR_PTR(-ENOENT
);
1679 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
1684 files
= get_files_struct(task
);
1687 inode
->i_mode
= S_IFLNK
;
1690 * We are not taking a ref to the file structure, so we must
1693 spin_lock(&files
->file_lock
);
1694 file
= fcheck_files(files
, fd
);
1697 if (file
->f_mode
& 1)
1698 inode
->i_mode
|= S_IRUSR
| S_IXUSR
;
1699 if (file
->f_mode
& 2)
1700 inode
->i_mode
|= S_IWUSR
| S_IXUSR
;
1701 spin_unlock(&files
->file_lock
);
1702 put_files_struct(files
);
1704 inode
->i_op
= &proc_pid_link_inode_operations
;
1706 ei
->op
.proc_get_link
= proc_fd_link
;
1707 dentry
->d_op
= &tid_fd_dentry_operations
;
1708 d_add(dentry
, inode
);
1709 /* Close the race of the process dying before we return the dentry */
1710 if (tid_fd_revalidate(dentry
, NULL
))
1716 spin_unlock(&files
->file_lock
);
1717 put_files_struct(files
);
1723 static struct dentry
*proc_lookupfd_common(struct inode
*dir
,
1724 struct dentry
*dentry
,
1725 instantiate_t instantiate
)
1727 struct task_struct
*task
= get_proc_task(dir
);
1728 unsigned fd
= name_to_int(dentry
);
1729 struct dentry
*result
= ERR_PTR(-ENOENT
);
1736 result
= instantiate(dir
, dentry
, task
, &fd
);
1738 put_task_struct(task
);
1743 static int proc_readfd_common(struct file
* filp
, void * dirent
,
1744 filldir_t filldir
, instantiate_t instantiate
)
1746 struct dentry
*dentry
= filp
->f_path
.dentry
;
1747 struct inode
*inode
= dentry
->d_inode
;
1748 struct task_struct
*p
= get_proc_task(inode
);
1749 unsigned int fd
, ino
;
1751 struct files_struct
* files
;
1761 if (filldir(dirent
, ".", 1, 0, inode
->i_ino
, DT_DIR
) < 0)
1765 ino
= parent_ino(dentry
);
1766 if (filldir(dirent
, "..", 2, 1, ino
, DT_DIR
) < 0)
1770 files
= get_files_struct(p
);
1774 for (fd
= filp
->f_pos
-2;
1775 fd
< files_fdtable(files
)->max_fds
;
1776 fd
++, filp
->f_pos
++) {
1777 char name
[PROC_NUMBUF
];
1780 if (!fcheck_files(files
, fd
))
1784 len
= snprintf(name
, sizeof(name
), "%d", fd
);
1785 if (proc_fill_cache(filp
, dirent
, filldir
,
1786 name
, len
, instantiate
,
1794 put_files_struct(files
);
1802 static struct dentry
*proc_lookupfd(struct inode
*dir
, struct dentry
*dentry
,
1803 struct nameidata
*nd
)
1805 return proc_lookupfd_common(dir
, dentry
, proc_fd_instantiate
);
1808 static int proc_readfd(struct file
*filp
, void *dirent
, filldir_t filldir
)
1810 return proc_readfd_common(filp
, dirent
, filldir
, proc_fd_instantiate
);
1813 static ssize_t
proc_fdinfo_read(struct file
*file
, char __user
*buf
,
1814 size_t len
, loff_t
*ppos
)
1816 char tmp
[PROC_FDINFO_MAX
];
1817 int err
= proc_fd_info(file
->f_path
.dentry
->d_inode
, NULL
, tmp
);
1819 err
= simple_read_from_buffer(buf
, len
, ppos
, tmp
, strlen(tmp
));
1823 static const struct file_operations proc_fdinfo_file_operations
= {
1824 .open
= nonseekable_open
,
1825 .read
= proc_fdinfo_read
,
1828 static const struct file_operations proc_fd_operations
= {
1829 .read
= generic_read_dir
,
1830 .readdir
= proc_readfd
,
1834 * /proc/pid/fd needs a special permission handler so that a process can still
1835 * access /proc/self/fd after it has executed a setuid().
1837 static int proc_fd_permission(struct inode
*inode
, int mask
,
1838 struct nameidata
*nd
)
1842 rv
= generic_permission(inode
, mask
, NULL
);
1845 if (task_pid(current
) == proc_pid(inode
))
1851 * proc directories can do almost nothing..
1853 static const struct inode_operations proc_fd_inode_operations
= {
1854 .lookup
= proc_lookupfd
,
1855 .permission
= proc_fd_permission
,
1856 .setattr
= proc_setattr
,
1859 static struct dentry
*proc_fdinfo_instantiate(struct inode
*dir
,
1860 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
1862 unsigned fd
= *(unsigned *)ptr
;
1863 struct inode
*inode
;
1864 struct proc_inode
*ei
;
1865 struct dentry
*error
= ERR_PTR(-ENOENT
);
1867 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
1872 inode
->i_mode
= S_IFREG
| S_IRUSR
;
1873 inode
->i_fop
= &proc_fdinfo_file_operations
;
1874 dentry
->d_op
= &tid_fd_dentry_operations
;
1875 d_add(dentry
, inode
);
1876 /* Close the race of the process dying before we return the dentry */
1877 if (tid_fd_revalidate(dentry
, NULL
))
1884 static struct dentry
*proc_lookupfdinfo(struct inode
*dir
,
1885 struct dentry
*dentry
,
1886 struct nameidata
*nd
)
1888 return proc_lookupfd_common(dir
, dentry
, proc_fdinfo_instantiate
);
1891 static int proc_readfdinfo(struct file
*filp
, void *dirent
, filldir_t filldir
)
1893 return proc_readfd_common(filp
, dirent
, filldir
,
1894 proc_fdinfo_instantiate
);
1897 static const struct file_operations proc_fdinfo_operations
= {
1898 .read
= generic_read_dir
,
1899 .readdir
= proc_readfdinfo
,
1903 * proc directories can do almost nothing..
1905 static const struct inode_operations proc_fdinfo_inode_operations
= {
1906 .lookup
= proc_lookupfdinfo
,
1907 .setattr
= proc_setattr
,
1911 static struct dentry
*proc_pident_instantiate(struct inode
*dir
,
1912 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
1914 const struct pid_entry
*p
= ptr
;
1915 struct inode
*inode
;
1916 struct proc_inode
*ei
;
1917 struct dentry
*error
= ERR_PTR(-EINVAL
);
1919 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
1924 inode
->i_mode
= p
->mode
;
1925 if (S_ISDIR(inode
->i_mode
))
1926 inode
->i_nlink
= 2; /* Use getattr to fix if necessary */
1928 inode
->i_op
= p
->iop
;
1930 inode
->i_fop
= p
->fop
;
1932 dentry
->d_op
= &pid_dentry_operations
;
1933 d_add(dentry
, inode
);
1934 /* Close the race of the process dying before we return the dentry */
1935 if (pid_revalidate(dentry
, NULL
))
1941 static struct dentry
*proc_pident_lookup(struct inode
*dir
,
1942 struct dentry
*dentry
,
1943 const struct pid_entry
*ents
,
1946 struct inode
*inode
;
1947 struct dentry
*error
;
1948 struct task_struct
*task
= get_proc_task(dir
);
1949 const struct pid_entry
*p
, *last
;
1951 error
= ERR_PTR(-ENOENT
);
1958 * Yes, it does not scale. And it should not. Don't add
1959 * new entries into /proc/<tgid>/ without very good reasons.
1961 last
= &ents
[nents
- 1];
1962 for (p
= ents
; p
<= last
; p
++) {
1963 if (p
->len
!= dentry
->d_name
.len
)
1965 if (!memcmp(dentry
->d_name
.name
, p
->name
, p
->len
))
1971 error
= proc_pident_instantiate(dir
, dentry
, task
, p
);
1973 put_task_struct(task
);
1978 static int proc_pident_fill_cache(struct file
*filp
, void *dirent
,
1979 filldir_t filldir
, struct task_struct
*task
, const struct pid_entry
*p
)
1981 return proc_fill_cache(filp
, dirent
, filldir
, p
->name
, p
->len
,
1982 proc_pident_instantiate
, task
, p
);
1985 static int proc_pident_readdir(struct file
*filp
,
1986 void *dirent
, filldir_t filldir
,
1987 const struct pid_entry
*ents
, unsigned int nents
)
1990 struct dentry
*dentry
= filp
->f_path
.dentry
;
1991 struct inode
*inode
= dentry
->d_inode
;
1992 struct task_struct
*task
= get_proc_task(inode
);
1993 const struct pid_entry
*p
, *last
;
2006 if (filldir(dirent
, ".", 1, i
, ino
, DT_DIR
) < 0)
2012 ino
= parent_ino(dentry
);
2013 if (filldir(dirent
, "..", 2, i
, ino
, DT_DIR
) < 0)
2025 last
= &ents
[nents
- 1];
2027 if (proc_pident_fill_cache(filp
, dirent
, filldir
, task
, p
) < 0)
2036 put_task_struct(task
);
2041 #ifdef CONFIG_SECURITY
2042 static ssize_t
proc_pid_attr_read(struct file
* file
, char __user
* buf
,
2043 size_t count
, loff_t
*ppos
)
2045 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
2048 struct task_struct
*task
= get_proc_task(inode
);
2053 length
= security_getprocattr(task
,
2054 (char*)file
->f_path
.dentry
->d_name
.name
,
2056 put_task_struct(task
);
2058 length
= simple_read_from_buffer(buf
, count
, ppos
, p
, length
);
2063 static ssize_t
proc_pid_attr_write(struct file
* file
, const char __user
* buf
,
2064 size_t count
, loff_t
*ppos
)
2066 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
2069 struct task_struct
*task
= get_proc_task(inode
);
2074 if (count
> PAGE_SIZE
)
2077 /* No partial writes. */
2083 page
= (char*)__get_free_page(GFP_TEMPORARY
);
2088 if (copy_from_user(page
, buf
, count
))
2091 length
= security_setprocattr(task
,
2092 (char*)file
->f_path
.dentry
->d_name
.name
,
2093 (void*)page
, count
);
2095 free_page((unsigned long) page
);
2097 put_task_struct(task
);
2102 static const struct file_operations proc_pid_attr_operations
= {
2103 .read
= proc_pid_attr_read
,
2104 .write
= proc_pid_attr_write
,
2107 static const struct pid_entry attr_dir_stuff
[] = {
2108 REG("current", S_IRUGO
|S_IWUGO
, pid_attr
),
2109 REG("prev", S_IRUGO
, pid_attr
),
2110 REG("exec", S_IRUGO
|S_IWUGO
, pid_attr
),
2111 REG("fscreate", S_IRUGO
|S_IWUGO
, pid_attr
),
2112 REG("keycreate", S_IRUGO
|S_IWUGO
, pid_attr
),
2113 REG("sockcreate", S_IRUGO
|S_IWUGO
, pid_attr
),
2116 static int proc_attr_dir_readdir(struct file
* filp
,
2117 void * dirent
, filldir_t filldir
)
2119 return proc_pident_readdir(filp
,dirent
,filldir
,
2120 attr_dir_stuff
,ARRAY_SIZE(attr_dir_stuff
));
2123 static const struct file_operations proc_attr_dir_operations
= {
2124 .read
= generic_read_dir
,
2125 .readdir
= proc_attr_dir_readdir
,
2128 static struct dentry
*proc_attr_dir_lookup(struct inode
*dir
,
2129 struct dentry
*dentry
, struct nameidata
*nd
)
2131 return proc_pident_lookup(dir
, dentry
,
2132 attr_dir_stuff
, ARRAY_SIZE(attr_dir_stuff
));
2135 static const struct inode_operations proc_attr_dir_inode_operations
= {
2136 .lookup
= proc_attr_dir_lookup
,
2137 .getattr
= pid_getattr
,
2138 .setattr
= proc_setattr
,
2143 #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2144 static ssize_t
proc_coredump_filter_read(struct file
*file
, char __user
*buf
,
2145 size_t count
, loff_t
*ppos
)
2147 struct task_struct
*task
= get_proc_task(file
->f_dentry
->d_inode
);
2148 struct mm_struct
*mm
;
2149 char buffer
[PROC_NUMBUF
];
2157 mm
= get_task_mm(task
);
2159 len
= snprintf(buffer
, sizeof(buffer
), "%08lx\n",
2160 ((mm
->flags
& MMF_DUMP_FILTER_MASK
) >>
2161 MMF_DUMP_FILTER_SHIFT
));
2163 ret
= simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
2166 put_task_struct(task
);
2171 static ssize_t
proc_coredump_filter_write(struct file
*file
,
2172 const char __user
*buf
,
2176 struct task_struct
*task
;
2177 struct mm_struct
*mm
;
2178 char buffer
[PROC_NUMBUF
], *end
;
2185 memset(buffer
, 0, sizeof(buffer
));
2186 if (count
> sizeof(buffer
) - 1)
2187 count
= sizeof(buffer
) - 1;
2188 if (copy_from_user(buffer
, buf
, count
))
2192 val
= (unsigned int)simple_strtoul(buffer
, &end
, 0);
2195 if (end
- buffer
== 0)
2199 task
= get_proc_task(file
->f_dentry
->d_inode
);
2204 mm
= get_task_mm(task
);
2208 for (i
= 0, mask
= 1; i
< MMF_DUMP_FILTER_BITS
; i
++, mask
<<= 1) {
2210 set_bit(i
+ MMF_DUMP_FILTER_SHIFT
, &mm
->flags
);
2212 clear_bit(i
+ MMF_DUMP_FILTER_SHIFT
, &mm
->flags
);
2217 put_task_struct(task
);
2222 static const struct file_operations proc_coredump_filter_operations
= {
2223 .read
= proc_coredump_filter_read
,
2224 .write
= proc_coredump_filter_write
,
2231 static int proc_self_readlink(struct dentry
*dentry
, char __user
*buffer
,
2234 struct pid_namespace
*ns
= dentry
->d_sb
->s_fs_info
;
2235 pid_t tgid
= task_tgid_nr_ns(current
, ns
);
2236 char tmp
[PROC_NUMBUF
];
2239 sprintf(tmp
, "%d", tgid
);
2240 return vfs_readlink(dentry
,buffer
,buflen
,tmp
);
2243 static void *proc_self_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
2245 struct pid_namespace
*ns
= dentry
->d_sb
->s_fs_info
;
2246 pid_t tgid
= task_tgid_nr_ns(current
, ns
);
2247 char tmp
[PROC_NUMBUF
];
2249 return ERR_PTR(-ENOENT
);
2250 sprintf(tmp
, "%d", task_tgid_nr_ns(current
, ns
));
2251 return ERR_PTR(vfs_follow_link(nd
,tmp
));
2254 static const struct inode_operations proc_self_inode_operations
= {
2255 .readlink
= proc_self_readlink
,
2256 .follow_link
= proc_self_follow_link
,
2262 * These are the directory entries in the root directory of /proc
2263 * that properly belong to the /proc filesystem, as they describe
2264 * describe something that is process related.
2266 static const struct pid_entry proc_base_stuff
[] = {
2267 NOD("self", S_IFLNK
|S_IRWXUGO
,
2268 &proc_self_inode_operations
, NULL
, {}),
2272 * Exceptional case: normally we are not allowed to unhash a busy
2273 * directory. In this case, however, we can do it - no aliasing problems
2274 * due to the way we treat inodes.
2276 static int proc_base_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
2278 struct inode
*inode
= dentry
->d_inode
;
2279 struct task_struct
*task
= get_proc_task(inode
);
2281 put_task_struct(task
);
2288 static struct dentry_operations proc_base_dentry_operations
=
2290 .d_revalidate
= proc_base_revalidate
,
2291 .d_delete
= pid_delete_dentry
,
2294 static struct dentry
*proc_base_instantiate(struct inode
*dir
,
2295 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
2297 const struct pid_entry
*p
= ptr
;
2298 struct inode
*inode
;
2299 struct proc_inode
*ei
;
2300 struct dentry
*error
= ERR_PTR(-EINVAL
);
2302 /* Allocate the inode */
2303 error
= ERR_PTR(-ENOMEM
);
2304 inode
= new_inode(dir
->i_sb
);
2308 /* Initialize the inode */
2310 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
2313 * grab the reference to the task.
2315 ei
->pid
= get_task_pid(task
, PIDTYPE_PID
);
2321 inode
->i_mode
= p
->mode
;
2322 if (S_ISDIR(inode
->i_mode
))
2324 if (S_ISLNK(inode
->i_mode
))
2327 inode
->i_op
= p
->iop
;
2329 inode
->i_fop
= p
->fop
;
2331 dentry
->d_op
= &proc_base_dentry_operations
;
2332 d_add(dentry
, inode
);
2341 static struct dentry
*proc_base_lookup(struct inode
*dir
, struct dentry
*dentry
)
2343 struct dentry
*error
;
2344 struct task_struct
*task
= get_proc_task(dir
);
2345 const struct pid_entry
*p
, *last
;
2347 error
= ERR_PTR(-ENOENT
);
2352 /* Lookup the directory entry */
2353 last
= &proc_base_stuff
[ARRAY_SIZE(proc_base_stuff
) - 1];
2354 for (p
= proc_base_stuff
; p
<= last
; p
++) {
2355 if (p
->len
!= dentry
->d_name
.len
)
2357 if (!memcmp(dentry
->d_name
.name
, p
->name
, p
->len
))
2363 error
= proc_base_instantiate(dir
, dentry
, task
, p
);
2366 put_task_struct(task
);
2371 static int proc_base_fill_cache(struct file
*filp
, void *dirent
,
2372 filldir_t filldir
, struct task_struct
*task
, const struct pid_entry
*p
)
2374 return proc_fill_cache(filp
, dirent
, filldir
, p
->name
, p
->len
,
2375 proc_base_instantiate
, task
, p
);
2378 #ifdef CONFIG_TASK_IO_ACCOUNTING
2379 static int proc_pid_io_accounting(struct task_struct
*task
, char *buffer
)
2381 return sprintf(buffer
,
2382 #ifdef CONFIG_TASK_XACCT
2388 "read_bytes: %llu\n"
2389 "write_bytes: %llu\n"
2390 "cancelled_write_bytes: %llu\n",
2391 #ifdef CONFIG_TASK_XACCT
2392 (unsigned long long)task
->rchar
,
2393 (unsigned long long)task
->wchar
,
2394 (unsigned long long)task
->syscr
,
2395 (unsigned long long)task
->syscw
,
2397 (unsigned long long)task
->ioac
.read_bytes
,
2398 (unsigned long long)task
->ioac
.write_bytes
,
2399 (unsigned long long)task
->ioac
.cancelled_write_bytes
);
2406 static const struct file_operations proc_task_operations
;
2407 static const struct inode_operations proc_task_inode_operations
;
2409 static const struct pid_entry tgid_base_stuff
[] = {
2410 DIR("task", S_IRUGO
|S_IXUGO
, task
),
2411 DIR("fd", S_IRUSR
|S_IXUSR
, fd
),
2412 DIR("fdinfo", S_IRUSR
|S_IXUSR
, fdinfo
),
2414 DIR("net", S_IRUGO
|S_IXUGO
, net
),
2416 REG("environ", S_IRUSR
, environ
),
2417 INF("auxv", S_IRUSR
, pid_auxv
),
2418 ONE("status", S_IRUGO
, pid_status
),
2419 INF("limits", S_IRUSR
, pid_limits
),
2420 #ifdef CONFIG_SCHED_DEBUG
2421 REG("sched", S_IRUGO
|S_IWUSR
, pid_sched
),
2423 INF("cmdline", S_IRUGO
, pid_cmdline
),
2424 ONE("stat", S_IRUGO
, tgid_stat
),
2425 ONE("statm", S_IRUGO
, pid_statm
),
2426 REG("maps", S_IRUGO
, maps
),
2428 REG("numa_maps", S_IRUGO
, numa_maps
),
2430 REG("mem", S_IRUSR
|S_IWUSR
, mem
),
2434 REG("mounts", S_IRUGO
, mounts
),
2435 REG("mountinfo", S_IRUGO
, mountinfo
),
2436 REG("mountstats", S_IRUSR
, mountstats
),
2437 #ifdef CONFIG_PROC_PAGE_MONITOR
2438 REG("clear_refs", S_IWUSR
, clear_refs
),
2439 REG("smaps", S_IRUGO
, smaps
),
2440 REG("pagemap", S_IRUSR
, pagemap
),
2442 #ifdef CONFIG_SECURITY
2443 DIR("attr", S_IRUGO
|S_IXUGO
, attr_dir
),
2445 #ifdef CONFIG_KALLSYMS
2446 INF("wchan", S_IRUGO
, pid_wchan
),
2448 #ifdef CONFIG_SCHEDSTATS
2449 INF("schedstat", S_IRUGO
, pid_schedstat
),
2451 #ifdef CONFIG_LATENCYTOP
2452 REG("latency", S_IRUGO
, lstats
),
2454 #ifdef CONFIG_PROC_PID_CPUSET
2455 REG("cpuset", S_IRUGO
, cpuset
),
2457 #ifdef CONFIG_CGROUPS
2458 REG("cgroup", S_IRUGO
, cgroup
),
2460 INF("oom_score", S_IRUGO
, oom_score
),
2461 REG("oom_adj", S_IRUGO
|S_IWUSR
, oom_adjust
),
2462 #ifdef CONFIG_AUDITSYSCALL
2463 REG("loginuid", S_IWUSR
|S_IRUGO
, loginuid
),
2464 REG("sessionid", S_IRUGO
, sessionid
),
2466 #ifdef CONFIG_FAULT_INJECTION
2467 REG("make-it-fail", S_IRUGO
|S_IWUSR
, fault_inject
),
2469 #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2470 REG("coredump_filter", S_IRUGO
|S_IWUSR
, coredump_filter
),
2472 #ifdef CONFIG_TASK_IO_ACCOUNTING
2473 INF("io", S_IRUGO
, pid_io_accounting
),
2477 static int proc_tgid_base_readdir(struct file
* filp
,
2478 void * dirent
, filldir_t filldir
)
2480 return proc_pident_readdir(filp
,dirent
,filldir
,
2481 tgid_base_stuff
,ARRAY_SIZE(tgid_base_stuff
));
2484 static const struct file_operations proc_tgid_base_operations
= {
2485 .read
= generic_read_dir
,
2486 .readdir
= proc_tgid_base_readdir
,
2489 static struct dentry
*proc_tgid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
){
2490 return proc_pident_lookup(dir
, dentry
,
2491 tgid_base_stuff
, ARRAY_SIZE(tgid_base_stuff
));
2494 static const struct inode_operations proc_tgid_base_inode_operations
= {
2495 .lookup
= proc_tgid_base_lookup
,
2496 .getattr
= pid_getattr
,
2497 .setattr
= proc_setattr
,
2500 static void proc_flush_task_mnt(struct vfsmount
*mnt
, pid_t pid
, pid_t tgid
)
2502 struct dentry
*dentry
, *leader
, *dir
;
2503 char buf
[PROC_NUMBUF
];
2507 name
.len
= snprintf(buf
, sizeof(buf
), "%d", pid
);
2508 dentry
= d_hash_and_lookup(mnt
->mnt_root
, &name
);
2510 if (!(current
->flags
& PF_EXITING
))
2511 shrink_dcache_parent(dentry
);
2520 name
.len
= snprintf(buf
, sizeof(buf
), "%d", tgid
);
2521 leader
= d_hash_and_lookup(mnt
->mnt_root
, &name
);
2526 name
.len
= strlen(name
.name
);
2527 dir
= d_hash_and_lookup(leader
, &name
);
2529 goto out_put_leader
;
2532 name
.len
= snprintf(buf
, sizeof(buf
), "%d", pid
);
2533 dentry
= d_hash_and_lookup(dir
, &name
);
2535 shrink_dcache_parent(dentry
);
2548 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2549 * @task: task that should be flushed.
2551 * When flushing dentries from proc, one needs to flush them from global
2552 * proc (proc_mnt) and from all the namespaces' procs this task was seen
2553 * in. This call is supposed to do all of this job.
2555 * Looks in the dcache for
2557 * /proc/@tgid/task/@pid
2558 * if either directory is present flushes it and all of it'ts children
2561 * It is safe and reasonable to cache /proc entries for a task until
2562 * that task exits. After that they just clog up the dcache with
2563 * useless entries, possibly causing useful dcache entries to be
2564 * flushed instead. This routine is proved to flush those useless
2565 * dcache entries at process exit time.
2567 * NOTE: This routine is just an optimization so it does not guarantee
2568 * that no dcache entries will exist at process exit time it
2569 * just makes it very unlikely that any will persist.
2572 void proc_flush_task(struct task_struct
*task
)
2575 struct pid
*pid
, *tgid
= NULL
;
2578 pid
= task_pid(task
);
2579 if (thread_group_leader(task
))
2580 tgid
= task_tgid(task
);
2582 for (i
= 0; i
<= pid
->level
; i
++) {
2583 upid
= &pid
->numbers
[i
];
2584 proc_flush_task_mnt(upid
->ns
->proc_mnt
, upid
->nr
,
2585 tgid
? tgid
->numbers
[i
].nr
: 0);
2588 upid
= &pid
->numbers
[pid
->level
];
2590 pid_ns_release_proc(upid
->ns
);
2593 static struct dentry
*proc_pid_instantiate(struct inode
*dir
,
2594 struct dentry
* dentry
,
2595 struct task_struct
*task
, const void *ptr
)
2597 struct dentry
*error
= ERR_PTR(-ENOENT
);
2598 struct inode
*inode
;
2600 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
2604 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
2605 inode
->i_op
= &proc_tgid_base_inode_operations
;
2606 inode
->i_fop
= &proc_tgid_base_operations
;
2607 inode
->i_flags
|=S_IMMUTABLE
;
2609 inode
->i_nlink
= 2 + pid_entry_count_dirs(tgid_base_stuff
,
2610 ARRAY_SIZE(tgid_base_stuff
));
2612 dentry
->d_op
= &pid_dentry_operations
;
2614 d_add(dentry
, inode
);
2615 /* Close the race of the process dying before we return the dentry */
2616 if (pid_revalidate(dentry
, NULL
))
2622 struct dentry
*proc_pid_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
)
2624 struct dentry
*result
= ERR_PTR(-ENOENT
);
2625 struct task_struct
*task
;
2627 struct pid_namespace
*ns
;
2629 result
= proc_base_lookup(dir
, dentry
);
2630 if (!IS_ERR(result
) || PTR_ERR(result
) != -ENOENT
)
2633 tgid
= name_to_int(dentry
);
2637 ns
= dentry
->d_sb
->s_fs_info
;
2639 task
= find_task_by_pid_ns(tgid
, ns
);
2641 get_task_struct(task
);
2646 result
= proc_pid_instantiate(dir
, dentry
, task
, NULL
);
2647 put_task_struct(task
);
2653 * Find the first task with tgid >= tgid
2658 struct task_struct
*task
;
2660 static struct tgid_iter
next_tgid(struct pid_namespace
*ns
, struct tgid_iter iter
)
2665 put_task_struct(iter
.task
);
2669 pid
= find_ge_pid(iter
.tgid
, ns
);
2671 iter
.tgid
= pid_nr_ns(pid
, ns
);
2672 iter
.task
= pid_task(pid
, PIDTYPE_PID
);
2673 /* What we to know is if the pid we have find is the
2674 * pid of a thread_group_leader. Testing for task
2675 * being a thread_group_leader is the obvious thing
2676 * todo but there is a window when it fails, due to
2677 * the pid transfer logic in de_thread.
2679 * So we perform the straight forward test of seeing
2680 * if the pid we have found is the pid of a thread
2681 * group leader, and don't worry if the task we have
2682 * found doesn't happen to be a thread group leader.
2683 * As we don't care in the case of readdir.
2685 if (!iter
.task
|| !has_group_leader_pid(iter
.task
)) {
2689 get_task_struct(iter
.task
);
2695 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
2697 static int proc_pid_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
2698 struct tgid_iter iter
)
2700 char name
[PROC_NUMBUF
];
2701 int len
= snprintf(name
, sizeof(name
), "%d", iter
.tgid
);
2702 return proc_fill_cache(filp
, dirent
, filldir
, name
, len
,
2703 proc_pid_instantiate
, iter
.task
, NULL
);
2706 /* for the /proc/ directory itself, after non-process stuff has been done */
2707 int proc_pid_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
2709 unsigned int nr
= filp
->f_pos
- FIRST_PROCESS_ENTRY
;
2710 struct task_struct
*reaper
= get_proc_task(filp
->f_path
.dentry
->d_inode
);
2711 struct tgid_iter iter
;
2712 struct pid_namespace
*ns
;
2717 for (; nr
< ARRAY_SIZE(proc_base_stuff
); filp
->f_pos
++, nr
++) {
2718 const struct pid_entry
*p
= &proc_base_stuff
[nr
];
2719 if (proc_base_fill_cache(filp
, dirent
, filldir
, reaper
, p
) < 0)
2723 ns
= filp
->f_dentry
->d_sb
->s_fs_info
;
2725 iter
.tgid
= filp
->f_pos
- TGID_OFFSET
;
2726 for (iter
= next_tgid(ns
, iter
);
2728 iter
.tgid
+= 1, iter
= next_tgid(ns
, iter
)) {
2729 filp
->f_pos
= iter
.tgid
+ TGID_OFFSET
;
2730 if (proc_pid_fill_cache(filp
, dirent
, filldir
, iter
) < 0) {
2731 put_task_struct(iter
.task
);
2735 filp
->f_pos
= PID_MAX_LIMIT
+ TGID_OFFSET
;
2737 put_task_struct(reaper
);
2745 static const struct pid_entry tid_base_stuff
[] = {
2746 DIR("fd", S_IRUSR
|S_IXUSR
, fd
),
2747 DIR("fdinfo", S_IRUSR
|S_IXUSR
, fdinfo
),
2748 REG("environ", S_IRUSR
, environ
),
2749 INF("auxv", S_IRUSR
, pid_auxv
),
2750 ONE("status", S_IRUGO
, pid_status
),
2751 INF("limits", S_IRUSR
, pid_limits
),
2752 #ifdef CONFIG_SCHED_DEBUG
2753 REG("sched", S_IRUGO
|S_IWUSR
, pid_sched
),
2755 INF("cmdline", S_IRUGO
, pid_cmdline
),
2756 ONE("stat", S_IRUGO
, tid_stat
),
2757 ONE("statm", S_IRUGO
, pid_statm
),
2758 REG("maps", S_IRUGO
, maps
),
2760 REG("numa_maps", S_IRUGO
, numa_maps
),
2762 REG("mem", S_IRUSR
|S_IWUSR
, mem
),
2766 REG("mounts", S_IRUGO
, mounts
),
2767 REG("mountinfo", S_IRUGO
, mountinfo
),
2768 #ifdef CONFIG_PROC_PAGE_MONITOR
2769 REG("clear_refs", S_IWUSR
, clear_refs
),
2770 REG("smaps", S_IRUGO
, smaps
),
2771 REG("pagemap", S_IRUSR
, pagemap
),
2773 #ifdef CONFIG_SECURITY
2774 DIR("attr", S_IRUGO
|S_IXUGO
, attr_dir
),
2776 #ifdef CONFIG_KALLSYMS
2777 INF("wchan", S_IRUGO
, pid_wchan
),
2779 #ifdef CONFIG_SCHEDSTATS
2780 INF("schedstat", S_IRUGO
, pid_schedstat
),
2782 #ifdef CONFIG_LATENCYTOP
2783 REG("latency", S_IRUGO
, lstats
),
2785 #ifdef CONFIG_PROC_PID_CPUSET
2786 REG("cpuset", S_IRUGO
, cpuset
),
2788 #ifdef CONFIG_CGROUPS
2789 REG("cgroup", S_IRUGO
, cgroup
),
2791 INF("oom_score", S_IRUGO
, oom_score
),
2792 REG("oom_adj", S_IRUGO
|S_IWUSR
, oom_adjust
),
2793 #ifdef CONFIG_AUDITSYSCALL
2794 REG("loginuid", S_IWUSR
|S_IRUGO
, loginuid
),
2795 REG("sessionid", S_IRUSR
, sessionid
),
2797 #ifdef CONFIG_FAULT_INJECTION
2798 REG("make-it-fail", S_IRUGO
|S_IWUSR
, fault_inject
),
2802 static int proc_tid_base_readdir(struct file
* filp
,
2803 void * dirent
, filldir_t filldir
)
2805 return proc_pident_readdir(filp
,dirent
,filldir
,
2806 tid_base_stuff
,ARRAY_SIZE(tid_base_stuff
));
2809 static struct dentry
*proc_tid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
){
2810 return proc_pident_lookup(dir
, dentry
,
2811 tid_base_stuff
, ARRAY_SIZE(tid_base_stuff
));
2814 static const struct file_operations proc_tid_base_operations
= {
2815 .read
= generic_read_dir
,
2816 .readdir
= proc_tid_base_readdir
,
2819 static const struct inode_operations proc_tid_base_inode_operations
= {
2820 .lookup
= proc_tid_base_lookup
,
2821 .getattr
= pid_getattr
,
2822 .setattr
= proc_setattr
,
2825 static struct dentry
*proc_task_instantiate(struct inode
*dir
,
2826 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
2828 struct dentry
*error
= ERR_PTR(-ENOENT
);
2829 struct inode
*inode
;
2830 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
2834 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
2835 inode
->i_op
= &proc_tid_base_inode_operations
;
2836 inode
->i_fop
= &proc_tid_base_operations
;
2837 inode
->i_flags
|=S_IMMUTABLE
;
2839 inode
->i_nlink
= 2 + pid_entry_count_dirs(tid_base_stuff
,
2840 ARRAY_SIZE(tid_base_stuff
));
2842 dentry
->d_op
= &pid_dentry_operations
;
2844 d_add(dentry
, inode
);
2845 /* Close the race of the process dying before we return the dentry */
2846 if (pid_revalidate(dentry
, NULL
))
2852 static struct dentry
*proc_task_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
)
2854 struct dentry
*result
= ERR_PTR(-ENOENT
);
2855 struct task_struct
*task
;
2856 struct task_struct
*leader
= get_proc_task(dir
);
2858 struct pid_namespace
*ns
;
2863 tid
= name_to_int(dentry
);
2867 ns
= dentry
->d_sb
->s_fs_info
;
2869 task
= find_task_by_pid_ns(tid
, ns
);
2871 get_task_struct(task
);
2875 if (!same_thread_group(leader
, task
))
2878 result
= proc_task_instantiate(dir
, dentry
, task
, NULL
);
2880 put_task_struct(task
);
2882 put_task_struct(leader
);
2888 * Find the first tid of a thread group to return to user space.
2890 * Usually this is just the thread group leader, but if the users
2891 * buffer was too small or there was a seek into the middle of the
2892 * directory we have more work todo.
2894 * In the case of a short read we start with find_task_by_pid.
2896 * In the case of a seek we start with the leader and walk nr
2899 static struct task_struct
*first_tid(struct task_struct
*leader
,
2900 int tid
, int nr
, struct pid_namespace
*ns
)
2902 struct task_struct
*pos
;
2905 /* Attempt to start with the pid of a thread */
2906 if (tid
&& (nr
> 0)) {
2907 pos
= find_task_by_pid_ns(tid
, ns
);
2908 if (pos
&& (pos
->group_leader
== leader
))
2912 /* If nr exceeds the number of threads there is nothing todo */
2914 if (nr
&& nr
>= get_nr_threads(leader
))
2917 /* If we haven't found our starting place yet start
2918 * with the leader and walk nr threads forward.
2920 for (pos
= leader
; nr
> 0; --nr
) {
2921 pos
= next_thread(pos
);
2922 if (pos
== leader
) {
2928 get_task_struct(pos
);
2935 * Find the next thread in the thread list.
2936 * Return NULL if there is an error or no next thread.
2938 * The reference to the input task_struct is released.
2940 static struct task_struct
*next_tid(struct task_struct
*start
)
2942 struct task_struct
*pos
= NULL
;
2944 if (pid_alive(start
)) {
2945 pos
= next_thread(start
);
2946 if (thread_group_leader(pos
))
2949 get_task_struct(pos
);
2952 put_task_struct(start
);
2956 static int proc_task_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
2957 struct task_struct
*task
, int tid
)
2959 char name
[PROC_NUMBUF
];
2960 int len
= snprintf(name
, sizeof(name
), "%d", tid
);
2961 return proc_fill_cache(filp
, dirent
, filldir
, name
, len
,
2962 proc_task_instantiate
, task
, NULL
);
2965 /* for the /proc/TGID/task/ directories */
2966 static int proc_task_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
2968 struct dentry
*dentry
= filp
->f_path
.dentry
;
2969 struct inode
*inode
= dentry
->d_inode
;
2970 struct task_struct
*leader
= NULL
;
2971 struct task_struct
*task
;
2972 int retval
= -ENOENT
;
2975 unsigned long pos
= filp
->f_pos
; /* avoiding "long long" filp->f_pos */
2976 struct pid_namespace
*ns
;
2978 task
= get_proc_task(inode
);
2982 if (pid_alive(task
)) {
2983 leader
= task
->group_leader
;
2984 get_task_struct(leader
);
2987 put_task_struct(task
);
2995 if (filldir(dirent
, ".", 1, pos
, ino
, DT_DIR
) < 0)
3000 ino
= parent_ino(dentry
);
3001 if (filldir(dirent
, "..", 2, pos
, ino
, DT_DIR
) < 0)
3007 /* f_version caches the tgid value that the last readdir call couldn't
3008 * return. lseek aka telldir automagically resets f_version to 0.
3010 ns
= filp
->f_dentry
->d_sb
->s_fs_info
;
3011 tid
= (int)filp
->f_version
;
3012 filp
->f_version
= 0;
3013 for (task
= first_tid(leader
, tid
, pos
- 2, ns
);
3015 task
= next_tid(task
), pos
++) {
3016 tid
= task_pid_nr_ns(task
, ns
);
3017 if (proc_task_fill_cache(filp
, dirent
, filldir
, task
, tid
) < 0) {
3018 /* returning this tgid failed, save it as the first
3019 * pid for the next readir call */
3020 filp
->f_version
= (u64
)tid
;
3021 put_task_struct(task
);
3027 put_task_struct(leader
);
3032 static int proc_task_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
3034 struct inode
*inode
= dentry
->d_inode
;
3035 struct task_struct
*p
= get_proc_task(inode
);
3036 generic_fillattr(inode
, stat
);
3040 stat
->nlink
+= get_nr_threads(p
);
3048 static const struct inode_operations proc_task_inode_operations
= {
3049 .lookup
= proc_task_lookup
,
3050 .getattr
= proc_task_getattr
,
3051 .setattr
= proc_setattr
,
3054 static const struct file_operations proc_task_operations
= {
3055 .read
= generic_read_dir
,
3056 .readdir
= proc_task_readdir
,