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