diff --git a/include/ipc_ns.h b/include/ipc_ns.h index 52b01d88c..f5a643ea3 100644 --- a/include/ipc_ns.h +++ b/include/ipc_ns.h @@ -8,7 +8,9 @@ extern void show_ipc_var(int fd, struct cr_options *); extern void show_ipc_shm(int fd, struct cr_options *); extern void show_ipc_msg(int fd, struct cr_options *); extern void show_ipc_sem(int fd, struct cr_options *); -extern int dump_ipc_ns(int ns_pid, struct cr_fdset *fdset); +extern int dump_ipc_ns(int ns_pid, const struct cr_fdset *fdset); extern int prepare_ipc_ns(int pid); +extern struct ns_desc ipc_ns_desc; + #endif /* __CR_IPC_NS_H__ */ diff --git a/include/mount.h b/include/mount.h index fbf3cf390..f7c096dd4 100644 --- a/include/mount.h +++ b/include/mount.h @@ -22,4 +22,6 @@ struct mount_info; extern struct mount_info *lookup_mnt_id(unsigned int id); extern struct mount_info *lookup_mnt_sdev(unsigned int s_dev); +extern struct ns_desc mnt_ns_desc; + #endif /* __CR_MOUNT_H__ */ diff --git a/include/namespaces.h b/include/namespaces.h index 8b3404201..b12cf11cb 100644 --- a/include/namespaces.h +++ b/include/namespaces.h @@ -8,7 +8,14 @@ int dump_namespaces(struct pid *pid, unsigned int ns_flags); int prepare_namespace(int pid, unsigned long clone_flags); struct cr_options; int try_show_namespaces(int pid, struct cr_options *); -int switch_ns(int pid, int type, char *ns, int *rst); -int restore_ns(int rst, int type); + +struct ns_desc { + unsigned int cflag; + char *str; +}; + +int switch_ns(int pid, struct ns_desc *nd, int *rst); +int restore_ns(int rst, struct ns_desc *nd); +extern struct ns_desc pid_ns_desc; #endif /* __CR_NS_H__ */ diff --git a/include/net.h b/include/net.h index 321493c50..fb3b50343 100644 --- a/include/net.h +++ b/include/net.h @@ -20,4 +20,6 @@ struct veth_pair { extern int network_lock(void); extern void network_unlock(void); +extern struct ns_desc net_ns_desc; + #endif /* __CR_NET_H__ */ diff --git a/include/uts_ns.h b/include/uts_ns.h index 3c013d5af..2291feec9 100644 --- a/include/uts_ns.h +++ b/include/uts_ns.h @@ -8,4 +8,6 @@ struct cr_options; void show_utsns(int fd, struct cr_options *); int prepare_utsns(int pid); +extern struct ns_desc uts_ns_desc; + #endif /* __CR_UTS_NS_H__ */ diff --git a/ipc_ns.c b/ipc_ns.c index 223a4363e..c94b3b5f4 100644 --- a/ipc_ns.c +++ b/ipc_ns.c @@ -12,6 +12,7 @@ #include "syscall.h" #include "namespaces.h" #include "sysctl.h" +#include "ipc_ns.h" #include "protobuf.h" #include "protobuf/ipc-var.pb-c.h" @@ -436,7 +437,7 @@ int dump_ipc_ns(int ns_pid, const struct cr_fdset *fdset) { int ret; - ret = switch_ns(ns_pid, CLONE_NEWIPC, "ipc", NULL); + ret = switch_ns(ns_pid, &ipc_ns_desc, NULL); if (ret < 0) return ret; @@ -873,3 +874,8 @@ int prepare_ipc_ns(int pid) return ret; return 0; } + +struct ns_desc ipc_ns_desc = { + .cflag = CLONE_NEWIPC, + .str = "ipc", +}; diff --git a/mount.c b/mount.c index 198a480cc..3a45de2e1 100644 --- a/mount.c +++ b/mount.c @@ -19,7 +19,7 @@ #include "mount.h" #include "proc_parse.h" #include "image.h" - +#include "namespaces.h" #include "protobuf.h" #include "protobuf/mnt.pb-c.h" @@ -763,3 +763,8 @@ int mntns_collect_root(pid_t pid) return 0; } + +struct ns_desc mnt_ns_desc = { + .cflag = CLONE_NEWNS, + .str = "mnt", +}; diff --git a/namespaces.c b/namespaces.c index dc8e00896..c0413392f 100644 --- a/namespaces.c +++ b/namespaces.c @@ -10,13 +10,13 @@ #include "namespaces.h" #include "net.h" -int switch_ns(int pid, int type, char *ns, int *rst) +int switch_ns(int pid, struct ns_desc *nd, int *rst) { char buf[32]; int nsfd; int ret = -1; - snprintf(buf, sizeof(buf), "/proc/%d/ns/%s", pid, ns); + snprintf(buf, sizeof(buf), "/proc/%d/ns/%s", pid, nd->str); nsfd = open(buf, O_RDONLY); if (nsfd < 0) { pr_perror("Can't open ipcns file"); @@ -24,7 +24,7 @@ int switch_ns(int pid, int type, char *ns, int *rst) } if (rst) { - snprintf(buf, sizeof(buf), "/proc/self/ns/%s", ns); + snprintf(buf, sizeof(buf), "/proc/self/ns/%s", nd->str); *rst = open(buf, O_RDONLY); if (*rst < 0) { pr_perror("Can't open ns file"); @@ -32,9 +32,9 @@ int switch_ns(int pid, int type, char *ns, int *rst) } } - ret = setns(nsfd, type); + ret = setns(nsfd, nd->cflag); if (ret < 0) { - pr_perror("Can't setns %d/%s", pid, ns); + pr_perror("Can't setns %d/%s", pid, nd->str); goto err_set; } @@ -50,11 +50,11 @@ err_ns: return -1; } -int restore_ns(int rst, int type) +int restore_ns(int rst, struct ns_desc *nd) { int ret; - ret = setns(rst, type); + ret = setns(rst, nd->cflag); if (ret < 0) pr_perror("Can't restore ns back"); @@ -197,3 +197,8 @@ int try_show_namespaces(int ns_pid, struct cr_options *o) close_cr_fdset(&fdset); return 0; } + +struct ns_desc pid_ns_desc = { + .cflag = CLONE_NEWPID, + .str = "pid", +}; diff --git a/net.c b/net.c index 94fa7ae6f..b725aa0f1 100644 --- a/net.c +++ b/net.c @@ -339,7 +339,7 @@ int dump_net_ns(int pid, struct cr_fdset *fds) { int ret; - ret = switch_ns(pid, CLONE_NEWNET, "net", NULL); + ret = switch_ns(pid, &net_ns_desc, NULL); if (!ret) ret = dump_links(fds); if (!ret) @@ -402,3 +402,7 @@ void network_unlock(void) run_scripts("network-unlock"); } +struct ns_desc net_ns_desc = { + .cflag = CLONE_NEWNET, + .str = "net", +}; diff --git a/parasite-syscall.c b/parasite-syscall.c index c7a8b205f..af71878a2 100644 --- a/parasite-syscall.c +++ b/parasite-syscall.c @@ -20,6 +20,7 @@ #include "crtools.h" #include "namespaces.h" #include "pstree.h" +#include "net.h" #include #include @@ -300,7 +301,7 @@ static int parasite_init(struct parasite_ctl *ctl, pid_t pid, int nr_threads) if (opts.namespaces_flags & CLONE_NEWNET) { pr_info("Switching to %d's net for tsock creation\n", pid); - if (switch_ns(pid, CLONE_NEWNET, "net", &rst)) + if (switch_ns(pid, &net_ns_desc, &rst)) return -1; } @@ -315,7 +316,7 @@ static int parasite_init(struct parasite_ctl *ctl, pid_t pid, int nr_threads) goto err; } - if (rst > 0 && restore_ns(rst, CLONE_NEWNET) < 0) + if (rst > 0 && restore_ns(rst, &net_ns_desc) < 0) goto err; } else { struct sockaddr addr = { .sa_family = AF_UNSPEC, }; diff --git a/sockets.c b/sockets.c index 175d165a6..f5edd17e4 100644 --- a/sockets.c +++ b/sockets.c @@ -428,7 +428,7 @@ int collect_sockets(int pid) if (opts.namespaces_flags & CLONE_NEWNET) { pr_info("Switching to %d's net for collecting sockets\n", pid); - if (switch_ns(pid, CLONE_NEWNET, "net", &rst)) + if (switch_ns(pid, &net_ns_desc, &rst)) return -1; } @@ -521,7 +521,7 @@ int collect_sockets(int pid) close(nl); out: - if (rst > 0 && restore_ns(rst, CLONE_NEWNET) < 0) + if (rst > 0 && restore_ns(rst, &net_ns_desc) < 0) err = -1; return err; } diff --git a/uts_ns.c b/uts_ns.c index c7b82c7bb..7e2fff1fb 100644 --- a/uts_ns.c +++ b/uts_ns.c @@ -8,6 +8,7 @@ #include "syscall.h" #include "namespaces.h" #include "sysctl.h" +#include "uts_ns.h" #include "protobuf.h" #include "protobuf/utsns.pb-c.h" @@ -18,7 +19,7 @@ int dump_uts_ns(int ns_pid, struct cr_fdset *fdset) struct utsname ubuf; UtsnsEntry ue = UTSNS_ENTRY__INIT; - ret = switch_ns(ns_pid, CLONE_NEWUTS, "uts", NULL); + ret = switch_ns(ns_pid, &uts_ns_desc, NULL); if (ret < 0) return ret; @@ -68,3 +69,8 @@ void show_utsns(int fd, struct cr_options *o) { pb_show_vertical(fd, PB_UTSNS); } + +struct ns_desc uts_ns_desc = { + .cflag = CLONE_NEWUTS, + .str = "uts", +};