[sysprof: 40/63] libsysprof-capture: Use mkostemp() rather than g_file_open_tmp()



commit 621e7ea31476cfc191f6db2c2f276fb7255b789e
Author: Philip Withnall <withnall endlessm com>
Date:   Thu Jul 2 12:23:28 2020 +0100

    libsysprof-capture: Use mkostemp() rather than g_file_open_tmp()
    
    Signed-off-by: Philip Withnall <withnall endlessm com>
    
    Helps: #40

 src/libsysprof-capture/sysprof-platform.c | 48 +++++++++++++++++++++++++++----
 1 file changed, 43 insertions(+), 5 deletions(-)
---
diff --git a/src/libsysprof-capture/sysprof-platform.c b/src/libsysprof-capture/sysprof-platform.c
index 3449cfa..1a344a5 100644
--- a/src/libsysprof-capture/sysprof-platform.c
+++ b/src/libsysprof-capture/sysprof-platform.c
@@ -58,12 +58,34 @@
 
 #include <glib.h>
 #include <glib/gstdio.h>
+#include <stdlib.h>
 #include <sys/syscall.h>
 #include <unistd.h>
 
 #include "sysprof-capture-util-private.h"
 #include "sysprof-platform.h"
 
+#ifndef __NR_memfd_create
+static const char *
+get_tmpdir (void)
+{
+  const char *tmpdir = NULL;
+
+  if (tmpdir == NULL || *tmpdir == '\0')
+    tmpdir = getenv ("TMPDIR");
+
+#ifdef P_tmpdir
+  if (tmpdir == NULL || *tmpdir == '\0')
+    tmpdir = P_tmpdir;
+#endif
+
+  if (tmpdir == NULL || *tmpdir == '\0')
+    tmpdir = "/tmp";
+
+  return tmpdir;
+}
+#endif  /* !__NR_memfd_create */
+
 /**
  * sysprof_memfd_create:
  * @name: (nullable): A descriptive name for the memfd or %NULL
@@ -82,8 +104,10 @@ sysprof_memfd_create (const char *name)
     name = "[sysprof]";
   return syscall (__NR_memfd_create, name, 0);
 #else
-  gchar *name_used = NULL;
   int fd;
+  const char *tmpdir;
+  char *template = NULL;
+  size_t template_len = 0;
 
   /*
    * TODO: It would be nice to ensure tmpfs
@@ -93,14 +117,28 @@ sysprof_memfd_create (const char *name)
    * that the tmpfile we open is on tmpfs so that we get anonymous backed
    * pages after unlinking.
    */
-  fd = g_file_open_tmp (NULL, &name_used, NULL);
 
-  if (name_used != NULL)
+  tmpdir = get_tmpdir ();
+  template_len = strlen (tmpdir) + 1 + strlen ("sysprof-XXXXXX") + 1;
+  template = sysprof_malloc0 (template_len);
+  if (template == NULL)
+    {
+      errno = ENOMEM;
+      return -1;
+    }
+
+  snprintf (template, template_len, "%s/sysprof-XXXXXX", tmpdir);
+
+  fd = TEMP_FAILURE_RETRY (mkostemp (template, O_BINARY | O_CLOEXEC));
+  if (fd < 0)
     {
-      g_unlink (name_used);
-      g_free (name_used);
+      free (template);
+      return -1;
     }
 
+  unlink (template);
+  free (template);
+
   return fd;
 #endif
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]