mirror of
https://github.com/systemd/systemd.git
synced 2024-12-17 22:23:39 +08:00
networkd: generate resolv.conf
This adds support to generate a basic resolv.conf in /run/systemd/network. This file will not take any effect unless a symlink is created from /etc/resolv.conf. Nameservers received over DHCP takes precedence over statically configured ones. Note: /etc/resolv.conf is severely limited, so in the future we will likely rather provide a much more powerfull nss plugin (or something to that effect), but this should allow current users to function without any loss of functionality.
This commit is contained in:
parent
924fe4304a
commit
3bef724f7e
@ -4107,6 +4107,7 @@ systemd_networkd_LDADD = \
|
||||
libsystemd-id128-internal.la \
|
||||
libsystemd-rtnl.la \
|
||||
libsystemd-dhcp.la \
|
||||
libsystemd-label.la \
|
||||
libsystemd-shared.la
|
||||
|
||||
nodist_systemunit_DATA += \
|
||||
@ -4133,6 +4134,7 @@ test_network_LDADD = \
|
||||
libsystemd-daemon-internal.la \
|
||||
libsystemd-rtnl.la \
|
||||
libsystemd-dhcp.la \
|
||||
libsystemd-label.la \
|
||||
libsystemd-shared.la
|
||||
|
||||
tests += \
|
||||
|
@ -190,6 +190,43 @@ int address_configure(Address *address, Link *link,
|
||||
return 0;
|
||||
}
|
||||
|
||||
int config_parse_dns(const char *unit,
|
||||
const char *filename,
|
||||
unsigned line,
|
||||
const char *section,
|
||||
unsigned section_line,
|
||||
const char *lvalue,
|
||||
int ltype,
|
||||
const char *rvalue,
|
||||
void *data,
|
||||
void *userdata) {
|
||||
Address **dns = data;
|
||||
_cleanup_address_free_ Address *n = NULL;
|
||||
int r;
|
||||
|
||||
assert(filename);
|
||||
assert(section);
|
||||
assert(lvalue);
|
||||
assert(rvalue);
|
||||
assert(data);
|
||||
|
||||
r = address_new_dynamic(&n);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
r = net_parse_inaddr(rvalue, &n->family, &n->in_addr);
|
||||
if (r < 0) {
|
||||
log_syntax(unit, LOG_ERR, filename, line, EINVAL,
|
||||
"DNS address is invalid, ignoring assignment: %s", rvalue);
|
||||
return 0;
|
||||
}
|
||||
|
||||
*dns = n;
|
||||
n = NULL;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int config_parse_address(const char *unit,
|
||||
const char *filename,
|
||||
unsigned line,
|
||||
|
@ -25,6 +25,7 @@ Network.Bridge, config_parse_bridge, 0, offsetof(Networ
|
||||
Network.DHCP, config_parse_bool, 0, offsetof(Network, dhcp)
|
||||
Network.Address, config_parse_address, 0, 0
|
||||
Network.Gateway, config_parse_gateway, 0, 0
|
||||
Network.DNS, config_parse_dns, 0, offsetof(Network, dns)
|
||||
Address.Address, config_parse_address, 0, 0
|
||||
Address.Label, config_parse_label, 0, 0
|
||||
Route.Gateway, config_parse_gateway, 0, 0
|
||||
|
@ -378,6 +378,7 @@ static void dhcp_handler(sd_dhcp_client *client, int event, void *userdata) {
|
||||
if (event == DHCP_EVENT_IP_CHANGE || event == DHCP_EVENT_IP_ACQUIRE) {
|
||||
_cleanup_address_free_ Address *addr = NULL;
|
||||
_cleanup_route_free_ Route *rt = NULL;
|
||||
struct in_addr **nameservers;
|
||||
|
||||
log_struct_link(LOG_INFO, link,
|
||||
"MESSAGE=%s: DHCPv4 address %u.%u.%u.%u/%u via %u.%u.%u.%u",
|
||||
@ -420,6 +421,13 @@ static void dhcp_handler(sd_dhcp_client *client, int event, void *userdata) {
|
||||
addr = NULL;
|
||||
rt = NULL;
|
||||
|
||||
r = sd_dhcp_client_get_dns(client, &nameservers);
|
||||
if (r >= 0) {
|
||||
r = manager_update_resolv_conf(link->manager);
|
||||
if (r < 0)
|
||||
log_error("Failed to update resolv.conf");
|
||||
}
|
||||
|
||||
link_enter_set_addresses(link);
|
||||
}
|
||||
|
||||
|
@ -19,10 +19,13 @@
|
||||
along with systemd; If not, see <http://www.gnu.org/licenses/>.
|
||||
***/
|
||||
|
||||
#include <resolv.h>
|
||||
|
||||
#include "path-util.h"
|
||||
#include "networkd.h"
|
||||
#include "libudev-private.h"
|
||||
#include "udev-util.h"
|
||||
#include "mkdir.h"
|
||||
|
||||
const char* const network_dirs[] = {
|
||||
"/etc/systemd/network",
|
||||
@ -276,3 +279,75 @@ int manager_rtnl_listen(Manager *m) {
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void append_dns(FILE *f, struct in_addr *dns, unsigned char family, unsigned *count) {
|
||||
char buf[INET6_ADDRSTRLEN];
|
||||
const char *address;
|
||||
|
||||
address = inet_ntop(family, dns, buf, INET6_ADDRSTRLEN);
|
||||
if (!address) {
|
||||
log_warning("Invalid DNS address. Ignoring.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (*count == MAXNS)
|
||||
fputs("# Too many dynamic name servers configured, the "
|
||||
"following entries will be ignored\n", f);
|
||||
|
||||
fprintf(f, "nameserver %s\n", address);
|
||||
|
||||
(*count) ++;
|
||||
}
|
||||
|
||||
int manager_update_resolv_conf(Manager *m) {
|
||||
_cleanup_free_ char *temp_path = NULL;
|
||||
_cleanup_fclose_ FILE *f = NULL;
|
||||
Link *link;
|
||||
Iterator i;
|
||||
unsigned count = 0;
|
||||
int r;
|
||||
|
||||
assert(m);
|
||||
|
||||
r = mkdir_safe_label("/run/systemd/network", 0755, 0, 0);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
r = fopen_temporary("/run/systemd/network/resolv.conf", &f, &temp_path);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
fchmod(fileno(f), 0644);
|
||||
|
||||
fputs("# This file is managed by systemd-networkd(8). Do not edit.\n", f);
|
||||
|
||||
HASHMAP_FOREACH(link, m->links, i) {
|
||||
if (link->dhcp) {
|
||||
struct in_addr **nameservers;
|
||||
|
||||
r = sd_dhcp_client_get_dns(link->dhcp, &nameservers);
|
||||
if (r >= 0) {
|
||||
unsigned j;
|
||||
|
||||
for (j = 0; nameservers[j]; j++)
|
||||
append_dns(f, nameservers[j], AF_INET, &count);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
HASHMAP_FOREACH(link, m->links, i)
|
||||
if (link->network && link->network->dns)
|
||||
append_dns(f, &link->network->dns->in_addr.in,
|
||||
link->network->dns->family, &count);
|
||||
|
||||
fflush(f);
|
||||
|
||||
if (ferror(f) || rename(temp_path, "/run/systemd/network/resolv.conf") < 0) {
|
||||
r = -errno;
|
||||
unlink("/run/systemd/network/resolv.conf");
|
||||
unlink(temp_path);
|
||||
return r;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -144,6 +144,8 @@ void network_free(Network *network) {
|
||||
|
||||
free(network->description);
|
||||
|
||||
address_free(network->dns);
|
||||
|
||||
while ((route = network->static_routes))
|
||||
route_free(route);
|
||||
|
||||
@ -197,6 +199,12 @@ int network_apply(Manager *manager, Network *network, Link *link) {
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
if (network->dns) {
|
||||
r = manager_update_resolv_conf(manager);
|
||||
if (r < 0)
|
||||
return r;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -60,6 +60,13 @@ int main(int argc, char *argv[]) {
|
||||
if (r < 0)
|
||||
goto out;
|
||||
|
||||
|
||||
/* write out empty resolv.conf to avoid a
|
||||
* dangling symlink */
|
||||
r = manager_update_resolv_conf(m);
|
||||
if (r < 0)
|
||||
goto out;
|
||||
|
||||
sd_notify(false,
|
||||
"READY=1\n"
|
||||
"STATUS=Processing requests...");
|
||||
|
@ -88,6 +88,7 @@ struct Network {
|
||||
|
||||
LIST_HEAD(Address, static_addresses);
|
||||
LIST_HEAD(Route, static_routes);
|
||||
Address *dns;
|
||||
|
||||
Hashmap *addresses_by_section;
|
||||
Hashmap *routes_by_section;
|
||||
@ -157,6 +158,7 @@ struct Link {
|
||||
|
||||
Route *dhcp_route;
|
||||
Address *dhcp_address;
|
||||
Address *dns;
|
||||
|
||||
LinkState state;
|
||||
|
||||
@ -195,6 +197,8 @@ int manager_udev_listen(Manager *m);
|
||||
|
||||
int manager_rtnl_listen(Manager *m);
|
||||
|
||||
int manager_update_resolv_conf(Manager *m);
|
||||
|
||||
DEFINE_TRIVIAL_CLEANUP_FUNC(Manager*, manager_free);
|
||||
#define _cleanup_manager_free_ _cleanup_(manager_freep)
|
||||
|
||||
@ -258,6 +262,10 @@ int address_drop(Address *address, Link *link, sd_rtnl_message_handler_t callbac
|
||||
DEFINE_TRIVIAL_CLEANUP_FUNC(Address*, address_free);
|
||||
#define _cleanup_address_free_ _cleanup_(address_freep)
|
||||
|
||||
int config_parse_dns(const char *unit, const char *filename, unsigned line,
|
||||
const char *section, unsigned section_line, const char *lvalue,
|
||||
int ltype, const char *rvalue, void *data, void *userdata);
|
||||
|
||||
int config_parse_address(const char *unit, const char *filename, unsigned line,
|
||||
const char *section, unsigned section_line, const char *lvalue,
|
||||
int ltype, const char *rvalue, void *data, void *userdata);
|
||||
|
Loading…
Reference in New Issue
Block a user