blob: 00d93204f988c753d1ba308421936c457277ff25 [file] [log] [blame]
Michael S. Tsirkin2292b332010-03-17 13:07:58 +02001/*
2 * event notifier support
3 *
4 * Copyright Red Hat, Inc. 2010
5 *
6 * Authors:
7 * Michael S. Tsirkin <mst@redhat.com>
8 *
Paolo Bonzini6b620ca2012-01-13 17:44:23 +01009 * 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.
Michael S. Tsirkin2292b332010-03-17 13:07:58 +020011 */
12
Peter Maydellaafd7582016-01-29 17:49:55 +000013#include "qemu/osdep.h"
Paolo Bonzinie80c2622012-07-05 17:16:24 +020014#include "qemu-common.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020015#include "qemu/cutils.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010016#include "qemu/event_notifier.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010017#include "qemu/main-loop.h"
Paolo Bonzinie80c2622012-07-05 17:16:24 +020018
Michael S. Tsirkin2292b332010-03-17 13:07:58 +020019#ifdef CONFIG_EVENTFD
20#include <sys/eventfd.h>
21#endif
22
Markus Armbruster330b5832016-03-15 19:34:20 +010023#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 */
Paolo Bonzinie80c2622012-07-05 17:16:24 +020028void event_notifier_init_fd(EventNotifier *e, int fd)
29{
Paolo Bonzinid0cc2fb2010-05-24 17:17:04 +020030 e->rfd = fd;
31 e->wfd = fd;
Paolo Bonzinie80c2622012-07-05 17:16:24 +020032}
Markus Armbruster330b5832016-03-15 19:34:20 +010033#endif
Paolo Bonzinie80c2622012-07-05 17:16:24 +020034
Michael S. Tsirkin2292b332010-03-17 13:07:58 +020035int event_notifier_init(EventNotifier *e, int active)
36{
Paolo Bonzinid0cc2fb2010-05-24 17:17:04 +020037 int fds[2];
38 int ret;
39
Michael S. Tsirkin2292b332010-03-17 13:07:58 +020040#ifdef CONFIG_EVENTFD
Paolo Bonzinid0cc2fb2010-05-24 17:17:04 +020041 ret = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
Michael S. Tsirkin2292b332010-03-17 13:07:58 +020042#else
Paolo Bonzinid0cc2fb2010-05-24 17:17:04 +020043 ret = -1;
44 errno = ENOSYS;
Michael S. Tsirkin2292b332010-03-17 13:07:58 +020045#endif
Paolo Bonzinid0cc2fb2010-05-24 17:17:04 +020046 if (ret >= 0) {
47 e->rfd = e->wfd = ret;
48 } else {
49 if (errno != ENOSYS) {
50 return -errno;
51 }
52 if (qemu_pipe(fds) < 0) {
53 return -errno;
54 }
55 ret = fcntl_setfl(fds[0], O_NONBLOCK);
56 if (ret < 0) {
57 ret = -errno;
58 goto fail;
59 }
60 ret = fcntl_setfl(fds[1], O_NONBLOCK);
61 if (ret < 0) {
62 ret = -errno;
63 goto fail;
64 }
65 e->rfd = fds[0];
66 e->wfd = fds[1];
67 }
68 if (active) {
69 event_notifier_set(e);
70 }
71 return 0;
72
73fail:
74 close(fds[0]);
75 close(fds[1]);
76 return ret;
Michael S. Tsirkin2292b332010-03-17 13:07:58 +020077}
78
79void event_notifier_cleanup(EventNotifier *e)
80{
Paolo Bonzinid0cc2fb2010-05-24 17:17:04 +020081 if (e->rfd != e->wfd) {
82 close(e->rfd);
83 }
Frediano Ziglio105e1022019-10-23 13:26:51 +010084 e->rfd = -1;
Paolo Bonzinid0cc2fb2010-05-24 17:17:04 +020085 close(e->wfd);
Halil Pasicaa262922017-03-02 19:13:08 +010086 e->wfd = -1;
Michael S. Tsirkin2292b332010-03-17 13:07:58 +020087}
88
Marc-André Lureau12f0b682015-10-13 12:12:16 +020089int event_notifier_get_fd(const EventNotifier *e)
Michael S. Tsirkin2292b332010-03-17 13:07:58 +020090{
Paolo Bonzinid0cc2fb2010-05-24 17:17:04 +020091 return e->rfd;
Michael S. Tsirkin2292b332010-03-17 13:07:58 +020092}
93
Paolo Bonzini2ec10b92012-07-05 17:16:22 +020094int event_notifier_set(EventNotifier *e)
95{
Paolo Bonzinid0cc2fb2010-05-24 17:17:04 +020096 static const uint64_t value = 1;
97 ssize_t ret;
98
99 do {
100 ret = write(e->wfd, &value, sizeof(value));
101 } while (ret < 0 && errno == EINTR);
102
103 /* EAGAIN is fine, a read must be pending. */
104 if (ret < 0 && errno != EAGAIN) {
105 return -errno;
106 }
107 return 0;
Paolo Bonzini2ec10b92012-07-05 17:16:22 +0200108}
109
Michael S. Tsirkin2292b332010-03-17 13:07:58 +0200110int event_notifier_test_and_clear(EventNotifier *e)
111{
Paolo Bonzinid0cc2fb2010-05-24 17:17:04 +0200112 int value;
113 ssize_t len;
114 char buffer[512];
115
116 /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */
117 value = 0;
118 do {
119 len = read(e->rfd, buffer, sizeof(buffer));
120 value |= (len > 0);
121 } while ((len == -1 && errno == EINTR) || len == sizeof(buffer));
122
123 return value;
Michael S. Tsirkin2292b332010-03-17 13:07:58 +0200124}