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