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> |
| 15 | #include <linux/smp_lock.h> |
| 16 | #include <linux/slab.h> |
| 17 | #include <linux/module.h> |
| 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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
| 24 | #include <asm/poll.h> |
| 25 | #include <asm/siginfo.h> |
| 26 | #include <asm/uaccess.h> |
| 27 | |
Harvey Harrison | fc9b52c | 2008-02-08 04:19:52 -0800 | [diff] [blame] | 28 | void set_close_on_exec(unsigned int fd, int flag) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | { |
| 30 | struct files_struct *files = current->files; |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 31 | struct fdtable *fdt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | spin_lock(&files->file_lock); |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 33 | fdt = files_fdtable(files); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | if (flag) |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 35 | FD_SET(fd, fdt->close_on_exec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | else |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 37 | FD_CLR(fd, fdt->close_on_exec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | spin_unlock(&files->file_lock); |
| 39 | } |
| 40 | |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 41 | static int get_close_on_exec(unsigned int fd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | { |
| 43 | struct files_struct *files = current->files; |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 44 | struct fdtable *fdt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | int res; |
Dipankar Sarma | b835996 | 2005-09-09 13:04:14 -0700 | [diff] [blame] | 46 | rcu_read_lock(); |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 47 | fdt = files_fdtable(files); |
| 48 | res = FD_ISSET(fd, fdt->close_on_exec); |
Dipankar Sarma | b835996 | 2005-09-09 13:04:14 -0700 | [diff] [blame] | 49 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | return res; |
| 51 | } |
| 52 | |
| 53 | /* |
| 54 | * locate_fd finds a free file descriptor in the open_fds fdset, |
| 55 | * expanding the fd arrays if necessary. Must be called with the |
| 56 | * file_lock held for write. |
| 57 | */ |
| 58 | |
Al Viro | f8f9570 | 2008-04-23 20:38:10 -0400 | [diff] [blame] | 59 | static int locate_fd(unsigned int orig_start, int cloexec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | { |
Al Viro | f8f9570 | 2008-04-23 20:38:10 -0400 | [diff] [blame] | 61 | struct files_struct *files = current->files; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | unsigned int newfd; |
| 63 | unsigned int start; |
| 64 | int error; |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 65 | struct fdtable *fdt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | |
Al Viro | f8f9570 | 2008-04-23 20:38:10 -0400 | [diff] [blame] | 67 | spin_lock(&files->file_lock); |
| 68 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | error = -EINVAL; |
| 70 | if (orig_start >= current->signal->rlim[RLIMIT_NOFILE].rlim_cur) |
| 71 | goto out; |
| 72 | |
| 73 | repeat: |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 74 | fdt = files_fdtable(files); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | /* |
| 76 | * Someone might have closed fd's in the range |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 77 | * orig_start..fdt->next_fd |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | */ |
| 79 | start = orig_start; |
Eric Dumazet | 0c9e63f | 2006-03-23 03:00:12 -0800 | [diff] [blame] | 80 | if (start < files->next_fd) |
| 81 | start = files->next_fd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | |
| 83 | newfd = start; |
Vadim Lobanov | bbea9f6 | 2006-12-10 02:21:12 -0800 | [diff] [blame] | 84 | if (start < fdt->max_fds) |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 85 | newfd = find_next_zero_bit(fdt->open_fds->fds_bits, |
Vadim Lobanov | bbea9f6 | 2006-12-10 02:21:12 -0800 | [diff] [blame] | 86 | fdt->max_fds, start); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | |
| 88 | error = -EMFILE; |
| 89 | if (newfd >= current->signal->rlim[RLIMIT_NOFILE].rlim_cur) |
| 90 | goto out; |
| 91 | |
| 92 | error = expand_files(files, newfd); |
| 93 | if (error < 0) |
| 94 | goto out; |
| 95 | |
| 96 | /* |
| 97 | * If we needed to expand the fs array we |
| 98 | * might have blocked - try again. |
| 99 | */ |
| 100 | if (error) |
| 101 | goto repeat; |
| 102 | |
Eric Dumazet | 0c9e63f | 2006-03-23 03:00:12 -0800 | [diff] [blame] | 103 | if (start <= files->next_fd) |
| 104 | files->next_fd = newfd + 1; |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 105 | |
Al Viro | f8f9570 | 2008-04-23 20:38:10 -0400 | [diff] [blame] | 106 | FD_SET(newfd, fdt->open_fds); |
| 107 | if (cloexec) |
| 108 | FD_SET(newfd, fdt->close_on_exec); |
| 109 | else |
| 110 | FD_CLR(newfd, fdt->close_on_exec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | error = newfd; |
Al Viro | f8f9570 | 2008-04-23 20:38:10 -0400 | [diff] [blame] | 112 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | out: |
Al Viro | f8f9570 | 2008-04-23 20:38:10 -0400 | [diff] [blame] | 114 | spin_unlock(&files->file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | return error; |
| 116 | } |
| 117 | |
Ulrich Drepper | 22d2b35 | 2007-10-16 23:30:26 -0700 | [diff] [blame] | 118 | static int dupfd(struct file *file, unsigned int start, int cloexec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | { |
Al Viro | f8f9570 | 2008-04-23 20:38:10 -0400 | [diff] [blame] | 120 | int fd = locate_fd(start, cloexec); |
| 121 | if (fd >= 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | fd_install(fd, file); |
Al Viro | f8f9570 | 2008-04-23 20:38:10 -0400 | [diff] [blame] | 123 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | fput(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | |
| 126 | return fd; |
| 127 | } |
| 128 | |
| 129 | asmlinkage long sys_dup2(unsigned int oldfd, unsigned int newfd) |
| 130 | { |
| 131 | int err = -EBADF; |
| 132 | struct file * file, *tofree; |
| 133 | struct files_struct * files = current->files; |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 134 | struct fdtable *fdt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | |
| 136 | spin_lock(&files->file_lock); |
| 137 | if (!(file = fcheck(oldfd))) |
| 138 | goto out_unlock; |
| 139 | err = newfd; |
| 140 | if (newfd == oldfd) |
| 141 | goto out_unlock; |
| 142 | err = -EBADF; |
| 143 | if (newfd >= current->signal->rlim[RLIMIT_NOFILE].rlim_cur) |
| 144 | goto out_unlock; |
| 145 | get_file(file); /* We are now finished with oldfd */ |
| 146 | |
| 147 | err = expand_files(files, newfd); |
| 148 | if (err < 0) |
| 149 | goto out_fput; |
| 150 | |
| 151 | /* To avoid races with open() and dup(), we will mark the fd as |
| 152 | * in-use in the open-file bitmap throughout the entire dup2() |
| 153 | * process. This is quite safe: do_close() uses the fd array |
| 154 | * entry, not the bitmap, to decide what work needs to be |
| 155 | * done. --sct */ |
| 156 | /* Doesn't work. open() might be there first. --AV */ |
| 157 | |
| 158 | /* Yes. It's a race. In user space. Nothing sane to do */ |
| 159 | err = -EBUSY; |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 160 | fdt = files_fdtable(files); |
| 161 | tofree = fdt->fd[newfd]; |
| 162 | if (!tofree && FD_ISSET(newfd, fdt->open_fds)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | goto out_fput; |
| 164 | |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 165 | rcu_assign_pointer(fdt->fd[newfd], file); |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 166 | FD_SET(newfd, fdt->open_fds); |
| 167 | FD_CLR(newfd, fdt->close_on_exec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | spin_unlock(&files->file_lock); |
| 169 | |
| 170 | if (tofree) |
| 171 | filp_close(tofree, files); |
| 172 | err = newfd; |
| 173 | out: |
| 174 | return err; |
| 175 | out_unlock: |
| 176 | spin_unlock(&files->file_lock); |
| 177 | goto out; |
| 178 | |
| 179 | out_fput: |
| 180 | spin_unlock(&files->file_lock); |
| 181 | fput(file); |
| 182 | goto out; |
| 183 | } |
| 184 | |
| 185 | asmlinkage long sys_dup(unsigned int fildes) |
| 186 | { |
| 187 | int ret = -EBADF; |
| 188 | struct file * file = fget(fildes); |
| 189 | |
| 190 | if (file) |
Ulrich Drepper | 22d2b35 | 2007-10-16 23:30:26 -0700 | [diff] [blame] | 191 | ret = dupfd(file, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | return ret; |
| 193 | } |
| 194 | |
| 195 | #define SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | FASYNC | O_DIRECT | O_NOATIME) |
| 196 | |
| 197 | static int setfl(int fd, struct file * filp, unsigned long arg) |
| 198 | { |
Josef "Jeff" Sipek | 0f7fc9e | 2006-12-08 02:36:35 -0800 | [diff] [blame] | 199 | struct inode * inode = filp->f_path.dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | int error = 0; |
| 201 | |
dean gaudet | 7d95c8f | 2006-02-03 03:04:30 -0800 | [diff] [blame] | 202 | /* |
| 203 | * O_APPEND cannot be cleared if the file is marked as append-only |
| 204 | * and the file is open for write. |
| 205 | */ |
| 206 | if (((arg ^ filp->f_flags) & O_APPEND) && IS_APPEND(inode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | return -EPERM; |
| 208 | |
| 209 | /* O_NOATIME can only be set by the owner or superuser */ |
| 210 | if ((arg & O_NOATIME) && !(filp->f_flags & O_NOATIME)) |
Satyam Sharma | 3bd858a | 2007-07-17 15:00:08 +0530 | [diff] [blame] | 211 | if (!is_owner_or_cap(inode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | return -EPERM; |
| 213 | |
| 214 | /* required for strict SunOS emulation */ |
| 215 | if (O_NONBLOCK != O_NDELAY) |
| 216 | if (arg & O_NDELAY) |
| 217 | arg |= O_NONBLOCK; |
| 218 | |
| 219 | if (arg & O_DIRECT) { |
| 220 | if (!filp->f_mapping || !filp->f_mapping->a_ops || |
| 221 | !filp->f_mapping->a_ops->direct_IO) |
| 222 | return -EINVAL; |
| 223 | } |
| 224 | |
| 225 | if (filp->f_op && filp->f_op->check_flags) |
| 226 | error = filp->f_op->check_flags(arg); |
| 227 | if (error) |
| 228 | return error; |
| 229 | |
| 230 | lock_kernel(); |
| 231 | if ((arg ^ filp->f_flags) & FASYNC) { |
| 232 | if (filp->f_op && filp->f_op->fasync) { |
| 233 | error = filp->f_op->fasync(fd, filp, (arg & FASYNC) != 0); |
| 234 | if (error < 0) |
| 235 | goto out; |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | filp->f_flags = (arg & SETFL_MASK) | (filp->f_flags & ~SETFL_MASK); |
| 240 | out: |
| 241 | unlock_kernel(); |
| 242 | return error; |
| 243 | } |
| 244 | |
Eric W. Biederman | 609d7fa | 2006-10-02 02:17:15 -0700 | [diff] [blame] | 245 | static void f_modown(struct file *filp, struct pid *pid, enum pid_type type, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | uid_t uid, uid_t euid, int force) |
| 247 | { |
| 248 | write_lock_irq(&filp->f_owner.lock); |
| 249 | if (force || !filp->f_owner.pid) { |
Eric W. Biederman | 609d7fa | 2006-10-02 02:17:15 -0700 | [diff] [blame] | 250 | put_pid(filp->f_owner.pid); |
| 251 | filp->f_owner.pid = get_pid(pid); |
| 252 | filp->f_owner.pid_type = type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | filp->f_owner.uid = uid; |
| 254 | filp->f_owner.euid = euid; |
| 255 | } |
| 256 | write_unlock_irq(&filp->f_owner.lock); |
| 257 | } |
| 258 | |
Eric W. Biederman | 609d7fa | 2006-10-02 02:17:15 -0700 | [diff] [blame] | 259 | int __f_setown(struct file *filp, struct pid *pid, enum pid_type type, |
| 260 | int force) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | { |
| 262 | int err; |
| 263 | |
| 264 | err = security_file_set_fowner(filp); |
| 265 | if (err) |
| 266 | return err; |
| 267 | |
Eric W. Biederman | 609d7fa | 2006-10-02 02:17:15 -0700 | [diff] [blame] | 268 | f_modown(filp, pid, type, current->uid, current->euid, force); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | return 0; |
| 270 | } |
Eric W. Biederman | 609d7fa | 2006-10-02 02:17:15 -0700 | [diff] [blame] | 271 | EXPORT_SYMBOL(__f_setown); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | |
Eric W. Biederman | 609d7fa | 2006-10-02 02:17:15 -0700 | [diff] [blame] | 273 | int f_setown(struct file *filp, unsigned long arg, int force) |
| 274 | { |
| 275 | enum pid_type type; |
| 276 | struct pid *pid; |
| 277 | int who = arg; |
| 278 | int result; |
| 279 | type = PIDTYPE_PID; |
| 280 | if (who < 0) { |
| 281 | type = PIDTYPE_PGID; |
| 282 | who = -who; |
| 283 | } |
| 284 | rcu_read_lock(); |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 285 | pid = find_vpid(who); |
Eric W. Biederman | 609d7fa | 2006-10-02 02:17:15 -0700 | [diff] [blame] | 286 | result = __f_setown(filp, pid, type, force); |
| 287 | rcu_read_unlock(); |
| 288 | return result; |
| 289 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | EXPORT_SYMBOL(f_setown); |
| 291 | |
| 292 | void f_delown(struct file *filp) |
| 293 | { |
Eric W. Biederman | 609d7fa | 2006-10-02 02:17:15 -0700 | [diff] [blame] | 294 | f_modown(filp, NULL, PIDTYPE_PID, 0, 0, 1); |
| 295 | } |
| 296 | |
| 297 | pid_t f_getown(struct file *filp) |
| 298 | { |
| 299 | pid_t pid; |
Eric W. Biederman | 43fa1adb | 2006-10-02 02:17:27 -0700 | [diff] [blame] | 300 | read_lock(&filp->f_owner.lock); |
Pavel Emelyanov | 6c5f3e7 | 2008-02-08 04:19:20 -0800 | [diff] [blame] | 301 | pid = pid_vnr(filp->f_owner.pid); |
Eric W. Biederman | 609d7fa | 2006-10-02 02:17:15 -0700 | [diff] [blame] | 302 | if (filp->f_owner.pid_type == PIDTYPE_PGID) |
| 303 | pid = -pid; |
Eric W. Biederman | 43fa1adb | 2006-10-02 02:17:27 -0700 | [diff] [blame] | 304 | read_unlock(&filp->f_owner.lock); |
Eric W. Biederman | 609d7fa | 2006-10-02 02:17:15 -0700 | [diff] [blame] | 305 | return pid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | static long do_fcntl(int fd, unsigned int cmd, unsigned long arg, |
| 309 | struct file *filp) |
| 310 | { |
| 311 | long err = -EINVAL; |
| 312 | |
| 313 | switch (cmd) { |
| 314 | case F_DUPFD: |
Ulrich Drepper | 22d2b35 | 2007-10-16 23:30:26 -0700 | [diff] [blame] | 315 | case F_DUPFD_CLOEXEC: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | get_file(filp); |
Ulrich Drepper | 22d2b35 | 2007-10-16 23:30:26 -0700 | [diff] [blame] | 317 | err = dupfd(filp, arg, cmd == F_DUPFD_CLOEXEC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | break; |
| 319 | case F_GETFD: |
| 320 | err = get_close_on_exec(fd) ? FD_CLOEXEC : 0; |
| 321 | break; |
| 322 | case F_SETFD: |
| 323 | err = 0; |
| 324 | set_close_on_exec(fd, arg & FD_CLOEXEC); |
| 325 | break; |
| 326 | case F_GETFL: |
| 327 | err = filp->f_flags; |
| 328 | break; |
| 329 | case F_SETFL: |
| 330 | err = setfl(fd, filp, arg); |
| 331 | break; |
| 332 | case F_GETLK: |
| 333 | err = fcntl_getlk(filp, (struct flock __user *) arg); |
| 334 | break; |
| 335 | case F_SETLK: |
| 336 | case F_SETLKW: |
Peter Staubach | c293621 | 2005-07-27 11:45:09 -0700 | [diff] [blame] | 337 | err = fcntl_setlk(fd, filp, cmd, (struct flock __user *) arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | break; |
| 339 | case F_GETOWN: |
| 340 | /* |
| 341 | * XXX If f_owner is a process group, the |
| 342 | * negative return value will get converted |
| 343 | * into an error. Oops. If we keep the |
| 344 | * current syscall conventions, the only way |
| 345 | * to fix this will be in libc. |
| 346 | */ |
Eric W. Biederman | 609d7fa | 2006-10-02 02:17:15 -0700 | [diff] [blame] | 347 | err = f_getown(filp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | force_successful_syscall_return(); |
| 349 | break; |
| 350 | case F_SETOWN: |
| 351 | err = f_setown(filp, arg, 1); |
| 352 | break; |
| 353 | case F_GETSIG: |
| 354 | err = filp->f_owner.signum; |
| 355 | break; |
| 356 | case F_SETSIG: |
| 357 | /* arg == 0 restores default behaviour. */ |
Jesper Juhl | 7ed20e1 | 2005-05-01 08:59:14 -0700 | [diff] [blame] | 358 | if (!valid_signal(arg)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | break; |
| 360 | } |
| 361 | err = 0; |
| 362 | filp->f_owner.signum = arg; |
| 363 | break; |
| 364 | case F_GETLEASE: |
| 365 | err = fcntl_getlease(filp); |
| 366 | break; |
| 367 | case F_SETLEASE: |
| 368 | err = fcntl_setlease(fd, filp, arg); |
| 369 | break; |
| 370 | case F_NOTIFY: |
| 371 | err = fcntl_dirnotify(fd, filp, arg); |
| 372 | break; |
| 373 | default: |
| 374 | break; |
| 375 | } |
| 376 | return err; |
| 377 | } |
| 378 | |
| 379 | asmlinkage long sys_fcntl(unsigned int fd, unsigned int cmd, unsigned long arg) |
| 380 | { |
| 381 | struct file *filp; |
| 382 | long err = -EBADF; |
| 383 | |
| 384 | filp = fget(fd); |
| 385 | if (!filp) |
| 386 | goto out; |
| 387 | |
| 388 | err = security_file_fcntl(filp, cmd, arg); |
| 389 | if (err) { |
| 390 | fput(filp); |
| 391 | return err; |
| 392 | } |
| 393 | |
| 394 | err = do_fcntl(fd, cmd, arg, filp); |
| 395 | |
| 396 | fput(filp); |
| 397 | out: |
| 398 | return err; |
| 399 | } |
| 400 | |
| 401 | #if BITS_PER_LONG == 32 |
| 402 | asmlinkage long sys_fcntl64(unsigned int fd, unsigned int cmd, unsigned long arg) |
| 403 | { |
| 404 | struct file * filp; |
| 405 | long err; |
| 406 | |
| 407 | err = -EBADF; |
| 408 | filp = fget(fd); |
| 409 | if (!filp) |
| 410 | goto out; |
| 411 | |
| 412 | err = security_file_fcntl(filp, cmd, arg); |
| 413 | if (err) { |
| 414 | fput(filp); |
| 415 | return err; |
| 416 | } |
| 417 | err = -EBADF; |
| 418 | |
| 419 | switch (cmd) { |
| 420 | case F_GETLK64: |
| 421 | err = fcntl_getlk64(filp, (struct flock64 __user *) arg); |
| 422 | break; |
| 423 | case F_SETLK64: |
| 424 | case F_SETLKW64: |
Peter Staubach | c293621 | 2005-07-27 11:45:09 -0700 | [diff] [blame] | 425 | err = fcntl_setlk64(fd, filp, cmd, |
| 426 | (struct flock64 __user *) arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | break; |
| 428 | default: |
| 429 | err = do_fcntl(fd, cmd, arg, filp); |
| 430 | break; |
| 431 | } |
| 432 | fput(filp); |
| 433 | out: |
| 434 | return err; |
| 435 | } |
| 436 | #endif |
| 437 | |
| 438 | /* Table to convert sigio signal codes into poll band bitmaps */ |
| 439 | |
Eric Dumazet | fa3536c | 2006-03-26 01:37:24 -0800 | [diff] [blame] | 440 | static const long band_table[NSIGPOLL] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | POLLIN | POLLRDNORM, /* POLL_IN */ |
| 442 | POLLOUT | POLLWRNORM | POLLWRBAND, /* POLL_OUT */ |
| 443 | POLLIN | POLLRDNORM | POLLMSG, /* POLL_MSG */ |
| 444 | POLLERR, /* POLL_ERR */ |
| 445 | POLLPRI | POLLRDBAND, /* POLL_PRI */ |
| 446 | POLLHUP | POLLERR /* POLL_HUP */ |
| 447 | }; |
| 448 | |
| 449 | static inline int sigio_perm(struct task_struct *p, |
| 450 | struct fown_struct *fown, int sig) |
| 451 | { |
| 452 | return (((fown->euid == 0) || |
| 453 | (fown->euid == p->suid) || (fown->euid == p->uid) || |
| 454 | (fown->uid == p->suid) || (fown->uid == p->uid)) && |
| 455 | !security_file_send_sigiotask(p, fown, sig)); |
| 456 | } |
| 457 | |
| 458 | static void send_sigio_to_task(struct task_struct *p, |
| 459 | struct fown_struct *fown, |
| 460 | int fd, |
| 461 | int reason) |
| 462 | { |
| 463 | if (!sigio_perm(p, fown, fown->signum)) |
| 464 | return; |
| 465 | |
| 466 | switch (fown->signum) { |
| 467 | siginfo_t si; |
| 468 | default: |
| 469 | /* Queue a rt signal with the appropriate fd as its |
| 470 | value. We use SI_SIGIO as the source, not |
| 471 | SI_KERNEL, since kernel signals always get |
| 472 | delivered even if we can't queue. Failure to |
| 473 | queue in this case _should_ be reported; we fall |
| 474 | back to SIGIO in that case. --sct */ |
| 475 | si.si_signo = fown->signum; |
| 476 | si.si_errno = 0; |
| 477 | si.si_code = reason; |
| 478 | /* Make sure we are called with one of the POLL_* |
| 479 | reasons, otherwise we could leak kernel stack into |
| 480 | userspace. */ |
Eric Sesterhenn | f6298aa | 2006-04-02 13:37:19 +0200 | [diff] [blame] | 481 | BUG_ON((reason & __SI_MASK) != __SI_POLL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | if (reason - POLL_IN >= NSIGPOLL) |
| 483 | si.si_band = ~0L; |
| 484 | else |
| 485 | si.si_band = band_table[reason - POLL_IN]; |
| 486 | si.si_fd = fd; |
Oleg Nesterov | 850d6fb | 2006-01-08 01:03:29 -0800 | [diff] [blame] | 487 | if (!group_send_sig_info(fown->signum, &si, p)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | break; |
| 489 | /* fall-through: fall back on the old plain SIGIO signal */ |
| 490 | case 0: |
Oleg Nesterov | 850d6fb | 2006-01-08 01:03:29 -0800 | [diff] [blame] | 491 | group_send_sig_info(SIGIO, SEND_SIG_PRIV, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | } |
| 493 | } |
| 494 | |
| 495 | void send_sigio(struct fown_struct *fown, int fd, int band) |
| 496 | { |
| 497 | struct task_struct *p; |
Eric W. Biederman | 609d7fa | 2006-10-02 02:17:15 -0700 | [diff] [blame] | 498 | enum pid_type type; |
| 499 | struct pid *pid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | |
| 501 | read_lock(&fown->lock); |
Eric W. Biederman | 609d7fa | 2006-10-02 02:17:15 -0700 | [diff] [blame] | 502 | type = fown->pid_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | pid = fown->pid; |
| 504 | if (!pid) |
| 505 | goto out_unlock_fown; |
| 506 | |
| 507 | read_lock(&tasklist_lock); |
Eric W. Biederman | 609d7fa | 2006-10-02 02:17:15 -0700 | [diff] [blame] | 508 | do_each_pid_task(pid, type, p) { |
| 509 | send_sigio_to_task(p, fown, fd, band); |
| 510 | } while_each_pid_task(pid, type, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | read_unlock(&tasklist_lock); |
| 512 | out_unlock_fown: |
| 513 | read_unlock(&fown->lock); |
| 514 | } |
| 515 | |
| 516 | static void send_sigurg_to_task(struct task_struct *p, |
| 517 | struct fown_struct *fown) |
| 518 | { |
| 519 | if (sigio_perm(p, fown, SIGURG)) |
Oleg Nesterov | 850d6fb | 2006-01-08 01:03:29 -0800 | [diff] [blame] | 520 | group_send_sig_info(SIGURG, SEND_SIG_PRIV, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | int send_sigurg(struct fown_struct *fown) |
| 524 | { |
| 525 | struct task_struct *p; |
Eric W. Biederman | 609d7fa | 2006-10-02 02:17:15 -0700 | [diff] [blame] | 526 | enum pid_type type; |
| 527 | struct pid *pid; |
| 528 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | |
| 530 | read_lock(&fown->lock); |
Eric W. Biederman | 609d7fa | 2006-10-02 02:17:15 -0700 | [diff] [blame] | 531 | type = fown->pid_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | pid = fown->pid; |
| 533 | if (!pid) |
| 534 | goto out_unlock_fown; |
| 535 | |
| 536 | ret = 1; |
| 537 | |
| 538 | read_lock(&tasklist_lock); |
Eric W. Biederman | 609d7fa | 2006-10-02 02:17:15 -0700 | [diff] [blame] | 539 | do_each_pid_task(pid, type, p) { |
| 540 | send_sigurg_to_task(p, fown); |
| 541 | } while_each_pid_task(pid, type, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | read_unlock(&tasklist_lock); |
| 543 | out_unlock_fown: |
| 544 | read_unlock(&fown->lock); |
| 545 | return ret; |
| 546 | } |
| 547 | |
| 548 | static DEFINE_RWLOCK(fasync_lock); |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 549 | static struct kmem_cache *fasync_cache __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | |
| 551 | /* |
| 552 | * fasync_helper() is used by some character device drivers (mainly mice) |
| 553 | * to set up the fasync queue. It returns negative on error, 0 if it did |
| 554 | * no changes and positive if it added/deleted the entry. |
| 555 | */ |
| 556 | int fasync_helper(int fd, struct file * filp, int on, struct fasync_struct **fapp) |
| 557 | { |
| 558 | struct fasync_struct *fa, **fp; |
| 559 | struct fasync_struct *new = NULL; |
| 560 | int result = 0; |
| 561 | |
| 562 | if (on) { |
Christoph Lameter | e94b176 | 2006-12-06 20:33:17 -0800 | [diff] [blame] | 563 | new = kmem_cache_alloc(fasync_cache, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | if (!new) |
| 565 | return -ENOMEM; |
| 566 | } |
| 567 | write_lock_irq(&fasync_lock); |
| 568 | for (fp = fapp; (fa = *fp) != NULL; fp = &fa->fa_next) { |
| 569 | if (fa->fa_file == filp) { |
| 570 | if(on) { |
| 571 | fa->fa_fd = fd; |
| 572 | kmem_cache_free(fasync_cache, new); |
| 573 | } else { |
| 574 | *fp = fa->fa_next; |
| 575 | kmem_cache_free(fasync_cache, fa); |
| 576 | result = 1; |
| 577 | } |
| 578 | goto out; |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | if (on) { |
| 583 | new->magic = FASYNC_MAGIC; |
| 584 | new->fa_file = filp; |
| 585 | new->fa_fd = fd; |
| 586 | new->fa_next = *fapp; |
| 587 | *fapp = new; |
| 588 | result = 1; |
| 589 | } |
| 590 | out: |
| 591 | write_unlock_irq(&fasync_lock); |
| 592 | return result; |
| 593 | } |
| 594 | |
| 595 | EXPORT_SYMBOL(fasync_helper); |
| 596 | |
| 597 | void __kill_fasync(struct fasync_struct *fa, int sig, int band) |
| 598 | { |
| 599 | while (fa) { |
| 600 | struct fown_struct * fown; |
| 601 | if (fa->magic != FASYNC_MAGIC) { |
| 602 | printk(KERN_ERR "kill_fasync: bad magic number in " |
| 603 | "fasync_struct!\n"); |
| 604 | return; |
| 605 | } |
| 606 | fown = &fa->fa_file->f_owner; |
| 607 | /* Don't send SIGURG to processes which have not set a |
| 608 | queued signum: SIGURG has its own default signalling |
| 609 | mechanism. */ |
| 610 | if (!(sig == SIGURG && fown->signum == 0)) |
| 611 | send_sigio(fown, fa->fa_fd, band); |
| 612 | fa = fa->fa_next; |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | EXPORT_SYMBOL(__kill_fasync); |
| 617 | |
| 618 | void kill_fasync(struct fasync_struct **fp, int sig, int band) |
| 619 | { |
| 620 | /* First a quick test without locking: usually |
| 621 | * the list is empty. |
| 622 | */ |
| 623 | if (*fp) { |
| 624 | read_lock(&fasync_lock); |
| 625 | /* reread *fp after obtaining the lock */ |
| 626 | __kill_fasync(*fp, sig, band); |
| 627 | read_unlock(&fasync_lock); |
| 628 | } |
| 629 | } |
| 630 | EXPORT_SYMBOL(kill_fasync); |
| 631 | |
| 632 | static int __init fasync_init(void) |
| 633 | { |
| 634 | fasync_cache = kmem_cache_create("fasync_cache", |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 635 | sizeof(struct fasync_struct), 0, SLAB_PANIC, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | return 0; |
| 637 | } |
| 638 | |
| 639 | module_init(fasync_init) |