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