mirror of
https://github.com/checkpoint-restore/criu
synced 2025-09-03 07:45:17 +00:00
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: Pavel Emelyanov <xemul@parallels.com>
This commit is contained in:
@@ -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_msg(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 struct ns_desc ipc_ns_desc;
|
||||
|
||||
#endif /* __CR_IPC_NS_H__ */
|
||||
|
@@ -22,4 +22,6 @@ struct mount_info;
|
||||
extern struct mount_info *lookup_mnt_id(unsigned int id);
|
||||
extern struct mount_info *lookup_mnt_sdev(unsigned int s_dev);
|
||||
|
||||
extern struct ns_desc mnt_ns_desc;
|
||||
|
||||
#endif /* __CR_MOUNT_H__ */
|
||||
|
@@ -8,7 +8,14 @@ int dump_namespaces(struct pid *pid, unsigned int ns_flags);
|
||||
int prepare_namespace(int pid, unsigned long clone_flags);
|
||||
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__ */
|
||||
|
@@ -20,4 +20,6 @@ struct veth_pair {
|
||||
extern int network_lock(void);
|
||||
extern void network_unlock(void);
|
||||
|
||||
extern struct ns_desc net_ns_desc;
|
||||
|
||||
#endif /* __CR_NET_H__ */
|
||||
|
@@ -8,4 +8,6 @@ struct cr_options;
|
||||
void show_utsns(int fd, struct cr_options *);
|
||||
int prepare_utsns(int pid);
|
||||
|
||||
extern struct ns_desc uts_ns_desc;
|
||||
|
||||
#endif /* __CR_UTS_NS_H__ */
|
||||
|
8
ipc_ns.c
8
ipc_ns.c
@@ -12,6 +12,7 @@
|
||||
#include "syscall.h"
|
||||
#include "namespaces.h"
|
||||
#include "sysctl.h"
|
||||
#include "ipc_ns.h"
|
||||
|
||||
#include "protobuf.h"
|
||||
#include "protobuf/ipc-var.pb-c.h"
|
||||
@@ -436,7 +437,7 @@ int dump_ipc_ns(int ns_pid, const struct cr_fdset *fdset)
|
||||
{
|
||||
int ret;
|
||||
|
||||
ret = switch_ns(ns_pid, CLONE_NEWIPC, "ipc", NULL);
|
||||
ret = switch_ns(ns_pid, &ipc_ns_desc, NULL);
|
||||
if (ret < 0)
|
||||
return ret;
|
||||
|
||||
@@ -873,3 +874,8 @@ int prepare_ipc_ns(int pid)
|
||||
return ret;
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct ns_desc ipc_ns_desc = {
|
||||
.cflag = CLONE_NEWIPC,
|
||||
.str = "ipc",
|
||||
};
|
||||
|
7
mount.c
7
mount.c
@@ -19,7 +19,7 @@
|
||||
#include "mount.h"
|
||||
#include "proc_parse.h"
|
||||
#include "image.h"
|
||||
|
||||
#include "namespaces.h"
|
||||
#include "protobuf.h"
|
||||
#include "protobuf/mnt.pb-c.h"
|
||||
|
||||
@@ -763,3 +763,8 @@ int mntns_collect_root(pid_t pid)
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct ns_desc mnt_ns_desc = {
|
||||
.cflag = CLONE_NEWNS,
|
||||
.str = "mnt",
|
||||
};
|
||||
|
19
namespaces.c
19
namespaces.c
@@ -10,13 +10,13 @@
|
||||
#include "namespaces.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];
|
||||
int nsfd;
|
||||
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);
|
||||
if (nsfd < 0) {
|
||||
pr_perror("Can't open ipcns file");
|
||||
@@ -24,7 +24,7 @@ int switch_ns(int pid, int type, char *ns, int *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);
|
||||
if (*rst < 0) {
|
||||
pr_perror("Can't open ns file");
|
||||
@@ -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) {
|
||||
pr_perror("Can't setns %d/%s", pid, ns);
|
||||
pr_perror("Can't setns %d/%s", pid, nd->str);
|
||||
goto err_set;
|
||||
}
|
||||
|
||||
@@ -50,11 +50,11 @@ err_ns:
|
||||
return -1;
|
||||
}
|
||||
|
||||
int restore_ns(int rst, int type)
|
||||
int restore_ns(int rst, struct ns_desc *nd)
|
||||
{
|
||||
int ret;
|
||||
|
||||
ret = setns(rst, type);
|
||||
ret = setns(rst, nd->cflag);
|
||||
if (ret < 0)
|
||||
pr_perror("Can't restore ns back");
|
||||
|
||||
@@ -197,3 +197,8 @@ int try_show_namespaces(int ns_pid, struct cr_options *o)
|
||||
close_cr_fdset(&fdset);
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct ns_desc pid_ns_desc = {
|
||||
.cflag = CLONE_NEWPID,
|
||||
.str = "pid",
|
||||
};
|
||||
|
6
net.c
6
net.c
@@ -339,7 +339,7 @@ int dump_net_ns(int pid, struct cr_fdset *fds)
|
||||
{
|
||||
int ret;
|
||||
|
||||
ret = switch_ns(pid, CLONE_NEWNET, "net", NULL);
|
||||
ret = switch_ns(pid, &net_ns_desc, NULL);
|
||||
if (!ret)
|
||||
ret = dump_links(fds);
|
||||
if (!ret)
|
||||
@@ -402,3 +402,7 @@ void network_unlock(void)
|
||||
run_scripts("network-unlock");
|
||||
}
|
||||
|
||||
struct ns_desc net_ns_desc = {
|
||||
.cflag = CLONE_NEWNET,
|
||||
.str = "net",
|
||||
};
|
||||
|
@@ -20,6 +20,7 @@
|
||||
#include "crtools.h"
|
||||
#include "namespaces.h"
|
||||
#include "pstree.h"
|
||||
#include "net.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
@@ -300,7 +301,7 @@ static int parasite_init(struct parasite_ctl *ctl, pid_t pid, int nr_threads)
|
||||
if (opts.namespaces_flags & CLONE_NEWNET) {
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -315,7 +316,7 @@ static int parasite_init(struct parasite_ctl *ctl, pid_t pid, int nr_threads)
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (rst > 0 && restore_ns(rst, CLONE_NEWNET) < 0)
|
||||
if (rst > 0 && restore_ns(rst, &net_ns_desc) < 0)
|
||||
goto err;
|
||||
} else {
|
||||
struct sockaddr addr = { .sa_family = AF_UNSPEC, };
|
||||
|
@@ -428,7 +428,7 @@ int collect_sockets(int pid)
|
||||
if (opts.namespaces_flags & CLONE_NEWNET) {
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -521,7 +521,7 @@ int collect_sockets(int pid)
|
||||
|
||||
close(nl);
|
||||
out:
|
||||
if (rst > 0 && restore_ns(rst, CLONE_NEWNET) < 0)
|
||||
if (rst > 0 && restore_ns(rst, &net_ns_desc) < 0)
|
||||
err = -1;
|
||||
return err;
|
||||
}
|
||||
|
8
uts_ns.c
8
uts_ns.c
@@ -8,6 +8,7 @@
|
||||
#include "syscall.h"
|
||||
#include "namespaces.h"
|
||||
#include "sysctl.h"
|
||||
#include "uts_ns.h"
|
||||
|
||||
#include "protobuf.h"
|
||||
#include "protobuf/utsns.pb-c.h"
|
||||
@@ -18,7 +19,7 @@ int dump_uts_ns(int ns_pid, struct cr_fdset *fdset)
|
||||
struct utsname ubuf;
|
||||
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)
|
||||
return ret;
|
||||
|
||||
@@ -68,3 +69,8 @@ void show_utsns(int fd, struct cr_options *o)
|
||||
{
|
||||
pb_show_vertical(fd, PB_UTSNS);
|
||||
}
|
||||
|
||||
struct ns_desc uts_ns_desc = {
|
||||
.cflag = CLONE_NEWUTS,
|
||||
.str = "uts",
|
||||
};
|
||||
|
Reference in New Issue
Block a user