blob: 8442c6e63c517d1e0a36201cde009e5bed142a48 [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
Paolo Bonzinie80c2622012-07-05 17:16:24 +020013#include "qemu-common.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010014#include "qemu/event_notifier.h"
Paolo Bonzinidccfcd02013-04-08 16:55:25 +020015#include "sysemu/char.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010016#include "qemu/main-loop.h"
Paolo Bonzinie80c2622012-07-05 17:16:24 +020017
Michael S. Tsirkin2292b332010-03-17 13:07:58 +020018#ifdef CONFIG_EVENTFD
19#include <sys/eventfd.h>
20#endif
21
Paolo Bonzinie80c2622012-07-05 17:16:24 +020022void event_notifier_init_fd(EventNotifier *e, int fd)
23{
Paolo Bonzinid0cc2fb2010-05-24 17:17:04 +020024 e->rfd = fd;
25 e->wfd = fd;
Paolo Bonzinie80c2622012-07-05 17:16:24 +020026}
27
Michael S. Tsirkin2292b332010-03-17 13:07:58 +020028int event_notifier_init(EventNotifier *e, int active)
29{
Paolo Bonzinid0cc2fb2010-05-24 17:17:04 +020030 int fds[2];
31 int ret;
32
Michael S. Tsirkin2292b332010-03-17 13:07:58 +020033#ifdef CONFIG_EVENTFD
Paolo Bonzinid0cc2fb2010-05-24 17:17:04 +020034 ret = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
Michael S. Tsirkin2292b332010-03-17 13:07:58 +020035#else
Paolo Bonzinid0cc2fb2010-05-24 17:17:04 +020036 ret = -1;
37 errno = ENOSYS;
Michael S. Tsirkin2292b332010-03-17 13:07:58 +020038#endif
Paolo Bonzinid0cc2fb2010-05-24 17:17:04 +020039 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
66fail:
67 close(fds[0]);
68 close(fds[1]);
69 return ret;
Michael S. Tsirkin2292b332010-03-17 13:07:58 +020070}
71
72void event_notifier_cleanup(EventNotifier *e)
73{
Paolo Bonzinid0cc2fb2010-05-24 17:17:04 +020074 if (e->rfd != e->wfd) {
75 close(e->rfd);
76 }
77 close(e->wfd);
Michael S. Tsirkin2292b332010-03-17 13:07:58 +020078}
79
80int event_notifier_get_fd(EventNotifier *e)
81{
Paolo Bonzinid0cc2fb2010-05-24 17:17:04 +020082 return e->rfd;
Michael S. Tsirkin2292b332010-03-17 13:07:58 +020083}
84
Paolo Bonzini6bf819f2012-07-05 17:16:28 +020085int event_notifier_set_handler(EventNotifier *e,
86 EventNotifierHandler *handler)
87{
Paolo Bonzinid0cc2fb2010-05-24 17:17:04 +020088 return qemu_set_fd_handler(e->rfd, (IOHandler *)handler, NULL, e);
Paolo Bonzini6bf819f2012-07-05 17:16:28 +020089}
90
Paolo Bonzini2ec10b92012-07-05 17:16:22 +020091int event_notifier_set(EventNotifier *e)
92{
Paolo Bonzinid0cc2fb2010-05-24 17:17:04 +020093 static const uint64_t value = 1;
94 ssize_t ret;
95
96 do {
97 ret = write(e->wfd, &value, sizeof(value));
98 } while (ret < 0 && errno == EINTR);
99
100 /* EAGAIN is fine, a read must be pending. */
101 if (ret < 0 && errno != EAGAIN) {
102 return -errno;
103 }
104 return 0;
Paolo Bonzini2ec10b92012-07-05 17:16:22 +0200105}
106
Michael S. Tsirkin2292b332010-03-17 13:07:58 +0200107int event_notifier_test_and_clear(EventNotifier *e)
108{
Paolo Bonzinid0cc2fb2010-05-24 17:17:04 +0200109 int value;
110 ssize_t len;
111 char buffer[512];
112
113 /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */
114 value = 0;
115 do {
116 len = read(e->rfd, buffer, sizeof(buffer));
117 value |= (len > 0);
118 } while ((len == -1 && errno == EINTR) || len == sizeof(buffer));
119
120 return value;
Michael S. Tsirkin2292b332010-03-17 13:07:58 +0200121}