summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--idle.c6
-rw-r--r--server.c79
-rw-r--r--server.h4
3 files changed, 87 insertions, 2 deletions
diff --git a/idle.c b/idle.c
index 518999b..fc6be9b 100644
--- a/idle.c
+++ b/idle.c
@@ -223,8 +223,10 @@ idle_check_handshake_reply(struct cfg *cfg, struct uring_task *task, int res)
idle->server->idle_count = 0;
else {
idle->server->idle_count++;
- if (idle->server->idle_count > idle->server->idle_timeout)
- fprintf(stderr, "Would shutdown idle server %s\n", idle->server->name);
+ if (idle->server->idle_count > idle->server->idle_timeout) {
+ fprintf(stderr, "Stopping idle server %s\n", idle->server->name);
+ server_stop(cfg, idle->server);
+ }
}
return;
diff --git a/server.c b/server.c
index 44779eb..d51e127 100644
--- a/server.c
+++ b/server.c
@@ -3,6 +3,9 @@
#include <arpa/inet.h>
#include <unistd.h>
#include <inttypes.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <unistd.h>
#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)
{
diff --git a/server.h b/server.h
index e8e6ba8..cb4fd76 100644
--- a/server.h
+++ b/server.h
@@ -56,6 +56,10 @@ void server_delete(struct cfg *cfg, struct server *scfg);
void server_delete_by_name(struct cfg *cfg, const char *name);
+bool server_start(struct cfg *cfg, struct server *scfg);
+
+bool server_stop(struct cfg *cfg, struct server *scfg);
+
bool server_commit(struct cfg *cfg, struct server *scfg);
bool server_add_remote(struct cfg *cfg, struct server *scfg,