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> 68d85f50d5SNeil Horman #include <linux/resource.h> 695096add8SKees Cook #include <linux/module.h> 701da177e4SLinus Torvalds #include <linux/mount.h> 711da177e4SLinus Torvalds #include <linux/security.h> 721da177e4SLinus Torvalds #include <linux/ptrace.h> 730d094efeSRoland McGrath #include <linux/tracehook.h> 74a424316cSPaul Menage #include <linux/cgroup.h> 751da177e4SLinus Torvalds #include <linux/cpuset.h> 761da177e4SLinus Torvalds #include <linux/audit.h> 775addc5ddSAl Viro #include <linux/poll.h> 781651e14eSSerge E. Hallyn #include <linux/nsproxy.h> 798ac773b4SAlexey Dobriyan #include <linux/oom.h> 803cb4a0bbSKawai, Hidehiro #include <linux/elf.h> 8160347f67SPavel Emelyanov #include <linux/pid_namespace.h> 821da177e4SLinus Torvalds #include "internal.h" 831da177e4SLinus Torvalds 840f2fe20fSEric W. Biederman /* NOTE: 850f2fe20fSEric W. Biederman * Implementing inode permission operations in /proc is almost 860f2fe20fSEric W. Biederman * certainly an error. Permission checks need to happen during 870f2fe20fSEric W. Biederman * each system call not at open time. The reason is that most of 880f2fe20fSEric W. Biederman * what we wish to check for permissions in /proc varies at runtime. 890f2fe20fSEric W. Biederman * 900f2fe20fSEric W. Biederman * The classic example of a problem is opening file descriptors 910f2fe20fSEric W. Biederman * in /proc for a task before it execs a suid executable. 920f2fe20fSEric W. Biederman */ 930f2fe20fSEric W. Biederman 941da177e4SLinus Torvalds struct pid_entry { 951da177e4SLinus Torvalds char *name; 96c5141e6dSEric Dumazet int len; 971da177e4SLinus Torvalds mode_t mode; 98c5ef1c42SArjan van de Ven const struct inode_operations *iop; 9900977a59SArjan van de Ven const struct file_operations *fop; 10020cdc894SEric W. Biederman union proc_op op; 1011da177e4SLinus Torvalds }; 1021da177e4SLinus Torvalds 10361a28784SEric W. Biederman #define NOD(NAME, MODE, IOP, FOP, OP) { \ 10420cdc894SEric W. Biederman .name = (NAME), \ 105c5141e6dSEric Dumazet .len = sizeof(NAME) - 1, \ 10620cdc894SEric W. Biederman .mode = MODE, \ 10720cdc894SEric W. Biederman .iop = IOP, \ 10820cdc894SEric W. Biederman .fop = FOP, \ 10920cdc894SEric W. Biederman .op = OP, \ 11020cdc894SEric W. Biederman } 11120cdc894SEric W. Biederman 11261a28784SEric W. Biederman #define DIR(NAME, MODE, OTYPE) \ 11361a28784SEric W. Biederman NOD(NAME, (S_IFDIR|(MODE)), \ 11420cdc894SEric W. Biederman &proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations, \ 11520cdc894SEric W. Biederman {} ) 11661a28784SEric W. Biederman #define LNK(NAME, OTYPE) \ 11761a28784SEric W. Biederman NOD(NAME, (S_IFLNK|S_IRWXUGO), \ 11820cdc894SEric W. Biederman &proc_pid_link_inode_operations, NULL, \ 11920cdc894SEric W. Biederman { .proc_get_link = &proc_##OTYPE##_link } ) 12061a28784SEric W. Biederman #define REG(NAME, MODE, OTYPE) \ 12161a28784SEric W. Biederman NOD(NAME, (S_IFREG|(MODE)), NULL, \ 12220cdc894SEric W. Biederman &proc_##OTYPE##_operations, {}) 12361a28784SEric W. Biederman #define INF(NAME, MODE, OTYPE) \ 12461a28784SEric W. Biederman NOD(NAME, (S_IFREG|(MODE)), \ 12520cdc894SEric W. Biederman NULL, &proc_info_file_operations, \ 12620cdc894SEric W. Biederman { .proc_read = &proc_##OTYPE } ) 127be614086SEric W. Biederman #define ONE(NAME, MODE, OTYPE) \ 128be614086SEric W. Biederman NOD(NAME, (S_IFREG|(MODE)), \ 129be614086SEric W. Biederman NULL, &proc_single_file_operations, \ 130be614086SEric W. Biederman { .proc_show = &proc_##OTYPE } ) 1311da177e4SLinus Torvalds 132aed54175SVegard Nossum /* 133aed54175SVegard Nossum * Count the number of hardlinks for the pid_entry table, excluding the . 134aed54175SVegard Nossum * and .. links. 135aed54175SVegard Nossum */ 136aed54175SVegard Nossum static unsigned int pid_entry_count_dirs(const struct pid_entry *entries, 137aed54175SVegard Nossum unsigned int n) 138aed54175SVegard Nossum { 139aed54175SVegard Nossum unsigned int i; 140aed54175SVegard Nossum unsigned int count; 141aed54175SVegard Nossum 142aed54175SVegard Nossum count = 0; 143aed54175SVegard Nossum for (i = 0; i < n; ++i) { 144aed54175SVegard Nossum if (S_ISDIR(entries[i].mode)) 145aed54175SVegard Nossum ++count; 146aed54175SVegard Nossum } 147aed54175SVegard Nossum 148aed54175SVegard Nossum return count; 149aed54175SVegard Nossum } 150aed54175SVegard Nossum 1510494f6ecSMiklos Szeredi static struct fs_struct *get_fs_struct(struct task_struct *task) 1521da177e4SLinus Torvalds { 1531da177e4SLinus Torvalds struct fs_struct *fs; 1540494f6ecSMiklos Szeredi task_lock(task); 1550494f6ecSMiklos Szeredi fs = task->fs; 1561da177e4SLinus Torvalds if(fs) 1571da177e4SLinus Torvalds atomic_inc(&fs->count); 1580494f6ecSMiklos Szeredi task_unlock(task); 1590494f6ecSMiklos Szeredi return fs; 1600494f6ecSMiklos Szeredi } 1610494f6ecSMiklos Szeredi 16299f89551SEric W. Biederman static int get_nr_threads(struct task_struct *tsk) 16399f89551SEric W. Biederman { 16499f89551SEric W. Biederman unsigned long flags; 16599f89551SEric W. Biederman int count = 0; 16699f89551SEric W. Biederman 16799f89551SEric W. Biederman if (lock_task_sighand(tsk, &flags)) { 16899f89551SEric W. Biederman count = atomic_read(&tsk->signal->count); 16999f89551SEric W. Biederman unlock_task_sighand(tsk, &flags); 17099f89551SEric W. Biederman } 17199f89551SEric W. Biederman return count; 17299f89551SEric W. Biederman } 17399f89551SEric W. Biederman 1743dcd25f3SJan Blunck static int proc_cwd_link(struct inode *inode, struct path *path) 1750494f6ecSMiklos Szeredi { 17699f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 17799f89551SEric W. Biederman struct fs_struct *fs = NULL; 1780494f6ecSMiklos Szeredi int result = -ENOENT; 17999f89551SEric W. Biederman 18099f89551SEric W. Biederman if (task) { 18199f89551SEric W. Biederman fs = get_fs_struct(task); 18299f89551SEric W. Biederman put_task_struct(task); 18399f89551SEric W. Biederman } 1841da177e4SLinus Torvalds if (fs) { 1851da177e4SLinus Torvalds read_lock(&fs->lock); 1863dcd25f3SJan Blunck *path = fs->pwd; 1873dcd25f3SJan Blunck path_get(&fs->pwd); 1881da177e4SLinus Torvalds read_unlock(&fs->lock); 1891da177e4SLinus Torvalds result = 0; 1901da177e4SLinus Torvalds put_fs_struct(fs); 1911da177e4SLinus Torvalds } 1921da177e4SLinus Torvalds return result; 1931da177e4SLinus Torvalds } 1941da177e4SLinus Torvalds 1953dcd25f3SJan Blunck static int proc_root_link(struct inode *inode, struct path *path) 1961da177e4SLinus Torvalds { 19799f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 19899f89551SEric W. Biederman struct fs_struct *fs = NULL; 1991da177e4SLinus Torvalds int result = -ENOENT; 20099f89551SEric W. Biederman 20199f89551SEric W. Biederman if (task) { 20299f89551SEric W. Biederman fs = get_fs_struct(task); 20399f89551SEric W. Biederman put_task_struct(task); 20499f89551SEric W. Biederman } 2051da177e4SLinus Torvalds if (fs) { 2061da177e4SLinus Torvalds read_lock(&fs->lock); 2073dcd25f3SJan Blunck *path = fs->root; 2083dcd25f3SJan Blunck path_get(&fs->root); 2091da177e4SLinus Torvalds read_unlock(&fs->lock); 2101da177e4SLinus Torvalds result = 0; 2111da177e4SLinus Torvalds put_fs_struct(fs); 2121da177e4SLinus Torvalds } 2131da177e4SLinus Torvalds return result; 2141da177e4SLinus Torvalds } 2151da177e4SLinus Torvalds 216638fa202SRoland McGrath /* 217638fa202SRoland McGrath * Return zero if current may access user memory in @task, -error if not. 218638fa202SRoland McGrath */ 219638fa202SRoland McGrath static int check_mem_permission(struct task_struct *task) 220638fa202SRoland McGrath { 221638fa202SRoland McGrath /* 222638fa202SRoland McGrath * A task can always look at itself, in case it chooses 223638fa202SRoland McGrath * to use system calls instead of load instructions. 224638fa202SRoland McGrath */ 225638fa202SRoland McGrath if (task == current) 226638fa202SRoland McGrath return 0; 227638fa202SRoland McGrath 228638fa202SRoland McGrath /* 229638fa202SRoland McGrath * If current is actively ptrace'ing, and would also be 230638fa202SRoland McGrath * permitted to freshly attach with ptrace now, permit it. 231638fa202SRoland McGrath */ 2320d094efeSRoland McGrath if (task_is_stopped_or_traced(task)) { 2330d094efeSRoland McGrath int match; 2340d094efeSRoland McGrath rcu_read_lock(); 2350d094efeSRoland McGrath match = (tracehook_tracer_task(task) == current); 2360d094efeSRoland McGrath rcu_read_unlock(); 2370d094efeSRoland McGrath if (match && ptrace_may_access(task, PTRACE_MODE_ATTACH)) 238638fa202SRoland McGrath return 0; 2390d094efeSRoland McGrath } 240638fa202SRoland McGrath 241638fa202SRoland McGrath /* 242638fa202SRoland McGrath * Noone else is allowed. 243638fa202SRoland McGrath */ 244638fa202SRoland McGrath return -EPERM; 245638fa202SRoland McGrath } 2461da177e4SLinus Torvalds 247831830b5SAl Viro struct mm_struct *mm_for_maps(struct task_struct *task) 248831830b5SAl Viro { 249831830b5SAl Viro struct mm_struct *mm = get_task_mm(task); 250831830b5SAl Viro if (!mm) 251831830b5SAl Viro return NULL; 252831830b5SAl Viro down_read(&mm->mmap_sem); 253831830b5SAl Viro task_lock(task); 254831830b5SAl Viro if (task->mm != mm) 255831830b5SAl Viro goto out; 256006ebb40SStephen Smalley if (task->mm != current->mm && 257006ebb40SStephen Smalley __ptrace_may_access(task, PTRACE_MODE_READ) < 0) 258831830b5SAl Viro goto out; 259831830b5SAl Viro task_unlock(task); 260831830b5SAl Viro return mm; 261831830b5SAl Viro out: 262831830b5SAl Viro task_unlock(task); 263831830b5SAl Viro up_read(&mm->mmap_sem); 264831830b5SAl Viro mmput(mm); 265831830b5SAl Viro return NULL; 266831830b5SAl Viro } 267831830b5SAl Viro 2681da177e4SLinus Torvalds static int proc_pid_cmdline(struct task_struct *task, char * buffer) 2691da177e4SLinus Torvalds { 2701da177e4SLinus Torvalds int res = 0; 2711da177e4SLinus Torvalds unsigned int len; 2721da177e4SLinus Torvalds struct mm_struct *mm = get_task_mm(task); 2731da177e4SLinus Torvalds if (!mm) 2741da177e4SLinus Torvalds goto out; 2751da177e4SLinus Torvalds if (!mm->arg_end) 2761da177e4SLinus Torvalds goto out_mm; /* Shh! No looking before we're done */ 2771da177e4SLinus Torvalds 2781da177e4SLinus Torvalds len = mm->arg_end - mm->arg_start; 2791da177e4SLinus Torvalds 2801da177e4SLinus Torvalds if (len > PAGE_SIZE) 2811da177e4SLinus Torvalds len = PAGE_SIZE; 2821da177e4SLinus Torvalds 2831da177e4SLinus Torvalds res = access_process_vm(task, mm->arg_start, buffer, len, 0); 2841da177e4SLinus Torvalds 2851da177e4SLinus Torvalds // If the nul at the end of args has been overwritten, then 2861da177e4SLinus Torvalds // assume application is using setproctitle(3). 2871da177e4SLinus Torvalds if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) { 2881da177e4SLinus Torvalds len = strnlen(buffer, res); 2891da177e4SLinus Torvalds if (len < res) { 2901da177e4SLinus Torvalds res = len; 2911da177e4SLinus Torvalds } else { 2921da177e4SLinus Torvalds len = mm->env_end - mm->env_start; 2931da177e4SLinus Torvalds if (len > PAGE_SIZE - res) 2941da177e4SLinus Torvalds len = PAGE_SIZE - res; 2951da177e4SLinus Torvalds res += access_process_vm(task, mm->env_start, buffer+res, len, 0); 2961da177e4SLinus Torvalds res = strnlen(buffer, res); 2971da177e4SLinus Torvalds } 2981da177e4SLinus Torvalds } 2991da177e4SLinus Torvalds out_mm: 3001da177e4SLinus Torvalds mmput(mm); 3011da177e4SLinus Torvalds out: 3021da177e4SLinus Torvalds return res; 3031da177e4SLinus Torvalds } 3041da177e4SLinus Torvalds 3051da177e4SLinus Torvalds static int proc_pid_auxv(struct task_struct *task, char *buffer) 3061da177e4SLinus Torvalds { 3071da177e4SLinus Torvalds int res = 0; 3081da177e4SLinus Torvalds struct mm_struct *mm = get_task_mm(task); 3091da177e4SLinus Torvalds if (mm) { 3101da177e4SLinus Torvalds unsigned int nwords = 0; 3111da177e4SLinus Torvalds do 3121da177e4SLinus Torvalds nwords += 2; 3131da177e4SLinus Torvalds while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */ 3141da177e4SLinus Torvalds res = nwords * sizeof(mm->saved_auxv[0]); 3151da177e4SLinus Torvalds if (res > PAGE_SIZE) 3161da177e4SLinus Torvalds res = PAGE_SIZE; 3171da177e4SLinus Torvalds memcpy(buffer, mm->saved_auxv, res); 3181da177e4SLinus Torvalds mmput(mm); 3191da177e4SLinus Torvalds } 3201da177e4SLinus Torvalds return res; 3211da177e4SLinus Torvalds } 3221da177e4SLinus Torvalds 3231da177e4SLinus Torvalds 3241da177e4SLinus Torvalds #ifdef CONFIG_KALLSYMS 3251da177e4SLinus Torvalds /* 3261da177e4SLinus Torvalds * Provides a wchan file via kallsyms in a proper one-value-per-file format. 3271da177e4SLinus Torvalds * Returns the resolved symbol. If that fails, simply return the address. 3281da177e4SLinus Torvalds */ 3291da177e4SLinus Torvalds static int proc_pid_wchan(struct task_struct *task, char *buffer) 3301da177e4SLinus Torvalds { 331ffb45122SAlexey Dobriyan unsigned long wchan; 3329281aceaSTejun Heo char symname[KSYM_NAME_LEN]; 3331da177e4SLinus Torvalds 3341da177e4SLinus Torvalds wchan = get_wchan(task); 3351da177e4SLinus Torvalds 3369d65cb4aSAlexey Dobriyan if (lookup_symbol_name(wchan, symname) < 0) 3371da177e4SLinus Torvalds return sprintf(buffer, "%lu", wchan); 3389d65cb4aSAlexey Dobriyan else 3399d65cb4aSAlexey Dobriyan return sprintf(buffer, "%s", symname); 3401da177e4SLinus Torvalds } 3411da177e4SLinus Torvalds #endif /* CONFIG_KALLSYMS */ 3421da177e4SLinus Torvalds 3431da177e4SLinus Torvalds #ifdef CONFIG_SCHEDSTATS 3441da177e4SLinus Torvalds /* 3451da177e4SLinus Torvalds * Provides /proc/PID/schedstat 3461da177e4SLinus Torvalds */ 3471da177e4SLinus Torvalds static int proc_pid_schedstat(struct task_struct *task, char *buffer) 3481da177e4SLinus Torvalds { 349172ba844SBalbir Singh return sprintf(buffer, "%llu %llu %lu\n", 3501da177e4SLinus Torvalds task->sched_info.cpu_time, 3511da177e4SLinus Torvalds task->sched_info.run_delay, 3522d72376bSIngo Molnar task->sched_info.pcount); 3531da177e4SLinus Torvalds } 3541da177e4SLinus Torvalds #endif 3551da177e4SLinus Torvalds 3569745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP 3579745512cSArjan van de Ven static int lstats_show_proc(struct seq_file *m, void *v) 3589745512cSArjan van de Ven { 3599745512cSArjan van de Ven int i; 36013d77c37SHiroshi Shimamoto struct inode *inode = m->private; 36113d77c37SHiroshi Shimamoto struct task_struct *task = get_proc_task(inode); 3629745512cSArjan van de Ven 36313d77c37SHiroshi Shimamoto if (!task) 36413d77c37SHiroshi Shimamoto return -ESRCH; 36513d77c37SHiroshi Shimamoto seq_puts(m, "Latency Top version : v0.1\n"); 3669745512cSArjan van de Ven for (i = 0; i < 32; i++) { 3679745512cSArjan van de Ven if (task->latency_record[i].backtrace[0]) { 3689745512cSArjan van de Ven int q; 3699745512cSArjan van de Ven seq_printf(m, "%i %li %li ", 3709745512cSArjan van de Ven task->latency_record[i].count, 3719745512cSArjan van de Ven task->latency_record[i].time, 3729745512cSArjan van de Ven task->latency_record[i].max); 3739745512cSArjan van de Ven for (q = 0; q < LT_BACKTRACEDEPTH; q++) { 3749745512cSArjan van de Ven char sym[KSYM_NAME_LEN]; 3759745512cSArjan van de Ven char *c; 3769745512cSArjan van de Ven if (!task->latency_record[i].backtrace[q]) 3779745512cSArjan van de Ven break; 3789745512cSArjan van de Ven if (task->latency_record[i].backtrace[q] == ULONG_MAX) 3799745512cSArjan van de Ven break; 3809745512cSArjan van de Ven sprint_symbol(sym, task->latency_record[i].backtrace[q]); 3819745512cSArjan van de Ven c = strchr(sym, '+'); 3829745512cSArjan van de Ven if (c) 3839745512cSArjan van de Ven *c = 0; 3849745512cSArjan van de Ven seq_printf(m, "%s ", sym); 3859745512cSArjan van de Ven } 3869745512cSArjan van de Ven seq_printf(m, "\n"); 3879745512cSArjan van de Ven } 3889745512cSArjan van de Ven 3899745512cSArjan van de Ven } 39013d77c37SHiroshi Shimamoto put_task_struct(task); 3919745512cSArjan van de Ven return 0; 3929745512cSArjan van de Ven } 3939745512cSArjan van de Ven 3949745512cSArjan van de Ven static int lstats_open(struct inode *inode, struct file *file) 3959745512cSArjan van de Ven { 39613d77c37SHiroshi Shimamoto return single_open(file, lstats_show_proc, inode); 397d6643d12SHiroshi Shimamoto } 398d6643d12SHiroshi Shimamoto 3999745512cSArjan van de Ven static ssize_t lstats_write(struct file *file, const char __user *buf, 4009745512cSArjan van de Ven size_t count, loff_t *offs) 4019745512cSArjan van de Ven { 40213d77c37SHiroshi Shimamoto struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 4039745512cSArjan van de Ven 40413d77c37SHiroshi Shimamoto if (!task) 40513d77c37SHiroshi Shimamoto return -ESRCH; 4069745512cSArjan van de Ven clear_all_latency_tracing(task); 40713d77c37SHiroshi Shimamoto put_task_struct(task); 4089745512cSArjan van de Ven 4099745512cSArjan van de Ven return count; 4109745512cSArjan van de Ven } 4119745512cSArjan van de Ven 4129745512cSArjan van de Ven static const struct file_operations proc_lstats_operations = { 4139745512cSArjan van de Ven .open = lstats_open, 4149745512cSArjan van de Ven .read = seq_read, 4159745512cSArjan van de Ven .write = lstats_write, 4169745512cSArjan van de Ven .llseek = seq_lseek, 41713d77c37SHiroshi Shimamoto .release = single_release, 4189745512cSArjan van de Ven }; 4199745512cSArjan van de Ven 4209745512cSArjan van de Ven #endif 4219745512cSArjan van de Ven 4221da177e4SLinus Torvalds /* The badness from the OOM killer */ 4231da177e4SLinus Torvalds unsigned long badness(struct task_struct *p, unsigned long uptime); 4241da177e4SLinus Torvalds static int proc_oom_score(struct task_struct *task, char *buffer) 4251da177e4SLinus Torvalds { 4261da177e4SLinus Torvalds unsigned long points; 4271da177e4SLinus Torvalds struct timespec uptime; 4281da177e4SLinus Torvalds 4291da177e4SLinus Torvalds do_posix_clock_monotonic_gettime(&uptime); 43019c5d45aSAlexey Dobriyan read_lock(&tasklist_lock); 4311da177e4SLinus Torvalds points = badness(task, uptime.tv_sec); 43219c5d45aSAlexey Dobriyan read_unlock(&tasklist_lock); 4331da177e4SLinus Torvalds return sprintf(buffer, "%lu\n", points); 4341da177e4SLinus Torvalds } 4351da177e4SLinus Torvalds 436d85f50d5SNeil Horman struct limit_names { 437d85f50d5SNeil Horman char *name; 438d85f50d5SNeil Horman char *unit; 439d85f50d5SNeil Horman }; 440d85f50d5SNeil Horman 441d85f50d5SNeil Horman static const struct limit_names lnames[RLIM_NLIMITS] = { 442d85f50d5SNeil Horman [RLIMIT_CPU] = {"Max cpu time", "ms"}, 443d85f50d5SNeil Horman [RLIMIT_FSIZE] = {"Max file size", "bytes"}, 444d85f50d5SNeil Horman [RLIMIT_DATA] = {"Max data size", "bytes"}, 445d85f50d5SNeil Horman [RLIMIT_STACK] = {"Max stack size", "bytes"}, 446d85f50d5SNeil Horman [RLIMIT_CORE] = {"Max core file size", "bytes"}, 447d85f50d5SNeil Horman [RLIMIT_RSS] = {"Max resident set", "bytes"}, 448d85f50d5SNeil Horman [RLIMIT_NPROC] = {"Max processes", "processes"}, 449d85f50d5SNeil Horman [RLIMIT_NOFILE] = {"Max open files", "files"}, 450d85f50d5SNeil Horman [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"}, 451d85f50d5SNeil Horman [RLIMIT_AS] = {"Max address space", "bytes"}, 452d85f50d5SNeil Horman [RLIMIT_LOCKS] = {"Max file locks", "locks"}, 453d85f50d5SNeil Horman [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"}, 454d85f50d5SNeil Horman [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"}, 455d85f50d5SNeil Horman [RLIMIT_NICE] = {"Max nice priority", NULL}, 456d85f50d5SNeil Horman [RLIMIT_RTPRIO] = {"Max realtime priority", NULL}, 4578808117cSEugene Teo [RLIMIT_RTTIME] = {"Max realtime timeout", "us"}, 458d85f50d5SNeil Horman }; 459d85f50d5SNeil Horman 460d85f50d5SNeil Horman /* Display limits for a process */ 461d85f50d5SNeil Horman static int proc_pid_limits(struct task_struct *task, char *buffer) 462d85f50d5SNeil Horman { 463d85f50d5SNeil Horman unsigned int i; 464d85f50d5SNeil Horman int count = 0; 465d85f50d5SNeil Horman unsigned long flags; 466d85f50d5SNeil Horman char *bufptr = buffer; 467d85f50d5SNeil Horman 468d85f50d5SNeil Horman struct rlimit rlim[RLIM_NLIMITS]; 469d85f50d5SNeil Horman 470a6bebbc8SLai Jiangshan if (!lock_task_sighand(task, &flags)) 471d85f50d5SNeil Horman return 0; 472d85f50d5SNeil Horman memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS); 473d85f50d5SNeil Horman unlock_task_sighand(task, &flags); 474d85f50d5SNeil Horman 475d85f50d5SNeil Horman /* 476d85f50d5SNeil Horman * print the file header 477d85f50d5SNeil Horman */ 478d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n", 479d85f50d5SNeil Horman "Limit", "Soft Limit", "Hard Limit", "Units"); 480d85f50d5SNeil Horman 481d85f50d5SNeil Horman for (i = 0; i < RLIM_NLIMITS; i++) { 482d85f50d5SNeil Horman if (rlim[i].rlim_cur == RLIM_INFINITY) 483d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-25s %-20s ", 484d85f50d5SNeil Horman lnames[i].name, "unlimited"); 485d85f50d5SNeil Horman else 486d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-25s %-20lu ", 487d85f50d5SNeil Horman lnames[i].name, rlim[i].rlim_cur); 488d85f50d5SNeil Horman 489d85f50d5SNeil Horman if (rlim[i].rlim_max == RLIM_INFINITY) 490d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-20s ", "unlimited"); 491d85f50d5SNeil Horman else 492d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-20lu ", 493d85f50d5SNeil Horman rlim[i].rlim_max); 494d85f50d5SNeil Horman 495d85f50d5SNeil Horman if (lnames[i].unit) 496d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-10s\n", 497d85f50d5SNeil Horman lnames[i].unit); 498d85f50d5SNeil Horman else 499d85f50d5SNeil Horman count += sprintf(&bufptr[count], "\n"); 500d85f50d5SNeil Horman } 501d85f50d5SNeil Horman 502d85f50d5SNeil Horman return count; 503d85f50d5SNeil Horman } 504d85f50d5SNeil Horman 505ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK 506ebcb6734SRoland McGrath static int proc_pid_syscall(struct task_struct *task, char *buffer) 507ebcb6734SRoland McGrath { 508ebcb6734SRoland McGrath long nr; 509ebcb6734SRoland McGrath unsigned long args[6], sp, pc; 510ebcb6734SRoland McGrath 511ebcb6734SRoland McGrath if (task_current_syscall(task, &nr, args, 6, &sp, &pc)) 512ebcb6734SRoland McGrath return sprintf(buffer, "running\n"); 513ebcb6734SRoland McGrath 514ebcb6734SRoland McGrath if (nr < 0) 515ebcb6734SRoland McGrath return sprintf(buffer, "%ld 0x%lx 0x%lx\n", nr, sp, pc); 516ebcb6734SRoland McGrath 517ebcb6734SRoland McGrath return sprintf(buffer, 518ebcb6734SRoland McGrath "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n", 519ebcb6734SRoland McGrath nr, 520ebcb6734SRoland McGrath args[0], args[1], args[2], args[3], args[4], args[5], 521ebcb6734SRoland McGrath sp, pc); 522ebcb6734SRoland McGrath } 523ebcb6734SRoland McGrath #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */ 524ebcb6734SRoland McGrath 5251da177e4SLinus Torvalds /************************************************************************/ 5261da177e4SLinus Torvalds /* Here the fs part begins */ 5271da177e4SLinus Torvalds /************************************************************************/ 5281da177e4SLinus Torvalds 5291da177e4SLinus Torvalds /* permission checks */ 530778c1144SEric W. Biederman static int proc_fd_access_allowed(struct inode *inode) 5311da177e4SLinus Torvalds { 532778c1144SEric W. Biederman struct task_struct *task; 533778c1144SEric W. Biederman int allowed = 0; 534df26c40eSEric W. Biederman /* Allow access to a task's file descriptors if it is us or we 535df26c40eSEric W. Biederman * may use ptrace attach to the process and find out that 536df26c40eSEric W. Biederman * information. 537778c1144SEric W. Biederman */ 538778c1144SEric W. Biederman task = get_proc_task(inode); 539df26c40eSEric W. Biederman if (task) { 540006ebb40SStephen Smalley allowed = ptrace_may_access(task, PTRACE_MODE_READ); 541778c1144SEric W. Biederman put_task_struct(task); 542df26c40eSEric W. Biederman } 543778c1144SEric W. Biederman return allowed; 5441da177e4SLinus Torvalds } 5451da177e4SLinus Torvalds 5466d76fa58SLinus Torvalds static int proc_setattr(struct dentry *dentry, struct iattr *attr) 5476d76fa58SLinus Torvalds { 5486d76fa58SLinus Torvalds int error; 5496d76fa58SLinus Torvalds struct inode *inode = dentry->d_inode; 5506d76fa58SLinus Torvalds 5516d76fa58SLinus Torvalds if (attr->ia_valid & ATTR_MODE) 5526d76fa58SLinus Torvalds return -EPERM; 5536d76fa58SLinus Torvalds 5546d76fa58SLinus Torvalds error = inode_change_ok(inode, attr); 5556d76fa58SLinus Torvalds if (!error) 5566d76fa58SLinus Torvalds error = inode_setattr(inode, attr); 5576d76fa58SLinus Torvalds return error; 5586d76fa58SLinus Torvalds } 5596d76fa58SLinus Torvalds 560c5ef1c42SArjan van de Ven static const struct inode_operations proc_def_inode_operations = { 5616d76fa58SLinus Torvalds .setattr = proc_setattr, 5626d76fa58SLinus Torvalds }; 5636d76fa58SLinus Torvalds 564a1a2c409SMiklos Szeredi static int mounts_open_common(struct inode *inode, struct file *file, 565a1a2c409SMiklos Szeredi const struct seq_operations *op) 5661da177e4SLinus Torvalds { 56799f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 568cf7b708cSPavel Emelyanov struct nsproxy *nsp; 5696b3286edSKirill Korotaev struct mnt_namespace *ns = NULL; 570a1a2c409SMiklos Szeredi struct fs_struct *fs = NULL; 571a1a2c409SMiklos Szeredi struct path root; 5725addc5ddSAl Viro struct proc_mounts *p; 5735addc5ddSAl Viro int ret = -EINVAL; 5745addc5ddSAl Viro 57599f89551SEric W. Biederman if (task) { 576cf7b708cSPavel Emelyanov rcu_read_lock(); 577cf7b708cSPavel Emelyanov nsp = task_nsproxy(task); 578cf7b708cSPavel Emelyanov if (nsp) { 579cf7b708cSPavel Emelyanov ns = nsp->mnt_ns; 5806b3286edSKirill Korotaev if (ns) 5816b3286edSKirill Korotaev get_mnt_ns(ns); 582863c4702SAlexey Dobriyan } 583cf7b708cSPavel Emelyanov rcu_read_unlock(); 584a1a2c409SMiklos Szeredi if (ns) 585a1a2c409SMiklos Szeredi fs = get_fs_struct(task); 58699f89551SEric W. Biederman put_task_struct(task); 58799f89551SEric W. Biederman } 5881da177e4SLinus Torvalds 589a1a2c409SMiklos Szeredi if (!ns) 590a1a2c409SMiklos Szeredi goto err; 591a1a2c409SMiklos Szeredi if (!fs) 592a1a2c409SMiklos Szeredi goto err_put_ns; 593a1a2c409SMiklos Szeredi 594a1a2c409SMiklos Szeredi read_lock(&fs->lock); 595a1a2c409SMiklos Szeredi root = fs->root; 596a1a2c409SMiklos Szeredi path_get(&root); 597a1a2c409SMiklos Szeredi read_unlock(&fs->lock); 598a1a2c409SMiklos Szeredi put_fs_struct(fs); 599a1a2c409SMiklos Szeredi 6005addc5ddSAl Viro ret = -ENOMEM; 6015addc5ddSAl Viro p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL); 602a1a2c409SMiklos Szeredi if (!p) 603a1a2c409SMiklos Szeredi goto err_put_path; 604a1a2c409SMiklos Szeredi 6055addc5ddSAl Viro file->private_data = &p->m; 606a1a2c409SMiklos Szeredi ret = seq_open(file, op); 607a1a2c409SMiklos Szeredi if (ret) 608a1a2c409SMiklos Szeredi goto err_free; 609a1a2c409SMiklos Szeredi 610a1a2c409SMiklos Szeredi p->m.private = p; 611a1a2c409SMiklos Szeredi p->ns = ns; 612a1a2c409SMiklos Szeredi p->root = root; 6136b3286edSKirill Korotaev p->event = ns->event; 614a1a2c409SMiklos Szeredi 6155addc5ddSAl Viro return 0; 616a1a2c409SMiklos Szeredi 617a1a2c409SMiklos Szeredi err_free: 6185addc5ddSAl Viro kfree(p); 619a1a2c409SMiklos Szeredi err_put_path: 620a1a2c409SMiklos Szeredi path_put(&root); 621a1a2c409SMiklos Szeredi err_put_ns: 6226b3286edSKirill Korotaev put_mnt_ns(ns); 623a1a2c409SMiklos Szeredi err: 6241da177e4SLinus Torvalds return ret; 6251da177e4SLinus Torvalds } 6261da177e4SLinus Torvalds 6271da177e4SLinus Torvalds static int mounts_release(struct inode *inode, struct file *file) 6281da177e4SLinus Torvalds { 629a1a2c409SMiklos Szeredi struct proc_mounts *p = file->private_data; 630a1a2c409SMiklos Szeredi path_put(&p->root); 631a1a2c409SMiklos Szeredi put_mnt_ns(p->ns); 6321da177e4SLinus Torvalds return seq_release(inode, file); 6331da177e4SLinus Torvalds } 6341da177e4SLinus Torvalds 6355addc5ddSAl Viro static unsigned mounts_poll(struct file *file, poll_table *wait) 6365addc5ddSAl Viro { 6375addc5ddSAl Viro struct proc_mounts *p = file->private_data; 638a1a2c409SMiklos Szeredi struct mnt_namespace *ns = p->ns; 6395addc5ddSAl Viro unsigned res = 0; 6405addc5ddSAl Viro 6415addc5ddSAl Viro poll_wait(file, &ns->poll, wait); 6425addc5ddSAl Viro 6435addc5ddSAl Viro spin_lock(&vfsmount_lock); 6445addc5ddSAl Viro if (p->event != ns->event) { 6455addc5ddSAl Viro p->event = ns->event; 6465addc5ddSAl Viro res = POLLERR; 6475addc5ddSAl Viro } 6485addc5ddSAl Viro spin_unlock(&vfsmount_lock); 6495addc5ddSAl Viro 6505addc5ddSAl Viro return res; 6515addc5ddSAl Viro } 6525addc5ddSAl Viro 653a1a2c409SMiklos Szeredi static int mounts_open(struct inode *inode, struct file *file) 654a1a2c409SMiklos Szeredi { 655a1a2c409SMiklos Szeredi return mounts_open_common(inode, file, &mounts_op); 656a1a2c409SMiklos Szeredi } 657a1a2c409SMiklos Szeredi 65800977a59SArjan van de Ven static const struct file_operations proc_mounts_operations = { 6591da177e4SLinus Torvalds .open = mounts_open, 6601da177e4SLinus Torvalds .read = seq_read, 6611da177e4SLinus Torvalds .llseek = seq_lseek, 6621da177e4SLinus Torvalds .release = mounts_release, 6635addc5ddSAl Viro .poll = mounts_poll, 6641da177e4SLinus Torvalds }; 6651da177e4SLinus Torvalds 6662d4d4864SRam Pai static int mountinfo_open(struct inode *inode, struct file *file) 6672d4d4864SRam Pai { 6682d4d4864SRam Pai return mounts_open_common(inode, file, &mountinfo_op); 6692d4d4864SRam Pai } 6702d4d4864SRam Pai 6712d4d4864SRam Pai static const struct file_operations proc_mountinfo_operations = { 6722d4d4864SRam Pai .open = mountinfo_open, 6732d4d4864SRam Pai .read = seq_read, 6742d4d4864SRam Pai .llseek = seq_lseek, 6752d4d4864SRam Pai .release = mounts_release, 6762d4d4864SRam Pai .poll = mounts_poll, 6772d4d4864SRam Pai }; 6782d4d4864SRam Pai 679b4629fe2SChuck Lever static int mountstats_open(struct inode *inode, struct file *file) 680b4629fe2SChuck Lever { 681a1a2c409SMiklos Szeredi return mounts_open_common(inode, file, &mountstats_op); 682b4629fe2SChuck Lever } 683b4629fe2SChuck Lever 68400977a59SArjan van de Ven static const struct file_operations proc_mountstats_operations = { 685b4629fe2SChuck Lever .open = mountstats_open, 686b4629fe2SChuck Lever .read = seq_read, 687b4629fe2SChuck Lever .llseek = seq_lseek, 688b4629fe2SChuck Lever .release = mounts_release, 689b4629fe2SChuck Lever }; 690b4629fe2SChuck Lever 6911da177e4SLinus Torvalds #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */ 6921da177e4SLinus Torvalds 6931da177e4SLinus Torvalds static ssize_t proc_info_read(struct file * file, char __user * buf, 6941da177e4SLinus Torvalds size_t count, loff_t *ppos) 6951da177e4SLinus Torvalds { 6962fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 6971da177e4SLinus Torvalds unsigned long page; 6981da177e4SLinus Torvalds ssize_t length; 69999f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 70099f89551SEric W. Biederman 70199f89551SEric W. Biederman length = -ESRCH; 70299f89551SEric W. Biederman if (!task) 70399f89551SEric W. Biederman goto out_no_task; 7041da177e4SLinus Torvalds 7051da177e4SLinus Torvalds if (count > PROC_BLOCK_SIZE) 7061da177e4SLinus Torvalds count = PROC_BLOCK_SIZE; 70799f89551SEric W. Biederman 70899f89551SEric W. Biederman length = -ENOMEM; 709e12ba74dSMel Gorman if (!(page = __get_free_page(GFP_TEMPORARY))) 71099f89551SEric W. Biederman goto out; 7111da177e4SLinus Torvalds 7121da177e4SLinus Torvalds length = PROC_I(inode)->op.proc_read(task, (char*)page); 7131da177e4SLinus Torvalds 7141da177e4SLinus Torvalds if (length >= 0) 7151da177e4SLinus Torvalds length = simple_read_from_buffer(buf, count, ppos, (char *)page, length); 7161da177e4SLinus Torvalds free_page(page); 71799f89551SEric W. Biederman out: 71899f89551SEric W. Biederman put_task_struct(task); 71999f89551SEric W. Biederman out_no_task: 7201da177e4SLinus Torvalds return length; 7211da177e4SLinus Torvalds } 7221da177e4SLinus Torvalds 72300977a59SArjan van de Ven static const struct file_operations proc_info_file_operations = { 7241da177e4SLinus Torvalds .read = proc_info_read, 7251da177e4SLinus Torvalds }; 7261da177e4SLinus Torvalds 727be614086SEric W. Biederman static int proc_single_show(struct seq_file *m, void *v) 728be614086SEric W. Biederman { 729be614086SEric W. Biederman struct inode *inode = m->private; 730be614086SEric W. Biederman struct pid_namespace *ns; 731be614086SEric W. Biederman struct pid *pid; 732be614086SEric W. Biederman struct task_struct *task; 733be614086SEric W. Biederman int ret; 734be614086SEric W. Biederman 735be614086SEric W. Biederman ns = inode->i_sb->s_fs_info; 736be614086SEric W. Biederman pid = proc_pid(inode); 737be614086SEric W. Biederman task = get_pid_task(pid, PIDTYPE_PID); 738be614086SEric W. Biederman if (!task) 739be614086SEric W. Biederman return -ESRCH; 740be614086SEric W. Biederman 741be614086SEric W. Biederman ret = PROC_I(inode)->op.proc_show(m, ns, pid, task); 742be614086SEric W. Biederman 743be614086SEric W. Biederman put_task_struct(task); 744be614086SEric W. Biederman return ret; 745be614086SEric W. Biederman } 746be614086SEric W. Biederman 747be614086SEric W. Biederman static int proc_single_open(struct inode *inode, struct file *filp) 748be614086SEric W. Biederman { 749be614086SEric W. Biederman int ret; 750be614086SEric W. Biederman ret = single_open(filp, proc_single_show, NULL); 751be614086SEric W. Biederman if (!ret) { 752be614086SEric W. Biederman struct seq_file *m = filp->private_data; 753be614086SEric W. Biederman 754be614086SEric W. Biederman m->private = inode; 755be614086SEric W. Biederman } 756be614086SEric W. Biederman return ret; 757be614086SEric W. Biederman } 758be614086SEric W. Biederman 759be614086SEric W. Biederman static const struct file_operations proc_single_file_operations = { 760be614086SEric W. Biederman .open = proc_single_open, 761be614086SEric W. Biederman .read = seq_read, 762be614086SEric W. Biederman .llseek = seq_lseek, 763be614086SEric W. Biederman .release = single_release, 764be614086SEric W. Biederman }; 765be614086SEric W. Biederman 7661da177e4SLinus Torvalds static int mem_open(struct inode* inode, struct file* file) 7671da177e4SLinus Torvalds { 7681da177e4SLinus Torvalds file->private_data = (void*)((long)current->self_exec_id); 7691da177e4SLinus Torvalds return 0; 7701da177e4SLinus Torvalds } 7711da177e4SLinus Torvalds 7721da177e4SLinus Torvalds static ssize_t mem_read(struct file * file, char __user * buf, 7731da177e4SLinus Torvalds size_t count, loff_t *ppos) 7741da177e4SLinus Torvalds { 7752fddfeefSJosef "Jeff" Sipek struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode); 7761da177e4SLinus Torvalds char *page; 7771da177e4SLinus Torvalds unsigned long src = *ppos; 7781da177e4SLinus Torvalds int ret = -ESRCH; 7791da177e4SLinus Torvalds struct mm_struct *mm; 7801da177e4SLinus Torvalds 78199f89551SEric W. Biederman if (!task) 78299f89551SEric W. Biederman goto out_no_task; 78399f89551SEric W. Biederman 784638fa202SRoland McGrath if (check_mem_permission(task)) 7851da177e4SLinus Torvalds goto out; 7861da177e4SLinus Torvalds 7871da177e4SLinus Torvalds ret = -ENOMEM; 788e12ba74dSMel Gorman page = (char *)__get_free_page(GFP_TEMPORARY); 7891da177e4SLinus Torvalds if (!page) 7901da177e4SLinus Torvalds goto out; 7911da177e4SLinus Torvalds 7921da177e4SLinus Torvalds ret = 0; 7931da177e4SLinus Torvalds 7941da177e4SLinus Torvalds mm = get_task_mm(task); 7951da177e4SLinus Torvalds if (!mm) 7961da177e4SLinus Torvalds goto out_free; 7971da177e4SLinus Torvalds 7981da177e4SLinus Torvalds ret = -EIO; 7991da177e4SLinus Torvalds 8001da177e4SLinus Torvalds if (file->private_data != (void*)((long)current->self_exec_id)) 8011da177e4SLinus Torvalds goto out_put; 8021da177e4SLinus Torvalds 8031da177e4SLinus Torvalds ret = 0; 8041da177e4SLinus Torvalds 8051da177e4SLinus Torvalds while (count > 0) { 8061da177e4SLinus Torvalds int this_len, retval; 8071da177e4SLinus Torvalds 8081da177e4SLinus Torvalds this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count; 8091da177e4SLinus Torvalds retval = access_process_vm(task, src, page, this_len, 0); 810638fa202SRoland McGrath if (!retval || check_mem_permission(task)) { 8111da177e4SLinus Torvalds if (!ret) 8121da177e4SLinus Torvalds ret = -EIO; 8131da177e4SLinus Torvalds break; 8141da177e4SLinus Torvalds } 8151da177e4SLinus Torvalds 8161da177e4SLinus Torvalds if (copy_to_user(buf, page, retval)) { 8171da177e4SLinus Torvalds ret = -EFAULT; 8181da177e4SLinus Torvalds break; 8191da177e4SLinus Torvalds } 8201da177e4SLinus Torvalds 8211da177e4SLinus Torvalds ret += retval; 8221da177e4SLinus Torvalds src += retval; 8231da177e4SLinus Torvalds buf += retval; 8241da177e4SLinus Torvalds count -= retval; 8251da177e4SLinus Torvalds } 8261da177e4SLinus Torvalds *ppos = src; 8271da177e4SLinus Torvalds 8281da177e4SLinus Torvalds out_put: 8291da177e4SLinus Torvalds mmput(mm); 8301da177e4SLinus Torvalds out_free: 8311da177e4SLinus Torvalds free_page((unsigned long) page); 8321da177e4SLinus Torvalds out: 83399f89551SEric W. Biederman put_task_struct(task); 83499f89551SEric W. Biederman out_no_task: 8351da177e4SLinus Torvalds return ret; 8361da177e4SLinus Torvalds } 8371da177e4SLinus Torvalds 8381da177e4SLinus Torvalds #define mem_write NULL 8391da177e4SLinus Torvalds 8401da177e4SLinus Torvalds #ifndef mem_write 8411da177e4SLinus Torvalds /* This is a security hazard */ 84263967fa9SGlauber de Oliveira Costa static ssize_t mem_write(struct file * file, const char __user *buf, 8431da177e4SLinus Torvalds size_t count, loff_t *ppos) 8441da177e4SLinus Torvalds { 845f7ca54f4SFrederik Deweerdt int copied; 8461da177e4SLinus Torvalds char *page; 8472fddfeefSJosef "Jeff" Sipek struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode); 8481da177e4SLinus Torvalds unsigned long dst = *ppos; 8491da177e4SLinus Torvalds 85099f89551SEric W. Biederman copied = -ESRCH; 85199f89551SEric W. Biederman if (!task) 85299f89551SEric W. Biederman goto out_no_task; 8531da177e4SLinus Torvalds 854638fa202SRoland McGrath if (check_mem_permission(task)) 85599f89551SEric W. Biederman goto out; 85699f89551SEric W. Biederman 85799f89551SEric W. Biederman copied = -ENOMEM; 858e12ba74dSMel Gorman page = (char *)__get_free_page(GFP_TEMPORARY); 8591da177e4SLinus Torvalds if (!page) 86099f89551SEric W. Biederman goto out; 8611da177e4SLinus Torvalds 862f7ca54f4SFrederik Deweerdt copied = 0; 8631da177e4SLinus Torvalds while (count > 0) { 8641da177e4SLinus Torvalds int this_len, retval; 8651da177e4SLinus Torvalds 8661da177e4SLinus Torvalds this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count; 8671da177e4SLinus Torvalds if (copy_from_user(page, buf, this_len)) { 8681da177e4SLinus Torvalds copied = -EFAULT; 8691da177e4SLinus Torvalds break; 8701da177e4SLinus Torvalds } 8711da177e4SLinus Torvalds retval = access_process_vm(task, dst, page, this_len, 1); 8721da177e4SLinus Torvalds if (!retval) { 8731da177e4SLinus Torvalds if (!copied) 8741da177e4SLinus Torvalds copied = -EIO; 8751da177e4SLinus Torvalds break; 8761da177e4SLinus Torvalds } 8771da177e4SLinus Torvalds copied += retval; 8781da177e4SLinus Torvalds buf += retval; 8791da177e4SLinus Torvalds dst += retval; 8801da177e4SLinus Torvalds count -= retval; 8811da177e4SLinus Torvalds } 8821da177e4SLinus Torvalds *ppos = dst; 8831da177e4SLinus Torvalds free_page((unsigned long) page); 88499f89551SEric W. Biederman out: 88599f89551SEric W. Biederman put_task_struct(task); 88699f89551SEric W. Biederman out_no_task: 8871da177e4SLinus Torvalds return copied; 8881da177e4SLinus Torvalds } 8891da177e4SLinus Torvalds #endif 8901da177e4SLinus Torvalds 89185863e47SMatt Mackall loff_t mem_lseek(struct file *file, loff_t offset, int orig) 8921da177e4SLinus Torvalds { 8931da177e4SLinus Torvalds switch (orig) { 8941da177e4SLinus Torvalds case 0: 8951da177e4SLinus Torvalds file->f_pos = offset; 8961da177e4SLinus Torvalds break; 8971da177e4SLinus Torvalds case 1: 8981da177e4SLinus Torvalds file->f_pos += offset; 8991da177e4SLinus Torvalds break; 9001da177e4SLinus Torvalds default: 9011da177e4SLinus Torvalds return -EINVAL; 9021da177e4SLinus Torvalds } 9031da177e4SLinus Torvalds force_successful_syscall_return(); 9041da177e4SLinus Torvalds return file->f_pos; 9051da177e4SLinus Torvalds } 9061da177e4SLinus Torvalds 90700977a59SArjan van de Ven static const struct file_operations proc_mem_operations = { 9081da177e4SLinus Torvalds .llseek = mem_lseek, 9091da177e4SLinus Torvalds .read = mem_read, 9101da177e4SLinus Torvalds .write = mem_write, 9111da177e4SLinus Torvalds .open = mem_open, 9121da177e4SLinus Torvalds }; 9131da177e4SLinus Torvalds 914315e28c8SJames Pearson static ssize_t environ_read(struct file *file, char __user *buf, 915315e28c8SJames Pearson size_t count, loff_t *ppos) 916315e28c8SJames Pearson { 917315e28c8SJames Pearson struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 918315e28c8SJames Pearson char *page; 919315e28c8SJames Pearson unsigned long src = *ppos; 920315e28c8SJames Pearson int ret = -ESRCH; 921315e28c8SJames Pearson struct mm_struct *mm; 922315e28c8SJames Pearson 923315e28c8SJames Pearson if (!task) 924315e28c8SJames Pearson goto out_no_task; 925315e28c8SJames Pearson 926006ebb40SStephen Smalley if (!ptrace_may_access(task, PTRACE_MODE_READ)) 927315e28c8SJames Pearson goto out; 928315e28c8SJames Pearson 929315e28c8SJames Pearson ret = -ENOMEM; 930315e28c8SJames Pearson page = (char *)__get_free_page(GFP_TEMPORARY); 931315e28c8SJames Pearson if (!page) 932315e28c8SJames Pearson goto out; 933315e28c8SJames Pearson 934315e28c8SJames Pearson ret = 0; 935315e28c8SJames Pearson 936315e28c8SJames Pearson mm = get_task_mm(task); 937315e28c8SJames Pearson if (!mm) 938315e28c8SJames Pearson goto out_free; 939315e28c8SJames Pearson 940315e28c8SJames Pearson while (count > 0) { 941315e28c8SJames Pearson int this_len, retval, max_len; 942315e28c8SJames Pearson 943315e28c8SJames Pearson this_len = mm->env_end - (mm->env_start + src); 944315e28c8SJames Pearson 945315e28c8SJames Pearson if (this_len <= 0) 946315e28c8SJames Pearson break; 947315e28c8SJames Pearson 948315e28c8SJames Pearson max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count; 949315e28c8SJames Pearson this_len = (this_len > max_len) ? max_len : this_len; 950315e28c8SJames Pearson 951315e28c8SJames Pearson retval = access_process_vm(task, (mm->env_start + src), 952315e28c8SJames Pearson page, this_len, 0); 953315e28c8SJames Pearson 954315e28c8SJames Pearson if (retval <= 0) { 955315e28c8SJames Pearson ret = retval; 956315e28c8SJames Pearson break; 957315e28c8SJames Pearson } 958315e28c8SJames Pearson 959315e28c8SJames Pearson if (copy_to_user(buf, page, retval)) { 960315e28c8SJames Pearson ret = -EFAULT; 961315e28c8SJames Pearson break; 962315e28c8SJames Pearson } 963315e28c8SJames Pearson 964315e28c8SJames Pearson ret += retval; 965315e28c8SJames Pearson src += retval; 966315e28c8SJames Pearson buf += retval; 967315e28c8SJames Pearson count -= retval; 968315e28c8SJames Pearson } 969315e28c8SJames Pearson *ppos = src; 970315e28c8SJames Pearson 971315e28c8SJames Pearson mmput(mm); 972315e28c8SJames Pearson out_free: 973315e28c8SJames Pearson free_page((unsigned long) page); 974315e28c8SJames Pearson out: 975315e28c8SJames Pearson put_task_struct(task); 976315e28c8SJames Pearson out_no_task: 977315e28c8SJames Pearson return ret; 978315e28c8SJames Pearson } 979315e28c8SJames Pearson 980315e28c8SJames Pearson static const struct file_operations proc_environ_operations = { 981315e28c8SJames Pearson .read = environ_read, 982315e28c8SJames Pearson }; 983315e28c8SJames Pearson 9841da177e4SLinus Torvalds static ssize_t oom_adjust_read(struct file *file, char __user *buf, 9851da177e4SLinus Torvalds size_t count, loff_t *ppos) 9861da177e4SLinus Torvalds { 9872fddfeefSJosef "Jeff" Sipek struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode); 9888578cea7SEric W. Biederman char buffer[PROC_NUMBUF]; 9891da177e4SLinus Torvalds size_t len; 99099f89551SEric W. Biederman int oom_adjust; 9911da177e4SLinus Torvalds 99299f89551SEric W. Biederman if (!task) 99399f89551SEric W. Biederman return -ESRCH; 99499f89551SEric W. Biederman oom_adjust = task->oomkilladj; 99599f89551SEric W. Biederman put_task_struct(task); 99699f89551SEric W. Biederman 9978578cea7SEric W. Biederman len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust); 9980c28f287SAkinobu Mita 9990c28f287SAkinobu Mita return simple_read_from_buffer(buf, count, ppos, buffer, len); 10001da177e4SLinus Torvalds } 10011da177e4SLinus Torvalds 10021da177e4SLinus Torvalds static ssize_t oom_adjust_write(struct file *file, const char __user *buf, 10031da177e4SLinus Torvalds size_t count, loff_t *ppos) 10041da177e4SLinus Torvalds { 100599f89551SEric W. Biederman struct task_struct *task; 10068578cea7SEric W. Biederman char buffer[PROC_NUMBUF], *end; 10071da177e4SLinus Torvalds int oom_adjust; 10081da177e4SLinus Torvalds 10098578cea7SEric W. Biederman memset(buffer, 0, sizeof(buffer)); 10108578cea7SEric W. Biederman if (count > sizeof(buffer) - 1) 10118578cea7SEric W. Biederman count = sizeof(buffer) - 1; 10121da177e4SLinus Torvalds if (copy_from_user(buffer, buf, count)) 10131da177e4SLinus Torvalds return -EFAULT; 10141da177e4SLinus Torvalds oom_adjust = simple_strtol(buffer, &end, 0); 10158ac773b4SAlexey Dobriyan if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) && 10168ac773b4SAlexey Dobriyan oom_adjust != OOM_DISABLE) 10171da177e4SLinus Torvalds return -EINVAL; 10181da177e4SLinus Torvalds if (*end == '\n') 10191da177e4SLinus Torvalds end++; 10202fddfeefSJosef "Jeff" Sipek task = get_proc_task(file->f_path.dentry->d_inode); 102199f89551SEric W. Biederman if (!task) 102299f89551SEric W. Biederman return -ESRCH; 10238fb4fc68SGuillem Jover if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) { 10248fb4fc68SGuillem Jover put_task_struct(task); 10258fb4fc68SGuillem Jover return -EACCES; 10268fb4fc68SGuillem Jover } 10271da177e4SLinus Torvalds task->oomkilladj = oom_adjust; 102899f89551SEric W. Biederman put_task_struct(task); 10291da177e4SLinus Torvalds if (end - buffer == 0) 10301da177e4SLinus Torvalds return -EIO; 10311da177e4SLinus Torvalds return end - buffer; 10321da177e4SLinus Torvalds } 10331da177e4SLinus Torvalds 103400977a59SArjan van de Ven static const struct file_operations proc_oom_adjust_operations = { 10351da177e4SLinus Torvalds .read = oom_adjust_read, 10361da177e4SLinus Torvalds .write = oom_adjust_write, 10371da177e4SLinus Torvalds }; 10381da177e4SLinus Torvalds 10391da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL 10401da177e4SLinus Torvalds #define TMPBUFLEN 21 10411da177e4SLinus Torvalds static ssize_t proc_loginuid_read(struct file * file, char __user * buf, 10421da177e4SLinus Torvalds size_t count, loff_t *ppos) 10431da177e4SLinus Torvalds { 10442fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 104599f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 10461da177e4SLinus Torvalds ssize_t length; 10471da177e4SLinus Torvalds char tmpbuf[TMPBUFLEN]; 10481da177e4SLinus Torvalds 104999f89551SEric W. Biederman if (!task) 105099f89551SEric W. Biederman return -ESRCH; 10511da177e4SLinus Torvalds length = scnprintf(tmpbuf, TMPBUFLEN, "%u", 10520c11b942SAl Viro audit_get_loginuid(task)); 105399f89551SEric W. Biederman put_task_struct(task); 10541da177e4SLinus Torvalds return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 10551da177e4SLinus Torvalds } 10561da177e4SLinus Torvalds 10571da177e4SLinus Torvalds static ssize_t proc_loginuid_write(struct file * file, const char __user * buf, 10581da177e4SLinus Torvalds size_t count, loff_t *ppos) 10591da177e4SLinus Torvalds { 10602fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 10611da177e4SLinus Torvalds char *page, *tmp; 10621da177e4SLinus Torvalds ssize_t length; 10631da177e4SLinus Torvalds uid_t loginuid; 10641da177e4SLinus Torvalds 10651da177e4SLinus Torvalds if (!capable(CAP_AUDIT_CONTROL)) 10661da177e4SLinus Torvalds return -EPERM; 10671da177e4SLinus Torvalds 106813b41b09SEric W. Biederman if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) 10691da177e4SLinus Torvalds return -EPERM; 10701da177e4SLinus Torvalds 1071e0182909SAl Viro if (count >= PAGE_SIZE) 1072e0182909SAl Viro count = PAGE_SIZE - 1; 10731da177e4SLinus Torvalds 10741da177e4SLinus Torvalds if (*ppos != 0) { 10751da177e4SLinus Torvalds /* No partial writes. */ 10761da177e4SLinus Torvalds return -EINVAL; 10771da177e4SLinus Torvalds } 1078e12ba74dSMel Gorman page = (char*)__get_free_page(GFP_TEMPORARY); 10791da177e4SLinus Torvalds if (!page) 10801da177e4SLinus Torvalds return -ENOMEM; 10811da177e4SLinus Torvalds length = -EFAULT; 10821da177e4SLinus Torvalds if (copy_from_user(page, buf, count)) 10831da177e4SLinus Torvalds goto out_free_page; 10841da177e4SLinus Torvalds 1085e0182909SAl Viro page[count] = '\0'; 10861da177e4SLinus Torvalds loginuid = simple_strtoul(page, &tmp, 10); 10871da177e4SLinus Torvalds if (tmp == page) { 10881da177e4SLinus Torvalds length = -EINVAL; 10891da177e4SLinus Torvalds goto out_free_page; 10901da177e4SLinus Torvalds 10911da177e4SLinus Torvalds } 109299f89551SEric W. Biederman length = audit_set_loginuid(current, loginuid); 10931da177e4SLinus Torvalds if (likely(length == 0)) 10941da177e4SLinus Torvalds length = count; 10951da177e4SLinus Torvalds 10961da177e4SLinus Torvalds out_free_page: 10971da177e4SLinus Torvalds free_page((unsigned long) page); 10981da177e4SLinus Torvalds return length; 10991da177e4SLinus Torvalds } 11001da177e4SLinus Torvalds 110100977a59SArjan van de Ven static const struct file_operations proc_loginuid_operations = { 11021da177e4SLinus Torvalds .read = proc_loginuid_read, 11031da177e4SLinus Torvalds .write = proc_loginuid_write, 11041da177e4SLinus Torvalds }; 11051e0bd755SEric Paris 11061e0bd755SEric Paris static ssize_t proc_sessionid_read(struct file * file, char __user * buf, 11071e0bd755SEric Paris size_t count, loff_t *ppos) 11081e0bd755SEric Paris { 11091e0bd755SEric Paris struct inode * inode = file->f_path.dentry->d_inode; 11101e0bd755SEric Paris struct task_struct *task = get_proc_task(inode); 11111e0bd755SEric Paris ssize_t length; 11121e0bd755SEric Paris char tmpbuf[TMPBUFLEN]; 11131e0bd755SEric Paris 11141e0bd755SEric Paris if (!task) 11151e0bd755SEric Paris return -ESRCH; 11161e0bd755SEric Paris length = scnprintf(tmpbuf, TMPBUFLEN, "%u", 11171e0bd755SEric Paris audit_get_sessionid(task)); 11181e0bd755SEric Paris put_task_struct(task); 11191e0bd755SEric Paris return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 11201e0bd755SEric Paris } 11211e0bd755SEric Paris 11221e0bd755SEric Paris static const struct file_operations proc_sessionid_operations = { 11231e0bd755SEric Paris .read = proc_sessionid_read, 11241e0bd755SEric Paris }; 11251da177e4SLinus Torvalds #endif 11261da177e4SLinus Torvalds 1127f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION 1128f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_read(struct file * file, char __user * buf, 1129f4f154fdSAkinobu Mita size_t count, loff_t *ppos) 1130f4f154fdSAkinobu Mita { 1131f4f154fdSAkinobu Mita struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 1132f4f154fdSAkinobu Mita char buffer[PROC_NUMBUF]; 1133f4f154fdSAkinobu Mita size_t len; 1134f4f154fdSAkinobu Mita int make_it_fail; 1135f4f154fdSAkinobu Mita 1136f4f154fdSAkinobu Mita if (!task) 1137f4f154fdSAkinobu Mita return -ESRCH; 1138f4f154fdSAkinobu Mita make_it_fail = task->make_it_fail; 1139f4f154fdSAkinobu Mita put_task_struct(task); 1140f4f154fdSAkinobu Mita 1141f4f154fdSAkinobu Mita len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail); 11420c28f287SAkinobu Mita 11430c28f287SAkinobu Mita return simple_read_from_buffer(buf, count, ppos, buffer, len); 1144f4f154fdSAkinobu Mita } 1145f4f154fdSAkinobu Mita 1146f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_write(struct file * file, 1147f4f154fdSAkinobu Mita const char __user * buf, size_t count, loff_t *ppos) 1148f4f154fdSAkinobu Mita { 1149f4f154fdSAkinobu Mita struct task_struct *task; 1150f4f154fdSAkinobu Mita char buffer[PROC_NUMBUF], *end; 1151f4f154fdSAkinobu Mita int make_it_fail; 1152f4f154fdSAkinobu Mita 1153f4f154fdSAkinobu Mita if (!capable(CAP_SYS_RESOURCE)) 1154f4f154fdSAkinobu Mita return -EPERM; 1155f4f154fdSAkinobu Mita memset(buffer, 0, sizeof(buffer)); 1156f4f154fdSAkinobu Mita if (count > sizeof(buffer) - 1) 1157f4f154fdSAkinobu Mita count = sizeof(buffer) - 1; 1158f4f154fdSAkinobu Mita if (copy_from_user(buffer, buf, count)) 1159f4f154fdSAkinobu Mita return -EFAULT; 1160f4f154fdSAkinobu Mita make_it_fail = simple_strtol(buffer, &end, 0); 1161f4f154fdSAkinobu Mita if (*end == '\n') 1162f4f154fdSAkinobu Mita end++; 1163f4f154fdSAkinobu Mita task = get_proc_task(file->f_dentry->d_inode); 1164f4f154fdSAkinobu Mita if (!task) 1165f4f154fdSAkinobu Mita return -ESRCH; 1166f4f154fdSAkinobu Mita task->make_it_fail = make_it_fail; 1167f4f154fdSAkinobu Mita put_task_struct(task); 1168f4f154fdSAkinobu Mita if (end - buffer == 0) 1169f4f154fdSAkinobu Mita return -EIO; 1170f4f154fdSAkinobu Mita return end - buffer; 1171f4f154fdSAkinobu Mita } 1172f4f154fdSAkinobu Mita 117300977a59SArjan van de Ven static const struct file_operations proc_fault_inject_operations = { 1174f4f154fdSAkinobu Mita .read = proc_fault_inject_read, 1175f4f154fdSAkinobu Mita .write = proc_fault_inject_write, 1176f4f154fdSAkinobu Mita }; 1177f4f154fdSAkinobu Mita #endif 1178f4f154fdSAkinobu Mita 11799745512cSArjan van de Ven 118043ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG 118143ae34cbSIngo Molnar /* 118243ae34cbSIngo Molnar * Print out various scheduling related per-task fields: 118343ae34cbSIngo Molnar */ 118443ae34cbSIngo Molnar static int sched_show(struct seq_file *m, void *v) 118543ae34cbSIngo Molnar { 118643ae34cbSIngo Molnar struct inode *inode = m->private; 118743ae34cbSIngo Molnar struct task_struct *p; 118843ae34cbSIngo Molnar 118943ae34cbSIngo Molnar WARN_ON(!inode); 119043ae34cbSIngo Molnar 119143ae34cbSIngo Molnar p = get_proc_task(inode); 119243ae34cbSIngo Molnar if (!p) 119343ae34cbSIngo Molnar return -ESRCH; 119443ae34cbSIngo Molnar proc_sched_show_task(p, m); 119543ae34cbSIngo Molnar 119643ae34cbSIngo Molnar put_task_struct(p); 119743ae34cbSIngo Molnar 119843ae34cbSIngo Molnar return 0; 119943ae34cbSIngo Molnar } 120043ae34cbSIngo Molnar 120143ae34cbSIngo Molnar static ssize_t 120243ae34cbSIngo Molnar sched_write(struct file *file, const char __user *buf, 120343ae34cbSIngo Molnar size_t count, loff_t *offset) 120443ae34cbSIngo Molnar { 120543ae34cbSIngo Molnar struct inode *inode = file->f_path.dentry->d_inode; 120643ae34cbSIngo Molnar struct task_struct *p; 120743ae34cbSIngo Molnar 120843ae34cbSIngo Molnar WARN_ON(!inode); 120943ae34cbSIngo Molnar 121043ae34cbSIngo Molnar p = get_proc_task(inode); 121143ae34cbSIngo Molnar if (!p) 121243ae34cbSIngo Molnar return -ESRCH; 121343ae34cbSIngo Molnar proc_sched_set_task(p); 121443ae34cbSIngo Molnar 121543ae34cbSIngo Molnar put_task_struct(p); 121643ae34cbSIngo Molnar 121743ae34cbSIngo Molnar return count; 121843ae34cbSIngo Molnar } 121943ae34cbSIngo Molnar 122043ae34cbSIngo Molnar static int sched_open(struct inode *inode, struct file *filp) 122143ae34cbSIngo Molnar { 122243ae34cbSIngo Molnar int ret; 122343ae34cbSIngo Molnar 122443ae34cbSIngo Molnar ret = single_open(filp, sched_show, NULL); 122543ae34cbSIngo Molnar if (!ret) { 122643ae34cbSIngo Molnar struct seq_file *m = filp->private_data; 122743ae34cbSIngo Molnar 122843ae34cbSIngo Molnar m->private = inode; 122943ae34cbSIngo Molnar } 123043ae34cbSIngo Molnar return ret; 123143ae34cbSIngo Molnar } 123243ae34cbSIngo Molnar 123343ae34cbSIngo Molnar static const struct file_operations proc_pid_sched_operations = { 123443ae34cbSIngo Molnar .open = sched_open, 123543ae34cbSIngo Molnar .read = seq_read, 123643ae34cbSIngo Molnar .write = sched_write, 123743ae34cbSIngo Molnar .llseek = seq_lseek, 12385ea473a1SAlexey Dobriyan .release = single_release, 123943ae34cbSIngo Molnar }; 124043ae34cbSIngo Molnar 124143ae34cbSIngo Molnar #endif 124243ae34cbSIngo Molnar 1243925d1c40SMatt Helsley /* 1244925d1c40SMatt Helsley * We added or removed a vma mapping the executable. The vmas are only mapped 1245925d1c40SMatt Helsley * during exec and are not mapped with the mmap system call. 1246925d1c40SMatt Helsley * Callers must hold down_write() on the mm's mmap_sem for these 1247925d1c40SMatt Helsley */ 1248925d1c40SMatt Helsley void added_exe_file_vma(struct mm_struct *mm) 1249925d1c40SMatt Helsley { 1250925d1c40SMatt Helsley mm->num_exe_file_vmas++; 1251925d1c40SMatt Helsley } 1252925d1c40SMatt Helsley 1253925d1c40SMatt Helsley void removed_exe_file_vma(struct mm_struct *mm) 1254925d1c40SMatt Helsley { 1255925d1c40SMatt Helsley mm->num_exe_file_vmas--; 1256925d1c40SMatt Helsley if ((mm->num_exe_file_vmas == 0) && mm->exe_file){ 1257925d1c40SMatt Helsley fput(mm->exe_file); 1258925d1c40SMatt Helsley mm->exe_file = NULL; 1259925d1c40SMatt Helsley } 1260925d1c40SMatt Helsley 1261925d1c40SMatt Helsley } 1262925d1c40SMatt Helsley 1263925d1c40SMatt Helsley void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file) 1264925d1c40SMatt Helsley { 1265925d1c40SMatt Helsley if (new_exe_file) 1266925d1c40SMatt Helsley get_file(new_exe_file); 1267925d1c40SMatt Helsley if (mm->exe_file) 1268925d1c40SMatt Helsley fput(mm->exe_file); 1269925d1c40SMatt Helsley mm->exe_file = new_exe_file; 1270925d1c40SMatt Helsley mm->num_exe_file_vmas = 0; 1271925d1c40SMatt Helsley } 1272925d1c40SMatt Helsley 1273925d1c40SMatt Helsley struct file *get_mm_exe_file(struct mm_struct *mm) 1274925d1c40SMatt Helsley { 1275925d1c40SMatt Helsley struct file *exe_file; 1276925d1c40SMatt Helsley 1277925d1c40SMatt Helsley /* We need mmap_sem to protect against races with removal of 1278925d1c40SMatt Helsley * VM_EXECUTABLE vmas */ 1279925d1c40SMatt Helsley down_read(&mm->mmap_sem); 1280925d1c40SMatt Helsley exe_file = mm->exe_file; 1281925d1c40SMatt Helsley if (exe_file) 1282925d1c40SMatt Helsley get_file(exe_file); 1283925d1c40SMatt Helsley up_read(&mm->mmap_sem); 1284925d1c40SMatt Helsley return exe_file; 1285925d1c40SMatt Helsley } 1286925d1c40SMatt Helsley 1287925d1c40SMatt Helsley void dup_mm_exe_file(struct mm_struct *oldmm, struct mm_struct *newmm) 1288925d1c40SMatt Helsley { 1289925d1c40SMatt Helsley /* It's safe to write the exe_file pointer without exe_file_lock because 1290925d1c40SMatt Helsley * this is called during fork when the task is not yet in /proc */ 1291925d1c40SMatt Helsley newmm->exe_file = get_mm_exe_file(oldmm); 1292925d1c40SMatt Helsley } 1293925d1c40SMatt Helsley 1294925d1c40SMatt Helsley static int proc_exe_link(struct inode *inode, struct path *exe_path) 1295925d1c40SMatt Helsley { 1296925d1c40SMatt Helsley struct task_struct *task; 1297925d1c40SMatt Helsley struct mm_struct *mm; 1298925d1c40SMatt Helsley struct file *exe_file; 1299925d1c40SMatt Helsley 1300925d1c40SMatt Helsley task = get_proc_task(inode); 1301925d1c40SMatt Helsley if (!task) 1302925d1c40SMatt Helsley return -ENOENT; 1303925d1c40SMatt Helsley mm = get_task_mm(task); 1304925d1c40SMatt Helsley put_task_struct(task); 1305925d1c40SMatt Helsley if (!mm) 1306925d1c40SMatt Helsley return -ENOENT; 1307925d1c40SMatt Helsley exe_file = get_mm_exe_file(mm); 1308925d1c40SMatt Helsley mmput(mm); 1309925d1c40SMatt Helsley if (exe_file) { 1310925d1c40SMatt Helsley *exe_path = exe_file->f_path; 1311925d1c40SMatt Helsley path_get(&exe_file->f_path); 1312925d1c40SMatt Helsley fput(exe_file); 1313925d1c40SMatt Helsley return 0; 1314925d1c40SMatt Helsley } else 1315925d1c40SMatt Helsley return -ENOENT; 1316925d1c40SMatt Helsley } 1317925d1c40SMatt Helsley 1318008b150aSAl Viro static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd) 13191da177e4SLinus Torvalds { 13201da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 13211da177e4SLinus Torvalds int error = -EACCES; 13221da177e4SLinus Torvalds 13231da177e4SLinus Torvalds /* We don't need a base pointer in the /proc filesystem */ 13241d957f9bSJan Blunck path_put(&nd->path); 13251da177e4SLinus Torvalds 1326778c1144SEric W. Biederman /* Are we allowed to snoop on the tasks file descriptors? */ 1327778c1144SEric W. Biederman if (!proc_fd_access_allowed(inode)) 13281da177e4SLinus Torvalds goto out; 13291da177e4SLinus Torvalds 13303dcd25f3SJan Blunck error = PROC_I(inode)->op.proc_get_link(inode, &nd->path); 13311da177e4SLinus Torvalds nd->last_type = LAST_BIND; 13321da177e4SLinus Torvalds out: 1333008b150aSAl Viro return ERR_PTR(error); 13341da177e4SLinus Torvalds } 13351da177e4SLinus Torvalds 13363dcd25f3SJan Blunck static int do_proc_readlink(struct path *path, char __user *buffer, int buflen) 13371da177e4SLinus Torvalds { 1338e12ba74dSMel Gorman char *tmp = (char*)__get_free_page(GFP_TEMPORARY); 13393dcd25f3SJan Blunck char *pathname; 13401da177e4SLinus Torvalds int len; 13411da177e4SLinus Torvalds 13421da177e4SLinus Torvalds if (!tmp) 13431da177e4SLinus Torvalds return -ENOMEM; 13441da177e4SLinus Torvalds 1345cf28b486SJan Blunck pathname = d_path(path, tmp, PAGE_SIZE); 13463dcd25f3SJan Blunck len = PTR_ERR(pathname); 13473dcd25f3SJan Blunck if (IS_ERR(pathname)) 13481da177e4SLinus Torvalds goto out; 13493dcd25f3SJan Blunck len = tmp + PAGE_SIZE - 1 - pathname; 13501da177e4SLinus Torvalds 13511da177e4SLinus Torvalds if (len > buflen) 13521da177e4SLinus Torvalds len = buflen; 13533dcd25f3SJan Blunck if (copy_to_user(buffer, pathname, len)) 13541da177e4SLinus Torvalds len = -EFAULT; 13551da177e4SLinus Torvalds out: 13561da177e4SLinus Torvalds free_page((unsigned long)tmp); 13571da177e4SLinus Torvalds return len; 13581da177e4SLinus Torvalds } 13591da177e4SLinus Torvalds 13601da177e4SLinus Torvalds static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen) 13611da177e4SLinus Torvalds { 13621da177e4SLinus Torvalds int error = -EACCES; 13631da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 13643dcd25f3SJan Blunck struct path path; 13651da177e4SLinus Torvalds 1366778c1144SEric W. Biederman /* Are we allowed to snoop on the tasks file descriptors? */ 1367778c1144SEric W. Biederman if (!proc_fd_access_allowed(inode)) 13681da177e4SLinus Torvalds goto out; 13691da177e4SLinus Torvalds 13703dcd25f3SJan Blunck error = PROC_I(inode)->op.proc_get_link(inode, &path); 13711da177e4SLinus Torvalds if (error) 13721da177e4SLinus Torvalds goto out; 13731da177e4SLinus Torvalds 13743dcd25f3SJan Blunck error = do_proc_readlink(&path, buffer, buflen); 13753dcd25f3SJan Blunck path_put(&path); 13761da177e4SLinus Torvalds out: 13771da177e4SLinus Torvalds return error; 13781da177e4SLinus Torvalds } 13791da177e4SLinus Torvalds 1380c5ef1c42SArjan van de Ven static const struct inode_operations proc_pid_link_inode_operations = { 13811da177e4SLinus Torvalds .readlink = proc_pid_readlink, 13826d76fa58SLinus Torvalds .follow_link = proc_pid_follow_link, 13836d76fa58SLinus Torvalds .setattr = proc_setattr, 13841da177e4SLinus Torvalds }; 13851da177e4SLinus Torvalds 138628a6d671SEric W. Biederman 138728a6d671SEric W. Biederman /* building an inode */ 138828a6d671SEric W. Biederman 138928a6d671SEric W. Biederman static int task_dumpable(struct task_struct *task) 139028a6d671SEric W. Biederman { 139128a6d671SEric W. Biederman int dumpable = 0; 139228a6d671SEric W. Biederman struct mm_struct *mm; 139328a6d671SEric W. Biederman 139428a6d671SEric W. Biederman task_lock(task); 139528a6d671SEric W. Biederman mm = task->mm; 139628a6d671SEric W. Biederman if (mm) 13976c5d5238SKawai, Hidehiro dumpable = get_dumpable(mm); 139828a6d671SEric W. Biederman task_unlock(task); 139928a6d671SEric W. Biederman if(dumpable == 1) 140028a6d671SEric W. Biederman return 1; 140128a6d671SEric W. Biederman return 0; 140228a6d671SEric W. Biederman } 140328a6d671SEric W. Biederman 140428a6d671SEric W. Biederman 140561a28784SEric W. Biederman static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task) 140628a6d671SEric W. Biederman { 140728a6d671SEric W. Biederman struct inode * inode; 140828a6d671SEric W. Biederman struct proc_inode *ei; 1409*c69e8d9cSDavid Howells const struct cred *cred; 141028a6d671SEric W. Biederman 141128a6d671SEric W. Biederman /* We need a new inode */ 141228a6d671SEric W. Biederman 141328a6d671SEric W. Biederman inode = new_inode(sb); 141428a6d671SEric W. Biederman if (!inode) 141528a6d671SEric W. Biederman goto out; 141628a6d671SEric W. Biederman 141728a6d671SEric W. Biederman /* Common stuff */ 141828a6d671SEric W. Biederman ei = PROC_I(inode); 141928a6d671SEric W. Biederman inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; 142028a6d671SEric W. Biederman inode->i_op = &proc_def_inode_operations; 142128a6d671SEric W. Biederman 142228a6d671SEric W. Biederman /* 142328a6d671SEric W. Biederman * grab the reference to task. 142428a6d671SEric W. Biederman */ 14251a657f78SOleg Nesterov ei->pid = get_task_pid(task, PIDTYPE_PID); 142628a6d671SEric W. Biederman if (!ei->pid) 142728a6d671SEric W. Biederman goto out_unlock; 142828a6d671SEric W. Biederman 142928a6d671SEric W. Biederman inode->i_uid = 0; 143028a6d671SEric W. Biederman inode->i_gid = 0; 143128a6d671SEric W. Biederman if (task_dumpable(task)) { 1432*c69e8d9cSDavid Howells rcu_read_lock(); 1433*c69e8d9cSDavid Howells cred = __task_cred(task); 1434*c69e8d9cSDavid Howells inode->i_uid = cred->euid; 1435*c69e8d9cSDavid Howells inode->i_gid = cred->egid; 1436*c69e8d9cSDavid Howells rcu_read_unlock(); 143728a6d671SEric W. Biederman } 143828a6d671SEric W. Biederman security_task_to_inode(task, inode); 143928a6d671SEric W. Biederman 144028a6d671SEric W. Biederman out: 144128a6d671SEric W. Biederman return inode; 144228a6d671SEric W. Biederman 144328a6d671SEric W. Biederman out_unlock: 144428a6d671SEric W. Biederman iput(inode); 144528a6d671SEric W. Biederman return NULL; 144628a6d671SEric W. Biederman } 144728a6d671SEric W. Biederman 144828a6d671SEric W. Biederman static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) 144928a6d671SEric W. Biederman { 145028a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 145128a6d671SEric W. Biederman struct task_struct *task; 1452*c69e8d9cSDavid Howells const struct cred *cred; 1453*c69e8d9cSDavid Howells 145428a6d671SEric W. Biederman generic_fillattr(inode, stat); 145528a6d671SEric W. Biederman 145628a6d671SEric W. Biederman rcu_read_lock(); 145728a6d671SEric W. Biederman stat->uid = 0; 145828a6d671SEric W. Biederman stat->gid = 0; 145928a6d671SEric W. Biederman task = pid_task(proc_pid(inode), PIDTYPE_PID); 146028a6d671SEric W. Biederman if (task) { 146128a6d671SEric W. Biederman if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) || 146228a6d671SEric W. Biederman task_dumpable(task)) { 1463*c69e8d9cSDavid Howells cred = __task_cred(task); 1464*c69e8d9cSDavid Howells stat->uid = cred->euid; 1465*c69e8d9cSDavid Howells stat->gid = cred->egid; 146628a6d671SEric W. Biederman } 146728a6d671SEric W. Biederman } 146828a6d671SEric W. Biederman rcu_read_unlock(); 146928a6d671SEric W. Biederman return 0; 147028a6d671SEric W. Biederman } 147128a6d671SEric W. Biederman 147228a6d671SEric W. Biederman /* dentry stuff */ 147328a6d671SEric W. Biederman 147428a6d671SEric W. Biederman /* 147528a6d671SEric W. Biederman * Exceptional case: normally we are not allowed to unhash a busy 147628a6d671SEric W. Biederman * directory. In this case, however, we can do it - no aliasing problems 147728a6d671SEric W. Biederman * due to the way we treat inodes. 147828a6d671SEric W. Biederman * 147928a6d671SEric W. Biederman * Rewrite the inode's ownerships here because the owning task may have 148028a6d671SEric W. Biederman * performed a setuid(), etc. 148128a6d671SEric W. Biederman * 148228a6d671SEric W. Biederman * Before the /proc/pid/status file was created the only way to read 148328a6d671SEric W. Biederman * the effective uid of a /process was to stat /proc/pid. Reading 148428a6d671SEric W. Biederman * /proc/pid/status is slow enough that procps and other packages 148528a6d671SEric W. Biederman * kept stating /proc/pid. To keep the rules in /proc simple I have 148628a6d671SEric W. Biederman * made this apply to all per process world readable and executable 148728a6d671SEric W. Biederman * directories. 148828a6d671SEric W. Biederman */ 148928a6d671SEric W. Biederman static int pid_revalidate(struct dentry *dentry, struct nameidata *nd) 149028a6d671SEric W. Biederman { 149128a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 149228a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 1493*c69e8d9cSDavid Howells const struct cred *cred; 1494*c69e8d9cSDavid Howells 149528a6d671SEric W. Biederman if (task) { 149628a6d671SEric W. Biederman if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) || 149728a6d671SEric W. Biederman task_dumpable(task)) { 1498*c69e8d9cSDavid Howells rcu_read_lock(); 1499*c69e8d9cSDavid Howells cred = __task_cred(task); 1500*c69e8d9cSDavid Howells inode->i_uid = cred->euid; 1501*c69e8d9cSDavid Howells inode->i_gid = cred->egid; 1502*c69e8d9cSDavid Howells rcu_read_unlock(); 150328a6d671SEric W. Biederman } else { 150428a6d671SEric W. Biederman inode->i_uid = 0; 150528a6d671SEric W. Biederman inode->i_gid = 0; 150628a6d671SEric W. Biederman } 150728a6d671SEric W. Biederman inode->i_mode &= ~(S_ISUID | S_ISGID); 150828a6d671SEric W. Biederman security_task_to_inode(task, inode); 150928a6d671SEric W. Biederman put_task_struct(task); 151028a6d671SEric W. Biederman return 1; 151128a6d671SEric W. Biederman } 151228a6d671SEric W. Biederman d_drop(dentry); 151328a6d671SEric W. Biederman return 0; 151428a6d671SEric W. Biederman } 151528a6d671SEric W. Biederman 151628a6d671SEric W. Biederman static int pid_delete_dentry(struct dentry * dentry) 151728a6d671SEric W. Biederman { 151828a6d671SEric W. Biederman /* Is the task we represent dead? 151928a6d671SEric W. Biederman * If so, then don't put the dentry on the lru list, 152028a6d671SEric W. Biederman * kill it immediately. 152128a6d671SEric W. Biederman */ 152228a6d671SEric W. Biederman return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first; 152328a6d671SEric W. Biederman } 152428a6d671SEric W. Biederman 152528a6d671SEric W. Biederman static struct dentry_operations pid_dentry_operations = 152628a6d671SEric W. Biederman { 152728a6d671SEric W. Biederman .d_revalidate = pid_revalidate, 152828a6d671SEric W. Biederman .d_delete = pid_delete_dentry, 152928a6d671SEric W. Biederman }; 153028a6d671SEric W. Biederman 153128a6d671SEric W. Biederman /* Lookups */ 153228a6d671SEric W. Biederman 1533c5141e6dSEric Dumazet typedef struct dentry *instantiate_t(struct inode *, struct dentry *, 1534c5141e6dSEric Dumazet struct task_struct *, const void *); 153561a28784SEric W. Biederman 15361c0d04c9SEric W. Biederman /* 15371c0d04c9SEric W. Biederman * Fill a directory entry. 15381c0d04c9SEric W. Biederman * 15391c0d04c9SEric W. Biederman * If possible create the dcache entry and derive our inode number and 15401c0d04c9SEric W. Biederman * file type from dcache entry. 15411c0d04c9SEric W. Biederman * 15421c0d04c9SEric W. Biederman * Since all of the proc inode numbers are dynamically generated, the inode 15431c0d04c9SEric W. Biederman * numbers do not exist until the inode is cache. This means creating the 15441c0d04c9SEric W. Biederman * the dcache entry in readdir is necessary to keep the inode numbers 15451c0d04c9SEric W. Biederman * reported by readdir in sync with the inode numbers reported 15461c0d04c9SEric W. Biederman * by stat. 15471c0d04c9SEric W. Biederman */ 154861a28784SEric W. Biederman static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir, 154961a28784SEric W. Biederman char *name, int len, 1550c5141e6dSEric Dumazet instantiate_t instantiate, struct task_struct *task, const void *ptr) 155161a28784SEric W. Biederman { 15522fddfeefSJosef "Jeff" Sipek struct dentry *child, *dir = filp->f_path.dentry; 155361a28784SEric W. Biederman struct inode *inode; 155461a28784SEric W. Biederman struct qstr qname; 155561a28784SEric W. Biederman ino_t ino = 0; 155661a28784SEric W. Biederman unsigned type = DT_UNKNOWN; 155761a28784SEric W. Biederman 155861a28784SEric W. Biederman qname.name = name; 155961a28784SEric W. Biederman qname.len = len; 156061a28784SEric W. Biederman qname.hash = full_name_hash(name, len); 156161a28784SEric W. Biederman 156261a28784SEric W. Biederman child = d_lookup(dir, &qname); 156361a28784SEric W. Biederman if (!child) { 156461a28784SEric W. Biederman struct dentry *new; 156561a28784SEric W. Biederman new = d_alloc(dir, &qname); 156661a28784SEric W. Biederman if (new) { 156761a28784SEric W. Biederman child = instantiate(dir->d_inode, new, task, ptr); 156861a28784SEric W. Biederman if (child) 156961a28784SEric W. Biederman dput(new); 157061a28784SEric W. Biederman else 157161a28784SEric W. Biederman child = new; 157261a28784SEric W. Biederman } 157361a28784SEric W. Biederman } 157461a28784SEric W. Biederman if (!child || IS_ERR(child) || !child->d_inode) 157561a28784SEric W. Biederman goto end_instantiate; 157661a28784SEric W. Biederman inode = child->d_inode; 157761a28784SEric W. Biederman if (inode) { 157861a28784SEric W. Biederman ino = inode->i_ino; 157961a28784SEric W. Biederman type = inode->i_mode >> 12; 158061a28784SEric W. Biederman } 158161a28784SEric W. Biederman dput(child); 158261a28784SEric W. Biederman end_instantiate: 158361a28784SEric W. Biederman if (!ino) 158461a28784SEric W. Biederman ino = find_inode_number(dir, &qname); 158561a28784SEric W. Biederman if (!ino) 158661a28784SEric W. Biederman ino = 1; 158761a28784SEric W. Biederman return filldir(dirent, name, len, filp->f_pos, ino, type); 158861a28784SEric W. Biederman } 158961a28784SEric W. Biederman 159028a6d671SEric W. Biederman static unsigned name_to_int(struct dentry *dentry) 159128a6d671SEric W. Biederman { 159228a6d671SEric W. Biederman const char *name = dentry->d_name.name; 159328a6d671SEric W. Biederman int len = dentry->d_name.len; 159428a6d671SEric W. Biederman unsigned n = 0; 159528a6d671SEric W. Biederman 159628a6d671SEric W. Biederman if (len > 1 && *name == '0') 159728a6d671SEric W. Biederman goto out; 159828a6d671SEric W. Biederman while (len-- > 0) { 159928a6d671SEric W. Biederman unsigned c = *name++ - '0'; 160028a6d671SEric W. Biederman if (c > 9) 160128a6d671SEric W. Biederman goto out; 160228a6d671SEric W. Biederman if (n >= (~0U-9)/10) 160328a6d671SEric W. Biederman goto out; 160428a6d671SEric W. Biederman n *= 10; 160528a6d671SEric W. Biederman n += c; 160628a6d671SEric W. Biederman } 160728a6d671SEric W. Biederman return n; 160828a6d671SEric W. Biederman out: 160928a6d671SEric W. Biederman return ~0U; 161028a6d671SEric W. Biederman } 161128a6d671SEric W. Biederman 161227932742SMiklos Szeredi #define PROC_FDINFO_MAX 64 161327932742SMiklos Szeredi 16143dcd25f3SJan Blunck static int proc_fd_info(struct inode *inode, struct path *path, char *info) 161528a6d671SEric W. Biederman { 161628a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 161728a6d671SEric W. Biederman struct files_struct *files = NULL; 161828a6d671SEric W. Biederman struct file *file; 161928a6d671SEric W. Biederman int fd = proc_fd(inode); 162028a6d671SEric W. Biederman 162128a6d671SEric W. Biederman if (task) { 162228a6d671SEric W. Biederman files = get_files_struct(task); 162328a6d671SEric W. Biederman put_task_struct(task); 162428a6d671SEric W. Biederman } 162528a6d671SEric W. Biederman if (files) { 162628a6d671SEric W. Biederman /* 162728a6d671SEric W. Biederman * We are not taking a ref to the file structure, so we must 162828a6d671SEric W. Biederman * hold ->file_lock. 162928a6d671SEric W. Biederman */ 163028a6d671SEric W. Biederman spin_lock(&files->file_lock); 163128a6d671SEric W. Biederman file = fcheck_files(files, fd); 163228a6d671SEric W. Biederman if (file) { 16333dcd25f3SJan Blunck if (path) { 16343dcd25f3SJan Blunck *path = file->f_path; 16353dcd25f3SJan Blunck path_get(&file->f_path); 16363dcd25f3SJan Blunck } 163727932742SMiklos Szeredi if (info) 163827932742SMiklos Szeredi snprintf(info, PROC_FDINFO_MAX, 163927932742SMiklos Szeredi "pos:\t%lli\n" 164027932742SMiklos Szeredi "flags:\t0%o\n", 164127932742SMiklos Szeredi (long long) file->f_pos, 164227932742SMiklos Szeredi file->f_flags); 164328a6d671SEric W. Biederman spin_unlock(&files->file_lock); 164428a6d671SEric W. Biederman put_files_struct(files); 164528a6d671SEric W. Biederman return 0; 164628a6d671SEric W. Biederman } 164728a6d671SEric W. Biederman spin_unlock(&files->file_lock); 164828a6d671SEric W. Biederman put_files_struct(files); 164928a6d671SEric W. Biederman } 165028a6d671SEric W. Biederman return -ENOENT; 165128a6d671SEric W. Biederman } 165228a6d671SEric W. Biederman 16533dcd25f3SJan Blunck static int proc_fd_link(struct inode *inode, struct path *path) 165427932742SMiklos Szeredi { 16553dcd25f3SJan Blunck return proc_fd_info(inode, path, NULL); 165627932742SMiklos Szeredi } 165727932742SMiklos Szeredi 165828a6d671SEric W. Biederman static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd) 165928a6d671SEric W. Biederman { 166028a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 166128a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 166228a6d671SEric W. Biederman int fd = proc_fd(inode); 166328a6d671SEric W. Biederman struct files_struct *files; 1664*c69e8d9cSDavid Howells const struct cred *cred; 166528a6d671SEric W. Biederman 166628a6d671SEric W. Biederman if (task) { 166728a6d671SEric W. Biederman files = get_files_struct(task); 166828a6d671SEric W. Biederman if (files) { 166928a6d671SEric W. Biederman rcu_read_lock(); 167028a6d671SEric W. Biederman if (fcheck_files(files, fd)) { 167128a6d671SEric W. Biederman rcu_read_unlock(); 167228a6d671SEric W. Biederman put_files_struct(files); 167328a6d671SEric W. Biederman if (task_dumpable(task)) { 1674*c69e8d9cSDavid Howells rcu_read_lock(); 1675*c69e8d9cSDavid Howells cred = __task_cred(task); 1676*c69e8d9cSDavid Howells inode->i_uid = cred->euid; 1677*c69e8d9cSDavid Howells inode->i_gid = cred->egid; 1678*c69e8d9cSDavid Howells rcu_read_unlock(); 167928a6d671SEric W. Biederman } else { 168028a6d671SEric W. Biederman inode->i_uid = 0; 168128a6d671SEric W. Biederman inode->i_gid = 0; 168228a6d671SEric W. Biederman } 168328a6d671SEric W. Biederman inode->i_mode &= ~(S_ISUID | S_ISGID); 168428a6d671SEric W. Biederman security_task_to_inode(task, inode); 168528a6d671SEric W. Biederman put_task_struct(task); 168628a6d671SEric W. Biederman return 1; 168728a6d671SEric W. Biederman } 168828a6d671SEric W. Biederman rcu_read_unlock(); 168928a6d671SEric W. Biederman put_files_struct(files); 169028a6d671SEric W. Biederman } 169128a6d671SEric W. Biederman put_task_struct(task); 169228a6d671SEric W. Biederman } 169328a6d671SEric W. Biederman d_drop(dentry); 169428a6d671SEric W. Biederman return 0; 169528a6d671SEric W. Biederman } 169628a6d671SEric W. Biederman 169728a6d671SEric W. Biederman static struct dentry_operations tid_fd_dentry_operations = 169828a6d671SEric W. Biederman { 169928a6d671SEric W. Biederman .d_revalidate = tid_fd_revalidate, 170028a6d671SEric W. Biederman .d_delete = pid_delete_dentry, 170128a6d671SEric W. Biederman }; 170228a6d671SEric W. Biederman 1703444ceed8SEric W. Biederman static struct dentry *proc_fd_instantiate(struct inode *dir, 1704c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 170528a6d671SEric W. Biederman { 1706c5141e6dSEric Dumazet unsigned fd = *(const unsigned *)ptr; 170728a6d671SEric W. Biederman struct file *file; 170828a6d671SEric W. Biederman struct files_struct *files; 170928a6d671SEric W. Biederman struct inode *inode; 171028a6d671SEric W. Biederman struct proc_inode *ei; 1711444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-ENOENT); 171228a6d671SEric W. Biederman 171361a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 171428a6d671SEric W. Biederman if (!inode) 171528a6d671SEric W. Biederman goto out; 171628a6d671SEric W. Biederman ei = PROC_I(inode); 171728a6d671SEric W. Biederman ei->fd = fd; 171828a6d671SEric W. Biederman files = get_files_struct(task); 171928a6d671SEric W. Biederman if (!files) 1720444ceed8SEric W. Biederman goto out_iput; 172128a6d671SEric W. Biederman inode->i_mode = S_IFLNK; 172228a6d671SEric W. Biederman 172328a6d671SEric W. Biederman /* 172428a6d671SEric W. Biederman * We are not taking a ref to the file structure, so we must 172528a6d671SEric W. Biederman * hold ->file_lock. 172628a6d671SEric W. Biederman */ 172728a6d671SEric W. Biederman spin_lock(&files->file_lock); 172828a6d671SEric W. Biederman file = fcheck_files(files, fd); 172928a6d671SEric W. Biederman if (!file) 1730444ceed8SEric W. Biederman goto out_unlock; 1731aeb5d727SAl Viro if (file->f_mode & FMODE_READ) 173228a6d671SEric W. Biederman inode->i_mode |= S_IRUSR | S_IXUSR; 1733aeb5d727SAl Viro if (file->f_mode & FMODE_WRITE) 173428a6d671SEric W. Biederman inode->i_mode |= S_IWUSR | S_IXUSR; 173528a6d671SEric W. Biederman spin_unlock(&files->file_lock); 173628a6d671SEric W. Biederman put_files_struct(files); 1737444ceed8SEric W. Biederman 173828a6d671SEric W. Biederman inode->i_op = &proc_pid_link_inode_operations; 173928a6d671SEric W. Biederman inode->i_size = 64; 174028a6d671SEric W. Biederman ei->op.proc_get_link = proc_fd_link; 174128a6d671SEric W. Biederman dentry->d_op = &tid_fd_dentry_operations; 174228a6d671SEric W. Biederman d_add(dentry, inode); 174328a6d671SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 174428a6d671SEric W. Biederman if (tid_fd_revalidate(dentry, NULL)) 1745444ceed8SEric W. Biederman error = NULL; 1746444ceed8SEric W. Biederman 1747444ceed8SEric W. Biederman out: 1748444ceed8SEric W. Biederman return error; 1749444ceed8SEric W. Biederman out_unlock: 1750444ceed8SEric W. Biederman spin_unlock(&files->file_lock); 1751444ceed8SEric W. Biederman put_files_struct(files); 1752444ceed8SEric W. Biederman out_iput: 1753444ceed8SEric W. Biederman iput(inode); 1754444ceed8SEric W. Biederman goto out; 1755444ceed8SEric W. Biederman } 1756444ceed8SEric W. Biederman 175727932742SMiklos Szeredi static struct dentry *proc_lookupfd_common(struct inode *dir, 175827932742SMiklos Szeredi struct dentry *dentry, 175927932742SMiklos Szeredi instantiate_t instantiate) 1760444ceed8SEric W. Biederman { 1761444ceed8SEric W. Biederman struct task_struct *task = get_proc_task(dir); 1762444ceed8SEric W. Biederman unsigned fd = name_to_int(dentry); 1763444ceed8SEric W. Biederman struct dentry *result = ERR_PTR(-ENOENT); 1764444ceed8SEric W. Biederman 1765444ceed8SEric W. Biederman if (!task) 1766444ceed8SEric W. Biederman goto out_no_task; 1767444ceed8SEric W. Biederman if (fd == ~0U) 1768444ceed8SEric W. Biederman goto out; 1769444ceed8SEric W. Biederman 177027932742SMiklos Szeredi result = instantiate(dir, dentry, task, &fd); 177128a6d671SEric W. Biederman out: 177228a6d671SEric W. Biederman put_task_struct(task); 177328a6d671SEric W. Biederman out_no_task: 177428a6d671SEric W. Biederman return result; 177528a6d671SEric W. Biederman } 177628a6d671SEric W. Biederman 177727932742SMiklos Szeredi static int proc_readfd_common(struct file * filp, void * dirent, 177827932742SMiklos Szeredi filldir_t filldir, instantiate_t instantiate) 17791da177e4SLinus Torvalds { 17802fddfeefSJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 17815634708bSEric W. Biederman struct inode *inode = dentry->d_inode; 178299f89551SEric W. Biederman struct task_struct *p = get_proc_task(inode); 1783457c2510SPavel Emelyanov unsigned int fd, ino; 17841da177e4SLinus Torvalds int retval; 17851da177e4SLinus Torvalds struct files_struct * files; 17861da177e4SLinus Torvalds 17871da177e4SLinus Torvalds retval = -ENOENT; 178899f89551SEric W. Biederman if (!p) 178999f89551SEric W. Biederman goto out_no_task; 17901da177e4SLinus Torvalds retval = 0; 17911da177e4SLinus Torvalds 17921da177e4SLinus Torvalds fd = filp->f_pos; 17931da177e4SLinus Torvalds switch (fd) { 17941da177e4SLinus Torvalds case 0: 17951da177e4SLinus Torvalds if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0) 17961da177e4SLinus Torvalds goto out; 17971da177e4SLinus Torvalds filp->f_pos++; 17981da177e4SLinus Torvalds case 1: 17995634708bSEric W. Biederman ino = parent_ino(dentry); 18001da177e4SLinus Torvalds if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0) 18011da177e4SLinus Torvalds goto out; 18021da177e4SLinus Torvalds filp->f_pos++; 18031da177e4SLinus Torvalds default: 18041da177e4SLinus Torvalds files = get_files_struct(p); 18051da177e4SLinus Torvalds if (!files) 18061da177e4SLinus Torvalds goto out; 1807b835996fSDipankar Sarma rcu_read_lock(); 18081da177e4SLinus Torvalds for (fd = filp->f_pos-2; 18099b4f526cSAl Viro fd < files_fdtable(files)->max_fds; 18101da177e4SLinus Torvalds fd++, filp->f_pos++) { 181127932742SMiklos Szeredi char name[PROC_NUMBUF]; 181227932742SMiklos Szeredi int len; 18131da177e4SLinus Torvalds 18141da177e4SLinus Torvalds if (!fcheck_files(files, fd)) 18151da177e4SLinus Torvalds continue; 1816b835996fSDipankar Sarma rcu_read_unlock(); 18171da177e4SLinus Torvalds 181827932742SMiklos Szeredi len = snprintf(name, sizeof(name), "%d", fd); 181927932742SMiklos Szeredi if (proc_fill_cache(filp, dirent, filldir, 182027932742SMiklos Szeredi name, len, instantiate, 182127932742SMiklos Szeredi p, &fd) < 0) { 1822b835996fSDipankar Sarma rcu_read_lock(); 18231da177e4SLinus Torvalds break; 18241da177e4SLinus Torvalds } 1825b835996fSDipankar Sarma rcu_read_lock(); 18261da177e4SLinus Torvalds } 1827b835996fSDipankar Sarma rcu_read_unlock(); 18281da177e4SLinus Torvalds put_files_struct(files); 18291da177e4SLinus Torvalds } 18301da177e4SLinus Torvalds out: 183199f89551SEric W. Biederman put_task_struct(p); 183299f89551SEric W. Biederman out_no_task: 18331da177e4SLinus Torvalds return retval; 18341da177e4SLinus Torvalds } 18351da177e4SLinus Torvalds 183627932742SMiklos Szeredi static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry, 183727932742SMiklos Szeredi struct nameidata *nd) 183827932742SMiklos Szeredi { 183927932742SMiklos Szeredi return proc_lookupfd_common(dir, dentry, proc_fd_instantiate); 184027932742SMiklos Szeredi } 184127932742SMiklos Szeredi 184227932742SMiklos Szeredi static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir) 184327932742SMiklos Szeredi { 184427932742SMiklos Szeredi return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate); 184527932742SMiklos Szeredi } 184627932742SMiklos Szeredi 184727932742SMiklos Szeredi static ssize_t proc_fdinfo_read(struct file *file, char __user *buf, 184827932742SMiklos Szeredi size_t len, loff_t *ppos) 184927932742SMiklos Szeredi { 185027932742SMiklos Szeredi char tmp[PROC_FDINFO_MAX]; 18513dcd25f3SJan Blunck int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp); 185227932742SMiklos Szeredi if (!err) 185327932742SMiklos Szeredi err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp)); 185427932742SMiklos Szeredi return err; 185527932742SMiklos Szeredi } 185627932742SMiklos Szeredi 185727932742SMiklos Szeredi static const struct file_operations proc_fdinfo_file_operations = { 185827932742SMiklos Szeredi .open = nonseekable_open, 185927932742SMiklos Szeredi .read = proc_fdinfo_read, 186027932742SMiklos Szeredi }; 186127932742SMiklos Szeredi 186200977a59SArjan van de Ven static const struct file_operations proc_fd_operations = { 18631da177e4SLinus Torvalds .read = generic_read_dir, 18641da177e4SLinus Torvalds .readdir = proc_readfd, 18651da177e4SLinus Torvalds }; 18661da177e4SLinus Torvalds 18671da177e4SLinus Torvalds /* 18688948e11fSAlexey Dobriyan * /proc/pid/fd needs a special permission handler so that a process can still 18698948e11fSAlexey Dobriyan * access /proc/self/fd after it has executed a setuid(). 18708948e11fSAlexey Dobriyan */ 1871e6305c43SAl Viro static int proc_fd_permission(struct inode *inode, int mask) 18728948e11fSAlexey Dobriyan { 18738948e11fSAlexey Dobriyan int rv; 18748948e11fSAlexey Dobriyan 18758948e11fSAlexey Dobriyan rv = generic_permission(inode, mask, NULL); 18768948e11fSAlexey Dobriyan if (rv == 0) 18778948e11fSAlexey Dobriyan return 0; 18788948e11fSAlexey Dobriyan if (task_pid(current) == proc_pid(inode)) 18798948e11fSAlexey Dobriyan rv = 0; 18808948e11fSAlexey Dobriyan return rv; 18818948e11fSAlexey Dobriyan } 18828948e11fSAlexey Dobriyan 18838948e11fSAlexey Dobriyan /* 18841da177e4SLinus Torvalds * proc directories can do almost nothing.. 18851da177e4SLinus Torvalds */ 1886c5ef1c42SArjan van de Ven static const struct inode_operations proc_fd_inode_operations = { 18871da177e4SLinus Torvalds .lookup = proc_lookupfd, 18888948e11fSAlexey Dobriyan .permission = proc_fd_permission, 18896d76fa58SLinus Torvalds .setattr = proc_setattr, 18901da177e4SLinus Torvalds }; 18911da177e4SLinus Torvalds 189227932742SMiklos Szeredi static struct dentry *proc_fdinfo_instantiate(struct inode *dir, 189327932742SMiklos Szeredi struct dentry *dentry, struct task_struct *task, const void *ptr) 189427932742SMiklos Szeredi { 189527932742SMiklos Szeredi unsigned fd = *(unsigned *)ptr; 189627932742SMiklos Szeredi struct inode *inode; 189727932742SMiklos Szeredi struct proc_inode *ei; 189827932742SMiklos Szeredi struct dentry *error = ERR_PTR(-ENOENT); 189927932742SMiklos Szeredi 190027932742SMiklos Szeredi inode = proc_pid_make_inode(dir->i_sb, task); 190127932742SMiklos Szeredi if (!inode) 190227932742SMiklos Szeredi goto out; 190327932742SMiklos Szeredi ei = PROC_I(inode); 190427932742SMiklos Szeredi ei->fd = fd; 190527932742SMiklos Szeredi inode->i_mode = S_IFREG | S_IRUSR; 190627932742SMiklos Szeredi inode->i_fop = &proc_fdinfo_file_operations; 190727932742SMiklos Szeredi dentry->d_op = &tid_fd_dentry_operations; 190827932742SMiklos Szeredi d_add(dentry, inode); 190927932742SMiklos Szeredi /* Close the race of the process dying before we return the dentry */ 191027932742SMiklos Szeredi if (tid_fd_revalidate(dentry, NULL)) 191127932742SMiklos Szeredi error = NULL; 191227932742SMiklos Szeredi 191327932742SMiklos Szeredi out: 191427932742SMiklos Szeredi return error; 191527932742SMiklos Szeredi } 191627932742SMiklos Szeredi 191727932742SMiklos Szeredi static struct dentry *proc_lookupfdinfo(struct inode *dir, 191827932742SMiklos Szeredi struct dentry *dentry, 191927932742SMiklos Szeredi struct nameidata *nd) 192027932742SMiklos Szeredi { 192127932742SMiklos Szeredi return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate); 192227932742SMiklos Szeredi } 192327932742SMiklos Szeredi 192427932742SMiklos Szeredi static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir) 192527932742SMiklos Szeredi { 192627932742SMiklos Szeredi return proc_readfd_common(filp, dirent, filldir, 192727932742SMiklos Szeredi proc_fdinfo_instantiate); 192827932742SMiklos Szeredi } 192927932742SMiklos Szeredi 193027932742SMiklos Szeredi static const struct file_operations proc_fdinfo_operations = { 193127932742SMiklos Szeredi .read = generic_read_dir, 193227932742SMiklos Szeredi .readdir = proc_readfdinfo, 193327932742SMiklos Szeredi }; 193427932742SMiklos Szeredi 193527932742SMiklos Szeredi /* 193627932742SMiklos Szeredi * proc directories can do almost nothing.. 193727932742SMiklos Szeredi */ 193827932742SMiklos Szeredi static const struct inode_operations proc_fdinfo_inode_operations = { 193927932742SMiklos Szeredi .lookup = proc_lookupfdinfo, 194027932742SMiklos Szeredi .setattr = proc_setattr, 194127932742SMiklos Szeredi }; 194227932742SMiklos Szeredi 194327932742SMiklos Szeredi 1944444ceed8SEric W. Biederman static struct dentry *proc_pident_instantiate(struct inode *dir, 1945c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 1946444ceed8SEric W. Biederman { 1947c5141e6dSEric Dumazet const struct pid_entry *p = ptr; 1948444ceed8SEric W. Biederman struct inode *inode; 1949444ceed8SEric W. Biederman struct proc_inode *ei; 1950444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-EINVAL); 1951444ceed8SEric W. Biederman 195261a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 1953444ceed8SEric W. Biederman if (!inode) 1954444ceed8SEric W. Biederman goto out; 1955444ceed8SEric W. Biederman 1956444ceed8SEric W. Biederman ei = PROC_I(inode); 1957444ceed8SEric W. Biederman inode->i_mode = p->mode; 1958444ceed8SEric W. Biederman if (S_ISDIR(inode->i_mode)) 1959444ceed8SEric W. Biederman inode->i_nlink = 2; /* Use getattr to fix if necessary */ 1960444ceed8SEric W. Biederman if (p->iop) 1961444ceed8SEric W. Biederman inode->i_op = p->iop; 1962444ceed8SEric W. Biederman if (p->fop) 1963444ceed8SEric W. Biederman inode->i_fop = p->fop; 1964444ceed8SEric W. Biederman ei->op = p->op; 1965444ceed8SEric W. Biederman dentry->d_op = &pid_dentry_operations; 1966444ceed8SEric W. Biederman d_add(dentry, inode); 1967444ceed8SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 1968444ceed8SEric W. Biederman if (pid_revalidate(dentry, NULL)) 1969444ceed8SEric W. Biederman error = NULL; 1970444ceed8SEric W. Biederman out: 1971444ceed8SEric W. Biederman return error; 1972444ceed8SEric W. Biederman } 1973444ceed8SEric W. Biederman 19741da177e4SLinus Torvalds static struct dentry *proc_pident_lookup(struct inode *dir, 19751da177e4SLinus Torvalds struct dentry *dentry, 1976c5141e6dSEric Dumazet const struct pid_entry *ents, 19777bcd6b0eSEric W. Biederman unsigned int nents) 19781da177e4SLinus Torvalds { 19791da177e4SLinus Torvalds struct inode *inode; 1980cd6a3ce9SEric W. Biederman struct dentry *error; 198199f89551SEric W. Biederman struct task_struct *task = get_proc_task(dir); 1982c5141e6dSEric Dumazet const struct pid_entry *p, *last; 19831da177e4SLinus Torvalds 1984cd6a3ce9SEric W. Biederman error = ERR_PTR(-ENOENT); 19851da177e4SLinus Torvalds inode = NULL; 19861da177e4SLinus Torvalds 198799f89551SEric W. Biederman if (!task) 198899f89551SEric W. Biederman goto out_no_task; 19891da177e4SLinus Torvalds 199020cdc894SEric W. Biederman /* 199120cdc894SEric W. Biederman * Yes, it does not scale. And it should not. Don't add 199220cdc894SEric W. Biederman * new entries into /proc/<tgid>/ without very good reasons. 199320cdc894SEric W. Biederman */ 19947bcd6b0eSEric W. Biederman last = &ents[nents - 1]; 19957bcd6b0eSEric W. Biederman for (p = ents; p <= last; p++) { 19961da177e4SLinus Torvalds if (p->len != dentry->d_name.len) 19971da177e4SLinus Torvalds continue; 19981da177e4SLinus Torvalds if (!memcmp(dentry->d_name.name, p->name, p->len)) 19991da177e4SLinus Torvalds break; 20001da177e4SLinus Torvalds } 20017bcd6b0eSEric W. Biederman if (p > last) 20021da177e4SLinus Torvalds goto out; 20031da177e4SLinus Torvalds 2004444ceed8SEric W. Biederman error = proc_pident_instantiate(dir, dentry, task, p); 20051da177e4SLinus Torvalds out: 200699f89551SEric W. Biederman put_task_struct(task); 200799f89551SEric W. Biederman out_no_task: 2008cd6a3ce9SEric W. Biederman return error; 20091da177e4SLinus Torvalds } 20101da177e4SLinus Torvalds 2011c5141e6dSEric Dumazet static int proc_pident_fill_cache(struct file *filp, void *dirent, 2012c5141e6dSEric Dumazet filldir_t filldir, struct task_struct *task, const struct pid_entry *p) 201361a28784SEric W. Biederman { 201461a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, p->name, p->len, 201561a28784SEric W. Biederman proc_pident_instantiate, task, p); 201661a28784SEric W. Biederman } 201761a28784SEric W. Biederman 201828a6d671SEric W. Biederman static int proc_pident_readdir(struct file *filp, 201928a6d671SEric W. Biederman void *dirent, filldir_t filldir, 2020c5141e6dSEric Dumazet const struct pid_entry *ents, unsigned int nents) 202128a6d671SEric W. Biederman { 202228a6d671SEric W. Biederman int i; 20232fddfeefSJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 202428a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 202528a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 2026c5141e6dSEric Dumazet const struct pid_entry *p, *last; 202728a6d671SEric W. Biederman ino_t ino; 202828a6d671SEric W. Biederman int ret; 202928a6d671SEric W. Biederman 203028a6d671SEric W. Biederman ret = -ENOENT; 203128a6d671SEric W. Biederman if (!task) 203261a28784SEric W. Biederman goto out_no_task; 203328a6d671SEric W. Biederman 203428a6d671SEric W. Biederman ret = 0; 203528a6d671SEric W. Biederman i = filp->f_pos; 203628a6d671SEric W. Biederman switch (i) { 203728a6d671SEric W. Biederman case 0: 203828a6d671SEric W. Biederman ino = inode->i_ino; 203928a6d671SEric W. Biederman if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0) 204028a6d671SEric W. Biederman goto out; 204128a6d671SEric W. Biederman i++; 204228a6d671SEric W. Biederman filp->f_pos++; 204328a6d671SEric W. Biederman /* fall through */ 204428a6d671SEric W. Biederman case 1: 204528a6d671SEric W. Biederman ino = parent_ino(dentry); 204628a6d671SEric W. Biederman if (filldir(dirent, "..", 2, 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 default: 205228a6d671SEric W. Biederman i -= 2; 205328a6d671SEric W. Biederman if (i >= nents) { 205428a6d671SEric W. Biederman ret = 1; 205528a6d671SEric W. Biederman goto out; 205628a6d671SEric W. Biederman } 205728a6d671SEric W. Biederman p = ents + i; 20587bcd6b0eSEric W. Biederman last = &ents[nents - 1]; 20597bcd6b0eSEric W. Biederman while (p <= last) { 206061a28784SEric W. Biederman if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0) 206128a6d671SEric W. Biederman goto out; 206228a6d671SEric W. Biederman filp->f_pos++; 206328a6d671SEric W. Biederman p++; 206428a6d671SEric W. Biederman } 20651da177e4SLinus Torvalds } 20661da177e4SLinus Torvalds 206728a6d671SEric W. Biederman ret = 1; 206828a6d671SEric W. Biederman out: 206961a28784SEric W. Biederman put_task_struct(task); 207061a28784SEric W. Biederman out_no_task: 207128a6d671SEric W. Biederman return ret; 20721da177e4SLinus Torvalds } 20731da177e4SLinus Torvalds 20741da177e4SLinus Torvalds #ifdef CONFIG_SECURITY 207528a6d671SEric W. Biederman static ssize_t proc_pid_attr_read(struct file * file, char __user * buf, 207628a6d671SEric W. Biederman size_t count, loff_t *ppos) 207728a6d671SEric W. Biederman { 20782fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 207904ff9708SAl Viro char *p = NULL; 208028a6d671SEric W. Biederman ssize_t length; 208128a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 208228a6d671SEric W. Biederman 208328a6d671SEric W. Biederman if (!task) 208404ff9708SAl Viro return -ESRCH; 208528a6d671SEric W. Biederman 208628a6d671SEric W. Biederman length = security_getprocattr(task, 20872fddfeefSJosef "Jeff" Sipek (char*)file->f_path.dentry->d_name.name, 208804ff9708SAl Viro &p); 208928a6d671SEric W. Biederman put_task_struct(task); 209004ff9708SAl Viro if (length > 0) 209104ff9708SAl Viro length = simple_read_from_buffer(buf, count, ppos, p, length); 209204ff9708SAl Viro kfree(p); 209328a6d671SEric W. Biederman return length; 209428a6d671SEric W. Biederman } 209528a6d671SEric W. Biederman 209628a6d671SEric W. Biederman static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf, 209728a6d671SEric W. Biederman size_t count, loff_t *ppos) 209828a6d671SEric W. Biederman { 20992fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 210028a6d671SEric W. Biederman char *page; 210128a6d671SEric W. Biederman ssize_t length; 210228a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 210328a6d671SEric W. Biederman 210428a6d671SEric W. Biederman length = -ESRCH; 210528a6d671SEric W. Biederman if (!task) 210628a6d671SEric W. Biederman goto out_no_task; 210728a6d671SEric W. Biederman if (count > PAGE_SIZE) 210828a6d671SEric W. Biederman count = PAGE_SIZE; 210928a6d671SEric W. Biederman 211028a6d671SEric W. Biederman /* No partial writes. */ 211128a6d671SEric W. Biederman length = -EINVAL; 211228a6d671SEric W. Biederman if (*ppos != 0) 211328a6d671SEric W. Biederman goto out; 211428a6d671SEric W. Biederman 211528a6d671SEric W. Biederman length = -ENOMEM; 2116e12ba74dSMel Gorman page = (char*)__get_free_page(GFP_TEMPORARY); 211728a6d671SEric W. Biederman if (!page) 211828a6d671SEric W. Biederman goto out; 211928a6d671SEric W. Biederman 212028a6d671SEric W. Biederman length = -EFAULT; 212128a6d671SEric W. Biederman if (copy_from_user(page, buf, count)) 212228a6d671SEric W. Biederman goto out_free; 212328a6d671SEric W. Biederman 212428a6d671SEric W. Biederman length = security_setprocattr(task, 21252fddfeefSJosef "Jeff" Sipek (char*)file->f_path.dentry->d_name.name, 212628a6d671SEric W. Biederman (void*)page, count); 212728a6d671SEric W. Biederman out_free: 212828a6d671SEric W. Biederman free_page((unsigned long) page); 212928a6d671SEric W. Biederman out: 213028a6d671SEric W. Biederman put_task_struct(task); 213128a6d671SEric W. Biederman out_no_task: 213228a6d671SEric W. Biederman return length; 213328a6d671SEric W. Biederman } 213428a6d671SEric W. Biederman 213500977a59SArjan van de Ven static const struct file_operations proc_pid_attr_operations = { 213628a6d671SEric W. Biederman .read = proc_pid_attr_read, 213728a6d671SEric W. Biederman .write = proc_pid_attr_write, 213828a6d671SEric W. Biederman }; 213928a6d671SEric W. Biederman 2140c5141e6dSEric Dumazet static const struct pid_entry attr_dir_stuff[] = { 214161a28784SEric W. Biederman REG("current", S_IRUGO|S_IWUGO, pid_attr), 214261a28784SEric W. Biederman REG("prev", S_IRUGO, pid_attr), 214361a28784SEric W. Biederman REG("exec", S_IRUGO|S_IWUGO, pid_attr), 214461a28784SEric W. Biederman REG("fscreate", S_IRUGO|S_IWUGO, pid_attr), 214561a28784SEric W. Biederman REG("keycreate", S_IRUGO|S_IWUGO, pid_attr), 214661a28784SEric W. Biederman REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr), 214728a6d671SEric W. Biederman }; 214828a6d671SEric W. Biederman 214972d9dcfcSEric W. Biederman static int proc_attr_dir_readdir(struct file * filp, 21501da177e4SLinus Torvalds void * dirent, filldir_t filldir) 21511da177e4SLinus Torvalds { 21521da177e4SLinus Torvalds return proc_pident_readdir(filp,dirent,filldir, 215372d9dcfcSEric W. Biederman attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff)); 21541da177e4SLinus Torvalds } 21551da177e4SLinus Torvalds 215600977a59SArjan van de Ven static const struct file_operations proc_attr_dir_operations = { 21571da177e4SLinus Torvalds .read = generic_read_dir, 215872d9dcfcSEric W. Biederman .readdir = proc_attr_dir_readdir, 21591da177e4SLinus Torvalds }; 21601da177e4SLinus Torvalds 216172d9dcfcSEric W. Biederman static struct dentry *proc_attr_dir_lookup(struct inode *dir, 21621da177e4SLinus Torvalds struct dentry *dentry, struct nameidata *nd) 21631da177e4SLinus Torvalds { 21647bcd6b0eSEric W. Biederman return proc_pident_lookup(dir, dentry, 21657bcd6b0eSEric W. Biederman attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff)); 21661da177e4SLinus Torvalds } 21671da177e4SLinus Torvalds 2168c5ef1c42SArjan van de Ven static const struct inode_operations proc_attr_dir_inode_operations = { 216972d9dcfcSEric W. Biederman .lookup = proc_attr_dir_lookup, 217099f89551SEric W. Biederman .getattr = pid_getattr, 21716d76fa58SLinus Torvalds .setattr = proc_setattr, 21721da177e4SLinus Torvalds }; 21731da177e4SLinus Torvalds 21741da177e4SLinus Torvalds #endif 21751da177e4SLinus Torvalds 21763cb4a0bbSKawai, Hidehiro #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE) 21773cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf, 21783cb4a0bbSKawai, Hidehiro size_t count, loff_t *ppos) 21793cb4a0bbSKawai, Hidehiro { 21803cb4a0bbSKawai, Hidehiro struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 21813cb4a0bbSKawai, Hidehiro struct mm_struct *mm; 21823cb4a0bbSKawai, Hidehiro char buffer[PROC_NUMBUF]; 21833cb4a0bbSKawai, Hidehiro size_t len; 21843cb4a0bbSKawai, Hidehiro int ret; 21853cb4a0bbSKawai, Hidehiro 21863cb4a0bbSKawai, Hidehiro if (!task) 21873cb4a0bbSKawai, Hidehiro return -ESRCH; 21883cb4a0bbSKawai, Hidehiro 21893cb4a0bbSKawai, Hidehiro ret = 0; 21903cb4a0bbSKawai, Hidehiro mm = get_task_mm(task); 21913cb4a0bbSKawai, Hidehiro if (mm) { 21923cb4a0bbSKawai, Hidehiro len = snprintf(buffer, sizeof(buffer), "%08lx\n", 21933cb4a0bbSKawai, Hidehiro ((mm->flags & MMF_DUMP_FILTER_MASK) >> 21943cb4a0bbSKawai, Hidehiro MMF_DUMP_FILTER_SHIFT)); 21953cb4a0bbSKawai, Hidehiro mmput(mm); 21963cb4a0bbSKawai, Hidehiro ret = simple_read_from_buffer(buf, count, ppos, buffer, len); 21973cb4a0bbSKawai, Hidehiro } 21983cb4a0bbSKawai, Hidehiro 21993cb4a0bbSKawai, Hidehiro put_task_struct(task); 22003cb4a0bbSKawai, Hidehiro 22013cb4a0bbSKawai, Hidehiro return ret; 22023cb4a0bbSKawai, Hidehiro } 22033cb4a0bbSKawai, Hidehiro 22043cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_write(struct file *file, 22053cb4a0bbSKawai, Hidehiro const char __user *buf, 22063cb4a0bbSKawai, Hidehiro size_t count, 22073cb4a0bbSKawai, Hidehiro loff_t *ppos) 22083cb4a0bbSKawai, Hidehiro { 22093cb4a0bbSKawai, Hidehiro struct task_struct *task; 22103cb4a0bbSKawai, Hidehiro struct mm_struct *mm; 22113cb4a0bbSKawai, Hidehiro char buffer[PROC_NUMBUF], *end; 22123cb4a0bbSKawai, Hidehiro unsigned int val; 22133cb4a0bbSKawai, Hidehiro int ret; 22143cb4a0bbSKawai, Hidehiro int i; 22153cb4a0bbSKawai, Hidehiro unsigned long mask; 22163cb4a0bbSKawai, Hidehiro 22173cb4a0bbSKawai, Hidehiro ret = -EFAULT; 22183cb4a0bbSKawai, Hidehiro memset(buffer, 0, sizeof(buffer)); 22193cb4a0bbSKawai, Hidehiro if (count > sizeof(buffer) - 1) 22203cb4a0bbSKawai, Hidehiro count = sizeof(buffer) - 1; 22213cb4a0bbSKawai, Hidehiro if (copy_from_user(buffer, buf, count)) 22223cb4a0bbSKawai, Hidehiro goto out_no_task; 22233cb4a0bbSKawai, Hidehiro 22243cb4a0bbSKawai, Hidehiro ret = -EINVAL; 22253cb4a0bbSKawai, Hidehiro val = (unsigned int)simple_strtoul(buffer, &end, 0); 22263cb4a0bbSKawai, Hidehiro if (*end == '\n') 22273cb4a0bbSKawai, Hidehiro end++; 22283cb4a0bbSKawai, Hidehiro if (end - buffer == 0) 22293cb4a0bbSKawai, Hidehiro goto out_no_task; 22303cb4a0bbSKawai, Hidehiro 22313cb4a0bbSKawai, Hidehiro ret = -ESRCH; 22323cb4a0bbSKawai, Hidehiro task = get_proc_task(file->f_dentry->d_inode); 22333cb4a0bbSKawai, Hidehiro if (!task) 22343cb4a0bbSKawai, Hidehiro goto out_no_task; 22353cb4a0bbSKawai, Hidehiro 22363cb4a0bbSKawai, Hidehiro ret = end - buffer; 22373cb4a0bbSKawai, Hidehiro mm = get_task_mm(task); 22383cb4a0bbSKawai, Hidehiro if (!mm) 22393cb4a0bbSKawai, Hidehiro goto out_no_mm; 22403cb4a0bbSKawai, Hidehiro 22413cb4a0bbSKawai, Hidehiro for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) { 22423cb4a0bbSKawai, Hidehiro if (val & mask) 22433cb4a0bbSKawai, Hidehiro set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags); 22443cb4a0bbSKawai, Hidehiro else 22453cb4a0bbSKawai, Hidehiro clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags); 22463cb4a0bbSKawai, Hidehiro } 22473cb4a0bbSKawai, Hidehiro 22483cb4a0bbSKawai, Hidehiro mmput(mm); 22493cb4a0bbSKawai, Hidehiro out_no_mm: 22503cb4a0bbSKawai, Hidehiro put_task_struct(task); 22513cb4a0bbSKawai, Hidehiro out_no_task: 22523cb4a0bbSKawai, Hidehiro return ret; 22533cb4a0bbSKawai, Hidehiro } 22543cb4a0bbSKawai, Hidehiro 22553cb4a0bbSKawai, Hidehiro static const struct file_operations proc_coredump_filter_operations = { 22563cb4a0bbSKawai, Hidehiro .read = proc_coredump_filter_read, 22573cb4a0bbSKawai, Hidehiro .write = proc_coredump_filter_write, 22583cb4a0bbSKawai, Hidehiro }; 22593cb4a0bbSKawai, Hidehiro #endif 22603cb4a0bbSKawai, Hidehiro 22611da177e4SLinus Torvalds /* 22621da177e4SLinus Torvalds * /proc/self: 22631da177e4SLinus Torvalds */ 22641da177e4SLinus Torvalds static int proc_self_readlink(struct dentry *dentry, char __user *buffer, 22651da177e4SLinus Torvalds int buflen) 22661da177e4SLinus Torvalds { 2267488e5bc4SEric W. Biederman struct pid_namespace *ns = dentry->d_sb->s_fs_info; 2268b55fcb22SAndrew Morton pid_t tgid = task_tgid_nr_ns(current, ns); 22698578cea7SEric W. Biederman char tmp[PROC_NUMBUF]; 2270b55fcb22SAndrew Morton if (!tgid) 2271488e5bc4SEric W. Biederman return -ENOENT; 2272b55fcb22SAndrew Morton sprintf(tmp, "%d", tgid); 22731da177e4SLinus Torvalds return vfs_readlink(dentry,buffer,buflen,tmp); 22741da177e4SLinus Torvalds } 22751da177e4SLinus Torvalds 2276008b150aSAl Viro static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd) 22771da177e4SLinus Torvalds { 2278488e5bc4SEric W. Biederman struct pid_namespace *ns = dentry->d_sb->s_fs_info; 2279b55fcb22SAndrew Morton pid_t tgid = task_tgid_nr_ns(current, ns); 22808578cea7SEric W. Biederman char tmp[PROC_NUMBUF]; 2281b55fcb22SAndrew Morton if (!tgid) 2282488e5bc4SEric W. Biederman return ERR_PTR(-ENOENT); 2283b55fcb22SAndrew Morton sprintf(tmp, "%d", task_tgid_nr_ns(current, ns)); 2284008b150aSAl Viro return ERR_PTR(vfs_follow_link(nd,tmp)); 22851da177e4SLinus Torvalds } 22861da177e4SLinus Torvalds 2287c5ef1c42SArjan van de Ven static const struct inode_operations proc_self_inode_operations = { 22881da177e4SLinus Torvalds .readlink = proc_self_readlink, 22891da177e4SLinus Torvalds .follow_link = proc_self_follow_link, 22901da177e4SLinus Torvalds }; 22911da177e4SLinus Torvalds 229228a6d671SEric W. Biederman /* 2293801199ceSEric W. Biederman * proc base 2294801199ceSEric W. Biederman * 2295801199ceSEric W. Biederman * These are the directory entries in the root directory of /proc 2296801199ceSEric W. Biederman * that properly belong to the /proc filesystem, as they describe 2297801199ceSEric W. Biederman * describe something that is process related. 2298801199ceSEric W. Biederman */ 2299c5141e6dSEric Dumazet static const struct pid_entry proc_base_stuff[] = { 230061a28784SEric W. Biederman NOD("self", S_IFLNK|S_IRWXUGO, 2301801199ceSEric W. Biederman &proc_self_inode_operations, NULL, {}), 2302801199ceSEric W. Biederman }; 2303801199ceSEric W. Biederman 2304801199ceSEric W. Biederman /* 2305801199ceSEric W. Biederman * Exceptional case: normally we are not allowed to unhash a busy 2306801199ceSEric W. Biederman * directory. In this case, however, we can do it - no aliasing problems 2307801199ceSEric W. Biederman * due to the way we treat inodes. 2308801199ceSEric W. Biederman */ 2309801199ceSEric W. Biederman static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd) 2310801199ceSEric W. Biederman { 2311801199ceSEric W. Biederman struct inode *inode = dentry->d_inode; 2312801199ceSEric W. Biederman struct task_struct *task = get_proc_task(inode); 2313801199ceSEric W. Biederman if (task) { 2314801199ceSEric W. Biederman put_task_struct(task); 2315801199ceSEric W. Biederman return 1; 2316801199ceSEric W. Biederman } 2317801199ceSEric W. Biederman d_drop(dentry); 2318801199ceSEric W. Biederman return 0; 2319801199ceSEric W. Biederman } 2320801199ceSEric W. Biederman 2321801199ceSEric W. Biederman static struct dentry_operations proc_base_dentry_operations = 2322801199ceSEric W. Biederman { 2323801199ceSEric W. Biederman .d_revalidate = proc_base_revalidate, 2324801199ceSEric W. Biederman .d_delete = pid_delete_dentry, 2325801199ceSEric W. Biederman }; 2326801199ceSEric W. Biederman 2327444ceed8SEric W. Biederman static struct dentry *proc_base_instantiate(struct inode *dir, 2328c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 2329801199ceSEric W. Biederman { 2330c5141e6dSEric Dumazet const struct pid_entry *p = ptr; 2331801199ceSEric W. Biederman struct inode *inode; 2332801199ceSEric W. Biederman struct proc_inode *ei; 2333444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-EINVAL); 2334801199ceSEric W. Biederman 2335801199ceSEric W. Biederman /* Allocate the inode */ 2336801199ceSEric W. Biederman error = ERR_PTR(-ENOMEM); 2337801199ceSEric W. Biederman inode = new_inode(dir->i_sb); 2338801199ceSEric W. Biederman if (!inode) 2339801199ceSEric W. Biederman goto out; 2340801199ceSEric W. Biederman 2341801199ceSEric W. Biederman /* Initialize the inode */ 2342801199ceSEric W. Biederman ei = PROC_I(inode); 2343801199ceSEric W. Biederman inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; 2344801199ceSEric W. Biederman 2345801199ceSEric W. Biederman /* 2346801199ceSEric W. Biederman * grab the reference to the task. 2347801199ceSEric W. Biederman */ 23481a657f78SOleg Nesterov ei->pid = get_task_pid(task, PIDTYPE_PID); 2349801199ceSEric W. Biederman if (!ei->pid) 2350801199ceSEric W. Biederman goto out_iput; 2351801199ceSEric W. Biederman 2352801199ceSEric W. Biederman inode->i_uid = 0; 2353801199ceSEric W. Biederman inode->i_gid = 0; 2354801199ceSEric W. Biederman inode->i_mode = p->mode; 2355801199ceSEric W. Biederman if (S_ISDIR(inode->i_mode)) 2356801199ceSEric W. Biederman inode->i_nlink = 2; 2357801199ceSEric W. Biederman if (S_ISLNK(inode->i_mode)) 2358801199ceSEric W. Biederman inode->i_size = 64; 2359801199ceSEric W. Biederman if (p->iop) 2360801199ceSEric W. Biederman inode->i_op = p->iop; 2361801199ceSEric W. Biederman if (p->fop) 2362801199ceSEric W. Biederman inode->i_fop = p->fop; 2363801199ceSEric W. Biederman ei->op = p->op; 2364801199ceSEric W. Biederman dentry->d_op = &proc_base_dentry_operations; 2365801199ceSEric W. Biederman d_add(dentry, inode); 2366801199ceSEric W. Biederman error = NULL; 2367801199ceSEric W. Biederman out: 2368801199ceSEric W. Biederman return error; 2369801199ceSEric W. Biederman out_iput: 2370801199ceSEric W. Biederman iput(inode); 2371801199ceSEric W. Biederman goto out; 2372801199ceSEric W. Biederman } 2373801199ceSEric W. Biederman 2374444ceed8SEric W. Biederman static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry) 2375444ceed8SEric W. Biederman { 2376444ceed8SEric W. Biederman struct dentry *error; 2377444ceed8SEric W. Biederman struct task_struct *task = get_proc_task(dir); 2378c5141e6dSEric Dumazet const struct pid_entry *p, *last; 2379444ceed8SEric W. Biederman 2380444ceed8SEric W. Biederman error = ERR_PTR(-ENOENT); 2381444ceed8SEric W. Biederman 2382444ceed8SEric W. Biederman if (!task) 2383444ceed8SEric W. Biederman goto out_no_task; 2384444ceed8SEric W. Biederman 2385444ceed8SEric W. Biederman /* Lookup the directory entry */ 23867bcd6b0eSEric W. Biederman last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1]; 23877bcd6b0eSEric W. Biederman for (p = proc_base_stuff; p <= last; p++) { 2388444ceed8SEric W. Biederman if (p->len != dentry->d_name.len) 2389444ceed8SEric W. Biederman continue; 2390444ceed8SEric W. Biederman if (!memcmp(dentry->d_name.name, p->name, p->len)) 2391444ceed8SEric W. Biederman break; 2392444ceed8SEric W. Biederman } 23937bcd6b0eSEric W. Biederman if (p > last) 2394444ceed8SEric W. Biederman goto out; 2395444ceed8SEric W. Biederman 2396444ceed8SEric W. Biederman error = proc_base_instantiate(dir, dentry, task, p); 2397444ceed8SEric W. Biederman 2398444ceed8SEric W. Biederman out: 2399444ceed8SEric W. Biederman put_task_struct(task); 2400444ceed8SEric W. Biederman out_no_task: 2401444ceed8SEric W. Biederman return error; 2402444ceed8SEric W. Biederman } 2403444ceed8SEric W. Biederman 2404c5141e6dSEric Dumazet static int proc_base_fill_cache(struct file *filp, void *dirent, 2405c5141e6dSEric Dumazet filldir_t filldir, struct task_struct *task, const struct pid_entry *p) 240661a28784SEric W. Biederman { 240761a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, p->name, p->len, 240861a28784SEric W. Biederman proc_base_instantiate, task, p); 240961a28784SEric W. Biederman } 241061a28784SEric W. Biederman 2411aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING 2412297c5d92SAndrea Righi static int do_io_accounting(struct task_struct *task, char *buffer, int whole) 2413aba76fdbSAndrew Morton { 2414940389b8SAndrea Righi struct task_io_accounting acct = task->ioac; 2415297c5d92SAndrea Righi unsigned long flags; 2416297c5d92SAndrea Righi 24175995477aSAndrea Righi if (whole && lock_task_sighand(task, &flags)) { 2418b2d002dbSAndrea Righi struct task_struct *t = task; 2419297c5d92SAndrea Righi 24205995477aSAndrea Righi task_io_accounting_add(&acct, &task->signal->ioac); 24215995477aSAndrea Righi while_each_thread(task, t) 24225995477aSAndrea Righi task_io_accounting_add(&acct, &t->ioac); 2423297c5d92SAndrea Righi 2424297c5d92SAndrea Righi unlock_task_sighand(task, &flags); 2425297c5d92SAndrea Righi } 2426aba76fdbSAndrew Morton return sprintf(buffer, 2427aba76fdbSAndrew Morton "rchar: %llu\n" 2428aba76fdbSAndrew Morton "wchar: %llu\n" 2429aba76fdbSAndrew Morton "syscr: %llu\n" 2430aba76fdbSAndrew Morton "syscw: %llu\n" 2431aba76fdbSAndrew Morton "read_bytes: %llu\n" 2432aba76fdbSAndrew Morton "write_bytes: %llu\n" 2433aba76fdbSAndrew Morton "cancelled_write_bytes: %llu\n", 24347c44319dSAlexander Beregalov (unsigned long long)acct.rchar, 24357c44319dSAlexander Beregalov (unsigned long long)acct.wchar, 24367c44319dSAlexander Beregalov (unsigned long long)acct.syscr, 24377c44319dSAlexander Beregalov (unsigned long long)acct.syscw, 24387c44319dSAlexander Beregalov (unsigned long long)acct.read_bytes, 24397c44319dSAlexander Beregalov (unsigned long long)acct.write_bytes, 24407c44319dSAlexander Beregalov (unsigned long long)acct.cancelled_write_bytes); 2441aba76fdbSAndrew Morton } 2442297c5d92SAndrea Righi 2443297c5d92SAndrea Righi static int proc_tid_io_accounting(struct task_struct *task, char *buffer) 2444297c5d92SAndrea Righi { 2445297c5d92SAndrea Righi return do_io_accounting(task, buffer, 0); 2446297c5d92SAndrea Righi } 2447297c5d92SAndrea Righi 2448297c5d92SAndrea Righi static int proc_tgid_io_accounting(struct task_struct *task, char *buffer) 2449297c5d92SAndrea Righi { 2450297c5d92SAndrea Righi return do_io_accounting(task, buffer, 1); 2451297c5d92SAndrea Righi } 2452297c5d92SAndrea Righi #endif /* CONFIG_TASK_IO_ACCOUNTING */ 2453aba76fdbSAndrew Morton 245447830723SKees Cook static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns, 245547830723SKees Cook struct pid *pid, struct task_struct *task) 245647830723SKees Cook { 245747830723SKees Cook seq_printf(m, "%08x\n", task->personality); 245847830723SKees Cook return 0; 245947830723SKees Cook } 246047830723SKees Cook 2461801199ceSEric W. Biederman /* 246228a6d671SEric W. Biederman * Thread groups 246328a6d671SEric W. Biederman */ 246400977a59SArjan van de Ven static const struct file_operations proc_task_operations; 2465c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations; 246620cdc894SEric W. Biederman 2467c5141e6dSEric Dumazet static const struct pid_entry tgid_base_stuff[] = { 246861a28784SEric W. Biederman DIR("task", S_IRUGO|S_IXUGO, task), 246961a28784SEric W. Biederman DIR("fd", S_IRUSR|S_IXUSR, fd), 247027932742SMiklos Szeredi DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo), 2471b2211a36SAndrew Morton #ifdef CONFIG_NET 24724f42c288SAndre Noll DIR("net", S_IRUGO|S_IXUGO, net), 2473b2211a36SAndrew Morton #endif 2474315e28c8SJames Pearson REG("environ", S_IRUSR, environ), 247561a28784SEric W. Biederman INF("auxv", S_IRUSR, pid_auxv), 2476df5f8314SEric W. Biederman ONE("status", S_IRUGO, pid_status), 247747830723SKees Cook ONE("personality", S_IRUSR, pid_personality), 2478d85f50d5SNeil Horman INF("limits", S_IRUSR, pid_limits), 247943ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG 248043ae34cbSIngo Molnar REG("sched", S_IRUGO|S_IWUSR, pid_sched), 248143ae34cbSIngo Molnar #endif 2482ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK 2483ebcb6734SRoland McGrath INF("syscall", S_IRUSR, pid_syscall), 2484ebcb6734SRoland McGrath #endif 248561a28784SEric W. Biederman INF("cmdline", S_IRUGO, pid_cmdline), 2486ee992744SEric W. Biederman ONE("stat", S_IRUGO, tgid_stat), 2487a56d3fc7SEric W. Biederman ONE("statm", S_IRUGO, pid_statm), 248861a28784SEric W. Biederman REG("maps", S_IRUGO, maps), 248928a6d671SEric W. Biederman #ifdef CONFIG_NUMA 249061a28784SEric W. Biederman REG("numa_maps", S_IRUGO, numa_maps), 249128a6d671SEric W. Biederman #endif 249261a28784SEric W. Biederman REG("mem", S_IRUSR|S_IWUSR, mem), 249361a28784SEric W. Biederman LNK("cwd", cwd), 249461a28784SEric W. Biederman LNK("root", root), 249561a28784SEric W. Biederman LNK("exe", exe), 249661a28784SEric W. Biederman REG("mounts", S_IRUGO, mounts), 24972d4d4864SRam Pai REG("mountinfo", S_IRUGO, mountinfo), 249861a28784SEric W. Biederman REG("mountstats", S_IRUSR, mountstats), 24991e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR 2500b813e931SDavid Rientjes REG("clear_refs", S_IWUSR, clear_refs), 250161a28784SEric W. Biederman REG("smaps", S_IRUGO, smaps), 250285863e47SMatt Mackall REG("pagemap", S_IRUSR, pagemap), 250328a6d671SEric W. Biederman #endif 250428a6d671SEric W. Biederman #ifdef CONFIG_SECURITY 250572d9dcfcSEric W. Biederman DIR("attr", S_IRUGO|S_IXUGO, attr_dir), 250628a6d671SEric W. Biederman #endif 250728a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS 250861a28784SEric W. Biederman INF("wchan", S_IRUGO, pid_wchan), 250928a6d671SEric W. Biederman #endif 251028a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS 251161a28784SEric W. Biederman INF("schedstat", S_IRUGO, pid_schedstat), 251228a6d671SEric W. Biederman #endif 25139745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP 25149745512cSArjan van de Ven REG("latency", S_IRUGO, lstats), 25159745512cSArjan van de Ven #endif 25168793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET 251761a28784SEric W. Biederman REG("cpuset", S_IRUGO, cpuset), 251828a6d671SEric W. Biederman #endif 2519a424316cSPaul Menage #ifdef CONFIG_CGROUPS 2520a424316cSPaul Menage REG("cgroup", S_IRUGO, cgroup), 2521a424316cSPaul Menage #endif 252261a28784SEric W. Biederman INF("oom_score", S_IRUGO, oom_score), 252361a28784SEric W. Biederman REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust), 252428a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL 252561a28784SEric W. Biederman REG("loginuid", S_IWUSR|S_IRUGO, loginuid), 25266ee65046SSteve Grubb REG("sessionid", S_IRUGO, sessionid), 252728a6d671SEric W. Biederman #endif 2528f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION 2529f4f154fdSAkinobu Mita REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject), 2530f4f154fdSAkinobu Mita #endif 25313cb4a0bbSKawai, Hidehiro #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE) 25323cb4a0bbSKawai, Hidehiro REG("coredump_filter", S_IRUGO|S_IWUSR, coredump_filter), 25333cb4a0bbSKawai, Hidehiro #endif 2534aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING 2535297c5d92SAndrea Righi INF("io", S_IRUGO, tgid_io_accounting), 2536aba76fdbSAndrew Morton #endif 253728a6d671SEric W. Biederman }; 253828a6d671SEric W. Biederman 253928a6d671SEric W. Biederman static int proc_tgid_base_readdir(struct file * filp, 254028a6d671SEric W. Biederman void * dirent, filldir_t filldir) 254128a6d671SEric W. Biederman { 254228a6d671SEric W. Biederman return proc_pident_readdir(filp,dirent,filldir, 254328a6d671SEric W. Biederman tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff)); 254428a6d671SEric W. Biederman } 254528a6d671SEric W. Biederman 254600977a59SArjan van de Ven static const struct file_operations proc_tgid_base_operations = { 254728a6d671SEric W. Biederman .read = generic_read_dir, 254828a6d671SEric W. Biederman .readdir = proc_tgid_base_readdir, 254928a6d671SEric W. Biederman }; 255028a6d671SEric W. Biederman 255128a6d671SEric W. Biederman static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){ 25527bcd6b0eSEric W. Biederman return proc_pident_lookup(dir, dentry, 25537bcd6b0eSEric W. Biederman tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff)); 255428a6d671SEric W. Biederman } 255528a6d671SEric W. Biederman 2556c5ef1c42SArjan van de Ven static const struct inode_operations proc_tgid_base_inode_operations = { 255728a6d671SEric W. Biederman .lookup = proc_tgid_base_lookup, 255828a6d671SEric W. Biederman .getattr = pid_getattr, 255928a6d671SEric W. Biederman .setattr = proc_setattr, 256028a6d671SEric W. Biederman }; 256128a6d671SEric W. Biederman 256260347f67SPavel Emelyanov static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid) 25631da177e4SLinus Torvalds { 256448e6484dSEric W. Biederman struct dentry *dentry, *leader, *dir; 25658578cea7SEric W. Biederman char buf[PROC_NUMBUF]; 256648e6484dSEric W. Biederman struct qstr name; 25671da177e4SLinus Torvalds 256848e6484dSEric W. Biederman name.name = buf; 256960347f67SPavel Emelyanov name.len = snprintf(buf, sizeof(buf), "%d", pid); 257060347f67SPavel Emelyanov dentry = d_hash_and_lookup(mnt->mnt_root, &name); 257148e6484dSEric W. Biederman if (dentry) { 25727766755aSAndrea Arcangeli if (!(current->flags & PF_EXITING)) 257348e6484dSEric W. Biederman shrink_dcache_parent(dentry); 257448e6484dSEric W. Biederman d_drop(dentry); 257548e6484dSEric W. Biederman dput(dentry); 25761da177e4SLinus Torvalds } 25771da177e4SLinus Torvalds 257860347f67SPavel Emelyanov if (tgid == 0) 257948e6484dSEric W. Biederman goto out; 25801da177e4SLinus Torvalds 258148e6484dSEric W. Biederman name.name = buf; 258260347f67SPavel Emelyanov name.len = snprintf(buf, sizeof(buf), "%d", tgid); 258360347f67SPavel Emelyanov leader = d_hash_and_lookup(mnt->mnt_root, &name); 258448e6484dSEric W. Biederman if (!leader) 258548e6484dSEric W. Biederman goto out; 258648e6484dSEric W. Biederman 258748e6484dSEric W. Biederman name.name = "task"; 258848e6484dSEric W. Biederman name.len = strlen(name.name); 258948e6484dSEric W. Biederman dir = d_hash_and_lookup(leader, &name); 259048e6484dSEric W. Biederman if (!dir) 259148e6484dSEric W. Biederman goto out_put_leader; 259248e6484dSEric W. Biederman 259348e6484dSEric W. Biederman name.name = buf; 259460347f67SPavel Emelyanov name.len = snprintf(buf, sizeof(buf), "%d", pid); 259548e6484dSEric W. Biederman dentry = d_hash_and_lookup(dir, &name); 259648e6484dSEric W. Biederman if (dentry) { 259748e6484dSEric W. Biederman shrink_dcache_parent(dentry); 259848e6484dSEric W. Biederman d_drop(dentry); 259948e6484dSEric W. Biederman dput(dentry); 26001da177e4SLinus Torvalds } 260148e6484dSEric W. Biederman 260248e6484dSEric W. Biederman dput(dir); 260348e6484dSEric W. Biederman out_put_leader: 260448e6484dSEric W. Biederman dput(leader); 260548e6484dSEric W. Biederman out: 260648e6484dSEric W. Biederman return; 26071da177e4SLinus Torvalds } 26081da177e4SLinus Torvalds 26090895e91dSRandy Dunlap /** 26100895e91dSRandy Dunlap * proc_flush_task - Remove dcache entries for @task from the /proc dcache. 26110895e91dSRandy Dunlap * @task: task that should be flushed. 26120895e91dSRandy Dunlap * 26130895e91dSRandy Dunlap * When flushing dentries from proc, one needs to flush them from global 261460347f67SPavel Emelyanov * proc (proc_mnt) and from all the namespaces' procs this task was seen 26150895e91dSRandy Dunlap * in. This call is supposed to do all of this job. 26160895e91dSRandy Dunlap * 26170895e91dSRandy Dunlap * Looks in the dcache for 26180895e91dSRandy Dunlap * /proc/@pid 26190895e91dSRandy Dunlap * /proc/@tgid/task/@pid 26200895e91dSRandy Dunlap * if either directory is present flushes it and all of it'ts children 26210895e91dSRandy Dunlap * from the dcache. 26220895e91dSRandy Dunlap * 26230895e91dSRandy Dunlap * It is safe and reasonable to cache /proc entries for a task until 26240895e91dSRandy Dunlap * that task exits. After that they just clog up the dcache with 26250895e91dSRandy Dunlap * useless entries, possibly causing useful dcache entries to be 26260895e91dSRandy Dunlap * flushed instead. This routine is proved to flush those useless 26270895e91dSRandy Dunlap * dcache entries at process exit time. 26280895e91dSRandy Dunlap * 26290895e91dSRandy Dunlap * NOTE: This routine is just an optimization so it does not guarantee 26300895e91dSRandy Dunlap * that no dcache entries will exist at process exit time it 26310895e91dSRandy Dunlap * just makes it very unlikely that any will persist. 263260347f67SPavel Emelyanov */ 263360347f67SPavel Emelyanov 263460347f67SPavel Emelyanov void proc_flush_task(struct task_struct *task) 263560347f67SPavel Emelyanov { 26369fcc2d15SEric W. Biederman int i; 26379fcc2d15SEric W. Biederman struct pid *pid, *tgid = NULL; 2638130f77ecSPavel Emelyanov struct upid *upid; 2639130f77ecSPavel Emelyanov 2640130f77ecSPavel Emelyanov pid = task_pid(task); 26419fcc2d15SEric W. Biederman if (thread_group_leader(task)) 2642130f77ecSPavel Emelyanov tgid = task_tgid(task); 26439fcc2d15SEric W. Biederman 26449fcc2d15SEric W. Biederman for (i = 0; i <= pid->level; i++) { 2645130f77ecSPavel Emelyanov upid = &pid->numbers[i]; 2646130f77ecSPavel Emelyanov proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr, 26479fcc2d15SEric W. Biederman tgid ? tgid->numbers[i].nr : 0); 2648130f77ecSPavel Emelyanov } 26496f4e6433SPavel Emelyanov 26506f4e6433SPavel Emelyanov upid = &pid->numbers[pid->level]; 26516f4e6433SPavel Emelyanov if (upid->nr == 1) 26526f4e6433SPavel Emelyanov pid_ns_release_proc(upid->ns); 265360347f67SPavel Emelyanov } 265460347f67SPavel Emelyanov 26559711ef99SAdrian Bunk static struct dentry *proc_pid_instantiate(struct inode *dir, 26569711ef99SAdrian Bunk struct dentry * dentry, 2657c5141e6dSEric Dumazet struct task_struct *task, const void *ptr) 2658444ceed8SEric W. Biederman { 2659444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-ENOENT); 2660444ceed8SEric W. Biederman struct inode *inode; 2661444ceed8SEric W. Biederman 266261a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 2663444ceed8SEric W. Biederman if (!inode) 2664444ceed8SEric W. Biederman goto out; 2665444ceed8SEric W. Biederman 2666444ceed8SEric W. Biederman inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO; 2667444ceed8SEric W. Biederman inode->i_op = &proc_tgid_base_inode_operations; 2668444ceed8SEric W. Biederman inode->i_fop = &proc_tgid_base_operations; 2669444ceed8SEric W. Biederman inode->i_flags|=S_IMMUTABLE; 2670aed54175SVegard Nossum 2671aed54175SVegard Nossum inode->i_nlink = 2 + pid_entry_count_dirs(tgid_base_stuff, 2672aed54175SVegard Nossum ARRAY_SIZE(tgid_base_stuff)); 2673444ceed8SEric W. Biederman 2674444ceed8SEric W. Biederman dentry->d_op = &pid_dentry_operations; 2675444ceed8SEric W. Biederman 2676444ceed8SEric W. Biederman d_add(dentry, inode); 2677444ceed8SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 2678444ceed8SEric W. Biederman if (pid_revalidate(dentry, NULL)) 2679444ceed8SEric W. Biederman error = NULL; 2680444ceed8SEric W. Biederman out: 2681444ceed8SEric W. Biederman return error; 2682444ceed8SEric W. Biederman } 2683444ceed8SEric W. Biederman 26841da177e4SLinus Torvalds struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd) 26851da177e4SLinus Torvalds { 2686cd6a3ce9SEric W. Biederman struct dentry *result = ERR_PTR(-ENOENT); 26871da177e4SLinus Torvalds struct task_struct *task; 26881da177e4SLinus Torvalds unsigned tgid; 2689b488893aSPavel Emelyanov struct pid_namespace *ns; 26901da177e4SLinus Torvalds 2691801199ceSEric W. Biederman result = proc_base_lookup(dir, dentry); 2692801199ceSEric W. Biederman if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT) 2693801199ceSEric W. Biederman goto out; 2694801199ceSEric W. Biederman 26951da177e4SLinus Torvalds tgid = name_to_int(dentry); 26961da177e4SLinus Torvalds if (tgid == ~0U) 26971da177e4SLinus Torvalds goto out; 26981da177e4SLinus Torvalds 2699b488893aSPavel Emelyanov ns = dentry->d_sb->s_fs_info; 2700de758734SEric W. Biederman rcu_read_lock(); 2701b488893aSPavel Emelyanov task = find_task_by_pid_ns(tgid, ns); 27021da177e4SLinus Torvalds if (task) 27031da177e4SLinus Torvalds get_task_struct(task); 2704de758734SEric W. Biederman rcu_read_unlock(); 27051da177e4SLinus Torvalds if (!task) 27061da177e4SLinus Torvalds goto out; 27071da177e4SLinus Torvalds 2708444ceed8SEric W. Biederman result = proc_pid_instantiate(dir, dentry, task, NULL); 270948e6484dSEric W. Biederman put_task_struct(task); 27101da177e4SLinus Torvalds out: 2711cd6a3ce9SEric W. Biederman return result; 27121da177e4SLinus Torvalds } 27131da177e4SLinus Torvalds 27141da177e4SLinus Torvalds /* 27150804ef4bSEric W. Biederman * Find the first task with tgid >= tgid 27160bc58a91SEric W. Biederman * 27171da177e4SLinus Torvalds */ 271819fd4bb2SEric W. Biederman struct tgid_iter { 271919fd4bb2SEric W. Biederman unsigned int tgid; 27200804ef4bSEric W. Biederman struct task_struct *task; 272119fd4bb2SEric W. Biederman }; 272219fd4bb2SEric W. Biederman static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter) 272319fd4bb2SEric W. Biederman { 27240804ef4bSEric W. Biederman struct pid *pid; 27251da177e4SLinus Torvalds 272619fd4bb2SEric W. Biederman if (iter.task) 272719fd4bb2SEric W. Biederman put_task_struct(iter.task); 27280804ef4bSEric W. Biederman rcu_read_lock(); 27290804ef4bSEric W. Biederman retry: 273019fd4bb2SEric W. Biederman iter.task = NULL; 273119fd4bb2SEric W. Biederman pid = find_ge_pid(iter.tgid, ns); 27320804ef4bSEric W. Biederman if (pid) { 273319fd4bb2SEric W. Biederman iter.tgid = pid_nr_ns(pid, ns); 273419fd4bb2SEric W. Biederman iter.task = pid_task(pid, PIDTYPE_PID); 27350804ef4bSEric W. Biederman /* What we to know is if the pid we have find is the 27360804ef4bSEric W. Biederman * pid of a thread_group_leader. Testing for task 27370804ef4bSEric W. Biederman * being a thread_group_leader is the obvious thing 27380804ef4bSEric W. Biederman * todo but there is a window when it fails, due to 27390804ef4bSEric W. Biederman * the pid transfer logic in de_thread. 27400804ef4bSEric W. Biederman * 27410804ef4bSEric W. Biederman * So we perform the straight forward test of seeing 27420804ef4bSEric W. Biederman * if the pid we have found is the pid of a thread 27430804ef4bSEric W. Biederman * group leader, and don't worry if the task we have 27440804ef4bSEric W. Biederman * found doesn't happen to be a thread group leader. 27450804ef4bSEric W. Biederman * As we don't care in the case of readdir. 27460bc58a91SEric W. Biederman */ 274719fd4bb2SEric W. Biederman if (!iter.task || !has_group_leader_pid(iter.task)) { 274819fd4bb2SEric W. Biederman iter.tgid += 1; 27490804ef4bSEric W. Biederman goto retry; 275019fd4bb2SEric W. Biederman } 275119fd4bb2SEric W. Biederman get_task_struct(iter.task); 27521da177e4SLinus Torvalds } 2753454cc105SEric W. Biederman rcu_read_unlock(); 275419fd4bb2SEric W. Biederman return iter; 27551da177e4SLinus Torvalds } 27561da177e4SLinus Torvalds 27577bcd6b0eSEric W. Biederman #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff)) 27581da177e4SLinus Torvalds 275961a28784SEric W. Biederman static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir, 276019fd4bb2SEric W. Biederman struct tgid_iter iter) 276161a28784SEric W. Biederman { 276261a28784SEric W. Biederman char name[PROC_NUMBUF]; 276319fd4bb2SEric W. Biederman int len = snprintf(name, sizeof(name), "%d", iter.tgid); 276461a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, name, len, 276519fd4bb2SEric W. Biederman proc_pid_instantiate, iter.task, NULL); 276661a28784SEric W. Biederman } 276761a28784SEric W. Biederman 27681da177e4SLinus Torvalds /* for the /proc/ directory itself, after non-process stuff has been done */ 27691da177e4SLinus Torvalds int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir) 27701da177e4SLinus Torvalds { 27711da177e4SLinus Torvalds unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY; 27722fddfeefSJosef "Jeff" Sipek struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode); 277319fd4bb2SEric W. Biederman struct tgid_iter iter; 2774b488893aSPavel Emelyanov struct pid_namespace *ns; 27751da177e4SLinus Torvalds 277661a28784SEric W. Biederman if (!reaper) 277761a28784SEric W. Biederman goto out_no_task; 277861a28784SEric W. Biederman 27797bcd6b0eSEric W. Biederman for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) { 2780c5141e6dSEric Dumazet const struct pid_entry *p = &proc_base_stuff[nr]; 278161a28784SEric W. Biederman if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0) 2782801199ceSEric W. Biederman goto out; 27831da177e4SLinus Torvalds } 27841da177e4SLinus Torvalds 2785b488893aSPavel Emelyanov ns = filp->f_dentry->d_sb->s_fs_info; 278619fd4bb2SEric W. Biederman iter.task = NULL; 278719fd4bb2SEric W. Biederman iter.tgid = filp->f_pos - TGID_OFFSET; 278819fd4bb2SEric W. Biederman for (iter = next_tgid(ns, iter); 278919fd4bb2SEric W. Biederman iter.task; 279019fd4bb2SEric W. Biederman iter.tgid += 1, iter = next_tgid(ns, iter)) { 279119fd4bb2SEric W. Biederman filp->f_pos = iter.tgid + TGID_OFFSET; 279219fd4bb2SEric W. Biederman if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) { 279319fd4bb2SEric W. Biederman put_task_struct(iter.task); 27940804ef4bSEric W. Biederman goto out; 27951da177e4SLinus Torvalds } 27961da177e4SLinus Torvalds } 27970804ef4bSEric W. Biederman filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET; 27980804ef4bSEric W. Biederman out: 279961a28784SEric W. Biederman put_task_struct(reaper); 280061a28784SEric W. Biederman out_no_task: 28011da177e4SLinus Torvalds return 0; 28021da177e4SLinus Torvalds } 28031da177e4SLinus Torvalds 28040bc58a91SEric W. Biederman /* 280528a6d671SEric W. Biederman * Tasks 280628a6d671SEric W. Biederman */ 2807c5141e6dSEric Dumazet static const struct pid_entry tid_base_stuff[] = { 280861a28784SEric W. Biederman DIR("fd", S_IRUSR|S_IXUSR, fd), 280927932742SMiklos Szeredi DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo), 2810315e28c8SJames Pearson REG("environ", S_IRUSR, environ), 281161a28784SEric W. Biederman INF("auxv", S_IRUSR, pid_auxv), 2812df5f8314SEric W. Biederman ONE("status", S_IRUGO, pid_status), 281347830723SKees Cook ONE("personality", S_IRUSR, pid_personality), 2814d85f50d5SNeil Horman INF("limits", S_IRUSR, pid_limits), 281543ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG 281643ae34cbSIngo Molnar REG("sched", S_IRUGO|S_IWUSR, pid_sched), 281743ae34cbSIngo Molnar #endif 2818ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK 2819ebcb6734SRoland McGrath INF("syscall", S_IRUSR, pid_syscall), 2820ebcb6734SRoland McGrath #endif 282161a28784SEric W. Biederman INF("cmdline", S_IRUGO, pid_cmdline), 2822ee992744SEric W. Biederman ONE("stat", S_IRUGO, tid_stat), 2823a56d3fc7SEric W. Biederman ONE("statm", S_IRUGO, pid_statm), 282461a28784SEric W. Biederman REG("maps", S_IRUGO, maps), 282528a6d671SEric W. Biederman #ifdef CONFIG_NUMA 282661a28784SEric W. Biederman REG("numa_maps", S_IRUGO, numa_maps), 282728a6d671SEric W. Biederman #endif 282861a28784SEric W. Biederman REG("mem", S_IRUSR|S_IWUSR, mem), 282961a28784SEric W. Biederman LNK("cwd", cwd), 283061a28784SEric W. Biederman LNK("root", root), 283161a28784SEric W. Biederman LNK("exe", exe), 283261a28784SEric W. Biederman REG("mounts", S_IRUGO, mounts), 28332d4d4864SRam Pai REG("mountinfo", S_IRUGO, mountinfo), 28341e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR 2835b813e931SDavid Rientjes REG("clear_refs", S_IWUSR, clear_refs), 283661a28784SEric W. Biederman REG("smaps", S_IRUGO, smaps), 283785863e47SMatt Mackall REG("pagemap", S_IRUSR, pagemap), 283828a6d671SEric W. Biederman #endif 283928a6d671SEric W. Biederman #ifdef CONFIG_SECURITY 284072d9dcfcSEric W. Biederman DIR("attr", S_IRUGO|S_IXUGO, attr_dir), 284128a6d671SEric W. Biederman #endif 284228a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS 284361a28784SEric W. Biederman INF("wchan", S_IRUGO, pid_wchan), 284428a6d671SEric W. Biederman #endif 284528a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS 284661a28784SEric W. Biederman INF("schedstat", S_IRUGO, pid_schedstat), 284728a6d671SEric W. Biederman #endif 28489745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP 28499745512cSArjan van de Ven REG("latency", S_IRUGO, lstats), 28509745512cSArjan van de Ven #endif 28518793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET 285261a28784SEric W. Biederman REG("cpuset", S_IRUGO, cpuset), 285328a6d671SEric W. Biederman #endif 2854a424316cSPaul Menage #ifdef CONFIG_CGROUPS 2855a424316cSPaul Menage REG("cgroup", S_IRUGO, cgroup), 2856a424316cSPaul Menage #endif 285761a28784SEric W. Biederman INF("oom_score", S_IRUGO, oom_score), 285861a28784SEric W. Biederman REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust), 285928a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL 286061a28784SEric W. Biederman REG("loginuid", S_IWUSR|S_IRUGO, loginuid), 28611e0bd755SEric Paris REG("sessionid", S_IRUSR, sessionid), 286228a6d671SEric W. Biederman #endif 2863f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION 2864f4f154fdSAkinobu Mita REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject), 2865f4f154fdSAkinobu Mita #endif 2866297c5d92SAndrea Righi #ifdef CONFIG_TASK_IO_ACCOUNTING 2867297c5d92SAndrea Righi INF("io", S_IRUGO, tid_io_accounting), 2868297c5d92SAndrea Righi #endif 286928a6d671SEric W. Biederman }; 287028a6d671SEric W. Biederman 287128a6d671SEric W. Biederman static int proc_tid_base_readdir(struct file * filp, 287228a6d671SEric W. Biederman void * dirent, filldir_t filldir) 287328a6d671SEric W. Biederman { 287428a6d671SEric W. Biederman return proc_pident_readdir(filp,dirent,filldir, 287528a6d671SEric W. Biederman tid_base_stuff,ARRAY_SIZE(tid_base_stuff)); 287628a6d671SEric W. Biederman } 287728a6d671SEric W. Biederman 287828a6d671SEric W. Biederman static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){ 28797bcd6b0eSEric W. Biederman return proc_pident_lookup(dir, dentry, 28807bcd6b0eSEric W. Biederman tid_base_stuff, ARRAY_SIZE(tid_base_stuff)); 288128a6d671SEric W. Biederman } 288228a6d671SEric W. Biederman 288300977a59SArjan van de Ven static const struct file_operations proc_tid_base_operations = { 288428a6d671SEric W. Biederman .read = generic_read_dir, 288528a6d671SEric W. Biederman .readdir = proc_tid_base_readdir, 288628a6d671SEric W. Biederman }; 288728a6d671SEric W. Biederman 2888c5ef1c42SArjan van de Ven static const struct inode_operations proc_tid_base_inode_operations = { 288928a6d671SEric W. Biederman .lookup = proc_tid_base_lookup, 289028a6d671SEric W. Biederman .getattr = pid_getattr, 289128a6d671SEric W. Biederman .setattr = proc_setattr, 289228a6d671SEric W. Biederman }; 289328a6d671SEric W. Biederman 2894444ceed8SEric W. Biederman static struct dentry *proc_task_instantiate(struct inode *dir, 2895c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 2896444ceed8SEric W. Biederman { 2897444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-ENOENT); 2898444ceed8SEric W. Biederman struct inode *inode; 289961a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 2900444ceed8SEric W. Biederman 2901444ceed8SEric W. Biederman if (!inode) 2902444ceed8SEric W. Biederman goto out; 2903444ceed8SEric W. Biederman inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO; 2904444ceed8SEric W. Biederman inode->i_op = &proc_tid_base_inode_operations; 2905444ceed8SEric W. Biederman inode->i_fop = &proc_tid_base_operations; 2906444ceed8SEric W. Biederman inode->i_flags|=S_IMMUTABLE; 2907aed54175SVegard Nossum 2908aed54175SVegard Nossum inode->i_nlink = 2 + pid_entry_count_dirs(tid_base_stuff, 2909aed54175SVegard Nossum ARRAY_SIZE(tid_base_stuff)); 2910444ceed8SEric W. Biederman 2911444ceed8SEric W. Biederman dentry->d_op = &pid_dentry_operations; 2912444ceed8SEric W. Biederman 2913444ceed8SEric W. Biederman d_add(dentry, inode); 2914444ceed8SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 2915444ceed8SEric W. Biederman if (pid_revalidate(dentry, NULL)) 2916444ceed8SEric W. Biederman error = NULL; 2917444ceed8SEric W. Biederman out: 2918444ceed8SEric W. Biederman return error; 2919444ceed8SEric W. Biederman } 2920444ceed8SEric W. Biederman 292128a6d671SEric W. Biederman static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd) 292228a6d671SEric W. Biederman { 292328a6d671SEric W. Biederman struct dentry *result = ERR_PTR(-ENOENT); 292428a6d671SEric W. Biederman struct task_struct *task; 292528a6d671SEric W. Biederman struct task_struct *leader = get_proc_task(dir); 292628a6d671SEric W. Biederman unsigned tid; 2927b488893aSPavel Emelyanov struct pid_namespace *ns; 292828a6d671SEric W. Biederman 292928a6d671SEric W. Biederman if (!leader) 293028a6d671SEric W. Biederman goto out_no_task; 293128a6d671SEric W. Biederman 293228a6d671SEric W. Biederman tid = name_to_int(dentry); 293328a6d671SEric W. Biederman if (tid == ~0U) 293428a6d671SEric W. Biederman goto out; 293528a6d671SEric W. Biederman 2936b488893aSPavel Emelyanov ns = dentry->d_sb->s_fs_info; 293728a6d671SEric W. Biederman rcu_read_lock(); 2938b488893aSPavel Emelyanov task = find_task_by_pid_ns(tid, ns); 293928a6d671SEric W. Biederman if (task) 294028a6d671SEric W. Biederman get_task_struct(task); 294128a6d671SEric W. Biederman rcu_read_unlock(); 294228a6d671SEric W. Biederman if (!task) 294328a6d671SEric W. Biederman goto out; 2944bac0abd6SPavel Emelyanov if (!same_thread_group(leader, task)) 294528a6d671SEric W. Biederman goto out_drop_task; 294628a6d671SEric W. Biederman 2947444ceed8SEric W. Biederman result = proc_task_instantiate(dir, dentry, task, NULL); 294828a6d671SEric W. Biederman out_drop_task: 294928a6d671SEric W. Biederman put_task_struct(task); 295028a6d671SEric W. Biederman out: 295128a6d671SEric W. Biederman put_task_struct(leader); 295228a6d671SEric W. Biederman out_no_task: 295328a6d671SEric W. Biederman return result; 295428a6d671SEric W. Biederman } 295528a6d671SEric W. Biederman 295628a6d671SEric W. Biederman /* 29570bc58a91SEric W. Biederman * Find the first tid of a thread group to return to user space. 29580bc58a91SEric W. Biederman * 29590bc58a91SEric W. Biederman * Usually this is just the thread group leader, but if the users 29600bc58a91SEric W. Biederman * buffer was too small or there was a seek into the middle of the 29610bc58a91SEric W. Biederman * directory we have more work todo. 29620bc58a91SEric W. Biederman * 29630bc58a91SEric W. Biederman * In the case of a short read we start with find_task_by_pid. 29640bc58a91SEric W. Biederman * 29650bc58a91SEric W. Biederman * In the case of a seek we start with the leader and walk nr 29660bc58a91SEric W. Biederman * threads past it. 29670bc58a91SEric W. Biederman */ 2968cc288738SEric W. Biederman static struct task_struct *first_tid(struct task_struct *leader, 2969b488893aSPavel Emelyanov int tid, int nr, struct pid_namespace *ns) 29700bc58a91SEric W. Biederman { 2971a872ff0cSOleg Nesterov struct task_struct *pos; 29720bc58a91SEric W. Biederman 2973cc288738SEric W. Biederman rcu_read_lock(); 29740bc58a91SEric W. Biederman /* Attempt to start with the pid of a thread */ 29750bc58a91SEric W. Biederman if (tid && (nr > 0)) { 2976b488893aSPavel Emelyanov pos = find_task_by_pid_ns(tid, ns); 2977a872ff0cSOleg Nesterov if (pos && (pos->group_leader == leader)) 2978a872ff0cSOleg Nesterov goto found; 29790bc58a91SEric W. Biederman } 29800bc58a91SEric W. Biederman 29810bc58a91SEric W. Biederman /* If nr exceeds the number of threads there is nothing todo */ 29820bc58a91SEric W. Biederman pos = NULL; 2983a872ff0cSOleg Nesterov if (nr && nr >= get_nr_threads(leader)) 2984a872ff0cSOleg Nesterov goto out; 2985a872ff0cSOleg Nesterov 2986a872ff0cSOleg Nesterov /* If we haven't found our starting place yet start 2987a872ff0cSOleg Nesterov * with the leader and walk nr threads forward. 2988a872ff0cSOleg Nesterov */ 2989a872ff0cSOleg Nesterov for (pos = leader; nr > 0; --nr) { 2990a872ff0cSOleg Nesterov pos = next_thread(pos); 2991a872ff0cSOleg Nesterov if (pos == leader) { 2992a872ff0cSOleg Nesterov pos = NULL; 2993a872ff0cSOleg Nesterov goto out; 2994a872ff0cSOleg Nesterov } 2995a872ff0cSOleg Nesterov } 2996a872ff0cSOleg Nesterov found: 2997a872ff0cSOleg Nesterov get_task_struct(pos); 2998a872ff0cSOleg Nesterov out: 2999cc288738SEric W. Biederman rcu_read_unlock(); 30000bc58a91SEric W. Biederman return pos; 30010bc58a91SEric W. Biederman } 30020bc58a91SEric W. Biederman 30030bc58a91SEric W. Biederman /* 30040bc58a91SEric W. Biederman * Find the next thread in the thread list. 30050bc58a91SEric W. Biederman * Return NULL if there is an error or no next thread. 30060bc58a91SEric W. Biederman * 30070bc58a91SEric W. Biederman * The reference to the input task_struct is released. 30080bc58a91SEric W. Biederman */ 30090bc58a91SEric W. Biederman static struct task_struct *next_tid(struct task_struct *start) 30100bc58a91SEric W. Biederman { 3011c1df7fb8SOleg Nesterov struct task_struct *pos = NULL; 3012cc288738SEric W. Biederman rcu_read_lock(); 3013c1df7fb8SOleg Nesterov if (pid_alive(start)) { 30140bc58a91SEric W. Biederman pos = next_thread(start); 3015c1df7fb8SOleg Nesterov if (thread_group_leader(pos)) 30160bc58a91SEric W. Biederman pos = NULL; 3017c1df7fb8SOleg Nesterov else 3018c1df7fb8SOleg Nesterov get_task_struct(pos); 3019c1df7fb8SOleg Nesterov } 3020cc288738SEric W. Biederman rcu_read_unlock(); 30210bc58a91SEric W. Biederman put_task_struct(start); 30220bc58a91SEric W. Biederman return pos; 30230bc58a91SEric W. Biederman } 30240bc58a91SEric W. Biederman 302561a28784SEric W. Biederman static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir, 302661a28784SEric W. Biederman struct task_struct *task, int tid) 302761a28784SEric W. Biederman { 302861a28784SEric W. Biederman char name[PROC_NUMBUF]; 302961a28784SEric W. Biederman int len = snprintf(name, sizeof(name), "%d", tid); 303061a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, name, len, 303161a28784SEric W. Biederman proc_task_instantiate, task, NULL); 303261a28784SEric W. Biederman } 303361a28784SEric W. Biederman 30341da177e4SLinus Torvalds /* for the /proc/TGID/task/ directories */ 30351da177e4SLinus Torvalds static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir) 30361da177e4SLinus Torvalds { 30372fddfeefSJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 30381da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 30397d895244SGuillaume Chazarain struct task_struct *leader = NULL; 30400bc58a91SEric W. Biederman struct task_struct *task; 30411da177e4SLinus Torvalds int retval = -ENOENT; 30421da177e4SLinus Torvalds ino_t ino; 30430bc58a91SEric W. Biederman int tid; 30441da177e4SLinus Torvalds unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */ 3045b488893aSPavel Emelyanov struct pid_namespace *ns; 30461da177e4SLinus Torvalds 30477d895244SGuillaume Chazarain task = get_proc_task(inode); 30487d895244SGuillaume Chazarain if (!task) 30497d895244SGuillaume Chazarain goto out_no_task; 30507d895244SGuillaume Chazarain rcu_read_lock(); 30517d895244SGuillaume Chazarain if (pid_alive(task)) { 30527d895244SGuillaume Chazarain leader = task->group_leader; 30537d895244SGuillaume Chazarain get_task_struct(leader); 30547d895244SGuillaume Chazarain } 30557d895244SGuillaume Chazarain rcu_read_unlock(); 30567d895244SGuillaume Chazarain put_task_struct(task); 305799f89551SEric W. Biederman if (!leader) 305899f89551SEric W. Biederman goto out_no_task; 30591da177e4SLinus Torvalds retval = 0; 30601da177e4SLinus Torvalds 30611da177e4SLinus Torvalds switch (pos) { 30621da177e4SLinus Torvalds case 0: 30631da177e4SLinus Torvalds ino = inode->i_ino; 30641da177e4SLinus Torvalds if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0) 30651da177e4SLinus Torvalds goto out; 30661da177e4SLinus Torvalds pos++; 30671da177e4SLinus Torvalds /* fall through */ 30681da177e4SLinus Torvalds case 1: 30691da177e4SLinus Torvalds ino = parent_ino(dentry); 30701da177e4SLinus Torvalds if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0) 30711da177e4SLinus Torvalds goto out; 30721da177e4SLinus Torvalds pos++; 30731da177e4SLinus Torvalds /* fall through */ 30741da177e4SLinus Torvalds } 30751da177e4SLinus Torvalds 30760bc58a91SEric W. Biederman /* f_version caches the tgid value that the last readdir call couldn't 30770bc58a91SEric W. Biederman * return. lseek aka telldir automagically resets f_version to 0. 30780bc58a91SEric W. Biederman */ 3079b488893aSPavel Emelyanov ns = filp->f_dentry->d_sb->s_fs_info; 30802b47c361SMathieu Desnoyers tid = (int)filp->f_version; 30810bc58a91SEric W. Biederman filp->f_version = 0; 3082b488893aSPavel Emelyanov for (task = first_tid(leader, tid, pos - 2, ns); 30830bc58a91SEric W. Biederman task; 30840bc58a91SEric W. Biederman task = next_tid(task), pos++) { 3085b488893aSPavel Emelyanov tid = task_pid_nr_ns(task, ns); 308661a28784SEric W. Biederman if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) { 30870bc58a91SEric W. Biederman /* returning this tgid failed, save it as the first 30880bc58a91SEric W. Biederman * pid for the next readir call */ 30892b47c361SMathieu Desnoyers filp->f_version = (u64)tid; 30900bc58a91SEric W. Biederman put_task_struct(task); 30911da177e4SLinus Torvalds break; 30920bc58a91SEric W. Biederman } 30931da177e4SLinus Torvalds } 30941da177e4SLinus Torvalds out: 30951da177e4SLinus Torvalds filp->f_pos = pos; 309699f89551SEric W. Biederman put_task_struct(leader); 309799f89551SEric W. Biederman out_no_task: 30981da177e4SLinus Torvalds return retval; 30991da177e4SLinus Torvalds } 31006e66b52bSEric W. Biederman 31016e66b52bSEric W. Biederman static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) 31026e66b52bSEric W. Biederman { 31036e66b52bSEric W. Biederman struct inode *inode = dentry->d_inode; 310499f89551SEric W. Biederman struct task_struct *p = get_proc_task(inode); 31056e66b52bSEric W. Biederman generic_fillattr(inode, stat); 31066e66b52bSEric W. Biederman 310799f89551SEric W. Biederman if (p) { 310899f89551SEric W. Biederman stat->nlink += get_nr_threads(p); 310999f89551SEric W. Biederman put_task_struct(p); 31106e66b52bSEric W. Biederman } 31116e66b52bSEric W. Biederman 31126e66b52bSEric W. Biederman return 0; 31136e66b52bSEric W. Biederman } 311428a6d671SEric W. Biederman 3115c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations = { 311628a6d671SEric W. Biederman .lookup = proc_task_lookup, 311728a6d671SEric W. Biederman .getattr = proc_task_getattr, 311828a6d671SEric W. Biederman .setattr = proc_setattr, 311928a6d671SEric W. Biederman }; 312028a6d671SEric W. Biederman 312100977a59SArjan van de Ven static const struct file_operations proc_task_operations = { 312228a6d671SEric W. Biederman .read = generic_read_dir, 312328a6d671SEric W. Biederman .readdir = proc_task_readdir, 312428a6d671SEric W. Biederman }; 3125