From 7c20ae7f7737c35a9e322ee712a0c342d00f536e Mon Sep 17 00:00:00 2001 From: David Härdeman Date: Tue, 9 Jun 2020 13:54:38 +0200 Subject: Add basic stop/start methods --- server.c | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) (limited to 'server.c') diff --git a/server.c b/server.c index 44779eb..d51e127 100644 --- a/server.c +++ b/server.c @@ -3,6 +3,9 @@ #include #include #include +#include +#include +#include #include "main.h" #include "uring.h" @@ -221,6 +224,82 @@ out: return false; } +static bool +exec_cmd(struct cfg *cfg, struct server *scfg, const char *cmd) +{ + pid_t pid, wpid; + int wstatus; + + if (!cfg || !scfg || !cmd) + return false; + + pid = fork(); + if (pid < 0) { + perror("fork"); + return false; + + } else if (pid > 0) { + do { + wpid = waitpid(pid, &wstatus, 0); + if (wpid < 0) { + perror("waitpid"); + return false; + } + } while (!WIFEXITED(wstatus)); + + fprintf(stderr, "Child %s exited: %i\n", scfg->start_exec, WEXITSTATUS(wstatus)); + if (WEXITSTATUS(wstatus) == 0) + return true; + else + return false; + + } else { + execl(scfg->start_exec, scfg->start_exec, NULL); + perror("execl"); + exit(EXIT_FAILURE); + } +} + +bool +server_start(struct cfg *cfg, struct server *scfg) +{ + if (!cfg || !scfg) + return false; + + switch (scfg->start_method) { + + case SERVER_START_METHOD_EXEC: + + return exec_cmd(cfg, scfg, scfg->start_exec); + + case SERVER_START_METHOD_UNDEFINED: + default: + break; + } + + return false; +} + +bool +server_stop(struct cfg *cfg, struct server *scfg) +{ + if (!cfg || !scfg) + return false; + + switch (scfg->stop_method) { + + case SERVER_STOP_METHOD_EXEC: + + return exec_cmd(cfg, scfg, scfg->stop_exec); + + case SERVER_STOP_METHOD_UNDEFINED: + default: + break; + } + + return false; +} + bool server_commit(struct cfg *cfg, struct server *scfg) { -- cgit v1.2.3