Commit 2d56d1b0 authored by Pavel Emelyanov's avatar Pavel Emelyanov

ns: Add ability to save original ns and restoring it back while switcing

This will be required for parasite transport socket creation -- it will
have to be created in a net ns we're putting parasite in and then we'll
have to restore it back to original to go on dumping.
Signed-off-by: 's avatarPavel Emelyanov <xemul@parallels.com>
parent 9eda83b7
...@@ -7,5 +7,6 @@ int dump_namespaces(struct pid *pid, unsigned int ns_flags); ...@@ -7,5 +7,6 @@ int dump_namespaces(struct pid *pid, unsigned int ns_flags);
int prepare_namespace(int pid, unsigned long clone_flags); int prepare_namespace(int pid, unsigned long clone_flags);
struct cr_options; struct cr_options;
int try_show_namespaces(int pid, struct cr_options *); int try_show_namespaces(int pid, struct cr_options *);
int switch_ns(int pid, int type, char *ns); int switch_ns(int pid, int type, char *ns, int *rst);
int restore_ns(int rst, int type);
#endif #endif
...@@ -456,7 +456,7 @@ int dump_ipc_ns(int ns_pid, const struct cr_fdset *fdset) ...@@ -456,7 +456,7 @@ int dump_ipc_ns(int ns_pid, const struct cr_fdset *fdset)
{ {
int ret; int ret;
ret = switch_ns(ns_pid, CLONE_NEWIPC, "ipc"); ret = switch_ns(ns_pid, CLONE_NEWIPC, "ipc", NULL);
if (ret < 0) if (ret < 0)
return ret; return ret;
......
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
#include "mount.h" #include "mount.h"
#include "namespaces.h" #include "namespaces.h"
int switch_ns(int pid, int type, char *ns) int switch_ns(int pid, int type, char *ns, int *rst)
{ {
char buf[32]; char buf[32];
int nsfd; int nsfd;
...@@ -19,15 +19,44 @@ int switch_ns(int pid, int type, char *ns) ...@@ -19,15 +19,44 @@ int switch_ns(int pid, int type, char *ns)
nsfd = open(buf, O_RDONLY); nsfd = open(buf, O_RDONLY);
if (nsfd < 0) { if (nsfd < 0) {
pr_perror("Can't open ipcns file"); pr_perror("Can't open ipcns file");
goto out; goto err_ns;
}
if (rst) {
snprintf(buf, sizeof(buf), "/proc/self/ns/%s", ns);
*rst = open(buf, O_RDONLY);
if (*rst < 0) {
pr_perror("Can't open ns file");
goto err_rst;
}
} }
ret = setns(nsfd, type); ret = setns(nsfd, type);
if (ret < 0) if (ret < 0) {
pr_perror("Can't setns %d/%s", pid, ns); pr_perror("Can't setns %d/%s", pid, ns);
goto err_set;
}
close(nsfd);
return 0;
err_set:
if (rst)
close(*rst);
err_rst:
close(nsfd); close(nsfd);
out: err_ns:
return -1;
}
int restore_ns(int rst, int type)
{
int ret;
ret = setns(rst, type);
if (ret < 0)
pr_perror("Can't restore ns back");
return ret; return ret;
} }
......
...@@ -18,7 +18,7 @@ int dump_uts_ns(int ns_pid, struct cr_fdset *fdset) ...@@ -18,7 +18,7 @@ int dump_uts_ns(int ns_pid, struct cr_fdset *fdset)
struct utsname ubuf; struct utsname ubuf;
UtsnsEntry ue = UTSNS_ENTRY__INIT; UtsnsEntry ue = UTSNS_ENTRY__INIT;
ret = switch_ns(ns_pid, CLONE_NEWUTS, "uts"); ret = switch_ns(ns_pid, CLONE_NEWUTS, "uts", NULL);
if (ret < 0) if (ret < 0)
return ret; return ret;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment