]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
resolve: when writing of private resolv.confs fails, do not remove old copies
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Mon, 20 Apr 2020 12:27:44 +0000 (14:27 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Mon, 20 Apr 2020 13:48:05 +0000 (15:48 +0200)
All callers ignore the return value.

This is almost entirely theoretical, since writing to /run is unlikely to
fail..., but the user is almost certainly better off keeping the old copy
around and having working dns resolution with an out-of-date dns server list
than having having a dangling /etc/resolv.conf symlink.

src/resolve/resolved-resolv-conf.c

index 763fc097407320888b82121d85ad87e3b02762c1..c060ccd7144741e0a2301eef69ac0d9d64a97c0b 100644 (file)
@@ -359,41 +359,38 @@ int manager_write_resolv_conf(Manager *m) {
 
         (void) fchmod(fileno(f_uplink), 0644);
 
-        r = fopen_temporary_label(PRIVATE_STUB_RESOLV_CONF, PRIVATE_STUB_RESOLV_CONF, &f_stub, &temp_path_stub);
-        if (r < 0)
-                return log_warning_errno(r, "Failed to open new %s for writing: %m", PRIVATE_STUB_RESOLV_CONF);
-
-        (void) fchmod(fileno(f_stub), 0644);
-
         r = write_uplink_resolv_conf_contents(f_uplink, dns, domains);
         if (r < 0) {
                 log_error_errno(r, "Failed to write new %s: %m", PRIVATE_UPLINK_RESOLV_CONF);
                 goto fail;
         }
 
-        if (rename(temp_path_uplink, PRIVATE_UPLINK_RESOLV_CONF) < 0) {
-                r = log_error_errno(errno, "Failed to move new %s into place: %m", PRIVATE_UPLINK_RESOLV_CONF);
+        r = fopen_temporary_label(PRIVATE_STUB_RESOLV_CONF, PRIVATE_STUB_RESOLV_CONF, &f_stub, &temp_path_stub);
+        if (r < 0) {
+                log_warning_errno(r, "Failed to open new %s for writing: %m", PRIVATE_STUB_RESOLV_CONF);
                 goto fail;
         }
 
+        (void) fchmod(fileno(f_stub), 0644);
+
         r = write_stub_resolv_conf_contents(f_stub, dns, domains);
         if (r < 0) {
                 log_error_errno(r, "Failed to write new %s: %m", PRIVATE_STUB_RESOLV_CONF);
                 goto fail;
         }
 
-        if (rename(temp_path_stub, PRIVATE_STUB_RESOLV_CONF) < 0) {
-                r = log_error_errno(errno, "Failed to move new %s into place: %m", PRIVATE_STUB_RESOLV_CONF);
-                goto fail;
-        }
+        if (rename(temp_path_uplink, PRIVATE_UPLINK_RESOLV_CONF) < 0)
+                r = log_error_errno(errno, "Failed to move new %s into place: %m", PRIVATE_UPLINK_RESOLV_CONF);
 
-        return 0;
+        if (rename(temp_path_stub, PRIVATE_STUB_RESOLV_CONF) < 0)
+                r = log_error_errno(errno, "Failed to move new %s into place: %m", PRIVATE_STUB_RESOLV_CONF);
 
-fail:
-        (void) unlink(PRIVATE_UPLINK_RESOLV_CONF);
-        (void) unlink(temp_path_uplink);
-        (void) unlink(PRIVATE_STUB_RESOLV_CONF);
-        (void) unlink(temp_path_stub);
+ fail:
+        if (r < 0) {
+                /* Something went wrong, perform cleanup... */
+                (void) unlink(temp_path_uplink);
+                (void) unlink(temp_path_stub);
+        }
 
         return r;
 }