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