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