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> 56*5995477aSAndrea Righi #include <linux/task_io_accounting_ops.h> 571da177e4SLinus Torvalds #include <linux/init.h> 5816f7e0feSRandy Dunlap #include <linux/capability.h> 591da177e4SLinus Torvalds #include <linux/file.h> 609f3acc31SAl Viro #include <linux/fdtable.h> 611da177e4SLinus Torvalds #include <linux/string.h> 621da177e4SLinus Torvalds #include <linux/seq_file.h> 631da177e4SLinus Torvalds #include <linux/namei.h> 646b3286edSKirill Korotaev #include <linux/mnt_namespace.h> 651da177e4SLinus Torvalds #include <linux/mm.h> 66b835996fSDipankar Sarma #include <linux/rcupdate.h> 671da177e4SLinus Torvalds #include <linux/kallsyms.h> 68d85f50d5SNeil Horman #include <linux/resource.h> 695096add8SKees Cook #include <linux/module.h> 701da177e4SLinus Torvalds #include <linux/mount.h> 711da177e4SLinus Torvalds #include <linux/security.h> 721da177e4SLinus Torvalds #include <linux/ptrace.h> 730d094efeSRoland McGrath #include <linux/tracehook.h> 74a424316cSPaul Menage #include <linux/cgroup.h> 751da177e4SLinus Torvalds #include <linux/cpuset.h> 761da177e4SLinus Torvalds #include <linux/audit.h> 775addc5ddSAl Viro #include <linux/poll.h> 781651e14eSSerge E. Hallyn #include <linux/nsproxy.h> 798ac773b4SAlexey Dobriyan #include <linux/oom.h> 803cb4a0bbSKawai, Hidehiro #include <linux/elf.h> 8160347f67SPavel Emelyanov #include <linux/pid_namespace.h> 821da177e4SLinus Torvalds #include "internal.h" 831da177e4SLinus Torvalds 840f2fe20fSEric W. Biederman /* NOTE: 850f2fe20fSEric W. Biederman * Implementing inode permission operations in /proc is almost 860f2fe20fSEric W. Biederman * certainly an error. Permission checks need to happen during 870f2fe20fSEric W. Biederman * each system call not at open time. The reason is that most of 880f2fe20fSEric W. Biederman * what we wish to check for permissions in /proc varies at runtime. 890f2fe20fSEric W. Biederman * 900f2fe20fSEric W. Biederman * The classic example of a problem is opening file descriptors 910f2fe20fSEric W. Biederman * in /proc for a task before it execs a suid executable. 920f2fe20fSEric W. Biederman */ 930f2fe20fSEric W. Biederman 941da177e4SLinus Torvalds struct pid_entry { 951da177e4SLinus Torvalds char *name; 96c5141e6dSEric Dumazet int len; 971da177e4SLinus Torvalds mode_t mode; 98c5ef1c42SArjan van de Ven const struct inode_operations *iop; 9900977a59SArjan van de Ven const struct file_operations *fop; 10020cdc894SEric W. Biederman union proc_op op; 1011da177e4SLinus Torvalds }; 1021da177e4SLinus Torvalds 10361a28784SEric W. Biederman #define NOD(NAME, MODE, IOP, FOP, OP) { \ 10420cdc894SEric W. Biederman .name = (NAME), \ 105c5141e6dSEric Dumazet .len = sizeof(NAME) - 1, \ 10620cdc894SEric W. Biederman .mode = MODE, \ 10720cdc894SEric W. Biederman .iop = IOP, \ 10820cdc894SEric W. Biederman .fop = FOP, \ 10920cdc894SEric W. Biederman .op = OP, \ 11020cdc894SEric W. Biederman } 11120cdc894SEric W. Biederman 11261a28784SEric W. Biederman #define DIR(NAME, MODE, OTYPE) \ 11361a28784SEric W. Biederman NOD(NAME, (S_IFDIR|(MODE)), \ 11420cdc894SEric W. Biederman &proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations, \ 11520cdc894SEric W. Biederman {} ) 11661a28784SEric W. Biederman #define LNK(NAME, OTYPE) \ 11761a28784SEric W. Biederman NOD(NAME, (S_IFLNK|S_IRWXUGO), \ 11820cdc894SEric W. Biederman &proc_pid_link_inode_operations, NULL, \ 11920cdc894SEric W. Biederman { .proc_get_link = &proc_##OTYPE##_link } ) 12061a28784SEric W. Biederman #define REG(NAME, MODE, OTYPE) \ 12161a28784SEric W. Biederman NOD(NAME, (S_IFREG|(MODE)), NULL, \ 12220cdc894SEric W. Biederman &proc_##OTYPE##_operations, {}) 12361a28784SEric W. Biederman #define INF(NAME, MODE, OTYPE) \ 12461a28784SEric W. Biederman NOD(NAME, (S_IFREG|(MODE)), \ 12520cdc894SEric W. Biederman NULL, &proc_info_file_operations, \ 12620cdc894SEric W. Biederman { .proc_read = &proc_##OTYPE } ) 127be614086SEric W. Biederman #define ONE(NAME, MODE, OTYPE) \ 128be614086SEric W. Biederman NOD(NAME, (S_IFREG|(MODE)), \ 129be614086SEric W. Biederman NULL, &proc_single_file_operations, \ 130be614086SEric W. Biederman { .proc_show = &proc_##OTYPE } ) 1311da177e4SLinus Torvalds 132aed54175SVegard Nossum /* 133aed54175SVegard Nossum * Count the number of hardlinks for the pid_entry table, excluding the . 134aed54175SVegard Nossum * and .. links. 135aed54175SVegard Nossum */ 136aed54175SVegard Nossum static unsigned int pid_entry_count_dirs(const struct pid_entry *entries, 137aed54175SVegard Nossum unsigned int n) 138aed54175SVegard Nossum { 139aed54175SVegard Nossum unsigned int i; 140aed54175SVegard Nossum unsigned int count; 141aed54175SVegard Nossum 142aed54175SVegard Nossum count = 0; 143aed54175SVegard Nossum for (i = 0; i < n; ++i) { 144aed54175SVegard Nossum if (S_ISDIR(entries[i].mode)) 145aed54175SVegard Nossum ++count; 146aed54175SVegard Nossum } 147aed54175SVegard Nossum 148aed54175SVegard Nossum return count; 149aed54175SVegard Nossum } 150aed54175SVegard Nossum 1515096add8SKees Cook int maps_protect; 1525096add8SKees Cook EXPORT_SYMBOL(maps_protect); 1535096add8SKees Cook 1540494f6ecSMiklos Szeredi static struct fs_struct *get_fs_struct(struct task_struct *task) 1551da177e4SLinus Torvalds { 1561da177e4SLinus Torvalds struct fs_struct *fs; 1570494f6ecSMiklos Szeredi task_lock(task); 1580494f6ecSMiklos Szeredi fs = task->fs; 1591da177e4SLinus Torvalds if(fs) 1601da177e4SLinus Torvalds atomic_inc(&fs->count); 1610494f6ecSMiklos Szeredi task_unlock(task); 1620494f6ecSMiklos Szeredi return fs; 1630494f6ecSMiklos Szeredi } 1640494f6ecSMiklos Szeredi 16599f89551SEric W. Biederman static int get_nr_threads(struct task_struct *tsk) 16699f89551SEric W. Biederman { 16799f89551SEric W. Biederman /* Must be called with the rcu_read_lock held */ 16899f89551SEric W. Biederman unsigned long flags; 16999f89551SEric W. Biederman int count = 0; 17099f89551SEric W. Biederman 17199f89551SEric W. Biederman if (lock_task_sighand(tsk, &flags)) { 17299f89551SEric W. Biederman count = atomic_read(&tsk->signal->count); 17399f89551SEric W. Biederman unlock_task_sighand(tsk, &flags); 17499f89551SEric W. Biederman } 17599f89551SEric W. Biederman return count; 17699f89551SEric W. Biederman } 17799f89551SEric W. Biederman 1783dcd25f3SJan Blunck static int proc_cwd_link(struct inode *inode, struct path *path) 1790494f6ecSMiklos Szeredi { 18099f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 18199f89551SEric W. Biederman struct fs_struct *fs = NULL; 1820494f6ecSMiklos Szeredi int result = -ENOENT; 18399f89551SEric W. Biederman 18499f89551SEric W. Biederman if (task) { 18599f89551SEric W. Biederman fs = get_fs_struct(task); 18699f89551SEric W. Biederman put_task_struct(task); 18799f89551SEric W. Biederman } 1881da177e4SLinus Torvalds if (fs) { 1891da177e4SLinus Torvalds read_lock(&fs->lock); 1903dcd25f3SJan Blunck *path = fs->pwd; 1913dcd25f3SJan Blunck path_get(&fs->pwd); 1921da177e4SLinus Torvalds read_unlock(&fs->lock); 1931da177e4SLinus Torvalds result = 0; 1941da177e4SLinus Torvalds put_fs_struct(fs); 1951da177e4SLinus Torvalds } 1961da177e4SLinus Torvalds return result; 1971da177e4SLinus Torvalds } 1981da177e4SLinus Torvalds 1993dcd25f3SJan Blunck static int proc_root_link(struct inode *inode, struct path *path) 2001da177e4SLinus Torvalds { 20199f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 20299f89551SEric W. Biederman struct fs_struct *fs = NULL; 2031da177e4SLinus Torvalds int result = -ENOENT; 20499f89551SEric W. Biederman 20599f89551SEric W. Biederman if (task) { 20699f89551SEric W. Biederman fs = get_fs_struct(task); 20799f89551SEric W. Biederman put_task_struct(task); 20899f89551SEric W. Biederman } 2091da177e4SLinus Torvalds if (fs) { 2101da177e4SLinus Torvalds read_lock(&fs->lock); 2113dcd25f3SJan Blunck *path = fs->root; 2123dcd25f3SJan Blunck path_get(&fs->root); 2131da177e4SLinus Torvalds read_unlock(&fs->lock); 2141da177e4SLinus Torvalds result = 0; 2151da177e4SLinus Torvalds put_fs_struct(fs); 2161da177e4SLinus Torvalds } 2171da177e4SLinus Torvalds return result; 2181da177e4SLinus Torvalds } 2191da177e4SLinus Torvalds 220638fa202SRoland McGrath /* 221638fa202SRoland McGrath * Return zero if current may access user memory in @task, -error if not. 222638fa202SRoland McGrath */ 223638fa202SRoland McGrath static int check_mem_permission(struct task_struct *task) 224638fa202SRoland McGrath { 225638fa202SRoland McGrath /* 226638fa202SRoland McGrath * A task can always look at itself, in case it chooses 227638fa202SRoland McGrath * to use system calls instead of load instructions. 228638fa202SRoland McGrath */ 229638fa202SRoland McGrath if (task == current) 230638fa202SRoland McGrath return 0; 231638fa202SRoland McGrath 232638fa202SRoland McGrath /* 233638fa202SRoland McGrath * If current is actively ptrace'ing, and would also be 234638fa202SRoland McGrath * permitted to freshly attach with ptrace now, permit it. 235638fa202SRoland McGrath */ 2360d094efeSRoland McGrath if (task_is_stopped_or_traced(task)) { 2370d094efeSRoland McGrath int match; 2380d094efeSRoland McGrath rcu_read_lock(); 2390d094efeSRoland McGrath match = (tracehook_tracer_task(task) == current); 2400d094efeSRoland McGrath rcu_read_unlock(); 2410d094efeSRoland McGrath if (match && ptrace_may_access(task, PTRACE_MODE_ATTACH)) 242638fa202SRoland McGrath return 0; 2430d094efeSRoland McGrath } 244638fa202SRoland McGrath 245638fa202SRoland McGrath /* 246638fa202SRoland McGrath * Noone else is allowed. 247638fa202SRoland McGrath */ 248638fa202SRoland McGrath return -EPERM; 249638fa202SRoland McGrath } 2501da177e4SLinus Torvalds 251831830b5SAl Viro struct mm_struct *mm_for_maps(struct task_struct *task) 252831830b5SAl Viro { 253831830b5SAl Viro struct mm_struct *mm = get_task_mm(task); 254831830b5SAl Viro if (!mm) 255831830b5SAl Viro return NULL; 256831830b5SAl Viro down_read(&mm->mmap_sem); 257831830b5SAl Viro task_lock(task); 258831830b5SAl Viro if (task->mm != mm) 259831830b5SAl Viro goto out; 260006ebb40SStephen Smalley if (task->mm != current->mm && 261006ebb40SStephen Smalley __ptrace_may_access(task, PTRACE_MODE_READ) < 0) 262831830b5SAl Viro goto out; 263831830b5SAl Viro task_unlock(task); 264831830b5SAl Viro return mm; 265831830b5SAl Viro out: 266831830b5SAl Viro task_unlock(task); 267831830b5SAl Viro up_read(&mm->mmap_sem); 268831830b5SAl Viro mmput(mm); 269831830b5SAl Viro return NULL; 270831830b5SAl Viro } 271831830b5SAl Viro 2721da177e4SLinus Torvalds static int proc_pid_cmdline(struct task_struct *task, char * buffer) 2731da177e4SLinus Torvalds { 2741da177e4SLinus Torvalds int res = 0; 2751da177e4SLinus Torvalds unsigned int len; 2761da177e4SLinus Torvalds struct mm_struct *mm = get_task_mm(task); 2771da177e4SLinus Torvalds if (!mm) 2781da177e4SLinus Torvalds goto out; 2791da177e4SLinus Torvalds if (!mm->arg_end) 2801da177e4SLinus Torvalds goto out_mm; /* Shh! No looking before we're done */ 2811da177e4SLinus Torvalds 2821da177e4SLinus Torvalds len = mm->arg_end - mm->arg_start; 2831da177e4SLinus Torvalds 2841da177e4SLinus Torvalds if (len > PAGE_SIZE) 2851da177e4SLinus Torvalds len = PAGE_SIZE; 2861da177e4SLinus Torvalds 2871da177e4SLinus Torvalds res = access_process_vm(task, mm->arg_start, buffer, len, 0); 2881da177e4SLinus Torvalds 2891da177e4SLinus Torvalds // If the nul at the end of args has been overwritten, then 2901da177e4SLinus Torvalds // assume application is using setproctitle(3). 2911da177e4SLinus Torvalds if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) { 2921da177e4SLinus Torvalds len = strnlen(buffer, res); 2931da177e4SLinus Torvalds if (len < res) { 2941da177e4SLinus Torvalds res = len; 2951da177e4SLinus Torvalds } else { 2961da177e4SLinus Torvalds len = mm->env_end - mm->env_start; 2971da177e4SLinus Torvalds if (len > PAGE_SIZE - res) 2981da177e4SLinus Torvalds len = PAGE_SIZE - res; 2991da177e4SLinus Torvalds res += access_process_vm(task, mm->env_start, buffer+res, len, 0); 3001da177e4SLinus Torvalds res = strnlen(buffer, res); 3011da177e4SLinus Torvalds } 3021da177e4SLinus Torvalds } 3031da177e4SLinus Torvalds out_mm: 3041da177e4SLinus Torvalds mmput(mm); 3051da177e4SLinus Torvalds out: 3061da177e4SLinus Torvalds return res; 3071da177e4SLinus Torvalds } 3081da177e4SLinus Torvalds 3091da177e4SLinus Torvalds static int proc_pid_auxv(struct task_struct *task, char *buffer) 3101da177e4SLinus Torvalds { 3111da177e4SLinus Torvalds int res = 0; 3121da177e4SLinus Torvalds struct mm_struct *mm = get_task_mm(task); 3131da177e4SLinus Torvalds if (mm) { 3141da177e4SLinus Torvalds unsigned int nwords = 0; 3151da177e4SLinus Torvalds do 3161da177e4SLinus Torvalds nwords += 2; 3171da177e4SLinus Torvalds while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */ 3181da177e4SLinus Torvalds res = nwords * sizeof(mm->saved_auxv[0]); 3191da177e4SLinus Torvalds if (res > PAGE_SIZE) 3201da177e4SLinus Torvalds res = PAGE_SIZE; 3211da177e4SLinus Torvalds memcpy(buffer, mm->saved_auxv, res); 3221da177e4SLinus Torvalds mmput(mm); 3231da177e4SLinus Torvalds } 3241da177e4SLinus Torvalds return res; 3251da177e4SLinus Torvalds } 3261da177e4SLinus Torvalds 3271da177e4SLinus Torvalds 3281da177e4SLinus Torvalds #ifdef CONFIG_KALLSYMS 3291da177e4SLinus Torvalds /* 3301da177e4SLinus Torvalds * Provides a wchan file via kallsyms in a proper one-value-per-file format. 3311da177e4SLinus Torvalds * Returns the resolved symbol. If that fails, simply return the address. 3321da177e4SLinus Torvalds */ 3331da177e4SLinus Torvalds static int proc_pid_wchan(struct task_struct *task, char *buffer) 3341da177e4SLinus Torvalds { 335ffb45122SAlexey Dobriyan unsigned long wchan; 3369281aceaSTejun Heo char symname[KSYM_NAME_LEN]; 3371da177e4SLinus Torvalds 3381da177e4SLinus Torvalds wchan = get_wchan(task); 3391da177e4SLinus Torvalds 3409d65cb4aSAlexey Dobriyan if (lookup_symbol_name(wchan, symname) < 0) 3411da177e4SLinus Torvalds return sprintf(buffer, "%lu", wchan); 3429d65cb4aSAlexey Dobriyan else 3439d65cb4aSAlexey Dobriyan return sprintf(buffer, "%s", symname); 3441da177e4SLinus Torvalds } 3451da177e4SLinus Torvalds #endif /* CONFIG_KALLSYMS */ 3461da177e4SLinus Torvalds 3471da177e4SLinus Torvalds #ifdef CONFIG_SCHEDSTATS 3481da177e4SLinus Torvalds /* 3491da177e4SLinus Torvalds * Provides /proc/PID/schedstat 3501da177e4SLinus Torvalds */ 3511da177e4SLinus Torvalds static int proc_pid_schedstat(struct task_struct *task, char *buffer) 3521da177e4SLinus Torvalds { 353172ba844SBalbir Singh return sprintf(buffer, "%llu %llu %lu\n", 3541da177e4SLinus Torvalds task->sched_info.cpu_time, 3551da177e4SLinus Torvalds task->sched_info.run_delay, 3562d72376bSIngo Molnar task->sched_info.pcount); 3571da177e4SLinus Torvalds } 3581da177e4SLinus Torvalds #endif 3591da177e4SLinus Torvalds 3609745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP 3619745512cSArjan van de Ven static int lstats_show_proc(struct seq_file *m, void *v) 3629745512cSArjan van de Ven { 3639745512cSArjan van de Ven int i; 36413d77c37SHiroshi Shimamoto struct inode *inode = m->private; 36513d77c37SHiroshi Shimamoto struct task_struct *task = get_proc_task(inode); 3669745512cSArjan van de Ven 36713d77c37SHiroshi Shimamoto if (!task) 36813d77c37SHiroshi Shimamoto return -ESRCH; 36913d77c37SHiroshi Shimamoto seq_puts(m, "Latency Top version : v0.1\n"); 3709745512cSArjan van de Ven for (i = 0; i < 32; i++) { 3719745512cSArjan van de Ven if (task->latency_record[i].backtrace[0]) { 3729745512cSArjan van de Ven int q; 3739745512cSArjan van de Ven seq_printf(m, "%i %li %li ", 3749745512cSArjan van de Ven task->latency_record[i].count, 3759745512cSArjan van de Ven task->latency_record[i].time, 3769745512cSArjan van de Ven task->latency_record[i].max); 3779745512cSArjan van de Ven for (q = 0; q < LT_BACKTRACEDEPTH; q++) { 3789745512cSArjan van de Ven char sym[KSYM_NAME_LEN]; 3799745512cSArjan van de Ven char *c; 3809745512cSArjan van de Ven if (!task->latency_record[i].backtrace[q]) 3819745512cSArjan van de Ven break; 3829745512cSArjan van de Ven if (task->latency_record[i].backtrace[q] == ULONG_MAX) 3839745512cSArjan van de Ven break; 3849745512cSArjan van de Ven sprint_symbol(sym, task->latency_record[i].backtrace[q]); 3859745512cSArjan van de Ven c = strchr(sym, '+'); 3869745512cSArjan van de Ven if (c) 3879745512cSArjan van de Ven *c = 0; 3889745512cSArjan van de Ven seq_printf(m, "%s ", sym); 3899745512cSArjan van de Ven } 3909745512cSArjan van de Ven seq_printf(m, "\n"); 3919745512cSArjan van de Ven } 3929745512cSArjan van de Ven 3939745512cSArjan van de Ven } 39413d77c37SHiroshi Shimamoto put_task_struct(task); 3959745512cSArjan van de Ven return 0; 3969745512cSArjan van de Ven } 3979745512cSArjan van de Ven 3989745512cSArjan van de Ven static int lstats_open(struct inode *inode, struct file *file) 3999745512cSArjan van de Ven { 40013d77c37SHiroshi Shimamoto return single_open(file, lstats_show_proc, inode); 401d6643d12SHiroshi Shimamoto } 402d6643d12SHiroshi Shimamoto 4039745512cSArjan van de Ven static ssize_t lstats_write(struct file *file, const char __user *buf, 4049745512cSArjan van de Ven size_t count, loff_t *offs) 4059745512cSArjan van de Ven { 40613d77c37SHiroshi Shimamoto struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 4079745512cSArjan van de Ven 40813d77c37SHiroshi Shimamoto if (!task) 40913d77c37SHiroshi Shimamoto return -ESRCH; 4109745512cSArjan van de Ven clear_all_latency_tracing(task); 41113d77c37SHiroshi Shimamoto put_task_struct(task); 4129745512cSArjan van de Ven 4139745512cSArjan van de Ven return count; 4149745512cSArjan van de Ven } 4159745512cSArjan van de Ven 4169745512cSArjan van de Ven static const struct file_operations proc_lstats_operations = { 4179745512cSArjan van de Ven .open = lstats_open, 4189745512cSArjan van de Ven .read = seq_read, 4199745512cSArjan van de Ven .write = lstats_write, 4209745512cSArjan van de Ven .llseek = seq_lseek, 42113d77c37SHiroshi Shimamoto .release = single_release, 4229745512cSArjan van de Ven }; 4239745512cSArjan van de Ven 4249745512cSArjan van de Ven #endif 4259745512cSArjan van de Ven 4261da177e4SLinus Torvalds /* The badness from the OOM killer */ 4271da177e4SLinus Torvalds unsigned long badness(struct task_struct *p, unsigned long uptime); 4281da177e4SLinus Torvalds static int proc_oom_score(struct task_struct *task, char *buffer) 4291da177e4SLinus Torvalds { 4301da177e4SLinus Torvalds unsigned long points; 4311da177e4SLinus Torvalds struct timespec uptime; 4321da177e4SLinus Torvalds 4331da177e4SLinus Torvalds do_posix_clock_monotonic_gettime(&uptime); 43419c5d45aSAlexey Dobriyan read_lock(&tasklist_lock); 4351da177e4SLinus Torvalds points = badness(task, uptime.tv_sec); 43619c5d45aSAlexey Dobriyan read_unlock(&tasklist_lock); 4371da177e4SLinus Torvalds return sprintf(buffer, "%lu\n", points); 4381da177e4SLinus Torvalds } 4391da177e4SLinus Torvalds 440d85f50d5SNeil Horman struct limit_names { 441d85f50d5SNeil Horman char *name; 442d85f50d5SNeil Horman char *unit; 443d85f50d5SNeil Horman }; 444d85f50d5SNeil Horman 445d85f50d5SNeil Horman static const struct limit_names lnames[RLIM_NLIMITS] = { 446d85f50d5SNeil Horman [RLIMIT_CPU] = {"Max cpu time", "ms"}, 447d85f50d5SNeil Horman [RLIMIT_FSIZE] = {"Max file size", "bytes"}, 448d85f50d5SNeil Horman [RLIMIT_DATA] = {"Max data size", "bytes"}, 449d85f50d5SNeil Horman [RLIMIT_STACK] = {"Max stack size", "bytes"}, 450d85f50d5SNeil Horman [RLIMIT_CORE] = {"Max core file size", "bytes"}, 451d85f50d5SNeil Horman [RLIMIT_RSS] = {"Max resident set", "bytes"}, 452d85f50d5SNeil Horman [RLIMIT_NPROC] = {"Max processes", "processes"}, 453d85f50d5SNeil Horman [RLIMIT_NOFILE] = {"Max open files", "files"}, 454d85f50d5SNeil Horman [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"}, 455d85f50d5SNeil Horman [RLIMIT_AS] = {"Max address space", "bytes"}, 456d85f50d5SNeil Horman [RLIMIT_LOCKS] = {"Max file locks", "locks"}, 457d85f50d5SNeil Horman [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"}, 458d85f50d5SNeil Horman [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"}, 459d85f50d5SNeil Horman [RLIMIT_NICE] = {"Max nice priority", NULL}, 460d85f50d5SNeil Horman [RLIMIT_RTPRIO] = {"Max realtime priority", NULL}, 4618808117cSEugene Teo [RLIMIT_RTTIME] = {"Max realtime timeout", "us"}, 462d85f50d5SNeil Horman }; 463d85f50d5SNeil Horman 464d85f50d5SNeil Horman /* Display limits for a process */ 465d85f50d5SNeil Horman static int proc_pid_limits(struct task_struct *task, char *buffer) 466d85f50d5SNeil Horman { 467d85f50d5SNeil Horman unsigned int i; 468d85f50d5SNeil Horman int count = 0; 469d85f50d5SNeil Horman unsigned long flags; 470d85f50d5SNeil Horman char *bufptr = buffer; 471d85f50d5SNeil Horman 472d85f50d5SNeil Horman struct rlimit rlim[RLIM_NLIMITS]; 473d85f50d5SNeil Horman 474d85f50d5SNeil Horman rcu_read_lock(); 475d85f50d5SNeil Horman if (!lock_task_sighand(task,&flags)) { 476d85f50d5SNeil Horman rcu_read_unlock(); 477d85f50d5SNeil Horman return 0; 478d85f50d5SNeil Horman } 479d85f50d5SNeil Horman memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS); 480d85f50d5SNeil Horman unlock_task_sighand(task, &flags); 481d85f50d5SNeil Horman rcu_read_unlock(); 482d85f50d5SNeil Horman 483d85f50d5SNeil Horman /* 484d85f50d5SNeil Horman * print the file header 485d85f50d5SNeil Horman */ 486d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n", 487d85f50d5SNeil Horman "Limit", "Soft Limit", "Hard Limit", "Units"); 488d85f50d5SNeil Horman 489d85f50d5SNeil Horman for (i = 0; i < RLIM_NLIMITS; i++) { 490d85f50d5SNeil Horman if (rlim[i].rlim_cur == RLIM_INFINITY) 491d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-25s %-20s ", 492d85f50d5SNeil Horman lnames[i].name, "unlimited"); 493d85f50d5SNeil Horman else 494d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-25s %-20lu ", 495d85f50d5SNeil Horman lnames[i].name, rlim[i].rlim_cur); 496d85f50d5SNeil Horman 497d85f50d5SNeil Horman if (rlim[i].rlim_max == RLIM_INFINITY) 498d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-20s ", "unlimited"); 499d85f50d5SNeil Horman else 500d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-20lu ", 501d85f50d5SNeil Horman rlim[i].rlim_max); 502d85f50d5SNeil Horman 503d85f50d5SNeil Horman if (lnames[i].unit) 504d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-10s\n", 505d85f50d5SNeil Horman lnames[i].unit); 506d85f50d5SNeil Horman else 507d85f50d5SNeil Horman count += sprintf(&bufptr[count], "\n"); 508d85f50d5SNeil Horman } 509d85f50d5SNeil Horman 510d85f50d5SNeil Horman return count; 511d85f50d5SNeil Horman } 512d85f50d5SNeil Horman 513ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK 514ebcb6734SRoland McGrath static int proc_pid_syscall(struct task_struct *task, char *buffer) 515ebcb6734SRoland McGrath { 516ebcb6734SRoland McGrath long nr; 517ebcb6734SRoland McGrath unsigned long args[6], sp, pc; 518ebcb6734SRoland McGrath 519ebcb6734SRoland McGrath if (task_current_syscall(task, &nr, args, 6, &sp, &pc)) 520ebcb6734SRoland McGrath return sprintf(buffer, "running\n"); 521ebcb6734SRoland McGrath 522ebcb6734SRoland McGrath if (nr < 0) 523ebcb6734SRoland McGrath return sprintf(buffer, "%ld 0x%lx 0x%lx\n", nr, sp, pc); 524ebcb6734SRoland McGrath 525ebcb6734SRoland McGrath return sprintf(buffer, 526ebcb6734SRoland McGrath "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n", 527ebcb6734SRoland McGrath nr, 528ebcb6734SRoland McGrath args[0], args[1], args[2], args[3], args[4], args[5], 529ebcb6734SRoland McGrath sp, pc); 530ebcb6734SRoland McGrath } 531ebcb6734SRoland McGrath #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */ 532ebcb6734SRoland McGrath 5331da177e4SLinus Torvalds /************************************************************************/ 5341da177e4SLinus Torvalds /* Here the fs part begins */ 5351da177e4SLinus Torvalds /************************************************************************/ 5361da177e4SLinus Torvalds 5371da177e4SLinus Torvalds /* permission checks */ 538778c1144SEric W. Biederman static int proc_fd_access_allowed(struct inode *inode) 5391da177e4SLinus Torvalds { 540778c1144SEric W. Biederman struct task_struct *task; 541778c1144SEric W. Biederman int allowed = 0; 542df26c40eSEric W. Biederman /* Allow access to a task's file descriptors if it is us or we 543df26c40eSEric W. Biederman * may use ptrace attach to the process and find out that 544df26c40eSEric W. Biederman * information. 545778c1144SEric W. Biederman */ 546778c1144SEric W. Biederman task = get_proc_task(inode); 547df26c40eSEric W. Biederman if (task) { 548006ebb40SStephen Smalley allowed = ptrace_may_access(task, PTRACE_MODE_READ); 549778c1144SEric W. Biederman put_task_struct(task); 550df26c40eSEric W. Biederman } 551778c1144SEric W. Biederman return allowed; 5521da177e4SLinus Torvalds } 5531da177e4SLinus Torvalds 5546d76fa58SLinus Torvalds static int proc_setattr(struct dentry *dentry, struct iattr *attr) 5556d76fa58SLinus Torvalds { 5566d76fa58SLinus Torvalds int error; 5576d76fa58SLinus Torvalds struct inode *inode = dentry->d_inode; 5586d76fa58SLinus Torvalds 5596d76fa58SLinus Torvalds if (attr->ia_valid & ATTR_MODE) 5606d76fa58SLinus Torvalds return -EPERM; 5616d76fa58SLinus Torvalds 5626d76fa58SLinus Torvalds error = inode_change_ok(inode, attr); 5636d76fa58SLinus Torvalds if (!error) 5646d76fa58SLinus Torvalds error = inode_setattr(inode, attr); 5656d76fa58SLinus Torvalds return error; 5666d76fa58SLinus Torvalds } 5676d76fa58SLinus Torvalds 568c5ef1c42SArjan van de Ven static const struct inode_operations proc_def_inode_operations = { 5696d76fa58SLinus Torvalds .setattr = proc_setattr, 5706d76fa58SLinus Torvalds }; 5716d76fa58SLinus Torvalds 572a1a2c409SMiklos Szeredi static int mounts_open_common(struct inode *inode, struct file *file, 573a1a2c409SMiklos Szeredi const struct seq_operations *op) 5741da177e4SLinus Torvalds { 57599f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 576cf7b708cSPavel Emelyanov struct nsproxy *nsp; 5776b3286edSKirill Korotaev struct mnt_namespace *ns = NULL; 578a1a2c409SMiklos Szeredi struct fs_struct *fs = NULL; 579a1a2c409SMiklos Szeredi struct path root; 5805addc5ddSAl Viro struct proc_mounts *p; 5815addc5ddSAl Viro int ret = -EINVAL; 5825addc5ddSAl Viro 58399f89551SEric W. Biederman if (task) { 584cf7b708cSPavel Emelyanov rcu_read_lock(); 585cf7b708cSPavel Emelyanov nsp = task_nsproxy(task); 586cf7b708cSPavel Emelyanov if (nsp) { 587cf7b708cSPavel Emelyanov ns = nsp->mnt_ns; 5886b3286edSKirill Korotaev if (ns) 5896b3286edSKirill Korotaev get_mnt_ns(ns); 590863c4702SAlexey Dobriyan } 591cf7b708cSPavel Emelyanov rcu_read_unlock(); 592a1a2c409SMiklos Szeredi if (ns) 593a1a2c409SMiklos Szeredi fs = get_fs_struct(task); 59499f89551SEric W. Biederman put_task_struct(task); 59599f89551SEric W. Biederman } 5961da177e4SLinus Torvalds 597a1a2c409SMiklos Szeredi if (!ns) 598a1a2c409SMiklos Szeredi goto err; 599a1a2c409SMiklos Szeredi if (!fs) 600a1a2c409SMiklos Szeredi goto err_put_ns; 601a1a2c409SMiklos Szeredi 602a1a2c409SMiklos Szeredi read_lock(&fs->lock); 603a1a2c409SMiklos Szeredi root = fs->root; 604a1a2c409SMiklos Szeredi path_get(&root); 605a1a2c409SMiklos Szeredi read_unlock(&fs->lock); 606a1a2c409SMiklos Szeredi put_fs_struct(fs); 607a1a2c409SMiklos Szeredi 6085addc5ddSAl Viro ret = -ENOMEM; 6095addc5ddSAl Viro p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL); 610a1a2c409SMiklos Szeredi if (!p) 611a1a2c409SMiklos Szeredi goto err_put_path; 612a1a2c409SMiklos Szeredi 6135addc5ddSAl Viro file->private_data = &p->m; 614a1a2c409SMiklos Szeredi ret = seq_open(file, op); 615a1a2c409SMiklos Szeredi if (ret) 616a1a2c409SMiklos Szeredi goto err_free; 617a1a2c409SMiklos Szeredi 618a1a2c409SMiklos Szeredi p->m.private = p; 619a1a2c409SMiklos Szeredi p->ns = ns; 620a1a2c409SMiklos Szeredi p->root = root; 6216b3286edSKirill Korotaev p->event = ns->event; 622a1a2c409SMiklos Szeredi 6235addc5ddSAl Viro return 0; 624a1a2c409SMiklos Szeredi 625a1a2c409SMiklos Szeredi err_free: 6265addc5ddSAl Viro kfree(p); 627a1a2c409SMiklos Szeredi err_put_path: 628a1a2c409SMiklos Szeredi path_put(&root); 629a1a2c409SMiklos Szeredi err_put_ns: 6306b3286edSKirill Korotaev put_mnt_ns(ns); 631a1a2c409SMiklos Szeredi err: 6321da177e4SLinus Torvalds return ret; 6331da177e4SLinus Torvalds } 6341da177e4SLinus Torvalds 6351da177e4SLinus Torvalds static int mounts_release(struct inode *inode, struct file *file) 6361da177e4SLinus Torvalds { 637a1a2c409SMiklos Szeredi struct proc_mounts *p = file->private_data; 638a1a2c409SMiklos Szeredi path_put(&p->root); 639a1a2c409SMiklos Szeredi put_mnt_ns(p->ns); 6401da177e4SLinus Torvalds return seq_release(inode, file); 6411da177e4SLinus Torvalds } 6421da177e4SLinus Torvalds 6435addc5ddSAl Viro static unsigned mounts_poll(struct file *file, poll_table *wait) 6445addc5ddSAl Viro { 6455addc5ddSAl Viro struct proc_mounts *p = file->private_data; 646a1a2c409SMiklos Szeredi struct mnt_namespace *ns = p->ns; 6475addc5ddSAl Viro unsigned res = 0; 6485addc5ddSAl Viro 6495addc5ddSAl Viro poll_wait(file, &ns->poll, wait); 6505addc5ddSAl Viro 6515addc5ddSAl Viro spin_lock(&vfsmount_lock); 6525addc5ddSAl Viro if (p->event != ns->event) { 6535addc5ddSAl Viro p->event = ns->event; 6545addc5ddSAl Viro res = POLLERR; 6555addc5ddSAl Viro } 6565addc5ddSAl Viro spin_unlock(&vfsmount_lock); 6575addc5ddSAl Viro 6585addc5ddSAl Viro return res; 6595addc5ddSAl Viro } 6605addc5ddSAl Viro 661a1a2c409SMiklos Szeredi static int mounts_open(struct inode *inode, struct file *file) 662a1a2c409SMiklos Szeredi { 663a1a2c409SMiklos Szeredi return mounts_open_common(inode, file, &mounts_op); 664a1a2c409SMiklos Szeredi } 665a1a2c409SMiklos Szeredi 66600977a59SArjan van de Ven static const struct file_operations proc_mounts_operations = { 6671da177e4SLinus Torvalds .open = mounts_open, 6681da177e4SLinus Torvalds .read = seq_read, 6691da177e4SLinus Torvalds .llseek = seq_lseek, 6701da177e4SLinus Torvalds .release = mounts_release, 6715addc5ddSAl Viro .poll = mounts_poll, 6721da177e4SLinus Torvalds }; 6731da177e4SLinus Torvalds 6742d4d4864SRam Pai static int mountinfo_open(struct inode *inode, struct file *file) 6752d4d4864SRam Pai { 6762d4d4864SRam Pai return mounts_open_common(inode, file, &mountinfo_op); 6772d4d4864SRam Pai } 6782d4d4864SRam Pai 6792d4d4864SRam Pai static const struct file_operations proc_mountinfo_operations = { 6802d4d4864SRam Pai .open = mountinfo_open, 6812d4d4864SRam Pai .read = seq_read, 6822d4d4864SRam Pai .llseek = seq_lseek, 6832d4d4864SRam Pai .release = mounts_release, 6842d4d4864SRam Pai .poll = mounts_poll, 6852d4d4864SRam Pai }; 6862d4d4864SRam Pai 687b4629fe2SChuck Lever static int mountstats_open(struct inode *inode, struct file *file) 688b4629fe2SChuck Lever { 689a1a2c409SMiklos Szeredi return mounts_open_common(inode, file, &mountstats_op); 690b4629fe2SChuck Lever } 691b4629fe2SChuck Lever 69200977a59SArjan van de Ven static const struct file_operations proc_mountstats_operations = { 693b4629fe2SChuck Lever .open = mountstats_open, 694b4629fe2SChuck Lever .read = seq_read, 695b4629fe2SChuck Lever .llseek = seq_lseek, 696b4629fe2SChuck Lever .release = mounts_release, 697b4629fe2SChuck Lever }; 698b4629fe2SChuck Lever 6991da177e4SLinus Torvalds #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */ 7001da177e4SLinus Torvalds 7011da177e4SLinus Torvalds static ssize_t proc_info_read(struct file * file, char __user * buf, 7021da177e4SLinus Torvalds size_t count, loff_t *ppos) 7031da177e4SLinus Torvalds { 7042fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 7051da177e4SLinus Torvalds unsigned long page; 7061da177e4SLinus Torvalds ssize_t length; 70799f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 70899f89551SEric W. Biederman 70999f89551SEric W. Biederman length = -ESRCH; 71099f89551SEric W. Biederman if (!task) 71199f89551SEric W. Biederman goto out_no_task; 7121da177e4SLinus Torvalds 7131da177e4SLinus Torvalds if (count > PROC_BLOCK_SIZE) 7141da177e4SLinus Torvalds count = PROC_BLOCK_SIZE; 71599f89551SEric W. Biederman 71699f89551SEric W. Biederman length = -ENOMEM; 717e12ba74dSMel Gorman if (!(page = __get_free_page(GFP_TEMPORARY))) 71899f89551SEric W. Biederman goto out; 7191da177e4SLinus Torvalds 7201da177e4SLinus Torvalds length = PROC_I(inode)->op.proc_read(task, (char*)page); 7211da177e4SLinus Torvalds 7221da177e4SLinus Torvalds if (length >= 0) 7231da177e4SLinus Torvalds length = simple_read_from_buffer(buf, count, ppos, (char *)page, length); 7241da177e4SLinus Torvalds free_page(page); 72599f89551SEric W. Biederman out: 72699f89551SEric W. Biederman put_task_struct(task); 72799f89551SEric W. Biederman out_no_task: 7281da177e4SLinus Torvalds return length; 7291da177e4SLinus Torvalds } 7301da177e4SLinus Torvalds 73100977a59SArjan van de Ven static const struct file_operations proc_info_file_operations = { 7321da177e4SLinus Torvalds .read = proc_info_read, 7331da177e4SLinus Torvalds }; 7341da177e4SLinus Torvalds 735be614086SEric W. Biederman static int proc_single_show(struct seq_file *m, void *v) 736be614086SEric W. Biederman { 737be614086SEric W. Biederman struct inode *inode = m->private; 738be614086SEric W. Biederman struct pid_namespace *ns; 739be614086SEric W. Biederman struct pid *pid; 740be614086SEric W. Biederman struct task_struct *task; 741be614086SEric W. Biederman int ret; 742be614086SEric W. Biederman 743be614086SEric W. Biederman ns = inode->i_sb->s_fs_info; 744be614086SEric W. Biederman pid = proc_pid(inode); 745be614086SEric W. Biederman task = get_pid_task(pid, PIDTYPE_PID); 746be614086SEric W. Biederman if (!task) 747be614086SEric W. Biederman return -ESRCH; 748be614086SEric W. Biederman 749be614086SEric W. Biederman ret = PROC_I(inode)->op.proc_show(m, ns, pid, task); 750be614086SEric W. Biederman 751be614086SEric W. Biederman put_task_struct(task); 752be614086SEric W. Biederman return ret; 753be614086SEric W. Biederman } 754be614086SEric W. Biederman 755be614086SEric W. Biederman static int proc_single_open(struct inode *inode, struct file *filp) 756be614086SEric W. Biederman { 757be614086SEric W. Biederman int ret; 758be614086SEric W. Biederman ret = single_open(filp, proc_single_show, NULL); 759be614086SEric W. Biederman if (!ret) { 760be614086SEric W. Biederman struct seq_file *m = filp->private_data; 761be614086SEric W. Biederman 762be614086SEric W. Biederman m->private = inode; 763be614086SEric W. Biederman } 764be614086SEric W. Biederman return ret; 765be614086SEric W. Biederman } 766be614086SEric W. Biederman 767be614086SEric W. Biederman static const struct file_operations proc_single_file_operations = { 768be614086SEric W. Biederman .open = proc_single_open, 769be614086SEric W. Biederman .read = seq_read, 770be614086SEric W. Biederman .llseek = seq_lseek, 771be614086SEric W. Biederman .release = single_release, 772be614086SEric W. Biederman }; 773be614086SEric W. Biederman 7741da177e4SLinus Torvalds static int mem_open(struct inode* inode, struct file* file) 7751da177e4SLinus Torvalds { 7761da177e4SLinus Torvalds file->private_data = (void*)((long)current->self_exec_id); 7771da177e4SLinus Torvalds return 0; 7781da177e4SLinus Torvalds } 7791da177e4SLinus Torvalds 7801da177e4SLinus Torvalds static ssize_t mem_read(struct file * file, char __user * buf, 7811da177e4SLinus Torvalds size_t count, loff_t *ppos) 7821da177e4SLinus Torvalds { 7832fddfeefSJosef "Jeff" Sipek struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode); 7841da177e4SLinus Torvalds char *page; 7851da177e4SLinus Torvalds unsigned long src = *ppos; 7861da177e4SLinus Torvalds int ret = -ESRCH; 7871da177e4SLinus Torvalds struct mm_struct *mm; 7881da177e4SLinus Torvalds 78999f89551SEric W. Biederman if (!task) 79099f89551SEric W. Biederman goto out_no_task; 79199f89551SEric W. Biederman 792638fa202SRoland McGrath if (check_mem_permission(task)) 7931da177e4SLinus Torvalds goto out; 7941da177e4SLinus Torvalds 7951da177e4SLinus Torvalds ret = -ENOMEM; 796e12ba74dSMel Gorman page = (char *)__get_free_page(GFP_TEMPORARY); 7971da177e4SLinus Torvalds if (!page) 7981da177e4SLinus Torvalds goto out; 7991da177e4SLinus Torvalds 8001da177e4SLinus Torvalds ret = 0; 8011da177e4SLinus Torvalds 8021da177e4SLinus Torvalds mm = get_task_mm(task); 8031da177e4SLinus Torvalds if (!mm) 8041da177e4SLinus Torvalds goto out_free; 8051da177e4SLinus Torvalds 8061da177e4SLinus Torvalds ret = -EIO; 8071da177e4SLinus Torvalds 8081da177e4SLinus Torvalds if (file->private_data != (void*)((long)current->self_exec_id)) 8091da177e4SLinus Torvalds goto out_put; 8101da177e4SLinus Torvalds 8111da177e4SLinus Torvalds ret = 0; 8121da177e4SLinus Torvalds 8131da177e4SLinus Torvalds while (count > 0) { 8141da177e4SLinus Torvalds int this_len, retval; 8151da177e4SLinus Torvalds 8161da177e4SLinus Torvalds this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count; 8171da177e4SLinus Torvalds retval = access_process_vm(task, src, page, this_len, 0); 818638fa202SRoland McGrath if (!retval || check_mem_permission(task)) { 8191da177e4SLinus Torvalds if (!ret) 8201da177e4SLinus Torvalds ret = -EIO; 8211da177e4SLinus Torvalds break; 8221da177e4SLinus Torvalds } 8231da177e4SLinus Torvalds 8241da177e4SLinus Torvalds if (copy_to_user(buf, page, retval)) { 8251da177e4SLinus Torvalds ret = -EFAULT; 8261da177e4SLinus Torvalds break; 8271da177e4SLinus Torvalds } 8281da177e4SLinus Torvalds 8291da177e4SLinus Torvalds ret += retval; 8301da177e4SLinus Torvalds src += retval; 8311da177e4SLinus Torvalds buf += retval; 8321da177e4SLinus Torvalds count -= retval; 8331da177e4SLinus Torvalds } 8341da177e4SLinus Torvalds *ppos = src; 8351da177e4SLinus Torvalds 8361da177e4SLinus Torvalds out_put: 8371da177e4SLinus Torvalds mmput(mm); 8381da177e4SLinus Torvalds out_free: 8391da177e4SLinus Torvalds free_page((unsigned long) page); 8401da177e4SLinus Torvalds out: 84199f89551SEric W. Biederman put_task_struct(task); 84299f89551SEric W. Biederman out_no_task: 8431da177e4SLinus Torvalds return ret; 8441da177e4SLinus Torvalds } 8451da177e4SLinus Torvalds 8461da177e4SLinus Torvalds #define mem_write NULL 8471da177e4SLinus Torvalds 8481da177e4SLinus Torvalds #ifndef mem_write 8491da177e4SLinus Torvalds /* This is a security hazard */ 85063967fa9SGlauber de Oliveira Costa static ssize_t mem_write(struct file * file, const char __user *buf, 8511da177e4SLinus Torvalds size_t count, loff_t *ppos) 8521da177e4SLinus Torvalds { 853f7ca54f4SFrederik Deweerdt int copied; 8541da177e4SLinus Torvalds char *page; 8552fddfeefSJosef "Jeff" Sipek struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode); 8561da177e4SLinus Torvalds unsigned long dst = *ppos; 8571da177e4SLinus Torvalds 85899f89551SEric W. Biederman copied = -ESRCH; 85999f89551SEric W. Biederman if (!task) 86099f89551SEric W. Biederman goto out_no_task; 8611da177e4SLinus Torvalds 862638fa202SRoland McGrath if (check_mem_permission(task)) 86399f89551SEric W. Biederman goto out; 86499f89551SEric W. Biederman 86599f89551SEric W. Biederman copied = -ENOMEM; 866e12ba74dSMel Gorman page = (char *)__get_free_page(GFP_TEMPORARY); 8671da177e4SLinus Torvalds if (!page) 86899f89551SEric W. Biederman goto out; 8691da177e4SLinus Torvalds 870f7ca54f4SFrederik Deweerdt copied = 0; 8711da177e4SLinus Torvalds while (count > 0) { 8721da177e4SLinus Torvalds int this_len, retval; 8731da177e4SLinus Torvalds 8741da177e4SLinus Torvalds this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count; 8751da177e4SLinus Torvalds if (copy_from_user(page, buf, this_len)) { 8761da177e4SLinus Torvalds copied = -EFAULT; 8771da177e4SLinus Torvalds break; 8781da177e4SLinus Torvalds } 8791da177e4SLinus Torvalds retval = access_process_vm(task, dst, page, this_len, 1); 8801da177e4SLinus Torvalds if (!retval) { 8811da177e4SLinus Torvalds if (!copied) 8821da177e4SLinus Torvalds copied = -EIO; 8831da177e4SLinus Torvalds break; 8841da177e4SLinus Torvalds } 8851da177e4SLinus Torvalds copied += retval; 8861da177e4SLinus Torvalds buf += retval; 8871da177e4SLinus Torvalds dst += retval; 8881da177e4SLinus Torvalds count -= retval; 8891da177e4SLinus Torvalds } 8901da177e4SLinus Torvalds *ppos = dst; 8911da177e4SLinus Torvalds free_page((unsigned long) page); 89299f89551SEric W. Biederman out: 89399f89551SEric W. Biederman put_task_struct(task); 89499f89551SEric W. Biederman out_no_task: 8951da177e4SLinus Torvalds return copied; 8961da177e4SLinus Torvalds } 8971da177e4SLinus Torvalds #endif 8981da177e4SLinus Torvalds 89985863e47SMatt Mackall loff_t mem_lseek(struct file *file, loff_t offset, int orig) 9001da177e4SLinus Torvalds { 9011da177e4SLinus Torvalds switch (orig) { 9021da177e4SLinus Torvalds case 0: 9031da177e4SLinus Torvalds file->f_pos = offset; 9041da177e4SLinus Torvalds break; 9051da177e4SLinus Torvalds case 1: 9061da177e4SLinus Torvalds file->f_pos += offset; 9071da177e4SLinus Torvalds break; 9081da177e4SLinus Torvalds default: 9091da177e4SLinus Torvalds return -EINVAL; 9101da177e4SLinus Torvalds } 9111da177e4SLinus Torvalds force_successful_syscall_return(); 9121da177e4SLinus Torvalds return file->f_pos; 9131da177e4SLinus Torvalds } 9141da177e4SLinus Torvalds 91500977a59SArjan van de Ven static const struct file_operations proc_mem_operations = { 9161da177e4SLinus Torvalds .llseek = mem_lseek, 9171da177e4SLinus Torvalds .read = mem_read, 9181da177e4SLinus Torvalds .write = mem_write, 9191da177e4SLinus Torvalds .open = mem_open, 9201da177e4SLinus Torvalds }; 9211da177e4SLinus Torvalds 922315e28c8SJames Pearson static ssize_t environ_read(struct file *file, char __user *buf, 923315e28c8SJames Pearson size_t count, loff_t *ppos) 924315e28c8SJames Pearson { 925315e28c8SJames Pearson struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 926315e28c8SJames Pearson char *page; 927315e28c8SJames Pearson unsigned long src = *ppos; 928315e28c8SJames Pearson int ret = -ESRCH; 929315e28c8SJames Pearson struct mm_struct *mm; 930315e28c8SJames Pearson 931315e28c8SJames Pearson if (!task) 932315e28c8SJames Pearson goto out_no_task; 933315e28c8SJames Pearson 934006ebb40SStephen Smalley if (!ptrace_may_access(task, PTRACE_MODE_READ)) 935315e28c8SJames Pearson goto out; 936315e28c8SJames Pearson 937315e28c8SJames Pearson ret = -ENOMEM; 938315e28c8SJames Pearson page = (char *)__get_free_page(GFP_TEMPORARY); 939315e28c8SJames Pearson if (!page) 940315e28c8SJames Pearson goto out; 941315e28c8SJames Pearson 942315e28c8SJames Pearson ret = 0; 943315e28c8SJames Pearson 944315e28c8SJames Pearson mm = get_task_mm(task); 945315e28c8SJames Pearson if (!mm) 946315e28c8SJames Pearson goto out_free; 947315e28c8SJames Pearson 948315e28c8SJames Pearson while (count > 0) { 949315e28c8SJames Pearson int this_len, retval, max_len; 950315e28c8SJames Pearson 951315e28c8SJames Pearson this_len = mm->env_end - (mm->env_start + src); 952315e28c8SJames Pearson 953315e28c8SJames Pearson if (this_len <= 0) 954315e28c8SJames Pearson break; 955315e28c8SJames Pearson 956315e28c8SJames Pearson max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count; 957315e28c8SJames Pearson this_len = (this_len > max_len) ? max_len : this_len; 958315e28c8SJames Pearson 959315e28c8SJames Pearson retval = access_process_vm(task, (mm->env_start + src), 960315e28c8SJames Pearson page, this_len, 0); 961315e28c8SJames Pearson 962315e28c8SJames Pearson if (retval <= 0) { 963315e28c8SJames Pearson ret = retval; 964315e28c8SJames Pearson break; 965315e28c8SJames Pearson } 966315e28c8SJames Pearson 967315e28c8SJames Pearson if (copy_to_user(buf, page, retval)) { 968315e28c8SJames Pearson ret = -EFAULT; 969315e28c8SJames Pearson break; 970315e28c8SJames Pearson } 971315e28c8SJames Pearson 972315e28c8SJames Pearson ret += retval; 973315e28c8SJames Pearson src += retval; 974315e28c8SJames Pearson buf += retval; 975315e28c8SJames Pearson count -= retval; 976315e28c8SJames Pearson } 977315e28c8SJames Pearson *ppos = src; 978315e28c8SJames Pearson 979315e28c8SJames Pearson mmput(mm); 980315e28c8SJames Pearson out_free: 981315e28c8SJames Pearson free_page((unsigned long) page); 982315e28c8SJames Pearson out: 983315e28c8SJames Pearson put_task_struct(task); 984315e28c8SJames Pearson out_no_task: 985315e28c8SJames Pearson return ret; 986315e28c8SJames Pearson } 987315e28c8SJames Pearson 988315e28c8SJames Pearson static const struct file_operations proc_environ_operations = { 989315e28c8SJames Pearson .read = environ_read, 990315e28c8SJames Pearson }; 991315e28c8SJames Pearson 9921da177e4SLinus Torvalds static ssize_t oom_adjust_read(struct file *file, char __user *buf, 9931da177e4SLinus Torvalds size_t count, loff_t *ppos) 9941da177e4SLinus Torvalds { 9952fddfeefSJosef "Jeff" Sipek struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode); 9968578cea7SEric W. Biederman char buffer[PROC_NUMBUF]; 9971da177e4SLinus Torvalds size_t len; 99899f89551SEric W. Biederman int oom_adjust; 9991da177e4SLinus Torvalds 100099f89551SEric W. Biederman if (!task) 100199f89551SEric W. Biederman return -ESRCH; 100299f89551SEric W. Biederman oom_adjust = task->oomkilladj; 100399f89551SEric W. Biederman put_task_struct(task); 100499f89551SEric W. Biederman 10058578cea7SEric W. Biederman len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust); 10060c28f287SAkinobu Mita 10070c28f287SAkinobu Mita return simple_read_from_buffer(buf, count, ppos, buffer, len); 10081da177e4SLinus Torvalds } 10091da177e4SLinus Torvalds 10101da177e4SLinus Torvalds static ssize_t oom_adjust_write(struct file *file, const char __user *buf, 10111da177e4SLinus Torvalds size_t count, loff_t *ppos) 10121da177e4SLinus Torvalds { 101399f89551SEric W. Biederman struct task_struct *task; 10148578cea7SEric W. Biederman char buffer[PROC_NUMBUF], *end; 10151da177e4SLinus Torvalds int oom_adjust; 10161da177e4SLinus Torvalds 10178578cea7SEric W. Biederman memset(buffer, 0, sizeof(buffer)); 10188578cea7SEric W. Biederman if (count > sizeof(buffer) - 1) 10198578cea7SEric W. Biederman count = sizeof(buffer) - 1; 10201da177e4SLinus Torvalds if (copy_from_user(buffer, buf, count)) 10211da177e4SLinus Torvalds return -EFAULT; 10221da177e4SLinus Torvalds oom_adjust = simple_strtol(buffer, &end, 0); 10238ac773b4SAlexey Dobriyan if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) && 10248ac773b4SAlexey Dobriyan oom_adjust != OOM_DISABLE) 10251da177e4SLinus Torvalds return -EINVAL; 10261da177e4SLinus Torvalds if (*end == '\n') 10271da177e4SLinus Torvalds end++; 10282fddfeefSJosef "Jeff" Sipek task = get_proc_task(file->f_path.dentry->d_inode); 102999f89551SEric W. Biederman if (!task) 103099f89551SEric W. Biederman return -ESRCH; 10318fb4fc68SGuillem Jover if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) { 10328fb4fc68SGuillem Jover put_task_struct(task); 10338fb4fc68SGuillem Jover return -EACCES; 10348fb4fc68SGuillem Jover } 10351da177e4SLinus Torvalds task->oomkilladj = oom_adjust; 103699f89551SEric W. Biederman put_task_struct(task); 10371da177e4SLinus Torvalds if (end - buffer == 0) 10381da177e4SLinus Torvalds return -EIO; 10391da177e4SLinus Torvalds return end - buffer; 10401da177e4SLinus Torvalds } 10411da177e4SLinus Torvalds 104200977a59SArjan van de Ven static const struct file_operations proc_oom_adjust_operations = { 10431da177e4SLinus Torvalds .read = oom_adjust_read, 10441da177e4SLinus Torvalds .write = oom_adjust_write, 10451da177e4SLinus Torvalds }; 10461da177e4SLinus Torvalds 10471da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL 10481da177e4SLinus Torvalds #define TMPBUFLEN 21 10491da177e4SLinus Torvalds static ssize_t proc_loginuid_read(struct file * file, char __user * buf, 10501da177e4SLinus Torvalds size_t count, loff_t *ppos) 10511da177e4SLinus Torvalds { 10522fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 105399f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 10541da177e4SLinus Torvalds ssize_t length; 10551da177e4SLinus Torvalds char tmpbuf[TMPBUFLEN]; 10561da177e4SLinus Torvalds 105799f89551SEric W. Biederman if (!task) 105899f89551SEric W. Biederman return -ESRCH; 10591da177e4SLinus Torvalds length = scnprintf(tmpbuf, TMPBUFLEN, "%u", 10600c11b942SAl Viro audit_get_loginuid(task)); 106199f89551SEric W. Biederman put_task_struct(task); 10621da177e4SLinus Torvalds return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 10631da177e4SLinus Torvalds } 10641da177e4SLinus Torvalds 10651da177e4SLinus Torvalds static ssize_t proc_loginuid_write(struct file * file, const char __user * buf, 10661da177e4SLinus Torvalds size_t count, loff_t *ppos) 10671da177e4SLinus Torvalds { 10682fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 10691da177e4SLinus Torvalds char *page, *tmp; 10701da177e4SLinus Torvalds ssize_t length; 10711da177e4SLinus Torvalds uid_t loginuid; 10721da177e4SLinus Torvalds 10731da177e4SLinus Torvalds if (!capable(CAP_AUDIT_CONTROL)) 10741da177e4SLinus Torvalds return -EPERM; 10751da177e4SLinus Torvalds 107613b41b09SEric W. Biederman if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) 10771da177e4SLinus Torvalds return -EPERM; 10781da177e4SLinus Torvalds 1079e0182909SAl Viro if (count >= PAGE_SIZE) 1080e0182909SAl Viro count = PAGE_SIZE - 1; 10811da177e4SLinus Torvalds 10821da177e4SLinus Torvalds if (*ppos != 0) { 10831da177e4SLinus Torvalds /* No partial writes. */ 10841da177e4SLinus Torvalds return -EINVAL; 10851da177e4SLinus Torvalds } 1086e12ba74dSMel Gorman page = (char*)__get_free_page(GFP_TEMPORARY); 10871da177e4SLinus Torvalds if (!page) 10881da177e4SLinus Torvalds return -ENOMEM; 10891da177e4SLinus Torvalds length = -EFAULT; 10901da177e4SLinus Torvalds if (copy_from_user(page, buf, count)) 10911da177e4SLinus Torvalds goto out_free_page; 10921da177e4SLinus Torvalds 1093e0182909SAl Viro page[count] = '\0'; 10941da177e4SLinus Torvalds loginuid = simple_strtoul(page, &tmp, 10); 10951da177e4SLinus Torvalds if (tmp == page) { 10961da177e4SLinus Torvalds length = -EINVAL; 10971da177e4SLinus Torvalds goto out_free_page; 10981da177e4SLinus Torvalds 10991da177e4SLinus Torvalds } 110099f89551SEric W. Biederman length = audit_set_loginuid(current, loginuid); 11011da177e4SLinus Torvalds if (likely(length == 0)) 11021da177e4SLinus Torvalds length = count; 11031da177e4SLinus Torvalds 11041da177e4SLinus Torvalds out_free_page: 11051da177e4SLinus Torvalds free_page((unsigned long) page); 11061da177e4SLinus Torvalds return length; 11071da177e4SLinus Torvalds } 11081da177e4SLinus Torvalds 110900977a59SArjan van de Ven static const struct file_operations proc_loginuid_operations = { 11101da177e4SLinus Torvalds .read = proc_loginuid_read, 11111da177e4SLinus Torvalds .write = proc_loginuid_write, 11121da177e4SLinus Torvalds }; 11131e0bd755SEric Paris 11141e0bd755SEric Paris static ssize_t proc_sessionid_read(struct file * file, char __user * buf, 11151e0bd755SEric Paris size_t count, loff_t *ppos) 11161e0bd755SEric Paris { 11171e0bd755SEric Paris struct inode * inode = file->f_path.dentry->d_inode; 11181e0bd755SEric Paris struct task_struct *task = get_proc_task(inode); 11191e0bd755SEric Paris ssize_t length; 11201e0bd755SEric Paris char tmpbuf[TMPBUFLEN]; 11211e0bd755SEric Paris 11221e0bd755SEric Paris if (!task) 11231e0bd755SEric Paris return -ESRCH; 11241e0bd755SEric Paris length = scnprintf(tmpbuf, TMPBUFLEN, "%u", 11251e0bd755SEric Paris audit_get_sessionid(task)); 11261e0bd755SEric Paris put_task_struct(task); 11271e0bd755SEric Paris return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 11281e0bd755SEric Paris } 11291e0bd755SEric Paris 11301e0bd755SEric Paris static const struct file_operations proc_sessionid_operations = { 11311e0bd755SEric Paris .read = proc_sessionid_read, 11321e0bd755SEric Paris }; 11331da177e4SLinus Torvalds #endif 11341da177e4SLinus Torvalds 1135f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION 1136f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_read(struct file * file, char __user * buf, 1137f4f154fdSAkinobu Mita size_t count, loff_t *ppos) 1138f4f154fdSAkinobu Mita { 1139f4f154fdSAkinobu Mita struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 1140f4f154fdSAkinobu Mita char buffer[PROC_NUMBUF]; 1141f4f154fdSAkinobu Mita size_t len; 1142f4f154fdSAkinobu Mita int make_it_fail; 1143f4f154fdSAkinobu Mita 1144f4f154fdSAkinobu Mita if (!task) 1145f4f154fdSAkinobu Mita return -ESRCH; 1146f4f154fdSAkinobu Mita make_it_fail = task->make_it_fail; 1147f4f154fdSAkinobu Mita put_task_struct(task); 1148f4f154fdSAkinobu Mita 1149f4f154fdSAkinobu Mita len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail); 11500c28f287SAkinobu Mita 11510c28f287SAkinobu Mita return simple_read_from_buffer(buf, count, ppos, buffer, len); 1152f4f154fdSAkinobu Mita } 1153f4f154fdSAkinobu Mita 1154f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_write(struct file * file, 1155f4f154fdSAkinobu Mita const char __user * buf, size_t count, loff_t *ppos) 1156f4f154fdSAkinobu Mita { 1157f4f154fdSAkinobu Mita struct task_struct *task; 1158f4f154fdSAkinobu Mita char buffer[PROC_NUMBUF], *end; 1159f4f154fdSAkinobu Mita int make_it_fail; 1160f4f154fdSAkinobu Mita 1161f4f154fdSAkinobu Mita if (!capable(CAP_SYS_RESOURCE)) 1162f4f154fdSAkinobu Mita return -EPERM; 1163f4f154fdSAkinobu Mita memset(buffer, 0, sizeof(buffer)); 1164f4f154fdSAkinobu Mita if (count > sizeof(buffer) - 1) 1165f4f154fdSAkinobu Mita count = sizeof(buffer) - 1; 1166f4f154fdSAkinobu Mita if (copy_from_user(buffer, buf, count)) 1167f4f154fdSAkinobu Mita return -EFAULT; 1168f4f154fdSAkinobu Mita make_it_fail = simple_strtol(buffer, &end, 0); 1169f4f154fdSAkinobu Mita if (*end == '\n') 1170f4f154fdSAkinobu Mita end++; 1171f4f154fdSAkinobu Mita task = get_proc_task(file->f_dentry->d_inode); 1172f4f154fdSAkinobu Mita if (!task) 1173f4f154fdSAkinobu Mita return -ESRCH; 1174f4f154fdSAkinobu Mita task->make_it_fail = make_it_fail; 1175f4f154fdSAkinobu Mita put_task_struct(task); 1176f4f154fdSAkinobu Mita if (end - buffer == 0) 1177f4f154fdSAkinobu Mita return -EIO; 1178f4f154fdSAkinobu Mita return end - buffer; 1179f4f154fdSAkinobu Mita } 1180f4f154fdSAkinobu Mita 118100977a59SArjan van de Ven static const struct file_operations proc_fault_inject_operations = { 1182f4f154fdSAkinobu Mita .read = proc_fault_inject_read, 1183f4f154fdSAkinobu Mita .write = proc_fault_inject_write, 1184f4f154fdSAkinobu Mita }; 1185f4f154fdSAkinobu Mita #endif 1186f4f154fdSAkinobu Mita 11879745512cSArjan van de Ven 118843ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG 118943ae34cbSIngo Molnar /* 119043ae34cbSIngo Molnar * Print out various scheduling related per-task fields: 119143ae34cbSIngo Molnar */ 119243ae34cbSIngo Molnar static int sched_show(struct seq_file *m, void *v) 119343ae34cbSIngo Molnar { 119443ae34cbSIngo Molnar struct inode *inode = m->private; 119543ae34cbSIngo Molnar struct task_struct *p; 119643ae34cbSIngo Molnar 119743ae34cbSIngo Molnar WARN_ON(!inode); 119843ae34cbSIngo Molnar 119943ae34cbSIngo Molnar p = get_proc_task(inode); 120043ae34cbSIngo Molnar if (!p) 120143ae34cbSIngo Molnar return -ESRCH; 120243ae34cbSIngo Molnar proc_sched_show_task(p, m); 120343ae34cbSIngo Molnar 120443ae34cbSIngo Molnar put_task_struct(p); 120543ae34cbSIngo Molnar 120643ae34cbSIngo Molnar return 0; 120743ae34cbSIngo Molnar } 120843ae34cbSIngo Molnar 120943ae34cbSIngo Molnar static ssize_t 121043ae34cbSIngo Molnar sched_write(struct file *file, const char __user *buf, 121143ae34cbSIngo Molnar size_t count, loff_t *offset) 121243ae34cbSIngo Molnar { 121343ae34cbSIngo Molnar struct inode *inode = file->f_path.dentry->d_inode; 121443ae34cbSIngo Molnar struct task_struct *p; 121543ae34cbSIngo Molnar 121643ae34cbSIngo Molnar WARN_ON(!inode); 121743ae34cbSIngo Molnar 121843ae34cbSIngo Molnar p = get_proc_task(inode); 121943ae34cbSIngo Molnar if (!p) 122043ae34cbSIngo Molnar return -ESRCH; 122143ae34cbSIngo Molnar proc_sched_set_task(p); 122243ae34cbSIngo Molnar 122343ae34cbSIngo Molnar put_task_struct(p); 122443ae34cbSIngo Molnar 122543ae34cbSIngo Molnar return count; 122643ae34cbSIngo Molnar } 122743ae34cbSIngo Molnar 122843ae34cbSIngo Molnar static int sched_open(struct inode *inode, struct file *filp) 122943ae34cbSIngo Molnar { 123043ae34cbSIngo Molnar int ret; 123143ae34cbSIngo Molnar 123243ae34cbSIngo Molnar ret = single_open(filp, sched_show, NULL); 123343ae34cbSIngo Molnar if (!ret) { 123443ae34cbSIngo Molnar struct seq_file *m = filp->private_data; 123543ae34cbSIngo Molnar 123643ae34cbSIngo Molnar m->private = inode; 123743ae34cbSIngo Molnar } 123843ae34cbSIngo Molnar return ret; 123943ae34cbSIngo Molnar } 124043ae34cbSIngo Molnar 124143ae34cbSIngo Molnar static const struct file_operations proc_pid_sched_operations = { 124243ae34cbSIngo Molnar .open = sched_open, 124343ae34cbSIngo Molnar .read = seq_read, 124443ae34cbSIngo Molnar .write = sched_write, 124543ae34cbSIngo Molnar .llseek = seq_lseek, 12465ea473a1SAlexey Dobriyan .release = single_release, 124743ae34cbSIngo Molnar }; 124843ae34cbSIngo Molnar 124943ae34cbSIngo Molnar #endif 125043ae34cbSIngo Molnar 1251925d1c40SMatt Helsley /* 1252925d1c40SMatt Helsley * We added or removed a vma mapping the executable. The vmas are only mapped 1253925d1c40SMatt Helsley * during exec and are not mapped with the mmap system call. 1254925d1c40SMatt Helsley * Callers must hold down_write() on the mm's mmap_sem for these 1255925d1c40SMatt Helsley */ 1256925d1c40SMatt Helsley void added_exe_file_vma(struct mm_struct *mm) 1257925d1c40SMatt Helsley { 1258925d1c40SMatt Helsley mm->num_exe_file_vmas++; 1259925d1c40SMatt Helsley } 1260925d1c40SMatt Helsley 1261925d1c40SMatt Helsley void removed_exe_file_vma(struct mm_struct *mm) 1262925d1c40SMatt Helsley { 1263925d1c40SMatt Helsley mm->num_exe_file_vmas--; 1264925d1c40SMatt Helsley if ((mm->num_exe_file_vmas == 0) && mm->exe_file){ 1265925d1c40SMatt Helsley fput(mm->exe_file); 1266925d1c40SMatt Helsley mm->exe_file = NULL; 1267925d1c40SMatt Helsley } 1268925d1c40SMatt Helsley 1269925d1c40SMatt Helsley } 1270925d1c40SMatt Helsley 1271925d1c40SMatt Helsley void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file) 1272925d1c40SMatt Helsley { 1273925d1c40SMatt Helsley if (new_exe_file) 1274925d1c40SMatt Helsley get_file(new_exe_file); 1275925d1c40SMatt Helsley if (mm->exe_file) 1276925d1c40SMatt Helsley fput(mm->exe_file); 1277925d1c40SMatt Helsley mm->exe_file = new_exe_file; 1278925d1c40SMatt Helsley mm->num_exe_file_vmas = 0; 1279925d1c40SMatt Helsley } 1280925d1c40SMatt Helsley 1281925d1c40SMatt Helsley struct file *get_mm_exe_file(struct mm_struct *mm) 1282925d1c40SMatt Helsley { 1283925d1c40SMatt Helsley struct file *exe_file; 1284925d1c40SMatt Helsley 1285925d1c40SMatt Helsley /* We need mmap_sem to protect against races with removal of 1286925d1c40SMatt Helsley * VM_EXECUTABLE vmas */ 1287925d1c40SMatt Helsley down_read(&mm->mmap_sem); 1288925d1c40SMatt Helsley exe_file = mm->exe_file; 1289925d1c40SMatt Helsley if (exe_file) 1290925d1c40SMatt Helsley get_file(exe_file); 1291925d1c40SMatt Helsley up_read(&mm->mmap_sem); 1292925d1c40SMatt Helsley return exe_file; 1293925d1c40SMatt Helsley } 1294925d1c40SMatt Helsley 1295925d1c40SMatt Helsley void dup_mm_exe_file(struct mm_struct *oldmm, struct mm_struct *newmm) 1296925d1c40SMatt Helsley { 1297925d1c40SMatt Helsley /* It's safe to write the exe_file pointer without exe_file_lock because 1298925d1c40SMatt Helsley * this is called during fork when the task is not yet in /proc */ 1299925d1c40SMatt Helsley newmm->exe_file = get_mm_exe_file(oldmm); 1300925d1c40SMatt Helsley } 1301925d1c40SMatt Helsley 1302925d1c40SMatt Helsley static int proc_exe_link(struct inode *inode, struct path *exe_path) 1303925d1c40SMatt Helsley { 1304925d1c40SMatt Helsley struct task_struct *task; 1305925d1c40SMatt Helsley struct mm_struct *mm; 1306925d1c40SMatt Helsley struct file *exe_file; 1307925d1c40SMatt Helsley 1308925d1c40SMatt Helsley task = get_proc_task(inode); 1309925d1c40SMatt Helsley if (!task) 1310925d1c40SMatt Helsley return -ENOENT; 1311925d1c40SMatt Helsley mm = get_task_mm(task); 1312925d1c40SMatt Helsley put_task_struct(task); 1313925d1c40SMatt Helsley if (!mm) 1314925d1c40SMatt Helsley return -ENOENT; 1315925d1c40SMatt Helsley exe_file = get_mm_exe_file(mm); 1316925d1c40SMatt Helsley mmput(mm); 1317925d1c40SMatt Helsley if (exe_file) { 1318925d1c40SMatt Helsley *exe_path = exe_file->f_path; 1319925d1c40SMatt Helsley path_get(&exe_file->f_path); 1320925d1c40SMatt Helsley fput(exe_file); 1321925d1c40SMatt Helsley return 0; 1322925d1c40SMatt Helsley } else 1323925d1c40SMatt Helsley return -ENOENT; 1324925d1c40SMatt Helsley } 1325925d1c40SMatt Helsley 1326008b150aSAl Viro static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd) 13271da177e4SLinus Torvalds { 13281da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 13291da177e4SLinus Torvalds int error = -EACCES; 13301da177e4SLinus Torvalds 13311da177e4SLinus Torvalds /* We don't need a base pointer in the /proc filesystem */ 13321d957f9bSJan Blunck path_put(&nd->path); 13331da177e4SLinus Torvalds 1334778c1144SEric W. Biederman /* Are we allowed to snoop on the tasks file descriptors? */ 1335778c1144SEric W. Biederman if (!proc_fd_access_allowed(inode)) 13361da177e4SLinus Torvalds goto out; 13371da177e4SLinus Torvalds 13383dcd25f3SJan Blunck error = PROC_I(inode)->op.proc_get_link(inode, &nd->path); 13391da177e4SLinus Torvalds nd->last_type = LAST_BIND; 13401da177e4SLinus Torvalds out: 1341008b150aSAl Viro return ERR_PTR(error); 13421da177e4SLinus Torvalds } 13431da177e4SLinus Torvalds 13443dcd25f3SJan Blunck static int do_proc_readlink(struct path *path, char __user *buffer, int buflen) 13451da177e4SLinus Torvalds { 1346e12ba74dSMel Gorman char *tmp = (char*)__get_free_page(GFP_TEMPORARY); 13473dcd25f3SJan Blunck char *pathname; 13481da177e4SLinus Torvalds int len; 13491da177e4SLinus Torvalds 13501da177e4SLinus Torvalds if (!tmp) 13511da177e4SLinus Torvalds return -ENOMEM; 13521da177e4SLinus Torvalds 1353cf28b486SJan Blunck pathname = d_path(path, tmp, PAGE_SIZE); 13543dcd25f3SJan Blunck len = PTR_ERR(pathname); 13553dcd25f3SJan Blunck if (IS_ERR(pathname)) 13561da177e4SLinus Torvalds goto out; 13573dcd25f3SJan Blunck len = tmp + PAGE_SIZE - 1 - pathname; 13581da177e4SLinus Torvalds 13591da177e4SLinus Torvalds if (len > buflen) 13601da177e4SLinus Torvalds len = buflen; 13613dcd25f3SJan Blunck if (copy_to_user(buffer, pathname, len)) 13621da177e4SLinus Torvalds len = -EFAULT; 13631da177e4SLinus Torvalds out: 13641da177e4SLinus Torvalds free_page((unsigned long)tmp); 13651da177e4SLinus Torvalds return len; 13661da177e4SLinus Torvalds } 13671da177e4SLinus Torvalds 13681da177e4SLinus Torvalds static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen) 13691da177e4SLinus Torvalds { 13701da177e4SLinus Torvalds int error = -EACCES; 13711da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 13723dcd25f3SJan Blunck struct path path; 13731da177e4SLinus Torvalds 1374778c1144SEric W. Biederman /* Are we allowed to snoop on the tasks file descriptors? */ 1375778c1144SEric W. Biederman if (!proc_fd_access_allowed(inode)) 13761da177e4SLinus Torvalds goto out; 13771da177e4SLinus Torvalds 13783dcd25f3SJan Blunck error = PROC_I(inode)->op.proc_get_link(inode, &path); 13791da177e4SLinus Torvalds if (error) 13801da177e4SLinus Torvalds goto out; 13811da177e4SLinus Torvalds 13823dcd25f3SJan Blunck error = do_proc_readlink(&path, buffer, buflen); 13833dcd25f3SJan Blunck path_put(&path); 13841da177e4SLinus Torvalds out: 13851da177e4SLinus Torvalds return error; 13861da177e4SLinus Torvalds } 13871da177e4SLinus Torvalds 1388c5ef1c42SArjan van de Ven static const struct inode_operations proc_pid_link_inode_operations = { 13891da177e4SLinus Torvalds .readlink = proc_pid_readlink, 13906d76fa58SLinus Torvalds .follow_link = proc_pid_follow_link, 13916d76fa58SLinus Torvalds .setattr = proc_setattr, 13921da177e4SLinus Torvalds }; 13931da177e4SLinus Torvalds 139428a6d671SEric W. Biederman 139528a6d671SEric W. Biederman /* building an inode */ 139628a6d671SEric W. Biederman 139728a6d671SEric W. Biederman static int task_dumpable(struct task_struct *task) 139828a6d671SEric W. Biederman { 139928a6d671SEric W. Biederman int dumpable = 0; 140028a6d671SEric W. Biederman struct mm_struct *mm; 140128a6d671SEric W. Biederman 140228a6d671SEric W. Biederman task_lock(task); 140328a6d671SEric W. Biederman mm = task->mm; 140428a6d671SEric W. Biederman if (mm) 14056c5d5238SKawai, Hidehiro dumpable = get_dumpable(mm); 140628a6d671SEric W. Biederman task_unlock(task); 140728a6d671SEric W. Biederman if(dumpable == 1) 140828a6d671SEric W. Biederman return 1; 140928a6d671SEric W. Biederman return 0; 141028a6d671SEric W. Biederman } 141128a6d671SEric W. Biederman 141228a6d671SEric W. Biederman 141361a28784SEric W. Biederman static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task) 141428a6d671SEric W. Biederman { 141528a6d671SEric W. Biederman struct inode * inode; 141628a6d671SEric W. Biederman struct proc_inode *ei; 141728a6d671SEric W. Biederman 141828a6d671SEric W. Biederman /* We need a new inode */ 141928a6d671SEric W. Biederman 142028a6d671SEric W. Biederman inode = new_inode(sb); 142128a6d671SEric W. Biederman if (!inode) 142228a6d671SEric W. Biederman goto out; 142328a6d671SEric W. Biederman 142428a6d671SEric W. Biederman /* Common stuff */ 142528a6d671SEric W. Biederman ei = PROC_I(inode); 142628a6d671SEric W. Biederman inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; 142728a6d671SEric W. Biederman inode->i_op = &proc_def_inode_operations; 142828a6d671SEric W. Biederman 142928a6d671SEric W. Biederman /* 143028a6d671SEric W. Biederman * grab the reference to task. 143128a6d671SEric W. Biederman */ 14321a657f78SOleg Nesterov ei->pid = get_task_pid(task, PIDTYPE_PID); 143328a6d671SEric W. Biederman if (!ei->pid) 143428a6d671SEric W. Biederman goto out_unlock; 143528a6d671SEric W. Biederman 143628a6d671SEric W. Biederman inode->i_uid = 0; 143728a6d671SEric W. Biederman inode->i_gid = 0; 143828a6d671SEric W. Biederman if (task_dumpable(task)) { 143928a6d671SEric W. Biederman inode->i_uid = task->euid; 144028a6d671SEric W. Biederman inode->i_gid = task->egid; 144128a6d671SEric W. Biederman } 144228a6d671SEric W. Biederman security_task_to_inode(task, inode); 144328a6d671SEric W. Biederman 144428a6d671SEric W. Biederman out: 144528a6d671SEric W. Biederman return inode; 144628a6d671SEric W. Biederman 144728a6d671SEric W. Biederman out_unlock: 144828a6d671SEric W. Biederman iput(inode); 144928a6d671SEric W. Biederman return NULL; 145028a6d671SEric W. Biederman } 145128a6d671SEric W. Biederman 145228a6d671SEric W. Biederman static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) 145328a6d671SEric W. Biederman { 145428a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 145528a6d671SEric W. Biederman struct task_struct *task; 145628a6d671SEric W. Biederman generic_fillattr(inode, stat); 145728a6d671SEric W. Biederman 145828a6d671SEric W. Biederman rcu_read_lock(); 145928a6d671SEric W. Biederman stat->uid = 0; 146028a6d671SEric W. Biederman stat->gid = 0; 146128a6d671SEric W. Biederman task = pid_task(proc_pid(inode), PIDTYPE_PID); 146228a6d671SEric W. Biederman if (task) { 146328a6d671SEric W. Biederman if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) || 146428a6d671SEric W. Biederman task_dumpable(task)) { 146528a6d671SEric W. Biederman stat->uid = task->euid; 146628a6d671SEric W. Biederman stat->gid = task->egid; 146728a6d671SEric W. Biederman } 146828a6d671SEric W. Biederman } 146928a6d671SEric W. Biederman rcu_read_unlock(); 147028a6d671SEric W. Biederman return 0; 147128a6d671SEric W. Biederman } 147228a6d671SEric W. Biederman 147328a6d671SEric W. Biederman /* dentry stuff */ 147428a6d671SEric W. Biederman 147528a6d671SEric W. Biederman /* 147628a6d671SEric W. Biederman * Exceptional case: normally we are not allowed to unhash a busy 147728a6d671SEric W. Biederman * directory. In this case, however, we can do it - no aliasing problems 147828a6d671SEric W. Biederman * due to the way we treat inodes. 147928a6d671SEric W. Biederman * 148028a6d671SEric W. Biederman * Rewrite the inode's ownerships here because the owning task may have 148128a6d671SEric W. Biederman * performed a setuid(), etc. 148228a6d671SEric W. Biederman * 148328a6d671SEric W. Biederman * Before the /proc/pid/status file was created the only way to read 148428a6d671SEric W. Biederman * the effective uid of a /process was to stat /proc/pid. Reading 148528a6d671SEric W. Biederman * /proc/pid/status is slow enough that procps and other packages 148628a6d671SEric W. Biederman * kept stating /proc/pid. To keep the rules in /proc simple I have 148728a6d671SEric W. Biederman * made this apply to all per process world readable and executable 148828a6d671SEric W. Biederman * directories. 148928a6d671SEric W. Biederman */ 149028a6d671SEric W. Biederman static int pid_revalidate(struct dentry *dentry, struct nameidata *nd) 149128a6d671SEric W. Biederman { 149228a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 149328a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 149428a6d671SEric W. Biederman if (task) { 149528a6d671SEric W. Biederman if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) || 149628a6d671SEric W. Biederman task_dumpable(task)) { 149728a6d671SEric W. Biederman inode->i_uid = task->euid; 149828a6d671SEric W. Biederman inode->i_gid = task->egid; 149928a6d671SEric W. Biederman } else { 150028a6d671SEric W. Biederman inode->i_uid = 0; 150128a6d671SEric W. Biederman inode->i_gid = 0; 150228a6d671SEric W. Biederman } 150328a6d671SEric W. Biederman inode->i_mode &= ~(S_ISUID | S_ISGID); 150428a6d671SEric W. Biederman security_task_to_inode(task, inode); 150528a6d671SEric W. Biederman put_task_struct(task); 150628a6d671SEric W. Biederman return 1; 150728a6d671SEric W. Biederman } 150828a6d671SEric W. Biederman d_drop(dentry); 150928a6d671SEric W. Biederman return 0; 151028a6d671SEric W. Biederman } 151128a6d671SEric W. Biederman 151228a6d671SEric W. Biederman static int pid_delete_dentry(struct dentry * dentry) 151328a6d671SEric W. Biederman { 151428a6d671SEric W. Biederman /* Is the task we represent dead? 151528a6d671SEric W. Biederman * If so, then don't put the dentry on the lru list, 151628a6d671SEric W. Biederman * kill it immediately. 151728a6d671SEric W. Biederman */ 151828a6d671SEric W. Biederman return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first; 151928a6d671SEric W. Biederman } 152028a6d671SEric W. Biederman 152128a6d671SEric W. Biederman static struct dentry_operations pid_dentry_operations = 152228a6d671SEric W. Biederman { 152328a6d671SEric W. Biederman .d_revalidate = pid_revalidate, 152428a6d671SEric W. Biederman .d_delete = pid_delete_dentry, 152528a6d671SEric W. Biederman }; 152628a6d671SEric W. Biederman 152728a6d671SEric W. Biederman /* Lookups */ 152828a6d671SEric W. Biederman 1529c5141e6dSEric Dumazet typedef struct dentry *instantiate_t(struct inode *, struct dentry *, 1530c5141e6dSEric Dumazet struct task_struct *, const void *); 153161a28784SEric W. Biederman 15321c0d04c9SEric W. Biederman /* 15331c0d04c9SEric W. Biederman * Fill a directory entry. 15341c0d04c9SEric W. Biederman * 15351c0d04c9SEric W. Biederman * If possible create the dcache entry and derive our inode number and 15361c0d04c9SEric W. Biederman * file type from dcache entry. 15371c0d04c9SEric W. Biederman * 15381c0d04c9SEric W. Biederman * Since all of the proc inode numbers are dynamically generated, the inode 15391c0d04c9SEric W. Biederman * numbers do not exist until the inode is cache. This means creating the 15401c0d04c9SEric W. Biederman * the dcache entry in readdir is necessary to keep the inode numbers 15411c0d04c9SEric W. Biederman * reported by readdir in sync with the inode numbers reported 15421c0d04c9SEric W. Biederman * by stat. 15431c0d04c9SEric W. Biederman */ 154461a28784SEric W. Biederman static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir, 154561a28784SEric W. Biederman char *name, int len, 1546c5141e6dSEric Dumazet instantiate_t instantiate, struct task_struct *task, const void *ptr) 154761a28784SEric W. Biederman { 15482fddfeefSJosef "Jeff" Sipek struct dentry *child, *dir = filp->f_path.dentry; 154961a28784SEric W. Biederman struct inode *inode; 155061a28784SEric W. Biederman struct qstr qname; 155161a28784SEric W. Biederman ino_t ino = 0; 155261a28784SEric W. Biederman unsigned type = DT_UNKNOWN; 155361a28784SEric W. Biederman 155461a28784SEric W. Biederman qname.name = name; 155561a28784SEric W. Biederman qname.len = len; 155661a28784SEric W. Biederman qname.hash = full_name_hash(name, len); 155761a28784SEric W. Biederman 155861a28784SEric W. Biederman child = d_lookup(dir, &qname); 155961a28784SEric W. Biederman if (!child) { 156061a28784SEric W. Biederman struct dentry *new; 156161a28784SEric W. Biederman new = d_alloc(dir, &qname); 156261a28784SEric W. Biederman if (new) { 156361a28784SEric W. Biederman child = instantiate(dir->d_inode, new, task, ptr); 156461a28784SEric W. Biederman if (child) 156561a28784SEric W. Biederman dput(new); 156661a28784SEric W. Biederman else 156761a28784SEric W. Biederman child = new; 156861a28784SEric W. Biederman } 156961a28784SEric W. Biederman } 157061a28784SEric W. Biederman if (!child || IS_ERR(child) || !child->d_inode) 157161a28784SEric W. Biederman goto end_instantiate; 157261a28784SEric W. Biederman inode = child->d_inode; 157361a28784SEric W. Biederman if (inode) { 157461a28784SEric W. Biederman ino = inode->i_ino; 157561a28784SEric W. Biederman type = inode->i_mode >> 12; 157661a28784SEric W. Biederman } 157761a28784SEric W. Biederman dput(child); 157861a28784SEric W. Biederman end_instantiate: 157961a28784SEric W. Biederman if (!ino) 158061a28784SEric W. Biederman ino = find_inode_number(dir, &qname); 158161a28784SEric W. Biederman if (!ino) 158261a28784SEric W. Biederman ino = 1; 158361a28784SEric W. Biederman return filldir(dirent, name, len, filp->f_pos, ino, type); 158461a28784SEric W. Biederman } 158561a28784SEric W. Biederman 158628a6d671SEric W. Biederman static unsigned name_to_int(struct dentry *dentry) 158728a6d671SEric W. Biederman { 158828a6d671SEric W. Biederman const char *name = dentry->d_name.name; 158928a6d671SEric W. Biederman int len = dentry->d_name.len; 159028a6d671SEric W. Biederman unsigned n = 0; 159128a6d671SEric W. Biederman 159228a6d671SEric W. Biederman if (len > 1 && *name == '0') 159328a6d671SEric W. Biederman goto out; 159428a6d671SEric W. Biederman while (len-- > 0) { 159528a6d671SEric W. Biederman unsigned c = *name++ - '0'; 159628a6d671SEric W. Biederman if (c > 9) 159728a6d671SEric W. Biederman goto out; 159828a6d671SEric W. Biederman if (n >= (~0U-9)/10) 159928a6d671SEric W. Biederman goto out; 160028a6d671SEric W. Biederman n *= 10; 160128a6d671SEric W. Biederman n += c; 160228a6d671SEric W. Biederman } 160328a6d671SEric W. Biederman return n; 160428a6d671SEric W. Biederman out: 160528a6d671SEric W. Biederman return ~0U; 160628a6d671SEric W. Biederman } 160728a6d671SEric W. Biederman 160827932742SMiklos Szeredi #define PROC_FDINFO_MAX 64 160927932742SMiklos Szeredi 16103dcd25f3SJan Blunck static int proc_fd_info(struct inode *inode, struct path *path, char *info) 161128a6d671SEric W. Biederman { 161228a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 161328a6d671SEric W. Biederman struct files_struct *files = NULL; 161428a6d671SEric W. Biederman struct file *file; 161528a6d671SEric W. Biederman int fd = proc_fd(inode); 161628a6d671SEric W. Biederman 161728a6d671SEric W. Biederman if (task) { 161828a6d671SEric W. Biederman files = get_files_struct(task); 161928a6d671SEric W. Biederman put_task_struct(task); 162028a6d671SEric W. Biederman } 162128a6d671SEric W. Biederman if (files) { 162228a6d671SEric W. Biederman /* 162328a6d671SEric W. Biederman * We are not taking a ref to the file structure, so we must 162428a6d671SEric W. Biederman * hold ->file_lock. 162528a6d671SEric W. Biederman */ 162628a6d671SEric W. Biederman spin_lock(&files->file_lock); 162728a6d671SEric W. Biederman file = fcheck_files(files, fd); 162828a6d671SEric W. Biederman if (file) { 16293dcd25f3SJan Blunck if (path) { 16303dcd25f3SJan Blunck *path = file->f_path; 16313dcd25f3SJan Blunck path_get(&file->f_path); 16323dcd25f3SJan Blunck } 163327932742SMiklos Szeredi if (info) 163427932742SMiklos Szeredi snprintf(info, PROC_FDINFO_MAX, 163527932742SMiklos Szeredi "pos:\t%lli\n" 163627932742SMiklos Szeredi "flags:\t0%o\n", 163727932742SMiklos Szeredi (long long) file->f_pos, 163827932742SMiklos Szeredi file->f_flags); 163928a6d671SEric W. Biederman spin_unlock(&files->file_lock); 164028a6d671SEric W. Biederman put_files_struct(files); 164128a6d671SEric W. Biederman return 0; 164228a6d671SEric W. Biederman } 164328a6d671SEric W. Biederman spin_unlock(&files->file_lock); 164428a6d671SEric W. Biederman put_files_struct(files); 164528a6d671SEric W. Biederman } 164628a6d671SEric W. Biederman return -ENOENT; 164728a6d671SEric W. Biederman } 164828a6d671SEric W. Biederman 16493dcd25f3SJan Blunck static int proc_fd_link(struct inode *inode, struct path *path) 165027932742SMiklos Szeredi { 16513dcd25f3SJan Blunck return proc_fd_info(inode, path, NULL); 165227932742SMiklos Szeredi } 165327932742SMiklos Szeredi 165428a6d671SEric W. Biederman static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd) 165528a6d671SEric W. Biederman { 165628a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 165728a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 165828a6d671SEric W. Biederman int fd = proc_fd(inode); 165928a6d671SEric W. Biederman struct files_struct *files; 166028a6d671SEric W. Biederman 166128a6d671SEric W. Biederman if (task) { 166228a6d671SEric W. Biederman files = get_files_struct(task); 166328a6d671SEric W. Biederman if (files) { 166428a6d671SEric W. Biederman rcu_read_lock(); 166528a6d671SEric W. Biederman if (fcheck_files(files, fd)) { 166628a6d671SEric W. Biederman rcu_read_unlock(); 166728a6d671SEric W. Biederman put_files_struct(files); 166828a6d671SEric W. Biederman if (task_dumpable(task)) { 166928a6d671SEric W. Biederman inode->i_uid = task->euid; 167028a6d671SEric W. Biederman inode->i_gid = task->egid; 167128a6d671SEric W. Biederman } else { 167228a6d671SEric W. Biederman inode->i_uid = 0; 167328a6d671SEric W. Biederman inode->i_gid = 0; 167428a6d671SEric W. Biederman } 167528a6d671SEric W. Biederman inode->i_mode &= ~(S_ISUID | S_ISGID); 167628a6d671SEric W. Biederman security_task_to_inode(task, inode); 167728a6d671SEric W. Biederman put_task_struct(task); 167828a6d671SEric W. Biederman return 1; 167928a6d671SEric W. Biederman } 168028a6d671SEric W. Biederman rcu_read_unlock(); 168128a6d671SEric W. Biederman put_files_struct(files); 168228a6d671SEric W. Biederman } 168328a6d671SEric W. Biederman put_task_struct(task); 168428a6d671SEric W. Biederman } 168528a6d671SEric W. Biederman d_drop(dentry); 168628a6d671SEric W. Biederman return 0; 168728a6d671SEric W. Biederman } 168828a6d671SEric W. Biederman 168928a6d671SEric W. Biederman static struct dentry_operations tid_fd_dentry_operations = 169028a6d671SEric W. Biederman { 169128a6d671SEric W. Biederman .d_revalidate = tid_fd_revalidate, 169228a6d671SEric W. Biederman .d_delete = pid_delete_dentry, 169328a6d671SEric W. Biederman }; 169428a6d671SEric W. Biederman 1695444ceed8SEric W. Biederman static struct dentry *proc_fd_instantiate(struct inode *dir, 1696c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 169728a6d671SEric W. Biederman { 1698c5141e6dSEric Dumazet unsigned fd = *(const unsigned *)ptr; 169928a6d671SEric W. Biederman struct file *file; 170028a6d671SEric W. Biederman struct files_struct *files; 170128a6d671SEric W. Biederman struct inode *inode; 170228a6d671SEric W. Biederman struct proc_inode *ei; 1703444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-ENOENT); 170428a6d671SEric W. Biederman 170561a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 170628a6d671SEric W. Biederman if (!inode) 170728a6d671SEric W. Biederman goto out; 170828a6d671SEric W. Biederman ei = PROC_I(inode); 170928a6d671SEric W. Biederman ei->fd = fd; 171028a6d671SEric W. Biederman files = get_files_struct(task); 171128a6d671SEric W. Biederman if (!files) 1712444ceed8SEric W. Biederman goto out_iput; 171328a6d671SEric W. Biederman inode->i_mode = S_IFLNK; 171428a6d671SEric W. Biederman 171528a6d671SEric W. Biederman /* 171628a6d671SEric W. Biederman * We are not taking a ref to the file structure, so we must 171728a6d671SEric W. Biederman * hold ->file_lock. 171828a6d671SEric W. Biederman */ 171928a6d671SEric W. Biederman spin_lock(&files->file_lock); 172028a6d671SEric W. Biederman file = fcheck_files(files, fd); 172128a6d671SEric W. Biederman if (!file) 1722444ceed8SEric W. Biederman goto out_unlock; 172328a6d671SEric W. Biederman if (file->f_mode & 1) 172428a6d671SEric W. Biederman inode->i_mode |= S_IRUSR | S_IXUSR; 172528a6d671SEric W. Biederman if (file->f_mode & 2) 172628a6d671SEric W. Biederman inode->i_mode |= S_IWUSR | S_IXUSR; 172728a6d671SEric W. Biederman spin_unlock(&files->file_lock); 172828a6d671SEric W. Biederman put_files_struct(files); 1729444ceed8SEric W. Biederman 173028a6d671SEric W. Biederman inode->i_op = &proc_pid_link_inode_operations; 173128a6d671SEric W. Biederman inode->i_size = 64; 173228a6d671SEric W. Biederman ei->op.proc_get_link = proc_fd_link; 173328a6d671SEric W. Biederman dentry->d_op = &tid_fd_dentry_operations; 173428a6d671SEric W. Biederman d_add(dentry, inode); 173528a6d671SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 173628a6d671SEric W. Biederman if (tid_fd_revalidate(dentry, NULL)) 1737444ceed8SEric W. Biederman error = NULL; 1738444ceed8SEric W. Biederman 1739444ceed8SEric W. Biederman out: 1740444ceed8SEric W. Biederman return error; 1741444ceed8SEric W. Biederman out_unlock: 1742444ceed8SEric W. Biederman spin_unlock(&files->file_lock); 1743444ceed8SEric W. Biederman put_files_struct(files); 1744444ceed8SEric W. Biederman out_iput: 1745444ceed8SEric W. Biederman iput(inode); 1746444ceed8SEric W. Biederman goto out; 1747444ceed8SEric W. Biederman } 1748444ceed8SEric W. Biederman 174927932742SMiklos Szeredi static struct dentry *proc_lookupfd_common(struct inode *dir, 175027932742SMiklos Szeredi struct dentry *dentry, 175127932742SMiklos Szeredi instantiate_t instantiate) 1752444ceed8SEric W. Biederman { 1753444ceed8SEric W. Biederman struct task_struct *task = get_proc_task(dir); 1754444ceed8SEric W. Biederman unsigned fd = name_to_int(dentry); 1755444ceed8SEric W. Biederman struct dentry *result = ERR_PTR(-ENOENT); 1756444ceed8SEric W. Biederman 1757444ceed8SEric W. Biederman if (!task) 1758444ceed8SEric W. Biederman goto out_no_task; 1759444ceed8SEric W. Biederman if (fd == ~0U) 1760444ceed8SEric W. Biederman goto out; 1761444ceed8SEric W. Biederman 176227932742SMiklos Szeredi result = instantiate(dir, dentry, task, &fd); 176328a6d671SEric W. Biederman out: 176428a6d671SEric W. Biederman put_task_struct(task); 176528a6d671SEric W. Biederman out_no_task: 176628a6d671SEric W. Biederman return result; 176728a6d671SEric W. Biederman } 176828a6d671SEric W. Biederman 176927932742SMiklos Szeredi static int proc_readfd_common(struct file * filp, void * dirent, 177027932742SMiklos Szeredi filldir_t filldir, instantiate_t instantiate) 17711da177e4SLinus Torvalds { 17722fddfeefSJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 17735634708bSEric W. Biederman struct inode *inode = dentry->d_inode; 177499f89551SEric W. Biederman struct task_struct *p = get_proc_task(inode); 1775457c2510SPavel Emelyanov unsigned int fd, ino; 17761da177e4SLinus Torvalds int retval; 17771da177e4SLinus Torvalds struct files_struct * files; 17781da177e4SLinus Torvalds 17791da177e4SLinus Torvalds retval = -ENOENT; 178099f89551SEric W. Biederman if (!p) 178199f89551SEric W. Biederman goto out_no_task; 17821da177e4SLinus Torvalds retval = 0; 17831da177e4SLinus Torvalds 17841da177e4SLinus Torvalds fd = filp->f_pos; 17851da177e4SLinus Torvalds switch (fd) { 17861da177e4SLinus Torvalds case 0: 17871da177e4SLinus Torvalds if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0) 17881da177e4SLinus Torvalds goto out; 17891da177e4SLinus Torvalds filp->f_pos++; 17901da177e4SLinus Torvalds case 1: 17915634708bSEric W. Biederman ino = parent_ino(dentry); 17921da177e4SLinus Torvalds if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0) 17931da177e4SLinus Torvalds goto out; 17941da177e4SLinus Torvalds filp->f_pos++; 17951da177e4SLinus Torvalds default: 17961da177e4SLinus Torvalds files = get_files_struct(p); 17971da177e4SLinus Torvalds if (!files) 17981da177e4SLinus Torvalds goto out; 1799b835996fSDipankar Sarma rcu_read_lock(); 18001da177e4SLinus Torvalds for (fd = filp->f_pos-2; 18019b4f526cSAl Viro fd < files_fdtable(files)->max_fds; 18021da177e4SLinus Torvalds fd++, filp->f_pos++) { 180327932742SMiklos Szeredi char name[PROC_NUMBUF]; 180427932742SMiklos Szeredi int len; 18051da177e4SLinus Torvalds 18061da177e4SLinus Torvalds if (!fcheck_files(files, fd)) 18071da177e4SLinus Torvalds continue; 1808b835996fSDipankar Sarma rcu_read_unlock(); 18091da177e4SLinus Torvalds 181027932742SMiklos Szeredi len = snprintf(name, sizeof(name), "%d", fd); 181127932742SMiklos Szeredi if (proc_fill_cache(filp, dirent, filldir, 181227932742SMiklos Szeredi name, len, instantiate, 181327932742SMiklos Szeredi p, &fd) < 0) { 1814b835996fSDipankar Sarma rcu_read_lock(); 18151da177e4SLinus Torvalds break; 18161da177e4SLinus Torvalds } 1817b835996fSDipankar Sarma rcu_read_lock(); 18181da177e4SLinus Torvalds } 1819b835996fSDipankar Sarma rcu_read_unlock(); 18201da177e4SLinus Torvalds put_files_struct(files); 18211da177e4SLinus Torvalds } 18221da177e4SLinus Torvalds out: 182399f89551SEric W. Biederman put_task_struct(p); 182499f89551SEric W. Biederman out_no_task: 18251da177e4SLinus Torvalds return retval; 18261da177e4SLinus Torvalds } 18271da177e4SLinus Torvalds 182827932742SMiklos Szeredi static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry, 182927932742SMiklos Szeredi struct nameidata *nd) 183027932742SMiklos Szeredi { 183127932742SMiklos Szeredi return proc_lookupfd_common(dir, dentry, proc_fd_instantiate); 183227932742SMiklos Szeredi } 183327932742SMiklos Szeredi 183427932742SMiklos Szeredi static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir) 183527932742SMiklos Szeredi { 183627932742SMiklos Szeredi return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate); 183727932742SMiklos Szeredi } 183827932742SMiklos Szeredi 183927932742SMiklos Szeredi static ssize_t proc_fdinfo_read(struct file *file, char __user *buf, 184027932742SMiklos Szeredi size_t len, loff_t *ppos) 184127932742SMiklos Szeredi { 184227932742SMiklos Szeredi char tmp[PROC_FDINFO_MAX]; 18433dcd25f3SJan Blunck int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp); 184427932742SMiklos Szeredi if (!err) 184527932742SMiklos Szeredi err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp)); 184627932742SMiklos Szeredi return err; 184727932742SMiklos Szeredi } 184827932742SMiklos Szeredi 184927932742SMiklos Szeredi static const struct file_operations proc_fdinfo_file_operations = { 185027932742SMiklos Szeredi .open = nonseekable_open, 185127932742SMiklos Szeredi .read = proc_fdinfo_read, 185227932742SMiklos Szeredi }; 185327932742SMiklos Szeredi 185400977a59SArjan van de Ven static const struct file_operations proc_fd_operations = { 18551da177e4SLinus Torvalds .read = generic_read_dir, 18561da177e4SLinus Torvalds .readdir = proc_readfd, 18571da177e4SLinus Torvalds }; 18581da177e4SLinus Torvalds 18591da177e4SLinus Torvalds /* 18608948e11fSAlexey Dobriyan * /proc/pid/fd needs a special permission handler so that a process can still 18618948e11fSAlexey Dobriyan * access /proc/self/fd after it has executed a setuid(). 18628948e11fSAlexey Dobriyan */ 1863e6305c43SAl Viro static int proc_fd_permission(struct inode *inode, int mask) 18648948e11fSAlexey Dobriyan { 18658948e11fSAlexey Dobriyan int rv; 18668948e11fSAlexey Dobriyan 18678948e11fSAlexey Dobriyan rv = generic_permission(inode, mask, NULL); 18688948e11fSAlexey Dobriyan if (rv == 0) 18698948e11fSAlexey Dobriyan return 0; 18708948e11fSAlexey Dobriyan if (task_pid(current) == proc_pid(inode)) 18718948e11fSAlexey Dobriyan rv = 0; 18728948e11fSAlexey Dobriyan return rv; 18738948e11fSAlexey Dobriyan } 18748948e11fSAlexey Dobriyan 18758948e11fSAlexey Dobriyan /* 18761da177e4SLinus Torvalds * proc directories can do almost nothing.. 18771da177e4SLinus Torvalds */ 1878c5ef1c42SArjan van de Ven static const struct inode_operations proc_fd_inode_operations = { 18791da177e4SLinus Torvalds .lookup = proc_lookupfd, 18808948e11fSAlexey Dobriyan .permission = proc_fd_permission, 18816d76fa58SLinus Torvalds .setattr = proc_setattr, 18821da177e4SLinus Torvalds }; 18831da177e4SLinus Torvalds 188427932742SMiklos Szeredi static struct dentry *proc_fdinfo_instantiate(struct inode *dir, 188527932742SMiklos Szeredi struct dentry *dentry, struct task_struct *task, const void *ptr) 188627932742SMiklos Szeredi { 188727932742SMiklos Szeredi unsigned fd = *(unsigned *)ptr; 188827932742SMiklos Szeredi struct inode *inode; 188927932742SMiklos Szeredi struct proc_inode *ei; 189027932742SMiklos Szeredi struct dentry *error = ERR_PTR(-ENOENT); 189127932742SMiklos Szeredi 189227932742SMiklos Szeredi inode = proc_pid_make_inode(dir->i_sb, task); 189327932742SMiklos Szeredi if (!inode) 189427932742SMiklos Szeredi goto out; 189527932742SMiklos Szeredi ei = PROC_I(inode); 189627932742SMiklos Szeredi ei->fd = fd; 189727932742SMiklos Szeredi inode->i_mode = S_IFREG | S_IRUSR; 189827932742SMiklos Szeredi inode->i_fop = &proc_fdinfo_file_operations; 189927932742SMiklos Szeredi dentry->d_op = &tid_fd_dentry_operations; 190027932742SMiklos Szeredi d_add(dentry, inode); 190127932742SMiklos Szeredi /* Close the race of the process dying before we return the dentry */ 190227932742SMiklos Szeredi if (tid_fd_revalidate(dentry, NULL)) 190327932742SMiklos Szeredi error = NULL; 190427932742SMiklos Szeredi 190527932742SMiklos Szeredi out: 190627932742SMiklos Szeredi return error; 190727932742SMiklos Szeredi } 190827932742SMiklos Szeredi 190927932742SMiklos Szeredi static struct dentry *proc_lookupfdinfo(struct inode *dir, 191027932742SMiklos Szeredi struct dentry *dentry, 191127932742SMiklos Szeredi struct nameidata *nd) 191227932742SMiklos Szeredi { 191327932742SMiklos Szeredi return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate); 191427932742SMiklos Szeredi } 191527932742SMiklos Szeredi 191627932742SMiklos Szeredi static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir) 191727932742SMiklos Szeredi { 191827932742SMiklos Szeredi return proc_readfd_common(filp, dirent, filldir, 191927932742SMiklos Szeredi proc_fdinfo_instantiate); 192027932742SMiklos Szeredi } 192127932742SMiklos Szeredi 192227932742SMiklos Szeredi static const struct file_operations proc_fdinfo_operations = { 192327932742SMiklos Szeredi .read = generic_read_dir, 192427932742SMiklos Szeredi .readdir = proc_readfdinfo, 192527932742SMiklos Szeredi }; 192627932742SMiklos Szeredi 192727932742SMiklos Szeredi /* 192827932742SMiklos Szeredi * proc directories can do almost nothing.. 192927932742SMiklos Szeredi */ 193027932742SMiklos Szeredi static const struct inode_operations proc_fdinfo_inode_operations = { 193127932742SMiklos Szeredi .lookup = proc_lookupfdinfo, 193227932742SMiklos Szeredi .setattr = proc_setattr, 193327932742SMiklos Szeredi }; 193427932742SMiklos Szeredi 193527932742SMiklos Szeredi 1936444ceed8SEric W. Biederman static struct dentry *proc_pident_instantiate(struct inode *dir, 1937c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 1938444ceed8SEric W. Biederman { 1939c5141e6dSEric Dumazet const struct pid_entry *p = ptr; 1940444ceed8SEric W. Biederman struct inode *inode; 1941444ceed8SEric W. Biederman struct proc_inode *ei; 1942444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-EINVAL); 1943444ceed8SEric W. Biederman 194461a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 1945444ceed8SEric W. Biederman if (!inode) 1946444ceed8SEric W. Biederman goto out; 1947444ceed8SEric W. Biederman 1948444ceed8SEric W. Biederman ei = PROC_I(inode); 1949444ceed8SEric W. Biederman inode->i_mode = p->mode; 1950444ceed8SEric W. Biederman if (S_ISDIR(inode->i_mode)) 1951444ceed8SEric W. Biederman inode->i_nlink = 2; /* Use getattr to fix if necessary */ 1952444ceed8SEric W. Biederman if (p->iop) 1953444ceed8SEric W. Biederman inode->i_op = p->iop; 1954444ceed8SEric W. Biederman if (p->fop) 1955444ceed8SEric W. Biederman inode->i_fop = p->fop; 1956444ceed8SEric W. Biederman ei->op = p->op; 1957444ceed8SEric W. Biederman dentry->d_op = &pid_dentry_operations; 1958444ceed8SEric W. Biederman d_add(dentry, inode); 1959444ceed8SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 1960444ceed8SEric W. Biederman if (pid_revalidate(dentry, NULL)) 1961444ceed8SEric W. Biederman error = NULL; 1962444ceed8SEric W. Biederman out: 1963444ceed8SEric W. Biederman return error; 1964444ceed8SEric W. Biederman } 1965444ceed8SEric W. Biederman 19661da177e4SLinus Torvalds static struct dentry *proc_pident_lookup(struct inode *dir, 19671da177e4SLinus Torvalds struct dentry *dentry, 1968c5141e6dSEric Dumazet const struct pid_entry *ents, 19697bcd6b0eSEric W. Biederman unsigned int nents) 19701da177e4SLinus Torvalds { 19711da177e4SLinus Torvalds struct inode *inode; 1972cd6a3ce9SEric W. Biederman struct dentry *error; 197399f89551SEric W. Biederman struct task_struct *task = get_proc_task(dir); 1974c5141e6dSEric Dumazet const struct pid_entry *p, *last; 19751da177e4SLinus Torvalds 1976cd6a3ce9SEric W. Biederman error = ERR_PTR(-ENOENT); 19771da177e4SLinus Torvalds inode = NULL; 19781da177e4SLinus Torvalds 197999f89551SEric W. Biederman if (!task) 198099f89551SEric W. Biederman goto out_no_task; 19811da177e4SLinus Torvalds 198220cdc894SEric W. Biederman /* 198320cdc894SEric W. Biederman * Yes, it does not scale. And it should not. Don't add 198420cdc894SEric W. Biederman * new entries into /proc/<tgid>/ without very good reasons. 198520cdc894SEric W. Biederman */ 19867bcd6b0eSEric W. Biederman last = &ents[nents - 1]; 19877bcd6b0eSEric W. Biederman for (p = ents; p <= last; p++) { 19881da177e4SLinus Torvalds if (p->len != dentry->d_name.len) 19891da177e4SLinus Torvalds continue; 19901da177e4SLinus Torvalds if (!memcmp(dentry->d_name.name, p->name, p->len)) 19911da177e4SLinus Torvalds break; 19921da177e4SLinus Torvalds } 19937bcd6b0eSEric W. Biederman if (p > last) 19941da177e4SLinus Torvalds goto out; 19951da177e4SLinus Torvalds 1996444ceed8SEric W. Biederman error = proc_pident_instantiate(dir, dentry, task, p); 19971da177e4SLinus Torvalds out: 199899f89551SEric W. Biederman put_task_struct(task); 199999f89551SEric W. Biederman out_no_task: 2000cd6a3ce9SEric W. Biederman return error; 20011da177e4SLinus Torvalds } 20021da177e4SLinus Torvalds 2003c5141e6dSEric Dumazet static int proc_pident_fill_cache(struct file *filp, void *dirent, 2004c5141e6dSEric Dumazet filldir_t filldir, struct task_struct *task, const struct pid_entry *p) 200561a28784SEric W. Biederman { 200661a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, p->name, p->len, 200761a28784SEric W. Biederman proc_pident_instantiate, task, p); 200861a28784SEric W. Biederman } 200961a28784SEric W. Biederman 201028a6d671SEric W. Biederman static int proc_pident_readdir(struct file *filp, 201128a6d671SEric W. Biederman void *dirent, filldir_t filldir, 2012c5141e6dSEric Dumazet const struct pid_entry *ents, unsigned int nents) 201328a6d671SEric W. Biederman { 201428a6d671SEric W. Biederman int i; 20152fddfeefSJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 201628a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 201728a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 2018c5141e6dSEric Dumazet const struct pid_entry *p, *last; 201928a6d671SEric W. Biederman ino_t ino; 202028a6d671SEric W. Biederman int ret; 202128a6d671SEric W. Biederman 202228a6d671SEric W. Biederman ret = -ENOENT; 202328a6d671SEric W. Biederman if (!task) 202461a28784SEric W. Biederman goto out_no_task; 202528a6d671SEric W. Biederman 202628a6d671SEric W. Biederman ret = 0; 202728a6d671SEric W. Biederman i = filp->f_pos; 202828a6d671SEric W. Biederman switch (i) { 202928a6d671SEric W. Biederman case 0: 203028a6d671SEric W. Biederman ino = inode->i_ino; 203128a6d671SEric W. Biederman if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0) 203228a6d671SEric W. Biederman goto out; 203328a6d671SEric W. Biederman i++; 203428a6d671SEric W. Biederman filp->f_pos++; 203528a6d671SEric W. Biederman /* fall through */ 203628a6d671SEric W. Biederman case 1: 203728a6d671SEric W. Biederman ino = parent_ino(dentry); 203828a6d671SEric W. Biederman if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0) 203928a6d671SEric W. Biederman goto out; 204028a6d671SEric W. Biederman i++; 204128a6d671SEric W. Biederman filp->f_pos++; 204228a6d671SEric W. Biederman /* fall through */ 204328a6d671SEric W. Biederman default: 204428a6d671SEric W. Biederman i -= 2; 204528a6d671SEric W. Biederman if (i >= nents) { 204628a6d671SEric W. Biederman ret = 1; 204728a6d671SEric W. Biederman goto out; 204828a6d671SEric W. Biederman } 204928a6d671SEric W. Biederman p = ents + i; 20507bcd6b0eSEric W. Biederman last = &ents[nents - 1]; 20517bcd6b0eSEric W. Biederman while (p <= last) { 205261a28784SEric W. Biederman if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0) 205328a6d671SEric W. Biederman goto out; 205428a6d671SEric W. Biederman filp->f_pos++; 205528a6d671SEric W. Biederman p++; 205628a6d671SEric W. Biederman } 20571da177e4SLinus Torvalds } 20581da177e4SLinus Torvalds 205928a6d671SEric W. Biederman ret = 1; 206028a6d671SEric W. Biederman out: 206161a28784SEric W. Biederman put_task_struct(task); 206261a28784SEric W. Biederman out_no_task: 206328a6d671SEric W. Biederman return ret; 20641da177e4SLinus Torvalds } 20651da177e4SLinus Torvalds 20661da177e4SLinus Torvalds #ifdef CONFIG_SECURITY 206728a6d671SEric W. Biederman static ssize_t proc_pid_attr_read(struct file * file, char __user * buf, 206828a6d671SEric W. Biederman size_t count, loff_t *ppos) 206928a6d671SEric W. Biederman { 20702fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 207104ff9708SAl Viro char *p = NULL; 207228a6d671SEric W. Biederman ssize_t length; 207328a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 207428a6d671SEric W. Biederman 207528a6d671SEric W. Biederman if (!task) 207604ff9708SAl Viro return -ESRCH; 207728a6d671SEric W. Biederman 207828a6d671SEric W. Biederman length = security_getprocattr(task, 20792fddfeefSJosef "Jeff" Sipek (char*)file->f_path.dentry->d_name.name, 208004ff9708SAl Viro &p); 208128a6d671SEric W. Biederman put_task_struct(task); 208204ff9708SAl Viro if (length > 0) 208304ff9708SAl Viro length = simple_read_from_buffer(buf, count, ppos, p, length); 208404ff9708SAl Viro kfree(p); 208528a6d671SEric W. Biederman return length; 208628a6d671SEric W. Biederman } 208728a6d671SEric W. Biederman 208828a6d671SEric W. Biederman static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf, 208928a6d671SEric W. Biederman size_t count, loff_t *ppos) 209028a6d671SEric W. Biederman { 20912fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 209228a6d671SEric W. Biederman char *page; 209328a6d671SEric W. Biederman ssize_t length; 209428a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 209528a6d671SEric W. Biederman 209628a6d671SEric W. Biederman length = -ESRCH; 209728a6d671SEric W. Biederman if (!task) 209828a6d671SEric W. Biederman goto out_no_task; 209928a6d671SEric W. Biederman if (count > PAGE_SIZE) 210028a6d671SEric W. Biederman count = PAGE_SIZE; 210128a6d671SEric W. Biederman 210228a6d671SEric W. Biederman /* No partial writes. */ 210328a6d671SEric W. Biederman length = -EINVAL; 210428a6d671SEric W. Biederman if (*ppos != 0) 210528a6d671SEric W. Biederman goto out; 210628a6d671SEric W. Biederman 210728a6d671SEric W. Biederman length = -ENOMEM; 2108e12ba74dSMel Gorman page = (char*)__get_free_page(GFP_TEMPORARY); 210928a6d671SEric W. Biederman if (!page) 211028a6d671SEric W. Biederman goto out; 211128a6d671SEric W. Biederman 211228a6d671SEric W. Biederman length = -EFAULT; 211328a6d671SEric W. Biederman if (copy_from_user(page, buf, count)) 211428a6d671SEric W. Biederman goto out_free; 211528a6d671SEric W. Biederman 211628a6d671SEric W. Biederman length = security_setprocattr(task, 21172fddfeefSJosef "Jeff" Sipek (char*)file->f_path.dentry->d_name.name, 211828a6d671SEric W. Biederman (void*)page, count); 211928a6d671SEric W. Biederman out_free: 212028a6d671SEric W. Biederman free_page((unsigned long) page); 212128a6d671SEric W. Biederman out: 212228a6d671SEric W. Biederman put_task_struct(task); 212328a6d671SEric W. Biederman out_no_task: 212428a6d671SEric W. Biederman return length; 212528a6d671SEric W. Biederman } 212628a6d671SEric W. Biederman 212700977a59SArjan van de Ven static const struct file_operations proc_pid_attr_operations = { 212828a6d671SEric W. Biederman .read = proc_pid_attr_read, 212928a6d671SEric W. Biederman .write = proc_pid_attr_write, 213028a6d671SEric W. Biederman }; 213128a6d671SEric W. Biederman 2132c5141e6dSEric Dumazet static const struct pid_entry attr_dir_stuff[] = { 213361a28784SEric W. Biederman REG("current", S_IRUGO|S_IWUGO, pid_attr), 213461a28784SEric W. Biederman REG("prev", S_IRUGO, pid_attr), 213561a28784SEric W. Biederman REG("exec", S_IRUGO|S_IWUGO, pid_attr), 213661a28784SEric W. Biederman REG("fscreate", S_IRUGO|S_IWUGO, pid_attr), 213761a28784SEric W. Biederman REG("keycreate", S_IRUGO|S_IWUGO, pid_attr), 213861a28784SEric W. Biederman REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr), 213928a6d671SEric W. Biederman }; 214028a6d671SEric W. Biederman 214172d9dcfcSEric W. Biederman static int proc_attr_dir_readdir(struct file * filp, 21421da177e4SLinus Torvalds void * dirent, filldir_t filldir) 21431da177e4SLinus Torvalds { 21441da177e4SLinus Torvalds return proc_pident_readdir(filp,dirent,filldir, 214572d9dcfcSEric W. Biederman attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff)); 21461da177e4SLinus Torvalds } 21471da177e4SLinus Torvalds 214800977a59SArjan van de Ven static const struct file_operations proc_attr_dir_operations = { 21491da177e4SLinus Torvalds .read = generic_read_dir, 215072d9dcfcSEric W. Biederman .readdir = proc_attr_dir_readdir, 21511da177e4SLinus Torvalds }; 21521da177e4SLinus Torvalds 215372d9dcfcSEric W. Biederman static struct dentry *proc_attr_dir_lookup(struct inode *dir, 21541da177e4SLinus Torvalds struct dentry *dentry, struct nameidata *nd) 21551da177e4SLinus Torvalds { 21567bcd6b0eSEric W. Biederman return proc_pident_lookup(dir, dentry, 21577bcd6b0eSEric W. Biederman attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff)); 21581da177e4SLinus Torvalds } 21591da177e4SLinus Torvalds 2160c5ef1c42SArjan van de Ven static const struct inode_operations proc_attr_dir_inode_operations = { 216172d9dcfcSEric W. Biederman .lookup = proc_attr_dir_lookup, 216299f89551SEric W. Biederman .getattr = pid_getattr, 21636d76fa58SLinus Torvalds .setattr = proc_setattr, 21641da177e4SLinus Torvalds }; 21651da177e4SLinus Torvalds 21661da177e4SLinus Torvalds #endif 21671da177e4SLinus Torvalds 21683cb4a0bbSKawai, Hidehiro #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE) 21693cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf, 21703cb4a0bbSKawai, Hidehiro size_t count, loff_t *ppos) 21713cb4a0bbSKawai, Hidehiro { 21723cb4a0bbSKawai, Hidehiro struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 21733cb4a0bbSKawai, Hidehiro struct mm_struct *mm; 21743cb4a0bbSKawai, Hidehiro char buffer[PROC_NUMBUF]; 21753cb4a0bbSKawai, Hidehiro size_t len; 21763cb4a0bbSKawai, Hidehiro int ret; 21773cb4a0bbSKawai, Hidehiro 21783cb4a0bbSKawai, Hidehiro if (!task) 21793cb4a0bbSKawai, Hidehiro return -ESRCH; 21803cb4a0bbSKawai, Hidehiro 21813cb4a0bbSKawai, Hidehiro ret = 0; 21823cb4a0bbSKawai, Hidehiro mm = get_task_mm(task); 21833cb4a0bbSKawai, Hidehiro if (mm) { 21843cb4a0bbSKawai, Hidehiro len = snprintf(buffer, sizeof(buffer), "%08lx\n", 21853cb4a0bbSKawai, Hidehiro ((mm->flags & MMF_DUMP_FILTER_MASK) >> 21863cb4a0bbSKawai, Hidehiro MMF_DUMP_FILTER_SHIFT)); 21873cb4a0bbSKawai, Hidehiro mmput(mm); 21883cb4a0bbSKawai, Hidehiro ret = simple_read_from_buffer(buf, count, ppos, buffer, len); 21893cb4a0bbSKawai, Hidehiro } 21903cb4a0bbSKawai, Hidehiro 21913cb4a0bbSKawai, Hidehiro put_task_struct(task); 21923cb4a0bbSKawai, Hidehiro 21933cb4a0bbSKawai, Hidehiro return ret; 21943cb4a0bbSKawai, Hidehiro } 21953cb4a0bbSKawai, Hidehiro 21963cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_write(struct file *file, 21973cb4a0bbSKawai, Hidehiro const char __user *buf, 21983cb4a0bbSKawai, Hidehiro size_t count, 21993cb4a0bbSKawai, Hidehiro loff_t *ppos) 22003cb4a0bbSKawai, Hidehiro { 22013cb4a0bbSKawai, Hidehiro struct task_struct *task; 22023cb4a0bbSKawai, Hidehiro struct mm_struct *mm; 22033cb4a0bbSKawai, Hidehiro char buffer[PROC_NUMBUF], *end; 22043cb4a0bbSKawai, Hidehiro unsigned int val; 22053cb4a0bbSKawai, Hidehiro int ret; 22063cb4a0bbSKawai, Hidehiro int i; 22073cb4a0bbSKawai, Hidehiro unsigned long mask; 22083cb4a0bbSKawai, Hidehiro 22093cb4a0bbSKawai, Hidehiro ret = -EFAULT; 22103cb4a0bbSKawai, Hidehiro memset(buffer, 0, sizeof(buffer)); 22113cb4a0bbSKawai, Hidehiro if (count > sizeof(buffer) - 1) 22123cb4a0bbSKawai, Hidehiro count = sizeof(buffer) - 1; 22133cb4a0bbSKawai, Hidehiro if (copy_from_user(buffer, buf, count)) 22143cb4a0bbSKawai, Hidehiro goto out_no_task; 22153cb4a0bbSKawai, Hidehiro 22163cb4a0bbSKawai, Hidehiro ret = -EINVAL; 22173cb4a0bbSKawai, Hidehiro val = (unsigned int)simple_strtoul(buffer, &end, 0); 22183cb4a0bbSKawai, Hidehiro if (*end == '\n') 22193cb4a0bbSKawai, Hidehiro end++; 22203cb4a0bbSKawai, Hidehiro if (end - buffer == 0) 22213cb4a0bbSKawai, Hidehiro goto out_no_task; 22223cb4a0bbSKawai, Hidehiro 22233cb4a0bbSKawai, Hidehiro ret = -ESRCH; 22243cb4a0bbSKawai, Hidehiro task = get_proc_task(file->f_dentry->d_inode); 22253cb4a0bbSKawai, Hidehiro if (!task) 22263cb4a0bbSKawai, Hidehiro goto out_no_task; 22273cb4a0bbSKawai, Hidehiro 22283cb4a0bbSKawai, Hidehiro ret = end - buffer; 22293cb4a0bbSKawai, Hidehiro mm = get_task_mm(task); 22303cb4a0bbSKawai, Hidehiro if (!mm) 22313cb4a0bbSKawai, Hidehiro goto out_no_mm; 22323cb4a0bbSKawai, Hidehiro 22333cb4a0bbSKawai, Hidehiro for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) { 22343cb4a0bbSKawai, Hidehiro if (val & mask) 22353cb4a0bbSKawai, Hidehiro set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags); 22363cb4a0bbSKawai, Hidehiro else 22373cb4a0bbSKawai, Hidehiro clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags); 22383cb4a0bbSKawai, Hidehiro } 22393cb4a0bbSKawai, Hidehiro 22403cb4a0bbSKawai, Hidehiro mmput(mm); 22413cb4a0bbSKawai, Hidehiro out_no_mm: 22423cb4a0bbSKawai, Hidehiro put_task_struct(task); 22433cb4a0bbSKawai, Hidehiro out_no_task: 22443cb4a0bbSKawai, Hidehiro return ret; 22453cb4a0bbSKawai, Hidehiro } 22463cb4a0bbSKawai, Hidehiro 22473cb4a0bbSKawai, Hidehiro static const struct file_operations proc_coredump_filter_operations = { 22483cb4a0bbSKawai, Hidehiro .read = proc_coredump_filter_read, 22493cb4a0bbSKawai, Hidehiro .write = proc_coredump_filter_write, 22503cb4a0bbSKawai, Hidehiro }; 22513cb4a0bbSKawai, Hidehiro #endif 22523cb4a0bbSKawai, Hidehiro 22531da177e4SLinus Torvalds /* 22541da177e4SLinus Torvalds * /proc/self: 22551da177e4SLinus Torvalds */ 22561da177e4SLinus Torvalds static int proc_self_readlink(struct dentry *dentry, char __user *buffer, 22571da177e4SLinus Torvalds int buflen) 22581da177e4SLinus Torvalds { 2259488e5bc4SEric W. Biederman struct pid_namespace *ns = dentry->d_sb->s_fs_info; 2260b55fcb22SAndrew Morton pid_t tgid = task_tgid_nr_ns(current, ns); 22618578cea7SEric W. Biederman char tmp[PROC_NUMBUF]; 2262b55fcb22SAndrew Morton if (!tgid) 2263488e5bc4SEric W. Biederman return -ENOENT; 2264b55fcb22SAndrew Morton sprintf(tmp, "%d", tgid); 22651da177e4SLinus Torvalds return vfs_readlink(dentry,buffer,buflen,tmp); 22661da177e4SLinus Torvalds } 22671da177e4SLinus Torvalds 2268008b150aSAl Viro static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd) 22691da177e4SLinus Torvalds { 2270488e5bc4SEric W. Biederman struct pid_namespace *ns = dentry->d_sb->s_fs_info; 2271b55fcb22SAndrew Morton pid_t tgid = task_tgid_nr_ns(current, ns); 22728578cea7SEric W. Biederman char tmp[PROC_NUMBUF]; 2273b55fcb22SAndrew Morton if (!tgid) 2274488e5bc4SEric W. Biederman return ERR_PTR(-ENOENT); 2275b55fcb22SAndrew Morton sprintf(tmp, "%d", task_tgid_nr_ns(current, ns)); 2276008b150aSAl Viro return ERR_PTR(vfs_follow_link(nd,tmp)); 22771da177e4SLinus Torvalds } 22781da177e4SLinus Torvalds 2279c5ef1c42SArjan van de Ven static const struct inode_operations proc_self_inode_operations = { 22801da177e4SLinus Torvalds .readlink = proc_self_readlink, 22811da177e4SLinus Torvalds .follow_link = proc_self_follow_link, 22821da177e4SLinus Torvalds }; 22831da177e4SLinus Torvalds 228428a6d671SEric W. Biederman /* 2285801199ceSEric W. Biederman * proc base 2286801199ceSEric W. Biederman * 2287801199ceSEric W. Biederman * These are the directory entries in the root directory of /proc 2288801199ceSEric W. Biederman * that properly belong to the /proc filesystem, as they describe 2289801199ceSEric W. Biederman * describe something that is process related. 2290801199ceSEric W. Biederman */ 2291c5141e6dSEric Dumazet static const struct pid_entry proc_base_stuff[] = { 229261a28784SEric W. Biederman NOD("self", S_IFLNK|S_IRWXUGO, 2293801199ceSEric W. Biederman &proc_self_inode_operations, NULL, {}), 2294801199ceSEric W. Biederman }; 2295801199ceSEric W. Biederman 2296801199ceSEric W. Biederman /* 2297801199ceSEric W. Biederman * Exceptional case: normally we are not allowed to unhash a busy 2298801199ceSEric W. Biederman * directory. In this case, however, we can do it - no aliasing problems 2299801199ceSEric W. Biederman * due to the way we treat inodes. 2300801199ceSEric W. Biederman */ 2301801199ceSEric W. Biederman static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd) 2302801199ceSEric W. Biederman { 2303801199ceSEric W. Biederman struct inode *inode = dentry->d_inode; 2304801199ceSEric W. Biederman struct task_struct *task = get_proc_task(inode); 2305801199ceSEric W. Biederman if (task) { 2306801199ceSEric W. Biederman put_task_struct(task); 2307801199ceSEric W. Biederman return 1; 2308801199ceSEric W. Biederman } 2309801199ceSEric W. Biederman d_drop(dentry); 2310801199ceSEric W. Biederman return 0; 2311801199ceSEric W. Biederman } 2312801199ceSEric W. Biederman 2313801199ceSEric W. Biederman static struct dentry_operations proc_base_dentry_operations = 2314801199ceSEric W. Biederman { 2315801199ceSEric W. Biederman .d_revalidate = proc_base_revalidate, 2316801199ceSEric W. Biederman .d_delete = pid_delete_dentry, 2317801199ceSEric W. Biederman }; 2318801199ceSEric W. Biederman 2319444ceed8SEric W. Biederman static struct dentry *proc_base_instantiate(struct inode *dir, 2320c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 2321801199ceSEric W. Biederman { 2322c5141e6dSEric Dumazet const struct pid_entry *p = ptr; 2323801199ceSEric W. Biederman struct inode *inode; 2324801199ceSEric W. Biederman struct proc_inode *ei; 2325444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-EINVAL); 2326801199ceSEric W. Biederman 2327801199ceSEric W. Biederman /* Allocate the inode */ 2328801199ceSEric W. Biederman error = ERR_PTR(-ENOMEM); 2329801199ceSEric W. Biederman inode = new_inode(dir->i_sb); 2330801199ceSEric W. Biederman if (!inode) 2331801199ceSEric W. Biederman goto out; 2332801199ceSEric W. Biederman 2333801199ceSEric W. Biederman /* Initialize the inode */ 2334801199ceSEric W. Biederman ei = PROC_I(inode); 2335801199ceSEric W. Biederman inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; 2336801199ceSEric W. Biederman 2337801199ceSEric W. Biederman /* 2338801199ceSEric W. Biederman * grab the reference to the task. 2339801199ceSEric W. Biederman */ 23401a657f78SOleg Nesterov ei->pid = get_task_pid(task, PIDTYPE_PID); 2341801199ceSEric W. Biederman if (!ei->pid) 2342801199ceSEric W. Biederman goto out_iput; 2343801199ceSEric W. Biederman 2344801199ceSEric W. Biederman inode->i_uid = 0; 2345801199ceSEric W. Biederman inode->i_gid = 0; 2346801199ceSEric W. Biederman inode->i_mode = p->mode; 2347801199ceSEric W. Biederman if (S_ISDIR(inode->i_mode)) 2348801199ceSEric W. Biederman inode->i_nlink = 2; 2349801199ceSEric W. Biederman if (S_ISLNK(inode->i_mode)) 2350801199ceSEric W. Biederman inode->i_size = 64; 2351801199ceSEric W. Biederman if (p->iop) 2352801199ceSEric W. Biederman inode->i_op = p->iop; 2353801199ceSEric W. Biederman if (p->fop) 2354801199ceSEric W. Biederman inode->i_fop = p->fop; 2355801199ceSEric W. Biederman ei->op = p->op; 2356801199ceSEric W. Biederman dentry->d_op = &proc_base_dentry_operations; 2357801199ceSEric W. Biederman d_add(dentry, inode); 2358801199ceSEric W. Biederman error = NULL; 2359801199ceSEric W. Biederman out: 2360801199ceSEric W. Biederman return error; 2361801199ceSEric W. Biederman out_iput: 2362801199ceSEric W. Biederman iput(inode); 2363801199ceSEric W. Biederman goto out; 2364801199ceSEric W. Biederman } 2365801199ceSEric W. Biederman 2366444ceed8SEric W. Biederman static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry) 2367444ceed8SEric W. Biederman { 2368444ceed8SEric W. Biederman struct dentry *error; 2369444ceed8SEric W. Biederman struct task_struct *task = get_proc_task(dir); 2370c5141e6dSEric Dumazet const struct pid_entry *p, *last; 2371444ceed8SEric W. Biederman 2372444ceed8SEric W. Biederman error = ERR_PTR(-ENOENT); 2373444ceed8SEric W. Biederman 2374444ceed8SEric W. Biederman if (!task) 2375444ceed8SEric W. Biederman goto out_no_task; 2376444ceed8SEric W. Biederman 2377444ceed8SEric W. Biederman /* Lookup the directory entry */ 23787bcd6b0eSEric W. Biederman last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1]; 23797bcd6b0eSEric W. Biederman for (p = proc_base_stuff; p <= last; p++) { 2380444ceed8SEric W. Biederman if (p->len != dentry->d_name.len) 2381444ceed8SEric W. Biederman continue; 2382444ceed8SEric W. Biederman if (!memcmp(dentry->d_name.name, p->name, p->len)) 2383444ceed8SEric W. Biederman break; 2384444ceed8SEric W. Biederman } 23857bcd6b0eSEric W. Biederman if (p > last) 2386444ceed8SEric W. Biederman goto out; 2387444ceed8SEric W. Biederman 2388444ceed8SEric W. Biederman error = proc_base_instantiate(dir, dentry, task, p); 2389444ceed8SEric W. Biederman 2390444ceed8SEric W. Biederman out: 2391444ceed8SEric W. Biederman put_task_struct(task); 2392444ceed8SEric W. Biederman out_no_task: 2393444ceed8SEric W. Biederman return error; 2394444ceed8SEric W. Biederman } 2395444ceed8SEric W. Biederman 2396c5141e6dSEric Dumazet static int proc_base_fill_cache(struct file *filp, void *dirent, 2397c5141e6dSEric Dumazet filldir_t filldir, struct task_struct *task, const struct pid_entry *p) 239861a28784SEric W. Biederman { 239961a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, p->name, p->len, 240061a28784SEric W. Biederman proc_base_instantiate, task, p); 240161a28784SEric W. Biederman } 240261a28784SEric W. Biederman 2403aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING 2404297c5d92SAndrea Righi static int do_io_accounting(struct task_struct *task, char *buffer, int whole) 2405aba76fdbSAndrew Morton { 2406*5995477aSAndrea Righi struct proc_io_accounting acct = task->ioac; 2407297c5d92SAndrea Righi unsigned long flags; 2408297c5d92SAndrea Righi 2409*5995477aSAndrea Righi if (whole && lock_task_sighand(task, &flags)) { 2410b2d002dbSAndrea Righi struct task_struct *t = task; 2411297c5d92SAndrea Righi 2412*5995477aSAndrea Righi task_io_accounting_add(&acct, &task->signal->ioac); 2413*5995477aSAndrea Righi while_each_thread(task, t) 2414*5995477aSAndrea Righi task_io_accounting_add(&acct, &t->ioac); 2415297c5d92SAndrea Righi 2416297c5d92SAndrea Righi unlock_task_sighand(task, &flags); 2417297c5d92SAndrea Righi } 2418aba76fdbSAndrew Morton return sprintf(buffer, 2419aba76fdbSAndrew Morton "rchar: %llu\n" 2420aba76fdbSAndrew Morton "wchar: %llu\n" 2421aba76fdbSAndrew Morton "syscr: %llu\n" 2422aba76fdbSAndrew Morton "syscw: %llu\n" 2423aba76fdbSAndrew Morton "read_bytes: %llu\n" 2424aba76fdbSAndrew Morton "write_bytes: %llu\n" 2425aba76fdbSAndrew Morton "cancelled_write_bytes: %llu\n", 2426*5995477aSAndrea Righi acct.chr.rchar, acct.chr.wchar, 2427*5995477aSAndrea Righi acct.chr.syscr, acct.chr.syscw, 2428*5995477aSAndrea Righi acct.blk.read_bytes, acct.blk.write_bytes, 2429*5995477aSAndrea Righi acct.blk.cancelled_write_bytes); 2430aba76fdbSAndrew Morton } 2431297c5d92SAndrea Righi 2432297c5d92SAndrea Righi static int proc_tid_io_accounting(struct task_struct *task, char *buffer) 2433297c5d92SAndrea Righi { 2434297c5d92SAndrea Righi return do_io_accounting(task, buffer, 0); 2435297c5d92SAndrea Righi } 2436297c5d92SAndrea Righi 2437297c5d92SAndrea Righi static int proc_tgid_io_accounting(struct task_struct *task, char *buffer) 2438297c5d92SAndrea Righi { 2439297c5d92SAndrea Righi return do_io_accounting(task, buffer, 1); 2440297c5d92SAndrea Righi } 2441297c5d92SAndrea Righi #endif /* CONFIG_TASK_IO_ACCOUNTING */ 2442aba76fdbSAndrew Morton 2443801199ceSEric W. Biederman /* 244428a6d671SEric W. Biederman * Thread groups 244528a6d671SEric W. Biederman */ 244600977a59SArjan van de Ven static const struct file_operations proc_task_operations; 2447c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations; 244820cdc894SEric W. Biederman 2449c5141e6dSEric Dumazet static const struct pid_entry tgid_base_stuff[] = { 245061a28784SEric W. Biederman DIR("task", S_IRUGO|S_IXUGO, task), 245161a28784SEric W. Biederman DIR("fd", S_IRUSR|S_IXUSR, fd), 245227932742SMiklos Szeredi DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo), 2453b2211a36SAndrew Morton #ifdef CONFIG_NET 24544f42c288SAndre Noll DIR("net", S_IRUGO|S_IXUGO, net), 2455b2211a36SAndrew Morton #endif 2456315e28c8SJames Pearson REG("environ", S_IRUSR, environ), 245761a28784SEric W. Biederman INF("auxv", S_IRUSR, pid_auxv), 2458df5f8314SEric W. Biederman ONE("status", S_IRUGO, pid_status), 2459d85f50d5SNeil Horman INF("limits", S_IRUSR, pid_limits), 246043ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG 246143ae34cbSIngo Molnar REG("sched", S_IRUGO|S_IWUSR, pid_sched), 246243ae34cbSIngo Molnar #endif 2463ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK 2464ebcb6734SRoland McGrath INF("syscall", S_IRUSR, pid_syscall), 2465ebcb6734SRoland McGrath #endif 246661a28784SEric W. Biederman INF("cmdline", S_IRUGO, pid_cmdline), 2467ee992744SEric W. Biederman ONE("stat", S_IRUGO, tgid_stat), 2468a56d3fc7SEric W. Biederman ONE("statm", S_IRUGO, pid_statm), 246961a28784SEric W. Biederman REG("maps", S_IRUGO, maps), 247028a6d671SEric W. Biederman #ifdef CONFIG_NUMA 247161a28784SEric W. Biederman REG("numa_maps", S_IRUGO, numa_maps), 247228a6d671SEric W. Biederman #endif 247361a28784SEric W. Biederman REG("mem", S_IRUSR|S_IWUSR, mem), 247461a28784SEric W. Biederman LNK("cwd", cwd), 247561a28784SEric W. Biederman LNK("root", root), 247661a28784SEric W. Biederman LNK("exe", exe), 247761a28784SEric W. Biederman REG("mounts", S_IRUGO, mounts), 24782d4d4864SRam Pai REG("mountinfo", S_IRUGO, mountinfo), 247961a28784SEric W. Biederman REG("mountstats", S_IRUSR, mountstats), 24801e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR 2481b813e931SDavid Rientjes REG("clear_refs", S_IWUSR, clear_refs), 248261a28784SEric W. Biederman REG("smaps", S_IRUGO, smaps), 248385863e47SMatt Mackall REG("pagemap", S_IRUSR, pagemap), 248428a6d671SEric W. Biederman #endif 248528a6d671SEric W. Biederman #ifdef CONFIG_SECURITY 248672d9dcfcSEric W. Biederman DIR("attr", S_IRUGO|S_IXUGO, attr_dir), 248728a6d671SEric W. Biederman #endif 248828a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS 248961a28784SEric W. Biederman INF("wchan", S_IRUGO, pid_wchan), 249028a6d671SEric W. Biederman #endif 249128a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS 249261a28784SEric W. Biederman INF("schedstat", S_IRUGO, pid_schedstat), 249328a6d671SEric W. Biederman #endif 24949745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP 24959745512cSArjan van de Ven REG("latency", S_IRUGO, lstats), 24969745512cSArjan van de Ven #endif 24978793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET 249861a28784SEric W. Biederman REG("cpuset", S_IRUGO, cpuset), 249928a6d671SEric W. Biederman #endif 2500a424316cSPaul Menage #ifdef CONFIG_CGROUPS 2501a424316cSPaul Menage REG("cgroup", S_IRUGO, cgroup), 2502a424316cSPaul Menage #endif 250361a28784SEric W. Biederman INF("oom_score", S_IRUGO, oom_score), 250461a28784SEric W. Biederman REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust), 250528a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL 250661a28784SEric W. Biederman REG("loginuid", S_IWUSR|S_IRUGO, loginuid), 25076ee65046SSteve Grubb REG("sessionid", S_IRUGO, sessionid), 250828a6d671SEric W. Biederman #endif 2509f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION 2510f4f154fdSAkinobu Mita REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject), 2511f4f154fdSAkinobu Mita #endif 25123cb4a0bbSKawai, Hidehiro #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE) 25133cb4a0bbSKawai, Hidehiro REG("coredump_filter", S_IRUGO|S_IWUSR, coredump_filter), 25143cb4a0bbSKawai, Hidehiro #endif 2515aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING 2516297c5d92SAndrea Righi INF("io", S_IRUGO, tgid_io_accounting), 2517aba76fdbSAndrew Morton #endif 251828a6d671SEric W. Biederman }; 251928a6d671SEric W. Biederman 252028a6d671SEric W. Biederman static int proc_tgid_base_readdir(struct file * filp, 252128a6d671SEric W. Biederman void * dirent, filldir_t filldir) 252228a6d671SEric W. Biederman { 252328a6d671SEric W. Biederman return proc_pident_readdir(filp,dirent,filldir, 252428a6d671SEric W. Biederman tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff)); 252528a6d671SEric W. Biederman } 252628a6d671SEric W. Biederman 252700977a59SArjan van de Ven static const struct file_operations proc_tgid_base_operations = { 252828a6d671SEric W. Biederman .read = generic_read_dir, 252928a6d671SEric W. Biederman .readdir = proc_tgid_base_readdir, 253028a6d671SEric W. Biederman }; 253128a6d671SEric W. Biederman 253228a6d671SEric W. Biederman static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){ 25337bcd6b0eSEric W. Biederman return proc_pident_lookup(dir, dentry, 25347bcd6b0eSEric W. Biederman tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff)); 253528a6d671SEric W. Biederman } 253628a6d671SEric W. Biederman 2537c5ef1c42SArjan van de Ven static const struct inode_operations proc_tgid_base_inode_operations = { 253828a6d671SEric W. Biederman .lookup = proc_tgid_base_lookup, 253928a6d671SEric W. Biederman .getattr = pid_getattr, 254028a6d671SEric W. Biederman .setattr = proc_setattr, 254128a6d671SEric W. Biederman }; 254228a6d671SEric W. Biederman 254360347f67SPavel Emelyanov static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid) 25441da177e4SLinus Torvalds { 254548e6484dSEric W. Biederman struct dentry *dentry, *leader, *dir; 25468578cea7SEric W. Biederman char buf[PROC_NUMBUF]; 254748e6484dSEric W. Biederman struct qstr name; 25481da177e4SLinus Torvalds 254948e6484dSEric W. Biederman name.name = buf; 255060347f67SPavel Emelyanov name.len = snprintf(buf, sizeof(buf), "%d", pid); 255160347f67SPavel Emelyanov dentry = d_hash_and_lookup(mnt->mnt_root, &name); 255248e6484dSEric W. Biederman if (dentry) { 25537766755aSAndrea Arcangeli if (!(current->flags & PF_EXITING)) 255448e6484dSEric W. Biederman shrink_dcache_parent(dentry); 255548e6484dSEric W. Biederman d_drop(dentry); 255648e6484dSEric W. Biederman dput(dentry); 25571da177e4SLinus Torvalds } 25581da177e4SLinus Torvalds 255960347f67SPavel Emelyanov if (tgid == 0) 256048e6484dSEric W. Biederman goto out; 25611da177e4SLinus Torvalds 256248e6484dSEric W. Biederman name.name = buf; 256360347f67SPavel Emelyanov name.len = snprintf(buf, sizeof(buf), "%d", tgid); 256460347f67SPavel Emelyanov leader = d_hash_and_lookup(mnt->mnt_root, &name); 256548e6484dSEric W. Biederman if (!leader) 256648e6484dSEric W. Biederman goto out; 256748e6484dSEric W. Biederman 256848e6484dSEric W. Biederman name.name = "task"; 256948e6484dSEric W. Biederman name.len = strlen(name.name); 257048e6484dSEric W. Biederman dir = d_hash_and_lookup(leader, &name); 257148e6484dSEric W. Biederman if (!dir) 257248e6484dSEric W. Biederman goto out_put_leader; 257348e6484dSEric W. Biederman 257448e6484dSEric W. Biederman name.name = buf; 257560347f67SPavel Emelyanov name.len = snprintf(buf, sizeof(buf), "%d", pid); 257648e6484dSEric W. Biederman dentry = d_hash_and_lookup(dir, &name); 257748e6484dSEric W. Biederman if (dentry) { 257848e6484dSEric W. Biederman shrink_dcache_parent(dentry); 257948e6484dSEric W. Biederman d_drop(dentry); 258048e6484dSEric W. Biederman dput(dentry); 25811da177e4SLinus Torvalds } 258248e6484dSEric W. Biederman 258348e6484dSEric W. Biederman dput(dir); 258448e6484dSEric W. Biederman out_put_leader: 258548e6484dSEric W. Biederman dput(leader); 258648e6484dSEric W. Biederman out: 258748e6484dSEric W. Biederman return; 25881da177e4SLinus Torvalds } 25891da177e4SLinus Torvalds 25900895e91dSRandy Dunlap /** 25910895e91dSRandy Dunlap * proc_flush_task - Remove dcache entries for @task from the /proc dcache. 25920895e91dSRandy Dunlap * @task: task that should be flushed. 25930895e91dSRandy Dunlap * 25940895e91dSRandy Dunlap * When flushing dentries from proc, one needs to flush them from global 259560347f67SPavel Emelyanov * proc (proc_mnt) and from all the namespaces' procs this task was seen 25960895e91dSRandy Dunlap * in. This call is supposed to do all of this job. 25970895e91dSRandy Dunlap * 25980895e91dSRandy Dunlap * Looks in the dcache for 25990895e91dSRandy Dunlap * /proc/@pid 26000895e91dSRandy Dunlap * /proc/@tgid/task/@pid 26010895e91dSRandy Dunlap * if either directory is present flushes it and all of it'ts children 26020895e91dSRandy Dunlap * from the dcache. 26030895e91dSRandy Dunlap * 26040895e91dSRandy Dunlap * It is safe and reasonable to cache /proc entries for a task until 26050895e91dSRandy Dunlap * that task exits. After that they just clog up the dcache with 26060895e91dSRandy Dunlap * useless entries, possibly causing useful dcache entries to be 26070895e91dSRandy Dunlap * flushed instead. This routine is proved to flush those useless 26080895e91dSRandy Dunlap * dcache entries at process exit time. 26090895e91dSRandy Dunlap * 26100895e91dSRandy Dunlap * NOTE: This routine is just an optimization so it does not guarantee 26110895e91dSRandy Dunlap * that no dcache entries will exist at process exit time it 26120895e91dSRandy Dunlap * just makes it very unlikely that any will persist. 261360347f67SPavel Emelyanov */ 261460347f67SPavel Emelyanov 261560347f67SPavel Emelyanov void proc_flush_task(struct task_struct *task) 261660347f67SPavel Emelyanov { 26179fcc2d15SEric W. Biederman int i; 26189fcc2d15SEric W. Biederman struct pid *pid, *tgid = NULL; 2619130f77ecSPavel Emelyanov struct upid *upid; 2620130f77ecSPavel Emelyanov 2621130f77ecSPavel Emelyanov pid = task_pid(task); 26229fcc2d15SEric W. Biederman if (thread_group_leader(task)) 2623130f77ecSPavel Emelyanov tgid = task_tgid(task); 26249fcc2d15SEric W. Biederman 26259fcc2d15SEric W. Biederman for (i = 0; i <= pid->level; i++) { 2626130f77ecSPavel Emelyanov upid = &pid->numbers[i]; 2627130f77ecSPavel Emelyanov proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr, 26289fcc2d15SEric W. Biederman tgid ? tgid->numbers[i].nr : 0); 2629130f77ecSPavel Emelyanov } 26306f4e6433SPavel Emelyanov 26316f4e6433SPavel Emelyanov upid = &pid->numbers[pid->level]; 26326f4e6433SPavel Emelyanov if (upid->nr == 1) 26336f4e6433SPavel Emelyanov pid_ns_release_proc(upid->ns); 263460347f67SPavel Emelyanov } 263560347f67SPavel Emelyanov 26369711ef99SAdrian Bunk static struct dentry *proc_pid_instantiate(struct inode *dir, 26379711ef99SAdrian Bunk struct dentry * dentry, 2638c5141e6dSEric Dumazet struct task_struct *task, const void *ptr) 2639444ceed8SEric W. Biederman { 2640444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-ENOENT); 2641444ceed8SEric W. Biederman struct inode *inode; 2642444ceed8SEric W. Biederman 264361a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 2644444ceed8SEric W. Biederman if (!inode) 2645444ceed8SEric W. Biederman goto out; 2646444ceed8SEric W. Biederman 2647444ceed8SEric W. Biederman inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO; 2648444ceed8SEric W. Biederman inode->i_op = &proc_tgid_base_inode_operations; 2649444ceed8SEric W. Biederman inode->i_fop = &proc_tgid_base_operations; 2650444ceed8SEric W. Biederman inode->i_flags|=S_IMMUTABLE; 2651aed54175SVegard Nossum 2652aed54175SVegard Nossum inode->i_nlink = 2 + pid_entry_count_dirs(tgid_base_stuff, 2653aed54175SVegard Nossum ARRAY_SIZE(tgid_base_stuff)); 2654444ceed8SEric W. Biederman 2655444ceed8SEric W. Biederman dentry->d_op = &pid_dentry_operations; 2656444ceed8SEric W. Biederman 2657444ceed8SEric W. Biederman d_add(dentry, inode); 2658444ceed8SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 2659444ceed8SEric W. Biederman if (pid_revalidate(dentry, NULL)) 2660444ceed8SEric W. Biederman error = NULL; 2661444ceed8SEric W. Biederman out: 2662444ceed8SEric W. Biederman return error; 2663444ceed8SEric W. Biederman } 2664444ceed8SEric W. Biederman 26651da177e4SLinus Torvalds struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd) 26661da177e4SLinus Torvalds { 2667cd6a3ce9SEric W. Biederman struct dentry *result = ERR_PTR(-ENOENT); 26681da177e4SLinus Torvalds struct task_struct *task; 26691da177e4SLinus Torvalds unsigned tgid; 2670b488893aSPavel Emelyanov struct pid_namespace *ns; 26711da177e4SLinus Torvalds 2672801199ceSEric W. Biederman result = proc_base_lookup(dir, dentry); 2673801199ceSEric W. Biederman if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT) 2674801199ceSEric W. Biederman goto out; 2675801199ceSEric W. Biederman 26761da177e4SLinus Torvalds tgid = name_to_int(dentry); 26771da177e4SLinus Torvalds if (tgid == ~0U) 26781da177e4SLinus Torvalds goto out; 26791da177e4SLinus Torvalds 2680b488893aSPavel Emelyanov ns = dentry->d_sb->s_fs_info; 2681de758734SEric W. Biederman rcu_read_lock(); 2682b488893aSPavel Emelyanov task = find_task_by_pid_ns(tgid, ns); 26831da177e4SLinus Torvalds if (task) 26841da177e4SLinus Torvalds get_task_struct(task); 2685de758734SEric W. Biederman rcu_read_unlock(); 26861da177e4SLinus Torvalds if (!task) 26871da177e4SLinus Torvalds goto out; 26881da177e4SLinus Torvalds 2689444ceed8SEric W. Biederman result = proc_pid_instantiate(dir, dentry, task, NULL); 269048e6484dSEric W. Biederman put_task_struct(task); 26911da177e4SLinus Torvalds out: 2692cd6a3ce9SEric W. Biederman return result; 26931da177e4SLinus Torvalds } 26941da177e4SLinus Torvalds 26951da177e4SLinus Torvalds /* 26960804ef4bSEric W. Biederman * Find the first task with tgid >= tgid 26970bc58a91SEric W. Biederman * 26981da177e4SLinus Torvalds */ 269919fd4bb2SEric W. Biederman struct tgid_iter { 270019fd4bb2SEric W. Biederman unsigned int tgid; 27010804ef4bSEric W. Biederman struct task_struct *task; 270219fd4bb2SEric W. Biederman }; 270319fd4bb2SEric W. Biederman static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter) 270419fd4bb2SEric W. Biederman { 27050804ef4bSEric W. Biederman struct pid *pid; 27061da177e4SLinus Torvalds 270719fd4bb2SEric W. Biederman if (iter.task) 270819fd4bb2SEric W. Biederman put_task_struct(iter.task); 27090804ef4bSEric W. Biederman rcu_read_lock(); 27100804ef4bSEric W. Biederman retry: 271119fd4bb2SEric W. Biederman iter.task = NULL; 271219fd4bb2SEric W. Biederman pid = find_ge_pid(iter.tgid, ns); 27130804ef4bSEric W. Biederman if (pid) { 271419fd4bb2SEric W. Biederman iter.tgid = pid_nr_ns(pid, ns); 271519fd4bb2SEric W. Biederman iter.task = pid_task(pid, PIDTYPE_PID); 27160804ef4bSEric W. Biederman /* What we to know is if the pid we have find is the 27170804ef4bSEric W. Biederman * pid of a thread_group_leader. Testing for task 27180804ef4bSEric W. Biederman * being a thread_group_leader is the obvious thing 27190804ef4bSEric W. Biederman * todo but there is a window when it fails, due to 27200804ef4bSEric W. Biederman * the pid transfer logic in de_thread. 27210804ef4bSEric W. Biederman * 27220804ef4bSEric W. Biederman * So we perform the straight forward test of seeing 27230804ef4bSEric W. Biederman * if the pid we have found is the pid of a thread 27240804ef4bSEric W. Biederman * group leader, and don't worry if the task we have 27250804ef4bSEric W. Biederman * found doesn't happen to be a thread group leader. 27260804ef4bSEric W. Biederman * As we don't care in the case of readdir. 27270bc58a91SEric W. Biederman */ 272819fd4bb2SEric W. Biederman if (!iter.task || !has_group_leader_pid(iter.task)) { 272919fd4bb2SEric W. Biederman iter.tgid += 1; 27300804ef4bSEric W. Biederman goto retry; 273119fd4bb2SEric W. Biederman } 273219fd4bb2SEric W. Biederman get_task_struct(iter.task); 27331da177e4SLinus Torvalds } 2734454cc105SEric W. Biederman rcu_read_unlock(); 273519fd4bb2SEric W. Biederman return iter; 27361da177e4SLinus Torvalds } 27371da177e4SLinus Torvalds 27387bcd6b0eSEric W. Biederman #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff)) 27391da177e4SLinus Torvalds 274061a28784SEric W. Biederman static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir, 274119fd4bb2SEric W. Biederman struct tgid_iter iter) 274261a28784SEric W. Biederman { 274361a28784SEric W. Biederman char name[PROC_NUMBUF]; 274419fd4bb2SEric W. Biederman int len = snprintf(name, sizeof(name), "%d", iter.tgid); 274561a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, name, len, 274619fd4bb2SEric W. Biederman proc_pid_instantiate, iter.task, NULL); 274761a28784SEric W. Biederman } 274861a28784SEric W. Biederman 27491da177e4SLinus Torvalds /* for the /proc/ directory itself, after non-process stuff has been done */ 27501da177e4SLinus Torvalds int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir) 27511da177e4SLinus Torvalds { 27521da177e4SLinus Torvalds unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY; 27532fddfeefSJosef "Jeff" Sipek struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode); 275419fd4bb2SEric W. Biederman struct tgid_iter iter; 2755b488893aSPavel Emelyanov struct pid_namespace *ns; 27561da177e4SLinus Torvalds 275761a28784SEric W. Biederman if (!reaper) 275861a28784SEric W. Biederman goto out_no_task; 275961a28784SEric W. Biederman 27607bcd6b0eSEric W. Biederman for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) { 2761c5141e6dSEric Dumazet const struct pid_entry *p = &proc_base_stuff[nr]; 276261a28784SEric W. Biederman if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0) 2763801199ceSEric W. Biederman goto out; 27641da177e4SLinus Torvalds } 27651da177e4SLinus Torvalds 2766b488893aSPavel Emelyanov ns = filp->f_dentry->d_sb->s_fs_info; 276719fd4bb2SEric W. Biederman iter.task = NULL; 276819fd4bb2SEric W. Biederman iter.tgid = filp->f_pos - TGID_OFFSET; 276919fd4bb2SEric W. Biederman for (iter = next_tgid(ns, iter); 277019fd4bb2SEric W. Biederman iter.task; 277119fd4bb2SEric W. Biederman iter.tgid += 1, iter = next_tgid(ns, iter)) { 277219fd4bb2SEric W. Biederman filp->f_pos = iter.tgid + TGID_OFFSET; 277319fd4bb2SEric W. Biederman if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) { 277419fd4bb2SEric W. Biederman put_task_struct(iter.task); 27750804ef4bSEric W. Biederman goto out; 27761da177e4SLinus Torvalds } 27771da177e4SLinus Torvalds } 27780804ef4bSEric W. Biederman filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET; 27790804ef4bSEric W. Biederman out: 278061a28784SEric W. Biederman put_task_struct(reaper); 278161a28784SEric W. Biederman out_no_task: 27821da177e4SLinus Torvalds return 0; 27831da177e4SLinus Torvalds } 27841da177e4SLinus Torvalds 27850bc58a91SEric W. Biederman /* 278628a6d671SEric W. Biederman * Tasks 278728a6d671SEric W. Biederman */ 2788c5141e6dSEric Dumazet static const struct pid_entry tid_base_stuff[] = { 278961a28784SEric W. Biederman DIR("fd", S_IRUSR|S_IXUSR, fd), 279027932742SMiklos Szeredi DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo), 2791315e28c8SJames Pearson REG("environ", S_IRUSR, environ), 279261a28784SEric W. Biederman INF("auxv", S_IRUSR, pid_auxv), 2793df5f8314SEric W. Biederman ONE("status", S_IRUGO, pid_status), 2794d85f50d5SNeil Horman INF("limits", S_IRUSR, pid_limits), 279543ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG 279643ae34cbSIngo Molnar REG("sched", S_IRUGO|S_IWUSR, pid_sched), 279743ae34cbSIngo Molnar #endif 2798ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK 2799ebcb6734SRoland McGrath INF("syscall", S_IRUSR, pid_syscall), 2800ebcb6734SRoland McGrath #endif 280161a28784SEric W. Biederman INF("cmdline", S_IRUGO, pid_cmdline), 2802ee992744SEric W. Biederman ONE("stat", S_IRUGO, tid_stat), 2803a56d3fc7SEric W. Biederman ONE("statm", S_IRUGO, pid_statm), 280461a28784SEric W. Biederman REG("maps", S_IRUGO, maps), 280528a6d671SEric W. Biederman #ifdef CONFIG_NUMA 280661a28784SEric W. Biederman REG("numa_maps", S_IRUGO, numa_maps), 280728a6d671SEric W. Biederman #endif 280861a28784SEric W. Biederman REG("mem", S_IRUSR|S_IWUSR, mem), 280961a28784SEric W. Biederman LNK("cwd", cwd), 281061a28784SEric W. Biederman LNK("root", root), 281161a28784SEric W. Biederman LNK("exe", exe), 281261a28784SEric W. Biederman REG("mounts", S_IRUGO, mounts), 28132d4d4864SRam Pai REG("mountinfo", S_IRUGO, mountinfo), 28141e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR 2815b813e931SDavid Rientjes REG("clear_refs", S_IWUSR, clear_refs), 281661a28784SEric W. Biederman REG("smaps", S_IRUGO, smaps), 281785863e47SMatt Mackall REG("pagemap", S_IRUSR, pagemap), 281828a6d671SEric W. Biederman #endif 281928a6d671SEric W. Biederman #ifdef CONFIG_SECURITY 282072d9dcfcSEric W. Biederman DIR("attr", S_IRUGO|S_IXUGO, attr_dir), 282128a6d671SEric W. Biederman #endif 282228a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS 282361a28784SEric W. Biederman INF("wchan", S_IRUGO, pid_wchan), 282428a6d671SEric W. Biederman #endif 282528a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS 282661a28784SEric W. Biederman INF("schedstat", S_IRUGO, pid_schedstat), 282728a6d671SEric W. Biederman #endif 28289745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP 28299745512cSArjan van de Ven REG("latency", S_IRUGO, lstats), 28309745512cSArjan van de Ven #endif 28318793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET 283261a28784SEric W. Biederman REG("cpuset", S_IRUGO, cpuset), 283328a6d671SEric W. Biederman #endif 2834a424316cSPaul Menage #ifdef CONFIG_CGROUPS 2835a424316cSPaul Menage REG("cgroup", S_IRUGO, cgroup), 2836a424316cSPaul Menage #endif 283761a28784SEric W. Biederman INF("oom_score", S_IRUGO, oom_score), 283861a28784SEric W. Biederman REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust), 283928a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL 284061a28784SEric W. Biederman REG("loginuid", S_IWUSR|S_IRUGO, loginuid), 28411e0bd755SEric Paris REG("sessionid", S_IRUSR, sessionid), 284228a6d671SEric W. Biederman #endif 2843f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION 2844f4f154fdSAkinobu Mita REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject), 2845f4f154fdSAkinobu Mita #endif 2846297c5d92SAndrea Righi #ifdef CONFIG_TASK_IO_ACCOUNTING 2847297c5d92SAndrea Righi INF("io", S_IRUGO, tid_io_accounting), 2848297c5d92SAndrea Righi #endif 284928a6d671SEric W. Biederman }; 285028a6d671SEric W. Biederman 285128a6d671SEric W. Biederman static int proc_tid_base_readdir(struct file * filp, 285228a6d671SEric W. Biederman void * dirent, filldir_t filldir) 285328a6d671SEric W. Biederman { 285428a6d671SEric W. Biederman return proc_pident_readdir(filp,dirent,filldir, 285528a6d671SEric W. Biederman tid_base_stuff,ARRAY_SIZE(tid_base_stuff)); 285628a6d671SEric W. Biederman } 285728a6d671SEric W. Biederman 285828a6d671SEric W. Biederman static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){ 28597bcd6b0eSEric W. Biederman return proc_pident_lookup(dir, dentry, 28607bcd6b0eSEric W. Biederman tid_base_stuff, ARRAY_SIZE(tid_base_stuff)); 286128a6d671SEric W. Biederman } 286228a6d671SEric W. Biederman 286300977a59SArjan van de Ven static const struct file_operations proc_tid_base_operations = { 286428a6d671SEric W. Biederman .read = generic_read_dir, 286528a6d671SEric W. Biederman .readdir = proc_tid_base_readdir, 286628a6d671SEric W. Biederman }; 286728a6d671SEric W. Biederman 2868c5ef1c42SArjan van de Ven static const struct inode_operations proc_tid_base_inode_operations = { 286928a6d671SEric W. Biederman .lookup = proc_tid_base_lookup, 287028a6d671SEric W. Biederman .getattr = pid_getattr, 287128a6d671SEric W. Biederman .setattr = proc_setattr, 287228a6d671SEric W. Biederman }; 287328a6d671SEric W. Biederman 2874444ceed8SEric W. Biederman static struct dentry *proc_task_instantiate(struct inode *dir, 2875c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 2876444ceed8SEric W. Biederman { 2877444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-ENOENT); 2878444ceed8SEric W. Biederman struct inode *inode; 287961a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 2880444ceed8SEric W. Biederman 2881444ceed8SEric W. Biederman if (!inode) 2882444ceed8SEric W. Biederman goto out; 2883444ceed8SEric W. Biederman inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO; 2884444ceed8SEric W. Biederman inode->i_op = &proc_tid_base_inode_operations; 2885444ceed8SEric W. Biederman inode->i_fop = &proc_tid_base_operations; 2886444ceed8SEric W. Biederman inode->i_flags|=S_IMMUTABLE; 2887aed54175SVegard Nossum 2888aed54175SVegard Nossum inode->i_nlink = 2 + pid_entry_count_dirs(tid_base_stuff, 2889aed54175SVegard Nossum ARRAY_SIZE(tid_base_stuff)); 2890444ceed8SEric W. Biederman 2891444ceed8SEric W. Biederman dentry->d_op = &pid_dentry_operations; 2892444ceed8SEric W. Biederman 2893444ceed8SEric W. Biederman d_add(dentry, inode); 2894444ceed8SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 2895444ceed8SEric W. Biederman if (pid_revalidate(dentry, NULL)) 2896444ceed8SEric W. Biederman error = NULL; 2897444ceed8SEric W. Biederman out: 2898444ceed8SEric W. Biederman return error; 2899444ceed8SEric W. Biederman } 2900444ceed8SEric W. Biederman 290128a6d671SEric W. Biederman static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd) 290228a6d671SEric W. Biederman { 290328a6d671SEric W. Biederman struct dentry *result = ERR_PTR(-ENOENT); 290428a6d671SEric W. Biederman struct task_struct *task; 290528a6d671SEric W. Biederman struct task_struct *leader = get_proc_task(dir); 290628a6d671SEric W. Biederman unsigned tid; 2907b488893aSPavel Emelyanov struct pid_namespace *ns; 290828a6d671SEric W. Biederman 290928a6d671SEric W. Biederman if (!leader) 291028a6d671SEric W. Biederman goto out_no_task; 291128a6d671SEric W. Biederman 291228a6d671SEric W. Biederman tid = name_to_int(dentry); 291328a6d671SEric W. Biederman if (tid == ~0U) 291428a6d671SEric W. Biederman goto out; 291528a6d671SEric W. Biederman 2916b488893aSPavel Emelyanov ns = dentry->d_sb->s_fs_info; 291728a6d671SEric W. Biederman rcu_read_lock(); 2918b488893aSPavel Emelyanov task = find_task_by_pid_ns(tid, ns); 291928a6d671SEric W. Biederman if (task) 292028a6d671SEric W. Biederman get_task_struct(task); 292128a6d671SEric W. Biederman rcu_read_unlock(); 292228a6d671SEric W. Biederman if (!task) 292328a6d671SEric W. Biederman goto out; 2924bac0abd6SPavel Emelyanov if (!same_thread_group(leader, task)) 292528a6d671SEric W. Biederman goto out_drop_task; 292628a6d671SEric W. Biederman 2927444ceed8SEric W. Biederman result = proc_task_instantiate(dir, dentry, task, NULL); 292828a6d671SEric W. Biederman out_drop_task: 292928a6d671SEric W. Biederman put_task_struct(task); 293028a6d671SEric W. Biederman out: 293128a6d671SEric W. Biederman put_task_struct(leader); 293228a6d671SEric W. Biederman out_no_task: 293328a6d671SEric W. Biederman return result; 293428a6d671SEric W. Biederman } 293528a6d671SEric W. Biederman 293628a6d671SEric W. Biederman /* 29370bc58a91SEric W. Biederman * Find the first tid of a thread group to return to user space. 29380bc58a91SEric W. Biederman * 29390bc58a91SEric W. Biederman * Usually this is just the thread group leader, but if the users 29400bc58a91SEric W. Biederman * buffer was too small or there was a seek into the middle of the 29410bc58a91SEric W. Biederman * directory we have more work todo. 29420bc58a91SEric W. Biederman * 29430bc58a91SEric W. Biederman * In the case of a short read we start with find_task_by_pid. 29440bc58a91SEric W. Biederman * 29450bc58a91SEric W. Biederman * In the case of a seek we start with the leader and walk nr 29460bc58a91SEric W. Biederman * threads past it. 29470bc58a91SEric W. Biederman */ 2948cc288738SEric W. Biederman static struct task_struct *first_tid(struct task_struct *leader, 2949b488893aSPavel Emelyanov int tid, int nr, struct pid_namespace *ns) 29500bc58a91SEric W. Biederman { 2951a872ff0cSOleg Nesterov struct task_struct *pos; 29520bc58a91SEric W. Biederman 2953cc288738SEric W. Biederman rcu_read_lock(); 29540bc58a91SEric W. Biederman /* Attempt to start with the pid of a thread */ 29550bc58a91SEric W. Biederman if (tid && (nr > 0)) { 2956b488893aSPavel Emelyanov pos = find_task_by_pid_ns(tid, ns); 2957a872ff0cSOleg Nesterov if (pos && (pos->group_leader == leader)) 2958a872ff0cSOleg Nesterov goto found; 29590bc58a91SEric W. Biederman } 29600bc58a91SEric W. Biederman 29610bc58a91SEric W. Biederman /* If nr exceeds the number of threads there is nothing todo */ 29620bc58a91SEric W. Biederman pos = NULL; 2963a872ff0cSOleg Nesterov if (nr && nr >= get_nr_threads(leader)) 2964a872ff0cSOleg Nesterov goto out; 2965a872ff0cSOleg Nesterov 2966a872ff0cSOleg Nesterov /* If we haven't found our starting place yet start 2967a872ff0cSOleg Nesterov * with the leader and walk nr threads forward. 2968a872ff0cSOleg Nesterov */ 2969a872ff0cSOleg Nesterov for (pos = leader; nr > 0; --nr) { 2970a872ff0cSOleg Nesterov pos = next_thread(pos); 2971a872ff0cSOleg Nesterov if (pos == leader) { 2972a872ff0cSOleg Nesterov pos = NULL; 2973a872ff0cSOleg Nesterov goto out; 2974a872ff0cSOleg Nesterov } 2975a872ff0cSOleg Nesterov } 2976a872ff0cSOleg Nesterov found: 2977a872ff0cSOleg Nesterov get_task_struct(pos); 2978a872ff0cSOleg Nesterov out: 2979cc288738SEric W. Biederman rcu_read_unlock(); 29800bc58a91SEric W. Biederman return pos; 29810bc58a91SEric W. Biederman } 29820bc58a91SEric W. Biederman 29830bc58a91SEric W. Biederman /* 29840bc58a91SEric W. Biederman * Find the next thread in the thread list. 29850bc58a91SEric W. Biederman * Return NULL if there is an error or no next thread. 29860bc58a91SEric W. Biederman * 29870bc58a91SEric W. Biederman * The reference to the input task_struct is released. 29880bc58a91SEric W. Biederman */ 29890bc58a91SEric W. Biederman static struct task_struct *next_tid(struct task_struct *start) 29900bc58a91SEric W. Biederman { 2991c1df7fb8SOleg Nesterov struct task_struct *pos = NULL; 2992cc288738SEric W. Biederman rcu_read_lock(); 2993c1df7fb8SOleg Nesterov if (pid_alive(start)) { 29940bc58a91SEric W. Biederman pos = next_thread(start); 2995c1df7fb8SOleg Nesterov if (thread_group_leader(pos)) 29960bc58a91SEric W. Biederman pos = NULL; 2997c1df7fb8SOleg Nesterov else 2998c1df7fb8SOleg Nesterov get_task_struct(pos); 2999c1df7fb8SOleg Nesterov } 3000cc288738SEric W. Biederman rcu_read_unlock(); 30010bc58a91SEric W. Biederman put_task_struct(start); 30020bc58a91SEric W. Biederman return pos; 30030bc58a91SEric W. Biederman } 30040bc58a91SEric W. Biederman 300561a28784SEric W. Biederman static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir, 300661a28784SEric W. Biederman struct task_struct *task, int tid) 300761a28784SEric W. Biederman { 300861a28784SEric W. Biederman char name[PROC_NUMBUF]; 300961a28784SEric W. Biederman int len = snprintf(name, sizeof(name), "%d", tid); 301061a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, name, len, 301161a28784SEric W. Biederman proc_task_instantiate, task, NULL); 301261a28784SEric W. Biederman } 301361a28784SEric W. Biederman 30141da177e4SLinus Torvalds /* for the /proc/TGID/task/ directories */ 30151da177e4SLinus Torvalds static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir) 30161da177e4SLinus Torvalds { 30172fddfeefSJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 30181da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 30197d895244SGuillaume Chazarain struct task_struct *leader = NULL; 30200bc58a91SEric W. Biederman struct task_struct *task; 30211da177e4SLinus Torvalds int retval = -ENOENT; 30221da177e4SLinus Torvalds ino_t ino; 30230bc58a91SEric W. Biederman int tid; 30241da177e4SLinus Torvalds unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */ 3025b488893aSPavel Emelyanov struct pid_namespace *ns; 30261da177e4SLinus Torvalds 30277d895244SGuillaume Chazarain task = get_proc_task(inode); 30287d895244SGuillaume Chazarain if (!task) 30297d895244SGuillaume Chazarain goto out_no_task; 30307d895244SGuillaume Chazarain rcu_read_lock(); 30317d895244SGuillaume Chazarain if (pid_alive(task)) { 30327d895244SGuillaume Chazarain leader = task->group_leader; 30337d895244SGuillaume Chazarain get_task_struct(leader); 30347d895244SGuillaume Chazarain } 30357d895244SGuillaume Chazarain rcu_read_unlock(); 30367d895244SGuillaume Chazarain put_task_struct(task); 303799f89551SEric W. Biederman if (!leader) 303899f89551SEric W. Biederman goto out_no_task; 30391da177e4SLinus Torvalds retval = 0; 30401da177e4SLinus Torvalds 30411da177e4SLinus Torvalds switch (pos) { 30421da177e4SLinus Torvalds case 0: 30431da177e4SLinus Torvalds ino = inode->i_ino; 30441da177e4SLinus Torvalds if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0) 30451da177e4SLinus Torvalds goto out; 30461da177e4SLinus Torvalds pos++; 30471da177e4SLinus Torvalds /* fall through */ 30481da177e4SLinus Torvalds case 1: 30491da177e4SLinus Torvalds ino = parent_ino(dentry); 30501da177e4SLinus Torvalds if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0) 30511da177e4SLinus Torvalds goto out; 30521da177e4SLinus Torvalds pos++; 30531da177e4SLinus Torvalds /* fall through */ 30541da177e4SLinus Torvalds } 30551da177e4SLinus Torvalds 30560bc58a91SEric W. Biederman /* f_version caches the tgid value that the last readdir call couldn't 30570bc58a91SEric W. Biederman * return. lseek aka telldir automagically resets f_version to 0. 30580bc58a91SEric W. Biederman */ 3059b488893aSPavel Emelyanov ns = filp->f_dentry->d_sb->s_fs_info; 30602b47c361SMathieu Desnoyers tid = (int)filp->f_version; 30610bc58a91SEric W. Biederman filp->f_version = 0; 3062b488893aSPavel Emelyanov for (task = first_tid(leader, tid, pos - 2, ns); 30630bc58a91SEric W. Biederman task; 30640bc58a91SEric W. Biederman task = next_tid(task), pos++) { 3065b488893aSPavel Emelyanov tid = task_pid_nr_ns(task, ns); 306661a28784SEric W. Biederman if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) { 30670bc58a91SEric W. Biederman /* returning this tgid failed, save it as the first 30680bc58a91SEric W. Biederman * pid for the next readir call */ 30692b47c361SMathieu Desnoyers filp->f_version = (u64)tid; 30700bc58a91SEric W. Biederman put_task_struct(task); 30711da177e4SLinus Torvalds break; 30720bc58a91SEric W. Biederman } 30731da177e4SLinus Torvalds } 30741da177e4SLinus Torvalds out: 30751da177e4SLinus Torvalds filp->f_pos = pos; 307699f89551SEric W. Biederman put_task_struct(leader); 307799f89551SEric W. Biederman out_no_task: 30781da177e4SLinus Torvalds return retval; 30791da177e4SLinus Torvalds } 30806e66b52bSEric W. Biederman 30816e66b52bSEric W. Biederman static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) 30826e66b52bSEric W. Biederman { 30836e66b52bSEric W. Biederman struct inode *inode = dentry->d_inode; 308499f89551SEric W. Biederman struct task_struct *p = get_proc_task(inode); 30856e66b52bSEric W. Biederman generic_fillattr(inode, stat); 30866e66b52bSEric W. Biederman 308799f89551SEric W. Biederman if (p) { 308899f89551SEric W. Biederman rcu_read_lock(); 308999f89551SEric W. Biederman stat->nlink += get_nr_threads(p); 309099f89551SEric W. Biederman rcu_read_unlock(); 309199f89551SEric W. Biederman put_task_struct(p); 30926e66b52bSEric W. Biederman } 30936e66b52bSEric W. Biederman 30946e66b52bSEric W. Biederman return 0; 30956e66b52bSEric W. Biederman } 309628a6d671SEric W. Biederman 3097c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations = { 309828a6d671SEric W. Biederman .lookup = proc_task_lookup, 309928a6d671SEric W. Biederman .getattr = proc_task_getattr, 310028a6d671SEric W. Biederman .setattr = proc_setattr, 310128a6d671SEric W. Biederman }; 310228a6d671SEric W. Biederman 310300977a59SArjan van de Ven static const struct file_operations proc_task_operations = { 310428a6d671SEric W. Biederman .read = generic_read_dir, 310528a6d671SEric W. Biederman .readdir = proc_task_readdir, 310628a6d671SEric W. Biederman }; 3107