[vlc-devel] [PATCH v3 2/14] platform: obey $TMPDIR for linux/posix in vlc_memfd()

Lyndon Brown jnqnfe at gmail.com
Wed Oct 14 04:52:13 CEST 2020


From: Lyndon Brown <jnqnfe at gmail.com>
Date: Mon, 5 Oct 2020 21:07:14 +0100
Subject: platform: obey $TMPDIR for linux/posix in vlc_memfd()

on posix/linux the $TMPDIR path should be preferred, using "/tmp" as a
fallback. we have not used our new config_GetTempPath() helper here in
order to avoid an unnecessary strdup.

diff --git a/src/linux/filesystem.c b/src/linux/filesystem.c
index e94a60ec45..dfd691ddd2 100644
--- a/src/linux/filesystem.c
+++ b/src/linux/filesystem.c
@@ -43,16 +43,24 @@ int vlc_memfd(void)
         return fd;
 #endif
 
+    const char *tempdir = getenv("TMPDIR");
+    if (tempdir == NULL)
+        tempdir = "/tmp";
+
     /* Fallback to open with O_TMPFILE, */
-    fd = open("/tmp", O_RDWR | O_CLOEXEC | O_TMPFILE, S_IRUSR | S_IWUSR);
+    fd = open(tempdir, O_RDWR | O_CLOEXEC | O_TMPFILE, S_IRUSR | S_IWUSR);
     if (fd != -1 || (errno != EISDIR && errno != EOPNOTSUPP))
         return fd;
 
     /* Fallback to POSIX implementation if O_TMPFILE is not supported (errno is
      * EISDIR, or EOPNOTSUPP, cf. man open(2). */
-    char bufpath[] = "/tmp/"PACKAGE_NAME"XXXXXX";
+    char *bufpath;
+    if (asprintf(&bufpath, "%s/" PACKAGE_NAME "XXXXXX", tempdir) == -1)
+        return -1;
+
     fd = vlc_mkstemp(bufpath);
     if (fd != -1)
         unlink(bufpath);
+    free(bufpath);
     return fd;
 }
diff --git a/src/posix/filesystem.c b/src/posix/filesystem.c
index 60a997d9e0..aaa936047e 100644
--- a/src/posix/filesystem.c
+++ b/src/posix/filesystem.c
@@ -90,12 +90,19 @@ int vlc_mkstemp (char *template)
 
 VLC_WEAK int vlc_memfd(void)
 {
-    char bufpath[] = "/tmp/"PACKAGE_NAME"XXXXXX";
+    const char *tempdir = getenv("TMPDIR");
+    if (tempdir == NULL)
+        tempdir = "/tmp";
+
     int fd;
+    char *bufpath;
+    if (asprintf(&bufpath, "%s/" PACKAGE_NAME "XXXXXX", tempdir) == -1)
+        return -1;
 
     fd = vlc_mkstemp (bufpath);
     if (fd != -1)
         unlink (bufpath);
+    free(bufpath);
     return fd;
 }
 



More information about the vlc-devel mailing list