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