chiark / gitweb /
memfd: always create our memfds with CLOEXEC set
[elogind.git] / src / shared / memfd.c
index c21642f49ae9b568a6ec9b2822b9cb5ff0ee7130..f3ce8f84da50a9eb0c3ce6c9727f1c2df8033677 100644 (file)
 #include <sys/mman.h>
 #include <sys/prctl.h>
 
+#ifdef HAVE_LINUX_MEMFD_H
+#  include <linux/memfd.h>
+#endif
+
 #include "util.h"
 #include "bus-label.h"
-#include "missing.h"
 #include "memfd.h"
 #include "utf8.h"
+#include "missing.h"
 
 int memfd_new(const char *name) {
-
         _cleanup_free_ char *g = NULL;
         int fd;
 
-        if (name) {
-                g = utf8_escape_invalid(name);
-                if (!g)
-                        return -ENOMEM;
-
-                name = g;
-        } else {
+        if (!name) {
                 char pr[17] = {};
 
                 /* If no name is specified we generate one. We include
@@ -54,7 +51,13 @@ int memfd_new(const char *name) {
                 if (isempty(pr))
                         name = "sd";
                 else {
-                        g = strappend("sd-", pr);
+                        _cleanup_free_ char *e = NULL;
+
+                        e = utf8_escape_invalid(pr);
+                        if (!e)
+                                return -ENOMEM;
+
+                        g = strappend("sd-", e);
                         if (!g)
                                 return -ENOMEM;
 
@@ -62,7 +65,7 @@ int memfd_new(const char *name) {
                 }
         }
 
-        fd = memfd_create(name, MFD_ALLOW_SEALING);
+        fd = memfd_create(name, MFD_ALLOW_SEALING | MFD_CLOEXEC);
         if (fd < 0)
                 return -errno;
 
@@ -130,7 +133,7 @@ int memfd_get_size(int fd, uint64_t *sz) {
                 return -errno;
 
         *sz = stat.st_size;
-        return r;
+        return 0;
 }
 
 int memfd_set_size(int fd, uint64_t sz) {
@@ -142,7 +145,7 @@ int memfd_set_size(int fd, uint64_t sz) {
         if (r < 0)
                 return -errno;
 
-        return r;
+        return 0;
 }
 
 int memfd_new_and_map(const char *name, size_t sz, void **p) {