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
This commit is contained in:
Harald Welte 2020-04-11 12:14:56 +02:00 committed by laforge
parent 20d9d154c5
commit 61b010c25a
1 changed files with 13 additions and 1 deletions

View File

@ -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)