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: Andrei Vagin <avagin@virtuozzo.com>
Signed-off-by: Pavel Emelyanov <xemul@virtuozzo.com>
This commit is contained in:
Andrei Vagin 2016-10-27 09:18:52 +03:00 committed by Pavel Emelyanov
parent 8837f0eea1
commit 6ecf660dfe
2 changed files with 17 additions and 8 deletions

View file

@ -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 *);

View file

@ -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;
}