diff options
author | David Härdeman <david@hardeman.nu> | 2020-07-12 02:30:31 +0200 |
---|---|---|
committer | David Härdeman <david@hardeman.nu> | 2020-07-12 02:30:31 +0200 |
commit | f06ff46151c38b2259c5ee4ae83d38c6f6fc0c8c (patch) | |
tree | 44f6d00604a153300eb3be9dcec96d17030727b8 /minecproxy | |
parent | d198326ef6ec3031afbe7ee47b727cc52fc1198a (diff) |
Teach minecctl to create proper mcserver files, auto-generate systemd service names as necessary
Diffstat (limited to 'minecproxy')
-rw-r--r-- | minecproxy/server-config.c | 14 | ||||
-rw-r--r-- | minecproxy/server-proxy.c | 36 | ||||
-rw-r--r-- | minecproxy/server-rcon.c | 26 | ||||
-rw-r--r-- | minecproxy/server.c | 71 | ||||
-rw-r--r-- | minecproxy/server.h | 1 |
5 files changed, 65 insertions, 83 deletions
diff --git a/minecproxy/server-config.c b/minecproxy/server-config.c index 7009952..959f1d0 100644 --- a/minecproxy/server-config.c +++ b/minecproxy/server-config.c @@ -39,18 +39,18 @@ static void server_cfg_read_cb(struct uring_task *task, int res) assert_task_alive(DBG_CFG, task); if (res <= 0) { - error("error reading config file for %s: %s", server->name, + error("error reading config file for %s: %s", server->scfg.name, strerror(-res)); server_delete(server); } - debug(DBG_CFG, "%s: parsing cfg (%i bytes)", server->name, res); + debug(DBG_CFG, "%s: parsing cfg (%i bytes)", server->scfg.name, res); uring_task_close_fd(&server->task); if (!scfg_parse(&server->scfg, server->tbuf.buf, true, &lineno, &error)) { error("%s: failed to parse config file, line %u: %s", - server->name, lineno, error); + server->scfg.name, lineno, error); server_delete(server); return; } @@ -74,12 +74,12 @@ static void server_cfg_open_cb(struct uring_task *task, int res) assert_task_alive(DBG_CFG, task); if (res < 0) { - error("open(%s) failed: %s", server->name, strerror(-res)); + error("open(%s) failed: %s", server->scfg.name, strerror(-res)); server_delete(server); return; } - debug(DBG_CFG, "reading server cfg %s (fd %i)", server->name, res); + debug(DBG_CFG, "reading server cfg %s (fd %i)", server->scfg.name, res); uring_task_set_fd(&server->task, res); uring_tbuf_read_until_eof(&server->task, server_cfg_read_cb); } @@ -184,8 +184,8 @@ static void inotify_cb(struct uring_task *task, int res) (IN_MOVED_TO | IN_CREATE | IN_CLOSE_WRITE)) { server = server_new(event->name); verbose("New server config file detected: %s", - server->name); - uring_openat(&server->task, server->name, + event->name); + uring_openat(&server->task, event->name, server_cfg_open_cb); } else error("inotify: unknown event: 0x%08x", event->mask); diff --git a/minecproxy/server-proxy.c b/minecproxy/server-proxy.c index 9d7b59e..e486a73 100644 --- a/minecproxy/server-proxy.c +++ b/minecproxy/server-proxy.c @@ -70,7 +70,7 @@ static void proxy_free(struct uring_task *task) assert_return(task); - debug(DBG_PROXY, "server: %s, src: %s, dst: %s", proxy->server->name, + debug(DBG_PROXY, "server: %s, src: %s, dst: %s", proxy->server->scfg.name, proxy->client_conn.remote.addrstr, proxy->server_conn.remote.addrstr); @@ -82,7 +82,7 @@ static void proxy_free(struct uring_task *task) info("%s: proxy connection %s -> %s closed " "(CtS: %s, StC: %s), duration %s", - proxy->server->name, proxy->client_conn.remote.addrstr, + proxy->server->scfg.name, proxy->client_conn.remote.addrstr, proxy->server_conn.remote.addrstr, cts, stc, duration); } @@ -97,7 +97,7 @@ static void proxy_client_free(struct uring_task *task) assert_return(task); - debug(DBG_PROXY, "%s: client connection closed", proxy->server->name); + debug(DBG_PROXY, "%s: client connection closed", proxy->server->scfg.name); } static void proxy_server_free(struct uring_task *task) @@ -107,12 +107,12 @@ static void proxy_server_free(struct uring_task *task) assert_return(task); - debug(DBG_PROXY, "%s: server connection closed", proxy->server->name); + debug(DBG_PROXY, "%s: server connection closed", proxy->server->scfg.name); } void proxy_delete(struct server_proxy *proxy) { - debug(DBG_PROXY, "%s: shutting down proxy %p", proxy->server->name, + debug(DBG_PROXY, "%s: shutting down proxy %p", proxy->server->scfg.name, proxy); assert_return(proxy); @@ -147,7 +147,7 @@ static void proxy_client_written_out(struct uring_task *task, int res) assert_task_alive(DBG_PROXY, task); if (res <= 0) { - debug(DBG_PROXY, "%s: res: %i", proxy->server->name, res); + debug(DBG_PROXY, "%s: res: %i", proxy->server->scfg.name, res); proxy_delete(proxy); return; } @@ -166,7 +166,7 @@ static void proxy_client_read_in(struct uring_task *task, int res) assert_task_alive(DBG_PROXY, task); if (res <= 0) { - debug(DBG_PROXY, "%s: res: %i", proxy->server->name, res); + debug(DBG_PROXY, "%s: res: %i", proxy->server->scfg.name, res); proxy_delete(proxy); return; } @@ -186,7 +186,7 @@ static void proxy_server_written_out(struct uring_task *task, int res) assert_task_alive(DBG_PROXY, task); if (res <= 0) { - debug(DBG_PROXY, "%s: res: %i", proxy->server->name, res); + debug(DBG_PROXY, "%s: res: %i", proxy->server->scfg.name, res); proxy_delete(proxy); return; } @@ -205,7 +205,7 @@ static void proxy_server_read_in(struct uring_task *task, int res) assert_task_alive(DBG_PROXY, task); if (res <= 0) { - debug(DBG_PROXY, "%s: res: %i", proxy->server->name, res); + debug(DBG_PROXY, "%s: res: %i", proxy->server->scfg.name, res); proxy_delete(proxy); return; } @@ -228,7 +228,7 @@ static void proxy_client_spliced_out(struct uring_task *task, int res) assert_task_alive(DBG_PROXY, task); if (res <= 0) { - debug(DBG_PROXY, "%s: res: %i", proxy->server->name, res); + debug(DBG_PROXY, "%s: res: %i", proxy->server->scfg.name, res); proxy_delete(proxy); return; } @@ -247,7 +247,7 @@ static void proxy_client_spliced_in(struct uring_task *task, int res) assert_task_alive(DBG_PROXY, task); if (res <= 0) { - debug(DBG_PROXY, "%s: res: %i", proxy->server->name, res); + debug(DBG_PROXY, "%s: res: %i", proxy->server->scfg.name, res); proxy_delete(proxy); return; } @@ -267,7 +267,7 @@ static void proxy_server_spliced_out(struct uring_task *task, int res) assert_task_alive(DBG_PROXY, task); if (res <= 0) { - debug(DBG_PROXY, "%s: res: %i", proxy->server->name, res); + debug(DBG_PROXY, "%s: res: %i", proxy->server->scfg.name, res); proxy_delete(proxy); return; } @@ -286,7 +286,7 @@ static void proxy_server_spliced_in(struct uring_task *task, int res) assert_task_alive(DBG_PROXY, task); if (res <= 0) { - debug(DBG_PROXY, "%s: res: %i", proxy->server->name, res); + debug(DBG_PROXY, "%s: res: %i", proxy->server->scfg.name, res); proxy_delete(proxy); return; } @@ -307,7 +307,7 @@ static void proxy_connected_cb(struct connection *conn, bool connected) proxy->connecting = false; if (!connected) { error("%s: proxy connection to remote server failed", - proxy->server->name); + proxy->server->scfg.name); if (!proxy->ptask.active) proxy_delete(proxy); return; @@ -316,7 +316,7 @@ static void proxy_connected_cb(struct connection *conn, bool connected) ptimer_del_task(&proxy->ptask); proxy->sfd = proxy->servertask.fd; - verbose("%s: proxy connection %s -> %s opened", proxy->server->name, + verbose("%s: proxy connection %s -> %s opened", proxy->server->scfg.name, proxy->client_conn.remote.addrstr, proxy->server_conn.remote.addrstr); proxy->begin = time(NULL); @@ -439,7 +439,7 @@ static void local_accept(struct uring_task *task, int res) assert_return(task); assert_task_alive(DBG_PROXY, task); - debug(DBG_PROXY, "task %p, res %i, server %s", task, res, server->name); + debug(DBG_PROXY, "task %p, res %i, server %s", task, res, server->scfg.name); if (res < 0) { error("res: %i", res); @@ -448,7 +448,7 @@ static void local_accept(struct uring_task *task, int res) saddr_set_addrstr(&local->client); - verbose("%s: incoming proxy connection: %s -> %s", server->name, + verbose("%s: incoming proxy connection: %s -> %s", server->scfg.name, local->client.addrstr, local->local.addrstr); if (list_empty(&server->scfg.remotes)) { @@ -571,7 +571,7 @@ struct server_local *local_new(struct server *server, struct saddr *saddr) return NULL; } - debug(DBG_PROXY, "%s adding local: %s", server->name, saddr->addrstr); + debug(DBG_PROXY, "%s adding local: %s", server->scfg.name, saddr->addrstr); local->local = *saddr; local->server = server; uring_task_init(&local->task, "local", &server->task, local_free); diff --git a/minecproxy/server-rcon.c b/minecproxy/server-rcon.c index c8a91c3..a728bab 100644 --- a/minecproxy/server-rcon.c +++ b/minecproxy/server-rcon.c @@ -32,14 +32,14 @@ static bool rcon_check_reply(struct uring_task *task, int res, int32_t *id, const char *error; if (res < 0) { - error("rcon(%s): reading reply failed, res: %i", server->name, - res); + error("rcon(%s): reading reply failed, res: %i", + server->scfg.name, res); goto error; } if (!rcon_protocol_read_packet(task->tbuf->buf, task->tbuf->len, id, type, msg, &error)) { - error("rcon(%s): failed to parse packet: %s", server->name, + error("rcon(%s): failed to parse packet: %s", server->scfg.name, error); goto error; } @@ -66,10 +66,10 @@ static void rcon_stop_reply(struct uring_task *task, int res) if (!rcon_protocol_verify_response(2, id, RCON_PACKET_COMMAND, type, &error)) - error("rcon(%s): stop cmd failed - %s", server->name, error); + error("rcon(%s): stop cmd failed - %s", server->scfg.name, error); else verbose("rcon(%s): stop command sent, reply: %s", - server->name, msg); + server->scfg.name, msg); uring_task_close_fd(task); } @@ -83,12 +83,12 @@ static void rcon_stop_sent(struct uring_task *task, int res) if (res != task->tbuf->len) { error("rcon(%s): sending stop cmd failed, res: %i", - server->name, res); + server->scfg.name, res); uring_task_close_fd(task); return; } - debug(DBG_RCON, "rcon(%s): stop cmd sent", server->name); + debug(DBG_RCON, "rcon(%s): stop cmd sent", server->scfg.name); uring_tbuf_read_until(task, rcon_packet_complete, rcon_stop_reply); } @@ -107,11 +107,11 @@ static void rcon_login_reply(struct uring_task *task, int res) if (!rcon_protocol_verify_response(1, id, RCON_PACKET_LOGIN, type, &error)) { - error("rcon(%s): login failed - %s", server->name, error); + error("rcon(%s): login failed - %s", server->scfg.name, error); goto error; } - debug(DBG_RCON, "rcon(%s): login successful", server->name); + debug(DBG_RCON, "rcon(%s): login successful", server->scfg.name); rcon_protocol_create_packet(task->tbuf->buf, sizeof(task->tbuf->buf), &task->tbuf->len, 2, RCON_PACKET_COMMAND, "stop"); @@ -130,13 +130,13 @@ static void rcon_login_sent(struct uring_task *task, int res) assert_task_alive(DBG_RCON, task); if (res != task->tbuf->len) { - error("rcon(%s): sending login failed, res: %i", server->name, + error("rcon(%s): sending login failed, res: %i", server->scfg.name, res); uring_task_close_fd(task); return; } - debug(DBG_RCON, "rcon(%s): login sent", server->name); + debug(DBG_RCON, "rcon(%s): login sent", server->scfg.name); uring_tbuf_read_until(task, rcon_packet_complete, rcon_login_reply); } @@ -148,7 +148,7 @@ static void rcon_connected_cb(struct connection *conn, bool connected) assert_task_alive(DBG_RCON, &server->rcon_task); if (!connected) { - error("rcon (%s): connection failed", server->name); + error("rcon (%s): connection failed", server->scfg.name); return; } @@ -166,7 +166,7 @@ static void rcon_free(struct uring_task *task) assert_return(task); - debug(DBG_RCON, "task %p, server %s (%p)", task, server->name, server); + debug(DBG_RCON, "task %p, server %s (%p)", task, server->scfg.name, server); } void rcon_stop(struct server *server) diff --git a/minecproxy/server.c b/minecproxy/server.c index b1e9b05..7a992d1 100644 --- a/minecproxy/server.c +++ b/minecproxy/server.c @@ -46,9 +46,8 @@ static void server_free(struct uring_task *task) assert_return(task); - debug(DBG_SRV, "freeing server %s (%p)", server->name, server); + debug(DBG_SRV, "freeing server (%p)", server); list_del(&server->list); - xfree(server->name); xfree(server); } @@ -59,7 +58,7 @@ void server_delete(struct server *server) assert_return(server); - verbose("Removing server %s", server->name); + verbose("Removing server %s", server->scfg.name); server->state = SERVER_STATE_DEAD; rcon_delete(server); @@ -101,7 +100,7 @@ static void server_dump(struct server *server) assert_return(server); - verbose("Server %s:", server->name); + verbose("Server %s:", server->scfg.name); verbose(" * Filename: %s", server->scfg.filename); switch (server->scfg.type) { case SERVER_TYPE_ANNOUNCE: @@ -225,7 +224,7 @@ static bool server_check_running(struct server *server) /* FIXME: other methods, rcon? */ if (server->scfg.systemd_service) { verbose("%s: checking if systemd service is running", - server->name); + server->scfg.name); if (systemd_service_running(&server->scfg, &error)) { server->state = SERVER_STATE_RUNNING; return true; @@ -246,12 +245,12 @@ bool server_start(struct server *server) switch (server->scfg.start_method) { case SERVER_START_METHOD_EXEC: verbose("Starting server %s via external cmd", - server->name); + server->scfg.name); return server_exec(server, server->scfg.start_exec); case SERVER_START_METHOD_SYSTEMD: verbose("Starting server %s via systemd (%s)", - server->name, + server->scfg.name, server->scfg.systemd_service); if (systemd_service_start(&server->scfg)) { @@ -276,12 +275,12 @@ bool server_stop(struct server *server) switch (server->scfg.stop_method) { case SERVER_STOP_METHOD_EXEC: verbose("Stopping server %s via external cmd", - server->name); + server->scfg.name); return server_exec(server, server->scfg.stop_exec); case SERVER_STOP_METHOD_SYSTEMD: verbose("Stopping server %s via systemd (%s)", - server->name, + server->scfg.name, server->scfg.systemd_service); if (systemd_service_stop(&server->scfg)) { server->state = SERVER_STATE_STOPPED; @@ -290,7 +289,7 @@ bool server_stop(struct server *server) return server_check_running(server); case SERVER_STOP_METHOD_RCON: - verbose("Stopping server %s via rcon", server->name); + verbose("Stopping server %s via rcon", server->scfg.name); rcon_stop(server); return true; @@ -315,7 +314,7 @@ void server_update_active_players(struct server *server, int count) assert_task_alive(DBG_IDLE, &server->idle_task); debug(DBG_IDLE, "%s: currently %i active players", - server->name, count); + server->scfg.name, count); if (count < 0) return; @@ -327,7 +326,7 @@ void server_update_active_players(struct server *server, int count) server->idle_count++; if (server->idle_count > server->scfg.idle_timeout) { - verbose("stopping idle server %s", server->name); + verbose("stopping idle server %s", server->scfg.name); server_stop(server); } } @@ -342,7 +341,7 @@ static void server_idle_connected_cb(struct connection *conn, bool connected) if (!connected) { debug(DBG_IDLE, "idle check connection to remote server (%s) failed", - server->name); + server->scfg.name); server->idle_count = 0; server->state = SERVER_STATE_STOPPED; return; @@ -390,11 +389,11 @@ static void server_announce_cb(struct uring_task *task, int res) assert_return(task); if (res < 0) - error("%s: failure %i", server->name, res); + error("%s: failure %i", server->scfg.name, res); else if (res == server->ann_buf.len) - debug(DBG_ANN, "%s: ok (%i)", server->name, res); + debug(DBG_ANN, "%s: ok (%i)", server->scfg.name, res); else - debug(DBG_ANN, "%s: unexpected result: %i", server->name, res); + debug(DBG_ANN, "%s: unexpected result: %i", server->scfg.name, res); uring_task_set_fd(&server->ann_task, -1); } @@ -410,7 +409,7 @@ bool server_announce(struct server *server, int fd) server->state != SERVER_STATE_RUNNING)) return false; - debug(DBG_ANN, "announcing server: %s", server->name); + debug(DBG_ANN, "announcing server: %s", server->scfg.name); uring_task_set_fd(&server->ann_task, fd); uring_tbuf_sendmsg(&server->ann_task, server_announce_cb); return true; @@ -422,12 +421,12 @@ bool server_commit(struct server *server) struct saddr *saddr, *tmp; int r; - assert_return(server && server->name, false); + assert_return(server && server->scfg.name, false); assert_task_alive_or(DBG_SRV, &server->task, return false); if (!list_empty(&server->scfg.dnslookups)) { debug(DBG_SRV, "%s: called with pending DNS requests", - server->name); + server->scfg.name); return false; } @@ -437,23 +436,23 @@ bool server_commit(struct server *server) } if (!list_empty(&server->proxys)) { - error("%s: proxys not empty?", server->name); + error("%s: proxys not empty?", server->scfg.name); return false; } if (!scfg_validate(&server->scfg, &error)) { error("%s: failed to validate config file (%s)", - server->name, error); + server->scfg.name, error); server_delete(server); return false; } r = snprintf(server->ann_buf.buf, sizeof(server->ann_buf.buf), "[MOTD]%s[/MOTD][AD]%" PRIu16 "[/AD]", - server->scfg.pretty_name ? server->scfg.pretty_name : server->name, + server->scfg.pretty_name ? server->scfg.pretty_name : server->scfg.name, server->scfg.announce_port); if (r < 1 || r >= sizeof(server->ann_buf.buf)) { - error("%s: unable to create announce msg: %i\n", server->name, + error("%s: unable to create announce msg: %i\n", server->scfg.name, r); return false; } @@ -469,14 +468,14 @@ bool server_commit(struct server *server) local = local_new(server, saddr); if (!local) { error("%s: failed to create local listener", - server->name); + server->scfg.name); server_delete(server); return false; } list_add(&local->list, &server->listenings); if (!local_open(local)) { error("%s: failed to open listening port", - server->name); + server->scfg.name); server_delete(server); return false; } @@ -486,19 +485,18 @@ bool server_commit(struct server *server) server_check_running(server); - debug(DBG_SRV, "%s: success", server->name); + debug(DBG_SRV, "%s: success", server->scfg.name); return true; } struct server *server_new(const char *filename) { struct server *server; - const char *suffix; assert_return(!empty_str(filename), NULL); list_for_each_entry(server, &cfg->servers, list) { - if (!streq(filename, server->name)) + if (!streq(filename, server->scfg.filename)) continue; error("attempt to add duplicate server: %s", filename); return server; @@ -511,29 +509,14 @@ struct server *server_new(const char *filename) return NULL; } - suffix = strrchr(filename, '.'); - if (!suffix || suffix == filename) { - error("invalid filename: %s", filename); - xfree(server); - return NULL; - } - - server->name = xstrndup(filename, suffix - filename); - if (!server->name) { - error("failed to create server name: %s", filename); - xfree(server); - return NULL; - } - if (!scfg_init(&server->scfg, filename)) { - xfree(server->name); xfree(server); return NULL; } server->state = SERVER_STATE_INIT; - uring_task_init(&server->task, server->name, uring_parent(), + uring_task_init(&server->task, server->scfg.name, uring_parent(), server_free); uring_task_set_buf(&server->task, &server->tbuf); diff --git a/minecproxy/server.h b/minecproxy/server.h index 8058d42..41aaf41 100644 --- a/minecproxy/server.h +++ b/minecproxy/server.h @@ -15,7 +15,6 @@ enum server_state { /* clang-format on */ struct server { - char *name; struct server_config scfg; struct list_head listenings; struct list_head proxys; |