From: Michael Tremer Date: Fri, 14 Apr 2023 11:57:48 +0000 (+0000) Subject: networkd: ports: Keep a permanent reference to links X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=02801f0d507924a4efb8c462f6fc775ab5793ebc;p=network.git networkd: ports: Keep a permanent reference to links Signed-off-by: Michael Tremer --- diff --git a/src/networkd/port.c b/src/networkd/port.c index c6c8781d..3c4f0b3e 100644 --- a/src/networkd/port.c +++ b/src/networkd/port.c @@ -36,6 +36,9 @@ struct nw_port { nw_daemon* daemon; int nrefs; + // Link + nw_link* link; + char name[IF_NAMESIZE]; nw_port_type_t type; @@ -66,6 +69,8 @@ static nw_port_type_t nw_port_type_from_string(const char* s) { } static void nw_port_free(nw_port* port) { + if (port->link) + nw_link_unref(port->link); if (port->config) nw_config_unref(port->config); if (port->daemon) @@ -134,6 +139,14 @@ static int nw_port_setup(nw_port* port) { char path[PATH_MAX]; int r; + // Find the link + port->link = nw_daemon_get_link_by_name(port->daemon, port->name); + if (port->link) { + DEBUG("%s: Found matching link %d\n", port->name, nw_link_ifindex(port->link)); + } else { + DEBUG("%s: Could not find matching link\n", port->name); + } + // Compose the path to the main configuration file r = nw_path_join(path, PORT_CONFIG_DIR, port->name); if (r) @@ -240,7 +253,10 @@ char* nw_port_bus_path(nw_port* port) { } static nw_link* nw_port_get_link(nw_port* port) { - return nw_daemon_get_link_by_name(port->daemon, port->name); + if (!port->link) + return NULL; + + return nw_link_ref(port->link); } const nw_address_t* nw_port_get_address(nw_port* port) { @@ -248,14 +264,8 @@ const nw_address_t* nw_port_get_address(nw_port* port) { } int nw_port_has_carrier(nw_port* port) { - int has_carrier = 0; - - // Fetch link - nw_link* link = nw_port_get_link(port); - if (link) { - has_carrier = nw_link_has_carrier(link); - nw_link_unref(link); - } + if (!port->link) + return 0; - return has_carrier; + return nw_link_has_carrier(port->link); }