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