blob: 76420c5b560c15bb7a6a0c7960a2072ebe174bc1 [file] [log] [blame]
Michael S. Tsirkin2292b332010-03-17 13:07:58 +02001/*
2 * event notifier support
3 *
4 * Copyright Red Hat, Inc. 2010
5 *
6 * Authors:
7 * Michael S. Tsirkin <mst@redhat.com>
8 *
Paolo Bonzini6b620ca2012-01-13 17:44:23 +01009 * 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. Tsirkin2292b332010-03-17 13:07:58 +020011 */
12
Peter Maydellaafd7582016-01-29 17:49:55 +000013#include "qemu/osdep.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020014#include "qemu/cutils.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010015#include "qemu/event_notifier.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010016#include "qemu/main-loop.h"
Paolo Bonzinie80c2622012-07-05 17:16:24 +020017
Michael S. Tsirkin2292b332010-03-17 13:07:58 +020018#ifdef CONFIG_EVENTFD
19#include <sys/eventfd.h>
20#endif
21
Markus Armbruster330b5832016-03-15 19:34:20 +010022#ifdef CONFIG_EVENTFD
23/*
24 * Initialize @e with existing file descriptor @fd.
25 * @fd must be a genuine eventfd object, emulation with pipe won't do.
26 */
Paolo Bonzinie80c2622012-07-05 17:16:24 +020027void event_notifier_init_fd(EventNotifier *e, int fd)
28{
Paolo Bonzinid0cc2fb2010-05-24 17:17:04 +020029 e->rfd = fd;
30 e->wfd = fd;
Maxim Levitskye34e47e2020-12-17 17:00:40 +020031 e->initialized = true;
Paolo Bonzinie80c2622012-07-05 17:16:24 +020032}
Markus Armbruster330b5832016-03-15 19:34:20 +010033#endif
Paolo Bonzinie80c2622012-07-05 17:16:24 +020034
Michael S. Tsirkin2292b332010-03-17 13:07:58 +020035int event_notifier_init(EventNotifier *e, int active)
36{
Paolo Bonzinid0cc2fb2010-05-24 17:17:04 +020037 int fds[2];
38 int ret;
39
Michael S. Tsirkin2292b332010-03-17 13:07:58 +020040#ifdef CONFIG_EVENTFD
Paolo Bonzinid0cc2fb2010-05-24 17:17:04 +020041 ret = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
Michael S. Tsirkin2292b332010-03-17 13:07:58 +020042#else
Paolo Bonzinid0cc2fb2010-05-24 17:17:04 +020043 ret = -1;
44 errno = ENOSYS;
Michael S. Tsirkin2292b332010-03-17 13:07:58 +020045#endif
Paolo Bonzinid0cc2fb2010-05-24 17:17:04 +020046 if (ret >= 0) {
47 e->rfd = e->wfd = ret;
48 } else {
49 if (errno != ENOSYS) {
50 return -errno;
51 }
Marc-André Lureaua7241972022-03-29 15:21:00 +040052 if (!g_unix_open_pipe(fds, FD_CLOEXEC, NULL)) {
Paolo Bonzinid0cc2fb2010-05-24 17:17:04 +020053 return -errno;
54 }
Marc-André Lureau4d14cb02022-03-29 15:25:05 +040055 if (!g_unix_set_fd_nonblocking(fds[0], true, NULL)) {
Paolo Bonzinid0cc2fb2010-05-24 17:17:04 +020056 ret = -errno;
57 goto fail;
58 }
Marc-André Lureau4d14cb02022-03-29 15:25:05 +040059 if (!g_unix_set_fd_nonblocking(fds[1], true, NULL)) {
Paolo Bonzinid0cc2fb2010-05-24 17:17:04 +020060 ret = -errno;
61 goto fail;
62 }
63 e->rfd = fds[0];
64 e->wfd = fds[1];
65 }
Greg Kurz82e27562021-02-16 13:02:47 +010066 e->initialized = true;
Paolo Bonzinid0cc2fb2010-05-24 17:17:04 +020067 if (active) {
68 event_notifier_set(e);
69 }
70 return 0;
71
72fail:
73 close(fds[0]);
74 close(fds[1]);
75 return ret;
Michael S. Tsirkin2292b332010-03-17 13:07:58 +020076}
77
78void event_notifier_cleanup(EventNotifier *e)
79{
Maxim Levitskye34e47e2020-12-17 17:00:40 +020080 if (!e->initialized) {
81 return;
82 }
83
Paolo Bonzinid0cc2fb2010-05-24 17:17:04 +020084 if (e->rfd != e->wfd) {
85 close(e->rfd);
86 }
Maxim Levitskye34e47e2020-12-17 17:00:40 +020087
Frediano Ziglio105e1022019-10-23 13:26:51 +010088 e->rfd = -1;
Paolo Bonzinid0cc2fb2010-05-24 17:17:04 +020089 close(e->wfd);
Halil Pasicaa262922017-03-02 19:13:08 +010090 e->wfd = -1;
Maxim Levitskye34e47e2020-12-17 17:00:40 +020091 e->initialized = false;
Michael S. Tsirkin2292b332010-03-17 13:07:58 +020092}
93
Marc-André Lureau12f0b682015-10-13 12:12:16 +020094int event_notifier_get_fd(const EventNotifier *e)
Michael S. Tsirkin2292b332010-03-17 13:07:58 +020095{
Paolo Bonzinid0cc2fb2010-05-24 17:17:04 +020096 return e->rfd;
Michael S. Tsirkin2292b332010-03-17 13:07:58 +020097}
98
Sergio Lopez3bcf0fb2022-03-04 11:08:51 +010099int event_notifier_get_wfd(const EventNotifier *e)
100{
101 return e->wfd;
102}
103
Paolo Bonzini2ec10b92012-07-05 17:16:22 +0200104int event_notifier_set(EventNotifier *e)
105{
Paolo Bonzinid0cc2fb2010-05-24 17:17:04 +0200106 static const uint64_t value = 1;
107 ssize_t ret;
108
Maxim Levitskye34e47e2020-12-17 17:00:40 +0200109 if (!e->initialized) {
110 return -1;
111 }
112
Paolo Bonzinid0cc2fb2010-05-24 17:17:04 +0200113 do {
114 ret = write(e->wfd, &value, sizeof(value));
115 } while (ret < 0 && errno == EINTR);
116
117 /* EAGAIN is fine, a read must be pending. */
118 if (ret < 0 && errno != EAGAIN) {
119 return -errno;
120 }
121 return 0;
Paolo Bonzini2ec10b92012-07-05 17:16:22 +0200122}
123
Michael S. Tsirkin2292b332010-03-17 13:07:58 +0200124int event_notifier_test_and_clear(EventNotifier *e)
125{
Paolo Bonzinid0cc2fb2010-05-24 17:17:04 +0200126 int value;
127 ssize_t len;
128 char buffer[512];
129
Maxim Levitskye34e47e2020-12-17 17:00:40 +0200130 if (!e->initialized) {
131 return 0;
132 }
133
Paolo Bonzinid0cc2fb2010-05-24 17:17:04 +0200134 /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */
135 value = 0;
136 do {
137 len = read(e->rfd, buffer, sizeof(buffer));
138 value |= (len > 0);
139 } while ((len == -1 && errno == EINTR) || len == sizeof(buffer));
140
141 return value;
Michael S. Tsirkin2292b332010-03-17 13:07:58 +0200142}