blob: 5ef953e6f908ea2d6f68061310a106fd0179d15a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Viro9f3acc32008-04-24 07:44:08 -040012#include <linux/fdtable.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080013#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/dnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/slab.h>
16#include <linux/module.h>
17#include <linux/security.h>
18#include <linux/ptrace.h>
Jesper Juhl7ed20e12005-05-01 08:59:14 -070019#include <linux/signal.h>
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070020#include <linux/rcupdate.h>
Pavel Emelyanovb4888932007-10-18 23:40:14 -070021#include <linux/pid_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23#include <asm/poll.h>
24#include <asm/siginfo.h>
25#include <asm/uaccess.h>
26
Harvey Harrisonfc9b52c2008-02-08 04:19:52 -080027void set_close_on_exec(unsigned int fd, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -070028{
29 struct files_struct *files = current->files;
Dipankar Sarmabadf1662005-09-09 13:04:10 -070030 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 spin_lock(&files->file_lock);
Dipankar Sarmabadf1662005-09-09 13:04:10 -070032 fdt = files_fdtable(files);
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 if (flag)
Dipankar Sarmabadf1662005-09-09 13:04:10 -070034 FD_SET(fd, fdt->close_on_exec);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 else
Dipankar Sarmabadf1662005-09-09 13:04:10 -070036 FD_CLR(fd, fdt->close_on_exec);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 spin_unlock(&files->file_lock);
38}
39
Arjan van de Ven858119e2006-01-14 13:20:43 -080040static int get_close_on_exec(unsigned int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
42 struct files_struct *files = current->files;
Dipankar Sarmabadf1662005-09-09 13:04:10 -070043 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 int res;
Dipankar Sarmab8359962005-09-09 13:04:14 -070045 rcu_read_lock();
Dipankar Sarmabadf1662005-09-09 13:04:10 -070046 fdt = files_fdtable(files);
47 res = FD_ISSET(fd, fdt->close_on_exec);
Dipankar Sarmab8359962005-09-09 13:04:14 -070048 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 return res;
50}
51
Heiko Carstensa26eab22009-01-14 14:14:17 +010052SYSCALL_DEFINE3(dup3, unsigned int, oldfd, unsigned int, newfd, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053{
54 int err = -EBADF;
55 struct file * file, *tofree;
56 struct files_struct * files = current->files;
Dipankar Sarmabadf1662005-09-09 13:04:10 -070057 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Ulrich Drepper336dd1f2008-07-23 21:29:29 -070059 if ((flags & ~O_CLOEXEC) != 0)
60 return -EINVAL;
61
Al Viro6c5d0512008-07-26 13:38:19 -040062 if (unlikely(oldfd == newfd))
63 return -EINVAL;
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 spin_lock(&files->file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 err = expand_files(files, newfd);
Al Viro1b7e1902008-07-30 06:18:03 -040067 file = fcheck(oldfd);
68 if (unlikely(!file))
69 goto Ebadf;
Al Viro4e1e0182008-07-26 16:01:20 -040070 if (unlikely(err < 0)) {
71 if (err == -EMFILE)
Al Viro1b7e1902008-07-30 06:18:03 -040072 goto Ebadf;
73 goto out_unlock;
Al Viro4e1e0182008-07-26 16:01:20 -040074 }
Al Viro1b7e1902008-07-30 06:18:03 -040075 /*
76 * We need to detect attempts to do dup2() over allocated but still
77 * not finished descriptor. NB: OpenBSD avoids that at the price of
78 * extra work in their equivalent of fget() - they insert struct
79 * file immediately after grabbing descriptor, mark it larval if
80 * more work (e.g. actual opening) is needed and make sure that
81 * fget() treats larval files as absent. Potentially interesting,
82 * but while extra work in fget() is trivial, locking implications
83 * and amount of surgery on open()-related paths in VFS are not.
84 * FreeBSD fails with -EBADF in the same situation, NetBSD "solution"
85 * deadlocks in rather amusing ways, AFAICS. All of that is out of
86 * scope of POSIX or SUS, since neither considers shared descriptor
87 * tables and this condition does not arise without those.
88 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 err = -EBUSY;
Dipankar Sarmabadf1662005-09-09 13:04:10 -070090 fdt = files_fdtable(files);
91 tofree = fdt->fd[newfd];
92 if (!tofree && FD_ISSET(newfd, fdt->open_fds))
Al Viro1b7e1902008-07-30 06:18:03 -040093 goto out_unlock;
94 get_file(file);
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070095 rcu_assign_pointer(fdt->fd[newfd], file);
Dipankar Sarmabadf1662005-09-09 13:04:10 -070096 FD_SET(newfd, fdt->open_fds);
Ulrich Drepper336dd1f2008-07-23 21:29:29 -070097 if (flags & O_CLOEXEC)
98 FD_SET(newfd, fdt->close_on_exec);
99 else
100 FD_CLR(newfd, fdt->close_on_exec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 spin_unlock(&files->file_lock);
102
103 if (tofree)
104 filp_close(tofree, files);
Al Viro1b7e1902008-07-30 06:18:03 -0400105
106 return newfd;
107
108Ebadf:
109 err = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110out_unlock:
111 spin_unlock(&files->file_lock);
Al Viro1b7e1902008-07-30 06:18:03 -0400112 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113}
114
Heiko Carstensa26eab22009-01-14 14:14:17 +0100115SYSCALL_DEFINE2(dup2, unsigned int, oldfd, unsigned int, newfd)
Ulrich Drepper336dd1f2008-07-23 21:29:29 -0700116{
Al Viro6c5d0512008-07-26 13:38:19 -0400117 if (unlikely(newfd == oldfd)) { /* corner case */
118 struct files_struct *files = current->files;
Jeff Mahoney2b79bc42009-05-11 14:25:34 -0400119 int retval = oldfd;
120
Al Viro6c5d0512008-07-26 13:38:19 -0400121 rcu_read_lock();
122 if (!fcheck_files(files, oldfd))
Jeff Mahoney2b79bc42009-05-11 14:25:34 -0400123 retval = -EBADF;
Al Viro6c5d0512008-07-26 13:38:19 -0400124 rcu_read_unlock();
Jeff Mahoney2b79bc42009-05-11 14:25:34 -0400125 return retval;
Al Viro6c5d0512008-07-26 13:38:19 -0400126 }
Ulrich Drepper336dd1f2008-07-23 21:29:29 -0700127 return sys_dup3(oldfd, newfd, 0);
128}
129
Heiko Carstensa26eab22009-01-14 14:14:17 +0100130SYSCALL_DEFINE1(dup, unsigned int, fildes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
132 int ret = -EBADF;
Al Viro1027abe2008-07-30 04:13:04 -0400133 struct file *file = fget(fildes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Al Viro1027abe2008-07-30 04:13:04 -0400135 if (file) {
136 ret = get_unused_fd();
137 if (ret >= 0)
138 fd_install(ret, file);
139 else
140 fput(file);
141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 return ret;
143}
144
Jonathan Corbet76398422009-02-01 14:26:59 -0700145#define SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT | O_NOATIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147static int setfl(int fd, struct file * filp, unsigned long arg)
148{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800149 struct inode * inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 int error = 0;
151
dean gaudet7d95c8f2006-02-03 03:04:30 -0800152 /*
153 * O_APPEND cannot be cleared if the file is marked as append-only
154 * and the file is open for write.
155 */
156 if (((arg ^ filp->f_flags) & O_APPEND) && IS_APPEND(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 return -EPERM;
158
159 /* O_NOATIME can only be set by the owner or superuser */
160 if ((arg & O_NOATIME) && !(filp->f_flags & O_NOATIME))
Satyam Sharma3bd858a2007-07-17 15:00:08 +0530161 if (!is_owner_or_cap(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 return -EPERM;
163
164 /* required for strict SunOS emulation */
165 if (O_NONBLOCK != O_NDELAY)
166 if (arg & O_NDELAY)
167 arg |= O_NONBLOCK;
168
169 if (arg & O_DIRECT) {
170 if (!filp->f_mapping || !filp->f_mapping->a_ops ||
171 !filp->f_mapping->a_ops->direct_IO)
172 return -EINVAL;
173 }
174
175 if (filp->f_op && filp->f_op->check_flags)
176 error = filp->f_op->check_flags(arg);
177 if (error)
178 return error;
179
Jonathan Corbet218d11a2008-12-05 16:12:48 -0700180 /*
Jonathan Corbet76398422009-02-01 14:26:59 -0700181 * ->fasync() is responsible for setting the FASYNC bit.
Jonathan Corbet218d11a2008-12-05 16:12:48 -0700182 */
Jonathan Corbet76398422009-02-01 14:26:59 -0700183 if (((arg ^ filp->f_flags) & FASYNC) && filp->f_op &&
184 filp->f_op->fasync) {
185 error = filp->f_op->fasync(fd, filp, (arg & FASYNC) != 0);
186 if (error < 0)
187 goto out;
Jonathan Corbet60aa4922009-02-01 14:52:56 -0700188 if (error > 0)
189 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 }
Jonathan Corbetdb1dd4d2009-02-06 15:25:24 -0700191 spin_lock(&filp->f_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 filp->f_flags = (arg & SETFL_MASK) | (filp->f_flags & ~SETFL_MASK);
Jonathan Corbetdb1dd4d2009-02-06 15:25:24 -0700193 spin_unlock(&filp->f_lock);
Jonathan Corbet76398422009-02-01 14:26:59 -0700194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 return error;
197}
198
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700199static void f_modown(struct file *filp, struct pid *pid, enum pid_type type,
Oleg Nesterov2f38d702009-06-16 22:07:46 +0200200 int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
Greg Kroah-Hartmanb04da8b2010-01-26 15:04:02 -0800202 unsigned long flags;
203
204 write_lock_irqsave(&filp->f_owner.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 if (force || !filp->f_owner.pid) {
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700206 put_pid(filp->f_owner.pid);
207 filp->f_owner.pid = get_pid(pid);
208 filp->f_owner.pid_type = type;
Oleg Nesterov2f38d702009-06-16 22:07:46 +0200209
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 Torvalds1da177e2005-04-16 15:20:36 -0700215 }
Greg Kroah-Hartmanb04da8b2010-01-26 15:04:02 -0800216 write_unlock_irqrestore(&filp->f_owner.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217}
218
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700219int __f_setown(struct file *filp, struct pid *pid, enum pid_type type,
220 int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
222 int err;
Oleg Nesterov2f38d702009-06-16 22:07:46 +0200223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 err = security_file_set_fowner(filp);
225 if (err)
226 return err;
227
Oleg Nesterov2f38d702009-06-16 22:07:46 +0200228 f_modown(filp, pid, type, force);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 return 0;
230}
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700231EXPORT_SYMBOL(__f_setown);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700233int 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 Emelyanovb4888932007-10-18 23:40:14 -0700245 pid = find_vpid(who);
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700246 result = __f_setown(filp, pid, type, force);
247 rcu_read_unlock();
248 return result;
249}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250EXPORT_SYMBOL(f_setown);
251
252void f_delown(struct file *filp)
253{
Oleg Nesterov2f38d702009-06-16 22:07:46 +0200254 f_modown(filp, NULL, PIDTYPE_PID, 1);
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700255}
256
257pid_t f_getown(struct file *filp)
258{
259 pid_t pid;
Eric W. Biederman43fa1ad2006-10-02 02:17:27 -0700260 read_lock(&filp->f_owner.lock);
Pavel Emelyanov6c5f3e72008-02-08 04:19:20 -0800261 pid = pid_vnr(filp->f_owner.pid);
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700262 if (filp->f_owner.pid_type == PIDTYPE_PGID)
263 pid = -pid;
Eric W. Biederman43fa1ad2006-10-02 02:17:27 -0700264 read_unlock(&filp->f_owner.lock);
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700265 return pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266}
267
Peter Zijlstraba0a6c92009-09-23 15:57:03 -0700268static 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)
278 return ret;
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 Zijlstra978b4052009-11-17 14:06:24 -0800289 case F_OWNER_PGRP:
Peter Zijlstraba0a6c92009-09-23 15:57:03 -0700290 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
308static 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 Zijlstra978b4052009-11-17 14:06:24 -0800326 owner.type = F_OWNER_PGRP;
Peter Zijlstraba0a6c92009-09-23 15:57:03 -0700327 break;
328
329 default:
330 WARN_ON(1);
331 ret = -EINVAL;
332 break;
333 }
334 read_unlock(&filp->f_owner.lock);
335
336 if (!ret)
337 ret = copy_to_user(owner_p, &owner, sizeof(owner));
338 return ret;
339}
340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341static long do_fcntl(int fd, unsigned int cmd, unsigned long arg,
342 struct file *filp)
343{
344 long err = -EINVAL;
345
346 switch (cmd) {
347 case F_DUPFD:
Ulrich Drepper22d2b352007-10-16 23:30:26 -0700348 case F_DUPFD_CLOEXEC:
Al Viro4e1e0182008-07-26 16:01:20 -0400349 if (arg >= current->signal->rlim[RLIMIT_NOFILE].rlim_cur)
350 break;
Al Viro1027abe2008-07-30 04:13:04 -0400351 err = alloc_fd(arg, cmd == F_DUPFD_CLOEXEC ? O_CLOEXEC : 0);
352 if (err >= 0) {
353 get_file(filp);
354 fd_install(err, filp);
355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 break;
357 case F_GETFD:
358 err = get_close_on_exec(fd) ? FD_CLOEXEC : 0;
359 break;
360 case F_SETFD:
361 err = 0;
362 set_close_on_exec(fd, arg & FD_CLOEXEC);
363 break;
364 case F_GETFL:
365 err = filp->f_flags;
366 break;
367 case F_SETFL:
368 err = setfl(fd, filp, arg);
369 break;
370 case F_GETLK:
371 err = fcntl_getlk(filp, (struct flock __user *) arg);
372 break;
373 case F_SETLK:
374 case F_SETLKW:
Peter Staubachc2936212005-07-27 11:45:09 -0700375 err = fcntl_setlk(fd, filp, cmd, (struct flock __user *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 break;
377 case F_GETOWN:
378 /*
379 * XXX If f_owner is a process group, the
380 * negative return value will get converted
381 * into an error. Oops. If we keep the
382 * current syscall conventions, the only way
383 * to fix this will be in libc.
384 */
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700385 err = f_getown(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 force_successful_syscall_return();
387 break;
388 case F_SETOWN:
389 err = f_setown(filp, arg, 1);
390 break;
Peter Zijlstraba0a6c92009-09-23 15:57:03 -0700391 case F_GETOWN_EX:
392 err = f_getown_ex(filp, arg);
393 break;
394 case F_SETOWN_EX:
395 err = f_setown_ex(filp, arg);
396 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 case F_GETSIG:
398 err = filp->f_owner.signum;
399 break;
400 case F_SETSIG:
401 /* arg == 0 restores default behaviour. */
Jesper Juhl7ed20e12005-05-01 08:59:14 -0700402 if (!valid_signal(arg)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 break;
404 }
405 err = 0;
406 filp->f_owner.signum = arg;
407 break;
408 case F_GETLEASE:
409 err = fcntl_getlease(filp);
410 break;
411 case F_SETLEASE:
412 err = fcntl_setlease(fd, filp, arg);
413 break;
414 case F_NOTIFY:
415 err = fcntl_dirnotify(fd, filp, arg);
416 break;
417 default:
418 break;
419 }
420 return err;
421}
422
Heiko Carstensa26eab22009-01-14 14:14:17 +0100423SYSCALL_DEFINE3(fcntl, unsigned int, fd, unsigned int, cmd, unsigned long, arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
425 struct file *filp;
426 long err = -EBADF;
427
428 filp = fget(fd);
429 if (!filp)
430 goto out;
431
432 err = security_file_fcntl(filp, cmd, arg);
433 if (err) {
434 fput(filp);
435 return err;
436 }
437
438 err = do_fcntl(fd, cmd, arg, filp);
439
440 fput(filp);
441out:
442 return err;
443}
444
445#if BITS_PER_LONG == 32
Heiko Carstensa26eab22009-01-14 14:14:17 +0100446SYSCALL_DEFINE3(fcntl64, unsigned int, fd, unsigned int, cmd,
447 unsigned long, arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
449 struct file * filp;
450 long err;
451
452 err = -EBADF;
453 filp = fget(fd);
454 if (!filp)
455 goto out;
456
457 err = security_file_fcntl(filp, cmd, arg);
458 if (err) {
459 fput(filp);
460 return err;
461 }
462 err = -EBADF;
463
464 switch (cmd) {
465 case F_GETLK64:
466 err = fcntl_getlk64(filp, (struct flock64 __user *) arg);
467 break;
468 case F_SETLK64:
469 case F_SETLKW64:
Peter Staubachc2936212005-07-27 11:45:09 -0700470 err = fcntl_setlk64(fd, filp, cmd,
471 (struct flock64 __user *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 break;
473 default:
474 err = do_fcntl(fd, cmd, arg, filp);
475 break;
476 }
477 fput(filp);
478out:
479 return err;
480}
481#endif
482
483/* Table to convert sigio signal codes into poll band bitmaps */
484
Eric Dumazetfa3536c2006-03-26 01:37:24 -0800485static const long band_table[NSIGPOLL] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 POLLIN | POLLRDNORM, /* POLL_IN */
487 POLLOUT | POLLWRNORM | POLLWRBAND, /* POLL_OUT */
488 POLLIN | POLLRDNORM | POLLMSG, /* POLL_MSG */
489 POLLERR, /* POLL_ERR */
490 POLLPRI | POLLRDBAND, /* POLL_PRI */
491 POLLHUP | POLLERR /* POLL_HUP */
492};
493
494static inline int sigio_perm(struct task_struct *p,
495 struct fown_struct *fown, int sig)
496{
David Howellsc69e8d92008-11-14 10:39:19 +1100497 const struct cred *cred;
498 int ret;
499
500 rcu_read_lock();
501 cred = __task_cred(p);
502 ret = ((fown->euid == 0 ||
503 fown->euid == cred->suid || fown->euid == cred->uid ||
504 fown->uid == cred->suid || fown->uid == cred->uid) &&
505 !security_file_send_sigiotask(p, fown, sig));
506 rcu_read_unlock();
507 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508}
509
510static void send_sigio_to_task(struct task_struct *p,
Oleg Nesterov8eeee4e2009-06-17 00:27:10 +0200511 struct fown_struct *fown,
Peter Zijlstraba0a6c92009-09-23 15:57:03 -0700512 int fd, int reason, int group)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
Oleg Nesterov8eeee4e2009-06-17 00:27:10 +0200514 /*
515 * F_SETSIG can change ->signum lockless in parallel, make
516 * sure we read it once and use the same value throughout.
517 */
518 int signum = ACCESS_ONCE(fown->signum);
519
520 if (!sigio_perm(p, fown, signum))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 return;
522
Oleg Nesterov8eeee4e2009-06-17 00:27:10 +0200523 switch (signum) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 siginfo_t si;
525 default:
526 /* Queue a rt signal with the appropriate fd as its
527 value. We use SI_SIGIO as the source, not
528 SI_KERNEL, since kernel signals always get
529 delivered even if we can't queue. Failure to
530 queue in this case _should_ be reported; we fall
531 back to SIGIO in that case. --sct */
Oleg Nesterov8eeee4e2009-06-17 00:27:10 +0200532 si.si_signo = signum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 si.si_errno = 0;
534 si.si_code = reason;
535 /* Make sure we are called with one of the POLL_*
536 reasons, otherwise we could leak kernel stack into
537 userspace. */
Eric Sesterhennf6298aa2006-04-02 13:37:19 +0200538 BUG_ON((reason & __SI_MASK) != __SI_POLL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 if (reason - POLL_IN >= NSIGPOLL)
540 si.si_band = ~0L;
541 else
542 si.si_band = band_table[reason - POLL_IN];
543 si.si_fd = fd;
Peter Zijlstraba0a6c92009-09-23 15:57:03 -0700544 if (!do_send_sig_info(signum, &si, p, group))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 break;
546 /* fall-through: fall back on the old plain SIGIO signal */
547 case 0:
Peter Zijlstraba0a6c92009-09-23 15:57:03 -0700548 do_send_sig_info(SIGIO, SEND_SIG_PRIV, p, group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 }
550}
551
552void send_sigio(struct fown_struct *fown, int fd, int band)
553{
554 struct task_struct *p;
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700555 enum pid_type type;
556 struct pid *pid;
Peter Zijlstraba0a6c92009-09-23 15:57:03 -0700557 int group = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
559 read_lock(&fown->lock);
Peter Zijlstraba0a6c92009-09-23 15:57:03 -0700560
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700561 type = fown->pid_type;
Peter Zijlstraba0a6c92009-09-23 15:57:03 -0700562 if (type == PIDTYPE_MAX) {
563 group = 0;
564 type = PIDTYPE_PID;
565 }
566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 pid = fown->pid;
568 if (!pid)
569 goto out_unlock_fown;
570
571 read_lock(&tasklist_lock);
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700572 do_each_pid_task(pid, type, p) {
Peter Zijlstraba0a6c92009-09-23 15:57:03 -0700573 send_sigio_to_task(p, fown, fd, band, group);
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700574 } while_each_pid_task(pid, type, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 read_unlock(&tasklist_lock);
576 out_unlock_fown:
577 read_unlock(&fown->lock);
578}
579
580static void send_sigurg_to_task(struct task_struct *p,
Peter Zijlstraba0a6c92009-09-23 15:57:03 -0700581 struct fown_struct *fown, int group)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
583 if (sigio_perm(p, fown, SIGURG))
Peter Zijlstraba0a6c92009-09-23 15:57:03 -0700584 do_send_sig_info(SIGURG, SEND_SIG_PRIV, p, group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585}
586
587int send_sigurg(struct fown_struct *fown)
588{
589 struct task_struct *p;
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700590 enum pid_type type;
591 struct pid *pid;
Peter Zijlstraba0a6c92009-09-23 15:57:03 -0700592 int group = 1;
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700593 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595 read_lock(&fown->lock);
Peter Zijlstraba0a6c92009-09-23 15:57:03 -0700596
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700597 type = fown->pid_type;
Peter Zijlstraba0a6c92009-09-23 15:57:03 -0700598 if (type == PIDTYPE_MAX) {
599 group = 0;
600 type = PIDTYPE_PID;
601 }
602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 pid = fown->pid;
604 if (!pid)
605 goto out_unlock_fown;
606
607 ret = 1;
608
609 read_lock(&tasklist_lock);
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700610 do_each_pid_task(pid, type, p) {
Peter Zijlstraba0a6c92009-09-23 15:57:03 -0700611 send_sigurg_to_task(p, fown, group);
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700612 } while_each_pid_task(pid, type, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 read_unlock(&tasklist_lock);
614 out_unlock_fown:
615 read_unlock(&fown->lock);
616 return ret;
617}
618
619static DEFINE_RWLOCK(fasync_lock);
Christoph Lametere18b8902006-12-06 20:33:20 -0800620static struct kmem_cache *fasync_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622/*
Linus Torvalds53281b62009-12-16 08:23:37 -0800623 * Remove a fasync entry. If successfully removed, return
624 * positive and clear the FASYNC flag. If no entry exists,
625 * do nothing and return 0.
626 *
627 * NOTE! It is very important that the FASYNC flag always
628 * match the state "is the filp on a fasync list".
629 *
630 * We always take the 'filp->f_lock', in since fasync_lock
631 * needs to be irq-safe.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 */
Linus Torvalds53281b62009-12-16 08:23:37 -0800633static int fasync_remove_entry(struct file *filp, struct fasync_struct **fapp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
635 struct fasync_struct *fa, **fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 int result = 0;
637
Jonathan Corbet4a6a4492009-03-27 12:24:31 -0600638 spin_lock(&filp->f_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 write_lock_irq(&fasync_lock);
640 for (fp = fapp; (fa = *fp) != NULL; fp = &fa->fa_next) {
Linus Torvalds53281b62009-12-16 08:23:37 -0800641 if (fa->fa_file != filp)
642 continue;
643 *fp = fa->fa_next;
644 kmem_cache_free(fasync_cache, fa);
Jonathan Corbet76398422009-02-01 14:26:59 -0700645 filp->f_flags &= ~FASYNC;
Linus Torvalds53281b62009-12-16 08:23:37 -0800646 result = 1;
647 break;
648 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 write_unlock_irq(&fasync_lock);
Jonathan Corbet4a6a4492009-03-27 12:24:31 -0600650 spin_unlock(&filp->f_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 return result;
652}
653
Linus Torvalds53281b62009-12-16 08:23:37 -0800654/*
655 * Add a fasync entry. Return negative on error, positive if
656 * added, and zero if did nothing but change an existing one.
657 *
658 * NOTE! It is very important that the FASYNC flag always
659 * match the state "is the filp on a fasync list".
660 */
661static int fasync_add_entry(int fd, struct file *filp, struct fasync_struct **fapp)
662{
663 struct fasync_struct *new, *fa, **fp;
664 int result = 0;
665
666 new = kmem_cache_alloc(fasync_cache, GFP_KERNEL);
667 if (!new)
668 return -ENOMEM;
669
670 spin_lock(&filp->f_lock);
671 write_lock_irq(&fasync_lock);
672 for (fp = fapp; (fa = *fp) != NULL; fp = &fa->fa_next) {
673 if (fa->fa_file != filp)
674 continue;
675 fa->fa_fd = fd;
676 kmem_cache_free(fasync_cache, new);
677 goto out;
678 }
679
680 new->magic = FASYNC_MAGIC;
681 new->fa_file = filp;
682 new->fa_fd = fd;
683 new->fa_next = *fapp;
684 *fapp = new;
685 result = 1;
686 filp->f_flags |= FASYNC;
687
688out:
689 write_unlock_irq(&fasync_lock);
690 spin_unlock(&filp->f_lock);
691 return result;
692}
693
694/*
695 * fasync_helper() is used by almost all character device drivers
696 * to set up the fasync queue, and for regular files by the file
697 * lease code. It returns negative on error, 0 if it did no changes
698 * and positive if it added/deleted the entry.
699 */
700int fasync_helper(int fd, struct file * filp, int on, struct fasync_struct **fapp)
701{
702 if (!on)
703 return fasync_remove_entry(filp, fapp);
704 return fasync_add_entry(fd, filp, fapp);
705}
706
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707EXPORT_SYMBOL(fasync_helper);
708
709void __kill_fasync(struct fasync_struct *fa, int sig, int band)
710{
711 while (fa) {
712 struct fown_struct * fown;
713 if (fa->magic != FASYNC_MAGIC) {
714 printk(KERN_ERR "kill_fasync: bad magic number in "
715 "fasync_struct!\n");
716 return;
717 }
718 fown = &fa->fa_file->f_owner;
719 /* Don't send SIGURG to processes which have not set a
720 queued signum: SIGURG has its own default signalling
721 mechanism. */
722 if (!(sig == SIGURG && fown->signum == 0))
723 send_sigio(fown, fa->fa_fd, band);
724 fa = fa->fa_next;
725 }
726}
727
728EXPORT_SYMBOL(__kill_fasync);
729
730void kill_fasync(struct fasync_struct **fp, int sig, int band)
731{
732 /* First a quick test without locking: usually
733 * the list is empty.
734 */
735 if (*fp) {
736 read_lock(&fasync_lock);
737 /* reread *fp after obtaining the lock */
738 __kill_fasync(*fp, sig, band);
739 read_unlock(&fasync_lock);
740 }
741}
742EXPORT_SYMBOL(kill_fasync);
743
744static int __init fasync_init(void)
745{
746 fasync_cache = kmem_cache_create("fasync_cache",
Paul Mundt20c2df82007-07-20 10:11:58 +0900747 sizeof(struct fasync_struct), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 return 0;
749}
750
751module_init(fasync_init)