[CRIU] [PATCH v3 1/3] namespaces: split UTS and generic code

Kinsbursky Stanislav skinsbursky at openvz.org
Tue Jan 31 03:29:23 EST 2012


From: Stanislav Kinsbursky <skinsbursky at parallels.com>

Generic code will be used for other namespaces.

Signed-off-by: Stanislav Kinsbursky <skinsbursky at parallels.com>

---
 Makefile             |    1 
 cr-show.c            |    1 
 include/namespaces.h |    2 -
 include/uts_ns.h     |   10 ++++
 namespaces.c         |  134 +-------------------------------------------------
 uts_ns.c             |  135 ++++++++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 150 insertions(+), 133 deletions(-)
 create mode 100644 include/uts_ns.h
 create mode 100644 uts_ns.c

diff --git a/Makefile b/Makefile
index 4f1e500..712a7d6 100644
--- a/Makefile
+++ b/Makefile
@@ -39,6 +39,7 @@ OBJS		+= libnetlink.o
 OBJS		+= sockets.o
 OBJS		+= files.o
 OBJS		+= namespaces.o
+OBJS		+= uts_ns.o
 
 OBJS-BLOB	+= parasite.o
 SRCS-BLOB	+= $(patsubst %.o,%.c,$(OBJS-BLOB))
diff --git a/cr-show.c b/cr-show.c
index c012e69..68174d1 100644
--- a/cr-show.c
+++ b/cr-show.c
@@ -19,6 +19,7 @@
 #include "util.h"
 #include "sockets.h"
 #include "image.h"
+#include "uts_ns.h"
 
 #define DEF_PAGES_PER_LINE	6
 
diff --git a/include/namespaces.h b/include/namespaces.h
index 518855f..f7bd0fa 100644
--- a/include/namespaces.h
+++ b/include/namespaces.h
@@ -3,5 +3,5 @@
 int dump_namespaces(int pid);
 int prepare_namespace(int pid, unsigned long clone_flags);
 int try_show_namespaces(int pid);
-void show_utsns(int fd);
+int switch_ns(int pid, int type, char *ns);
 #endif
diff --git a/include/uts_ns.h b/include/uts_ns.h
new file mode 100644
index 0000000..2e51ae1
--- /dev/null
+++ b/include/uts_ns.h
@@ -0,0 +1,10 @@
+#ifndef CR_UTS_NS_H_
+#define CR_UTS_NS_H_
+
+#include "crtools.h"
+
+int dump_uts_ns(int ns_pid, struct cr_fdset *fdset);
+void show_utsns(int fd);
+int prepare_utsns(int pid);
+
+#endif /* CR_UTS_NS_H_ */
diff --git a/namespaces.c b/namespaces.c
index 6370bc2..7aa6f6c 100644
--- a/namespaces.c
+++ b/namespaces.c
@@ -1,13 +1,11 @@
 #include <unistd.h>
 #include <fcntl.h>
 #include <sys/wait.h>
-#include <sys/types.h>
-#include <sys/utsname.h>
 #include "util.h"
-#include "crtools.h"
 #include "syscall.h"
+#include "uts_ns.h"
 
-static int switch_ns(int pid, int type, char *ns)
+int switch_ns(int pid, int type, char *ns)
 {
 	char buf[32];
 	int nsfd, ret;
@@ -28,43 +26,6 @@ out:
 	return ret;
 }
 
-static int dump_uts_string(int fd, char *str)
-{
-	int ret;
-	u32 len;
-
-	len = strlen(str);
-	ret = write_img(fd, &len);
-	if (ret == 0)
-		ret = write_img_buf(fd, str, len);
-
-	return ret;
-}
-
-static int dump_uts_ns(int ns_pid, struct cr_fdset *fdset)
-{
-	int fd, ret;
-	struct utsname ubuf;
-
-	ret = switch_ns(ns_pid, CLONE_NEWUTS, "uts");
-	if (ret < 0)
-		return ret;
-
-	ret = uname(&ubuf);
-	if (ret < 0) {
-		pr_perror("Error calling uname\n");
-		return ret;
-	}
-
-	fd = fdset->fds[CR_FD_UTSNS];
-
-	ret = dump_uts_string(fd, ubuf.nodename);
-	if (!ret)
-		ret = dump_uts_string(fd, ubuf.domainname);
-
-	return ret;
-}
-
 static int do_dump_namespaces(int ns_pid)
 {
 	struct cr_fdset *fdset;
@@ -124,67 +85,6 @@ int dump_namespaces(int ns_pid)
 	return 0;
 }
 
-static int prepare_uts_str(int fd, char *n)
-{
-	int ret;
-	u32 len;
-	char str[65], path[128];
-
-	ret = read_img(fd, &len);
-	if (ret > 0) {
-		if (len >= 65) {
-			pr_err("Corrupted %s\n", n);
-			return -1;
-		}
-
-		ret = read_img_buf(fd, str, len);
-		if (ret < 0)
-			return -1;
-
-		str[len] = '\0';
-
-		snprintf(path, sizeof(path),
-				"/proc/sys/kernel/%s", n);
-		fd = open(path, O_WRONLY);
-		if (fd < 0) {
-			pr_perror("Can't open %s\n", path);
-			return -1;
-		}
-
-		pr_info("Restoging %s to [%s]\n", n, str);
-
-		ret = write(fd, str, len);
-		close(fd);
-		if (ret != len) {
-			pr_perror("Can't write %s to %s\n",
-					str, path);
-			return -1;
-		}
-
-		ret = 0;
-	}
-
-	return ret;
-}
-
-static int prepare_utsns(int pid)
-{
-	int fd, ret;
-	u32 len;
-	char str[65];
-
-	fd = open_image_ro(CR_FD_UTSNS, pid);
-	if (fd < 0)
-		return -1;
-
-	ret = prepare_uts_str(fd, "hostname");
-	if (!ret)
-		ret = prepare_uts_str(fd, "domainname");
-
-	close(fd);
-	return ret;
-}
-
 int prepare_namespace(int pid, unsigned long clone_flags)
 {
 	int ret = 0;
@@ -198,36 +98,6 @@ int prepare_namespace(int pid, unsigned long clone_flags)
 	return ret;
 }
 
-static void show_uts_string(int fd, char *n)
-{
-	int ret;
-	u32 len;
-	char str[65];
-
-	ret = read_img(fd, &len);
-	if (ret > 0) {
-		if (len >= 65) {
-			pr_err("Corrupted hostname\n");
-			return;
-		}
-
-		ret = read_img_buf(fd, str, len);
-		if (ret < 0)
-			return;
-
-		str[len] = '\0';
-		pr_info("%s: [%s]\n", n, str);
-	}
-}
-
-void show_utsns(int fd)
-{
-	pr_img_head(CR_FD_UTSNS);
-	show_uts_string(fd, "hostname");
-	show_uts_string(fd, "domainname");
-	pr_img_tail(CR_FD_UTSNS);
-}
-
 int try_show_namespaces(int ns_pid)
 {
 	struct cr_fdset *fdset;
diff --git a/uts_ns.c b/uts_ns.c
new file mode 100644
index 0000000..bb4fcb4
--- /dev/null
+++ b/uts_ns.c
@@ -0,0 +1,135 @@
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/utsname.h>
+#include "util.h"
+#include "crtools.h"
+#include "syscall.h"
+#include "namespaces.h"
+
+static int dump_uts_string(int fd, char *str)
+{
+	int ret;
+	u32 len;
+
+	len = strlen(str);
+	ret = write_img(fd, &len);
+	if (ret == 0)
+		ret = write_img_buf(fd, str, len);
+
+	return ret;
+}
+
+int dump_uts_ns(int ns_pid, struct cr_fdset *fdset)
+{
+	int fd, ret;
+	struct utsname ubuf;
+
+	ret = switch_ns(ns_pid, CLONE_NEWUTS, "uts");
+	if (ret < 0)
+		return ret;
+
+	ret = uname(&ubuf);
+	if (ret < 0) {
+		pr_perror("Error calling uname\n");
+		return ret;
+	}
+
+	fd = fdset->fds[CR_FD_UTSNS];
+
+	ret = dump_uts_string(fd, ubuf.nodename);
+	if (!ret)
+		ret = dump_uts_string(fd, ubuf.domainname);
+
+	return ret;
+}
+
+static int prepare_uts_str(int fd, char *n)
+{
+	int ret;
+	u32 len;
+	char str[65], path[128];
+
+	ret = read_img(fd, &len);
+	if (ret > 0) {
+		if (len >= 65) {
+			pr_err("Corrupted %s\n", n);
+			return -1;
+		}
+
+		ret = read_img_buf(fd, str, len);
+		if (ret < 0)
+			return -1;
+
+		str[len] = '\0';
+
+		snprintf(path, sizeof(path),
+				"/proc/sys/kernel/%s", n);
+		fd = open(path, O_WRONLY);
+		if (fd < 0) {
+			pr_perror("Can't open %s\n", path);
+			return -1;
+		}
+
+		pr_info("Restoging %s to [%s]\n", n, str);
+
+		ret = write(fd, str, len);
+		close(fd);
+		if (ret != len) {
+			pr_perror("Can't write %s to %s\n",
+					str, path);
+			return -1;
+		}
+
+		ret = 0;
+	}
+
+	return ret;
+}
+
+int prepare_utsns(int pid)
+{
+	int fd, ret;
+	u32 len;
+	char str[65];
+
+	fd = open_image_ro(CR_FD_UTSNS, pid);
+	if (fd < 0)
+		return -1;
+
+	ret = prepare_uts_str(fd, "hostname");
+	if (!ret)
+		ret = prepare_uts_str(fd, "domainname");
+
+	close(fd);
+	return ret;
+}
+
+static void show_uts_string(int fd, char *n)
+{
+	int ret;
+	u32 len;
+	char str[65];
+
+	ret = read_img(fd, &len);
+	if (ret > 0) {
+		if (len >= 65) {
+			pr_err("Corrupted hostname\n");
+			return;
+		}
+
+		ret = read_img_buf(fd, str, len);
+		if (ret < 0)
+			return;
+
+		str[len] = '\0';
+		pr_info("%s: [%s]\n", n, str);
+	}
+}
+
+void show_utsns(int fd)
+{
+	pr_img_head(CR_FD_UTSNS);
+	show_uts_string(fd, "hostname");
+	show_uts_string(fd, "domainname");
+	pr_img_tail(CR_FD_UTSNS);
+}



More information about the CRIU mailing list