DnsServer *manager_get_dns_server(Manager *m) { Link *l; assert(m); /* Try to read updates resolv.conf */ manager_read_resolv_conf(m); /* If no DNS server was chosen so far, pick the first one */ if (!m->current_dns_server) manager_set_dns_server(m, m->dns_servers); if (!m->current_dns_server) { bool found = false; Iterator i; /* No DNS servers configured, let's see if there are * any on any links. If not, we use the fallback * servers */ HASHMAP_FOREACH(l, m->links, i) if (l->dns_servers) { found = true; break; } if (!found) manager_set_dns_server(m, m->fallback_dns_servers); }
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; int r; assert(m); /* Read the system /etc/resolv.conf first */ (void) manager_read_resolv_conf(m); /* Add the full list to a set, to filter out duplicates */ r = manager_compile_dns_servers(m, &dns); if (r < 0) return log_warning_errno(r, "Failed to compile list of DNS servers: %m"); r = manager_compile_search_domains(m, &domains, false); 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); if (r < 0) return log_warning_errno(r, "Failed to open private resolv.conf file for writing: %m"); (void) fchmod(fileno(f), 0644); r = write_resolv_conf_contents(f, 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) { r = log_error_errno(errno, "Failed to move private resolv.conf file into place: %m"); goto fail; } return 0; fail: (void) unlink(PRIVATE_RESOLV_CONF); (void) unlink(temp_path); return r; }