diff mbox series

[RFC,iproute2-next,3/5] lib/namespace: Factor out code for reuse

Message ID 20231009182753.851551-4-toke@redhat.com (mailing list archive)
State RFC
Delegated to: David Ahern
Headers show
Series Persisting of mount namespaces along with network namespaces | expand

Checks

Context Check Description
netdev/tree_selection success Not a local patch

Commit Message

Toke Høiland-Jørgensen Oct. 9, 2023, 6:27 p.m. UTC
Factor out the code that switches namespaces and the code that sets up a new
mount namespace into utility functions that can be reused when we add mount
namespace pinning.

No functional change is intended with this patch.

Signed-off-by: Toke Høiland-Jørgensen <toke@redhat.com>
---
 include/namespace.h |  1 +
 lib/namespace.c     | 73 ++++++++++++++++++++++++++++++++-------------
 2 files changed, 54 insertions(+), 20 deletions(-)
diff mbox series

Patch

diff --git a/include/namespace.h b/include/namespace.h
index e47f9b5d49d1..b694a12e8397 100644
--- a/include/namespace.h
+++ b/include/namespace.h
@@ -49,6 +49,7 @@  static inline int setns(int fd, int nstype)
 }
 #endif /* HAVE_SETNS */
 
+int prepare_mountns(const char *name, bool do_unshare);
 int netns_switch(char *netns);
 int netns_get_fd(const char *netns);
 int netns_foreach(int (*func)(char *nsname, void *arg), void *arg);
diff --git a/lib/namespace.c b/lib/namespace.c
index 1202fa85f97d..5e310762f34b 100644
--- a/lib/namespace.c
+++ b/lib/namespace.c
@@ -11,6 +11,25 @@ 
 #include "utils.h"
 #include "namespace.h"
 
+static struct namespace_typename {
+	int		type;		/* CLONE_NEW* */
+	const char	*name;		/* <type> */
+} namespace_names[] = {
+	{ .type = CLONE_NEWNET,   .name = "network"  },
+	{ .type = CLONE_NEWNS,    .name = "mount"  },
+	{ .name = NULL }
+};
+
+static const char *ns_typename(int nstype)
+{
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(namespace_names); i++)
+		if (namespace_names[i].type == nstype)
+			return namespace_names[i].name;
+	return NULL;
+}
+
 static void bind_etc(const char *name)
 {
 	char etc_netns_path[sizeof(NETNS_ETC_DIR) + NAME_MAX];
@@ -42,30 +61,12 @@  static void bind_etc(const char *name)
 	closedir(dir);
 }
 
-int netns_switch(char *name)
+int prepare_mountns(const char *name, bool do_unshare)
 {
-	char net_path[PATH_MAX];
-	int netns;
 	unsigned long mountflags = 0;
 	struct statvfs fsstat;
 
-	snprintf(net_path, sizeof(net_path), "%s/%s", NETNS_RUN_DIR, name);
-	netns = open(net_path, O_RDONLY | O_CLOEXEC);
-	if (netns < 0) {
-		fprintf(stderr, "Cannot open network namespace \"%s\": %s\n",
-			name, strerror(errno));
-		return -1;
-	}
-
-	if (setns(netns, CLONE_NEWNET) < 0) {
-		fprintf(stderr, "setting the network namespace \"%s\" failed: %s\n",
-			name, strerror(errno));
-		close(netns);
-		return -1;
-	}
-	close(netns);
-
-	if (unshare(CLONE_NEWNS) < 0) {
+	if (do_unshare && unshare(CLONE_NEWNS) < 0) {
 		fprintf(stderr, "unshare failed: %s\n", strerror(errno));
 		return -1;
 	}
@@ -97,6 +98,38 @@  int netns_switch(char *name)
 	return 0;
 }
 
+static int switch_ns(const char *parent_dir, const char *name, int nstype)
+{
+	char ns_path[PATH_MAX];
+	int ns_fd;
+
+	snprintf(ns_path, sizeof(ns_path), "%s/%s", parent_dir, name);
+	ns_fd = open(ns_path, O_RDONLY | O_CLOEXEC);
+	if (ns_fd < 0) {
+		fprintf(stderr, "Cannot open %s namespace \"%s\": %s\n",
+			ns_typename(nstype), name, strerror(errno));
+		return -1;
+	}
+
+	if (setns(ns_fd, nstype) < 0) {
+		fprintf(stderr, "setting the %s namespace \"%s\" failed: %s\n",
+			ns_typename(nstype), name, strerror(errno));
+		close(ns_fd);
+		return -1;
+	}
+	close(ns_fd);
+	return 0;
+}
+
+int netns_switch(char *name)
+{
+
+	if (switch_ns(NETNS_RUN_DIR, name, CLONE_NEWNET))
+		return -1;
+
+	return prepare_mountns(name, true);
+}
+
 int netns_get_fd(const char *name)
 {
 	char pathbuf[PATH_MAX];