[vlc-devel] [PATCH] filesystem: fix vlc_memfd when "/tmp" is missing

Thomas Guillem thomas at gllm.fr
Mon Oct 5 16:23:40 CEST 2015


Try using the TMPDIR environment variable if "/tmp" is missing.
---
 src/posix/filesystem.c | 34 +++++++++++++++++++++++++++++-----
 1 file changed, 29 insertions(+), 5 deletions(-)

diff --git a/src/posix/filesystem.c b/src/posix/filesystem.c
index 9cf6876..b8a7e4a 100644
--- a/src/posix/filesystem.c
+++ b/src/posix/filesystem.c
@@ -112,11 +112,11 @@ int vlc_mkstemp (char *template)
     return fd;
 }
 
-int vlc_memfd (void)
+static int vlc_memfd_tmpname (const char *tmp_dir, char *tmp_file)
 {
     int fd;
 #ifdef O_TMPFILE
-    fd = vlc_open ("/tmp", O_RDWR|O_TMPFILE, S_IRUSR|S_IWUSR);
+    fd = vlc_open (tmp_dir, O_RDWR|O_TMPFILE, S_IRUSR|S_IWUSR);
     if (fd != -1)
         return fd;
     /* ENOENT means either /tmp is missing (!) or the kernel does not support
@@ -127,11 +127,35 @@ int vlc_memfd (void)
         return -1;
 #endif
 
+    fd = vlc_mkstemp (tmp_file);
+    if (fd != -1)
+        unlink (tmp_file);
+    return fd;
+}
+
+int vlc_memfd (void)
+{
+    int fd;
+
     char bufpath[] = "/tmp/"PACKAGE_NAME"XXXXXX";
 
-    fd = vlc_mkstemp (bufpath);
-    if (fd != -1)
-        unlink (bufpath);
+    if ((fd = vlc_memfd_tmpname("/tmp", bufpath)) == -1)
+    {
+        /* vlc_memfd failed with "/tmp" dir, try using TMPDIR env */
+
+        const char *tmp_dir = (const char *) getenv("TMPDIR");
+        char *tmp_file = NULL;
+
+        if (!strcmp(tmp_dir, "/tmp"))
+            return -1;
+
+        if (asprintf(&tmp_file, "%s/"PACKAGE_NAME"XXXXXX", tmp_dir) < 0)
+            return -1;
+
+        fd = vlc_memfd_tmpname(tmp_dir, tmp_file);
+        free(tmp_file);
+    }
+
     return fd;
 }
 
-- 
2.1.4



More information about the vlc-devel mailing list