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