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