11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/fs/proc/base.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 1991, 1992 Linus Torvalds 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * proc base directory handling functions 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * 1999, Al Viro. Rewritten. Now it covers the whole per-process part. 91da177e4SLinus Torvalds * Instead of using magical inumbers to determine the kind of object 101da177e4SLinus Torvalds * we allocate and fill in-core inodes upon lookup. They don't even 111da177e4SLinus Torvalds * go into icache. We cache the reference to task_struct upon lookup too. 121da177e4SLinus Torvalds * Eventually it should become a filesystem in its own. We don't use the 131da177e4SLinus Torvalds * rest of procfs anymore. 14e070ad49SMauricio Lin * 15e070ad49SMauricio Lin * 16e070ad49SMauricio Lin * Changelog: 17e070ad49SMauricio Lin * 17-Jan-2005 18e070ad49SMauricio Lin * Allan Bezerra 19e070ad49SMauricio Lin * Bruna Moreira <bruna.moreira@indt.org.br> 20e070ad49SMauricio Lin * Edjard Mota <edjard.mota@indt.org.br> 21e070ad49SMauricio Lin * Ilias Biris <ilias.biris@indt.org.br> 22e070ad49SMauricio Lin * Mauricio Lin <mauricio.lin@indt.org.br> 23e070ad49SMauricio Lin * 24e070ad49SMauricio Lin * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT 25e070ad49SMauricio Lin * 26e070ad49SMauricio Lin * A new process specific entry (smaps) included in /proc. It shows the 27e070ad49SMauricio Lin * size of rss for each memory area. The maps entry lacks information 28e070ad49SMauricio Lin * about physical memory size (rss) for each mapped file, i.e., 29e070ad49SMauricio Lin * rss information for executables and library files. 30e070ad49SMauricio Lin * This additional information is useful for any tools that need to know 31e070ad49SMauricio Lin * about physical memory consumption for a process specific library. 32e070ad49SMauricio Lin * 33e070ad49SMauricio Lin * Changelog: 34e070ad49SMauricio Lin * 21-Feb-2005 35e070ad49SMauricio Lin * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT 36e070ad49SMauricio Lin * Pud inclusion in the page table walking. 37e070ad49SMauricio Lin * 38e070ad49SMauricio Lin * ChangeLog: 39e070ad49SMauricio Lin * 10-Mar-2005 40e070ad49SMauricio Lin * 10LE Instituto Nokia de Tecnologia - INdT: 41e070ad49SMauricio Lin * A better way to walks through the page table as suggested by Hugh Dickins. 42e070ad49SMauricio Lin * 43e070ad49SMauricio Lin * Simo Piiroinen <simo.piiroinen@nokia.com>: 44e070ad49SMauricio Lin * Smaps information related to shared, private, clean and dirty pages. 45e070ad49SMauricio Lin * 46e070ad49SMauricio Lin * Paul Mundt <paul.mundt@nokia.com>: 47e070ad49SMauricio Lin * Overall revision about smaps. 481da177e4SLinus Torvalds */ 491da177e4SLinus Torvalds 501da177e4SLinus Torvalds #include <asm/uaccess.h> 511da177e4SLinus Torvalds 521da177e4SLinus Torvalds #include <linux/errno.h> 531da177e4SLinus Torvalds #include <linux/time.h> 541da177e4SLinus Torvalds #include <linux/proc_fs.h> 551da177e4SLinus Torvalds #include <linux/stat.h> 565995477aSAndrea Righi #include <linux/task_io_accounting_ops.h> 571da177e4SLinus Torvalds #include <linux/init.h> 5816f7e0feSRandy Dunlap #include <linux/capability.h> 591da177e4SLinus Torvalds #include <linux/file.h> 609f3acc31SAl Viro #include <linux/fdtable.h> 611da177e4SLinus Torvalds #include <linux/string.h> 621da177e4SLinus Torvalds #include <linux/seq_file.h> 631da177e4SLinus Torvalds #include <linux/namei.h> 646b3286edSKirill Korotaev #include <linux/mnt_namespace.h> 651da177e4SLinus Torvalds #include <linux/mm.h> 66b835996fSDipankar Sarma #include <linux/rcupdate.h> 671da177e4SLinus Torvalds #include <linux/kallsyms.h> 682ec220e2SKen Chen #include <linux/stacktrace.h> 69d85f50d5SNeil Horman #include <linux/resource.h> 705096add8SKees Cook #include <linux/module.h> 711da177e4SLinus Torvalds #include <linux/mount.h> 721da177e4SLinus Torvalds #include <linux/security.h> 731da177e4SLinus Torvalds #include <linux/ptrace.h> 740d094efeSRoland McGrath #include <linux/tracehook.h> 75a424316cSPaul Menage #include <linux/cgroup.h> 761da177e4SLinus Torvalds #include <linux/cpuset.h> 771da177e4SLinus Torvalds #include <linux/audit.h> 785addc5ddSAl Viro #include <linux/poll.h> 791651e14eSSerge E. Hallyn #include <linux/nsproxy.h> 808ac773b4SAlexey Dobriyan #include <linux/oom.h> 813cb4a0bbSKawai, Hidehiro #include <linux/elf.h> 8260347f67SPavel Emelyanov #include <linux/pid_namespace.h> 831da177e4SLinus Torvalds #include "internal.h" 841da177e4SLinus Torvalds 850f2fe20fSEric W. Biederman /* NOTE: 860f2fe20fSEric W. Biederman * Implementing inode permission operations in /proc is almost 870f2fe20fSEric W. Biederman * certainly an error. Permission checks need to happen during 880f2fe20fSEric W. Biederman * each system call not at open time. The reason is that most of 890f2fe20fSEric W. Biederman * what we wish to check for permissions in /proc varies at runtime. 900f2fe20fSEric W. Biederman * 910f2fe20fSEric W. Biederman * The classic example of a problem is opening file descriptors 920f2fe20fSEric W. Biederman * in /proc for a task before it execs a suid executable. 930f2fe20fSEric W. Biederman */ 940f2fe20fSEric W. Biederman 951da177e4SLinus Torvalds struct pid_entry { 961da177e4SLinus Torvalds char *name; 97c5141e6dSEric Dumazet int len; 981da177e4SLinus Torvalds mode_t mode; 99c5ef1c42SArjan van de Ven const struct inode_operations *iop; 10000977a59SArjan van de Ven const struct file_operations *fop; 10120cdc894SEric W. Biederman union proc_op op; 1021da177e4SLinus Torvalds }; 1031da177e4SLinus Torvalds 10461a28784SEric W. Biederman #define NOD(NAME, MODE, IOP, FOP, OP) { \ 10520cdc894SEric W. Biederman .name = (NAME), \ 106c5141e6dSEric Dumazet .len = sizeof(NAME) - 1, \ 10720cdc894SEric W. Biederman .mode = MODE, \ 10820cdc894SEric W. Biederman .iop = IOP, \ 10920cdc894SEric W. Biederman .fop = FOP, \ 11020cdc894SEric W. Biederman .op = OP, \ 11120cdc894SEric W. Biederman } 11220cdc894SEric W. Biederman 113631f9c18SAlexey Dobriyan #define DIR(NAME, MODE, iops, fops) \ 114631f9c18SAlexey Dobriyan NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} ) 115631f9c18SAlexey Dobriyan #define LNK(NAME, get_link) \ 11661a28784SEric W. Biederman NOD(NAME, (S_IFLNK|S_IRWXUGO), \ 11720cdc894SEric W. Biederman &proc_pid_link_inode_operations, NULL, \ 118631f9c18SAlexey Dobriyan { .proc_get_link = get_link } ) 119631f9c18SAlexey Dobriyan #define REG(NAME, MODE, fops) \ 120631f9c18SAlexey Dobriyan NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {}) 121631f9c18SAlexey Dobriyan #define INF(NAME, MODE, read) \ 12261a28784SEric W. Biederman NOD(NAME, (S_IFREG|(MODE)), \ 12320cdc894SEric W. Biederman NULL, &proc_info_file_operations, \ 124631f9c18SAlexey Dobriyan { .proc_read = read } ) 125631f9c18SAlexey Dobriyan #define ONE(NAME, MODE, show) \ 126be614086SEric W. Biederman NOD(NAME, (S_IFREG|(MODE)), \ 127be614086SEric W. Biederman NULL, &proc_single_file_operations, \ 128631f9c18SAlexey Dobriyan { .proc_show = show } ) 1291da177e4SLinus Torvalds 130aed54175SVegard Nossum /* 131aed54175SVegard Nossum * Count the number of hardlinks for the pid_entry table, excluding the . 132aed54175SVegard Nossum * and .. links. 133aed54175SVegard Nossum */ 134aed54175SVegard Nossum static unsigned int pid_entry_count_dirs(const struct pid_entry *entries, 135aed54175SVegard Nossum unsigned int n) 136aed54175SVegard Nossum { 137aed54175SVegard Nossum unsigned int i; 138aed54175SVegard Nossum unsigned int count; 139aed54175SVegard Nossum 140aed54175SVegard Nossum count = 0; 141aed54175SVegard Nossum for (i = 0; i < n; ++i) { 142aed54175SVegard Nossum if (S_ISDIR(entries[i].mode)) 143aed54175SVegard Nossum ++count; 144aed54175SVegard Nossum } 145aed54175SVegard Nossum 146aed54175SVegard Nossum return count; 147aed54175SVegard Nossum } 148aed54175SVegard Nossum 149*7c2c7d99SHugh Dickins static int get_fs_path(struct task_struct *task, struct path *path, bool root) 1501da177e4SLinus Torvalds { 1511da177e4SLinus Torvalds struct fs_struct *fs; 152*7c2c7d99SHugh Dickins int result = -ENOENT; 153*7c2c7d99SHugh Dickins 1540494f6ecSMiklos Szeredi task_lock(task); 1550494f6ecSMiklos Szeredi fs = task->fs; 156*7c2c7d99SHugh Dickins if (fs) { 157*7c2c7d99SHugh Dickins read_lock(&fs->lock); 158*7c2c7d99SHugh Dickins *path = root ? fs->root : fs->pwd; 159*7c2c7d99SHugh Dickins path_get(path); 160*7c2c7d99SHugh Dickins read_unlock(&fs->lock); 161*7c2c7d99SHugh Dickins result = 0; 162*7c2c7d99SHugh Dickins } 1630494f6ecSMiklos Szeredi task_unlock(task); 164*7c2c7d99SHugh Dickins return result; 1650494f6ecSMiklos Szeredi } 1660494f6ecSMiklos Szeredi 16799f89551SEric W. Biederman static int get_nr_threads(struct task_struct *tsk) 16899f89551SEric W. Biederman { 16999f89551SEric W. Biederman unsigned long flags; 17099f89551SEric W. Biederman int count = 0; 17199f89551SEric W. Biederman 17299f89551SEric W. Biederman if (lock_task_sighand(tsk, &flags)) { 17399f89551SEric W. Biederman count = atomic_read(&tsk->signal->count); 17499f89551SEric W. Biederman unlock_task_sighand(tsk, &flags); 17599f89551SEric W. Biederman } 17699f89551SEric W. Biederman return count; 17799f89551SEric W. Biederman } 17899f89551SEric W. Biederman 1793dcd25f3SJan Blunck static int proc_cwd_link(struct inode *inode, struct path *path) 1800494f6ecSMiklos Szeredi { 18199f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 1820494f6ecSMiklos Szeredi int result = -ENOENT; 18399f89551SEric W. Biederman 18499f89551SEric W. Biederman if (task) { 185*7c2c7d99SHugh Dickins result = get_fs_path(task, path, 0); 18699f89551SEric W. Biederman put_task_struct(task); 18799f89551SEric W. Biederman } 1881da177e4SLinus Torvalds return result; 1891da177e4SLinus Torvalds } 1901da177e4SLinus Torvalds 1913dcd25f3SJan Blunck static int proc_root_link(struct inode *inode, struct path *path) 1921da177e4SLinus Torvalds { 19399f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 1941da177e4SLinus Torvalds int result = -ENOENT; 19599f89551SEric W. Biederman 19699f89551SEric W. Biederman if (task) { 197*7c2c7d99SHugh Dickins result = get_fs_path(task, path, 1); 19899f89551SEric W. Biederman put_task_struct(task); 19999f89551SEric W. Biederman } 2001da177e4SLinus Torvalds return result; 2011da177e4SLinus Torvalds } 2021da177e4SLinus Torvalds 203638fa202SRoland McGrath /* 204638fa202SRoland McGrath * Return zero if current may access user memory in @task, -error if not. 205638fa202SRoland McGrath */ 206638fa202SRoland McGrath static int check_mem_permission(struct task_struct *task) 207638fa202SRoland McGrath { 208638fa202SRoland McGrath /* 209638fa202SRoland McGrath * A task can always look at itself, in case it chooses 210638fa202SRoland McGrath * to use system calls instead of load instructions. 211638fa202SRoland McGrath */ 212638fa202SRoland McGrath if (task == current) 213638fa202SRoland McGrath return 0; 214638fa202SRoland McGrath 215638fa202SRoland McGrath /* 216638fa202SRoland McGrath * If current is actively ptrace'ing, and would also be 217638fa202SRoland McGrath * permitted to freshly attach with ptrace now, permit it. 218638fa202SRoland McGrath */ 2190d094efeSRoland McGrath if (task_is_stopped_or_traced(task)) { 2200d094efeSRoland McGrath int match; 2210d094efeSRoland McGrath rcu_read_lock(); 2220d094efeSRoland McGrath match = (tracehook_tracer_task(task) == current); 2230d094efeSRoland McGrath rcu_read_unlock(); 2240d094efeSRoland McGrath if (match && ptrace_may_access(task, PTRACE_MODE_ATTACH)) 225638fa202SRoland McGrath return 0; 2260d094efeSRoland McGrath } 227638fa202SRoland McGrath 228638fa202SRoland McGrath /* 229638fa202SRoland McGrath * Noone else is allowed. 230638fa202SRoland McGrath */ 231638fa202SRoland McGrath return -EPERM; 232638fa202SRoland McGrath } 2331da177e4SLinus Torvalds 234831830b5SAl Viro struct mm_struct *mm_for_maps(struct task_struct *task) 235831830b5SAl Viro { 236831830b5SAl Viro struct mm_struct *mm = get_task_mm(task); 237831830b5SAl Viro if (!mm) 238831830b5SAl Viro return NULL; 239831830b5SAl Viro down_read(&mm->mmap_sem); 240831830b5SAl Viro task_lock(task); 241831830b5SAl Viro if (task->mm != mm) 242831830b5SAl Viro goto out; 243006ebb40SStephen Smalley if (task->mm != current->mm && 244006ebb40SStephen Smalley __ptrace_may_access(task, PTRACE_MODE_READ) < 0) 245831830b5SAl Viro goto out; 246831830b5SAl Viro task_unlock(task); 247831830b5SAl Viro return mm; 248831830b5SAl Viro out: 249831830b5SAl Viro task_unlock(task); 250831830b5SAl Viro up_read(&mm->mmap_sem); 251831830b5SAl Viro mmput(mm); 252831830b5SAl Viro return NULL; 253831830b5SAl Viro } 254831830b5SAl Viro 2551da177e4SLinus Torvalds static int proc_pid_cmdline(struct task_struct *task, char * buffer) 2561da177e4SLinus Torvalds { 2571da177e4SLinus Torvalds int res = 0; 2581da177e4SLinus Torvalds unsigned int len; 2591da177e4SLinus Torvalds struct mm_struct *mm = get_task_mm(task); 2601da177e4SLinus Torvalds if (!mm) 2611da177e4SLinus Torvalds goto out; 2621da177e4SLinus Torvalds if (!mm->arg_end) 2631da177e4SLinus Torvalds goto out_mm; /* Shh! No looking before we're done */ 2641da177e4SLinus Torvalds 2651da177e4SLinus Torvalds len = mm->arg_end - mm->arg_start; 2661da177e4SLinus Torvalds 2671da177e4SLinus Torvalds if (len > PAGE_SIZE) 2681da177e4SLinus Torvalds len = PAGE_SIZE; 2691da177e4SLinus Torvalds 2701da177e4SLinus Torvalds res = access_process_vm(task, mm->arg_start, buffer, len, 0); 2711da177e4SLinus Torvalds 2721da177e4SLinus Torvalds // If the nul at the end of args has been overwritten, then 2731da177e4SLinus Torvalds // assume application is using setproctitle(3). 2741da177e4SLinus Torvalds if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) { 2751da177e4SLinus Torvalds len = strnlen(buffer, res); 2761da177e4SLinus Torvalds if (len < res) { 2771da177e4SLinus Torvalds res = len; 2781da177e4SLinus Torvalds } else { 2791da177e4SLinus Torvalds len = mm->env_end - mm->env_start; 2801da177e4SLinus Torvalds if (len > PAGE_SIZE - res) 2811da177e4SLinus Torvalds len = PAGE_SIZE - res; 2821da177e4SLinus Torvalds res += access_process_vm(task, mm->env_start, buffer+res, len, 0); 2831da177e4SLinus Torvalds res = strnlen(buffer, res); 2841da177e4SLinus Torvalds } 2851da177e4SLinus Torvalds } 2861da177e4SLinus Torvalds out_mm: 2871da177e4SLinus Torvalds mmput(mm); 2881da177e4SLinus Torvalds out: 2891da177e4SLinus Torvalds return res; 2901da177e4SLinus Torvalds } 2911da177e4SLinus Torvalds 2921da177e4SLinus Torvalds static int proc_pid_auxv(struct task_struct *task, char *buffer) 2931da177e4SLinus Torvalds { 2941da177e4SLinus Torvalds int res = 0; 2951da177e4SLinus Torvalds struct mm_struct *mm = get_task_mm(task); 2961da177e4SLinus Torvalds if (mm) { 2971da177e4SLinus Torvalds unsigned int nwords = 0; 298dfe6b7d9SHannes Eder do { 2991da177e4SLinus Torvalds nwords += 2; 300dfe6b7d9SHannes Eder } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */ 3011da177e4SLinus Torvalds res = nwords * sizeof(mm->saved_auxv[0]); 3021da177e4SLinus Torvalds if (res > PAGE_SIZE) 3031da177e4SLinus Torvalds res = PAGE_SIZE; 3041da177e4SLinus Torvalds memcpy(buffer, mm->saved_auxv, res); 3051da177e4SLinus Torvalds mmput(mm); 3061da177e4SLinus Torvalds } 3071da177e4SLinus Torvalds return res; 3081da177e4SLinus Torvalds } 3091da177e4SLinus Torvalds 3101da177e4SLinus Torvalds 3111da177e4SLinus Torvalds #ifdef CONFIG_KALLSYMS 3121da177e4SLinus Torvalds /* 3131da177e4SLinus Torvalds * Provides a wchan file via kallsyms in a proper one-value-per-file format. 3141da177e4SLinus Torvalds * Returns the resolved symbol. If that fails, simply return the address. 3151da177e4SLinus Torvalds */ 3161da177e4SLinus Torvalds static int proc_pid_wchan(struct task_struct *task, char *buffer) 3171da177e4SLinus Torvalds { 318ffb45122SAlexey Dobriyan unsigned long wchan; 3199281aceaSTejun Heo char symname[KSYM_NAME_LEN]; 3201da177e4SLinus Torvalds 3211da177e4SLinus Torvalds wchan = get_wchan(task); 3221da177e4SLinus Torvalds 3239d65cb4aSAlexey Dobriyan if (lookup_symbol_name(wchan, symname) < 0) 3241da177e4SLinus Torvalds return sprintf(buffer, "%lu", wchan); 3259d65cb4aSAlexey Dobriyan else 3269d65cb4aSAlexey Dobriyan return sprintf(buffer, "%s", symname); 3271da177e4SLinus Torvalds } 3281da177e4SLinus Torvalds #endif /* CONFIG_KALLSYMS */ 3291da177e4SLinus Torvalds 3302ec220e2SKen Chen #ifdef CONFIG_STACKTRACE 3312ec220e2SKen Chen 3322ec220e2SKen Chen #define MAX_STACK_TRACE_DEPTH 64 3332ec220e2SKen Chen 3342ec220e2SKen Chen static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns, 3352ec220e2SKen Chen struct pid *pid, struct task_struct *task) 3362ec220e2SKen Chen { 3372ec220e2SKen Chen struct stack_trace trace; 3382ec220e2SKen Chen unsigned long *entries; 3392ec220e2SKen Chen int i; 3402ec220e2SKen Chen 3412ec220e2SKen Chen entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL); 3422ec220e2SKen Chen if (!entries) 3432ec220e2SKen Chen return -ENOMEM; 3442ec220e2SKen Chen 3452ec220e2SKen Chen trace.nr_entries = 0; 3462ec220e2SKen Chen trace.max_entries = MAX_STACK_TRACE_DEPTH; 3472ec220e2SKen Chen trace.entries = entries; 3482ec220e2SKen Chen trace.skip = 0; 3492ec220e2SKen Chen save_stack_trace_tsk(task, &trace); 3502ec220e2SKen Chen 3512ec220e2SKen Chen for (i = 0; i < trace.nr_entries; i++) { 3522ec220e2SKen Chen seq_printf(m, "[<%p>] %pS\n", 3532ec220e2SKen Chen (void *)entries[i], (void *)entries[i]); 3542ec220e2SKen Chen } 3552ec220e2SKen Chen kfree(entries); 3562ec220e2SKen Chen 3572ec220e2SKen Chen return 0; 3582ec220e2SKen Chen } 3592ec220e2SKen Chen #endif 3602ec220e2SKen Chen 3611da177e4SLinus Torvalds #ifdef CONFIG_SCHEDSTATS 3621da177e4SLinus Torvalds /* 3631da177e4SLinus Torvalds * Provides /proc/PID/schedstat 3641da177e4SLinus Torvalds */ 3651da177e4SLinus Torvalds static int proc_pid_schedstat(struct task_struct *task, char *buffer) 3661da177e4SLinus Torvalds { 367172ba844SBalbir Singh return sprintf(buffer, "%llu %llu %lu\n", 368826e08b0SIngo Molnar (unsigned long long)task->se.sum_exec_runtime, 369826e08b0SIngo Molnar (unsigned long long)task->sched_info.run_delay, 3702d72376bSIngo Molnar task->sched_info.pcount); 3711da177e4SLinus Torvalds } 3721da177e4SLinus Torvalds #endif 3731da177e4SLinus Torvalds 3749745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP 3759745512cSArjan van de Ven static int lstats_show_proc(struct seq_file *m, void *v) 3769745512cSArjan van de Ven { 3779745512cSArjan van de Ven int i; 37813d77c37SHiroshi Shimamoto struct inode *inode = m->private; 37913d77c37SHiroshi Shimamoto struct task_struct *task = get_proc_task(inode); 3809745512cSArjan van de Ven 38113d77c37SHiroshi Shimamoto if (!task) 38213d77c37SHiroshi Shimamoto return -ESRCH; 38313d77c37SHiroshi Shimamoto seq_puts(m, "Latency Top version : v0.1\n"); 3849745512cSArjan van de Ven for (i = 0; i < 32; i++) { 3859745512cSArjan van de Ven if (task->latency_record[i].backtrace[0]) { 3869745512cSArjan van de Ven int q; 3879745512cSArjan van de Ven seq_printf(m, "%i %li %li ", 3889745512cSArjan van de Ven task->latency_record[i].count, 3899745512cSArjan van de Ven task->latency_record[i].time, 3909745512cSArjan van de Ven task->latency_record[i].max); 3919745512cSArjan van de Ven for (q = 0; q < LT_BACKTRACEDEPTH; q++) { 3929c246247SHugh Dickins char sym[KSYM_SYMBOL_LEN]; 3939745512cSArjan van de Ven char *c; 3949745512cSArjan van de Ven if (!task->latency_record[i].backtrace[q]) 3959745512cSArjan van de Ven break; 3969745512cSArjan van de Ven if (task->latency_record[i].backtrace[q] == ULONG_MAX) 3979745512cSArjan van de Ven break; 3989745512cSArjan van de Ven sprint_symbol(sym, task->latency_record[i].backtrace[q]); 3999745512cSArjan van de Ven c = strchr(sym, '+'); 4009745512cSArjan van de Ven if (c) 4019745512cSArjan van de Ven *c = 0; 4029745512cSArjan van de Ven seq_printf(m, "%s ", sym); 4039745512cSArjan van de Ven } 4049745512cSArjan van de Ven seq_printf(m, "\n"); 4059745512cSArjan van de Ven } 4069745512cSArjan van de Ven 4079745512cSArjan van de Ven } 40813d77c37SHiroshi Shimamoto put_task_struct(task); 4099745512cSArjan van de Ven return 0; 4109745512cSArjan van de Ven } 4119745512cSArjan van de Ven 4129745512cSArjan van de Ven static int lstats_open(struct inode *inode, struct file *file) 4139745512cSArjan van de Ven { 41413d77c37SHiroshi Shimamoto return single_open(file, lstats_show_proc, inode); 415d6643d12SHiroshi Shimamoto } 416d6643d12SHiroshi Shimamoto 4179745512cSArjan van de Ven static ssize_t lstats_write(struct file *file, const char __user *buf, 4189745512cSArjan van de Ven size_t count, loff_t *offs) 4199745512cSArjan van de Ven { 42013d77c37SHiroshi Shimamoto struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 4219745512cSArjan van de Ven 42213d77c37SHiroshi Shimamoto if (!task) 42313d77c37SHiroshi Shimamoto return -ESRCH; 4249745512cSArjan van de Ven clear_all_latency_tracing(task); 42513d77c37SHiroshi Shimamoto put_task_struct(task); 4269745512cSArjan van de Ven 4279745512cSArjan van de Ven return count; 4289745512cSArjan van de Ven } 4299745512cSArjan van de Ven 4309745512cSArjan van de Ven static const struct file_operations proc_lstats_operations = { 4319745512cSArjan van de Ven .open = lstats_open, 4329745512cSArjan van de Ven .read = seq_read, 4339745512cSArjan van de Ven .write = lstats_write, 4349745512cSArjan van de Ven .llseek = seq_lseek, 43513d77c37SHiroshi Shimamoto .release = single_release, 4369745512cSArjan van de Ven }; 4379745512cSArjan van de Ven 4389745512cSArjan van de Ven #endif 4399745512cSArjan van de Ven 4401da177e4SLinus Torvalds /* The badness from the OOM killer */ 4411da177e4SLinus Torvalds unsigned long badness(struct task_struct *p, unsigned long uptime); 4421da177e4SLinus Torvalds static int proc_oom_score(struct task_struct *task, char *buffer) 4431da177e4SLinus Torvalds { 4441da177e4SLinus Torvalds unsigned long points; 4451da177e4SLinus Torvalds struct timespec uptime; 4461da177e4SLinus Torvalds 4471da177e4SLinus Torvalds do_posix_clock_monotonic_gettime(&uptime); 44819c5d45aSAlexey Dobriyan read_lock(&tasklist_lock); 4491da177e4SLinus Torvalds points = badness(task, uptime.tv_sec); 45019c5d45aSAlexey Dobriyan read_unlock(&tasklist_lock); 4511da177e4SLinus Torvalds return sprintf(buffer, "%lu\n", points); 4521da177e4SLinus Torvalds } 4531da177e4SLinus Torvalds 454d85f50d5SNeil Horman struct limit_names { 455d85f50d5SNeil Horman char *name; 456d85f50d5SNeil Horman char *unit; 457d85f50d5SNeil Horman }; 458d85f50d5SNeil Horman 459d85f50d5SNeil Horman static const struct limit_names lnames[RLIM_NLIMITS] = { 460d85f50d5SNeil Horman [RLIMIT_CPU] = {"Max cpu time", "ms"}, 461d85f50d5SNeil Horman [RLIMIT_FSIZE] = {"Max file size", "bytes"}, 462d85f50d5SNeil Horman [RLIMIT_DATA] = {"Max data size", "bytes"}, 463d85f50d5SNeil Horman [RLIMIT_STACK] = {"Max stack size", "bytes"}, 464d85f50d5SNeil Horman [RLIMIT_CORE] = {"Max core file size", "bytes"}, 465d85f50d5SNeil Horman [RLIMIT_RSS] = {"Max resident set", "bytes"}, 466d85f50d5SNeil Horman [RLIMIT_NPROC] = {"Max processes", "processes"}, 467d85f50d5SNeil Horman [RLIMIT_NOFILE] = {"Max open files", "files"}, 468d85f50d5SNeil Horman [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"}, 469d85f50d5SNeil Horman [RLIMIT_AS] = {"Max address space", "bytes"}, 470d85f50d5SNeil Horman [RLIMIT_LOCKS] = {"Max file locks", "locks"}, 471d85f50d5SNeil Horman [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"}, 472d85f50d5SNeil Horman [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"}, 473d85f50d5SNeil Horman [RLIMIT_NICE] = {"Max nice priority", NULL}, 474d85f50d5SNeil Horman [RLIMIT_RTPRIO] = {"Max realtime priority", NULL}, 4758808117cSEugene Teo [RLIMIT_RTTIME] = {"Max realtime timeout", "us"}, 476d85f50d5SNeil Horman }; 477d85f50d5SNeil Horman 478d85f50d5SNeil Horman /* Display limits for a process */ 479d85f50d5SNeil Horman static int proc_pid_limits(struct task_struct *task, char *buffer) 480d85f50d5SNeil Horman { 481d85f50d5SNeil Horman unsigned int i; 482d85f50d5SNeil Horman int count = 0; 483d85f50d5SNeil Horman unsigned long flags; 484d85f50d5SNeil Horman char *bufptr = buffer; 485d85f50d5SNeil Horman 486d85f50d5SNeil Horman struct rlimit rlim[RLIM_NLIMITS]; 487d85f50d5SNeil Horman 488a6bebbc8SLai Jiangshan if (!lock_task_sighand(task, &flags)) 489d85f50d5SNeil Horman return 0; 490d85f50d5SNeil Horman memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS); 491d85f50d5SNeil Horman unlock_task_sighand(task, &flags); 492d85f50d5SNeil Horman 493d85f50d5SNeil Horman /* 494d85f50d5SNeil Horman * print the file header 495d85f50d5SNeil Horman */ 496d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n", 497d85f50d5SNeil Horman "Limit", "Soft Limit", "Hard Limit", "Units"); 498d85f50d5SNeil Horman 499d85f50d5SNeil Horman for (i = 0; i < RLIM_NLIMITS; i++) { 500d85f50d5SNeil Horman if (rlim[i].rlim_cur == RLIM_INFINITY) 501d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-25s %-20s ", 502d85f50d5SNeil Horman lnames[i].name, "unlimited"); 503d85f50d5SNeil Horman else 504d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-25s %-20lu ", 505d85f50d5SNeil Horman lnames[i].name, rlim[i].rlim_cur); 506d85f50d5SNeil Horman 507d85f50d5SNeil Horman if (rlim[i].rlim_max == RLIM_INFINITY) 508d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-20s ", "unlimited"); 509d85f50d5SNeil Horman else 510d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-20lu ", 511d85f50d5SNeil Horman rlim[i].rlim_max); 512d85f50d5SNeil Horman 513d85f50d5SNeil Horman if (lnames[i].unit) 514d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-10s\n", 515d85f50d5SNeil Horman lnames[i].unit); 516d85f50d5SNeil Horman else 517d85f50d5SNeil Horman count += sprintf(&bufptr[count], "\n"); 518d85f50d5SNeil Horman } 519d85f50d5SNeil Horman 520d85f50d5SNeil Horman return count; 521d85f50d5SNeil Horman } 522d85f50d5SNeil Horman 523ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK 524ebcb6734SRoland McGrath static int proc_pid_syscall(struct task_struct *task, char *buffer) 525ebcb6734SRoland McGrath { 526ebcb6734SRoland McGrath long nr; 527ebcb6734SRoland McGrath unsigned long args[6], sp, pc; 528ebcb6734SRoland McGrath 529ebcb6734SRoland McGrath if (task_current_syscall(task, &nr, args, 6, &sp, &pc)) 530ebcb6734SRoland McGrath return sprintf(buffer, "running\n"); 531ebcb6734SRoland McGrath 532ebcb6734SRoland McGrath if (nr < 0) 533ebcb6734SRoland McGrath return sprintf(buffer, "%ld 0x%lx 0x%lx\n", nr, sp, pc); 534ebcb6734SRoland McGrath 535ebcb6734SRoland McGrath return sprintf(buffer, 536ebcb6734SRoland McGrath "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n", 537ebcb6734SRoland McGrath nr, 538ebcb6734SRoland McGrath args[0], args[1], args[2], args[3], args[4], args[5], 539ebcb6734SRoland McGrath sp, pc); 540ebcb6734SRoland McGrath } 541ebcb6734SRoland McGrath #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */ 542ebcb6734SRoland McGrath 5431da177e4SLinus Torvalds /************************************************************************/ 5441da177e4SLinus Torvalds /* Here the fs part begins */ 5451da177e4SLinus Torvalds /************************************************************************/ 5461da177e4SLinus Torvalds 5471da177e4SLinus Torvalds /* permission checks */ 548778c1144SEric W. Biederman static int proc_fd_access_allowed(struct inode *inode) 5491da177e4SLinus Torvalds { 550778c1144SEric W. Biederman struct task_struct *task; 551778c1144SEric W. Biederman int allowed = 0; 552df26c40eSEric W. Biederman /* Allow access to a task's file descriptors if it is us or we 553df26c40eSEric W. Biederman * may use ptrace attach to the process and find out that 554df26c40eSEric W. Biederman * information. 555778c1144SEric W. Biederman */ 556778c1144SEric W. Biederman task = get_proc_task(inode); 557df26c40eSEric W. Biederman if (task) { 558006ebb40SStephen Smalley allowed = ptrace_may_access(task, PTRACE_MODE_READ); 559778c1144SEric W. Biederman put_task_struct(task); 560df26c40eSEric W. Biederman } 561778c1144SEric W. Biederman return allowed; 5621da177e4SLinus Torvalds } 5631da177e4SLinus Torvalds 5646d76fa58SLinus Torvalds static int proc_setattr(struct dentry *dentry, struct iattr *attr) 5656d76fa58SLinus Torvalds { 5666d76fa58SLinus Torvalds int error; 5676d76fa58SLinus Torvalds struct inode *inode = dentry->d_inode; 5686d76fa58SLinus Torvalds 5696d76fa58SLinus Torvalds if (attr->ia_valid & ATTR_MODE) 5706d76fa58SLinus Torvalds return -EPERM; 5716d76fa58SLinus Torvalds 5726d76fa58SLinus Torvalds error = inode_change_ok(inode, attr); 5736d76fa58SLinus Torvalds if (!error) 5746d76fa58SLinus Torvalds error = inode_setattr(inode, attr); 5756d76fa58SLinus Torvalds return error; 5766d76fa58SLinus Torvalds } 5776d76fa58SLinus Torvalds 578c5ef1c42SArjan van de Ven static const struct inode_operations proc_def_inode_operations = { 5796d76fa58SLinus Torvalds .setattr = proc_setattr, 5806d76fa58SLinus Torvalds }; 5816d76fa58SLinus Torvalds 582a1a2c409SMiklos Szeredi static int mounts_open_common(struct inode *inode, struct file *file, 583a1a2c409SMiklos Szeredi const struct seq_operations *op) 5841da177e4SLinus Torvalds { 58599f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 586cf7b708cSPavel Emelyanov struct nsproxy *nsp; 5876b3286edSKirill Korotaev struct mnt_namespace *ns = NULL; 588a1a2c409SMiklos Szeredi struct path root; 5895addc5ddSAl Viro struct proc_mounts *p; 5905addc5ddSAl Viro int ret = -EINVAL; 5915addc5ddSAl Viro 59299f89551SEric W. Biederman if (task) { 593cf7b708cSPavel Emelyanov rcu_read_lock(); 594cf7b708cSPavel Emelyanov nsp = task_nsproxy(task); 595cf7b708cSPavel Emelyanov if (nsp) { 596cf7b708cSPavel Emelyanov ns = nsp->mnt_ns; 5976b3286edSKirill Korotaev if (ns) 5986b3286edSKirill Korotaev get_mnt_ns(ns); 599863c4702SAlexey Dobriyan } 600cf7b708cSPavel Emelyanov rcu_read_unlock(); 601*7c2c7d99SHugh Dickins if (ns && get_fs_path(task, &root, 1) == 0) 602*7c2c7d99SHugh Dickins ret = 0; 60399f89551SEric W. Biederman put_task_struct(task); 60499f89551SEric W. Biederman } 6051da177e4SLinus Torvalds 606a1a2c409SMiklos Szeredi if (!ns) 607a1a2c409SMiklos Szeredi goto err; 608*7c2c7d99SHugh Dickins if (ret) 609a1a2c409SMiklos Szeredi goto err_put_ns; 610a1a2c409SMiklos Szeredi 6115addc5ddSAl Viro ret = -ENOMEM; 6125addc5ddSAl Viro p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL); 613a1a2c409SMiklos Szeredi if (!p) 614a1a2c409SMiklos Szeredi goto err_put_path; 615a1a2c409SMiklos Szeredi 6165addc5ddSAl Viro file->private_data = &p->m; 617a1a2c409SMiklos Szeredi ret = seq_open(file, op); 618a1a2c409SMiklos Szeredi if (ret) 619a1a2c409SMiklos Szeredi goto err_free; 620a1a2c409SMiklos Szeredi 621a1a2c409SMiklos Szeredi p->m.private = p; 622a1a2c409SMiklos Szeredi p->ns = ns; 623a1a2c409SMiklos Szeredi p->root = root; 6246b3286edSKirill Korotaev p->event = ns->event; 625a1a2c409SMiklos Szeredi 6265addc5ddSAl Viro return 0; 627a1a2c409SMiklos Szeredi 628a1a2c409SMiklos Szeredi err_free: 6295addc5ddSAl Viro kfree(p); 630a1a2c409SMiklos Szeredi err_put_path: 631a1a2c409SMiklos Szeredi path_put(&root); 632a1a2c409SMiklos Szeredi err_put_ns: 6336b3286edSKirill Korotaev put_mnt_ns(ns); 634a1a2c409SMiklos Szeredi err: 6351da177e4SLinus Torvalds return ret; 6361da177e4SLinus Torvalds } 6371da177e4SLinus Torvalds 6381da177e4SLinus Torvalds static int mounts_release(struct inode *inode, struct file *file) 6391da177e4SLinus Torvalds { 640a1a2c409SMiklos Szeredi struct proc_mounts *p = file->private_data; 641a1a2c409SMiklos Szeredi path_put(&p->root); 642a1a2c409SMiklos Szeredi put_mnt_ns(p->ns); 6431da177e4SLinus Torvalds return seq_release(inode, file); 6441da177e4SLinus Torvalds } 6451da177e4SLinus Torvalds 6465addc5ddSAl Viro static unsigned mounts_poll(struct file *file, poll_table *wait) 6475addc5ddSAl Viro { 6485addc5ddSAl Viro struct proc_mounts *p = file->private_data; 649a1a2c409SMiklos Szeredi struct mnt_namespace *ns = p->ns; 6505addc5ddSAl Viro unsigned res = 0; 6515addc5ddSAl Viro 6525addc5ddSAl Viro poll_wait(file, &ns->poll, wait); 6535addc5ddSAl Viro 6545addc5ddSAl Viro spin_lock(&vfsmount_lock); 6555addc5ddSAl Viro if (p->event != ns->event) { 6565addc5ddSAl Viro p->event = ns->event; 6575addc5ddSAl Viro res = POLLERR; 6585addc5ddSAl Viro } 6595addc5ddSAl Viro spin_unlock(&vfsmount_lock); 6605addc5ddSAl Viro 6615addc5ddSAl Viro return res; 6625addc5ddSAl Viro } 6635addc5ddSAl Viro 664a1a2c409SMiklos Szeredi static int mounts_open(struct inode *inode, struct file *file) 665a1a2c409SMiklos Szeredi { 666a1a2c409SMiklos Szeredi return mounts_open_common(inode, file, &mounts_op); 667a1a2c409SMiklos Szeredi } 668a1a2c409SMiklos Szeredi 66900977a59SArjan van de Ven static const struct file_operations proc_mounts_operations = { 6701da177e4SLinus Torvalds .open = mounts_open, 6711da177e4SLinus Torvalds .read = seq_read, 6721da177e4SLinus Torvalds .llseek = seq_lseek, 6731da177e4SLinus Torvalds .release = mounts_release, 6745addc5ddSAl Viro .poll = mounts_poll, 6751da177e4SLinus Torvalds }; 6761da177e4SLinus Torvalds 6772d4d4864SRam Pai static int mountinfo_open(struct inode *inode, struct file *file) 6782d4d4864SRam Pai { 6792d4d4864SRam Pai return mounts_open_common(inode, file, &mountinfo_op); 6802d4d4864SRam Pai } 6812d4d4864SRam Pai 6822d4d4864SRam Pai static const struct file_operations proc_mountinfo_operations = { 6832d4d4864SRam Pai .open = mountinfo_open, 6842d4d4864SRam Pai .read = seq_read, 6852d4d4864SRam Pai .llseek = seq_lseek, 6862d4d4864SRam Pai .release = mounts_release, 6872d4d4864SRam Pai .poll = mounts_poll, 6882d4d4864SRam Pai }; 6892d4d4864SRam Pai 690b4629fe2SChuck Lever static int mountstats_open(struct inode *inode, struct file *file) 691b4629fe2SChuck Lever { 692a1a2c409SMiklos Szeredi return mounts_open_common(inode, file, &mountstats_op); 693b4629fe2SChuck Lever } 694b4629fe2SChuck Lever 69500977a59SArjan van de Ven static const struct file_operations proc_mountstats_operations = { 696b4629fe2SChuck Lever .open = mountstats_open, 697b4629fe2SChuck Lever .read = seq_read, 698b4629fe2SChuck Lever .llseek = seq_lseek, 699b4629fe2SChuck Lever .release = mounts_release, 700b4629fe2SChuck Lever }; 701b4629fe2SChuck Lever 7021da177e4SLinus Torvalds #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */ 7031da177e4SLinus Torvalds 7041da177e4SLinus Torvalds static ssize_t proc_info_read(struct file * file, char __user * buf, 7051da177e4SLinus Torvalds size_t count, loff_t *ppos) 7061da177e4SLinus Torvalds { 7072fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 7081da177e4SLinus Torvalds unsigned long page; 7091da177e4SLinus Torvalds ssize_t length; 71099f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 71199f89551SEric W. Biederman 71299f89551SEric W. Biederman length = -ESRCH; 71399f89551SEric W. Biederman if (!task) 71499f89551SEric W. Biederman goto out_no_task; 7151da177e4SLinus Torvalds 7161da177e4SLinus Torvalds if (count > PROC_BLOCK_SIZE) 7171da177e4SLinus Torvalds count = PROC_BLOCK_SIZE; 71899f89551SEric W. Biederman 71999f89551SEric W. Biederman length = -ENOMEM; 720e12ba74dSMel Gorman if (!(page = __get_free_page(GFP_TEMPORARY))) 72199f89551SEric W. Biederman goto out; 7221da177e4SLinus Torvalds 7231da177e4SLinus Torvalds length = PROC_I(inode)->op.proc_read(task, (char*)page); 7241da177e4SLinus Torvalds 7251da177e4SLinus Torvalds if (length >= 0) 7261da177e4SLinus Torvalds length = simple_read_from_buffer(buf, count, ppos, (char *)page, length); 7271da177e4SLinus Torvalds free_page(page); 72899f89551SEric W. Biederman out: 72999f89551SEric W. Biederman put_task_struct(task); 73099f89551SEric W. Biederman out_no_task: 7311da177e4SLinus Torvalds return length; 7321da177e4SLinus Torvalds } 7331da177e4SLinus Torvalds 73400977a59SArjan van de Ven static const struct file_operations proc_info_file_operations = { 7351da177e4SLinus Torvalds .read = proc_info_read, 7361da177e4SLinus Torvalds }; 7371da177e4SLinus Torvalds 738be614086SEric W. Biederman static int proc_single_show(struct seq_file *m, void *v) 739be614086SEric W. Biederman { 740be614086SEric W. Biederman struct inode *inode = m->private; 741be614086SEric W. Biederman struct pid_namespace *ns; 742be614086SEric W. Biederman struct pid *pid; 743be614086SEric W. Biederman struct task_struct *task; 744be614086SEric W. Biederman int ret; 745be614086SEric W. Biederman 746be614086SEric W. Biederman ns = inode->i_sb->s_fs_info; 747be614086SEric W. Biederman pid = proc_pid(inode); 748be614086SEric W. Biederman task = get_pid_task(pid, PIDTYPE_PID); 749be614086SEric W. Biederman if (!task) 750be614086SEric W. Biederman return -ESRCH; 751be614086SEric W. Biederman 752be614086SEric W. Biederman ret = PROC_I(inode)->op.proc_show(m, ns, pid, task); 753be614086SEric W. Biederman 754be614086SEric W. Biederman put_task_struct(task); 755be614086SEric W. Biederman return ret; 756be614086SEric W. Biederman } 757be614086SEric W. Biederman 758be614086SEric W. Biederman static int proc_single_open(struct inode *inode, struct file *filp) 759be614086SEric W. Biederman { 760be614086SEric W. Biederman int ret; 761be614086SEric W. Biederman ret = single_open(filp, proc_single_show, NULL); 762be614086SEric W. Biederman if (!ret) { 763be614086SEric W. Biederman struct seq_file *m = filp->private_data; 764be614086SEric W. Biederman 765be614086SEric W. Biederman m->private = inode; 766be614086SEric W. Biederman } 767be614086SEric W. Biederman return ret; 768be614086SEric W. Biederman } 769be614086SEric W. Biederman 770be614086SEric W. Biederman static const struct file_operations proc_single_file_operations = { 771be614086SEric W. Biederman .open = proc_single_open, 772be614086SEric W. Biederman .read = seq_read, 773be614086SEric W. Biederman .llseek = seq_lseek, 774be614086SEric W. Biederman .release = single_release, 775be614086SEric W. Biederman }; 776be614086SEric W. Biederman 7771da177e4SLinus Torvalds static int mem_open(struct inode* inode, struct file* file) 7781da177e4SLinus Torvalds { 7791da177e4SLinus Torvalds file->private_data = (void*)((long)current->self_exec_id); 7801da177e4SLinus Torvalds return 0; 7811da177e4SLinus Torvalds } 7821da177e4SLinus Torvalds 7831da177e4SLinus Torvalds static ssize_t mem_read(struct file * file, char __user * buf, 7841da177e4SLinus Torvalds size_t count, loff_t *ppos) 7851da177e4SLinus Torvalds { 7862fddfeefSJosef "Jeff" Sipek struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode); 7871da177e4SLinus Torvalds char *page; 7881da177e4SLinus Torvalds unsigned long src = *ppos; 7891da177e4SLinus Torvalds int ret = -ESRCH; 7901da177e4SLinus Torvalds struct mm_struct *mm; 7911da177e4SLinus Torvalds 79299f89551SEric W. Biederman if (!task) 79399f89551SEric W. Biederman goto out_no_task; 79499f89551SEric W. Biederman 795638fa202SRoland McGrath if (check_mem_permission(task)) 7961da177e4SLinus Torvalds goto out; 7971da177e4SLinus Torvalds 7981da177e4SLinus Torvalds ret = -ENOMEM; 799e12ba74dSMel Gorman page = (char *)__get_free_page(GFP_TEMPORARY); 8001da177e4SLinus Torvalds if (!page) 8011da177e4SLinus Torvalds goto out; 8021da177e4SLinus Torvalds 8031da177e4SLinus Torvalds ret = 0; 8041da177e4SLinus Torvalds 8051da177e4SLinus Torvalds mm = get_task_mm(task); 8061da177e4SLinus Torvalds if (!mm) 8071da177e4SLinus Torvalds goto out_free; 8081da177e4SLinus Torvalds 8091da177e4SLinus Torvalds ret = -EIO; 8101da177e4SLinus Torvalds 8111da177e4SLinus Torvalds if (file->private_data != (void*)((long)current->self_exec_id)) 8121da177e4SLinus Torvalds goto out_put; 8131da177e4SLinus Torvalds 8141da177e4SLinus Torvalds ret = 0; 8151da177e4SLinus Torvalds 8161da177e4SLinus Torvalds while (count > 0) { 8171da177e4SLinus Torvalds int this_len, retval; 8181da177e4SLinus Torvalds 8191da177e4SLinus Torvalds this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count; 8201da177e4SLinus Torvalds retval = access_process_vm(task, src, page, this_len, 0); 821638fa202SRoland McGrath if (!retval || check_mem_permission(task)) { 8221da177e4SLinus Torvalds if (!ret) 8231da177e4SLinus Torvalds ret = -EIO; 8241da177e4SLinus Torvalds break; 8251da177e4SLinus Torvalds } 8261da177e4SLinus Torvalds 8271da177e4SLinus Torvalds if (copy_to_user(buf, page, retval)) { 8281da177e4SLinus Torvalds ret = -EFAULT; 8291da177e4SLinus Torvalds break; 8301da177e4SLinus Torvalds } 8311da177e4SLinus Torvalds 8321da177e4SLinus Torvalds ret += retval; 8331da177e4SLinus Torvalds src += retval; 8341da177e4SLinus Torvalds buf += retval; 8351da177e4SLinus Torvalds count -= retval; 8361da177e4SLinus Torvalds } 8371da177e4SLinus Torvalds *ppos = src; 8381da177e4SLinus Torvalds 8391da177e4SLinus Torvalds out_put: 8401da177e4SLinus Torvalds mmput(mm); 8411da177e4SLinus Torvalds out_free: 8421da177e4SLinus Torvalds free_page((unsigned long) page); 8431da177e4SLinus Torvalds out: 84499f89551SEric W. Biederman put_task_struct(task); 84599f89551SEric W. Biederman out_no_task: 8461da177e4SLinus Torvalds return ret; 8471da177e4SLinus Torvalds } 8481da177e4SLinus Torvalds 8491da177e4SLinus Torvalds #define mem_write NULL 8501da177e4SLinus Torvalds 8511da177e4SLinus Torvalds #ifndef mem_write 8521da177e4SLinus Torvalds /* This is a security hazard */ 85363967fa9SGlauber de Oliveira Costa static ssize_t mem_write(struct file * file, const char __user *buf, 8541da177e4SLinus Torvalds size_t count, loff_t *ppos) 8551da177e4SLinus Torvalds { 856f7ca54f4SFrederik Deweerdt int copied; 8571da177e4SLinus Torvalds char *page; 8582fddfeefSJosef "Jeff" Sipek struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode); 8591da177e4SLinus Torvalds unsigned long dst = *ppos; 8601da177e4SLinus Torvalds 86199f89551SEric W. Biederman copied = -ESRCH; 86299f89551SEric W. Biederman if (!task) 86399f89551SEric W. Biederman goto out_no_task; 8641da177e4SLinus Torvalds 865638fa202SRoland McGrath if (check_mem_permission(task)) 86699f89551SEric W. Biederman goto out; 86799f89551SEric W. Biederman 86899f89551SEric W. Biederman copied = -ENOMEM; 869e12ba74dSMel Gorman page = (char *)__get_free_page(GFP_TEMPORARY); 8701da177e4SLinus Torvalds if (!page) 87199f89551SEric W. Biederman goto out; 8721da177e4SLinus Torvalds 873f7ca54f4SFrederik Deweerdt copied = 0; 8741da177e4SLinus Torvalds while (count > 0) { 8751da177e4SLinus Torvalds int this_len, retval; 8761da177e4SLinus Torvalds 8771da177e4SLinus Torvalds this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count; 8781da177e4SLinus Torvalds if (copy_from_user(page, buf, this_len)) { 8791da177e4SLinus Torvalds copied = -EFAULT; 8801da177e4SLinus Torvalds break; 8811da177e4SLinus Torvalds } 8821da177e4SLinus Torvalds retval = access_process_vm(task, dst, page, this_len, 1); 8831da177e4SLinus Torvalds if (!retval) { 8841da177e4SLinus Torvalds if (!copied) 8851da177e4SLinus Torvalds copied = -EIO; 8861da177e4SLinus Torvalds break; 8871da177e4SLinus Torvalds } 8881da177e4SLinus Torvalds copied += retval; 8891da177e4SLinus Torvalds buf += retval; 8901da177e4SLinus Torvalds dst += retval; 8911da177e4SLinus Torvalds count -= retval; 8921da177e4SLinus Torvalds } 8931da177e4SLinus Torvalds *ppos = dst; 8941da177e4SLinus Torvalds free_page((unsigned long) page); 89599f89551SEric W. Biederman out: 89699f89551SEric W. Biederman put_task_struct(task); 89799f89551SEric W. Biederman out_no_task: 8981da177e4SLinus Torvalds return copied; 8991da177e4SLinus Torvalds } 9001da177e4SLinus Torvalds #endif 9011da177e4SLinus Torvalds 90285863e47SMatt Mackall loff_t mem_lseek(struct file *file, loff_t offset, int orig) 9031da177e4SLinus Torvalds { 9041da177e4SLinus Torvalds switch (orig) { 9051da177e4SLinus Torvalds case 0: 9061da177e4SLinus Torvalds file->f_pos = offset; 9071da177e4SLinus Torvalds break; 9081da177e4SLinus Torvalds case 1: 9091da177e4SLinus Torvalds file->f_pos += offset; 9101da177e4SLinus Torvalds break; 9111da177e4SLinus Torvalds default: 9121da177e4SLinus Torvalds return -EINVAL; 9131da177e4SLinus Torvalds } 9141da177e4SLinus Torvalds force_successful_syscall_return(); 9151da177e4SLinus Torvalds return file->f_pos; 9161da177e4SLinus Torvalds } 9171da177e4SLinus Torvalds 91800977a59SArjan van de Ven static const struct file_operations proc_mem_operations = { 9191da177e4SLinus Torvalds .llseek = mem_lseek, 9201da177e4SLinus Torvalds .read = mem_read, 9211da177e4SLinus Torvalds .write = mem_write, 9221da177e4SLinus Torvalds .open = mem_open, 9231da177e4SLinus Torvalds }; 9241da177e4SLinus Torvalds 925315e28c8SJames Pearson static ssize_t environ_read(struct file *file, char __user *buf, 926315e28c8SJames Pearson size_t count, loff_t *ppos) 927315e28c8SJames Pearson { 928315e28c8SJames Pearson struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 929315e28c8SJames Pearson char *page; 930315e28c8SJames Pearson unsigned long src = *ppos; 931315e28c8SJames Pearson int ret = -ESRCH; 932315e28c8SJames Pearson struct mm_struct *mm; 933315e28c8SJames Pearson 934315e28c8SJames Pearson if (!task) 935315e28c8SJames Pearson goto out_no_task; 936315e28c8SJames Pearson 937006ebb40SStephen Smalley if (!ptrace_may_access(task, PTRACE_MODE_READ)) 938315e28c8SJames Pearson goto out; 939315e28c8SJames Pearson 940315e28c8SJames Pearson ret = -ENOMEM; 941315e28c8SJames Pearson page = (char *)__get_free_page(GFP_TEMPORARY); 942315e28c8SJames Pearson if (!page) 943315e28c8SJames Pearson goto out; 944315e28c8SJames Pearson 945315e28c8SJames Pearson ret = 0; 946315e28c8SJames Pearson 947315e28c8SJames Pearson mm = get_task_mm(task); 948315e28c8SJames Pearson if (!mm) 949315e28c8SJames Pearson goto out_free; 950315e28c8SJames Pearson 951315e28c8SJames Pearson while (count > 0) { 952315e28c8SJames Pearson int this_len, retval, max_len; 953315e28c8SJames Pearson 954315e28c8SJames Pearson this_len = mm->env_end - (mm->env_start + src); 955315e28c8SJames Pearson 956315e28c8SJames Pearson if (this_len <= 0) 957315e28c8SJames Pearson break; 958315e28c8SJames Pearson 959315e28c8SJames Pearson max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count; 960315e28c8SJames Pearson this_len = (this_len > max_len) ? max_len : this_len; 961315e28c8SJames Pearson 962315e28c8SJames Pearson retval = access_process_vm(task, (mm->env_start + src), 963315e28c8SJames Pearson page, this_len, 0); 964315e28c8SJames Pearson 965315e28c8SJames Pearson if (retval <= 0) { 966315e28c8SJames Pearson ret = retval; 967315e28c8SJames Pearson break; 968315e28c8SJames Pearson } 969315e28c8SJames Pearson 970315e28c8SJames Pearson if (copy_to_user(buf, page, retval)) { 971315e28c8SJames Pearson ret = -EFAULT; 972315e28c8SJames Pearson break; 973315e28c8SJames Pearson } 974315e28c8SJames Pearson 975315e28c8SJames Pearson ret += retval; 976315e28c8SJames Pearson src += retval; 977315e28c8SJames Pearson buf += retval; 978315e28c8SJames Pearson count -= retval; 979315e28c8SJames Pearson } 980315e28c8SJames Pearson *ppos = src; 981315e28c8SJames Pearson 982315e28c8SJames Pearson mmput(mm); 983315e28c8SJames Pearson out_free: 984315e28c8SJames Pearson free_page((unsigned long) page); 985315e28c8SJames Pearson out: 986315e28c8SJames Pearson put_task_struct(task); 987315e28c8SJames Pearson out_no_task: 988315e28c8SJames Pearson return ret; 989315e28c8SJames Pearson } 990315e28c8SJames Pearson 991315e28c8SJames Pearson static const struct file_operations proc_environ_operations = { 992315e28c8SJames Pearson .read = environ_read, 993315e28c8SJames Pearson }; 994315e28c8SJames Pearson 9951da177e4SLinus Torvalds static ssize_t oom_adjust_read(struct file *file, char __user *buf, 9961da177e4SLinus Torvalds size_t count, loff_t *ppos) 9971da177e4SLinus Torvalds { 9982fddfeefSJosef "Jeff" Sipek struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode); 9998578cea7SEric W. Biederman char buffer[PROC_NUMBUF]; 10001da177e4SLinus Torvalds size_t len; 100199f89551SEric W. Biederman int oom_adjust; 10021da177e4SLinus Torvalds 100399f89551SEric W. Biederman if (!task) 100499f89551SEric W. Biederman return -ESRCH; 100599f89551SEric W. Biederman oom_adjust = task->oomkilladj; 100699f89551SEric W. Biederman put_task_struct(task); 100799f89551SEric W. Biederman 10088578cea7SEric W. Biederman len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust); 10090c28f287SAkinobu Mita 10100c28f287SAkinobu Mita return simple_read_from_buffer(buf, count, ppos, buffer, len); 10111da177e4SLinus Torvalds } 10121da177e4SLinus Torvalds 10131da177e4SLinus Torvalds static ssize_t oom_adjust_write(struct file *file, const char __user *buf, 10141da177e4SLinus Torvalds size_t count, loff_t *ppos) 10151da177e4SLinus Torvalds { 101699f89551SEric W. Biederman struct task_struct *task; 10178578cea7SEric W. Biederman char buffer[PROC_NUMBUF], *end; 10181da177e4SLinus Torvalds int oom_adjust; 10191da177e4SLinus Torvalds 10208578cea7SEric W. Biederman memset(buffer, 0, sizeof(buffer)); 10218578cea7SEric W. Biederman if (count > sizeof(buffer) - 1) 10228578cea7SEric W. Biederman count = sizeof(buffer) - 1; 10231da177e4SLinus Torvalds if (copy_from_user(buffer, buf, count)) 10241da177e4SLinus Torvalds return -EFAULT; 10251da177e4SLinus Torvalds oom_adjust = simple_strtol(buffer, &end, 0); 10268ac773b4SAlexey Dobriyan if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) && 10278ac773b4SAlexey Dobriyan oom_adjust != OOM_DISABLE) 10281da177e4SLinus Torvalds return -EINVAL; 10291da177e4SLinus Torvalds if (*end == '\n') 10301da177e4SLinus Torvalds end++; 10312fddfeefSJosef "Jeff" Sipek task = get_proc_task(file->f_path.dentry->d_inode); 103299f89551SEric W. Biederman if (!task) 103399f89551SEric W. Biederman return -ESRCH; 10348fb4fc68SGuillem Jover if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) { 10358fb4fc68SGuillem Jover put_task_struct(task); 10368fb4fc68SGuillem Jover return -EACCES; 10378fb4fc68SGuillem Jover } 10381da177e4SLinus Torvalds task->oomkilladj = oom_adjust; 103999f89551SEric W. Biederman put_task_struct(task); 10401da177e4SLinus Torvalds if (end - buffer == 0) 10411da177e4SLinus Torvalds return -EIO; 10421da177e4SLinus Torvalds return end - buffer; 10431da177e4SLinus Torvalds } 10441da177e4SLinus Torvalds 104500977a59SArjan van de Ven static const struct file_operations proc_oom_adjust_operations = { 10461da177e4SLinus Torvalds .read = oom_adjust_read, 10471da177e4SLinus Torvalds .write = oom_adjust_write, 10481da177e4SLinus Torvalds }; 10491da177e4SLinus Torvalds 10501da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL 10511da177e4SLinus Torvalds #define TMPBUFLEN 21 10521da177e4SLinus Torvalds static ssize_t proc_loginuid_read(struct file * file, char __user * buf, 10531da177e4SLinus Torvalds size_t count, loff_t *ppos) 10541da177e4SLinus Torvalds { 10552fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 105699f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 10571da177e4SLinus Torvalds ssize_t length; 10581da177e4SLinus Torvalds char tmpbuf[TMPBUFLEN]; 10591da177e4SLinus Torvalds 106099f89551SEric W. Biederman if (!task) 106199f89551SEric W. Biederman return -ESRCH; 10621da177e4SLinus Torvalds length = scnprintf(tmpbuf, TMPBUFLEN, "%u", 10630c11b942SAl Viro audit_get_loginuid(task)); 106499f89551SEric W. Biederman put_task_struct(task); 10651da177e4SLinus Torvalds return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 10661da177e4SLinus Torvalds } 10671da177e4SLinus Torvalds 10681da177e4SLinus Torvalds static ssize_t proc_loginuid_write(struct file * file, const char __user * buf, 10691da177e4SLinus Torvalds size_t count, loff_t *ppos) 10701da177e4SLinus Torvalds { 10712fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 10721da177e4SLinus Torvalds char *page, *tmp; 10731da177e4SLinus Torvalds ssize_t length; 10741da177e4SLinus Torvalds uid_t loginuid; 10751da177e4SLinus Torvalds 10761da177e4SLinus Torvalds if (!capable(CAP_AUDIT_CONTROL)) 10771da177e4SLinus Torvalds return -EPERM; 10781da177e4SLinus Torvalds 107913b41b09SEric W. Biederman if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) 10801da177e4SLinus Torvalds return -EPERM; 10811da177e4SLinus Torvalds 1082e0182909SAl Viro if (count >= PAGE_SIZE) 1083e0182909SAl Viro count = PAGE_SIZE - 1; 10841da177e4SLinus Torvalds 10851da177e4SLinus Torvalds if (*ppos != 0) { 10861da177e4SLinus Torvalds /* No partial writes. */ 10871da177e4SLinus Torvalds return -EINVAL; 10881da177e4SLinus Torvalds } 1089e12ba74dSMel Gorman page = (char*)__get_free_page(GFP_TEMPORARY); 10901da177e4SLinus Torvalds if (!page) 10911da177e4SLinus Torvalds return -ENOMEM; 10921da177e4SLinus Torvalds length = -EFAULT; 10931da177e4SLinus Torvalds if (copy_from_user(page, buf, count)) 10941da177e4SLinus Torvalds goto out_free_page; 10951da177e4SLinus Torvalds 1096e0182909SAl Viro page[count] = '\0'; 10971da177e4SLinus Torvalds loginuid = simple_strtoul(page, &tmp, 10); 10981da177e4SLinus Torvalds if (tmp == page) { 10991da177e4SLinus Torvalds length = -EINVAL; 11001da177e4SLinus Torvalds goto out_free_page; 11011da177e4SLinus Torvalds 11021da177e4SLinus Torvalds } 110399f89551SEric W. Biederman length = audit_set_loginuid(current, loginuid); 11041da177e4SLinus Torvalds if (likely(length == 0)) 11051da177e4SLinus Torvalds length = count; 11061da177e4SLinus Torvalds 11071da177e4SLinus Torvalds out_free_page: 11081da177e4SLinus Torvalds free_page((unsigned long) page); 11091da177e4SLinus Torvalds return length; 11101da177e4SLinus Torvalds } 11111da177e4SLinus Torvalds 111200977a59SArjan van de Ven static const struct file_operations proc_loginuid_operations = { 11131da177e4SLinus Torvalds .read = proc_loginuid_read, 11141da177e4SLinus Torvalds .write = proc_loginuid_write, 11151da177e4SLinus Torvalds }; 11161e0bd755SEric Paris 11171e0bd755SEric Paris static ssize_t proc_sessionid_read(struct file * file, char __user * buf, 11181e0bd755SEric Paris size_t count, loff_t *ppos) 11191e0bd755SEric Paris { 11201e0bd755SEric Paris struct inode * inode = file->f_path.dentry->d_inode; 11211e0bd755SEric Paris struct task_struct *task = get_proc_task(inode); 11221e0bd755SEric Paris ssize_t length; 11231e0bd755SEric Paris char tmpbuf[TMPBUFLEN]; 11241e0bd755SEric Paris 11251e0bd755SEric Paris if (!task) 11261e0bd755SEric Paris return -ESRCH; 11271e0bd755SEric Paris length = scnprintf(tmpbuf, TMPBUFLEN, "%u", 11281e0bd755SEric Paris audit_get_sessionid(task)); 11291e0bd755SEric Paris put_task_struct(task); 11301e0bd755SEric Paris return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 11311e0bd755SEric Paris } 11321e0bd755SEric Paris 11331e0bd755SEric Paris static const struct file_operations proc_sessionid_operations = { 11341e0bd755SEric Paris .read = proc_sessionid_read, 11351e0bd755SEric Paris }; 11361da177e4SLinus Torvalds #endif 11371da177e4SLinus Torvalds 1138f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION 1139f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_read(struct file * file, char __user * buf, 1140f4f154fdSAkinobu Mita size_t count, loff_t *ppos) 1141f4f154fdSAkinobu Mita { 1142f4f154fdSAkinobu Mita struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 1143f4f154fdSAkinobu Mita char buffer[PROC_NUMBUF]; 1144f4f154fdSAkinobu Mita size_t len; 1145f4f154fdSAkinobu Mita int make_it_fail; 1146f4f154fdSAkinobu Mita 1147f4f154fdSAkinobu Mita if (!task) 1148f4f154fdSAkinobu Mita return -ESRCH; 1149f4f154fdSAkinobu Mita make_it_fail = task->make_it_fail; 1150f4f154fdSAkinobu Mita put_task_struct(task); 1151f4f154fdSAkinobu Mita 1152f4f154fdSAkinobu Mita len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail); 11530c28f287SAkinobu Mita 11540c28f287SAkinobu Mita return simple_read_from_buffer(buf, count, ppos, buffer, len); 1155f4f154fdSAkinobu Mita } 1156f4f154fdSAkinobu Mita 1157f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_write(struct file * file, 1158f4f154fdSAkinobu Mita const char __user * buf, size_t count, loff_t *ppos) 1159f4f154fdSAkinobu Mita { 1160f4f154fdSAkinobu Mita struct task_struct *task; 1161f4f154fdSAkinobu Mita char buffer[PROC_NUMBUF], *end; 1162f4f154fdSAkinobu Mita int make_it_fail; 1163f4f154fdSAkinobu Mita 1164f4f154fdSAkinobu Mita if (!capable(CAP_SYS_RESOURCE)) 1165f4f154fdSAkinobu Mita return -EPERM; 1166f4f154fdSAkinobu Mita memset(buffer, 0, sizeof(buffer)); 1167f4f154fdSAkinobu Mita if (count > sizeof(buffer) - 1) 1168f4f154fdSAkinobu Mita count = sizeof(buffer) - 1; 1169f4f154fdSAkinobu Mita if (copy_from_user(buffer, buf, count)) 1170f4f154fdSAkinobu Mita return -EFAULT; 1171f4f154fdSAkinobu Mita make_it_fail = simple_strtol(buffer, &end, 0); 1172f4f154fdSAkinobu Mita if (*end == '\n') 1173f4f154fdSAkinobu Mita end++; 1174f4f154fdSAkinobu Mita task = get_proc_task(file->f_dentry->d_inode); 1175f4f154fdSAkinobu Mita if (!task) 1176f4f154fdSAkinobu Mita return -ESRCH; 1177f4f154fdSAkinobu Mita task->make_it_fail = make_it_fail; 1178f4f154fdSAkinobu Mita put_task_struct(task); 1179f4f154fdSAkinobu Mita if (end - buffer == 0) 1180f4f154fdSAkinobu Mita return -EIO; 1181f4f154fdSAkinobu Mita return end - buffer; 1182f4f154fdSAkinobu Mita } 1183f4f154fdSAkinobu Mita 118400977a59SArjan van de Ven static const struct file_operations proc_fault_inject_operations = { 1185f4f154fdSAkinobu Mita .read = proc_fault_inject_read, 1186f4f154fdSAkinobu Mita .write = proc_fault_inject_write, 1187f4f154fdSAkinobu Mita }; 1188f4f154fdSAkinobu Mita #endif 1189f4f154fdSAkinobu Mita 11909745512cSArjan van de Ven 119143ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG 119243ae34cbSIngo Molnar /* 119343ae34cbSIngo Molnar * Print out various scheduling related per-task fields: 119443ae34cbSIngo Molnar */ 119543ae34cbSIngo Molnar static int sched_show(struct seq_file *m, void *v) 119643ae34cbSIngo Molnar { 119743ae34cbSIngo Molnar struct inode *inode = m->private; 119843ae34cbSIngo Molnar struct task_struct *p; 119943ae34cbSIngo Molnar 120043ae34cbSIngo Molnar p = get_proc_task(inode); 120143ae34cbSIngo Molnar if (!p) 120243ae34cbSIngo Molnar return -ESRCH; 120343ae34cbSIngo Molnar proc_sched_show_task(p, m); 120443ae34cbSIngo Molnar 120543ae34cbSIngo Molnar put_task_struct(p); 120643ae34cbSIngo Molnar 120743ae34cbSIngo Molnar return 0; 120843ae34cbSIngo Molnar } 120943ae34cbSIngo Molnar 121043ae34cbSIngo Molnar static ssize_t 121143ae34cbSIngo Molnar sched_write(struct file *file, const char __user *buf, 121243ae34cbSIngo Molnar size_t count, loff_t *offset) 121343ae34cbSIngo Molnar { 121443ae34cbSIngo Molnar struct inode *inode = file->f_path.dentry->d_inode; 121543ae34cbSIngo Molnar struct task_struct *p; 121643ae34cbSIngo Molnar 121743ae34cbSIngo Molnar p = get_proc_task(inode); 121843ae34cbSIngo Molnar if (!p) 121943ae34cbSIngo Molnar return -ESRCH; 122043ae34cbSIngo Molnar proc_sched_set_task(p); 122143ae34cbSIngo Molnar 122243ae34cbSIngo Molnar put_task_struct(p); 122343ae34cbSIngo Molnar 122443ae34cbSIngo Molnar return count; 122543ae34cbSIngo Molnar } 122643ae34cbSIngo Molnar 122743ae34cbSIngo Molnar static int sched_open(struct inode *inode, struct file *filp) 122843ae34cbSIngo Molnar { 122943ae34cbSIngo Molnar int ret; 123043ae34cbSIngo Molnar 123143ae34cbSIngo Molnar ret = single_open(filp, sched_show, NULL); 123243ae34cbSIngo Molnar if (!ret) { 123343ae34cbSIngo Molnar struct seq_file *m = filp->private_data; 123443ae34cbSIngo Molnar 123543ae34cbSIngo Molnar m->private = inode; 123643ae34cbSIngo Molnar } 123743ae34cbSIngo Molnar return ret; 123843ae34cbSIngo Molnar } 123943ae34cbSIngo Molnar 124043ae34cbSIngo Molnar static const struct file_operations proc_pid_sched_operations = { 124143ae34cbSIngo Molnar .open = sched_open, 124243ae34cbSIngo Molnar .read = seq_read, 124343ae34cbSIngo Molnar .write = sched_write, 124443ae34cbSIngo Molnar .llseek = seq_lseek, 12455ea473a1SAlexey Dobriyan .release = single_release, 124643ae34cbSIngo Molnar }; 124743ae34cbSIngo Molnar 124843ae34cbSIngo Molnar #endif 124943ae34cbSIngo Molnar 1250925d1c40SMatt Helsley /* 1251925d1c40SMatt Helsley * We added or removed a vma mapping the executable. The vmas are only mapped 1252925d1c40SMatt Helsley * during exec and are not mapped with the mmap system call. 1253925d1c40SMatt Helsley * Callers must hold down_write() on the mm's mmap_sem for these 1254925d1c40SMatt Helsley */ 1255925d1c40SMatt Helsley void added_exe_file_vma(struct mm_struct *mm) 1256925d1c40SMatt Helsley { 1257925d1c40SMatt Helsley mm->num_exe_file_vmas++; 1258925d1c40SMatt Helsley } 1259925d1c40SMatt Helsley 1260925d1c40SMatt Helsley void removed_exe_file_vma(struct mm_struct *mm) 1261925d1c40SMatt Helsley { 1262925d1c40SMatt Helsley mm->num_exe_file_vmas--; 1263925d1c40SMatt Helsley if ((mm->num_exe_file_vmas == 0) && mm->exe_file){ 1264925d1c40SMatt Helsley fput(mm->exe_file); 1265925d1c40SMatt Helsley mm->exe_file = NULL; 1266925d1c40SMatt Helsley } 1267925d1c40SMatt Helsley 1268925d1c40SMatt Helsley } 1269925d1c40SMatt Helsley 1270925d1c40SMatt Helsley void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file) 1271925d1c40SMatt Helsley { 1272925d1c40SMatt Helsley if (new_exe_file) 1273925d1c40SMatt Helsley get_file(new_exe_file); 1274925d1c40SMatt Helsley if (mm->exe_file) 1275925d1c40SMatt Helsley fput(mm->exe_file); 1276925d1c40SMatt Helsley mm->exe_file = new_exe_file; 1277925d1c40SMatt Helsley mm->num_exe_file_vmas = 0; 1278925d1c40SMatt Helsley } 1279925d1c40SMatt Helsley 1280925d1c40SMatt Helsley struct file *get_mm_exe_file(struct mm_struct *mm) 1281925d1c40SMatt Helsley { 1282925d1c40SMatt Helsley struct file *exe_file; 1283925d1c40SMatt Helsley 1284925d1c40SMatt Helsley /* We need mmap_sem to protect against races with removal of 1285925d1c40SMatt Helsley * VM_EXECUTABLE vmas */ 1286925d1c40SMatt Helsley down_read(&mm->mmap_sem); 1287925d1c40SMatt Helsley exe_file = mm->exe_file; 1288925d1c40SMatt Helsley if (exe_file) 1289925d1c40SMatt Helsley get_file(exe_file); 1290925d1c40SMatt Helsley up_read(&mm->mmap_sem); 1291925d1c40SMatt Helsley return exe_file; 1292925d1c40SMatt Helsley } 1293925d1c40SMatt Helsley 1294925d1c40SMatt Helsley void dup_mm_exe_file(struct mm_struct *oldmm, struct mm_struct *newmm) 1295925d1c40SMatt Helsley { 1296925d1c40SMatt Helsley /* It's safe to write the exe_file pointer without exe_file_lock because 1297925d1c40SMatt Helsley * this is called during fork when the task is not yet in /proc */ 1298925d1c40SMatt Helsley newmm->exe_file = get_mm_exe_file(oldmm); 1299925d1c40SMatt Helsley } 1300925d1c40SMatt Helsley 1301925d1c40SMatt Helsley static int proc_exe_link(struct inode *inode, struct path *exe_path) 1302925d1c40SMatt Helsley { 1303925d1c40SMatt Helsley struct task_struct *task; 1304925d1c40SMatt Helsley struct mm_struct *mm; 1305925d1c40SMatt Helsley struct file *exe_file; 1306925d1c40SMatt Helsley 1307925d1c40SMatt Helsley task = get_proc_task(inode); 1308925d1c40SMatt Helsley if (!task) 1309925d1c40SMatt Helsley return -ENOENT; 1310925d1c40SMatt Helsley mm = get_task_mm(task); 1311925d1c40SMatt Helsley put_task_struct(task); 1312925d1c40SMatt Helsley if (!mm) 1313925d1c40SMatt Helsley return -ENOENT; 1314925d1c40SMatt Helsley exe_file = get_mm_exe_file(mm); 1315925d1c40SMatt Helsley mmput(mm); 1316925d1c40SMatt Helsley if (exe_file) { 1317925d1c40SMatt Helsley *exe_path = exe_file->f_path; 1318925d1c40SMatt Helsley path_get(&exe_file->f_path); 1319925d1c40SMatt Helsley fput(exe_file); 1320925d1c40SMatt Helsley return 0; 1321925d1c40SMatt Helsley } else 1322925d1c40SMatt Helsley return -ENOENT; 1323925d1c40SMatt Helsley } 1324925d1c40SMatt Helsley 1325008b150aSAl Viro static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd) 13261da177e4SLinus Torvalds { 13271da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 13281da177e4SLinus Torvalds int error = -EACCES; 13291da177e4SLinus Torvalds 13301da177e4SLinus Torvalds /* We don't need a base pointer in the /proc filesystem */ 13311d957f9bSJan Blunck path_put(&nd->path); 13321da177e4SLinus Torvalds 1333778c1144SEric W. Biederman /* Are we allowed to snoop on the tasks file descriptors? */ 1334778c1144SEric W. Biederman if (!proc_fd_access_allowed(inode)) 13351da177e4SLinus Torvalds goto out; 13361da177e4SLinus Torvalds 13373dcd25f3SJan Blunck error = PROC_I(inode)->op.proc_get_link(inode, &nd->path); 13381da177e4SLinus Torvalds nd->last_type = LAST_BIND; 13391da177e4SLinus Torvalds out: 1340008b150aSAl Viro return ERR_PTR(error); 13411da177e4SLinus Torvalds } 13421da177e4SLinus Torvalds 13433dcd25f3SJan Blunck static int do_proc_readlink(struct path *path, char __user *buffer, int buflen) 13441da177e4SLinus Torvalds { 1345e12ba74dSMel Gorman char *tmp = (char*)__get_free_page(GFP_TEMPORARY); 13463dcd25f3SJan Blunck char *pathname; 13471da177e4SLinus Torvalds int len; 13481da177e4SLinus Torvalds 13491da177e4SLinus Torvalds if (!tmp) 13501da177e4SLinus Torvalds return -ENOMEM; 13511da177e4SLinus Torvalds 1352cf28b486SJan Blunck pathname = d_path(path, tmp, PAGE_SIZE); 13533dcd25f3SJan Blunck len = PTR_ERR(pathname); 13543dcd25f3SJan Blunck if (IS_ERR(pathname)) 13551da177e4SLinus Torvalds goto out; 13563dcd25f3SJan Blunck len = tmp + PAGE_SIZE - 1 - pathname; 13571da177e4SLinus Torvalds 13581da177e4SLinus Torvalds if (len > buflen) 13591da177e4SLinus Torvalds len = buflen; 13603dcd25f3SJan Blunck if (copy_to_user(buffer, pathname, len)) 13611da177e4SLinus Torvalds len = -EFAULT; 13621da177e4SLinus Torvalds out: 13631da177e4SLinus Torvalds free_page((unsigned long)tmp); 13641da177e4SLinus Torvalds return len; 13651da177e4SLinus Torvalds } 13661da177e4SLinus Torvalds 13671da177e4SLinus Torvalds static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen) 13681da177e4SLinus Torvalds { 13691da177e4SLinus Torvalds int error = -EACCES; 13701da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 13713dcd25f3SJan Blunck struct path path; 13721da177e4SLinus Torvalds 1373778c1144SEric W. Biederman /* Are we allowed to snoop on the tasks file descriptors? */ 1374778c1144SEric W. Biederman if (!proc_fd_access_allowed(inode)) 13751da177e4SLinus Torvalds goto out; 13761da177e4SLinus Torvalds 13773dcd25f3SJan Blunck error = PROC_I(inode)->op.proc_get_link(inode, &path); 13781da177e4SLinus Torvalds if (error) 13791da177e4SLinus Torvalds goto out; 13801da177e4SLinus Torvalds 13813dcd25f3SJan Blunck error = do_proc_readlink(&path, buffer, buflen); 13823dcd25f3SJan Blunck path_put(&path); 13831da177e4SLinus Torvalds out: 13841da177e4SLinus Torvalds return error; 13851da177e4SLinus Torvalds } 13861da177e4SLinus Torvalds 1387c5ef1c42SArjan van de Ven static const struct inode_operations proc_pid_link_inode_operations = { 13881da177e4SLinus Torvalds .readlink = proc_pid_readlink, 13896d76fa58SLinus Torvalds .follow_link = proc_pid_follow_link, 13906d76fa58SLinus Torvalds .setattr = proc_setattr, 13911da177e4SLinus Torvalds }; 13921da177e4SLinus Torvalds 139328a6d671SEric W. Biederman 139428a6d671SEric W. Biederman /* building an inode */ 139528a6d671SEric W. Biederman 139628a6d671SEric W. Biederman static int task_dumpable(struct task_struct *task) 139728a6d671SEric W. Biederman { 139828a6d671SEric W. Biederman int dumpable = 0; 139928a6d671SEric W. Biederman struct mm_struct *mm; 140028a6d671SEric W. Biederman 140128a6d671SEric W. Biederman task_lock(task); 140228a6d671SEric W. Biederman mm = task->mm; 140328a6d671SEric W. Biederman if (mm) 14046c5d5238SKawai, Hidehiro dumpable = get_dumpable(mm); 140528a6d671SEric W. Biederman task_unlock(task); 140628a6d671SEric W. Biederman if(dumpable == 1) 140728a6d671SEric W. Biederman return 1; 140828a6d671SEric W. Biederman return 0; 140928a6d671SEric W. Biederman } 141028a6d671SEric W. Biederman 141128a6d671SEric W. Biederman 141261a28784SEric W. Biederman static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task) 141328a6d671SEric W. Biederman { 141428a6d671SEric W. Biederman struct inode * inode; 141528a6d671SEric W. Biederman struct proc_inode *ei; 1416c69e8d9cSDavid Howells const struct cred *cred; 141728a6d671SEric W. Biederman 141828a6d671SEric W. Biederman /* We need a new inode */ 141928a6d671SEric W. Biederman 142028a6d671SEric W. Biederman inode = new_inode(sb); 142128a6d671SEric W. Biederman if (!inode) 142228a6d671SEric W. Biederman goto out; 142328a6d671SEric W. Biederman 142428a6d671SEric W. Biederman /* Common stuff */ 142528a6d671SEric W. Biederman ei = PROC_I(inode); 142628a6d671SEric W. Biederman inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; 142728a6d671SEric W. Biederman inode->i_op = &proc_def_inode_operations; 142828a6d671SEric W. Biederman 142928a6d671SEric W. Biederman /* 143028a6d671SEric W. Biederman * grab the reference to task. 143128a6d671SEric W. Biederman */ 14321a657f78SOleg Nesterov ei->pid = get_task_pid(task, PIDTYPE_PID); 143328a6d671SEric W. Biederman if (!ei->pid) 143428a6d671SEric W. Biederman goto out_unlock; 143528a6d671SEric W. Biederman 143628a6d671SEric W. Biederman if (task_dumpable(task)) { 1437c69e8d9cSDavid Howells rcu_read_lock(); 1438c69e8d9cSDavid Howells cred = __task_cred(task); 1439c69e8d9cSDavid Howells inode->i_uid = cred->euid; 1440c69e8d9cSDavid Howells inode->i_gid = cred->egid; 1441c69e8d9cSDavid Howells rcu_read_unlock(); 144228a6d671SEric W. Biederman } 144328a6d671SEric W. Biederman security_task_to_inode(task, inode); 144428a6d671SEric W. Biederman 144528a6d671SEric W. Biederman out: 144628a6d671SEric W. Biederman return inode; 144728a6d671SEric W. Biederman 144828a6d671SEric W. Biederman out_unlock: 144928a6d671SEric W. Biederman iput(inode); 145028a6d671SEric W. Biederman return NULL; 145128a6d671SEric W. Biederman } 145228a6d671SEric W. Biederman 145328a6d671SEric W. Biederman static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) 145428a6d671SEric W. Biederman { 145528a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 145628a6d671SEric W. Biederman struct task_struct *task; 1457c69e8d9cSDavid Howells const struct cred *cred; 1458c69e8d9cSDavid Howells 145928a6d671SEric W. Biederman generic_fillattr(inode, stat); 146028a6d671SEric W. Biederman 146128a6d671SEric W. Biederman rcu_read_lock(); 146228a6d671SEric W. Biederman stat->uid = 0; 146328a6d671SEric W. Biederman stat->gid = 0; 146428a6d671SEric W. Biederman task = pid_task(proc_pid(inode), PIDTYPE_PID); 146528a6d671SEric W. Biederman if (task) { 146628a6d671SEric W. Biederman if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) || 146728a6d671SEric W. Biederman task_dumpable(task)) { 1468c69e8d9cSDavid Howells cred = __task_cred(task); 1469c69e8d9cSDavid Howells stat->uid = cred->euid; 1470c69e8d9cSDavid Howells stat->gid = cred->egid; 147128a6d671SEric W. Biederman } 147228a6d671SEric W. Biederman } 147328a6d671SEric W. Biederman rcu_read_unlock(); 147428a6d671SEric W. Biederman return 0; 147528a6d671SEric W. Biederman } 147628a6d671SEric W. Biederman 147728a6d671SEric W. Biederman /* dentry stuff */ 147828a6d671SEric W. Biederman 147928a6d671SEric W. Biederman /* 148028a6d671SEric W. Biederman * Exceptional case: normally we are not allowed to unhash a busy 148128a6d671SEric W. Biederman * directory. In this case, however, we can do it - no aliasing problems 148228a6d671SEric W. Biederman * due to the way we treat inodes. 148328a6d671SEric W. Biederman * 148428a6d671SEric W. Biederman * Rewrite the inode's ownerships here because the owning task may have 148528a6d671SEric W. Biederman * performed a setuid(), etc. 148628a6d671SEric W. Biederman * 148728a6d671SEric W. Biederman * Before the /proc/pid/status file was created the only way to read 148828a6d671SEric W. Biederman * the effective uid of a /process was to stat /proc/pid. Reading 148928a6d671SEric W. Biederman * /proc/pid/status is slow enough that procps and other packages 149028a6d671SEric W. Biederman * kept stating /proc/pid. To keep the rules in /proc simple I have 149128a6d671SEric W. Biederman * made this apply to all per process world readable and executable 149228a6d671SEric W. Biederman * directories. 149328a6d671SEric W. Biederman */ 149428a6d671SEric W. Biederman static int pid_revalidate(struct dentry *dentry, struct nameidata *nd) 149528a6d671SEric W. Biederman { 149628a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 149728a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 1498c69e8d9cSDavid Howells const struct cred *cred; 1499c69e8d9cSDavid Howells 150028a6d671SEric W. Biederman if (task) { 150128a6d671SEric W. Biederman if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) || 150228a6d671SEric W. Biederman task_dumpable(task)) { 1503c69e8d9cSDavid Howells rcu_read_lock(); 1504c69e8d9cSDavid Howells cred = __task_cred(task); 1505c69e8d9cSDavid Howells inode->i_uid = cred->euid; 1506c69e8d9cSDavid Howells inode->i_gid = cred->egid; 1507c69e8d9cSDavid Howells rcu_read_unlock(); 150828a6d671SEric W. Biederman } else { 150928a6d671SEric W. Biederman inode->i_uid = 0; 151028a6d671SEric W. Biederman inode->i_gid = 0; 151128a6d671SEric W. Biederman } 151228a6d671SEric W. Biederman inode->i_mode &= ~(S_ISUID | S_ISGID); 151328a6d671SEric W. Biederman security_task_to_inode(task, inode); 151428a6d671SEric W. Biederman put_task_struct(task); 151528a6d671SEric W. Biederman return 1; 151628a6d671SEric W. Biederman } 151728a6d671SEric W. Biederman d_drop(dentry); 151828a6d671SEric W. Biederman return 0; 151928a6d671SEric W. Biederman } 152028a6d671SEric W. Biederman 152128a6d671SEric W. Biederman static int pid_delete_dentry(struct dentry * dentry) 152228a6d671SEric W. Biederman { 152328a6d671SEric W. Biederman /* Is the task we represent dead? 152428a6d671SEric W. Biederman * If so, then don't put the dentry on the lru list, 152528a6d671SEric W. Biederman * kill it immediately. 152628a6d671SEric W. Biederman */ 152728a6d671SEric W. Biederman return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first; 152828a6d671SEric W. Biederman } 152928a6d671SEric W. Biederman 1530d72f71ebSAl Viro static const struct dentry_operations pid_dentry_operations = 153128a6d671SEric W. Biederman { 153228a6d671SEric W. Biederman .d_revalidate = pid_revalidate, 153328a6d671SEric W. Biederman .d_delete = pid_delete_dentry, 153428a6d671SEric W. Biederman }; 153528a6d671SEric W. Biederman 153628a6d671SEric W. Biederman /* Lookups */ 153728a6d671SEric W. Biederman 1538c5141e6dSEric Dumazet typedef struct dentry *instantiate_t(struct inode *, struct dentry *, 1539c5141e6dSEric Dumazet struct task_struct *, const void *); 154061a28784SEric W. Biederman 15411c0d04c9SEric W. Biederman /* 15421c0d04c9SEric W. Biederman * Fill a directory entry. 15431c0d04c9SEric W. Biederman * 15441c0d04c9SEric W. Biederman * If possible create the dcache entry and derive our inode number and 15451c0d04c9SEric W. Biederman * file type from dcache entry. 15461c0d04c9SEric W. Biederman * 15471c0d04c9SEric W. Biederman * Since all of the proc inode numbers are dynamically generated, the inode 15481c0d04c9SEric W. Biederman * numbers do not exist until the inode is cache. This means creating the 15491c0d04c9SEric W. Biederman * the dcache entry in readdir is necessary to keep the inode numbers 15501c0d04c9SEric W. Biederman * reported by readdir in sync with the inode numbers reported 15511c0d04c9SEric W. Biederman * by stat. 15521c0d04c9SEric W. Biederman */ 155361a28784SEric W. Biederman static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir, 155461a28784SEric W. Biederman char *name, int len, 1555c5141e6dSEric Dumazet instantiate_t instantiate, struct task_struct *task, const void *ptr) 155661a28784SEric W. Biederman { 15572fddfeefSJosef "Jeff" Sipek struct dentry *child, *dir = filp->f_path.dentry; 155861a28784SEric W. Biederman struct inode *inode; 155961a28784SEric W. Biederman struct qstr qname; 156061a28784SEric W. Biederman ino_t ino = 0; 156161a28784SEric W. Biederman unsigned type = DT_UNKNOWN; 156261a28784SEric W. Biederman 156361a28784SEric W. Biederman qname.name = name; 156461a28784SEric W. Biederman qname.len = len; 156561a28784SEric W. Biederman qname.hash = full_name_hash(name, len); 156661a28784SEric W. Biederman 156761a28784SEric W. Biederman child = d_lookup(dir, &qname); 156861a28784SEric W. Biederman if (!child) { 156961a28784SEric W. Biederman struct dentry *new; 157061a28784SEric W. Biederman new = d_alloc(dir, &qname); 157161a28784SEric W. Biederman if (new) { 157261a28784SEric W. Biederman child = instantiate(dir->d_inode, new, task, ptr); 157361a28784SEric W. Biederman if (child) 157461a28784SEric W. Biederman dput(new); 157561a28784SEric W. Biederman else 157661a28784SEric W. Biederman child = new; 157761a28784SEric W. Biederman } 157861a28784SEric W. Biederman } 157961a28784SEric W. Biederman if (!child || IS_ERR(child) || !child->d_inode) 158061a28784SEric W. Biederman goto end_instantiate; 158161a28784SEric W. Biederman inode = child->d_inode; 158261a28784SEric W. Biederman if (inode) { 158361a28784SEric W. Biederman ino = inode->i_ino; 158461a28784SEric W. Biederman type = inode->i_mode >> 12; 158561a28784SEric W. Biederman } 158661a28784SEric W. Biederman dput(child); 158761a28784SEric W. Biederman end_instantiate: 158861a28784SEric W. Biederman if (!ino) 158961a28784SEric W. Biederman ino = find_inode_number(dir, &qname); 159061a28784SEric W. Biederman if (!ino) 159161a28784SEric W. Biederman ino = 1; 159261a28784SEric W. Biederman return filldir(dirent, name, len, filp->f_pos, ino, type); 159361a28784SEric W. Biederman } 159461a28784SEric W. Biederman 159528a6d671SEric W. Biederman static unsigned name_to_int(struct dentry *dentry) 159628a6d671SEric W. Biederman { 159728a6d671SEric W. Biederman const char *name = dentry->d_name.name; 159828a6d671SEric W. Biederman int len = dentry->d_name.len; 159928a6d671SEric W. Biederman unsigned n = 0; 160028a6d671SEric W. Biederman 160128a6d671SEric W. Biederman if (len > 1 && *name == '0') 160228a6d671SEric W. Biederman goto out; 160328a6d671SEric W. Biederman while (len-- > 0) { 160428a6d671SEric W. Biederman unsigned c = *name++ - '0'; 160528a6d671SEric W. Biederman if (c > 9) 160628a6d671SEric W. Biederman goto out; 160728a6d671SEric W. Biederman if (n >= (~0U-9)/10) 160828a6d671SEric W. Biederman goto out; 160928a6d671SEric W. Biederman n *= 10; 161028a6d671SEric W. Biederman n += c; 161128a6d671SEric W. Biederman } 161228a6d671SEric W. Biederman return n; 161328a6d671SEric W. Biederman out: 161428a6d671SEric W. Biederman return ~0U; 161528a6d671SEric W. Biederman } 161628a6d671SEric W. Biederman 161727932742SMiklos Szeredi #define PROC_FDINFO_MAX 64 161827932742SMiklos Szeredi 16193dcd25f3SJan Blunck static int proc_fd_info(struct inode *inode, struct path *path, char *info) 162028a6d671SEric W. Biederman { 162128a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 162228a6d671SEric W. Biederman struct files_struct *files = NULL; 162328a6d671SEric W. Biederman struct file *file; 162428a6d671SEric W. Biederman int fd = proc_fd(inode); 162528a6d671SEric W. Biederman 162628a6d671SEric W. Biederman if (task) { 162728a6d671SEric W. Biederman files = get_files_struct(task); 162828a6d671SEric W. Biederman put_task_struct(task); 162928a6d671SEric W. Biederman } 163028a6d671SEric W. Biederman if (files) { 163128a6d671SEric W. Biederman /* 163228a6d671SEric W. Biederman * We are not taking a ref to the file structure, so we must 163328a6d671SEric W. Biederman * hold ->file_lock. 163428a6d671SEric W. Biederman */ 163528a6d671SEric W. Biederman spin_lock(&files->file_lock); 163628a6d671SEric W. Biederman file = fcheck_files(files, fd); 163728a6d671SEric W. Biederman if (file) { 16383dcd25f3SJan Blunck if (path) { 16393dcd25f3SJan Blunck *path = file->f_path; 16403dcd25f3SJan Blunck path_get(&file->f_path); 16413dcd25f3SJan Blunck } 164227932742SMiklos Szeredi if (info) 164327932742SMiklos Szeredi snprintf(info, PROC_FDINFO_MAX, 164427932742SMiklos Szeredi "pos:\t%lli\n" 164527932742SMiklos Szeredi "flags:\t0%o\n", 164627932742SMiklos Szeredi (long long) file->f_pos, 164727932742SMiklos Szeredi file->f_flags); 164828a6d671SEric W. Biederman spin_unlock(&files->file_lock); 164928a6d671SEric W. Biederman put_files_struct(files); 165028a6d671SEric W. Biederman return 0; 165128a6d671SEric W. Biederman } 165228a6d671SEric W. Biederman spin_unlock(&files->file_lock); 165328a6d671SEric W. Biederman put_files_struct(files); 165428a6d671SEric W. Biederman } 165528a6d671SEric W. Biederman return -ENOENT; 165628a6d671SEric W. Biederman } 165728a6d671SEric W. Biederman 16583dcd25f3SJan Blunck static int proc_fd_link(struct inode *inode, struct path *path) 165927932742SMiklos Szeredi { 16603dcd25f3SJan Blunck return proc_fd_info(inode, path, NULL); 166127932742SMiklos Szeredi } 166227932742SMiklos Szeredi 166328a6d671SEric W. Biederman static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd) 166428a6d671SEric W. Biederman { 166528a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 166628a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 166728a6d671SEric W. Biederman int fd = proc_fd(inode); 166828a6d671SEric W. Biederman struct files_struct *files; 1669c69e8d9cSDavid Howells const struct cred *cred; 167028a6d671SEric W. Biederman 167128a6d671SEric W. Biederman if (task) { 167228a6d671SEric W. Biederman files = get_files_struct(task); 167328a6d671SEric W. Biederman if (files) { 167428a6d671SEric W. Biederman rcu_read_lock(); 167528a6d671SEric W. Biederman if (fcheck_files(files, fd)) { 167628a6d671SEric W. Biederman rcu_read_unlock(); 167728a6d671SEric W. Biederman put_files_struct(files); 167828a6d671SEric W. Biederman if (task_dumpable(task)) { 1679c69e8d9cSDavid Howells rcu_read_lock(); 1680c69e8d9cSDavid Howells cred = __task_cred(task); 1681c69e8d9cSDavid Howells inode->i_uid = cred->euid; 1682c69e8d9cSDavid Howells inode->i_gid = cred->egid; 1683c69e8d9cSDavid Howells rcu_read_unlock(); 168428a6d671SEric W. Biederman } else { 168528a6d671SEric W. Biederman inode->i_uid = 0; 168628a6d671SEric W. Biederman inode->i_gid = 0; 168728a6d671SEric W. Biederman } 168828a6d671SEric W. Biederman inode->i_mode &= ~(S_ISUID | S_ISGID); 168928a6d671SEric W. Biederman security_task_to_inode(task, inode); 169028a6d671SEric W. Biederman put_task_struct(task); 169128a6d671SEric W. Biederman return 1; 169228a6d671SEric W. Biederman } 169328a6d671SEric W. Biederman rcu_read_unlock(); 169428a6d671SEric W. Biederman put_files_struct(files); 169528a6d671SEric W. Biederman } 169628a6d671SEric W. Biederman put_task_struct(task); 169728a6d671SEric W. Biederman } 169828a6d671SEric W. Biederman d_drop(dentry); 169928a6d671SEric W. Biederman return 0; 170028a6d671SEric W. Biederman } 170128a6d671SEric W. Biederman 1702d72f71ebSAl Viro static const struct dentry_operations tid_fd_dentry_operations = 170328a6d671SEric W. Biederman { 170428a6d671SEric W. Biederman .d_revalidate = tid_fd_revalidate, 170528a6d671SEric W. Biederman .d_delete = pid_delete_dentry, 170628a6d671SEric W. Biederman }; 170728a6d671SEric W. Biederman 1708444ceed8SEric W. Biederman static struct dentry *proc_fd_instantiate(struct inode *dir, 1709c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 171028a6d671SEric W. Biederman { 1711c5141e6dSEric Dumazet unsigned fd = *(const unsigned *)ptr; 171228a6d671SEric W. Biederman struct file *file; 171328a6d671SEric W. Biederman struct files_struct *files; 171428a6d671SEric W. Biederman struct inode *inode; 171528a6d671SEric W. Biederman struct proc_inode *ei; 1716444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-ENOENT); 171728a6d671SEric W. Biederman 171861a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 171928a6d671SEric W. Biederman if (!inode) 172028a6d671SEric W. Biederman goto out; 172128a6d671SEric W. Biederman ei = PROC_I(inode); 172228a6d671SEric W. Biederman ei->fd = fd; 172328a6d671SEric W. Biederman files = get_files_struct(task); 172428a6d671SEric W. Biederman if (!files) 1725444ceed8SEric W. Biederman goto out_iput; 172628a6d671SEric W. Biederman inode->i_mode = S_IFLNK; 172728a6d671SEric W. Biederman 172828a6d671SEric W. Biederman /* 172928a6d671SEric W. Biederman * We are not taking a ref to the file structure, so we must 173028a6d671SEric W. Biederman * hold ->file_lock. 173128a6d671SEric W. Biederman */ 173228a6d671SEric W. Biederman spin_lock(&files->file_lock); 173328a6d671SEric W. Biederman file = fcheck_files(files, fd); 173428a6d671SEric W. Biederman if (!file) 1735444ceed8SEric W. Biederman goto out_unlock; 1736aeb5d727SAl Viro if (file->f_mode & FMODE_READ) 173728a6d671SEric W. Biederman inode->i_mode |= S_IRUSR | S_IXUSR; 1738aeb5d727SAl Viro if (file->f_mode & FMODE_WRITE) 173928a6d671SEric W. Biederman inode->i_mode |= S_IWUSR | S_IXUSR; 174028a6d671SEric W. Biederman spin_unlock(&files->file_lock); 174128a6d671SEric W. Biederman put_files_struct(files); 1742444ceed8SEric W. Biederman 174328a6d671SEric W. Biederman inode->i_op = &proc_pid_link_inode_operations; 174428a6d671SEric W. Biederman inode->i_size = 64; 174528a6d671SEric W. Biederman ei->op.proc_get_link = proc_fd_link; 174628a6d671SEric W. Biederman dentry->d_op = &tid_fd_dentry_operations; 174728a6d671SEric W. Biederman d_add(dentry, inode); 174828a6d671SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 174928a6d671SEric W. Biederman if (tid_fd_revalidate(dentry, NULL)) 1750444ceed8SEric W. Biederman error = NULL; 1751444ceed8SEric W. Biederman 1752444ceed8SEric W. Biederman out: 1753444ceed8SEric W. Biederman return error; 1754444ceed8SEric W. Biederman out_unlock: 1755444ceed8SEric W. Biederman spin_unlock(&files->file_lock); 1756444ceed8SEric W. Biederman put_files_struct(files); 1757444ceed8SEric W. Biederman out_iput: 1758444ceed8SEric W. Biederman iput(inode); 1759444ceed8SEric W. Biederman goto out; 1760444ceed8SEric W. Biederman } 1761444ceed8SEric W. Biederman 176227932742SMiklos Szeredi static struct dentry *proc_lookupfd_common(struct inode *dir, 176327932742SMiklos Szeredi struct dentry *dentry, 176427932742SMiklos Szeredi instantiate_t instantiate) 1765444ceed8SEric W. Biederman { 1766444ceed8SEric W. Biederman struct task_struct *task = get_proc_task(dir); 1767444ceed8SEric W. Biederman unsigned fd = name_to_int(dentry); 1768444ceed8SEric W. Biederman struct dentry *result = ERR_PTR(-ENOENT); 1769444ceed8SEric W. Biederman 1770444ceed8SEric W. Biederman if (!task) 1771444ceed8SEric W. Biederman goto out_no_task; 1772444ceed8SEric W. Biederman if (fd == ~0U) 1773444ceed8SEric W. Biederman goto out; 1774444ceed8SEric W. Biederman 177527932742SMiklos Szeredi result = instantiate(dir, dentry, task, &fd); 177628a6d671SEric W. Biederman out: 177728a6d671SEric W. Biederman put_task_struct(task); 177828a6d671SEric W. Biederman out_no_task: 177928a6d671SEric W. Biederman return result; 178028a6d671SEric W. Biederman } 178128a6d671SEric W. Biederman 178227932742SMiklos Szeredi static int proc_readfd_common(struct file * filp, void * dirent, 178327932742SMiklos Szeredi filldir_t filldir, instantiate_t instantiate) 17841da177e4SLinus Torvalds { 17852fddfeefSJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 17865634708bSEric W. Biederman struct inode *inode = dentry->d_inode; 178799f89551SEric W. Biederman struct task_struct *p = get_proc_task(inode); 1788457c2510SPavel Emelyanov unsigned int fd, ino; 17891da177e4SLinus Torvalds int retval; 17901da177e4SLinus Torvalds struct files_struct * files; 17911da177e4SLinus Torvalds 17921da177e4SLinus Torvalds retval = -ENOENT; 179399f89551SEric W. Biederman if (!p) 179499f89551SEric W. Biederman goto out_no_task; 17951da177e4SLinus Torvalds retval = 0; 17961da177e4SLinus Torvalds 17971da177e4SLinus Torvalds fd = filp->f_pos; 17981da177e4SLinus Torvalds switch (fd) { 17991da177e4SLinus Torvalds case 0: 18001da177e4SLinus Torvalds if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0) 18011da177e4SLinus Torvalds goto out; 18021da177e4SLinus Torvalds filp->f_pos++; 18031da177e4SLinus Torvalds case 1: 18045634708bSEric W. Biederman ino = parent_ino(dentry); 18051da177e4SLinus Torvalds if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0) 18061da177e4SLinus Torvalds goto out; 18071da177e4SLinus Torvalds filp->f_pos++; 18081da177e4SLinus Torvalds default: 18091da177e4SLinus Torvalds files = get_files_struct(p); 18101da177e4SLinus Torvalds if (!files) 18111da177e4SLinus Torvalds goto out; 1812b835996fSDipankar Sarma rcu_read_lock(); 18131da177e4SLinus Torvalds for (fd = filp->f_pos-2; 18149b4f526cSAl Viro fd < files_fdtable(files)->max_fds; 18151da177e4SLinus Torvalds fd++, filp->f_pos++) { 181627932742SMiklos Szeredi char name[PROC_NUMBUF]; 181727932742SMiklos Szeredi int len; 18181da177e4SLinus Torvalds 18191da177e4SLinus Torvalds if (!fcheck_files(files, fd)) 18201da177e4SLinus Torvalds continue; 1821b835996fSDipankar Sarma rcu_read_unlock(); 18221da177e4SLinus Torvalds 182327932742SMiklos Szeredi len = snprintf(name, sizeof(name), "%d", fd); 182427932742SMiklos Szeredi if (proc_fill_cache(filp, dirent, filldir, 182527932742SMiklos Szeredi name, len, instantiate, 182627932742SMiklos Szeredi p, &fd) < 0) { 1827b835996fSDipankar Sarma rcu_read_lock(); 18281da177e4SLinus Torvalds break; 18291da177e4SLinus Torvalds } 1830b835996fSDipankar Sarma rcu_read_lock(); 18311da177e4SLinus Torvalds } 1832b835996fSDipankar Sarma rcu_read_unlock(); 18331da177e4SLinus Torvalds put_files_struct(files); 18341da177e4SLinus Torvalds } 18351da177e4SLinus Torvalds out: 183699f89551SEric W. Biederman put_task_struct(p); 183799f89551SEric W. Biederman out_no_task: 18381da177e4SLinus Torvalds return retval; 18391da177e4SLinus Torvalds } 18401da177e4SLinus Torvalds 184127932742SMiklos Szeredi static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry, 184227932742SMiklos Szeredi struct nameidata *nd) 184327932742SMiklos Szeredi { 184427932742SMiklos Szeredi return proc_lookupfd_common(dir, dentry, proc_fd_instantiate); 184527932742SMiklos Szeredi } 184627932742SMiklos Szeredi 184727932742SMiklos Szeredi static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir) 184827932742SMiklos Szeredi { 184927932742SMiklos Szeredi return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate); 185027932742SMiklos Szeredi } 185127932742SMiklos Szeredi 185227932742SMiklos Szeredi static ssize_t proc_fdinfo_read(struct file *file, char __user *buf, 185327932742SMiklos Szeredi size_t len, loff_t *ppos) 185427932742SMiklos Szeredi { 185527932742SMiklos Szeredi char tmp[PROC_FDINFO_MAX]; 18563dcd25f3SJan Blunck int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp); 185727932742SMiklos Szeredi if (!err) 185827932742SMiklos Szeredi err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp)); 185927932742SMiklos Szeredi return err; 186027932742SMiklos Szeredi } 186127932742SMiklos Szeredi 186227932742SMiklos Szeredi static const struct file_operations proc_fdinfo_file_operations = { 186327932742SMiklos Szeredi .open = nonseekable_open, 186427932742SMiklos Szeredi .read = proc_fdinfo_read, 186527932742SMiklos Szeredi }; 186627932742SMiklos Szeredi 186700977a59SArjan van de Ven static const struct file_operations proc_fd_operations = { 18681da177e4SLinus Torvalds .read = generic_read_dir, 18691da177e4SLinus Torvalds .readdir = proc_readfd, 18701da177e4SLinus Torvalds }; 18711da177e4SLinus Torvalds 18721da177e4SLinus Torvalds /* 18738948e11fSAlexey Dobriyan * /proc/pid/fd needs a special permission handler so that a process can still 18748948e11fSAlexey Dobriyan * access /proc/self/fd after it has executed a setuid(). 18758948e11fSAlexey Dobriyan */ 1876e6305c43SAl Viro static int proc_fd_permission(struct inode *inode, int mask) 18778948e11fSAlexey Dobriyan { 18788948e11fSAlexey Dobriyan int rv; 18798948e11fSAlexey Dobriyan 18808948e11fSAlexey Dobriyan rv = generic_permission(inode, mask, NULL); 18818948e11fSAlexey Dobriyan if (rv == 0) 18828948e11fSAlexey Dobriyan return 0; 18838948e11fSAlexey Dobriyan if (task_pid(current) == proc_pid(inode)) 18848948e11fSAlexey Dobriyan rv = 0; 18858948e11fSAlexey Dobriyan return rv; 18868948e11fSAlexey Dobriyan } 18878948e11fSAlexey Dobriyan 18888948e11fSAlexey Dobriyan /* 18891da177e4SLinus Torvalds * proc directories can do almost nothing.. 18901da177e4SLinus Torvalds */ 1891c5ef1c42SArjan van de Ven static const struct inode_operations proc_fd_inode_operations = { 18921da177e4SLinus Torvalds .lookup = proc_lookupfd, 18938948e11fSAlexey Dobriyan .permission = proc_fd_permission, 18946d76fa58SLinus Torvalds .setattr = proc_setattr, 18951da177e4SLinus Torvalds }; 18961da177e4SLinus Torvalds 189727932742SMiklos Szeredi static struct dentry *proc_fdinfo_instantiate(struct inode *dir, 189827932742SMiklos Szeredi struct dentry *dentry, struct task_struct *task, const void *ptr) 189927932742SMiklos Szeredi { 190027932742SMiklos Szeredi unsigned fd = *(unsigned *)ptr; 190127932742SMiklos Szeredi struct inode *inode; 190227932742SMiklos Szeredi struct proc_inode *ei; 190327932742SMiklos Szeredi struct dentry *error = ERR_PTR(-ENOENT); 190427932742SMiklos Szeredi 190527932742SMiklos Szeredi inode = proc_pid_make_inode(dir->i_sb, task); 190627932742SMiklos Szeredi if (!inode) 190727932742SMiklos Szeredi goto out; 190827932742SMiklos Szeredi ei = PROC_I(inode); 190927932742SMiklos Szeredi ei->fd = fd; 191027932742SMiklos Szeredi inode->i_mode = S_IFREG | S_IRUSR; 191127932742SMiklos Szeredi inode->i_fop = &proc_fdinfo_file_operations; 191227932742SMiklos Szeredi dentry->d_op = &tid_fd_dentry_operations; 191327932742SMiklos Szeredi d_add(dentry, inode); 191427932742SMiklos Szeredi /* Close the race of the process dying before we return the dentry */ 191527932742SMiklos Szeredi if (tid_fd_revalidate(dentry, NULL)) 191627932742SMiklos Szeredi error = NULL; 191727932742SMiklos Szeredi 191827932742SMiklos Szeredi out: 191927932742SMiklos Szeredi return error; 192027932742SMiklos Szeredi } 192127932742SMiklos Szeredi 192227932742SMiklos Szeredi static struct dentry *proc_lookupfdinfo(struct inode *dir, 192327932742SMiklos Szeredi struct dentry *dentry, 192427932742SMiklos Szeredi struct nameidata *nd) 192527932742SMiklos Szeredi { 192627932742SMiklos Szeredi return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate); 192727932742SMiklos Szeredi } 192827932742SMiklos Szeredi 192927932742SMiklos Szeredi static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir) 193027932742SMiklos Szeredi { 193127932742SMiklos Szeredi return proc_readfd_common(filp, dirent, filldir, 193227932742SMiklos Szeredi proc_fdinfo_instantiate); 193327932742SMiklos Szeredi } 193427932742SMiklos Szeredi 193527932742SMiklos Szeredi static const struct file_operations proc_fdinfo_operations = { 193627932742SMiklos Szeredi .read = generic_read_dir, 193727932742SMiklos Szeredi .readdir = proc_readfdinfo, 193827932742SMiklos Szeredi }; 193927932742SMiklos Szeredi 194027932742SMiklos Szeredi /* 194127932742SMiklos Szeredi * proc directories can do almost nothing.. 194227932742SMiklos Szeredi */ 194327932742SMiklos Szeredi static const struct inode_operations proc_fdinfo_inode_operations = { 194427932742SMiklos Szeredi .lookup = proc_lookupfdinfo, 194527932742SMiklos Szeredi .setattr = proc_setattr, 194627932742SMiklos Szeredi }; 194727932742SMiklos Szeredi 194827932742SMiklos Szeredi 1949444ceed8SEric W. Biederman static struct dentry *proc_pident_instantiate(struct inode *dir, 1950c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 1951444ceed8SEric W. Biederman { 1952c5141e6dSEric Dumazet const struct pid_entry *p = ptr; 1953444ceed8SEric W. Biederman struct inode *inode; 1954444ceed8SEric W. Biederman struct proc_inode *ei; 1955444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-EINVAL); 1956444ceed8SEric W. Biederman 195761a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 1958444ceed8SEric W. Biederman if (!inode) 1959444ceed8SEric W. Biederman goto out; 1960444ceed8SEric W. Biederman 1961444ceed8SEric W. Biederman ei = PROC_I(inode); 1962444ceed8SEric W. Biederman inode->i_mode = p->mode; 1963444ceed8SEric W. Biederman if (S_ISDIR(inode->i_mode)) 1964444ceed8SEric W. Biederman inode->i_nlink = 2; /* Use getattr to fix if necessary */ 1965444ceed8SEric W. Biederman if (p->iop) 1966444ceed8SEric W. Biederman inode->i_op = p->iop; 1967444ceed8SEric W. Biederman if (p->fop) 1968444ceed8SEric W. Biederman inode->i_fop = p->fop; 1969444ceed8SEric W. Biederman ei->op = p->op; 1970444ceed8SEric W. Biederman dentry->d_op = &pid_dentry_operations; 1971444ceed8SEric W. Biederman d_add(dentry, inode); 1972444ceed8SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 1973444ceed8SEric W. Biederman if (pid_revalidate(dentry, NULL)) 1974444ceed8SEric W. Biederman error = NULL; 1975444ceed8SEric W. Biederman out: 1976444ceed8SEric W. Biederman return error; 1977444ceed8SEric W. Biederman } 1978444ceed8SEric W. Biederman 19791da177e4SLinus Torvalds static struct dentry *proc_pident_lookup(struct inode *dir, 19801da177e4SLinus Torvalds struct dentry *dentry, 1981c5141e6dSEric Dumazet const struct pid_entry *ents, 19827bcd6b0eSEric W. Biederman unsigned int nents) 19831da177e4SLinus Torvalds { 1984cd6a3ce9SEric W. Biederman struct dentry *error; 198599f89551SEric W. Biederman struct task_struct *task = get_proc_task(dir); 1986c5141e6dSEric Dumazet const struct pid_entry *p, *last; 19871da177e4SLinus Torvalds 1988cd6a3ce9SEric W. Biederman error = ERR_PTR(-ENOENT); 19891da177e4SLinus Torvalds 199099f89551SEric W. Biederman if (!task) 199199f89551SEric W. Biederman goto out_no_task; 19921da177e4SLinus Torvalds 199320cdc894SEric W. Biederman /* 199420cdc894SEric W. Biederman * Yes, it does not scale. And it should not. Don't add 199520cdc894SEric W. Biederman * new entries into /proc/<tgid>/ without very good reasons. 199620cdc894SEric W. Biederman */ 19977bcd6b0eSEric W. Biederman last = &ents[nents - 1]; 19987bcd6b0eSEric W. Biederman for (p = ents; p <= last; p++) { 19991da177e4SLinus Torvalds if (p->len != dentry->d_name.len) 20001da177e4SLinus Torvalds continue; 20011da177e4SLinus Torvalds if (!memcmp(dentry->d_name.name, p->name, p->len)) 20021da177e4SLinus Torvalds break; 20031da177e4SLinus Torvalds } 20047bcd6b0eSEric W. Biederman if (p > last) 20051da177e4SLinus Torvalds goto out; 20061da177e4SLinus Torvalds 2007444ceed8SEric W. Biederman error = proc_pident_instantiate(dir, dentry, task, p); 20081da177e4SLinus Torvalds out: 200999f89551SEric W. Biederman put_task_struct(task); 201099f89551SEric W. Biederman out_no_task: 2011cd6a3ce9SEric W. Biederman return error; 20121da177e4SLinus Torvalds } 20131da177e4SLinus Torvalds 2014c5141e6dSEric Dumazet static int proc_pident_fill_cache(struct file *filp, void *dirent, 2015c5141e6dSEric Dumazet filldir_t filldir, struct task_struct *task, const struct pid_entry *p) 201661a28784SEric W. Biederman { 201761a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, p->name, p->len, 201861a28784SEric W. Biederman proc_pident_instantiate, task, p); 201961a28784SEric W. Biederman } 202061a28784SEric W. Biederman 202128a6d671SEric W. Biederman static int proc_pident_readdir(struct file *filp, 202228a6d671SEric W. Biederman void *dirent, filldir_t filldir, 2023c5141e6dSEric Dumazet const struct pid_entry *ents, unsigned int nents) 202428a6d671SEric W. Biederman { 202528a6d671SEric W. Biederman int i; 20262fddfeefSJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 202728a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 202828a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 2029c5141e6dSEric Dumazet const struct pid_entry *p, *last; 203028a6d671SEric W. Biederman ino_t ino; 203128a6d671SEric W. Biederman int ret; 203228a6d671SEric W. Biederman 203328a6d671SEric W. Biederman ret = -ENOENT; 203428a6d671SEric W. Biederman if (!task) 203561a28784SEric W. Biederman goto out_no_task; 203628a6d671SEric W. Biederman 203728a6d671SEric W. Biederman ret = 0; 203828a6d671SEric W. Biederman i = filp->f_pos; 203928a6d671SEric W. Biederman switch (i) { 204028a6d671SEric W. Biederman case 0: 204128a6d671SEric W. Biederman ino = inode->i_ino; 204228a6d671SEric W. Biederman if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0) 204328a6d671SEric W. Biederman goto out; 204428a6d671SEric W. Biederman i++; 204528a6d671SEric W. Biederman filp->f_pos++; 204628a6d671SEric W. Biederman /* fall through */ 204728a6d671SEric W. Biederman case 1: 204828a6d671SEric W. Biederman ino = parent_ino(dentry); 204928a6d671SEric W. Biederman if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0) 205028a6d671SEric W. Biederman goto out; 205128a6d671SEric W. Biederman i++; 205228a6d671SEric W. Biederman filp->f_pos++; 205328a6d671SEric W. Biederman /* fall through */ 205428a6d671SEric W. Biederman default: 205528a6d671SEric W. Biederman i -= 2; 205628a6d671SEric W. Biederman if (i >= nents) { 205728a6d671SEric W. Biederman ret = 1; 205828a6d671SEric W. Biederman goto out; 205928a6d671SEric W. Biederman } 206028a6d671SEric W. Biederman p = ents + i; 20617bcd6b0eSEric W. Biederman last = &ents[nents - 1]; 20627bcd6b0eSEric W. Biederman while (p <= last) { 206361a28784SEric W. Biederman if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0) 206428a6d671SEric W. Biederman goto out; 206528a6d671SEric W. Biederman filp->f_pos++; 206628a6d671SEric W. Biederman p++; 206728a6d671SEric W. Biederman } 20681da177e4SLinus Torvalds } 20691da177e4SLinus Torvalds 207028a6d671SEric W. Biederman ret = 1; 207128a6d671SEric W. Biederman out: 207261a28784SEric W. Biederman put_task_struct(task); 207361a28784SEric W. Biederman out_no_task: 207428a6d671SEric W. Biederman return ret; 20751da177e4SLinus Torvalds } 20761da177e4SLinus Torvalds 20771da177e4SLinus Torvalds #ifdef CONFIG_SECURITY 207828a6d671SEric W. Biederman static ssize_t proc_pid_attr_read(struct file * file, char __user * buf, 207928a6d671SEric W. Biederman size_t count, loff_t *ppos) 208028a6d671SEric W. Biederman { 20812fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 208204ff9708SAl Viro char *p = NULL; 208328a6d671SEric W. Biederman ssize_t length; 208428a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 208528a6d671SEric W. Biederman 208628a6d671SEric W. Biederman if (!task) 208704ff9708SAl Viro return -ESRCH; 208828a6d671SEric W. Biederman 208928a6d671SEric W. Biederman length = security_getprocattr(task, 20902fddfeefSJosef "Jeff" Sipek (char*)file->f_path.dentry->d_name.name, 209104ff9708SAl Viro &p); 209228a6d671SEric W. Biederman put_task_struct(task); 209304ff9708SAl Viro if (length > 0) 209404ff9708SAl Viro length = simple_read_from_buffer(buf, count, ppos, p, length); 209504ff9708SAl Viro kfree(p); 209628a6d671SEric W. Biederman return length; 209728a6d671SEric W. Biederman } 209828a6d671SEric W. Biederman 209928a6d671SEric W. Biederman static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf, 210028a6d671SEric W. Biederman size_t count, loff_t *ppos) 210128a6d671SEric W. Biederman { 21022fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 210328a6d671SEric W. Biederman char *page; 210428a6d671SEric W. Biederman ssize_t length; 210528a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 210628a6d671SEric W. Biederman 210728a6d671SEric W. Biederman length = -ESRCH; 210828a6d671SEric W. Biederman if (!task) 210928a6d671SEric W. Biederman goto out_no_task; 211028a6d671SEric W. Biederman if (count > PAGE_SIZE) 211128a6d671SEric W. Biederman count = PAGE_SIZE; 211228a6d671SEric W. Biederman 211328a6d671SEric W. Biederman /* No partial writes. */ 211428a6d671SEric W. Biederman length = -EINVAL; 211528a6d671SEric W. Biederman if (*ppos != 0) 211628a6d671SEric W. Biederman goto out; 211728a6d671SEric W. Biederman 211828a6d671SEric W. Biederman length = -ENOMEM; 2119e12ba74dSMel Gorman page = (char*)__get_free_page(GFP_TEMPORARY); 212028a6d671SEric W. Biederman if (!page) 212128a6d671SEric W. Biederman goto out; 212228a6d671SEric W. Biederman 212328a6d671SEric W. Biederman length = -EFAULT; 212428a6d671SEric W. Biederman if (copy_from_user(page, buf, count)) 212528a6d671SEric W. Biederman goto out_free; 212628a6d671SEric W. Biederman 212728a6d671SEric W. Biederman length = security_setprocattr(task, 21282fddfeefSJosef "Jeff" Sipek (char*)file->f_path.dentry->d_name.name, 212928a6d671SEric W. Biederman (void*)page, count); 213028a6d671SEric W. Biederman out_free: 213128a6d671SEric W. Biederman free_page((unsigned long) page); 213228a6d671SEric W. Biederman out: 213328a6d671SEric W. Biederman put_task_struct(task); 213428a6d671SEric W. Biederman out_no_task: 213528a6d671SEric W. Biederman return length; 213628a6d671SEric W. Biederman } 213728a6d671SEric W. Biederman 213800977a59SArjan van de Ven static const struct file_operations proc_pid_attr_operations = { 213928a6d671SEric W. Biederman .read = proc_pid_attr_read, 214028a6d671SEric W. Biederman .write = proc_pid_attr_write, 214128a6d671SEric W. Biederman }; 214228a6d671SEric W. Biederman 2143c5141e6dSEric Dumazet static const struct pid_entry attr_dir_stuff[] = { 2144631f9c18SAlexey Dobriyan REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations), 2145631f9c18SAlexey Dobriyan REG("prev", S_IRUGO, proc_pid_attr_operations), 2146631f9c18SAlexey Dobriyan REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations), 2147631f9c18SAlexey Dobriyan REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations), 2148631f9c18SAlexey Dobriyan REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations), 2149631f9c18SAlexey Dobriyan REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations), 215028a6d671SEric W. Biederman }; 215128a6d671SEric W. Biederman 215272d9dcfcSEric W. Biederman static int proc_attr_dir_readdir(struct file * filp, 21531da177e4SLinus Torvalds void * dirent, filldir_t filldir) 21541da177e4SLinus Torvalds { 21551da177e4SLinus Torvalds return proc_pident_readdir(filp,dirent,filldir, 215672d9dcfcSEric W. Biederman attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff)); 21571da177e4SLinus Torvalds } 21581da177e4SLinus Torvalds 215900977a59SArjan van de Ven static const struct file_operations proc_attr_dir_operations = { 21601da177e4SLinus Torvalds .read = generic_read_dir, 216172d9dcfcSEric W. Biederman .readdir = proc_attr_dir_readdir, 21621da177e4SLinus Torvalds }; 21631da177e4SLinus Torvalds 216472d9dcfcSEric W. Biederman static struct dentry *proc_attr_dir_lookup(struct inode *dir, 21651da177e4SLinus Torvalds struct dentry *dentry, struct nameidata *nd) 21661da177e4SLinus Torvalds { 21677bcd6b0eSEric W. Biederman return proc_pident_lookup(dir, dentry, 21687bcd6b0eSEric W. Biederman attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff)); 21691da177e4SLinus Torvalds } 21701da177e4SLinus Torvalds 2171c5ef1c42SArjan van de Ven static const struct inode_operations proc_attr_dir_inode_operations = { 217272d9dcfcSEric W. Biederman .lookup = proc_attr_dir_lookup, 217399f89551SEric W. Biederman .getattr = pid_getattr, 21746d76fa58SLinus Torvalds .setattr = proc_setattr, 21751da177e4SLinus Torvalds }; 21761da177e4SLinus Torvalds 21771da177e4SLinus Torvalds #endif 21781da177e4SLinus Torvalds 21793cb4a0bbSKawai, Hidehiro #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE) 21803cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf, 21813cb4a0bbSKawai, Hidehiro size_t count, loff_t *ppos) 21823cb4a0bbSKawai, Hidehiro { 21833cb4a0bbSKawai, Hidehiro struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 21843cb4a0bbSKawai, Hidehiro struct mm_struct *mm; 21853cb4a0bbSKawai, Hidehiro char buffer[PROC_NUMBUF]; 21863cb4a0bbSKawai, Hidehiro size_t len; 21873cb4a0bbSKawai, Hidehiro int ret; 21883cb4a0bbSKawai, Hidehiro 21893cb4a0bbSKawai, Hidehiro if (!task) 21903cb4a0bbSKawai, Hidehiro return -ESRCH; 21913cb4a0bbSKawai, Hidehiro 21923cb4a0bbSKawai, Hidehiro ret = 0; 21933cb4a0bbSKawai, Hidehiro mm = get_task_mm(task); 21943cb4a0bbSKawai, Hidehiro if (mm) { 21953cb4a0bbSKawai, Hidehiro len = snprintf(buffer, sizeof(buffer), "%08lx\n", 21963cb4a0bbSKawai, Hidehiro ((mm->flags & MMF_DUMP_FILTER_MASK) >> 21973cb4a0bbSKawai, Hidehiro MMF_DUMP_FILTER_SHIFT)); 21983cb4a0bbSKawai, Hidehiro mmput(mm); 21993cb4a0bbSKawai, Hidehiro ret = simple_read_from_buffer(buf, count, ppos, buffer, len); 22003cb4a0bbSKawai, Hidehiro } 22013cb4a0bbSKawai, Hidehiro 22023cb4a0bbSKawai, Hidehiro put_task_struct(task); 22033cb4a0bbSKawai, Hidehiro 22043cb4a0bbSKawai, Hidehiro return ret; 22053cb4a0bbSKawai, Hidehiro } 22063cb4a0bbSKawai, Hidehiro 22073cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_write(struct file *file, 22083cb4a0bbSKawai, Hidehiro const char __user *buf, 22093cb4a0bbSKawai, Hidehiro size_t count, 22103cb4a0bbSKawai, Hidehiro loff_t *ppos) 22113cb4a0bbSKawai, Hidehiro { 22123cb4a0bbSKawai, Hidehiro struct task_struct *task; 22133cb4a0bbSKawai, Hidehiro struct mm_struct *mm; 22143cb4a0bbSKawai, Hidehiro char buffer[PROC_NUMBUF], *end; 22153cb4a0bbSKawai, Hidehiro unsigned int val; 22163cb4a0bbSKawai, Hidehiro int ret; 22173cb4a0bbSKawai, Hidehiro int i; 22183cb4a0bbSKawai, Hidehiro unsigned long mask; 22193cb4a0bbSKawai, Hidehiro 22203cb4a0bbSKawai, Hidehiro ret = -EFAULT; 22213cb4a0bbSKawai, Hidehiro memset(buffer, 0, sizeof(buffer)); 22223cb4a0bbSKawai, Hidehiro if (count > sizeof(buffer) - 1) 22233cb4a0bbSKawai, Hidehiro count = sizeof(buffer) - 1; 22243cb4a0bbSKawai, Hidehiro if (copy_from_user(buffer, buf, count)) 22253cb4a0bbSKawai, Hidehiro goto out_no_task; 22263cb4a0bbSKawai, Hidehiro 22273cb4a0bbSKawai, Hidehiro ret = -EINVAL; 22283cb4a0bbSKawai, Hidehiro val = (unsigned int)simple_strtoul(buffer, &end, 0); 22293cb4a0bbSKawai, Hidehiro if (*end == '\n') 22303cb4a0bbSKawai, Hidehiro end++; 22313cb4a0bbSKawai, Hidehiro if (end - buffer == 0) 22323cb4a0bbSKawai, Hidehiro goto out_no_task; 22333cb4a0bbSKawai, Hidehiro 22343cb4a0bbSKawai, Hidehiro ret = -ESRCH; 22353cb4a0bbSKawai, Hidehiro task = get_proc_task(file->f_dentry->d_inode); 22363cb4a0bbSKawai, Hidehiro if (!task) 22373cb4a0bbSKawai, Hidehiro goto out_no_task; 22383cb4a0bbSKawai, Hidehiro 22393cb4a0bbSKawai, Hidehiro ret = end - buffer; 22403cb4a0bbSKawai, Hidehiro mm = get_task_mm(task); 22413cb4a0bbSKawai, Hidehiro if (!mm) 22423cb4a0bbSKawai, Hidehiro goto out_no_mm; 22433cb4a0bbSKawai, Hidehiro 22443cb4a0bbSKawai, Hidehiro for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) { 22453cb4a0bbSKawai, Hidehiro if (val & mask) 22463cb4a0bbSKawai, Hidehiro set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags); 22473cb4a0bbSKawai, Hidehiro else 22483cb4a0bbSKawai, Hidehiro clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags); 22493cb4a0bbSKawai, Hidehiro } 22503cb4a0bbSKawai, Hidehiro 22513cb4a0bbSKawai, Hidehiro mmput(mm); 22523cb4a0bbSKawai, Hidehiro out_no_mm: 22533cb4a0bbSKawai, Hidehiro put_task_struct(task); 22543cb4a0bbSKawai, Hidehiro out_no_task: 22553cb4a0bbSKawai, Hidehiro return ret; 22563cb4a0bbSKawai, Hidehiro } 22573cb4a0bbSKawai, Hidehiro 22583cb4a0bbSKawai, Hidehiro static const struct file_operations proc_coredump_filter_operations = { 22593cb4a0bbSKawai, Hidehiro .read = proc_coredump_filter_read, 22603cb4a0bbSKawai, Hidehiro .write = proc_coredump_filter_write, 22613cb4a0bbSKawai, Hidehiro }; 22623cb4a0bbSKawai, Hidehiro #endif 22633cb4a0bbSKawai, Hidehiro 22641da177e4SLinus Torvalds /* 22651da177e4SLinus Torvalds * /proc/self: 22661da177e4SLinus Torvalds */ 22671da177e4SLinus Torvalds static int proc_self_readlink(struct dentry *dentry, char __user *buffer, 22681da177e4SLinus Torvalds int buflen) 22691da177e4SLinus Torvalds { 2270488e5bc4SEric W. Biederman struct pid_namespace *ns = dentry->d_sb->s_fs_info; 2271b55fcb22SAndrew Morton pid_t tgid = task_tgid_nr_ns(current, ns); 22728578cea7SEric W. Biederman char tmp[PROC_NUMBUF]; 2273b55fcb22SAndrew Morton if (!tgid) 2274488e5bc4SEric W. Biederman return -ENOENT; 2275b55fcb22SAndrew Morton sprintf(tmp, "%d", tgid); 22761da177e4SLinus Torvalds return vfs_readlink(dentry,buffer,buflen,tmp); 22771da177e4SLinus Torvalds } 22781da177e4SLinus Torvalds 2279008b150aSAl Viro static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd) 22801da177e4SLinus Torvalds { 2281488e5bc4SEric W. Biederman struct pid_namespace *ns = dentry->d_sb->s_fs_info; 2282b55fcb22SAndrew Morton pid_t tgid = task_tgid_nr_ns(current, ns); 22838578cea7SEric W. Biederman char tmp[PROC_NUMBUF]; 2284b55fcb22SAndrew Morton if (!tgid) 2285488e5bc4SEric W. Biederman return ERR_PTR(-ENOENT); 2286b55fcb22SAndrew Morton sprintf(tmp, "%d", task_tgid_nr_ns(current, ns)); 2287008b150aSAl Viro return ERR_PTR(vfs_follow_link(nd,tmp)); 22881da177e4SLinus Torvalds } 22891da177e4SLinus Torvalds 2290c5ef1c42SArjan van de Ven static const struct inode_operations proc_self_inode_operations = { 22911da177e4SLinus Torvalds .readlink = proc_self_readlink, 22921da177e4SLinus Torvalds .follow_link = proc_self_follow_link, 22931da177e4SLinus Torvalds }; 22941da177e4SLinus Torvalds 229528a6d671SEric W. Biederman /* 2296801199ceSEric W. Biederman * proc base 2297801199ceSEric W. Biederman * 2298801199ceSEric W. Biederman * These are the directory entries in the root directory of /proc 2299801199ceSEric W. Biederman * that properly belong to the /proc filesystem, as they describe 2300801199ceSEric W. Biederman * describe something that is process related. 2301801199ceSEric W. Biederman */ 2302c5141e6dSEric Dumazet static const struct pid_entry proc_base_stuff[] = { 230361a28784SEric W. Biederman NOD("self", S_IFLNK|S_IRWXUGO, 2304801199ceSEric W. Biederman &proc_self_inode_operations, NULL, {}), 2305801199ceSEric W. Biederman }; 2306801199ceSEric W. Biederman 2307801199ceSEric W. Biederman /* 2308801199ceSEric W. Biederman * Exceptional case: normally we are not allowed to unhash a busy 2309801199ceSEric W. Biederman * directory. In this case, however, we can do it - no aliasing problems 2310801199ceSEric W. Biederman * due to the way we treat inodes. 2311801199ceSEric W. Biederman */ 2312801199ceSEric W. Biederman static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd) 2313801199ceSEric W. Biederman { 2314801199ceSEric W. Biederman struct inode *inode = dentry->d_inode; 2315801199ceSEric W. Biederman struct task_struct *task = get_proc_task(inode); 2316801199ceSEric W. Biederman if (task) { 2317801199ceSEric W. Biederman put_task_struct(task); 2318801199ceSEric W. Biederman return 1; 2319801199ceSEric W. Biederman } 2320801199ceSEric W. Biederman d_drop(dentry); 2321801199ceSEric W. Biederman return 0; 2322801199ceSEric W. Biederman } 2323801199ceSEric W. Biederman 2324d72f71ebSAl Viro static const struct dentry_operations proc_base_dentry_operations = 2325801199ceSEric W. Biederman { 2326801199ceSEric W. Biederman .d_revalidate = proc_base_revalidate, 2327801199ceSEric W. Biederman .d_delete = pid_delete_dentry, 2328801199ceSEric W. Biederman }; 2329801199ceSEric W. Biederman 2330444ceed8SEric W. Biederman static struct dentry *proc_base_instantiate(struct inode *dir, 2331c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 2332801199ceSEric W. Biederman { 2333c5141e6dSEric Dumazet const struct pid_entry *p = ptr; 2334801199ceSEric W. Biederman struct inode *inode; 2335801199ceSEric W. Biederman struct proc_inode *ei; 2336444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-EINVAL); 2337801199ceSEric W. Biederman 2338801199ceSEric W. Biederman /* Allocate the inode */ 2339801199ceSEric W. Biederman error = ERR_PTR(-ENOMEM); 2340801199ceSEric W. Biederman inode = new_inode(dir->i_sb); 2341801199ceSEric W. Biederman if (!inode) 2342801199ceSEric W. Biederman goto out; 2343801199ceSEric W. Biederman 2344801199ceSEric W. Biederman /* Initialize the inode */ 2345801199ceSEric W. Biederman ei = PROC_I(inode); 2346801199ceSEric W. Biederman inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; 2347801199ceSEric W. Biederman 2348801199ceSEric W. Biederman /* 2349801199ceSEric W. Biederman * grab the reference to the task. 2350801199ceSEric W. Biederman */ 23511a657f78SOleg Nesterov ei->pid = get_task_pid(task, PIDTYPE_PID); 2352801199ceSEric W. Biederman if (!ei->pid) 2353801199ceSEric W. Biederman goto out_iput; 2354801199ceSEric W. Biederman 2355801199ceSEric W. Biederman inode->i_mode = p->mode; 2356801199ceSEric W. Biederman if (S_ISDIR(inode->i_mode)) 2357801199ceSEric W. Biederman inode->i_nlink = 2; 2358801199ceSEric W. Biederman if (S_ISLNK(inode->i_mode)) 2359801199ceSEric W. Biederman inode->i_size = 64; 2360801199ceSEric W. Biederman if (p->iop) 2361801199ceSEric W. Biederman inode->i_op = p->iop; 2362801199ceSEric W. Biederman if (p->fop) 2363801199ceSEric W. Biederman inode->i_fop = p->fop; 2364801199ceSEric W. Biederman ei->op = p->op; 2365801199ceSEric W. Biederman dentry->d_op = &proc_base_dentry_operations; 2366801199ceSEric W. Biederman d_add(dentry, inode); 2367801199ceSEric W. Biederman error = NULL; 2368801199ceSEric W. Biederman out: 2369801199ceSEric W. Biederman return error; 2370801199ceSEric W. Biederman out_iput: 2371801199ceSEric W. Biederman iput(inode); 2372801199ceSEric W. Biederman goto out; 2373801199ceSEric W. Biederman } 2374801199ceSEric W. Biederman 2375444ceed8SEric W. Biederman static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry) 2376444ceed8SEric W. Biederman { 2377444ceed8SEric W. Biederman struct dentry *error; 2378444ceed8SEric W. Biederman struct task_struct *task = get_proc_task(dir); 2379c5141e6dSEric Dumazet const struct pid_entry *p, *last; 2380444ceed8SEric W. Biederman 2381444ceed8SEric W. Biederman error = ERR_PTR(-ENOENT); 2382444ceed8SEric W. Biederman 2383444ceed8SEric W. Biederman if (!task) 2384444ceed8SEric W. Biederman goto out_no_task; 2385444ceed8SEric W. Biederman 2386444ceed8SEric W. Biederman /* Lookup the directory entry */ 23877bcd6b0eSEric W. Biederman last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1]; 23887bcd6b0eSEric W. Biederman for (p = proc_base_stuff; p <= last; p++) { 2389444ceed8SEric W. Biederman if (p->len != dentry->d_name.len) 2390444ceed8SEric W. Biederman continue; 2391444ceed8SEric W. Biederman if (!memcmp(dentry->d_name.name, p->name, p->len)) 2392444ceed8SEric W. Biederman break; 2393444ceed8SEric W. Biederman } 23947bcd6b0eSEric W. Biederman if (p > last) 2395444ceed8SEric W. Biederman goto out; 2396444ceed8SEric W. Biederman 2397444ceed8SEric W. Biederman error = proc_base_instantiate(dir, dentry, task, p); 2398444ceed8SEric W. Biederman 2399444ceed8SEric W. Biederman out: 2400444ceed8SEric W. Biederman put_task_struct(task); 2401444ceed8SEric W. Biederman out_no_task: 2402444ceed8SEric W. Biederman return error; 2403444ceed8SEric W. Biederman } 2404444ceed8SEric W. Biederman 2405c5141e6dSEric Dumazet static int proc_base_fill_cache(struct file *filp, void *dirent, 2406c5141e6dSEric Dumazet filldir_t filldir, struct task_struct *task, const struct pid_entry *p) 240761a28784SEric W. Biederman { 240861a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, p->name, p->len, 240961a28784SEric W. Biederman proc_base_instantiate, task, p); 241061a28784SEric W. Biederman } 241161a28784SEric W. Biederman 2412aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING 2413297c5d92SAndrea Righi static int do_io_accounting(struct task_struct *task, char *buffer, int whole) 2414aba76fdbSAndrew Morton { 2415940389b8SAndrea Righi struct task_io_accounting acct = task->ioac; 2416297c5d92SAndrea Righi unsigned long flags; 2417297c5d92SAndrea Righi 24185995477aSAndrea Righi if (whole && lock_task_sighand(task, &flags)) { 2419b2d002dbSAndrea Righi struct task_struct *t = task; 2420297c5d92SAndrea Righi 24215995477aSAndrea Righi task_io_accounting_add(&acct, &task->signal->ioac); 24225995477aSAndrea Righi while_each_thread(task, t) 24235995477aSAndrea Righi task_io_accounting_add(&acct, &t->ioac); 2424297c5d92SAndrea Righi 2425297c5d92SAndrea Righi unlock_task_sighand(task, &flags); 2426297c5d92SAndrea Righi } 2427aba76fdbSAndrew Morton return sprintf(buffer, 2428aba76fdbSAndrew Morton "rchar: %llu\n" 2429aba76fdbSAndrew Morton "wchar: %llu\n" 2430aba76fdbSAndrew Morton "syscr: %llu\n" 2431aba76fdbSAndrew Morton "syscw: %llu\n" 2432aba76fdbSAndrew Morton "read_bytes: %llu\n" 2433aba76fdbSAndrew Morton "write_bytes: %llu\n" 2434aba76fdbSAndrew Morton "cancelled_write_bytes: %llu\n", 24357c44319dSAlexander Beregalov (unsigned long long)acct.rchar, 24367c44319dSAlexander Beregalov (unsigned long long)acct.wchar, 24377c44319dSAlexander Beregalov (unsigned long long)acct.syscr, 24387c44319dSAlexander Beregalov (unsigned long long)acct.syscw, 24397c44319dSAlexander Beregalov (unsigned long long)acct.read_bytes, 24407c44319dSAlexander Beregalov (unsigned long long)acct.write_bytes, 24417c44319dSAlexander Beregalov (unsigned long long)acct.cancelled_write_bytes); 2442aba76fdbSAndrew Morton } 2443297c5d92SAndrea Righi 2444297c5d92SAndrea Righi static int proc_tid_io_accounting(struct task_struct *task, char *buffer) 2445297c5d92SAndrea Righi { 2446297c5d92SAndrea Righi return do_io_accounting(task, buffer, 0); 2447297c5d92SAndrea Righi } 2448297c5d92SAndrea Righi 2449297c5d92SAndrea Righi static int proc_tgid_io_accounting(struct task_struct *task, char *buffer) 2450297c5d92SAndrea Righi { 2451297c5d92SAndrea Righi return do_io_accounting(task, buffer, 1); 2452297c5d92SAndrea Righi } 2453297c5d92SAndrea Righi #endif /* CONFIG_TASK_IO_ACCOUNTING */ 2454aba76fdbSAndrew Morton 245547830723SKees Cook static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns, 245647830723SKees Cook struct pid *pid, struct task_struct *task) 245747830723SKees Cook { 245847830723SKees Cook seq_printf(m, "%08x\n", task->personality); 245947830723SKees Cook return 0; 246047830723SKees Cook } 246147830723SKees Cook 2462801199ceSEric W. Biederman /* 246328a6d671SEric W. Biederman * Thread groups 246428a6d671SEric W. Biederman */ 246500977a59SArjan van de Ven static const struct file_operations proc_task_operations; 2466c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations; 246720cdc894SEric W. Biederman 2468c5141e6dSEric Dumazet static const struct pid_entry tgid_base_stuff[] = { 2469631f9c18SAlexey Dobriyan DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations), 2470631f9c18SAlexey Dobriyan DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations), 2471631f9c18SAlexey Dobriyan DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations), 2472b2211a36SAndrew Morton #ifdef CONFIG_NET 2473631f9c18SAlexey Dobriyan DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations), 2474b2211a36SAndrew Morton #endif 2475631f9c18SAlexey Dobriyan REG("environ", S_IRUSR, proc_environ_operations), 2476631f9c18SAlexey Dobriyan INF("auxv", S_IRUSR, proc_pid_auxv), 2477631f9c18SAlexey Dobriyan ONE("status", S_IRUGO, proc_pid_status), 2478631f9c18SAlexey Dobriyan ONE("personality", S_IRUSR, proc_pid_personality), 2479631f9c18SAlexey Dobriyan INF("limits", S_IRUSR, proc_pid_limits), 248043ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG 2481631f9c18SAlexey Dobriyan REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations), 248243ae34cbSIngo Molnar #endif 2483ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK 2484631f9c18SAlexey Dobriyan INF("syscall", S_IRUSR, proc_pid_syscall), 2485ebcb6734SRoland McGrath #endif 2486631f9c18SAlexey Dobriyan INF("cmdline", S_IRUGO, proc_pid_cmdline), 2487631f9c18SAlexey Dobriyan ONE("stat", S_IRUGO, proc_tgid_stat), 2488631f9c18SAlexey Dobriyan ONE("statm", S_IRUGO, proc_pid_statm), 2489631f9c18SAlexey Dobriyan REG("maps", S_IRUGO, proc_maps_operations), 249028a6d671SEric W. Biederman #ifdef CONFIG_NUMA 2491631f9c18SAlexey Dobriyan REG("numa_maps", S_IRUGO, proc_numa_maps_operations), 249228a6d671SEric W. Biederman #endif 2493631f9c18SAlexey Dobriyan REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations), 2494631f9c18SAlexey Dobriyan LNK("cwd", proc_cwd_link), 2495631f9c18SAlexey Dobriyan LNK("root", proc_root_link), 2496631f9c18SAlexey Dobriyan LNK("exe", proc_exe_link), 2497631f9c18SAlexey Dobriyan REG("mounts", S_IRUGO, proc_mounts_operations), 2498631f9c18SAlexey Dobriyan REG("mountinfo", S_IRUGO, proc_mountinfo_operations), 2499631f9c18SAlexey Dobriyan REG("mountstats", S_IRUSR, proc_mountstats_operations), 25001e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR 2501631f9c18SAlexey Dobriyan REG("clear_refs", S_IWUSR, proc_clear_refs_operations), 2502631f9c18SAlexey Dobriyan REG("smaps", S_IRUGO, proc_smaps_operations), 2503631f9c18SAlexey Dobriyan REG("pagemap", S_IRUSR, proc_pagemap_operations), 250428a6d671SEric W. Biederman #endif 250528a6d671SEric W. Biederman #ifdef CONFIG_SECURITY 2506631f9c18SAlexey Dobriyan DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations), 250728a6d671SEric W. Biederman #endif 250828a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS 2509631f9c18SAlexey Dobriyan INF("wchan", S_IRUGO, proc_pid_wchan), 251028a6d671SEric W. Biederman #endif 25112ec220e2SKen Chen #ifdef CONFIG_STACKTRACE 25122ec220e2SKen Chen ONE("stack", S_IRUSR, proc_pid_stack), 251328a6d671SEric W. Biederman #endif 251428a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS 2515631f9c18SAlexey Dobriyan INF("schedstat", S_IRUGO, proc_pid_schedstat), 251628a6d671SEric W. Biederman #endif 25179745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP 2518631f9c18SAlexey Dobriyan REG("latency", S_IRUGO, proc_lstats_operations), 25199745512cSArjan van de Ven #endif 25208793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET 2521631f9c18SAlexey Dobriyan REG("cpuset", S_IRUGO, proc_cpuset_operations), 252228a6d671SEric W. Biederman #endif 2523a424316cSPaul Menage #ifdef CONFIG_CGROUPS 2524631f9c18SAlexey Dobriyan REG("cgroup", S_IRUGO, proc_cgroup_operations), 2525a424316cSPaul Menage #endif 2526631f9c18SAlexey Dobriyan INF("oom_score", S_IRUGO, proc_oom_score), 2527631f9c18SAlexey Dobriyan REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations), 252828a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL 2529631f9c18SAlexey Dobriyan REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations), 2530631f9c18SAlexey Dobriyan REG("sessionid", S_IRUGO, proc_sessionid_operations), 253128a6d671SEric W. Biederman #endif 2532f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION 2533631f9c18SAlexey Dobriyan REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations), 2534f4f154fdSAkinobu Mita #endif 25353cb4a0bbSKawai, Hidehiro #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE) 2536631f9c18SAlexey Dobriyan REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations), 25373cb4a0bbSKawai, Hidehiro #endif 2538aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING 2539631f9c18SAlexey Dobriyan INF("io", S_IRUGO, proc_tgid_io_accounting), 2540aba76fdbSAndrew Morton #endif 254128a6d671SEric W. Biederman }; 254228a6d671SEric W. Biederman 254328a6d671SEric W. Biederman static int proc_tgid_base_readdir(struct file * filp, 254428a6d671SEric W. Biederman void * dirent, filldir_t filldir) 254528a6d671SEric W. Biederman { 254628a6d671SEric W. Biederman return proc_pident_readdir(filp,dirent,filldir, 254728a6d671SEric W. Biederman tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff)); 254828a6d671SEric W. Biederman } 254928a6d671SEric W. Biederman 255000977a59SArjan van de Ven static const struct file_operations proc_tgid_base_operations = { 255128a6d671SEric W. Biederman .read = generic_read_dir, 255228a6d671SEric W. Biederman .readdir = proc_tgid_base_readdir, 255328a6d671SEric W. Biederman }; 255428a6d671SEric W. Biederman 255528a6d671SEric W. Biederman static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){ 25567bcd6b0eSEric W. Biederman return proc_pident_lookup(dir, dentry, 25577bcd6b0eSEric W. Biederman tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff)); 255828a6d671SEric W. Biederman } 255928a6d671SEric W. Biederman 2560c5ef1c42SArjan van de Ven static const struct inode_operations proc_tgid_base_inode_operations = { 256128a6d671SEric W. Biederman .lookup = proc_tgid_base_lookup, 256228a6d671SEric W. Biederman .getattr = pid_getattr, 256328a6d671SEric W. Biederman .setattr = proc_setattr, 256428a6d671SEric W. Biederman }; 256528a6d671SEric W. Biederman 256660347f67SPavel Emelyanov static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid) 25671da177e4SLinus Torvalds { 256848e6484dSEric W. Biederman struct dentry *dentry, *leader, *dir; 25698578cea7SEric W. Biederman char buf[PROC_NUMBUF]; 257048e6484dSEric W. Biederman struct qstr name; 25711da177e4SLinus Torvalds 257248e6484dSEric W. Biederman name.name = buf; 257360347f67SPavel Emelyanov name.len = snprintf(buf, sizeof(buf), "%d", pid); 257460347f67SPavel Emelyanov dentry = d_hash_and_lookup(mnt->mnt_root, &name); 257548e6484dSEric W. Biederman if (dentry) { 25767766755aSAndrea Arcangeli if (!(current->flags & PF_EXITING)) 257748e6484dSEric W. Biederman shrink_dcache_parent(dentry); 257848e6484dSEric W. Biederman d_drop(dentry); 257948e6484dSEric W. Biederman dput(dentry); 25801da177e4SLinus Torvalds } 25811da177e4SLinus Torvalds 258260347f67SPavel Emelyanov if (tgid == 0) 258348e6484dSEric W. Biederman goto out; 25841da177e4SLinus Torvalds 258548e6484dSEric W. Biederman name.name = buf; 258660347f67SPavel Emelyanov name.len = snprintf(buf, sizeof(buf), "%d", tgid); 258760347f67SPavel Emelyanov leader = d_hash_and_lookup(mnt->mnt_root, &name); 258848e6484dSEric W. Biederman if (!leader) 258948e6484dSEric W. Biederman goto out; 259048e6484dSEric W. Biederman 259148e6484dSEric W. Biederman name.name = "task"; 259248e6484dSEric W. Biederman name.len = strlen(name.name); 259348e6484dSEric W. Biederman dir = d_hash_and_lookup(leader, &name); 259448e6484dSEric W. Biederman if (!dir) 259548e6484dSEric W. Biederman goto out_put_leader; 259648e6484dSEric W. Biederman 259748e6484dSEric W. Biederman name.name = buf; 259860347f67SPavel Emelyanov name.len = snprintf(buf, sizeof(buf), "%d", pid); 259948e6484dSEric W. Biederman dentry = d_hash_and_lookup(dir, &name); 260048e6484dSEric W. Biederman if (dentry) { 260148e6484dSEric W. Biederman shrink_dcache_parent(dentry); 260248e6484dSEric W. Biederman d_drop(dentry); 260348e6484dSEric W. Biederman dput(dentry); 26041da177e4SLinus Torvalds } 260548e6484dSEric W. Biederman 260648e6484dSEric W. Biederman dput(dir); 260748e6484dSEric W. Biederman out_put_leader: 260848e6484dSEric W. Biederman dput(leader); 260948e6484dSEric W. Biederman out: 261048e6484dSEric W. Biederman return; 26111da177e4SLinus Torvalds } 26121da177e4SLinus Torvalds 26130895e91dSRandy Dunlap /** 26140895e91dSRandy Dunlap * proc_flush_task - Remove dcache entries for @task from the /proc dcache. 26150895e91dSRandy Dunlap * @task: task that should be flushed. 26160895e91dSRandy Dunlap * 26170895e91dSRandy Dunlap * When flushing dentries from proc, one needs to flush them from global 261860347f67SPavel Emelyanov * proc (proc_mnt) and from all the namespaces' procs this task was seen 26190895e91dSRandy Dunlap * in. This call is supposed to do all of this job. 26200895e91dSRandy Dunlap * 26210895e91dSRandy Dunlap * Looks in the dcache for 26220895e91dSRandy Dunlap * /proc/@pid 26230895e91dSRandy Dunlap * /proc/@tgid/task/@pid 26240895e91dSRandy Dunlap * if either directory is present flushes it and all of it'ts children 26250895e91dSRandy Dunlap * from the dcache. 26260895e91dSRandy Dunlap * 26270895e91dSRandy Dunlap * It is safe and reasonable to cache /proc entries for a task until 26280895e91dSRandy Dunlap * that task exits. After that they just clog up the dcache with 26290895e91dSRandy Dunlap * useless entries, possibly causing useful dcache entries to be 26300895e91dSRandy Dunlap * flushed instead. This routine is proved to flush those useless 26310895e91dSRandy Dunlap * dcache entries at process exit time. 26320895e91dSRandy Dunlap * 26330895e91dSRandy Dunlap * NOTE: This routine is just an optimization so it does not guarantee 26340895e91dSRandy Dunlap * that no dcache entries will exist at process exit time it 26350895e91dSRandy Dunlap * just makes it very unlikely that any will persist. 263660347f67SPavel Emelyanov */ 263760347f67SPavel Emelyanov 263860347f67SPavel Emelyanov void proc_flush_task(struct task_struct *task) 263960347f67SPavel Emelyanov { 26409fcc2d15SEric W. Biederman int i; 26419fcc2d15SEric W. Biederman struct pid *pid, *tgid = NULL; 2642130f77ecSPavel Emelyanov struct upid *upid; 2643130f77ecSPavel Emelyanov 2644130f77ecSPavel Emelyanov pid = task_pid(task); 26459fcc2d15SEric W. Biederman if (thread_group_leader(task)) 2646130f77ecSPavel Emelyanov tgid = task_tgid(task); 26479fcc2d15SEric W. Biederman 26489fcc2d15SEric W. Biederman for (i = 0; i <= pid->level; i++) { 2649130f77ecSPavel Emelyanov upid = &pid->numbers[i]; 2650130f77ecSPavel Emelyanov proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr, 26519fcc2d15SEric W. Biederman tgid ? tgid->numbers[i].nr : 0); 2652130f77ecSPavel Emelyanov } 26536f4e6433SPavel Emelyanov 26546f4e6433SPavel Emelyanov upid = &pid->numbers[pid->level]; 26556f4e6433SPavel Emelyanov if (upid->nr == 1) 26566f4e6433SPavel Emelyanov pid_ns_release_proc(upid->ns); 265760347f67SPavel Emelyanov } 265860347f67SPavel Emelyanov 26599711ef99SAdrian Bunk static struct dentry *proc_pid_instantiate(struct inode *dir, 26609711ef99SAdrian Bunk struct dentry * dentry, 2661c5141e6dSEric Dumazet struct task_struct *task, const void *ptr) 2662444ceed8SEric W. Biederman { 2663444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-ENOENT); 2664444ceed8SEric W. Biederman struct inode *inode; 2665444ceed8SEric W. Biederman 266661a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 2667444ceed8SEric W. Biederman if (!inode) 2668444ceed8SEric W. Biederman goto out; 2669444ceed8SEric W. Biederman 2670444ceed8SEric W. Biederman inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO; 2671444ceed8SEric W. Biederman inode->i_op = &proc_tgid_base_inode_operations; 2672444ceed8SEric W. Biederman inode->i_fop = &proc_tgid_base_operations; 2673444ceed8SEric W. Biederman inode->i_flags|=S_IMMUTABLE; 2674aed54175SVegard Nossum 2675aed54175SVegard Nossum inode->i_nlink = 2 + pid_entry_count_dirs(tgid_base_stuff, 2676aed54175SVegard Nossum ARRAY_SIZE(tgid_base_stuff)); 2677444ceed8SEric W. Biederman 2678444ceed8SEric W. Biederman dentry->d_op = &pid_dentry_operations; 2679444ceed8SEric W. Biederman 2680444ceed8SEric W. Biederman d_add(dentry, inode); 2681444ceed8SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 2682444ceed8SEric W. Biederman if (pid_revalidate(dentry, NULL)) 2683444ceed8SEric W. Biederman error = NULL; 2684444ceed8SEric W. Biederman out: 2685444ceed8SEric W. Biederman return error; 2686444ceed8SEric W. Biederman } 2687444ceed8SEric W. Biederman 26881da177e4SLinus Torvalds struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd) 26891da177e4SLinus Torvalds { 2690cd6a3ce9SEric W. Biederman struct dentry *result = ERR_PTR(-ENOENT); 26911da177e4SLinus Torvalds struct task_struct *task; 26921da177e4SLinus Torvalds unsigned tgid; 2693b488893aSPavel Emelyanov struct pid_namespace *ns; 26941da177e4SLinus Torvalds 2695801199ceSEric W. Biederman result = proc_base_lookup(dir, dentry); 2696801199ceSEric W. Biederman if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT) 2697801199ceSEric W. Biederman goto out; 2698801199ceSEric W. Biederman 26991da177e4SLinus Torvalds tgid = name_to_int(dentry); 27001da177e4SLinus Torvalds if (tgid == ~0U) 27011da177e4SLinus Torvalds goto out; 27021da177e4SLinus Torvalds 2703b488893aSPavel Emelyanov ns = dentry->d_sb->s_fs_info; 2704de758734SEric W. Biederman rcu_read_lock(); 2705b488893aSPavel Emelyanov task = find_task_by_pid_ns(tgid, ns); 27061da177e4SLinus Torvalds if (task) 27071da177e4SLinus Torvalds get_task_struct(task); 2708de758734SEric W. Biederman rcu_read_unlock(); 27091da177e4SLinus Torvalds if (!task) 27101da177e4SLinus Torvalds goto out; 27111da177e4SLinus Torvalds 2712444ceed8SEric W. Biederman result = proc_pid_instantiate(dir, dentry, task, NULL); 271348e6484dSEric W. Biederman put_task_struct(task); 27141da177e4SLinus Torvalds out: 2715cd6a3ce9SEric W. Biederman return result; 27161da177e4SLinus Torvalds } 27171da177e4SLinus Torvalds 27181da177e4SLinus Torvalds /* 27190804ef4bSEric W. Biederman * Find the first task with tgid >= tgid 27200bc58a91SEric W. Biederman * 27211da177e4SLinus Torvalds */ 272219fd4bb2SEric W. Biederman struct tgid_iter { 272319fd4bb2SEric W. Biederman unsigned int tgid; 27240804ef4bSEric W. Biederman struct task_struct *task; 272519fd4bb2SEric W. Biederman }; 272619fd4bb2SEric W. Biederman static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter) 272719fd4bb2SEric W. Biederman { 27280804ef4bSEric W. Biederman struct pid *pid; 27291da177e4SLinus Torvalds 273019fd4bb2SEric W. Biederman if (iter.task) 273119fd4bb2SEric W. Biederman put_task_struct(iter.task); 27320804ef4bSEric W. Biederman rcu_read_lock(); 27330804ef4bSEric W. Biederman retry: 273419fd4bb2SEric W. Biederman iter.task = NULL; 273519fd4bb2SEric W. Biederman pid = find_ge_pid(iter.tgid, ns); 27360804ef4bSEric W. Biederman if (pid) { 273719fd4bb2SEric W. Biederman iter.tgid = pid_nr_ns(pid, ns); 273819fd4bb2SEric W. Biederman iter.task = pid_task(pid, PIDTYPE_PID); 27390804ef4bSEric W. Biederman /* What we to know is if the pid we have find is the 27400804ef4bSEric W. Biederman * pid of a thread_group_leader. Testing for task 27410804ef4bSEric W. Biederman * being a thread_group_leader is the obvious thing 27420804ef4bSEric W. Biederman * todo but there is a window when it fails, due to 27430804ef4bSEric W. Biederman * the pid transfer logic in de_thread. 27440804ef4bSEric W. Biederman * 27450804ef4bSEric W. Biederman * So we perform the straight forward test of seeing 27460804ef4bSEric W. Biederman * if the pid we have found is the pid of a thread 27470804ef4bSEric W. Biederman * group leader, and don't worry if the task we have 27480804ef4bSEric W. Biederman * found doesn't happen to be a thread group leader. 27490804ef4bSEric W. Biederman * As we don't care in the case of readdir. 27500bc58a91SEric W. Biederman */ 275119fd4bb2SEric W. Biederman if (!iter.task || !has_group_leader_pid(iter.task)) { 275219fd4bb2SEric W. Biederman iter.tgid += 1; 27530804ef4bSEric W. Biederman goto retry; 275419fd4bb2SEric W. Biederman } 275519fd4bb2SEric W. Biederman get_task_struct(iter.task); 27561da177e4SLinus Torvalds } 2757454cc105SEric W. Biederman rcu_read_unlock(); 275819fd4bb2SEric W. Biederman return iter; 27591da177e4SLinus Torvalds } 27601da177e4SLinus Torvalds 27617bcd6b0eSEric W. Biederman #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff)) 27621da177e4SLinus Torvalds 276361a28784SEric W. Biederman static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir, 276419fd4bb2SEric W. Biederman struct tgid_iter iter) 276561a28784SEric W. Biederman { 276661a28784SEric W. Biederman char name[PROC_NUMBUF]; 276719fd4bb2SEric W. Biederman int len = snprintf(name, sizeof(name), "%d", iter.tgid); 276861a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, name, len, 276919fd4bb2SEric W. Biederman proc_pid_instantiate, iter.task, NULL); 277061a28784SEric W. Biederman } 277161a28784SEric W. Biederman 27721da177e4SLinus Torvalds /* for the /proc/ directory itself, after non-process stuff has been done */ 27731da177e4SLinus Torvalds int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir) 27741da177e4SLinus Torvalds { 27751da177e4SLinus Torvalds unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY; 27762fddfeefSJosef "Jeff" Sipek struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode); 277719fd4bb2SEric W. Biederman struct tgid_iter iter; 2778b488893aSPavel Emelyanov struct pid_namespace *ns; 27791da177e4SLinus Torvalds 278061a28784SEric W. Biederman if (!reaper) 278161a28784SEric W. Biederman goto out_no_task; 278261a28784SEric W. Biederman 27837bcd6b0eSEric W. Biederman for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) { 2784c5141e6dSEric Dumazet const struct pid_entry *p = &proc_base_stuff[nr]; 278561a28784SEric W. Biederman if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0) 2786801199ceSEric W. Biederman goto out; 27871da177e4SLinus Torvalds } 27881da177e4SLinus Torvalds 2789b488893aSPavel Emelyanov ns = filp->f_dentry->d_sb->s_fs_info; 279019fd4bb2SEric W. Biederman iter.task = NULL; 279119fd4bb2SEric W. Biederman iter.tgid = filp->f_pos - TGID_OFFSET; 279219fd4bb2SEric W. Biederman for (iter = next_tgid(ns, iter); 279319fd4bb2SEric W. Biederman iter.task; 279419fd4bb2SEric W. Biederman iter.tgid += 1, iter = next_tgid(ns, iter)) { 279519fd4bb2SEric W. Biederman filp->f_pos = iter.tgid + TGID_OFFSET; 279619fd4bb2SEric W. Biederman if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) { 279719fd4bb2SEric W. Biederman put_task_struct(iter.task); 27980804ef4bSEric W. Biederman goto out; 27991da177e4SLinus Torvalds } 28001da177e4SLinus Torvalds } 28010804ef4bSEric W. Biederman filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET; 28020804ef4bSEric W. Biederman out: 280361a28784SEric W. Biederman put_task_struct(reaper); 280461a28784SEric W. Biederman out_no_task: 28051da177e4SLinus Torvalds return 0; 28061da177e4SLinus Torvalds } 28071da177e4SLinus Torvalds 28080bc58a91SEric W. Biederman /* 280928a6d671SEric W. Biederman * Tasks 281028a6d671SEric W. Biederman */ 2811c5141e6dSEric Dumazet static const struct pid_entry tid_base_stuff[] = { 2812631f9c18SAlexey Dobriyan DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations), 2813631f9c18SAlexey Dobriyan DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fd_operations), 2814631f9c18SAlexey Dobriyan REG("environ", S_IRUSR, proc_environ_operations), 2815631f9c18SAlexey Dobriyan INF("auxv", S_IRUSR, proc_pid_auxv), 2816631f9c18SAlexey Dobriyan ONE("status", S_IRUGO, proc_pid_status), 2817631f9c18SAlexey Dobriyan ONE("personality", S_IRUSR, proc_pid_personality), 2818631f9c18SAlexey Dobriyan INF("limits", S_IRUSR, proc_pid_limits), 281943ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG 2820631f9c18SAlexey Dobriyan REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations), 282143ae34cbSIngo Molnar #endif 2822ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK 2823631f9c18SAlexey Dobriyan INF("syscall", S_IRUSR, proc_pid_syscall), 2824ebcb6734SRoland McGrath #endif 2825631f9c18SAlexey Dobriyan INF("cmdline", S_IRUGO, proc_pid_cmdline), 2826631f9c18SAlexey Dobriyan ONE("stat", S_IRUGO, proc_tid_stat), 2827631f9c18SAlexey Dobriyan ONE("statm", S_IRUGO, proc_pid_statm), 2828631f9c18SAlexey Dobriyan REG("maps", S_IRUGO, proc_maps_operations), 282928a6d671SEric W. Biederman #ifdef CONFIG_NUMA 2830631f9c18SAlexey Dobriyan REG("numa_maps", S_IRUGO, proc_numa_maps_operations), 283128a6d671SEric W. Biederman #endif 2832631f9c18SAlexey Dobriyan REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations), 2833631f9c18SAlexey Dobriyan LNK("cwd", proc_cwd_link), 2834631f9c18SAlexey Dobriyan LNK("root", proc_root_link), 2835631f9c18SAlexey Dobriyan LNK("exe", proc_exe_link), 2836631f9c18SAlexey Dobriyan REG("mounts", S_IRUGO, proc_mounts_operations), 2837631f9c18SAlexey Dobriyan REG("mountinfo", S_IRUGO, proc_mountinfo_operations), 28381e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR 2839631f9c18SAlexey Dobriyan REG("clear_refs", S_IWUSR, proc_clear_refs_operations), 2840631f9c18SAlexey Dobriyan REG("smaps", S_IRUGO, proc_smaps_operations), 2841631f9c18SAlexey Dobriyan REG("pagemap", S_IRUSR, proc_pagemap_operations), 284228a6d671SEric W. Biederman #endif 284328a6d671SEric W. Biederman #ifdef CONFIG_SECURITY 2844631f9c18SAlexey Dobriyan DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations), 284528a6d671SEric W. Biederman #endif 284628a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS 2847631f9c18SAlexey Dobriyan INF("wchan", S_IRUGO, proc_pid_wchan), 284828a6d671SEric W. Biederman #endif 28492ec220e2SKen Chen #ifdef CONFIG_STACKTRACE 28502ec220e2SKen Chen ONE("stack", S_IRUSR, proc_pid_stack), 285128a6d671SEric W. Biederman #endif 285228a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS 2853631f9c18SAlexey Dobriyan INF("schedstat", S_IRUGO, proc_pid_schedstat), 285428a6d671SEric W. Biederman #endif 28559745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP 2856631f9c18SAlexey Dobriyan REG("latency", S_IRUGO, proc_lstats_operations), 28579745512cSArjan van de Ven #endif 28588793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET 2859631f9c18SAlexey Dobriyan REG("cpuset", S_IRUGO, proc_cpuset_operations), 286028a6d671SEric W. Biederman #endif 2861a424316cSPaul Menage #ifdef CONFIG_CGROUPS 2862631f9c18SAlexey Dobriyan REG("cgroup", S_IRUGO, proc_cgroup_operations), 2863a424316cSPaul Menage #endif 2864631f9c18SAlexey Dobriyan INF("oom_score", S_IRUGO, proc_oom_score), 2865631f9c18SAlexey Dobriyan REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations), 286628a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL 2867631f9c18SAlexey Dobriyan REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations), 2868631f9c18SAlexey Dobriyan REG("sessionid", S_IRUSR, proc_sessionid_operations), 286928a6d671SEric W. Biederman #endif 2870f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION 2871631f9c18SAlexey Dobriyan REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations), 2872f4f154fdSAkinobu Mita #endif 2873297c5d92SAndrea Righi #ifdef CONFIG_TASK_IO_ACCOUNTING 2874631f9c18SAlexey Dobriyan INF("io", S_IRUGO, proc_tid_io_accounting), 2875297c5d92SAndrea Righi #endif 287628a6d671SEric W. Biederman }; 287728a6d671SEric W. Biederman 287828a6d671SEric W. Biederman static int proc_tid_base_readdir(struct file * filp, 287928a6d671SEric W. Biederman void * dirent, filldir_t filldir) 288028a6d671SEric W. Biederman { 288128a6d671SEric W. Biederman return proc_pident_readdir(filp,dirent,filldir, 288228a6d671SEric W. Biederman tid_base_stuff,ARRAY_SIZE(tid_base_stuff)); 288328a6d671SEric W. Biederman } 288428a6d671SEric W. Biederman 288528a6d671SEric W. Biederman static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){ 28867bcd6b0eSEric W. Biederman return proc_pident_lookup(dir, dentry, 28877bcd6b0eSEric W. Biederman tid_base_stuff, ARRAY_SIZE(tid_base_stuff)); 288828a6d671SEric W. Biederman } 288928a6d671SEric W. Biederman 289000977a59SArjan van de Ven static const struct file_operations proc_tid_base_operations = { 289128a6d671SEric W. Biederman .read = generic_read_dir, 289228a6d671SEric W. Biederman .readdir = proc_tid_base_readdir, 289328a6d671SEric W. Biederman }; 289428a6d671SEric W. Biederman 2895c5ef1c42SArjan van de Ven static const struct inode_operations proc_tid_base_inode_operations = { 289628a6d671SEric W. Biederman .lookup = proc_tid_base_lookup, 289728a6d671SEric W. Biederman .getattr = pid_getattr, 289828a6d671SEric W. Biederman .setattr = proc_setattr, 289928a6d671SEric W. Biederman }; 290028a6d671SEric W. Biederman 2901444ceed8SEric W. Biederman static struct dentry *proc_task_instantiate(struct inode *dir, 2902c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 2903444ceed8SEric W. Biederman { 2904444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-ENOENT); 2905444ceed8SEric W. Biederman struct inode *inode; 290661a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 2907444ceed8SEric W. Biederman 2908444ceed8SEric W. Biederman if (!inode) 2909444ceed8SEric W. Biederman goto out; 2910444ceed8SEric W. Biederman inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO; 2911444ceed8SEric W. Biederman inode->i_op = &proc_tid_base_inode_operations; 2912444ceed8SEric W. Biederman inode->i_fop = &proc_tid_base_operations; 2913444ceed8SEric W. Biederman inode->i_flags|=S_IMMUTABLE; 2914aed54175SVegard Nossum 2915aed54175SVegard Nossum inode->i_nlink = 2 + pid_entry_count_dirs(tid_base_stuff, 2916aed54175SVegard Nossum ARRAY_SIZE(tid_base_stuff)); 2917444ceed8SEric W. Biederman 2918444ceed8SEric W. Biederman dentry->d_op = &pid_dentry_operations; 2919444ceed8SEric W. Biederman 2920444ceed8SEric W. Biederman d_add(dentry, inode); 2921444ceed8SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 2922444ceed8SEric W. Biederman if (pid_revalidate(dentry, NULL)) 2923444ceed8SEric W. Biederman error = NULL; 2924444ceed8SEric W. Biederman out: 2925444ceed8SEric W. Biederman return error; 2926444ceed8SEric W. Biederman } 2927444ceed8SEric W. Biederman 292828a6d671SEric W. Biederman static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd) 292928a6d671SEric W. Biederman { 293028a6d671SEric W. Biederman struct dentry *result = ERR_PTR(-ENOENT); 293128a6d671SEric W. Biederman struct task_struct *task; 293228a6d671SEric W. Biederman struct task_struct *leader = get_proc_task(dir); 293328a6d671SEric W. Biederman unsigned tid; 2934b488893aSPavel Emelyanov struct pid_namespace *ns; 293528a6d671SEric W. Biederman 293628a6d671SEric W. Biederman if (!leader) 293728a6d671SEric W. Biederman goto out_no_task; 293828a6d671SEric W. Biederman 293928a6d671SEric W. Biederman tid = name_to_int(dentry); 294028a6d671SEric W. Biederman if (tid == ~0U) 294128a6d671SEric W. Biederman goto out; 294228a6d671SEric W. Biederman 2943b488893aSPavel Emelyanov ns = dentry->d_sb->s_fs_info; 294428a6d671SEric W. Biederman rcu_read_lock(); 2945b488893aSPavel Emelyanov task = find_task_by_pid_ns(tid, ns); 294628a6d671SEric W. Biederman if (task) 294728a6d671SEric W. Biederman get_task_struct(task); 294828a6d671SEric W. Biederman rcu_read_unlock(); 294928a6d671SEric W. Biederman if (!task) 295028a6d671SEric W. Biederman goto out; 2951bac0abd6SPavel Emelyanov if (!same_thread_group(leader, task)) 295228a6d671SEric W. Biederman goto out_drop_task; 295328a6d671SEric W. Biederman 2954444ceed8SEric W. Biederman result = proc_task_instantiate(dir, dentry, task, NULL); 295528a6d671SEric W. Biederman out_drop_task: 295628a6d671SEric W. Biederman put_task_struct(task); 295728a6d671SEric W. Biederman out: 295828a6d671SEric W. Biederman put_task_struct(leader); 295928a6d671SEric W. Biederman out_no_task: 296028a6d671SEric W. Biederman return result; 296128a6d671SEric W. Biederman } 296228a6d671SEric W. Biederman 296328a6d671SEric W. Biederman /* 29640bc58a91SEric W. Biederman * Find the first tid of a thread group to return to user space. 29650bc58a91SEric W. Biederman * 29660bc58a91SEric W. Biederman * Usually this is just the thread group leader, but if the users 29670bc58a91SEric W. Biederman * buffer was too small or there was a seek into the middle of the 29680bc58a91SEric W. Biederman * directory we have more work todo. 29690bc58a91SEric W. Biederman * 29700bc58a91SEric W. Biederman * In the case of a short read we start with find_task_by_pid. 29710bc58a91SEric W. Biederman * 29720bc58a91SEric W. Biederman * In the case of a seek we start with the leader and walk nr 29730bc58a91SEric W. Biederman * threads past it. 29740bc58a91SEric W. Biederman */ 2975cc288738SEric W. Biederman static struct task_struct *first_tid(struct task_struct *leader, 2976b488893aSPavel Emelyanov int tid, int nr, struct pid_namespace *ns) 29770bc58a91SEric W. Biederman { 2978a872ff0cSOleg Nesterov struct task_struct *pos; 29790bc58a91SEric W. Biederman 2980cc288738SEric W. Biederman rcu_read_lock(); 29810bc58a91SEric W. Biederman /* Attempt to start with the pid of a thread */ 29820bc58a91SEric W. Biederman if (tid && (nr > 0)) { 2983b488893aSPavel Emelyanov pos = find_task_by_pid_ns(tid, ns); 2984a872ff0cSOleg Nesterov if (pos && (pos->group_leader == leader)) 2985a872ff0cSOleg Nesterov goto found; 29860bc58a91SEric W. Biederman } 29870bc58a91SEric W. Biederman 29880bc58a91SEric W. Biederman /* If nr exceeds the number of threads there is nothing todo */ 29890bc58a91SEric W. Biederman pos = NULL; 2990a872ff0cSOleg Nesterov if (nr && nr >= get_nr_threads(leader)) 2991a872ff0cSOleg Nesterov goto out; 2992a872ff0cSOleg Nesterov 2993a872ff0cSOleg Nesterov /* If we haven't found our starting place yet start 2994a872ff0cSOleg Nesterov * with the leader and walk nr threads forward. 2995a872ff0cSOleg Nesterov */ 2996a872ff0cSOleg Nesterov for (pos = leader; nr > 0; --nr) { 2997a872ff0cSOleg Nesterov pos = next_thread(pos); 2998a872ff0cSOleg Nesterov if (pos == leader) { 2999a872ff0cSOleg Nesterov pos = NULL; 3000a872ff0cSOleg Nesterov goto out; 3001a872ff0cSOleg Nesterov } 3002a872ff0cSOleg Nesterov } 3003a872ff0cSOleg Nesterov found: 3004a872ff0cSOleg Nesterov get_task_struct(pos); 3005a872ff0cSOleg Nesterov out: 3006cc288738SEric W. Biederman rcu_read_unlock(); 30070bc58a91SEric W. Biederman return pos; 30080bc58a91SEric W. Biederman } 30090bc58a91SEric W. Biederman 30100bc58a91SEric W. Biederman /* 30110bc58a91SEric W. Biederman * Find the next thread in the thread list. 30120bc58a91SEric W. Biederman * Return NULL if there is an error or no next thread. 30130bc58a91SEric W. Biederman * 30140bc58a91SEric W. Biederman * The reference to the input task_struct is released. 30150bc58a91SEric W. Biederman */ 30160bc58a91SEric W. Biederman static struct task_struct *next_tid(struct task_struct *start) 30170bc58a91SEric W. Biederman { 3018c1df7fb8SOleg Nesterov struct task_struct *pos = NULL; 3019cc288738SEric W. Biederman rcu_read_lock(); 3020c1df7fb8SOleg Nesterov if (pid_alive(start)) { 30210bc58a91SEric W. Biederman pos = next_thread(start); 3022c1df7fb8SOleg Nesterov if (thread_group_leader(pos)) 30230bc58a91SEric W. Biederman pos = NULL; 3024c1df7fb8SOleg Nesterov else 3025c1df7fb8SOleg Nesterov get_task_struct(pos); 3026c1df7fb8SOleg Nesterov } 3027cc288738SEric W. Biederman rcu_read_unlock(); 30280bc58a91SEric W. Biederman put_task_struct(start); 30290bc58a91SEric W. Biederman return pos; 30300bc58a91SEric W. Biederman } 30310bc58a91SEric W. Biederman 303261a28784SEric W. Biederman static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir, 303361a28784SEric W. Biederman struct task_struct *task, int tid) 303461a28784SEric W. Biederman { 303561a28784SEric W. Biederman char name[PROC_NUMBUF]; 303661a28784SEric W. Biederman int len = snprintf(name, sizeof(name), "%d", tid); 303761a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, name, len, 303861a28784SEric W. Biederman proc_task_instantiate, task, NULL); 303961a28784SEric W. Biederman } 304061a28784SEric W. Biederman 30411da177e4SLinus Torvalds /* for the /proc/TGID/task/ directories */ 30421da177e4SLinus Torvalds static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir) 30431da177e4SLinus Torvalds { 30442fddfeefSJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 30451da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 30467d895244SGuillaume Chazarain struct task_struct *leader = NULL; 30470bc58a91SEric W. Biederman struct task_struct *task; 30481da177e4SLinus Torvalds int retval = -ENOENT; 30491da177e4SLinus Torvalds ino_t ino; 30500bc58a91SEric W. Biederman int tid; 3051b488893aSPavel Emelyanov struct pid_namespace *ns; 30521da177e4SLinus Torvalds 30537d895244SGuillaume Chazarain task = get_proc_task(inode); 30547d895244SGuillaume Chazarain if (!task) 30557d895244SGuillaume Chazarain goto out_no_task; 30567d895244SGuillaume Chazarain rcu_read_lock(); 30577d895244SGuillaume Chazarain if (pid_alive(task)) { 30587d895244SGuillaume Chazarain leader = task->group_leader; 30597d895244SGuillaume Chazarain get_task_struct(leader); 30607d895244SGuillaume Chazarain } 30617d895244SGuillaume Chazarain rcu_read_unlock(); 30627d895244SGuillaume Chazarain put_task_struct(task); 306399f89551SEric W. Biederman if (!leader) 306499f89551SEric W. Biederman goto out_no_task; 30651da177e4SLinus Torvalds retval = 0; 30661da177e4SLinus Torvalds 3067ee568b25SLinus Torvalds switch ((unsigned long)filp->f_pos) { 30681da177e4SLinus Torvalds case 0: 30691da177e4SLinus Torvalds ino = inode->i_ino; 3070ee6f779bSZhang Le if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) < 0) 30711da177e4SLinus Torvalds goto out; 3072ee6f779bSZhang Le filp->f_pos++; 30731da177e4SLinus Torvalds /* fall through */ 30741da177e4SLinus Torvalds case 1: 30751da177e4SLinus Torvalds ino = parent_ino(dentry); 3076ee6f779bSZhang Le if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) < 0) 30771da177e4SLinus Torvalds goto out; 3078ee6f779bSZhang Le filp->f_pos++; 30791da177e4SLinus Torvalds /* fall through */ 30801da177e4SLinus Torvalds } 30811da177e4SLinus Torvalds 30820bc58a91SEric W. Biederman /* f_version caches the tgid value that the last readdir call couldn't 30830bc58a91SEric W. Biederman * return. lseek aka telldir automagically resets f_version to 0. 30840bc58a91SEric W. Biederman */ 3085b488893aSPavel Emelyanov ns = filp->f_dentry->d_sb->s_fs_info; 30862b47c361SMathieu Desnoyers tid = (int)filp->f_version; 30870bc58a91SEric W. Biederman filp->f_version = 0; 3088ee6f779bSZhang Le for (task = first_tid(leader, tid, filp->f_pos - 2, ns); 30890bc58a91SEric W. Biederman task; 3090ee6f779bSZhang Le task = next_tid(task), filp->f_pos++) { 3091b488893aSPavel Emelyanov tid = task_pid_nr_ns(task, ns); 309261a28784SEric W. Biederman if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) { 30930bc58a91SEric W. Biederman /* returning this tgid failed, save it as the first 30940bc58a91SEric W. Biederman * pid for the next readir call */ 30952b47c361SMathieu Desnoyers filp->f_version = (u64)tid; 30960bc58a91SEric W. Biederman put_task_struct(task); 30971da177e4SLinus Torvalds break; 30980bc58a91SEric W. Biederman } 30991da177e4SLinus Torvalds } 31001da177e4SLinus Torvalds out: 310199f89551SEric W. Biederman put_task_struct(leader); 310299f89551SEric W. Biederman out_no_task: 31031da177e4SLinus Torvalds return retval; 31041da177e4SLinus Torvalds } 31056e66b52bSEric W. Biederman 31066e66b52bSEric W. Biederman static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) 31076e66b52bSEric W. Biederman { 31086e66b52bSEric W. Biederman struct inode *inode = dentry->d_inode; 310999f89551SEric W. Biederman struct task_struct *p = get_proc_task(inode); 31106e66b52bSEric W. Biederman generic_fillattr(inode, stat); 31116e66b52bSEric W. Biederman 311299f89551SEric W. Biederman if (p) { 311399f89551SEric W. Biederman stat->nlink += get_nr_threads(p); 311499f89551SEric W. Biederman put_task_struct(p); 31156e66b52bSEric W. Biederman } 31166e66b52bSEric W. Biederman 31176e66b52bSEric W. Biederman return 0; 31186e66b52bSEric W. Biederman } 311928a6d671SEric W. Biederman 3120c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations = { 312128a6d671SEric W. Biederman .lookup = proc_task_lookup, 312228a6d671SEric W. Biederman .getattr = proc_task_getattr, 312328a6d671SEric W. Biederman .setattr = proc_setattr, 312428a6d671SEric W. Biederman }; 312528a6d671SEric W. Biederman 312600977a59SArjan van de Ven static const struct file_operations proc_task_operations = { 312728a6d671SEric W. Biederman .read = generic_read_dir, 312828a6d671SEric W. Biederman .readdir = proc_task_readdir, 312928a6d671SEric W. Biederman }; 3130