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