resolved-manager.c revision 987d561fe202568c0a2880cf047c7833fd365c7a
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer This file is part of systemd.
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer Copyright 2014 Tom Gundersen <teg@jklm.no>
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer systemd is free software; you can redistribute it and/or modify it
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer under the terms of the GNU Lesser General Public License as published by
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer the Free Software Foundation; either version 2.1 of the License, or
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer (at your option) any later version.
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer systemd is distributed in the hope that it will be useful, but
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer WITHOUT ANY WARRANTY; without even the implied warranty of
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer Lesser General Public License for more details.
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer You should have received a copy of the GNU Lesser General Public License
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer along with systemd; If not, see <http://www.gnu.org/licenses/>.
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic int set_fallback_dns(Manager *m, const char *string) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer FOREACH_WORD_QUOTED(word, length, string, state) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = net_parse_inaddr(addrstr, &address->family, &address->in_addr);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_debug("Ignoring invalid DNS address '%s'", addrstr);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer LIST_FIND_TAIL(addresses, m->fallback_dns, tail);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer LIST_INSERT_AFTER(addresses, m->fallback_dns, tail, address);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer const char *unit,
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer LIST_REMOVE(addresses, m->fallback_dns, address);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic int manager_parse_config_file(Manager *m) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = config_parse(NULL, "/etc/systemd/resolved.conf", NULL,
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer "Resolve\0", config_item_perf_lookup, (void*) resolved_gperf_lookup,
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer false, false, m);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_warning("Failed to parse configuration file: %s", strerror(-r));
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer sd_event_add_signal(m->event, NULL, SIGTERM, NULL, NULL);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer sd_event_add_signal(m->event, NULL, SIGINT, NULL, NULL);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer LIST_REMOVE(addresses, m->fallback_dns, address);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic void append_dns(FILE *f, void *dns, unsigned char family, unsigned *count) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer address = inet_ntop(family, dns, buf, INET6_ADDRSTRLEN);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer fputs("# Too many DNS servers configured, the following entries "
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer "may be ignored\n", f);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer const char *path = "/run/systemd/resolve/resolv.conf";
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer unsigned count = 0;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer int n, r, i;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer fputs("# This file is managed by systemd-resolved(8). Do not edit.\n#\n"
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer "# Third party programs must not access this file directly, but\n"
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer "# only through the symlink at /etc/resolv.conf. To manage\n"
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer "# resolv.conf(5) in a different way, replace the symlink by a\n"
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer "# static file or a different symlink.\n\n", f);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer for (i = 0; i < n; i++) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer _cleanup_dhcp_lease_unref_ sd_dhcp_lease *lease = NULL;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r > 0) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = sd_network_get_dhcp_lease(indices[i], &lease);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r >= 0) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = sd_dhcp_lease_get_dns(lease, &nameservers, &nameservers_size);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r >= 0) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer for (j = 0; j < nameservers_size; j++)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer append_dns(f, &nameservers[j], AF_INET, &count);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = sd_network_get_dns(indices[i], &nameservers, &nameservers_size);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r >= 0) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer for (j = 0; j < nameservers_size; j++)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer append_dns(f, &nameservers[j], AF_INET, &count);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = sd_network_get_dns6(indices[i], &nameservers6, &nameservers_size);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r >= 0) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer for (j = 0; j < nameservers_size; j++)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer append_dns(f, &nameservers6[j], AF_INET6, &count);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer LIST_FOREACH(addresses, address, m->fallback_dns)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer append_dns(f, &address->in_addr, address->family, &count);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (ferror(f) || rename(temp_path, path) < 0) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic int manager_network_event_handler(sd_event_source *s, int fd, uint32_t revents,
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_warning("Could not update resolv.conf: %s", strerror(-r));
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerint manager_network_monitor_listen(Manager *m) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer _cleanup_event_source_unref_ sd_event_source *event_source = NULL;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer _cleanup_network_monitor_unref_ sd_network_monitor *monitor = NULL;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer events = sd_network_monitor_get_events(monitor);