From 61b010c25a969bf0fd8af4441635d395b5fdcac6 Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Sat, 11 Apr 2020 12:14:56 +0200 Subject: [PATCH] lib/netns: OSMO_ASSERT() if user doesn't call init_netns() It is vital that init_netns() is called first in order to initialize default_nsfd. Change-Id: Ic16646fa7d60c578056b17351c5fe2090a81dff0 --- lib/netns.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/netns.c b/lib/netns.c index 1e77a04..6e0e179 100644 --- a/lib/netns.c +++ b/lib/netns.c @@ -40,12 +40,14 @@ #include #include +#include + #include "netns.h" #define NETNS_PATH "/var/run/netns" /*! default namespace of the GGSN process */ -static int default_nsfd; +static int default_nsfd = -1; /*! switch to a (non-default) namespace, store existing signal mask in oldmask. * \param[in] nsfd file descriptor representing the namespace to whch we shall switch @@ -56,6 +58,8 @@ int switch_ns(int nsfd, sigset_t *oldmask) sigset_t intmask; int rc; + OSMO_ASSERT(default_nsfd >= 0); + if (sigfillset(&intmask) < 0) return -errno; if ((rc = sigprocmask(SIG_BLOCK, &intmask, oldmask)) != 0) @@ -71,6 +75,8 @@ int switch_ns(int nsfd, sigset_t *oldmask) * \returns 0 on successs; negative errno value in case of error */ int restore_ns(sigset_t *oldmask) { + OSMO_ASSERT(default_nsfd >= 0); + int rc; if (setns(default_nsfd, CLONE_NEWNET) < 0) return -errno; @@ -87,6 +93,8 @@ int open_ns(int nsfd, const char *pathname, int flags) int fd; int rc; + OSMO_ASSERT(default_nsfd >= 0); + /* mask off all signals, store old signal mask */ if (sigfillset(&intmask) < 0) return -errno; @@ -127,6 +135,8 @@ int socket_ns(int nsfd, int domain, int type, int protocol) int sk; int rc; + OSMO_ASSERT(default_nsfd >= 0); + /* mask off all signals, store old signal mask */ if (sigfillset(&intmask) < 0) return -errno; @@ -177,6 +187,8 @@ int get_nsfd(const char *name) sigset_t intmask, oldmask; char path[MAXPATHLEN] = NETNS_PATH; + OSMO_ASSERT(default_nsfd >= 0); + /* create /var/run/netns, if it doesn't exist already */ rc = mkdir(path, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH); if (rc < 0 && errno != EEXIST)