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