]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
core/manager: split out creation of serialization fd out to a helper
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sat, 11 Feb 2017 23:33:16 +0000 (18:33 -0500)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Mon, 20 Feb 2017 23:49:09 +0000 (18:49 -0500)
There is a slight change in behaviour: the user manager for root will create a
temporary file in /run/systemd, not /tmp. I don't think this matters, but
simplifies implementation.

src/basic/fileio.c
src/basic/fileio.h
src/core/manager.c
src/test/test-fd-util.c

index c43b0583a4a93e6f28658d9de84ae063e4efc98b..ac65fada35ac75cacb010f6a21a35fecec9f1da4 100644 (file)
@@ -1342,6 +1342,25 @@ int open_tmpfile_linkable(const char *target, int flags, char **ret_path) {
         return fd;
 }
 
+int open_serialization_fd(const char *ident) {
+        int fd = -1;
+
+        fd = memfd_create(ident, MFD_CLOEXEC);
+        if (fd < 0) {
+                const char *path;
+
+                path = getpid() == 1 ? "/run/systemd" : "/tmp";
+                fd = open_tmpfile_unlinkable(path, O_RDWR|O_CLOEXEC);
+                if (fd < 0)
+                        return fd;
+
+                log_debug("Serializing %s to %s.", ident, path);
+        } else
+                log_debug("Serializing %s to memfd.", ident);
+
+        return fd;
+}
+
 int link_tmpfile(int fd, const char *path, const char *target) {
 
         assert(fd >= 0);
index 17b38a5d60e5567a8fb6e9ea379de644676eabe1..64852b15a893dc76fd765690175b19b470bdef1e 100644 (file)
@@ -84,6 +84,7 @@ int fputs_with_space(FILE *f, const char *s, const char *separator, bool *space)
 
 int open_tmpfile_unlinkable(const char *directory, int flags);
 int open_tmpfile_linkable(const char *target, int flags, char **ret_path);
+int open_serialization_fd(const char *ident);
 
 int link_tmpfile(int fd, const char *path, const char *target);
 
index 0884534cc49567b946117edb11bcb9e295c3a128..d432512a5902376704dcc1d462b4f69941aca4fb 100644 (file)
@@ -2437,22 +2437,14 @@ void manager_send_unit_plymouth(Manager *m, Unit *u) {
 }
 
 int manager_open_serialization(Manager *m, FILE **_f) {
-        int fd = -1;
+        int fd;
         FILE *f;
 
         assert(_f);
 
-        fd = memfd_create("systemd-serialization", MFD_CLOEXEC);
-        if (fd < 0) {
-                const char *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.");
+        fd = open_serialization_fd("systemd-state");
+        if (fd < 0)
+                return fd;
 
         f = fdopen(fd, "w+");
         if (!f) {
@@ -2461,7 +2453,6 @@ int manager_open_serialization(Manager *m, FILE **_f) {
         }
 
         *_f = f;
-
         return 0;
 }
 
index f555bb976ccc31c8c407d64fe80640ade9740b7a..4425b5fe5fd0bbb0f67a91fd2a6c2a4b74a0100a 100644 (file)
@@ -94,10 +94,20 @@ static void test_same_fd(void) {
         assert_se(same_fd(b, a) == 0);
 }
 
+static void test_open_serialization_fd(void) {
+        _cleanup_close_ int fd = -1;
+
+        fd = open_serialization_fd("test");
+        assert_se(fd >= 0);
+
+        write(fd, "test\n", 5);
+}
+
 int main(int argc, char *argv[]) {
         test_close_many();
         test_close_nointr();
         test_same_fd();
+        test_open_serialization_fd();
 
         return 0;
 }