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