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