diff --git a/meson.build b/meson.build index 2df1af3..3e9da90 100644 --- a/meson.build +++ b/meson.build @@ -93,6 +93,13 @@ if host_system == 'linux' and cc.has_header('sys/sdt.h') config.set('HAVE_USDT', true) endif +if cc.has_function('memfd_create') + config.set('HAVE_MEMFD', true) + config.set('HAVE_MEMFD_CREATE', true) +elif cc.has_function('SYS_memfd_create', prefix : '#include ') + config.set('HAVE_MEMFD', true) +endif + configure_file( output: 'config.h', configuration: config, diff --git a/src/keyboard.c b/src/keyboard.c index 5c2b0c1..d0ddb35 100644 --- a/src/keyboard.c +++ b/src/keyboard.c @@ -171,7 +171,7 @@ int keyboard_init(struct keyboard* self, const char* layout) size_t keymap_len = strlen(keymap_string); - int keymap_fd = shm_alloc_fd(0); + int keymap_fd = shm_alloc_fd(keymap_len); if (keymap_fd < 0) goto fd_failure; diff --git a/src/shm.c b/src/shm.c index af1e06a..610b857 100644 --- a/src/shm.c +++ b/src/shm.c @@ -8,6 +8,18 @@ #include #include +#include "config.h" + +// Linux with glibc < 2.27 has no wrapper +#if defined(HAVE_MEMFD) && !defined(HAVE_MEMFD_CREATE) +#include + +static inline int memfd_create(const char *name, unsigned int flags) { + return syscall(SYS_memfd_create, name, flags); +} +#endif + +#ifndef HAVE_MEMFD static void randname(char *buf) { struct timespec ts; @@ -19,9 +31,16 @@ static void randname(char *buf) r >>= 5; } } +#endif static int create_shm_file(void) { +#ifdef HAVE_MEMFD + return memfd_create("wayvnc-shm", 0); +#elif defined(__FreeBSD__) + // memfd_create added in FreeBSD 13, but SHM_ANON has been supported for ages + return shm_open(SHM_ANON, O_RDWR | O_CREAT | O_EXCL, 0600); +#else int retries = 100; do { @@ -37,6 +56,7 @@ static int create_shm_file(void) } while (retries > 0 && errno == EEXIST); return -1; +#endif } int shm_alloc_fd(size_t size)