libvhost-user includes <sys/eventfd.h> unconditionally and uses
eventfd_read() and eventfd_write(). The header is not available on every
POSIX system, e.g. macOS, so the build fails there:

  libvhost-user.c:31:10: fatal error: 'sys/eventfd.h' file not found

Probe for eventfd() in the subproject's meson.build, using the same test
QEMU's top-level meson.build uses for CONFIG_EVENTFD, and only include
the header when CONFIG_EVENTFD is set.

Without eventfd the front-end falls back to pipes, so provide
eventfd_read() and eventfd_write() replacements with plain read() and
write().

Signed-off-by: Tim Pambor <[email protected]>
---
 subprojects/libvhost-user/libvhost-user.c | 17 ++++++++++++++++-
 subprojects/libvhost-user/meson.build     |  6 ++++++
 2 files changed, 22 insertions(+), 1 deletion(-)

diff --git a/subprojects/libvhost-user/libvhost-user.c 
b/subprojects/libvhost-user/libvhost-user.c
index adda700630..ece2bfcdd7 100644
--- a/subprojects/libvhost-user/libvhost-user.c
+++ b/subprojects/libvhost-user/libvhost-user.c
@@ -28,7 +28,6 @@
 #include <inttypes.h>
 #include <sys/types.h>
 #include <sys/socket.h>
-#include <sys/eventfd.h>
 #include <sys/mman.h>
 #include <endian.h>
 
@@ -52,6 +51,22 @@
 
 #endif
 
+#ifdef CONFIG_EVENTFD
+#include <sys/eventfd.h>
+#else
+#define eventfd_t uint64_t
+
+int eventfd_write(int fd, eventfd_t value)
+{
+    return (write(fd, &value, sizeof(value)) == sizeof(value)) ? 0 : -1;
+}
+
+int eventfd_read(int fd, eventfd_t *value)
+{
+    return (read(fd, value, sizeof(*value)) == sizeof(*value)) ? 0 : -1;
+}
+#endif
+
 #include "include/atomic.h"
 
 #include "libvhost-user.h"
diff --git a/subprojects/libvhost-user/meson.build 
b/subprojects/libvhost-user/meson.build
index aa5894a642..2f9aedb208 100644
--- a/subprojects/libvhost-user/meson.build
+++ b/subprojects/libvhost-user/meson.build
@@ -20,6 +20,12 @@ if cc.links('''
   vhost_user_flags += '-DCONFIG_MEMFD'
 endif
 
+if cc.links('''
+    #include <sys/eventfd.h>
+    int main(void) { return eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC); }''')
+  vhost_user_flags += '-DCONFIG_EVENTFD'
+endif
+
 vhost_user = static_library('vhost-user',
                             files('libvhost-user.c'),
                             dependencies: threads,

-- 
2.55.0


Reply via email to