[Bf-blender-cvs] [4e65b1ef6cd] master: GHOST/wayland: create mmap-ed file manually if memfd_create is unavailable

Christian Rauch noreply at git.blender.org
Thu Jul 15 00:29:51 CEST 2021


Commit: 4e65b1ef6cdaa0fbe6bac1d1f4a54da4d3247584
Author: Christian Rauch
Date:   Mon Jul 12 23:28:41 2021 +0100
Branches: master
https://developer.blender.org/rB4e65b1ef6cdaa0fbe6bac1d1f4a54da4d3247584

GHOST/wayland: create mmap-ed file manually if memfd_create is unavailable

===================================================================

M	intern/ghost/CMakeLists.txt
M	intern/ghost/intern/GHOST_SystemWayland.cpp

===================================================================

diff --git a/intern/ghost/CMakeLists.txt b/intern/ghost/CMakeLists.txt
index a35c9fffcab..76cac1049fb 100644
--- a/intern/ghost/CMakeLists.txt
+++ b/intern/ghost/CMakeLists.txt
@@ -288,6 +288,13 @@ elseif(WITH_GHOST_X11 OR WITH_GHOST_WAYLAND)
       ${dbus_INCLUDE_DIRS}
     )
 
+    include(CheckSymbolExists)
+    set(CMAKE_REQUIRED_DEFINITIONS "-D_GNU_SOURCE")
+    check_symbol_exists(memfd_create "sys/mman.h" HAVE_MEMFD_CREATE)
+    if (HAVE_MEMFD_CREATE)
+      add_definitions(-DHAVE_MEMFD_CREATE)
+    endif()
+
     list(APPEND SRC
       intern/GHOST_SystemWayland.cpp
       intern/GHOST_WindowWayland.cpp
diff --git a/intern/ghost/intern/GHOST_SystemWayland.cpp b/intern/ghost/intern/GHOST_SystemWayland.cpp
index 75a80de983d..38700845405 100644
--- a/intern/ghost/intern/GHOST_SystemWayland.cpp
+++ b/intern/ghost/intern/GHOST_SystemWayland.cpp
@@ -1804,13 +1804,43 @@ GHOST_TSuccess GHOST_SystemWayland::setCustomCursorShape(uint8_t *bitmap,
   static const int32_t stride = sizex * 4; /* ARGB */
   cursor->file_buffer->size = size_t(stride * sizey);
 
+#ifdef HAVE_MEMFD_CREATE
   const int fd = memfd_create("blender-cursor-custom", MFD_CLOEXEC | MFD_ALLOW_SEALING);
-  fcntl(fd, F_ADD_SEALS, F_SEAL_SHRINK);
-  posix_fallocate(fd, 0, int32_t(cursor->file_buffer->size));
+  if (fd >= 0) {
+    fcntl(fd, F_ADD_SEALS, F_SEAL_SHRINK | F_SEAL_SEAL);
+  }
+#else
+  char *path = getenv("XDG_RUNTIME_DIR");
+  if (!path) {
+    errno = ENOENT;
+    return GHOST_kFailure;
+  }
+
+  char *tmpname;
+  asprintf(&tmpname, "%s/%s", path, "blender-XXXXXX");
+  const int fd = mkostemp(tmpname, O_CLOEXEC);
+  if (fd >= 0) {
+    unlink(tmpname);
+  }
+  free(tmpname);
+#endif
+
+  if (fd < 0) {
+    return GHOST_kFailure;
+  }
+
+  if (posix_fallocate(fd, 0, int32_t(cursor->file_buffer->size)) != 0) {
+    return GHOST_kFailure;
+  }
 
   cursor->file_buffer->data = mmap(
       nullptr, cursor->file_buffer->size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
 
+  if (cursor->file_buffer->data == MAP_FAILED) {
+    close(fd);
+    return GHOST_kFailure;
+  }
+
   struct wl_shm_pool *pool = wl_shm_create_pool(d->shm, fd, int32_t(cursor->file_buffer->size));
 
   wl_buffer *buffer = wl_shm_pool_create_buffer(



More information about the Bf-blender-cvs mailing list