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