From fc25e880dfb1f804742006bcdd15ac70d18b4144 Mon Sep 17 00:00:00 2001 From: David Härdeman Date: Sat, 20 Jun 2020 11:23:11 +0200 Subject: Add some basic server states --- server.c | 47 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 9 deletions(-) (limited to 'server.c') diff --git a/server.c b/server.c index d11e6a7..c4bbc0c 100644 --- a/server.c +++ b/server.c @@ -291,6 +291,7 @@ server_exec_free(struct uring_task *task) #define P_PIDFD 3 #endif +/* FIXME: update states */ static void server_exec_done(struct cfg *cfg, struct uring_task *task, int res) { @@ -363,6 +364,24 @@ server_exec(struct cfg *cfg, struct server *scfg, const char *cmd) return true; } +static bool +server_check_running(struct cfg *cfg, struct server *scfg) +{ + /* FIXME: other methods, rcon? */ + if (scfg->systemd_service) { + verbose("%s: checking if systemd service is running\n", scfg->name); + if (systemd_service_running(cfg, scfg)) { + scfg->state = SERVER_STATE_RUNNING; + return true; + } else { + scfg->state = SERVER_STATE_STOPPED; + return false; + } + } + + return false; +} + bool server_start(struct cfg *cfg, struct server *scfg) { @@ -381,7 +400,12 @@ server_start(struct cfg *cfg, struct server *scfg) case SERVER_START_METHOD_SYSTEMD: verbose("Starting server %s via systemd (%s)\n", scfg->name, scfg->systemd_service); - return systemd_service_start(cfg, scfg); + + if (systemd_service_start(cfg, scfg)) { + scfg->state = SERVER_STATE_RUNNING; + return true; + } else + return server_check_running(cfg, scfg); case SERVER_START_METHOD_UNDEFINED: default: @@ -409,7 +433,11 @@ server_stop(struct cfg *cfg, struct server *scfg) case SERVER_STOP_METHOD_SYSTEMD: verbose("Stopping server %s via systemd (%s)\n", scfg->name, scfg->systemd_service); - return systemd_service_stop(cfg, scfg); + if (systemd_service_stop(cfg, scfg)) { + scfg->state = SERVER_STATE_STOPPED; + return true; + } else + return server_check_running(cfg, scfg); case SERVER_STOP_METHOD_RCON: verbose("Stopping server %s via rcon\n", scfg->name); @@ -438,6 +466,11 @@ server_commit(struct cfg *cfg, struct server *scfg) if (scfg->task.dead) return false; + if (scfg->state != SERVER_STATE_INIT) { + error("called in wrong state\n"); + return false; + } + if (!list_empty(&scfg->proxys)) { error("%s: proxys not empty?\n", scfg->name); return false; @@ -448,8 +481,6 @@ server_commit(struct cfg *cfg, struct server *scfg) return true; } - /* FIXME: running? */ - if (scfg->stop_method == SERVER_STOP_METHOD_RCON && list_empty(&scfg->rcons)) { error("%s: rcon stop method missing rcon address\n", @@ -580,11 +611,9 @@ server_commit(struct cfg *cfg, struct server *scfg) } idle_init(cfg, scfg); + scfg->state = SERVER_STATE_CFG_OK; - if (scfg->systemd_service) { - verbose("%s: checking if systemd service is running\n", scfg->name); - systemd_service_running(cfg, scfg); - } + server_check_running(cfg, scfg); debug(DBG_SRV, "success\n"); return true; @@ -781,10 +810,10 @@ server_new(struct cfg *cfg, const char *name) return NULL; } + scfg->state = SERVER_STATE_INIT; scfg->cfg = cfg; scfg->type = SERVER_TYPE_UNDEFINED; scfg->name = xstrdup(name); - scfg->running = false; scfg->stop_method = SERVER_STOP_METHOD_UNDEFINED; scfg->start_method = SERVER_START_METHOD_UNDEFINED; uring_task_init(&scfg->task, "scfg", uring_parent(cfg), server_free); -- cgit v1.2.3