From 8ac80915e0408aa0448fe525cabb28beea244840 Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 22 Sep 2014 16:27:00 +0400 Subject: [PATCH] ns: Factor out namespace switching call Signed-off-by: Pavel Emelyanov --- include/ipc_ns.h | 2 +- include/net.h | 2 +- include/uts_ns.h | 2 +- ipc_ns.c | 6 +----- namespaces.c | 24 ++++++++++++++++-------- net.c | 6 ++---- uts_ns.c | 6 +----- 7 files changed, 23 insertions(+), 25 deletions(-) diff --git a/include/ipc_ns.h b/include/ipc_ns.h index a50281fc9..c89098926 100644 --- a/include/ipc_ns.h +++ b/include/ipc_ns.h @@ -1,7 +1,7 @@ #ifndef __CR_IPC_NS_H__ #define __CR_IPC_NS_H__ -extern int dump_ipc_ns(int ns_pid, int ns_id); +extern int dump_ipc_ns(int ns_id); extern int prepare_ipc_ns(int pid); extern struct ns_desc ipc_ns_desc; diff --git a/include/net.h b/include/net.h index be9d81ea4..8aed0436d 100644 --- a/include/net.h +++ b/include/net.h @@ -4,7 +4,7 @@ #include "list.h" struct cr_imgset; -extern int dump_net_ns(int pid, int ns_id); +extern int dump_net_ns(int ns_id); extern int prepare_net_ns(int pid); extern int netns_pre_create(void); diff --git a/include/uts_ns.h b/include/uts_ns.h index ff499ac65..ab054ffe8 100644 --- a/include/uts_ns.h +++ b/include/uts_ns.h @@ -1,7 +1,7 @@ #ifndef __CR_UTS_NS_H__ #define __CR_UTS_NS_H__ -extern int dump_uts_ns(int ns_pid, int ns_id); +extern int dump_uts_ns(int ns_id); extern int prepare_utsns(int pid); extern struct ns_desc uts_ns_desc; diff --git a/ipc_ns.c b/ipc_ns.c index 8ac429840..1e4985560 100644 --- a/ipc_ns.c +++ b/ipc_ns.c @@ -456,7 +456,7 @@ static int dump_ipc_data(const struct cr_imgset *imgset) return 0; } -int dump_ipc_ns(int ns_pid, int ns_id) +int dump_ipc_ns(int ns_id) { int ret; struct cr_imgset *imgset; @@ -465,10 +465,6 @@ int dump_ipc_ns(int ns_pid, int ns_id) if (imgset == NULL) return -1; - ret = switch_ns(ns_pid, &ipc_ns_desc, NULL); - if (ret < 0) - goto err; - ret = dump_ipc_data(imgset); if (ret < 0) { pr_err("Failed to write IPC namespace data\n"); diff --git a/namespaces.c b/namespaces.c index 8021cfffb..cbf86310e 100644 --- a/namespaces.c +++ b/namespaces.c @@ -424,27 +424,27 @@ int gen_predump_ns_mask(void) static int do_dump_namespaces(struct ns_id *ns) { - int ret = -1; + int ret; + + ret = switch_ns(ns->pid, ns->nd, NULL); + if (ret) + return ret; switch (ns->nd->cflag) { - case CLONE_NEWPID: - case CLONE_NEWNS: - ret = 0; - break; case CLONE_NEWUTS: pr_info("Dump UTS namespace %d via %d\n", ns->id, ns->pid); - ret = dump_uts_ns(ns->pid, ns->id); + ret = dump_uts_ns(ns->id); break; case CLONE_NEWIPC: pr_info("Dump IPC namespace %d via %d\n", ns->id, ns->pid); - ret = dump_ipc_ns(ns->pid, ns->id); + ret = dump_ipc_ns(ns->id); break; case CLONE_NEWNET: pr_info("Dump NET namespace info %d via %d\n", ns->id, ns->pid); - ret = dump_net_ns(ns->pid, ns->id); + ret = dump_net_ns(ns->id); break; default: pr_err("Unknown namespace flag %x", ns->nd->cflag); @@ -485,6 +485,14 @@ int dump_namespaces(struct pstree_item *item, unsigned int ns_flags) if (ns->pid == getpid()) continue; + switch (ns->nd->cflag) { + /* No data for pid namespaces to dump */ + case CLONE_NEWPID: + /* Dumped explicitly with dump_mnt_namespaces() */ + case CLONE_NEWNS: + continue; + } + pid = fork(); if (pid < 0) { pr_perror("Can't fork ns dumper"); diff --git a/net.c b/net.c index fdfd40b9e..1ad0186e3 100644 --- a/net.c +++ b/net.c @@ -533,7 +533,7 @@ static int mount_ns_sysfs(void) return ns_sysfs_fd >= 0 ? 0 : -1; } -int dump_net_ns(int pid, int ns_id) +int dump_net_ns(int ns_id) { struct cr_imgset *fds; int ret; @@ -542,9 +542,7 @@ int dump_net_ns(int pid, int ns_id) if (fds == NULL) return -1; - ret = switch_ns(pid, &net_ns_desc, NULL); - if (!ret) - ret = mount_ns_sysfs(); + ret = mount_ns_sysfs(); if (!ret) ret = dump_links(fds); if (!ret) diff --git a/uts_ns.c b/uts_ns.c index b17dde992..766cae888 100644 --- a/uts_ns.c +++ b/uts_ns.c @@ -12,7 +12,7 @@ #include "protobuf.h" #include "protobuf/utsns.pb-c.h" -int dump_uts_ns(int ns_pid, int ns_id) +int dump_uts_ns(int ns_id) { int ret; struct cr_img *img; @@ -23,10 +23,6 @@ int dump_uts_ns(int ns_pid, int ns_id) if (!img) return -1; - ret = switch_ns(ns_pid, &uts_ns_desc, NULL); - if (ret < 0) - goto err; - ret = uname(&ubuf); if (ret < 0) { pr_perror("Error calling uname");