[PATCH weston 1/2] os_create_anonymous_file: support XDG_RUNTIME_DIR being unset

Emilio Pozuelo Monfort pochu27 at gmail.com
Mon Mar 11 09:10:58 PDT 2013


This adds a local copy of wl_get_runtime_dir() from
wayland's src/wayland-util.c

https://bugs.freedesktop.org/show_bug.cgi?id=62092
---
 shared/os-compatibility.c |   39 +++++++++++++++++++++++++++++++++------
 1 file changed, 33 insertions(+), 6 deletions(-)

diff --git a/shared/os-compatibility.c b/shared/os-compatibility.c
index 21d4d02..97bdda0 100644
--- a/shared/os-compatibility.c
+++ b/shared/os-compatibility.c
@@ -30,6 +30,7 @@
 #include <sys/epoll.h>
 #include <string.h>
 #include <stdlib.h>
+#include <stdio.h>
 
 #include "os-compatibility.h"
 
@@ -118,6 +119,31 @@ create_tmpfile_cloexec(char *tmpname)
 	return fd;
 }
 
+static char *
+_get_runtime_dir (void)
+{
+       const char *runtime_dir, *home_dir;
+       char *dir;
+
+       /* Try XDG_RUNTIME_DIR first */
+       runtime_dir = getenv("XDG_RUNTIME_DIR");
+       if (runtime_dir != NULL)
+               return strdup(runtime_dir);
+
+       /* XDG_RUNTIME_DIR unset, fall back to ~/.cache */
+       home_dir = getenv("HOME");
+       if (!home_dir) {
+               errno = ENOENT;
+               return NULL;
+       }
+
+       dir = malloc(strlen(home_dir) + strlen("/.cache") + 1);
+       if (!dir)
+               return NULL;
+       sprintf(dir, "%s/%s", home_dir, ".cache");
+       return dir;
+}
+
 /*
  * Create a new, unique, anonymous file of the given size, and
  * return the file descriptor for it. The file descriptor is set
@@ -137,21 +163,22 @@ int
 os_create_anonymous_file(off_t size)
 {
 	static const char template[] = "/weston-shared-XXXXXX";
-	const char *path;
+	char *path;
 	char *name;
 	int fd;
 
-	path = getenv("XDG_RUNTIME_DIR");
-	if (!path) {
-		errno = ENOENT;
+	path = _get_runtime_dir();
+	if (!path)
 		return -1;
-	}
 
 	name = malloc(strlen(path) + sizeof(template));
-	if (!name)
+	if (!name) {
+		free(path);
 		return -1;
+	}
 
 	strcpy(name, path);
+	free(path);
 	strcat(name, template);
 
 	fd = create_tmpfile_cloexec(name);
-- 
1.7.10.4



More information about the wayland-devel mailing list