[CRIU] [PATCH 1/8] zdtm: Make write_map() a part of lib

Kirill Tkhai ktkhai at virtuozzo.com
Wed Jun 28 14:48:12 MSK 2017


It's implemented and used in many places,
so extract the code to library function.

Signed-off-by: Kirill Tkhai <ktkhai at virtuozzo.com>
---
 test/zdtm/lib/ns.c                    |   50 +++++++++++++++++----------------
 test/zdtm/lib/ns.h                    |    1 +
 test/zdtm/static/pidns01.c            |   25 +----------------
 test/zdtm/static/userns-leaked-sock.c |   23 +--------------
 test/zdtm/static/userns00.c           |   31 +++-----------------
 test/zdtm/static/userns01.c           |   23 +--------------
 6 files changed, 36 insertions(+), 117 deletions(-)

diff --git a/test/zdtm/lib/ns.c b/test/zdtm/lib/ns.c
index 7a0949f22..ccf18be13 100644
--- a/test/zdtm/lib/ns.c
+++ b/test/zdtm/lib/ns.c
@@ -19,6 +19,29 @@
 #include "zdtmtst.h"
 #include "ns.h"
 
+int write_map(pid_t pid, const char *file, const char *map)
+{
+	char path[PATH_MAX];
+	int fd, ret;
+
+	sprintf(path, "/proc/%d/%s", pid, file);
+	fd = open(path, O_WRONLY);
+	if (fd < 0) {
+		fail("Can't open");
+		return -1;
+	}
+	ret = strlen(map);
+	ret -= write(fd, map, ret);
+	if (ret) {
+		fail("Can't write");
+		close(fd);
+		return -1;
+	}
+	close(fd);
+
+	return 0;
+}
+
 extern int pivot_root(const char *new_root, const char *put_old);
 static int prepare_mntns(void)
 {
@@ -384,32 +407,11 @@ void ns_create(int argc, char **argv)
 	close(args.status_pipe[1]);
 
 	if (flags & CLONE_NEWUSER) {
-		char pname[PATH_MAX];
-		int fd;
-
-		snprintf(pname, sizeof(pname), "/proc/%d/uid_map", pid);
-		fd = open(pname, O_WRONLY);
-		if (fd < 0) {
-			fprintf(stderr, "open(%s): %m\n", pname);
-			exit(1);
-		}
-		if (write(fd, UID_MAP, sizeof(UID_MAP)) < 0) {
-			fprintf(stderr, "write(" UID_MAP "): %m\n");
+		if (write_map(pid, "uid_map", UID_MAP) ||
+		    write_map(pid, "gid_map", GID_MAP)) {
+			fprintf(stderr, "Can't setup maps: %m\n");
 			exit(1);
 		}
-		close(fd);
-
-		snprintf(pname, sizeof(pname), "/proc/%d/gid_map", pid);
-		fd = open(pname, O_WRONLY);
-		if (fd < 0) {
-			fprintf(stderr, "open(%s): %m\n", pname);
-			exit(1);
-		}
-		if (write(fd, GID_MAP, sizeof(GID_MAP)) < 0) {
-			fprintf(stderr, "write(" GID_MAP "): %m\n");
-			exit(1);
-		}
-		close(fd);
 	}
 	shutdown(args.status_pipe[0], SHUT_WR);
 
diff --git a/test/zdtm/lib/ns.h b/test/zdtm/lib/ns.h
index 23378bc60..846c50841 100644
--- a/test/zdtm/lib/ns.h
+++ b/test/zdtm/lib/ns.h
@@ -8,6 +8,7 @@ extern char *pidfile;
 
 extern void ns_create(int argc, char **argv);
 extern int ns_init(int argc, char **argv);
+extern int write_map(pid_t pid, const char *file, const char *map);
 
 extern void test_waitsig(void);
 extern void parseargs(int, char **);
diff --git a/test/zdtm/static/pidns01.c b/test/zdtm/static/pidns01.c
index 70137edc4..dba02e918 100644
--- a/test/zdtm/static/pidns01.c
+++ b/test/zdtm/static/pidns01.c
@@ -16,6 +16,7 @@
 
 #include "zdtmtst.h"
 #include "lock.h"
+#include "ns.h"
 /*
 	parent (pid_ns1, user_ns1)
 	  |
@@ -54,28 +55,6 @@ int child_fn(void *unused)
 	return pid == getpid() ? 0 : 1;
 }
 
-int write_map(pid_t pid, char *map)
-{
-	char path[PATH_MAX];
-	int fd, ret;
-
-	sprintf(path, "/proc/%d/%s", pid, map);
-	fd = open(path, O_WRONLY);
-	if (fd < 0) {
-		fail("Can't open");
-		return -1;
-	}
-	ret = write(fd, "0 0 1\n", 6);
-	if (ret != 6) {
-		fail("Can't write");
-		close(fd);
-		return -1;
-	}
-	close(fd);
-
-	return 0;
-}
-
 int __get_ns_id(int fd, unsigned int *id)
 {
 	struct stat st;
@@ -122,7 +101,7 @@ int main(int argc, char **argv)
 		return 1;
 	}
 
-	if (write_map(pid, "uid_map") || write_map(pid, "gid_map")) {
+	if (write_map(pid, "uid_map", "0 0 1") || write_map(pid, "gid_map", "0 0 1")) {
 		fail("write map");
 		goto err;
 	}
diff --git a/test/zdtm/static/userns-leaked-sock.c b/test/zdtm/static/userns-leaked-sock.c
index 5f4e9145f..f62c4ff6d 100644
--- a/test/zdtm/static/userns-leaked-sock.c
+++ b/test/zdtm/static/userns-leaked-sock.c
@@ -13,6 +13,7 @@
 #include <sys/un.h>
 
 #include "zdtmtst.h"
+#include "ns.h"
 #include "lock.h"
 
 const char *test_doc	= "Check that restorer for sockets is choosed right in dependence of net_ns->user_ns";
@@ -28,28 +29,6 @@ enum {
 
 futex_t *futex;
 
-int write_map(pid_t pid, char *file, char *map)
-{
-	char path[PATH_MAX];
-	int fd, ret;
-
-	sprintf(path, "/proc/%d/%s", pid, file);
-	fd = open(path, O_WRONLY);
-	if (fd < 0) {
-		fail("Can't open");
-		return -1;
-	}
-	ret = write(fd, map, strlen(map));
-	if (ret != strlen(map)) {
-		fail("Can't write");
-		close(fd);
-		return -1;
-	}
-	close(fd);
-
-	return 0;
-}
-
 int child_fn(void *arg)
 {
 	int ret = 1, sk = -1, orig_sk = (int)(long)arg;
diff --git a/test/zdtm/static/userns00.c b/test/zdtm/static/userns00.c
index 1b6f5c13b..45dcf443e 100644
--- a/test/zdtm/static/userns00.c
+++ b/test/zdtm/static/userns00.c
@@ -14,6 +14,7 @@
 #include <dirent.h>
 
 #include "zdtmtst.h"
+#include "ns.h"
 #include "lock.h"
 
 /*
@@ -70,28 +71,6 @@ int get_user_ns(pid_t pid, unsigned int *ns_id)
 	return 0;
 }
 
-int write_map(pid_t pid, char *map)
-{
-	char path[PATH_MAX];
-	int fd, ret;
-
-	sprintf(path, "/proc/%d/%s", pid, map);
-	fd = open(path, O_WRONLY);
-	if (fd < 0) {
-		fail("Can't open");
-		return -1;
-	}
-	ret = write(fd, "0 0 1\n", 6);
-	if (ret != 6) {
-		fail("Can't write");
-		close(fd);
-		return -1;
-	}
-	close(fd);
-
-	return 0;
-}
-
 /* Child1 creates its own namespace */
 int child1(void)
 {
@@ -107,8 +86,8 @@ int child1(void)
 	futex_set_and_wake(futex, CHILD1_CREATED);
 	futex_wait_while_lt(futex, GRAND_CHILD_CREATED);
 
-	if (write_map(*grand_child_pid, "uid_map") < 0 ||
-	    write_map(*grand_child_pid, "gid_map") < 0) {
+	if (write_map(*grand_child_pid, "uid_map", "0 0 1") < 0 ||
+	    write_map(*grand_child_pid, "gid_map", "0 0 1") < 0) {
 		fail("write map");
 		futex_set_and_wake(futex, EMERGENCY_ABORT);
 		return 2;
@@ -242,8 +221,8 @@ int main(int argc, char **argv)
 
 	futex_wait_while_lt(futex, CHILD1_CREATED);
 
-	if (write_map(pid1, "uid_map") < 0 ||
-	    write_map(pid1, "gid_map") < 0) {
+	if (write_map(pid1, "uid_map", "0 0 1") < 0 ||
+	    write_map(pid1, "gid_map", "0 0 1") < 0) {
 		fail("write map");
 		goto err;
 	}
diff --git a/test/zdtm/static/userns01.c b/test/zdtm/static/userns01.c
index 8e4401eca..91eca047c 100644
--- a/test/zdtm/static/userns01.c
+++ b/test/zdtm/static/userns01.c
@@ -17,6 +17,7 @@
 
 #include "zdtmtst.h"
 #include "lock.h"
+#include "ns.h"
 
 const char *test_doc	= "Check UID and GID in unshared userns remains the same";
 const char *test_author	= "Kirill Tkhai <ktkhai at virtuozzo.com>";
@@ -44,28 +45,6 @@ enum {
 gid_t gid_list[] = {3, 14, 15, 92}; /* Must be sorted */
 futex_t *futex;
 
-int write_map(pid_t pid, char *file, char *map)
-{
-	char path[PATH_MAX];
-	int fd, ret;
-
-	sprintf(path, "/proc/%d/%s", pid, file);
-	fd = open(path, O_WRONLY);
-	if (fd < 0) {
-		fail("Can't open");
-		return -1;
-	}
-	ret = write(fd, map, strlen(map));
-	if (ret != strlen(map)) {
-		fail("Can't write");
-		close(fd);
-		return -1;
-	}
-	close(fd);
-
-	return 0;
-}
-
 int compare_int(const void *a, const void *b)
 {
 	const int *x = a, *y = b;



More information about the CRIU mailing list