|
|
8def76 |
From 56dfe34480259eebd91c9a4dc57a6fe15c07e60a Mon Sep 17 00:00:00 2001
|
|
|
8def76 |
From: Andrea Claudi <aclaudi@redhat.com>
|
|
|
8def76 |
Date: Fri, 28 Jun 2019 14:12:36 +0200
|
|
|
8def76 |
Subject: [PATCH] netns: make netns_{save,restore} static
|
|
|
8def76 |
|
|
|
8def76 |
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1719759
|
|
|
8def76 |
Upstream Status: iproute2.git commit b2e2922373a6c
|
|
|
8def76 |
Conflicts: context change due to missing commit e3dbcb2a12ab1
|
|
|
8def76 |
("netns: add subcommand to attach an existing network namespace")
|
|
|
8def76 |
|
|
|
8def76 |
commit b2e2922373a6c65ed08b57926e61f3621d89a70a
|
|
|
8def76 |
Author: Matteo Croce <mcroce@redhat.com>
|
|
|
8def76 |
Date: Tue Jun 18 16:49:35 2019 +0200
|
|
|
8def76 |
|
|
|
8def76 |
netns: make netns_{save,restore} static
|
|
|
8def76 |
|
|
|
8def76 |
The netns_{save,restore} functions are only used in ipnetns.c now, since
|
|
|
8def76 |
the restore is not needed anymore after the netns exec command.
|
|
|
8def76 |
Move them in ipnetns.c, and make them static.
|
|
|
8def76 |
|
|
|
8def76 |
Signed-off-by: Matteo Croce <mcroce@redhat.com>
|
|
|
8def76 |
Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
|
|
|
8def76 |
---
|
|
|
8def76 |
include/namespace.h | 2 --
|
|
|
8def76 |
ip/ip.c | 1 -
|
|
|
8def76 |
ip/ipnetns.c | 31 +++++++++++++++++++++++++++++++
|
|
|
8def76 |
lib/namespace.c | 31 -------------------------------
|
|
|
8def76 |
4 files changed, 31 insertions(+), 34 deletions(-)
|
|
|
8def76 |
|
|
|
8def76 |
diff --git a/include/namespace.h b/include/namespace.h
|
|
|
8def76 |
index 89cdda11782e8..e47f9b5d49d12 100644
|
|
|
8def76 |
--- a/include/namespace.h
|
|
|
8def76 |
+++ b/include/namespace.h
|
|
|
8def76 |
@@ -49,8 +49,6 @@ static inline int setns(int fd, int nstype)
|
|
|
8def76 |
}
|
|
|
8def76 |
#endif /* HAVE_SETNS */
|
|
|
8def76 |
|
|
|
8def76 |
-void netns_save(void);
|
|
|
8def76 |
-void netns_restore(void);
|
|
|
8def76 |
int netns_switch(char *netns);
|
|
|
8def76 |
int netns_get_fd(const char *netns);
|
|
|
8def76 |
int netns_foreach(int (*func)(char *nsname, void *arg), void *arg);
|
|
|
8def76 |
diff --git a/ip/ip.c b/ip/ip.c
|
|
|
8def76 |
index 6e8230b3ee584..2ca55e37a4c62 100644
|
|
|
8def76 |
--- a/ip/ip.c
|
|
|
8def76 |
+++ b/ip/ip.c
|
|
|
8def76 |
@@ -158,7 +158,6 @@ static int batch(const char *name)
|
|
|
8def76 |
if (!force)
|
|
|
8def76 |
break;
|
|
|
8def76 |
}
|
|
|
8def76 |
- netns_restore();
|
|
|
8def76 |
}
|
|
|
8def76 |
if (line)
|
|
|
8def76 |
free(line);
|
|
|
8def76 |
diff --git a/ip/ipnetns.c b/ip/ipnetns.c
|
|
|
8def76 |
index 10bfe2eb69e0b..40848a5cf10ac 100644
|
|
|
8def76 |
--- a/ip/ipnetns.c
|
|
|
8def76 |
+++ b/ip/ipnetns.c
|
|
|
8def76 |
@@ -42,6 +42,7 @@ static int usage(void)
|
|
|
8def76 |
static struct rtnl_handle rtnsh = { .fd = -1 };
|
|
|
8def76 |
|
|
|
8def76 |
static int have_rtnl_getnsid = -1;
|
|
|
8def76 |
+static int saved_netns = -1;
|
|
|
8def76 |
|
|
|
8def76 |
static int ipnetns_accept_msg(const struct sockaddr_nl *who,
|
|
|
8def76 |
struct rtnl_ctrl_data *ctrl,
|
|
|
8def76 |
@@ -634,6 +635,33 @@ static int create_netns_dir(void)
|
|
|
8def76 |
return 0;
|
|
|
8def76 |
}
|
|
|
8def76 |
|
|
|
8def76 |
+/* Obtain a FD for the current namespace, so we can reenter it later */
|
|
|
8def76 |
+static void netns_save(void)
|
|
|
8def76 |
+{
|
|
|
8def76 |
+ if (saved_netns != -1)
|
|
|
8def76 |
+ return;
|
|
|
8def76 |
+
|
|
|
8def76 |
+ saved_netns = open("/proc/self/ns/net", O_RDONLY | O_CLOEXEC);
|
|
|
8def76 |
+ if (saved_netns == -1) {
|
|
|
8def76 |
+ perror("Cannot open init namespace");
|
|
|
8def76 |
+ exit(1);
|
|
|
8def76 |
+ }
|
|
|
8def76 |
+}
|
|
|
8def76 |
+
|
|
|
8def76 |
+static void netns_restore(void)
|
|
|
8def76 |
+{
|
|
|
8def76 |
+ if (saved_netns == -1)
|
|
|
8def76 |
+ return;
|
|
|
8def76 |
+
|
|
|
8def76 |
+ if (setns(saved_netns, CLONE_NEWNET)) {
|
|
|
8def76 |
+ perror("setns");
|
|
|
8def76 |
+ exit(1);
|
|
|
8def76 |
+ }
|
|
|
8def76 |
+
|
|
|
8def76 |
+ close(saved_netns);
|
|
|
8def76 |
+ saved_netns = -1;
|
|
|
8def76 |
+}
|
|
|
8def76 |
+
|
|
|
8def76 |
static int netns_add(int argc, char **argv)
|
|
|
8def76 |
{
|
|
|
8def76 |
/* This function creates a new network namespace and
|
|
|
8def76 |
@@ -704,8 +732,11 @@ static int netns_add(int argc, char **argv)
|
|
|
8def76 |
netns_path, strerror(errno));
|
|
|
8def76 |
goto out_delete;
|
|
|
8def76 |
}
|
|
|
8def76 |
+ netns_restore();
|
|
|
8def76 |
+
|
|
|
8def76 |
return 0;
|
|
|
8def76 |
out_delete:
|
|
|
8def76 |
+ netns_restore();
|
|
|
8def76 |
netns_delete(argc, argv);
|
|
|
8def76 |
return -1;
|
|
|
8def76 |
}
|
|
|
8def76 |
diff --git a/lib/namespace.c b/lib/namespace.c
|
|
|
8def76 |
index a2aea57ad4109..06ae0a48c2243 100644
|
|
|
8def76 |
--- a/lib/namespace.c
|
|
|
8def76 |
+++ b/lib/namespace.c
|
|
|
8def76 |
@@ -15,35 +15,6 @@
|
|
|
8def76 |
#include "utils.h"
|
|
|
8def76 |
#include "namespace.h"
|
|
|
8def76 |
|
|
|
8def76 |
-static int saved_netns = -1;
|
|
|
8def76 |
-
|
|
|
8def76 |
-/* Obtain a FD for the current namespace, so we can reenter it later */
|
|
|
8def76 |
-void netns_save(void)
|
|
|
8def76 |
-{
|
|
|
8def76 |
- if (saved_netns != -1)
|
|
|
8def76 |
- return;
|
|
|
8def76 |
-
|
|
|
8def76 |
- saved_netns = open("/proc/self/ns/net", O_RDONLY | O_CLOEXEC);
|
|
|
8def76 |
- if (saved_netns == -1) {
|
|
|
8def76 |
- perror("Cannot open init namespace");
|
|
|
8def76 |
- exit(1);
|
|
|
8def76 |
- }
|
|
|
8def76 |
-}
|
|
|
8def76 |
-
|
|
|
8def76 |
-void netns_restore(void)
|
|
|
8def76 |
-{
|
|
|
8def76 |
- if (saved_netns == -1)
|
|
|
8def76 |
- return;
|
|
|
8def76 |
-
|
|
|
8def76 |
- if (setns(saved_netns, CLONE_NEWNET)) {
|
|
|
8def76 |
- perror("setns");
|
|
|
8def76 |
- exit(1);
|
|
|
8def76 |
- }
|
|
|
8def76 |
-
|
|
|
8def76 |
- close(saved_netns);
|
|
|
8def76 |
- saved_netns = -1;
|
|
|
8def76 |
-}
|
|
|
8def76 |
-
|
|
|
8def76 |
static void bind_etc(const char *name)
|
|
|
8def76 |
{
|
|
|
8def76 |
char etc_netns_path[sizeof(NETNS_ETC_DIR) + NAME_MAX];
|
|
|
8def76 |
@@ -90,8 +61,6 @@ int netns_switch(char *name)
|
|
|
8def76 |
return -1;
|
|
|
8def76 |
}
|
|
|
8def76 |
|
|
|
8def76 |
- netns_save();
|
|
|
8def76 |
-
|
|
|
8def76 |
if (setns(netns, CLONE_NEWNET) < 0) {
|
|
|
8def76 |
fprintf(stderr, "setting the network namespace \"%s\" failed: %s\n",
|
|
|
8def76 |
name, strerror(errno));
|
|
|
8def76 |
--
|
|
|
8def76 |
2.20.1
|
|
|
8def76 |
|