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