[lxc-devel] [lxc/master] network: fixes

brauner on Github lxc-bot at linuxcontainers.org
Thu Mar 19 11:47:39 UTC 2020


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 364 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20200319/4a2f2ff7/attachment.bin>
-------------- next part --------------
From 3473ca7649af8e755b869589c31e256a8c69ed32 Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Thu, 19 Mar 2020 12:45:23 +0100
Subject: [PATCH 1/2] network: introduce and use is_empty_string()

since some members of struct lxc_netdev are arrays, not pointers.

Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
 src/lxc/network.c      | 26 +++++++++++---------------
 src/lxc/string_utils.h |  5 +++++
 2 files changed, 16 insertions(+), 15 deletions(-)

diff --git a/src/lxc/network.c b/src/lxc/network.c
index 544aab2753..b89c29a032 100644
--- a/src/lxc/network.c
+++ b/src/lxc/network.c
@@ -504,7 +504,7 @@ static int instantiate_macvlan(struct lxc_handler *handler, struct lxc_netdev *n
 	}
 
 	strlcpy(netdev->created_name, peer, IFNAMSIZ);
-	if (netdev->name[0] == '\0')
+	if (is_empty_string(netdev->name))
 		(void)strlcpy(netdev->name, peer, IFNAMSIZ);
 
 	netdev->ifindex = if_nametoindex(peer);
@@ -668,7 +668,7 @@ static int instantiate_ipvlan(struct lxc_handler *handler, struct lxc_netdev *ne
 	}
 
 	strlcpy(netdev->created_name, peer, IFNAMSIZ);
-	if (netdev->name[0] == '\0')
+	if (is_empty_string(netdev->name))
 		(void)strlcpy(netdev->name, peer, IFNAMSIZ);
 
 	netdev->ifindex = if_nametoindex(peer);
@@ -743,7 +743,7 @@ static int instantiate_vlan(struct lxc_handler *handler, struct lxc_netdev *netd
 	}
 
 	strlcpy(netdev->created_name, peer, IFNAMSIZ);
-	if (netdev->name[0] == '\0')
+	if (is_empty_string(netdev->name))
 		(void)strlcpy(netdev->name, peer, IFNAMSIZ);
 
 	netdev->ifindex = if_nametoindex(peer);
@@ -818,7 +818,7 @@ static int instantiate_phys(struct lxc_handler *handler, struct lxc_netdev *netd
 	}
 
 	strlcpy(netdev->created_name, netdev->link, IFNAMSIZ);
-	if (netdev->name[0] == '\0')
+	if (is_empty_string(netdev->name))
 		(void)strlcpy(netdev->name, netdev->link, IFNAMSIZ);
 
 	/*
@@ -924,7 +924,7 @@ static int instantiate_ns_veth(struct lxc_netdev *netdev)
 				       errno, "Failed to retrieve ifindex for network device with name %s",
 				       netdev->created_name);
 
-	if (netdev->name[0] == '\0')
+	if (is_empty_string(netdev->name))
 		(void)strlcpy(netdev->name, "eth%d", IFNAMSIZ);
 
 	if (strcmp(netdev->created_name, netdev->name) != 0) {
@@ -1179,10 +1179,8 @@ static int lxc_netdev_move_by_index_fd(int ifindex, int fd, const char *ifname)
 	if (nla_put_u32(nlmsg, IFLA_NET_NS_FD, fd))
 		goto out;
 
-	if (ifname != NULL) {
-		if (nla_put_string(nlmsg, IFLA_IFNAME, ifname))
-			goto out;
-	}
+	if (!is_empty_string(ifname) && nla_put_string(nlmsg, IFLA_IFNAME, ifname))
+		goto out;
 
 	err = netlink_transaction(&nlh, nlmsg, nlmsg);
 out:
@@ -1219,10 +1217,8 @@ int lxc_netdev_move_by_index(int ifindex, pid_t pid, const char *ifname)
 	if (nla_put_u32(nlmsg, IFLA_NET_NS_PID, pid))
 		goto out;
 
-	if (ifname != NULL) {
-		if (nla_put_string(nlmsg, IFLA_IFNAME, ifname))
-			goto out;
-	}
+	if (!is_empty_string(ifname) && nla_put_string(nlmsg, IFLA_IFNAME, ifname))
+		goto out;
 
 	err = netlink_transaction(&nlh, nlmsg, nlmsg);
 out:
@@ -2812,8 +2808,8 @@ static int lxc_create_network_unpriv_exec(const char *lxcpath, const char *lxcna
 
 		INFO("Execing lxc-user-nic create %s %s %s veth %s %s", lxcpath,
 		     lxcname, pidstr, netdev_link,
-		     netdev->name[0] != '\0' ? netdev->name : "(null)");
-		if (netdev->name[0] != '\0')
+		     !is_empty_string(netdev->name) ? netdev->name : "(null)");
+		if (!is_empty_string(netdev->name))
 			execlp(LXC_USERNIC_PATH, LXC_USERNIC_PATH, "create",
 			       lxcpath, lxcname, pidstr, "veth", netdev_link,
 			       netdev->name, (char *)NULL);
diff --git a/src/lxc/string_utils.h b/src/lxc/string_utils.h
index ec0c1acef7..2720f1097e 100644
--- a/src/lxc/string_utils.h
+++ b/src/lxc/string_utils.h
@@ -98,4 +98,9 @@ extern char *lxc_trim_whitespace_in_place(char *buffer);
 extern int lxc_is_line_empty(const char *line);
 extern void remove_trailing_slashes(char *p);
 
+static inline bool is_empty_string(const char *s)
+{
+	return !s || strcmp(s, "") == 0;
+}
+
 #endif /* __LXC_STRING_UTILS_H */

From 279ce00f3692684be7046956f37ad1cccdbaa234 Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Thu, 19 Mar 2020 12:46:00 +0100
Subject: [PATCH 2/2] network: fix moving network devices with custom name

Closes #1271.
Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
 src/lxc/network.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/lxc/network.c b/src/lxc/network.c
index b89c29a032..0e79500f1f 100644
--- a/src/lxc/network.c
+++ b/src/lxc/network.c
@@ -3359,7 +3359,7 @@ int lxc_network_move_created_netdev_priv(struct lxc_handler *handler)
 			physname = is_wlan(netdev->link);
 
 		if (physname)
-			ret = lxc_netdev_move_wlan(physname, netdev->link, pid, NULL);
+			ret = lxc_netdev_move_wlan(physname, netdev->link, pid, netdev->name);
 		else
 			ret = lxc_netdev_move_by_index(netdev->ifindex, pid, netdev->name);
 		if (ret) {


More information about the lxc-devel mailing list