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