signal/signalfd: Add support for SIGSYS
[linux-2.6-block.git] / fs / signalfd.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
fba2afaa
DL
2/*
3 * fs/signalfd.c
4 *
5 * Copyright (C) 2003 Linus Torvalds
6 *
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.
b8fceee1 15 * Sat May 19, 2007: Davi E. M. Arnaut <davi@haxent.com.br>
b3762bfc 16 * Retrieve multiple signals with one read() call
b8fceee1
DL
17 * Sun Jul 15, 2007: Davide Libenzi <davidel@xmailserver.org>
18 * Attach to the sighand only during read() and poll().
fba2afaa
DL
19 */
20
21#include <linux/file.h>
22#include <linux/poll.h>
23#include <linux/init.h>
24#include <linux/fs.h>
25#include <linux/sched.h>
5a0e3ad6 26#include <linux/slab.h>
fba2afaa
DL
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>
7ec37dfd 32#include <linux/syscalls.h>
138d22b5 33#include <linux/proc_fs.h>
7d197ed4 34#include <linux/compat.h>
fba2afaa 35
d80e731e
ON
36void signalfd_cleanup(struct sighand_struct *sighand)
37{
38 wait_queue_head_t *wqh = &sighand->signalfd_wqh;
971316f0
ON
39 /*
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
5f0d5a3a 42 * sighand_cachep is SLAB_TYPESAFE_BY_RCU, we can safely return.
971316f0 43 */
d80e731e
ON
44 if (likely(!waitqueue_active(wqh)))
45 return;
46
ac6424b9 47 /* wait_queue_entry_t->func(POLLFREE) should do remove_wait_queue() */
a9a08845 48 wake_up_poll(wqh, EPOLLHUP | POLLFREE);
d80e731e
ON
49}
50
fba2afaa 51struct signalfd_ctx {
fba2afaa 52 sigset_t sigmask;
fba2afaa
DL
53};
54
fba2afaa
DL
55static int signalfd_release(struct inode *inode, struct file *file)
56{
b8fceee1 57 kfree(file->private_data);
fba2afaa
DL
58 return 0;
59}
60
076ccb76 61static __poll_t signalfd_poll(struct file *file, poll_table *wait)
fba2afaa
DL
62{
63 struct signalfd_ctx *ctx = file->private_data;
076ccb76 64 __poll_t events = 0;
fba2afaa 65
b8fceee1 66 poll_wait(file, &current->sighand->signalfd_wqh, wait);
fba2afaa 67
b8fceee1
DL
68 spin_lock_irq(&current->sighand->siglock);
69 if (next_signal(&current->pending, &ctx->sigmask) ||
70 next_signal(&current->signal->shared_pending,
71 &ctx->sigmask))
a9a08845 72 events |= EPOLLIN;
b8fceee1 73 spin_unlock_irq(&current->sighand->siglock);
fba2afaa
DL
74
75 return events;
76}
77
78/*
79 * Copied from copy_siginfo_to_user() in kernel/signal.c
80 */
81static int signalfd_copyinfo(struct signalfd_siginfo __user *uinfo,
82 siginfo_t const *kinfo)
83{
5611f55e 84 struct signalfd_siginfo new;
fba2afaa
DL
85
86 BUILD_BUG_ON(sizeof(struct signalfd_siginfo) != 128);
87
88 /*
14e4a0f2 89 * Unused members should be zero ...
fba2afaa 90 */
5611f55e 91 memset(&new, 0, sizeof(new));
fba2afaa
DL
92
93 /*
94 * If you change siginfo_t structure, please be sure
95 * this code is fixed accordingly.
96 */
5611f55e
EB
97 new.ssi_signo = kinfo->si_signo;
98 new.ssi_errno = kinfo->si_errno;
99 new.ssi_code = kinfo->si_code;
cc731525
EB
100 switch (siginfo_layout(kinfo->si_signo, kinfo->si_code)) {
101 case SIL_KILL:
5611f55e
EB
102 new.ssi_pid = kinfo->si_pid;
103 new.ssi_uid = kinfo->si_uid;
fba2afaa 104 break;
cc731525 105 case SIL_TIMER:
5611f55e
EB
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;
fba2afaa 110 break;
cc731525 111 case SIL_POLL:
5611f55e
EB
112 new.ssi_band = kinfo->si_band;
113 new.ssi_fd = kinfo->si_fd;
fba2afaa 114 break;
cc731525 115 case SIL_FAULT:
5611f55e 116 new.ssi_addr = (long) kinfo->si_addr;
fba2afaa 117#ifdef __ARCH_SI_TRAPNO
5611f55e 118 new.ssi_trapno = kinfo->si_trapno;
b8aeec34 119#endif
9026e820 120 /*
b8aeec34
HS
121 * Other callers might not initialize the si_lsb field,
122 * so check explicitly for the right codes here.
123 */
3ead7c52 124 if (kinfo->si_signo == SIGBUS &&
4181d225
EB
125 ((kinfo->si_code == BUS_MCEERR_AR) ||
126 (kinfo->si_code == BUS_MCEERR_AO)))
5611f55e 127 new.ssi_addr_lsb = (short) kinfo->si_addr_lsb;
fba2afaa 128 break;
cc731525 129 case SIL_CHLD:
5611f55e
EB
130 new.ssi_pid = kinfo->si_pid;
131 new.ssi_uid = kinfo->si_uid;
132 new.ssi_status = kinfo->si_status;
133 new.ssi_utime = kinfo->si_utime;
134 new.ssi_stime = kinfo->si_stime;
fba2afaa 135 break;
cc731525 136 case SIL_RT:
0859ab59
DL
137 /*
138 * This case catches also the signals queued by sigqueue().
139 */
5611f55e
EB
140 new.ssi_pid = kinfo->si_pid;
141 new.ssi_uid = kinfo->si_uid;
142 new.ssi_ptr = (long) kinfo->si_ptr;
143 new.ssi_int = kinfo->si_int;
fba2afaa 144 break;
76b7f670
EB
145 case SIL_SYS:
146 new.ssi_call_addr = (long) kinfo->si_call_addr;
147 new.ssi_syscall = kinfo->si_syscall;
148 new.ssi_arch = kinfo->si_arch;
149 break;
fba2afaa
DL
150 }
151
5611f55e
EB
152 if (copy_to_user(uinfo, &new, sizeof(struct signalfd_siginfo)))
153 return -EFAULT;
154
155 return sizeof(*uinfo);
fba2afaa
DL
156}
157
b3762bfc
DA
158static ssize_t signalfd_dequeue(struct signalfd_ctx *ctx, siginfo_t *info,
159 int nonblock)
160{
161 ssize_t ret;
b3762bfc
DA
162 DECLARE_WAITQUEUE(wait, current);
163
b8fceee1
DL
164 spin_lock_irq(&current->sighand->siglock);
165 ret = dequeue_signal(current, &ctx->sigmask, info);
b3762bfc
DA
166 switch (ret) {
167 case 0:
168 if (!nonblock)
169 break;
170 ret = -EAGAIN;
171 default:
b8fceee1 172 spin_unlock_irq(&current->sighand->siglock);
b3762bfc
DA
173 return ret;
174 }
175
b8fceee1 176 add_wait_queue(&current->sighand->signalfd_wqh, &wait);
b3762bfc
DA
177 for (;;) {
178 set_current_state(TASK_INTERRUPTIBLE);
b8fceee1 179 ret = dequeue_signal(current, &ctx->sigmask, info);
b3762bfc
DA
180 if (ret != 0)
181 break;
182 if (signal_pending(current)) {
183 ret = -ERESTARTSYS;
184 break;
185 }
b8fceee1 186 spin_unlock_irq(&current->sighand->siglock);
b3762bfc 187 schedule();
b8fceee1 188 spin_lock_irq(&current->sighand->siglock);
b3762bfc 189 }
b8fceee1 190 spin_unlock_irq(&current->sighand->siglock);
b3762bfc 191
b8fceee1 192 remove_wait_queue(&current->sighand->signalfd_wqh, &wait);
b3762bfc
DA
193 __set_current_state(TASK_RUNNING);
194
195 return ret;
196}
197
fba2afaa 198/*
b8fceee1
DL
199 * Returns a multiple of the size of a "struct signalfd_siginfo", or a negative
200 * error code. The "count" parameter must be at least the size of a
201 * "struct signalfd_siginfo".
fba2afaa
DL
202 */
203static ssize_t signalfd_read(struct file *file, char __user *buf, size_t count,
204 loff_t *ppos)
205{
206 struct signalfd_ctx *ctx = file->private_data;
b3762bfc
DA
207 struct signalfd_siginfo __user *siginfo;
208 int nonblock = file->f_flags & O_NONBLOCK;
209 ssize_t ret, total = 0;
fba2afaa 210 siginfo_t info;
fba2afaa 211
b3762bfc
DA
212 count /= sizeof(struct signalfd_siginfo);
213 if (!count)
fba2afaa 214 return -EINVAL;
fba2afaa 215
b3762bfc 216 siginfo = (struct signalfd_siginfo __user *) buf;
b3762bfc
DA
217 do {
218 ret = signalfd_dequeue(ctx, &info, nonblock);
219 if (unlikely(ret <= 0))
220 break;
221 ret = signalfd_copyinfo(siginfo, &info);
222 if (ret < 0)
223 break;
224 siginfo++;
225 total += ret;
226 nonblock = 1;
227 } while (--count);
228
b8fceee1 229 return total ? total: ret;
fba2afaa
DL
230}
231
138d22b5 232#ifdef CONFIG_PROC_FS
a3816ab0 233static void signalfd_show_fdinfo(struct seq_file *m, struct file *f)
138d22b5
CG
234{
235 struct signalfd_ctx *ctx = f->private_data;
236 sigset_t sigmask;
237
238 sigmask = ctx->sigmask;
239 signotset(&sigmask);
240 render_sigset_t(m, "sigmask:\t", &sigmask);
138d22b5
CG
241}
242#endif
243
fba2afaa 244static const struct file_operations signalfd_fops = {
138d22b5
CG
245#ifdef CONFIG_PROC_FS
246 .show_fdinfo = signalfd_show_fdinfo,
247#endif
fba2afaa
DL
248 .release = signalfd_release,
249 .poll = signalfd_poll,
250 .read = signalfd_read,
6038f373 251 .llseek = noop_llseek,
fba2afaa
DL
252};
253
52fb6db0
DB
254static int do_signalfd4(int ufd, sigset_t __user *user_mask, size_t sizemask,
255 int flags)
fba2afaa 256{
fba2afaa
DL
257 sigset_t sigmask;
258 struct signalfd_ctx *ctx;
fba2afaa 259
e38b36f3
UD
260 /* Check the SFD_* constants for consistency. */
261 BUILD_BUG_ON(SFD_CLOEXEC != O_CLOEXEC);
262 BUILD_BUG_ON(SFD_NONBLOCK != O_NONBLOCK);
263
5fb5e049 264 if (flags & ~(SFD_CLOEXEC | SFD_NONBLOCK))
9deb27ba
UD
265 return -EINVAL;
266
fba2afaa
DL
267 if (sizemask != sizeof(sigset_t) ||
268 copy_from_user(&sigmask, user_mask, sizeof(sigmask)))
f50cadaa 269 return -EINVAL;
fba2afaa
DL
270 sigdelsetmask(&sigmask, sigmask(SIGKILL) | sigmask(SIGSTOP));
271 signotset(&sigmask);
272
273 if (ufd == -1) {
274 ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
275 if (!ctx)
276 return -ENOMEM;
277
fba2afaa 278 ctx->sigmask = sigmask;
fba2afaa
DL
279
280 /*
281 * When we call this, the initialization must be complete, since
282 * anon_inode_getfd() will install the fd.
283 */
7d9dbca3 284 ufd = anon_inode_getfd("[signalfd]", &signalfd_fops, ctx,
628ff7c1 285 O_RDWR | (flags & (O_CLOEXEC | O_NONBLOCK)));
2030a42c
AV
286 if (ufd < 0)
287 kfree(ctx);
fba2afaa 288 } else {
2903ff01
AV
289 struct fd f = fdget(ufd);
290 if (!f.file)
fba2afaa 291 return -EBADF;
2903ff01
AV
292 ctx = f.file->private_data;
293 if (f.file->f_op != &signalfd_fops) {
294 fdput(f);
fba2afaa
DL
295 return -EINVAL;
296 }
b8fceee1
DL
297 spin_lock_irq(&current->sighand->siglock);
298 ctx->sigmask = sigmask;
299 spin_unlock_irq(&current->sighand->siglock);
300
301 wake_up(&current->sighand->signalfd_wqh);
2903ff01 302 fdput(f);
fba2afaa
DL
303 }
304
305 return ufd;
fba2afaa 306}
9deb27ba 307
52fb6db0
DB
308SYSCALL_DEFINE4(signalfd4, int, ufd, sigset_t __user *, user_mask,
309 size_t, sizemask, int, flags)
310{
311 return do_signalfd4(ufd, user_mask, sizemask, flags);
312}
313
836f92ad
HC
314SYSCALL_DEFINE3(signalfd, int, ufd, sigset_t __user *, user_mask,
315 size_t, sizemask)
9deb27ba 316{
52fb6db0 317 return do_signalfd4(ufd, user_mask, sizemask, 0);
9deb27ba 318}
7d197ed4
AV
319
320#ifdef CONFIG_COMPAT
570484bf
DB
321static long do_compat_signalfd4(int ufd,
322 const compat_sigset_t __user *sigmask,
323 compat_size_t sigsetsize, int flags)
7d197ed4 324{
7d197ed4
AV
325 sigset_t tmp;
326 sigset_t __user *ksigmask;
327
328 if (sigsetsize != sizeof(compat_sigset_t))
329 return -EINVAL;
3968cf62 330 if (get_compat_sigset(&tmp, sigmask))
7d197ed4 331 return -EFAULT;
7d197ed4
AV
332 ksigmask = compat_alloc_user_space(sizeof(sigset_t));
333 if (copy_to_user(ksigmask, &tmp, sizeof(sigset_t)))
334 return -EFAULT;
335
52fb6db0 336 return do_signalfd4(ufd, ksigmask, sizeof(sigset_t), flags);
7d197ed4
AV
337}
338
570484bf
DB
339COMPAT_SYSCALL_DEFINE4(signalfd4, int, ufd,
340 const compat_sigset_t __user *, sigmask,
341 compat_size_t, sigsetsize,
342 int, flags)
343{
344 return do_compat_signalfd4(ufd, sigmask, sigsetsize, flags);
345}
346
7d197ed4
AV
347COMPAT_SYSCALL_DEFINE3(signalfd, int, ufd,
348 const compat_sigset_t __user *,sigmask,
349 compat_size_t, sigsetsize)
350{
570484bf 351 return do_compat_signalfd4(ufd, sigmask, sigsetsize, 0);
7d197ed4
AV
352}
353#endif