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" |
Marcelo Tosatti | dcc38d1 | 2010-10-11 15:31:15 -0300 | [diff] [blame] | 17 | #include "qemu-common.h" |
Jan Kiszka | 518420d | 2013-05-02 10:21:18 +0200 | [diff] [blame] | 18 | #include "qemu/thread.h" |
Marcelo Tosatti | dcc38d1 | 2010-10-11 15:31:15 -0300 | [diff] [blame] | 19 | |
| 20 | #include <sys/syscall.h> |
Marcelo Tosatti | dcc38d1 | 2010-10-11 15:31:15 -0300 | [diff] [blame] | 21 | |
| 22 | struct sigfd_compat_info |
| 23 | { |
| 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; |
Marcelo Tosatti | dcc38d1 | 2010-10-11 15:31:15 -0300 | [diff] [blame] | 45 | size_t offset = 0; |
| 46 | |
Tristan Gingold | 30faaf7 | 2011-02-18 14:17:16 +0100 | [diff] [blame] | 47 | memset(&buffer, 0, sizeof(buffer)); |
| 48 | buffer.ssi_signo = sig; |
| 49 | |
Marcelo Tosatti | dcc38d1 | 2010-10-11 15:31:15 -0300 | [diff] [blame] | 50 | while (offset < sizeof(buffer)) { |
| 51 | ssize_t len; |
| 52 | |
Tristan Gingold | 30faaf7 | 2011-02-18 14:17:16 +0100 | [diff] [blame] | 53 | len = write(info->fd, (char *)&buffer + offset, |
Marcelo Tosatti | dcc38d1 | 2010-10-11 15:31:15 -0300 | [diff] [blame] | 54 | sizeof(buffer) - offset); |
| 55 | if (len == -1 && errno == EINTR) |
| 56 | continue; |
| 57 | |
| 58 | if (len <= 0) { |
Tristan Gingold | 30faaf7 | 2011-02-18 14:17:16 +0100 | [diff] [blame] | 59 | return NULL; |
Marcelo Tosatti | dcc38d1 | 2010-10-11 15:31:15 -0300 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | offset += len; |
| 63 | } |
| 64 | } |
Tristan Gingold | 30faaf7 | 2011-02-18 14:17:16 +0100 | [diff] [blame] | 65 | } |
Marcelo Tosatti | dcc38d1 | 2010-10-11 15:31:15 -0300 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | static int qemu_signalfd_compat(const sigset_t *mask) |
| 69 | { |
Marcelo Tosatti | dcc38d1 | 2010-10-11 15:31:15 -0300 | [diff] [blame] | 70 | struct sigfd_compat_info *info; |
Jan Kiszka | 518420d | 2013-05-02 10:21:18 +0200 | [diff] [blame] | 71 | QemuThread thread; |
Marcelo Tosatti | dcc38d1 | 2010-10-11 15:31:15 -0300 | [diff] [blame] | 72 | int fds[2]; |
| 73 | |
| 74 | info = malloc(sizeof(*info)); |
| 75 | if (info == NULL) { |
| 76 | errno = ENOMEM; |
| 77 | return -1; |
| 78 | } |
| 79 | |
| 80 | if (pipe(fds) == -1) { |
| 81 | free(info); |
| 82 | return -1; |
| 83 | } |
| 84 | |
| 85 | qemu_set_cloexec(fds[0]); |
| 86 | qemu_set_cloexec(fds[1]); |
| 87 | |
| 88 | memcpy(&info->mask, mask, sizeof(*mask)); |
| 89 | info->fd = fds[1]; |
| 90 | |
Dr. David Alan Gilbert | 4900116 | 2014-01-30 10:20:32 +0000 | [diff] [blame] | 91 | qemu_thread_create(&thread, "signalfd_compat", sigwait_compat, info, |
| 92 | QEMU_THREAD_DETACHED); |
Marcelo Tosatti | dcc38d1 | 2010-10-11 15:31:15 -0300 | [diff] [blame] | 93 | |
| 94 | return fds[0]; |
| 95 | } |
| 96 | |
| 97 | int qemu_signalfd(const sigset_t *mask) |
| 98 | { |
| 99 | #if defined(CONFIG_SIGNALFD) |
| 100 | int ret; |
| 101 | |
| 102 | ret = syscall(SYS_signalfd, -1, mask, _NSIG / 8); |
| 103 | if (ret != -1) { |
| 104 | qemu_set_cloexec(ret); |
| 105 | return ret; |
| 106 | } |
| 107 | #endif |
| 108 | |
| 109 | return qemu_signalfd_compat(mask); |
| 110 | } |