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 | |
Paolo Bonzini | e80c262 | 2012-07-05 17:16:24 +0200 | [diff] [blame] | 13 | #include "qemu-common.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 14 | #include "qemu/event_notifier.h" |
Paolo Bonzini | dccfcd0 | 2013-04-08 16:55:25 +0200 | [diff] [blame] | 15 | #include "sysemu/char.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 16 | #include "qemu/main-loop.h" |
Paolo Bonzini | e80c262 | 2012-07-05 17:16:24 +0200 | [diff] [blame] | 17 | |
Michael S. Tsirkin | 2292b33 | 2010-03-17 13:07:58 +0200 | [diff] [blame] | 18 | #ifdef CONFIG_EVENTFD |
| 19 | #include <sys/eventfd.h> |
| 20 | #endif |
| 21 | |
Paolo Bonzini | e80c262 | 2012-07-05 17:16:24 +0200 | [diff] [blame] | 22 | void event_notifier_init_fd(EventNotifier *e, int fd) |
| 23 | { |
Paolo Bonzini | d0cc2fb | 2010-05-24 17:17:04 +0200 | [diff] [blame] | 24 | e->rfd = fd; |
| 25 | e->wfd = fd; |
Paolo Bonzini | e80c262 | 2012-07-05 17:16:24 +0200 | [diff] [blame] | 26 | } |
| 27 | |
Michael S. Tsirkin | 2292b33 | 2010-03-17 13:07:58 +0200 | [diff] [blame] | 28 | int event_notifier_init(EventNotifier *e, int active) |
| 29 | { |
Paolo Bonzini | d0cc2fb | 2010-05-24 17:17:04 +0200 | [diff] [blame] | 30 | int fds[2]; |
| 31 | int ret; |
| 32 | |
Michael S. Tsirkin | 2292b33 | 2010-03-17 13:07:58 +0200 | [diff] [blame] | 33 | #ifdef CONFIG_EVENTFD |
Paolo Bonzini | d0cc2fb | 2010-05-24 17:17:04 +0200 | [diff] [blame] | 34 | ret = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC); |
Michael S. Tsirkin | 2292b33 | 2010-03-17 13:07:58 +0200 | [diff] [blame] | 35 | #else |
Paolo Bonzini | d0cc2fb | 2010-05-24 17:17:04 +0200 | [diff] [blame] | 36 | ret = -1; |
| 37 | errno = ENOSYS; |
Michael S. Tsirkin | 2292b33 | 2010-03-17 13:07:58 +0200 | [diff] [blame] | 38 | #endif |
Paolo Bonzini | d0cc2fb | 2010-05-24 17:17:04 +0200 | [diff] [blame] | 39 | if (ret >= 0) { |
| 40 | e->rfd = e->wfd = ret; |
| 41 | } else { |
| 42 | if (errno != ENOSYS) { |
| 43 | return -errno; |
| 44 | } |
| 45 | if (qemu_pipe(fds) < 0) { |
| 46 | return -errno; |
| 47 | } |
| 48 | ret = fcntl_setfl(fds[0], O_NONBLOCK); |
| 49 | if (ret < 0) { |
| 50 | ret = -errno; |
| 51 | goto fail; |
| 52 | } |
| 53 | ret = fcntl_setfl(fds[1], O_NONBLOCK); |
| 54 | if (ret < 0) { |
| 55 | ret = -errno; |
| 56 | goto fail; |
| 57 | } |
| 58 | e->rfd = fds[0]; |
| 59 | e->wfd = fds[1]; |
| 60 | } |
| 61 | if (active) { |
| 62 | event_notifier_set(e); |
| 63 | } |
| 64 | return 0; |
| 65 | |
| 66 | fail: |
| 67 | close(fds[0]); |
| 68 | close(fds[1]); |
| 69 | return ret; |
Michael S. Tsirkin | 2292b33 | 2010-03-17 13:07:58 +0200 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | void event_notifier_cleanup(EventNotifier *e) |
| 73 | { |
Paolo Bonzini | d0cc2fb | 2010-05-24 17:17:04 +0200 | [diff] [blame] | 74 | if (e->rfd != e->wfd) { |
| 75 | close(e->rfd); |
| 76 | } |
| 77 | close(e->wfd); |
Michael S. Tsirkin | 2292b33 | 2010-03-17 13:07:58 +0200 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | int event_notifier_get_fd(EventNotifier *e) |
| 81 | { |
Paolo Bonzini | d0cc2fb | 2010-05-24 17:17:04 +0200 | [diff] [blame] | 82 | return e->rfd; |
Michael S. Tsirkin | 2292b33 | 2010-03-17 13:07:58 +0200 | [diff] [blame] | 83 | } |
| 84 | |
Paolo Bonzini | 6bf819f | 2012-07-05 17:16:28 +0200 | [diff] [blame] | 85 | int event_notifier_set_handler(EventNotifier *e, |
| 86 | EventNotifierHandler *handler) |
| 87 | { |
Paolo Bonzini | d0cc2fb | 2010-05-24 17:17:04 +0200 | [diff] [blame] | 88 | return qemu_set_fd_handler(e->rfd, (IOHandler *)handler, NULL, e); |
Paolo Bonzini | 6bf819f | 2012-07-05 17:16:28 +0200 | [diff] [blame] | 89 | } |
| 90 | |
Paolo Bonzini | 2ec10b9 | 2012-07-05 17:16:22 +0200 | [diff] [blame] | 91 | int event_notifier_set(EventNotifier *e) |
| 92 | { |
Paolo Bonzini | d0cc2fb | 2010-05-24 17:17:04 +0200 | [diff] [blame] | 93 | static const uint64_t value = 1; |
| 94 | ssize_t ret; |
| 95 | |
| 96 | do { |
| 97 | ret = write(e->wfd, &value, sizeof(value)); |
| 98 | } while (ret < 0 && errno == EINTR); |
| 99 | |
| 100 | /* EAGAIN is fine, a read must be pending. */ |
| 101 | if (ret < 0 && errno != EAGAIN) { |
| 102 | return -errno; |
| 103 | } |
| 104 | return 0; |
Paolo Bonzini | 2ec10b9 | 2012-07-05 17:16:22 +0200 | [diff] [blame] | 105 | } |
| 106 | |
Michael S. Tsirkin | 2292b33 | 2010-03-17 13:07:58 +0200 | [diff] [blame] | 107 | int event_notifier_test_and_clear(EventNotifier *e) |
| 108 | { |
Paolo Bonzini | d0cc2fb | 2010-05-24 17:17:04 +0200 | [diff] [blame] | 109 | int value; |
| 110 | ssize_t len; |
| 111 | char buffer[512]; |
| 112 | |
| 113 | /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */ |
| 114 | value = 0; |
| 115 | do { |
| 116 | len = read(e->rfd, buffer, sizeof(buffer)); |
| 117 | value |= (len > 0); |
| 118 | } while ((len == -1 && errno == EINTR) || len == sizeof(buffer)); |
| 119 | |
| 120 | return value; |
Michael S. Tsirkin | 2292b33 | 2010-03-17 13:07:58 +0200 | [diff] [blame] | 121 | } |