Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/proc/base.c |
| 3 | * |
| 4 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 5 | * |
| 6 | * proc base directory handling functions |
| 7 | * |
| 8 | * 1999, Al Viro. Rewritten. Now it covers the whole per-process part. |
| 9 | * Instead of using magical inumbers to determine the kind of object |
| 10 | * we allocate and fill in-core inodes upon lookup. They don't even |
| 11 | * go into icache. We cache the reference to task_struct upon lookup too. |
| 12 | * Eventually it should become a filesystem in its own. We don't use the |
| 13 | * rest of procfs anymore. |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 14 | * |
| 15 | * |
| 16 | * Changelog: |
| 17 | * 17-Jan-2005 |
| 18 | * Allan Bezerra |
| 19 | * Bruna Moreira <bruna.moreira@indt.org.br> |
| 20 | * Edjard Mota <edjard.mota@indt.org.br> |
| 21 | * Ilias Biris <ilias.biris@indt.org.br> |
| 22 | * Mauricio Lin <mauricio.lin@indt.org.br> |
| 23 | * |
| 24 | * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT |
| 25 | * |
| 26 | * A new process specific entry (smaps) included in /proc. It shows the |
| 27 | * size of rss for each memory area. The maps entry lacks information |
| 28 | * about physical memory size (rss) for each mapped file, i.e., |
| 29 | * rss information for executables and library files. |
| 30 | * This additional information is useful for any tools that need to know |
| 31 | * about physical memory consumption for a process specific library. |
| 32 | * |
| 33 | * Changelog: |
| 34 | * 21-Feb-2005 |
| 35 | * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT |
| 36 | * Pud inclusion in the page table walking. |
| 37 | * |
| 38 | * ChangeLog: |
| 39 | * 10-Mar-2005 |
| 40 | * 10LE Instituto Nokia de Tecnologia - INdT: |
| 41 | * A better way to walks through the page table as suggested by Hugh Dickins. |
| 42 | * |
| 43 | * Simo Piiroinen <simo.piiroinen@nokia.com>: |
| 44 | * Smaps information related to shared, private, clean and dirty pages. |
| 45 | * |
| 46 | * Paul Mundt <paul.mundt@nokia.com>: |
| 47 | * Overall revision about smaps. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | */ |
| 49 | |
| 50 | #include <asm/uaccess.h> |
| 51 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | #include <linux/errno.h> |
| 53 | #include <linux/time.h> |
| 54 | #include <linux/proc_fs.h> |
| 55 | #include <linux/stat.h> |
Andrea Righi | 5995477 | 2008-07-27 17:29:15 +0200 | [diff] [blame] | 56 | #include <linux/task_io_accounting_ops.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | #include <linux/init.h> |
Randy Dunlap | 16f7e0f | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 58 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | #include <linux/file.h> |
Al Viro | 9f3acc3 | 2008-04-24 07:44:08 -0400 | [diff] [blame] | 60 | #include <linux/fdtable.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | #include <linux/string.h> |
| 62 | #include <linux/seq_file.h> |
| 63 | #include <linux/namei.h> |
Kirill Korotaev | 6b3286e | 2006-12-08 02:37:56 -0800 | [diff] [blame] | 64 | #include <linux/mnt_namespace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | #include <linux/mm.h> |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 66 | #include <linux/swap.h> |
Dipankar Sarma | b835996 | 2005-09-09 13:04:14 -0700 | [diff] [blame] | 67 | #include <linux/rcupdate.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | #include <linux/kallsyms.h> |
Ken Chen | 2ec220e | 2008-11-10 11:26:08 +0300 | [diff] [blame] | 69 | #include <linux/stacktrace.h> |
Neil Horman | d85f50d | 2007-10-18 23:40:37 -0700 | [diff] [blame] | 70 | #include <linux/resource.h> |
Kees Cook | 5096add | 2007-05-08 00:26:04 -0700 | [diff] [blame] | 71 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | #include <linux/mount.h> |
| 73 | #include <linux/security.h> |
| 74 | #include <linux/ptrace.h> |
Roland McGrath | 0d094ef | 2008-07-25 19:45:49 -0700 | [diff] [blame] | 75 | #include <linux/tracehook.h> |
Andrew Morton | 87ebdc0 | 2013-02-27 17:03:16 -0800 | [diff] [blame] | 76 | #include <linux/printk.h> |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 77 | #include <linux/cgroup.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | #include <linux/cpuset.h> |
| 79 | #include <linux/audit.h> |
Al Viro | 5addc5d | 2005-11-07 17:15:49 -0500 | [diff] [blame] | 80 | #include <linux/poll.h> |
Serge E. Hallyn | 1651e14 | 2006-10-02 02:18:08 -0700 | [diff] [blame] | 81 | #include <linux/nsproxy.h> |
Alexey Dobriyan | 8ac773b | 2006-10-19 23:28:32 -0700 | [diff] [blame] | 82 | #include <linux/oom.h> |
Kawai, Hidehiro | 3cb4a0b | 2007-07-19 01:48:28 -0700 | [diff] [blame] | 83 | #include <linux/elf.h> |
Pavel Emelyanov | 60347f6 | 2007-10-18 23:40:03 -0700 | [diff] [blame] | 84 | #include <linux/pid_namespace.h> |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 85 | #include <linux/user_namespace.h> |
Al Viro | 5ad4e53 | 2009-03-29 19:50:06 -0400 | [diff] [blame] | 86 | #include <linux/fs_struct.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 87 | #include <linux/slab.h> |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 88 | #include <linux/flex_array.h> |
Pavel Emelyanov | 48f6a7a | 2013-03-11 13:12:45 +0400 | [diff] [blame] | 89 | #include <linux/posix-timers.h> |
Chris Metcalf | f133ecc | 2011-05-26 12:40:09 -0400 | [diff] [blame] | 90 | #ifdef CONFIG_HARDWALL |
| 91 | #include <asm/hardwall.h> |
| 92 | #endif |
KAMEZAWA Hiroyuki | 43d2b11 | 2012-01-10 15:08:09 -0800 | [diff] [blame] | 93 | #include <trace/events/oom.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | #include "internal.h" |
Cyrill Gorcunov | faf60af | 2012-08-23 14:43:24 +0400 | [diff] [blame] | 95 | #include "fd.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | |
Eric W. Biederman | 0f2fe20 | 2006-06-26 00:25:46 -0700 | [diff] [blame] | 97 | /* NOTE: |
| 98 | * Implementing inode permission operations in /proc is almost |
| 99 | * certainly an error. Permission checks need to happen during |
| 100 | * each system call not at open time. The reason is that most of |
| 101 | * what we wish to check for permissions in /proc varies at runtime. |
| 102 | * |
| 103 | * The classic example of a problem is opening file descriptors |
| 104 | * in /proc for a task before it execs a suid executable. |
| 105 | */ |
| 106 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | struct pid_entry { |
Alexey Dobriyan | cedbcca | 2014-08-08 14:21:33 -0700 | [diff] [blame] | 108 | const char *name; |
Eric Dumazet | c5141e6 | 2007-05-08 00:26:15 -0700 | [diff] [blame] | 109 | int len; |
Al Viro | d161a13 | 2011-07-24 03:36:29 -0400 | [diff] [blame] | 110 | umode_t mode; |
Arjan van de Ven | c5ef1c4 | 2007-02-12 00:55:40 -0800 | [diff] [blame] | 111 | const struct inode_operations *iop; |
Arjan van de Ven | 00977a5 | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 112 | const struct file_operations *fop; |
Eric W. Biederman | 20cdc89 | 2006-10-02 02:17:07 -0700 | [diff] [blame] | 113 | union proc_op op; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | }; |
| 115 | |
Eric W. Biederman | 61a2878 | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 116 | #define NOD(NAME, MODE, IOP, FOP, OP) { \ |
Eric W. Biederman | 20cdc89 | 2006-10-02 02:17:07 -0700 | [diff] [blame] | 117 | .name = (NAME), \ |
Eric Dumazet | c5141e6 | 2007-05-08 00:26:15 -0700 | [diff] [blame] | 118 | .len = sizeof(NAME) - 1, \ |
Eric W. Biederman | 20cdc89 | 2006-10-02 02:17:07 -0700 | [diff] [blame] | 119 | .mode = MODE, \ |
| 120 | .iop = IOP, \ |
| 121 | .fop = FOP, \ |
| 122 | .op = OP, \ |
| 123 | } |
| 124 | |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 125 | #define DIR(NAME, MODE, iops, fops) \ |
| 126 | NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} ) |
| 127 | #define LNK(NAME, get_link) \ |
Eric W. Biederman | 61a2878 | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 128 | NOD(NAME, (S_IFLNK|S_IRWXUGO), \ |
Eric W. Biederman | 20cdc89 | 2006-10-02 02:17:07 -0700 | [diff] [blame] | 129 | &proc_pid_link_inode_operations, NULL, \ |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 130 | { .proc_get_link = get_link } ) |
| 131 | #define REG(NAME, MODE, fops) \ |
| 132 | NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {}) |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 133 | #define ONE(NAME, MODE, show) \ |
Eric W. Biederman | be61408 | 2008-02-08 04:18:30 -0800 | [diff] [blame] | 134 | NOD(NAME, (S_IFREG|(MODE)), \ |
| 135 | NULL, &proc_single_file_operations, \ |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 136 | { .proc_show = show } ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | |
Vegard Nossum | aed5417 | 2008-06-05 22:46:53 -0700 | [diff] [blame] | 138 | /* |
| 139 | * Count the number of hardlinks for the pid_entry table, excluding the . |
| 140 | * and .. links. |
| 141 | */ |
| 142 | static unsigned int pid_entry_count_dirs(const struct pid_entry *entries, |
| 143 | unsigned int n) |
| 144 | { |
| 145 | unsigned int i; |
| 146 | unsigned int count; |
| 147 | |
| 148 | count = 0; |
| 149 | for (i = 0; i < n; ++i) { |
| 150 | if (S_ISDIR(entries[i].mode)) |
| 151 | ++count; |
| 152 | } |
| 153 | |
| 154 | return count; |
| 155 | } |
| 156 | |
Miklos Szeredi | f7ad3c6 | 2010-08-10 11:41:36 +0200 | [diff] [blame] | 157 | static int get_task_root(struct task_struct *task, struct path *root) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | { |
Hugh Dickins | 7c2c7d9 | 2009-03-28 23:21:27 +0000 | [diff] [blame] | 159 | int result = -ENOENT; |
| 160 | |
Miklos Szeredi | 0494f6e | 2005-09-06 15:18:22 -0700 | [diff] [blame] | 161 | task_lock(task); |
Miklos Szeredi | f7ad3c6 | 2010-08-10 11:41:36 +0200 | [diff] [blame] | 162 | if (task->fs) { |
| 163 | get_fs_root(task->fs, root); |
Hugh Dickins | 7c2c7d9 | 2009-03-28 23:21:27 +0000 | [diff] [blame] | 164 | result = 0; |
| 165 | } |
Miklos Szeredi | 0494f6e | 2005-09-06 15:18:22 -0700 | [diff] [blame] | 166 | task_unlock(task); |
Hugh Dickins | 7c2c7d9 | 2009-03-28 23:21:27 +0000 | [diff] [blame] | 167 | return result; |
Miklos Szeredi | 0494f6e | 2005-09-06 15:18:22 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Cyrill Gorcunov | 7773fbc | 2012-01-10 15:11:20 -0800 | [diff] [blame] | 170 | static int proc_cwd_link(struct dentry *dentry, struct path *path) |
Miklos Szeredi | 0494f6e | 2005-09-06 15:18:22 -0700 | [diff] [blame] | 171 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 172 | struct task_struct *task = get_proc_task(d_inode(dentry)); |
Miklos Szeredi | 0494f6e | 2005-09-06 15:18:22 -0700 | [diff] [blame] | 173 | int result = -ENOENT; |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 174 | |
| 175 | if (task) { |
Miklos Szeredi | f7ad3c6 | 2010-08-10 11:41:36 +0200 | [diff] [blame] | 176 | task_lock(task); |
| 177 | if (task->fs) { |
| 178 | get_fs_pwd(task->fs, path); |
| 179 | result = 0; |
| 180 | } |
| 181 | task_unlock(task); |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 182 | put_task_struct(task); |
| 183 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | return result; |
| 185 | } |
| 186 | |
Cyrill Gorcunov | 7773fbc | 2012-01-10 15:11:20 -0800 | [diff] [blame] | 187 | static int proc_root_link(struct dentry *dentry, struct path *path) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 189 | struct task_struct *task = get_proc_task(d_inode(dentry)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | int result = -ENOENT; |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 191 | |
| 192 | if (task) { |
Miklos Szeredi | f7ad3c6 | 2010-08-10 11:41:36 +0200 | [diff] [blame] | 193 | result = get_task_root(task, path); |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 194 | put_task_struct(task); |
| 195 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | return result; |
| 197 | } |
| 198 | |
Alexey Dobriyan | 2ca66ff | 2014-08-08 14:21:41 -0700 | [diff] [blame] | 199 | static int proc_pid_cmdline(struct seq_file *m, struct pid_namespace *ns, |
| 200 | struct pid *pid, struct task_struct *task) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | { |
Alexey Dobriyan | 2ca66ff | 2014-08-08 14:21:41 -0700 | [diff] [blame] | 202 | /* |
| 203 | * Rely on struct seq_operations::show() being called once |
| 204 | * per internal buffer allocation. See single_open(), traverse(). |
| 205 | */ |
| 206 | BUG_ON(m->size < PAGE_SIZE); |
| 207 | m->count += get_cmdline(task, m->buf, PAGE_SIZE); |
| 208 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Alexey Dobriyan | f9ea536 | 2014-08-08 14:21:35 -0700 | [diff] [blame] | 211 | static int proc_pid_auxv(struct seq_file *m, struct pid_namespace *ns, |
| 212 | struct pid *pid, struct task_struct *task) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | { |
Cong Wang | e7dcd99 | 2012-05-31 16:26:17 -0700 | [diff] [blame] | 214 | struct mm_struct *mm = mm_access(task, PTRACE_MODE_READ); |
Al Viro | 2fadaef | 2011-02-15 22:52:11 -0500 | [diff] [blame] | 215 | if (mm && !IS_ERR(mm)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | unsigned int nwords = 0; |
Hannes Eder | dfe6b7d | 2008-12-30 18:49:13 +0300 | [diff] [blame] | 217 | do { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | nwords += 2; |
Hannes Eder | dfe6b7d | 2008-12-30 18:49:13 +0300 | [diff] [blame] | 219 | } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */ |
Alexey Dobriyan | f9ea536 | 2014-08-08 14:21:35 -0700 | [diff] [blame] | 220 | seq_write(m, mm->saved_auxv, nwords * sizeof(mm->saved_auxv[0])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | mmput(mm); |
Alexey Dobriyan | f9ea536 | 2014-08-08 14:21:35 -0700 | [diff] [blame] | 222 | return 0; |
| 223 | } else |
| 224 | return PTR_ERR(mm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | |
| 228 | #ifdef CONFIG_KALLSYMS |
| 229 | /* |
| 230 | * Provides a wchan file via kallsyms in a proper one-value-per-file format. |
| 231 | * Returns the resolved symbol. If that fails, simply return the address. |
| 232 | */ |
Alexey Dobriyan | edfcd60 | 2014-08-08 14:21:44 -0700 | [diff] [blame] | 233 | static int proc_pid_wchan(struct seq_file *m, struct pid_namespace *ns, |
| 234 | struct pid *pid, struct task_struct *task) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | { |
Alexey Dobriyan | ffb4512 | 2007-05-08 00:28:41 -0700 | [diff] [blame] | 236 | unsigned long wchan; |
Tejun Heo | 9281ace | 2007-07-17 04:03:51 -0700 | [diff] [blame] | 237 | char symname[KSYM_NAME_LEN]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | |
| 239 | wchan = get_wchan(task); |
| 240 | |
Joe Perches | 25ce319 | 2015-04-15 16:18:17 -0700 | [diff] [blame] | 241 | if (lookup_symbol_name(wchan, symname) < 0) { |
Jake Edge | f83ce3e | 2009-05-04 12:51:14 -0600 | [diff] [blame] | 242 | if (!ptrace_may_access(task, PTRACE_MODE_READ)) |
| 243 | return 0; |
Joe Perches | 25ce319 | 2015-04-15 16:18:17 -0700 | [diff] [blame] | 244 | seq_printf(m, "%lu", wchan); |
| 245 | } else { |
| 246 | seq_printf(m, "%s", symname); |
| 247 | } |
| 248 | |
| 249 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | } |
| 251 | #endif /* CONFIG_KALLSYMS */ |
| 252 | |
Al Viro | a9712bc | 2011-03-23 15:52:50 -0400 | [diff] [blame] | 253 | static int lock_trace(struct task_struct *task) |
| 254 | { |
| 255 | int err = mutex_lock_killable(&task->signal->cred_guard_mutex); |
| 256 | if (err) |
| 257 | return err; |
| 258 | if (!ptrace_may_access(task, PTRACE_MODE_ATTACH)) { |
| 259 | mutex_unlock(&task->signal->cred_guard_mutex); |
| 260 | return -EPERM; |
| 261 | } |
| 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | static void unlock_trace(struct task_struct *task) |
| 266 | { |
| 267 | mutex_unlock(&task->signal->cred_guard_mutex); |
| 268 | } |
| 269 | |
Ken Chen | 2ec220e | 2008-11-10 11:26:08 +0300 | [diff] [blame] | 270 | #ifdef CONFIG_STACKTRACE |
| 271 | |
| 272 | #define MAX_STACK_TRACE_DEPTH 64 |
| 273 | |
| 274 | static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns, |
| 275 | struct pid *pid, struct task_struct *task) |
| 276 | { |
| 277 | struct stack_trace trace; |
| 278 | unsigned long *entries; |
Al Viro | a9712bc | 2011-03-23 15:52:50 -0400 | [diff] [blame] | 279 | int err; |
Ken Chen | 2ec220e | 2008-11-10 11:26:08 +0300 | [diff] [blame] | 280 | int i; |
| 281 | |
| 282 | entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL); |
| 283 | if (!entries) |
| 284 | return -ENOMEM; |
| 285 | |
| 286 | trace.nr_entries = 0; |
| 287 | trace.max_entries = MAX_STACK_TRACE_DEPTH; |
| 288 | trace.entries = entries; |
| 289 | trace.skip = 0; |
Ken Chen | 2ec220e | 2008-11-10 11:26:08 +0300 | [diff] [blame] | 290 | |
Al Viro | a9712bc | 2011-03-23 15:52:50 -0400 | [diff] [blame] | 291 | err = lock_trace(task); |
| 292 | if (!err) { |
| 293 | save_stack_trace_tsk(task, &trace); |
| 294 | |
| 295 | for (i = 0; i < trace.nr_entries; i++) { |
Linus Torvalds | b81a618 | 2011-03-23 20:51:42 -0700 | [diff] [blame] | 296 | seq_printf(m, "[<%pK>] %pS\n", |
Al Viro | a9712bc | 2011-03-23 15:52:50 -0400 | [diff] [blame] | 297 | (void *)entries[i], (void *)entries[i]); |
| 298 | } |
| 299 | unlock_trace(task); |
Ken Chen | 2ec220e | 2008-11-10 11:26:08 +0300 | [diff] [blame] | 300 | } |
| 301 | kfree(entries); |
| 302 | |
Al Viro | a9712bc | 2011-03-23 15:52:50 -0400 | [diff] [blame] | 303 | return err; |
Ken Chen | 2ec220e | 2008-11-10 11:26:08 +0300 | [diff] [blame] | 304 | } |
| 305 | #endif |
| 306 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | #ifdef CONFIG_SCHEDSTATS |
| 308 | /* |
| 309 | * Provides /proc/PID/schedstat |
| 310 | */ |
Alexey Dobriyan | f6e826c | 2014-08-08 14:21:46 -0700 | [diff] [blame] | 311 | static int proc_pid_schedstat(struct seq_file *m, struct pid_namespace *ns, |
| 312 | struct pid *pid, struct task_struct *task) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | { |
Joe Perches | 25ce319 | 2015-04-15 16:18:17 -0700 | [diff] [blame] | 314 | seq_printf(m, "%llu %llu %lu\n", |
| 315 | (unsigned long long)task->se.sum_exec_runtime, |
| 316 | (unsigned long long)task->sched_info.run_delay, |
| 317 | task->sched_info.pcount); |
| 318 | |
| 319 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | } |
| 321 | #endif |
| 322 | |
Arjan van de Ven | 9745512 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 323 | #ifdef CONFIG_LATENCYTOP |
| 324 | static int lstats_show_proc(struct seq_file *m, void *v) |
| 325 | { |
| 326 | int i; |
Hiroshi Shimamoto | 13d77c3 | 2008-02-20 16:53:29 -0800 | [diff] [blame] | 327 | struct inode *inode = m->private; |
| 328 | struct task_struct *task = get_proc_task(inode); |
Arjan van de Ven | 9745512 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 329 | |
Hiroshi Shimamoto | 13d77c3 | 2008-02-20 16:53:29 -0800 | [diff] [blame] | 330 | if (!task) |
| 331 | return -ESRCH; |
| 332 | seq_puts(m, "Latency Top version : v0.1\n"); |
Arjan van de Ven | 9745512 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 333 | for (i = 0; i < 32; i++) { |
Joe Perches | 34e49d4 | 2011-01-12 17:00:30 -0800 | [diff] [blame] | 334 | struct latency_record *lr = &task->latency_record[i]; |
| 335 | if (lr->backtrace[0]) { |
Arjan van de Ven | 9745512 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 336 | int q; |
Joe Perches | 34e49d4 | 2011-01-12 17:00:30 -0800 | [diff] [blame] | 337 | seq_printf(m, "%i %li %li", |
| 338 | lr->count, lr->time, lr->max); |
Arjan van de Ven | 9745512 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 339 | for (q = 0; q < LT_BACKTRACEDEPTH; q++) { |
Joe Perches | 34e49d4 | 2011-01-12 17:00:30 -0800 | [diff] [blame] | 340 | unsigned long bt = lr->backtrace[q]; |
| 341 | if (!bt) |
Arjan van de Ven | 9745512 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 342 | break; |
Joe Perches | 34e49d4 | 2011-01-12 17:00:30 -0800 | [diff] [blame] | 343 | if (bt == ULONG_MAX) |
Arjan van de Ven | 9745512 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 344 | break; |
Joe Perches | 34e49d4 | 2011-01-12 17:00:30 -0800 | [diff] [blame] | 345 | seq_printf(m, " %ps", (void *)bt); |
Arjan van de Ven | 9745512 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 346 | } |
Alexey Dobriyan | 9d6de12 | 2011-01-12 17:00:32 -0800 | [diff] [blame] | 347 | seq_putc(m, '\n'); |
Arjan van de Ven | 9745512 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | } |
Hiroshi Shimamoto | 13d77c3 | 2008-02-20 16:53:29 -0800 | [diff] [blame] | 351 | put_task_struct(task); |
Arjan van de Ven | 9745512 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 352 | return 0; |
| 353 | } |
| 354 | |
| 355 | static int lstats_open(struct inode *inode, struct file *file) |
| 356 | { |
Hiroshi Shimamoto | 13d77c3 | 2008-02-20 16:53:29 -0800 | [diff] [blame] | 357 | return single_open(file, lstats_show_proc, inode); |
Hiroshi Shimamoto | d6643d1 | 2008-02-14 10:27:00 -0800 | [diff] [blame] | 358 | } |
| 359 | |
Arjan van de Ven | 9745512 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 360 | static ssize_t lstats_write(struct file *file, const char __user *buf, |
| 361 | size_t count, loff_t *offs) |
| 362 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 363 | struct task_struct *task = get_proc_task(file_inode(file)); |
Arjan van de Ven | 9745512 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 364 | |
Hiroshi Shimamoto | 13d77c3 | 2008-02-20 16:53:29 -0800 | [diff] [blame] | 365 | if (!task) |
| 366 | return -ESRCH; |
Arjan van de Ven | 9745512 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 367 | clear_all_latency_tracing(task); |
Hiroshi Shimamoto | 13d77c3 | 2008-02-20 16:53:29 -0800 | [diff] [blame] | 368 | put_task_struct(task); |
Arjan van de Ven | 9745512 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 369 | |
| 370 | return count; |
| 371 | } |
| 372 | |
| 373 | static const struct file_operations proc_lstats_operations = { |
| 374 | .open = lstats_open, |
| 375 | .read = seq_read, |
| 376 | .write = lstats_write, |
| 377 | .llseek = seq_lseek, |
Hiroshi Shimamoto | 13d77c3 | 2008-02-20 16:53:29 -0800 | [diff] [blame] | 378 | .release = single_release, |
Arjan van de Ven | 9745512 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 379 | }; |
| 380 | |
| 381 | #endif |
| 382 | |
Alexey Dobriyan | 6ba51e3 | 2014-08-08 14:21:48 -0700 | [diff] [blame] | 383 | static int proc_oom_score(struct seq_file *m, struct pid_namespace *ns, |
| 384 | struct pid *pid, struct task_struct *task) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | { |
David Rientjes | a7f638f | 2012-05-29 15:06:47 -0700 | [diff] [blame] | 386 | unsigned long totalpages = totalram_pages + total_swap_pages; |
Oleg Nesterov | b95c35e | 2010-04-01 15:13:57 +0200 | [diff] [blame] | 387 | unsigned long points = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | |
Alexey Dobriyan | 19c5d45 | 2007-05-08 00:26:46 -0700 | [diff] [blame] | 389 | read_lock(&tasklist_lock); |
Oleg Nesterov | b95c35e | 2010-04-01 15:13:57 +0200 | [diff] [blame] | 390 | if (pid_alive(task)) |
David Rientjes | a7f638f | 2012-05-29 15:06:47 -0700 | [diff] [blame] | 391 | points = oom_badness(task, NULL, NULL, totalpages) * |
| 392 | 1000 / totalpages; |
Alexey Dobriyan | 19c5d45 | 2007-05-08 00:26:46 -0700 | [diff] [blame] | 393 | read_unlock(&tasklist_lock); |
Joe Perches | 25ce319 | 2015-04-15 16:18:17 -0700 | [diff] [blame] | 394 | seq_printf(m, "%lu\n", points); |
| 395 | |
| 396 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | } |
| 398 | |
Neil Horman | d85f50d | 2007-10-18 23:40:37 -0700 | [diff] [blame] | 399 | struct limit_names { |
Alexey Dobriyan | cedbcca | 2014-08-08 14:21:33 -0700 | [diff] [blame] | 400 | const char *name; |
| 401 | const char *unit; |
Neil Horman | d85f50d | 2007-10-18 23:40:37 -0700 | [diff] [blame] | 402 | }; |
| 403 | |
| 404 | static const struct limit_names lnames[RLIM_NLIMITS] = { |
Kees Cook | cff4edb | 2009-09-22 16:45:32 -0700 | [diff] [blame] | 405 | [RLIMIT_CPU] = {"Max cpu time", "seconds"}, |
Neil Horman | d85f50d | 2007-10-18 23:40:37 -0700 | [diff] [blame] | 406 | [RLIMIT_FSIZE] = {"Max file size", "bytes"}, |
| 407 | [RLIMIT_DATA] = {"Max data size", "bytes"}, |
| 408 | [RLIMIT_STACK] = {"Max stack size", "bytes"}, |
| 409 | [RLIMIT_CORE] = {"Max core file size", "bytes"}, |
| 410 | [RLIMIT_RSS] = {"Max resident set", "bytes"}, |
| 411 | [RLIMIT_NPROC] = {"Max processes", "processes"}, |
| 412 | [RLIMIT_NOFILE] = {"Max open files", "files"}, |
| 413 | [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"}, |
| 414 | [RLIMIT_AS] = {"Max address space", "bytes"}, |
| 415 | [RLIMIT_LOCKS] = {"Max file locks", "locks"}, |
| 416 | [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"}, |
| 417 | [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"}, |
| 418 | [RLIMIT_NICE] = {"Max nice priority", NULL}, |
| 419 | [RLIMIT_RTPRIO] = {"Max realtime priority", NULL}, |
Eugene Teo | 8808117 | 2008-02-23 15:23:52 -0800 | [diff] [blame] | 420 | [RLIMIT_RTTIME] = {"Max realtime timeout", "us"}, |
Neil Horman | d85f50d | 2007-10-18 23:40:37 -0700 | [diff] [blame] | 421 | }; |
| 422 | |
| 423 | /* Display limits for a process */ |
Alexey Dobriyan | 1c963eb | 2014-08-08 14:21:37 -0700 | [diff] [blame] | 424 | static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns, |
| 425 | struct pid *pid, struct task_struct *task) |
Neil Horman | d85f50d | 2007-10-18 23:40:37 -0700 | [diff] [blame] | 426 | { |
| 427 | unsigned int i; |
Neil Horman | d85f50d | 2007-10-18 23:40:37 -0700 | [diff] [blame] | 428 | unsigned long flags; |
Neil Horman | d85f50d | 2007-10-18 23:40:37 -0700 | [diff] [blame] | 429 | |
| 430 | struct rlimit rlim[RLIM_NLIMITS]; |
| 431 | |
Lai Jiangshan | a6bebbc | 2008-10-05 00:51:15 +0400 | [diff] [blame] | 432 | if (!lock_task_sighand(task, &flags)) |
Neil Horman | d85f50d | 2007-10-18 23:40:37 -0700 | [diff] [blame] | 433 | return 0; |
Neil Horman | d85f50d | 2007-10-18 23:40:37 -0700 | [diff] [blame] | 434 | memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS); |
| 435 | unlock_task_sighand(task, &flags); |
Neil Horman | d85f50d | 2007-10-18 23:40:37 -0700 | [diff] [blame] | 436 | |
| 437 | /* |
| 438 | * print the file header |
| 439 | */ |
Alexey Dobriyan | 1c963eb | 2014-08-08 14:21:37 -0700 | [diff] [blame] | 440 | seq_printf(m, "%-25s %-20s %-20s %-10s\n", |
Joe Perches | 25ce319 | 2015-04-15 16:18:17 -0700 | [diff] [blame] | 441 | "Limit", "Soft Limit", "Hard Limit", "Units"); |
Neil Horman | d85f50d | 2007-10-18 23:40:37 -0700 | [diff] [blame] | 442 | |
| 443 | for (i = 0; i < RLIM_NLIMITS; i++) { |
| 444 | if (rlim[i].rlim_cur == RLIM_INFINITY) |
Alexey Dobriyan | 1c963eb | 2014-08-08 14:21:37 -0700 | [diff] [blame] | 445 | seq_printf(m, "%-25s %-20s ", |
Joe Perches | 25ce319 | 2015-04-15 16:18:17 -0700 | [diff] [blame] | 446 | lnames[i].name, "unlimited"); |
Neil Horman | d85f50d | 2007-10-18 23:40:37 -0700 | [diff] [blame] | 447 | else |
Alexey Dobriyan | 1c963eb | 2014-08-08 14:21:37 -0700 | [diff] [blame] | 448 | seq_printf(m, "%-25s %-20lu ", |
Joe Perches | 25ce319 | 2015-04-15 16:18:17 -0700 | [diff] [blame] | 449 | lnames[i].name, rlim[i].rlim_cur); |
Neil Horman | d85f50d | 2007-10-18 23:40:37 -0700 | [diff] [blame] | 450 | |
| 451 | if (rlim[i].rlim_max == RLIM_INFINITY) |
Alexey Dobriyan | 1c963eb | 2014-08-08 14:21:37 -0700 | [diff] [blame] | 452 | seq_printf(m, "%-20s ", "unlimited"); |
Neil Horman | d85f50d | 2007-10-18 23:40:37 -0700 | [diff] [blame] | 453 | else |
Alexey Dobriyan | 1c963eb | 2014-08-08 14:21:37 -0700 | [diff] [blame] | 454 | seq_printf(m, "%-20lu ", rlim[i].rlim_max); |
Neil Horman | d85f50d | 2007-10-18 23:40:37 -0700 | [diff] [blame] | 455 | |
| 456 | if (lnames[i].unit) |
Alexey Dobriyan | 1c963eb | 2014-08-08 14:21:37 -0700 | [diff] [blame] | 457 | seq_printf(m, "%-10s\n", lnames[i].unit); |
Neil Horman | d85f50d | 2007-10-18 23:40:37 -0700 | [diff] [blame] | 458 | else |
Alexey Dobriyan | 1c963eb | 2014-08-08 14:21:37 -0700 | [diff] [blame] | 459 | seq_putc(m, '\n'); |
Neil Horman | d85f50d | 2007-10-18 23:40:37 -0700 | [diff] [blame] | 460 | } |
| 461 | |
Alexey Dobriyan | 1c963eb | 2014-08-08 14:21:37 -0700 | [diff] [blame] | 462 | return 0; |
Neil Horman | d85f50d | 2007-10-18 23:40:37 -0700 | [diff] [blame] | 463 | } |
| 464 | |
Roland McGrath | ebcb673 | 2008-07-25 19:46:00 -0700 | [diff] [blame] | 465 | #ifdef CONFIG_HAVE_ARCH_TRACEHOOK |
Alexey Dobriyan | 09d93bd | 2014-08-08 14:21:39 -0700 | [diff] [blame] | 466 | static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns, |
| 467 | struct pid *pid, struct task_struct *task) |
Roland McGrath | ebcb673 | 2008-07-25 19:46:00 -0700 | [diff] [blame] | 468 | { |
| 469 | long nr; |
| 470 | unsigned long args[6], sp, pc; |
Joe Perches | 25ce319 | 2015-04-15 16:18:17 -0700 | [diff] [blame] | 471 | int res; |
| 472 | |
| 473 | res = lock_trace(task); |
Al Viro | a9712bc | 2011-03-23 15:52:50 -0400 | [diff] [blame] | 474 | if (res) |
| 475 | return res; |
Roland McGrath | ebcb673 | 2008-07-25 19:46:00 -0700 | [diff] [blame] | 476 | |
| 477 | if (task_current_syscall(task, &nr, args, 6, &sp, &pc)) |
Alexey Dobriyan | 09d93bd | 2014-08-08 14:21:39 -0700 | [diff] [blame] | 478 | seq_puts(m, "running\n"); |
Al Viro | a9712bc | 2011-03-23 15:52:50 -0400 | [diff] [blame] | 479 | else if (nr < 0) |
Alexey Dobriyan | 09d93bd | 2014-08-08 14:21:39 -0700 | [diff] [blame] | 480 | seq_printf(m, "%ld 0x%lx 0x%lx\n", nr, sp, pc); |
Al Viro | a9712bc | 2011-03-23 15:52:50 -0400 | [diff] [blame] | 481 | else |
Alexey Dobriyan | 09d93bd | 2014-08-08 14:21:39 -0700 | [diff] [blame] | 482 | seq_printf(m, |
Roland McGrath | ebcb673 | 2008-07-25 19:46:00 -0700 | [diff] [blame] | 483 | "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n", |
| 484 | nr, |
| 485 | args[0], args[1], args[2], args[3], args[4], args[5], |
| 486 | sp, pc); |
Al Viro | a9712bc | 2011-03-23 15:52:50 -0400 | [diff] [blame] | 487 | unlock_trace(task); |
Joe Perches | 25ce319 | 2015-04-15 16:18:17 -0700 | [diff] [blame] | 488 | |
| 489 | return 0; |
Roland McGrath | ebcb673 | 2008-07-25 19:46:00 -0700 | [diff] [blame] | 490 | } |
| 491 | #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */ |
| 492 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | /************************************************************************/ |
| 494 | /* Here the fs part begins */ |
| 495 | /************************************************************************/ |
| 496 | |
| 497 | /* permission checks */ |
Eric W. Biederman | 778c114 | 2006-06-26 00:25:58 -0700 | [diff] [blame] | 498 | static int proc_fd_access_allowed(struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | { |
Eric W. Biederman | 778c114 | 2006-06-26 00:25:58 -0700 | [diff] [blame] | 500 | struct task_struct *task; |
| 501 | int allowed = 0; |
Eric W. Biederman | df26c40 | 2006-06-26 00:25:59 -0700 | [diff] [blame] | 502 | /* Allow access to a task's file descriptors if it is us or we |
| 503 | * may use ptrace attach to the process and find out that |
| 504 | * information. |
Eric W. Biederman | 778c114 | 2006-06-26 00:25:58 -0700 | [diff] [blame] | 505 | */ |
| 506 | task = get_proc_task(inode); |
Eric W. Biederman | df26c40 | 2006-06-26 00:25:59 -0700 | [diff] [blame] | 507 | if (task) { |
Stephen Smalley | 006ebb4 | 2008-05-19 08:32:49 -0400 | [diff] [blame] | 508 | allowed = ptrace_may_access(task, PTRACE_MODE_READ); |
Eric W. Biederman | 778c114 | 2006-06-26 00:25:58 -0700 | [diff] [blame] | 509 | put_task_struct(task); |
Eric W. Biederman | df26c40 | 2006-06-26 00:25:59 -0700 | [diff] [blame] | 510 | } |
Eric W. Biederman | 778c114 | 2006-06-26 00:25:58 -0700 | [diff] [blame] | 511 | return allowed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | } |
| 513 | |
Eric W. Biederman | 6b4e306 | 2010-03-07 16:41:34 -0800 | [diff] [blame] | 514 | int proc_setattr(struct dentry *dentry, struct iattr *attr) |
Linus Torvalds | 6d76fa5 | 2006-07-15 12:26:45 -0700 | [diff] [blame] | 515 | { |
| 516 | int error; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 517 | struct inode *inode = d_inode(dentry); |
Linus Torvalds | 6d76fa5 | 2006-07-15 12:26:45 -0700 | [diff] [blame] | 518 | |
| 519 | if (attr->ia_valid & ATTR_MODE) |
| 520 | return -EPERM; |
| 521 | |
| 522 | error = inode_change_ok(inode, attr); |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 523 | if (error) |
| 524 | return error; |
| 525 | |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 526 | setattr_copy(inode, attr); |
| 527 | mark_inode_dirty(inode); |
| 528 | return 0; |
Linus Torvalds | 6d76fa5 | 2006-07-15 12:26:45 -0700 | [diff] [blame] | 529 | } |
| 530 | |
Vasiliy Kulikov | 0499680 | 2012-01-10 15:11:31 -0800 | [diff] [blame] | 531 | /* |
| 532 | * May current process learn task's sched/cmdline info (for hide_pid_min=1) |
| 533 | * or euid/egid (for hide_pid_min=2)? |
| 534 | */ |
| 535 | static bool has_pid_permissions(struct pid_namespace *pid, |
| 536 | struct task_struct *task, |
| 537 | int hide_pid_min) |
| 538 | { |
| 539 | if (pid->hide_pid < hide_pid_min) |
| 540 | return true; |
| 541 | if (in_group_p(pid->pid_gid)) |
| 542 | return true; |
| 543 | return ptrace_may_access(task, PTRACE_MODE_READ); |
| 544 | } |
| 545 | |
| 546 | |
| 547 | static int proc_pid_permission(struct inode *inode, int mask) |
| 548 | { |
| 549 | struct pid_namespace *pid = inode->i_sb->s_fs_info; |
| 550 | struct task_struct *task; |
| 551 | bool has_perms; |
| 552 | |
| 553 | task = get_proc_task(inode); |
Xiaotian Feng | a2ef990 | 2012-01-12 17:17:08 -0800 | [diff] [blame] | 554 | if (!task) |
| 555 | return -ESRCH; |
Vasiliy Kulikov | 0499680 | 2012-01-10 15:11:31 -0800 | [diff] [blame] | 556 | has_perms = has_pid_permissions(pid, task, 1); |
| 557 | put_task_struct(task); |
| 558 | |
| 559 | if (!has_perms) { |
| 560 | if (pid->hide_pid == 2) { |
| 561 | /* |
| 562 | * Let's make getdents(), stat(), and open() |
| 563 | * consistent with each other. If a process |
| 564 | * may not stat() a file, it shouldn't be seen |
| 565 | * in procfs at all. |
| 566 | */ |
| 567 | return -ENOENT; |
| 568 | } |
| 569 | |
| 570 | return -EPERM; |
| 571 | } |
| 572 | return generic_permission(inode, mask); |
| 573 | } |
| 574 | |
| 575 | |
| 576 | |
Arjan van de Ven | c5ef1c4 | 2007-02-12 00:55:40 -0800 | [diff] [blame] | 577 | static const struct inode_operations proc_def_inode_operations = { |
Linus Torvalds | 6d76fa5 | 2006-07-15 12:26:45 -0700 | [diff] [blame] | 578 | .setattr = proc_setattr, |
| 579 | }; |
| 580 | |
Eric W. Biederman | be61408 | 2008-02-08 04:18:30 -0800 | [diff] [blame] | 581 | static int proc_single_show(struct seq_file *m, void *v) |
| 582 | { |
| 583 | struct inode *inode = m->private; |
| 584 | struct pid_namespace *ns; |
| 585 | struct pid *pid; |
| 586 | struct task_struct *task; |
| 587 | int ret; |
| 588 | |
| 589 | ns = inode->i_sb->s_fs_info; |
| 590 | pid = proc_pid(inode); |
| 591 | task = get_pid_task(pid, PIDTYPE_PID); |
| 592 | if (!task) |
| 593 | return -ESRCH; |
| 594 | |
| 595 | ret = PROC_I(inode)->op.proc_show(m, ns, pid, task); |
| 596 | |
| 597 | put_task_struct(task); |
| 598 | return ret; |
| 599 | } |
| 600 | |
| 601 | static int proc_single_open(struct inode *inode, struct file *filp) |
| 602 | { |
Jovi Zhang | c6a3405 | 2011-01-12 17:00:34 -0800 | [diff] [blame] | 603 | return single_open(filp, proc_single_show, inode); |
Eric W. Biederman | be61408 | 2008-02-08 04:18:30 -0800 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | static const struct file_operations proc_single_file_operations = { |
| 607 | .open = proc_single_open, |
| 608 | .read = seq_read, |
| 609 | .llseek = seq_lseek, |
| 610 | .release = single_release, |
| 611 | }; |
| 612 | |
Oleg Nesterov | 5381e16 | 2014-10-09 15:25:24 -0700 | [diff] [blame] | 613 | |
| 614 | struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode) |
| 615 | { |
| 616 | struct task_struct *task = get_proc_task(inode); |
| 617 | struct mm_struct *mm = ERR_PTR(-ESRCH); |
| 618 | |
| 619 | if (task) { |
| 620 | mm = mm_access(task, mode); |
| 621 | put_task_struct(task); |
| 622 | |
| 623 | if (!IS_ERR_OR_NULL(mm)) { |
| 624 | /* ensure this mm_struct can't be freed */ |
| 625 | atomic_inc(&mm->mm_count); |
| 626 | /* but do not pin its memory */ |
| 627 | mmput(mm); |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | return mm; |
| 632 | } |
| 633 | |
Cong Wang | b409e57 | 2012-05-31 16:26:17 -0700 | [diff] [blame] | 634 | static int __mem_open(struct inode *inode, struct file *file, unsigned int mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | { |
Oleg Nesterov | 5381e16 | 2014-10-09 15:25:24 -0700 | [diff] [blame] | 636 | struct mm_struct *mm = proc_mem_open(inode, mode); |
Linus Torvalds | e268337 | 2012-01-17 15:21:19 -0800 | [diff] [blame] | 637 | |
| 638 | if (IS_ERR(mm)) |
| 639 | return PTR_ERR(mm); |
| 640 | |
Linus Torvalds | e268337 | 2012-01-17 15:21:19 -0800 | [diff] [blame] | 641 | file->private_data = mm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | return 0; |
| 643 | } |
| 644 | |
Cong Wang | b409e57 | 2012-05-31 16:26:17 -0700 | [diff] [blame] | 645 | static int mem_open(struct inode *inode, struct file *file) |
| 646 | { |
Djalal Harouni | bc452b4 | 2012-07-30 14:42:28 -0700 | [diff] [blame] | 647 | int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH); |
| 648 | |
| 649 | /* OK to pass negative loff_t, we can catch out-of-range */ |
| 650 | file->f_mode |= FMODE_UNSIGNED_OFFSET; |
| 651 | |
| 652 | return ret; |
Cong Wang | b409e57 | 2012-05-31 16:26:17 -0700 | [diff] [blame] | 653 | } |
| 654 | |
Oleg Nesterov | 572d34b | 2012-01-31 17:14:54 +0100 | [diff] [blame] | 655 | static ssize_t mem_rw(struct file *file, char __user *buf, |
| 656 | size_t count, loff_t *ppos, int write) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | { |
Linus Torvalds | e268337 | 2012-01-17 15:21:19 -0800 | [diff] [blame] | 658 | struct mm_struct *mm = file->private_data; |
Oleg Nesterov | 572d34b | 2012-01-31 17:14:54 +0100 | [diff] [blame] | 659 | unsigned long addr = *ppos; |
| 660 | ssize_t copied; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | char *page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | |
Linus Torvalds | e268337 | 2012-01-17 15:21:19 -0800 | [diff] [blame] | 663 | if (!mm) |
| 664 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | |
Mel Gorman | e12ba74 | 2007-10-16 01:25:52 -0700 | [diff] [blame] | 666 | page = (char *)__get_free_page(GFP_TEMPORARY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | if (!page) |
Linus Torvalds | e268337 | 2012-01-17 15:21:19 -0800 | [diff] [blame] | 668 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | |
Frederik Deweerdt | f7ca54f | 2006-09-29 02:01:02 -0700 | [diff] [blame] | 670 | copied = 0; |
Oleg Nesterov | 6d08f2c | 2012-01-31 17:15:11 +0100 | [diff] [blame] | 671 | if (!atomic_inc_not_zero(&mm->mm_users)) |
| 672 | goto free; |
| 673 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | while (count > 0) { |
Oleg Nesterov | 572d34b | 2012-01-31 17:14:54 +0100 | [diff] [blame] | 675 | int this_len = min_t(int, count, PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | |
Oleg Nesterov | 572d34b | 2012-01-31 17:14:54 +0100 | [diff] [blame] | 677 | if (write && copy_from_user(page, buf, this_len)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | copied = -EFAULT; |
| 679 | break; |
| 680 | } |
Oleg Nesterov | 572d34b | 2012-01-31 17:14:54 +0100 | [diff] [blame] | 681 | |
| 682 | this_len = access_remote_vm(mm, addr, page, this_len, write); |
| 683 | if (!this_len) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | if (!copied) |
| 685 | copied = -EIO; |
| 686 | break; |
| 687 | } |
Oleg Nesterov | 572d34b | 2012-01-31 17:14:54 +0100 | [diff] [blame] | 688 | |
| 689 | if (!write && copy_to_user(buf, page, this_len)) { |
| 690 | copied = -EFAULT; |
| 691 | break; |
| 692 | } |
| 693 | |
| 694 | buf += this_len; |
| 695 | addr += this_len; |
| 696 | copied += this_len; |
| 697 | count -= this_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | } |
Oleg Nesterov | 572d34b | 2012-01-31 17:14:54 +0100 | [diff] [blame] | 699 | *ppos = addr; |
KOSAKI Motohiro | 30cd890 | 2011-05-26 16:25:52 -0700 | [diff] [blame] | 700 | |
Oleg Nesterov | 6d08f2c | 2012-01-31 17:15:11 +0100 | [diff] [blame] | 701 | mmput(mm); |
| 702 | free: |
KOSAKI Motohiro | 30cd890 | 2011-05-26 16:25:52 -0700 | [diff] [blame] | 703 | free_page((unsigned long) page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | return copied; |
| 705 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | |
Oleg Nesterov | 572d34b | 2012-01-31 17:14:54 +0100 | [diff] [blame] | 707 | static ssize_t mem_read(struct file *file, char __user *buf, |
| 708 | size_t count, loff_t *ppos) |
| 709 | { |
| 710 | return mem_rw(file, buf, count, ppos, 0); |
| 711 | } |
| 712 | |
| 713 | static ssize_t mem_write(struct file *file, const char __user *buf, |
| 714 | size_t count, loff_t *ppos) |
| 715 | { |
| 716 | return mem_rw(file, (char __user*)buf, count, ppos, 1); |
| 717 | } |
| 718 | |
Matt Mackall | 85863e4 | 2008-02-04 22:29:04 -0800 | [diff] [blame] | 719 | loff_t mem_lseek(struct file *file, loff_t offset, int orig) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | { |
| 721 | switch (orig) { |
| 722 | case 0: |
| 723 | file->f_pos = offset; |
| 724 | break; |
| 725 | case 1: |
| 726 | file->f_pos += offset; |
| 727 | break; |
| 728 | default: |
| 729 | return -EINVAL; |
| 730 | } |
| 731 | force_successful_syscall_return(); |
| 732 | return file->f_pos; |
| 733 | } |
| 734 | |
Linus Torvalds | e268337 | 2012-01-17 15:21:19 -0800 | [diff] [blame] | 735 | static int mem_release(struct inode *inode, struct file *file) |
| 736 | { |
| 737 | struct mm_struct *mm = file->private_data; |
Oleg Nesterov | 71879d3 | 2012-01-31 17:14:38 +0100 | [diff] [blame] | 738 | if (mm) |
Oleg Nesterov | 6d08f2c | 2012-01-31 17:15:11 +0100 | [diff] [blame] | 739 | mmdrop(mm); |
Linus Torvalds | e268337 | 2012-01-17 15:21:19 -0800 | [diff] [blame] | 740 | return 0; |
| 741 | } |
| 742 | |
Arjan van de Ven | 00977a5 | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 743 | static const struct file_operations proc_mem_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | .llseek = mem_lseek, |
| 745 | .read = mem_read, |
| 746 | .write = mem_write, |
| 747 | .open = mem_open, |
Linus Torvalds | e268337 | 2012-01-17 15:21:19 -0800 | [diff] [blame] | 748 | .release = mem_release, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | }; |
| 750 | |
Cong Wang | b409e57 | 2012-05-31 16:26:17 -0700 | [diff] [blame] | 751 | static int environ_open(struct inode *inode, struct file *file) |
| 752 | { |
| 753 | return __mem_open(inode, file, PTRACE_MODE_READ); |
| 754 | } |
| 755 | |
James Pearson | 315e28c | 2007-10-16 23:30:17 -0700 | [diff] [blame] | 756 | static ssize_t environ_read(struct file *file, char __user *buf, |
| 757 | size_t count, loff_t *ppos) |
| 758 | { |
James Pearson | 315e28c | 2007-10-16 23:30:17 -0700 | [diff] [blame] | 759 | char *page; |
| 760 | unsigned long src = *ppos; |
Cong Wang | b409e57 | 2012-05-31 16:26:17 -0700 | [diff] [blame] | 761 | int ret = 0; |
| 762 | struct mm_struct *mm = file->private_data; |
James Pearson | 315e28c | 2007-10-16 23:30:17 -0700 | [diff] [blame] | 763 | |
Cong Wang | b409e57 | 2012-05-31 16:26:17 -0700 | [diff] [blame] | 764 | if (!mm) |
| 765 | return 0; |
James Pearson | 315e28c | 2007-10-16 23:30:17 -0700 | [diff] [blame] | 766 | |
James Pearson | 315e28c | 2007-10-16 23:30:17 -0700 | [diff] [blame] | 767 | page = (char *)__get_free_page(GFP_TEMPORARY); |
| 768 | if (!page) |
Cong Wang | b409e57 | 2012-05-31 16:26:17 -0700 | [diff] [blame] | 769 | return -ENOMEM; |
James Pearson | 315e28c | 2007-10-16 23:30:17 -0700 | [diff] [blame] | 770 | |
Al Viro | d6f64b8 | 2011-02-15 22:26:01 -0500 | [diff] [blame] | 771 | ret = 0; |
Cong Wang | b409e57 | 2012-05-31 16:26:17 -0700 | [diff] [blame] | 772 | if (!atomic_inc_not_zero(&mm->mm_users)) |
| 773 | goto free; |
James Pearson | 315e28c | 2007-10-16 23:30:17 -0700 | [diff] [blame] | 774 | while (count > 0) { |
Djalal Harouni | e8905ec | 2012-07-30 14:42:26 -0700 | [diff] [blame] | 775 | size_t this_len, max_len; |
| 776 | int retval; |
| 777 | |
| 778 | if (src >= (mm->env_end - mm->env_start)) |
| 779 | break; |
James Pearson | 315e28c | 2007-10-16 23:30:17 -0700 | [diff] [blame] | 780 | |
| 781 | this_len = mm->env_end - (mm->env_start + src); |
| 782 | |
Djalal Harouni | e8905ec | 2012-07-30 14:42:26 -0700 | [diff] [blame] | 783 | max_len = min_t(size_t, PAGE_SIZE, count); |
| 784 | this_len = min(max_len, this_len); |
James Pearson | 315e28c | 2007-10-16 23:30:17 -0700 | [diff] [blame] | 785 | |
Cong Wang | b409e57 | 2012-05-31 16:26:17 -0700 | [diff] [blame] | 786 | retval = access_remote_vm(mm, (mm->env_start + src), |
James Pearson | 315e28c | 2007-10-16 23:30:17 -0700 | [diff] [blame] | 787 | page, this_len, 0); |
| 788 | |
| 789 | if (retval <= 0) { |
| 790 | ret = retval; |
| 791 | break; |
| 792 | } |
| 793 | |
| 794 | if (copy_to_user(buf, page, retval)) { |
| 795 | ret = -EFAULT; |
| 796 | break; |
| 797 | } |
| 798 | |
| 799 | ret += retval; |
| 800 | src += retval; |
| 801 | buf += retval; |
| 802 | count -= retval; |
| 803 | } |
| 804 | *ppos = src; |
James Pearson | 315e28c | 2007-10-16 23:30:17 -0700 | [diff] [blame] | 805 | mmput(mm); |
Cong Wang | b409e57 | 2012-05-31 16:26:17 -0700 | [diff] [blame] | 806 | |
| 807 | free: |
James Pearson | 315e28c | 2007-10-16 23:30:17 -0700 | [diff] [blame] | 808 | free_page((unsigned long) page); |
James Pearson | 315e28c | 2007-10-16 23:30:17 -0700 | [diff] [blame] | 809 | return ret; |
| 810 | } |
| 811 | |
| 812 | static const struct file_operations proc_environ_operations = { |
Cong Wang | b409e57 | 2012-05-31 16:26:17 -0700 | [diff] [blame] | 813 | .open = environ_open, |
James Pearson | 315e28c | 2007-10-16 23:30:17 -0700 | [diff] [blame] | 814 | .read = environ_read, |
Arnd Bergmann | 87df842 | 2010-03-17 23:06:02 +0100 | [diff] [blame] | 815 | .llseek = generic_file_llseek, |
Cong Wang | b409e57 | 2012-05-31 16:26:17 -0700 | [diff] [blame] | 816 | .release = mem_release, |
James Pearson | 315e28c | 2007-10-16 23:30:17 -0700 | [diff] [blame] | 817 | }; |
| 818 | |
David Rientjes | fa0cbbf | 2012-11-12 17:53:04 -0800 | [diff] [blame] | 819 | static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count, |
| 820 | loff_t *ppos) |
| 821 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 822 | struct task_struct *task = get_proc_task(file_inode(file)); |
David Rientjes | fa0cbbf | 2012-11-12 17:53:04 -0800 | [diff] [blame] | 823 | char buffer[PROC_NUMBUF]; |
| 824 | int oom_adj = OOM_ADJUST_MIN; |
| 825 | size_t len; |
| 826 | unsigned long flags; |
| 827 | |
| 828 | if (!task) |
| 829 | return -ESRCH; |
| 830 | if (lock_task_sighand(task, &flags)) { |
| 831 | if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX) |
| 832 | oom_adj = OOM_ADJUST_MAX; |
| 833 | else |
| 834 | oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) / |
| 835 | OOM_SCORE_ADJ_MAX; |
| 836 | unlock_task_sighand(task, &flags); |
| 837 | } |
| 838 | put_task_struct(task); |
| 839 | len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj); |
| 840 | return simple_read_from_buffer(buf, count, ppos, buffer, len); |
| 841 | } |
| 842 | |
| 843 | static ssize_t oom_adj_write(struct file *file, const char __user *buf, |
| 844 | size_t count, loff_t *ppos) |
| 845 | { |
| 846 | struct task_struct *task; |
| 847 | char buffer[PROC_NUMBUF]; |
| 848 | int oom_adj; |
| 849 | unsigned long flags; |
| 850 | int err; |
| 851 | |
| 852 | memset(buffer, 0, sizeof(buffer)); |
| 853 | if (count > sizeof(buffer) - 1) |
| 854 | count = sizeof(buffer) - 1; |
| 855 | if (copy_from_user(buffer, buf, count)) { |
| 856 | err = -EFAULT; |
| 857 | goto out; |
| 858 | } |
| 859 | |
| 860 | err = kstrtoint(strstrip(buffer), 0, &oom_adj); |
| 861 | if (err) |
| 862 | goto out; |
| 863 | if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) && |
| 864 | oom_adj != OOM_DISABLE) { |
| 865 | err = -EINVAL; |
| 866 | goto out; |
| 867 | } |
| 868 | |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 869 | task = get_proc_task(file_inode(file)); |
David Rientjes | fa0cbbf | 2012-11-12 17:53:04 -0800 | [diff] [blame] | 870 | if (!task) { |
| 871 | err = -ESRCH; |
| 872 | goto out; |
| 873 | } |
| 874 | |
| 875 | task_lock(task); |
| 876 | if (!task->mm) { |
| 877 | err = -EINVAL; |
| 878 | goto err_task_lock; |
| 879 | } |
| 880 | |
| 881 | if (!lock_task_sighand(task, &flags)) { |
| 882 | err = -ESRCH; |
| 883 | goto err_task_lock; |
| 884 | } |
| 885 | |
| 886 | /* |
| 887 | * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum |
| 888 | * value is always attainable. |
| 889 | */ |
| 890 | if (oom_adj == OOM_ADJUST_MAX) |
| 891 | oom_adj = OOM_SCORE_ADJ_MAX; |
| 892 | else |
| 893 | oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE; |
| 894 | |
| 895 | if (oom_adj < task->signal->oom_score_adj && |
| 896 | !capable(CAP_SYS_RESOURCE)) { |
| 897 | err = -EACCES; |
| 898 | goto err_sighand; |
| 899 | } |
| 900 | |
| 901 | /* |
| 902 | * /proc/pid/oom_adj is provided for legacy purposes, ask users to use |
| 903 | * /proc/pid/oom_score_adj instead. |
| 904 | */ |
Andrew Morton | 87ebdc0 | 2013-02-27 17:03:16 -0800 | [diff] [blame] | 905 | pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n", |
David Rientjes | fa0cbbf | 2012-11-12 17:53:04 -0800 | [diff] [blame] | 906 | current->comm, task_pid_nr(current), task_pid_nr(task), |
| 907 | task_pid_nr(task)); |
| 908 | |
| 909 | task->signal->oom_score_adj = oom_adj; |
| 910 | trace_oom_score_adj_update(task); |
| 911 | err_sighand: |
| 912 | unlock_task_sighand(task, &flags); |
| 913 | err_task_lock: |
| 914 | task_unlock(task); |
| 915 | put_task_struct(task); |
| 916 | out: |
| 917 | return err < 0 ? err : count; |
| 918 | } |
| 919 | |
| 920 | static const struct file_operations proc_oom_adj_operations = { |
| 921 | .read = oom_adj_read, |
| 922 | .write = oom_adj_write, |
| 923 | .llseek = generic_file_llseek, |
| 924 | }; |
| 925 | |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 926 | static ssize_t oom_score_adj_read(struct file *file, char __user *buf, |
| 927 | size_t count, loff_t *ppos) |
| 928 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 929 | struct task_struct *task = get_proc_task(file_inode(file)); |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 930 | char buffer[PROC_NUMBUF]; |
David Rientjes | a9c58b90 | 2012-12-11 16:02:54 -0800 | [diff] [blame] | 931 | short oom_score_adj = OOM_SCORE_ADJ_MIN; |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 932 | unsigned long flags; |
| 933 | size_t len; |
| 934 | |
| 935 | if (!task) |
| 936 | return -ESRCH; |
| 937 | if (lock_task_sighand(task, &flags)) { |
| 938 | oom_score_adj = task->signal->oom_score_adj; |
| 939 | unlock_task_sighand(task, &flags); |
| 940 | } |
| 941 | put_task_struct(task); |
David Rientjes | a9c58b90 | 2012-12-11 16:02:54 -0800 | [diff] [blame] | 942 | len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj); |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 943 | return simple_read_from_buffer(buf, count, ppos, buffer, len); |
| 944 | } |
| 945 | |
| 946 | static ssize_t oom_score_adj_write(struct file *file, const char __user *buf, |
| 947 | size_t count, loff_t *ppos) |
| 948 | { |
| 949 | struct task_struct *task; |
| 950 | char buffer[PROC_NUMBUF]; |
| 951 | unsigned long flags; |
Alexey Dobriyan | 0a8cb8e | 2011-05-26 16:25:50 -0700 | [diff] [blame] | 952 | int oom_score_adj; |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 953 | int err; |
| 954 | |
| 955 | memset(buffer, 0, sizeof(buffer)); |
| 956 | if (count > sizeof(buffer) - 1) |
| 957 | count = sizeof(buffer) - 1; |
David Rientjes | 723548b | 2010-10-26 14:21:25 -0700 | [diff] [blame] | 958 | if (copy_from_user(buffer, buf, count)) { |
| 959 | err = -EFAULT; |
| 960 | goto out; |
| 961 | } |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 962 | |
Alexey Dobriyan | 0a8cb8e | 2011-05-26 16:25:50 -0700 | [diff] [blame] | 963 | err = kstrtoint(strstrip(buffer), 0, &oom_score_adj); |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 964 | if (err) |
David Rientjes | 723548b | 2010-10-26 14:21:25 -0700 | [diff] [blame] | 965 | goto out; |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 966 | if (oom_score_adj < OOM_SCORE_ADJ_MIN || |
David Rientjes | 723548b | 2010-10-26 14:21:25 -0700 | [diff] [blame] | 967 | oom_score_adj > OOM_SCORE_ADJ_MAX) { |
| 968 | err = -EINVAL; |
| 969 | goto out; |
| 970 | } |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 971 | |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 972 | task = get_proc_task(file_inode(file)); |
David Rientjes | 723548b | 2010-10-26 14:21:25 -0700 | [diff] [blame] | 973 | if (!task) { |
| 974 | err = -ESRCH; |
| 975 | goto out; |
| 976 | } |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 977 | |
Ying Han | 3d5992d | 2010-10-26 14:21:23 -0700 | [diff] [blame] | 978 | task_lock(task); |
| 979 | if (!task->mm) { |
David Rientjes | 723548b | 2010-10-26 14:21:25 -0700 | [diff] [blame] | 980 | err = -EINVAL; |
| 981 | goto err_task_lock; |
Ying Han | 3d5992d | 2010-10-26 14:21:23 -0700 | [diff] [blame] | 982 | } |
David Rientjes | d19d547 | 2010-10-26 14:21:26 -0700 | [diff] [blame] | 983 | |
| 984 | if (!lock_task_sighand(task, &flags)) { |
| 985 | err = -ESRCH; |
| 986 | goto err_task_lock; |
| 987 | } |
| 988 | |
David Rientjes | a9c58b90 | 2012-12-11 16:02:54 -0800 | [diff] [blame] | 989 | if ((short)oom_score_adj < task->signal->oom_score_adj_min && |
David Rientjes | d19d547 | 2010-10-26 14:21:26 -0700 | [diff] [blame] | 990 | !capable(CAP_SYS_RESOURCE)) { |
| 991 | err = -EACCES; |
| 992 | goto err_sighand; |
| 993 | } |
| 994 | |
David Rientjes | a9c58b90 | 2012-12-11 16:02:54 -0800 | [diff] [blame] | 995 | task->signal->oom_score_adj = (short)oom_score_adj; |
Mandeep Singh Baines | dabb16f | 2011-01-13 15:46:05 -0800 | [diff] [blame] | 996 | if (has_capability_noaudit(current, CAP_SYS_RESOURCE)) |
David Rientjes | a9c58b90 | 2012-12-11 16:02:54 -0800 | [diff] [blame] | 997 | task->signal->oom_score_adj_min = (short)oom_score_adj; |
KAMEZAWA Hiroyuki | 43d2b11 | 2012-01-10 15:08:09 -0800 | [diff] [blame] | 998 | trace_oom_score_adj_update(task); |
Davidlohr Bueso | 01dc52e | 2012-10-08 16:29:30 -0700 | [diff] [blame] | 999 | |
David Rientjes | 723548b | 2010-10-26 14:21:25 -0700 | [diff] [blame] | 1000 | err_sighand: |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 1001 | unlock_task_sighand(task, &flags); |
David Rientjes | d19d547 | 2010-10-26 14:21:26 -0700 | [diff] [blame] | 1002 | err_task_lock: |
| 1003 | task_unlock(task); |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 1004 | put_task_struct(task); |
David Rientjes | 723548b | 2010-10-26 14:21:25 -0700 | [diff] [blame] | 1005 | out: |
| 1006 | return err < 0 ? err : count; |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 1007 | } |
| 1008 | |
| 1009 | static const struct file_operations proc_oom_score_adj_operations = { |
| 1010 | .read = oom_score_adj_read, |
| 1011 | .write = oom_score_adj_write, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 1012 | .llseek = default_llseek, |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 1013 | }; |
| 1014 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | #ifdef CONFIG_AUDITSYSCALL |
| 1016 | #define TMPBUFLEN 21 |
| 1017 | static ssize_t proc_loginuid_read(struct file * file, char __user * buf, |
| 1018 | size_t count, loff_t *ppos) |
| 1019 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1020 | struct inode * inode = file_inode(file); |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 1021 | struct task_struct *task = get_proc_task(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1022 | ssize_t length; |
| 1023 | char tmpbuf[TMPBUFLEN]; |
| 1024 | |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 1025 | if (!task) |
| 1026 | return -ESRCH; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1027 | length = scnprintf(tmpbuf, TMPBUFLEN, "%u", |
Eric W. Biederman | e1760bd | 2012-09-10 22:39:43 -0700 | [diff] [blame] | 1028 | from_kuid(file->f_cred->user_ns, |
| 1029 | audit_get_loginuid(task))); |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 1030 | put_task_struct(task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); |
| 1032 | } |
| 1033 | |
| 1034 | static ssize_t proc_loginuid_write(struct file * file, const char __user * buf, |
| 1035 | size_t count, loff_t *ppos) |
| 1036 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1037 | struct inode * inode = file_inode(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | char *page, *tmp; |
| 1039 | ssize_t length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1040 | uid_t loginuid; |
Eric W. Biederman | e1760bd | 2012-09-10 22:39:43 -0700 | [diff] [blame] | 1041 | kuid_t kloginuid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | |
Paul E. McKenney | 7dc5215 | 2010-02-22 17:04:52 -0800 | [diff] [blame] | 1043 | rcu_read_lock(); |
| 1044 | if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) { |
| 1045 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1046 | return -EPERM; |
Paul E. McKenney | 7dc5215 | 2010-02-22 17:04:52 -0800 | [diff] [blame] | 1047 | } |
| 1048 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | |
Al Viro | e018290 | 2006-05-18 08:28:02 -0400 | [diff] [blame] | 1050 | if (count >= PAGE_SIZE) |
| 1051 | count = PAGE_SIZE - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1052 | |
| 1053 | if (*ppos != 0) { |
| 1054 | /* No partial writes. */ |
| 1055 | return -EINVAL; |
| 1056 | } |
Mel Gorman | e12ba74 | 2007-10-16 01:25:52 -0700 | [diff] [blame] | 1057 | page = (char*)__get_free_page(GFP_TEMPORARY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1058 | if (!page) |
| 1059 | return -ENOMEM; |
| 1060 | length = -EFAULT; |
| 1061 | if (copy_from_user(page, buf, count)) |
| 1062 | goto out_free_page; |
| 1063 | |
Al Viro | e018290 | 2006-05-18 08:28:02 -0400 | [diff] [blame] | 1064 | page[count] = '\0'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1065 | loginuid = simple_strtoul(page, &tmp, 10); |
| 1066 | if (tmp == page) { |
| 1067 | length = -EINVAL; |
| 1068 | goto out_free_page; |
| 1069 | |
| 1070 | } |
Eric Paris | 81407c8 | 2013-05-24 09:49:14 -0400 | [diff] [blame] | 1071 | |
| 1072 | /* is userspace tring to explicitly UNSET the loginuid? */ |
| 1073 | if (loginuid == AUDIT_UID_UNSET) { |
| 1074 | kloginuid = INVALID_UID; |
| 1075 | } else { |
| 1076 | kloginuid = make_kuid(file->f_cred->user_ns, loginuid); |
| 1077 | if (!uid_valid(kloginuid)) { |
| 1078 | length = -EINVAL; |
| 1079 | goto out_free_page; |
| 1080 | } |
Eric W. Biederman | e1760bd | 2012-09-10 22:39:43 -0700 | [diff] [blame] | 1081 | } |
| 1082 | |
| 1083 | length = audit_set_loginuid(kloginuid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1084 | if (likely(length == 0)) |
| 1085 | length = count; |
| 1086 | |
| 1087 | out_free_page: |
| 1088 | free_page((unsigned long) page); |
| 1089 | return length; |
| 1090 | } |
| 1091 | |
Arjan van de Ven | 00977a5 | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 1092 | static const struct file_operations proc_loginuid_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1093 | .read = proc_loginuid_read, |
| 1094 | .write = proc_loginuid_write, |
Arnd Bergmann | 87df842 | 2010-03-17 23:06:02 +0100 | [diff] [blame] | 1095 | .llseek = generic_file_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1096 | }; |
Eric Paris | 1e0bd75 | 2008-03-13 08:15:31 -0400 | [diff] [blame] | 1097 | |
| 1098 | static ssize_t proc_sessionid_read(struct file * file, char __user * buf, |
| 1099 | size_t count, loff_t *ppos) |
| 1100 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1101 | struct inode * inode = file_inode(file); |
Eric Paris | 1e0bd75 | 2008-03-13 08:15:31 -0400 | [diff] [blame] | 1102 | struct task_struct *task = get_proc_task(inode); |
| 1103 | ssize_t length; |
| 1104 | char tmpbuf[TMPBUFLEN]; |
| 1105 | |
| 1106 | if (!task) |
| 1107 | return -ESRCH; |
| 1108 | length = scnprintf(tmpbuf, TMPBUFLEN, "%u", |
| 1109 | audit_get_sessionid(task)); |
| 1110 | put_task_struct(task); |
| 1111 | return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); |
| 1112 | } |
| 1113 | |
| 1114 | static const struct file_operations proc_sessionid_operations = { |
| 1115 | .read = proc_sessionid_read, |
Arnd Bergmann | 87df842 | 2010-03-17 23:06:02 +0100 | [diff] [blame] | 1116 | .llseek = generic_file_llseek, |
Eric Paris | 1e0bd75 | 2008-03-13 08:15:31 -0400 | [diff] [blame] | 1117 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1118 | #endif |
| 1119 | |
Akinobu Mita | f4f154f | 2006-12-08 02:39:47 -0800 | [diff] [blame] | 1120 | #ifdef CONFIG_FAULT_INJECTION |
| 1121 | static ssize_t proc_fault_inject_read(struct file * file, char __user * buf, |
| 1122 | size_t count, loff_t *ppos) |
| 1123 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1124 | struct task_struct *task = get_proc_task(file_inode(file)); |
Akinobu Mita | f4f154f | 2006-12-08 02:39:47 -0800 | [diff] [blame] | 1125 | char buffer[PROC_NUMBUF]; |
| 1126 | size_t len; |
| 1127 | int make_it_fail; |
Akinobu Mita | f4f154f | 2006-12-08 02:39:47 -0800 | [diff] [blame] | 1128 | |
| 1129 | if (!task) |
| 1130 | return -ESRCH; |
| 1131 | make_it_fail = task->make_it_fail; |
| 1132 | put_task_struct(task); |
| 1133 | |
| 1134 | len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail); |
Akinobu Mita | 0c28f28 | 2007-05-08 00:31:41 -0700 | [diff] [blame] | 1135 | |
| 1136 | return simple_read_from_buffer(buf, count, ppos, buffer, len); |
Akinobu Mita | f4f154f | 2006-12-08 02:39:47 -0800 | [diff] [blame] | 1137 | } |
| 1138 | |
| 1139 | static ssize_t proc_fault_inject_write(struct file * file, |
| 1140 | const char __user * buf, size_t count, loff_t *ppos) |
| 1141 | { |
| 1142 | struct task_struct *task; |
| 1143 | char buffer[PROC_NUMBUF], *end; |
| 1144 | int make_it_fail; |
| 1145 | |
| 1146 | if (!capable(CAP_SYS_RESOURCE)) |
| 1147 | return -EPERM; |
| 1148 | memset(buffer, 0, sizeof(buffer)); |
| 1149 | if (count > sizeof(buffer) - 1) |
| 1150 | count = sizeof(buffer) - 1; |
| 1151 | if (copy_from_user(buffer, buf, count)) |
| 1152 | return -EFAULT; |
Vincent Li | cba8aaf | 2009-09-22 16:45:38 -0700 | [diff] [blame] | 1153 | make_it_fail = simple_strtol(strstrip(buffer), &end, 0); |
| 1154 | if (*end) |
| 1155 | return -EINVAL; |
Dave Jones | 16caed3 | 2014-04-07 15:39:15 -0700 | [diff] [blame] | 1156 | if (make_it_fail < 0 || make_it_fail > 1) |
| 1157 | return -EINVAL; |
| 1158 | |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1159 | task = get_proc_task(file_inode(file)); |
Akinobu Mita | f4f154f | 2006-12-08 02:39:47 -0800 | [diff] [blame] | 1160 | if (!task) |
| 1161 | return -ESRCH; |
| 1162 | task->make_it_fail = make_it_fail; |
| 1163 | put_task_struct(task); |
Vincent Li | cba8aaf | 2009-09-22 16:45:38 -0700 | [diff] [blame] | 1164 | |
| 1165 | return count; |
Akinobu Mita | f4f154f | 2006-12-08 02:39:47 -0800 | [diff] [blame] | 1166 | } |
| 1167 | |
Arjan van de Ven | 00977a5 | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 1168 | static const struct file_operations proc_fault_inject_operations = { |
Akinobu Mita | f4f154f | 2006-12-08 02:39:47 -0800 | [diff] [blame] | 1169 | .read = proc_fault_inject_read, |
| 1170 | .write = proc_fault_inject_write, |
Arnd Bergmann | 87df842 | 2010-03-17 23:06:02 +0100 | [diff] [blame] | 1171 | .llseek = generic_file_llseek, |
Akinobu Mita | f4f154f | 2006-12-08 02:39:47 -0800 | [diff] [blame] | 1172 | }; |
| 1173 | #endif |
| 1174 | |
Arjan van de Ven | 9745512 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 1175 | |
Ingo Molnar | 43ae34c | 2007-07-09 18:52:00 +0200 | [diff] [blame] | 1176 | #ifdef CONFIG_SCHED_DEBUG |
| 1177 | /* |
| 1178 | * Print out various scheduling related per-task fields: |
| 1179 | */ |
| 1180 | static int sched_show(struct seq_file *m, void *v) |
| 1181 | { |
| 1182 | struct inode *inode = m->private; |
| 1183 | struct task_struct *p; |
| 1184 | |
Ingo Molnar | 43ae34c | 2007-07-09 18:52:00 +0200 | [diff] [blame] | 1185 | p = get_proc_task(inode); |
| 1186 | if (!p) |
| 1187 | return -ESRCH; |
| 1188 | proc_sched_show_task(p, m); |
| 1189 | |
| 1190 | put_task_struct(p); |
| 1191 | |
| 1192 | return 0; |
| 1193 | } |
| 1194 | |
| 1195 | static ssize_t |
| 1196 | sched_write(struct file *file, const char __user *buf, |
| 1197 | size_t count, loff_t *offset) |
| 1198 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1199 | struct inode *inode = file_inode(file); |
Ingo Molnar | 43ae34c | 2007-07-09 18:52:00 +0200 | [diff] [blame] | 1200 | struct task_struct *p; |
| 1201 | |
Ingo Molnar | 43ae34c | 2007-07-09 18:52:00 +0200 | [diff] [blame] | 1202 | p = get_proc_task(inode); |
| 1203 | if (!p) |
| 1204 | return -ESRCH; |
| 1205 | proc_sched_set_task(p); |
| 1206 | |
| 1207 | put_task_struct(p); |
| 1208 | |
| 1209 | return count; |
| 1210 | } |
| 1211 | |
| 1212 | static int sched_open(struct inode *inode, struct file *filp) |
| 1213 | { |
Jovi Zhang | c6a3405 | 2011-01-12 17:00:34 -0800 | [diff] [blame] | 1214 | return single_open(filp, sched_show, inode); |
Ingo Molnar | 43ae34c | 2007-07-09 18:52:00 +0200 | [diff] [blame] | 1215 | } |
| 1216 | |
| 1217 | static const struct file_operations proc_pid_sched_operations = { |
| 1218 | .open = sched_open, |
| 1219 | .read = seq_read, |
| 1220 | .write = sched_write, |
| 1221 | .llseek = seq_lseek, |
Alexey Dobriyan | 5ea473a | 2007-07-31 00:38:50 -0700 | [diff] [blame] | 1222 | .release = single_release, |
Ingo Molnar | 43ae34c | 2007-07-09 18:52:00 +0200 | [diff] [blame] | 1223 | }; |
| 1224 | |
| 1225 | #endif |
| 1226 | |
Mike Galbraith | 5091faa | 2010-11-30 14:18:03 +0100 | [diff] [blame] | 1227 | #ifdef CONFIG_SCHED_AUTOGROUP |
| 1228 | /* |
| 1229 | * Print out autogroup related information: |
| 1230 | */ |
| 1231 | static int sched_autogroup_show(struct seq_file *m, void *v) |
| 1232 | { |
| 1233 | struct inode *inode = m->private; |
| 1234 | struct task_struct *p; |
| 1235 | |
| 1236 | p = get_proc_task(inode); |
| 1237 | if (!p) |
| 1238 | return -ESRCH; |
| 1239 | proc_sched_autogroup_show_task(p, m); |
| 1240 | |
| 1241 | put_task_struct(p); |
| 1242 | |
| 1243 | return 0; |
| 1244 | } |
| 1245 | |
| 1246 | static ssize_t |
| 1247 | sched_autogroup_write(struct file *file, const char __user *buf, |
| 1248 | size_t count, loff_t *offset) |
| 1249 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1250 | struct inode *inode = file_inode(file); |
Mike Galbraith | 5091faa | 2010-11-30 14:18:03 +0100 | [diff] [blame] | 1251 | struct task_struct *p; |
| 1252 | char buffer[PROC_NUMBUF]; |
Alexey Dobriyan | 0a8cb8e | 2011-05-26 16:25:50 -0700 | [diff] [blame] | 1253 | int nice; |
Mike Galbraith | 5091faa | 2010-11-30 14:18:03 +0100 | [diff] [blame] | 1254 | int err; |
| 1255 | |
| 1256 | memset(buffer, 0, sizeof(buffer)); |
| 1257 | if (count > sizeof(buffer) - 1) |
| 1258 | count = sizeof(buffer) - 1; |
| 1259 | if (copy_from_user(buffer, buf, count)) |
| 1260 | return -EFAULT; |
| 1261 | |
Alexey Dobriyan | 0a8cb8e | 2011-05-26 16:25:50 -0700 | [diff] [blame] | 1262 | err = kstrtoint(strstrip(buffer), 0, &nice); |
| 1263 | if (err < 0) |
| 1264 | return err; |
Mike Galbraith | 5091faa | 2010-11-30 14:18:03 +0100 | [diff] [blame] | 1265 | |
| 1266 | p = get_proc_task(inode); |
| 1267 | if (!p) |
| 1268 | return -ESRCH; |
| 1269 | |
Hiroshi Shimamoto | 2e5b5b3 | 2012-02-23 17:41:27 +0900 | [diff] [blame] | 1270 | err = proc_sched_autogroup_set_nice(p, nice); |
Mike Galbraith | 5091faa | 2010-11-30 14:18:03 +0100 | [diff] [blame] | 1271 | if (err) |
| 1272 | count = err; |
| 1273 | |
| 1274 | put_task_struct(p); |
| 1275 | |
| 1276 | return count; |
| 1277 | } |
| 1278 | |
| 1279 | static int sched_autogroup_open(struct inode *inode, struct file *filp) |
| 1280 | { |
| 1281 | int ret; |
| 1282 | |
| 1283 | ret = single_open(filp, sched_autogroup_show, NULL); |
| 1284 | if (!ret) { |
| 1285 | struct seq_file *m = filp->private_data; |
| 1286 | |
| 1287 | m->private = inode; |
| 1288 | } |
| 1289 | return ret; |
| 1290 | } |
| 1291 | |
| 1292 | static const struct file_operations proc_pid_sched_autogroup_operations = { |
| 1293 | .open = sched_autogroup_open, |
| 1294 | .read = seq_read, |
| 1295 | .write = sched_autogroup_write, |
| 1296 | .llseek = seq_lseek, |
| 1297 | .release = single_release, |
| 1298 | }; |
| 1299 | |
| 1300 | #endif /* CONFIG_SCHED_AUTOGROUP */ |
| 1301 | |
john stultz | 4614a696b | 2009-12-14 18:00:05 -0800 | [diff] [blame] | 1302 | static ssize_t comm_write(struct file *file, const char __user *buf, |
| 1303 | size_t count, loff_t *offset) |
| 1304 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1305 | struct inode *inode = file_inode(file); |
john stultz | 4614a696b | 2009-12-14 18:00:05 -0800 | [diff] [blame] | 1306 | struct task_struct *p; |
| 1307 | char buffer[TASK_COMM_LEN]; |
David Rientjes | 830e0fc | 2013-04-30 15:28:18 -0700 | [diff] [blame] | 1308 | const size_t maxlen = sizeof(buffer) - 1; |
john stultz | 4614a696b | 2009-12-14 18:00:05 -0800 | [diff] [blame] | 1309 | |
| 1310 | memset(buffer, 0, sizeof(buffer)); |
David Rientjes | 830e0fc | 2013-04-30 15:28:18 -0700 | [diff] [blame] | 1311 | if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count)) |
john stultz | 4614a696b | 2009-12-14 18:00:05 -0800 | [diff] [blame] | 1312 | return -EFAULT; |
| 1313 | |
| 1314 | p = get_proc_task(inode); |
| 1315 | if (!p) |
| 1316 | return -ESRCH; |
| 1317 | |
| 1318 | if (same_thread_group(current, p)) |
| 1319 | set_task_comm(p, buffer); |
| 1320 | else |
| 1321 | count = -EINVAL; |
| 1322 | |
| 1323 | put_task_struct(p); |
| 1324 | |
| 1325 | return count; |
| 1326 | } |
| 1327 | |
| 1328 | static int comm_show(struct seq_file *m, void *v) |
| 1329 | { |
| 1330 | struct inode *inode = m->private; |
| 1331 | struct task_struct *p; |
| 1332 | |
| 1333 | p = get_proc_task(inode); |
| 1334 | if (!p) |
| 1335 | return -ESRCH; |
| 1336 | |
| 1337 | task_lock(p); |
| 1338 | seq_printf(m, "%s\n", p->comm); |
| 1339 | task_unlock(p); |
| 1340 | |
| 1341 | put_task_struct(p); |
| 1342 | |
| 1343 | return 0; |
| 1344 | } |
| 1345 | |
| 1346 | static int comm_open(struct inode *inode, struct file *filp) |
| 1347 | { |
Jovi Zhang | c6a3405 | 2011-01-12 17:00:34 -0800 | [diff] [blame] | 1348 | return single_open(filp, comm_show, inode); |
john stultz | 4614a696b | 2009-12-14 18:00:05 -0800 | [diff] [blame] | 1349 | } |
| 1350 | |
| 1351 | static const struct file_operations proc_pid_set_comm_operations = { |
| 1352 | .open = comm_open, |
| 1353 | .read = seq_read, |
| 1354 | .write = comm_write, |
| 1355 | .llseek = seq_lseek, |
| 1356 | .release = single_release, |
| 1357 | }; |
| 1358 | |
Cyrill Gorcunov | 7773fbc | 2012-01-10 15:11:20 -0800 | [diff] [blame] | 1359 | static int proc_exe_link(struct dentry *dentry, struct path *exe_path) |
Matt Helsley | 925d1c4 | 2008-04-29 01:01:36 -0700 | [diff] [blame] | 1360 | { |
| 1361 | struct task_struct *task; |
| 1362 | struct mm_struct *mm; |
| 1363 | struct file *exe_file; |
| 1364 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1365 | task = get_proc_task(d_inode(dentry)); |
Matt Helsley | 925d1c4 | 2008-04-29 01:01:36 -0700 | [diff] [blame] | 1366 | if (!task) |
| 1367 | return -ENOENT; |
| 1368 | mm = get_task_mm(task); |
| 1369 | put_task_struct(task); |
| 1370 | if (!mm) |
| 1371 | return -ENOENT; |
| 1372 | exe_file = get_mm_exe_file(mm); |
| 1373 | mmput(mm); |
| 1374 | if (exe_file) { |
| 1375 | *exe_path = exe_file->f_path; |
| 1376 | path_get(&exe_file->f_path); |
| 1377 | fput(exe_file); |
| 1378 | return 0; |
| 1379 | } else |
| 1380 | return -ENOENT; |
| 1381 | } |
| 1382 | |
Al Viro | 008b150 | 2005-08-20 00:17:39 +0100 | [diff] [blame] | 1383 | static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1384 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1385 | struct inode *inode = d_inode(dentry); |
Christoph Hellwig | 408ef01 | 2012-06-18 10:47:03 -0400 | [diff] [blame] | 1386 | struct path path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1387 | int error = -EACCES; |
| 1388 | |
Eric W. Biederman | 778c114 | 2006-06-26 00:25:58 -0700 | [diff] [blame] | 1389 | /* Are we allowed to snoop on the tasks file descriptors? */ |
| 1390 | if (!proc_fd_access_allowed(inode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1391 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1392 | |
Christoph Hellwig | 408ef01 | 2012-06-18 10:47:03 -0400 | [diff] [blame] | 1393 | error = PROC_I(inode)->op.proc_get_link(dentry, &path); |
| 1394 | if (error) |
| 1395 | goto out; |
| 1396 | |
Christoph Hellwig | b5fb63c1 | 2012-06-18 10:47:04 -0400 | [diff] [blame] | 1397 | nd_jump_link(nd, &path); |
Christoph Hellwig | 408ef01 | 2012-06-18 10:47:03 -0400 | [diff] [blame] | 1398 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1399 | out: |
Al Viro | 008b150 | 2005-08-20 00:17:39 +0100 | [diff] [blame] | 1400 | return ERR_PTR(error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1401 | } |
| 1402 | |
Jan Blunck | 3dcd25f | 2008-02-14 19:38:35 -0800 | [diff] [blame] | 1403 | static int do_proc_readlink(struct path *path, char __user *buffer, int buflen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1404 | { |
Mel Gorman | e12ba74 | 2007-10-16 01:25:52 -0700 | [diff] [blame] | 1405 | char *tmp = (char*)__get_free_page(GFP_TEMPORARY); |
Jan Blunck | 3dcd25f | 2008-02-14 19:38:35 -0800 | [diff] [blame] | 1406 | char *pathname; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1407 | int len; |
| 1408 | |
| 1409 | if (!tmp) |
| 1410 | return -ENOMEM; |
Akinobu Mita | 0c28f28 | 2007-05-08 00:31:41 -0700 | [diff] [blame] | 1411 | |
Eric W. Biederman | 7b2a69b | 2010-12-05 15:51:21 -0800 | [diff] [blame] | 1412 | pathname = d_path(path, tmp, PAGE_SIZE); |
Jan Blunck | 3dcd25f | 2008-02-14 19:38:35 -0800 | [diff] [blame] | 1413 | len = PTR_ERR(pathname); |
| 1414 | if (IS_ERR(pathname)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1415 | goto out; |
Jan Blunck | 3dcd25f | 2008-02-14 19:38:35 -0800 | [diff] [blame] | 1416 | len = tmp + PAGE_SIZE - 1 - pathname; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1417 | |
| 1418 | if (len > buflen) |
| 1419 | len = buflen; |
Jan Blunck | 3dcd25f | 2008-02-14 19:38:35 -0800 | [diff] [blame] | 1420 | if (copy_to_user(buffer, pathname, len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1421 | len = -EFAULT; |
| 1422 | out: |
| 1423 | free_page((unsigned long)tmp); |
| 1424 | return len; |
| 1425 | } |
| 1426 | |
| 1427 | static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen) |
| 1428 | { |
| 1429 | int error = -EACCES; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1430 | struct inode *inode = d_inode(dentry); |
Jan Blunck | 3dcd25f | 2008-02-14 19:38:35 -0800 | [diff] [blame] | 1431 | struct path path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1432 | |
Eric W. Biederman | 778c114 | 2006-06-26 00:25:58 -0700 | [diff] [blame] | 1433 | /* Are we allowed to snoop on the tasks file descriptors? */ |
| 1434 | if (!proc_fd_access_allowed(inode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1435 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1436 | |
Cyrill Gorcunov | 7773fbc | 2012-01-10 15:11:20 -0800 | [diff] [blame] | 1437 | error = PROC_I(inode)->op.proc_get_link(dentry, &path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1438 | if (error) |
| 1439 | goto out; |
| 1440 | |
Jan Blunck | 3dcd25f | 2008-02-14 19:38:35 -0800 | [diff] [blame] | 1441 | error = do_proc_readlink(&path, buffer, buflen); |
| 1442 | path_put(&path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1443 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1444 | return error; |
| 1445 | } |
| 1446 | |
Cyrill Gorcunov | faf60af | 2012-08-23 14:43:24 +0400 | [diff] [blame] | 1447 | const struct inode_operations proc_pid_link_inode_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1448 | .readlink = proc_pid_readlink, |
Linus Torvalds | 6d76fa5 | 2006-07-15 12:26:45 -0700 | [diff] [blame] | 1449 | .follow_link = proc_pid_follow_link, |
| 1450 | .setattr = proc_setattr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1451 | }; |
| 1452 | |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 1453 | |
| 1454 | /* building an inode */ |
| 1455 | |
Eric W. Biederman | 6b4e306 | 2010-03-07 16:41:34 -0800 | [diff] [blame] | 1456 | struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task) |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 1457 | { |
| 1458 | struct inode * inode; |
| 1459 | struct proc_inode *ei; |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1460 | const struct cred *cred; |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 1461 | |
| 1462 | /* We need a new inode */ |
| 1463 | |
| 1464 | inode = new_inode(sb); |
| 1465 | if (!inode) |
| 1466 | goto out; |
| 1467 | |
| 1468 | /* Common stuff */ |
| 1469 | ei = PROC_I(inode); |
Christoph Hellwig | 85fe402 | 2010-10-23 11:19:54 -0400 | [diff] [blame] | 1470 | inode->i_ino = get_next_ino(); |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 1471 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 1472 | inode->i_op = &proc_def_inode_operations; |
| 1473 | |
| 1474 | /* |
| 1475 | * grab the reference to task. |
| 1476 | */ |
Oleg Nesterov | 1a657f78 | 2006-10-02 02:18:59 -0700 | [diff] [blame] | 1477 | ei->pid = get_task_pid(task, PIDTYPE_PID); |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 1478 | if (!ei->pid) |
| 1479 | goto out_unlock; |
| 1480 | |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 1481 | if (task_dumpable(task)) { |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1482 | rcu_read_lock(); |
| 1483 | cred = __task_cred(task); |
| 1484 | inode->i_uid = cred->euid; |
| 1485 | inode->i_gid = cred->egid; |
| 1486 | rcu_read_unlock(); |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 1487 | } |
| 1488 | security_task_to_inode(task, inode); |
| 1489 | |
| 1490 | out: |
| 1491 | return inode; |
| 1492 | |
| 1493 | out_unlock: |
| 1494 | iput(inode); |
| 1495 | return NULL; |
| 1496 | } |
| 1497 | |
Eric W. Biederman | 6b4e306 | 2010-03-07 16:41:34 -0800 | [diff] [blame] | 1498 | int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 1499 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1500 | struct inode *inode = d_inode(dentry); |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 1501 | struct task_struct *task; |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1502 | const struct cred *cred; |
Vasiliy Kulikov | 0499680 | 2012-01-10 15:11:31 -0800 | [diff] [blame] | 1503 | struct pid_namespace *pid = dentry->d_sb->s_fs_info; |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1504 | |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 1505 | generic_fillattr(inode, stat); |
| 1506 | |
| 1507 | rcu_read_lock(); |
Eric W. Biederman | dcb0f22 | 2012-02-09 08:48:21 -0800 | [diff] [blame] | 1508 | stat->uid = GLOBAL_ROOT_UID; |
| 1509 | stat->gid = GLOBAL_ROOT_GID; |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 1510 | task = pid_task(proc_pid(inode), PIDTYPE_PID); |
| 1511 | if (task) { |
Vasiliy Kulikov | 0499680 | 2012-01-10 15:11:31 -0800 | [diff] [blame] | 1512 | if (!has_pid_permissions(pid, task, 2)) { |
| 1513 | rcu_read_unlock(); |
| 1514 | /* |
| 1515 | * This doesn't prevent learning whether PID exists, |
| 1516 | * it only makes getattr() consistent with readdir(). |
| 1517 | */ |
| 1518 | return -ENOENT; |
| 1519 | } |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 1520 | if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) || |
| 1521 | task_dumpable(task)) { |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1522 | cred = __task_cred(task); |
| 1523 | stat->uid = cred->euid; |
| 1524 | stat->gid = cred->egid; |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 1525 | } |
| 1526 | } |
| 1527 | rcu_read_unlock(); |
| 1528 | return 0; |
| 1529 | } |
| 1530 | |
| 1531 | /* dentry stuff */ |
| 1532 | |
| 1533 | /* |
| 1534 | * Exceptional case: normally we are not allowed to unhash a busy |
| 1535 | * directory. In this case, however, we can do it - no aliasing problems |
| 1536 | * due to the way we treat inodes. |
| 1537 | * |
| 1538 | * Rewrite the inode's ownerships here because the owning task may have |
| 1539 | * performed a setuid(), etc. |
| 1540 | * |
| 1541 | * Before the /proc/pid/status file was created the only way to read |
| 1542 | * the effective uid of a /process was to stat /proc/pid. Reading |
| 1543 | * /proc/pid/status is slow enough that procps and other packages |
| 1544 | * kept stating /proc/pid. To keep the rules in /proc simple I have |
| 1545 | * made this apply to all per process world readable and executable |
| 1546 | * directories. |
| 1547 | */ |
Al Viro | 0b728e1 | 2012-06-10 16:03:43 -0400 | [diff] [blame] | 1548 | int pid_revalidate(struct dentry *dentry, unsigned int flags) |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 1549 | { |
Nick Piggin | 34286d6 | 2011-01-07 17:49:57 +1100 | [diff] [blame] | 1550 | struct inode *inode; |
| 1551 | struct task_struct *task; |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1552 | const struct cred *cred; |
| 1553 | |
Al Viro | 0b728e1 | 2012-06-10 16:03:43 -0400 | [diff] [blame] | 1554 | if (flags & LOOKUP_RCU) |
Nick Piggin | 34286d6 | 2011-01-07 17:49:57 +1100 | [diff] [blame] | 1555 | return -ECHILD; |
| 1556 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1557 | inode = d_inode(dentry); |
Nick Piggin | 34286d6 | 2011-01-07 17:49:57 +1100 | [diff] [blame] | 1558 | task = get_proc_task(inode); |
| 1559 | |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 1560 | if (task) { |
| 1561 | if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) || |
| 1562 | task_dumpable(task)) { |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1563 | rcu_read_lock(); |
| 1564 | cred = __task_cred(task); |
| 1565 | inode->i_uid = cred->euid; |
| 1566 | inode->i_gid = cred->egid; |
| 1567 | rcu_read_unlock(); |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 1568 | } else { |
Eric W. Biederman | dcb0f22 | 2012-02-09 08:48:21 -0800 | [diff] [blame] | 1569 | inode->i_uid = GLOBAL_ROOT_UID; |
| 1570 | inode->i_gid = GLOBAL_ROOT_GID; |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 1571 | } |
| 1572 | inode->i_mode &= ~(S_ISUID | S_ISGID); |
| 1573 | security_task_to_inode(task, inode); |
| 1574 | put_task_struct(task); |
| 1575 | return 1; |
| 1576 | } |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 1577 | return 0; |
| 1578 | } |
| 1579 | |
Oleg Nesterov | d855a4b | 2014-01-23 15:55:39 -0800 | [diff] [blame] | 1580 | static inline bool proc_inode_is_dead(struct inode *inode) |
| 1581 | { |
| 1582 | return !proc_pid(inode)->tasks[PIDTYPE_PID].first; |
| 1583 | } |
| 1584 | |
David Howells | 1dd704b | 2013-04-12 01:08:50 +0100 | [diff] [blame] | 1585 | int pid_delete_dentry(const struct dentry *dentry) |
| 1586 | { |
| 1587 | /* Is the task we represent dead? |
| 1588 | * If so, then don't put the dentry on the lru list, |
| 1589 | * kill it immediately. |
| 1590 | */ |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1591 | return proc_inode_is_dead(d_inode(dentry)); |
David Howells | 1dd704b | 2013-04-12 01:08:50 +0100 | [diff] [blame] | 1592 | } |
| 1593 | |
Eric W. Biederman | 6b4e306 | 2010-03-07 16:41:34 -0800 | [diff] [blame] | 1594 | const struct dentry_operations pid_dentry_operations = |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 1595 | { |
| 1596 | .d_revalidate = pid_revalidate, |
| 1597 | .d_delete = pid_delete_dentry, |
| 1598 | }; |
| 1599 | |
| 1600 | /* Lookups */ |
| 1601 | |
Eric W. Biederman | 1c0d04c | 2006-10-02 02:18:57 -0700 | [diff] [blame] | 1602 | /* |
| 1603 | * Fill a directory entry. |
| 1604 | * |
| 1605 | * If possible create the dcache entry and derive our inode number and |
| 1606 | * file type from dcache entry. |
| 1607 | * |
| 1608 | * Since all of the proc inode numbers are dynamically generated, the inode |
| 1609 | * numbers do not exist until the inode is cache. This means creating the |
| 1610 | * the dcache entry in readdir is necessary to keep the inode numbers |
| 1611 | * reported by readdir in sync with the inode numbers reported |
| 1612 | * by stat. |
| 1613 | */ |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 1614 | bool proc_fill_cache(struct file *file, struct dir_context *ctx, |
Eric W. Biederman | 6b4e306 | 2010-03-07 16:41:34 -0800 | [diff] [blame] | 1615 | const char *name, int len, |
Eric Dumazet | c5141e6 | 2007-05-08 00:26:15 -0700 | [diff] [blame] | 1616 | instantiate_t instantiate, struct task_struct *task, const void *ptr) |
Eric W. Biederman | 61a2878 | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 1617 | { |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 1618 | struct dentry *child, *dir = file->f_path.dentry; |
Al Viro | 1df98b8 | 2013-06-15 11:33:10 +0400 | [diff] [blame] | 1619 | struct qstr qname = QSTR_INIT(name, len); |
Eric W. Biederman | 61a2878 | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 1620 | struct inode *inode; |
Al Viro | 1df98b8 | 2013-06-15 11:33:10 +0400 | [diff] [blame] | 1621 | unsigned type; |
| 1622 | ino_t ino; |
Eric W. Biederman | 61a2878 | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 1623 | |
Al Viro | 1df98b8 | 2013-06-15 11:33:10 +0400 | [diff] [blame] | 1624 | child = d_hash_and_lookup(dir, &qname); |
Eric W. Biederman | 61a2878 | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 1625 | if (!child) { |
Al Viro | 1df98b8 | 2013-06-15 11:33:10 +0400 | [diff] [blame] | 1626 | child = d_alloc(dir, &qname); |
| 1627 | if (!child) |
| 1628 | goto end_instantiate; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1629 | if (instantiate(d_inode(dir), child, task, ptr) < 0) { |
Al Viro | 1df98b8 | 2013-06-15 11:33:10 +0400 | [diff] [blame] | 1630 | dput(child); |
| 1631 | goto end_instantiate; |
Eric W. Biederman | 61a2878 | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 1632 | } |
| 1633 | } |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1634 | inode = d_inode(child); |
Al Viro | 147ce69 | 2013-06-15 10:26:35 +0400 | [diff] [blame] | 1635 | ino = inode->i_ino; |
| 1636 | type = inode->i_mode >> 12; |
Eric W. Biederman | 61a2878 | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 1637 | dput(child); |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 1638 | return dir_emit(ctx, name, len, ino, type); |
Al Viro | 1df98b8 | 2013-06-15 11:33:10 +0400 | [diff] [blame] | 1639 | |
| 1640 | end_instantiate: |
| 1641 | return dir_emit(ctx, name, len, 1, DT_UNKNOWN); |
Eric W. Biederman | 61a2878 | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 1642 | } |
| 1643 | |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1644 | #ifdef CONFIG_CHECKPOINT_RESTORE |
| 1645 | |
| 1646 | /* |
| 1647 | * dname_to_vma_addr - maps a dentry name into two unsigned longs |
| 1648 | * which represent vma start and end addresses. |
| 1649 | */ |
| 1650 | static int dname_to_vma_addr(struct dentry *dentry, |
| 1651 | unsigned long *start, unsigned long *end) |
| 1652 | { |
| 1653 | if (sscanf(dentry->d_name.name, "%lx-%lx", start, end) != 2) |
| 1654 | return -EINVAL; |
| 1655 | |
| 1656 | return 0; |
| 1657 | } |
| 1658 | |
Al Viro | 0b728e1 | 2012-06-10 16:03:43 -0400 | [diff] [blame] | 1659 | static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags) |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1660 | { |
| 1661 | unsigned long vm_start, vm_end; |
| 1662 | bool exact_vma_exists = false; |
| 1663 | struct mm_struct *mm = NULL; |
| 1664 | struct task_struct *task; |
| 1665 | const struct cred *cred; |
| 1666 | struct inode *inode; |
| 1667 | int status = 0; |
| 1668 | |
Al Viro | 0b728e1 | 2012-06-10 16:03:43 -0400 | [diff] [blame] | 1669 | if (flags & LOOKUP_RCU) |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1670 | return -ECHILD; |
| 1671 | |
| 1672 | if (!capable(CAP_SYS_ADMIN)) { |
Zhao Hongjiang | 4173581 | 2013-02-20 13:13:55 +1100 | [diff] [blame] | 1673 | status = -EPERM; |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1674 | goto out_notask; |
| 1675 | } |
| 1676 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1677 | inode = d_inode(dentry); |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1678 | task = get_proc_task(inode); |
| 1679 | if (!task) |
| 1680 | goto out_notask; |
| 1681 | |
Cong Wang | 2344bec | 2012-05-31 16:26:18 -0700 | [diff] [blame] | 1682 | mm = mm_access(task, PTRACE_MODE_READ); |
| 1683 | if (IS_ERR_OR_NULL(mm)) |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1684 | goto out; |
| 1685 | |
| 1686 | if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) { |
| 1687 | down_read(&mm->mmap_sem); |
| 1688 | exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end); |
| 1689 | up_read(&mm->mmap_sem); |
| 1690 | } |
| 1691 | |
| 1692 | mmput(mm); |
| 1693 | |
| 1694 | if (exact_vma_exists) { |
| 1695 | if (task_dumpable(task)) { |
| 1696 | rcu_read_lock(); |
| 1697 | cred = __task_cred(task); |
| 1698 | inode->i_uid = cred->euid; |
| 1699 | inode->i_gid = cred->egid; |
| 1700 | rcu_read_unlock(); |
| 1701 | } else { |
Eric W. Biederman | dcb0f22 | 2012-02-09 08:48:21 -0800 | [diff] [blame] | 1702 | inode->i_uid = GLOBAL_ROOT_UID; |
| 1703 | inode->i_gid = GLOBAL_ROOT_GID; |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1704 | } |
| 1705 | security_task_to_inode(task, inode); |
| 1706 | status = 1; |
| 1707 | } |
| 1708 | |
| 1709 | out: |
| 1710 | put_task_struct(task); |
| 1711 | |
| 1712 | out_notask: |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1713 | return status; |
| 1714 | } |
| 1715 | |
| 1716 | static const struct dentry_operations tid_map_files_dentry_operations = { |
| 1717 | .d_revalidate = map_files_d_revalidate, |
| 1718 | .d_delete = pid_delete_dentry, |
| 1719 | }; |
| 1720 | |
| 1721 | static int proc_map_files_get_link(struct dentry *dentry, struct path *path) |
| 1722 | { |
| 1723 | unsigned long vm_start, vm_end; |
| 1724 | struct vm_area_struct *vma; |
| 1725 | struct task_struct *task; |
| 1726 | struct mm_struct *mm; |
| 1727 | int rc; |
| 1728 | |
| 1729 | rc = -ENOENT; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1730 | task = get_proc_task(d_inode(dentry)); |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1731 | if (!task) |
| 1732 | goto out; |
| 1733 | |
| 1734 | mm = get_task_mm(task); |
| 1735 | put_task_struct(task); |
| 1736 | if (!mm) |
| 1737 | goto out; |
| 1738 | |
| 1739 | rc = dname_to_vma_addr(dentry, &vm_start, &vm_end); |
| 1740 | if (rc) |
| 1741 | goto out_mmput; |
| 1742 | |
Artem Fetishev | 70335ab | 2014-03-10 15:49:45 -0700 | [diff] [blame] | 1743 | rc = -ENOENT; |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1744 | down_read(&mm->mmap_sem); |
| 1745 | vma = find_exact_vma(mm, vm_start, vm_end); |
| 1746 | if (vma && vma->vm_file) { |
| 1747 | *path = vma->vm_file->f_path; |
| 1748 | path_get(path); |
| 1749 | rc = 0; |
| 1750 | } |
| 1751 | up_read(&mm->mmap_sem); |
| 1752 | |
| 1753 | out_mmput: |
| 1754 | mmput(mm); |
| 1755 | out: |
| 1756 | return rc; |
| 1757 | } |
| 1758 | |
| 1759 | struct map_files_info { |
Al Viro | 7b540d0 | 2012-08-27 14:55:26 -0400 | [diff] [blame] | 1760 | fmode_t mode; |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1761 | unsigned long len; |
| 1762 | unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */ |
| 1763 | }; |
| 1764 | |
Al Viro | c52a47a | 2013-06-15 11:15:20 +0400 | [diff] [blame] | 1765 | static int |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1766 | proc_map_files_instantiate(struct inode *dir, struct dentry *dentry, |
| 1767 | struct task_struct *task, const void *ptr) |
| 1768 | { |
Al Viro | 7b540d0 | 2012-08-27 14:55:26 -0400 | [diff] [blame] | 1769 | fmode_t mode = (fmode_t)(unsigned long)ptr; |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1770 | struct proc_inode *ei; |
| 1771 | struct inode *inode; |
| 1772 | |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1773 | inode = proc_pid_make_inode(dir->i_sb, task); |
| 1774 | if (!inode) |
Al Viro | c52a47a | 2013-06-15 11:15:20 +0400 | [diff] [blame] | 1775 | return -ENOENT; |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1776 | |
| 1777 | ei = PROC_I(inode); |
| 1778 | ei->op.proc_get_link = proc_map_files_get_link; |
| 1779 | |
| 1780 | inode->i_op = &proc_pid_link_inode_operations; |
| 1781 | inode->i_size = 64; |
| 1782 | inode->i_mode = S_IFLNK; |
| 1783 | |
Al Viro | 7b540d0 | 2012-08-27 14:55:26 -0400 | [diff] [blame] | 1784 | if (mode & FMODE_READ) |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1785 | inode->i_mode |= S_IRUSR; |
Al Viro | 7b540d0 | 2012-08-27 14:55:26 -0400 | [diff] [blame] | 1786 | if (mode & FMODE_WRITE) |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1787 | inode->i_mode |= S_IWUSR; |
| 1788 | |
| 1789 | d_set_d_op(dentry, &tid_map_files_dentry_operations); |
| 1790 | d_add(dentry, inode); |
| 1791 | |
Al Viro | c52a47a | 2013-06-15 11:15:20 +0400 | [diff] [blame] | 1792 | return 0; |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1793 | } |
| 1794 | |
| 1795 | static struct dentry *proc_map_files_lookup(struct inode *dir, |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 1796 | struct dentry *dentry, unsigned int flags) |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1797 | { |
| 1798 | unsigned long vm_start, vm_end; |
| 1799 | struct vm_area_struct *vma; |
| 1800 | struct task_struct *task; |
Al Viro | c52a47a | 2013-06-15 11:15:20 +0400 | [diff] [blame] | 1801 | int result; |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1802 | struct mm_struct *mm; |
| 1803 | |
Al Viro | c52a47a | 2013-06-15 11:15:20 +0400 | [diff] [blame] | 1804 | result = -EPERM; |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1805 | if (!capable(CAP_SYS_ADMIN)) |
| 1806 | goto out; |
| 1807 | |
Al Viro | c52a47a | 2013-06-15 11:15:20 +0400 | [diff] [blame] | 1808 | result = -ENOENT; |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1809 | task = get_proc_task(dir); |
| 1810 | if (!task) |
| 1811 | goto out; |
| 1812 | |
Al Viro | c52a47a | 2013-06-15 11:15:20 +0400 | [diff] [blame] | 1813 | result = -EACCES; |
Cyrill Gorcunov | eb94cd9 | 2012-05-17 17:03:25 -0700 | [diff] [blame] | 1814 | if (!ptrace_may_access(task, PTRACE_MODE_READ)) |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1815 | goto out_put_task; |
| 1816 | |
Al Viro | c52a47a | 2013-06-15 11:15:20 +0400 | [diff] [blame] | 1817 | result = -ENOENT; |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1818 | if (dname_to_vma_addr(dentry, &vm_start, &vm_end)) |
Cyrill Gorcunov | eb94cd9 | 2012-05-17 17:03:25 -0700 | [diff] [blame] | 1819 | goto out_put_task; |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1820 | |
| 1821 | mm = get_task_mm(task); |
| 1822 | if (!mm) |
Cyrill Gorcunov | eb94cd9 | 2012-05-17 17:03:25 -0700 | [diff] [blame] | 1823 | goto out_put_task; |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1824 | |
| 1825 | down_read(&mm->mmap_sem); |
| 1826 | vma = find_exact_vma(mm, vm_start, vm_end); |
| 1827 | if (!vma) |
| 1828 | goto out_no_vma; |
| 1829 | |
Stanislav Kinsbursky | 05f5648 | 2012-11-26 16:29:42 -0800 | [diff] [blame] | 1830 | if (vma->vm_file) |
| 1831 | result = proc_map_files_instantiate(dir, dentry, task, |
| 1832 | (void *)(unsigned long)vma->vm_file->f_mode); |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1833 | |
| 1834 | out_no_vma: |
| 1835 | up_read(&mm->mmap_sem); |
| 1836 | mmput(mm); |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1837 | out_put_task: |
| 1838 | put_task_struct(task); |
| 1839 | out: |
Al Viro | c52a47a | 2013-06-15 11:15:20 +0400 | [diff] [blame] | 1840 | return ERR_PTR(result); |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1841 | } |
| 1842 | |
| 1843 | static const struct inode_operations proc_map_files_inode_operations = { |
| 1844 | .lookup = proc_map_files_lookup, |
| 1845 | .permission = proc_fd_permission, |
| 1846 | .setattr = proc_setattr, |
| 1847 | }; |
| 1848 | |
| 1849 | static int |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 1850 | proc_map_files_readdir(struct file *file, struct dir_context *ctx) |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1851 | { |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1852 | struct vm_area_struct *vma; |
| 1853 | struct task_struct *task; |
| 1854 | struct mm_struct *mm; |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 1855 | unsigned long nr_files, pos, i; |
| 1856 | struct flex_array *fa = NULL; |
| 1857 | struct map_files_info info; |
| 1858 | struct map_files_info *p; |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1859 | int ret; |
| 1860 | |
Zhao Hongjiang | 4173581 | 2013-02-20 13:13:55 +1100 | [diff] [blame] | 1861 | ret = -EPERM; |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1862 | if (!capable(CAP_SYS_ADMIN)) |
| 1863 | goto out; |
| 1864 | |
| 1865 | ret = -ENOENT; |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 1866 | task = get_proc_task(file_inode(file)); |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1867 | if (!task) |
| 1868 | goto out; |
| 1869 | |
| 1870 | ret = -EACCES; |
Cyrill Gorcunov | eb94cd9 | 2012-05-17 17:03:25 -0700 | [diff] [blame] | 1871 | if (!ptrace_may_access(task, PTRACE_MODE_READ)) |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1872 | goto out_put_task; |
| 1873 | |
| 1874 | ret = 0; |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 1875 | if (!dir_emit_dots(file, ctx)) |
| 1876 | goto out_put_task; |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1877 | |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 1878 | mm = get_task_mm(task); |
| 1879 | if (!mm) |
| 1880 | goto out_put_task; |
| 1881 | down_read(&mm->mmap_sem); |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1882 | |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 1883 | nr_files = 0; |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1884 | |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 1885 | /* |
| 1886 | * We need two passes here: |
| 1887 | * |
| 1888 | * 1) Collect vmas of mapped files with mmap_sem taken |
| 1889 | * 2) Release mmap_sem and instantiate entries |
| 1890 | * |
| 1891 | * otherwise we get lockdep complained, since filldir() |
| 1892 | * routine might require mmap_sem taken in might_fault(). |
| 1893 | */ |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1894 | |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 1895 | for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) { |
| 1896 | if (vma->vm_file && ++pos > ctx->pos) |
| 1897 | nr_files++; |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1898 | } |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 1899 | |
| 1900 | if (nr_files) { |
| 1901 | fa = flex_array_alloc(sizeof(info), nr_files, |
| 1902 | GFP_KERNEL); |
| 1903 | if (!fa || flex_array_prealloc(fa, 0, nr_files, |
| 1904 | GFP_KERNEL)) { |
| 1905 | ret = -ENOMEM; |
| 1906 | if (fa) |
| 1907 | flex_array_free(fa); |
| 1908 | up_read(&mm->mmap_sem); |
| 1909 | mmput(mm); |
| 1910 | goto out_put_task; |
| 1911 | } |
| 1912 | for (i = 0, vma = mm->mmap, pos = 2; vma; |
| 1913 | vma = vma->vm_next) { |
| 1914 | if (!vma->vm_file) |
| 1915 | continue; |
| 1916 | if (++pos <= ctx->pos) |
| 1917 | continue; |
| 1918 | |
| 1919 | info.mode = vma->vm_file->f_mode; |
| 1920 | info.len = snprintf(info.name, |
| 1921 | sizeof(info.name), "%lx-%lx", |
| 1922 | vma->vm_start, vma->vm_end); |
| 1923 | if (flex_array_put(fa, i++, &info, GFP_KERNEL)) |
| 1924 | BUG(); |
| 1925 | } |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1926 | } |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 1927 | up_read(&mm->mmap_sem); |
| 1928 | |
| 1929 | for (i = 0; i < nr_files; i++) { |
| 1930 | p = flex_array_get(fa, i); |
| 1931 | if (!proc_fill_cache(file, ctx, |
| 1932 | p->name, p->len, |
| 1933 | proc_map_files_instantiate, |
| 1934 | task, |
| 1935 | (void *)(unsigned long)p->mode)) |
| 1936 | break; |
| 1937 | ctx->pos++; |
| 1938 | } |
| 1939 | if (fa) |
| 1940 | flex_array_free(fa); |
| 1941 | mmput(mm); |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1942 | |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1943 | out_put_task: |
| 1944 | put_task_struct(task); |
| 1945 | out: |
| 1946 | return ret; |
| 1947 | } |
| 1948 | |
| 1949 | static const struct file_operations proc_map_files_operations = { |
| 1950 | .read = generic_read_dir, |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 1951 | .iterate = proc_map_files_readdir, |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1952 | .llseek = default_llseek, |
| 1953 | }; |
| 1954 | |
Pavel Emelyanov | 48f6a7a | 2013-03-11 13:12:45 +0400 | [diff] [blame] | 1955 | struct timers_private { |
| 1956 | struct pid *pid; |
| 1957 | struct task_struct *task; |
| 1958 | struct sighand_struct *sighand; |
Pavel Emelyanov | 57b8015 | 2013-03-11 13:13:08 +0400 | [diff] [blame] | 1959 | struct pid_namespace *ns; |
Pavel Emelyanov | 48f6a7a | 2013-03-11 13:12:45 +0400 | [diff] [blame] | 1960 | unsigned long flags; |
| 1961 | }; |
| 1962 | |
| 1963 | static void *timers_start(struct seq_file *m, loff_t *pos) |
| 1964 | { |
| 1965 | struct timers_private *tp = m->private; |
| 1966 | |
| 1967 | tp->task = get_pid_task(tp->pid, PIDTYPE_PID); |
| 1968 | if (!tp->task) |
| 1969 | return ERR_PTR(-ESRCH); |
| 1970 | |
| 1971 | tp->sighand = lock_task_sighand(tp->task, &tp->flags); |
| 1972 | if (!tp->sighand) |
| 1973 | return ERR_PTR(-ESRCH); |
| 1974 | |
| 1975 | return seq_list_start(&tp->task->signal->posix_timers, *pos); |
| 1976 | } |
| 1977 | |
| 1978 | static void *timers_next(struct seq_file *m, void *v, loff_t *pos) |
| 1979 | { |
| 1980 | struct timers_private *tp = m->private; |
| 1981 | return seq_list_next(v, &tp->task->signal->posix_timers, pos); |
| 1982 | } |
| 1983 | |
| 1984 | static void timers_stop(struct seq_file *m, void *v) |
| 1985 | { |
| 1986 | struct timers_private *tp = m->private; |
| 1987 | |
| 1988 | if (tp->sighand) { |
| 1989 | unlock_task_sighand(tp->task, &tp->flags); |
| 1990 | tp->sighand = NULL; |
| 1991 | } |
| 1992 | |
| 1993 | if (tp->task) { |
| 1994 | put_task_struct(tp->task); |
| 1995 | tp->task = NULL; |
| 1996 | } |
| 1997 | } |
| 1998 | |
| 1999 | static int show_timer(struct seq_file *m, void *v) |
| 2000 | { |
| 2001 | struct k_itimer *timer; |
Pavel Emelyanov | 57b8015 | 2013-03-11 13:13:08 +0400 | [diff] [blame] | 2002 | struct timers_private *tp = m->private; |
| 2003 | int notify; |
Alexey Dobriyan | cedbcca | 2014-08-08 14:21:33 -0700 | [diff] [blame] | 2004 | static const char * const nstr[] = { |
Pavel Emelyanov | 57b8015 | 2013-03-11 13:13:08 +0400 | [diff] [blame] | 2005 | [SIGEV_SIGNAL] = "signal", |
| 2006 | [SIGEV_NONE] = "none", |
| 2007 | [SIGEV_THREAD] = "thread", |
| 2008 | }; |
Pavel Emelyanov | 48f6a7a | 2013-03-11 13:12:45 +0400 | [diff] [blame] | 2009 | |
| 2010 | timer = list_entry((struct list_head *)v, struct k_itimer, list); |
Pavel Emelyanov | 57b8015 | 2013-03-11 13:13:08 +0400 | [diff] [blame] | 2011 | notify = timer->it_sigev_notify; |
| 2012 | |
Pavel Emelyanov | 48f6a7a | 2013-03-11 13:12:45 +0400 | [diff] [blame] | 2013 | seq_printf(m, "ID: %d\n", timer->it_id); |
Joe Perches | 25ce319 | 2015-04-15 16:18:17 -0700 | [diff] [blame] | 2014 | seq_printf(m, "signal: %d/%p\n", |
| 2015 | timer->sigq->info.si_signo, |
| 2016 | timer->sigq->info.si_value.sival_ptr); |
Pavel Emelyanov | 57b8015 | 2013-03-11 13:13:08 +0400 | [diff] [blame] | 2017 | seq_printf(m, "notify: %s/%s.%d\n", |
Joe Perches | 25ce319 | 2015-04-15 16:18:17 -0700 | [diff] [blame] | 2018 | nstr[notify & ~SIGEV_THREAD_ID], |
| 2019 | (notify & SIGEV_THREAD_ID) ? "tid" : "pid", |
| 2020 | pid_nr_ns(timer->it_pid, tp->ns)); |
Pavel Tikhomirov | 15ef029 | 2013-05-17 02:12:03 +0400 | [diff] [blame] | 2021 | seq_printf(m, "ClockID: %d\n", timer->it_clock); |
Pavel Emelyanov | 48f6a7a | 2013-03-11 13:12:45 +0400 | [diff] [blame] | 2022 | |
| 2023 | return 0; |
| 2024 | } |
| 2025 | |
| 2026 | static const struct seq_operations proc_timers_seq_ops = { |
| 2027 | .start = timers_start, |
| 2028 | .next = timers_next, |
| 2029 | .stop = timers_stop, |
| 2030 | .show = show_timer, |
| 2031 | }; |
| 2032 | |
| 2033 | static int proc_timers_open(struct inode *inode, struct file *file) |
| 2034 | { |
| 2035 | struct timers_private *tp; |
| 2036 | |
| 2037 | tp = __seq_open_private(file, &proc_timers_seq_ops, |
| 2038 | sizeof(struct timers_private)); |
| 2039 | if (!tp) |
| 2040 | return -ENOMEM; |
| 2041 | |
| 2042 | tp->pid = proc_pid(inode); |
Pavel Emelyanov | 57b8015 | 2013-03-11 13:13:08 +0400 | [diff] [blame] | 2043 | tp->ns = inode->i_sb->s_fs_info; |
Pavel Emelyanov | 48f6a7a | 2013-03-11 13:12:45 +0400 | [diff] [blame] | 2044 | return 0; |
| 2045 | } |
| 2046 | |
| 2047 | static const struct file_operations proc_timers_operations = { |
| 2048 | .open = proc_timers_open, |
| 2049 | .read = seq_read, |
| 2050 | .llseek = seq_lseek, |
| 2051 | .release = seq_release_private, |
| 2052 | }; |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 2053 | #endif /* CONFIG_CHECKPOINT_RESTORE */ |
| 2054 | |
Al Viro | c52a47a | 2013-06-15 11:15:20 +0400 | [diff] [blame] | 2055 | static int proc_pident_instantiate(struct inode *dir, |
Eric Dumazet | c5141e6 | 2007-05-08 00:26:15 -0700 | [diff] [blame] | 2056 | struct dentry *dentry, struct task_struct *task, const void *ptr) |
Eric W. Biederman | 444ceed | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 2057 | { |
Eric Dumazet | c5141e6 | 2007-05-08 00:26:15 -0700 | [diff] [blame] | 2058 | const struct pid_entry *p = ptr; |
Eric W. Biederman | 444ceed | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 2059 | struct inode *inode; |
| 2060 | struct proc_inode *ei; |
Eric W. Biederman | 444ceed | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 2061 | |
Eric W. Biederman | 61a2878 | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 2062 | inode = proc_pid_make_inode(dir->i_sb, task); |
Eric W. Biederman | 444ceed | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 2063 | if (!inode) |
| 2064 | goto out; |
| 2065 | |
| 2066 | ei = PROC_I(inode); |
| 2067 | inode->i_mode = p->mode; |
| 2068 | if (S_ISDIR(inode->i_mode)) |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 2069 | set_nlink(inode, 2); /* Use getattr to fix if necessary */ |
Eric W. Biederman | 444ceed | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 2070 | if (p->iop) |
| 2071 | inode->i_op = p->iop; |
| 2072 | if (p->fop) |
| 2073 | inode->i_fop = p->fop; |
| 2074 | ei->op = p->op; |
Nick Piggin | fb045ad | 2011-01-07 17:49:55 +1100 | [diff] [blame] | 2075 | d_set_d_op(dentry, &pid_dentry_operations); |
Eric W. Biederman | 444ceed | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 2076 | d_add(dentry, inode); |
| 2077 | /* Close the race of the process dying before we return the dentry */ |
Al Viro | 0b728e1 | 2012-06-10 16:03:43 -0400 | [diff] [blame] | 2078 | if (pid_revalidate(dentry, 0)) |
Al Viro | c52a47a | 2013-06-15 11:15:20 +0400 | [diff] [blame] | 2079 | return 0; |
Eric W. Biederman | 444ceed | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 2080 | out: |
Al Viro | c52a47a | 2013-06-15 11:15:20 +0400 | [diff] [blame] | 2081 | return -ENOENT; |
Eric W. Biederman | 444ceed | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 2082 | } |
| 2083 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2084 | static struct dentry *proc_pident_lookup(struct inode *dir, |
| 2085 | struct dentry *dentry, |
Eric Dumazet | c5141e6 | 2007-05-08 00:26:15 -0700 | [diff] [blame] | 2086 | const struct pid_entry *ents, |
Eric W. Biederman | 7bcd6b0 | 2006-10-02 02:18:56 -0700 | [diff] [blame] | 2087 | unsigned int nents) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2088 | { |
Al Viro | c52a47a | 2013-06-15 11:15:20 +0400 | [diff] [blame] | 2089 | int error; |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 2090 | struct task_struct *task = get_proc_task(dir); |
Eric Dumazet | c5141e6 | 2007-05-08 00:26:15 -0700 | [diff] [blame] | 2091 | const struct pid_entry *p, *last; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2092 | |
Al Viro | c52a47a | 2013-06-15 11:15:20 +0400 | [diff] [blame] | 2093 | error = -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2094 | |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 2095 | if (!task) |
| 2096 | goto out_no_task; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2097 | |
Eric W. Biederman | 20cdc89 | 2006-10-02 02:17:07 -0700 | [diff] [blame] | 2098 | /* |
| 2099 | * Yes, it does not scale. And it should not. Don't add |
| 2100 | * new entries into /proc/<tgid>/ without very good reasons. |
| 2101 | */ |
Eric W. Biederman | 7bcd6b0 | 2006-10-02 02:18:56 -0700 | [diff] [blame] | 2102 | last = &ents[nents - 1]; |
| 2103 | for (p = ents; p <= last; p++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2104 | if (p->len != dentry->d_name.len) |
| 2105 | continue; |
| 2106 | if (!memcmp(dentry->d_name.name, p->name, p->len)) |
| 2107 | break; |
| 2108 | } |
Eric W. Biederman | 7bcd6b0 | 2006-10-02 02:18:56 -0700 | [diff] [blame] | 2109 | if (p > last) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2110 | goto out; |
| 2111 | |
Eric W. Biederman | 444ceed | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 2112 | error = proc_pident_instantiate(dir, dentry, task, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2113 | out: |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 2114 | put_task_struct(task); |
| 2115 | out_no_task: |
Al Viro | c52a47a | 2013-06-15 11:15:20 +0400 | [diff] [blame] | 2116 | return ERR_PTR(error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2117 | } |
| 2118 | |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 2119 | static int proc_pident_readdir(struct file *file, struct dir_context *ctx, |
Eric Dumazet | c5141e6 | 2007-05-08 00:26:15 -0700 | [diff] [blame] | 2120 | const struct pid_entry *ents, unsigned int nents) |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2121 | { |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 2122 | struct task_struct *task = get_proc_task(file_inode(file)); |
| 2123 | const struct pid_entry *p; |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2124 | |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2125 | if (!task) |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 2126 | return -ENOENT; |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2127 | |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 2128 | if (!dir_emit_dots(file, ctx)) |
| 2129 | goto out; |
| 2130 | |
| 2131 | if (ctx->pos >= nents + 2) |
| 2132 | goto out; |
| 2133 | |
| 2134 | for (p = ents + (ctx->pos - 2); p <= ents + nents - 1; p++) { |
| 2135 | if (!proc_fill_cache(file, ctx, p->name, p->len, |
| 2136 | proc_pident_instantiate, task, p)) |
| 2137 | break; |
| 2138 | ctx->pos++; |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2139 | } |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2140 | out: |
Eric W. Biederman | 61a2878 | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 2141 | put_task_struct(task); |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 2142 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2143 | } |
| 2144 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2145 | #ifdef CONFIG_SECURITY |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2146 | static ssize_t proc_pid_attr_read(struct file * file, char __user * buf, |
| 2147 | size_t count, loff_t *ppos) |
| 2148 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 2149 | struct inode * inode = file_inode(file); |
Al Viro | 04ff970 | 2007-03-12 16:17:58 +0000 | [diff] [blame] | 2150 | char *p = NULL; |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2151 | ssize_t length; |
| 2152 | struct task_struct *task = get_proc_task(inode); |
| 2153 | |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2154 | if (!task) |
Al Viro | 04ff970 | 2007-03-12 16:17:58 +0000 | [diff] [blame] | 2155 | return -ESRCH; |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2156 | |
| 2157 | length = security_getprocattr(task, |
Josef "Jeff" Sipek | 2fddfee | 2006-12-08 02:36:36 -0800 | [diff] [blame] | 2158 | (char*)file->f_path.dentry->d_name.name, |
Al Viro | 04ff970 | 2007-03-12 16:17:58 +0000 | [diff] [blame] | 2159 | &p); |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2160 | put_task_struct(task); |
Al Viro | 04ff970 | 2007-03-12 16:17:58 +0000 | [diff] [blame] | 2161 | if (length > 0) |
| 2162 | length = simple_read_from_buffer(buf, count, ppos, p, length); |
| 2163 | kfree(p); |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2164 | return length; |
| 2165 | } |
| 2166 | |
| 2167 | static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf, |
| 2168 | size_t count, loff_t *ppos) |
| 2169 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 2170 | struct inode * inode = file_inode(file); |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2171 | char *page; |
| 2172 | ssize_t length; |
| 2173 | struct task_struct *task = get_proc_task(inode); |
| 2174 | |
| 2175 | length = -ESRCH; |
| 2176 | if (!task) |
| 2177 | goto out_no_task; |
| 2178 | if (count > PAGE_SIZE) |
| 2179 | count = PAGE_SIZE; |
| 2180 | |
| 2181 | /* No partial writes. */ |
| 2182 | length = -EINVAL; |
| 2183 | if (*ppos != 0) |
| 2184 | goto out; |
| 2185 | |
| 2186 | length = -ENOMEM; |
Mel Gorman | e12ba74 | 2007-10-16 01:25:52 -0700 | [diff] [blame] | 2187 | page = (char*)__get_free_page(GFP_TEMPORARY); |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2188 | if (!page) |
| 2189 | goto out; |
| 2190 | |
| 2191 | length = -EFAULT; |
| 2192 | if (copy_from_user(page, buf, count)) |
| 2193 | goto out_free; |
| 2194 | |
David Howells | 107db7c | 2009-05-08 13:55:27 +0100 | [diff] [blame] | 2195 | /* Guard against adverse ptrace interaction */ |
KOSAKI Motohiro | 9b1bf12 | 2010-10-27 15:34:08 -0700 | [diff] [blame] | 2196 | length = mutex_lock_interruptible(&task->signal->cred_guard_mutex); |
David Howells | 107db7c | 2009-05-08 13:55:27 +0100 | [diff] [blame] | 2197 | if (length < 0) |
| 2198 | goto out_free; |
| 2199 | |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2200 | length = security_setprocattr(task, |
Josef "Jeff" Sipek | 2fddfee | 2006-12-08 02:36:36 -0800 | [diff] [blame] | 2201 | (char*)file->f_path.dentry->d_name.name, |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2202 | (void*)page, count); |
KOSAKI Motohiro | 9b1bf12 | 2010-10-27 15:34:08 -0700 | [diff] [blame] | 2203 | mutex_unlock(&task->signal->cred_guard_mutex); |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2204 | out_free: |
| 2205 | free_page((unsigned long) page); |
| 2206 | out: |
| 2207 | put_task_struct(task); |
| 2208 | out_no_task: |
| 2209 | return length; |
| 2210 | } |
| 2211 | |
Arjan van de Ven | 00977a5 | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 2212 | static const struct file_operations proc_pid_attr_operations = { |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2213 | .read = proc_pid_attr_read, |
| 2214 | .write = proc_pid_attr_write, |
Arnd Bergmann | 87df842 | 2010-03-17 23:06:02 +0100 | [diff] [blame] | 2215 | .llseek = generic_file_llseek, |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2216 | }; |
| 2217 | |
Eric Dumazet | c5141e6 | 2007-05-08 00:26:15 -0700 | [diff] [blame] | 2218 | static const struct pid_entry attr_dir_stuff[] = { |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 2219 | REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations), |
| 2220 | REG("prev", S_IRUGO, proc_pid_attr_operations), |
| 2221 | REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations), |
| 2222 | REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations), |
| 2223 | REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations), |
| 2224 | REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations), |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2225 | }; |
| 2226 | |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 2227 | static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2228 | { |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 2229 | return proc_pident_readdir(file, ctx, |
| 2230 | attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2231 | } |
| 2232 | |
Arjan van de Ven | 00977a5 | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 2233 | static const struct file_operations proc_attr_dir_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2234 | .read = generic_read_dir, |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 2235 | .iterate = proc_attr_dir_readdir, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 2236 | .llseek = default_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2237 | }; |
| 2238 | |
Eric W. Biederman | 72d9dcf | 2006-10-02 02:18:50 -0700 | [diff] [blame] | 2239 | static struct dentry *proc_attr_dir_lookup(struct inode *dir, |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 2240 | struct dentry *dentry, unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2241 | { |
Eric W. Biederman | 7bcd6b0 | 2006-10-02 02:18:56 -0700 | [diff] [blame] | 2242 | return proc_pident_lookup(dir, dentry, |
| 2243 | attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2244 | } |
| 2245 | |
Arjan van de Ven | c5ef1c4 | 2007-02-12 00:55:40 -0800 | [diff] [blame] | 2246 | static const struct inode_operations proc_attr_dir_inode_operations = { |
Eric W. Biederman | 72d9dcf | 2006-10-02 02:18:50 -0700 | [diff] [blame] | 2247 | .lookup = proc_attr_dir_lookup, |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 2248 | .getattr = pid_getattr, |
Linus Torvalds | 6d76fa5 | 2006-07-15 12:26:45 -0700 | [diff] [blame] | 2249 | .setattr = proc_setattr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2250 | }; |
| 2251 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2252 | #endif |
| 2253 | |
Christoph Hellwig | 698ba7b | 2009-12-15 16:47:37 -0800 | [diff] [blame] | 2254 | #ifdef CONFIG_ELF_CORE |
Kawai, Hidehiro | 3cb4a0b | 2007-07-19 01:48:28 -0700 | [diff] [blame] | 2255 | static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf, |
| 2256 | size_t count, loff_t *ppos) |
| 2257 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 2258 | struct task_struct *task = get_proc_task(file_inode(file)); |
Kawai, Hidehiro | 3cb4a0b | 2007-07-19 01:48:28 -0700 | [diff] [blame] | 2259 | struct mm_struct *mm; |
| 2260 | char buffer[PROC_NUMBUF]; |
| 2261 | size_t len; |
| 2262 | int ret; |
| 2263 | |
| 2264 | if (!task) |
| 2265 | return -ESRCH; |
| 2266 | |
| 2267 | ret = 0; |
| 2268 | mm = get_task_mm(task); |
| 2269 | if (mm) { |
| 2270 | len = snprintf(buffer, sizeof(buffer), "%08lx\n", |
| 2271 | ((mm->flags & MMF_DUMP_FILTER_MASK) >> |
| 2272 | MMF_DUMP_FILTER_SHIFT)); |
| 2273 | mmput(mm); |
| 2274 | ret = simple_read_from_buffer(buf, count, ppos, buffer, len); |
| 2275 | } |
| 2276 | |
| 2277 | put_task_struct(task); |
| 2278 | |
| 2279 | return ret; |
| 2280 | } |
| 2281 | |
| 2282 | static ssize_t proc_coredump_filter_write(struct file *file, |
| 2283 | const char __user *buf, |
| 2284 | size_t count, |
| 2285 | loff_t *ppos) |
| 2286 | { |
| 2287 | struct task_struct *task; |
| 2288 | struct mm_struct *mm; |
| 2289 | char buffer[PROC_NUMBUF], *end; |
| 2290 | unsigned int val; |
| 2291 | int ret; |
| 2292 | int i; |
| 2293 | unsigned long mask; |
| 2294 | |
| 2295 | ret = -EFAULT; |
| 2296 | memset(buffer, 0, sizeof(buffer)); |
| 2297 | if (count > sizeof(buffer) - 1) |
| 2298 | count = sizeof(buffer) - 1; |
| 2299 | if (copy_from_user(buffer, buf, count)) |
| 2300 | goto out_no_task; |
| 2301 | |
| 2302 | ret = -EINVAL; |
| 2303 | val = (unsigned int)simple_strtoul(buffer, &end, 0); |
| 2304 | if (*end == '\n') |
| 2305 | end++; |
| 2306 | if (end - buffer == 0) |
| 2307 | goto out_no_task; |
| 2308 | |
| 2309 | ret = -ESRCH; |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 2310 | task = get_proc_task(file_inode(file)); |
Kawai, Hidehiro | 3cb4a0b | 2007-07-19 01:48:28 -0700 | [diff] [blame] | 2311 | if (!task) |
| 2312 | goto out_no_task; |
| 2313 | |
| 2314 | ret = end - buffer; |
| 2315 | mm = get_task_mm(task); |
| 2316 | if (!mm) |
| 2317 | goto out_no_mm; |
| 2318 | |
| 2319 | for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) { |
| 2320 | if (val & mask) |
| 2321 | set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags); |
| 2322 | else |
| 2323 | clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags); |
| 2324 | } |
| 2325 | |
| 2326 | mmput(mm); |
| 2327 | out_no_mm: |
| 2328 | put_task_struct(task); |
| 2329 | out_no_task: |
| 2330 | return ret; |
| 2331 | } |
| 2332 | |
| 2333 | static const struct file_operations proc_coredump_filter_operations = { |
| 2334 | .read = proc_coredump_filter_read, |
| 2335 | .write = proc_coredump_filter_write, |
Arnd Bergmann | 87df842 | 2010-03-17 23:06:02 +0100 | [diff] [blame] | 2336 | .llseek = generic_file_llseek, |
Kawai, Hidehiro | 3cb4a0b | 2007-07-19 01:48:28 -0700 | [diff] [blame] | 2337 | }; |
| 2338 | #endif |
| 2339 | |
Andrew Morton | aba76fd | 2006-12-10 02:19:48 -0800 | [diff] [blame] | 2340 | #ifdef CONFIG_TASK_IO_ACCOUNTING |
Alexey Dobriyan | 19aadc9 | 2014-08-08 14:21:50 -0700 | [diff] [blame] | 2341 | static int do_io_accounting(struct task_struct *task, struct seq_file *m, int whole) |
Andrew Morton | aba76fd | 2006-12-10 02:19:48 -0800 | [diff] [blame] | 2342 | { |
Andrea Righi | 940389b | 2008-07-28 00:48:12 +0200 | [diff] [blame] | 2343 | struct task_io_accounting acct = task->ioac; |
Andrea Righi | 5995477 | 2008-07-27 17:29:15 +0200 | [diff] [blame] | 2344 | unsigned long flags; |
Vasiliy Kulikov | 293eb1e | 2011-07-26 16:08:38 -0700 | [diff] [blame] | 2345 | int result; |
Andrea Righi | 297c5d9 | 2008-07-25 01:48:49 -0700 | [diff] [blame] | 2346 | |
Vasiliy Kulikov | 293eb1e | 2011-07-26 16:08:38 -0700 | [diff] [blame] | 2347 | result = mutex_lock_killable(&task->signal->cred_guard_mutex); |
| 2348 | if (result) |
| 2349 | return result; |
| 2350 | |
| 2351 | if (!ptrace_may_access(task, PTRACE_MODE_READ)) { |
| 2352 | result = -EACCES; |
| 2353 | goto out_unlock; |
| 2354 | } |
Vasiliy Kulikov | 1d1221f | 2011-06-24 16:08:38 +0400 | [diff] [blame] | 2355 | |
Andrea Righi | 5995477 | 2008-07-27 17:29:15 +0200 | [diff] [blame] | 2356 | if (whole && lock_task_sighand(task, &flags)) { |
| 2357 | struct task_struct *t = task; |
Andrea Righi | b2d002d | 2008-07-26 15:22:27 -0700 | [diff] [blame] | 2358 | |
Andrea Righi | 5995477 | 2008-07-27 17:29:15 +0200 | [diff] [blame] | 2359 | task_io_accounting_add(&acct, &task->signal->ioac); |
| 2360 | while_each_thread(task, t) |
| 2361 | task_io_accounting_add(&acct, &t->ioac); |
Andrea Righi | 297c5d9 | 2008-07-25 01:48:49 -0700 | [diff] [blame] | 2362 | |
Andrea Righi | 5995477 | 2008-07-27 17:29:15 +0200 | [diff] [blame] | 2363 | unlock_task_sighand(task, &flags); |
Andrea Righi | 297c5d9 | 2008-07-25 01:48:49 -0700 | [diff] [blame] | 2364 | } |
Joe Perches | 25ce319 | 2015-04-15 16:18:17 -0700 | [diff] [blame] | 2365 | seq_printf(m, |
| 2366 | "rchar: %llu\n" |
| 2367 | "wchar: %llu\n" |
| 2368 | "syscr: %llu\n" |
| 2369 | "syscw: %llu\n" |
| 2370 | "read_bytes: %llu\n" |
| 2371 | "write_bytes: %llu\n" |
| 2372 | "cancelled_write_bytes: %llu\n", |
| 2373 | (unsigned long long)acct.rchar, |
| 2374 | (unsigned long long)acct.wchar, |
| 2375 | (unsigned long long)acct.syscr, |
| 2376 | (unsigned long long)acct.syscw, |
| 2377 | (unsigned long long)acct.read_bytes, |
| 2378 | (unsigned long long)acct.write_bytes, |
| 2379 | (unsigned long long)acct.cancelled_write_bytes); |
| 2380 | result = 0; |
| 2381 | |
Vasiliy Kulikov | 293eb1e | 2011-07-26 16:08:38 -0700 | [diff] [blame] | 2382 | out_unlock: |
| 2383 | mutex_unlock(&task->signal->cred_guard_mutex); |
| 2384 | return result; |
Andrew Morton | aba76fd | 2006-12-10 02:19:48 -0800 | [diff] [blame] | 2385 | } |
Andrea Righi | 297c5d9 | 2008-07-25 01:48:49 -0700 | [diff] [blame] | 2386 | |
Alexey Dobriyan | 19aadc9 | 2014-08-08 14:21:50 -0700 | [diff] [blame] | 2387 | static int proc_tid_io_accounting(struct seq_file *m, struct pid_namespace *ns, |
| 2388 | struct pid *pid, struct task_struct *task) |
Andrea Righi | 297c5d9 | 2008-07-25 01:48:49 -0700 | [diff] [blame] | 2389 | { |
Alexey Dobriyan | 19aadc9 | 2014-08-08 14:21:50 -0700 | [diff] [blame] | 2390 | return do_io_accounting(task, m, 0); |
Andrea Righi | 297c5d9 | 2008-07-25 01:48:49 -0700 | [diff] [blame] | 2391 | } |
| 2392 | |
Alexey Dobriyan | 19aadc9 | 2014-08-08 14:21:50 -0700 | [diff] [blame] | 2393 | static int proc_tgid_io_accounting(struct seq_file *m, struct pid_namespace *ns, |
| 2394 | struct pid *pid, struct task_struct *task) |
Andrea Righi | 297c5d9 | 2008-07-25 01:48:49 -0700 | [diff] [blame] | 2395 | { |
Alexey Dobriyan | 19aadc9 | 2014-08-08 14:21:50 -0700 | [diff] [blame] | 2396 | return do_io_accounting(task, m, 1); |
Andrea Righi | 297c5d9 | 2008-07-25 01:48:49 -0700 | [diff] [blame] | 2397 | } |
| 2398 | #endif /* CONFIG_TASK_IO_ACCOUNTING */ |
Andrew Morton | aba76fd | 2006-12-10 02:19:48 -0800 | [diff] [blame] | 2399 | |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 2400 | #ifdef CONFIG_USER_NS |
| 2401 | static int proc_id_map_open(struct inode *inode, struct file *file, |
Fabian Frederick | ccf94f1 | 2014-08-08 14:21:22 -0700 | [diff] [blame] | 2402 | const struct seq_operations *seq_ops) |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 2403 | { |
| 2404 | struct user_namespace *ns = NULL; |
| 2405 | struct task_struct *task; |
| 2406 | struct seq_file *seq; |
| 2407 | int ret = -EINVAL; |
| 2408 | |
| 2409 | task = get_proc_task(inode); |
| 2410 | if (task) { |
| 2411 | rcu_read_lock(); |
| 2412 | ns = get_user_ns(task_cred_xxx(task, user_ns)); |
| 2413 | rcu_read_unlock(); |
| 2414 | put_task_struct(task); |
| 2415 | } |
| 2416 | if (!ns) |
| 2417 | goto err; |
| 2418 | |
| 2419 | ret = seq_open(file, seq_ops); |
| 2420 | if (ret) |
| 2421 | goto err_put_ns; |
| 2422 | |
| 2423 | seq = file->private_data; |
| 2424 | seq->private = ns; |
| 2425 | |
| 2426 | return 0; |
| 2427 | err_put_ns: |
| 2428 | put_user_ns(ns); |
| 2429 | err: |
| 2430 | return ret; |
| 2431 | } |
| 2432 | |
| 2433 | static int proc_id_map_release(struct inode *inode, struct file *file) |
| 2434 | { |
| 2435 | struct seq_file *seq = file->private_data; |
| 2436 | struct user_namespace *ns = seq->private; |
| 2437 | put_user_ns(ns); |
| 2438 | return seq_release(inode, file); |
| 2439 | } |
| 2440 | |
| 2441 | static int proc_uid_map_open(struct inode *inode, struct file *file) |
| 2442 | { |
| 2443 | return proc_id_map_open(inode, file, &proc_uid_seq_operations); |
| 2444 | } |
| 2445 | |
| 2446 | static int proc_gid_map_open(struct inode *inode, struct file *file) |
| 2447 | { |
| 2448 | return proc_id_map_open(inode, file, &proc_gid_seq_operations); |
| 2449 | } |
| 2450 | |
Eric W. Biederman | f76d207 | 2012-08-30 01:24:05 -0700 | [diff] [blame] | 2451 | static int proc_projid_map_open(struct inode *inode, struct file *file) |
| 2452 | { |
| 2453 | return proc_id_map_open(inode, file, &proc_projid_seq_operations); |
| 2454 | } |
| 2455 | |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 2456 | static const struct file_operations proc_uid_map_operations = { |
| 2457 | .open = proc_uid_map_open, |
| 2458 | .write = proc_uid_map_write, |
| 2459 | .read = seq_read, |
| 2460 | .llseek = seq_lseek, |
| 2461 | .release = proc_id_map_release, |
| 2462 | }; |
| 2463 | |
| 2464 | static const struct file_operations proc_gid_map_operations = { |
| 2465 | .open = proc_gid_map_open, |
| 2466 | .write = proc_gid_map_write, |
| 2467 | .read = seq_read, |
| 2468 | .llseek = seq_lseek, |
| 2469 | .release = proc_id_map_release, |
| 2470 | }; |
Eric W. Biederman | f76d207 | 2012-08-30 01:24:05 -0700 | [diff] [blame] | 2471 | |
| 2472 | static const struct file_operations proc_projid_map_operations = { |
| 2473 | .open = proc_projid_map_open, |
| 2474 | .write = proc_projid_map_write, |
| 2475 | .read = seq_read, |
| 2476 | .llseek = seq_lseek, |
| 2477 | .release = proc_id_map_release, |
| 2478 | }; |
Eric W. Biederman | 9cc4651 | 2014-12-02 12:27:26 -0600 | [diff] [blame] | 2479 | |
| 2480 | static int proc_setgroups_open(struct inode *inode, struct file *file) |
| 2481 | { |
| 2482 | struct user_namespace *ns = NULL; |
| 2483 | struct task_struct *task; |
| 2484 | int ret; |
| 2485 | |
| 2486 | ret = -ESRCH; |
| 2487 | task = get_proc_task(inode); |
| 2488 | if (task) { |
| 2489 | rcu_read_lock(); |
| 2490 | ns = get_user_ns(task_cred_xxx(task, user_ns)); |
| 2491 | rcu_read_unlock(); |
| 2492 | put_task_struct(task); |
| 2493 | } |
| 2494 | if (!ns) |
| 2495 | goto err; |
| 2496 | |
| 2497 | if (file->f_mode & FMODE_WRITE) { |
| 2498 | ret = -EACCES; |
| 2499 | if (!ns_capable(ns, CAP_SYS_ADMIN)) |
| 2500 | goto err_put_ns; |
| 2501 | } |
| 2502 | |
| 2503 | ret = single_open(file, &proc_setgroups_show, ns); |
| 2504 | if (ret) |
| 2505 | goto err_put_ns; |
| 2506 | |
| 2507 | return 0; |
| 2508 | err_put_ns: |
| 2509 | put_user_ns(ns); |
| 2510 | err: |
| 2511 | return ret; |
| 2512 | } |
| 2513 | |
| 2514 | static int proc_setgroups_release(struct inode *inode, struct file *file) |
| 2515 | { |
| 2516 | struct seq_file *seq = file->private_data; |
| 2517 | struct user_namespace *ns = seq->private; |
| 2518 | int ret = single_release(inode, file); |
| 2519 | put_user_ns(ns); |
| 2520 | return ret; |
| 2521 | } |
| 2522 | |
| 2523 | static const struct file_operations proc_setgroups_operations = { |
| 2524 | .open = proc_setgroups_open, |
| 2525 | .write = proc_setgroups_write, |
| 2526 | .read = seq_read, |
| 2527 | .llseek = seq_lseek, |
| 2528 | .release = proc_setgroups_release, |
| 2529 | }; |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 2530 | #endif /* CONFIG_USER_NS */ |
| 2531 | |
Kees Cook | 4783072 | 2008-10-06 03:11:58 +0400 | [diff] [blame] | 2532 | static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns, |
| 2533 | struct pid *pid, struct task_struct *task) |
| 2534 | { |
Al Viro | a9712bc | 2011-03-23 15:52:50 -0400 | [diff] [blame] | 2535 | int err = lock_trace(task); |
| 2536 | if (!err) { |
| 2537 | seq_printf(m, "%08x\n", task->personality); |
| 2538 | unlock_trace(task); |
| 2539 | } |
| 2540 | return err; |
Kees Cook | 4783072 | 2008-10-06 03:11:58 +0400 | [diff] [blame] | 2541 | } |
| 2542 | |
Eric W. Biederman | 801199c | 2006-10-02 02:18:48 -0700 | [diff] [blame] | 2543 | /* |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2544 | * Thread groups |
| 2545 | */ |
Arjan van de Ven | 00977a5 | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 2546 | static const struct file_operations proc_task_operations; |
Arjan van de Ven | c5ef1c4 | 2007-02-12 00:55:40 -0800 | [diff] [blame] | 2547 | static const struct inode_operations proc_task_inode_operations; |
Eric W. Biederman | 20cdc89 | 2006-10-02 02:17:07 -0700 | [diff] [blame] | 2548 | |
Eric Dumazet | c5141e6 | 2007-05-08 00:26:15 -0700 | [diff] [blame] | 2549 | static const struct pid_entry tgid_base_stuff[] = { |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 2550 | DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations), |
| 2551 | DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations), |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 2552 | #ifdef CONFIG_CHECKPOINT_RESTORE |
| 2553 | DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations), |
| 2554 | #endif |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 2555 | DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations), |
Eric W. Biederman | 6b4e306 | 2010-03-07 16:41:34 -0800 | [diff] [blame] | 2556 | DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations), |
Andrew Morton | b2211a3 | 2008-03-11 18:03:35 -0700 | [diff] [blame] | 2557 | #ifdef CONFIG_NET |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 2558 | DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations), |
Andrew Morton | b2211a3 | 2008-03-11 18:03:35 -0700 | [diff] [blame] | 2559 | #endif |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 2560 | REG("environ", S_IRUSR, proc_environ_operations), |
Alexey Dobriyan | f9ea536 | 2014-08-08 14:21:35 -0700 | [diff] [blame] | 2561 | ONE("auxv", S_IRUSR, proc_pid_auxv), |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 2562 | ONE("status", S_IRUGO, proc_pid_status), |
Djalal Harouni | 35a3504 | 2014-04-07 15:38:36 -0700 | [diff] [blame] | 2563 | ONE("personality", S_IRUSR, proc_pid_personality), |
Alexey Dobriyan | 1c963eb | 2014-08-08 14:21:37 -0700 | [diff] [blame] | 2564 | ONE("limits", S_IRUGO, proc_pid_limits), |
Ingo Molnar | 43ae34c | 2007-07-09 18:52:00 +0200 | [diff] [blame] | 2565 | #ifdef CONFIG_SCHED_DEBUG |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 2566 | REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations), |
Ingo Molnar | 43ae34c | 2007-07-09 18:52:00 +0200 | [diff] [blame] | 2567 | #endif |
Mike Galbraith | 5091faa | 2010-11-30 14:18:03 +0100 | [diff] [blame] | 2568 | #ifdef CONFIG_SCHED_AUTOGROUP |
| 2569 | REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations), |
| 2570 | #endif |
john stultz | 4614a696b | 2009-12-14 18:00:05 -0800 | [diff] [blame] | 2571 | REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations), |
Roland McGrath | ebcb673 | 2008-07-25 19:46:00 -0700 | [diff] [blame] | 2572 | #ifdef CONFIG_HAVE_ARCH_TRACEHOOK |
Alexey Dobriyan | 09d93bd | 2014-08-08 14:21:39 -0700 | [diff] [blame] | 2573 | ONE("syscall", S_IRUSR, proc_pid_syscall), |
Roland McGrath | ebcb673 | 2008-07-25 19:46:00 -0700 | [diff] [blame] | 2574 | #endif |
Alexey Dobriyan | 2ca66ff | 2014-08-08 14:21:41 -0700 | [diff] [blame] | 2575 | ONE("cmdline", S_IRUGO, proc_pid_cmdline), |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 2576 | ONE("stat", S_IRUGO, proc_tgid_stat), |
| 2577 | ONE("statm", S_IRUGO, proc_pid_statm), |
Siddhesh Poyarekar | b764375 | 2012-03-21 16:34:04 -0700 | [diff] [blame] | 2578 | REG("maps", S_IRUGO, proc_pid_maps_operations), |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2579 | #ifdef CONFIG_NUMA |
Siddhesh Poyarekar | b764375 | 2012-03-21 16:34:04 -0700 | [diff] [blame] | 2580 | REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations), |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2581 | #endif |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 2582 | REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations), |
| 2583 | LNK("cwd", proc_cwd_link), |
| 2584 | LNK("root", proc_root_link), |
| 2585 | LNK("exe", proc_exe_link), |
| 2586 | REG("mounts", S_IRUGO, proc_mounts_operations), |
| 2587 | REG("mountinfo", S_IRUGO, proc_mountinfo_operations), |
| 2588 | REG("mountstats", S_IRUSR, proc_mountstats_operations), |
Matt Mackall | 1e88328 | 2008-02-04 22:29:07 -0800 | [diff] [blame] | 2589 | #ifdef CONFIG_PROC_PAGE_MONITOR |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 2590 | REG("clear_refs", S_IWUSR, proc_clear_refs_operations), |
Siddhesh Poyarekar | b764375 | 2012-03-21 16:34:04 -0700 | [diff] [blame] | 2591 | REG("smaps", S_IRUGO, proc_pid_smaps_operations), |
Djalal Harouni | 32ed74a | 2014-04-07 15:38:38 -0700 | [diff] [blame] | 2592 | REG("pagemap", S_IRUSR, proc_pagemap_operations), |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2593 | #endif |
| 2594 | #ifdef CONFIG_SECURITY |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 2595 | DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations), |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2596 | #endif |
| 2597 | #ifdef CONFIG_KALLSYMS |
Alexey Dobriyan | edfcd60 | 2014-08-08 14:21:44 -0700 | [diff] [blame] | 2598 | ONE("wchan", S_IRUGO, proc_pid_wchan), |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2599 | #endif |
Ken Chen | 2ec220e | 2008-11-10 11:26:08 +0300 | [diff] [blame] | 2600 | #ifdef CONFIG_STACKTRACE |
Djalal Harouni | 35a3504 | 2014-04-07 15:38:36 -0700 | [diff] [blame] | 2601 | ONE("stack", S_IRUSR, proc_pid_stack), |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2602 | #endif |
| 2603 | #ifdef CONFIG_SCHEDSTATS |
Alexey Dobriyan | f6e826c | 2014-08-08 14:21:46 -0700 | [diff] [blame] | 2604 | ONE("schedstat", S_IRUGO, proc_pid_schedstat), |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2605 | #endif |
Arjan van de Ven | 9745512 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 2606 | #ifdef CONFIG_LATENCYTOP |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 2607 | REG("latency", S_IRUGO, proc_lstats_operations), |
Arjan van de Ven | 9745512 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 2608 | #endif |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 2609 | #ifdef CONFIG_PROC_PID_CPUSET |
Zefan Li | 52de477 | 2014-09-18 16:03:36 +0800 | [diff] [blame] | 2610 | ONE("cpuset", S_IRUGO, proc_cpuset_show), |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2611 | #endif |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 2612 | #ifdef CONFIG_CGROUPS |
Zefan Li | 006f4ac | 2014-09-18 16:03:15 +0800 | [diff] [blame] | 2613 | ONE("cgroup", S_IRUGO, proc_cgroup_show), |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 2614 | #endif |
Alexey Dobriyan | 6ba51e3 | 2014-08-08 14:21:48 -0700 | [diff] [blame] | 2615 | ONE("oom_score", S_IRUGO, proc_oom_score), |
David Rientjes | fa0cbbf | 2012-11-12 17:53:04 -0800 | [diff] [blame] | 2616 | REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations), |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 2617 | REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations), |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2618 | #ifdef CONFIG_AUDITSYSCALL |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 2619 | REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations), |
| 2620 | REG("sessionid", S_IRUGO, proc_sessionid_operations), |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2621 | #endif |
Akinobu Mita | f4f154f | 2006-12-08 02:39:47 -0800 | [diff] [blame] | 2622 | #ifdef CONFIG_FAULT_INJECTION |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 2623 | REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations), |
Akinobu Mita | f4f154f | 2006-12-08 02:39:47 -0800 | [diff] [blame] | 2624 | #endif |
Christoph Hellwig | 698ba7b | 2009-12-15 16:47:37 -0800 | [diff] [blame] | 2625 | #ifdef CONFIG_ELF_CORE |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 2626 | REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations), |
Kawai, Hidehiro | 3cb4a0b | 2007-07-19 01:48:28 -0700 | [diff] [blame] | 2627 | #endif |
Andrew Morton | aba76fd | 2006-12-10 02:19:48 -0800 | [diff] [blame] | 2628 | #ifdef CONFIG_TASK_IO_ACCOUNTING |
Alexey Dobriyan | 19aadc9 | 2014-08-08 14:21:50 -0700 | [diff] [blame] | 2629 | ONE("io", S_IRUSR, proc_tgid_io_accounting), |
Andrew Morton | aba76fd | 2006-12-10 02:19:48 -0800 | [diff] [blame] | 2630 | #endif |
Chris Metcalf | f133ecc | 2011-05-26 12:40:09 -0400 | [diff] [blame] | 2631 | #ifdef CONFIG_HARDWALL |
Alexey Dobriyan | d962c14 | 2014-08-08 14:21:52 -0700 | [diff] [blame] | 2632 | ONE("hardwall", S_IRUGO, proc_pid_hardwall), |
Chris Metcalf | f133ecc | 2011-05-26 12:40:09 -0400 | [diff] [blame] | 2633 | #endif |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 2634 | #ifdef CONFIG_USER_NS |
| 2635 | REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations), |
| 2636 | REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations), |
Eric W. Biederman | f76d207 | 2012-08-30 01:24:05 -0700 | [diff] [blame] | 2637 | REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations), |
Eric W. Biederman | 9cc4651 | 2014-12-02 12:27:26 -0600 | [diff] [blame] | 2638 | REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations), |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 2639 | #endif |
Pavel Emelyanov | 48f6a7a | 2013-03-11 13:12:45 +0400 | [diff] [blame] | 2640 | #ifdef CONFIG_CHECKPOINT_RESTORE |
| 2641 | REG("timers", S_IRUGO, proc_timers_operations), |
| 2642 | #endif |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2643 | }; |
| 2644 | |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 2645 | static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx) |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2646 | { |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 2647 | return proc_pident_readdir(file, ctx, |
| 2648 | tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff)); |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2649 | } |
| 2650 | |
Arjan van de Ven | 00977a5 | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 2651 | static const struct file_operations proc_tgid_base_operations = { |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2652 | .read = generic_read_dir, |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 2653 | .iterate = proc_tgid_base_readdir, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 2654 | .llseek = default_llseek, |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2655 | }; |
| 2656 | |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 2657 | static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags) |
| 2658 | { |
Eric W. Biederman | 7bcd6b0 | 2006-10-02 02:18:56 -0700 | [diff] [blame] | 2659 | return proc_pident_lookup(dir, dentry, |
| 2660 | tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff)); |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2661 | } |
| 2662 | |
Arjan van de Ven | c5ef1c4 | 2007-02-12 00:55:40 -0800 | [diff] [blame] | 2663 | static const struct inode_operations proc_tgid_base_inode_operations = { |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2664 | .lookup = proc_tgid_base_lookup, |
| 2665 | .getattr = pid_getattr, |
| 2666 | .setattr = proc_setattr, |
Vasiliy Kulikov | 0499680 | 2012-01-10 15:11:31 -0800 | [diff] [blame] | 2667 | .permission = proc_pid_permission, |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2668 | }; |
| 2669 | |
Pavel Emelyanov | 60347f6 | 2007-10-18 23:40:03 -0700 | [diff] [blame] | 2670 | static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2671 | { |
Eric W. Biederman | 48e6484 | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 2672 | struct dentry *dentry, *leader, *dir; |
Eric W. Biederman | 8578cea | 2006-06-26 00:25:54 -0700 | [diff] [blame] | 2673 | char buf[PROC_NUMBUF]; |
Eric W. Biederman | 48e6484 | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 2674 | struct qstr name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2675 | |
Eric W. Biederman | 48e6484 | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 2676 | name.name = buf; |
Pavel Emelyanov | 60347f6 | 2007-10-18 23:40:03 -0700 | [diff] [blame] | 2677 | name.len = snprintf(buf, sizeof(buf), "%d", pid); |
Al Viro | 4f522a2 | 2013-02-11 23:20:37 -0500 | [diff] [blame] | 2678 | /* no ->d_hash() rejects on procfs */ |
Pavel Emelyanov | 60347f6 | 2007-10-18 23:40:03 -0700 | [diff] [blame] | 2679 | dentry = d_hash_and_lookup(mnt->mnt_root, &name); |
Eric W. Biederman | 48e6484 | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 2680 | if (dentry) { |
Eric W. Biederman | bbd5192 | 2014-02-13 10:24:23 -0800 | [diff] [blame] | 2681 | d_invalidate(dentry); |
Eric W. Biederman | 48e6484 | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 2682 | dput(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2683 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2684 | |
Oleg Nesterov | c35a7f1 | 2014-12-10 15:54:56 -0800 | [diff] [blame] | 2685 | if (pid == tgid) |
| 2686 | return; |
| 2687 | |
Eric W. Biederman | 48e6484 | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 2688 | name.name = buf; |
Pavel Emelyanov | 60347f6 | 2007-10-18 23:40:03 -0700 | [diff] [blame] | 2689 | name.len = snprintf(buf, sizeof(buf), "%d", tgid); |
| 2690 | leader = d_hash_and_lookup(mnt->mnt_root, &name); |
Eric W. Biederman | 48e6484 | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 2691 | if (!leader) |
| 2692 | goto out; |
| 2693 | |
| 2694 | name.name = "task"; |
| 2695 | name.len = strlen(name.name); |
| 2696 | dir = d_hash_and_lookup(leader, &name); |
| 2697 | if (!dir) |
| 2698 | goto out_put_leader; |
| 2699 | |
| 2700 | name.name = buf; |
Pavel Emelyanov | 60347f6 | 2007-10-18 23:40:03 -0700 | [diff] [blame] | 2701 | name.len = snprintf(buf, sizeof(buf), "%d", pid); |
Eric W. Biederman | 48e6484 | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 2702 | dentry = d_hash_and_lookup(dir, &name); |
| 2703 | if (dentry) { |
Eric W. Biederman | bbd5192 | 2014-02-13 10:24:23 -0800 | [diff] [blame] | 2704 | d_invalidate(dentry); |
Eric W. Biederman | 48e6484 | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 2705 | dput(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2706 | } |
Eric W. Biederman | 48e6484 | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 2707 | |
| 2708 | dput(dir); |
| 2709 | out_put_leader: |
| 2710 | dput(leader); |
| 2711 | out: |
| 2712 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2713 | } |
| 2714 | |
Randy Dunlap | 0895e91 | 2007-10-21 21:00:10 -0700 | [diff] [blame] | 2715 | /** |
| 2716 | * proc_flush_task - Remove dcache entries for @task from the /proc dcache. |
| 2717 | * @task: task that should be flushed. |
| 2718 | * |
| 2719 | * When flushing dentries from proc, one needs to flush them from global |
Pavel Emelyanov | 60347f6 | 2007-10-18 23:40:03 -0700 | [diff] [blame] | 2720 | * proc (proc_mnt) and from all the namespaces' procs this task was seen |
Randy Dunlap | 0895e91 | 2007-10-21 21:00:10 -0700 | [diff] [blame] | 2721 | * in. This call is supposed to do all of this job. |
| 2722 | * |
| 2723 | * Looks in the dcache for |
| 2724 | * /proc/@pid |
| 2725 | * /proc/@tgid/task/@pid |
| 2726 | * if either directory is present flushes it and all of it'ts children |
| 2727 | * from the dcache. |
| 2728 | * |
| 2729 | * It is safe and reasonable to cache /proc entries for a task until |
| 2730 | * that task exits. After that they just clog up the dcache with |
| 2731 | * useless entries, possibly causing useful dcache entries to be |
| 2732 | * flushed instead. This routine is proved to flush those useless |
| 2733 | * dcache entries at process exit time. |
| 2734 | * |
| 2735 | * NOTE: This routine is just an optimization so it does not guarantee |
| 2736 | * that no dcache entries will exist at process exit time it |
| 2737 | * just makes it very unlikely that any will persist. |
Pavel Emelyanov | 60347f6 | 2007-10-18 23:40:03 -0700 | [diff] [blame] | 2738 | */ |
| 2739 | |
| 2740 | void proc_flush_task(struct task_struct *task) |
| 2741 | { |
Eric W. Biederman | 9fcc2d1 | 2007-11-14 17:00:07 -0800 | [diff] [blame] | 2742 | int i; |
Oleg Nesterov | 9b4d1cbe | 2009-09-22 16:45:34 -0700 | [diff] [blame] | 2743 | struct pid *pid, *tgid; |
Pavel Emelyanov | 130f77e | 2007-10-18 23:40:11 -0700 | [diff] [blame] | 2744 | struct upid *upid; |
| 2745 | |
Pavel Emelyanov | 130f77e | 2007-10-18 23:40:11 -0700 | [diff] [blame] | 2746 | pid = task_pid(task); |
Oleg Nesterov | 9b4d1cbe | 2009-09-22 16:45:34 -0700 | [diff] [blame] | 2747 | tgid = task_tgid(task); |
Pavel Emelyanov | 130f77e | 2007-10-18 23:40:11 -0700 | [diff] [blame] | 2748 | |
Eric W. Biederman | 9fcc2d1 | 2007-11-14 17:00:07 -0800 | [diff] [blame] | 2749 | for (i = 0; i <= pid->level; i++) { |
Pavel Emelyanov | 130f77e | 2007-10-18 23:40:11 -0700 | [diff] [blame] | 2750 | upid = &pid->numbers[i]; |
| 2751 | proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr, |
Oleg Nesterov | 9b4d1cbe | 2009-09-22 16:45:34 -0700 | [diff] [blame] | 2752 | tgid->numbers[i].nr); |
Pavel Emelyanov | 130f77e | 2007-10-18 23:40:11 -0700 | [diff] [blame] | 2753 | } |
Pavel Emelyanov | 60347f6 | 2007-10-18 23:40:03 -0700 | [diff] [blame] | 2754 | } |
| 2755 | |
Al Viro | c52a47a | 2013-06-15 11:15:20 +0400 | [diff] [blame] | 2756 | static int proc_pid_instantiate(struct inode *dir, |
| 2757 | struct dentry * dentry, |
| 2758 | struct task_struct *task, const void *ptr) |
Eric W. Biederman | 444ceed | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 2759 | { |
Eric W. Biederman | 444ceed | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 2760 | struct inode *inode; |
| 2761 | |
Eric W. Biederman | 61a2878 | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 2762 | inode = proc_pid_make_inode(dir->i_sb, task); |
Eric W. Biederman | 444ceed | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 2763 | if (!inode) |
| 2764 | goto out; |
| 2765 | |
| 2766 | inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO; |
| 2767 | inode->i_op = &proc_tgid_base_inode_operations; |
| 2768 | inode->i_fop = &proc_tgid_base_operations; |
| 2769 | inode->i_flags|=S_IMMUTABLE; |
Vegard Nossum | aed5417 | 2008-06-05 22:46:53 -0700 | [diff] [blame] | 2770 | |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 2771 | set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff, |
| 2772 | ARRAY_SIZE(tgid_base_stuff))); |
Eric W. Biederman | 444ceed | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 2773 | |
Nick Piggin | fb045ad | 2011-01-07 17:49:55 +1100 | [diff] [blame] | 2774 | d_set_d_op(dentry, &pid_dentry_operations); |
Eric W. Biederman | 444ceed | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 2775 | |
| 2776 | d_add(dentry, inode); |
| 2777 | /* Close the race of the process dying before we return the dentry */ |
Al Viro | 0b728e1 | 2012-06-10 16:03:43 -0400 | [diff] [blame] | 2778 | if (pid_revalidate(dentry, 0)) |
Al Viro | c52a47a | 2013-06-15 11:15:20 +0400 | [diff] [blame] | 2779 | return 0; |
Eric W. Biederman | 444ceed | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 2780 | out: |
Al Viro | c52a47a | 2013-06-15 11:15:20 +0400 | [diff] [blame] | 2781 | return -ENOENT; |
Eric W. Biederman | 444ceed | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 2782 | } |
| 2783 | |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 2784 | struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2785 | { |
Alexey Dobriyan | 335eb53 | 2014-08-08 14:21:27 -0700 | [diff] [blame] | 2786 | int result = -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2787 | struct task_struct *task; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2788 | unsigned tgid; |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 2789 | struct pid_namespace *ns; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2790 | |
Alexey Dobriyan | dbcdb50 | 2014-08-08 14:21:25 -0700 | [diff] [blame] | 2791 | tgid = name_to_int(&dentry->d_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2792 | if (tgid == ~0U) |
| 2793 | goto out; |
| 2794 | |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 2795 | ns = dentry->d_sb->s_fs_info; |
Eric W. Biederman | de75873 | 2006-06-26 00:25:51 -0700 | [diff] [blame] | 2796 | rcu_read_lock(); |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 2797 | task = find_task_by_pid_ns(tgid, ns); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2798 | if (task) |
| 2799 | get_task_struct(task); |
Eric W. Biederman | de75873 | 2006-06-26 00:25:51 -0700 | [diff] [blame] | 2800 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2801 | if (!task) |
| 2802 | goto out; |
| 2803 | |
Eric W. Biederman | 444ceed | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 2804 | result = proc_pid_instantiate(dir, dentry, task, NULL); |
Eric W. Biederman | 48e6484 | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 2805 | put_task_struct(task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2806 | out: |
Al Viro | c52a47a | 2013-06-15 11:15:20 +0400 | [diff] [blame] | 2807 | return ERR_PTR(result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2808 | } |
| 2809 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2810 | /* |
Eric W. Biederman | 0804ef4 | 2006-10-02 02:17:04 -0700 | [diff] [blame] | 2811 | * Find the first task with tgid >= tgid |
Eric W. Biederman | 0bc58a9 | 2006-06-26 00:25:50 -0700 | [diff] [blame] | 2812 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2813 | */ |
Eric W. Biederman | 19fd4bb | 2007-11-28 16:21:26 -0800 | [diff] [blame] | 2814 | struct tgid_iter { |
| 2815 | unsigned int tgid; |
Eric W. Biederman | 0804ef4 | 2006-10-02 02:17:04 -0700 | [diff] [blame] | 2816 | struct task_struct *task; |
Eric W. Biederman | 19fd4bb | 2007-11-28 16:21:26 -0800 | [diff] [blame] | 2817 | }; |
| 2818 | static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter) |
| 2819 | { |
Eric W. Biederman | 0804ef4 | 2006-10-02 02:17:04 -0700 | [diff] [blame] | 2820 | struct pid *pid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2821 | |
Eric W. Biederman | 19fd4bb | 2007-11-28 16:21:26 -0800 | [diff] [blame] | 2822 | if (iter.task) |
| 2823 | put_task_struct(iter.task); |
Eric W. Biederman | 0804ef4 | 2006-10-02 02:17:04 -0700 | [diff] [blame] | 2824 | rcu_read_lock(); |
| 2825 | retry: |
Eric W. Biederman | 19fd4bb | 2007-11-28 16:21:26 -0800 | [diff] [blame] | 2826 | iter.task = NULL; |
| 2827 | pid = find_ge_pid(iter.tgid, ns); |
Eric W. Biederman | 0804ef4 | 2006-10-02 02:17:04 -0700 | [diff] [blame] | 2828 | if (pid) { |
Eric W. Biederman | 19fd4bb | 2007-11-28 16:21:26 -0800 | [diff] [blame] | 2829 | iter.tgid = pid_nr_ns(pid, ns); |
| 2830 | iter.task = pid_task(pid, PIDTYPE_PID); |
Eric W. Biederman | 0804ef4 | 2006-10-02 02:17:04 -0700 | [diff] [blame] | 2831 | /* What we to know is if the pid we have find is the |
| 2832 | * pid of a thread_group_leader. Testing for task |
| 2833 | * being a thread_group_leader is the obvious thing |
| 2834 | * todo but there is a window when it fails, due to |
| 2835 | * the pid transfer logic in de_thread. |
| 2836 | * |
| 2837 | * So we perform the straight forward test of seeing |
| 2838 | * if the pid we have found is the pid of a thread |
| 2839 | * group leader, and don't worry if the task we have |
| 2840 | * found doesn't happen to be a thread group leader. |
| 2841 | * As we don't care in the case of readdir. |
| 2842 | */ |
Eric W. Biederman | 19fd4bb | 2007-11-28 16:21:26 -0800 | [diff] [blame] | 2843 | if (!iter.task || !has_group_leader_pid(iter.task)) { |
| 2844 | iter.tgid += 1; |
Eric W. Biederman | 0804ef4 | 2006-10-02 02:17:04 -0700 | [diff] [blame] | 2845 | goto retry; |
Eric W. Biederman | 19fd4bb | 2007-11-28 16:21:26 -0800 | [diff] [blame] | 2846 | } |
| 2847 | get_task_struct(iter.task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2848 | } |
Eric W. Biederman | 454cc10 | 2006-06-26 00:25:51 -0700 | [diff] [blame] | 2849 | rcu_read_unlock(); |
Eric W. Biederman | 19fd4bb | 2007-11-28 16:21:26 -0800 | [diff] [blame] | 2850 | return iter; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2851 | } |
| 2852 | |
Eric W. Biederman | 0097875 | 2014-07-31 03:10:50 -0700 | [diff] [blame] | 2853 | #define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2854 | |
| 2855 | /* for the /proc/ directory itself, after non-process stuff has been done */ |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 2856 | int proc_pid_readdir(struct file *file, struct dir_context *ctx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2857 | { |
Eric W. Biederman | 19fd4bb | 2007-11-28 16:21:26 -0800 | [diff] [blame] | 2858 | struct tgid_iter iter; |
Al Viro | 3aa3377 | 2014-10-31 00:42:35 -0400 | [diff] [blame] | 2859 | struct pid_namespace *ns = file_inode(file)->i_sb->s_fs_info; |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 2860 | loff_t pos = ctx->pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2861 | |
Al Viro | 021ada7 | 2013-03-29 19:27:05 -0400 | [diff] [blame] | 2862 | if (pos >= PID_MAX_LIMIT + TGID_OFFSET) |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 2863 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2864 | |
Eric W. Biederman | 0097875 | 2014-07-31 03:10:50 -0700 | [diff] [blame] | 2865 | if (pos == TGID_OFFSET - 2) { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 2866 | struct inode *inode = d_inode(ns->proc_self); |
Al Viro | db96316 | 2013-06-15 10:45:10 +0400 | [diff] [blame] | 2867 | if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK)) |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 2868 | return 0; |
Eric W. Biederman | 0097875 | 2014-07-31 03:10:50 -0700 | [diff] [blame] | 2869 | ctx->pos = pos = pos + 1; |
Al Viro | 021ada7 | 2013-03-29 19:27:05 -0400 | [diff] [blame] | 2870 | } |
Eric W. Biederman | 0097875 | 2014-07-31 03:10:50 -0700 | [diff] [blame] | 2871 | if (pos == TGID_OFFSET - 1) { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 2872 | struct inode *inode = d_inode(ns->proc_thread_self); |
Eric W. Biederman | 0097875 | 2014-07-31 03:10:50 -0700 | [diff] [blame] | 2873 | if (!dir_emit(ctx, "thread-self", 11, inode->i_ino, DT_LNK)) |
| 2874 | return 0; |
| 2875 | ctx->pos = pos = pos + 1; |
| 2876 | } |
| 2877 | iter.tgid = pos - TGID_OFFSET; |
Eric W. Biederman | 19fd4bb | 2007-11-28 16:21:26 -0800 | [diff] [blame] | 2878 | iter.task = NULL; |
Eric W. Biederman | 19fd4bb | 2007-11-28 16:21:26 -0800 | [diff] [blame] | 2879 | for (iter = next_tgid(ns, iter); |
| 2880 | iter.task; |
| 2881 | iter.tgid += 1, iter = next_tgid(ns, iter)) { |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 2882 | char name[PROC_NUMBUF]; |
| 2883 | int len; |
| 2884 | if (!has_pid_permissions(ns, iter.task, 2)) |
| 2885 | continue; |
Vasiliy Kulikov | 0499680 | 2012-01-10 15:11:31 -0800 | [diff] [blame] | 2886 | |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 2887 | len = snprintf(name, sizeof(name), "%d", iter.tgid); |
| 2888 | ctx->pos = iter.tgid + TGID_OFFSET; |
| 2889 | if (!proc_fill_cache(file, ctx, name, len, |
| 2890 | proc_pid_instantiate, iter.task, NULL)) { |
Eric W. Biederman | 19fd4bb | 2007-11-28 16:21:26 -0800 | [diff] [blame] | 2891 | put_task_struct(iter.task); |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 2892 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2893 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2894 | } |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 2895 | ctx->pos = PID_MAX_LIMIT + TGID_OFFSET; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2896 | return 0; |
| 2897 | } |
| 2898 | |
Eric W. Biederman | 0bc58a9 | 2006-06-26 00:25:50 -0700 | [diff] [blame] | 2899 | /* |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2900 | * Tasks |
| 2901 | */ |
Eric Dumazet | c5141e6 | 2007-05-08 00:26:15 -0700 | [diff] [blame] | 2902 | static const struct pid_entry tid_base_stuff[] = { |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 2903 | DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations), |
Jerome Marchand | 3835541 | 2010-04-27 13:13:06 -0700 | [diff] [blame] | 2904 | DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations), |
Eric W. Biederman | 6b4e306 | 2010-03-07 16:41:34 -0800 | [diff] [blame] | 2905 | DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations), |
Eric W. Biederman | 6ba8ed7 | 2014-07-31 16:27:08 -0700 | [diff] [blame] | 2906 | #ifdef CONFIG_NET |
| 2907 | DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations), |
| 2908 | #endif |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 2909 | REG("environ", S_IRUSR, proc_environ_operations), |
Alexey Dobriyan | f9ea536 | 2014-08-08 14:21:35 -0700 | [diff] [blame] | 2910 | ONE("auxv", S_IRUSR, proc_pid_auxv), |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 2911 | ONE("status", S_IRUGO, proc_pid_status), |
Djalal Harouni | 35a3504 | 2014-04-07 15:38:36 -0700 | [diff] [blame] | 2912 | ONE("personality", S_IRUSR, proc_pid_personality), |
Alexey Dobriyan | 1c963eb | 2014-08-08 14:21:37 -0700 | [diff] [blame] | 2913 | ONE("limits", S_IRUGO, proc_pid_limits), |
Ingo Molnar | 43ae34c | 2007-07-09 18:52:00 +0200 | [diff] [blame] | 2914 | #ifdef CONFIG_SCHED_DEBUG |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 2915 | REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations), |
Ingo Molnar | 43ae34c | 2007-07-09 18:52:00 +0200 | [diff] [blame] | 2916 | #endif |
john stultz | 4614a696b | 2009-12-14 18:00:05 -0800 | [diff] [blame] | 2917 | REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations), |
Roland McGrath | ebcb673 | 2008-07-25 19:46:00 -0700 | [diff] [blame] | 2918 | #ifdef CONFIG_HAVE_ARCH_TRACEHOOK |
Alexey Dobriyan | 09d93bd | 2014-08-08 14:21:39 -0700 | [diff] [blame] | 2919 | ONE("syscall", S_IRUSR, proc_pid_syscall), |
Roland McGrath | ebcb673 | 2008-07-25 19:46:00 -0700 | [diff] [blame] | 2920 | #endif |
Alexey Dobriyan | 2ca66ff | 2014-08-08 14:21:41 -0700 | [diff] [blame] | 2921 | ONE("cmdline", S_IRUGO, proc_pid_cmdline), |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 2922 | ONE("stat", S_IRUGO, proc_tid_stat), |
| 2923 | ONE("statm", S_IRUGO, proc_pid_statm), |
Siddhesh Poyarekar | b764375 | 2012-03-21 16:34:04 -0700 | [diff] [blame] | 2924 | REG("maps", S_IRUGO, proc_tid_maps_operations), |
Cyrill Gorcunov | 81841161 | 2012-05-31 16:26:43 -0700 | [diff] [blame] | 2925 | #ifdef CONFIG_CHECKPOINT_RESTORE |
| 2926 | REG("children", S_IRUGO, proc_tid_children_operations), |
| 2927 | #endif |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2928 | #ifdef CONFIG_NUMA |
Siddhesh Poyarekar | b764375 | 2012-03-21 16:34:04 -0700 | [diff] [blame] | 2929 | REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations), |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2930 | #endif |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 2931 | REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations), |
| 2932 | LNK("cwd", proc_cwd_link), |
| 2933 | LNK("root", proc_root_link), |
| 2934 | LNK("exe", proc_exe_link), |
| 2935 | REG("mounts", S_IRUGO, proc_mounts_operations), |
| 2936 | REG("mountinfo", S_IRUGO, proc_mountinfo_operations), |
Matt Mackall | 1e88328 | 2008-02-04 22:29:07 -0800 | [diff] [blame] | 2937 | #ifdef CONFIG_PROC_PAGE_MONITOR |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 2938 | REG("clear_refs", S_IWUSR, proc_clear_refs_operations), |
Siddhesh Poyarekar | b764375 | 2012-03-21 16:34:04 -0700 | [diff] [blame] | 2939 | REG("smaps", S_IRUGO, proc_tid_smaps_operations), |
Djalal Harouni | 32ed74a | 2014-04-07 15:38:38 -0700 | [diff] [blame] | 2940 | REG("pagemap", S_IRUSR, proc_pagemap_operations), |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2941 | #endif |
| 2942 | #ifdef CONFIG_SECURITY |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 2943 | DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations), |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2944 | #endif |
| 2945 | #ifdef CONFIG_KALLSYMS |
Alexey Dobriyan | edfcd60 | 2014-08-08 14:21:44 -0700 | [diff] [blame] | 2946 | ONE("wchan", S_IRUGO, proc_pid_wchan), |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2947 | #endif |
Ken Chen | 2ec220e | 2008-11-10 11:26:08 +0300 | [diff] [blame] | 2948 | #ifdef CONFIG_STACKTRACE |
Djalal Harouni | 35a3504 | 2014-04-07 15:38:36 -0700 | [diff] [blame] | 2949 | ONE("stack", S_IRUSR, proc_pid_stack), |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2950 | #endif |
| 2951 | #ifdef CONFIG_SCHEDSTATS |
Alexey Dobriyan | f6e826c | 2014-08-08 14:21:46 -0700 | [diff] [blame] | 2952 | ONE("schedstat", S_IRUGO, proc_pid_schedstat), |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2953 | #endif |
Arjan van de Ven | 9745512 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 2954 | #ifdef CONFIG_LATENCYTOP |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 2955 | REG("latency", S_IRUGO, proc_lstats_operations), |
Arjan van de Ven | 9745512 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 2956 | #endif |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 2957 | #ifdef CONFIG_PROC_PID_CPUSET |
Zefan Li | 52de477 | 2014-09-18 16:03:36 +0800 | [diff] [blame] | 2958 | ONE("cpuset", S_IRUGO, proc_cpuset_show), |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2959 | #endif |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 2960 | #ifdef CONFIG_CGROUPS |
Zefan Li | 006f4ac | 2014-09-18 16:03:15 +0800 | [diff] [blame] | 2961 | ONE("cgroup", S_IRUGO, proc_cgroup_show), |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 2962 | #endif |
Alexey Dobriyan | 6ba51e3 | 2014-08-08 14:21:48 -0700 | [diff] [blame] | 2963 | ONE("oom_score", S_IRUGO, proc_oom_score), |
David Rientjes | fa0cbbf | 2012-11-12 17:53:04 -0800 | [diff] [blame] | 2964 | REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations), |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 2965 | REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations), |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2966 | #ifdef CONFIG_AUDITSYSCALL |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 2967 | REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations), |
Al Viro | 26ec3c6 | 2011-02-15 21:24:05 -0500 | [diff] [blame] | 2968 | REG("sessionid", S_IRUGO, proc_sessionid_operations), |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2969 | #endif |
Akinobu Mita | f4f154f | 2006-12-08 02:39:47 -0800 | [diff] [blame] | 2970 | #ifdef CONFIG_FAULT_INJECTION |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 2971 | REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations), |
Akinobu Mita | f4f154f | 2006-12-08 02:39:47 -0800 | [diff] [blame] | 2972 | #endif |
Andrea Righi | 297c5d9 | 2008-07-25 01:48:49 -0700 | [diff] [blame] | 2973 | #ifdef CONFIG_TASK_IO_ACCOUNTING |
Alexey Dobriyan | 19aadc9 | 2014-08-08 14:21:50 -0700 | [diff] [blame] | 2974 | ONE("io", S_IRUSR, proc_tid_io_accounting), |
Andrea Righi | 297c5d9 | 2008-07-25 01:48:49 -0700 | [diff] [blame] | 2975 | #endif |
Chris Metcalf | f133ecc | 2011-05-26 12:40:09 -0400 | [diff] [blame] | 2976 | #ifdef CONFIG_HARDWALL |
Alexey Dobriyan | d962c14 | 2014-08-08 14:21:52 -0700 | [diff] [blame] | 2977 | ONE("hardwall", S_IRUGO, proc_pid_hardwall), |
Chris Metcalf | f133ecc | 2011-05-26 12:40:09 -0400 | [diff] [blame] | 2978 | #endif |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 2979 | #ifdef CONFIG_USER_NS |
| 2980 | REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations), |
| 2981 | REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations), |
Eric W. Biederman | f76d207 | 2012-08-30 01:24:05 -0700 | [diff] [blame] | 2982 | REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations), |
Eric W. Biederman | 9cc4651 | 2014-12-02 12:27:26 -0600 | [diff] [blame] | 2983 | REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations), |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 2984 | #endif |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2985 | }; |
| 2986 | |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 2987 | static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx) |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2988 | { |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 2989 | return proc_pident_readdir(file, ctx, |
| 2990 | tid_base_stuff, ARRAY_SIZE(tid_base_stuff)); |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2991 | } |
| 2992 | |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 2993 | static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags) |
| 2994 | { |
Eric W. Biederman | 7bcd6b0 | 2006-10-02 02:18:56 -0700 | [diff] [blame] | 2995 | return proc_pident_lookup(dir, dentry, |
| 2996 | tid_base_stuff, ARRAY_SIZE(tid_base_stuff)); |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2997 | } |
| 2998 | |
Arjan van de Ven | 00977a5 | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 2999 | static const struct file_operations proc_tid_base_operations = { |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3000 | .read = generic_read_dir, |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 3001 | .iterate = proc_tid_base_readdir, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 3002 | .llseek = default_llseek, |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3003 | }; |
| 3004 | |
Arjan van de Ven | c5ef1c4 | 2007-02-12 00:55:40 -0800 | [diff] [blame] | 3005 | static const struct inode_operations proc_tid_base_inode_operations = { |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3006 | .lookup = proc_tid_base_lookup, |
| 3007 | .getattr = pid_getattr, |
| 3008 | .setattr = proc_setattr, |
| 3009 | }; |
| 3010 | |
Al Viro | c52a47a | 2013-06-15 11:15:20 +0400 | [diff] [blame] | 3011 | static int proc_task_instantiate(struct inode *dir, |
Eric Dumazet | c5141e6 | 2007-05-08 00:26:15 -0700 | [diff] [blame] | 3012 | struct dentry *dentry, struct task_struct *task, const void *ptr) |
Eric W. Biederman | 444ceed | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 3013 | { |
Eric W. Biederman | 444ceed | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 3014 | struct inode *inode; |
Eric W. Biederman | 61a2878 | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 3015 | inode = proc_pid_make_inode(dir->i_sb, task); |
Eric W. Biederman | 444ceed | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 3016 | |
| 3017 | if (!inode) |
| 3018 | goto out; |
| 3019 | inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO; |
| 3020 | inode->i_op = &proc_tid_base_inode_operations; |
| 3021 | inode->i_fop = &proc_tid_base_operations; |
| 3022 | inode->i_flags|=S_IMMUTABLE; |
Vegard Nossum | aed5417 | 2008-06-05 22:46:53 -0700 | [diff] [blame] | 3023 | |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 3024 | set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff, |
| 3025 | ARRAY_SIZE(tid_base_stuff))); |
Eric W. Biederman | 444ceed | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 3026 | |
Nick Piggin | fb045ad | 2011-01-07 17:49:55 +1100 | [diff] [blame] | 3027 | d_set_d_op(dentry, &pid_dentry_operations); |
Eric W. Biederman | 444ceed | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 3028 | |
| 3029 | d_add(dentry, inode); |
| 3030 | /* Close the race of the process dying before we return the dentry */ |
Al Viro | 0b728e1 | 2012-06-10 16:03:43 -0400 | [diff] [blame] | 3031 | if (pid_revalidate(dentry, 0)) |
Al Viro | c52a47a | 2013-06-15 11:15:20 +0400 | [diff] [blame] | 3032 | return 0; |
Eric W. Biederman | 444ceed | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 3033 | out: |
Al Viro | c52a47a | 2013-06-15 11:15:20 +0400 | [diff] [blame] | 3034 | return -ENOENT; |
Eric W. Biederman | 444ceed | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 3035 | } |
| 3036 | |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 3037 | static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags) |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3038 | { |
Al Viro | c52a47a | 2013-06-15 11:15:20 +0400 | [diff] [blame] | 3039 | int result = -ENOENT; |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3040 | struct task_struct *task; |
| 3041 | struct task_struct *leader = get_proc_task(dir); |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3042 | unsigned tid; |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 3043 | struct pid_namespace *ns; |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3044 | |
| 3045 | if (!leader) |
| 3046 | goto out_no_task; |
| 3047 | |
Alexey Dobriyan | dbcdb50 | 2014-08-08 14:21:25 -0700 | [diff] [blame] | 3048 | tid = name_to_int(&dentry->d_name); |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3049 | if (tid == ~0U) |
| 3050 | goto out; |
| 3051 | |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 3052 | ns = dentry->d_sb->s_fs_info; |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3053 | rcu_read_lock(); |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 3054 | task = find_task_by_pid_ns(tid, ns); |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3055 | if (task) |
| 3056 | get_task_struct(task); |
| 3057 | rcu_read_unlock(); |
| 3058 | if (!task) |
| 3059 | goto out; |
Pavel Emelyanov | bac0abd | 2007-10-18 23:40:18 -0700 | [diff] [blame] | 3060 | if (!same_thread_group(leader, task)) |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3061 | goto out_drop_task; |
| 3062 | |
Eric W. Biederman | 444ceed | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 3063 | result = proc_task_instantiate(dir, dentry, task, NULL); |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3064 | out_drop_task: |
| 3065 | put_task_struct(task); |
| 3066 | out: |
| 3067 | put_task_struct(leader); |
| 3068 | out_no_task: |
Al Viro | c52a47a | 2013-06-15 11:15:20 +0400 | [diff] [blame] | 3069 | return ERR_PTR(result); |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3070 | } |
| 3071 | |
| 3072 | /* |
Eric W. Biederman | 0bc58a9 | 2006-06-26 00:25:50 -0700 | [diff] [blame] | 3073 | * Find the first tid of a thread group to return to user space. |
| 3074 | * |
| 3075 | * Usually this is just the thread group leader, but if the users |
| 3076 | * buffer was too small or there was a seek into the middle of the |
| 3077 | * directory we have more work todo. |
| 3078 | * |
| 3079 | * In the case of a short read we start with find_task_by_pid. |
| 3080 | * |
| 3081 | * In the case of a seek we start with the leader and walk nr |
| 3082 | * threads past it. |
| 3083 | */ |
Oleg Nesterov | 9f6e963 | 2014-01-23 15:55:40 -0800 | [diff] [blame] | 3084 | static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos, |
| 3085 | struct pid_namespace *ns) |
Eric W. Biederman | 0bc58a9 | 2006-06-26 00:25:50 -0700 | [diff] [blame] | 3086 | { |
Oleg Nesterov | d855a4b | 2014-01-23 15:55:39 -0800 | [diff] [blame] | 3087 | struct task_struct *pos, *task; |
Oleg Nesterov | 9f6e963 | 2014-01-23 15:55:40 -0800 | [diff] [blame] | 3088 | unsigned long nr = f_pos; |
| 3089 | |
| 3090 | if (nr != f_pos) /* 32bit overflow? */ |
| 3091 | return NULL; |
Eric W. Biederman | 0bc58a9 | 2006-06-26 00:25:50 -0700 | [diff] [blame] | 3092 | |
Eric W. Biederman | cc28873 | 2006-06-26 00:26:01 -0700 | [diff] [blame] | 3093 | rcu_read_lock(); |
Oleg Nesterov | d855a4b | 2014-01-23 15:55:39 -0800 | [diff] [blame] | 3094 | task = pid_task(pid, PIDTYPE_PID); |
| 3095 | if (!task) |
| 3096 | goto fail; |
| 3097 | |
| 3098 | /* Attempt to start with the tid of a thread */ |
Oleg Nesterov | 9f6e963 | 2014-01-23 15:55:40 -0800 | [diff] [blame] | 3099 | if (tid && nr) { |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 3100 | pos = find_task_by_pid_ns(tid, ns); |
Oleg Nesterov | d855a4b | 2014-01-23 15:55:39 -0800 | [diff] [blame] | 3101 | if (pos && same_thread_group(pos, task)) |
Oleg Nesterov | a872ff0 | 2006-06-26 00:26:01 -0700 | [diff] [blame] | 3102 | goto found; |
Eric W. Biederman | 0bc58a9 | 2006-06-26 00:25:50 -0700 | [diff] [blame] | 3103 | } |
| 3104 | |
| 3105 | /* If nr exceeds the number of threads there is nothing todo */ |
Oleg Nesterov | 9f6e963 | 2014-01-23 15:55:40 -0800 | [diff] [blame] | 3106 | if (nr >= get_nr_threads(task)) |
Oleg Nesterov | c986c14 | 2014-01-23 15:55:38 -0800 | [diff] [blame] | 3107 | goto fail; |
Oleg Nesterov | a872ff0 | 2006-06-26 00:26:01 -0700 | [diff] [blame] | 3108 | |
| 3109 | /* If we haven't found our starting place yet start |
| 3110 | * with the leader and walk nr threads forward. |
| 3111 | */ |
Oleg Nesterov | d855a4b | 2014-01-23 15:55:39 -0800 | [diff] [blame] | 3112 | pos = task = task->group_leader; |
Oleg Nesterov | c986c14 | 2014-01-23 15:55:38 -0800 | [diff] [blame] | 3113 | do { |
Oleg Nesterov | 9f6e963 | 2014-01-23 15:55:40 -0800 | [diff] [blame] | 3114 | if (!nr--) |
Oleg Nesterov | c986c14 | 2014-01-23 15:55:38 -0800 | [diff] [blame] | 3115 | goto found; |
Oleg Nesterov | d855a4b | 2014-01-23 15:55:39 -0800 | [diff] [blame] | 3116 | } while_each_thread(task, pos); |
Oleg Nesterov | c986c14 | 2014-01-23 15:55:38 -0800 | [diff] [blame] | 3117 | fail: |
| 3118 | pos = NULL; |
| 3119 | goto out; |
Oleg Nesterov | a872ff0 | 2006-06-26 00:26:01 -0700 | [diff] [blame] | 3120 | found: |
| 3121 | get_task_struct(pos); |
| 3122 | out: |
Eric W. Biederman | cc28873 | 2006-06-26 00:26:01 -0700 | [diff] [blame] | 3123 | rcu_read_unlock(); |
Eric W. Biederman | 0bc58a9 | 2006-06-26 00:25:50 -0700 | [diff] [blame] | 3124 | return pos; |
| 3125 | } |
| 3126 | |
| 3127 | /* |
| 3128 | * Find the next thread in the thread list. |
| 3129 | * Return NULL if there is an error or no next thread. |
| 3130 | * |
| 3131 | * The reference to the input task_struct is released. |
| 3132 | */ |
| 3133 | static struct task_struct *next_tid(struct task_struct *start) |
| 3134 | { |
Oleg Nesterov | c1df7fb | 2006-06-26 00:26:02 -0700 | [diff] [blame] | 3135 | struct task_struct *pos = NULL; |
Eric W. Biederman | cc28873 | 2006-06-26 00:26:01 -0700 | [diff] [blame] | 3136 | rcu_read_lock(); |
Oleg Nesterov | c1df7fb | 2006-06-26 00:26:02 -0700 | [diff] [blame] | 3137 | if (pid_alive(start)) { |
Eric W. Biederman | 0bc58a9 | 2006-06-26 00:25:50 -0700 | [diff] [blame] | 3138 | pos = next_thread(start); |
Oleg Nesterov | c1df7fb | 2006-06-26 00:26:02 -0700 | [diff] [blame] | 3139 | if (thread_group_leader(pos)) |
| 3140 | pos = NULL; |
| 3141 | else |
| 3142 | get_task_struct(pos); |
| 3143 | } |
Eric W. Biederman | cc28873 | 2006-06-26 00:26:01 -0700 | [diff] [blame] | 3144 | rcu_read_unlock(); |
Eric W. Biederman | 0bc58a9 | 2006-06-26 00:25:50 -0700 | [diff] [blame] | 3145 | put_task_struct(start); |
| 3146 | return pos; |
| 3147 | } |
| 3148 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3149 | /* for the /proc/TGID/task/ directories */ |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 3150 | static int proc_task_readdir(struct file *file, struct dir_context *ctx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3151 | { |
Oleg Nesterov | d855a4b | 2014-01-23 15:55:39 -0800 | [diff] [blame] | 3152 | struct inode *inode = file_inode(file); |
| 3153 | struct task_struct *task; |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 3154 | struct pid_namespace *ns; |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 3155 | int tid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3156 | |
Oleg Nesterov | d855a4b | 2014-01-23 15:55:39 -0800 | [diff] [blame] | 3157 | if (proc_inode_is_dead(inode)) |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 3158 | return -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3159 | |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 3160 | if (!dir_emit_dots(file, ctx)) |
Oleg Nesterov | d855a4b | 2014-01-23 15:55:39 -0800 | [diff] [blame] | 3161 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3162 | |
Eric W. Biederman | 0bc58a9 | 2006-06-26 00:25:50 -0700 | [diff] [blame] | 3163 | /* f_version caches the tgid value that the last readdir call couldn't |
| 3164 | * return. lseek aka telldir automagically resets f_version to 0. |
| 3165 | */ |
Al Viro | 3aa3377 | 2014-10-31 00:42:35 -0400 | [diff] [blame] | 3166 | ns = inode->i_sb->s_fs_info; |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 3167 | tid = (int)file->f_version; |
| 3168 | file->f_version = 0; |
Oleg Nesterov | d855a4b | 2014-01-23 15:55:39 -0800 | [diff] [blame] | 3169 | for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns); |
Eric W. Biederman | 0bc58a9 | 2006-06-26 00:25:50 -0700 | [diff] [blame] | 3170 | task; |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 3171 | task = next_tid(task), ctx->pos++) { |
| 3172 | char name[PROC_NUMBUF]; |
| 3173 | int len; |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 3174 | tid = task_pid_nr_ns(task, ns); |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 3175 | len = snprintf(name, sizeof(name), "%d", tid); |
| 3176 | if (!proc_fill_cache(file, ctx, name, len, |
| 3177 | proc_task_instantiate, task, NULL)) { |
Eric W. Biederman | 0bc58a9 | 2006-06-26 00:25:50 -0700 | [diff] [blame] | 3178 | /* returning this tgid failed, save it as the first |
| 3179 | * pid for the next readir call */ |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 3180 | file->f_version = (u64)tid; |
Eric W. Biederman | 0bc58a9 | 2006-06-26 00:25:50 -0700 | [diff] [blame] | 3181 | put_task_struct(task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3182 | break; |
Eric W. Biederman | 0bc58a9 | 2006-06-26 00:25:50 -0700 | [diff] [blame] | 3183 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3184 | } |
Oleg Nesterov | d855a4b | 2014-01-23 15:55:39 -0800 | [diff] [blame] | 3185 | |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 3186 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3187 | } |
Eric W. Biederman | 6e66b52 | 2006-06-26 00:25:47 -0700 | [diff] [blame] | 3188 | |
| 3189 | static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) |
| 3190 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 3191 | struct inode *inode = d_inode(dentry); |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 3192 | struct task_struct *p = get_proc_task(inode); |
Eric W. Biederman | 6e66b52 | 2006-06-26 00:25:47 -0700 | [diff] [blame] | 3193 | generic_fillattr(inode, stat); |
| 3194 | |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 3195 | if (p) { |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 3196 | stat->nlink += get_nr_threads(p); |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 3197 | put_task_struct(p); |
Eric W. Biederman | 6e66b52 | 2006-06-26 00:25:47 -0700 | [diff] [blame] | 3198 | } |
| 3199 | |
| 3200 | return 0; |
| 3201 | } |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3202 | |
Arjan van de Ven | c5ef1c4 | 2007-02-12 00:55:40 -0800 | [diff] [blame] | 3203 | static const struct inode_operations proc_task_inode_operations = { |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3204 | .lookup = proc_task_lookup, |
| 3205 | .getattr = proc_task_getattr, |
| 3206 | .setattr = proc_setattr, |
Vasiliy Kulikov | 0499680 | 2012-01-10 15:11:31 -0800 | [diff] [blame] | 3207 | .permission = proc_pid_permission, |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3208 | }; |
| 3209 | |
Arjan van de Ven | 00977a5 | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 3210 | static const struct file_operations proc_task_operations = { |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3211 | .read = generic_read_dir, |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 3212 | .iterate = proc_task_readdir, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 3213 | .llseek = default_llseek, |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3214 | }; |