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
,
124 PROC_TGID_ATTR_KEYCREATE
,
126 #ifdef CONFIG_AUDITSYSCALL
130 PROC_TGID_OOM_ADJUST
,
134 #ifdef CONFIG_SECCOMP
154 #ifdef CONFIG_SCHEDSTATS
157 #ifdef CONFIG_CPUSETS
160 #ifdef CONFIG_SECURITY
162 PROC_TID_ATTR_CURRENT
,
165 PROC_TID_ATTR_FSCREATE
,
166 PROC_TID_ATTR_KEYCREATE
,
168 #ifdef CONFIG_AUDITSYSCALL
174 /* Add new entries before this */
175 PROC_TID_FD_DIR
= 0x8000, /* 0x8000-0xffff */
185 #define E(type,name,mode) {(type),sizeof(name)-1,(name),(mode)}
187 static struct pid_entry tgid_base_stuff
[] = {
188 E(PROC_TGID_TASK
, "task", S_IFDIR
|S_IRUGO
|S_IXUGO
),
189 E(PROC_TGID_FD
, "fd", S_IFDIR
|S_IRUSR
|S_IXUSR
),
190 E(PROC_TGID_ENVIRON
, "environ", S_IFREG
|S_IRUSR
),
191 E(PROC_TGID_AUXV
, "auxv", S_IFREG
|S_IRUSR
),
192 E(PROC_TGID_STATUS
, "status", S_IFREG
|S_IRUGO
),
193 E(PROC_TGID_CMDLINE
, "cmdline", S_IFREG
|S_IRUGO
),
194 E(PROC_TGID_STAT
, "stat", S_IFREG
|S_IRUGO
),
195 E(PROC_TGID_STATM
, "statm", S_IFREG
|S_IRUGO
),
196 E(PROC_TGID_MAPS
, "maps", S_IFREG
|S_IRUGO
),
198 E(PROC_TGID_NUMA_MAPS
, "numa_maps", S_IFREG
|S_IRUGO
),
200 E(PROC_TGID_MEM
, "mem", S_IFREG
|S_IRUSR
|S_IWUSR
),
201 #ifdef CONFIG_SECCOMP
202 E(PROC_TGID_SECCOMP
, "seccomp", S_IFREG
|S_IRUSR
|S_IWUSR
),
204 E(PROC_TGID_CWD
, "cwd", S_IFLNK
|S_IRWXUGO
),
205 E(PROC_TGID_ROOT
, "root", S_IFLNK
|S_IRWXUGO
),
206 E(PROC_TGID_EXE
, "exe", S_IFLNK
|S_IRWXUGO
),
207 E(PROC_TGID_MOUNTS
, "mounts", S_IFREG
|S_IRUGO
),
208 E(PROC_TGID_MOUNTSTATS
, "mountstats", S_IFREG
|S_IRUSR
),
210 E(PROC_TGID_SMAPS
, "smaps", S_IFREG
|S_IRUGO
),
212 #ifdef CONFIG_SECURITY
213 E(PROC_TGID_ATTR
, "attr", S_IFDIR
|S_IRUGO
|S_IXUGO
),
215 #ifdef CONFIG_KALLSYMS
216 E(PROC_TGID_WCHAN
, "wchan", S_IFREG
|S_IRUGO
),
218 #ifdef CONFIG_SCHEDSTATS
219 E(PROC_TGID_SCHEDSTAT
, "schedstat", S_IFREG
|S_IRUGO
),
221 #ifdef CONFIG_CPUSETS
222 E(PROC_TGID_CPUSET
, "cpuset", S_IFREG
|S_IRUGO
),
224 E(PROC_TGID_OOM_SCORE
, "oom_score",S_IFREG
|S_IRUGO
),
225 E(PROC_TGID_OOM_ADJUST
,"oom_adj", S_IFREG
|S_IRUGO
|S_IWUSR
),
226 #ifdef CONFIG_AUDITSYSCALL
227 E(PROC_TGID_LOGINUID
, "loginuid", S_IFREG
|S_IWUSR
|S_IRUGO
),
231 static struct pid_entry tid_base_stuff
[] = {
232 E(PROC_TID_FD
, "fd", S_IFDIR
|S_IRUSR
|S_IXUSR
),
233 E(PROC_TID_ENVIRON
, "environ", S_IFREG
|S_IRUSR
),
234 E(PROC_TID_AUXV
, "auxv", S_IFREG
|S_IRUSR
),
235 E(PROC_TID_STATUS
, "status", S_IFREG
|S_IRUGO
),
236 E(PROC_TID_CMDLINE
, "cmdline", S_IFREG
|S_IRUGO
),
237 E(PROC_TID_STAT
, "stat", S_IFREG
|S_IRUGO
),
238 E(PROC_TID_STATM
, "statm", S_IFREG
|S_IRUGO
),
239 E(PROC_TID_MAPS
, "maps", S_IFREG
|S_IRUGO
),
241 E(PROC_TID_NUMA_MAPS
, "numa_maps", S_IFREG
|S_IRUGO
),
243 E(PROC_TID_MEM
, "mem", S_IFREG
|S_IRUSR
|S_IWUSR
),
244 #ifdef CONFIG_SECCOMP
245 E(PROC_TID_SECCOMP
, "seccomp", S_IFREG
|S_IRUSR
|S_IWUSR
),
247 E(PROC_TID_CWD
, "cwd", S_IFLNK
|S_IRWXUGO
),
248 E(PROC_TID_ROOT
, "root", S_IFLNK
|S_IRWXUGO
),
249 E(PROC_TID_EXE
, "exe", S_IFLNK
|S_IRWXUGO
),
250 E(PROC_TID_MOUNTS
, "mounts", S_IFREG
|S_IRUGO
),
252 E(PROC_TID_SMAPS
, "smaps", S_IFREG
|S_IRUGO
),
254 #ifdef CONFIG_SECURITY
255 E(PROC_TID_ATTR
, "attr", S_IFDIR
|S_IRUGO
|S_IXUGO
),
257 #ifdef CONFIG_KALLSYMS
258 E(PROC_TID_WCHAN
, "wchan", S_IFREG
|S_IRUGO
),
260 #ifdef CONFIG_SCHEDSTATS
261 E(PROC_TID_SCHEDSTAT
, "schedstat",S_IFREG
|S_IRUGO
),
263 #ifdef CONFIG_CPUSETS
264 E(PROC_TID_CPUSET
, "cpuset", S_IFREG
|S_IRUGO
),
266 E(PROC_TID_OOM_SCORE
, "oom_score",S_IFREG
|S_IRUGO
),
267 E(PROC_TID_OOM_ADJUST
, "oom_adj", S_IFREG
|S_IRUGO
|S_IWUSR
),
268 #ifdef CONFIG_AUDITSYSCALL
269 E(PROC_TID_LOGINUID
, "loginuid", S_IFREG
|S_IWUSR
|S_IRUGO
),
274 #ifdef CONFIG_SECURITY
275 static struct pid_entry tgid_attr_stuff
[] = {
276 E(PROC_TGID_ATTR_CURRENT
, "current", S_IFREG
|S_IRUGO
|S_IWUGO
),
277 E(PROC_TGID_ATTR_PREV
, "prev", S_IFREG
|S_IRUGO
),
278 E(PROC_TGID_ATTR_EXEC
, "exec", S_IFREG
|S_IRUGO
|S_IWUGO
),
279 E(PROC_TGID_ATTR_FSCREATE
, "fscreate", S_IFREG
|S_IRUGO
|S_IWUGO
),
280 E(PROC_TGID_ATTR_KEYCREATE
, "keycreate", S_IFREG
|S_IRUGO
|S_IWUGO
),
283 static struct pid_entry tid_attr_stuff
[] = {
284 E(PROC_TID_ATTR_CURRENT
, "current", S_IFREG
|S_IRUGO
|S_IWUGO
),
285 E(PROC_TID_ATTR_PREV
, "prev", S_IFREG
|S_IRUGO
),
286 E(PROC_TID_ATTR_EXEC
, "exec", S_IFREG
|S_IRUGO
|S_IWUGO
),
287 E(PROC_TID_ATTR_FSCREATE
, "fscreate", S_IFREG
|S_IRUGO
|S_IWUGO
),
288 E(PROC_TID_ATTR_KEYCREATE
, "keycreate", S_IFREG
|S_IRUGO
|S_IWUGO
),
295 static int proc_fd_link(struct inode
*inode
, struct dentry
**dentry
, struct vfsmount
**mnt
)
297 struct task_struct
*task
= proc_task(inode
);
298 struct files_struct
*files
;
300 int fd
= proc_type(inode
) - PROC_TID_FD_DIR
;
302 files
= get_files_struct(task
);
305 * We are not taking a ref to the file structure, so we must
308 spin_lock(&files
->file_lock
);
309 file
= fcheck_files(files
, fd
);
311 *mnt
= mntget(file
->f_vfsmnt
);
312 *dentry
= dget(file
->f_dentry
);
313 spin_unlock(&files
->file_lock
);
314 put_files_struct(files
);
317 spin_unlock(&files
->file_lock
);
318 put_files_struct(files
);
323 static struct fs_struct
*get_fs_struct(struct task_struct
*task
)
325 struct fs_struct
*fs
;
329 atomic_inc(&fs
->count
);
334 static int proc_cwd_link(struct inode
*inode
, struct dentry
**dentry
, struct vfsmount
**mnt
)
336 struct fs_struct
*fs
= get_fs_struct(proc_task(inode
));
337 int result
= -ENOENT
;
339 read_lock(&fs
->lock
);
340 *mnt
= mntget(fs
->pwdmnt
);
341 *dentry
= dget(fs
->pwd
);
342 read_unlock(&fs
->lock
);
349 static int proc_root_link(struct inode
*inode
, struct dentry
**dentry
, struct vfsmount
**mnt
)
351 struct fs_struct
*fs
= get_fs_struct(proc_task(inode
));
352 int result
= -ENOENT
;
354 read_lock(&fs
->lock
);
355 *mnt
= mntget(fs
->rootmnt
);
356 *dentry
= dget(fs
->root
);
357 read_unlock(&fs
->lock
);
365 /* Same as proc_root_link, but this addionally tries to get fs from other
366 * threads in the group */
367 static int proc_task_root_link(struct inode
*inode
, struct dentry
**dentry
,
368 struct vfsmount
**mnt
)
370 struct fs_struct
*fs
;
371 int result
= -ENOENT
;
372 struct task_struct
*leader
= proc_task(inode
);
377 atomic_inc(&fs
->count
);
380 /* Try to get fs from other threads */
382 read_lock(&tasklist_lock
);
383 if (pid_alive(leader
)) {
384 struct task_struct
*task
= leader
;
386 while ((task
= next_thread(task
)) != leader
) {
390 atomic_inc(&fs
->count
);
397 read_unlock(&tasklist_lock
);
401 read_lock(&fs
->lock
);
402 *mnt
= mntget(fs
->rootmnt
);
403 *dentry
= dget(fs
->root
);
404 read_unlock(&fs
->lock
);
412 #define MAY_PTRACE(task) \
413 (task == current || \
414 (task->parent == current && \
415 (task->ptrace & PT_PTRACED) && \
416 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
417 security_ptrace(current,task) == 0))
419 static int proc_pid_environ(struct task_struct
*task
, char * buffer
)
422 struct mm_struct
*mm
= get_task_mm(task
);
424 unsigned int len
= mm
->env_end
- mm
->env_start
;
427 res
= access_process_vm(task
, mm
->env_start
, buffer
, len
, 0);
428 if (!ptrace_may_attach(task
))
435 static int proc_pid_cmdline(struct task_struct
*task
, char * buffer
)
439 struct mm_struct
*mm
= get_task_mm(task
);
443 goto out_mm
; /* Shh! No looking before we're done */
445 len
= mm
->arg_end
- mm
->arg_start
;
450 res
= access_process_vm(task
, mm
->arg_start
, buffer
, len
, 0);
452 // If the nul at the end of args has been overwritten, then
453 // assume application is using setproctitle(3).
454 if (res
> 0 && buffer
[res
-1] != '\0' && len
< PAGE_SIZE
) {
455 len
= strnlen(buffer
, res
);
459 len
= mm
->env_end
- mm
->env_start
;
460 if (len
> PAGE_SIZE
- res
)
461 len
= PAGE_SIZE
- res
;
462 res
+= access_process_vm(task
, mm
->env_start
, buffer
+res
, len
, 0);
463 res
= strnlen(buffer
, res
);
472 static int proc_pid_auxv(struct task_struct
*task
, char *buffer
)
475 struct mm_struct
*mm
= get_task_mm(task
);
477 unsigned int nwords
= 0;
480 while (mm
->saved_auxv
[nwords
- 2] != 0); /* AT_NULL */
481 res
= nwords
* sizeof(mm
->saved_auxv
[0]);
484 memcpy(buffer
, mm
->saved_auxv
, res
);
491 #ifdef CONFIG_KALLSYMS
493 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
494 * Returns the resolved symbol. If that fails, simply return the address.
496 static int proc_pid_wchan(struct task_struct
*task
, char *buffer
)
499 const char *sym_name
;
500 unsigned long wchan
, size
, offset
;
501 char namebuf
[KSYM_NAME_LEN
+1];
503 wchan
= get_wchan(task
);
505 sym_name
= kallsyms_lookup(wchan
, &size
, &offset
, &modname
, namebuf
);
507 return sprintf(buffer
, "%s", sym_name
);
508 return sprintf(buffer
, "%lu", wchan
);
510 #endif /* CONFIG_KALLSYMS */
512 #ifdef CONFIG_SCHEDSTATS
514 * Provides /proc/PID/schedstat
516 static int proc_pid_schedstat(struct task_struct
*task
, char *buffer
)
518 return sprintf(buffer
, "%lu %lu %lu\n",
519 task
->sched_info
.cpu_time
,
520 task
->sched_info
.run_delay
,
521 task
->sched_info
.pcnt
);
525 /* The badness from the OOM killer */
526 unsigned long badness(struct task_struct
*p
, unsigned long uptime
);
527 static int proc_oom_score(struct task_struct
*task
, char *buffer
)
529 unsigned long points
;
530 struct timespec uptime
;
532 do_posix_clock_monotonic_gettime(&uptime
);
533 points
= badness(task
, uptime
.tv_sec
);
534 return sprintf(buffer
, "%lu\n", points
);
537 /************************************************************************/
538 /* Here the fs part begins */
539 /************************************************************************/
541 /* permission checks */
543 /* If the process being read is separated by chroot from the reading process,
544 * don't let the reader access the threads.
546 * note: this does dput(root) and mntput(vfsmnt) on exit.
548 static int proc_check_chroot(struct dentry
*root
, struct vfsmount
*vfsmnt
)
550 struct dentry
*de
, *base
;
551 struct vfsmount
*our_vfsmnt
, *mnt
;
554 read_lock(¤t
->fs
->lock
);
555 our_vfsmnt
= mntget(current
->fs
->rootmnt
);
556 base
= dget(current
->fs
->root
);
557 read_unlock(¤t
->fs
->lock
);
559 spin_lock(&vfsmount_lock
);
563 while (mnt
!= our_vfsmnt
) {
564 if (mnt
== mnt
->mnt_parent
)
566 de
= mnt
->mnt_mountpoint
;
567 mnt
= mnt
->mnt_parent
;
570 if (!is_subdir(de
, base
))
572 spin_unlock(&vfsmount_lock
);
581 spin_unlock(&vfsmount_lock
);
586 static int proc_check_root(struct inode
*inode
)
589 struct vfsmount
*vfsmnt
;
591 if (proc_root_link(inode
, &root
, &vfsmnt
)) /* Ewww... */
593 return proc_check_chroot(root
, vfsmnt
);
596 static int proc_permission(struct inode
*inode
, int mask
, struct nameidata
*nd
)
598 if (generic_permission(inode
, mask
, NULL
) != 0)
600 return proc_check_root(inode
);
603 static int proc_task_permission(struct inode
*inode
, int mask
, struct nameidata
*nd
)
606 struct vfsmount
*vfsmnt
;
608 if (generic_permission(inode
, mask
, NULL
) != 0)
611 if (proc_task_root_link(inode
, &root
, &vfsmnt
))
614 return proc_check_chroot(root
, vfsmnt
);
617 extern struct seq_operations proc_pid_maps_op
;
618 static int maps_open(struct inode
*inode
, struct file
*file
)
620 struct task_struct
*task
= proc_task(inode
);
621 int ret
= seq_open(file
, &proc_pid_maps_op
);
623 struct seq_file
*m
= file
->private_data
;
629 static struct file_operations proc_maps_operations
= {
633 .release
= seq_release
,
637 extern struct seq_operations proc_pid_numa_maps_op
;
638 static int numa_maps_open(struct inode
*inode
, struct file
*file
)
640 struct task_struct
*task
= proc_task(inode
);
641 int ret
= seq_open(file
, &proc_pid_numa_maps_op
);
643 struct seq_file
*m
= file
->private_data
;
649 static struct file_operations proc_numa_maps_operations
= {
650 .open
= numa_maps_open
,
653 .release
= seq_release
,
658 extern struct seq_operations proc_pid_smaps_op
;
659 static int smaps_open(struct inode
*inode
, struct file
*file
)
661 struct task_struct
*task
= proc_task(inode
);
662 int ret
= seq_open(file
, &proc_pid_smaps_op
);
664 struct seq_file
*m
= file
->private_data
;
670 static struct file_operations proc_smaps_operations
= {
674 .release
= seq_release
,
678 extern struct seq_operations mounts_op
;
684 static int mounts_open(struct inode
*inode
, struct file
*file
)
686 struct task_struct
*task
= proc_task(inode
);
687 struct namespace *namespace;
688 struct proc_mounts
*p
;
692 namespace = task
->namespace;
694 get_namespace(namespace);
699 p
= kmalloc(sizeof(struct proc_mounts
), GFP_KERNEL
);
701 file
->private_data
= &p
->m
;
702 ret
= seq_open(file
, &mounts_op
);
704 p
->m
.private = namespace;
705 p
->event
= namespace->event
;
710 put_namespace(namespace);
715 static int mounts_release(struct inode
*inode
, struct file
*file
)
717 struct seq_file
*m
= file
->private_data
;
718 struct namespace *namespace = m
->private;
719 put_namespace(namespace);
720 return seq_release(inode
, file
);
723 static unsigned mounts_poll(struct file
*file
, poll_table
*wait
)
725 struct proc_mounts
*p
= file
->private_data
;
726 struct namespace *ns
= p
->m
.private;
729 poll_wait(file
, &ns
->poll
, wait
);
731 spin_lock(&vfsmount_lock
);
732 if (p
->event
!= ns
->event
) {
733 p
->event
= ns
->event
;
736 spin_unlock(&vfsmount_lock
);
741 static struct file_operations proc_mounts_operations
= {
745 .release
= mounts_release
,
749 extern struct seq_operations mountstats_op
;
750 static int mountstats_open(struct inode
*inode
, struct file
*file
)
752 struct task_struct
*task
= proc_task(inode
);
753 int ret
= seq_open(file
, &mountstats_op
);
756 struct seq_file
*m
= file
->private_data
;
757 struct namespace *namespace;
759 namespace = task
->namespace;
761 get_namespace(namespace);
765 m
->private = namespace;
767 seq_release(inode
, file
);
774 static struct file_operations proc_mountstats_operations
= {
775 .open
= mountstats_open
,
778 .release
= mounts_release
,
781 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
783 static ssize_t
proc_info_read(struct file
* file
, char __user
* buf
,
784 size_t count
, loff_t
*ppos
)
786 struct inode
* inode
= file
->f_dentry
->d_inode
;
789 struct task_struct
*task
= proc_task(inode
);
791 if (count
> PROC_BLOCK_SIZE
)
792 count
= PROC_BLOCK_SIZE
;
793 if (!(page
= __get_free_page(GFP_KERNEL
)))
796 length
= PROC_I(inode
)->op
.proc_read(task
, (char*)page
);
799 length
= simple_read_from_buffer(buf
, count
, ppos
, (char *)page
, length
);
804 static struct file_operations proc_info_file_operations
= {
805 .read
= proc_info_read
,
808 static int mem_open(struct inode
* inode
, struct file
* file
)
810 file
->private_data
= (void*)((long)current
->self_exec_id
);
814 static ssize_t
mem_read(struct file
* file
, char __user
* buf
,
815 size_t count
, loff_t
*ppos
)
817 struct task_struct
*task
= proc_task(file
->f_dentry
->d_inode
);
819 unsigned long src
= *ppos
;
821 struct mm_struct
*mm
;
823 if (!MAY_PTRACE(task
) || !ptrace_may_attach(task
))
827 page
= (char *)__get_free_page(GFP_USER
);
833 mm
= get_task_mm(task
);
839 if (file
->private_data
!= (void*)((long)current
->self_exec_id
))
845 int this_len
, retval
;
847 this_len
= (count
> PAGE_SIZE
) ? PAGE_SIZE
: count
;
848 retval
= access_process_vm(task
, src
, page
, this_len
, 0);
849 if (!retval
|| !MAY_PTRACE(task
) || !ptrace_may_attach(task
)) {
855 if (copy_to_user(buf
, page
, retval
)) {
870 free_page((unsigned long) page
);
875 #define mem_write NULL
878 /* This is a security hazard */
879 static ssize_t
mem_write(struct file
* file
, const char * buf
,
880 size_t count
, loff_t
*ppos
)
884 struct task_struct
*task
= proc_task(file
->f_dentry
->d_inode
);
885 unsigned long dst
= *ppos
;
887 if (!MAY_PTRACE(task
) || !ptrace_may_attach(task
))
890 page
= (char *)__get_free_page(GFP_USER
);
895 int this_len
, retval
;
897 this_len
= (count
> PAGE_SIZE
) ? PAGE_SIZE
: count
;
898 if (copy_from_user(page
, buf
, this_len
)) {
902 retval
= access_process_vm(task
, dst
, page
, this_len
, 1);
914 free_page((unsigned long) page
);
919 static loff_t
mem_lseek(struct file
* file
, loff_t offset
, int orig
)
923 file
->f_pos
= offset
;
926 file
->f_pos
+= offset
;
931 force_successful_syscall_return();
935 static struct file_operations proc_mem_operations
= {
942 static ssize_t
oom_adjust_read(struct file
*file
, char __user
*buf
,
943 size_t count
, loff_t
*ppos
)
945 struct task_struct
*task
= proc_task(file
->f_dentry
->d_inode
);
948 int oom_adjust
= task
->oomkilladj
;
949 loff_t __ppos
= *ppos
;
951 len
= sprintf(buffer
, "%i\n", oom_adjust
);
954 if (count
> len
-__ppos
)
956 if (copy_to_user(buf
, buffer
+ __ppos
, count
))
958 *ppos
= __ppos
+ count
;
962 static ssize_t
oom_adjust_write(struct file
*file
, const char __user
*buf
,
963 size_t count
, loff_t
*ppos
)
965 struct task_struct
*task
= proc_task(file
->f_dentry
->d_inode
);
966 char buffer
[8], *end
;
969 if (!capable(CAP_SYS_RESOURCE
))
971 memset(buffer
, 0, 8);
974 if (copy_from_user(buffer
, buf
, count
))
976 oom_adjust
= simple_strtol(buffer
, &end
, 0);
977 if ((oom_adjust
< -16 || oom_adjust
> 15) && oom_adjust
!= OOM_DISABLE
)
981 task
->oomkilladj
= oom_adjust
;
982 if (end
- buffer
== 0)
987 static struct file_operations proc_oom_adjust_operations
= {
988 .read
= oom_adjust_read
,
989 .write
= oom_adjust_write
,
992 static struct inode_operations proc_mem_inode_operations
= {
993 .permission
= proc_permission
,
996 #ifdef CONFIG_AUDITSYSCALL
998 static ssize_t
proc_loginuid_read(struct file
* file
, char __user
* buf
,
999 size_t count
, loff_t
*ppos
)
1001 struct inode
* inode
= file
->f_dentry
->d_inode
;
1002 struct task_struct
*task
= proc_task(inode
);
1004 char tmpbuf
[TMPBUFLEN
];
1006 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
1007 audit_get_loginuid(task
->audit_context
));
1008 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1011 static ssize_t
proc_loginuid_write(struct file
* file
, const char __user
* buf
,
1012 size_t count
, loff_t
*ppos
)
1014 struct inode
* inode
= file
->f_dentry
->d_inode
;
1017 struct task_struct
*task
= proc_task(inode
);
1020 if (!capable(CAP_AUDIT_CONTROL
))
1023 if (current
!= task
)
1026 if (count
>= PAGE_SIZE
)
1027 count
= PAGE_SIZE
- 1;
1030 /* No partial writes. */
1033 page
= (char*)__get_free_page(GFP_USER
);
1037 if (copy_from_user(page
, buf
, count
))
1041 loginuid
= simple_strtoul(page
, &tmp
, 10);
1047 length
= audit_set_loginuid(task
, loginuid
);
1048 if (likely(length
== 0))
1052 free_page((unsigned long) page
);
1056 static struct file_operations proc_loginuid_operations
= {
1057 .read
= proc_loginuid_read
,
1058 .write
= proc_loginuid_write
,
1062 #ifdef CONFIG_SECCOMP
1063 static ssize_t
seccomp_read(struct file
*file
, char __user
*buf
,
1064 size_t count
, loff_t
*ppos
)
1066 struct task_struct
*tsk
= proc_task(file
->f_dentry
->d_inode
);
1068 loff_t __ppos
= *ppos
;
1071 /* no need to print the trailing zero, so use only len */
1072 len
= sprintf(__buf
, "%u\n", tsk
->seccomp
.mode
);
1075 if (count
> len
- __ppos
)
1076 count
= len
- __ppos
;
1077 if (copy_to_user(buf
, __buf
+ __ppos
, count
))
1079 *ppos
= __ppos
+ count
;
1083 static ssize_t
seccomp_write(struct file
*file
, const char __user
*buf
,
1084 size_t count
, loff_t
*ppos
)
1086 struct task_struct
*tsk
= proc_task(file
->f_dentry
->d_inode
);
1087 char __buf
[20], *end
;
1088 unsigned int seccomp_mode
;
1090 /* can set it only once to be even more secure */
1091 if (unlikely(tsk
->seccomp
.mode
))
1094 memset(__buf
, 0, sizeof(__buf
));
1095 count
= min(count
, sizeof(__buf
) - 1);
1096 if (copy_from_user(__buf
, buf
, count
))
1098 seccomp_mode
= simple_strtoul(__buf
, &end
, 0);
1101 if (seccomp_mode
&& seccomp_mode
<= NR_SECCOMP_MODES
) {
1102 tsk
->seccomp
.mode
= seccomp_mode
;
1103 set_tsk_thread_flag(tsk
, TIF_SECCOMP
);
1106 if (unlikely(!(end
- __buf
)))
1111 static struct file_operations proc_seccomp_operations
= {
1112 .read
= seccomp_read
,
1113 .write
= seccomp_write
,
1115 #endif /* CONFIG_SECCOMP */
1117 static void *proc_pid_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
1119 struct inode
*inode
= dentry
->d_inode
;
1120 int error
= -EACCES
;
1122 /* We don't need a base pointer in the /proc filesystem */
1125 if (current
->fsuid
!= inode
->i_uid
&& !capable(CAP_DAC_OVERRIDE
))
1127 error
= proc_check_root(inode
);
1131 error
= PROC_I(inode
)->op
.proc_get_link(inode
, &nd
->dentry
, &nd
->mnt
);
1132 nd
->last_type
= LAST_BIND
;
1134 return ERR_PTR(error
);
1137 static int do_proc_readlink(struct dentry
*dentry
, struct vfsmount
*mnt
,
1138 char __user
*buffer
, int buflen
)
1140 struct inode
* inode
;
1141 char *tmp
= (char*)__get_free_page(GFP_KERNEL
), *path
;
1147 inode
= dentry
->d_inode
;
1148 path
= d_path(dentry
, mnt
, tmp
, PAGE_SIZE
);
1149 len
= PTR_ERR(path
);
1152 len
= tmp
+ PAGE_SIZE
- 1 - path
;
1156 if (copy_to_user(buffer
, path
, len
))
1159 free_page((unsigned long)tmp
);
1163 static int proc_pid_readlink(struct dentry
* dentry
, char __user
* buffer
, int buflen
)
1165 int error
= -EACCES
;
1166 struct inode
*inode
= dentry
->d_inode
;
1168 struct vfsmount
*mnt
= NULL
;
1171 if (current
->fsuid
!= inode
->i_uid
&& !capable(CAP_DAC_OVERRIDE
))
1173 error
= proc_check_root(inode
);
1177 error
= PROC_I(inode
)->op
.proc_get_link(inode
, &de
, &mnt
);
1181 error
= do_proc_readlink(de
, mnt
, buffer
, buflen
);
1188 static struct inode_operations proc_pid_link_inode_operations
= {
1189 .readlink
= proc_pid_readlink
,
1190 .follow_link
= proc_pid_follow_link
1195 static int proc_readfd(struct file
* filp
, void * dirent
, filldir_t filldir
)
1197 struct dentry
*dentry
= filp
->f_dentry
;
1198 struct inode
*inode
= dentry
->d_inode
;
1199 struct task_struct
*p
= proc_task(inode
);
1200 unsigned int fd
, tid
, ino
;
1203 struct files_struct
* files
;
1204 struct fdtable
*fdt
;
1215 if (filldir(dirent
, ".", 1, 0, inode
->i_ino
, DT_DIR
) < 0)
1219 ino
= parent_ino(dentry
);
1220 if (filldir(dirent
, "..", 2, 1, ino
, DT_DIR
) < 0)
1224 files
= get_files_struct(p
);
1228 fdt
= files_fdtable(files
);
1229 for (fd
= filp
->f_pos
-2;
1231 fd
++, filp
->f_pos
++) {
1234 if (!fcheck_files(files
, fd
))
1242 buf
[j
] = '0' + (i
% 10);
1246 ino
= fake_ino(tid
, PROC_TID_FD_DIR
+ fd
);
1247 if (filldir(dirent
, buf
+j
, NUMBUF
-j
, fd
+2, ino
, DT_LNK
) < 0) {
1254 put_files_struct(files
);
1260 static int proc_pident_readdir(struct file
*filp
,
1261 void *dirent
, filldir_t filldir
,
1262 struct pid_entry
*ents
, unsigned int nents
)
1266 struct dentry
*dentry
= filp
->f_dentry
;
1267 struct inode
*inode
= dentry
->d_inode
;
1268 struct pid_entry
*p
;
1273 if (!pid_alive(proc_task(inode
)))
1277 pid
= proc_task(inode
)->pid
;
1282 if (filldir(dirent
, ".", 1, i
, ino
, DT_DIR
) < 0)
1288 ino
= parent_ino(dentry
);
1289 if (filldir(dirent
, "..", 2, i
, ino
, DT_DIR
) < 0)
1302 if (filldir(dirent
, p
->name
, p
->len
, filp
->f_pos
,
1303 fake_ino(pid
, p
->type
), p
->mode
>> 12) < 0)
1315 static int proc_tgid_base_readdir(struct file
* filp
,
1316 void * dirent
, filldir_t filldir
)
1318 return proc_pident_readdir(filp
,dirent
,filldir
,
1319 tgid_base_stuff
,ARRAY_SIZE(tgid_base_stuff
));
1322 static int proc_tid_base_readdir(struct file
* filp
,
1323 void * dirent
, filldir_t filldir
)
1325 return proc_pident_readdir(filp
,dirent
,filldir
,
1326 tid_base_stuff
,ARRAY_SIZE(tid_base_stuff
));
1329 /* building an inode */
1331 static int task_dumpable(struct task_struct
*task
)
1334 struct mm_struct
*mm
;
1339 dumpable
= mm
->dumpable
;
1347 static struct inode
*proc_pid_make_inode(struct super_block
* sb
, struct task_struct
*task
, int ino
)
1349 struct inode
* inode
;
1350 struct proc_inode
*ei
;
1352 /* We need a new inode */
1354 inode
= new_inode(sb
);
1360 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1361 inode
->i_ino
= fake_ino(task
->pid
, ino
);
1363 if (!pid_alive(task
))
1367 * grab the reference to task.
1369 get_task_struct(task
);
1374 if (ino
== PROC_TGID_INO
|| ino
== PROC_TID_INO
|| task_dumpable(task
)) {
1375 inode
->i_uid
= task
->euid
;
1376 inode
->i_gid
= task
->egid
;
1378 security_task_to_inode(task
, inode
);
1391 * Exceptional case: normally we are not allowed to unhash a busy
1392 * directory. In this case, however, we can do it - no aliasing problems
1393 * due to the way we treat inodes.
1395 * Rewrite the inode's ownerships here because the owning task may have
1396 * performed a setuid(), etc.
1398 static int pid_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1400 struct inode
*inode
= dentry
->d_inode
;
1401 struct task_struct
*task
= proc_task(inode
);
1402 if (pid_alive(task
)) {
1403 if (proc_type(inode
) == PROC_TGID_INO
|| proc_type(inode
) == PROC_TID_INO
|| task_dumpable(task
)) {
1404 inode
->i_uid
= task
->euid
;
1405 inode
->i_gid
= task
->egid
;
1410 security_task_to_inode(task
, inode
);
1417 static int tid_fd_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1419 struct inode
*inode
= dentry
->d_inode
;
1420 struct task_struct
*task
= proc_task(inode
);
1421 int fd
= proc_type(inode
) - PROC_TID_FD_DIR
;
1422 struct files_struct
*files
;
1424 files
= get_files_struct(task
);
1427 if (fcheck_files(files
, fd
)) {
1429 put_files_struct(files
);
1430 if (task_dumpable(task
)) {
1431 inode
->i_uid
= task
->euid
;
1432 inode
->i_gid
= task
->egid
;
1437 security_task_to_inode(task
, inode
);
1441 put_files_struct(files
);
1447 static void pid_base_iput(struct dentry
*dentry
, struct inode
*inode
)
1449 struct task_struct
*task
= proc_task(inode
);
1450 spin_lock(&task
->proc_lock
);
1451 if (task
->proc_dentry
== dentry
)
1452 task
->proc_dentry
= NULL
;
1453 spin_unlock(&task
->proc_lock
);
1457 static int pid_delete_dentry(struct dentry
* dentry
)
1459 /* Is the task we represent dead?
1460 * If so, then don't put the dentry on the lru list,
1461 * kill it immediately.
1463 return !pid_alive(proc_task(dentry
->d_inode
));
1466 static struct dentry_operations tid_fd_dentry_operations
=
1468 .d_revalidate
= tid_fd_revalidate
,
1469 .d_delete
= pid_delete_dentry
,
1472 static struct dentry_operations pid_dentry_operations
=
1474 .d_revalidate
= pid_revalidate
,
1475 .d_delete
= pid_delete_dentry
,
1478 static struct dentry_operations pid_base_dentry_operations
=
1480 .d_revalidate
= pid_revalidate
,
1481 .d_iput
= pid_base_iput
,
1482 .d_delete
= pid_delete_dentry
,
1487 static unsigned name_to_int(struct dentry
*dentry
)
1489 const char *name
= dentry
->d_name
.name
;
1490 int len
= dentry
->d_name
.len
;
1493 if (len
> 1 && *name
== '0')
1496 unsigned c
= *name
++ - '0';
1499 if (n
>= (~0U-9)/10)
1510 static struct dentry
*proc_lookupfd(struct inode
* dir
, struct dentry
* dentry
, struct nameidata
*nd
)
1512 struct task_struct
*task
= proc_task(dir
);
1513 unsigned fd
= name_to_int(dentry
);
1515 struct files_struct
* files
;
1516 struct inode
*inode
;
1517 struct proc_inode
*ei
;
1521 if (!pid_alive(task
))
1524 inode
= proc_pid_make_inode(dir
->i_sb
, task
, PROC_TID_FD_DIR
+fd
);
1528 files
= get_files_struct(task
);
1531 inode
->i_mode
= S_IFLNK
;
1534 * We are not taking a ref to the file structure, so we must
1537 spin_lock(&files
->file_lock
);
1538 file
= fcheck_files(files
, fd
);
1541 if (file
->f_mode
& 1)
1542 inode
->i_mode
|= S_IRUSR
| S_IXUSR
;
1543 if (file
->f_mode
& 2)
1544 inode
->i_mode
|= S_IWUSR
| S_IXUSR
;
1545 spin_unlock(&files
->file_lock
);
1546 put_files_struct(files
);
1547 inode
->i_op
= &proc_pid_link_inode_operations
;
1549 ei
->op
.proc_get_link
= proc_fd_link
;
1550 dentry
->d_op
= &tid_fd_dentry_operations
;
1551 d_add(dentry
, inode
);
1555 spin_unlock(&files
->file_lock
);
1556 put_files_struct(files
);
1560 return ERR_PTR(-ENOENT
);
1563 static int proc_task_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
);
1564 static struct dentry
*proc_task_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
);
1566 static struct file_operations proc_fd_operations
= {
1567 .read
= generic_read_dir
,
1568 .readdir
= proc_readfd
,
1571 static struct file_operations proc_task_operations
= {
1572 .read
= generic_read_dir
,
1573 .readdir
= proc_task_readdir
,
1577 * proc directories can do almost nothing..
1579 static struct inode_operations proc_fd_inode_operations
= {
1580 .lookup
= proc_lookupfd
,
1581 .permission
= proc_permission
,
1584 static struct inode_operations proc_task_inode_operations
= {
1585 .lookup
= proc_task_lookup
,
1586 .permission
= proc_task_permission
,
1589 #ifdef CONFIG_SECURITY
1590 static ssize_t
proc_pid_attr_read(struct file
* file
, char __user
* buf
,
1591 size_t count
, loff_t
*ppos
)
1593 struct inode
* inode
= file
->f_dentry
->d_inode
;
1596 struct task_struct
*task
= proc_task(inode
);
1598 if (count
> PAGE_SIZE
)
1600 if (!(page
= __get_free_page(GFP_KERNEL
)))
1603 length
= security_getprocattr(task
,
1604 (char*)file
->f_dentry
->d_name
.name
,
1605 (void*)page
, count
);
1607 length
= simple_read_from_buffer(buf
, count
, ppos
, (char *)page
, length
);
1612 static ssize_t
proc_pid_attr_write(struct file
* file
, const char __user
* buf
,
1613 size_t count
, loff_t
*ppos
)
1615 struct inode
* inode
= file
->f_dentry
->d_inode
;
1618 struct task_struct
*task
= proc_task(inode
);
1620 if (count
> PAGE_SIZE
)
1623 /* No partial writes. */
1626 page
= (char*)__get_free_page(GFP_USER
);
1630 if (copy_from_user(page
, buf
, count
))
1633 length
= security_setprocattr(task
,
1634 (char*)file
->f_dentry
->d_name
.name
,
1635 (void*)page
, count
);
1637 free_page((unsigned long) page
);
1641 static struct file_operations proc_pid_attr_operations
= {
1642 .read
= proc_pid_attr_read
,
1643 .write
= proc_pid_attr_write
,
1646 static struct file_operations proc_tid_attr_operations
;
1647 static struct inode_operations proc_tid_attr_inode_operations
;
1648 static struct file_operations proc_tgid_attr_operations
;
1649 static struct inode_operations proc_tgid_attr_inode_operations
;
1652 static int get_tid_list(int index
, unsigned int *tids
, struct inode
*dir
);
1655 static struct dentry
*proc_pident_lookup(struct inode
*dir
,
1656 struct dentry
*dentry
,
1657 struct pid_entry
*ents
)
1659 struct inode
*inode
;
1661 struct task_struct
*task
= proc_task(dir
);
1662 struct pid_entry
*p
;
1663 struct proc_inode
*ei
;
1668 if (!pid_alive(task
))
1671 for (p
= ents
; p
->name
; p
++) {
1672 if (p
->len
!= dentry
->d_name
.len
)
1674 if (!memcmp(dentry
->d_name
.name
, p
->name
, p
->len
))
1681 inode
= proc_pid_make_inode(dir
->i_sb
, task
, p
->type
);
1686 inode
->i_mode
= p
->mode
;
1688 * Yes, it does not scale. And it should not. Don't add
1689 * new entries into /proc/<tgid>/ without very good reasons.
1692 case PROC_TGID_TASK
:
1693 inode
->i_nlink
= 2 + get_tid_list(2, NULL
, dir
);
1694 inode
->i_op
= &proc_task_inode_operations
;
1695 inode
->i_fop
= &proc_task_operations
;
1700 inode
->i_op
= &proc_fd_inode_operations
;
1701 inode
->i_fop
= &proc_fd_operations
;
1705 inode
->i_op
= &proc_pid_link_inode_operations
;
1706 ei
->op
.proc_get_link
= proc_exe_link
;
1710 inode
->i_op
= &proc_pid_link_inode_operations
;
1711 ei
->op
.proc_get_link
= proc_cwd_link
;
1714 case PROC_TGID_ROOT
:
1715 inode
->i_op
= &proc_pid_link_inode_operations
;
1716 ei
->op
.proc_get_link
= proc_root_link
;
1718 case PROC_TID_ENVIRON
:
1719 case PROC_TGID_ENVIRON
:
1720 inode
->i_fop
= &proc_info_file_operations
;
1721 ei
->op
.proc_read
= proc_pid_environ
;
1724 case PROC_TGID_AUXV
:
1725 inode
->i_fop
= &proc_info_file_operations
;
1726 ei
->op
.proc_read
= proc_pid_auxv
;
1728 case PROC_TID_STATUS
:
1729 case PROC_TGID_STATUS
:
1730 inode
->i_fop
= &proc_info_file_operations
;
1731 ei
->op
.proc_read
= proc_pid_status
;
1734 inode
->i_fop
= &proc_info_file_operations
;
1735 ei
->op
.proc_read
= proc_tid_stat
;
1737 case PROC_TGID_STAT
:
1738 inode
->i_fop
= &proc_info_file_operations
;
1739 ei
->op
.proc_read
= proc_tgid_stat
;
1741 case PROC_TID_CMDLINE
:
1742 case PROC_TGID_CMDLINE
:
1743 inode
->i_fop
= &proc_info_file_operations
;
1744 ei
->op
.proc_read
= proc_pid_cmdline
;
1746 case PROC_TID_STATM
:
1747 case PROC_TGID_STATM
:
1748 inode
->i_fop
= &proc_info_file_operations
;
1749 ei
->op
.proc_read
= proc_pid_statm
;
1752 case PROC_TGID_MAPS
:
1753 inode
->i_fop
= &proc_maps_operations
;
1756 case PROC_TID_NUMA_MAPS
:
1757 case PROC_TGID_NUMA_MAPS
:
1758 inode
->i_fop
= &proc_numa_maps_operations
;
1763 inode
->i_op
= &proc_mem_inode_operations
;
1764 inode
->i_fop
= &proc_mem_operations
;
1766 #ifdef CONFIG_SECCOMP
1767 case PROC_TID_SECCOMP
:
1768 case PROC_TGID_SECCOMP
:
1769 inode
->i_fop
= &proc_seccomp_operations
;
1771 #endif /* CONFIG_SECCOMP */
1772 case PROC_TID_MOUNTS
:
1773 case PROC_TGID_MOUNTS
:
1774 inode
->i_fop
= &proc_mounts_operations
;
1777 case PROC_TID_SMAPS
:
1778 case PROC_TGID_SMAPS
:
1779 inode
->i_fop
= &proc_smaps_operations
;
1782 case PROC_TID_MOUNTSTATS
:
1783 case PROC_TGID_MOUNTSTATS
:
1784 inode
->i_fop
= &proc_mountstats_operations
;
1786 #ifdef CONFIG_SECURITY
1789 inode
->i_op
= &proc_tid_attr_inode_operations
;
1790 inode
->i_fop
= &proc_tid_attr_operations
;
1792 case PROC_TGID_ATTR
:
1794 inode
->i_op
= &proc_tgid_attr_inode_operations
;
1795 inode
->i_fop
= &proc_tgid_attr_operations
;
1797 case PROC_TID_ATTR_CURRENT
:
1798 case PROC_TGID_ATTR_CURRENT
:
1799 case PROC_TID_ATTR_PREV
:
1800 case PROC_TGID_ATTR_PREV
:
1801 case PROC_TID_ATTR_EXEC
:
1802 case PROC_TGID_ATTR_EXEC
:
1803 case PROC_TID_ATTR_FSCREATE
:
1804 case PROC_TGID_ATTR_FSCREATE
:
1805 case PROC_TID_ATTR_KEYCREATE
:
1806 case PROC_TGID_ATTR_KEYCREATE
:
1807 inode
->i_fop
= &proc_pid_attr_operations
;
1810 #ifdef CONFIG_KALLSYMS
1811 case PROC_TID_WCHAN
:
1812 case PROC_TGID_WCHAN
:
1813 inode
->i_fop
= &proc_info_file_operations
;
1814 ei
->op
.proc_read
= proc_pid_wchan
;
1817 #ifdef CONFIG_SCHEDSTATS
1818 case PROC_TID_SCHEDSTAT
:
1819 case PROC_TGID_SCHEDSTAT
:
1820 inode
->i_fop
= &proc_info_file_operations
;
1821 ei
->op
.proc_read
= proc_pid_schedstat
;
1824 #ifdef CONFIG_CPUSETS
1825 case PROC_TID_CPUSET
:
1826 case PROC_TGID_CPUSET
:
1827 inode
->i_fop
= &proc_cpuset_operations
;
1830 case PROC_TID_OOM_SCORE
:
1831 case PROC_TGID_OOM_SCORE
:
1832 inode
->i_fop
= &proc_info_file_operations
;
1833 ei
->op
.proc_read
= proc_oom_score
;
1835 case PROC_TID_OOM_ADJUST
:
1836 case PROC_TGID_OOM_ADJUST
:
1837 inode
->i_fop
= &proc_oom_adjust_operations
;
1839 #ifdef CONFIG_AUDITSYSCALL
1840 case PROC_TID_LOGINUID
:
1841 case PROC_TGID_LOGINUID
:
1842 inode
->i_fop
= &proc_loginuid_operations
;
1846 printk("procfs: impossible type (%d)",p
->type
);
1848 return ERR_PTR(-EINVAL
);
1850 dentry
->d_op
= &pid_dentry_operations
;
1851 d_add(dentry
, inode
);
1855 return ERR_PTR(error
);
1858 static struct dentry
*proc_tgid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
){
1859 return proc_pident_lookup(dir
, dentry
, tgid_base_stuff
);
1862 static struct dentry
*proc_tid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
){
1863 return proc_pident_lookup(dir
, dentry
, tid_base_stuff
);
1866 static struct file_operations proc_tgid_base_operations
= {
1867 .read
= generic_read_dir
,
1868 .readdir
= proc_tgid_base_readdir
,
1871 static struct file_operations proc_tid_base_operations
= {
1872 .read
= generic_read_dir
,
1873 .readdir
= proc_tid_base_readdir
,
1876 static struct inode_operations proc_tgid_base_inode_operations
= {
1877 .lookup
= proc_tgid_base_lookup
,
1880 static struct inode_operations proc_tid_base_inode_operations
= {
1881 .lookup
= proc_tid_base_lookup
,
1884 #ifdef CONFIG_SECURITY
1885 static int proc_tgid_attr_readdir(struct file
* filp
,
1886 void * dirent
, filldir_t filldir
)
1888 return proc_pident_readdir(filp
,dirent
,filldir
,
1889 tgid_attr_stuff
,ARRAY_SIZE(tgid_attr_stuff
));
1892 static int proc_tid_attr_readdir(struct file
* filp
,
1893 void * dirent
, filldir_t filldir
)
1895 return proc_pident_readdir(filp
,dirent
,filldir
,
1896 tid_attr_stuff
,ARRAY_SIZE(tid_attr_stuff
));
1899 static struct file_operations proc_tgid_attr_operations
= {
1900 .read
= generic_read_dir
,
1901 .readdir
= proc_tgid_attr_readdir
,
1904 static struct file_operations proc_tid_attr_operations
= {
1905 .read
= generic_read_dir
,
1906 .readdir
= proc_tid_attr_readdir
,
1909 static struct dentry
*proc_tgid_attr_lookup(struct inode
*dir
,
1910 struct dentry
*dentry
, struct nameidata
*nd
)
1912 return proc_pident_lookup(dir
, dentry
, tgid_attr_stuff
);
1915 static struct dentry
*proc_tid_attr_lookup(struct inode
*dir
,
1916 struct dentry
*dentry
, struct nameidata
*nd
)
1918 return proc_pident_lookup(dir
, dentry
, tid_attr_stuff
);
1921 static struct inode_operations proc_tgid_attr_inode_operations
= {
1922 .lookup
= proc_tgid_attr_lookup
,
1925 static struct inode_operations proc_tid_attr_inode_operations
= {
1926 .lookup
= proc_tid_attr_lookup
,
1933 static int proc_self_readlink(struct dentry
*dentry
, char __user
*buffer
,
1937 sprintf(tmp
, "%d", current
->tgid
);
1938 return vfs_readlink(dentry
,buffer
,buflen
,tmp
);
1941 static void *proc_self_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
1944 sprintf(tmp
, "%d", current
->tgid
);
1945 return ERR_PTR(vfs_follow_link(nd
,tmp
));
1948 static struct inode_operations proc_self_inode_operations
= {
1949 .readlink
= proc_self_readlink
,
1950 .follow_link
= proc_self_follow_link
,
1954 * proc_pid_unhash - Unhash /proc/@pid entry from the dcache.
1955 * @p: task that should be flushed.
1957 * Drops the /proc/@pid dcache entry from the hash chains.
1959 * Dropping /proc/@pid entries and detach_pid must be synchroneous,
1960 * otherwise e.g. /proc/@pid/exe might point to the wrong executable,
1961 * if the pid value is immediately reused. This is enforced by
1962 * - caller must acquire spin_lock(p->proc_lock)
1963 * - must be called before detach_pid()
1964 * - proc_pid_lookup acquires proc_lock, and checks that
1965 * the target is not dead by looking at the attach count
1969 struct dentry
*proc_pid_unhash(struct task_struct
*p
)
1971 struct dentry
*proc_dentry
;
1973 proc_dentry
= p
->proc_dentry
;
1974 if (proc_dentry
!= NULL
) {
1976 spin_lock(&dcache_lock
);
1977 spin_lock(&proc_dentry
->d_lock
);
1978 if (!d_unhashed(proc_dentry
)) {
1979 dget_locked(proc_dentry
);
1980 __d_drop(proc_dentry
);
1981 spin_unlock(&proc_dentry
->d_lock
);
1983 spin_unlock(&proc_dentry
->d_lock
);
1986 spin_unlock(&dcache_lock
);
1992 * proc_pid_flush - recover memory used by stale /proc/@pid/x entries
1993 * @proc_dentry: directoy to prune.
1995 * Shrink the /proc directory that was used by the just killed thread.
1998 void proc_pid_flush(struct dentry
*proc_dentry
)
2001 if(proc_dentry
!= NULL
) {
2002 shrink_dcache_parent(proc_dentry
);
2008 struct dentry
*proc_pid_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
)
2010 struct task_struct
*task
;
2011 struct inode
*inode
;
2012 struct proc_inode
*ei
;
2016 if (dentry
->d_name
.len
== 4 && !memcmp(dentry
->d_name
.name
,"self",4)) {
2017 inode
= new_inode(dir
->i_sb
);
2019 return ERR_PTR(-ENOMEM
);
2021 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
2022 inode
->i_ino
= fake_ino(0, PROC_TGID_INO
);
2024 inode
->i_mode
= S_IFLNK
|S_IRWXUGO
;
2025 inode
->i_uid
= inode
->i_gid
= 0;
2027 inode
->i_op
= &proc_self_inode_operations
;
2028 d_add(dentry
, inode
);
2031 tgid
= name_to_int(dentry
);
2035 read_lock(&tasklist_lock
);
2036 task
= find_task_by_pid(tgid
);
2038 get_task_struct(task
);
2039 read_unlock(&tasklist_lock
);
2043 inode
= proc_pid_make_inode(dir
->i_sb
, task
, PROC_TGID_INO
);
2047 put_task_struct(task
);
2050 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
2051 inode
->i_op
= &proc_tgid_base_inode_operations
;
2052 inode
->i_fop
= &proc_tgid_base_operations
;
2053 inode
->i_flags
|=S_IMMUTABLE
;
2054 #ifdef CONFIG_SECURITY
2060 dentry
->d_op
= &pid_base_dentry_operations
;
2063 d_add(dentry
, inode
);
2064 spin_lock(&task
->proc_lock
);
2065 task
->proc_dentry
= dentry
;
2066 if (!pid_alive(task
)) {
2067 dentry
= proc_pid_unhash(task
);
2070 spin_unlock(&task
->proc_lock
);
2072 put_task_struct(task
);
2074 proc_pid_flush(dentry
);
2079 return ERR_PTR(-ENOENT
);
2083 static struct dentry
*proc_task_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
)
2085 struct task_struct
*task
;
2086 struct task_struct
*leader
= proc_task(dir
);
2087 struct inode
*inode
;
2090 tid
= name_to_int(dentry
);
2094 read_lock(&tasklist_lock
);
2095 task
= find_task_by_pid(tid
);
2097 get_task_struct(task
);
2098 read_unlock(&tasklist_lock
);
2101 if (leader
->tgid
!= task
->tgid
)
2104 inode
= proc_pid_make_inode(dir
->i_sb
, task
, PROC_TID_INO
);
2109 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
2110 inode
->i_op
= &proc_tid_base_inode_operations
;
2111 inode
->i_fop
= &proc_tid_base_operations
;
2112 inode
->i_flags
|=S_IMMUTABLE
;
2113 #ifdef CONFIG_SECURITY
2119 dentry
->d_op
= &pid_base_dentry_operations
;
2121 d_add(dentry
, inode
);
2123 put_task_struct(task
);
2126 put_task_struct(task
);
2128 return ERR_PTR(-ENOENT
);
2131 #define PROC_NUMBUF 10
2132 #define PROC_MAXPIDS 20
2135 * Get a few tgid's to return for filldir - we need to hold the
2136 * tasklist lock while doing this, and we must release it before
2137 * we actually do the filldir itself, so we use a temp buffer..
2139 static int get_tgid_list(int index
, unsigned long version
, unsigned int *tgids
)
2141 struct task_struct
*p
;
2145 read_lock(&tasklist_lock
);
2148 p
= find_task_by_pid(version
);
2149 if (p
&& !thread_group_leader(p
))
2156 p
= next_task(&init_task
);
2158 for ( ; p
!= &init_task
; p
= next_task(p
)) {
2164 tgids
[nr_tgids
] = tgid
;
2166 if (nr_tgids
>= PROC_MAXPIDS
)
2169 read_unlock(&tasklist_lock
);
2174 * Get a few tid's to return for filldir - we need to hold the
2175 * tasklist lock while doing this, and we must release it before
2176 * we actually do the filldir itself, so we use a temp buffer..
2178 static int get_tid_list(int index
, unsigned int *tids
, struct inode
*dir
)
2180 struct task_struct
*leader_task
= proc_task(dir
);
2181 struct task_struct
*task
= leader_task
;
2185 read_lock(&tasklist_lock
);
2187 * The starting point task (leader_task) might be an already
2188 * unlinked task, which cannot be used to access the task-list
2189 * via next_thread().
2191 if (pid_alive(task
)) do {
2192 int tid
= task
->pid
;
2197 tids
[nr_tids
] = tid
;
2199 if (nr_tids
>= PROC_MAXPIDS
)
2201 } while ((task
= next_thread(task
)) != leader_task
);
2202 read_unlock(&tasklist_lock
);
2206 /* for the /proc/ directory itself, after non-process stuff has been done */
2207 int proc_pid_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
2209 unsigned int tgid_array
[PROC_MAXPIDS
];
2210 char buf
[PROC_NUMBUF
];
2211 unsigned int nr
= filp
->f_pos
- FIRST_PROCESS_ENTRY
;
2212 unsigned int nr_tgids
, i
;
2216 ino_t ino
= fake_ino(0,PROC_TGID_INO
);
2217 if (filldir(dirent
, "self", 4, filp
->f_pos
, ino
, DT_LNK
) < 0)
2223 /* f_version caches the tgid value that the last readdir call couldn't
2224 * return. lseek aka telldir automagically resets f_version to 0.
2226 next_tgid
= filp
->f_version
;
2227 filp
->f_version
= 0;
2229 nr_tgids
= get_tgid_list(nr
, next_tgid
, tgid_array
);
2231 /* no more entries ! */
2236 /* do not use the last found pid, reserve it for next_tgid */
2237 if (nr_tgids
== PROC_MAXPIDS
) {
2239 next_tgid
= tgid_array
[nr_tgids
];
2242 for (i
=0;i
<nr_tgids
;i
++) {
2243 int tgid
= tgid_array
[i
];
2244 ino_t ino
= fake_ino(tgid
,PROC_TGID_INO
);
2245 unsigned long j
= PROC_NUMBUF
;
2248 buf
[--j
] = '0' + (tgid
% 10);
2249 while ((tgid
/= 10) != 0);
2251 if (filldir(dirent
, buf
+j
, PROC_NUMBUF
-j
, filp
->f_pos
, ino
, DT_DIR
) < 0) {
2252 /* returning this tgid failed, save it as the first
2253 * pid for the next readir call */
2254 filp
->f_version
= tgid_array
[i
];
2265 /* for the /proc/TGID/task/ directories */
2266 static int proc_task_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
2268 unsigned int tid_array
[PROC_MAXPIDS
];
2269 char buf
[PROC_NUMBUF
];
2270 unsigned int nr_tids
, i
;
2271 struct dentry
*dentry
= filp
->f_dentry
;
2272 struct inode
*inode
= dentry
->d_inode
;
2273 int retval
= -ENOENT
;
2275 unsigned long pos
= filp
->f_pos
; /* avoiding "long long" filp->f_pos */
2277 if (!pid_alive(proc_task(inode
)))
2284 if (filldir(dirent
, ".", 1, pos
, ino
, DT_DIR
) < 0)
2289 ino
= parent_ino(dentry
);
2290 if (filldir(dirent
, "..", 2, pos
, ino
, DT_DIR
) < 0)
2296 nr_tids
= get_tid_list(pos
, tid_array
, inode
);
2297 inode
->i_nlink
= pos
+ nr_tids
;
2299 for (i
= 0; i
< nr_tids
; i
++) {
2300 unsigned long j
= PROC_NUMBUF
;
2301 int tid
= tid_array
[i
];
2303 ino
= fake_ino(tid
,PROC_TID_INO
);
2306 buf
[--j
] = '0' + (tid
% 10);
2307 while ((tid
/= 10) != 0);
2309 if (filldir(dirent
, buf
+j
, PROC_NUMBUF
-j
, pos
, ino
, DT_DIR
) < 0)