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> 86f133eccaSChris Metcalf #ifdef CONFIG_HARDWALL 87f133eccaSChris Metcalf #include <asm/hardwall.h> 88f133eccaSChris Metcalf #endif 89*43d2b113SKAMEZAWA Hiroyuki #include <trace/events/oom.h> 901da177e4SLinus Torvalds #include "internal.h" 911da177e4SLinus Torvalds 920f2fe20fSEric W. Biederman /* NOTE: 930f2fe20fSEric W. Biederman * Implementing inode permission operations in /proc is almost 940f2fe20fSEric W. Biederman * certainly an error. Permission checks need to happen during 950f2fe20fSEric W. Biederman * each system call not at open time. The reason is that most of 960f2fe20fSEric W. Biederman * what we wish to check for permissions in /proc varies at runtime. 970f2fe20fSEric W. Biederman * 980f2fe20fSEric W. Biederman * The classic example of a problem is opening file descriptors 990f2fe20fSEric W. Biederman * in /proc for a task before it execs a suid executable. 1000f2fe20fSEric W. Biederman */ 1010f2fe20fSEric W. Biederman 1021da177e4SLinus Torvalds struct pid_entry { 1031da177e4SLinus Torvalds char *name; 104c5141e6dSEric Dumazet int len; 105d161a13fSAl Viro umode_t mode; 106c5ef1c42SArjan van de Ven const struct inode_operations *iop; 10700977a59SArjan van de Ven const struct file_operations *fop; 10820cdc894SEric W. Biederman union proc_op op; 1091da177e4SLinus Torvalds }; 1101da177e4SLinus Torvalds 11161a28784SEric W. Biederman #define NOD(NAME, MODE, IOP, FOP, OP) { \ 11220cdc894SEric W. Biederman .name = (NAME), \ 113c5141e6dSEric Dumazet .len = sizeof(NAME) - 1, \ 11420cdc894SEric W. Biederman .mode = MODE, \ 11520cdc894SEric W. Biederman .iop = IOP, \ 11620cdc894SEric W. Biederman .fop = FOP, \ 11720cdc894SEric W. Biederman .op = OP, \ 11820cdc894SEric W. Biederman } 11920cdc894SEric W. Biederman 120631f9c18SAlexey Dobriyan #define DIR(NAME, MODE, iops, fops) \ 121631f9c18SAlexey Dobriyan NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} ) 122631f9c18SAlexey Dobriyan #define LNK(NAME, get_link) \ 12361a28784SEric W. Biederman NOD(NAME, (S_IFLNK|S_IRWXUGO), \ 12420cdc894SEric W. Biederman &proc_pid_link_inode_operations, NULL, \ 125631f9c18SAlexey Dobriyan { .proc_get_link = get_link } ) 126631f9c18SAlexey Dobriyan #define REG(NAME, MODE, fops) \ 127631f9c18SAlexey Dobriyan NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {}) 128631f9c18SAlexey Dobriyan #define INF(NAME, MODE, read) \ 12961a28784SEric W. Biederman NOD(NAME, (S_IFREG|(MODE)), \ 13020cdc894SEric W. Biederman NULL, &proc_info_file_operations, \ 131631f9c18SAlexey Dobriyan { .proc_read = read } ) 132631f9c18SAlexey Dobriyan #define ONE(NAME, MODE, show) \ 133be614086SEric W. Biederman NOD(NAME, (S_IFREG|(MODE)), \ 134be614086SEric W. Biederman NULL, &proc_single_file_operations, \ 135631f9c18SAlexey Dobriyan { .proc_show = show } ) 1361da177e4SLinus Torvalds 137aed54175SVegard Nossum /* 138aed54175SVegard Nossum * Count the number of hardlinks for the pid_entry table, excluding the . 139aed54175SVegard Nossum * and .. links. 140aed54175SVegard Nossum */ 141aed54175SVegard Nossum static unsigned int pid_entry_count_dirs(const struct pid_entry *entries, 142aed54175SVegard Nossum unsigned int n) 143aed54175SVegard Nossum { 144aed54175SVegard Nossum unsigned int i; 145aed54175SVegard Nossum unsigned int count; 146aed54175SVegard Nossum 147aed54175SVegard Nossum count = 0; 148aed54175SVegard Nossum for (i = 0; i < n; ++i) { 149aed54175SVegard Nossum if (S_ISDIR(entries[i].mode)) 150aed54175SVegard Nossum ++count; 151aed54175SVegard Nossum } 152aed54175SVegard Nossum 153aed54175SVegard Nossum return count; 154aed54175SVegard Nossum } 155aed54175SVegard Nossum 156f7ad3c6bSMiklos Szeredi static int get_task_root(struct task_struct *task, struct path *root) 1571da177e4SLinus Torvalds { 1587c2c7d99SHugh Dickins int result = -ENOENT; 1597c2c7d99SHugh Dickins 1600494f6ecSMiklos Szeredi task_lock(task); 161f7ad3c6bSMiklos Szeredi if (task->fs) { 162f7ad3c6bSMiklos Szeredi get_fs_root(task->fs, root); 1637c2c7d99SHugh Dickins result = 0; 1647c2c7d99SHugh Dickins } 1650494f6ecSMiklos Szeredi task_unlock(task); 1667c2c7d99SHugh Dickins return result; 1670494f6ecSMiklos Szeredi } 1680494f6ecSMiklos Szeredi 1693dcd25f3SJan Blunck static int proc_cwd_link(struct inode *inode, struct path *path) 1700494f6ecSMiklos Szeredi { 17199f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 1720494f6ecSMiklos Szeredi int result = -ENOENT; 17399f89551SEric W. Biederman 17499f89551SEric W. Biederman if (task) { 175f7ad3c6bSMiklos Szeredi task_lock(task); 176f7ad3c6bSMiklos Szeredi if (task->fs) { 177f7ad3c6bSMiklos Szeredi get_fs_pwd(task->fs, path); 178f7ad3c6bSMiklos Szeredi result = 0; 179f7ad3c6bSMiklos Szeredi } 180f7ad3c6bSMiklos Szeredi task_unlock(task); 18199f89551SEric W. Biederman put_task_struct(task); 18299f89551SEric W. Biederman } 1831da177e4SLinus Torvalds return result; 1841da177e4SLinus Torvalds } 1851da177e4SLinus Torvalds 1863dcd25f3SJan Blunck static int proc_root_link(struct inode *inode, struct path *path) 1871da177e4SLinus Torvalds { 18899f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 1891da177e4SLinus Torvalds int result = -ENOENT; 19099f89551SEric W. Biederman 19199f89551SEric W. Biederman if (task) { 192f7ad3c6bSMiklos Szeredi result = get_task_root(task, path); 19399f89551SEric W. Biederman put_task_struct(task); 19499f89551SEric W. Biederman } 1951da177e4SLinus Torvalds return result; 1961da177e4SLinus Torvalds } 1971da177e4SLinus Torvalds 1988b0db9dbSStephen Wilson static struct mm_struct *__check_mem_permission(struct task_struct *task) 199638fa202SRoland McGrath { 2008b0db9dbSStephen Wilson struct mm_struct *mm; 2018b0db9dbSStephen Wilson 2028b0db9dbSStephen Wilson mm = get_task_mm(task); 2038b0db9dbSStephen Wilson if (!mm) 2048b0db9dbSStephen Wilson return ERR_PTR(-EINVAL); 2058b0db9dbSStephen Wilson 206638fa202SRoland McGrath /* 207638fa202SRoland McGrath * A task can always look at itself, in case it chooses 208638fa202SRoland McGrath * to use system calls instead of load instructions. 209638fa202SRoland McGrath */ 210638fa202SRoland McGrath if (task == current) 2118b0db9dbSStephen Wilson return mm; 212638fa202SRoland McGrath 213638fa202SRoland McGrath /* 214638fa202SRoland McGrath * If current is actively ptrace'ing, and would also be 215638fa202SRoland McGrath * permitted to freshly attach with ptrace now, permit it. 216638fa202SRoland McGrath */ 2170d094efeSRoland McGrath if (task_is_stopped_or_traced(task)) { 2180d094efeSRoland McGrath int match; 2190d094efeSRoland McGrath rcu_read_lock(); 22006d98473STejun Heo match = (ptrace_parent(task) == current); 2210d094efeSRoland McGrath rcu_read_unlock(); 2220d094efeSRoland McGrath if (match && ptrace_may_access(task, PTRACE_MODE_ATTACH)) 2238b0db9dbSStephen Wilson return mm; 2240d094efeSRoland McGrath } 225638fa202SRoland McGrath 226638fa202SRoland McGrath /* 227638fa202SRoland McGrath * No one else is allowed. 228638fa202SRoland McGrath */ 2298b0db9dbSStephen Wilson mmput(mm); 2308b0db9dbSStephen Wilson return ERR_PTR(-EPERM); 231638fa202SRoland McGrath } 2321da177e4SLinus Torvalds 23318f661bcSStephen Wilson /* 2348b0db9dbSStephen Wilson * If current may access user memory in @task return a reference to the 2358b0db9dbSStephen Wilson * corresponding mm, otherwise ERR_PTR. 23618f661bcSStephen Wilson */ 2378b0db9dbSStephen Wilson static struct mm_struct *check_mem_permission(struct task_struct *task) 23818f661bcSStephen Wilson { 2398b0db9dbSStephen Wilson struct mm_struct *mm; 24018f661bcSStephen Wilson int err; 24118f661bcSStephen Wilson 24218f661bcSStephen Wilson /* 24318f661bcSStephen Wilson * Avoid racing if task exec's as we might get a new mm but validate 24418f661bcSStephen Wilson * against old credentials. 24518f661bcSStephen Wilson */ 24618f661bcSStephen Wilson err = mutex_lock_killable(&task->signal->cred_guard_mutex); 24718f661bcSStephen Wilson if (err) 2488b0db9dbSStephen Wilson return ERR_PTR(err); 24918f661bcSStephen Wilson 2508b0db9dbSStephen Wilson mm = __check_mem_permission(task); 25118f661bcSStephen Wilson mutex_unlock(&task->signal->cred_guard_mutex); 25218f661bcSStephen Wilson 2538b0db9dbSStephen Wilson return mm; 2541da177e4SLinus Torvalds } 2551da177e4SLinus Torvalds 256831830b5SAl Viro struct mm_struct *mm_for_maps(struct task_struct *task) 257831830b5SAl Viro { 258704b836cSOleg Nesterov struct mm_struct *mm; 259ec6fd8a4SAl Viro int err; 26000f89d21SOleg Nesterov 261ec6fd8a4SAl Viro err = mutex_lock_killable(&task->signal->cred_guard_mutex); 262ec6fd8a4SAl Viro if (err) 263ec6fd8a4SAl Viro return ERR_PTR(err); 264704b836cSOleg Nesterov 265704b836cSOleg Nesterov mm = get_task_mm(task); 266704b836cSOleg Nesterov if (mm && mm != current->mm && 267704b836cSOleg Nesterov !ptrace_may_access(task, PTRACE_MODE_READ)) { 268831830b5SAl Viro mmput(mm); 269ec6fd8a4SAl Viro mm = ERR_PTR(-EACCES); 270831830b5SAl Viro } 2719b1bf12dSKOSAKI Motohiro mutex_unlock(&task->signal->cred_guard_mutex); 272704b836cSOleg Nesterov 27313f0feafSOleg Nesterov return mm; 27413f0feafSOleg Nesterov } 275831830b5SAl Viro 2761da177e4SLinus Torvalds static int proc_pid_cmdline(struct task_struct *task, char * buffer) 2771da177e4SLinus Torvalds { 2781da177e4SLinus Torvalds int res = 0; 2791da177e4SLinus Torvalds unsigned int len; 2801da177e4SLinus Torvalds struct mm_struct *mm = get_task_mm(task); 2811da177e4SLinus Torvalds if (!mm) 2821da177e4SLinus Torvalds goto out; 2831da177e4SLinus Torvalds if (!mm->arg_end) 2841da177e4SLinus Torvalds goto out_mm; /* Shh! No looking before we're done */ 2851da177e4SLinus Torvalds 2861da177e4SLinus Torvalds len = mm->arg_end - mm->arg_start; 2871da177e4SLinus Torvalds 2881da177e4SLinus Torvalds if (len > PAGE_SIZE) 2891da177e4SLinus Torvalds len = PAGE_SIZE; 2901da177e4SLinus Torvalds 2911da177e4SLinus Torvalds res = access_process_vm(task, mm->arg_start, buffer, len, 0); 2921da177e4SLinus Torvalds 2931da177e4SLinus Torvalds // If the nul at the end of args has been overwritten, then 2941da177e4SLinus Torvalds // assume application is using setproctitle(3). 2951da177e4SLinus Torvalds if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) { 2961da177e4SLinus Torvalds len = strnlen(buffer, res); 2971da177e4SLinus Torvalds if (len < res) { 2981da177e4SLinus Torvalds res = len; 2991da177e4SLinus Torvalds } else { 3001da177e4SLinus Torvalds len = mm->env_end - mm->env_start; 3011da177e4SLinus Torvalds if (len > PAGE_SIZE - res) 3021da177e4SLinus Torvalds len = PAGE_SIZE - res; 3031da177e4SLinus Torvalds res += access_process_vm(task, mm->env_start, buffer+res, len, 0); 3041da177e4SLinus Torvalds res = strnlen(buffer, res); 3051da177e4SLinus Torvalds } 3061da177e4SLinus Torvalds } 3071da177e4SLinus Torvalds out_mm: 3081da177e4SLinus Torvalds mmput(mm); 3091da177e4SLinus Torvalds out: 3101da177e4SLinus Torvalds return res; 3111da177e4SLinus Torvalds } 3121da177e4SLinus Torvalds 3131da177e4SLinus Torvalds static int proc_pid_auxv(struct task_struct *task, char *buffer) 3141da177e4SLinus Torvalds { 3152fadaef4SAl Viro struct mm_struct *mm = mm_for_maps(task); 3162fadaef4SAl Viro int res = PTR_ERR(mm); 3172fadaef4SAl Viro if (mm && !IS_ERR(mm)) { 3181da177e4SLinus Torvalds unsigned int nwords = 0; 319dfe6b7d9SHannes Eder do { 3201da177e4SLinus Torvalds nwords += 2; 321dfe6b7d9SHannes Eder } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */ 3221da177e4SLinus Torvalds res = nwords * sizeof(mm->saved_auxv[0]); 3231da177e4SLinus Torvalds if (res > PAGE_SIZE) 3241da177e4SLinus Torvalds res = PAGE_SIZE; 3251da177e4SLinus Torvalds memcpy(buffer, mm->saved_auxv, res); 3261da177e4SLinus Torvalds mmput(mm); 3271da177e4SLinus Torvalds } 3281da177e4SLinus Torvalds return res; 3291da177e4SLinus Torvalds } 3301da177e4SLinus Torvalds 3311da177e4SLinus Torvalds 3321da177e4SLinus Torvalds #ifdef CONFIG_KALLSYMS 3331da177e4SLinus Torvalds /* 3341da177e4SLinus Torvalds * Provides a wchan file via kallsyms in a proper one-value-per-file format. 3351da177e4SLinus Torvalds * Returns the resolved symbol. If that fails, simply return the address. 3361da177e4SLinus Torvalds */ 3371da177e4SLinus Torvalds static int proc_pid_wchan(struct task_struct *task, char *buffer) 3381da177e4SLinus Torvalds { 339ffb45122SAlexey Dobriyan unsigned long wchan; 3409281aceaSTejun Heo char symname[KSYM_NAME_LEN]; 3411da177e4SLinus Torvalds 3421da177e4SLinus Torvalds wchan = get_wchan(task); 3431da177e4SLinus Torvalds 3449d65cb4aSAlexey Dobriyan if (lookup_symbol_name(wchan, symname) < 0) 345f83ce3e6SJake Edge if (!ptrace_may_access(task, PTRACE_MODE_READ)) 346f83ce3e6SJake Edge return 0; 347f83ce3e6SJake Edge else 3481da177e4SLinus Torvalds return sprintf(buffer, "%lu", wchan); 3499d65cb4aSAlexey Dobriyan else 3509d65cb4aSAlexey Dobriyan return sprintf(buffer, "%s", symname); 3511da177e4SLinus Torvalds } 3521da177e4SLinus Torvalds #endif /* CONFIG_KALLSYMS */ 3531da177e4SLinus Torvalds 354a9712bc1SAl Viro static int lock_trace(struct task_struct *task) 355a9712bc1SAl Viro { 356a9712bc1SAl Viro int err = mutex_lock_killable(&task->signal->cred_guard_mutex); 357a9712bc1SAl Viro if (err) 358a9712bc1SAl Viro return err; 359a9712bc1SAl Viro if (!ptrace_may_access(task, PTRACE_MODE_ATTACH)) { 360a9712bc1SAl Viro mutex_unlock(&task->signal->cred_guard_mutex); 361a9712bc1SAl Viro return -EPERM; 362a9712bc1SAl Viro } 363a9712bc1SAl Viro return 0; 364a9712bc1SAl Viro } 365a9712bc1SAl Viro 366a9712bc1SAl Viro static void unlock_trace(struct task_struct *task) 367a9712bc1SAl Viro { 368a9712bc1SAl Viro mutex_unlock(&task->signal->cred_guard_mutex); 369a9712bc1SAl Viro } 370a9712bc1SAl Viro 3712ec220e2SKen Chen #ifdef CONFIG_STACKTRACE 3722ec220e2SKen Chen 3732ec220e2SKen Chen #define MAX_STACK_TRACE_DEPTH 64 3742ec220e2SKen Chen 3752ec220e2SKen Chen static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns, 3762ec220e2SKen Chen struct pid *pid, struct task_struct *task) 3772ec220e2SKen Chen { 3782ec220e2SKen Chen struct stack_trace trace; 3792ec220e2SKen Chen unsigned long *entries; 380a9712bc1SAl Viro int err; 3812ec220e2SKen Chen int i; 3822ec220e2SKen Chen 3832ec220e2SKen Chen entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL); 3842ec220e2SKen Chen if (!entries) 3852ec220e2SKen Chen return -ENOMEM; 3862ec220e2SKen Chen 3872ec220e2SKen Chen trace.nr_entries = 0; 3882ec220e2SKen Chen trace.max_entries = MAX_STACK_TRACE_DEPTH; 3892ec220e2SKen Chen trace.entries = entries; 3902ec220e2SKen Chen trace.skip = 0; 391a9712bc1SAl Viro 392a9712bc1SAl Viro err = lock_trace(task); 393a9712bc1SAl Viro if (!err) { 3942ec220e2SKen Chen save_stack_trace_tsk(task, &trace); 3952ec220e2SKen Chen 3962ec220e2SKen Chen for (i = 0; i < trace.nr_entries; i++) { 39751e03149SKonstantin Khlebnikov seq_printf(m, "[<%pK>] %pS\n", 3982ec220e2SKen Chen (void *)entries[i], (void *)entries[i]); 3992ec220e2SKen Chen } 400a9712bc1SAl Viro unlock_trace(task); 401a9712bc1SAl Viro } 4022ec220e2SKen Chen kfree(entries); 4032ec220e2SKen Chen 404a9712bc1SAl Viro return err; 4052ec220e2SKen Chen } 4062ec220e2SKen Chen #endif 4072ec220e2SKen Chen 4081da177e4SLinus Torvalds #ifdef CONFIG_SCHEDSTATS 4091da177e4SLinus Torvalds /* 4101da177e4SLinus Torvalds * Provides /proc/PID/schedstat 4111da177e4SLinus Torvalds */ 4121da177e4SLinus Torvalds static int proc_pid_schedstat(struct task_struct *task, char *buffer) 4131da177e4SLinus Torvalds { 414172ba844SBalbir Singh return sprintf(buffer, "%llu %llu %lu\n", 415826e08b0SIngo Molnar (unsigned long long)task->se.sum_exec_runtime, 416826e08b0SIngo Molnar (unsigned long long)task->sched_info.run_delay, 4172d72376bSIngo Molnar task->sched_info.pcount); 4181da177e4SLinus Torvalds } 4191da177e4SLinus Torvalds #endif 4201da177e4SLinus Torvalds 4219745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP 4229745512cSArjan van de Ven static int lstats_show_proc(struct seq_file *m, void *v) 4239745512cSArjan van de Ven { 4249745512cSArjan van de Ven int i; 42513d77c37SHiroshi Shimamoto struct inode *inode = m->private; 42613d77c37SHiroshi Shimamoto struct task_struct *task = get_proc_task(inode); 4279745512cSArjan van de Ven 42813d77c37SHiroshi Shimamoto if (!task) 42913d77c37SHiroshi Shimamoto return -ESRCH; 43013d77c37SHiroshi Shimamoto seq_puts(m, "Latency Top version : v0.1\n"); 4319745512cSArjan van de Ven for (i = 0; i < 32; i++) { 43234e49d4fSJoe Perches struct latency_record *lr = &task->latency_record[i]; 43334e49d4fSJoe Perches if (lr->backtrace[0]) { 4349745512cSArjan van de Ven int q; 4359745512cSArjan van de Ven seq_printf(m, "%i %li %li", 43634e49d4fSJoe Perches lr->count, lr->time, lr->max); 4379745512cSArjan van de Ven for (q = 0; q < LT_BACKTRACEDEPTH; q++) { 43834e49d4fSJoe Perches unsigned long bt = lr->backtrace[q]; 43934e49d4fSJoe Perches if (!bt) 4409745512cSArjan van de Ven break; 44134e49d4fSJoe Perches if (bt == ULONG_MAX) 4429745512cSArjan van de Ven break; 44334e49d4fSJoe Perches seq_printf(m, " %ps", (void *)bt); 4449745512cSArjan van de Ven } 4459d6de12fSAlexey Dobriyan seq_putc(m, '\n'); 4469745512cSArjan van de Ven } 4479745512cSArjan van de Ven 4489745512cSArjan van de Ven } 44913d77c37SHiroshi Shimamoto put_task_struct(task); 4509745512cSArjan van de Ven return 0; 4519745512cSArjan van de Ven } 4529745512cSArjan van de Ven 4539745512cSArjan van de Ven static int lstats_open(struct inode *inode, struct file *file) 4549745512cSArjan van de Ven { 45513d77c37SHiroshi Shimamoto return single_open(file, lstats_show_proc, inode); 456d6643d12SHiroshi Shimamoto } 457d6643d12SHiroshi Shimamoto 4589745512cSArjan van de Ven static ssize_t lstats_write(struct file *file, const char __user *buf, 4599745512cSArjan van de Ven size_t count, loff_t *offs) 4609745512cSArjan van de Ven { 46113d77c37SHiroshi Shimamoto struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 4629745512cSArjan van de Ven 46313d77c37SHiroshi Shimamoto if (!task) 46413d77c37SHiroshi Shimamoto return -ESRCH; 4659745512cSArjan van de Ven clear_all_latency_tracing(task); 46613d77c37SHiroshi Shimamoto put_task_struct(task); 4679745512cSArjan van de Ven 4689745512cSArjan van de Ven return count; 4699745512cSArjan van de Ven } 4709745512cSArjan van de Ven 4719745512cSArjan van de Ven static const struct file_operations proc_lstats_operations = { 4729745512cSArjan van de Ven .open = lstats_open, 4739745512cSArjan van de Ven .read = seq_read, 4749745512cSArjan van de Ven .write = lstats_write, 4759745512cSArjan van de Ven .llseek = seq_lseek, 47613d77c37SHiroshi Shimamoto .release = single_release, 4779745512cSArjan van de Ven }; 4789745512cSArjan van de Ven 4799745512cSArjan van de Ven #endif 4809745512cSArjan van de Ven 4811da177e4SLinus Torvalds static int proc_oom_score(struct task_struct *task, char *buffer) 4821da177e4SLinus Torvalds { 483b95c35e7SOleg Nesterov unsigned long points = 0; 4841da177e4SLinus Torvalds 48519c5d45aSAlexey Dobriyan read_lock(&tasklist_lock); 486b95c35e7SOleg Nesterov if (pid_alive(task)) 487a63d83f4SDavid Rientjes points = oom_badness(task, NULL, NULL, 488a63d83f4SDavid Rientjes totalram_pages + total_swap_pages); 48919c5d45aSAlexey Dobriyan read_unlock(&tasklist_lock); 4901da177e4SLinus Torvalds return sprintf(buffer, "%lu\n", points); 4911da177e4SLinus Torvalds } 4921da177e4SLinus Torvalds 493d85f50d5SNeil Horman struct limit_names { 494d85f50d5SNeil Horman char *name; 495d85f50d5SNeil Horman char *unit; 496d85f50d5SNeil Horman }; 497d85f50d5SNeil Horman 498d85f50d5SNeil Horman static const struct limit_names lnames[RLIM_NLIMITS] = { 499cff4edb5SKees Cook [RLIMIT_CPU] = {"Max cpu time", "seconds"}, 500d85f50d5SNeil Horman [RLIMIT_FSIZE] = {"Max file size", "bytes"}, 501d85f50d5SNeil Horman [RLIMIT_DATA] = {"Max data size", "bytes"}, 502d85f50d5SNeil Horman [RLIMIT_STACK] = {"Max stack size", "bytes"}, 503d85f50d5SNeil Horman [RLIMIT_CORE] = {"Max core file size", "bytes"}, 504d85f50d5SNeil Horman [RLIMIT_RSS] = {"Max resident set", "bytes"}, 505d85f50d5SNeil Horman [RLIMIT_NPROC] = {"Max processes", "processes"}, 506d85f50d5SNeil Horman [RLIMIT_NOFILE] = {"Max open files", "files"}, 507d85f50d5SNeil Horman [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"}, 508d85f50d5SNeil Horman [RLIMIT_AS] = {"Max address space", "bytes"}, 509d85f50d5SNeil Horman [RLIMIT_LOCKS] = {"Max file locks", "locks"}, 510d85f50d5SNeil Horman [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"}, 511d85f50d5SNeil Horman [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"}, 512d85f50d5SNeil Horman [RLIMIT_NICE] = {"Max nice priority", NULL}, 513d85f50d5SNeil Horman [RLIMIT_RTPRIO] = {"Max realtime priority", NULL}, 5148808117cSEugene Teo [RLIMIT_RTTIME] = {"Max realtime timeout", "us"}, 515d85f50d5SNeil Horman }; 516d85f50d5SNeil Horman 517d85f50d5SNeil Horman /* Display limits for a process */ 518d85f50d5SNeil Horman static int proc_pid_limits(struct task_struct *task, char *buffer) 519d85f50d5SNeil Horman { 520d85f50d5SNeil Horman unsigned int i; 521d85f50d5SNeil Horman int count = 0; 522d85f50d5SNeil Horman unsigned long flags; 523d85f50d5SNeil Horman char *bufptr = buffer; 524d85f50d5SNeil Horman 525d85f50d5SNeil Horman struct rlimit rlim[RLIM_NLIMITS]; 526d85f50d5SNeil Horman 527a6bebbc8SLai Jiangshan if (!lock_task_sighand(task, &flags)) 528d85f50d5SNeil Horman return 0; 529d85f50d5SNeil Horman memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS); 530d85f50d5SNeil Horman unlock_task_sighand(task, &flags); 531d85f50d5SNeil Horman 532d85f50d5SNeil Horman /* 533d85f50d5SNeil Horman * print the file header 534d85f50d5SNeil Horman */ 535d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n", 536d85f50d5SNeil Horman "Limit", "Soft Limit", "Hard Limit", "Units"); 537d85f50d5SNeil Horman 538d85f50d5SNeil Horman for (i = 0; i < RLIM_NLIMITS; i++) { 539d85f50d5SNeil Horman if (rlim[i].rlim_cur == RLIM_INFINITY) 540d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-25s %-20s ", 541d85f50d5SNeil Horman lnames[i].name, "unlimited"); 542d85f50d5SNeil Horman else 543d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-25s %-20lu ", 544d85f50d5SNeil Horman lnames[i].name, rlim[i].rlim_cur); 545d85f50d5SNeil Horman 546d85f50d5SNeil Horman if (rlim[i].rlim_max == RLIM_INFINITY) 547d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-20s ", "unlimited"); 548d85f50d5SNeil Horman else 549d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-20lu ", 550d85f50d5SNeil Horman rlim[i].rlim_max); 551d85f50d5SNeil Horman 552d85f50d5SNeil Horman if (lnames[i].unit) 553d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-10s\n", 554d85f50d5SNeil Horman lnames[i].unit); 555d85f50d5SNeil Horman else 556d85f50d5SNeil Horman count += sprintf(&bufptr[count], "\n"); 557d85f50d5SNeil Horman } 558d85f50d5SNeil Horman 559d85f50d5SNeil Horman return count; 560d85f50d5SNeil Horman } 561d85f50d5SNeil Horman 562ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK 563ebcb6734SRoland McGrath static int proc_pid_syscall(struct task_struct *task, char *buffer) 564ebcb6734SRoland McGrath { 565ebcb6734SRoland McGrath long nr; 566ebcb6734SRoland McGrath unsigned long args[6], sp, pc; 567a9712bc1SAl Viro int res = lock_trace(task); 568a9712bc1SAl Viro if (res) 569a9712bc1SAl Viro return res; 570ebcb6734SRoland McGrath 571ebcb6734SRoland McGrath if (task_current_syscall(task, &nr, args, 6, &sp, &pc)) 572a9712bc1SAl Viro res = sprintf(buffer, "running\n"); 573a9712bc1SAl Viro else if (nr < 0) 574a9712bc1SAl Viro res = sprintf(buffer, "%ld 0x%lx 0x%lx\n", nr, sp, pc); 575a9712bc1SAl Viro else 576a9712bc1SAl Viro res = sprintf(buffer, 577ebcb6734SRoland McGrath "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n", 578ebcb6734SRoland McGrath nr, 579ebcb6734SRoland McGrath args[0], args[1], args[2], args[3], args[4], args[5], 580ebcb6734SRoland McGrath sp, pc); 581a9712bc1SAl Viro unlock_trace(task); 582a9712bc1SAl Viro return res; 583ebcb6734SRoland McGrath } 584ebcb6734SRoland McGrath #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */ 585ebcb6734SRoland McGrath 5861da177e4SLinus Torvalds /************************************************************************/ 5871da177e4SLinus Torvalds /* Here the fs part begins */ 5881da177e4SLinus Torvalds /************************************************************************/ 5891da177e4SLinus Torvalds 5901da177e4SLinus Torvalds /* permission checks */ 591778c1144SEric W. Biederman static int proc_fd_access_allowed(struct inode *inode) 5921da177e4SLinus Torvalds { 593778c1144SEric W. Biederman struct task_struct *task; 594778c1144SEric W. Biederman int allowed = 0; 595df26c40eSEric W. Biederman /* Allow access to a task's file descriptors if it is us or we 596df26c40eSEric W. Biederman * may use ptrace attach to the process and find out that 597df26c40eSEric W. Biederman * information. 598778c1144SEric W. Biederman */ 599778c1144SEric W. Biederman task = get_proc_task(inode); 600df26c40eSEric W. Biederman if (task) { 601006ebb40SStephen Smalley allowed = ptrace_may_access(task, PTRACE_MODE_READ); 602778c1144SEric W. Biederman put_task_struct(task); 603df26c40eSEric W. Biederman } 604778c1144SEric W. Biederman return allowed; 6051da177e4SLinus Torvalds } 6061da177e4SLinus Torvalds 6076b4e306aSEric W. Biederman int proc_setattr(struct dentry *dentry, struct iattr *attr) 6086d76fa58SLinus Torvalds { 6096d76fa58SLinus Torvalds int error; 6106d76fa58SLinus Torvalds struct inode *inode = dentry->d_inode; 6116d76fa58SLinus Torvalds 6126d76fa58SLinus Torvalds if (attr->ia_valid & ATTR_MODE) 6136d76fa58SLinus Torvalds return -EPERM; 6146d76fa58SLinus Torvalds 6156d76fa58SLinus Torvalds error = inode_change_ok(inode, attr); 6161025774cSChristoph Hellwig if (error) 6176d76fa58SLinus Torvalds return error; 6181025774cSChristoph Hellwig 6191025774cSChristoph Hellwig if ((attr->ia_valid & ATTR_SIZE) && 6201025774cSChristoph Hellwig attr->ia_size != i_size_read(inode)) { 6211025774cSChristoph Hellwig error = vmtruncate(inode, attr->ia_size); 6221025774cSChristoph Hellwig if (error) 6231025774cSChristoph Hellwig return error; 6241025774cSChristoph Hellwig } 6251025774cSChristoph Hellwig 6261025774cSChristoph Hellwig setattr_copy(inode, attr); 6271025774cSChristoph Hellwig mark_inode_dirty(inode); 6281025774cSChristoph Hellwig return 0; 6296d76fa58SLinus Torvalds } 6306d76fa58SLinus Torvalds 631c5ef1c42SArjan van de Ven static const struct inode_operations proc_def_inode_operations = { 6326d76fa58SLinus Torvalds .setattr = proc_setattr, 6336d76fa58SLinus Torvalds }; 6346d76fa58SLinus Torvalds 6351da177e4SLinus Torvalds #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */ 6361da177e4SLinus Torvalds 6371da177e4SLinus Torvalds static ssize_t proc_info_read(struct file * file, char __user * buf, 6381da177e4SLinus Torvalds size_t count, loff_t *ppos) 6391da177e4SLinus Torvalds { 6402fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 6411da177e4SLinus Torvalds unsigned long page; 6421da177e4SLinus Torvalds ssize_t length; 64399f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 64499f89551SEric W. Biederman 64599f89551SEric W. Biederman length = -ESRCH; 64699f89551SEric W. Biederman if (!task) 64799f89551SEric W. Biederman goto out_no_task; 6481da177e4SLinus Torvalds 6491da177e4SLinus Torvalds if (count > PROC_BLOCK_SIZE) 6501da177e4SLinus Torvalds count = PROC_BLOCK_SIZE; 65199f89551SEric W. Biederman 65299f89551SEric W. Biederman length = -ENOMEM; 653e12ba74dSMel Gorman if (!(page = __get_free_page(GFP_TEMPORARY))) 65499f89551SEric W. Biederman goto out; 6551da177e4SLinus Torvalds 6561da177e4SLinus Torvalds length = PROC_I(inode)->op.proc_read(task, (char*)page); 6571da177e4SLinus Torvalds 6581da177e4SLinus Torvalds if (length >= 0) 6591da177e4SLinus Torvalds length = simple_read_from_buffer(buf, count, ppos, (char *)page, length); 6601da177e4SLinus Torvalds free_page(page); 66199f89551SEric W. Biederman out: 66299f89551SEric W. Biederman put_task_struct(task); 66399f89551SEric W. Biederman out_no_task: 6641da177e4SLinus Torvalds return length; 6651da177e4SLinus Torvalds } 6661da177e4SLinus Torvalds 66700977a59SArjan van de Ven static const struct file_operations proc_info_file_operations = { 6681da177e4SLinus Torvalds .read = proc_info_read, 66987df8424SArnd Bergmann .llseek = generic_file_llseek, 6701da177e4SLinus Torvalds }; 6711da177e4SLinus Torvalds 672be614086SEric W. Biederman static int proc_single_show(struct seq_file *m, void *v) 673be614086SEric W. Biederman { 674be614086SEric W. Biederman struct inode *inode = m->private; 675be614086SEric W. Biederman struct pid_namespace *ns; 676be614086SEric W. Biederman struct pid *pid; 677be614086SEric W. Biederman struct task_struct *task; 678be614086SEric W. Biederman int ret; 679be614086SEric W. Biederman 680be614086SEric W. Biederman ns = inode->i_sb->s_fs_info; 681be614086SEric W. Biederman pid = proc_pid(inode); 682be614086SEric W. Biederman task = get_pid_task(pid, PIDTYPE_PID); 683be614086SEric W. Biederman if (!task) 684be614086SEric W. Biederman return -ESRCH; 685be614086SEric W. Biederman 686be614086SEric W. Biederman ret = PROC_I(inode)->op.proc_show(m, ns, pid, task); 687be614086SEric W. Biederman 688be614086SEric W. Biederman put_task_struct(task); 689be614086SEric W. Biederman return ret; 690be614086SEric W. Biederman } 691be614086SEric W. Biederman 692be614086SEric W. Biederman static int proc_single_open(struct inode *inode, struct file *filp) 693be614086SEric W. Biederman { 694c6a34058SJovi Zhang return single_open(filp, proc_single_show, inode); 695be614086SEric W. Biederman } 696be614086SEric W. Biederman 697be614086SEric W. Biederman static const struct file_operations proc_single_file_operations = { 698be614086SEric W. Biederman .open = proc_single_open, 699be614086SEric W. Biederman .read = seq_read, 700be614086SEric W. Biederman .llseek = seq_lseek, 701be614086SEric W. Biederman .release = single_release, 702be614086SEric W. Biederman }; 703be614086SEric W. Biederman 7041da177e4SLinus Torvalds static int mem_open(struct inode* inode, struct file* file) 7051da177e4SLinus Torvalds { 7061da177e4SLinus Torvalds file->private_data = (void*)((long)current->self_exec_id); 7074a3956c7SKAMEZAWA Hiroyuki /* OK to pass negative loff_t, we can catch out-of-range */ 7084a3956c7SKAMEZAWA Hiroyuki file->f_mode |= FMODE_UNSIGNED_OFFSET; 7091da177e4SLinus Torvalds return 0; 7101da177e4SLinus Torvalds } 7111da177e4SLinus Torvalds 7121da177e4SLinus Torvalds static ssize_t mem_read(struct file * file, char __user * buf, 7131da177e4SLinus Torvalds size_t count, loff_t *ppos) 7141da177e4SLinus Torvalds { 7152fddfeefSJosef "Jeff" Sipek struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode); 7161da177e4SLinus Torvalds char *page; 7171da177e4SLinus Torvalds unsigned long src = *ppos; 7181da177e4SLinus Torvalds int ret = -ESRCH; 7191da177e4SLinus Torvalds struct mm_struct *mm; 7201da177e4SLinus Torvalds 72199f89551SEric W. Biederman if (!task) 72299f89551SEric W. Biederman goto out_no_task; 72399f89551SEric W. Biederman 7241da177e4SLinus Torvalds ret = -ENOMEM; 725e12ba74dSMel Gorman page = (char *)__get_free_page(GFP_TEMPORARY); 7261da177e4SLinus Torvalds if (!page) 7271da177e4SLinus Torvalds goto out; 7281da177e4SLinus Torvalds 7298b0db9dbSStephen Wilson mm = check_mem_permission(task); 7308b0db9dbSStephen Wilson ret = PTR_ERR(mm); 7318b0db9dbSStephen Wilson if (IS_ERR(mm)) 7321da177e4SLinus Torvalds goto out_free; 7331da177e4SLinus Torvalds 7341da177e4SLinus Torvalds ret = -EIO; 7351da177e4SLinus Torvalds 7361da177e4SLinus Torvalds if (file->private_data != (void*)((long)current->self_exec_id)) 7371da177e4SLinus Torvalds goto out_put; 7381da177e4SLinus Torvalds 7391da177e4SLinus Torvalds ret = 0; 7401da177e4SLinus Torvalds 7411da177e4SLinus Torvalds while (count > 0) { 7421da177e4SLinus Torvalds int this_len, retval; 7431da177e4SLinus Torvalds 7441da177e4SLinus Torvalds this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count; 7458b0db9dbSStephen Wilson retval = access_remote_vm(mm, src, page, this_len, 0); 7468b0db9dbSStephen Wilson if (!retval) { 7471da177e4SLinus Torvalds if (!ret) 7481da177e4SLinus Torvalds ret = -EIO; 7491da177e4SLinus Torvalds break; 7501da177e4SLinus Torvalds } 7511da177e4SLinus Torvalds 7521da177e4SLinus Torvalds if (copy_to_user(buf, page, retval)) { 7531da177e4SLinus Torvalds ret = -EFAULT; 7541da177e4SLinus Torvalds break; 7551da177e4SLinus Torvalds } 7561da177e4SLinus Torvalds 7571da177e4SLinus Torvalds ret += retval; 7581da177e4SLinus Torvalds src += retval; 7591da177e4SLinus Torvalds buf += retval; 7601da177e4SLinus Torvalds count -= retval; 7611da177e4SLinus Torvalds } 7621da177e4SLinus Torvalds *ppos = src; 7631da177e4SLinus Torvalds 7641da177e4SLinus Torvalds out_put: 7651da177e4SLinus Torvalds mmput(mm); 7661da177e4SLinus Torvalds out_free: 7671da177e4SLinus Torvalds free_page((unsigned long) page); 7681da177e4SLinus Torvalds out: 76999f89551SEric W. Biederman put_task_struct(task); 77099f89551SEric W. Biederman out_no_task: 7711da177e4SLinus Torvalds return ret; 7721da177e4SLinus Torvalds } 7731da177e4SLinus Torvalds 77463967fa9SGlauber de Oliveira Costa static ssize_t mem_write(struct file * file, const char __user *buf, 7751da177e4SLinus Torvalds size_t count, loff_t *ppos) 7761da177e4SLinus Torvalds { 777f7ca54f4SFrederik Deweerdt int copied; 7781da177e4SLinus Torvalds char *page; 7792fddfeefSJosef "Jeff" Sipek struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode); 7801da177e4SLinus Torvalds unsigned long dst = *ppos; 7818b0db9dbSStephen Wilson struct mm_struct *mm; 7821da177e4SLinus Torvalds 78399f89551SEric W. Biederman copied = -ESRCH; 78499f89551SEric W. Biederman if (!task) 78599f89551SEric W. Biederman goto out_no_task; 7861da177e4SLinus Torvalds 78799f89551SEric W. Biederman copied = -ENOMEM; 788e12ba74dSMel Gorman page = (char *)__get_free_page(GFP_TEMPORARY); 7891da177e4SLinus Torvalds if (!page) 79030cd8903SKOSAKI Motohiro goto out_task; 79130cd8903SKOSAKI Motohiro 79230cd8903SKOSAKI Motohiro mm = check_mem_permission(task); 79330cd8903SKOSAKI Motohiro copied = PTR_ERR(mm); 79430cd8903SKOSAKI Motohiro if (IS_ERR(mm)) 79530cd8903SKOSAKI Motohiro goto out_free; 79630cd8903SKOSAKI Motohiro 79730cd8903SKOSAKI Motohiro copied = -EIO; 79830cd8903SKOSAKI Motohiro if (file->private_data != (void *)((long)current->self_exec_id)) 7998b0db9dbSStephen Wilson goto out_mm; 8001da177e4SLinus Torvalds 801f7ca54f4SFrederik Deweerdt copied = 0; 8021da177e4SLinus Torvalds while (count > 0) { 8031da177e4SLinus Torvalds int this_len, retval; 8041da177e4SLinus Torvalds 8051da177e4SLinus Torvalds this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count; 8061da177e4SLinus Torvalds if (copy_from_user(page, buf, this_len)) { 8071da177e4SLinus Torvalds copied = -EFAULT; 8081da177e4SLinus Torvalds break; 8091da177e4SLinus Torvalds } 8108b0db9dbSStephen Wilson retval = access_remote_vm(mm, dst, page, this_len, 1); 8111da177e4SLinus Torvalds if (!retval) { 8121da177e4SLinus Torvalds if (!copied) 8131da177e4SLinus Torvalds copied = -EIO; 8141da177e4SLinus Torvalds break; 8151da177e4SLinus Torvalds } 8161da177e4SLinus Torvalds copied += retval; 8171da177e4SLinus Torvalds buf += retval; 8181da177e4SLinus Torvalds dst += retval; 8191da177e4SLinus Torvalds count -= retval; 8201da177e4SLinus Torvalds } 8211da177e4SLinus Torvalds *ppos = dst; 82230cd8903SKOSAKI Motohiro 8238b0db9dbSStephen Wilson out_mm: 8248b0db9dbSStephen Wilson mmput(mm); 82530cd8903SKOSAKI Motohiro out_free: 82630cd8903SKOSAKI Motohiro free_page((unsigned long) page); 8278b0db9dbSStephen Wilson out_task: 82899f89551SEric W. Biederman put_task_struct(task); 82999f89551SEric W. Biederman out_no_task: 8301da177e4SLinus Torvalds return copied; 8311da177e4SLinus Torvalds } 8321da177e4SLinus Torvalds 83385863e47SMatt Mackall loff_t mem_lseek(struct file *file, loff_t offset, int orig) 8341da177e4SLinus Torvalds { 8351da177e4SLinus Torvalds switch (orig) { 8361da177e4SLinus Torvalds case 0: 8371da177e4SLinus Torvalds file->f_pos = offset; 8381da177e4SLinus Torvalds break; 8391da177e4SLinus Torvalds case 1: 8401da177e4SLinus Torvalds file->f_pos += offset; 8411da177e4SLinus Torvalds break; 8421da177e4SLinus Torvalds default: 8431da177e4SLinus Torvalds return -EINVAL; 8441da177e4SLinus Torvalds } 8451da177e4SLinus Torvalds force_successful_syscall_return(); 8461da177e4SLinus Torvalds return file->f_pos; 8471da177e4SLinus Torvalds } 8481da177e4SLinus Torvalds 84900977a59SArjan van de Ven static const struct file_operations proc_mem_operations = { 8501da177e4SLinus Torvalds .llseek = mem_lseek, 8511da177e4SLinus Torvalds .read = mem_read, 8521da177e4SLinus Torvalds .write = mem_write, 8531da177e4SLinus Torvalds .open = mem_open, 8541da177e4SLinus Torvalds }; 8551da177e4SLinus Torvalds 856315e28c8SJames Pearson static ssize_t environ_read(struct file *file, char __user *buf, 857315e28c8SJames Pearson size_t count, loff_t *ppos) 858315e28c8SJames Pearson { 859315e28c8SJames Pearson struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 860315e28c8SJames Pearson char *page; 861315e28c8SJames Pearson unsigned long src = *ppos; 862315e28c8SJames Pearson int ret = -ESRCH; 863315e28c8SJames Pearson struct mm_struct *mm; 864315e28c8SJames Pearson 865315e28c8SJames Pearson if (!task) 866315e28c8SJames Pearson goto out_no_task; 867315e28c8SJames Pearson 868315e28c8SJames Pearson ret = -ENOMEM; 869315e28c8SJames Pearson page = (char *)__get_free_page(GFP_TEMPORARY); 870315e28c8SJames Pearson if (!page) 871315e28c8SJames Pearson goto out; 872315e28c8SJames Pearson 873315e28c8SJames Pearson 874d6f64b89SAl Viro mm = mm_for_maps(task); 875d6f64b89SAl Viro ret = PTR_ERR(mm); 876d6f64b89SAl Viro if (!mm || IS_ERR(mm)) 877315e28c8SJames Pearson goto out_free; 878315e28c8SJames Pearson 879d6f64b89SAl Viro ret = 0; 880315e28c8SJames Pearson while (count > 0) { 881315e28c8SJames Pearson int this_len, retval, max_len; 882315e28c8SJames Pearson 883315e28c8SJames Pearson this_len = mm->env_end - (mm->env_start + src); 884315e28c8SJames Pearson 885315e28c8SJames Pearson if (this_len <= 0) 886315e28c8SJames Pearson break; 887315e28c8SJames Pearson 888315e28c8SJames Pearson max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count; 889315e28c8SJames Pearson this_len = (this_len > max_len) ? max_len : this_len; 890315e28c8SJames Pearson 891315e28c8SJames Pearson retval = access_process_vm(task, (mm->env_start + src), 892315e28c8SJames Pearson page, this_len, 0); 893315e28c8SJames Pearson 894315e28c8SJames Pearson if (retval <= 0) { 895315e28c8SJames Pearson ret = retval; 896315e28c8SJames Pearson break; 897315e28c8SJames Pearson } 898315e28c8SJames Pearson 899315e28c8SJames Pearson if (copy_to_user(buf, page, retval)) { 900315e28c8SJames Pearson ret = -EFAULT; 901315e28c8SJames Pearson break; 902315e28c8SJames Pearson } 903315e28c8SJames Pearson 904315e28c8SJames Pearson ret += retval; 905315e28c8SJames Pearson src += retval; 906315e28c8SJames Pearson buf += retval; 907315e28c8SJames Pearson count -= retval; 908315e28c8SJames Pearson } 909315e28c8SJames Pearson *ppos = src; 910315e28c8SJames Pearson 911315e28c8SJames Pearson mmput(mm); 912315e28c8SJames Pearson out_free: 913315e28c8SJames Pearson free_page((unsigned long) page); 914315e28c8SJames Pearson out: 915315e28c8SJames Pearson put_task_struct(task); 916315e28c8SJames Pearson out_no_task: 917315e28c8SJames Pearson return ret; 918315e28c8SJames Pearson } 919315e28c8SJames Pearson 920315e28c8SJames Pearson static const struct file_operations proc_environ_operations = { 921315e28c8SJames Pearson .read = environ_read, 92287df8424SArnd Bergmann .llseek = generic_file_llseek, 923315e28c8SJames Pearson }; 924315e28c8SJames Pearson 9251da177e4SLinus Torvalds static ssize_t oom_adjust_read(struct file *file, char __user *buf, 9261da177e4SLinus Torvalds size_t count, loff_t *ppos) 9271da177e4SLinus Torvalds { 9282fddfeefSJosef "Jeff" Sipek struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode); 9298578cea7SEric W. Biederman char buffer[PROC_NUMBUF]; 9301da177e4SLinus Torvalds size_t len; 93128b83c51SKOSAKI Motohiro int oom_adjust = OOM_DISABLE; 93228b83c51SKOSAKI Motohiro unsigned long flags; 9331da177e4SLinus Torvalds 93499f89551SEric W. Biederman if (!task) 93599f89551SEric W. Biederman return -ESRCH; 93628b83c51SKOSAKI Motohiro 93728b83c51SKOSAKI Motohiro if (lock_task_sighand(task, &flags)) { 93828b83c51SKOSAKI Motohiro oom_adjust = task->signal->oom_adj; 93928b83c51SKOSAKI Motohiro unlock_task_sighand(task, &flags); 94028b83c51SKOSAKI Motohiro } 94128b83c51SKOSAKI Motohiro 94299f89551SEric W. Biederman put_task_struct(task); 94399f89551SEric W. Biederman 9448578cea7SEric W. Biederman len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust); 9450c28f287SAkinobu Mita 9460c28f287SAkinobu Mita return simple_read_from_buffer(buf, count, ppos, buffer, len); 9471da177e4SLinus Torvalds } 9481da177e4SLinus Torvalds 9491da177e4SLinus Torvalds static ssize_t oom_adjust_write(struct file *file, const char __user *buf, 9501da177e4SLinus Torvalds size_t count, loff_t *ppos) 9511da177e4SLinus Torvalds { 95299f89551SEric W. Biederman struct task_struct *task; 9535d863b89SKOSAKI Motohiro char buffer[PROC_NUMBUF]; 9540a8cb8e3SAlexey Dobriyan int oom_adjust; 95528b83c51SKOSAKI Motohiro unsigned long flags; 9565d863b89SKOSAKI Motohiro int err; 9571da177e4SLinus Torvalds 9588578cea7SEric W. Biederman memset(buffer, 0, sizeof(buffer)); 9598578cea7SEric W. Biederman if (count > sizeof(buffer) - 1) 9608578cea7SEric W. Biederman count = sizeof(buffer) - 1; 961723548bfSDavid Rientjes if (copy_from_user(buffer, buf, count)) { 962723548bfSDavid Rientjes err = -EFAULT; 963723548bfSDavid Rientjes goto out; 964723548bfSDavid Rientjes } 9655d863b89SKOSAKI Motohiro 9660a8cb8e3SAlexey Dobriyan err = kstrtoint(strstrip(buffer), 0, &oom_adjust); 9675d863b89SKOSAKI Motohiro if (err) 968723548bfSDavid Rientjes goto out; 9698ac773b4SAlexey Dobriyan if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) && 970723548bfSDavid Rientjes oom_adjust != OOM_DISABLE) { 971723548bfSDavid Rientjes err = -EINVAL; 972723548bfSDavid Rientjes goto out; 973723548bfSDavid Rientjes } 9745d863b89SKOSAKI Motohiro 9752fddfeefSJosef "Jeff" Sipek task = get_proc_task(file->f_path.dentry->d_inode); 976723548bfSDavid Rientjes if (!task) { 977723548bfSDavid Rientjes err = -ESRCH; 978723548bfSDavid Rientjes goto out; 979723548bfSDavid Rientjes } 98028b83c51SKOSAKI Motohiro 9813d5992d2SYing Han task_lock(task); 9823d5992d2SYing Han if (!task->mm) { 983723548bfSDavid Rientjes err = -EINVAL; 984723548bfSDavid Rientjes goto err_task_lock; 9853d5992d2SYing Han } 9863d5992d2SYing Han 987d19d5476SDavid Rientjes if (!lock_task_sighand(task, &flags)) { 988d19d5476SDavid Rientjes err = -ESRCH; 989d19d5476SDavid Rientjes goto err_task_lock; 990d19d5476SDavid Rientjes } 991d19d5476SDavid Rientjes 992d19d5476SDavid Rientjes if (oom_adjust < task->signal->oom_adj && !capable(CAP_SYS_RESOURCE)) { 993d19d5476SDavid Rientjes err = -EACCES; 994d19d5476SDavid Rientjes goto err_sighand; 995d19d5476SDavid Rientjes } 996d19d5476SDavid Rientjes 99751b1bd2aSDavid Rientjes /* 99851b1bd2aSDavid Rientjes * Warn that /proc/pid/oom_adj is deprecated, see 99951b1bd2aSDavid Rientjes * Documentation/feature-removal-schedule.txt. 100051b1bd2aSDavid Rientjes */ 1001c2142704SLinus Torvalds printk_once(KERN_WARNING "%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n", 1002be8f684dSDavid Rientjes current->comm, task_pid_nr(current), task_pid_nr(task), 1003be8f684dSDavid Rientjes task_pid_nr(task)); 100428b83c51SKOSAKI Motohiro task->signal->oom_adj = oom_adjust; 1005a63d83f4SDavid Rientjes /* 1006a63d83f4SDavid Rientjes * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum 1007a63d83f4SDavid Rientjes * value is always attainable. 1008a63d83f4SDavid Rientjes */ 1009a63d83f4SDavid Rientjes if (task->signal->oom_adj == OOM_ADJUST_MAX) 1010a63d83f4SDavid Rientjes task->signal->oom_score_adj = OOM_SCORE_ADJ_MAX; 1011a63d83f4SDavid Rientjes else 1012a63d83f4SDavid Rientjes task->signal->oom_score_adj = (oom_adjust * OOM_SCORE_ADJ_MAX) / 1013a63d83f4SDavid Rientjes -OOM_DISABLE; 1014*43d2b113SKAMEZAWA Hiroyuki trace_oom_score_adj_update(task); 1015723548bfSDavid Rientjes err_sighand: 101628b83c51SKOSAKI Motohiro unlock_task_sighand(task, &flags); 1017d19d5476SDavid Rientjes err_task_lock: 1018d19d5476SDavid Rientjes task_unlock(task); 101999f89551SEric W. Biederman put_task_struct(task); 1020723548bfSDavid Rientjes out: 1021723548bfSDavid Rientjes return err < 0 ? err : count; 10221da177e4SLinus Torvalds } 10231da177e4SLinus Torvalds 102400977a59SArjan van de Ven static const struct file_operations proc_oom_adjust_operations = { 10251da177e4SLinus Torvalds .read = oom_adjust_read, 10261da177e4SLinus Torvalds .write = oom_adjust_write, 102787df8424SArnd Bergmann .llseek = generic_file_llseek, 10281da177e4SLinus Torvalds }; 10291da177e4SLinus Torvalds 1030a63d83f4SDavid Rientjes static ssize_t oom_score_adj_read(struct file *file, char __user *buf, 1031a63d83f4SDavid Rientjes size_t count, loff_t *ppos) 1032a63d83f4SDavid Rientjes { 1033a63d83f4SDavid Rientjes struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode); 1034a63d83f4SDavid Rientjes char buffer[PROC_NUMBUF]; 1035a63d83f4SDavid Rientjes int oom_score_adj = OOM_SCORE_ADJ_MIN; 1036a63d83f4SDavid Rientjes unsigned long flags; 1037a63d83f4SDavid Rientjes size_t len; 1038a63d83f4SDavid Rientjes 1039a63d83f4SDavid Rientjes if (!task) 1040a63d83f4SDavid Rientjes return -ESRCH; 1041a63d83f4SDavid Rientjes if (lock_task_sighand(task, &flags)) { 1042a63d83f4SDavid Rientjes oom_score_adj = task->signal->oom_score_adj; 1043a63d83f4SDavid Rientjes unlock_task_sighand(task, &flags); 1044a63d83f4SDavid Rientjes } 1045a63d83f4SDavid Rientjes put_task_struct(task); 1046a63d83f4SDavid Rientjes len = snprintf(buffer, sizeof(buffer), "%d\n", oom_score_adj); 1047a63d83f4SDavid Rientjes return simple_read_from_buffer(buf, count, ppos, buffer, len); 1048a63d83f4SDavid Rientjes } 1049a63d83f4SDavid Rientjes 1050a63d83f4SDavid Rientjes static ssize_t oom_score_adj_write(struct file *file, const char __user *buf, 1051a63d83f4SDavid Rientjes size_t count, loff_t *ppos) 1052a63d83f4SDavid Rientjes { 1053a63d83f4SDavid Rientjes struct task_struct *task; 1054a63d83f4SDavid Rientjes char buffer[PROC_NUMBUF]; 1055a63d83f4SDavid Rientjes unsigned long flags; 10560a8cb8e3SAlexey Dobriyan int oom_score_adj; 1057a63d83f4SDavid Rientjes int err; 1058a63d83f4SDavid Rientjes 1059a63d83f4SDavid Rientjes memset(buffer, 0, sizeof(buffer)); 1060a63d83f4SDavid Rientjes if (count > sizeof(buffer) - 1) 1061a63d83f4SDavid Rientjes count = sizeof(buffer) - 1; 1062723548bfSDavid Rientjes if (copy_from_user(buffer, buf, count)) { 1063723548bfSDavid Rientjes err = -EFAULT; 1064723548bfSDavid Rientjes goto out; 1065723548bfSDavid Rientjes } 1066a63d83f4SDavid Rientjes 10670a8cb8e3SAlexey Dobriyan err = kstrtoint(strstrip(buffer), 0, &oom_score_adj); 1068a63d83f4SDavid Rientjes if (err) 1069723548bfSDavid Rientjes goto out; 1070a63d83f4SDavid Rientjes if (oom_score_adj < OOM_SCORE_ADJ_MIN || 1071723548bfSDavid Rientjes oom_score_adj > OOM_SCORE_ADJ_MAX) { 1072723548bfSDavid Rientjes err = -EINVAL; 1073723548bfSDavid Rientjes goto out; 1074723548bfSDavid Rientjes } 1075a63d83f4SDavid Rientjes 1076a63d83f4SDavid Rientjes task = get_proc_task(file->f_path.dentry->d_inode); 1077723548bfSDavid Rientjes if (!task) { 1078723548bfSDavid Rientjes err = -ESRCH; 1079723548bfSDavid Rientjes goto out; 1080723548bfSDavid Rientjes } 1081a63d83f4SDavid Rientjes 10823d5992d2SYing Han task_lock(task); 10833d5992d2SYing Han if (!task->mm) { 1084723548bfSDavid Rientjes err = -EINVAL; 1085723548bfSDavid Rientjes goto err_task_lock; 10863d5992d2SYing Han } 1087d19d5476SDavid Rientjes 1088d19d5476SDavid Rientjes if (!lock_task_sighand(task, &flags)) { 1089d19d5476SDavid Rientjes err = -ESRCH; 1090d19d5476SDavid Rientjes goto err_task_lock; 1091d19d5476SDavid Rientjes } 1092d19d5476SDavid Rientjes 1093dabb16f6SMandeep Singh Baines if (oom_score_adj < task->signal->oom_score_adj_min && 1094d19d5476SDavid Rientjes !capable(CAP_SYS_RESOURCE)) { 1095d19d5476SDavid Rientjes err = -EACCES; 1096d19d5476SDavid Rientjes goto err_sighand; 1097d19d5476SDavid Rientjes } 1098d19d5476SDavid Rientjes 1099a63d83f4SDavid Rientjes task->signal->oom_score_adj = oom_score_adj; 1100dabb16f6SMandeep Singh Baines if (has_capability_noaudit(current, CAP_SYS_RESOURCE)) 1101dabb16f6SMandeep Singh Baines task->signal->oom_score_adj_min = oom_score_adj; 1102*43d2b113SKAMEZAWA Hiroyuki trace_oom_score_adj_update(task); 1103a63d83f4SDavid Rientjes /* 1104a63d83f4SDavid Rientjes * Scale /proc/pid/oom_adj appropriately ensuring that OOM_DISABLE is 1105a63d83f4SDavid Rientjes * always attainable. 1106a63d83f4SDavid Rientjes */ 1107a63d83f4SDavid Rientjes if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MIN) 1108a63d83f4SDavid Rientjes task->signal->oom_adj = OOM_DISABLE; 1109a63d83f4SDavid Rientjes else 1110a63d83f4SDavid Rientjes task->signal->oom_adj = (oom_score_adj * OOM_ADJUST_MAX) / 1111a63d83f4SDavid Rientjes OOM_SCORE_ADJ_MAX; 1112723548bfSDavid Rientjes err_sighand: 1113a63d83f4SDavid Rientjes unlock_task_sighand(task, &flags); 1114d19d5476SDavid Rientjes err_task_lock: 1115d19d5476SDavid Rientjes task_unlock(task); 1116a63d83f4SDavid Rientjes put_task_struct(task); 1117723548bfSDavid Rientjes out: 1118723548bfSDavid Rientjes return err < 0 ? err : count; 1119a63d83f4SDavid Rientjes } 1120a63d83f4SDavid Rientjes 1121a63d83f4SDavid Rientjes static const struct file_operations proc_oom_score_adj_operations = { 1122a63d83f4SDavid Rientjes .read = oom_score_adj_read, 1123a63d83f4SDavid Rientjes .write = oom_score_adj_write, 11246038f373SArnd Bergmann .llseek = default_llseek, 1125a63d83f4SDavid Rientjes }; 1126a63d83f4SDavid Rientjes 11271da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL 11281da177e4SLinus Torvalds #define TMPBUFLEN 21 11291da177e4SLinus Torvalds static ssize_t proc_loginuid_read(struct file * file, char __user * buf, 11301da177e4SLinus Torvalds size_t count, loff_t *ppos) 11311da177e4SLinus Torvalds { 11322fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 113399f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 11341da177e4SLinus Torvalds ssize_t length; 11351da177e4SLinus Torvalds char tmpbuf[TMPBUFLEN]; 11361da177e4SLinus Torvalds 113799f89551SEric W. Biederman if (!task) 113899f89551SEric W. Biederman return -ESRCH; 11391da177e4SLinus Torvalds length = scnprintf(tmpbuf, TMPBUFLEN, "%u", 11400c11b942SAl Viro audit_get_loginuid(task)); 114199f89551SEric W. Biederman put_task_struct(task); 11421da177e4SLinus Torvalds return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 11431da177e4SLinus Torvalds } 11441da177e4SLinus Torvalds 11451da177e4SLinus Torvalds static ssize_t proc_loginuid_write(struct file * file, const char __user * buf, 11461da177e4SLinus Torvalds size_t count, loff_t *ppos) 11471da177e4SLinus Torvalds { 11482fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 11491da177e4SLinus Torvalds char *page, *tmp; 11501da177e4SLinus Torvalds ssize_t length; 11511da177e4SLinus Torvalds uid_t loginuid; 11521da177e4SLinus Torvalds 11531da177e4SLinus Torvalds if (!capable(CAP_AUDIT_CONTROL)) 11541da177e4SLinus Torvalds return -EPERM; 11551da177e4SLinus Torvalds 11567dc52157SPaul E. McKenney rcu_read_lock(); 11577dc52157SPaul E. McKenney if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) { 11587dc52157SPaul E. McKenney rcu_read_unlock(); 11591da177e4SLinus Torvalds return -EPERM; 11607dc52157SPaul E. McKenney } 11617dc52157SPaul E. McKenney rcu_read_unlock(); 11621da177e4SLinus Torvalds 1163e0182909SAl Viro if (count >= PAGE_SIZE) 1164e0182909SAl Viro count = PAGE_SIZE - 1; 11651da177e4SLinus Torvalds 11661da177e4SLinus Torvalds if (*ppos != 0) { 11671da177e4SLinus Torvalds /* No partial writes. */ 11681da177e4SLinus Torvalds return -EINVAL; 11691da177e4SLinus Torvalds } 1170e12ba74dSMel Gorman page = (char*)__get_free_page(GFP_TEMPORARY); 11711da177e4SLinus Torvalds if (!page) 11721da177e4SLinus Torvalds return -ENOMEM; 11731da177e4SLinus Torvalds length = -EFAULT; 11741da177e4SLinus Torvalds if (copy_from_user(page, buf, count)) 11751da177e4SLinus Torvalds goto out_free_page; 11761da177e4SLinus Torvalds 1177e0182909SAl Viro page[count] = '\0'; 11781da177e4SLinus Torvalds loginuid = simple_strtoul(page, &tmp, 10); 11791da177e4SLinus Torvalds if (tmp == page) { 11801da177e4SLinus Torvalds length = -EINVAL; 11811da177e4SLinus Torvalds goto out_free_page; 11821da177e4SLinus Torvalds 11831da177e4SLinus Torvalds } 118499f89551SEric W. Biederman length = audit_set_loginuid(current, loginuid); 11851da177e4SLinus Torvalds if (likely(length == 0)) 11861da177e4SLinus Torvalds length = count; 11871da177e4SLinus Torvalds 11881da177e4SLinus Torvalds out_free_page: 11891da177e4SLinus Torvalds free_page((unsigned long) page); 11901da177e4SLinus Torvalds return length; 11911da177e4SLinus Torvalds } 11921da177e4SLinus Torvalds 119300977a59SArjan van de Ven static const struct file_operations proc_loginuid_operations = { 11941da177e4SLinus Torvalds .read = proc_loginuid_read, 11951da177e4SLinus Torvalds .write = proc_loginuid_write, 119687df8424SArnd Bergmann .llseek = generic_file_llseek, 11971da177e4SLinus Torvalds }; 11981e0bd755SEric Paris 11991e0bd755SEric Paris static ssize_t proc_sessionid_read(struct file * file, char __user * buf, 12001e0bd755SEric Paris size_t count, loff_t *ppos) 12011e0bd755SEric Paris { 12021e0bd755SEric Paris struct inode * inode = file->f_path.dentry->d_inode; 12031e0bd755SEric Paris struct task_struct *task = get_proc_task(inode); 12041e0bd755SEric Paris ssize_t length; 12051e0bd755SEric Paris char tmpbuf[TMPBUFLEN]; 12061e0bd755SEric Paris 12071e0bd755SEric Paris if (!task) 12081e0bd755SEric Paris return -ESRCH; 12091e0bd755SEric Paris length = scnprintf(tmpbuf, TMPBUFLEN, "%u", 12101e0bd755SEric Paris audit_get_sessionid(task)); 12111e0bd755SEric Paris put_task_struct(task); 12121e0bd755SEric Paris return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 12131e0bd755SEric Paris } 12141e0bd755SEric Paris 12151e0bd755SEric Paris static const struct file_operations proc_sessionid_operations = { 12161e0bd755SEric Paris .read = proc_sessionid_read, 121787df8424SArnd Bergmann .llseek = generic_file_llseek, 12181e0bd755SEric Paris }; 12191da177e4SLinus Torvalds #endif 12201da177e4SLinus Torvalds 1221f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION 1222f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_read(struct file * file, char __user * buf, 1223f4f154fdSAkinobu Mita size_t count, loff_t *ppos) 1224f4f154fdSAkinobu Mita { 1225f4f154fdSAkinobu Mita struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 1226f4f154fdSAkinobu Mita char buffer[PROC_NUMBUF]; 1227f4f154fdSAkinobu Mita size_t len; 1228f4f154fdSAkinobu Mita int make_it_fail; 1229f4f154fdSAkinobu Mita 1230f4f154fdSAkinobu Mita if (!task) 1231f4f154fdSAkinobu Mita return -ESRCH; 1232f4f154fdSAkinobu Mita make_it_fail = task->make_it_fail; 1233f4f154fdSAkinobu Mita put_task_struct(task); 1234f4f154fdSAkinobu Mita 1235f4f154fdSAkinobu Mita len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail); 12360c28f287SAkinobu Mita 12370c28f287SAkinobu Mita return simple_read_from_buffer(buf, count, ppos, buffer, len); 1238f4f154fdSAkinobu Mita } 1239f4f154fdSAkinobu Mita 1240f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_write(struct file * file, 1241f4f154fdSAkinobu Mita const char __user * buf, size_t count, loff_t *ppos) 1242f4f154fdSAkinobu Mita { 1243f4f154fdSAkinobu Mita struct task_struct *task; 1244f4f154fdSAkinobu Mita char buffer[PROC_NUMBUF], *end; 1245f4f154fdSAkinobu Mita int make_it_fail; 1246f4f154fdSAkinobu Mita 1247f4f154fdSAkinobu Mita if (!capable(CAP_SYS_RESOURCE)) 1248f4f154fdSAkinobu Mita return -EPERM; 1249f4f154fdSAkinobu Mita memset(buffer, 0, sizeof(buffer)); 1250f4f154fdSAkinobu Mita if (count > sizeof(buffer) - 1) 1251f4f154fdSAkinobu Mita count = sizeof(buffer) - 1; 1252f4f154fdSAkinobu Mita if (copy_from_user(buffer, buf, count)) 1253f4f154fdSAkinobu Mita return -EFAULT; 1254cba8aafeSVincent Li make_it_fail = simple_strtol(strstrip(buffer), &end, 0); 1255cba8aafeSVincent Li if (*end) 1256cba8aafeSVincent Li return -EINVAL; 1257f4f154fdSAkinobu Mita task = get_proc_task(file->f_dentry->d_inode); 1258f4f154fdSAkinobu Mita if (!task) 1259f4f154fdSAkinobu Mita return -ESRCH; 1260f4f154fdSAkinobu Mita task->make_it_fail = make_it_fail; 1261f4f154fdSAkinobu Mita put_task_struct(task); 1262cba8aafeSVincent Li 1263cba8aafeSVincent Li return count; 1264f4f154fdSAkinobu Mita } 1265f4f154fdSAkinobu Mita 126600977a59SArjan van de Ven static const struct file_operations proc_fault_inject_operations = { 1267f4f154fdSAkinobu Mita .read = proc_fault_inject_read, 1268f4f154fdSAkinobu Mita .write = proc_fault_inject_write, 126987df8424SArnd Bergmann .llseek = generic_file_llseek, 1270f4f154fdSAkinobu Mita }; 1271f4f154fdSAkinobu Mita #endif 1272f4f154fdSAkinobu Mita 12739745512cSArjan van de Ven 127443ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG 127543ae34cbSIngo Molnar /* 127643ae34cbSIngo Molnar * Print out various scheduling related per-task fields: 127743ae34cbSIngo Molnar */ 127843ae34cbSIngo Molnar static int sched_show(struct seq_file *m, void *v) 127943ae34cbSIngo Molnar { 128043ae34cbSIngo Molnar struct inode *inode = m->private; 128143ae34cbSIngo Molnar struct task_struct *p; 128243ae34cbSIngo Molnar 128343ae34cbSIngo Molnar p = get_proc_task(inode); 128443ae34cbSIngo Molnar if (!p) 128543ae34cbSIngo Molnar return -ESRCH; 128643ae34cbSIngo Molnar proc_sched_show_task(p, m); 128743ae34cbSIngo Molnar 128843ae34cbSIngo Molnar put_task_struct(p); 128943ae34cbSIngo Molnar 129043ae34cbSIngo Molnar return 0; 129143ae34cbSIngo Molnar } 129243ae34cbSIngo Molnar 129343ae34cbSIngo Molnar static ssize_t 129443ae34cbSIngo Molnar sched_write(struct file *file, const char __user *buf, 129543ae34cbSIngo Molnar size_t count, loff_t *offset) 129643ae34cbSIngo Molnar { 129743ae34cbSIngo Molnar struct inode *inode = file->f_path.dentry->d_inode; 129843ae34cbSIngo Molnar struct task_struct *p; 129943ae34cbSIngo Molnar 130043ae34cbSIngo Molnar p = get_proc_task(inode); 130143ae34cbSIngo Molnar if (!p) 130243ae34cbSIngo Molnar return -ESRCH; 130343ae34cbSIngo Molnar proc_sched_set_task(p); 130443ae34cbSIngo Molnar 130543ae34cbSIngo Molnar put_task_struct(p); 130643ae34cbSIngo Molnar 130743ae34cbSIngo Molnar return count; 130843ae34cbSIngo Molnar } 130943ae34cbSIngo Molnar 131043ae34cbSIngo Molnar static int sched_open(struct inode *inode, struct file *filp) 131143ae34cbSIngo Molnar { 1312c6a34058SJovi Zhang return single_open(filp, sched_show, inode); 131343ae34cbSIngo Molnar } 131443ae34cbSIngo Molnar 131543ae34cbSIngo Molnar static const struct file_operations proc_pid_sched_operations = { 131643ae34cbSIngo Molnar .open = sched_open, 131743ae34cbSIngo Molnar .read = seq_read, 131843ae34cbSIngo Molnar .write = sched_write, 131943ae34cbSIngo Molnar .llseek = seq_lseek, 13205ea473a1SAlexey Dobriyan .release = single_release, 132143ae34cbSIngo Molnar }; 132243ae34cbSIngo Molnar 132343ae34cbSIngo Molnar #endif 132443ae34cbSIngo Molnar 13255091faa4SMike Galbraith #ifdef CONFIG_SCHED_AUTOGROUP 13265091faa4SMike Galbraith /* 13275091faa4SMike Galbraith * Print out autogroup related information: 13285091faa4SMike Galbraith */ 13295091faa4SMike Galbraith static int sched_autogroup_show(struct seq_file *m, void *v) 13305091faa4SMike Galbraith { 13315091faa4SMike Galbraith struct inode *inode = m->private; 13325091faa4SMike Galbraith struct task_struct *p; 13335091faa4SMike Galbraith 13345091faa4SMike Galbraith p = get_proc_task(inode); 13355091faa4SMike Galbraith if (!p) 13365091faa4SMike Galbraith return -ESRCH; 13375091faa4SMike Galbraith proc_sched_autogroup_show_task(p, m); 13385091faa4SMike Galbraith 13395091faa4SMike Galbraith put_task_struct(p); 13405091faa4SMike Galbraith 13415091faa4SMike Galbraith return 0; 13425091faa4SMike Galbraith } 13435091faa4SMike Galbraith 13445091faa4SMike Galbraith static ssize_t 13455091faa4SMike Galbraith sched_autogroup_write(struct file *file, const char __user *buf, 13465091faa4SMike Galbraith size_t count, loff_t *offset) 13475091faa4SMike Galbraith { 13485091faa4SMike Galbraith struct inode *inode = file->f_path.dentry->d_inode; 13495091faa4SMike Galbraith struct task_struct *p; 13505091faa4SMike Galbraith char buffer[PROC_NUMBUF]; 13510a8cb8e3SAlexey Dobriyan int nice; 13525091faa4SMike Galbraith int err; 13535091faa4SMike Galbraith 13545091faa4SMike Galbraith memset(buffer, 0, sizeof(buffer)); 13555091faa4SMike Galbraith if (count > sizeof(buffer) - 1) 13565091faa4SMike Galbraith count = sizeof(buffer) - 1; 13575091faa4SMike Galbraith if (copy_from_user(buffer, buf, count)) 13585091faa4SMike Galbraith return -EFAULT; 13595091faa4SMike Galbraith 13600a8cb8e3SAlexey Dobriyan err = kstrtoint(strstrip(buffer), 0, &nice); 13610a8cb8e3SAlexey Dobriyan if (err < 0) 13620a8cb8e3SAlexey Dobriyan return err; 13635091faa4SMike Galbraith 13645091faa4SMike Galbraith p = get_proc_task(inode); 13655091faa4SMike Galbraith if (!p) 13665091faa4SMike Galbraith return -ESRCH; 13675091faa4SMike Galbraith 13685091faa4SMike Galbraith err = nice; 13695091faa4SMike Galbraith err = proc_sched_autogroup_set_nice(p, &err); 13705091faa4SMike Galbraith if (err) 13715091faa4SMike Galbraith count = err; 13725091faa4SMike Galbraith 13735091faa4SMike Galbraith put_task_struct(p); 13745091faa4SMike Galbraith 13755091faa4SMike Galbraith return count; 13765091faa4SMike Galbraith } 13775091faa4SMike Galbraith 13785091faa4SMike Galbraith static int sched_autogroup_open(struct inode *inode, struct file *filp) 13795091faa4SMike Galbraith { 13805091faa4SMike Galbraith int ret; 13815091faa4SMike Galbraith 13825091faa4SMike Galbraith ret = single_open(filp, sched_autogroup_show, NULL); 13835091faa4SMike Galbraith if (!ret) { 13845091faa4SMike Galbraith struct seq_file *m = filp->private_data; 13855091faa4SMike Galbraith 13865091faa4SMike Galbraith m->private = inode; 13875091faa4SMike Galbraith } 13885091faa4SMike Galbraith return ret; 13895091faa4SMike Galbraith } 13905091faa4SMike Galbraith 13915091faa4SMike Galbraith static const struct file_operations proc_pid_sched_autogroup_operations = { 13925091faa4SMike Galbraith .open = sched_autogroup_open, 13935091faa4SMike Galbraith .read = seq_read, 13945091faa4SMike Galbraith .write = sched_autogroup_write, 13955091faa4SMike Galbraith .llseek = seq_lseek, 13965091faa4SMike Galbraith .release = single_release, 13975091faa4SMike Galbraith }; 13985091faa4SMike Galbraith 13995091faa4SMike Galbraith #endif /* CONFIG_SCHED_AUTOGROUP */ 14005091faa4SMike Galbraith 14014614a696Sjohn stultz static ssize_t comm_write(struct file *file, const char __user *buf, 14024614a696Sjohn stultz size_t count, loff_t *offset) 14034614a696Sjohn stultz { 14044614a696Sjohn stultz struct inode *inode = file->f_path.dentry->d_inode; 14054614a696Sjohn stultz struct task_struct *p; 14064614a696Sjohn stultz char buffer[TASK_COMM_LEN]; 14074614a696Sjohn stultz 14084614a696Sjohn stultz memset(buffer, 0, sizeof(buffer)); 14094614a696Sjohn stultz if (count > sizeof(buffer) - 1) 14104614a696Sjohn stultz count = sizeof(buffer) - 1; 14114614a696Sjohn stultz if (copy_from_user(buffer, buf, count)) 14124614a696Sjohn stultz return -EFAULT; 14134614a696Sjohn stultz 14144614a696Sjohn stultz p = get_proc_task(inode); 14154614a696Sjohn stultz if (!p) 14164614a696Sjohn stultz return -ESRCH; 14174614a696Sjohn stultz 14184614a696Sjohn stultz if (same_thread_group(current, p)) 14194614a696Sjohn stultz set_task_comm(p, buffer); 14204614a696Sjohn stultz else 14214614a696Sjohn stultz count = -EINVAL; 14224614a696Sjohn stultz 14234614a696Sjohn stultz put_task_struct(p); 14244614a696Sjohn stultz 14254614a696Sjohn stultz return count; 14264614a696Sjohn stultz } 14274614a696Sjohn stultz 14284614a696Sjohn stultz static int comm_show(struct seq_file *m, void *v) 14294614a696Sjohn stultz { 14304614a696Sjohn stultz struct inode *inode = m->private; 14314614a696Sjohn stultz struct task_struct *p; 14324614a696Sjohn stultz 14334614a696Sjohn stultz p = get_proc_task(inode); 14344614a696Sjohn stultz if (!p) 14354614a696Sjohn stultz return -ESRCH; 14364614a696Sjohn stultz 14374614a696Sjohn stultz task_lock(p); 14384614a696Sjohn stultz seq_printf(m, "%s\n", p->comm); 14394614a696Sjohn stultz task_unlock(p); 14404614a696Sjohn stultz 14414614a696Sjohn stultz put_task_struct(p); 14424614a696Sjohn stultz 14434614a696Sjohn stultz return 0; 14444614a696Sjohn stultz } 14454614a696Sjohn stultz 14464614a696Sjohn stultz static int comm_open(struct inode *inode, struct file *filp) 14474614a696Sjohn stultz { 1448c6a34058SJovi Zhang return single_open(filp, comm_show, inode); 14494614a696Sjohn stultz } 14504614a696Sjohn stultz 14514614a696Sjohn stultz static const struct file_operations proc_pid_set_comm_operations = { 14524614a696Sjohn stultz .open = comm_open, 14534614a696Sjohn stultz .read = seq_read, 14544614a696Sjohn stultz .write = comm_write, 14554614a696Sjohn stultz .llseek = seq_lseek, 14564614a696Sjohn stultz .release = single_release, 14574614a696Sjohn stultz }; 14584614a696Sjohn stultz 1459925d1c40SMatt Helsley static int proc_exe_link(struct inode *inode, struct path *exe_path) 1460925d1c40SMatt Helsley { 1461925d1c40SMatt Helsley struct task_struct *task; 1462925d1c40SMatt Helsley struct mm_struct *mm; 1463925d1c40SMatt Helsley struct file *exe_file; 1464925d1c40SMatt Helsley 1465925d1c40SMatt Helsley task = get_proc_task(inode); 1466925d1c40SMatt Helsley if (!task) 1467925d1c40SMatt Helsley return -ENOENT; 1468925d1c40SMatt Helsley mm = get_task_mm(task); 1469925d1c40SMatt Helsley put_task_struct(task); 1470925d1c40SMatt Helsley if (!mm) 1471925d1c40SMatt Helsley return -ENOENT; 1472925d1c40SMatt Helsley exe_file = get_mm_exe_file(mm); 1473925d1c40SMatt Helsley mmput(mm); 1474925d1c40SMatt Helsley if (exe_file) { 1475925d1c40SMatt Helsley *exe_path = exe_file->f_path; 1476925d1c40SMatt Helsley path_get(&exe_file->f_path); 1477925d1c40SMatt Helsley fput(exe_file); 1478925d1c40SMatt Helsley return 0; 1479925d1c40SMatt Helsley } else 1480925d1c40SMatt Helsley return -ENOENT; 1481925d1c40SMatt Helsley } 1482925d1c40SMatt Helsley 1483008b150aSAl Viro static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd) 14841da177e4SLinus Torvalds { 14851da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 14861da177e4SLinus Torvalds int error = -EACCES; 14871da177e4SLinus Torvalds 14881da177e4SLinus Torvalds /* We don't need a base pointer in the /proc filesystem */ 14891d957f9bSJan Blunck path_put(&nd->path); 14901da177e4SLinus Torvalds 1491778c1144SEric W. Biederman /* Are we allowed to snoop on the tasks file descriptors? */ 1492778c1144SEric W. Biederman if (!proc_fd_access_allowed(inode)) 14931da177e4SLinus Torvalds goto out; 14941da177e4SLinus Torvalds 14953dcd25f3SJan Blunck error = PROC_I(inode)->op.proc_get_link(inode, &nd->path); 14961da177e4SLinus Torvalds out: 1497008b150aSAl Viro return ERR_PTR(error); 14981da177e4SLinus Torvalds } 14991da177e4SLinus Torvalds 15003dcd25f3SJan Blunck static int do_proc_readlink(struct path *path, char __user *buffer, int buflen) 15011da177e4SLinus Torvalds { 1502e12ba74dSMel Gorman char *tmp = (char*)__get_free_page(GFP_TEMPORARY); 15033dcd25f3SJan Blunck char *pathname; 15041da177e4SLinus Torvalds int len; 15051da177e4SLinus Torvalds 15061da177e4SLinus Torvalds if (!tmp) 15071da177e4SLinus Torvalds return -ENOMEM; 15081da177e4SLinus Torvalds 15097b2a69baSEric W. Biederman pathname = d_path(path, tmp, PAGE_SIZE); 15103dcd25f3SJan Blunck len = PTR_ERR(pathname); 15113dcd25f3SJan Blunck if (IS_ERR(pathname)) 15121da177e4SLinus Torvalds goto out; 15133dcd25f3SJan Blunck len = tmp + PAGE_SIZE - 1 - pathname; 15141da177e4SLinus Torvalds 15151da177e4SLinus Torvalds if (len > buflen) 15161da177e4SLinus Torvalds len = buflen; 15173dcd25f3SJan Blunck if (copy_to_user(buffer, pathname, len)) 15181da177e4SLinus Torvalds len = -EFAULT; 15191da177e4SLinus Torvalds out: 15201da177e4SLinus Torvalds free_page((unsigned long)tmp); 15211da177e4SLinus Torvalds return len; 15221da177e4SLinus Torvalds } 15231da177e4SLinus Torvalds 15241da177e4SLinus Torvalds static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen) 15251da177e4SLinus Torvalds { 15261da177e4SLinus Torvalds int error = -EACCES; 15271da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 15283dcd25f3SJan Blunck struct path path; 15291da177e4SLinus Torvalds 1530778c1144SEric W. Biederman /* Are we allowed to snoop on the tasks file descriptors? */ 1531778c1144SEric W. Biederman if (!proc_fd_access_allowed(inode)) 15321da177e4SLinus Torvalds goto out; 15331da177e4SLinus Torvalds 15343dcd25f3SJan Blunck error = PROC_I(inode)->op.proc_get_link(inode, &path); 15351da177e4SLinus Torvalds if (error) 15361da177e4SLinus Torvalds goto out; 15371da177e4SLinus Torvalds 15383dcd25f3SJan Blunck error = do_proc_readlink(&path, buffer, buflen); 15393dcd25f3SJan Blunck path_put(&path); 15401da177e4SLinus Torvalds out: 15411da177e4SLinus Torvalds return error; 15421da177e4SLinus Torvalds } 15431da177e4SLinus Torvalds 1544c5ef1c42SArjan van de Ven static const struct inode_operations proc_pid_link_inode_operations = { 15451da177e4SLinus Torvalds .readlink = proc_pid_readlink, 15466d76fa58SLinus Torvalds .follow_link = proc_pid_follow_link, 15476d76fa58SLinus Torvalds .setattr = proc_setattr, 15481da177e4SLinus Torvalds }; 15491da177e4SLinus Torvalds 155028a6d671SEric W. Biederman 155128a6d671SEric W. Biederman /* building an inode */ 155228a6d671SEric W. Biederman 155328a6d671SEric W. Biederman static int task_dumpable(struct task_struct *task) 155428a6d671SEric W. Biederman { 155528a6d671SEric W. Biederman int dumpable = 0; 155628a6d671SEric W. Biederman struct mm_struct *mm; 155728a6d671SEric W. Biederman 155828a6d671SEric W. Biederman task_lock(task); 155928a6d671SEric W. Biederman mm = task->mm; 156028a6d671SEric W. Biederman if (mm) 15616c5d5238SKawai, Hidehiro dumpable = get_dumpable(mm); 156228a6d671SEric W. Biederman task_unlock(task); 156328a6d671SEric W. Biederman if(dumpable == 1) 156428a6d671SEric W. Biederman return 1; 156528a6d671SEric W. Biederman return 0; 156628a6d671SEric W. Biederman } 156728a6d671SEric W. Biederman 15686b4e306aSEric W. Biederman struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task) 156928a6d671SEric W. Biederman { 157028a6d671SEric W. Biederman struct inode * inode; 157128a6d671SEric W. Biederman struct proc_inode *ei; 1572c69e8d9cSDavid Howells const struct cred *cred; 157328a6d671SEric W. Biederman 157428a6d671SEric W. Biederman /* We need a new inode */ 157528a6d671SEric W. Biederman 157628a6d671SEric W. Biederman inode = new_inode(sb); 157728a6d671SEric W. Biederman if (!inode) 157828a6d671SEric W. Biederman goto out; 157928a6d671SEric W. Biederman 158028a6d671SEric W. Biederman /* Common stuff */ 158128a6d671SEric W. Biederman ei = PROC_I(inode); 158285fe4025SChristoph Hellwig inode->i_ino = get_next_ino(); 158328a6d671SEric W. Biederman inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; 158428a6d671SEric W. Biederman inode->i_op = &proc_def_inode_operations; 158528a6d671SEric W. Biederman 158628a6d671SEric W. Biederman /* 158728a6d671SEric W. Biederman * grab the reference to task. 158828a6d671SEric W. Biederman */ 15891a657f78SOleg Nesterov ei->pid = get_task_pid(task, PIDTYPE_PID); 159028a6d671SEric W. Biederman if (!ei->pid) 159128a6d671SEric W. Biederman goto out_unlock; 159228a6d671SEric W. Biederman 159328a6d671SEric W. Biederman if (task_dumpable(task)) { 1594c69e8d9cSDavid Howells rcu_read_lock(); 1595c69e8d9cSDavid Howells cred = __task_cred(task); 1596c69e8d9cSDavid Howells inode->i_uid = cred->euid; 1597c69e8d9cSDavid Howells inode->i_gid = cred->egid; 1598c69e8d9cSDavid Howells rcu_read_unlock(); 159928a6d671SEric W. Biederman } 160028a6d671SEric W. Biederman security_task_to_inode(task, inode); 160128a6d671SEric W. Biederman 160228a6d671SEric W. Biederman out: 160328a6d671SEric W. Biederman return inode; 160428a6d671SEric W. Biederman 160528a6d671SEric W. Biederman out_unlock: 160628a6d671SEric W. Biederman iput(inode); 160728a6d671SEric W. Biederman return NULL; 160828a6d671SEric W. Biederman } 160928a6d671SEric W. Biederman 16106b4e306aSEric W. Biederman int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) 161128a6d671SEric W. Biederman { 161228a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 161328a6d671SEric W. Biederman struct task_struct *task; 1614c69e8d9cSDavid Howells const struct cred *cred; 1615c69e8d9cSDavid Howells 161628a6d671SEric W. Biederman generic_fillattr(inode, stat); 161728a6d671SEric W. Biederman 161828a6d671SEric W. Biederman rcu_read_lock(); 161928a6d671SEric W. Biederman stat->uid = 0; 162028a6d671SEric W. Biederman stat->gid = 0; 162128a6d671SEric W. Biederman task = pid_task(proc_pid(inode), PIDTYPE_PID); 162228a6d671SEric W. Biederman if (task) { 162328a6d671SEric W. Biederman if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) || 162428a6d671SEric W. Biederman task_dumpable(task)) { 1625c69e8d9cSDavid Howells cred = __task_cred(task); 1626c69e8d9cSDavid Howells stat->uid = cred->euid; 1627c69e8d9cSDavid Howells stat->gid = cred->egid; 162828a6d671SEric W. Biederman } 162928a6d671SEric W. Biederman } 163028a6d671SEric W. Biederman rcu_read_unlock(); 163128a6d671SEric W. Biederman return 0; 163228a6d671SEric W. Biederman } 163328a6d671SEric W. Biederman 163428a6d671SEric W. Biederman /* dentry stuff */ 163528a6d671SEric W. Biederman 163628a6d671SEric W. Biederman /* 163728a6d671SEric W. Biederman * Exceptional case: normally we are not allowed to unhash a busy 163828a6d671SEric W. Biederman * directory. In this case, however, we can do it - no aliasing problems 163928a6d671SEric W. Biederman * due to the way we treat inodes. 164028a6d671SEric W. Biederman * 164128a6d671SEric W. Biederman * Rewrite the inode's ownerships here because the owning task may have 164228a6d671SEric W. Biederman * performed a setuid(), etc. 164328a6d671SEric W. Biederman * 164428a6d671SEric W. Biederman * Before the /proc/pid/status file was created the only way to read 164528a6d671SEric W. Biederman * the effective uid of a /process was to stat /proc/pid. Reading 164628a6d671SEric W. Biederman * /proc/pid/status is slow enough that procps and other packages 164728a6d671SEric W. Biederman * kept stating /proc/pid. To keep the rules in /proc simple I have 164828a6d671SEric W. Biederman * made this apply to all per process world readable and executable 164928a6d671SEric W. Biederman * directories. 165028a6d671SEric W. Biederman */ 16516b4e306aSEric W. Biederman int pid_revalidate(struct dentry *dentry, struct nameidata *nd) 165228a6d671SEric W. Biederman { 165334286d66SNick Piggin struct inode *inode; 165434286d66SNick Piggin struct task_struct *task; 1655c69e8d9cSDavid Howells const struct cred *cred; 1656c69e8d9cSDavid Howells 165734286d66SNick Piggin if (nd && nd->flags & LOOKUP_RCU) 165834286d66SNick Piggin return -ECHILD; 165934286d66SNick Piggin 166034286d66SNick Piggin inode = dentry->d_inode; 166134286d66SNick Piggin task = get_proc_task(inode); 166234286d66SNick Piggin 166328a6d671SEric W. Biederman if (task) { 166428a6d671SEric W. Biederman if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) || 166528a6d671SEric W. Biederman task_dumpable(task)) { 1666c69e8d9cSDavid Howells rcu_read_lock(); 1667c69e8d9cSDavid Howells cred = __task_cred(task); 1668c69e8d9cSDavid Howells inode->i_uid = cred->euid; 1669c69e8d9cSDavid Howells inode->i_gid = cred->egid; 1670c69e8d9cSDavid Howells rcu_read_unlock(); 167128a6d671SEric W. Biederman } else { 167228a6d671SEric W. Biederman inode->i_uid = 0; 167328a6d671SEric W. Biederman inode->i_gid = 0; 167428a6d671SEric W. Biederman } 167528a6d671SEric W. Biederman inode->i_mode &= ~(S_ISUID | S_ISGID); 167628a6d671SEric W. Biederman security_task_to_inode(task, inode); 167728a6d671SEric W. Biederman put_task_struct(task); 167828a6d671SEric W. Biederman return 1; 167928a6d671SEric W. Biederman } 168028a6d671SEric W. Biederman d_drop(dentry); 168128a6d671SEric W. Biederman return 0; 168228a6d671SEric W. Biederman } 168328a6d671SEric W. Biederman 1684fe15ce44SNick Piggin static int pid_delete_dentry(const struct dentry * dentry) 168528a6d671SEric W. Biederman { 168628a6d671SEric W. Biederman /* Is the task we represent dead? 168728a6d671SEric W. Biederman * If so, then don't put the dentry on the lru list, 168828a6d671SEric W. Biederman * kill it immediately. 168928a6d671SEric W. Biederman */ 169028a6d671SEric W. Biederman return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first; 169128a6d671SEric W. Biederman } 169228a6d671SEric W. Biederman 16936b4e306aSEric W. Biederman const struct dentry_operations pid_dentry_operations = 169428a6d671SEric W. Biederman { 169528a6d671SEric W. Biederman .d_revalidate = pid_revalidate, 169628a6d671SEric W. Biederman .d_delete = pid_delete_dentry, 169728a6d671SEric W. Biederman }; 169828a6d671SEric W. Biederman 169928a6d671SEric W. Biederman /* Lookups */ 170028a6d671SEric W. Biederman 17011c0d04c9SEric W. Biederman /* 17021c0d04c9SEric W. Biederman * Fill a directory entry. 17031c0d04c9SEric W. Biederman * 17041c0d04c9SEric W. Biederman * If possible create the dcache entry and derive our inode number and 17051c0d04c9SEric W. Biederman * file type from dcache entry. 17061c0d04c9SEric W. Biederman * 17071c0d04c9SEric W. Biederman * Since all of the proc inode numbers are dynamically generated, the inode 17081c0d04c9SEric W. Biederman * numbers do not exist until the inode is cache. This means creating the 17091c0d04c9SEric W. Biederman * the dcache entry in readdir is necessary to keep the inode numbers 17101c0d04c9SEric W. Biederman * reported by readdir in sync with the inode numbers reported 17111c0d04c9SEric W. Biederman * by stat. 17121c0d04c9SEric W. Biederman */ 17136b4e306aSEric W. Biederman int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir, 17146b4e306aSEric W. Biederman const char *name, int len, 1715c5141e6dSEric Dumazet instantiate_t instantiate, struct task_struct *task, const void *ptr) 171661a28784SEric W. Biederman { 17172fddfeefSJosef "Jeff" Sipek struct dentry *child, *dir = filp->f_path.dentry; 171861a28784SEric W. Biederman struct inode *inode; 171961a28784SEric W. Biederman struct qstr qname; 172061a28784SEric W. Biederman ino_t ino = 0; 172161a28784SEric W. Biederman unsigned type = DT_UNKNOWN; 172261a28784SEric W. Biederman 172361a28784SEric W. Biederman qname.name = name; 172461a28784SEric W. Biederman qname.len = len; 172561a28784SEric W. Biederman qname.hash = full_name_hash(name, len); 172661a28784SEric W. Biederman 172761a28784SEric W. Biederman child = d_lookup(dir, &qname); 172861a28784SEric W. Biederman if (!child) { 172961a28784SEric W. Biederman struct dentry *new; 173061a28784SEric W. Biederman new = d_alloc(dir, &qname); 173161a28784SEric W. Biederman if (new) { 173261a28784SEric W. Biederman child = instantiate(dir->d_inode, new, task, ptr); 173361a28784SEric W. Biederman if (child) 173461a28784SEric W. Biederman dput(new); 173561a28784SEric W. Biederman else 173661a28784SEric W. Biederman child = new; 173761a28784SEric W. Biederman } 173861a28784SEric W. Biederman } 173961a28784SEric W. Biederman if (!child || IS_ERR(child) || !child->d_inode) 174061a28784SEric W. Biederman goto end_instantiate; 174161a28784SEric W. Biederman inode = child->d_inode; 174261a28784SEric W. Biederman if (inode) { 174361a28784SEric W. Biederman ino = inode->i_ino; 174461a28784SEric W. Biederman type = inode->i_mode >> 12; 174561a28784SEric W. Biederman } 174661a28784SEric W. Biederman dput(child); 174761a28784SEric W. Biederman end_instantiate: 174861a28784SEric W. Biederman if (!ino) 174961a28784SEric W. Biederman ino = find_inode_number(dir, &qname); 175061a28784SEric W. Biederman if (!ino) 175161a28784SEric W. Biederman ino = 1; 175261a28784SEric W. Biederman return filldir(dirent, name, len, filp->f_pos, ino, type); 175361a28784SEric W. Biederman } 175461a28784SEric W. Biederman 175528a6d671SEric W. Biederman static unsigned name_to_int(struct dentry *dentry) 175628a6d671SEric W. Biederman { 175728a6d671SEric W. Biederman const char *name = dentry->d_name.name; 175828a6d671SEric W. Biederman int len = dentry->d_name.len; 175928a6d671SEric W. Biederman unsigned n = 0; 176028a6d671SEric W. Biederman 176128a6d671SEric W. Biederman if (len > 1 && *name == '0') 176228a6d671SEric W. Biederman goto out; 176328a6d671SEric W. Biederman while (len-- > 0) { 176428a6d671SEric W. Biederman unsigned c = *name++ - '0'; 176528a6d671SEric W. Biederman if (c > 9) 176628a6d671SEric W. Biederman goto out; 176728a6d671SEric W. Biederman if (n >= (~0U-9)/10) 176828a6d671SEric W. Biederman goto out; 176928a6d671SEric W. Biederman n *= 10; 177028a6d671SEric W. Biederman n += c; 177128a6d671SEric W. Biederman } 177228a6d671SEric W. Biederman return n; 177328a6d671SEric W. Biederman out: 177428a6d671SEric W. Biederman return ~0U; 177528a6d671SEric W. Biederman } 177628a6d671SEric W. Biederman 177727932742SMiklos Szeredi #define PROC_FDINFO_MAX 64 177827932742SMiklos Szeredi 17793dcd25f3SJan Blunck static int proc_fd_info(struct inode *inode, struct path *path, char *info) 178028a6d671SEric W. Biederman { 17815e442a49SLinus Torvalds struct task_struct *task = get_proc_task(inode); 17825e442a49SLinus Torvalds struct files_struct *files = NULL; 178328a6d671SEric W. Biederman struct file *file; 178428a6d671SEric W. Biederman int fd = proc_fd(inode); 178528a6d671SEric W. Biederman 17865e442a49SLinus Torvalds if (task) { 178728a6d671SEric W. Biederman files = get_files_struct(task); 17885e442a49SLinus Torvalds put_task_struct(task); 17895e442a49SLinus Torvalds } 17905e442a49SLinus Torvalds if (files) { 179128a6d671SEric W. Biederman /* 179228a6d671SEric W. Biederman * We are not taking a ref to the file structure, so we must 179328a6d671SEric W. Biederman * hold ->file_lock. 179428a6d671SEric W. Biederman */ 179528a6d671SEric W. Biederman spin_lock(&files->file_lock); 179628a6d671SEric W. Biederman file = fcheck_files(files, fd); 179728a6d671SEric W. Biederman if (file) { 17981117f72eSLinus Torvalds unsigned int f_flags; 17991117f72eSLinus Torvalds struct fdtable *fdt; 18001117f72eSLinus Torvalds 18011117f72eSLinus Torvalds fdt = files_fdtable(files); 18021117f72eSLinus Torvalds f_flags = file->f_flags & ~O_CLOEXEC; 18031117f72eSLinus Torvalds if (FD_ISSET(fd, fdt->close_on_exec)) 18041117f72eSLinus Torvalds f_flags |= O_CLOEXEC; 18051117f72eSLinus Torvalds 18063dcd25f3SJan Blunck if (path) { 18073dcd25f3SJan Blunck *path = file->f_path; 18083dcd25f3SJan Blunck path_get(&file->f_path); 18093dcd25f3SJan Blunck } 181027932742SMiklos Szeredi if (info) 181127932742SMiklos Szeredi snprintf(info, PROC_FDINFO_MAX, 181227932742SMiklos Szeredi "pos:\t%lli\n" 181327932742SMiklos Szeredi "flags:\t0%o\n", 181427932742SMiklos Szeredi (long long) file->f_pos, 18151117f72eSLinus Torvalds f_flags); 181628a6d671SEric W. Biederman spin_unlock(&files->file_lock); 181728a6d671SEric W. Biederman put_files_struct(files); 18185e442a49SLinus Torvalds return 0; 18195e442a49SLinus Torvalds } 18205e442a49SLinus Torvalds spin_unlock(&files->file_lock); 18215e442a49SLinus Torvalds put_files_struct(files); 18225e442a49SLinus Torvalds } 18235e442a49SLinus Torvalds return -ENOENT; 182428a6d671SEric W. Biederman } 182528a6d671SEric W. Biederman 18263dcd25f3SJan Blunck static int proc_fd_link(struct inode *inode, struct path *path) 182727932742SMiklos Szeredi { 18283dcd25f3SJan Blunck return proc_fd_info(inode, path, NULL); 182927932742SMiklos Szeredi } 183027932742SMiklos Szeredi 183128a6d671SEric W. Biederman static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd) 183228a6d671SEric W. Biederman { 183334286d66SNick Piggin struct inode *inode; 183434286d66SNick Piggin struct task_struct *task; 183534286d66SNick Piggin int fd; 183628a6d671SEric W. Biederman struct files_struct *files; 1837c69e8d9cSDavid Howells const struct cred *cred; 183828a6d671SEric W. Biederman 183934286d66SNick Piggin if (nd && nd->flags & LOOKUP_RCU) 184034286d66SNick Piggin return -ECHILD; 184134286d66SNick Piggin 184234286d66SNick Piggin inode = dentry->d_inode; 184334286d66SNick Piggin task = get_proc_task(inode); 184434286d66SNick Piggin fd = proc_fd(inode); 184534286d66SNick Piggin 184628a6d671SEric W. Biederman if (task) { 184728a6d671SEric W. Biederman files = get_files_struct(task); 184828a6d671SEric W. Biederman if (files) { 184928a6d671SEric W. Biederman rcu_read_lock(); 185028a6d671SEric W. Biederman if (fcheck_files(files, fd)) { 185128a6d671SEric W. Biederman rcu_read_unlock(); 185228a6d671SEric W. Biederman put_files_struct(files); 185328a6d671SEric W. Biederman if (task_dumpable(task)) { 1854c69e8d9cSDavid Howells rcu_read_lock(); 1855c69e8d9cSDavid Howells cred = __task_cred(task); 1856c69e8d9cSDavid Howells inode->i_uid = cred->euid; 1857c69e8d9cSDavid Howells inode->i_gid = cred->egid; 1858c69e8d9cSDavid Howells rcu_read_unlock(); 185928a6d671SEric W. Biederman } else { 186028a6d671SEric W. Biederman inode->i_uid = 0; 186128a6d671SEric W. Biederman inode->i_gid = 0; 186228a6d671SEric W. Biederman } 186328a6d671SEric W. Biederman inode->i_mode &= ~(S_ISUID | S_ISGID); 186428a6d671SEric W. Biederman security_task_to_inode(task, inode); 186528a6d671SEric W. Biederman put_task_struct(task); 186628a6d671SEric W. Biederman return 1; 186728a6d671SEric W. Biederman } 186828a6d671SEric W. Biederman rcu_read_unlock(); 186928a6d671SEric W. Biederman put_files_struct(files); 187028a6d671SEric W. Biederman } 187128a6d671SEric W. Biederman put_task_struct(task); 187228a6d671SEric W. Biederman } 187328a6d671SEric W. Biederman d_drop(dentry); 187428a6d671SEric W. Biederman return 0; 187528a6d671SEric W. Biederman } 187628a6d671SEric W. Biederman 1877d72f71ebSAl Viro static const struct dentry_operations tid_fd_dentry_operations = 187828a6d671SEric W. Biederman { 187928a6d671SEric W. Biederman .d_revalidate = tid_fd_revalidate, 188028a6d671SEric W. Biederman .d_delete = pid_delete_dentry, 188128a6d671SEric W. Biederman }; 188228a6d671SEric W. Biederman 1883444ceed8SEric W. Biederman static struct dentry *proc_fd_instantiate(struct inode *dir, 1884c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 188528a6d671SEric W. Biederman { 1886c5141e6dSEric Dumazet unsigned fd = *(const unsigned *)ptr; 188728a6d671SEric W. Biederman struct file *file; 188828a6d671SEric W. Biederman struct files_struct *files; 188928a6d671SEric W. Biederman struct inode *inode; 189028a6d671SEric W. Biederman struct proc_inode *ei; 1891444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-ENOENT); 189228a6d671SEric W. Biederman 189361a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 189428a6d671SEric W. Biederman if (!inode) 189528a6d671SEric W. Biederman goto out; 189628a6d671SEric W. Biederman ei = PROC_I(inode); 189728a6d671SEric W. Biederman ei->fd = fd; 189828a6d671SEric W. Biederman files = get_files_struct(task); 189928a6d671SEric W. Biederman if (!files) 1900444ceed8SEric W. Biederman goto out_iput; 190128a6d671SEric W. Biederman inode->i_mode = S_IFLNK; 190228a6d671SEric W. Biederman 190328a6d671SEric W. Biederman /* 190428a6d671SEric W. Biederman * We are not taking a ref to the file structure, so we must 190528a6d671SEric W. Biederman * hold ->file_lock. 190628a6d671SEric W. Biederman */ 190728a6d671SEric W. Biederman spin_lock(&files->file_lock); 190828a6d671SEric W. Biederman file = fcheck_files(files, fd); 190928a6d671SEric W. Biederman if (!file) 1910444ceed8SEric W. Biederman goto out_unlock; 1911aeb5d727SAl Viro if (file->f_mode & FMODE_READ) 191228a6d671SEric W. Biederman inode->i_mode |= S_IRUSR | S_IXUSR; 1913aeb5d727SAl Viro if (file->f_mode & FMODE_WRITE) 191428a6d671SEric W. Biederman inode->i_mode |= S_IWUSR | S_IXUSR; 191528a6d671SEric W. Biederman spin_unlock(&files->file_lock); 191628a6d671SEric W. Biederman put_files_struct(files); 1917444ceed8SEric W. Biederman 19185e442a49SLinus Torvalds inode->i_op = &proc_pid_link_inode_operations; 191928a6d671SEric W. Biederman inode->i_size = 64; 192028a6d671SEric W. Biederman ei->op.proc_get_link = proc_fd_link; 1921fb045adbSNick Piggin d_set_d_op(dentry, &tid_fd_dentry_operations); 192228a6d671SEric W. Biederman d_add(dentry, inode); 192328a6d671SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 192428a6d671SEric W. Biederman if (tid_fd_revalidate(dentry, NULL)) 1925444ceed8SEric W. Biederman error = NULL; 1926444ceed8SEric W. Biederman 1927444ceed8SEric W. Biederman out: 1928444ceed8SEric W. Biederman return error; 1929444ceed8SEric W. Biederman out_unlock: 1930444ceed8SEric W. Biederman spin_unlock(&files->file_lock); 1931444ceed8SEric W. Biederman put_files_struct(files); 1932444ceed8SEric W. Biederman out_iput: 1933444ceed8SEric W. Biederman iput(inode); 1934444ceed8SEric W. Biederman goto out; 1935444ceed8SEric W. Biederman } 1936444ceed8SEric W. Biederman 193727932742SMiklos Szeredi static struct dentry *proc_lookupfd_common(struct inode *dir, 193827932742SMiklos Szeredi struct dentry *dentry, 193927932742SMiklos Szeredi instantiate_t instantiate) 1940444ceed8SEric W. Biederman { 1941444ceed8SEric W. Biederman struct task_struct *task = get_proc_task(dir); 1942444ceed8SEric W. Biederman unsigned fd = name_to_int(dentry); 1943444ceed8SEric W. Biederman struct dentry *result = ERR_PTR(-ENOENT); 1944444ceed8SEric W. Biederman 1945444ceed8SEric W. Biederman if (!task) 1946444ceed8SEric W. Biederman goto out_no_task; 1947444ceed8SEric W. Biederman if (fd == ~0U) 1948444ceed8SEric W. Biederman goto out; 1949444ceed8SEric W. Biederman 195027932742SMiklos Szeredi result = instantiate(dir, dentry, task, &fd); 195128a6d671SEric W. Biederman out: 195228a6d671SEric W. Biederman put_task_struct(task); 195328a6d671SEric W. Biederman out_no_task: 195428a6d671SEric W. Biederman return result; 195528a6d671SEric W. Biederman } 195628a6d671SEric W. Biederman 195727932742SMiklos Szeredi static int proc_readfd_common(struct file * filp, void * dirent, 195827932742SMiklos Szeredi filldir_t filldir, instantiate_t instantiate) 19591da177e4SLinus Torvalds { 19602fddfeefSJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 19615634708bSEric W. Biederman struct inode *inode = dentry->d_inode; 196299f89551SEric W. Biederman struct task_struct *p = get_proc_task(inode); 1963457c2510SPavel Emelyanov unsigned int fd, ino; 19641da177e4SLinus Torvalds int retval; 19651da177e4SLinus Torvalds struct files_struct * files; 19661da177e4SLinus Torvalds 19671da177e4SLinus Torvalds retval = -ENOENT; 196899f89551SEric W. Biederman if (!p) 196999f89551SEric W. Biederman goto out_no_task; 19701da177e4SLinus Torvalds retval = 0; 19711da177e4SLinus Torvalds 19721da177e4SLinus Torvalds fd = filp->f_pos; 19731da177e4SLinus Torvalds switch (fd) { 19741da177e4SLinus Torvalds case 0: 19751da177e4SLinus Torvalds if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0) 19765e442a49SLinus Torvalds goto out; 19771da177e4SLinus Torvalds filp->f_pos++; 19781da177e4SLinus Torvalds case 1: 19795634708bSEric W. Biederman ino = parent_ino(dentry); 19801da177e4SLinus Torvalds if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0) 19815e442a49SLinus Torvalds goto out; 19821da177e4SLinus Torvalds filp->f_pos++; 19831da177e4SLinus Torvalds default: 19841da177e4SLinus Torvalds files = get_files_struct(p); 19851da177e4SLinus Torvalds if (!files) 19865e442a49SLinus Torvalds goto out; 1987b835996fSDipankar Sarma rcu_read_lock(); 19881da177e4SLinus Torvalds for (fd = filp->f_pos-2; 19899b4f526cSAl Viro fd < files_fdtable(files)->max_fds; 19901da177e4SLinus Torvalds fd++, filp->f_pos++) { 199127932742SMiklos Szeredi char name[PROC_NUMBUF]; 199227932742SMiklos Szeredi int len; 19931da177e4SLinus Torvalds 19941da177e4SLinus Torvalds if (!fcheck_files(files, fd)) 19951da177e4SLinus Torvalds continue; 1996b835996fSDipankar Sarma rcu_read_unlock(); 19971da177e4SLinus Torvalds 199827932742SMiklos Szeredi len = snprintf(name, sizeof(name), "%d", fd); 199927932742SMiklos Szeredi if (proc_fill_cache(filp, dirent, filldir, 200027932742SMiklos Szeredi name, len, instantiate, 200127932742SMiklos Szeredi p, &fd) < 0) { 2002b835996fSDipankar Sarma rcu_read_lock(); 20031da177e4SLinus Torvalds break; 20041da177e4SLinus Torvalds } 2005b835996fSDipankar Sarma rcu_read_lock(); 20061da177e4SLinus Torvalds } 2007b835996fSDipankar Sarma rcu_read_unlock(); 20081da177e4SLinus Torvalds put_files_struct(files); 20091da177e4SLinus Torvalds } 20101da177e4SLinus Torvalds out: 201199f89551SEric W. Biederman put_task_struct(p); 201299f89551SEric W. Biederman out_no_task: 20131da177e4SLinus Torvalds return retval; 20141da177e4SLinus Torvalds } 20151da177e4SLinus Torvalds 201627932742SMiklos Szeredi static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry, 201727932742SMiklos Szeredi struct nameidata *nd) 201827932742SMiklos Szeredi { 201927932742SMiklos Szeredi return proc_lookupfd_common(dir, dentry, proc_fd_instantiate); 202027932742SMiklos Szeredi } 202127932742SMiklos Szeredi 202227932742SMiklos Szeredi static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir) 202327932742SMiklos Szeredi { 202427932742SMiklos Szeredi return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate); 202527932742SMiklos Szeredi } 202627932742SMiklos Szeredi 202727932742SMiklos Szeredi static ssize_t proc_fdinfo_read(struct file *file, char __user *buf, 202827932742SMiklos Szeredi size_t len, loff_t *ppos) 202927932742SMiklos Szeredi { 203027932742SMiklos Szeredi char tmp[PROC_FDINFO_MAX]; 20313dcd25f3SJan Blunck int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp); 203227932742SMiklos Szeredi if (!err) 203327932742SMiklos Szeredi err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp)); 203427932742SMiklos Szeredi return err; 203527932742SMiklos Szeredi } 203627932742SMiklos Szeredi 203727932742SMiklos Szeredi static const struct file_operations proc_fdinfo_file_operations = { 203827932742SMiklos Szeredi .open = nonseekable_open, 203927932742SMiklos Szeredi .read = proc_fdinfo_read, 20406038f373SArnd Bergmann .llseek = no_llseek, 204127932742SMiklos Szeredi }; 204227932742SMiklos Szeredi 204300977a59SArjan van de Ven static const struct file_operations proc_fd_operations = { 20441da177e4SLinus Torvalds .read = generic_read_dir, 20451da177e4SLinus Torvalds .readdir = proc_readfd, 20466038f373SArnd Bergmann .llseek = default_llseek, 20471da177e4SLinus Torvalds }; 20481da177e4SLinus Torvalds 20491da177e4SLinus Torvalds /* 20508948e11fSAlexey Dobriyan * /proc/pid/fd needs a special permission handler so that a process can still 20518948e11fSAlexey Dobriyan * access /proc/self/fd after it has executed a setuid(). 20528948e11fSAlexey Dobriyan */ 205310556cb2SAl Viro static int proc_fd_permission(struct inode *inode, int mask) 20548948e11fSAlexey Dobriyan { 20552830ba7fSAl Viro int rv = generic_permission(inode, mask); 20568948e11fSAlexey Dobriyan if (rv == 0) 20578948e11fSAlexey Dobriyan return 0; 20588948e11fSAlexey Dobriyan if (task_pid(current) == proc_pid(inode)) 20598948e11fSAlexey Dobriyan rv = 0; 20608948e11fSAlexey Dobriyan return rv; 20618948e11fSAlexey Dobriyan } 20628948e11fSAlexey Dobriyan 20638948e11fSAlexey Dobriyan /* 20641da177e4SLinus Torvalds * proc directories can do almost nothing.. 20651da177e4SLinus Torvalds */ 2066c5ef1c42SArjan van de Ven static const struct inode_operations proc_fd_inode_operations = { 20671da177e4SLinus Torvalds .lookup = proc_lookupfd, 20688948e11fSAlexey Dobriyan .permission = proc_fd_permission, 20696d76fa58SLinus Torvalds .setattr = proc_setattr, 20701da177e4SLinus Torvalds }; 20711da177e4SLinus Torvalds 207227932742SMiklos Szeredi static struct dentry *proc_fdinfo_instantiate(struct inode *dir, 207327932742SMiklos Szeredi struct dentry *dentry, struct task_struct *task, const void *ptr) 207427932742SMiklos Szeredi { 207527932742SMiklos Szeredi unsigned fd = *(unsigned *)ptr; 207627932742SMiklos Szeredi struct inode *inode; 207727932742SMiklos Szeredi struct proc_inode *ei; 207827932742SMiklos Szeredi struct dentry *error = ERR_PTR(-ENOENT); 207927932742SMiklos Szeredi 208027932742SMiklos Szeredi inode = proc_pid_make_inode(dir->i_sb, task); 208127932742SMiklos Szeredi if (!inode) 208227932742SMiklos Szeredi goto out; 208327932742SMiklos Szeredi ei = PROC_I(inode); 208427932742SMiklos Szeredi ei->fd = fd; 208527932742SMiklos Szeredi inode->i_mode = S_IFREG | S_IRUSR; 208627932742SMiklos Szeredi inode->i_fop = &proc_fdinfo_file_operations; 2087fb045adbSNick Piggin d_set_d_op(dentry, &tid_fd_dentry_operations); 208827932742SMiklos Szeredi d_add(dentry, inode); 208927932742SMiklos Szeredi /* Close the race of the process dying before we return the dentry */ 209027932742SMiklos Szeredi if (tid_fd_revalidate(dentry, NULL)) 209127932742SMiklos Szeredi error = NULL; 209227932742SMiklos Szeredi 209327932742SMiklos Szeredi out: 209427932742SMiklos Szeredi return error; 209527932742SMiklos Szeredi } 209627932742SMiklos Szeredi 209727932742SMiklos Szeredi static struct dentry *proc_lookupfdinfo(struct inode *dir, 209827932742SMiklos Szeredi struct dentry *dentry, 209927932742SMiklos Szeredi struct nameidata *nd) 210027932742SMiklos Szeredi { 210127932742SMiklos Szeredi return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate); 210227932742SMiklos Szeredi } 210327932742SMiklos Szeredi 210427932742SMiklos Szeredi static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir) 210527932742SMiklos Szeredi { 210627932742SMiklos Szeredi return proc_readfd_common(filp, dirent, filldir, 210727932742SMiklos Szeredi proc_fdinfo_instantiate); 210827932742SMiklos Szeredi } 210927932742SMiklos Szeredi 211027932742SMiklos Szeredi static const struct file_operations proc_fdinfo_operations = { 211127932742SMiklos Szeredi .read = generic_read_dir, 211227932742SMiklos Szeredi .readdir = proc_readfdinfo, 21136038f373SArnd Bergmann .llseek = default_llseek, 211427932742SMiklos Szeredi }; 211527932742SMiklos Szeredi 211627932742SMiklos Szeredi /* 211727932742SMiklos Szeredi * proc directories can do almost nothing.. 211827932742SMiklos Szeredi */ 211927932742SMiklos Szeredi static const struct inode_operations proc_fdinfo_inode_operations = { 212027932742SMiklos Szeredi .lookup = proc_lookupfdinfo, 212127932742SMiklos Szeredi .setattr = proc_setattr, 212227932742SMiklos Szeredi }; 212327932742SMiklos Szeredi 212427932742SMiklos Szeredi 2125444ceed8SEric W. Biederman static struct dentry *proc_pident_instantiate(struct inode *dir, 2126c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 2127444ceed8SEric W. Biederman { 2128c5141e6dSEric Dumazet const struct pid_entry *p = ptr; 2129444ceed8SEric W. Biederman struct inode *inode; 2130444ceed8SEric W. Biederman struct proc_inode *ei; 2131bd6daba9SKOSAKI Motohiro struct dentry *error = ERR_PTR(-ENOENT); 2132444ceed8SEric W. Biederman 213361a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 2134444ceed8SEric W. Biederman if (!inode) 2135444ceed8SEric W. Biederman goto out; 2136444ceed8SEric W. Biederman 2137444ceed8SEric W. Biederman ei = PROC_I(inode); 2138444ceed8SEric W. Biederman inode->i_mode = p->mode; 2139444ceed8SEric W. Biederman if (S_ISDIR(inode->i_mode)) 2140bfe86848SMiklos Szeredi set_nlink(inode, 2); /* Use getattr to fix if necessary */ 2141444ceed8SEric W. Biederman if (p->iop) 2142444ceed8SEric W. Biederman inode->i_op = p->iop; 2143444ceed8SEric W. Biederman if (p->fop) 2144444ceed8SEric W. Biederman inode->i_fop = p->fop; 2145444ceed8SEric W. Biederman ei->op = p->op; 2146fb045adbSNick Piggin d_set_d_op(dentry, &pid_dentry_operations); 2147444ceed8SEric W. Biederman d_add(dentry, inode); 2148444ceed8SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 2149444ceed8SEric W. Biederman if (pid_revalidate(dentry, NULL)) 2150444ceed8SEric W. Biederman error = NULL; 2151444ceed8SEric W. Biederman out: 2152444ceed8SEric W. Biederman return error; 2153444ceed8SEric W. Biederman } 2154444ceed8SEric W. Biederman 21551da177e4SLinus Torvalds static struct dentry *proc_pident_lookup(struct inode *dir, 21561da177e4SLinus Torvalds struct dentry *dentry, 2157c5141e6dSEric Dumazet const struct pid_entry *ents, 21587bcd6b0eSEric W. Biederman unsigned int nents) 21591da177e4SLinus Torvalds { 2160cd6a3ce9SEric W. Biederman struct dentry *error; 216199f89551SEric W. Biederman struct task_struct *task = get_proc_task(dir); 2162c5141e6dSEric Dumazet const struct pid_entry *p, *last; 21631da177e4SLinus Torvalds 2164cd6a3ce9SEric W. Biederman error = ERR_PTR(-ENOENT); 21651da177e4SLinus Torvalds 216699f89551SEric W. Biederman if (!task) 216799f89551SEric W. Biederman goto out_no_task; 21681da177e4SLinus Torvalds 216920cdc894SEric W. Biederman /* 217020cdc894SEric W. Biederman * Yes, it does not scale. And it should not. Don't add 217120cdc894SEric W. Biederman * new entries into /proc/<tgid>/ without very good reasons. 217220cdc894SEric W. Biederman */ 21737bcd6b0eSEric W. Biederman last = &ents[nents - 1]; 21747bcd6b0eSEric W. Biederman for (p = ents; p <= last; p++) { 21751da177e4SLinus Torvalds if (p->len != dentry->d_name.len) 21761da177e4SLinus Torvalds continue; 21771da177e4SLinus Torvalds if (!memcmp(dentry->d_name.name, p->name, p->len)) 21781da177e4SLinus Torvalds break; 21791da177e4SLinus Torvalds } 21807bcd6b0eSEric W. Biederman if (p > last) 21811da177e4SLinus Torvalds goto out; 21821da177e4SLinus Torvalds 2183444ceed8SEric W. Biederman error = proc_pident_instantiate(dir, dentry, task, p); 21841da177e4SLinus Torvalds out: 218599f89551SEric W. Biederman put_task_struct(task); 218699f89551SEric W. Biederman out_no_task: 2187cd6a3ce9SEric W. Biederman return error; 21881da177e4SLinus Torvalds } 21891da177e4SLinus Torvalds 2190c5141e6dSEric Dumazet static int proc_pident_fill_cache(struct file *filp, void *dirent, 2191c5141e6dSEric Dumazet filldir_t filldir, struct task_struct *task, const struct pid_entry *p) 219261a28784SEric W. Biederman { 219361a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, p->name, p->len, 219461a28784SEric W. Biederman proc_pident_instantiate, task, p); 219561a28784SEric W. Biederman } 219661a28784SEric W. Biederman 219728a6d671SEric W. Biederman static int proc_pident_readdir(struct file *filp, 219828a6d671SEric W. Biederman void *dirent, filldir_t filldir, 2199c5141e6dSEric Dumazet const struct pid_entry *ents, unsigned int nents) 220028a6d671SEric W. Biederman { 220128a6d671SEric W. Biederman int i; 22022fddfeefSJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 220328a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 220428a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 2205c5141e6dSEric Dumazet const struct pid_entry *p, *last; 220628a6d671SEric W. Biederman ino_t ino; 220728a6d671SEric W. Biederman int ret; 220828a6d671SEric W. Biederman 220928a6d671SEric W. Biederman ret = -ENOENT; 221028a6d671SEric W. Biederman if (!task) 221161a28784SEric W. Biederman goto out_no_task; 221228a6d671SEric W. Biederman 221328a6d671SEric W. Biederman ret = 0; 221428a6d671SEric W. Biederman i = filp->f_pos; 221528a6d671SEric W. Biederman switch (i) { 221628a6d671SEric W. Biederman case 0: 221728a6d671SEric W. Biederman ino = inode->i_ino; 221828a6d671SEric W. Biederman if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0) 221928a6d671SEric W. Biederman goto out; 222028a6d671SEric W. Biederman i++; 222128a6d671SEric W. Biederman filp->f_pos++; 222228a6d671SEric W. Biederman /* fall through */ 222328a6d671SEric W. Biederman case 1: 222428a6d671SEric W. Biederman ino = parent_ino(dentry); 222528a6d671SEric W. Biederman if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0) 222628a6d671SEric W. Biederman goto out; 222728a6d671SEric W. Biederman i++; 222828a6d671SEric W. Biederman filp->f_pos++; 222928a6d671SEric W. Biederman /* fall through */ 223028a6d671SEric W. Biederman default: 223128a6d671SEric W. Biederman i -= 2; 223228a6d671SEric W. Biederman if (i >= nents) { 223328a6d671SEric W. Biederman ret = 1; 223428a6d671SEric W. Biederman goto out; 223528a6d671SEric W. Biederman } 223628a6d671SEric W. Biederman p = ents + i; 22377bcd6b0eSEric W. Biederman last = &ents[nents - 1]; 22387bcd6b0eSEric W. Biederman while (p <= last) { 223961a28784SEric W. Biederman if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0) 224028a6d671SEric W. Biederman goto out; 224128a6d671SEric W. Biederman filp->f_pos++; 224228a6d671SEric W. Biederman p++; 224328a6d671SEric W. Biederman } 22441da177e4SLinus Torvalds } 22451da177e4SLinus Torvalds 224628a6d671SEric W. Biederman ret = 1; 224728a6d671SEric W. Biederman out: 224861a28784SEric W. Biederman put_task_struct(task); 224961a28784SEric W. Biederman out_no_task: 225028a6d671SEric W. Biederman return ret; 22511da177e4SLinus Torvalds } 22521da177e4SLinus Torvalds 22531da177e4SLinus Torvalds #ifdef CONFIG_SECURITY 225428a6d671SEric W. Biederman static ssize_t proc_pid_attr_read(struct file * file, char __user * buf, 225528a6d671SEric W. Biederman size_t count, loff_t *ppos) 225628a6d671SEric W. Biederman { 22572fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 225804ff9708SAl Viro char *p = NULL; 225928a6d671SEric W. Biederman ssize_t length; 226028a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 226128a6d671SEric W. Biederman 226228a6d671SEric W. Biederman if (!task) 226304ff9708SAl Viro return -ESRCH; 226428a6d671SEric W. Biederman 226528a6d671SEric W. Biederman length = security_getprocattr(task, 22662fddfeefSJosef "Jeff" Sipek (char*)file->f_path.dentry->d_name.name, 226704ff9708SAl Viro &p); 226828a6d671SEric W. Biederman put_task_struct(task); 226904ff9708SAl Viro if (length > 0) 227004ff9708SAl Viro length = simple_read_from_buffer(buf, count, ppos, p, length); 227104ff9708SAl Viro kfree(p); 227228a6d671SEric W. Biederman return length; 227328a6d671SEric W. Biederman } 227428a6d671SEric W. Biederman 227528a6d671SEric W. Biederman static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf, 227628a6d671SEric W. Biederman size_t count, loff_t *ppos) 227728a6d671SEric W. Biederman { 22782fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 227928a6d671SEric W. Biederman char *page; 228028a6d671SEric W. Biederman ssize_t length; 228128a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 228228a6d671SEric W. Biederman 228328a6d671SEric W. Biederman length = -ESRCH; 228428a6d671SEric W. Biederman if (!task) 228528a6d671SEric W. Biederman goto out_no_task; 228628a6d671SEric W. Biederman if (count > PAGE_SIZE) 228728a6d671SEric W. Biederman count = PAGE_SIZE; 228828a6d671SEric W. Biederman 228928a6d671SEric W. Biederman /* No partial writes. */ 229028a6d671SEric W. Biederman length = -EINVAL; 229128a6d671SEric W. Biederman if (*ppos != 0) 229228a6d671SEric W. Biederman goto out; 229328a6d671SEric W. Biederman 229428a6d671SEric W. Biederman length = -ENOMEM; 2295e12ba74dSMel Gorman page = (char*)__get_free_page(GFP_TEMPORARY); 229628a6d671SEric W. Biederman if (!page) 229728a6d671SEric W. Biederman goto out; 229828a6d671SEric W. Biederman 229928a6d671SEric W. Biederman length = -EFAULT; 230028a6d671SEric W. Biederman if (copy_from_user(page, buf, count)) 230128a6d671SEric W. Biederman goto out_free; 230228a6d671SEric W. Biederman 2303107db7c7SDavid Howells /* Guard against adverse ptrace interaction */ 23049b1bf12dSKOSAKI Motohiro length = mutex_lock_interruptible(&task->signal->cred_guard_mutex); 2305107db7c7SDavid Howells if (length < 0) 2306107db7c7SDavid Howells goto out_free; 2307107db7c7SDavid Howells 230828a6d671SEric W. Biederman length = security_setprocattr(task, 23092fddfeefSJosef "Jeff" Sipek (char*)file->f_path.dentry->d_name.name, 231028a6d671SEric W. Biederman (void*)page, count); 23119b1bf12dSKOSAKI Motohiro mutex_unlock(&task->signal->cred_guard_mutex); 231228a6d671SEric W. Biederman out_free: 231328a6d671SEric W. Biederman free_page((unsigned long) page); 231428a6d671SEric W. Biederman out: 231528a6d671SEric W. Biederman put_task_struct(task); 231628a6d671SEric W. Biederman out_no_task: 231728a6d671SEric W. Biederman return length; 231828a6d671SEric W. Biederman } 231928a6d671SEric W. Biederman 232000977a59SArjan van de Ven static const struct file_operations proc_pid_attr_operations = { 232128a6d671SEric W. Biederman .read = proc_pid_attr_read, 232228a6d671SEric W. Biederman .write = proc_pid_attr_write, 232387df8424SArnd Bergmann .llseek = generic_file_llseek, 232428a6d671SEric W. Biederman }; 232528a6d671SEric W. Biederman 2326c5141e6dSEric Dumazet static const struct pid_entry attr_dir_stuff[] = { 2327631f9c18SAlexey Dobriyan REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations), 2328631f9c18SAlexey Dobriyan REG("prev", S_IRUGO, proc_pid_attr_operations), 2329631f9c18SAlexey Dobriyan REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations), 2330631f9c18SAlexey Dobriyan REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations), 2331631f9c18SAlexey Dobriyan REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations), 2332631f9c18SAlexey Dobriyan REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations), 233328a6d671SEric W. Biederman }; 233428a6d671SEric W. Biederman 233572d9dcfcSEric W. Biederman static int proc_attr_dir_readdir(struct file * filp, 23361da177e4SLinus Torvalds void * dirent, filldir_t filldir) 23371da177e4SLinus Torvalds { 23381da177e4SLinus Torvalds return proc_pident_readdir(filp,dirent,filldir, 233972d9dcfcSEric W. Biederman attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff)); 23401da177e4SLinus Torvalds } 23411da177e4SLinus Torvalds 234200977a59SArjan van de Ven static const struct file_operations proc_attr_dir_operations = { 23431da177e4SLinus Torvalds .read = generic_read_dir, 234472d9dcfcSEric W. Biederman .readdir = proc_attr_dir_readdir, 23456038f373SArnd Bergmann .llseek = default_llseek, 23461da177e4SLinus Torvalds }; 23471da177e4SLinus Torvalds 234872d9dcfcSEric W. Biederman static struct dentry *proc_attr_dir_lookup(struct inode *dir, 23491da177e4SLinus Torvalds struct dentry *dentry, struct nameidata *nd) 23501da177e4SLinus Torvalds { 23517bcd6b0eSEric W. Biederman return proc_pident_lookup(dir, dentry, 23527bcd6b0eSEric W. Biederman attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff)); 23531da177e4SLinus Torvalds } 23541da177e4SLinus Torvalds 2355c5ef1c42SArjan van de Ven static const struct inode_operations proc_attr_dir_inode_operations = { 235672d9dcfcSEric W. Biederman .lookup = proc_attr_dir_lookup, 235799f89551SEric W. Biederman .getattr = pid_getattr, 23586d76fa58SLinus Torvalds .setattr = proc_setattr, 23591da177e4SLinus Torvalds }; 23601da177e4SLinus Torvalds 23611da177e4SLinus Torvalds #endif 23621da177e4SLinus Torvalds 2363698ba7b5SChristoph Hellwig #ifdef CONFIG_ELF_CORE 23643cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf, 23653cb4a0bbSKawai, Hidehiro size_t count, loff_t *ppos) 23663cb4a0bbSKawai, Hidehiro { 23673cb4a0bbSKawai, Hidehiro struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 23683cb4a0bbSKawai, Hidehiro struct mm_struct *mm; 23693cb4a0bbSKawai, Hidehiro char buffer[PROC_NUMBUF]; 23703cb4a0bbSKawai, Hidehiro size_t len; 23713cb4a0bbSKawai, Hidehiro int ret; 23723cb4a0bbSKawai, Hidehiro 23733cb4a0bbSKawai, Hidehiro if (!task) 23743cb4a0bbSKawai, Hidehiro return -ESRCH; 23753cb4a0bbSKawai, Hidehiro 23763cb4a0bbSKawai, Hidehiro ret = 0; 23773cb4a0bbSKawai, Hidehiro mm = get_task_mm(task); 23783cb4a0bbSKawai, Hidehiro if (mm) { 23793cb4a0bbSKawai, Hidehiro len = snprintf(buffer, sizeof(buffer), "%08lx\n", 23803cb4a0bbSKawai, Hidehiro ((mm->flags & MMF_DUMP_FILTER_MASK) >> 23813cb4a0bbSKawai, Hidehiro MMF_DUMP_FILTER_SHIFT)); 23823cb4a0bbSKawai, Hidehiro mmput(mm); 23833cb4a0bbSKawai, Hidehiro ret = simple_read_from_buffer(buf, count, ppos, buffer, len); 23843cb4a0bbSKawai, Hidehiro } 23853cb4a0bbSKawai, Hidehiro 23863cb4a0bbSKawai, Hidehiro put_task_struct(task); 23873cb4a0bbSKawai, Hidehiro 23883cb4a0bbSKawai, Hidehiro return ret; 23893cb4a0bbSKawai, Hidehiro } 23903cb4a0bbSKawai, Hidehiro 23913cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_write(struct file *file, 23923cb4a0bbSKawai, Hidehiro const char __user *buf, 23933cb4a0bbSKawai, Hidehiro size_t count, 23943cb4a0bbSKawai, Hidehiro loff_t *ppos) 23953cb4a0bbSKawai, Hidehiro { 23963cb4a0bbSKawai, Hidehiro struct task_struct *task; 23973cb4a0bbSKawai, Hidehiro struct mm_struct *mm; 23983cb4a0bbSKawai, Hidehiro char buffer[PROC_NUMBUF], *end; 23993cb4a0bbSKawai, Hidehiro unsigned int val; 24003cb4a0bbSKawai, Hidehiro int ret; 24013cb4a0bbSKawai, Hidehiro int i; 24023cb4a0bbSKawai, Hidehiro unsigned long mask; 24033cb4a0bbSKawai, Hidehiro 24043cb4a0bbSKawai, Hidehiro ret = -EFAULT; 24053cb4a0bbSKawai, Hidehiro memset(buffer, 0, sizeof(buffer)); 24063cb4a0bbSKawai, Hidehiro if (count > sizeof(buffer) - 1) 24073cb4a0bbSKawai, Hidehiro count = sizeof(buffer) - 1; 24083cb4a0bbSKawai, Hidehiro if (copy_from_user(buffer, buf, count)) 24093cb4a0bbSKawai, Hidehiro goto out_no_task; 24103cb4a0bbSKawai, Hidehiro 24113cb4a0bbSKawai, Hidehiro ret = -EINVAL; 24123cb4a0bbSKawai, Hidehiro val = (unsigned int)simple_strtoul(buffer, &end, 0); 24133cb4a0bbSKawai, Hidehiro if (*end == '\n') 24143cb4a0bbSKawai, Hidehiro end++; 24153cb4a0bbSKawai, Hidehiro if (end - buffer == 0) 24163cb4a0bbSKawai, Hidehiro goto out_no_task; 24173cb4a0bbSKawai, Hidehiro 24183cb4a0bbSKawai, Hidehiro ret = -ESRCH; 24193cb4a0bbSKawai, Hidehiro task = get_proc_task(file->f_dentry->d_inode); 24203cb4a0bbSKawai, Hidehiro if (!task) 24213cb4a0bbSKawai, Hidehiro goto out_no_task; 24223cb4a0bbSKawai, Hidehiro 24233cb4a0bbSKawai, Hidehiro ret = end - buffer; 24243cb4a0bbSKawai, Hidehiro mm = get_task_mm(task); 24253cb4a0bbSKawai, Hidehiro if (!mm) 24263cb4a0bbSKawai, Hidehiro goto out_no_mm; 24273cb4a0bbSKawai, Hidehiro 24283cb4a0bbSKawai, Hidehiro for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) { 24293cb4a0bbSKawai, Hidehiro if (val & mask) 24303cb4a0bbSKawai, Hidehiro set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags); 24313cb4a0bbSKawai, Hidehiro else 24323cb4a0bbSKawai, Hidehiro clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags); 24333cb4a0bbSKawai, Hidehiro } 24343cb4a0bbSKawai, Hidehiro 24353cb4a0bbSKawai, Hidehiro mmput(mm); 24363cb4a0bbSKawai, Hidehiro out_no_mm: 24373cb4a0bbSKawai, Hidehiro put_task_struct(task); 24383cb4a0bbSKawai, Hidehiro out_no_task: 24393cb4a0bbSKawai, Hidehiro return ret; 24403cb4a0bbSKawai, Hidehiro } 24413cb4a0bbSKawai, Hidehiro 24423cb4a0bbSKawai, Hidehiro static const struct file_operations proc_coredump_filter_operations = { 24433cb4a0bbSKawai, Hidehiro .read = proc_coredump_filter_read, 24443cb4a0bbSKawai, Hidehiro .write = proc_coredump_filter_write, 244587df8424SArnd Bergmann .llseek = generic_file_llseek, 24463cb4a0bbSKawai, Hidehiro }; 24473cb4a0bbSKawai, Hidehiro #endif 24483cb4a0bbSKawai, Hidehiro 24491da177e4SLinus Torvalds /* 24501da177e4SLinus Torvalds * /proc/self: 24511da177e4SLinus Torvalds */ 24521da177e4SLinus Torvalds static int proc_self_readlink(struct dentry *dentry, char __user *buffer, 24531da177e4SLinus Torvalds int buflen) 24541da177e4SLinus Torvalds { 2455488e5bc4SEric W. Biederman struct pid_namespace *ns = dentry->d_sb->s_fs_info; 2456b55fcb22SAndrew Morton pid_t tgid = task_tgid_nr_ns(current, ns); 24578578cea7SEric W. Biederman char tmp[PROC_NUMBUF]; 2458b55fcb22SAndrew Morton if (!tgid) 2459488e5bc4SEric W. Biederman return -ENOENT; 2460b55fcb22SAndrew Morton sprintf(tmp, "%d", tgid); 24611da177e4SLinus Torvalds return vfs_readlink(dentry,buffer,buflen,tmp); 24621da177e4SLinus Torvalds } 24631da177e4SLinus Torvalds 2464008b150aSAl Viro static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd) 24651da177e4SLinus Torvalds { 2466488e5bc4SEric W. Biederman struct pid_namespace *ns = dentry->d_sb->s_fs_info; 2467b55fcb22SAndrew Morton pid_t tgid = task_tgid_nr_ns(current, ns); 24687fee4868SAl Viro char *name = ERR_PTR(-ENOENT); 24697fee4868SAl Viro if (tgid) { 24707fee4868SAl Viro name = __getname(); 24717fee4868SAl Viro if (!name) 24727fee4868SAl Viro name = ERR_PTR(-ENOMEM); 24737fee4868SAl Viro else 24747fee4868SAl Viro sprintf(name, "%d", tgid); 24757fee4868SAl Viro } 24767fee4868SAl Viro nd_set_link(nd, name); 24777fee4868SAl Viro return NULL; 24787fee4868SAl Viro } 24797fee4868SAl Viro 24807fee4868SAl Viro static void proc_self_put_link(struct dentry *dentry, struct nameidata *nd, 24817fee4868SAl Viro void *cookie) 24827fee4868SAl Viro { 24837fee4868SAl Viro char *s = nd_get_link(nd); 24847fee4868SAl Viro if (!IS_ERR(s)) 24857fee4868SAl Viro __putname(s); 24861da177e4SLinus Torvalds } 24871da177e4SLinus Torvalds 2488c5ef1c42SArjan van de Ven static const struct inode_operations proc_self_inode_operations = { 24891da177e4SLinus Torvalds .readlink = proc_self_readlink, 24901da177e4SLinus Torvalds .follow_link = proc_self_follow_link, 24917fee4868SAl Viro .put_link = proc_self_put_link, 24921da177e4SLinus Torvalds }; 24931da177e4SLinus Torvalds 249428a6d671SEric W. Biederman /* 2495801199ceSEric W. Biederman * proc base 2496801199ceSEric W. Biederman * 2497801199ceSEric W. Biederman * These are the directory entries in the root directory of /proc 2498801199ceSEric W. Biederman * that properly belong to the /proc filesystem, as they describe 2499801199ceSEric W. Biederman * describe something that is process related. 2500801199ceSEric W. Biederman */ 2501c5141e6dSEric Dumazet static const struct pid_entry proc_base_stuff[] = { 250261a28784SEric W. Biederman NOD("self", S_IFLNK|S_IRWXUGO, 2503801199ceSEric W. Biederman &proc_self_inode_operations, NULL, {}), 2504801199ceSEric W. Biederman }; 2505801199ceSEric W. Biederman 2506444ceed8SEric W. Biederman static struct dentry *proc_base_instantiate(struct inode *dir, 2507c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 2508801199ceSEric W. Biederman { 2509c5141e6dSEric Dumazet const struct pid_entry *p = ptr; 2510801199ceSEric W. Biederman struct inode *inode; 2511801199ceSEric W. Biederman struct proc_inode *ei; 251273d36460SDan Carpenter struct dentry *error; 2513801199ceSEric W. Biederman 2514801199ceSEric W. Biederman /* Allocate the inode */ 2515801199ceSEric W. Biederman error = ERR_PTR(-ENOMEM); 2516801199ceSEric W. Biederman inode = new_inode(dir->i_sb); 2517801199ceSEric W. Biederman if (!inode) 2518801199ceSEric W. Biederman goto out; 2519801199ceSEric W. Biederman 2520801199ceSEric W. Biederman /* Initialize the inode */ 2521801199ceSEric W. Biederman ei = PROC_I(inode); 252285fe4025SChristoph Hellwig inode->i_ino = get_next_ino(); 2523801199ceSEric W. Biederman inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; 2524801199ceSEric W. Biederman 2525801199ceSEric W. Biederman /* 2526801199ceSEric W. Biederman * grab the reference to the task. 2527801199ceSEric W. Biederman */ 25281a657f78SOleg Nesterov ei->pid = get_task_pid(task, PIDTYPE_PID); 2529801199ceSEric W. Biederman if (!ei->pid) 2530801199ceSEric W. Biederman goto out_iput; 2531801199ceSEric W. Biederman 2532801199ceSEric W. Biederman inode->i_mode = p->mode; 2533801199ceSEric W. Biederman if (S_ISDIR(inode->i_mode)) 2534bfe86848SMiklos Szeredi set_nlink(inode, 2); 2535801199ceSEric W. Biederman if (S_ISLNK(inode->i_mode)) 2536801199ceSEric W. Biederman inode->i_size = 64; 2537801199ceSEric W. Biederman if (p->iop) 2538801199ceSEric W. Biederman inode->i_op = p->iop; 2539801199ceSEric W. Biederman if (p->fop) 2540801199ceSEric W. Biederman inode->i_fop = p->fop; 2541801199ceSEric W. Biederman ei->op = p->op; 2542801199ceSEric W. Biederman d_add(dentry, inode); 2543801199ceSEric W. Biederman error = NULL; 2544801199ceSEric W. Biederman out: 2545801199ceSEric W. Biederman return error; 2546801199ceSEric W. Biederman out_iput: 2547801199ceSEric W. Biederman iput(inode); 2548801199ceSEric W. Biederman goto out; 2549801199ceSEric W. Biederman } 2550801199ceSEric W. Biederman 2551444ceed8SEric W. Biederman static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry) 2552444ceed8SEric W. Biederman { 2553444ceed8SEric W. Biederman struct dentry *error; 2554444ceed8SEric W. Biederman struct task_struct *task = get_proc_task(dir); 2555c5141e6dSEric Dumazet const struct pid_entry *p, *last; 2556444ceed8SEric W. Biederman 2557444ceed8SEric W. Biederman error = ERR_PTR(-ENOENT); 2558444ceed8SEric W. Biederman 2559444ceed8SEric W. Biederman if (!task) 2560444ceed8SEric W. Biederman goto out_no_task; 2561444ceed8SEric W. Biederman 2562444ceed8SEric W. Biederman /* Lookup the directory entry */ 25637bcd6b0eSEric W. Biederman last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1]; 25647bcd6b0eSEric W. Biederman for (p = proc_base_stuff; p <= last; p++) { 2565444ceed8SEric W. Biederman if (p->len != dentry->d_name.len) 2566444ceed8SEric W. Biederman continue; 2567444ceed8SEric W. Biederman if (!memcmp(dentry->d_name.name, p->name, p->len)) 2568444ceed8SEric W. Biederman break; 2569444ceed8SEric W. Biederman } 25707bcd6b0eSEric W. Biederman if (p > last) 2571444ceed8SEric W. Biederman goto out; 2572444ceed8SEric W. Biederman 2573444ceed8SEric W. Biederman error = proc_base_instantiate(dir, dentry, task, p); 2574444ceed8SEric W. Biederman 2575444ceed8SEric W. Biederman out: 2576444ceed8SEric W. Biederman put_task_struct(task); 2577444ceed8SEric W. Biederman out_no_task: 2578444ceed8SEric W. Biederman return error; 2579444ceed8SEric W. Biederman } 2580444ceed8SEric W. Biederman 2581c5141e6dSEric Dumazet static int proc_base_fill_cache(struct file *filp, void *dirent, 2582c5141e6dSEric Dumazet filldir_t filldir, struct task_struct *task, const struct pid_entry *p) 258361a28784SEric W. Biederman { 258461a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, p->name, p->len, 258561a28784SEric W. Biederman proc_base_instantiate, task, p); 258661a28784SEric W. Biederman } 258761a28784SEric W. Biederman 2588aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING 2589297c5d92SAndrea Righi static int do_io_accounting(struct task_struct *task, char *buffer, int whole) 2590aba76fdbSAndrew Morton { 2591940389b8SAndrea Righi struct task_io_accounting acct = task->ioac; 2592297c5d92SAndrea Righi unsigned long flags; 2593293eb1e7SVasiliy Kulikov int result; 2594297c5d92SAndrea Righi 2595293eb1e7SVasiliy Kulikov result = mutex_lock_killable(&task->signal->cred_guard_mutex); 2596293eb1e7SVasiliy Kulikov if (result) 2597293eb1e7SVasiliy Kulikov return result; 2598293eb1e7SVasiliy Kulikov 2599293eb1e7SVasiliy Kulikov if (!ptrace_may_access(task, PTRACE_MODE_READ)) { 2600293eb1e7SVasiliy Kulikov result = -EACCES; 2601293eb1e7SVasiliy Kulikov goto out_unlock; 2602293eb1e7SVasiliy Kulikov } 26031d1221f3SVasiliy Kulikov 26045995477aSAndrea Righi if (whole && lock_task_sighand(task, &flags)) { 2605b2d002dbSAndrea Righi struct task_struct *t = task; 2606297c5d92SAndrea Righi 26075995477aSAndrea Righi task_io_accounting_add(&acct, &task->signal->ioac); 26085995477aSAndrea Righi while_each_thread(task, t) 26095995477aSAndrea Righi task_io_accounting_add(&acct, &t->ioac); 2610297c5d92SAndrea Righi 2611297c5d92SAndrea Righi unlock_task_sighand(task, &flags); 2612297c5d92SAndrea Righi } 2613293eb1e7SVasiliy Kulikov result = sprintf(buffer, 2614aba76fdbSAndrew Morton "rchar: %llu\n" 2615aba76fdbSAndrew Morton "wchar: %llu\n" 2616aba76fdbSAndrew Morton "syscr: %llu\n" 2617aba76fdbSAndrew Morton "syscw: %llu\n" 2618aba76fdbSAndrew Morton "read_bytes: %llu\n" 2619aba76fdbSAndrew Morton "write_bytes: %llu\n" 2620aba76fdbSAndrew Morton "cancelled_write_bytes: %llu\n", 26217c44319dSAlexander Beregalov (unsigned long long)acct.rchar, 26227c44319dSAlexander Beregalov (unsigned long long)acct.wchar, 26237c44319dSAlexander Beregalov (unsigned long long)acct.syscr, 26247c44319dSAlexander Beregalov (unsigned long long)acct.syscw, 26257c44319dSAlexander Beregalov (unsigned long long)acct.read_bytes, 26267c44319dSAlexander Beregalov (unsigned long long)acct.write_bytes, 26277c44319dSAlexander Beregalov (unsigned long long)acct.cancelled_write_bytes); 2628293eb1e7SVasiliy Kulikov out_unlock: 2629293eb1e7SVasiliy Kulikov mutex_unlock(&task->signal->cred_guard_mutex); 2630293eb1e7SVasiliy Kulikov return result; 2631aba76fdbSAndrew Morton } 2632297c5d92SAndrea Righi 2633297c5d92SAndrea Righi static int proc_tid_io_accounting(struct task_struct *task, char *buffer) 2634297c5d92SAndrea Righi { 2635297c5d92SAndrea Righi return do_io_accounting(task, buffer, 0); 2636297c5d92SAndrea Righi } 2637297c5d92SAndrea Righi 2638297c5d92SAndrea Righi static int proc_tgid_io_accounting(struct task_struct *task, char *buffer) 2639297c5d92SAndrea Righi { 2640297c5d92SAndrea Righi return do_io_accounting(task, buffer, 1); 2641297c5d92SAndrea Righi } 2642297c5d92SAndrea Righi #endif /* CONFIG_TASK_IO_ACCOUNTING */ 2643aba76fdbSAndrew Morton 264447830723SKees Cook static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns, 264547830723SKees Cook struct pid *pid, struct task_struct *task) 264647830723SKees Cook { 2647a9712bc1SAl Viro int err = lock_trace(task); 2648a9712bc1SAl Viro if (!err) { 264947830723SKees Cook seq_printf(m, "%08x\n", task->personality); 2650a9712bc1SAl Viro unlock_trace(task); 2651a9712bc1SAl Viro } 2652a9712bc1SAl Viro return err; 265347830723SKees Cook } 265447830723SKees Cook 2655801199ceSEric W. Biederman /* 265628a6d671SEric W. Biederman * Thread groups 265728a6d671SEric W. Biederman */ 265800977a59SArjan van de Ven static const struct file_operations proc_task_operations; 2659c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations; 266020cdc894SEric W. Biederman 2661c5141e6dSEric Dumazet static const struct pid_entry tgid_base_stuff[] = { 2662631f9c18SAlexey Dobriyan DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations), 2663631f9c18SAlexey Dobriyan DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations), 2664631f9c18SAlexey Dobriyan DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations), 26656b4e306aSEric W. Biederman DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations), 2666b2211a36SAndrew Morton #ifdef CONFIG_NET 2667631f9c18SAlexey Dobriyan DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations), 2668b2211a36SAndrew Morton #endif 2669631f9c18SAlexey Dobriyan REG("environ", S_IRUSR, proc_environ_operations), 2670631f9c18SAlexey Dobriyan INF("auxv", S_IRUSR, proc_pid_auxv), 2671631f9c18SAlexey Dobriyan ONE("status", S_IRUGO, proc_pid_status), 2672a9712bc1SAl Viro ONE("personality", S_IRUGO, proc_pid_personality), 26733036e7b4SJiri Olsa INF("limits", S_IRUGO, proc_pid_limits), 267443ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG 2675631f9c18SAlexey Dobriyan REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations), 267643ae34cbSIngo Molnar #endif 26775091faa4SMike Galbraith #ifdef CONFIG_SCHED_AUTOGROUP 26785091faa4SMike Galbraith REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations), 26795091faa4SMike Galbraith #endif 26804614a696Sjohn stultz REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations), 2681ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK 2682a9712bc1SAl Viro INF("syscall", S_IRUGO, proc_pid_syscall), 2683ebcb6734SRoland McGrath #endif 2684631f9c18SAlexey Dobriyan INF("cmdline", S_IRUGO, proc_pid_cmdline), 2685631f9c18SAlexey Dobriyan ONE("stat", S_IRUGO, proc_tgid_stat), 2686631f9c18SAlexey Dobriyan ONE("statm", S_IRUGO, proc_pid_statm), 2687631f9c18SAlexey Dobriyan REG("maps", S_IRUGO, proc_maps_operations), 268828a6d671SEric W. Biederman #ifdef CONFIG_NUMA 2689631f9c18SAlexey Dobriyan REG("numa_maps", S_IRUGO, proc_numa_maps_operations), 269028a6d671SEric W. Biederman #endif 2691631f9c18SAlexey Dobriyan REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations), 2692631f9c18SAlexey Dobriyan LNK("cwd", proc_cwd_link), 2693631f9c18SAlexey Dobriyan LNK("root", proc_root_link), 2694631f9c18SAlexey Dobriyan LNK("exe", proc_exe_link), 2695631f9c18SAlexey Dobriyan REG("mounts", S_IRUGO, proc_mounts_operations), 2696631f9c18SAlexey Dobriyan REG("mountinfo", S_IRUGO, proc_mountinfo_operations), 2697631f9c18SAlexey Dobriyan REG("mountstats", S_IRUSR, proc_mountstats_operations), 26981e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR 2699631f9c18SAlexey Dobriyan REG("clear_refs", S_IWUSR, proc_clear_refs_operations), 2700631f9c18SAlexey Dobriyan REG("smaps", S_IRUGO, proc_smaps_operations), 2701ca6b0bf0SAl Viro REG("pagemap", S_IRUGO, proc_pagemap_operations), 270228a6d671SEric W. Biederman #endif 270328a6d671SEric W. Biederman #ifdef CONFIG_SECURITY 2704631f9c18SAlexey Dobriyan DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations), 270528a6d671SEric W. Biederman #endif 270628a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS 2707631f9c18SAlexey Dobriyan INF("wchan", S_IRUGO, proc_pid_wchan), 270828a6d671SEric W. Biederman #endif 27092ec220e2SKen Chen #ifdef CONFIG_STACKTRACE 2710a9712bc1SAl Viro ONE("stack", S_IRUGO, proc_pid_stack), 271128a6d671SEric W. Biederman #endif 271228a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS 2713631f9c18SAlexey Dobriyan INF("schedstat", S_IRUGO, proc_pid_schedstat), 271428a6d671SEric W. Biederman #endif 27159745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP 2716631f9c18SAlexey Dobriyan REG("latency", S_IRUGO, proc_lstats_operations), 27179745512cSArjan van de Ven #endif 27188793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET 2719631f9c18SAlexey Dobriyan REG("cpuset", S_IRUGO, proc_cpuset_operations), 272028a6d671SEric W. Biederman #endif 2721a424316cSPaul Menage #ifdef CONFIG_CGROUPS 2722631f9c18SAlexey Dobriyan REG("cgroup", S_IRUGO, proc_cgroup_operations), 2723a424316cSPaul Menage #endif 2724631f9c18SAlexey Dobriyan INF("oom_score", S_IRUGO, proc_oom_score), 2725631f9c18SAlexey Dobriyan REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations), 2726a63d83f4SDavid Rientjes REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations), 272728a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL 2728631f9c18SAlexey Dobriyan REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations), 2729631f9c18SAlexey Dobriyan REG("sessionid", S_IRUGO, proc_sessionid_operations), 273028a6d671SEric W. Biederman #endif 2731f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION 2732631f9c18SAlexey Dobriyan REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations), 2733f4f154fdSAkinobu Mita #endif 2734698ba7b5SChristoph Hellwig #ifdef CONFIG_ELF_CORE 2735631f9c18SAlexey Dobriyan REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations), 27363cb4a0bbSKawai, Hidehiro #endif 2737aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING 27381d1221f3SVasiliy Kulikov INF("io", S_IRUSR, proc_tgid_io_accounting), 2739aba76fdbSAndrew Morton #endif 2740f133eccaSChris Metcalf #ifdef CONFIG_HARDWALL 2741f133eccaSChris Metcalf INF("hardwall", S_IRUGO, proc_pid_hardwall), 2742f133eccaSChris Metcalf #endif 274328a6d671SEric W. Biederman }; 274428a6d671SEric W. Biederman 274528a6d671SEric W. Biederman static int proc_tgid_base_readdir(struct file * filp, 274628a6d671SEric W. Biederman void * dirent, filldir_t filldir) 274728a6d671SEric W. Biederman { 274828a6d671SEric W. Biederman return proc_pident_readdir(filp,dirent,filldir, 274928a6d671SEric W. Biederman tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff)); 275028a6d671SEric W. Biederman } 275128a6d671SEric W. Biederman 275200977a59SArjan van de Ven static const struct file_operations proc_tgid_base_operations = { 275328a6d671SEric W. Biederman .read = generic_read_dir, 275428a6d671SEric W. Biederman .readdir = proc_tgid_base_readdir, 27556038f373SArnd Bergmann .llseek = default_llseek, 275628a6d671SEric W. Biederman }; 275728a6d671SEric W. Biederman 275828a6d671SEric W. Biederman static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){ 27597bcd6b0eSEric W. Biederman return proc_pident_lookup(dir, dentry, 27607bcd6b0eSEric W. Biederman tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff)); 276128a6d671SEric W. Biederman } 276228a6d671SEric W. Biederman 2763c5ef1c42SArjan van de Ven static const struct inode_operations proc_tgid_base_inode_operations = { 276428a6d671SEric W. Biederman .lookup = proc_tgid_base_lookup, 276528a6d671SEric W. Biederman .getattr = pid_getattr, 276628a6d671SEric W. Biederman .setattr = proc_setattr, 276728a6d671SEric W. Biederman }; 276828a6d671SEric W. Biederman 276960347f67SPavel Emelyanov static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid) 27701da177e4SLinus Torvalds { 277148e6484dSEric W. Biederman struct dentry *dentry, *leader, *dir; 27728578cea7SEric W. Biederman char buf[PROC_NUMBUF]; 277348e6484dSEric W. Biederman struct qstr name; 27741da177e4SLinus Torvalds 277548e6484dSEric W. Biederman name.name = buf; 277660347f67SPavel Emelyanov name.len = snprintf(buf, sizeof(buf), "%d", pid); 277760347f67SPavel Emelyanov dentry = d_hash_and_lookup(mnt->mnt_root, &name); 277848e6484dSEric W. Biederman if (dentry) { 277948e6484dSEric W. Biederman shrink_dcache_parent(dentry); 278048e6484dSEric W. Biederman d_drop(dentry); 278148e6484dSEric W. Biederman dput(dentry); 27821da177e4SLinus Torvalds } 27831da177e4SLinus Torvalds 278448e6484dSEric W. Biederman name.name = buf; 278560347f67SPavel Emelyanov name.len = snprintf(buf, sizeof(buf), "%d", tgid); 278660347f67SPavel Emelyanov leader = d_hash_and_lookup(mnt->mnt_root, &name); 278748e6484dSEric W. Biederman if (!leader) 278848e6484dSEric W. Biederman goto out; 278948e6484dSEric W. Biederman 279048e6484dSEric W. Biederman name.name = "task"; 279148e6484dSEric W. Biederman name.len = strlen(name.name); 279248e6484dSEric W. Biederman dir = d_hash_and_lookup(leader, &name); 279348e6484dSEric W. Biederman if (!dir) 279448e6484dSEric W. Biederman goto out_put_leader; 279548e6484dSEric W. Biederman 279648e6484dSEric W. Biederman name.name = buf; 279760347f67SPavel Emelyanov name.len = snprintf(buf, sizeof(buf), "%d", pid); 279848e6484dSEric W. Biederman dentry = d_hash_and_lookup(dir, &name); 279948e6484dSEric W. Biederman if (dentry) { 280048e6484dSEric W. Biederman shrink_dcache_parent(dentry); 280148e6484dSEric W. Biederman d_drop(dentry); 280248e6484dSEric W. Biederman dput(dentry); 28031da177e4SLinus Torvalds } 280448e6484dSEric W. Biederman 280548e6484dSEric W. Biederman dput(dir); 280648e6484dSEric W. Biederman out_put_leader: 280748e6484dSEric W. Biederman dput(leader); 280848e6484dSEric W. Biederman out: 280948e6484dSEric W. Biederman return; 28101da177e4SLinus Torvalds } 28111da177e4SLinus Torvalds 28120895e91dSRandy Dunlap /** 28130895e91dSRandy Dunlap * proc_flush_task - Remove dcache entries for @task from the /proc dcache. 28140895e91dSRandy Dunlap * @task: task that should be flushed. 28150895e91dSRandy Dunlap * 28160895e91dSRandy Dunlap * When flushing dentries from proc, one needs to flush them from global 281760347f67SPavel Emelyanov * proc (proc_mnt) and from all the namespaces' procs this task was seen 28180895e91dSRandy Dunlap * in. This call is supposed to do all of this job. 28190895e91dSRandy Dunlap * 28200895e91dSRandy Dunlap * Looks in the dcache for 28210895e91dSRandy Dunlap * /proc/@pid 28220895e91dSRandy Dunlap * /proc/@tgid/task/@pid 28230895e91dSRandy Dunlap * if either directory is present flushes it and all of it'ts children 28240895e91dSRandy Dunlap * from the dcache. 28250895e91dSRandy Dunlap * 28260895e91dSRandy Dunlap * It is safe and reasonable to cache /proc entries for a task until 28270895e91dSRandy Dunlap * that task exits. After that they just clog up the dcache with 28280895e91dSRandy Dunlap * useless entries, possibly causing useful dcache entries to be 28290895e91dSRandy Dunlap * flushed instead. This routine is proved to flush those useless 28300895e91dSRandy Dunlap * dcache entries at process exit time. 28310895e91dSRandy Dunlap * 28320895e91dSRandy Dunlap * NOTE: This routine is just an optimization so it does not guarantee 28330895e91dSRandy Dunlap * that no dcache entries will exist at process exit time it 28340895e91dSRandy Dunlap * just makes it very unlikely that any will persist. 283560347f67SPavel Emelyanov */ 283660347f67SPavel Emelyanov 283760347f67SPavel Emelyanov void proc_flush_task(struct task_struct *task) 283860347f67SPavel Emelyanov { 28399fcc2d15SEric W. Biederman int i; 28409b4d1cbeSOleg Nesterov struct pid *pid, *tgid; 2841130f77ecSPavel Emelyanov struct upid *upid; 2842130f77ecSPavel Emelyanov 2843130f77ecSPavel Emelyanov pid = task_pid(task); 2844130f77ecSPavel Emelyanov tgid = task_tgid(task); 28459fcc2d15SEric W. Biederman 28469fcc2d15SEric W. Biederman for (i = 0; i <= pid->level; i++) { 2847130f77ecSPavel Emelyanov upid = &pid->numbers[i]; 2848130f77ecSPavel Emelyanov proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr, 28499b4d1cbeSOleg Nesterov tgid->numbers[i].nr); 2850130f77ecSPavel Emelyanov } 28516f4e6433SPavel Emelyanov 28526f4e6433SPavel Emelyanov upid = &pid->numbers[pid->level]; 28536f4e6433SPavel Emelyanov if (upid->nr == 1) 28546f4e6433SPavel Emelyanov pid_ns_release_proc(upid->ns); 285560347f67SPavel Emelyanov } 285660347f67SPavel Emelyanov 28579711ef99SAdrian Bunk static struct dentry *proc_pid_instantiate(struct inode *dir, 28589711ef99SAdrian Bunk struct dentry * dentry, 2859c5141e6dSEric Dumazet struct task_struct *task, const void *ptr) 2860444ceed8SEric W. Biederman { 2861444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-ENOENT); 2862444ceed8SEric W. Biederman struct inode *inode; 2863444ceed8SEric W. Biederman 286461a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 2865444ceed8SEric W. Biederman if (!inode) 2866444ceed8SEric W. Biederman goto out; 2867444ceed8SEric W. Biederman 2868444ceed8SEric W. Biederman inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO; 2869444ceed8SEric W. Biederman inode->i_op = &proc_tgid_base_inode_operations; 2870444ceed8SEric W. Biederman inode->i_fop = &proc_tgid_base_operations; 2871444ceed8SEric W. Biederman inode->i_flags|=S_IMMUTABLE; 2872aed54175SVegard Nossum 2873bfe86848SMiklos Szeredi set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff, 2874bfe86848SMiklos Szeredi ARRAY_SIZE(tgid_base_stuff))); 2875444ceed8SEric W. Biederman 2876fb045adbSNick Piggin d_set_d_op(dentry, &pid_dentry_operations); 2877444ceed8SEric W. Biederman 2878444ceed8SEric W. Biederman d_add(dentry, inode); 2879444ceed8SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 2880444ceed8SEric W. Biederman if (pid_revalidate(dentry, NULL)) 2881444ceed8SEric W. Biederman error = NULL; 2882444ceed8SEric W. Biederman out: 2883444ceed8SEric W. Biederman return error; 2884444ceed8SEric W. Biederman } 2885444ceed8SEric W. Biederman 28861da177e4SLinus Torvalds struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd) 28871da177e4SLinus Torvalds { 288873d36460SDan Carpenter struct dentry *result; 28891da177e4SLinus Torvalds struct task_struct *task; 28901da177e4SLinus Torvalds unsigned tgid; 2891b488893aSPavel Emelyanov struct pid_namespace *ns; 28921da177e4SLinus Torvalds 2893801199ceSEric W. Biederman result = proc_base_lookup(dir, dentry); 2894801199ceSEric W. Biederman if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT) 2895801199ceSEric W. Biederman goto out; 2896801199ceSEric W. Biederman 28971da177e4SLinus Torvalds tgid = name_to_int(dentry); 28981da177e4SLinus Torvalds if (tgid == ~0U) 28991da177e4SLinus Torvalds goto out; 29001da177e4SLinus Torvalds 2901b488893aSPavel Emelyanov ns = dentry->d_sb->s_fs_info; 2902de758734SEric W. Biederman rcu_read_lock(); 2903b488893aSPavel Emelyanov task = find_task_by_pid_ns(tgid, ns); 29041da177e4SLinus Torvalds if (task) 29051da177e4SLinus Torvalds get_task_struct(task); 2906de758734SEric W. Biederman rcu_read_unlock(); 29071da177e4SLinus Torvalds if (!task) 29081da177e4SLinus Torvalds goto out; 29091da177e4SLinus Torvalds 2910444ceed8SEric W. Biederman result = proc_pid_instantiate(dir, dentry, task, NULL); 291148e6484dSEric W. Biederman put_task_struct(task); 29121da177e4SLinus Torvalds out: 2913cd6a3ce9SEric W. Biederman return result; 29141da177e4SLinus Torvalds } 29151da177e4SLinus Torvalds 29161da177e4SLinus Torvalds /* 29170804ef4bSEric W. Biederman * Find the first task with tgid >= tgid 29180bc58a91SEric W. Biederman * 29191da177e4SLinus Torvalds */ 292019fd4bb2SEric W. Biederman struct tgid_iter { 292119fd4bb2SEric W. Biederman unsigned int tgid; 29220804ef4bSEric W. Biederman struct task_struct *task; 292319fd4bb2SEric W. Biederman }; 292419fd4bb2SEric W. Biederman static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter) 292519fd4bb2SEric W. Biederman { 29260804ef4bSEric W. Biederman struct pid *pid; 29271da177e4SLinus Torvalds 292819fd4bb2SEric W. Biederman if (iter.task) 292919fd4bb2SEric W. Biederman put_task_struct(iter.task); 29300804ef4bSEric W. Biederman rcu_read_lock(); 29310804ef4bSEric W. Biederman retry: 293219fd4bb2SEric W. Biederman iter.task = NULL; 293319fd4bb2SEric W. Biederman pid = find_ge_pid(iter.tgid, ns); 29340804ef4bSEric W. Biederman if (pid) { 293519fd4bb2SEric W. Biederman iter.tgid = pid_nr_ns(pid, ns); 293619fd4bb2SEric W. Biederman iter.task = pid_task(pid, PIDTYPE_PID); 29370804ef4bSEric W. Biederman /* What we to know is if the pid we have find is the 29380804ef4bSEric W. Biederman * pid of a thread_group_leader. Testing for task 29390804ef4bSEric W. Biederman * being a thread_group_leader is the obvious thing 29400804ef4bSEric W. Biederman * todo but there is a window when it fails, due to 29410804ef4bSEric W. Biederman * the pid transfer logic in de_thread. 29420804ef4bSEric W. Biederman * 29430804ef4bSEric W. Biederman * So we perform the straight forward test of seeing 29440804ef4bSEric W. Biederman * if the pid we have found is the pid of a thread 29450804ef4bSEric W. Biederman * group leader, and don't worry if the task we have 29460804ef4bSEric W. Biederman * found doesn't happen to be a thread group leader. 29470804ef4bSEric W. Biederman * As we don't care in the case of readdir. 29480bc58a91SEric W. Biederman */ 294919fd4bb2SEric W. Biederman if (!iter.task || !has_group_leader_pid(iter.task)) { 295019fd4bb2SEric W. Biederman iter.tgid += 1; 29510804ef4bSEric W. Biederman goto retry; 295219fd4bb2SEric W. Biederman } 295319fd4bb2SEric W. Biederman get_task_struct(iter.task); 29541da177e4SLinus Torvalds } 2955454cc105SEric W. Biederman rcu_read_unlock(); 295619fd4bb2SEric W. Biederman return iter; 29571da177e4SLinus Torvalds } 29581da177e4SLinus Torvalds 29597bcd6b0eSEric W. Biederman #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff)) 29601da177e4SLinus Torvalds 296161a28784SEric W. Biederman static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir, 296219fd4bb2SEric W. Biederman struct tgid_iter iter) 296361a28784SEric W. Biederman { 296461a28784SEric W. Biederman char name[PROC_NUMBUF]; 296519fd4bb2SEric W. Biederman int len = snprintf(name, sizeof(name), "%d", iter.tgid); 296661a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, name, len, 296719fd4bb2SEric W. Biederman proc_pid_instantiate, iter.task, NULL); 296861a28784SEric W. Biederman } 296961a28784SEric W. Biederman 29701da177e4SLinus Torvalds /* for the /proc/ directory itself, after non-process stuff has been done */ 29711da177e4SLinus Torvalds int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir) 29721da177e4SLinus Torvalds { 2973d8bdc59fSLinus Torvalds unsigned int nr; 2974d8bdc59fSLinus Torvalds struct task_struct *reaper; 297519fd4bb2SEric W. Biederman struct tgid_iter iter; 2976b488893aSPavel Emelyanov struct pid_namespace *ns; 29771da177e4SLinus Torvalds 2978d8bdc59fSLinus Torvalds if (filp->f_pos >= PID_MAX_LIMIT + TGID_OFFSET) 2979d8bdc59fSLinus Torvalds goto out_no_task; 2980d8bdc59fSLinus Torvalds nr = filp->f_pos - FIRST_PROCESS_ENTRY; 2981d8bdc59fSLinus Torvalds 2982d8bdc59fSLinus Torvalds reaper = get_proc_task(filp->f_path.dentry->d_inode); 298361a28784SEric W. Biederman if (!reaper) 298461a28784SEric W. Biederman goto out_no_task; 298561a28784SEric W. Biederman 29867bcd6b0eSEric W. Biederman for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) { 2987c5141e6dSEric Dumazet const struct pid_entry *p = &proc_base_stuff[nr]; 298861a28784SEric W. Biederman if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0) 2989801199ceSEric W. Biederman goto out; 29901da177e4SLinus Torvalds } 29911da177e4SLinus Torvalds 2992b488893aSPavel Emelyanov ns = filp->f_dentry->d_sb->s_fs_info; 299319fd4bb2SEric W. Biederman iter.task = NULL; 299419fd4bb2SEric W. Biederman iter.tgid = filp->f_pos - TGID_OFFSET; 299519fd4bb2SEric W. Biederman for (iter = next_tgid(ns, iter); 299619fd4bb2SEric W. Biederman iter.task; 299719fd4bb2SEric W. Biederman iter.tgid += 1, iter = next_tgid(ns, iter)) { 299819fd4bb2SEric W. Biederman filp->f_pos = iter.tgid + TGID_OFFSET; 299919fd4bb2SEric W. Biederman if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) { 300019fd4bb2SEric W. Biederman put_task_struct(iter.task); 30010804ef4bSEric W. Biederman goto out; 30021da177e4SLinus Torvalds } 30031da177e4SLinus Torvalds } 30040804ef4bSEric W. Biederman filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET; 30050804ef4bSEric W. Biederman out: 300661a28784SEric W. Biederman put_task_struct(reaper); 300761a28784SEric W. Biederman out_no_task: 30081da177e4SLinus Torvalds return 0; 30091da177e4SLinus Torvalds } 30101da177e4SLinus Torvalds 30110bc58a91SEric W. Biederman /* 301228a6d671SEric W. Biederman * Tasks 301328a6d671SEric W. Biederman */ 3014c5141e6dSEric Dumazet static const struct pid_entry tid_base_stuff[] = { 3015631f9c18SAlexey Dobriyan DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations), 30163835541dSJerome Marchand DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations), 30176b4e306aSEric W. Biederman DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations), 3018631f9c18SAlexey Dobriyan REG("environ", S_IRUSR, proc_environ_operations), 3019631f9c18SAlexey Dobriyan INF("auxv", S_IRUSR, proc_pid_auxv), 3020631f9c18SAlexey Dobriyan ONE("status", S_IRUGO, proc_pid_status), 3021a9712bc1SAl Viro ONE("personality", S_IRUGO, proc_pid_personality), 30223036e7b4SJiri Olsa INF("limits", S_IRUGO, proc_pid_limits), 302343ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG 3024631f9c18SAlexey Dobriyan REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations), 302543ae34cbSIngo Molnar #endif 30264614a696Sjohn stultz REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations), 3027ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK 3028a9712bc1SAl Viro INF("syscall", S_IRUGO, proc_pid_syscall), 3029ebcb6734SRoland McGrath #endif 3030631f9c18SAlexey Dobriyan INF("cmdline", S_IRUGO, proc_pid_cmdline), 3031631f9c18SAlexey Dobriyan ONE("stat", S_IRUGO, proc_tid_stat), 3032631f9c18SAlexey Dobriyan ONE("statm", S_IRUGO, proc_pid_statm), 3033631f9c18SAlexey Dobriyan REG("maps", S_IRUGO, proc_maps_operations), 303428a6d671SEric W. Biederman #ifdef CONFIG_NUMA 3035631f9c18SAlexey Dobriyan REG("numa_maps", S_IRUGO, proc_numa_maps_operations), 303628a6d671SEric W. Biederman #endif 3037631f9c18SAlexey Dobriyan REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations), 3038631f9c18SAlexey Dobriyan LNK("cwd", proc_cwd_link), 3039631f9c18SAlexey Dobriyan LNK("root", proc_root_link), 3040631f9c18SAlexey Dobriyan LNK("exe", proc_exe_link), 3041631f9c18SAlexey Dobriyan REG("mounts", S_IRUGO, proc_mounts_operations), 3042631f9c18SAlexey Dobriyan REG("mountinfo", S_IRUGO, proc_mountinfo_operations), 30431e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR 3044631f9c18SAlexey Dobriyan REG("clear_refs", S_IWUSR, proc_clear_refs_operations), 3045631f9c18SAlexey Dobriyan REG("smaps", S_IRUGO, proc_smaps_operations), 3046ca6b0bf0SAl Viro REG("pagemap", S_IRUGO, proc_pagemap_operations), 304728a6d671SEric W. Biederman #endif 304828a6d671SEric W. Biederman #ifdef CONFIG_SECURITY 3049631f9c18SAlexey Dobriyan DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations), 305028a6d671SEric W. Biederman #endif 305128a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS 3052631f9c18SAlexey Dobriyan INF("wchan", S_IRUGO, proc_pid_wchan), 305328a6d671SEric W. Biederman #endif 30542ec220e2SKen Chen #ifdef CONFIG_STACKTRACE 3055a9712bc1SAl Viro ONE("stack", S_IRUGO, proc_pid_stack), 305628a6d671SEric W. Biederman #endif 305728a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS 3058631f9c18SAlexey Dobriyan INF("schedstat", S_IRUGO, proc_pid_schedstat), 305928a6d671SEric W. Biederman #endif 30609745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP 3061631f9c18SAlexey Dobriyan REG("latency", S_IRUGO, proc_lstats_operations), 30629745512cSArjan van de Ven #endif 30638793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET 3064631f9c18SAlexey Dobriyan REG("cpuset", S_IRUGO, proc_cpuset_operations), 306528a6d671SEric W. Biederman #endif 3066a424316cSPaul Menage #ifdef CONFIG_CGROUPS 3067631f9c18SAlexey Dobriyan REG("cgroup", S_IRUGO, proc_cgroup_operations), 3068a424316cSPaul Menage #endif 3069631f9c18SAlexey Dobriyan INF("oom_score", S_IRUGO, proc_oom_score), 3070631f9c18SAlexey Dobriyan REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations), 3071a63d83f4SDavid Rientjes REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations), 307228a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL 3073631f9c18SAlexey Dobriyan REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations), 307426ec3c64SAl Viro REG("sessionid", S_IRUGO, proc_sessionid_operations), 307528a6d671SEric W. Biederman #endif 3076f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION 3077631f9c18SAlexey Dobriyan REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations), 3078f4f154fdSAkinobu Mita #endif 3079297c5d92SAndrea Righi #ifdef CONFIG_TASK_IO_ACCOUNTING 30801d1221f3SVasiliy Kulikov INF("io", S_IRUSR, proc_tid_io_accounting), 3081297c5d92SAndrea Righi #endif 3082f133eccaSChris Metcalf #ifdef CONFIG_HARDWALL 3083f133eccaSChris Metcalf INF("hardwall", S_IRUGO, proc_pid_hardwall), 3084f133eccaSChris Metcalf #endif 308528a6d671SEric W. Biederman }; 308628a6d671SEric W. Biederman 308728a6d671SEric W. Biederman static int proc_tid_base_readdir(struct file * filp, 308828a6d671SEric W. Biederman void * dirent, filldir_t filldir) 308928a6d671SEric W. Biederman { 309028a6d671SEric W. Biederman return proc_pident_readdir(filp,dirent,filldir, 309128a6d671SEric W. Biederman tid_base_stuff,ARRAY_SIZE(tid_base_stuff)); 309228a6d671SEric W. Biederman } 309328a6d671SEric W. Biederman 309428a6d671SEric W. Biederman static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){ 30957bcd6b0eSEric W. Biederman return proc_pident_lookup(dir, dentry, 30967bcd6b0eSEric W. Biederman tid_base_stuff, ARRAY_SIZE(tid_base_stuff)); 309728a6d671SEric W. Biederman } 309828a6d671SEric W. Biederman 309900977a59SArjan van de Ven static const struct file_operations proc_tid_base_operations = { 310028a6d671SEric W. Biederman .read = generic_read_dir, 310128a6d671SEric W. Biederman .readdir = proc_tid_base_readdir, 31026038f373SArnd Bergmann .llseek = default_llseek, 310328a6d671SEric W. Biederman }; 310428a6d671SEric W. Biederman 3105c5ef1c42SArjan van de Ven static const struct inode_operations proc_tid_base_inode_operations = { 310628a6d671SEric W. Biederman .lookup = proc_tid_base_lookup, 310728a6d671SEric W. Biederman .getattr = pid_getattr, 310828a6d671SEric W. Biederman .setattr = proc_setattr, 310928a6d671SEric W. Biederman }; 311028a6d671SEric W. Biederman 3111444ceed8SEric W. Biederman static struct dentry *proc_task_instantiate(struct inode *dir, 3112c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 3113444ceed8SEric W. Biederman { 3114444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-ENOENT); 3115444ceed8SEric W. Biederman struct inode *inode; 311661a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 3117444ceed8SEric W. Biederman 3118444ceed8SEric W. Biederman if (!inode) 3119444ceed8SEric W. Biederman goto out; 3120444ceed8SEric W. Biederman inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO; 3121444ceed8SEric W. Biederman inode->i_op = &proc_tid_base_inode_operations; 3122444ceed8SEric W. Biederman inode->i_fop = &proc_tid_base_operations; 3123444ceed8SEric W. Biederman inode->i_flags|=S_IMMUTABLE; 3124aed54175SVegard Nossum 3125bfe86848SMiklos Szeredi set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff, 3126bfe86848SMiklos Szeredi ARRAY_SIZE(tid_base_stuff))); 3127444ceed8SEric W. Biederman 3128fb045adbSNick Piggin d_set_d_op(dentry, &pid_dentry_operations); 3129444ceed8SEric W. Biederman 3130444ceed8SEric W. Biederman d_add(dentry, inode); 3131444ceed8SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 3132444ceed8SEric W. Biederman if (pid_revalidate(dentry, NULL)) 3133444ceed8SEric W. Biederman error = NULL; 3134444ceed8SEric W. Biederman out: 3135444ceed8SEric W. Biederman return error; 3136444ceed8SEric W. Biederman } 3137444ceed8SEric W. Biederman 313828a6d671SEric W. Biederman static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd) 313928a6d671SEric W. Biederman { 314028a6d671SEric W. Biederman struct dentry *result = ERR_PTR(-ENOENT); 314128a6d671SEric W. Biederman struct task_struct *task; 314228a6d671SEric W. Biederman struct task_struct *leader = get_proc_task(dir); 314328a6d671SEric W. Biederman unsigned tid; 3144b488893aSPavel Emelyanov struct pid_namespace *ns; 314528a6d671SEric W. Biederman 314628a6d671SEric W. Biederman if (!leader) 314728a6d671SEric W. Biederman goto out_no_task; 314828a6d671SEric W. Biederman 314928a6d671SEric W. Biederman tid = name_to_int(dentry); 315028a6d671SEric W. Biederman if (tid == ~0U) 315128a6d671SEric W. Biederman goto out; 315228a6d671SEric W. Biederman 3153b488893aSPavel Emelyanov ns = dentry->d_sb->s_fs_info; 315428a6d671SEric W. Biederman rcu_read_lock(); 3155b488893aSPavel Emelyanov task = find_task_by_pid_ns(tid, ns); 315628a6d671SEric W. Biederman if (task) 315728a6d671SEric W. Biederman get_task_struct(task); 315828a6d671SEric W. Biederman rcu_read_unlock(); 315928a6d671SEric W. Biederman if (!task) 316028a6d671SEric W. Biederman goto out; 3161bac0abd6SPavel Emelyanov if (!same_thread_group(leader, task)) 316228a6d671SEric W. Biederman goto out_drop_task; 316328a6d671SEric W. Biederman 3164444ceed8SEric W. Biederman result = proc_task_instantiate(dir, dentry, task, NULL); 316528a6d671SEric W. Biederman out_drop_task: 316628a6d671SEric W. Biederman put_task_struct(task); 316728a6d671SEric W. Biederman out: 316828a6d671SEric W. Biederman put_task_struct(leader); 316928a6d671SEric W. Biederman out_no_task: 317028a6d671SEric W. Biederman return result; 317128a6d671SEric W. Biederman } 317228a6d671SEric W. Biederman 317328a6d671SEric W. Biederman /* 31740bc58a91SEric W. Biederman * Find the first tid of a thread group to return to user space. 31750bc58a91SEric W. Biederman * 31760bc58a91SEric W. Biederman * Usually this is just the thread group leader, but if the users 31770bc58a91SEric W. Biederman * buffer was too small or there was a seek into the middle of the 31780bc58a91SEric W. Biederman * directory we have more work todo. 31790bc58a91SEric W. Biederman * 31800bc58a91SEric W. Biederman * In the case of a short read we start with find_task_by_pid. 31810bc58a91SEric W. Biederman * 31820bc58a91SEric W. Biederman * In the case of a seek we start with the leader and walk nr 31830bc58a91SEric W. Biederman * threads past it. 31840bc58a91SEric W. Biederman */ 3185cc288738SEric W. Biederman static struct task_struct *first_tid(struct task_struct *leader, 3186b488893aSPavel Emelyanov int tid, int nr, struct pid_namespace *ns) 31870bc58a91SEric W. Biederman { 3188a872ff0cSOleg Nesterov struct task_struct *pos; 31890bc58a91SEric W. Biederman 3190cc288738SEric W. Biederman rcu_read_lock(); 31910bc58a91SEric W. Biederman /* Attempt to start with the pid of a thread */ 31920bc58a91SEric W. Biederman if (tid && (nr > 0)) { 3193b488893aSPavel Emelyanov pos = find_task_by_pid_ns(tid, ns); 3194a872ff0cSOleg Nesterov if (pos && (pos->group_leader == leader)) 3195a872ff0cSOleg Nesterov goto found; 31960bc58a91SEric W. Biederman } 31970bc58a91SEric W. Biederman 31980bc58a91SEric W. Biederman /* If nr exceeds the number of threads there is nothing todo */ 31990bc58a91SEric W. Biederman pos = NULL; 3200a872ff0cSOleg Nesterov if (nr && nr >= get_nr_threads(leader)) 3201a872ff0cSOleg Nesterov goto out; 3202a872ff0cSOleg Nesterov 3203a872ff0cSOleg Nesterov /* If we haven't found our starting place yet start 3204a872ff0cSOleg Nesterov * with the leader and walk nr threads forward. 3205a872ff0cSOleg Nesterov */ 3206a872ff0cSOleg Nesterov for (pos = leader; nr > 0; --nr) { 3207a872ff0cSOleg Nesterov pos = next_thread(pos); 3208a872ff0cSOleg Nesterov if (pos == leader) { 3209a872ff0cSOleg Nesterov pos = NULL; 3210a872ff0cSOleg Nesterov goto out; 3211a872ff0cSOleg Nesterov } 3212a872ff0cSOleg Nesterov } 3213a872ff0cSOleg Nesterov found: 3214a872ff0cSOleg Nesterov get_task_struct(pos); 3215a872ff0cSOleg Nesterov out: 3216cc288738SEric W. Biederman rcu_read_unlock(); 32170bc58a91SEric W. Biederman return pos; 32180bc58a91SEric W. Biederman } 32190bc58a91SEric W. Biederman 32200bc58a91SEric W. Biederman /* 32210bc58a91SEric W. Biederman * Find the next thread in the thread list. 32220bc58a91SEric W. Biederman * Return NULL if there is an error or no next thread. 32230bc58a91SEric W. Biederman * 32240bc58a91SEric W. Biederman * The reference to the input task_struct is released. 32250bc58a91SEric W. Biederman */ 32260bc58a91SEric W. Biederman static struct task_struct *next_tid(struct task_struct *start) 32270bc58a91SEric W. Biederman { 3228c1df7fb8SOleg Nesterov struct task_struct *pos = NULL; 3229cc288738SEric W. Biederman rcu_read_lock(); 3230c1df7fb8SOleg Nesterov if (pid_alive(start)) { 32310bc58a91SEric W. Biederman pos = next_thread(start); 3232c1df7fb8SOleg Nesterov if (thread_group_leader(pos)) 32330bc58a91SEric W. Biederman pos = NULL; 3234c1df7fb8SOleg Nesterov else 3235c1df7fb8SOleg Nesterov get_task_struct(pos); 3236c1df7fb8SOleg Nesterov } 3237cc288738SEric W. Biederman rcu_read_unlock(); 32380bc58a91SEric W. Biederman put_task_struct(start); 32390bc58a91SEric W. Biederman return pos; 32400bc58a91SEric W. Biederman } 32410bc58a91SEric W. Biederman 324261a28784SEric W. Biederman static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir, 324361a28784SEric W. Biederman struct task_struct *task, int tid) 324461a28784SEric W. Biederman { 324561a28784SEric W. Biederman char name[PROC_NUMBUF]; 324661a28784SEric W. Biederman int len = snprintf(name, sizeof(name), "%d", tid); 324761a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, name, len, 324861a28784SEric W. Biederman proc_task_instantiate, task, NULL); 324961a28784SEric W. Biederman } 325061a28784SEric W. Biederman 32511da177e4SLinus Torvalds /* for the /proc/TGID/task/ directories */ 32521da177e4SLinus Torvalds static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir) 32531da177e4SLinus Torvalds { 32542fddfeefSJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 32551da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 32567d895244SGuillaume Chazarain struct task_struct *leader = NULL; 32570bc58a91SEric W. Biederman struct task_struct *task; 32581da177e4SLinus Torvalds int retval = -ENOENT; 32591da177e4SLinus Torvalds ino_t ino; 32600bc58a91SEric W. Biederman int tid; 3261b488893aSPavel Emelyanov struct pid_namespace *ns; 32621da177e4SLinus Torvalds 32637d895244SGuillaume Chazarain task = get_proc_task(inode); 32647d895244SGuillaume Chazarain if (!task) 32657d895244SGuillaume Chazarain goto out_no_task; 32667d895244SGuillaume Chazarain rcu_read_lock(); 32677d895244SGuillaume Chazarain if (pid_alive(task)) { 32687d895244SGuillaume Chazarain leader = task->group_leader; 32697d895244SGuillaume Chazarain get_task_struct(leader); 32707d895244SGuillaume Chazarain } 32717d895244SGuillaume Chazarain rcu_read_unlock(); 32727d895244SGuillaume Chazarain put_task_struct(task); 327399f89551SEric W. Biederman if (!leader) 327499f89551SEric W. Biederman goto out_no_task; 32751da177e4SLinus Torvalds retval = 0; 32761da177e4SLinus Torvalds 3277ee568b25SLinus Torvalds switch ((unsigned long)filp->f_pos) { 32781da177e4SLinus Torvalds case 0: 32791da177e4SLinus Torvalds ino = inode->i_ino; 3280ee6f779bSZhang Le if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) < 0) 32811da177e4SLinus Torvalds goto out; 3282ee6f779bSZhang Le filp->f_pos++; 32831da177e4SLinus Torvalds /* fall through */ 32841da177e4SLinus Torvalds case 1: 32851da177e4SLinus Torvalds ino = parent_ino(dentry); 3286ee6f779bSZhang Le if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) < 0) 32871da177e4SLinus Torvalds goto out; 3288ee6f779bSZhang Le filp->f_pos++; 32891da177e4SLinus Torvalds /* fall through */ 32901da177e4SLinus Torvalds } 32911da177e4SLinus Torvalds 32920bc58a91SEric W. Biederman /* f_version caches the tgid value that the last readdir call couldn't 32930bc58a91SEric W. Biederman * return. lseek aka telldir automagically resets f_version to 0. 32940bc58a91SEric W. Biederman */ 3295b488893aSPavel Emelyanov ns = filp->f_dentry->d_sb->s_fs_info; 32962b47c361SMathieu Desnoyers tid = (int)filp->f_version; 32970bc58a91SEric W. Biederman filp->f_version = 0; 3298ee6f779bSZhang Le for (task = first_tid(leader, tid, filp->f_pos - 2, ns); 32990bc58a91SEric W. Biederman task; 3300ee6f779bSZhang Le task = next_tid(task), filp->f_pos++) { 3301b488893aSPavel Emelyanov tid = task_pid_nr_ns(task, ns); 330261a28784SEric W. Biederman if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) { 33030bc58a91SEric W. Biederman /* returning this tgid failed, save it as the first 33040bc58a91SEric W. Biederman * pid for the next readir call */ 33052b47c361SMathieu Desnoyers filp->f_version = (u64)tid; 33060bc58a91SEric W. Biederman put_task_struct(task); 33071da177e4SLinus Torvalds break; 33080bc58a91SEric W. Biederman } 33091da177e4SLinus Torvalds } 33101da177e4SLinus Torvalds out: 331199f89551SEric W. Biederman put_task_struct(leader); 331299f89551SEric W. Biederman out_no_task: 33131da177e4SLinus Torvalds return retval; 33141da177e4SLinus Torvalds } 33156e66b52bSEric W. Biederman 33166e66b52bSEric W. Biederman static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) 33176e66b52bSEric W. Biederman { 33186e66b52bSEric W. Biederman struct inode *inode = dentry->d_inode; 331999f89551SEric W. Biederman struct task_struct *p = get_proc_task(inode); 33206e66b52bSEric W. Biederman generic_fillattr(inode, stat); 33216e66b52bSEric W. Biederman 332299f89551SEric W. Biederman if (p) { 332399f89551SEric W. Biederman stat->nlink += get_nr_threads(p); 332499f89551SEric W. Biederman put_task_struct(p); 33256e66b52bSEric W. Biederman } 33266e66b52bSEric W. Biederman 33276e66b52bSEric W. Biederman return 0; 33286e66b52bSEric W. Biederman } 332928a6d671SEric W. Biederman 3330c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations = { 333128a6d671SEric W. Biederman .lookup = proc_task_lookup, 333228a6d671SEric W. Biederman .getattr = proc_task_getattr, 333328a6d671SEric W. Biederman .setattr = proc_setattr, 333428a6d671SEric W. Biederman }; 333528a6d671SEric W. Biederman 333600977a59SArjan van de Ven static const struct file_operations proc_task_operations = { 333728a6d671SEric W. Biederman .read = generic_read_dir, 333828a6d671SEric W. Biederman .readdir = proc_task_readdir, 33396038f373SArnd Bergmann .llseek = default_llseek, 334028a6d671SEric W. Biederman }; 3341