diff --git a/test/zdtm/lib/ns.c b/test/zdtm/lib/ns.c index 78cc91cf3..ad264074c 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); + if (write_pidfile(pid)) exit(1); - } - if (dprintf(fd, "%d", pid) == -1) { - fprintf(stderr, "Can't write in the file %s: %m\n", pidfile); - exit(1); - } - close(fd); exit(0); } diff --git a/test/zdtm/lib/test.c b/test/zdtm/lib/test.c index 57b6cf649..62608e8f5 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 a8f9e02ce..1afad70c7 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 #include #include