11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/fs/proc/base.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 1991, 1992 Linus Torvalds 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * proc base directory handling functions 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * 1999, Al Viro. Rewritten. Now it covers the whole per-process part. 91da177e4SLinus Torvalds * Instead of using magical inumbers to determine the kind of object 101da177e4SLinus Torvalds * we allocate and fill in-core inodes upon lookup. They don't even 111da177e4SLinus Torvalds * go into icache. We cache the reference to task_struct upon lookup too. 121da177e4SLinus Torvalds * Eventually it should become a filesystem in its own. We don't use the 131da177e4SLinus Torvalds * rest of procfs anymore. 14e070ad49SMauricio Lin * 15e070ad49SMauricio Lin * 16e070ad49SMauricio Lin * Changelog: 17e070ad49SMauricio Lin * 17-Jan-2005 18e070ad49SMauricio Lin * Allan Bezerra 19e070ad49SMauricio Lin * Bruna Moreira <bruna.moreira@indt.org.br> 20e070ad49SMauricio Lin * Edjard Mota <edjard.mota@indt.org.br> 21e070ad49SMauricio Lin * Ilias Biris <ilias.biris@indt.org.br> 22e070ad49SMauricio Lin * Mauricio Lin <mauricio.lin@indt.org.br> 23e070ad49SMauricio Lin * 24e070ad49SMauricio Lin * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT 25e070ad49SMauricio Lin * 26e070ad49SMauricio Lin * A new process specific entry (smaps) included in /proc. It shows the 27e070ad49SMauricio Lin * size of rss for each memory area. The maps entry lacks information 28e070ad49SMauricio Lin * about physical memory size (rss) for each mapped file, i.e., 29e070ad49SMauricio Lin * rss information for executables and library files. 30e070ad49SMauricio Lin * This additional information is useful for any tools that need to know 31e070ad49SMauricio Lin * about physical memory consumption for a process specific library. 32e070ad49SMauricio Lin * 33e070ad49SMauricio Lin * Changelog: 34e070ad49SMauricio Lin * 21-Feb-2005 35e070ad49SMauricio Lin * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT 36e070ad49SMauricio Lin * Pud inclusion in the page table walking. 37e070ad49SMauricio Lin * 38e070ad49SMauricio Lin * ChangeLog: 39e070ad49SMauricio Lin * 10-Mar-2005 40e070ad49SMauricio Lin * 10LE Instituto Nokia de Tecnologia - INdT: 41e070ad49SMauricio Lin * A better way to walks through the page table as suggested by Hugh Dickins. 42e070ad49SMauricio Lin * 43e070ad49SMauricio Lin * Simo Piiroinen <simo.piiroinen@nokia.com>: 44e070ad49SMauricio Lin * Smaps information related to shared, private, clean and dirty pages. 45e070ad49SMauricio Lin * 46e070ad49SMauricio Lin * Paul Mundt <paul.mundt@nokia.com>: 47e070ad49SMauricio Lin * Overall revision about smaps. 481da177e4SLinus Torvalds */ 491da177e4SLinus Torvalds 501da177e4SLinus Torvalds #include <asm/uaccess.h> 511da177e4SLinus Torvalds 521da177e4SLinus Torvalds #include <linux/errno.h> 531da177e4SLinus Torvalds #include <linux/time.h> 541da177e4SLinus Torvalds #include <linux/proc_fs.h> 551da177e4SLinus Torvalds #include <linux/stat.h> 565995477aSAndrea Righi #include <linux/task_io_accounting_ops.h> 571da177e4SLinus Torvalds #include <linux/init.h> 5816f7e0feSRandy Dunlap #include <linux/capability.h> 591da177e4SLinus Torvalds #include <linux/file.h> 609f3acc31SAl Viro #include <linux/fdtable.h> 611da177e4SLinus Torvalds #include <linux/string.h> 621da177e4SLinus Torvalds #include <linux/seq_file.h> 631da177e4SLinus Torvalds #include <linux/namei.h> 646b3286edSKirill Korotaev #include <linux/mnt_namespace.h> 651da177e4SLinus Torvalds #include <linux/mm.h> 66a63d83f4SDavid Rientjes #include <linux/swap.h> 67b835996fSDipankar Sarma #include <linux/rcupdate.h> 681da177e4SLinus Torvalds #include <linux/kallsyms.h> 692ec220e2SKen Chen #include <linux/stacktrace.h> 70d85f50d5SNeil Horman #include <linux/resource.h> 715096add8SKees Cook #include <linux/module.h> 721da177e4SLinus Torvalds #include <linux/mount.h> 731da177e4SLinus Torvalds #include <linux/security.h> 741da177e4SLinus Torvalds #include <linux/ptrace.h> 750d094efeSRoland McGrath #include <linux/tracehook.h> 76a424316cSPaul Menage #include <linux/cgroup.h> 771da177e4SLinus Torvalds #include <linux/cpuset.h> 781da177e4SLinus Torvalds #include <linux/audit.h> 795addc5ddSAl Viro #include <linux/poll.h> 801651e14eSSerge E. Hallyn #include <linux/nsproxy.h> 818ac773b4SAlexey Dobriyan #include <linux/oom.h> 823cb4a0bbSKawai, Hidehiro #include <linux/elf.h> 8360347f67SPavel Emelyanov #include <linux/pid_namespace.h> 845ad4e53bSAl Viro #include <linux/fs_struct.h> 855a0e3ad6STejun Heo #include <linux/slab.h> 861da177e4SLinus Torvalds #include "internal.h" 871da177e4SLinus Torvalds 880f2fe20fSEric W. Biederman /* NOTE: 890f2fe20fSEric W. Biederman * Implementing inode permission operations in /proc is almost 900f2fe20fSEric W. Biederman * certainly an error. Permission checks need to happen during 910f2fe20fSEric W. Biederman * each system call not at open time. The reason is that most of 920f2fe20fSEric W. Biederman * what we wish to check for permissions in /proc varies at runtime. 930f2fe20fSEric W. Biederman * 940f2fe20fSEric W. Biederman * The classic example of a problem is opening file descriptors 950f2fe20fSEric W. Biederman * in /proc for a task before it execs a suid executable. 960f2fe20fSEric W. Biederman */ 970f2fe20fSEric W. Biederman 981da177e4SLinus Torvalds struct pid_entry { 991da177e4SLinus Torvalds char *name; 100c5141e6dSEric Dumazet int len; 1011da177e4SLinus Torvalds mode_t mode; 102c5ef1c42SArjan van de Ven const struct inode_operations *iop; 10300977a59SArjan van de Ven const struct file_operations *fop; 10420cdc894SEric W. Biederman union proc_op op; 1051da177e4SLinus Torvalds }; 1061da177e4SLinus Torvalds 10761a28784SEric W. Biederman #define NOD(NAME, MODE, IOP, FOP, OP) { \ 10820cdc894SEric W. Biederman .name = (NAME), \ 109c5141e6dSEric Dumazet .len = sizeof(NAME) - 1, \ 11020cdc894SEric W. Biederman .mode = MODE, \ 11120cdc894SEric W. Biederman .iop = IOP, \ 11220cdc894SEric W. Biederman .fop = FOP, \ 11320cdc894SEric W. Biederman .op = OP, \ 11420cdc894SEric W. Biederman } 11520cdc894SEric W. Biederman 116631f9c18SAlexey Dobriyan #define DIR(NAME, MODE, iops, fops) \ 117631f9c18SAlexey Dobriyan NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} ) 118631f9c18SAlexey Dobriyan #define LNK(NAME, get_link) \ 11961a28784SEric W. Biederman NOD(NAME, (S_IFLNK|S_IRWXUGO), \ 12020cdc894SEric W. Biederman &proc_pid_link_inode_operations, NULL, \ 121631f9c18SAlexey Dobriyan { .proc_get_link = get_link } ) 122631f9c18SAlexey Dobriyan #define REG(NAME, MODE, fops) \ 123631f9c18SAlexey Dobriyan NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {}) 124631f9c18SAlexey Dobriyan #define INF(NAME, MODE, read) \ 12561a28784SEric W. Biederman NOD(NAME, (S_IFREG|(MODE)), \ 12620cdc894SEric W. Biederman NULL, &proc_info_file_operations, \ 127631f9c18SAlexey Dobriyan { .proc_read = read } ) 128631f9c18SAlexey Dobriyan #define ONE(NAME, MODE, show) \ 129be614086SEric W. Biederman NOD(NAME, (S_IFREG|(MODE)), \ 130be614086SEric W. Biederman NULL, &proc_single_file_operations, \ 131631f9c18SAlexey Dobriyan { .proc_show = show } ) 1321da177e4SLinus Torvalds 133aed54175SVegard Nossum /* 134aed54175SVegard Nossum * Count the number of hardlinks for the pid_entry table, excluding the . 135aed54175SVegard Nossum * and .. links. 136aed54175SVegard Nossum */ 137aed54175SVegard Nossum static unsigned int pid_entry_count_dirs(const struct pid_entry *entries, 138aed54175SVegard Nossum unsigned int n) 139aed54175SVegard Nossum { 140aed54175SVegard Nossum unsigned int i; 141aed54175SVegard Nossum unsigned int count; 142aed54175SVegard Nossum 143aed54175SVegard Nossum count = 0; 144aed54175SVegard Nossum for (i = 0; i < n; ++i) { 145aed54175SVegard Nossum if (S_ISDIR(entries[i].mode)) 146aed54175SVegard Nossum ++count; 147aed54175SVegard Nossum } 148aed54175SVegard Nossum 149aed54175SVegard Nossum return count; 150aed54175SVegard Nossum } 151aed54175SVegard Nossum 152f7ad3c6bSMiklos Szeredi static int get_task_root(struct task_struct *task, struct path *root) 1531da177e4SLinus Torvalds { 1547c2c7d99SHugh Dickins int result = -ENOENT; 1557c2c7d99SHugh Dickins 1560494f6ecSMiklos Szeredi task_lock(task); 157f7ad3c6bSMiklos Szeredi if (task->fs) { 158f7ad3c6bSMiklos Szeredi get_fs_root(task->fs, root); 1597c2c7d99SHugh Dickins result = 0; 1607c2c7d99SHugh Dickins } 1610494f6ecSMiklos Szeredi task_unlock(task); 1627c2c7d99SHugh Dickins return result; 1630494f6ecSMiklos Szeredi } 1640494f6ecSMiklos Szeredi 1653dcd25f3SJan Blunck static int proc_cwd_link(struct inode *inode, struct path *path) 1660494f6ecSMiklos Szeredi { 16799f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 1680494f6ecSMiklos Szeredi int result = -ENOENT; 16999f89551SEric W. Biederman 17099f89551SEric W. Biederman if (task) { 171f7ad3c6bSMiklos Szeredi task_lock(task); 172f7ad3c6bSMiklos Szeredi if (task->fs) { 173f7ad3c6bSMiklos Szeredi get_fs_pwd(task->fs, path); 174f7ad3c6bSMiklos Szeredi result = 0; 175f7ad3c6bSMiklos Szeredi } 176f7ad3c6bSMiklos Szeredi task_unlock(task); 17799f89551SEric W. Biederman put_task_struct(task); 17899f89551SEric W. Biederman } 1791da177e4SLinus Torvalds return result; 1801da177e4SLinus Torvalds } 1811da177e4SLinus Torvalds 1823dcd25f3SJan Blunck static int proc_root_link(struct inode *inode, struct path *path) 1831da177e4SLinus Torvalds { 18499f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 1851da177e4SLinus Torvalds int result = -ENOENT; 18699f89551SEric W. Biederman 18799f89551SEric W. Biederman if (task) { 188f7ad3c6bSMiklos Szeredi result = get_task_root(task, path); 18999f89551SEric W. Biederman put_task_struct(task); 19099f89551SEric W. Biederman } 1911da177e4SLinus Torvalds return result; 1921da177e4SLinus Torvalds } 1931da177e4SLinus Torvalds 1948b0db9dbSStephen Wilson static struct mm_struct *__check_mem_permission(struct task_struct *task) 195638fa202SRoland McGrath { 1968b0db9dbSStephen Wilson struct mm_struct *mm; 1978b0db9dbSStephen Wilson 1988b0db9dbSStephen Wilson mm = get_task_mm(task); 1998b0db9dbSStephen Wilson if (!mm) 2008b0db9dbSStephen Wilson return ERR_PTR(-EINVAL); 2018b0db9dbSStephen Wilson 202638fa202SRoland McGrath /* 203638fa202SRoland McGrath * A task can always look at itself, in case it chooses 204638fa202SRoland McGrath * to use system calls instead of load instructions. 205638fa202SRoland McGrath */ 206638fa202SRoland McGrath if (task == current) 2078b0db9dbSStephen Wilson return mm; 208638fa202SRoland McGrath 209638fa202SRoland McGrath /* 210638fa202SRoland McGrath * If current is actively ptrace'ing, and would also be 211638fa202SRoland McGrath * permitted to freshly attach with ptrace now, permit it. 212638fa202SRoland McGrath */ 2130d094efeSRoland McGrath if (task_is_stopped_or_traced(task)) { 2140d094efeSRoland McGrath int match; 2150d094efeSRoland McGrath rcu_read_lock(); 2160d094efeSRoland McGrath match = (tracehook_tracer_task(task) == current); 2170d094efeSRoland McGrath rcu_read_unlock(); 2180d094efeSRoland McGrath if (match && ptrace_may_access(task, PTRACE_MODE_ATTACH)) 2198b0db9dbSStephen Wilson return mm; 2200d094efeSRoland McGrath } 221638fa202SRoland McGrath 222638fa202SRoland McGrath /* 223638fa202SRoland McGrath * No one else is allowed. 224638fa202SRoland McGrath */ 2258b0db9dbSStephen Wilson mmput(mm); 2268b0db9dbSStephen Wilson return ERR_PTR(-EPERM); 227638fa202SRoland McGrath } 2281da177e4SLinus Torvalds 22918f661bcSStephen Wilson /* 2308b0db9dbSStephen Wilson * If current may access user memory in @task return a reference to the 2318b0db9dbSStephen Wilson * corresponding mm, otherwise ERR_PTR. 23218f661bcSStephen Wilson */ 2338b0db9dbSStephen Wilson static struct mm_struct *check_mem_permission(struct task_struct *task) 23418f661bcSStephen Wilson { 2358b0db9dbSStephen Wilson struct mm_struct *mm; 23618f661bcSStephen Wilson int err; 23718f661bcSStephen Wilson 23818f661bcSStephen Wilson /* 23918f661bcSStephen Wilson * Avoid racing if task exec's as we might get a new mm but validate 24018f661bcSStephen Wilson * against old credentials. 24118f661bcSStephen Wilson */ 24218f661bcSStephen Wilson err = mutex_lock_killable(&task->signal->cred_guard_mutex); 24318f661bcSStephen Wilson if (err) 2448b0db9dbSStephen Wilson return ERR_PTR(err); 24518f661bcSStephen Wilson 2468b0db9dbSStephen Wilson mm = __check_mem_permission(task); 24718f661bcSStephen Wilson mutex_unlock(&task->signal->cred_guard_mutex); 24818f661bcSStephen Wilson 2498b0db9dbSStephen Wilson return mm; 2501da177e4SLinus Torvalds } 2511da177e4SLinus Torvalds 252831830b5SAl Viro struct mm_struct *mm_for_maps(struct task_struct *task) 253831830b5SAl Viro { 254704b836cSOleg Nesterov struct mm_struct *mm; 255ec6fd8a4SAl Viro int err; 25600f89d21SOleg Nesterov 257ec6fd8a4SAl Viro err = mutex_lock_killable(&task->signal->cred_guard_mutex); 258ec6fd8a4SAl Viro if (err) 259ec6fd8a4SAl Viro return ERR_PTR(err); 260704b836cSOleg Nesterov 261704b836cSOleg Nesterov mm = get_task_mm(task); 262704b836cSOleg Nesterov if (mm && mm != current->mm && 263704b836cSOleg Nesterov !ptrace_may_access(task, PTRACE_MODE_READ)) { 264831830b5SAl Viro mmput(mm); 265ec6fd8a4SAl Viro mm = ERR_PTR(-EACCES); 266831830b5SAl Viro } 2679b1bf12dSKOSAKI Motohiro mutex_unlock(&task->signal->cred_guard_mutex); 268704b836cSOleg Nesterov 26913f0feafSOleg Nesterov return mm; 27013f0feafSOleg Nesterov } 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 { 3112fadaef4SAl Viro struct mm_struct *mm = mm_for_maps(task); 3122fadaef4SAl Viro int res = PTR_ERR(mm); 3132fadaef4SAl Viro if (mm && !IS_ERR(mm)) { 3141da177e4SLinus Torvalds unsigned int nwords = 0; 315dfe6b7d9SHannes Eder do { 3161da177e4SLinus Torvalds nwords += 2; 317dfe6b7d9SHannes Eder } 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) 341f83ce3e6SJake Edge if (!ptrace_may_access(task, PTRACE_MODE_READ)) 342f83ce3e6SJake Edge return 0; 343f83ce3e6SJake Edge else 3441da177e4SLinus Torvalds return sprintf(buffer, "%lu", wchan); 3459d65cb4aSAlexey Dobriyan else 3469d65cb4aSAlexey Dobriyan return sprintf(buffer, "%s", symname); 3471da177e4SLinus Torvalds } 3481da177e4SLinus Torvalds #endif /* CONFIG_KALLSYMS */ 3491da177e4SLinus Torvalds 350a9712bc1SAl Viro static int lock_trace(struct task_struct *task) 351a9712bc1SAl Viro { 352a9712bc1SAl Viro int err = mutex_lock_killable(&task->signal->cred_guard_mutex); 353a9712bc1SAl Viro if (err) 354a9712bc1SAl Viro return err; 355a9712bc1SAl Viro if (!ptrace_may_access(task, PTRACE_MODE_ATTACH)) { 356a9712bc1SAl Viro mutex_unlock(&task->signal->cred_guard_mutex); 357a9712bc1SAl Viro return -EPERM; 358a9712bc1SAl Viro } 359a9712bc1SAl Viro return 0; 360a9712bc1SAl Viro } 361a9712bc1SAl Viro 362a9712bc1SAl Viro static void unlock_trace(struct task_struct *task) 363a9712bc1SAl Viro { 364a9712bc1SAl Viro mutex_unlock(&task->signal->cred_guard_mutex); 365a9712bc1SAl Viro } 366a9712bc1SAl Viro 3672ec220e2SKen Chen #ifdef CONFIG_STACKTRACE 3682ec220e2SKen Chen 3692ec220e2SKen Chen #define MAX_STACK_TRACE_DEPTH 64 3702ec220e2SKen Chen 3712ec220e2SKen Chen static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns, 3722ec220e2SKen Chen struct pid *pid, struct task_struct *task) 3732ec220e2SKen Chen { 3742ec220e2SKen Chen struct stack_trace trace; 3752ec220e2SKen Chen unsigned long *entries; 376a9712bc1SAl Viro int err; 3772ec220e2SKen Chen int i; 3782ec220e2SKen Chen 3792ec220e2SKen Chen entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL); 3802ec220e2SKen Chen if (!entries) 3812ec220e2SKen Chen return -ENOMEM; 3822ec220e2SKen Chen 3832ec220e2SKen Chen trace.nr_entries = 0; 3842ec220e2SKen Chen trace.max_entries = MAX_STACK_TRACE_DEPTH; 3852ec220e2SKen Chen trace.entries = entries; 3862ec220e2SKen Chen trace.skip = 0; 387a9712bc1SAl Viro 388a9712bc1SAl Viro err = lock_trace(task); 389a9712bc1SAl Viro if (!err) { 3902ec220e2SKen Chen save_stack_trace_tsk(task, &trace); 3912ec220e2SKen Chen 3922ec220e2SKen Chen for (i = 0; i < trace.nr_entries; i++) { 39351e03149SKonstantin Khlebnikov seq_printf(m, "[<%pK>] %pS\n", 3942ec220e2SKen Chen (void *)entries[i], (void *)entries[i]); 3952ec220e2SKen Chen } 396a9712bc1SAl Viro unlock_trace(task); 397a9712bc1SAl Viro } 3982ec220e2SKen Chen kfree(entries); 3992ec220e2SKen Chen 400a9712bc1SAl Viro return err; 4012ec220e2SKen Chen } 4022ec220e2SKen Chen #endif 4032ec220e2SKen Chen 4041da177e4SLinus Torvalds #ifdef CONFIG_SCHEDSTATS 4051da177e4SLinus Torvalds /* 4061da177e4SLinus Torvalds * Provides /proc/PID/schedstat 4071da177e4SLinus Torvalds */ 4081da177e4SLinus Torvalds static int proc_pid_schedstat(struct task_struct *task, char *buffer) 4091da177e4SLinus Torvalds { 410172ba844SBalbir Singh return sprintf(buffer, "%llu %llu %lu\n", 411826e08b0SIngo Molnar (unsigned long long)task->se.sum_exec_runtime, 412826e08b0SIngo Molnar (unsigned long long)task->sched_info.run_delay, 4132d72376bSIngo Molnar task->sched_info.pcount); 4141da177e4SLinus Torvalds } 4151da177e4SLinus Torvalds #endif 4161da177e4SLinus Torvalds 4179745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP 4189745512cSArjan van de Ven static int lstats_show_proc(struct seq_file *m, void *v) 4199745512cSArjan van de Ven { 4209745512cSArjan van de Ven int i; 42113d77c37SHiroshi Shimamoto struct inode *inode = m->private; 42213d77c37SHiroshi Shimamoto struct task_struct *task = get_proc_task(inode); 4239745512cSArjan van de Ven 42413d77c37SHiroshi Shimamoto if (!task) 42513d77c37SHiroshi Shimamoto return -ESRCH; 42613d77c37SHiroshi Shimamoto seq_puts(m, "Latency Top version : v0.1\n"); 4279745512cSArjan van de Ven for (i = 0; i < 32; i++) { 42834e49d4fSJoe Perches struct latency_record *lr = &task->latency_record[i]; 42934e49d4fSJoe Perches if (lr->backtrace[0]) { 4309745512cSArjan van de Ven int q; 4319745512cSArjan van de Ven seq_printf(m, "%i %li %li", 43234e49d4fSJoe Perches lr->count, lr->time, lr->max); 4339745512cSArjan van de Ven for (q = 0; q < LT_BACKTRACEDEPTH; q++) { 43434e49d4fSJoe Perches unsigned long bt = lr->backtrace[q]; 43534e49d4fSJoe Perches if (!bt) 4369745512cSArjan van de Ven break; 43734e49d4fSJoe Perches if (bt == ULONG_MAX) 4389745512cSArjan van de Ven break; 43934e49d4fSJoe Perches seq_printf(m, " %ps", (void *)bt); 4409745512cSArjan van de Ven } 4419d6de12fSAlexey Dobriyan seq_putc(m, '\n'); 4429745512cSArjan van de Ven } 4439745512cSArjan van de Ven 4449745512cSArjan van de Ven } 44513d77c37SHiroshi Shimamoto put_task_struct(task); 4469745512cSArjan van de Ven return 0; 4479745512cSArjan van de Ven } 4489745512cSArjan van de Ven 4499745512cSArjan van de Ven static int lstats_open(struct inode *inode, struct file *file) 4509745512cSArjan van de Ven { 45113d77c37SHiroshi Shimamoto return single_open(file, lstats_show_proc, inode); 452d6643d12SHiroshi Shimamoto } 453d6643d12SHiroshi Shimamoto 4549745512cSArjan van de Ven static ssize_t lstats_write(struct file *file, const char __user *buf, 4559745512cSArjan van de Ven size_t count, loff_t *offs) 4569745512cSArjan van de Ven { 45713d77c37SHiroshi Shimamoto struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 4589745512cSArjan van de Ven 45913d77c37SHiroshi Shimamoto if (!task) 46013d77c37SHiroshi Shimamoto return -ESRCH; 4619745512cSArjan van de Ven clear_all_latency_tracing(task); 46213d77c37SHiroshi Shimamoto put_task_struct(task); 4639745512cSArjan van de Ven 4649745512cSArjan van de Ven return count; 4659745512cSArjan van de Ven } 4669745512cSArjan van de Ven 4679745512cSArjan van de Ven static const struct file_operations proc_lstats_operations = { 4689745512cSArjan van de Ven .open = lstats_open, 4699745512cSArjan van de Ven .read = seq_read, 4709745512cSArjan van de Ven .write = lstats_write, 4719745512cSArjan van de Ven .llseek = seq_lseek, 47213d77c37SHiroshi Shimamoto .release = single_release, 4739745512cSArjan van de Ven }; 4749745512cSArjan van de Ven 4759745512cSArjan van de Ven #endif 4769745512cSArjan van de Ven 4771da177e4SLinus Torvalds static int proc_oom_score(struct task_struct *task, char *buffer) 4781da177e4SLinus Torvalds { 479b95c35e7SOleg Nesterov unsigned long points = 0; 4801da177e4SLinus Torvalds 48119c5d45aSAlexey Dobriyan read_lock(&tasklist_lock); 482b95c35e7SOleg Nesterov if (pid_alive(task)) 483a63d83f4SDavid Rientjes points = oom_badness(task, NULL, NULL, 484a63d83f4SDavid Rientjes totalram_pages + total_swap_pages); 48519c5d45aSAlexey Dobriyan read_unlock(&tasklist_lock); 4861da177e4SLinus Torvalds return sprintf(buffer, "%lu\n", points); 4871da177e4SLinus Torvalds } 4881da177e4SLinus Torvalds 489d85f50d5SNeil Horman struct limit_names { 490d85f50d5SNeil Horman char *name; 491d85f50d5SNeil Horman char *unit; 492d85f50d5SNeil Horman }; 493d85f50d5SNeil Horman 494d85f50d5SNeil Horman static const struct limit_names lnames[RLIM_NLIMITS] = { 495cff4edb5SKees Cook [RLIMIT_CPU] = {"Max cpu time", "seconds"}, 496d85f50d5SNeil Horman [RLIMIT_FSIZE] = {"Max file size", "bytes"}, 497d85f50d5SNeil Horman [RLIMIT_DATA] = {"Max data size", "bytes"}, 498d85f50d5SNeil Horman [RLIMIT_STACK] = {"Max stack size", "bytes"}, 499d85f50d5SNeil Horman [RLIMIT_CORE] = {"Max core file size", "bytes"}, 500d85f50d5SNeil Horman [RLIMIT_RSS] = {"Max resident set", "bytes"}, 501d85f50d5SNeil Horman [RLIMIT_NPROC] = {"Max processes", "processes"}, 502d85f50d5SNeil Horman [RLIMIT_NOFILE] = {"Max open files", "files"}, 503d85f50d5SNeil Horman [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"}, 504d85f50d5SNeil Horman [RLIMIT_AS] = {"Max address space", "bytes"}, 505d85f50d5SNeil Horman [RLIMIT_LOCKS] = {"Max file locks", "locks"}, 506d85f50d5SNeil Horman [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"}, 507d85f50d5SNeil Horman [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"}, 508d85f50d5SNeil Horman [RLIMIT_NICE] = {"Max nice priority", NULL}, 509d85f50d5SNeil Horman [RLIMIT_RTPRIO] = {"Max realtime priority", NULL}, 5108808117cSEugene Teo [RLIMIT_RTTIME] = {"Max realtime timeout", "us"}, 511d85f50d5SNeil Horman }; 512d85f50d5SNeil Horman 513d85f50d5SNeil Horman /* Display limits for a process */ 514d85f50d5SNeil Horman static int proc_pid_limits(struct task_struct *task, char *buffer) 515d85f50d5SNeil Horman { 516d85f50d5SNeil Horman unsigned int i; 517d85f50d5SNeil Horman int count = 0; 518d85f50d5SNeil Horman unsigned long flags; 519d85f50d5SNeil Horman char *bufptr = buffer; 520d85f50d5SNeil Horman 521d85f50d5SNeil Horman struct rlimit rlim[RLIM_NLIMITS]; 522d85f50d5SNeil Horman 523a6bebbc8SLai Jiangshan if (!lock_task_sighand(task, &flags)) 524d85f50d5SNeil Horman return 0; 525d85f50d5SNeil Horman memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS); 526d85f50d5SNeil Horman unlock_task_sighand(task, &flags); 527d85f50d5SNeil Horman 528d85f50d5SNeil Horman /* 529d85f50d5SNeil Horman * print the file header 530d85f50d5SNeil Horman */ 531d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n", 532d85f50d5SNeil Horman "Limit", "Soft Limit", "Hard Limit", "Units"); 533d85f50d5SNeil Horman 534d85f50d5SNeil Horman for (i = 0; i < RLIM_NLIMITS; i++) { 535d85f50d5SNeil Horman if (rlim[i].rlim_cur == RLIM_INFINITY) 536d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-25s %-20s ", 537d85f50d5SNeil Horman lnames[i].name, "unlimited"); 538d85f50d5SNeil Horman else 539d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-25s %-20lu ", 540d85f50d5SNeil Horman lnames[i].name, rlim[i].rlim_cur); 541d85f50d5SNeil Horman 542d85f50d5SNeil Horman if (rlim[i].rlim_max == RLIM_INFINITY) 543d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-20s ", "unlimited"); 544d85f50d5SNeil Horman else 545d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-20lu ", 546d85f50d5SNeil Horman rlim[i].rlim_max); 547d85f50d5SNeil Horman 548d85f50d5SNeil Horman if (lnames[i].unit) 549d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-10s\n", 550d85f50d5SNeil Horman lnames[i].unit); 551d85f50d5SNeil Horman else 552d85f50d5SNeil Horman count += sprintf(&bufptr[count], "\n"); 553d85f50d5SNeil Horman } 554d85f50d5SNeil Horman 555d85f50d5SNeil Horman return count; 556d85f50d5SNeil Horman } 557d85f50d5SNeil Horman 558ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK 559ebcb6734SRoland McGrath static int proc_pid_syscall(struct task_struct *task, char *buffer) 560ebcb6734SRoland McGrath { 561ebcb6734SRoland McGrath long nr; 562ebcb6734SRoland McGrath unsigned long args[6], sp, pc; 563a9712bc1SAl Viro int res = lock_trace(task); 564a9712bc1SAl Viro if (res) 565a9712bc1SAl Viro return res; 566ebcb6734SRoland McGrath 567ebcb6734SRoland McGrath if (task_current_syscall(task, &nr, args, 6, &sp, &pc)) 568a9712bc1SAl Viro res = sprintf(buffer, "running\n"); 569a9712bc1SAl Viro else if (nr < 0) 570a9712bc1SAl Viro res = sprintf(buffer, "%ld 0x%lx 0x%lx\n", nr, sp, pc); 571a9712bc1SAl Viro else 572a9712bc1SAl Viro res = sprintf(buffer, 573ebcb6734SRoland McGrath "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n", 574ebcb6734SRoland McGrath nr, 575ebcb6734SRoland McGrath args[0], args[1], args[2], args[3], args[4], args[5], 576ebcb6734SRoland McGrath sp, pc); 577a9712bc1SAl Viro unlock_trace(task); 578a9712bc1SAl Viro return res; 579ebcb6734SRoland McGrath } 580ebcb6734SRoland McGrath #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */ 581ebcb6734SRoland McGrath 5821da177e4SLinus Torvalds /************************************************************************/ 5831da177e4SLinus Torvalds /* Here the fs part begins */ 5841da177e4SLinus Torvalds /************************************************************************/ 5851da177e4SLinus Torvalds 5861da177e4SLinus Torvalds /* permission checks */ 587778c1144SEric W. Biederman static int proc_fd_access_allowed(struct inode *inode) 5881da177e4SLinus Torvalds { 589778c1144SEric W. Biederman struct task_struct *task; 590778c1144SEric W. Biederman int allowed = 0; 591df26c40eSEric W. Biederman /* Allow access to a task's file descriptors if it is us or we 592df26c40eSEric W. Biederman * may use ptrace attach to the process and find out that 593df26c40eSEric W. Biederman * information. 594778c1144SEric W. Biederman */ 595778c1144SEric W. Biederman task = get_proc_task(inode); 596df26c40eSEric W. Biederman if (task) { 597006ebb40SStephen Smalley allowed = ptrace_may_access(task, PTRACE_MODE_READ); 598778c1144SEric W. Biederman put_task_struct(task); 599df26c40eSEric W. Biederman } 600778c1144SEric W. Biederman return allowed; 6011da177e4SLinus Torvalds } 6021da177e4SLinus Torvalds 603*6b4e306aSEric W. Biederman int proc_setattr(struct dentry *dentry, struct iattr *attr) 6046d76fa58SLinus Torvalds { 6056d76fa58SLinus Torvalds int error; 6066d76fa58SLinus Torvalds struct inode *inode = dentry->d_inode; 6076d76fa58SLinus Torvalds 6086d76fa58SLinus Torvalds if (attr->ia_valid & ATTR_MODE) 6096d76fa58SLinus Torvalds return -EPERM; 6106d76fa58SLinus Torvalds 6116d76fa58SLinus Torvalds error = inode_change_ok(inode, attr); 6121025774cSChristoph Hellwig if (error) 6136d76fa58SLinus Torvalds return error; 6141025774cSChristoph Hellwig 6151025774cSChristoph Hellwig if ((attr->ia_valid & ATTR_SIZE) && 6161025774cSChristoph Hellwig attr->ia_size != i_size_read(inode)) { 6171025774cSChristoph Hellwig error = vmtruncate(inode, attr->ia_size); 6181025774cSChristoph Hellwig if (error) 6191025774cSChristoph Hellwig return error; 6201025774cSChristoph Hellwig } 6211025774cSChristoph Hellwig 6221025774cSChristoph Hellwig setattr_copy(inode, attr); 6231025774cSChristoph Hellwig mark_inode_dirty(inode); 6241025774cSChristoph Hellwig return 0; 6256d76fa58SLinus Torvalds } 6266d76fa58SLinus Torvalds 627c5ef1c42SArjan van de Ven static const struct inode_operations proc_def_inode_operations = { 6286d76fa58SLinus Torvalds .setattr = proc_setattr, 6296d76fa58SLinus Torvalds }; 6306d76fa58SLinus Torvalds 631a1a2c409SMiklos Szeredi static int mounts_open_common(struct inode *inode, struct file *file, 632a1a2c409SMiklos Szeredi const struct seq_operations *op) 6331da177e4SLinus Torvalds { 63499f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 635cf7b708cSPavel Emelyanov struct nsproxy *nsp; 6366b3286edSKirill Korotaev struct mnt_namespace *ns = NULL; 637a1a2c409SMiklos Szeredi struct path root; 6385addc5ddSAl Viro struct proc_mounts *p; 6395addc5ddSAl Viro int ret = -EINVAL; 6405addc5ddSAl Viro 64199f89551SEric W. Biederman if (task) { 642cf7b708cSPavel Emelyanov rcu_read_lock(); 643cf7b708cSPavel Emelyanov nsp = task_nsproxy(task); 644cf7b708cSPavel Emelyanov if (nsp) { 645cf7b708cSPavel Emelyanov ns = nsp->mnt_ns; 6466b3286edSKirill Korotaev if (ns) 6476b3286edSKirill Korotaev get_mnt_ns(ns); 648863c4702SAlexey Dobriyan } 649cf7b708cSPavel Emelyanov rcu_read_unlock(); 650f7ad3c6bSMiklos Szeredi if (ns && get_task_root(task, &root) == 0) 6517c2c7d99SHugh Dickins ret = 0; 65299f89551SEric W. Biederman put_task_struct(task); 65399f89551SEric W. Biederman } 6541da177e4SLinus Torvalds 655a1a2c409SMiklos Szeredi if (!ns) 656a1a2c409SMiklos Szeredi goto err; 6577c2c7d99SHugh Dickins if (ret) 658a1a2c409SMiklos Szeredi goto err_put_ns; 659a1a2c409SMiklos Szeredi 6605addc5ddSAl Viro ret = -ENOMEM; 6615addc5ddSAl Viro p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL); 662a1a2c409SMiklos Szeredi if (!p) 663a1a2c409SMiklos Szeredi goto err_put_path; 664a1a2c409SMiklos Szeredi 6655addc5ddSAl Viro file->private_data = &p->m; 666a1a2c409SMiklos Szeredi ret = seq_open(file, op); 667a1a2c409SMiklos Szeredi if (ret) 668a1a2c409SMiklos Szeredi goto err_free; 669a1a2c409SMiklos Szeredi 670a1a2c409SMiklos Szeredi p->m.private = p; 671a1a2c409SMiklos Szeredi p->ns = ns; 672a1a2c409SMiklos Szeredi p->root = root; 6736b3286edSKirill Korotaev p->event = ns->event; 674a1a2c409SMiklos Szeredi 6755addc5ddSAl Viro return 0; 676a1a2c409SMiklos Szeredi 677a1a2c409SMiklos Szeredi err_free: 6785addc5ddSAl Viro kfree(p); 679a1a2c409SMiklos Szeredi err_put_path: 680a1a2c409SMiklos Szeredi path_put(&root); 681a1a2c409SMiklos Szeredi err_put_ns: 6826b3286edSKirill Korotaev put_mnt_ns(ns); 683a1a2c409SMiklos Szeredi err: 6841da177e4SLinus Torvalds return ret; 6851da177e4SLinus Torvalds } 6861da177e4SLinus Torvalds 6871da177e4SLinus Torvalds static int mounts_release(struct inode *inode, struct file *file) 6881da177e4SLinus Torvalds { 689a1a2c409SMiklos Szeredi struct proc_mounts *p = file->private_data; 690a1a2c409SMiklos Szeredi path_put(&p->root); 691a1a2c409SMiklos Szeredi put_mnt_ns(p->ns); 6921da177e4SLinus Torvalds return seq_release(inode, file); 6931da177e4SLinus Torvalds } 6941da177e4SLinus Torvalds 6955addc5ddSAl Viro static unsigned mounts_poll(struct file *file, poll_table *wait) 6965addc5ddSAl Viro { 6975addc5ddSAl Viro struct proc_mounts *p = file->private_data; 69831b07093SKOSAKI Motohiro unsigned res = POLLIN | POLLRDNORM; 6995addc5ddSAl Viro 7009f5596afSAl Viro poll_wait(file, &p->ns->poll, wait); 7019f5596afSAl Viro if (mnt_had_events(p)) 70231b07093SKOSAKI Motohiro res |= POLLERR | POLLPRI; 7035addc5ddSAl Viro 7045addc5ddSAl Viro return res; 7055addc5ddSAl Viro } 7065addc5ddSAl Viro 707a1a2c409SMiklos Szeredi static int mounts_open(struct inode *inode, struct file *file) 708a1a2c409SMiklos Szeredi { 709a1a2c409SMiklos Szeredi return mounts_open_common(inode, file, &mounts_op); 710a1a2c409SMiklos Szeredi } 711a1a2c409SMiklos Szeredi 71200977a59SArjan van de Ven static const struct file_operations proc_mounts_operations = { 7131da177e4SLinus Torvalds .open = mounts_open, 7141da177e4SLinus Torvalds .read = seq_read, 7151da177e4SLinus Torvalds .llseek = seq_lseek, 7161da177e4SLinus Torvalds .release = mounts_release, 7175addc5ddSAl Viro .poll = mounts_poll, 7181da177e4SLinus Torvalds }; 7191da177e4SLinus Torvalds 7202d4d4864SRam Pai static int mountinfo_open(struct inode *inode, struct file *file) 7212d4d4864SRam Pai { 7222d4d4864SRam Pai return mounts_open_common(inode, file, &mountinfo_op); 7232d4d4864SRam Pai } 7242d4d4864SRam Pai 7252d4d4864SRam Pai static const struct file_operations proc_mountinfo_operations = { 7262d4d4864SRam Pai .open = mountinfo_open, 7272d4d4864SRam Pai .read = seq_read, 7282d4d4864SRam Pai .llseek = seq_lseek, 7292d4d4864SRam Pai .release = mounts_release, 7302d4d4864SRam Pai .poll = mounts_poll, 7312d4d4864SRam Pai }; 7322d4d4864SRam Pai 733b4629fe2SChuck Lever static int mountstats_open(struct inode *inode, struct file *file) 734b4629fe2SChuck Lever { 735a1a2c409SMiklos Szeredi return mounts_open_common(inode, file, &mountstats_op); 736b4629fe2SChuck Lever } 737b4629fe2SChuck Lever 73800977a59SArjan van de Ven static const struct file_operations proc_mountstats_operations = { 739b4629fe2SChuck Lever .open = mountstats_open, 740b4629fe2SChuck Lever .read = seq_read, 741b4629fe2SChuck Lever .llseek = seq_lseek, 742b4629fe2SChuck Lever .release = mounts_release, 743b4629fe2SChuck Lever }; 744b4629fe2SChuck Lever 7451da177e4SLinus Torvalds #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */ 7461da177e4SLinus Torvalds 7471da177e4SLinus Torvalds static ssize_t proc_info_read(struct file * file, char __user * buf, 7481da177e4SLinus Torvalds size_t count, loff_t *ppos) 7491da177e4SLinus Torvalds { 7502fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 7511da177e4SLinus Torvalds unsigned long page; 7521da177e4SLinus Torvalds ssize_t length; 75399f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 75499f89551SEric W. Biederman 75599f89551SEric W. Biederman length = -ESRCH; 75699f89551SEric W. Biederman if (!task) 75799f89551SEric W. Biederman goto out_no_task; 7581da177e4SLinus Torvalds 7591da177e4SLinus Torvalds if (count > PROC_BLOCK_SIZE) 7601da177e4SLinus Torvalds count = PROC_BLOCK_SIZE; 76199f89551SEric W. Biederman 76299f89551SEric W. Biederman length = -ENOMEM; 763e12ba74dSMel Gorman if (!(page = __get_free_page(GFP_TEMPORARY))) 76499f89551SEric W. Biederman goto out; 7651da177e4SLinus Torvalds 7661da177e4SLinus Torvalds length = PROC_I(inode)->op.proc_read(task, (char*)page); 7671da177e4SLinus Torvalds 7681da177e4SLinus Torvalds if (length >= 0) 7691da177e4SLinus Torvalds length = simple_read_from_buffer(buf, count, ppos, (char *)page, length); 7701da177e4SLinus Torvalds free_page(page); 77199f89551SEric W. Biederman out: 77299f89551SEric W. Biederman put_task_struct(task); 77399f89551SEric W. Biederman out_no_task: 7741da177e4SLinus Torvalds return length; 7751da177e4SLinus Torvalds } 7761da177e4SLinus Torvalds 77700977a59SArjan van de Ven static const struct file_operations proc_info_file_operations = { 7781da177e4SLinus Torvalds .read = proc_info_read, 77987df8424SArnd Bergmann .llseek = generic_file_llseek, 7801da177e4SLinus Torvalds }; 7811da177e4SLinus Torvalds 782be614086SEric W. Biederman static int proc_single_show(struct seq_file *m, void *v) 783be614086SEric W. Biederman { 784be614086SEric W. Biederman struct inode *inode = m->private; 785be614086SEric W. Biederman struct pid_namespace *ns; 786be614086SEric W. Biederman struct pid *pid; 787be614086SEric W. Biederman struct task_struct *task; 788be614086SEric W. Biederman int ret; 789be614086SEric W. Biederman 790be614086SEric W. Biederman ns = inode->i_sb->s_fs_info; 791be614086SEric W. Biederman pid = proc_pid(inode); 792be614086SEric W. Biederman task = get_pid_task(pid, PIDTYPE_PID); 793be614086SEric W. Biederman if (!task) 794be614086SEric W. Biederman return -ESRCH; 795be614086SEric W. Biederman 796be614086SEric W. Biederman ret = PROC_I(inode)->op.proc_show(m, ns, pid, task); 797be614086SEric W. Biederman 798be614086SEric W. Biederman put_task_struct(task); 799be614086SEric W. Biederman return ret; 800be614086SEric W. Biederman } 801be614086SEric W. Biederman 802be614086SEric W. Biederman static int proc_single_open(struct inode *inode, struct file *filp) 803be614086SEric W. Biederman { 804c6a34058SJovi Zhang return single_open(filp, proc_single_show, inode); 805be614086SEric W. Biederman } 806be614086SEric W. Biederman 807be614086SEric W. Biederman static const struct file_operations proc_single_file_operations = { 808be614086SEric W. Biederman .open = proc_single_open, 809be614086SEric W. Biederman .read = seq_read, 810be614086SEric W. Biederman .llseek = seq_lseek, 811be614086SEric W. Biederman .release = single_release, 812be614086SEric W. Biederman }; 813be614086SEric W. Biederman 8141da177e4SLinus Torvalds static int mem_open(struct inode* inode, struct file* file) 8151da177e4SLinus Torvalds { 8161da177e4SLinus Torvalds file->private_data = (void*)((long)current->self_exec_id); 8174a3956c7SKAMEZAWA Hiroyuki /* OK to pass negative loff_t, we can catch out-of-range */ 8184a3956c7SKAMEZAWA Hiroyuki file->f_mode |= FMODE_UNSIGNED_OFFSET; 8191da177e4SLinus Torvalds return 0; 8201da177e4SLinus Torvalds } 8211da177e4SLinus Torvalds 8221da177e4SLinus Torvalds static ssize_t mem_read(struct file * file, char __user * buf, 8231da177e4SLinus Torvalds size_t count, loff_t *ppos) 8241da177e4SLinus Torvalds { 8252fddfeefSJosef "Jeff" Sipek struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode); 8261da177e4SLinus Torvalds char *page; 8271da177e4SLinus Torvalds unsigned long src = *ppos; 8281da177e4SLinus Torvalds int ret = -ESRCH; 8291da177e4SLinus Torvalds struct mm_struct *mm; 8301da177e4SLinus Torvalds 83199f89551SEric W. Biederman if (!task) 83299f89551SEric W. Biederman goto out_no_task; 83399f89551SEric W. Biederman 8341da177e4SLinus Torvalds ret = -ENOMEM; 835e12ba74dSMel Gorman page = (char *)__get_free_page(GFP_TEMPORARY); 8361da177e4SLinus Torvalds if (!page) 8371da177e4SLinus Torvalds goto out; 8381da177e4SLinus Torvalds 8398b0db9dbSStephen Wilson mm = check_mem_permission(task); 8408b0db9dbSStephen Wilson ret = PTR_ERR(mm); 8418b0db9dbSStephen Wilson if (IS_ERR(mm)) 8421da177e4SLinus Torvalds goto out_free; 8431da177e4SLinus Torvalds 8441da177e4SLinus Torvalds ret = -EIO; 8451da177e4SLinus Torvalds 8461da177e4SLinus Torvalds if (file->private_data != (void*)((long)current->self_exec_id)) 8471da177e4SLinus Torvalds goto out_put; 8481da177e4SLinus Torvalds 8491da177e4SLinus Torvalds ret = 0; 8501da177e4SLinus Torvalds 8511da177e4SLinus Torvalds while (count > 0) { 8521da177e4SLinus Torvalds int this_len, retval; 8531da177e4SLinus Torvalds 8541da177e4SLinus Torvalds this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count; 8558b0db9dbSStephen Wilson retval = access_remote_vm(mm, src, page, this_len, 0); 8568b0db9dbSStephen Wilson if (!retval) { 8571da177e4SLinus Torvalds if (!ret) 8581da177e4SLinus Torvalds ret = -EIO; 8591da177e4SLinus Torvalds break; 8601da177e4SLinus Torvalds } 8611da177e4SLinus Torvalds 8621da177e4SLinus Torvalds if (copy_to_user(buf, page, retval)) { 8631da177e4SLinus Torvalds ret = -EFAULT; 8641da177e4SLinus Torvalds break; 8651da177e4SLinus Torvalds } 8661da177e4SLinus Torvalds 8671da177e4SLinus Torvalds ret += retval; 8681da177e4SLinus Torvalds src += retval; 8691da177e4SLinus Torvalds buf += retval; 8701da177e4SLinus Torvalds count -= retval; 8711da177e4SLinus Torvalds } 8721da177e4SLinus Torvalds *ppos = src; 8731da177e4SLinus Torvalds 8741da177e4SLinus Torvalds out_put: 8751da177e4SLinus Torvalds mmput(mm); 8761da177e4SLinus Torvalds out_free: 8771da177e4SLinus Torvalds free_page((unsigned long) page); 8781da177e4SLinus Torvalds out: 87999f89551SEric W. Biederman put_task_struct(task); 88099f89551SEric W. Biederman out_no_task: 8811da177e4SLinus Torvalds return ret; 8821da177e4SLinus Torvalds } 8831da177e4SLinus Torvalds 88463967fa9SGlauber de Oliveira Costa static ssize_t mem_write(struct file * file, const char __user *buf, 8851da177e4SLinus Torvalds size_t count, loff_t *ppos) 8861da177e4SLinus Torvalds { 887f7ca54f4SFrederik Deweerdt int copied; 8881da177e4SLinus Torvalds char *page; 8892fddfeefSJosef "Jeff" Sipek struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode); 8901da177e4SLinus Torvalds unsigned long dst = *ppos; 8918b0db9dbSStephen Wilson struct mm_struct *mm; 8921da177e4SLinus Torvalds 89399f89551SEric W. Biederman copied = -ESRCH; 89499f89551SEric W. Biederman if (!task) 89599f89551SEric W. Biederman goto out_no_task; 8961da177e4SLinus Torvalds 8978b0db9dbSStephen Wilson mm = check_mem_permission(task); 8988b0db9dbSStephen Wilson copied = PTR_ERR(mm); 8998b0db9dbSStephen Wilson if (IS_ERR(mm)) 9008b0db9dbSStephen Wilson goto out_task; 90199f89551SEric W. Biederman 90226947f8cSStephen Wilson copied = -EIO; 90326947f8cSStephen Wilson if (file->private_data != (void *)((long)current->self_exec_id)) 9048b0db9dbSStephen Wilson goto out_mm; 90599f89551SEric W. Biederman 90699f89551SEric W. Biederman copied = -ENOMEM; 907e12ba74dSMel Gorman page = (char *)__get_free_page(GFP_TEMPORARY); 9081da177e4SLinus Torvalds if (!page) 9098b0db9dbSStephen Wilson goto out_mm; 9101da177e4SLinus Torvalds 911f7ca54f4SFrederik Deweerdt copied = 0; 9121da177e4SLinus Torvalds while (count > 0) { 9131da177e4SLinus Torvalds int this_len, retval; 9141da177e4SLinus Torvalds 9151da177e4SLinus Torvalds this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count; 9161da177e4SLinus Torvalds if (copy_from_user(page, buf, this_len)) { 9171da177e4SLinus Torvalds copied = -EFAULT; 9181da177e4SLinus Torvalds break; 9191da177e4SLinus Torvalds } 9208b0db9dbSStephen Wilson retval = access_remote_vm(mm, dst, page, this_len, 1); 9211da177e4SLinus Torvalds if (!retval) { 9221da177e4SLinus Torvalds if (!copied) 9231da177e4SLinus Torvalds copied = -EIO; 9241da177e4SLinus Torvalds break; 9251da177e4SLinus Torvalds } 9261da177e4SLinus Torvalds copied += retval; 9271da177e4SLinus Torvalds buf += retval; 9281da177e4SLinus Torvalds dst += retval; 9291da177e4SLinus Torvalds count -= retval; 9301da177e4SLinus Torvalds } 9311da177e4SLinus Torvalds *ppos = dst; 9321da177e4SLinus Torvalds free_page((unsigned long) page); 9338b0db9dbSStephen Wilson out_mm: 9348b0db9dbSStephen Wilson mmput(mm); 9358b0db9dbSStephen Wilson out_task: 93699f89551SEric W. Biederman put_task_struct(task); 93799f89551SEric W. Biederman out_no_task: 9381da177e4SLinus Torvalds return copied; 9391da177e4SLinus Torvalds } 9401da177e4SLinus Torvalds 94185863e47SMatt Mackall loff_t mem_lseek(struct file *file, loff_t offset, int orig) 9421da177e4SLinus Torvalds { 9431da177e4SLinus Torvalds switch (orig) { 9441da177e4SLinus Torvalds case 0: 9451da177e4SLinus Torvalds file->f_pos = offset; 9461da177e4SLinus Torvalds break; 9471da177e4SLinus Torvalds case 1: 9481da177e4SLinus Torvalds file->f_pos += offset; 9491da177e4SLinus Torvalds break; 9501da177e4SLinus Torvalds default: 9511da177e4SLinus Torvalds return -EINVAL; 9521da177e4SLinus Torvalds } 9531da177e4SLinus Torvalds force_successful_syscall_return(); 9541da177e4SLinus Torvalds return file->f_pos; 9551da177e4SLinus Torvalds } 9561da177e4SLinus Torvalds 95700977a59SArjan van de Ven static const struct file_operations proc_mem_operations = { 9581da177e4SLinus Torvalds .llseek = mem_lseek, 9591da177e4SLinus Torvalds .read = mem_read, 9601da177e4SLinus Torvalds .write = mem_write, 9611da177e4SLinus Torvalds .open = mem_open, 9621da177e4SLinus Torvalds }; 9631da177e4SLinus Torvalds 964315e28c8SJames Pearson static ssize_t environ_read(struct file *file, char __user *buf, 965315e28c8SJames Pearson size_t count, loff_t *ppos) 966315e28c8SJames Pearson { 967315e28c8SJames Pearson struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 968315e28c8SJames Pearson char *page; 969315e28c8SJames Pearson unsigned long src = *ppos; 970315e28c8SJames Pearson int ret = -ESRCH; 971315e28c8SJames Pearson struct mm_struct *mm; 972315e28c8SJames Pearson 973315e28c8SJames Pearson if (!task) 974315e28c8SJames Pearson goto out_no_task; 975315e28c8SJames Pearson 976315e28c8SJames Pearson ret = -ENOMEM; 977315e28c8SJames Pearson page = (char *)__get_free_page(GFP_TEMPORARY); 978315e28c8SJames Pearson if (!page) 979315e28c8SJames Pearson goto out; 980315e28c8SJames Pearson 981315e28c8SJames Pearson 982d6f64b89SAl Viro mm = mm_for_maps(task); 983d6f64b89SAl Viro ret = PTR_ERR(mm); 984d6f64b89SAl Viro if (!mm || IS_ERR(mm)) 985315e28c8SJames Pearson goto out_free; 986315e28c8SJames Pearson 987d6f64b89SAl Viro ret = 0; 988315e28c8SJames Pearson while (count > 0) { 989315e28c8SJames Pearson int this_len, retval, max_len; 990315e28c8SJames Pearson 991315e28c8SJames Pearson this_len = mm->env_end - (mm->env_start + src); 992315e28c8SJames Pearson 993315e28c8SJames Pearson if (this_len <= 0) 994315e28c8SJames Pearson break; 995315e28c8SJames Pearson 996315e28c8SJames Pearson max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count; 997315e28c8SJames Pearson this_len = (this_len > max_len) ? max_len : this_len; 998315e28c8SJames Pearson 999315e28c8SJames Pearson retval = access_process_vm(task, (mm->env_start + src), 1000315e28c8SJames Pearson page, this_len, 0); 1001315e28c8SJames Pearson 1002315e28c8SJames Pearson if (retval <= 0) { 1003315e28c8SJames Pearson ret = retval; 1004315e28c8SJames Pearson break; 1005315e28c8SJames Pearson } 1006315e28c8SJames Pearson 1007315e28c8SJames Pearson if (copy_to_user(buf, page, retval)) { 1008315e28c8SJames Pearson ret = -EFAULT; 1009315e28c8SJames Pearson break; 1010315e28c8SJames Pearson } 1011315e28c8SJames Pearson 1012315e28c8SJames Pearson ret += retval; 1013315e28c8SJames Pearson src += retval; 1014315e28c8SJames Pearson buf += retval; 1015315e28c8SJames Pearson count -= retval; 1016315e28c8SJames Pearson } 1017315e28c8SJames Pearson *ppos = src; 1018315e28c8SJames Pearson 1019315e28c8SJames Pearson mmput(mm); 1020315e28c8SJames Pearson out_free: 1021315e28c8SJames Pearson free_page((unsigned long) page); 1022315e28c8SJames Pearson out: 1023315e28c8SJames Pearson put_task_struct(task); 1024315e28c8SJames Pearson out_no_task: 1025315e28c8SJames Pearson return ret; 1026315e28c8SJames Pearson } 1027315e28c8SJames Pearson 1028315e28c8SJames Pearson static const struct file_operations proc_environ_operations = { 1029315e28c8SJames Pearson .read = environ_read, 103087df8424SArnd Bergmann .llseek = generic_file_llseek, 1031315e28c8SJames Pearson }; 1032315e28c8SJames Pearson 10331da177e4SLinus Torvalds static ssize_t oom_adjust_read(struct file *file, char __user *buf, 10341da177e4SLinus Torvalds size_t count, loff_t *ppos) 10351da177e4SLinus Torvalds { 10362fddfeefSJosef "Jeff" Sipek struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode); 10378578cea7SEric W. Biederman char buffer[PROC_NUMBUF]; 10381da177e4SLinus Torvalds size_t len; 103928b83c51SKOSAKI Motohiro int oom_adjust = OOM_DISABLE; 104028b83c51SKOSAKI Motohiro unsigned long flags; 10411da177e4SLinus Torvalds 104299f89551SEric W. Biederman if (!task) 104399f89551SEric W. Biederman return -ESRCH; 104428b83c51SKOSAKI Motohiro 104528b83c51SKOSAKI Motohiro if (lock_task_sighand(task, &flags)) { 104628b83c51SKOSAKI Motohiro oom_adjust = task->signal->oom_adj; 104728b83c51SKOSAKI Motohiro unlock_task_sighand(task, &flags); 104828b83c51SKOSAKI Motohiro } 104928b83c51SKOSAKI Motohiro 105099f89551SEric W. Biederman put_task_struct(task); 105199f89551SEric W. Biederman 10528578cea7SEric W. Biederman len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust); 10530c28f287SAkinobu Mita 10540c28f287SAkinobu Mita return simple_read_from_buffer(buf, count, ppos, buffer, len); 10551da177e4SLinus Torvalds } 10561da177e4SLinus Torvalds 10571da177e4SLinus Torvalds static ssize_t oom_adjust_write(struct file *file, const char __user *buf, 10581da177e4SLinus Torvalds size_t count, loff_t *ppos) 10591da177e4SLinus Torvalds { 106099f89551SEric W. Biederman struct task_struct *task; 10615d863b89SKOSAKI Motohiro char buffer[PROC_NUMBUF]; 10625d863b89SKOSAKI Motohiro long oom_adjust; 106328b83c51SKOSAKI Motohiro unsigned long flags; 10645d863b89SKOSAKI Motohiro int err; 10651da177e4SLinus Torvalds 10668578cea7SEric W. Biederman memset(buffer, 0, sizeof(buffer)); 10678578cea7SEric W. Biederman if (count > sizeof(buffer) - 1) 10688578cea7SEric W. Biederman count = sizeof(buffer) - 1; 1069723548bfSDavid Rientjes if (copy_from_user(buffer, buf, count)) { 1070723548bfSDavid Rientjes err = -EFAULT; 1071723548bfSDavid Rientjes goto out; 1072723548bfSDavid Rientjes } 10735d863b89SKOSAKI Motohiro 10745d863b89SKOSAKI Motohiro err = strict_strtol(strstrip(buffer), 0, &oom_adjust); 10755d863b89SKOSAKI Motohiro if (err) 1076723548bfSDavid Rientjes goto out; 10778ac773b4SAlexey Dobriyan if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) && 1078723548bfSDavid Rientjes oom_adjust != OOM_DISABLE) { 1079723548bfSDavid Rientjes err = -EINVAL; 1080723548bfSDavid Rientjes goto out; 1081723548bfSDavid Rientjes } 10825d863b89SKOSAKI Motohiro 10832fddfeefSJosef "Jeff" Sipek task = get_proc_task(file->f_path.dentry->d_inode); 1084723548bfSDavid Rientjes if (!task) { 1085723548bfSDavid Rientjes err = -ESRCH; 1086723548bfSDavid Rientjes goto out; 1087723548bfSDavid Rientjes } 108828b83c51SKOSAKI Motohiro 10893d5992d2SYing Han task_lock(task); 10903d5992d2SYing Han if (!task->mm) { 1091723548bfSDavid Rientjes err = -EINVAL; 1092723548bfSDavid Rientjes goto err_task_lock; 10933d5992d2SYing Han } 10943d5992d2SYing Han 1095d19d5476SDavid Rientjes if (!lock_task_sighand(task, &flags)) { 1096d19d5476SDavid Rientjes err = -ESRCH; 1097d19d5476SDavid Rientjes goto err_task_lock; 1098d19d5476SDavid Rientjes } 1099d19d5476SDavid Rientjes 1100d19d5476SDavid Rientjes if (oom_adjust < task->signal->oom_adj && !capable(CAP_SYS_RESOURCE)) { 1101d19d5476SDavid Rientjes err = -EACCES; 1102d19d5476SDavid Rientjes goto err_sighand; 1103d19d5476SDavid Rientjes } 1104d19d5476SDavid Rientjes 11053d5992d2SYing Han if (oom_adjust != task->signal->oom_adj) { 11063d5992d2SYing Han if (oom_adjust == OOM_DISABLE) 11073d5992d2SYing Han atomic_inc(&task->mm->oom_disable_count); 11083d5992d2SYing Han if (task->signal->oom_adj == OOM_DISABLE) 11093d5992d2SYing Han atomic_dec(&task->mm->oom_disable_count); 11103d5992d2SYing Han } 11113d5992d2SYing Han 111251b1bd2aSDavid Rientjes /* 111351b1bd2aSDavid Rientjes * Warn that /proc/pid/oom_adj is deprecated, see 111451b1bd2aSDavid Rientjes * Documentation/feature-removal-schedule.txt. 111551b1bd2aSDavid Rientjes */ 111651b1bd2aSDavid Rientjes printk_once(KERN_WARNING "%s (%d): /proc/%d/oom_adj is deprecated, " 111751b1bd2aSDavid Rientjes "please use /proc/%d/oom_score_adj instead.\n", 111851b1bd2aSDavid Rientjes current->comm, task_pid_nr(current), 111951b1bd2aSDavid Rientjes task_pid_nr(task), task_pid_nr(task)); 112028b83c51SKOSAKI Motohiro task->signal->oom_adj = oom_adjust; 1121a63d83f4SDavid Rientjes /* 1122a63d83f4SDavid Rientjes * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum 1123a63d83f4SDavid Rientjes * value is always attainable. 1124a63d83f4SDavid Rientjes */ 1125a63d83f4SDavid Rientjes if (task->signal->oom_adj == OOM_ADJUST_MAX) 1126a63d83f4SDavid Rientjes task->signal->oom_score_adj = OOM_SCORE_ADJ_MAX; 1127a63d83f4SDavid Rientjes else 1128a63d83f4SDavid Rientjes task->signal->oom_score_adj = (oom_adjust * OOM_SCORE_ADJ_MAX) / 1129a63d83f4SDavid Rientjes -OOM_DISABLE; 1130723548bfSDavid Rientjes err_sighand: 113128b83c51SKOSAKI Motohiro unlock_task_sighand(task, &flags); 1132d19d5476SDavid Rientjes err_task_lock: 1133d19d5476SDavid Rientjes task_unlock(task); 113499f89551SEric W. Biederman put_task_struct(task); 1135723548bfSDavid Rientjes out: 1136723548bfSDavid Rientjes return err < 0 ? err : count; 11371da177e4SLinus Torvalds } 11381da177e4SLinus Torvalds 113900977a59SArjan van de Ven static const struct file_operations proc_oom_adjust_operations = { 11401da177e4SLinus Torvalds .read = oom_adjust_read, 11411da177e4SLinus Torvalds .write = oom_adjust_write, 114287df8424SArnd Bergmann .llseek = generic_file_llseek, 11431da177e4SLinus Torvalds }; 11441da177e4SLinus Torvalds 1145a63d83f4SDavid Rientjes static ssize_t oom_score_adj_read(struct file *file, char __user *buf, 1146a63d83f4SDavid Rientjes size_t count, loff_t *ppos) 1147a63d83f4SDavid Rientjes { 1148a63d83f4SDavid Rientjes struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode); 1149a63d83f4SDavid Rientjes char buffer[PROC_NUMBUF]; 1150a63d83f4SDavid Rientjes int oom_score_adj = OOM_SCORE_ADJ_MIN; 1151a63d83f4SDavid Rientjes unsigned long flags; 1152a63d83f4SDavid Rientjes size_t len; 1153a63d83f4SDavid Rientjes 1154a63d83f4SDavid Rientjes if (!task) 1155a63d83f4SDavid Rientjes return -ESRCH; 1156a63d83f4SDavid Rientjes if (lock_task_sighand(task, &flags)) { 1157a63d83f4SDavid Rientjes oom_score_adj = task->signal->oom_score_adj; 1158a63d83f4SDavid Rientjes unlock_task_sighand(task, &flags); 1159a63d83f4SDavid Rientjes } 1160a63d83f4SDavid Rientjes put_task_struct(task); 1161a63d83f4SDavid Rientjes len = snprintf(buffer, sizeof(buffer), "%d\n", oom_score_adj); 1162a63d83f4SDavid Rientjes return simple_read_from_buffer(buf, count, ppos, buffer, len); 1163a63d83f4SDavid Rientjes } 1164a63d83f4SDavid Rientjes 1165a63d83f4SDavid Rientjes static ssize_t oom_score_adj_write(struct file *file, const char __user *buf, 1166a63d83f4SDavid Rientjes size_t count, loff_t *ppos) 1167a63d83f4SDavid Rientjes { 1168a63d83f4SDavid Rientjes struct task_struct *task; 1169a63d83f4SDavid Rientjes char buffer[PROC_NUMBUF]; 1170a63d83f4SDavid Rientjes unsigned long flags; 1171a63d83f4SDavid Rientjes long oom_score_adj; 1172a63d83f4SDavid Rientjes int err; 1173a63d83f4SDavid Rientjes 1174a63d83f4SDavid Rientjes memset(buffer, 0, sizeof(buffer)); 1175a63d83f4SDavid Rientjes if (count > sizeof(buffer) - 1) 1176a63d83f4SDavid Rientjes count = sizeof(buffer) - 1; 1177723548bfSDavid Rientjes if (copy_from_user(buffer, buf, count)) { 1178723548bfSDavid Rientjes err = -EFAULT; 1179723548bfSDavid Rientjes goto out; 1180723548bfSDavid Rientjes } 1181a63d83f4SDavid Rientjes 1182a63d83f4SDavid Rientjes err = strict_strtol(strstrip(buffer), 0, &oom_score_adj); 1183a63d83f4SDavid Rientjes if (err) 1184723548bfSDavid Rientjes goto out; 1185a63d83f4SDavid Rientjes if (oom_score_adj < OOM_SCORE_ADJ_MIN || 1186723548bfSDavid Rientjes oom_score_adj > OOM_SCORE_ADJ_MAX) { 1187723548bfSDavid Rientjes err = -EINVAL; 1188723548bfSDavid Rientjes goto out; 1189723548bfSDavid Rientjes } 1190a63d83f4SDavid Rientjes 1191a63d83f4SDavid Rientjes task = get_proc_task(file->f_path.dentry->d_inode); 1192723548bfSDavid Rientjes if (!task) { 1193723548bfSDavid Rientjes err = -ESRCH; 1194723548bfSDavid Rientjes goto out; 1195723548bfSDavid Rientjes } 1196a63d83f4SDavid Rientjes 11973d5992d2SYing Han task_lock(task); 11983d5992d2SYing Han if (!task->mm) { 1199723548bfSDavid Rientjes err = -EINVAL; 1200723548bfSDavid Rientjes goto err_task_lock; 12013d5992d2SYing Han } 1202d19d5476SDavid Rientjes 1203d19d5476SDavid Rientjes if (!lock_task_sighand(task, &flags)) { 1204d19d5476SDavid Rientjes err = -ESRCH; 1205d19d5476SDavid Rientjes goto err_task_lock; 1206d19d5476SDavid Rientjes } 1207d19d5476SDavid Rientjes 1208dabb16f6SMandeep Singh Baines if (oom_score_adj < task->signal->oom_score_adj_min && 1209d19d5476SDavid Rientjes !capable(CAP_SYS_RESOURCE)) { 1210d19d5476SDavid Rientjes err = -EACCES; 1211d19d5476SDavid Rientjes goto err_sighand; 1212d19d5476SDavid Rientjes } 1213d19d5476SDavid Rientjes 12143d5992d2SYing Han if (oom_score_adj != task->signal->oom_score_adj) { 12153d5992d2SYing Han if (oom_score_adj == OOM_SCORE_ADJ_MIN) 12163d5992d2SYing Han atomic_inc(&task->mm->oom_disable_count); 12173d5992d2SYing Han if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MIN) 12183d5992d2SYing Han atomic_dec(&task->mm->oom_disable_count); 12193d5992d2SYing Han } 1220a63d83f4SDavid Rientjes task->signal->oom_score_adj = oom_score_adj; 1221dabb16f6SMandeep Singh Baines if (has_capability_noaudit(current, CAP_SYS_RESOURCE)) 1222dabb16f6SMandeep Singh Baines task->signal->oom_score_adj_min = oom_score_adj; 1223a63d83f4SDavid Rientjes /* 1224a63d83f4SDavid Rientjes * Scale /proc/pid/oom_adj appropriately ensuring that OOM_DISABLE is 1225a63d83f4SDavid Rientjes * always attainable. 1226a63d83f4SDavid Rientjes */ 1227a63d83f4SDavid Rientjes if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MIN) 1228a63d83f4SDavid Rientjes task->signal->oom_adj = OOM_DISABLE; 1229a63d83f4SDavid Rientjes else 1230a63d83f4SDavid Rientjes task->signal->oom_adj = (oom_score_adj * OOM_ADJUST_MAX) / 1231a63d83f4SDavid Rientjes OOM_SCORE_ADJ_MAX; 1232723548bfSDavid Rientjes err_sighand: 1233a63d83f4SDavid Rientjes unlock_task_sighand(task, &flags); 1234d19d5476SDavid Rientjes err_task_lock: 1235d19d5476SDavid Rientjes task_unlock(task); 1236a63d83f4SDavid Rientjes put_task_struct(task); 1237723548bfSDavid Rientjes out: 1238723548bfSDavid Rientjes return err < 0 ? err : count; 1239a63d83f4SDavid Rientjes } 1240a63d83f4SDavid Rientjes 1241a63d83f4SDavid Rientjes static const struct file_operations proc_oom_score_adj_operations = { 1242a63d83f4SDavid Rientjes .read = oom_score_adj_read, 1243a63d83f4SDavid Rientjes .write = oom_score_adj_write, 12446038f373SArnd Bergmann .llseek = default_llseek, 1245a63d83f4SDavid Rientjes }; 1246a63d83f4SDavid Rientjes 12471da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL 12481da177e4SLinus Torvalds #define TMPBUFLEN 21 12491da177e4SLinus Torvalds static ssize_t proc_loginuid_read(struct file * file, char __user * buf, 12501da177e4SLinus Torvalds size_t count, loff_t *ppos) 12511da177e4SLinus Torvalds { 12522fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 125399f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 12541da177e4SLinus Torvalds ssize_t length; 12551da177e4SLinus Torvalds char tmpbuf[TMPBUFLEN]; 12561da177e4SLinus Torvalds 125799f89551SEric W. Biederman if (!task) 125899f89551SEric W. Biederman return -ESRCH; 12591da177e4SLinus Torvalds length = scnprintf(tmpbuf, TMPBUFLEN, "%u", 12600c11b942SAl Viro audit_get_loginuid(task)); 126199f89551SEric W. Biederman put_task_struct(task); 12621da177e4SLinus Torvalds return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 12631da177e4SLinus Torvalds } 12641da177e4SLinus Torvalds 12651da177e4SLinus Torvalds static ssize_t proc_loginuid_write(struct file * file, const char __user * buf, 12661da177e4SLinus Torvalds size_t count, loff_t *ppos) 12671da177e4SLinus Torvalds { 12682fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 12691da177e4SLinus Torvalds char *page, *tmp; 12701da177e4SLinus Torvalds ssize_t length; 12711da177e4SLinus Torvalds uid_t loginuid; 12721da177e4SLinus Torvalds 12731da177e4SLinus Torvalds if (!capable(CAP_AUDIT_CONTROL)) 12741da177e4SLinus Torvalds return -EPERM; 12751da177e4SLinus Torvalds 12767dc52157SPaul E. McKenney rcu_read_lock(); 12777dc52157SPaul E. McKenney if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) { 12787dc52157SPaul E. McKenney rcu_read_unlock(); 12791da177e4SLinus Torvalds return -EPERM; 12807dc52157SPaul E. McKenney } 12817dc52157SPaul E. McKenney rcu_read_unlock(); 12821da177e4SLinus Torvalds 1283e0182909SAl Viro if (count >= PAGE_SIZE) 1284e0182909SAl Viro count = PAGE_SIZE - 1; 12851da177e4SLinus Torvalds 12861da177e4SLinus Torvalds if (*ppos != 0) { 12871da177e4SLinus Torvalds /* No partial writes. */ 12881da177e4SLinus Torvalds return -EINVAL; 12891da177e4SLinus Torvalds } 1290e12ba74dSMel Gorman page = (char*)__get_free_page(GFP_TEMPORARY); 12911da177e4SLinus Torvalds if (!page) 12921da177e4SLinus Torvalds return -ENOMEM; 12931da177e4SLinus Torvalds length = -EFAULT; 12941da177e4SLinus Torvalds if (copy_from_user(page, buf, count)) 12951da177e4SLinus Torvalds goto out_free_page; 12961da177e4SLinus Torvalds 1297e0182909SAl Viro page[count] = '\0'; 12981da177e4SLinus Torvalds loginuid = simple_strtoul(page, &tmp, 10); 12991da177e4SLinus Torvalds if (tmp == page) { 13001da177e4SLinus Torvalds length = -EINVAL; 13011da177e4SLinus Torvalds goto out_free_page; 13021da177e4SLinus Torvalds 13031da177e4SLinus Torvalds } 130499f89551SEric W. Biederman length = audit_set_loginuid(current, loginuid); 13051da177e4SLinus Torvalds if (likely(length == 0)) 13061da177e4SLinus Torvalds length = count; 13071da177e4SLinus Torvalds 13081da177e4SLinus Torvalds out_free_page: 13091da177e4SLinus Torvalds free_page((unsigned long) page); 13101da177e4SLinus Torvalds return length; 13111da177e4SLinus Torvalds } 13121da177e4SLinus Torvalds 131300977a59SArjan van de Ven static const struct file_operations proc_loginuid_operations = { 13141da177e4SLinus Torvalds .read = proc_loginuid_read, 13151da177e4SLinus Torvalds .write = proc_loginuid_write, 131687df8424SArnd Bergmann .llseek = generic_file_llseek, 13171da177e4SLinus Torvalds }; 13181e0bd755SEric Paris 13191e0bd755SEric Paris static ssize_t proc_sessionid_read(struct file * file, char __user * buf, 13201e0bd755SEric Paris size_t count, loff_t *ppos) 13211e0bd755SEric Paris { 13221e0bd755SEric Paris struct inode * inode = file->f_path.dentry->d_inode; 13231e0bd755SEric Paris struct task_struct *task = get_proc_task(inode); 13241e0bd755SEric Paris ssize_t length; 13251e0bd755SEric Paris char tmpbuf[TMPBUFLEN]; 13261e0bd755SEric Paris 13271e0bd755SEric Paris if (!task) 13281e0bd755SEric Paris return -ESRCH; 13291e0bd755SEric Paris length = scnprintf(tmpbuf, TMPBUFLEN, "%u", 13301e0bd755SEric Paris audit_get_sessionid(task)); 13311e0bd755SEric Paris put_task_struct(task); 13321e0bd755SEric Paris return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 13331e0bd755SEric Paris } 13341e0bd755SEric Paris 13351e0bd755SEric Paris static const struct file_operations proc_sessionid_operations = { 13361e0bd755SEric Paris .read = proc_sessionid_read, 133787df8424SArnd Bergmann .llseek = generic_file_llseek, 13381e0bd755SEric Paris }; 13391da177e4SLinus Torvalds #endif 13401da177e4SLinus Torvalds 1341f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION 1342f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_read(struct file * file, char __user * buf, 1343f4f154fdSAkinobu Mita size_t count, loff_t *ppos) 1344f4f154fdSAkinobu Mita { 1345f4f154fdSAkinobu Mita struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 1346f4f154fdSAkinobu Mita char buffer[PROC_NUMBUF]; 1347f4f154fdSAkinobu Mita size_t len; 1348f4f154fdSAkinobu Mita int make_it_fail; 1349f4f154fdSAkinobu Mita 1350f4f154fdSAkinobu Mita if (!task) 1351f4f154fdSAkinobu Mita return -ESRCH; 1352f4f154fdSAkinobu Mita make_it_fail = task->make_it_fail; 1353f4f154fdSAkinobu Mita put_task_struct(task); 1354f4f154fdSAkinobu Mita 1355f4f154fdSAkinobu Mita len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail); 13560c28f287SAkinobu Mita 13570c28f287SAkinobu Mita return simple_read_from_buffer(buf, count, ppos, buffer, len); 1358f4f154fdSAkinobu Mita } 1359f4f154fdSAkinobu Mita 1360f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_write(struct file * file, 1361f4f154fdSAkinobu Mita const char __user * buf, size_t count, loff_t *ppos) 1362f4f154fdSAkinobu Mita { 1363f4f154fdSAkinobu Mita struct task_struct *task; 1364f4f154fdSAkinobu Mita char buffer[PROC_NUMBUF], *end; 1365f4f154fdSAkinobu Mita int make_it_fail; 1366f4f154fdSAkinobu Mita 1367f4f154fdSAkinobu Mita if (!capable(CAP_SYS_RESOURCE)) 1368f4f154fdSAkinobu Mita return -EPERM; 1369f4f154fdSAkinobu Mita memset(buffer, 0, sizeof(buffer)); 1370f4f154fdSAkinobu Mita if (count > sizeof(buffer) - 1) 1371f4f154fdSAkinobu Mita count = sizeof(buffer) - 1; 1372f4f154fdSAkinobu Mita if (copy_from_user(buffer, buf, count)) 1373f4f154fdSAkinobu Mita return -EFAULT; 1374cba8aafeSVincent Li make_it_fail = simple_strtol(strstrip(buffer), &end, 0); 1375cba8aafeSVincent Li if (*end) 1376cba8aafeSVincent Li return -EINVAL; 1377f4f154fdSAkinobu Mita task = get_proc_task(file->f_dentry->d_inode); 1378f4f154fdSAkinobu Mita if (!task) 1379f4f154fdSAkinobu Mita return -ESRCH; 1380f4f154fdSAkinobu Mita task->make_it_fail = make_it_fail; 1381f4f154fdSAkinobu Mita put_task_struct(task); 1382cba8aafeSVincent Li 1383cba8aafeSVincent Li return count; 1384f4f154fdSAkinobu Mita } 1385f4f154fdSAkinobu Mita 138600977a59SArjan van de Ven static const struct file_operations proc_fault_inject_operations = { 1387f4f154fdSAkinobu Mita .read = proc_fault_inject_read, 1388f4f154fdSAkinobu Mita .write = proc_fault_inject_write, 138987df8424SArnd Bergmann .llseek = generic_file_llseek, 1390f4f154fdSAkinobu Mita }; 1391f4f154fdSAkinobu Mita #endif 1392f4f154fdSAkinobu Mita 13939745512cSArjan van de Ven 139443ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG 139543ae34cbSIngo Molnar /* 139643ae34cbSIngo Molnar * Print out various scheduling related per-task fields: 139743ae34cbSIngo Molnar */ 139843ae34cbSIngo Molnar static int sched_show(struct seq_file *m, void *v) 139943ae34cbSIngo Molnar { 140043ae34cbSIngo Molnar struct inode *inode = m->private; 140143ae34cbSIngo Molnar struct task_struct *p; 140243ae34cbSIngo Molnar 140343ae34cbSIngo Molnar p = get_proc_task(inode); 140443ae34cbSIngo Molnar if (!p) 140543ae34cbSIngo Molnar return -ESRCH; 140643ae34cbSIngo Molnar proc_sched_show_task(p, m); 140743ae34cbSIngo Molnar 140843ae34cbSIngo Molnar put_task_struct(p); 140943ae34cbSIngo Molnar 141043ae34cbSIngo Molnar return 0; 141143ae34cbSIngo Molnar } 141243ae34cbSIngo Molnar 141343ae34cbSIngo Molnar static ssize_t 141443ae34cbSIngo Molnar sched_write(struct file *file, const char __user *buf, 141543ae34cbSIngo Molnar size_t count, loff_t *offset) 141643ae34cbSIngo Molnar { 141743ae34cbSIngo Molnar struct inode *inode = file->f_path.dentry->d_inode; 141843ae34cbSIngo Molnar struct task_struct *p; 141943ae34cbSIngo Molnar 142043ae34cbSIngo Molnar p = get_proc_task(inode); 142143ae34cbSIngo Molnar if (!p) 142243ae34cbSIngo Molnar return -ESRCH; 142343ae34cbSIngo Molnar proc_sched_set_task(p); 142443ae34cbSIngo Molnar 142543ae34cbSIngo Molnar put_task_struct(p); 142643ae34cbSIngo Molnar 142743ae34cbSIngo Molnar return count; 142843ae34cbSIngo Molnar } 142943ae34cbSIngo Molnar 143043ae34cbSIngo Molnar static int sched_open(struct inode *inode, struct file *filp) 143143ae34cbSIngo Molnar { 1432c6a34058SJovi Zhang return single_open(filp, sched_show, inode); 143343ae34cbSIngo Molnar } 143443ae34cbSIngo Molnar 143543ae34cbSIngo Molnar static const struct file_operations proc_pid_sched_operations = { 143643ae34cbSIngo Molnar .open = sched_open, 143743ae34cbSIngo Molnar .read = seq_read, 143843ae34cbSIngo Molnar .write = sched_write, 143943ae34cbSIngo Molnar .llseek = seq_lseek, 14405ea473a1SAlexey Dobriyan .release = single_release, 144143ae34cbSIngo Molnar }; 144243ae34cbSIngo Molnar 144343ae34cbSIngo Molnar #endif 144443ae34cbSIngo Molnar 14455091faa4SMike Galbraith #ifdef CONFIG_SCHED_AUTOGROUP 14465091faa4SMike Galbraith /* 14475091faa4SMike Galbraith * Print out autogroup related information: 14485091faa4SMike Galbraith */ 14495091faa4SMike Galbraith static int sched_autogroup_show(struct seq_file *m, void *v) 14505091faa4SMike Galbraith { 14515091faa4SMike Galbraith struct inode *inode = m->private; 14525091faa4SMike Galbraith struct task_struct *p; 14535091faa4SMike Galbraith 14545091faa4SMike Galbraith p = get_proc_task(inode); 14555091faa4SMike Galbraith if (!p) 14565091faa4SMike Galbraith return -ESRCH; 14575091faa4SMike Galbraith proc_sched_autogroup_show_task(p, m); 14585091faa4SMike Galbraith 14595091faa4SMike Galbraith put_task_struct(p); 14605091faa4SMike Galbraith 14615091faa4SMike Galbraith return 0; 14625091faa4SMike Galbraith } 14635091faa4SMike Galbraith 14645091faa4SMike Galbraith static ssize_t 14655091faa4SMike Galbraith sched_autogroup_write(struct file *file, const char __user *buf, 14665091faa4SMike Galbraith size_t count, loff_t *offset) 14675091faa4SMike Galbraith { 14685091faa4SMike Galbraith struct inode *inode = file->f_path.dentry->d_inode; 14695091faa4SMike Galbraith struct task_struct *p; 14705091faa4SMike Galbraith char buffer[PROC_NUMBUF]; 14715091faa4SMike Galbraith long nice; 14725091faa4SMike Galbraith int err; 14735091faa4SMike Galbraith 14745091faa4SMike Galbraith memset(buffer, 0, sizeof(buffer)); 14755091faa4SMike Galbraith if (count > sizeof(buffer) - 1) 14765091faa4SMike Galbraith count = sizeof(buffer) - 1; 14775091faa4SMike Galbraith if (copy_from_user(buffer, buf, count)) 14785091faa4SMike Galbraith return -EFAULT; 14795091faa4SMike Galbraith 14805091faa4SMike Galbraith err = strict_strtol(strstrip(buffer), 0, &nice); 14815091faa4SMike Galbraith if (err) 14825091faa4SMike Galbraith return -EINVAL; 14835091faa4SMike Galbraith 14845091faa4SMike Galbraith p = get_proc_task(inode); 14855091faa4SMike Galbraith if (!p) 14865091faa4SMike Galbraith return -ESRCH; 14875091faa4SMike Galbraith 14885091faa4SMike Galbraith err = nice; 14895091faa4SMike Galbraith err = proc_sched_autogroup_set_nice(p, &err); 14905091faa4SMike Galbraith if (err) 14915091faa4SMike Galbraith count = err; 14925091faa4SMike Galbraith 14935091faa4SMike Galbraith put_task_struct(p); 14945091faa4SMike Galbraith 14955091faa4SMike Galbraith return count; 14965091faa4SMike Galbraith } 14975091faa4SMike Galbraith 14985091faa4SMike Galbraith static int sched_autogroup_open(struct inode *inode, struct file *filp) 14995091faa4SMike Galbraith { 15005091faa4SMike Galbraith int ret; 15015091faa4SMike Galbraith 15025091faa4SMike Galbraith ret = single_open(filp, sched_autogroup_show, NULL); 15035091faa4SMike Galbraith if (!ret) { 15045091faa4SMike Galbraith struct seq_file *m = filp->private_data; 15055091faa4SMike Galbraith 15065091faa4SMike Galbraith m->private = inode; 15075091faa4SMike Galbraith } 15085091faa4SMike Galbraith return ret; 15095091faa4SMike Galbraith } 15105091faa4SMike Galbraith 15115091faa4SMike Galbraith static const struct file_operations proc_pid_sched_autogroup_operations = { 15125091faa4SMike Galbraith .open = sched_autogroup_open, 15135091faa4SMike Galbraith .read = seq_read, 15145091faa4SMike Galbraith .write = sched_autogroup_write, 15155091faa4SMike Galbraith .llseek = seq_lseek, 15165091faa4SMike Galbraith .release = single_release, 15175091faa4SMike Galbraith }; 15185091faa4SMike Galbraith 15195091faa4SMike Galbraith #endif /* CONFIG_SCHED_AUTOGROUP */ 15205091faa4SMike Galbraith 15214614a696Sjohn stultz static ssize_t comm_write(struct file *file, const char __user *buf, 15224614a696Sjohn stultz size_t count, loff_t *offset) 15234614a696Sjohn stultz { 15244614a696Sjohn stultz struct inode *inode = file->f_path.dentry->d_inode; 15254614a696Sjohn stultz struct task_struct *p; 15264614a696Sjohn stultz char buffer[TASK_COMM_LEN]; 15274614a696Sjohn stultz 15284614a696Sjohn stultz memset(buffer, 0, sizeof(buffer)); 15294614a696Sjohn stultz if (count > sizeof(buffer) - 1) 15304614a696Sjohn stultz count = sizeof(buffer) - 1; 15314614a696Sjohn stultz if (copy_from_user(buffer, buf, count)) 15324614a696Sjohn stultz return -EFAULT; 15334614a696Sjohn stultz 15344614a696Sjohn stultz p = get_proc_task(inode); 15354614a696Sjohn stultz if (!p) 15364614a696Sjohn stultz return -ESRCH; 15374614a696Sjohn stultz 15384614a696Sjohn stultz if (same_thread_group(current, p)) 15394614a696Sjohn stultz set_task_comm(p, buffer); 15404614a696Sjohn stultz else 15414614a696Sjohn stultz count = -EINVAL; 15424614a696Sjohn stultz 15434614a696Sjohn stultz put_task_struct(p); 15444614a696Sjohn stultz 15454614a696Sjohn stultz return count; 15464614a696Sjohn stultz } 15474614a696Sjohn stultz 15484614a696Sjohn stultz static int comm_show(struct seq_file *m, void *v) 15494614a696Sjohn stultz { 15504614a696Sjohn stultz struct inode *inode = m->private; 15514614a696Sjohn stultz struct task_struct *p; 15524614a696Sjohn stultz 15534614a696Sjohn stultz p = get_proc_task(inode); 15544614a696Sjohn stultz if (!p) 15554614a696Sjohn stultz return -ESRCH; 15564614a696Sjohn stultz 15574614a696Sjohn stultz task_lock(p); 15584614a696Sjohn stultz seq_printf(m, "%s\n", p->comm); 15594614a696Sjohn stultz task_unlock(p); 15604614a696Sjohn stultz 15614614a696Sjohn stultz put_task_struct(p); 15624614a696Sjohn stultz 15634614a696Sjohn stultz return 0; 15644614a696Sjohn stultz } 15654614a696Sjohn stultz 15664614a696Sjohn stultz static int comm_open(struct inode *inode, struct file *filp) 15674614a696Sjohn stultz { 1568c6a34058SJovi Zhang return single_open(filp, comm_show, inode); 15694614a696Sjohn stultz } 15704614a696Sjohn stultz 15714614a696Sjohn stultz static const struct file_operations proc_pid_set_comm_operations = { 15724614a696Sjohn stultz .open = comm_open, 15734614a696Sjohn stultz .read = seq_read, 15744614a696Sjohn stultz .write = comm_write, 15754614a696Sjohn stultz .llseek = seq_lseek, 15764614a696Sjohn stultz .release = single_release, 15774614a696Sjohn stultz }; 15784614a696Sjohn stultz 1579925d1c40SMatt Helsley /* 1580925d1c40SMatt Helsley * We added or removed a vma mapping the executable. The vmas are only mapped 1581925d1c40SMatt Helsley * during exec and are not mapped with the mmap system call. 1582925d1c40SMatt Helsley * Callers must hold down_write() on the mm's mmap_sem for these 1583925d1c40SMatt Helsley */ 1584925d1c40SMatt Helsley void added_exe_file_vma(struct mm_struct *mm) 1585925d1c40SMatt Helsley { 1586925d1c40SMatt Helsley mm->num_exe_file_vmas++; 1587925d1c40SMatt Helsley } 1588925d1c40SMatt Helsley 1589925d1c40SMatt Helsley void removed_exe_file_vma(struct mm_struct *mm) 1590925d1c40SMatt Helsley { 1591925d1c40SMatt Helsley mm->num_exe_file_vmas--; 1592925d1c40SMatt Helsley if ((mm->num_exe_file_vmas == 0) && mm->exe_file){ 1593925d1c40SMatt Helsley fput(mm->exe_file); 1594925d1c40SMatt Helsley mm->exe_file = NULL; 1595925d1c40SMatt Helsley } 1596925d1c40SMatt Helsley 1597925d1c40SMatt Helsley } 1598925d1c40SMatt Helsley 1599925d1c40SMatt Helsley void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file) 1600925d1c40SMatt Helsley { 1601925d1c40SMatt Helsley if (new_exe_file) 1602925d1c40SMatt Helsley get_file(new_exe_file); 1603925d1c40SMatt Helsley if (mm->exe_file) 1604925d1c40SMatt Helsley fput(mm->exe_file); 1605925d1c40SMatt Helsley mm->exe_file = new_exe_file; 1606925d1c40SMatt Helsley mm->num_exe_file_vmas = 0; 1607925d1c40SMatt Helsley } 1608925d1c40SMatt Helsley 1609925d1c40SMatt Helsley struct file *get_mm_exe_file(struct mm_struct *mm) 1610925d1c40SMatt Helsley { 1611925d1c40SMatt Helsley struct file *exe_file; 1612925d1c40SMatt Helsley 1613925d1c40SMatt Helsley /* We need mmap_sem to protect against races with removal of 1614925d1c40SMatt Helsley * VM_EXECUTABLE vmas */ 1615925d1c40SMatt Helsley down_read(&mm->mmap_sem); 1616925d1c40SMatt Helsley exe_file = mm->exe_file; 1617925d1c40SMatt Helsley if (exe_file) 1618925d1c40SMatt Helsley get_file(exe_file); 1619925d1c40SMatt Helsley up_read(&mm->mmap_sem); 1620925d1c40SMatt Helsley return exe_file; 1621925d1c40SMatt Helsley } 1622925d1c40SMatt Helsley 1623925d1c40SMatt Helsley void dup_mm_exe_file(struct mm_struct *oldmm, struct mm_struct *newmm) 1624925d1c40SMatt Helsley { 1625925d1c40SMatt Helsley /* It's safe to write the exe_file pointer without exe_file_lock because 1626925d1c40SMatt Helsley * this is called during fork when the task is not yet in /proc */ 1627925d1c40SMatt Helsley newmm->exe_file = get_mm_exe_file(oldmm); 1628925d1c40SMatt Helsley } 1629925d1c40SMatt Helsley 1630925d1c40SMatt Helsley static int proc_exe_link(struct inode *inode, struct path *exe_path) 1631925d1c40SMatt Helsley { 1632925d1c40SMatt Helsley struct task_struct *task; 1633925d1c40SMatt Helsley struct mm_struct *mm; 1634925d1c40SMatt Helsley struct file *exe_file; 1635925d1c40SMatt Helsley 1636925d1c40SMatt Helsley task = get_proc_task(inode); 1637925d1c40SMatt Helsley if (!task) 1638925d1c40SMatt Helsley return -ENOENT; 1639925d1c40SMatt Helsley mm = get_task_mm(task); 1640925d1c40SMatt Helsley put_task_struct(task); 1641925d1c40SMatt Helsley if (!mm) 1642925d1c40SMatt Helsley return -ENOENT; 1643925d1c40SMatt Helsley exe_file = get_mm_exe_file(mm); 1644925d1c40SMatt Helsley mmput(mm); 1645925d1c40SMatt Helsley if (exe_file) { 1646925d1c40SMatt Helsley *exe_path = exe_file->f_path; 1647925d1c40SMatt Helsley path_get(&exe_file->f_path); 1648925d1c40SMatt Helsley fput(exe_file); 1649925d1c40SMatt Helsley return 0; 1650925d1c40SMatt Helsley } else 1651925d1c40SMatt Helsley return -ENOENT; 1652925d1c40SMatt Helsley } 1653925d1c40SMatt Helsley 1654008b150aSAl Viro static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd) 16551da177e4SLinus Torvalds { 16561da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 16571da177e4SLinus Torvalds int error = -EACCES; 16581da177e4SLinus Torvalds 16591da177e4SLinus Torvalds /* We don't need a base pointer in the /proc filesystem */ 16601d957f9bSJan Blunck path_put(&nd->path); 16611da177e4SLinus Torvalds 1662778c1144SEric W. Biederman /* Are we allowed to snoop on the tasks file descriptors? */ 1663778c1144SEric W. Biederman if (!proc_fd_access_allowed(inode)) 16641da177e4SLinus Torvalds goto out; 16651da177e4SLinus Torvalds 16663dcd25f3SJan Blunck error = PROC_I(inode)->op.proc_get_link(inode, &nd->path); 16671da177e4SLinus Torvalds out: 1668008b150aSAl Viro return ERR_PTR(error); 16691da177e4SLinus Torvalds } 16701da177e4SLinus Torvalds 16713dcd25f3SJan Blunck static int do_proc_readlink(struct path *path, char __user *buffer, int buflen) 16721da177e4SLinus Torvalds { 1673e12ba74dSMel Gorman char *tmp = (char*)__get_free_page(GFP_TEMPORARY); 16743dcd25f3SJan Blunck char *pathname; 16751da177e4SLinus Torvalds int len; 16761da177e4SLinus Torvalds 16771da177e4SLinus Torvalds if (!tmp) 16781da177e4SLinus Torvalds return -ENOMEM; 16791da177e4SLinus Torvalds 16807b2a69baSEric W. Biederman pathname = d_path(path, tmp, PAGE_SIZE); 16813dcd25f3SJan Blunck len = PTR_ERR(pathname); 16823dcd25f3SJan Blunck if (IS_ERR(pathname)) 16831da177e4SLinus Torvalds goto out; 16843dcd25f3SJan Blunck len = tmp + PAGE_SIZE - 1 - pathname; 16851da177e4SLinus Torvalds 16861da177e4SLinus Torvalds if (len > buflen) 16871da177e4SLinus Torvalds len = buflen; 16883dcd25f3SJan Blunck if (copy_to_user(buffer, pathname, len)) 16891da177e4SLinus Torvalds len = -EFAULT; 16901da177e4SLinus Torvalds out: 16911da177e4SLinus Torvalds free_page((unsigned long)tmp); 16921da177e4SLinus Torvalds return len; 16931da177e4SLinus Torvalds } 16941da177e4SLinus Torvalds 16951da177e4SLinus Torvalds static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen) 16961da177e4SLinus Torvalds { 16971da177e4SLinus Torvalds int error = -EACCES; 16981da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 16993dcd25f3SJan Blunck struct path path; 17001da177e4SLinus Torvalds 1701778c1144SEric W. Biederman /* Are we allowed to snoop on the tasks file descriptors? */ 1702778c1144SEric W. Biederman if (!proc_fd_access_allowed(inode)) 17031da177e4SLinus Torvalds goto out; 17041da177e4SLinus Torvalds 17053dcd25f3SJan Blunck error = PROC_I(inode)->op.proc_get_link(inode, &path); 17061da177e4SLinus Torvalds if (error) 17071da177e4SLinus Torvalds goto out; 17081da177e4SLinus Torvalds 17093dcd25f3SJan Blunck error = do_proc_readlink(&path, buffer, buflen); 17103dcd25f3SJan Blunck path_put(&path); 17111da177e4SLinus Torvalds out: 17121da177e4SLinus Torvalds return error; 17131da177e4SLinus Torvalds } 17141da177e4SLinus Torvalds 1715c5ef1c42SArjan van de Ven static const struct inode_operations proc_pid_link_inode_operations = { 17161da177e4SLinus Torvalds .readlink = proc_pid_readlink, 17176d76fa58SLinus Torvalds .follow_link = proc_pid_follow_link, 17186d76fa58SLinus Torvalds .setattr = proc_setattr, 17191da177e4SLinus Torvalds }; 17201da177e4SLinus Torvalds 172128a6d671SEric W. Biederman 172228a6d671SEric W. Biederman /* building an inode */ 172328a6d671SEric W. Biederman 172428a6d671SEric W. Biederman static int task_dumpable(struct task_struct *task) 172528a6d671SEric W. Biederman { 172628a6d671SEric W. Biederman int dumpable = 0; 172728a6d671SEric W. Biederman struct mm_struct *mm; 172828a6d671SEric W. Biederman 172928a6d671SEric W. Biederman task_lock(task); 173028a6d671SEric W. Biederman mm = task->mm; 173128a6d671SEric W. Biederman if (mm) 17326c5d5238SKawai, Hidehiro dumpable = get_dumpable(mm); 173328a6d671SEric W. Biederman task_unlock(task); 173428a6d671SEric W. Biederman if(dumpable == 1) 173528a6d671SEric W. Biederman return 1; 173628a6d671SEric W. Biederman return 0; 173728a6d671SEric W. Biederman } 173828a6d671SEric W. Biederman 1739*6b4e306aSEric W. Biederman struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task) 174028a6d671SEric W. Biederman { 174128a6d671SEric W. Biederman struct inode * inode; 174228a6d671SEric W. Biederman struct proc_inode *ei; 1743c69e8d9cSDavid Howells const struct cred *cred; 174428a6d671SEric W. Biederman 174528a6d671SEric W. Biederman /* We need a new inode */ 174628a6d671SEric W. Biederman 174728a6d671SEric W. Biederman inode = new_inode(sb); 174828a6d671SEric W. Biederman if (!inode) 174928a6d671SEric W. Biederman goto out; 175028a6d671SEric W. Biederman 175128a6d671SEric W. Biederman /* Common stuff */ 175228a6d671SEric W. Biederman ei = PROC_I(inode); 175385fe4025SChristoph Hellwig inode->i_ino = get_next_ino(); 175428a6d671SEric W. Biederman inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; 175528a6d671SEric W. Biederman inode->i_op = &proc_def_inode_operations; 175628a6d671SEric W. Biederman 175728a6d671SEric W. Biederman /* 175828a6d671SEric W. Biederman * grab the reference to task. 175928a6d671SEric W. Biederman */ 17601a657f78SOleg Nesterov ei->pid = get_task_pid(task, PIDTYPE_PID); 176128a6d671SEric W. Biederman if (!ei->pid) 176228a6d671SEric W. Biederman goto out_unlock; 176328a6d671SEric W. Biederman 176428a6d671SEric W. Biederman if (task_dumpable(task)) { 1765c69e8d9cSDavid Howells rcu_read_lock(); 1766c69e8d9cSDavid Howells cred = __task_cred(task); 1767c69e8d9cSDavid Howells inode->i_uid = cred->euid; 1768c69e8d9cSDavid Howells inode->i_gid = cred->egid; 1769c69e8d9cSDavid Howells rcu_read_unlock(); 177028a6d671SEric W. Biederman } 177128a6d671SEric W. Biederman security_task_to_inode(task, inode); 177228a6d671SEric W. Biederman 177328a6d671SEric W. Biederman out: 177428a6d671SEric W. Biederman return inode; 177528a6d671SEric W. Biederman 177628a6d671SEric W. Biederman out_unlock: 177728a6d671SEric W. Biederman iput(inode); 177828a6d671SEric W. Biederman return NULL; 177928a6d671SEric W. Biederman } 178028a6d671SEric W. Biederman 1781*6b4e306aSEric W. Biederman int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) 178228a6d671SEric W. Biederman { 178328a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 178428a6d671SEric W. Biederman struct task_struct *task; 1785c69e8d9cSDavid Howells const struct cred *cred; 1786c69e8d9cSDavid Howells 178728a6d671SEric W. Biederman generic_fillattr(inode, stat); 178828a6d671SEric W. Biederman 178928a6d671SEric W. Biederman rcu_read_lock(); 179028a6d671SEric W. Biederman stat->uid = 0; 179128a6d671SEric W. Biederman stat->gid = 0; 179228a6d671SEric W. Biederman task = pid_task(proc_pid(inode), PIDTYPE_PID); 179328a6d671SEric W. Biederman if (task) { 179428a6d671SEric W. Biederman if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) || 179528a6d671SEric W. Biederman task_dumpable(task)) { 1796c69e8d9cSDavid Howells cred = __task_cred(task); 1797c69e8d9cSDavid Howells stat->uid = cred->euid; 1798c69e8d9cSDavid Howells stat->gid = cred->egid; 179928a6d671SEric W. Biederman } 180028a6d671SEric W. Biederman } 180128a6d671SEric W. Biederman rcu_read_unlock(); 180228a6d671SEric W. Biederman return 0; 180328a6d671SEric W. Biederman } 180428a6d671SEric W. Biederman 180528a6d671SEric W. Biederman /* dentry stuff */ 180628a6d671SEric W. Biederman 180728a6d671SEric W. Biederman /* 180828a6d671SEric W. Biederman * Exceptional case: normally we are not allowed to unhash a busy 180928a6d671SEric W. Biederman * directory. In this case, however, we can do it - no aliasing problems 181028a6d671SEric W. Biederman * due to the way we treat inodes. 181128a6d671SEric W. Biederman * 181228a6d671SEric W. Biederman * Rewrite the inode's ownerships here because the owning task may have 181328a6d671SEric W. Biederman * performed a setuid(), etc. 181428a6d671SEric W. Biederman * 181528a6d671SEric W. Biederman * Before the /proc/pid/status file was created the only way to read 181628a6d671SEric W. Biederman * the effective uid of a /process was to stat /proc/pid. Reading 181728a6d671SEric W. Biederman * /proc/pid/status is slow enough that procps and other packages 181828a6d671SEric W. Biederman * kept stating /proc/pid. To keep the rules in /proc simple I have 181928a6d671SEric W. Biederman * made this apply to all per process world readable and executable 182028a6d671SEric W. Biederman * directories. 182128a6d671SEric W. Biederman */ 1822*6b4e306aSEric W. Biederman int pid_revalidate(struct dentry *dentry, struct nameidata *nd) 182328a6d671SEric W. Biederman { 182434286d66SNick Piggin struct inode *inode; 182534286d66SNick Piggin struct task_struct *task; 1826c69e8d9cSDavid Howells const struct cred *cred; 1827c69e8d9cSDavid Howells 182834286d66SNick Piggin if (nd && nd->flags & LOOKUP_RCU) 182934286d66SNick Piggin return -ECHILD; 183034286d66SNick Piggin 183134286d66SNick Piggin inode = dentry->d_inode; 183234286d66SNick Piggin task = get_proc_task(inode); 183334286d66SNick Piggin 183428a6d671SEric W. Biederman if (task) { 183528a6d671SEric W. Biederman if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) || 183628a6d671SEric W. Biederman task_dumpable(task)) { 1837c69e8d9cSDavid Howells rcu_read_lock(); 1838c69e8d9cSDavid Howells cred = __task_cred(task); 1839c69e8d9cSDavid Howells inode->i_uid = cred->euid; 1840c69e8d9cSDavid Howells inode->i_gid = cred->egid; 1841c69e8d9cSDavid Howells rcu_read_unlock(); 184228a6d671SEric W. Biederman } else { 184328a6d671SEric W. Biederman inode->i_uid = 0; 184428a6d671SEric W. Biederman inode->i_gid = 0; 184528a6d671SEric W. Biederman } 184628a6d671SEric W. Biederman inode->i_mode &= ~(S_ISUID | S_ISGID); 184728a6d671SEric W. Biederman security_task_to_inode(task, inode); 184828a6d671SEric W. Biederman put_task_struct(task); 184928a6d671SEric W. Biederman return 1; 185028a6d671SEric W. Biederman } 185128a6d671SEric W. Biederman d_drop(dentry); 185228a6d671SEric W. Biederman return 0; 185328a6d671SEric W. Biederman } 185428a6d671SEric W. Biederman 1855fe15ce44SNick Piggin static int pid_delete_dentry(const struct dentry * dentry) 185628a6d671SEric W. Biederman { 185728a6d671SEric W. Biederman /* Is the task we represent dead? 185828a6d671SEric W. Biederman * If so, then don't put the dentry on the lru list, 185928a6d671SEric W. Biederman * kill it immediately. 186028a6d671SEric W. Biederman */ 186128a6d671SEric W. Biederman return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first; 186228a6d671SEric W. Biederman } 186328a6d671SEric W. Biederman 1864*6b4e306aSEric W. Biederman const struct dentry_operations pid_dentry_operations = 186528a6d671SEric W. Biederman { 186628a6d671SEric W. Biederman .d_revalidate = pid_revalidate, 186728a6d671SEric W. Biederman .d_delete = pid_delete_dentry, 186828a6d671SEric W. Biederman }; 186928a6d671SEric W. Biederman 187028a6d671SEric W. Biederman /* Lookups */ 187128a6d671SEric W. Biederman 18721c0d04c9SEric W. Biederman /* 18731c0d04c9SEric W. Biederman * Fill a directory entry. 18741c0d04c9SEric W. Biederman * 18751c0d04c9SEric W. Biederman * If possible create the dcache entry and derive our inode number and 18761c0d04c9SEric W. Biederman * file type from dcache entry. 18771c0d04c9SEric W. Biederman * 18781c0d04c9SEric W. Biederman * Since all of the proc inode numbers are dynamically generated, the inode 18791c0d04c9SEric W. Biederman * numbers do not exist until the inode is cache. This means creating the 18801c0d04c9SEric W. Biederman * the dcache entry in readdir is necessary to keep the inode numbers 18811c0d04c9SEric W. Biederman * reported by readdir in sync with the inode numbers reported 18821c0d04c9SEric W. Biederman * by stat. 18831c0d04c9SEric W. Biederman */ 1884*6b4e306aSEric W. Biederman int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir, 1885*6b4e306aSEric W. Biederman const char *name, int len, 1886c5141e6dSEric Dumazet instantiate_t instantiate, struct task_struct *task, const void *ptr) 188761a28784SEric W. Biederman { 18882fddfeefSJosef "Jeff" Sipek struct dentry *child, *dir = filp->f_path.dentry; 188961a28784SEric W. Biederman struct inode *inode; 189061a28784SEric W. Biederman struct qstr qname; 189161a28784SEric W. Biederman ino_t ino = 0; 189261a28784SEric W. Biederman unsigned type = DT_UNKNOWN; 189361a28784SEric W. Biederman 189461a28784SEric W. Biederman qname.name = name; 189561a28784SEric W. Biederman qname.len = len; 189661a28784SEric W. Biederman qname.hash = full_name_hash(name, len); 189761a28784SEric W. Biederman 189861a28784SEric W. Biederman child = d_lookup(dir, &qname); 189961a28784SEric W. Biederman if (!child) { 190061a28784SEric W. Biederman struct dentry *new; 190161a28784SEric W. Biederman new = d_alloc(dir, &qname); 190261a28784SEric W. Biederman if (new) { 190361a28784SEric W. Biederman child = instantiate(dir->d_inode, new, task, ptr); 190461a28784SEric W. Biederman if (child) 190561a28784SEric W. Biederman dput(new); 190661a28784SEric W. Biederman else 190761a28784SEric W. Biederman child = new; 190861a28784SEric W. Biederman } 190961a28784SEric W. Biederman } 191061a28784SEric W. Biederman if (!child || IS_ERR(child) || !child->d_inode) 191161a28784SEric W. Biederman goto end_instantiate; 191261a28784SEric W. Biederman inode = child->d_inode; 191361a28784SEric W. Biederman if (inode) { 191461a28784SEric W. Biederman ino = inode->i_ino; 191561a28784SEric W. Biederman type = inode->i_mode >> 12; 191661a28784SEric W. Biederman } 191761a28784SEric W. Biederman dput(child); 191861a28784SEric W. Biederman end_instantiate: 191961a28784SEric W. Biederman if (!ino) 192061a28784SEric W. Biederman ino = find_inode_number(dir, &qname); 192161a28784SEric W. Biederman if (!ino) 192261a28784SEric W. Biederman ino = 1; 192361a28784SEric W. Biederman return filldir(dirent, name, len, filp->f_pos, ino, type); 192461a28784SEric W. Biederman } 192561a28784SEric W. Biederman 192628a6d671SEric W. Biederman static unsigned name_to_int(struct dentry *dentry) 192728a6d671SEric W. Biederman { 192828a6d671SEric W. Biederman const char *name = dentry->d_name.name; 192928a6d671SEric W. Biederman int len = dentry->d_name.len; 193028a6d671SEric W. Biederman unsigned n = 0; 193128a6d671SEric W. Biederman 193228a6d671SEric W. Biederman if (len > 1 && *name == '0') 193328a6d671SEric W. Biederman goto out; 193428a6d671SEric W. Biederman while (len-- > 0) { 193528a6d671SEric W. Biederman unsigned c = *name++ - '0'; 193628a6d671SEric W. Biederman if (c > 9) 193728a6d671SEric W. Biederman goto out; 193828a6d671SEric W. Biederman if (n >= (~0U-9)/10) 193928a6d671SEric W. Biederman goto out; 194028a6d671SEric W. Biederman n *= 10; 194128a6d671SEric W. Biederman n += c; 194228a6d671SEric W. Biederman } 194328a6d671SEric W. Biederman return n; 194428a6d671SEric W. Biederman out: 194528a6d671SEric W. Biederman return ~0U; 194628a6d671SEric W. Biederman } 194728a6d671SEric W. Biederman 194827932742SMiklos Szeredi #define PROC_FDINFO_MAX 64 194927932742SMiklos Szeredi 19503dcd25f3SJan Blunck static int proc_fd_info(struct inode *inode, struct path *path, char *info) 195128a6d671SEric W. Biederman { 195228a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 195328a6d671SEric W. Biederman struct files_struct *files = NULL; 195428a6d671SEric W. Biederman struct file *file; 195528a6d671SEric W. Biederman int fd = proc_fd(inode); 195628a6d671SEric W. Biederman 195728a6d671SEric W. Biederman if (task) { 195828a6d671SEric W. Biederman files = get_files_struct(task); 195928a6d671SEric W. Biederman put_task_struct(task); 196028a6d671SEric W. Biederman } 196128a6d671SEric W. Biederman if (files) { 196228a6d671SEric W. Biederman /* 196328a6d671SEric W. Biederman * We are not taking a ref to the file structure, so we must 196428a6d671SEric W. Biederman * hold ->file_lock. 196528a6d671SEric W. Biederman */ 196628a6d671SEric W. Biederman spin_lock(&files->file_lock); 196728a6d671SEric W. Biederman file = fcheck_files(files, fd); 196828a6d671SEric W. Biederman if (file) { 19693dcd25f3SJan Blunck if (path) { 19703dcd25f3SJan Blunck *path = file->f_path; 19713dcd25f3SJan Blunck path_get(&file->f_path); 19723dcd25f3SJan Blunck } 197327932742SMiklos Szeredi if (info) 197427932742SMiklos Szeredi snprintf(info, PROC_FDINFO_MAX, 197527932742SMiklos Szeredi "pos:\t%lli\n" 197627932742SMiklos Szeredi "flags:\t0%o\n", 197727932742SMiklos Szeredi (long long) file->f_pos, 197827932742SMiklos Szeredi file->f_flags); 197928a6d671SEric W. Biederman spin_unlock(&files->file_lock); 198028a6d671SEric W. Biederman put_files_struct(files); 198128a6d671SEric W. Biederman return 0; 198228a6d671SEric W. Biederman } 198328a6d671SEric W. Biederman spin_unlock(&files->file_lock); 198428a6d671SEric W. Biederman put_files_struct(files); 198528a6d671SEric W. Biederman } 198628a6d671SEric W. Biederman return -ENOENT; 198728a6d671SEric W. Biederman } 198828a6d671SEric W. Biederman 19893dcd25f3SJan Blunck static int proc_fd_link(struct inode *inode, struct path *path) 199027932742SMiklos Szeredi { 19913dcd25f3SJan Blunck return proc_fd_info(inode, path, NULL); 199227932742SMiklos Szeredi } 199327932742SMiklos Szeredi 199428a6d671SEric W. Biederman static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd) 199528a6d671SEric W. Biederman { 199634286d66SNick Piggin struct inode *inode; 199734286d66SNick Piggin struct task_struct *task; 199834286d66SNick Piggin int fd; 199928a6d671SEric W. Biederman struct files_struct *files; 2000c69e8d9cSDavid Howells const struct cred *cred; 200128a6d671SEric W. Biederman 200234286d66SNick Piggin if (nd && nd->flags & LOOKUP_RCU) 200334286d66SNick Piggin return -ECHILD; 200434286d66SNick Piggin 200534286d66SNick Piggin inode = dentry->d_inode; 200634286d66SNick Piggin task = get_proc_task(inode); 200734286d66SNick Piggin fd = proc_fd(inode); 200834286d66SNick Piggin 200928a6d671SEric W. Biederman if (task) { 201028a6d671SEric W. Biederman files = get_files_struct(task); 201128a6d671SEric W. Biederman if (files) { 201228a6d671SEric W. Biederman rcu_read_lock(); 201328a6d671SEric W. Biederman if (fcheck_files(files, fd)) { 201428a6d671SEric W. Biederman rcu_read_unlock(); 201528a6d671SEric W. Biederman put_files_struct(files); 201628a6d671SEric W. Biederman if (task_dumpable(task)) { 2017c69e8d9cSDavid Howells rcu_read_lock(); 2018c69e8d9cSDavid Howells cred = __task_cred(task); 2019c69e8d9cSDavid Howells inode->i_uid = cred->euid; 2020c69e8d9cSDavid Howells inode->i_gid = cred->egid; 2021c69e8d9cSDavid Howells rcu_read_unlock(); 202228a6d671SEric W. Biederman } else { 202328a6d671SEric W. Biederman inode->i_uid = 0; 202428a6d671SEric W. Biederman inode->i_gid = 0; 202528a6d671SEric W. Biederman } 202628a6d671SEric W. Biederman inode->i_mode &= ~(S_ISUID | S_ISGID); 202728a6d671SEric W. Biederman security_task_to_inode(task, inode); 202828a6d671SEric W. Biederman put_task_struct(task); 202928a6d671SEric W. Biederman return 1; 203028a6d671SEric W. Biederman } 203128a6d671SEric W. Biederman rcu_read_unlock(); 203228a6d671SEric W. Biederman put_files_struct(files); 203328a6d671SEric W. Biederman } 203428a6d671SEric W. Biederman put_task_struct(task); 203528a6d671SEric W. Biederman } 203628a6d671SEric W. Biederman d_drop(dentry); 203728a6d671SEric W. Biederman return 0; 203828a6d671SEric W. Biederman } 203928a6d671SEric W. Biederman 2040d72f71ebSAl Viro static const struct dentry_operations tid_fd_dentry_operations = 204128a6d671SEric W. Biederman { 204228a6d671SEric W. Biederman .d_revalidate = tid_fd_revalidate, 204328a6d671SEric W. Biederman .d_delete = pid_delete_dentry, 204428a6d671SEric W. Biederman }; 204528a6d671SEric W. Biederman 2046444ceed8SEric W. Biederman static struct dentry *proc_fd_instantiate(struct inode *dir, 2047c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 204828a6d671SEric W. Biederman { 2049c5141e6dSEric Dumazet unsigned fd = *(const unsigned *)ptr; 205028a6d671SEric W. Biederman struct file *file; 205128a6d671SEric W. Biederman struct files_struct *files; 205228a6d671SEric W. Biederman struct inode *inode; 205328a6d671SEric W. Biederman struct proc_inode *ei; 2054444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-ENOENT); 205528a6d671SEric W. Biederman 205661a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 205728a6d671SEric W. Biederman if (!inode) 205828a6d671SEric W. Biederman goto out; 205928a6d671SEric W. Biederman ei = PROC_I(inode); 206028a6d671SEric W. Biederman ei->fd = fd; 206128a6d671SEric W. Biederman files = get_files_struct(task); 206228a6d671SEric W. Biederman if (!files) 2063444ceed8SEric W. Biederman goto out_iput; 206428a6d671SEric W. Biederman inode->i_mode = S_IFLNK; 206528a6d671SEric W. Biederman 206628a6d671SEric W. Biederman /* 206728a6d671SEric W. Biederman * We are not taking a ref to the file structure, so we must 206828a6d671SEric W. Biederman * hold ->file_lock. 206928a6d671SEric W. Biederman */ 207028a6d671SEric W. Biederman spin_lock(&files->file_lock); 207128a6d671SEric W. Biederman file = fcheck_files(files, fd); 207228a6d671SEric W. Biederman if (!file) 2073444ceed8SEric W. Biederman goto out_unlock; 2074aeb5d727SAl Viro if (file->f_mode & FMODE_READ) 207528a6d671SEric W. Biederman inode->i_mode |= S_IRUSR | S_IXUSR; 2076aeb5d727SAl Viro if (file->f_mode & FMODE_WRITE) 207728a6d671SEric W. Biederman inode->i_mode |= S_IWUSR | S_IXUSR; 207828a6d671SEric W. Biederman spin_unlock(&files->file_lock); 207928a6d671SEric W. Biederman put_files_struct(files); 2080444ceed8SEric W. Biederman 208128a6d671SEric W. Biederman inode->i_op = &proc_pid_link_inode_operations; 208228a6d671SEric W. Biederman inode->i_size = 64; 208328a6d671SEric W. Biederman ei->op.proc_get_link = proc_fd_link; 2084fb045adbSNick Piggin d_set_d_op(dentry, &tid_fd_dentry_operations); 208528a6d671SEric W. Biederman d_add(dentry, inode); 208628a6d671SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 208728a6d671SEric W. Biederman if (tid_fd_revalidate(dentry, NULL)) 2088444ceed8SEric W. Biederman error = NULL; 2089444ceed8SEric W. Biederman 2090444ceed8SEric W. Biederman out: 2091444ceed8SEric W. Biederman return error; 2092444ceed8SEric W. Biederman out_unlock: 2093444ceed8SEric W. Biederman spin_unlock(&files->file_lock); 2094444ceed8SEric W. Biederman put_files_struct(files); 2095444ceed8SEric W. Biederman out_iput: 2096444ceed8SEric W. Biederman iput(inode); 2097444ceed8SEric W. Biederman goto out; 2098444ceed8SEric W. Biederman } 2099444ceed8SEric W. Biederman 210027932742SMiklos Szeredi static struct dentry *proc_lookupfd_common(struct inode *dir, 210127932742SMiklos Szeredi struct dentry *dentry, 210227932742SMiklos Szeredi instantiate_t instantiate) 2103444ceed8SEric W. Biederman { 2104444ceed8SEric W. Biederman struct task_struct *task = get_proc_task(dir); 2105444ceed8SEric W. Biederman unsigned fd = name_to_int(dentry); 2106444ceed8SEric W. Biederman struct dentry *result = ERR_PTR(-ENOENT); 2107444ceed8SEric W. Biederman 2108444ceed8SEric W. Biederman if (!task) 2109444ceed8SEric W. Biederman goto out_no_task; 2110444ceed8SEric W. Biederman if (fd == ~0U) 2111444ceed8SEric W. Biederman goto out; 2112444ceed8SEric W. Biederman 211327932742SMiklos Szeredi result = instantiate(dir, dentry, task, &fd); 211428a6d671SEric W. Biederman out: 211528a6d671SEric W. Biederman put_task_struct(task); 211628a6d671SEric W. Biederman out_no_task: 211728a6d671SEric W. Biederman return result; 211828a6d671SEric W. Biederman } 211928a6d671SEric W. Biederman 212027932742SMiklos Szeredi static int proc_readfd_common(struct file * filp, void * dirent, 212127932742SMiklos Szeredi filldir_t filldir, instantiate_t instantiate) 21221da177e4SLinus Torvalds { 21232fddfeefSJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 21245634708bSEric W. Biederman struct inode *inode = dentry->d_inode; 212599f89551SEric W. Biederman struct task_struct *p = get_proc_task(inode); 2126457c2510SPavel Emelyanov unsigned int fd, ino; 21271da177e4SLinus Torvalds int retval; 21281da177e4SLinus Torvalds struct files_struct * files; 21291da177e4SLinus Torvalds 21301da177e4SLinus Torvalds retval = -ENOENT; 213199f89551SEric W. Biederman if (!p) 213299f89551SEric W. Biederman goto out_no_task; 21331da177e4SLinus Torvalds retval = 0; 21341da177e4SLinus Torvalds 21351da177e4SLinus Torvalds fd = filp->f_pos; 21361da177e4SLinus Torvalds switch (fd) { 21371da177e4SLinus Torvalds case 0: 21381da177e4SLinus Torvalds if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0) 21391da177e4SLinus Torvalds goto out; 21401da177e4SLinus Torvalds filp->f_pos++; 21411da177e4SLinus Torvalds case 1: 21425634708bSEric W. Biederman ino = parent_ino(dentry); 21431da177e4SLinus Torvalds if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0) 21441da177e4SLinus Torvalds goto out; 21451da177e4SLinus Torvalds filp->f_pos++; 21461da177e4SLinus Torvalds default: 21471da177e4SLinus Torvalds files = get_files_struct(p); 21481da177e4SLinus Torvalds if (!files) 21491da177e4SLinus Torvalds goto out; 2150b835996fSDipankar Sarma rcu_read_lock(); 21511da177e4SLinus Torvalds for (fd = filp->f_pos-2; 21529b4f526cSAl Viro fd < files_fdtable(files)->max_fds; 21531da177e4SLinus Torvalds fd++, filp->f_pos++) { 215427932742SMiklos Szeredi char name[PROC_NUMBUF]; 215527932742SMiklos Szeredi int len; 21561da177e4SLinus Torvalds 21571da177e4SLinus Torvalds if (!fcheck_files(files, fd)) 21581da177e4SLinus Torvalds continue; 2159b835996fSDipankar Sarma rcu_read_unlock(); 21601da177e4SLinus Torvalds 216127932742SMiklos Szeredi len = snprintf(name, sizeof(name), "%d", fd); 216227932742SMiklos Szeredi if (proc_fill_cache(filp, dirent, filldir, 216327932742SMiklos Szeredi name, len, instantiate, 216427932742SMiklos Szeredi p, &fd) < 0) { 2165b835996fSDipankar Sarma rcu_read_lock(); 21661da177e4SLinus Torvalds break; 21671da177e4SLinus Torvalds } 2168b835996fSDipankar Sarma rcu_read_lock(); 21691da177e4SLinus Torvalds } 2170b835996fSDipankar Sarma rcu_read_unlock(); 21711da177e4SLinus Torvalds put_files_struct(files); 21721da177e4SLinus Torvalds } 21731da177e4SLinus Torvalds out: 217499f89551SEric W. Biederman put_task_struct(p); 217599f89551SEric W. Biederman out_no_task: 21761da177e4SLinus Torvalds return retval; 21771da177e4SLinus Torvalds } 21781da177e4SLinus Torvalds 217927932742SMiklos Szeredi static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry, 218027932742SMiklos Szeredi struct nameidata *nd) 218127932742SMiklos Szeredi { 218227932742SMiklos Szeredi return proc_lookupfd_common(dir, dentry, proc_fd_instantiate); 218327932742SMiklos Szeredi } 218427932742SMiklos Szeredi 218527932742SMiklos Szeredi static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir) 218627932742SMiklos Szeredi { 218727932742SMiklos Szeredi return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate); 218827932742SMiklos Szeredi } 218927932742SMiklos Szeredi 219027932742SMiklos Szeredi static ssize_t proc_fdinfo_read(struct file *file, char __user *buf, 219127932742SMiklos Szeredi size_t len, loff_t *ppos) 219227932742SMiklos Szeredi { 219327932742SMiklos Szeredi char tmp[PROC_FDINFO_MAX]; 21943dcd25f3SJan Blunck int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp); 219527932742SMiklos Szeredi if (!err) 219627932742SMiklos Szeredi err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp)); 219727932742SMiklos Szeredi return err; 219827932742SMiklos Szeredi } 219927932742SMiklos Szeredi 220027932742SMiklos Szeredi static const struct file_operations proc_fdinfo_file_operations = { 220127932742SMiklos Szeredi .open = nonseekable_open, 220227932742SMiklos Szeredi .read = proc_fdinfo_read, 22036038f373SArnd Bergmann .llseek = no_llseek, 220427932742SMiklos Szeredi }; 220527932742SMiklos Szeredi 220600977a59SArjan van de Ven static const struct file_operations proc_fd_operations = { 22071da177e4SLinus Torvalds .read = generic_read_dir, 22081da177e4SLinus Torvalds .readdir = proc_readfd, 22096038f373SArnd Bergmann .llseek = default_llseek, 22101da177e4SLinus Torvalds }; 22111da177e4SLinus Torvalds 22121da177e4SLinus Torvalds /* 22138948e11fSAlexey Dobriyan * /proc/pid/fd needs a special permission handler so that a process can still 22148948e11fSAlexey Dobriyan * access /proc/self/fd after it has executed a setuid(). 22158948e11fSAlexey Dobriyan */ 2216b74c79e9SNick Piggin static int proc_fd_permission(struct inode *inode, int mask, unsigned int flags) 22178948e11fSAlexey Dobriyan { 22188948e11fSAlexey Dobriyan int rv; 22198948e11fSAlexey Dobriyan 2220b74c79e9SNick Piggin if (flags & IPERM_FLAG_RCU) 2221b74c79e9SNick Piggin return -ECHILD; 2222b74c79e9SNick Piggin rv = generic_permission(inode, mask, flags, NULL); 22238948e11fSAlexey Dobriyan if (rv == 0) 22248948e11fSAlexey Dobriyan return 0; 22258948e11fSAlexey Dobriyan if (task_pid(current) == proc_pid(inode)) 22268948e11fSAlexey Dobriyan rv = 0; 22278948e11fSAlexey Dobriyan return rv; 22288948e11fSAlexey Dobriyan } 22298948e11fSAlexey Dobriyan 22308948e11fSAlexey Dobriyan /* 22311da177e4SLinus Torvalds * proc directories can do almost nothing.. 22321da177e4SLinus Torvalds */ 2233c5ef1c42SArjan van de Ven static const struct inode_operations proc_fd_inode_operations = { 22341da177e4SLinus Torvalds .lookup = proc_lookupfd, 22358948e11fSAlexey Dobriyan .permission = proc_fd_permission, 22366d76fa58SLinus Torvalds .setattr = proc_setattr, 22371da177e4SLinus Torvalds }; 22381da177e4SLinus Torvalds 223927932742SMiklos Szeredi static struct dentry *proc_fdinfo_instantiate(struct inode *dir, 224027932742SMiklos Szeredi struct dentry *dentry, struct task_struct *task, const void *ptr) 224127932742SMiklos Szeredi { 224227932742SMiklos Szeredi unsigned fd = *(unsigned *)ptr; 224327932742SMiklos Szeredi struct inode *inode; 224427932742SMiklos Szeredi struct proc_inode *ei; 224527932742SMiklos Szeredi struct dentry *error = ERR_PTR(-ENOENT); 224627932742SMiklos Szeredi 224727932742SMiklos Szeredi inode = proc_pid_make_inode(dir->i_sb, task); 224827932742SMiklos Szeredi if (!inode) 224927932742SMiklos Szeredi goto out; 225027932742SMiklos Szeredi ei = PROC_I(inode); 225127932742SMiklos Szeredi ei->fd = fd; 225227932742SMiklos Szeredi inode->i_mode = S_IFREG | S_IRUSR; 225327932742SMiklos Szeredi inode->i_fop = &proc_fdinfo_file_operations; 2254fb045adbSNick Piggin d_set_d_op(dentry, &tid_fd_dentry_operations); 225527932742SMiklos Szeredi d_add(dentry, inode); 225627932742SMiklos Szeredi /* Close the race of the process dying before we return the dentry */ 225727932742SMiklos Szeredi if (tid_fd_revalidate(dentry, NULL)) 225827932742SMiklos Szeredi error = NULL; 225927932742SMiklos Szeredi 226027932742SMiklos Szeredi out: 226127932742SMiklos Szeredi return error; 226227932742SMiklos Szeredi } 226327932742SMiklos Szeredi 226427932742SMiklos Szeredi static struct dentry *proc_lookupfdinfo(struct inode *dir, 226527932742SMiklos Szeredi struct dentry *dentry, 226627932742SMiklos Szeredi struct nameidata *nd) 226727932742SMiklos Szeredi { 226827932742SMiklos Szeredi return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate); 226927932742SMiklos Szeredi } 227027932742SMiklos Szeredi 227127932742SMiklos Szeredi static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir) 227227932742SMiklos Szeredi { 227327932742SMiklos Szeredi return proc_readfd_common(filp, dirent, filldir, 227427932742SMiklos Szeredi proc_fdinfo_instantiate); 227527932742SMiklos Szeredi } 227627932742SMiklos Szeredi 227727932742SMiklos Szeredi static const struct file_operations proc_fdinfo_operations = { 227827932742SMiklos Szeredi .read = generic_read_dir, 227927932742SMiklos Szeredi .readdir = proc_readfdinfo, 22806038f373SArnd Bergmann .llseek = default_llseek, 228127932742SMiklos Szeredi }; 228227932742SMiklos Szeredi 228327932742SMiklos Szeredi /* 228427932742SMiklos Szeredi * proc directories can do almost nothing.. 228527932742SMiklos Szeredi */ 228627932742SMiklos Szeredi static const struct inode_operations proc_fdinfo_inode_operations = { 228727932742SMiklos Szeredi .lookup = proc_lookupfdinfo, 228827932742SMiklos Szeredi .setattr = proc_setattr, 228927932742SMiklos Szeredi }; 229027932742SMiklos Szeredi 229127932742SMiklos Szeredi 2292444ceed8SEric W. Biederman static struct dentry *proc_pident_instantiate(struct inode *dir, 2293c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 2294444ceed8SEric W. Biederman { 2295c5141e6dSEric Dumazet const struct pid_entry *p = ptr; 2296444ceed8SEric W. Biederman struct inode *inode; 2297444ceed8SEric W. Biederman struct proc_inode *ei; 2298bd6daba9SKOSAKI Motohiro struct dentry *error = ERR_PTR(-ENOENT); 2299444ceed8SEric W. Biederman 230061a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 2301444ceed8SEric W. Biederman if (!inode) 2302444ceed8SEric W. Biederman goto out; 2303444ceed8SEric W. Biederman 2304444ceed8SEric W. Biederman ei = PROC_I(inode); 2305444ceed8SEric W. Biederman inode->i_mode = p->mode; 2306444ceed8SEric W. Biederman if (S_ISDIR(inode->i_mode)) 2307444ceed8SEric W. Biederman inode->i_nlink = 2; /* Use getattr to fix if necessary */ 2308444ceed8SEric W. Biederman if (p->iop) 2309444ceed8SEric W. Biederman inode->i_op = p->iop; 2310444ceed8SEric W. Biederman if (p->fop) 2311444ceed8SEric W. Biederman inode->i_fop = p->fop; 2312444ceed8SEric W. Biederman ei->op = p->op; 2313fb045adbSNick Piggin d_set_d_op(dentry, &pid_dentry_operations); 2314444ceed8SEric W. Biederman d_add(dentry, inode); 2315444ceed8SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 2316444ceed8SEric W. Biederman if (pid_revalidate(dentry, NULL)) 2317444ceed8SEric W. Biederman error = NULL; 2318444ceed8SEric W. Biederman out: 2319444ceed8SEric W. Biederman return error; 2320444ceed8SEric W. Biederman } 2321444ceed8SEric W. Biederman 23221da177e4SLinus Torvalds static struct dentry *proc_pident_lookup(struct inode *dir, 23231da177e4SLinus Torvalds struct dentry *dentry, 2324c5141e6dSEric Dumazet const struct pid_entry *ents, 23257bcd6b0eSEric W. Biederman unsigned int nents) 23261da177e4SLinus Torvalds { 2327cd6a3ce9SEric W. Biederman struct dentry *error; 232899f89551SEric W. Biederman struct task_struct *task = get_proc_task(dir); 2329c5141e6dSEric Dumazet const struct pid_entry *p, *last; 23301da177e4SLinus Torvalds 2331cd6a3ce9SEric W. Biederman error = ERR_PTR(-ENOENT); 23321da177e4SLinus Torvalds 233399f89551SEric W. Biederman if (!task) 233499f89551SEric W. Biederman goto out_no_task; 23351da177e4SLinus Torvalds 233620cdc894SEric W. Biederman /* 233720cdc894SEric W. Biederman * Yes, it does not scale. And it should not. Don't add 233820cdc894SEric W. Biederman * new entries into /proc/<tgid>/ without very good reasons. 233920cdc894SEric W. Biederman */ 23407bcd6b0eSEric W. Biederman last = &ents[nents - 1]; 23417bcd6b0eSEric W. Biederman for (p = ents; p <= last; p++) { 23421da177e4SLinus Torvalds if (p->len != dentry->d_name.len) 23431da177e4SLinus Torvalds continue; 23441da177e4SLinus Torvalds if (!memcmp(dentry->d_name.name, p->name, p->len)) 23451da177e4SLinus Torvalds break; 23461da177e4SLinus Torvalds } 23477bcd6b0eSEric W. Biederman if (p > last) 23481da177e4SLinus Torvalds goto out; 23491da177e4SLinus Torvalds 2350444ceed8SEric W. Biederman error = proc_pident_instantiate(dir, dentry, task, p); 23511da177e4SLinus Torvalds out: 235299f89551SEric W. Biederman put_task_struct(task); 235399f89551SEric W. Biederman out_no_task: 2354cd6a3ce9SEric W. Biederman return error; 23551da177e4SLinus Torvalds } 23561da177e4SLinus Torvalds 2357c5141e6dSEric Dumazet static int proc_pident_fill_cache(struct file *filp, void *dirent, 2358c5141e6dSEric Dumazet filldir_t filldir, struct task_struct *task, const struct pid_entry *p) 235961a28784SEric W. Biederman { 236061a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, p->name, p->len, 236161a28784SEric W. Biederman proc_pident_instantiate, task, p); 236261a28784SEric W. Biederman } 236361a28784SEric W. Biederman 236428a6d671SEric W. Biederman static int proc_pident_readdir(struct file *filp, 236528a6d671SEric W. Biederman void *dirent, filldir_t filldir, 2366c5141e6dSEric Dumazet const struct pid_entry *ents, unsigned int nents) 236728a6d671SEric W. Biederman { 236828a6d671SEric W. Biederman int i; 23692fddfeefSJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 237028a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 237128a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 2372c5141e6dSEric Dumazet const struct pid_entry *p, *last; 237328a6d671SEric W. Biederman ino_t ino; 237428a6d671SEric W. Biederman int ret; 237528a6d671SEric W. Biederman 237628a6d671SEric W. Biederman ret = -ENOENT; 237728a6d671SEric W. Biederman if (!task) 237861a28784SEric W. Biederman goto out_no_task; 237928a6d671SEric W. Biederman 238028a6d671SEric W. Biederman ret = 0; 238128a6d671SEric W. Biederman i = filp->f_pos; 238228a6d671SEric W. Biederman switch (i) { 238328a6d671SEric W. Biederman case 0: 238428a6d671SEric W. Biederman ino = inode->i_ino; 238528a6d671SEric W. Biederman if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0) 238628a6d671SEric W. Biederman goto out; 238728a6d671SEric W. Biederman i++; 238828a6d671SEric W. Biederman filp->f_pos++; 238928a6d671SEric W. Biederman /* fall through */ 239028a6d671SEric W. Biederman case 1: 239128a6d671SEric W. Biederman ino = parent_ino(dentry); 239228a6d671SEric W. Biederman if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0) 239328a6d671SEric W. Biederman goto out; 239428a6d671SEric W. Biederman i++; 239528a6d671SEric W. Biederman filp->f_pos++; 239628a6d671SEric W. Biederman /* fall through */ 239728a6d671SEric W. Biederman default: 239828a6d671SEric W. Biederman i -= 2; 239928a6d671SEric W. Biederman if (i >= nents) { 240028a6d671SEric W. Biederman ret = 1; 240128a6d671SEric W. Biederman goto out; 240228a6d671SEric W. Biederman } 240328a6d671SEric W. Biederman p = ents + i; 24047bcd6b0eSEric W. Biederman last = &ents[nents - 1]; 24057bcd6b0eSEric W. Biederman while (p <= last) { 240661a28784SEric W. Biederman if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0) 240728a6d671SEric W. Biederman goto out; 240828a6d671SEric W. Biederman filp->f_pos++; 240928a6d671SEric W. Biederman p++; 241028a6d671SEric W. Biederman } 24111da177e4SLinus Torvalds } 24121da177e4SLinus Torvalds 241328a6d671SEric W. Biederman ret = 1; 241428a6d671SEric W. Biederman out: 241561a28784SEric W. Biederman put_task_struct(task); 241661a28784SEric W. Biederman out_no_task: 241728a6d671SEric W. Biederman return ret; 24181da177e4SLinus Torvalds } 24191da177e4SLinus Torvalds 24201da177e4SLinus Torvalds #ifdef CONFIG_SECURITY 242128a6d671SEric W. Biederman static ssize_t proc_pid_attr_read(struct file * file, char __user * buf, 242228a6d671SEric W. Biederman size_t count, loff_t *ppos) 242328a6d671SEric W. Biederman { 24242fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 242504ff9708SAl Viro char *p = NULL; 242628a6d671SEric W. Biederman ssize_t length; 242728a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 242828a6d671SEric W. Biederman 242928a6d671SEric W. Biederman if (!task) 243004ff9708SAl Viro return -ESRCH; 243128a6d671SEric W. Biederman 243228a6d671SEric W. Biederman length = security_getprocattr(task, 24332fddfeefSJosef "Jeff" Sipek (char*)file->f_path.dentry->d_name.name, 243404ff9708SAl Viro &p); 243528a6d671SEric W. Biederman put_task_struct(task); 243604ff9708SAl Viro if (length > 0) 243704ff9708SAl Viro length = simple_read_from_buffer(buf, count, ppos, p, length); 243804ff9708SAl Viro kfree(p); 243928a6d671SEric W. Biederman return length; 244028a6d671SEric W. Biederman } 244128a6d671SEric W. Biederman 244228a6d671SEric W. Biederman static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf, 244328a6d671SEric W. Biederman size_t count, loff_t *ppos) 244428a6d671SEric W. Biederman { 24452fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 244628a6d671SEric W. Biederman char *page; 244728a6d671SEric W. Biederman ssize_t length; 244828a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 244928a6d671SEric W. Biederman 245028a6d671SEric W. Biederman length = -ESRCH; 245128a6d671SEric W. Biederman if (!task) 245228a6d671SEric W. Biederman goto out_no_task; 245328a6d671SEric W. Biederman if (count > PAGE_SIZE) 245428a6d671SEric W. Biederman count = PAGE_SIZE; 245528a6d671SEric W. Biederman 245628a6d671SEric W. Biederman /* No partial writes. */ 245728a6d671SEric W. Biederman length = -EINVAL; 245828a6d671SEric W. Biederman if (*ppos != 0) 245928a6d671SEric W. Biederman goto out; 246028a6d671SEric W. Biederman 246128a6d671SEric W. Biederman length = -ENOMEM; 2462e12ba74dSMel Gorman page = (char*)__get_free_page(GFP_TEMPORARY); 246328a6d671SEric W. Biederman if (!page) 246428a6d671SEric W. Biederman goto out; 246528a6d671SEric W. Biederman 246628a6d671SEric W. Biederman length = -EFAULT; 246728a6d671SEric W. Biederman if (copy_from_user(page, buf, count)) 246828a6d671SEric W. Biederman goto out_free; 246928a6d671SEric W. Biederman 2470107db7c7SDavid Howells /* Guard against adverse ptrace interaction */ 24719b1bf12dSKOSAKI Motohiro length = mutex_lock_interruptible(&task->signal->cred_guard_mutex); 2472107db7c7SDavid Howells if (length < 0) 2473107db7c7SDavid Howells goto out_free; 2474107db7c7SDavid Howells 247528a6d671SEric W. Biederman length = security_setprocattr(task, 24762fddfeefSJosef "Jeff" Sipek (char*)file->f_path.dentry->d_name.name, 247728a6d671SEric W. Biederman (void*)page, count); 24789b1bf12dSKOSAKI Motohiro mutex_unlock(&task->signal->cred_guard_mutex); 247928a6d671SEric W. Biederman out_free: 248028a6d671SEric W. Biederman free_page((unsigned long) page); 248128a6d671SEric W. Biederman out: 248228a6d671SEric W. Biederman put_task_struct(task); 248328a6d671SEric W. Biederman out_no_task: 248428a6d671SEric W. Biederman return length; 248528a6d671SEric W. Biederman } 248628a6d671SEric W. Biederman 248700977a59SArjan van de Ven static const struct file_operations proc_pid_attr_operations = { 248828a6d671SEric W. Biederman .read = proc_pid_attr_read, 248928a6d671SEric W. Biederman .write = proc_pid_attr_write, 249087df8424SArnd Bergmann .llseek = generic_file_llseek, 249128a6d671SEric W. Biederman }; 249228a6d671SEric W. Biederman 2493c5141e6dSEric Dumazet static const struct pid_entry attr_dir_stuff[] = { 2494631f9c18SAlexey Dobriyan REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations), 2495631f9c18SAlexey Dobriyan REG("prev", S_IRUGO, proc_pid_attr_operations), 2496631f9c18SAlexey Dobriyan REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations), 2497631f9c18SAlexey Dobriyan REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations), 2498631f9c18SAlexey Dobriyan REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations), 2499631f9c18SAlexey Dobriyan REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations), 250028a6d671SEric W. Biederman }; 250128a6d671SEric W. Biederman 250272d9dcfcSEric W. Biederman static int proc_attr_dir_readdir(struct file * filp, 25031da177e4SLinus Torvalds void * dirent, filldir_t filldir) 25041da177e4SLinus Torvalds { 25051da177e4SLinus Torvalds return proc_pident_readdir(filp,dirent,filldir, 250672d9dcfcSEric W. Biederman attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff)); 25071da177e4SLinus Torvalds } 25081da177e4SLinus Torvalds 250900977a59SArjan van de Ven static const struct file_operations proc_attr_dir_operations = { 25101da177e4SLinus Torvalds .read = generic_read_dir, 251172d9dcfcSEric W. Biederman .readdir = proc_attr_dir_readdir, 25126038f373SArnd Bergmann .llseek = default_llseek, 25131da177e4SLinus Torvalds }; 25141da177e4SLinus Torvalds 251572d9dcfcSEric W. Biederman static struct dentry *proc_attr_dir_lookup(struct inode *dir, 25161da177e4SLinus Torvalds struct dentry *dentry, struct nameidata *nd) 25171da177e4SLinus Torvalds { 25187bcd6b0eSEric W. Biederman return proc_pident_lookup(dir, dentry, 25197bcd6b0eSEric W. Biederman attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff)); 25201da177e4SLinus Torvalds } 25211da177e4SLinus Torvalds 2522c5ef1c42SArjan van de Ven static const struct inode_operations proc_attr_dir_inode_operations = { 252372d9dcfcSEric W. Biederman .lookup = proc_attr_dir_lookup, 252499f89551SEric W. Biederman .getattr = pid_getattr, 25256d76fa58SLinus Torvalds .setattr = proc_setattr, 25261da177e4SLinus Torvalds }; 25271da177e4SLinus Torvalds 25281da177e4SLinus Torvalds #endif 25291da177e4SLinus Torvalds 2530698ba7b5SChristoph Hellwig #ifdef CONFIG_ELF_CORE 25313cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf, 25323cb4a0bbSKawai, Hidehiro size_t count, loff_t *ppos) 25333cb4a0bbSKawai, Hidehiro { 25343cb4a0bbSKawai, Hidehiro struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 25353cb4a0bbSKawai, Hidehiro struct mm_struct *mm; 25363cb4a0bbSKawai, Hidehiro char buffer[PROC_NUMBUF]; 25373cb4a0bbSKawai, Hidehiro size_t len; 25383cb4a0bbSKawai, Hidehiro int ret; 25393cb4a0bbSKawai, Hidehiro 25403cb4a0bbSKawai, Hidehiro if (!task) 25413cb4a0bbSKawai, Hidehiro return -ESRCH; 25423cb4a0bbSKawai, Hidehiro 25433cb4a0bbSKawai, Hidehiro ret = 0; 25443cb4a0bbSKawai, Hidehiro mm = get_task_mm(task); 25453cb4a0bbSKawai, Hidehiro if (mm) { 25463cb4a0bbSKawai, Hidehiro len = snprintf(buffer, sizeof(buffer), "%08lx\n", 25473cb4a0bbSKawai, Hidehiro ((mm->flags & MMF_DUMP_FILTER_MASK) >> 25483cb4a0bbSKawai, Hidehiro MMF_DUMP_FILTER_SHIFT)); 25493cb4a0bbSKawai, Hidehiro mmput(mm); 25503cb4a0bbSKawai, Hidehiro ret = simple_read_from_buffer(buf, count, ppos, buffer, len); 25513cb4a0bbSKawai, Hidehiro } 25523cb4a0bbSKawai, Hidehiro 25533cb4a0bbSKawai, Hidehiro put_task_struct(task); 25543cb4a0bbSKawai, Hidehiro 25553cb4a0bbSKawai, Hidehiro return ret; 25563cb4a0bbSKawai, Hidehiro } 25573cb4a0bbSKawai, Hidehiro 25583cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_write(struct file *file, 25593cb4a0bbSKawai, Hidehiro const char __user *buf, 25603cb4a0bbSKawai, Hidehiro size_t count, 25613cb4a0bbSKawai, Hidehiro loff_t *ppos) 25623cb4a0bbSKawai, Hidehiro { 25633cb4a0bbSKawai, Hidehiro struct task_struct *task; 25643cb4a0bbSKawai, Hidehiro struct mm_struct *mm; 25653cb4a0bbSKawai, Hidehiro char buffer[PROC_NUMBUF], *end; 25663cb4a0bbSKawai, Hidehiro unsigned int val; 25673cb4a0bbSKawai, Hidehiro int ret; 25683cb4a0bbSKawai, Hidehiro int i; 25693cb4a0bbSKawai, Hidehiro unsigned long mask; 25703cb4a0bbSKawai, Hidehiro 25713cb4a0bbSKawai, Hidehiro ret = -EFAULT; 25723cb4a0bbSKawai, Hidehiro memset(buffer, 0, sizeof(buffer)); 25733cb4a0bbSKawai, Hidehiro if (count > sizeof(buffer) - 1) 25743cb4a0bbSKawai, Hidehiro count = sizeof(buffer) - 1; 25753cb4a0bbSKawai, Hidehiro if (copy_from_user(buffer, buf, count)) 25763cb4a0bbSKawai, Hidehiro goto out_no_task; 25773cb4a0bbSKawai, Hidehiro 25783cb4a0bbSKawai, Hidehiro ret = -EINVAL; 25793cb4a0bbSKawai, Hidehiro val = (unsigned int)simple_strtoul(buffer, &end, 0); 25803cb4a0bbSKawai, Hidehiro if (*end == '\n') 25813cb4a0bbSKawai, Hidehiro end++; 25823cb4a0bbSKawai, Hidehiro if (end - buffer == 0) 25833cb4a0bbSKawai, Hidehiro goto out_no_task; 25843cb4a0bbSKawai, Hidehiro 25853cb4a0bbSKawai, Hidehiro ret = -ESRCH; 25863cb4a0bbSKawai, Hidehiro task = get_proc_task(file->f_dentry->d_inode); 25873cb4a0bbSKawai, Hidehiro if (!task) 25883cb4a0bbSKawai, Hidehiro goto out_no_task; 25893cb4a0bbSKawai, Hidehiro 25903cb4a0bbSKawai, Hidehiro ret = end - buffer; 25913cb4a0bbSKawai, Hidehiro mm = get_task_mm(task); 25923cb4a0bbSKawai, Hidehiro if (!mm) 25933cb4a0bbSKawai, Hidehiro goto out_no_mm; 25943cb4a0bbSKawai, Hidehiro 25953cb4a0bbSKawai, Hidehiro for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) { 25963cb4a0bbSKawai, Hidehiro if (val & mask) 25973cb4a0bbSKawai, Hidehiro set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags); 25983cb4a0bbSKawai, Hidehiro else 25993cb4a0bbSKawai, Hidehiro clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags); 26003cb4a0bbSKawai, Hidehiro } 26013cb4a0bbSKawai, Hidehiro 26023cb4a0bbSKawai, Hidehiro mmput(mm); 26033cb4a0bbSKawai, Hidehiro out_no_mm: 26043cb4a0bbSKawai, Hidehiro put_task_struct(task); 26053cb4a0bbSKawai, Hidehiro out_no_task: 26063cb4a0bbSKawai, Hidehiro return ret; 26073cb4a0bbSKawai, Hidehiro } 26083cb4a0bbSKawai, Hidehiro 26093cb4a0bbSKawai, Hidehiro static const struct file_operations proc_coredump_filter_operations = { 26103cb4a0bbSKawai, Hidehiro .read = proc_coredump_filter_read, 26113cb4a0bbSKawai, Hidehiro .write = proc_coredump_filter_write, 261287df8424SArnd Bergmann .llseek = generic_file_llseek, 26133cb4a0bbSKawai, Hidehiro }; 26143cb4a0bbSKawai, Hidehiro #endif 26153cb4a0bbSKawai, Hidehiro 26161da177e4SLinus Torvalds /* 26171da177e4SLinus Torvalds * /proc/self: 26181da177e4SLinus Torvalds */ 26191da177e4SLinus Torvalds static int proc_self_readlink(struct dentry *dentry, char __user *buffer, 26201da177e4SLinus Torvalds int buflen) 26211da177e4SLinus Torvalds { 2622488e5bc4SEric W. Biederman struct pid_namespace *ns = dentry->d_sb->s_fs_info; 2623b55fcb22SAndrew Morton pid_t tgid = task_tgid_nr_ns(current, ns); 26248578cea7SEric W. Biederman char tmp[PROC_NUMBUF]; 2625b55fcb22SAndrew Morton if (!tgid) 2626488e5bc4SEric W. Biederman return -ENOENT; 2627b55fcb22SAndrew Morton sprintf(tmp, "%d", tgid); 26281da177e4SLinus Torvalds return vfs_readlink(dentry,buffer,buflen,tmp); 26291da177e4SLinus Torvalds } 26301da177e4SLinus Torvalds 2631008b150aSAl Viro static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd) 26321da177e4SLinus Torvalds { 2633488e5bc4SEric W. Biederman struct pid_namespace *ns = dentry->d_sb->s_fs_info; 2634b55fcb22SAndrew Morton pid_t tgid = task_tgid_nr_ns(current, ns); 26357fee4868SAl Viro char *name = ERR_PTR(-ENOENT); 26367fee4868SAl Viro if (tgid) { 26377fee4868SAl Viro name = __getname(); 26387fee4868SAl Viro if (!name) 26397fee4868SAl Viro name = ERR_PTR(-ENOMEM); 26407fee4868SAl Viro else 26417fee4868SAl Viro sprintf(name, "%d", tgid); 26427fee4868SAl Viro } 26437fee4868SAl Viro nd_set_link(nd, name); 26447fee4868SAl Viro return NULL; 26457fee4868SAl Viro } 26467fee4868SAl Viro 26477fee4868SAl Viro static void proc_self_put_link(struct dentry *dentry, struct nameidata *nd, 26487fee4868SAl Viro void *cookie) 26497fee4868SAl Viro { 26507fee4868SAl Viro char *s = nd_get_link(nd); 26517fee4868SAl Viro if (!IS_ERR(s)) 26527fee4868SAl Viro __putname(s); 26531da177e4SLinus Torvalds } 26541da177e4SLinus Torvalds 2655c5ef1c42SArjan van de Ven static const struct inode_operations proc_self_inode_operations = { 26561da177e4SLinus Torvalds .readlink = proc_self_readlink, 26571da177e4SLinus Torvalds .follow_link = proc_self_follow_link, 26587fee4868SAl Viro .put_link = proc_self_put_link, 26591da177e4SLinus Torvalds }; 26601da177e4SLinus Torvalds 266128a6d671SEric W. Biederman /* 2662801199ceSEric W. Biederman * proc base 2663801199ceSEric W. Biederman * 2664801199ceSEric W. Biederman * These are the directory entries in the root directory of /proc 2665801199ceSEric W. Biederman * that properly belong to the /proc filesystem, as they describe 2666801199ceSEric W. Biederman * describe something that is process related. 2667801199ceSEric W. Biederman */ 2668c5141e6dSEric Dumazet static const struct pid_entry proc_base_stuff[] = { 266961a28784SEric W. Biederman NOD("self", S_IFLNK|S_IRWXUGO, 2670801199ceSEric W. Biederman &proc_self_inode_operations, NULL, {}), 2671801199ceSEric W. Biederman }; 2672801199ceSEric W. Biederman 2673444ceed8SEric W. Biederman static struct dentry *proc_base_instantiate(struct inode *dir, 2674c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 2675801199ceSEric W. Biederman { 2676c5141e6dSEric Dumazet const struct pid_entry *p = ptr; 2677801199ceSEric W. Biederman struct inode *inode; 2678801199ceSEric W. Biederman struct proc_inode *ei; 267973d36460SDan Carpenter struct dentry *error; 2680801199ceSEric W. Biederman 2681801199ceSEric W. Biederman /* Allocate the inode */ 2682801199ceSEric W. Biederman error = ERR_PTR(-ENOMEM); 2683801199ceSEric W. Biederman inode = new_inode(dir->i_sb); 2684801199ceSEric W. Biederman if (!inode) 2685801199ceSEric W. Biederman goto out; 2686801199ceSEric W. Biederman 2687801199ceSEric W. Biederman /* Initialize the inode */ 2688801199ceSEric W. Biederman ei = PROC_I(inode); 268985fe4025SChristoph Hellwig inode->i_ino = get_next_ino(); 2690801199ceSEric W. Biederman inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; 2691801199ceSEric W. Biederman 2692801199ceSEric W. Biederman /* 2693801199ceSEric W. Biederman * grab the reference to the task. 2694801199ceSEric W. Biederman */ 26951a657f78SOleg Nesterov ei->pid = get_task_pid(task, PIDTYPE_PID); 2696801199ceSEric W. Biederman if (!ei->pid) 2697801199ceSEric W. Biederman goto out_iput; 2698801199ceSEric W. Biederman 2699801199ceSEric W. Biederman inode->i_mode = p->mode; 2700801199ceSEric W. Biederman if (S_ISDIR(inode->i_mode)) 2701801199ceSEric W. Biederman inode->i_nlink = 2; 2702801199ceSEric W. Biederman if (S_ISLNK(inode->i_mode)) 2703801199ceSEric W. Biederman inode->i_size = 64; 2704801199ceSEric W. Biederman if (p->iop) 2705801199ceSEric W. Biederman inode->i_op = p->iop; 2706801199ceSEric W. Biederman if (p->fop) 2707801199ceSEric W. Biederman inode->i_fop = p->fop; 2708801199ceSEric W. Biederman ei->op = p->op; 2709801199ceSEric W. Biederman d_add(dentry, inode); 2710801199ceSEric W. Biederman error = NULL; 2711801199ceSEric W. Biederman out: 2712801199ceSEric W. Biederman return error; 2713801199ceSEric W. Biederman out_iput: 2714801199ceSEric W. Biederman iput(inode); 2715801199ceSEric W. Biederman goto out; 2716801199ceSEric W. Biederman } 2717801199ceSEric W. Biederman 2718444ceed8SEric W. Biederman static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry) 2719444ceed8SEric W. Biederman { 2720444ceed8SEric W. Biederman struct dentry *error; 2721444ceed8SEric W. Biederman struct task_struct *task = get_proc_task(dir); 2722c5141e6dSEric Dumazet const struct pid_entry *p, *last; 2723444ceed8SEric W. Biederman 2724444ceed8SEric W. Biederman error = ERR_PTR(-ENOENT); 2725444ceed8SEric W. Biederman 2726444ceed8SEric W. Biederman if (!task) 2727444ceed8SEric W. Biederman goto out_no_task; 2728444ceed8SEric W. Biederman 2729444ceed8SEric W. Biederman /* Lookup the directory entry */ 27307bcd6b0eSEric W. Biederman last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1]; 27317bcd6b0eSEric W. Biederman for (p = proc_base_stuff; p <= last; p++) { 2732444ceed8SEric W. Biederman if (p->len != dentry->d_name.len) 2733444ceed8SEric W. Biederman continue; 2734444ceed8SEric W. Biederman if (!memcmp(dentry->d_name.name, p->name, p->len)) 2735444ceed8SEric W. Biederman break; 2736444ceed8SEric W. Biederman } 27377bcd6b0eSEric W. Biederman if (p > last) 2738444ceed8SEric W. Biederman goto out; 2739444ceed8SEric W. Biederman 2740444ceed8SEric W. Biederman error = proc_base_instantiate(dir, dentry, task, p); 2741444ceed8SEric W. Biederman 2742444ceed8SEric W. Biederman out: 2743444ceed8SEric W. Biederman put_task_struct(task); 2744444ceed8SEric W. Biederman out_no_task: 2745444ceed8SEric W. Biederman return error; 2746444ceed8SEric W. Biederman } 2747444ceed8SEric W. Biederman 2748c5141e6dSEric Dumazet static int proc_base_fill_cache(struct file *filp, void *dirent, 2749c5141e6dSEric Dumazet filldir_t filldir, struct task_struct *task, const struct pid_entry *p) 275061a28784SEric W. Biederman { 275161a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, p->name, p->len, 275261a28784SEric W. Biederman proc_base_instantiate, task, p); 275361a28784SEric W. Biederman } 275461a28784SEric W. Biederman 2755aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING 2756297c5d92SAndrea Righi static int do_io_accounting(struct task_struct *task, char *buffer, int whole) 2757aba76fdbSAndrew Morton { 2758940389b8SAndrea Righi struct task_io_accounting acct = task->ioac; 2759297c5d92SAndrea Righi unsigned long flags; 2760297c5d92SAndrea Righi 27615995477aSAndrea Righi if (whole && lock_task_sighand(task, &flags)) { 2762b2d002dbSAndrea Righi struct task_struct *t = task; 2763297c5d92SAndrea Righi 27645995477aSAndrea Righi task_io_accounting_add(&acct, &task->signal->ioac); 27655995477aSAndrea Righi while_each_thread(task, t) 27665995477aSAndrea Righi task_io_accounting_add(&acct, &t->ioac); 2767297c5d92SAndrea Righi 2768297c5d92SAndrea Righi unlock_task_sighand(task, &flags); 2769297c5d92SAndrea Righi } 2770aba76fdbSAndrew Morton return sprintf(buffer, 2771aba76fdbSAndrew Morton "rchar: %llu\n" 2772aba76fdbSAndrew Morton "wchar: %llu\n" 2773aba76fdbSAndrew Morton "syscr: %llu\n" 2774aba76fdbSAndrew Morton "syscw: %llu\n" 2775aba76fdbSAndrew Morton "read_bytes: %llu\n" 2776aba76fdbSAndrew Morton "write_bytes: %llu\n" 2777aba76fdbSAndrew Morton "cancelled_write_bytes: %llu\n", 27787c44319dSAlexander Beregalov (unsigned long long)acct.rchar, 27797c44319dSAlexander Beregalov (unsigned long long)acct.wchar, 27807c44319dSAlexander Beregalov (unsigned long long)acct.syscr, 27817c44319dSAlexander Beregalov (unsigned long long)acct.syscw, 27827c44319dSAlexander Beregalov (unsigned long long)acct.read_bytes, 27837c44319dSAlexander Beregalov (unsigned long long)acct.write_bytes, 27847c44319dSAlexander Beregalov (unsigned long long)acct.cancelled_write_bytes); 2785aba76fdbSAndrew Morton } 2786297c5d92SAndrea Righi 2787297c5d92SAndrea Righi static int proc_tid_io_accounting(struct task_struct *task, char *buffer) 2788297c5d92SAndrea Righi { 2789297c5d92SAndrea Righi return do_io_accounting(task, buffer, 0); 2790297c5d92SAndrea Righi } 2791297c5d92SAndrea Righi 2792297c5d92SAndrea Righi static int proc_tgid_io_accounting(struct task_struct *task, char *buffer) 2793297c5d92SAndrea Righi { 2794297c5d92SAndrea Righi return do_io_accounting(task, buffer, 1); 2795297c5d92SAndrea Righi } 2796297c5d92SAndrea Righi #endif /* CONFIG_TASK_IO_ACCOUNTING */ 2797aba76fdbSAndrew Morton 279847830723SKees Cook static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns, 279947830723SKees Cook struct pid *pid, struct task_struct *task) 280047830723SKees Cook { 2801a9712bc1SAl Viro int err = lock_trace(task); 2802a9712bc1SAl Viro if (!err) { 280347830723SKees Cook seq_printf(m, "%08x\n", task->personality); 2804a9712bc1SAl Viro unlock_trace(task); 2805a9712bc1SAl Viro } 2806a9712bc1SAl Viro return err; 280747830723SKees Cook } 280847830723SKees Cook 2809801199ceSEric W. Biederman /* 281028a6d671SEric W. Biederman * Thread groups 281128a6d671SEric W. Biederman */ 281200977a59SArjan van de Ven static const struct file_operations proc_task_operations; 2813c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations; 281420cdc894SEric W. Biederman 2815c5141e6dSEric Dumazet static const struct pid_entry tgid_base_stuff[] = { 2816631f9c18SAlexey Dobriyan DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations), 2817631f9c18SAlexey Dobriyan DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations), 2818631f9c18SAlexey Dobriyan DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations), 2819*6b4e306aSEric W. Biederman DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations), 2820b2211a36SAndrew Morton #ifdef CONFIG_NET 2821631f9c18SAlexey Dobriyan DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations), 2822b2211a36SAndrew Morton #endif 2823631f9c18SAlexey Dobriyan REG("environ", S_IRUSR, proc_environ_operations), 2824631f9c18SAlexey Dobriyan INF("auxv", S_IRUSR, proc_pid_auxv), 2825631f9c18SAlexey Dobriyan ONE("status", S_IRUGO, proc_pid_status), 2826a9712bc1SAl Viro ONE("personality", S_IRUGO, proc_pid_personality), 28273036e7b4SJiri Olsa INF("limits", S_IRUGO, proc_pid_limits), 282843ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG 2829631f9c18SAlexey Dobriyan REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations), 283043ae34cbSIngo Molnar #endif 28315091faa4SMike Galbraith #ifdef CONFIG_SCHED_AUTOGROUP 28325091faa4SMike Galbraith REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations), 28335091faa4SMike Galbraith #endif 28344614a696Sjohn stultz REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations), 2835ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK 2836a9712bc1SAl Viro INF("syscall", S_IRUGO, proc_pid_syscall), 2837ebcb6734SRoland McGrath #endif 2838631f9c18SAlexey Dobriyan INF("cmdline", S_IRUGO, proc_pid_cmdline), 2839631f9c18SAlexey Dobriyan ONE("stat", S_IRUGO, proc_tgid_stat), 2840631f9c18SAlexey Dobriyan ONE("statm", S_IRUGO, proc_pid_statm), 2841631f9c18SAlexey Dobriyan REG("maps", S_IRUGO, proc_maps_operations), 284228a6d671SEric W. Biederman #ifdef CONFIG_NUMA 2843631f9c18SAlexey Dobriyan REG("numa_maps", S_IRUGO, proc_numa_maps_operations), 284428a6d671SEric W. Biederman #endif 2845631f9c18SAlexey Dobriyan REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations), 2846631f9c18SAlexey Dobriyan LNK("cwd", proc_cwd_link), 2847631f9c18SAlexey Dobriyan LNK("root", proc_root_link), 2848631f9c18SAlexey Dobriyan LNK("exe", proc_exe_link), 2849631f9c18SAlexey Dobriyan REG("mounts", S_IRUGO, proc_mounts_operations), 2850631f9c18SAlexey Dobriyan REG("mountinfo", S_IRUGO, proc_mountinfo_operations), 2851631f9c18SAlexey Dobriyan REG("mountstats", S_IRUSR, proc_mountstats_operations), 28521e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR 2853631f9c18SAlexey Dobriyan REG("clear_refs", S_IWUSR, proc_clear_refs_operations), 2854631f9c18SAlexey Dobriyan REG("smaps", S_IRUGO, proc_smaps_operations), 2855ca6b0bf0SAl Viro REG("pagemap", S_IRUGO, proc_pagemap_operations), 285628a6d671SEric W. Biederman #endif 285728a6d671SEric W. Biederman #ifdef CONFIG_SECURITY 2858631f9c18SAlexey Dobriyan DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations), 285928a6d671SEric W. Biederman #endif 286028a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS 2861631f9c18SAlexey Dobriyan INF("wchan", S_IRUGO, proc_pid_wchan), 286228a6d671SEric W. Biederman #endif 28632ec220e2SKen Chen #ifdef CONFIG_STACKTRACE 2864a9712bc1SAl Viro ONE("stack", S_IRUGO, proc_pid_stack), 286528a6d671SEric W. Biederman #endif 286628a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS 2867631f9c18SAlexey Dobriyan INF("schedstat", S_IRUGO, proc_pid_schedstat), 286828a6d671SEric W. Biederman #endif 28699745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP 2870631f9c18SAlexey Dobriyan REG("latency", S_IRUGO, proc_lstats_operations), 28719745512cSArjan van de Ven #endif 28728793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET 2873631f9c18SAlexey Dobriyan REG("cpuset", S_IRUGO, proc_cpuset_operations), 287428a6d671SEric W. Biederman #endif 2875a424316cSPaul Menage #ifdef CONFIG_CGROUPS 2876631f9c18SAlexey Dobriyan REG("cgroup", S_IRUGO, proc_cgroup_operations), 2877a424316cSPaul Menage #endif 2878631f9c18SAlexey Dobriyan INF("oom_score", S_IRUGO, proc_oom_score), 2879631f9c18SAlexey Dobriyan REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations), 2880a63d83f4SDavid Rientjes REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations), 288128a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL 2882631f9c18SAlexey Dobriyan REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations), 2883631f9c18SAlexey Dobriyan REG("sessionid", S_IRUGO, proc_sessionid_operations), 288428a6d671SEric W. Biederman #endif 2885f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION 2886631f9c18SAlexey Dobriyan REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations), 2887f4f154fdSAkinobu Mita #endif 2888698ba7b5SChristoph Hellwig #ifdef CONFIG_ELF_CORE 2889631f9c18SAlexey Dobriyan REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations), 28903cb4a0bbSKawai, Hidehiro #endif 2891aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING 2892631f9c18SAlexey Dobriyan INF("io", S_IRUGO, proc_tgid_io_accounting), 2893aba76fdbSAndrew Morton #endif 289428a6d671SEric W. Biederman }; 289528a6d671SEric W. Biederman 289628a6d671SEric W. Biederman static int proc_tgid_base_readdir(struct file * filp, 289728a6d671SEric W. Biederman void * dirent, filldir_t filldir) 289828a6d671SEric W. Biederman { 289928a6d671SEric W. Biederman return proc_pident_readdir(filp,dirent,filldir, 290028a6d671SEric W. Biederman tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff)); 290128a6d671SEric W. Biederman } 290228a6d671SEric W. Biederman 290300977a59SArjan van de Ven static const struct file_operations proc_tgid_base_operations = { 290428a6d671SEric W. Biederman .read = generic_read_dir, 290528a6d671SEric W. Biederman .readdir = proc_tgid_base_readdir, 29066038f373SArnd Bergmann .llseek = default_llseek, 290728a6d671SEric W. Biederman }; 290828a6d671SEric W. Biederman 290928a6d671SEric W. Biederman static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){ 29107bcd6b0eSEric W. Biederman return proc_pident_lookup(dir, dentry, 29117bcd6b0eSEric W. Biederman tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff)); 291228a6d671SEric W. Biederman } 291328a6d671SEric W. Biederman 2914c5ef1c42SArjan van de Ven static const struct inode_operations proc_tgid_base_inode_operations = { 291528a6d671SEric W. Biederman .lookup = proc_tgid_base_lookup, 291628a6d671SEric W. Biederman .getattr = pid_getattr, 291728a6d671SEric W. Biederman .setattr = proc_setattr, 291828a6d671SEric W. Biederman }; 291928a6d671SEric W. Biederman 292060347f67SPavel Emelyanov static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid) 29211da177e4SLinus Torvalds { 292248e6484dSEric W. Biederman struct dentry *dentry, *leader, *dir; 29238578cea7SEric W. Biederman char buf[PROC_NUMBUF]; 292448e6484dSEric W. Biederman struct qstr name; 29251da177e4SLinus Torvalds 292648e6484dSEric W. Biederman name.name = buf; 292760347f67SPavel Emelyanov name.len = snprintf(buf, sizeof(buf), "%d", pid); 292860347f67SPavel Emelyanov dentry = d_hash_and_lookup(mnt->mnt_root, &name); 292948e6484dSEric W. Biederman if (dentry) { 293048e6484dSEric W. Biederman shrink_dcache_parent(dentry); 293148e6484dSEric W. Biederman d_drop(dentry); 293248e6484dSEric W. Biederman dput(dentry); 29331da177e4SLinus Torvalds } 29341da177e4SLinus Torvalds 293548e6484dSEric W. Biederman name.name = buf; 293660347f67SPavel Emelyanov name.len = snprintf(buf, sizeof(buf), "%d", tgid); 293760347f67SPavel Emelyanov leader = d_hash_and_lookup(mnt->mnt_root, &name); 293848e6484dSEric W. Biederman if (!leader) 293948e6484dSEric W. Biederman goto out; 294048e6484dSEric W. Biederman 294148e6484dSEric W. Biederman name.name = "task"; 294248e6484dSEric W. Biederman name.len = strlen(name.name); 294348e6484dSEric W. Biederman dir = d_hash_and_lookup(leader, &name); 294448e6484dSEric W. Biederman if (!dir) 294548e6484dSEric W. Biederman goto out_put_leader; 294648e6484dSEric W. Biederman 294748e6484dSEric W. Biederman name.name = buf; 294860347f67SPavel Emelyanov name.len = snprintf(buf, sizeof(buf), "%d", pid); 294948e6484dSEric W. Biederman dentry = d_hash_and_lookup(dir, &name); 295048e6484dSEric W. Biederman if (dentry) { 295148e6484dSEric W. Biederman shrink_dcache_parent(dentry); 295248e6484dSEric W. Biederman d_drop(dentry); 295348e6484dSEric W. Biederman dput(dentry); 29541da177e4SLinus Torvalds } 295548e6484dSEric W. Biederman 295648e6484dSEric W. Biederman dput(dir); 295748e6484dSEric W. Biederman out_put_leader: 295848e6484dSEric W. Biederman dput(leader); 295948e6484dSEric W. Biederman out: 296048e6484dSEric W. Biederman return; 29611da177e4SLinus Torvalds } 29621da177e4SLinus Torvalds 29630895e91dSRandy Dunlap /** 29640895e91dSRandy Dunlap * proc_flush_task - Remove dcache entries for @task from the /proc dcache. 29650895e91dSRandy Dunlap * @task: task that should be flushed. 29660895e91dSRandy Dunlap * 29670895e91dSRandy Dunlap * When flushing dentries from proc, one needs to flush them from global 296860347f67SPavel Emelyanov * proc (proc_mnt) and from all the namespaces' procs this task was seen 29690895e91dSRandy Dunlap * in. This call is supposed to do all of this job. 29700895e91dSRandy Dunlap * 29710895e91dSRandy Dunlap * Looks in the dcache for 29720895e91dSRandy Dunlap * /proc/@pid 29730895e91dSRandy Dunlap * /proc/@tgid/task/@pid 29740895e91dSRandy Dunlap * if either directory is present flushes it and all of it'ts children 29750895e91dSRandy Dunlap * from the dcache. 29760895e91dSRandy Dunlap * 29770895e91dSRandy Dunlap * It is safe and reasonable to cache /proc entries for a task until 29780895e91dSRandy Dunlap * that task exits. After that they just clog up the dcache with 29790895e91dSRandy Dunlap * useless entries, possibly causing useful dcache entries to be 29800895e91dSRandy Dunlap * flushed instead. This routine is proved to flush those useless 29810895e91dSRandy Dunlap * dcache entries at process exit time. 29820895e91dSRandy Dunlap * 29830895e91dSRandy Dunlap * NOTE: This routine is just an optimization so it does not guarantee 29840895e91dSRandy Dunlap * that no dcache entries will exist at process exit time it 29850895e91dSRandy Dunlap * just makes it very unlikely that any will persist. 298660347f67SPavel Emelyanov */ 298760347f67SPavel Emelyanov 298860347f67SPavel Emelyanov void proc_flush_task(struct task_struct *task) 298960347f67SPavel Emelyanov { 29909fcc2d15SEric W. Biederman int i; 29919b4d1cbeSOleg Nesterov struct pid *pid, *tgid; 2992130f77ecSPavel Emelyanov struct upid *upid; 2993130f77ecSPavel Emelyanov 2994130f77ecSPavel Emelyanov pid = task_pid(task); 2995130f77ecSPavel Emelyanov tgid = task_tgid(task); 29969fcc2d15SEric W. Biederman 29979fcc2d15SEric W. Biederman for (i = 0; i <= pid->level; i++) { 2998130f77ecSPavel Emelyanov upid = &pid->numbers[i]; 2999130f77ecSPavel Emelyanov proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr, 30009b4d1cbeSOleg Nesterov tgid->numbers[i].nr); 3001130f77ecSPavel Emelyanov } 30026f4e6433SPavel Emelyanov 30036f4e6433SPavel Emelyanov upid = &pid->numbers[pid->level]; 30046f4e6433SPavel Emelyanov if (upid->nr == 1) 30056f4e6433SPavel Emelyanov pid_ns_release_proc(upid->ns); 300660347f67SPavel Emelyanov } 300760347f67SPavel Emelyanov 30089711ef99SAdrian Bunk static struct dentry *proc_pid_instantiate(struct inode *dir, 30099711ef99SAdrian Bunk struct dentry * dentry, 3010c5141e6dSEric Dumazet struct task_struct *task, const void *ptr) 3011444ceed8SEric W. Biederman { 3012444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-ENOENT); 3013444ceed8SEric W. Biederman struct inode *inode; 3014444ceed8SEric W. Biederman 301561a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 3016444ceed8SEric W. Biederman if (!inode) 3017444ceed8SEric W. Biederman goto out; 3018444ceed8SEric W. Biederman 3019444ceed8SEric W. Biederman inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO; 3020444ceed8SEric W. Biederman inode->i_op = &proc_tgid_base_inode_operations; 3021444ceed8SEric W. Biederman inode->i_fop = &proc_tgid_base_operations; 3022444ceed8SEric W. Biederman inode->i_flags|=S_IMMUTABLE; 3023aed54175SVegard Nossum 3024aed54175SVegard Nossum inode->i_nlink = 2 + pid_entry_count_dirs(tgid_base_stuff, 3025aed54175SVegard Nossum ARRAY_SIZE(tgid_base_stuff)); 3026444ceed8SEric W. Biederman 3027fb045adbSNick Piggin d_set_d_op(dentry, &pid_dentry_operations); 3028444ceed8SEric W. Biederman 3029444ceed8SEric W. Biederman d_add(dentry, inode); 3030444ceed8SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 3031444ceed8SEric W. Biederman if (pid_revalidate(dentry, NULL)) 3032444ceed8SEric W. Biederman error = NULL; 3033444ceed8SEric W. Biederman out: 3034444ceed8SEric W. Biederman return error; 3035444ceed8SEric W. Biederman } 3036444ceed8SEric W. Biederman 30371da177e4SLinus Torvalds struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd) 30381da177e4SLinus Torvalds { 303973d36460SDan Carpenter struct dentry *result; 30401da177e4SLinus Torvalds struct task_struct *task; 30411da177e4SLinus Torvalds unsigned tgid; 3042b488893aSPavel Emelyanov struct pid_namespace *ns; 30431da177e4SLinus Torvalds 3044801199ceSEric W. Biederman result = proc_base_lookup(dir, dentry); 3045801199ceSEric W. Biederman if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT) 3046801199ceSEric W. Biederman goto out; 3047801199ceSEric W. Biederman 30481da177e4SLinus Torvalds tgid = name_to_int(dentry); 30491da177e4SLinus Torvalds if (tgid == ~0U) 30501da177e4SLinus Torvalds goto out; 30511da177e4SLinus Torvalds 3052b488893aSPavel Emelyanov ns = dentry->d_sb->s_fs_info; 3053de758734SEric W. Biederman rcu_read_lock(); 3054b488893aSPavel Emelyanov task = find_task_by_pid_ns(tgid, ns); 30551da177e4SLinus Torvalds if (task) 30561da177e4SLinus Torvalds get_task_struct(task); 3057de758734SEric W. Biederman rcu_read_unlock(); 30581da177e4SLinus Torvalds if (!task) 30591da177e4SLinus Torvalds goto out; 30601da177e4SLinus Torvalds 3061444ceed8SEric W. Biederman result = proc_pid_instantiate(dir, dentry, task, NULL); 306248e6484dSEric W. Biederman put_task_struct(task); 30631da177e4SLinus Torvalds out: 3064cd6a3ce9SEric W. Biederman return result; 30651da177e4SLinus Torvalds } 30661da177e4SLinus Torvalds 30671da177e4SLinus Torvalds /* 30680804ef4bSEric W. Biederman * Find the first task with tgid >= tgid 30690bc58a91SEric W. Biederman * 30701da177e4SLinus Torvalds */ 307119fd4bb2SEric W. Biederman struct tgid_iter { 307219fd4bb2SEric W. Biederman unsigned int tgid; 30730804ef4bSEric W. Biederman struct task_struct *task; 307419fd4bb2SEric W. Biederman }; 307519fd4bb2SEric W. Biederman static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter) 307619fd4bb2SEric W. Biederman { 30770804ef4bSEric W. Biederman struct pid *pid; 30781da177e4SLinus Torvalds 307919fd4bb2SEric W. Biederman if (iter.task) 308019fd4bb2SEric W. Biederman put_task_struct(iter.task); 30810804ef4bSEric W. Biederman rcu_read_lock(); 30820804ef4bSEric W. Biederman retry: 308319fd4bb2SEric W. Biederman iter.task = NULL; 308419fd4bb2SEric W. Biederman pid = find_ge_pid(iter.tgid, ns); 30850804ef4bSEric W. Biederman if (pid) { 308619fd4bb2SEric W. Biederman iter.tgid = pid_nr_ns(pid, ns); 308719fd4bb2SEric W. Biederman iter.task = pid_task(pid, PIDTYPE_PID); 30880804ef4bSEric W. Biederman /* What we to know is if the pid we have find is the 30890804ef4bSEric W. Biederman * pid of a thread_group_leader. Testing for task 30900804ef4bSEric W. Biederman * being a thread_group_leader is the obvious thing 30910804ef4bSEric W. Biederman * todo but there is a window when it fails, due to 30920804ef4bSEric W. Biederman * the pid transfer logic in de_thread. 30930804ef4bSEric W. Biederman * 30940804ef4bSEric W. Biederman * So we perform the straight forward test of seeing 30950804ef4bSEric W. Biederman * if the pid we have found is the pid of a thread 30960804ef4bSEric W. Biederman * group leader, and don't worry if the task we have 30970804ef4bSEric W. Biederman * found doesn't happen to be a thread group leader. 30980804ef4bSEric W. Biederman * As we don't care in the case of readdir. 30990bc58a91SEric W. Biederman */ 310019fd4bb2SEric W. Biederman if (!iter.task || !has_group_leader_pid(iter.task)) { 310119fd4bb2SEric W. Biederman iter.tgid += 1; 31020804ef4bSEric W. Biederman goto retry; 310319fd4bb2SEric W. Biederman } 310419fd4bb2SEric W. Biederman get_task_struct(iter.task); 31051da177e4SLinus Torvalds } 3106454cc105SEric W. Biederman rcu_read_unlock(); 310719fd4bb2SEric W. Biederman return iter; 31081da177e4SLinus Torvalds } 31091da177e4SLinus Torvalds 31107bcd6b0eSEric W. Biederman #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff)) 31111da177e4SLinus Torvalds 311261a28784SEric W. Biederman static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir, 311319fd4bb2SEric W. Biederman struct tgid_iter iter) 311461a28784SEric W. Biederman { 311561a28784SEric W. Biederman char name[PROC_NUMBUF]; 311619fd4bb2SEric W. Biederman int len = snprintf(name, sizeof(name), "%d", iter.tgid); 311761a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, name, len, 311819fd4bb2SEric W. Biederman proc_pid_instantiate, iter.task, NULL); 311961a28784SEric W. Biederman } 312061a28784SEric W. Biederman 31211da177e4SLinus Torvalds /* for the /proc/ directory itself, after non-process stuff has been done */ 31221da177e4SLinus Torvalds int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir) 31231da177e4SLinus Torvalds { 3124d8bdc59fSLinus Torvalds unsigned int nr; 3125d8bdc59fSLinus Torvalds struct task_struct *reaper; 312619fd4bb2SEric W. Biederman struct tgid_iter iter; 3127b488893aSPavel Emelyanov struct pid_namespace *ns; 31281da177e4SLinus Torvalds 3129d8bdc59fSLinus Torvalds if (filp->f_pos >= PID_MAX_LIMIT + TGID_OFFSET) 3130d8bdc59fSLinus Torvalds goto out_no_task; 3131d8bdc59fSLinus Torvalds nr = filp->f_pos - FIRST_PROCESS_ENTRY; 3132d8bdc59fSLinus Torvalds 3133d8bdc59fSLinus Torvalds reaper = get_proc_task(filp->f_path.dentry->d_inode); 313461a28784SEric W. Biederman if (!reaper) 313561a28784SEric W. Biederman goto out_no_task; 313661a28784SEric W. Biederman 31377bcd6b0eSEric W. Biederman for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) { 3138c5141e6dSEric Dumazet const struct pid_entry *p = &proc_base_stuff[nr]; 313961a28784SEric W. Biederman if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0) 3140801199ceSEric W. Biederman goto out; 31411da177e4SLinus Torvalds } 31421da177e4SLinus Torvalds 3143b488893aSPavel Emelyanov ns = filp->f_dentry->d_sb->s_fs_info; 314419fd4bb2SEric W. Biederman iter.task = NULL; 314519fd4bb2SEric W. Biederman iter.tgid = filp->f_pos - TGID_OFFSET; 314619fd4bb2SEric W. Biederman for (iter = next_tgid(ns, iter); 314719fd4bb2SEric W. Biederman iter.task; 314819fd4bb2SEric W. Biederman iter.tgid += 1, iter = next_tgid(ns, iter)) { 314919fd4bb2SEric W. Biederman filp->f_pos = iter.tgid + TGID_OFFSET; 315019fd4bb2SEric W. Biederman if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) { 315119fd4bb2SEric W. Biederman put_task_struct(iter.task); 31520804ef4bSEric W. Biederman goto out; 31531da177e4SLinus Torvalds } 31541da177e4SLinus Torvalds } 31550804ef4bSEric W. Biederman filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET; 31560804ef4bSEric W. Biederman out: 315761a28784SEric W. Biederman put_task_struct(reaper); 315861a28784SEric W. Biederman out_no_task: 31591da177e4SLinus Torvalds return 0; 31601da177e4SLinus Torvalds } 31611da177e4SLinus Torvalds 31620bc58a91SEric W. Biederman /* 316328a6d671SEric W. Biederman * Tasks 316428a6d671SEric W. Biederman */ 3165c5141e6dSEric Dumazet static const struct pid_entry tid_base_stuff[] = { 3166631f9c18SAlexey Dobriyan DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations), 31673835541dSJerome Marchand DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations), 3168*6b4e306aSEric W. Biederman DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations), 3169631f9c18SAlexey Dobriyan REG("environ", S_IRUSR, proc_environ_operations), 3170631f9c18SAlexey Dobriyan INF("auxv", S_IRUSR, proc_pid_auxv), 3171631f9c18SAlexey Dobriyan ONE("status", S_IRUGO, proc_pid_status), 3172a9712bc1SAl Viro ONE("personality", S_IRUGO, proc_pid_personality), 31733036e7b4SJiri Olsa INF("limits", S_IRUGO, proc_pid_limits), 317443ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG 3175631f9c18SAlexey Dobriyan REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations), 317643ae34cbSIngo Molnar #endif 31774614a696Sjohn stultz REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations), 3178ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK 3179a9712bc1SAl Viro INF("syscall", S_IRUGO, proc_pid_syscall), 3180ebcb6734SRoland McGrath #endif 3181631f9c18SAlexey Dobriyan INF("cmdline", S_IRUGO, proc_pid_cmdline), 3182631f9c18SAlexey Dobriyan ONE("stat", S_IRUGO, proc_tid_stat), 3183631f9c18SAlexey Dobriyan ONE("statm", S_IRUGO, proc_pid_statm), 3184631f9c18SAlexey Dobriyan REG("maps", S_IRUGO, proc_maps_operations), 318528a6d671SEric W. Biederman #ifdef CONFIG_NUMA 3186631f9c18SAlexey Dobriyan REG("numa_maps", S_IRUGO, proc_numa_maps_operations), 318728a6d671SEric W. Biederman #endif 3188631f9c18SAlexey Dobriyan REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations), 3189631f9c18SAlexey Dobriyan LNK("cwd", proc_cwd_link), 3190631f9c18SAlexey Dobriyan LNK("root", proc_root_link), 3191631f9c18SAlexey Dobriyan LNK("exe", proc_exe_link), 3192631f9c18SAlexey Dobriyan REG("mounts", S_IRUGO, proc_mounts_operations), 3193631f9c18SAlexey Dobriyan REG("mountinfo", S_IRUGO, proc_mountinfo_operations), 31941e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR 3195631f9c18SAlexey Dobriyan REG("clear_refs", S_IWUSR, proc_clear_refs_operations), 3196631f9c18SAlexey Dobriyan REG("smaps", S_IRUGO, proc_smaps_operations), 3197ca6b0bf0SAl Viro REG("pagemap", S_IRUGO, proc_pagemap_operations), 319828a6d671SEric W. Biederman #endif 319928a6d671SEric W. Biederman #ifdef CONFIG_SECURITY 3200631f9c18SAlexey Dobriyan DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations), 320128a6d671SEric W. Biederman #endif 320228a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS 3203631f9c18SAlexey Dobriyan INF("wchan", S_IRUGO, proc_pid_wchan), 320428a6d671SEric W. Biederman #endif 32052ec220e2SKen Chen #ifdef CONFIG_STACKTRACE 3206a9712bc1SAl Viro ONE("stack", S_IRUGO, proc_pid_stack), 320728a6d671SEric W. Biederman #endif 320828a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS 3209631f9c18SAlexey Dobriyan INF("schedstat", S_IRUGO, proc_pid_schedstat), 321028a6d671SEric W. Biederman #endif 32119745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP 3212631f9c18SAlexey Dobriyan REG("latency", S_IRUGO, proc_lstats_operations), 32139745512cSArjan van de Ven #endif 32148793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET 3215631f9c18SAlexey Dobriyan REG("cpuset", S_IRUGO, proc_cpuset_operations), 321628a6d671SEric W. Biederman #endif 3217a424316cSPaul Menage #ifdef CONFIG_CGROUPS 3218631f9c18SAlexey Dobriyan REG("cgroup", S_IRUGO, proc_cgroup_operations), 3219a424316cSPaul Menage #endif 3220631f9c18SAlexey Dobriyan INF("oom_score", S_IRUGO, proc_oom_score), 3221631f9c18SAlexey Dobriyan REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations), 3222a63d83f4SDavid Rientjes REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations), 322328a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL 3224631f9c18SAlexey Dobriyan REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations), 322526ec3c64SAl Viro REG("sessionid", S_IRUGO, proc_sessionid_operations), 322628a6d671SEric W. Biederman #endif 3227f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION 3228631f9c18SAlexey Dobriyan REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations), 3229f4f154fdSAkinobu Mita #endif 3230297c5d92SAndrea Righi #ifdef CONFIG_TASK_IO_ACCOUNTING 3231631f9c18SAlexey Dobriyan INF("io", S_IRUGO, proc_tid_io_accounting), 3232297c5d92SAndrea Righi #endif 323328a6d671SEric W. Biederman }; 323428a6d671SEric W. Biederman 323528a6d671SEric W. Biederman static int proc_tid_base_readdir(struct file * filp, 323628a6d671SEric W. Biederman void * dirent, filldir_t filldir) 323728a6d671SEric W. Biederman { 323828a6d671SEric W. Biederman return proc_pident_readdir(filp,dirent,filldir, 323928a6d671SEric W. Biederman tid_base_stuff,ARRAY_SIZE(tid_base_stuff)); 324028a6d671SEric W. Biederman } 324128a6d671SEric W. Biederman 324228a6d671SEric W. Biederman static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){ 32437bcd6b0eSEric W. Biederman return proc_pident_lookup(dir, dentry, 32447bcd6b0eSEric W. Biederman tid_base_stuff, ARRAY_SIZE(tid_base_stuff)); 324528a6d671SEric W. Biederman } 324628a6d671SEric W. Biederman 324700977a59SArjan van de Ven static const struct file_operations proc_tid_base_operations = { 324828a6d671SEric W. Biederman .read = generic_read_dir, 324928a6d671SEric W. Biederman .readdir = proc_tid_base_readdir, 32506038f373SArnd Bergmann .llseek = default_llseek, 325128a6d671SEric W. Biederman }; 325228a6d671SEric W. Biederman 3253c5ef1c42SArjan van de Ven static const struct inode_operations proc_tid_base_inode_operations = { 325428a6d671SEric W. Biederman .lookup = proc_tid_base_lookup, 325528a6d671SEric W. Biederman .getattr = pid_getattr, 325628a6d671SEric W. Biederman .setattr = proc_setattr, 325728a6d671SEric W. Biederman }; 325828a6d671SEric W. Biederman 3259444ceed8SEric W. Biederman static struct dentry *proc_task_instantiate(struct inode *dir, 3260c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 3261444ceed8SEric W. Biederman { 3262444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-ENOENT); 3263444ceed8SEric W. Biederman struct inode *inode; 326461a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 3265444ceed8SEric W. Biederman 3266444ceed8SEric W. Biederman if (!inode) 3267444ceed8SEric W. Biederman goto out; 3268444ceed8SEric W. Biederman inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO; 3269444ceed8SEric W. Biederman inode->i_op = &proc_tid_base_inode_operations; 3270444ceed8SEric W. Biederman inode->i_fop = &proc_tid_base_operations; 3271444ceed8SEric W. Biederman inode->i_flags|=S_IMMUTABLE; 3272aed54175SVegard Nossum 3273aed54175SVegard Nossum inode->i_nlink = 2 + pid_entry_count_dirs(tid_base_stuff, 3274aed54175SVegard Nossum ARRAY_SIZE(tid_base_stuff)); 3275444ceed8SEric W. Biederman 3276fb045adbSNick Piggin d_set_d_op(dentry, &pid_dentry_operations); 3277444ceed8SEric W. Biederman 3278444ceed8SEric W. Biederman d_add(dentry, inode); 3279444ceed8SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 3280444ceed8SEric W. Biederman if (pid_revalidate(dentry, NULL)) 3281444ceed8SEric W. Biederman error = NULL; 3282444ceed8SEric W. Biederman out: 3283444ceed8SEric W. Biederman return error; 3284444ceed8SEric W. Biederman } 3285444ceed8SEric W. Biederman 328628a6d671SEric W. Biederman static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd) 328728a6d671SEric W. Biederman { 328828a6d671SEric W. Biederman struct dentry *result = ERR_PTR(-ENOENT); 328928a6d671SEric W. Biederman struct task_struct *task; 329028a6d671SEric W. Biederman struct task_struct *leader = get_proc_task(dir); 329128a6d671SEric W. Biederman unsigned tid; 3292b488893aSPavel Emelyanov struct pid_namespace *ns; 329328a6d671SEric W. Biederman 329428a6d671SEric W. Biederman if (!leader) 329528a6d671SEric W. Biederman goto out_no_task; 329628a6d671SEric W. Biederman 329728a6d671SEric W. Biederman tid = name_to_int(dentry); 329828a6d671SEric W. Biederman if (tid == ~0U) 329928a6d671SEric W. Biederman goto out; 330028a6d671SEric W. Biederman 3301b488893aSPavel Emelyanov ns = dentry->d_sb->s_fs_info; 330228a6d671SEric W. Biederman rcu_read_lock(); 3303b488893aSPavel Emelyanov task = find_task_by_pid_ns(tid, ns); 330428a6d671SEric W. Biederman if (task) 330528a6d671SEric W. Biederman get_task_struct(task); 330628a6d671SEric W. Biederman rcu_read_unlock(); 330728a6d671SEric W. Biederman if (!task) 330828a6d671SEric W. Biederman goto out; 3309bac0abd6SPavel Emelyanov if (!same_thread_group(leader, task)) 331028a6d671SEric W. Biederman goto out_drop_task; 331128a6d671SEric W. Biederman 3312444ceed8SEric W. Biederman result = proc_task_instantiate(dir, dentry, task, NULL); 331328a6d671SEric W. Biederman out_drop_task: 331428a6d671SEric W. Biederman put_task_struct(task); 331528a6d671SEric W. Biederman out: 331628a6d671SEric W. Biederman put_task_struct(leader); 331728a6d671SEric W. Biederman out_no_task: 331828a6d671SEric W. Biederman return result; 331928a6d671SEric W. Biederman } 332028a6d671SEric W. Biederman 332128a6d671SEric W. Biederman /* 33220bc58a91SEric W. Biederman * Find the first tid of a thread group to return to user space. 33230bc58a91SEric W. Biederman * 33240bc58a91SEric W. Biederman * Usually this is just the thread group leader, but if the users 33250bc58a91SEric W. Biederman * buffer was too small or there was a seek into the middle of the 33260bc58a91SEric W. Biederman * directory we have more work todo. 33270bc58a91SEric W. Biederman * 33280bc58a91SEric W. Biederman * In the case of a short read we start with find_task_by_pid. 33290bc58a91SEric W. Biederman * 33300bc58a91SEric W. Biederman * In the case of a seek we start with the leader and walk nr 33310bc58a91SEric W. Biederman * threads past it. 33320bc58a91SEric W. Biederman */ 3333cc288738SEric W. Biederman static struct task_struct *first_tid(struct task_struct *leader, 3334b488893aSPavel Emelyanov int tid, int nr, struct pid_namespace *ns) 33350bc58a91SEric W. Biederman { 3336a872ff0cSOleg Nesterov struct task_struct *pos; 33370bc58a91SEric W. Biederman 3338cc288738SEric W. Biederman rcu_read_lock(); 33390bc58a91SEric W. Biederman /* Attempt to start with the pid of a thread */ 33400bc58a91SEric W. Biederman if (tid && (nr > 0)) { 3341b488893aSPavel Emelyanov pos = find_task_by_pid_ns(tid, ns); 3342a872ff0cSOleg Nesterov if (pos && (pos->group_leader == leader)) 3343a872ff0cSOleg Nesterov goto found; 33440bc58a91SEric W. Biederman } 33450bc58a91SEric W. Biederman 33460bc58a91SEric W. Biederman /* If nr exceeds the number of threads there is nothing todo */ 33470bc58a91SEric W. Biederman pos = NULL; 3348a872ff0cSOleg Nesterov if (nr && nr >= get_nr_threads(leader)) 3349a872ff0cSOleg Nesterov goto out; 3350a872ff0cSOleg Nesterov 3351a872ff0cSOleg Nesterov /* If we haven't found our starting place yet start 3352a872ff0cSOleg Nesterov * with the leader and walk nr threads forward. 3353a872ff0cSOleg Nesterov */ 3354a872ff0cSOleg Nesterov for (pos = leader; nr > 0; --nr) { 3355a872ff0cSOleg Nesterov pos = next_thread(pos); 3356a872ff0cSOleg Nesterov if (pos == leader) { 3357a872ff0cSOleg Nesterov pos = NULL; 3358a872ff0cSOleg Nesterov goto out; 3359a872ff0cSOleg Nesterov } 3360a872ff0cSOleg Nesterov } 3361a872ff0cSOleg Nesterov found: 3362a872ff0cSOleg Nesterov get_task_struct(pos); 3363a872ff0cSOleg Nesterov out: 3364cc288738SEric W. Biederman rcu_read_unlock(); 33650bc58a91SEric W. Biederman return pos; 33660bc58a91SEric W. Biederman } 33670bc58a91SEric W. Biederman 33680bc58a91SEric W. Biederman /* 33690bc58a91SEric W. Biederman * Find the next thread in the thread list. 33700bc58a91SEric W. Biederman * Return NULL if there is an error or no next thread. 33710bc58a91SEric W. Biederman * 33720bc58a91SEric W. Biederman * The reference to the input task_struct is released. 33730bc58a91SEric W. Biederman */ 33740bc58a91SEric W. Biederman static struct task_struct *next_tid(struct task_struct *start) 33750bc58a91SEric W. Biederman { 3376c1df7fb8SOleg Nesterov struct task_struct *pos = NULL; 3377cc288738SEric W. Biederman rcu_read_lock(); 3378c1df7fb8SOleg Nesterov if (pid_alive(start)) { 33790bc58a91SEric W. Biederman pos = next_thread(start); 3380c1df7fb8SOleg Nesterov if (thread_group_leader(pos)) 33810bc58a91SEric W. Biederman pos = NULL; 3382c1df7fb8SOleg Nesterov else 3383c1df7fb8SOleg Nesterov get_task_struct(pos); 3384c1df7fb8SOleg Nesterov } 3385cc288738SEric W. Biederman rcu_read_unlock(); 33860bc58a91SEric W. Biederman put_task_struct(start); 33870bc58a91SEric W. Biederman return pos; 33880bc58a91SEric W. Biederman } 33890bc58a91SEric W. Biederman 339061a28784SEric W. Biederman static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir, 339161a28784SEric W. Biederman struct task_struct *task, int tid) 339261a28784SEric W. Biederman { 339361a28784SEric W. Biederman char name[PROC_NUMBUF]; 339461a28784SEric W. Biederman int len = snprintf(name, sizeof(name), "%d", tid); 339561a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, name, len, 339661a28784SEric W. Biederman proc_task_instantiate, task, NULL); 339761a28784SEric W. Biederman } 339861a28784SEric W. Biederman 33991da177e4SLinus Torvalds /* for the /proc/TGID/task/ directories */ 34001da177e4SLinus Torvalds static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir) 34011da177e4SLinus Torvalds { 34022fddfeefSJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 34031da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 34047d895244SGuillaume Chazarain struct task_struct *leader = NULL; 34050bc58a91SEric W. Biederman struct task_struct *task; 34061da177e4SLinus Torvalds int retval = -ENOENT; 34071da177e4SLinus Torvalds ino_t ino; 34080bc58a91SEric W. Biederman int tid; 3409b488893aSPavel Emelyanov struct pid_namespace *ns; 34101da177e4SLinus Torvalds 34117d895244SGuillaume Chazarain task = get_proc_task(inode); 34127d895244SGuillaume Chazarain if (!task) 34137d895244SGuillaume Chazarain goto out_no_task; 34147d895244SGuillaume Chazarain rcu_read_lock(); 34157d895244SGuillaume Chazarain if (pid_alive(task)) { 34167d895244SGuillaume Chazarain leader = task->group_leader; 34177d895244SGuillaume Chazarain get_task_struct(leader); 34187d895244SGuillaume Chazarain } 34197d895244SGuillaume Chazarain rcu_read_unlock(); 34207d895244SGuillaume Chazarain put_task_struct(task); 342199f89551SEric W. Biederman if (!leader) 342299f89551SEric W. Biederman goto out_no_task; 34231da177e4SLinus Torvalds retval = 0; 34241da177e4SLinus Torvalds 3425ee568b25SLinus Torvalds switch ((unsigned long)filp->f_pos) { 34261da177e4SLinus Torvalds case 0: 34271da177e4SLinus Torvalds ino = inode->i_ino; 3428ee6f779bSZhang Le if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) < 0) 34291da177e4SLinus Torvalds goto out; 3430ee6f779bSZhang Le filp->f_pos++; 34311da177e4SLinus Torvalds /* fall through */ 34321da177e4SLinus Torvalds case 1: 34331da177e4SLinus Torvalds ino = parent_ino(dentry); 3434ee6f779bSZhang Le if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) < 0) 34351da177e4SLinus Torvalds goto out; 3436ee6f779bSZhang Le filp->f_pos++; 34371da177e4SLinus Torvalds /* fall through */ 34381da177e4SLinus Torvalds } 34391da177e4SLinus Torvalds 34400bc58a91SEric W. Biederman /* f_version caches the tgid value that the last readdir call couldn't 34410bc58a91SEric W. Biederman * return. lseek aka telldir automagically resets f_version to 0. 34420bc58a91SEric W. Biederman */ 3443b488893aSPavel Emelyanov ns = filp->f_dentry->d_sb->s_fs_info; 34442b47c361SMathieu Desnoyers tid = (int)filp->f_version; 34450bc58a91SEric W. Biederman filp->f_version = 0; 3446ee6f779bSZhang Le for (task = first_tid(leader, tid, filp->f_pos - 2, ns); 34470bc58a91SEric W. Biederman task; 3448ee6f779bSZhang Le task = next_tid(task), filp->f_pos++) { 3449b488893aSPavel Emelyanov tid = task_pid_nr_ns(task, ns); 345061a28784SEric W. Biederman if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) { 34510bc58a91SEric W. Biederman /* returning this tgid failed, save it as the first 34520bc58a91SEric W. Biederman * pid for the next readir call */ 34532b47c361SMathieu Desnoyers filp->f_version = (u64)tid; 34540bc58a91SEric W. Biederman put_task_struct(task); 34551da177e4SLinus Torvalds break; 34560bc58a91SEric W. Biederman } 34571da177e4SLinus Torvalds } 34581da177e4SLinus Torvalds out: 345999f89551SEric W. Biederman put_task_struct(leader); 346099f89551SEric W. Biederman out_no_task: 34611da177e4SLinus Torvalds return retval; 34621da177e4SLinus Torvalds } 34636e66b52bSEric W. Biederman 34646e66b52bSEric W. Biederman static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) 34656e66b52bSEric W. Biederman { 34666e66b52bSEric W. Biederman struct inode *inode = dentry->d_inode; 346799f89551SEric W. Biederman struct task_struct *p = get_proc_task(inode); 34686e66b52bSEric W. Biederman generic_fillattr(inode, stat); 34696e66b52bSEric W. Biederman 347099f89551SEric W. Biederman if (p) { 347199f89551SEric W. Biederman stat->nlink += get_nr_threads(p); 347299f89551SEric W. Biederman put_task_struct(p); 34736e66b52bSEric W. Biederman } 34746e66b52bSEric W. Biederman 34756e66b52bSEric W. Biederman return 0; 34766e66b52bSEric W. Biederman } 347728a6d671SEric W. Biederman 3478c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations = { 347928a6d671SEric W. Biederman .lookup = proc_task_lookup, 348028a6d671SEric W. Biederman .getattr = proc_task_getattr, 348128a6d671SEric W. Biederman .setattr = proc_setattr, 348228a6d671SEric W. Biederman }; 348328a6d671SEric W. Biederman 348400977a59SArjan van de Ven static const struct file_operations proc_task_operations = { 348528a6d671SEric W. Biederman .read = generic_read_dir, 348628a6d671SEric W. Biederman .readdir = proc_task_readdir, 34876038f373SArnd Bergmann .llseek = default_llseek, 348828a6d671SEric W. Biederman }; 3489