Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/fcntl.c |
| 3 | * |
| 4 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 5 | */ |
| 6 | |
| 7 | #include <linux/syscalls.h> |
| 8 | #include <linux/init.h> |
| 9 | #include <linux/mm.h> |
| 10 | #include <linux/fs.h> |
| 11 | #include <linux/file.h> |
Al Viro | 9f3acc3 | 2008-04-24 07:44:08 -0400 | [diff] [blame] | 12 | #include <linux/fdtable.h> |
Randy Dunlap | 16f7e0f | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 13 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/dnotify.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/slab.h> |
| 16 | #include <linux/module.h> |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 17 | #include <linux/pipe_fs_i.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/security.h> |
| 19 | #include <linux/ptrace.h> |
Jesper Juhl | 7ed20e1 | 2005-05-01 08:59:14 -0700 | [diff] [blame] | 20 | #include <linux/signal.h> |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 21 | #include <linux/rcupdate.h> |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 22 | #include <linux/pid_namespace.h> |
David Herrmann | c9ccca2 | 2014-08-08 14:25:27 -0700 | [diff] [blame] | 23 | #include <linux/shmem_fs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
| 25 | #include <asm/poll.h> |
| 26 | #include <asm/siginfo.h> |
| 27 | #include <asm/uaccess.h> |
| 28 | |
Harvey Harrison | fc9b52c | 2008-02-08 04:19:52 -0800 | [diff] [blame] | 29 | void set_close_on_exec(unsigned int fd, int flag) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | { |
| 31 | struct files_struct *files = current->files; |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 32 | struct fdtable *fdt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | spin_lock(&files->file_lock); |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 34 | fdt = files_fdtable(files); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | if (flag) |
David Howells | 1dce27c | 2012-02-16 17:49:42 +0000 | [diff] [blame] | 36 | __set_close_on_exec(fd, fdt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | else |
David Howells | 1dce27c | 2012-02-16 17:49:42 +0000 | [diff] [blame] | 38 | __clear_close_on_exec(fd, fdt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | spin_unlock(&files->file_lock); |
| 40 | } |
| 41 | |
David Howells | 1dce27c | 2012-02-16 17:49:42 +0000 | [diff] [blame] | 42 | static bool get_close_on_exec(unsigned int fd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | { |
| 44 | struct files_struct *files = current->files; |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 45 | struct fdtable *fdt; |
David Howells | 1dce27c | 2012-02-16 17:49:42 +0000 | [diff] [blame] | 46 | bool res; |
Dipankar Sarma | b835996 | 2005-09-09 13:04:14 -0700 | [diff] [blame] | 47 | rcu_read_lock(); |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 48 | fdt = files_fdtable(files); |
David Howells | 1dce27c | 2012-02-16 17:49:42 +0000 | [diff] [blame] | 49 | res = close_on_exec(fd, fdt); |
Dipankar Sarma | b835996 | 2005-09-09 13:04:14 -0700 | [diff] [blame] | 50 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | return res; |
| 52 | } |
| 53 | |
Heiko Carstens | a26eab2 | 2009-01-14 14:14:17 +0100 | [diff] [blame] | 54 | SYSCALL_DEFINE3(dup3, unsigned int, oldfd, unsigned int, newfd, int, flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | { |
| 56 | int err = -EBADF; |
| 57 | struct file * file, *tofree; |
| 58 | struct files_struct * files = current->files; |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 59 | struct fdtable *fdt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
Ulrich Drepper | 336dd1f | 2008-07-23 21:29:29 -0700 | [diff] [blame] | 61 | if ((flags & ~O_CLOEXEC) != 0) |
| 62 | return -EINVAL; |
| 63 | |
Al Viro | 6c5d051 | 2008-07-26 13:38:19 -0400 | [diff] [blame] | 64 | if (unlikely(oldfd == newfd)) |
| 65 | return -EINVAL; |
| 66 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | spin_lock(&files->file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | err = expand_files(files, newfd); |
Al Viro | 1b7e190 | 2008-07-30 06:18:03 -0400 | [diff] [blame] | 69 | file = fcheck(oldfd); |
| 70 | if (unlikely(!file)) |
| 71 | goto Ebadf; |
Al Viro | 4e1e018 | 2008-07-26 16:01:20 -0400 | [diff] [blame] | 72 | if (unlikely(err < 0)) { |
| 73 | if (err == -EMFILE) |
Al Viro | 1b7e190 | 2008-07-30 06:18:03 -0400 | [diff] [blame] | 74 | goto Ebadf; |
| 75 | goto out_unlock; |
Al Viro | 4e1e018 | 2008-07-26 16:01:20 -0400 | [diff] [blame] | 76 | } |
Al Viro | 1b7e190 | 2008-07-30 06:18:03 -0400 | [diff] [blame] | 77 | /* |
| 78 | * We need to detect attempts to do dup2() over allocated but still |
| 79 | * not finished descriptor. NB: OpenBSD avoids that at the price of |
| 80 | * extra work in their equivalent of fget() - they insert struct |
| 81 | * file immediately after grabbing descriptor, mark it larval if |
| 82 | * more work (e.g. actual opening) is needed and make sure that |
| 83 | * fget() treats larval files as absent. Potentially interesting, |
| 84 | * but while extra work in fget() is trivial, locking implications |
| 85 | * and amount of surgery on open()-related paths in VFS are not. |
| 86 | * FreeBSD fails with -EBADF in the same situation, NetBSD "solution" |
| 87 | * deadlocks in rather amusing ways, AFAICS. All of that is out of |
| 88 | * scope of POSIX or SUS, since neither considers shared descriptor |
| 89 | * tables and this condition does not arise without those. |
| 90 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | err = -EBUSY; |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 92 | fdt = files_fdtable(files); |
| 93 | tofree = fdt->fd[newfd]; |
David Howells | 1dce27c | 2012-02-16 17:49:42 +0000 | [diff] [blame] | 94 | if (!tofree && fd_is_open(newfd, fdt)) |
Al Viro | 1b7e190 | 2008-07-30 06:18:03 -0400 | [diff] [blame] | 95 | goto out_unlock; |
| 96 | get_file(file); |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 97 | rcu_assign_pointer(fdt->fd[newfd], file); |
David Howells | 1dce27c | 2012-02-16 17:49:42 +0000 | [diff] [blame] | 98 | __set_open_fd(newfd, fdt); |
Ulrich Drepper | 336dd1f | 2008-07-23 21:29:29 -0700 | [diff] [blame] | 99 | if (flags & O_CLOEXEC) |
David Howells | 1dce27c | 2012-02-16 17:49:42 +0000 | [diff] [blame] | 100 | __set_close_on_exec(newfd, fdt); |
Ulrich Drepper | 336dd1f | 2008-07-23 21:29:29 -0700 | [diff] [blame] | 101 | else |
David Howells | 1dce27c | 2012-02-16 17:49:42 +0000 | [diff] [blame] | 102 | __clear_close_on_exec(newfd, fdt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | spin_unlock(&files->file_lock); |
| 104 | |
| 105 | if (tofree) |
| 106 | filp_close(tofree, files); |
Al Viro | 1b7e190 | 2008-07-30 06:18:03 -0400 | [diff] [blame] | 107 | |
| 108 | return newfd; |
| 109 | |
| 110 | Ebadf: |
| 111 | err = -EBADF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | out_unlock: |
| 113 | spin_unlock(&files->file_lock); |
Al Viro | 1b7e190 | 2008-07-30 06:18:03 -0400 | [diff] [blame] | 114 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Heiko Carstens | a26eab2 | 2009-01-14 14:14:17 +0100 | [diff] [blame] | 117 | SYSCALL_DEFINE2(dup2, unsigned int, oldfd, unsigned int, newfd) |
Ulrich Drepper | 336dd1f | 2008-07-23 21:29:29 -0700 | [diff] [blame] | 118 | { |
Al Viro | 6c5d051 | 2008-07-26 13:38:19 -0400 | [diff] [blame] | 119 | if (unlikely(newfd == oldfd)) { /* corner case */ |
| 120 | struct files_struct *files = current->files; |
Jeff Mahoney | 2b79bc4 | 2009-05-11 14:25:34 -0400 | [diff] [blame] | 121 | int retval = oldfd; |
| 122 | |
Al Viro | 6c5d051 | 2008-07-26 13:38:19 -0400 | [diff] [blame] | 123 | rcu_read_lock(); |
| 124 | if (!fcheck_files(files, oldfd)) |
Jeff Mahoney | 2b79bc4 | 2009-05-11 14:25:34 -0400 | [diff] [blame] | 125 | retval = -EBADF; |
Al Viro | 6c5d051 | 2008-07-26 13:38:19 -0400 | [diff] [blame] | 126 | rcu_read_unlock(); |
Jeff Mahoney | 2b79bc4 | 2009-05-11 14:25:34 -0400 | [diff] [blame] | 127 | return retval; |
Al Viro | 6c5d051 | 2008-07-26 13:38:19 -0400 | [diff] [blame] | 128 | } |
Ulrich Drepper | 336dd1f | 2008-07-23 21:29:29 -0700 | [diff] [blame] | 129 | return sys_dup3(oldfd, newfd, 0); |
| 130 | } |
| 131 | |
Heiko Carstens | a26eab2 | 2009-01-14 14:14:17 +0100 | [diff] [blame] | 132 | SYSCALL_DEFINE1(dup, unsigned int, fildes) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | { |
| 134 | int ret = -EBADF; |
Al Viro | 1abf0c7 | 2011-03-13 03:51:11 -0400 | [diff] [blame] | 135 | struct file *file = fget_raw(fildes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | |
Al Viro | 1027abe | 2008-07-30 04:13:04 -0400 | [diff] [blame] | 137 | if (file) { |
| 138 | ret = get_unused_fd(); |
| 139 | if (ret >= 0) |
| 140 | fd_install(ret, file); |
| 141 | else |
| 142 | fput(file); |
| 143 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | return ret; |
| 145 | } |
| 146 | |
Jonathan Corbet | 7639842 | 2009-02-01 14:26:59 -0700 | [diff] [blame] | 147 | #define SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT | O_NOATIME) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | |
| 149 | static int setfl(int fd, struct file * filp, unsigned long arg) |
| 150 | { |
Josef "Jeff" Sipek | 0f7fc9e | 2006-12-08 02:36:35 -0800 | [diff] [blame] | 151 | struct inode * inode = filp->f_path.dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | int error = 0; |
| 153 | |
dean gaudet | 7d95c8f | 2006-02-03 03:04:30 -0800 | [diff] [blame] | 154 | /* |
| 155 | * O_APPEND cannot be cleared if the file is marked as append-only |
| 156 | * and the file is open for write. |
| 157 | */ |
| 158 | if (((arg ^ filp->f_flags) & O_APPEND) && IS_APPEND(inode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | return -EPERM; |
| 160 | |
| 161 | /* O_NOATIME can only be set by the owner or superuser */ |
| 162 | if ((arg & O_NOATIME) && !(filp->f_flags & O_NOATIME)) |
Serge E. Hallyn | 2e14967 | 2011-03-23 16:43:26 -0700 | [diff] [blame] | 163 | if (!inode_owner_or_capable(inode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | return -EPERM; |
| 165 | |
| 166 | /* required for strict SunOS emulation */ |
| 167 | if (O_NONBLOCK != O_NDELAY) |
| 168 | if (arg & O_NDELAY) |
| 169 | arg |= O_NONBLOCK; |
| 170 | |
| 171 | if (arg & O_DIRECT) { |
| 172 | if (!filp->f_mapping || !filp->f_mapping->a_ops || |
| 173 | !filp->f_mapping->a_ops->direct_IO) |
| 174 | return -EINVAL; |
| 175 | } |
| 176 | |
| 177 | if (filp->f_op && filp->f_op->check_flags) |
| 178 | error = filp->f_op->check_flags(arg); |
| 179 | if (error) |
| 180 | return error; |
| 181 | |
Jonathan Corbet | 218d11a | 2008-12-05 16:12:48 -0700 | [diff] [blame] | 182 | /* |
Jonathan Corbet | 7639842 | 2009-02-01 14:26:59 -0700 | [diff] [blame] | 183 | * ->fasync() is responsible for setting the FASYNC bit. |
Jonathan Corbet | 218d11a | 2008-12-05 16:12:48 -0700 | [diff] [blame] | 184 | */ |
Jonathan Corbet | 7639842 | 2009-02-01 14:26:59 -0700 | [diff] [blame] | 185 | if (((arg ^ filp->f_flags) & FASYNC) && filp->f_op && |
| 186 | filp->f_op->fasync) { |
| 187 | error = filp->f_op->fasync(fd, filp, (arg & FASYNC) != 0); |
| 188 | if (error < 0) |
| 189 | goto out; |
Jonathan Corbet | 60aa492 | 2009-02-01 14:52:56 -0700 | [diff] [blame] | 190 | if (error > 0) |
| 191 | error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | } |
Jonathan Corbet | db1dd4d | 2009-02-06 15:25:24 -0700 | [diff] [blame] | 193 | spin_lock(&filp->f_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | filp->f_flags = (arg & SETFL_MASK) | (filp->f_flags & ~SETFL_MASK); |
Jonathan Corbet | db1dd4d | 2009-02-06 15:25:24 -0700 | [diff] [blame] | 195 | spin_unlock(&filp->f_lock); |
Jonathan Corbet | 7639842 | 2009-02-01 14:26:59 -0700 | [diff] [blame] | 196 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | return error; |
| 199 | } |
| 200 | |
Eric W. Biederman | 609d7fa | 2006-10-02 02:17:15 -0700 | [diff] [blame] | 201 | static void f_modown(struct file *filp, struct pid *pid, enum pid_type type, |
Oleg Nesterov | 2f38d70 | 2009-06-16 22:07:46 +0200 | [diff] [blame] | 202 | int force) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | { |
Linus Torvalds | 80e1e82 | 2010-02-07 10:11:23 -0800 | [diff] [blame] | 204 | write_lock_irq(&filp->f_owner.lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | if (force || !filp->f_owner.pid) { |
Eric W. Biederman | 609d7fa | 2006-10-02 02:17:15 -0700 | [diff] [blame] | 206 | put_pid(filp->f_owner.pid); |
| 207 | filp->f_owner.pid = get_pid(pid); |
| 208 | filp->f_owner.pid_type = type; |
Oleg Nesterov | 2f38d70 | 2009-06-16 22:07:46 +0200 | [diff] [blame] | 209 | |
| 210 | if (pid) { |
| 211 | const struct cred *cred = current_cred(); |
| 212 | filp->f_owner.uid = cred->uid; |
| 213 | filp->f_owner.euid = cred->euid; |
| 214 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | } |
Linus Torvalds | 80e1e82 | 2010-02-07 10:11:23 -0800 | [diff] [blame] | 216 | write_unlock_irq(&filp->f_owner.lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Eric W. Biederman | 609d7fa | 2006-10-02 02:17:15 -0700 | [diff] [blame] | 219 | int __f_setown(struct file *filp, struct pid *pid, enum pid_type type, |
| 220 | int force) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | { |
| 222 | int err; |
Oleg Nesterov | 2f38d70 | 2009-06-16 22:07:46 +0200 | [diff] [blame] | 223 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | err = security_file_set_fowner(filp); |
| 225 | if (err) |
| 226 | return err; |
| 227 | |
Oleg Nesterov | 2f38d70 | 2009-06-16 22:07:46 +0200 | [diff] [blame] | 228 | f_modown(filp, pid, type, force); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | return 0; |
| 230 | } |
Eric W. Biederman | 609d7fa | 2006-10-02 02:17:15 -0700 | [diff] [blame] | 231 | EXPORT_SYMBOL(__f_setown); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | |
Eric W. Biederman | 609d7fa | 2006-10-02 02:17:15 -0700 | [diff] [blame] | 233 | int f_setown(struct file *filp, unsigned long arg, int force) |
| 234 | { |
| 235 | enum pid_type type; |
| 236 | struct pid *pid; |
| 237 | int who = arg; |
| 238 | int result; |
| 239 | type = PIDTYPE_PID; |
| 240 | if (who < 0) { |
| 241 | type = PIDTYPE_PGID; |
| 242 | who = -who; |
| 243 | } |
| 244 | rcu_read_lock(); |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 245 | pid = find_vpid(who); |
Eric W. Biederman | 609d7fa | 2006-10-02 02:17:15 -0700 | [diff] [blame] | 246 | result = __f_setown(filp, pid, type, force); |
| 247 | rcu_read_unlock(); |
| 248 | return result; |
| 249 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | EXPORT_SYMBOL(f_setown); |
| 251 | |
| 252 | void f_delown(struct file *filp) |
| 253 | { |
Oleg Nesterov | 2f38d70 | 2009-06-16 22:07:46 +0200 | [diff] [blame] | 254 | f_modown(filp, NULL, PIDTYPE_PID, 1); |
Eric W. Biederman | 609d7fa | 2006-10-02 02:17:15 -0700 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | pid_t f_getown(struct file *filp) |
| 258 | { |
| 259 | pid_t pid; |
Eric W. Biederman | 43fa1ad | 2006-10-02 02:17:27 -0700 | [diff] [blame] | 260 | read_lock(&filp->f_owner.lock); |
Pavel Emelyanov | 6c5f3e7 | 2008-02-08 04:19:20 -0800 | [diff] [blame] | 261 | pid = pid_vnr(filp->f_owner.pid); |
Eric W. Biederman | 609d7fa | 2006-10-02 02:17:15 -0700 | [diff] [blame] | 262 | if (filp->f_owner.pid_type == PIDTYPE_PGID) |
| 263 | pid = -pid; |
Eric W. Biederman | 43fa1ad | 2006-10-02 02:17:27 -0700 | [diff] [blame] | 264 | read_unlock(&filp->f_owner.lock); |
Eric W. Biederman | 609d7fa | 2006-10-02 02:17:15 -0700 | [diff] [blame] | 265 | return pid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | } |
| 267 | |
Peter Zijlstra | ba0a6c9 | 2009-09-23 15:57:03 -0700 | [diff] [blame] | 268 | static int f_setown_ex(struct file *filp, unsigned long arg) |
| 269 | { |
| 270 | struct f_owner_ex * __user owner_p = (void * __user)arg; |
| 271 | struct f_owner_ex owner; |
| 272 | struct pid *pid; |
| 273 | int type; |
| 274 | int ret; |
| 275 | |
| 276 | ret = copy_from_user(&owner, owner_p, sizeof(owner)); |
| 277 | if (ret) |
Dan Carpenter | 5b54470 | 2010-06-03 12:35:42 +0200 | [diff] [blame] | 278 | return -EFAULT; |
Peter Zijlstra | ba0a6c9 | 2009-09-23 15:57:03 -0700 | [diff] [blame] | 279 | |
| 280 | switch (owner.type) { |
| 281 | case F_OWNER_TID: |
| 282 | type = PIDTYPE_MAX; |
| 283 | break; |
| 284 | |
| 285 | case F_OWNER_PID: |
| 286 | type = PIDTYPE_PID; |
| 287 | break; |
| 288 | |
Peter Zijlstra | 978b405 | 2009-11-17 14:06:24 -0800 | [diff] [blame] | 289 | case F_OWNER_PGRP: |
Peter Zijlstra | ba0a6c9 | 2009-09-23 15:57:03 -0700 | [diff] [blame] | 290 | type = PIDTYPE_PGID; |
| 291 | break; |
| 292 | |
| 293 | default: |
| 294 | return -EINVAL; |
| 295 | } |
| 296 | |
| 297 | rcu_read_lock(); |
| 298 | pid = find_vpid(owner.pid); |
| 299 | if (owner.pid && !pid) |
| 300 | ret = -ESRCH; |
| 301 | else |
| 302 | ret = __f_setown(filp, pid, type, 1); |
| 303 | rcu_read_unlock(); |
| 304 | |
| 305 | return ret; |
| 306 | } |
| 307 | |
| 308 | static int f_getown_ex(struct file *filp, unsigned long arg) |
| 309 | { |
| 310 | struct f_owner_ex * __user owner_p = (void * __user)arg; |
| 311 | struct f_owner_ex owner; |
| 312 | int ret = 0; |
| 313 | |
| 314 | read_lock(&filp->f_owner.lock); |
| 315 | owner.pid = pid_vnr(filp->f_owner.pid); |
| 316 | switch (filp->f_owner.pid_type) { |
| 317 | case PIDTYPE_MAX: |
| 318 | owner.type = F_OWNER_TID; |
| 319 | break; |
| 320 | |
| 321 | case PIDTYPE_PID: |
| 322 | owner.type = F_OWNER_PID; |
| 323 | break; |
| 324 | |
| 325 | case PIDTYPE_PGID: |
Peter Zijlstra | 978b405 | 2009-11-17 14:06:24 -0800 | [diff] [blame] | 326 | owner.type = F_OWNER_PGRP; |
Peter Zijlstra | ba0a6c9 | 2009-09-23 15:57:03 -0700 | [diff] [blame] | 327 | break; |
| 328 | |
| 329 | default: |
| 330 | WARN_ON(1); |
| 331 | ret = -EINVAL; |
| 332 | break; |
| 333 | } |
| 334 | read_unlock(&filp->f_owner.lock); |
| 335 | |
Dan Carpenter | 5b54470 | 2010-06-03 12:35:42 +0200 | [diff] [blame] | 336 | if (!ret) { |
Peter Zijlstra | ba0a6c9 | 2009-09-23 15:57:03 -0700 | [diff] [blame] | 337 | ret = copy_to_user(owner_p, &owner, sizeof(owner)); |
Dan Carpenter | 5b54470 | 2010-06-03 12:35:42 +0200 | [diff] [blame] | 338 | if (ret) |
| 339 | ret = -EFAULT; |
| 340 | } |
Peter Zijlstra | ba0a6c9 | 2009-09-23 15:57:03 -0700 | [diff] [blame] | 341 | return ret; |
| 342 | } |
| 343 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | static long do_fcntl(int fd, unsigned int cmd, unsigned long arg, |
| 345 | struct file *filp) |
| 346 | { |
| 347 | long err = -EINVAL; |
| 348 | |
| 349 | switch (cmd) { |
| 350 | case F_DUPFD: |
Ulrich Drepper | 22d2b35 | 2007-10-16 23:30:26 -0700 | [diff] [blame] | 351 | case F_DUPFD_CLOEXEC: |
Jiri Slaby | d554ed89 | 2010-03-05 13:42:42 -0800 | [diff] [blame] | 352 | if (arg >= rlimit(RLIMIT_NOFILE)) |
Al Viro | 4e1e018 | 2008-07-26 16:01:20 -0400 | [diff] [blame] | 353 | break; |
Al Viro | 1027abe | 2008-07-30 04:13:04 -0400 | [diff] [blame] | 354 | err = alloc_fd(arg, cmd == F_DUPFD_CLOEXEC ? O_CLOEXEC : 0); |
| 355 | if (err >= 0) { |
| 356 | get_file(filp); |
| 357 | fd_install(err, filp); |
| 358 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | break; |
| 360 | case F_GETFD: |
| 361 | err = get_close_on_exec(fd) ? FD_CLOEXEC : 0; |
| 362 | break; |
| 363 | case F_SETFD: |
| 364 | err = 0; |
| 365 | set_close_on_exec(fd, arg & FD_CLOEXEC); |
| 366 | break; |
| 367 | case F_GETFL: |
| 368 | err = filp->f_flags; |
| 369 | break; |
| 370 | case F_SETFL: |
| 371 | err = setfl(fd, filp, arg); |
| 372 | break; |
Jeff Layton | f68ae18 | 2014-02-03 12:13:10 -0500 | [diff] [blame] | 373 | #if BITS_PER_LONG != 32 |
| 374 | /* 32-bit arches must use fcntl64() */ |
Jeff Layton | 744c99d | 2014-04-22 08:23:58 -0400 | [diff] [blame] | 375 | case F_OFD_GETLK: |
Jeff Layton | f68ae18 | 2014-02-03 12:13:10 -0500 | [diff] [blame] | 376 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | case F_GETLK: |
Jeff Layton | a3b0c80 | 2014-02-03 12:13:09 -0500 | [diff] [blame] | 378 | err = fcntl_getlk(filp, cmd, (struct flock __user *) arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | break; |
Jeff Layton | f68ae18 | 2014-02-03 12:13:10 -0500 | [diff] [blame] | 380 | #if BITS_PER_LONG != 32 |
| 381 | /* 32-bit arches must use fcntl64() */ |
Jeff Layton | 744c99d | 2014-04-22 08:23:58 -0400 | [diff] [blame] | 382 | case F_OFD_SETLK: |
| 383 | case F_OFD_SETLKW: |
Jeff Layton | f68ae18 | 2014-02-03 12:13:10 -0500 | [diff] [blame] | 384 | #endif |
| 385 | /* Fallthrough */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | case F_SETLK: |
| 387 | case F_SETLKW: |
Peter Staubach | c293621 | 2005-07-27 11:45:09 -0700 | [diff] [blame] | 388 | err = fcntl_setlk(fd, filp, cmd, (struct flock __user *) arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | break; |
| 390 | case F_GETOWN: |
| 391 | /* |
| 392 | * XXX If f_owner is a process group, the |
| 393 | * negative return value will get converted |
| 394 | * into an error. Oops. If we keep the |
| 395 | * current syscall conventions, the only way |
| 396 | * to fix this will be in libc. |
| 397 | */ |
Eric W. Biederman | 609d7fa | 2006-10-02 02:17:15 -0700 | [diff] [blame] | 398 | err = f_getown(filp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | force_successful_syscall_return(); |
| 400 | break; |
| 401 | case F_SETOWN: |
| 402 | err = f_setown(filp, arg, 1); |
| 403 | break; |
Peter Zijlstra | ba0a6c9 | 2009-09-23 15:57:03 -0700 | [diff] [blame] | 404 | case F_GETOWN_EX: |
| 405 | err = f_getown_ex(filp, arg); |
| 406 | break; |
| 407 | case F_SETOWN_EX: |
| 408 | err = f_setown_ex(filp, arg); |
| 409 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | case F_GETSIG: |
| 411 | err = filp->f_owner.signum; |
| 412 | break; |
| 413 | case F_SETSIG: |
| 414 | /* arg == 0 restores default behaviour. */ |
Jesper Juhl | 7ed20e1 | 2005-05-01 08:59:14 -0700 | [diff] [blame] | 415 | if (!valid_signal(arg)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | break; |
| 417 | } |
| 418 | err = 0; |
| 419 | filp->f_owner.signum = arg; |
| 420 | break; |
| 421 | case F_GETLEASE: |
| 422 | err = fcntl_getlease(filp); |
| 423 | break; |
| 424 | case F_SETLEASE: |
| 425 | err = fcntl_setlease(fd, filp, arg); |
| 426 | break; |
| 427 | case F_NOTIFY: |
| 428 | err = fcntl_dirnotify(fd, filp, arg); |
| 429 | break; |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 430 | case F_SETPIPE_SZ: |
| 431 | case F_GETPIPE_SZ: |
| 432 | err = pipe_fcntl(filp, cmd, arg); |
| 433 | break; |
David Herrmann | c9ccca2 | 2014-08-08 14:25:27 -0700 | [diff] [blame] | 434 | case F_ADD_SEALS: |
| 435 | case F_GET_SEALS: |
| 436 | err = shmem_fcntl(filp, cmd, arg); |
| 437 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | default: |
| 439 | break; |
| 440 | } |
| 441 | return err; |
| 442 | } |
| 443 | |
Al Viro | 1abf0c7 | 2011-03-13 03:51:11 -0400 | [diff] [blame] | 444 | static int check_fcntl_cmd(unsigned cmd) |
| 445 | { |
| 446 | switch (cmd) { |
| 447 | case F_DUPFD: |
| 448 | case F_DUPFD_CLOEXEC: |
| 449 | case F_GETFD: |
| 450 | case F_SETFD: |
| 451 | case F_GETFL: |
| 452 | return 1; |
| 453 | } |
| 454 | return 0; |
| 455 | } |
| 456 | |
Heiko Carstens | a26eab2 | 2009-01-14 14:14:17 +0100 | [diff] [blame] | 457 | SYSCALL_DEFINE3(fcntl, unsigned int, fd, unsigned int, cmd, unsigned long, arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | { |
| 459 | struct file *filp; |
| 460 | long err = -EBADF; |
| 461 | |
Al Viro | 1abf0c7 | 2011-03-13 03:51:11 -0400 | [diff] [blame] | 462 | filp = fget_raw(fd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | if (!filp) |
| 464 | goto out; |
| 465 | |
Al Viro | 1abf0c7 | 2011-03-13 03:51:11 -0400 | [diff] [blame] | 466 | if (unlikely(filp->f_mode & FMODE_PATH)) { |
| 467 | if (!check_fcntl_cmd(cmd)) { |
| 468 | fput(filp); |
| 469 | goto out; |
| 470 | } |
| 471 | } |
| 472 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | err = security_file_fcntl(filp, cmd, arg); |
| 474 | if (err) { |
| 475 | fput(filp); |
| 476 | return err; |
| 477 | } |
| 478 | |
| 479 | err = do_fcntl(fd, cmd, arg, filp); |
| 480 | |
| 481 | fput(filp); |
| 482 | out: |
| 483 | return err; |
| 484 | } |
| 485 | |
| 486 | #if BITS_PER_LONG == 32 |
Heiko Carstens | a26eab2 | 2009-01-14 14:14:17 +0100 | [diff] [blame] | 487 | SYSCALL_DEFINE3(fcntl64, unsigned int, fd, unsigned int, cmd, |
| 488 | unsigned long, arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | { |
| 490 | struct file * filp; |
| 491 | long err; |
| 492 | |
| 493 | err = -EBADF; |
Al Viro | 1abf0c7 | 2011-03-13 03:51:11 -0400 | [diff] [blame] | 494 | filp = fget_raw(fd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | if (!filp) |
| 496 | goto out; |
| 497 | |
Al Viro | 1abf0c7 | 2011-03-13 03:51:11 -0400 | [diff] [blame] | 498 | if (unlikely(filp->f_mode & FMODE_PATH)) { |
| 499 | if (!check_fcntl_cmd(cmd)) { |
| 500 | fput(filp); |
| 501 | goto out; |
| 502 | } |
| 503 | } |
| 504 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | err = security_file_fcntl(filp, cmd, arg); |
| 506 | if (err) { |
| 507 | fput(filp); |
| 508 | return err; |
| 509 | } |
| 510 | err = -EBADF; |
| 511 | |
| 512 | switch (cmd) { |
Jeff Layton | f68ae18 | 2014-02-03 12:13:10 -0500 | [diff] [blame] | 513 | case F_GETLK64: |
Jeff Layton | 744c99d | 2014-04-22 08:23:58 -0400 | [diff] [blame] | 514 | case F_OFD_GETLK: |
Jeff Layton | f68ae18 | 2014-02-03 12:13:10 -0500 | [diff] [blame] | 515 | err = fcntl_getlk64(filp, cmd, (struct flock64 __user *) arg); |
| 516 | break; |
| 517 | case F_SETLK64: |
| 518 | case F_SETLKW64: |
Jeff Layton | 744c99d | 2014-04-22 08:23:58 -0400 | [diff] [blame] | 519 | case F_OFD_SETLK: |
| 520 | case F_OFD_SETLKW: |
Jeff Layton | f68ae18 | 2014-02-03 12:13:10 -0500 | [diff] [blame] | 521 | err = fcntl_setlk64(fd, filp, cmd, |
| 522 | (struct flock64 __user *) arg); |
| 523 | break; |
| 524 | default: |
| 525 | err = do_fcntl(fd, cmd, arg, filp); |
| 526 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | } |
| 528 | fput(filp); |
| 529 | out: |
| 530 | return err; |
| 531 | } |
| 532 | #endif |
| 533 | |
| 534 | /* Table to convert sigio signal codes into poll band bitmaps */ |
| 535 | |
Eric Dumazet | fa3536c | 2006-03-26 01:37:24 -0800 | [diff] [blame] | 536 | static const long band_table[NSIGPOLL] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | POLLIN | POLLRDNORM, /* POLL_IN */ |
| 538 | POLLOUT | POLLWRNORM | POLLWRBAND, /* POLL_OUT */ |
| 539 | POLLIN | POLLRDNORM | POLLMSG, /* POLL_MSG */ |
| 540 | POLLERR, /* POLL_ERR */ |
| 541 | POLLPRI | POLLRDBAND, /* POLL_PRI */ |
| 542 | POLLHUP | POLLERR /* POLL_HUP */ |
| 543 | }; |
| 544 | |
| 545 | static inline int sigio_perm(struct task_struct *p, |
| 546 | struct fown_struct *fown, int sig) |
| 547 | { |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 548 | const struct cred *cred; |
| 549 | int ret; |
| 550 | |
| 551 | rcu_read_lock(); |
| 552 | cred = __task_cred(p); |
| 553 | ret = ((fown->euid == 0 || |
| 554 | fown->euid == cred->suid || fown->euid == cred->uid || |
| 555 | fown->uid == cred->suid || fown->uid == cred->uid) && |
| 556 | !security_file_send_sigiotask(p, fown, sig)); |
| 557 | rcu_read_unlock(); |
| 558 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | static void send_sigio_to_task(struct task_struct *p, |
Oleg Nesterov | 8eeee4e | 2009-06-17 00:27:10 +0200 | [diff] [blame] | 562 | struct fown_struct *fown, |
Peter Zijlstra | ba0a6c9 | 2009-09-23 15:57:03 -0700 | [diff] [blame] | 563 | int fd, int reason, int group) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | { |
Oleg Nesterov | 8eeee4e | 2009-06-17 00:27:10 +0200 | [diff] [blame] | 565 | /* |
| 566 | * F_SETSIG can change ->signum lockless in parallel, make |
| 567 | * sure we read it once and use the same value throughout. |
| 568 | */ |
| 569 | int signum = ACCESS_ONCE(fown->signum); |
| 570 | |
| 571 | if (!sigio_perm(p, fown, signum)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | return; |
| 573 | |
Oleg Nesterov | 8eeee4e | 2009-06-17 00:27:10 +0200 | [diff] [blame] | 574 | switch (signum) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | siginfo_t si; |
| 576 | default: |
| 577 | /* Queue a rt signal with the appropriate fd as its |
| 578 | value. We use SI_SIGIO as the source, not |
| 579 | SI_KERNEL, since kernel signals always get |
| 580 | delivered even if we can't queue. Failure to |
| 581 | queue in this case _should_ be reported; we fall |
| 582 | back to SIGIO in that case. --sct */ |
Oleg Nesterov | 8eeee4e | 2009-06-17 00:27:10 +0200 | [diff] [blame] | 583 | si.si_signo = signum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | si.si_errno = 0; |
| 585 | si.si_code = reason; |
| 586 | /* Make sure we are called with one of the POLL_* |
| 587 | reasons, otherwise we could leak kernel stack into |
| 588 | userspace. */ |
Eric Sesterhenn | f6298aa | 2006-04-02 13:37:19 +0200 | [diff] [blame] | 589 | BUG_ON((reason & __SI_MASK) != __SI_POLL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | if (reason - POLL_IN >= NSIGPOLL) |
| 591 | si.si_band = ~0L; |
| 592 | else |
| 593 | si.si_band = band_table[reason - POLL_IN]; |
| 594 | si.si_fd = fd; |
Peter Zijlstra | ba0a6c9 | 2009-09-23 15:57:03 -0700 | [diff] [blame] | 595 | if (!do_send_sig_info(signum, &si, p, group)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | break; |
| 597 | /* fall-through: fall back on the old plain SIGIO signal */ |
| 598 | case 0: |
Peter Zijlstra | ba0a6c9 | 2009-09-23 15:57:03 -0700 | [diff] [blame] | 599 | do_send_sig_info(SIGIO, SEND_SIG_PRIV, p, group); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | } |
| 601 | } |
| 602 | |
| 603 | void send_sigio(struct fown_struct *fown, int fd, int band) |
| 604 | { |
| 605 | struct task_struct *p; |
Eric W. Biederman | 609d7fa | 2006-10-02 02:17:15 -0700 | [diff] [blame] | 606 | enum pid_type type; |
| 607 | struct pid *pid; |
Peter Zijlstra | ba0a6c9 | 2009-09-23 15:57:03 -0700 | [diff] [blame] | 608 | int group = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | |
| 610 | read_lock(&fown->lock); |
Peter Zijlstra | ba0a6c9 | 2009-09-23 15:57:03 -0700 | [diff] [blame] | 611 | |
Eric W. Biederman | 609d7fa | 2006-10-02 02:17:15 -0700 | [diff] [blame] | 612 | type = fown->pid_type; |
Peter Zijlstra | ba0a6c9 | 2009-09-23 15:57:03 -0700 | [diff] [blame] | 613 | if (type == PIDTYPE_MAX) { |
| 614 | group = 0; |
| 615 | type = PIDTYPE_PID; |
| 616 | } |
| 617 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | pid = fown->pid; |
| 619 | if (!pid) |
| 620 | goto out_unlock_fown; |
| 621 | |
| 622 | read_lock(&tasklist_lock); |
Eric W. Biederman | 609d7fa | 2006-10-02 02:17:15 -0700 | [diff] [blame] | 623 | do_each_pid_task(pid, type, p) { |
Peter Zijlstra | ba0a6c9 | 2009-09-23 15:57:03 -0700 | [diff] [blame] | 624 | send_sigio_to_task(p, fown, fd, band, group); |
Eric W. Biederman | 609d7fa | 2006-10-02 02:17:15 -0700 | [diff] [blame] | 625 | } while_each_pid_task(pid, type, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | read_unlock(&tasklist_lock); |
| 627 | out_unlock_fown: |
| 628 | read_unlock(&fown->lock); |
| 629 | } |
| 630 | |
| 631 | static void send_sigurg_to_task(struct task_struct *p, |
Peter Zijlstra | ba0a6c9 | 2009-09-23 15:57:03 -0700 | [diff] [blame] | 632 | struct fown_struct *fown, int group) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | { |
| 634 | if (sigio_perm(p, fown, SIGURG)) |
Peter Zijlstra | ba0a6c9 | 2009-09-23 15:57:03 -0700 | [diff] [blame] | 635 | do_send_sig_info(SIGURG, SEND_SIG_PRIV, p, group); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | int send_sigurg(struct fown_struct *fown) |
| 639 | { |
| 640 | struct task_struct *p; |
Eric W. Biederman | 609d7fa | 2006-10-02 02:17:15 -0700 | [diff] [blame] | 641 | enum pid_type type; |
| 642 | struct pid *pid; |
Peter Zijlstra | ba0a6c9 | 2009-09-23 15:57:03 -0700 | [diff] [blame] | 643 | int group = 1; |
Eric W. Biederman | 609d7fa | 2006-10-02 02:17:15 -0700 | [diff] [blame] | 644 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | |
| 646 | read_lock(&fown->lock); |
Peter Zijlstra | ba0a6c9 | 2009-09-23 15:57:03 -0700 | [diff] [blame] | 647 | |
Eric W. Biederman | 609d7fa | 2006-10-02 02:17:15 -0700 | [diff] [blame] | 648 | type = fown->pid_type; |
Peter Zijlstra | ba0a6c9 | 2009-09-23 15:57:03 -0700 | [diff] [blame] | 649 | if (type == PIDTYPE_MAX) { |
| 650 | group = 0; |
| 651 | type = PIDTYPE_PID; |
| 652 | } |
| 653 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | pid = fown->pid; |
| 655 | if (!pid) |
| 656 | goto out_unlock_fown; |
| 657 | |
| 658 | ret = 1; |
| 659 | |
| 660 | read_lock(&tasklist_lock); |
Eric W. Biederman | 609d7fa | 2006-10-02 02:17:15 -0700 | [diff] [blame] | 661 | do_each_pid_task(pid, type, p) { |
Peter Zijlstra | ba0a6c9 | 2009-09-23 15:57:03 -0700 | [diff] [blame] | 662 | send_sigurg_to_task(p, fown, group); |
Eric W. Biederman | 609d7fa | 2006-10-02 02:17:15 -0700 | [diff] [blame] | 663 | } while_each_pid_task(pid, type, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | read_unlock(&tasklist_lock); |
| 665 | out_unlock_fown: |
| 666 | read_unlock(&fown->lock); |
| 667 | return ret; |
| 668 | } |
| 669 | |
Eric Dumazet | 989a297 | 2010-04-14 09:55:35 +0000 | [diff] [blame] | 670 | static DEFINE_SPINLOCK(fasync_lock); |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 671 | static struct kmem_cache *fasync_cache __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | |
Eric Dumazet | 989a297 | 2010-04-14 09:55:35 +0000 | [diff] [blame] | 673 | static void fasync_free_rcu(struct rcu_head *head) |
| 674 | { |
| 675 | kmem_cache_free(fasync_cache, |
| 676 | container_of(head, struct fasync_struct, fa_rcu)); |
| 677 | } |
| 678 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | /* |
Linus Torvalds | 53281b6 | 2009-12-16 08:23:37 -0800 | [diff] [blame] | 680 | * Remove a fasync entry. If successfully removed, return |
| 681 | * positive and clear the FASYNC flag. If no entry exists, |
| 682 | * do nothing and return 0. |
| 683 | * |
| 684 | * NOTE! It is very important that the FASYNC flag always |
| 685 | * match the state "is the filp on a fasync list". |
| 686 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | */ |
Linus Torvalds | f7347ce | 2010-10-27 12:38:12 -0400 | [diff] [blame] | 688 | int fasync_remove_entry(struct file *filp, struct fasync_struct **fapp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | { |
| 690 | struct fasync_struct *fa, **fp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | int result = 0; |
| 692 | |
Jonathan Corbet | 4a6a449 | 2009-03-27 12:24:31 -0600 | [diff] [blame] | 693 | spin_lock(&filp->f_lock); |
Eric Dumazet | 989a297 | 2010-04-14 09:55:35 +0000 | [diff] [blame] | 694 | spin_lock(&fasync_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | for (fp = fapp; (fa = *fp) != NULL; fp = &fa->fa_next) { |
Linus Torvalds | 53281b6 | 2009-12-16 08:23:37 -0800 | [diff] [blame] | 696 | if (fa->fa_file != filp) |
| 697 | continue; |
Eric Dumazet | 989a297 | 2010-04-14 09:55:35 +0000 | [diff] [blame] | 698 | |
| 699 | spin_lock_irq(&fa->fa_lock); |
| 700 | fa->fa_file = NULL; |
| 701 | spin_unlock_irq(&fa->fa_lock); |
| 702 | |
Linus Torvalds | 53281b6 | 2009-12-16 08:23:37 -0800 | [diff] [blame] | 703 | *fp = fa->fa_next; |
Eric Dumazet | 989a297 | 2010-04-14 09:55:35 +0000 | [diff] [blame] | 704 | call_rcu(&fa->fa_rcu, fasync_free_rcu); |
Jonathan Corbet | 7639842 | 2009-02-01 14:26:59 -0700 | [diff] [blame] | 705 | filp->f_flags &= ~FASYNC; |
Linus Torvalds | 53281b6 | 2009-12-16 08:23:37 -0800 | [diff] [blame] | 706 | result = 1; |
| 707 | break; |
| 708 | } |
Eric Dumazet | 989a297 | 2010-04-14 09:55:35 +0000 | [diff] [blame] | 709 | spin_unlock(&fasync_lock); |
Jonathan Corbet | 4a6a449 | 2009-03-27 12:24:31 -0600 | [diff] [blame] | 710 | spin_unlock(&filp->f_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | return result; |
| 712 | } |
| 713 | |
Linus Torvalds | f7347ce | 2010-10-27 12:38:12 -0400 | [diff] [blame] | 714 | struct fasync_struct *fasync_alloc(void) |
Linus Torvalds | 53281b6 | 2009-12-16 08:23:37 -0800 | [diff] [blame] | 715 | { |
Linus Torvalds | f7347ce | 2010-10-27 12:38:12 -0400 | [diff] [blame] | 716 | return kmem_cache_alloc(fasync_cache, GFP_KERNEL); |
| 717 | } |
Linus Torvalds | 53281b6 | 2009-12-16 08:23:37 -0800 | [diff] [blame] | 718 | |
Linus Torvalds | f7347ce | 2010-10-27 12:38:12 -0400 | [diff] [blame] | 719 | /* |
| 720 | * NOTE! This can be used only for unused fasync entries: |
| 721 | * entries that actually got inserted on the fasync list |
| 722 | * need to be released by rcu - see fasync_remove_entry. |
| 723 | */ |
| 724 | void fasync_free(struct fasync_struct *new) |
| 725 | { |
| 726 | kmem_cache_free(fasync_cache, new); |
| 727 | } |
| 728 | |
| 729 | /* |
| 730 | * Insert a new entry into the fasync list. Return the pointer to the |
| 731 | * old one if we didn't use the new one. |
Linus Torvalds | 55f335a | 2010-10-27 18:17:02 -0700 | [diff] [blame] | 732 | * |
| 733 | * NOTE! It is very important that the FASYNC flag always |
| 734 | * match the state "is the filp on a fasync list". |
Linus Torvalds | f7347ce | 2010-10-27 12:38:12 -0400 | [diff] [blame] | 735 | */ |
| 736 | struct fasync_struct *fasync_insert_entry(int fd, struct file *filp, struct fasync_struct **fapp, struct fasync_struct *new) |
| 737 | { |
| 738 | struct fasync_struct *fa, **fp; |
Linus Torvalds | 53281b6 | 2009-12-16 08:23:37 -0800 | [diff] [blame] | 739 | |
| 740 | spin_lock(&filp->f_lock); |
Eric Dumazet | 989a297 | 2010-04-14 09:55:35 +0000 | [diff] [blame] | 741 | spin_lock(&fasync_lock); |
Linus Torvalds | 53281b6 | 2009-12-16 08:23:37 -0800 | [diff] [blame] | 742 | for (fp = fapp; (fa = *fp) != NULL; fp = &fa->fa_next) { |
| 743 | if (fa->fa_file != filp) |
| 744 | continue; |
Eric Dumazet | 989a297 | 2010-04-14 09:55:35 +0000 | [diff] [blame] | 745 | |
| 746 | spin_lock_irq(&fa->fa_lock); |
Linus Torvalds | 53281b6 | 2009-12-16 08:23:37 -0800 | [diff] [blame] | 747 | fa->fa_fd = fd; |
Eric Dumazet | 989a297 | 2010-04-14 09:55:35 +0000 | [diff] [blame] | 748 | spin_unlock_irq(&fa->fa_lock); |
Linus Torvalds | 53281b6 | 2009-12-16 08:23:37 -0800 | [diff] [blame] | 749 | goto out; |
| 750 | } |
| 751 | |
Eric Dumazet | 989a297 | 2010-04-14 09:55:35 +0000 | [diff] [blame] | 752 | spin_lock_init(&new->fa_lock); |
Linus Torvalds | 53281b6 | 2009-12-16 08:23:37 -0800 | [diff] [blame] | 753 | new->magic = FASYNC_MAGIC; |
| 754 | new->fa_file = filp; |
| 755 | new->fa_fd = fd; |
| 756 | new->fa_next = *fapp; |
Eric Dumazet | 989a297 | 2010-04-14 09:55:35 +0000 | [diff] [blame] | 757 | rcu_assign_pointer(*fapp, new); |
Linus Torvalds | 53281b6 | 2009-12-16 08:23:37 -0800 | [diff] [blame] | 758 | filp->f_flags |= FASYNC; |
| 759 | |
| 760 | out: |
Eric Dumazet | 989a297 | 2010-04-14 09:55:35 +0000 | [diff] [blame] | 761 | spin_unlock(&fasync_lock); |
Linus Torvalds | 53281b6 | 2009-12-16 08:23:37 -0800 | [diff] [blame] | 762 | spin_unlock(&filp->f_lock); |
Linus Torvalds | f7347ce | 2010-10-27 12:38:12 -0400 | [diff] [blame] | 763 | return fa; |
| 764 | } |
| 765 | |
| 766 | /* |
| 767 | * Add a fasync entry. Return negative on error, positive if |
| 768 | * added, and zero if did nothing but change an existing one. |
Linus Torvalds | f7347ce | 2010-10-27 12:38:12 -0400 | [diff] [blame] | 769 | */ |
| 770 | static int fasync_add_entry(int fd, struct file *filp, struct fasync_struct **fapp) |
| 771 | { |
| 772 | struct fasync_struct *new; |
| 773 | |
| 774 | new = fasync_alloc(); |
| 775 | if (!new) |
| 776 | return -ENOMEM; |
| 777 | |
| 778 | /* |
| 779 | * fasync_insert_entry() returns the old (update) entry if |
| 780 | * it existed. |
| 781 | * |
| 782 | * So free the (unused) new entry and return 0 to let the |
| 783 | * caller know that we didn't add any new fasync entries. |
| 784 | */ |
| 785 | if (fasync_insert_entry(fd, filp, fapp, new)) { |
| 786 | fasync_free(new); |
| 787 | return 0; |
| 788 | } |
| 789 | |
| 790 | return 1; |
Linus Torvalds | 53281b6 | 2009-12-16 08:23:37 -0800 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | /* |
| 794 | * fasync_helper() is used by almost all character device drivers |
| 795 | * to set up the fasync queue, and for regular files by the file |
| 796 | * lease code. It returns negative on error, 0 if it did no changes |
| 797 | * and positive if it added/deleted the entry. |
| 798 | */ |
| 799 | int fasync_helper(int fd, struct file * filp, int on, struct fasync_struct **fapp) |
| 800 | { |
| 801 | if (!on) |
| 802 | return fasync_remove_entry(filp, fapp); |
| 803 | return fasync_add_entry(fd, filp, fapp); |
| 804 | } |
| 805 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | EXPORT_SYMBOL(fasync_helper); |
| 807 | |
Eric Dumazet | 989a297 | 2010-04-14 09:55:35 +0000 | [diff] [blame] | 808 | /* |
| 809 | * rcu_read_lock() is held |
| 810 | */ |
| 811 | static void kill_fasync_rcu(struct fasync_struct *fa, int sig, int band) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | { |
| 813 | while (fa) { |
Eric Dumazet | 989a297 | 2010-04-14 09:55:35 +0000 | [diff] [blame] | 814 | struct fown_struct *fown; |
Andrew Morton | f4985dc | 2010-06-29 15:05:42 -0700 | [diff] [blame] | 815 | unsigned long flags; |
| 816 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | if (fa->magic != FASYNC_MAGIC) { |
| 818 | printk(KERN_ERR "kill_fasync: bad magic number in " |
| 819 | "fasync_struct!\n"); |
| 820 | return; |
| 821 | } |
Andrew Morton | f4985dc | 2010-06-29 15:05:42 -0700 | [diff] [blame] | 822 | spin_lock_irqsave(&fa->fa_lock, flags); |
Eric Dumazet | 989a297 | 2010-04-14 09:55:35 +0000 | [diff] [blame] | 823 | if (fa->fa_file) { |
| 824 | fown = &fa->fa_file->f_owner; |
| 825 | /* Don't send SIGURG to processes which have not set a |
| 826 | queued signum: SIGURG has its own default signalling |
| 827 | mechanism. */ |
| 828 | if (!(sig == SIGURG && fown->signum == 0)) |
| 829 | send_sigio(fown, fa->fa_fd, band); |
| 830 | } |
Andrew Morton | f4985dc | 2010-06-29 15:05:42 -0700 | [diff] [blame] | 831 | spin_unlock_irqrestore(&fa->fa_lock, flags); |
Eric Dumazet | 989a297 | 2010-04-14 09:55:35 +0000 | [diff] [blame] | 832 | fa = rcu_dereference(fa->fa_next); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 | } |
| 834 | } |
| 835 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | void kill_fasync(struct fasync_struct **fp, int sig, int band) |
| 837 | { |
| 838 | /* First a quick test without locking: usually |
| 839 | * the list is empty. |
| 840 | */ |
| 841 | if (*fp) { |
Eric Dumazet | 989a297 | 2010-04-14 09:55:35 +0000 | [diff] [blame] | 842 | rcu_read_lock(); |
| 843 | kill_fasync_rcu(rcu_dereference(*fp), sig, band); |
| 844 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | } |
| 846 | } |
| 847 | EXPORT_SYMBOL(kill_fasync); |
| 848 | |
Wu Fengguang | 454eedb | 2010-08-10 18:01:29 -0700 | [diff] [blame] | 849 | static int __init fcntl_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | { |
James Bottomley | 3ab04d5 | 2010-09-09 16:38:12 -0700 | [diff] [blame] | 851 | /* |
| 852 | * Please add new bits here to ensure allocation uniqueness. |
| 853 | * Exceptions: O_NONBLOCK is a two bit define on parisc; O_NDELAY |
| 854 | * is defined as O_NONBLOCK on some platforms and not on others. |
| 855 | */ |
Zheng Liu | f421b79 | 2013-07-25 08:13:19 +0800 | [diff] [blame] | 856 | BUILD_BUG_ON(20 - 1 /* for O_RDONLY being 0 */ != HWEIGHT32( |
Wu Fengguang | 454eedb | 2010-08-10 18:01:29 -0700 | [diff] [blame] | 857 | O_RDONLY | O_WRONLY | O_RDWR | |
| 858 | O_CREAT | O_EXCL | O_NOCTTY | |
James Bottomley | 3ab04d5 | 2010-09-09 16:38:12 -0700 | [diff] [blame] | 859 | O_TRUNC | O_APPEND | /* O_NONBLOCK | */ |
Wu Fengguang | 454eedb | 2010-08-10 18:01:29 -0700 | [diff] [blame] | 860 | __O_SYNC | O_DSYNC | FASYNC | |
| 861 | O_DIRECT | O_LARGEFILE | O_DIRECTORY | |
| 862 | O_NOFOLLOW | O_NOATIME | O_CLOEXEC | |
Zheng Liu | f421b79 | 2013-07-25 08:13:19 +0800 | [diff] [blame] | 863 | __FMODE_EXEC | O_PATH | __O_TMPFILE |
Wu Fengguang | 454eedb | 2010-08-10 18:01:29 -0700 | [diff] [blame] | 864 | )); |
| 865 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | fasync_cache = kmem_cache_create("fasync_cache", |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 867 | sizeof(struct fasync_struct), 0, SLAB_PANIC, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | return 0; |
| 869 | } |
| 870 | |
Wu Fengguang | 454eedb | 2010-08-10 18:01:29 -0700 | [diff] [blame] | 871 | module_init(fcntl_init) |