[PATCH wayland] cursor: Use shm_open() instead of XDG_RUNTIME_DIR files

Quentin Glidic sardemff7+wayland at sardemff7.net
Wed Oct 18 09:23:49 UTC 2017


From: Quentin Glidic <sardemff7+git at sardemff7.net>

Using XDG_RUNTIME_DIR to create anonymous files is problematic when
XDG_RUNTIME_DIR is not a tmpfs. OTOH, shm_open is in POSIX.1-2001, so it
should be available on all platforms we care about.
As a bonus, the created fd is already CLOEXEC.

Signed-off-by: Quentin Glidic <sardemff7+git at sardemff7.net>
---
 configure.ac              |  2 +-
 cursor/os-compatibility.c | 68 +++--------------------------------------------
 2 files changed, 5 insertions(+), 65 deletions(-)

diff --git a/configure.ac b/configure.ac
index fcb9718..71ed420 100644
--- a/configure.ac
+++ b/configure.ac
@@ -61,7 +61,7 @@ if test "x$GCC" = "xyes"; then
 fi
 AC_SUBST(GCC_CFLAGS)
 
-AC_CHECK_FUNCS([accept4 mkostemp posix_fallocate])
+AC_CHECK_FUNCS([accept4 posix_fallocate])
 
 AC_ARG_ENABLE([libraries],
 	      [AC_HELP_STRING([--disable-libraries],
diff --git a/cursor/os-compatibility.c b/cursor/os-compatibility.c
index e972d21..69aeab9 100644
--- a/cursor/os-compatibility.c
+++ b/cursor/os-compatibility.c
@@ -26,6 +26,7 @@
 #define _GNU_SOURCE
 
 #include <sys/types.h>
+#include <sys/mman.h>
 #include <unistd.h>
 #include <fcntl.h>
 #include <errno.h>
@@ -35,60 +36,13 @@
 #include "config.h"
 #include "os-compatibility.h"
 
-#ifndef HAVE_MKOSTEMP
-static int
-set_cloexec_or_close(int fd)
-{
-	long flags;
-
-	if (fd == -1)
-		return -1;
-
-	flags = fcntl(fd, F_GETFD);
-	if (flags == -1)
-		goto err;
-
-	if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1)
-		goto err;
-
-	return fd;
-
-err:
-	close(fd);
-	return -1;
-}
-#endif
-
-static int
-create_tmpfile_cloexec(char *tmpname)
-{
-	int fd;
-
-#ifdef HAVE_MKOSTEMP
-	fd = mkostemp(tmpname, O_CLOEXEC);
-	if (fd >= 0)
-		unlink(tmpname);
-#else
-	fd = mkstemp(tmpname);
-	if (fd >= 0) {
-		fd = set_cloexec_or_close(fd);
-		unlink(tmpname);
-	}
-#endif
-
-	return fd;
-}
-
 /*
  * Create a new, unique, anonymous file of the given size, and
  * return the file descriptor for it. The file descriptor is set
  * CLOEXEC. The file is immediately suitable for mmap()'ing
  * the given size at offset zero.
  *
- * The file should not have a permanent backing store like a disk,
- * but may have if XDG_RUNTIME_DIR is not properly implemented in OS.
- *
- * The file name is deleted from the file system.
+ * The file will not have a permanent backing store like a disk.
  *
  * The file is suitable for buffer sharing between processes by
  * transmitting the file descriptor over Unix sockets using the
@@ -109,22 +63,8 @@ os_create_anonymous_file(off_t size)
 	int fd;
 	int ret;
 
-	path = getenv("XDG_RUNTIME_DIR");
-	if (!path) {
-		errno = ENOENT;
-		return -1;
-	}
-
-	name = malloc(strlen(path) + sizeof(template));
-	if (!name)
-		return -1;
-
-	strcpy(name, path);
-	strcat(name, template);
-
-	fd = create_tmpfile_cloexec(name);
-
-	free(name);
+	fd = shm_open("/wayland-cursor-shared", O_CREAT | O_RDWR, 0);
+	shm_unlink("/wayland-cursor-shared");
 
 	if (fd < 0)
 		return -1;
-- 
2.13.4



More information about the wayland-devel mailing list