2 * event notifier support
4 * Copyright Red Hat, Inc. 2010
7 * Michael S. Tsirkin <mst@redhat.com>
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.
13 #include "qemu-common.h"
14 #include "event_notifier.h"
15 #include "qemu-char.h"
18 #include <sys/eventfd.h>
21 void event_notifier_init_fd(EventNotifier
*e
, int fd
)
27 int event_notifier_init(EventNotifier
*e
, int active
)
33 ret
= eventfd(0, EFD_NONBLOCK
| EFD_CLOEXEC
);
39 e
->rfd
= e
->wfd
= ret
;
41 if (errno
!= ENOSYS
) {
44 if (qemu_pipe(fds
) < 0) {
47 ret
= fcntl_setfl(fds
[0], O_NONBLOCK
);
52 ret
= fcntl_setfl(fds
[1], O_NONBLOCK
);
61 event_notifier_set(e
);
71 void event_notifier_cleanup(EventNotifier
*e
)
73 if (e
->rfd
!= e
->wfd
) {
79 int event_notifier_get_fd(EventNotifier
*e
)
84 int event_notifier_set_handler(EventNotifier
*e
,
85 EventNotifierHandler
*handler
)
87 return qemu_set_fd_handler(e
->rfd
, (IOHandler
*)handler
, NULL
, e
);
90 int event_notifier_set(EventNotifier
*e
)
92 static const uint64_t value
= 1;
96 ret
= write(e
->wfd
, &value
, sizeof(value
));
97 } while (ret
< 0 && errno
== EINTR
);
99 /* EAGAIN is fine, a read must be pending. */
100 if (ret
< 0 && errno
!= EAGAIN
) {
106 int event_notifier_test_and_clear(EventNotifier
*e
)
112 /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */
115 len
= read(e
->rfd
, buffer
, sizeof(buffer
));
117 } while ((len
== -1 && errno
== EINTR
) || len
== sizeof(buffer
));