1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2003 Linus Torvalds
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.
16 * Retrieve multiple signals with one read() call
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 wake_up_pollfree(&sighand->signalfd_wqh);
45 static int signalfd_release(struct inode *inode, struct file *file)
47 kfree(file->private_data);
51 static __poll_t signalfd_poll(struct file *file, poll_table *wait)
53 struct signalfd_ctx *ctx = file->private_data;
56 poll_wait(file, ¤t->sighand->signalfd_wqh, wait);
58 spin_lock_irq(¤t->sighand->siglock);
59 if (next_signal(¤t->pending, &ctx->sigmask) ||
60 next_signal(¤t->signal->shared_pending,
63 spin_unlock_irq(¤t->sighand->siglock);
69 * Copied from copy_siginfo_to_user() in kernel/signal.c
71 static int signalfd_copyinfo(struct iov_iter *to, kernel_siginfo_t const *kinfo)
73 struct signalfd_siginfo new;
75 BUILD_BUG_ON(sizeof(struct signalfd_siginfo) != 128);
78 * Unused members should be zero ...
80 memset(&new, 0, sizeof(new));
83 * If you change siginfo_t structure, please be sure
84 * this code is fixed accordingly.
86 new.ssi_signo = kinfo->si_signo;
87 new.ssi_errno = kinfo->si_errno;
88 new.ssi_code = kinfo->si_code;
89 switch (siginfo_layout(kinfo->si_signo, kinfo->si_code)) {
91 new.ssi_pid = kinfo->si_pid;
92 new.ssi_uid = kinfo->si_uid;
95 new.ssi_tid = kinfo->si_tid;
96 new.ssi_overrun = kinfo->si_overrun;
97 new.ssi_ptr = (long) kinfo->si_ptr;
98 new.ssi_int = kinfo->si_int;
101 new.ssi_band = kinfo->si_band;
102 new.ssi_fd = kinfo->si_fd;
104 case SIL_FAULT_BNDERR:
105 case SIL_FAULT_PKUERR:
106 case SIL_FAULT_PERF_EVENT:
108 * Fall through to the SIL_FAULT case. SIL_FAULT_BNDERR,
109 * SIL_FAULT_PKUERR, and SIL_FAULT_PERF_EVENT are only
110 * generated by faults that deliver them synchronously to
111 * userspace. In case someone injects one of these signals
112 * and signalfd catches it treat it as SIL_FAULT.
115 new.ssi_addr = (long) kinfo->si_addr;
117 case SIL_FAULT_TRAPNO:
118 new.ssi_addr = (long) kinfo->si_addr;
119 new.ssi_trapno = kinfo->si_trapno;
121 case SIL_FAULT_MCEERR:
122 new.ssi_addr = (long) kinfo->si_addr;
123 new.ssi_addr_lsb = (short) kinfo->si_addr_lsb;
126 new.ssi_pid = kinfo->si_pid;
127 new.ssi_uid = kinfo->si_uid;
128 new.ssi_status = kinfo->si_status;
129 new.ssi_utime = kinfo->si_utime;
130 new.ssi_stime = kinfo->si_stime;
134 * This case catches also the signals queued by sigqueue().
136 new.ssi_pid = kinfo->si_pid;
137 new.ssi_uid = kinfo->si_uid;
138 new.ssi_ptr = (long) kinfo->si_ptr;
139 new.ssi_int = kinfo->si_int;
142 new.ssi_call_addr = (long) kinfo->si_call_addr;
143 new.ssi_syscall = kinfo->si_syscall;
144 new.ssi_arch = kinfo->si_arch;
148 if (!copy_to_iter_full(&new, sizeof(struct signalfd_siginfo), to))
151 return sizeof(struct signalfd_siginfo);
154 static ssize_t signalfd_dequeue(struct signalfd_ctx *ctx, kernel_siginfo_t *info,
159 DECLARE_WAITQUEUE(wait, current);
161 spin_lock_irq(¤t->sighand->siglock);
162 ret = dequeue_signal(current, &ctx->sigmask, info, &type);
170 spin_unlock_irq(¤t->sighand->siglock);
174 add_wait_queue(¤t->sighand->signalfd_wqh, &wait);
176 set_current_state(TASK_INTERRUPTIBLE);
177 ret = dequeue_signal(current, &ctx->sigmask, info, &type);
180 if (signal_pending(current)) {
184 spin_unlock_irq(¤t->sighand->siglock);
186 spin_lock_irq(¤t->sighand->siglock);
188 spin_unlock_irq(¤t->sighand->siglock);
190 remove_wait_queue(¤t->sighand->signalfd_wqh, &wait);
191 __set_current_state(TASK_RUNNING);
197 * Returns a multiple of the size of a "struct signalfd_siginfo", or a negative
198 * error code. The "count" parameter must be at least the size of a
199 * "struct signalfd_siginfo".
201 static ssize_t signalfd_read_iter(struct kiocb *iocb, struct iov_iter *to)
203 struct file *file = iocb->ki_filp;
204 struct signalfd_ctx *ctx = file->private_data;
205 size_t count = iov_iter_count(to);
206 ssize_t ret, total = 0;
207 kernel_siginfo_t info;
210 count /= sizeof(struct signalfd_siginfo);
214 nonblock = file->f_flags & O_NONBLOCK || iocb->ki_flags & IOCB_NOWAIT;
216 ret = signalfd_dequeue(ctx, &info, nonblock);
217 if (unlikely(ret <= 0))
219 ret = signalfd_copyinfo(to, &info);
226 return total ? total: ret;
229 #ifdef CONFIG_PROC_FS
230 static void signalfd_show_fdinfo(struct seq_file *m, struct file *f)
232 struct signalfd_ctx *ctx = f->private_data;
235 sigmask = ctx->sigmask;
237 render_sigset_t(m, "sigmask:\t", &sigmask);
241 static const struct file_operations signalfd_fops = {
242 #ifdef CONFIG_PROC_FS
243 .show_fdinfo = signalfd_show_fdinfo,
245 .release = signalfd_release,
246 .poll = signalfd_poll,
247 .read_iter = signalfd_read_iter,
248 .llseek = noop_llseek,
251 static int do_signalfd4(int ufd, sigset_t *mask, int flags)
253 struct signalfd_ctx *ctx;
255 /* Check the SFD_* constants for consistency. */
256 BUILD_BUG_ON(SFD_CLOEXEC != O_CLOEXEC);
257 BUILD_BUG_ON(SFD_NONBLOCK != O_NONBLOCK);
259 if (flags & ~(SFD_CLOEXEC | SFD_NONBLOCK))
262 sigdelsetmask(mask, sigmask(SIGKILL) | sigmask(SIGSTOP));
268 ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
272 ctx->sigmask = *mask;
274 ufd = get_unused_fd_flags(flags & O_CLOEXEC);
280 file = anon_inode_getfile("[signalfd]", &signalfd_fops, ctx,
281 O_RDWR | (flags & O_NONBLOCK));
287 file->f_mode |= FMODE_NOWAIT;
290 * When we call this, the initialization must be complete, since
291 * anon_inode_getfd() will install the fd.
293 fd_install(ufd, file);
295 struct fd f = fdget(ufd);
298 ctx = f.file->private_data;
299 if (f.file->f_op != &signalfd_fops) {
303 spin_lock_irq(¤t->sighand->siglock);
304 ctx->sigmask = *mask;
305 spin_unlock_irq(¤t->sighand->siglock);
307 wake_up(¤t->sighand->signalfd_wqh);
314 SYSCALL_DEFINE4(signalfd4, int, ufd, sigset_t __user *, user_mask,
315 size_t, sizemask, int, flags)
319 if (sizemask != sizeof(sigset_t))
321 if (copy_from_user(&mask, user_mask, sizeof(mask)))
323 return do_signalfd4(ufd, &mask, flags);
326 SYSCALL_DEFINE3(signalfd, int, ufd, sigset_t __user *, user_mask,
331 if (sizemask != sizeof(sigset_t))
333 if (copy_from_user(&mask, user_mask, sizeof(mask)))
335 return do_signalfd4(ufd, &mask, 0);
339 static long do_compat_signalfd4(int ufd,
340 const compat_sigset_t __user *user_mask,
341 compat_size_t sigsetsize, int flags)
345 if (sigsetsize != sizeof(compat_sigset_t))
347 if (get_compat_sigset(&mask, user_mask))
349 return do_signalfd4(ufd, &mask, flags);
352 COMPAT_SYSCALL_DEFINE4(signalfd4, int, ufd,
353 const compat_sigset_t __user *, user_mask,
354 compat_size_t, sigsetsize,
357 return do_compat_signalfd4(ufd, user_mask, sigsetsize, flags);
360 COMPAT_SYSCALL_DEFINE3(signalfd, int, ufd,
361 const compat_sigset_t __user *, user_mask,
362 compat_size_t, sigsetsize)
364 return do_compat_signalfd4(ufd, user_mask, sigsetsize, 0);