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 wake_up_pollfree(&sighand->signalfd_wqh);
45 static int signalfd_release(struct inode *inode, struct file *file)
47 kfree(file->private_data);
51 static unsigned int signalfd_poll(struct file *file, poll_table *wait)
53 struct signalfd_ctx *ctx = file->private_data;
54 unsigned int events = 0;
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 signalfd_siginfo __user *uinfo,
72 siginfo_t const *kinfo)
76 BUILD_BUG_ON(sizeof(struct signalfd_siginfo) != 128);
79 * Unused members should be zero ...
81 err = __clear_user(uinfo, sizeof(*uinfo));
84 * If you change siginfo_t structure, please be sure
85 * this code is fixed accordingly.
87 err |= __put_user(kinfo->si_signo, &uinfo->ssi_signo);
88 err |= __put_user(kinfo->si_errno, &uinfo->ssi_errno);
89 err |= __put_user(kinfo->si_code, &uinfo->ssi_code);
90 switch (siginfo_layout(kinfo->si_signo, kinfo->si_code)) {
92 err |= __put_user(kinfo->si_pid, &uinfo->ssi_pid);
93 err |= __put_user(kinfo->si_uid, &uinfo->ssi_uid);
96 err |= __put_user(kinfo->si_tid, &uinfo->ssi_tid);
97 err |= __put_user(kinfo->si_overrun, &uinfo->ssi_overrun);
98 err |= __put_user((long) kinfo->si_ptr, &uinfo->ssi_ptr);
99 err |= __put_user(kinfo->si_int, &uinfo->ssi_int);
102 err |= __put_user(kinfo->si_band, &uinfo->ssi_band);
103 err |= __put_user(kinfo->si_fd, &uinfo->ssi_fd);
106 err |= __put_user((long) kinfo->si_addr, &uinfo->ssi_addr);
107 #ifdef __ARCH_SI_TRAPNO
108 err |= __put_user(kinfo->si_trapno, &uinfo->ssi_trapno);
112 * Other callers might not initialize the si_lsb field,
113 * so check explicitly for the right codes here.
115 if (kinfo->si_signo == SIGBUS &&
116 (kinfo->si_code == BUS_MCEERR_AR ||
117 kinfo->si_code == BUS_MCEERR_AO))
118 err |= __put_user((short) kinfo->si_addr_lsb,
119 &uinfo->ssi_addr_lsb);
123 err |= __put_user(kinfo->si_pid, &uinfo->ssi_pid);
124 err |= __put_user(kinfo->si_uid, &uinfo->ssi_uid);
125 err |= __put_user(kinfo->si_status, &uinfo->ssi_status);
126 err |= __put_user(kinfo->si_utime, &uinfo->ssi_utime);
127 err |= __put_user(kinfo->si_stime, &uinfo->ssi_stime);
132 * This case catches also the signals queued by sigqueue().
134 err |= __put_user(kinfo->si_pid, &uinfo->ssi_pid);
135 err |= __put_user(kinfo->si_uid, &uinfo->ssi_uid);
136 err |= __put_user((long) kinfo->si_ptr, &uinfo->ssi_ptr);
137 err |= __put_user(kinfo->si_int, &uinfo->ssi_int);
141 return err ? -EFAULT: sizeof(*uinfo);
144 static ssize_t signalfd_dequeue(struct signalfd_ctx *ctx, siginfo_t *info,
148 DECLARE_WAITQUEUE(wait, current);
150 spin_lock_irq(¤t->sighand->siglock);
151 ret = dequeue_signal(current, &ctx->sigmask, info);
158 spin_unlock_irq(¤t->sighand->siglock);
162 add_wait_queue(¤t->sighand->signalfd_wqh, &wait);
164 set_current_state(TASK_INTERRUPTIBLE);
165 ret = dequeue_signal(current, &ctx->sigmask, info);
168 if (signal_pending(current)) {
172 spin_unlock_irq(¤t->sighand->siglock);
174 spin_lock_irq(¤t->sighand->siglock);
176 spin_unlock_irq(¤t->sighand->siglock);
178 remove_wait_queue(¤t->sighand->signalfd_wqh, &wait);
179 __set_current_state(TASK_RUNNING);
185 * Returns a multiple of the size of a "struct signalfd_siginfo", or a negative
186 * error code. The "count" parameter must be at least the size of a
187 * "struct signalfd_siginfo".
189 static ssize_t signalfd_read(struct file *file, char __user *buf, size_t count,
192 struct signalfd_ctx *ctx = file->private_data;
193 struct signalfd_siginfo __user *siginfo;
194 int nonblock = file->f_flags & O_NONBLOCK;
195 ssize_t ret, total = 0;
198 count /= sizeof(struct signalfd_siginfo);
202 siginfo = (struct signalfd_siginfo __user *) buf;
204 ret = signalfd_dequeue(ctx, &info, nonblock);
205 if (unlikely(ret <= 0))
207 ret = signalfd_copyinfo(siginfo, &info);
215 return total ? total: ret;
218 #ifdef CONFIG_PROC_FS
219 static void signalfd_show_fdinfo(struct seq_file *m, struct file *f)
221 struct signalfd_ctx *ctx = f->private_data;
224 sigmask = ctx->sigmask;
226 render_sigset_t(m, "sigmask:\t", &sigmask);
230 static const struct file_operations signalfd_fops = {
231 #ifdef CONFIG_PROC_FS
232 .show_fdinfo = signalfd_show_fdinfo,
234 .release = signalfd_release,
235 .poll = signalfd_poll,
236 .read = signalfd_read,
237 .llseek = noop_llseek,
240 SYSCALL_DEFINE4(signalfd4, int, ufd, sigset_t __user *, user_mask,
241 size_t, sizemask, int, flags)
244 struct signalfd_ctx *ctx;
246 /* Check the SFD_* constants for consistency. */
247 BUILD_BUG_ON(SFD_CLOEXEC != O_CLOEXEC);
248 BUILD_BUG_ON(SFD_NONBLOCK != O_NONBLOCK);
250 if (flags & ~(SFD_CLOEXEC | SFD_NONBLOCK))
253 if (sizemask != sizeof(sigset_t) ||
254 copy_from_user(&sigmask, user_mask, sizeof(sigmask)))
256 sigdelsetmask(&sigmask, sigmask(SIGKILL) | sigmask(SIGSTOP));
260 ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
264 ctx->sigmask = sigmask;
267 * When we call this, the initialization must be complete, since
268 * anon_inode_getfd() will install the fd.
270 ufd = anon_inode_getfd("[signalfd]", &signalfd_fops, ctx,
271 O_RDWR | (flags & (O_CLOEXEC | O_NONBLOCK)));
275 struct fd f = fdget(ufd);
278 ctx = f.file->private_data;
279 if (f.file->f_op != &signalfd_fops) {
283 spin_lock_irq(¤t->sighand->siglock);
284 ctx->sigmask = sigmask;
285 spin_unlock_irq(¤t->sighand->siglock);
287 wake_up(¤t->sighand->signalfd_wqh);
294 SYSCALL_DEFINE3(signalfd, int, ufd, sigset_t __user *, user_mask,
297 return sys_signalfd4(ufd, user_mask, sizemask, 0);
301 COMPAT_SYSCALL_DEFINE4(signalfd4, int, ufd,
302 const compat_sigset_t __user *,sigmask,
303 compat_size_t, sigsetsize,
306 compat_sigset_t ss32;
308 sigset_t __user *ksigmask;
310 if (sigsetsize != sizeof(compat_sigset_t))
312 if (copy_from_user(&ss32, sigmask, sizeof(ss32)))
314 sigset_from_compat(&tmp, &ss32);
315 ksigmask = compat_alloc_user_space(sizeof(sigset_t));
316 if (copy_to_user(ksigmask, &tmp, sizeof(sigset_t)))
319 return sys_signalfd4(ufd, ksigmask, sizeof(sigset_t), flags);
322 COMPAT_SYSCALL_DEFINE3(signalfd, int, ufd,
323 const compat_sigset_t __user *,sigmask,
324 compat_size_t, sigsetsize)
326 return compat_sys_signalfd4(ufd, sigmask, sigsetsize, 0);