Skip to content

Commit

Permalink
wait for systemd tun device unit to be active before configuring dns (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
scareything committed Apr 29, 2024
1 parent 90b416d commit 94b479f
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 7 deletions.
55 changes: 50 additions & 5 deletions programs/ziti-edge-tunnel/netif_driver/linux/resolvers.c
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,53 @@ static bool set_systemd_resolved_link_setting(sd_bus *bus, const char *tun, cons
return true;
}

bool try_libsystemd_resolver(void) {
// wait for systemd to recognize the tun device before configuring, lest the configuration get overwritten
static bool wait_for_tun(const char *name, sd_bus *bus, unsigned int timeout_ms) {
const unsigned int delay_ms = 250;
char systemd_path[128];
unsigned int iterations = timeout_ms / delay_ms;
bool active = false;
snprintf(systemd_path, sizeof(systemd_path), "/org/freedesktop/systemd1/unit/sys_2dsubsystem_2dnet_2ddevices_2d%s_2edevice", name);

ZITI_LOG(DEBUG, "waiting %d ms for systemd path '%s' to become active", timeout_ms, systemd_path);

for (int count = 0; count < iterations && active == false; count++, uv_sleep(delay_ms)) {
sd_bus_message *message = NULL;

int r = sd_bus_get_property_f(
bus,
"org.freedesktop.systemd1",
systemd_path,
"org.freedesktop.systemd1.Unit",
"ActiveState",
NULL,
&message,
"s"
);
if (r < 0) {
ZITI_LOG(VERBOSE, "failed to get ActiveState property: %s", strerror(-r));
continue;
}

const char *state = NULL;
r = sd_bus_message_read_f(message, "s", &state);
if (r < 0) {
ZITI_LOG(VERBOSE, "failed to read property: %s", strerror(-r));
} else {
if (state) {
ZITI_LOG(DEBUG, "device state (c=%d): %s", count, state);
if (strcmp(state, "active") == 0) {
active = true;
}
}
}
sd_bus_message_unref_f(message);
}

return false;
}

bool try_libsystemd_resolver(const char *tun_name) {
uv_once(&guard, init_libsystemd);
if (!libsystemd_dl_success) {
return false;
Expand All @@ -347,15 +393,14 @@ bool try_libsystemd_resolver(void) {
r = sd_bus_open_system_f(&bus);
if ((r >= 0) && (sd_bus_is_bus_client_f(bus) > 0)) {
ZITI_LOG(DEBUG, "Connected to system DBus");
wait_for_tun(tun_name, bus, 3000);
r = sd_bus_is_acquired_name(bus, RESOLVED_DBUS_NAME);
if (r != 0) {
ZITI_LOG(WARN, "libsystemd resolver unsuccessful. Falling back to legacy resolvers");
return false;
}
if (r == 0) {
ZITI_LOG(INFO, "systemd-resolved selected as DNS resolver manager");
return true;
}
ZITI_LOG(INFO, "systemd-resolved selected as DNS resolver manager");
return true;
} else {
ZITI_LOG(DEBUG, "Could not create system DBus client");
}
Expand Down
2 changes: 1 addition & 1 deletion programs/ziti-edge-tunnel/netif_driver/linux/resolvers.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
#endif

#ifndef EXCLUDE_LIBSYSTEMD_RESOLVER
bool try_libsystemd_resolver(void);
bool try_libsystemd_resolver(const char *tun_name);
#endif
bool is_systemd_resolved_primary_resolver(void);
bool is_resolvconf_systemd_resolved(void);
Expand Down
2 changes: 1 addition & 1 deletion programs/ziti-edge-tunnel/netif_driver/linux/tun.c
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ static void dns_update_systemd_resolve(const char* tun, unsigned int ifindex, co

static void find_dns_updater() {
#ifndef EXCLUDE_LIBSYSTEMD_RESOLVER
if(try_libsystemd_resolver()) {
if(try_libsystemd_resolver(dns_maintainer.tun_name)) {
dns_updater = dns_update_systemd_resolved;
return;
}
Expand Down

0 comments on commit 94b479f

Please sign in to comment.