[CRIU] [PATCH 4/4] netdevconf: restore network devices config options
Pavel Tikhomirov
ptikhomirov at parallels.com
Wed Sep 10 01:02:28 PDT 2014
Signed-off-by: Pavel Tikhomirov <ptikhomirov at parallels.com>
---
net.c | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 89 insertions(+)
diff --git a/net.c b/net.c
index 5619fe4..0631b9f 100644
--- a/net.c
+++ b/net.c
@@ -646,6 +646,93 @@ static inline int restore_iptables(int pid)
return ret;
}
+static int restore_one_option(char *option, char *path, void *data)
+{
+ int ret;
+ int val;
+ FILE *fp;
+ NetDeviceIpv4ConfEntry *netdev = data;
+
+ ret = get_opt_index(option);
+ if (ret < 0)
+ return -1;
+
+ val = netdev->data[ret];
+
+ ret = access(path, W_OK);
+ if (ret < 0) {
+ /*
+ * If don't have access, at least check option don't change
+ * mainly it's made for mc_forwarding option: it is write protected
+ */
+ int tmp;
+ pr_debug("%s option is write protected", option);
+ fp = fopen(path, "r");
+ if (fp == NULL) {
+ pr_perror("fopen");
+ return -1;
+ }
+
+ ret = fscanf(fp, "%d", &tmp);
+ if (ret < 0) {
+ pr_perror("fscanf");
+ return -1;
+ }
+
+ if (val != tmp) {
+ pr_err("Network device ipv4 config option %s corrupted", option);
+ return -1;
+ }
+ return 0;
+ }
+
+ fp = fopen(path, "w");
+ if (fp == NULL) {
+ pr_perror("fopen");
+ return -1;
+ }
+
+ ret = fprintf(fp, "%d", val);
+ if (ret < 0) {
+ pr_perror("fprintf");
+ return -1;
+ }
+
+ fclose(fp);
+ return 0;
+}
+
+static int restore_net_devs_conf(int pid)
+{
+ int fd;
+ NetDeviceIpv4ConfEntry *netdev;
+
+ fd = open_image(CR_FD_NETDEV_CONF, O_RSTR, pid);
+ if (fd < 0) {
+ pr_perror("open_image");
+ return -1;
+ }
+
+ while (1) {
+ int ret;
+ char path[PATH_MAX];
+
+ ret = pb_read_one_eof(fd, &netdev, PB_NETDEV_CONF);
+ if (ret <= 0)
+ break;
+
+ snprintf(path, sizeof(path), "%s/%s", NET_IPV4_CONF_DIR, netdev->dev_name);
+ ret = for_each_entry_do(path, restore_one_option, (void*)netdev);
+ if (ret < 0)
+ return -1;
+
+ net_device_ipv4_conf_entry__free_unpacked(netdev, NULL);
+ }
+
+ close(fd);
+ return 0;
+}
+
static int mount_ns_sysfs(void)
{
char sys_mount[] = "crtools-sys.XXXXXX";
@@ -727,6 +814,8 @@ int prepare_net_ns(int pid)
ret = restore_route(pid);
if (!ret)
ret = restore_iptables(pid);
+ if (!ret)
+ ret = restore_net_devs_conf(pid);
close(ns_fd);
--
1.9.3
More information about the CRIU
mailing list