/src/qemu/util/event_notifier-posix.c
Line | Count | Source |
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 | 0 | { |
30 | 0 | assert(fd >= 0); |
31 | 0 | e->rfd = fd; |
32 | 0 | e->wfd = fd; |
33 | 0 | e->initialized = true; |
34 | 0 | } |
35 | | #endif |
36 | | |
37 | | int event_notifier_init(EventNotifier *e, int active) |
38 | 3.91M | { |
39 | 3.91M | int fds[2]; |
40 | 3.91M | int ret; |
41 | 3.91M | Error *local_err = NULL; |
42 | | |
43 | 3.91M | #ifdef CONFIG_EVENTFD |
44 | 3.91M | ret = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC); |
45 | | #else |
46 | | ret = -1; |
47 | | errno = ENOSYS; |
48 | | #endif |
49 | 3.91M | if (ret >= 0) { |
50 | 3.91M | e->rfd = e->wfd = ret; |
51 | 3.91M | } else { |
52 | 0 | if (errno != ENOSYS) { |
53 | 0 | return -errno; |
54 | 0 | } |
55 | 0 | if (!g_unix_open_pipe(fds, FD_CLOEXEC, NULL)) { |
56 | 0 | return -errno; |
57 | 0 | } |
58 | 0 | if (!qemu_set_blocking(fds[0], false, &local_err)) { |
59 | 0 | ret = -errno; |
60 | 0 | goto fail; |
61 | 0 | } |
62 | 0 | if (!qemu_set_blocking(fds[1], false, &local_err)) { |
63 | 0 | ret = -errno; |
64 | 0 | goto fail; |
65 | 0 | } |
66 | 0 | e->rfd = fds[0]; |
67 | 0 | e->wfd = fds[1]; |
68 | 0 | } |
69 | 3.91M | e->initialized = true; |
70 | 3.91M | if (active) { |
71 | 2.59M | event_notifier_set(e); |
72 | 2.59M | } |
73 | 3.91M | return 0; |
74 | | |
75 | 0 | fail: |
76 | 0 | error_report_err(local_err); |
77 | 0 | close(fds[0]); |
78 | 0 | close(fds[1]); |
79 | 0 | return ret; |
80 | 3.91M | } |
81 | | |
82 | | void event_notifier_cleanup(EventNotifier *e) |
83 | 3.91M | { |
84 | 3.91M | if (!e->initialized) { |
85 | 0 | return; |
86 | 0 | } |
87 | | |
88 | 3.91M | if (e->rfd != e->wfd) { |
89 | 0 | close(e->rfd); |
90 | 0 | } |
91 | | |
92 | 3.91M | e->rfd = -1; |
93 | 3.91M | close(e->wfd); |
94 | 3.91M | e->wfd = -1; |
95 | 3.91M | e->initialized = false; |
96 | 3.91M | } |
97 | | |
98 | | int event_notifier_get_fd(const EventNotifier *e) |
99 | 9.27M | { |
100 | 9.27M | assert(e->initialized); |
101 | 9.27M | assert(e->rfd >= 0); |
102 | 9.27M | return e->rfd; |
103 | 9.27M | } |
104 | | |
105 | | int event_notifier_get_wfd(const EventNotifier *e) |
106 | 0 | { |
107 | 0 | assert(e->initialized); |
108 | 0 | assert(e->wfd >= 0); |
109 | 0 | return e->wfd; |
110 | 0 | } |
111 | | |
112 | | bool event_notifier_initialized(const EventNotifier *e) |
113 | 0 | { |
114 | 0 | return e->initialized; |
115 | 0 | } |
116 | | |
117 | | int event_notifier_set(EventNotifier *e) |
118 | 6.63M | { |
119 | 6.63M | static const uint64_t value = 1; |
120 | 6.63M | ssize_t ret; |
121 | | |
122 | 6.63M | if (!e->initialized) { |
123 | 0 | return -1; |
124 | 0 | } |
125 | | |
126 | 6.63M | do { |
127 | 6.63M | ret = write(e->wfd, &value, sizeof(value)); |
128 | 6.63M | } while (ret < 0 && errno == EINTR); |
129 | | |
130 | | /* EAGAIN is fine, a read must be pending. */ |
131 | 6.63M | if (ret < 0 && errno != EAGAIN) { |
132 | 0 | return -errno; |
133 | 0 | } |
134 | 6.63M | return 0; |
135 | 6.63M | } |
136 | | |
137 | | int event_notifier_test_and_clear(EventNotifier *e) |
138 | 7.99M | { |
139 | 7.99M | int value; |
140 | 7.99M | ssize_t len; |
141 | 7.99M | char buffer[512]; |
142 | | |
143 | 7.99M | if (!e->initialized) { |
144 | 0 | return 0; |
145 | 0 | } |
146 | | |
147 | | /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */ |
148 | 7.99M | value = 0; |
149 | 7.99M | do { |
150 | 7.99M | len = read(e->rfd, buffer, sizeof(buffer)); |
151 | 7.99M | value |= (len > 0); |
152 | 7.99M | } while ((len == -1 && errno == EINTR) || len == sizeof(buffer)); |
153 | | |
154 | 7.99M | return value; |
155 | 7.99M | } |