resolved: create private stub resolve file for /etc/resolv.conf integration (#7014)
This creates a second private resolve.conf file which lists the stub resolver and the resolved acquired search domains. This runtime file should be used as a symlink target for /etc/resolv.conf such that non-nss based applications can resolve search domains. Fixes: #7009
This commit is contained in:
committed by
Lennart Poettering
parent
ceeb433c39
commit
e6b2d948f8
@@ -61,7 +61,13 @@ int manager_read_resolv_conf(Manager *m) {
|
||||
return 0;
|
||||
|
||||
/* Is it symlinked to our own file? */
|
||||
if (stat(PRIVATE_RESOLV_CONF, &own) >= 0 &&
|
||||
if (stat(PRIVATE_UPLINK_RESOLV_CONF, &own) >= 0 &&
|
||||
st.st_dev == own.st_dev &&
|
||||
st.st_ino == own.st_ino)
|
||||
return 0;
|
||||
|
||||
/* Is it symlinked to our own stub file? */
|
||||
if (stat(PRIVATE_STUB_RESOLV_CONF, &own) >= 0 &&
|
||||
st.st_dev == own.st_dev &&
|
||||
st.st_ino == own.st_ino)
|
||||
return 0;
|
||||
@@ -206,7 +212,7 @@ static void write_resolv_conf_search(
|
||||
fputs_unlocked("\n", f);
|
||||
}
|
||||
|
||||
static int write_resolv_conf_contents(FILE *f, OrderedSet *dns, OrderedSet *domains) {
|
||||
static int write_uplink_resolv_conf_contents(FILE *f, OrderedSet *dns, OrderedSet *domains) {
|
||||
Iterator i;
|
||||
|
||||
fputs_unlocked("# This file is managed by man:systemd-resolved(8). Do not edit.\n#\n"
|
||||
@@ -234,11 +240,25 @@ static int write_resolv_conf_contents(FILE *f, OrderedSet *dns, OrderedSet *doma
|
||||
return fflush_and_check(f);
|
||||
}
|
||||
|
||||
static int write_stub_resolv_conf_contents(FILE *f, OrderedSet *dns, OrderedSet *domains) {
|
||||
fputs("# This file is managed by man:systemd-resolved(8). Do not edit.\n#\n"
|
||||
"# 127.0.0.53 is the systemd-resolved stub resolver.\n"
|
||||
"# run \"systemd-resolve --status\" to see details about the actual nameservers.\n"
|
||||
"nameserver 127.0.0.53\n\n", f);
|
||||
|
||||
if (!ordered_set_isempty(domains))
|
||||
write_resolv_conf_search(domains, f);
|
||||
|
||||
return fflush_and_check(f);
|
||||
}
|
||||
|
||||
int manager_write_resolv_conf(Manager *m) {
|
||||
|
||||
_cleanup_ordered_set_free_ OrderedSet *dns = NULL, *domains = NULL;
|
||||
_cleanup_free_ char *temp_path = NULL;
|
||||
_cleanup_fclose_ FILE *f = NULL;
|
||||
_cleanup_free_ char *temp_path_uplink = NULL;
|
||||
_cleanup_free_ char *temp_path_stub = NULL;
|
||||
_cleanup_fclose_ FILE *f_uplink = NULL;
|
||||
_cleanup_fclose_ FILE *f_stub = NULL;
|
||||
int r;
|
||||
|
||||
assert(m);
|
||||
@@ -255,28 +275,44 @@ int manager_write_resolv_conf(Manager *m) {
|
||||
if (r < 0)
|
||||
return log_warning_errno(r, "Failed to compile list of search domains: %m");
|
||||
|
||||
r = fopen_temporary_label(PRIVATE_RESOLV_CONF, PRIVATE_RESOLV_CONF, &f, &temp_path);
|
||||
r = fopen_temporary_label(PRIVATE_UPLINK_RESOLV_CONF, PRIVATE_UPLINK_RESOLV_CONF, &f_uplink, &temp_path_uplink);
|
||||
if (r < 0)
|
||||
return log_warning_errno(r, "Failed to open private resolv.conf file for writing: %m");
|
||||
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 private stub-resolv.conf file for writing: %m");
|
||||
(void) fchmod(fileno(f_uplink), 0644);
|
||||
(void) fchmod(fileno(f_stub), 0644);
|
||||
|
||||
(void) fchmod(fileno(f), 0644);
|
||||
|
||||
r = write_resolv_conf_contents(f, dns, domains);
|
||||
r = write_uplink_resolv_conf_contents(f_uplink, dns, domains);
|
||||
if (r < 0) {
|
||||
log_error_errno(r, "Failed to write private resolv.conf contents: %m");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
if (rename(temp_path, PRIVATE_RESOLV_CONF) < 0) {
|
||||
if (rename(temp_path_uplink, PRIVATE_UPLINK_RESOLV_CONF) < 0) {
|
||||
r = log_error_errno(errno, "Failed to move private resolv.conf file into place: %m");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
r = write_stub_resolv_conf_contents(f_stub, dns, domains);
|
||||
if (r < 0) {
|
||||
log_error_errno(r, "Failed to write private stub-resolv.conf contents: %m");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
if (rename(temp_path_stub, PRIVATE_STUB_RESOLV_CONF) < 0) {
|
||||
r = log_error_errno(errno, "Failed to move private stub-resolv.conf file into place: %m");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
fail:
|
||||
(void) unlink(PRIVATE_RESOLV_CONF);
|
||||
(void) unlink(temp_path);
|
||||
(void) unlink(PRIVATE_UPLINK_RESOLV_CONF);
|
||||
(void) unlink(temp_path_uplink);
|
||||
(void) unlink(PRIVATE_STUB_RESOLV_CONF);
|
||||
(void) unlink(temp_path_stub);
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
@@ -21,7 +21,8 @@
|
||||
|
||||
#include "resolved-manager.h"
|
||||
|
||||
#define PRIVATE_RESOLV_CONF "/run/systemd/resolve/resolv.conf"
|
||||
#define PRIVATE_UPLINK_RESOLV_CONF "/run/systemd/resolve/resolv.conf"
|
||||
#define PRIVATE_STUB_RESOLV_CONF "/run/systemd/resolve/stub-resolv.conf"
|
||||
|
||||
int manager_read_resolv_conf(Manager *m);
|
||||
int manager_write_resolv_conf(Manager *m);
|
||||
|
||||
@@ -117,10 +117,6 @@ int main(int argc, char *argv[]) {
|
||||
sd_event_get_exit_code(m->event, &r);
|
||||
|
||||
finish:
|
||||
/* systemd-nspawn checks for private resolv.conf to decide whether
|
||||
or not to mount it into the container. So just delete it. */
|
||||
(void) unlink(PRIVATE_RESOLV_CONF);
|
||||
|
||||
sd_notify(false,
|
||||
"STOPPING=1\n"
|
||||
"STATUS=Shutting down...");
|
||||
|
||||
Reference in New Issue
Block a user