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_attach(task
))
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
&& __ptrace_may_attach(task
) < 0)
260 up_read(&mm
->mmap_sem
);
265 static int proc_pid_cmdline(struct task_struct
*task
, char * buffer
)
269 struct mm_struct
*mm
= get_task_mm(task
);
273 goto out_mm
; /* Shh! No looking before we're done */
275 len
= mm
->arg_end
- mm
->arg_start
;
280 res
= access_process_vm(task
, mm
->arg_start
, buffer
, len
, 0);
282 // If the nul at the end of args has been overwritten, then
283 // assume application is using setproctitle(3).
284 if (res
> 0 && buffer
[res
-1] != '\0' && len
< PAGE_SIZE
) {
285 len
= strnlen(buffer
, res
);
289 len
= mm
->env_end
- mm
->env_start
;
290 if (len
> PAGE_SIZE
- res
)
291 len
= PAGE_SIZE
- res
;
292 res
+= access_process_vm(task
, mm
->env_start
, buffer
+res
, len
, 0);
293 res
= strnlen(buffer
, res
);
302 static int proc_pid_auxv(struct task_struct
*task
, char *buffer
)
305 struct mm_struct
*mm
= get_task_mm(task
);
307 unsigned int nwords
= 0;
310 while (mm
->saved_auxv
[nwords
- 2] != 0); /* AT_NULL */
311 res
= nwords
* sizeof(mm
->saved_auxv
[0]);
314 memcpy(buffer
, mm
->saved_auxv
, res
);
321 #ifdef CONFIG_KALLSYMS
323 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
324 * Returns the resolved symbol. If that fails, simply return the address.
326 static int proc_pid_wchan(struct task_struct
*task
, char *buffer
)
329 char symname
[KSYM_NAME_LEN
];
331 wchan
= get_wchan(task
);
333 if (lookup_symbol_name(wchan
, symname
) < 0)
334 return sprintf(buffer
, "%lu", wchan
);
336 return sprintf(buffer
, "%s", symname
);
338 #endif /* CONFIG_KALLSYMS */
340 #ifdef CONFIG_SCHEDSTATS
342 * Provides /proc/PID/schedstat
344 static int proc_pid_schedstat(struct task_struct
*task
, char *buffer
)
346 return sprintf(buffer
, "%llu %llu %lu\n",
347 task
->sched_info
.cpu_time
,
348 task
->sched_info
.run_delay
,
349 task
->sched_info
.pcount
);
353 #ifdef CONFIG_LATENCYTOP
354 static int lstats_show_proc(struct seq_file
*m
, void *v
)
357 struct inode
*inode
= m
->private;
358 struct task_struct
*task
= get_proc_task(inode
);
362 seq_puts(m
, "Latency Top version : v0.1\n");
363 for (i
= 0; i
< 32; i
++) {
364 if (task
->latency_record
[i
].backtrace
[0]) {
366 seq_printf(m
, "%i %li %li ",
367 task
->latency_record
[i
].count
,
368 task
->latency_record
[i
].time
,
369 task
->latency_record
[i
].max
);
370 for (q
= 0; q
< LT_BACKTRACEDEPTH
; q
++) {
371 char sym
[KSYM_NAME_LEN
];
373 if (!task
->latency_record
[i
].backtrace
[q
])
375 if (task
->latency_record
[i
].backtrace
[q
] == ULONG_MAX
)
377 sprint_symbol(sym
, task
->latency_record
[i
].backtrace
[q
]);
378 c
= strchr(sym
, '+');
381 seq_printf(m
, "%s ", sym
);
387 put_task_struct(task
);
391 static int lstats_open(struct inode
*inode
, struct file
*file
)
393 return single_open(file
, lstats_show_proc
, inode
);
396 static ssize_t
lstats_write(struct file
*file
, const char __user
*buf
,
397 size_t count
, loff_t
*offs
)
399 struct task_struct
*task
= get_proc_task(file
->f_dentry
->d_inode
);
403 clear_all_latency_tracing(task
);
404 put_task_struct(task
);
409 static const struct file_operations proc_lstats_operations
= {
412 .write
= lstats_write
,
414 .release
= single_release
,
419 /* The badness from the OOM killer */
420 unsigned long badness(struct task_struct
*p
, unsigned long uptime
);
421 static int proc_oom_score(struct task_struct
*task
, char *buffer
)
423 unsigned long points
;
424 struct timespec uptime
;
426 do_posix_clock_monotonic_gettime(&uptime
);
427 read_lock(&tasklist_lock
);
428 points
= badness(task
, uptime
.tv_sec
);
429 read_unlock(&tasklist_lock
);
430 return sprintf(buffer
, "%lu\n", points
);
438 static const struct limit_names lnames
[RLIM_NLIMITS
] = {
439 [RLIMIT_CPU
] = {"Max cpu time", "ms"},
440 [RLIMIT_FSIZE
] = {"Max file size", "bytes"},
441 [RLIMIT_DATA
] = {"Max data size", "bytes"},
442 [RLIMIT_STACK
] = {"Max stack size", "bytes"},
443 [RLIMIT_CORE
] = {"Max core file size", "bytes"},
444 [RLIMIT_RSS
] = {"Max resident set", "bytes"},
445 [RLIMIT_NPROC
] = {"Max processes", "processes"},
446 [RLIMIT_NOFILE
] = {"Max open files", "files"},
447 [RLIMIT_MEMLOCK
] = {"Max locked memory", "bytes"},
448 [RLIMIT_AS
] = {"Max address space", "bytes"},
449 [RLIMIT_LOCKS
] = {"Max file locks", "locks"},
450 [RLIMIT_SIGPENDING
] = {"Max pending signals", "signals"},
451 [RLIMIT_MSGQUEUE
] = {"Max msgqueue size", "bytes"},
452 [RLIMIT_NICE
] = {"Max nice priority", NULL
},
453 [RLIMIT_RTPRIO
] = {"Max realtime priority", NULL
},
454 [RLIMIT_RTTIME
] = {"Max realtime timeout", "us"},
457 /* Display limits for a process */
458 static int proc_pid_limits(struct task_struct
*task
, char *buffer
)
463 char *bufptr
= buffer
;
465 struct rlimit rlim
[RLIM_NLIMITS
];
468 if (!lock_task_sighand(task
,&flags
)) {
472 memcpy(rlim
, task
->signal
->rlim
, sizeof(struct rlimit
) * RLIM_NLIMITS
);
473 unlock_task_sighand(task
, &flags
);
477 * print the file header
479 count
+= sprintf(&bufptr
[count
], "%-25s %-20s %-20s %-10s\n",
480 "Limit", "Soft Limit", "Hard Limit", "Units");
482 for (i
= 0; i
< RLIM_NLIMITS
; i
++) {
483 if (rlim
[i
].rlim_cur
== RLIM_INFINITY
)
484 count
+= sprintf(&bufptr
[count
], "%-25s %-20s ",
485 lnames
[i
].name
, "unlimited");
487 count
+= sprintf(&bufptr
[count
], "%-25s %-20lu ",
488 lnames
[i
].name
, rlim
[i
].rlim_cur
);
490 if (rlim
[i
].rlim_max
== RLIM_INFINITY
)
491 count
+= sprintf(&bufptr
[count
], "%-20s ", "unlimited");
493 count
+= sprintf(&bufptr
[count
], "%-20lu ",
497 count
+= sprintf(&bufptr
[count
], "%-10s\n",
500 count
+= sprintf(&bufptr
[count
], "\n");
506 /************************************************************************/
507 /* Here the fs part begins */
508 /************************************************************************/
510 /* permission checks */
511 static int proc_fd_access_allowed(struct inode
*inode
)
513 struct task_struct
*task
;
515 /* Allow access to a task's file descriptors if it is us or we
516 * may use ptrace attach to the process and find out that
519 task
= get_proc_task(inode
);
521 allowed
= ptrace_may_attach(task
);
522 put_task_struct(task
);
527 static int proc_setattr(struct dentry
*dentry
, struct iattr
*attr
)
530 struct inode
*inode
= dentry
->d_inode
;
532 if (attr
->ia_valid
& ATTR_MODE
)
535 error
= inode_change_ok(inode
, attr
);
537 error
= inode_setattr(inode
, attr
);
541 static const struct inode_operations proc_def_inode_operations
= {
542 .setattr
= proc_setattr
,
545 static int mounts_open_common(struct inode
*inode
, struct file
*file
,
546 const struct seq_operations
*op
)
548 struct task_struct
*task
= get_proc_task(inode
);
550 struct mnt_namespace
*ns
= NULL
;
551 struct fs_struct
*fs
= NULL
;
553 struct proc_mounts
*p
;
558 nsp
= task_nsproxy(task
);
566 fs
= get_fs_struct(task
);
567 put_task_struct(task
);
575 read_lock(&fs
->lock
);
578 read_unlock(&fs
->lock
);
582 p
= kmalloc(sizeof(struct proc_mounts
), GFP_KERNEL
);
586 file
->private_data
= &p
->m
;
587 ret
= seq_open(file
, op
);
594 p
->event
= ns
->event
;
608 static int mounts_release(struct inode
*inode
, struct file
*file
)
610 struct proc_mounts
*p
= file
->private_data
;
613 return seq_release(inode
, file
);
616 static unsigned mounts_poll(struct file
*file
, poll_table
*wait
)
618 struct proc_mounts
*p
= file
->private_data
;
619 struct mnt_namespace
*ns
= p
->ns
;
622 poll_wait(file
, &ns
->poll
, wait
);
624 spin_lock(&vfsmount_lock
);
625 if (p
->event
!= ns
->event
) {
626 p
->event
= ns
->event
;
629 spin_unlock(&vfsmount_lock
);
634 static int mounts_open(struct inode
*inode
, struct file
*file
)
636 return mounts_open_common(inode
, file
, &mounts_op
);
639 static const struct file_operations proc_mounts_operations
= {
643 .release
= mounts_release
,
647 static int mountinfo_open(struct inode
*inode
, struct file
*file
)
649 return mounts_open_common(inode
, file
, &mountinfo_op
);
652 static const struct file_operations proc_mountinfo_operations
= {
653 .open
= mountinfo_open
,
656 .release
= mounts_release
,
660 static int mountstats_open(struct inode
*inode
, struct file
*file
)
662 return mounts_open_common(inode
, file
, &mountstats_op
);
665 static const struct file_operations proc_mountstats_operations
= {
666 .open
= mountstats_open
,
669 .release
= mounts_release
,
672 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
674 static ssize_t
proc_info_read(struct file
* file
, char __user
* buf
,
675 size_t count
, loff_t
*ppos
)
677 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
680 struct task_struct
*task
= get_proc_task(inode
);
686 if (count
> PROC_BLOCK_SIZE
)
687 count
= PROC_BLOCK_SIZE
;
690 if (!(page
= __get_free_page(GFP_TEMPORARY
)))
693 length
= PROC_I(inode
)->op
.proc_read(task
, (char*)page
);
696 length
= simple_read_from_buffer(buf
, count
, ppos
, (char *)page
, length
);
699 put_task_struct(task
);
704 static const struct file_operations proc_info_file_operations
= {
705 .read
= proc_info_read
,
708 static int proc_single_show(struct seq_file
*m
, void *v
)
710 struct inode
*inode
= m
->private;
711 struct pid_namespace
*ns
;
713 struct task_struct
*task
;
716 ns
= inode
->i_sb
->s_fs_info
;
717 pid
= proc_pid(inode
);
718 task
= get_pid_task(pid
, PIDTYPE_PID
);
722 ret
= PROC_I(inode
)->op
.proc_show(m
, ns
, pid
, task
);
724 put_task_struct(task
);
728 static int proc_single_open(struct inode
*inode
, struct file
*filp
)
731 ret
= single_open(filp
, proc_single_show
, NULL
);
733 struct seq_file
*m
= filp
->private_data
;
740 static const struct file_operations proc_single_file_operations
= {
741 .open
= proc_single_open
,
744 .release
= single_release
,
747 static int mem_open(struct inode
* inode
, struct file
* file
)
749 file
->private_data
= (void*)((long)current
->self_exec_id
);
753 static ssize_t
mem_read(struct file
* file
, char __user
* buf
,
754 size_t count
, loff_t
*ppos
)
756 struct task_struct
*task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
758 unsigned long src
= *ppos
;
760 struct mm_struct
*mm
;
765 if (check_mem_permission(task
))
769 page
= (char *)__get_free_page(GFP_TEMPORARY
);
775 mm
= get_task_mm(task
);
781 if (file
->private_data
!= (void*)((long)current
->self_exec_id
))
787 int this_len
, retval
;
789 this_len
= (count
> PAGE_SIZE
) ? PAGE_SIZE
: count
;
790 retval
= access_process_vm(task
, src
, page
, this_len
, 0);
791 if (!retval
|| check_mem_permission(task
)) {
797 if (copy_to_user(buf
, page
, retval
)) {
812 free_page((unsigned long) page
);
814 put_task_struct(task
);
819 #define mem_write NULL
822 /* This is a security hazard */
823 static ssize_t
mem_write(struct file
* file
, const char __user
*buf
,
824 size_t count
, loff_t
*ppos
)
828 struct task_struct
*task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
829 unsigned long dst
= *ppos
;
835 if (check_mem_permission(task
))
839 page
= (char *)__get_free_page(GFP_TEMPORARY
);
845 int this_len
, retval
;
847 this_len
= (count
> PAGE_SIZE
) ? PAGE_SIZE
: count
;
848 if (copy_from_user(page
, buf
, this_len
)) {
852 retval
= access_process_vm(task
, dst
, page
, this_len
, 1);
864 free_page((unsigned long) page
);
866 put_task_struct(task
);
872 loff_t
mem_lseek(struct file
*file
, loff_t offset
, int orig
)
876 file
->f_pos
= offset
;
879 file
->f_pos
+= offset
;
884 force_successful_syscall_return();
888 static const struct file_operations proc_mem_operations
= {
895 static ssize_t
environ_read(struct file
*file
, char __user
*buf
,
896 size_t count
, loff_t
*ppos
)
898 struct task_struct
*task
= get_proc_task(file
->f_dentry
->d_inode
);
900 unsigned long src
= *ppos
;
902 struct mm_struct
*mm
;
907 if (!ptrace_may_attach(task
))
911 page
= (char *)__get_free_page(GFP_TEMPORARY
);
917 mm
= get_task_mm(task
);
922 int this_len
, retval
, max_len
;
924 this_len
= mm
->env_end
- (mm
->env_start
+ src
);
929 max_len
= (count
> PAGE_SIZE
) ? PAGE_SIZE
: count
;
930 this_len
= (this_len
> max_len
) ? max_len
: this_len
;
932 retval
= access_process_vm(task
, (mm
->env_start
+ src
),
940 if (copy_to_user(buf
, page
, retval
)) {
954 free_page((unsigned long) page
);
956 put_task_struct(task
);
961 static const struct file_operations proc_environ_operations
= {
962 .read
= environ_read
,
965 static ssize_t
oom_adjust_read(struct file
*file
, char __user
*buf
,
966 size_t count
, loff_t
*ppos
)
968 struct task_struct
*task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
969 char buffer
[PROC_NUMBUF
];
975 oom_adjust
= task
->oomkilladj
;
976 put_task_struct(task
);
978 len
= snprintf(buffer
, sizeof(buffer
), "%i\n", oom_adjust
);
980 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
983 static ssize_t
oom_adjust_write(struct file
*file
, const char __user
*buf
,
984 size_t count
, loff_t
*ppos
)
986 struct task_struct
*task
;
987 char buffer
[PROC_NUMBUF
], *end
;
990 memset(buffer
, 0, sizeof(buffer
));
991 if (count
> sizeof(buffer
) - 1)
992 count
= sizeof(buffer
) - 1;
993 if (copy_from_user(buffer
, buf
, count
))
995 oom_adjust
= simple_strtol(buffer
, &end
, 0);
996 if ((oom_adjust
< OOM_ADJUST_MIN
|| oom_adjust
> OOM_ADJUST_MAX
) &&
997 oom_adjust
!= OOM_DISABLE
)
1001 task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
1004 if (oom_adjust
< task
->oomkilladj
&& !capable(CAP_SYS_RESOURCE
)) {
1005 put_task_struct(task
);
1008 task
->oomkilladj
= oom_adjust
;
1009 put_task_struct(task
);
1010 if (end
- buffer
== 0)
1012 return end
- buffer
;
1015 static const struct file_operations proc_oom_adjust_operations
= {
1016 .read
= oom_adjust_read
,
1017 .write
= oom_adjust_write
,
1020 #ifdef CONFIG_AUDITSYSCALL
1021 #define TMPBUFLEN 21
1022 static ssize_t
proc_loginuid_read(struct file
* file
, char __user
* buf
,
1023 size_t count
, loff_t
*ppos
)
1025 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
1026 struct task_struct
*task
= get_proc_task(inode
);
1028 char tmpbuf
[TMPBUFLEN
];
1032 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
1033 audit_get_loginuid(task
));
1034 put_task_struct(task
);
1035 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1038 static ssize_t
proc_loginuid_write(struct file
* file
, const char __user
* buf
,
1039 size_t count
, loff_t
*ppos
)
1041 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
1046 if (!capable(CAP_AUDIT_CONTROL
))
1049 if (current
!= pid_task(proc_pid(inode
), PIDTYPE_PID
))
1052 if (count
>= PAGE_SIZE
)
1053 count
= PAGE_SIZE
- 1;
1056 /* No partial writes. */
1059 page
= (char*)__get_free_page(GFP_TEMPORARY
);
1063 if (copy_from_user(page
, buf
, count
))
1067 loginuid
= simple_strtoul(page
, &tmp
, 10);
1073 length
= audit_set_loginuid(current
, loginuid
);
1074 if (likely(length
== 0))
1078 free_page((unsigned long) page
);
1082 static const struct file_operations proc_loginuid_operations
= {
1083 .read
= proc_loginuid_read
,
1084 .write
= proc_loginuid_write
,
1087 static ssize_t
proc_sessionid_read(struct file
* file
, char __user
* buf
,
1088 size_t count
, loff_t
*ppos
)
1090 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
1091 struct task_struct
*task
= get_proc_task(inode
);
1093 char tmpbuf
[TMPBUFLEN
];
1097 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
1098 audit_get_sessionid(task
));
1099 put_task_struct(task
);
1100 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1103 static const struct file_operations proc_sessionid_operations
= {
1104 .read
= proc_sessionid_read
,
1108 #ifdef CONFIG_FAULT_INJECTION
1109 static ssize_t
proc_fault_inject_read(struct file
* file
, char __user
* buf
,
1110 size_t count
, loff_t
*ppos
)
1112 struct task_struct
*task
= get_proc_task(file
->f_dentry
->d_inode
);
1113 char buffer
[PROC_NUMBUF
];
1119 make_it_fail
= task
->make_it_fail
;
1120 put_task_struct(task
);
1122 len
= snprintf(buffer
, sizeof(buffer
), "%i\n", make_it_fail
);
1124 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
1127 static ssize_t
proc_fault_inject_write(struct file
* file
,
1128 const char __user
* buf
, size_t count
, loff_t
*ppos
)
1130 struct task_struct
*task
;
1131 char buffer
[PROC_NUMBUF
], *end
;
1134 if (!capable(CAP_SYS_RESOURCE
))
1136 memset(buffer
, 0, sizeof(buffer
));
1137 if (count
> sizeof(buffer
) - 1)
1138 count
= sizeof(buffer
) - 1;
1139 if (copy_from_user(buffer
, buf
, count
))
1141 make_it_fail
= simple_strtol(buffer
, &end
, 0);
1144 task
= get_proc_task(file
->f_dentry
->d_inode
);
1147 task
->make_it_fail
= make_it_fail
;
1148 put_task_struct(task
);
1149 if (end
- buffer
== 0)
1151 return end
- buffer
;
1154 static const struct file_operations proc_fault_inject_operations
= {
1155 .read
= proc_fault_inject_read
,
1156 .write
= proc_fault_inject_write
,
1161 #ifdef CONFIG_SCHED_DEBUG
1163 * Print out various scheduling related per-task fields:
1165 static int sched_show(struct seq_file
*m
, void *v
)
1167 struct inode
*inode
= m
->private;
1168 struct task_struct
*p
;
1172 p
= get_proc_task(inode
);
1175 proc_sched_show_task(p
, m
);
1183 sched_write(struct file
*file
, const char __user
*buf
,
1184 size_t count
, loff_t
*offset
)
1186 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1187 struct task_struct
*p
;
1191 p
= get_proc_task(inode
);
1194 proc_sched_set_task(p
);
1201 static int sched_open(struct inode
*inode
, struct file
*filp
)
1205 ret
= single_open(filp
, sched_show
, NULL
);
1207 struct seq_file
*m
= filp
->private_data
;
1214 static const struct file_operations proc_pid_sched_operations
= {
1217 .write
= sched_write
,
1218 .llseek
= seq_lseek
,
1219 .release
= single_release
,
1225 * We added or removed a vma mapping the executable. The vmas are only mapped
1226 * during exec and are not mapped with the mmap system call.
1227 * Callers must hold down_write() on the mm's mmap_sem for these
1229 void added_exe_file_vma(struct mm_struct
*mm
)
1231 mm
->num_exe_file_vmas
++;
1234 void removed_exe_file_vma(struct mm_struct
*mm
)
1236 mm
->num_exe_file_vmas
--;
1237 if ((mm
->num_exe_file_vmas
== 0) && mm
->exe_file
){
1239 mm
->exe_file
= NULL
;
1244 void set_mm_exe_file(struct mm_struct
*mm
, struct file
*new_exe_file
)
1247 get_file(new_exe_file
);
1250 mm
->exe_file
= new_exe_file
;
1251 mm
->num_exe_file_vmas
= 0;
1254 struct file
*get_mm_exe_file(struct mm_struct
*mm
)
1256 struct file
*exe_file
;
1258 /* We need mmap_sem to protect against races with removal of
1259 * VM_EXECUTABLE vmas */
1260 down_read(&mm
->mmap_sem
);
1261 exe_file
= mm
->exe_file
;
1264 up_read(&mm
->mmap_sem
);
1268 void dup_mm_exe_file(struct mm_struct
*oldmm
, struct mm_struct
*newmm
)
1270 /* It's safe to write the exe_file pointer without exe_file_lock because
1271 * this is called during fork when the task is not yet in /proc */
1272 newmm
->exe_file
= get_mm_exe_file(oldmm
);
1275 static int proc_exe_link(struct inode
*inode
, struct path
*exe_path
)
1277 struct task_struct
*task
;
1278 struct mm_struct
*mm
;
1279 struct file
*exe_file
;
1281 task
= get_proc_task(inode
);
1284 mm
= get_task_mm(task
);
1285 put_task_struct(task
);
1288 exe_file
= get_mm_exe_file(mm
);
1291 *exe_path
= exe_file
->f_path
;
1292 path_get(&exe_file
->f_path
);
1299 static void *proc_pid_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
1301 struct inode
*inode
= dentry
->d_inode
;
1302 int error
= -EACCES
;
1304 /* We don't need a base pointer in the /proc filesystem */
1305 path_put(&nd
->path
);
1307 /* Are we allowed to snoop on the tasks file descriptors? */
1308 if (!proc_fd_access_allowed(inode
))
1311 error
= PROC_I(inode
)->op
.proc_get_link(inode
, &nd
->path
);
1312 nd
->last_type
= LAST_BIND
;
1314 return ERR_PTR(error
);
1317 static int do_proc_readlink(struct path
*path
, char __user
*buffer
, int buflen
)
1319 char *tmp
= (char*)__get_free_page(GFP_TEMPORARY
);
1326 pathname
= d_path(path
, tmp
, PAGE_SIZE
);
1327 len
= PTR_ERR(pathname
);
1328 if (IS_ERR(pathname
))
1330 len
= tmp
+ PAGE_SIZE
- 1 - pathname
;
1334 if (copy_to_user(buffer
, pathname
, len
))
1337 free_page((unsigned long)tmp
);
1341 static int proc_pid_readlink(struct dentry
* dentry
, char __user
* buffer
, int buflen
)
1343 int error
= -EACCES
;
1344 struct inode
*inode
= dentry
->d_inode
;
1347 /* Are we allowed to snoop on the tasks file descriptors? */
1348 if (!proc_fd_access_allowed(inode
))
1351 error
= PROC_I(inode
)->op
.proc_get_link(inode
, &path
);
1355 error
= do_proc_readlink(&path
, buffer
, buflen
);
1361 static const struct inode_operations proc_pid_link_inode_operations
= {
1362 .readlink
= proc_pid_readlink
,
1363 .follow_link
= proc_pid_follow_link
,
1364 .setattr
= proc_setattr
,
1368 /* building an inode */
1370 static int task_dumpable(struct task_struct
*task
)
1373 struct mm_struct
*mm
;
1378 dumpable
= get_dumpable(mm
);
1386 static struct inode
*proc_pid_make_inode(struct super_block
* sb
, struct task_struct
*task
)
1388 struct inode
* inode
;
1389 struct proc_inode
*ei
;
1391 /* We need a new inode */
1393 inode
= new_inode(sb
);
1399 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1400 inode
->i_op
= &proc_def_inode_operations
;
1403 * grab the reference to task.
1405 ei
->pid
= get_task_pid(task
, PIDTYPE_PID
);
1411 if (task_dumpable(task
)) {
1412 inode
->i_uid
= task
->euid
;
1413 inode
->i_gid
= task
->egid
;
1415 security_task_to_inode(task
, inode
);
1425 static int pid_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
1427 struct inode
*inode
= dentry
->d_inode
;
1428 struct task_struct
*task
;
1429 generic_fillattr(inode
, stat
);
1434 task
= pid_task(proc_pid(inode
), PIDTYPE_PID
);
1436 if ((inode
->i_mode
== (S_IFDIR
|S_IRUGO
|S_IXUGO
)) ||
1437 task_dumpable(task
)) {
1438 stat
->uid
= task
->euid
;
1439 stat
->gid
= task
->egid
;
1449 * Exceptional case: normally we are not allowed to unhash a busy
1450 * directory. In this case, however, we can do it - no aliasing problems
1451 * due to the way we treat inodes.
1453 * Rewrite the inode's ownerships here because the owning task may have
1454 * performed a setuid(), etc.
1456 * Before the /proc/pid/status file was created the only way to read
1457 * the effective uid of a /process was to stat /proc/pid. Reading
1458 * /proc/pid/status is slow enough that procps and other packages
1459 * kept stating /proc/pid. To keep the rules in /proc simple I have
1460 * made this apply to all per process world readable and executable
1463 static int pid_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1465 struct inode
*inode
= dentry
->d_inode
;
1466 struct task_struct
*task
= get_proc_task(inode
);
1468 if ((inode
->i_mode
== (S_IFDIR
|S_IRUGO
|S_IXUGO
)) ||
1469 task_dumpable(task
)) {
1470 inode
->i_uid
= task
->euid
;
1471 inode
->i_gid
= task
->egid
;
1476 inode
->i_mode
&= ~(S_ISUID
| S_ISGID
);
1477 security_task_to_inode(task
, inode
);
1478 put_task_struct(task
);
1485 static int pid_delete_dentry(struct dentry
* dentry
)
1487 /* Is the task we represent dead?
1488 * If so, then don't put the dentry on the lru list,
1489 * kill it immediately.
1491 return !proc_pid(dentry
->d_inode
)->tasks
[PIDTYPE_PID
].first
;
1494 static struct dentry_operations pid_dentry_operations
=
1496 .d_revalidate
= pid_revalidate
,
1497 .d_delete
= pid_delete_dentry
,
1502 typedef struct dentry
*instantiate_t(struct inode
*, struct dentry
*,
1503 struct task_struct
*, const void *);
1506 * Fill a directory entry.
1508 * If possible create the dcache entry and derive our inode number and
1509 * file type from dcache entry.
1511 * Since all of the proc inode numbers are dynamically generated, the inode
1512 * numbers do not exist until the inode is cache. This means creating the
1513 * the dcache entry in readdir is necessary to keep the inode numbers
1514 * reported by readdir in sync with the inode numbers reported
1517 static int proc_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
1518 char *name
, int len
,
1519 instantiate_t instantiate
, struct task_struct
*task
, const void *ptr
)
1521 struct dentry
*child
, *dir
= filp
->f_path
.dentry
;
1522 struct inode
*inode
;
1525 unsigned type
= DT_UNKNOWN
;
1529 qname
.hash
= full_name_hash(name
, len
);
1531 child
= d_lookup(dir
, &qname
);
1534 new = d_alloc(dir
, &qname
);
1536 child
= instantiate(dir
->d_inode
, new, task
, ptr
);
1543 if (!child
|| IS_ERR(child
) || !child
->d_inode
)
1544 goto end_instantiate
;
1545 inode
= child
->d_inode
;
1548 type
= inode
->i_mode
>> 12;
1553 ino
= find_inode_number(dir
, &qname
);
1556 return filldir(dirent
, name
, len
, filp
->f_pos
, ino
, type
);
1559 static unsigned name_to_int(struct dentry
*dentry
)
1561 const char *name
= dentry
->d_name
.name
;
1562 int len
= dentry
->d_name
.len
;
1565 if (len
> 1 && *name
== '0')
1568 unsigned c
= *name
++ - '0';
1571 if (n
>= (~0U-9)/10)
1581 #define PROC_FDINFO_MAX 64
1583 static int proc_fd_info(struct inode
*inode
, struct path
*path
, char *info
)
1585 struct task_struct
*task
= get_proc_task(inode
);
1586 struct files_struct
*files
= NULL
;
1588 int fd
= proc_fd(inode
);
1591 files
= get_files_struct(task
);
1592 put_task_struct(task
);
1596 * We are not taking a ref to the file structure, so we must
1599 spin_lock(&files
->file_lock
);
1600 file
= fcheck_files(files
, fd
);
1603 *path
= file
->f_path
;
1604 path_get(&file
->f_path
);
1607 snprintf(info
, PROC_FDINFO_MAX
,
1610 (long long) file
->f_pos
,
1612 spin_unlock(&files
->file_lock
);
1613 put_files_struct(files
);
1616 spin_unlock(&files
->file_lock
);
1617 put_files_struct(files
);
1622 static int proc_fd_link(struct inode
*inode
, struct path
*path
)
1624 return proc_fd_info(inode
, path
, NULL
);
1627 static int tid_fd_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1629 struct inode
*inode
= dentry
->d_inode
;
1630 struct task_struct
*task
= get_proc_task(inode
);
1631 int fd
= proc_fd(inode
);
1632 struct files_struct
*files
;
1635 files
= get_files_struct(task
);
1638 if (fcheck_files(files
, fd
)) {
1640 put_files_struct(files
);
1641 if (task_dumpable(task
)) {
1642 inode
->i_uid
= task
->euid
;
1643 inode
->i_gid
= task
->egid
;
1648 inode
->i_mode
&= ~(S_ISUID
| S_ISGID
);
1649 security_task_to_inode(task
, inode
);
1650 put_task_struct(task
);
1654 put_files_struct(files
);
1656 put_task_struct(task
);
1662 static struct dentry_operations tid_fd_dentry_operations
=
1664 .d_revalidate
= tid_fd_revalidate
,
1665 .d_delete
= pid_delete_dentry
,
1668 static struct dentry
*proc_fd_instantiate(struct inode
*dir
,
1669 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
1671 unsigned fd
= *(const unsigned *)ptr
;
1673 struct files_struct
*files
;
1674 struct inode
*inode
;
1675 struct proc_inode
*ei
;
1676 struct dentry
*error
= ERR_PTR(-ENOENT
);
1678 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
1683 files
= get_files_struct(task
);
1686 inode
->i_mode
= S_IFLNK
;
1689 * We are not taking a ref to the file structure, so we must
1692 spin_lock(&files
->file_lock
);
1693 file
= fcheck_files(files
, fd
);
1696 if (file
->f_mode
& 1)
1697 inode
->i_mode
|= S_IRUSR
| S_IXUSR
;
1698 if (file
->f_mode
& 2)
1699 inode
->i_mode
|= S_IWUSR
| S_IXUSR
;
1700 spin_unlock(&files
->file_lock
);
1701 put_files_struct(files
);
1703 inode
->i_op
= &proc_pid_link_inode_operations
;
1705 ei
->op
.proc_get_link
= proc_fd_link
;
1706 dentry
->d_op
= &tid_fd_dentry_operations
;
1707 d_add(dentry
, inode
);
1708 /* Close the race of the process dying before we return the dentry */
1709 if (tid_fd_revalidate(dentry
, NULL
))
1715 spin_unlock(&files
->file_lock
);
1716 put_files_struct(files
);
1722 static struct dentry
*proc_lookupfd_common(struct inode
*dir
,
1723 struct dentry
*dentry
,
1724 instantiate_t instantiate
)
1726 struct task_struct
*task
= get_proc_task(dir
);
1727 unsigned fd
= name_to_int(dentry
);
1728 struct dentry
*result
= ERR_PTR(-ENOENT
);
1735 result
= instantiate(dir
, dentry
, task
, &fd
);
1737 put_task_struct(task
);
1742 static int proc_readfd_common(struct file
* filp
, void * dirent
,
1743 filldir_t filldir
, instantiate_t instantiate
)
1745 struct dentry
*dentry
= filp
->f_path
.dentry
;
1746 struct inode
*inode
= dentry
->d_inode
;
1747 struct task_struct
*p
= get_proc_task(inode
);
1748 unsigned int fd
, ino
;
1750 struct files_struct
* files
;
1760 if (filldir(dirent
, ".", 1, 0, inode
->i_ino
, DT_DIR
) < 0)
1764 ino
= parent_ino(dentry
);
1765 if (filldir(dirent
, "..", 2, 1, ino
, DT_DIR
) < 0)
1769 files
= get_files_struct(p
);
1773 for (fd
= filp
->f_pos
-2;
1774 fd
< files_fdtable(files
)->max_fds
;
1775 fd
++, filp
->f_pos
++) {
1776 char name
[PROC_NUMBUF
];
1779 if (!fcheck_files(files
, fd
))
1783 len
= snprintf(name
, sizeof(name
), "%d", fd
);
1784 if (proc_fill_cache(filp
, dirent
, filldir
,
1785 name
, len
, instantiate
,
1793 put_files_struct(files
);
1801 static struct dentry
*proc_lookupfd(struct inode
*dir
, struct dentry
*dentry
,
1802 struct nameidata
*nd
)
1804 return proc_lookupfd_common(dir
, dentry
, proc_fd_instantiate
);
1807 static int proc_readfd(struct file
*filp
, void *dirent
, filldir_t filldir
)
1809 return proc_readfd_common(filp
, dirent
, filldir
, proc_fd_instantiate
);
1812 static ssize_t
proc_fdinfo_read(struct file
*file
, char __user
*buf
,
1813 size_t len
, loff_t
*ppos
)
1815 char tmp
[PROC_FDINFO_MAX
];
1816 int err
= proc_fd_info(file
->f_path
.dentry
->d_inode
, NULL
, tmp
);
1818 err
= simple_read_from_buffer(buf
, len
, ppos
, tmp
, strlen(tmp
));
1822 static const struct file_operations proc_fdinfo_file_operations
= {
1823 .open
= nonseekable_open
,
1824 .read
= proc_fdinfo_read
,
1827 static const struct file_operations proc_fd_operations
= {
1828 .read
= generic_read_dir
,
1829 .readdir
= proc_readfd
,
1833 * /proc/pid/fd needs a special permission handler so that a process can still
1834 * access /proc/self/fd after it has executed a setuid().
1836 static int proc_fd_permission(struct inode
*inode
, int mask
,
1837 struct nameidata
*nd
)
1841 rv
= generic_permission(inode
, mask
, NULL
);
1844 if (task_pid(current
) == proc_pid(inode
))
1850 * proc directories can do almost nothing..
1852 static const struct inode_operations proc_fd_inode_operations
= {
1853 .lookup
= proc_lookupfd
,
1854 .permission
= proc_fd_permission
,
1855 .setattr
= proc_setattr
,
1858 static struct dentry
*proc_fdinfo_instantiate(struct inode
*dir
,
1859 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
1861 unsigned fd
= *(unsigned *)ptr
;
1862 struct inode
*inode
;
1863 struct proc_inode
*ei
;
1864 struct dentry
*error
= ERR_PTR(-ENOENT
);
1866 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
1871 inode
->i_mode
= S_IFREG
| S_IRUSR
;
1872 inode
->i_fop
= &proc_fdinfo_file_operations
;
1873 dentry
->d_op
= &tid_fd_dentry_operations
;
1874 d_add(dentry
, inode
);
1875 /* Close the race of the process dying before we return the dentry */
1876 if (tid_fd_revalidate(dentry
, NULL
))
1883 static struct dentry
*proc_lookupfdinfo(struct inode
*dir
,
1884 struct dentry
*dentry
,
1885 struct nameidata
*nd
)
1887 return proc_lookupfd_common(dir
, dentry
, proc_fdinfo_instantiate
);
1890 static int proc_readfdinfo(struct file
*filp
, void *dirent
, filldir_t filldir
)
1892 return proc_readfd_common(filp
, dirent
, filldir
,
1893 proc_fdinfo_instantiate
);
1896 static const struct file_operations proc_fdinfo_operations
= {
1897 .read
= generic_read_dir
,
1898 .readdir
= proc_readfdinfo
,
1902 * proc directories can do almost nothing..
1904 static const struct inode_operations proc_fdinfo_inode_operations
= {
1905 .lookup
= proc_lookupfdinfo
,
1906 .setattr
= proc_setattr
,
1910 static struct dentry
*proc_pident_instantiate(struct inode
*dir
,
1911 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
1913 const struct pid_entry
*p
= ptr
;
1914 struct inode
*inode
;
1915 struct proc_inode
*ei
;
1916 struct dentry
*error
= ERR_PTR(-EINVAL
);
1918 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
1923 inode
->i_mode
= p
->mode
;
1924 if (S_ISDIR(inode
->i_mode
))
1925 inode
->i_nlink
= 2; /* Use getattr to fix if necessary */
1927 inode
->i_op
= p
->iop
;
1929 inode
->i_fop
= p
->fop
;
1931 dentry
->d_op
= &pid_dentry_operations
;
1932 d_add(dentry
, inode
);
1933 /* Close the race of the process dying before we return the dentry */
1934 if (pid_revalidate(dentry
, NULL
))
1940 static struct dentry
*proc_pident_lookup(struct inode
*dir
,
1941 struct dentry
*dentry
,
1942 const struct pid_entry
*ents
,
1945 struct inode
*inode
;
1946 struct dentry
*error
;
1947 struct task_struct
*task
= get_proc_task(dir
);
1948 const struct pid_entry
*p
, *last
;
1950 error
= ERR_PTR(-ENOENT
);
1957 * Yes, it does not scale. And it should not. Don't add
1958 * new entries into /proc/<tgid>/ without very good reasons.
1960 last
= &ents
[nents
- 1];
1961 for (p
= ents
; p
<= last
; p
++) {
1962 if (p
->len
!= dentry
->d_name
.len
)
1964 if (!memcmp(dentry
->d_name
.name
, p
->name
, p
->len
))
1970 error
= proc_pident_instantiate(dir
, dentry
, task
, p
);
1972 put_task_struct(task
);
1977 static int proc_pident_fill_cache(struct file
*filp
, void *dirent
,
1978 filldir_t filldir
, struct task_struct
*task
, const struct pid_entry
*p
)
1980 return proc_fill_cache(filp
, dirent
, filldir
, p
->name
, p
->len
,
1981 proc_pident_instantiate
, task
, p
);
1984 static int proc_pident_readdir(struct file
*filp
,
1985 void *dirent
, filldir_t filldir
,
1986 const struct pid_entry
*ents
, unsigned int nents
)
1989 struct dentry
*dentry
= filp
->f_path
.dentry
;
1990 struct inode
*inode
= dentry
->d_inode
;
1991 struct task_struct
*task
= get_proc_task(inode
);
1992 const struct pid_entry
*p
, *last
;
2005 if (filldir(dirent
, ".", 1, i
, ino
, DT_DIR
) < 0)
2011 ino
= parent_ino(dentry
);
2012 if (filldir(dirent
, "..", 2, i
, ino
, DT_DIR
) < 0)
2024 last
= &ents
[nents
- 1];
2026 if (proc_pident_fill_cache(filp
, dirent
, filldir
, task
, p
) < 0)
2035 put_task_struct(task
);
2040 #ifdef CONFIG_SECURITY
2041 static ssize_t
proc_pid_attr_read(struct file
* file
, char __user
* buf
,
2042 size_t count
, loff_t
*ppos
)
2044 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
2047 struct task_struct
*task
= get_proc_task(inode
);
2052 length
= security_getprocattr(task
,
2053 (char*)file
->f_path
.dentry
->d_name
.name
,
2055 put_task_struct(task
);
2057 length
= simple_read_from_buffer(buf
, count
, ppos
, p
, length
);
2062 static ssize_t
proc_pid_attr_write(struct file
* file
, const char __user
* buf
,
2063 size_t count
, loff_t
*ppos
)
2065 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
2068 struct task_struct
*task
= get_proc_task(inode
);
2073 if (count
> PAGE_SIZE
)
2076 /* No partial writes. */
2082 page
= (char*)__get_free_page(GFP_TEMPORARY
);
2087 if (copy_from_user(page
, buf
, count
))
2090 length
= security_setprocattr(task
,
2091 (char*)file
->f_path
.dentry
->d_name
.name
,
2092 (void*)page
, count
);
2094 free_page((unsigned long) page
);
2096 put_task_struct(task
);
2101 static const struct file_operations proc_pid_attr_operations
= {
2102 .read
= proc_pid_attr_read
,
2103 .write
= proc_pid_attr_write
,
2106 static const struct pid_entry attr_dir_stuff
[] = {
2107 REG("current", S_IRUGO
|S_IWUGO
, pid_attr
),
2108 REG("prev", S_IRUGO
, pid_attr
),
2109 REG("exec", S_IRUGO
|S_IWUGO
, pid_attr
),
2110 REG("fscreate", S_IRUGO
|S_IWUGO
, pid_attr
),
2111 REG("keycreate", S_IRUGO
|S_IWUGO
, pid_attr
),
2112 REG("sockcreate", S_IRUGO
|S_IWUGO
, pid_attr
),
2115 static int proc_attr_dir_readdir(struct file
* filp
,
2116 void * dirent
, filldir_t filldir
)
2118 return proc_pident_readdir(filp
,dirent
,filldir
,
2119 attr_dir_stuff
,ARRAY_SIZE(attr_dir_stuff
));
2122 static const struct file_operations proc_attr_dir_operations
= {
2123 .read
= generic_read_dir
,
2124 .readdir
= proc_attr_dir_readdir
,
2127 static struct dentry
*proc_attr_dir_lookup(struct inode
*dir
,
2128 struct dentry
*dentry
, struct nameidata
*nd
)
2130 return proc_pident_lookup(dir
, dentry
,
2131 attr_dir_stuff
, ARRAY_SIZE(attr_dir_stuff
));
2134 static const struct inode_operations proc_attr_dir_inode_operations
= {
2135 .lookup
= proc_attr_dir_lookup
,
2136 .getattr
= pid_getattr
,
2137 .setattr
= proc_setattr
,
2142 #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2143 static ssize_t
proc_coredump_filter_read(struct file
*file
, char __user
*buf
,
2144 size_t count
, loff_t
*ppos
)
2146 struct task_struct
*task
= get_proc_task(file
->f_dentry
->d_inode
);
2147 struct mm_struct
*mm
;
2148 char buffer
[PROC_NUMBUF
];
2156 mm
= get_task_mm(task
);
2158 len
= snprintf(buffer
, sizeof(buffer
), "%08lx\n",
2159 ((mm
->flags
& MMF_DUMP_FILTER_MASK
) >>
2160 MMF_DUMP_FILTER_SHIFT
));
2162 ret
= simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
2165 put_task_struct(task
);
2170 static ssize_t
proc_coredump_filter_write(struct file
*file
,
2171 const char __user
*buf
,
2175 struct task_struct
*task
;
2176 struct mm_struct
*mm
;
2177 char buffer
[PROC_NUMBUF
], *end
;
2184 memset(buffer
, 0, sizeof(buffer
));
2185 if (count
> sizeof(buffer
) - 1)
2186 count
= sizeof(buffer
) - 1;
2187 if (copy_from_user(buffer
, buf
, count
))
2191 val
= (unsigned int)simple_strtoul(buffer
, &end
, 0);
2194 if (end
- buffer
== 0)
2198 task
= get_proc_task(file
->f_dentry
->d_inode
);
2203 mm
= get_task_mm(task
);
2207 for (i
= 0, mask
= 1; i
< MMF_DUMP_FILTER_BITS
; i
++, mask
<<= 1) {
2209 set_bit(i
+ MMF_DUMP_FILTER_SHIFT
, &mm
->flags
);
2211 clear_bit(i
+ MMF_DUMP_FILTER_SHIFT
, &mm
->flags
);
2216 put_task_struct(task
);
2221 static const struct file_operations proc_coredump_filter_operations
= {
2222 .read
= proc_coredump_filter_read
,
2223 .write
= proc_coredump_filter_write
,
2230 static int proc_self_readlink(struct dentry
*dentry
, char __user
*buffer
,
2233 struct pid_namespace
*ns
= dentry
->d_sb
->s_fs_info
;
2234 pid_t tgid
= task_tgid_nr_ns(current
, ns
);
2235 char tmp
[PROC_NUMBUF
];
2238 sprintf(tmp
, "%d", tgid
);
2239 return vfs_readlink(dentry
,buffer
,buflen
,tmp
);
2242 static void *proc_self_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
2244 struct pid_namespace
*ns
= dentry
->d_sb
->s_fs_info
;
2245 pid_t tgid
= task_tgid_nr_ns(current
, ns
);
2246 char tmp
[PROC_NUMBUF
];
2248 return ERR_PTR(-ENOENT
);
2249 sprintf(tmp
, "%d", task_tgid_nr_ns(current
, ns
));
2250 return ERR_PTR(vfs_follow_link(nd
,tmp
));
2253 static const struct inode_operations proc_self_inode_operations
= {
2254 .readlink
= proc_self_readlink
,
2255 .follow_link
= proc_self_follow_link
,
2261 * These are the directory entries in the root directory of /proc
2262 * that properly belong to the /proc filesystem, as they describe
2263 * describe something that is process related.
2265 static const struct pid_entry proc_base_stuff
[] = {
2266 NOD("self", S_IFLNK
|S_IRWXUGO
,
2267 &proc_self_inode_operations
, NULL
, {}),
2271 * Exceptional case: normally we are not allowed to unhash a busy
2272 * directory. In this case, however, we can do it - no aliasing problems
2273 * due to the way we treat inodes.
2275 static int proc_base_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
2277 struct inode
*inode
= dentry
->d_inode
;
2278 struct task_struct
*task
= get_proc_task(inode
);
2280 put_task_struct(task
);
2287 static struct dentry_operations proc_base_dentry_operations
=
2289 .d_revalidate
= proc_base_revalidate
,
2290 .d_delete
= pid_delete_dentry
,
2293 static struct dentry
*proc_base_instantiate(struct inode
*dir
,
2294 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
2296 const struct pid_entry
*p
= ptr
;
2297 struct inode
*inode
;
2298 struct proc_inode
*ei
;
2299 struct dentry
*error
= ERR_PTR(-EINVAL
);
2301 /* Allocate the inode */
2302 error
= ERR_PTR(-ENOMEM
);
2303 inode
= new_inode(dir
->i_sb
);
2307 /* Initialize the inode */
2309 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
2312 * grab the reference to the task.
2314 ei
->pid
= get_task_pid(task
, PIDTYPE_PID
);
2320 inode
->i_mode
= p
->mode
;
2321 if (S_ISDIR(inode
->i_mode
))
2323 if (S_ISLNK(inode
->i_mode
))
2326 inode
->i_op
= p
->iop
;
2328 inode
->i_fop
= p
->fop
;
2330 dentry
->d_op
= &proc_base_dentry_operations
;
2331 d_add(dentry
, inode
);
2340 static struct dentry
*proc_base_lookup(struct inode
*dir
, struct dentry
*dentry
)
2342 struct dentry
*error
;
2343 struct task_struct
*task
= get_proc_task(dir
);
2344 const struct pid_entry
*p
, *last
;
2346 error
= ERR_PTR(-ENOENT
);
2351 /* Lookup the directory entry */
2352 last
= &proc_base_stuff
[ARRAY_SIZE(proc_base_stuff
) - 1];
2353 for (p
= proc_base_stuff
; p
<= last
; p
++) {
2354 if (p
->len
!= dentry
->d_name
.len
)
2356 if (!memcmp(dentry
->d_name
.name
, p
->name
, p
->len
))
2362 error
= proc_base_instantiate(dir
, dentry
, task
, p
);
2365 put_task_struct(task
);
2370 static int proc_base_fill_cache(struct file
*filp
, void *dirent
,
2371 filldir_t filldir
, struct task_struct
*task
, const struct pid_entry
*p
)
2373 return proc_fill_cache(filp
, dirent
, filldir
, p
->name
, p
->len
,
2374 proc_base_instantiate
, task
, p
);
2377 #ifdef CONFIG_TASK_IO_ACCOUNTING
2378 static int proc_pid_io_accounting(struct task_struct
*task
, char *buffer
)
2380 return sprintf(buffer
,
2381 #ifdef CONFIG_TASK_XACCT
2387 "read_bytes: %llu\n"
2388 "write_bytes: %llu\n"
2389 "cancelled_write_bytes: %llu\n",
2390 #ifdef CONFIG_TASK_XACCT
2391 (unsigned long long)task
->rchar
,
2392 (unsigned long long)task
->wchar
,
2393 (unsigned long long)task
->syscr
,
2394 (unsigned long long)task
->syscw
,
2396 (unsigned long long)task
->ioac
.read_bytes
,
2397 (unsigned long long)task
->ioac
.write_bytes
,
2398 (unsigned long long)task
->ioac
.cancelled_write_bytes
);
2405 static const struct file_operations proc_task_operations
;
2406 static const struct inode_operations proc_task_inode_operations
;
2408 static const struct pid_entry tgid_base_stuff
[] = {
2409 DIR("task", S_IRUGO
|S_IXUGO
, task
),
2410 DIR("fd", S_IRUSR
|S_IXUSR
, fd
),
2411 DIR("fdinfo", S_IRUSR
|S_IXUSR
, fdinfo
),
2413 DIR("net", S_IRUGO
|S_IXUGO
, net
),
2415 REG("environ", S_IRUSR
, environ
),
2416 INF("auxv", S_IRUSR
, pid_auxv
),
2417 ONE("status", S_IRUGO
, pid_status
),
2418 INF("limits", S_IRUSR
, pid_limits
),
2419 #ifdef CONFIG_SCHED_DEBUG
2420 REG("sched", S_IRUGO
|S_IWUSR
, pid_sched
),
2422 INF("cmdline", S_IRUGO
, pid_cmdline
),
2423 ONE("stat", S_IRUGO
, tgid_stat
),
2424 ONE("statm", S_IRUGO
, pid_statm
),
2425 REG("maps", S_IRUGO
, maps
),
2427 REG("numa_maps", S_IRUGO
, numa_maps
),
2429 REG("mem", S_IRUSR
|S_IWUSR
, mem
),
2433 REG("mounts", S_IRUGO
, mounts
),
2434 REG("mountinfo", S_IRUGO
, mountinfo
),
2435 REG("mountstats", S_IRUSR
, mountstats
),
2436 #ifdef CONFIG_PROC_PAGE_MONITOR
2437 REG("clear_refs", S_IWUSR
, clear_refs
),
2438 REG("smaps", S_IRUGO
, smaps
),
2439 REG("pagemap", S_IRUSR
, pagemap
),
2441 #ifdef CONFIG_SECURITY
2442 DIR("attr", S_IRUGO
|S_IXUGO
, attr_dir
),
2444 #ifdef CONFIG_KALLSYMS
2445 INF("wchan", S_IRUGO
, pid_wchan
),
2447 #ifdef CONFIG_SCHEDSTATS
2448 INF("schedstat", S_IRUGO
, pid_schedstat
),
2450 #ifdef CONFIG_LATENCYTOP
2451 REG("latency", S_IRUGO
, lstats
),
2453 #ifdef CONFIG_PROC_PID_CPUSET
2454 REG("cpuset", S_IRUGO
, cpuset
),
2456 #ifdef CONFIG_CGROUPS
2457 REG("cgroup", S_IRUGO
, cgroup
),
2459 INF("oom_score", S_IRUGO
, oom_score
),
2460 REG("oom_adj", S_IRUGO
|S_IWUSR
, oom_adjust
),
2461 #ifdef CONFIG_AUDITSYSCALL
2462 REG("loginuid", S_IWUSR
|S_IRUGO
, loginuid
),
2463 REG("sessionid", S_IRUGO
, sessionid
),
2465 #ifdef CONFIG_FAULT_INJECTION
2466 REG("make-it-fail", S_IRUGO
|S_IWUSR
, fault_inject
),
2468 #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2469 REG("coredump_filter", S_IRUGO
|S_IWUSR
, coredump_filter
),
2471 #ifdef CONFIG_TASK_IO_ACCOUNTING
2472 INF("io", S_IRUGO
, pid_io_accounting
),
2476 static int proc_tgid_base_readdir(struct file
* filp
,
2477 void * dirent
, filldir_t filldir
)
2479 return proc_pident_readdir(filp
,dirent
,filldir
,
2480 tgid_base_stuff
,ARRAY_SIZE(tgid_base_stuff
));
2483 static const struct file_operations proc_tgid_base_operations
= {
2484 .read
= generic_read_dir
,
2485 .readdir
= proc_tgid_base_readdir
,
2488 static struct dentry
*proc_tgid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
){
2489 return proc_pident_lookup(dir
, dentry
,
2490 tgid_base_stuff
, ARRAY_SIZE(tgid_base_stuff
));
2493 static const struct inode_operations proc_tgid_base_inode_operations
= {
2494 .lookup
= proc_tgid_base_lookup
,
2495 .getattr
= pid_getattr
,
2496 .setattr
= proc_setattr
,
2499 static void proc_flush_task_mnt(struct vfsmount
*mnt
, pid_t pid
, pid_t tgid
)
2501 struct dentry
*dentry
, *leader
, *dir
;
2502 char buf
[PROC_NUMBUF
];
2506 name
.len
= snprintf(buf
, sizeof(buf
), "%d", pid
);
2507 dentry
= d_hash_and_lookup(mnt
->mnt_root
, &name
);
2509 if (!(current
->flags
& PF_EXITING
))
2510 shrink_dcache_parent(dentry
);
2519 name
.len
= snprintf(buf
, sizeof(buf
), "%d", tgid
);
2520 leader
= d_hash_and_lookup(mnt
->mnt_root
, &name
);
2525 name
.len
= strlen(name
.name
);
2526 dir
= d_hash_and_lookup(leader
, &name
);
2528 goto out_put_leader
;
2531 name
.len
= snprintf(buf
, sizeof(buf
), "%d", pid
);
2532 dentry
= d_hash_and_lookup(dir
, &name
);
2534 shrink_dcache_parent(dentry
);
2547 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2548 * @task: task that should be flushed.
2550 * When flushing dentries from proc, one needs to flush them from global
2551 * proc (proc_mnt) and from all the namespaces' procs this task was seen
2552 * in. This call is supposed to do all of this job.
2554 * Looks in the dcache for
2556 * /proc/@tgid/task/@pid
2557 * if either directory is present flushes it and all of it'ts children
2560 * It is safe and reasonable to cache /proc entries for a task until
2561 * that task exits. After that they just clog up the dcache with
2562 * useless entries, possibly causing useful dcache entries to be
2563 * flushed instead. This routine is proved to flush those useless
2564 * dcache entries at process exit time.
2566 * NOTE: This routine is just an optimization so it does not guarantee
2567 * that no dcache entries will exist at process exit time it
2568 * just makes it very unlikely that any will persist.
2571 void proc_flush_task(struct task_struct
*task
)
2574 struct pid
*pid
, *tgid
= NULL
;
2577 pid
= task_pid(task
);
2578 if (thread_group_leader(task
))
2579 tgid
= task_tgid(task
);
2581 for (i
= 0; i
<= pid
->level
; i
++) {
2582 upid
= &pid
->numbers
[i
];
2583 proc_flush_task_mnt(upid
->ns
->proc_mnt
, upid
->nr
,
2584 tgid
? tgid
->numbers
[i
].nr
: 0);
2587 upid
= &pid
->numbers
[pid
->level
];
2589 pid_ns_release_proc(upid
->ns
);
2592 static struct dentry
*proc_pid_instantiate(struct inode
*dir
,
2593 struct dentry
* dentry
,
2594 struct task_struct
*task
, const void *ptr
)
2596 struct dentry
*error
= ERR_PTR(-ENOENT
);
2597 struct inode
*inode
;
2599 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
2603 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
2604 inode
->i_op
= &proc_tgid_base_inode_operations
;
2605 inode
->i_fop
= &proc_tgid_base_operations
;
2606 inode
->i_flags
|=S_IMMUTABLE
;
2608 inode
->i_nlink
= 2 + pid_entry_count_dirs(tgid_base_stuff
,
2609 ARRAY_SIZE(tgid_base_stuff
));
2611 dentry
->d_op
= &pid_dentry_operations
;
2613 d_add(dentry
, inode
);
2614 /* Close the race of the process dying before we return the dentry */
2615 if (pid_revalidate(dentry
, NULL
))
2621 struct dentry
*proc_pid_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
)
2623 struct dentry
*result
= ERR_PTR(-ENOENT
);
2624 struct task_struct
*task
;
2626 struct pid_namespace
*ns
;
2628 result
= proc_base_lookup(dir
, dentry
);
2629 if (!IS_ERR(result
) || PTR_ERR(result
) != -ENOENT
)
2632 tgid
= name_to_int(dentry
);
2636 ns
= dentry
->d_sb
->s_fs_info
;
2638 task
= find_task_by_pid_ns(tgid
, ns
);
2640 get_task_struct(task
);
2645 result
= proc_pid_instantiate(dir
, dentry
, task
, NULL
);
2646 put_task_struct(task
);
2652 * Find the first task with tgid >= tgid
2657 struct task_struct
*task
;
2659 static struct tgid_iter
next_tgid(struct pid_namespace
*ns
, struct tgid_iter iter
)
2664 put_task_struct(iter
.task
);
2668 pid
= find_ge_pid(iter
.tgid
, ns
);
2670 iter
.tgid
= pid_nr_ns(pid
, ns
);
2671 iter
.task
= pid_task(pid
, PIDTYPE_PID
);
2672 /* What we to know is if the pid we have find is the
2673 * pid of a thread_group_leader. Testing for task
2674 * being a thread_group_leader is the obvious thing
2675 * todo but there is a window when it fails, due to
2676 * the pid transfer logic in de_thread.
2678 * So we perform the straight forward test of seeing
2679 * if the pid we have found is the pid of a thread
2680 * group leader, and don't worry if the task we have
2681 * found doesn't happen to be a thread group leader.
2682 * As we don't care in the case of readdir.
2684 if (!iter
.task
|| !has_group_leader_pid(iter
.task
)) {
2688 get_task_struct(iter
.task
);
2694 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
2696 static int proc_pid_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
2697 struct tgid_iter iter
)
2699 char name
[PROC_NUMBUF
];
2700 int len
= snprintf(name
, sizeof(name
), "%d", iter
.tgid
);
2701 return proc_fill_cache(filp
, dirent
, filldir
, name
, len
,
2702 proc_pid_instantiate
, iter
.task
, NULL
);
2705 /* for the /proc/ directory itself, after non-process stuff has been done */
2706 int proc_pid_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
2708 unsigned int nr
= filp
->f_pos
- FIRST_PROCESS_ENTRY
;
2709 struct task_struct
*reaper
= get_proc_task(filp
->f_path
.dentry
->d_inode
);
2710 struct tgid_iter iter
;
2711 struct pid_namespace
*ns
;
2716 for (; nr
< ARRAY_SIZE(proc_base_stuff
); filp
->f_pos
++, nr
++) {
2717 const struct pid_entry
*p
= &proc_base_stuff
[nr
];
2718 if (proc_base_fill_cache(filp
, dirent
, filldir
, reaper
, p
) < 0)
2722 ns
= filp
->f_dentry
->d_sb
->s_fs_info
;
2724 iter
.tgid
= filp
->f_pos
- TGID_OFFSET
;
2725 for (iter
= next_tgid(ns
, iter
);
2727 iter
.tgid
+= 1, iter
= next_tgid(ns
, iter
)) {
2728 filp
->f_pos
= iter
.tgid
+ TGID_OFFSET
;
2729 if (proc_pid_fill_cache(filp
, dirent
, filldir
, iter
) < 0) {
2730 put_task_struct(iter
.task
);
2734 filp
->f_pos
= PID_MAX_LIMIT
+ TGID_OFFSET
;
2736 put_task_struct(reaper
);
2744 static const struct pid_entry tid_base_stuff
[] = {
2745 DIR("fd", S_IRUSR
|S_IXUSR
, fd
),
2746 DIR("fdinfo", S_IRUSR
|S_IXUSR
, fdinfo
),
2747 REG("environ", S_IRUSR
, environ
),
2748 INF("auxv", S_IRUSR
, pid_auxv
),
2749 ONE("status", S_IRUGO
, pid_status
),
2750 INF("limits", S_IRUSR
, pid_limits
),
2751 #ifdef CONFIG_SCHED_DEBUG
2752 REG("sched", S_IRUGO
|S_IWUSR
, pid_sched
),
2754 INF("cmdline", S_IRUGO
, pid_cmdline
),
2755 ONE("stat", S_IRUGO
, tid_stat
),
2756 ONE("statm", S_IRUGO
, pid_statm
),
2757 REG("maps", S_IRUGO
, maps
),
2759 REG("numa_maps", S_IRUGO
, numa_maps
),
2761 REG("mem", S_IRUSR
|S_IWUSR
, mem
),
2765 REG("mounts", S_IRUGO
, mounts
),
2766 REG("mountinfo", S_IRUGO
, mountinfo
),
2767 #ifdef CONFIG_PROC_PAGE_MONITOR
2768 REG("clear_refs", S_IWUSR
, clear_refs
),
2769 REG("smaps", S_IRUGO
, smaps
),
2770 REG("pagemap", S_IRUSR
, pagemap
),
2772 #ifdef CONFIG_SECURITY
2773 DIR("attr", S_IRUGO
|S_IXUGO
, attr_dir
),
2775 #ifdef CONFIG_KALLSYMS
2776 INF("wchan", S_IRUGO
, pid_wchan
),
2778 #ifdef CONFIG_SCHEDSTATS
2779 INF("schedstat", S_IRUGO
, pid_schedstat
),
2781 #ifdef CONFIG_LATENCYTOP
2782 REG("latency", S_IRUGO
, lstats
),
2784 #ifdef CONFIG_PROC_PID_CPUSET
2785 REG("cpuset", S_IRUGO
, cpuset
),
2787 #ifdef CONFIG_CGROUPS
2788 REG("cgroup", S_IRUGO
, cgroup
),
2790 INF("oom_score", S_IRUGO
, oom_score
),
2791 REG("oom_adj", S_IRUGO
|S_IWUSR
, oom_adjust
),
2792 #ifdef CONFIG_AUDITSYSCALL
2793 REG("loginuid", S_IWUSR
|S_IRUGO
, loginuid
),
2794 REG("sessionid", S_IRUSR
, sessionid
),
2796 #ifdef CONFIG_FAULT_INJECTION
2797 REG("make-it-fail", S_IRUGO
|S_IWUSR
, fault_inject
),
2801 static int proc_tid_base_readdir(struct file
* filp
,
2802 void * dirent
, filldir_t filldir
)
2804 return proc_pident_readdir(filp
,dirent
,filldir
,
2805 tid_base_stuff
,ARRAY_SIZE(tid_base_stuff
));
2808 static struct dentry
*proc_tid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
){
2809 return proc_pident_lookup(dir
, dentry
,
2810 tid_base_stuff
, ARRAY_SIZE(tid_base_stuff
));
2813 static const struct file_operations proc_tid_base_operations
= {
2814 .read
= generic_read_dir
,
2815 .readdir
= proc_tid_base_readdir
,
2818 static const struct inode_operations proc_tid_base_inode_operations
= {
2819 .lookup
= proc_tid_base_lookup
,
2820 .getattr
= pid_getattr
,
2821 .setattr
= proc_setattr
,
2824 static struct dentry
*proc_task_instantiate(struct inode
*dir
,
2825 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
2827 struct dentry
*error
= ERR_PTR(-ENOENT
);
2828 struct inode
*inode
;
2829 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
2833 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
2834 inode
->i_op
= &proc_tid_base_inode_operations
;
2835 inode
->i_fop
= &proc_tid_base_operations
;
2836 inode
->i_flags
|=S_IMMUTABLE
;
2838 inode
->i_nlink
= 2 + pid_entry_count_dirs(tid_base_stuff
,
2839 ARRAY_SIZE(tid_base_stuff
));
2841 dentry
->d_op
= &pid_dentry_operations
;
2843 d_add(dentry
, inode
);
2844 /* Close the race of the process dying before we return the dentry */
2845 if (pid_revalidate(dentry
, NULL
))
2851 static struct dentry
*proc_task_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
)
2853 struct dentry
*result
= ERR_PTR(-ENOENT
);
2854 struct task_struct
*task
;
2855 struct task_struct
*leader
= get_proc_task(dir
);
2857 struct pid_namespace
*ns
;
2862 tid
= name_to_int(dentry
);
2866 ns
= dentry
->d_sb
->s_fs_info
;
2868 task
= find_task_by_pid_ns(tid
, ns
);
2870 get_task_struct(task
);
2874 if (!same_thread_group(leader
, task
))
2877 result
= proc_task_instantiate(dir
, dentry
, task
, NULL
);
2879 put_task_struct(task
);
2881 put_task_struct(leader
);
2887 * Find the first tid of a thread group to return to user space.
2889 * Usually this is just the thread group leader, but if the users
2890 * buffer was too small or there was a seek into the middle of the
2891 * directory we have more work todo.
2893 * In the case of a short read we start with find_task_by_pid.
2895 * In the case of a seek we start with the leader and walk nr
2898 static struct task_struct
*first_tid(struct task_struct
*leader
,
2899 int tid
, int nr
, struct pid_namespace
*ns
)
2901 struct task_struct
*pos
;
2904 /* Attempt to start with the pid of a thread */
2905 if (tid
&& (nr
> 0)) {
2906 pos
= find_task_by_pid_ns(tid
, ns
);
2907 if (pos
&& (pos
->group_leader
== leader
))
2911 /* If nr exceeds the number of threads there is nothing todo */
2913 if (nr
&& nr
>= get_nr_threads(leader
))
2916 /* If we haven't found our starting place yet start
2917 * with the leader and walk nr threads forward.
2919 for (pos
= leader
; nr
> 0; --nr
) {
2920 pos
= next_thread(pos
);
2921 if (pos
== leader
) {
2927 get_task_struct(pos
);
2934 * Find the next thread in the thread list.
2935 * Return NULL if there is an error or no next thread.
2937 * The reference to the input task_struct is released.
2939 static struct task_struct
*next_tid(struct task_struct
*start
)
2941 struct task_struct
*pos
= NULL
;
2943 if (pid_alive(start
)) {
2944 pos
= next_thread(start
);
2945 if (thread_group_leader(pos
))
2948 get_task_struct(pos
);
2951 put_task_struct(start
);
2955 static int proc_task_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
2956 struct task_struct
*task
, int tid
)
2958 char name
[PROC_NUMBUF
];
2959 int len
= snprintf(name
, sizeof(name
), "%d", tid
);
2960 return proc_fill_cache(filp
, dirent
, filldir
, name
, len
,
2961 proc_task_instantiate
, task
, NULL
);
2964 /* for the /proc/TGID/task/ directories */
2965 static int proc_task_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
2967 struct dentry
*dentry
= filp
->f_path
.dentry
;
2968 struct inode
*inode
= dentry
->d_inode
;
2969 struct task_struct
*leader
= NULL
;
2970 struct task_struct
*task
;
2971 int retval
= -ENOENT
;
2974 unsigned long pos
= filp
->f_pos
; /* avoiding "long long" filp->f_pos */
2975 struct pid_namespace
*ns
;
2977 task
= get_proc_task(inode
);
2981 if (pid_alive(task
)) {
2982 leader
= task
->group_leader
;
2983 get_task_struct(leader
);
2986 put_task_struct(task
);
2994 if (filldir(dirent
, ".", 1, pos
, ino
, DT_DIR
) < 0)
2999 ino
= parent_ino(dentry
);
3000 if (filldir(dirent
, "..", 2, pos
, ino
, DT_DIR
) < 0)
3006 /* f_version caches the tgid value that the last readdir call couldn't
3007 * return. lseek aka telldir automagically resets f_version to 0.
3009 ns
= filp
->f_dentry
->d_sb
->s_fs_info
;
3010 tid
= (int)filp
->f_version
;
3011 filp
->f_version
= 0;
3012 for (task
= first_tid(leader
, tid
, pos
- 2, ns
);
3014 task
= next_tid(task
), pos
++) {
3015 tid
= task_pid_nr_ns(task
, ns
);
3016 if (proc_task_fill_cache(filp
, dirent
, filldir
, task
, tid
) < 0) {
3017 /* returning this tgid failed, save it as the first
3018 * pid for the next readir call */
3019 filp
->f_version
= (u64
)tid
;
3020 put_task_struct(task
);
3026 put_task_struct(leader
);
3031 static int proc_task_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
3033 struct inode
*inode
= dentry
->d_inode
;
3034 struct task_struct
*p
= get_proc_task(inode
);
3035 generic_fillattr(inode
, stat
);
3039 stat
->nlink
+= get_nr_threads(p
);
3047 static const struct inode_operations proc_task_inode_operations
= {
3048 .lookup
= proc_task_lookup
,
3049 .getattr
= proc_task_getattr
,
3050 .setattr
= proc_setattr
,
3053 static const struct file_operations proc_task_operations
= {
3054 .read
= generic_read_dir
,
3055 .readdir
= proc_task_readdir
,