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