Commit c88d4099 authored by Pavel Emelyanov's avatar Pavel Emelyanov

zdtm: Factor out pidfiles creation

After the pidfile manipulation is sanitized, we can have
a helper for pidfile wrting.
Signed-off-by: 's avatarPavel Emelyanov <xemul@virtuozzo.com>
parent 8abf6d2d
...@@ -408,16 +408,8 @@ void ns_create(int argc, char **argv) ...@@ -408,16 +408,8 @@ void ns_create(int argc, char **argv)
exit(1); exit(1);
} }
fd = open(pidfile, O_CREAT | O_EXCL | O_WRONLY, 0666); if (write_pidfile(pid))
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);
exit(1); exit(1);
}
close(fd);
exit(0); exit(0);
} }
......
...@@ -91,10 +91,28 @@ void test_ext_init(int argc, char **argv) ...@@ -91,10 +91,28 @@ void test_ext_init(int argc, char **argv)
exit(1); 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) void test_init(int argc, char **argv)
{ {
pid_t pid; pid_t pid;
static FILE *pidf;
char *val; char *val;
struct sigaction sa = { struct sigaction sa = {
.sa_handler = sig_hand, .sa_handler = sig_hand,
...@@ -166,12 +184,6 @@ void test_init(int argc, char **argv) ...@@ -166,12 +184,6 @@ void test_init(int argc, char **argv)
setup_outfile(); setup_outfile();
redir_stdfds(); redir_stdfds();
pidf = fopen(pidfile, "wx");
if (!pidf) {
pr_perror("Can't create pid file %s", pidfile);
exit(1);
}
pid = fork(); pid = fork();
if (pid < 0) { if (pid < 0) {
pr_perror("Daemonizing failed"); pr_perror("Daemonizing failed");
...@@ -198,13 +210,12 @@ void test_init(int argc, char **argv) ...@@ -198,13 +210,12 @@ void test_init(int argc, char **argv)
} }
} }
fprintf(pidf, "%d\n", pid); if (write_pidfile(pid))
fclose(pidf); exit(1);
_exit(0); _exit(0);
} }
fclose(pidf);
if (setsid() < 0) { if (setsid() < 0) {
pr_perror("Can't become session group leader"); pr_perror("Can't become session group leader");
exit(1); exit(1);
......
...@@ -92,6 +92,8 @@ extern int parse_opt_ulong(char *param, void *arg); ...@@ -92,6 +92,8 @@ extern int parse_opt_ulong(char *param, void *arg);
extern int parse_opt_string(char *param, void *arg); extern int parse_opt_string(char *param, void *arg);
#define param_check_string(name, p) __param_check(name, p, char *) #define param_check_string(name, p) __param_check(name, p, char *)
extern int write_pidfile(int pid);
#include <stdio.h> #include <stdio.h>
#include <errno.h> #include <errno.h>
#include <string.h> #include <string.h>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment