]>
Commit | Line | Data |
---|---|---|
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" |
e80c262b | 14 | #include "qemu-common.h" |
f348b6d1 | 15 | #include "qemu/cutils.h" |
1de7afc9 | 16 | #include "qemu/event_notifier.h" |
1de7afc9 | 17 | #include "qemu/main-loop.h" |
e80c262b | 18 | |
2292b339 MT |
19 | #ifdef CONFIG_EVENTFD |
20 | #include <sys/eventfd.h> | |
21 | #endif | |
22 | ||
330b5836 MA |
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 | */ | |
e80c262b PB |
28 | void event_notifier_init_fd(EventNotifier *e, int fd) |
29 | { | |
d0cc2fbf PB |
30 | e->rfd = fd; |
31 | e->wfd = fd; | |
f87d72f5 | 32 | e->cleanup = NULL; |
e80c262b | 33 | } |
330b5836 | 34 | #endif |
e80c262b | 35 | |
2292b339 MT |
36 | int event_notifier_init(EventNotifier *e, int active) |
37 | { | |
d0cc2fbf PB |
38 | int fds[2]; |
39 | int ret; | |
40 | ||
2292b339 | 41 | #ifdef CONFIG_EVENTFD |
d0cc2fbf | 42 | ret = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC); |
2292b339 | 43 | #else |
d0cc2fbf PB |
44 | ret = -1; |
45 | errno = ENOSYS; | |
2292b339 | 46 | #endif |
d0cc2fbf PB |
47 | if (ret >= 0) { |
48 | e->rfd = e->wfd = ret; | |
49 | } else { | |
50 | if (errno != ENOSYS) { | |
51 | return -errno; | |
52 | } | |
53 | if (qemu_pipe(fds) < 0) { | |
54 | return -errno; | |
55 | } | |
56 | ret = fcntl_setfl(fds[0], O_NONBLOCK); | |
57 | if (ret < 0) { | |
58 | ret = -errno; | |
59 | goto fail; | |
60 | } | |
61 | ret = fcntl_setfl(fds[1], O_NONBLOCK); | |
62 | if (ret < 0) { | |
63 | ret = -errno; | |
64 | goto fail; | |
65 | } | |
66 | e->rfd = fds[0]; | |
67 | e->wfd = fds[1]; | |
68 | } | |
f87d72f5 | 69 | e->cleanup = NULL; |
d0cc2fbf PB |
70 | if (active) { |
71 | event_notifier_set(e); | |
72 | } | |
73 | return 0; | |
74 | ||
75 | fail: | |
76 | close(fds[0]); | |
77 | close(fds[1]); | |
78 | return ret; | |
2292b339 MT |
79 | } |
80 | ||
81 | void event_notifier_cleanup(EventNotifier *e) | |
82 | { | |
d0cc2fbf PB |
83 | if (e->rfd != e->wfd) { |
84 | close(e->rfd); | |
85 | } | |
86 | close(e->wfd); | |
f87d72f5 | 87 | e->rfd = -1; |
aa262928 | 88 | e->wfd = -1; |
f87d72f5 | 89 | e->cleanup = NULL; |
2292b339 MT |
90 | } |
91 | ||
12f0b68c | 92 | int event_notifier_get_fd(const EventNotifier *e) |
2292b339 | 93 | { |
d0cc2fbf | 94 | return e->rfd; |
2292b339 MT |
95 | } |
96 | ||
2ec10b95 PB |
97 | int event_notifier_set(EventNotifier *e) |
98 | { | |
d0cc2fbf PB |
99 | static const uint64_t value = 1; |
100 | ssize_t ret; | |
101 | ||
102 | do { | |
103 | ret = write(e->wfd, &value, sizeof(value)); | |
104 | } while (ret < 0 && errno == EINTR); | |
105 | ||
106 | /* EAGAIN is fine, a read must be pending. */ | |
107 | if (ret < 0 && errno != EAGAIN) { | |
108 | return -errno; | |
109 | } | |
110 | return 0; | |
2ec10b95 PB |
111 | } |
112 | ||
2292b339 MT |
113 | int event_notifier_test_and_clear(EventNotifier *e) |
114 | { | |
d0cc2fbf PB |
115 | int value; |
116 | ssize_t len; | |
117 | char buffer[512]; | |
118 | ||
119 | /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */ | |
120 | value = 0; | |
121 | do { | |
122 | len = read(e->rfd, buffer, sizeof(buffer)); | |
123 | value |= (len > 0); | |
124 | } while ((len == -1 && errno == EINTR) || len == sizeof(buffer)); | |
125 | ||
126 | return value; | |
2292b339 | 127 | } |