aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHarald Welte <laforge@osmocom.org>2020-04-11 12:14:56 +0200
committerlaforge <laforge@osmocom.org>2020-04-14 13:19:35 +0000
commit61b010c25a969bf0fd8af4441635d395b5fdcac6 (patch)
tree01aa0134ebd638c29549b5b5ccb4f8e79f029ffd
parent20d9d154c506f1d7dd7bf8fa0581bbf22be4630f (diff)
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
-rw-r--r--lib/netns.c14
1 files changed, 13 insertions, 1 deletions
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 <fcntl.h>
#include <errno.h>
+#include <osmocom/core/utils.h>
+
#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)