| 1 | /* |
| 2 | * event notifier support |
| 3 | * |
| 4 | * Copyright Red Hat, Inc. 2010 |
| 5 | * |
| 6 | * Authors: |
| 7 | * Michael S. Tsirkin <mst@redhat.com> |
| 8 | * |
| 9 | * This work is licensed under the terms of the GNU GPL, version 2 or later. |
| 10 | * See the COPYING file in the top-level directory. |
| 11 | */ |
| 12 | |
| 13 | #include "qemu/osdep.h" |
| 14 | #include "qapi/error.h" |
| 15 | #include "qemu/cutils.h" |
| 16 | #include "qemu/event_notifier.h" |
| 17 | #include "qemu/main-loop.h" |
| 18 | |
| 19 | #ifdef CONFIG_EVENTFD |
| 20 | #include <sys/eventfd.h> |
| 21 | #endif |
| 22 | |
| 23 | #ifdef CONFIG_EVENTFD |
| 24 | /* |
| 25 | * Initialize @e with existing file descriptor @fd. |
| 26 | * @fd must be a genuine eventfd object, emulation with pipe won't do. |
| 27 | */ |
| 28 | void event_notifier_init_fd(EventNotifier *e, int fd) |
| 29 | { |
| 30 | e->rfd = fd; |
| 31 | e->wfd = fd; |
| 32 | e->initialized = true; |
| 33 | } |
| 34 | #endif |
| 35 | |
| 36 | int event_notifier_init(EventNotifier *e, int active) |
| 37 | { |
| 38 | int fds[2]; |
| 39 | int ret; |
| 40 | Error *local_err = NULL; |
| 41 | |
| 42 | #ifdef CONFIG_EVENTFD |
| 43 | ret = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC); |
| 44 | #else |
| 45 | ret = -1; |
| 46 | errno = ENOSYS; |
| 47 | #endif |
| 48 | if (ret >= 0) { |
| 49 | e->rfd = e->wfd = ret; |
| 50 | } else { |
| 51 | if (errno != ENOSYS) { |
| 52 | return -errno; |
| 53 | } |
| 54 | if (!g_unix_open_pipe(fds, FD_CLOEXEC, NULL)) { |
| 55 | return -errno; |
| 56 | } |
| 57 | if (!qemu_set_blocking(fds[0], false, &local_err)) { |
| 58 | ret = -errno; |
| 59 | goto fail; |
| 60 | } |
| 61 | if (!qemu_set_blocking(fds[1], false, &local_err)) { |
| 62 | ret = -errno; |
| 63 | goto fail; |
| 64 | } |
| 65 | e->rfd = fds[0]; |
| 66 | e->wfd = fds[1]; |
| 67 | } |
| 68 | e->initialized = true; |
| 69 | if (active) { |
| 70 | event_notifier_set(e); |
| 71 | } |
| 72 | return 0; |
| 73 | |
| 74 | fail: |
| 75 | error_report_err(local_err); |
| 76 | close(fds[0]); |
| 77 | close(fds[1]); |
| 78 | return ret; |
| 79 | } |
| 80 | |
| 81 | void event_notifier_cleanup(EventNotifier *e) |
| 82 | { |
| 83 | if (!e->initialized) { |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | if (e->rfd != e->wfd) { |
| 88 | close(e->rfd); |
| 89 | } |
| 90 | |
| 91 | e->rfd = -1; |
| 92 | close(e->wfd); |
| 93 | e->wfd = -1; |
| 94 | e->initialized = false; |
| 95 | } |
| 96 | |
| 97 | int event_notifier_get_fd(const EventNotifier *e) |
| 98 | { |
| 99 | return e->rfd; |
| 100 | } |
| 101 | |
| 102 | int event_notifier_get_wfd(const EventNotifier *e) |
| 103 | { |
| 104 | return e->wfd; |
| 105 | } |
| 106 | |
| 107 | int event_notifier_set(EventNotifier *e) |
| 108 | { |
| 109 | static const uint64_t value = 1; |
| 110 | ssize_t ret; |
| 111 | |
| 112 | if (!e->initialized) { |
| 113 | return -1; |
| 114 | } |
| 115 | |
| 116 | do { |
| 117 | ret = write(e->wfd, &value, sizeof(value)); |
| 118 | } while (ret < 0 && errno == EINTR); |
| 119 | |
| 120 | /* EAGAIN is fine, a read must be pending. */ |
| 121 | if (ret < 0 && errno != EAGAIN) { |
| 122 | return -errno; |
| 123 | } |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | int event_notifier_test_and_clear(EventNotifier *e) |
| 128 | { |
| 129 | int value; |
| 130 | ssize_t len; |
| 131 | char buffer[512]; |
| 132 | |
| 133 | if (!e->initialized) { |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */ |
| 138 | value = 0; |
| 139 | do { |
| 140 | len = read(e->rfd, buffer, sizeof(buffer)); |
| 141 | value |= (len > 0); |
| 142 | } while ((len == -1 && errno == EINTR) || len == sizeof(buffer)); |
| 143 | |
| 144 | return value; |
| 145 | } |