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