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