aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--meson.build4
-rw-r--r--pgo/pgo.c9
-rw-r--r--shm.c14
3 files changed, 25 insertions, 2 deletions
diff --git a/meson.build b/meson.build
index de159204..e1b9eb51 100644
--- a/meson.build
+++ b/meson.build
@@ -12,6 +12,10 @@ is_debug_build = get_option('buildtype').startswith('debug')
cc = meson.get_compiler('c')
+if cc.has_function('memfd_create')
+ add_project_arguments('-DMEMFD_CREATE', language: 'c')
+endif
+
add_project_arguments(
['-D_POSIX_C_SOURCE=200809L', '-D_GNU_SOURCE=200809L'] +
(is_debug_build
diff --git a/pgo/pgo.c b/pgo/pgo.c
index 39c20de0..fa95a0ec 100644
--- a/pgo/pgo.c
+++ b/pgo/pgo.c
@@ -257,7 +257,16 @@ main(int argc, const char *const *argv)
close(fd);
+#if defined(MEMFD_CREATE)
int mem_fd = memfd_create("foot-pgo-ptmx", MFD_CLOEXEC);
+#elif defined(__FreeBSD__)
+ // memfd_create on FreeBSD 13 is SHM_ANON without sealing support
+ int mem_fd = shm_open(SHM_ANON, O_RDWR | O_CLOEXEC, 0600);
+#else
+ char name[] = "/tmp/foot-pgo-ptmx-XXXXXX";
+ int mem_fd = mkostemp(name, O_CLOEXEC);
+ unlink(name);
+#endif
if (mem_fd < 0) {
fprintf(stderr, "error: failed to create memory FD\n");
goto out;
diff --git a/shm.c b/shm.c
index 81de653b..85d67f9c 100644
--- a/shm.c
+++ b/shm.c
@@ -10,8 +10,7 @@
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/time.h>
-#include <linux/mman.h>
-#include <linux/memfd.h>
+#include <sys/mman.h>
#include <fcntl.h>
#include <pixman.h>
@@ -276,7 +275,16 @@ shm_get_buffer(struct wl_shm *shm, int width, int height, unsigned long cookie,
LOG_DBG("cookie=%lx: allocating new buffer: %zu KB", cookie, size / 1024);
/* Backing memory for SHM */
+#if defined(MEMFD_CREATE)
pool_fd = memfd_create("foot-wayland-shm-buffer-pool", MFD_CLOEXEC | MFD_ALLOW_SEALING);
+#elif defined(__FreeBSD__)
+ // memfd_create on FreeBSD 13 is SHM_ANON without sealing support
+ pool_fd = shm_open(SHM_ANON, O_RDWR | O_CLOEXEC, 0600);
+#else
+ char name[] = "/tmp/foot-wayland-shm-buffer-pool-XXXXXX";
+ pool_fd = mkostemp(name, O_CLOEXEC);
+ unlink(name);
+#endif
if (pool_fd == -1) {
LOG_ERRNO("failed to create SHM backing memory file");
goto err;
@@ -335,6 +343,7 @@ shm_get_buffer(struct wl_shm *shm, int width, int height, unsigned long cookie,
goto err;
}
+#if defined(MEMFD_CREATE)
/* Seal file - we no longer allow any kind of resizing */
/* TODO: wayland mmaps(PROT_WRITE), for some unknown reason, hence we cannot use F_SEAL_FUTURE_WRITE */
if (fcntl(pool_fd, F_ADD_SEALS,
@@ -343,6 +352,7 @@ shm_get_buffer(struct wl_shm *shm, int width, int height, unsigned long cookie,
LOG_ERRNO("failed to seal SHM backing memory file");
/* This is not a fatal error */
}
+#endif
pool = wl_shm_create_pool(shm, pool_fd, memfd_size);
if (pool == NULL) {