Commit 9353051b authored by Pavel Emelyanov's avatar Pavel Emelyanov

ns: Check ns type with type field

Actually make use of the ns->type field and remove all getpid()'s
and other strange/inconsistent checks.
Signed-off-by: 's avatarPavel Emelyanov <xemul@parallels.com>
parent 22b72566
...@@ -99,7 +99,8 @@ static int check_sock_diag(void) ...@@ -99,7 +99,8 @@ static int check_sock_diag(void)
int ret; int ret;
struct ns_id ns; struct ns_id ns;
ns.pid = 0; ns.ns_pid = 0;
ns.type = NS_CRIU;
ns.net.nlsk = socket(PF_NETLINK, SOCK_RAW, NETLINK_SOCK_DIAG); ns.net.nlsk = socket(PF_NETLINK, SOCK_RAW, NETLINK_SOCK_DIAG);
if (ns.net.nlsk < 0) { if (ns.net.nlsk < 0) {
pr_perror("Can't make diag socket for check"); pr_perror("Can't make diag socket for check");
...@@ -757,7 +758,7 @@ static int (*chk_feature)(void); ...@@ -757,7 +758,7 @@ static int (*chk_feature)(void);
int cr_check(void) int cr_check(void)
{ {
struct ns_id ns = { .pid = getpid(), .nd = &mnt_ns_desc }; struct ns_id ns = { .type = NS_CRIU, .ns_pid = PROC_SELF, .nd = &mnt_ns_desc };
int ret = 0; int ret = 0;
if (!is_root_user()) if (!is_root_user())
......
...@@ -19,7 +19,7 @@ enum ns_type { ...@@ -19,7 +19,7 @@ enum ns_type {
struct ns_id { struct ns_id {
unsigned int kid; unsigned int kid;
unsigned int id; unsigned int id;
pid_t pid; pid_t ns_pid;
struct ns_desc *nd; struct ns_desc *nd;
struct ns_id *next; struct ns_id *next;
enum ns_type type; enum ns_type type;
......
...@@ -807,11 +807,10 @@ static struct mount_info *find_best_external_match(struct mount_info *list, stru ...@@ -807,11 +807,10 @@ static struct mount_info *find_best_external_match(struct mount_info *list, stru
static struct ns_id *find_ext_ns_id(void) static struct ns_id *find_ext_ns_id(void)
{ {
int pid = getpid();
struct ns_id *ns; struct ns_id *ns;
for (ns = ns_ids; ns->next; ns = ns->next) for (ns = ns_ids; ns->next; ns = ns->next)
if (ns->pid == pid && ns->nd == &mnt_ns_desc) { if (ns->type == NS_CRIU && ns->nd == &mnt_ns_desc) {
if (!ns->mnt.mntinfo_list && if (!ns->mnt.mntinfo_list &&
!collect_mntinfo(ns, true)) !collect_mntinfo(ns, true))
break; break;
...@@ -1653,9 +1652,9 @@ struct mount_info *collect_mntinfo(struct ns_id *ns, bool for_dump) ...@@ -1653,9 +1652,9 @@ struct mount_info *collect_mntinfo(struct ns_id *ns, bool for_dump)
{ {
struct mount_info *pm; struct mount_info *pm;
pm = parse_mountinfo(ns->pid, ns, for_dump); pm = parse_mountinfo(ns->ns_pid, ns, for_dump);
if (!pm) { if (!pm) {
pr_err("Can't parse %d's mountinfo\n", ns->pid); pr_err("Can't parse %d's mountinfo\n", ns->ns_pid);
return NULL; return NULL;
} }
...@@ -2449,7 +2448,7 @@ static int collect_mnt_from_image(struct mount_info **pms, struct ns_id *nsid) ...@@ -2449,7 +2448,7 @@ static int collect_mnt_from_image(struct mount_info **pms, struct ns_id *nsid)
if (!img) if (!img)
return -1; return -1;
if (nsid->id != root_item->ids->mnt_ns_id) if (nsid->type == NS_OTHER)
root_len = print_ns_root(nsid, root, sizeof(root)); root_len = print_ns_root(nsid, root, sizeof(root));
pr_debug("Reading mountpoint images\n"); pr_debug("Reading mountpoint images\n");
...@@ -2536,13 +2535,15 @@ static int read_mnt_ns_img(void) ...@@ -2536,13 +2535,15 @@ static int read_mnt_ns_img(void)
if (nsid->nd != &mnt_ns_desc) if (nsid->nd != &mnt_ns_desc)
continue; continue;
if (nsid->id != root_item->ids->mnt_ns_id) if (nsid->type != NS_ROOT) {
BUG_ON(nsid->type == NS_CRIU);
/* /*
* If we have more than one (root) namespace, * If we have more than one (root) namespace,
* then we'll need the roots yard. * then we'll need the roots yard.
*/ */
if (create_mnt_roots()) if (create_mnt_roots())
return -1; return -1;
}
if (collect_mnt_from_image(&pms, nsid)) if (collect_mnt_from_image(&pms, nsid))
return -1; return -1;
...@@ -2567,22 +2568,21 @@ char *rst_get_mnt_root(int mnt_id) ...@@ -2567,22 +2568,21 @@ char *rst_get_mnt_root(int mnt_id)
if (m == NULL) if (m == NULL)
return NULL; return NULL;
if (m->nsid->pid == getpid()) if (m->nsid->type == NS_OTHER)
return path; print_ns_root(m->nsid, path, sizeof(path));
print_ns_root(m->nsid, path, sizeof(path));
return path; return path;
} }
static int do_restore_task_mnt_ns(struct ns_id *nsid) static int do_restore_task_mnt_ns(struct ns_id *nsid, struct pstree_item *current)
{ {
char path[PATH_MAX]; char path[PATH_MAX];
if (nsid->pid != getpid()) { if (nsid->ns_pid != current->pid.virt) {
int fd; int fd;
futex_wait_while_eq(&nsid->ns_created, 0); futex_wait_while_eq(&nsid->ns_created, 0);
fd = open_proc(nsid->pid, "ns/mnt"); fd = open_proc(nsid->ns_pid, "ns/mnt");
if (fd < 0) if (fd < 0)
return -1; return -1;
...@@ -2633,7 +2633,9 @@ int restore_task_mnt_ns(struct pstree_item *current) ...@@ -2633,7 +2633,9 @@ int restore_task_mnt_ns(struct pstree_item *current)
return -1; return -1;
} }
if (do_restore_task_mnt_ns(nsid)) BUG_ON(nsid->type != NS_OTHER);
if (do_restore_task_mnt_ns(nsid, current))
return -1; return -1;
} }
...@@ -2749,7 +2751,7 @@ int prepare_mnt_ns(void) ...@@ -2749,7 +2751,7 @@ int prepare_mnt_ns(void)
{ {
int ret = -1; int ret = -1;
struct mount_info *old; struct mount_info *old;
struct ns_id ns = { .pid = PROC_SELF, .nd = &mnt_ns_desc }; struct ns_id ns = { .type = NS_CRIU, .ns_pid = PROC_SELF, .nd = &mnt_ns_desc };
if (!(root_ns_mask & CLONE_NEWNS)) if (!(root_ns_mask & CLONE_NEWNS))
return rst_collect_local_mntns(); return rst_collect_local_mntns();
...@@ -2889,7 +2891,7 @@ set_root: ...@@ -2889,7 +2891,7 @@ set_root:
int mntns_get_root_fd(struct ns_id *mntns) int mntns_get_root_fd(struct ns_id *mntns)
{ {
return __mntns_get_root_fd(mntns->pid); return __mntns_get_root_fd(mntns->ns_pid);
} }
struct ns_id *lookup_nsid_by_mnt_id(int mnt_id) struct ns_id *lookup_nsid_by_mnt_id(int mnt_id)
...@@ -2933,7 +2935,7 @@ static int collect_mntns(struct ns_id *ns, void *__arg) ...@@ -2933,7 +2935,7 @@ static int collect_mntns(struct ns_id *ns, void *__arg)
if (!pms) if (!pms)
return -1; return -1;
if (arg->for_dump && ns->pid != getpid()) if (arg->for_dump && ns->type != NS_CRIU)
arg->need_to_validate = true; arg->need_to_validate = true;
mntinfo_add_list(pms); mntinfo_add_list(pms);
...@@ -2973,19 +2975,15 @@ err: ...@@ -2973,19 +2975,15 @@ err:
int dump_mnt_namespaces(void) int dump_mnt_namespaces(void)
{ {
struct ns_id *nsid; struct ns_id *nsid;
int n = 0;
if (!(root_ns_mask & CLONE_NEWNS)) if (!(root_ns_mask & CLONE_NEWNS))
return 0; return 0;
for (nsid = ns_ids; nsid != NULL; nsid = nsid->next) { for (nsid = ns_ids; nsid != NULL; nsid = nsid->next) {
if (nsid->nd != &mnt_ns_desc) if (nsid->nd != &mnt_ns_desc || nsid->type == NS_CRIU)
continue;
if (nsid->pid == getpid())
continue; continue;
if (++n == 2 && check_mnt_id()) { if ((nsid->type == NS_OTHER) && check_mnt_id()) {
pr_err("Nested mount namespaces are not supported " pr_err("Nested mount namespaces are not supported "
"without mnt_id in fdinfo\n"); "without mnt_id in fdinfo\n");
return -1; return -1;
......
...@@ -128,11 +128,11 @@ static void nsid_add(struct ns_id *ns, struct ns_desc *nd, unsigned int id, pid_ ...@@ -128,11 +128,11 @@ static void nsid_add(struct ns_id *ns, struct ns_desc *nd, unsigned int id, pid_
{ {
ns->nd = nd; ns->nd = nd;
ns->id = id; ns->id = id;
ns->pid = pid; ns->ns_pid = pid;
ns->next = ns_ids; ns->next = ns_ids;
ns_ids = ns; ns_ids = ns;
pr_info("Add %s ns %d pid %d\n", nd->str, ns->id, ns->pid); pr_info("Add %s ns %d pid %d\n", nd->str, ns->id, ns->ns_pid);
} }
static struct ns_id *__rst_new_ns_id(unsigned int id, pid_t pid, static struct ns_id *__rst_new_ns_id(unsigned int id, pid_t pid,
...@@ -162,8 +162,8 @@ int rst_add_ns_id(unsigned int id, struct pstree_item *i, struct ns_desc *nd) ...@@ -162,8 +162,8 @@ int rst_add_ns_id(unsigned int id, struct pstree_item *i, struct ns_desc *nd)
nsid = lookup_ns_by_id(id, nd); nsid = lookup_ns_by_id(id, nd);
if (nsid) { if (nsid) {
if (pid_rst_prio(pid, nsid->pid)) if (pid_rst_prio(pid, nsid->ns_pid))
nsid->pid = pid; nsid->ns_pid = pid;
return 0; return 0;
} }
...@@ -221,7 +221,7 @@ int walk_namespaces(struct ns_desc *nd, int (*cb)(struct ns_id *, void *), void ...@@ -221,7 +221,7 @@ int walk_namespaces(struct ns_desc *nd, int (*cb)(struct ns_id *, void *), void
if (ns->nd != nd) if (ns->nd != nd)
continue; continue;
if (ns->pid == getpid()) { if (ns->type == NS_CRIU) {
if (root_ns_mask & nd->cflag) if (root_ns_mask & nd->cflag)
continue; continue;
...@@ -719,24 +719,24 @@ static int do_dump_namespaces(struct ns_id *ns) ...@@ -719,24 +719,24 @@ static int do_dump_namespaces(struct ns_id *ns)
{ {
int ret; int ret;
ret = switch_ns(ns->pid, ns->nd, NULL); ret = switch_ns(ns->ns_pid, ns->nd, NULL);
if (ret) if (ret)
return ret; return ret;
switch (ns->nd->cflag) { switch (ns->nd->cflag) {
case CLONE_NEWUTS: case CLONE_NEWUTS:
pr_info("Dump UTS namespace %d via %d\n", pr_info("Dump UTS namespace %d via %d\n",
ns->id, ns->pid); ns->id, ns->ns_pid);
ret = dump_uts_ns(ns->id); ret = dump_uts_ns(ns->id);
break; break;
case CLONE_NEWIPC: case CLONE_NEWIPC:
pr_info("Dump IPC namespace %d via %d\n", pr_info("Dump IPC namespace %d via %d\n",
ns->id, ns->pid); ns->id, ns->ns_pid);
ret = dump_ipc_ns(ns->id); ret = dump_ipc_ns(ns->id);
break; break;
case CLONE_NEWNET: case CLONE_NEWNET:
pr_info("Dump NET namespace info %d via %d\n", pr_info("Dump NET namespace info %d via %d\n",
ns->id, ns->pid); ns->id, ns->ns_pid);
ret = dump_net_ns(ns->id); ret = dump_net_ns(ns->id);
break; break;
default: default:
...@@ -775,7 +775,7 @@ int dump_namespaces(struct pstree_item *item, unsigned int ns_flags) ...@@ -775,7 +775,7 @@ int dump_namespaces(struct pstree_item *item, unsigned int ns_flags)
for (ns = ns_ids; ns; ns = ns->next) { for (ns = ns_ids; ns; ns = ns->next) {
/* Skip current namespaces, which are in the list too */ /* Skip current namespaces, which are in the list too */
if (ns->pid == getpid()) if (ns->type == NS_CRIU)
continue; continue;
switch (ns->nd->cflag) { switch (ns->nd->cflag) {
......
...@@ -827,9 +827,9 @@ static int prep_ns_sockets(struct ns_id *ns, bool for_dump) ...@@ -827,9 +827,9 @@ static int prep_ns_sockets(struct ns_id *ns, bool for_dump)
{ {
int nsret = -1, ret; int nsret = -1, ret;
if (ns->pid != getpid()) { if (ns->type != NS_CRIU) {
pr_info("Switching to %d's net for collecting sockets\n", ns->pid); pr_info("Switching to %d's net for collecting sockets\n", ns->ns_pid);
if (switch_ns(ns->pid, &net_ns_desc, &nsret)) if (switch_ns(ns->ns_pid, &net_ns_desc, &nsret))
return -1; return -1;
} }
...@@ -872,7 +872,7 @@ static int collect_net_ns(struct ns_id *ns, void *oarg) ...@@ -872,7 +872,7 @@ static int collect_net_ns(struct ns_id *ns, void *oarg)
bool for_dump = (oarg == (void *)1); bool for_dump = (oarg == (void *)1);
int ret; int ret;
pr_info("Collecting netns %d/%d\n", ns->id, ns->pid); pr_info("Collecting netns %d/%d\n", ns->id, ns->ns_pid);
ret = prep_ns_sockets(ns, for_dump); ret = prep_ns_sockets(ns, for_dump);
if (ret) if (ret)
return ret; return ret;
......
...@@ -698,7 +698,7 @@ int collect_sockets(struct ns_id *ns) ...@@ -698,7 +698,7 @@ int collect_sockets(struct ns_id *ns)
tmp = do_collect_req(nl, &req, sizeof(req), packet_receive_one, NULL); tmp = do_collect_req(nl, &req, sizeof(req), packet_receive_one, NULL);
if (tmp) { if (tmp) {
pr_warn("The current kernel doesn't support packet_diag\n"); pr_warn("The current kernel doesn't support packet_diag\n");
if (ns->pid == 0 || tmp != -ENOENT) /* Fedora 19 */ if (ns->ns_pid == 0 || tmp != -ENOENT) /* Fedora 19 */
err = tmp; err = tmp;
} }
...@@ -708,7 +708,7 @@ int collect_sockets(struct ns_id *ns) ...@@ -708,7 +708,7 @@ int collect_sockets(struct ns_id *ns)
tmp = do_collect_req(nl, &req, sizeof(req), netlink_receive_one, NULL); tmp = do_collect_req(nl, &req, sizeof(req), netlink_receive_one, NULL);
if (tmp) { if (tmp) {
pr_warn("The current kernel doesn't support netlink_diag\n"); pr_warn("The current kernel doesn't support netlink_diag\n");
if (ns->pid == 0 || tmp != -ENOENT) /* Fedora 19 */ if (ns->ns_pid == 0 || tmp != -ENOENT) /* Fedora 19 */
err = tmp; err = tmp;
} }
...@@ -716,7 +716,7 @@ int collect_sockets(struct ns_id *ns) ...@@ -716,7 +716,7 @@ int collect_sockets(struct ns_id *ns)
close(nl); close(nl);
ns->net.nlsk = -1; ns->net.nlsk = -1;
if (err && (ns->pid == getpid())) { if (err && (ns->type == NS_CRIU)) {
/* /*
* If netns isn't dumped, criu will fail only * If netns isn't dumped, criu will fail only
* if an unsupported socket will be really dumped. * if an unsupported socket will be really dumped.
......
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