signal: Rename SIL_PERF_EVENT SIL_FAULT_PERF_EVENT for consistency
[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,
ae7795bc 82 kernel_siginfo_t const *kinfo)
fba2afaa 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;
31931c93
EB
115 case SIL_FAULT_BNDERR:
116 case SIL_FAULT_PKUERR:
f4ac7302 117 case SIL_FAULT_PERF_EVENT:
31931c93 118 /*
922e3013 119 * Fall through to the SIL_FAULT case. SIL_FAULT_BNDERR,
f4ac7302 120 * SIL_FAULT_PKUERR, and SIL_FAULT_PERF_EVENT are only
922e3013
EB
121 * generated by faults that deliver them synchronously to
122 * userspace. In case someone injects one of these signals
123 * and signalfd catches it treat it as SIL_FAULT.
31931c93 124 */
cc731525 125 case SIL_FAULT:
5611f55e 126 new.ssi_addr = (long) kinfo->si_addr;
9abcabe3
EB
127 break;
128 case SIL_FAULT_TRAPNO:
129 new.ssi_addr = (long) kinfo->si_addr;
5611f55e 130 new.ssi_trapno = kinfo->si_trapno;
31931c93
EB
131 break;
132 case SIL_FAULT_MCEERR:
133 new.ssi_addr = (long) kinfo->si_addr;
31931c93 134 new.ssi_addr_lsb = (short) kinfo->si_addr_lsb;
fba2afaa 135 break;
cc731525 136 case SIL_CHLD:
5611f55e
EB
137 new.ssi_pid = kinfo->si_pid;
138 new.ssi_uid = kinfo->si_uid;
139 new.ssi_status = kinfo->si_status;
140 new.ssi_utime = kinfo->si_utime;
141 new.ssi_stime = kinfo->si_stime;
fba2afaa 142 break;
cc731525 143 case SIL_RT:
0859ab59
DL
144 /*
145 * This case catches also the signals queued by sigqueue().
146 */
5611f55e
EB
147 new.ssi_pid = kinfo->si_pid;
148 new.ssi_uid = kinfo->si_uid;
149 new.ssi_ptr = (long) kinfo->si_ptr;
150 new.ssi_int = kinfo->si_int;
fba2afaa 151 break;
76b7f670
EB
152 case SIL_SYS:
153 new.ssi_call_addr = (long) kinfo->si_call_addr;
154 new.ssi_syscall = kinfo->si_syscall;
155 new.ssi_arch = kinfo->si_arch;
156 break;
fba2afaa
DL
157 }
158
5611f55e
EB
159 if (copy_to_user(uinfo, &new, sizeof(struct signalfd_siginfo)))
160 return -EFAULT;
161
162 return sizeof(*uinfo);
fba2afaa
DL
163}
164
ae7795bc 165static ssize_t signalfd_dequeue(struct signalfd_ctx *ctx, kernel_siginfo_t *info,
b3762bfc
DA
166 int nonblock)
167{
168 ssize_t ret;
b3762bfc
DA
169 DECLARE_WAITQUEUE(wait, current);
170
b8fceee1
DL
171 spin_lock_irq(&current->sighand->siglock);
172 ret = dequeue_signal(current, &ctx->sigmask, info);
b3762bfc
DA
173 switch (ret) {
174 case 0:
175 if (!nonblock)
176 break;
177 ret = -EAGAIN;
df561f66 178 fallthrough;
b3762bfc 179 default:
b8fceee1 180 spin_unlock_irq(&current->sighand->siglock);
b3762bfc
DA
181 return ret;
182 }
183
b8fceee1 184 add_wait_queue(&current->sighand->signalfd_wqh, &wait);
b3762bfc
DA
185 for (;;) {
186 set_current_state(TASK_INTERRUPTIBLE);
b8fceee1 187 ret = dequeue_signal(current, &ctx->sigmask, info);
b3762bfc
DA
188 if (ret != 0)
189 break;
190 if (signal_pending(current)) {
191 ret = -ERESTARTSYS;
192 break;
193 }
b8fceee1 194 spin_unlock_irq(&current->sighand->siglock);
b3762bfc 195 schedule();
b8fceee1 196 spin_lock_irq(&current->sighand->siglock);
b3762bfc 197 }
b8fceee1 198 spin_unlock_irq(&current->sighand->siglock);
b3762bfc 199
b8fceee1 200 remove_wait_queue(&current->sighand->signalfd_wqh, &wait);
b3762bfc
DA
201 __set_current_state(TASK_RUNNING);
202
203 return ret;
204}
205
fba2afaa 206/*
b8fceee1
DL
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".
fba2afaa
DL
210 */
211static ssize_t signalfd_read(struct file *file, char __user *buf, size_t count,
212 loff_t *ppos)
213{
214 struct signalfd_ctx *ctx = file->private_data;
b3762bfc
DA
215 struct signalfd_siginfo __user *siginfo;
216 int nonblock = file->f_flags & O_NONBLOCK;
217 ssize_t ret, total = 0;
ae7795bc 218 kernel_siginfo_t info;
fba2afaa 219
b3762bfc
DA
220 count /= sizeof(struct signalfd_siginfo);
221 if (!count)
fba2afaa 222 return -EINVAL;
fba2afaa 223
b3762bfc 224 siginfo = (struct signalfd_siginfo __user *) buf;
b3762bfc
DA
225 do {
226 ret = signalfd_dequeue(ctx, &info, nonblock);
227 if (unlikely(ret <= 0))
228 break;
229 ret = signalfd_copyinfo(siginfo, &info);
230 if (ret < 0)
231 break;
232 siginfo++;
233 total += ret;
234 nonblock = 1;
235 } while (--count);
236
b8fceee1 237 return total ? total: ret;
fba2afaa
DL
238}
239
138d22b5 240#ifdef CONFIG_PROC_FS
a3816ab0 241static void signalfd_show_fdinfo(struct seq_file *m, struct file *f)
138d22b5
CG
242{
243 struct signalfd_ctx *ctx = f->private_data;
244 sigset_t sigmask;
245
246 sigmask = ctx->sigmask;
247 signotset(&sigmask);
248 render_sigset_t(m, "sigmask:\t", &sigmask);
138d22b5
CG
249}
250#endif
251
fba2afaa 252static const struct file_operations signalfd_fops = {
138d22b5
CG
253#ifdef CONFIG_PROC_FS
254 .show_fdinfo = signalfd_show_fdinfo,
255#endif
fba2afaa
DL
256 .release = signalfd_release,
257 .poll = signalfd_poll,
258 .read = signalfd_read,
6038f373 259 .llseek = noop_llseek,
fba2afaa
DL
260};
261
5ed0127f 262static int do_signalfd4(int ufd, sigset_t *mask, int flags)
fba2afaa 263{
fba2afaa 264 struct signalfd_ctx *ctx;
fba2afaa 265
e38b36f3
UD
266 /* Check the SFD_* constants for consistency. */
267 BUILD_BUG_ON(SFD_CLOEXEC != O_CLOEXEC);
268 BUILD_BUG_ON(SFD_NONBLOCK != O_NONBLOCK);
269
5fb5e049 270 if (flags & ~(SFD_CLOEXEC | SFD_NONBLOCK))
9deb27ba
UD
271 return -EINVAL;
272
5ed0127f
AV
273 sigdelsetmask(mask, sigmask(SIGKILL) | sigmask(SIGSTOP));
274 signotset(mask);
fba2afaa
DL
275
276 if (ufd == -1) {
277 ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
278 if (!ctx)
279 return -ENOMEM;
280
5ed0127f 281 ctx->sigmask = *mask;
fba2afaa
DL
282
283 /*
284 * When we call this, the initialization must be complete, since
285 * anon_inode_getfd() will install the fd.
286 */
7d9dbca3 287 ufd = anon_inode_getfd("[signalfd]", &signalfd_fops, ctx,
628ff7c1 288 O_RDWR | (flags & (O_CLOEXEC | O_NONBLOCK)));
2030a42c
AV
289 if (ufd < 0)
290 kfree(ctx);
fba2afaa 291 } else {
2903ff01
AV
292 struct fd f = fdget(ufd);
293 if (!f.file)
fba2afaa 294 return -EBADF;
2903ff01
AV
295 ctx = f.file->private_data;
296 if (f.file->f_op != &signalfd_fops) {
297 fdput(f);
fba2afaa
DL
298 return -EINVAL;
299 }
b8fceee1 300 spin_lock_irq(&current->sighand->siglock);
5ed0127f 301 ctx->sigmask = *mask;
b8fceee1
DL
302 spin_unlock_irq(&current->sighand->siglock);
303
304 wake_up(&current->sighand->signalfd_wqh);
2903ff01 305 fdput(f);
fba2afaa
DL
306 }
307
308 return ufd;
fba2afaa 309}
9deb27ba 310
52fb6db0
DB
311SYSCALL_DEFINE4(signalfd4, int, ufd, sigset_t __user *, user_mask,
312 size_t, sizemask, int, flags)
313{
5ed0127f
AV
314 sigset_t mask;
315
a089e3fd 316 if (sizemask != sizeof(sigset_t))
5ed0127f 317 return -EINVAL;
a089e3fd
HD
318 if (copy_from_user(&mask, user_mask, sizeof(mask)))
319 return -EFAULT;
5ed0127f 320 return do_signalfd4(ufd, &mask, flags);
52fb6db0
DB
321}
322
836f92ad
HC
323SYSCALL_DEFINE3(signalfd, int, ufd, sigset_t __user *, user_mask,
324 size_t, sizemask)
9deb27ba 325{
5ed0127f
AV
326 sigset_t mask;
327
a089e3fd 328 if (sizemask != sizeof(sigset_t))
5ed0127f 329 return -EINVAL;
a089e3fd
HD
330 if (copy_from_user(&mask, user_mask, sizeof(mask)))
331 return -EFAULT;
5ed0127f 332 return do_signalfd4(ufd, &mask, 0);
9deb27ba 333}
7d197ed4
AV
334
335#ifdef CONFIG_COMPAT
570484bf 336static long do_compat_signalfd4(int ufd,
5ed0127f 337 const compat_sigset_t __user *user_mask,
570484bf 338 compat_size_t sigsetsize, int flags)
7d197ed4 339{
5ed0127f 340 sigset_t mask;
7d197ed4
AV
341
342 if (sigsetsize != sizeof(compat_sigset_t))
343 return -EINVAL;
5ed0127f 344 if (get_compat_sigset(&mask, user_mask))
7d197ed4 345 return -EFAULT;
5ed0127f 346 return do_signalfd4(ufd, &mask, flags);
7d197ed4
AV
347}
348
570484bf 349COMPAT_SYSCALL_DEFINE4(signalfd4, int, ufd,
5ed0127f 350 const compat_sigset_t __user *, user_mask,
570484bf
DB
351 compat_size_t, sigsetsize,
352 int, flags)
353{
5ed0127f 354 return do_compat_signalfd4(ufd, user_mask, sigsetsize, flags);
570484bf
DB
355}
356
7d197ed4 357COMPAT_SYSCALL_DEFINE3(signalfd, int, ufd,
5ed0127f 358 const compat_sigset_t __user *, user_mask,
7d197ed4
AV
359 compat_size_t, sigsetsize)
360{
5ed0127f 361 return do_compat_signalfd4(ufd, user_mask, sigsetsize, 0);
7d197ed4
AV
362}
363#endif