shm: support memfd and SHM_ANON

pull/31/head
Greg V 2020-04-06 00:43:24 +03:00
parent 720b127dee
commit d610076614
2 changed files with 27 additions and 0 deletions

View File

@ -93,6 +93,13 @@ if host_system == 'linux' and cc.has_header('sys/sdt.h')
config.set('HAVE_USDT', true) config.set('HAVE_USDT', true)
endif 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( configure_file(
output: 'config.h', output: 'config.h',
configuration: config, configuration: config,

View File

@ -8,6 +8,18 @@
#include <time.h> #include <time.h>
#include <unistd.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) static void randname(char *buf)
{ {
struct timespec ts; struct timespec ts;
@ -19,9 +31,16 @@ static void randname(char *buf)
r >>= 5; r >>= 5;
} }
} }
#endif
static int create_shm_file(void) 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; int retries = 100;
do { do {
@ -37,6 +56,7 @@ static int create_shm_file(void)
} while (retries > 0 && errno == EEXIST); } while (retries > 0 && errno == EEXIST);
return -1; return -1;
#endif
} }
int shm_alloc_fd(size_t size) int shm_alloc_fd(size_t size)