1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 1991, 1992 Linus Torvalds
8 #include <linux/syscalls.h>
9 #include <linux/init.h>
11 #include <linux/sched/task.h>
13 #include <linux/file.h>
14 #include <linux/fdtable.h>
15 #include <linux/capability.h>
16 #include <linux/dnotify.h>
17 #include <linux/slab.h>
18 #include <linux/module.h>
19 #include <linux/pipe_fs_i.h>
20 #include <linux/security.h>
21 #include <linux/ptrace.h>
22 #include <linux/signal.h>
23 #include <linux/rcupdate.h>
24 #include <linux/pid_namespace.h>
25 #include <linux/user_namespace.h>
26 #include <linux/memfd.h>
27 #include <linux/compat.h>
28 #include <linux/mount.h>
30 #include <linux/poll.h>
31 #include <asm/siginfo.h>
32 #include <linux/uaccess.h>
34 #define SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT | O_NOATIME)
36 int setfl(int fd
, struct file
*filp
, unsigned long arg
)
38 struct inode
* inode
= file_inode(filp
);
42 * O_APPEND cannot be cleared if the file is marked as append-only
43 * and the file is open for write.
45 if (((arg
^ filp
->f_flags
) & O_APPEND
) && IS_APPEND(inode
))
48 /* O_NOATIME can only be set by the owner or superuser */
49 if ((arg
& O_NOATIME
) && !(filp
->f_flags
& O_NOATIME
))
50 if (!inode_owner_or_capable(file_mnt_user_ns(filp
), inode
))
53 /* required for strict SunOS emulation */
54 if (O_NONBLOCK
!= O_NDELAY
)
58 /* Pipe packetized mode is controlled by O_DIRECT flag */
59 if (!S_ISFIFO(inode
->i_mode
) && (arg
& O_DIRECT
)) {
60 if (!filp
->f_mapping
|| !filp
->f_mapping
->a_ops
||
61 !filp
->f_mapping
->a_ops
->direct_IO
)
65 if (filp
->f_op
->check_flags
)
66 error
= filp
->f_op
->check_flags(arg
);
67 if (!error
&& filp
->f_op
->setfl
)
68 error
= filp
->f_op
->setfl(filp
, arg
);
73 * ->fasync() is responsible for setting the FASYNC bit.
75 if (((arg
^ filp
->f_flags
) & FASYNC
) && filp
->f_op
->fasync
) {
76 error
= filp
->f_op
->fasync(fd
, filp
, (arg
& FASYNC
) != 0);
82 spin_lock(&filp
->f_lock
);
83 filp
->f_flags
= (arg
& SETFL_MASK
) | (filp
->f_flags
& ~SETFL_MASK
);
84 spin_unlock(&filp
->f_lock
);
89 EXPORT_SYMBOL_GPL(setfl
);
91 static void f_modown(struct file
*filp
, struct pid
*pid
, enum pid_type type
,
94 write_lock_irq(&filp
->f_owner
.lock
);
95 if (force
|| !filp
->f_owner
.pid
) {
96 put_pid(filp
->f_owner
.pid
);
97 filp
->f_owner
.pid
= get_pid(pid
);
98 filp
->f_owner
.pid_type
= type
;
101 const struct cred
*cred
= current_cred();
102 filp
->f_owner
.uid
= cred
->uid
;
103 filp
->f_owner
.euid
= cred
->euid
;
106 write_unlock_irq(&filp
->f_owner
.lock
);
109 void __f_setown(struct file
*filp
, struct pid
*pid
, enum pid_type type
,
112 security_file_set_fowner(filp
);
113 f_modown(filp
, pid
, type
, force
);
115 EXPORT_SYMBOL(__f_setown
);
117 int f_setown(struct file
*filp
, unsigned long arg
, int force
)
120 struct pid
*pid
= NULL
;
121 int who
= arg
, ret
= 0;
125 /* avoid overflow below */
135 pid
= find_vpid(who
);
141 __f_setown(filp
, pid
, type
, force
);
146 EXPORT_SYMBOL(f_setown
);
148 void f_delown(struct file
*filp
)
150 f_modown(filp
, NULL
, PIDTYPE_TGID
, 1);
153 pid_t
f_getown(struct file
*filp
)
157 read_lock_irq(&filp
->f_owner
.lock
);
159 if (pid_task(filp
->f_owner
.pid
, filp
->f_owner
.pid_type
)) {
160 pid
= pid_vnr(filp
->f_owner
.pid
);
161 if (filp
->f_owner
.pid_type
== PIDTYPE_PGID
)
165 read_unlock_irq(&filp
->f_owner
.lock
);
169 static int f_setown_ex(struct file
*filp
, unsigned long arg
)
171 struct f_owner_ex __user
*owner_p
= (void __user
*)arg
;
172 struct f_owner_ex owner
;
177 ret
= copy_from_user(&owner
, owner_p
, sizeof(owner
));
181 switch (owner
.type
) {
199 pid
= find_vpid(owner
.pid
);
200 if (owner
.pid
&& !pid
)
203 __f_setown(filp
, pid
, type
, 1);
209 static int f_getown_ex(struct file
*filp
, unsigned long arg
)
211 struct f_owner_ex __user
*owner_p
= (void __user
*)arg
;
212 struct f_owner_ex owner
= {};
215 read_lock_irq(&filp
->f_owner
.lock
);
217 if (pid_task(filp
->f_owner
.pid
, filp
->f_owner
.pid_type
))
218 owner
.pid
= pid_vnr(filp
->f_owner
.pid
);
220 switch (filp
->f_owner
.pid_type
) {
222 owner
.type
= F_OWNER_TID
;
226 owner
.type
= F_OWNER_PID
;
230 owner
.type
= F_OWNER_PGRP
;
238 read_unlock_irq(&filp
->f_owner
.lock
);
241 ret
= copy_to_user(owner_p
, &owner
, sizeof(owner
));
248 #ifdef CONFIG_CHECKPOINT_RESTORE
249 static int f_getowner_uids(struct file
*filp
, unsigned long arg
)
251 struct user_namespace
*user_ns
= current_user_ns();
252 uid_t __user
*dst
= (void __user
*)arg
;
256 read_lock_irq(&filp
->f_owner
.lock
);
257 src
[0] = from_kuid(user_ns
, filp
->f_owner
.uid
);
258 src
[1] = from_kuid(user_ns
, filp
->f_owner
.euid
);
259 read_unlock_irq(&filp
->f_owner
.lock
);
261 err
= put_user(src
[0], &dst
[0]);
262 err
|= put_user(src
[1], &dst
[1]);
267 static int f_getowner_uids(struct file
*filp
, unsigned long arg
)
273 static bool rw_hint_valid(enum rw_hint hint
)
276 case RWH_WRITE_LIFE_NOT_SET
:
277 case RWH_WRITE_LIFE_NONE
:
278 case RWH_WRITE_LIFE_SHORT
:
279 case RWH_WRITE_LIFE_MEDIUM
:
280 case RWH_WRITE_LIFE_LONG
:
281 case RWH_WRITE_LIFE_EXTREME
:
288 static long fcntl_rw_hint(struct file
*file
, unsigned int cmd
,
291 struct inode
*inode
= file_inode(file
);
292 u64 __user
*argp
= (u64 __user
*)arg
;
297 case F_GET_FILE_RW_HINT
:
298 h
= file_write_hint(file
);
299 if (copy_to_user(argp
, &h
, sizeof(*argp
)))
302 case F_SET_FILE_RW_HINT
:
303 if (copy_from_user(&h
, argp
, sizeof(h
)))
305 hint
= (enum rw_hint
) h
;
306 if (!rw_hint_valid(hint
))
309 spin_lock(&file
->f_lock
);
310 file
->f_write_hint
= hint
;
311 spin_unlock(&file
->f_lock
);
314 h
= inode
->i_write_hint
;
315 if (copy_to_user(argp
, &h
, sizeof(*argp
)))
319 if (copy_from_user(&h
, argp
, sizeof(h
)))
321 hint
= (enum rw_hint
) h
;
322 if (!rw_hint_valid(hint
))
326 inode
->i_write_hint
= hint
;
334 static long do_fcntl(int fd
, unsigned int cmd
, unsigned long arg
,
337 void __user
*argp
= (void __user
*)arg
;
343 err
= f_dupfd(arg
, filp
, 0);
345 case F_DUPFD_CLOEXEC
:
346 err
= f_dupfd(arg
, filp
, O_CLOEXEC
);
349 err
= get_close_on_exec(fd
) ? FD_CLOEXEC
: 0;
353 set_close_on_exec(fd
, arg
& FD_CLOEXEC
);
359 err
= setfl(fd
, filp
, arg
);
361 #if BITS_PER_LONG != 32
362 /* 32-bit arches must use fcntl64() */
366 if (copy_from_user(&flock
, argp
, sizeof(flock
)))
368 err
= fcntl_getlk(filp
, cmd
, &flock
);
369 if (!err
&& copy_to_user(argp
, &flock
, sizeof(flock
)))
372 #if BITS_PER_LONG != 32
373 /* 32-bit arches must use fcntl64() */
380 if (copy_from_user(&flock
, argp
, sizeof(flock
)))
382 err
= fcntl_setlk(fd
, filp
, cmd
, &flock
);
386 * XXX If f_owner is a process group, the
387 * negative return value will get converted
388 * into an error. Oops. If we keep the
389 * current syscall conventions, the only way
390 * to fix this will be in libc.
392 err
= f_getown(filp
);
393 force_successful_syscall_return();
396 err
= f_setown(filp
, arg
, 1);
399 err
= f_getown_ex(filp
, arg
);
402 err
= f_setown_ex(filp
, arg
);
404 case F_GETOWNER_UIDS
:
405 err
= f_getowner_uids(filp
, arg
);
408 err
= filp
->f_owner
.signum
;
411 /* arg == 0 restores default behaviour. */
412 if (!valid_signal(arg
)) {
416 filp
->f_owner
.signum
= arg
;
419 err
= fcntl_getlease(filp
);
422 err
= fcntl_setlease(fd
, filp
, arg
);
425 err
= fcntl_dirnotify(fd
, filp
, arg
);
429 err
= pipe_fcntl(filp
, cmd
, arg
);
433 err
= memfd_fcntl(filp
, cmd
, arg
);
437 case F_GET_FILE_RW_HINT
:
438 case F_SET_FILE_RW_HINT
:
439 err
= fcntl_rw_hint(filp
, cmd
, arg
);
447 static int check_fcntl_cmd(unsigned cmd
)
451 case F_DUPFD_CLOEXEC
:
460 SYSCALL_DEFINE3(fcntl
, unsigned int, fd
, unsigned int, cmd
, unsigned long, arg
)
462 struct fd f
= fdget_raw(fd
);
468 if (unlikely(f
.file
->f_mode
& FMODE_PATH
)) {
469 if (!check_fcntl_cmd(cmd
))
473 err
= security_file_fcntl(f
.file
, cmd
, arg
);
475 err
= do_fcntl(fd
, cmd
, arg
, f
.file
);
483 #if BITS_PER_LONG == 32
484 SYSCALL_DEFINE3(fcntl64
, unsigned int, fd
, unsigned int, cmd
,
487 void __user
*argp
= (void __user
*)arg
;
488 struct fd f
= fdget_raw(fd
);
489 struct flock64 flock
;
495 if (unlikely(f
.file
->f_mode
& FMODE_PATH
)) {
496 if (!check_fcntl_cmd(cmd
))
500 err
= security_file_fcntl(f
.file
, cmd
, arg
);
508 if (copy_from_user(&flock
, argp
, sizeof(flock
)))
510 err
= fcntl_getlk64(f
.file
, cmd
, &flock
);
511 if (!err
&& copy_to_user(argp
, &flock
, sizeof(flock
)))
519 if (copy_from_user(&flock
, argp
, sizeof(flock
)))
521 err
= fcntl_setlk64(fd
, f
.file
, cmd
, &flock
);
524 err
= do_fcntl(fd
, cmd
, arg
, f
.file
);
535 /* careful - don't use anywhere else */
536 #define copy_flock_fields(dst, src) \
537 (dst)->l_type = (src)->l_type; \
538 (dst)->l_whence = (src)->l_whence; \
539 (dst)->l_start = (src)->l_start; \
540 (dst)->l_len = (src)->l_len; \
541 (dst)->l_pid = (src)->l_pid;
543 static int get_compat_flock(struct flock
*kfl
, const struct compat_flock __user
*ufl
)
545 struct compat_flock fl
;
547 if (copy_from_user(&fl
, ufl
, sizeof(struct compat_flock
)))
549 copy_flock_fields(kfl
, &fl
);
553 static int get_compat_flock64(struct flock
*kfl
, const struct compat_flock64 __user
*ufl
)
555 struct compat_flock64 fl
;
557 if (copy_from_user(&fl
, ufl
, sizeof(struct compat_flock64
)))
559 copy_flock_fields(kfl
, &fl
);
563 static int put_compat_flock(const struct flock
*kfl
, struct compat_flock __user
*ufl
)
565 struct compat_flock fl
;
567 memset(&fl
, 0, sizeof(struct compat_flock
));
568 copy_flock_fields(&fl
, kfl
);
569 if (copy_to_user(ufl
, &fl
, sizeof(struct compat_flock
)))
574 static int put_compat_flock64(const struct flock
*kfl
, struct compat_flock64 __user
*ufl
)
576 struct compat_flock64 fl
;
578 BUILD_BUG_ON(sizeof(kfl
->l_start
) > sizeof(ufl
->l_start
));
579 BUILD_BUG_ON(sizeof(kfl
->l_len
) > sizeof(ufl
->l_len
));
581 memset(&fl
, 0, sizeof(struct compat_flock64
));
582 copy_flock_fields(&fl
, kfl
);
583 if (copy_to_user(ufl
, &fl
, sizeof(struct compat_flock64
)))
587 #undef copy_flock_fields
590 convert_fcntl_cmd(unsigned int cmd
)
605 * GETLK was successful and we need to return the data, but it needs to fit in
606 * the compat structure.
607 * l_start shouldn't be too big, unless the original start + end is greater than
608 * COMPAT_OFF_T_MAX, in which case the app was asking for trouble, so we return
609 * -EOVERFLOW in that case. l_len could be too big, in which case we just
610 * truncate it, and only allow the app to see that part of the conflicting lock
611 * that might make sense to it anyway
613 static int fixup_compat_flock(struct flock
*flock
)
615 if (flock
->l_start
> COMPAT_OFF_T_MAX
)
617 if (flock
->l_len
> COMPAT_OFF_T_MAX
)
618 flock
->l_len
= COMPAT_OFF_T_MAX
;
622 static long do_compat_fcntl64(unsigned int fd
, unsigned int cmd
,
625 struct fd f
= fdget_raw(fd
);
632 if (unlikely(f
.file
->f_mode
& FMODE_PATH
)) {
633 if (!check_fcntl_cmd(cmd
))
637 err
= security_file_fcntl(f
.file
, cmd
, arg
);
643 err
= get_compat_flock(&flock
, compat_ptr(arg
));
646 err
= fcntl_getlk(f
.file
, convert_fcntl_cmd(cmd
), &flock
);
649 err
= fixup_compat_flock(&flock
);
651 err
= put_compat_flock(&flock
, compat_ptr(arg
));
655 err
= get_compat_flock64(&flock
, compat_ptr(arg
));
658 err
= fcntl_getlk(f
.file
, convert_fcntl_cmd(cmd
), &flock
);
660 err
= put_compat_flock64(&flock
, compat_ptr(arg
));
664 err
= get_compat_flock(&flock
, compat_ptr(arg
));
667 err
= fcntl_setlk(fd
, f
.file
, convert_fcntl_cmd(cmd
), &flock
);
673 err
= get_compat_flock64(&flock
, compat_ptr(arg
));
676 err
= fcntl_setlk(fd
, f
.file
, convert_fcntl_cmd(cmd
), &flock
);
679 err
= do_fcntl(fd
, cmd
, arg
, f
.file
);
687 COMPAT_SYSCALL_DEFINE3(fcntl64
, unsigned int, fd
, unsigned int, cmd
,
690 return do_compat_fcntl64(fd
, cmd
, arg
);
693 COMPAT_SYSCALL_DEFINE3(fcntl
, unsigned int, fd
, unsigned int, cmd
,
705 return do_compat_fcntl64(fd
, cmd
, arg
);
709 /* Table to convert sigio signal codes into poll band bitmaps */
711 static const __poll_t band_table
[NSIGPOLL
] = {
712 EPOLLIN
| EPOLLRDNORM
, /* POLL_IN */
713 EPOLLOUT
| EPOLLWRNORM
| EPOLLWRBAND
, /* POLL_OUT */
714 EPOLLIN
| EPOLLRDNORM
| EPOLLMSG
, /* POLL_MSG */
715 EPOLLERR
, /* POLL_ERR */
716 EPOLLPRI
| EPOLLRDBAND
, /* POLL_PRI */
717 EPOLLHUP
| EPOLLERR
/* POLL_HUP */
720 static inline int sigio_perm(struct task_struct
*p
,
721 struct fown_struct
*fown
, int sig
)
723 const struct cred
*cred
;
727 cred
= __task_cred(p
);
728 ret
= ((uid_eq(fown
->euid
, GLOBAL_ROOT_UID
) ||
729 uid_eq(fown
->euid
, cred
->suid
) || uid_eq(fown
->euid
, cred
->uid
) ||
730 uid_eq(fown
->uid
, cred
->suid
) || uid_eq(fown
->uid
, cred
->uid
)) &&
731 !security_file_send_sigiotask(p
, fown
, sig
));
736 static void send_sigio_to_task(struct task_struct
*p
,
737 struct fown_struct
*fown
,
738 int fd
, int reason
, enum pid_type type
)
741 * F_SETSIG can change ->signum lockless in parallel, make
742 * sure we read it once and use the same value throughout.
744 int signum
= READ_ONCE(fown
->signum
);
746 if (!sigio_perm(p
, fown
, signum
))
753 /* Queue a rt signal with the appropriate fd as its
754 value. We use SI_SIGIO as the source, not
755 SI_KERNEL, since kernel signals always get
756 delivered even if we can't queue. Failure to
757 queue in this case _should_ be reported; we fall
758 back to SIGIO in that case. --sct */
760 si
.si_signo
= signum
;
764 * Posix definies POLL_IN and friends to be signal
765 * specific si_codes for SIG_POLL. Linux extended
766 * these si_codes to other signals in a way that is
767 * ambiguous if other signals also have signal
768 * specific si_codes. In that case use SI_SIGIO instead
769 * to remove the ambiguity.
771 if ((signum
!= SIGPOLL
) && sig_specific_sicodes(signum
))
772 si
.si_code
= SI_SIGIO
;
774 /* Make sure we are called with one of the POLL_*
775 reasons, otherwise we could leak kernel stack into
777 BUG_ON((reason
< POLL_IN
) || ((reason
- POLL_IN
) >= NSIGPOLL
));
778 if (reason
- POLL_IN
>= NSIGPOLL
)
781 si
.si_band
= mangle_poll(band_table
[reason
- POLL_IN
]);
783 if (!do_send_sig_info(signum
, &si
, p
, type
))
786 fallthrough
; /* fall back on the old plain SIGIO signal */
788 do_send_sig_info(SIGIO
, SEND_SIG_PRIV
, p
, type
);
792 void send_sigio(struct fown_struct
*fown
, int fd
, int band
)
794 struct task_struct
*p
;
799 read_lock_irqsave(&fown
->lock
, flags
);
801 type
= fown
->pid_type
;
804 goto out_unlock_fown
;
806 if (type
<= PIDTYPE_TGID
) {
808 p
= pid_task(pid
, PIDTYPE_PID
);
810 send_sigio_to_task(p
, fown
, fd
, band
, type
);
813 read_lock(&tasklist_lock
);
814 do_each_pid_task(pid
, type
, p
) {
815 send_sigio_to_task(p
, fown
, fd
, band
, type
);
816 } while_each_pid_task(pid
, type
, p
);
817 read_unlock(&tasklist_lock
);
820 read_unlock_irqrestore(&fown
->lock
, flags
);
823 static void send_sigurg_to_task(struct task_struct
*p
,
824 struct fown_struct
*fown
, enum pid_type type
)
826 if (sigio_perm(p
, fown
, SIGURG
))
827 do_send_sig_info(SIGURG
, SEND_SIG_PRIV
, p
, type
);
830 int send_sigurg(struct fown_struct
*fown
)
832 struct task_struct
*p
;
838 read_lock_irqsave(&fown
->lock
, flags
);
840 type
= fown
->pid_type
;
843 goto out_unlock_fown
;
847 if (type
<= PIDTYPE_TGID
) {
849 p
= pid_task(pid
, PIDTYPE_PID
);
851 send_sigurg_to_task(p
, fown
, type
);
854 read_lock(&tasklist_lock
);
855 do_each_pid_task(pid
, type
, p
) {
856 send_sigurg_to_task(p
, fown
, type
);
857 } while_each_pid_task(pid
, type
, p
);
858 read_unlock(&tasklist_lock
);
861 read_unlock_irqrestore(&fown
->lock
, flags
);
865 static DEFINE_SPINLOCK(fasync_lock
);
866 static struct kmem_cache
*fasync_cache __read_mostly
;
868 static void fasync_free_rcu(struct rcu_head
*head
)
870 kmem_cache_free(fasync_cache
,
871 container_of(head
, struct fasync_struct
, fa_rcu
));
875 * Remove a fasync entry. If successfully removed, return
876 * positive and clear the FASYNC flag. If no entry exists,
877 * do nothing and return 0.
879 * NOTE! It is very important that the FASYNC flag always
880 * match the state "is the filp on a fasync list".
883 int fasync_remove_entry(struct file
*filp
, struct fasync_struct
**fapp
)
885 struct fasync_struct
*fa
, **fp
;
888 spin_lock(&filp
->f_lock
);
889 spin_lock(&fasync_lock
);
890 for (fp
= fapp
; (fa
= *fp
) != NULL
; fp
= &fa
->fa_next
) {
891 if (fa
->fa_file
!= filp
)
894 write_lock_irq(&fa
->fa_lock
);
896 write_unlock_irq(&fa
->fa_lock
);
899 call_rcu(&fa
->fa_rcu
, fasync_free_rcu
);
900 filp
->f_flags
&= ~FASYNC
;
904 spin_unlock(&fasync_lock
);
905 spin_unlock(&filp
->f_lock
);
909 struct fasync_struct
*fasync_alloc(void)
911 return kmem_cache_alloc(fasync_cache
, GFP_KERNEL
);
915 * NOTE! This can be used only for unused fasync entries:
916 * entries that actually got inserted on the fasync list
917 * need to be released by rcu - see fasync_remove_entry.
919 void fasync_free(struct fasync_struct
*new)
921 kmem_cache_free(fasync_cache
, new);
925 * Insert a new entry into the fasync list. Return the pointer to the
926 * old one if we didn't use the new one.
928 * NOTE! It is very important that the FASYNC flag always
929 * match the state "is the filp on a fasync list".
931 struct fasync_struct
*fasync_insert_entry(int fd
, struct file
*filp
, struct fasync_struct
**fapp
, struct fasync_struct
*new)
933 struct fasync_struct
*fa
, **fp
;
935 spin_lock(&filp
->f_lock
);
936 spin_lock(&fasync_lock
);
937 for (fp
= fapp
; (fa
= *fp
) != NULL
; fp
= &fa
->fa_next
) {
938 if (fa
->fa_file
!= filp
)
941 write_lock_irq(&fa
->fa_lock
);
943 write_unlock_irq(&fa
->fa_lock
);
947 rwlock_init(&new->fa_lock
);
948 new->magic
= FASYNC_MAGIC
;
951 new->fa_next
= *fapp
;
952 rcu_assign_pointer(*fapp
, new);
953 filp
->f_flags
|= FASYNC
;
956 spin_unlock(&fasync_lock
);
957 spin_unlock(&filp
->f_lock
);
962 * Add a fasync entry. Return negative on error, positive if
963 * added, and zero if did nothing but change an existing one.
965 static int fasync_add_entry(int fd
, struct file
*filp
, struct fasync_struct
**fapp
)
967 struct fasync_struct
*new;
969 new = fasync_alloc();
974 * fasync_insert_entry() returns the old (update) entry if
977 * So free the (unused) new entry and return 0 to let the
978 * caller know that we didn't add any new fasync entries.
980 if (fasync_insert_entry(fd
, filp
, fapp
, new)) {
989 * fasync_helper() is used by almost all character device drivers
990 * to set up the fasync queue, and for regular files by the file
991 * lease code. It returns negative on error, 0 if it did no changes
992 * and positive if it added/deleted the entry.
994 int fasync_helper(int fd
, struct file
* filp
, int on
, struct fasync_struct
**fapp
)
997 return fasync_remove_entry(filp
, fapp
);
998 return fasync_add_entry(fd
, filp
, fapp
);
1001 EXPORT_SYMBOL(fasync_helper
);
1004 * rcu_read_lock() is held
1006 static void kill_fasync_rcu(struct fasync_struct
*fa
, int sig
, int band
)
1009 struct fown_struct
*fown
;
1010 unsigned long flags
;
1012 if (fa
->magic
!= FASYNC_MAGIC
) {
1013 printk(KERN_ERR
"kill_fasync: bad magic number in "
1014 "fasync_struct!\n");
1017 read_lock_irqsave(&fa
->fa_lock
, flags
);
1019 fown
= &fa
->fa_file
->f_owner
;
1020 /* Don't send SIGURG to processes which have not set a
1021 queued signum: SIGURG has its own default signalling
1023 if (!(sig
== SIGURG
&& fown
->signum
== 0))
1024 send_sigio(fown
, fa
->fa_fd
, band
);
1026 read_unlock_irqrestore(&fa
->fa_lock
, flags
);
1027 fa
= rcu_dereference(fa
->fa_next
);
1031 void kill_fasync(struct fasync_struct
**fp
, int sig
, int band
)
1033 /* First a quick test without locking: usually
1034 * the list is empty.
1038 kill_fasync_rcu(rcu_dereference(*fp
), sig
, band
);
1042 EXPORT_SYMBOL(kill_fasync
);
1044 static int __init
fcntl_init(void)
1047 * Please add new bits here to ensure allocation uniqueness.
1048 * Exceptions: O_NONBLOCK is a two bit define on parisc; O_NDELAY
1049 * is defined as O_NONBLOCK on some platforms and not on others.
1051 BUILD_BUG_ON(21 - 1 /* for O_RDONLY being 0 */ !=
1053 (VALID_OPEN_FLAGS
& ~(O_NONBLOCK
| O_NDELAY
)) |
1054 __FMODE_EXEC
| __FMODE_NONOTIFY
));
1056 fasync_cache
= kmem_cache_create("fasync_cache",
1057 sizeof(struct fasync_struct
), 0,
1058 SLAB_PANIC
| SLAB_ACCOUNT
, NULL
);
1062 module_init(fcntl_init
)