[CRIU] [PATCH 1/5] zdtm: test shared file tables

Andrey Vagin avagin at openvz.org
Fri Nov 23 03:24:27 EST 2012


Signed-off-by: Andrey Vagin <avagin at openvz.org>
---
 test/zdtm/live/static/Makefile     |   1 +
 test/zdtm/live/static/fdt_shared.c | 157 +++++++++++++++++++++++++++++++++++++
 2 files changed, 158 insertions(+)
 create mode 100644 test/zdtm/live/static/fdt_shared.c

diff --git a/test/zdtm/live/static/Makefile b/test/zdtm/live/static/Makefile
index 551102d..3944fee 100644
--- a/test/zdtm/live/static/Makefile
+++ b/test/zdtm/live/static/Makefile
@@ -4,6 +4,7 @@ override CPPFLAGS += -I$(LIBDIR)
 CFLAGS	= -g -O2 -Wall -Werror -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=0
 
 TST_NOFILE	=				\
+		fdt_shared			\
 		busyloop00			\
 		sleeping00			\
 		pid00				\
diff --git a/test/zdtm/live/static/fdt_shared.c b/test/zdtm/live/static/fdt_shared.c
new file mode 100644
index 0000000..7b215e8
--- /dev/null
+++ b/test/zdtm/live/static/fdt_shared.c
@@ -0,0 +1,157 @@
+#define _GNU_SOURCE             /* See feature_test_macros(7) */
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <sched.h>
+#include <signal.h>
+#include <sys/wait.h>
+
+#include "zdtmtst.h"
+
+const char *test_doc	= "Check a shared file descriptor table.";
+const char *test_author	= "Andrew Vagin <avagin at openvz.org>";
+
+#define STACK_SIZE 4096
+#define TEST_FD 128
+
+static pid_t clone_child(int (*fn)(void *), int flags)
+{
+	char stack[STACK_SIZE];
+	pid_t pid;
+
+	pid = clone(fn, stack + STACK_SIZE,
+			flags | SIGCHLD, NULL);
+	if (pid == -1) {
+		err("Unable to clone a new process\n");
+		return -1;
+	}
+
+	return pid;
+}
+
+static int child2(void *_arg)
+{
+	char buf[10];
+
+	test_waitsig();
+
+	if (read(TEST_FD, buf, sizeof(buf)) != sizeof(buf)) {
+		err("Unable to read 10 bytes from %d\n", TEST_FD);
+		return 1;
+	}
+
+	return 0;
+}
+
+static int child3(void *_arg)
+{
+	test_waitsig();
+
+	if (close(TEST_FD) != -1) {
+		fail("%d is exist\n", TEST_FD);
+		return 1;
+	}
+
+	return 0;
+}
+
+static int child(void *_arg)
+{
+	char buf[10];
+	pid_t pid, pid2;
+	int status;
+
+	pid = clone_child(child2, CLONE_FILES);
+	if (pid < 0)
+		return 1;
+
+	pid2 = clone_child(child3, 0);
+	if (pid < 0)
+		return 1;
+
+	test_waitsig();
+
+	kill(pid2, SIGTERM);
+	kill(pid, SIGTERM);
+	waitpid(pid2, &status, 0);
+
+	if (status) {
+		fail("The child3 returned %d\n", status);
+		return 1;
+	}
+
+	waitpid(pid, &status, 0);
+
+	if (status) {
+		fail("The child2 returned %d\n", status);
+		return 1;
+	}
+
+	if (read(TEST_FD, buf, sizeof(buf)) != sizeof(buf)) {
+		err("Unable to read 10 bytes from %d\n", TEST_FD);
+		return 1;
+	}
+
+	if (close(TEST_FD) == -1) {
+		err("Unable to close(%d)\n", TEST_FD);
+		return 1;
+	}
+
+	return 0;
+}
+
+int main(int argc, char ** argv)
+{
+	int status;
+	pid_t pid, pid2;
+	int fd;
+
+	test_init(argc, argv);
+
+	pid = clone_child(child, CLONE_FILES);
+	if (pid < 0)
+		return 1;
+
+	pid2 = clone_child(child2, CLONE_FILES);
+	if (pid2 < 0)
+		return 1;
+
+	test_daemon();
+	test_waitsig();
+
+	fd = open("/dev/zero", O_RDONLY);
+	if (fd == -1) {
+		err("Can't open /dev/zero\n");
+		return -1;
+	}
+	fd = dup2(fd, TEST_FD);
+	if (fd == -1) {
+		err("Can't dup fd to %d\n", fd, TEST_FD);
+		return -1;
+	}
+
+	kill(pid2, SIGTERM);
+	waitpid(pid2, &status, 0);
+	kill(pid, SIGTERM);
+
+	if (status) {
+		fail("The child returned %d\n", status);
+		return 1;
+	}
+
+	waitpid(pid, &status, 0);
+	if (status) {
+		fail("The child returned %d\n", status);
+		return 1;
+	}
+
+	if (close(TEST_FD) == 0) {
+		fail("%d was not closed\n", TEST_FD);
+		return 1;
+	}
+
+	pass();
+
+	return 0;
+}
-- 
1.7.11.7



More information about the CRIU mailing list