core: use a memfd for serialization
authorLennart Poettering <lennart@poettering.net>
Fri, 3 Feb 2017 15:30:00 +0000 (16:30 +0100)
committerLennart Poettering <lennart@poettering.net>
Mon, 6 Feb 2017 15:58:35 +0000 (16:58 +0100)
If we can, use a memfd for serializing state during a daemon reload or
reexec. Fall back to a file in /run/systemd or /tmp only if memfds are
not available.

See: #5016

src/core/manager.c

index b22f85f..e4da945 100644 (file)
@@ -2436,18 +2436,22 @@ void manager_send_unit_plymouth(Manager *m, Unit *u) {
 }
 
 int manager_open_serialization(Manager *m, FILE **_f) {
-        const char *path;
         int fd = -1;
         FILE *f;
 
         assert(_f);
 
-        path = MANAGER_IS_SYSTEM(m) ? "/run/systemd" : "/tmp";
-        fd = open_tmpfile_unlinkable(path, O_RDWR|O_CLOEXEC);
-        if (fd < 0)
-                return -errno;
+        fd = memfd_create("systemd-serialization", MFD_CLOEXEC);
+        if (fd < 0) {
+                const char *path;
 
-        log_debug("Serializing state to %s", path);
+                path = MANAGER_IS_SYSTEM(m) ? "/run/systemd" : "/tmp";
+                fd = open_tmpfile_unlinkable(path, O_RDWR|O_CLOEXEC);
+                if (fd < 0)
+                        return -errno;
+                log_debug("Serializing state to %s.", path);
+        } else
+                log_debug("Serializing state to memfd.");
 
         f = fdopen(fd, "w+");
         if (!f) {