From d463d455da3a7360f39e8ff60d0737ca61c04996 Mon Sep 17 00:00:00 2001 From: Ariadne Conill Date: Thu, 20 Aug 2020 03:08:29 -0600 Subject: [PATCH] simplify executor `use` statements --- libifupdown/interface-file.c | 12 ++++++------ libifupdown/interface.c | 23 +++++++++++++---------- libifupdown/interface.h | 1 + 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/libifupdown/interface-file.c b/libifupdown/interface-file.c index 674cecd..bd6e0ba 100644 --- a/libifupdown/interface-file.c +++ b/libifupdown/interface-file.c @@ -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 - and assume 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")) diff --git a/libifupdown/interface.c b/libifupdown/interface.c index 8841c13..3f3bb69 100644 --- a/libifupdown/interface.c +++ b/libifupdown/interface.c @@ -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 diff --git a/libifupdown/interface.h b/libifupdown/interface.h index 045f848..8b808b3 100644 --- a/libifupdown/interface.h +++ b/libifupdown/interface.h @@ -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);