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; |
Eric W. Biederman | df26c40 | 2006-06-26 00:25:59 -0700 | [diff] [blame^] | 539 | /* Allow access to a task's file descriptors if it is us or we |
| 540 | * may use ptrace attach to the process and find out that |
| 541 | * information. |
Eric W. Biederman | 778c114 | 2006-06-26 00:25:58 -0700 | [diff] [blame] | 542 | */ |
| 543 | task = get_proc_task(inode); |
Eric W. Biederman | df26c40 | 2006-06-26 00:25:59 -0700 | [diff] [blame^] | 544 | if (task) { |
| 545 | allowed = ptrace_may_attach(task); |
Eric W. Biederman | 778c114 | 2006-06-26 00:25:58 -0700 | [diff] [blame] | 546 | put_task_struct(task); |
Eric W. Biederman | df26c40 | 2006-06-26 00:25:59 -0700 | [diff] [blame^] | 547 | } |
Eric W. Biederman | 778c114 | 2006-06-26 00:25:58 -0700 | [diff] [blame] | 548 | return allowed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | } |
| 550 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | extern struct seq_operations mounts_op; |
Al Viro | 5addc5d | 2005-11-07 17:15:49 -0500 | [diff] [blame] | 552 | struct proc_mounts { |
| 553 | struct seq_file m; |
| 554 | int event; |
| 555 | }; |
| 556 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | static int mounts_open(struct inode *inode, struct file *file) |
| 558 | { |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 559 | struct task_struct *task = get_proc_task(inode); |
| 560 | struct namespace *namespace = NULL; |
Al Viro | 5addc5d | 2005-11-07 17:15:49 -0500 | [diff] [blame] | 561 | struct proc_mounts *p; |
| 562 | int ret = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 564 | if (task) { |
| 565 | task_lock(task); |
| 566 | namespace = task->namespace; |
| 567 | if (namespace) |
| 568 | get_namespace(namespace); |
| 569 | task_unlock(task); |
| 570 | put_task_struct(task); |
| 571 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | |
Al Viro | 5addc5d | 2005-11-07 17:15:49 -0500 | [diff] [blame] | 573 | if (namespace) { |
| 574 | ret = -ENOMEM; |
| 575 | p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL); |
| 576 | if (p) { |
| 577 | file->private_data = &p->m; |
| 578 | ret = seq_open(file, &mounts_op); |
| 579 | if (!ret) { |
| 580 | p->m.private = namespace; |
| 581 | p->event = namespace->event; |
| 582 | return 0; |
| 583 | } |
| 584 | kfree(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | } |
Al Viro | 5addc5d | 2005-11-07 17:15:49 -0500 | [diff] [blame] | 586 | put_namespace(namespace); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | } |
| 588 | return ret; |
| 589 | } |
| 590 | |
| 591 | static int mounts_release(struct inode *inode, struct file *file) |
| 592 | { |
| 593 | struct seq_file *m = file->private_data; |
| 594 | struct namespace *namespace = m->private; |
| 595 | put_namespace(namespace); |
| 596 | return seq_release(inode, file); |
| 597 | } |
| 598 | |
Al Viro | 5addc5d | 2005-11-07 17:15:49 -0500 | [diff] [blame] | 599 | static unsigned mounts_poll(struct file *file, poll_table *wait) |
| 600 | { |
| 601 | struct proc_mounts *p = file->private_data; |
| 602 | struct namespace *ns = p->m.private; |
| 603 | unsigned res = 0; |
| 604 | |
| 605 | poll_wait(file, &ns->poll, wait); |
| 606 | |
| 607 | spin_lock(&vfsmount_lock); |
| 608 | if (p->event != ns->event) { |
| 609 | p->event = ns->event; |
| 610 | res = POLLERR; |
| 611 | } |
| 612 | spin_unlock(&vfsmount_lock); |
| 613 | |
| 614 | return res; |
| 615 | } |
| 616 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | static struct file_operations proc_mounts_operations = { |
| 618 | .open = mounts_open, |
| 619 | .read = seq_read, |
| 620 | .llseek = seq_lseek, |
| 621 | .release = mounts_release, |
Al Viro | 5addc5d | 2005-11-07 17:15:49 -0500 | [diff] [blame] | 622 | .poll = mounts_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | }; |
| 624 | |
Chuck Lever | b4629fe | 2006-03-20 13:44:12 -0500 | [diff] [blame] | 625 | extern struct seq_operations mountstats_op; |
| 626 | static int mountstats_open(struct inode *inode, struct file *file) |
| 627 | { |
Chuck Lever | b4629fe | 2006-03-20 13:44:12 -0500 | [diff] [blame] | 628 | int ret = seq_open(file, &mountstats_op); |
| 629 | |
| 630 | if (!ret) { |
| 631 | struct seq_file *m = file->private_data; |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 632 | struct namespace *namespace = NULL; |
| 633 | struct task_struct *task = get_proc_task(inode); |
| 634 | |
| 635 | if (task) { |
| 636 | task_lock(task); |
| 637 | namespace = task->namespace; |
| 638 | if (namespace) |
| 639 | get_namespace(namespace); |
| 640 | task_unlock(task); |
| 641 | put_task_struct(task); |
| 642 | } |
Chuck Lever | b4629fe | 2006-03-20 13:44:12 -0500 | [diff] [blame] | 643 | |
| 644 | if (namespace) |
| 645 | m->private = namespace; |
| 646 | else { |
| 647 | seq_release(inode, file); |
| 648 | ret = -EINVAL; |
| 649 | } |
| 650 | } |
| 651 | return ret; |
| 652 | } |
| 653 | |
| 654 | static struct file_operations proc_mountstats_operations = { |
| 655 | .open = mountstats_open, |
| 656 | .read = seq_read, |
| 657 | .llseek = seq_lseek, |
| 658 | .release = mounts_release, |
| 659 | }; |
| 660 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */ |
| 662 | |
| 663 | static ssize_t proc_info_read(struct file * file, char __user * buf, |
| 664 | size_t count, loff_t *ppos) |
| 665 | { |
| 666 | struct inode * inode = file->f_dentry->d_inode; |
| 667 | unsigned long page; |
| 668 | ssize_t length; |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 669 | struct task_struct *task = get_proc_task(inode); |
| 670 | |
| 671 | length = -ESRCH; |
| 672 | if (!task) |
| 673 | goto out_no_task; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | |
| 675 | if (count > PROC_BLOCK_SIZE) |
| 676 | count = PROC_BLOCK_SIZE; |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 677 | |
| 678 | length = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | if (!(page = __get_free_page(GFP_KERNEL))) |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 680 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | |
| 682 | length = PROC_I(inode)->op.proc_read(task, (char*)page); |
| 683 | |
| 684 | if (length >= 0) |
| 685 | length = simple_read_from_buffer(buf, count, ppos, (char *)page, length); |
| 686 | free_page(page); |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 687 | out: |
| 688 | put_task_struct(task); |
| 689 | out_no_task: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | return length; |
| 691 | } |
| 692 | |
| 693 | static struct file_operations proc_info_file_operations = { |
| 694 | .read = proc_info_read, |
| 695 | }; |
| 696 | |
| 697 | static int mem_open(struct inode* inode, struct file* file) |
| 698 | { |
| 699 | file->private_data = (void*)((long)current->self_exec_id); |
| 700 | return 0; |
| 701 | } |
| 702 | |
| 703 | static ssize_t mem_read(struct file * file, char __user * buf, |
| 704 | size_t count, loff_t *ppos) |
| 705 | { |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 706 | struct task_struct *task = get_proc_task(file->f_dentry->d_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | char *page; |
| 708 | unsigned long src = *ppos; |
| 709 | int ret = -ESRCH; |
| 710 | struct mm_struct *mm; |
| 711 | |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 712 | if (!task) |
| 713 | goto out_no_task; |
| 714 | |
Miklos Szeredi | ab8d11b | 2005-09-06 15:18:24 -0700 | [diff] [blame] | 715 | if (!MAY_PTRACE(task) || !ptrace_may_attach(task)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | goto out; |
| 717 | |
| 718 | ret = -ENOMEM; |
| 719 | page = (char *)__get_free_page(GFP_USER); |
| 720 | if (!page) |
| 721 | goto out; |
| 722 | |
| 723 | ret = 0; |
| 724 | |
| 725 | mm = get_task_mm(task); |
| 726 | if (!mm) |
| 727 | goto out_free; |
| 728 | |
| 729 | ret = -EIO; |
| 730 | |
| 731 | if (file->private_data != (void*)((long)current->self_exec_id)) |
| 732 | goto out_put; |
| 733 | |
| 734 | ret = 0; |
| 735 | |
| 736 | while (count > 0) { |
| 737 | int this_len, retval; |
| 738 | |
| 739 | this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count; |
| 740 | retval = access_process_vm(task, src, page, this_len, 0); |
Miklos Szeredi | ab8d11b | 2005-09-06 15:18:24 -0700 | [diff] [blame] | 741 | if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | if (!ret) |
| 743 | ret = -EIO; |
| 744 | break; |
| 745 | } |
| 746 | |
| 747 | if (copy_to_user(buf, page, retval)) { |
| 748 | ret = -EFAULT; |
| 749 | break; |
| 750 | } |
| 751 | |
| 752 | ret += retval; |
| 753 | src += retval; |
| 754 | buf += retval; |
| 755 | count -= retval; |
| 756 | } |
| 757 | *ppos = src; |
| 758 | |
| 759 | out_put: |
| 760 | mmput(mm); |
| 761 | out_free: |
| 762 | free_page((unsigned long) page); |
| 763 | out: |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 764 | put_task_struct(task); |
| 765 | out_no_task: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | return ret; |
| 767 | } |
| 768 | |
| 769 | #define mem_write NULL |
| 770 | |
| 771 | #ifndef mem_write |
| 772 | /* This is a security hazard */ |
| 773 | static ssize_t mem_write(struct file * file, const char * buf, |
| 774 | size_t count, loff_t *ppos) |
| 775 | { |
| 776 | int copied = 0; |
| 777 | char *page; |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 778 | struct task_struct *task = get_proc_task(file->f_dentry->d_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | unsigned long dst = *ppos; |
| 780 | |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 781 | copied = -ESRCH; |
| 782 | if (!task) |
| 783 | goto out_no_task; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 785 | if (!MAY_PTRACE(task) || !ptrace_may_attach(task)) |
| 786 | goto out; |
| 787 | |
| 788 | copied = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | page = (char *)__get_free_page(GFP_USER); |
| 790 | if (!page) |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 791 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | |
| 793 | while (count > 0) { |
| 794 | int this_len, retval; |
| 795 | |
| 796 | this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count; |
| 797 | if (copy_from_user(page, buf, this_len)) { |
| 798 | copied = -EFAULT; |
| 799 | break; |
| 800 | } |
| 801 | retval = access_process_vm(task, dst, page, this_len, 1); |
| 802 | if (!retval) { |
| 803 | if (!copied) |
| 804 | copied = -EIO; |
| 805 | break; |
| 806 | } |
| 807 | copied += retval; |
| 808 | buf += retval; |
| 809 | dst += retval; |
| 810 | count -= retval; |
| 811 | } |
| 812 | *ppos = dst; |
| 813 | free_page((unsigned long) page); |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 814 | out: |
| 815 | put_task_struct(task); |
| 816 | out_no_task: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | return copied; |
| 818 | } |
| 819 | #endif |
| 820 | |
| 821 | static loff_t mem_lseek(struct file * file, loff_t offset, int orig) |
| 822 | { |
| 823 | switch (orig) { |
| 824 | case 0: |
| 825 | file->f_pos = offset; |
| 826 | break; |
| 827 | case 1: |
| 828 | file->f_pos += offset; |
| 829 | break; |
| 830 | default: |
| 831 | return -EINVAL; |
| 832 | } |
| 833 | force_successful_syscall_return(); |
| 834 | return file->f_pos; |
| 835 | } |
| 836 | |
| 837 | static struct file_operations proc_mem_operations = { |
| 838 | .llseek = mem_lseek, |
| 839 | .read = mem_read, |
| 840 | .write = mem_write, |
| 841 | .open = mem_open, |
| 842 | }; |
| 843 | |
| 844 | static ssize_t oom_adjust_read(struct file *file, char __user *buf, |
| 845 | size_t count, loff_t *ppos) |
| 846 | { |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 847 | 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] | 848 | char buffer[PROC_NUMBUF]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | size_t len; |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 850 | int oom_adjust; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | loff_t __ppos = *ppos; |
| 852 | |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 853 | if (!task) |
| 854 | return -ESRCH; |
| 855 | oom_adjust = task->oomkilladj; |
| 856 | put_task_struct(task); |
| 857 | |
Eric W. Biederman | 8578cea | 2006-06-26 00:25:54 -0700 | [diff] [blame] | 858 | len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 859 | if (__ppos >= len) |
| 860 | return 0; |
| 861 | if (count > len-__ppos) |
| 862 | count = len-__ppos; |
| 863 | if (copy_to_user(buf, buffer + __ppos, count)) |
| 864 | return -EFAULT; |
| 865 | *ppos = __ppos + count; |
| 866 | return count; |
| 867 | } |
| 868 | |
| 869 | static ssize_t oom_adjust_write(struct file *file, const char __user *buf, |
| 870 | size_t count, loff_t *ppos) |
| 871 | { |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 872 | struct task_struct *task; |
Eric W. Biederman | 8578cea | 2006-06-26 00:25:54 -0700 | [diff] [blame] | 873 | char buffer[PROC_NUMBUF], *end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | int oom_adjust; |
| 875 | |
| 876 | if (!capable(CAP_SYS_RESOURCE)) |
| 877 | return -EPERM; |
Eric W. Biederman | 8578cea | 2006-06-26 00:25:54 -0700 | [diff] [blame] | 878 | memset(buffer, 0, sizeof(buffer)); |
| 879 | if (count > sizeof(buffer) - 1) |
| 880 | count = sizeof(buffer) - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | if (copy_from_user(buffer, buf, count)) |
| 882 | return -EFAULT; |
| 883 | oom_adjust = simple_strtol(buffer, &end, 0); |
Andrea Arcangeli | 79befd0 | 2005-04-16 15:24:05 -0700 | [diff] [blame] | 884 | if ((oom_adjust < -16 || oom_adjust > 15) && oom_adjust != OOM_DISABLE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | return -EINVAL; |
| 886 | if (*end == '\n') |
| 887 | end++; |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 888 | task = get_proc_task(file->f_dentry->d_inode); |
| 889 | if (!task) |
| 890 | return -ESRCH; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | task->oomkilladj = oom_adjust; |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 892 | put_task_struct(task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | if (end - buffer == 0) |
| 894 | return -EIO; |
| 895 | return end - buffer; |
| 896 | } |
| 897 | |
| 898 | static struct file_operations proc_oom_adjust_operations = { |
| 899 | .read = oom_adjust_read, |
| 900 | .write = oom_adjust_write, |
| 901 | }; |
| 902 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | #ifdef CONFIG_AUDITSYSCALL |
| 904 | #define TMPBUFLEN 21 |
| 905 | static ssize_t proc_loginuid_read(struct file * file, char __user * buf, |
| 906 | size_t count, loff_t *ppos) |
| 907 | { |
| 908 | struct inode * inode = file->f_dentry->d_inode; |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 909 | struct task_struct *task = get_proc_task(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | ssize_t length; |
| 911 | char tmpbuf[TMPBUFLEN]; |
| 912 | |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 913 | if (!task) |
| 914 | return -ESRCH; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | length = scnprintf(tmpbuf, TMPBUFLEN, "%u", |
| 916 | audit_get_loginuid(task->audit_context)); |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 917 | put_task_struct(task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); |
| 919 | } |
| 920 | |
| 921 | static ssize_t proc_loginuid_write(struct file * file, const char __user * buf, |
| 922 | size_t count, loff_t *ppos) |
| 923 | { |
| 924 | struct inode * inode = file->f_dentry->d_inode; |
| 925 | char *page, *tmp; |
| 926 | ssize_t length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | uid_t loginuid; |
| 928 | |
| 929 | if (!capable(CAP_AUDIT_CONTROL)) |
| 930 | return -EPERM; |
| 931 | |
Eric W. Biederman | 13b41b0 | 2006-06-26 00:25:56 -0700 | [diff] [blame] | 932 | if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | return -EPERM; |
| 934 | |
Al Viro | e018290 | 2006-05-18 08:28:02 -0400 | [diff] [blame] | 935 | if (count >= PAGE_SIZE) |
| 936 | count = PAGE_SIZE - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | |
| 938 | if (*ppos != 0) { |
| 939 | /* No partial writes. */ |
| 940 | return -EINVAL; |
| 941 | } |
| 942 | page = (char*)__get_free_page(GFP_USER); |
| 943 | if (!page) |
| 944 | return -ENOMEM; |
| 945 | length = -EFAULT; |
| 946 | if (copy_from_user(page, buf, count)) |
| 947 | goto out_free_page; |
| 948 | |
Al Viro | e018290 | 2006-05-18 08:28:02 -0400 | [diff] [blame] | 949 | page[count] = '\0'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | loginuid = simple_strtoul(page, &tmp, 10); |
| 951 | if (tmp == page) { |
| 952 | length = -EINVAL; |
| 953 | goto out_free_page; |
| 954 | |
| 955 | } |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 956 | length = audit_set_loginuid(current, loginuid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | if (likely(length == 0)) |
| 958 | length = count; |
| 959 | |
| 960 | out_free_page: |
| 961 | free_page((unsigned long) page); |
| 962 | return length; |
| 963 | } |
| 964 | |
| 965 | static struct file_operations proc_loginuid_operations = { |
| 966 | .read = proc_loginuid_read, |
| 967 | .write = proc_loginuid_write, |
| 968 | }; |
| 969 | #endif |
| 970 | |
| 971 | #ifdef CONFIG_SECCOMP |
| 972 | static ssize_t seccomp_read(struct file *file, char __user *buf, |
| 973 | size_t count, loff_t *ppos) |
| 974 | { |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 975 | struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 976 | char __buf[20]; |
| 977 | loff_t __ppos = *ppos; |
| 978 | size_t len; |
| 979 | |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 980 | if (!tsk) |
| 981 | return -ESRCH; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 982 | /* no need to print the trailing zero, so use only len */ |
| 983 | len = sprintf(__buf, "%u\n", tsk->seccomp.mode); |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 984 | put_task_struct(tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | if (__ppos >= len) |
| 986 | return 0; |
| 987 | if (count > len - __ppos) |
| 988 | count = len - __ppos; |
| 989 | if (copy_to_user(buf, __buf + __ppos, count)) |
| 990 | return -EFAULT; |
| 991 | *ppos = __ppos + count; |
| 992 | return count; |
| 993 | } |
| 994 | |
| 995 | static ssize_t seccomp_write(struct file *file, const char __user *buf, |
| 996 | size_t count, loff_t *ppos) |
| 997 | { |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 998 | struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | char __buf[20], *end; |
| 1000 | unsigned int seccomp_mode; |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 1001 | ssize_t result; |
| 1002 | |
| 1003 | result = -ESRCH; |
| 1004 | if (!tsk) |
| 1005 | goto out_no_task; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1006 | |
| 1007 | /* can set it only once to be even more secure */ |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 1008 | result = -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1009 | if (unlikely(tsk->seccomp.mode)) |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 1010 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1011 | |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 1012 | result = -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1013 | memset(__buf, 0, sizeof(__buf)); |
| 1014 | count = min(count, sizeof(__buf) - 1); |
| 1015 | if (copy_from_user(__buf, buf, count)) |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 1016 | goto out; |
| 1017 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1018 | seccomp_mode = simple_strtoul(__buf, &end, 0); |
| 1019 | if (*end == '\n') |
| 1020 | end++; |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 1021 | result = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1022 | if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) { |
| 1023 | tsk->seccomp.mode = seccomp_mode; |
| 1024 | set_tsk_thread_flag(tsk, TIF_SECCOMP); |
| 1025 | } else |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 1026 | goto out; |
| 1027 | result = -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | if (unlikely(!(end - __buf))) |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 1029 | goto out; |
| 1030 | result = end - __buf; |
| 1031 | out: |
| 1032 | put_task_struct(tsk); |
| 1033 | out_no_task: |
| 1034 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1035 | } |
| 1036 | |
| 1037 | static struct file_operations proc_seccomp_operations = { |
| 1038 | .read = seccomp_read, |
| 1039 | .write = seccomp_write, |
| 1040 | }; |
| 1041 | #endif /* CONFIG_SECCOMP */ |
| 1042 | |
Al Viro | 008b150 | 2005-08-20 00:17:39 +0100 | [diff] [blame] | 1043 | static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | { |
| 1045 | struct inode *inode = dentry->d_inode; |
| 1046 | int error = -EACCES; |
| 1047 | |
| 1048 | /* We don't need a base pointer in the /proc filesystem */ |
| 1049 | path_release(nd); |
| 1050 | |
Eric W. Biederman | 778c114 | 2006-06-26 00:25:58 -0700 | [diff] [blame] | 1051 | /* Are we allowed to snoop on the tasks file descriptors? */ |
| 1052 | if (!proc_fd_access_allowed(inode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1054 | |
| 1055 | error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt); |
| 1056 | nd->last_type = LAST_BIND; |
| 1057 | out: |
Al Viro | 008b150 | 2005-08-20 00:17:39 +0100 | [diff] [blame] | 1058 | return ERR_PTR(error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 | } |
| 1060 | |
| 1061 | static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt, |
| 1062 | char __user *buffer, int buflen) |
| 1063 | { |
| 1064 | struct inode * inode; |
| 1065 | char *tmp = (char*)__get_free_page(GFP_KERNEL), *path; |
| 1066 | int len; |
| 1067 | |
| 1068 | if (!tmp) |
| 1069 | return -ENOMEM; |
| 1070 | |
| 1071 | inode = dentry->d_inode; |
| 1072 | path = d_path(dentry, mnt, tmp, PAGE_SIZE); |
| 1073 | len = PTR_ERR(path); |
| 1074 | if (IS_ERR(path)) |
| 1075 | goto out; |
| 1076 | len = tmp + PAGE_SIZE - 1 - path; |
| 1077 | |
| 1078 | if (len > buflen) |
| 1079 | len = buflen; |
| 1080 | if (copy_to_user(buffer, path, len)) |
| 1081 | len = -EFAULT; |
| 1082 | out: |
| 1083 | free_page((unsigned long)tmp); |
| 1084 | return len; |
| 1085 | } |
| 1086 | |
| 1087 | static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen) |
| 1088 | { |
| 1089 | int error = -EACCES; |
| 1090 | struct inode *inode = dentry->d_inode; |
| 1091 | struct dentry *de; |
| 1092 | struct vfsmount *mnt = NULL; |
| 1093 | |
Eric W. Biederman | 778c114 | 2006-06-26 00:25:58 -0700 | [diff] [blame] | 1094 | /* Are we allowed to snoop on the tasks file descriptors? */ |
| 1095 | if (!proc_fd_access_allowed(inode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1096 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1097 | |
| 1098 | error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt); |
| 1099 | if (error) |
| 1100 | goto out; |
| 1101 | |
| 1102 | error = do_proc_readlink(de, mnt, buffer, buflen); |
| 1103 | dput(de); |
| 1104 | mntput(mnt); |
| 1105 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | return error; |
| 1107 | } |
| 1108 | |
| 1109 | static struct inode_operations proc_pid_link_inode_operations = { |
| 1110 | .readlink = proc_pid_readlink, |
| 1111 | .follow_link = proc_pid_follow_link |
| 1112 | }; |
| 1113 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1114 | static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir) |
| 1115 | { |
Eric W. Biederman | 5634708 | 2006-06-26 00:25:40 -0700 | [diff] [blame] | 1116 | struct dentry *dentry = filp->f_dentry; |
| 1117 | struct inode *inode = dentry->d_inode; |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 1118 | struct task_struct *p = get_proc_task(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1119 | unsigned int fd, tid, ino; |
| 1120 | int retval; |
Eric W. Biederman | 8578cea | 2006-06-26 00:25:54 -0700 | [diff] [blame] | 1121 | char buf[PROC_NUMBUF]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1122 | struct files_struct * files; |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 1123 | struct fdtable *fdt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1124 | |
| 1125 | retval = -ENOENT; |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 1126 | if (!p) |
| 1127 | goto out_no_task; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1128 | retval = 0; |
| 1129 | tid = p->pid; |
| 1130 | |
| 1131 | fd = filp->f_pos; |
| 1132 | switch (fd) { |
| 1133 | case 0: |
| 1134 | if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0) |
| 1135 | goto out; |
| 1136 | filp->f_pos++; |
| 1137 | case 1: |
Eric W. Biederman | 5634708 | 2006-06-26 00:25:40 -0700 | [diff] [blame] | 1138 | ino = parent_ino(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1139 | if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0) |
| 1140 | goto out; |
| 1141 | filp->f_pos++; |
| 1142 | default: |
| 1143 | files = get_files_struct(p); |
| 1144 | if (!files) |
| 1145 | goto out; |
Dipankar Sarma | b835996 | 2005-09-09 13:04:14 -0700 | [diff] [blame] | 1146 | rcu_read_lock(); |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 1147 | fdt = files_fdtable(files); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1148 | for (fd = filp->f_pos-2; |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 1149 | fd < fdt->max_fds; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1150 | fd++, filp->f_pos++) { |
| 1151 | unsigned int i,j; |
| 1152 | |
| 1153 | if (!fcheck_files(files, fd)) |
| 1154 | continue; |
Dipankar Sarma | b835996 | 2005-09-09 13:04:14 -0700 | [diff] [blame] | 1155 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1156 | |
Eric W. Biederman | 8578cea | 2006-06-26 00:25:54 -0700 | [diff] [blame] | 1157 | j = PROC_NUMBUF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1158 | i = fd; |
| 1159 | do { |
| 1160 | j--; |
| 1161 | buf[j] = '0' + (i % 10); |
| 1162 | i /= 10; |
| 1163 | } while (i); |
| 1164 | |
| 1165 | ino = fake_ino(tid, PROC_TID_FD_DIR + fd); |
Eric W. Biederman | 8578cea | 2006-06-26 00:25:54 -0700 | [diff] [blame] | 1166 | 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] | 1167 | rcu_read_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1168 | break; |
| 1169 | } |
Dipankar Sarma | b835996 | 2005-09-09 13:04:14 -0700 | [diff] [blame] | 1170 | rcu_read_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1171 | } |
Dipankar Sarma | b835996 | 2005-09-09 13:04:14 -0700 | [diff] [blame] | 1172 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1173 | put_files_struct(files); |
| 1174 | } |
| 1175 | out: |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 1176 | put_task_struct(p); |
| 1177 | out_no_task: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1178 | return retval; |
| 1179 | } |
| 1180 | |
| 1181 | static int proc_pident_readdir(struct file *filp, |
| 1182 | void *dirent, filldir_t filldir, |
| 1183 | struct pid_entry *ents, unsigned int nents) |
| 1184 | { |
| 1185 | int i; |
| 1186 | int pid; |
| 1187 | struct dentry *dentry = filp->f_dentry; |
| 1188 | struct inode *inode = dentry->d_inode; |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 1189 | struct task_struct *task = get_proc_task(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1190 | struct pid_entry *p; |
| 1191 | ino_t ino; |
| 1192 | int ret; |
| 1193 | |
| 1194 | ret = -ENOENT; |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 1195 | if (!task) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1196 | goto out; |
| 1197 | |
| 1198 | ret = 0; |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 1199 | pid = task->pid; |
| 1200 | put_task_struct(task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1201 | i = filp->f_pos; |
| 1202 | switch (i) { |
| 1203 | case 0: |
| 1204 | ino = inode->i_ino; |
| 1205 | if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0) |
| 1206 | goto out; |
| 1207 | i++; |
| 1208 | filp->f_pos++; |
| 1209 | /* fall through */ |
| 1210 | case 1: |
| 1211 | ino = parent_ino(dentry); |
| 1212 | if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0) |
| 1213 | goto out; |
| 1214 | i++; |
| 1215 | filp->f_pos++; |
| 1216 | /* fall through */ |
| 1217 | default: |
| 1218 | i -= 2; |
| 1219 | if (i >= nents) { |
| 1220 | ret = 1; |
| 1221 | goto out; |
| 1222 | } |
| 1223 | p = ents + i; |
| 1224 | while (p->name) { |
| 1225 | if (filldir(dirent, p->name, p->len, filp->f_pos, |
| 1226 | fake_ino(pid, p->type), p->mode >> 12) < 0) |
| 1227 | goto out; |
| 1228 | filp->f_pos++; |
| 1229 | p++; |
| 1230 | } |
| 1231 | } |
| 1232 | |
| 1233 | ret = 1; |
| 1234 | out: |
| 1235 | return ret; |
| 1236 | } |
| 1237 | |
| 1238 | static int proc_tgid_base_readdir(struct file * filp, |
| 1239 | void * dirent, filldir_t filldir) |
| 1240 | { |
| 1241 | return proc_pident_readdir(filp,dirent,filldir, |
| 1242 | tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff)); |
| 1243 | } |
| 1244 | |
| 1245 | static int proc_tid_base_readdir(struct file * filp, |
| 1246 | void * dirent, filldir_t filldir) |
| 1247 | { |
| 1248 | return proc_pident_readdir(filp,dirent,filldir, |
| 1249 | tid_base_stuff,ARRAY_SIZE(tid_base_stuff)); |
| 1250 | } |
| 1251 | |
| 1252 | /* building an inode */ |
| 1253 | |
| 1254 | static int task_dumpable(struct task_struct *task) |
| 1255 | { |
| 1256 | int dumpable = 0; |
| 1257 | struct mm_struct *mm; |
| 1258 | |
| 1259 | task_lock(task); |
| 1260 | mm = task->mm; |
| 1261 | if (mm) |
| 1262 | dumpable = mm->dumpable; |
| 1263 | task_unlock(task); |
Alan Cox | d6e7114 | 2005-06-23 00:09:43 -0700 | [diff] [blame] | 1264 | if(dumpable == 1) |
| 1265 | return 1; |
| 1266 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1267 | } |
| 1268 | |
| 1269 | |
| 1270 | static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task, int ino) |
| 1271 | { |
| 1272 | struct inode * inode; |
| 1273 | struct proc_inode *ei; |
| 1274 | |
| 1275 | /* We need a new inode */ |
| 1276 | |
| 1277 | inode = new_inode(sb); |
| 1278 | if (!inode) |
| 1279 | goto out; |
| 1280 | |
| 1281 | /* Common stuff */ |
| 1282 | ei = PROC_I(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1283 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
| 1284 | inode->i_ino = fake_ino(task->pid, ino); |
| 1285 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1286 | /* |
| 1287 | * grab the reference to task. |
| 1288 | */ |
Eric W. Biederman | 13b41b0 | 2006-06-26 00:25:56 -0700 | [diff] [blame] | 1289 | ei->pid = get_pid(task->pids[PIDTYPE_PID].pid); |
| 1290 | if (!ei->pid) |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 1291 | goto out_unlock; |
| 1292 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1293 | inode->i_uid = 0; |
| 1294 | inode->i_gid = 0; |
Eric W. Biederman | 87bfbf6 | 2006-06-26 00:25:43 -0700 | [diff] [blame] | 1295 | if (task_dumpable(task)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1296 | inode->i_uid = task->euid; |
| 1297 | inode->i_gid = task->egid; |
| 1298 | } |
| 1299 | security_task_to_inode(task, inode); |
| 1300 | |
| 1301 | out: |
| 1302 | return inode; |
| 1303 | |
| 1304 | out_unlock: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1305 | iput(inode); |
| 1306 | return NULL; |
| 1307 | } |
| 1308 | |
| 1309 | /* dentry stuff */ |
| 1310 | |
| 1311 | /* |
| 1312 | * Exceptional case: normally we are not allowed to unhash a busy |
| 1313 | * directory. In this case, however, we can do it - no aliasing problems |
| 1314 | * due to the way we treat inodes. |
| 1315 | * |
| 1316 | * Rewrite the inode's ownerships here because the owning task may have |
| 1317 | * performed a setuid(), etc. |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 1318 | * |
| 1319 | * Before the /proc/pid/status file was created the only way to read |
| 1320 | * the effective uid of a /process was to stat /proc/pid. Reading |
| 1321 | * /proc/pid/status is slow enough that procps and other packages |
| 1322 | * kept stating /proc/pid. To keep the rules in /proc simple I have |
| 1323 | * made this apply to all per process world readable and executable |
| 1324 | * directories. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1325 | */ |
| 1326 | static int pid_revalidate(struct dentry *dentry, struct nameidata *nd) |
| 1327 | { |
| 1328 | struct inode *inode = dentry->d_inode; |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 1329 | struct task_struct *task = get_proc_task(inode); |
| 1330 | if (task) { |
| 1331 | if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) || |
| 1332 | task_dumpable(task)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1333 | inode->i_uid = task->euid; |
| 1334 | inode->i_gid = task->egid; |
| 1335 | } else { |
| 1336 | inode->i_uid = 0; |
| 1337 | inode->i_gid = 0; |
| 1338 | } |
| 1339 | security_task_to_inode(task, inode); |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 1340 | put_task_struct(task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1341 | return 1; |
| 1342 | } |
| 1343 | d_drop(dentry); |
| 1344 | return 0; |
| 1345 | } |
| 1346 | |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 1347 | static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) |
| 1348 | { |
| 1349 | struct inode *inode = dentry->d_inode; |
| 1350 | struct task_struct *task; |
| 1351 | generic_fillattr(inode, stat); |
| 1352 | |
| 1353 | rcu_read_lock(); |
| 1354 | stat->uid = 0; |
| 1355 | stat->gid = 0; |
| 1356 | task = pid_task(proc_pid(inode), PIDTYPE_PID); |
| 1357 | if (task) { |
| 1358 | if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) || |
| 1359 | task_dumpable(task)) { |
| 1360 | stat->uid = task->euid; |
| 1361 | stat->gid = task->egid; |
| 1362 | } |
| 1363 | } |
| 1364 | rcu_read_unlock(); |
| 1365 | return 0; |
| 1366 | } |
| 1367 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1368 | static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd) |
| 1369 | { |
| 1370 | struct inode *inode = dentry->d_inode; |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 1371 | struct task_struct *task = get_proc_task(inode); |
Eric W. Biederman | aed7a6c | 2006-06-26 00:25:44 -0700 | [diff] [blame] | 1372 | int fd = proc_fd(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1373 | struct files_struct *files; |
| 1374 | |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 1375 | if (task) { |
| 1376 | files = get_files_struct(task); |
| 1377 | if (files) { |
| 1378 | rcu_read_lock(); |
| 1379 | if (fcheck_files(files, fd)) { |
| 1380 | rcu_read_unlock(); |
| 1381 | put_files_struct(files); |
| 1382 | if (task_dumpable(task)) { |
| 1383 | inode->i_uid = task->euid; |
| 1384 | inode->i_gid = task->egid; |
| 1385 | } else { |
| 1386 | inode->i_uid = 0; |
| 1387 | inode->i_gid = 0; |
| 1388 | } |
| 1389 | security_task_to_inode(task, inode); |
| 1390 | put_task_struct(task); |
| 1391 | return 1; |
| 1392 | } |
Dipankar Sarma | b835996 | 2005-09-09 13:04:14 -0700 | [diff] [blame] | 1393 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1394 | put_files_struct(files); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1395 | } |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 1396 | put_task_struct(task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1397 | } |
| 1398 | d_drop(dentry); |
| 1399 | return 0; |
| 1400 | } |
| 1401 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1402 | static int pid_delete_dentry(struct dentry * dentry) |
| 1403 | { |
| 1404 | /* Is the task we represent dead? |
| 1405 | * If so, then don't put the dentry on the lru list, |
| 1406 | * kill it immediately. |
| 1407 | */ |
Eric W. Biederman | 13b41b0 | 2006-06-26 00:25:56 -0700 | [diff] [blame] | 1408 | return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1409 | } |
| 1410 | |
| 1411 | static struct dentry_operations tid_fd_dentry_operations = |
| 1412 | { |
| 1413 | .d_revalidate = tid_fd_revalidate, |
| 1414 | .d_delete = pid_delete_dentry, |
| 1415 | }; |
| 1416 | |
| 1417 | static struct dentry_operations pid_dentry_operations = |
| 1418 | { |
| 1419 | .d_revalidate = pid_revalidate, |
| 1420 | .d_delete = pid_delete_dentry, |
| 1421 | }; |
| 1422 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1423 | /* Lookups */ |
| 1424 | |
| 1425 | static unsigned name_to_int(struct dentry *dentry) |
| 1426 | { |
| 1427 | const char *name = dentry->d_name.name; |
| 1428 | int len = dentry->d_name.len; |
| 1429 | unsigned n = 0; |
| 1430 | |
| 1431 | if (len > 1 && *name == '0') |
| 1432 | goto out; |
| 1433 | while (len-- > 0) { |
| 1434 | unsigned c = *name++ - '0'; |
| 1435 | if (c > 9) |
| 1436 | goto out; |
| 1437 | if (n >= (~0U-9)/10) |
| 1438 | goto out; |
| 1439 | n *= 10; |
| 1440 | n += c; |
| 1441 | } |
| 1442 | return n; |
| 1443 | out: |
| 1444 | return ~0U; |
| 1445 | } |
| 1446 | |
| 1447 | /* SMP-safe */ |
| 1448 | static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd) |
| 1449 | { |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 1450 | struct task_struct *task = get_proc_task(dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1451 | unsigned fd = name_to_int(dentry); |
Eric W. Biederman | cd6a3ce | 2006-06-26 00:25:49 -0700 | [diff] [blame] | 1452 | struct dentry *result = ERR_PTR(-ENOENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1453 | struct file * file; |
| 1454 | struct files_struct * files; |
| 1455 | struct inode *inode; |
| 1456 | struct proc_inode *ei; |
| 1457 | |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 1458 | if (!task) |
| 1459 | goto out_no_task; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1460 | if (fd == ~0U) |
| 1461 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1462 | |
| 1463 | inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_FD_DIR+fd); |
| 1464 | if (!inode) |
| 1465 | goto out; |
| 1466 | ei = PROC_I(inode); |
Eric W. Biederman | aed7a6c | 2006-06-26 00:25:44 -0700 | [diff] [blame] | 1467 | ei->fd = fd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1468 | files = get_files_struct(task); |
| 1469 | if (!files) |
| 1470 | goto out_unlock; |
| 1471 | inode->i_mode = S_IFLNK; |
Dipankar Sarma | ca99c1d | 2006-04-18 22:21:46 -0700 | [diff] [blame] | 1472 | |
| 1473 | /* |
| 1474 | * We are not taking a ref to the file structure, so we must |
| 1475 | * hold ->file_lock. |
| 1476 | */ |
| 1477 | spin_lock(&files->file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1478 | file = fcheck_files(files, fd); |
| 1479 | if (!file) |
| 1480 | goto out_unlock2; |
| 1481 | if (file->f_mode & 1) |
| 1482 | inode->i_mode |= S_IRUSR | S_IXUSR; |
| 1483 | if (file->f_mode & 2) |
| 1484 | inode->i_mode |= S_IWUSR | S_IXUSR; |
Dipankar Sarma | ca99c1d | 2006-04-18 22:21:46 -0700 | [diff] [blame] | 1485 | spin_unlock(&files->file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1486 | put_files_struct(files); |
| 1487 | inode->i_op = &proc_pid_link_inode_operations; |
| 1488 | inode->i_size = 64; |
| 1489 | ei->op.proc_get_link = proc_fd_link; |
| 1490 | dentry->d_op = &tid_fd_dentry_operations; |
| 1491 | d_add(dentry, inode); |
Eric W. Biederman | cd6a3ce | 2006-06-26 00:25:49 -0700 | [diff] [blame] | 1492 | /* Close the race of the process dying before we return the dentry */ |
| 1493 | if (tid_fd_revalidate(dentry, NULL)) |
| 1494 | result = NULL; |
| 1495 | out: |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 1496 | put_task_struct(task); |
| 1497 | out_no_task: |
Eric W. Biederman | cd6a3ce | 2006-06-26 00:25:49 -0700 | [diff] [blame] | 1498 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1499 | |
| 1500 | out_unlock2: |
Dipankar Sarma | ca99c1d | 2006-04-18 22:21:46 -0700 | [diff] [blame] | 1501 | spin_unlock(&files->file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1502 | put_files_struct(files); |
| 1503 | out_unlock: |
| 1504 | iput(inode); |
Eric W. Biederman | cd6a3ce | 2006-06-26 00:25:49 -0700 | [diff] [blame] | 1505 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1506 | } |
| 1507 | |
| 1508 | static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir); |
| 1509 | 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] | 1510 | 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] | 1511 | |
| 1512 | static struct file_operations proc_fd_operations = { |
| 1513 | .read = generic_read_dir, |
| 1514 | .readdir = proc_readfd, |
| 1515 | }; |
| 1516 | |
| 1517 | static struct file_operations proc_task_operations = { |
| 1518 | .read = generic_read_dir, |
| 1519 | .readdir = proc_task_readdir, |
| 1520 | }; |
| 1521 | |
| 1522 | /* |
| 1523 | * proc directories can do almost nothing.. |
| 1524 | */ |
| 1525 | static struct inode_operations proc_fd_inode_operations = { |
| 1526 | .lookup = proc_lookupfd, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1527 | }; |
| 1528 | |
| 1529 | static struct inode_operations proc_task_inode_operations = { |
| 1530 | .lookup = proc_task_lookup, |
Eric W. Biederman | 6e66b52 | 2006-06-26 00:25:47 -0700 | [diff] [blame] | 1531 | .getattr = proc_task_getattr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1532 | }; |
| 1533 | |
| 1534 | #ifdef CONFIG_SECURITY |
| 1535 | static ssize_t proc_pid_attr_read(struct file * file, char __user * buf, |
| 1536 | size_t count, loff_t *ppos) |
| 1537 | { |
| 1538 | struct inode * inode = file->f_dentry->d_inode; |
| 1539 | unsigned long page; |
| 1540 | ssize_t length; |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 1541 | struct task_struct *task = get_proc_task(inode); |
| 1542 | |
| 1543 | length = -ESRCH; |
| 1544 | if (!task) |
| 1545 | goto out_no_task; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1546 | |
| 1547 | if (count > PAGE_SIZE) |
| 1548 | count = PAGE_SIZE; |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 1549 | length = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1550 | if (!(page = __get_free_page(GFP_KERNEL))) |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 1551 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1552 | |
| 1553 | length = security_getprocattr(task, |
| 1554 | (char*)file->f_dentry->d_name.name, |
| 1555 | (void*)page, count); |
| 1556 | if (length >= 0) |
| 1557 | length = simple_read_from_buffer(buf, count, ppos, (char *)page, length); |
| 1558 | free_page(page); |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 1559 | out: |
| 1560 | put_task_struct(task); |
| 1561 | out_no_task: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1562 | return length; |
| 1563 | } |
| 1564 | |
| 1565 | static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf, |
| 1566 | size_t count, loff_t *ppos) |
| 1567 | { |
| 1568 | struct inode * inode = file->f_dentry->d_inode; |
| 1569 | char *page; |
| 1570 | ssize_t length; |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 1571 | struct task_struct *task = get_proc_task(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1572 | |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 1573 | length = -ESRCH; |
| 1574 | if (!task) |
| 1575 | goto out_no_task; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1576 | if (count > PAGE_SIZE) |
| 1577 | count = PAGE_SIZE; |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 1578 | |
| 1579 | /* No partial writes. */ |
| 1580 | length = -EINVAL; |
| 1581 | if (*ppos != 0) |
| 1582 | goto out; |
| 1583 | |
| 1584 | length = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1585 | page = (char*)__get_free_page(GFP_USER); |
| 1586 | if (!page) |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 1587 | goto out; |
| 1588 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1589 | length = -EFAULT; |
| 1590 | if (copy_from_user(page, buf, count)) |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 1591 | goto out_free; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1592 | |
| 1593 | length = security_setprocattr(task, |
| 1594 | (char*)file->f_dentry->d_name.name, |
| 1595 | (void*)page, count); |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 1596 | out_free: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1597 | free_page((unsigned long) page); |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 1598 | out: |
| 1599 | put_task_struct(task); |
| 1600 | out_no_task: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1601 | return length; |
| 1602 | } |
| 1603 | |
| 1604 | static struct file_operations proc_pid_attr_operations = { |
| 1605 | .read = proc_pid_attr_read, |
| 1606 | .write = proc_pid_attr_write, |
| 1607 | }; |
| 1608 | |
| 1609 | static struct file_operations proc_tid_attr_operations; |
| 1610 | static struct inode_operations proc_tid_attr_inode_operations; |
| 1611 | static struct file_operations proc_tgid_attr_operations; |
| 1612 | static struct inode_operations proc_tgid_attr_inode_operations; |
| 1613 | #endif |
| 1614 | |
| 1615 | /* SMP-safe */ |
| 1616 | static struct dentry *proc_pident_lookup(struct inode *dir, |
| 1617 | struct dentry *dentry, |
| 1618 | struct pid_entry *ents) |
| 1619 | { |
| 1620 | struct inode *inode; |
Eric W. Biederman | cd6a3ce | 2006-06-26 00:25:49 -0700 | [diff] [blame] | 1621 | struct dentry *error; |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 1622 | struct task_struct *task = get_proc_task(dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1623 | struct pid_entry *p; |
| 1624 | struct proc_inode *ei; |
| 1625 | |
Eric W. Biederman | cd6a3ce | 2006-06-26 00:25:49 -0700 | [diff] [blame] | 1626 | error = ERR_PTR(-ENOENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1627 | inode = NULL; |
| 1628 | |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 1629 | if (!task) |
| 1630 | goto out_no_task; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1631 | |
| 1632 | for (p = ents; p->name; p++) { |
| 1633 | if (p->len != dentry->d_name.len) |
| 1634 | continue; |
| 1635 | if (!memcmp(dentry->d_name.name, p->name, p->len)) |
| 1636 | break; |
| 1637 | } |
| 1638 | if (!p->name) |
| 1639 | goto out; |
| 1640 | |
Eric W. Biederman | cd6a3ce | 2006-06-26 00:25:49 -0700 | [diff] [blame] | 1641 | error = ERR_PTR(-EINVAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1642 | inode = proc_pid_make_inode(dir->i_sb, task, p->type); |
| 1643 | if (!inode) |
| 1644 | goto out; |
| 1645 | |
| 1646 | ei = PROC_I(inode); |
| 1647 | inode->i_mode = p->mode; |
| 1648 | /* |
| 1649 | * Yes, it does not scale. And it should not. Don't add |
| 1650 | * new entries into /proc/<tgid>/ without very good reasons. |
| 1651 | */ |
| 1652 | switch(p->type) { |
| 1653 | case PROC_TGID_TASK: |
Eric W. Biederman | 6e66b52 | 2006-06-26 00:25:47 -0700 | [diff] [blame] | 1654 | inode->i_nlink = 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1655 | inode->i_op = &proc_task_inode_operations; |
| 1656 | inode->i_fop = &proc_task_operations; |
| 1657 | break; |
| 1658 | case PROC_TID_FD: |
| 1659 | case PROC_TGID_FD: |
| 1660 | inode->i_nlink = 2; |
| 1661 | inode->i_op = &proc_fd_inode_operations; |
| 1662 | inode->i_fop = &proc_fd_operations; |
| 1663 | break; |
| 1664 | case PROC_TID_EXE: |
| 1665 | case PROC_TGID_EXE: |
| 1666 | inode->i_op = &proc_pid_link_inode_operations; |
| 1667 | ei->op.proc_get_link = proc_exe_link; |
| 1668 | break; |
| 1669 | case PROC_TID_CWD: |
| 1670 | case PROC_TGID_CWD: |
| 1671 | inode->i_op = &proc_pid_link_inode_operations; |
| 1672 | ei->op.proc_get_link = proc_cwd_link; |
| 1673 | break; |
| 1674 | case PROC_TID_ROOT: |
| 1675 | case PROC_TGID_ROOT: |
| 1676 | inode->i_op = &proc_pid_link_inode_operations; |
| 1677 | ei->op.proc_get_link = proc_root_link; |
| 1678 | break; |
| 1679 | case PROC_TID_ENVIRON: |
| 1680 | case PROC_TGID_ENVIRON: |
| 1681 | inode->i_fop = &proc_info_file_operations; |
| 1682 | ei->op.proc_read = proc_pid_environ; |
| 1683 | break; |
| 1684 | case PROC_TID_AUXV: |
| 1685 | case PROC_TGID_AUXV: |
| 1686 | inode->i_fop = &proc_info_file_operations; |
| 1687 | ei->op.proc_read = proc_pid_auxv; |
| 1688 | break; |
| 1689 | case PROC_TID_STATUS: |
| 1690 | case PROC_TGID_STATUS: |
| 1691 | inode->i_fop = &proc_info_file_operations; |
| 1692 | ei->op.proc_read = proc_pid_status; |
| 1693 | break; |
| 1694 | case PROC_TID_STAT: |
| 1695 | inode->i_fop = &proc_info_file_operations; |
| 1696 | ei->op.proc_read = proc_tid_stat; |
| 1697 | break; |
| 1698 | case PROC_TGID_STAT: |
| 1699 | inode->i_fop = &proc_info_file_operations; |
| 1700 | ei->op.proc_read = proc_tgid_stat; |
| 1701 | break; |
| 1702 | case PROC_TID_CMDLINE: |
| 1703 | case PROC_TGID_CMDLINE: |
| 1704 | inode->i_fop = &proc_info_file_operations; |
| 1705 | ei->op.proc_read = proc_pid_cmdline; |
| 1706 | break; |
| 1707 | case PROC_TID_STATM: |
| 1708 | case PROC_TGID_STATM: |
| 1709 | inode->i_fop = &proc_info_file_operations; |
| 1710 | ei->op.proc_read = proc_pid_statm; |
| 1711 | break; |
| 1712 | case PROC_TID_MAPS: |
| 1713 | case PROC_TGID_MAPS: |
| 1714 | inode->i_fop = &proc_maps_operations; |
| 1715 | break; |
Christoph Lameter | 6e21c8f | 2005-09-03 15:54:45 -0700 | [diff] [blame] | 1716 | #ifdef CONFIG_NUMA |
| 1717 | case PROC_TID_NUMA_MAPS: |
| 1718 | case PROC_TGID_NUMA_MAPS: |
| 1719 | inode->i_fop = &proc_numa_maps_operations; |
| 1720 | break; |
| 1721 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1722 | case PROC_TID_MEM: |
| 1723 | case PROC_TGID_MEM: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1724 | inode->i_fop = &proc_mem_operations; |
| 1725 | break; |
| 1726 | #ifdef CONFIG_SECCOMP |
| 1727 | case PROC_TID_SECCOMP: |
| 1728 | case PROC_TGID_SECCOMP: |
| 1729 | inode->i_fop = &proc_seccomp_operations; |
| 1730 | break; |
| 1731 | #endif /* CONFIG_SECCOMP */ |
| 1732 | case PROC_TID_MOUNTS: |
| 1733 | case PROC_TGID_MOUNTS: |
| 1734 | inode->i_fop = &proc_mounts_operations; |
| 1735 | break; |
Yoshinori Sato | 63c6764 | 2005-10-14 15:59:11 -0700 | [diff] [blame] | 1736 | #ifdef CONFIG_MMU |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 1737 | case PROC_TID_SMAPS: |
| 1738 | case PROC_TGID_SMAPS: |
| 1739 | inode->i_fop = &proc_smaps_operations; |
| 1740 | break; |
Yoshinori Sato | 63c6764 | 2005-10-14 15:59:11 -0700 | [diff] [blame] | 1741 | #endif |
Chuck Lever | b4629fe | 2006-03-20 13:44:12 -0500 | [diff] [blame] | 1742 | case PROC_TID_MOUNTSTATS: |
| 1743 | case PROC_TGID_MOUNTSTATS: |
| 1744 | inode->i_fop = &proc_mountstats_operations; |
| 1745 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1746 | #ifdef CONFIG_SECURITY |
| 1747 | case PROC_TID_ATTR: |
| 1748 | inode->i_nlink = 2; |
| 1749 | inode->i_op = &proc_tid_attr_inode_operations; |
| 1750 | inode->i_fop = &proc_tid_attr_operations; |
| 1751 | break; |
| 1752 | case PROC_TGID_ATTR: |
| 1753 | inode->i_nlink = 2; |
| 1754 | inode->i_op = &proc_tgid_attr_inode_operations; |
| 1755 | inode->i_fop = &proc_tgid_attr_operations; |
| 1756 | break; |
| 1757 | case PROC_TID_ATTR_CURRENT: |
| 1758 | case PROC_TGID_ATTR_CURRENT: |
| 1759 | case PROC_TID_ATTR_PREV: |
| 1760 | case PROC_TGID_ATTR_PREV: |
| 1761 | case PROC_TID_ATTR_EXEC: |
| 1762 | case PROC_TGID_ATTR_EXEC: |
| 1763 | case PROC_TID_ATTR_FSCREATE: |
| 1764 | case PROC_TGID_ATTR_FSCREATE: |
Michael LeMay | 4eb582c | 2006-06-26 00:24:57 -0700 | [diff] [blame] | 1765 | case PROC_TID_ATTR_KEYCREATE: |
| 1766 | case PROC_TGID_ATTR_KEYCREATE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1767 | inode->i_fop = &proc_pid_attr_operations; |
| 1768 | break; |
| 1769 | #endif |
| 1770 | #ifdef CONFIG_KALLSYMS |
| 1771 | case PROC_TID_WCHAN: |
| 1772 | case PROC_TGID_WCHAN: |
| 1773 | inode->i_fop = &proc_info_file_operations; |
| 1774 | ei->op.proc_read = proc_pid_wchan; |
| 1775 | break; |
| 1776 | #endif |
| 1777 | #ifdef CONFIG_SCHEDSTATS |
| 1778 | case PROC_TID_SCHEDSTAT: |
| 1779 | case PROC_TGID_SCHEDSTAT: |
| 1780 | inode->i_fop = &proc_info_file_operations; |
| 1781 | ei->op.proc_read = proc_pid_schedstat; |
| 1782 | break; |
| 1783 | #endif |
| 1784 | #ifdef CONFIG_CPUSETS |
| 1785 | case PROC_TID_CPUSET: |
| 1786 | case PROC_TGID_CPUSET: |
| 1787 | inode->i_fop = &proc_cpuset_operations; |
| 1788 | break; |
| 1789 | #endif |
| 1790 | case PROC_TID_OOM_SCORE: |
| 1791 | case PROC_TGID_OOM_SCORE: |
| 1792 | inode->i_fop = &proc_info_file_operations; |
| 1793 | ei->op.proc_read = proc_oom_score; |
| 1794 | break; |
| 1795 | case PROC_TID_OOM_ADJUST: |
| 1796 | case PROC_TGID_OOM_ADJUST: |
| 1797 | inode->i_fop = &proc_oom_adjust_operations; |
| 1798 | break; |
| 1799 | #ifdef CONFIG_AUDITSYSCALL |
| 1800 | case PROC_TID_LOGINUID: |
| 1801 | case PROC_TGID_LOGINUID: |
| 1802 | inode->i_fop = &proc_loginuid_operations; |
| 1803 | break; |
| 1804 | #endif |
| 1805 | default: |
| 1806 | printk("procfs: impossible type (%d)",p->type); |
| 1807 | iput(inode); |
Eric W. Biederman | cd6a3ce | 2006-06-26 00:25:49 -0700 | [diff] [blame] | 1808 | error = ERR_PTR(-EINVAL); |
| 1809 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1810 | } |
| 1811 | dentry->d_op = &pid_dentry_operations; |
| 1812 | d_add(dentry, inode); |
Eric W. Biederman | cd6a3ce | 2006-06-26 00:25:49 -0700 | [diff] [blame] | 1813 | /* Close the race of the process dying before we return the dentry */ |
| 1814 | if (pid_revalidate(dentry, NULL)) |
| 1815 | error = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1816 | out: |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 1817 | put_task_struct(task); |
| 1818 | out_no_task: |
Eric W. Biederman | cd6a3ce | 2006-06-26 00:25:49 -0700 | [diff] [blame] | 1819 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1820 | } |
| 1821 | |
| 1822 | static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){ |
| 1823 | return proc_pident_lookup(dir, dentry, tgid_base_stuff); |
| 1824 | } |
| 1825 | |
| 1826 | static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){ |
| 1827 | return proc_pident_lookup(dir, dentry, tid_base_stuff); |
| 1828 | } |
| 1829 | |
| 1830 | static struct file_operations proc_tgid_base_operations = { |
| 1831 | .read = generic_read_dir, |
| 1832 | .readdir = proc_tgid_base_readdir, |
| 1833 | }; |
| 1834 | |
| 1835 | static struct file_operations proc_tid_base_operations = { |
| 1836 | .read = generic_read_dir, |
| 1837 | .readdir = proc_tid_base_readdir, |
| 1838 | }; |
| 1839 | |
| 1840 | static struct inode_operations proc_tgid_base_inode_operations = { |
| 1841 | .lookup = proc_tgid_base_lookup, |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 1842 | .getattr = pid_getattr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1843 | }; |
| 1844 | |
| 1845 | static struct inode_operations proc_tid_base_inode_operations = { |
| 1846 | .lookup = proc_tid_base_lookup, |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 1847 | .getattr = pid_getattr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1848 | }; |
| 1849 | |
| 1850 | #ifdef CONFIG_SECURITY |
| 1851 | static int proc_tgid_attr_readdir(struct file * filp, |
| 1852 | void * dirent, filldir_t filldir) |
| 1853 | { |
| 1854 | return proc_pident_readdir(filp,dirent,filldir, |
| 1855 | tgid_attr_stuff,ARRAY_SIZE(tgid_attr_stuff)); |
| 1856 | } |
| 1857 | |
| 1858 | static int proc_tid_attr_readdir(struct file * filp, |
| 1859 | void * dirent, filldir_t filldir) |
| 1860 | { |
| 1861 | return proc_pident_readdir(filp,dirent,filldir, |
| 1862 | tid_attr_stuff,ARRAY_SIZE(tid_attr_stuff)); |
| 1863 | } |
| 1864 | |
| 1865 | static struct file_operations proc_tgid_attr_operations = { |
| 1866 | .read = generic_read_dir, |
| 1867 | .readdir = proc_tgid_attr_readdir, |
| 1868 | }; |
| 1869 | |
| 1870 | static struct file_operations proc_tid_attr_operations = { |
| 1871 | .read = generic_read_dir, |
| 1872 | .readdir = proc_tid_attr_readdir, |
| 1873 | }; |
| 1874 | |
| 1875 | static struct dentry *proc_tgid_attr_lookup(struct inode *dir, |
| 1876 | struct dentry *dentry, struct nameidata *nd) |
| 1877 | { |
| 1878 | return proc_pident_lookup(dir, dentry, tgid_attr_stuff); |
| 1879 | } |
| 1880 | |
| 1881 | static struct dentry *proc_tid_attr_lookup(struct inode *dir, |
| 1882 | struct dentry *dentry, struct nameidata *nd) |
| 1883 | { |
| 1884 | return proc_pident_lookup(dir, dentry, tid_attr_stuff); |
| 1885 | } |
| 1886 | |
| 1887 | static struct inode_operations proc_tgid_attr_inode_operations = { |
| 1888 | .lookup = proc_tgid_attr_lookup, |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 1889 | .getattr = pid_getattr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1890 | }; |
| 1891 | |
| 1892 | static struct inode_operations proc_tid_attr_inode_operations = { |
| 1893 | .lookup = proc_tid_attr_lookup, |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 1894 | .getattr = pid_getattr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1895 | }; |
| 1896 | #endif |
| 1897 | |
| 1898 | /* |
| 1899 | * /proc/self: |
| 1900 | */ |
| 1901 | static int proc_self_readlink(struct dentry *dentry, char __user *buffer, |
| 1902 | int buflen) |
| 1903 | { |
Eric W. Biederman | 8578cea | 2006-06-26 00:25:54 -0700 | [diff] [blame] | 1904 | char tmp[PROC_NUMBUF]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1905 | sprintf(tmp, "%d", current->tgid); |
| 1906 | return vfs_readlink(dentry,buffer,buflen,tmp); |
| 1907 | } |
| 1908 | |
Al Viro | 008b150 | 2005-08-20 00:17:39 +0100 | [diff] [blame] | 1909 | static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1910 | { |
Eric W. Biederman | 8578cea | 2006-06-26 00:25:54 -0700 | [diff] [blame] | 1911 | char tmp[PROC_NUMBUF]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1912 | sprintf(tmp, "%d", current->tgid); |
Al Viro | 008b150 | 2005-08-20 00:17:39 +0100 | [diff] [blame] | 1913 | return ERR_PTR(vfs_follow_link(nd,tmp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1914 | } |
| 1915 | |
| 1916 | static struct inode_operations proc_self_inode_operations = { |
| 1917 | .readlink = proc_self_readlink, |
| 1918 | .follow_link = proc_self_follow_link, |
| 1919 | }; |
| 1920 | |
| 1921 | /** |
Eric W. Biederman | 48e6484 | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 1922 | * proc_flush_task - Remove dcache entries for @task from the /proc dcache. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1923 | * |
Eric W. Biederman | 48e6484 | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 1924 | * @task: task that should be flushed. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1925 | * |
Eric W. Biederman | 48e6484 | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 1926 | * Looks in the dcache for |
| 1927 | * /proc/@pid |
| 1928 | * /proc/@tgid/task/@pid |
| 1929 | * if either directory is present flushes it and all of it'ts children |
| 1930 | * from the dcache. |
| 1931 | * |
| 1932 | * It is safe and reasonable to cache /proc entries for a task until |
| 1933 | * that task exits. After that they just clog up the dcache with |
| 1934 | * useless entries, possibly causing useful dcache entries to be |
| 1935 | * flushed instead. This routine is proved to flush those useless |
| 1936 | * dcache entries at process exit time. |
| 1937 | * |
| 1938 | * NOTE: This routine is just an optimization so it does not guarantee |
| 1939 | * that no dcache entries will exist at process exit time it |
| 1940 | * just makes it very unlikely that any will persist. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1941 | */ |
Eric W. Biederman | 48e6484 | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 1942 | void proc_flush_task(struct task_struct *task) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1943 | { |
Eric W. Biederman | 48e6484 | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 1944 | struct dentry *dentry, *leader, *dir; |
Eric W. Biederman | 8578cea | 2006-06-26 00:25:54 -0700 | [diff] [blame] | 1945 | char buf[PROC_NUMBUF]; |
Eric W. Biederman | 48e6484 | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 1946 | struct qstr name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1947 | |
Eric W. Biederman | 48e6484 | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 1948 | name.name = buf; |
| 1949 | name.len = snprintf(buf, sizeof(buf), "%d", task->pid); |
| 1950 | dentry = d_hash_and_lookup(proc_mnt->mnt_root, &name); |
| 1951 | if (dentry) { |
| 1952 | shrink_dcache_parent(dentry); |
| 1953 | d_drop(dentry); |
| 1954 | dput(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1955 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1956 | |
Eric W. Biederman | 48e6484 | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 1957 | if (thread_group_leader(task)) |
| 1958 | goto out; |
| 1959 | |
| 1960 | name.name = buf; |
| 1961 | name.len = snprintf(buf, sizeof(buf), "%d", task->tgid); |
| 1962 | leader = d_hash_and_lookup(proc_mnt->mnt_root, &name); |
| 1963 | if (!leader) |
| 1964 | goto out; |
| 1965 | |
| 1966 | name.name = "task"; |
| 1967 | name.len = strlen(name.name); |
| 1968 | dir = d_hash_and_lookup(leader, &name); |
| 1969 | if (!dir) |
| 1970 | goto out_put_leader; |
| 1971 | |
| 1972 | name.name = buf; |
| 1973 | name.len = snprintf(buf, sizeof(buf), "%d", task->pid); |
| 1974 | dentry = d_hash_and_lookup(dir, &name); |
| 1975 | if (dentry) { |
| 1976 | shrink_dcache_parent(dentry); |
| 1977 | d_drop(dentry); |
| 1978 | dput(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1979 | } |
Eric W. Biederman | 48e6484 | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 1980 | |
| 1981 | dput(dir); |
| 1982 | out_put_leader: |
| 1983 | dput(leader); |
| 1984 | out: |
| 1985 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1986 | } |
| 1987 | |
| 1988 | /* SMP-safe */ |
| 1989 | struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd) |
| 1990 | { |
Eric W. Biederman | cd6a3ce | 2006-06-26 00:25:49 -0700 | [diff] [blame] | 1991 | struct dentry *result = ERR_PTR(-ENOENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1992 | struct task_struct *task; |
| 1993 | struct inode *inode; |
| 1994 | struct proc_inode *ei; |
| 1995 | unsigned tgid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1996 | |
| 1997 | if (dentry->d_name.len == 4 && !memcmp(dentry->d_name.name,"self",4)) { |
| 1998 | inode = new_inode(dir->i_sb); |
| 1999 | if (!inode) |
| 2000 | return ERR_PTR(-ENOMEM); |
| 2001 | ei = PROC_I(inode); |
| 2002 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
| 2003 | inode->i_ino = fake_ino(0, PROC_TGID_INO); |
| 2004 | ei->pde = NULL; |
| 2005 | inode->i_mode = S_IFLNK|S_IRWXUGO; |
| 2006 | inode->i_uid = inode->i_gid = 0; |
| 2007 | inode->i_size = 64; |
| 2008 | inode->i_op = &proc_self_inode_operations; |
| 2009 | d_add(dentry, inode); |
| 2010 | return NULL; |
| 2011 | } |
| 2012 | tgid = name_to_int(dentry); |
| 2013 | if (tgid == ~0U) |
| 2014 | goto out; |
| 2015 | |
Eric W. Biederman | de75873 | 2006-06-26 00:25:51 -0700 | [diff] [blame] | 2016 | rcu_read_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2017 | task = find_task_by_pid(tgid); |
| 2018 | if (task) |
| 2019 | get_task_struct(task); |
Eric W. Biederman | de75873 | 2006-06-26 00:25:51 -0700 | [diff] [blame] | 2020 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2021 | if (!task) |
| 2022 | goto out; |
| 2023 | |
| 2024 | 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] | 2025 | if (!inode) |
| 2026 | goto out_put_task; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2027 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2028 | inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO; |
| 2029 | inode->i_op = &proc_tgid_base_inode_operations; |
| 2030 | inode->i_fop = &proc_tgid_base_operations; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2031 | inode->i_flags|=S_IMMUTABLE; |
Daniel Drake | bcf88e1 | 2005-05-01 08:59:03 -0700 | [diff] [blame] | 2032 | #ifdef CONFIG_SECURITY |
| 2033 | inode->i_nlink = 5; |
| 2034 | #else |
| 2035 | inode->i_nlink = 4; |
| 2036 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2037 | |
Eric W. Biederman | 48e6484 | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 2038 | dentry->d_op = &pid_dentry_operations; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2039 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2040 | d_add(dentry, inode); |
Eric W. Biederman | cd6a3ce | 2006-06-26 00:25:49 -0700 | [diff] [blame] | 2041 | /* Close the race of the process dying before we return the dentry */ |
| 2042 | if (pid_revalidate(dentry, NULL)) |
| 2043 | result = NULL; |
Eric W. Biederman | 48e6484 | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 2044 | |
Eric W. Biederman | cd6a3ce | 2006-06-26 00:25:49 -0700 | [diff] [blame] | 2045 | out_put_task: |
Eric W. Biederman | 48e6484 | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 2046 | put_task_struct(task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2047 | out: |
Eric W. Biederman | cd6a3ce | 2006-06-26 00:25:49 -0700 | [diff] [blame] | 2048 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2049 | } |
| 2050 | |
| 2051 | /* SMP-safe */ |
| 2052 | static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd) |
| 2053 | { |
Eric W. Biederman | cd6a3ce | 2006-06-26 00:25:49 -0700 | [diff] [blame] | 2054 | struct dentry *result = ERR_PTR(-ENOENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2055 | struct task_struct *task; |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 2056 | struct task_struct *leader = get_proc_task(dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2057 | struct inode *inode; |
| 2058 | unsigned tid; |
| 2059 | |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 2060 | if (!leader) |
| 2061 | goto out_no_task; |
| 2062 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2063 | tid = name_to_int(dentry); |
| 2064 | if (tid == ~0U) |
| 2065 | goto out; |
| 2066 | |
Eric W. Biederman | de75873 | 2006-06-26 00:25:51 -0700 | [diff] [blame] | 2067 | rcu_read_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2068 | task = find_task_by_pid(tid); |
| 2069 | if (task) |
| 2070 | get_task_struct(task); |
Eric W. Biederman | de75873 | 2006-06-26 00:25:51 -0700 | [diff] [blame] | 2071 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2072 | if (!task) |
| 2073 | goto out; |
| 2074 | if (leader->tgid != task->tgid) |
| 2075 | goto out_drop_task; |
| 2076 | |
| 2077 | inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_INO); |
| 2078 | |
| 2079 | |
| 2080 | if (!inode) |
| 2081 | goto out_drop_task; |
| 2082 | inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO; |
| 2083 | inode->i_op = &proc_tid_base_inode_operations; |
| 2084 | inode->i_fop = &proc_tid_base_operations; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2085 | inode->i_flags|=S_IMMUTABLE; |
Daniel Drake | bcf88e1 | 2005-05-01 08:59:03 -0700 | [diff] [blame] | 2086 | #ifdef CONFIG_SECURITY |
| 2087 | inode->i_nlink = 4; |
| 2088 | #else |
| 2089 | inode->i_nlink = 3; |
| 2090 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2091 | |
Eric W. Biederman | 48e6484 | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 2092 | dentry->d_op = &pid_dentry_operations; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2093 | |
| 2094 | d_add(dentry, inode); |
Eric W. Biederman | cd6a3ce | 2006-06-26 00:25:49 -0700 | [diff] [blame] | 2095 | /* Close the race of the process dying before we return the dentry */ |
| 2096 | if (pid_revalidate(dentry, NULL)) |
| 2097 | result = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2098 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2099 | out_drop_task: |
| 2100 | put_task_struct(task); |
| 2101 | out: |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 2102 | put_task_struct(leader); |
| 2103 | out_no_task: |
Eric W. Biederman | cd6a3ce | 2006-06-26 00:25:49 -0700 | [diff] [blame] | 2104 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2105 | } |
| 2106 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2107 | /* |
Eric W. Biederman | 0bc58a9 | 2006-06-26 00:25:50 -0700 | [diff] [blame] | 2108 | * Find the first tgid to return to user space. |
| 2109 | * |
| 2110 | * Usually this is just whatever follows &init_task, but if the users |
| 2111 | * buffer was too small to hold the full list or there was a seek into |
| 2112 | * the middle of the directory we have more work to do. |
| 2113 | * |
| 2114 | * In the case of a short read we start with find_task_by_pid. |
| 2115 | * |
| 2116 | * In the case of a seek we start with &init_task and walk nr |
| 2117 | * threads past it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2118 | */ |
Eric W. Biederman | 9cc8cbc | 2006-06-26 00:25:52 -0700 | [diff] [blame] | 2119 | static struct task_struct *first_tgid(int tgid, unsigned int nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2120 | { |
Eric W. Biederman | 9cc8cbc | 2006-06-26 00:25:52 -0700 | [diff] [blame] | 2121 | struct task_struct *pos; |
Eric W. Biederman | 454cc10 | 2006-06-26 00:25:51 -0700 | [diff] [blame] | 2122 | rcu_read_lock(); |
Eric W. Biederman | 0bc58a9 | 2006-06-26 00:25:50 -0700 | [diff] [blame] | 2123 | if (tgid && nr) { |
| 2124 | pos = find_task_by_pid(tgid); |
Eric W. Biederman | 9cc8cbc | 2006-06-26 00:25:52 -0700 | [diff] [blame] | 2125 | if (pos && thread_group_leader(pos)) |
| 2126 | goto found; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2127 | } |
Eric W. Biederman | 0bc58a9 | 2006-06-26 00:25:50 -0700 | [diff] [blame] | 2128 | /* If nr exceeds the number of processes get out quickly */ |
Eric W. Biederman | 9cc8cbc | 2006-06-26 00:25:52 -0700 | [diff] [blame] | 2129 | pos = NULL; |
Eric W. Biederman | 0bc58a9 | 2006-06-26 00:25:50 -0700 | [diff] [blame] | 2130 | if (nr && nr >= nr_processes()) |
| 2131 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2132 | |
Eric W. Biederman | 0bc58a9 | 2006-06-26 00:25:50 -0700 | [diff] [blame] | 2133 | /* If we haven't found our starting place yet start with |
| 2134 | * the init_task and walk nr tasks forward. |
| 2135 | */ |
Eric W. Biederman | 9cc8cbc | 2006-06-26 00:25:52 -0700 | [diff] [blame] | 2136 | for (pos = next_task(&init_task); nr > 0; --nr) { |
| 2137 | pos = next_task(pos); |
| 2138 | if (pos == &init_task) { |
| 2139 | pos = NULL; |
| 2140 | goto done; |
| 2141 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2142 | } |
Eric W. Biederman | 9cc8cbc | 2006-06-26 00:25:52 -0700 | [diff] [blame] | 2143 | found: |
| 2144 | get_task_struct(pos); |
Eric W. Biederman | 0bc58a9 | 2006-06-26 00:25:50 -0700 | [diff] [blame] | 2145 | done: |
Eric W. Biederman | 454cc10 | 2006-06-26 00:25:51 -0700 | [diff] [blame] | 2146 | rcu_read_unlock(); |
Eric W. Biederman | 0bc58a9 | 2006-06-26 00:25:50 -0700 | [diff] [blame] | 2147 | return pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2148 | } |
| 2149 | |
| 2150 | /* |
Eric W. Biederman | 0bc58a9 | 2006-06-26 00:25:50 -0700 | [diff] [blame] | 2151 | * Find the next task in the task list. |
| 2152 | * Return NULL if we loop or there is any error. |
| 2153 | * |
| 2154 | * The reference to the input task_struct is released. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2155 | */ |
Eric W. Biederman | 0bc58a9 | 2006-06-26 00:25:50 -0700 | [diff] [blame] | 2156 | static struct task_struct *next_tgid(struct task_struct *start) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2157 | { |
Eric W. Biederman | 0bc58a9 | 2006-06-26 00:25:50 -0700 | [diff] [blame] | 2158 | struct task_struct *pos; |
Eric W. Biederman | 454cc10 | 2006-06-26 00:25:51 -0700 | [diff] [blame] | 2159 | rcu_read_lock(); |
Eric W. Biederman | 0bc58a9 | 2006-06-26 00:25:50 -0700 | [diff] [blame] | 2160 | pos = start; |
| 2161 | if (pid_alive(start)) |
| 2162 | pos = next_task(start); |
| 2163 | if (pid_alive(pos) && (pos != &init_task)) { |
| 2164 | get_task_struct(pos); |
| 2165 | goto done; |
| 2166 | } |
| 2167 | pos = NULL; |
| 2168 | done: |
Eric W. Biederman | 454cc10 | 2006-06-26 00:25:51 -0700 | [diff] [blame] | 2169 | rcu_read_unlock(); |
Eric W. Biederman | 0bc58a9 | 2006-06-26 00:25:50 -0700 | [diff] [blame] | 2170 | put_task_struct(start); |
| 2171 | return pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2172 | } |
| 2173 | |
| 2174 | /* for the /proc/ directory itself, after non-process stuff has been done */ |
| 2175 | int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir) |
| 2176 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2177 | char buf[PROC_NUMBUF]; |
| 2178 | unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY; |
Eric W. Biederman | 0bc58a9 | 2006-06-26 00:25:50 -0700 | [diff] [blame] | 2179 | struct task_struct *task; |
| 2180 | int tgid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2181 | |
| 2182 | if (!nr) { |
| 2183 | ino_t ino = fake_ino(0,PROC_TGID_INO); |
| 2184 | if (filldir(dirent, "self", 4, filp->f_pos, ino, DT_LNK) < 0) |
| 2185 | return 0; |
| 2186 | filp->f_pos++; |
| 2187 | nr++; |
| 2188 | } |
Eric W. Biederman | 0bc58a9 | 2006-06-26 00:25:50 -0700 | [diff] [blame] | 2189 | nr -= 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2190 | |
| 2191 | /* f_version caches the tgid value that the last readdir call couldn't |
| 2192 | * return. lseek aka telldir automagically resets f_version to 0. |
| 2193 | */ |
Eric W. Biederman | 0bc58a9 | 2006-06-26 00:25:50 -0700 | [diff] [blame] | 2194 | tgid = filp->f_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2195 | filp->f_version = 0; |
Eric W. Biederman | 0bc58a9 | 2006-06-26 00:25:50 -0700 | [diff] [blame] | 2196 | for (task = first_tgid(tgid, nr); |
| 2197 | task; |
| 2198 | task = next_tgid(task), filp->f_pos++) { |
| 2199 | int len; |
| 2200 | ino_t ino; |
| 2201 | tgid = task->pid; |
| 2202 | len = snprintf(buf, sizeof(buf), "%d", tgid); |
| 2203 | ino = fake_ino(tgid, PROC_TGID_INO); |
| 2204 | if (filldir(dirent, buf, len, filp->f_pos, ino, DT_DIR) < 0) { |
| 2205 | /* returning this tgid failed, save it as the first |
| 2206 | * pid for the next readir call */ |
| 2207 | filp->f_version = tgid; |
| 2208 | put_task_struct(task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2209 | break; |
| 2210 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2211 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2212 | return 0; |
| 2213 | } |
| 2214 | |
Eric W. Biederman | 0bc58a9 | 2006-06-26 00:25:50 -0700 | [diff] [blame] | 2215 | /* |
| 2216 | * Find the first tid of a thread group to return to user space. |
| 2217 | * |
| 2218 | * Usually this is just the thread group leader, but if the users |
| 2219 | * buffer was too small or there was a seek into the middle of the |
| 2220 | * directory we have more work todo. |
| 2221 | * |
| 2222 | * In the case of a short read we start with find_task_by_pid. |
| 2223 | * |
| 2224 | * In the case of a seek we start with the leader and walk nr |
| 2225 | * threads past it. |
| 2226 | */ |
| 2227 | static struct task_struct *first_tid(struct task_struct *leader, int tid, int nr) |
| 2228 | { |
| 2229 | struct task_struct *pos = NULL; |
| 2230 | read_lock(&tasklist_lock); |
| 2231 | |
| 2232 | /* Attempt to start with the pid of a thread */ |
| 2233 | if (tid && (nr > 0)) { |
| 2234 | pos = find_task_by_pid(tid); |
| 2235 | if (pos && (pos->group_leader != leader)) |
| 2236 | pos = NULL; |
| 2237 | if (pos) |
| 2238 | nr = 0; |
| 2239 | } |
| 2240 | |
| 2241 | /* If nr exceeds the number of threads there is nothing todo */ |
| 2242 | if (nr) { |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 2243 | if (nr >= get_nr_threads(leader)) |
Eric W. Biederman | 0bc58a9 | 2006-06-26 00:25:50 -0700 | [diff] [blame] | 2244 | goto done; |
| 2245 | } |
| 2246 | |
| 2247 | /* If we haven't found our starting place yet start with the |
| 2248 | * leader and walk nr threads forward. |
| 2249 | */ |
| 2250 | if (!pos && (nr >= 0)) |
| 2251 | pos = leader; |
| 2252 | |
| 2253 | for (; pos && pid_alive(pos); pos = next_thread(pos)) { |
| 2254 | if (--nr > 0) |
| 2255 | continue; |
| 2256 | get_task_struct(pos); |
| 2257 | goto done; |
| 2258 | } |
| 2259 | pos = NULL; |
| 2260 | done: |
| 2261 | read_unlock(&tasklist_lock); |
| 2262 | return pos; |
| 2263 | } |
| 2264 | |
| 2265 | /* |
| 2266 | * Find the next thread in the thread list. |
| 2267 | * Return NULL if there is an error or no next thread. |
| 2268 | * |
| 2269 | * The reference to the input task_struct is released. |
| 2270 | */ |
| 2271 | static struct task_struct *next_tid(struct task_struct *start) |
| 2272 | { |
| 2273 | struct task_struct *pos; |
| 2274 | read_lock(&tasklist_lock); |
| 2275 | pos = start; |
| 2276 | if (pid_alive(start)) |
| 2277 | pos = next_thread(start); |
| 2278 | if (pid_alive(pos) && (pos != start->group_leader)) |
| 2279 | get_task_struct(pos); |
| 2280 | else |
| 2281 | pos = NULL; |
| 2282 | read_unlock(&tasklist_lock); |
| 2283 | put_task_struct(start); |
| 2284 | return pos; |
| 2285 | } |
| 2286 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2287 | /* for the /proc/TGID/task/ directories */ |
| 2288 | static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir) |
| 2289 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2290 | char buf[PROC_NUMBUF]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2291 | struct dentry *dentry = filp->f_dentry; |
| 2292 | struct inode *inode = dentry->d_inode; |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 2293 | struct task_struct *leader = get_proc_task(inode); |
Eric W. Biederman | 0bc58a9 | 2006-06-26 00:25:50 -0700 | [diff] [blame] | 2294 | struct task_struct *task; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2295 | int retval = -ENOENT; |
| 2296 | ino_t ino; |
Eric W. Biederman | 0bc58a9 | 2006-06-26 00:25:50 -0700 | [diff] [blame] | 2297 | int tid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2298 | unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */ |
| 2299 | |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 2300 | if (!leader) |
| 2301 | goto out_no_task; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2302 | retval = 0; |
| 2303 | |
| 2304 | switch (pos) { |
| 2305 | case 0: |
| 2306 | ino = inode->i_ino; |
| 2307 | if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0) |
| 2308 | goto out; |
| 2309 | pos++; |
| 2310 | /* fall through */ |
| 2311 | case 1: |
| 2312 | ino = parent_ino(dentry); |
| 2313 | if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0) |
| 2314 | goto out; |
| 2315 | pos++; |
| 2316 | /* fall through */ |
| 2317 | } |
| 2318 | |
Eric W. Biederman | 0bc58a9 | 2006-06-26 00:25:50 -0700 | [diff] [blame] | 2319 | /* f_version caches the tgid value that the last readdir call couldn't |
| 2320 | * return. lseek aka telldir automagically resets f_version to 0. |
| 2321 | */ |
| 2322 | tid = filp->f_version; |
| 2323 | filp->f_version = 0; |
| 2324 | for (task = first_tid(leader, tid, pos - 2); |
| 2325 | task; |
| 2326 | task = next_tid(task), pos++) { |
| 2327 | int len; |
| 2328 | tid = task->pid; |
| 2329 | len = snprintf(buf, sizeof(buf), "%d", tid); |
| 2330 | ino = fake_ino(tid, PROC_TID_INO); |
| 2331 | if (filldir(dirent, buf, len, pos, ino, DT_DIR < 0)) { |
| 2332 | /* returning this tgid failed, save it as the first |
| 2333 | * pid for the next readir call */ |
| 2334 | filp->f_version = tid; |
| 2335 | put_task_struct(task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2336 | break; |
Eric W. Biederman | 0bc58a9 | 2006-06-26 00:25:50 -0700 | [diff] [blame] | 2337 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2338 | } |
| 2339 | out: |
| 2340 | filp->f_pos = pos; |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 2341 | put_task_struct(leader); |
| 2342 | out_no_task: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2343 | return retval; |
| 2344 | } |
Eric W. Biederman | 6e66b52 | 2006-06-26 00:25:47 -0700 | [diff] [blame] | 2345 | |
| 2346 | static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) |
| 2347 | { |
| 2348 | struct inode *inode = dentry->d_inode; |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 2349 | struct task_struct *p = get_proc_task(inode); |
Eric W. Biederman | 6e66b52 | 2006-06-26 00:25:47 -0700 | [diff] [blame] | 2350 | generic_fillattr(inode, stat); |
| 2351 | |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 2352 | if (p) { |
| 2353 | rcu_read_lock(); |
| 2354 | stat->nlink += get_nr_threads(p); |
| 2355 | rcu_read_unlock(); |
| 2356 | put_task_struct(p); |
Eric W. Biederman | 6e66b52 | 2006-06-26 00:25:47 -0700 | [diff] [blame] | 2357 | } |
| 2358 | |
| 2359 | return 0; |
| 2360 | } |