Merge pull request #31 from myfreeweb/shmem

Fix keyboard shared memory, use memfd/SHM_ANON
pull/32/head
Andri Yngvason 2020-04-05 23:37:01 +00:00 committed by GitHub
commit b39655df15
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 1 deletions

View File

@ -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 <sys/syscall.h>')
config.set('HAVE_MEMFD', true)
endif
configure_file(
output: 'config.h',
configuration: config,

View File

@ -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;

View File

@ -8,6 +8,18 @@
#include <time.h>
#include <unistd.h>
#include "config.h"
// Linux with glibc < 2.27 has no wrapper
#if defined(HAVE_MEMFD) && !defined(HAVE_MEMFD_CREATE)
#include <sys/syscall.h>
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)