simplify executor use statements

This commit is contained in:
Ariadne Conill 2020-08-20 03:08:29 -06:00
parent 9caffc01c2
commit d463d455da
3 changed files with 20 additions and 16 deletions

View file

@ -125,11 +125,11 @@ lif_interface_file_parse(struct lif_dict *collection, const char *filename)
if (!strcmp(token, "dhcp"))
{
cur_iface->is_dhcp = true;
lif_dict_add(&cur_iface->vars, "use", strdup("dhcp"));
lif_interface_use_executor(cur_iface, "dhcp");
}
else if (!strcmp(token, "ppp"))
{
lif_dict_add(&cur_iface->vars, "use", strdup("ppp"));
lif_interface_use_executor(cur_iface, "ppp");
}
else if (!strcmp(token, "inherits"))
{
@ -176,7 +176,7 @@ lif_interface_file_parse(struct lif_dict *collection, const char *filename)
else if (!strcmp(executor, "link"))
continue;
lif_dict_add(&cur_iface->vars, token, strdup(executor));
lif_interface_use_executor(cur_iface, executor);
}
else if (!strcmp(token, "inherit"))
{
@ -214,12 +214,12 @@ lif_interface_file_parse(struct lif_dict *collection, const char *filename)
/* Check if token looks like <word1>-<word*> and assume <word1> is an addon */
char *word_end = strchr(token, '-');
if (word_end)
if (word_end != NULL)
{
/* Copy word1 to not mangle *token */
char *addon = strndup(token, word_end - token);
if (lif_dict_add_once(&cur_iface->vars, "use", addon, (lif_dict_cmp_t) strcmp) == NULL)
free(addon);
lif_interface_use_executor(cur_iface, addon);
free(addon);
/* pass requires as compatibility env vars to appropriate executors (bridge, bond) */
if (!strcmp(addon, "dhcp"))

View file

@ -63,10 +63,7 @@ lif_interface_init(struct lif_interface *interface, const char *ifname)
interface->ifname = strdup(ifname);
if (strchr(ifname, '.') == NULL)
lif_dict_add(&interface->vars, "use", strdup("link"));
else
lif_dict_add(&interface->vars, "use", strdup("vlan"));
lif_interface_use_executor(interface, strchr(ifname, '.') == NULL ? "link" : "vlan");
}
bool
@ -80,11 +77,8 @@ lif_interface_address_add(struct lif_interface *interface, const char *address)
return false;
}
if (!interface->is_static)
{
lif_dict_add(&interface->vars, "use", strdup("static"));
interface->is_static = true;
}
lif_interface_use_executor(interface, "static");
interface->is_static = true;
lif_dict_add(&interface->vars, "address", addr);
@ -137,6 +131,15 @@ lif_interface_fini(struct lif_interface *interface)
free(interface->ifname);
}
void
lif_interface_use_executor(struct lif_interface *interface, const char *executor)
{
char *exec_addon = strdup(executor);
if (lif_dict_add_once(&interface->vars, "use", exec_addon, (lif_dict_cmp_t) strcmp) == NULL)
free(exec_addon);
}
void
lif_interface_collection_init(struct lif_dict *collection)
{
@ -147,7 +150,7 @@ lif_interface_collection_init(struct lif_dict *collection)
/* always enable loopback interface as part of a collection */
if_lo = lif_interface_collection_find(collection, "lo");
if_lo->is_auto = true;
lif_dict_add(&if_lo->vars, "use", strdup("loopback"));
lif_interface_use_executor(if_lo, "loopback");
}
void

View file

@ -68,6 +68,7 @@ extern void lif_interface_init(struct lif_interface *interface, const char *ifna
extern bool lif_interface_address_add(struct lif_interface *interface, const char *address);
extern void lif_interface_address_delete(struct lif_interface *interface, const char *address);
extern void lif_interface_fini(struct lif_interface *interface);
extern void lif_interface_use_executor(struct lif_interface *interface, const char *executor);
extern void lif_interface_collection_init(struct lif_dict *collection);
extern void lif_interface_collection_fini(struct lif_dict *collection);