[CRIU] [PATCH 2/3] zdtm: Factor out pidfiles creation

Pavel Emelyanov xemul at virtuozzo.com
Wed Mar 2 07:22:25 PST 2016


After the pidfile manipulation is sanitized, we can have
a helper for pidfile wrting.

Signed-off-by: Pavel Emelyanov <xemul at virtuozzo.com>
---
 test/zdtm/lib/ns.c      | 10 +---------
 test/zdtm/lib/test.c    | 33 ++++++++++++++++++++++-----------
 test/zdtm/lib/zdtmtst.h |  2 ++
 3 files changed, 25 insertions(+), 20 deletions(-)

diff --git a/test/zdtm/lib/ns.c b/test/zdtm/lib/ns.c
index 78cc91c..ad26407 100644
--- a/test/zdtm/lib/ns.c
+++ b/test/zdtm/lib/ns.c
@@ -408,16 +408,8 @@ void ns_create(int argc, char **argv)
 		exit(1);
 	}
 
-	fd = open(pidfile, O_CREAT | O_EXCL | O_WRONLY, 0666);
-	if (fd == -1) {
-		fprintf(stderr, "Can't create the file %s: %m\n", pidfile);
-		exit(1);
-	}
-	if (dprintf(fd, "%d", pid) == -1) {
-		fprintf(stderr, "Can't write in the file %s: %m\n", pidfile);
+	if (write_pidfile(pid))
 		exit(1);
-	}
-	close(fd);
 
 	exit(0);
 }
diff --git a/test/zdtm/lib/test.c b/test/zdtm/lib/test.c
index 57b6cf6..62608e8 100644
--- a/test/zdtm/lib/test.c
+++ b/test/zdtm/lib/test.c
@@ -91,10 +91,28 @@ void test_ext_init(int argc, char **argv)
 		exit(1);
 }
 
+int write_pidfile(int pid)
+{
+	int fd;
+
+	fd = open(pidfile, O_CREAT | O_EXCL | O_WRONLY, 0666);
+	if (fd == -1) {
+		fprintf(stderr, "Can't create the file %s: %m\n", pidfile);
+		return -1;
+	}
+	if (dprintf(fd, "%d", pid) == -1) {
+		fprintf(stderr, "Can't write in the file %s: %m\n", pidfile);
+		return -1;
+	}
+
+	close(fd);
+
+	return 0;
+}
+
 void test_init(int argc, char **argv)
 {
 	pid_t pid;
-	static FILE *pidf;
 	char *val;
 	struct sigaction sa = {
 		.sa_handler	= sig_hand,
@@ -166,12 +184,6 @@ void test_init(int argc, char **argv)
 	setup_outfile();
 	redir_stdfds();
 
-	pidf = fopen(pidfile, "wx");
-	if (!pidf) {
-		pr_perror("Can't create pid file %s", pidfile);
-		exit(1);
-	}
-
 	pid = fork();
 	if (pid < 0) {
 		pr_perror("Daemonizing failed");
@@ -198,13 +210,12 @@ void test_init(int argc, char **argv)
 			}
 		}
 
-		fprintf(pidf, "%d\n", pid);
-		fclose(pidf);
+		if (write_pidfile(pid))
+			exit(1);
+
 		_exit(0);
 	}
 
-	fclose(pidf);
-
 	if (setsid() < 0) {
 		pr_perror("Can't become session group leader");
 		exit(1);
diff --git a/test/zdtm/lib/zdtmtst.h b/test/zdtm/lib/zdtmtst.h
index a8f9e02..1afad70 100644
--- a/test/zdtm/lib/zdtmtst.h
+++ b/test/zdtm/lib/zdtmtst.h
@@ -92,6 +92,8 @@ extern int parse_opt_ulong(char *param, void *arg);
 extern int parse_opt_string(char *param, void *arg);
 #define param_check_string(name, p) __param_check(name, p, char *)
 
+extern int write_pidfile(int pid);
+
 #include <stdio.h>
 #include <errno.h>
 #include <string.h>
-- 
2.5.0



More information about the CRIU mailing list