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/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/kallsyms.h>
66 #include <linux/mount.h>
67 #include <linux/security.h>
68 #include <linux/ptrace.h>
69 #include <linux/seccomp.h>
70 #include <linux/cpuset.h>
71 #include <linux/audit.h>
75 * For hysterical raisins we keep the same inumbers as in the old procfs.
76 * Feel free to change the macro below - just keep the range distinct from
77 * inumbers of the rest of procfs (currently those are in 0x0000--0xffff).
78 * As soon as we'll get a separate superblock we will be able to forget
79 * about magical ranges too.
82 #define fake_ino(pid,ino) (((pid)<<16)|(ino))
84 enum pid_directory_inos
{
106 #ifdef CONFIG_SCHEDSTATS
109 #ifdef CONFIG_CPUSETS
112 #ifdef CONFIG_SECURITY
114 PROC_TGID_ATTR_CURRENT
,
117 PROC_TGID_ATTR_FSCREATE
,
119 #ifdef CONFIG_AUDITSYSCALL
124 PROC_TGID_OOM_ADJUST
,
128 #ifdef CONFIG_SECCOMP
145 #ifdef CONFIG_SCHEDSTATS
148 #ifdef CONFIG_CPUSETS
151 #ifdef CONFIG_SECURITY
153 PROC_TID_ATTR_CURRENT
,
156 PROC_TID_ATTR_FSCREATE
,
158 #ifdef CONFIG_AUDITSYSCALL
161 PROC_TID_FD_DIR
= 0x8000, /* 0x8000-0xffff */
173 #define E(type,name,mode) {(type),sizeof(name)-1,(name),(mode)}
175 static struct pid_entry tgid_base_stuff
[] = {
176 E(PROC_TGID_TASK
, "task", S_IFDIR
|S_IRUGO
|S_IXUGO
),
177 E(PROC_TGID_FD
, "fd", S_IFDIR
|S_IRUSR
|S_IXUSR
),
178 E(PROC_TGID_ENVIRON
, "environ", S_IFREG
|S_IRUSR
),
179 E(PROC_TGID_AUXV
, "auxv", S_IFREG
|S_IRUSR
),
180 E(PROC_TGID_STATUS
, "status", S_IFREG
|S_IRUGO
),
181 E(PROC_TGID_CMDLINE
, "cmdline", S_IFREG
|S_IRUGO
),
182 E(PROC_TGID_STAT
, "stat", S_IFREG
|S_IRUGO
),
183 E(PROC_TGID_STATM
, "statm", S_IFREG
|S_IRUGO
),
184 E(PROC_TGID_MAPS
, "maps", S_IFREG
|S_IRUGO
),
186 E(PROC_TGID_NUMA_MAPS
, "numa_maps", S_IFREG
|S_IRUGO
),
188 E(PROC_TGID_MEM
, "mem", S_IFREG
|S_IRUSR
|S_IWUSR
),
189 #ifdef CONFIG_SECCOMP
190 E(PROC_TGID_SECCOMP
, "seccomp", S_IFREG
|S_IRUSR
|S_IWUSR
),
192 E(PROC_TGID_CWD
, "cwd", S_IFLNK
|S_IRWXUGO
),
193 E(PROC_TGID_ROOT
, "root", S_IFLNK
|S_IRWXUGO
),
194 E(PROC_TGID_EXE
, "exe", S_IFLNK
|S_IRWXUGO
),
195 E(PROC_TGID_MOUNTS
, "mounts", S_IFREG
|S_IRUGO
),
196 E(PROC_TGID_SMAPS
, "smaps", S_IFREG
|S_IRUGO
),
197 #ifdef CONFIG_SECURITY
198 E(PROC_TGID_ATTR
, "attr", S_IFDIR
|S_IRUGO
|S_IXUGO
),
200 #ifdef CONFIG_KALLSYMS
201 E(PROC_TGID_WCHAN
, "wchan", S_IFREG
|S_IRUGO
),
203 #ifdef CONFIG_SCHEDSTATS
204 E(PROC_TGID_SCHEDSTAT
, "schedstat", S_IFREG
|S_IRUGO
),
206 #ifdef CONFIG_CPUSETS
207 E(PROC_TGID_CPUSET
, "cpuset", S_IFREG
|S_IRUGO
),
209 E(PROC_TGID_OOM_SCORE
, "oom_score",S_IFREG
|S_IRUGO
),
210 E(PROC_TGID_OOM_ADJUST
,"oom_adj", S_IFREG
|S_IRUGO
|S_IWUSR
),
211 #ifdef CONFIG_AUDITSYSCALL
212 E(PROC_TGID_LOGINUID
, "loginuid", S_IFREG
|S_IWUSR
|S_IRUGO
),
216 static struct pid_entry tid_base_stuff
[] = {
217 E(PROC_TID_FD
, "fd", S_IFDIR
|S_IRUSR
|S_IXUSR
),
218 E(PROC_TID_ENVIRON
, "environ", S_IFREG
|S_IRUSR
),
219 E(PROC_TID_AUXV
, "auxv", S_IFREG
|S_IRUSR
),
220 E(PROC_TID_STATUS
, "status", S_IFREG
|S_IRUGO
),
221 E(PROC_TID_CMDLINE
, "cmdline", S_IFREG
|S_IRUGO
),
222 E(PROC_TID_STAT
, "stat", S_IFREG
|S_IRUGO
),
223 E(PROC_TID_STATM
, "statm", S_IFREG
|S_IRUGO
),
224 E(PROC_TID_MAPS
, "maps", S_IFREG
|S_IRUGO
),
226 E(PROC_TID_NUMA_MAPS
, "numa_maps", S_IFREG
|S_IRUGO
),
228 E(PROC_TID_MEM
, "mem", S_IFREG
|S_IRUSR
|S_IWUSR
),
229 #ifdef CONFIG_SECCOMP
230 E(PROC_TID_SECCOMP
, "seccomp", S_IFREG
|S_IRUSR
|S_IWUSR
),
232 E(PROC_TID_CWD
, "cwd", S_IFLNK
|S_IRWXUGO
),
233 E(PROC_TID_ROOT
, "root", S_IFLNK
|S_IRWXUGO
),
234 E(PROC_TID_EXE
, "exe", S_IFLNK
|S_IRWXUGO
),
235 E(PROC_TID_MOUNTS
, "mounts", S_IFREG
|S_IRUGO
),
236 E(PROC_TID_SMAPS
, "smaps", S_IFREG
|S_IRUGO
),
237 #ifdef CONFIG_SECURITY
238 E(PROC_TID_ATTR
, "attr", S_IFDIR
|S_IRUGO
|S_IXUGO
),
240 #ifdef CONFIG_KALLSYMS
241 E(PROC_TID_WCHAN
, "wchan", S_IFREG
|S_IRUGO
),
243 #ifdef CONFIG_SCHEDSTATS
244 E(PROC_TID_SCHEDSTAT
, "schedstat",S_IFREG
|S_IRUGO
),
246 #ifdef CONFIG_CPUSETS
247 E(PROC_TID_CPUSET
, "cpuset", S_IFREG
|S_IRUGO
),
249 E(PROC_TID_OOM_SCORE
, "oom_score",S_IFREG
|S_IRUGO
),
250 E(PROC_TID_OOM_ADJUST
, "oom_adj", S_IFREG
|S_IRUGO
|S_IWUSR
),
251 #ifdef CONFIG_AUDITSYSCALL
252 E(PROC_TID_LOGINUID
, "loginuid", S_IFREG
|S_IWUSR
|S_IRUGO
),
257 #ifdef CONFIG_SECURITY
258 static struct pid_entry tgid_attr_stuff
[] = {
259 E(PROC_TGID_ATTR_CURRENT
, "current", S_IFREG
|S_IRUGO
|S_IWUGO
),
260 E(PROC_TGID_ATTR_PREV
, "prev", S_IFREG
|S_IRUGO
),
261 E(PROC_TGID_ATTR_EXEC
, "exec", S_IFREG
|S_IRUGO
|S_IWUGO
),
262 E(PROC_TGID_ATTR_FSCREATE
, "fscreate", S_IFREG
|S_IRUGO
|S_IWUGO
),
265 static struct pid_entry tid_attr_stuff
[] = {
266 E(PROC_TID_ATTR_CURRENT
, "current", S_IFREG
|S_IRUGO
|S_IWUGO
),
267 E(PROC_TID_ATTR_PREV
, "prev", S_IFREG
|S_IRUGO
),
268 E(PROC_TID_ATTR_EXEC
, "exec", S_IFREG
|S_IRUGO
|S_IWUGO
),
269 E(PROC_TID_ATTR_FSCREATE
, "fscreate", S_IFREG
|S_IRUGO
|S_IWUGO
),
276 static int proc_fd_link(struct inode
*inode
, struct dentry
**dentry
, struct vfsmount
**mnt
)
278 struct task_struct
*task
= proc_task(inode
);
279 struct files_struct
*files
;
281 int fd
= proc_type(inode
) - PROC_TID_FD_DIR
;
283 files
= get_files_struct(task
);
285 spin_lock(&files
->file_lock
);
286 file
= fcheck_files(files
, fd
);
288 *mnt
= mntget(file
->f_vfsmnt
);
289 *dentry
= dget(file
->f_dentry
);
290 spin_unlock(&files
->file_lock
);
291 put_files_struct(files
);
294 spin_unlock(&files
->file_lock
);
295 put_files_struct(files
);
300 static int proc_cwd_link(struct inode
*inode
, struct dentry
**dentry
, struct vfsmount
**mnt
)
302 struct fs_struct
*fs
;
303 int result
= -ENOENT
;
304 task_lock(proc_task(inode
));
305 fs
= proc_task(inode
)->fs
;
307 atomic_inc(&fs
->count
);
308 task_unlock(proc_task(inode
));
310 read_lock(&fs
->lock
);
311 *mnt
= mntget(fs
->pwdmnt
);
312 *dentry
= dget(fs
->pwd
);
313 read_unlock(&fs
->lock
);
320 static int proc_root_link(struct inode
*inode
, struct dentry
**dentry
, struct vfsmount
**mnt
)
322 struct fs_struct
*fs
;
323 int result
= -ENOENT
;
324 task_lock(proc_task(inode
));
325 fs
= proc_task(inode
)->fs
;
327 atomic_inc(&fs
->count
);
328 task_unlock(proc_task(inode
));
330 read_lock(&fs
->lock
);
331 *mnt
= mntget(fs
->rootmnt
);
332 *dentry
= dget(fs
->root
);
333 read_unlock(&fs
->lock
);
340 #define MAY_PTRACE(task) \
341 (task == current || \
342 (task->parent == current && \
343 (task->ptrace & PT_PTRACED) && \
344 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
345 security_ptrace(current,task) == 0))
347 static int may_ptrace_attach(struct task_struct
*task
)
355 if (((current
->uid
!= task
->euid
) ||
356 (current
->uid
!= task
->suid
) ||
357 (current
->uid
!= task
->uid
) ||
358 (current
->gid
!= task
->egid
) ||
359 (current
->gid
!= task
->sgid
) ||
360 (current
->gid
!= task
->gid
)) && !capable(CAP_SYS_PTRACE
))
363 if (task
->mm
->dumpable
!= 1 && !capable(CAP_SYS_PTRACE
))
365 if (security_ptrace(current
, task
))
374 static int proc_pid_environ(struct task_struct
*task
, char * buffer
)
377 struct mm_struct
*mm
= get_task_mm(task
);
379 unsigned int len
= mm
->env_end
- mm
->env_start
;
382 res
= access_process_vm(task
, mm
->env_start
, buffer
, len
, 0);
383 if (!may_ptrace_attach(task
))
390 static int proc_pid_cmdline(struct task_struct
*task
, char * buffer
)
394 struct mm_struct
*mm
= get_task_mm(task
);
398 goto out_mm
; /* Shh! No looking before we're done */
400 len
= mm
->arg_end
- mm
->arg_start
;
405 res
= access_process_vm(task
, mm
->arg_start
, buffer
, len
, 0);
407 // If the nul at the end of args has been overwritten, then
408 // assume application is using setproctitle(3).
409 if (res
> 0 && buffer
[res
-1] != '\0' && len
< PAGE_SIZE
) {
410 len
= strnlen(buffer
, res
);
414 len
= mm
->env_end
- mm
->env_start
;
415 if (len
> PAGE_SIZE
- res
)
416 len
= PAGE_SIZE
- res
;
417 res
+= access_process_vm(task
, mm
->env_start
, buffer
+res
, len
, 0);
418 res
= strnlen(buffer
, res
);
427 static int proc_pid_auxv(struct task_struct
*task
, char *buffer
)
430 struct mm_struct
*mm
= get_task_mm(task
);
432 unsigned int nwords
= 0;
435 while (mm
->saved_auxv
[nwords
- 2] != 0); /* AT_NULL */
436 res
= nwords
* sizeof(mm
->saved_auxv
[0]);
439 memcpy(buffer
, mm
->saved_auxv
, res
);
446 #ifdef CONFIG_KALLSYMS
448 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
449 * Returns the resolved symbol. If that fails, simply return the address.
451 static int proc_pid_wchan(struct task_struct
*task
, char *buffer
)
454 const char *sym_name
;
455 unsigned long wchan
, size
, offset
;
456 char namebuf
[KSYM_NAME_LEN
+1];
458 wchan
= get_wchan(task
);
460 sym_name
= kallsyms_lookup(wchan
, &size
, &offset
, &modname
, namebuf
);
462 return sprintf(buffer
, "%s", sym_name
);
463 return sprintf(buffer
, "%lu", wchan
);
465 #endif /* CONFIG_KALLSYMS */
467 #ifdef CONFIG_SCHEDSTATS
469 * Provides /proc/PID/schedstat
471 static int proc_pid_schedstat(struct task_struct
*task
, char *buffer
)
473 return sprintf(buffer
, "%lu %lu %lu\n",
474 task
->sched_info
.cpu_time
,
475 task
->sched_info
.run_delay
,
476 task
->sched_info
.pcnt
);
480 /* The badness from the OOM killer */
481 unsigned long badness(struct task_struct
*p
, unsigned long uptime
);
482 static int proc_oom_score(struct task_struct
*task
, char *buffer
)
484 unsigned long points
;
485 struct timespec uptime
;
487 do_posix_clock_monotonic_gettime(&uptime
);
488 points
= badness(task
, uptime
.tv_sec
);
489 return sprintf(buffer
, "%lu\n", points
);
492 /************************************************************************/
493 /* Here the fs part begins */
494 /************************************************************************/
496 /* permission checks */
498 static int proc_check_root(struct inode
*inode
)
500 struct dentry
*de
, *base
, *root
;
501 struct vfsmount
*our_vfsmnt
, *vfsmnt
, *mnt
;
504 if (proc_root_link(inode
, &root
, &vfsmnt
)) /* Ewww... */
506 read_lock(¤t
->fs
->lock
);
507 our_vfsmnt
= mntget(current
->fs
->rootmnt
);
508 base
= dget(current
->fs
->root
);
509 read_unlock(¤t
->fs
->lock
);
511 spin_lock(&vfsmount_lock
);
515 while (vfsmnt
!= our_vfsmnt
) {
516 if (vfsmnt
== vfsmnt
->mnt_parent
)
518 de
= vfsmnt
->mnt_mountpoint
;
519 vfsmnt
= vfsmnt
->mnt_parent
;
522 if (!is_subdir(de
, base
))
524 spin_unlock(&vfsmount_lock
);
533 spin_unlock(&vfsmount_lock
);
538 static int proc_permission(struct inode
*inode
, int mask
, struct nameidata
*nd
)
540 if (generic_permission(inode
, mask
, NULL
) != 0)
542 return proc_check_root(inode
);
545 extern struct seq_operations proc_pid_maps_op
;
546 static int maps_open(struct inode
*inode
, struct file
*file
)
548 struct task_struct
*task
= proc_task(inode
);
549 int ret
= seq_open(file
, &proc_pid_maps_op
);
551 struct seq_file
*m
= file
->private_data
;
557 static struct file_operations proc_maps_operations
= {
561 .release
= seq_release
,
565 extern struct seq_operations proc_pid_numa_maps_op
;
566 static int numa_maps_open(struct inode
*inode
, struct file
*file
)
568 struct task_struct
*task
= proc_task(inode
);
569 int ret
= seq_open(file
, &proc_pid_numa_maps_op
);
571 struct seq_file
*m
= file
->private_data
;
577 static struct file_operations proc_numa_maps_operations
= {
578 .open
= numa_maps_open
,
581 .release
= seq_release
,
585 extern struct seq_operations proc_pid_smaps_op
;
586 static int smaps_open(struct inode
*inode
, struct file
*file
)
588 struct task_struct
*task
= proc_task(inode
);
589 int ret
= seq_open(file
, &proc_pid_smaps_op
);
591 struct seq_file
*m
= file
->private_data
;
597 static struct file_operations proc_smaps_operations
= {
601 .release
= seq_release
,
604 extern struct seq_operations mounts_op
;
605 static int mounts_open(struct inode
*inode
, struct file
*file
)
607 struct task_struct
*task
= proc_task(inode
);
608 int ret
= seq_open(file
, &mounts_op
);
611 struct seq_file
*m
= file
->private_data
;
612 struct namespace *namespace;
614 namespace = task
->namespace;
616 get_namespace(namespace);
620 m
->private = namespace;
622 seq_release(inode
, file
);
629 static int mounts_release(struct inode
*inode
, struct file
*file
)
631 struct seq_file
*m
= file
->private_data
;
632 struct namespace *namespace = m
->private;
633 put_namespace(namespace);
634 return seq_release(inode
, file
);
637 static struct file_operations proc_mounts_operations
= {
641 .release
= mounts_release
,
644 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
646 static ssize_t
proc_info_read(struct file
* file
, char __user
* buf
,
647 size_t count
, loff_t
*ppos
)
649 struct inode
* inode
= file
->f_dentry
->d_inode
;
652 struct task_struct
*task
= proc_task(inode
);
654 if (count
> PROC_BLOCK_SIZE
)
655 count
= PROC_BLOCK_SIZE
;
656 if (!(page
= __get_free_page(GFP_KERNEL
)))
659 length
= PROC_I(inode
)->op
.proc_read(task
, (char*)page
);
662 length
= simple_read_from_buffer(buf
, count
, ppos
, (char *)page
, length
);
667 static struct file_operations proc_info_file_operations
= {
668 .read
= proc_info_read
,
671 static int mem_open(struct inode
* inode
, struct file
* file
)
673 file
->private_data
= (void*)((long)current
->self_exec_id
);
677 static ssize_t
mem_read(struct file
* file
, char __user
* buf
,
678 size_t count
, loff_t
*ppos
)
680 struct task_struct
*task
= proc_task(file
->f_dentry
->d_inode
);
682 unsigned long src
= *ppos
;
684 struct mm_struct
*mm
;
686 if (!MAY_PTRACE(task
) || !may_ptrace_attach(task
))
690 page
= (char *)__get_free_page(GFP_USER
);
696 mm
= get_task_mm(task
);
702 if (file
->private_data
!= (void*)((long)current
->self_exec_id
))
708 int this_len
, retval
;
710 this_len
= (count
> PAGE_SIZE
) ? PAGE_SIZE
: count
;
711 retval
= access_process_vm(task
, src
, page
, this_len
, 0);
712 if (!retval
|| !MAY_PTRACE(task
) || !may_ptrace_attach(task
)) {
718 if (copy_to_user(buf
, page
, retval
)) {
733 free_page((unsigned long) page
);
738 #define mem_write NULL
741 /* This is a security hazard */
742 static ssize_t
mem_write(struct file
* file
, const char * buf
,
743 size_t count
, loff_t
*ppos
)
747 struct task_struct
*task
= proc_task(file
->f_dentry
->d_inode
);
748 unsigned long dst
= *ppos
;
750 if (!MAY_PTRACE(task
) || !may_ptrace_attach(task
))
753 page
= (char *)__get_free_page(GFP_USER
);
758 int this_len
, retval
;
760 this_len
= (count
> PAGE_SIZE
) ? PAGE_SIZE
: count
;
761 if (copy_from_user(page
, buf
, this_len
)) {
765 retval
= access_process_vm(task
, dst
, page
, this_len
, 1);
777 free_page((unsigned long) page
);
782 static loff_t
mem_lseek(struct file
* file
, loff_t offset
, int orig
)
786 file
->f_pos
= offset
;
789 file
->f_pos
+= offset
;
794 force_successful_syscall_return();
798 static struct file_operations proc_mem_operations
= {
805 static ssize_t
oom_adjust_read(struct file
*file
, char __user
*buf
,
806 size_t count
, loff_t
*ppos
)
808 struct task_struct
*task
= proc_task(file
->f_dentry
->d_inode
);
811 int oom_adjust
= task
->oomkilladj
;
812 loff_t __ppos
= *ppos
;
814 len
= sprintf(buffer
, "%i\n", oom_adjust
);
817 if (count
> len
-__ppos
)
819 if (copy_to_user(buf
, buffer
+ __ppos
, count
))
821 *ppos
= __ppos
+ count
;
825 static ssize_t
oom_adjust_write(struct file
*file
, const char __user
*buf
,
826 size_t count
, loff_t
*ppos
)
828 struct task_struct
*task
= proc_task(file
->f_dentry
->d_inode
);
829 char buffer
[8], *end
;
832 if (!capable(CAP_SYS_RESOURCE
))
834 memset(buffer
, 0, 8);
837 if (copy_from_user(buffer
, buf
, count
))
839 oom_adjust
= simple_strtol(buffer
, &end
, 0);
840 if ((oom_adjust
< -16 || oom_adjust
> 15) && oom_adjust
!= OOM_DISABLE
)
844 task
->oomkilladj
= oom_adjust
;
845 if (end
- buffer
== 0)
850 static struct file_operations proc_oom_adjust_operations
= {
851 .read
= oom_adjust_read
,
852 .write
= oom_adjust_write
,
855 static struct inode_operations proc_mem_inode_operations
= {
856 .permission
= proc_permission
,
859 #ifdef CONFIG_AUDITSYSCALL
861 static ssize_t
proc_loginuid_read(struct file
* file
, char __user
* buf
,
862 size_t count
, loff_t
*ppos
)
864 struct inode
* inode
= file
->f_dentry
->d_inode
;
865 struct task_struct
*task
= proc_task(inode
);
867 char tmpbuf
[TMPBUFLEN
];
869 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
870 audit_get_loginuid(task
->audit_context
));
871 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
874 static ssize_t
proc_loginuid_write(struct file
* file
, const char __user
* buf
,
875 size_t count
, loff_t
*ppos
)
877 struct inode
* inode
= file
->f_dentry
->d_inode
;
880 struct task_struct
*task
= proc_task(inode
);
883 if (!capable(CAP_AUDIT_CONTROL
))
889 if (count
> PAGE_SIZE
)
893 /* No partial writes. */
896 page
= (char*)__get_free_page(GFP_USER
);
900 if (copy_from_user(page
, buf
, count
))
903 loginuid
= simple_strtoul(page
, &tmp
, 10);
909 length
= audit_set_loginuid(task
, loginuid
);
910 if (likely(length
== 0))
914 free_page((unsigned long) page
);
918 static struct file_operations proc_loginuid_operations
= {
919 .read
= proc_loginuid_read
,
920 .write
= proc_loginuid_write
,
924 #ifdef CONFIG_SECCOMP
925 static ssize_t
seccomp_read(struct file
*file
, char __user
*buf
,
926 size_t count
, loff_t
*ppos
)
928 struct task_struct
*tsk
= proc_task(file
->f_dentry
->d_inode
);
930 loff_t __ppos
= *ppos
;
933 /* no need to print the trailing zero, so use only len */
934 len
= sprintf(__buf
, "%u\n", tsk
->seccomp
.mode
);
937 if (count
> len
- __ppos
)
938 count
= len
- __ppos
;
939 if (copy_to_user(buf
, __buf
+ __ppos
, count
))
941 *ppos
= __ppos
+ count
;
945 static ssize_t
seccomp_write(struct file
*file
, const char __user
*buf
,
946 size_t count
, loff_t
*ppos
)
948 struct task_struct
*tsk
= proc_task(file
->f_dentry
->d_inode
);
949 char __buf
[20], *end
;
950 unsigned int seccomp_mode
;
952 /* can set it only once to be even more secure */
953 if (unlikely(tsk
->seccomp
.mode
))
956 memset(__buf
, 0, sizeof(__buf
));
957 count
= min(count
, sizeof(__buf
) - 1);
958 if (copy_from_user(__buf
, buf
, count
))
960 seccomp_mode
= simple_strtoul(__buf
, &end
, 0);
963 if (seccomp_mode
&& seccomp_mode
<= NR_SECCOMP_MODES
) {
964 tsk
->seccomp
.mode
= seccomp_mode
;
965 set_tsk_thread_flag(tsk
, TIF_SECCOMP
);
968 if (unlikely(!(end
- __buf
)))
973 static struct file_operations proc_seccomp_operations
= {
974 .read
= seccomp_read
,
975 .write
= seccomp_write
,
977 #endif /* CONFIG_SECCOMP */
979 static void *proc_pid_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
981 struct inode
*inode
= dentry
->d_inode
;
984 /* We don't need a base pointer in the /proc filesystem */
987 if (current
->fsuid
!= inode
->i_uid
&& !capable(CAP_DAC_OVERRIDE
))
989 error
= proc_check_root(inode
);
993 error
= PROC_I(inode
)->op
.proc_get_link(inode
, &nd
->dentry
, &nd
->mnt
);
994 nd
->last_type
= LAST_BIND
;
996 return ERR_PTR(error
);
999 static int do_proc_readlink(struct dentry
*dentry
, struct vfsmount
*mnt
,
1000 char __user
*buffer
, int buflen
)
1002 struct inode
* inode
;
1003 char *tmp
= (char*)__get_free_page(GFP_KERNEL
), *path
;
1009 inode
= dentry
->d_inode
;
1010 path
= d_path(dentry
, mnt
, tmp
, PAGE_SIZE
);
1011 len
= PTR_ERR(path
);
1014 len
= tmp
+ PAGE_SIZE
- 1 - path
;
1018 if (copy_to_user(buffer
, path
, len
))
1021 free_page((unsigned long)tmp
);
1025 static int proc_pid_readlink(struct dentry
* dentry
, char __user
* buffer
, int buflen
)
1027 int error
= -EACCES
;
1028 struct inode
*inode
= dentry
->d_inode
;
1030 struct vfsmount
*mnt
= NULL
;
1034 if (current
->fsuid
!= inode
->i_uid
&& !capable(CAP_DAC_OVERRIDE
))
1036 error
= proc_check_root(inode
);
1040 error
= PROC_I(inode
)->op
.proc_get_link(inode
, &de
, &mnt
);
1044 error
= do_proc_readlink(de
, mnt
, buffer
, buflen
);
1052 static struct inode_operations proc_pid_link_inode_operations
= {
1053 .readlink
= proc_pid_readlink
,
1054 .follow_link
= proc_pid_follow_link
1059 static int proc_readfd(struct file
* filp
, void * dirent
, filldir_t filldir
)
1061 struct inode
*inode
= filp
->f_dentry
->d_inode
;
1062 struct task_struct
*p
= proc_task(inode
);
1063 unsigned int fd
, tid
, ino
;
1066 struct files_struct
* files
;
1077 if (filldir(dirent
, ".", 1, 0, inode
->i_ino
, DT_DIR
) < 0)
1081 ino
= fake_ino(tid
, PROC_TID_INO
);
1082 if (filldir(dirent
, "..", 2, 1, ino
, DT_DIR
) < 0)
1086 files
= get_files_struct(p
);
1089 spin_lock(&files
->file_lock
);
1090 for (fd
= filp
->f_pos
-2;
1091 fd
< files
->max_fds
;
1092 fd
++, filp
->f_pos
++) {
1095 if (!fcheck_files(files
, fd
))
1097 spin_unlock(&files
->file_lock
);
1103 buf
[j
] = '0' + (i
% 10);
1107 ino
= fake_ino(tid
, PROC_TID_FD_DIR
+ fd
);
1108 if (filldir(dirent
, buf
+j
, NUMBUF
-j
, fd
+2, ino
, DT_LNK
) < 0) {
1109 spin_lock(&files
->file_lock
);
1112 spin_lock(&files
->file_lock
);
1114 spin_unlock(&files
->file_lock
);
1115 put_files_struct(files
);
1121 static int proc_pident_readdir(struct file
*filp
,
1122 void *dirent
, filldir_t filldir
,
1123 struct pid_entry
*ents
, unsigned int nents
)
1127 struct dentry
*dentry
= filp
->f_dentry
;
1128 struct inode
*inode
= dentry
->d_inode
;
1129 struct pid_entry
*p
;
1134 if (!pid_alive(proc_task(inode
)))
1138 pid
= proc_task(inode
)->pid
;
1143 if (filldir(dirent
, ".", 1, i
, ino
, DT_DIR
) < 0)
1149 ino
= parent_ino(dentry
);
1150 if (filldir(dirent
, "..", 2, i
, ino
, DT_DIR
) < 0)
1163 if (filldir(dirent
, p
->name
, p
->len
, filp
->f_pos
,
1164 fake_ino(pid
, p
->type
), p
->mode
>> 12) < 0)
1176 static int proc_tgid_base_readdir(struct file
* filp
,
1177 void * dirent
, filldir_t filldir
)
1179 return proc_pident_readdir(filp
,dirent
,filldir
,
1180 tgid_base_stuff
,ARRAY_SIZE(tgid_base_stuff
));
1183 static int proc_tid_base_readdir(struct file
* filp
,
1184 void * dirent
, filldir_t filldir
)
1186 return proc_pident_readdir(filp
,dirent
,filldir
,
1187 tid_base_stuff
,ARRAY_SIZE(tid_base_stuff
));
1190 /* building an inode */
1192 static int task_dumpable(struct task_struct
*task
)
1195 struct mm_struct
*mm
;
1200 dumpable
= mm
->dumpable
;
1208 static struct inode
*proc_pid_make_inode(struct super_block
* sb
, struct task_struct
*task
, int ino
)
1210 struct inode
* inode
;
1211 struct proc_inode
*ei
;
1213 /* We need a new inode */
1215 inode
= new_inode(sb
);
1222 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1223 inode
->i_ino
= fake_ino(task
->pid
, ino
);
1225 if (!pid_alive(task
))
1229 * grab the reference to task.
1231 get_task_struct(task
);
1236 if (ino
== PROC_TGID_INO
|| ino
== PROC_TID_INO
|| task_dumpable(task
)) {
1237 inode
->i_uid
= task
->euid
;
1238 inode
->i_gid
= task
->egid
;
1240 security_task_to_inode(task
, inode
);
1254 * Exceptional case: normally we are not allowed to unhash a busy
1255 * directory. In this case, however, we can do it - no aliasing problems
1256 * due to the way we treat inodes.
1258 * Rewrite the inode's ownerships here because the owning task may have
1259 * performed a setuid(), etc.
1261 static int pid_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1263 struct inode
*inode
= dentry
->d_inode
;
1264 struct task_struct
*task
= proc_task(inode
);
1265 if (pid_alive(task
)) {
1266 if (proc_type(inode
) == PROC_TGID_INO
|| proc_type(inode
) == PROC_TID_INO
|| task_dumpable(task
)) {
1267 inode
->i_uid
= task
->euid
;
1268 inode
->i_gid
= task
->egid
;
1273 security_task_to_inode(task
, inode
);
1280 static int tid_fd_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1282 struct inode
*inode
= dentry
->d_inode
;
1283 struct task_struct
*task
= proc_task(inode
);
1284 int fd
= proc_type(inode
) - PROC_TID_FD_DIR
;
1285 struct files_struct
*files
;
1287 files
= get_files_struct(task
);
1289 spin_lock(&files
->file_lock
);
1290 if (fcheck_files(files
, fd
)) {
1291 spin_unlock(&files
->file_lock
);
1292 put_files_struct(files
);
1293 if (task_dumpable(task
)) {
1294 inode
->i_uid
= task
->euid
;
1295 inode
->i_gid
= task
->egid
;
1300 security_task_to_inode(task
, inode
);
1303 spin_unlock(&files
->file_lock
);
1304 put_files_struct(files
);
1310 static void pid_base_iput(struct dentry
*dentry
, struct inode
*inode
)
1312 struct task_struct
*task
= proc_task(inode
);
1313 spin_lock(&task
->proc_lock
);
1314 if (task
->proc_dentry
== dentry
)
1315 task
->proc_dentry
= NULL
;
1316 spin_unlock(&task
->proc_lock
);
1320 static int pid_delete_dentry(struct dentry
* dentry
)
1322 /* Is the task we represent dead?
1323 * If so, then don't put the dentry on the lru list,
1324 * kill it immediately.
1326 return !pid_alive(proc_task(dentry
->d_inode
));
1329 static struct dentry_operations tid_fd_dentry_operations
=
1331 .d_revalidate
= tid_fd_revalidate
,
1332 .d_delete
= pid_delete_dentry
,
1335 static struct dentry_operations pid_dentry_operations
=
1337 .d_revalidate
= pid_revalidate
,
1338 .d_delete
= pid_delete_dentry
,
1341 static struct dentry_operations pid_base_dentry_operations
=
1343 .d_revalidate
= pid_revalidate
,
1344 .d_iput
= pid_base_iput
,
1345 .d_delete
= pid_delete_dentry
,
1350 static unsigned name_to_int(struct dentry
*dentry
)
1352 const char *name
= dentry
->d_name
.name
;
1353 int len
= dentry
->d_name
.len
;
1356 if (len
> 1 && *name
== '0')
1359 unsigned c
= *name
++ - '0';
1362 if (n
>= (~0U-9)/10)
1373 static struct dentry
*proc_lookupfd(struct inode
* dir
, struct dentry
* dentry
, struct nameidata
*nd
)
1375 struct task_struct
*task
= proc_task(dir
);
1376 unsigned fd
= name_to_int(dentry
);
1378 struct files_struct
* files
;
1379 struct inode
*inode
;
1380 struct proc_inode
*ei
;
1384 if (!pid_alive(task
))
1387 inode
= proc_pid_make_inode(dir
->i_sb
, task
, PROC_TID_FD_DIR
+fd
);
1391 files
= get_files_struct(task
);
1394 inode
->i_mode
= S_IFLNK
;
1395 spin_lock(&files
->file_lock
);
1396 file
= fcheck_files(files
, fd
);
1399 if (file
->f_mode
& 1)
1400 inode
->i_mode
|= S_IRUSR
| S_IXUSR
;
1401 if (file
->f_mode
& 2)
1402 inode
->i_mode
|= S_IWUSR
| S_IXUSR
;
1403 spin_unlock(&files
->file_lock
);
1404 put_files_struct(files
);
1405 inode
->i_op
= &proc_pid_link_inode_operations
;
1407 ei
->op
.proc_get_link
= proc_fd_link
;
1408 dentry
->d_op
= &tid_fd_dentry_operations
;
1409 d_add(dentry
, inode
);
1413 spin_unlock(&files
->file_lock
);
1414 put_files_struct(files
);
1418 return ERR_PTR(-ENOENT
);
1421 static int proc_task_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
);
1422 static struct dentry
*proc_task_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
);
1424 static struct file_operations proc_fd_operations
= {
1425 .read
= generic_read_dir
,
1426 .readdir
= proc_readfd
,
1429 static struct file_operations proc_task_operations
= {
1430 .read
= generic_read_dir
,
1431 .readdir
= proc_task_readdir
,
1435 * proc directories can do almost nothing..
1437 static struct inode_operations proc_fd_inode_operations
= {
1438 .lookup
= proc_lookupfd
,
1439 .permission
= proc_permission
,
1442 static struct inode_operations proc_task_inode_operations
= {
1443 .lookup
= proc_task_lookup
,
1444 .permission
= proc_permission
,
1447 #ifdef CONFIG_SECURITY
1448 static ssize_t
proc_pid_attr_read(struct file
* file
, char __user
* buf
,
1449 size_t count
, loff_t
*ppos
)
1451 struct inode
* inode
= file
->f_dentry
->d_inode
;
1454 struct task_struct
*task
= proc_task(inode
);
1456 if (count
> PAGE_SIZE
)
1458 if (!(page
= __get_free_page(GFP_KERNEL
)))
1461 length
= security_getprocattr(task
,
1462 (char*)file
->f_dentry
->d_name
.name
,
1463 (void*)page
, count
);
1465 length
= simple_read_from_buffer(buf
, count
, ppos
, (char *)page
, length
);
1470 static ssize_t
proc_pid_attr_write(struct file
* file
, const char __user
* buf
,
1471 size_t count
, loff_t
*ppos
)
1473 struct inode
* inode
= file
->f_dentry
->d_inode
;
1476 struct task_struct
*task
= proc_task(inode
);
1478 if (count
> PAGE_SIZE
)
1481 /* No partial writes. */
1484 page
= (char*)__get_free_page(GFP_USER
);
1488 if (copy_from_user(page
, buf
, count
))
1491 length
= security_setprocattr(task
,
1492 (char*)file
->f_dentry
->d_name
.name
,
1493 (void*)page
, count
);
1495 free_page((unsigned long) page
);
1499 static struct file_operations proc_pid_attr_operations
= {
1500 .read
= proc_pid_attr_read
,
1501 .write
= proc_pid_attr_write
,
1504 static struct file_operations proc_tid_attr_operations
;
1505 static struct inode_operations proc_tid_attr_inode_operations
;
1506 static struct file_operations proc_tgid_attr_operations
;
1507 static struct inode_operations proc_tgid_attr_inode_operations
;
1510 static int get_tid_list(int index
, unsigned int *tids
, struct inode
*dir
);
1513 static struct dentry
*proc_pident_lookup(struct inode
*dir
,
1514 struct dentry
*dentry
,
1515 struct pid_entry
*ents
)
1517 struct inode
*inode
;
1519 struct task_struct
*task
= proc_task(dir
);
1520 struct pid_entry
*p
;
1521 struct proc_inode
*ei
;
1526 if (!pid_alive(task
))
1529 for (p
= ents
; p
->name
; p
++) {
1530 if (p
->len
!= dentry
->d_name
.len
)
1532 if (!memcmp(dentry
->d_name
.name
, p
->name
, p
->len
))
1539 inode
= proc_pid_make_inode(dir
->i_sb
, task
, p
->type
);
1544 inode
->i_mode
= p
->mode
;
1546 * Yes, it does not scale. And it should not. Don't add
1547 * new entries into /proc/<tgid>/ without very good reasons.
1550 case PROC_TGID_TASK
:
1551 inode
->i_nlink
= 2 + get_tid_list(2, NULL
, dir
);
1552 inode
->i_op
= &proc_task_inode_operations
;
1553 inode
->i_fop
= &proc_task_operations
;
1558 inode
->i_op
= &proc_fd_inode_operations
;
1559 inode
->i_fop
= &proc_fd_operations
;
1563 inode
->i_op
= &proc_pid_link_inode_operations
;
1564 ei
->op
.proc_get_link
= proc_exe_link
;
1568 inode
->i_op
= &proc_pid_link_inode_operations
;
1569 ei
->op
.proc_get_link
= proc_cwd_link
;
1572 case PROC_TGID_ROOT
:
1573 inode
->i_op
= &proc_pid_link_inode_operations
;
1574 ei
->op
.proc_get_link
= proc_root_link
;
1576 case PROC_TID_ENVIRON
:
1577 case PROC_TGID_ENVIRON
:
1578 inode
->i_fop
= &proc_info_file_operations
;
1579 ei
->op
.proc_read
= proc_pid_environ
;
1582 case PROC_TGID_AUXV
:
1583 inode
->i_fop
= &proc_info_file_operations
;
1584 ei
->op
.proc_read
= proc_pid_auxv
;
1586 case PROC_TID_STATUS
:
1587 case PROC_TGID_STATUS
:
1588 inode
->i_fop
= &proc_info_file_operations
;
1589 ei
->op
.proc_read
= proc_pid_status
;
1592 inode
->i_fop
= &proc_info_file_operations
;
1593 ei
->op
.proc_read
= proc_tid_stat
;
1595 case PROC_TGID_STAT
:
1596 inode
->i_fop
= &proc_info_file_operations
;
1597 ei
->op
.proc_read
= proc_tgid_stat
;
1599 case PROC_TID_CMDLINE
:
1600 case PROC_TGID_CMDLINE
:
1601 inode
->i_fop
= &proc_info_file_operations
;
1602 ei
->op
.proc_read
= proc_pid_cmdline
;
1604 case PROC_TID_STATM
:
1605 case PROC_TGID_STATM
:
1606 inode
->i_fop
= &proc_info_file_operations
;
1607 ei
->op
.proc_read
= proc_pid_statm
;
1610 case PROC_TGID_MAPS
:
1611 inode
->i_fop
= &proc_maps_operations
;
1614 case PROC_TID_NUMA_MAPS
:
1615 case PROC_TGID_NUMA_MAPS
:
1616 inode
->i_fop
= &proc_numa_maps_operations
;
1621 inode
->i_op
= &proc_mem_inode_operations
;
1622 inode
->i_fop
= &proc_mem_operations
;
1624 #ifdef CONFIG_SECCOMP
1625 case PROC_TID_SECCOMP
:
1626 case PROC_TGID_SECCOMP
:
1627 inode
->i_fop
= &proc_seccomp_operations
;
1629 #endif /* CONFIG_SECCOMP */
1630 case PROC_TID_MOUNTS
:
1631 case PROC_TGID_MOUNTS
:
1632 inode
->i_fop
= &proc_mounts_operations
;
1634 case PROC_TID_SMAPS
:
1635 case PROC_TGID_SMAPS
:
1636 inode
->i_fop
= &proc_smaps_operations
;
1638 #ifdef CONFIG_SECURITY
1641 inode
->i_op
= &proc_tid_attr_inode_operations
;
1642 inode
->i_fop
= &proc_tid_attr_operations
;
1644 case PROC_TGID_ATTR
:
1646 inode
->i_op
= &proc_tgid_attr_inode_operations
;
1647 inode
->i_fop
= &proc_tgid_attr_operations
;
1649 case PROC_TID_ATTR_CURRENT
:
1650 case PROC_TGID_ATTR_CURRENT
:
1651 case PROC_TID_ATTR_PREV
:
1652 case PROC_TGID_ATTR_PREV
:
1653 case PROC_TID_ATTR_EXEC
:
1654 case PROC_TGID_ATTR_EXEC
:
1655 case PROC_TID_ATTR_FSCREATE
:
1656 case PROC_TGID_ATTR_FSCREATE
:
1657 inode
->i_fop
= &proc_pid_attr_operations
;
1660 #ifdef CONFIG_KALLSYMS
1661 case PROC_TID_WCHAN
:
1662 case PROC_TGID_WCHAN
:
1663 inode
->i_fop
= &proc_info_file_operations
;
1664 ei
->op
.proc_read
= proc_pid_wchan
;
1667 #ifdef CONFIG_SCHEDSTATS
1668 case PROC_TID_SCHEDSTAT
:
1669 case PROC_TGID_SCHEDSTAT
:
1670 inode
->i_fop
= &proc_info_file_operations
;
1671 ei
->op
.proc_read
= proc_pid_schedstat
;
1674 #ifdef CONFIG_CPUSETS
1675 case PROC_TID_CPUSET
:
1676 case PROC_TGID_CPUSET
:
1677 inode
->i_fop
= &proc_cpuset_operations
;
1680 case PROC_TID_OOM_SCORE
:
1681 case PROC_TGID_OOM_SCORE
:
1682 inode
->i_fop
= &proc_info_file_operations
;
1683 ei
->op
.proc_read
= proc_oom_score
;
1685 case PROC_TID_OOM_ADJUST
:
1686 case PROC_TGID_OOM_ADJUST
:
1687 inode
->i_fop
= &proc_oom_adjust_operations
;
1689 #ifdef CONFIG_AUDITSYSCALL
1690 case PROC_TID_LOGINUID
:
1691 case PROC_TGID_LOGINUID
:
1692 inode
->i_fop
= &proc_loginuid_operations
;
1696 printk("procfs: impossible type (%d)",p
->type
);
1698 return ERR_PTR(-EINVAL
);
1700 dentry
->d_op
= &pid_dentry_operations
;
1701 d_add(dentry
, inode
);
1705 return ERR_PTR(error
);
1708 static struct dentry
*proc_tgid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
){
1709 return proc_pident_lookup(dir
, dentry
, tgid_base_stuff
);
1712 static struct dentry
*proc_tid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
){
1713 return proc_pident_lookup(dir
, dentry
, tid_base_stuff
);
1716 static struct file_operations proc_tgid_base_operations
= {
1717 .read
= generic_read_dir
,
1718 .readdir
= proc_tgid_base_readdir
,
1721 static struct file_operations proc_tid_base_operations
= {
1722 .read
= generic_read_dir
,
1723 .readdir
= proc_tid_base_readdir
,
1726 static struct inode_operations proc_tgid_base_inode_operations
= {
1727 .lookup
= proc_tgid_base_lookup
,
1730 static struct inode_operations proc_tid_base_inode_operations
= {
1731 .lookup
= proc_tid_base_lookup
,
1734 #ifdef CONFIG_SECURITY
1735 static int proc_tgid_attr_readdir(struct file
* filp
,
1736 void * dirent
, filldir_t filldir
)
1738 return proc_pident_readdir(filp
,dirent
,filldir
,
1739 tgid_attr_stuff
,ARRAY_SIZE(tgid_attr_stuff
));
1742 static int proc_tid_attr_readdir(struct file
* filp
,
1743 void * dirent
, filldir_t filldir
)
1745 return proc_pident_readdir(filp
,dirent
,filldir
,
1746 tid_attr_stuff
,ARRAY_SIZE(tid_attr_stuff
));
1749 static struct file_operations proc_tgid_attr_operations
= {
1750 .read
= generic_read_dir
,
1751 .readdir
= proc_tgid_attr_readdir
,
1754 static struct file_operations proc_tid_attr_operations
= {
1755 .read
= generic_read_dir
,
1756 .readdir
= proc_tid_attr_readdir
,
1759 static struct dentry
*proc_tgid_attr_lookup(struct inode
*dir
,
1760 struct dentry
*dentry
, struct nameidata
*nd
)
1762 return proc_pident_lookup(dir
, dentry
, tgid_attr_stuff
);
1765 static struct dentry
*proc_tid_attr_lookup(struct inode
*dir
,
1766 struct dentry
*dentry
, struct nameidata
*nd
)
1768 return proc_pident_lookup(dir
, dentry
, tid_attr_stuff
);
1771 static struct inode_operations proc_tgid_attr_inode_operations
= {
1772 .lookup
= proc_tgid_attr_lookup
,
1775 static struct inode_operations proc_tid_attr_inode_operations
= {
1776 .lookup
= proc_tid_attr_lookup
,
1783 static int proc_self_readlink(struct dentry
*dentry
, char __user
*buffer
,
1787 sprintf(tmp
, "%d", current
->tgid
);
1788 return vfs_readlink(dentry
,buffer
,buflen
,tmp
);
1791 static void *proc_self_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
1794 sprintf(tmp
, "%d", current
->tgid
);
1795 return ERR_PTR(vfs_follow_link(nd
,tmp
));
1798 static struct inode_operations proc_self_inode_operations
= {
1799 .readlink
= proc_self_readlink
,
1800 .follow_link
= proc_self_follow_link
,
1804 * proc_pid_unhash - Unhash /proc/@pid entry from the dcache.
1805 * @p: task that should be flushed.
1807 * Drops the /proc/@pid dcache entry from the hash chains.
1809 * Dropping /proc/@pid entries and detach_pid must be synchroneous,
1810 * otherwise e.g. /proc/@pid/exe might point to the wrong executable,
1811 * if the pid value is immediately reused. This is enforced by
1812 * - caller must acquire spin_lock(p->proc_lock)
1813 * - must be called before detach_pid()
1814 * - proc_pid_lookup acquires proc_lock, and checks that
1815 * the target is not dead by looking at the attach count
1819 struct dentry
*proc_pid_unhash(struct task_struct
*p
)
1821 struct dentry
*proc_dentry
;
1823 proc_dentry
= p
->proc_dentry
;
1824 if (proc_dentry
!= NULL
) {
1826 spin_lock(&dcache_lock
);
1827 spin_lock(&proc_dentry
->d_lock
);
1828 if (!d_unhashed(proc_dentry
)) {
1829 dget_locked(proc_dentry
);
1830 __d_drop(proc_dentry
);
1831 spin_unlock(&proc_dentry
->d_lock
);
1833 spin_unlock(&proc_dentry
->d_lock
);
1836 spin_unlock(&dcache_lock
);
1842 * proc_pid_flush - recover memory used by stale /proc/@pid/x entries
1843 * @proc_dentry: directoy to prune.
1845 * Shrink the /proc directory that was used by the just killed thread.
1848 void proc_pid_flush(struct dentry
*proc_dentry
)
1851 if(proc_dentry
!= NULL
) {
1852 shrink_dcache_parent(proc_dentry
);
1858 struct dentry
*proc_pid_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
)
1860 struct task_struct
*task
;
1861 struct inode
*inode
;
1862 struct proc_inode
*ei
;
1866 if (dentry
->d_name
.len
== 4 && !memcmp(dentry
->d_name
.name
,"self",4)) {
1867 inode
= new_inode(dir
->i_sb
);
1869 return ERR_PTR(-ENOMEM
);
1871 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1872 inode
->i_ino
= fake_ino(0, PROC_TGID_INO
);
1874 inode
->i_mode
= S_IFLNK
|S_IRWXUGO
;
1875 inode
->i_uid
= inode
->i_gid
= 0;
1877 inode
->i_op
= &proc_self_inode_operations
;
1878 d_add(dentry
, inode
);
1881 tgid
= name_to_int(dentry
);
1885 read_lock(&tasklist_lock
);
1886 task
= find_task_by_pid(tgid
);
1888 get_task_struct(task
);
1889 read_unlock(&tasklist_lock
);
1893 inode
= proc_pid_make_inode(dir
->i_sb
, task
, PROC_TGID_INO
);
1897 put_task_struct(task
);
1900 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
1901 inode
->i_op
= &proc_tgid_base_inode_operations
;
1902 inode
->i_fop
= &proc_tgid_base_operations
;
1903 inode
->i_flags
|=S_IMMUTABLE
;
1904 #ifdef CONFIG_SECURITY
1910 dentry
->d_op
= &pid_base_dentry_operations
;
1913 d_add(dentry
, inode
);
1914 spin_lock(&task
->proc_lock
);
1915 task
->proc_dentry
= dentry
;
1916 if (!pid_alive(task
)) {
1917 dentry
= proc_pid_unhash(task
);
1920 spin_unlock(&task
->proc_lock
);
1922 put_task_struct(task
);
1924 proc_pid_flush(dentry
);
1929 return ERR_PTR(-ENOENT
);
1933 static struct dentry
*proc_task_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
)
1935 struct task_struct
*task
;
1936 struct task_struct
*leader
= proc_task(dir
);
1937 struct inode
*inode
;
1940 tid
= name_to_int(dentry
);
1944 read_lock(&tasklist_lock
);
1945 task
= find_task_by_pid(tid
);
1947 get_task_struct(task
);
1948 read_unlock(&tasklist_lock
);
1951 if (leader
->tgid
!= task
->tgid
)
1954 inode
= proc_pid_make_inode(dir
->i_sb
, task
, PROC_TID_INO
);
1959 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
1960 inode
->i_op
= &proc_tid_base_inode_operations
;
1961 inode
->i_fop
= &proc_tid_base_operations
;
1962 inode
->i_flags
|=S_IMMUTABLE
;
1963 #ifdef CONFIG_SECURITY
1969 dentry
->d_op
= &pid_base_dentry_operations
;
1971 d_add(dentry
, inode
);
1973 put_task_struct(task
);
1976 put_task_struct(task
);
1978 return ERR_PTR(-ENOENT
);
1981 #define PROC_NUMBUF 10
1982 #define PROC_MAXPIDS 20
1985 * Get a few tgid's to return for filldir - we need to hold the
1986 * tasklist lock while doing this, and we must release it before
1987 * we actually do the filldir itself, so we use a temp buffer..
1989 static int get_tgid_list(int index
, unsigned long version
, unsigned int *tgids
)
1991 struct task_struct
*p
;
1995 read_lock(&tasklist_lock
);
1998 p
= find_task_by_pid(version
);
1999 if (p
&& !thread_group_leader(p
))
2006 p
= next_task(&init_task
);
2008 for ( ; p
!= &init_task
; p
= next_task(p
)) {
2014 tgids
[nr_tgids
] = tgid
;
2016 if (nr_tgids
>= PROC_MAXPIDS
)
2019 read_unlock(&tasklist_lock
);
2024 * Get a few tid's to return for filldir - we need to hold the
2025 * tasklist lock while doing this, and we must release it before
2026 * we actually do the filldir itself, so we use a temp buffer..
2028 static int get_tid_list(int index
, unsigned int *tids
, struct inode
*dir
)
2030 struct task_struct
*leader_task
= proc_task(dir
);
2031 struct task_struct
*task
= leader_task
;
2035 read_lock(&tasklist_lock
);
2037 * The starting point task (leader_task) might be an already
2038 * unlinked task, which cannot be used to access the task-list
2039 * via next_thread().
2041 if (pid_alive(task
)) do {
2042 int tid
= task
->pid
;
2047 tids
[nr_tids
] = tid
;
2049 if (nr_tids
>= PROC_MAXPIDS
)
2051 } while ((task
= next_thread(task
)) != leader_task
);
2052 read_unlock(&tasklist_lock
);
2056 /* for the /proc/ directory itself, after non-process stuff has been done */
2057 int proc_pid_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
2059 unsigned int tgid_array
[PROC_MAXPIDS
];
2060 char buf
[PROC_NUMBUF
];
2061 unsigned int nr
= filp
->f_pos
- FIRST_PROCESS_ENTRY
;
2062 unsigned int nr_tgids
, i
;
2066 ino_t ino
= fake_ino(0,PROC_TGID_INO
);
2067 if (filldir(dirent
, "self", 4, filp
->f_pos
, ino
, DT_LNK
) < 0)
2073 /* f_version caches the tgid value that the last readdir call couldn't
2074 * return. lseek aka telldir automagically resets f_version to 0.
2076 next_tgid
= filp
->f_version
;
2077 filp
->f_version
= 0;
2079 nr_tgids
= get_tgid_list(nr
, next_tgid
, tgid_array
);
2081 /* no more entries ! */
2086 /* do not use the last found pid, reserve it for next_tgid */
2087 if (nr_tgids
== PROC_MAXPIDS
) {
2089 next_tgid
= tgid_array
[nr_tgids
];
2092 for (i
=0;i
<nr_tgids
;i
++) {
2093 int tgid
= tgid_array
[i
];
2094 ino_t ino
= fake_ino(tgid
,PROC_TGID_INO
);
2095 unsigned long j
= PROC_NUMBUF
;
2098 buf
[--j
] = '0' + (tgid
% 10);
2099 while ((tgid
/= 10) != 0);
2101 if (filldir(dirent
, buf
+j
, PROC_NUMBUF
-j
, filp
->f_pos
, ino
, DT_DIR
) < 0) {
2102 /* returning this tgid failed, save it as the first
2103 * pid for the next readir call */
2104 filp
->f_version
= tgid_array
[i
];
2115 /* for the /proc/TGID/task/ directories */
2116 static int proc_task_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
2118 unsigned int tid_array
[PROC_MAXPIDS
];
2119 char buf
[PROC_NUMBUF
];
2120 unsigned int nr_tids
, i
;
2121 struct dentry
*dentry
= filp
->f_dentry
;
2122 struct inode
*inode
= dentry
->d_inode
;
2123 int retval
= -ENOENT
;
2125 unsigned long pos
= filp
->f_pos
; /* avoiding "long long" filp->f_pos */
2127 if (!pid_alive(proc_task(inode
)))
2134 if (filldir(dirent
, ".", 1, pos
, ino
, DT_DIR
) < 0)
2139 ino
= parent_ino(dentry
);
2140 if (filldir(dirent
, "..", 2, pos
, ino
, DT_DIR
) < 0)
2146 nr_tids
= get_tid_list(pos
, tid_array
, inode
);
2147 inode
->i_nlink
= pos
+ nr_tids
;
2149 for (i
= 0; i
< nr_tids
; i
++) {
2150 unsigned long j
= PROC_NUMBUF
;
2151 int tid
= tid_array
[i
];
2153 ino
= fake_ino(tid
,PROC_TID_INO
);
2156 buf
[--j
] = '0' + (tid
% 10);
2157 while ((tid
/= 10) != 0);
2159 if (filldir(dirent
, buf
+j
, PROC_NUMBUF
-j
, pos
, ino
, DT_DIR
) < 0)