]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
network-generator: rewrite unit if it already exists and its content changed 26944/head
authorAntonio Alvarez Feijoo <antonio.feijoo@suse.com>
Mon, 10 Apr 2023 13:18:53 +0000 (15:18 +0200)
committerAntonio Alvarez Feijoo <antonio.feijoo@suse.com>
Mon, 10 Apr 2023 13:18:53 +0000 (15:18 +0200)
When the `systemd-network-generator` is included in the initrd and runs from
there first, the next times it runs after switching to real root it
thinks there is a duplicate entry on the kernel command line.

This patch rewrites the unit file if the content has changed, instead of
displaying an error message.

src/network/generator/main.c

index a0af0b831b40660aa012844bbc7486d62bda65c4..492980249b7c849f1e518a9a109ae9ab8b1d9880 100644 (file)
@@ -4,6 +4,7 @@
 
 #include "build.h"
 #include "fd-util.h"
+#include "fs-util.h"
 #include "generator.h"
 #include "macro.h"
 #include "main-func.h"
 static const char *arg_root = NULL;
 
 static int network_save(Network *network, const char *dest_dir) {
-        _cleanup_free_ char *filename = NULL;
+        _cleanup_free_ char *filename = NULL, *p = NULL;
+        _cleanup_(unlink_and_freep) char *temp_path = NULL;
         _cleanup_fclose_ FILE *f = NULL;
         int r;
 
         assert(network);
 
+        r = generator_open_unit_file_full(dest_dir, NULL, NULL, &f, &temp_path);
+        if (r < 0)
+                return r;
+
+        network_dump(network, f);
+
         r = asprintf(&filename, "%s-%s.network",
                      isempty(network->ifname) ? "91" : "90",
                      isempty(network->ifname) ? "default" : network->ifname);
         if (r < 0)
                 return log_oom();
 
-        r = generator_open_unit_file(dest_dir, "kernel command line", filename, &f);
+        p = path_join(dest_dir, filename);
+        if (!p)
+                return log_oom();
+
+        r = conservative_rename(temp_path, p);
         if (r < 0)
                 return r;
 
-        network_dump(network, f);
-
+        temp_path = mfree(temp_path);
         return 0;
 }
 
 static int netdev_save(NetDev *netdev, const char *dest_dir) {
-        _cleanup_free_ char *filename = NULL;
+        _cleanup_free_ char *filename = NULL, *p = NULL;
+        _cleanup_(unlink_and_freep) char *temp_path = NULL;
         _cleanup_fclose_ FILE *f = NULL;
         int r;
 
         assert(netdev);
 
+        r = generator_open_unit_file_full(dest_dir, NULL, NULL, &f, &temp_path);
+        if (r < 0)
+                return r;
+
+        netdev_dump(netdev, f);
+
         r = asprintf(&filename, "90-%s.netdev",
                      netdev->ifname);
         if (r < 0)
                 return log_oom();
 
-        r = generator_open_unit_file(dest_dir, "kernel command line", filename, &f);
+        p = path_join(dest_dir, filename);
+        if (!p)
+                return log_oom();
+
+        r = conservative_rename(temp_path, p);
         if (r < 0)
                 return r;
 
-        netdev_dump(netdev, f);
-
+        temp_path = mfree(temp_path);
         return 0;
 }
 
 static int link_save(Link *link, const char *dest_dir) {
-        _cleanup_free_ char *filename = NULL;
+        _cleanup_free_ char *filename = NULL, *p = NULL;
+        _cleanup_(unlink_and_freep) char *temp_path = NULL;
         _cleanup_fclose_ FILE *f = NULL;
         int r;
 
         assert(link);
 
+        r = generator_open_unit_file_full(dest_dir, NULL, NULL, &f, &temp_path);
+        if (r < 0)
+                return r;
+
+        link_dump(link, f);
+
         filename = strjoin(!isempty(link->ifname) ? "90" :
                            !hw_addr_is_null(&link->mac) ? "91" : "92",
                            "-", link->filename, ".link");
         if (!filename)
                 return log_oom();
 
-        r = generator_open_unit_file(dest_dir, "kernel command line", filename, &f);
+        p = path_join(dest_dir, filename);
+        if (!p)
+                return log_oom();
+
+        r = conservative_rename(temp_path, p);
         if (r < 0)
                 return r;
 
-        link_dump(link, f);
-
+        temp_path = mfree(temp_path);
         return 0;
 }