Commit 6ecf660d authored by Andrei Vagin's avatar Andrei Vagin Committed by Pavel Emelyanov

namespaces: add switch_ns_by_fd

It's like switch_ns, but it gets a namespace file descriptor instead of pid.

travis-ci: success for net: simplify restore of macvlan-s (rev2)
Signed-off-by: 's avatarAndrei Vagin <avagin@virtuozzo.com>
Signed-off-by: 's avatarPavel Emelyanov <xemul@virtuozzo.com>
parent 8837f0ee
......@@ -127,6 +127,7 @@ extern int prepare_namespace_before_tasks(void);
extern int prepare_namespace(struct pstree_item *item, unsigned long clone_flags);
extern int switch_ns(int pid, struct ns_desc *nd, int *rst);
extern int switch_ns_by_fd(int nsfd, struct ns_desc *nd, int *rst);
extern int restore_ns(int rst, struct ns_desc *nd);
extern int dump_task_ns_ids(struct pstree_item *);
......
......@@ -221,39 +221,47 @@ bool check_ns_proc(struct fd_link *link)
int switch_ns(int pid, struct ns_desc *nd, int *rst)
{
char buf[32];
int nsfd;
int ret = -1;
int ret;
nsfd = open_proc(pid, "ns/%s", nd->str);
if (nsfd < 0) {
pr_perror("Can't open ns file");
goto err_ns;
return -1;
}
ret = switch_ns_by_fd(nsfd, nd, rst);
close(nsfd);
return ret;
}
int switch_ns_by_fd(int nsfd, struct ns_desc *nd, int *rst)
{
char buf[32];
int ret = -1;
if (rst) {
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");
goto err_rst;
goto err_ns;
}
}
ret = setns(nsfd, nd->cflag);
if (ret < 0) {
pr_perror("Can't setns %d/%s", pid, nd->str);
pr_perror("Can't setns %d/%s", nsfd, nd->str);
goto err_set;
}
close(nsfd);
return 0;
err_set:
if (rst)
close(*rst);
err_rst:
close(nsfd);
err_ns:
return -1;
}
......
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