pinns: support pinning host ns
#include "utils.h"
#include "sysctl.h"
+static bool is_host_ns(const char* const optarg);
static int bind_ns(const char *pin_path, const char *filename, const char *ns_name, pid_t pid);
static int directory_exists_or_create(const char* path);
@@ -28,6 +29,8 @@ enum {
GID_MAPPING = 1001,
};
+const char* const HOSTNS = "host";
+
int main(int argc, char **argv) {
const char *uid_mapping = NULL;
const char *gid_mapping = NULL;
@@ -63,27 +66,33 @@ int main(int argc, char **argv) {
while ((c = getopt_long(argc, argv, "pchuUind:f:s:", long_options, NULL)) != -1) {
switch (c) {
case 'u':
+ if (!is_host_ns (optarg))
unshare_flags |= CLONE_NEWUTS;
bind_uts = true;
num_unshares++;
break;
case 'i':
+ if (!is_host_ns (optarg))
unshare_flags |= CLONE_NEWIPC;
bind_ipc = true;
num_unshares++;
break;
case 'n':
+ if (!is_host_ns (optarg)) {
unshare_flags |= CLONE_NEWNET;
+ }
bind_net = true;
num_unshares++;
break;
case 'U':
+ if (!is_host_ns (optarg))
unshare_flags |= CLONE_NEWUSER;
bind_user = true;
num_unshares++;
break;
case 'c':
#ifdef CLONE_NEWCGROUP
+ if (!is_host_ns (optarg))
unshare_flags |= CLONE_NEWCGROUP;
bind_cgroup = true;
num_unshares++;
@@ -254,6 +263,11 @@ int main(int argc, char **argv) {
return EXIT_SUCCESS;
}
+// returns true if the option is equal to 'host'
+static bool is_host_ns(const char* const optarg) {
+ return optarg && !strcmp (optarg, HOSTNS);
+}
+
static int bind_ns(const char *pin_path, const char *filename, const char *ns_name, pid_t pid) {
char bind_path[PATH_MAX];
char ns_path[PATH_MAX];
