chiark / gitweb /
memfd: always create our memfds with CLOEXEC set
[elogind.git] / src / shared / memfd.c
index 6e2319d6eed5f9262598780af4cad81d64703c57..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;
 
-        assert(name);
-
-        if (name) {
-                /* The kernel side is pretty picky about the character
-                 * set here, let's do the usual bus escaping to deal
-                 * with that. */
-
-                g = bus_label_escape(name);
-                if (!g)
-                        return -ENOMEM;
-
-                name = g;
-
-        } else {
+        if (!name) {
                 char pr[17] = {};
 
                 /* If no name is specified we generate one. We include
@@ -62,7 +53,7 @@ int memfd_new(const char *name) {
                 else {
                         _cleanup_free_ char *e = NULL;
 
-                        e = bus_label_escape(pr);
+                        e = utf8_escape_invalid(pr);
                         if (!e)
                                 return -ENOMEM;
 
@@ -74,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;
 
@@ -142,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) {
@@ -154,14 +145,13 @@ 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) {
         _cleanup_close_ int fd = -1;
         int r;
 
-        assert(name);
         assert(sz > 0);
         assert(p);
 
@@ -221,7 +211,7 @@ int memfd_get_name(int fd, char **name) {
         if (!n)
                 return -ENOMEM;
 
-        e = bus_label_unescape(n);
+        e = utf8_escape_invalid(n);
         if (!e)
                 return -ENOMEM;