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