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/task_io_accounting_ops.h>
57 #include <linux/init.h>
58 #include <linux/capability.h>
59 #include <linux/file.h>
60 #include <linux/fdtable.h>
61 #include <linux/string.h>
62 #include <linux/seq_file.h>
63 #include <linux/namei.h>
64 #include <linux/mnt_namespace.h>
66 #include <linux/rcupdate.h>
67 #include <linux/kallsyms.h>
68 #include <linux/stacktrace.h>
69 #include <linux/resource.h>
70 #include <linux/module.h>
71 #include <linux/mount.h>
72 #include <linux/security.h>
73 #include <linux/ptrace.h>
74 #include <linux/tracehook.h>
75 #include <linux/cgroup.h>
76 #include <linux/cpuset.h>
77 #include <linux/audit.h>
78 #include <linux/poll.h>
79 #include <linux/nsproxy.h>
80 #include <linux/oom.h>
81 #include <linux/elf.h>
82 #include <linux/pid_namespace.h>
86 * Implementing inode permission operations in /proc is almost
87 * certainly an error. Permission checks need to happen during
88 * each system call not at open time. The reason is that most of
89 * what we wish to check for permissions in /proc varies at runtime.
91 * The classic example of a problem is opening file descriptors
92 * in /proc for a task before it execs a suid executable.
99 const struct inode_operations
*iop
;
100 const struct file_operations
*fop
;
104 #define NOD(NAME, MODE, IOP, FOP, OP) { \
106 .len = sizeof(NAME) - 1, \
113 #define DIR(NAME, MODE, iops, fops) \
114 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
115 #define LNK(NAME, get_link) \
116 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
117 &proc_pid_link_inode_operations, NULL, \
118 { .proc_get_link = get_link } )
119 #define REG(NAME, MODE, fops) \
120 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
121 #define INF(NAME, MODE, read) \
122 NOD(NAME, (S_IFREG|(MODE)), \
123 NULL, &proc_info_file_operations, \
124 { .proc_read = read } )
125 #define ONE(NAME, MODE, show) \
126 NOD(NAME, (S_IFREG|(MODE)), \
127 NULL, &proc_single_file_operations, \
128 { .proc_show = show } )
131 * Count the number of hardlinks for the pid_entry table, excluding the .
134 static unsigned int pid_entry_count_dirs(const struct pid_entry
*entries
,
141 for (i
= 0; i
< n
; ++i
) {
142 if (S_ISDIR(entries
[i
].mode
))
149 static int get_fs_path(struct task_struct
*task
, struct path
*path
, bool root
)
151 struct fs_struct
*fs
;
152 int result
= -ENOENT
;
157 read_lock(&fs
->lock
);
158 *path
= root
? fs
->root
: fs
->pwd
;
160 read_unlock(&fs
->lock
);
167 static int get_nr_threads(struct task_struct
*tsk
)
172 if (lock_task_sighand(tsk
, &flags
)) {
173 count
= atomic_read(&tsk
->signal
->count
);
174 unlock_task_sighand(tsk
, &flags
);
179 static int proc_cwd_link(struct inode
*inode
, struct path
*path
)
181 struct task_struct
*task
= get_proc_task(inode
);
182 int result
= -ENOENT
;
185 result
= get_fs_path(task
, path
, 0);
186 put_task_struct(task
);
191 static int proc_root_link(struct inode
*inode
, struct path
*path
)
193 struct task_struct
*task
= get_proc_task(inode
);
194 int result
= -ENOENT
;
197 result
= get_fs_path(task
, path
, 1);
198 put_task_struct(task
);
204 * Return zero if current may access user memory in @task, -error if not.
206 static int check_mem_permission(struct task_struct
*task
)
209 * A task can always look at itself, in case it chooses
210 * to use system calls instead of load instructions.
216 * If current is actively ptrace'ing, and would also be
217 * permitted to freshly attach with ptrace now, permit it.
219 if (task_is_stopped_or_traced(task
)) {
222 match
= (tracehook_tracer_task(task
) == current
);
224 if (match
&& ptrace_may_access(task
, PTRACE_MODE_ATTACH
))
229 * Noone else is allowed.
234 struct mm_struct
*mm_for_maps(struct task_struct
*task
)
236 struct mm_struct
*mm
= get_task_mm(task
);
239 down_read(&mm
->mmap_sem
);
243 if (task
->mm
!= current
->mm
&&
244 __ptrace_may_access(task
, PTRACE_MODE_READ
) < 0)
250 up_read(&mm
->mmap_sem
);
255 static int proc_pid_cmdline(struct task_struct
*task
, char * buffer
)
259 struct mm_struct
*mm
= get_task_mm(task
);
263 goto out_mm
; /* Shh! No looking before we're done */
265 len
= mm
->arg_end
- mm
->arg_start
;
270 res
= access_process_vm(task
, mm
->arg_start
, buffer
, len
, 0);
272 // If the nul at the end of args has been overwritten, then
273 // assume application is using setproctitle(3).
274 if (res
> 0 && buffer
[res
-1] != '\0' && len
< PAGE_SIZE
) {
275 len
= strnlen(buffer
, res
);
279 len
= mm
->env_end
- mm
->env_start
;
280 if (len
> PAGE_SIZE
- res
)
281 len
= PAGE_SIZE
- res
;
282 res
+= access_process_vm(task
, mm
->env_start
, buffer
+res
, len
, 0);
283 res
= strnlen(buffer
, res
);
292 static int proc_pid_auxv(struct task_struct
*task
, char *buffer
)
295 struct mm_struct
*mm
= get_task_mm(task
);
297 unsigned int nwords
= 0;
300 } while (mm
->saved_auxv
[nwords
- 2] != 0); /* AT_NULL */
301 res
= nwords
* sizeof(mm
->saved_auxv
[0]);
304 memcpy(buffer
, mm
->saved_auxv
, res
);
311 #ifdef CONFIG_KALLSYMS
313 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
314 * Returns the resolved symbol. If that fails, simply return the address.
316 static int proc_pid_wchan(struct task_struct
*task
, char *buffer
)
319 char symname
[KSYM_NAME_LEN
];
321 wchan
= get_wchan(task
);
323 if (lookup_symbol_name(wchan
, symname
) < 0)
324 if (!ptrace_may_access(task
, PTRACE_MODE_READ
))
327 return sprintf(buffer
, "%lu", wchan
);
329 return sprintf(buffer
, "%s", symname
);
331 #endif /* CONFIG_KALLSYMS */
333 #ifdef CONFIG_STACKTRACE
335 #define MAX_STACK_TRACE_DEPTH 64
337 static int proc_pid_stack(struct seq_file
*m
, struct pid_namespace
*ns
,
338 struct pid
*pid
, struct task_struct
*task
)
340 struct stack_trace trace
;
341 unsigned long *entries
;
344 entries
= kmalloc(MAX_STACK_TRACE_DEPTH
* sizeof(*entries
), GFP_KERNEL
);
348 trace
.nr_entries
= 0;
349 trace
.max_entries
= MAX_STACK_TRACE_DEPTH
;
350 trace
.entries
= entries
;
352 save_stack_trace_tsk(task
, &trace
);
354 for (i
= 0; i
< trace
.nr_entries
; i
++) {
355 seq_printf(m
, "[<%p>] %pS\n",
356 (void *)entries
[i
], (void *)entries
[i
]);
364 #ifdef CONFIG_SCHEDSTATS
366 * Provides /proc/PID/schedstat
368 static int proc_pid_schedstat(struct task_struct
*task
, char *buffer
)
370 return sprintf(buffer
, "%llu %llu %lu\n",
371 (unsigned long long)task
->se
.sum_exec_runtime
,
372 (unsigned long long)task
->sched_info
.run_delay
,
373 task
->sched_info
.pcount
);
377 #ifdef CONFIG_LATENCYTOP
378 static int lstats_show_proc(struct seq_file
*m
, void *v
)
381 struct inode
*inode
= m
->private;
382 struct task_struct
*task
= get_proc_task(inode
);
386 seq_puts(m
, "Latency Top version : v0.1\n");
387 for (i
= 0; i
< 32; i
++) {
388 if (task
->latency_record
[i
].backtrace
[0]) {
390 seq_printf(m
, "%i %li %li ",
391 task
->latency_record
[i
].count
,
392 task
->latency_record
[i
].time
,
393 task
->latency_record
[i
].max
);
394 for (q
= 0; q
< LT_BACKTRACEDEPTH
; q
++) {
395 char sym
[KSYM_SYMBOL_LEN
];
397 if (!task
->latency_record
[i
].backtrace
[q
])
399 if (task
->latency_record
[i
].backtrace
[q
] == ULONG_MAX
)
401 sprint_symbol(sym
, task
->latency_record
[i
].backtrace
[q
]);
402 c
= strchr(sym
, '+');
405 seq_printf(m
, "%s ", sym
);
411 put_task_struct(task
);
415 static int lstats_open(struct inode
*inode
, struct file
*file
)
417 return single_open(file
, lstats_show_proc
, inode
);
420 static ssize_t
lstats_write(struct file
*file
, const char __user
*buf
,
421 size_t count
, loff_t
*offs
)
423 struct task_struct
*task
= get_proc_task(file
->f_dentry
->d_inode
);
427 clear_all_latency_tracing(task
);
428 put_task_struct(task
);
433 static const struct file_operations proc_lstats_operations
= {
436 .write
= lstats_write
,
438 .release
= single_release
,
443 /* The badness from the OOM killer */
444 unsigned long badness(struct task_struct
*p
, unsigned long uptime
);
445 static int proc_oom_score(struct task_struct
*task
, char *buffer
)
447 unsigned long points
;
448 struct timespec uptime
;
450 do_posix_clock_monotonic_gettime(&uptime
);
451 read_lock(&tasklist_lock
);
452 points
= badness(task
, uptime
.tv_sec
);
453 read_unlock(&tasklist_lock
);
454 return sprintf(buffer
, "%lu\n", points
);
462 static const struct limit_names lnames
[RLIM_NLIMITS
] = {
463 [RLIMIT_CPU
] = {"Max cpu time", "ms"},
464 [RLIMIT_FSIZE
] = {"Max file size", "bytes"},
465 [RLIMIT_DATA
] = {"Max data size", "bytes"},
466 [RLIMIT_STACK
] = {"Max stack size", "bytes"},
467 [RLIMIT_CORE
] = {"Max core file size", "bytes"},
468 [RLIMIT_RSS
] = {"Max resident set", "bytes"},
469 [RLIMIT_NPROC
] = {"Max processes", "processes"},
470 [RLIMIT_NOFILE
] = {"Max open files", "files"},
471 [RLIMIT_MEMLOCK
] = {"Max locked memory", "bytes"},
472 [RLIMIT_AS
] = {"Max address space", "bytes"},
473 [RLIMIT_LOCKS
] = {"Max file locks", "locks"},
474 [RLIMIT_SIGPENDING
] = {"Max pending signals", "signals"},
475 [RLIMIT_MSGQUEUE
] = {"Max msgqueue size", "bytes"},
476 [RLIMIT_NICE
] = {"Max nice priority", NULL
},
477 [RLIMIT_RTPRIO
] = {"Max realtime priority", NULL
},
478 [RLIMIT_RTTIME
] = {"Max realtime timeout", "us"},
481 /* Display limits for a process */
482 static int proc_pid_limits(struct task_struct
*task
, char *buffer
)
487 char *bufptr
= buffer
;
489 struct rlimit rlim
[RLIM_NLIMITS
];
491 if (!lock_task_sighand(task
, &flags
))
493 memcpy(rlim
, task
->signal
->rlim
, sizeof(struct rlimit
) * RLIM_NLIMITS
);
494 unlock_task_sighand(task
, &flags
);
497 * print the file header
499 count
+= sprintf(&bufptr
[count
], "%-25s %-20s %-20s %-10s\n",
500 "Limit", "Soft Limit", "Hard Limit", "Units");
502 for (i
= 0; i
< RLIM_NLIMITS
; i
++) {
503 if (rlim
[i
].rlim_cur
== RLIM_INFINITY
)
504 count
+= sprintf(&bufptr
[count
], "%-25s %-20s ",
505 lnames
[i
].name
, "unlimited");
507 count
+= sprintf(&bufptr
[count
], "%-25s %-20lu ",
508 lnames
[i
].name
, rlim
[i
].rlim_cur
);
510 if (rlim
[i
].rlim_max
== RLIM_INFINITY
)
511 count
+= sprintf(&bufptr
[count
], "%-20s ", "unlimited");
513 count
+= sprintf(&bufptr
[count
], "%-20lu ",
517 count
+= sprintf(&bufptr
[count
], "%-10s\n",
520 count
+= sprintf(&bufptr
[count
], "\n");
526 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
527 static int proc_pid_syscall(struct task_struct
*task
, char *buffer
)
530 unsigned long args
[6], sp
, pc
;
532 if (task_current_syscall(task
, &nr
, args
, 6, &sp
, &pc
))
533 return sprintf(buffer
, "running\n");
536 return sprintf(buffer
, "%ld 0x%lx 0x%lx\n", nr
, sp
, pc
);
538 return sprintf(buffer
,
539 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
541 args
[0], args
[1], args
[2], args
[3], args
[4], args
[5],
544 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
546 /************************************************************************/
547 /* Here the fs part begins */
548 /************************************************************************/
550 /* permission checks */
551 static int proc_fd_access_allowed(struct inode
*inode
)
553 struct task_struct
*task
;
555 /* Allow access to a task's file descriptors if it is us or we
556 * may use ptrace attach to the process and find out that
559 task
= get_proc_task(inode
);
561 allowed
= ptrace_may_access(task
, PTRACE_MODE_READ
);
562 put_task_struct(task
);
567 static int proc_setattr(struct dentry
*dentry
, struct iattr
*attr
)
570 struct inode
*inode
= dentry
->d_inode
;
572 if (attr
->ia_valid
& ATTR_MODE
)
575 error
= inode_change_ok(inode
, attr
);
577 error
= inode_setattr(inode
, attr
);
581 static const struct inode_operations proc_def_inode_operations
= {
582 .setattr
= proc_setattr
,
585 static int mounts_open_common(struct inode
*inode
, struct file
*file
,
586 const struct seq_operations
*op
)
588 struct task_struct
*task
= get_proc_task(inode
);
590 struct mnt_namespace
*ns
= NULL
;
592 struct proc_mounts
*p
;
597 nsp
= task_nsproxy(task
);
604 if (ns
&& get_fs_path(task
, &root
, 1) == 0)
606 put_task_struct(task
);
615 p
= kmalloc(sizeof(struct proc_mounts
), GFP_KERNEL
);
619 file
->private_data
= &p
->m
;
620 ret
= seq_open(file
, op
);
627 p
->event
= ns
->event
;
641 static int mounts_release(struct inode
*inode
, struct file
*file
)
643 struct proc_mounts
*p
= file
->private_data
;
646 return seq_release(inode
, file
);
649 static unsigned mounts_poll(struct file
*file
, poll_table
*wait
)
651 struct proc_mounts
*p
= file
->private_data
;
652 struct mnt_namespace
*ns
= p
->ns
;
655 poll_wait(file
, &ns
->poll
, wait
);
657 spin_lock(&vfsmount_lock
);
658 if (p
->event
!= ns
->event
) {
659 p
->event
= ns
->event
;
662 spin_unlock(&vfsmount_lock
);
667 static int mounts_open(struct inode
*inode
, struct file
*file
)
669 return mounts_open_common(inode
, file
, &mounts_op
);
672 static const struct file_operations proc_mounts_operations
= {
676 .release
= mounts_release
,
680 static int mountinfo_open(struct inode
*inode
, struct file
*file
)
682 return mounts_open_common(inode
, file
, &mountinfo_op
);
685 static const struct file_operations proc_mountinfo_operations
= {
686 .open
= mountinfo_open
,
689 .release
= mounts_release
,
693 static int mountstats_open(struct inode
*inode
, struct file
*file
)
695 return mounts_open_common(inode
, file
, &mountstats_op
);
698 static const struct file_operations proc_mountstats_operations
= {
699 .open
= mountstats_open
,
702 .release
= mounts_release
,
705 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
707 static ssize_t
proc_info_read(struct file
* file
, char __user
* buf
,
708 size_t count
, loff_t
*ppos
)
710 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
713 struct task_struct
*task
= get_proc_task(inode
);
719 if (count
> PROC_BLOCK_SIZE
)
720 count
= PROC_BLOCK_SIZE
;
723 if (!(page
= __get_free_page(GFP_TEMPORARY
)))
726 length
= PROC_I(inode
)->op
.proc_read(task
, (char*)page
);
729 length
= simple_read_from_buffer(buf
, count
, ppos
, (char *)page
, length
);
732 put_task_struct(task
);
737 static const struct file_operations proc_info_file_operations
= {
738 .read
= proc_info_read
,
741 static int proc_single_show(struct seq_file
*m
, void *v
)
743 struct inode
*inode
= m
->private;
744 struct pid_namespace
*ns
;
746 struct task_struct
*task
;
749 ns
= inode
->i_sb
->s_fs_info
;
750 pid
= proc_pid(inode
);
751 task
= get_pid_task(pid
, PIDTYPE_PID
);
755 ret
= PROC_I(inode
)->op
.proc_show(m
, ns
, pid
, task
);
757 put_task_struct(task
);
761 static int proc_single_open(struct inode
*inode
, struct file
*filp
)
764 ret
= single_open(filp
, proc_single_show
, NULL
);
766 struct seq_file
*m
= filp
->private_data
;
773 static const struct file_operations proc_single_file_operations
= {
774 .open
= proc_single_open
,
777 .release
= single_release
,
780 static int mem_open(struct inode
* inode
, struct file
* file
)
782 file
->private_data
= (void*)((long)current
->self_exec_id
);
786 static ssize_t
mem_read(struct file
* file
, char __user
* buf
,
787 size_t count
, loff_t
*ppos
)
789 struct task_struct
*task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
791 unsigned long src
= *ppos
;
793 struct mm_struct
*mm
;
798 if (check_mem_permission(task
))
802 page
= (char *)__get_free_page(GFP_TEMPORARY
);
808 mm
= get_task_mm(task
);
814 if (file
->private_data
!= (void*)((long)current
->self_exec_id
))
820 int this_len
, retval
;
822 this_len
= (count
> PAGE_SIZE
) ? PAGE_SIZE
: count
;
823 retval
= access_process_vm(task
, src
, page
, this_len
, 0);
824 if (!retval
|| check_mem_permission(task
)) {
830 if (copy_to_user(buf
, page
, retval
)) {
845 free_page((unsigned long) page
);
847 put_task_struct(task
);
852 #define mem_write NULL
855 /* This is a security hazard */
856 static ssize_t
mem_write(struct file
* file
, const char __user
*buf
,
857 size_t count
, loff_t
*ppos
)
861 struct task_struct
*task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
862 unsigned long dst
= *ppos
;
868 if (check_mem_permission(task
))
872 page
= (char *)__get_free_page(GFP_TEMPORARY
);
878 int this_len
, retval
;
880 this_len
= (count
> PAGE_SIZE
) ? PAGE_SIZE
: count
;
881 if (copy_from_user(page
, buf
, this_len
)) {
885 retval
= access_process_vm(task
, dst
, page
, this_len
, 1);
897 free_page((unsigned long) page
);
899 put_task_struct(task
);
905 loff_t
mem_lseek(struct file
*file
, loff_t offset
, int orig
)
909 file
->f_pos
= offset
;
912 file
->f_pos
+= offset
;
917 force_successful_syscall_return();
921 static const struct file_operations proc_mem_operations
= {
928 static ssize_t
environ_read(struct file
*file
, char __user
*buf
,
929 size_t count
, loff_t
*ppos
)
931 struct task_struct
*task
= get_proc_task(file
->f_dentry
->d_inode
);
933 unsigned long src
= *ppos
;
935 struct mm_struct
*mm
;
940 if (!ptrace_may_access(task
, PTRACE_MODE_READ
))
944 page
= (char *)__get_free_page(GFP_TEMPORARY
);
950 mm
= get_task_mm(task
);
955 int this_len
, retval
, max_len
;
957 this_len
= mm
->env_end
- (mm
->env_start
+ src
);
962 max_len
= (count
> PAGE_SIZE
) ? PAGE_SIZE
: count
;
963 this_len
= (this_len
> max_len
) ? max_len
: this_len
;
965 retval
= access_process_vm(task
, (mm
->env_start
+ src
),
973 if (copy_to_user(buf
, page
, retval
)) {
987 free_page((unsigned long) page
);
989 put_task_struct(task
);
994 static const struct file_operations proc_environ_operations
= {
995 .read
= environ_read
,
998 static ssize_t
oom_adjust_read(struct file
*file
, char __user
*buf
,
999 size_t count
, loff_t
*ppos
)
1001 struct task_struct
*task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
1002 char buffer
[PROC_NUMBUF
];
1008 oom_adjust
= task
->oomkilladj
;
1009 put_task_struct(task
);
1011 len
= snprintf(buffer
, sizeof(buffer
), "%i\n", oom_adjust
);
1013 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
1016 static ssize_t
oom_adjust_write(struct file
*file
, const char __user
*buf
,
1017 size_t count
, loff_t
*ppos
)
1019 struct task_struct
*task
;
1020 char buffer
[PROC_NUMBUF
], *end
;
1023 memset(buffer
, 0, sizeof(buffer
));
1024 if (count
> sizeof(buffer
) - 1)
1025 count
= sizeof(buffer
) - 1;
1026 if (copy_from_user(buffer
, buf
, count
))
1028 oom_adjust
= simple_strtol(buffer
, &end
, 0);
1029 if ((oom_adjust
< OOM_ADJUST_MIN
|| oom_adjust
> OOM_ADJUST_MAX
) &&
1030 oom_adjust
!= OOM_DISABLE
)
1034 task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
1037 if (oom_adjust
< task
->oomkilladj
&& !capable(CAP_SYS_RESOURCE
)) {
1038 put_task_struct(task
);
1041 task
->oomkilladj
= oom_adjust
;
1042 put_task_struct(task
);
1043 if (end
- buffer
== 0)
1045 return end
- buffer
;
1048 static const struct file_operations proc_oom_adjust_operations
= {
1049 .read
= oom_adjust_read
,
1050 .write
= oom_adjust_write
,
1053 #ifdef CONFIG_AUDITSYSCALL
1054 #define TMPBUFLEN 21
1055 static ssize_t
proc_loginuid_read(struct file
* file
, char __user
* buf
,
1056 size_t count
, loff_t
*ppos
)
1058 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
1059 struct task_struct
*task
= get_proc_task(inode
);
1061 char tmpbuf
[TMPBUFLEN
];
1065 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
1066 audit_get_loginuid(task
));
1067 put_task_struct(task
);
1068 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1071 static ssize_t
proc_loginuid_write(struct file
* file
, const char __user
* buf
,
1072 size_t count
, loff_t
*ppos
)
1074 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
1079 if (!capable(CAP_AUDIT_CONTROL
))
1082 if (current
!= pid_task(proc_pid(inode
), PIDTYPE_PID
))
1085 if (count
>= PAGE_SIZE
)
1086 count
= PAGE_SIZE
- 1;
1089 /* No partial writes. */
1092 page
= (char*)__get_free_page(GFP_TEMPORARY
);
1096 if (copy_from_user(page
, buf
, count
))
1100 loginuid
= simple_strtoul(page
, &tmp
, 10);
1106 length
= audit_set_loginuid(current
, loginuid
);
1107 if (likely(length
== 0))
1111 free_page((unsigned long) page
);
1115 static const struct file_operations proc_loginuid_operations
= {
1116 .read
= proc_loginuid_read
,
1117 .write
= proc_loginuid_write
,
1120 static ssize_t
proc_sessionid_read(struct file
* file
, char __user
* buf
,
1121 size_t count
, loff_t
*ppos
)
1123 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
1124 struct task_struct
*task
= get_proc_task(inode
);
1126 char tmpbuf
[TMPBUFLEN
];
1130 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
1131 audit_get_sessionid(task
));
1132 put_task_struct(task
);
1133 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1136 static const struct file_operations proc_sessionid_operations
= {
1137 .read
= proc_sessionid_read
,
1141 #ifdef CONFIG_FAULT_INJECTION
1142 static ssize_t
proc_fault_inject_read(struct file
* file
, char __user
* buf
,
1143 size_t count
, loff_t
*ppos
)
1145 struct task_struct
*task
= get_proc_task(file
->f_dentry
->d_inode
);
1146 char buffer
[PROC_NUMBUF
];
1152 make_it_fail
= task
->make_it_fail
;
1153 put_task_struct(task
);
1155 len
= snprintf(buffer
, sizeof(buffer
), "%i\n", make_it_fail
);
1157 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
1160 static ssize_t
proc_fault_inject_write(struct file
* file
,
1161 const char __user
* buf
, size_t count
, loff_t
*ppos
)
1163 struct task_struct
*task
;
1164 char buffer
[PROC_NUMBUF
], *end
;
1167 if (!capable(CAP_SYS_RESOURCE
))
1169 memset(buffer
, 0, sizeof(buffer
));
1170 if (count
> sizeof(buffer
) - 1)
1171 count
= sizeof(buffer
) - 1;
1172 if (copy_from_user(buffer
, buf
, count
))
1174 make_it_fail
= simple_strtol(buffer
, &end
, 0);
1177 task
= get_proc_task(file
->f_dentry
->d_inode
);
1180 task
->make_it_fail
= make_it_fail
;
1181 put_task_struct(task
);
1182 if (end
- buffer
== 0)
1184 return end
- buffer
;
1187 static const struct file_operations proc_fault_inject_operations
= {
1188 .read
= proc_fault_inject_read
,
1189 .write
= proc_fault_inject_write
,
1194 #ifdef CONFIG_SCHED_DEBUG
1196 * Print out various scheduling related per-task fields:
1198 static int sched_show(struct seq_file
*m
, void *v
)
1200 struct inode
*inode
= m
->private;
1201 struct task_struct
*p
;
1203 p
= get_proc_task(inode
);
1206 proc_sched_show_task(p
, m
);
1214 sched_write(struct file
*file
, const char __user
*buf
,
1215 size_t count
, loff_t
*offset
)
1217 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1218 struct task_struct
*p
;
1220 p
= get_proc_task(inode
);
1223 proc_sched_set_task(p
);
1230 static int sched_open(struct inode
*inode
, struct file
*filp
)
1234 ret
= single_open(filp
, sched_show
, NULL
);
1236 struct seq_file
*m
= filp
->private_data
;
1243 static const struct file_operations proc_pid_sched_operations
= {
1246 .write
= sched_write
,
1247 .llseek
= seq_lseek
,
1248 .release
= single_release
,
1254 * We added or removed a vma mapping the executable. The vmas are only mapped
1255 * during exec and are not mapped with the mmap system call.
1256 * Callers must hold down_write() on the mm's mmap_sem for these
1258 void added_exe_file_vma(struct mm_struct
*mm
)
1260 mm
->num_exe_file_vmas
++;
1263 void removed_exe_file_vma(struct mm_struct
*mm
)
1265 mm
->num_exe_file_vmas
--;
1266 if ((mm
->num_exe_file_vmas
== 0) && mm
->exe_file
){
1268 mm
->exe_file
= NULL
;
1273 void set_mm_exe_file(struct mm_struct
*mm
, struct file
*new_exe_file
)
1276 get_file(new_exe_file
);
1279 mm
->exe_file
= new_exe_file
;
1280 mm
->num_exe_file_vmas
= 0;
1283 struct file
*get_mm_exe_file(struct mm_struct
*mm
)
1285 struct file
*exe_file
;
1287 /* We need mmap_sem to protect against races with removal of
1288 * VM_EXECUTABLE vmas */
1289 down_read(&mm
->mmap_sem
);
1290 exe_file
= mm
->exe_file
;
1293 up_read(&mm
->mmap_sem
);
1297 void dup_mm_exe_file(struct mm_struct
*oldmm
, struct mm_struct
*newmm
)
1299 /* It's safe to write the exe_file pointer without exe_file_lock because
1300 * this is called during fork when the task is not yet in /proc */
1301 newmm
->exe_file
= get_mm_exe_file(oldmm
);
1304 static int proc_exe_link(struct inode
*inode
, struct path
*exe_path
)
1306 struct task_struct
*task
;
1307 struct mm_struct
*mm
;
1308 struct file
*exe_file
;
1310 task
= get_proc_task(inode
);
1313 mm
= get_task_mm(task
);
1314 put_task_struct(task
);
1317 exe_file
= get_mm_exe_file(mm
);
1320 *exe_path
= exe_file
->f_path
;
1321 path_get(&exe_file
->f_path
);
1328 static void *proc_pid_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
1330 struct inode
*inode
= dentry
->d_inode
;
1331 int error
= -EACCES
;
1333 /* We don't need a base pointer in the /proc filesystem */
1334 path_put(&nd
->path
);
1336 /* Are we allowed to snoop on the tasks file descriptors? */
1337 if (!proc_fd_access_allowed(inode
))
1340 error
= PROC_I(inode
)->op
.proc_get_link(inode
, &nd
->path
);
1341 nd
->last_type
= LAST_BIND
;
1343 return ERR_PTR(error
);
1346 static int do_proc_readlink(struct path
*path
, char __user
*buffer
, int buflen
)
1348 char *tmp
= (char*)__get_free_page(GFP_TEMPORARY
);
1355 pathname
= d_path(path
, tmp
, PAGE_SIZE
);
1356 len
= PTR_ERR(pathname
);
1357 if (IS_ERR(pathname
))
1359 len
= tmp
+ PAGE_SIZE
- 1 - pathname
;
1363 if (copy_to_user(buffer
, pathname
, len
))
1366 free_page((unsigned long)tmp
);
1370 static int proc_pid_readlink(struct dentry
* dentry
, char __user
* buffer
, int buflen
)
1372 int error
= -EACCES
;
1373 struct inode
*inode
= dentry
->d_inode
;
1376 /* Are we allowed to snoop on the tasks file descriptors? */
1377 if (!proc_fd_access_allowed(inode
))
1380 error
= PROC_I(inode
)->op
.proc_get_link(inode
, &path
);
1384 error
= do_proc_readlink(&path
, buffer
, buflen
);
1390 static const struct inode_operations proc_pid_link_inode_operations
= {
1391 .readlink
= proc_pid_readlink
,
1392 .follow_link
= proc_pid_follow_link
,
1393 .setattr
= proc_setattr
,
1397 /* building an inode */
1399 static int task_dumpable(struct task_struct
*task
)
1402 struct mm_struct
*mm
;
1407 dumpable
= get_dumpable(mm
);
1415 static struct inode
*proc_pid_make_inode(struct super_block
* sb
, struct task_struct
*task
)
1417 struct inode
* inode
;
1418 struct proc_inode
*ei
;
1419 const struct cred
*cred
;
1421 /* We need a new inode */
1423 inode
= new_inode(sb
);
1429 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1430 inode
->i_op
= &proc_def_inode_operations
;
1433 * grab the reference to task.
1435 ei
->pid
= get_task_pid(task
, PIDTYPE_PID
);
1439 if (task_dumpable(task
)) {
1441 cred
= __task_cred(task
);
1442 inode
->i_uid
= cred
->euid
;
1443 inode
->i_gid
= cred
->egid
;
1446 security_task_to_inode(task
, inode
);
1456 static int pid_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
1458 struct inode
*inode
= dentry
->d_inode
;
1459 struct task_struct
*task
;
1460 const struct cred
*cred
;
1462 generic_fillattr(inode
, stat
);
1467 task
= pid_task(proc_pid(inode
), PIDTYPE_PID
);
1469 if ((inode
->i_mode
== (S_IFDIR
|S_IRUGO
|S_IXUGO
)) ||
1470 task_dumpable(task
)) {
1471 cred
= __task_cred(task
);
1472 stat
->uid
= cred
->euid
;
1473 stat
->gid
= cred
->egid
;
1483 * Exceptional case: normally we are not allowed to unhash a busy
1484 * directory. In this case, however, we can do it - no aliasing problems
1485 * due to the way we treat inodes.
1487 * Rewrite the inode's ownerships here because the owning task may have
1488 * performed a setuid(), etc.
1490 * Before the /proc/pid/status file was created the only way to read
1491 * the effective uid of a /process was to stat /proc/pid. Reading
1492 * /proc/pid/status is slow enough that procps and other packages
1493 * kept stating /proc/pid. To keep the rules in /proc simple I have
1494 * made this apply to all per process world readable and executable
1497 static int pid_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1499 struct inode
*inode
= dentry
->d_inode
;
1500 struct task_struct
*task
= get_proc_task(inode
);
1501 const struct cred
*cred
;
1504 if ((inode
->i_mode
== (S_IFDIR
|S_IRUGO
|S_IXUGO
)) ||
1505 task_dumpable(task
)) {
1507 cred
= __task_cred(task
);
1508 inode
->i_uid
= cred
->euid
;
1509 inode
->i_gid
= cred
->egid
;
1515 inode
->i_mode
&= ~(S_ISUID
| S_ISGID
);
1516 security_task_to_inode(task
, inode
);
1517 put_task_struct(task
);
1524 static int pid_delete_dentry(struct dentry
* dentry
)
1526 /* Is the task we represent dead?
1527 * If so, then don't put the dentry on the lru list,
1528 * kill it immediately.
1530 return !proc_pid(dentry
->d_inode
)->tasks
[PIDTYPE_PID
].first
;
1533 static struct dentry_operations pid_dentry_operations
=
1535 .d_revalidate
= pid_revalidate
,
1536 .d_delete
= pid_delete_dentry
,
1541 typedef struct dentry
*instantiate_t(struct inode
*, struct dentry
*,
1542 struct task_struct
*, const void *);
1545 * Fill a directory entry.
1547 * If possible create the dcache entry and derive our inode number and
1548 * file type from dcache entry.
1550 * Since all of the proc inode numbers are dynamically generated, the inode
1551 * numbers do not exist until the inode is cache. This means creating the
1552 * the dcache entry in readdir is necessary to keep the inode numbers
1553 * reported by readdir in sync with the inode numbers reported
1556 static int proc_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
1557 char *name
, int len
,
1558 instantiate_t instantiate
, struct task_struct
*task
, const void *ptr
)
1560 struct dentry
*child
, *dir
= filp
->f_path
.dentry
;
1561 struct inode
*inode
;
1564 unsigned type
= DT_UNKNOWN
;
1568 qname
.hash
= full_name_hash(name
, len
);
1570 child
= d_lookup(dir
, &qname
);
1573 new = d_alloc(dir
, &qname
);
1575 child
= instantiate(dir
->d_inode
, new, task
, ptr
);
1582 if (!child
|| IS_ERR(child
) || !child
->d_inode
)
1583 goto end_instantiate
;
1584 inode
= child
->d_inode
;
1587 type
= inode
->i_mode
>> 12;
1592 ino
= find_inode_number(dir
, &qname
);
1595 return filldir(dirent
, name
, len
, filp
->f_pos
, ino
, type
);
1598 static unsigned name_to_int(struct dentry
*dentry
)
1600 const char *name
= dentry
->d_name
.name
;
1601 int len
= dentry
->d_name
.len
;
1604 if (len
> 1 && *name
== '0')
1607 unsigned c
= *name
++ - '0';
1610 if (n
>= (~0U-9)/10)
1620 #define PROC_FDINFO_MAX 64
1622 static int proc_fd_info(struct inode
*inode
, struct path
*path
, char *info
)
1624 struct task_struct
*task
= get_proc_task(inode
);
1625 struct files_struct
*files
= NULL
;
1627 int fd
= proc_fd(inode
);
1630 files
= get_files_struct(task
);
1631 put_task_struct(task
);
1635 * We are not taking a ref to the file structure, so we must
1638 spin_lock(&files
->file_lock
);
1639 file
= fcheck_files(files
, fd
);
1642 *path
= file
->f_path
;
1643 path_get(&file
->f_path
);
1646 snprintf(info
, PROC_FDINFO_MAX
,
1649 (long long) file
->f_pos
,
1651 spin_unlock(&files
->file_lock
);
1652 put_files_struct(files
);
1655 spin_unlock(&files
->file_lock
);
1656 put_files_struct(files
);
1661 static int proc_fd_link(struct inode
*inode
, struct path
*path
)
1663 return proc_fd_info(inode
, path
, NULL
);
1666 static int tid_fd_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1668 struct inode
*inode
= dentry
->d_inode
;
1669 struct task_struct
*task
= get_proc_task(inode
);
1670 int fd
= proc_fd(inode
);
1671 struct files_struct
*files
;
1672 const struct cred
*cred
;
1675 files
= get_files_struct(task
);
1678 if (fcheck_files(files
, fd
)) {
1680 put_files_struct(files
);
1681 if (task_dumpable(task
)) {
1683 cred
= __task_cred(task
);
1684 inode
->i_uid
= cred
->euid
;
1685 inode
->i_gid
= cred
->egid
;
1691 inode
->i_mode
&= ~(S_ISUID
| S_ISGID
);
1692 security_task_to_inode(task
, inode
);
1693 put_task_struct(task
);
1697 put_files_struct(files
);
1699 put_task_struct(task
);
1705 static struct dentry_operations tid_fd_dentry_operations
=
1707 .d_revalidate
= tid_fd_revalidate
,
1708 .d_delete
= pid_delete_dentry
,
1711 static struct dentry
*proc_fd_instantiate(struct inode
*dir
,
1712 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
1714 unsigned fd
= *(const unsigned *)ptr
;
1716 struct files_struct
*files
;
1717 struct inode
*inode
;
1718 struct proc_inode
*ei
;
1719 struct dentry
*error
= ERR_PTR(-ENOENT
);
1721 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
1726 files
= get_files_struct(task
);
1729 inode
->i_mode
= S_IFLNK
;
1732 * We are not taking a ref to the file structure, so we must
1735 spin_lock(&files
->file_lock
);
1736 file
= fcheck_files(files
, fd
);
1739 if (file
->f_mode
& FMODE_READ
)
1740 inode
->i_mode
|= S_IRUSR
| S_IXUSR
;
1741 if (file
->f_mode
& FMODE_WRITE
)
1742 inode
->i_mode
|= S_IWUSR
| S_IXUSR
;
1743 spin_unlock(&files
->file_lock
);
1744 put_files_struct(files
);
1746 inode
->i_op
= &proc_pid_link_inode_operations
;
1748 ei
->op
.proc_get_link
= proc_fd_link
;
1749 dentry
->d_op
= &tid_fd_dentry_operations
;
1750 d_add(dentry
, inode
);
1751 /* Close the race of the process dying before we return the dentry */
1752 if (tid_fd_revalidate(dentry
, NULL
))
1758 spin_unlock(&files
->file_lock
);
1759 put_files_struct(files
);
1765 static struct dentry
*proc_lookupfd_common(struct inode
*dir
,
1766 struct dentry
*dentry
,
1767 instantiate_t instantiate
)
1769 struct task_struct
*task
= get_proc_task(dir
);
1770 unsigned fd
= name_to_int(dentry
);
1771 struct dentry
*result
= ERR_PTR(-ENOENT
);
1778 result
= instantiate(dir
, dentry
, task
, &fd
);
1780 put_task_struct(task
);
1785 static int proc_readfd_common(struct file
* filp
, void * dirent
,
1786 filldir_t filldir
, instantiate_t instantiate
)
1788 struct dentry
*dentry
= filp
->f_path
.dentry
;
1789 struct inode
*inode
= dentry
->d_inode
;
1790 struct task_struct
*p
= get_proc_task(inode
);
1791 unsigned int fd
, ino
;
1793 struct files_struct
* files
;
1803 if (filldir(dirent
, ".", 1, 0, inode
->i_ino
, DT_DIR
) < 0)
1807 ino
= parent_ino(dentry
);
1808 if (filldir(dirent
, "..", 2, 1, ino
, DT_DIR
) < 0)
1812 files
= get_files_struct(p
);
1816 for (fd
= filp
->f_pos
-2;
1817 fd
< files_fdtable(files
)->max_fds
;
1818 fd
++, filp
->f_pos
++) {
1819 char name
[PROC_NUMBUF
];
1822 if (!fcheck_files(files
, fd
))
1826 len
= snprintf(name
, sizeof(name
), "%d", fd
);
1827 if (proc_fill_cache(filp
, dirent
, filldir
,
1828 name
, len
, instantiate
,
1836 put_files_struct(files
);
1844 static struct dentry
*proc_lookupfd(struct inode
*dir
, struct dentry
*dentry
,
1845 struct nameidata
*nd
)
1847 return proc_lookupfd_common(dir
, dentry
, proc_fd_instantiate
);
1850 static int proc_readfd(struct file
*filp
, void *dirent
, filldir_t filldir
)
1852 return proc_readfd_common(filp
, dirent
, filldir
, proc_fd_instantiate
);
1855 static ssize_t
proc_fdinfo_read(struct file
*file
, char __user
*buf
,
1856 size_t len
, loff_t
*ppos
)
1858 char tmp
[PROC_FDINFO_MAX
];
1859 int err
= proc_fd_info(file
->f_path
.dentry
->d_inode
, NULL
, tmp
);
1861 err
= simple_read_from_buffer(buf
, len
, ppos
, tmp
, strlen(tmp
));
1865 static const struct file_operations proc_fdinfo_file_operations
= {
1866 .open
= nonseekable_open
,
1867 .read
= proc_fdinfo_read
,
1870 static const struct file_operations proc_fd_operations
= {
1871 .read
= generic_read_dir
,
1872 .readdir
= proc_readfd
,
1876 * /proc/pid/fd needs a special permission handler so that a process can still
1877 * access /proc/self/fd after it has executed a setuid().
1879 static int proc_fd_permission(struct inode
*inode
, int mask
)
1883 rv
= generic_permission(inode
, mask
, NULL
);
1886 if (task_pid(current
) == proc_pid(inode
))
1892 * proc directories can do almost nothing..
1894 static const struct inode_operations proc_fd_inode_operations
= {
1895 .lookup
= proc_lookupfd
,
1896 .permission
= proc_fd_permission
,
1897 .setattr
= proc_setattr
,
1900 static struct dentry
*proc_fdinfo_instantiate(struct inode
*dir
,
1901 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
1903 unsigned fd
= *(unsigned *)ptr
;
1904 struct inode
*inode
;
1905 struct proc_inode
*ei
;
1906 struct dentry
*error
= ERR_PTR(-ENOENT
);
1908 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
1913 inode
->i_mode
= S_IFREG
| S_IRUSR
;
1914 inode
->i_fop
= &proc_fdinfo_file_operations
;
1915 dentry
->d_op
= &tid_fd_dentry_operations
;
1916 d_add(dentry
, inode
);
1917 /* Close the race of the process dying before we return the dentry */
1918 if (tid_fd_revalidate(dentry
, NULL
))
1925 static struct dentry
*proc_lookupfdinfo(struct inode
*dir
,
1926 struct dentry
*dentry
,
1927 struct nameidata
*nd
)
1929 return proc_lookupfd_common(dir
, dentry
, proc_fdinfo_instantiate
);
1932 static int proc_readfdinfo(struct file
*filp
, void *dirent
, filldir_t filldir
)
1934 return proc_readfd_common(filp
, dirent
, filldir
,
1935 proc_fdinfo_instantiate
);
1938 static const struct file_operations proc_fdinfo_operations
= {
1939 .read
= generic_read_dir
,
1940 .readdir
= proc_readfdinfo
,
1944 * proc directories can do almost nothing..
1946 static const struct inode_operations proc_fdinfo_inode_operations
= {
1947 .lookup
= proc_lookupfdinfo
,
1948 .setattr
= proc_setattr
,
1952 static struct dentry
*proc_pident_instantiate(struct inode
*dir
,
1953 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
1955 const struct pid_entry
*p
= ptr
;
1956 struct inode
*inode
;
1957 struct proc_inode
*ei
;
1958 struct dentry
*error
= ERR_PTR(-EINVAL
);
1960 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
1965 inode
->i_mode
= p
->mode
;
1966 if (S_ISDIR(inode
->i_mode
))
1967 inode
->i_nlink
= 2; /* Use getattr to fix if necessary */
1969 inode
->i_op
= p
->iop
;
1971 inode
->i_fop
= p
->fop
;
1973 dentry
->d_op
= &pid_dentry_operations
;
1974 d_add(dentry
, inode
);
1975 /* Close the race of the process dying before we return the dentry */
1976 if (pid_revalidate(dentry
, NULL
))
1982 static struct dentry
*proc_pident_lookup(struct inode
*dir
,
1983 struct dentry
*dentry
,
1984 const struct pid_entry
*ents
,
1987 struct dentry
*error
;
1988 struct task_struct
*task
= get_proc_task(dir
);
1989 const struct pid_entry
*p
, *last
;
1991 error
= ERR_PTR(-ENOENT
);
1997 * Yes, it does not scale. And it should not. Don't add
1998 * new entries into /proc/<tgid>/ without very good reasons.
2000 last
= &ents
[nents
- 1];
2001 for (p
= ents
; p
<= last
; p
++) {
2002 if (p
->len
!= dentry
->d_name
.len
)
2004 if (!memcmp(dentry
->d_name
.name
, p
->name
, p
->len
))
2010 error
= proc_pident_instantiate(dir
, dentry
, task
, p
);
2012 put_task_struct(task
);
2017 static int proc_pident_fill_cache(struct file
*filp
, void *dirent
,
2018 filldir_t filldir
, struct task_struct
*task
, const struct pid_entry
*p
)
2020 return proc_fill_cache(filp
, dirent
, filldir
, p
->name
, p
->len
,
2021 proc_pident_instantiate
, task
, p
);
2024 static int proc_pident_readdir(struct file
*filp
,
2025 void *dirent
, filldir_t filldir
,
2026 const struct pid_entry
*ents
, unsigned int nents
)
2029 struct dentry
*dentry
= filp
->f_path
.dentry
;
2030 struct inode
*inode
= dentry
->d_inode
;
2031 struct task_struct
*task
= get_proc_task(inode
);
2032 const struct pid_entry
*p
, *last
;
2045 if (filldir(dirent
, ".", 1, i
, ino
, DT_DIR
) < 0)
2051 ino
= parent_ino(dentry
);
2052 if (filldir(dirent
, "..", 2, i
, ino
, DT_DIR
) < 0)
2064 last
= &ents
[nents
- 1];
2066 if (proc_pident_fill_cache(filp
, dirent
, filldir
, task
, p
) < 0)
2075 put_task_struct(task
);
2080 #ifdef CONFIG_SECURITY
2081 static ssize_t
proc_pid_attr_read(struct file
* file
, char __user
* buf
,
2082 size_t count
, loff_t
*ppos
)
2084 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
2087 struct task_struct
*task
= get_proc_task(inode
);
2092 length
= security_getprocattr(task
,
2093 (char*)file
->f_path
.dentry
->d_name
.name
,
2095 put_task_struct(task
);
2097 length
= simple_read_from_buffer(buf
, count
, ppos
, p
, length
);
2102 static ssize_t
proc_pid_attr_write(struct file
* file
, const char __user
* buf
,
2103 size_t count
, loff_t
*ppos
)
2105 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
2108 struct task_struct
*task
= get_proc_task(inode
);
2113 if (count
> PAGE_SIZE
)
2116 /* No partial writes. */
2122 page
= (char*)__get_free_page(GFP_TEMPORARY
);
2127 if (copy_from_user(page
, buf
, count
))
2130 length
= security_setprocattr(task
,
2131 (char*)file
->f_path
.dentry
->d_name
.name
,
2132 (void*)page
, count
);
2134 free_page((unsigned long) page
);
2136 put_task_struct(task
);
2141 static const struct file_operations proc_pid_attr_operations
= {
2142 .read
= proc_pid_attr_read
,
2143 .write
= proc_pid_attr_write
,
2146 static const struct pid_entry attr_dir_stuff
[] = {
2147 REG("current", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2148 REG("prev", S_IRUGO
, proc_pid_attr_operations
),
2149 REG("exec", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2150 REG("fscreate", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2151 REG("keycreate", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2152 REG("sockcreate", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2155 static int proc_attr_dir_readdir(struct file
* filp
,
2156 void * dirent
, filldir_t filldir
)
2158 return proc_pident_readdir(filp
,dirent
,filldir
,
2159 attr_dir_stuff
,ARRAY_SIZE(attr_dir_stuff
));
2162 static const struct file_operations proc_attr_dir_operations
= {
2163 .read
= generic_read_dir
,
2164 .readdir
= proc_attr_dir_readdir
,
2167 static struct dentry
*proc_attr_dir_lookup(struct inode
*dir
,
2168 struct dentry
*dentry
, struct nameidata
*nd
)
2170 return proc_pident_lookup(dir
, dentry
,
2171 attr_dir_stuff
, ARRAY_SIZE(attr_dir_stuff
));
2174 static const struct inode_operations proc_attr_dir_inode_operations
= {
2175 .lookup
= proc_attr_dir_lookup
,
2176 .getattr
= pid_getattr
,
2177 .setattr
= proc_setattr
,
2182 #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2183 static ssize_t
proc_coredump_filter_read(struct file
*file
, char __user
*buf
,
2184 size_t count
, loff_t
*ppos
)
2186 struct task_struct
*task
= get_proc_task(file
->f_dentry
->d_inode
);
2187 struct mm_struct
*mm
;
2188 char buffer
[PROC_NUMBUF
];
2196 mm
= get_task_mm(task
);
2198 len
= snprintf(buffer
, sizeof(buffer
), "%08lx\n",
2199 ((mm
->flags
& MMF_DUMP_FILTER_MASK
) >>
2200 MMF_DUMP_FILTER_SHIFT
));
2202 ret
= simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
2205 put_task_struct(task
);
2210 static ssize_t
proc_coredump_filter_write(struct file
*file
,
2211 const char __user
*buf
,
2215 struct task_struct
*task
;
2216 struct mm_struct
*mm
;
2217 char buffer
[PROC_NUMBUF
], *end
;
2224 memset(buffer
, 0, sizeof(buffer
));
2225 if (count
> sizeof(buffer
) - 1)
2226 count
= sizeof(buffer
) - 1;
2227 if (copy_from_user(buffer
, buf
, count
))
2231 val
= (unsigned int)simple_strtoul(buffer
, &end
, 0);
2234 if (end
- buffer
== 0)
2238 task
= get_proc_task(file
->f_dentry
->d_inode
);
2243 mm
= get_task_mm(task
);
2247 for (i
= 0, mask
= 1; i
< MMF_DUMP_FILTER_BITS
; i
++, mask
<<= 1) {
2249 set_bit(i
+ MMF_DUMP_FILTER_SHIFT
, &mm
->flags
);
2251 clear_bit(i
+ MMF_DUMP_FILTER_SHIFT
, &mm
->flags
);
2256 put_task_struct(task
);
2261 static const struct file_operations proc_coredump_filter_operations
= {
2262 .read
= proc_coredump_filter_read
,
2263 .write
= proc_coredump_filter_write
,
2270 static int proc_self_readlink(struct dentry
*dentry
, char __user
*buffer
,
2273 struct pid_namespace
*ns
= dentry
->d_sb
->s_fs_info
;
2274 pid_t tgid
= task_tgid_nr_ns(current
, ns
);
2275 char tmp
[PROC_NUMBUF
];
2278 sprintf(tmp
, "%d", tgid
);
2279 return vfs_readlink(dentry
,buffer
,buflen
,tmp
);
2282 static void *proc_self_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
2284 struct pid_namespace
*ns
= dentry
->d_sb
->s_fs_info
;
2285 pid_t tgid
= task_tgid_nr_ns(current
, ns
);
2286 char tmp
[PROC_NUMBUF
];
2288 return ERR_PTR(-ENOENT
);
2289 sprintf(tmp
, "%d", task_tgid_nr_ns(current
, ns
));
2290 return ERR_PTR(vfs_follow_link(nd
,tmp
));
2293 static const struct inode_operations proc_self_inode_operations
= {
2294 .readlink
= proc_self_readlink
,
2295 .follow_link
= proc_self_follow_link
,
2301 * These are the directory entries in the root directory of /proc
2302 * that properly belong to the /proc filesystem, as they describe
2303 * describe something that is process related.
2305 static const struct pid_entry proc_base_stuff
[] = {
2306 NOD("self", S_IFLNK
|S_IRWXUGO
,
2307 &proc_self_inode_operations
, NULL
, {}),
2311 * Exceptional case: normally we are not allowed to unhash a busy
2312 * directory. In this case, however, we can do it - no aliasing problems
2313 * due to the way we treat inodes.
2315 static int proc_base_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
2317 struct inode
*inode
= dentry
->d_inode
;
2318 struct task_struct
*task
= get_proc_task(inode
);
2320 put_task_struct(task
);
2327 static struct dentry_operations proc_base_dentry_operations
=
2329 .d_revalidate
= proc_base_revalidate
,
2330 .d_delete
= pid_delete_dentry
,
2333 static struct dentry
*proc_base_instantiate(struct inode
*dir
,
2334 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
2336 const struct pid_entry
*p
= ptr
;
2337 struct inode
*inode
;
2338 struct proc_inode
*ei
;
2339 struct dentry
*error
= ERR_PTR(-EINVAL
);
2341 /* Allocate the inode */
2342 error
= ERR_PTR(-ENOMEM
);
2343 inode
= new_inode(dir
->i_sb
);
2347 /* Initialize the inode */
2349 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
2352 * grab the reference to the task.
2354 ei
->pid
= get_task_pid(task
, PIDTYPE_PID
);
2358 inode
->i_mode
= p
->mode
;
2359 if (S_ISDIR(inode
->i_mode
))
2361 if (S_ISLNK(inode
->i_mode
))
2364 inode
->i_op
= p
->iop
;
2366 inode
->i_fop
= p
->fop
;
2368 dentry
->d_op
= &proc_base_dentry_operations
;
2369 d_add(dentry
, inode
);
2378 static struct dentry
*proc_base_lookup(struct inode
*dir
, struct dentry
*dentry
)
2380 struct dentry
*error
;
2381 struct task_struct
*task
= get_proc_task(dir
);
2382 const struct pid_entry
*p
, *last
;
2384 error
= ERR_PTR(-ENOENT
);
2389 /* Lookup the directory entry */
2390 last
= &proc_base_stuff
[ARRAY_SIZE(proc_base_stuff
) - 1];
2391 for (p
= proc_base_stuff
; p
<= last
; p
++) {
2392 if (p
->len
!= dentry
->d_name
.len
)
2394 if (!memcmp(dentry
->d_name
.name
, p
->name
, p
->len
))
2400 error
= proc_base_instantiate(dir
, dentry
, task
, p
);
2403 put_task_struct(task
);
2408 static int proc_base_fill_cache(struct file
*filp
, void *dirent
,
2409 filldir_t filldir
, struct task_struct
*task
, const struct pid_entry
*p
)
2411 return proc_fill_cache(filp
, dirent
, filldir
, p
->name
, p
->len
,
2412 proc_base_instantiate
, task
, p
);
2415 #ifdef CONFIG_TASK_IO_ACCOUNTING
2416 static int do_io_accounting(struct task_struct
*task
, char *buffer
, int whole
)
2418 struct task_io_accounting acct
= task
->ioac
;
2419 unsigned long flags
;
2421 if (whole
&& lock_task_sighand(task
, &flags
)) {
2422 struct task_struct
*t
= task
;
2424 task_io_accounting_add(&acct
, &task
->signal
->ioac
);
2425 while_each_thread(task
, t
)
2426 task_io_accounting_add(&acct
, &t
->ioac
);
2428 unlock_task_sighand(task
, &flags
);
2430 return sprintf(buffer
,
2435 "read_bytes: %llu\n"
2436 "write_bytes: %llu\n"
2437 "cancelled_write_bytes: %llu\n",
2438 (unsigned long long)acct
.rchar
,
2439 (unsigned long long)acct
.wchar
,
2440 (unsigned long long)acct
.syscr
,
2441 (unsigned long long)acct
.syscw
,
2442 (unsigned long long)acct
.read_bytes
,
2443 (unsigned long long)acct
.write_bytes
,
2444 (unsigned long long)acct
.cancelled_write_bytes
);
2447 static int proc_tid_io_accounting(struct task_struct
*task
, char *buffer
)
2449 return do_io_accounting(task
, buffer
, 0);
2452 static int proc_tgid_io_accounting(struct task_struct
*task
, char *buffer
)
2454 return do_io_accounting(task
, buffer
, 1);
2456 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2458 static int proc_pid_personality(struct seq_file
*m
, struct pid_namespace
*ns
,
2459 struct pid
*pid
, struct task_struct
*task
)
2461 seq_printf(m
, "%08x\n", task
->personality
);
2468 static const struct file_operations proc_task_operations
;
2469 static const struct inode_operations proc_task_inode_operations
;
2471 static const struct pid_entry tgid_base_stuff
[] = {
2472 DIR("task", S_IRUGO
|S_IXUGO
, proc_task_inode_operations
, proc_task_operations
),
2473 DIR("fd", S_IRUSR
|S_IXUSR
, proc_fd_inode_operations
, proc_fd_operations
),
2474 DIR("fdinfo", S_IRUSR
|S_IXUSR
, proc_fdinfo_inode_operations
, proc_fdinfo_operations
),
2476 DIR("net", S_IRUGO
|S_IXUGO
, proc_net_inode_operations
, proc_net_operations
),
2478 REG("environ", S_IRUSR
, proc_environ_operations
),
2479 INF("auxv", S_IRUSR
, proc_pid_auxv
),
2480 ONE("status", S_IRUGO
, proc_pid_status
),
2481 ONE("personality", S_IRUSR
, proc_pid_personality
),
2482 INF("limits", S_IRUSR
, proc_pid_limits
),
2483 #ifdef CONFIG_SCHED_DEBUG
2484 REG("sched", S_IRUGO
|S_IWUSR
, proc_pid_sched_operations
),
2486 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2487 INF("syscall", S_IRUSR
, proc_pid_syscall
),
2489 INF("cmdline", S_IRUGO
, proc_pid_cmdline
),
2490 ONE("stat", S_IRUGO
, proc_tgid_stat
),
2491 ONE("statm", S_IRUGO
, proc_pid_statm
),
2492 REG("maps", S_IRUGO
, proc_maps_operations
),
2494 REG("numa_maps", S_IRUGO
, proc_numa_maps_operations
),
2496 REG("mem", S_IRUSR
|S_IWUSR
, proc_mem_operations
),
2497 LNK("cwd", proc_cwd_link
),
2498 LNK("root", proc_root_link
),
2499 LNK("exe", proc_exe_link
),
2500 REG("mounts", S_IRUGO
, proc_mounts_operations
),
2501 REG("mountinfo", S_IRUGO
, proc_mountinfo_operations
),
2502 REG("mountstats", S_IRUSR
, proc_mountstats_operations
),
2503 #ifdef CONFIG_PROC_PAGE_MONITOR
2504 REG("clear_refs", S_IWUSR
, proc_clear_refs_operations
),
2505 REG("smaps", S_IRUGO
, proc_smaps_operations
),
2506 REG("pagemap", S_IRUSR
, proc_pagemap_operations
),
2508 #ifdef CONFIG_SECURITY
2509 DIR("attr", S_IRUGO
|S_IXUGO
, proc_attr_dir_inode_operations
, proc_attr_dir_operations
),
2511 #ifdef CONFIG_KALLSYMS
2512 INF("wchan", S_IRUGO
, proc_pid_wchan
),
2514 #ifdef CONFIG_STACKTRACE
2515 ONE("stack", S_IRUSR
, proc_pid_stack
),
2517 #ifdef CONFIG_SCHEDSTATS
2518 INF("schedstat", S_IRUGO
, proc_pid_schedstat
),
2520 #ifdef CONFIG_LATENCYTOP
2521 REG("latency", S_IRUGO
, proc_lstats_operations
),
2523 #ifdef CONFIG_PROC_PID_CPUSET
2524 REG("cpuset", S_IRUGO
, proc_cpuset_operations
),
2526 #ifdef CONFIG_CGROUPS
2527 REG("cgroup", S_IRUGO
, proc_cgroup_operations
),
2529 INF("oom_score", S_IRUGO
, proc_oom_score
),
2530 REG("oom_adj", S_IRUGO
|S_IWUSR
, proc_oom_adjust_operations
),
2531 #ifdef CONFIG_AUDITSYSCALL
2532 REG("loginuid", S_IWUSR
|S_IRUGO
, proc_loginuid_operations
),
2533 REG("sessionid", S_IRUGO
, proc_sessionid_operations
),
2535 #ifdef CONFIG_FAULT_INJECTION
2536 REG("make-it-fail", S_IRUGO
|S_IWUSR
, proc_fault_inject_operations
),
2538 #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2539 REG("coredump_filter", S_IRUGO
|S_IWUSR
, proc_coredump_filter_operations
),
2541 #ifdef CONFIG_TASK_IO_ACCOUNTING
2542 INF("io", S_IRUGO
, proc_tgid_io_accounting
),
2546 static int proc_tgid_base_readdir(struct file
* filp
,
2547 void * dirent
, filldir_t filldir
)
2549 return proc_pident_readdir(filp
,dirent
,filldir
,
2550 tgid_base_stuff
,ARRAY_SIZE(tgid_base_stuff
));
2553 static const struct file_operations proc_tgid_base_operations
= {
2554 .read
= generic_read_dir
,
2555 .readdir
= proc_tgid_base_readdir
,
2558 static struct dentry
*proc_tgid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
){
2559 return proc_pident_lookup(dir
, dentry
,
2560 tgid_base_stuff
, ARRAY_SIZE(tgid_base_stuff
));
2563 static const struct inode_operations proc_tgid_base_inode_operations
= {
2564 .lookup
= proc_tgid_base_lookup
,
2565 .getattr
= pid_getattr
,
2566 .setattr
= proc_setattr
,
2569 static void proc_flush_task_mnt(struct vfsmount
*mnt
, pid_t pid
, pid_t tgid
)
2571 struct dentry
*dentry
, *leader
, *dir
;
2572 char buf
[PROC_NUMBUF
];
2576 name
.len
= snprintf(buf
, sizeof(buf
), "%d", pid
);
2577 dentry
= d_hash_and_lookup(mnt
->mnt_root
, &name
);
2579 if (!(current
->flags
& PF_EXITING
))
2580 shrink_dcache_parent(dentry
);
2589 name
.len
= snprintf(buf
, sizeof(buf
), "%d", tgid
);
2590 leader
= d_hash_and_lookup(mnt
->mnt_root
, &name
);
2595 name
.len
= strlen(name
.name
);
2596 dir
= d_hash_and_lookup(leader
, &name
);
2598 goto out_put_leader
;
2601 name
.len
= snprintf(buf
, sizeof(buf
), "%d", pid
);
2602 dentry
= d_hash_and_lookup(dir
, &name
);
2604 shrink_dcache_parent(dentry
);
2617 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2618 * @task: task that should be flushed.
2620 * When flushing dentries from proc, one needs to flush them from global
2621 * proc (proc_mnt) and from all the namespaces' procs this task was seen
2622 * in. This call is supposed to do all of this job.
2624 * Looks in the dcache for
2626 * /proc/@tgid/task/@pid
2627 * if either directory is present flushes it and all of it'ts children
2630 * It is safe and reasonable to cache /proc entries for a task until
2631 * that task exits. After that they just clog up the dcache with
2632 * useless entries, possibly causing useful dcache entries to be
2633 * flushed instead. This routine is proved to flush those useless
2634 * dcache entries at process exit time.
2636 * NOTE: This routine is just an optimization so it does not guarantee
2637 * that no dcache entries will exist at process exit time it
2638 * just makes it very unlikely that any will persist.
2641 void proc_flush_task(struct task_struct
*task
)
2644 struct pid
*pid
, *tgid
= NULL
;
2647 pid
= task_pid(task
);
2648 if (thread_group_leader(task
))
2649 tgid
= task_tgid(task
);
2651 for (i
= 0; i
<= pid
->level
; i
++) {
2652 upid
= &pid
->numbers
[i
];
2653 proc_flush_task_mnt(upid
->ns
->proc_mnt
, upid
->nr
,
2654 tgid
? tgid
->numbers
[i
].nr
: 0);
2657 upid
= &pid
->numbers
[pid
->level
];
2659 pid_ns_release_proc(upid
->ns
);
2662 static struct dentry
*proc_pid_instantiate(struct inode
*dir
,
2663 struct dentry
* dentry
,
2664 struct task_struct
*task
, const void *ptr
)
2666 struct dentry
*error
= ERR_PTR(-ENOENT
);
2667 struct inode
*inode
;
2669 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
2673 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
2674 inode
->i_op
= &proc_tgid_base_inode_operations
;
2675 inode
->i_fop
= &proc_tgid_base_operations
;
2676 inode
->i_flags
|=S_IMMUTABLE
;
2678 inode
->i_nlink
= 2 + pid_entry_count_dirs(tgid_base_stuff
,
2679 ARRAY_SIZE(tgid_base_stuff
));
2681 dentry
->d_op
= &pid_dentry_operations
;
2683 d_add(dentry
, inode
);
2684 /* Close the race of the process dying before we return the dentry */
2685 if (pid_revalidate(dentry
, NULL
))
2691 struct dentry
*proc_pid_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
)
2693 struct dentry
*result
= ERR_PTR(-ENOENT
);
2694 struct task_struct
*task
;
2696 struct pid_namespace
*ns
;
2698 result
= proc_base_lookup(dir
, dentry
);
2699 if (!IS_ERR(result
) || PTR_ERR(result
) != -ENOENT
)
2702 tgid
= name_to_int(dentry
);
2706 ns
= dentry
->d_sb
->s_fs_info
;
2708 task
= find_task_by_pid_ns(tgid
, ns
);
2710 get_task_struct(task
);
2715 result
= proc_pid_instantiate(dir
, dentry
, task
, NULL
);
2716 put_task_struct(task
);
2722 * Find the first task with tgid >= tgid
2727 struct task_struct
*task
;
2729 static struct tgid_iter
next_tgid(struct pid_namespace
*ns
, struct tgid_iter iter
)
2734 put_task_struct(iter
.task
);
2738 pid
= find_ge_pid(iter
.tgid
, ns
);
2740 iter
.tgid
= pid_nr_ns(pid
, ns
);
2741 iter
.task
= pid_task(pid
, PIDTYPE_PID
);
2742 /* What we to know is if the pid we have find is the
2743 * pid of a thread_group_leader. Testing for task
2744 * being a thread_group_leader is the obvious thing
2745 * todo but there is a window when it fails, due to
2746 * the pid transfer logic in de_thread.
2748 * So we perform the straight forward test of seeing
2749 * if the pid we have found is the pid of a thread
2750 * group leader, and don't worry if the task we have
2751 * found doesn't happen to be a thread group leader.
2752 * As we don't care in the case of readdir.
2754 if (!iter
.task
|| !has_group_leader_pid(iter
.task
)) {
2758 get_task_struct(iter
.task
);
2764 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
2766 static int proc_pid_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
2767 struct tgid_iter iter
)
2769 char name
[PROC_NUMBUF
];
2770 int len
= snprintf(name
, sizeof(name
), "%d", iter
.tgid
);
2771 return proc_fill_cache(filp
, dirent
, filldir
, name
, len
,
2772 proc_pid_instantiate
, iter
.task
, NULL
);
2775 /* for the /proc/ directory itself, after non-process stuff has been done */
2776 int proc_pid_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
2778 unsigned int nr
= filp
->f_pos
- FIRST_PROCESS_ENTRY
;
2779 struct task_struct
*reaper
= get_proc_task(filp
->f_path
.dentry
->d_inode
);
2780 struct tgid_iter iter
;
2781 struct pid_namespace
*ns
;
2786 for (; nr
< ARRAY_SIZE(proc_base_stuff
); filp
->f_pos
++, nr
++) {
2787 const struct pid_entry
*p
= &proc_base_stuff
[nr
];
2788 if (proc_base_fill_cache(filp
, dirent
, filldir
, reaper
, p
) < 0)
2792 ns
= filp
->f_dentry
->d_sb
->s_fs_info
;
2794 iter
.tgid
= filp
->f_pos
- TGID_OFFSET
;
2795 for (iter
= next_tgid(ns
, iter
);
2797 iter
.tgid
+= 1, iter
= next_tgid(ns
, iter
)) {
2798 filp
->f_pos
= iter
.tgid
+ TGID_OFFSET
;
2799 if (proc_pid_fill_cache(filp
, dirent
, filldir
, iter
) < 0) {
2800 put_task_struct(iter
.task
);
2804 filp
->f_pos
= PID_MAX_LIMIT
+ TGID_OFFSET
;
2806 put_task_struct(reaper
);
2814 static const struct pid_entry tid_base_stuff
[] = {
2815 DIR("fd", S_IRUSR
|S_IXUSR
, proc_fd_inode_operations
, proc_fd_operations
),
2816 DIR("fdinfo", S_IRUSR
|S_IXUSR
, proc_fdinfo_inode_operations
, proc_fd_operations
),
2817 REG("environ", S_IRUSR
, proc_environ_operations
),
2818 INF("auxv", S_IRUSR
, proc_pid_auxv
),
2819 ONE("status", S_IRUGO
, proc_pid_status
),
2820 ONE("personality", S_IRUSR
, proc_pid_personality
),
2821 INF("limits", S_IRUSR
, proc_pid_limits
),
2822 #ifdef CONFIG_SCHED_DEBUG
2823 REG("sched", S_IRUGO
|S_IWUSR
, proc_pid_sched_operations
),
2825 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2826 INF("syscall", S_IRUSR
, proc_pid_syscall
),
2828 INF("cmdline", S_IRUGO
, proc_pid_cmdline
),
2829 ONE("stat", S_IRUGO
, proc_tid_stat
),
2830 ONE("statm", S_IRUGO
, proc_pid_statm
),
2831 REG("maps", S_IRUGO
, proc_maps_operations
),
2833 REG("numa_maps", S_IRUGO
, proc_numa_maps_operations
),
2835 REG("mem", S_IRUSR
|S_IWUSR
, proc_mem_operations
),
2836 LNK("cwd", proc_cwd_link
),
2837 LNK("root", proc_root_link
),
2838 LNK("exe", proc_exe_link
),
2839 REG("mounts", S_IRUGO
, proc_mounts_operations
),
2840 REG("mountinfo", S_IRUGO
, proc_mountinfo_operations
),
2841 #ifdef CONFIG_PROC_PAGE_MONITOR
2842 REG("clear_refs", S_IWUSR
, proc_clear_refs_operations
),
2843 REG("smaps", S_IRUGO
, proc_smaps_operations
),
2844 REG("pagemap", S_IRUSR
, proc_pagemap_operations
),
2846 #ifdef CONFIG_SECURITY
2847 DIR("attr", S_IRUGO
|S_IXUGO
, proc_attr_dir_inode_operations
, proc_attr_dir_operations
),
2849 #ifdef CONFIG_KALLSYMS
2850 INF("wchan", S_IRUGO
, proc_pid_wchan
),
2852 #ifdef CONFIG_STACKTRACE
2853 ONE("stack", S_IRUSR
, proc_pid_stack
),
2855 #ifdef CONFIG_SCHEDSTATS
2856 INF("schedstat", S_IRUGO
, proc_pid_schedstat
),
2858 #ifdef CONFIG_LATENCYTOP
2859 REG("latency", S_IRUGO
, proc_lstats_operations
),
2861 #ifdef CONFIG_PROC_PID_CPUSET
2862 REG("cpuset", S_IRUGO
, proc_cpuset_operations
),
2864 #ifdef CONFIG_CGROUPS
2865 REG("cgroup", S_IRUGO
, proc_cgroup_operations
),
2867 INF("oom_score", S_IRUGO
, proc_oom_score
),
2868 REG("oom_adj", S_IRUGO
|S_IWUSR
, proc_oom_adjust_operations
),
2869 #ifdef CONFIG_AUDITSYSCALL
2870 REG("loginuid", S_IWUSR
|S_IRUGO
, proc_loginuid_operations
),
2871 REG("sessionid", S_IRUSR
, proc_sessionid_operations
),
2873 #ifdef CONFIG_FAULT_INJECTION
2874 REG("make-it-fail", S_IRUGO
|S_IWUSR
, proc_fault_inject_operations
),
2876 #ifdef CONFIG_TASK_IO_ACCOUNTING
2877 INF("io", S_IRUGO
, proc_tid_io_accounting
),
2881 static int proc_tid_base_readdir(struct file
* filp
,
2882 void * dirent
, filldir_t filldir
)
2884 return proc_pident_readdir(filp
,dirent
,filldir
,
2885 tid_base_stuff
,ARRAY_SIZE(tid_base_stuff
));
2888 static struct dentry
*proc_tid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
){
2889 return proc_pident_lookup(dir
, dentry
,
2890 tid_base_stuff
, ARRAY_SIZE(tid_base_stuff
));
2893 static const struct file_operations proc_tid_base_operations
= {
2894 .read
= generic_read_dir
,
2895 .readdir
= proc_tid_base_readdir
,
2898 static const struct inode_operations proc_tid_base_inode_operations
= {
2899 .lookup
= proc_tid_base_lookup
,
2900 .getattr
= pid_getattr
,
2901 .setattr
= proc_setattr
,
2904 static struct dentry
*proc_task_instantiate(struct inode
*dir
,
2905 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
2907 struct dentry
*error
= ERR_PTR(-ENOENT
);
2908 struct inode
*inode
;
2909 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
2913 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
2914 inode
->i_op
= &proc_tid_base_inode_operations
;
2915 inode
->i_fop
= &proc_tid_base_operations
;
2916 inode
->i_flags
|=S_IMMUTABLE
;
2918 inode
->i_nlink
= 2 + pid_entry_count_dirs(tid_base_stuff
,
2919 ARRAY_SIZE(tid_base_stuff
));
2921 dentry
->d_op
= &pid_dentry_operations
;
2923 d_add(dentry
, inode
);
2924 /* Close the race of the process dying before we return the dentry */
2925 if (pid_revalidate(dentry
, NULL
))
2931 static struct dentry
*proc_task_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
)
2933 struct dentry
*result
= ERR_PTR(-ENOENT
);
2934 struct task_struct
*task
;
2935 struct task_struct
*leader
= get_proc_task(dir
);
2937 struct pid_namespace
*ns
;
2942 tid
= name_to_int(dentry
);
2946 ns
= dentry
->d_sb
->s_fs_info
;
2948 task
= find_task_by_pid_ns(tid
, ns
);
2950 get_task_struct(task
);
2954 if (!same_thread_group(leader
, task
))
2957 result
= proc_task_instantiate(dir
, dentry
, task
, NULL
);
2959 put_task_struct(task
);
2961 put_task_struct(leader
);
2967 * Find the first tid of a thread group to return to user space.
2969 * Usually this is just the thread group leader, but if the users
2970 * buffer was too small or there was a seek into the middle of the
2971 * directory we have more work todo.
2973 * In the case of a short read we start with find_task_by_pid.
2975 * In the case of a seek we start with the leader and walk nr
2978 static struct task_struct
*first_tid(struct task_struct
*leader
,
2979 int tid
, int nr
, struct pid_namespace
*ns
)
2981 struct task_struct
*pos
;
2984 /* Attempt to start with the pid of a thread */
2985 if (tid
&& (nr
> 0)) {
2986 pos
= find_task_by_pid_ns(tid
, ns
);
2987 if (pos
&& (pos
->group_leader
== leader
))
2991 /* If nr exceeds the number of threads there is nothing todo */
2993 if (nr
&& nr
>= get_nr_threads(leader
))
2996 /* If we haven't found our starting place yet start
2997 * with the leader and walk nr threads forward.
2999 for (pos
= leader
; nr
> 0; --nr
) {
3000 pos
= next_thread(pos
);
3001 if (pos
== leader
) {
3007 get_task_struct(pos
);
3014 * Find the next thread in the thread list.
3015 * Return NULL if there is an error or no next thread.
3017 * The reference to the input task_struct is released.
3019 static struct task_struct
*next_tid(struct task_struct
*start
)
3021 struct task_struct
*pos
= NULL
;
3023 if (pid_alive(start
)) {
3024 pos
= next_thread(start
);
3025 if (thread_group_leader(pos
))
3028 get_task_struct(pos
);
3031 put_task_struct(start
);
3035 static int proc_task_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
3036 struct task_struct
*task
, int tid
)
3038 char name
[PROC_NUMBUF
];
3039 int len
= snprintf(name
, sizeof(name
), "%d", tid
);
3040 return proc_fill_cache(filp
, dirent
, filldir
, name
, len
,
3041 proc_task_instantiate
, task
, NULL
);
3044 /* for the /proc/TGID/task/ directories */
3045 static int proc_task_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
3047 struct dentry
*dentry
= filp
->f_path
.dentry
;
3048 struct inode
*inode
= dentry
->d_inode
;
3049 struct task_struct
*leader
= NULL
;
3050 struct task_struct
*task
;
3051 int retval
= -ENOENT
;
3054 struct pid_namespace
*ns
;
3056 task
= get_proc_task(inode
);
3060 if (pid_alive(task
)) {
3061 leader
= task
->group_leader
;
3062 get_task_struct(leader
);
3065 put_task_struct(task
);
3070 switch ((unsigned long)filp
->f_pos
) {
3073 if (filldir(dirent
, ".", 1, filp
->f_pos
, ino
, DT_DIR
) < 0)
3078 ino
= parent_ino(dentry
);
3079 if (filldir(dirent
, "..", 2, filp
->f_pos
, ino
, DT_DIR
) < 0)
3085 /* f_version caches the tgid value that the last readdir call couldn't
3086 * return. lseek aka telldir automagically resets f_version to 0.
3088 ns
= filp
->f_dentry
->d_sb
->s_fs_info
;
3089 tid
= (int)filp
->f_version
;
3090 filp
->f_version
= 0;
3091 for (task
= first_tid(leader
, tid
, filp
->f_pos
- 2, ns
);
3093 task
= next_tid(task
), filp
->f_pos
++) {
3094 tid
= task_pid_nr_ns(task
, ns
);
3095 if (proc_task_fill_cache(filp
, dirent
, filldir
, task
, tid
) < 0) {
3096 /* returning this tgid failed, save it as the first
3097 * pid for the next readir call */
3098 filp
->f_version
= (u64
)tid
;
3099 put_task_struct(task
);
3104 put_task_struct(leader
);
3109 static int proc_task_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
3111 struct inode
*inode
= dentry
->d_inode
;
3112 struct task_struct
*p
= get_proc_task(inode
);
3113 generic_fillattr(inode
, stat
);
3116 stat
->nlink
+= get_nr_threads(p
);
3123 static const struct inode_operations proc_task_inode_operations
= {
3124 .lookup
= proc_task_lookup
,
3125 .getattr
= proc_task_getattr
,
3126 .setattr
= proc_setattr
,
3129 static const struct file_operations proc_task_operations
= {
3130 .read
= generic_read_dir
,
3131 .readdir
= proc_task_readdir
,