confile: cleanup get_network_config_ops()

parent d395fe61
...@@ -4690,29 +4690,24 @@ static struct lxc_config_t *get_network_config_ops(const char *key, ...@@ -4690,29 +4690,24 @@ static struct lxc_config_t *get_network_config_ops(const char *key,
ssize_t *idx, ssize_t *idx,
char **deindexed_key) char **deindexed_key)
{ {
__do_free char *copy = NULL;
struct lxc_config_t *config = NULL;
int ret; int ret;
unsigned int tmpidx; unsigned int tmpidx;
size_t numstrlen; size_t numstrlen;
char *copy, *idx_start, *idx_end; char *idx_start, *idx_end;
struct lxc_config_t *config = NULL;
/* check that this is a sensible network key */ /* check that this is a sensible network key */
if (strncmp("lxc.net.", key, 8)) { if (strncmp("lxc.net.", key, 8))
ERROR("Invalid network configuration key \"%s\"", key); return log_error_errno(NULL, EINVAL, "Invalid network configuration key \"%s\"", key);
return NULL;
}
copy = strdup(key); copy = strdup(key);
if (!copy) { if (!copy)
ERROR("Failed to duplicate string \"%s\"", key); return log_error_errno(NULL, ENOMEM, "Failed to duplicate string \"%s\"", key);
return NULL;
}
/* lxc.net.<n> */ /* lxc.net.<n> */
if (!isdigit(*(key + 8))) { if (!isdigit(*(key + 8)))
ERROR("Failed to detect digit in string \"%s\"", key + 8); return log_error_errno(NULL, EINVAL, "Failed to detect digit in string \"%s\"", key + 8);
goto on_error;
}
/* beginning of index string */ /* beginning of index string */
idx_start = (copy + 7); idx_start = (copy + 7);
...@@ -4726,22 +4721,16 @@ static struct lxc_config_t *get_network_config_ops(const char *key, ...@@ -4726,22 +4721,16 @@ static struct lxc_config_t *get_network_config_ops(const char *key,
/* parse current index */ /* parse current index */
ret = lxc_safe_uint((idx_start + 1), &tmpidx); ret = lxc_safe_uint((idx_start + 1), &tmpidx);
if (ret < 0) { if (ret < 0) {
errno = -ret;
SYSERROR("Failed to parse unsigned integer from string \"%s\"",
idx_start + 1);
*idx = ret; *idx = ret;
goto on_error; return log_error_errno(NULL, -ret, "Failed to parse unsigned integer from string \"%s\"", idx_start + 1);
} }
/* This, of course is utterly nonsensical on so many levels, but /* This, of course is utterly nonsensical on so many levels, but
* better safe than sorry. * better safe than sorry.
* (Checking for INT_MAX here is intentional.) * (Checking for INT_MAX here is intentional.)
*/ */
if (tmpidx == INT_MAX) { if (tmpidx == INT_MAX)
SYSERROR("Number of configured networks would overflow the " return log_error_errno(NULL, ERANGE, "Number of configured networks would overflow the counter");
"counter");
goto on_error;
}
*idx = tmpidx; *idx = tmpidx;
numstrlen = strlen((idx_start + 1)); numstrlen = strlen((idx_start + 1));
...@@ -4752,29 +4741,21 @@ static struct lxc_config_t *get_network_config_ops(const char *key, ...@@ -4752,29 +4741,21 @@ static struct lxc_config_t *get_network_config_ops(const char *key,
/* lxc.net.<idx>.<subkey> */ /* lxc.net.<idx>.<subkey> */
if (idx_end) { if (idx_end) {
*idx_end = '.'; *idx_end = '.';
if (strlen(idx_end + 1) == 0) { if (strlen(idx_end + 1) == 0)
ERROR("No subkey in network configuration key \"%s\"", key); return log_error_errno(NULL, EINVAL, "No subkey in network configuration key \"%s\"", key);
goto on_error;
}
memmove(copy + 8, idx_end + 1, strlen(idx_end + 1)); memmove(copy + 8, idx_end + 1, strlen(idx_end + 1));
copy[strlen(key) - (numstrlen + 1)] = '\0'; copy[strlen(key) - (numstrlen + 1)] = '\0';
config = lxc_get_config(copy); config = lxc_get_config(copy);
if (!config) { if (!config)
ERROR("Unknown network configuration key \"%s\"", key); return log_error_errno(NULL, ENOENT, "Unknown network configuration key \"%s\"", key);
goto on_error;
}
} }
if (deindexed_key) if (deindexed_key)
*deindexed_key = copy; *deindexed_key = move_ptr(copy);
return config; return config;
on_error:
free(copy);
return NULL;
} }
/* Config entry is something like "lxc.net.0.ipv4" the key 'lxc.net.' was /* Config entry is something like "lxc.net.0.ipv4" the key 'lxc.net.' was
...@@ -4784,34 +4765,28 @@ on_error: ...@@ -4784,34 +4765,28 @@ on_error:
static int set_config_net_nic(const char *key, const char *value, static int set_config_net_nic(const char *key, const char *value,
struct lxc_conf *lxc_conf, void *data) struct lxc_conf *lxc_conf, void *data)
{ {
int ret; __do_free char *deindexed_key = NULL;
ssize_t idx = -1;
const char *idxstring; const char *idxstring;
struct lxc_config_t *config; struct lxc_config_t *config;
struct lxc_netdev *netdev; struct lxc_netdev *netdev;
ssize_t idx = -1;
char *deindexed_key = NULL;
idxstring = key + 8; idxstring = key + 8;
if (!isdigit(*idxstring)) if (!isdigit(*idxstring))
return -1; return ret_errno(EINVAL);
if (lxc_config_value_empty(value)) if (lxc_config_value_empty(value))
return clr_config_net_nic(key, lxc_conf, data); return clr_config_net_nic(key, lxc_conf, data);
config = get_network_config_ops(key, lxc_conf, &idx, &deindexed_key); config = get_network_config_ops(key, lxc_conf, &idx, &deindexed_key);
if (!config || idx < 0) if (!config || idx < 0)
return -1; return -errno;
netdev = lxc_get_netdev_by_idx(lxc_conf, (unsigned int)idx, true); netdev = lxc_get_netdev_by_idx(lxc_conf, (unsigned int)idx, true);
if (!netdev) { if (!netdev)
free(deindexed_key); return ret_errno(EINVAL);
return -1;
}
ret = config->set(deindexed_key, value, lxc_conf, netdev);
free(deindexed_key);
return ret; return config->set(deindexed_key, value, lxc_conf, netdev);
} }
static int clr_config_net_nic(const char *key, struct lxc_conf *lxc_conf, static int clr_config_net_nic(const char *key, struct lxc_conf *lxc_conf,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment