]> git.ipfire.org Git - thirdparty/iproute2.git/commitdiff
netns: make netns_{save,restore} static
authorMatteo Croce <mcroce@redhat.com>
Tue, 18 Jun 2019 14:49:35 +0000 (16:49 +0200)
committerStephen Hemminger <stephen@networkplumber.org>
Thu, 20 Jun 2019 21:30:41 +0000 (14:30 -0700)
The netns_{save,restore} functions are only used in ipnetns.c now, since
the restore is not needed anymore after the netns exec command.
Move them in ipnetns.c, and make them static.

Signed-off-by: Matteo Croce <mcroce@redhat.com>
Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
include/namespace.h
ip/ip.c
ip/ipnetns.c
lib/namespace.c

index 89cdda11782e8ad1ca2e56df1a174a19f3f952b1..e47f9b5d49d12fae39be243763d2a87df0446f7a 100644 (file)
@@ -49,8 +49,6 @@ static inline int setns(int fd, int nstype)
 }
 #endif /* HAVE_SETNS */
 
-void netns_save(void);
-void netns_restore(void);
 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/ip/ip.c b/ip/ip.c
index d64d43e19c5fc2fa142414f14440548b10dae857..e4131714018f9a843a02cbff2fa9054eae75e75d 100644 (file)
--- a/ip/ip.c
+++ b/ip/ip.c
@@ -158,7 +158,6 @@ static int batch(const char *name)
                        if (!force)
                                break;
                }
-               netns_restore();
        }
        if (line)
                free(line);
index e32e44bdbb4ed497a6e699a337ee21827b15bfde..7e6cfe82e6f8f89c9e31daf12a21fe9945602fd0 100644 (file)
@@ -44,6 +44,7 @@ static int usage(void)
 static struct rtnl_handle rtnsh = { .fd = -1 };
 
 static int have_rtnl_getnsid = -1;
+static int saved_netns = -1;
 
 static int ipnetns_accept_msg(struct rtnl_ctrl_data *ctrl,
                              struct nlmsghdr *n, void *arg)
@@ -634,6 +635,33 @@ static int create_netns_dir(void)
        return 0;
 }
 
+/* Obtain a FD for the current namespace, so we can reenter it later */
+static void netns_save(void)
+{
+       if (saved_netns != -1)
+               return;
+
+       saved_netns = open("/proc/self/ns/net", O_RDONLY | O_CLOEXEC);
+       if (saved_netns == -1) {
+               perror("Cannot open init namespace");
+               exit(1);
+       }
+}
+
+static void netns_restore(void)
+{
+       if (saved_netns == -1)
+               return;
+
+       if (setns(saved_netns, CLONE_NEWNET)) {
+               perror("setns");
+               exit(1);
+       }
+
+       close(saved_netns);
+       saved_netns = -1;
+}
+
 static int netns_add(int argc, char **argv, bool create)
 {
        /* This function creates a new network namespace and
@@ -727,9 +755,12 @@ static int netns_add(int argc, char **argv, bool create)
                        proc_path, netns_path, strerror(errno));
                goto out_delete;
        }
+       netns_restore();
+
        return 0;
 out_delete:
        if (create) {
+               netns_restore();
                netns_delete(argc, argv);
        } else if (unlink(netns_path) < 0) {
                fprintf(stderr, "Cannot remove namespace file \"%s\": %s\n",
index a2aea57ad410958e9faeb2f04c6c865731199699..06ae0a48c2243afa4847013a022504dc420a1b45 100644 (file)
 #include "utils.h"
 #include "namespace.h"
 
-static int saved_netns = -1;
-
-/* Obtain a FD for the current namespace, so we can reenter it later */
-void netns_save(void)
-{
-       if (saved_netns != -1)
-               return;
-
-       saved_netns = open("/proc/self/ns/net", O_RDONLY | O_CLOEXEC);
-       if (saved_netns == -1) {
-               perror("Cannot open init namespace");
-               exit(1);
-       }
-}
-
-void netns_restore(void)
-{
-       if (saved_netns == -1)
-               return;
-
-       if (setns(saved_netns, CLONE_NEWNET)) {
-               perror("setns");
-               exit(1);
-       }
-
-       close(saved_netns);
-       saved_netns = -1;
-}
-
 static void bind_etc(const char *name)
 {
        char etc_netns_path[sizeof(NETNS_ETC_DIR) + NAME_MAX];
@@ -90,8 +61,6 @@ int netns_switch(char *name)
                return -1;
        }
 
-       netns_save();
-
        if (setns(netns, CLONE_NEWNET) < 0) {
                fprintf(stderr, "setting the network namespace \"%s\" failed: %s\n",
                        name, strerror(errno));