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