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> 561da177e4SLinus Torvalds #include <linux/init.h> 5716f7e0feSRandy Dunlap #include <linux/capability.h> 581da177e4SLinus Torvalds #include <linux/file.h> 599f3acc31SAl Viro #include <linux/fdtable.h> 601da177e4SLinus Torvalds #include <linux/string.h> 611da177e4SLinus Torvalds #include <linux/seq_file.h> 621da177e4SLinus Torvalds #include <linux/namei.h> 636b3286edSKirill Korotaev #include <linux/mnt_namespace.h> 641da177e4SLinus Torvalds #include <linux/mm.h> 65b835996fSDipankar Sarma #include <linux/rcupdate.h> 661da177e4SLinus Torvalds #include <linux/kallsyms.h> 67d85f50d5SNeil Horman #include <linux/resource.h> 685096add8SKees Cook #include <linux/module.h> 691da177e4SLinus Torvalds #include <linux/mount.h> 701da177e4SLinus Torvalds #include <linux/security.h> 711da177e4SLinus Torvalds #include <linux/ptrace.h> 72a424316cSPaul Menage #include <linux/cgroup.h> 731da177e4SLinus Torvalds #include <linux/cpuset.h> 741da177e4SLinus Torvalds #include <linux/audit.h> 755addc5ddSAl Viro #include <linux/poll.h> 761651e14eSSerge E. Hallyn #include <linux/nsproxy.h> 778ac773b4SAlexey Dobriyan #include <linux/oom.h> 783cb4a0bbSKawai, Hidehiro #include <linux/elf.h> 7960347f67SPavel Emelyanov #include <linux/pid_namespace.h> 801da177e4SLinus Torvalds #include "internal.h" 811da177e4SLinus Torvalds 820f2fe20fSEric W. Biederman /* NOTE: 830f2fe20fSEric W. Biederman * Implementing inode permission operations in /proc is almost 840f2fe20fSEric W. Biederman * certainly an error. Permission checks need to happen during 850f2fe20fSEric W. Biederman * each system call not at open time. The reason is that most of 860f2fe20fSEric W. Biederman * what we wish to check for permissions in /proc varies at runtime. 870f2fe20fSEric W. Biederman * 880f2fe20fSEric W. Biederman * The classic example of a problem is opening file descriptors 890f2fe20fSEric W. Biederman * in /proc for a task before it execs a suid executable. 900f2fe20fSEric W. Biederman */ 910f2fe20fSEric W. Biederman 921da177e4SLinus Torvalds struct pid_entry { 931da177e4SLinus Torvalds char *name; 94c5141e6dSEric Dumazet int len; 951da177e4SLinus Torvalds mode_t mode; 96c5ef1c42SArjan van de Ven const struct inode_operations *iop; 9700977a59SArjan van de Ven const struct file_operations *fop; 9820cdc894SEric W. Biederman union proc_op op; 991da177e4SLinus Torvalds }; 1001da177e4SLinus Torvalds 10161a28784SEric W. Biederman #define NOD(NAME, MODE, IOP, FOP, OP) { \ 10220cdc894SEric W. Biederman .name = (NAME), \ 103c5141e6dSEric Dumazet .len = sizeof(NAME) - 1, \ 10420cdc894SEric W. Biederman .mode = MODE, \ 10520cdc894SEric W. Biederman .iop = IOP, \ 10620cdc894SEric W. Biederman .fop = FOP, \ 10720cdc894SEric W. Biederman .op = OP, \ 10820cdc894SEric W. Biederman } 10920cdc894SEric W. Biederman 11061a28784SEric W. Biederman #define DIR(NAME, MODE, OTYPE) \ 11161a28784SEric W. Biederman NOD(NAME, (S_IFDIR|(MODE)), \ 11220cdc894SEric W. Biederman &proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations, \ 11320cdc894SEric W. Biederman {} ) 11461a28784SEric W. Biederman #define LNK(NAME, OTYPE) \ 11561a28784SEric W. Biederman NOD(NAME, (S_IFLNK|S_IRWXUGO), \ 11620cdc894SEric W. Biederman &proc_pid_link_inode_operations, NULL, \ 11720cdc894SEric W. Biederman { .proc_get_link = &proc_##OTYPE##_link } ) 11861a28784SEric W. Biederman #define REG(NAME, MODE, OTYPE) \ 11961a28784SEric W. Biederman NOD(NAME, (S_IFREG|(MODE)), NULL, \ 12020cdc894SEric W. Biederman &proc_##OTYPE##_operations, {}) 12161a28784SEric W. Biederman #define INF(NAME, MODE, OTYPE) \ 12261a28784SEric W. Biederman NOD(NAME, (S_IFREG|(MODE)), \ 12320cdc894SEric W. Biederman NULL, &proc_info_file_operations, \ 12420cdc894SEric W. Biederman { .proc_read = &proc_##OTYPE } ) 125be614086SEric W. Biederman #define ONE(NAME, MODE, OTYPE) \ 126be614086SEric W. Biederman NOD(NAME, (S_IFREG|(MODE)), \ 127be614086SEric W. Biederman NULL, &proc_single_file_operations, \ 128be614086SEric W. Biederman { .proc_show = &proc_##OTYPE } ) 1291da177e4SLinus Torvalds 130*aed54175SVegard Nossum /* 131*aed54175SVegard Nossum * Count the number of hardlinks for the pid_entry table, excluding the . 132*aed54175SVegard Nossum * and .. links. 133*aed54175SVegard Nossum */ 134*aed54175SVegard Nossum static unsigned int pid_entry_count_dirs(const struct pid_entry *entries, 135*aed54175SVegard Nossum unsigned int n) 136*aed54175SVegard Nossum { 137*aed54175SVegard Nossum unsigned int i; 138*aed54175SVegard Nossum unsigned int count; 139*aed54175SVegard Nossum 140*aed54175SVegard Nossum count = 0; 141*aed54175SVegard Nossum for (i = 0; i < n; ++i) { 142*aed54175SVegard Nossum if (S_ISDIR(entries[i].mode)) 143*aed54175SVegard Nossum ++count; 144*aed54175SVegard Nossum } 145*aed54175SVegard Nossum 146*aed54175SVegard Nossum return count; 147*aed54175SVegard Nossum } 148*aed54175SVegard Nossum 1495096add8SKees Cook int maps_protect; 1505096add8SKees Cook EXPORT_SYMBOL(maps_protect); 1515096add8SKees Cook 1520494f6ecSMiklos Szeredi static struct fs_struct *get_fs_struct(struct task_struct *task) 1531da177e4SLinus Torvalds { 1541da177e4SLinus Torvalds struct fs_struct *fs; 1550494f6ecSMiklos Szeredi task_lock(task); 1560494f6ecSMiklos Szeredi fs = task->fs; 1571da177e4SLinus Torvalds if(fs) 1581da177e4SLinus Torvalds atomic_inc(&fs->count); 1590494f6ecSMiklos Szeredi task_unlock(task); 1600494f6ecSMiklos Szeredi return fs; 1610494f6ecSMiklos Szeredi } 1620494f6ecSMiklos Szeredi 16399f89551SEric W. Biederman static int get_nr_threads(struct task_struct *tsk) 16499f89551SEric W. Biederman { 16599f89551SEric W. Biederman /* Must be called with the rcu_read_lock held */ 16699f89551SEric W. Biederman unsigned long flags; 16799f89551SEric W. Biederman int count = 0; 16899f89551SEric W. Biederman 16999f89551SEric W. Biederman if (lock_task_sighand(tsk, &flags)) { 17099f89551SEric W. Biederman count = atomic_read(&tsk->signal->count); 17199f89551SEric W. Biederman unlock_task_sighand(tsk, &flags); 17299f89551SEric W. Biederman } 17399f89551SEric W. Biederman return count; 17499f89551SEric W. Biederman } 17599f89551SEric W. Biederman 1763dcd25f3SJan Blunck static int proc_cwd_link(struct inode *inode, struct path *path) 1770494f6ecSMiklos Szeredi { 17899f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 17999f89551SEric W. Biederman struct fs_struct *fs = NULL; 1800494f6ecSMiklos Szeredi int result = -ENOENT; 18199f89551SEric W. Biederman 18299f89551SEric W. Biederman if (task) { 18399f89551SEric W. Biederman fs = get_fs_struct(task); 18499f89551SEric W. Biederman put_task_struct(task); 18599f89551SEric W. Biederman } 1861da177e4SLinus Torvalds if (fs) { 1871da177e4SLinus Torvalds read_lock(&fs->lock); 1883dcd25f3SJan Blunck *path = fs->pwd; 1893dcd25f3SJan Blunck path_get(&fs->pwd); 1901da177e4SLinus Torvalds read_unlock(&fs->lock); 1911da177e4SLinus Torvalds result = 0; 1921da177e4SLinus Torvalds put_fs_struct(fs); 1931da177e4SLinus Torvalds } 1941da177e4SLinus Torvalds return result; 1951da177e4SLinus Torvalds } 1961da177e4SLinus Torvalds 1973dcd25f3SJan Blunck static int proc_root_link(struct inode *inode, struct path *path) 1981da177e4SLinus Torvalds { 19999f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 20099f89551SEric W. Biederman struct fs_struct *fs = NULL; 2011da177e4SLinus Torvalds int result = -ENOENT; 20299f89551SEric W. Biederman 20399f89551SEric W. Biederman if (task) { 20499f89551SEric W. Biederman fs = get_fs_struct(task); 20599f89551SEric W. Biederman put_task_struct(task); 20699f89551SEric W. Biederman } 2071da177e4SLinus Torvalds if (fs) { 2081da177e4SLinus Torvalds read_lock(&fs->lock); 2093dcd25f3SJan Blunck *path = fs->root; 2103dcd25f3SJan Blunck path_get(&fs->root); 2111da177e4SLinus Torvalds read_unlock(&fs->lock); 2121da177e4SLinus Torvalds result = 0; 2131da177e4SLinus Torvalds put_fs_struct(fs); 2141da177e4SLinus Torvalds } 2151da177e4SLinus Torvalds return result; 2161da177e4SLinus Torvalds } 2171da177e4SLinus Torvalds 218638fa202SRoland McGrath /* 219638fa202SRoland McGrath * Return zero if current may access user memory in @task, -error if not. 220638fa202SRoland McGrath */ 221638fa202SRoland McGrath static int check_mem_permission(struct task_struct *task) 222638fa202SRoland McGrath { 223638fa202SRoland McGrath /* 224638fa202SRoland McGrath * A task can always look at itself, in case it chooses 225638fa202SRoland McGrath * to use system calls instead of load instructions. 226638fa202SRoland McGrath */ 227638fa202SRoland McGrath if (task == current) 228638fa202SRoland McGrath return 0; 229638fa202SRoland McGrath 230638fa202SRoland McGrath /* 231638fa202SRoland McGrath * If current is actively ptrace'ing, and would also be 232638fa202SRoland McGrath * permitted to freshly attach with ptrace now, permit it. 233638fa202SRoland McGrath */ 234638fa202SRoland McGrath if (task->parent == current && (task->ptrace & PT_PTRACED) && 235638fa202SRoland McGrath task_is_stopped_or_traced(task) && 236638fa202SRoland McGrath ptrace_may_attach(task)) 237638fa202SRoland McGrath return 0; 238638fa202SRoland McGrath 239638fa202SRoland McGrath /* 240638fa202SRoland McGrath * Noone else is allowed. 241638fa202SRoland McGrath */ 242638fa202SRoland McGrath return -EPERM; 243638fa202SRoland McGrath } 2441da177e4SLinus Torvalds 245831830b5SAl Viro struct mm_struct *mm_for_maps(struct task_struct *task) 246831830b5SAl Viro { 247831830b5SAl Viro struct mm_struct *mm = get_task_mm(task); 248831830b5SAl Viro if (!mm) 249831830b5SAl Viro return NULL; 250831830b5SAl Viro down_read(&mm->mmap_sem); 251831830b5SAl Viro task_lock(task); 252831830b5SAl Viro if (task->mm != mm) 253831830b5SAl Viro goto out; 254831830b5SAl Viro if (task->mm != current->mm && __ptrace_may_attach(task) < 0) 255831830b5SAl Viro goto out; 256831830b5SAl Viro task_unlock(task); 257831830b5SAl Viro return mm; 258831830b5SAl Viro out: 259831830b5SAl Viro task_unlock(task); 260831830b5SAl Viro up_read(&mm->mmap_sem); 261831830b5SAl Viro mmput(mm); 262831830b5SAl Viro return NULL; 263831830b5SAl Viro } 264831830b5SAl Viro 2651da177e4SLinus Torvalds static int proc_pid_cmdline(struct task_struct *task, char * buffer) 2661da177e4SLinus Torvalds { 2671da177e4SLinus Torvalds int res = 0; 2681da177e4SLinus Torvalds unsigned int len; 2691da177e4SLinus Torvalds struct mm_struct *mm = get_task_mm(task); 2701da177e4SLinus Torvalds if (!mm) 2711da177e4SLinus Torvalds goto out; 2721da177e4SLinus Torvalds if (!mm->arg_end) 2731da177e4SLinus Torvalds goto out_mm; /* Shh! No looking before we're done */ 2741da177e4SLinus Torvalds 2751da177e4SLinus Torvalds len = mm->arg_end - mm->arg_start; 2761da177e4SLinus Torvalds 2771da177e4SLinus Torvalds if (len > PAGE_SIZE) 2781da177e4SLinus Torvalds len = PAGE_SIZE; 2791da177e4SLinus Torvalds 2801da177e4SLinus Torvalds res = access_process_vm(task, mm->arg_start, buffer, len, 0); 2811da177e4SLinus Torvalds 2821da177e4SLinus Torvalds // If the nul at the end of args has been overwritten, then 2831da177e4SLinus Torvalds // assume application is using setproctitle(3). 2841da177e4SLinus Torvalds if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) { 2851da177e4SLinus Torvalds len = strnlen(buffer, res); 2861da177e4SLinus Torvalds if (len < res) { 2871da177e4SLinus Torvalds res = len; 2881da177e4SLinus Torvalds } else { 2891da177e4SLinus Torvalds len = mm->env_end - mm->env_start; 2901da177e4SLinus Torvalds if (len > PAGE_SIZE - res) 2911da177e4SLinus Torvalds len = PAGE_SIZE - res; 2921da177e4SLinus Torvalds res += access_process_vm(task, mm->env_start, buffer+res, len, 0); 2931da177e4SLinus Torvalds res = strnlen(buffer, res); 2941da177e4SLinus Torvalds } 2951da177e4SLinus Torvalds } 2961da177e4SLinus Torvalds out_mm: 2971da177e4SLinus Torvalds mmput(mm); 2981da177e4SLinus Torvalds out: 2991da177e4SLinus Torvalds return res; 3001da177e4SLinus Torvalds } 3011da177e4SLinus Torvalds 3021da177e4SLinus Torvalds static int proc_pid_auxv(struct task_struct *task, char *buffer) 3031da177e4SLinus Torvalds { 3041da177e4SLinus Torvalds int res = 0; 3051da177e4SLinus Torvalds struct mm_struct *mm = get_task_mm(task); 3061da177e4SLinus Torvalds if (mm) { 3071da177e4SLinus Torvalds unsigned int nwords = 0; 3081da177e4SLinus Torvalds do 3091da177e4SLinus Torvalds nwords += 2; 3101da177e4SLinus Torvalds while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */ 3111da177e4SLinus Torvalds res = nwords * sizeof(mm->saved_auxv[0]); 3121da177e4SLinus Torvalds if (res > PAGE_SIZE) 3131da177e4SLinus Torvalds res = PAGE_SIZE; 3141da177e4SLinus Torvalds memcpy(buffer, mm->saved_auxv, res); 3151da177e4SLinus Torvalds mmput(mm); 3161da177e4SLinus Torvalds } 3171da177e4SLinus Torvalds return res; 3181da177e4SLinus Torvalds } 3191da177e4SLinus Torvalds 3201da177e4SLinus Torvalds 3211da177e4SLinus Torvalds #ifdef CONFIG_KALLSYMS 3221da177e4SLinus Torvalds /* 3231da177e4SLinus Torvalds * Provides a wchan file via kallsyms in a proper one-value-per-file format. 3241da177e4SLinus Torvalds * Returns the resolved symbol. If that fails, simply return the address. 3251da177e4SLinus Torvalds */ 3261da177e4SLinus Torvalds static int proc_pid_wchan(struct task_struct *task, char *buffer) 3271da177e4SLinus Torvalds { 328ffb45122SAlexey Dobriyan unsigned long wchan; 3299281aceaSTejun Heo char symname[KSYM_NAME_LEN]; 3301da177e4SLinus Torvalds 3311da177e4SLinus Torvalds wchan = get_wchan(task); 3321da177e4SLinus Torvalds 3339d65cb4aSAlexey Dobriyan if (lookup_symbol_name(wchan, symname) < 0) 3341da177e4SLinus Torvalds return sprintf(buffer, "%lu", wchan); 3359d65cb4aSAlexey Dobriyan else 3369d65cb4aSAlexey Dobriyan return sprintf(buffer, "%s", symname); 3371da177e4SLinus Torvalds } 3381da177e4SLinus Torvalds #endif /* CONFIG_KALLSYMS */ 3391da177e4SLinus Torvalds 3401da177e4SLinus Torvalds #ifdef CONFIG_SCHEDSTATS 3411da177e4SLinus Torvalds /* 3421da177e4SLinus Torvalds * Provides /proc/PID/schedstat 3431da177e4SLinus Torvalds */ 3441da177e4SLinus Torvalds static int proc_pid_schedstat(struct task_struct *task, char *buffer) 3451da177e4SLinus Torvalds { 346172ba844SBalbir Singh return sprintf(buffer, "%llu %llu %lu\n", 3471da177e4SLinus Torvalds task->sched_info.cpu_time, 3481da177e4SLinus Torvalds task->sched_info.run_delay, 3492d72376bSIngo Molnar task->sched_info.pcount); 3501da177e4SLinus Torvalds } 3511da177e4SLinus Torvalds #endif 3521da177e4SLinus Torvalds 3539745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP 3549745512cSArjan van de Ven static int lstats_show_proc(struct seq_file *m, void *v) 3559745512cSArjan van de Ven { 3569745512cSArjan van de Ven int i; 35713d77c37SHiroshi Shimamoto struct inode *inode = m->private; 35813d77c37SHiroshi Shimamoto struct task_struct *task = get_proc_task(inode); 3599745512cSArjan van de Ven 36013d77c37SHiroshi Shimamoto if (!task) 36113d77c37SHiroshi Shimamoto return -ESRCH; 36213d77c37SHiroshi Shimamoto seq_puts(m, "Latency Top version : v0.1\n"); 3639745512cSArjan van de Ven for (i = 0; i < 32; i++) { 3649745512cSArjan van de Ven if (task->latency_record[i].backtrace[0]) { 3659745512cSArjan van de Ven int q; 3669745512cSArjan van de Ven seq_printf(m, "%i %li %li ", 3679745512cSArjan van de Ven task->latency_record[i].count, 3689745512cSArjan van de Ven task->latency_record[i].time, 3699745512cSArjan van de Ven task->latency_record[i].max); 3709745512cSArjan van de Ven for (q = 0; q < LT_BACKTRACEDEPTH; q++) { 3719745512cSArjan van de Ven char sym[KSYM_NAME_LEN]; 3729745512cSArjan van de Ven char *c; 3739745512cSArjan van de Ven if (!task->latency_record[i].backtrace[q]) 3749745512cSArjan van de Ven break; 3759745512cSArjan van de Ven if (task->latency_record[i].backtrace[q] == ULONG_MAX) 3769745512cSArjan van de Ven break; 3779745512cSArjan van de Ven sprint_symbol(sym, task->latency_record[i].backtrace[q]); 3789745512cSArjan van de Ven c = strchr(sym, '+'); 3799745512cSArjan van de Ven if (c) 3809745512cSArjan van de Ven *c = 0; 3819745512cSArjan van de Ven seq_printf(m, "%s ", sym); 3829745512cSArjan van de Ven } 3839745512cSArjan van de Ven seq_printf(m, "\n"); 3849745512cSArjan van de Ven } 3859745512cSArjan van de Ven 3869745512cSArjan van de Ven } 38713d77c37SHiroshi Shimamoto put_task_struct(task); 3889745512cSArjan van de Ven return 0; 3899745512cSArjan van de Ven } 3909745512cSArjan van de Ven 3919745512cSArjan van de Ven static int lstats_open(struct inode *inode, struct file *file) 3929745512cSArjan van de Ven { 39313d77c37SHiroshi Shimamoto return single_open(file, lstats_show_proc, inode); 394d6643d12SHiroshi Shimamoto } 395d6643d12SHiroshi Shimamoto 3969745512cSArjan van de Ven static ssize_t lstats_write(struct file *file, const char __user *buf, 3979745512cSArjan van de Ven size_t count, loff_t *offs) 3989745512cSArjan van de Ven { 39913d77c37SHiroshi Shimamoto struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 4009745512cSArjan van de Ven 40113d77c37SHiroshi Shimamoto if (!task) 40213d77c37SHiroshi Shimamoto return -ESRCH; 4039745512cSArjan van de Ven clear_all_latency_tracing(task); 40413d77c37SHiroshi Shimamoto put_task_struct(task); 4059745512cSArjan van de Ven 4069745512cSArjan van de Ven return count; 4079745512cSArjan van de Ven } 4089745512cSArjan van de Ven 4099745512cSArjan van de Ven static const struct file_operations proc_lstats_operations = { 4109745512cSArjan van de Ven .open = lstats_open, 4119745512cSArjan van de Ven .read = seq_read, 4129745512cSArjan van de Ven .write = lstats_write, 4139745512cSArjan van de Ven .llseek = seq_lseek, 41413d77c37SHiroshi Shimamoto .release = single_release, 4159745512cSArjan van de Ven }; 4169745512cSArjan van de Ven 4179745512cSArjan van de Ven #endif 4189745512cSArjan van de Ven 4191da177e4SLinus Torvalds /* The badness from the OOM killer */ 4201da177e4SLinus Torvalds unsigned long badness(struct task_struct *p, unsigned long uptime); 4211da177e4SLinus Torvalds static int proc_oom_score(struct task_struct *task, char *buffer) 4221da177e4SLinus Torvalds { 4231da177e4SLinus Torvalds unsigned long points; 4241da177e4SLinus Torvalds struct timespec uptime; 4251da177e4SLinus Torvalds 4261da177e4SLinus Torvalds do_posix_clock_monotonic_gettime(&uptime); 42719c5d45aSAlexey Dobriyan read_lock(&tasklist_lock); 4281da177e4SLinus Torvalds points = badness(task, uptime.tv_sec); 42919c5d45aSAlexey Dobriyan read_unlock(&tasklist_lock); 4301da177e4SLinus Torvalds return sprintf(buffer, "%lu\n", points); 4311da177e4SLinus Torvalds } 4321da177e4SLinus Torvalds 433d85f50d5SNeil Horman struct limit_names { 434d85f50d5SNeil Horman char *name; 435d85f50d5SNeil Horman char *unit; 436d85f50d5SNeil Horman }; 437d85f50d5SNeil Horman 438d85f50d5SNeil Horman static const struct limit_names lnames[RLIM_NLIMITS] = { 439d85f50d5SNeil Horman [RLIMIT_CPU] = {"Max cpu time", "ms"}, 440d85f50d5SNeil Horman [RLIMIT_FSIZE] = {"Max file size", "bytes"}, 441d85f50d5SNeil Horman [RLIMIT_DATA] = {"Max data size", "bytes"}, 442d85f50d5SNeil Horman [RLIMIT_STACK] = {"Max stack size", "bytes"}, 443d85f50d5SNeil Horman [RLIMIT_CORE] = {"Max core file size", "bytes"}, 444d85f50d5SNeil Horman [RLIMIT_RSS] = {"Max resident set", "bytes"}, 445d85f50d5SNeil Horman [RLIMIT_NPROC] = {"Max processes", "processes"}, 446d85f50d5SNeil Horman [RLIMIT_NOFILE] = {"Max open files", "files"}, 447d85f50d5SNeil Horman [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"}, 448d85f50d5SNeil Horman [RLIMIT_AS] = {"Max address space", "bytes"}, 449d85f50d5SNeil Horman [RLIMIT_LOCKS] = {"Max file locks", "locks"}, 450d85f50d5SNeil Horman [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"}, 451d85f50d5SNeil Horman [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"}, 452d85f50d5SNeil Horman [RLIMIT_NICE] = {"Max nice priority", NULL}, 453d85f50d5SNeil Horman [RLIMIT_RTPRIO] = {"Max realtime priority", NULL}, 4548808117cSEugene Teo [RLIMIT_RTTIME] = {"Max realtime timeout", "us"}, 455d85f50d5SNeil Horman }; 456d85f50d5SNeil Horman 457d85f50d5SNeil Horman /* Display limits for a process */ 458d85f50d5SNeil Horman static int proc_pid_limits(struct task_struct *task, char *buffer) 459d85f50d5SNeil Horman { 460d85f50d5SNeil Horman unsigned int i; 461d85f50d5SNeil Horman int count = 0; 462d85f50d5SNeil Horman unsigned long flags; 463d85f50d5SNeil Horman char *bufptr = buffer; 464d85f50d5SNeil Horman 465d85f50d5SNeil Horman struct rlimit rlim[RLIM_NLIMITS]; 466d85f50d5SNeil Horman 467d85f50d5SNeil Horman rcu_read_lock(); 468d85f50d5SNeil Horman if (!lock_task_sighand(task,&flags)) { 469d85f50d5SNeil Horman rcu_read_unlock(); 470d85f50d5SNeil Horman return 0; 471d85f50d5SNeil Horman } 472d85f50d5SNeil Horman memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS); 473d85f50d5SNeil Horman unlock_task_sighand(task, &flags); 474d85f50d5SNeil Horman rcu_read_unlock(); 475d85f50d5SNeil Horman 476d85f50d5SNeil Horman /* 477d85f50d5SNeil Horman * print the file header 478d85f50d5SNeil Horman */ 479d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n", 480d85f50d5SNeil Horman "Limit", "Soft Limit", "Hard Limit", "Units"); 481d85f50d5SNeil Horman 482d85f50d5SNeil Horman for (i = 0; i < RLIM_NLIMITS; i++) { 483d85f50d5SNeil Horman if (rlim[i].rlim_cur == RLIM_INFINITY) 484d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-25s %-20s ", 485d85f50d5SNeil Horman lnames[i].name, "unlimited"); 486d85f50d5SNeil Horman else 487d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-25s %-20lu ", 488d85f50d5SNeil Horman lnames[i].name, rlim[i].rlim_cur); 489d85f50d5SNeil Horman 490d85f50d5SNeil Horman if (rlim[i].rlim_max == RLIM_INFINITY) 491d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-20s ", "unlimited"); 492d85f50d5SNeil Horman else 493d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-20lu ", 494d85f50d5SNeil Horman rlim[i].rlim_max); 495d85f50d5SNeil Horman 496d85f50d5SNeil Horman if (lnames[i].unit) 497d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-10s\n", 498d85f50d5SNeil Horman lnames[i].unit); 499d85f50d5SNeil Horman else 500d85f50d5SNeil Horman count += sprintf(&bufptr[count], "\n"); 501d85f50d5SNeil Horman } 502d85f50d5SNeil Horman 503d85f50d5SNeil Horman return count; 504d85f50d5SNeil Horman } 505d85f50d5SNeil Horman 5061da177e4SLinus Torvalds /************************************************************************/ 5071da177e4SLinus Torvalds /* Here the fs part begins */ 5081da177e4SLinus Torvalds /************************************************************************/ 5091da177e4SLinus Torvalds 5101da177e4SLinus Torvalds /* permission checks */ 511778c1144SEric W. Biederman static int proc_fd_access_allowed(struct inode *inode) 5121da177e4SLinus Torvalds { 513778c1144SEric W. Biederman struct task_struct *task; 514778c1144SEric W. Biederman int allowed = 0; 515df26c40eSEric W. Biederman /* Allow access to a task's file descriptors if it is us or we 516df26c40eSEric W. Biederman * may use ptrace attach to the process and find out that 517df26c40eSEric W. Biederman * information. 518778c1144SEric W. Biederman */ 519778c1144SEric W. Biederman task = get_proc_task(inode); 520df26c40eSEric W. Biederman if (task) { 521778c1144SEric W. Biederman allowed = ptrace_may_attach(task); 522778c1144SEric W. Biederman put_task_struct(task); 523df26c40eSEric W. Biederman } 524778c1144SEric W. Biederman return allowed; 5251da177e4SLinus Torvalds } 5261da177e4SLinus Torvalds 5276d76fa58SLinus Torvalds static int proc_setattr(struct dentry *dentry, struct iattr *attr) 5286d76fa58SLinus Torvalds { 5296d76fa58SLinus Torvalds int error; 5306d76fa58SLinus Torvalds struct inode *inode = dentry->d_inode; 5316d76fa58SLinus Torvalds 5326d76fa58SLinus Torvalds if (attr->ia_valid & ATTR_MODE) 5336d76fa58SLinus Torvalds return -EPERM; 5346d76fa58SLinus Torvalds 5356d76fa58SLinus Torvalds error = inode_change_ok(inode, attr); 5366d76fa58SLinus Torvalds if (!error) 5376d76fa58SLinus Torvalds error = inode_setattr(inode, attr); 5386d76fa58SLinus Torvalds return error; 5396d76fa58SLinus Torvalds } 5406d76fa58SLinus Torvalds 541c5ef1c42SArjan van de Ven static const struct inode_operations proc_def_inode_operations = { 5426d76fa58SLinus Torvalds .setattr = proc_setattr, 5436d76fa58SLinus Torvalds }; 5446d76fa58SLinus Torvalds 545a1a2c409SMiklos Szeredi static int mounts_open_common(struct inode *inode, struct file *file, 546a1a2c409SMiklos Szeredi const struct seq_operations *op) 5471da177e4SLinus Torvalds { 54899f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 549cf7b708cSPavel Emelyanov struct nsproxy *nsp; 5506b3286edSKirill Korotaev struct mnt_namespace *ns = NULL; 551a1a2c409SMiklos Szeredi struct fs_struct *fs = NULL; 552a1a2c409SMiklos Szeredi struct path root; 5535addc5ddSAl Viro struct proc_mounts *p; 5545addc5ddSAl Viro int ret = -EINVAL; 5555addc5ddSAl Viro 55699f89551SEric W. Biederman if (task) { 557cf7b708cSPavel Emelyanov rcu_read_lock(); 558cf7b708cSPavel Emelyanov nsp = task_nsproxy(task); 559cf7b708cSPavel Emelyanov if (nsp) { 560cf7b708cSPavel Emelyanov ns = nsp->mnt_ns; 5616b3286edSKirill Korotaev if (ns) 5626b3286edSKirill Korotaev get_mnt_ns(ns); 563863c4702SAlexey Dobriyan } 564cf7b708cSPavel Emelyanov rcu_read_unlock(); 565a1a2c409SMiklos Szeredi if (ns) 566a1a2c409SMiklos Szeredi fs = get_fs_struct(task); 56799f89551SEric W. Biederman put_task_struct(task); 56899f89551SEric W. Biederman } 5691da177e4SLinus Torvalds 570a1a2c409SMiklos Szeredi if (!ns) 571a1a2c409SMiklos Szeredi goto err; 572a1a2c409SMiklos Szeredi if (!fs) 573a1a2c409SMiklos Szeredi goto err_put_ns; 574a1a2c409SMiklos Szeredi 575a1a2c409SMiklos Szeredi read_lock(&fs->lock); 576a1a2c409SMiklos Szeredi root = fs->root; 577a1a2c409SMiklos Szeredi path_get(&root); 578a1a2c409SMiklos Szeredi read_unlock(&fs->lock); 579a1a2c409SMiklos Szeredi put_fs_struct(fs); 580a1a2c409SMiklos Szeredi 5815addc5ddSAl Viro ret = -ENOMEM; 5825addc5ddSAl Viro p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL); 583a1a2c409SMiklos Szeredi if (!p) 584a1a2c409SMiklos Szeredi goto err_put_path; 585a1a2c409SMiklos Szeredi 5865addc5ddSAl Viro file->private_data = &p->m; 587a1a2c409SMiklos Szeredi ret = seq_open(file, op); 588a1a2c409SMiklos Szeredi if (ret) 589a1a2c409SMiklos Szeredi goto err_free; 590a1a2c409SMiklos Szeredi 591a1a2c409SMiklos Szeredi p->m.private = p; 592a1a2c409SMiklos Szeredi p->ns = ns; 593a1a2c409SMiklos Szeredi p->root = root; 5946b3286edSKirill Korotaev p->event = ns->event; 595a1a2c409SMiklos Szeredi 5965addc5ddSAl Viro return 0; 597a1a2c409SMiklos Szeredi 598a1a2c409SMiklos Szeredi err_free: 5995addc5ddSAl Viro kfree(p); 600a1a2c409SMiklos Szeredi err_put_path: 601a1a2c409SMiklos Szeredi path_put(&root); 602a1a2c409SMiklos Szeredi err_put_ns: 6036b3286edSKirill Korotaev put_mnt_ns(ns); 604a1a2c409SMiklos Szeredi err: 6051da177e4SLinus Torvalds return ret; 6061da177e4SLinus Torvalds } 6071da177e4SLinus Torvalds 6081da177e4SLinus Torvalds static int mounts_release(struct inode *inode, struct file *file) 6091da177e4SLinus Torvalds { 610a1a2c409SMiklos Szeredi struct proc_mounts *p = file->private_data; 611a1a2c409SMiklos Szeredi path_put(&p->root); 612a1a2c409SMiklos Szeredi put_mnt_ns(p->ns); 6131da177e4SLinus Torvalds return seq_release(inode, file); 6141da177e4SLinus Torvalds } 6151da177e4SLinus Torvalds 6165addc5ddSAl Viro static unsigned mounts_poll(struct file *file, poll_table *wait) 6175addc5ddSAl Viro { 6185addc5ddSAl Viro struct proc_mounts *p = file->private_data; 619a1a2c409SMiklos Szeredi struct mnt_namespace *ns = p->ns; 6205addc5ddSAl Viro unsigned res = 0; 6215addc5ddSAl Viro 6225addc5ddSAl Viro poll_wait(file, &ns->poll, wait); 6235addc5ddSAl Viro 6245addc5ddSAl Viro spin_lock(&vfsmount_lock); 6255addc5ddSAl Viro if (p->event != ns->event) { 6265addc5ddSAl Viro p->event = ns->event; 6275addc5ddSAl Viro res = POLLERR; 6285addc5ddSAl Viro } 6295addc5ddSAl Viro spin_unlock(&vfsmount_lock); 6305addc5ddSAl Viro 6315addc5ddSAl Viro return res; 6325addc5ddSAl Viro } 6335addc5ddSAl Viro 634a1a2c409SMiklos Szeredi static int mounts_open(struct inode *inode, struct file *file) 635a1a2c409SMiklos Szeredi { 636a1a2c409SMiklos Szeredi return mounts_open_common(inode, file, &mounts_op); 637a1a2c409SMiklos Szeredi } 638a1a2c409SMiklos Szeredi 63900977a59SArjan van de Ven static const struct file_operations proc_mounts_operations = { 6401da177e4SLinus Torvalds .open = mounts_open, 6411da177e4SLinus Torvalds .read = seq_read, 6421da177e4SLinus Torvalds .llseek = seq_lseek, 6431da177e4SLinus Torvalds .release = mounts_release, 6445addc5ddSAl Viro .poll = mounts_poll, 6451da177e4SLinus Torvalds }; 6461da177e4SLinus Torvalds 6472d4d4864SRam Pai static int mountinfo_open(struct inode *inode, struct file *file) 6482d4d4864SRam Pai { 6492d4d4864SRam Pai return mounts_open_common(inode, file, &mountinfo_op); 6502d4d4864SRam Pai } 6512d4d4864SRam Pai 6522d4d4864SRam Pai static const struct file_operations proc_mountinfo_operations = { 6532d4d4864SRam Pai .open = mountinfo_open, 6542d4d4864SRam Pai .read = seq_read, 6552d4d4864SRam Pai .llseek = seq_lseek, 6562d4d4864SRam Pai .release = mounts_release, 6572d4d4864SRam Pai .poll = mounts_poll, 6582d4d4864SRam Pai }; 6592d4d4864SRam Pai 660b4629fe2SChuck Lever static int mountstats_open(struct inode *inode, struct file *file) 661b4629fe2SChuck Lever { 662a1a2c409SMiklos Szeredi return mounts_open_common(inode, file, &mountstats_op); 663b4629fe2SChuck Lever } 664b4629fe2SChuck Lever 66500977a59SArjan van de Ven static const struct file_operations proc_mountstats_operations = { 666b4629fe2SChuck Lever .open = mountstats_open, 667b4629fe2SChuck Lever .read = seq_read, 668b4629fe2SChuck Lever .llseek = seq_lseek, 669b4629fe2SChuck Lever .release = mounts_release, 670b4629fe2SChuck Lever }; 671b4629fe2SChuck Lever 6721da177e4SLinus Torvalds #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */ 6731da177e4SLinus Torvalds 6741da177e4SLinus Torvalds static ssize_t proc_info_read(struct file * file, char __user * buf, 6751da177e4SLinus Torvalds size_t count, loff_t *ppos) 6761da177e4SLinus Torvalds { 6772fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 6781da177e4SLinus Torvalds unsigned long page; 6791da177e4SLinus Torvalds ssize_t length; 68099f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 68199f89551SEric W. Biederman 68299f89551SEric W. Biederman length = -ESRCH; 68399f89551SEric W. Biederman if (!task) 68499f89551SEric W. Biederman goto out_no_task; 6851da177e4SLinus Torvalds 6861da177e4SLinus Torvalds if (count > PROC_BLOCK_SIZE) 6871da177e4SLinus Torvalds count = PROC_BLOCK_SIZE; 68899f89551SEric W. Biederman 68999f89551SEric W. Biederman length = -ENOMEM; 690e12ba74dSMel Gorman if (!(page = __get_free_page(GFP_TEMPORARY))) 69199f89551SEric W. Biederman goto out; 6921da177e4SLinus Torvalds 6931da177e4SLinus Torvalds length = PROC_I(inode)->op.proc_read(task, (char*)page); 6941da177e4SLinus Torvalds 6951da177e4SLinus Torvalds if (length >= 0) 6961da177e4SLinus Torvalds length = simple_read_from_buffer(buf, count, ppos, (char *)page, length); 6971da177e4SLinus Torvalds free_page(page); 69899f89551SEric W. Biederman out: 69999f89551SEric W. Biederman put_task_struct(task); 70099f89551SEric W. Biederman out_no_task: 7011da177e4SLinus Torvalds return length; 7021da177e4SLinus Torvalds } 7031da177e4SLinus Torvalds 70400977a59SArjan van de Ven static const struct file_operations proc_info_file_operations = { 7051da177e4SLinus Torvalds .read = proc_info_read, 7061da177e4SLinus Torvalds }; 7071da177e4SLinus Torvalds 708be614086SEric W. Biederman static int proc_single_show(struct seq_file *m, void *v) 709be614086SEric W. Biederman { 710be614086SEric W. Biederman struct inode *inode = m->private; 711be614086SEric W. Biederman struct pid_namespace *ns; 712be614086SEric W. Biederman struct pid *pid; 713be614086SEric W. Biederman struct task_struct *task; 714be614086SEric W. Biederman int ret; 715be614086SEric W. Biederman 716be614086SEric W. Biederman ns = inode->i_sb->s_fs_info; 717be614086SEric W. Biederman pid = proc_pid(inode); 718be614086SEric W. Biederman task = get_pid_task(pid, PIDTYPE_PID); 719be614086SEric W. Biederman if (!task) 720be614086SEric W. Biederman return -ESRCH; 721be614086SEric W. Biederman 722be614086SEric W. Biederman ret = PROC_I(inode)->op.proc_show(m, ns, pid, task); 723be614086SEric W. Biederman 724be614086SEric W. Biederman put_task_struct(task); 725be614086SEric W. Biederman return ret; 726be614086SEric W. Biederman } 727be614086SEric W. Biederman 728be614086SEric W. Biederman static int proc_single_open(struct inode *inode, struct file *filp) 729be614086SEric W. Biederman { 730be614086SEric W. Biederman int ret; 731be614086SEric W. Biederman ret = single_open(filp, proc_single_show, NULL); 732be614086SEric W. Biederman if (!ret) { 733be614086SEric W. Biederman struct seq_file *m = filp->private_data; 734be614086SEric W. Biederman 735be614086SEric W. Biederman m->private = inode; 736be614086SEric W. Biederman } 737be614086SEric W. Biederman return ret; 738be614086SEric W. Biederman } 739be614086SEric W. Biederman 740be614086SEric W. Biederman static const struct file_operations proc_single_file_operations = { 741be614086SEric W. Biederman .open = proc_single_open, 742be614086SEric W. Biederman .read = seq_read, 743be614086SEric W. Biederman .llseek = seq_lseek, 744be614086SEric W. Biederman .release = single_release, 745be614086SEric W. Biederman }; 746be614086SEric W. Biederman 7471da177e4SLinus Torvalds static int mem_open(struct inode* inode, struct file* file) 7481da177e4SLinus Torvalds { 7491da177e4SLinus Torvalds file->private_data = (void*)((long)current->self_exec_id); 7501da177e4SLinus Torvalds return 0; 7511da177e4SLinus Torvalds } 7521da177e4SLinus Torvalds 7531da177e4SLinus Torvalds static ssize_t mem_read(struct file * file, char __user * buf, 7541da177e4SLinus Torvalds size_t count, loff_t *ppos) 7551da177e4SLinus Torvalds { 7562fddfeefSJosef "Jeff" Sipek struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode); 7571da177e4SLinus Torvalds char *page; 7581da177e4SLinus Torvalds unsigned long src = *ppos; 7591da177e4SLinus Torvalds int ret = -ESRCH; 7601da177e4SLinus Torvalds struct mm_struct *mm; 7611da177e4SLinus Torvalds 76299f89551SEric W. Biederman if (!task) 76399f89551SEric W. Biederman goto out_no_task; 76499f89551SEric W. Biederman 765638fa202SRoland McGrath if (check_mem_permission(task)) 7661da177e4SLinus Torvalds goto out; 7671da177e4SLinus Torvalds 7681da177e4SLinus Torvalds ret = -ENOMEM; 769e12ba74dSMel Gorman page = (char *)__get_free_page(GFP_TEMPORARY); 7701da177e4SLinus Torvalds if (!page) 7711da177e4SLinus Torvalds goto out; 7721da177e4SLinus Torvalds 7731da177e4SLinus Torvalds ret = 0; 7741da177e4SLinus Torvalds 7751da177e4SLinus Torvalds mm = get_task_mm(task); 7761da177e4SLinus Torvalds if (!mm) 7771da177e4SLinus Torvalds goto out_free; 7781da177e4SLinus Torvalds 7791da177e4SLinus Torvalds ret = -EIO; 7801da177e4SLinus Torvalds 7811da177e4SLinus Torvalds if (file->private_data != (void*)((long)current->self_exec_id)) 7821da177e4SLinus Torvalds goto out_put; 7831da177e4SLinus Torvalds 7841da177e4SLinus Torvalds ret = 0; 7851da177e4SLinus Torvalds 7861da177e4SLinus Torvalds while (count > 0) { 7871da177e4SLinus Torvalds int this_len, retval; 7881da177e4SLinus Torvalds 7891da177e4SLinus Torvalds this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count; 7901da177e4SLinus Torvalds retval = access_process_vm(task, src, page, this_len, 0); 791638fa202SRoland McGrath if (!retval || check_mem_permission(task)) { 7921da177e4SLinus Torvalds if (!ret) 7931da177e4SLinus Torvalds ret = -EIO; 7941da177e4SLinus Torvalds break; 7951da177e4SLinus Torvalds } 7961da177e4SLinus Torvalds 7971da177e4SLinus Torvalds if (copy_to_user(buf, page, retval)) { 7981da177e4SLinus Torvalds ret = -EFAULT; 7991da177e4SLinus Torvalds break; 8001da177e4SLinus Torvalds } 8011da177e4SLinus Torvalds 8021da177e4SLinus Torvalds ret += retval; 8031da177e4SLinus Torvalds src += retval; 8041da177e4SLinus Torvalds buf += retval; 8051da177e4SLinus Torvalds count -= retval; 8061da177e4SLinus Torvalds } 8071da177e4SLinus Torvalds *ppos = src; 8081da177e4SLinus Torvalds 8091da177e4SLinus Torvalds out_put: 8101da177e4SLinus Torvalds mmput(mm); 8111da177e4SLinus Torvalds out_free: 8121da177e4SLinus Torvalds free_page((unsigned long) page); 8131da177e4SLinus Torvalds out: 81499f89551SEric W. Biederman put_task_struct(task); 81599f89551SEric W. Biederman out_no_task: 8161da177e4SLinus Torvalds return ret; 8171da177e4SLinus Torvalds } 8181da177e4SLinus Torvalds 8191da177e4SLinus Torvalds #define mem_write NULL 8201da177e4SLinus Torvalds 8211da177e4SLinus Torvalds #ifndef mem_write 8221da177e4SLinus Torvalds /* This is a security hazard */ 82363967fa9SGlauber de Oliveira Costa static ssize_t mem_write(struct file * file, const char __user *buf, 8241da177e4SLinus Torvalds size_t count, loff_t *ppos) 8251da177e4SLinus Torvalds { 826f7ca54f4SFrederik Deweerdt int copied; 8271da177e4SLinus Torvalds char *page; 8282fddfeefSJosef "Jeff" Sipek struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode); 8291da177e4SLinus Torvalds unsigned long dst = *ppos; 8301da177e4SLinus Torvalds 83199f89551SEric W. Biederman copied = -ESRCH; 83299f89551SEric W. Biederman if (!task) 83399f89551SEric W. Biederman goto out_no_task; 8341da177e4SLinus Torvalds 835638fa202SRoland McGrath if (check_mem_permission(task)) 83699f89551SEric W. Biederman goto out; 83799f89551SEric W. Biederman 83899f89551SEric W. Biederman copied = -ENOMEM; 839e12ba74dSMel Gorman page = (char *)__get_free_page(GFP_TEMPORARY); 8401da177e4SLinus Torvalds if (!page) 84199f89551SEric W. Biederman goto out; 8421da177e4SLinus Torvalds 843f7ca54f4SFrederik Deweerdt copied = 0; 8441da177e4SLinus Torvalds while (count > 0) { 8451da177e4SLinus Torvalds int this_len, retval; 8461da177e4SLinus Torvalds 8471da177e4SLinus Torvalds this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count; 8481da177e4SLinus Torvalds if (copy_from_user(page, buf, this_len)) { 8491da177e4SLinus Torvalds copied = -EFAULT; 8501da177e4SLinus Torvalds break; 8511da177e4SLinus Torvalds } 8521da177e4SLinus Torvalds retval = access_process_vm(task, dst, page, this_len, 1); 8531da177e4SLinus Torvalds if (!retval) { 8541da177e4SLinus Torvalds if (!copied) 8551da177e4SLinus Torvalds copied = -EIO; 8561da177e4SLinus Torvalds break; 8571da177e4SLinus Torvalds } 8581da177e4SLinus Torvalds copied += retval; 8591da177e4SLinus Torvalds buf += retval; 8601da177e4SLinus Torvalds dst += retval; 8611da177e4SLinus Torvalds count -= retval; 8621da177e4SLinus Torvalds } 8631da177e4SLinus Torvalds *ppos = dst; 8641da177e4SLinus Torvalds free_page((unsigned long) page); 86599f89551SEric W. Biederman out: 86699f89551SEric W. Biederman put_task_struct(task); 86799f89551SEric W. Biederman out_no_task: 8681da177e4SLinus Torvalds return copied; 8691da177e4SLinus Torvalds } 8701da177e4SLinus Torvalds #endif 8711da177e4SLinus Torvalds 87285863e47SMatt Mackall loff_t mem_lseek(struct file *file, loff_t offset, int orig) 8731da177e4SLinus Torvalds { 8741da177e4SLinus Torvalds switch (orig) { 8751da177e4SLinus Torvalds case 0: 8761da177e4SLinus Torvalds file->f_pos = offset; 8771da177e4SLinus Torvalds break; 8781da177e4SLinus Torvalds case 1: 8791da177e4SLinus Torvalds file->f_pos += offset; 8801da177e4SLinus Torvalds break; 8811da177e4SLinus Torvalds default: 8821da177e4SLinus Torvalds return -EINVAL; 8831da177e4SLinus Torvalds } 8841da177e4SLinus Torvalds force_successful_syscall_return(); 8851da177e4SLinus Torvalds return file->f_pos; 8861da177e4SLinus Torvalds } 8871da177e4SLinus Torvalds 88800977a59SArjan van de Ven static const struct file_operations proc_mem_operations = { 8891da177e4SLinus Torvalds .llseek = mem_lseek, 8901da177e4SLinus Torvalds .read = mem_read, 8911da177e4SLinus Torvalds .write = mem_write, 8921da177e4SLinus Torvalds .open = mem_open, 8931da177e4SLinus Torvalds }; 8941da177e4SLinus Torvalds 895315e28c8SJames Pearson static ssize_t environ_read(struct file *file, char __user *buf, 896315e28c8SJames Pearson size_t count, loff_t *ppos) 897315e28c8SJames Pearson { 898315e28c8SJames Pearson struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 899315e28c8SJames Pearson char *page; 900315e28c8SJames Pearson unsigned long src = *ppos; 901315e28c8SJames Pearson int ret = -ESRCH; 902315e28c8SJames Pearson struct mm_struct *mm; 903315e28c8SJames Pearson 904315e28c8SJames Pearson if (!task) 905315e28c8SJames Pearson goto out_no_task; 906315e28c8SJames Pearson 907315e28c8SJames Pearson if (!ptrace_may_attach(task)) 908315e28c8SJames Pearson goto out; 909315e28c8SJames Pearson 910315e28c8SJames Pearson ret = -ENOMEM; 911315e28c8SJames Pearson page = (char *)__get_free_page(GFP_TEMPORARY); 912315e28c8SJames Pearson if (!page) 913315e28c8SJames Pearson goto out; 914315e28c8SJames Pearson 915315e28c8SJames Pearson ret = 0; 916315e28c8SJames Pearson 917315e28c8SJames Pearson mm = get_task_mm(task); 918315e28c8SJames Pearson if (!mm) 919315e28c8SJames Pearson goto out_free; 920315e28c8SJames Pearson 921315e28c8SJames Pearson while (count > 0) { 922315e28c8SJames Pearson int this_len, retval, max_len; 923315e28c8SJames Pearson 924315e28c8SJames Pearson this_len = mm->env_end - (mm->env_start + src); 925315e28c8SJames Pearson 926315e28c8SJames Pearson if (this_len <= 0) 927315e28c8SJames Pearson break; 928315e28c8SJames Pearson 929315e28c8SJames Pearson max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count; 930315e28c8SJames Pearson this_len = (this_len > max_len) ? max_len : this_len; 931315e28c8SJames Pearson 932315e28c8SJames Pearson retval = access_process_vm(task, (mm->env_start + src), 933315e28c8SJames Pearson page, this_len, 0); 934315e28c8SJames Pearson 935315e28c8SJames Pearson if (retval <= 0) { 936315e28c8SJames Pearson ret = retval; 937315e28c8SJames Pearson break; 938315e28c8SJames Pearson } 939315e28c8SJames Pearson 940315e28c8SJames Pearson if (copy_to_user(buf, page, retval)) { 941315e28c8SJames Pearson ret = -EFAULT; 942315e28c8SJames Pearson break; 943315e28c8SJames Pearson } 944315e28c8SJames Pearson 945315e28c8SJames Pearson ret += retval; 946315e28c8SJames Pearson src += retval; 947315e28c8SJames Pearson buf += retval; 948315e28c8SJames Pearson count -= retval; 949315e28c8SJames Pearson } 950315e28c8SJames Pearson *ppos = src; 951315e28c8SJames Pearson 952315e28c8SJames Pearson mmput(mm); 953315e28c8SJames Pearson out_free: 954315e28c8SJames Pearson free_page((unsigned long) page); 955315e28c8SJames Pearson out: 956315e28c8SJames Pearson put_task_struct(task); 957315e28c8SJames Pearson out_no_task: 958315e28c8SJames Pearson return ret; 959315e28c8SJames Pearson } 960315e28c8SJames Pearson 961315e28c8SJames Pearson static const struct file_operations proc_environ_operations = { 962315e28c8SJames Pearson .read = environ_read, 963315e28c8SJames Pearson }; 964315e28c8SJames Pearson 9651da177e4SLinus Torvalds static ssize_t oom_adjust_read(struct file *file, char __user *buf, 9661da177e4SLinus Torvalds size_t count, loff_t *ppos) 9671da177e4SLinus Torvalds { 9682fddfeefSJosef "Jeff" Sipek struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode); 9698578cea7SEric W. Biederman char buffer[PROC_NUMBUF]; 9701da177e4SLinus Torvalds size_t len; 97199f89551SEric W. Biederman int oom_adjust; 9721da177e4SLinus Torvalds 97399f89551SEric W. Biederman if (!task) 97499f89551SEric W. Biederman return -ESRCH; 97599f89551SEric W. Biederman oom_adjust = task->oomkilladj; 97699f89551SEric W. Biederman put_task_struct(task); 97799f89551SEric W. Biederman 9788578cea7SEric W. Biederman len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust); 9790c28f287SAkinobu Mita 9800c28f287SAkinobu Mita return simple_read_from_buffer(buf, count, ppos, buffer, len); 9811da177e4SLinus Torvalds } 9821da177e4SLinus Torvalds 9831da177e4SLinus Torvalds static ssize_t oom_adjust_write(struct file *file, const char __user *buf, 9841da177e4SLinus Torvalds size_t count, loff_t *ppos) 9851da177e4SLinus Torvalds { 98699f89551SEric W. Biederman struct task_struct *task; 9878578cea7SEric W. Biederman char buffer[PROC_NUMBUF], *end; 9881da177e4SLinus Torvalds int oom_adjust; 9891da177e4SLinus Torvalds 9908578cea7SEric W. Biederman memset(buffer, 0, sizeof(buffer)); 9918578cea7SEric W. Biederman if (count > sizeof(buffer) - 1) 9928578cea7SEric W. Biederman count = sizeof(buffer) - 1; 9931da177e4SLinus Torvalds if (copy_from_user(buffer, buf, count)) 9941da177e4SLinus Torvalds return -EFAULT; 9951da177e4SLinus Torvalds oom_adjust = simple_strtol(buffer, &end, 0); 9968ac773b4SAlexey Dobriyan if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) && 9978ac773b4SAlexey Dobriyan oom_adjust != OOM_DISABLE) 9981da177e4SLinus Torvalds return -EINVAL; 9991da177e4SLinus Torvalds if (*end == '\n') 10001da177e4SLinus Torvalds end++; 10012fddfeefSJosef "Jeff" Sipek task = get_proc_task(file->f_path.dentry->d_inode); 100299f89551SEric W. Biederman if (!task) 100399f89551SEric W. Biederman return -ESRCH; 10048fb4fc68SGuillem Jover if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) { 10058fb4fc68SGuillem Jover put_task_struct(task); 10068fb4fc68SGuillem Jover return -EACCES; 10078fb4fc68SGuillem Jover } 10081da177e4SLinus Torvalds task->oomkilladj = oom_adjust; 100999f89551SEric W. Biederman put_task_struct(task); 10101da177e4SLinus Torvalds if (end - buffer == 0) 10111da177e4SLinus Torvalds return -EIO; 10121da177e4SLinus Torvalds return end - buffer; 10131da177e4SLinus Torvalds } 10141da177e4SLinus Torvalds 101500977a59SArjan van de Ven static const struct file_operations proc_oom_adjust_operations = { 10161da177e4SLinus Torvalds .read = oom_adjust_read, 10171da177e4SLinus Torvalds .write = oom_adjust_write, 10181da177e4SLinus Torvalds }; 10191da177e4SLinus Torvalds 10201da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL 10211da177e4SLinus Torvalds #define TMPBUFLEN 21 10221da177e4SLinus Torvalds static ssize_t proc_loginuid_read(struct file * file, char __user * buf, 10231da177e4SLinus Torvalds size_t count, loff_t *ppos) 10241da177e4SLinus Torvalds { 10252fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 102699f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 10271da177e4SLinus Torvalds ssize_t length; 10281da177e4SLinus Torvalds char tmpbuf[TMPBUFLEN]; 10291da177e4SLinus Torvalds 103099f89551SEric W. Biederman if (!task) 103199f89551SEric W. Biederman return -ESRCH; 10321da177e4SLinus Torvalds length = scnprintf(tmpbuf, TMPBUFLEN, "%u", 10330c11b942SAl Viro audit_get_loginuid(task)); 103499f89551SEric W. Biederman put_task_struct(task); 10351da177e4SLinus Torvalds return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 10361da177e4SLinus Torvalds } 10371da177e4SLinus Torvalds 10381da177e4SLinus Torvalds static ssize_t proc_loginuid_write(struct file * file, const char __user * buf, 10391da177e4SLinus Torvalds size_t count, loff_t *ppos) 10401da177e4SLinus Torvalds { 10412fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 10421da177e4SLinus Torvalds char *page, *tmp; 10431da177e4SLinus Torvalds ssize_t length; 10441da177e4SLinus Torvalds uid_t loginuid; 10451da177e4SLinus Torvalds 10461da177e4SLinus Torvalds if (!capable(CAP_AUDIT_CONTROL)) 10471da177e4SLinus Torvalds return -EPERM; 10481da177e4SLinus Torvalds 104913b41b09SEric W. Biederman if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) 10501da177e4SLinus Torvalds return -EPERM; 10511da177e4SLinus Torvalds 1052e0182909SAl Viro if (count >= PAGE_SIZE) 1053e0182909SAl Viro count = PAGE_SIZE - 1; 10541da177e4SLinus Torvalds 10551da177e4SLinus Torvalds if (*ppos != 0) { 10561da177e4SLinus Torvalds /* No partial writes. */ 10571da177e4SLinus Torvalds return -EINVAL; 10581da177e4SLinus Torvalds } 1059e12ba74dSMel Gorman page = (char*)__get_free_page(GFP_TEMPORARY); 10601da177e4SLinus Torvalds if (!page) 10611da177e4SLinus Torvalds return -ENOMEM; 10621da177e4SLinus Torvalds length = -EFAULT; 10631da177e4SLinus Torvalds if (copy_from_user(page, buf, count)) 10641da177e4SLinus Torvalds goto out_free_page; 10651da177e4SLinus Torvalds 1066e0182909SAl Viro page[count] = '\0'; 10671da177e4SLinus Torvalds loginuid = simple_strtoul(page, &tmp, 10); 10681da177e4SLinus Torvalds if (tmp == page) { 10691da177e4SLinus Torvalds length = -EINVAL; 10701da177e4SLinus Torvalds goto out_free_page; 10711da177e4SLinus Torvalds 10721da177e4SLinus Torvalds } 107399f89551SEric W. Biederman length = audit_set_loginuid(current, loginuid); 10741da177e4SLinus Torvalds if (likely(length == 0)) 10751da177e4SLinus Torvalds length = count; 10761da177e4SLinus Torvalds 10771da177e4SLinus Torvalds out_free_page: 10781da177e4SLinus Torvalds free_page((unsigned long) page); 10791da177e4SLinus Torvalds return length; 10801da177e4SLinus Torvalds } 10811da177e4SLinus Torvalds 108200977a59SArjan van de Ven static const struct file_operations proc_loginuid_operations = { 10831da177e4SLinus Torvalds .read = proc_loginuid_read, 10841da177e4SLinus Torvalds .write = proc_loginuid_write, 10851da177e4SLinus Torvalds }; 10861e0bd755SEric Paris 10871e0bd755SEric Paris static ssize_t proc_sessionid_read(struct file * file, char __user * buf, 10881e0bd755SEric Paris size_t count, loff_t *ppos) 10891e0bd755SEric Paris { 10901e0bd755SEric Paris struct inode * inode = file->f_path.dentry->d_inode; 10911e0bd755SEric Paris struct task_struct *task = get_proc_task(inode); 10921e0bd755SEric Paris ssize_t length; 10931e0bd755SEric Paris char tmpbuf[TMPBUFLEN]; 10941e0bd755SEric Paris 10951e0bd755SEric Paris if (!task) 10961e0bd755SEric Paris return -ESRCH; 10971e0bd755SEric Paris length = scnprintf(tmpbuf, TMPBUFLEN, "%u", 10981e0bd755SEric Paris audit_get_sessionid(task)); 10991e0bd755SEric Paris put_task_struct(task); 11001e0bd755SEric Paris return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 11011e0bd755SEric Paris } 11021e0bd755SEric Paris 11031e0bd755SEric Paris static const struct file_operations proc_sessionid_operations = { 11041e0bd755SEric Paris .read = proc_sessionid_read, 11051e0bd755SEric Paris }; 11061da177e4SLinus Torvalds #endif 11071da177e4SLinus Torvalds 1108f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION 1109f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_read(struct file * file, char __user * buf, 1110f4f154fdSAkinobu Mita size_t count, loff_t *ppos) 1111f4f154fdSAkinobu Mita { 1112f4f154fdSAkinobu Mita struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 1113f4f154fdSAkinobu Mita char buffer[PROC_NUMBUF]; 1114f4f154fdSAkinobu Mita size_t len; 1115f4f154fdSAkinobu Mita int make_it_fail; 1116f4f154fdSAkinobu Mita 1117f4f154fdSAkinobu Mita if (!task) 1118f4f154fdSAkinobu Mita return -ESRCH; 1119f4f154fdSAkinobu Mita make_it_fail = task->make_it_fail; 1120f4f154fdSAkinobu Mita put_task_struct(task); 1121f4f154fdSAkinobu Mita 1122f4f154fdSAkinobu Mita len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail); 11230c28f287SAkinobu Mita 11240c28f287SAkinobu Mita return simple_read_from_buffer(buf, count, ppos, buffer, len); 1125f4f154fdSAkinobu Mita } 1126f4f154fdSAkinobu Mita 1127f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_write(struct file * file, 1128f4f154fdSAkinobu Mita const char __user * buf, size_t count, loff_t *ppos) 1129f4f154fdSAkinobu Mita { 1130f4f154fdSAkinobu Mita struct task_struct *task; 1131f4f154fdSAkinobu Mita char buffer[PROC_NUMBUF], *end; 1132f4f154fdSAkinobu Mita int make_it_fail; 1133f4f154fdSAkinobu Mita 1134f4f154fdSAkinobu Mita if (!capable(CAP_SYS_RESOURCE)) 1135f4f154fdSAkinobu Mita return -EPERM; 1136f4f154fdSAkinobu Mita memset(buffer, 0, sizeof(buffer)); 1137f4f154fdSAkinobu Mita if (count > sizeof(buffer) - 1) 1138f4f154fdSAkinobu Mita count = sizeof(buffer) - 1; 1139f4f154fdSAkinobu Mita if (copy_from_user(buffer, buf, count)) 1140f4f154fdSAkinobu Mita return -EFAULT; 1141f4f154fdSAkinobu Mita make_it_fail = simple_strtol(buffer, &end, 0); 1142f4f154fdSAkinobu Mita if (*end == '\n') 1143f4f154fdSAkinobu Mita end++; 1144f4f154fdSAkinobu Mita task = get_proc_task(file->f_dentry->d_inode); 1145f4f154fdSAkinobu Mita if (!task) 1146f4f154fdSAkinobu Mita return -ESRCH; 1147f4f154fdSAkinobu Mita task->make_it_fail = make_it_fail; 1148f4f154fdSAkinobu Mita put_task_struct(task); 1149f4f154fdSAkinobu Mita if (end - buffer == 0) 1150f4f154fdSAkinobu Mita return -EIO; 1151f4f154fdSAkinobu Mita return end - buffer; 1152f4f154fdSAkinobu Mita } 1153f4f154fdSAkinobu Mita 115400977a59SArjan van de Ven static const struct file_operations proc_fault_inject_operations = { 1155f4f154fdSAkinobu Mita .read = proc_fault_inject_read, 1156f4f154fdSAkinobu Mita .write = proc_fault_inject_write, 1157f4f154fdSAkinobu Mita }; 1158f4f154fdSAkinobu Mita #endif 1159f4f154fdSAkinobu Mita 11609745512cSArjan van de Ven 116143ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG 116243ae34cbSIngo Molnar /* 116343ae34cbSIngo Molnar * Print out various scheduling related per-task fields: 116443ae34cbSIngo Molnar */ 116543ae34cbSIngo Molnar static int sched_show(struct seq_file *m, void *v) 116643ae34cbSIngo Molnar { 116743ae34cbSIngo Molnar struct inode *inode = m->private; 116843ae34cbSIngo Molnar struct task_struct *p; 116943ae34cbSIngo Molnar 117043ae34cbSIngo Molnar WARN_ON(!inode); 117143ae34cbSIngo Molnar 117243ae34cbSIngo Molnar p = get_proc_task(inode); 117343ae34cbSIngo Molnar if (!p) 117443ae34cbSIngo Molnar return -ESRCH; 117543ae34cbSIngo Molnar proc_sched_show_task(p, m); 117643ae34cbSIngo Molnar 117743ae34cbSIngo Molnar put_task_struct(p); 117843ae34cbSIngo Molnar 117943ae34cbSIngo Molnar return 0; 118043ae34cbSIngo Molnar } 118143ae34cbSIngo Molnar 118243ae34cbSIngo Molnar static ssize_t 118343ae34cbSIngo Molnar sched_write(struct file *file, const char __user *buf, 118443ae34cbSIngo Molnar size_t count, loff_t *offset) 118543ae34cbSIngo Molnar { 118643ae34cbSIngo Molnar struct inode *inode = file->f_path.dentry->d_inode; 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_set_task(p); 119543ae34cbSIngo Molnar 119643ae34cbSIngo Molnar put_task_struct(p); 119743ae34cbSIngo Molnar 119843ae34cbSIngo Molnar return count; 119943ae34cbSIngo Molnar } 120043ae34cbSIngo Molnar 120143ae34cbSIngo Molnar static int sched_open(struct inode *inode, struct file *filp) 120243ae34cbSIngo Molnar { 120343ae34cbSIngo Molnar int ret; 120443ae34cbSIngo Molnar 120543ae34cbSIngo Molnar ret = single_open(filp, sched_show, NULL); 120643ae34cbSIngo Molnar if (!ret) { 120743ae34cbSIngo Molnar struct seq_file *m = filp->private_data; 120843ae34cbSIngo Molnar 120943ae34cbSIngo Molnar m->private = inode; 121043ae34cbSIngo Molnar } 121143ae34cbSIngo Molnar return ret; 121243ae34cbSIngo Molnar } 121343ae34cbSIngo Molnar 121443ae34cbSIngo Molnar static const struct file_operations proc_pid_sched_operations = { 121543ae34cbSIngo Molnar .open = sched_open, 121643ae34cbSIngo Molnar .read = seq_read, 121743ae34cbSIngo Molnar .write = sched_write, 121843ae34cbSIngo Molnar .llseek = seq_lseek, 12195ea473a1SAlexey Dobriyan .release = single_release, 122043ae34cbSIngo Molnar }; 122143ae34cbSIngo Molnar 122243ae34cbSIngo Molnar #endif 122343ae34cbSIngo Molnar 1224925d1c40SMatt Helsley /* 1225925d1c40SMatt Helsley * We added or removed a vma mapping the executable. The vmas are only mapped 1226925d1c40SMatt Helsley * during exec and are not mapped with the mmap system call. 1227925d1c40SMatt Helsley * Callers must hold down_write() on the mm's mmap_sem for these 1228925d1c40SMatt Helsley */ 1229925d1c40SMatt Helsley void added_exe_file_vma(struct mm_struct *mm) 1230925d1c40SMatt Helsley { 1231925d1c40SMatt Helsley mm->num_exe_file_vmas++; 1232925d1c40SMatt Helsley } 1233925d1c40SMatt Helsley 1234925d1c40SMatt Helsley void removed_exe_file_vma(struct mm_struct *mm) 1235925d1c40SMatt Helsley { 1236925d1c40SMatt Helsley mm->num_exe_file_vmas--; 1237925d1c40SMatt Helsley if ((mm->num_exe_file_vmas == 0) && mm->exe_file){ 1238925d1c40SMatt Helsley fput(mm->exe_file); 1239925d1c40SMatt Helsley mm->exe_file = NULL; 1240925d1c40SMatt Helsley } 1241925d1c40SMatt Helsley 1242925d1c40SMatt Helsley } 1243925d1c40SMatt Helsley 1244925d1c40SMatt Helsley void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file) 1245925d1c40SMatt Helsley { 1246925d1c40SMatt Helsley if (new_exe_file) 1247925d1c40SMatt Helsley get_file(new_exe_file); 1248925d1c40SMatt Helsley if (mm->exe_file) 1249925d1c40SMatt Helsley fput(mm->exe_file); 1250925d1c40SMatt Helsley mm->exe_file = new_exe_file; 1251925d1c40SMatt Helsley mm->num_exe_file_vmas = 0; 1252925d1c40SMatt Helsley } 1253925d1c40SMatt Helsley 1254925d1c40SMatt Helsley struct file *get_mm_exe_file(struct mm_struct *mm) 1255925d1c40SMatt Helsley { 1256925d1c40SMatt Helsley struct file *exe_file; 1257925d1c40SMatt Helsley 1258925d1c40SMatt Helsley /* We need mmap_sem to protect against races with removal of 1259925d1c40SMatt Helsley * VM_EXECUTABLE vmas */ 1260925d1c40SMatt Helsley down_read(&mm->mmap_sem); 1261925d1c40SMatt Helsley exe_file = mm->exe_file; 1262925d1c40SMatt Helsley if (exe_file) 1263925d1c40SMatt Helsley get_file(exe_file); 1264925d1c40SMatt Helsley up_read(&mm->mmap_sem); 1265925d1c40SMatt Helsley return exe_file; 1266925d1c40SMatt Helsley } 1267925d1c40SMatt Helsley 1268925d1c40SMatt Helsley void dup_mm_exe_file(struct mm_struct *oldmm, struct mm_struct *newmm) 1269925d1c40SMatt Helsley { 1270925d1c40SMatt Helsley /* It's safe to write the exe_file pointer without exe_file_lock because 1271925d1c40SMatt Helsley * this is called during fork when the task is not yet in /proc */ 1272925d1c40SMatt Helsley newmm->exe_file = get_mm_exe_file(oldmm); 1273925d1c40SMatt Helsley } 1274925d1c40SMatt Helsley 1275925d1c40SMatt Helsley static int proc_exe_link(struct inode *inode, struct path *exe_path) 1276925d1c40SMatt Helsley { 1277925d1c40SMatt Helsley struct task_struct *task; 1278925d1c40SMatt Helsley struct mm_struct *mm; 1279925d1c40SMatt Helsley struct file *exe_file; 1280925d1c40SMatt Helsley 1281925d1c40SMatt Helsley task = get_proc_task(inode); 1282925d1c40SMatt Helsley if (!task) 1283925d1c40SMatt Helsley return -ENOENT; 1284925d1c40SMatt Helsley mm = get_task_mm(task); 1285925d1c40SMatt Helsley put_task_struct(task); 1286925d1c40SMatt Helsley if (!mm) 1287925d1c40SMatt Helsley return -ENOENT; 1288925d1c40SMatt Helsley exe_file = get_mm_exe_file(mm); 1289925d1c40SMatt Helsley mmput(mm); 1290925d1c40SMatt Helsley if (exe_file) { 1291925d1c40SMatt Helsley *exe_path = exe_file->f_path; 1292925d1c40SMatt Helsley path_get(&exe_file->f_path); 1293925d1c40SMatt Helsley fput(exe_file); 1294925d1c40SMatt Helsley return 0; 1295925d1c40SMatt Helsley } else 1296925d1c40SMatt Helsley return -ENOENT; 1297925d1c40SMatt Helsley } 1298925d1c40SMatt Helsley 1299008b150aSAl Viro static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd) 13001da177e4SLinus Torvalds { 13011da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 13021da177e4SLinus Torvalds int error = -EACCES; 13031da177e4SLinus Torvalds 13041da177e4SLinus Torvalds /* We don't need a base pointer in the /proc filesystem */ 13051d957f9bSJan Blunck path_put(&nd->path); 13061da177e4SLinus Torvalds 1307778c1144SEric W. Biederman /* Are we allowed to snoop on the tasks file descriptors? */ 1308778c1144SEric W. Biederman if (!proc_fd_access_allowed(inode)) 13091da177e4SLinus Torvalds goto out; 13101da177e4SLinus Torvalds 13113dcd25f3SJan Blunck error = PROC_I(inode)->op.proc_get_link(inode, &nd->path); 13121da177e4SLinus Torvalds nd->last_type = LAST_BIND; 13131da177e4SLinus Torvalds out: 1314008b150aSAl Viro return ERR_PTR(error); 13151da177e4SLinus Torvalds } 13161da177e4SLinus Torvalds 13173dcd25f3SJan Blunck static int do_proc_readlink(struct path *path, char __user *buffer, int buflen) 13181da177e4SLinus Torvalds { 1319e12ba74dSMel Gorman char *tmp = (char*)__get_free_page(GFP_TEMPORARY); 13203dcd25f3SJan Blunck char *pathname; 13211da177e4SLinus Torvalds int len; 13221da177e4SLinus Torvalds 13231da177e4SLinus Torvalds if (!tmp) 13241da177e4SLinus Torvalds return -ENOMEM; 13251da177e4SLinus Torvalds 1326cf28b486SJan Blunck pathname = d_path(path, tmp, PAGE_SIZE); 13273dcd25f3SJan Blunck len = PTR_ERR(pathname); 13283dcd25f3SJan Blunck if (IS_ERR(pathname)) 13291da177e4SLinus Torvalds goto out; 13303dcd25f3SJan Blunck len = tmp + PAGE_SIZE - 1 - pathname; 13311da177e4SLinus Torvalds 13321da177e4SLinus Torvalds if (len > buflen) 13331da177e4SLinus Torvalds len = buflen; 13343dcd25f3SJan Blunck if (copy_to_user(buffer, pathname, len)) 13351da177e4SLinus Torvalds len = -EFAULT; 13361da177e4SLinus Torvalds out: 13371da177e4SLinus Torvalds free_page((unsigned long)tmp); 13381da177e4SLinus Torvalds return len; 13391da177e4SLinus Torvalds } 13401da177e4SLinus Torvalds 13411da177e4SLinus Torvalds static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen) 13421da177e4SLinus Torvalds { 13431da177e4SLinus Torvalds int error = -EACCES; 13441da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 13453dcd25f3SJan Blunck struct path path; 13461da177e4SLinus Torvalds 1347778c1144SEric W. Biederman /* Are we allowed to snoop on the tasks file descriptors? */ 1348778c1144SEric W. Biederman if (!proc_fd_access_allowed(inode)) 13491da177e4SLinus Torvalds goto out; 13501da177e4SLinus Torvalds 13513dcd25f3SJan Blunck error = PROC_I(inode)->op.proc_get_link(inode, &path); 13521da177e4SLinus Torvalds if (error) 13531da177e4SLinus Torvalds goto out; 13541da177e4SLinus Torvalds 13553dcd25f3SJan Blunck error = do_proc_readlink(&path, buffer, buflen); 13563dcd25f3SJan Blunck path_put(&path); 13571da177e4SLinus Torvalds out: 13581da177e4SLinus Torvalds return error; 13591da177e4SLinus Torvalds } 13601da177e4SLinus Torvalds 1361c5ef1c42SArjan van de Ven static const struct inode_operations proc_pid_link_inode_operations = { 13621da177e4SLinus Torvalds .readlink = proc_pid_readlink, 13636d76fa58SLinus Torvalds .follow_link = proc_pid_follow_link, 13646d76fa58SLinus Torvalds .setattr = proc_setattr, 13651da177e4SLinus Torvalds }; 13661da177e4SLinus Torvalds 136728a6d671SEric W. Biederman 136828a6d671SEric W. Biederman /* building an inode */ 136928a6d671SEric W. Biederman 137028a6d671SEric W. Biederman static int task_dumpable(struct task_struct *task) 137128a6d671SEric W. Biederman { 137228a6d671SEric W. Biederman int dumpable = 0; 137328a6d671SEric W. Biederman struct mm_struct *mm; 137428a6d671SEric W. Biederman 137528a6d671SEric W. Biederman task_lock(task); 137628a6d671SEric W. Biederman mm = task->mm; 137728a6d671SEric W. Biederman if (mm) 13786c5d5238SKawai, Hidehiro dumpable = get_dumpable(mm); 137928a6d671SEric W. Biederman task_unlock(task); 138028a6d671SEric W. Biederman if(dumpable == 1) 138128a6d671SEric W. Biederman return 1; 138228a6d671SEric W. Biederman return 0; 138328a6d671SEric W. Biederman } 138428a6d671SEric W. Biederman 138528a6d671SEric W. Biederman 138661a28784SEric W. Biederman static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task) 138728a6d671SEric W. Biederman { 138828a6d671SEric W. Biederman struct inode * inode; 138928a6d671SEric W. Biederman struct proc_inode *ei; 139028a6d671SEric W. Biederman 139128a6d671SEric W. Biederman /* We need a new inode */ 139228a6d671SEric W. Biederman 139328a6d671SEric W. Biederman inode = new_inode(sb); 139428a6d671SEric W. Biederman if (!inode) 139528a6d671SEric W. Biederman goto out; 139628a6d671SEric W. Biederman 139728a6d671SEric W. Biederman /* Common stuff */ 139828a6d671SEric W. Biederman ei = PROC_I(inode); 139928a6d671SEric W. Biederman inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; 140028a6d671SEric W. Biederman inode->i_op = &proc_def_inode_operations; 140128a6d671SEric W. Biederman 140228a6d671SEric W. Biederman /* 140328a6d671SEric W. Biederman * grab the reference to task. 140428a6d671SEric W. Biederman */ 14051a657f78SOleg Nesterov ei->pid = get_task_pid(task, PIDTYPE_PID); 140628a6d671SEric W. Biederman if (!ei->pid) 140728a6d671SEric W. Biederman goto out_unlock; 140828a6d671SEric W. Biederman 140928a6d671SEric W. Biederman inode->i_uid = 0; 141028a6d671SEric W. Biederman inode->i_gid = 0; 141128a6d671SEric W. Biederman if (task_dumpable(task)) { 141228a6d671SEric W. Biederman inode->i_uid = task->euid; 141328a6d671SEric W. Biederman inode->i_gid = task->egid; 141428a6d671SEric W. Biederman } 141528a6d671SEric W. Biederman security_task_to_inode(task, inode); 141628a6d671SEric W. Biederman 141728a6d671SEric W. Biederman out: 141828a6d671SEric W. Biederman return inode; 141928a6d671SEric W. Biederman 142028a6d671SEric W. Biederman out_unlock: 142128a6d671SEric W. Biederman iput(inode); 142228a6d671SEric W. Biederman return NULL; 142328a6d671SEric W. Biederman } 142428a6d671SEric W. Biederman 142528a6d671SEric W. Biederman static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) 142628a6d671SEric W. Biederman { 142728a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 142828a6d671SEric W. Biederman struct task_struct *task; 142928a6d671SEric W. Biederman generic_fillattr(inode, stat); 143028a6d671SEric W. Biederman 143128a6d671SEric W. Biederman rcu_read_lock(); 143228a6d671SEric W. Biederman stat->uid = 0; 143328a6d671SEric W. Biederman stat->gid = 0; 143428a6d671SEric W. Biederman task = pid_task(proc_pid(inode), PIDTYPE_PID); 143528a6d671SEric W. Biederman if (task) { 143628a6d671SEric W. Biederman if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) || 143728a6d671SEric W. Biederman task_dumpable(task)) { 143828a6d671SEric W. Biederman stat->uid = task->euid; 143928a6d671SEric W. Biederman stat->gid = task->egid; 144028a6d671SEric W. Biederman } 144128a6d671SEric W. Biederman } 144228a6d671SEric W. Biederman rcu_read_unlock(); 144328a6d671SEric W. Biederman return 0; 144428a6d671SEric W. Biederman } 144528a6d671SEric W. Biederman 144628a6d671SEric W. Biederman /* dentry stuff */ 144728a6d671SEric W. Biederman 144828a6d671SEric W. Biederman /* 144928a6d671SEric W. Biederman * Exceptional case: normally we are not allowed to unhash a busy 145028a6d671SEric W. Biederman * directory. In this case, however, we can do it - no aliasing problems 145128a6d671SEric W. Biederman * due to the way we treat inodes. 145228a6d671SEric W. Biederman * 145328a6d671SEric W. Biederman * Rewrite the inode's ownerships here because the owning task may have 145428a6d671SEric W. Biederman * performed a setuid(), etc. 145528a6d671SEric W. Biederman * 145628a6d671SEric W. Biederman * Before the /proc/pid/status file was created the only way to read 145728a6d671SEric W. Biederman * the effective uid of a /process was to stat /proc/pid. Reading 145828a6d671SEric W. Biederman * /proc/pid/status is slow enough that procps and other packages 145928a6d671SEric W. Biederman * kept stating /proc/pid. To keep the rules in /proc simple I have 146028a6d671SEric W. Biederman * made this apply to all per process world readable and executable 146128a6d671SEric W. Biederman * directories. 146228a6d671SEric W. Biederman */ 146328a6d671SEric W. Biederman static int pid_revalidate(struct dentry *dentry, struct nameidata *nd) 146428a6d671SEric W. Biederman { 146528a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 146628a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 146728a6d671SEric W. Biederman if (task) { 146828a6d671SEric W. Biederman if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) || 146928a6d671SEric W. Biederman task_dumpable(task)) { 147028a6d671SEric W. Biederman inode->i_uid = task->euid; 147128a6d671SEric W. Biederman inode->i_gid = task->egid; 147228a6d671SEric W. Biederman } else { 147328a6d671SEric W. Biederman inode->i_uid = 0; 147428a6d671SEric W. Biederman inode->i_gid = 0; 147528a6d671SEric W. Biederman } 147628a6d671SEric W. Biederman inode->i_mode &= ~(S_ISUID | S_ISGID); 147728a6d671SEric W. Biederman security_task_to_inode(task, inode); 147828a6d671SEric W. Biederman put_task_struct(task); 147928a6d671SEric W. Biederman return 1; 148028a6d671SEric W. Biederman } 148128a6d671SEric W. Biederman d_drop(dentry); 148228a6d671SEric W. Biederman return 0; 148328a6d671SEric W. Biederman } 148428a6d671SEric W. Biederman 148528a6d671SEric W. Biederman static int pid_delete_dentry(struct dentry * dentry) 148628a6d671SEric W. Biederman { 148728a6d671SEric W. Biederman /* Is the task we represent dead? 148828a6d671SEric W. Biederman * If so, then don't put the dentry on the lru list, 148928a6d671SEric W. Biederman * kill it immediately. 149028a6d671SEric W. Biederman */ 149128a6d671SEric W. Biederman return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first; 149228a6d671SEric W. Biederman } 149328a6d671SEric W. Biederman 149428a6d671SEric W. Biederman static struct dentry_operations pid_dentry_operations = 149528a6d671SEric W. Biederman { 149628a6d671SEric W. Biederman .d_revalidate = pid_revalidate, 149728a6d671SEric W. Biederman .d_delete = pid_delete_dentry, 149828a6d671SEric W. Biederman }; 149928a6d671SEric W. Biederman 150028a6d671SEric W. Biederman /* Lookups */ 150128a6d671SEric W. Biederman 1502c5141e6dSEric Dumazet typedef struct dentry *instantiate_t(struct inode *, struct dentry *, 1503c5141e6dSEric Dumazet struct task_struct *, const void *); 150461a28784SEric W. Biederman 15051c0d04c9SEric W. Biederman /* 15061c0d04c9SEric W. Biederman * Fill a directory entry. 15071c0d04c9SEric W. Biederman * 15081c0d04c9SEric W. Biederman * If possible create the dcache entry and derive our inode number and 15091c0d04c9SEric W. Biederman * file type from dcache entry. 15101c0d04c9SEric W. Biederman * 15111c0d04c9SEric W. Biederman * Since all of the proc inode numbers are dynamically generated, the inode 15121c0d04c9SEric W. Biederman * numbers do not exist until the inode is cache. This means creating the 15131c0d04c9SEric W. Biederman * the dcache entry in readdir is necessary to keep the inode numbers 15141c0d04c9SEric W. Biederman * reported by readdir in sync with the inode numbers reported 15151c0d04c9SEric W. Biederman * by stat. 15161c0d04c9SEric W. Biederman */ 151761a28784SEric W. Biederman static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir, 151861a28784SEric W. Biederman char *name, int len, 1519c5141e6dSEric Dumazet instantiate_t instantiate, struct task_struct *task, const void *ptr) 152061a28784SEric W. Biederman { 15212fddfeefSJosef "Jeff" Sipek struct dentry *child, *dir = filp->f_path.dentry; 152261a28784SEric W. Biederman struct inode *inode; 152361a28784SEric W. Biederman struct qstr qname; 152461a28784SEric W. Biederman ino_t ino = 0; 152561a28784SEric W. Biederman unsigned type = DT_UNKNOWN; 152661a28784SEric W. Biederman 152761a28784SEric W. Biederman qname.name = name; 152861a28784SEric W. Biederman qname.len = len; 152961a28784SEric W. Biederman qname.hash = full_name_hash(name, len); 153061a28784SEric W. Biederman 153161a28784SEric W. Biederman child = d_lookup(dir, &qname); 153261a28784SEric W. Biederman if (!child) { 153361a28784SEric W. Biederman struct dentry *new; 153461a28784SEric W. Biederman new = d_alloc(dir, &qname); 153561a28784SEric W. Biederman if (new) { 153661a28784SEric W. Biederman child = instantiate(dir->d_inode, new, task, ptr); 153761a28784SEric W. Biederman if (child) 153861a28784SEric W. Biederman dput(new); 153961a28784SEric W. Biederman else 154061a28784SEric W. Biederman child = new; 154161a28784SEric W. Biederman } 154261a28784SEric W. Biederman } 154361a28784SEric W. Biederman if (!child || IS_ERR(child) || !child->d_inode) 154461a28784SEric W. Biederman goto end_instantiate; 154561a28784SEric W. Biederman inode = child->d_inode; 154661a28784SEric W. Biederman if (inode) { 154761a28784SEric W. Biederman ino = inode->i_ino; 154861a28784SEric W. Biederman type = inode->i_mode >> 12; 154961a28784SEric W. Biederman } 155061a28784SEric W. Biederman dput(child); 155161a28784SEric W. Biederman end_instantiate: 155261a28784SEric W. Biederman if (!ino) 155361a28784SEric W. Biederman ino = find_inode_number(dir, &qname); 155461a28784SEric W. Biederman if (!ino) 155561a28784SEric W. Biederman ino = 1; 155661a28784SEric W. Biederman return filldir(dirent, name, len, filp->f_pos, ino, type); 155761a28784SEric W. Biederman } 155861a28784SEric W. Biederman 155928a6d671SEric W. Biederman static unsigned name_to_int(struct dentry *dentry) 156028a6d671SEric W. Biederman { 156128a6d671SEric W. Biederman const char *name = dentry->d_name.name; 156228a6d671SEric W. Biederman int len = dentry->d_name.len; 156328a6d671SEric W. Biederman unsigned n = 0; 156428a6d671SEric W. Biederman 156528a6d671SEric W. Biederman if (len > 1 && *name == '0') 156628a6d671SEric W. Biederman goto out; 156728a6d671SEric W. Biederman while (len-- > 0) { 156828a6d671SEric W. Biederman unsigned c = *name++ - '0'; 156928a6d671SEric W. Biederman if (c > 9) 157028a6d671SEric W. Biederman goto out; 157128a6d671SEric W. Biederman if (n >= (~0U-9)/10) 157228a6d671SEric W. Biederman goto out; 157328a6d671SEric W. Biederman n *= 10; 157428a6d671SEric W. Biederman n += c; 157528a6d671SEric W. Biederman } 157628a6d671SEric W. Biederman return n; 157728a6d671SEric W. Biederman out: 157828a6d671SEric W. Biederman return ~0U; 157928a6d671SEric W. Biederman } 158028a6d671SEric W. Biederman 158127932742SMiklos Szeredi #define PROC_FDINFO_MAX 64 158227932742SMiklos Szeredi 15833dcd25f3SJan Blunck static int proc_fd_info(struct inode *inode, struct path *path, char *info) 158428a6d671SEric W. Biederman { 158528a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 158628a6d671SEric W. Biederman struct files_struct *files = NULL; 158728a6d671SEric W. Biederman struct file *file; 158828a6d671SEric W. Biederman int fd = proc_fd(inode); 158928a6d671SEric W. Biederman 159028a6d671SEric W. Biederman if (task) { 159128a6d671SEric W. Biederman files = get_files_struct(task); 159228a6d671SEric W. Biederman put_task_struct(task); 159328a6d671SEric W. Biederman } 159428a6d671SEric W. Biederman if (files) { 159528a6d671SEric W. Biederman /* 159628a6d671SEric W. Biederman * We are not taking a ref to the file structure, so we must 159728a6d671SEric W. Biederman * hold ->file_lock. 159828a6d671SEric W. Biederman */ 159928a6d671SEric W. Biederman spin_lock(&files->file_lock); 160028a6d671SEric W. Biederman file = fcheck_files(files, fd); 160128a6d671SEric W. Biederman if (file) { 16023dcd25f3SJan Blunck if (path) { 16033dcd25f3SJan Blunck *path = file->f_path; 16043dcd25f3SJan Blunck path_get(&file->f_path); 16053dcd25f3SJan Blunck } 160627932742SMiklos Szeredi if (info) 160727932742SMiklos Szeredi snprintf(info, PROC_FDINFO_MAX, 160827932742SMiklos Szeredi "pos:\t%lli\n" 160927932742SMiklos Szeredi "flags:\t0%o\n", 161027932742SMiklos Szeredi (long long) file->f_pos, 161127932742SMiklos Szeredi file->f_flags); 161228a6d671SEric W. Biederman spin_unlock(&files->file_lock); 161328a6d671SEric W. Biederman put_files_struct(files); 161428a6d671SEric W. Biederman return 0; 161528a6d671SEric W. Biederman } 161628a6d671SEric W. Biederman spin_unlock(&files->file_lock); 161728a6d671SEric W. Biederman put_files_struct(files); 161828a6d671SEric W. Biederman } 161928a6d671SEric W. Biederman return -ENOENT; 162028a6d671SEric W. Biederman } 162128a6d671SEric W. Biederman 16223dcd25f3SJan Blunck static int proc_fd_link(struct inode *inode, struct path *path) 162327932742SMiklos Szeredi { 16243dcd25f3SJan Blunck return proc_fd_info(inode, path, NULL); 162527932742SMiklos Szeredi } 162627932742SMiklos Szeredi 162728a6d671SEric W. Biederman static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd) 162828a6d671SEric W. Biederman { 162928a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 163028a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 163128a6d671SEric W. Biederman int fd = proc_fd(inode); 163228a6d671SEric W. Biederman struct files_struct *files; 163328a6d671SEric W. Biederman 163428a6d671SEric W. Biederman if (task) { 163528a6d671SEric W. Biederman files = get_files_struct(task); 163628a6d671SEric W. Biederman if (files) { 163728a6d671SEric W. Biederman rcu_read_lock(); 163828a6d671SEric W. Biederman if (fcheck_files(files, fd)) { 163928a6d671SEric W. Biederman rcu_read_unlock(); 164028a6d671SEric W. Biederman put_files_struct(files); 164128a6d671SEric W. Biederman if (task_dumpable(task)) { 164228a6d671SEric W. Biederman inode->i_uid = task->euid; 164328a6d671SEric W. Biederman inode->i_gid = task->egid; 164428a6d671SEric W. Biederman } else { 164528a6d671SEric W. Biederman inode->i_uid = 0; 164628a6d671SEric W. Biederman inode->i_gid = 0; 164728a6d671SEric W. Biederman } 164828a6d671SEric W. Biederman inode->i_mode &= ~(S_ISUID | S_ISGID); 164928a6d671SEric W. Biederman security_task_to_inode(task, inode); 165028a6d671SEric W. Biederman put_task_struct(task); 165128a6d671SEric W. Biederman return 1; 165228a6d671SEric W. Biederman } 165328a6d671SEric W. Biederman rcu_read_unlock(); 165428a6d671SEric W. Biederman put_files_struct(files); 165528a6d671SEric W. Biederman } 165628a6d671SEric W. Biederman put_task_struct(task); 165728a6d671SEric W. Biederman } 165828a6d671SEric W. Biederman d_drop(dentry); 165928a6d671SEric W. Biederman return 0; 166028a6d671SEric W. Biederman } 166128a6d671SEric W. Biederman 166228a6d671SEric W. Biederman static struct dentry_operations tid_fd_dentry_operations = 166328a6d671SEric W. Biederman { 166428a6d671SEric W. Biederman .d_revalidate = tid_fd_revalidate, 166528a6d671SEric W. Biederman .d_delete = pid_delete_dentry, 166628a6d671SEric W. Biederman }; 166728a6d671SEric W. Biederman 1668444ceed8SEric W. Biederman static struct dentry *proc_fd_instantiate(struct inode *dir, 1669c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 167028a6d671SEric W. Biederman { 1671c5141e6dSEric Dumazet unsigned fd = *(const unsigned *)ptr; 167228a6d671SEric W. Biederman struct file *file; 167328a6d671SEric W. Biederman struct files_struct *files; 167428a6d671SEric W. Biederman struct inode *inode; 167528a6d671SEric W. Biederman struct proc_inode *ei; 1676444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-ENOENT); 167728a6d671SEric W. Biederman 167861a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 167928a6d671SEric W. Biederman if (!inode) 168028a6d671SEric W. Biederman goto out; 168128a6d671SEric W. Biederman ei = PROC_I(inode); 168228a6d671SEric W. Biederman ei->fd = fd; 168328a6d671SEric W. Biederman files = get_files_struct(task); 168428a6d671SEric W. Biederman if (!files) 1685444ceed8SEric W. Biederman goto out_iput; 168628a6d671SEric W. Biederman inode->i_mode = S_IFLNK; 168728a6d671SEric W. Biederman 168828a6d671SEric W. Biederman /* 168928a6d671SEric W. Biederman * We are not taking a ref to the file structure, so we must 169028a6d671SEric W. Biederman * hold ->file_lock. 169128a6d671SEric W. Biederman */ 169228a6d671SEric W. Biederman spin_lock(&files->file_lock); 169328a6d671SEric W. Biederman file = fcheck_files(files, fd); 169428a6d671SEric W. Biederman if (!file) 1695444ceed8SEric W. Biederman goto out_unlock; 169628a6d671SEric W. Biederman if (file->f_mode & 1) 169728a6d671SEric W. Biederman inode->i_mode |= S_IRUSR | S_IXUSR; 169828a6d671SEric W. Biederman if (file->f_mode & 2) 169928a6d671SEric W. Biederman inode->i_mode |= S_IWUSR | S_IXUSR; 170028a6d671SEric W. Biederman spin_unlock(&files->file_lock); 170128a6d671SEric W. Biederman put_files_struct(files); 1702444ceed8SEric W. Biederman 170328a6d671SEric W. Biederman inode->i_op = &proc_pid_link_inode_operations; 170428a6d671SEric W. Biederman inode->i_size = 64; 170528a6d671SEric W. Biederman ei->op.proc_get_link = proc_fd_link; 170628a6d671SEric W. Biederman dentry->d_op = &tid_fd_dentry_operations; 170728a6d671SEric W. Biederman d_add(dentry, inode); 170828a6d671SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 170928a6d671SEric W. Biederman if (tid_fd_revalidate(dentry, NULL)) 1710444ceed8SEric W. Biederman error = NULL; 1711444ceed8SEric W. Biederman 1712444ceed8SEric W. Biederman out: 1713444ceed8SEric W. Biederman return error; 1714444ceed8SEric W. Biederman out_unlock: 1715444ceed8SEric W. Biederman spin_unlock(&files->file_lock); 1716444ceed8SEric W. Biederman put_files_struct(files); 1717444ceed8SEric W. Biederman out_iput: 1718444ceed8SEric W. Biederman iput(inode); 1719444ceed8SEric W. Biederman goto out; 1720444ceed8SEric W. Biederman } 1721444ceed8SEric W. Biederman 172227932742SMiklos Szeredi static struct dentry *proc_lookupfd_common(struct inode *dir, 172327932742SMiklos Szeredi struct dentry *dentry, 172427932742SMiklos Szeredi instantiate_t instantiate) 1725444ceed8SEric W. Biederman { 1726444ceed8SEric W. Biederman struct task_struct *task = get_proc_task(dir); 1727444ceed8SEric W. Biederman unsigned fd = name_to_int(dentry); 1728444ceed8SEric W. Biederman struct dentry *result = ERR_PTR(-ENOENT); 1729444ceed8SEric W. Biederman 1730444ceed8SEric W. Biederman if (!task) 1731444ceed8SEric W. Biederman goto out_no_task; 1732444ceed8SEric W. Biederman if (fd == ~0U) 1733444ceed8SEric W. Biederman goto out; 1734444ceed8SEric W. Biederman 173527932742SMiklos Szeredi result = instantiate(dir, dentry, task, &fd); 173628a6d671SEric W. Biederman out: 173728a6d671SEric W. Biederman put_task_struct(task); 173828a6d671SEric W. Biederman out_no_task: 173928a6d671SEric W. Biederman return result; 174028a6d671SEric W. Biederman } 174128a6d671SEric W. Biederman 174227932742SMiklos Szeredi static int proc_readfd_common(struct file * filp, void * dirent, 174327932742SMiklos Szeredi filldir_t filldir, instantiate_t instantiate) 17441da177e4SLinus Torvalds { 17452fddfeefSJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 17465634708bSEric W. Biederman struct inode *inode = dentry->d_inode; 174799f89551SEric W. Biederman struct task_struct *p = get_proc_task(inode); 1748457c2510SPavel Emelyanov unsigned int fd, ino; 17491da177e4SLinus Torvalds int retval; 17501da177e4SLinus Torvalds struct files_struct * files; 17511da177e4SLinus Torvalds 17521da177e4SLinus Torvalds retval = -ENOENT; 175399f89551SEric W. Biederman if (!p) 175499f89551SEric W. Biederman goto out_no_task; 17551da177e4SLinus Torvalds retval = 0; 17561da177e4SLinus Torvalds 17571da177e4SLinus Torvalds fd = filp->f_pos; 17581da177e4SLinus Torvalds switch (fd) { 17591da177e4SLinus Torvalds case 0: 17601da177e4SLinus Torvalds if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0) 17611da177e4SLinus Torvalds goto out; 17621da177e4SLinus Torvalds filp->f_pos++; 17631da177e4SLinus Torvalds case 1: 17645634708bSEric W. Biederman ino = parent_ino(dentry); 17651da177e4SLinus Torvalds if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0) 17661da177e4SLinus Torvalds goto out; 17671da177e4SLinus Torvalds filp->f_pos++; 17681da177e4SLinus Torvalds default: 17691da177e4SLinus Torvalds files = get_files_struct(p); 17701da177e4SLinus Torvalds if (!files) 17711da177e4SLinus Torvalds goto out; 1772b835996fSDipankar Sarma rcu_read_lock(); 17731da177e4SLinus Torvalds for (fd = filp->f_pos-2; 17749b4f526cSAl Viro fd < files_fdtable(files)->max_fds; 17751da177e4SLinus Torvalds fd++, filp->f_pos++) { 177627932742SMiklos Szeredi char name[PROC_NUMBUF]; 177727932742SMiklos Szeredi int len; 17781da177e4SLinus Torvalds 17791da177e4SLinus Torvalds if (!fcheck_files(files, fd)) 17801da177e4SLinus Torvalds continue; 1781b835996fSDipankar Sarma rcu_read_unlock(); 17821da177e4SLinus Torvalds 178327932742SMiklos Szeredi len = snprintf(name, sizeof(name), "%d", fd); 178427932742SMiklos Szeredi if (proc_fill_cache(filp, dirent, filldir, 178527932742SMiklos Szeredi name, len, instantiate, 178627932742SMiklos Szeredi p, &fd) < 0) { 1787b835996fSDipankar Sarma rcu_read_lock(); 17881da177e4SLinus Torvalds break; 17891da177e4SLinus Torvalds } 1790b835996fSDipankar Sarma rcu_read_lock(); 17911da177e4SLinus Torvalds } 1792b835996fSDipankar Sarma rcu_read_unlock(); 17931da177e4SLinus Torvalds put_files_struct(files); 17941da177e4SLinus Torvalds } 17951da177e4SLinus Torvalds out: 179699f89551SEric W. Biederman put_task_struct(p); 179799f89551SEric W. Biederman out_no_task: 17981da177e4SLinus Torvalds return retval; 17991da177e4SLinus Torvalds } 18001da177e4SLinus Torvalds 180127932742SMiklos Szeredi static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry, 180227932742SMiklos Szeredi struct nameidata *nd) 180327932742SMiklos Szeredi { 180427932742SMiklos Szeredi return proc_lookupfd_common(dir, dentry, proc_fd_instantiate); 180527932742SMiklos Szeredi } 180627932742SMiklos Szeredi 180727932742SMiklos Szeredi static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir) 180827932742SMiklos Szeredi { 180927932742SMiklos Szeredi return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate); 181027932742SMiklos Szeredi } 181127932742SMiklos Szeredi 181227932742SMiklos Szeredi static ssize_t proc_fdinfo_read(struct file *file, char __user *buf, 181327932742SMiklos Szeredi size_t len, loff_t *ppos) 181427932742SMiklos Szeredi { 181527932742SMiklos Szeredi char tmp[PROC_FDINFO_MAX]; 18163dcd25f3SJan Blunck int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp); 181727932742SMiklos Szeredi if (!err) 181827932742SMiklos Szeredi err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp)); 181927932742SMiklos Szeredi return err; 182027932742SMiklos Szeredi } 182127932742SMiklos Szeredi 182227932742SMiklos Szeredi static const struct file_operations proc_fdinfo_file_operations = { 182327932742SMiklos Szeredi .open = nonseekable_open, 182427932742SMiklos Szeredi .read = proc_fdinfo_read, 182527932742SMiklos Szeredi }; 182627932742SMiklos Szeredi 182700977a59SArjan van de Ven static const struct file_operations proc_fd_operations = { 18281da177e4SLinus Torvalds .read = generic_read_dir, 18291da177e4SLinus Torvalds .readdir = proc_readfd, 18301da177e4SLinus Torvalds }; 18311da177e4SLinus Torvalds 18321da177e4SLinus Torvalds /* 18338948e11fSAlexey Dobriyan * /proc/pid/fd needs a special permission handler so that a process can still 18348948e11fSAlexey Dobriyan * access /proc/self/fd after it has executed a setuid(). 18358948e11fSAlexey Dobriyan */ 18368948e11fSAlexey Dobriyan static int proc_fd_permission(struct inode *inode, int mask, 18378948e11fSAlexey Dobriyan struct nameidata *nd) 18388948e11fSAlexey Dobriyan { 18398948e11fSAlexey Dobriyan int rv; 18408948e11fSAlexey Dobriyan 18418948e11fSAlexey Dobriyan rv = generic_permission(inode, mask, NULL); 18428948e11fSAlexey Dobriyan if (rv == 0) 18438948e11fSAlexey Dobriyan return 0; 18448948e11fSAlexey Dobriyan if (task_pid(current) == proc_pid(inode)) 18458948e11fSAlexey Dobriyan rv = 0; 18468948e11fSAlexey Dobriyan return rv; 18478948e11fSAlexey Dobriyan } 18488948e11fSAlexey Dobriyan 18498948e11fSAlexey Dobriyan /* 18501da177e4SLinus Torvalds * proc directories can do almost nothing.. 18511da177e4SLinus Torvalds */ 1852c5ef1c42SArjan van de Ven static const struct inode_operations proc_fd_inode_operations = { 18531da177e4SLinus Torvalds .lookup = proc_lookupfd, 18548948e11fSAlexey Dobriyan .permission = proc_fd_permission, 18556d76fa58SLinus Torvalds .setattr = proc_setattr, 18561da177e4SLinus Torvalds }; 18571da177e4SLinus Torvalds 185827932742SMiklos Szeredi static struct dentry *proc_fdinfo_instantiate(struct inode *dir, 185927932742SMiklos Szeredi struct dentry *dentry, struct task_struct *task, const void *ptr) 186027932742SMiklos Szeredi { 186127932742SMiklos Szeredi unsigned fd = *(unsigned *)ptr; 186227932742SMiklos Szeredi struct inode *inode; 186327932742SMiklos Szeredi struct proc_inode *ei; 186427932742SMiklos Szeredi struct dentry *error = ERR_PTR(-ENOENT); 186527932742SMiklos Szeredi 186627932742SMiklos Szeredi inode = proc_pid_make_inode(dir->i_sb, task); 186727932742SMiklos Szeredi if (!inode) 186827932742SMiklos Szeredi goto out; 186927932742SMiklos Szeredi ei = PROC_I(inode); 187027932742SMiklos Szeredi ei->fd = fd; 187127932742SMiklos Szeredi inode->i_mode = S_IFREG | S_IRUSR; 187227932742SMiklos Szeredi inode->i_fop = &proc_fdinfo_file_operations; 187327932742SMiklos Szeredi dentry->d_op = &tid_fd_dentry_operations; 187427932742SMiklos Szeredi d_add(dentry, inode); 187527932742SMiklos Szeredi /* Close the race of the process dying before we return the dentry */ 187627932742SMiklos Szeredi if (tid_fd_revalidate(dentry, NULL)) 187727932742SMiklos Szeredi error = NULL; 187827932742SMiklos Szeredi 187927932742SMiklos Szeredi out: 188027932742SMiklos Szeredi return error; 188127932742SMiklos Szeredi } 188227932742SMiklos Szeredi 188327932742SMiklos Szeredi static struct dentry *proc_lookupfdinfo(struct inode *dir, 188427932742SMiklos Szeredi struct dentry *dentry, 188527932742SMiklos Szeredi struct nameidata *nd) 188627932742SMiklos Szeredi { 188727932742SMiklos Szeredi return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate); 188827932742SMiklos Szeredi } 188927932742SMiklos Szeredi 189027932742SMiklos Szeredi static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir) 189127932742SMiklos Szeredi { 189227932742SMiklos Szeredi return proc_readfd_common(filp, dirent, filldir, 189327932742SMiklos Szeredi proc_fdinfo_instantiate); 189427932742SMiklos Szeredi } 189527932742SMiklos Szeredi 189627932742SMiklos Szeredi static const struct file_operations proc_fdinfo_operations = { 189727932742SMiklos Szeredi .read = generic_read_dir, 189827932742SMiklos Szeredi .readdir = proc_readfdinfo, 189927932742SMiklos Szeredi }; 190027932742SMiklos Szeredi 190127932742SMiklos Szeredi /* 190227932742SMiklos Szeredi * proc directories can do almost nothing.. 190327932742SMiklos Szeredi */ 190427932742SMiklos Szeredi static const struct inode_operations proc_fdinfo_inode_operations = { 190527932742SMiklos Szeredi .lookup = proc_lookupfdinfo, 190627932742SMiklos Szeredi .setattr = proc_setattr, 190727932742SMiklos Szeredi }; 190827932742SMiklos Szeredi 190927932742SMiklos Szeredi 1910444ceed8SEric W. Biederman static struct dentry *proc_pident_instantiate(struct inode *dir, 1911c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 1912444ceed8SEric W. Biederman { 1913c5141e6dSEric Dumazet const struct pid_entry *p = ptr; 1914444ceed8SEric W. Biederman struct inode *inode; 1915444ceed8SEric W. Biederman struct proc_inode *ei; 1916444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-EINVAL); 1917444ceed8SEric W. Biederman 191861a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 1919444ceed8SEric W. Biederman if (!inode) 1920444ceed8SEric W. Biederman goto out; 1921444ceed8SEric W. Biederman 1922444ceed8SEric W. Biederman ei = PROC_I(inode); 1923444ceed8SEric W. Biederman inode->i_mode = p->mode; 1924444ceed8SEric W. Biederman if (S_ISDIR(inode->i_mode)) 1925444ceed8SEric W. Biederman inode->i_nlink = 2; /* Use getattr to fix if necessary */ 1926444ceed8SEric W. Biederman if (p->iop) 1927444ceed8SEric W. Biederman inode->i_op = p->iop; 1928444ceed8SEric W. Biederman if (p->fop) 1929444ceed8SEric W. Biederman inode->i_fop = p->fop; 1930444ceed8SEric W. Biederman ei->op = p->op; 1931444ceed8SEric W. Biederman dentry->d_op = &pid_dentry_operations; 1932444ceed8SEric W. Biederman d_add(dentry, inode); 1933444ceed8SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 1934444ceed8SEric W. Biederman if (pid_revalidate(dentry, NULL)) 1935444ceed8SEric W. Biederman error = NULL; 1936444ceed8SEric W. Biederman out: 1937444ceed8SEric W. Biederman return error; 1938444ceed8SEric W. Biederman } 1939444ceed8SEric W. Biederman 19401da177e4SLinus Torvalds static struct dentry *proc_pident_lookup(struct inode *dir, 19411da177e4SLinus Torvalds struct dentry *dentry, 1942c5141e6dSEric Dumazet const struct pid_entry *ents, 19437bcd6b0eSEric W. Biederman unsigned int nents) 19441da177e4SLinus Torvalds { 19451da177e4SLinus Torvalds struct inode *inode; 1946cd6a3ce9SEric W. Biederman struct dentry *error; 194799f89551SEric W. Biederman struct task_struct *task = get_proc_task(dir); 1948c5141e6dSEric Dumazet const struct pid_entry *p, *last; 19491da177e4SLinus Torvalds 1950cd6a3ce9SEric W. Biederman error = ERR_PTR(-ENOENT); 19511da177e4SLinus Torvalds inode = NULL; 19521da177e4SLinus Torvalds 195399f89551SEric W. Biederman if (!task) 195499f89551SEric W. Biederman goto out_no_task; 19551da177e4SLinus Torvalds 195620cdc894SEric W. Biederman /* 195720cdc894SEric W. Biederman * Yes, it does not scale. And it should not. Don't add 195820cdc894SEric W. Biederman * new entries into /proc/<tgid>/ without very good reasons. 195920cdc894SEric W. Biederman */ 19607bcd6b0eSEric W. Biederman last = &ents[nents - 1]; 19617bcd6b0eSEric W. Biederman for (p = ents; p <= last; p++) { 19621da177e4SLinus Torvalds if (p->len != dentry->d_name.len) 19631da177e4SLinus Torvalds continue; 19641da177e4SLinus Torvalds if (!memcmp(dentry->d_name.name, p->name, p->len)) 19651da177e4SLinus Torvalds break; 19661da177e4SLinus Torvalds } 19677bcd6b0eSEric W. Biederman if (p > last) 19681da177e4SLinus Torvalds goto out; 19691da177e4SLinus Torvalds 1970444ceed8SEric W. Biederman error = proc_pident_instantiate(dir, dentry, task, p); 19711da177e4SLinus Torvalds out: 197299f89551SEric W. Biederman put_task_struct(task); 197399f89551SEric W. Biederman out_no_task: 1974cd6a3ce9SEric W. Biederman return error; 19751da177e4SLinus Torvalds } 19761da177e4SLinus Torvalds 1977c5141e6dSEric Dumazet static int proc_pident_fill_cache(struct file *filp, void *dirent, 1978c5141e6dSEric Dumazet filldir_t filldir, struct task_struct *task, const struct pid_entry *p) 197961a28784SEric W. Biederman { 198061a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, p->name, p->len, 198161a28784SEric W. Biederman proc_pident_instantiate, task, p); 198261a28784SEric W. Biederman } 198361a28784SEric W. Biederman 198428a6d671SEric W. Biederman static int proc_pident_readdir(struct file *filp, 198528a6d671SEric W. Biederman void *dirent, filldir_t filldir, 1986c5141e6dSEric Dumazet const struct pid_entry *ents, unsigned int nents) 198728a6d671SEric W. Biederman { 198828a6d671SEric W. Biederman int i; 19892fddfeefSJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 199028a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 199128a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 1992c5141e6dSEric Dumazet const struct pid_entry *p, *last; 199328a6d671SEric W. Biederman ino_t ino; 199428a6d671SEric W. Biederman int ret; 199528a6d671SEric W. Biederman 199628a6d671SEric W. Biederman ret = -ENOENT; 199728a6d671SEric W. Biederman if (!task) 199861a28784SEric W. Biederman goto out_no_task; 199928a6d671SEric W. Biederman 200028a6d671SEric W. Biederman ret = 0; 200128a6d671SEric W. Biederman i = filp->f_pos; 200228a6d671SEric W. Biederman switch (i) { 200328a6d671SEric W. Biederman case 0: 200428a6d671SEric W. Biederman ino = inode->i_ino; 200528a6d671SEric W. Biederman if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0) 200628a6d671SEric W. Biederman goto out; 200728a6d671SEric W. Biederman i++; 200828a6d671SEric W. Biederman filp->f_pos++; 200928a6d671SEric W. Biederman /* fall through */ 201028a6d671SEric W. Biederman case 1: 201128a6d671SEric W. Biederman ino = parent_ino(dentry); 201228a6d671SEric W. Biederman if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0) 201328a6d671SEric W. Biederman goto out; 201428a6d671SEric W. Biederman i++; 201528a6d671SEric W. Biederman filp->f_pos++; 201628a6d671SEric W. Biederman /* fall through */ 201728a6d671SEric W. Biederman default: 201828a6d671SEric W. Biederman i -= 2; 201928a6d671SEric W. Biederman if (i >= nents) { 202028a6d671SEric W. Biederman ret = 1; 202128a6d671SEric W. Biederman goto out; 202228a6d671SEric W. Biederman } 202328a6d671SEric W. Biederman p = ents + i; 20247bcd6b0eSEric W. Biederman last = &ents[nents - 1]; 20257bcd6b0eSEric W. Biederman while (p <= last) { 202661a28784SEric W. Biederman if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0) 202728a6d671SEric W. Biederman goto out; 202828a6d671SEric W. Biederman filp->f_pos++; 202928a6d671SEric W. Biederman p++; 203028a6d671SEric W. Biederman } 20311da177e4SLinus Torvalds } 20321da177e4SLinus Torvalds 203328a6d671SEric W. Biederman ret = 1; 203428a6d671SEric W. Biederman out: 203561a28784SEric W. Biederman put_task_struct(task); 203661a28784SEric W. Biederman out_no_task: 203728a6d671SEric W. Biederman return ret; 20381da177e4SLinus Torvalds } 20391da177e4SLinus Torvalds 20401da177e4SLinus Torvalds #ifdef CONFIG_SECURITY 204128a6d671SEric W. Biederman static ssize_t proc_pid_attr_read(struct file * file, char __user * buf, 204228a6d671SEric W. Biederman size_t count, loff_t *ppos) 204328a6d671SEric W. Biederman { 20442fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 204504ff9708SAl Viro char *p = NULL; 204628a6d671SEric W. Biederman ssize_t length; 204728a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 204828a6d671SEric W. Biederman 204928a6d671SEric W. Biederman if (!task) 205004ff9708SAl Viro return -ESRCH; 205128a6d671SEric W. Biederman 205228a6d671SEric W. Biederman length = security_getprocattr(task, 20532fddfeefSJosef "Jeff" Sipek (char*)file->f_path.dentry->d_name.name, 205404ff9708SAl Viro &p); 205528a6d671SEric W. Biederman put_task_struct(task); 205604ff9708SAl Viro if (length > 0) 205704ff9708SAl Viro length = simple_read_from_buffer(buf, count, ppos, p, length); 205804ff9708SAl Viro kfree(p); 205928a6d671SEric W. Biederman return length; 206028a6d671SEric W. Biederman } 206128a6d671SEric W. Biederman 206228a6d671SEric W. Biederman static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf, 206328a6d671SEric W. Biederman size_t count, loff_t *ppos) 206428a6d671SEric W. Biederman { 20652fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 206628a6d671SEric W. Biederman char *page; 206728a6d671SEric W. Biederman ssize_t length; 206828a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 206928a6d671SEric W. Biederman 207028a6d671SEric W. Biederman length = -ESRCH; 207128a6d671SEric W. Biederman if (!task) 207228a6d671SEric W. Biederman goto out_no_task; 207328a6d671SEric W. Biederman if (count > PAGE_SIZE) 207428a6d671SEric W. Biederman count = PAGE_SIZE; 207528a6d671SEric W. Biederman 207628a6d671SEric W. Biederman /* No partial writes. */ 207728a6d671SEric W. Biederman length = -EINVAL; 207828a6d671SEric W. Biederman if (*ppos != 0) 207928a6d671SEric W. Biederman goto out; 208028a6d671SEric W. Biederman 208128a6d671SEric W. Biederman length = -ENOMEM; 2082e12ba74dSMel Gorman page = (char*)__get_free_page(GFP_TEMPORARY); 208328a6d671SEric W. Biederman if (!page) 208428a6d671SEric W. Biederman goto out; 208528a6d671SEric W. Biederman 208628a6d671SEric W. Biederman length = -EFAULT; 208728a6d671SEric W. Biederman if (copy_from_user(page, buf, count)) 208828a6d671SEric W. Biederman goto out_free; 208928a6d671SEric W. Biederman 209028a6d671SEric W. Biederman length = security_setprocattr(task, 20912fddfeefSJosef "Jeff" Sipek (char*)file->f_path.dentry->d_name.name, 209228a6d671SEric W. Biederman (void*)page, count); 209328a6d671SEric W. Biederman out_free: 209428a6d671SEric W. Biederman free_page((unsigned long) page); 209528a6d671SEric W. Biederman out: 209628a6d671SEric W. Biederman put_task_struct(task); 209728a6d671SEric W. Biederman out_no_task: 209828a6d671SEric W. Biederman return length; 209928a6d671SEric W. Biederman } 210028a6d671SEric W. Biederman 210100977a59SArjan van de Ven static const struct file_operations proc_pid_attr_operations = { 210228a6d671SEric W. Biederman .read = proc_pid_attr_read, 210328a6d671SEric W. Biederman .write = proc_pid_attr_write, 210428a6d671SEric W. Biederman }; 210528a6d671SEric W. Biederman 2106c5141e6dSEric Dumazet static const struct pid_entry attr_dir_stuff[] = { 210761a28784SEric W. Biederman REG("current", S_IRUGO|S_IWUGO, pid_attr), 210861a28784SEric W. Biederman REG("prev", S_IRUGO, pid_attr), 210961a28784SEric W. Biederman REG("exec", S_IRUGO|S_IWUGO, pid_attr), 211061a28784SEric W. Biederman REG("fscreate", S_IRUGO|S_IWUGO, pid_attr), 211161a28784SEric W. Biederman REG("keycreate", S_IRUGO|S_IWUGO, pid_attr), 211261a28784SEric W. Biederman REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr), 211328a6d671SEric W. Biederman }; 211428a6d671SEric W. Biederman 211572d9dcfcSEric W. Biederman static int proc_attr_dir_readdir(struct file * filp, 21161da177e4SLinus Torvalds void * dirent, filldir_t filldir) 21171da177e4SLinus Torvalds { 21181da177e4SLinus Torvalds return proc_pident_readdir(filp,dirent,filldir, 211972d9dcfcSEric W. Biederman attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff)); 21201da177e4SLinus Torvalds } 21211da177e4SLinus Torvalds 212200977a59SArjan van de Ven static const struct file_operations proc_attr_dir_operations = { 21231da177e4SLinus Torvalds .read = generic_read_dir, 212472d9dcfcSEric W. Biederman .readdir = proc_attr_dir_readdir, 21251da177e4SLinus Torvalds }; 21261da177e4SLinus Torvalds 212772d9dcfcSEric W. Biederman static struct dentry *proc_attr_dir_lookup(struct inode *dir, 21281da177e4SLinus Torvalds struct dentry *dentry, struct nameidata *nd) 21291da177e4SLinus Torvalds { 21307bcd6b0eSEric W. Biederman return proc_pident_lookup(dir, dentry, 21317bcd6b0eSEric W. Biederman attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff)); 21321da177e4SLinus Torvalds } 21331da177e4SLinus Torvalds 2134c5ef1c42SArjan van de Ven static const struct inode_operations proc_attr_dir_inode_operations = { 213572d9dcfcSEric W. Biederman .lookup = proc_attr_dir_lookup, 213699f89551SEric W. Biederman .getattr = pid_getattr, 21376d76fa58SLinus Torvalds .setattr = proc_setattr, 21381da177e4SLinus Torvalds }; 21391da177e4SLinus Torvalds 21401da177e4SLinus Torvalds #endif 21411da177e4SLinus Torvalds 21423cb4a0bbSKawai, Hidehiro #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE) 21433cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf, 21443cb4a0bbSKawai, Hidehiro size_t count, loff_t *ppos) 21453cb4a0bbSKawai, Hidehiro { 21463cb4a0bbSKawai, Hidehiro struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 21473cb4a0bbSKawai, Hidehiro struct mm_struct *mm; 21483cb4a0bbSKawai, Hidehiro char buffer[PROC_NUMBUF]; 21493cb4a0bbSKawai, Hidehiro size_t len; 21503cb4a0bbSKawai, Hidehiro int ret; 21513cb4a0bbSKawai, Hidehiro 21523cb4a0bbSKawai, Hidehiro if (!task) 21533cb4a0bbSKawai, Hidehiro return -ESRCH; 21543cb4a0bbSKawai, Hidehiro 21553cb4a0bbSKawai, Hidehiro ret = 0; 21563cb4a0bbSKawai, Hidehiro mm = get_task_mm(task); 21573cb4a0bbSKawai, Hidehiro if (mm) { 21583cb4a0bbSKawai, Hidehiro len = snprintf(buffer, sizeof(buffer), "%08lx\n", 21593cb4a0bbSKawai, Hidehiro ((mm->flags & MMF_DUMP_FILTER_MASK) >> 21603cb4a0bbSKawai, Hidehiro MMF_DUMP_FILTER_SHIFT)); 21613cb4a0bbSKawai, Hidehiro mmput(mm); 21623cb4a0bbSKawai, Hidehiro ret = simple_read_from_buffer(buf, count, ppos, buffer, len); 21633cb4a0bbSKawai, Hidehiro } 21643cb4a0bbSKawai, Hidehiro 21653cb4a0bbSKawai, Hidehiro put_task_struct(task); 21663cb4a0bbSKawai, Hidehiro 21673cb4a0bbSKawai, Hidehiro return ret; 21683cb4a0bbSKawai, Hidehiro } 21693cb4a0bbSKawai, Hidehiro 21703cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_write(struct file *file, 21713cb4a0bbSKawai, Hidehiro const char __user *buf, 21723cb4a0bbSKawai, Hidehiro size_t count, 21733cb4a0bbSKawai, Hidehiro loff_t *ppos) 21743cb4a0bbSKawai, Hidehiro { 21753cb4a0bbSKawai, Hidehiro struct task_struct *task; 21763cb4a0bbSKawai, Hidehiro struct mm_struct *mm; 21773cb4a0bbSKawai, Hidehiro char buffer[PROC_NUMBUF], *end; 21783cb4a0bbSKawai, Hidehiro unsigned int val; 21793cb4a0bbSKawai, Hidehiro int ret; 21803cb4a0bbSKawai, Hidehiro int i; 21813cb4a0bbSKawai, Hidehiro unsigned long mask; 21823cb4a0bbSKawai, Hidehiro 21833cb4a0bbSKawai, Hidehiro ret = -EFAULT; 21843cb4a0bbSKawai, Hidehiro memset(buffer, 0, sizeof(buffer)); 21853cb4a0bbSKawai, Hidehiro if (count > sizeof(buffer) - 1) 21863cb4a0bbSKawai, Hidehiro count = sizeof(buffer) - 1; 21873cb4a0bbSKawai, Hidehiro if (copy_from_user(buffer, buf, count)) 21883cb4a0bbSKawai, Hidehiro goto out_no_task; 21893cb4a0bbSKawai, Hidehiro 21903cb4a0bbSKawai, Hidehiro ret = -EINVAL; 21913cb4a0bbSKawai, Hidehiro val = (unsigned int)simple_strtoul(buffer, &end, 0); 21923cb4a0bbSKawai, Hidehiro if (*end == '\n') 21933cb4a0bbSKawai, Hidehiro end++; 21943cb4a0bbSKawai, Hidehiro if (end - buffer == 0) 21953cb4a0bbSKawai, Hidehiro goto out_no_task; 21963cb4a0bbSKawai, Hidehiro 21973cb4a0bbSKawai, Hidehiro ret = -ESRCH; 21983cb4a0bbSKawai, Hidehiro task = get_proc_task(file->f_dentry->d_inode); 21993cb4a0bbSKawai, Hidehiro if (!task) 22003cb4a0bbSKawai, Hidehiro goto out_no_task; 22013cb4a0bbSKawai, Hidehiro 22023cb4a0bbSKawai, Hidehiro ret = end - buffer; 22033cb4a0bbSKawai, Hidehiro mm = get_task_mm(task); 22043cb4a0bbSKawai, Hidehiro if (!mm) 22053cb4a0bbSKawai, Hidehiro goto out_no_mm; 22063cb4a0bbSKawai, Hidehiro 22073cb4a0bbSKawai, Hidehiro for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) { 22083cb4a0bbSKawai, Hidehiro if (val & mask) 22093cb4a0bbSKawai, Hidehiro set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags); 22103cb4a0bbSKawai, Hidehiro else 22113cb4a0bbSKawai, Hidehiro clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags); 22123cb4a0bbSKawai, Hidehiro } 22133cb4a0bbSKawai, Hidehiro 22143cb4a0bbSKawai, Hidehiro mmput(mm); 22153cb4a0bbSKawai, Hidehiro out_no_mm: 22163cb4a0bbSKawai, Hidehiro put_task_struct(task); 22173cb4a0bbSKawai, Hidehiro out_no_task: 22183cb4a0bbSKawai, Hidehiro return ret; 22193cb4a0bbSKawai, Hidehiro } 22203cb4a0bbSKawai, Hidehiro 22213cb4a0bbSKawai, Hidehiro static const struct file_operations proc_coredump_filter_operations = { 22223cb4a0bbSKawai, Hidehiro .read = proc_coredump_filter_read, 22233cb4a0bbSKawai, Hidehiro .write = proc_coredump_filter_write, 22243cb4a0bbSKawai, Hidehiro }; 22253cb4a0bbSKawai, Hidehiro #endif 22263cb4a0bbSKawai, Hidehiro 22271da177e4SLinus Torvalds /* 22281da177e4SLinus Torvalds * /proc/self: 22291da177e4SLinus Torvalds */ 22301da177e4SLinus Torvalds static int proc_self_readlink(struct dentry *dentry, char __user *buffer, 22311da177e4SLinus Torvalds int buflen) 22321da177e4SLinus Torvalds { 2233488e5bc4SEric W. Biederman struct pid_namespace *ns = dentry->d_sb->s_fs_info; 2234b55fcb22SAndrew Morton pid_t tgid = task_tgid_nr_ns(current, ns); 22358578cea7SEric W. Biederman char tmp[PROC_NUMBUF]; 2236b55fcb22SAndrew Morton if (!tgid) 2237488e5bc4SEric W. Biederman return -ENOENT; 2238b55fcb22SAndrew Morton sprintf(tmp, "%d", tgid); 22391da177e4SLinus Torvalds return vfs_readlink(dentry,buffer,buflen,tmp); 22401da177e4SLinus Torvalds } 22411da177e4SLinus Torvalds 2242008b150aSAl Viro static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd) 22431da177e4SLinus Torvalds { 2244488e5bc4SEric W. Biederman struct pid_namespace *ns = dentry->d_sb->s_fs_info; 2245b55fcb22SAndrew Morton pid_t tgid = task_tgid_nr_ns(current, ns); 22468578cea7SEric W. Biederman char tmp[PROC_NUMBUF]; 2247b55fcb22SAndrew Morton if (!tgid) 2248488e5bc4SEric W. Biederman return ERR_PTR(-ENOENT); 2249b55fcb22SAndrew Morton sprintf(tmp, "%d", task_tgid_nr_ns(current, ns)); 2250008b150aSAl Viro return ERR_PTR(vfs_follow_link(nd,tmp)); 22511da177e4SLinus Torvalds } 22521da177e4SLinus Torvalds 2253c5ef1c42SArjan van de Ven static const struct inode_operations proc_self_inode_operations = { 22541da177e4SLinus Torvalds .readlink = proc_self_readlink, 22551da177e4SLinus Torvalds .follow_link = proc_self_follow_link, 22561da177e4SLinus Torvalds }; 22571da177e4SLinus Torvalds 225828a6d671SEric W. Biederman /* 2259801199ceSEric W. Biederman * proc base 2260801199ceSEric W. Biederman * 2261801199ceSEric W. Biederman * These are the directory entries in the root directory of /proc 2262801199ceSEric W. Biederman * that properly belong to the /proc filesystem, as they describe 2263801199ceSEric W. Biederman * describe something that is process related. 2264801199ceSEric W. Biederman */ 2265c5141e6dSEric Dumazet static const struct pid_entry proc_base_stuff[] = { 226661a28784SEric W. Biederman NOD("self", S_IFLNK|S_IRWXUGO, 2267801199ceSEric W. Biederman &proc_self_inode_operations, NULL, {}), 2268801199ceSEric W. Biederman }; 2269801199ceSEric W. Biederman 2270801199ceSEric W. Biederman /* 2271801199ceSEric W. Biederman * Exceptional case: normally we are not allowed to unhash a busy 2272801199ceSEric W. Biederman * directory. In this case, however, we can do it - no aliasing problems 2273801199ceSEric W. Biederman * due to the way we treat inodes. 2274801199ceSEric W. Biederman */ 2275801199ceSEric W. Biederman static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd) 2276801199ceSEric W. Biederman { 2277801199ceSEric W. Biederman struct inode *inode = dentry->d_inode; 2278801199ceSEric W. Biederman struct task_struct *task = get_proc_task(inode); 2279801199ceSEric W. Biederman if (task) { 2280801199ceSEric W. Biederman put_task_struct(task); 2281801199ceSEric W. Biederman return 1; 2282801199ceSEric W. Biederman } 2283801199ceSEric W. Biederman d_drop(dentry); 2284801199ceSEric W. Biederman return 0; 2285801199ceSEric W. Biederman } 2286801199ceSEric W. Biederman 2287801199ceSEric W. Biederman static struct dentry_operations proc_base_dentry_operations = 2288801199ceSEric W. Biederman { 2289801199ceSEric W. Biederman .d_revalidate = proc_base_revalidate, 2290801199ceSEric W. Biederman .d_delete = pid_delete_dentry, 2291801199ceSEric W. Biederman }; 2292801199ceSEric W. Biederman 2293444ceed8SEric W. Biederman static struct dentry *proc_base_instantiate(struct inode *dir, 2294c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 2295801199ceSEric W. Biederman { 2296c5141e6dSEric Dumazet const struct pid_entry *p = ptr; 2297801199ceSEric W. Biederman struct inode *inode; 2298801199ceSEric W. Biederman struct proc_inode *ei; 2299444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-EINVAL); 2300801199ceSEric W. Biederman 2301801199ceSEric W. Biederman /* Allocate the inode */ 2302801199ceSEric W. Biederman error = ERR_PTR(-ENOMEM); 2303801199ceSEric W. Biederman inode = new_inode(dir->i_sb); 2304801199ceSEric W. Biederman if (!inode) 2305801199ceSEric W. Biederman goto out; 2306801199ceSEric W. Biederman 2307801199ceSEric W. Biederman /* Initialize the inode */ 2308801199ceSEric W. Biederman ei = PROC_I(inode); 2309801199ceSEric W. Biederman inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; 2310801199ceSEric W. Biederman 2311801199ceSEric W. Biederman /* 2312801199ceSEric W. Biederman * grab the reference to the task. 2313801199ceSEric W. Biederman */ 23141a657f78SOleg Nesterov ei->pid = get_task_pid(task, PIDTYPE_PID); 2315801199ceSEric W. Biederman if (!ei->pid) 2316801199ceSEric W. Biederman goto out_iput; 2317801199ceSEric W. Biederman 2318801199ceSEric W. Biederman inode->i_uid = 0; 2319801199ceSEric W. Biederman inode->i_gid = 0; 2320801199ceSEric W. Biederman inode->i_mode = p->mode; 2321801199ceSEric W. Biederman if (S_ISDIR(inode->i_mode)) 2322801199ceSEric W. Biederman inode->i_nlink = 2; 2323801199ceSEric W. Biederman if (S_ISLNK(inode->i_mode)) 2324801199ceSEric W. Biederman inode->i_size = 64; 2325801199ceSEric W. Biederman if (p->iop) 2326801199ceSEric W. Biederman inode->i_op = p->iop; 2327801199ceSEric W. Biederman if (p->fop) 2328801199ceSEric W. Biederman inode->i_fop = p->fop; 2329801199ceSEric W. Biederman ei->op = p->op; 2330801199ceSEric W. Biederman dentry->d_op = &proc_base_dentry_operations; 2331801199ceSEric W. Biederman d_add(dentry, inode); 2332801199ceSEric W. Biederman error = NULL; 2333801199ceSEric W. Biederman out: 2334801199ceSEric W. Biederman return error; 2335801199ceSEric W. Biederman out_iput: 2336801199ceSEric W. Biederman iput(inode); 2337801199ceSEric W. Biederman goto out; 2338801199ceSEric W. Biederman } 2339801199ceSEric W. Biederman 2340444ceed8SEric W. Biederman static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry) 2341444ceed8SEric W. Biederman { 2342444ceed8SEric W. Biederman struct dentry *error; 2343444ceed8SEric W. Biederman struct task_struct *task = get_proc_task(dir); 2344c5141e6dSEric Dumazet const struct pid_entry *p, *last; 2345444ceed8SEric W. Biederman 2346444ceed8SEric W. Biederman error = ERR_PTR(-ENOENT); 2347444ceed8SEric W. Biederman 2348444ceed8SEric W. Biederman if (!task) 2349444ceed8SEric W. Biederman goto out_no_task; 2350444ceed8SEric W. Biederman 2351444ceed8SEric W. Biederman /* Lookup the directory entry */ 23527bcd6b0eSEric W. Biederman last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1]; 23537bcd6b0eSEric W. Biederman for (p = proc_base_stuff; p <= last; p++) { 2354444ceed8SEric W. Biederman if (p->len != dentry->d_name.len) 2355444ceed8SEric W. Biederman continue; 2356444ceed8SEric W. Biederman if (!memcmp(dentry->d_name.name, p->name, p->len)) 2357444ceed8SEric W. Biederman break; 2358444ceed8SEric W. Biederman } 23597bcd6b0eSEric W. Biederman if (p > last) 2360444ceed8SEric W. Biederman goto out; 2361444ceed8SEric W. Biederman 2362444ceed8SEric W. Biederman error = proc_base_instantiate(dir, dentry, task, p); 2363444ceed8SEric W. Biederman 2364444ceed8SEric W. Biederman out: 2365444ceed8SEric W. Biederman put_task_struct(task); 2366444ceed8SEric W. Biederman out_no_task: 2367444ceed8SEric W. Biederman return error; 2368444ceed8SEric W. Biederman } 2369444ceed8SEric W. Biederman 2370c5141e6dSEric Dumazet static int proc_base_fill_cache(struct file *filp, void *dirent, 2371c5141e6dSEric Dumazet filldir_t filldir, struct task_struct *task, const struct pid_entry *p) 237261a28784SEric W. Biederman { 237361a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, p->name, p->len, 237461a28784SEric W. Biederman proc_base_instantiate, task, p); 237561a28784SEric W. Biederman } 237661a28784SEric W. Biederman 2377aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING 2378aba76fdbSAndrew Morton static int proc_pid_io_accounting(struct task_struct *task, char *buffer) 2379aba76fdbSAndrew Morton { 2380aba76fdbSAndrew Morton return sprintf(buffer, 23814b98d11bSAlexey Dobriyan #ifdef CONFIG_TASK_XACCT 2382aba76fdbSAndrew Morton "rchar: %llu\n" 2383aba76fdbSAndrew Morton "wchar: %llu\n" 2384aba76fdbSAndrew Morton "syscr: %llu\n" 2385aba76fdbSAndrew Morton "syscw: %llu\n" 23864b98d11bSAlexey Dobriyan #endif 2387aba76fdbSAndrew Morton "read_bytes: %llu\n" 2388aba76fdbSAndrew Morton "write_bytes: %llu\n" 2389aba76fdbSAndrew Morton "cancelled_write_bytes: %llu\n", 23904b98d11bSAlexey Dobriyan #ifdef CONFIG_TASK_XACCT 2391aba76fdbSAndrew Morton (unsigned long long)task->rchar, 2392aba76fdbSAndrew Morton (unsigned long long)task->wchar, 2393aba76fdbSAndrew Morton (unsigned long long)task->syscr, 2394aba76fdbSAndrew Morton (unsigned long long)task->syscw, 23954b98d11bSAlexey Dobriyan #endif 2396aba76fdbSAndrew Morton (unsigned long long)task->ioac.read_bytes, 2397aba76fdbSAndrew Morton (unsigned long long)task->ioac.write_bytes, 2398aba76fdbSAndrew Morton (unsigned long long)task->ioac.cancelled_write_bytes); 2399aba76fdbSAndrew Morton } 2400aba76fdbSAndrew Morton #endif 2401aba76fdbSAndrew Morton 2402801199ceSEric W. Biederman /* 240328a6d671SEric W. Biederman * Thread groups 240428a6d671SEric W. Biederman */ 240500977a59SArjan van de Ven static const struct file_operations proc_task_operations; 2406c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations; 240720cdc894SEric W. Biederman 2408c5141e6dSEric Dumazet static const struct pid_entry tgid_base_stuff[] = { 240961a28784SEric W. Biederman DIR("task", S_IRUGO|S_IXUGO, task), 241061a28784SEric W. Biederman DIR("fd", S_IRUSR|S_IXUSR, fd), 241127932742SMiklos Szeredi DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo), 2412b2211a36SAndrew Morton #ifdef CONFIG_NET 24134f42c288SAndre Noll DIR("net", S_IRUGO|S_IXUGO, net), 2414b2211a36SAndrew Morton #endif 2415315e28c8SJames Pearson REG("environ", S_IRUSR, environ), 241661a28784SEric W. Biederman INF("auxv", S_IRUSR, pid_auxv), 2417df5f8314SEric W. Biederman ONE("status", S_IRUGO, pid_status), 2418d85f50d5SNeil Horman INF("limits", S_IRUSR, pid_limits), 241943ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG 242043ae34cbSIngo Molnar REG("sched", S_IRUGO|S_IWUSR, pid_sched), 242143ae34cbSIngo Molnar #endif 242261a28784SEric W. Biederman INF("cmdline", S_IRUGO, pid_cmdline), 2423ee992744SEric W. Biederman ONE("stat", S_IRUGO, tgid_stat), 2424a56d3fc7SEric W. Biederman ONE("statm", S_IRUGO, pid_statm), 242561a28784SEric W. Biederman REG("maps", S_IRUGO, maps), 242628a6d671SEric W. Biederman #ifdef CONFIG_NUMA 242761a28784SEric W. Biederman REG("numa_maps", S_IRUGO, numa_maps), 242828a6d671SEric W. Biederman #endif 242961a28784SEric W. Biederman REG("mem", S_IRUSR|S_IWUSR, mem), 243061a28784SEric W. Biederman LNK("cwd", cwd), 243161a28784SEric W. Biederman LNK("root", root), 243261a28784SEric W. Biederman LNK("exe", exe), 243361a28784SEric W. Biederman REG("mounts", S_IRUGO, mounts), 24342d4d4864SRam Pai REG("mountinfo", S_IRUGO, mountinfo), 243561a28784SEric W. Biederman REG("mountstats", S_IRUSR, mountstats), 24361e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR 2437b813e931SDavid Rientjes REG("clear_refs", S_IWUSR, clear_refs), 243861a28784SEric W. Biederman REG("smaps", S_IRUGO, smaps), 243985863e47SMatt Mackall REG("pagemap", S_IRUSR, pagemap), 244028a6d671SEric W. Biederman #endif 244128a6d671SEric W. Biederman #ifdef CONFIG_SECURITY 244272d9dcfcSEric W. Biederman DIR("attr", S_IRUGO|S_IXUGO, attr_dir), 244328a6d671SEric W. Biederman #endif 244428a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS 244561a28784SEric W. Biederman INF("wchan", S_IRUGO, pid_wchan), 244628a6d671SEric W. Biederman #endif 244728a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS 244861a28784SEric W. Biederman INF("schedstat", S_IRUGO, pid_schedstat), 244928a6d671SEric W. Biederman #endif 24509745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP 24519745512cSArjan van de Ven REG("latency", S_IRUGO, lstats), 24529745512cSArjan van de Ven #endif 24538793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET 245461a28784SEric W. Biederman REG("cpuset", S_IRUGO, cpuset), 245528a6d671SEric W. Biederman #endif 2456a424316cSPaul Menage #ifdef CONFIG_CGROUPS 2457a424316cSPaul Menage REG("cgroup", S_IRUGO, cgroup), 2458a424316cSPaul Menage #endif 245961a28784SEric W. Biederman INF("oom_score", S_IRUGO, oom_score), 246061a28784SEric W. Biederman REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust), 246128a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL 246261a28784SEric W. Biederman REG("loginuid", S_IWUSR|S_IRUGO, loginuid), 24636ee65046SSteve Grubb REG("sessionid", S_IRUGO, sessionid), 246428a6d671SEric W. Biederman #endif 2465f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION 2466f4f154fdSAkinobu Mita REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject), 2467f4f154fdSAkinobu Mita #endif 24683cb4a0bbSKawai, Hidehiro #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE) 24693cb4a0bbSKawai, Hidehiro REG("coredump_filter", S_IRUGO|S_IWUSR, coredump_filter), 24703cb4a0bbSKawai, Hidehiro #endif 2471aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING 2472aba76fdbSAndrew Morton INF("io", S_IRUGO, pid_io_accounting), 2473aba76fdbSAndrew Morton #endif 247428a6d671SEric W. Biederman }; 247528a6d671SEric W. Biederman 247628a6d671SEric W. Biederman static int proc_tgid_base_readdir(struct file * filp, 247728a6d671SEric W. Biederman void * dirent, filldir_t filldir) 247828a6d671SEric W. Biederman { 247928a6d671SEric W. Biederman return proc_pident_readdir(filp,dirent,filldir, 248028a6d671SEric W. Biederman tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff)); 248128a6d671SEric W. Biederman } 248228a6d671SEric W. Biederman 248300977a59SArjan van de Ven static const struct file_operations proc_tgid_base_operations = { 248428a6d671SEric W. Biederman .read = generic_read_dir, 248528a6d671SEric W. Biederman .readdir = proc_tgid_base_readdir, 248628a6d671SEric W. Biederman }; 248728a6d671SEric W. Biederman 248828a6d671SEric W. Biederman static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){ 24897bcd6b0eSEric W. Biederman return proc_pident_lookup(dir, dentry, 24907bcd6b0eSEric W. Biederman tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff)); 249128a6d671SEric W. Biederman } 249228a6d671SEric W. Biederman 2493c5ef1c42SArjan van de Ven static const struct inode_operations proc_tgid_base_inode_operations = { 249428a6d671SEric W. Biederman .lookup = proc_tgid_base_lookup, 249528a6d671SEric W. Biederman .getattr = pid_getattr, 249628a6d671SEric W. Biederman .setattr = proc_setattr, 249728a6d671SEric W. Biederman }; 249828a6d671SEric W. Biederman 249960347f67SPavel Emelyanov static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid) 25001da177e4SLinus Torvalds { 250148e6484dSEric W. Biederman struct dentry *dentry, *leader, *dir; 25028578cea7SEric W. Biederman char buf[PROC_NUMBUF]; 250348e6484dSEric W. Biederman struct qstr name; 25041da177e4SLinus Torvalds 250548e6484dSEric W. Biederman name.name = buf; 250660347f67SPavel Emelyanov name.len = snprintf(buf, sizeof(buf), "%d", pid); 250760347f67SPavel Emelyanov dentry = d_hash_and_lookup(mnt->mnt_root, &name); 250848e6484dSEric W. Biederman if (dentry) { 25097766755aSAndrea Arcangeli if (!(current->flags & PF_EXITING)) 251048e6484dSEric W. Biederman shrink_dcache_parent(dentry); 251148e6484dSEric W. Biederman d_drop(dentry); 251248e6484dSEric W. Biederman dput(dentry); 25131da177e4SLinus Torvalds } 25141da177e4SLinus Torvalds 251560347f67SPavel Emelyanov if (tgid == 0) 251648e6484dSEric W. Biederman goto out; 25171da177e4SLinus Torvalds 251848e6484dSEric W. Biederman name.name = buf; 251960347f67SPavel Emelyanov name.len = snprintf(buf, sizeof(buf), "%d", tgid); 252060347f67SPavel Emelyanov leader = d_hash_and_lookup(mnt->mnt_root, &name); 252148e6484dSEric W. Biederman if (!leader) 252248e6484dSEric W. Biederman goto out; 252348e6484dSEric W. Biederman 252448e6484dSEric W. Biederman name.name = "task"; 252548e6484dSEric W. Biederman name.len = strlen(name.name); 252648e6484dSEric W. Biederman dir = d_hash_and_lookup(leader, &name); 252748e6484dSEric W. Biederman if (!dir) 252848e6484dSEric W. Biederman goto out_put_leader; 252948e6484dSEric W. Biederman 253048e6484dSEric W. Biederman name.name = buf; 253160347f67SPavel Emelyanov name.len = snprintf(buf, sizeof(buf), "%d", pid); 253248e6484dSEric W. Biederman dentry = d_hash_and_lookup(dir, &name); 253348e6484dSEric W. Biederman if (dentry) { 253448e6484dSEric W. Biederman shrink_dcache_parent(dentry); 253548e6484dSEric W. Biederman d_drop(dentry); 253648e6484dSEric W. Biederman dput(dentry); 25371da177e4SLinus Torvalds } 253848e6484dSEric W. Biederman 253948e6484dSEric W. Biederman dput(dir); 254048e6484dSEric W. Biederman out_put_leader: 254148e6484dSEric W. Biederman dput(leader); 254248e6484dSEric W. Biederman out: 254348e6484dSEric W. Biederman return; 25441da177e4SLinus Torvalds } 25451da177e4SLinus Torvalds 25460895e91dSRandy Dunlap /** 25470895e91dSRandy Dunlap * proc_flush_task - Remove dcache entries for @task from the /proc dcache. 25480895e91dSRandy Dunlap * @task: task that should be flushed. 25490895e91dSRandy Dunlap * 25500895e91dSRandy Dunlap * When flushing dentries from proc, one needs to flush them from global 255160347f67SPavel Emelyanov * proc (proc_mnt) and from all the namespaces' procs this task was seen 25520895e91dSRandy Dunlap * in. This call is supposed to do all of this job. 25530895e91dSRandy Dunlap * 25540895e91dSRandy Dunlap * Looks in the dcache for 25550895e91dSRandy Dunlap * /proc/@pid 25560895e91dSRandy Dunlap * /proc/@tgid/task/@pid 25570895e91dSRandy Dunlap * if either directory is present flushes it and all of it'ts children 25580895e91dSRandy Dunlap * from the dcache. 25590895e91dSRandy Dunlap * 25600895e91dSRandy Dunlap * It is safe and reasonable to cache /proc entries for a task until 25610895e91dSRandy Dunlap * that task exits. After that they just clog up the dcache with 25620895e91dSRandy Dunlap * useless entries, possibly causing useful dcache entries to be 25630895e91dSRandy Dunlap * flushed instead. This routine is proved to flush those useless 25640895e91dSRandy Dunlap * dcache entries at process exit time. 25650895e91dSRandy Dunlap * 25660895e91dSRandy Dunlap * NOTE: This routine is just an optimization so it does not guarantee 25670895e91dSRandy Dunlap * that no dcache entries will exist at process exit time it 25680895e91dSRandy Dunlap * just makes it very unlikely that any will persist. 256960347f67SPavel Emelyanov */ 257060347f67SPavel Emelyanov 257160347f67SPavel Emelyanov void proc_flush_task(struct task_struct *task) 257260347f67SPavel Emelyanov { 25739fcc2d15SEric W. Biederman int i; 25749fcc2d15SEric W. Biederman struct pid *pid, *tgid = NULL; 2575130f77ecSPavel Emelyanov struct upid *upid; 2576130f77ecSPavel Emelyanov 2577130f77ecSPavel Emelyanov pid = task_pid(task); 25789fcc2d15SEric W. Biederman if (thread_group_leader(task)) 2579130f77ecSPavel Emelyanov tgid = task_tgid(task); 25809fcc2d15SEric W. Biederman 25819fcc2d15SEric W. Biederman for (i = 0; i <= pid->level; i++) { 2582130f77ecSPavel Emelyanov upid = &pid->numbers[i]; 2583130f77ecSPavel Emelyanov proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr, 25849fcc2d15SEric W. Biederman tgid ? tgid->numbers[i].nr : 0); 2585130f77ecSPavel Emelyanov } 25866f4e6433SPavel Emelyanov 25876f4e6433SPavel Emelyanov upid = &pid->numbers[pid->level]; 25886f4e6433SPavel Emelyanov if (upid->nr == 1) 25896f4e6433SPavel Emelyanov pid_ns_release_proc(upid->ns); 259060347f67SPavel Emelyanov } 259160347f67SPavel Emelyanov 25929711ef99SAdrian Bunk static struct dentry *proc_pid_instantiate(struct inode *dir, 25939711ef99SAdrian Bunk struct dentry * dentry, 2594c5141e6dSEric Dumazet struct task_struct *task, const void *ptr) 2595444ceed8SEric W. Biederman { 2596444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-ENOENT); 2597444ceed8SEric W. Biederman struct inode *inode; 2598444ceed8SEric W. Biederman 259961a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 2600444ceed8SEric W. Biederman if (!inode) 2601444ceed8SEric W. Biederman goto out; 2602444ceed8SEric W. Biederman 2603444ceed8SEric W. Biederman inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO; 2604444ceed8SEric W. Biederman inode->i_op = &proc_tgid_base_inode_operations; 2605444ceed8SEric W. Biederman inode->i_fop = &proc_tgid_base_operations; 2606444ceed8SEric W. Biederman inode->i_flags|=S_IMMUTABLE; 2607*aed54175SVegard Nossum 2608*aed54175SVegard Nossum inode->i_nlink = 2 + pid_entry_count_dirs(tgid_base_stuff, 2609*aed54175SVegard Nossum ARRAY_SIZE(tgid_base_stuff)); 2610444ceed8SEric W. Biederman 2611444ceed8SEric W. Biederman dentry->d_op = &pid_dentry_operations; 2612444ceed8SEric W. Biederman 2613444ceed8SEric W. Biederman d_add(dentry, inode); 2614444ceed8SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 2615444ceed8SEric W. Biederman if (pid_revalidate(dentry, NULL)) 2616444ceed8SEric W. Biederman error = NULL; 2617444ceed8SEric W. Biederman out: 2618444ceed8SEric W. Biederman return error; 2619444ceed8SEric W. Biederman } 2620444ceed8SEric W. Biederman 26211da177e4SLinus Torvalds struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd) 26221da177e4SLinus Torvalds { 2623cd6a3ce9SEric W. Biederman struct dentry *result = ERR_PTR(-ENOENT); 26241da177e4SLinus Torvalds struct task_struct *task; 26251da177e4SLinus Torvalds unsigned tgid; 2626b488893aSPavel Emelyanov struct pid_namespace *ns; 26271da177e4SLinus Torvalds 2628801199ceSEric W. Biederman result = proc_base_lookup(dir, dentry); 2629801199ceSEric W. Biederman if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT) 2630801199ceSEric W. Biederman goto out; 2631801199ceSEric W. Biederman 26321da177e4SLinus Torvalds tgid = name_to_int(dentry); 26331da177e4SLinus Torvalds if (tgid == ~0U) 26341da177e4SLinus Torvalds goto out; 26351da177e4SLinus Torvalds 2636b488893aSPavel Emelyanov ns = dentry->d_sb->s_fs_info; 2637de758734SEric W. Biederman rcu_read_lock(); 2638b488893aSPavel Emelyanov task = find_task_by_pid_ns(tgid, ns); 26391da177e4SLinus Torvalds if (task) 26401da177e4SLinus Torvalds get_task_struct(task); 2641de758734SEric W. Biederman rcu_read_unlock(); 26421da177e4SLinus Torvalds if (!task) 26431da177e4SLinus Torvalds goto out; 26441da177e4SLinus Torvalds 2645444ceed8SEric W. Biederman result = proc_pid_instantiate(dir, dentry, task, NULL); 264648e6484dSEric W. Biederman put_task_struct(task); 26471da177e4SLinus Torvalds out: 2648cd6a3ce9SEric W. Biederman return result; 26491da177e4SLinus Torvalds } 26501da177e4SLinus Torvalds 26511da177e4SLinus Torvalds /* 26520804ef4bSEric W. Biederman * Find the first task with tgid >= tgid 26530bc58a91SEric W. Biederman * 26541da177e4SLinus Torvalds */ 265519fd4bb2SEric W. Biederman struct tgid_iter { 265619fd4bb2SEric W. Biederman unsigned int tgid; 26570804ef4bSEric W. Biederman struct task_struct *task; 265819fd4bb2SEric W. Biederman }; 265919fd4bb2SEric W. Biederman static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter) 266019fd4bb2SEric W. Biederman { 26610804ef4bSEric W. Biederman struct pid *pid; 26621da177e4SLinus Torvalds 266319fd4bb2SEric W. Biederman if (iter.task) 266419fd4bb2SEric W. Biederman put_task_struct(iter.task); 26650804ef4bSEric W. Biederman rcu_read_lock(); 26660804ef4bSEric W. Biederman retry: 266719fd4bb2SEric W. Biederman iter.task = NULL; 266819fd4bb2SEric W. Biederman pid = find_ge_pid(iter.tgid, ns); 26690804ef4bSEric W. Biederman if (pid) { 267019fd4bb2SEric W. Biederman iter.tgid = pid_nr_ns(pid, ns); 267119fd4bb2SEric W. Biederman iter.task = pid_task(pid, PIDTYPE_PID); 26720804ef4bSEric W. Biederman /* What we to know is if the pid we have find is the 26730804ef4bSEric W. Biederman * pid of a thread_group_leader. Testing for task 26740804ef4bSEric W. Biederman * being a thread_group_leader is the obvious thing 26750804ef4bSEric W. Biederman * todo but there is a window when it fails, due to 26760804ef4bSEric W. Biederman * the pid transfer logic in de_thread. 26770804ef4bSEric W. Biederman * 26780804ef4bSEric W. Biederman * So we perform the straight forward test of seeing 26790804ef4bSEric W. Biederman * if the pid we have found is the pid of a thread 26800804ef4bSEric W. Biederman * group leader, and don't worry if the task we have 26810804ef4bSEric W. Biederman * found doesn't happen to be a thread group leader. 26820804ef4bSEric W. Biederman * As we don't care in the case of readdir. 26830bc58a91SEric W. Biederman */ 268419fd4bb2SEric W. Biederman if (!iter.task || !has_group_leader_pid(iter.task)) { 268519fd4bb2SEric W. Biederman iter.tgid += 1; 26860804ef4bSEric W. Biederman goto retry; 268719fd4bb2SEric W. Biederman } 268819fd4bb2SEric W. Biederman get_task_struct(iter.task); 26891da177e4SLinus Torvalds } 2690454cc105SEric W. Biederman rcu_read_unlock(); 269119fd4bb2SEric W. Biederman return iter; 26921da177e4SLinus Torvalds } 26931da177e4SLinus Torvalds 26947bcd6b0eSEric W. Biederman #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff)) 26951da177e4SLinus Torvalds 269661a28784SEric W. Biederman static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir, 269719fd4bb2SEric W. Biederman struct tgid_iter iter) 269861a28784SEric W. Biederman { 269961a28784SEric W. Biederman char name[PROC_NUMBUF]; 270019fd4bb2SEric W. Biederman int len = snprintf(name, sizeof(name), "%d", iter.tgid); 270161a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, name, len, 270219fd4bb2SEric W. Biederman proc_pid_instantiate, iter.task, NULL); 270361a28784SEric W. Biederman } 270461a28784SEric W. Biederman 27051da177e4SLinus Torvalds /* for the /proc/ directory itself, after non-process stuff has been done */ 27061da177e4SLinus Torvalds int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir) 27071da177e4SLinus Torvalds { 27081da177e4SLinus Torvalds unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY; 27092fddfeefSJosef "Jeff" Sipek struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode); 271019fd4bb2SEric W. Biederman struct tgid_iter iter; 2711b488893aSPavel Emelyanov struct pid_namespace *ns; 27121da177e4SLinus Torvalds 271361a28784SEric W. Biederman if (!reaper) 271461a28784SEric W. Biederman goto out_no_task; 271561a28784SEric W. Biederman 27167bcd6b0eSEric W. Biederman for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) { 2717c5141e6dSEric Dumazet const struct pid_entry *p = &proc_base_stuff[nr]; 271861a28784SEric W. Biederman if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0) 2719801199ceSEric W. Biederman goto out; 27201da177e4SLinus Torvalds } 27211da177e4SLinus Torvalds 2722b488893aSPavel Emelyanov ns = filp->f_dentry->d_sb->s_fs_info; 272319fd4bb2SEric W. Biederman iter.task = NULL; 272419fd4bb2SEric W. Biederman iter.tgid = filp->f_pos - TGID_OFFSET; 272519fd4bb2SEric W. Biederman for (iter = next_tgid(ns, iter); 272619fd4bb2SEric W. Biederman iter.task; 272719fd4bb2SEric W. Biederman iter.tgid += 1, iter = next_tgid(ns, iter)) { 272819fd4bb2SEric W. Biederman filp->f_pos = iter.tgid + TGID_OFFSET; 272919fd4bb2SEric W. Biederman if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) { 273019fd4bb2SEric W. Biederman put_task_struct(iter.task); 27310804ef4bSEric W. Biederman goto out; 27321da177e4SLinus Torvalds } 27331da177e4SLinus Torvalds } 27340804ef4bSEric W. Biederman filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET; 27350804ef4bSEric W. Biederman out: 273661a28784SEric W. Biederman put_task_struct(reaper); 273761a28784SEric W. Biederman out_no_task: 27381da177e4SLinus Torvalds return 0; 27391da177e4SLinus Torvalds } 27401da177e4SLinus Torvalds 27410bc58a91SEric W. Biederman /* 274228a6d671SEric W. Biederman * Tasks 274328a6d671SEric W. Biederman */ 2744c5141e6dSEric Dumazet static const struct pid_entry tid_base_stuff[] = { 274561a28784SEric W. Biederman DIR("fd", S_IRUSR|S_IXUSR, fd), 274627932742SMiklos Szeredi DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo), 2747315e28c8SJames Pearson REG("environ", S_IRUSR, environ), 274861a28784SEric W. Biederman INF("auxv", S_IRUSR, pid_auxv), 2749df5f8314SEric W. Biederman ONE("status", S_IRUGO, pid_status), 2750d85f50d5SNeil Horman INF("limits", S_IRUSR, pid_limits), 275143ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG 275243ae34cbSIngo Molnar REG("sched", S_IRUGO|S_IWUSR, pid_sched), 275343ae34cbSIngo Molnar #endif 275461a28784SEric W. Biederman INF("cmdline", S_IRUGO, pid_cmdline), 2755ee992744SEric W. Biederman ONE("stat", S_IRUGO, tid_stat), 2756a56d3fc7SEric W. Biederman ONE("statm", S_IRUGO, pid_statm), 275761a28784SEric W. Biederman REG("maps", S_IRUGO, maps), 275828a6d671SEric W. Biederman #ifdef CONFIG_NUMA 275961a28784SEric W. Biederman REG("numa_maps", S_IRUGO, numa_maps), 276028a6d671SEric W. Biederman #endif 276161a28784SEric W. Biederman REG("mem", S_IRUSR|S_IWUSR, mem), 276261a28784SEric W. Biederman LNK("cwd", cwd), 276361a28784SEric W. Biederman LNK("root", root), 276461a28784SEric W. Biederman LNK("exe", exe), 276561a28784SEric W. Biederman REG("mounts", S_IRUGO, mounts), 27662d4d4864SRam Pai REG("mountinfo", S_IRUGO, mountinfo), 27671e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR 2768b813e931SDavid Rientjes REG("clear_refs", S_IWUSR, clear_refs), 276961a28784SEric W. Biederman REG("smaps", S_IRUGO, smaps), 277085863e47SMatt Mackall REG("pagemap", S_IRUSR, pagemap), 277128a6d671SEric W. Biederman #endif 277228a6d671SEric W. Biederman #ifdef CONFIG_SECURITY 277372d9dcfcSEric W. Biederman DIR("attr", S_IRUGO|S_IXUGO, attr_dir), 277428a6d671SEric W. Biederman #endif 277528a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS 277661a28784SEric W. Biederman INF("wchan", S_IRUGO, pid_wchan), 277728a6d671SEric W. Biederman #endif 277828a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS 277961a28784SEric W. Biederman INF("schedstat", S_IRUGO, pid_schedstat), 278028a6d671SEric W. Biederman #endif 27819745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP 27829745512cSArjan van de Ven REG("latency", S_IRUGO, lstats), 27839745512cSArjan van de Ven #endif 27848793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET 278561a28784SEric W. Biederman REG("cpuset", S_IRUGO, cpuset), 278628a6d671SEric W. Biederman #endif 2787a424316cSPaul Menage #ifdef CONFIG_CGROUPS 2788a424316cSPaul Menage REG("cgroup", S_IRUGO, cgroup), 2789a424316cSPaul Menage #endif 279061a28784SEric W. Biederman INF("oom_score", S_IRUGO, oom_score), 279161a28784SEric W. Biederman REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust), 279228a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL 279361a28784SEric W. Biederman REG("loginuid", S_IWUSR|S_IRUGO, loginuid), 27941e0bd755SEric Paris REG("sessionid", S_IRUSR, sessionid), 279528a6d671SEric W. Biederman #endif 2796f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION 2797f4f154fdSAkinobu Mita REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject), 2798f4f154fdSAkinobu Mita #endif 279928a6d671SEric W. Biederman }; 280028a6d671SEric W. Biederman 280128a6d671SEric W. Biederman static int proc_tid_base_readdir(struct file * filp, 280228a6d671SEric W. Biederman void * dirent, filldir_t filldir) 280328a6d671SEric W. Biederman { 280428a6d671SEric W. Biederman return proc_pident_readdir(filp,dirent,filldir, 280528a6d671SEric W. Biederman tid_base_stuff,ARRAY_SIZE(tid_base_stuff)); 280628a6d671SEric W. Biederman } 280728a6d671SEric W. Biederman 280828a6d671SEric W. Biederman static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){ 28097bcd6b0eSEric W. Biederman return proc_pident_lookup(dir, dentry, 28107bcd6b0eSEric W. Biederman tid_base_stuff, ARRAY_SIZE(tid_base_stuff)); 281128a6d671SEric W. Biederman } 281228a6d671SEric W. Biederman 281300977a59SArjan van de Ven static const struct file_operations proc_tid_base_operations = { 281428a6d671SEric W. Biederman .read = generic_read_dir, 281528a6d671SEric W. Biederman .readdir = proc_tid_base_readdir, 281628a6d671SEric W. Biederman }; 281728a6d671SEric W. Biederman 2818c5ef1c42SArjan van de Ven static const struct inode_operations proc_tid_base_inode_operations = { 281928a6d671SEric W. Biederman .lookup = proc_tid_base_lookup, 282028a6d671SEric W. Biederman .getattr = pid_getattr, 282128a6d671SEric W. Biederman .setattr = proc_setattr, 282228a6d671SEric W. Biederman }; 282328a6d671SEric W. Biederman 2824444ceed8SEric W. Biederman static struct dentry *proc_task_instantiate(struct inode *dir, 2825c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 2826444ceed8SEric W. Biederman { 2827444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-ENOENT); 2828444ceed8SEric W. Biederman struct inode *inode; 282961a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 2830444ceed8SEric W. Biederman 2831444ceed8SEric W. Biederman if (!inode) 2832444ceed8SEric W. Biederman goto out; 2833444ceed8SEric W. Biederman inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO; 2834444ceed8SEric W. Biederman inode->i_op = &proc_tid_base_inode_operations; 2835444ceed8SEric W. Biederman inode->i_fop = &proc_tid_base_operations; 2836444ceed8SEric W. Biederman inode->i_flags|=S_IMMUTABLE; 2837*aed54175SVegard Nossum 2838*aed54175SVegard Nossum inode->i_nlink = 2 + pid_entry_count_dirs(tid_base_stuff, 2839*aed54175SVegard Nossum ARRAY_SIZE(tid_base_stuff)); 2840444ceed8SEric W. Biederman 2841444ceed8SEric W. Biederman dentry->d_op = &pid_dentry_operations; 2842444ceed8SEric W. Biederman 2843444ceed8SEric W. Biederman d_add(dentry, inode); 2844444ceed8SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 2845444ceed8SEric W. Biederman if (pid_revalidate(dentry, NULL)) 2846444ceed8SEric W. Biederman error = NULL; 2847444ceed8SEric W. Biederman out: 2848444ceed8SEric W. Biederman return error; 2849444ceed8SEric W. Biederman } 2850444ceed8SEric W. Biederman 285128a6d671SEric W. Biederman static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd) 285228a6d671SEric W. Biederman { 285328a6d671SEric W. Biederman struct dentry *result = ERR_PTR(-ENOENT); 285428a6d671SEric W. Biederman struct task_struct *task; 285528a6d671SEric W. Biederman struct task_struct *leader = get_proc_task(dir); 285628a6d671SEric W. Biederman unsigned tid; 2857b488893aSPavel Emelyanov struct pid_namespace *ns; 285828a6d671SEric W. Biederman 285928a6d671SEric W. Biederman if (!leader) 286028a6d671SEric W. Biederman goto out_no_task; 286128a6d671SEric W. Biederman 286228a6d671SEric W. Biederman tid = name_to_int(dentry); 286328a6d671SEric W. Biederman if (tid == ~0U) 286428a6d671SEric W. Biederman goto out; 286528a6d671SEric W. Biederman 2866b488893aSPavel Emelyanov ns = dentry->d_sb->s_fs_info; 286728a6d671SEric W. Biederman rcu_read_lock(); 2868b488893aSPavel Emelyanov task = find_task_by_pid_ns(tid, ns); 286928a6d671SEric W. Biederman if (task) 287028a6d671SEric W. Biederman get_task_struct(task); 287128a6d671SEric W. Biederman rcu_read_unlock(); 287228a6d671SEric W. Biederman if (!task) 287328a6d671SEric W. Biederman goto out; 2874bac0abd6SPavel Emelyanov if (!same_thread_group(leader, task)) 287528a6d671SEric W. Biederman goto out_drop_task; 287628a6d671SEric W. Biederman 2877444ceed8SEric W. Biederman result = proc_task_instantiate(dir, dentry, task, NULL); 287828a6d671SEric W. Biederman out_drop_task: 287928a6d671SEric W. Biederman put_task_struct(task); 288028a6d671SEric W. Biederman out: 288128a6d671SEric W. Biederman put_task_struct(leader); 288228a6d671SEric W. Biederman out_no_task: 288328a6d671SEric W. Biederman return result; 288428a6d671SEric W. Biederman } 288528a6d671SEric W. Biederman 288628a6d671SEric W. Biederman /* 28870bc58a91SEric W. Biederman * Find the first tid of a thread group to return to user space. 28880bc58a91SEric W. Biederman * 28890bc58a91SEric W. Biederman * Usually this is just the thread group leader, but if the users 28900bc58a91SEric W. Biederman * buffer was too small or there was a seek into the middle of the 28910bc58a91SEric W. Biederman * directory we have more work todo. 28920bc58a91SEric W. Biederman * 28930bc58a91SEric W. Biederman * In the case of a short read we start with find_task_by_pid. 28940bc58a91SEric W. Biederman * 28950bc58a91SEric W. Biederman * In the case of a seek we start with the leader and walk nr 28960bc58a91SEric W. Biederman * threads past it. 28970bc58a91SEric W. Biederman */ 2898cc288738SEric W. Biederman static struct task_struct *first_tid(struct task_struct *leader, 2899b488893aSPavel Emelyanov int tid, int nr, struct pid_namespace *ns) 29000bc58a91SEric W. Biederman { 2901a872ff0cSOleg Nesterov struct task_struct *pos; 29020bc58a91SEric W. Biederman 2903cc288738SEric W. Biederman rcu_read_lock(); 29040bc58a91SEric W. Biederman /* Attempt to start with the pid of a thread */ 29050bc58a91SEric W. Biederman if (tid && (nr > 0)) { 2906b488893aSPavel Emelyanov pos = find_task_by_pid_ns(tid, ns); 2907a872ff0cSOleg Nesterov if (pos && (pos->group_leader == leader)) 2908a872ff0cSOleg Nesterov goto found; 29090bc58a91SEric W. Biederman } 29100bc58a91SEric W. Biederman 29110bc58a91SEric W. Biederman /* If nr exceeds the number of threads there is nothing todo */ 29120bc58a91SEric W. Biederman pos = NULL; 2913a872ff0cSOleg Nesterov if (nr && nr >= get_nr_threads(leader)) 2914a872ff0cSOleg Nesterov goto out; 2915a872ff0cSOleg Nesterov 2916a872ff0cSOleg Nesterov /* If we haven't found our starting place yet start 2917a872ff0cSOleg Nesterov * with the leader and walk nr threads forward. 2918a872ff0cSOleg Nesterov */ 2919a872ff0cSOleg Nesterov for (pos = leader; nr > 0; --nr) { 2920a872ff0cSOleg Nesterov pos = next_thread(pos); 2921a872ff0cSOleg Nesterov if (pos == leader) { 2922a872ff0cSOleg Nesterov pos = NULL; 2923a872ff0cSOleg Nesterov goto out; 2924a872ff0cSOleg Nesterov } 2925a872ff0cSOleg Nesterov } 2926a872ff0cSOleg Nesterov found: 2927a872ff0cSOleg Nesterov get_task_struct(pos); 2928a872ff0cSOleg Nesterov out: 2929cc288738SEric W. Biederman rcu_read_unlock(); 29300bc58a91SEric W. Biederman return pos; 29310bc58a91SEric W. Biederman } 29320bc58a91SEric W. Biederman 29330bc58a91SEric W. Biederman /* 29340bc58a91SEric W. Biederman * Find the next thread in the thread list. 29350bc58a91SEric W. Biederman * Return NULL if there is an error or no next thread. 29360bc58a91SEric W. Biederman * 29370bc58a91SEric W. Biederman * The reference to the input task_struct is released. 29380bc58a91SEric W. Biederman */ 29390bc58a91SEric W. Biederman static struct task_struct *next_tid(struct task_struct *start) 29400bc58a91SEric W. Biederman { 2941c1df7fb8SOleg Nesterov struct task_struct *pos = NULL; 2942cc288738SEric W. Biederman rcu_read_lock(); 2943c1df7fb8SOleg Nesterov if (pid_alive(start)) { 29440bc58a91SEric W. Biederman pos = next_thread(start); 2945c1df7fb8SOleg Nesterov if (thread_group_leader(pos)) 29460bc58a91SEric W. Biederman pos = NULL; 2947c1df7fb8SOleg Nesterov else 2948c1df7fb8SOleg Nesterov get_task_struct(pos); 2949c1df7fb8SOleg Nesterov } 2950cc288738SEric W. Biederman rcu_read_unlock(); 29510bc58a91SEric W. Biederman put_task_struct(start); 29520bc58a91SEric W. Biederman return pos; 29530bc58a91SEric W. Biederman } 29540bc58a91SEric W. Biederman 295561a28784SEric W. Biederman static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir, 295661a28784SEric W. Biederman struct task_struct *task, int tid) 295761a28784SEric W. Biederman { 295861a28784SEric W. Biederman char name[PROC_NUMBUF]; 295961a28784SEric W. Biederman int len = snprintf(name, sizeof(name), "%d", tid); 296061a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, name, len, 296161a28784SEric W. Biederman proc_task_instantiate, task, NULL); 296261a28784SEric W. Biederman } 296361a28784SEric W. Biederman 29641da177e4SLinus Torvalds /* for the /proc/TGID/task/ directories */ 29651da177e4SLinus Torvalds static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir) 29661da177e4SLinus Torvalds { 29672fddfeefSJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 29681da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 29697d895244SGuillaume Chazarain struct task_struct *leader = NULL; 29700bc58a91SEric W. Biederman struct task_struct *task; 29711da177e4SLinus Torvalds int retval = -ENOENT; 29721da177e4SLinus Torvalds ino_t ino; 29730bc58a91SEric W. Biederman int tid; 29741da177e4SLinus Torvalds unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */ 2975b488893aSPavel Emelyanov struct pid_namespace *ns; 29761da177e4SLinus Torvalds 29777d895244SGuillaume Chazarain task = get_proc_task(inode); 29787d895244SGuillaume Chazarain if (!task) 29797d895244SGuillaume Chazarain goto out_no_task; 29807d895244SGuillaume Chazarain rcu_read_lock(); 29817d895244SGuillaume Chazarain if (pid_alive(task)) { 29827d895244SGuillaume Chazarain leader = task->group_leader; 29837d895244SGuillaume Chazarain get_task_struct(leader); 29847d895244SGuillaume Chazarain } 29857d895244SGuillaume Chazarain rcu_read_unlock(); 29867d895244SGuillaume Chazarain put_task_struct(task); 298799f89551SEric W. Biederman if (!leader) 298899f89551SEric W. Biederman goto out_no_task; 29891da177e4SLinus Torvalds retval = 0; 29901da177e4SLinus Torvalds 29911da177e4SLinus Torvalds switch (pos) { 29921da177e4SLinus Torvalds case 0: 29931da177e4SLinus Torvalds ino = inode->i_ino; 29941da177e4SLinus Torvalds if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0) 29951da177e4SLinus Torvalds goto out; 29961da177e4SLinus Torvalds pos++; 29971da177e4SLinus Torvalds /* fall through */ 29981da177e4SLinus Torvalds case 1: 29991da177e4SLinus Torvalds ino = parent_ino(dentry); 30001da177e4SLinus Torvalds if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0) 30011da177e4SLinus Torvalds goto out; 30021da177e4SLinus Torvalds pos++; 30031da177e4SLinus Torvalds /* fall through */ 30041da177e4SLinus Torvalds } 30051da177e4SLinus Torvalds 30060bc58a91SEric W. Biederman /* f_version caches the tgid value that the last readdir call couldn't 30070bc58a91SEric W. Biederman * return. lseek aka telldir automagically resets f_version to 0. 30080bc58a91SEric W. Biederman */ 3009b488893aSPavel Emelyanov ns = filp->f_dentry->d_sb->s_fs_info; 30102b47c361SMathieu Desnoyers tid = (int)filp->f_version; 30110bc58a91SEric W. Biederman filp->f_version = 0; 3012b488893aSPavel Emelyanov for (task = first_tid(leader, tid, pos - 2, ns); 30130bc58a91SEric W. Biederman task; 30140bc58a91SEric W. Biederman task = next_tid(task), pos++) { 3015b488893aSPavel Emelyanov tid = task_pid_nr_ns(task, ns); 301661a28784SEric W. Biederman if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) { 30170bc58a91SEric W. Biederman /* returning this tgid failed, save it as the first 30180bc58a91SEric W. Biederman * pid for the next readir call */ 30192b47c361SMathieu Desnoyers filp->f_version = (u64)tid; 30200bc58a91SEric W. Biederman put_task_struct(task); 30211da177e4SLinus Torvalds break; 30220bc58a91SEric W. Biederman } 30231da177e4SLinus Torvalds } 30241da177e4SLinus Torvalds out: 30251da177e4SLinus Torvalds filp->f_pos = pos; 302699f89551SEric W. Biederman put_task_struct(leader); 302799f89551SEric W. Biederman out_no_task: 30281da177e4SLinus Torvalds return retval; 30291da177e4SLinus Torvalds } 30306e66b52bSEric W. Biederman 30316e66b52bSEric W. Biederman static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) 30326e66b52bSEric W. Biederman { 30336e66b52bSEric W. Biederman struct inode *inode = dentry->d_inode; 303499f89551SEric W. Biederman struct task_struct *p = get_proc_task(inode); 30356e66b52bSEric W. Biederman generic_fillattr(inode, stat); 30366e66b52bSEric W. Biederman 303799f89551SEric W. Biederman if (p) { 303899f89551SEric W. Biederman rcu_read_lock(); 303999f89551SEric W. Biederman stat->nlink += get_nr_threads(p); 304099f89551SEric W. Biederman rcu_read_unlock(); 304199f89551SEric W. Biederman put_task_struct(p); 30426e66b52bSEric W. Biederman } 30436e66b52bSEric W. Biederman 30446e66b52bSEric W. Biederman return 0; 30456e66b52bSEric W. Biederman } 304628a6d671SEric W. Biederman 3047c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations = { 304828a6d671SEric W. Biederman .lookup = proc_task_lookup, 304928a6d671SEric W. Biederman .getattr = proc_task_getattr, 305028a6d671SEric W. Biederman .setattr = proc_setattr, 305128a6d671SEric W. Biederman }; 305228a6d671SEric W. Biederman 305300977a59SArjan van de Ven static const struct file_operations proc_task_operations = { 305428a6d671SEric W. Biederman .read = generic_read_dir, 305528a6d671SEric W. Biederman .readdir = proc_task_readdir, 305628a6d671SEric W. Biederman }; 3057