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> 835ad4e53bSAl Viro #include <linux/fs_struct.h> 841da177e4SLinus Torvalds #include "internal.h" 851da177e4SLinus Torvalds 860f2fe20fSEric W. Biederman /* NOTE: 870f2fe20fSEric W. Biederman * Implementing inode permission operations in /proc is almost 880f2fe20fSEric W. Biederman * certainly an error. Permission checks need to happen during 890f2fe20fSEric W. Biederman * each system call not at open time. The reason is that most of 900f2fe20fSEric W. Biederman * what we wish to check for permissions in /proc varies at runtime. 910f2fe20fSEric W. Biederman * 920f2fe20fSEric W. Biederman * The classic example of a problem is opening file descriptors 930f2fe20fSEric W. Biederman * in /proc for a task before it execs a suid executable. 940f2fe20fSEric W. Biederman */ 950f2fe20fSEric W. Biederman 961da177e4SLinus Torvalds struct pid_entry { 971da177e4SLinus Torvalds char *name; 98c5141e6dSEric Dumazet int len; 991da177e4SLinus Torvalds mode_t mode; 100c5ef1c42SArjan van de Ven const struct inode_operations *iop; 10100977a59SArjan van de Ven const struct file_operations *fop; 10220cdc894SEric W. Biederman union proc_op op; 1031da177e4SLinus Torvalds }; 1041da177e4SLinus Torvalds 10561a28784SEric W. Biederman #define NOD(NAME, MODE, IOP, FOP, OP) { \ 10620cdc894SEric W. Biederman .name = (NAME), \ 107c5141e6dSEric Dumazet .len = sizeof(NAME) - 1, \ 10820cdc894SEric W. Biederman .mode = MODE, \ 10920cdc894SEric W. Biederman .iop = IOP, \ 11020cdc894SEric W. Biederman .fop = FOP, \ 11120cdc894SEric W. Biederman .op = OP, \ 11220cdc894SEric W. Biederman } 11320cdc894SEric W. Biederman 114631f9c18SAlexey Dobriyan #define DIR(NAME, MODE, iops, fops) \ 115631f9c18SAlexey Dobriyan NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} ) 116631f9c18SAlexey Dobriyan #define LNK(NAME, get_link) \ 11761a28784SEric W. Biederman NOD(NAME, (S_IFLNK|S_IRWXUGO), \ 11820cdc894SEric W. Biederman &proc_pid_link_inode_operations, NULL, \ 119631f9c18SAlexey Dobriyan { .proc_get_link = get_link } ) 120631f9c18SAlexey Dobriyan #define REG(NAME, MODE, fops) \ 121631f9c18SAlexey Dobriyan NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {}) 122631f9c18SAlexey Dobriyan #define INF(NAME, MODE, read) \ 12361a28784SEric W. Biederman NOD(NAME, (S_IFREG|(MODE)), \ 12420cdc894SEric W. Biederman NULL, &proc_info_file_operations, \ 125631f9c18SAlexey Dobriyan { .proc_read = read } ) 126631f9c18SAlexey Dobriyan #define ONE(NAME, MODE, show) \ 127be614086SEric W. Biederman NOD(NAME, (S_IFREG|(MODE)), \ 128be614086SEric W. Biederman NULL, &proc_single_file_operations, \ 129631f9c18SAlexey Dobriyan { .proc_show = show } ) 1301da177e4SLinus Torvalds 131aed54175SVegard Nossum /* 132aed54175SVegard Nossum * Count the number of hardlinks for the pid_entry table, excluding the . 133aed54175SVegard Nossum * and .. links. 134aed54175SVegard Nossum */ 135aed54175SVegard Nossum static unsigned int pid_entry_count_dirs(const struct pid_entry *entries, 136aed54175SVegard Nossum unsigned int n) 137aed54175SVegard Nossum { 138aed54175SVegard Nossum unsigned int i; 139aed54175SVegard Nossum unsigned int count; 140aed54175SVegard Nossum 141aed54175SVegard Nossum count = 0; 142aed54175SVegard Nossum for (i = 0; i < n; ++i) { 143aed54175SVegard Nossum if (S_ISDIR(entries[i].mode)) 144aed54175SVegard Nossum ++count; 145aed54175SVegard Nossum } 146aed54175SVegard Nossum 147aed54175SVegard Nossum return count; 148aed54175SVegard Nossum } 149aed54175SVegard Nossum 1507c2c7d99SHugh Dickins static int get_fs_path(struct task_struct *task, struct path *path, bool root) 1511da177e4SLinus Torvalds { 1521da177e4SLinus Torvalds struct fs_struct *fs; 1537c2c7d99SHugh Dickins int result = -ENOENT; 1547c2c7d99SHugh Dickins 1550494f6ecSMiklos Szeredi task_lock(task); 1560494f6ecSMiklos Szeredi fs = task->fs; 1577c2c7d99SHugh Dickins if (fs) { 1587c2c7d99SHugh Dickins read_lock(&fs->lock); 1597c2c7d99SHugh Dickins *path = root ? fs->root : fs->pwd; 1607c2c7d99SHugh Dickins path_get(path); 1617c2c7d99SHugh Dickins read_unlock(&fs->lock); 1627c2c7d99SHugh Dickins result = 0; 1637c2c7d99SHugh Dickins } 1640494f6ecSMiklos Szeredi task_unlock(task); 1657c2c7d99SHugh Dickins return result; 1660494f6ecSMiklos Szeredi } 1670494f6ecSMiklos Szeredi 16899f89551SEric W. Biederman static int get_nr_threads(struct task_struct *tsk) 16999f89551SEric W. Biederman { 17099f89551SEric W. Biederman unsigned long flags; 17199f89551SEric W. Biederman int count = 0; 17299f89551SEric W. Biederman 17399f89551SEric W. Biederman if (lock_task_sighand(tsk, &flags)) { 17499f89551SEric W. Biederman count = atomic_read(&tsk->signal->count); 17599f89551SEric W. Biederman unlock_task_sighand(tsk, &flags); 17699f89551SEric W. Biederman } 17799f89551SEric W. Biederman return count; 17899f89551SEric W. Biederman } 17999f89551SEric W. Biederman 1803dcd25f3SJan Blunck static int proc_cwd_link(struct inode *inode, struct path *path) 1810494f6ecSMiklos Szeredi { 18299f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 1830494f6ecSMiklos Szeredi int result = -ENOENT; 18499f89551SEric W. Biederman 18599f89551SEric W. Biederman if (task) { 1867c2c7d99SHugh Dickins result = get_fs_path(task, path, 0); 18799f89551SEric W. Biederman put_task_struct(task); 18899f89551SEric W. Biederman } 1891da177e4SLinus Torvalds return result; 1901da177e4SLinus Torvalds } 1911da177e4SLinus Torvalds 1923dcd25f3SJan Blunck static int proc_root_link(struct inode *inode, struct path *path) 1931da177e4SLinus Torvalds { 19499f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 1951da177e4SLinus Torvalds int result = -ENOENT; 19699f89551SEric W. Biederman 19799f89551SEric W. Biederman if (task) { 1987c2c7d99SHugh Dickins result = get_fs_path(task, path, 1); 19999f89551SEric W. Biederman put_task_struct(task); 20099f89551SEric W. Biederman } 2011da177e4SLinus Torvalds return result; 2021da177e4SLinus Torvalds } 2031da177e4SLinus Torvalds 204638fa202SRoland McGrath /* 205638fa202SRoland McGrath * Return zero if current may access user memory in @task, -error if not. 206638fa202SRoland McGrath */ 207638fa202SRoland McGrath static int check_mem_permission(struct task_struct *task) 208638fa202SRoland McGrath { 209638fa202SRoland McGrath /* 210638fa202SRoland McGrath * A task can always look at itself, in case it chooses 211638fa202SRoland McGrath * to use system calls instead of load instructions. 212638fa202SRoland McGrath */ 213638fa202SRoland McGrath if (task == current) 214638fa202SRoland McGrath return 0; 215638fa202SRoland McGrath 216638fa202SRoland McGrath /* 217638fa202SRoland McGrath * If current is actively ptrace'ing, and would also be 218638fa202SRoland McGrath * permitted to freshly attach with ptrace now, permit it. 219638fa202SRoland McGrath */ 2200d094efeSRoland McGrath if (task_is_stopped_or_traced(task)) { 2210d094efeSRoland McGrath int match; 2220d094efeSRoland McGrath rcu_read_lock(); 2230d094efeSRoland McGrath match = (tracehook_tracer_task(task) == current); 2240d094efeSRoland McGrath rcu_read_unlock(); 2250d094efeSRoland McGrath if (match && ptrace_may_access(task, PTRACE_MODE_ATTACH)) 226638fa202SRoland McGrath return 0; 2270d094efeSRoland McGrath } 228638fa202SRoland McGrath 229638fa202SRoland McGrath /* 230638fa202SRoland McGrath * Noone else is allowed. 231638fa202SRoland McGrath */ 232638fa202SRoland McGrath return -EPERM; 233638fa202SRoland McGrath } 2341da177e4SLinus Torvalds 235831830b5SAl Viro struct mm_struct *mm_for_maps(struct task_struct *task) 236831830b5SAl Viro { 237831830b5SAl Viro struct mm_struct *mm = get_task_mm(task); 238831830b5SAl Viro if (!mm) 239831830b5SAl Viro return NULL; 240831830b5SAl Viro down_read(&mm->mmap_sem); 241831830b5SAl Viro task_lock(task); 242831830b5SAl Viro if (task->mm != mm) 243831830b5SAl Viro goto out; 244006ebb40SStephen Smalley if (task->mm != current->mm && 245006ebb40SStephen Smalley __ptrace_may_access(task, PTRACE_MODE_READ) < 0) 246831830b5SAl Viro goto out; 247831830b5SAl Viro task_unlock(task); 248831830b5SAl Viro return mm; 249831830b5SAl Viro out: 250831830b5SAl Viro task_unlock(task); 251831830b5SAl Viro up_read(&mm->mmap_sem); 252831830b5SAl Viro mmput(mm); 253831830b5SAl Viro return NULL; 254831830b5SAl Viro } 255831830b5SAl Viro 2561da177e4SLinus Torvalds static int proc_pid_cmdline(struct task_struct *task, char * buffer) 2571da177e4SLinus Torvalds { 2581da177e4SLinus Torvalds int res = 0; 2591da177e4SLinus Torvalds unsigned int len; 2601da177e4SLinus Torvalds struct mm_struct *mm = get_task_mm(task); 2611da177e4SLinus Torvalds if (!mm) 2621da177e4SLinus Torvalds goto out; 2631da177e4SLinus Torvalds if (!mm->arg_end) 2641da177e4SLinus Torvalds goto out_mm; /* Shh! No looking before we're done */ 2651da177e4SLinus Torvalds 2661da177e4SLinus Torvalds len = mm->arg_end - mm->arg_start; 2671da177e4SLinus Torvalds 2681da177e4SLinus Torvalds if (len > PAGE_SIZE) 2691da177e4SLinus Torvalds len = PAGE_SIZE; 2701da177e4SLinus Torvalds 2711da177e4SLinus Torvalds res = access_process_vm(task, mm->arg_start, buffer, len, 0); 2721da177e4SLinus Torvalds 2731da177e4SLinus Torvalds // If the nul at the end of args has been overwritten, then 2741da177e4SLinus Torvalds // assume application is using setproctitle(3). 2751da177e4SLinus Torvalds if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) { 2761da177e4SLinus Torvalds len = strnlen(buffer, res); 2771da177e4SLinus Torvalds if (len < res) { 2781da177e4SLinus Torvalds res = len; 2791da177e4SLinus Torvalds } else { 2801da177e4SLinus Torvalds len = mm->env_end - mm->env_start; 2811da177e4SLinus Torvalds if (len > PAGE_SIZE - res) 2821da177e4SLinus Torvalds len = PAGE_SIZE - res; 2831da177e4SLinus Torvalds res += access_process_vm(task, mm->env_start, buffer+res, len, 0); 2841da177e4SLinus Torvalds res = strnlen(buffer, res); 2851da177e4SLinus Torvalds } 2861da177e4SLinus Torvalds } 2871da177e4SLinus Torvalds out_mm: 2881da177e4SLinus Torvalds mmput(mm); 2891da177e4SLinus Torvalds out: 2901da177e4SLinus Torvalds return res; 2911da177e4SLinus Torvalds } 2921da177e4SLinus Torvalds 2931da177e4SLinus Torvalds static int proc_pid_auxv(struct task_struct *task, char *buffer) 2941da177e4SLinus Torvalds { 2951da177e4SLinus Torvalds int res = 0; 2961da177e4SLinus Torvalds struct mm_struct *mm = get_task_mm(task); 2971da177e4SLinus Torvalds if (mm) { 2981da177e4SLinus Torvalds unsigned int nwords = 0; 299dfe6b7d9SHannes Eder do { 3001da177e4SLinus Torvalds nwords += 2; 301dfe6b7d9SHannes Eder } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */ 3021da177e4SLinus Torvalds res = nwords * sizeof(mm->saved_auxv[0]); 3031da177e4SLinus Torvalds if (res > PAGE_SIZE) 3041da177e4SLinus Torvalds res = PAGE_SIZE; 3051da177e4SLinus Torvalds memcpy(buffer, mm->saved_auxv, res); 3061da177e4SLinus Torvalds mmput(mm); 3071da177e4SLinus Torvalds } 3081da177e4SLinus Torvalds return res; 3091da177e4SLinus Torvalds } 3101da177e4SLinus Torvalds 3111da177e4SLinus Torvalds 3121da177e4SLinus Torvalds #ifdef CONFIG_KALLSYMS 3131da177e4SLinus Torvalds /* 3141da177e4SLinus Torvalds * Provides a wchan file via kallsyms in a proper one-value-per-file format. 3151da177e4SLinus Torvalds * Returns the resolved symbol. If that fails, simply return the address. 3161da177e4SLinus Torvalds */ 3171da177e4SLinus Torvalds static int proc_pid_wchan(struct task_struct *task, char *buffer) 3181da177e4SLinus Torvalds { 319ffb45122SAlexey Dobriyan unsigned long wchan; 3209281aceaSTejun Heo char symname[KSYM_NAME_LEN]; 3211da177e4SLinus Torvalds 3221da177e4SLinus Torvalds wchan = get_wchan(task); 3231da177e4SLinus Torvalds 3249d65cb4aSAlexey Dobriyan if (lookup_symbol_name(wchan, symname) < 0) 325f83ce3e6SJake Edge if (!ptrace_may_access(task, PTRACE_MODE_READ)) 326f83ce3e6SJake Edge return 0; 327f83ce3e6SJake Edge else 3281da177e4SLinus Torvalds return sprintf(buffer, "%lu", wchan); 3299d65cb4aSAlexey Dobriyan else 3309d65cb4aSAlexey Dobriyan return sprintf(buffer, "%s", symname); 3311da177e4SLinus Torvalds } 3321da177e4SLinus Torvalds #endif /* CONFIG_KALLSYMS */ 3331da177e4SLinus Torvalds 3342ec220e2SKen Chen #ifdef CONFIG_STACKTRACE 3352ec220e2SKen Chen 3362ec220e2SKen Chen #define MAX_STACK_TRACE_DEPTH 64 3372ec220e2SKen Chen 3382ec220e2SKen Chen static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns, 3392ec220e2SKen Chen struct pid *pid, struct task_struct *task) 3402ec220e2SKen Chen { 3412ec220e2SKen Chen struct stack_trace trace; 3422ec220e2SKen Chen unsigned long *entries; 3432ec220e2SKen Chen int i; 3442ec220e2SKen Chen 3452ec220e2SKen Chen entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL); 3462ec220e2SKen Chen if (!entries) 3472ec220e2SKen Chen return -ENOMEM; 3482ec220e2SKen Chen 3492ec220e2SKen Chen trace.nr_entries = 0; 3502ec220e2SKen Chen trace.max_entries = MAX_STACK_TRACE_DEPTH; 3512ec220e2SKen Chen trace.entries = entries; 3522ec220e2SKen Chen trace.skip = 0; 3532ec220e2SKen Chen save_stack_trace_tsk(task, &trace); 3542ec220e2SKen Chen 3552ec220e2SKen Chen for (i = 0; i < trace.nr_entries; i++) { 3562ec220e2SKen Chen seq_printf(m, "[<%p>] %pS\n", 3572ec220e2SKen Chen (void *)entries[i], (void *)entries[i]); 3582ec220e2SKen Chen } 3592ec220e2SKen Chen kfree(entries); 3602ec220e2SKen Chen 3612ec220e2SKen Chen return 0; 3622ec220e2SKen Chen } 3632ec220e2SKen Chen #endif 3642ec220e2SKen Chen 3651da177e4SLinus Torvalds #ifdef CONFIG_SCHEDSTATS 3661da177e4SLinus Torvalds /* 3671da177e4SLinus Torvalds * Provides /proc/PID/schedstat 3681da177e4SLinus Torvalds */ 3691da177e4SLinus Torvalds static int proc_pid_schedstat(struct task_struct *task, char *buffer) 3701da177e4SLinus Torvalds { 371172ba844SBalbir Singh return sprintf(buffer, "%llu %llu %lu\n", 372826e08b0SIngo Molnar (unsigned long long)task->se.sum_exec_runtime, 373826e08b0SIngo Molnar (unsigned long long)task->sched_info.run_delay, 3742d72376bSIngo Molnar task->sched_info.pcount); 3751da177e4SLinus Torvalds } 3761da177e4SLinus Torvalds #endif 3771da177e4SLinus Torvalds 3789745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP 3799745512cSArjan van de Ven static int lstats_show_proc(struct seq_file *m, void *v) 3809745512cSArjan van de Ven { 3819745512cSArjan van de Ven int i; 38213d77c37SHiroshi Shimamoto struct inode *inode = m->private; 38313d77c37SHiroshi Shimamoto struct task_struct *task = get_proc_task(inode); 3849745512cSArjan van de Ven 38513d77c37SHiroshi Shimamoto if (!task) 38613d77c37SHiroshi Shimamoto return -ESRCH; 38713d77c37SHiroshi Shimamoto seq_puts(m, "Latency Top version : v0.1\n"); 3889745512cSArjan van de Ven for (i = 0; i < 32; i++) { 3899745512cSArjan van de Ven if (task->latency_record[i].backtrace[0]) { 3909745512cSArjan van de Ven int q; 3919745512cSArjan van de Ven seq_printf(m, "%i %li %li ", 3929745512cSArjan van de Ven task->latency_record[i].count, 3939745512cSArjan van de Ven task->latency_record[i].time, 3949745512cSArjan van de Ven task->latency_record[i].max); 3959745512cSArjan van de Ven for (q = 0; q < LT_BACKTRACEDEPTH; q++) { 3969c246247SHugh Dickins char sym[KSYM_SYMBOL_LEN]; 3979745512cSArjan van de Ven char *c; 3989745512cSArjan van de Ven if (!task->latency_record[i].backtrace[q]) 3999745512cSArjan van de Ven break; 4009745512cSArjan van de Ven if (task->latency_record[i].backtrace[q] == ULONG_MAX) 4019745512cSArjan van de Ven break; 4029745512cSArjan van de Ven sprint_symbol(sym, task->latency_record[i].backtrace[q]); 4039745512cSArjan van de Ven c = strchr(sym, '+'); 4049745512cSArjan van de Ven if (c) 4059745512cSArjan van de Ven *c = 0; 4069745512cSArjan van de Ven seq_printf(m, "%s ", sym); 4079745512cSArjan van de Ven } 4089745512cSArjan van de Ven seq_printf(m, "\n"); 4099745512cSArjan van de Ven } 4109745512cSArjan van de Ven 4119745512cSArjan van de Ven } 41213d77c37SHiroshi Shimamoto put_task_struct(task); 4139745512cSArjan van de Ven return 0; 4149745512cSArjan van de Ven } 4159745512cSArjan van de Ven 4169745512cSArjan van de Ven static int lstats_open(struct inode *inode, struct file *file) 4179745512cSArjan van de Ven { 41813d77c37SHiroshi Shimamoto return single_open(file, lstats_show_proc, inode); 419d6643d12SHiroshi Shimamoto } 420d6643d12SHiroshi Shimamoto 4219745512cSArjan van de Ven static ssize_t lstats_write(struct file *file, const char __user *buf, 4229745512cSArjan van de Ven size_t count, loff_t *offs) 4239745512cSArjan van de Ven { 42413d77c37SHiroshi Shimamoto struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 4259745512cSArjan van de Ven 42613d77c37SHiroshi Shimamoto if (!task) 42713d77c37SHiroshi Shimamoto return -ESRCH; 4289745512cSArjan van de Ven clear_all_latency_tracing(task); 42913d77c37SHiroshi Shimamoto put_task_struct(task); 4309745512cSArjan van de Ven 4319745512cSArjan van de Ven return count; 4329745512cSArjan van de Ven } 4339745512cSArjan van de Ven 4349745512cSArjan van de Ven static const struct file_operations proc_lstats_operations = { 4359745512cSArjan van de Ven .open = lstats_open, 4369745512cSArjan van de Ven .read = seq_read, 4379745512cSArjan van de Ven .write = lstats_write, 4389745512cSArjan van de Ven .llseek = seq_lseek, 43913d77c37SHiroshi Shimamoto .release = single_release, 4409745512cSArjan van de Ven }; 4419745512cSArjan van de Ven 4429745512cSArjan van de Ven #endif 4439745512cSArjan van de Ven 4441da177e4SLinus Torvalds /* The badness from the OOM killer */ 4451da177e4SLinus Torvalds unsigned long badness(struct task_struct *p, unsigned long uptime); 4461da177e4SLinus Torvalds static int proc_oom_score(struct task_struct *task, char *buffer) 4471da177e4SLinus Torvalds { 4481da177e4SLinus Torvalds unsigned long points; 4491da177e4SLinus Torvalds struct timespec uptime; 4501da177e4SLinus Torvalds 4511da177e4SLinus Torvalds do_posix_clock_monotonic_gettime(&uptime); 45219c5d45aSAlexey Dobriyan read_lock(&tasklist_lock); 4531da177e4SLinus Torvalds points = badness(task, uptime.tv_sec); 45419c5d45aSAlexey Dobriyan read_unlock(&tasklist_lock); 4551da177e4SLinus Torvalds return sprintf(buffer, "%lu\n", points); 4561da177e4SLinus Torvalds } 4571da177e4SLinus Torvalds 458d85f50d5SNeil Horman struct limit_names { 459d85f50d5SNeil Horman char *name; 460d85f50d5SNeil Horman char *unit; 461d85f50d5SNeil Horman }; 462d85f50d5SNeil Horman 463d85f50d5SNeil Horman static const struct limit_names lnames[RLIM_NLIMITS] = { 464d85f50d5SNeil Horman [RLIMIT_CPU] = {"Max cpu time", "ms"}, 465d85f50d5SNeil Horman [RLIMIT_FSIZE] = {"Max file size", "bytes"}, 466d85f50d5SNeil Horman [RLIMIT_DATA] = {"Max data size", "bytes"}, 467d85f50d5SNeil Horman [RLIMIT_STACK] = {"Max stack size", "bytes"}, 468d85f50d5SNeil Horman [RLIMIT_CORE] = {"Max core file size", "bytes"}, 469d85f50d5SNeil Horman [RLIMIT_RSS] = {"Max resident set", "bytes"}, 470d85f50d5SNeil Horman [RLIMIT_NPROC] = {"Max processes", "processes"}, 471d85f50d5SNeil Horman [RLIMIT_NOFILE] = {"Max open files", "files"}, 472d85f50d5SNeil Horman [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"}, 473d85f50d5SNeil Horman [RLIMIT_AS] = {"Max address space", "bytes"}, 474d85f50d5SNeil Horman [RLIMIT_LOCKS] = {"Max file locks", "locks"}, 475d85f50d5SNeil Horman [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"}, 476d85f50d5SNeil Horman [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"}, 477d85f50d5SNeil Horman [RLIMIT_NICE] = {"Max nice priority", NULL}, 478d85f50d5SNeil Horman [RLIMIT_RTPRIO] = {"Max realtime priority", NULL}, 4798808117cSEugene Teo [RLIMIT_RTTIME] = {"Max realtime timeout", "us"}, 480d85f50d5SNeil Horman }; 481d85f50d5SNeil Horman 482d85f50d5SNeil Horman /* Display limits for a process */ 483d85f50d5SNeil Horman static int proc_pid_limits(struct task_struct *task, char *buffer) 484d85f50d5SNeil Horman { 485d85f50d5SNeil Horman unsigned int i; 486d85f50d5SNeil Horman int count = 0; 487d85f50d5SNeil Horman unsigned long flags; 488d85f50d5SNeil Horman char *bufptr = buffer; 489d85f50d5SNeil Horman 490d85f50d5SNeil Horman struct rlimit rlim[RLIM_NLIMITS]; 491d85f50d5SNeil Horman 492a6bebbc8SLai Jiangshan if (!lock_task_sighand(task, &flags)) 493d85f50d5SNeil Horman return 0; 494d85f50d5SNeil Horman memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS); 495d85f50d5SNeil Horman unlock_task_sighand(task, &flags); 496d85f50d5SNeil Horman 497d85f50d5SNeil Horman /* 498d85f50d5SNeil Horman * print the file header 499d85f50d5SNeil Horman */ 500d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n", 501d85f50d5SNeil Horman "Limit", "Soft Limit", "Hard Limit", "Units"); 502d85f50d5SNeil Horman 503d85f50d5SNeil Horman for (i = 0; i < RLIM_NLIMITS; i++) { 504d85f50d5SNeil Horman if (rlim[i].rlim_cur == RLIM_INFINITY) 505d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-25s %-20s ", 506d85f50d5SNeil Horman lnames[i].name, "unlimited"); 507d85f50d5SNeil Horman else 508d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-25s %-20lu ", 509d85f50d5SNeil Horman lnames[i].name, rlim[i].rlim_cur); 510d85f50d5SNeil Horman 511d85f50d5SNeil Horman if (rlim[i].rlim_max == RLIM_INFINITY) 512d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-20s ", "unlimited"); 513d85f50d5SNeil Horman else 514d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-20lu ", 515d85f50d5SNeil Horman rlim[i].rlim_max); 516d85f50d5SNeil Horman 517d85f50d5SNeil Horman if (lnames[i].unit) 518d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-10s\n", 519d85f50d5SNeil Horman lnames[i].unit); 520d85f50d5SNeil Horman else 521d85f50d5SNeil Horman count += sprintf(&bufptr[count], "\n"); 522d85f50d5SNeil Horman } 523d85f50d5SNeil Horman 524d85f50d5SNeil Horman return count; 525d85f50d5SNeil Horman } 526d85f50d5SNeil Horman 527ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK 528ebcb6734SRoland McGrath static int proc_pid_syscall(struct task_struct *task, char *buffer) 529ebcb6734SRoland McGrath { 530ebcb6734SRoland McGrath long nr; 531ebcb6734SRoland McGrath unsigned long args[6], sp, pc; 532ebcb6734SRoland McGrath 533ebcb6734SRoland McGrath if (task_current_syscall(task, &nr, args, 6, &sp, &pc)) 534ebcb6734SRoland McGrath return sprintf(buffer, "running\n"); 535ebcb6734SRoland McGrath 536ebcb6734SRoland McGrath if (nr < 0) 537ebcb6734SRoland McGrath return sprintf(buffer, "%ld 0x%lx 0x%lx\n", nr, sp, pc); 538ebcb6734SRoland McGrath 539ebcb6734SRoland McGrath return sprintf(buffer, 540ebcb6734SRoland McGrath "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n", 541ebcb6734SRoland McGrath nr, 542ebcb6734SRoland McGrath args[0], args[1], args[2], args[3], args[4], args[5], 543ebcb6734SRoland McGrath sp, pc); 544ebcb6734SRoland McGrath } 545ebcb6734SRoland McGrath #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */ 546ebcb6734SRoland McGrath 5471da177e4SLinus Torvalds /************************************************************************/ 5481da177e4SLinus Torvalds /* Here the fs part begins */ 5491da177e4SLinus Torvalds /************************************************************************/ 5501da177e4SLinus Torvalds 5511da177e4SLinus Torvalds /* permission checks */ 552778c1144SEric W. Biederman static int proc_fd_access_allowed(struct inode *inode) 5531da177e4SLinus Torvalds { 554778c1144SEric W. Biederman struct task_struct *task; 555778c1144SEric W. Biederman int allowed = 0; 556df26c40eSEric W. Biederman /* Allow access to a task's file descriptors if it is us or we 557df26c40eSEric W. Biederman * may use ptrace attach to the process and find out that 558df26c40eSEric W. Biederman * information. 559778c1144SEric W. Biederman */ 560778c1144SEric W. Biederman task = get_proc_task(inode); 561df26c40eSEric W. Biederman if (task) { 562006ebb40SStephen Smalley allowed = ptrace_may_access(task, PTRACE_MODE_READ); 563778c1144SEric W. Biederman put_task_struct(task); 564df26c40eSEric W. Biederman } 565778c1144SEric W. Biederman return allowed; 5661da177e4SLinus Torvalds } 5671da177e4SLinus Torvalds 5686d76fa58SLinus Torvalds static int proc_setattr(struct dentry *dentry, struct iattr *attr) 5696d76fa58SLinus Torvalds { 5706d76fa58SLinus Torvalds int error; 5716d76fa58SLinus Torvalds struct inode *inode = dentry->d_inode; 5726d76fa58SLinus Torvalds 5736d76fa58SLinus Torvalds if (attr->ia_valid & ATTR_MODE) 5746d76fa58SLinus Torvalds return -EPERM; 5756d76fa58SLinus Torvalds 5766d76fa58SLinus Torvalds error = inode_change_ok(inode, attr); 5776d76fa58SLinus Torvalds if (!error) 5786d76fa58SLinus Torvalds error = inode_setattr(inode, attr); 5796d76fa58SLinus Torvalds return error; 5806d76fa58SLinus Torvalds } 5816d76fa58SLinus Torvalds 582c5ef1c42SArjan van de Ven static const struct inode_operations proc_def_inode_operations = { 5836d76fa58SLinus Torvalds .setattr = proc_setattr, 5846d76fa58SLinus Torvalds }; 5856d76fa58SLinus Torvalds 586a1a2c409SMiklos Szeredi static int mounts_open_common(struct inode *inode, struct file *file, 587a1a2c409SMiklos Szeredi const struct seq_operations *op) 5881da177e4SLinus Torvalds { 58999f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 590cf7b708cSPavel Emelyanov struct nsproxy *nsp; 5916b3286edSKirill Korotaev struct mnt_namespace *ns = NULL; 592a1a2c409SMiklos Szeredi struct path root; 5935addc5ddSAl Viro struct proc_mounts *p; 5945addc5ddSAl Viro int ret = -EINVAL; 5955addc5ddSAl Viro 59699f89551SEric W. Biederman if (task) { 597cf7b708cSPavel Emelyanov rcu_read_lock(); 598cf7b708cSPavel Emelyanov nsp = task_nsproxy(task); 599cf7b708cSPavel Emelyanov if (nsp) { 600cf7b708cSPavel Emelyanov ns = nsp->mnt_ns; 6016b3286edSKirill Korotaev if (ns) 6026b3286edSKirill Korotaev get_mnt_ns(ns); 603863c4702SAlexey Dobriyan } 604cf7b708cSPavel Emelyanov rcu_read_unlock(); 6057c2c7d99SHugh Dickins if (ns && get_fs_path(task, &root, 1) == 0) 6067c2c7d99SHugh Dickins ret = 0; 60799f89551SEric W. Biederman put_task_struct(task); 60899f89551SEric W. Biederman } 6091da177e4SLinus Torvalds 610a1a2c409SMiklos Szeredi if (!ns) 611a1a2c409SMiklos Szeredi goto err; 6127c2c7d99SHugh Dickins if (ret) 613a1a2c409SMiklos Szeredi goto err_put_ns; 614a1a2c409SMiklos Szeredi 6155addc5ddSAl Viro ret = -ENOMEM; 6165addc5ddSAl Viro p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL); 617a1a2c409SMiklos Szeredi if (!p) 618a1a2c409SMiklos Szeredi goto err_put_path; 619a1a2c409SMiklos Szeredi 6205addc5ddSAl Viro file->private_data = &p->m; 621a1a2c409SMiklos Szeredi ret = seq_open(file, op); 622a1a2c409SMiklos Szeredi if (ret) 623a1a2c409SMiklos Szeredi goto err_free; 624a1a2c409SMiklos Szeredi 625a1a2c409SMiklos Szeredi p->m.private = p; 626a1a2c409SMiklos Szeredi p->ns = ns; 627a1a2c409SMiklos Szeredi p->root = root; 6286b3286edSKirill Korotaev p->event = ns->event; 629a1a2c409SMiklos Szeredi 6305addc5ddSAl Viro return 0; 631a1a2c409SMiklos Szeredi 632a1a2c409SMiklos Szeredi err_free: 6335addc5ddSAl Viro kfree(p); 634a1a2c409SMiklos Szeredi err_put_path: 635a1a2c409SMiklos Szeredi path_put(&root); 636a1a2c409SMiklos Szeredi err_put_ns: 6376b3286edSKirill Korotaev put_mnt_ns(ns); 638a1a2c409SMiklos Szeredi err: 6391da177e4SLinus Torvalds return ret; 6401da177e4SLinus Torvalds } 6411da177e4SLinus Torvalds 6421da177e4SLinus Torvalds static int mounts_release(struct inode *inode, struct file *file) 6431da177e4SLinus Torvalds { 644a1a2c409SMiklos Szeredi struct proc_mounts *p = file->private_data; 645a1a2c409SMiklos Szeredi path_put(&p->root); 646a1a2c409SMiklos Szeredi put_mnt_ns(p->ns); 6471da177e4SLinus Torvalds return seq_release(inode, file); 6481da177e4SLinus Torvalds } 6491da177e4SLinus Torvalds 6505addc5ddSAl Viro static unsigned mounts_poll(struct file *file, poll_table *wait) 6515addc5ddSAl Viro { 6525addc5ddSAl Viro struct proc_mounts *p = file->private_data; 653a1a2c409SMiklos Szeredi struct mnt_namespace *ns = p->ns; 65431b07093SKOSAKI Motohiro unsigned res = POLLIN | POLLRDNORM; 6555addc5ddSAl Viro 6565addc5ddSAl Viro poll_wait(file, &ns->poll, wait); 6575addc5ddSAl Viro 6585addc5ddSAl Viro spin_lock(&vfsmount_lock); 6595addc5ddSAl Viro if (p->event != ns->event) { 6605addc5ddSAl Viro p->event = ns->event; 66131b07093SKOSAKI Motohiro res |= POLLERR | POLLPRI; 6625addc5ddSAl Viro } 6635addc5ddSAl Viro spin_unlock(&vfsmount_lock); 6645addc5ddSAl Viro 6655addc5ddSAl Viro return res; 6665addc5ddSAl Viro } 6675addc5ddSAl Viro 668a1a2c409SMiklos Szeredi static int mounts_open(struct inode *inode, struct file *file) 669a1a2c409SMiklos Szeredi { 670a1a2c409SMiklos Szeredi return mounts_open_common(inode, file, &mounts_op); 671a1a2c409SMiklos Szeredi } 672a1a2c409SMiklos Szeredi 67300977a59SArjan van de Ven static const struct file_operations proc_mounts_operations = { 6741da177e4SLinus Torvalds .open = mounts_open, 6751da177e4SLinus Torvalds .read = seq_read, 6761da177e4SLinus Torvalds .llseek = seq_lseek, 6771da177e4SLinus Torvalds .release = mounts_release, 6785addc5ddSAl Viro .poll = mounts_poll, 6791da177e4SLinus Torvalds }; 6801da177e4SLinus Torvalds 6812d4d4864SRam Pai static int mountinfo_open(struct inode *inode, struct file *file) 6822d4d4864SRam Pai { 6832d4d4864SRam Pai return mounts_open_common(inode, file, &mountinfo_op); 6842d4d4864SRam Pai } 6852d4d4864SRam Pai 6862d4d4864SRam Pai static const struct file_operations proc_mountinfo_operations = { 6872d4d4864SRam Pai .open = mountinfo_open, 6882d4d4864SRam Pai .read = seq_read, 6892d4d4864SRam Pai .llseek = seq_lseek, 6902d4d4864SRam Pai .release = mounts_release, 6912d4d4864SRam Pai .poll = mounts_poll, 6922d4d4864SRam Pai }; 6932d4d4864SRam Pai 694b4629fe2SChuck Lever static int mountstats_open(struct inode *inode, struct file *file) 695b4629fe2SChuck Lever { 696a1a2c409SMiklos Szeredi return mounts_open_common(inode, file, &mountstats_op); 697b4629fe2SChuck Lever } 698b4629fe2SChuck Lever 69900977a59SArjan van de Ven static const struct file_operations proc_mountstats_operations = { 700b4629fe2SChuck Lever .open = mountstats_open, 701b4629fe2SChuck Lever .read = seq_read, 702b4629fe2SChuck Lever .llseek = seq_lseek, 703b4629fe2SChuck Lever .release = mounts_release, 704b4629fe2SChuck Lever }; 705b4629fe2SChuck Lever 7061da177e4SLinus Torvalds #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */ 7071da177e4SLinus Torvalds 7081da177e4SLinus Torvalds static ssize_t proc_info_read(struct file * file, char __user * buf, 7091da177e4SLinus Torvalds size_t count, loff_t *ppos) 7101da177e4SLinus Torvalds { 7112fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 7121da177e4SLinus Torvalds unsigned long page; 7131da177e4SLinus Torvalds ssize_t length; 71499f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 71599f89551SEric W. Biederman 71699f89551SEric W. Biederman length = -ESRCH; 71799f89551SEric W. Biederman if (!task) 71899f89551SEric W. Biederman goto out_no_task; 7191da177e4SLinus Torvalds 7201da177e4SLinus Torvalds if (count > PROC_BLOCK_SIZE) 7211da177e4SLinus Torvalds count = PROC_BLOCK_SIZE; 72299f89551SEric W. Biederman 72399f89551SEric W. Biederman length = -ENOMEM; 724e12ba74dSMel Gorman if (!(page = __get_free_page(GFP_TEMPORARY))) 72599f89551SEric W. Biederman goto out; 7261da177e4SLinus Torvalds 7271da177e4SLinus Torvalds length = PROC_I(inode)->op.proc_read(task, (char*)page); 7281da177e4SLinus Torvalds 7291da177e4SLinus Torvalds if (length >= 0) 7301da177e4SLinus Torvalds length = simple_read_from_buffer(buf, count, ppos, (char *)page, length); 7311da177e4SLinus Torvalds free_page(page); 73299f89551SEric W. Biederman out: 73399f89551SEric W. Biederman put_task_struct(task); 73499f89551SEric W. Biederman out_no_task: 7351da177e4SLinus Torvalds return length; 7361da177e4SLinus Torvalds } 7371da177e4SLinus Torvalds 73800977a59SArjan van de Ven static const struct file_operations proc_info_file_operations = { 7391da177e4SLinus Torvalds .read = proc_info_read, 7401da177e4SLinus Torvalds }; 7411da177e4SLinus Torvalds 742be614086SEric W. Biederman static int proc_single_show(struct seq_file *m, void *v) 743be614086SEric W. Biederman { 744be614086SEric W. Biederman struct inode *inode = m->private; 745be614086SEric W. Biederman struct pid_namespace *ns; 746be614086SEric W. Biederman struct pid *pid; 747be614086SEric W. Biederman struct task_struct *task; 748be614086SEric W. Biederman int ret; 749be614086SEric W. Biederman 750be614086SEric W. Biederman ns = inode->i_sb->s_fs_info; 751be614086SEric W. Biederman pid = proc_pid(inode); 752be614086SEric W. Biederman task = get_pid_task(pid, PIDTYPE_PID); 753be614086SEric W. Biederman if (!task) 754be614086SEric W. Biederman return -ESRCH; 755be614086SEric W. Biederman 756be614086SEric W. Biederman ret = PROC_I(inode)->op.proc_show(m, ns, pid, task); 757be614086SEric W. Biederman 758be614086SEric W. Biederman put_task_struct(task); 759be614086SEric W. Biederman return ret; 760be614086SEric W. Biederman } 761be614086SEric W. Biederman 762be614086SEric W. Biederman static int proc_single_open(struct inode *inode, struct file *filp) 763be614086SEric W. Biederman { 764be614086SEric W. Biederman int ret; 765be614086SEric W. Biederman ret = single_open(filp, proc_single_show, NULL); 766be614086SEric W. Biederman if (!ret) { 767be614086SEric W. Biederman struct seq_file *m = filp->private_data; 768be614086SEric W. Biederman 769be614086SEric W. Biederman m->private = inode; 770be614086SEric W. Biederman } 771be614086SEric W. Biederman return ret; 772be614086SEric W. Biederman } 773be614086SEric W. Biederman 774be614086SEric W. Biederman static const struct file_operations proc_single_file_operations = { 775be614086SEric W. Biederman .open = proc_single_open, 776be614086SEric W. Biederman .read = seq_read, 777be614086SEric W. Biederman .llseek = seq_lseek, 778be614086SEric W. Biederman .release = single_release, 779be614086SEric W. Biederman }; 780be614086SEric W. Biederman 7811da177e4SLinus Torvalds static int mem_open(struct inode* inode, struct file* file) 7821da177e4SLinus Torvalds { 7831da177e4SLinus Torvalds file->private_data = (void*)((long)current->self_exec_id); 7841da177e4SLinus Torvalds return 0; 7851da177e4SLinus Torvalds } 7861da177e4SLinus Torvalds 7871da177e4SLinus Torvalds static ssize_t mem_read(struct file * file, char __user * buf, 7881da177e4SLinus Torvalds size_t count, loff_t *ppos) 7891da177e4SLinus Torvalds { 7902fddfeefSJosef "Jeff" Sipek struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode); 7911da177e4SLinus Torvalds char *page; 7921da177e4SLinus Torvalds unsigned long src = *ppos; 7931da177e4SLinus Torvalds int ret = -ESRCH; 7941da177e4SLinus Torvalds struct mm_struct *mm; 7951da177e4SLinus Torvalds 79699f89551SEric W. Biederman if (!task) 79799f89551SEric W. Biederman goto out_no_task; 79899f89551SEric W. Biederman 799638fa202SRoland McGrath if (check_mem_permission(task)) 8001da177e4SLinus Torvalds goto out; 8011da177e4SLinus Torvalds 8021da177e4SLinus Torvalds ret = -ENOMEM; 803e12ba74dSMel Gorman page = (char *)__get_free_page(GFP_TEMPORARY); 8041da177e4SLinus Torvalds if (!page) 8051da177e4SLinus Torvalds goto out; 8061da177e4SLinus Torvalds 8071da177e4SLinus Torvalds ret = 0; 8081da177e4SLinus Torvalds 8091da177e4SLinus Torvalds mm = get_task_mm(task); 8101da177e4SLinus Torvalds if (!mm) 8111da177e4SLinus Torvalds goto out_free; 8121da177e4SLinus Torvalds 8131da177e4SLinus Torvalds ret = -EIO; 8141da177e4SLinus Torvalds 8151da177e4SLinus Torvalds if (file->private_data != (void*)((long)current->self_exec_id)) 8161da177e4SLinus Torvalds goto out_put; 8171da177e4SLinus Torvalds 8181da177e4SLinus Torvalds ret = 0; 8191da177e4SLinus Torvalds 8201da177e4SLinus Torvalds while (count > 0) { 8211da177e4SLinus Torvalds int this_len, retval; 8221da177e4SLinus Torvalds 8231da177e4SLinus Torvalds this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count; 8241da177e4SLinus Torvalds retval = access_process_vm(task, src, page, this_len, 0); 825638fa202SRoland McGrath if (!retval || check_mem_permission(task)) { 8261da177e4SLinus Torvalds if (!ret) 8271da177e4SLinus Torvalds ret = -EIO; 8281da177e4SLinus Torvalds break; 8291da177e4SLinus Torvalds } 8301da177e4SLinus Torvalds 8311da177e4SLinus Torvalds if (copy_to_user(buf, page, retval)) { 8321da177e4SLinus Torvalds ret = -EFAULT; 8331da177e4SLinus Torvalds break; 8341da177e4SLinus Torvalds } 8351da177e4SLinus Torvalds 8361da177e4SLinus Torvalds ret += retval; 8371da177e4SLinus Torvalds src += retval; 8381da177e4SLinus Torvalds buf += retval; 8391da177e4SLinus Torvalds count -= retval; 8401da177e4SLinus Torvalds } 8411da177e4SLinus Torvalds *ppos = src; 8421da177e4SLinus Torvalds 8431da177e4SLinus Torvalds out_put: 8441da177e4SLinus Torvalds mmput(mm); 8451da177e4SLinus Torvalds out_free: 8461da177e4SLinus Torvalds free_page((unsigned long) page); 8471da177e4SLinus Torvalds out: 84899f89551SEric W. Biederman put_task_struct(task); 84999f89551SEric W. Biederman out_no_task: 8501da177e4SLinus Torvalds return ret; 8511da177e4SLinus Torvalds } 8521da177e4SLinus Torvalds 8531da177e4SLinus Torvalds #define mem_write NULL 8541da177e4SLinus Torvalds 8551da177e4SLinus Torvalds #ifndef mem_write 8561da177e4SLinus Torvalds /* This is a security hazard */ 85763967fa9SGlauber de Oliveira Costa static ssize_t mem_write(struct file * file, const char __user *buf, 8581da177e4SLinus Torvalds size_t count, loff_t *ppos) 8591da177e4SLinus Torvalds { 860f7ca54f4SFrederik Deweerdt int copied; 8611da177e4SLinus Torvalds char *page; 8622fddfeefSJosef "Jeff" Sipek struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode); 8631da177e4SLinus Torvalds unsigned long dst = *ppos; 8641da177e4SLinus Torvalds 86599f89551SEric W. Biederman copied = -ESRCH; 86699f89551SEric W. Biederman if (!task) 86799f89551SEric W. Biederman goto out_no_task; 8681da177e4SLinus Torvalds 869638fa202SRoland McGrath if (check_mem_permission(task)) 87099f89551SEric W. Biederman goto out; 87199f89551SEric W. Biederman 87299f89551SEric W. Biederman copied = -ENOMEM; 873e12ba74dSMel Gorman page = (char *)__get_free_page(GFP_TEMPORARY); 8741da177e4SLinus Torvalds if (!page) 87599f89551SEric W. Biederman goto out; 8761da177e4SLinus Torvalds 877f7ca54f4SFrederik Deweerdt copied = 0; 8781da177e4SLinus Torvalds while (count > 0) { 8791da177e4SLinus Torvalds int this_len, retval; 8801da177e4SLinus Torvalds 8811da177e4SLinus Torvalds this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count; 8821da177e4SLinus Torvalds if (copy_from_user(page, buf, this_len)) { 8831da177e4SLinus Torvalds copied = -EFAULT; 8841da177e4SLinus Torvalds break; 8851da177e4SLinus Torvalds } 8861da177e4SLinus Torvalds retval = access_process_vm(task, dst, page, this_len, 1); 8871da177e4SLinus Torvalds if (!retval) { 8881da177e4SLinus Torvalds if (!copied) 8891da177e4SLinus Torvalds copied = -EIO; 8901da177e4SLinus Torvalds break; 8911da177e4SLinus Torvalds } 8921da177e4SLinus Torvalds copied += retval; 8931da177e4SLinus Torvalds buf += retval; 8941da177e4SLinus Torvalds dst += retval; 8951da177e4SLinus Torvalds count -= retval; 8961da177e4SLinus Torvalds } 8971da177e4SLinus Torvalds *ppos = dst; 8981da177e4SLinus Torvalds free_page((unsigned long) page); 89999f89551SEric W. Biederman out: 90099f89551SEric W. Biederman put_task_struct(task); 90199f89551SEric W. Biederman out_no_task: 9021da177e4SLinus Torvalds return copied; 9031da177e4SLinus Torvalds } 9041da177e4SLinus Torvalds #endif 9051da177e4SLinus Torvalds 90685863e47SMatt Mackall loff_t mem_lseek(struct file *file, loff_t offset, int orig) 9071da177e4SLinus Torvalds { 9081da177e4SLinus Torvalds switch (orig) { 9091da177e4SLinus Torvalds case 0: 9101da177e4SLinus Torvalds file->f_pos = offset; 9111da177e4SLinus Torvalds break; 9121da177e4SLinus Torvalds case 1: 9131da177e4SLinus Torvalds file->f_pos += offset; 9141da177e4SLinus Torvalds break; 9151da177e4SLinus Torvalds default: 9161da177e4SLinus Torvalds return -EINVAL; 9171da177e4SLinus Torvalds } 9181da177e4SLinus Torvalds force_successful_syscall_return(); 9191da177e4SLinus Torvalds return file->f_pos; 9201da177e4SLinus Torvalds } 9211da177e4SLinus Torvalds 92200977a59SArjan van de Ven static const struct file_operations proc_mem_operations = { 9231da177e4SLinus Torvalds .llseek = mem_lseek, 9241da177e4SLinus Torvalds .read = mem_read, 9251da177e4SLinus Torvalds .write = mem_write, 9261da177e4SLinus Torvalds .open = mem_open, 9271da177e4SLinus Torvalds }; 9281da177e4SLinus Torvalds 929315e28c8SJames Pearson static ssize_t environ_read(struct file *file, char __user *buf, 930315e28c8SJames Pearson size_t count, loff_t *ppos) 931315e28c8SJames Pearson { 932315e28c8SJames Pearson struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 933315e28c8SJames Pearson char *page; 934315e28c8SJames Pearson unsigned long src = *ppos; 935315e28c8SJames Pearson int ret = -ESRCH; 936315e28c8SJames Pearson struct mm_struct *mm; 937315e28c8SJames Pearson 938315e28c8SJames Pearson if (!task) 939315e28c8SJames Pearson goto out_no_task; 940315e28c8SJames Pearson 941006ebb40SStephen Smalley if (!ptrace_may_access(task, PTRACE_MODE_READ)) 942315e28c8SJames Pearson goto out; 943315e28c8SJames Pearson 944315e28c8SJames Pearson ret = -ENOMEM; 945315e28c8SJames Pearson page = (char *)__get_free_page(GFP_TEMPORARY); 946315e28c8SJames Pearson if (!page) 947315e28c8SJames Pearson goto out; 948315e28c8SJames Pearson 949315e28c8SJames Pearson ret = 0; 950315e28c8SJames Pearson 951315e28c8SJames Pearson mm = get_task_mm(task); 952315e28c8SJames Pearson if (!mm) 953315e28c8SJames Pearson goto out_free; 954315e28c8SJames Pearson 955315e28c8SJames Pearson while (count > 0) { 956315e28c8SJames Pearson int this_len, retval, max_len; 957315e28c8SJames Pearson 958315e28c8SJames Pearson this_len = mm->env_end - (mm->env_start + src); 959315e28c8SJames Pearson 960315e28c8SJames Pearson if (this_len <= 0) 961315e28c8SJames Pearson break; 962315e28c8SJames Pearson 963315e28c8SJames Pearson max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count; 964315e28c8SJames Pearson this_len = (this_len > max_len) ? max_len : this_len; 965315e28c8SJames Pearson 966315e28c8SJames Pearson retval = access_process_vm(task, (mm->env_start + src), 967315e28c8SJames Pearson page, this_len, 0); 968315e28c8SJames Pearson 969315e28c8SJames Pearson if (retval <= 0) { 970315e28c8SJames Pearson ret = retval; 971315e28c8SJames Pearson break; 972315e28c8SJames Pearson } 973315e28c8SJames Pearson 974315e28c8SJames Pearson if (copy_to_user(buf, page, retval)) { 975315e28c8SJames Pearson ret = -EFAULT; 976315e28c8SJames Pearson break; 977315e28c8SJames Pearson } 978315e28c8SJames Pearson 979315e28c8SJames Pearson ret += retval; 980315e28c8SJames Pearson src += retval; 981315e28c8SJames Pearson buf += retval; 982315e28c8SJames Pearson count -= retval; 983315e28c8SJames Pearson } 984315e28c8SJames Pearson *ppos = src; 985315e28c8SJames Pearson 986315e28c8SJames Pearson mmput(mm); 987315e28c8SJames Pearson out_free: 988315e28c8SJames Pearson free_page((unsigned long) page); 989315e28c8SJames Pearson out: 990315e28c8SJames Pearson put_task_struct(task); 991315e28c8SJames Pearson out_no_task: 992315e28c8SJames Pearson return ret; 993315e28c8SJames Pearson } 994315e28c8SJames Pearson 995315e28c8SJames Pearson static const struct file_operations proc_environ_operations = { 996315e28c8SJames Pearson .read = environ_read, 997315e28c8SJames Pearson }; 998315e28c8SJames Pearson 9991da177e4SLinus Torvalds static ssize_t oom_adjust_read(struct file *file, char __user *buf, 10001da177e4SLinus Torvalds size_t count, loff_t *ppos) 10011da177e4SLinus Torvalds { 10022fddfeefSJosef "Jeff" Sipek struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode); 10038578cea7SEric W. Biederman char buffer[PROC_NUMBUF]; 10041da177e4SLinus Torvalds size_t len; 100599f89551SEric W. Biederman int oom_adjust; 10061da177e4SLinus Torvalds 100799f89551SEric W. Biederman if (!task) 100899f89551SEric W. Biederman return -ESRCH; 100999f89551SEric W. Biederman oom_adjust = task->oomkilladj; 101099f89551SEric W. Biederman put_task_struct(task); 101199f89551SEric W. Biederman 10128578cea7SEric W. Biederman len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust); 10130c28f287SAkinobu Mita 10140c28f287SAkinobu Mita return simple_read_from_buffer(buf, count, ppos, buffer, len); 10151da177e4SLinus Torvalds } 10161da177e4SLinus Torvalds 10171da177e4SLinus Torvalds static ssize_t oom_adjust_write(struct file *file, const char __user *buf, 10181da177e4SLinus Torvalds size_t count, loff_t *ppos) 10191da177e4SLinus Torvalds { 102099f89551SEric W. Biederman struct task_struct *task; 10218578cea7SEric W. Biederman char buffer[PROC_NUMBUF], *end; 10221da177e4SLinus Torvalds int oom_adjust; 10231da177e4SLinus Torvalds 10248578cea7SEric W. Biederman memset(buffer, 0, sizeof(buffer)); 10258578cea7SEric W. Biederman if (count > sizeof(buffer) - 1) 10268578cea7SEric W. Biederman count = sizeof(buffer) - 1; 10271da177e4SLinus Torvalds if (copy_from_user(buffer, buf, count)) 10281da177e4SLinus Torvalds return -EFAULT; 10291da177e4SLinus Torvalds oom_adjust = simple_strtol(buffer, &end, 0); 10308ac773b4SAlexey Dobriyan if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) && 10318ac773b4SAlexey Dobriyan oom_adjust != OOM_DISABLE) 10321da177e4SLinus Torvalds return -EINVAL; 10331da177e4SLinus Torvalds if (*end == '\n') 10341da177e4SLinus Torvalds end++; 10352fddfeefSJosef "Jeff" Sipek task = get_proc_task(file->f_path.dentry->d_inode); 103699f89551SEric W. Biederman if (!task) 103799f89551SEric W. Biederman return -ESRCH; 10388fb4fc68SGuillem Jover if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) { 10398fb4fc68SGuillem Jover put_task_struct(task); 10408fb4fc68SGuillem Jover return -EACCES; 10418fb4fc68SGuillem Jover } 10421da177e4SLinus Torvalds task->oomkilladj = oom_adjust; 104399f89551SEric W. Biederman put_task_struct(task); 10441da177e4SLinus Torvalds if (end - buffer == 0) 10451da177e4SLinus Torvalds return -EIO; 10461da177e4SLinus Torvalds return end - buffer; 10471da177e4SLinus Torvalds } 10481da177e4SLinus Torvalds 104900977a59SArjan van de Ven static const struct file_operations proc_oom_adjust_operations = { 10501da177e4SLinus Torvalds .read = oom_adjust_read, 10511da177e4SLinus Torvalds .write = oom_adjust_write, 10521da177e4SLinus Torvalds }; 10531da177e4SLinus Torvalds 10541da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL 10551da177e4SLinus Torvalds #define TMPBUFLEN 21 10561da177e4SLinus Torvalds static ssize_t proc_loginuid_read(struct file * file, char __user * buf, 10571da177e4SLinus Torvalds size_t count, loff_t *ppos) 10581da177e4SLinus Torvalds { 10592fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 106099f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 10611da177e4SLinus Torvalds ssize_t length; 10621da177e4SLinus Torvalds char tmpbuf[TMPBUFLEN]; 10631da177e4SLinus Torvalds 106499f89551SEric W. Biederman if (!task) 106599f89551SEric W. Biederman return -ESRCH; 10661da177e4SLinus Torvalds length = scnprintf(tmpbuf, TMPBUFLEN, "%u", 10670c11b942SAl Viro audit_get_loginuid(task)); 106899f89551SEric W. Biederman put_task_struct(task); 10691da177e4SLinus Torvalds return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 10701da177e4SLinus Torvalds } 10711da177e4SLinus Torvalds 10721da177e4SLinus Torvalds static ssize_t proc_loginuid_write(struct file * file, const char __user * buf, 10731da177e4SLinus Torvalds size_t count, loff_t *ppos) 10741da177e4SLinus Torvalds { 10752fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 10761da177e4SLinus Torvalds char *page, *tmp; 10771da177e4SLinus Torvalds ssize_t length; 10781da177e4SLinus Torvalds uid_t loginuid; 10791da177e4SLinus Torvalds 10801da177e4SLinus Torvalds if (!capable(CAP_AUDIT_CONTROL)) 10811da177e4SLinus Torvalds return -EPERM; 10821da177e4SLinus Torvalds 108313b41b09SEric W. Biederman if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) 10841da177e4SLinus Torvalds return -EPERM; 10851da177e4SLinus Torvalds 1086e0182909SAl Viro if (count >= PAGE_SIZE) 1087e0182909SAl Viro count = PAGE_SIZE - 1; 10881da177e4SLinus Torvalds 10891da177e4SLinus Torvalds if (*ppos != 0) { 10901da177e4SLinus Torvalds /* No partial writes. */ 10911da177e4SLinus Torvalds return -EINVAL; 10921da177e4SLinus Torvalds } 1093e12ba74dSMel Gorman page = (char*)__get_free_page(GFP_TEMPORARY); 10941da177e4SLinus Torvalds if (!page) 10951da177e4SLinus Torvalds return -ENOMEM; 10961da177e4SLinus Torvalds length = -EFAULT; 10971da177e4SLinus Torvalds if (copy_from_user(page, buf, count)) 10981da177e4SLinus Torvalds goto out_free_page; 10991da177e4SLinus Torvalds 1100e0182909SAl Viro page[count] = '\0'; 11011da177e4SLinus Torvalds loginuid = simple_strtoul(page, &tmp, 10); 11021da177e4SLinus Torvalds if (tmp == page) { 11031da177e4SLinus Torvalds length = -EINVAL; 11041da177e4SLinus Torvalds goto out_free_page; 11051da177e4SLinus Torvalds 11061da177e4SLinus Torvalds } 110799f89551SEric W. Biederman length = audit_set_loginuid(current, loginuid); 11081da177e4SLinus Torvalds if (likely(length == 0)) 11091da177e4SLinus Torvalds length = count; 11101da177e4SLinus Torvalds 11111da177e4SLinus Torvalds out_free_page: 11121da177e4SLinus Torvalds free_page((unsigned long) page); 11131da177e4SLinus Torvalds return length; 11141da177e4SLinus Torvalds } 11151da177e4SLinus Torvalds 111600977a59SArjan van de Ven static const struct file_operations proc_loginuid_operations = { 11171da177e4SLinus Torvalds .read = proc_loginuid_read, 11181da177e4SLinus Torvalds .write = proc_loginuid_write, 11191da177e4SLinus Torvalds }; 11201e0bd755SEric Paris 11211e0bd755SEric Paris static ssize_t proc_sessionid_read(struct file * file, char __user * buf, 11221e0bd755SEric Paris size_t count, loff_t *ppos) 11231e0bd755SEric Paris { 11241e0bd755SEric Paris struct inode * inode = file->f_path.dentry->d_inode; 11251e0bd755SEric Paris struct task_struct *task = get_proc_task(inode); 11261e0bd755SEric Paris ssize_t length; 11271e0bd755SEric Paris char tmpbuf[TMPBUFLEN]; 11281e0bd755SEric Paris 11291e0bd755SEric Paris if (!task) 11301e0bd755SEric Paris return -ESRCH; 11311e0bd755SEric Paris length = scnprintf(tmpbuf, TMPBUFLEN, "%u", 11321e0bd755SEric Paris audit_get_sessionid(task)); 11331e0bd755SEric Paris put_task_struct(task); 11341e0bd755SEric Paris return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 11351e0bd755SEric Paris } 11361e0bd755SEric Paris 11371e0bd755SEric Paris static const struct file_operations proc_sessionid_operations = { 11381e0bd755SEric Paris .read = proc_sessionid_read, 11391e0bd755SEric Paris }; 11401da177e4SLinus Torvalds #endif 11411da177e4SLinus Torvalds 1142f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION 1143f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_read(struct file * file, char __user * buf, 1144f4f154fdSAkinobu Mita size_t count, loff_t *ppos) 1145f4f154fdSAkinobu Mita { 1146f4f154fdSAkinobu Mita struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 1147f4f154fdSAkinobu Mita char buffer[PROC_NUMBUF]; 1148f4f154fdSAkinobu Mita size_t len; 1149f4f154fdSAkinobu Mita int make_it_fail; 1150f4f154fdSAkinobu Mita 1151f4f154fdSAkinobu Mita if (!task) 1152f4f154fdSAkinobu Mita return -ESRCH; 1153f4f154fdSAkinobu Mita make_it_fail = task->make_it_fail; 1154f4f154fdSAkinobu Mita put_task_struct(task); 1155f4f154fdSAkinobu Mita 1156f4f154fdSAkinobu Mita len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail); 11570c28f287SAkinobu Mita 11580c28f287SAkinobu Mita return simple_read_from_buffer(buf, count, ppos, buffer, len); 1159f4f154fdSAkinobu Mita } 1160f4f154fdSAkinobu Mita 1161f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_write(struct file * file, 1162f4f154fdSAkinobu Mita const char __user * buf, size_t count, loff_t *ppos) 1163f4f154fdSAkinobu Mita { 1164f4f154fdSAkinobu Mita struct task_struct *task; 1165f4f154fdSAkinobu Mita char buffer[PROC_NUMBUF], *end; 1166f4f154fdSAkinobu Mita int make_it_fail; 1167f4f154fdSAkinobu Mita 1168f4f154fdSAkinobu Mita if (!capable(CAP_SYS_RESOURCE)) 1169f4f154fdSAkinobu Mita return -EPERM; 1170f4f154fdSAkinobu Mita memset(buffer, 0, sizeof(buffer)); 1171f4f154fdSAkinobu Mita if (count > sizeof(buffer) - 1) 1172f4f154fdSAkinobu Mita count = sizeof(buffer) - 1; 1173f4f154fdSAkinobu Mita if (copy_from_user(buffer, buf, count)) 1174f4f154fdSAkinobu Mita return -EFAULT; 1175f4f154fdSAkinobu Mita make_it_fail = simple_strtol(buffer, &end, 0); 1176f4f154fdSAkinobu Mita if (*end == '\n') 1177f4f154fdSAkinobu Mita end++; 1178f4f154fdSAkinobu Mita task = get_proc_task(file->f_dentry->d_inode); 1179f4f154fdSAkinobu Mita if (!task) 1180f4f154fdSAkinobu Mita return -ESRCH; 1181f4f154fdSAkinobu Mita task->make_it_fail = make_it_fail; 1182f4f154fdSAkinobu Mita put_task_struct(task); 1183f4f154fdSAkinobu Mita if (end - buffer == 0) 1184f4f154fdSAkinobu Mita return -EIO; 1185f4f154fdSAkinobu Mita return end - buffer; 1186f4f154fdSAkinobu Mita } 1187f4f154fdSAkinobu Mita 118800977a59SArjan van de Ven static const struct file_operations proc_fault_inject_operations = { 1189f4f154fdSAkinobu Mita .read = proc_fault_inject_read, 1190f4f154fdSAkinobu Mita .write = proc_fault_inject_write, 1191f4f154fdSAkinobu Mita }; 1192f4f154fdSAkinobu Mita #endif 1193f4f154fdSAkinobu Mita 11949745512cSArjan van de Ven 119543ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG 119643ae34cbSIngo Molnar /* 119743ae34cbSIngo Molnar * Print out various scheduling related per-task fields: 119843ae34cbSIngo Molnar */ 119943ae34cbSIngo Molnar static int sched_show(struct seq_file *m, void *v) 120043ae34cbSIngo Molnar { 120143ae34cbSIngo Molnar struct inode *inode = m->private; 120243ae34cbSIngo Molnar struct task_struct *p; 120343ae34cbSIngo Molnar 120443ae34cbSIngo Molnar p = get_proc_task(inode); 120543ae34cbSIngo Molnar if (!p) 120643ae34cbSIngo Molnar return -ESRCH; 120743ae34cbSIngo Molnar proc_sched_show_task(p, m); 120843ae34cbSIngo Molnar 120943ae34cbSIngo Molnar put_task_struct(p); 121043ae34cbSIngo Molnar 121143ae34cbSIngo Molnar return 0; 121243ae34cbSIngo Molnar } 121343ae34cbSIngo Molnar 121443ae34cbSIngo Molnar static ssize_t 121543ae34cbSIngo Molnar sched_write(struct file *file, const char __user *buf, 121643ae34cbSIngo Molnar size_t count, loff_t *offset) 121743ae34cbSIngo Molnar { 121843ae34cbSIngo Molnar struct inode *inode = file->f_path.dentry->d_inode; 121943ae34cbSIngo Molnar struct task_struct *p; 122043ae34cbSIngo Molnar 122143ae34cbSIngo Molnar p = get_proc_task(inode); 122243ae34cbSIngo Molnar if (!p) 122343ae34cbSIngo Molnar return -ESRCH; 122443ae34cbSIngo Molnar proc_sched_set_task(p); 122543ae34cbSIngo Molnar 122643ae34cbSIngo Molnar put_task_struct(p); 122743ae34cbSIngo Molnar 122843ae34cbSIngo Molnar return count; 122943ae34cbSIngo Molnar } 123043ae34cbSIngo Molnar 123143ae34cbSIngo Molnar static int sched_open(struct inode *inode, struct file *filp) 123243ae34cbSIngo Molnar { 123343ae34cbSIngo Molnar int ret; 123443ae34cbSIngo Molnar 123543ae34cbSIngo Molnar ret = single_open(filp, sched_show, NULL); 123643ae34cbSIngo Molnar if (!ret) { 123743ae34cbSIngo Molnar struct seq_file *m = filp->private_data; 123843ae34cbSIngo Molnar 123943ae34cbSIngo Molnar m->private = inode; 124043ae34cbSIngo Molnar } 124143ae34cbSIngo Molnar return ret; 124243ae34cbSIngo Molnar } 124343ae34cbSIngo Molnar 124443ae34cbSIngo Molnar static const struct file_operations proc_pid_sched_operations = { 124543ae34cbSIngo Molnar .open = sched_open, 124643ae34cbSIngo Molnar .read = seq_read, 124743ae34cbSIngo Molnar .write = sched_write, 124843ae34cbSIngo Molnar .llseek = seq_lseek, 12495ea473a1SAlexey Dobriyan .release = single_release, 125043ae34cbSIngo Molnar }; 125143ae34cbSIngo Molnar 125243ae34cbSIngo Molnar #endif 125343ae34cbSIngo Molnar 1254925d1c40SMatt Helsley /* 1255925d1c40SMatt Helsley * We added or removed a vma mapping the executable. The vmas are only mapped 1256925d1c40SMatt Helsley * during exec and are not mapped with the mmap system call. 1257925d1c40SMatt Helsley * Callers must hold down_write() on the mm's mmap_sem for these 1258925d1c40SMatt Helsley */ 1259925d1c40SMatt Helsley void added_exe_file_vma(struct mm_struct *mm) 1260925d1c40SMatt Helsley { 1261925d1c40SMatt Helsley mm->num_exe_file_vmas++; 1262925d1c40SMatt Helsley } 1263925d1c40SMatt Helsley 1264925d1c40SMatt Helsley void removed_exe_file_vma(struct mm_struct *mm) 1265925d1c40SMatt Helsley { 1266925d1c40SMatt Helsley mm->num_exe_file_vmas--; 1267925d1c40SMatt Helsley if ((mm->num_exe_file_vmas == 0) && mm->exe_file){ 1268925d1c40SMatt Helsley fput(mm->exe_file); 1269925d1c40SMatt Helsley mm->exe_file = NULL; 1270925d1c40SMatt Helsley } 1271925d1c40SMatt Helsley 1272925d1c40SMatt Helsley } 1273925d1c40SMatt Helsley 1274925d1c40SMatt Helsley void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file) 1275925d1c40SMatt Helsley { 1276925d1c40SMatt Helsley if (new_exe_file) 1277925d1c40SMatt Helsley get_file(new_exe_file); 1278925d1c40SMatt Helsley if (mm->exe_file) 1279925d1c40SMatt Helsley fput(mm->exe_file); 1280925d1c40SMatt Helsley mm->exe_file = new_exe_file; 1281925d1c40SMatt Helsley mm->num_exe_file_vmas = 0; 1282925d1c40SMatt Helsley } 1283925d1c40SMatt Helsley 1284925d1c40SMatt Helsley struct file *get_mm_exe_file(struct mm_struct *mm) 1285925d1c40SMatt Helsley { 1286925d1c40SMatt Helsley struct file *exe_file; 1287925d1c40SMatt Helsley 1288925d1c40SMatt Helsley /* We need mmap_sem to protect against races with removal of 1289925d1c40SMatt Helsley * VM_EXECUTABLE vmas */ 1290925d1c40SMatt Helsley down_read(&mm->mmap_sem); 1291925d1c40SMatt Helsley exe_file = mm->exe_file; 1292925d1c40SMatt Helsley if (exe_file) 1293925d1c40SMatt Helsley get_file(exe_file); 1294925d1c40SMatt Helsley up_read(&mm->mmap_sem); 1295925d1c40SMatt Helsley return exe_file; 1296925d1c40SMatt Helsley } 1297925d1c40SMatt Helsley 1298925d1c40SMatt Helsley void dup_mm_exe_file(struct mm_struct *oldmm, struct mm_struct *newmm) 1299925d1c40SMatt Helsley { 1300925d1c40SMatt Helsley /* It's safe to write the exe_file pointer without exe_file_lock because 1301925d1c40SMatt Helsley * this is called during fork when the task is not yet in /proc */ 1302925d1c40SMatt Helsley newmm->exe_file = get_mm_exe_file(oldmm); 1303925d1c40SMatt Helsley } 1304925d1c40SMatt Helsley 1305925d1c40SMatt Helsley static int proc_exe_link(struct inode *inode, struct path *exe_path) 1306925d1c40SMatt Helsley { 1307925d1c40SMatt Helsley struct task_struct *task; 1308925d1c40SMatt Helsley struct mm_struct *mm; 1309925d1c40SMatt Helsley struct file *exe_file; 1310925d1c40SMatt Helsley 1311925d1c40SMatt Helsley task = get_proc_task(inode); 1312925d1c40SMatt Helsley if (!task) 1313925d1c40SMatt Helsley return -ENOENT; 1314925d1c40SMatt Helsley mm = get_task_mm(task); 1315925d1c40SMatt Helsley put_task_struct(task); 1316925d1c40SMatt Helsley if (!mm) 1317925d1c40SMatt Helsley return -ENOENT; 1318925d1c40SMatt Helsley exe_file = get_mm_exe_file(mm); 1319925d1c40SMatt Helsley mmput(mm); 1320925d1c40SMatt Helsley if (exe_file) { 1321925d1c40SMatt Helsley *exe_path = exe_file->f_path; 1322925d1c40SMatt Helsley path_get(&exe_file->f_path); 1323925d1c40SMatt Helsley fput(exe_file); 1324925d1c40SMatt Helsley return 0; 1325925d1c40SMatt Helsley } else 1326925d1c40SMatt Helsley return -ENOENT; 1327925d1c40SMatt Helsley } 1328925d1c40SMatt Helsley 1329008b150aSAl Viro static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd) 13301da177e4SLinus Torvalds { 13311da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 13321da177e4SLinus Torvalds int error = -EACCES; 13331da177e4SLinus Torvalds 13341da177e4SLinus Torvalds /* We don't need a base pointer in the /proc filesystem */ 13351d957f9bSJan Blunck path_put(&nd->path); 13361da177e4SLinus Torvalds 1337778c1144SEric W. Biederman /* Are we allowed to snoop on the tasks file descriptors? */ 1338778c1144SEric W. Biederman if (!proc_fd_access_allowed(inode)) 13391da177e4SLinus Torvalds goto out; 13401da177e4SLinus Torvalds 13413dcd25f3SJan Blunck error = PROC_I(inode)->op.proc_get_link(inode, &nd->path); 13421da177e4SLinus Torvalds nd->last_type = LAST_BIND; 13431da177e4SLinus Torvalds out: 1344008b150aSAl Viro return ERR_PTR(error); 13451da177e4SLinus Torvalds } 13461da177e4SLinus Torvalds 13473dcd25f3SJan Blunck static int do_proc_readlink(struct path *path, char __user *buffer, int buflen) 13481da177e4SLinus Torvalds { 1349e12ba74dSMel Gorman char *tmp = (char*)__get_free_page(GFP_TEMPORARY); 13503dcd25f3SJan Blunck char *pathname; 13511da177e4SLinus Torvalds int len; 13521da177e4SLinus Torvalds 13531da177e4SLinus Torvalds if (!tmp) 13541da177e4SLinus Torvalds return -ENOMEM; 13551da177e4SLinus Torvalds 1356cf28b486SJan Blunck pathname = d_path(path, tmp, PAGE_SIZE); 13573dcd25f3SJan Blunck len = PTR_ERR(pathname); 13583dcd25f3SJan Blunck if (IS_ERR(pathname)) 13591da177e4SLinus Torvalds goto out; 13603dcd25f3SJan Blunck len = tmp + PAGE_SIZE - 1 - pathname; 13611da177e4SLinus Torvalds 13621da177e4SLinus Torvalds if (len > buflen) 13631da177e4SLinus Torvalds len = buflen; 13643dcd25f3SJan Blunck if (copy_to_user(buffer, pathname, len)) 13651da177e4SLinus Torvalds len = -EFAULT; 13661da177e4SLinus Torvalds out: 13671da177e4SLinus Torvalds free_page((unsigned long)tmp); 13681da177e4SLinus Torvalds return len; 13691da177e4SLinus Torvalds } 13701da177e4SLinus Torvalds 13711da177e4SLinus Torvalds static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen) 13721da177e4SLinus Torvalds { 13731da177e4SLinus Torvalds int error = -EACCES; 13741da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 13753dcd25f3SJan Blunck struct path path; 13761da177e4SLinus Torvalds 1377778c1144SEric W. Biederman /* Are we allowed to snoop on the tasks file descriptors? */ 1378778c1144SEric W. Biederman if (!proc_fd_access_allowed(inode)) 13791da177e4SLinus Torvalds goto out; 13801da177e4SLinus Torvalds 13813dcd25f3SJan Blunck error = PROC_I(inode)->op.proc_get_link(inode, &path); 13821da177e4SLinus Torvalds if (error) 13831da177e4SLinus Torvalds goto out; 13841da177e4SLinus Torvalds 13853dcd25f3SJan Blunck error = do_proc_readlink(&path, buffer, buflen); 13863dcd25f3SJan Blunck path_put(&path); 13871da177e4SLinus Torvalds out: 13881da177e4SLinus Torvalds return error; 13891da177e4SLinus Torvalds } 13901da177e4SLinus Torvalds 1391c5ef1c42SArjan van de Ven static const struct inode_operations proc_pid_link_inode_operations = { 13921da177e4SLinus Torvalds .readlink = proc_pid_readlink, 13936d76fa58SLinus Torvalds .follow_link = proc_pid_follow_link, 13946d76fa58SLinus Torvalds .setattr = proc_setattr, 13951da177e4SLinus Torvalds }; 13961da177e4SLinus Torvalds 139728a6d671SEric W. Biederman 139828a6d671SEric W. Biederman /* building an inode */ 139928a6d671SEric W. Biederman 140028a6d671SEric W. Biederman static int task_dumpable(struct task_struct *task) 140128a6d671SEric W. Biederman { 140228a6d671SEric W. Biederman int dumpable = 0; 140328a6d671SEric W. Biederman struct mm_struct *mm; 140428a6d671SEric W. Biederman 140528a6d671SEric W. Biederman task_lock(task); 140628a6d671SEric W. Biederman mm = task->mm; 140728a6d671SEric W. Biederman if (mm) 14086c5d5238SKawai, Hidehiro dumpable = get_dumpable(mm); 140928a6d671SEric W. Biederman task_unlock(task); 141028a6d671SEric W. Biederman if(dumpable == 1) 141128a6d671SEric W. Biederman return 1; 141228a6d671SEric W. Biederman return 0; 141328a6d671SEric W. Biederman } 141428a6d671SEric W. Biederman 141528a6d671SEric W. Biederman 141661a28784SEric W. Biederman static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task) 141728a6d671SEric W. Biederman { 141828a6d671SEric W. Biederman struct inode * inode; 141928a6d671SEric W. Biederman struct proc_inode *ei; 1420c69e8d9cSDavid Howells const struct cred *cred; 142128a6d671SEric W. Biederman 142228a6d671SEric W. Biederman /* We need a new inode */ 142328a6d671SEric W. Biederman 142428a6d671SEric W. Biederman inode = new_inode(sb); 142528a6d671SEric W. Biederman if (!inode) 142628a6d671SEric W. Biederman goto out; 142728a6d671SEric W. Biederman 142828a6d671SEric W. Biederman /* Common stuff */ 142928a6d671SEric W. Biederman ei = PROC_I(inode); 143028a6d671SEric W. Biederman inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; 143128a6d671SEric W. Biederman inode->i_op = &proc_def_inode_operations; 143228a6d671SEric W. Biederman 143328a6d671SEric W. Biederman /* 143428a6d671SEric W. Biederman * grab the reference to task. 143528a6d671SEric W. Biederman */ 14361a657f78SOleg Nesterov ei->pid = get_task_pid(task, PIDTYPE_PID); 143728a6d671SEric W. Biederman if (!ei->pid) 143828a6d671SEric W. Biederman goto out_unlock; 143928a6d671SEric W. Biederman 144028a6d671SEric W. Biederman if (task_dumpable(task)) { 1441c69e8d9cSDavid Howells rcu_read_lock(); 1442c69e8d9cSDavid Howells cred = __task_cred(task); 1443c69e8d9cSDavid Howells inode->i_uid = cred->euid; 1444c69e8d9cSDavid Howells inode->i_gid = cred->egid; 1445c69e8d9cSDavid Howells rcu_read_unlock(); 144628a6d671SEric W. Biederman } 144728a6d671SEric W. Biederman security_task_to_inode(task, inode); 144828a6d671SEric W. Biederman 144928a6d671SEric W. Biederman out: 145028a6d671SEric W. Biederman return inode; 145128a6d671SEric W. Biederman 145228a6d671SEric W. Biederman out_unlock: 145328a6d671SEric W. Biederman iput(inode); 145428a6d671SEric W. Biederman return NULL; 145528a6d671SEric W. Biederman } 145628a6d671SEric W. Biederman 145728a6d671SEric W. Biederman static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) 145828a6d671SEric W. Biederman { 145928a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 146028a6d671SEric W. Biederman struct task_struct *task; 1461c69e8d9cSDavid Howells const struct cred *cred; 1462c69e8d9cSDavid Howells 146328a6d671SEric W. Biederman generic_fillattr(inode, stat); 146428a6d671SEric W. Biederman 146528a6d671SEric W. Biederman rcu_read_lock(); 146628a6d671SEric W. Biederman stat->uid = 0; 146728a6d671SEric W. Biederman stat->gid = 0; 146828a6d671SEric W. Biederman task = pid_task(proc_pid(inode), PIDTYPE_PID); 146928a6d671SEric W. Biederman if (task) { 147028a6d671SEric W. Biederman if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) || 147128a6d671SEric W. Biederman task_dumpable(task)) { 1472c69e8d9cSDavid Howells cred = __task_cred(task); 1473c69e8d9cSDavid Howells stat->uid = cred->euid; 1474c69e8d9cSDavid Howells stat->gid = cred->egid; 147528a6d671SEric W. Biederman } 147628a6d671SEric W. Biederman } 147728a6d671SEric W. Biederman rcu_read_unlock(); 147828a6d671SEric W. Biederman return 0; 147928a6d671SEric W. Biederman } 148028a6d671SEric W. Biederman 148128a6d671SEric W. Biederman /* dentry stuff */ 148228a6d671SEric W. Biederman 148328a6d671SEric W. Biederman /* 148428a6d671SEric W. Biederman * Exceptional case: normally we are not allowed to unhash a busy 148528a6d671SEric W. Biederman * directory. In this case, however, we can do it - no aliasing problems 148628a6d671SEric W. Biederman * due to the way we treat inodes. 148728a6d671SEric W. Biederman * 148828a6d671SEric W. Biederman * Rewrite the inode's ownerships here because the owning task may have 148928a6d671SEric W. Biederman * performed a setuid(), etc. 149028a6d671SEric W. Biederman * 149128a6d671SEric W. Biederman * Before the /proc/pid/status file was created the only way to read 149228a6d671SEric W. Biederman * the effective uid of a /process was to stat /proc/pid. Reading 149328a6d671SEric W. Biederman * /proc/pid/status is slow enough that procps and other packages 149428a6d671SEric W. Biederman * kept stating /proc/pid. To keep the rules in /proc simple I have 149528a6d671SEric W. Biederman * made this apply to all per process world readable and executable 149628a6d671SEric W. Biederman * directories. 149728a6d671SEric W. Biederman */ 149828a6d671SEric W. Biederman static int pid_revalidate(struct dentry *dentry, struct nameidata *nd) 149928a6d671SEric W. Biederman { 150028a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 150128a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 1502c69e8d9cSDavid Howells const struct cred *cred; 1503c69e8d9cSDavid Howells 150428a6d671SEric W. Biederman if (task) { 150528a6d671SEric W. Biederman if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) || 150628a6d671SEric W. Biederman task_dumpable(task)) { 1507c69e8d9cSDavid Howells rcu_read_lock(); 1508c69e8d9cSDavid Howells cred = __task_cred(task); 1509c69e8d9cSDavid Howells inode->i_uid = cred->euid; 1510c69e8d9cSDavid Howells inode->i_gid = cred->egid; 1511c69e8d9cSDavid Howells rcu_read_unlock(); 151228a6d671SEric W. Biederman } else { 151328a6d671SEric W. Biederman inode->i_uid = 0; 151428a6d671SEric W. Biederman inode->i_gid = 0; 151528a6d671SEric W. Biederman } 151628a6d671SEric W. Biederman inode->i_mode &= ~(S_ISUID | S_ISGID); 151728a6d671SEric W. Biederman security_task_to_inode(task, inode); 151828a6d671SEric W. Biederman put_task_struct(task); 151928a6d671SEric W. Biederman return 1; 152028a6d671SEric W. Biederman } 152128a6d671SEric W. Biederman d_drop(dentry); 152228a6d671SEric W. Biederman return 0; 152328a6d671SEric W. Biederman } 152428a6d671SEric W. Biederman 152528a6d671SEric W. Biederman static int pid_delete_dentry(struct dentry * dentry) 152628a6d671SEric W. Biederman { 152728a6d671SEric W. Biederman /* Is the task we represent dead? 152828a6d671SEric W. Biederman * If so, then don't put the dentry on the lru list, 152928a6d671SEric W. Biederman * kill it immediately. 153028a6d671SEric W. Biederman */ 153128a6d671SEric W. Biederman return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first; 153228a6d671SEric W. Biederman } 153328a6d671SEric W. Biederman 1534d72f71ebSAl Viro static const struct dentry_operations pid_dentry_operations = 153528a6d671SEric W. Biederman { 153628a6d671SEric W. Biederman .d_revalidate = pid_revalidate, 153728a6d671SEric W. Biederman .d_delete = pid_delete_dentry, 153828a6d671SEric W. Biederman }; 153928a6d671SEric W. Biederman 154028a6d671SEric W. Biederman /* Lookups */ 154128a6d671SEric W. Biederman 1542c5141e6dSEric Dumazet typedef struct dentry *instantiate_t(struct inode *, struct dentry *, 1543c5141e6dSEric Dumazet struct task_struct *, const void *); 154461a28784SEric W. Biederman 15451c0d04c9SEric W. Biederman /* 15461c0d04c9SEric W. Biederman * Fill a directory entry. 15471c0d04c9SEric W. Biederman * 15481c0d04c9SEric W. Biederman * If possible create the dcache entry and derive our inode number and 15491c0d04c9SEric W. Biederman * file type from dcache entry. 15501c0d04c9SEric W. Biederman * 15511c0d04c9SEric W. Biederman * Since all of the proc inode numbers are dynamically generated, the inode 15521c0d04c9SEric W. Biederman * numbers do not exist until the inode is cache. This means creating the 15531c0d04c9SEric W. Biederman * the dcache entry in readdir is necessary to keep the inode numbers 15541c0d04c9SEric W. Biederman * reported by readdir in sync with the inode numbers reported 15551c0d04c9SEric W. Biederman * by stat. 15561c0d04c9SEric W. Biederman */ 155761a28784SEric W. Biederman static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir, 155861a28784SEric W. Biederman char *name, int len, 1559c5141e6dSEric Dumazet instantiate_t instantiate, struct task_struct *task, const void *ptr) 156061a28784SEric W. Biederman { 15612fddfeefSJosef "Jeff" Sipek struct dentry *child, *dir = filp->f_path.dentry; 156261a28784SEric W. Biederman struct inode *inode; 156361a28784SEric W. Biederman struct qstr qname; 156461a28784SEric W. Biederman ino_t ino = 0; 156561a28784SEric W. Biederman unsigned type = DT_UNKNOWN; 156661a28784SEric W. Biederman 156761a28784SEric W. Biederman qname.name = name; 156861a28784SEric W. Biederman qname.len = len; 156961a28784SEric W. Biederman qname.hash = full_name_hash(name, len); 157061a28784SEric W. Biederman 157161a28784SEric W. Biederman child = d_lookup(dir, &qname); 157261a28784SEric W. Biederman if (!child) { 157361a28784SEric W. Biederman struct dentry *new; 157461a28784SEric W. Biederman new = d_alloc(dir, &qname); 157561a28784SEric W. Biederman if (new) { 157661a28784SEric W. Biederman child = instantiate(dir->d_inode, new, task, ptr); 157761a28784SEric W. Biederman if (child) 157861a28784SEric W. Biederman dput(new); 157961a28784SEric W. Biederman else 158061a28784SEric W. Biederman child = new; 158161a28784SEric W. Biederman } 158261a28784SEric W. Biederman } 158361a28784SEric W. Biederman if (!child || IS_ERR(child) || !child->d_inode) 158461a28784SEric W. Biederman goto end_instantiate; 158561a28784SEric W. Biederman inode = child->d_inode; 158661a28784SEric W. Biederman if (inode) { 158761a28784SEric W. Biederman ino = inode->i_ino; 158861a28784SEric W. Biederman type = inode->i_mode >> 12; 158961a28784SEric W. Biederman } 159061a28784SEric W. Biederman dput(child); 159161a28784SEric W. Biederman end_instantiate: 159261a28784SEric W. Biederman if (!ino) 159361a28784SEric W. Biederman ino = find_inode_number(dir, &qname); 159461a28784SEric W. Biederman if (!ino) 159561a28784SEric W. Biederman ino = 1; 159661a28784SEric W. Biederman return filldir(dirent, name, len, filp->f_pos, ino, type); 159761a28784SEric W. Biederman } 159861a28784SEric W. Biederman 159928a6d671SEric W. Biederman static unsigned name_to_int(struct dentry *dentry) 160028a6d671SEric W. Biederman { 160128a6d671SEric W. Biederman const char *name = dentry->d_name.name; 160228a6d671SEric W. Biederman int len = dentry->d_name.len; 160328a6d671SEric W. Biederman unsigned n = 0; 160428a6d671SEric W. Biederman 160528a6d671SEric W. Biederman if (len > 1 && *name == '0') 160628a6d671SEric W. Biederman goto out; 160728a6d671SEric W. Biederman while (len-- > 0) { 160828a6d671SEric W. Biederman unsigned c = *name++ - '0'; 160928a6d671SEric W. Biederman if (c > 9) 161028a6d671SEric W. Biederman goto out; 161128a6d671SEric W. Biederman if (n >= (~0U-9)/10) 161228a6d671SEric W. Biederman goto out; 161328a6d671SEric W. Biederman n *= 10; 161428a6d671SEric W. Biederman n += c; 161528a6d671SEric W. Biederman } 161628a6d671SEric W. Biederman return n; 161728a6d671SEric W. Biederman out: 161828a6d671SEric W. Biederman return ~0U; 161928a6d671SEric W. Biederman } 162028a6d671SEric W. Biederman 162127932742SMiklos Szeredi #define PROC_FDINFO_MAX 64 162227932742SMiklos Szeredi 16233dcd25f3SJan Blunck static int proc_fd_info(struct inode *inode, struct path *path, char *info) 162428a6d671SEric W. Biederman { 162528a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 162628a6d671SEric W. Biederman struct files_struct *files = NULL; 162728a6d671SEric W. Biederman struct file *file; 162828a6d671SEric W. Biederman int fd = proc_fd(inode); 162928a6d671SEric W. Biederman 163028a6d671SEric W. Biederman if (task) { 163128a6d671SEric W. Biederman files = get_files_struct(task); 163228a6d671SEric W. Biederman put_task_struct(task); 163328a6d671SEric W. Biederman } 163428a6d671SEric W. Biederman if (files) { 163528a6d671SEric W. Biederman /* 163628a6d671SEric W. Biederman * We are not taking a ref to the file structure, so we must 163728a6d671SEric W. Biederman * hold ->file_lock. 163828a6d671SEric W. Biederman */ 163928a6d671SEric W. Biederman spin_lock(&files->file_lock); 164028a6d671SEric W. Biederman file = fcheck_files(files, fd); 164128a6d671SEric W. Biederman if (file) { 16423dcd25f3SJan Blunck if (path) { 16433dcd25f3SJan Blunck *path = file->f_path; 16443dcd25f3SJan Blunck path_get(&file->f_path); 16453dcd25f3SJan Blunck } 164627932742SMiklos Szeredi if (info) 164727932742SMiklos Szeredi snprintf(info, PROC_FDINFO_MAX, 164827932742SMiklos Szeredi "pos:\t%lli\n" 164927932742SMiklos Szeredi "flags:\t0%o\n", 165027932742SMiklos Szeredi (long long) file->f_pos, 165127932742SMiklos Szeredi file->f_flags); 165228a6d671SEric W. Biederman spin_unlock(&files->file_lock); 165328a6d671SEric W. Biederman put_files_struct(files); 165428a6d671SEric W. Biederman return 0; 165528a6d671SEric W. Biederman } 165628a6d671SEric W. Biederman spin_unlock(&files->file_lock); 165728a6d671SEric W. Biederman put_files_struct(files); 165828a6d671SEric W. Biederman } 165928a6d671SEric W. Biederman return -ENOENT; 166028a6d671SEric W. Biederman } 166128a6d671SEric W. Biederman 16623dcd25f3SJan Blunck static int proc_fd_link(struct inode *inode, struct path *path) 166327932742SMiklos Szeredi { 16643dcd25f3SJan Blunck return proc_fd_info(inode, path, NULL); 166527932742SMiklos Szeredi } 166627932742SMiklos Szeredi 166728a6d671SEric W. Biederman static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd) 166828a6d671SEric W. Biederman { 166928a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 167028a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 167128a6d671SEric W. Biederman int fd = proc_fd(inode); 167228a6d671SEric W. Biederman struct files_struct *files; 1673c69e8d9cSDavid Howells const struct cred *cred; 167428a6d671SEric W. Biederman 167528a6d671SEric W. Biederman if (task) { 167628a6d671SEric W. Biederman files = get_files_struct(task); 167728a6d671SEric W. Biederman if (files) { 167828a6d671SEric W. Biederman rcu_read_lock(); 167928a6d671SEric W. Biederman if (fcheck_files(files, fd)) { 168028a6d671SEric W. Biederman rcu_read_unlock(); 168128a6d671SEric W. Biederman put_files_struct(files); 168228a6d671SEric W. Biederman if (task_dumpable(task)) { 1683c69e8d9cSDavid Howells rcu_read_lock(); 1684c69e8d9cSDavid Howells cred = __task_cred(task); 1685c69e8d9cSDavid Howells inode->i_uid = cred->euid; 1686c69e8d9cSDavid Howells inode->i_gid = cred->egid; 1687c69e8d9cSDavid Howells rcu_read_unlock(); 168828a6d671SEric W. Biederman } else { 168928a6d671SEric W. Biederman inode->i_uid = 0; 169028a6d671SEric W. Biederman inode->i_gid = 0; 169128a6d671SEric W. Biederman } 169228a6d671SEric W. Biederman inode->i_mode &= ~(S_ISUID | S_ISGID); 169328a6d671SEric W. Biederman security_task_to_inode(task, inode); 169428a6d671SEric W. Biederman put_task_struct(task); 169528a6d671SEric W. Biederman return 1; 169628a6d671SEric W. Biederman } 169728a6d671SEric W. Biederman rcu_read_unlock(); 169828a6d671SEric W. Biederman put_files_struct(files); 169928a6d671SEric W. Biederman } 170028a6d671SEric W. Biederman put_task_struct(task); 170128a6d671SEric W. Biederman } 170228a6d671SEric W. Biederman d_drop(dentry); 170328a6d671SEric W. Biederman return 0; 170428a6d671SEric W. Biederman } 170528a6d671SEric W. Biederman 1706d72f71ebSAl Viro static const struct dentry_operations tid_fd_dentry_operations = 170728a6d671SEric W. Biederman { 170828a6d671SEric W. Biederman .d_revalidate = tid_fd_revalidate, 170928a6d671SEric W. Biederman .d_delete = pid_delete_dentry, 171028a6d671SEric W. Biederman }; 171128a6d671SEric W. Biederman 1712444ceed8SEric W. Biederman static struct dentry *proc_fd_instantiate(struct inode *dir, 1713c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 171428a6d671SEric W. Biederman { 1715c5141e6dSEric Dumazet unsigned fd = *(const unsigned *)ptr; 171628a6d671SEric W. Biederman struct file *file; 171728a6d671SEric W. Biederman struct files_struct *files; 171828a6d671SEric W. Biederman struct inode *inode; 171928a6d671SEric W. Biederman struct proc_inode *ei; 1720444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-ENOENT); 172128a6d671SEric W. Biederman 172261a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 172328a6d671SEric W. Biederman if (!inode) 172428a6d671SEric W. Biederman goto out; 172528a6d671SEric W. Biederman ei = PROC_I(inode); 172628a6d671SEric W. Biederman ei->fd = fd; 172728a6d671SEric W. Biederman files = get_files_struct(task); 172828a6d671SEric W. Biederman if (!files) 1729444ceed8SEric W. Biederman goto out_iput; 173028a6d671SEric W. Biederman inode->i_mode = S_IFLNK; 173128a6d671SEric W. Biederman 173228a6d671SEric W. Biederman /* 173328a6d671SEric W. Biederman * We are not taking a ref to the file structure, so we must 173428a6d671SEric W. Biederman * hold ->file_lock. 173528a6d671SEric W. Biederman */ 173628a6d671SEric W. Biederman spin_lock(&files->file_lock); 173728a6d671SEric W. Biederman file = fcheck_files(files, fd); 173828a6d671SEric W. Biederman if (!file) 1739444ceed8SEric W. Biederman goto out_unlock; 1740aeb5d727SAl Viro if (file->f_mode & FMODE_READ) 174128a6d671SEric W. Biederman inode->i_mode |= S_IRUSR | S_IXUSR; 1742aeb5d727SAl Viro if (file->f_mode & FMODE_WRITE) 174328a6d671SEric W. Biederman inode->i_mode |= S_IWUSR | S_IXUSR; 174428a6d671SEric W. Biederman spin_unlock(&files->file_lock); 174528a6d671SEric W. Biederman put_files_struct(files); 1746444ceed8SEric W. Biederman 174728a6d671SEric W. Biederman inode->i_op = &proc_pid_link_inode_operations; 174828a6d671SEric W. Biederman inode->i_size = 64; 174928a6d671SEric W. Biederman ei->op.proc_get_link = proc_fd_link; 175028a6d671SEric W. Biederman dentry->d_op = &tid_fd_dentry_operations; 175128a6d671SEric W. Biederman d_add(dentry, inode); 175228a6d671SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 175328a6d671SEric W. Biederman if (tid_fd_revalidate(dentry, NULL)) 1754444ceed8SEric W. Biederman error = NULL; 1755444ceed8SEric W. Biederman 1756444ceed8SEric W. Biederman out: 1757444ceed8SEric W. Biederman return error; 1758444ceed8SEric W. Biederman out_unlock: 1759444ceed8SEric W. Biederman spin_unlock(&files->file_lock); 1760444ceed8SEric W. Biederman put_files_struct(files); 1761444ceed8SEric W. Biederman out_iput: 1762444ceed8SEric W. Biederman iput(inode); 1763444ceed8SEric W. Biederman goto out; 1764444ceed8SEric W. Biederman } 1765444ceed8SEric W. Biederman 176627932742SMiklos Szeredi static struct dentry *proc_lookupfd_common(struct inode *dir, 176727932742SMiklos Szeredi struct dentry *dentry, 176827932742SMiklos Szeredi instantiate_t instantiate) 1769444ceed8SEric W. Biederman { 1770444ceed8SEric W. Biederman struct task_struct *task = get_proc_task(dir); 1771444ceed8SEric W. Biederman unsigned fd = name_to_int(dentry); 1772444ceed8SEric W. Biederman struct dentry *result = ERR_PTR(-ENOENT); 1773444ceed8SEric W. Biederman 1774444ceed8SEric W. Biederman if (!task) 1775444ceed8SEric W. Biederman goto out_no_task; 1776444ceed8SEric W. Biederman if (fd == ~0U) 1777444ceed8SEric W. Biederman goto out; 1778444ceed8SEric W. Biederman 177927932742SMiklos Szeredi result = instantiate(dir, dentry, task, &fd); 178028a6d671SEric W. Biederman out: 178128a6d671SEric W. Biederman put_task_struct(task); 178228a6d671SEric W. Biederman out_no_task: 178328a6d671SEric W. Biederman return result; 178428a6d671SEric W. Biederman } 178528a6d671SEric W. Biederman 178627932742SMiklos Szeredi static int proc_readfd_common(struct file * filp, void * dirent, 178727932742SMiklos Szeredi filldir_t filldir, instantiate_t instantiate) 17881da177e4SLinus Torvalds { 17892fddfeefSJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 17905634708bSEric W. Biederman struct inode *inode = dentry->d_inode; 179199f89551SEric W. Biederman struct task_struct *p = get_proc_task(inode); 1792457c2510SPavel Emelyanov unsigned int fd, ino; 17931da177e4SLinus Torvalds int retval; 17941da177e4SLinus Torvalds struct files_struct * files; 17951da177e4SLinus Torvalds 17961da177e4SLinus Torvalds retval = -ENOENT; 179799f89551SEric W. Biederman if (!p) 179899f89551SEric W. Biederman goto out_no_task; 17991da177e4SLinus Torvalds retval = 0; 18001da177e4SLinus Torvalds 18011da177e4SLinus Torvalds fd = filp->f_pos; 18021da177e4SLinus Torvalds switch (fd) { 18031da177e4SLinus Torvalds case 0: 18041da177e4SLinus Torvalds if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0) 18051da177e4SLinus Torvalds goto out; 18061da177e4SLinus Torvalds filp->f_pos++; 18071da177e4SLinus Torvalds case 1: 18085634708bSEric W. Biederman ino = parent_ino(dentry); 18091da177e4SLinus Torvalds if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0) 18101da177e4SLinus Torvalds goto out; 18111da177e4SLinus Torvalds filp->f_pos++; 18121da177e4SLinus Torvalds default: 18131da177e4SLinus Torvalds files = get_files_struct(p); 18141da177e4SLinus Torvalds if (!files) 18151da177e4SLinus Torvalds goto out; 1816b835996fSDipankar Sarma rcu_read_lock(); 18171da177e4SLinus Torvalds for (fd = filp->f_pos-2; 18189b4f526cSAl Viro fd < files_fdtable(files)->max_fds; 18191da177e4SLinus Torvalds fd++, filp->f_pos++) { 182027932742SMiklos Szeredi char name[PROC_NUMBUF]; 182127932742SMiklos Szeredi int len; 18221da177e4SLinus Torvalds 18231da177e4SLinus Torvalds if (!fcheck_files(files, fd)) 18241da177e4SLinus Torvalds continue; 1825b835996fSDipankar Sarma rcu_read_unlock(); 18261da177e4SLinus Torvalds 182727932742SMiklos Szeredi len = snprintf(name, sizeof(name), "%d", fd); 182827932742SMiklos Szeredi if (proc_fill_cache(filp, dirent, filldir, 182927932742SMiklos Szeredi name, len, instantiate, 183027932742SMiklos Szeredi p, &fd) < 0) { 1831b835996fSDipankar Sarma rcu_read_lock(); 18321da177e4SLinus Torvalds break; 18331da177e4SLinus Torvalds } 1834b835996fSDipankar Sarma rcu_read_lock(); 18351da177e4SLinus Torvalds } 1836b835996fSDipankar Sarma rcu_read_unlock(); 18371da177e4SLinus Torvalds put_files_struct(files); 18381da177e4SLinus Torvalds } 18391da177e4SLinus Torvalds out: 184099f89551SEric W. Biederman put_task_struct(p); 184199f89551SEric W. Biederman out_no_task: 18421da177e4SLinus Torvalds return retval; 18431da177e4SLinus Torvalds } 18441da177e4SLinus Torvalds 184527932742SMiklos Szeredi static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry, 184627932742SMiklos Szeredi struct nameidata *nd) 184727932742SMiklos Szeredi { 184827932742SMiklos Szeredi return proc_lookupfd_common(dir, dentry, proc_fd_instantiate); 184927932742SMiklos Szeredi } 185027932742SMiklos Szeredi 185127932742SMiklos Szeredi static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir) 185227932742SMiklos Szeredi { 185327932742SMiklos Szeredi return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate); 185427932742SMiklos Szeredi } 185527932742SMiklos Szeredi 185627932742SMiklos Szeredi static ssize_t proc_fdinfo_read(struct file *file, char __user *buf, 185727932742SMiklos Szeredi size_t len, loff_t *ppos) 185827932742SMiklos Szeredi { 185927932742SMiklos Szeredi char tmp[PROC_FDINFO_MAX]; 18603dcd25f3SJan Blunck int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp); 186127932742SMiklos Szeredi if (!err) 186227932742SMiklos Szeredi err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp)); 186327932742SMiklos Szeredi return err; 186427932742SMiklos Szeredi } 186527932742SMiklos Szeredi 186627932742SMiklos Szeredi static const struct file_operations proc_fdinfo_file_operations = { 186727932742SMiklos Szeredi .open = nonseekable_open, 186827932742SMiklos Szeredi .read = proc_fdinfo_read, 186927932742SMiklos Szeredi }; 187027932742SMiklos Szeredi 187100977a59SArjan van de Ven static const struct file_operations proc_fd_operations = { 18721da177e4SLinus Torvalds .read = generic_read_dir, 18731da177e4SLinus Torvalds .readdir = proc_readfd, 18741da177e4SLinus Torvalds }; 18751da177e4SLinus Torvalds 18761da177e4SLinus Torvalds /* 18778948e11fSAlexey Dobriyan * /proc/pid/fd needs a special permission handler so that a process can still 18788948e11fSAlexey Dobriyan * access /proc/self/fd after it has executed a setuid(). 18798948e11fSAlexey Dobriyan */ 1880e6305c43SAl Viro static int proc_fd_permission(struct inode *inode, int mask) 18818948e11fSAlexey Dobriyan { 18828948e11fSAlexey Dobriyan int rv; 18838948e11fSAlexey Dobriyan 18848948e11fSAlexey Dobriyan rv = generic_permission(inode, mask, NULL); 18858948e11fSAlexey Dobriyan if (rv == 0) 18868948e11fSAlexey Dobriyan return 0; 18878948e11fSAlexey Dobriyan if (task_pid(current) == proc_pid(inode)) 18888948e11fSAlexey Dobriyan rv = 0; 18898948e11fSAlexey Dobriyan return rv; 18908948e11fSAlexey Dobriyan } 18918948e11fSAlexey Dobriyan 18928948e11fSAlexey Dobriyan /* 18931da177e4SLinus Torvalds * proc directories can do almost nothing.. 18941da177e4SLinus Torvalds */ 1895c5ef1c42SArjan van de Ven static const struct inode_operations proc_fd_inode_operations = { 18961da177e4SLinus Torvalds .lookup = proc_lookupfd, 18978948e11fSAlexey Dobriyan .permission = proc_fd_permission, 18986d76fa58SLinus Torvalds .setattr = proc_setattr, 18991da177e4SLinus Torvalds }; 19001da177e4SLinus Torvalds 190127932742SMiklos Szeredi static struct dentry *proc_fdinfo_instantiate(struct inode *dir, 190227932742SMiklos Szeredi struct dentry *dentry, struct task_struct *task, const void *ptr) 190327932742SMiklos Szeredi { 190427932742SMiklos Szeredi unsigned fd = *(unsigned *)ptr; 190527932742SMiklos Szeredi struct inode *inode; 190627932742SMiklos Szeredi struct proc_inode *ei; 190727932742SMiklos Szeredi struct dentry *error = ERR_PTR(-ENOENT); 190827932742SMiklos Szeredi 190927932742SMiklos Szeredi inode = proc_pid_make_inode(dir->i_sb, task); 191027932742SMiklos Szeredi if (!inode) 191127932742SMiklos Szeredi goto out; 191227932742SMiklos Szeredi ei = PROC_I(inode); 191327932742SMiklos Szeredi ei->fd = fd; 191427932742SMiklos Szeredi inode->i_mode = S_IFREG | S_IRUSR; 191527932742SMiklos Szeredi inode->i_fop = &proc_fdinfo_file_operations; 191627932742SMiklos Szeredi dentry->d_op = &tid_fd_dentry_operations; 191727932742SMiklos Szeredi d_add(dentry, inode); 191827932742SMiklos Szeredi /* Close the race of the process dying before we return the dentry */ 191927932742SMiklos Szeredi if (tid_fd_revalidate(dentry, NULL)) 192027932742SMiklos Szeredi error = NULL; 192127932742SMiklos Szeredi 192227932742SMiklos Szeredi out: 192327932742SMiklos Szeredi return error; 192427932742SMiklos Szeredi } 192527932742SMiklos Szeredi 192627932742SMiklos Szeredi static struct dentry *proc_lookupfdinfo(struct inode *dir, 192727932742SMiklos Szeredi struct dentry *dentry, 192827932742SMiklos Szeredi struct nameidata *nd) 192927932742SMiklos Szeredi { 193027932742SMiklos Szeredi return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate); 193127932742SMiklos Szeredi } 193227932742SMiklos Szeredi 193327932742SMiklos Szeredi static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir) 193427932742SMiklos Szeredi { 193527932742SMiklos Szeredi return proc_readfd_common(filp, dirent, filldir, 193627932742SMiklos Szeredi proc_fdinfo_instantiate); 193727932742SMiklos Szeredi } 193827932742SMiklos Szeredi 193927932742SMiklos Szeredi static const struct file_operations proc_fdinfo_operations = { 194027932742SMiklos Szeredi .read = generic_read_dir, 194127932742SMiklos Szeredi .readdir = proc_readfdinfo, 194227932742SMiklos Szeredi }; 194327932742SMiklos Szeredi 194427932742SMiklos Szeredi /* 194527932742SMiklos Szeredi * proc directories can do almost nothing.. 194627932742SMiklos Szeredi */ 194727932742SMiklos Szeredi static const struct inode_operations proc_fdinfo_inode_operations = { 194827932742SMiklos Szeredi .lookup = proc_lookupfdinfo, 194927932742SMiklos Szeredi .setattr = proc_setattr, 195027932742SMiklos Szeredi }; 195127932742SMiklos Szeredi 195227932742SMiklos Szeredi 1953444ceed8SEric W. Biederman static struct dentry *proc_pident_instantiate(struct inode *dir, 1954c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 1955444ceed8SEric W. Biederman { 1956c5141e6dSEric Dumazet const struct pid_entry *p = ptr; 1957444ceed8SEric W. Biederman struct inode *inode; 1958444ceed8SEric W. Biederman struct proc_inode *ei; 1959444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-EINVAL); 1960444ceed8SEric W. Biederman 196161a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 1962444ceed8SEric W. Biederman if (!inode) 1963444ceed8SEric W. Biederman goto out; 1964444ceed8SEric W. Biederman 1965444ceed8SEric W. Biederman ei = PROC_I(inode); 1966444ceed8SEric W. Biederman inode->i_mode = p->mode; 1967444ceed8SEric W. Biederman if (S_ISDIR(inode->i_mode)) 1968444ceed8SEric W. Biederman inode->i_nlink = 2; /* Use getattr to fix if necessary */ 1969444ceed8SEric W. Biederman if (p->iop) 1970444ceed8SEric W. Biederman inode->i_op = p->iop; 1971444ceed8SEric W. Biederman if (p->fop) 1972444ceed8SEric W. Biederman inode->i_fop = p->fop; 1973444ceed8SEric W. Biederman ei->op = p->op; 1974444ceed8SEric W. Biederman dentry->d_op = &pid_dentry_operations; 1975444ceed8SEric W. Biederman d_add(dentry, inode); 1976444ceed8SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 1977444ceed8SEric W. Biederman if (pid_revalidate(dentry, NULL)) 1978444ceed8SEric W. Biederman error = NULL; 1979444ceed8SEric W. Biederman out: 1980444ceed8SEric W. Biederman return error; 1981444ceed8SEric W. Biederman } 1982444ceed8SEric W. Biederman 19831da177e4SLinus Torvalds static struct dentry *proc_pident_lookup(struct inode *dir, 19841da177e4SLinus Torvalds struct dentry *dentry, 1985c5141e6dSEric Dumazet const struct pid_entry *ents, 19867bcd6b0eSEric W. Biederman unsigned int nents) 19871da177e4SLinus Torvalds { 1988cd6a3ce9SEric W. Biederman struct dentry *error; 198999f89551SEric W. Biederman struct task_struct *task = get_proc_task(dir); 1990c5141e6dSEric Dumazet const struct pid_entry *p, *last; 19911da177e4SLinus Torvalds 1992cd6a3ce9SEric W. Biederman error = ERR_PTR(-ENOENT); 19931da177e4SLinus Torvalds 199499f89551SEric W. Biederman if (!task) 199599f89551SEric W. Biederman goto out_no_task; 19961da177e4SLinus Torvalds 199720cdc894SEric W. Biederman /* 199820cdc894SEric W. Biederman * Yes, it does not scale. And it should not. Don't add 199920cdc894SEric W. Biederman * new entries into /proc/<tgid>/ without very good reasons. 200020cdc894SEric W. Biederman */ 20017bcd6b0eSEric W. Biederman last = &ents[nents - 1]; 20027bcd6b0eSEric W. Biederman for (p = ents; p <= last; p++) { 20031da177e4SLinus Torvalds if (p->len != dentry->d_name.len) 20041da177e4SLinus Torvalds continue; 20051da177e4SLinus Torvalds if (!memcmp(dentry->d_name.name, p->name, p->len)) 20061da177e4SLinus Torvalds break; 20071da177e4SLinus Torvalds } 20087bcd6b0eSEric W. Biederman if (p > last) 20091da177e4SLinus Torvalds goto out; 20101da177e4SLinus Torvalds 2011444ceed8SEric W. Biederman error = proc_pident_instantiate(dir, dentry, task, p); 20121da177e4SLinus Torvalds out: 201399f89551SEric W. Biederman put_task_struct(task); 201499f89551SEric W. Biederman out_no_task: 2015cd6a3ce9SEric W. Biederman return error; 20161da177e4SLinus Torvalds } 20171da177e4SLinus Torvalds 2018c5141e6dSEric Dumazet static int proc_pident_fill_cache(struct file *filp, void *dirent, 2019c5141e6dSEric Dumazet filldir_t filldir, struct task_struct *task, const struct pid_entry *p) 202061a28784SEric W. Biederman { 202161a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, p->name, p->len, 202261a28784SEric W. Biederman proc_pident_instantiate, task, p); 202361a28784SEric W. Biederman } 202461a28784SEric W. Biederman 202528a6d671SEric W. Biederman static int proc_pident_readdir(struct file *filp, 202628a6d671SEric W. Biederman void *dirent, filldir_t filldir, 2027c5141e6dSEric Dumazet const struct pid_entry *ents, unsigned int nents) 202828a6d671SEric W. Biederman { 202928a6d671SEric W. Biederman int i; 20302fddfeefSJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 203128a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 203228a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 2033c5141e6dSEric Dumazet const struct pid_entry *p, *last; 203428a6d671SEric W. Biederman ino_t ino; 203528a6d671SEric W. Biederman int ret; 203628a6d671SEric W. Biederman 203728a6d671SEric W. Biederman ret = -ENOENT; 203828a6d671SEric W. Biederman if (!task) 203961a28784SEric W. Biederman goto out_no_task; 204028a6d671SEric W. Biederman 204128a6d671SEric W. Biederman ret = 0; 204228a6d671SEric W. Biederman i = filp->f_pos; 204328a6d671SEric W. Biederman switch (i) { 204428a6d671SEric W. Biederman case 0: 204528a6d671SEric W. Biederman ino = inode->i_ino; 204628a6d671SEric W. Biederman if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0) 204728a6d671SEric W. Biederman goto out; 204828a6d671SEric W. Biederman i++; 204928a6d671SEric W. Biederman filp->f_pos++; 205028a6d671SEric W. Biederman /* fall through */ 205128a6d671SEric W. Biederman case 1: 205228a6d671SEric W. Biederman ino = parent_ino(dentry); 205328a6d671SEric W. Biederman if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0) 205428a6d671SEric W. Biederman goto out; 205528a6d671SEric W. Biederman i++; 205628a6d671SEric W. Biederman filp->f_pos++; 205728a6d671SEric W. Biederman /* fall through */ 205828a6d671SEric W. Biederman default: 205928a6d671SEric W. Biederman i -= 2; 206028a6d671SEric W. Biederman if (i >= nents) { 206128a6d671SEric W. Biederman ret = 1; 206228a6d671SEric W. Biederman goto out; 206328a6d671SEric W. Biederman } 206428a6d671SEric W. Biederman p = ents + i; 20657bcd6b0eSEric W. Biederman last = &ents[nents - 1]; 20667bcd6b0eSEric W. Biederman while (p <= last) { 206761a28784SEric W. Biederman if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0) 206828a6d671SEric W. Biederman goto out; 206928a6d671SEric W. Biederman filp->f_pos++; 207028a6d671SEric W. Biederman p++; 207128a6d671SEric W. Biederman } 20721da177e4SLinus Torvalds } 20731da177e4SLinus Torvalds 207428a6d671SEric W. Biederman ret = 1; 207528a6d671SEric W. Biederman out: 207661a28784SEric W. Biederman put_task_struct(task); 207761a28784SEric W. Biederman out_no_task: 207828a6d671SEric W. Biederman return ret; 20791da177e4SLinus Torvalds } 20801da177e4SLinus Torvalds 20811da177e4SLinus Torvalds #ifdef CONFIG_SECURITY 208228a6d671SEric W. Biederman static ssize_t proc_pid_attr_read(struct file * file, char __user * buf, 208328a6d671SEric W. Biederman size_t count, loff_t *ppos) 208428a6d671SEric W. Biederman { 20852fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 208604ff9708SAl Viro char *p = NULL; 208728a6d671SEric W. Biederman ssize_t length; 208828a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 208928a6d671SEric W. Biederman 209028a6d671SEric W. Biederman if (!task) 209104ff9708SAl Viro return -ESRCH; 209228a6d671SEric W. Biederman 209328a6d671SEric W. Biederman length = security_getprocattr(task, 20942fddfeefSJosef "Jeff" Sipek (char*)file->f_path.dentry->d_name.name, 209504ff9708SAl Viro &p); 209628a6d671SEric W. Biederman put_task_struct(task); 209704ff9708SAl Viro if (length > 0) 209804ff9708SAl Viro length = simple_read_from_buffer(buf, count, ppos, p, length); 209904ff9708SAl Viro kfree(p); 210028a6d671SEric W. Biederman return length; 210128a6d671SEric W. Biederman } 210228a6d671SEric W. Biederman 210328a6d671SEric W. Biederman static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf, 210428a6d671SEric W. Biederman size_t count, loff_t *ppos) 210528a6d671SEric W. Biederman { 21062fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 210728a6d671SEric W. Biederman char *page; 210828a6d671SEric W. Biederman ssize_t length; 210928a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 211028a6d671SEric W. Biederman 211128a6d671SEric W. Biederman length = -ESRCH; 211228a6d671SEric W. Biederman if (!task) 211328a6d671SEric W. Biederman goto out_no_task; 211428a6d671SEric W. Biederman if (count > PAGE_SIZE) 211528a6d671SEric W. Biederman count = PAGE_SIZE; 211628a6d671SEric W. Biederman 211728a6d671SEric W. Biederman /* No partial writes. */ 211828a6d671SEric W. Biederman length = -EINVAL; 211928a6d671SEric W. Biederman if (*ppos != 0) 212028a6d671SEric W. Biederman goto out; 212128a6d671SEric W. Biederman 212228a6d671SEric W. Biederman length = -ENOMEM; 2123e12ba74dSMel Gorman page = (char*)__get_free_page(GFP_TEMPORARY); 212428a6d671SEric W. Biederman if (!page) 212528a6d671SEric W. Biederman goto out; 212628a6d671SEric W. Biederman 212728a6d671SEric W. Biederman length = -EFAULT; 212828a6d671SEric W. Biederman if (copy_from_user(page, buf, count)) 212928a6d671SEric W. Biederman goto out_free; 213028a6d671SEric W. Biederman 2131*107db7c7SDavid Howells /* Guard against adverse ptrace interaction */ 2132*107db7c7SDavid Howells length = mutex_lock_interruptible(&task->cred_guard_mutex); 2133*107db7c7SDavid Howells if (length < 0) 2134*107db7c7SDavid Howells goto out_free; 2135*107db7c7SDavid Howells 213628a6d671SEric W. Biederman length = security_setprocattr(task, 21372fddfeefSJosef "Jeff" Sipek (char*)file->f_path.dentry->d_name.name, 213828a6d671SEric W. Biederman (void*)page, count); 2139*107db7c7SDavid Howells mutex_unlock(&task->cred_guard_mutex); 214028a6d671SEric W. Biederman out_free: 214128a6d671SEric W. Biederman free_page((unsigned long) page); 214228a6d671SEric W. Biederman out: 214328a6d671SEric W. Biederman put_task_struct(task); 214428a6d671SEric W. Biederman out_no_task: 214528a6d671SEric W. Biederman return length; 214628a6d671SEric W. Biederman } 214728a6d671SEric W. Biederman 214800977a59SArjan van de Ven static const struct file_operations proc_pid_attr_operations = { 214928a6d671SEric W. Biederman .read = proc_pid_attr_read, 215028a6d671SEric W. Biederman .write = proc_pid_attr_write, 215128a6d671SEric W. Biederman }; 215228a6d671SEric W. Biederman 2153c5141e6dSEric Dumazet static const struct pid_entry attr_dir_stuff[] = { 2154631f9c18SAlexey Dobriyan REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations), 2155631f9c18SAlexey Dobriyan REG("prev", S_IRUGO, proc_pid_attr_operations), 2156631f9c18SAlexey Dobriyan REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations), 2157631f9c18SAlexey Dobriyan REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations), 2158631f9c18SAlexey Dobriyan REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations), 2159631f9c18SAlexey Dobriyan REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations), 216028a6d671SEric W. Biederman }; 216128a6d671SEric W. Biederman 216272d9dcfcSEric W. Biederman static int proc_attr_dir_readdir(struct file * filp, 21631da177e4SLinus Torvalds void * dirent, filldir_t filldir) 21641da177e4SLinus Torvalds { 21651da177e4SLinus Torvalds return proc_pident_readdir(filp,dirent,filldir, 216672d9dcfcSEric W. Biederman attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff)); 21671da177e4SLinus Torvalds } 21681da177e4SLinus Torvalds 216900977a59SArjan van de Ven static const struct file_operations proc_attr_dir_operations = { 21701da177e4SLinus Torvalds .read = generic_read_dir, 217172d9dcfcSEric W. Biederman .readdir = proc_attr_dir_readdir, 21721da177e4SLinus Torvalds }; 21731da177e4SLinus Torvalds 217472d9dcfcSEric W. Biederman static struct dentry *proc_attr_dir_lookup(struct inode *dir, 21751da177e4SLinus Torvalds struct dentry *dentry, struct nameidata *nd) 21761da177e4SLinus Torvalds { 21777bcd6b0eSEric W. Biederman return proc_pident_lookup(dir, dentry, 21787bcd6b0eSEric W. Biederman attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff)); 21791da177e4SLinus Torvalds } 21801da177e4SLinus Torvalds 2181c5ef1c42SArjan van de Ven static const struct inode_operations proc_attr_dir_inode_operations = { 218272d9dcfcSEric W. Biederman .lookup = proc_attr_dir_lookup, 218399f89551SEric W. Biederman .getattr = pid_getattr, 21846d76fa58SLinus Torvalds .setattr = proc_setattr, 21851da177e4SLinus Torvalds }; 21861da177e4SLinus Torvalds 21871da177e4SLinus Torvalds #endif 21881da177e4SLinus Torvalds 21893cb4a0bbSKawai, Hidehiro #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE) 21903cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf, 21913cb4a0bbSKawai, Hidehiro size_t count, loff_t *ppos) 21923cb4a0bbSKawai, Hidehiro { 21933cb4a0bbSKawai, Hidehiro struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 21943cb4a0bbSKawai, Hidehiro struct mm_struct *mm; 21953cb4a0bbSKawai, Hidehiro char buffer[PROC_NUMBUF]; 21963cb4a0bbSKawai, Hidehiro size_t len; 21973cb4a0bbSKawai, Hidehiro int ret; 21983cb4a0bbSKawai, Hidehiro 21993cb4a0bbSKawai, Hidehiro if (!task) 22003cb4a0bbSKawai, Hidehiro return -ESRCH; 22013cb4a0bbSKawai, Hidehiro 22023cb4a0bbSKawai, Hidehiro ret = 0; 22033cb4a0bbSKawai, Hidehiro mm = get_task_mm(task); 22043cb4a0bbSKawai, Hidehiro if (mm) { 22053cb4a0bbSKawai, Hidehiro len = snprintf(buffer, sizeof(buffer), "%08lx\n", 22063cb4a0bbSKawai, Hidehiro ((mm->flags & MMF_DUMP_FILTER_MASK) >> 22073cb4a0bbSKawai, Hidehiro MMF_DUMP_FILTER_SHIFT)); 22083cb4a0bbSKawai, Hidehiro mmput(mm); 22093cb4a0bbSKawai, Hidehiro ret = simple_read_from_buffer(buf, count, ppos, buffer, len); 22103cb4a0bbSKawai, Hidehiro } 22113cb4a0bbSKawai, Hidehiro 22123cb4a0bbSKawai, Hidehiro put_task_struct(task); 22133cb4a0bbSKawai, Hidehiro 22143cb4a0bbSKawai, Hidehiro return ret; 22153cb4a0bbSKawai, Hidehiro } 22163cb4a0bbSKawai, Hidehiro 22173cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_write(struct file *file, 22183cb4a0bbSKawai, Hidehiro const char __user *buf, 22193cb4a0bbSKawai, Hidehiro size_t count, 22203cb4a0bbSKawai, Hidehiro loff_t *ppos) 22213cb4a0bbSKawai, Hidehiro { 22223cb4a0bbSKawai, Hidehiro struct task_struct *task; 22233cb4a0bbSKawai, Hidehiro struct mm_struct *mm; 22243cb4a0bbSKawai, Hidehiro char buffer[PROC_NUMBUF], *end; 22253cb4a0bbSKawai, Hidehiro unsigned int val; 22263cb4a0bbSKawai, Hidehiro int ret; 22273cb4a0bbSKawai, Hidehiro int i; 22283cb4a0bbSKawai, Hidehiro unsigned long mask; 22293cb4a0bbSKawai, Hidehiro 22303cb4a0bbSKawai, Hidehiro ret = -EFAULT; 22313cb4a0bbSKawai, Hidehiro memset(buffer, 0, sizeof(buffer)); 22323cb4a0bbSKawai, Hidehiro if (count > sizeof(buffer) - 1) 22333cb4a0bbSKawai, Hidehiro count = sizeof(buffer) - 1; 22343cb4a0bbSKawai, Hidehiro if (copy_from_user(buffer, buf, count)) 22353cb4a0bbSKawai, Hidehiro goto out_no_task; 22363cb4a0bbSKawai, Hidehiro 22373cb4a0bbSKawai, Hidehiro ret = -EINVAL; 22383cb4a0bbSKawai, Hidehiro val = (unsigned int)simple_strtoul(buffer, &end, 0); 22393cb4a0bbSKawai, Hidehiro if (*end == '\n') 22403cb4a0bbSKawai, Hidehiro end++; 22413cb4a0bbSKawai, Hidehiro if (end - buffer == 0) 22423cb4a0bbSKawai, Hidehiro goto out_no_task; 22433cb4a0bbSKawai, Hidehiro 22443cb4a0bbSKawai, Hidehiro ret = -ESRCH; 22453cb4a0bbSKawai, Hidehiro task = get_proc_task(file->f_dentry->d_inode); 22463cb4a0bbSKawai, Hidehiro if (!task) 22473cb4a0bbSKawai, Hidehiro goto out_no_task; 22483cb4a0bbSKawai, Hidehiro 22493cb4a0bbSKawai, Hidehiro ret = end - buffer; 22503cb4a0bbSKawai, Hidehiro mm = get_task_mm(task); 22513cb4a0bbSKawai, Hidehiro if (!mm) 22523cb4a0bbSKawai, Hidehiro goto out_no_mm; 22533cb4a0bbSKawai, Hidehiro 22543cb4a0bbSKawai, Hidehiro for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) { 22553cb4a0bbSKawai, Hidehiro if (val & mask) 22563cb4a0bbSKawai, Hidehiro set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags); 22573cb4a0bbSKawai, Hidehiro else 22583cb4a0bbSKawai, Hidehiro clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags); 22593cb4a0bbSKawai, Hidehiro } 22603cb4a0bbSKawai, Hidehiro 22613cb4a0bbSKawai, Hidehiro mmput(mm); 22623cb4a0bbSKawai, Hidehiro out_no_mm: 22633cb4a0bbSKawai, Hidehiro put_task_struct(task); 22643cb4a0bbSKawai, Hidehiro out_no_task: 22653cb4a0bbSKawai, Hidehiro return ret; 22663cb4a0bbSKawai, Hidehiro } 22673cb4a0bbSKawai, Hidehiro 22683cb4a0bbSKawai, Hidehiro static const struct file_operations proc_coredump_filter_operations = { 22693cb4a0bbSKawai, Hidehiro .read = proc_coredump_filter_read, 22703cb4a0bbSKawai, Hidehiro .write = proc_coredump_filter_write, 22713cb4a0bbSKawai, Hidehiro }; 22723cb4a0bbSKawai, Hidehiro #endif 22733cb4a0bbSKawai, Hidehiro 22741da177e4SLinus Torvalds /* 22751da177e4SLinus Torvalds * /proc/self: 22761da177e4SLinus Torvalds */ 22771da177e4SLinus Torvalds static int proc_self_readlink(struct dentry *dentry, char __user *buffer, 22781da177e4SLinus Torvalds int buflen) 22791da177e4SLinus Torvalds { 2280488e5bc4SEric W. Biederman struct pid_namespace *ns = dentry->d_sb->s_fs_info; 2281b55fcb22SAndrew Morton pid_t tgid = task_tgid_nr_ns(current, ns); 22828578cea7SEric W. Biederman char tmp[PROC_NUMBUF]; 2283b55fcb22SAndrew Morton if (!tgid) 2284488e5bc4SEric W. Biederman return -ENOENT; 2285b55fcb22SAndrew Morton sprintf(tmp, "%d", tgid); 22861da177e4SLinus Torvalds return vfs_readlink(dentry,buffer,buflen,tmp); 22871da177e4SLinus Torvalds } 22881da177e4SLinus Torvalds 2289008b150aSAl Viro static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd) 22901da177e4SLinus Torvalds { 2291488e5bc4SEric W. Biederman struct pid_namespace *ns = dentry->d_sb->s_fs_info; 2292b55fcb22SAndrew Morton pid_t tgid = task_tgid_nr_ns(current, ns); 22938578cea7SEric W. Biederman char tmp[PROC_NUMBUF]; 2294b55fcb22SAndrew Morton if (!tgid) 2295488e5bc4SEric W. Biederman return ERR_PTR(-ENOENT); 2296b55fcb22SAndrew Morton sprintf(tmp, "%d", task_tgid_nr_ns(current, ns)); 2297008b150aSAl Viro return ERR_PTR(vfs_follow_link(nd,tmp)); 22981da177e4SLinus Torvalds } 22991da177e4SLinus Torvalds 2300c5ef1c42SArjan van de Ven static const struct inode_operations proc_self_inode_operations = { 23011da177e4SLinus Torvalds .readlink = proc_self_readlink, 23021da177e4SLinus Torvalds .follow_link = proc_self_follow_link, 23031da177e4SLinus Torvalds }; 23041da177e4SLinus Torvalds 230528a6d671SEric W. Biederman /* 2306801199ceSEric W. Biederman * proc base 2307801199ceSEric W. Biederman * 2308801199ceSEric W. Biederman * These are the directory entries in the root directory of /proc 2309801199ceSEric W. Biederman * that properly belong to the /proc filesystem, as they describe 2310801199ceSEric W. Biederman * describe something that is process related. 2311801199ceSEric W. Biederman */ 2312c5141e6dSEric Dumazet static const struct pid_entry proc_base_stuff[] = { 231361a28784SEric W. Biederman NOD("self", S_IFLNK|S_IRWXUGO, 2314801199ceSEric W. Biederman &proc_self_inode_operations, NULL, {}), 2315801199ceSEric W. Biederman }; 2316801199ceSEric W. Biederman 2317801199ceSEric W. Biederman /* 2318801199ceSEric W. Biederman * Exceptional case: normally we are not allowed to unhash a busy 2319801199ceSEric W. Biederman * directory. In this case, however, we can do it - no aliasing problems 2320801199ceSEric W. Biederman * due to the way we treat inodes. 2321801199ceSEric W. Biederman */ 2322801199ceSEric W. Biederman static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd) 2323801199ceSEric W. Biederman { 2324801199ceSEric W. Biederman struct inode *inode = dentry->d_inode; 2325801199ceSEric W. Biederman struct task_struct *task = get_proc_task(inode); 2326801199ceSEric W. Biederman if (task) { 2327801199ceSEric W. Biederman put_task_struct(task); 2328801199ceSEric W. Biederman return 1; 2329801199ceSEric W. Biederman } 2330801199ceSEric W. Biederman d_drop(dentry); 2331801199ceSEric W. Biederman return 0; 2332801199ceSEric W. Biederman } 2333801199ceSEric W. Biederman 2334d72f71ebSAl Viro static const struct dentry_operations proc_base_dentry_operations = 2335801199ceSEric W. Biederman { 2336801199ceSEric W. Biederman .d_revalidate = proc_base_revalidate, 2337801199ceSEric W. Biederman .d_delete = pid_delete_dentry, 2338801199ceSEric W. Biederman }; 2339801199ceSEric W. Biederman 2340444ceed8SEric W. Biederman static struct dentry *proc_base_instantiate(struct inode *dir, 2341c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 2342801199ceSEric W. Biederman { 2343c5141e6dSEric Dumazet const struct pid_entry *p = ptr; 2344801199ceSEric W. Biederman struct inode *inode; 2345801199ceSEric W. Biederman struct proc_inode *ei; 2346444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-EINVAL); 2347801199ceSEric W. Biederman 2348801199ceSEric W. Biederman /* Allocate the inode */ 2349801199ceSEric W. Biederman error = ERR_PTR(-ENOMEM); 2350801199ceSEric W. Biederman inode = new_inode(dir->i_sb); 2351801199ceSEric W. Biederman if (!inode) 2352801199ceSEric W. Biederman goto out; 2353801199ceSEric W. Biederman 2354801199ceSEric W. Biederman /* Initialize the inode */ 2355801199ceSEric W. Biederman ei = PROC_I(inode); 2356801199ceSEric W. Biederman inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; 2357801199ceSEric W. Biederman 2358801199ceSEric W. Biederman /* 2359801199ceSEric W. Biederman * grab the reference to the task. 2360801199ceSEric W. Biederman */ 23611a657f78SOleg Nesterov ei->pid = get_task_pid(task, PIDTYPE_PID); 2362801199ceSEric W. Biederman if (!ei->pid) 2363801199ceSEric W. Biederman goto out_iput; 2364801199ceSEric W. Biederman 2365801199ceSEric W. Biederman inode->i_mode = p->mode; 2366801199ceSEric W. Biederman if (S_ISDIR(inode->i_mode)) 2367801199ceSEric W. Biederman inode->i_nlink = 2; 2368801199ceSEric W. Biederman if (S_ISLNK(inode->i_mode)) 2369801199ceSEric W. Biederman inode->i_size = 64; 2370801199ceSEric W. Biederman if (p->iop) 2371801199ceSEric W. Biederman inode->i_op = p->iop; 2372801199ceSEric W. Biederman if (p->fop) 2373801199ceSEric W. Biederman inode->i_fop = p->fop; 2374801199ceSEric W. Biederman ei->op = p->op; 2375801199ceSEric W. Biederman dentry->d_op = &proc_base_dentry_operations; 2376801199ceSEric W. Biederman d_add(dentry, inode); 2377801199ceSEric W. Biederman error = NULL; 2378801199ceSEric W. Biederman out: 2379801199ceSEric W. Biederman return error; 2380801199ceSEric W. Biederman out_iput: 2381801199ceSEric W. Biederman iput(inode); 2382801199ceSEric W. Biederman goto out; 2383801199ceSEric W. Biederman } 2384801199ceSEric W. Biederman 2385444ceed8SEric W. Biederman static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry) 2386444ceed8SEric W. Biederman { 2387444ceed8SEric W. Biederman struct dentry *error; 2388444ceed8SEric W. Biederman struct task_struct *task = get_proc_task(dir); 2389c5141e6dSEric Dumazet const struct pid_entry *p, *last; 2390444ceed8SEric W. Biederman 2391444ceed8SEric W. Biederman error = ERR_PTR(-ENOENT); 2392444ceed8SEric W. Biederman 2393444ceed8SEric W. Biederman if (!task) 2394444ceed8SEric W. Biederman goto out_no_task; 2395444ceed8SEric W. Biederman 2396444ceed8SEric W. Biederman /* Lookup the directory entry */ 23977bcd6b0eSEric W. Biederman last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1]; 23987bcd6b0eSEric W. Biederman for (p = proc_base_stuff; p <= last; p++) { 2399444ceed8SEric W. Biederman if (p->len != dentry->d_name.len) 2400444ceed8SEric W. Biederman continue; 2401444ceed8SEric W. Biederman if (!memcmp(dentry->d_name.name, p->name, p->len)) 2402444ceed8SEric W. Biederman break; 2403444ceed8SEric W. Biederman } 24047bcd6b0eSEric W. Biederman if (p > last) 2405444ceed8SEric W. Biederman goto out; 2406444ceed8SEric W. Biederman 2407444ceed8SEric W. Biederman error = proc_base_instantiate(dir, dentry, task, p); 2408444ceed8SEric W. Biederman 2409444ceed8SEric W. Biederman out: 2410444ceed8SEric W. Biederman put_task_struct(task); 2411444ceed8SEric W. Biederman out_no_task: 2412444ceed8SEric W. Biederman return error; 2413444ceed8SEric W. Biederman } 2414444ceed8SEric W. Biederman 2415c5141e6dSEric Dumazet static int proc_base_fill_cache(struct file *filp, void *dirent, 2416c5141e6dSEric Dumazet filldir_t filldir, struct task_struct *task, const struct pid_entry *p) 241761a28784SEric W. Biederman { 241861a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, p->name, p->len, 241961a28784SEric W. Biederman proc_base_instantiate, task, p); 242061a28784SEric W. Biederman } 242161a28784SEric W. Biederman 2422aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING 2423297c5d92SAndrea Righi static int do_io_accounting(struct task_struct *task, char *buffer, int whole) 2424aba76fdbSAndrew Morton { 2425940389b8SAndrea Righi struct task_io_accounting acct = task->ioac; 2426297c5d92SAndrea Righi unsigned long flags; 2427297c5d92SAndrea Righi 24285995477aSAndrea Righi if (whole && lock_task_sighand(task, &flags)) { 2429b2d002dbSAndrea Righi struct task_struct *t = task; 2430297c5d92SAndrea Righi 24315995477aSAndrea Righi task_io_accounting_add(&acct, &task->signal->ioac); 24325995477aSAndrea Righi while_each_thread(task, t) 24335995477aSAndrea Righi task_io_accounting_add(&acct, &t->ioac); 2434297c5d92SAndrea Righi 2435297c5d92SAndrea Righi unlock_task_sighand(task, &flags); 2436297c5d92SAndrea Righi } 2437aba76fdbSAndrew Morton return sprintf(buffer, 2438aba76fdbSAndrew Morton "rchar: %llu\n" 2439aba76fdbSAndrew Morton "wchar: %llu\n" 2440aba76fdbSAndrew Morton "syscr: %llu\n" 2441aba76fdbSAndrew Morton "syscw: %llu\n" 2442aba76fdbSAndrew Morton "read_bytes: %llu\n" 2443aba76fdbSAndrew Morton "write_bytes: %llu\n" 2444aba76fdbSAndrew Morton "cancelled_write_bytes: %llu\n", 24457c44319dSAlexander Beregalov (unsigned long long)acct.rchar, 24467c44319dSAlexander Beregalov (unsigned long long)acct.wchar, 24477c44319dSAlexander Beregalov (unsigned long long)acct.syscr, 24487c44319dSAlexander Beregalov (unsigned long long)acct.syscw, 24497c44319dSAlexander Beregalov (unsigned long long)acct.read_bytes, 24507c44319dSAlexander Beregalov (unsigned long long)acct.write_bytes, 24517c44319dSAlexander Beregalov (unsigned long long)acct.cancelled_write_bytes); 2452aba76fdbSAndrew Morton } 2453297c5d92SAndrea Righi 2454297c5d92SAndrea Righi static int proc_tid_io_accounting(struct task_struct *task, char *buffer) 2455297c5d92SAndrea Righi { 2456297c5d92SAndrea Righi return do_io_accounting(task, buffer, 0); 2457297c5d92SAndrea Righi } 2458297c5d92SAndrea Righi 2459297c5d92SAndrea Righi static int proc_tgid_io_accounting(struct task_struct *task, char *buffer) 2460297c5d92SAndrea Righi { 2461297c5d92SAndrea Righi return do_io_accounting(task, buffer, 1); 2462297c5d92SAndrea Righi } 2463297c5d92SAndrea Righi #endif /* CONFIG_TASK_IO_ACCOUNTING */ 2464aba76fdbSAndrew Morton 246547830723SKees Cook static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns, 246647830723SKees Cook struct pid *pid, struct task_struct *task) 246747830723SKees Cook { 246847830723SKees Cook seq_printf(m, "%08x\n", task->personality); 246947830723SKees Cook return 0; 247047830723SKees Cook } 247147830723SKees Cook 2472801199ceSEric W. Biederman /* 247328a6d671SEric W. Biederman * Thread groups 247428a6d671SEric W. Biederman */ 247500977a59SArjan van de Ven static const struct file_operations proc_task_operations; 2476c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations; 247720cdc894SEric W. Biederman 2478c5141e6dSEric Dumazet static const struct pid_entry tgid_base_stuff[] = { 2479631f9c18SAlexey Dobriyan DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations), 2480631f9c18SAlexey Dobriyan DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations), 2481631f9c18SAlexey Dobriyan DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations), 2482b2211a36SAndrew Morton #ifdef CONFIG_NET 2483631f9c18SAlexey Dobriyan DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations), 2484b2211a36SAndrew Morton #endif 2485631f9c18SAlexey Dobriyan REG("environ", S_IRUSR, proc_environ_operations), 2486631f9c18SAlexey Dobriyan INF("auxv", S_IRUSR, proc_pid_auxv), 2487631f9c18SAlexey Dobriyan ONE("status", S_IRUGO, proc_pid_status), 2488631f9c18SAlexey Dobriyan ONE("personality", S_IRUSR, proc_pid_personality), 2489631f9c18SAlexey Dobriyan INF("limits", S_IRUSR, proc_pid_limits), 249043ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG 2491631f9c18SAlexey Dobriyan REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations), 249243ae34cbSIngo Molnar #endif 2493ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK 2494631f9c18SAlexey Dobriyan INF("syscall", S_IRUSR, proc_pid_syscall), 2495ebcb6734SRoland McGrath #endif 2496631f9c18SAlexey Dobriyan INF("cmdline", S_IRUGO, proc_pid_cmdline), 2497631f9c18SAlexey Dobriyan ONE("stat", S_IRUGO, proc_tgid_stat), 2498631f9c18SAlexey Dobriyan ONE("statm", S_IRUGO, proc_pid_statm), 2499631f9c18SAlexey Dobriyan REG("maps", S_IRUGO, proc_maps_operations), 250028a6d671SEric W. Biederman #ifdef CONFIG_NUMA 2501631f9c18SAlexey Dobriyan REG("numa_maps", S_IRUGO, proc_numa_maps_operations), 250228a6d671SEric W. Biederman #endif 2503631f9c18SAlexey Dobriyan REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations), 2504631f9c18SAlexey Dobriyan LNK("cwd", proc_cwd_link), 2505631f9c18SAlexey Dobriyan LNK("root", proc_root_link), 2506631f9c18SAlexey Dobriyan LNK("exe", proc_exe_link), 2507631f9c18SAlexey Dobriyan REG("mounts", S_IRUGO, proc_mounts_operations), 2508631f9c18SAlexey Dobriyan REG("mountinfo", S_IRUGO, proc_mountinfo_operations), 2509631f9c18SAlexey Dobriyan REG("mountstats", S_IRUSR, proc_mountstats_operations), 25101e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR 2511631f9c18SAlexey Dobriyan REG("clear_refs", S_IWUSR, proc_clear_refs_operations), 2512631f9c18SAlexey Dobriyan REG("smaps", S_IRUGO, proc_smaps_operations), 2513631f9c18SAlexey Dobriyan REG("pagemap", S_IRUSR, proc_pagemap_operations), 251428a6d671SEric W. Biederman #endif 251528a6d671SEric W. Biederman #ifdef CONFIG_SECURITY 2516631f9c18SAlexey Dobriyan DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations), 251728a6d671SEric W. Biederman #endif 251828a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS 2519631f9c18SAlexey Dobriyan INF("wchan", S_IRUGO, proc_pid_wchan), 252028a6d671SEric W. Biederman #endif 25212ec220e2SKen Chen #ifdef CONFIG_STACKTRACE 25222ec220e2SKen Chen ONE("stack", S_IRUSR, proc_pid_stack), 252328a6d671SEric W. Biederman #endif 252428a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS 2525631f9c18SAlexey Dobriyan INF("schedstat", S_IRUGO, proc_pid_schedstat), 252628a6d671SEric W. Biederman #endif 25279745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP 2528631f9c18SAlexey Dobriyan REG("latency", S_IRUGO, proc_lstats_operations), 25299745512cSArjan van de Ven #endif 25308793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET 2531631f9c18SAlexey Dobriyan REG("cpuset", S_IRUGO, proc_cpuset_operations), 253228a6d671SEric W. Biederman #endif 2533a424316cSPaul Menage #ifdef CONFIG_CGROUPS 2534631f9c18SAlexey Dobriyan REG("cgroup", S_IRUGO, proc_cgroup_operations), 2535a424316cSPaul Menage #endif 2536631f9c18SAlexey Dobriyan INF("oom_score", S_IRUGO, proc_oom_score), 2537631f9c18SAlexey Dobriyan REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations), 253828a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL 2539631f9c18SAlexey Dobriyan REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations), 2540631f9c18SAlexey Dobriyan REG("sessionid", S_IRUGO, proc_sessionid_operations), 254128a6d671SEric W. Biederman #endif 2542f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION 2543631f9c18SAlexey Dobriyan REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations), 2544f4f154fdSAkinobu Mita #endif 25453cb4a0bbSKawai, Hidehiro #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE) 2546631f9c18SAlexey Dobriyan REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations), 25473cb4a0bbSKawai, Hidehiro #endif 2548aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING 2549631f9c18SAlexey Dobriyan INF("io", S_IRUGO, proc_tgid_io_accounting), 2550aba76fdbSAndrew Morton #endif 255128a6d671SEric W. Biederman }; 255228a6d671SEric W. Biederman 255328a6d671SEric W. Biederman static int proc_tgid_base_readdir(struct file * filp, 255428a6d671SEric W. Biederman void * dirent, filldir_t filldir) 255528a6d671SEric W. Biederman { 255628a6d671SEric W. Biederman return proc_pident_readdir(filp,dirent,filldir, 255728a6d671SEric W. Biederman tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff)); 255828a6d671SEric W. Biederman } 255928a6d671SEric W. Biederman 256000977a59SArjan van de Ven static const struct file_operations proc_tgid_base_operations = { 256128a6d671SEric W. Biederman .read = generic_read_dir, 256228a6d671SEric W. Biederman .readdir = proc_tgid_base_readdir, 256328a6d671SEric W. Biederman }; 256428a6d671SEric W. Biederman 256528a6d671SEric W. Biederman static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){ 25667bcd6b0eSEric W. Biederman return proc_pident_lookup(dir, dentry, 25677bcd6b0eSEric W. Biederman tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff)); 256828a6d671SEric W. Biederman } 256928a6d671SEric W. Biederman 2570c5ef1c42SArjan van de Ven static const struct inode_operations proc_tgid_base_inode_operations = { 257128a6d671SEric W. Biederman .lookup = proc_tgid_base_lookup, 257228a6d671SEric W. Biederman .getattr = pid_getattr, 257328a6d671SEric W. Biederman .setattr = proc_setattr, 257428a6d671SEric W. Biederman }; 257528a6d671SEric W. Biederman 257660347f67SPavel Emelyanov static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid) 25771da177e4SLinus Torvalds { 257848e6484dSEric W. Biederman struct dentry *dentry, *leader, *dir; 25798578cea7SEric W. Biederman char buf[PROC_NUMBUF]; 258048e6484dSEric W. Biederman struct qstr name; 25811da177e4SLinus Torvalds 258248e6484dSEric W. Biederman name.name = buf; 258360347f67SPavel Emelyanov name.len = snprintf(buf, sizeof(buf), "%d", pid); 258460347f67SPavel Emelyanov dentry = d_hash_and_lookup(mnt->mnt_root, &name); 258548e6484dSEric W. Biederman if (dentry) { 25867766755aSAndrea Arcangeli if (!(current->flags & PF_EXITING)) 258748e6484dSEric W. Biederman shrink_dcache_parent(dentry); 258848e6484dSEric W. Biederman d_drop(dentry); 258948e6484dSEric W. Biederman dput(dentry); 25901da177e4SLinus Torvalds } 25911da177e4SLinus Torvalds 259260347f67SPavel Emelyanov if (tgid == 0) 259348e6484dSEric W. Biederman goto out; 25941da177e4SLinus Torvalds 259548e6484dSEric W. Biederman name.name = buf; 259660347f67SPavel Emelyanov name.len = snprintf(buf, sizeof(buf), "%d", tgid); 259760347f67SPavel Emelyanov leader = d_hash_and_lookup(mnt->mnt_root, &name); 259848e6484dSEric W. Biederman if (!leader) 259948e6484dSEric W. Biederman goto out; 260048e6484dSEric W. Biederman 260148e6484dSEric W. Biederman name.name = "task"; 260248e6484dSEric W. Biederman name.len = strlen(name.name); 260348e6484dSEric W. Biederman dir = d_hash_and_lookup(leader, &name); 260448e6484dSEric W. Biederman if (!dir) 260548e6484dSEric W. Biederman goto out_put_leader; 260648e6484dSEric W. Biederman 260748e6484dSEric W. Biederman name.name = buf; 260860347f67SPavel Emelyanov name.len = snprintf(buf, sizeof(buf), "%d", pid); 260948e6484dSEric W. Biederman dentry = d_hash_and_lookup(dir, &name); 261048e6484dSEric W. Biederman if (dentry) { 261148e6484dSEric W. Biederman shrink_dcache_parent(dentry); 261248e6484dSEric W. Biederman d_drop(dentry); 261348e6484dSEric W. Biederman dput(dentry); 26141da177e4SLinus Torvalds } 261548e6484dSEric W. Biederman 261648e6484dSEric W. Biederman dput(dir); 261748e6484dSEric W. Biederman out_put_leader: 261848e6484dSEric W. Biederman dput(leader); 261948e6484dSEric W. Biederman out: 262048e6484dSEric W. Biederman return; 26211da177e4SLinus Torvalds } 26221da177e4SLinus Torvalds 26230895e91dSRandy Dunlap /** 26240895e91dSRandy Dunlap * proc_flush_task - Remove dcache entries for @task from the /proc dcache. 26250895e91dSRandy Dunlap * @task: task that should be flushed. 26260895e91dSRandy Dunlap * 26270895e91dSRandy Dunlap * When flushing dentries from proc, one needs to flush them from global 262860347f67SPavel Emelyanov * proc (proc_mnt) and from all the namespaces' procs this task was seen 26290895e91dSRandy Dunlap * in. This call is supposed to do all of this job. 26300895e91dSRandy Dunlap * 26310895e91dSRandy Dunlap * Looks in the dcache for 26320895e91dSRandy Dunlap * /proc/@pid 26330895e91dSRandy Dunlap * /proc/@tgid/task/@pid 26340895e91dSRandy Dunlap * if either directory is present flushes it and all of it'ts children 26350895e91dSRandy Dunlap * from the dcache. 26360895e91dSRandy Dunlap * 26370895e91dSRandy Dunlap * It is safe and reasonable to cache /proc entries for a task until 26380895e91dSRandy Dunlap * that task exits. After that they just clog up the dcache with 26390895e91dSRandy Dunlap * useless entries, possibly causing useful dcache entries to be 26400895e91dSRandy Dunlap * flushed instead. This routine is proved to flush those useless 26410895e91dSRandy Dunlap * dcache entries at process exit time. 26420895e91dSRandy Dunlap * 26430895e91dSRandy Dunlap * NOTE: This routine is just an optimization so it does not guarantee 26440895e91dSRandy Dunlap * that no dcache entries will exist at process exit time it 26450895e91dSRandy Dunlap * just makes it very unlikely that any will persist. 264660347f67SPavel Emelyanov */ 264760347f67SPavel Emelyanov 264860347f67SPavel Emelyanov void proc_flush_task(struct task_struct *task) 264960347f67SPavel Emelyanov { 26509fcc2d15SEric W. Biederman int i; 26519fcc2d15SEric W. Biederman struct pid *pid, *tgid = NULL; 2652130f77ecSPavel Emelyanov struct upid *upid; 2653130f77ecSPavel Emelyanov 2654130f77ecSPavel Emelyanov pid = task_pid(task); 26559fcc2d15SEric W. Biederman if (thread_group_leader(task)) 2656130f77ecSPavel Emelyanov tgid = task_tgid(task); 26579fcc2d15SEric W. Biederman 26589fcc2d15SEric W. Biederman for (i = 0; i <= pid->level; i++) { 2659130f77ecSPavel Emelyanov upid = &pid->numbers[i]; 2660130f77ecSPavel Emelyanov proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr, 26619fcc2d15SEric W. Biederman tgid ? tgid->numbers[i].nr : 0); 2662130f77ecSPavel Emelyanov } 26636f4e6433SPavel Emelyanov 26646f4e6433SPavel Emelyanov upid = &pid->numbers[pid->level]; 26656f4e6433SPavel Emelyanov if (upid->nr == 1) 26666f4e6433SPavel Emelyanov pid_ns_release_proc(upid->ns); 266760347f67SPavel Emelyanov } 266860347f67SPavel Emelyanov 26699711ef99SAdrian Bunk static struct dentry *proc_pid_instantiate(struct inode *dir, 26709711ef99SAdrian Bunk struct dentry * dentry, 2671c5141e6dSEric Dumazet struct task_struct *task, const void *ptr) 2672444ceed8SEric W. Biederman { 2673444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-ENOENT); 2674444ceed8SEric W. Biederman struct inode *inode; 2675444ceed8SEric W. Biederman 267661a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 2677444ceed8SEric W. Biederman if (!inode) 2678444ceed8SEric W. Biederman goto out; 2679444ceed8SEric W. Biederman 2680444ceed8SEric W. Biederman inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO; 2681444ceed8SEric W. Biederman inode->i_op = &proc_tgid_base_inode_operations; 2682444ceed8SEric W. Biederman inode->i_fop = &proc_tgid_base_operations; 2683444ceed8SEric W. Biederman inode->i_flags|=S_IMMUTABLE; 2684aed54175SVegard Nossum 2685aed54175SVegard Nossum inode->i_nlink = 2 + pid_entry_count_dirs(tgid_base_stuff, 2686aed54175SVegard Nossum ARRAY_SIZE(tgid_base_stuff)); 2687444ceed8SEric W. Biederman 2688444ceed8SEric W. Biederman dentry->d_op = &pid_dentry_operations; 2689444ceed8SEric W. Biederman 2690444ceed8SEric W. Biederman d_add(dentry, inode); 2691444ceed8SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 2692444ceed8SEric W. Biederman if (pid_revalidate(dentry, NULL)) 2693444ceed8SEric W. Biederman error = NULL; 2694444ceed8SEric W. Biederman out: 2695444ceed8SEric W. Biederman return error; 2696444ceed8SEric W. Biederman } 2697444ceed8SEric W. Biederman 26981da177e4SLinus Torvalds struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd) 26991da177e4SLinus Torvalds { 2700cd6a3ce9SEric W. Biederman struct dentry *result = ERR_PTR(-ENOENT); 27011da177e4SLinus Torvalds struct task_struct *task; 27021da177e4SLinus Torvalds unsigned tgid; 2703b488893aSPavel Emelyanov struct pid_namespace *ns; 27041da177e4SLinus Torvalds 2705801199ceSEric W. Biederman result = proc_base_lookup(dir, dentry); 2706801199ceSEric W. Biederman if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT) 2707801199ceSEric W. Biederman goto out; 2708801199ceSEric W. Biederman 27091da177e4SLinus Torvalds tgid = name_to_int(dentry); 27101da177e4SLinus Torvalds if (tgid == ~0U) 27111da177e4SLinus Torvalds goto out; 27121da177e4SLinus Torvalds 2713b488893aSPavel Emelyanov ns = dentry->d_sb->s_fs_info; 2714de758734SEric W. Biederman rcu_read_lock(); 2715b488893aSPavel Emelyanov task = find_task_by_pid_ns(tgid, ns); 27161da177e4SLinus Torvalds if (task) 27171da177e4SLinus Torvalds get_task_struct(task); 2718de758734SEric W. Biederman rcu_read_unlock(); 27191da177e4SLinus Torvalds if (!task) 27201da177e4SLinus Torvalds goto out; 27211da177e4SLinus Torvalds 2722444ceed8SEric W. Biederman result = proc_pid_instantiate(dir, dentry, task, NULL); 272348e6484dSEric W. Biederman put_task_struct(task); 27241da177e4SLinus Torvalds out: 2725cd6a3ce9SEric W. Biederman return result; 27261da177e4SLinus Torvalds } 27271da177e4SLinus Torvalds 27281da177e4SLinus Torvalds /* 27290804ef4bSEric W. Biederman * Find the first task with tgid >= tgid 27300bc58a91SEric W. Biederman * 27311da177e4SLinus Torvalds */ 273219fd4bb2SEric W. Biederman struct tgid_iter { 273319fd4bb2SEric W. Biederman unsigned int tgid; 27340804ef4bSEric W. Biederman struct task_struct *task; 273519fd4bb2SEric W. Biederman }; 273619fd4bb2SEric W. Biederman static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter) 273719fd4bb2SEric W. Biederman { 27380804ef4bSEric W. Biederman struct pid *pid; 27391da177e4SLinus Torvalds 274019fd4bb2SEric W. Biederman if (iter.task) 274119fd4bb2SEric W. Biederman put_task_struct(iter.task); 27420804ef4bSEric W. Biederman rcu_read_lock(); 27430804ef4bSEric W. Biederman retry: 274419fd4bb2SEric W. Biederman iter.task = NULL; 274519fd4bb2SEric W. Biederman pid = find_ge_pid(iter.tgid, ns); 27460804ef4bSEric W. Biederman if (pid) { 274719fd4bb2SEric W. Biederman iter.tgid = pid_nr_ns(pid, ns); 274819fd4bb2SEric W. Biederman iter.task = pid_task(pid, PIDTYPE_PID); 27490804ef4bSEric W. Biederman /* What we to know is if the pid we have find is the 27500804ef4bSEric W. Biederman * pid of a thread_group_leader. Testing for task 27510804ef4bSEric W. Biederman * being a thread_group_leader is the obvious thing 27520804ef4bSEric W. Biederman * todo but there is a window when it fails, due to 27530804ef4bSEric W. Biederman * the pid transfer logic in de_thread. 27540804ef4bSEric W. Biederman * 27550804ef4bSEric W. Biederman * So we perform the straight forward test of seeing 27560804ef4bSEric W. Biederman * if the pid we have found is the pid of a thread 27570804ef4bSEric W. Biederman * group leader, and don't worry if the task we have 27580804ef4bSEric W. Biederman * found doesn't happen to be a thread group leader. 27590804ef4bSEric W. Biederman * As we don't care in the case of readdir. 27600bc58a91SEric W. Biederman */ 276119fd4bb2SEric W. Biederman if (!iter.task || !has_group_leader_pid(iter.task)) { 276219fd4bb2SEric W. Biederman iter.tgid += 1; 27630804ef4bSEric W. Biederman goto retry; 276419fd4bb2SEric W. Biederman } 276519fd4bb2SEric W. Biederman get_task_struct(iter.task); 27661da177e4SLinus Torvalds } 2767454cc105SEric W. Biederman rcu_read_unlock(); 276819fd4bb2SEric W. Biederman return iter; 27691da177e4SLinus Torvalds } 27701da177e4SLinus Torvalds 27717bcd6b0eSEric W. Biederman #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff)) 27721da177e4SLinus Torvalds 277361a28784SEric W. Biederman static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir, 277419fd4bb2SEric W. Biederman struct tgid_iter iter) 277561a28784SEric W. Biederman { 277661a28784SEric W. Biederman char name[PROC_NUMBUF]; 277719fd4bb2SEric W. Biederman int len = snprintf(name, sizeof(name), "%d", iter.tgid); 277861a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, name, len, 277919fd4bb2SEric W. Biederman proc_pid_instantiate, iter.task, NULL); 278061a28784SEric W. Biederman } 278161a28784SEric W. Biederman 27821da177e4SLinus Torvalds /* for the /proc/ directory itself, after non-process stuff has been done */ 27831da177e4SLinus Torvalds int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir) 27841da177e4SLinus Torvalds { 27851da177e4SLinus Torvalds unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY; 27862fddfeefSJosef "Jeff" Sipek struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode); 278719fd4bb2SEric W. Biederman struct tgid_iter iter; 2788b488893aSPavel Emelyanov struct pid_namespace *ns; 27891da177e4SLinus Torvalds 279061a28784SEric W. Biederman if (!reaper) 279161a28784SEric W. Biederman goto out_no_task; 279261a28784SEric W. Biederman 27937bcd6b0eSEric W. Biederman for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) { 2794c5141e6dSEric Dumazet const struct pid_entry *p = &proc_base_stuff[nr]; 279561a28784SEric W. Biederman if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0) 2796801199ceSEric W. Biederman goto out; 27971da177e4SLinus Torvalds } 27981da177e4SLinus Torvalds 2799b488893aSPavel Emelyanov ns = filp->f_dentry->d_sb->s_fs_info; 280019fd4bb2SEric W. Biederman iter.task = NULL; 280119fd4bb2SEric W. Biederman iter.tgid = filp->f_pos - TGID_OFFSET; 280219fd4bb2SEric W. Biederman for (iter = next_tgid(ns, iter); 280319fd4bb2SEric W. Biederman iter.task; 280419fd4bb2SEric W. Biederman iter.tgid += 1, iter = next_tgid(ns, iter)) { 280519fd4bb2SEric W. Biederman filp->f_pos = iter.tgid + TGID_OFFSET; 280619fd4bb2SEric W. Biederman if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) { 280719fd4bb2SEric W. Biederman put_task_struct(iter.task); 28080804ef4bSEric W. Biederman goto out; 28091da177e4SLinus Torvalds } 28101da177e4SLinus Torvalds } 28110804ef4bSEric W. Biederman filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET; 28120804ef4bSEric W. Biederman out: 281361a28784SEric W. Biederman put_task_struct(reaper); 281461a28784SEric W. Biederman out_no_task: 28151da177e4SLinus Torvalds return 0; 28161da177e4SLinus Torvalds } 28171da177e4SLinus Torvalds 28180bc58a91SEric W. Biederman /* 281928a6d671SEric W. Biederman * Tasks 282028a6d671SEric W. Biederman */ 2821c5141e6dSEric Dumazet static const struct pid_entry tid_base_stuff[] = { 2822631f9c18SAlexey Dobriyan DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations), 2823631f9c18SAlexey Dobriyan DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fd_operations), 2824631f9c18SAlexey Dobriyan REG("environ", S_IRUSR, proc_environ_operations), 2825631f9c18SAlexey Dobriyan INF("auxv", S_IRUSR, proc_pid_auxv), 2826631f9c18SAlexey Dobriyan ONE("status", S_IRUGO, proc_pid_status), 2827631f9c18SAlexey Dobriyan ONE("personality", S_IRUSR, proc_pid_personality), 2828631f9c18SAlexey Dobriyan INF("limits", S_IRUSR, proc_pid_limits), 282943ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG 2830631f9c18SAlexey Dobriyan REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations), 283143ae34cbSIngo Molnar #endif 2832ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK 2833631f9c18SAlexey Dobriyan INF("syscall", S_IRUSR, proc_pid_syscall), 2834ebcb6734SRoland McGrath #endif 2835631f9c18SAlexey Dobriyan INF("cmdline", S_IRUGO, proc_pid_cmdline), 2836631f9c18SAlexey Dobriyan ONE("stat", S_IRUGO, proc_tid_stat), 2837631f9c18SAlexey Dobriyan ONE("statm", S_IRUGO, proc_pid_statm), 2838631f9c18SAlexey Dobriyan REG("maps", S_IRUGO, proc_maps_operations), 283928a6d671SEric W. Biederman #ifdef CONFIG_NUMA 2840631f9c18SAlexey Dobriyan REG("numa_maps", S_IRUGO, proc_numa_maps_operations), 284128a6d671SEric W. Biederman #endif 2842631f9c18SAlexey Dobriyan REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations), 2843631f9c18SAlexey Dobriyan LNK("cwd", proc_cwd_link), 2844631f9c18SAlexey Dobriyan LNK("root", proc_root_link), 2845631f9c18SAlexey Dobriyan LNK("exe", proc_exe_link), 2846631f9c18SAlexey Dobriyan REG("mounts", S_IRUGO, proc_mounts_operations), 2847631f9c18SAlexey Dobriyan REG("mountinfo", S_IRUGO, proc_mountinfo_operations), 28481e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR 2849631f9c18SAlexey Dobriyan REG("clear_refs", S_IWUSR, proc_clear_refs_operations), 2850631f9c18SAlexey Dobriyan REG("smaps", S_IRUGO, proc_smaps_operations), 2851631f9c18SAlexey Dobriyan REG("pagemap", S_IRUSR, proc_pagemap_operations), 285228a6d671SEric W. Biederman #endif 285328a6d671SEric W. Biederman #ifdef CONFIG_SECURITY 2854631f9c18SAlexey Dobriyan DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations), 285528a6d671SEric W. Biederman #endif 285628a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS 2857631f9c18SAlexey Dobriyan INF("wchan", S_IRUGO, proc_pid_wchan), 285828a6d671SEric W. Biederman #endif 28592ec220e2SKen Chen #ifdef CONFIG_STACKTRACE 28602ec220e2SKen Chen ONE("stack", S_IRUSR, proc_pid_stack), 286128a6d671SEric W. Biederman #endif 286228a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS 2863631f9c18SAlexey Dobriyan INF("schedstat", S_IRUGO, proc_pid_schedstat), 286428a6d671SEric W. Biederman #endif 28659745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP 2866631f9c18SAlexey Dobriyan REG("latency", S_IRUGO, proc_lstats_operations), 28679745512cSArjan van de Ven #endif 28688793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET 2869631f9c18SAlexey Dobriyan REG("cpuset", S_IRUGO, proc_cpuset_operations), 287028a6d671SEric W. Biederman #endif 2871a424316cSPaul Menage #ifdef CONFIG_CGROUPS 2872631f9c18SAlexey Dobriyan REG("cgroup", S_IRUGO, proc_cgroup_operations), 2873a424316cSPaul Menage #endif 2874631f9c18SAlexey Dobriyan INF("oom_score", S_IRUGO, proc_oom_score), 2875631f9c18SAlexey Dobriyan REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations), 287628a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL 2877631f9c18SAlexey Dobriyan REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations), 2878631f9c18SAlexey Dobriyan REG("sessionid", S_IRUSR, proc_sessionid_operations), 287928a6d671SEric W. Biederman #endif 2880f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION 2881631f9c18SAlexey Dobriyan REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations), 2882f4f154fdSAkinobu Mita #endif 2883297c5d92SAndrea Righi #ifdef CONFIG_TASK_IO_ACCOUNTING 2884631f9c18SAlexey Dobriyan INF("io", S_IRUGO, proc_tid_io_accounting), 2885297c5d92SAndrea Righi #endif 288628a6d671SEric W. Biederman }; 288728a6d671SEric W. Biederman 288828a6d671SEric W. Biederman static int proc_tid_base_readdir(struct file * filp, 288928a6d671SEric W. Biederman void * dirent, filldir_t filldir) 289028a6d671SEric W. Biederman { 289128a6d671SEric W. Biederman return proc_pident_readdir(filp,dirent,filldir, 289228a6d671SEric W. Biederman tid_base_stuff,ARRAY_SIZE(tid_base_stuff)); 289328a6d671SEric W. Biederman } 289428a6d671SEric W. Biederman 289528a6d671SEric W. Biederman static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){ 28967bcd6b0eSEric W. Biederman return proc_pident_lookup(dir, dentry, 28977bcd6b0eSEric W. Biederman tid_base_stuff, ARRAY_SIZE(tid_base_stuff)); 289828a6d671SEric W. Biederman } 289928a6d671SEric W. Biederman 290000977a59SArjan van de Ven static const struct file_operations proc_tid_base_operations = { 290128a6d671SEric W. Biederman .read = generic_read_dir, 290228a6d671SEric W. Biederman .readdir = proc_tid_base_readdir, 290328a6d671SEric W. Biederman }; 290428a6d671SEric W. Biederman 2905c5ef1c42SArjan van de Ven static const struct inode_operations proc_tid_base_inode_operations = { 290628a6d671SEric W. Biederman .lookup = proc_tid_base_lookup, 290728a6d671SEric W. Biederman .getattr = pid_getattr, 290828a6d671SEric W. Biederman .setattr = proc_setattr, 290928a6d671SEric W. Biederman }; 291028a6d671SEric W. Biederman 2911444ceed8SEric W. Biederman static struct dentry *proc_task_instantiate(struct inode *dir, 2912c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 2913444ceed8SEric W. Biederman { 2914444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-ENOENT); 2915444ceed8SEric W. Biederman struct inode *inode; 291661a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 2917444ceed8SEric W. Biederman 2918444ceed8SEric W. Biederman if (!inode) 2919444ceed8SEric W. Biederman goto out; 2920444ceed8SEric W. Biederman inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO; 2921444ceed8SEric W. Biederman inode->i_op = &proc_tid_base_inode_operations; 2922444ceed8SEric W. Biederman inode->i_fop = &proc_tid_base_operations; 2923444ceed8SEric W. Biederman inode->i_flags|=S_IMMUTABLE; 2924aed54175SVegard Nossum 2925aed54175SVegard Nossum inode->i_nlink = 2 + pid_entry_count_dirs(tid_base_stuff, 2926aed54175SVegard Nossum ARRAY_SIZE(tid_base_stuff)); 2927444ceed8SEric W. Biederman 2928444ceed8SEric W. Biederman dentry->d_op = &pid_dentry_operations; 2929444ceed8SEric W. Biederman 2930444ceed8SEric W. Biederman d_add(dentry, inode); 2931444ceed8SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 2932444ceed8SEric W. Biederman if (pid_revalidate(dentry, NULL)) 2933444ceed8SEric W. Biederman error = NULL; 2934444ceed8SEric W. Biederman out: 2935444ceed8SEric W. Biederman return error; 2936444ceed8SEric W. Biederman } 2937444ceed8SEric W. Biederman 293828a6d671SEric W. Biederman static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd) 293928a6d671SEric W. Biederman { 294028a6d671SEric W. Biederman struct dentry *result = ERR_PTR(-ENOENT); 294128a6d671SEric W. Biederman struct task_struct *task; 294228a6d671SEric W. Biederman struct task_struct *leader = get_proc_task(dir); 294328a6d671SEric W. Biederman unsigned tid; 2944b488893aSPavel Emelyanov struct pid_namespace *ns; 294528a6d671SEric W. Biederman 294628a6d671SEric W. Biederman if (!leader) 294728a6d671SEric W. Biederman goto out_no_task; 294828a6d671SEric W. Biederman 294928a6d671SEric W. Biederman tid = name_to_int(dentry); 295028a6d671SEric W. Biederman if (tid == ~0U) 295128a6d671SEric W. Biederman goto out; 295228a6d671SEric W. Biederman 2953b488893aSPavel Emelyanov ns = dentry->d_sb->s_fs_info; 295428a6d671SEric W. Biederman rcu_read_lock(); 2955b488893aSPavel Emelyanov task = find_task_by_pid_ns(tid, ns); 295628a6d671SEric W. Biederman if (task) 295728a6d671SEric W. Biederman get_task_struct(task); 295828a6d671SEric W. Biederman rcu_read_unlock(); 295928a6d671SEric W. Biederman if (!task) 296028a6d671SEric W. Biederman goto out; 2961bac0abd6SPavel Emelyanov if (!same_thread_group(leader, task)) 296228a6d671SEric W. Biederman goto out_drop_task; 296328a6d671SEric W. Biederman 2964444ceed8SEric W. Biederman result = proc_task_instantiate(dir, dentry, task, NULL); 296528a6d671SEric W. Biederman out_drop_task: 296628a6d671SEric W. Biederman put_task_struct(task); 296728a6d671SEric W. Biederman out: 296828a6d671SEric W. Biederman put_task_struct(leader); 296928a6d671SEric W. Biederman out_no_task: 297028a6d671SEric W. Biederman return result; 297128a6d671SEric W. Biederman } 297228a6d671SEric W. Biederman 297328a6d671SEric W. Biederman /* 29740bc58a91SEric W. Biederman * Find the first tid of a thread group to return to user space. 29750bc58a91SEric W. Biederman * 29760bc58a91SEric W. Biederman * Usually this is just the thread group leader, but if the users 29770bc58a91SEric W. Biederman * buffer was too small or there was a seek into the middle of the 29780bc58a91SEric W. Biederman * directory we have more work todo. 29790bc58a91SEric W. Biederman * 29800bc58a91SEric W. Biederman * In the case of a short read we start with find_task_by_pid. 29810bc58a91SEric W. Biederman * 29820bc58a91SEric W. Biederman * In the case of a seek we start with the leader and walk nr 29830bc58a91SEric W. Biederman * threads past it. 29840bc58a91SEric W. Biederman */ 2985cc288738SEric W. Biederman static struct task_struct *first_tid(struct task_struct *leader, 2986b488893aSPavel Emelyanov int tid, int nr, struct pid_namespace *ns) 29870bc58a91SEric W. Biederman { 2988a872ff0cSOleg Nesterov struct task_struct *pos; 29890bc58a91SEric W. Biederman 2990cc288738SEric W. Biederman rcu_read_lock(); 29910bc58a91SEric W. Biederman /* Attempt to start with the pid of a thread */ 29920bc58a91SEric W. Biederman if (tid && (nr > 0)) { 2993b488893aSPavel Emelyanov pos = find_task_by_pid_ns(tid, ns); 2994a872ff0cSOleg Nesterov if (pos && (pos->group_leader == leader)) 2995a872ff0cSOleg Nesterov goto found; 29960bc58a91SEric W. Biederman } 29970bc58a91SEric W. Biederman 29980bc58a91SEric W. Biederman /* If nr exceeds the number of threads there is nothing todo */ 29990bc58a91SEric W. Biederman pos = NULL; 3000a872ff0cSOleg Nesterov if (nr && nr >= get_nr_threads(leader)) 3001a872ff0cSOleg Nesterov goto out; 3002a872ff0cSOleg Nesterov 3003a872ff0cSOleg Nesterov /* If we haven't found our starting place yet start 3004a872ff0cSOleg Nesterov * with the leader and walk nr threads forward. 3005a872ff0cSOleg Nesterov */ 3006a872ff0cSOleg Nesterov for (pos = leader; nr > 0; --nr) { 3007a872ff0cSOleg Nesterov pos = next_thread(pos); 3008a872ff0cSOleg Nesterov if (pos == leader) { 3009a872ff0cSOleg Nesterov pos = NULL; 3010a872ff0cSOleg Nesterov goto out; 3011a872ff0cSOleg Nesterov } 3012a872ff0cSOleg Nesterov } 3013a872ff0cSOleg Nesterov found: 3014a872ff0cSOleg Nesterov get_task_struct(pos); 3015a872ff0cSOleg Nesterov out: 3016cc288738SEric W. Biederman rcu_read_unlock(); 30170bc58a91SEric W. Biederman return pos; 30180bc58a91SEric W. Biederman } 30190bc58a91SEric W. Biederman 30200bc58a91SEric W. Biederman /* 30210bc58a91SEric W. Biederman * Find the next thread in the thread list. 30220bc58a91SEric W. Biederman * Return NULL if there is an error or no next thread. 30230bc58a91SEric W. Biederman * 30240bc58a91SEric W. Biederman * The reference to the input task_struct is released. 30250bc58a91SEric W. Biederman */ 30260bc58a91SEric W. Biederman static struct task_struct *next_tid(struct task_struct *start) 30270bc58a91SEric W. Biederman { 3028c1df7fb8SOleg Nesterov struct task_struct *pos = NULL; 3029cc288738SEric W. Biederman rcu_read_lock(); 3030c1df7fb8SOleg Nesterov if (pid_alive(start)) { 30310bc58a91SEric W. Biederman pos = next_thread(start); 3032c1df7fb8SOleg Nesterov if (thread_group_leader(pos)) 30330bc58a91SEric W. Biederman pos = NULL; 3034c1df7fb8SOleg Nesterov else 3035c1df7fb8SOleg Nesterov get_task_struct(pos); 3036c1df7fb8SOleg Nesterov } 3037cc288738SEric W. Biederman rcu_read_unlock(); 30380bc58a91SEric W. Biederman put_task_struct(start); 30390bc58a91SEric W. Biederman return pos; 30400bc58a91SEric W. Biederman } 30410bc58a91SEric W. Biederman 304261a28784SEric W. Biederman static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir, 304361a28784SEric W. Biederman struct task_struct *task, int tid) 304461a28784SEric W. Biederman { 304561a28784SEric W. Biederman char name[PROC_NUMBUF]; 304661a28784SEric W. Biederman int len = snprintf(name, sizeof(name), "%d", tid); 304761a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, name, len, 304861a28784SEric W. Biederman proc_task_instantiate, task, NULL); 304961a28784SEric W. Biederman } 305061a28784SEric W. Biederman 30511da177e4SLinus Torvalds /* for the /proc/TGID/task/ directories */ 30521da177e4SLinus Torvalds static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir) 30531da177e4SLinus Torvalds { 30542fddfeefSJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 30551da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 30567d895244SGuillaume Chazarain struct task_struct *leader = NULL; 30570bc58a91SEric W. Biederman struct task_struct *task; 30581da177e4SLinus Torvalds int retval = -ENOENT; 30591da177e4SLinus Torvalds ino_t ino; 30600bc58a91SEric W. Biederman int tid; 3061b488893aSPavel Emelyanov struct pid_namespace *ns; 30621da177e4SLinus Torvalds 30637d895244SGuillaume Chazarain task = get_proc_task(inode); 30647d895244SGuillaume Chazarain if (!task) 30657d895244SGuillaume Chazarain goto out_no_task; 30667d895244SGuillaume Chazarain rcu_read_lock(); 30677d895244SGuillaume Chazarain if (pid_alive(task)) { 30687d895244SGuillaume Chazarain leader = task->group_leader; 30697d895244SGuillaume Chazarain get_task_struct(leader); 30707d895244SGuillaume Chazarain } 30717d895244SGuillaume Chazarain rcu_read_unlock(); 30727d895244SGuillaume Chazarain put_task_struct(task); 307399f89551SEric W. Biederman if (!leader) 307499f89551SEric W. Biederman goto out_no_task; 30751da177e4SLinus Torvalds retval = 0; 30761da177e4SLinus Torvalds 3077ee568b25SLinus Torvalds switch ((unsigned long)filp->f_pos) { 30781da177e4SLinus Torvalds case 0: 30791da177e4SLinus Torvalds ino = inode->i_ino; 3080ee6f779bSZhang Le if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) < 0) 30811da177e4SLinus Torvalds goto out; 3082ee6f779bSZhang Le filp->f_pos++; 30831da177e4SLinus Torvalds /* fall through */ 30841da177e4SLinus Torvalds case 1: 30851da177e4SLinus Torvalds ino = parent_ino(dentry); 3086ee6f779bSZhang Le if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) < 0) 30871da177e4SLinus Torvalds goto out; 3088ee6f779bSZhang Le filp->f_pos++; 30891da177e4SLinus Torvalds /* fall through */ 30901da177e4SLinus Torvalds } 30911da177e4SLinus Torvalds 30920bc58a91SEric W. Biederman /* f_version caches the tgid value that the last readdir call couldn't 30930bc58a91SEric W. Biederman * return. lseek aka telldir automagically resets f_version to 0. 30940bc58a91SEric W. Biederman */ 3095b488893aSPavel Emelyanov ns = filp->f_dentry->d_sb->s_fs_info; 30962b47c361SMathieu Desnoyers tid = (int)filp->f_version; 30970bc58a91SEric W. Biederman filp->f_version = 0; 3098ee6f779bSZhang Le for (task = first_tid(leader, tid, filp->f_pos - 2, ns); 30990bc58a91SEric W. Biederman task; 3100ee6f779bSZhang Le task = next_tid(task), filp->f_pos++) { 3101b488893aSPavel Emelyanov tid = task_pid_nr_ns(task, ns); 310261a28784SEric W. Biederman if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) { 31030bc58a91SEric W. Biederman /* returning this tgid failed, save it as the first 31040bc58a91SEric W. Biederman * pid for the next readir call */ 31052b47c361SMathieu Desnoyers filp->f_version = (u64)tid; 31060bc58a91SEric W. Biederman put_task_struct(task); 31071da177e4SLinus Torvalds break; 31080bc58a91SEric W. Biederman } 31091da177e4SLinus Torvalds } 31101da177e4SLinus Torvalds out: 311199f89551SEric W. Biederman put_task_struct(leader); 311299f89551SEric W. Biederman out_no_task: 31131da177e4SLinus Torvalds return retval; 31141da177e4SLinus Torvalds } 31156e66b52bSEric W. Biederman 31166e66b52bSEric W. Biederman static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) 31176e66b52bSEric W. Biederman { 31186e66b52bSEric W. Biederman struct inode *inode = dentry->d_inode; 311999f89551SEric W. Biederman struct task_struct *p = get_proc_task(inode); 31206e66b52bSEric W. Biederman generic_fillattr(inode, stat); 31216e66b52bSEric W. Biederman 312299f89551SEric W. Biederman if (p) { 312399f89551SEric W. Biederman stat->nlink += get_nr_threads(p); 312499f89551SEric W. Biederman put_task_struct(p); 31256e66b52bSEric W. Biederman } 31266e66b52bSEric W. Biederman 31276e66b52bSEric W. Biederman return 0; 31286e66b52bSEric W. Biederman } 312928a6d671SEric W. Biederman 3130c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations = { 313128a6d671SEric W. Biederman .lookup = proc_task_lookup, 313228a6d671SEric W. Biederman .getattr = proc_task_getattr, 313328a6d671SEric W. Biederman .setattr = proc_setattr, 313428a6d671SEric W. Biederman }; 313528a6d671SEric W. Biederman 313600977a59SArjan van de Ven static const struct file_operations proc_task_operations = { 313728a6d671SEric W. Biederman .read = generic_read_dir, 313828a6d671SEric W. Biederman .readdir = proc_task_readdir, 313928a6d671SEric W. Biederman }; 3140