Marcelo Tosatti | dcc38d1 | 2010-10-11 15:31:15 -0300 | [diff] [blame] | 1 | /* |
| 2 | * signalfd/eventfd compatibility |
| 3 | * |
| 4 | * Copyright IBM, Corp. 2008 |
| 5 | * |
| 6 | * Authors: |
| 7 | * Anthony Liguori <aliguori@us.ibm.com> |
| 8 | * |
| 9 | * This work is licensed under the terms of the GNU GPL, version 2. See |
| 10 | * the COPYING file in the top-level directory. |
| 11 | * |
Paolo Bonzini | 6b620ca | 2012-01-13 17:44:23 +0100 | [diff] [blame] | 12 | * Contributions after 2012-01-13 are licensed under the terms of the |
| 13 | * GNU GPL, version 2 or (at your option) any later version. |
Marcelo Tosatti | dcc38d1 | 2010-10-11 15:31:15 -0300 | [diff] [blame] | 14 | */ |
| 15 | |
Peter Maydell | aafd758 | 2016-01-29 17:49:55 +0000 | [diff] [blame] | 16 | #include "qemu/osdep.h" |
Jan Kiszka | 518420d | 2013-05-02 10:21:18 +0200 | [diff] [blame] | 17 | #include "qemu/thread.h" |
Marcelo Tosatti | dcc38d1 | 2010-10-11 15:31:15 -0300 | [diff] [blame] | 18 | |
David CARLIER | 81b7b1e | 2020-07-13 14:36:10 +0100 | [diff] [blame] | 19 | #if defined(CONFIG_SIGNALFD) |
Kacper Słomiński | 6bd17dc | 2021-09-05 03:16:22 +0200 | [diff] [blame] | 20 | #include <sys/signalfd.h> |
David CARLIER | 81b7b1e | 2020-07-13 14:36:10 +0100 | [diff] [blame] | 21 | #endif |
Marcelo Tosatti | dcc38d1 | 2010-10-11 15:31:15 -0300 | [diff] [blame] | 22 | |
Mahmoud Mandour | 7e3a61c | 2021-03-15 12:58:13 +0200 | [diff] [blame] | 23 | struct sigfd_compat_info { |
Marcelo Tosatti | dcc38d1 | 2010-10-11 15:31:15 -0300 | [diff] [blame] | 24 | sigset_t mask; |
| 25 | int fd; |
| 26 | }; |
| 27 | |
| 28 | static void *sigwait_compat(void *opaque) |
| 29 | { |
| 30 | struct sigfd_compat_info *info = opaque; |
Marcelo Tosatti | dcc38d1 | 2010-10-11 15:31:15 -0300 | [diff] [blame] | 31 | |
Tristan Gingold | 30faaf7 | 2011-02-18 14:17:16 +0100 | [diff] [blame] | 32 | while (1) { |
| 33 | int sig; |
| 34 | int err; |
Marcelo Tosatti | dcc38d1 | 2010-10-11 15:31:15 -0300 | [diff] [blame] | 35 | |
Tristan Gingold | 30faaf7 | 2011-02-18 14:17:16 +0100 | [diff] [blame] | 36 | err = sigwait(&info->mask, &sig); |
| 37 | if (err != 0) { |
| 38 | if (errno == EINTR) { |
| 39 | continue; |
| 40 | } else { |
| 41 | return NULL; |
| 42 | } |
| 43 | } else { |
| 44 | struct qemu_signalfd_siginfo buffer; |
Tristan Gingold | 30faaf7 | 2011-02-18 14:17:16 +0100 | [diff] [blame] | 45 | memset(&buffer, 0, sizeof(buffer)); |
| 46 | buffer.ssi_signo = sig; |
| 47 | |
Marc-André Lureau | d0dedf2 | 2022-04-20 17:26:18 +0400 | [diff] [blame] | 48 | if (qemu_write_full(info->fd, &buffer, sizeof(buffer)) != sizeof(buffer)) { |
| 49 | return NULL; |
Marcelo Tosatti | dcc38d1 | 2010-10-11 15:31:15 -0300 | [diff] [blame] | 50 | } |
| 51 | } |
Tristan Gingold | 30faaf7 | 2011-02-18 14:17:16 +0100 | [diff] [blame] | 52 | } |
Marcelo Tosatti | dcc38d1 | 2010-10-11 15:31:15 -0300 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | static int qemu_signalfd_compat(const sigset_t *mask) |
| 56 | { |
Marcelo Tosatti | dcc38d1 | 2010-10-11 15:31:15 -0300 | [diff] [blame] | 57 | struct sigfd_compat_info *info; |
Jan Kiszka | 518420d | 2013-05-02 10:21:18 +0200 | [diff] [blame] | 58 | QemuThread thread; |
Marcelo Tosatti | dcc38d1 | 2010-10-11 15:31:15 -0300 | [diff] [blame] | 59 | int fds[2]; |
| 60 | |
Mahmoud Mandour | e0c5a18 | 2021-03-15 12:58:14 +0200 | [diff] [blame] | 61 | info = g_malloc(sizeof(*info)); |
Marcelo Tosatti | dcc38d1 | 2010-10-11 15:31:15 -0300 | [diff] [blame] | 62 | |
Marc-André Lureau | 81badab | 2022-04-22 14:45:35 +0400 | [diff] [blame] | 63 | if (!g_unix_open_pipe(fds, FD_CLOEXEC, NULL)) { |
Mahmoud Mandour | e0c5a18 | 2021-03-15 12:58:14 +0200 | [diff] [blame] | 64 | g_free(info); |
Marcelo Tosatti | dcc38d1 | 2010-10-11 15:31:15 -0300 | [diff] [blame] | 65 | return -1; |
| 66 | } |
| 67 | |
Marcelo Tosatti | dcc38d1 | 2010-10-11 15:31:15 -0300 | [diff] [blame] | 68 | memcpy(&info->mask, mask, sizeof(*mask)); |
| 69 | info->fd = fds[1]; |
| 70 | |
Dr. David Alan Gilbert | 4900116 | 2014-01-30 10:20:32 +0000 | [diff] [blame] | 71 | qemu_thread_create(&thread, "signalfd_compat", sigwait_compat, info, |
| 72 | QEMU_THREAD_DETACHED); |
Marcelo Tosatti | dcc38d1 | 2010-10-11 15:31:15 -0300 | [diff] [blame] | 73 | |
| 74 | return fds[0]; |
| 75 | } |
| 76 | |
| 77 | int qemu_signalfd(const sigset_t *mask) |
| 78 | { |
| 79 | #if defined(CONFIG_SIGNALFD) |
| 80 | int ret; |
| 81 | |
Kacper Słomiński | 6bd17dc | 2021-09-05 03:16:22 +0200 | [diff] [blame] | 82 | ret = signalfd(-1, mask, SFD_CLOEXEC); |
Marcelo Tosatti | dcc38d1 | 2010-10-11 15:31:15 -0300 | [diff] [blame] | 83 | if (ret != -1) { |
Marcelo Tosatti | dcc38d1 | 2010-10-11 15:31:15 -0300 | [diff] [blame] | 84 | return ret; |
| 85 | } |
| 86 | #endif |
| 87 | |
| 88 | return qemu_signalfd_compat(mask); |
| 89 | } |