Commit 3a1c7d1d authored by Pavel Emelyanov's avatar Pavel Emelyanov

ns: Introduce ns descriptors

These are structs that (now) tie together ns string
and the CLONE_ flag. It's nice to have one (some code
becomes simpler) and will help us with auto-namespaces
detection.
Signed-off-by: 's avatarPavel Emelyanov <xemul@parallels.com>
parent eb8f8c12
...@@ -8,7 +8,9 @@ extern void show_ipc_var(int fd, struct cr_options *); ...@@ -8,7 +8,9 @@ extern void show_ipc_var(int fd, struct cr_options *);
extern void show_ipc_shm(int fd, struct cr_options *); extern void show_ipc_shm(int fd, struct cr_options *);
extern void show_ipc_msg(int fd, struct cr_options *); extern void show_ipc_msg(int fd, struct cr_options *);
extern void show_ipc_sem(int fd, struct cr_options *); extern void show_ipc_sem(int fd, struct cr_options *);
extern int dump_ipc_ns(int ns_pid, struct cr_fdset *fdset); extern int dump_ipc_ns(int ns_pid, const struct cr_fdset *fdset);
extern int prepare_ipc_ns(int pid); extern int prepare_ipc_ns(int pid);
extern struct ns_desc ipc_ns_desc;
#endif /* __CR_IPC_NS_H__ */ #endif /* __CR_IPC_NS_H__ */
...@@ -22,4 +22,6 @@ struct mount_info; ...@@ -22,4 +22,6 @@ struct mount_info;
extern struct mount_info *lookup_mnt_id(unsigned int id); extern struct mount_info *lookup_mnt_id(unsigned int id);
extern struct mount_info *lookup_mnt_sdev(unsigned int s_dev); extern struct mount_info *lookup_mnt_sdev(unsigned int s_dev);
extern struct ns_desc mnt_ns_desc;
#endif /* __CR_MOUNT_H__ */ #endif /* __CR_MOUNT_H__ */
...@@ -8,7 +8,14 @@ int dump_namespaces(struct pid *pid, unsigned int ns_flags); ...@@ -8,7 +8,14 @@ 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 *rst);
int restore_ns(int rst, int type); struct ns_desc {
unsigned int cflag;
char *str;
};
int switch_ns(int pid, struct ns_desc *nd, int *rst);
int restore_ns(int rst, struct ns_desc *nd);
extern struct ns_desc pid_ns_desc;
#endif /* __CR_NS_H__ */ #endif /* __CR_NS_H__ */
...@@ -20,4 +20,6 @@ struct veth_pair { ...@@ -20,4 +20,6 @@ struct veth_pair {
extern int network_lock(void); extern int network_lock(void);
extern void network_unlock(void); extern void network_unlock(void);
extern struct ns_desc net_ns_desc;
#endif /* __CR_NET_H__ */ #endif /* __CR_NET_H__ */
...@@ -8,4 +8,6 @@ struct cr_options; ...@@ -8,4 +8,6 @@ struct cr_options;
void show_utsns(int fd, struct cr_options *); void show_utsns(int fd, struct cr_options *);
int prepare_utsns(int pid); int prepare_utsns(int pid);
extern struct ns_desc uts_ns_desc;
#endif /* __CR_UTS_NS_H__ */ #endif /* __CR_UTS_NS_H__ */
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
#include "syscall.h" #include "syscall.h"
#include "namespaces.h" #include "namespaces.h"
#include "sysctl.h" #include "sysctl.h"
#include "ipc_ns.h"
#include "protobuf.h" #include "protobuf.h"
#include "protobuf/ipc-var.pb-c.h" #include "protobuf/ipc-var.pb-c.h"
...@@ -436,7 +437,7 @@ int dump_ipc_ns(int ns_pid, const struct cr_fdset *fdset) ...@@ -436,7 +437,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", NULL); ret = switch_ns(ns_pid, &ipc_ns_desc, NULL);
if (ret < 0) if (ret < 0)
return ret; return ret;
...@@ -873,3 +874,8 @@ int prepare_ipc_ns(int pid) ...@@ -873,3 +874,8 @@ int prepare_ipc_ns(int pid)
return ret; return ret;
return 0; return 0;
} }
struct ns_desc ipc_ns_desc = {
.cflag = CLONE_NEWIPC,
.str = "ipc",
};
...@@ -19,7 +19,7 @@ ...@@ -19,7 +19,7 @@
#include "mount.h" #include "mount.h"
#include "proc_parse.h" #include "proc_parse.h"
#include "image.h" #include "image.h"
#include "namespaces.h"
#include "protobuf.h" #include "protobuf.h"
#include "protobuf/mnt.pb-c.h" #include "protobuf/mnt.pb-c.h"
...@@ -763,3 +763,8 @@ int mntns_collect_root(pid_t pid) ...@@ -763,3 +763,8 @@ int mntns_collect_root(pid_t pid)
return 0; return 0;
} }
struct ns_desc mnt_ns_desc = {
.cflag = CLONE_NEWNS,
.str = "mnt",
};
...@@ -10,13 +10,13 @@ ...@@ -10,13 +10,13 @@
#include "namespaces.h" #include "namespaces.h"
#include "net.h" #include "net.h"
int switch_ns(int pid, int type, char *ns, int *rst) int switch_ns(int pid, struct ns_desc *nd, int *rst)
{ {
char buf[32]; char buf[32];
int nsfd; int nsfd;
int ret = -1; int ret = -1;
snprintf(buf, sizeof(buf), "/proc/%d/ns/%s", pid, ns); snprintf(buf, sizeof(buf), "/proc/%d/ns/%s", pid, nd->str);
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");
...@@ -24,7 +24,7 @@ int switch_ns(int pid, int type, char *ns, int *rst) ...@@ -24,7 +24,7 @@ int switch_ns(int pid, int type, char *ns, int *rst)
} }
if (rst) { if (rst) {
snprintf(buf, sizeof(buf), "/proc/self/ns/%s", ns); snprintf(buf, sizeof(buf), "/proc/self/ns/%s", nd->str);
*rst = open(buf, O_RDONLY); *rst = open(buf, O_RDONLY);
if (*rst < 0) { if (*rst < 0) {
pr_perror("Can't open ns file"); pr_perror("Can't open ns file");
...@@ -32,9 +32,9 @@ int switch_ns(int pid, int type, char *ns, int *rst) ...@@ -32,9 +32,9 @@ int switch_ns(int pid, int type, char *ns, int *rst)
} }
} }
ret = setns(nsfd, type); ret = setns(nsfd, nd->cflag);
if (ret < 0) { if (ret < 0) {
pr_perror("Can't setns %d/%s", pid, ns); pr_perror("Can't setns %d/%s", pid, nd->str);
goto err_set; goto err_set;
} }
...@@ -50,11 +50,11 @@ err_ns: ...@@ -50,11 +50,11 @@ err_ns:
return -1; return -1;
} }
int restore_ns(int rst, int type) int restore_ns(int rst, struct ns_desc *nd)
{ {
int ret; int ret;
ret = setns(rst, type); ret = setns(rst, nd->cflag);
if (ret < 0) if (ret < 0)
pr_perror("Can't restore ns back"); pr_perror("Can't restore ns back");
...@@ -197,3 +197,8 @@ int try_show_namespaces(int ns_pid, struct cr_options *o) ...@@ -197,3 +197,8 @@ int try_show_namespaces(int ns_pid, struct cr_options *o)
close_cr_fdset(&fdset); close_cr_fdset(&fdset);
return 0; return 0;
} }
struct ns_desc pid_ns_desc = {
.cflag = CLONE_NEWPID,
.str = "pid",
};
...@@ -339,7 +339,7 @@ int dump_net_ns(int pid, struct cr_fdset *fds) ...@@ -339,7 +339,7 @@ int dump_net_ns(int pid, struct cr_fdset *fds)
{ {
int ret; int ret;
ret = switch_ns(pid, CLONE_NEWNET, "net", NULL); ret = switch_ns(pid, &net_ns_desc, NULL);
if (!ret) if (!ret)
ret = dump_links(fds); ret = dump_links(fds);
if (!ret) if (!ret)
...@@ -402,3 +402,7 @@ void network_unlock(void) ...@@ -402,3 +402,7 @@ void network_unlock(void)
run_scripts("network-unlock"); run_scripts("network-unlock");
} }
struct ns_desc net_ns_desc = {
.cflag = CLONE_NEWNET,
.str = "net",
};
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
#include "crtools.h" #include "crtools.h"
#include "namespaces.h" #include "namespaces.h"
#include "pstree.h" #include "pstree.h"
#include "net.h"
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
...@@ -300,7 +301,7 @@ static int parasite_init(struct parasite_ctl *ctl, pid_t pid, int nr_threads) ...@@ -300,7 +301,7 @@ static int parasite_init(struct parasite_ctl *ctl, pid_t pid, int nr_threads)
if (opts.namespaces_flags & CLONE_NEWNET) { if (opts.namespaces_flags & CLONE_NEWNET) {
pr_info("Switching to %d's net for tsock creation\n", pid); pr_info("Switching to %d's net for tsock creation\n", pid);
if (switch_ns(pid, CLONE_NEWNET, "net", &rst)) if (switch_ns(pid, &net_ns_desc, &rst))
return -1; return -1;
} }
...@@ -315,7 +316,7 @@ static int parasite_init(struct parasite_ctl *ctl, pid_t pid, int nr_threads) ...@@ -315,7 +316,7 @@ static int parasite_init(struct parasite_ctl *ctl, pid_t pid, int nr_threads)
goto err; goto err;
} }
if (rst > 0 && restore_ns(rst, CLONE_NEWNET) < 0) if (rst > 0 && restore_ns(rst, &net_ns_desc) < 0)
goto err; goto err;
} else { } else {
struct sockaddr addr = { .sa_family = AF_UNSPEC, }; struct sockaddr addr = { .sa_family = AF_UNSPEC, };
......
...@@ -428,7 +428,7 @@ int collect_sockets(int pid) ...@@ -428,7 +428,7 @@ int collect_sockets(int pid)
if (opts.namespaces_flags & CLONE_NEWNET) { if (opts.namespaces_flags & CLONE_NEWNET) {
pr_info("Switching to %d's net for collecting sockets\n", pid); pr_info("Switching to %d's net for collecting sockets\n", pid);
if (switch_ns(pid, CLONE_NEWNET, "net", &rst)) if (switch_ns(pid, &net_ns_desc, &rst))
return -1; return -1;
} }
...@@ -521,7 +521,7 @@ int collect_sockets(int pid) ...@@ -521,7 +521,7 @@ int collect_sockets(int pid)
close(nl); close(nl);
out: out:
if (rst > 0 && restore_ns(rst, CLONE_NEWNET) < 0) if (rst > 0 && restore_ns(rst, &net_ns_desc) < 0)
err = -1; err = -1;
return err; return err;
} }
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include "syscall.h" #include "syscall.h"
#include "namespaces.h" #include "namespaces.h"
#include "sysctl.h" #include "sysctl.h"
#include "uts_ns.h"
#include "protobuf.h" #include "protobuf.h"
#include "protobuf/utsns.pb-c.h" #include "protobuf/utsns.pb-c.h"
...@@ -18,7 +19,7 @@ int dump_uts_ns(int ns_pid, struct cr_fdset *fdset) ...@@ -18,7 +19,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", NULL); ret = switch_ns(ns_pid, &uts_ns_desc, NULL);
if (ret < 0) if (ret < 0)
return ret; return ret;
...@@ -68,3 +69,8 @@ void show_utsns(int fd, struct cr_options *o) ...@@ -68,3 +69,8 @@ void show_utsns(int fd, struct cr_options *o)
{ {
pb_show_vertical(fd, PB_UTSNS); pb_show_vertical(fd, PB_UTSNS);
} }
struct ns_desc uts_ns_desc = {
.cflag = CLONE_NEWUTS,
.str = "uts",
};
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