This is merely a historical archive of years 2008-2021, before the migration to mailman3.
A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.
laforge gerrit-no-reply at lists.osmocom.orglaforge has uploaded this change for review. ( https://gerrit.osmocom.org/c/osmo-ggsn/+/17801 )
Change subject: lib/netns.c: Add comments to the code, including doxygen API docs
......................................................................
lib/netns.c: Add comments to the code, including doxygen API docs
Change-Id: I0b20e4870bf62df0a459a621a64a4e2795340ceb
---
M lib/netns.c
1 file changed, 45 insertions(+), 0 deletions(-)
git pull ssh://gerrit.osmocom.org:29418/osmo-ggsn refs/changes/01/17801/1
diff --git a/lib/netns.c b/lib/netns.c
index 58ae036..43fca82 100644
--- a/lib/netns.c
+++ b/lib/netns.c
@@ -44,8 +44,13 @@
#define NETNS_PATH "/var/run/netns"
+/*! default namespace of the GGSN process */
static int default_nsfd;
+/*! 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
+ * \param[out] oldmaks caller-provided memory location to which old signal mask is stored
+ * \ returns 0 on success or negative (errno) in case of error */
int switch_ns(int nsfd, sigset_t *oldmask)
{
sigset_t intmask;
@@ -61,6 +66,9 @@
return 0;
}
+/*! switch back to the default namespace, restoring signal mask.
+ * \param[in] oldmask signal mask to restore after returning to default namespace
+ * \returns 0 on successs; negative errno value in case of error */
int restore_ns(sigset_t *oldmask)
{
int rc;
@@ -72,25 +80,31 @@
return 0;
}
+/*! open a file from within specified network namespace */
int open_ns(int nsfd, const char *pathname, int flags)
{
sigset_t intmask, oldmask;
int fd;
int rc;
+ /* mask off all signals, store old signal mask */
if (sigfillset(&intmask) < 0)
return -errno;
if ((rc = sigprocmask(SIG_BLOCK, &intmask, &oldmask)) != 0)
return -rc;
+ /* associate the calling thread with namespace file descriptor */
if (setns(nsfd, CLONE_NEWNET) < 0)
return -errno;
+ /* open the requested file/path */
if ((fd = open(pathname, flags)) < 0)
return -errno;
+ /* return back to default namespace */
if (setns(default_nsfd, CLONE_NEWNET) < 0) {
close(fd);
return -errno;
}
+ /* restore process mask */
if ((rc = sigprocmask(SIG_SETMASK, &oldmask, NULL)) != 0) {
close(fd);
return -rc;
@@ -99,26 +113,41 @@
return fd;
}
+/*! create a socket in another namespace.
+ * Switches temporarily to namespace indicated by nsfd, creates a socket in
+ * that namespace and then returns to the default namespace.
+ * \param[in] nsfd File descriptor of the namspace in which to create socket
+ * \param[in] domain Domain of the socket (AF_INET, ...)
+ * \param[in] type Type of the socket (SOCK_STREAM, ...)
+ * \param[in] protocol Protocol of the socket (IPPROTO_TCP, ...)
+ * \returns 0 on success; negative errno in case of error */
int socket_ns(int nsfd, int domain, int type, int protocol)
{
sigset_t intmask, oldmask;
int sk;
int rc;
+ /* mask off all signals, store old signal mask */
if (sigfillset(&intmask) < 0)
return -errno;
if ((rc = sigprocmask(SIG_BLOCK, &intmask, &oldmask)) != 0)
return -rc;
+ /* associate the calling thread with namespace file descriptor */
if (setns(nsfd, CLONE_NEWNET) < 0)
return -errno;
+
+ /* create socket of requested domain/type/proto */
if ((sk = socket(domain, type, protocol)) < 0)
return -errno;
+
+ /* return back to default namespace */
if (setns(default_nsfd, CLONE_NEWNET) < 0) {
close(sk);
return -errno;
}
+ /* restore process mask */
if ((rc = sigprocmask(SIG_SETMASK, &oldmask, NULL)) != 0) {
close(sk);
return -rc;
@@ -126,13 +155,21 @@
return sk;
}
+/*! initialize this network namespace helper module.
+ * Must be called before using any other functions of this file.
+ * \returns 0 on success; negative errno in case of error */
int init_netns()
{
+ /* store the default namespace for later reference */
if ((default_nsfd = open("/proc/self/ns/net", O_RDONLY)) < 0)
return -errno;
return 0;
}
+/*! create obtain file descriptor for network namespace of give name.
+ * Creates /var/run/netns if it doesn't exist already.
+ * \param[in] name Name of the network namespace (in /var/run/netns/)
+ * \returns File descriptor of network namespace; negative errno in case of error */
int get_nsfd(const char *name)
{
int rc;
@@ -140,10 +177,12 @@
sigset_t intmask, oldmask;
char path[MAXPATHLEN] = NETNS_PATH;
+ /* 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)
return rc;
+ /* create /var/run/netns/[name], if it doesn't exist already */
snprintf(path, sizeof(path), "%s/%s", NETNS_PATH, name);
fd = open(path, O_RDONLY|O_CREAT|O_EXCL, 0);
if (fd < 0) {
@@ -157,24 +196,30 @@
if (close(fd) < 0)
return -errno;
+ /* mask off all signals, store old signal mask */
if (sigfillset(&intmask) < 0)
return -errno;
if ((rc = sigprocmask(SIG_BLOCK, &intmask, &oldmask)) != 0)
return -rc;
+ /* create a new network namespace */
if (unshare(CLONE_NEWNET) < 0)
return -errno;
if (mount("/proc/self/ns/net", path, "none", MS_BIND, NULL) < 0)
return -errno;
+ /* switch back to default namespace */
if (setns(default_nsfd, CLONE_NEWNET) < 0)
return -errno;
+ /* restore process mask */
if ((rc = sigprocmask(SIG_SETMASK, &oldmask, NULL)) != 0)
return -rc;
+ /* finally, open the created namespace file descriptor from default ns */
if ((fd = open(path, O_RDONLY)) < 0)
return -errno;
+
return fd;
}
--
To view, visit https://gerrit.osmocom.org/c/osmo-ggsn/+/17801
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings
Gerrit-Project: osmo-ggsn
Gerrit-Branch: master
Gerrit-Change-Id: I0b20e4870bf62df0a459a621a64a4e2795340ceb
Gerrit-Change-Number: 17801
Gerrit-PatchSet: 1
Gerrit-Owner: laforge <laforge at osmocom.org>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20200411/b4420037/attachment.htm>