11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/fs/proc/base.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 1991, 1992 Linus Torvalds 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * proc base directory handling functions 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * 1999, Al Viro. Rewritten. Now it covers the whole per-process part. 91da177e4SLinus Torvalds * Instead of using magical inumbers to determine the kind of object 101da177e4SLinus Torvalds * we allocate and fill in-core inodes upon lookup. They don't even 111da177e4SLinus Torvalds * go into icache. We cache the reference to task_struct upon lookup too. 121da177e4SLinus Torvalds * Eventually it should become a filesystem in its own. We don't use the 131da177e4SLinus Torvalds * rest of procfs anymore. 14e070ad49SMauricio Lin * 15e070ad49SMauricio Lin * 16e070ad49SMauricio Lin * Changelog: 17e070ad49SMauricio Lin * 17-Jan-2005 18e070ad49SMauricio Lin * Allan Bezerra 19e070ad49SMauricio Lin * Bruna Moreira <bruna.moreira@indt.org.br> 20e070ad49SMauricio Lin * Edjard Mota <edjard.mota@indt.org.br> 21e070ad49SMauricio Lin * Ilias Biris <ilias.biris@indt.org.br> 22e070ad49SMauricio Lin * Mauricio Lin <mauricio.lin@indt.org.br> 23e070ad49SMauricio Lin * 24e070ad49SMauricio Lin * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT 25e070ad49SMauricio Lin * 26e070ad49SMauricio Lin * A new process specific entry (smaps) included in /proc. It shows the 27e070ad49SMauricio Lin * size of rss for each memory area. The maps entry lacks information 28e070ad49SMauricio Lin * about physical memory size (rss) for each mapped file, i.e., 29e070ad49SMauricio Lin * rss information for executables and library files. 30e070ad49SMauricio Lin * This additional information is useful for any tools that need to know 31e070ad49SMauricio Lin * about physical memory consumption for a process specific library. 32e070ad49SMauricio Lin * 33e070ad49SMauricio Lin * Changelog: 34e070ad49SMauricio Lin * 21-Feb-2005 35e070ad49SMauricio Lin * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT 36e070ad49SMauricio Lin * Pud inclusion in the page table walking. 37e070ad49SMauricio Lin * 38e070ad49SMauricio Lin * ChangeLog: 39e070ad49SMauricio Lin * 10-Mar-2005 40e070ad49SMauricio Lin * 10LE Instituto Nokia de Tecnologia - INdT: 41e070ad49SMauricio Lin * A better way to walks through the page table as suggested by Hugh Dickins. 42e070ad49SMauricio Lin * 43e070ad49SMauricio Lin * Simo Piiroinen <simo.piiroinen@nokia.com>: 44e070ad49SMauricio Lin * Smaps information related to shared, private, clean and dirty pages. 45e070ad49SMauricio Lin * 46e070ad49SMauricio Lin * Paul Mundt <paul.mundt@nokia.com>: 47e070ad49SMauricio Lin * Overall revision about smaps. 481da177e4SLinus Torvalds */ 491da177e4SLinus Torvalds 501da177e4SLinus Torvalds #include <asm/uaccess.h> 511da177e4SLinus Torvalds 521da177e4SLinus Torvalds #include <linux/errno.h> 531da177e4SLinus Torvalds #include <linux/time.h> 541da177e4SLinus Torvalds #include <linux/proc_fs.h> 551da177e4SLinus Torvalds #include <linux/stat.h> 565995477aSAndrea Righi #include <linux/task_io_accounting_ops.h> 571da177e4SLinus Torvalds #include <linux/init.h> 5816f7e0feSRandy Dunlap #include <linux/capability.h> 591da177e4SLinus Torvalds #include <linux/file.h> 609f3acc31SAl Viro #include <linux/fdtable.h> 611da177e4SLinus Torvalds #include <linux/string.h> 621da177e4SLinus Torvalds #include <linux/seq_file.h> 631da177e4SLinus Torvalds #include <linux/namei.h> 646b3286edSKirill Korotaev #include <linux/mnt_namespace.h> 651da177e4SLinus Torvalds #include <linux/mm.h> 66b835996fSDipankar Sarma #include <linux/rcupdate.h> 671da177e4SLinus Torvalds #include <linux/kallsyms.h> 68*2ec220e2SKen Chen #include <linux/stacktrace.h> 69d85f50d5SNeil Horman #include <linux/resource.h> 705096add8SKees Cook #include <linux/module.h> 711da177e4SLinus Torvalds #include <linux/mount.h> 721da177e4SLinus Torvalds #include <linux/security.h> 731da177e4SLinus Torvalds #include <linux/ptrace.h> 740d094efeSRoland McGrath #include <linux/tracehook.h> 75a424316cSPaul Menage #include <linux/cgroup.h> 761da177e4SLinus Torvalds #include <linux/cpuset.h> 771da177e4SLinus Torvalds #include <linux/audit.h> 785addc5ddSAl Viro #include <linux/poll.h> 791651e14eSSerge E. Hallyn #include <linux/nsproxy.h> 808ac773b4SAlexey Dobriyan #include <linux/oom.h> 813cb4a0bbSKawai, Hidehiro #include <linux/elf.h> 8260347f67SPavel Emelyanov #include <linux/pid_namespace.h> 831da177e4SLinus Torvalds #include "internal.h" 841da177e4SLinus Torvalds 850f2fe20fSEric W. Biederman /* NOTE: 860f2fe20fSEric W. Biederman * Implementing inode permission operations in /proc is almost 870f2fe20fSEric W. Biederman * certainly an error. Permission checks need to happen during 880f2fe20fSEric W. Biederman * each system call not at open time. The reason is that most of 890f2fe20fSEric W. Biederman * what we wish to check for permissions in /proc varies at runtime. 900f2fe20fSEric W. Biederman * 910f2fe20fSEric W. Biederman * The classic example of a problem is opening file descriptors 920f2fe20fSEric W. Biederman * in /proc for a task before it execs a suid executable. 930f2fe20fSEric W. Biederman */ 940f2fe20fSEric W. Biederman 951da177e4SLinus Torvalds struct pid_entry { 961da177e4SLinus Torvalds char *name; 97c5141e6dSEric Dumazet int len; 981da177e4SLinus Torvalds mode_t mode; 99c5ef1c42SArjan van de Ven const struct inode_operations *iop; 10000977a59SArjan van de Ven const struct file_operations *fop; 10120cdc894SEric W. Biederman union proc_op op; 1021da177e4SLinus Torvalds }; 1031da177e4SLinus Torvalds 10461a28784SEric W. Biederman #define NOD(NAME, MODE, IOP, FOP, OP) { \ 10520cdc894SEric W. Biederman .name = (NAME), \ 106c5141e6dSEric Dumazet .len = sizeof(NAME) - 1, \ 10720cdc894SEric W. Biederman .mode = MODE, \ 10820cdc894SEric W. Biederman .iop = IOP, \ 10920cdc894SEric W. Biederman .fop = FOP, \ 11020cdc894SEric W. Biederman .op = OP, \ 11120cdc894SEric W. Biederman } 11220cdc894SEric W. Biederman 113631f9c18SAlexey Dobriyan #define DIR(NAME, MODE, iops, fops) \ 114631f9c18SAlexey Dobriyan NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} ) 115631f9c18SAlexey Dobriyan #define LNK(NAME, get_link) \ 11661a28784SEric W. Biederman NOD(NAME, (S_IFLNK|S_IRWXUGO), \ 11720cdc894SEric W. Biederman &proc_pid_link_inode_operations, NULL, \ 118631f9c18SAlexey Dobriyan { .proc_get_link = get_link } ) 119631f9c18SAlexey Dobriyan #define REG(NAME, MODE, fops) \ 120631f9c18SAlexey Dobriyan NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {}) 121631f9c18SAlexey Dobriyan #define INF(NAME, MODE, read) \ 12261a28784SEric W. Biederman NOD(NAME, (S_IFREG|(MODE)), \ 12320cdc894SEric W. Biederman NULL, &proc_info_file_operations, \ 124631f9c18SAlexey Dobriyan { .proc_read = read } ) 125631f9c18SAlexey Dobriyan #define ONE(NAME, MODE, show) \ 126be614086SEric W. Biederman NOD(NAME, (S_IFREG|(MODE)), \ 127be614086SEric W. Biederman NULL, &proc_single_file_operations, \ 128631f9c18SAlexey Dobriyan { .proc_show = show } ) 1291da177e4SLinus Torvalds 130aed54175SVegard Nossum /* 131aed54175SVegard Nossum * Count the number of hardlinks for the pid_entry table, excluding the . 132aed54175SVegard Nossum * and .. links. 133aed54175SVegard Nossum */ 134aed54175SVegard Nossum static unsigned int pid_entry_count_dirs(const struct pid_entry *entries, 135aed54175SVegard Nossum unsigned int n) 136aed54175SVegard Nossum { 137aed54175SVegard Nossum unsigned int i; 138aed54175SVegard Nossum unsigned int count; 139aed54175SVegard Nossum 140aed54175SVegard Nossum count = 0; 141aed54175SVegard Nossum for (i = 0; i < n; ++i) { 142aed54175SVegard Nossum if (S_ISDIR(entries[i].mode)) 143aed54175SVegard Nossum ++count; 144aed54175SVegard Nossum } 145aed54175SVegard Nossum 146aed54175SVegard Nossum return count; 147aed54175SVegard Nossum } 148aed54175SVegard Nossum 1490494f6ecSMiklos Szeredi static struct fs_struct *get_fs_struct(struct task_struct *task) 1501da177e4SLinus Torvalds { 1511da177e4SLinus Torvalds struct fs_struct *fs; 1520494f6ecSMiklos Szeredi task_lock(task); 1530494f6ecSMiklos Szeredi fs = task->fs; 1541da177e4SLinus Torvalds if(fs) 1551da177e4SLinus Torvalds atomic_inc(&fs->count); 1560494f6ecSMiklos Szeredi task_unlock(task); 1570494f6ecSMiklos Szeredi return fs; 1580494f6ecSMiklos Szeredi } 1590494f6ecSMiklos Szeredi 16099f89551SEric W. Biederman static int get_nr_threads(struct task_struct *tsk) 16199f89551SEric W. Biederman { 16299f89551SEric W. Biederman unsigned long flags; 16399f89551SEric W. Biederman int count = 0; 16499f89551SEric W. Biederman 16599f89551SEric W. Biederman if (lock_task_sighand(tsk, &flags)) { 16699f89551SEric W. Biederman count = atomic_read(&tsk->signal->count); 16799f89551SEric W. Biederman unlock_task_sighand(tsk, &flags); 16899f89551SEric W. Biederman } 16999f89551SEric W. Biederman return count; 17099f89551SEric W. Biederman } 17199f89551SEric W. Biederman 1723dcd25f3SJan Blunck static int proc_cwd_link(struct inode *inode, struct path *path) 1730494f6ecSMiklos Szeredi { 17499f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 17599f89551SEric W. Biederman struct fs_struct *fs = NULL; 1760494f6ecSMiklos Szeredi int result = -ENOENT; 17799f89551SEric W. Biederman 17899f89551SEric W. Biederman if (task) { 17999f89551SEric W. Biederman fs = get_fs_struct(task); 18099f89551SEric W. Biederman put_task_struct(task); 18199f89551SEric W. Biederman } 1821da177e4SLinus Torvalds if (fs) { 1831da177e4SLinus Torvalds read_lock(&fs->lock); 1843dcd25f3SJan Blunck *path = fs->pwd; 1853dcd25f3SJan Blunck path_get(&fs->pwd); 1861da177e4SLinus Torvalds read_unlock(&fs->lock); 1871da177e4SLinus Torvalds result = 0; 1881da177e4SLinus Torvalds put_fs_struct(fs); 1891da177e4SLinus Torvalds } 1901da177e4SLinus Torvalds return result; 1911da177e4SLinus Torvalds } 1921da177e4SLinus Torvalds 1933dcd25f3SJan Blunck static int proc_root_link(struct inode *inode, struct path *path) 1941da177e4SLinus Torvalds { 19599f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 19699f89551SEric W. Biederman struct fs_struct *fs = NULL; 1971da177e4SLinus Torvalds int result = -ENOENT; 19899f89551SEric W. Biederman 19999f89551SEric W. Biederman if (task) { 20099f89551SEric W. Biederman fs = get_fs_struct(task); 20199f89551SEric W. Biederman put_task_struct(task); 20299f89551SEric W. Biederman } 2031da177e4SLinus Torvalds if (fs) { 2041da177e4SLinus Torvalds read_lock(&fs->lock); 2053dcd25f3SJan Blunck *path = fs->root; 2063dcd25f3SJan Blunck path_get(&fs->root); 2071da177e4SLinus Torvalds read_unlock(&fs->lock); 2081da177e4SLinus Torvalds result = 0; 2091da177e4SLinus Torvalds put_fs_struct(fs); 2101da177e4SLinus Torvalds } 2111da177e4SLinus Torvalds return result; 2121da177e4SLinus Torvalds } 2131da177e4SLinus Torvalds 214638fa202SRoland McGrath /* 215638fa202SRoland McGrath * Return zero if current may access user memory in @task, -error if not. 216638fa202SRoland McGrath */ 217638fa202SRoland McGrath static int check_mem_permission(struct task_struct *task) 218638fa202SRoland McGrath { 219638fa202SRoland McGrath /* 220638fa202SRoland McGrath * A task can always look at itself, in case it chooses 221638fa202SRoland McGrath * to use system calls instead of load instructions. 222638fa202SRoland McGrath */ 223638fa202SRoland McGrath if (task == current) 224638fa202SRoland McGrath return 0; 225638fa202SRoland McGrath 226638fa202SRoland McGrath /* 227638fa202SRoland McGrath * If current is actively ptrace'ing, and would also be 228638fa202SRoland McGrath * permitted to freshly attach with ptrace now, permit it. 229638fa202SRoland McGrath */ 2300d094efeSRoland McGrath if (task_is_stopped_or_traced(task)) { 2310d094efeSRoland McGrath int match; 2320d094efeSRoland McGrath rcu_read_lock(); 2330d094efeSRoland McGrath match = (tracehook_tracer_task(task) == current); 2340d094efeSRoland McGrath rcu_read_unlock(); 2350d094efeSRoland McGrath if (match && ptrace_may_access(task, PTRACE_MODE_ATTACH)) 236638fa202SRoland McGrath return 0; 2370d094efeSRoland McGrath } 238638fa202SRoland McGrath 239638fa202SRoland McGrath /* 240638fa202SRoland McGrath * Noone else is allowed. 241638fa202SRoland McGrath */ 242638fa202SRoland McGrath return -EPERM; 243638fa202SRoland McGrath } 2441da177e4SLinus Torvalds 245831830b5SAl Viro struct mm_struct *mm_for_maps(struct task_struct *task) 246831830b5SAl Viro { 247831830b5SAl Viro struct mm_struct *mm = get_task_mm(task); 248831830b5SAl Viro if (!mm) 249831830b5SAl Viro return NULL; 250831830b5SAl Viro down_read(&mm->mmap_sem); 251831830b5SAl Viro task_lock(task); 252831830b5SAl Viro if (task->mm != mm) 253831830b5SAl Viro goto out; 254006ebb40SStephen Smalley if (task->mm != current->mm && 255006ebb40SStephen Smalley __ptrace_may_access(task, PTRACE_MODE_READ) < 0) 256831830b5SAl Viro goto out; 257831830b5SAl Viro task_unlock(task); 258831830b5SAl Viro return mm; 259831830b5SAl Viro out: 260831830b5SAl Viro task_unlock(task); 261831830b5SAl Viro up_read(&mm->mmap_sem); 262831830b5SAl Viro mmput(mm); 263831830b5SAl Viro return NULL; 264831830b5SAl Viro } 265831830b5SAl Viro 2661da177e4SLinus Torvalds static int proc_pid_cmdline(struct task_struct *task, char * buffer) 2671da177e4SLinus Torvalds { 2681da177e4SLinus Torvalds int res = 0; 2691da177e4SLinus Torvalds unsigned int len; 2701da177e4SLinus Torvalds struct mm_struct *mm = get_task_mm(task); 2711da177e4SLinus Torvalds if (!mm) 2721da177e4SLinus Torvalds goto out; 2731da177e4SLinus Torvalds if (!mm->arg_end) 2741da177e4SLinus Torvalds goto out_mm; /* Shh! No looking before we're done */ 2751da177e4SLinus Torvalds 2761da177e4SLinus Torvalds len = mm->arg_end - mm->arg_start; 2771da177e4SLinus Torvalds 2781da177e4SLinus Torvalds if (len > PAGE_SIZE) 2791da177e4SLinus Torvalds len = PAGE_SIZE; 2801da177e4SLinus Torvalds 2811da177e4SLinus Torvalds res = access_process_vm(task, mm->arg_start, buffer, len, 0); 2821da177e4SLinus Torvalds 2831da177e4SLinus Torvalds // If the nul at the end of args has been overwritten, then 2841da177e4SLinus Torvalds // assume application is using setproctitle(3). 2851da177e4SLinus Torvalds if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) { 2861da177e4SLinus Torvalds len = strnlen(buffer, res); 2871da177e4SLinus Torvalds if (len < res) { 2881da177e4SLinus Torvalds res = len; 2891da177e4SLinus Torvalds } else { 2901da177e4SLinus Torvalds len = mm->env_end - mm->env_start; 2911da177e4SLinus Torvalds if (len > PAGE_SIZE - res) 2921da177e4SLinus Torvalds len = PAGE_SIZE - res; 2931da177e4SLinus Torvalds res += access_process_vm(task, mm->env_start, buffer+res, len, 0); 2941da177e4SLinus Torvalds res = strnlen(buffer, res); 2951da177e4SLinus Torvalds } 2961da177e4SLinus Torvalds } 2971da177e4SLinus Torvalds out_mm: 2981da177e4SLinus Torvalds mmput(mm); 2991da177e4SLinus Torvalds out: 3001da177e4SLinus Torvalds return res; 3011da177e4SLinus Torvalds } 3021da177e4SLinus Torvalds 3031da177e4SLinus Torvalds static int proc_pid_auxv(struct task_struct *task, char *buffer) 3041da177e4SLinus Torvalds { 3051da177e4SLinus Torvalds int res = 0; 3061da177e4SLinus Torvalds struct mm_struct *mm = get_task_mm(task); 3071da177e4SLinus Torvalds if (mm) { 3081da177e4SLinus Torvalds unsigned int nwords = 0; 3091da177e4SLinus Torvalds do 3101da177e4SLinus Torvalds nwords += 2; 3111da177e4SLinus Torvalds while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */ 3121da177e4SLinus Torvalds res = nwords * sizeof(mm->saved_auxv[0]); 3131da177e4SLinus Torvalds if (res > PAGE_SIZE) 3141da177e4SLinus Torvalds res = PAGE_SIZE; 3151da177e4SLinus Torvalds memcpy(buffer, mm->saved_auxv, res); 3161da177e4SLinus Torvalds mmput(mm); 3171da177e4SLinus Torvalds } 3181da177e4SLinus Torvalds return res; 3191da177e4SLinus Torvalds } 3201da177e4SLinus Torvalds 3211da177e4SLinus Torvalds 3221da177e4SLinus Torvalds #ifdef CONFIG_KALLSYMS 3231da177e4SLinus Torvalds /* 3241da177e4SLinus Torvalds * Provides a wchan file via kallsyms in a proper one-value-per-file format. 3251da177e4SLinus Torvalds * Returns the resolved symbol. If that fails, simply return the address. 3261da177e4SLinus Torvalds */ 3271da177e4SLinus Torvalds static int proc_pid_wchan(struct task_struct *task, char *buffer) 3281da177e4SLinus Torvalds { 329ffb45122SAlexey Dobriyan unsigned long wchan; 3309281aceaSTejun Heo char symname[KSYM_NAME_LEN]; 3311da177e4SLinus Torvalds 3321da177e4SLinus Torvalds wchan = get_wchan(task); 3331da177e4SLinus Torvalds 3349d65cb4aSAlexey Dobriyan if (lookup_symbol_name(wchan, symname) < 0) 3351da177e4SLinus Torvalds return sprintf(buffer, "%lu", wchan); 3369d65cb4aSAlexey Dobriyan else 3379d65cb4aSAlexey Dobriyan return sprintf(buffer, "%s", symname); 3381da177e4SLinus Torvalds } 3391da177e4SLinus Torvalds #endif /* CONFIG_KALLSYMS */ 3401da177e4SLinus Torvalds 341*2ec220e2SKen Chen #ifdef CONFIG_STACKTRACE 342*2ec220e2SKen Chen 343*2ec220e2SKen Chen #define MAX_STACK_TRACE_DEPTH 64 344*2ec220e2SKen Chen 345*2ec220e2SKen Chen static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns, 346*2ec220e2SKen Chen struct pid *pid, struct task_struct *task) 347*2ec220e2SKen Chen { 348*2ec220e2SKen Chen struct stack_trace trace; 349*2ec220e2SKen Chen unsigned long *entries; 350*2ec220e2SKen Chen int i; 351*2ec220e2SKen Chen 352*2ec220e2SKen Chen entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL); 353*2ec220e2SKen Chen if (!entries) 354*2ec220e2SKen Chen return -ENOMEM; 355*2ec220e2SKen Chen 356*2ec220e2SKen Chen trace.nr_entries = 0; 357*2ec220e2SKen Chen trace.max_entries = MAX_STACK_TRACE_DEPTH; 358*2ec220e2SKen Chen trace.entries = entries; 359*2ec220e2SKen Chen trace.skip = 0; 360*2ec220e2SKen Chen save_stack_trace_tsk(task, &trace); 361*2ec220e2SKen Chen 362*2ec220e2SKen Chen for (i = 0; i < trace.nr_entries; i++) { 363*2ec220e2SKen Chen seq_printf(m, "[<%p>] %pS\n", 364*2ec220e2SKen Chen (void *)entries[i], (void *)entries[i]); 365*2ec220e2SKen Chen } 366*2ec220e2SKen Chen kfree(entries); 367*2ec220e2SKen Chen 368*2ec220e2SKen Chen return 0; 369*2ec220e2SKen Chen } 370*2ec220e2SKen Chen #endif 371*2ec220e2SKen Chen 3721da177e4SLinus Torvalds #ifdef CONFIG_SCHEDSTATS 3731da177e4SLinus Torvalds /* 3741da177e4SLinus Torvalds * Provides /proc/PID/schedstat 3751da177e4SLinus Torvalds */ 3761da177e4SLinus Torvalds static int proc_pid_schedstat(struct task_struct *task, char *buffer) 3771da177e4SLinus Torvalds { 378172ba844SBalbir Singh return sprintf(buffer, "%llu %llu %lu\n", 379826e08b0SIngo Molnar (unsigned long long)task->se.sum_exec_runtime, 380826e08b0SIngo Molnar (unsigned long long)task->sched_info.run_delay, 3812d72376bSIngo Molnar task->sched_info.pcount); 3821da177e4SLinus Torvalds } 3831da177e4SLinus Torvalds #endif 3841da177e4SLinus Torvalds 3859745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP 3869745512cSArjan van de Ven static int lstats_show_proc(struct seq_file *m, void *v) 3879745512cSArjan van de Ven { 3889745512cSArjan van de Ven int i; 38913d77c37SHiroshi Shimamoto struct inode *inode = m->private; 39013d77c37SHiroshi Shimamoto struct task_struct *task = get_proc_task(inode); 3919745512cSArjan van de Ven 39213d77c37SHiroshi Shimamoto if (!task) 39313d77c37SHiroshi Shimamoto return -ESRCH; 39413d77c37SHiroshi Shimamoto seq_puts(m, "Latency Top version : v0.1\n"); 3959745512cSArjan van de Ven for (i = 0; i < 32; i++) { 3969745512cSArjan van de Ven if (task->latency_record[i].backtrace[0]) { 3979745512cSArjan van de Ven int q; 3989745512cSArjan van de Ven seq_printf(m, "%i %li %li ", 3999745512cSArjan van de Ven task->latency_record[i].count, 4009745512cSArjan van de Ven task->latency_record[i].time, 4019745512cSArjan van de Ven task->latency_record[i].max); 4029745512cSArjan van de Ven for (q = 0; q < LT_BACKTRACEDEPTH; q++) { 4039c246247SHugh Dickins char sym[KSYM_SYMBOL_LEN]; 4049745512cSArjan van de Ven char *c; 4059745512cSArjan van de Ven if (!task->latency_record[i].backtrace[q]) 4069745512cSArjan van de Ven break; 4079745512cSArjan van de Ven if (task->latency_record[i].backtrace[q] == ULONG_MAX) 4089745512cSArjan van de Ven break; 4099745512cSArjan van de Ven sprint_symbol(sym, task->latency_record[i].backtrace[q]); 4109745512cSArjan van de Ven c = strchr(sym, '+'); 4119745512cSArjan van de Ven if (c) 4129745512cSArjan van de Ven *c = 0; 4139745512cSArjan van de Ven seq_printf(m, "%s ", sym); 4149745512cSArjan van de Ven } 4159745512cSArjan van de Ven seq_printf(m, "\n"); 4169745512cSArjan van de Ven } 4179745512cSArjan van de Ven 4189745512cSArjan van de Ven } 41913d77c37SHiroshi Shimamoto put_task_struct(task); 4209745512cSArjan van de Ven return 0; 4219745512cSArjan van de Ven } 4229745512cSArjan van de Ven 4239745512cSArjan van de Ven static int lstats_open(struct inode *inode, struct file *file) 4249745512cSArjan van de Ven { 42513d77c37SHiroshi Shimamoto return single_open(file, lstats_show_proc, inode); 426d6643d12SHiroshi Shimamoto } 427d6643d12SHiroshi Shimamoto 4289745512cSArjan van de Ven static ssize_t lstats_write(struct file *file, const char __user *buf, 4299745512cSArjan van de Ven size_t count, loff_t *offs) 4309745512cSArjan van de Ven { 43113d77c37SHiroshi Shimamoto struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 4329745512cSArjan van de Ven 43313d77c37SHiroshi Shimamoto if (!task) 43413d77c37SHiroshi Shimamoto return -ESRCH; 4359745512cSArjan van de Ven clear_all_latency_tracing(task); 43613d77c37SHiroshi Shimamoto put_task_struct(task); 4379745512cSArjan van de Ven 4389745512cSArjan van de Ven return count; 4399745512cSArjan van de Ven } 4409745512cSArjan van de Ven 4419745512cSArjan van de Ven static const struct file_operations proc_lstats_operations = { 4429745512cSArjan van de Ven .open = lstats_open, 4439745512cSArjan van de Ven .read = seq_read, 4449745512cSArjan van de Ven .write = lstats_write, 4459745512cSArjan van de Ven .llseek = seq_lseek, 44613d77c37SHiroshi Shimamoto .release = single_release, 4479745512cSArjan van de Ven }; 4489745512cSArjan van de Ven 4499745512cSArjan van de Ven #endif 4509745512cSArjan van de Ven 4511da177e4SLinus Torvalds /* The badness from the OOM killer */ 4521da177e4SLinus Torvalds unsigned long badness(struct task_struct *p, unsigned long uptime); 4531da177e4SLinus Torvalds static int proc_oom_score(struct task_struct *task, char *buffer) 4541da177e4SLinus Torvalds { 4551da177e4SLinus Torvalds unsigned long points; 4561da177e4SLinus Torvalds struct timespec uptime; 4571da177e4SLinus Torvalds 4581da177e4SLinus Torvalds do_posix_clock_monotonic_gettime(&uptime); 45919c5d45aSAlexey Dobriyan read_lock(&tasklist_lock); 4601da177e4SLinus Torvalds points = badness(task, uptime.tv_sec); 46119c5d45aSAlexey Dobriyan read_unlock(&tasklist_lock); 4621da177e4SLinus Torvalds return sprintf(buffer, "%lu\n", points); 4631da177e4SLinus Torvalds } 4641da177e4SLinus Torvalds 465d85f50d5SNeil Horman struct limit_names { 466d85f50d5SNeil Horman char *name; 467d85f50d5SNeil Horman char *unit; 468d85f50d5SNeil Horman }; 469d85f50d5SNeil Horman 470d85f50d5SNeil Horman static const struct limit_names lnames[RLIM_NLIMITS] = { 471d85f50d5SNeil Horman [RLIMIT_CPU] = {"Max cpu time", "ms"}, 472d85f50d5SNeil Horman [RLIMIT_FSIZE] = {"Max file size", "bytes"}, 473d85f50d5SNeil Horman [RLIMIT_DATA] = {"Max data size", "bytes"}, 474d85f50d5SNeil Horman [RLIMIT_STACK] = {"Max stack size", "bytes"}, 475d85f50d5SNeil Horman [RLIMIT_CORE] = {"Max core file size", "bytes"}, 476d85f50d5SNeil Horman [RLIMIT_RSS] = {"Max resident set", "bytes"}, 477d85f50d5SNeil Horman [RLIMIT_NPROC] = {"Max processes", "processes"}, 478d85f50d5SNeil Horman [RLIMIT_NOFILE] = {"Max open files", "files"}, 479d85f50d5SNeil Horman [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"}, 480d85f50d5SNeil Horman [RLIMIT_AS] = {"Max address space", "bytes"}, 481d85f50d5SNeil Horman [RLIMIT_LOCKS] = {"Max file locks", "locks"}, 482d85f50d5SNeil Horman [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"}, 483d85f50d5SNeil Horman [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"}, 484d85f50d5SNeil Horman [RLIMIT_NICE] = {"Max nice priority", NULL}, 485d85f50d5SNeil Horman [RLIMIT_RTPRIO] = {"Max realtime priority", NULL}, 4868808117cSEugene Teo [RLIMIT_RTTIME] = {"Max realtime timeout", "us"}, 487d85f50d5SNeil Horman }; 488d85f50d5SNeil Horman 489d85f50d5SNeil Horman /* Display limits for a process */ 490d85f50d5SNeil Horman static int proc_pid_limits(struct task_struct *task, char *buffer) 491d85f50d5SNeil Horman { 492d85f50d5SNeil Horman unsigned int i; 493d85f50d5SNeil Horman int count = 0; 494d85f50d5SNeil Horman unsigned long flags; 495d85f50d5SNeil Horman char *bufptr = buffer; 496d85f50d5SNeil Horman 497d85f50d5SNeil Horman struct rlimit rlim[RLIM_NLIMITS]; 498d85f50d5SNeil Horman 499a6bebbc8SLai Jiangshan if (!lock_task_sighand(task, &flags)) 500d85f50d5SNeil Horman return 0; 501d85f50d5SNeil Horman memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS); 502d85f50d5SNeil Horman unlock_task_sighand(task, &flags); 503d85f50d5SNeil Horman 504d85f50d5SNeil Horman /* 505d85f50d5SNeil Horman * print the file header 506d85f50d5SNeil Horman */ 507d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n", 508d85f50d5SNeil Horman "Limit", "Soft Limit", "Hard Limit", "Units"); 509d85f50d5SNeil Horman 510d85f50d5SNeil Horman for (i = 0; i < RLIM_NLIMITS; i++) { 511d85f50d5SNeil Horman if (rlim[i].rlim_cur == RLIM_INFINITY) 512d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-25s %-20s ", 513d85f50d5SNeil Horman lnames[i].name, "unlimited"); 514d85f50d5SNeil Horman else 515d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-25s %-20lu ", 516d85f50d5SNeil Horman lnames[i].name, rlim[i].rlim_cur); 517d85f50d5SNeil Horman 518d85f50d5SNeil Horman if (rlim[i].rlim_max == RLIM_INFINITY) 519d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-20s ", "unlimited"); 520d85f50d5SNeil Horman else 521d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-20lu ", 522d85f50d5SNeil Horman rlim[i].rlim_max); 523d85f50d5SNeil Horman 524d85f50d5SNeil Horman if (lnames[i].unit) 525d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-10s\n", 526d85f50d5SNeil Horman lnames[i].unit); 527d85f50d5SNeil Horman else 528d85f50d5SNeil Horman count += sprintf(&bufptr[count], "\n"); 529d85f50d5SNeil Horman } 530d85f50d5SNeil Horman 531d85f50d5SNeil Horman return count; 532d85f50d5SNeil Horman } 533d85f50d5SNeil Horman 534ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK 535ebcb6734SRoland McGrath static int proc_pid_syscall(struct task_struct *task, char *buffer) 536ebcb6734SRoland McGrath { 537ebcb6734SRoland McGrath long nr; 538ebcb6734SRoland McGrath unsigned long args[6], sp, pc; 539ebcb6734SRoland McGrath 540ebcb6734SRoland McGrath if (task_current_syscall(task, &nr, args, 6, &sp, &pc)) 541ebcb6734SRoland McGrath return sprintf(buffer, "running\n"); 542ebcb6734SRoland McGrath 543ebcb6734SRoland McGrath if (nr < 0) 544ebcb6734SRoland McGrath return sprintf(buffer, "%ld 0x%lx 0x%lx\n", nr, sp, pc); 545ebcb6734SRoland McGrath 546ebcb6734SRoland McGrath return sprintf(buffer, 547ebcb6734SRoland McGrath "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n", 548ebcb6734SRoland McGrath nr, 549ebcb6734SRoland McGrath args[0], args[1], args[2], args[3], args[4], args[5], 550ebcb6734SRoland McGrath sp, pc); 551ebcb6734SRoland McGrath } 552ebcb6734SRoland McGrath #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */ 553ebcb6734SRoland McGrath 5541da177e4SLinus Torvalds /************************************************************************/ 5551da177e4SLinus Torvalds /* Here the fs part begins */ 5561da177e4SLinus Torvalds /************************************************************************/ 5571da177e4SLinus Torvalds 5581da177e4SLinus Torvalds /* permission checks */ 559778c1144SEric W. Biederman static int proc_fd_access_allowed(struct inode *inode) 5601da177e4SLinus Torvalds { 561778c1144SEric W. Biederman struct task_struct *task; 562778c1144SEric W. Biederman int allowed = 0; 563df26c40eSEric W. Biederman /* Allow access to a task's file descriptors if it is us or we 564df26c40eSEric W. Biederman * may use ptrace attach to the process and find out that 565df26c40eSEric W. Biederman * information. 566778c1144SEric W. Biederman */ 567778c1144SEric W. Biederman task = get_proc_task(inode); 568df26c40eSEric W. Biederman if (task) { 569006ebb40SStephen Smalley allowed = ptrace_may_access(task, PTRACE_MODE_READ); 570778c1144SEric W. Biederman put_task_struct(task); 571df26c40eSEric W. Biederman } 572778c1144SEric W. Biederman return allowed; 5731da177e4SLinus Torvalds } 5741da177e4SLinus Torvalds 5756d76fa58SLinus Torvalds static int proc_setattr(struct dentry *dentry, struct iattr *attr) 5766d76fa58SLinus Torvalds { 5776d76fa58SLinus Torvalds int error; 5786d76fa58SLinus Torvalds struct inode *inode = dentry->d_inode; 5796d76fa58SLinus Torvalds 5806d76fa58SLinus Torvalds if (attr->ia_valid & ATTR_MODE) 5816d76fa58SLinus Torvalds return -EPERM; 5826d76fa58SLinus Torvalds 5836d76fa58SLinus Torvalds error = inode_change_ok(inode, attr); 5846d76fa58SLinus Torvalds if (!error) 5856d76fa58SLinus Torvalds error = inode_setattr(inode, attr); 5866d76fa58SLinus Torvalds return error; 5876d76fa58SLinus Torvalds } 5886d76fa58SLinus Torvalds 589c5ef1c42SArjan van de Ven static const struct inode_operations proc_def_inode_operations = { 5906d76fa58SLinus Torvalds .setattr = proc_setattr, 5916d76fa58SLinus Torvalds }; 5926d76fa58SLinus Torvalds 593a1a2c409SMiklos Szeredi static int mounts_open_common(struct inode *inode, struct file *file, 594a1a2c409SMiklos Szeredi const struct seq_operations *op) 5951da177e4SLinus Torvalds { 59699f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 597cf7b708cSPavel Emelyanov struct nsproxy *nsp; 5986b3286edSKirill Korotaev struct mnt_namespace *ns = NULL; 599a1a2c409SMiklos Szeredi struct fs_struct *fs = NULL; 600a1a2c409SMiklos Szeredi struct path root; 6015addc5ddSAl Viro struct proc_mounts *p; 6025addc5ddSAl Viro int ret = -EINVAL; 6035addc5ddSAl Viro 60499f89551SEric W. Biederman if (task) { 605cf7b708cSPavel Emelyanov rcu_read_lock(); 606cf7b708cSPavel Emelyanov nsp = task_nsproxy(task); 607cf7b708cSPavel Emelyanov if (nsp) { 608cf7b708cSPavel Emelyanov ns = nsp->mnt_ns; 6096b3286edSKirill Korotaev if (ns) 6106b3286edSKirill Korotaev get_mnt_ns(ns); 611863c4702SAlexey Dobriyan } 612cf7b708cSPavel Emelyanov rcu_read_unlock(); 613a1a2c409SMiklos Szeredi if (ns) 614a1a2c409SMiklos Szeredi fs = get_fs_struct(task); 61599f89551SEric W. Biederman put_task_struct(task); 61699f89551SEric W. Biederman } 6171da177e4SLinus Torvalds 618a1a2c409SMiklos Szeredi if (!ns) 619a1a2c409SMiklos Szeredi goto err; 620a1a2c409SMiklos Szeredi if (!fs) 621a1a2c409SMiklos Szeredi goto err_put_ns; 622a1a2c409SMiklos Szeredi 623a1a2c409SMiklos Szeredi read_lock(&fs->lock); 624a1a2c409SMiklos Szeredi root = fs->root; 625a1a2c409SMiklos Szeredi path_get(&root); 626a1a2c409SMiklos Szeredi read_unlock(&fs->lock); 627a1a2c409SMiklos Szeredi put_fs_struct(fs); 628a1a2c409SMiklos Szeredi 6295addc5ddSAl Viro ret = -ENOMEM; 6305addc5ddSAl Viro p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL); 631a1a2c409SMiklos Szeredi if (!p) 632a1a2c409SMiklos Szeredi goto err_put_path; 633a1a2c409SMiklos Szeredi 6345addc5ddSAl Viro file->private_data = &p->m; 635a1a2c409SMiklos Szeredi ret = seq_open(file, op); 636a1a2c409SMiklos Szeredi if (ret) 637a1a2c409SMiklos Szeredi goto err_free; 638a1a2c409SMiklos Szeredi 639a1a2c409SMiklos Szeredi p->m.private = p; 640a1a2c409SMiklos Szeredi p->ns = ns; 641a1a2c409SMiklos Szeredi p->root = root; 6426b3286edSKirill Korotaev p->event = ns->event; 643a1a2c409SMiklos Szeredi 6445addc5ddSAl Viro return 0; 645a1a2c409SMiklos Szeredi 646a1a2c409SMiklos Szeredi err_free: 6475addc5ddSAl Viro kfree(p); 648a1a2c409SMiklos Szeredi err_put_path: 649a1a2c409SMiklos Szeredi path_put(&root); 650a1a2c409SMiklos Szeredi err_put_ns: 6516b3286edSKirill Korotaev put_mnt_ns(ns); 652a1a2c409SMiklos Szeredi err: 6531da177e4SLinus Torvalds return ret; 6541da177e4SLinus Torvalds } 6551da177e4SLinus Torvalds 6561da177e4SLinus Torvalds static int mounts_release(struct inode *inode, struct file *file) 6571da177e4SLinus Torvalds { 658a1a2c409SMiklos Szeredi struct proc_mounts *p = file->private_data; 659a1a2c409SMiklos Szeredi path_put(&p->root); 660a1a2c409SMiklos Szeredi put_mnt_ns(p->ns); 6611da177e4SLinus Torvalds return seq_release(inode, file); 6621da177e4SLinus Torvalds } 6631da177e4SLinus Torvalds 6645addc5ddSAl Viro static unsigned mounts_poll(struct file *file, poll_table *wait) 6655addc5ddSAl Viro { 6665addc5ddSAl Viro struct proc_mounts *p = file->private_data; 667a1a2c409SMiklos Szeredi struct mnt_namespace *ns = p->ns; 6685addc5ddSAl Viro unsigned res = 0; 6695addc5ddSAl Viro 6705addc5ddSAl Viro poll_wait(file, &ns->poll, wait); 6715addc5ddSAl Viro 6725addc5ddSAl Viro spin_lock(&vfsmount_lock); 6735addc5ddSAl Viro if (p->event != ns->event) { 6745addc5ddSAl Viro p->event = ns->event; 6755addc5ddSAl Viro res = POLLERR; 6765addc5ddSAl Viro } 6775addc5ddSAl Viro spin_unlock(&vfsmount_lock); 6785addc5ddSAl Viro 6795addc5ddSAl Viro return res; 6805addc5ddSAl Viro } 6815addc5ddSAl Viro 682a1a2c409SMiklos Szeredi static int mounts_open(struct inode *inode, struct file *file) 683a1a2c409SMiklos Szeredi { 684a1a2c409SMiklos Szeredi return mounts_open_common(inode, file, &mounts_op); 685a1a2c409SMiklos Szeredi } 686a1a2c409SMiklos Szeredi 68700977a59SArjan van de Ven static const struct file_operations proc_mounts_operations = { 6881da177e4SLinus Torvalds .open = mounts_open, 6891da177e4SLinus Torvalds .read = seq_read, 6901da177e4SLinus Torvalds .llseek = seq_lseek, 6911da177e4SLinus Torvalds .release = mounts_release, 6925addc5ddSAl Viro .poll = mounts_poll, 6931da177e4SLinus Torvalds }; 6941da177e4SLinus Torvalds 6952d4d4864SRam Pai static int mountinfo_open(struct inode *inode, struct file *file) 6962d4d4864SRam Pai { 6972d4d4864SRam Pai return mounts_open_common(inode, file, &mountinfo_op); 6982d4d4864SRam Pai } 6992d4d4864SRam Pai 7002d4d4864SRam Pai static const struct file_operations proc_mountinfo_operations = { 7012d4d4864SRam Pai .open = mountinfo_open, 7022d4d4864SRam Pai .read = seq_read, 7032d4d4864SRam Pai .llseek = seq_lseek, 7042d4d4864SRam Pai .release = mounts_release, 7052d4d4864SRam Pai .poll = mounts_poll, 7062d4d4864SRam Pai }; 7072d4d4864SRam Pai 708b4629fe2SChuck Lever static int mountstats_open(struct inode *inode, struct file *file) 709b4629fe2SChuck Lever { 710a1a2c409SMiklos Szeredi return mounts_open_common(inode, file, &mountstats_op); 711b4629fe2SChuck Lever } 712b4629fe2SChuck Lever 71300977a59SArjan van de Ven static const struct file_operations proc_mountstats_operations = { 714b4629fe2SChuck Lever .open = mountstats_open, 715b4629fe2SChuck Lever .read = seq_read, 716b4629fe2SChuck Lever .llseek = seq_lseek, 717b4629fe2SChuck Lever .release = mounts_release, 718b4629fe2SChuck Lever }; 719b4629fe2SChuck Lever 7201da177e4SLinus Torvalds #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */ 7211da177e4SLinus Torvalds 7221da177e4SLinus Torvalds static ssize_t proc_info_read(struct file * file, char __user * buf, 7231da177e4SLinus Torvalds size_t count, loff_t *ppos) 7241da177e4SLinus Torvalds { 7252fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 7261da177e4SLinus Torvalds unsigned long page; 7271da177e4SLinus Torvalds ssize_t length; 72899f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 72999f89551SEric W. Biederman 73099f89551SEric W. Biederman length = -ESRCH; 73199f89551SEric W. Biederman if (!task) 73299f89551SEric W. Biederman goto out_no_task; 7331da177e4SLinus Torvalds 7341da177e4SLinus Torvalds if (count > PROC_BLOCK_SIZE) 7351da177e4SLinus Torvalds count = PROC_BLOCK_SIZE; 73699f89551SEric W. Biederman 73799f89551SEric W. Biederman length = -ENOMEM; 738e12ba74dSMel Gorman if (!(page = __get_free_page(GFP_TEMPORARY))) 73999f89551SEric W. Biederman goto out; 7401da177e4SLinus Torvalds 7411da177e4SLinus Torvalds length = PROC_I(inode)->op.proc_read(task, (char*)page); 7421da177e4SLinus Torvalds 7431da177e4SLinus Torvalds if (length >= 0) 7441da177e4SLinus Torvalds length = simple_read_from_buffer(buf, count, ppos, (char *)page, length); 7451da177e4SLinus Torvalds free_page(page); 74699f89551SEric W. Biederman out: 74799f89551SEric W. Biederman put_task_struct(task); 74899f89551SEric W. Biederman out_no_task: 7491da177e4SLinus Torvalds return length; 7501da177e4SLinus Torvalds } 7511da177e4SLinus Torvalds 75200977a59SArjan van de Ven static const struct file_operations proc_info_file_operations = { 7531da177e4SLinus Torvalds .read = proc_info_read, 7541da177e4SLinus Torvalds }; 7551da177e4SLinus Torvalds 756be614086SEric W. Biederman static int proc_single_show(struct seq_file *m, void *v) 757be614086SEric W. Biederman { 758be614086SEric W. Biederman struct inode *inode = m->private; 759be614086SEric W. Biederman struct pid_namespace *ns; 760be614086SEric W. Biederman struct pid *pid; 761be614086SEric W. Biederman struct task_struct *task; 762be614086SEric W. Biederman int ret; 763be614086SEric W. Biederman 764be614086SEric W. Biederman ns = inode->i_sb->s_fs_info; 765be614086SEric W. Biederman pid = proc_pid(inode); 766be614086SEric W. Biederman task = get_pid_task(pid, PIDTYPE_PID); 767be614086SEric W. Biederman if (!task) 768be614086SEric W. Biederman return -ESRCH; 769be614086SEric W. Biederman 770be614086SEric W. Biederman ret = PROC_I(inode)->op.proc_show(m, ns, pid, task); 771be614086SEric W. Biederman 772be614086SEric W. Biederman put_task_struct(task); 773be614086SEric W. Biederman return ret; 774be614086SEric W. Biederman } 775be614086SEric W. Biederman 776be614086SEric W. Biederman static int proc_single_open(struct inode *inode, struct file *filp) 777be614086SEric W. Biederman { 778be614086SEric W. Biederman int ret; 779be614086SEric W. Biederman ret = single_open(filp, proc_single_show, NULL); 780be614086SEric W. Biederman if (!ret) { 781be614086SEric W. Biederman struct seq_file *m = filp->private_data; 782be614086SEric W. Biederman 783be614086SEric W. Biederman m->private = inode; 784be614086SEric W. Biederman } 785be614086SEric W. Biederman return ret; 786be614086SEric W. Biederman } 787be614086SEric W. Biederman 788be614086SEric W. Biederman static const struct file_operations proc_single_file_operations = { 789be614086SEric W. Biederman .open = proc_single_open, 790be614086SEric W. Biederman .read = seq_read, 791be614086SEric W. Biederman .llseek = seq_lseek, 792be614086SEric W. Biederman .release = single_release, 793be614086SEric W. Biederman }; 794be614086SEric W. Biederman 7951da177e4SLinus Torvalds static int mem_open(struct inode* inode, struct file* file) 7961da177e4SLinus Torvalds { 7971da177e4SLinus Torvalds file->private_data = (void*)((long)current->self_exec_id); 7981da177e4SLinus Torvalds return 0; 7991da177e4SLinus Torvalds } 8001da177e4SLinus Torvalds 8011da177e4SLinus Torvalds static ssize_t mem_read(struct file * file, char __user * buf, 8021da177e4SLinus Torvalds size_t count, loff_t *ppos) 8031da177e4SLinus Torvalds { 8042fddfeefSJosef "Jeff" Sipek struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode); 8051da177e4SLinus Torvalds char *page; 8061da177e4SLinus Torvalds unsigned long src = *ppos; 8071da177e4SLinus Torvalds int ret = -ESRCH; 8081da177e4SLinus Torvalds struct mm_struct *mm; 8091da177e4SLinus Torvalds 81099f89551SEric W. Biederman if (!task) 81199f89551SEric W. Biederman goto out_no_task; 81299f89551SEric W. Biederman 813638fa202SRoland McGrath if (check_mem_permission(task)) 8141da177e4SLinus Torvalds goto out; 8151da177e4SLinus Torvalds 8161da177e4SLinus Torvalds ret = -ENOMEM; 817e12ba74dSMel Gorman page = (char *)__get_free_page(GFP_TEMPORARY); 8181da177e4SLinus Torvalds if (!page) 8191da177e4SLinus Torvalds goto out; 8201da177e4SLinus Torvalds 8211da177e4SLinus Torvalds ret = 0; 8221da177e4SLinus Torvalds 8231da177e4SLinus Torvalds mm = get_task_mm(task); 8241da177e4SLinus Torvalds if (!mm) 8251da177e4SLinus Torvalds goto out_free; 8261da177e4SLinus Torvalds 8271da177e4SLinus Torvalds ret = -EIO; 8281da177e4SLinus Torvalds 8291da177e4SLinus Torvalds if (file->private_data != (void*)((long)current->self_exec_id)) 8301da177e4SLinus Torvalds goto out_put; 8311da177e4SLinus Torvalds 8321da177e4SLinus Torvalds ret = 0; 8331da177e4SLinus Torvalds 8341da177e4SLinus Torvalds while (count > 0) { 8351da177e4SLinus Torvalds int this_len, retval; 8361da177e4SLinus Torvalds 8371da177e4SLinus Torvalds this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count; 8381da177e4SLinus Torvalds retval = access_process_vm(task, src, page, this_len, 0); 839638fa202SRoland McGrath if (!retval || check_mem_permission(task)) { 8401da177e4SLinus Torvalds if (!ret) 8411da177e4SLinus Torvalds ret = -EIO; 8421da177e4SLinus Torvalds break; 8431da177e4SLinus Torvalds } 8441da177e4SLinus Torvalds 8451da177e4SLinus Torvalds if (copy_to_user(buf, page, retval)) { 8461da177e4SLinus Torvalds ret = -EFAULT; 8471da177e4SLinus Torvalds break; 8481da177e4SLinus Torvalds } 8491da177e4SLinus Torvalds 8501da177e4SLinus Torvalds ret += retval; 8511da177e4SLinus Torvalds src += retval; 8521da177e4SLinus Torvalds buf += retval; 8531da177e4SLinus Torvalds count -= retval; 8541da177e4SLinus Torvalds } 8551da177e4SLinus Torvalds *ppos = src; 8561da177e4SLinus Torvalds 8571da177e4SLinus Torvalds out_put: 8581da177e4SLinus Torvalds mmput(mm); 8591da177e4SLinus Torvalds out_free: 8601da177e4SLinus Torvalds free_page((unsigned long) page); 8611da177e4SLinus Torvalds out: 86299f89551SEric W. Biederman put_task_struct(task); 86399f89551SEric W. Biederman out_no_task: 8641da177e4SLinus Torvalds return ret; 8651da177e4SLinus Torvalds } 8661da177e4SLinus Torvalds 8671da177e4SLinus Torvalds #define mem_write NULL 8681da177e4SLinus Torvalds 8691da177e4SLinus Torvalds #ifndef mem_write 8701da177e4SLinus Torvalds /* This is a security hazard */ 87163967fa9SGlauber de Oliveira Costa static ssize_t mem_write(struct file * file, const char __user *buf, 8721da177e4SLinus Torvalds size_t count, loff_t *ppos) 8731da177e4SLinus Torvalds { 874f7ca54f4SFrederik Deweerdt int copied; 8751da177e4SLinus Torvalds char *page; 8762fddfeefSJosef "Jeff" Sipek struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode); 8771da177e4SLinus Torvalds unsigned long dst = *ppos; 8781da177e4SLinus Torvalds 87999f89551SEric W. Biederman copied = -ESRCH; 88099f89551SEric W. Biederman if (!task) 88199f89551SEric W. Biederman goto out_no_task; 8821da177e4SLinus Torvalds 883638fa202SRoland McGrath if (check_mem_permission(task)) 88499f89551SEric W. Biederman goto out; 88599f89551SEric W. Biederman 88699f89551SEric W. Biederman copied = -ENOMEM; 887e12ba74dSMel Gorman page = (char *)__get_free_page(GFP_TEMPORARY); 8881da177e4SLinus Torvalds if (!page) 88999f89551SEric W. Biederman goto out; 8901da177e4SLinus Torvalds 891f7ca54f4SFrederik Deweerdt copied = 0; 8921da177e4SLinus Torvalds while (count > 0) { 8931da177e4SLinus Torvalds int this_len, retval; 8941da177e4SLinus Torvalds 8951da177e4SLinus Torvalds this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count; 8961da177e4SLinus Torvalds if (copy_from_user(page, buf, this_len)) { 8971da177e4SLinus Torvalds copied = -EFAULT; 8981da177e4SLinus Torvalds break; 8991da177e4SLinus Torvalds } 9001da177e4SLinus Torvalds retval = access_process_vm(task, dst, page, this_len, 1); 9011da177e4SLinus Torvalds if (!retval) { 9021da177e4SLinus Torvalds if (!copied) 9031da177e4SLinus Torvalds copied = -EIO; 9041da177e4SLinus Torvalds break; 9051da177e4SLinus Torvalds } 9061da177e4SLinus Torvalds copied += retval; 9071da177e4SLinus Torvalds buf += retval; 9081da177e4SLinus Torvalds dst += retval; 9091da177e4SLinus Torvalds count -= retval; 9101da177e4SLinus Torvalds } 9111da177e4SLinus Torvalds *ppos = dst; 9121da177e4SLinus Torvalds free_page((unsigned long) page); 91399f89551SEric W. Biederman out: 91499f89551SEric W. Biederman put_task_struct(task); 91599f89551SEric W. Biederman out_no_task: 9161da177e4SLinus Torvalds return copied; 9171da177e4SLinus Torvalds } 9181da177e4SLinus Torvalds #endif 9191da177e4SLinus Torvalds 92085863e47SMatt Mackall loff_t mem_lseek(struct file *file, loff_t offset, int orig) 9211da177e4SLinus Torvalds { 9221da177e4SLinus Torvalds switch (orig) { 9231da177e4SLinus Torvalds case 0: 9241da177e4SLinus Torvalds file->f_pos = offset; 9251da177e4SLinus Torvalds break; 9261da177e4SLinus Torvalds case 1: 9271da177e4SLinus Torvalds file->f_pos += offset; 9281da177e4SLinus Torvalds break; 9291da177e4SLinus Torvalds default: 9301da177e4SLinus Torvalds return -EINVAL; 9311da177e4SLinus Torvalds } 9321da177e4SLinus Torvalds force_successful_syscall_return(); 9331da177e4SLinus Torvalds return file->f_pos; 9341da177e4SLinus Torvalds } 9351da177e4SLinus Torvalds 93600977a59SArjan van de Ven static const struct file_operations proc_mem_operations = { 9371da177e4SLinus Torvalds .llseek = mem_lseek, 9381da177e4SLinus Torvalds .read = mem_read, 9391da177e4SLinus Torvalds .write = mem_write, 9401da177e4SLinus Torvalds .open = mem_open, 9411da177e4SLinus Torvalds }; 9421da177e4SLinus Torvalds 943315e28c8SJames Pearson static ssize_t environ_read(struct file *file, char __user *buf, 944315e28c8SJames Pearson size_t count, loff_t *ppos) 945315e28c8SJames Pearson { 946315e28c8SJames Pearson struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 947315e28c8SJames Pearson char *page; 948315e28c8SJames Pearson unsigned long src = *ppos; 949315e28c8SJames Pearson int ret = -ESRCH; 950315e28c8SJames Pearson struct mm_struct *mm; 951315e28c8SJames Pearson 952315e28c8SJames Pearson if (!task) 953315e28c8SJames Pearson goto out_no_task; 954315e28c8SJames Pearson 955006ebb40SStephen Smalley if (!ptrace_may_access(task, PTRACE_MODE_READ)) 956315e28c8SJames Pearson goto out; 957315e28c8SJames Pearson 958315e28c8SJames Pearson ret = -ENOMEM; 959315e28c8SJames Pearson page = (char *)__get_free_page(GFP_TEMPORARY); 960315e28c8SJames Pearson if (!page) 961315e28c8SJames Pearson goto out; 962315e28c8SJames Pearson 963315e28c8SJames Pearson ret = 0; 964315e28c8SJames Pearson 965315e28c8SJames Pearson mm = get_task_mm(task); 966315e28c8SJames Pearson if (!mm) 967315e28c8SJames Pearson goto out_free; 968315e28c8SJames Pearson 969315e28c8SJames Pearson while (count > 0) { 970315e28c8SJames Pearson int this_len, retval, max_len; 971315e28c8SJames Pearson 972315e28c8SJames Pearson this_len = mm->env_end - (mm->env_start + src); 973315e28c8SJames Pearson 974315e28c8SJames Pearson if (this_len <= 0) 975315e28c8SJames Pearson break; 976315e28c8SJames Pearson 977315e28c8SJames Pearson max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count; 978315e28c8SJames Pearson this_len = (this_len > max_len) ? max_len : this_len; 979315e28c8SJames Pearson 980315e28c8SJames Pearson retval = access_process_vm(task, (mm->env_start + src), 981315e28c8SJames Pearson page, this_len, 0); 982315e28c8SJames Pearson 983315e28c8SJames Pearson if (retval <= 0) { 984315e28c8SJames Pearson ret = retval; 985315e28c8SJames Pearson break; 986315e28c8SJames Pearson } 987315e28c8SJames Pearson 988315e28c8SJames Pearson if (copy_to_user(buf, page, retval)) { 989315e28c8SJames Pearson ret = -EFAULT; 990315e28c8SJames Pearson break; 991315e28c8SJames Pearson } 992315e28c8SJames Pearson 993315e28c8SJames Pearson ret += retval; 994315e28c8SJames Pearson src += retval; 995315e28c8SJames Pearson buf += retval; 996315e28c8SJames Pearson count -= retval; 997315e28c8SJames Pearson } 998315e28c8SJames Pearson *ppos = src; 999315e28c8SJames Pearson 1000315e28c8SJames Pearson mmput(mm); 1001315e28c8SJames Pearson out_free: 1002315e28c8SJames Pearson free_page((unsigned long) page); 1003315e28c8SJames Pearson out: 1004315e28c8SJames Pearson put_task_struct(task); 1005315e28c8SJames Pearson out_no_task: 1006315e28c8SJames Pearson return ret; 1007315e28c8SJames Pearson } 1008315e28c8SJames Pearson 1009315e28c8SJames Pearson static const struct file_operations proc_environ_operations = { 1010315e28c8SJames Pearson .read = environ_read, 1011315e28c8SJames Pearson }; 1012315e28c8SJames Pearson 10131da177e4SLinus Torvalds static ssize_t oom_adjust_read(struct file *file, char __user *buf, 10141da177e4SLinus Torvalds size_t count, loff_t *ppos) 10151da177e4SLinus Torvalds { 10162fddfeefSJosef "Jeff" Sipek struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode); 10178578cea7SEric W. Biederman char buffer[PROC_NUMBUF]; 10181da177e4SLinus Torvalds size_t len; 101999f89551SEric W. Biederman int oom_adjust; 10201da177e4SLinus Torvalds 102199f89551SEric W. Biederman if (!task) 102299f89551SEric W. Biederman return -ESRCH; 102399f89551SEric W. Biederman oom_adjust = task->oomkilladj; 102499f89551SEric W. Biederman put_task_struct(task); 102599f89551SEric W. Biederman 10268578cea7SEric W. Biederman len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust); 10270c28f287SAkinobu Mita 10280c28f287SAkinobu Mita return simple_read_from_buffer(buf, count, ppos, buffer, len); 10291da177e4SLinus Torvalds } 10301da177e4SLinus Torvalds 10311da177e4SLinus Torvalds static ssize_t oom_adjust_write(struct file *file, const char __user *buf, 10321da177e4SLinus Torvalds size_t count, loff_t *ppos) 10331da177e4SLinus Torvalds { 103499f89551SEric W. Biederman struct task_struct *task; 10358578cea7SEric W. Biederman char buffer[PROC_NUMBUF], *end; 10361da177e4SLinus Torvalds int oom_adjust; 10371da177e4SLinus Torvalds 10388578cea7SEric W. Biederman memset(buffer, 0, sizeof(buffer)); 10398578cea7SEric W. Biederman if (count > sizeof(buffer) - 1) 10408578cea7SEric W. Biederman count = sizeof(buffer) - 1; 10411da177e4SLinus Torvalds if (copy_from_user(buffer, buf, count)) 10421da177e4SLinus Torvalds return -EFAULT; 10431da177e4SLinus Torvalds oom_adjust = simple_strtol(buffer, &end, 0); 10448ac773b4SAlexey Dobriyan if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) && 10458ac773b4SAlexey Dobriyan oom_adjust != OOM_DISABLE) 10461da177e4SLinus Torvalds return -EINVAL; 10471da177e4SLinus Torvalds if (*end == '\n') 10481da177e4SLinus Torvalds end++; 10492fddfeefSJosef "Jeff" Sipek task = get_proc_task(file->f_path.dentry->d_inode); 105099f89551SEric W. Biederman if (!task) 105199f89551SEric W. Biederman return -ESRCH; 10528fb4fc68SGuillem Jover if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) { 10538fb4fc68SGuillem Jover put_task_struct(task); 10548fb4fc68SGuillem Jover return -EACCES; 10558fb4fc68SGuillem Jover } 10561da177e4SLinus Torvalds task->oomkilladj = oom_adjust; 105799f89551SEric W. Biederman put_task_struct(task); 10581da177e4SLinus Torvalds if (end - buffer == 0) 10591da177e4SLinus Torvalds return -EIO; 10601da177e4SLinus Torvalds return end - buffer; 10611da177e4SLinus Torvalds } 10621da177e4SLinus Torvalds 106300977a59SArjan van de Ven static const struct file_operations proc_oom_adjust_operations = { 10641da177e4SLinus Torvalds .read = oom_adjust_read, 10651da177e4SLinus Torvalds .write = oom_adjust_write, 10661da177e4SLinus Torvalds }; 10671da177e4SLinus Torvalds 10681da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL 10691da177e4SLinus Torvalds #define TMPBUFLEN 21 10701da177e4SLinus Torvalds static ssize_t proc_loginuid_read(struct file * file, char __user * buf, 10711da177e4SLinus Torvalds size_t count, loff_t *ppos) 10721da177e4SLinus Torvalds { 10732fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 107499f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 10751da177e4SLinus Torvalds ssize_t length; 10761da177e4SLinus Torvalds char tmpbuf[TMPBUFLEN]; 10771da177e4SLinus Torvalds 107899f89551SEric W. Biederman if (!task) 107999f89551SEric W. Biederman return -ESRCH; 10801da177e4SLinus Torvalds length = scnprintf(tmpbuf, TMPBUFLEN, "%u", 10810c11b942SAl Viro audit_get_loginuid(task)); 108299f89551SEric W. Biederman put_task_struct(task); 10831da177e4SLinus Torvalds return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 10841da177e4SLinus Torvalds } 10851da177e4SLinus Torvalds 10861da177e4SLinus Torvalds static ssize_t proc_loginuid_write(struct file * file, const char __user * buf, 10871da177e4SLinus Torvalds size_t count, loff_t *ppos) 10881da177e4SLinus Torvalds { 10892fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 10901da177e4SLinus Torvalds char *page, *tmp; 10911da177e4SLinus Torvalds ssize_t length; 10921da177e4SLinus Torvalds uid_t loginuid; 10931da177e4SLinus Torvalds 10941da177e4SLinus Torvalds if (!capable(CAP_AUDIT_CONTROL)) 10951da177e4SLinus Torvalds return -EPERM; 10961da177e4SLinus Torvalds 109713b41b09SEric W. Biederman if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) 10981da177e4SLinus Torvalds return -EPERM; 10991da177e4SLinus Torvalds 1100e0182909SAl Viro if (count >= PAGE_SIZE) 1101e0182909SAl Viro count = PAGE_SIZE - 1; 11021da177e4SLinus Torvalds 11031da177e4SLinus Torvalds if (*ppos != 0) { 11041da177e4SLinus Torvalds /* No partial writes. */ 11051da177e4SLinus Torvalds return -EINVAL; 11061da177e4SLinus Torvalds } 1107e12ba74dSMel Gorman page = (char*)__get_free_page(GFP_TEMPORARY); 11081da177e4SLinus Torvalds if (!page) 11091da177e4SLinus Torvalds return -ENOMEM; 11101da177e4SLinus Torvalds length = -EFAULT; 11111da177e4SLinus Torvalds if (copy_from_user(page, buf, count)) 11121da177e4SLinus Torvalds goto out_free_page; 11131da177e4SLinus Torvalds 1114e0182909SAl Viro page[count] = '\0'; 11151da177e4SLinus Torvalds loginuid = simple_strtoul(page, &tmp, 10); 11161da177e4SLinus Torvalds if (tmp == page) { 11171da177e4SLinus Torvalds length = -EINVAL; 11181da177e4SLinus Torvalds goto out_free_page; 11191da177e4SLinus Torvalds 11201da177e4SLinus Torvalds } 112199f89551SEric W. Biederman length = audit_set_loginuid(current, loginuid); 11221da177e4SLinus Torvalds if (likely(length == 0)) 11231da177e4SLinus Torvalds length = count; 11241da177e4SLinus Torvalds 11251da177e4SLinus Torvalds out_free_page: 11261da177e4SLinus Torvalds free_page((unsigned long) page); 11271da177e4SLinus Torvalds return length; 11281da177e4SLinus Torvalds } 11291da177e4SLinus Torvalds 113000977a59SArjan van de Ven static const struct file_operations proc_loginuid_operations = { 11311da177e4SLinus Torvalds .read = proc_loginuid_read, 11321da177e4SLinus Torvalds .write = proc_loginuid_write, 11331da177e4SLinus Torvalds }; 11341e0bd755SEric Paris 11351e0bd755SEric Paris static ssize_t proc_sessionid_read(struct file * file, char __user * buf, 11361e0bd755SEric Paris size_t count, loff_t *ppos) 11371e0bd755SEric Paris { 11381e0bd755SEric Paris struct inode * inode = file->f_path.dentry->d_inode; 11391e0bd755SEric Paris struct task_struct *task = get_proc_task(inode); 11401e0bd755SEric Paris ssize_t length; 11411e0bd755SEric Paris char tmpbuf[TMPBUFLEN]; 11421e0bd755SEric Paris 11431e0bd755SEric Paris if (!task) 11441e0bd755SEric Paris return -ESRCH; 11451e0bd755SEric Paris length = scnprintf(tmpbuf, TMPBUFLEN, "%u", 11461e0bd755SEric Paris audit_get_sessionid(task)); 11471e0bd755SEric Paris put_task_struct(task); 11481e0bd755SEric Paris return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 11491e0bd755SEric Paris } 11501e0bd755SEric Paris 11511e0bd755SEric Paris static const struct file_operations proc_sessionid_operations = { 11521e0bd755SEric Paris .read = proc_sessionid_read, 11531e0bd755SEric Paris }; 11541da177e4SLinus Torvalds #endif 11551da177e4SLinus Torvalds 1156f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION 1157f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_read(struct file * file, char __user * buf, 1158f4f154fdSAkinobu Mita size_t count, loff_t *ppos) 1159f4f154fdSAkinobu Mita { 1160f4f154fdSAkinobu Mita struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 1161f4f154fdSAkinobu Mita char buffer[PROC_NUMBUF]; 1162f4f154fdSAkinobu Mita size_t len; 1163f4f154fdSAkinobu Mita int make_it_fail; 1164f4f154fdSAkinobu Mita 1165f4f154fdSAkinobu Mita if (!task) 1166f4f154fdSAkinobu Mita return -ESRCH; 1167f4f154fdSAkinobu Mita make_it_fail = task->make_it_fail; 1168f4f154fdSAkinobu Mita put_task_struct(task); 1169f4f154fdSAkinobu Mita 1170f4f154fdSAkinobu Mita len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail); 11710c28f287SAkinobu Mita 11720c28f287SAkinobu Mita return simple_read_from_buffer(buf, count, ppos, buffer, len); 1173f4f154fdSAkinobu Mita } 1174f4f154fdSAkinobu Mita 1175f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_write(struct file * file, 1176f4f154fdSAkinobu Mita const char __user * buf, size_t count, loff_t *ppos) 1177f4f154fdSAkinobu Mita { 1178f4f154fdSAkinobu Mita struct task_struct *task; 1179f4f154fdSAkinobu Mita char buffer[PROC_NUMBUF], *end; 1180f4f154fdSAkinobu Mita int make_it_fail; 1181f4f154fdSAkinobu Mita 1182f4f154fdSAkinobu Mita if (!capable(CAP_SYS_RESOURCE)) 1183f4f154fdSAkinobu Mita return -EPERM; 1184f4f154fdSAkinobu Mita memset(buffer, 0, sizeof(buffer)); 1185f4f154fdSAkinobu Mita if (count > sizeof(buffer) - 1) 1186f4f154fdSAkinobu Mita count = sizeof(buffer) - 1; 1187f4f154fdSAkinobu Mita if (copy_from_user(buffer, buf, count)) 1188f4f154fdSAkinobu Mita return -EFAULT; 1189f4f154fdSAkinobu Mita make_it_fail = simple_strtol(buffer, &end, 0); 1190f4f154fdSAkinobu Mita if (*end == '\n') 1191f4f154fdSAkinobu Mita end++; 1192f4f154fdSAkinobu Mita task = get_proc_task(file->f_dentry->d_inode); 1193f4f154fdSAkinobu Mita if (!task) 1194f4f154fdSAkinobu Mita return -ESRCH; 1195f4f154fdSAkinobu Mita task->make_it_fail = make_it_fail; 1196f4f154fdSAkinobu Mita put_task_struct(task); 1197f4f154fdSAkinobu Mita if (end - buffer == 0) 1198f4f154fdSAkinobu Mita return -EIO; 1199f4f154fdSAkinobu Mita return end - buffer; 1200f4f154fdSAkinobu Mita } 1201f4f154fdSAkinobu Mita 120200977a59SArjan van de Ven static const struct file_operations proc_fault_inject_operations = { 1203f4f154fdSAkinobu Mita .read = proc_fault_inject_read, 1204f4f154fdSAkinobu Mita .write = proc_fault_inject_write, 1205f4f154fdSAkinobu Mita }; 1206f4f154fdSAkinobu Mita #endif 1207f4f154fdSAkinobu Mita 12089745512cSArjan van de Ven 120943ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG 121043ae34cbSIngo Molnar /* 121143ae34cbSIngo Molnar * Print out various scheduling related per-task fields: 121243ae34cbSIngo Molnar */ 121343ae34cbSIngo Molnar static int sched_show(struct seq_file *m, void *v) 121443ae34cbSIngo Molnar { 121543ae34cbSIngo Molnar struct inode *inode = m->private; 121643ae34cbSIngo Molnar struct task_struct *p; 121743ae34cbSIngo Molnar 121843ae34cbSIngo Molnar p = get_proc_task(inode); 121943ae34cbSIngo Molnar if (!p) 122043ae34cbSIngo Molnar return -ESRCH; 122143ae34cbSIngo Molnar proc_sched_show_task(p, m); 122243ae34cbSIngo Molnar 122343ae34cbSIngo Molnar put_task_struct(p); 122443ae34cbSIngo Molnar 122543ae34cbSIngo Molnar return 0; 122643ae34cbSIngo Molnar } 122743ae34cbSIngo Molnar 122843ae34cbSIngo Molnar static ssize_t 122943ae34cbSIngo Molnar sched_write(struct file *file, const char __user *buf, 123043ae34cbSIngo Molnar size_t count, loff_t *offset) 123143ae34cbSIngo Molnar { 123243ae34cbSIngo Molnar struct inode *inode = file->f_path.dentry->d_inode; 123343ae34cbSIngo Molnar struct task_struct *p; 123443ae34cbSIngo Molnar 123543ae34cbSIngo Molnar p = get_proc_task(inode); 123643ae34cbSIngo Molnar if (!p) 123743ae34cbSIngo Molnar return -ESRCH; 123843ae34cbSIngo Molnar proc_sched_set_task(p); 123943ae34cbSIngo Molnar 124043ae34cbSIngo Molnar put_task_struct(p); 124143ae34cbSIngo Molnar 124243ae34cbSIngo Molnar return count; 124343ae34cbSIngo Molnar } 124443ae34cbSIngo Molnar 124543ae34cbSIngo Molnar static int sched_open(struct inode *inode, struct file *filp) 124643ae34cbSIngo Molnar { 124743ae34cbSIngo Molnar int ret; 124843ae34cbSIngo Molnar 124943ae34cbSIngo Molnar ret = single_open(filp, sched_show, NULL); 125043ae34cbSIngo Molnar if (!ret) { 125143ae34cbSIngo Molnar struct seq_file *m = filp->private_data; 125243ae34cbSIngo Molnar 125343ae34cbSIngo Molnar m->private = inode; 125443ae34cbSIngo Molnar } 125543ae34cbSIngo Molnar return ret; 125643ae34cbSIngo Molnar } 125743ae34cbSIngo Molnar 125843ae34cbSIngo Molnar static const struct file_operations proc_pid_sched_operations = { 125943ae34cbSIngo Molnar .open = sched_open, 126043ae34cbSIngo Molnar .read = seq_read, 126143ae34cbSIngo Molnar .write = sched_write, 126243ae34cbSIngo Molnar .llseek = seq_lseek, 12635ea473a1SAlexey Dobriyan .release = single_release, 126443ae34cbSIngo Molnar }; 126543ae34cbSIngo Molnar 126643ae34cbSIngo Molnar #endif 126743ae34cbSIngo Molnar 1268925d1c40SMatt Helsley /* 1269925d1c40SMatt Helsley * We added or removed a vma mapping the executable. The vmas are only mapped 1270925d1c40SMatt Helsley * during exec and are not mapped with the mmap system call. 1271925d1c40SMatt Helsley * Callers must hold down_write() on the mm's mmap_sem for these 1272925d1c40SMatt Helsley */ 1273925d1c40SMatt Helsley void added_exe_file_vma(struct mm_struct *mm) 1274925d1c40SMatt Helsley { 1275925d1c40SMatt Helsley mm->num_exe_file_vmas++; 1276925d1c40SMatt Helsley } 1277925d1c40SMatt Helsley 1278925d1c40SMatt Helsley void removed_exe_file_vma(struct mm_struct *mm) 1279925d1c40SMatt Helsley { 1280925d1c40SMatt Helsley mm->num_exe_file_vmas--; 1281925d1c40SMatt Helsley if ((mm->num_exe_file_vmas == 0) && mm->exe_file){ 1282925d1c40SMatt Helsley fput(mm->exe_file); 1283925d1c40SMatt Helsley mm->exe_file = NULL; 1284925d1c40SMatt Helsley } 1285925d1c40SMatt Helsley 1286925d1c40SMatt Helsley } 1287925d1c40SMatt Helsley 1288925d1c40SMatt Helsley void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file) 1289925d1c40SMatt Helsley { 1290925d1c40SMatt Helsley if (new_exe_file) 1291925d1c40SMatt Helsley get_file(new_exe_file); 1292925d1c40SMatt Helsley if (mm->exe_file) 1293925d1c40SMatt Helsley fput(mm->exe_file); 1294925d1c40SMatt Helsley mm->exe_file = new_exe_file; 1295925d1c40SMatt Helsley mm->num_exe_file_vmas = 0; 1296925d1c40SMatt Helsley } 1297925d1c40SMatt Helsley 1298925d1c40SMatt Helsley struct file *get_mm_exe_file(struct mm_struct *mm) 1299925d1c40SMatt Helsley { 1300925d1c40SMatt Helsley struct file *exe_file; 1301925d1c40SMatt Helsley 1302925d1c40SMatt Helsley /* We need mmap_sem to protect against races with removal of 1303925d1c40SMatt Helsley * VM_EXECUTABLE vmas */ 1304925d1c40SMatt Helsley down_read(&mm->mmap_sem); 1305925d1c40SMatt Helsley exe_file = mm->exe_file; 1306925d1c40SMatt Helsley if (exe_file) 1307925d1c40SMatt Helsley get_file(exe_file); 1308925d1c40SMatt Helsley up_read(&mm->mmap_sem); 1309925d1c40SMatt Helsley return exe_file; 1310925d1c40SMatt Helsley } 1311925d1c40SMatt Helsley 1312925d1c40SMatt Helsley void dup_mm_exe_file(struct mm_struct *oldmm, struct mm_struct *newmm) 1313925d1c40SMatt Helsley { 1314925d1c40SMatt Helsley /* It's safe to write the exe_file pointer without exe_file_lock because 1315925d1c40SMatt Helsley * this is called during fork when the task is not yet in /proc */ 1316925d1c40SMatt Helsley newmm->exe_file = get_mm_exe_file(oldmm); 1317925d1c40SMatt Helsley } 1318925d1c40SMatt Helsley 1319925d1c40SMatt Helsley static int proc_exe_link(struct inode *inode, struct path *exe_path) 1320925d1c40SMatt Helsley { 1321925d1c40SMatt Helsley struct task_struct *task; 1322925d1c40SMatt Helsley struct mm_struct *mm; 1323925d1c40SMatt Helsley struct file *exe_file; 1324925d1c40SMatt Helsley 1325925d1c40SMatt Helsley task = get_proc_task(inode); 1326925d1c40SMatt Helsley if (!task) 1327925d1c40SMatt Helsley return -ENOENT; 1328925d1c40SMatt Helsley mm = get_task_mm(task); 1329925d1c40SMatt Helsley put_task_struct(task); 1330925d1c40SMatt Helsley if (!mm) 1331925d1c40SMatt Helsley return -ENOENT; 1332925d1c40SMatt Helsley exe_file = get_mm_exe_file(mm); 1333925d1c40SMatt Helsley mmput(mm); 1334925d1c40SMatt Helsley if (exe_file) { 1335925d1c40SMatt Helsley *exe_path = exe_file->f_path; 1336925d1c40SMatt Helsley path_get(&exe_file->f_path); 1337925d1c40SMatt Helsley fput(exe_file); 1338925d1c40SMatt Helsley return 0; 1339925d1c40SMatt Helsley } else 1340925d1c40SMatt Helsley return -ENOENT; 1341925d1c40SMatt Helsley } 1342925d1c40SMatt Helsley 1343008b150aSAl Viro static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd) 13441da177e4SLinus Torvalds { 13451da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 13461da177e4SLinus Torvalds int error = -EACCES; 13471da177e4SLinus Torvalds 13481da177e4SLinus Torvalds /* We don't need a base pointer in the /proc filesystem */ 13491d957f9bSJan Blunck path_put(&nd->path); 13501da177e4SLinus Torvalds 1351778c1144SEric W. Biederman /* Are we allowed to snoop on the tasks file descriptors? */ 1352778c1144SEric W. Biederman if (!proc_fd_access_allowed(inode)) 13531da177e4SLinus Torvalds goto out; 13541da177e4SLinus Torvalds 13553dcd25f3SJan Blunck error = PROC_I(inode)->op.proc_get_link(inode, &nd->path); 13561da177e4SLinus Torvalds nd->last_type = LAST_BIND; 13571da177e4SLinus Torvalds out: 1358008b150aSAl Viro return ERR_PTR(error); 13591da177e4SLinus Torvalds } 13601da177e4SLinus Torvalds 13613dcd25f3SJan Blunck static int do_proc_readlink(struct path *path, char __user *buffer, int buflen) 13621da177e4SLinus Torvalds { 1363e12ba74dSMel Gorman char *tmp = (char*)__get_free_page(GFP_TEMPORARY); 13643dcd25f3SJan Blunck char *pathname; 13651da177e4SLinus Torvalds int len; 13661da177e4SLinus Torvalds 13671da177e4SLinus Torvalds if (!tmp) 13681da177e4SLinus Torvalds return -ENOMEM; 13691da177e4SLinus Torvalds 1370cf28b486SJan Blunck pathname = d_path(path, tmp, PAGE_SIZE); 13713dcd25f3SJan Blunck len = PTR_ERR(pathname); 13723dcd25f3SJan Blunck if (IS_ERR(pathname)) 13731da177e4SLinus Torvalds goto out; 13743dcd25f3SJan Blunck len = tmp + PAGE_SIZE - 1 - pathname; 13751da177e4SLinus Torvalds 13761da177e4SLinus Torvalds if (len > buflen) 13771da177e4SLinus Torvalds len = buflen; 13783dcd25f3SJan Blunck if (copy_to_user(buffer, pathname, len)) 13791da177e4SLinus Torvalds len = -EFAULT; 13801da177e4SLinus Torvalds out: 13811da177e4SLinus Torvalds free_page((unsigned long)tmp); 13821da177e4SLinus Torvalds return len; 13831da177e4SLinus Torvalds } 13841da177e4SLinus Torvalds 13851da177e4SLinus Torvalds static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen) 13861da177e4SLinus Torvalds { 13871da177e4SLinus Torvalds int error = -EACCES; 13881da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 13893dcd25f3SJan Blunck struct path path; 13901da177e4SLinus Torvalds 1391778c1144SEric W. Biederman /* Are we allowed to snoop on the tasks file descriptors? */ 1392778c1144SEric W. Biederman if (!proc_fd_access_allowed(inode)) 13931da177e4SLinus Torvalds goto out; 13941da177e4SLinus Torvalds 13953dcd25f3SJan Blunck error = PROC_I(inode)->op.proc_get_link(inode, &path); 13961da177e4SLinus Torvalds if (error) 13971da177e4SLinus Torvalds goto out; 13981da177e4SLinus Torvalds 13993dcd25f3SJan Blunck error = do_proc_readlink(&path, buffer, buflen); 14003dcd25f3SJan Blunck path_put(&path); 14011da177e4SLinus Torvalds out: 14021da177e4SLinus Torvalds return error; 14031da177e4SLinus Torvalds } 14041da177e4SLinus Torvalds 1405c5ef1c42SArjan van de Ven static const struct inode_operations proc_pid_link_inode_operations = { 14061da177e4SLinus Torvalds .readlink = proc_pid_readlink, 14076d76fa58SLinus Torvalds .follow_link = proc_pid_follow_link, 14086d76fa58SLinus Torvalds .setattr = proc_setattr, 14091da177e4SLinus Torvalds }; 14101da177e4SLinus Torvalds 141128a6d671SEric W. Biederman 141228a6d671SEric W. Biederman /* building an inode */ 141328a6d671SEric W. Biederman 141428a6d671SEric W. Biederman static int task_dumpable(struct task_struct *task) 141528a6d671SEric W. Biederman { 141628a6d671SEric W. Biederman int dumpable = 0; 141728a6d671SEric W. Biederman struct mm_struct *mm; 141828a6d671SEric W. Biederman 141928a6d671SEric W. Biederman task_lock(task); 142028a6d671SEric W. Biederman mm = task->mm; 142128a6d671SEric W. Biederman if (mm) 14226c5d5238SKawai, Hidehiro dumpable = get_dumpable(mm); 142328a6d671SEric W. Biederman task_unlock(task); 142428a6d671SEric W. Biederman if(dumpable == 1) 142528a6d671SEric W. Biederman return 1; 142628a6d671SEric W. Biederman return 0; 142728a6d671SEric W. Biederman } 142828a6d671SEric W. Biederman 142928a6d671SEric W. Biederman 143061a28784SEric W. Biederman static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task) 143128a6d671SEric W. Biederman { 143228a6d671SEric W. Biederman struct inode * inode; 143328a6d671SEric W. Biederman struct proc_inode *ei; 1434c69e8d9cSDavid Howells const struct cred *cred; 143528a6d671SEric W. Biederman 143628a6d671SEric W. Biederman /* We need a new inode */ 143728a6d671SEric W. Biederman 143828a6d671SEric W. Biederman inode = new_inode(sb); 143928a6d671SEric W. Biederman if (!inode) 144028a6d671SEric W. Biederman goto out; 144128a6d671SEric W. Biederman 144228a6d671SEric W. Biederman /* Common stuff */ 144328a6d671SEric W. Biederman ei = PROC_I(inode); 144428a6d671SEric W. Biederman inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; 144528a6d671SEric W. Biederman inode->i_op = &proc_def_inode_operations; 144628a6d671SEric W. Biederman 144728a6d671SEric W. Biederman /* 144828a6d671SEric W. Biederman * grab the reference to task. 144928a6d671SEric W. Biederman */ 14501a657f78SOleg Nesterov ei->pid = get_task_pid(task, PIDTYPE_PID); 145128a6d671SEric W. Biederman if (!ei->pid) 145228a6d671SEric W. Biederman goto out_unlock; 145328a6d671SEric W. Biederman 145428a6d671SEric W. Biederman inode->i_uid = 0; 145528a6d671SEric W. Biederman inode->i_gid = 0; 145628a6d671SEric W. Biederman if (task_dumpable(task)) { 1457c69e8d9cSDavid Howells rcu_read_lock(); 1458c69e8d9cSDavid Howells cred = __task_cred(task); 1459c69e8d9cSDavid Howells inode->i_uid = cred->euid; 1460c69e8d9cSDavid Howells inode->i_gid = cred->egid; 1461c69e8d9cSDavid Howells rcu_read_unlock(); 146228a6d671SEric W. Biederman } 146328a6d671SEric W. Biederman security_task_to_inode(task, inode); 146428a6d671SEric W. Biederman 146528a6d671SEric W. Biederman out: 146628a6d671SEric W. Biederman return inode; 146728a6d671SEric W. Biederman 146828a6d671SEric W. Biederman out_unlock: 146928a6d671SEric W. Biederman iput(inode); 147028a6d671SEric W. Biederman return NULL; 147128a6d671SEric W. Biederman } 147228a6d671SEric W. Biederman 147328a6d671SEric W. Biederman static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) 147428a6d671SEric W. Biederman { 147528a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 147628a6d671SEric W. Biederman struct task_struct *task; 1477c69e8d9cSDavid Howells const struct cred *cred; 1478c69e8d9cSDavid Howells 147928a6d671SEric W. Biederman generic_fillattr(inode, stat); 148028a6d671SEric W. Biederman 148128a6d671SEric W. Biederman rcu_read_lock(); 148228a6d671SEric W. Biederman stat->uid = 0; 148328a6d671SEric W. Biederman stat->gid = 0; 148428a6d671SEric W. Biederman task = pid_task(proc_pid(inode), PIDTYPE_PID); 148528a6d671SEric W. Biederman if (task) { 148628a6d671SEric W. Biederman if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) || 148728a6d671SEric W. Biederman task_dumpable(task)) { 1488c69e8d9cSDavid Howells cred = __task_cred(task); 1489c69e8d9cSDavid Howells stat->uid = cred->euid; 1490c69e8d9cSDavid Howells stat->gid = cred->egid; 149128a6d671SEric W. Biederman } 149228a6d671SEric W. Biederman } 149328a6d671SEric W. Biederman rcu_read_unlock(); 149428a6d671SEric W. Biederman return 0; 149528a6d671SEric W. Biederman } 149628a6d671SEric W. Biederman 149728a6d671SEric W. Biederman /* dentry stuff */ 149828a6d671SEric W. Biederman 149928a6d671SEric W. Biederman /* 150028a6d671SEric W. Biederman * Exceptional case: normally we are not allowed to unhash a busy 150128a6d671SEric W. Biederman * directory. In this case, however, we can do it - no aliasing problems 150228a6d671SEric W. Biederman * due to the way we treat inodes. 150328a6d671SEric W. Biederman * 150428a6d671SEric W. Biederman * Rewrite the inode's ownerships here because the owning task may have 150528a6d671SEric W. Biederman * performed a setuid(), etc. 150628a6d671SEric W. Biederman * 150728a6d671SEric W. Biederman * Before the /proc/pid/status file was created the only way to read 150828a6d671SEric W. Biederman * the effective uid of a /process was to stat /proc/pid. Reading 150928a6d671SEric W. Biederman * /proc/pid/status is slow enough that procps and other packages 151028a6d671SEric W. Biederman * kept stating /proc/pid. To keep the rules in /proc simple I have 151128a6d671SEric W. Biederman * made this apply to all per process world readable and executable 151228a6d671SEric W. Biederman * directories. 151328a6d671SEric W. Biederman */ 151428a6d671SEric W. Biederman static int pid_revalidate(struct dentry *dentry, struct nameidata *nd) 151528a6d671SEric W. Biederman { 151628a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 151728a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 1518c69e8d9cSDavid Howells const struct cred *cred; 1519c69e8d9cSDavid Howells 152028a6d671SEric W. Biederman if (task) { 152128a6d671SEric W. Biederman if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) || 152228a6d671SEric W. Biederman task_dumpable(task)) { 1523c69e8d9cSDavid Howells rcu_read_lock(); 1524c69e8d9cSDavid Howells cred = __task_cred(task); 1525c69e8d9cSDavid Howells inode->i_uid = cred->euid; 1526c69e8d9cSDavid Howells inode->i_gid = cred->egid; 1527c69e8d9cSDavid Howells rcu_read_unlock(); 152828a6d671SEric W. Biederman } else { 152928a6d671SEric W. Biederman inode->i_uid = 0; 153028a6d671SEric W. Biederman inode->i_gid = 0; 153128a6d671SEric W. Biederman } 153228a6d671SEric W. Biederman inode->i_mode &= ~(S_ISUID | S_ISGID); 153328a6d671SEric W. Biederman security_task_to_inode(task, inode); 153428a6d671SEric W. Biederman put_task_struct(task); 153528a6d671SEric W. Biederman return 1; 153628a6d671SEric W. Biederman } 153728a6d671SEric W. Biederman d_drop(dentry); 153828a6d671SEric W. Biederman return 0; 153928a6d671SEric W. Biederman } 154028a6d671SEric W. Biederman 154128a6d671SEric W. Biederman static int pid_delete_dentry(struct dentry * dentry) 154228a6d671SEric W. Biederman { 154328a6d671SEric W. Biederman /* Is the task we represent dead? 154428a6d671SEric W. Biederman * If so, then don't put the dentry on the lru list, 154528a6d671SEric W. Biederman * kill it immediately. 154628a6d671SEric W. Biederman */ 154728a6d671SEric W. Biederman return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first; 154828a6d671SEric W. Biederman } 154928a6d671SEric W. Biederman 155028a6d671SEric W. Biederman static struct dentry_operations pid_dentry_operations = 155128a6d671SEric W. Biederman { 155228a6d671SEric W. Biederman .d_revalidate = pid_revalidate, 155328a6d671SEric W. Biederman .d_delete = pid_delete_dentry, 155428a6d671SEric W. Biederman }; 155528a6d671SEric W. Biederman 155628a6d671SEric W. Biederman /* Lookups */ 155728a6d671SEric W. Biederman 1558c5141e6dSEric Dumazet typedef struct dentry *instantiate_t(struct inode *, struct dentry *, 1559c5141e6dSEric Dumazet struct task_struct *, const void *); 156061a28784SEric W. Biederman 15611c0d04c9SEric W. Biederman /* 15621c0d04c9SEric W. Biederman * Fill a directory entry. 15631c0d04c9SEric W. Biederman * 15641c0d04c9SEric W. Biederman * If possible create the dcache entry and derive our inode number and 15651c0d04c9SEric W. Biederman * file type from dcache entry. 15661c0d04c9SEric W. Biederman * 15671c0d04c9SEric W. Biederman * Since all of the proc inode numbers are dynamically generated, the inode 15681c0d04c9SEric W. Biederman * numbers do not exist until the inode is cache. This means creating the 15691c0d04c9SEric W. Biederman * the dcache entry in readdir is necessary to keep the inode numbers 15701c0d04c9SEric W. Biederman * reported by readdir in sync with the inode numbers reported 15711c0d04c9SEric W. Biederman * by stat. 15721c0d04c9SEric W. Biederman */ 157361a28784SEric W. Biederman static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir, 157461a28784SEric W. Biederman char *name, int len, 1575c5141e6dSEric Dumazet instantiate_t instantiate, struct task_struct *task, const void *ptr) 157661a28784SEric W. Biederman { 15772fddfeefSJosef "Jeff" Sipek struct dentry *child, *dir = filp->f_path.dentry; 157861a28784SEric W. Biederman struct inode *inode; 157961a28784SEric W. Biederman struct qstr qname; 158061a28784SEric W. Biederman ino_t ino = 0; 158161a28784SEric W. Biederman unsigned type = DT_UNKNOWN; 158261a28784SEric W. Biederman 158361a28784SEric W. Biederman qname.name = name; 158461a28784SEric W. Biederman qname.len = len; 158561a28784SEric W. Biederman qname.hash = full_name_hash(name, len); 158661a28784SEric W. Biederman 158761a28784SEric W. Biederman child = d_lookup(dir, &qname); 158861a28784SEric W. Biederman if (!child) { 158961a28784SEric W. Biederman struct dentry *new; 159061a28784SEric W. Biederman new = d_alloc(dir, &qname); 159161a28784SEric W. Biederman if (new) { 159261a28784SEric W. Biederman child = instantiate(dir->d_inode, new, task, ptr); 159361a28784SEric W. Biederman if (child) 159461a28784SEric W. Biederman dput(new); 159561a28784SEric W. Biederman else 159661a28784SEric W. Biederman child = new; 159761a28784SEric W. Biederman } 159861a28784SEric W. Biederman } 159961a28784SEric W. Biederman if (!child || IS_ERR(child) || !child->d_inode) 160061a28784SEric W. Biederman goto end_instantiate; 160161a28784SEric W. Biederman inode = child->d_inode; 160261a28784SEric W. Biederman if (inode) { 160361a28784SEric W. Biederman ino = inode->i_ino; 160461a28784SEric W. Biederman type = inode->i_mode >> 12; 160561a28784SEric W. Biederman } 160661a28784SEric W. Biederman dput(child); 160761a28784SEric W. Biederman end_instantiate: 160861a28784SEric W. Biederman if (!ino) 160961a28784SEric W. Biederman ino = find_inode_number(dir, &qname); 161061a28784SEric W. Biederman if (!ino) 161161a28784SEric W. Biederman ino = 1; 161261a28784SEric W. Biederman return filldir(dirent, name, len, filp->f_pos, ino, type); 161361a28784SEric W. Biederman } 161461a28784SEric W. Biederman 161528a6d671SEric W. Biederman static unsigned name_to_int(struct dentry *dentry) 161628a6d671SEric W. Biederman { 161728a6d671SEric W. Biederman const char *name = dentry->d_name.name; 161828a6d671SEric W. Biederman int len = dentry->d_name.len; 161928a6d671SEric W. Biederman unsigned n = 0; 162028a6d671SEric W. Biederman 162128a6d671SEric W. Biederman if (len > 1 && *name == '0') 162228a6d671SEric W. Biederman goto out; 162328a6d671SEric W. Biederman while (len-- > 0) { 162428a6d671SEric W. Biederman unsigned c = *name++ - '0'; 162528a6d671SEric W. Biederman if (c > 9) 162628a6d671SEric W. Biederman goto out; 162728a6d671SEric W. Biederman if (n >= (~0U-9)/10) 162828a6d671SEric W. Biederman goto out; 162928a6d671SEric W. Biederman n *= 10; 163028a6d671SEric W. Biederman n += c; 163128a6d671SEric W. Biederman } 163228a6d671SEric W. Biederman return n; 163328a6d671SEric W. Biederman out: 163428a6d671SEric W. Biederman return ~0U; 163528a6d671SEric W. Biederman } 163628a6d671SEric W. Biederman 163727932742SMiklos Szeredi #define PROC_FDINFO_MAX 64 163827932742SMiklos Szeredi 16393dcd25f3SJan Blunck static int proc_fd_info(struct inode *inode, struct path *path, char *info) 164028a6d671SEric W. Biederman { 164128a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 164228a6d671SEric W. Biederman struct files_struct *files = NULL; 164328a6d671SEric W. Biederman struct file *file; 164428a6d671SEric W. Biederman int fd = proc_fd(inode); 164528a6d671SEric W. Biederman 164628a6d671SEric W. Biederman if (task) { 164728a6d671SEric W. Biederman files = get_files_struct(task); 164828a6d671SEric W. Biederman put_task_struct(task); 164928a6d671SEric W. Biederman } 165028a6d671SEric W. Biederman if (files) { 165128a6d671SEric W. Biederman /* 165228a6d671SEric W. Biederman * We are not taking a ref to the file structure, so we must 165328a6d671SEric W. Biederman * hold ->file_lock. 165428a6d671SEric W. Biederman */ 165528a6d671SEric W. Biederman spin_lock(&files->file_lock); 165628a6d671SEric W. Biederman file = fcheck_files(files, fd); 165728a6d671SEric W. Biederman if (file) { 16583dcd25f3SJan Blunck if (path) { 16593dcd25f3SJan Blunck *path = file->f_path; 16603dcd25f3SJan Blunck path_get(&file->f_path); 16613dcd25f3SJan Blunck } 166227932742SMiklos Szeredi if (info) 166327932742SMiklos Szeredi snprintf(info, PROC_FDINFO_MAX, 166427932742SMiklos Szeredi "pos:\t%lli\n" 166527932742SMiklos Szeredi "flags:\t0%o\n", 166627932742SMiklos Szeredi (long long) file->f_pos, 166727932742SMiklos Szeredi file->f_flags); 166828a6d671SEric W. Biederman spin_unlock(&files->file_lock); 166928a6d671SEric W. Biederman put_files_struct(files); 167028a6d671SEric W. Biederman return 0; 167128a6d671SEric W. Biederman } 167228a6d671SEric W. Biederman spin_unlock(&files->file_lock); 167328a6d671SEric W. Biederman put_files_struct(files); 167428a6d671SEric W. Biederman } 167528a6d671SEric W. Biederman return -ENOENT; 167628a6d671SEric W. Biederman } 167728a6d671SEric W. Biederman 16783dcd25f3SJan Blunck static int proc_fd_link(struct inode *inode, struct path *path) 167927932742SMiklos Szeredi { 16803dcd25f3SJan Blunck return proc_fd_info(inode, path, NULL); 168127932742SMiklos Szeredi } 168227932742SMiklos Szeredi 168328a6d671SEric W. Biederman static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd) 168428a6d671SEric W. Biederman { 168528a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 168628a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 168728a6d671SEric W. Biederman int fd = proc_fd(inode); 168828a6d671SEric W. Biederman struct files_struct *files; 1689c69e8d9cSDavid Howells const struct cred *cred; 169028a6d671SEric W. Biederman 169128a6d671SEric W. Biederman if (task) { 169228a6d671SEric W. Biederman files = get_files_struct(task); 169328a6d671SEric W. Biederman if (files) { 169428a6d671SEric W. Biederman rcu_read_lock(); 169528a6d671SEric W. Biederman if (fcheck_files(files, fd)) { 169628a6d671SEric W. Biederman rcu_read_unlock(); 169728a6d671SEric W. Biederman put_files_struct(files); 169828a6d671SEric W. Biederman if (task_dumpable(task)) { 1699c69e8d9cSDavid Howells rcu_read_lock(); 1700c69e8d9cSDavid Howells cred = __task_cred(task); 1701c69e8d9cSDavid Howells inode->i_uid = cred->euid; 1702c69e8d9cSDavid Howells inode->i_gid = cred->egid; 1703c69e8d9cSDavid Howells rcu_read_unlock(); 170428a6d671SEric W. Biederman } else { 170528a6d671SEric W. Biederman inode->i_uid = 0; 170628a6d671SEric W. Biederman inode->i_gid = 0; 170728a6d671SEric W. Biederman } 170828a6d671SEric W. Biederman inode->i_mode &= ~(S_ISUID | S_ISGID); 170928a6d671SEric W. Biederman security_task_to_inode(task, inode); 171028a6d671SEric W. Biederman put_task_struct(task); 171128a6d671SEric W. Biederman return 1; 171228a6d671SEric W. Biederman } 171328a6d671SEric W. Biederman rcu_read_unlock(); 171428a6d671SEric W. Biederman put_files_struct(files); 171528a6d671SEric W. Biederman } 171628a6d671SEric W. Biederman put_task_struct(task); 171728a6d671SEric W. Biederman } 171828a6d671SEric W. Biederman d_drop(dentry); 171928a6d671SEric W. Biederman return 0; 172028a6d671SEric W. Biederman } 172128a6d671SEric W. Biederman 172228a6d671SEric W. Biederman static struct dentry_operations tid_fd_dentry_operations = 172328a6d671SEric W. Biederman { 172428a6d671SEric W. Biederman .d_revalidate = tid_fd_revalidate, 172528a6d671SEric W. Biederman .d_delete = pid_delete_dentry, 172628a6d671SEric W. Biederman }; 172728a6d671SEric W. Biederman 1728444ceed8SEric W. Biederman static struct dentry *proc_fd_instantiate(struct inode *dir, 1729c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 173028a6d671SEric W. Biederman { 1731c5141e6dSEric Dumazet unsigned fd = *(const unsigned *)ptr; 173228a6d671SEric W. Biederman struct file *file; 173328a6d671SEric W. Biederman struct files_struct *files; 173428a6d671SEric W. Biederman struct inode *inode; 173528a6d671SEric W. Biederman struct proc_inode *ei; 1736444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-ENOENT); 173728a6d671SEric W. Biederman 173861a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 173928a6d671SEric W. Biederman if (!inode) 174028a6d671SEric W. Biederman goto out; 174128a6d671SEric W. Biederman ei = PROC_I(inode); 174228a6d671SEric W. Biederman ei->fd = fd; 174328a6d671SEric W. Biederman files = get_files_struct(task); 174428a6d671SEric W. Biederman if (!files) 1745444ceed8SEric W. Biederman goto out_iput; 174628a6d671SEric W. Biederman inode->i_mode = S_IFLNK; 174728a6d671SEric W. Biederman 174828a6d671SEric W. Biederman /* 174928a6d671SEric W. Biederman * We are not taking a ref to the file structure, so we must 175028a6d671SEric W. Biederman * hold ->file_lock. 175128a6d671SEric W. Biederman */ 175228a6d671SEric W. Biederman spin_lock(&files->file_lock); 175328a6d671SEric W. Biederman file = fcheck_files(files, fd); 175428a6d671SEric W. Biederman if (!file) 1755444ceed8SEric W. Biederman goto out_unlock; 1756aeb5d727SAl Viro if (file->f_mode & FMODE_READ) 175728a6d671SEric W. Biederman inode->i_mode |= S_IRUSR | S_IXUSR; 1758aeb5d727SAl Viro if (file->f_mode & FMODE_WRITE) 175928a6d671SEric W. Biederman inode->i_mode |= S_IWUSR | S_IXUSR; 176028a6d671SEric W. Biederman spin_unlock(&files->file_lock); 176128a6d671SEric W. Biederman put_files_struct(files); 1762444ceed8SEric W. Biederman 176328a6d671SEric W. Biederman inode->i_op = &proc_pid_link_inode_operations; 176428a6d671SEric W. Biederman inode->i_size = 64; 176528a6d671SEric W. Biederman ei->op.proc_get_link = proc_fd_link; 176628a6d671SEric W. Biederman dentry->d_op = &tid_fd_dentry_operations; 176728a6d671SEric W. Biederman d_add(dentry, inode); 176828a6d671SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 176928a6d671SEric W. Biederman if (tid_fd_revalidate(dentry, NULL)) 1770444ceed8SEric W. Biederman error = NULL; 1771444ceed8SEric W. Biederman 1772444ceed8SEric W. Biederman out: 1773444ceed8SEric W. Biederman return error; 1774444ceed8SEric W. Biederman out_unlock: 1775444ceed8SEric W. Biederman spin_unlock(&files->file_lock); 1776444ceed8SEric W. Biederman put_files_struct(files); 1777444ceed8SEric W. Biederman out_iput: 1778444ceed8SEric W. Biederman iput(inode); 1779444ceed8SEric W. Biederman goto out; 1780444ceed8SEric W. Biederman } 1781444ceed8SEric W. Biederman 178227932742SMiklos Szeredi static struct dentry *proc_lookupfd_common(struct inode *dir, 178327932742SMiklos Szeredi struct dentry *dentry, 178427932742SMiklos Szeredi instantiate_t instantiate) 1785444ceed8SEric W. Biederman { 1786444ceed8SEric W. Biederman struct task_struct *task = get_proc_task(dir); 1787444ceed8SEric W. Biederman unsigned fd = name_to_int(dentry); 1788444ceed8SEric W. Biederman struct dentry *result = ERR_PTR(-ENOENT); 1789444ceed8SEric W. Biederman 1790444ceed8SEric W. Biederman if (!task) 1791444ceed8SEric W. Biederman goto out_no_task; 1792444ceed8SEric W. Biederman if (fd == ~0U) 1793444ceed8SEric W. Biederman goto out; 1794444ceed8SEric W. Biederman 179527932742SMiklos Szeredi result = instantiate(dir, dentry, task, &fd); 179628a6d671SEric W. Biederman out: 179728a6d671SEric W. Biederman put_task_struct(task); 179828a6d671SEric W. Biederman out_no_task: 179928a6d671SEric W. Biederman return result; 180028a6d671SEric W. Biederman } 180128a6d671SEric W. Biederman 180227932742SMiklos Szeredi static int proc_readfd_common(struct file * filp, void * dirent, 180327932742SMiklos Szeredi filldir_t filldir, instantiate_t instantiate) 18041da177e4SLinus Torvalds { 18052fddfeefSJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 18065634708bSEric W. Biederman struct inode *inode = dentry->d_inode; 180799f89551SEric W. Biederman struct task_struct *p = get_proc_task(inode); 1808457c2510SPavel Emelyanov unsigned int fd, ino; 18091da177e4SLinus Torvalds int retval; 18101da177e4SLinus Torvalds struct files_struct * files; 18111da177e4SLinus Torvalds 18121da177e4SLinus Torvalds retval = -ENOENT; 181399f89551SEric W. Biederman if (!p) 181499f89551SEric W. Biederman goto out_no_task; 18151da177e4SLinus Torvalds retval = 0; 18161da177e4SLinus Torvalds 18171da177e4SLinus Torvalds fd = filp->f_pos; 18181da177e4SLinus Torvalds switch (fd) { 18191da177e4SLinus Torvalds case 0: 18201da177e4SLinus Torvalds if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0) 18211da177e4SLinus Torvalds goto out; 18221da177e4SLinus Torvalds filp->f_pos++; 18231da177e4SLinus Torvalds case 1: 18245634708bSEric W. Biederman ino = parent_ino(dentry); 18251da177e4SLinus Torvalds if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0) 18261da177e4SLinus Torvalds goto out; 18271da177e4SLinus Torvalds filp->f_pos++; 18281da177e4SLinus Torvalds default: 18291da177e4SLinus Torvalds files = get_files_struct(p); 18301da177e4SLinus Torvalds if (!files) 18311da177e4SLinus Torvalds goto out; 1832b835996fSDipankar Sarma rcu_read_lock(); 18331da177e4SLinus Torvalds for (fd = filp->f_pos-2; 18349b4f526cSAl Viro fd < files_fdtable(files)->max_fds; 18351da177e4SLinus Torvalds fd++, filp->f_pos++) { 183627932742SMiklos Szeredi char name[PROC_NUMBUF]; 183727932742SMiklos Szeredi int len; 18381da177e4SLinus Torvalds 18391da177e4SLinus Torvalds if (!fcheck_files(files, fd)) 18401da177e4SLinus Torvalds continue; 1841b835996fSDipankar Sarma rcu_read_unlock(); 18421da177e4SLinus Torvalds 184327932742SMiklos Szeredi len = snprintf(name, sizeof(name), "%d", fd); 184427932742SMiklos Szeredi if (proc_fill_cache(filp, dirent, filldir, 184527932742SMiklos Szeredi name, len, instantiate, 184627932742SMiklos Szeredi p, &fd) < 0) { 1847b835996fSDipankar Sarma rcu_read_lock(); 18481da177e4SLinus Torvalds break; 18491da177e4SLinus Torvalds } 1850b835996fSDipankar Sarma rcu_read_lock(); 18511da177e4SLinus Torvalds } 1852b835996fSDipankar Sarma rcu_read_unlock(); 18531da177e4SLinus Torvalds put_files_struct(files); 18541da177e4SLinus Torvalds } 18551da177e4SLinus Torvalds out: 185699f89551SEric W. Biederman put_task_struct(p); 185799f89551SEric W. Biederman out_no_task: 18581da177e4SLinus Torvalds return retval; 18591da177e4SLinus Torvalds } 18601da177e4SLinus Torvalds 186127932742SMiklos Szeredi static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry, 186227932742SMiklos Szeredi struct nameidata *nd) 186327932742SMiklos Szeredi { 186427932742SMiklos Szeredi return proc_lookupfd_common(dir, dentry, proc_fd_instantiate); 186527932742SMiklos Szeredi } 186627932742SMiklos Szeredi 186727932742SMiklos Szeredi static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir) 186827932742SMiklos Szeredi { 186927932742SMiklos Szeredi return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate); 187027932742SMiklos Szeredi } 187127932742SMiklos Szeredi 187227932742SMiklos Szeredi static ssize_t proc_fdinfo_read(struct file *file, char __user *buf, 187327932742SMiklos Szeredi size_t len, loff_t *ppos) 187427932742SMiklos Szeredi { 187527932742SMiklos Szeredi char tmp[PROC_FDINFO_MAX]; 18763dcd25f3SJan Blunck int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp); 187727932742SMiklos Szeredi if (!err) 187827932742SMiklos Szeredi err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp)); 187927932742SMiklos Szeredi return err; 188027932742SMiklos Szeredi } 188127932742SMiklos Szeredi 188227932742SMiklos Szeredi static const struct file_operations proc_fdinfo_file_operations = { 188327932742SMiklos Szeredi .open = nonseekable_open, 188427932742SMiklos Szeredi .read = proc_fdinfo_read, 188527932742SMiklos Szeredi }; 188627932742SMiklos Szeredi 188700977a59SArjan van de Ven static const struct file_operations proc_fd_operations = { 18881da177e4SLinus Torvalds .read = generic_read_dir, 18891da177e4SLinus Torvalds .readdir = proc_readfd, 18901da177e4SLinus Torvalds }; 18911da177e4SLinus Torvalds 18921da177e4SLinus Torvalds /* 18938948e11fSAlexey Dobriyan * /proc/pid/fd needs a special permission handler so that a process can still 18948948e11fSAlexey Dobriyan * access /proc/self/fd after it has executed a setuid(). 18958948e11fSAlexey Dobriyan */ 1896e6305c43SAl Viro static int proc_fd_permission(struct inode *inode, int mask) 18978948e11fSAlexey Dobriyan { 18988948e11fSAlexey Dobriyan int rv; 18998948e11fSAlexey Dobriyan 19008948e11fSAlexey Dobriyan rv = generic_permission(inode, mask, NULL); 19018948e11fSAlexey Dobriyan if (rv == 0) 19028948e11fSAlexey Dobriyan return 0; 19038948e11fSAlexey Dobriyan if (task_pid(current) == proc_pid(inode)) 19048948e11fSAlexey Dobriyan rv = 0; 19058948e11fSAlexey Dobriyan return rv; 19068948e11fSAlexey Dobriyan } 19078948e11fSAlexey Dobriyan 19088948e11fSAlexey Dobriyan /* 19091da177e4SLinus Torvalds * proc directories can do almost nothing.. 19101da177e4SLinus Torvalds */ 1911c5ef1c42SArjan van de Ven static const struct inode_operations proc_fd_inode_operations = { 19121da177e4SLinus Torvalds .lookup = proc_lookupfd, 19138948e11fSAlexey Dobriyan .permission = proc_fd_permission, 19146d76fa58SLinus Torvalds .setattr = proc_setattr, 19151da177e4SLinus Torvalds }; 19161da177e4SLinus Torvalds 191727932742SMiklos Szeredi static struct dentry *proc_fdinfo_instantiate(struct inode *dir, 191827932742SMiklos Szeredi struct dentry *dentry, struct task_struct *task, const void *ptr) 191927932742SMiklos Szeredi { 192027932742SMiklos Szeredi unsigned fd = *(unsigned *)ptr; 192127932742SMiklos Szeredi struct inode *inode; 192227932742SMiklos Szeredi struct proc_inode *ei; 192327932742SMiklos Szeredi struct dentry *error = ERR_PTR(-ENOENT); 192427932742SMiklos Szeredi 192527932742SMiklos Szeredi inode = proc_pid_make_inode(dir->i_sb, task); 192627932742SMiklos Szeredi if (!inode) 192727932742SMiklos Szeredi goto out; 192827932742SMiklos Szeredi ei = PROC_I(inode); 192927932742SMiklos Szeredi ei->fd = fd; 193027932742SMiklos Szeredi inode->i_mode = S_IFREG | S_IRUSR; 193127932742SMiklos Szeredi inode->i_fop = &proc_fdinfo_file_operations; 193227932742SMiklos Szeredi dentry->d_op = &tid_fd_dentry_operations; 193327932742SMiklos Szeredi d_add(dentry, inode); 193427932742SMiklos Szeredi /* Close the race of the process dying before we return the dentry */ 193527932742SMiklos Szeredi if (tid_fd_revalidate(dentry, NULL)) 193627932742SMiklos Szeredi error = NULL; 193727932742SMiklos Szeredi 193827932742SMiklos Szeredi out: 193927932742SMiklos Szeredi return error; 194027932742SMiklos Szeredi } 194127932742SMiklos Szeredi 194227932742SMiklos Szeredi static struct dentry *proc_lookupfdinfo(struct inode *dir, 194327932742SMiklos Szeredi struct dentry *dentry, 194427932742SMiklos Szeredi struct nameidata *nd) 194527932742SMiklos Szeredi { 194627932742SMiklos Szeredi return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate); 194727932742SMiklos Szeredi } 194827932742SMiklos Szeredi 194927932742SMiklos Szeredi static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir) 195027932742SMiklos Szeredi { 195127932742SMiklos Szeredi return proc_readfd_common(filp, dirent, filldir, 195227932742SMiklos Szeredi proc_fdinfo_instantiate); 195327932742SMiklos Szeredi } 195427932742SMiklos Szeredi 195527932742SMiklos Szeredi static const struct file_operations proc_fdinfo_operations = { 195627932742SMiklos Szeredi .read = generic_read_dir, 195727932742SMiklos Szeredi .readdir = proc_readfdinfo, 195827932742SMiklos Szeredi }; 195927932742SMiklos Szeredi 196027932742SMiklos Szeredi /* 196127932742SMiklos Szeredi * proc directories can do almost nothing.. 196227932742SMiklos Szeredi */ 196327932742SMiklos Szeredi static const struct inode_operations proc_fdinfo_inode_operations = { 196427932742SMiklos Szeredi .lookup = proc_lookupfdinfo, 196527932742SMiklos Szeredi .setattr = proc_setattr, 196627932742SMiklos Szeredi }; 196727932742SMiklos Szeredi 196827932742SMiklos Szeredi 1969444ceed8SEric W. Biederman static struct dentry *proc_pident_instantiate(struct inode *dir, 1970c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 1971444ceed8SEric W. Biederman { 1972c5141e6dSEric Dumazet const struct pid_entry *p = ptr; 1973444ceed8SEric W. Biederman struct inode *inode; 1974444ceed8SEric W. Biederman struct proc_inode *ei; 1975444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-EINVAL); 1976444ceed8SEric W. Biederman 197761a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 1978444ceed8SEric W. Biederman if (!inode) 1979444ceed8SEric W. Biederman goto out; 1980444ceed8SEric W. Biederman 1981444ceed8SEric W. Biederman ei = PROC_I(inode); 1982444ceed8SEric W. Biederman inode->i_mode = p->mode; 1983444ceed8SEric W. Biederman if (S_ISDIR(inode->i_mode)) 1984444ceed8SEric W. Biederman inode->i_nlink = 2; /* Use getattr to fix if necessary */ 1985444ceed8SEric W. Biederman if (p->iop) 1986444ceed8SEric W. Biederman inode->i_op = p->iop; 1987444ceed8SEric W. Biederman if (p->fop) 1988444ceed8SEric W. Biederman inode->i_fop = p->fop; 1989444ceed8SEric W. Biederman ei->op = p->op; 1990444ceed8SEric W. Biederman dentry->d_op = &pid_dentry_operations; 1991444ceed8SEric W. Biederman d_add(dentry, inode); 1992444ceed8SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 1993444ceed8SEric W. Biederman if (pid_revalidate(dentry, NULL)) 1994444ceed8SEric W. Biederman error = NULL; 1995444ceed8SEric W. Biederman out: 1996444ceed8SEric W. Biederman return error; 1997444ceed8SEric W. Biederman } 1998444ceed8SEric W. Biederman 19991da177e4SLinus Torvalds static struct dentry *proc_pident_lookup(struct inode *dir, 20001da177e4SLinus Torvalds struct dentry *dentry, 2001c5141e6dSEric Dumazet const struct pid_entry *ents, 20027bcd6b0eSEric W. Biederman unsigned int nents) 20031da177e4SLinus Torvalds { 20041da177e4SLinus Torvalds struct inode *inode; 2005cd6a3ce9SEric W. Biederman struct dentry *error; 200699f89551SEric W. Biederman struct task_struct *task = get_proc_task(dir); 2007c5141e6dSEric Dumazet const struct pid_entry *p, *last; 20081da177e4SLinus Torvalds 2009cd6a3ce9SEric W. Biederman error = ERR_PTR(-ENOENT); 20101da177e4SLinus Torvalds inode = NULL; 20111da177e4SLinus Torvalds 201299f89551SEric W. Biederman if (!task) 201399f89551SEric W. Biederman goto out_no_task; 20141da177e4SLinus Torvalds 201520cdc894SEric W. Biederman /* 201620cdc894SEric W. Biederman * Yes, it does not scale. And it should not. Don't add 201720cdc894SEric W. Biederman * new entries into /proc/<tgid>/ without very good reasons. 201820cdc894SEric W. Biederman */ 20197bcd6b0eSEric W. Biederman last = &ents[nents - 1]; 20207bcd6b0eSEric W. Biederman for (p = ents; p <= last; p++) { 20211da177e4SLinus Torvalds if (p->len != dentry->d_name.len) 20221da177e4SLinus Torvalds continue; 20231da177e4SLinus Torvalds if (!memcmp(dentry->d_name.name, p->name, p->len)) 20241da177e4SLinus Torvalds break; 20251da177e4SLinus Torvalds } 20267bcd6b0eSEric W. Biederman if (p > last) 20271da177e4SLinus Torvalds goto out; 20281da177e4SLinus Torvalds 2029444ceed8SEric W. Biederman error = proc_pident_instantiate(dir, dentry, task, p); 20301da177e4SLinus Torvalds out: 203199f89551SEric W. Biederman put_task_struct(task); 203299f89551SEric W. Biederman out_no_task: 2033cd6a3ce9SEric W. Biederman return error; 20341da177e4SLinus Torvalds } 20351da177e4SLinus Torvalds 2036c5141e6dSEric Dumazet static int proc_pident_fill_cache(struct file *filp, void *dirent, 2037c5141e6dSEric Dumazet filldir_t filldir, struct task_struct *task, const struct pid_entry *p) 203861a28784SEric W. Biederman { 203961a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, p->name, p->len, 204061a28784SEric W. Biederman proc_pident_instantiate, task, p); 204161a28784SEric W. Biederman } 204261a28784SEric W. Biederman 204328a6d671SEric W. Biederman static int proc_pident_readdir(struct file *filp, 204428a6d671SEric W. Biederman void *dirent, filldir_t filldir, 2045c5141e6dSEric Dumazet const struct pid_entry *ents, unsigned int nents) 204628a6d671SEric W. Biederman { 204728a6d671SEric W. Biederman int i; 20482fddfeefSJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 204928a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 205028a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 2051c5141e6dSEric Dumazet const struct pid_entry *p, *last; 205228a6d671SEric W. Biederman ino_t ino; 205328a6d671SEric W. Biederman int ret; 205428a6d671SEric W. Biederman 205528a6d671SEric W. Biederman ret = -ENOENT; 205628a6d671SEric W. Biederman if (!task) 205761a28784SEric W. Biederman goto out_no_task; 205828a6d671SEric W. Biederman 205928a6d671SEric W. Biederman ret = 0; 206028a6d671SEric W. Biederman i = filp->f_pos; 206128a6d671SEric W. Biederman switch (i) { 206228a6d671SEric W. Biederman case 0: 206328a6d671SEric W. Biederman ino = inode->i_ino; 206428a6d671SEric W. Biederman if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0) 206528a6d671SEric W. Biederman goto out; 206628a6d671SEric W. Biederman i++; 206728a6d671SEric W. Biederman filp->f_pos++; 206828a6d671SEric W. Biederman /* fall through */ 206928a6d671SEric W. Biederman case 1: 207028a6d671SEric W. Biederman ino = parent_ino(dentry); 207128a6d671SEric W. Biederman if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0) 207228a6d671SEric W. Biederman goto out; 207328a6d671SEric W. Biederman i++; 207428a6d671SEric W. Biederman filp->f_pos++; 207528a6d671SEric W. Biederman /* fall through */ 207628a6d671SEric W. Biederman default: 207728a6d671SEric W. Biederman i -= 2; 207828a6d671SEric W. Biederman if (i >= nents) { 207928a6d671SEric W. Biederman ret = 1; 208028a6d671SEric W. Biederman goto out; 208128a6d671SEric W. Biederman } 208228a6d671SEric W. Biederman p = ents + i; 20837bcd6b0eSEric W. Biederman last = &ents[nents - 1]; 20847bcd6b0eSEric W. Biederman while (p <= last) { 208561a28784SEric W. Biederman if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0) 208628a6d671SEric W. Biederman goto out; 208728a6d671SEric W. Biederman filp->f_pos++; 208828a6d671SEric W. Biederman p++; 208928a6d671SEric W. Biederman } 20901da177e4SLinus Torvalds } 20911da177e4SLinus Torvalds 209228a6d671SEric W. Biederman ret = 1; 209328a6d671SEric W. Biederman out: 209461a28784SEric W. Biederman put_task_struct(task); 209561a28784SEric W. Biederman out_no_task: 209628a6d671SEric W. Biederman return ret; 20971da177e4SLinus Torvalds } 20981da177e4SLinus Torvalds 20991da177e4SLinus Torvalds #ifdef CONFIG_SECURITY 210028a6d671SEric W. Biederman static ssize_t proc_pid_attr_read(struct file * file, char __user * buf, 210128a6d671SEric W. Biederman size_t count, loff_t *ppos) 210228a6d671SEric W. Biederman { 21032fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 210404ff9708SAl Viro char *p = NULL; 210528a6d671SEric W. Biederman ssize_t length; 210628a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 210728a6d671SEric W. Biederman 210828a6d671SEric W. Biederman if (!task) 210904ff9708SAl Viro return -ESRCH; 211028a6d671SEric W. Biederman 211128a6d671SEric W. Biederman length = security_getprocattr(task, 21122fddfeefSJosef "Jeff" Sipek (char*)file->f_path.dentry->d_name.name, 211304ff9708SAl Viro &p); 211428a6d671SEric W. Biederman put_task_struct(task); 211504ff9708SAl Viro if (length > 0) 211604ff9708SAl Viro length = simple_read_from_buffer(buf, count, ppos, p, length); 211704ff9708SAl Viro kfree(p); 211828a6d671SEric W. Biederman return length; 211928a6d671SEric W. Biederman } 212028a6d671SEric W. Biederman 212128a6d671SEric W. Biederman static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf, 212228a6d671SEric W. Biederman size_t count, loff_t *ppos) 212328a6d671SEric W. Biederman { 21242fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 212528a6d671SEric W. Biederman char *page; 212628a6d671SEric W. Biederman ssize_t length; 212728a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 212828a6d671SEric W. Biederman 212928a6d671SEric W. Biederman length = -ESRCH; 213028a6d671SEric W. Biederman if (!task) 213128a6d671SEric W. Biederman goto out_no_task; 213228a6d671SEric W. Biederman if (count > PAGE_SIZE) 213328a6d671SEric W. Biederman count = PAGE_SIZE; 213428a6d671SEric W. Biederman 213528a6d671SEric W. Biederman /* No partial writes. */ 213628a6d671SEric W. Biederman length = -EINVAL; 213728a6d671SEric W. Biederman if (*ppos != 0) 213828a6d671SEric W. Biederman goto out; 213928a6d671SEric W. Biederman 214028a6d671SEric W. Biederman length = -ENOMEM; 2141e12ba74dSMel Gorman page = (char*)__get_free_page(GFP_TEMPORARY); 214228a6d671SEric W. Biederman if (!page) 214328a6d671SEric W. Biederman goto out; 214428a6d671SEric W. Biederman 214528a6d671SEric W. Biederman length = -EFAULT; 214628a6d671SEric W. Biederman if (copy_from_user(page, buf, count)) 214728a6d671SEric W. Biederman goto out_free; 214828a6d671SEric W. Biederman 214928a6d671SEric W. Biederman length = security_setprocattr(task, 21502fddfeefSJosef "Jeff" Sipek (char*)file->f_path.dentry->d_name.name, 215128a6d671SEric W. Biederman (void*)page, count); 215228a6d671SEric W. Biederman out_free: 215328a6d671SEric W. Biederman free_page((unsigned long) page); 215428a6d671SEric W. Biederman out: 215528a6d671SEric W. Biederman put_task_struct(task); 215628a6d671SEric W. Biederman out_no_task: 215728a6d671SEric W. Biederman return length; 215828a6d671SEric W. Biederman } 215928a6d671SEric W. Biederman 216000977a59SArjan van de Ven static const struct file_operations proc_pid_attr_operations = { 216128a6d671SEric W. Biederman .read = proc_pid_attr_read, 216228a6d671SEric W. Biederman .write = proc_pid_attr_write, 216328a6d671SEric W. Biederman }; 216428a6d671SEric W. Biederman 2165c5141e6dSEric Dumazet static const struct pid_entry attr_dir_stuff[] = { 2166631f9c18SAlexey Dobriyan REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations), 2167631f9c18SAlexey Dobriyan REG("prev", S_IRUGO, proc_pid_attr_operations), 2168631f9c18SAlexey Dobriyan REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations), 2169631f9c18SAlexey Dobriyan REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations), 2170631f9c18SAlexey Dobriyan REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations), 2171631f9c18SAlexey Dobriyan REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations), 217228a6d671SEric W. Biederman }; 217328a6d671SEric W. Biederman 217472d9dcfcSEric W. Biederman static int proc_attr_dir_readdir(struct file * filp, 21751da177e4SLinus Torvalds void * dirent, filldir_t filldir) 21761da177e4SLinus Torvalds { 21771da177e4SLinus Torvalds return proc_pident_readdir(filp,dirent,filldir, 217872d9dcfcSEric W. Biederman attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff)); 21791da177e4SLinus Torvalds } 21801da177e4SLinus Torvalds 218100977a59SArjan van de Ven static const struct file_operations proc_attr_dir_operations = { 21821da177e4SLinus Torvalds .read = generic_read_dir, 218372d9dcfcSEric W. Biederman .readdir = proc_attr_dir_readdir, 21841da177e4SLinus Torvalds }; 21851da177e4SLinus Torvalds 218672d9dcfcSEric W. Biederman static struct dentry *proc_attr_dir_lookup(struct inode *dir, 21871da177e4SLinus Torvalds struct dentry *dentry, struct nameidata *nd) 21881da177e4SLinus Torvalds { 21897bcd6b0eSEric W. Biederman return proc_pident_lookup(dir, dentry, 21907bcd6b0eSEric W. Biederman attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff)); 21911da177e4SLinus Torvalds } 21921da177e4SLinus Torvalds 2193c5ef1c42SArjan van de Ven static const struct inode_operations proc_attr_dir_inode_operations = { 219472d9dcfcSEric W. Biederman .lookup = proc_attr_dir_lookup, 219599f89551SEric W. Biederman .getattr = pid_getattr, 21966d76fa58SLinus Torvalds .setattr = proc_setattr, 21971da177e4SLinus Torvalds }; 21981da177e4SLinus Torvalds 21991da177e4SLinus Torvalds #endif 22001da177e4SLinus Torvalds 22013cb4a0bbSKawai, Hidehiro #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE) 22023cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf, 22033cb4a0bbSKawai, Hidehiro size_t count, loff_t *ppos) 22043cb4a0bbSKawai, Hidehiro { 22053cb4a0bbSKawai, Hidehiro struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 22063cb4a0bbSKawai, Hidehiro struct mm_struct *mm; 22073cb4a0bbSKawai, Hidehiro char buffer[PROC_NUMBUF]; 22083cb4a0bbSKawai, Hidehiro size_t len; 22093cb4a0bbSKawai, Hidehiro int ret; 22103cb4a0bbSKawai, Hidehiro 22113cb4a0bbSKawai, Hidehiro if (!task) 22123cb4a0bbSKawai, Hidehiro return -ESRCH; 22133cb4a0bbSKawai, Hidehiro 22143cb4a0bbSKawai, Hidehiro ret = 0; 22153cb4a0bbSKawai, Hidehiro mm = get_task_mm(task); 22163cb4a0bbSKawai, Hidehiro if (mm) { 22173cb4a0bbSKawai, Hidehiro len = snprintf(buffer, sizeof(buffer), "%08lx\n", 22183cb4a0bbSKawai, Hidehiro ((mm->flags & MMF_DUMP_FILTER_MASK) >> 22193cb4a0bbSKawai, Hidehiro MMF_DUMP_FILTER_SHIFT)); 22203cb4a0bbSKawai, Hidehiro mmput(mm); 22213cb4a0bbSKawai, Hidehiro ret = simple_read_from_buffer(buf, count, ppos, buffer, len); 22223cb4a0bbSKawai, Hidehiro } 22233cb4a0bbSKawai, Hidehiro 22243cb4a0bbSKawai, Hidehiro put_task_struct(task); 22253cb4a0bbSKawai, Hidehiro 22263cb4a0bbSKawai, Hidehiro return ret; 22273cb4a0bbSKawai, Hidehiro } 22283cb4a0bbSKawai, Hidehiro 22293cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_write(struct file *file, 22303cb4a0bbSKawai, Hidehiro const char __user *buf, 22313cb4a0bbSKawai, Hidehiro size_t count, 22323cb4a0bbSKawai, Hidehiro loff_t *ppos) 22333cb4a0bbSKawai, Hidehiro { 22343cb4a0bbSKawai, Hidehiro struct task_struct *task; 22353cb4a0bbSKawai, Hidehiro struct mm_struct *mm; 22363cb4a0bbSKawai, Hidehiro char buffer[PROC_NUMBUF], *end; 22373cb4a0bbSKawai, Hidehiro unsigned int val; 22383cb4a0bbSKawai, Hidehiro int ret; 22393cb4a0bbSKawai, Hidehiro int i; 22403cb4a0bbSKawai, Hidehiro unsigned long mask; 22413cb4a0bbSKawai, Hidehiro 22423cb4a0bbSKawai, Hidehiro ret = -EFAULT; 22433cb4a0bbSKawai, Hidehiro memset(buffer, 0, sizeof(buffer)); 22443cb4a0bbSKawai, Hidehiro if (count > sizeof(buffer) - 1) 22453cb4a0bbSKawai, Hidehiro count = sizeof(buffer) - 1; 22463cb4a0bbSKawai, Hidehiro if (copy_from_user(buffer, buf, count)) 22473cb4a0bbSKawai, Hidehiro goto out_no_task; 22483cb4a0bbSKawai, Hidehiro 22493cb4a0bbSKawai, Hidehiro ret = -EINVAL; 22503cb4a0bbSKawai, Hidehiro val = (unsigned int)simple_strtoul(buffer, &end, 0); 22513cb4a0bbSKawai, Hidehiro if (*end == '\n') 22523cb4a0bbSKawai, Hidehiro end++; 22533cb4a0bbSKawai, Hidehiro if (end - buffer == 0) 22543cb4a0bbSKawai, Hidehiro goto out_no_task; 22553cb4a0bbSKawai, Hidehiro 22563cb4a0bbSKawai, Hidehiro ret = -ESRCH; 22573cb4a0bbSKawai, Hidehiro task = get_proc_task(file->f_dentry->d_inode); 22583cb4a0bbSKawai, Hidehiro if (!task) 22593cb4a0bbSKawai, Hidehiro goto out_no_task; 22603cb4a0bbSKawai, Hidehiro 22613cb4a0bbSKawai, Hidehiro ret = end - buffer; 22623cb4a0bbSKawai, Hidehiro mm = get_task_mm(task); 22633cb4a0bbSKawai, Hidehiro if (!mm) 22643cb4a0bbSKawai, Hidehiro goto out_no_mm; 22653cb4a0bbSKawai, Hidehiro 22663cb4a0bbSKawai, Hidehiro for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) { 22673cb4a0bbSKawai, Hidehiro if (val & mask) 22683cb4a0bbSKawai, Hidehiro set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags); 22693cb4a0bbSKawai, Hidehiro else 22703cb4a0bbSKawai, Hidehiro clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags); 22713cb4a0bbSKawai, Hidehiro } 22723cb4a0bbSKawai, Hidehiro 22733cb4a0bbSKawai, Hidehiro mmput(mm); 22743cb4a0bbSKawai, Hidehiro out_no_mm: 22753cb4a0bbSKawai, Hidehiro put_task_struct(task); 22763cb4a0bbSKawai, Hidehiro out_no_task: 22773cb4a0bbSKawai, Hidehiro return ret; 22783cb4a0bbSKawai, Hidehiro } 22793cb4a0bbSKawai, Hidehiro 22803cb4a0bbSKawai, Hidehiro static const struct file_operations proc_coredump_filter_operations = { 22813cb4a0bbSKawai, Hidehiro .read = proc_coredump_filter_read, 22823cb4a0bbSKawai, Hidehiro .write = proc_coredump_filter_write, 22833cb4a0bbSKawai, Hidehiro }; 22843cb4a0bbSKawai, Hidehiro #endif 22853cb4a0bbSKawai, Hidehiro 22861da177e4SLinus Torvalds /* 22871da177e4SLinus Torvalds * /proc/self: 22881da177e4SLinus Torvalds */ 22891da177e4SLinus Torvalds static int proc_self_readlink(struct dentry *dentry, char __user *buffer, 22901da177e4SLinus Torvalds int buflen) 22911da177e4SLinus Torvalds { 2292488e5bc4SEric W. Biederman struct pid_namespace *ns = dentry->d_sb->s_fs_info; 2293b55fcb22SAndrew Morton pid_t tgid = task_tgid_nr_ns(current, ns); 22948578cea7SEric W. Biederman char tmp[PROC_NUMBUF]; 2295b55fcb22SAndrew Morton if (!tgid) 2296488e5bc4SEric W. Biederman return -ENOENT; 2297b55fcb22SAndrew Morton sprintf(tmp, "%d", tgid); 22981da177e4SLinus Torvalds return vfs_readlink(dentry,buffer,buflen,tmp); 22991da177e4SLinus Torvalds } 23001da177e4SLinus Torvalds 2301008b150aSAl Viro static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd) 23021da177e4SLinus Torvalds { 2303488e5bc4SEric W. Biederman struct pid_namespace *ns = dentry->d_sb->s_fs_info; 2304b55fcb22SAndrew Morton pid_t tgid = task_tgid_nr_ns(current, ns); 23058578cea7SEric W. Biederman char tmp[PROC_NUMBUF]; 2306b55fcb22SAndrew Morton if (!tgid) 2307488e5bc4SEric W. Biederman return ERR_PTR(-ENOENT); 2308b55fcb22SAndrew Morton sprintf(tmp, "%d", task_tgid_nr_ns(current, ns)); 2309008b150aSAl Viro return ERR_PTR(vfs_follow_link(nd,tmp)); 23101da177e4SLinus Torvalds } 23111da177e4SLinus Torvalds 2312c5ef1c42SArjan van de Ven static const struct inode_operations proc_self_inode_operations = { 23131da177e4SLinus Torvalds .readlink = proc_self_readlink, 23141da177e4SLinus Torvalds .follow_link = proc_self_follow_link, 23151da177e4SLinus Torvalds }; 23161da177e4SLinus Torvalds 231728a6d671SEric W. Biederman /* 2318801199ceSEric W. Biederman * proc base 2319801199ceSEric W. Biederman * 2320801199ceSEric W. Biederman * These are the directory entries in the root directory of /proc 2321801199ceSEric W. Biederman * that properly belong to the /proc filesystem, as they describe 2322801199ceSEric W. Biederman * describe something that is process related. 2323801199ceSEric W. Biederman */ 2324c5141e6dSEric Dumazet static const struct pid_entry proc_base_stuff[] = { 232561a28784SEric W. Biederman NOD("self", S_IFLNK|S_IRWXUGO, 2326801199ceSEric W. Biederman &proc_self_inode_operations, NULL, {}), 2327801199ceSEric W. Biederman }; 2328801199ceSEric W. Biederman 2329801199ceSEric W. Biederman /* 2330801199ceSEric W. Biederman * Exceptional case: normally we are not allowed to unhash a busy 2331801199ceSEric W. Biederman * directory. In this case, however, we can do it - no aliasing problems 2332801199ceSEric W. Biederman * due to the way we treat inodes. 2333801199ceSEric W. Biederman */ 2334801199ceSEric W. Biederman static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd) 2335801199ceSEric W. Biederman { 2336801199ceSEric W. Biederman struct inode *inode = dentry->d_inode; 2337801199ceSEric W. Biederman struct task_struct *task = get_proc_task(inode); 2338801199ceSEric W. Biederman if (task) { 2339801199ceSEric W. Biederman put_task_struct(task); 2340801199ceSEric W. Biederman return 1; 2341801199ceSEric W. Biederman } 2342801199ceSEric W. Biederman d_drop(dentry); 2343801199ceSEric W. Biederman return 0; 2344801199ceSEric W. Biederman } 2345801199ceSEric W. Biederman 2346801199ceSEric W. Biederman static struct dentry_operations proc_base_dentry_operations = 2347801199ceSEric W. Biederman { 2348801199ceSEric W. Biederman .d_revalidate = proc_base_revalidate, 2349801199ceSEric W. Biederman .d_delete = pid_delete_dentry, 2350801199ceSEric W. Biederman }; 2351801199ceSEric W. Biederman 2352444ceed8SEric W. Biederman static struct dentry *proc_base_instantiate(struct inode *dir, 2353c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 2354801199ceSEric W. Biederman { 2355c5141e6dSEric Dumazet const struct pid_entry *p = ptr; 2356801199ceSEric W. Biederman struct inode *inode; 2357801199ceSEric W. Biederman struct proc_inode *ei; 2358444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-EINVAL); 2359801199ceSEric W. Biederman 2360801199ceSEric W. Biederman /* Allocate the inode */ 2361801199ceSEric W. Biederman error = ERR_PTR(-ENOMEM); 2362801199ceSEric W. Biederman inode = new_inode(dir->i_sb); 2363801199ceSEric W. Biederman if (!inode) 2364801199ceSEric W. Biederman goto out; 2365801199ceSEric W. Biederman 2366801199ceSEric W. Biederman /* Initialize the inode */ 2367801199ceSEric W. Biederman ei = PROC_I(inode); 2368801199ceSEric W. Biederman inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; 2369801199ceSEric W. Biederman 2370801199ceSEric W. Biederman /* 2371801199ceSEric W. Biederman * grab the reference to the task. 2372801199ceSEric W. Biederman */ 23731a657f78SOleg Nesterov ei->pid = get_task_pid(task, PIDTYPE_PID); 2374801199ceSEric W. Biederman if (!ei->pid) 2375801199ceSEric W. Biederman goto out_iput; 2376801199ceSEric W. Biederman 2377801199ceSEric W. Biederman inode->i_uid = 0; 2378801199ceSEric W. Biederman inode->i_gid = 0; 2379801199ceSEric W. Biederman inode->i_mode = p->mode; 2380801199ceSEric W. Biederman if (S_ISDIR(inode->i_mode)) 2381801199ceSEric W. Biederman inode->i_nlink = 2; 2382801199ceSEric W. Biederman if (S_ISLNK(inode->i_mode)) 2383801199ceSEric W. Biederman inode->i_size = 64; 2384801199ceSEric W. Biederman if (p->iop) 2385801199ceSEric W. Biederman inode->i_op = p->iop; 2386801199ceSEric W. Biederman if (p->fop) 2387801199ceSEric W. Biederman inode->i_fop = p->fop; 2388801199ceSEric W. Biederman ei->op = p->op; 2389801199ceSEric W. Biederman dentry->d_op = &proc_base_dentry_operations; 2390801199ceSEric W. Biederman d_add(dentry, inode); 2391801199ceSEric W. Biederman error = NULL; 2392801199ceSEric W. Biederman out: 2393801199ceSEric W. Biederman return error; 2394801199ceSEric W. Biederman out_iput: 2395801199ceSEric W. Biederman iput(inode); 2396801199ceSEric W. Biederman goto out; 2397801199ceSEric W. Biederman } 2398801199ceSEric W. Biederman 2399444ceed8SEric W. Biederman static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry) 2400444ceed8SEric W. Biederman { 2401444ceed8SEric W. Biederman struct dentry *error; 2402444ceed8SEric W. Biederman struct task_struct *task = get_proc_task(dir); 2403c5141e6dSEric Dumazet const struct pid_entry *p, *last; 2404444ceed8SEric W. Biederman 2405444ceed8SEric W. Biederman error = ERR_PTR(-ENOENT); 2406444ceed8SEric W. Biederman 2407444ceed8SEric W. Biederman if (!task) 2408444ceed8SEric W. Biederman goto out_no_task; 2409444ceed8SEric W. Biederman 2410444ceed8SEric W. Biederman /* Lookup the directory entry */ 24117bcd6b0eSEric W. Biederman last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1]; 24127bcd6b0eSEric W. Biederman for (p = proc_base_stuff; p <= last; p++) { 2413444ceed8SEric W. Biederman if (p->len != dentry->d_name.len) 2414444ceed8SEric W. Biederman continue; 2415444ceed8SEric W. Biederman if (!memcmp(dentry->d_name.name, p->name, p->len)) 2416444ceed8SEric W. Biederman break; 2417444ceed8SEric W. Biederman } 24187bcd6b0eSEric W. Biederman if (p > last) 2419444ceed8SEric W. Biederman goto out; 2420444ceed8SEric W. Biederman 2421444ceed8SEric W. Biederman error = proc_base_instantiate(dir, dentry, task, p); 2422444ceed8SEric W. Biederman 2423444ceed8SEric W. Biederman out: 2424444ceed8SEric W. Biederman put_task_struct(task); 2425444ceed8SEric W. Biederman out_no_task: 2426444ceed8SEric W. Biederman return error; 2427444ceed8SEric W. Biederman } 2428444ceed8SEric W. Biederman 2429c5141e6dSEric Dumazet static int proc_base_fill_cache(struct file *filp, void *dirent, 2430c5141e6dSEric Dumazet filldir_t filldir, struct task_struct *task, const struct pid_entry *p) 243161a28784SEric W. Biederman { 243261a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, p->name, p->len, 243361a28784SEric W. Biederman proc_base_instantiate, task, p); 243461a28784SEric W. Biederman } 243561a28784SEric W. Biederman 2436aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING 2437297c5d92SAndrea Righi static int do_io_accounting(struct task_struct *task, char *buffer, int whole) 2438aba76fdbSAndrew Morton { 2439940389b8SAndrea Righi struct task_io_accounting acct = task->ioac; 2440297c5d92SAndrea Righi unsigned long flags; 2441297c5d92SAndrea Righi 24425995477aSAndrea Righi if (whole && lock_task_sighand(task, &flags)) { 2443b2d002dbSAndrea Righi struct task_struct *t = task; 2444297c5d92SAndrea Righi 24455995477aSAndrea Righi task_io_accounting_add(&acct, &task->signal->ioac); 24465995477aSAndrea Righi while_each_thread(task, t) 24475995477aSAndrea Righi task_io_accounting_add(&acct, &t->ioac); 2448297c5d92SAndrea Righi 2449297c5d92SAndrea Righi unlock_task_sighand(task, &flags); 2450297c5d92SAndrea Righi } 2451aba76fdbSAndrew Morton return sprintf(buffer, 2452aba76fdbSAndrew Morton "rchar: %llu\n" 2453aba76fdbSAndrew Morton "wchar: %llu\n" 2454aba76fdbSAndrew Morton "syscr: %llu\n" 2455aba76fdbSAndrew Morton "syscw: %llu\n" 2456aba76fdbSAndrew Morton "read_bytes: %llu\n" 2457aba76fdbSAndrew Morton "write_bytes: %llu\n" 2458aba76fdbSAndrew Morton "cancelled_write_bytes: %llu\n", 24597c44319dSAlexander Beregalov (unsigned long long)acct.rchar, 24607c44319dSAlexander Beregalov (unsigned long long)acct.wchar, 24617c44319dSAlexander Beregalov (unsigned long long)acct.syscr, 24627c44319dSAlexander Beregalov (unsigned long long)acct.syscw, 24637c44319dSAlexander Beregalov (unsigned long long)acct.read_bytes, 24647c44319dSAlexander Beregalov (unsigned long long)acct.write_bytes, 24657c44319dSAlexander Beregalov (unsigned long long)acct.cancelled_write_bytes); 2466aba76fdbSAndrew Morton } 2467297c5d92SAndrea Righi 2468297c5d92SAndrea Righi static int proc_tid_io_accounting(struct task_struct *task, char *buffer) 2469297c5d92SAndrea Righi { 2470297c5d92SAndrea Righi return do_io_accounting(task, buffer, 0); 2471297c5d92SAndrea Righi } 2472297c5d92SAndrea Righi 2473297c5d92SAndrea Righi static int proc_tgid_io_accounting(struct task_struct *task, char *buffer) 2474297c5d92SAndrea Righi { 2475297c5d92SAndrea Righi return do_io_accounting(task, buffer, 1); 2476297c5d92SAndrea Righi } 2477297c5d92SAndrea Righi #endif /* CONFIG_TASK_IO_ACCOUNTING */ 2478aba76fdbSAndrew Morton 247947830723SKees Cook static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns, 248047830723SKees Cook struct pid *pid, struct task_struct *task) 248147830723SKees Cook { 248247830723SKees Cook seq_printf(m, "%08x\n", task->personality); 248347830723SKees Cook return 0; 248447830723SKees Cook } 248547830723SKees Cook 2486801199ceSEric W. Biederman /* 248728a6d671SEric W. Biederman * Thread groups 248828a6d671SEric W. Biederman */ 248900977a59SArjan van de Ven static const struct file_operations proc_task_operations; 2490c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations; 249120cdc894SEric W. Biederman 2492c5141e6dSEric Dumazet static const struct pid_entry tgid_base_stuff[] = { 2493631f9c18SAlexey Dobriyan DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations), 2494631f9c18SAlexey Dobriyan DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations), 2495631f9c18SAlexey Dobriyan DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations), 2496b2211a36SAndrew Morton #ifdef CONFIG_NET 2497631f9c18SAlexey Dobriyan DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations), 2498b2211a36SAndrew Morton #endif 2499631f9c18SAlexey Dobriyan REG("environ", S_IRUSR, proc_environ_operations), 2500631f9c18SAlexey Dobriyan INF("auxv", S_IRUSR, proc_pid_auxv), 2501631f9c18SAlexey Dobriyan ONE("status", S_IRUGO, proc_pid_status), 2502631f9c18SAlexey Dobriyan ONE("personality", S_IRUSR, proc_pid_personality), 2503631f9c18SAlexey Dobriyan INF("limits", S_IRUSR, proc_pid_limits), 250443ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG 2505631f9c18SAlexey Dobriyan REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations), 250643ae34cbSIngo Molnar #endif 2507ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK 2508631f9c18SAlexey Dobriyan INF("syscall", S_IRUSR, proc_pid_syscall), 2509ebcb6734SRoland McGrath #endif 2510631f9c18SAlexey Dobriyan INF("cmdline", S_IRUGO, proc_pid_cmdline), 2511631f9c18SAlexey Dobriyan ONE("stat", S_IRUGO, proc_tgid_stat), 2512631f9c18SAlexey Dobriyan ONE("statm", S_IRUGO, proc_pid_statm), 2513631f9c18SAlexey Dobriyan REG("maps", S_IRUGO, proc_maps_operations), 251428a6d671SEric W. Biederman #ifdef CONFIG_NUMA 2515631f9c18SAlexey Dobriyan REG("numa_maps", S_IRUGO, proc_numa_maps_operations), 251628a6d671SEric W. Biederman #endif 2517631f9c18SAlexey Dobriyan REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations), 2518631f9c18SAlexey Dobriyan LNK("cwd", proc_cwd_link), 2519631f9c18SAlexey Dobriyan LNK("root", proc_root_link), 2520631f9c18SAlexey Dobriyan LNK("exe", proc_exe_link), 2521631f9c18SAlexey Dobriyan REG("mounts", S_IRUGO, proc_mounts_operations), 2522631f9c18SAlexey Dobriyan REG("mountinfo", S_IRUGO, proc_mountinfo_operations), 2523631f9c18SAlexey Dobriyan REG("mountstats", S_IRUSR, proc_mountstats_operations), 25241e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR 2525631f9c18SAlexey Dobriyan REG("clear_refs", S_IWUSR, proc_clear_refs_operations), 2526631f9c18SAlexey Dobriyan REG("smaps", S_IRUGO, proc_smaps_operations), 2527631f9c18SAlexey Dobriyan REG("pagemap", S_IRUSR, proc_pagemap_operations), 252828a6d671SEric W. Biederman #endif 252928a6d671SEric W. Biederman #ifdef CONFIG_SECURITY 2530631f9c18SAlexey Dobriyan DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations), 253128a6d671SEric W. Biederman #endif 253228a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS 2533631f9c18SAlexey Dobriyan INF("wchan", S_IRUGO, proc_pid_wchan), 253428a6d671SEric W. Biederman #endif 2535*2ec220e2SKen Chen #ifdef CONFIG_STACKTRACE 2536*2ec220e2SKen Chen ONE("stack", S_IRUSR, proc_pid_stack), 2537*2ec220e2SKen Chen #endif 253828a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS 2539631f9c18SAlexey Dobriyan INF("schedstat", S_IRUGO, proc_pid_schedstat), 254028a6d671SEric W. Biederman #endif 25419745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP 2542631f9c18SAlexey Dobriyan REG("latency", S_IRUGO, proc_lstats_operations), 25439745512cSArjan van de Ven #endif 25448793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET 2545631f9c18SAlexey Dobriyan REG("cpuset", S_IRUGO, proc_cpuset_operations), 254628a6d671SEric W. Biederman #endif 2547a424316cSPaul Menage #ifdef CONFIG_CGROUPS 2548631f9c18SAlexey Dobriyan REG("cgroup", S_IRUGO, proc_cgroup_operations), 2549a424316cSPaul Menage #endif 2550631f9c18SAlexey Dobriyan INF("oom_score", S_IRUGO, proc_oom_score), 2551631f9c18SAlexey Dobriyan REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations), 255228a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL 2553631f9c18SAlexey Dobriyan REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations), 2554631f9c18SAlexey Dobriyan REG("sessionid", S_IRUGO, proc_sessionid_operations), 255528a6d671SEric W. Biederman #endif 2556f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION 2557631f9c18SAlexey Dobriyan REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations), 2558f4f154fdSAkinobu Mita #endif 25593cb4a0bbSKawai, Hidehiro #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE) 2560631f9c18SAlexey Dobriyan REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations), 25613cb4a0bbSKawai, Hidehiro #endif 2562aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING 2563631f9c18SAlexey Dobriyan INF("io", S_IRUGO, proc_tgid_io_accounting), 2564aba76fdbSAndrew Morton #endif 256528a6d671SEric W. Biederman }; 256628a6d671SEric W. Biederman 256728a6d671SEric W. Biederman static int proc_tgid_base_readdir(struct file * filp, 256828a6d671SEric W. Biederman void * dirent, filldir_t filldir) 256928a6d671SEric W. Biederman { 257028a6d671SEric W. Biederman return proc_pident_readdir(filp,dirent,filldir, 257128a6d671SEric W. Biederman tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff)); 257228a6d671SEric W. Biederman } 257328a6d671SEric W. Biederman 257400977a59SArjan van de Ven static const struct file_operations proc_tgid_base_operations = { 257528a6d671SEric W. Biederman .read = generic_read_dir, 257628a6d671SEric W. Biederman .readdir = proc_tgid_base_readdir, 257728a6d671SEric W. Biederman }; 257828a6d671SEric W. Biederman 257928a6d671SEric W. Biederman static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){ 25807bcd6b0eSEric W. Biederman return proc_pident_lookup(dir, dentry, 25817bcd6b0eSEric W. Biederman tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff)); 258228a6d671SEric W. Biederman } 258328a6d671SEric W. Biederman 2584c5ef1c42SArjan van de Ven static const struct inode_operations proc_tgid_base_inode_operations = { 258528a6d671SEric W. Biederman .lookup = proc_tgid_base_lookup, 258628a6d671SEric W. Biederman .getattr = pid_getattr, 258728a6d671SEric W. Biederman .setattr = proc_setattr, 258828a6d671SEric W. Biederman }; 258928a6d671SEric W. Biederman 259060347f67SPavel Emelyanov static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid) 25911da177e4SLinus Torvalds { 259248e6484dSEric W. Biederman struct dentry *dentry, *leader, *dir; 25938578cea7SEric W. Biederman char buf[PROC_NUMBUF]; 259448e6484dSEric W. Biederman struct qstr name; 25951da177e4SLinus Torvalds 259648e6484dSEric W. Biederman name.name = buf; 259760347f67SPavel Emelyanov name.len = snprintf(buf, sizeof(buf), "%d", pid); 259860347f67SPavel Emelyanov dentry = d_hash_and_lookup(mnt->mnt_root, &name); 259948e6484dSEric W. Biederman if (dentry) { 26007766755aSAndrea Arcangeli if (!(current->flags & PF_EXITING)) 260148e6484dSEric W. Biederman shrink_dcache_parent(dentry); 260248e6484dSEric W. Biederman d_drop(dentry); 260348e6484dSEric W. Biederman dput(dentry); 26041da177e4SLinus Torvalds } 26051da177e4SLinus Torvalds 260660347f67SPavel Emelyanov if (tgid == 0) 260748e6484dSEric W. Biederman goto out; 26081da177e4SLinus Torvalds 260948e6484dSEric W. Biederman name.name = buf; 261060347f67SPavel Emelyanov name.len = snprintf(buf, sizeof(buf), "%d", tgid); 261160347f67SPavel Emelyanov leader = d_hash_and_lookup(mnt->mnt_root, &name); 261248e6484dSEric W. Biederman if (!leader) 261348e6484dSEric W. Biederman goto out; 261448e6484dSEric W. Biederman 261548e6484dSEric W. Biederman name.name = "task"; 261648e6484dSEric W. Biederman name.len = strlen(name.name); 261748e6484dSEric W. Biederman dir = d_hash_and_lookup(leader, &name); 261848e6484dSEric W. Biederman if (!dir) 261948e6484dSEric W. Biederman goto out_put_leader; 262048e6484dSEric W. Biederman 262148e6484dSEric W. Biederman name.name = buf; 262260347f67SPavel Emelyanov name.len = snprintf(buf, sizeof(buf), "%d", pid); 262348e6484dSEric W. Biederman dentry = d_hash_and_lookup(dir, &name); 262448e6484dSEric W. Biederman if (dentry) { 262548e6484dSEric W. Biederman shrink_dcache_parent(dentry); 262648e6484dSEric W. Biederman d_drop(dentry); 262748e6484dSEric W. Biederman dput(dentry); 26281da177e4SLinus Torvalds } 262948e6484dSEric W. Biederman 263048e6484dSEric W. Biederman dput(dir); 263148e6484dSEric W. Biederman out_put_leader: 263248e6484dSEric W. Biederman dput(leader); 263348e6484dSEric W. Biederman out: 263448e6484dSEric W. Biederman return; 26351da177e4SLinus Torvalds } 26361da177e4SLinus Torvalds 26370895e91dSRandy Dunlap /** 26380895e91dSRandy Dunlap * proc_flush_task - Remove dcache entries for @task from the /proc dcache. 26390895e91dSRandy Dunlap * @task: task that should be flushed. 26400895e91dSRandy Dunlap * 26410895e91dSRandy Dunlap * When flushing dentries from proc, one needs to flush them from global 264260347f67SPavel Emelyanov * proc (proc_mnt) and from all the namespaces' procs this task was seen 26430895e91dSRandy Dunlap * in. This call is supposed to do all of this job. 26440895e91dSRandy Dunlap * 26450895e91dSRandy Dunlap * Looks in the dcache for 26460895e91dSRandy Dunlap * /proc/@pid 26470895e91dSRandy Dunlap * /proc/@tgid/task/@pid 26480895e91dSRandy Dunlap * if either directory is present flushes it and all of it'ts children 26490895e91dSRandy Dunlap * from the dcache. 26500895e91dSRandy Dunlap * 26510895e91dSRandy Dunlap * It is safe and reasonable to cache /proc entries for a task until 26520895e91dSRandy Dunlap * that task exits. After that they just clog up the dcache with 26530895e91dSRandy Dunlap * useless entries, possibly causing useful dcache entries to be 26540895e91dSRandy Dunlap * flushed instead. This routine is proved to flush those useless 26550895e91dSRandy Dunlap * dcache entries at process exit time. 26560895e91dSRandy Dunlap * 26570895e91dSRandy Dunlap * NOTE: This routine is just an optimization so it does not guarantee 26580895e91dSRandy Dunlap * that no dcache entries will exist at process exit time it 26590895e91dSRandy Dunlap * just makes it very unlikely that any will persist. 266060347f67SPavel Emelyanov */ 266160347f67SPavel Emelyanov 266260347f67SPavel Emelyanov void proc_flush_task(struct task_struct *task) 266360347f67SPavel Emelyanov { 26649fcc2d15SEric W. Biederman int i; 26659fcc2d15SEric W. Biederman struct pid *pid, *tgid = NULL; 2666130f77ecSPavel Emelyanov struct upid *upid; 2667130f77ecSPavel Emelyanov 2668130f77ecSPavel Emelyanov pid = task_pid(task); 26699fcc2d15SEric W. Biederman if (thread_group_leader(task)) 2670130f77ecSPavel Emelyanov tgid = task_tgid(task); 26719fcc2d15SEric W. Biederman 26729fcc2d15SEric W. Biederman for (i = 0; i <= pid->level; i++) { 2673130f77ecSPavel Emelyanov upid = &pid->numbers[i]; 2674130f77ecSPavel Emelyanov proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr, 26759fcc2d15SEric W. Biederman tgid ? tgid->numbers[i].nr : 0); 2676130f77ecSPavel Emelyanov } 26776f4e6433SPavel Emelyanov 26786f4e6433SPavel Emelyanov upid = &pid->numbers[pid->level]; 26796f4e6433SPavel Emelyanov if (upid->nr == 1) 26806f4e6433SPavel Emelyanov pid_ns_release_proc(upid->ns); 268160347f67SPavel Emelyanov } 268260347f67SPavel Emelyanov 26839711ef99SAdrian Bunk static struct dentry *proc_pid_instantiate(struct inode *dir, 26849711ef99SAdrian Bunk struct dentry * dentry, 2685c5141e6dSEric Dumazet struct task_struct *task, const void *ptr) 2686444ceed8SEric W. Biederman { 2687444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-ENOENT); 2688444ceed8SEric W. Biederman struct inode *inode; 2689444ceed8SEric W. Biederman 269061a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 2691444ceed8SEric W. Biederman if (!inode) 2692444ceed8SEric W. Biederman goto out; 2693444ceed8SEric W. Biederman 2694444ceed8SEric W. Biederman inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO; 2695444ceed8SEric W. Biederman inode->i_op = &proc_tgid_base_inode_operations; 2696444ceed8SEric W. Biederman inode->i_fop = &proc_tgid_base_operations; 2697444ceed8SEric W. Biederman inode->i_flags|=S_IMMUTABLE; 2698aed54175SVegard Nossum 2699aed54175SVegard Nossum inode->i_nlink = 2 + pid_entry_count_dirs(tgid_base_stuff, 2700aed54175SVegard Nossum ARRAY_SIZE(tgid_base_stuff)); 2701444ceed8SEric W. Biederman 2702444ceed8SEric W. Biederman dentry->d_op = &pid_dentry_operations; 2703444ceed8SEric W. Biederman 2704444ceed8SEric W. Biederman d_add(dentry, inode); 2705444ceed8SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 2706444ceed8SEric W. Biederman if (pid_revalidate(dentry, NULL)) 2707444ceed8SEric W. Biederman error = NULL; 2708444ceed8SEric W. Biederman out: 2709444ceed8SEric W. Biederman return error; 2710444ceed8SEric W. Biederman } 2711444ceed8SEric W. Biederman 27121da177e4SLinus Torvalds struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd) 27131da177e4SLinus Torvalds { 2714cd6a3ce9SEric W. Biederman struct dentry *result = ERR_PTR(-ENOENT); 27151da177e4SLinus Torvalds struct task_struct *task; 27161da177e4SLinus Torvalds unsigned tgid; 2717b488893aSPavel Emelyanov struct pid_namespace *ns; 27181da177e4SLinus Torvalds 2719801199ceSEric W. Biederman result = proc_base_lookup(dir, dentry); 2720801199ceSEric W. Biederman if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT) 2721801199ceSEric W. Biederman goto out; 2722801199ceSEric W. Biederman 27231da177e4SLinus Torvalds tgid = name_to_int(dentry); 27241da177e4SLinus Torvalds if (tgid == ~0U) 27251da177e4SLinus Torvalds goto out; 27261da177e4SLinus Torvalds 2727b488893aSPavel Emelyanov ns = dentry->d_sb->s_fs_info; 2728de758734SEric W. Biederman rcu_read_lock(); 2729b488893aSPavel Emelyanov task = find_task_by_pid_ns(tgid, ns); 27301da177e4SLinus Torvalds if (task) 27311da177e4SLinus Torvalds get_task_struct(task); 2732de758734SEric W. Biederman rcu_read_unlock(); 27331da177e4SLinus Torvalds if (!task) 27341da177e4SLinus Torvalds goto out; 27351da177e4SLinus Torvalds 2736444ceed8SEric W. Biederman result = proc_pid_instantiate(dir, dentry, task, NULL); 273748e6484dSEric W. Biederman put_task_struct(task); 27381da177e4SLinus Torvalds out: 2739cd6a3ce9SEric W. Biederman return result; 27401da177e4SLinus Torvalds } 27411da177e4SLinus Torvalds 27421da177e4SLinus Torvalds /* 27430804ef4bSEric W. Biederman * Find the first task with tgid >= tgid 27440bc58a91SEric W. Biederman * 27451da177e4SLinus Torvalds */ 274619fd4bb2SEric W. Biederman struct tgid_iter { 274719fd4bb2SEric W. Biederman unsigned int tgid; 27480804ef4bSEric W. Biederman struct task_struct *task; 274919fd4bb2SEric W. Biederman }; 275019fd4bb2SEric W. Biederman static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter) 275119fd4bb2SEric W. Biederman { 27520804ef4bSEric W. Biederman struct pid *pid; 27531da177e4SLinus Torvalds 275419fd4bb2SEric W. Biederman if (iter.task) 275519fd4bb2SEric W. Biederman put_task_struct(iter.task); 27560804ef4bSEric W. Biederman rcu_read_lock(); 27570804ef4bSEric W. Biederman retry: 275819fd4bb2SEric W. Biederman iter.task = NULL; 275919fd4bb2SEric W. Biederman pid = find_ge_pid(iter.tgid, ns); 27600804ef4bSEric W. Biederman if (pid) { 276119fd4bb2SEric W. Biederman iter.tgid = pid_nr_ns(pid, ns); 276219fd4bb2SEric W. Biederman iter.task = pid_task(pid, PIDTYPE_PID); 27630804ef4bSEric W. Biederman /* What we to know is if the pid we have find is the 27640804ef4bSEric W. Biederman * pid of a thread_group_leader. Testing for task 27650804ef4bSEric W. Biederman * being a thread_group_leader is the obvious thing 27660804ef4bSEric W. Biederman * todo but there is a window when it fails, due to 27670804ef4bSEric W. Biederman * the pid transfer logic in de_thread. 27680804ef4bSEric W. Biederman * 27690804ef4bSEric W. Biederman * So we perform the straight forward test of seeing 27700804ef4bSEric W. Biederman * if the pid we have found is the pid of a thread 27710804ef4bSEric W. Biederman * group leader, and don't worry if the task we have 27720804ef4bSEric W. Biederman * found doesn't happen to be a thread group leader. 27730804ef4bSEric W. Biederman * As we don't care in the case of readdir. 27740bc58a91SEric W. Biederman */ 277519fd4bb2SEric W. Biederman if (!iter.task || !has_group_leader_pid(iter.task)) { 277619fd4bb2SEric W. Biederman iter.tgid += 1; 27770804ef4bSEric W. Biederman goto retry; 277819fd4bb2SEric W. Biederman } 277919fd4bb2SEric W. Biederman get_task_struct(iter.task); 27801da177e4SLinus Torvalds } 2781454cc105SEric W. Biederman rcu_read_unlock(); 278219fd4bb2SEric W. Biederman return iter; 27831da177e4SLinus Torvalds } 27841da177e4SLinus Torvalds 27857bcd6b0eSEric W. Biederman #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff)) 27861da177e4SLinus Torvalds 278761a28784SEric W. Biederman static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir, 278819fd4bb2SEric W. Biederman struct tgid_iter iter) 278961a28784SEric W. Biederman { 279061a28784SEric W. Biederman char name[PROC_NUMBUF]; 279119fd4bb2SEric W. Biederman int len = snprintf(name, sizeof(name), "%d", iter.tgid); 279261a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, name, len, 279319fd4bb2SEric W. Biederman proc_pid_instantiate, iter.task, NULL); 279461a28784SEric W. Biederman } 279561a28784SEric W. Biederman 27961da177e4SLinus Torvalds /* for the /proc/ directory itself, after non-process stuff has been done */ 27971da177e4SLinus Torvalds int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir) 27981da177e4SLinus Torvalds { 27991da177e4SLinus Torvalds unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY; 28002fddfeefSJosef "Jeff" Sipek struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode); 280119fd4bb2SEric W. Biederman struct tgid_iter iter; 2802b488893aSPavel Emelyanov struct pid_namespace *ns; 28031da177e4SLinus Torvalds 280461a28784SEric W. Biederman if (!reaper) 280561a28784SEric W. Biederman goto out_no_task; 280661a28784SEric W. Biederman 28077bcd6b0eSEric W. Biederman for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) { 2808c5141e6dSEric Dumazet const struct pid_entry *p = &proc_base_stuff[nr]; 280961a28784SEric W. Biederman if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0) 2810801199ceSEric W. Biederman goto out; 28111da177e4SLinus Torvalds } 28121da177e4SLinus Torvalds 2813b488893aSPavel Emelyanov ns = filp->f_dentry->d_sb->s_fs_info; 281419fd4bb2SEric W. Biederman iter.task = NULL; 281519fd4bb2SEric W. Biederman iter.tgid = filp->f_pos - TGID_OFFSET; 281619fd4bb2SEric W. Biederman for (iter = next_tgid(ns, iter); 281719fd4bb2SEric W. Biederman iter.task; 281819fd4bb2SEric W. Biederman iter.tgid += 1, iter = next_tgid(ns, iter)) { 281919fd4bb2SEric W. Biederman filp->f_pos = iter.tgid + TGID_OFFSET; 282019fd4bb2SEric W. Biederman if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) { 282119fd4bb2SEric W. Biederman put_task_struct(iter.task); 28220804ef4bSEric W. Biederman goto out; 28231da177e4SLinus Torvalds } 28241da177e4SLinus Torvalds } 28250804ef4bSEric W. Biederman filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET; 28260804ef4bSEric W. Biederman out: 282761a28784SEric W. Biederman put_task_struct(reaper); 282861a28784SEric W. Biederman out_no_task: 28291da177e4SLinus Torvalds return 0; 28301da177e4SLinus Torvalds } 28311da177e4SLinus Torvalds 28320bc58a91SEric W. Biederman /* 283328a6d671SEric W. Biederman * Tasks 283428a6d671SEric W. Biederman */ 2835c5141e6dSEric Dumazet static const struct pid_entry tid_base_stuff[] = { 2836631f9c18SAlexey Dobriyan DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations), 2837631f9c18SAlexey Dobriyan DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fd_operations), 2838631f9c18SAlexey Dobriyan REG("environ", S_IRUSR, proc_environ_operations), 2839631f9c18SAlexey Dobriyan INF("auxv", S_IRUSR, proc_pid_auxv), 2840631f9c18SAlexey Dobriyan ONE("status", S_IRUGO, proc_pid_status), 2841631f9c18SAlexey Dobriyan ONE("personality", S_IRUSR, proc_pid_personality), 2842631f9c18SAlexey Dobriyan INF("limits", S_IRUSR, proc_pid_limits), 284343ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG 2844631f9c18SAlexey Dobriyan REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations), 284543ae34cbSIngo Molnar #endif 2846ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK 2847631f9c18SAlexey Dobriyan INF("syscall", S_IRUSR, proc_pid_syscall), 2848ebcb6734SRoland McGrath #endif 2849631f9c18SAlexey Dobriyan INF("cmdline", S_IRUGO, proc_pid_cmdline), 2850631f9c18SAlexey Dobriyan ONE("stat", S_IRUGO, proc_tid_stat), 2851631f9c18SAlexey Dobriyan ONE("statm", S_IRUGO, proc_pid_statm), 2852631f9c18SAlexey Dobriyan REG("maps", S_IRUGO, proc_maps_operations), 285328a6d671SEric W. Biederman #ifdef CONFIG_NUMA 2854631f9c18SAlexey Dobriyan REG("numa_maps", S_IRUGO, proc_numa_maps_operations), 285528a6d671SEric W. Biederman #endif 2856631f9c18SAlexey Dobriyan REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations), 2857631f9c18SAlexey Dobriyan LNK("cwd", proc_cwd_link), 2858631f9c18SAlexey Dobriyan LNK("root", proc_root_link), 2859631f9c18SAlexey Dobriyan LNK("exe", proc_exe_link), 2860631f9c18SAlexey Dobriyan REG("mounts", S_IRUGO, proc_mounts_operations), 2861631f9c18SAlexey Dobriyan REG("mountinfo", S_IRUGO, proc_mountinfo_operations), 28621e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR 2863631f9c18SAlexey Dobriyan REG("clear_refs", S_IWUSR, proc_clear_refs_operations), 2864631f9c18SAlexey Dobriyan REG("smaps", S_IRUGO, proc_smaps_operations), 2865631f9c18SAlexey Dobriyan REG("pagemap", S_IRUSR, proc_pagemap_operations), 286628a6d671SEric W. Biederman #endif 286728a6d671SEric W. Biederman #ifdef CONFIG_SECURITY 2868631f9c18SAlexey Dobriyan DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations), 286928a6d671SEric W. Biederman #endif 287028a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS 2871631f9c18SAlexey Dobriyan INF("wchan", S_IRUGO, proc_pid_wchan), 287228a6d671SEric W. Biederman #endif 2873*2ec220e2SKen Chen #ifdef CONFIG_STACKTRACE 2874*2ec220e2SKen Chen ONE("stack", S_IRUSR, proc_pid_stack), 2875*2ec220e2SKen Chen #endif 287628a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS 2877631f9c18SAlexey Dobriyan INF("schedstat", S_IRUGO, proc_pid_schedstat), 287828a6d671SEric W. Biederman #endif 28799745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP 2880631f9c18SAlexey Dobriyan REG("latency", S_IRUGO, proc_lstats_operations), 28819745512cSArjan van de Ven #endif 28828793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET 2883631f9c18SAlexey Dobriyan REG("cpuset", S_IRUGO, proc_cpuset_operations), 288428a6d671SEric W. Biederman #endif 2885a424316cSPaul Menage #ifdef CONFIG_CGROUPS 2886631f9c18SAlexey Dobriyan REG("cgroup", S_IRUGO, proc_cgroup_operations), 2887a424316cSPaul Menage #endif 2888631f9c18SAlexey Dobriyan INF("oom_score", S_IRUGO, proc_oom_score), 2889631f9c18SAlexey Dobriyan REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations), 289028a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL 2891631f9c18SAlexey Dobriyan REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations), 2892631f9c18SAlexey Dobriyan REG("sessionid", S_IRUSR, proc_sessionid_operations), 289328a6d671SEric W. Biederman #endif 2894f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION 2895631f9c18SAlexey Dobriyan REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations), 2896f4f154fdSAkinobu Mita #endif 2897297c5d92SAndrea Righi #ifdef CONFIG_TASK_IO_ACCOUNTING 2898631f9c18SAlexey Dobriyan INF("io", S_IRUGO, proc_tid_io_accounting), 2899297c5d92SAndrea Righi #endif 290028a6d671SEric W. Biederman }; 290128a6d671SEric W. Biederman 290228a6d671SEric W. Biederman static int proc_tid_base_readdir(struct file * filp, 290328a6d671SEric W. Biederman void * dirent, filldir_t filldir) 290428a6d671SEric W. Biederman { 290528a6d671SEric W. Biederman return proc_pident_readdir(filp,dirent,filldir, 290628a6d671SEric W. Biederman tid_base_stuff,ARRAY_SIZE(tid_base_stuff)); 290728a6d671SEric W. Biederman } 290828a6d671SEric W. Biederman 290928a6d671SEric W. Biederman static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){ 29107bcd6b0eSEric W. Biederman return proc_pident_lookup(dir, dentry, 29117bcd6b0eSEric W. Biederman tid_base_stuff, ARRAY_SIZE(tid_base_stuff)); 291228a6d671SEric W. Biederman } 291328a6d671SEric W. Biederman 291400977a59SArjan van de Ven static const struct file_operations proc_tid_base_operations = { 291528a6d671SEric W. Biederman .read = generic_read_dir, 291628a6d671SEric W. Biederman .readdir = proc_tid_base_readdir, 291728a6d671SEric W. Biederman }; 291828a6d671SEric W. Biederman 2919c5ef1c42SArjan van de Ven static const struct inode_operations proc_tid_base_inode_operations = { 292028a6d671SEric W. Biederman .lookup = proc_tid_base_lookup, 292128a6d671SEric W. Biederman .getattr = pid_getattr, 292228a6d671SEric W. Biederman .setattr = proc_setattr, 292328a6d671SEric W. Biederman }; 292428a6d671SEric W. Biederman 2925444ceed8SEric W. Biederman static struct dentry *proc_task_instantiate(struct inode *dir, 2926c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 2927444ceed8SEric W. Biederman { 2928444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-ENOENT); 2929444ceed8SEric W. Biederman struct inode *inode; 293061a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 2931444ceed8SEric W. Biederman 2932444ceed8SEric W. Biederman if (!inode) 2933444ceed8SEric W. Biederman goto out; 2934444ceed8SEric W. Biederman inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO; 2935444ceed8SEric W. Biederman inode->i_op = &proc_tid_base_inode_operations; 2936444ceed8SEric W. Biederman inode->i_fop = &proc_tid_base_operations; 2937444ceed8SEric W. Biederman inode->i_flags|=S_IMMUTABLE; 2938aed54175SVegard Nossum 2939aed54175SVegard Nossum inode->i_nlink = 2 + pid_entry_count_dirs(tid_base_stuff, 2940aed54175SVegard Nossum ARRAY_SIZE(tid_base_stuff)); 2941444ceed8SEric W. Biederman 2942444ceed8SEric W. Biederman dentry->d_op = &pid_dentry_operations; 2943444ceed8SEric W. Biederman 2944444ceed8SEric W. Biederman d_add(dentry, inode); 2945444ceed8SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 2946444ceed8SEric W. Biederman if (pid_revalidate(dentry, NULL)) 2947444ceed8SEric W. Biederman error = NULL; 2948444ceed8SEric W. Biederman out: 2949444ceed8SEric W. Biederman return error; 2950444ceed8SEric W. Biederman } 2951444ceed8SEric W. Biederman 295228a6d671SEric W. Biederman static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd) 295328a6d671SEric W. Biederman { 295428a6d671SEric W. Biederman struct dentry *result = ERR_PTR(-ENOENT); 295528a6d671SEric W. Biederman struct task_struct *task; 295628a6d671SEric W. Biederman struct task_struct *leader = get_proc_task(dir); 295728a6d671SEric W. Biederman unsigned tid; 2958b488893aSPavel Emelyanov struct pid_namespace *ns; 295928a6d671SEric W. Biederman 296028a6d671SEric W. Biederman if (!leader) 296128a6d671SEric W. Biederman goto out_no_task; 296228a6d671SEric W. Biederman 296328a6d671SEric W. Biederman tid = name_to_int(dentry); 296428a6d671SEric W. Biederman if (tid == ~0U) 296528a6d671SEric W. Biederman goto out; 296628a6d671SEric W. Biederman 2967b488893aSPavel Emelyanov ns = dentry->d_sb->s_fs_info; 296828a6d671SEric W. Biederman rcu_read_lock(); 2969b488893aSPavel Emelyanov task = find_task_by_pid_ns(tid, ns); 297028a6d671SEric W. Biederman if (task) 297128a6d671SEric W. Biederman get_task_struct(task); 297228a6d671SEric W. Biederman rcu_read_unlock(); 297328a6d671SEric W. Biederman if (!task) 297428a6d671SEric W. Biederman goto out; 2975bac0abd6SPavel Emelyanov if (!same_thread_group(leader, task)) 297628a6d671SEric W. Biederman goto out_drop_task; 297728a6d671SEric W. Biederman 2978444ceed8SEric W. Biederman result = proc_task_instantiate(dir, dentry, task, NULL); 297928a6d671SEric W. Biederman out_drop_task: 298028a6d671SEric W. Biederman put_task_struct(task); 298128a6d671SEric W. Biederman out: 298228a6d671SEric W. Biederman put_task_struct(leader); 298328a6d671SEric W. Biederman out_no_task: 298428a6d671SEric W. Biederman return result; 298528a6d671SEric W. Biederman } 298628a6d671SEric W. Biederman 298728a6d671SEric W. Biederman /* 29880bc58a91SEric W. Biederman * Find the first tid of a thread group to return to user space. 29890bc58a91SEric W. Biederman * 29900bc58a91SEric W. Biederman * Usually this is just the thread group leader, but if the users 29910bc58a91SEric W. Biederman * buffer was too small or there was a seek into the middle of the 29920bc58a91SEric W. Biederman * directory we have more work todo. 29930bc58a91SEric W. Biederman * 29940bc58a91SEric W. Biederman * In the case of a short read we start with find_task_by_pid. 29950bc58a91SEric W. Biederman * 29960bc58a91SEric W. Biederman * In the case of a seek we start with the leader and walk nr 29970bc58a91SEric W. Biederman * threads past it. 29980bc58a91SEric W. Biederman */ 2999cc288738SEric W. Biederman static struct task_struct *first_tid(struct task_struct *leader, 3000b488893aSPavel Emelyanov int tid, int nr, struct pid_namespace *ns) 30010bc58a91SEric W. Biederman { 3002a872ff0cSOleg Nesterov struct task_struct *pos; 30030bc58a91SEric W. Biederman 3004cc288738SEric W. Biederman rcu_read_lock(); 30050bc58a91SEric W. Biederman /* Attempt to start with the pid of a thread */ 30060bc58a91SEric W. Biederman if (tid && (nr > 0)) { 3007b488893aSPavel Emelyanov pos = find_task_by_pid_ns(tid, ns); 3008a872ff0cSOleg Nesterov if (pos && (pos->group_leader == leader)) 3009a872ff0cSOleg Nesterov goto found; 30100bc58a91SEric W. Biederman } 30110bc58a91SEric W. Biederman 30120bc58a91SEric W. Biederman /* If nr exceeds the number of threads there is nothing todo */ 30130bc58a91SEric W. Biederman pos = NULL; 3014a872ff0cSOleg Nesterov if (nr && nr >= get_nr_threads(leader)) 3015a872ff0cSOleg Nesterov goto out; 3016a872ff0cSOleg Nesterov 3017a872ff0cSOleg Nesterov /* If we haven't found our starting place yet start 3018a872ff0cSOleg Nesterov * with the leader and walk nr threads forward. 3019a872ff0cSOleg Nesterov */ 3020a872ff0cSOleg Nesterov for (pos = leader; nr > 0; --nr) { 3021a872ff0cSOleg Nesterov pos = next_thread(pos); 3022a872ff0cSOleg Nesterov if (pos == leader) { 3023a872ff0cSOleg Nesterov pos = NULL; 3024a872ff0cSOleg Nesterov goto out; 3025a872ff0cSOleg Nesterov } 3026a872ff0cSOleg Nesterov } 3027a872ff0cSOleg Nesterov found: 3028a872ff0cSOleg Nesterov get_task_struct(pos); 3029a872ff0cSOleg Nesterov out: 3030cc288738SEric W. Biederman rcu_read_unlock(); 30310bc58a91SEric W. Biederman return pos; 30320bc58a91SEric W. Biederman } 30330bc58a91SEric W. Biederman 30340bc58a91SEric W. Biederman /* 30350bc58a91SEric W. Biederman * Find the next thread in the thread list. 30360bc58a91SEric W. Biederman * Return NULL if there is an error or no next thread. 30370bc58a91SEric W. Biederman * 30380bc58a91SEric W. Biederman * The reference to the input task_struct is released. 30390bc58a91SEric W. Biederman */ 30400bc58a91SEric W. Biederman static struct task_struct *next_tid(struct task_struct *start) 30410bc58a91SEric W. Biederman { 3042c1df7fb8SOleg Nesterov struct task_struct *pos = NULL; 3043cc288738SEric W. Biederman rcu_read_lock(); 3044c1df7fb8SOleg Nesterov if (pid_alive(start)) { 30450bc58a91SEric W. Biederman pos = next_thread(start); 3046c1df7fb8SOleg Nesterov if (thread_group_leader(pos)) 30470bc58a91SEric W. Biederman pos = NULL; 3048c1df7fb8SOleg Nesterov else 3049c1df7fb8SOleg Nesterov get_task_struct(pos); 3050c1df7fb8SOleg Nesterov } 3051cc288738SEric W. Biederman rcu_read_unlock(); 30520bc58a91SEric W. Biederman put_task_struct(start); 30530bc58a91SEric W. Biederman return pos; 30540bc58a91SEric W. Biederman } 30550bc58a91SEric W. Biederman 305661a28784SEric W. Biederman static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir, 305761a28784SEric W. Biederman struct task_struct *task, int tid) 305861a28784SEric W. Biederman { 305961a28784SEric W. Biederman char name[PROC_NUMBUF]; 306061a28784SEric W. Biederman int len = snprintf(name, sizeof(name), "%d", tid); 306161a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, name, len, 306261a28784SEric W. Biederman proc_task_instantiate, task, NULL); 306361a28784SEric W. Biederman } 306461a28784SEric W. Biederman 30651da177e4SLinus Torvalds /* for the /proc/TGID/task/ directories */ 30661da177e4SLinus Torvalds static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir) 30671da177e4SLinus Torvalds { 30682fddfeefSJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 30691da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 30707d895244SGuillaume Chazarain struct task_struct *leader = NULL; 30710bc58a91SEric W. Biederman struct task_struct *task; 30721da177e4SLinus Torvalds int retval = -ENOENT; 30731da177e4SLinus Torvalds ino_t ino; 30740bc58a91SEric W. Biederman int tid; 30751da177e4SLinus Torvalds unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */ 3076b488893aSPavel Emelyanov struct pid_namespace *ns; 30771da177e4SLinus Torvalds 30787d895244SGuillaume Chazarain task = get_proc_task(inode); 30797d895244SGuillaume Chazarain if (!task) 30807d895244SGuillaume Chazarain goto out_no_task; 30817d895244SGuillaume Chazarain rcu_read_lock(); 30827d895244SGuillaume Chazarain if (pid_alive(task)) { 30837d895244SGuillaume Chazarain leader = task->group_leader; 30847d895244SGuillaume Chazarain get_task_struct(leader); 30857d895244SGuillaume Chazarain } 30867d895244SGuillaume Chazarain rcu_read_unlock(); 30877d895244SGuillaume Chazarain put_task_struct(task); 308899f89551SEric W. Biederman if (!leader) 308999f89551SEric W. Biederman goto out_no_task; 30901da177e4SLinus Torvalds retval = 0; 30911da177e4SLinus Torvalds 30921da177e4SLinus Torvalds switch (pos) { 30931da177e4SLinus Torvalds case 0: 30941da177e4SLinus Torvalds ino = inode->i_ino; 30951da177e4SLinus Torvalds if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0) 30961da177e4SLinus Torvalds goto out; 30971da177e4SLinus Torvalds pos++; 30981da177e4SLinus Torvalds /* fall through */ 30991da177e4SLinus Torvalds case 1: 31001da177e4SLinus Torvalds ino = parent_ino(dentry); 31011da177e4SLinus Torvalds if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0) 31021da177e4SLinus Torvalds goto out; 31031da177e4SLinus Torvalds pos++; 31041da177e4SLinus Torvalds /* fall through */ 31051da177e4SLinus Torvalds } 31061da177e4SLinus Torvalds 31070bc58a91SEric W. Biederman /* f_version caches the tgid value that the last readdir call couldn't 31080bc58a91SEric W. Biederman * return. lseek aka telldir automagically resets f_version to 0. 31090bc58a91SEric W. Biederman */ 3110b488893aSPavel Emelyanov ns = filp->f_dentry->d_sb->s_fs_info; 31112b47c361SMathieu Desnoyers tid = (int)filp->f_version; 31120bc58a91SEric W. Biederman filp->f_version = 0; 3113b488893aSPavel Emelyanov for (task = first_tid(leader, tid, pos - 2, ns); 31140bc58a91SEric W. Biederman task; 31150bc58a91SEric W. Biederman task = next_tid(task), pos++) { 3116b488893aSPavel Emelyanov tid = task_pid_nr_ns(task, ns); 311761a28784SEric W. Biederman if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) { 31180bc58a91SEric W. Biederman /* returning this tgid failed, save it as the first 31190bc58a91SEric W. Biederman * pid for the next readir call */ 31202b47c361SMathieu Desnoyers filp->f_version = (u64)tid; 31210bc58a91SEric W. Biederman put_task_struct(task); 31221da177e4SLinus Torvalds break; 31230bc58a91SEric W. Biederman } 31241da177e4SLinus Torvalds } 31251da177e4SLinus Torvalds out: 31261da177e4SLinus Torvalds filp->f_pos = pos; 312799f89551SEric W. Biederman put_task_struct(leader); 312899f89551SEric W. Biederman out_no_task: 31291da177e4SLinus Torvalds return retval; 31301da177e4SLinus Torvalds } 31316e66b52bSEric W. Biederman 31326e66b52bSEric W. Biederman static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) 31336e66b52bSEric W. Biederman { 31346e66b52bSEric W. Biederman struct inode *inode = dentry->d_inode; 313599f89551SEric W. Biederman struct task_struct *p = get_proc_task(inode); 31366e66b52bSEric W. Biederman generic_fillattr(inode, stat); 31376e66b52bSEric W. Biederman 313899f89551SEric W. Biederman if (p) { 313999f89551SEric W. Biederman stat->nlink += get_nr_threads(p); 314099f89551SEric W. Biederman put_task_struct(p); 31416e66b52bSEric W. Biederman } 31426e66b52bSEric W. Biederman 31436e66b52bSEric W. Biederman return 0; 31446e66b52bSEric W. Biederman } 314528a6d671SEric W. Biederman 3146c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations = { 314728a6d671SEric W. Biederman .lookup = proc_task_lookup, 314828a6d671SEric W. Biederman .getattr = proc_task_getattr, 314928a6d671SEric W. Biederman .setattr = proc_setattr, 315028a6d671SEric W. Biederman }; 315128a6d671SEric W. Biederman 315200977a59SArjan van de Ven static const struct file_operations proc_task_operations = { 315328a6d671SEric W. Biederman .read = generic_read_dir, 315428a6d671SEric W. Biederman .readdir = proc_task_readdir, 315528a6d671SEric W. Biederman }; 3156