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