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