Michael S. Tsirkin | 2292b33 | 2010-03-17 13:07:58 +0200 | [diff] [blame] | 1 | /* |
| 2 | * event notifier support |
| 3 | * |
| 4 | * Copyright Red Hat, Inc. 2010 |
| 5 | * |
| 6 | * Authors: |
| 7 | * Michael S. Tsirkin <mst@redhat.com> |
| 8 | * |
Paolo Bonzini | 6b620ca | 2012-01-13 17:44:23 +0100 | [diff] [blame] | 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. |
Michael S. Tsirkin | 2292b33 | 2010-03-17 13:07:58 +0200 | [diff] [blame] | 11 | */ |
| 12 | |
Peter Maydell | aafd758 | 2016-01-29 17:49:55 +0000 | [diff] [blame] | 13 | #include "qemu/osdep.h" |
Paolo Bonzini | e80c262 | 2012-07-05 17:16:24 +0200 | [diff] [blame] | 14 | #include "qemu-common.h" |
Veronia Bahaa | f348b6d | 2016-03-20 19:16:19 +0200 | [diff] [blame] | 15 | #include "qemu/cutils.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 16 | #include "qemu/event_notifier.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 17 | #include "qemu/main-loop.h" |
Paolo Bonzini | e80c262 | 2012-07-05 17:16:24 +0200 | [diff] [blame] | 18 | |
Michael S. Tsirkin | 2292b33 | 2010-03-17 13:07:58 +0200 | [diff] [blame] | 19 | #ifdef CONFIG_EVENTFD |
| 20 | #include <sys/eventfd.h> |
| 21 | #endif |
| 22 | |
Markus Armbruster | 330b583 | 2016-03-15 19:34:20 +0100 | [diff] [blame] | 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 | */ |
Paolo Bonzini | e80c262 | 2012-07-05 17:16:24 +0200 | [diff] [blame] | 28 | void event_notifier_init_fd(EventNotifier *e, int fd) |
| 29 | { |
Paolo Bonzini | d0cc2fb | 2010-05-24 17:17:04 +0200 | [diff] [blame] | 30 | e->rfd = fd; |
| 31 | e->wfd = fd; |
Paolo Bonzini | e80c262 | 2012-07-05 17:16:24 +0200 | [diff] [blame] | 32 | } |
Markus Armbruster | 330b583 | 2016-03-15 19:34:20 +0100 | [diff] [blame] | 33 | #endif |
Paolo Bonzini | e80c262 | 2012-07-05 17:16:24 +0200 | [diff] [blame] | 34 | |
Michael S. Tsirkin | 2292b33 | 2010-03-17 13:07:58 +0200 | [diff] [blame] | 35 | int event_notifier_init(EventNotifier *e, int active) |
| 36 | { |
Paolo Bonzini | d0cc2fb | 2010-05-24 17:17:04 +0200 | [diff] [blame] | 37 | int fds[2]; |
| 38 | int ret; |
| 39 | |
Michael S. Tsirkin | 2292b33 | 2010-03-17 13:07:58 +0200 | [diff] [blame] | 40 | #ifdef CONFIG_EVENTFD |
Paolo Bonzini | d0cc2fb | 2010-05-24 17:17:04 +0200 | [diff] [blame] | 41 | ret = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC); |
Michael S. Tsirkin | 2292b33 | 2010-03-17 13:07:58 +0200 | [diff] [blame] | 42 | #else |
Paolo Bonzini | d0cc2fb | 2010-05-24 17:17:04 +0200 | [diff] [blame] | 43 | ret = -1; |
| 44 | errno = ENOSYS; |
Michael S. Tsirkin | 2292b33 | 2010-03-17 13:07:58 +0200 | [diff] [blame] | 45 | #endif |
Paolo Bonzini | d0cc2fb | 2010-05-24 17:17:04 +0200 | [diff] [blame] | 46 | if (ret >= 0) { |
| 47 | e->rfd = e->wfd = ret; |
| 48 | } else { |
| 49 | if (errno != ENOSYS) { |
| 50 | return -errno; |
| 51 | } |
| 52 | if (qemu_pipe(fds) < 0) { |
| 53 | return -errno; |
| 54 | } |
| 55 | ret = fcntl_setfl(fds[0], O_NONBLOCK); |
| 56 | if (ret < 0) { |
| 57 | ret = -errno; |
| 58 | goto fail; |
| 59 | } |
| 60 | ret = fcntl_setfl(fds[1], O_NONBLOCK); |
| 61 | if (ret < 0) { |
| 62 | ret = -errno; |
| 63 | goto fail; |
| 64 | } |
| 65 | e->rfd = fds[0]; |
| 66 | e->wfd = fds[1]; |
| 67 | } |
| 68 | if (active) { |
| 69 | event_notifier_set(e); |
| 70 | } |
| 71 | return 0; |
| 72 | |
| 73 | fail: |
| 74 | close(fds[0]); |
| 75 | close(fds[1]); |
| 76 | return ret; |
Michael S. Tsirkin | 2292b33 | 2010-03-17 13:07:58 +0200 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | void event_notifier_cleanup(EventNotifier *e) |
| 80 | { |
Paolo Bonzini | d0cc2fb | 2010-05-24 17:17:04 +0200 | [diff] [blame] | 81 | if (e->rfd != e->wfd) { |
| 82 | close(e->rfd); |
| 83 | } |
Frediano Ziglio | 105e102 | 2019-10-23 13:26:51 +0100 | [diff] [blame] | 84 | e->rfd = -1; |
Paolo Bonzini | d0cc2fb | 2010-05-24 17:17:04 +0200 | [diff] [blame] | 85 | close(e->wfd); |
Halil Pasic | aa26292 | 2017-03-02 19:13:08 +0100 | [diff] [blame] | 86 | e->wfd = -1; |
Michael S. Tsirkin | 2292b33 | 2010-03-17 13:07:58 +0200 | [diff] [blame] | 87 | } |
| 88 | |
Marc-André Lureau | 12f0b68 | 2015-10-13 12:12:16 +0200 | [diff] [blame] | 89 | int event_notifier_get_fd(const EventNotifier *e) |
Michael S. Tsirkin | 2292b33 | 2010-03-17 13:07:58 +0200 | [diff] [blame] | 90 | { |
Paolo Bonzini | d0cc2fb | 2010-05-24 17:17:04 +0200 | [diff] [blame] | 91 | return e->rfd; |
Michael S. Tsirkin | 2292b33 | 2010-03-17 13:07:58 +0200 | [diff] [blame] | 92 | } |
| 93 | |
Paolo Bonzini | 2ec10b9 | 2012-07-05 17:16:22 +0200 | [diff] [blame] | 94 | int event_notifier_set(EventNotifier *e) |
| 95 | { |
Paolo Bonzini | d0cc2fb | 2010-05-24 17:17:04 +0200 | [diff] [blame] | 96 | static const uint64_t value = 1; |
| 97 | ssize_t ret; |
| 98 | |
| 99 | do { |
| 100 | ret = write(e->wfd, &value, sizeof(value)); |
| 101 | } while (ret < 0 && errno == EINTR); |
| 102 | |
| 103 | /* EAGAIN is fine, a read must be pending. */ |
| 104 | if (ret < 0 && errno != EAGAIN) { |
| 105 | return -errno; |
| 106 | } |
| 107 | return 0; |
Paolo Bonzini | 2ec10b9 | 2012-07-05 17:16:22 +0200 | [diff] [blame] | 108 | } |
| 109 | |
Michael S. Tsirkin | 2292b33 | 2010-03-17 13:07:58 +0200 | [diff] [blame] | 110 | int event_notifier_test_and_clear(EventNotifier *e) |
| 111 | { |
Paolo Bonzini | d0cc2fb | 2010-05-24 17:17:04 +0200 | [diff] [blame] | 112 | int value; |
| 113 | ssize_t len; |
| 114 | char buffer[512]; |
| 115 | |
| 116 | /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */ |
| 117 | value = 0; |
| 118 | do { |
| 119 | len = read(e->rfd, buffer, sizeof(buffer)); |
| 120 | value |= (len > 0); |
| 121 | } while ((len == -1 && errno == EINTR) || len == sizeof(buffer)); |
| 122 | |
| 123 | return value; |
Michael S. Tsirkin | 2292b33 | 2010-03-17 13:07:58 +0200 | [diff] [blame] | 124 | } |