[lxc-devel] [PATCH 4/4] factor out common config evaluating code

Michael Tokarev mjt at tls.msk.ru
Tue Nov 17 23:07:33 UTC 2009


in confile.c we currently have a ton of functions each doing
the same thing.  Clean them up by providing common routines
to do the main work.

Signed-off-by: Michael Tokarev <mjt at tls.msk.ru>
---
 src/lxc/confile.c |  139 +++++++++++++---------------------------------------
 1 files changed, 35 insertions(+), 104 deletions(-)

diff --git a/src/lxc/confile.c b/src/lxc/confile.c
index 600dc49..e3e444e 100644
--- a/src/lxc/confile.c
+++ b/src/lxc/confile.c
@@ -148,158 +148,98 @@ static int config_ip_prefix(struct in_addr *addr)
 	return 0;
 }
 
-static int config_network_flags(const char *key, char *value, struct lxc_conf *lxc_conf)
+static struct lxc_netdev *
+network_netdev(const char *key, char *value, struct lxc_conf *lxc_conf)
 {
 	struct lxc_list *network = &lxc_conf->network;
 	struct lxc_netdev *netdev;
 
 	if (lxc_list_empty(network)) {
 		ERROR("network is not created for '%s' option", value);
-		return -1;
+		return NULL;
 	}
 
 	netdev = lxc_list_first_elem(network);
 	if (!netdev) {
 		ERROR("no network defined for '%s' option", value);
-		return -1;
+		return NULL;
 	}
 
-	netdev->flags |= IFF_UP;
-	return 0;
+	return netdev;
 }
 
-static int config_network_link(const char *key, char *value, struct lxc_conf *lxc_conf)
+static int network_ifname(char **valuep, char *value)
 {
-	struct lxc_list *network = &lxc_conf->network;
-	struct lxc_netdev *netdev;
-
-	if (lxc_list_empty(network)) {
-		ERROR("network is not created for %s", value);
-		return -1;
-	}
-
-	netdev = lxc_list_first_elem(network);
-	if (!netdev) {
-		ERROR("no network defined for %s", value);
-		return -1;
-	}
-
 	if (strlen(value) > IFNAMSIZ) {
 		ERROR("invalid interface name: %s", value);
 		return -1;
 	}
 
-	netdev->link = strdup(value);
+	*valuep = strdup(value);
 	return 0;
 }
 
-static int config_network_name(const char *key, char *value, struct lxc_conf *lxc_conf)
+static int config_network_flags(const char *key, char *value, struct lxc_conf *lxc_conf)
 {
-	struct lxc_list *network = &lxc_conf->network;
-	struct lxc_netdev *netdev;
-
-	if (lxc_list_empty(network)) {
-		ERROR("network is not created for %s", value);
+	struct lxc_netdev *netdev = network_netdev(key, value, lxc_conf);
+	if (!netdev)
 		return -1;
-	}
-
-	netdev = lxc_list_first_elem(network);
-	if (!netdev) {
-		ERROR("no network defined for %s", value);
-		return -1;
-	}
 
-	if (strlen(value) > IFNAMSIZ) {
-		ERROR("invalid interface name: %s", value);
-		return -1;
-	}
-
-	netdev->name = strdup(value);
+	netdev->flags |= IFF_UP;
 	return 0;
 }
 
-static int config_network_pair(const char *key, char *value, struct lxc_conf *lxc_conf)
+static int config_network_link(const char *key, char *value, struct lxc_conf *lxc_conf)
 {
-	struct lxc_list *network = &lxc_conf->network;
-	struct lxc_netdev *netdev;
-
-	if (lxc_list_empty(network)) {
-		ERROR("network is not created for %s", value);
+	struct lxc_netdev *netdev = network_netdev(key, value, lxc_conf);
+	if (!netdev)
 		return -1;
-	}
+	return network_ifname(&netdev->link, value);
+}
 
-	netdev = lxc_list_first_elem(network);
-	if (!netdev) {
-		ERROR("no network defined for %s", value);
+static int config_network_name(const char *key, char *value, struct lxc_conf *lxc_conf)
+{
+	struct lxc_netdev *netdev = network_netdev(key, value, lxc_conf);
+	if (!netdev)
 		return -1;
-	}
+	return network_ifname(&netdev->name, value);
+}
 
-	if (strlen(value) > IFNAMSIZ) {
-		ERROR("invalid interface name: %s", value);
+static int config_network_pair(const char *key, char *value, struct lxc_conf *lxc_conf)
+{
+	struct lxc_netdev *netdev = network_netdev(key, value, lxc_conf);
+	if (!netdev)
 		return -1;
-	}
-
-	netdev->pair = strdup(value);
-	return 0;
+	return network_ifname(&netdev->pair, value);
 }
 
 static int config_network_hwaddr(const char *key, char *value, struct lxc_conf *lxc_conf)
 {
-	struct lxc_list *network = &lxc_conf->network;
-	struct lxc_netdev *netdev;
-
-	if (lxc_list_empty(network)) {
-		ERROR("network is not created for %s", value);
-		return -1;
-	}
-
-	netdev = lxc_list_first_elem(network);
-	if (!netdev) {
-		ERROR("no network defined for %s", value);
+	struct lxc_netdev *netdev = network_netdev(key, value, lxc_conf);
+	if (!netdev)
 		return -1;
-	}
-
 	netdev->hwaddr = strdup(value);
 	return 0;
 }
 
 static int config_network_mtu(const char *key, char *value, struct lxc_conf *lxc_conf)
 {
-	struct lxc_list *network = &lxc_conf->network;
-	struct lxc_netdev *netdev;
-
-	if (lxc_list_empty(network)) {
-		ERROR("network is not created for %s", value);
+	struct lxc_netdev *netdev = network_netdev(key, value, lxc_conf);
+	if (!netdev)
 		return -1;
-	}
-
-	netdev = lxc_list_first_elem(network);
-	if (!netdev) {
-		ERROR("no network defined for %s", value);
-		return -1;
-	}
-
 	netdev->mtu = strdup(value);
 	return 0;
 }
 
 static int config_network_ipv4(const char *key, char *value, struct lxc_conf *lxc_conf)
 {
-	struct lxc_list *network = &lxc_conf->network;
 	struct lxc_inetdev *inetdev;
-	struct lxc_netdev *netdev;
 	struct lxc_list *list;
 	char *cursor, *slash, *addr = NULL, *bcast = NULL, *prefix = NULL;
 
-	if (lxc_list_empty(network)) {
-		ERROR("network is not created for '%s'", value);
+	struct lxc_netdev *netdev = network_netdev(key, value, lxc_conf);
+	if (!netdev)
 		return -1;
-	}
-
-	netdev = lxc_list_first_elem(network);
-	if (!netdev) {
-		ERROR("no netdev defined for '%s'", value);
-	}
 
 	inetdev = malloc(sizeof(*inetdev));
 	if (!inetdev) {
@@ -359,23 +299,14 @@ static int config_network_ipv4(const char *key, char *value, struct lxc_conf *lx
 
 static int config_network_ipv6(const char *key, char *value, struct lxc_conf *lxc_conf)
 {
-	struct lxc_list *network = &lxc_conf->network;
-	struct lxc_netdev *netdev;
 	struct lxc_inet6dev *inet6dev;
 	struct lxc_list *list;
 	char *slash;
 	char *netmask;
 
-	if (lxc_list_empty(network)) {
-		ERROR("network is not created for %s", value);
-		return -1;
-	}
-
-	netdev = lxc_list_first_elem(network);
-	if (!netdev) {
-		ERROR("no network defined for %s", value);
+	struct lxc_netdev *netdev = network_netdev(key, value, lxc_conf);
+	if (!netdev)
 		return -1;
-	}
 
 	inet6dev = malloc(sizeof(*inet6dev));
 	if (!inet6dev) {
-- 
1.6.3.3





More information about the lxc-devel mailing list