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