diff options
author | David Härdeman <david@hardeman.nu> | 2020-07-07 18:41:38 +0200 |
---|---|---|
committer | David Härdeman <david@hardeman.nu> | 2020-07-07 18:41:38 +0200 |
commit | 90e27b4356f2a6ab98e812c4096b0b76f94a8fb3 (patch) | |
tree | 7c900e498c426611c90ef8b2ba80d141456d9552 /minecproxy | |
parent | 0e2db6328946180c47bc09a486fe38ca0364e2af (diff) |
Flesh out the ping implementation
Diffstat (limited to 'minecproxy')
-rw-r--r-- | minecproxy/main.c | 4 | ||||
-rw-r--r-- | minecproxy/main.h | 2 | ||||
-rw-r--r-- | minecproxy/meson.build | 3 | ||||
-rw-r--r-- | minecproxy/server.c | 12 | ||||
-rw-r--r-- | minecproxy/systemd.c | 164 | ||||
-rw-r--r-- | minecproxy/systemd.h | 13 |
6 files changed, 9 insertions, 189 deletions
diff --git a/minecproxy/main.c b/minecproxy/main.c index 238b612..c51b01c 100644 --- a/minecproxy/main.c +++ b/minecproxy/main.c @@ -24,7 +24,7 @@ #include "server.h" #include "server-config.h" #include "announce.h" -#include "systemd.h" +#include "shared/systemd.h" #include "igmp.h" #include "idle.h" #include "ptimer.h" @@ -177,7 +177,7 @@ static void cfg_free(struct uring_task *task) assert_return(task && xcfg == cfg); debug(DBG_SIG, "called"); - systemd_delete(cfg); + systemd_delete(); xfree(cfg->igmp_iface); cfg->igmp_iface = NULL; exiting = true; diff --git a/minecproxy/main.h b/minecproxy/main.h index 12b16df..2c7be83 100644 --- a/minecproxy/main.h +++ b/minecproxy/main.h @@ -99,8 +99,6 @@ struct cfg { struct ptimer *ptimer; struct igmp *igmp; struct idle *idle; - struct sd_bus *sd_bus; - bool sd_bus_failed; struct uring_task task; struct list_head servers; }; diff --git a/minecproxy/meson.build b/minecproxy/meson.build index 959b975..155840c 100644 --- a/minecproxy/meson.build +++ b/minecproxy/meson.build @@ -11,17 +11,14 @@ minecproxy_sources = [ 'idle.c', 'ptimer.c', 'igmp.c', - 'systemd.c', 'misc.c', ] dep_liburing = dependency('liburing') -dep_libsystemd = dependency('libsystemd') dep_libcapng = dependency('libcap-ng') minecproxy_deps = [ dep_liburing, - dep_libsystemd, dep_libcapng, dep_config_h, dep_libshared, diff --git a/minecproxy/server.c b/minecproxy/server.c index d24c0ad..b1e9b05 100644 --- a/minecproxy/server.c +++ b/minecproxy/server.c @@ -18,9 +18,9 @@ #include "server-config.h" #include "server-proxy.h" #include "server-rcon.h" -#include "shared/config-parser.h" #include "idle.h" -#include "systemd.h" +#include "shared/config-parser.h" +#include "shared/systemd.h" void server_refdump(struct server *server) { @@ -218,13 +218,15 @@ static bool server_exec(struct server *server, const char *cmd) static bool server_check_running(struct server *server) { + const char *error; + assert_return(server, false); /* FIXME: other methods, rcon? */ if (server->scfg.systemd_service) { verbose("%s: checking if systemd service is running", server->name); - if (systemd_service_running(server)) { + if (systemd_service_running(&server->scfg, &error)) { server->state = SERVER_STATE_RUNNING; return true; } else { @@ -252,7 +254,7 @@ bool server_start(struct server *server) server->name, server->scfg.systemd_service); - if (systemd_service_start(server)) { + if (systemd_service_start(&server->scfg)) { server->state = SERVER_STATE_RUNNING; return true; } else @@ -281,7 +283,7 @@ bool server_stop(struct server *server) verbose("Stopping server %s via systemd (%s)", server->name, server->scfg.systemd_service); - if (systemd_service_stop(server)) { + if (systemd_service_stop(&server->scfg)) { server->state = SERVER_STATE_STOPPED; return true; } else diff --git a/minecproxy/systemd.c b/minecproxy/systemd.c deleted file mode 100644 index 94d5b8b..0000000 --- a/minecproxy/systemd.c +++ /dev/null @@ -1,164 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0 */ -#include <string.h> -#include <stdlib.h> -#include <systemd/sd-bus.h> -#include <errno.h> - -#include "main.h" -#include "server.h" -#include "systemd.h" -#include "config.h" - -void systemd_delete() -{ - assert_return_silent(cfg->sd_bus); - - sd_bus_unref(cfg->sd_bus); - cfg->sd_bus = NULL; -} - -static sd_bus *get_bus() -{ - int r; - - if (cfg->sd_bus_failed) - return NULL; - - if (!cfg->sd_bus) { - r = sd_bus_open_user(&cfg->sd_bus); - if (r < 0) { - error("failed to connect to user system bus: %s", - strerror(-r)); - cfg->sd_bus_failed = true; - return NULL; - } - } - - return cfg->sd_bus; -} - -/* - * Check if a systemd service is running. - * - * This is equivalent to (assuming service mc@world1): - * gdbus call --session - * --dest org.freedesktop.systemd1 - * --object-path /org/freedesktop/systemd1/unit/mc_40world1_2eservice - * --method org.freedesktop.DBus.Properties.Get - * "org.freedesktop.systemd1.Unit" - * "ActiveState" - */ -bool systemd_service_running(struct server *server) -{ - sd_bus *bus = get_bus(); - sd_bus_error error = SD_BUS_ERROR_NULL; - char *status = NULL; - bool running = false; - int r; - - assert_return(server && bus && server->scfg.systemd_service && - server->scfg.systemd_obj, - false); - -again: - r = sd_bus_get_property_string(bus, SYSTEMD_DBUS_SERVICE, - server->scfg.systemd_obj, - SYSTEMD_DBUS_INTERFACE, "ActiveState", - &error, &status); - if (r < 0) { - if (sd_bus_error_get_errno(&error) == EINTR) - goto again; - - error("failed to get status for service %s (%s): %s", - server->scfg.systemd_service, server->scfg.systemd_obj, - error.message); - goto out; - } - - if (streq(status, "active")) { - running = true; - debug(DBG_SYSD, "systemd service %s (%s) is active", - server->scfg.systemd_service, server->scfg.systemd_obj); - } else - debug(DBG_SYSD, "systemd service %s (%s) is not active", - server->scfg.systemd_service, server->scfg.systemd_obj); - -out: - free(status); - sd_bus_error_free(&error); - return running; -} - -static bool systemd_service_action(struct server *server, const char *action) -{ - sd_bus_error error = SD_BUS_ERROR_NULL; - sd_bus_message *m = NULL; - sd_bus *bus = get_bus(); - const char *path; - bool performed = false; - int r; - - assert_return(server && bus && server->scfg.systemd_service && - server->scfg.systemd_obj && action, - false); - -again: - r = sd_bus_call_method(bus, SYSTEMD_DBUS_SERVICE, server->scfg.systemd_obj, - SYSTEMD_DBUS_INTERFACE, action, &error, &m, "s", - "fail"); - if (r < 0) { - if (sd_bus_error_get_errno(&error) == EINTR) - goto again; - - error("failed to perform action %s on systemd service %s: %s", - action, server->scfg.systemd_service, error.message); - goto out; - } - - r = sd_bus_message_read(m, "o", &path); - if (r < 0) { - error("failed to parse response message: %s", strerror(-r)); - goto out; - } - - verbose("action %s queued for service %s", action, - server->scfg.systemd_service); - performed = true; - -out: - sd_bus_error_free(&error); - sd_bus_message_unref(m); - return performed; -} - -/* - * Stop systemd service. - * - * This is equivalent to (assuming service mc@world1): - * gdbus call --session - * --dest org.freedesktop.systemd1 - * --object-path /org/freedesktop/systemd1/unit/mc_40world1_2eservice - * --method org.freedesktop.systemd1.Unit.Stop "fail" - */ -bool systemd_service_stop(struct server *server) -{ - assert_return(server, false); - - return systemd_service_action(server, "Stop"); -} - -/* - * Start systemd service. - * - * This is equivalent to (assuming service mc@world1): - * gdbus call --session - * --dest org.freedesktop.systemd1 - * --object-path /org/freedesktop/systemd1/unit/mc_40world1_2eservice - * --method org.freedesktop.systemd1.Unit.Start "fail" - */ -bool systemd_service_start(struct server *server) -{ - assert_return(server, false); - - return systemd_service_action(server, "Start"); -} diff --git a/minecproxy/systemd.h b/minecproxy/systemd.h deleted file mode 100644 index f3f96e4..0000000 --- a/minecproxy/systemd.h +++ /dev/null @@ -1,13 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0 */ -#ifndef foosystemdhfoo -#define foosystemdhfoo - -void systemd_delete(); - -bool systemd_service_running(struct server *server); - -bool systemd_service_stop(struct server *server); - -bool systemd_service_start(struct server *server); - -#endif |