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 --- idle.c | 3 +++ server.c | 47 ++++++++++++++++++++++++++++++++++++++--------- server.h | 10 +++++++++- 3 files changed, 50 insertions(+), 10 deletions(-) diff --git a/idle.c b/idle.c index 8fac1f0..5f7ed17 100644 --- a/idle.c +++ b/idle.c @@ -252,6 +252,8 @@ idle_check_handshake_reply(struct cfg *cfg, struct uring_task *task, int res) if (player_count < 0) goto out; + idle->server->state = SERVER_STATE_RUNNING; + debug(DBG_IDLE, "%s: currently %i active players\n", idle->server->name, player_count); @@ -301,6 +303,7 @@ idle_check_connected_cb(struct cfg *cfg, struct connection *conn, bool connected "idle check connection to remote server (%s) failed\n", idle->server->name); idle->server->idle_count = 0; + idle->server->state = SERVER_STATE_STOPPED; return; } 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); diff --git a/server.h b/server.h index 3776883..ae0503a 100644 --- a/server.h +++ b/server.h @@ -1,6 +1,14 @@ #ifndef fooserverhfoo #define fooserverhfoo +enum server_state { + SERVER_STATE_INIT, + SERVER_STATE_CFG_OK, + SERVER_STATE_RUNNING, + SERVER_STATE_STOPPED, + SERVER_STATE_DEAD, +}; + enum server_type { SERVER_TYPE_UNDEFINED, SERVER_TYPE_ANNOUNCE, @@ -30,7 +38,7 @@ struct server { struct list_head proxys; struct list_head rcons; struct list_head dnslookups; - bool running; + enum server_state state; struct cfg *cfg; enum server_stop_method stop_method; -- cgit v1.2.3