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