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