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/config.h>
53 #include <linux/errno.h>
54 #include <linux/time.h>
55 #include <linux/proc_fs.h>
56 #include <linux/stat.h>
57 #include <linux/init.h>
58 #include <linux/capability.h>
59 #include <linux/file.h>
60 #include <linux/string.h>
61 #include <linux/seq_file.h>
62 #include <linux/namei.h>
63 #include <linux/namespace.h>
65 #include <linux/smp_lock.h>
66 #include <linux/rcupdate.h>
67 #include <linux/kallsyms.h>
68 #include <linux/mount.h>
69 #include <linux/security.h>
70 #include <linux/ptrace.h>
71 #include <linux/seccomp.h>
72 #include <linux/cpuset.h>
73 #include <linux/audit.h>
74 #include <linux/poll.h>
78 * For hysterical raisins we keep the same inumbers as in the old procfs.
79 * Feel free to change the macro below - just keep the range distinct from
80 * inumbers of the rest of procfs (currently those are in 0x0000--0xffff).
81 * As soon as we'll get a separate superblock we will be able to forget
82 * about magical ranges too.
85 #define fake_ino(pid,ino) (((pid)<<16)|(ino))
87 enum pid_directory_inos
{
107 PROC_TGID_MOUNTSTATS
,
112 #ifdef CONFIG_SCHEDSTATS
115 #ifdef CONFIG_CPUSETS
118 #ifdef CONFIG_SECURITY
120 PROC_TGID_ATTR_CURRENT
,
123 PROC_TGID_ATTR_FSCREATE
,
125 #ifdef CONFIG_AUDITSYSCALL
129 PROC_TGID_OOM_ADJUST
,
133 #ifdef CONFIG_SECCOMP
153 #ifdef CONFIG_SCHEDSTATS
156 #ifdef CONFIG_CPUSETS
159 #ifdef CONFIG_SECURITY
161 PROC_TID_ATTR_CURRENT
,
164 PROC_TID_ATTR_FSCREATE
,
166 #ifdef CONFIG_AUDITSYSCALL
172 /* Add new entries before this */
173 PROC_TID_FD_DIR
= 0x8000, /* 0x8000-0xffff */
183 #define E(type,name,mode) {(type),sizeof(name)-1,(name),(mode)}
185 static struct pid_entry tgid_base_stuff
[] = {
186 E(PROC_TGID_TASK
, "task", S_IFDIR
|S_IRUGO
|S_IXUGO
),
187 E(PROC_TGID_FD
, "fd", S_IFDIR
|S_IRUSR
|S_IXUSR
),
188 E(PROC_TGID_ENVIRON
, "environ", S_IFREG
|S_IRUSR
),
189 E(PROC_TGID_AUXV
, "auxv", S_IFREG
|S_IRUSR
),
190 E(PROC_TGID_STATUS
, "status", S_IFREG
|S_IRUGO
),
191 E(PROC_TGID_CMDLINE
, "cmdline", S_IFREG
|S_IRUGO
),
192 E(PROC_TGID_STAT
, "stat", S_IFREG
|S_IRUGO
),
193 E(PROC_TGID_STATM
, "statm", S_IFREG
|S_IRUGO
),
194 E(PROC_TGID_MAPS
, "maps", S_IFREG
|S_IRUGO
),
196 E(PROC_TGID_NUMA_MAPS
, "numa_maps", S_IFREG
|S_IRUGO
),
198 E(PROC_TGID_MEM
, "mem", S_IFREG
|S_IRUSR
|S_IWUSR
),
199 #ifdef CONFIG_SECCOMP
200 E(PROC_TGID_SECCOMP
, "seccomp", S_IFREG
|S_IRUSR
|S_IWUSR
),
202 E(PROC_TGID_CWD
, "cwd", S_IFLNK
|S_IRWXUGO
),
203 E(PROC_TGID_ROOT
, "root", S_IFLNK
|S_IRWXUGO
),
204 E(PROC_TGID_EXE
, "exe", S_IFLNK
|S_IRWXUGO
),
205 E(PROC_TGID_MOUNTS
, "mounts", S_IFREG
|S_IRUGO
),
206 E(PROC_TGID_MOUNTSTATS
, "mountstats", S_IFREG
|S_IRUSR
),
208 E(PROC_TGID_SMAPS
, "smaps", S_IFREG
|S_IRUGO
),
210 #ifdef CONFIG_SECURITY
211 E(PROC_TGID_ATTR
, "attr", S_IFDIR
|S_IRUGO
|S_IXUGO
),
213 #ifdef CONFIG_KALLSYMS
214 E(PROC_TGID_WCHAN
, "wchan", S_IFREG
|S_IRUGO
),
216 #ifdef CONFIG_SCHEDSTATS
217 E(PROC_TGID_SCHEDSTAT
, "schedstat", S_IFREG
|S_IRUGO
),
219 #ifdef CONFIG_CPUSETS
220 E(PROC_TGID_CPUSET
, "cpuset", S_IFREG
|S_IRUGO
),
222 E(PROC_TGID_OOM_SCORE
, "oom_score",S_IFREG
|S_IRUGO
),
223 E(PROC_TGID_OOM_ADJUST
,"oom_adj", S_IFREG
|S_IRUGO
|S_IWUSR
),
224 #ifdef CONFIG_AUDITSYSCALL
225 E(PROC_TGID_LOGINUID
, "loginuid", S_IFREG
|S_IWUSR
|S_IRUGO
),
229 static struct pid_entry tid_base_stuff
[] = {
230 E(PROC_TID_FD
, "fd", S_IFDIR
|S_IRUSR
|S_IXUSR
),
231 E(PROC_TID_ENVIRON
, "environ", S_IFREG
|S_IRUSR
),
232 E(PROC_TID_AUXV
, "auxv", S_IFREG
|S_IRUSR
),
233 E(PROC_TID_STATUS
, "status", S_IFREG
|S_IRUGO
),
234 E(PROC_TID_CMDLINE
, "cmdline", S_IFREG
|S_IRUGO
),
235 E(PROC_TID_STAT
, "stat", S_IFREG
|S_IRUGO
),
236 E(PROC_TID_STATM
, "statm", S_IFREG
|S_IRUGO
),
237 E(PROC_TID_MAPS
, "maps", S_IFREG
|S_IRUGO
),
239 E(PROC_TID_NUMA_MAPS
, "numa_maps", S_IFREG
|S_IRUGO
),
241 E(PROC_TID_MEM
, "mem", S_IFREG
|S_IRUSR
|S_IWUSR
),
242 #ifdef CONFIG_SECCOMP
243 E(PROC_TID_SECCOMP
, "seccomp", S_IFREG
|S_IRUSR
|S_IWUSR
),
245 E(PROC_TID_CWD
, "cwd", S_IFLNK
|S_IRWXUGO
),
246 E(PROC_TID_ROOT
, "root", S_IFLNK
|S_IRWXUGO
),
247 E(PROC_TID_EXE
, "exe", S_IFLNK
|S_IRWXUGO
),
248 E(PROC_TID_MOUNTS
, "mounts", S_IFREG
|S_IRUGO
),
250 E(PROC_TID_SMAPS
, "smaps", S_IFREG
|S_IRUGO
),
252 #ifdef CONFIG_SECURITY
253 E(PROC_TID_ATTR
, "attr", S_IFDIR
|S_IRUGO
|S_IXUGO
),
255 #ifdef CONFIG_KALLSYMS
256 E(PROC_TID_WCHAN
, "wchan", S_IFREG
|S_IRUGO
),
258 #ifdef CONFIG_SCHEDSTATS
259 E(PROC_TID_SCHEDSTAT
, "schedstat",S_IFREG
|S_IRUGO
),
261 #ifdef CONFIG_CPUSETS
262 E(PROC_TID_CPUSET
, "cpuset", S_IFREG
|S_IRUGO
),
264 E(PROC_TID_OOM_SCORE
, "oom_score",S_IFREG
|S_IRUGO
),
265 E(PROC_TID_OOM_ADJUST
, "oom_adj", S_IFREG
|S_IRUGO
|S_IWUSR
),
266 #ifdef CONFIG_AUDITSYSCALL
267 E(PROC_TID_LOGINUID
, "loginuid", S_IFREG
|S_IWUSR
|S_IRUGO
),
272 #ifdef CONFIG_SECURITY
273 static struct pid_entry tgid_attr_stuff
[] = {
274 E(PROC_TGID_ATTR_CURRENT
, "current", S_IFREG
|S_IRUGO
|S_IWUGO
),
275 E(PROC_TGID_ATTR_PREV
, "prev", S_IFREG
|S_IRUGO
),
276 E(PROC_TGID_ATTR_EXEC
, "exec", S_IFREG
|S_IRUGO
|S_IWUGO
),
277 E(PROC_TGID_ATTR_FSCREATE
, "fscreate", S_IFREG
|S_IRUGO
|S_IWUGO
),
280 static struct pid_entry tid_attr_stuff
[] = {
281 E(PROC_TID_ATTR_CURRENT
, "current", S_IFREG
|S_IRUGO
|S_IWUGO
),
282 E(PROC_TID_ATTR_PREV
, "prev", S_IFREG
|S_IRUGO
),
283 E(PROC_TID_ATTR_EXEC
, "exec", S_IFREG
|S_IRUGO
|S_IWUGO
),
284 E(PROC_TID_ATTR_FSCREATE
, "fscreate", S_IFREG
|S_IRUGO
|S_IWUGO
),
291 static int proc_fd_link(struct inode
*inode
, struct dentry
**dentry
, struct vfsmount
**mnt
)
293 struct task_struct
*task
= proc_task(inode
);
294 struct files_struct
*files
;
296 int fd
= proc_type(inode
) - PROC_TID_FD_DIR
;
298 files
= get_files_struct(task
);
301 file
= fcheck_files(files
, fd
);
303 *mnt
= mntget(file
->f_vfsmnt
);
304 *dentry
= dget(file
->f_dentry
);
306 put_files_struct(files
);
310 put_files_struct(files
);
315 static struct fs_struct
*get_fs_struct(struct task_struct
*task
)
317 struct fs_struct
*fs
;
321 atomic_inc(&fs
->count
);
326 static int proc_cwd_link(struct inode
*inode
, struct dentry
**dentry
, struct vfsmount
**mnt
)
328 struct fs_struct
*fs
= get_fs_struct(proc_task(inode
));
329 int result
= -ENOENT
;
331 read_lock(&fs
->lock
);
332 *mnt
= mntget(fs
->pwdmnt
);
333 *dentry
= dget(fs
->pwd
);
334 read_unlock(&fs
->lock
);
341 static int proc_root_link(struct inode
*inode
, struct dentry
**dentry
, struct vfsmount
**mnt
)
343 struct fs_struct
*fs
= get_fs_struct(proc_task(inode
));
344 int result
= -ENOENT
;
346 read_lock(&fs
->lock
);
347 *mnt
= mntget(fs
->rootmnt
);
348 *dentry
= dget(fs
->root
);
349 read_unlock(&fs
->lock
);
357 /* Same as proc_root_link, but this addionally tries to get fs from other
358 * threads in the group */
359 static int proc_task_root_link(struct inode
*inode
, struct dentry
**dentry
,
360 struct vfsmount
**mnt
)
362 struct fs_struct
*fs
;
363 int result
= -ENOENT
;
364 struct task_struct
*leader
= proc_task(inode
);
369 atomic_inc(&fs
->count
);
372 /* Try to get fs from other threads */
374 read_lock(&tasklist_lock
);
375 if (pid_alive(leader
)) {
376 struct task_struct
*task
= leader
;
378 while ((task
= next_thread(task
)) != leader
) {
382 atomic_inc(&fs
->count
);
389 read_unlock(&tasklist_lock
);
393 read_lock(&fs
->lock
);
394 *mnt
= mntget(fs
->rootmnt
);
395 *dentry
= dget(fs
->root
);
396 read_unlock(&fs
->lock
);
404 #define MAY_PTRACE(task) \
405 (task == current || \
406 (task->parent == current && \
407 (task->ptrace & PT_PTRACED) && \
408 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
409 security_ptrace(current,task) == 0))
411 static int proc_pid_environ(struct task_struct
*task
, char * buffer
)
414 struct mm_struct
*mm
= get_task_mm(task
);
416 unsigned int len
= mm
->env_end
- mm
->env_start
;
419 res
= access_process_vm(task
, mm
->env_start
, buffer
, len
, 0);
420 if (!ptrace_may_attach(task
))
427 static int proc_pid_cmdline(struct task_struct
*task
, char * buffer
)
431 struct mm_struct
*mm
= get_task_mm(task
);
435 goto out_mm
; /* Shh! No looking before we're done */
437 len
= mm
->arg_end
- mm
->arg_start
;
442 res
= access_process_vm(task
, mm
->arg_start
, buffer
, len
, 0);
444 // If the nul at the end of args has been overwritten, then
445 // assume application is using setproctitle(3).
446 if (res
> 0 && buffer
[res
-1] != '\0' && len
< PAGE_SIZE
) {
447 len
= strnlen(buffer
, res
);
451 len
= mm
->env_end
- mm
->env_start
;
452 if (len
> PAGE_SIZE
- res
)
453 len
= PAGE_SIZE
- res
;
454 res
+= access_process_vm(task
, mm
->env_start
, buffer
+res
, len
, 0);
455 res
= strnlen(buffer
, res
);
464 static int proc_pid_auxv(struct task_struct
*task
, char *buffer
)
467 struct mm_struct
*mm
= get_task_mm(task
);
469 unsigned int nwords
= 0;
472 while (mm
->saved_auxv
[nwords
- 2] != 0); /* AT_NULL */
473 res
= nwords
* sizeof(mm
->saved_auxv
[0]);
476 memcpy(buffer
, mm
->saved_auxv
, res
);
483 #ifdef CONFIG_KALLSYMS
485 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
486 * Returns the resolved symbol. If that fails, simply return the address.
488 static int proc_pid_wchan(struct task_struct
*task
, char *buffer
)
491 const char *sym_name
;
492 unsigned long wchan
, size
, offset
;
493 char namebuf
[KSYM_NAME_LEN
+1];
495 wchan
= get_wchan(task
);
497 sym_name
= kallsyms_lookup(wchan
, &size
, &offset
, &modname
, namebuf
);
499 return sprintf(buffer
, "%s", sym_name
);
500 return sprintf(buffer
, "%lu", wchan
);
502 #endif /* CONFIG_KALLSYMS */
504 #ifdef CONFIG_SCHEDSTATS
506 * Provides /proc/PID/schedstat
508 static int proc_pid_schedstat(struct task_struct
*task
, char *buffer
)
510 return sprintf(buffer
, "%lu %lu %lu\n",
511 task
->sched_info
.cpu_time
,
512 task
->sched_info
.run_delay
,
513 task
->sched_info
.pcnt
);
517 /* The badness from the OOM killer */
518 unsigned long badness(struct task_struct
*p
, unsigned long uptime
);
519 static int proc_oom_score(struct task_struct
*task
, char *buffer
)
521 unsigned long points
;
522 struct timespec uptime
;
524 do_posix_clock_monotonic_gettime(&uptime
);
525 points
= badness(task
, uptime
.tv_sec
);
526 return sprintf(buffer
, "%lu\n", points
);
529 /************************************************************************/
530 /* Here the fs part begins */
531 /************************************************************************/
533 /* permission checks */
535 /* If the process being read is separated by chroot from the reading process,
536 * don't let the reader access the threads.
538 * note: this does dput(root) and mntput(vfsmnt) on exit.
540 static int proc_check_chroot(struct dentry
*root
, struct vfsmount
*vfsmnt
)
542 struct dentry
*de
, *base
;
543 struct vfsmount
*our_vfsmnt
, *mnt
;
546 read_lock(¤t
->fs
->lock
);
547 our_vfsmnt
= mntget(current
->fs
->rootmnt
);
548 base
= dget(current
->fs
->root
);
549 read_unlock(¤t
->fs
->lock
);
551 spin_lock(&vfsmount_lock
);
555 while (mnt
!= our_vfsmnt
) {
556 if (mnt
== mnt
->mnt_parent
)
558 de
= mnt
->mnt_mountpoint
;
559 mnt
= mnt
->mnt_parent
;
562 if (!is_subdir(de
, base
))
564 spin_unlock(&vfsmount_lock
);
573 spin_unlock(&vfsmount_lock
);
578 static int proc_check_root(struct inode
*inode
)
581 struct vfsmount
*vfsmnt
;
583 if (proc_root_link(inode
, &root
, &vfsmnt
)) /* Ewww... */
585 return proc_check_chroot(root
, vfsmnt
);
588 static int proc_permission(struct inode
*inode
, int mask
, struct nameidata
*nd
)
590 if (generic_permission(inode
, mask
, NULL
) != 0)
592 return proc_check_root(inode
);
595 static int proc_task_permission(struct inode
*inode
, int mask
, struct nameidata
*nd
)
598 struct vfsmount
*vfsmnt
;
600 if (generic_permission(inode
, mask
, NULL
) != 0)
603 if (proc_task_root_link(inode
, &root
, &vfsmnt
))
606 return proc_check_chroot(root
, vfsmnt
);
609 extern struct seq_operations proc_pid_maps_op
;
610 static int maps_open(struct inode
*inode
, struct file
*file
)
612 struct task_struct
*task
= proc_task(inode
);
613 int ret
= seq_open(file
, &proc_pid_maps_op
);
615 struct seq_file
*m
= file
->private_data
;
621 static struct file_operations proc_maps_operations
= {
625 .release
= seq_release
,
629 extern struct seq_operations proc_pid_numa_maps_op
;
630 static int numa_maps_open(struct inode
*inode
, struct file
*file
)
632 struct task_struct
*task
= proc_task(inode
);
633 int ret
= seq_open(file
, &proc_pid_numa_maps_op
);
635 struct seq_file
*m
= file
->private_data
;
641 static struct file_operations proc_numa_maps_operations
= {
642 .open
= numa_maps_open
,
645 .release
= seq_release
,
650 extern struct seq_operations proc_pid_smaps_op
;
651 static int smaps_open(struct inode
*inode
, struct file
*file
)
653 struct task_struct
*task
= proc_task(inode
);
654 int ret
= seq_open(file
, &proc_pid_smaps_op
);
656 struct seq_file
*m
= file
->private_data
;
662 static struct file_operations proc_smaps_operations
= {
666 .release
= seq_release
,
670 extern struct seq_operations mounts_op
;
676 static int mounts_open(struct inode
*inode
, struct file
*file
)
678 struct task_struct
*task
= proc_task(inode
);
679 struct namespace *namespace;
680 struct proc_mounts
*p
;
684 namespace = task
->namespace;
686 get_namespace(namespace);
691 p
= kmalloc(sizeof(struct proc_mounts
), GFP_KERNEL
);
693 file
->private_data
= &p
->m
;
694 ret
= seq_open(file
, &mounts_op
);
696 p
->m
.private = namespace;
697 p
->event
= namespace->event
;
702 put_namespace(namespace);
707 static int mounts_release(struct inode
*inode
, struct file
*file
)
709 struct seq_file
*m
= file
->private_data
;
710 struct namespace *namespace = m
->private;
711 put_namespace(namespace);
712 return seq_release(inode
, file
);
715 static unsigned mounts_poll(struct file
*file
, poll_table
*wait
)
717 struct proc_mounts
*p
= file
->private_data
;
718 struct namespace *ns
= p
->m
.private;
721 poll_wait(file
, &ns
->poll
, wait
);
723 spin_lock(&vfsmount_lock
);
724 if (p
->event
!= ns
->event
) {
725 p
->event
= ns
->event
;
728 spin_unlock(&vfsmount_lock
);
733 static struct file_operations proc_mounts_operations
= {
737 .release
= mounts_release
,
741 extern struct seq_operations mountstats_op
;
742 static int mountstats_open(struct inode
*inode
, struct file
*file
)
744 struct task_struct
*task
= proc_task(inode
);
745 int ret
= seq_open(file
, &mountstats_op
);
748 struct seq_file
*m
= file
->private_data
;
749 struct namespace *namespace;
751 namespace = task
->namespace;
753 get_namespace(namespace);
757 m
->private = namespace;
759 seq_release(inode
, file
);
766 static struct file_operations proc_mountstats_operations
= {
767 .open
= mountstats_open
,
770 .release
= mounts_release
,
773 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
775 static ssize_t
proc_info_read(struct file
* file
, char __user
* buf
,
776 size_t count
, loff_t
*ppos
)
778 struct inode
* inode
= file
->f_dentry
->d_inode
;
781 struct task_struct
*task
= proc_task(inode
);
783 if (count
> PROC_BLOCK_SIZE
)
784 count
= PROC_BLOCK_SIZE
;
785 if (!(page
= __get_free_page(GFP_KERNEL
)))
788 length
= PROC_I(inode
)->op
.proc_read(task
, (char*)page
);
791 length
= simple_read_from_buffer(buf
, count
, ppos
, (char *)page
, length
);
796 static struct file_operations proc_info_file_operations
= {
797 .read
= proc_info_read
,
800 static int mem_open(struct inode
* inode
, struct file
* file
)
802 file
->private_data
= (void*)((long)current
->self_exec_id
);
806 static ssize_t
mem_read(struct file
* file
, char __user
* buf
,
807 size_t count
, loff_t
*ppos
)
809 struct task_struct
*task
= proc_task(file
->f_dentry
->d_inode
);
811 unsigned long src
= *ppos
;
813 struct mm_struct
*mm
;
815 if (!MAY_PTRACE(task
) || !ptrace_may_attach(task
))
819 page
= (char *)__get_free_page(GFP_USER
);
825 mm
= get_task_mm(task
);
831 if (file
->private_data
!= (void*)((long)current
->self_exec_id
))
837 int this_len
, retval
;
839 this_len
= (count
> PAGE_SIZE
) ? PAGE_SIZE
: count
;
840 retval
= access_process_vm(task
, src
, page
, this_len
, 0);
841 if (!retval
|| !MAY_PTRACE(task
) || !ptrace_may_attach(task
)) {
847 if (copy_to_user(buf
, page
, retval
)) {
862 free_page((unsigned long) page
);
867 #define mem_write NULL
870 /* This is a security hazard */
871 static ssize_t
mem_write(struct file
* file
, const char * buf
,
872 size_t count
, loff_t
*ppos
)
876 struct task_struct
*task
= proc_task(file
->f_dentry
->d_inode
);
877 unsigned long dst
= *ppos
;
879 if (!MAY_PTRACE(task
) || !ptrace_may_attach(task
))
882 page
= (char *)__get_free_page(GFP_USER
);
887 int this_len
, retval
;
889 this_len
= (count
> PAGE_SIZE
) ? PAGE_SIZE
: count
;
890 if (copy_from_user(page
, buf
, this_len
)) {
894 retval
= access_process_vm(task
, dst
, page
, this_len
, 1);
906 free_page((unsigned long) page
);
911 static loff_t
mem_lseek(struct file
* file
, loff_t offset
, int orig
)
915 file
->f_pos
= offset
;
918 file
->f_pos
+= offset
;
923 force_successful_syscall_return();
927 static struct file_operations proc_mem_operations
= {
934 static ssize_t
oom_adjust_read(struct file
*file
, char __user
*buf
,
935 size_t count
, loff_t
*ppos
)
937 struct task_struct
*task
= proc_task(file
->f_dentry
->d_inode
);
940 int oom_adjust
= task
->oomkilladj
;
941 loff_t __ppos
= *ppos
;
943 len
= sprintf(buffer
, "%i\n", oom_adjust
);
946 if (count
> len
-__ppos
)
948 if (copy_to_user(buf
, buffer
+ __ppos
, count
))
950 *ppos
= __ppos
+ count
;
954 static ssize_t
oom_adjust_write(struct file
*file
, const char __user
*buf
,
955 size_t count
, loff_t
*ppos
)
957 struct task_struct
*task
= proc_task(file
->f_dentry
->d_inode
);
958 char buffer
[8], *end
;
961 if (!capable(CAP_SYS_RESOURCE
))
963 memset(buffer
, 0, 8);
966 if (copy_from_user(buffer
, buf
, count
))
968 oom_adjust
= simple_strtol(buffer
, &end
, 0);
969 if ((oom_adjust
< -16 || oom_adjust
> 15) && oom_adjust
!= OOM_DISABLE
)
973 task
->oomkilladj
= oom_adjust
;
974 if (end
- buffer
== 0)
979 static struct file_operations proc_oom_adjust_operations
= {
980 .read
= oom_adjust_read
,
981 .write
= oom_adjust_write
,
984 static struct inode_operations proc_mem_inode_operations
= {
985 .permission
= proc_permission
,
988 #ifdef CONFIG_AUDITSYSCALL
990 static ssize_t
proc_loginuid_read(struct file
* file
, char __user
* buf
,
991 size_t count
, loff_t
*ppos
)
993 struct inode
* inode
= file
->f_dentry
->d_inode
;
994 struct task_struct
*task
= proc_task(inode
);
996 char tmpbuf
[TMPBUFLEN
];
998 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
999 audit_get_loginuid(task
->audit_context
));
1000 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1003 static ssize_t
proc_loginuid_write(struct file
* file
, const char __user
* buf
,
1004 size_t count
, loff_t
*ppos
)
1006 struct inode
* inode
= file
->f_dentry
->d_inode
;
1009 struct task_struct
*task
= proc_task(inode
);
1012 if (!capable(CAP_AUDIT_CONTROL
))
1015 if (current
!= task
)
1018 if (count
> PAGE_SIZE
)
1022 /* No partial writes. */
1025 page
= (char*)__get_free_page(GFP_USER
);
1029 if (copy_from_user(page
, buf
, count
))
1032 loginuid
= simple_strtoul(page
, &tmp
, 10);
1038 length
= audit_set_loginuid(task
, loginuid
);
1039 if (likely(length
== 0))
1043 free_page((unsigned long) page
);
1047 static struct file_operations proc_loginuid_operations
= {
1048 .read
= proc_loginuid_read
,
1049 .write
= proc_loginuid_write
,
1053 #ifdef CONFIG_SECCOMP
1054 static ssize_t
seccomp_read(struct file
*file
, char __user
*buf
,
1055 size_t count
, loff_t
*ppos
)
1057 struct task_struct
*tsk
= proc_task(file
->f_dentry
->d_inode
);
1059 loff_t __ppos
= *ppos
;
1062 /* no need to print the trailing zero, so use only len */
1063 len
= sprintf(__buf
, "%u\n", tsk
->seccomp
.mode
);
1066 if (count
> len
- __ppos
)
1067 count
= len
- __ppos
;
1068 if (copy_to_user(buf
, __buf
+ __ppos
, count
))
1070 *ppos
= __ppos
+ count
;
1074 static ssize_t
seccomp_write(struct file
*file
, const char __user
*buf
,
1075 size_t count
, loff_t
*ppos
)
1077 struct task_struct
*tsk
= proc_task(file
->f_dentry
->d_inode
);
1078 char __buf
[20], *end
;
1079 unsigned int seccomp_mode
;
1081 /* can set it only once to be even more secure */
1082 if (unlikely(tsk
->seccomp
.mode
))
1085 memset(__buf
, 0, sizeof(__buf
));
1086 count
= min(count
, sizeof(__buf
) - 1);
1087 if (copy_from_user(__buf
, buf
, count
))
1089 seccomp_mode
= simple_strtoul(__buf
, &end
, 0);
1092 if (seccomp_mode
&& seccomp_mode
<= NR_SECCOMP_MODES
) {
1093 tsk
->seccomp
.mode
= seccomp_mode
;
1094 set_tsk_thread_flag(tsk
, TIF_SECCOMP
);
1097 if (unlikely(!(end
- __buf
)))
1102 static struct file_operations proc_seccomp_operations
= {
1103 .read
= seccomp_read
,
1104 .write
= seccomp_write
,
1106 #endif /* CONFIG_SECCOMP */
1108 static void *proc_pid_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
1110 struct inode
*inode
= dentry
->d_inode
;
1111 int error
= -EACCES
;
1113 /* We don't need a base pointer in the /proc filesystem */
1116 if (current
->fsuid
!= inode
->i_uid
&& !capable(CAP_DAC_OVERRIDE
))
1118 error
= proc_check_root(inode
);
1122 error
= PROC_I(inode
)->op
.proc_get_link(inode
, &nd
->dentry
, &nd
->mnt
);
1123 nd
->last_type
= LAST_BIND
;
1125 return ERR_PTR(error
);
1128 static int do_proc_readlink(struct dentry
*dentry
, struct vfsmount
*mnt
,
1129 char __user
*buffer
, int buflen
)
1131 struct inode
* inode
;
1132 char *tmp
= (char*)__get_free_page(GFP_KERNEL
), *path
;
1138 inode
= dentry
->d_inode
;
1139 path
= d_path(dentry
, mnt
, tmp
, PAGE_SIZE
);
1140 len
= PTR_ERR(path
);
1143 len
= tmp
+ PAGE_SIZE
- 1 - path
;
1147 if (copy_to_user(buffer
, path
, len
))
1150 free_page((unsigned long)tmp
);
1154 static int proc_pid_readlink(struct dentry
* dentry
, char __user
* buffer
, int buflen
)
1156 int error
= -EACCES
;
1157 struct inode
*inode
= dentry
->d_inode
;
1159 struct vfsmount
*mnt
= NULL
;
1163 if (current
->fsuid
!= inode
->i_uid
&& !capable(CAP_DAC_OVERRIDE
))
1165 error
= proc_check_root(inode
);
1169 error
= PROC_I(inode
)->op
.proc_get_link(inode
, &de
, &mnt
);
1173 error
= do_proc_readlink(de
, mnt
, buffer
, buflen
);
1181 static struct inode_operations proc_pid_link_inode_operations
= {
1182 .readlink
= proc_pid_readlink
,
1183 .follow_link
= proc_pid_follow_link
1188 static int proc_readfd(struct file
* filp
, void * dirent
, filldir_t filldir
)
1190 struct inode
*inode
= filp
->f_dentry
->d_inode
;
1191 struct task_struct
*p
= proc_task(inode
);
1192 unsigned int fd
, tid
, ino
;
1195 struct files_struct
* files
;
1196 struct fdtable
*fdt
;
1207 if (filldir(dirent
, ".", 1, 0, inode
->i_ino
, DT_DIR
) < 0)
1211 ino
= fake_ino(tid
, PROC_TID_INO
);
1212 if (filldir(dirent
, "..", 2, 1, ino
, DT_DIR
) < 0)
1216 files
= get_files_struct(p
);
1220 fdt
= files_fdtable(files
);
1221 for (fd
= filp
->f_pos
-2;
1223 fd
++, filp
->f_pos
++) {
1226 if (!fcheck_files(files
, fd
))
1234 buf
[j
] = '0' + (i
% 10);
1238 ino
= fake_ino(tid
, PROC_TID_FD_DIR
+ fd
);
1239 if (filldir(dirent
, buf
+j
, NUMBUF
-j
, fd
+2, ino
, DT_LNK
) < 0) {
1246 put_files_struct(files
);
1252 static int proc_pident_readdir(struct file
*filp
,
1253 void *dirent
, filldir_t filldir
,
1254 struct pid_entry
*ents
, unsigned int nents
)
1258 struct dentry
*dentry
= filp
->f_dentry
;
1259 struct inode
*inode
= dentry
->d_inode
;
1260 struct pid_entry
*p
;
1265 if (!pid_alive(proc_task(inode
)))
1269 pid
= proc_task(inode
)->pid
;
1274 if (filldir(dirent
, ".", 1, i
, ino
, DT_DIR
) < 0)
1280 ino
= parent_ino(dentry
);
1281 if (filldir(dirent
, "..", 2, i
, ino
, DT_DIR
) < 0)
1294 if (filldir(dirent
, p
->name
, p
->len
, filp
->f_pos
,
1295 fake_ino(pid
, p
->type
), p
->mode
>> 12) < 0)
1307 static int proc_tgid_base_readdir(struct file
* filp
,
1308 void * dirent
, filldir_t filldir
)
1310 return proc_pident_readdir(filp
,dirent
,filldir
,
1311 tgid_base_stuff
,ARRAY_SIZE(tgid_base_stuff
));
1314 static int proc_tid_base_readdir(struct file
* filp
,
1315 void * dirent
, filldir_t filldir
)
1317 return proc_pident_readdir(filp
,dirent
,filldir
,
1318 tid_base_stuff
,ARRAY_SIZE(tid_base_stuff
));
1321 /* building an inode */
1323 static int task_dumpable(struct task_struct
*task
)
1326 struct mm_struct
*mm
;
1331 dumpable
= mm
->dumpable
;
1339 static struct inode
*proc_pid_make_inode(struct super_block
* sb
, struct task_struct
*task
, int ino
)
1341 struct inode
* inode
;
1342 struct proc_inode
*ei
;
1344 /* We need a new inode */
1346 inode
= new_inode(sb
);
1353 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1354 inode
->i_ino
= fake_ino(task
->pid
, ino
);
1356 if (!pid_alive(task
))
1360 * grab the reference to task.
1362 get_task_struct(task
);
1367 if (ino
== PROC_TGID_INO
|| ino
== PROC_TID_INO
|| task_dumpable(task
)) {
1368 inode
->i_uid
= task
->euid
;
1369 inode
->i_gid
= task
->egid
;
1371 security_task_to_inode(task
, inode
);
1385 * Exceptional case: normally we are not allowed to unhash a busy
1386 * directory. In this case, however, we can do it - no aliasing problems
1387 * due to the way we treat inodes.
1389 * Rewrite the inode's ownerships here because the owning task may have
1390 * performed a setuid(), etc.
1392 static int pid_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1394 struct inode
*inode
= dentry
->d_inode
;
1395 struct task_struct
*task
= proc_task(inode
);
1396 if (pid_alive(task
)) {
1397 if (proc_type(inode
) == PROC_TGID_INO
|| proc_type(inode
) == PROC_TID_INO
|| task_dumpable(task
)) {
1398 inode
->i_uid
= task
->euid
;
1399 inode
->i_gid
= task
->egid
;
1404 security_task_to_inode(task
, inode
);
1411 static int tid_fd_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1413 struct inode
*inode
= dentry
->d_inode
;
1414 struct task_struct
*task
= proc_task(inode
);
1415 int fd
= proc_type(inode
) - PROC_TID_FD_DIR
;
1416 struct files_struct
*files
;
1418 files
= get_files_struct(task
);
1421 if (fcheck_files(files
, fd
)) {
1423 put_files_struct(files
);
1424 if (task_dumpable(task
)) {
1425 inode
->i_uid
= task
->euid
;
1426 inode
->i_gid
= task
->egid
;
1431 security_task_to_inode(task
, inode
);
1435 put_files_struct(files
);
1441 static void pid_base_iput(struct dentry
*dentry
, struct inode
*inode
)
1443 struct task_struct
*task
= proc_task(inode
);
1444 spin_lock(&task
->proc_lock
);
1445 if (task
->proc_dentry
== dentry
)
1446 task
->proc_dentry
= NULL
;
1447 spin_unlock(&task
->proc_lock
);
1451 static int pid_delete_dentry(struct dentry
* dentry
)
1453 /* Is the task we represent dead?
1454 * If so, then don't put the dentry on the lru list,
1455 * kill it immediately.
1457 return !pid_alive(proc_task(dentry
->d_inode
));
1460 static struct dentry_operations tid_fd_dentry_operations
=
1462 .d_revalidate
= tid_fd_revalidate
,
1463 .d_delete
= pid_delete_dentry
,
1466 static struct dentry_operations pid_dentry_operations
=
1468 .d_revalidate
= pid_revalidate
,
1469 .d_delete
= pid_delete_dentry
,
1472 static struct dentry_operations pid_base_dentry_operations
=
1474 .d_revalidate
= pid_revalidate
,
1475 .d_iput
= pid_base_iput
,
1476 .d_delete
= pid_delete_dentry
,
1481 static unsigned name_to_int(struct dentry
*dentry
)
1483 const char *name
= dentry
->d_name
.name
;
1484 int len
= dentry
->d_name
.len
;
1487 if (len
> 1 && *name
== '0')
1490 unsigned c
= *name
++ - '0';
1493 if (n
>= (~0U-9)/10)
1504 static struct dentry
*proc_lookupfd(struct inode
* dir
, struct dentry
* dentry
, struct nameidata
*nd
)
1506 struct task_struct
*task
= proc_task(dir
);
1507 unsigned fd
= name_to_int(dentry
);
1509 struct files_struct
* files
;
1510 struct inode
*inode
;
1511 struct proc_inode
*ei
;
1515 if (!pid_alive(task
))
1518 inode
= proc_pid_make_inode(dir
->i_sb
, task
, PROC_TID_FD_DIR
+fd
);
1522 files
= get_files_struct(task
);
1525 inode
->i_mode
= S_IFLNK
;
1527 file
= fcheck_files(files
, fd
);
1530 if (file
->f_mode
& 1)
1531 inode
->i_mode
|= S_IRUSR
| S_IXUSR
;
1532 if (file
->f_mode
& 2)
1533 inode
->i_mode
|= S_IWUSR
| S_IXUSR
;
1535 put_files_struct(files
);
1536 inode
->i_op
= &proc_pid_link_inode_operations
;
1538 ei
->op
.proc_get_link
= proc_fd_link
;
1539 dentry
->d_op
= &tid_fd_dentry_operations
;
1540 d_add(dentry
, inode
);
1545 put_files_struct(files
);
1549 return ERR_PTR(-ENOENT
);
1552 static int proc_task_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
);
1553 static struct dentry
*proc_task_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
);
1555 static struct file_operations proc_fd_operations
= {
1556 .read
= generic_read_dir
,
1557 .readdir
= proc_readfd
,
1560 static struct file_operations proc_task_operations
= {
1561 .read
= generic_read_dir
,
1562 .readdir
= proc_task_readdir
,
1566 * proc directories can do almost nothing..
1568 static struct inode_operations proc_fd_inode_operations
= {
1569 .lookup
= proc_lookupfd
,
1570 .permission
= proc_permission
,
1573 static struct inode_operations proc_task_inode_operations
= {
1574 .lookup
= proc_task_lookup
,
1575 .permission
= proc_task_permission
,
1578 #ifdef CONFIG_SECURITY
1579 static ssize_t
proc_pid_attr_read(struct file
* file
, char __user
* buf
,
1580 size_t count
, loff_t
*ppos
)
1582 struct inode
* inode
= file
->f_dentry
->d_inode
;
1585 struct task_struct
*task
= proc_task(inode
);
1587 if (count
> PAGE_SIZE
)
1589 if (!(page
= __get_free_page(GFP_KERNEL
)))
1592 length
= security_getprocattr(task
,
1593 (char*)file
->f_dentry
->d_name
.name
,
1594 (void*)page
, count
);
1596 length
= simple_read_from_buffer(buf
, count
, ppos
, (char *)page
, length
);
1601 static ssize_t
proc_pid_attr_write(struct file
* file
, const char __user
* buf
,
1602 size_t count
, loff_t
*ppos
)
1604 struct inode
* inode
= file
->f_dentry
->d_inode
;
1607 struct task_struct
*task
= proc_task(inode
);
1609 if (count
> PAGE_SIZE
)
1612 /* No partial writes. */
1615 page
= (char*)__get_free_page(GFP_USER
);
1619 if (copy_from_user(page
, buf
, count
))
1622 length
= security_setprocattr(task
,
1623 (char*)file
->f_dentry
->d_name
.name
,
1624 (void*)page
, count
);
1626 free_page((unsigned long) page
);
1630 static struct file_operations proc_pid_attr_operations
= {
1631 .read
= proc_pid_attr_read
,
1632 .write
= proc_pid_attr_write
,
1635 static struct file_operations proc_tid_attr_operations
;
1636 static struct inode_operations proc_tid_attr_inode_operations
;
1637 static struct file_operations proc_tgid_attr_operations
;
1638 static struct inode_operations proc_tgid_attr_inode_operations
;
1641 static int get_tid_list(int index
, unsigned int *tids
, struct inode
*dir
);
1644 static struct dentry
*proc_pident_lookup(struct inode
*dir
,
1645 struct dentry
*dentry
,
1646 struct pid_entry
*ents
)
1648 struct inode
*inode
;
1650 struct task_struct
*task
= proc_task(dir
);
1651 struct pid_entry
*p
;
1652 struct proc_inode
*ei
;
1657 if (!pid_alive(task
))
1660 for (p
= ents
; p
->name
; p
++) {
1661 if (p
->len
!= dentry
->d_name
.len
)
1663 if (!memcmp(dentry
->d_name
.name
, p
->name
, p
->len
))
1670 inode
= proc_pid_make_inode(dir
->i_sb
, task
, p
->type
);
1675 inode
->i_mode
= p
->mode
;
1677 * Yes, it does not scale. And it should not. Don't add
1678 * new entries into /proc/<tgid>/ without very good reasons.
1681 case PROC_TGID_TASK
:
1682 inode
->i_nlink
= 2 + get_tid_list(2, NULL
, dir
);
1683 inode
->i_op
= &proc_task_inode_operations
;
1684 inode
->i_fop
= &proc_task_operations
;
1689 inode
->i_op
= &proc_fd_inode_operations
;
1690 inode
->i_fop
= &proc_fd_operations
;
1694 inode
->i_op
= &proc_pid_link_inode_operations
;
1695 ei
->op
.proc_get_link
= proc_exe_link
;
1699 inode
->i_op
= &proc_pid_link_inode_operations
;
1700 ei
->op
.proc_get_link
= proc_cwd_link
;
1703 case PROC_TGID_ROOT
:
1704 inode
->i_op
= &proc_pid_link_inode_operations
;
1705 ei
->op
.proc_get_link
= proc_root_link
;
1707 case PROC_TID_ENVIRON
:
1708 case PROC_TGID_ENVIRON
:
1709 inode
->i_fop
= &proc_info_file_operations
;
1710 ei
->op
.proc_read
= proc_pid_environ
;
1713 case PROC_TGID_AUXV
:
1714 inode
->i_fop
= &proc_info_file_operations
;
1715 ei
->op
.proc_read
= proc_pid_auxv
;
1717 case PROC_TID_STATUS
:
1718 case PROC_TGID_STATUS
:
1719 inode
->i_fop
= &proc_info_file_operations
;
1720 ei
->op
.proc_read
= proc_pid_status
;
1723 inode
->i_fop
= &proc_info_file_operations
;
1724 ei
->op
.proc_read
= proc_tid_stat
;
1726 case PROC_TGID_STAT
:
1727 inode
->i_fop
= &proc_info_file_operations
;
1728 ei
->op
.proc_read
= proc_tgid_stat
;
1730 case PROC_TID_CMDLINE
:
1731 case PROC_TGID_CMDLINE
:
1732 inode
->i_fop
= &proc_info_file_operations
;
1733 ei
->op
.proc_read
= proc_pid_cmdline
;
1735 case PROC_TID_STATM
:
1736 case PROC_TGID_STATM
:
1737 inode
->i_fop
= &proc_info_file_operations
;
1738 ei
->op
.proc_read
= proc_pid_statm
;
1741 case PROC_TGID_MAPS
:
1742 inode
->i_fop
= &proc_maps_operations
;
1745 case PROC_TID_NUMA_MAPS
:
1746 case PROC_TGID_NUMA_MAPS
:
1747 inode
->i_fop
= &proc_numa_maps_operations
;
1752 inode
->i_op
= &proc_mem_inode_operations
;
1753 inode
->i_fop
= &proc_mem_operations
;
1755 #ifdef CONFIG_SECCOMP
1756 case PROC_TID_SECCOMP
:
1757 case PROC_TGID_SECCOMP
:
1758 inode
->i_fop
= &proc_seccomp_operations
;
1760 #endif /* CONFIG_SECCOMP */
1761 case PROC_TID_MOUNTS
:
1762 case PROC_TGID_MOUNTS
:
1763 inode
->i_fop
= &proc_mounts_operations
;
1766 case PROC_TID_SMAPS
:
1767 case PROC_TGID_SMAPS
:
1768 inode
->i_fop
= &proc_smaps_operations
;
1771 case PROC_TID_MOUNTSTATS
:
1772 case PROC_TGID_MOUNTSTATS
:
1773 inode
->i_fop
= &proc_mountstats_operations
;
1775 #ifdef CONFIG_SECURITY
1778 inode
->i_op
= &proc_tid_attr_inode_operations
;
1779 inode
->i_fop
= &proc_tid_attr_operations
;
1781 case PROC_TGID_ATTR
:
1783 inode
->i_op
= &proc_tgid_attr_inode_operations
;
1784 inode
->i_fop
= &proc_tgid_attr_operations
;
1786 case PROC_TID_ATTR_CURRENT
:
1787 case PROC_TGID_ATTR_CURRENT
:
1788 case PROC_TID_ATTR_PREV
:
1789 case PROC_TGID_ATTR_PREV
:
1790 case PROC_TID_ATTR_EXEC
:
1791 case PROC_TGID_ATTR_EXEC
:
1792 case PROC_TID_ATTR_FSCREATE
:
1793 case PROC_TGID_ATTR_FSCREATE
:
1794 inode
->i_fop
= &proc_pid_attr_operations
;
1797 #ifdef CONFIG_KALLSYMS
1798 case PROC_TID_WCHAN
:
1799 case PROC_TGID_WCHAN
:
1800 inode
->i_fop
= &proc_info_file_operations
;
1801 ei
->op
.proc_read
= proc_pid_wchan
;
1804 #ifdef CONFIG_SCHEDSTATS
1805 case PROC_TID_SCHEDSTAT
:
1806 case PROC_TGID_SCHEDSTAT
:
1807 inode
->i_fop
= &proc_info_file_operations
;
1808 ei
->op
.proc_read
= proc_pid_schedstat
;
1811 #ifdef CONFIG_CPUSETS
1812 case PROC_TID_CPUSET
:
1813 case PROC_TGID_CPUSET
:
1814 inode
->i_fop
= &proc_cpuset_operations
;
1817 case PROC_TID_OOM_SCORE
:
1818 case PROC_TGID_OOM_SCORE
:
1819 inode
->i_fop
= &proc_info_file_operations
;
1820 ei
->op
.proc_read
= proc_oom_score
;
1822 case PROC_TID_OOM_ADJUST
:
1823 case PROC_TGID_OOM_ADJUST
:
1824 inode
->i_fop
= &proc_oom_adjust_operations
;
1826 #ifdef CONFIG_AUDITSYSCALL
1827 case PROC_TID_LOGINUID
:
1828 case PROC_TGID_LOGINUID
:
1829 inode
->i_fop
= &proc_loginuid_operations
;
1833 printk("procfs: impossible type (%d)",p
->type
);
1835 return ERR_PTR(-EINVAL
);
1837 dentry
->d_op
= &pid_dentry_operations
;
1838 d_add(dentry
, inode
);
1842 return ERR_PTR(error
);
1845 static struct dentry
*proc_tgid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
){
1846 return proc_pident_lookup(dir
, dentry
, tgid_base_stuff
);
1849 static struct dentry
*proc_tid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
){
1850 return proc_pident_lookup(dir
, dentry
, tid_base_stuff
);
1853 static struct file_operations proc_tgid_base_operations
= {
1854 .read
= generic_read_dir
,
1855 .readdir
= proc_tgid_base_readdir
,
1858 static struct file_operations proc_tid_base_operations
= {
1859 .read
= generic_read_dir
,
1860 .readdir
= proc_tid_base_readdir
,
1863 static struct inode_operations proc_tgid_base_inode_operations
= {
1864 .lookup
= proc_tgid_base_lookup
,
1867 static struct inode_operations proc_tid_base_inode_operations
= {
1868 .lookup
= proc_tid_base_lookup
,
1871 #ifdef CONFIG_SECURITY
1872 static int proc_tgid_attr_readdir(struct file
* filp
,
1873 void * dirent
, filldir_t filldir
)
1875 return proc_pident_readdir(filp
,dirent
,filldir
,
1876 tgid_attr_stuff
,ARRAY_SIZE(tgid_attr_stuff
));
1879 static int proc_tid_attr_readdir(struct file
* filp
,
1880 void * dirent
, filldir_t filldir
)
1882 return proc_pident_readdir(filp
,dirent
,filldir
,
1883 tid_attr_stuff
,ARRAY_SIZE(tid_attr_stuff
));
1886 static struct file_operations proc_tgid_attr_operations
= {
1887 .read
= generic_read_dir
,
1888 .readdir
= proc_tgid_attr_readdir
,
1891 static struct file_operations proc_tid_attr_operations
= {
1892 .read
= generic_read_dir
,
1893 .readdir
= proc_tid_attr_readdir
,
1896 static struct dentry
*proc_tgid_attr_lookup(struct inode
*dir
,
1897 struct dentry
*dentry
, struct nameidata
*nd
)
1899 return proc_pident_lookup(dir
, dentry
, tgid_attr_stuff
);
1902 static struct dentry
*proc_tid_attr_lookup(struct inode
*dir
,
1903 struct dentry
*dentry
, struct nameidata
*nd
)
1905 return proc_pident_lookup(dir
, dentry
, tid_attr_stuff
);
1908 static struct inode_operations proc_tgid_attr_inode_operations
= {
1909 .lookup
= proc_tgid_attr_lookup
,
1912 static struct inode_operations proc_tid_attr_inode_operations
= {
1913 .lookup
= proc_tid_attr_lookup
,
1920 static int proc_self_readlink(struct dentry
*dentry
, char __user
*buffer
,
1924 sprintf(tmp
, "%d", current
->tgid
);
1925 return vfs_readlink(dentry
,buffer
,buflen
,tmp
);
1928 static void *proc_self_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
1931 sprintf(tmp
, "%d", current
->tgid
);
1932 return ERR_PTR(vfs_follow_link(nd
,tmp
));
1935 static struct inode_operations proc_self_inode_operations
= {
1936 .readlink
= proc_self_readlink
,
1937 .follow_link
= proc_self_follow_link
,
1941 * proc_pid_unhash - Unhash /proc/@pid entry from the dcache.
1942 * @p: task that should be flushed.
1944 * Drops the /proc/@pid dcache entry from the hash chains.
1946 * Dropping /proc/@pid entries and detach_pid must be synchroneous,
1947 * otherwise e.g. /proc/@pid/exe might point to the wrong executable,
1948 * if the pid value is immediately reused. This is enforced by
1949 * - caller must acquire spin_lock(p->proc_lock)
1950 * - must be called before detach_pid()
1951 * - proc_pid_lookup acquires proc_lock, and checks that
1952 * the target is not dead by looking at the attach count
1956 struct dentry
*proc_pid_unhash(struct task_struct
*p
)
1958 struct dentry
*proc_dentry
;
1960 proc_dentry
= p
->proc_dentry
;
1961 if (proc_dentry
!= NULL
) {
1963 spin_lock(&dcache_lock
);
1964 spin_lock(&proc_dentry
->d_lock
);
1965 if (!d_unhashed(proc_dentry
)) {
1966 dget_locked(proc_dentry
);
1967 __d_drop(proc_dentry
);
1968 spin_unlock(&proc_dentry
->d_lock
);
1970 spin_unlock(&proc_dentry
->d_lock
);
1973 spin_unlock(&dcache_lock
);
1979 * proc_pid_flush - recover memory used by stale /proc/@pid/x entries
1980 * @proc_dentry: directoy to prune.
1982 * Shrink the /proc directory that was used by the just killed thread.
1985 void proc_pid_flush(struct dentry
*proc_dentry
)
1988 if(proc_dentry
!= NULL
) {
1989 shrink_dcache_parent(proc_dentry
);
1995 struct dentry
*proc_pid_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
)
1997 struct task_struct
*task
;
1998 struct inode
*inode
;
1999 struct proc_inode
*ei
;
2003 if (dentry
->d_name
.len
== 4 && !memcmp(dentry
->d_name
.name
,"self",4)) {
2004 inode
= new_inode(dir
->i_sb
);
2006 return ERR_PTR(-ENOMEM
);
2008 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
2009 inode
->i_ino
= fake_ino(0, PROC_TGID_INO
);
2011 inode
->i_mode
= S_IFLNK
|S_IRWXUGO
;
2012 inode
->i_uid
= inode
->i_gid
= 0;
2014 inode
->i_op
= &proc_self_inode_operations
;
2015 d_add(dentry
, inode
);
2018 tgid
= name_to_int(dentry
);
2022 read_lock(&tasklist_lock
);
2023 task
= find_task_by_pid(tgid
);
2025 get_task_struct(task
);
2026 read_unlock(&tasklist_lock
);
2030 inode
= proc_pid_make_inode(dir
->i_sb
, task
, PROC_TGID_INO
);
2034 put_task_struct(task
);
2037 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
2038 inode
->i_op
= &proc_tgid_base_inode_operations
;
2039 inode
->i_fop
= &proc_tgid_base_operations
;
2040 inode
->i_flags
|=S_IMMUTABLE
;
2041 #ifdef CONFIG_SECURITY
2047 dentry
->d_op
= &pid_base_dentry_operations
;
2050 d_add(dentry
, inode
);
2051 spin_lock(&task
->proc_lock
);
2052 task
->proc_dentry
= dentry
;
2053 if (!pid_alive(task
)) {
2054 dentry
= proc_pid_unhash(task
);
2057 spin_unlock(&task
->proc_lock
);
2059 put_task_struct(task
);
2061 proc_pid_flush(dentry
);
2066 return ERR_PTR(-ENOENT
);
2070 static struct dentry
*proc_task_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
)
2072 struct task_struct
*task
;
2073 struct task_struct
*leader
= proc_task(dir
);
2074 struct inode
*inode
;
2077 tid
= name_to_int(dentry
);
2081 read_lock(&tasklist_lock
);
2082 task
= find_task_by_pid(tid
);
2084 get_task_struct(task
);
2085 read_unlock(&tasklist_lock
);
2088 if (leader
->tgid
!= task
->tgid
)
2091 inode
= proc_pid_make_inode(dir
->i_sb
, task
, PROC_TID_INO
);
2096 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
2097 inode
->i_op
= &proc_tid_base_inode_operations
;
2098 inode
->i_fop
= &proc_tid_base_operations
;
2099 inode
->i_flags
|=S_IMMUTABLE
;
2100 #ifdef CONFIG_SECURITY
2106 dentry
->d_op
= &pid_base_dentry_operations
;
2108 d_add(dentry
, inode
);
2110 put_task_struct(task
);
2113 put_task_struct(task
);
2115 return ERR_PTR(-ENOENT
);
2118 #define PROC_NUMBUF 10
2119 #define PROC_MAXPIDS 20
2122 * Get a few tgid's to return for filldir - we need to hold the
2123 * tasklist lock while doing this, and we must release it before
2124 * we actually do the filldir itself, so we use a temp buffer..
2126 static int get_tgid_list(int index
, unsigned long version
, unsigned int *tgids
)
2128 struct task_struct
*p
;
2132 read_lock(&tasklist_lock
);
2135 p
= find_task_by_pid(version
);
2136 if (p
&& !thread_group_leader(p
))
2143 p
= next_task(&init_task
);
2145 for ( ; p
!= &init_task
; p
= next_task(p
)) {
2151 tgids
[nr_tgids
] = tgid
;
2153 if (nr_tgids
>= PROC_MAXPIDS
)
2156 read_unlock(&tasklist_lock
);
2161 * Get a few tid's to return for filldir - we need to hold the
2162 * tasklist lock while doing this, and we must release it before
2163 * we actually do the filldir itself, so we use a temp buffer..
2165 static int get_tid_list(int index
, unsigned int *tids
, struct inode
*dir
)
2167 struct task_struct
*leader_task
= proc_task(dir
);
2168 struct task_struct
*task
= leader_task
;
2172 read_lock(&tasklist_lock
);
2174 * The starting point task (leader_task) might be an already
2175 * unlinked task, which cannot be used to access the task-list
2176 * via next_thread().
2178 if (pid_alive(task
)) do {
2179 int tid
= task
->pid
;
2184 tids
[nr_tids
] = tid
;
2186 if (nr_tids
>= PROC_MAXPIDS
)
2188 } while ((task
= next_thread(task
)) != leader_task
);
2189 read_unlock(&tasklist_lock
);
2193 /* for the /proc/ directory itself, after non-process stuff has been done */
2194 int proc_pid_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
2196 unsigned int tgid_array
[PROC_MAXPIDS
];
2197 char buf
[PROC_NUMBUF
];
2198 unsigned int nr
= filp
->f_pos
- FIRST_PROCESS_ENTRY
;
2199 unsigned int nr_tgids
, i
;
2203 ino_t ino
= fake_ino(0,PROC_TGID_INO
);
2204 if (filldir(dirent
, "self", 4, filp
->f_pos
, ino
, DT_LNK
) < 0)
2210 /* f_version caches the tgid value that the last readdir call couldn't
2211 * return. lseek aka telldir automagically resets f_version to 0.
2213 next_tgid
= filp
->f_version
;
2214 filp
->f_version
= 0;
2216 nr_tgids
= get_tgid_list(nr
, next_tgid
, tgid_array
);
2218 /* no more entries ! */
2223 /* do not use the last found pid, reserve it for next_tgid */
2224 if (nr_tgids
== PROC_MAXPIDS
) {
2226 next_tgid
= tgid_array
[nr_tgids
];
2229 for (i
=0;i
<nr_tgids
;i
++) {
2230 int tgid
= tgid_array
[i
];
2231 ino_t ino
= fake_ino(tgid
,PROC_TGID_INO
);
2232 unsigned long j
= PROC_NUMBUF
;
2235 buf
[--j
] = '0' + (tgid
% 10);
2236 while ((tgid
/= 10) != 0);
2238 if (filldir(dirent
, buf
+j
, PROC_NUMBUF
-j
, filp
->f_pos
, ino
, DT_DIR
) < 0) {
2239 /* returning this tgid failed, save it as the first
2240 * pid for the next readir call */
2241 filp
->f_version
= tgid_array
[i
];
2252 /* for the /proc/TGID/task/ directories */
2253 static int proc_task_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
2255 unsigned int tid_array
[PROC_MAXPIDS
];
2256 char buf
[PROC_NUMBUF
];
2257 unsigned int nr_tids
, i
;
2258 struct dentry
*dentry
= filp
->f_dentry
;
2259 struct inode
*inode
= dentry
->d_inode
;
2260 int retval
= -ENOENT
;
2262 unsigned long pos
= filp
->f_pos
; /* avoiding "long long" filp->f_pos */
2264 if (!pid_alive(proc_task(inode
)))
2271 if (filldir(dirent
, ".", 1, pos
, ino
, DT_DIR
) < 0)
2276 ino
= parent_ino(dentry
);
2277 if (filldir(dirent
, "..", 2, pos
, ino
, DT_DIR
) < 0)
2283 nr_tids
= get_tid_list(pos
, tid_array
, inode
);
2284 inode
->i_nlink
= pos
+ nr_tids
;
2286 for (i
= 0; i
< nr_tids
; i
++) {
2287 unsigned long j
= PROC_NUMBUF
;
2288 int tid
= tid_array
[i
];
2290 ino
= fake_ino(tid
,PROC_TID_INO
);
2293 buf
[--j
] = '0' + (tid
% 10);
2294 while ((tid
/= 10) != 0);
2296 if (filldir(dirent
, buf
+j
, PROC_NUMBUF
-j
, pos
, ino
, DT_DIR
) < 0)