4 * Copyright (C) 1991, 1992 Linus Torvalds
6 * proc base directory handling functions
8 * 1999, Al Viro. Rewritten. Now it covers the whole per-process part.
9 * Instead of using magical inumbers to determine the kind of object
10 * we allocate and fill in-core inodes upon lookup. They don't even
11 * go into icache. We cache the reference to task_struct upon lookup too.
12 * Eventually it should become a filesystem in its own. We don't use the
13 * rest of procfs anymore.
19 * Bruna Moreira <bruna.moreira@indt.org.br>
20 * Edjard Mota <edjard.mota@indt.org.br>
21 * Ilias Biris <ilias.biris@indt.org.br>
22 * Mauricio Lin <mauricio.lin@indt.org.br>
24 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
26 * A new process specific entry (smaps) included in /proc. It shows the
27 * size of rss for each memory area. The maps entry lacks information
28 * about physical memory size (rss) for each mapped file, i.e.,
29 * rss information for executables and library files.
30 * This additional information is useful for any tools that need to know
31 * about physical memory consumption for a process specific library.
35 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
36 * Pud inclusion in the page table walking.
40 * 10LE Instituto Nokia de Tecnologia - INdT:
41 * A better way to walks through the page table as suggested by Hugh Dickins.
43 * Simo Piiroinen <simo.piiroinen@nokia.com>:
44 * Smaps information related to shared, private, clean and dirty pages.
46 * Paul Mundt <paul.mundt@nokia.com>:
47 * Overall revision about smaps.
50 #include <asm/uaccess.h>
52 #include <linux/errno.h>
53 #include <linux/time.h>
54 #include <linux/proc_fs.h>
55 #include <linux/stat.h>
56 #include <linux/init.h>
57 #include <linux/capability.h>
58 #include <linux/file.h>
59 #include <linux/string.h>
60 #include <linux/seq_file.h>
61 #include <linux/namei.h>
62 #include <linux/namespace.h>
64 #include <linux/smp_lock.h>
65 #include <linux/rcupdate.h>
66 #include <linux/kallsyms.h>
67 #include <linux/mount.h>
68 #include <linux/security.h>
69 #include <linux/ptrace.h>
70 #include <linux/seccomp.h>
71 #include <linux/cpuset.h>
72 #include <linux/audit.h>
73 #include <linux/poll.h>
77 * Implementing inode permission operations in /proc is almost
78 * certainly an error. Permission checks need to happen during
79 * each system call not at open time. The reason is that most of
80 * what we wish to check for permissions in /proc varies at runtime.
82 * The classic example of a problem is opening file descriptors
83 * in /proc for a task before it execs a suid executable.
87 * For hysterical raisins we keep the same inumbers as in the old procfs.
88 * Feel free to change the macro below - just keep the range distinct from
89 * inumbers of the rest of procfs (currently those are in 0x0000--0xffff).
90 * As soon as we'll get a separate superblock we will be able to forget
91 * about magical ranges too.
94 #define fake_ino(pid,ino) (((pid)<<16)|(ino))
96 enum pid_directory_inos
{
101 #ifdef CONFIG_SECCOMP
116 PROC_TGID_MOUNTSTATS
,
121 #ifdef CONFIG_SCHEDSTATS
124 #ifdef CONFIG_CPUSETS
127 #ifdef CONFIG_SECURITY
129 PROC_TGID_ATTR_CURRENT
,
132 PROC_TGID_ATTR_FSCREATE
,
133 PROC_TGID_ATTR_KEYCREATE
,
134 PROC_TGID_ATTR_SOCKCREATE
,
136 #ifdef CONFIG_AUDITSYSCALL
140 PROC_TGID_OOM_ADJUST
,
144 #ifdef CONFIG_SECCOMP
164 #ifdef CONFIG_SCHEDSTATS
167 #ifdef CONFIG_CPUSETS
170 #ifdef CONFIG_SECURITY
172 PROC_TID_ATTR_CURRENT
,
175 PROC_TID_ATTR_FSCREATE
,
176 PROC_TID_ATTR_KEYCREATE
,
177 PROC_TID_ATTR_SOCKCREATE
,
179 #ifdef CONFIG_AUDITSYSCALL
185 /* Add new entries before this */
186 PROC_TID_FD_DIR
= 0x8000, /* 0x8000-0xffff */
189 /* Worst case buffer size needed for holding an integer. */
190 #define PROC_NUMBUF 10
199 #define E(type,name,mode) {(type),sizeof(name)-1,(name),(mode)}
201 static struct pid_entry tgid_base_stuff
[] = {
202 E(PROC_TGID_TASK
, "task", S_IFDIR
|S_IRUGO
|S_IXUGO
),
203 E(PROC_TGID_FD
, "fd", S_IFDIR
|S_IRUSR
|S_IXUSR
),
204 E(PROC_TGID_ENVIRON
, "environ", S_IFREG
|S_IRUSR
),
205 E(PROC_TGID_AUXV
, "auxv", S_IFREG
|S_IRUSR
),
206 E(PROC_TGID_STATUS
, "status", S_IFREG
|S_IRUGO
),
207 E(PROC_TGID_CMDLINE
, "cmdline", S_IFREG
|S_IRUGO
),
208 E(PROC_TGID_STAT
, "stat", S_IFREG
|S_IRUGO
),
209 E(PROC_TGID_STATM
, "statm", S_IFREG
|S_IRUGO
),
210 E(PROC_TGID_MAPS
, "maps", S_IFREG
|S_IRUGO
),
212 E(PROC_TGID_NUMA_MAPS
, "numa_maps", S_IFREG
|S_IRUGO
),
214 E(PROC_TGID_MEM
, "mem", S_IFREG
|S_IRUSR
|S_IWUSR
),
215 #ifdef CONFIG_SECCOMP
216 E(PROC_TGID_SECCOMP
, "seccomp", S_IFREG
|S_IRUSR
|S_IWUSR
),
218 E(PROC_TGID_CWD
, "cwd", S_IFLNK
|S_IRWXUGO
),
219 E(PROC_TGID_ROOT
, "root", S_IFLNK
|S_IRWXUGO
),
220 E(PROC_TGID_EXE
, "exe", S_IFLNK
|S_IRWXUGO
),
221 E(PROC_TGID_MOUNTS
, "mounts", S_IFREG
|S_IRUGO
),
222 E(PROC_TGID_MOUNTSTATS
, "mountstats", S_IFREG
|S_IRUSR
),
224 E(PROC_TGID_SMAPS
, "smaps", S_IFREG
|S_IRUGO
),
226 #ifdef CONFIG_SECURITY
227 E(PROC_TGID_ATTR
, "attr", S_IFDIR
|S_IRUGO
|S_IXUGO
),
229 #ifdef CONFIG_KALLSYMS
230 E(PROC_TGID_WCHAN
, "wchan", S_IFREG
|S_IRUGO
),
232 #ifdef CONFIG_SCHEDSTATS
233 E(PROC_TGID_SCHEDSTAT
, "schedstat", S_IFREG
|S_IRUGO
),
235 #ifdef CONFIG_CPUSETS
236 E(PROC_TGID_CPUSET
, "cpuset", S_IFREG
|S_IRUGO
),
238 E(PROC_TGID_OOM_SCORE
, "oom_score",S_IFREG
|S_IRUGO
),
239 E(PROC_TGID_OOM_ADJUST
,"oom_adj", S_IFREG
|S_IRUGO
|S_IWUSR
),
240 #ifdef CONFIG_AUDITSYSCALL
241 E(PROC_TGID_LOGINUID
, "loginuid", S_IFREG
|S_IWUSR
|S_IRUGO
),
245 static struct pid_entry tid_base_stuff
[] = {
246 E(PROC_TID_FD
, "fd", S_IFDIR
|S_IRUSR
|S_IXUSR
),
247 E(PROC_TID_ENVIRON
, "environ", S_IFREG
|S_IRUSR
),
248 E(PROC_TID_AUXV
, "auxv", S_IFREG
|S_IRUSR
),
249 E(PROC_TID_STATUS
, "status", S_IFREG
|S_IRUGO
),
250 E(PROC_TID_CMDLINE
, "cmdline", S_IFREG
|S_IRUGO
),
251 E(PROC_TID_STAT
, "stat", S_IFREG
|S_IRUGO
),
252 E(PROC_TID_STATM
, "statm", S_IFREG
|S_IRUGO
),
253 E(PROC_TID_MAPS
, "maps", S_IFREG
|S_IRUGO
),
255 E(PROC_TID_NUMA_MAPS
, "numa_maps", S_IFREG
|S_IRUGO
),
257 E(PROC_TID_MEM
, "mem", S_IFREG
|S_IRUSR
|S_IWUSR
),
258 #ifdef CONFIG_SECCOMP
259 E(PROC_TID_SECCOMP
, "seccomp", S_IFREG
|S_IRUSR
|S_IWUSR
),
261 E(PROC_TID_CWD
, "cwd", S_IFLNK
|S_IRWXUGO
),
262 E(PROC_TID_ROOT
, "root", S_IFLNK
|S_IRWXUGO
),
263 E(PROC_TID_EXE
, "exe", S_IFLNK
|S_IRWXUGO
),
264 E(PROC_TID_MOUNTS
, "mounts", S_IFREG
|S_IRUGO
),
266 E(PROC_TID_SMAPS
, "smaps", S_IFREG
|S_IRUGO
),
268 #ifdef CONFIG_SECURITY
269 E(PROC_TID_ATTR
, "attr", S_IFDIR
|S_IRUGO
|S_IXUGO
),
271 #ifdef CONFIG_KALLSYMS
272 E(PROC_TID_WCHAN
, "wchan", S_IFREG
|S_IRUGO
),
274 #ifdef CONFIG_SCHEDSTATS
275 E(PROC_TID_SCHEDSTAT
, "schedstat",S_IFREG
|S_IRUGO
),
277 #ifdef CONFIG_CPUSETS
278 E(PROC_TID_CPUSET
, "cpuset", S_IFREG
|S_IRUGO
),
280 E(PROC_TID_OOM_SCORE
, "oom_score",S_IFREG
|S_IRUGO
),
281 E(PROC_TID_OOM_ADJUST
, "oom_adj", S_IFREG
|S_IRUGO
|S_IWUSR
),
282 #ifdef CONFIG_AUDITSYSCALL
283 E(PROC_TID_LOGINUID
, "loginuid", S_IFREG
|S_IWUSR
|S_IRUGO
),
288 #ifdef CONFIG_SECURITY
289 static struct pid_entry tgid_attr_stuff
[] = {
290 E(PROC_TGID_ATTR_CURRENT
, "current", S_IFREG
|S_IRUGO
|S_IWUGO
),
291 E(PROC_TGID_ATTR_PREV
, "prev", S_IFREG
|S_IRUGO
),
292 E(PROC_TGID_ATTR_EXEC
, "exec", S_IFREG
|S_IRUGO
|S_IWUGO
),
293 E(PROC_TGID_ATTR_FSCREATE
, "fscreate", S_IFREG
|S_IRUGO
|S_IWUGO
),
294 E(PROC_TGID_ATTR_KEYCREATE
, "keycreate", S_IFREG
|S_IRUGO
|S_IWUGO
),
295 E(PROC_TGID_ATTR_SOCKCREATE
, "sockcreate", S_IFREG
|S_IRUGO
|S_IWUGO
),
298 static struct pid_entry tid_attr_stuff
[] = {
299 E(PROC_TID_ATTR_CURRENT
, "current", S_IFREG
|S_IRUGO
|S_IWUGO
),
300 E(PROC_TID_ATTR_PREV
, "prev", S_IFREG
|S_IRUGO
),
301 E(PROC_TID_ATTR_EXEC
, "exec", S_IFREG
|S_IRUGO
|S_IWUGO
),
302 E(PROC_TID_ATTR_FSCREATE
, "fscreate", S_IFREG
|S_IRUGO
|S_IWUGO
),
303 E(PROC_TID_ATTR_KEYCREATE
, "keycreate", S_IFREG
|S_IRUGO
|S_IWUGO
),
304 E(PROC_TID_ATTR_SOCKCREATE
, "sockcreate", S_IFREG
|S_IRUGO
|S_IWUGO
),
311 static int proc_fd_link(struct inode
*inode
, struct dentry
**dentry
, struct vfsmount
**mnt
)
313 struct task_struct
*task
= get_proc_task(inode
);
314 struct files_struct
*files
= NULL
;
316 int fd
= proc_fd(inode
);
319 files
= get_files_struct(task
);
320 put_task_struct(task
);
324 * We are not taking a ref to the file structure, so we must
327 spin_lock(&files
->file_lock
);
328 file
= fcheck_files(files
, fd
);
330 *mnt
= mntget(file
->f_vfsmnt
);
331 *dentry
= dget(file
->f_dentry
);
332 spin_unlock(&files
->file_lock
);
333 put_files_struct(files
);
336 spin_unlock(&files
->file_lock
);
337 put_files_struct(files
);
342 static struct fs_struct
*get_fs_struct(struct task_struct
*task
)
344 struct fs_struct
*fs
;
348 atomic_inc(&fs
->count
);
353 static int get_nr_threads(struct task_struct
*tsk
)
355 /* Must be called with the rcu_read_lock held */
359 if (lock_task_sighand(tsk
, &flags
)) {
360 count
= atomic_read(&tsk
->signal
->count
);
361 unlock_task_sighand(tsk
, &flags
);
366 static int proc_cwd_link(struct inode
*inode
, struct dentry
**dentry
, struct vfsmount
**mnt
)
368 struct task_struct
*task
= get_proc_task(inode
);
369 struct fs_struct
*fs
= NULL
;
370 int result
= -ENOENT
;
373 fs
= get_fs_struct(task
);
374 put_task_struct(task
);
377 read_lock(&fs
->lock
);
378 *mnt
= mntget(fs
->pwdmnt
);
379 *dentry
= dget(fs
->pwd
);
380 read_unlock(&fs
->lock
);
387 static int proc_root_link(struct inode
*inode
, struct dentry
**dentry
, struct vfsmount
**mnt
)
389 struct task_struct
*task
= get_proc_task(inode
);
390 struct fs_struct
*fs
= NULL
;
391 int result
= -ENOENT
;
394 fs
= get_fs_struct(task
);
395 put_task_struct(task
);
398 read_lock(&fs
->lock
);
399 *mnt
= mntget(fs
->rootmnt
);
400 *dentry
= dget(fs
->root
);
401 read_unlock(&fs
->lock
);
408 #define MAY_PTRACE(task) \
409 (task == current || \
410 (task->parent == current && \
411 (task->ptrace & PT_PTRACED) && \
412 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
413 security_ptrace(current,task) == 0))
415 static int proc_pid_environ(struct task_struct
*task
, char * buffer
)
418 struct mm_struct
*mm
= get_task_mm(task
);
420 unsigned int len
= mm
->env_end
- mm
->env_start
;
423 res
= access_process_vm(task
, mm
->env_start
, buffer
, len
, 0);
424 if (!ptrace_may_attach(task
))
431 static int proc_pid_cmdline(struct task_struct
*task
, char * buffer
)
435 struct mm_struct
*mm
= get_task_mm(task
);
439 goto out_mm
; /* Shh! No looking before we're done */
441 len
= mm
->arg_end
- mm
->arg_start
;
446 res
= access_process_vm(task
, mm
->arg_start
, buffer
, len
, 0);
448 // If the nul at the end of args has been overwritten, then
449 // assume application is using setproctitle(3).
450 if (res
> 0 && buffer
[res
-1] != '\0' && len
< PAGE_SIZE
) {
451 len
= strnlen(buffer
, res
);
455 len
= mm
->env_end
- mm
->env_start
;
456 if (len
> PAGE_SIZE
- res
)
457 len
= PAGE_SIZE
- res
;
458 res
+= access_process_vm(task
, mm
->env_start
, buffer
+res
, len
, 0);
459 res
= strnlen(buffer
, res
);
468 static int proc_pid_auxv(struct task_struct
*task
, char *buffer
)
471 struct mm_struct
*mm
= get_task_mm(task
);
473 unsigned int nwords
= 0;
476 while (mm
->saved_auxv
[nwords
- 2] != 0); /* AT_NULL */
477 res
= nwords
* sizeof(mm
->saved_auxv
[0]);
480 memcpy(buffer
, mm
->saved_auxv
, res
);
487 #ifdef CONFIG_KALLSYMS
489 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
490 * Returns the resolved symbol. If that fails, simply return the address.
492 static int proc_pid_wchan(struct task_struct
*task
, char *buffer
)
495 const char *sym_name
;
496 unsigned long wchan
, size
, offset
;
497 char namebuf
[KSYM_NAME_LEN
+1];
499 wchan
= get_wchan(task
);
501 sym_name
= kallsyms_lookup(wchan
, &size
, &offset
, &modname
, namebuf
);
503 return sprintf(buffer
, "%s", sym_name
);
504 return sprintf(buffer
, "%lu", wchan
);
506 #endif /* CONFIG_KALLSYMS */
508 #ifdef CONFIG_SCHEDSTATS
510 * Provides /proc/PID/schedstat
512 static int proc_pid_schedstat(struct task_struct
*task
, char *buffer
)
514 return sprintf(buffer
, "%lu %lu %lu\n",
515 task
->sched_info
.cpu_time
,
516 task
->sched_info
.run_delay
,
517 task
->sched_info
.pcnt
);
521 /* The badness from the OOM killer */
522 unsigned long badness(struct task_struct
*p
, unsigned long uptime
);
523 static int proc_oom_score(struct task_struct
*task
, char *buffer
)
525 unsigned long points
;
526 struct timespec uptime
;
528 do_posix_clock_monotonic_gettime(&uptime
);
529 points
= badness(task
, uptime
.tv_sec
);
530 return sprintf(buffer
, "%lu\n", points
);
533 /************************************************************************/
534 /* Here the fs part begins */
535 /************************************************************************/
537 /* permission checks */
538 static int proc_fd_access_allowed(struct inode
*inode
)
540 struct task_struct
*task
;
542 /* Allow access to a task's file descriptors if it is us or we
543 * may use ptrace attach to the process and find out that
546 task
= get_proc_task(inode
);
548 allowed
= ptrace_may_attach(task
);
549 put_task_struct(task
);
554 static int proc_setattr(struct dentry
*dentry
, struct iattr
*attr
)
557 struct inode
*inode
= dentry
->d_inode
;
559 if (attr
->ia_valid
& ATTR_MODE
)
562 error
= inode_change_ok(inode
, attr
);
564 error
= security_inode_setattr(dentry
, attr
);
566 error
= inode_setattr(inode
, attr
);
571 static struct inode_operations proc_def_inode_operations
= {
572 .setattr
= proc_setattr
,
575 extern struct seq_operations mounts_op
;
581 static int mounts_open(struct inode
*inode
, struct file
*file
)
583 struct task_struct
*task
= get_proc_task(inode
);
584 struct namespace *namespace = NULL
;
585 struct proc_mounts
*p
;
590 namespace = task
->namespace;
592 get_namespace(namespace);
594 put_task_struct(task
);
599 p
= kmalloc(sizeof(struct proc_mounts
), GFP_KERNEL
);
601 file
->private_data
= &p
->m
;
602 ret
= seq_open(file
, &mounts_op
);
604 p
->m
.private = namespace;
605 p
->event
= namespace->event
;
610 put_namespace(namespace);
615 static int mounts_release(struct inode
*inode
, struct file
*file
)
617 struct seq_file
*m
= file
->private_data
;
618 struct namespace *namespace = m
->private;
619 put_namespace(namespace);
620 return seq_release(inode
, file
);
623 static unsigned mounts_poll(struct file
*file
, poll_table
*wait
)
625 struct proc_mounts
*p
= file
->private_data
;
626 struct namespace *ns
= p
->m
.private;
629 poll_wait(file
, &ns
->poll
, wait
);
631 spin_lock(&vfsmount_lock
);
632 if (p
->event
!= ns
->event
) {
633 p
->event
= ns
->event
;
636 spin_unlock(&vfsmount_lock
);
641 static struct file_operations proc_mounts_operations
= {
645 .release
= mounts_release
,
649 extern struct seq_operations mountstats_op
;
650 static int mountstats_open(struct inode
*inode
, struct file
*file
)
652 int ret
= seq_open(file
, &mountstats_op
);
655 struct seq_file
*m
= file
->private_data
;
656 struct namespace *namespace = NULL
;
657 struct task_struct
*task
= get_proc_task(inode
);
661 namespace = task
->namespace;
663 get_namespace(namespace);
665 put_task_struct(task
);
669 m
->private = namespace;
671 seq_release(inode
, file
);
678 static struct file_operations proc_mountstats_operations
= {
679 .open
= mountstats_open
,
682 .release
= mounts_release
,
685 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
687 static ssize_t
proc_info_read(struct file
* file
, char __user
* buf
,
688 size_t count
, loff_t
*ppos
)
690 struct inode
* inode
= file
->f_dentry
->d_inode
;
693 struct task_struct
*task
= get_proc_task(inode
);
699 if (count
> PROC_BLOCK_SIZE
)
700 count
= PROC_BLOCK_SIZE
;
703 if (!(page
= __get_free_page(GFP_KERNEL
)))
706 length
= PROC_I(inode
)->op
.proc_read(task
, (char*)page
);
709 length
= simple_read_from_buffer(buf
, count
, ppos
, (char *)page
, length
);
712 put_task_struct(task
);
717 static struct file_operations proc_info_file_operations
= {
718 .read
= proc_info_read
,
721 static int mem_open(struct inode
* inode
, struct file
* file
)
723 file
->private_data
= (void*)((long)current
->self_exec_id
);
727 static ssize_t
mem_read(struct file
* file
, char __user
* buf
,
728 size_t count
, loff_t
*ppos
)
730 struct task_struct
*task
= get_proc_task(file
->f_dentry
->d_inode
);
732 unsigned long src
= *ppos
;
734 struct mm_struct
*mm
;
739 if (!MAY_PTRACE(task
) || !ptrace_may_attach(task
))
743 page
= (char *)__get_free_page(GFP_USER
);
749 mm
= get_task_mm(task
);
755 if (file
->private_data
!= (void*)((long)current
->self_exec_id
))
761 int this_len
, retval
;
763 this_len
= (count
> PAGE_SIZE
) ? PAGE_SIZE
: count
;
764 retval
= access_process_vm(task
, src
, page
, this_len
, 0);
765 if (!retval
|| !MAY_PTRACE(task
) || !ptrace_may_attach(task
)) {
771 if (copy_to_user(buf
, page
, retval
)) {
786 free_page((unsigned long) page
);
788 put_task_struct(task
);
793 #define mem_write NULL
796 /* This is a security hazard */
797 static ssize_t
mem_write(struct file
* file
, const char * buf
,
798 size_t count
, loff_t
*ppos
)
802 struct task_struct
*task
= get_proc_task(file
->f_dentry
->d_inode
);
803 unsigned long dst
= *ppos
;
809 if (!MAY_PTRACE(task
) || !ptrace_may_attach(task
))
813 page
= (char *)__get_free_page(GFP_USER
);
818 int this_len
, retval
;
820 this_len
= (count
> PAGE_SIZE
) ? PAGE_SIZE
: count
;
821 if (copy_from_user(page
, buf
, this_len
)) {
825 retval
= access_process_vm(task
, dst
, page
, this_len
, 1);
837 free_page((unsigned long) page
);
839 put_task_struct(task
);
845 static loff_t
mem_lseek(struct file
* file
, loff_t offset
, int orig
)
849 file
->f_pos
= offset
;
852 file
->f_pos
+= offset
;
857 force_successful_syscall_return();
861 static struct file_operations proc_mem_operations
= {
868 static ssize_t
oom_adjust_read(struct file
*file
, char __user
*buf
,
869 size_t count
, loff_t
*ppos
)
871 struct task_struct
*task
= get_proc_task(file
->f_dentry
->d_inode
);
872 char buffer
[PROC_NUMBUF
];
875 loff_t __ppos
= *ppos
;
879 oom_adjust
= task
->oomkilladj
;
880 put_task_struct(task
);
882 len
= snprintf(buffer
, sizeof(buffer
), "%i\n", oom_adjust
);
885 if (count
> len
-__ppos
)
887 if (copy_to_user(buf
, buffer
+ __ppos
, count
))
889 *ppos
= __ppos
+ count
;
893 static ssize_t
oom_adjust_write(struct file
*file
, const char __user
*buf
,
894 size_t count
, loff_t
*ppos
)
896 struct task_struct
*task
;
897 char buffer
[PROC_NUMBUF
], *end
;
900 if (!capable(CAP_SYS_RESOURCE
))
902 memset(buffer
, 0, sizeof(buffer
));
903 if (count
> sizeof(buffer
) - 1)
904 count
= sizeof(buffer
) - 1;
905 if (copy_from_user(buffer
, buf
, count
))
907 oom_adjust
= simple_strtol(buffer
, &end
, 0);
908 if ((oom_adjust
< -16 || oom_adjust
> 15) && oom_adjust
!= OOM_DISABLE
)
912 task
= get_proc_task(file
->f_dentry
->d_inode
);
915 task
->oomkilladj
= oom_adjust
;
916 put_task_struct(task
);
917 if (end
- buffer
== 0)
922 static struct file_operations proc_oom_adjust_operations
= {
923 .read
= oom_adjust_read
,
924 .write
= oom_adjust_write
,
927 #ifdef CONFIG_AUDITSYSCALL
929 static ssize_t
proc_loginuid_read(struct file
* file
, char __user
* buf
,
930 size_t count
, loff_t
*ppos
)
932 struct inode
* inode
= file
->f_dentry
->d_inode
;
933 struct task_struct
*task
= get_proc_task(inode
);
935 char tmpbuf
[TMPBUFLEN
];
939 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
940 audit_get_loginuid(task
->audit_context
));
941 put_task_struct(task
);
942 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
945 static ssize_t
proc_loginuid_write(struct file
* file
, const char __user
* buf
,
946 size_t count
, loff_t
*ppos
)
948 struct inode
* inode
= file
->f_dentry
->d_inode
;
953 if (!capable(CAP_AUDIT_CONTROL
))
956 if (current
!= pid_task(proc_pid(inode
), PIDTYPE_PID
))
959 if (count
>= PAGE_SIZE
)
960 count
= PAGE_SIZE
- 1;
963 /* No partial writes. */
966 page
= (char*)__get_free_page(GFP_USER
);
970 if (copy_from_user(page
, buf
, count
))
974 loginuid
= simple_strtoul(page
, &tmp
, 10);
980 length
= audit_set_loginuid(current
, loginuid
);
981 if (likely(length
== 0))
985 free_page((unsigned long) page
);
989 static struct file_operations proc_loginuid_operations
= {
990 .read
= proc_loginuid_read
,
991 .write
= proc_loginuid_write
,
995 #ifdef CONFIG_SECCOMP
996 static ssize_t
seccomp_read(struct file
*file
, char __user
*buf
,
997 size_t count
, loff_t
*ppos
)
999 struct task_struct
*tsk
= get_proc_task(file
->f_dentry
->d_inode
);
1001 loff_t __ppos
= *ppos
;
1006 /* no need to print the trailing zero, so use only len */
1007 len
= sprintf(__buf
, "%u\n", tsk
->seccomp
.mode
);
1008 put_task_struct(tsk
);
1011 if (count
> len
- __ppos
)
1012 count
= len
- __ppos
;
1013 if (copy_to_user(buf
, __buf
+ __ppos
, count
))
1015 *ppos
= __ppos
+ count
;
1019 static ssize_t
seccomp_write(struct file
*file
, const char __user
*buf
,
1020 size_t count
, loff_t
*ppos
)
1022 struct task_struct
*tsk
= get_proc_task(file
->f_dentry
->d_inode
);
1023 char __buf
[20], *end
;
1024 unsigned int seccomp_mode
;
1031 /* can set it only once to be even more secure */
1033 if (unlikely(tsk
->seccomp
.mode
))
1037 memset(__buf
, 0, sizeof(__buf
));
1038 count
= min(count
, sizeof(__buf
) - 1);
1039 if (copy_from_user(__buf
, buf
, count
))
1042 seccomp_mode
= simple_strtoul(__buf
, &end
, 0);
1046 if (seccomp_mode
&& seccomp_mode
<= NR_SECCOMP_MODES
) {
1047 tsk
->seccomp
.mode
= seccomp_mode
;
1048 set_tsk_thread_flag(tsk
, TIF_SECCOMP
);
1052 if (unlikely(!(end
- __buf
)))
1054 result
= end
- __buf
;
1056 put_task_struct(tsk
);
1061 static struct file_operations proc_seccomp_operations
= {
1062 .read
= seccomp_read
,
1063 .write
= seccomp_write
,
1065 #endif /* CONFIG_SECCOMP */
1067 static void *proc_pid_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
1069 struct inode
*inode
= dentry
->d_inode
;
1070 int error
= -EACCES
;
1072 /* We don't need a base pointer in the /proc filesystem */
1075 /* Are we allowed to snoop on the tasks file descriptors? */
1076 if (!proc_fd_access_allowed(inode
))
1079 error
= PROC_I(inode
)->op
.proc_get_link(inode
, &nd
->dentry
, &nd
->mnt
);
1080 nd
->last_type
= LAST_BIND
;
1082 return ERR_PTR(error
);
1085 static int do_proc_readlink(struct dentry
*dentry
, struct vfsmount
*mnt
,
1086 char __user
*buffer
, int buflen
)
1088 struct inode
* inode
;
1089 char *tmp
= (char*)__get_free_page(GFP_KERNEL
), *path
;
1095 inode
= dentry
->d_inode
;
1096 path
= d_path(dentry
, mnt
, tmp
, PAGE_SIZE
);
1097 len
= PTR_ERR(path
);
1100 len
= tmp
+ PAGE_SIZE
- 1 - path
;
1104 if (copy_to_user(buffer
, path
, len
))
1107 free_page((unsigned long)tmp
);
1111 static int proc_pid_readlink(struct dentry
* dentry
, char __user
* buffer
, int buflen
)
1113 int error
= -EACCES
;
1114 struct inode
*inode
= dentry
->d_inode
;
1116 struct vfsmount
*mnt
= NULL
;
1118 /* Are we allowed to snoop on the tasks file descriptors? */
1119 if (!proc_fd_access_allowed(inode
))
1122 error
= PROC_I(inode
)->op
.proc_get_link(inode
, &de
, &mnt
);
1126 error
= do_proc_readlink(de
, mnt
, buffer
, buflen
);
1133 static struct inode_operations proc_pid_link_inode_operations
= {
1134 .readlink
= proc_pid_readlink
,
1135 .follow_link
= proc_pid_follow_link
,
1136 .setattr
= proc_setattr
,
1139 static int proc_readfd(struct file
* filp
, void * dirent
, filldir_t filldir
)
1141 struct dentry
*dentry
= filp
->f_dentry
;
1142 struct inode
*inode
= dentry
->d_inode
;
1143 struct task_struct
*p
= get_proc_task(inode
);
1144 unsigned int fd
, tid
, ino
;
1146 char buf
[PROC_NUMBUF
];
1147 struct files_struct
* files
;
1148 struct fdtable
*fdt
;
1159 if (filldir(dirent
, ".", 1, 0, inode
->i_ino
, DT_DIR
) < 0)
1163 ino
= parent_ino(dentry
);
1164 if (filldir(dirent
, "..", 2, 1, ino
, DT_DIR
) < 0)
1168 files
= get_files_struct(p
);
1172 fdt
= files_fdtable(files
);
1173 for (fd
= filp
->f_pos
-2;
1175 fd
++, filp
->f_pos
++) {
1178 if (!fcheck_files(files
, fd
))
1186 buf
[j
] = '0' + (i
% 10);
1190 ino
= fake_ino(tid
, PROC_TID_FD_DIR
+ fd
);
1191 if (filldir(dirent
, buf
+j
, PROC_NUMBUF
-j
, fd
+2, ino
, DT_LNK
) < 0) {
1198 put_files_struct(files
);
1206 static int proc_pident_readdir(struct file
*filp
,
1207 void *dirent
, filldir_t filldir
,
1208 struct pid_entry
*ents
, unsigned int nents
)
1212 struct dentry
*dentry
= filp
->f_dentry
;
1213 struct inode
*inode
= dentry
->d_inode
;
1214 struct task_struct
*task
= get_proc_task(inode
);
1215 struct pid_entry
*p
;
1225 put_task_struct(task
);
1230 if (filldir(dirent
, ".", 1, i
, ino
, DT_DIR
) < 0)
1236 ino
= parent_ino(dentry
);
1237 if (filldir(dirent
, "..", 2, i
, ino
, DT_DIR
) < 0)
1250 if (filldir(dirent
, p
->name
, p
->len
, filp
->f_pos
,
1251 fake_ino(pid
, p
->type
), p
->mode
>> 12) < 0)
1263 static int proc_tgid_base_readdir(struct file
* filp
,
1264 void * dirent
, filldir_t filldir
)
1266 return proc_pident_readdir(filp
,dirent
,filldir
,
1267 tgid_base_stuff
,ARRAY_SIZE(tgid_base_stuff
));
1270 static int proc_tid_base_readdir(struct file
* filp
,
1271 void * dirent
, filldir_t filldir
)
1273 return proc_pident_readdir(filp
,dirent
,filldir
,
1274 tid_base_stuff
,ARRAY_SIZE(tid_base_stuff
));
1277 /* building an inode */
1279 static int task_dumpable(struct task_struct
*task
)
1282 struct mm_struct
*mm
;
1287 dumpable
= mm
->dumpable
;
1295 static struct inode
*proc_pid_make_inode(struct super_block
* sb
, struct task_struct
*task
, int ino
)
1297 struct inode
* inode
;
1298 struct proc_inode
*ei
;
1300 /* We need a new inode */
1302 inode
= new_inode(sb
);
1308 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1309 inode
->i_ino
= fake_ino(task
->pid
, ino
);
1310 inode
->i_op
= &proc_def_inode_operations
;
1313 * grab the reference to task.
1315 ei
->pid
= get_pid(task
->pids
[PIDTYPE_PID
].pid
);
1321 if (task_dumpable(task
)) {
1322 inode
->i_uid
= task
->euid
;
1323 inode
->i_gid
= task
->egid
;
1325 security_task_to_inode(task
, inode
);
1338 * Exceptional case: normally we are not allowed to unhash a busy
1339 * directory. In this case, however, we can do it - no aliasing problems
1340 * due to the way we treat inodes.
1342 * Rewrite the inode's ownerships here because the owning task may have
1343 * performed a setuid(), etc.
1345 * Before the /proc/pid/status file was created the only way to read
1346 * the effective uid of a /process was to stat /proc/pid. Reading
1347 * /proc/pid/status is slow enough that procps and other packages
1348 * kept stating /proc/pid. To keep the rules in /proc simple I have
1349 * made this apply to all per process world readable and executable
1352 static int pid_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1354 struct inode
*inode
= dentry
->d_inode
;
1355 struct task_struct
*task
= get_proc_task(inode
);
1357 if ((inode
->i_mode
== (S_IFDIR
|S_IRUGO
|S_IXUGO
)) ||
1358 task_dumpable(task
)) {
1359 inode
->i_uid
= task
->euid
;
1360 inode
->i_gid
= task
->egid
;
1365 inode
->i_mode
&= ~(S_ISUID
| S_ISGID
);
1366 security_task_to_inode(task
, inode
);
1367 put_task_struct(task
);
1374 static int pid_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
1376 struct inode
*inode
= dentry
->d_inode
;
1377 struct task_struct
*task
;
1378 generic_fillattr(inode
, stat
);
1383 task
= pid_task(proc_pid(inode
), PIDTYPE_PID
);
1385 if ((inode
->i_mode
== (S_IFDIR
|S_IRUGO
|S_IXUGO
)) ||
1386 task_dumpable(task
)) {
1387 stat
->uid
= task
->euid
;
1388 stat
->gid
= task
->egid
;
1395 static int tid_fd_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1397 struct inode
*inode
= dentry
->d_inode
;
1398 struct task_struct
*task
= get_proc_task(inode
);
1399 int fd
= proc_fd(inode
);
1400 struct files_struct
*files
;
1403 files
= get_files_struct(task
);
1406 if (fcheck_files(files
, fd
)) {
1408 put_files_struct(files
);
1409 if (task_dumpable(task
)) {
1410 inode
->i_uid
= task
->euid
;
1411 inode
->i_gid
= task
->egid
;
1416 inode
->i_mode
&= ~(S_ISUID
| S_ISGID
);
1417 security_task_to_inode(task
, inode
);
1418 put_task_struct(task
);
1422 put_files_struct(files
);
1424 put_task_struct(task
);
1430 static int pid_delete_dentry(struct dentry
* dentry
)
1432 /* Is the task we represent dead?
1433 * If so, then don't put the dentry on the lru list,
1434 * kill it immediately.
1436 return !proc_pid(dentry
->d_inode
)->tasks
[PIDTYPE_PID
].first
;
1439 static struct dentry_operations tid_fd_dentry_operations
=
1441 .d_revalidate
= tid_fd_revalidate
,
1442 .d_delete
= pid_delete_dentry
,
1445 static struct dentry_operations pid_dentry_operations
=
1447 .d_revalidate
= pid_revalidate
,
1448 .d_delete
= pid_delete_dentry
,
1453 static unsigned name_to_int(struct dentry
*dentry
)
1455 const char *name
= dentry
->d_name
.name
;
1456 int len
= dentry
->d_name
.len
;
1459 if (len
> 1 && *name
== '0')
1462 unsigned c
= *name
++ - '0';
1465 if (n
>= (~0U-9)/10)
1476 static struct dentry
*proc_lookupfd(struct inode
* dir
, struct dentry
* dentry
, struct nameidata
*nd
)
1478 struct task_struct
*task
= get_proc_task(dir
);
1479 unsigned fd
= name_to_int(dentry
);
1480 struct dentry
*result
= ERR_PTR(-ENOENT
);
1482 struct files_struct
* files
;
1483 struct inode
*inode
;
1484 struct proc_inode
*ei
;
1491 inode
= proc_pid_make_inode(dir
->i_sb
, task
, PROC_TID_FD_DIR
+fd
);
1496 files
= get_files_struct(task
);
1499 inode
->i_mode
= S_IFLNK
;
1502 * We are not taking a ref to the file structure, so we must
1505 spin_lock(&files
->file_lock
);
1506 file
= fcheck_files(files
, fd
);
1509 if (file
->f_mode
& 1)
1510 inode
->i_mode
|= S_IRUSR
| S_IXUSR
;
1511 if (file
->f_mode
& 2)
1512 inode
->i_mode
|= S_IWUSR
| S_IXUSR
;
1513 spin_unlock(&files
->file_lock
);
1514 put_files_struct(files
);
1515 inode
->i_op
= &proc_pid_link_inode_operations
;
1517 ei
->op
.proc_get_link
= proc_fd_link
;
1518 dentry
->d_op
= &tid_fd_dentry_operations
;
1519 d_add(dentry
, inode
);
1520 /* Close the race of the process dying before we return the dentry */
1521 if (tid_fd_revalidate(dentry
, NULL
))
1524 put_task_struct(task
);
1529 spin_unlock(&files
->file_lock
);
1530 put_files_struct(files
);
1536 static int proc_task_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
);
1537 static struct dentry
*proc_task_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
);
1538 static int proc_task_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
);
1540 static struct file_operations proc_fd_operations
= {
1541 .read
= generic_read_dir
,
1542 .readdir
= proc_readfd
,
1545 static struct file_operations proc_task_operations
= {
1546 .read
= generic_read_dir
,
1547 .readdir
= proc_task_readdir
,
1551 * proc directories can do almost nothing..
1553 static struct inode_operations proc_fd_inode_operations
= {
1554 .lookup
= proc_lookupfd
,
1555 .setattr
= proc_setattr
,
1558 static struct inode_operations proc_task_inode_operations
= {
1559 .lookup
= proc_task_lookup
,
1560 .getattr
= proc_task_getattr
,
1561 .setattr
= proc_setattr
,
1564 #ifdef CONFIG_SECURITY
1565 static ssize_t
proc_pid_attr_read(struct file
* file
, char __user
* buf
,
1566 size_t count
, loff_t
*ppos
)
1568 struct inode
* inode
= file
->f_dentry
->d_inode
;
1571 struct task_struct
*task
= get_proc_task(inode
);
1577 if (count
> PAGE_SIZE
)
1580 if (!(page
= __get_free_page(GFP_KERNEL
)))
1583 length
= security_getprocattr(task
,
1584 (char*)file
->f_dentry
->d_name
.name
,
1585 (void*)page
, count
);
1587 length
= simple_read_from_buffer(buf
, count
, ppos
, (char *)page
, length
);
1590 put_task_struct(task
);
1595 static ssize_t
proc_pid_attr_write(struct file
* file
, const char __user
* buf
,
1596 size_t count
, loff_t
*ppos
)
1598 struct inode
* inode
= file
->f_dentry
->d_inode
;
1601 struct task_struct
*task
= get_proc_task(inode
);
1606 if (count
> PAGE_SIZE
)
1609 /* No partial writes. */
1615 page
= (char*)__get_free_page(GFP_USER
);
1620 if (copy_from_user(page
, buf
, count
))
1623 length
= security_setprocattr(task
,
1624 (char*)file
->f_dentry
->d_name
.name
,
1625 (void*)page
, count
);
1627 free_page((unsigned long) page
);
1629 put_task_struct(task
);
1634 static struct file_operations proc_pid_attr_operations
= {
1635 .read
= proc_pid_attr_read
,
1636 .write
= proc_pid_attr_write
,
1639 static struct file_operations proc_tid_attr_operations
;
1640 static struct inode_operations proc_tid_attr_inode_operations
;
1641 static struct file_operations proc_tgid_attr_operations
;
1642 static struct inode_operations proc_tgid_attr_inode_operations
;
1646 static struct dentry
*proc_pident_lookup(struct inode
*dir
,
1647 struct dentry
*dentry
,
1648 struct pid_entry
*ents
)
1650 struct inode
*inode
;
1651 struct dentry
*error
;
1652 struct task_struct
*task
= get_proc_task(dir
);
1653 struct pid_entry
*p
;
1654 struct proc_inode
*ei
;
1656 error
= ERR_PTR(-ENOENT
);
1662 for (p
= ents
; p
->name
; p
++) {
1663 if (p
->len
!= dentry
->d_name
.len
)
1665 if (!memcmp(dentry
->d_name
.name
, p
->name
, p
->len
))
1671 error
= ERR_PTR(-EINVAL
);
1672 inode
= proc_pid_make_inode(dir
->i_sb
, task
, p
->type
);
1677 inode
->i_mode
= p
->mode
;
1679 * Yes, it does not scale. And it should not. Don't add
1680 * new entries into /proc/<tgid>/ without very good reasons.
1683 case PROC_TGID_TASK
:
1685 inode
->i_op
= &proc_task_inode_operations
;
1686 inode
->i_fop
= &proc_task_operations
;
1691 inode
->i_op
= &proc_fd_inode_operations
;
1692 inode
->i_fop
= &proc_fd_operations
;
1696 inode
->i_op
= &proc_pid_link_inode_operations
;
1697 ei
->op
.proc_get_link
= proc_exe_link
;
1701 inode
->i_op
= &proc_pid_link_inode_operations
;
1702 ei
->op
.proc_get_link
= proc_cwd_link
;
1705 case PROC_TGID_ROOT
:
1706 inode
->i_op
= &proc_pid_link_inode_operations
;
1707 ei
->op
.proc_get_link
= proc_root_link
;
1709 case PROC_TID_ENVIRON
:
1710 case PROC_TGID_ENVIRON
:
1711 inode
->i_fop
= &proc_info_file_operations
;
1712 ei
->op
.proc_read
= proc_pid_environ
;
1715 case PROC_TGID_AUXV
:
1716 inode
->i_fop
= &proc_info_file_operations
;
1717 ei
->op
.proc_read
= proc_pid_auxv
;
1719 case PROC_TID_STATUS
:
1720 case PROC_TGID_STATUS
:
1721 inode
->i_fop
= &proc_info_file_operations
;
1722 ei
->op
.proc_read
= proc_pid_status
;
1725 inode
->i_fop
= &proc_info_file_operations
;
1726 ei
->op
.proc_read
= proc_tid_stat
;
1728 case PROC_TGID_STAT
:
1729 inode
->i_fop
= &proc_info_file_operations
;
1730 ei
->op
.proc_read
= proc_tgid_stat
;
1732 case PROC_TID_CMDLINE
:
1733 case PROC_TGID_CMDLINE
:
1734 inode
->i_fop
= &proc_info_file_operations
;
1735 ei
->op
.proc_read
= proc_pid_cmdline
;
1737 case PROC_TID_STATM
:
1738 case PROC_TGID_STATM
:
1739 inode
->i_fop
= &proc_info_file_operations
;
1740 ei
->op
.proc_read
= proc_pid_statm
;
1743 case PROC_TGID_MAPS
:
1744 inode
->i_fop
= &proc_maps_operations
;
1747 case PROC_TID_NUMA_MAPS
:
1748 case PROC_TGID_NUMA_MAPS
:
1749 inode
->i_fop
= &proc_numa_maps_operations
;
1754 inode
->i_fop
= &proc_mem_operations
;
1756 #ifdef CONFIG_SECCOMP
1757 case PROC_TID_SECCOMP
:
1758 case PROC_TGID_SECCOMP
:
1759 inode
->i_fop
= &proc_seccomp_operations
;
1761 #endif /* CONFIG_SECCOMP */
1762 case PROC_TID_MOUNTS
:
1763 case PROC_TGID_MOUNTS
:
1764 inode
->i_fop
= &proc_mounts_operations
;
1767 case PROC_TID_SMAPS
:
1768 case PROC_TGID_SMAPS
:
1769 inode
->i_fop
= &proc_smaps_operations
;
1772 case PROC_TID_MOUNTSTATS
:
1773 case PROC_TGID_MOUNTSTATS
:
1774 inode
->i_fop
= &proc_mountstats_operations
;
1776 #ifdef CONFIG_SECURITY
1779 inode
->i_op
= &proc_tid_attr_inode_operations
;
1780 inode
->i_fop
= &proc_tid_attr_operations
;
1782 case PROC_TGID_ATTR
:
1784 inode
->i_op
= &proc_tgid_attr_inode_operations
;
1785 inode
->i_fop
= &proc_tgid_attr_operations
;
1787 case PROC_TID_ATTR_CURRENT
:
1788 case PROC_TGID_ATTR_CURRENT
:
1789 case PROC_TID_ATTR_PREV
:
1790 case PROC_TGID_ATTR_PREV
:
1791 case PROC_TID_ATTR_EXEC
:
1792 case PROC_TGID_ATTR_EXEC
:
1793 case PROC_TID_ATTR_FSCREATE
:
1794 case PROC_TGID_ATTR_FSCREATE
:
1795 case PROC_TID_ATTR_KEYCREATE
:
1796 case PROC_TGID_ATTR_KEYCREATE
:
1797 case PROC_TID_ATTR_SOCKCREATE
:
1798 case PROC_TGID_ATTR_SOCKCREATE
:
1799 inode
->i_fop
= &proc_pid_attr_operations
;
1802 #ifdef CONFIG_KALLSYMS
1803 case PROC_TID_WCHAN
:
1804 case PROC_TGID_WCHAN
:
1805 inode
->i_fop
= &proc_info_file_operations
;
1806 ei
->op
.proc_read
= proc_pid_wchan
;
1809 #ifdef CONFIG_SCHEDSTATS
1810 case PROC_TID_SCHEDSTAT
:
1811 case PROC_TGID_SCHEDSTAT
:
1812 inode
->i_fop
= &proc_info_file_operations
;
1813 ei
->op
.proc_read
= proc_pid_schedstat
;
1816 #ifdef CONFIG_CPUSETS
1817 case PROC_TID_CPUSET
:
1818 case PROC_TGID_CPUSET
:
1819 inode
->i_fop
= &proc_cpuset_operations
;
1822 case PROC_TID_OOM_SCORE
:
1823 case PROC_TGID_OOM_SCORE
:
1824 inode
->i_fop
= &proc_info_file_operations
;
1825 ei
->op
.proc_read
= proc_oom_score
;
1827 case PROC_TID_OOM_ADJUST
:
1828 case PROC_TGID_OOM_ADJUST
:
1829 inode
->i_fop
= &proc_oom_adjust_operations
;
1831 #ifdef CONFIG_AUDITSYSCALL
1832 case PROC_TID_LOGINUID
:
1833 case PROC_TGID_LOGINUID
:
1834 inode
->i_fop
= &proc_loginuid_operations
;
1838 printk("procfs: impossible type (%d)",p
->type
);
1840 error
= ERR_PTR(-EINVAL
);
1843 dentry
->d_op
= &pid_dentry_operations
;
1844 d_add(dentry
, inode
);
1845 /* Close the race of the process dying before we return the dentry */
1846 if (pid_revalidate(dentry
, NULL
))
1849 put_task_struct(task
);
1854 static struct dentry
*proc_tgid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
){
1855 return proc_pident_lookup(dir
, dentry
, tgid_base_stuff
);
1858 static struct dentry
*proc_tid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
){
1859 return proc_pident_lookup(dir
, dentry
, tid_base_stuff
);
1862 static struct file_operations proc_tgid_base_operations
= {
1863 .read
= generic_read_dir
,
1864 .readdir
= proc_tgid_base_readdir
,
1867 static struct file_operations proc_tid_base_operations
= {
1868 .read
= generic_read_dir
,
1869 .readdir
= proc_tid_base_readdir
,
1872 static struct inode_operations proc_tgid_base_inode_operations
= {
1873 .lookup
= proc_tgid_base_lookup
,
1874 .getattr
= pid_getattr
,
1875 .setattr
= proc_setattr
,
1878 static struct inode_operations proc_tid_base_inode_operations
= {
1879 .lookup
= proc_tid_base_lookup
,
1880 .getattr
= pid_getattr
,
1881 .setattr
= proc_setattr
,
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
,
1923 .getattr
= pid_getattr
,
1924 .setattr
= proc_setattr
,
1927 static struct inode_operations proc_tid_attr_inode_operations
= {
1928 .lookup
= proc_tid_attr_lookup
,
1929 .getattr
= pid_getattr
,
1930 .setattr
= proc_setattr
,
1937 static int proc_self_readlink(struct dentry
*dentry
, char __user
*buffer
,
1940 char tmp
[PROC_NUMBUF
];
1941 sprintf(tmp
, "%d", current
->tgid
);
1942 return vfs_readlink(dentry
,buffer
,buflen
,tmp
);
1945 static void *proc_self_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
1947 char tmp
[PROC_NUMBUF
];
1948 sprintf(tmp
, "%d", current
->tgid
);
1949 return ERR_PTR(vfs_follow_link(nd
,tmp
));
1952 static struct inode_operations proc_self_inode_operations
= {
1953 .readlink
= proc_self_readlink
,
1954 .follow_link
= proc_self_follow_link
,
1958 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
1960 * @task: task that should be flushed.
1962 * Looks in the dcache for
1964 * /proc/@tgid/task/@pid
1965 * if either directory is present flushes it and all of it'ts children
1968 * It is safe and reasonable to cache /proc entries for a task until
1969 * that task exits. After that they just clog up the dcache with
1970 * useless entries, possibly causing useful dcache entries to be
1971 * flushed instead. This routine is proved to flush those useless
1972 * dcache entries at process exit time.
1974 * NOTE: This routine is just an optimization so it does not guarantee
1975 * that no dcache entries will exist at process exit time it
1976 * just makes it very unlikely that any will persist.
1978 void proc_flush_task(struct task_struct
*task
)
1980 struct dentry
*dentry
, *leader
, *dir
;
1981 char buf
[PROC_NUMBUF
];
1985 name
.len
= snprintf(buf
, sizeof(buf
), "%d", task
->pid
);
1986 dentry
= d_hash_and_lookup(proc_mnt
->mnt_root
, &name
);
1988 shrink_dcache_parent(dentry
);
1993 if (thread_group_leader(task
))
1997 name
.len
= snprintf(buf
, sizeof(buf
), "%d", task
->tgid
);
1998 leader
= d_hash_and_lookup(proc_mnt
->mnt_root
, &name
);
2003 name
.len
= strlen(name
.name
);
2004 dir
= d_hash_and_lookup(leader
, &name
);
2006 goto out_put_leader
;
2009 name
.len
= snprintf(buf
, sizeof(buf
), "%d", task
->pid
);
2010 dentry
= d_hash_and_lookup(dir
, &name
);
2012 shrink_dcache_parent(dentry
);
2025 struct dentry
*proc_pid_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
)
2027 struct dentry
*result
= ERR_PTR(-ENOENT
);
2028 struct task_struct
*task
;
2029 struct inode
*inode
;
2030 struct proc_inode
*ei
;
2033 if (dentry
->d_name
.len
== 4 && !memcmp(dentry
->d_name
.name
,"self",4)) {
2034 inode
= new_inode(dir
->i_sb
);
2036 return ERR_PTR(-ENOMEM
);
2038 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
2039 inode
->i_ino
= fake_ino(0, PROC_TGID_INO
);
2041 inode
->i_mode
= S_IFLNK
|S_IRWXUGO
;
2042 inode
->i_uid
= inode
->i_gid
= 0;
2044 inode
->i_op
= &proc_self_inode_operations
;
2045 d_add(dentry
, inode
);
2048 tgid
= name_to_int(dentry
);
2053 task
= find_task_by_pid(tgid
);
2055 get_task_struct(task
);
2060 inode
= proc_pid_make_inode(dir
->i_sb
, task
, PROC_TGID_INO
);
2064 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
2065 inode
->i_op
= &proc_tgid_base_inode_operations
;
2066 inode
->i_fop
= &proc_tgid_base_operations
;
2067 inode
->i_flags
|=S_IMMUTABLE
;
2068 #ifdef CONFIG_SECURITY
2074 dentry
->d_op
= &pid_dentry_operations
;
2076 d_add(dentry
, inode
);
2077 /* Close the race of the process dying before we return the dentry */
2078 if (pid_revalidate(dentry
, NULL
))
2082 put_task_struct(task
);
2088 static struct dentry
*proc_task_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
)
2090 struct dentry
*result
= ERR_PTR(-ENOENT
);
2091 struct task_struct
*task
;
2092 struct task_struct
*leader
= get_proc_task(dir
);
2093 struct inode
*inode
;
2099 tid
= name_to_int(dentry
);
2104 task
= find_task_by_pid(tid
);
2106 get_task_struct(task
);
2110 if (leader
->tgid
!= task
->tgid
)
2113 inode
= proc_pid_make_inode(dir
->i_sb
, task
, PROC_TID_INO
);
2118 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
2119 inode
->i_op
= &proc_tid_base_inode_operations
;
2120 inode
->i_fop
= &proc_tid_base_operations
;
2121 inode
->i_flags
|=S_IMMUTABLE
;
2122 #ifdef CONFIG_SECURITY
2128 dentry
->d_op
= &pid_dentry_operations
;
2130 d_add(dentry
, inode
);
2131 /* Close the race of the process dying before we return the dentry */
2132 if (pid_revalidate(dentry
, NULL
))
2136 put_task_struct(task
);
2138 put_task_struct(leader
);
2144 * Find the first tgid to return to user space.
2146 * Usually this is just whatever follows &init_task, but if the users
2147 * buffer was too small to hold the full list or there was a seek into
2148 * the middle of the directory we have more work to do.
2150 * In the case of a short read we start with find_task_by_pid.
2152 * In the case of a seek we start with &init_task and walk nr
2155 static struct task_struct
*first_tgid(int tgid
, unsigned int nr
)
2157 struct task_struct
*pos
;
2160 pos
= find_task_by_pid(tgid
);
2161 if (pos
&& thread_group_leader(pos
))
2164 /* If nr exceeds the number of processes get out quickly */
2166 if (nr
&& nr
>= nr_processes())
2169 /* If we haven't found our starting place yet start with
2170 * the init_task and walk nr tasks forward.
2172 for (pos
= next_task(&init_task
); nr
> 0; --nr
) {
2173 pos
= next_task(pos
);
2174 if (pos
== &init_task
) {
2180 get_task_struct(pos
);
2187 * Find the next task in the task list.
2188 * Return NULL if we loop or there is any error.
2190 * The reference to the input task_struct is released.
2192 static struct task_struct
*next_tgid(struct task_struct
*start
)
2194 struct task_struct
*pos
;
2197 if (pid_alive(start
))
2198 pos
= next_task(start
);
2199 if (pid_alive(pos
) && (pos
!= &init_task
)) {
2200 get_task_struct(pos
);
2206 put_task_struct(start
);
2210 /* for the /proc/ directory itself, after non-process stuff has been done */
2211 int proc_pid_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
2213 char buf
[PROC_NUMBUF
];
2214 unsigned int nr
= filp
->f_pos
- FIRST_PROCESS_ENTRY
;
2215 struct task_struct
*task
;
2219 ino_t ino
= fake_ino(0,PROC_TGID_INO
);
2220 if (filldir(dirent
, "self", 4, filp
->f_pos
, ino
, DT_LNK
) < 0)
2227 /* f_version caches the tgid value that the last readdir call couldn't
2228 * return. lseek aka telldir automagically resets f_version to 0.
2230 tgid
= filp
->f_version
;
2231 filp
->f_version
= 0;
2232 for (task
= first_tgid(tgid
, nr
);
2234 task
= next_tgid(task
), filp
->f_pos
++) {
2238 len
= snprintf(buf
, sizeof(buf
), "%d", tgid
);
2239 ino
= fake_ino(tgid
, PROC_TGID_INO
);
2240 if (filldir(dirent
, buf
, len
, filp
->f_pos
, ino
, DT_DIR
) < 0) {
2241 /* returning this tgid failed, save it as the first
2242 * pid for the next readir call */
2243 filp
->f_version
= tgid
;
2244 put_task_struct(task
);
2252 * Find the first tid of a thread group to return to user space.
2254 * Usually this is just the thread group leader, but if the users
2255 * buffer was too small or there was a seek into the middle of the
2256 * directory we have more work todo.
2258 * In the case of a short read we start with find_task_by_pid.
2260 * In the case of a seek we start with the leader and walk nr
2263 static struct task_struct
*first_tid(struct task_struct
*leader
,
2266 struct task_struct
*pos
;
2269 /* Attempt to start with the pid of a thread */
2270 if (tid
&& (nr
> 0)) {
2271 pos
= find_task_by_pid(tid
);
2272 if (pos
&& (pos
->group_leader
== leader
))
2276 /* If nr exceeds the number of threads there is nothing todo */
2278 if (nr
&& nr
>= get_nr_threads(leader
))
2281 /* If we haven't found our starting place yet start
2282 * with the leader and walk nr threads forward.
2284 for (pos
= leader
; nr
> 0; --nr
) {
2285 pos
= next_thread(pos
);
2286 if (pos
== leader
) {
2292 get_task_struct(pos
);
2299 * Find the next thread in the thread list.
2300 * Return NULL if there is an error or no next thread.
2302 * The reference to the input task_struct is released.
2304 static struct task_struct
*next_tid(struct task_struct
*start
)
2306 struct task_struct
*pos
= NULL
;
2308 if (pid_alive(start
)) {
2309 pos
= next_thread(start
);
2310 if (thread_group_leader(pos
))
2313 get_task_struct(pos
);
2316 put_task_struct(start
);
2320 /* for the /proc/TGID/task/ directories */
2321 static int proc_task_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
2323 char buf
[PROC_NUMBUF
];
2324 struct dentry
*dentry
= filp
->f_dentry
;
2325 struct inode
*inode
= dentry
->d_inode
;
2326 struct task_struct
*leader
= get_proc_task(inode
);
2327 struct task_struct
*task
;
2328 int retval
= -ENOENT
;
2331 unsigned long pos
= filp
->f_pos
; /* avoiding "long long" filp->f_pos */
2340 if (filldir(dirent
, ".", 1, pos
, ino
, DT_DIR
) < 0)
2345 ino
= parent_ino(dentry
);
2346 if (filldir(dirent
, "..", 2, pos
, ino
, DT_DIR
) < 0)
2352 /* f_version caches the tgid value that the last readdir call couldn't
2353 * return. lseek aka telldir automagically resets f_version to 0.
2355 tid
= filp
->f_version
;
2356 filp
->f_version
= 0;
2357 for (task
= first_tid(leader
, tid
, pos
- 2);
2359 task
= next_tid(task
), pos
++) {
2362 len
= snprintf(buf
, sizeof(buf
), "%d", tid
);
2363 ino
= fake_ino(tid
, PROC_TID_INO
);
2364 if (filldir(dirent
, buf
, len
, pos
, ino
, DT_DIR
< 0)) {
2365 /* returning this tgid failed, save it as the first
2366 * pid for the next readir call */
2367 filp
->f_version
= tid
;
2368 put_task_struct(task
);
2374 put_task_struct(leader
);
2379 static int proc_task_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
2381 struct inode
*inode
= dentry
->d_inode
;
2382 struct task_struct
*p
= get_proc_task(inode
);
2383 generic_fillattr(inode
, stat
);
2387 stat
->nlink
+= get_nr_threads(p
);