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