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