aboutgitcodebugslistschat
path: root/util.c
diff options
context:
space:
mode:
authorGiuseppe Scrivano <gscrivan@redhat.com>2021-09-29 16:11:06 +0200
committerStefano Brivio <sbrivio@redhat.com>2021-10-07 04:05:15 +0200
commit9a175cc2cea75b98fc3c20381f58dcabf24ef529 (patch)
tree44e40e4dff717ec2e4d12402a47bcf33a6f4f92b /util.c
parentab3283802206d19ea8509f5471b5c0928ca5835f (diff)
downloadpasst-9a175cc2cea75b98fc3c20381f58dcabf24ef529.tar
passt-9a175cc2cea75b98fc3c20381f58dcabf24ef529.tar.gz
passt-9a175cc2cea75b98fc3c20381f58dcabf24ef529.tar.bz2
passt-9a175cc2cea75b98fc3c20381f58dcabf24ef529.tar.lz
passt-9a175cc2cea75b98fc3c20381f58dcabf24ef529.tar.xz
passt-9a175cc2cea75b98fc3c20381f58dcabf24ef529.tar.zst
passt-9a175cc2cea75b98fc3c20381f58dcabf24ef529.zip
pasta: Allow specifying paths and names of namespaces
Based on a patch from Giuseppe Scrivano, this adds the ability to: - specify paths and names of target namespaces to join, instead of a PID, also for user namespaces, with --userns - request to join or create a network namespace only, without entering or creating a user namespace, with --netns-only - specify the base directory for netns mountpoints, with --nsrun-dir Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com> [sbrivio: reworked logic to actually join the given namespaces when they're not created, implemented --netns-only and --nsrun-dir, updated pasta demo script and man page] Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
Diffstat (limited to 'util.c')
-rw-r--r--util.c28
1 files changed, 8 insertions, 20 deletions
diff --git a/util.c b/util.c
index 66b088a..2a5c5ee 100644
--- a/util.c
+++ b/util.c
@@ -32,6 +32,7 @@
#include <stdarg.h>
#include <string.h>
#include <time.h>
+#include <errno.h>
#include "util.h"
#include "passt.h"
@@ -327,31 +328,18 @@ void procfs_scan_listen(char *name, uint8_t *map, uint8_t *exclude)
}
/**
- * ns_enter() - Enter user and network namespaces of process with given PID
- * @target_pid: Process PID
+ * ns_enter() - Enter configured network and user namespaces
+ * @c: Execution context
*
* Return: 0 on success, -1 on failure
*/
-int ns_enter(int target_pid)
+int ns_enter(struct ctx *c)
{
- char ns[PATH_MAX];
- int fd;
-
- snprintf(ns, PATH_MAX, "/proc/%i/ns/user", target_pid);
- if ((fd = open(ns, O_RDONLY)) < 0 || setns(fd, 0))
- goto fail;
- close(fd);
+ if (!c->netns_only && setns(c->pasta_userns_fd, 0))
+ return -errno;
- snprintf(ns, PATH_MAX, "/proc/%i/ns/net", target_pid);
- if ((fd = open(ns, O_RDONLY)) < 0 || setns(fd, 0))
- goto fail;
- close(fd);
+ if (setns(c->pasta_netns_fd, 0))
+ return -errno;
return 0;
-
-fail:
- if (fd != -1)
- close(fd);
-
- return -1;
}