]> git.ipfire.org Git - people/ms/linux.git/blame - fs/signalfd.c
Merge tag 'soc-fixes-6.0-rc7' of git://git.kernel.org/pub/scm/linux/kernel/git/soc/soc
[people/ms/linux.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{
9537bae0 38 wake_up_pollfree(&sighand->signalfd_wqh);
d80e731e
ON
39}
40
fba2afaa 41struct signalfd_ctx {
fba2afaa 42 sigset_t sigmask;
fba2afaa
DL
43};
44
fba2afaa
DL
45static int signalfd_release(struct inode *inode, struct file *file)
46{
b8fceee1 47 kfree(file->private_data);
fba2afaa
DL
48 return 0;
49}
50
076ccb76 51static __poll_t signalfd_poll(struct file *file, poll_table *wait)
fba2afaa
DL
52{
53 struct signalfd_ctx *ctx = file->private_data;
076ccb76 54 __poll_t events = 0;
fba2afaa 55
b8fceee1 56 poll_wait(file, &current->sighand->signalfd_wqh, wait);
fba2afaa 57
b8fceee1
DL
58 spin_lock_irq(&current->sighand->siglock);
59 if (next_signal(&current->pending, &ctx->sigmask) ||
60 next_signal(&current->signal->shared_pending,
61 &ctx->sigmask))
a9a08845 62 events |= EPOLLIN;
b8fceee1 63 spin_unlock_irq(&current->sighand->siglock);
fba2afaa
DL
64
65 return events;
66}
67
68/*
69 * Copied from copy_siginfo_to_user() in kernel/signal.c
70 */
71static int signalfd_copyinfo(struct signalfd_siginfo __user *uinfo,
ae7795bc 72 kernel_siginfo_t const *kinfo)
fba2afaa 73{
5611f55e 74 struct signalfd_siginfo new;
fba2afaa
DL
75
76 BUILD_BUG_ON(sizeof(struct signalfd_siginfo) != 128);
77
78 /*
14e4a0f2 79 * Unused members should be zero ...
fba2afaa 80 */
5611f55e 81 memset(&new, 0, sizeof(new));
fba2afaa
DL
82
83 /*
84 * If you change siginfo_t structure, please be sure
85 * this code is fixed accordingly.
86 */
5611f55e
EB
87 new.ssi_signo = kinfo->si_signo;
88 new.ssi_errno = kinfo->si_errno;
89 new.ssi_code = kinfo->si_code;
cc731525
EB
90 switch (siginfo_layout(kinfo->si_signo, kinfo->si_code)) {
91 case SIL_KILL:
5611f55e
EB
92 new.ssi_pid = kinfo->si_pid;
93 new.ssi_uid = kinfo->si_uid;
fba2afaa 94 break;
cc731525 95 case SIL_TIMER:
5611f55e
EB
96 new.ssi_tid = kinfo->si_tid;
97 new.ssi_overrun = kinfo->si_overrun;
98 new.ssi_ptr = (long) kinfo->si_ptr;
99 new.ssi_int = kinfo->si_int;
fba2afaa 100 break;
cc731525 101 case SIL_POLL:
5611f55e
EB
102 new.ssi_band = kinfo->si_band;
103 new.ssi_fd = kinfo->si_fd;
fba2afaa 104 break;
31931c93
EB
105 case SIL_FAULT_BNDERR:
106 case SIL_FAULT_PKUERR:
f4ac7302 107 case SIL_FAULT_PERF_EVENT:
31931c93 108 /*
922e3013 109 * Fall through to the SIL_FAULT case. SIL_FAULT_BNDERR,
f4ac7302 110 * SIL_FAULT_PKUERR, and SIL_FAULT_PERF_EVENT are only
922e3013
EB
111 * generated by faults that deliver them synchronously to
112 * userspace. In case someone injects one of these signals
113 * and signalfd catches it treat it as SIL_FAULT.
31931c93 114 */
cc731525 115 case SIL_FAULT:
5611f55e 116 new.ssi_addr = (long) kinfo->si_addr;
9abcabe3
EB
117 break;
118 case SIL_FAULT_TRAPNO:
119 new.ssi_addr = (long) kinfo->si_addr;
5611f55e 120 new.ssi_trapno = kinfo->si_trapno;
31931c93
EB
121 break;
122 case SIL_FAULT_MCEERR:
123 new.ssi_addr = (long) kinfo->si_addr;
31931c93 124 new.ssi_addr_lsb = (short) kinfo->si_addr_lsb;
fba2afaa 125 break;
cc731525 126 case SIL_CHLD:
5611f55e
EB
127 new.ssi_pid = kinfo->si_pid;
128 new.ssi_uid = kinfo->si_uid;
129 new.ssi_status = kinfo->si_status;
130 new.ssi_utime = kinfo->si_utime;
131 new.ssi_stime = kinfo->si_stime;
fba2afaa 132 break;
cc731525 133 case SIL_RT:
0859ab59
DL
134 /*
135 * This case catches also the signals queued by sigqueue().
136 */
5611f55e
EB
137 new.ssi_pid = kinfo->si_pid;
138 new.ssi_uid = kinfo->si_uid;
139 new.ssi_ptr = (long) kinfo->si_ptr;
140 new.ssi_int = kinfo->si_int;
fba2afaa 141 break;
76b7f670
EB
142 case SIL_SYS:
143 new.ssi_call_addr = (long) kinfo->si_call_addr;
144 new.ssi_syscall = kinfo->si_syscall;
145 new.ssi_arch = kinfo->si_arch;
146 break;
fba2afaa
DL
147 }
148
5611f55e
EB
149 if (copy_to_user(uinfo, &new, sizeof(struct signalfd_siginfo)))
150 return -EFAULT;
151
152 return sizeof(*uinfo);
fba2afaa
DL
153}
154
ae7795bc 155static ssize_t signalfd_dequeue(struct signalfd_ctx *ctx, kernel_siginfo_t *info,
b3762bfc
DA
156 int nonblock)
157{
5768d890 158 enum pid_type type;
b3762bfc 159 ssize_t ret;
b3762bfc
DA
160 DECLARE_WAITQUEUE(wait, current);
161
b8fceee1 162 spin_lock_irq(&current->sighand->siglock);
5768d890 163 ret = dequeue_signal(current, &ctx->sigmask, info, &type);
b3762bfc
DA
164 switch (ret) {
165 case 0:
166 if (!nonblock)
167 break;
168 ret = -EAGAIN;
df561f66 169 fallthrough;
b3762bfc 170 default:
b8fceee1 171 spin_unlock_irq(&current->sighand->siglock);
b3762bfc
DA
172 return ret;
173 }
174
b8fceee1 175 add_wait_queue(&current->sighand->signalfd_wqh, &wait);
b3762bfc
DA
176 for (;;) {
177 set_current_state(TASK_INTERRUPTIBLE);
5768d890 178 ret = dequeue_signal(current, &ctx->sigmask, info, &type);
b3762bfc
DA
179 if (ret != 0)
180 break;
181 if (signal_pending(current)) {
182 ret = -ERESTARTSYS;
183 break;
184 }
b8fceee1 185 spin_unlock_irq(&current->sighand->siglock);
b3762bfc 186 schedule();
b8fceee1 187 spin_lock_irq(&current->sighand->siglock);
b3762bfc 188 }
b8fceee1 189 spin_unlock_irq(&current->sighand->siglock);
b3762bfc 190
b8fceee1 191 remove_wait_queue(&current->sighand->signalfd_wqh, &wait);
b3762bfc
DA
192 __set_current_state(TASK_RUNNING);
193
194 return ret;
195}
196
fba2afaa 197/*
b8fceee1
DL
198 * Returns a multiple of the size of a "struct signalfd_siginfo", or a negative
199 * error code. The "count" parameter must be at least the size of a
200 * "struct signalfd_siginfo".
fba2afaa
DL
201 */
202static ssize_t signalfd_read(struct file *file, char __user *buf, size_t count,
203 loff_t *ppos)
204{
205 struct signalfd_ctx *ctx = file->private_data;
b3762bfc
DA
206 struct signalfd_siginfo __user *siginfo;
207 int nonblock = file->f_flags & O_NONBLOCK;
208 ssize_t ret, total = 0;
ae7795bc 209 kernel_siginfo_t info;
fba2afaa 210
b3762bfc
DA
211 count /= sizeof(struct signalfd_siginfo);
212 if (!count)
fba2afaa 213 return -EINVAL;
fba2afaa 214
b3762bfc 215 siginfo = (struct signalfd_siginfo __user *) buf;
b3762bfc
DA
216 do {
217 ret = signalfd_dequeue(ctx, &info, nonblock);
218 if (unlikely(ret <= 0))
219 break;
220 ret = signalfd_copyinfo(siginfo, &info);
221 if (ret < 0)
222 break;
223 siginfo++;
224 total += ret;
225 nonblock = 1;
226 } while (--count);
227
b8fceee1 228 return total ? total: ret;
fba2afaa
DL
229}
230
138d22b5 231#ifdef CONFIG_PROC_FS
a3816ab0 232static void signalfd_show_fdinfo(struct seq_file *m, struct file *f)
138d22b5
CG
233{
234 struct signalfd_ctx *ctx = f->private_data;
235 sigset_t sigmask;
236
237 sigmask = ctx->sigmask;
238 signotset(&sigmask);
239 render_sigset_t(m, "sigmask:\t", &sigmask);
138d22b5
CG
240}
241#endif
242
fba2afaa 243static const struct file_operations signalfd_fops = {
138d22b5
CG
244#ifdef CONFIG_PROC_FS
245 .show_fdinfo = signalfd_show_fdinfo,
246#endif
fba2afaa
DL
247 .release = signalfd_release,
248 .poll = signalfd_poll,
249 .read = signalfd_read,
6038f373 250 .llseek = noop_llseek,
fba2afaa
DL
251};
252
5ed0127f 253static int do_signalfd4(int ufd, sigset_t *mask, int flags)
fba2afaa 254{
fba2afaa 255 struct signalfd_ctx *ctx;
fba2afaa 256
e38b36f3
UD
257 /* Check the SFD_* constants for consistency. */
258 BUILD_BUG_ON(SFD_CLOEXEC != O_CLOEXEC);
259 BUILD_BUG_ON(SFD_NONBLOCK != O_NONBLOCK);
260
5fb5e049 261 if (flags & ~(SFD_CLOEXEC | SFD_NONBLOCK))
9deb27ba
UD
262 return -EINVAL;
263
5ed0127f
AV
264 sigdelsetmask(mask, sigmask(SIGKILL) | sigmask(SIGSTOP));
265 signotset(mask);
fba2afaa
DL
266
267 if (ufd == -1) {
268 ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
269 if (!ctx)
270 return -ENOMEM;
271
5ed0127f 272 ctx->sigmask = *mask;
fba2afaa
DL
273
274 /*
275 * When we call this, the initialization must be complete, since
276 * anon_inode_getfd() will install the fd.
277 */
7d9dbca3 278 ufd = anon_inode_getfd("[signalfd]", &signalfd_fops, ctx,
628ff7c1 279 O_RDWR | (flags & (O_CLOEXEC | O_NONBLOCK)));
2030a42c
AV
280 if (ufd < 0)
281 kfree(ctx);
fba2afaa 282 } else {
2903ff01
AV
283 struct fd f = fdget(ufd);
284 if (!f.file)
fba2afaa 285 return -EBADF;
2903ff01
AV
286 ctx = f.file->private_data;
287 if (f.file->f_op != &signalfd_fops) {
288 fdput(f);
fba2afaa
DL
289 return -EINVAL;
290 }
b8fceee1 291 spin_lock_irq(&current->sighand->siglock);
5ed0127f 292 ctx->sigmask = *mask;
b8fceee1
DL
293 spin_unlock_irq(&current->sighand->siglock);
294
295 wake_up(&current->sighand->signalfd_wqh);
2903ff01 296 fdput(f);
fba2afaa
DL
297 }
298
299 return ufd;
fba2afaa 300}
9deb27ba 301
52fb6db0
DB
302SYSCALL_DEFINE4(signalfd4, int, ufd, sigset_t __user *, user_mask,
303 size_t, sizemask, int, flags)
304{
5ed0127f
AV
305 sigset_t mask;
306
a089e3fd 307 if (sizemask != sizeof(sigset_t))
5ed0127f 308 return -EINVAL;
a089e3fd
HD
309 if (copy_from_user(&mask, user_mask, sizeof(mask)))
310 return -EFAULT;
5ed0127f 311 return do_signalfd4(ufd, &mask, flags);
52fb6db0
DB
312}
313
836f92ad
HC
314SYSCALL_DEFINE3(signalfd, int, ufd, sigset_t __user *, user_mask,
315 size_t, sizemask)
9deb27ba 316{
5ed0127f
AV
317 sigset_t mask;
318
a089e3fd 319 if (sizemask != sizeof(sigset_t))
5ed0127f 320 return -EINVAL;
a089e3fd
HD
321 if (copy_from_user(&mask, user_mask, sizeof(mask)))
322 return -EFAULT;
5ed0127f 323 return do_signalfd4(ufd, &mask, 0);
9deb27ba 324}
7d197ed4
AV
325
326#ifdef CONFIG_COMPAT
570484bf 327static long do_compat_signalfd4(int ufd,
5ed0127f 328 const compat_sigset_t __user *user_mask,
570484bf 329 compat_size_t sigsetsize, int flags)
7d197ed4 330{
5ed0127f 331 sigset_t mask;
7d197ed4
AV
332
333 if (sigsetsize != sizeof(compat_sigset_t))
334 return -EINVAL;
5ed0127f 335 if (get_compat_sigset(&mask, user_mask))
7d197ed4 336 return -EFAULT;
5ed0127f 337 return do_signalfd4(ufd, &mask, flags);
7d197ed4
AV
338}
339
570484bf 340COMPAT_SYSCALL_DEFINE4(signalfd4, int, ufd,
5ed0127f 341 const compat_sigset_t __user *, user_mask,
570484bf
DB
342 compat_size_t, sigsetsize,
343 int, flags)
344{
5ed0127f 345 return do_compat_signalfd4(ufd, user_mask, sigsetsize, flags);
570484bf
DB
346}
347
7d197ed4 348COMPAT_SYSCALL_DEFINE3(signalfd, int, ufd,
5ed0127f 349 const compat_sigset_t __user *, user_mask,
7d197ed4
AV
350 compat_size_t, sigsetsize)
351{
5ed0127f 352 return do_compat_signalfd4(ufd, user_mask, sigsetsize, 0);
7d197ed4
AV
353}
354#endif