xref: /openbmc/qemu/util/event_notifier-posix.c (revision fdee2025dd690b35099dd4d04eeef27c2bc1bc9c)
1 /*
2  * event notifier support
3  *
4  * Copyright Red Hat, Inc. 2010
5  *
6  * Authors:
7  *  Michael S. Tsirkin <mst@redhat.com>
8  *
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.
11  */
12 
13 #include "qemu/osdep.h"
14 #include "qemu-common.h"
15 #include "qemu/event_notifier.h"
16 #include "sysemu/char.h"
17 #include "qemu/main-loop.h"
18 
19 #ifdef CONFIG_EVENTFD
20 #include <sys/eventfd.h>
21 #endif
22 
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  */
28 void event_notifier_init_fd(EventNotifier *e, int fd)
29 {
30     e->rfd = fd;
31     e->wfd = fd;
32 }
33 #endif
34 
35 int event_notifier_init(EventNotifier *e, int active)
36 {
37     int fds[2];
38     int ret;
39 
40 #ifdef CONFIG_EVENTFD
41     ret = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
42 #else
43     ret = -1;
44     errno = ENOSYS;
45 #endif
46     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 
73 fail:
74     close(fds[0]);
75     close(fds[1]);
76     return ret;
77 }
78 
79 void event_notifier_cleanup(EventNotifier *e)
80 {
81     if (e->rfd != e->wfd) {
82         close(e->rfd);
83     }
84     close(e->wfd);
85 }
86 
87 int event_notifier_get_fd(const EventNotifier *e)
88 {
89     return e->rfd;
90 }
91 
92 int event_notifier_set_handler(EventNotifier *e,
93                                EventNotifierHandler *handler)
94 {
95     qemu_set_fd_handler(e->rfd, (IOHandler *)handler, NULL, e);
96     return 0;
97 }
98 
99 int event_notifier_set(EventNotifier *e)
100 {
101     static const uint64_t value = 1;
102     ssize_t ret;
103 
104     do {
105         ret = write(e->wfd, &value, sizeof(value));
106     } while (ret < 0 && errno == EINTR);
107 
108     /* EAGAIN is fine, a read must be pending.  */
109     if (ret < 0 && errno != EAGAIN) {
110         return -errno;
111     }
112     return 0;
113 }
114 
115 int event_notifier_test_and_clear(EventNotifier *e)
116 {
117     int value;
118     ssize_t len;
119     char buffer[512];
120 
121     /* Drain the notify pipe.  For eventfd, only 8 bytes will be read.  */
122     value = 0;
123     do {
124         len = read(e->rfd, buffer, sizeof(buffer));
125         value |= (len > 0);
126     } while ((len == -1 && errno == EINTR) || len == sizeof(buffer));
127 
128     return value;
129 }
130