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