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