1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2003 Linus Torvalds
7 * Mon Mar 5, 2007: Davide Libenzi <davidel@xmailserver.org>
8 * Changed ->read() to return a siginfo strcture instead of signal number.
9 * Fixed locking in ->poll().
10 * Added sighand-detach notification.
11 * Added fd re-use in sys_signalfd() syscall.
12 * Now using anonymous inode source.
13 * Thanks to Oleg Nesterov for useful code review and suggestions.
14 * More comments and suggestions from Arnd Bergmann.
15 * Sat May 19, 2007: Davi E. M. Arnaut <davi@haxent.com.br>
16 * Retrieve multiple signals with one read() call
17 * Sun Jul 15, 2007: Davide Libenzi <davidel@xmailserver.org>
18 * Attach to the sighand only during read() and poll().
21 #include <linux/file.h>
22 #include <linux/poll.h>
23 #include <linux/init.h>
25 #include <linux/sched.h>
26 #include <linux/slab.h>
27 #include <linux/kernel.h>
28 #include <linux/signal.h>
29 #include <linux/list.h>
30 #include <linux/anon_inodes.h>
31 #include <linux/signalfd.h>
32 #include <linux/syscalls.h>
33 #include <linux/proc_fs.h>
34 #include <linux/compat.h>
36 void signalfd_cleanup(struct sighand_struct
*sighand
)
38 wait_queue_head_t
*wqh
= &sighand
->signalfd_wqh
;
40 * The lockless check can race with remove_wait_queue() in progress,
41 * but in this case its caller should run under rcu_read_lock() and
42 * sighand_cachep is SLAB_TYPESAFE_BY_RCU, we can safely return.
44 if (likely(!waitqueue_active(wqh
)))
47 /* wait_queue_entry_t->func(POLLFREE) should do remove_wait_queue() */
48 wake_up_poll(wqh
, EPOLLHUP
| POLLFREE
);
55 static int signalfd_release(struct inode
*inode
, struct file
*file
)
57 kfree(file
->private_data
);
61 static __poll_t
signalfd_poll(struct file
*file
, poll_table
*wait
)
63 struct signalfd_ctx
*ctx
= file
->private_data
;
66 poll_wait(file
, ¤t
->sighand
->signalfd_wqh
, wait
);
68 spin_lock_irq(¤t
->sighand
->siglock
);
69 if (next_signal(¤t
->pending
, &ctx
->sigmask
) ||
70 next_signal(¤t
->signal
->shared_pending
,
73 spin_unlock_irq(¤t
->sighand
->siglock
);
79 * Copied from copy_siginfo_to_user() in kernel/signal.c
81 static int signalfd_copyinfo(struct signalfd_siginfo __user
*uinfo
,
82 siginfo_t
const *kinfo
)
84 struct signalfd_siginfo
new;
86 BUILD_BUG_ON(sizeof(struct signalfd_siginfo
) != 128);
89 * Unused members should be zero ...
91 memset(&new, 0, sizeof(new));
94 * If you change siginfo_t structure, please be sure
95 * this code is fixed accordingly.
97 new.ssi_signo
= kinfo
->si_signo
;
98 new.ssi_errno
= kinfo
->si_errno
;
99 new.ssi_code
= kinfo
->si_code
;
100 switch (siginfo_layout(kinfo
->si_signo
, kinfo
->si_code
)) {
102 new.ssi_pid
= kinfo
->si_pid
;
103 new.ssi_uid
= kinfo
->si_uid
;
106 new.ssi_tid
= kinfo
->si_tid
;
107 new.ssi_overrun
= kinfo
->si_overrun
;
108 new.ssi_ptr
= (long) kinfo
->si_ptr
;
109 new.ssi_int
= kinfo
->si_int
;
112 new.ssi_band
= kinfo
->si_band
;
113 new.ssi_fd
= kinfo
->si_fd
;
115 case SIL_FAULT_BNDERR
:
116 case SIL_FAULT_PKUERR
:
118 * Fall through to the SIL_FAULT case. Both SIL_FAULT_BNDERR
119 * and SIL_FAULT_PKUERR are only generated by faults that
120 * deliver them synchronously to userspace. In case someone
121 * injects one of these signals and signalfd catches it treat
125 new.ssi_addr
= (long) kinfo
->si_addr
;
126 #ifdef __ARCH_SI_TRAPNO
127 new.ssi_trapno
= kinfo
->si_trapno
;
130 case SIL_FAULT_MCEERR
:
131 new.ssi_addr
= (long) kinfo
->si_addr
;
132 #ifdef __ARCH_SI_TRAPNO
133 new.ssi_trapno
= kinfo
->si_trapno
;
135 new.ssi_addr_lsb
= (short) kinfo
->si_addr_lsb
;
138 new.ssi_pid
= kinfo
->si_pid
;
139 new.ssi_uid
= kinfo
->si_uid
;
140 new.ssi_status
= kinfo
->si_status
;
141 new.ssi_utime
= kinfo
->si_utime
;
142 new.ssi_stime
= kinfo
->si_stime
;
146 * This case catches also the signals queued by sigqueue().
148 new.ssi_pid
= kinfo
->si_pid
;
149 new.ssi_uid
= kinfo
->si_uid
;
150 new.ssi_ptr
= (long) kinfo
->si_ptr
;
151 new.ssi_int
= kinfo
->si_int
;
154 new.ssi_call_addr
= (long) kinfo
->si_call_addr
;
155 new.ssi_syscall
= kinfo
->si_syscall
;
156 new.ssi_arch
= kinfo
->si_arch
;
160 if (copy_to_user(uinfo
, &new, sizeof(struct signalfd_siginfo
)))
163 return sizeof(*uinfo
);
166 static ssize_t
signalfd_dequeue(struct signalfd_ctx
*ctx
, siginfo_t
*info
,
170 DECLARE_WAITQUEUE(wait
, current
);
172 spin_lock_irq(¤t
->sighand
->siglock
);
173 ret
= dequeue_signal(current
, &ctx
->sigmask
, info
);
180 spin_unlock_irq(¤t
->sighand
->siglock
);
184 add_wait_queue(¤t
->sighand
->signalfd_wqh
, &wait
);
186 set_current_state(TASK_INTERRUPTIBLE
);
187 ret
= dequeue_signal(current
, &ctx
->sigmask
, info
);
190 if (signal_pending(current
)) {
194 spin_unlock_irq(¤t
->sighand
->siglock
);
196 spin_lock_irq(¤t
->sighand
->siglock
);
198 spin_unlock_irq(¤t
->sighand
->siglock
);
200 remove_wait_queue(¤t
->sighand
->signalfd_wqh
, &wait
);
201 __set_current_state(TASK_RUNNING
);
207 * Returns a multiple of the size of a "struct signalfd_siginfo", or a negative
208 * error code. The "count" parameter must be at least the size of a
209 * "struct signalfd_siginfo".
211 static ssize_t
signalfd_read(struct file
*file
, char __user
*buf
, size_t count
,
214 struct signalfd_ctx
*ctx
= file
->private_data
;
215 struct signalfd_siginfo __user
*siginfo
;
216 int nonblock
= file
->f_flags
& O_NONBLOCK
;
217 ssize_t ret
, total
= 0;
220 count
/= sizeof(struct signalfd_siginfo
);
224 siginfo
= (struct signalfd_siginfo __user
*) buf
;
226 ret
= signalfd_dequeue(ctx
, &info
, nonblock
);
227 if (unlikely(ret
<= 0))
229 ret
= signalfd_copyinfo(siginfo
, &info
);
237 return total
? total
: ret
;
240 #ifdef CONFIG_PROC_FS
241 static void signalfd_show_fdinfo(struct seq_file
*m
, struct file
*f
)
243 struct signalfd_ctx
*ctx
= f
->private_data
;
246 sigmask
= ctx
->sigmask
;
248 render_sigset_t(m
, "sigmask:\t", &sigmask
);
252 static const struct file_operations signalfd_fops
= {
253 #ifdef CONFIG_PROC_FS
254 .show_fdinfo
= signalfd_show_fdinfo
,
256 .release
= signalfd_release
,
257 .poll
= signalfd_poll
,
258 .read
= signalfd_read
,
259 .llseek
= noop_llseek
,
262 static int do_signalfd4(int ufd
, sigset_t __user
*user_mask
, size_t sizemask
,
266 struct signalfd_ctx
*ctx
;
268 /* Check the SFD_* constants for consistency. */
269 BUILD_BUG_ON(SFD_CLOEXEC
!= O_CLOEXEC
);
270 BUILD_BUG_ON(SFD_NONBLOCK
!= O_NONBLOCK
);
272 if (flags
& ~(SFD_CLOEXEC
| SFD_NONBLOCK
))
275 if (sizemask
!= sizeof(sigset_t
) ||
276 copy_from_user(&sigmask
, user_mask
, sizeof(sigmask
)))
278 sigdelsetmask(&sigmask
, sigmask(SIGKILL
) | sigmask(SIGSTOP
));
282 ctx
= kmalloc(sizeof(*ctx
), GFP_KERNEL
);
286 ctx
->sigmask
= sigmask
;
289 * When we call this, the initialization must be complete, since
290 * anon_inode_getfd() will install the fd.
292 ufd
= anon_inode_getfd("[signalfd]", &signalfd_fops
, ctx
,
293 O_RDWR
| (flags
& (O_CLOEXEC
| O_NONBLOCK
)));
297 struct fd f
= fdget(ufd
);
300 ctx
= f
.file
->private_data
;
301 if (f
.file
->f_op
!= &signalfd_fops
) {
305 spin_lock_irq(¤t
->sighand
->siglock
);
306 ctx
->sigmask
= sigmask
;
307 spin_unlock_irq(¤t
->sighand
->siglock
);
309 wake_up(¤t
->sighand
->signalfd_wqh
);
316 SYSCALL_DEFINE4(signalfd4
, int, ufd
, sigset_t __user
*, user_mask
,
317 size_t, sizemask
, int, flags
)
319 return do_signalfd4(ufd
, user_mask
, sizemask
, flags
);
322 SYSCALL_DEFINE3(signalfd
, int, ufd
, sigset_t __user
*, user_mask
,
325 return do_signalfd4(ufd
, user_mask
, sizemask
, 0);
329 static long do_compat_signalfd4(int ufd
,
330 const compat_sigset_t __user
*sigmask
,
331 compat_size_t sigsetsize
, int flags
)
334 sigset_t __user
*ksigmask
;
336 if (sigsetsize
!= sizeof(compat_sigset_t
))
338 if (get_compat_sigset(&tmp
, sigmask
))
340 ksigmask
= compat_alloc_user_space(sizeof(sigset_t
));
341 if (copy_to_user(ksigmask
, &tmp
, sizeof(sigset_t
)))
344 return do_signalfd4(ufd
, ksigmask
, sizeof(sigset_t
), flags
);
347 COMPAT_SYSCALL_DEFINE4(signalfd4
, int, ufd
,
348 const compat_sigset_t __user
*, sigmask
,
349 compat_size_t
, sigsetsize
,
352 return do_compat_signalfd4(ufd
, sigmask
, sigsetsize
, flags
);
355 COMPAT_SYSCALL_DEFINE3(signalfd
, int, ufd
,
356 const compat_sigset_t __user
*,sigmask
,
357 compat_size_t
, sigsetsize
)
359 return do_compat_signalfd4(ufd
, sigmask
, sigsetsize
, 0);