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