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