[PATCH wayland 3/4] cursor: use os_create_anonymous_file()

Pekka Paalanen ppaalanen at gmail.com
Tue Jul 10 03:05:02 PDT 2012


Copy the implementation of os_create_anonymous_file() here from weston,
so we can use it instead of hardcoding a path to /tmp.

Signed-off-by: Pekka Paalanen <ppaalanen at gmail.com>
---
 configure.ac              |    2 +-
 cursor/Makefile.am        |    2 +
 cursor/os-compatibility.c |  128 +++++++++++++++++++++++++++++++++++++++++++++
 cursor/os-compatibility.h |   31 +++++++++++
 cursor/wayland-cursor.c   |    9 +--
 5 files changed, 165 insertions(+), 7 deletions(-)
 create mode 100644 cursor/os-compatibility.c
 create mode 100644 cursor/os-compatibility.h

diff --git a/configure.ac b/configure.ac
index ad9de80..ae32c19 100644
--- a/configure.ac
+++ b/configure.ac
@@ -39,7 +39,7 @@ if test "x$GCC" = "xyes"; then
 fi
 AC_SUBST(GCC_CFLAGS)
 
-AC_CHECK_FUNCS([accept4])
+AC_CHECK_FUNCS([accept4 mkostemp])
 
 AC_ARG_ENABLE([scanner],
               [AC_HELP_STRING([--disable-scanner],
diff --git a/cursor/Makefile.am b/cursor/Makefile.am
index 3efcb17..3987546 100644
--- a/cursor/Makefile.am
+++ b/cursor/Makefile.am
@@ -4,6 +4,8 @@ include_HEADERS = wayland-cursor.h
 
 libwayland_cursor_la_SOURCES =			\
 	wayland-cursor.c			\
+	os-compatibility.c			\
+	os-compatibility.h			\
 	xcursor.c				\
 	xcursor.h
 libwayland_cursor_la_LIBADD = $(top_builddir)/src/libwayland-client.la
diff --git a/cursor/os-compatibility.c b/cursor/os-compatibility.c
new file mode 100644
index 0000000..418b0d3
--- /dev/null
+++ b/cursor/os-compatibility.c
@@ -0,0 +1,128 @@
+/*
+ * Copyright © 2012 Collabora, Ltd.
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and its
+ * documentation for any purpose is hereby granted without fee, provided that
+ * the above copyright notice appear in all copies and that both that copyright
+ * notice and this permission notice appear in supporting documentation, and
+ * that the name of the copyright holders not be used in advertising or
+ * publicity pertaining to distribution of the software without specific,
+ * written prior permission.  The copyright holders make no representations
+ * about the suitability of this software for any purpose.  It is provided "as
+ * is" without express or implied warranty.
+ *
+ * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
+ * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
+ * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
+ * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
+ * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
+ * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
+ * OF THIS SOFTWARE.
+ */
+
+#define _GNU_SOURCE
+
+#include <sys/types.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <string.h>
+#include <stdlib.h>
+
+#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 is suitable for buffer sharing between processes by
+ * transmitting the file descriptor over Unix sockets using the
+ * SCM_RIGHTS methods.
+ */
+int
+os_create_anonymous_file(off_t size)
+{
+	static const char template[] = "/weston-shared-XXXXXX";
+	const char *path;
+	char *name;
+	int fd;
+
+	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);
+
+	if (fd < 0)
+		return -1;
+
+	if (ftruncate(fd, size) < 0) {
+		close(fd);
+		return -1;
+	}
+
+	return fd;
+}
diff --git a/cursor/os-compatibility.h b/cursor/os-compatibility.h
new file mode 100644
index 0000000..947555c
--- /dev/null
+++ b/cursor/os-compatibility.h
@@ -0,0 +1,31 @@
+/*
+ * Copyright © 2012 Collabora, Ltd.
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and its
+ * documentation for any purpose is hereby granted without fee, provided that
+ * the above copyright notice appear in all copies and that both that copyright
+ * notice and this permission notice appear in supporting documentation, and
+ * that the name of the copyright holders not be used in advertising or
+ * publicity pertaining to distribution of the software without specific,
+ * written prior permission.  The copyright holders make no representations
+ * about the suitability of this software for any purpose.  It is provided "as
+ * is" without express or implied warranty.
+ *
+ * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
+ * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
+ * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
+ * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
+ * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
+ * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
+ * OF THIS SOFTWARE.
+ */
+
+#ifndef OS_COMPATIBILITY_H
+#define OS_COMPATIBILITY_H
+
+#include <sys/types.h>
+
+int
+os_create_anonymous_file(off_t size);
+
+#endif /* OS_COMPATIBILITY_H */
diff --git a/cursor/wayland-cursor.c b/cursor/wayland-cursor.c
index 36a8b54..c41860a 100644
--- a/cursor/wayland-cursor.c
+++ b/cursor/wayland-cursor.c
@@ -29,6 +29,8 @@
 #include <unistd.h>
 #include <sys/mman.h>
 
+#include "os-compatibility.h"
+
 struct shm_pool {
 	struct wl_shm_pool *pool;
 	int fd;
@@ -41,22 +43,17 @@ static struct shm_pool *
 shm_pool_create(struct wl_shm *shm, int size)
 {
 	struct shm_pool *pool;
-	char filename[] = "/tmp/wayland-shm-XXXXXX";
 
 	pool = malloc(sizeof *pool);
 	if (!pool)
 		return NULL;
 
-	pool->fd = mkstemp(filename);
+	pool->fd = os_create_anonymous_file(size);
 	if (pool->fd < 0)
 		goto err_free;
 
-	if (ftruncate(pool->fd, size) < 0)
-		goto err_close;
-
 	pool->data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED,
 			  pool->fd, 0);
-	unlink(filename);
 
 	if (pool->data == MAP_FAILED)
 		goto err_close;
-- 
1.7.8.6



More information about the wayland-devel mailing list