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