netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Toke Høiland-Jørgensen" <toke@redhat.com>
To: David Ahern <dsahern@gmail.com>,
	Stephen Hemminger <stephen@networkplumber.org>
Cc: netdev@vger.kernel.org,
	"Toke Høiland-Jørgensen" <toke@redhat.com>,
	"Nicolas Dichtel" <nicolas.dichtel@6wind.com>,
	"Christian Brauner" <brauner@kernel.org>,
	"Eric W . Biederman" <ebiederm@xmission.com>,
	"David Laight" <David.Laight@ACULAB.COM>
Subject: [RFC PATCH iproute2-next 3/5] lib/namespace: Factor out code for reuse
Date: Mon,  9 Oct 2023 20:27:51 +0200	[thread overview]
Message-ID: <20231009182753.851551-4-toke@redhat.com> (raw)
In-Reply-To: <20231009182753.851551-1-toke@redhat.com>

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 --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];
-- 
2.42.0


  parent reply	other threads:[~2023-10-09 18:28 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-09 18:27 [RFC PATCH iproute2-next 0/5] Persisting of mount namespaces along with network namespaces Toke Høiland-Jørgensen
2023-10-09 18:27 ` [RFC PATCH iproute2-next 1/5] ip: Mount netns in child process instead of from inside the new namespace Toke Høiland-Jørgensen
2023-10-09 18:27 ` [RFC PATCH iproute2-next 2/5] ip: Split out code creating namespace mount dir so it can be reused Toke Høiland-Jørgensen
2023-10-09 18:27 ` Toke Høiland-Jørgensen [this message]
2023-10-09 18:27 ` [RFC PATCH iproute2-next 4/5] ip: Also create and persist mount namespace when creating netns Toke Høiland-Jørgensen
2023-10-09 18:27 ` [RFC PATCH iproute2-next 5/5] lib/namespace: Also mount a bpffs instance inside new mount namespaces Toke Høiland-Jørgensen
2023-10-09 20:32 ` [RFC PATCH iproute2-next 0/5] Persisting of mount namespaces along with network namespaces Eric W. Biederman
2023-10-09 22:03   ` Toke Høiland-Jørgensen
2023-10-10  0:14     ` Eric W. Biederman
2023-10-10 13:38       ` Toke Høiland-Jørgensen
2023-10-10 19:19         ` Eric W. Biederman
2023-10-11 13:49           ` Toke Høiland-Jørgensen
2023-10-11 14:55             ` Eric W. Biederman
2023-10-11 15:03               ` Toke Høiland-Jørgensen
2023-10-10  8:42   ` David Laight
2023-10-10 19:32     ` Eric W. Biederman
2023-10-10 21:51       ` David Laight

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231009182753.851551-4-toke@redhat.com \
    --to=toke@redhat.com \
    --cc=David.Laight@ACULAB.COM \
    --cc=brauner@kernel.org \
    --cc=dsahern@gmail.com \
    --cc=ebiederm@xmission.com \
    --cc=netdev@vger.kernel.org \
    --cc=nicolas.dichtel@6wind.com \
    --cc=stephen@networkplumber.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).