]> git.proxmox.com Git - mirror_qemu.git/blame - util/event_notifier-posix.c
Merge tag 'for-upstream' of https://gitlab.com/bonzini/qemu into staging
[mirror_qemu.git] / util / event_notifier-posix.c
CommitLineData
2292b339
MT
1/*
2 * event notifier support
3 *
4 * Copyright Red Hat, Inc. 2010
5 *
6 * Authors:
7 * Michael S. Tsirkin <mst@redhat.com>
8 *
6b620ca3
PB
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.
2292b339
MT
11 */
12
aafd7584 13#include "qemu/osdep.h"
f348b6d1 14#include "qemu/cutils.h"
1de7afc9 15#include "qemu/event_notifier.h"
1de7afc9 16#include "qemu/main-loop.h"
e80c262b 17
2292b339
MT
18#ifdef CONFIG_EVENTFD
19#include <sys/eventfd.h>
20#endif
21
330b5836
MA
22#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 */
e80c262b
PB
27void event_notifier_init_fd(EventNotifier *e, int fd)
28{
d0cc2fbf
PB
29 e->rfd = fd;
30 e->wfd = fd;
e34e47eb 31 e->initialized = true;
e80c262b 32}
330b5836 33#endif
e80c262b 34
2292b339
MT
35int event_notifier_init(EventNotifier *e, int active)
36{
d0cc2fbf
PB
37 int fds[2];
38 int ret;
39
2292b339 40#ifdef CONFIG_EVENTFD
d0cc2fbf 41 ret = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
2292b339 42#else
d0cc2fbf
PB
43 ret = -1;
44 errno = ENOSYS;
2292b339 45#endif
d0cc2fbf
PB
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 }
82e27568 68 e->initialized = true;
d0cc2fbf
PB
69 if (active) {
70 event_notifier_set(e);
71 }
72 return 0;
73
74fail:
75 close(fds[0]);
76 close(fds[1]);
77 return ret;
2292b339
MT
78}
79
80void event_notifier_cleanup(EventNotifier *e)
81{
e34e47eb
ML
82 if (!e->initialized) {
83 return;
84 }
85
d0cc2fbf
PB
86 if (e->rfd != e->wfd) {
87 close(e->rfd);
88 }
e34e47eb 89
105e1023 90 e->rfd = -1;
d0cc2fbf 91 close(e->wfd);
aa262928 92 e->wfd = -1;
e34e47eb 93 e->initialized = false;
2292b339
MT
94}
95
12f0b68c 96int event_notifier_get_fd(const EventNotifier *e)
2292b339 97{
d0cc2fbf 98 return e->rfd;
2292b339
MT
99}
100
3bcf0fb3
SL
101int event_notifier_get_wfd(const EventNotifier *e)
102{
103 return e->wfd;
104}
105
2ec10b95
PB
106int event_notifier_set(EventNotifier *e)
107{
d0cc2fbf
PB
108 static const uint64_t value = 1;
109 ssize_t ret;
110
e34e47eb
ML
111 if (!e->initialized) {
112 return -1;
113 }
114
d0cc2fbf
PB
115 do {
116 ret = write(e->wfd, &value, sizeof(value));
117 } while (ret < 0 && errno == EINTR);
118
119 /* EAGAIN is fine, a read must be pending. */
120 if (ret < 0 && errno != EAGAIN) {
121 return -errno;
122 }
123 return 0;
2ec10b95
PB
124}
125
2292b339
MT
126int event_notifier_test_and_clear(EventNotifier *e)
127{
d0cc2fbf
PB
128 int value;
129 ssize_t len;
130 char buffer[512];
131
e34e47eb
ML
132 if (!e->initialized) {
133 return 0;
134 }
135
d0cc2fbf
PB
136 /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */
137 value = 0;
138 do {
139 len = read(e->rfd, buffer, sizeof(buffer));
140 value |= (len > 0);
141 } while ((len == -1 && errno == EINTR) || len == sizeof(buffer));
142
143 return value;
2292b339 144}