return 0;
}
-int rtnl_set_link_properties(sd_rtnl *rtnl, int ifindex, const char *alias,
+int rtnl_set_link_properties(sd_rtnl **rtnl, int ifindex, const char *alias,
const struct ether_addr *mac, unsigned mtu) {
_cleanup_rtnl_message_unref_ sd_rtnl_message *message = NULL;
- bool need_update = false;
int r;
assert(rtnl);
if (!alias && !mac && mtu == 0)
return 0;
- r = sd_rtnl_message_new_link(rtnl, &message, RTM_SETLINK, ifindex);
+ if (!*rtnl) {
+ r = sd_rtnl_open(rtnl, 0);
+ if (r < 0)
+ return r;
+ }
+
+ r = sd_rtnl_message_new_link(*rtnl, &message, RTM_SETLINK, ifindex);
if (r < 0)
return r;
r = sd_rtnl_message_append_string(message, IFLA_IFALIAS, alias);
if (r < 0)
return r;
-
- need_update = true;
-
}
if (mac) {
r = sd_rtnl_message_append_ether_addr(message, IFLA_ADDRESS, mac);
if (r < 0)
return r;
-
- need_update = true;
}
if (mtu > 0) {
r = sd_rtnl_message_append_u32(message, IFLA_MTU, mtu);
if (r < 0)
return r;
-
- need_update = true;
}
- if (need_update) {
- r = sd_rtnl_call(rtnl, message, 0, NULL);
- if (r < 0)
- return r;
- }
+ r = sd_rtnl_call(*rtnl, message, 0, NULL);
+ if (r < 0)
+ return r;
return 0;
}
bool rtnl_message_type_is_route(uint16_t type);
int rtnl_set_link_name(sd_rtnl **rtnl, int ifindex, const char *name);
-int rtnl_set_link_properties(sd_rtnl *rtnl, int ifindex, const char *alias, const struct ether_addr *mac, unsigned mtu);
+int rtnl_set_link_properties(sd_rtnl **rtnl, int ifindex, const char *alias, const struct ether_addr *mac, unsigned mtu);
int rtnl_log_parse_error(int r);
int rtnl_log_create_error(int r);
return 0;
}
-int ethtool_get_driver(int fd, const char *ifname, char **ret) {
+int ethtool_get_driver(int *fd, const char *ifname, char **ret) {
struct ethtool_drvinfo ecmd = {
.cmd = ETHTOOL_GDRVINFO
};
char *d;
int r;
+ if (*fd < 0) {
+ r = ethtool_connect(fd);
+ if (r < 0) {
+ log_warning("link_config: could not connect to ethtool: %s", strerror(-r));
+ return r;
+ }
+ }
+
strscpy(ifr.ifr_name, IFNAMSIZ, ifname);
- r = ioctl(fd, SIOCETHTOOL, &ifr);
+ r = ioctl(*fd, SIOCETHTOOL, &ifr);
if (r < 0)
return -errno;
return 0;
}
-int ethtool_set_speed(int fd, const char *ifname, unsigned int speed, Duplex duplex)
+int ethtool_set_speed(int *fd, const char *ifname, unsigned int speed, Duplex duplex)
{
struct ethtool_cmd ecmd = {
.cmd = ETHTOOL_GSET
if (speed == 0 && duplex == _DUP_INVALID)
return 0;
+ if (*fd < 0) {
+ r = ethtool_connect(fd);
+ if (r < 0) {
+ log_warning("link_config: could not connect to ethtool: %s", strerror(-r));
+ return r;
+ }
+ }
+
strscpy(ifr.ifr_name, IFNAMSIZ, ifname);
- r = ioctl(fd, SIOCETHTOOL, &ifr);
+ r = ioctl(*fd, SIOCETHTOOL, &ifr);
if (r < 0)
return -errno;
if (need_update) {
ecmd.cmd = ETHTOOL_SSET;
- r = ioctl(fd, SIOCETHTOOL, &ifr);
+ r = ioctl(*fd, SIOCETHTOOL, &ifr);
if (r < 0)
return -errno;
}
return 0;
}
-int ethtool_set_wol(int fd, const char *ifname, WakeOnLan wol) {
+int ethtool_set_wol(int *fd, const char *ifname, WakeOnLan wol) {
struct ethtool_wolinfo ecmd = {
.cmd = ETHTOOL_GWOL
};
if (wol == _WOL_INVALID)
return 0;
+ if (*fd < 0) {
+ r = ethtool_connect(fd);
+ if (r < 0) {
+ log_warning("link_config: could not connect to ethtool: %s", strerror(-r));
+ return r;
+ }
+ }
+
strscpy(ifr.ifr_name, IFNAMSIZ, ifname);
- r = ioctl(fd, SIOCETHTOOL, &ifr);
+ r = ioctl(*fd, SIOCETHTOOL, &ifr);
if (r < 0)
return -errno;
if (need_update) {
ecmd.cmd = ETHTOOL_SWOL;
- r = ioctl(fd, SIOCETHTOOL, &ifr);
+ r = ioctl(*fd, SIOCETHTOOL, &ifr);
if (r < 0)
return -errno;
}
int ethtool_connect(int *ret);
-int ethtool_get_driver(int fd, const char *ifname, char **ret);
-int ethtool_set_speed(int fd, const char *ifname, unsigned int speed, Duplex duplex);
-int ethtool_set_wol(int fd, const char *ifname, WakeOnLan wol);
+int ethtool_get_driver(int *fd, const char *ifname, char **ret);
+int ethtool_set_speed(int *fd, const char *ifname, unsigned int speed, Duplex duplex);
+int ethtool_set_wol(int *fd, const char *ifname, WakeOnLan wol);
const char *duplex_to_string(Duplex d) _const_;
Duplex duplex_from_string(const char *d) _pure_;
return 0;
}
-static int link_config_ctx_connect(link_config_ctx *ctx) {
- int r;
-
- if (ctx->ethtool_fd == -1) {
- r = ethtool_connect(&ctx->ethtool_fd);
- if (r < 0) {
- log_warning("link_config: could not connect to ethtool: %s",
- strerror(-r));
- return r;
- }
- }
-
- if (!ctx->rtnl) {
- r = sd_rtnl_open(&ctx->rtnl, 0);
- if (r < 0) {
- log_warning("link_config: could not connect to rtnl: %s",
- strerror(-r));
- return r;
- }
- }
-
- return 0;
-}
-
static void link_configs_free(link_config_ctx *ctx) {
link_config *link, *link_next;
assert(device);
assert(name);
- r = link_config_ctx_connect(ctx);
- if (r < 0)
- return r;
-
old_name = udev_device_get_sysname(device);
if (!old_name)
return -EINVAL;
- r = ethtool_set_speed(ctx->ethtool_fd, old_name, config->speed / 1024,
+ r = ethtool_set_speed(&ctx->ethtool_fd, old_name, config->speed / 1024,
config->duplex);
if (r < 0)
log_warning("Could not set speed or duplex of %s to %u Mbps (%s): %s",
old_name, config->speed / 1024,
duplex_to_string(config->duplex), strerror(-r));
- r = ethtool_set_wol(ctx->ethtool_fd, old_name, config->wol);
+ r = ethtool_set_wol(&ctx->ethtool_fd, old_name, config->wol);
if (r < 0)
log_warning("Could not set WakeOnLan of %s to %s: %s",
old_name, wol_to_string(config->wol), strerror(-r));
mac = config->mac;
}
- r = rtnl_set_link_properties(ctx->rtnl, ifindex, config->alias, mac,
+ r = rtnl_set_link_properties(&ctx->rtnl, ifindex, config->alias, mac,
config->mtu);
if (r < 0) {
log_warning("Could not set Alias, MACAddress or MTU on %s: %s",
char *driver;
int r;
- r = link_config_ctx_connect(ctx);
- if (r < 0)
- return r;
-
name = udev_device_get_sysname(device);
if (!name)
return -EINVAL;
- r = ethtool_get_driver(ctx->ethtool_fd, name, &driver);
+ r = ethtool_get_driver(&ctx->ethtool_fd, name, &driver);
if (r < 0)
return r;