From 31afd2aaaeb76a12e232eab0fb6a550b73948737 Mon Sep 17 00:00:00 2001 From: David Härdeman Date: Mon, 22 Jun 2020 10:50:15 +0200 Subject: Make struct cfg global and make the corresponding changes throughout --- announce.c | 40 ++++++------ announce.h | 10 +-- cfgdir.c | 124 ++++++++++++++++++------------------- cfgdir.h | 6 +- config.c | 9 ++- config.h | 4 +- idle.c | 62 +++++++++---------- idle.h | 8 +-- igmp.c | 45 ++++++-------- igmp.h | 6 +- main.c | 95 +++++++++++++---------------- main.h | 6 +- proxy.c | 80 ++++++++++++------------ proxy.h | 5 +- rcon.c | 125 +++++++++++++++++++------------------- rcon.h | 4 +- server.c | 202 ++++++++++++++++++++++++++++++------------------------------- server.h | 53 +++++++--------- systemd.c | 35 +++++------ systemd.h | 18 ++---- uring.c | 188 ++++++++++++++++++++++++++++---------------------------- uring.h | 59 ++++++++---------- utils.c | 48 +++++++-------- utils.h | 17 +++--- 24 files changed, 594 insertions(+), 655 deletions(-) diff --git a/announce.c b/announce.c index 9944851..1225868 100644 --- a/announce.c +++ b/announce.c @@ -18,12 +18,12 @@ struct announce { }; static void -announce_cb(struct cfg *cfg, struct uring_task *task, int res) +announce_cb(struct uring_task *task, int res) { struct announce *aev = container_of(task, struct announce, task); struct server *server; - assert_return(cfg && task); + assert_return(task); assert_task_alive(DBG_ANN, task); if (res != sizeof(aev->value)) { @@ -33,8 +33,8 @@ announce_cb(struct cfg *cfg, struct uring_task *task, int res) debug(DBG_ANN, "timerfd value %" PRIu64, aev->value); list_for_each_entry(server, &cfg->servers, list) - server_announce(cfg, server, aev->mcast_fd); - uring_read(cfg, &aev->task, &aev->value, sizeof(aev->value), announce_cb); + server_announce(server, aev->mcast_fd); + uring_read(&aev->task, &aev->value, sizeof(aev->value), announce_cb); } static void @@ -50,25 +50,25 @@ announce_free(struct uring_task *task) } void -announce_refdump(struct announce *aev) +announce_refdump() { - assert_return_silent(aev); + assert_return_silent(cfg->aev); - uring_task_refdump(&aev->task); + uring_task_refdump(&cfg->aev->task); } void -announce_delete(struct cfg *cfg) +announce_delete() { - assert_return(cfg && cfg->aev); + assert_return(cfg->aev); debug(DBG_ANN, "closing fd %i", cfg->aev->task.fd); - uring_task_destroy(cfg, &cfg->aev->task); + uring_task_destroy(&cfg->aev->task); cfg->aev = NULL; } void -announce_stop(struct announce *aev) +announce_stop() { struct itimerspec tspec = { .it_interval = { @@ -81,14 +81,14 @@ announce_stop(struct announce *aev) } }; - assert_return(aev); + assert_return(cfg->aev); - if (timerfd_settime(aev->task.fd, 0, &tspec, NULL) != 0) + if (timerfd_settime(cfg->aev->task.fd, 0, &tspec, NULL) != 0) error("timerfd_settime: %m"); } void -announce_start(struct announce *aev) +announce_start() { struct itimerspec tspec = { .it_interval = { @@ -101,20 +101,20 @@ announce_start(struct announce *aev) } }; - assert_return(aev); + assert_return(cfg->aev); - if (timerfd_settime(aev->task.fd, 0, &tspec, NULL) != 0) + if (timerfd_settime(cfg->aev->task.fd, 0, &tspec, NULL) != 0) error("timerfd_settime: %m"); } void -announce_init(struct cfg *cfg) +announce_init() { struct announce *aev; int afd; int sfd; - assert_return(cfg); + assert_return(!cfg->aev); aev = zmalloc(sizeof(*aev)); if (!aev) @@ -128,10 +128,10 @@ announce_init(struct cfg *cfg) if (sfd < 0) die("socket: %m"); - uring_task_init(cfg, &aev->task, "announce", uring_parent(cfg), announce_free); + uring_task_init(&aev->task, "announce", uring_parent(), announce_free); uring_task_set_fd(&aev->task, afd); aev->mcast_fd = sfd; cfg->aev = aev; - uring_read(cfg, &aev->task, &aev->value, sizeof(aev->value), announce_cb); + uring_read(&aev->task, &aev->value, sizeof(aev->value), announce_cb); } diff --git a/announce.h b/announce.h index 6f805f1..c4bd8e3 100644 --- a/announce.h +++ b/announce.h @@ -1,14 +1,14 @@ #ifndef fooannouncehfoo #define fooannouncehfoo -void announce_refdump(struct announce *aev); +void announce_refdump(); -void announce_delete(struct cfg *cfg); +void announce_delete(); -void announce_stop(struct announce *aev); +void announce_stop(); -void announce_start(struct announce *aev); +void announce_start(); -void announce_init(struct cfg *cfg); +void announce_init(); #endif diff --git a/cfgdir.c b/cfgdir.c index 3e8cef3..ec8a0bf 100644 --- a/cfgdir.c +++ b/cfgdir.c @@ -17,10 +17,9 @@ #include "server.h" static void -scfg_dns_cb(struct dns_async *dns, bool (*server_cb)(struct cfg *, struct server *, struct saddr *)) +scfg_dns_cb(struct dns_async *dns, bool (*server_cb)(struct server *, struct saddr *)) { struct server *server; - struct cfg *cfg; struct sockaddr_in *in4; struct sockaddr_in6 *in6; struct saddr *saddr; @@ -30,8 +29,6 @@ scfg_dns_cb(struct dns_async *dns, bool (*server_cb)(struct cfg *, struct server assert_return(dns && dns->priv && server_cb); server = dns->priv; - cfg = server->cfg; - debug(DBG_DNS, "called, dns: %p, name: %s, server: %p, server->name: %s", dns, dns->name, server, server->name); @@ -62,13 +59,13 @@ scfg_dns_cb(struct dns_async *dns, bool (*server_cb)(struct cfg *, struct server case AF_INET: in4 = (struct sockaddr_in *)ai->ai_addr; saddr_set_ipv4(saddr, in4->sin_addr.s_addr, in4->sin_port); - server_cb(cfg, server, saddr); + server_cb(server, saddr); break; case AF_INET6: in6 = (struct sockaddr_in6 *)ai->ai_addr; saddr_set_ipv6(saddr, &in6->sin6_addr, in6->sin6_port); - server_cb(cfg, server, saddr); + server_cb(server, saddr); break; default: @@ -83,8 +80,8 @@ out: freeaddrinfo(results); list_del(&dns->list); xfree(dns); - uring_task_put(cfg, &server->task); - server_commit(cfg, server); + uring_task_put(&server->task); + server_commit(server); } static void @@ -189,14 +186,14 @@ struct cfg_key_value_map scfg_key_map[] = { }; static bool -handle_dns(struct cfg *cfg, struct server *server, const char *type, +handle_dns(struct server *server, const char *type, struct cfg_value *value, dns_cb_t *async_cb, - bool (*sync_cb)(struct cfg *, struct server *, struct saddr *)) + bool (*sync_cb)(struct server *, struct saddr *)) { struct saddr *saddr, *tmp; struct dns_async *dns; - assert_return(cfg && server && type && value && async_cb && sync_cb, false); + assert_return(server && type && value && async_cb && sync_cb, false); switch (value->type) { case CFG_VAL_TYPE_ADDRS: @@ -204,7 +201,7 @@ handle_dns(struct cfg *cfg, struct server *server, const char *type, list_for_each_entry_safe(saddr, tmp, &value->saddrs, list) { list_del(&saddr->list); - sync_cb(cfg, server, saddr); + sync_cb(server, saddr); } return true; @@ -216,7 +213,7 @@ handle_dns(struct cfg *cfg, struct server *server, const char *type, dns->cb = async_cb; dns->priv = server; list_add(&dns->list, &server->dnslookups); - uring_task_get(cfg, &server->task); + uring_task_get(&server->task); return true; default: @@ -225,15 +222,15 @@ handle_dns(struct cfg *cfg, struct server *server, const char *type, } static void -scfg_parse(struct cfg *cfg, struct server *server) +scfg_parse(struct server *server) { char *pos; - assert_return(cfg && server); + assert_return(server); pos = server->tbuf.buf; - if (!config_parse_header(cfg, server->name, "server", &pos)) + if (!config_parse_header(server->name, "server", &pos)) return; while (true) { @@ -241,7 +238,7 @@ scfg_parse(struct cfg *cfg, struct server *server) const char *keyname; struct cfg_value value; - if (!config_parse_line(cfg, server->name, &pos, scfg_key_map, + if (!config_parse_line(server->name, &pos, scfg_key_map, &key, &keyname, &value)) break; @@ -254,87 +251,87 @@ scfg_parse(struct cfg *cfg, struct server *server) case SCFG_KEY_TYPE: if (streq(value.str, "proxy")) { - if (!server_set_type(cfg, server, SERVER_TYPE_PROXY)) + if (!server_set_type(server, SERVER_TYPE_PROXY)) return; } else if (streq(value.str, "announce")) { - if (!server_set_type(cfg, server, SERVER_TYPE_ANNOUNCE)) + if (!server_set_type(server, SERVER_TYPE_ANNOUNCE)) return; } break; case SCFG_KEY_NAME: - if (!server_set_pretty_name(cfg, server, value.str)) + if (!server_set_pretty_name(server, value.str)) return; break; case SCFG_KEY_PORT: - if (!server_set_port(cfg, server, value.uint16)) + if (!server_set_port(server, value.uint16)) return; break; case SCFG_KEY_LOCAL: - if (!handle_dns(cfg, server, "local", &value, + if (!handle_dns(server, "local", &value, scfg_local_dns_cb, server_add_local)) return; break; case SCFG_KEY_REMOTE: - if (!handle_dns(cfg, server, "remote", &value, + if (!handle_dns(server, "remote", &value, scfg_remote_dns_cb, server_add_remote)) return; break; case SCFG_KEY_IDLE_TIMEOUT: - if (!server_set_idle_timeout(cfg, server, value.uint16)) + if (!server_set_idle_timeout(server, value.uint16)) return; break; case SCFG_KEY_STOP_METHOD: if (streq(value.str, "exec")) { - if (server_set_stop_method(cfg, server, SERVER_STOP_METHOD_EXEC)) + if (server_set_stop_method(server, SERVER_STOP_METHOD_EXEC)) break; } else if (streq(value.str, "rcon")) { - if (server_set_stop_method(cfg, server, SERVER_STOP_METHOD_RCON)) + if (server_set_stop_method(server, SERVER_STOP_METHOD_RCON)) break; } else if (streq(value.str, "systemd")) { - if (server_set_stop_method(cfg, server, SERVER_STOP_METHOD_SYSTEMD)) + if (server_set_stop_method(server, SERVER_STOP_METHOD_SYSTEMD)) break; } return; case SCFG_KEY_START_METHOD: if (streq(value.str, "exec")) { - if (server_set_start_method(cfg, server, SERVER_START_METHOD_EXEC)) + if (server_set_start_method(server, SERVER_START_METHOD_EXEC)) break; } else if (streq(value.str, "systemd")) { - if (server_set_start_method(cfg, server, SERVER_START_METHOD_SYSTEMD)) + if (server_set_start_method(server, SERVER_START_METHOD_SYSTEMD)) break; } return; case SCFG_KEY_STOP_EXEC: - if (!server_set_stop_exec(cfg, server, value.str)) + if (!server_set_stop_exec(server, value.str)) return; break; case SCFG_KEY_START_EXEC: - if (!server_set_start_exec(cfg, server, value.str)) + if (!server_set_start_exec(server, value.str)) return; break; case SCFG_KEY_RCON: - if (!handle_dns(cfg, server, "rcon", &value, + if (!handle_dns(server, "rcon", &value, scfg_rcon_dns_cb, server_add_rcon)) return; break; case SCFG_KEY_RCON_PASSWORD: - if (!server_set_rcon_password(cfg, server, value.str)) + if (!server_set_rcon_password(server, value.str)) return; break; case SCFG_KEY_SYSTEMD_SERVICE: - if (!server_set_systemd_service(cfg, server, value.str)) + if (!server_set_systemd_service(server, value.str)) return; break; @@ -346,42 +343,42 @@ scfg_parse(struct cfg *cfg, struct server *server) } static void -scfg_read_cb(struct cfg *cfg, struct uring_task *task, int res) +scfg_read_cb(struct uring_task *task, int res) { struct server *server = container_of(task, struct server, task); - assert_return(cfg && task); + assert_return(task); assert_task_alive(DBG_CFG, task); if (res <= 0) { error("error reading config file for %s: %s", server->name, strerror(-res)); - server_delete(cfg, server); + server_delete(server); } debug(DBG_CFG, "%s: parsing cfg (%i bytes)", server->name, res); - uring_task_close_fd(cfg, &server->task); - scfg_parse(cfg, server); - server_commit(cfg, server); + uring_task_close_fd(&server->task); + scfg_parse(server); + server_commit(server); } static void -scfg_open_cb(struct cfg *cfg, struct uring_task *task, int res) +scfg_open_cb(struct uring_task *task, int res) { struct server *server = container_of(task, struct server, task); - assert_return(cfg && task); + assert_return(task); assert_task_alive(DBG_CFG, task); if (res < 0) { error("open(%s) failed: %s", server->name, strerror(-res)); - server_delete(cfg, server); + server_delete(server); return; } debug(DBG_CFG, "reading server cfg %s (fd %i)", server->name, res); uring_task_set_fd(&server->task, res); - uring_tbuf_read_until_eof(cfg, &server->task, scfg_read_cb); + uring_tbuf_read_until_eof(&server->task, scfg_read_cb); } static bool @@ -410,9 +407,8 @@ static void inotify_free(struct uring_task *task) { struct inotify_ev *iev = container_of(task, struct inotify_ev, task); - struct cfg *cfg = container_of(task->parent, struct cfg, task); - assert_return(task && iev && cfg); + assert_return(task); debug(DBG_CFG, "called"); xfree(iev); @@ -463,14 +459,14 @@ inotify_event_dump(const struct inotify_event *event) } static void -inotify_cb(struct cfg *cfg, struct uring_task *task, int res) +inotify_cb(struct uring_task *task, int res) { struct inotify_ev *iev = container_of(task, struct inotify_ev, task); const struct inotify_event *event; char *ptr; struct server *server; - assert_return(cfg && task); + assert_return(task); assert_task_alive(DBG_CFG, task); if (res <= 0) { @@ -496,38 +492,38 @@ inotify_cb(struct cfg *cfg, struct uring_task *task, int res) continue; if (event->mask & (IN_MOVED_FROM | IN_DELETE)) - server_delete_by_name(cfg, event->name); + server_delete_by_name(event->name); else if (event->mask & (IN_MOVED_TO | IN_CREATE | IN_CLOSE_WRITE)) { - server = server_new(cfg, event->name); + server = server_new(event->name); verbose("New server config file detected: %s", server->name); - uring_openat(cfg, &server->task, server->name, scfg_open_cb); + uring_openat(&server->task, server->name, scfg_open_cb); } else error("inotify: unknown event: 0x%08x", event->mask); } - uring_read(cfg, &iev->task, iev->buf, sizeof(iev->buf), inotify_cb); + uring_read(&iev->task, iev->buf, sizeof(iev->buf), inotify_cb); } void -cfgdir_refdump(struct inotify_ev *iev) +cfgdir_refdump() { - assert_return(iev); + assert_return_silent(cfg->iev); - uring_task_refdump(&iev->task); + uring_task_refdump(&cfg->iev->task); } void -cfgdir_delete(struct cfg *cfg) +cfgdir_delete() { - assert_return(cfg && cfg->iev); + assert_return(cfg->iev); debug(DBG_CFG, "closing fd %i", cfg->iev->task.fd); - uring_task_destroy(cfg, &cfg->iev->task); + uring_task_destroy(&cfg->iev->task); cfg->iev = NULL; } void -cfgdir_init(struct cfg *cfg) +cfgdir_init() { int ifd; int iwd; @@ -536,7 +532,7 @@ cfgdir_init(struct cfg *cfg) struct dirent *dent; struct server *server; - assert_return(cfg); + assert_return(!cfg->iev); iev = zmalloc(sizeof(*iev)); if (!iev) @@ -555,10 +551,10 @@ cfgdir_init(struct cfg *cfg) if (iwd < 0) die("inotify_add_watch: %m"); - uring_task_init(cfg, &iev->task, "server_config", uring_parent(cfg), inotify_free); + uring_task_init(&iev->task, "server_config", uring_parent(), inotify_free); uring_task_set_fd(&iev->task, ifd); cfg->iev = iev; - uring_read(cfg, &iev->task, iev->buf, sizeof(iev->buf), inotify_cb); + uring_read(&iev->task, iev->buf, sizeof(iev->buf), inotify_cb); dir = opendir("."); if (!dir) @@ -570,9 +566,9 @@ cfgdir_init(struct cfg *cfg) if (!scfg_valid_filename(dent->d_name)) continue; - server = server_new(cfg, dent->d_name); + server = server_new(dent->d_name); if (server) - uring_openat(cfg, &server->task, server->name, scfg_open_cb); + uring_openat(&server->task, server->name, scfg_open_cb); } closedir(dir); diff --git a/cfgdir.h b/cfgdir.h index ff55fb9..2fe36eb 100644 --- a/cfgdir.h +++ b/cfgdir.h @@ -1,10 +1,10 @@ #ifndef foocfgdirhfoo #define foocfgdirhfoo -void cfgdir_delete(struct cfg *cfg); +void cfgdir_delete(); -void cfgdir_refdump(struct inotify_ev *iev); +void cfgdir_refdump(); -void cfgdir_init(struct cfg *cfg); +void cfgdir_init(); #endif diff --git a/config.c b/config.c index f49baad..eb980f8 100644 --- a/config.c +++ b/config.c @@ -315,14 +315,14 @@ error: /* Returns true if there's data left to parse in buf */ bool -config_parse_line(struct cfg *cfg, const char *filename, char **buf, +config_parse_line(const char *filename, char **buf, struct cfg_key_value_map *kvmap, int *rkey, const char **rkeyname, struct cfg_value *rvalue) { char *line, *tmp, *key; int i; - assert_return(cfg && buf && *buf && kvmap && rkey && rkeyname && rvalue, false); + assert_return(buf && *buf && kvmap && rkey && rkeyname && rvalue, false); eat_whitespace_and_comments(buf); line = get_line(buf); @@ -466,12 +466,11 @@ error: } bool -config_parse_header(struct cfg *cfg, const char *filename, const char *title, - char **buf) +config_parse_header(const char *filename, const char *title, char **buf) { char *line; - assert_return(cfg && !empty_str(filename) && !empty_str(title) && buf && *buf, false); + assert_return(!empty_str(filename) && !empty_str(title) && buf && *buf, false); eat_whitespace_and_comments(buf); diff --git a/config.h b/config.h index 0c28e0a..a88be90 100644 --- a/config.h +++ b/config.h @@ -48,12 +48,12 @@ struct cfg_value { }; }; -bool config_parse_line(struct cfg *cfg, const char *filename, char **buf, +bool config_parse_line(const char *filename, char **buf, struct cfg_key_value_map *kvmap, int *rkey, const char **rkeyname, struct cfg_value *rvalue); -bool config_parse_header(struct cfg *cfg, const char *filename, +bool config_parse_header(const char *filename, const char *title, char **buf); #endif diff --git a/idle.c b/idle.c index 0ced98d..15abd72 100644 --- a/idle.c +++ b/idle.c @@ -116,14 +116,14 @@ write_cmd(char **pos, const char *begin, const char *end) } static int -idle_check_handshake_complete(struct cfg *cfg, struct uring_task *task, int res) +idle_check_handshake_complete(struct uring_task *task, int res) { size_t remain; char *pos; int32_t mclen; int r; - assert_return(cfg && task, -EINVAL); + assert_return(task, -EINVAL); assert_task_alive_or(DBG_IDLE, task, return -EINTR); remain = task->tbuf->len; @@ -152,7 +152,7 @@ idle_check_handshake_complete(struct cfg *cfg, struct uring_task *task, int res) #define ONLINE_NEEDLE "\"online\"" static int -get_player_count(struct cfg *cfg, const char *pos, size_t remain) +get_player_count(const char *pos, size_t remain) { /* * Example JSON (line breaks added): @@ -166,7 +166,7 @@ get_player_count(struct cfg *cfg, const char *pos, size_t remain) char *end; unsigned count; - assert_return(cfg && pos && remain > 0, -1); + assert_return(pos && remain > 0, -1); online = memmem(pos, remain, ONLINE_NEEDLE, strlen(ONLINE_NEEDLE)); if (!online) { @@ -192,7 +192,7 @@ get_player_count(struct cfg *cfg, const char *pos, size_t remain) } static void -idle_check_handshake_reply(struct cfg *cfg, struct uring_task *task, int res) +idle_check_handshake_reply(struct uring_task *task, int res) { struct server *server = container_of(task, struct server, idle_task); int32_t mclen; @@ -202,7 +202,7 @@ idle_check_handshake_reply(struct cfg *cfg, struct uring_task *task, int res) int player_count = -1; int r; - assert_return(cfg && task); + assert_return(task); assert_task_alive(DBG_IDLE, task); debug(DBG_IDLE, "res: %i", res); @@ -257,34 +257,33 @@ idle_check_handshake_reply(struct cfg *cfg, struct uring_task *task, int res) fprintf(stderr, "%c", pos[i]); */ - player_count = get_player_count(cfg, pos, remain); + player_count = get_player_count(pos, remain); out: - uring_task_close_fd(cfg, task); - server_set_active_players(cfg, server, player_count); + uring_task_close_fd(task); + server_set_active_players(server, player_count); return; } static void -idle_check_handshake_sent(struct cfg *cfg, struct uring_task *task, int res) +idle_check_handshake_sent(struct uring_task *task, int res) { - assert_return(cfg && task); + assert_return(task); assert_task_alive(DBG_IDLE, task); debug(DBG_IDLE, "sent %i bytes", res); if (res < 0) { - uring_task_close_fd(cfg, task); + uring_task_close_fd(task); return; } - uring_tbuf_read_until(cfg, task, + uring_tbuf_read_until(task, idle_check_handshake_complete, idle_check_handshake_reply); } void -idle_check_get_player_count(struct cfg *cfg, struct server *server, - struct connection *conn) +idle_check_get_player_count(struct server *server, struct connection *conn) { char buf[1024]; char *pos; @@ -292,7 +291,7 @@ idle_check_get_player_count(struct cfg *cfg, struct server *server, uint16_t port; char hostname[INET6_ADDRSTRLEN]; - assert_return(cfg && server && conn && server->idle_task.priv); + assert_return(server && conn && server->idle_task.priv); port = saddr_port(&conn->remote); saddr_addr(&conn->remote, hostname, sizeof(hostname)); @@ -313,16 +312,16 @@ idle_check_get_player_count(struct cfg *cfg, struct server *server, server->idle_buf.len = (cmdbuf - server->idle_buf.buf); debug(DBG_IDLE, "sending MC message (%zu bytes)", server->idle_buf.len); - uring_tbuf_write(cfg, &server->idle_task, idle_check_handshake_sent); + uring_tbuf_write(&server->idle_task, idle_check_handshake_sent); } static void -idle_cb(struct cfg *cfg, struct uring_task *task, int res) +idle_cb(struct uring_task *task, int res) { struct idle *idle = container_of(task, struct idle, task); struct server *server; - assert_return(cfg && task); + assert_return(task); assert_task_alive(DBG_IDLE, task); if (res != sizeof(idle->value)) { @@ -333,9 +332,9 @@ idle_cb(struct cfg *cfg, struct uring_task *task, int res) debug(DBG_IDLE, "timer fired (value: %" PRIu64 ")", idle->value); list_for_each_entry(server, &cfg->servers, list) - server_idle_check(cfg, server); + server_idle_check(server); - uring_read(cfg, &idle->task, &idle->value, sizeof(idle->value), idle_cb); + uring_read(&idle->task, &idle->value, sizeof(idle->value), idle_cb); } static void @@ -349,26 +348,25 @@ idle_free(struct uring_task *task) } void -idle_refdump(struct idle *idle) +idle_refdump() { - assert_return_silent(idle); + assert_return_silent(cfg->idle); - uring_task_refdump(&idle->task); + uring_task_refdump(&cfg->idle->task); } void -idle_delete(struct cfg *cfg) +idle_delete() { - assert_return(cfg); - assert_return_silent(cfg->idle); + assert_return(cfg->idle); debug(DBG_IDLE, "closing fd %i", cfg->idle->task.fd); - uring_task_destroy(cfg, &cfg->idle->task); + uring_task_destroy(&cfg->idle->task); cfg->idle = NULL; } void -idle_init(struct cfg *cfg) +idle_init() { struct idle *idle; int ifd; @@ -384,7 +382,7 @@ idle_init(struct cfg *cfg) } }; - assert_return(cfg); + assert_return(!cfg->idle); idle = zmalloc(sizeof(*idle)); if (!idle) @@ -397,10 +395,10 @@ idle_init(struct cfg *cfg) if (timerfd_settime(ifd, 0, &tspec, NULL) != 0) die("timerfd_settime: %m"); - uring_task_init(cfg, &idle->task, "idle", uring_parent(cfg), idle_free); + uring_task_init(&idle->task, "idle", uring_parent(), idle_free); uring_task_set_fd(&idle->task, ifd); cfg->idle = idle; - uring_read(cfg, &idle->task, &idle->value, sizeof(idle->value), idle_cb); + uring_read(&idle->task, &idle->value, sizeof(idle->value), idle_cb); } diff --git a/idle.h b/idle.h index a0bf5c2..d7e4ab0 100644 --- a/idle.h +++ b/idle.h @@ -1,13 +1,13 @@ #ifndef fooidlehfoo #define fooidlehfoo -void idle_check_get_player_count(struct cfg *cfg, struct server *server, +void idle_check_get_player_count(struct server *server, struct connection *conn); -void idle_refdump(struct idle *idle); +void idle_refdump(); -void idle_delete(struct cfg *cfg); +void idle_delete(); -void idle_init(struct cfg *cfg); +void idle_init(); #endif diff --git a/igmp.c b/igmp.c index 01a4dc4..cebab40 100644 --- a/igmp.c +++ b/igmp.c @@ -183,7 +183,7 @@ igmp_match() } static void -igmp_parse(struct cfg *cfg, struct igmp *igmp) +igmp_parse(struct igmp *igmp) { char *buf; size_t len; @@ -191,7 +191,7 @@ igmp_parse(struct cfg *cfg, struct igmp *igmp) size_t body_len; union igmp_msg *igmp_msg; - assert_return(cfg && igmp); + assert_return(igmp); buf = igmp->task.tbuf->buf; len = igmp->task.tbuf->len; @@ -396,11 +396,11 @@ igmp_parse(struct cfg *cfg, struct igmp *igmp) } static void -igmp_read_cb(struct cfg *cfg, struct uring_task *task, int res) +igmp_read_cb(struct uring_task *task, int res) { struct igmp *igmp = container_of(task, struct igmp, task); - assert_return(cfg && task); + assert_return(task); assert_task_alive(DBG_IGMP, task); debug(DBG_IGMP, "task %p, igmp %p, res %i", task, igmp, res); @@ -413,11 +413,11 @@ igmp_read_cb(struct cfg *cfg, struct uring_task *task, int res) if (task->saddr.storage.ss_family == AF_PACKET || task->saddr.ll.sll_protocol == htons(ETH_P_IP)) - igmp_parse(cfg, igmp); + igmp_parse(igmp); else debug(DBG_IGMP, "invalid packet type received"); - uring_tbuf_read(cfg, &igmp->task, igmp_read_cb); + uring_tbuf_read(&igmp->task, igmp_read_cb); } static void @@ -431,26 +431,25 @@ igmp_free(struct uring_task *task) } void -igmp_refdump(struct igmp *igmp) +igmp_refdump() { - assert_return_silent(igmp); + assert_return_silent(cfg->igmp); - uring_task_refdump(&igmp->task); + uring_task_refdump(&cfg->igmp->task); } void -igmp_delete(struct cfg *cfg) +igmp_delete() { - assert_return(cfg); assert_return_silent(cfg->igmp); debug(DBG_IGMP, "closing fd %i", cfg->igmp->task.fd); - uring_task_destroy(cfg, &cfg->igmp->task); + uring_task_destroy(&cfg->igmp->task); cfg->igmp = NULL; } void -igmp_init(struct cfg *cfg) +igmp_init() { static const struct sock_filter filter[] = { BPF_STMT(BPF_LD + BPF_W + BPF_LEN, 0), /* A <- packet length */ @@ -506,13 +505,13 @@ igmp_init(struct cfg *cfg) int sfd; int opt; - assert_return(cfg); - if (!cfg->do_igmp) { debug(DBG_IGMP, "igmp snooping disabled"); return; } + assert_return(!cfg->igmp); + igmp = zmalloc(sizeof(*igmp)); if (!igmp) return; @@ -546,15 +545,11 @@ igmp_init(struct cfg *cfg) r = snprintf(ifreq.ifr_name, sizeof(ifreq.ifr_name), "%s", cfg->igmp_iface); - if (r < 0 || r >= sizeof(ifreq.ifr_name)) { - error("invalid interface name"); - goto error; - } + if (r < 0 || r >= sizeof(ifreq.ifr_name)) + die("invalid interface name: %s", cfg->igmp_iface); - if (ioctl(sfd, SIOCGIFINDEX, &ifreq) < 0) { - error("ioctl: %m"); - goto error; - } + if (ioctl(sfd, SIOCGIFINDEX, &ifreq) < 0) + die("ioctl: %m"); debug(DBG_IGMP, "using interface %s (%i)", cfg->igmp_iface, ifreq.ifr_ifindex); @@ -578,11 +573,11 @@ igmp_init(struct cfg *cfg) } debug(DBG_IGMP, "init successful, using fd %i", sfd); - uring_task_init(cfg, &igmp->task, "igmp", uring_parent(cfg), igmp_free); + uring_task_init(&igmp->task, "igmp", uring_parent(), igmp_free); uring_task_set_fd(&igmp->task, sfd); uring_task_set_buf(&igmp->task, &igmp->tbuf); igmp->task.saddr.addrlen = sizeof(igmp->task.saddr.ll); - uring_tbuf_recvmsg(cfg, &igmp->task, igmp_read_cb); + uring_tbuf_recvmsg(&igmp->task, igmp_read_cb); cfg->igmp = igmp; diff --git a/igmp.h b/igmp.h index df370c0..80875b0 100644 --- a/igmp.h +++ b/igmp.h @@ -1,10 +1,10 @@ #ifndef fooigmphfoo #define fooigmphfoo -void igmp_refdump(struct igmp *igmp); +void igmp_refdump(); -void igmp_delete(struct cfg *cfg); +void igmp_delete(); -void igmp_init(struct cfg *cfg); +void igmp_init(); #endif diff --git a/main.c b/main.c index d940771..b4d8449 100644 --- a/main.c +++ b/main.c @@ -31,6 +31,7 @@ #define DEFAULT_MAIN_CONFIG_FILE_PATH "./mcproxy.conf" /* Global, not in struct cfg since they're too low-level */ +struct cfg *cfg = NULL; bool exiting = false; unsigned debug_mask = DBG_ERROR | DBG_INFO; @@ -38,7 +39,6 @@ unsigned debug_mask = DBG_ERROR | DBG_INFO; static bool daemonize = false; static FILE *log_file = NULL; static const char *log_file_path = NULL; -static struct cfg *cfghack = NULL; #define ANSI_RED "\x1B[0;31m" #define ANSI_GREEN "\x1B[0;32m" @@ -177,9 +177,9 @@ __die(const char *fmt, ...) static void cfg_free(struct uring_task *task) { - struct cfg *cfg = container_of(task, struct cfg, task); + struct cfg *xcfg = container_of(task, struct cfg, task); - assert_return(task); + assert_return(task && xcfg == cfg); debug(DBG_SIG, "called"); systemd_delete(cfg); @@ -214,7 +214,7 @@ struct cfg_key_value_map mcfg_key_map[] = { }; static void -cfg_read(struct cfg *cfg) +cfg_read() { FILE *cfgfile; const char *path; @@ -261,7 +261,7 @@ cfg_read(struct cfg *cfg) *pos = '\0'; pos = buf; - if (!config_parse_header(cfg, path, "mcproxy", &pos)) + if (!config_parse_header(path, "mcproxy", &pos)) die("main config file (%s) invalid", path); while (true) { @@ -269,7 +269,7 @@ cfg_read(struct cfg *cfg) const char *keyname; struct cfg_value value; - if (!config_parse_line(cfg, path, &pos, mcfg_key_map, + if (!config_parse_line(path, &pos, mcfg_key_map, &key, &keyname, &value)) break; @@ -370,10 +370,9 @@ usage(int argc, char **argv, bool invalid) exit(invalid ? EXIT_FAILURE : EXIT_SUCCESS); } -static struct cfg * +static void cfg_init(int argc, char **argv) { - struct cfg *cfg; int c; unsigned i; @@ -385,7 +384,7 @@ cfg_init(int argc, char **argv) cfg->uid = geteuid(); cfg->gid = getegid(); - uring_task_init(cfg, &cfg->task, "main", NULL, cfg_free); + uring_task_init(&cfg->task, "main", NULL, cfg_free); list_init(&cfg->servers); while (true) { @@ -482,16 +481,11 @@ cfg_init(int argc, char **argv) if (!cfg->homedir) cfg->homedir = DEFAULT_HOMEDIR_PATH; - - return cfg; } static void -cfg_apply(struct cfg *cfg) +cfg_apply() { - if (!cfg) - die("invalid arguments"); - if (cfg->uid == 0 || cfg->gid == 0) /* This catches both -u root and running as root without -u */ die("Execution as root is not supported (use -u )"); @@ -544,7 +538,6 @@ cfg_apply(struct cfg *cfg) } struct signalfd_ev { - struct cfg *cfg; struct uring_task task; struct uring_task_buf tbuf; int pipe[2]; @@ -559,17 +552,15 @@ signalfd_free(struct uring_task *task) debug(DBG_SIG, "called"); close(sev->pipe[PIPE_WR]); - sev->cfg->sev = NULL; + cfg->sev = NULL; xfree(sev); } static void -dump_tree(struct cfg *cfg) +dump_tree() { struct server *server; - assert_return(cfg); - if (!debug_enabled(DBG_REF)) return; @@ -592,14 +583,14 @@ dump_tree(struct cfg *cfg) } static void -signalfd_read(struct cfg *cfg, struct uring_task *task, int res) +signalfd_read(struct uring_task *task, int res) { struct signalfd_ev *sev = container_of(task, struct signalfd_ev, task); struct server *server, *stmp; static unsigned count = 0; siginfo_t *si; - assert_return(cfg && task); + assert_return(task); assert_task_alive(DBG_SIG, task); si = (siginfo_t *)task->tbuf->buf; @@ -630,7 +621,7 @@ signalfd_read(struct cfg *cfg, struct uring_task *task, int res) case SIGUSR2: debug(DBG_SIG, "got a SIGUSR2"); - dump_tree(cfg); + dump_tree(); break; case SIGTERM: @@ -644,21 +635,21 @@ signalfd_read(struct cfg *cfg, struct uring_task *task, int res) case SIGHUP: count++; if (count > 5) { - dump_tree(cfghack); + dump_tree(); exit(EXIT_FAILURE); } verbose("got a signal to dump tree"); sd_notifyf(0, "STOPPING=1\nSTATUS=Received signal, exiting"); - dump_tree(cfg); - uring_task_put(cfg, &sev->task); - igmp_delete(cfg); - announce_delete(cfg); - idle_delete(cfg); - cfgdir_delete(cfg); + dump_tree(); + uring_task_put(&sev->task); + igmp_delete(); + announce_delete(); + idle_delete(); + cfgdir_delete(); list_for_each_entry_safe(server, stmp, &cfg->servers, list) - server_delete(cfg, server); - uring_delete(cfg); + server_delete(server); + uring_delete(); return; default: @@ -667,7 +658,7 @@ signalfd_read(struct cfg *cfg, struct uring_task *task, int res) } out: - uring_tbuf_read(cfg, &sev->task, signalfd_read); + uring_tbuf_read(&sev->task, signalfd_read); } static void @@ -677,20 +668,18 @@ hack_signal_handler(int signum, siginfo_t *si, void *ucontext) assert_return(signum > 0 && si); - r = write(cfghack->sev->pipe[PIPE_WR], si, sizeof(*si)); + r = write(cfg->sev->pipe[PIPE_WR], si, sizeof(*si)); if (r != sizeof(*si)) error("write: %zi\n", r); } static void -signalfd_init(struct cfg *cfg) +signalfd_init() { //sigset_t mask; struct signalfd_ev *sev; - assert_return(cfg); - sev = zmalloc(sizeof(*sev)); if (!sev) die("malloc: %m"); @@ -725,31 +714,28 @@ signalfd_init(struct cfg *cfg) debug(DBG_SIG, "using pipe fds %i -> %i", sev->pipe[PIPE_WR], sev->pipe[PIPE_RD]); - uring_task_init(cfg, &sev->task, "signal", uring_parent(cfg), signalfd_free); + uring_task_init(&sev->task, "signal", uring_parent(), signalfd_free); uring_task_set_fd(&sev->task, sev->pipe[PIPE_RD]); uring_task_set_buf(&sev->task, &sev->tbuf); cfg->sev = sev; - sev->cfg = cfg; - uring_tbuf_read(cfg, &sev->task, signalfd_read); + uring_tbuf_read(&sev->task, signalfd_read); } int main(int argc, char **argv) { - struct cfg *cfg; struct server *server; unsigned server_count; - cfg = cfg_init(argc, argv); - cfghack = cfg; + cfg_init(argc, argv); - cfg_apply(cfg); + cfg_apply(); - cfg_read(cfg); + cfg_read(); - uring_init(cfg); + uring_init(); - igmp_init(cfg); + igmp_init(); /* Drop CAP_NET_RAW (if we have it), only used for igmp */ capng_clear(CAPNG_SELECT_BOTH); @@ -765,17 +751,17 @@ main(int argc, char **argv) die("capng_apply failed"); } - signalfd_init(cfg); + signalfd_init(); - cfgdir_init(cfg); + cfgdir_init(); - announce_init(cfg); + announce_init(); - announce_start(cfg->aev); + announce_start(); - idle_init(cfg); + idle_init(); - uring_task_put(cfg, &cfg->task); + uring_task_put(&cfg->task); server_count = 0; list_for_each_entry(server, &cfg->servers, list) @@ -790,11 +776,12 @@ main(int argc, char **argv) info("mcproxy started, %u server configurations loaded", server_count); - uring_event_loop(cfg); + uring_event_loop(); verbose("Exiting"); xfree(cfg); + cfg = NULL; if (debug_enabled(DBG_MALLOC)) debug_resource_usage(); diff --git a/main.h b/main.h index e418f38..488ae8d 100644 --- a/main.h +++ b/main.h @@ -8,8 +8,8 @@ struct cfg; #include "utils.h" +extern struct cfg *cfg; extern bool exiting; - extern unsigned debug_mask; enum debug_lvl { @@ -103,8 +103,8 @@ do { \ struct uring_task; /* To save typing in all the function definitions below */ -typedef void (*utask_cb_t)(struct cfg *, struct uring_task *, int res); -typedef int (*rutask_cb_t)(struct cfg *, struct uring_task *, int res); +typedef void (*utask_cb_t)(struct uring_task *, int res); +typedef int (*rutask_cb_t)(struct uring_task *, int res); struct uring_task_buf { char buf[4096]; diff --git a/proxy.c b/proxy.c index 05117fd..28a207d 100644 --- a/proxy.c +++ b/proxy.c @@ -118,113 +118,113 @@ proxy_server_free(struct uring_task *task) } void -proxy_delete(struct cfg *cfg, struct server_proxy *proxy) +proxy_delete(struct server_proxy *proxy) { debug(DBG_PROXY, "%s: shutting down proxy %p", proxy->server->name, proxy); - assert_return(cfg && proxy); + assert_return(proxy); - uring_task_destroy(cfg, &proxy->servertask); - uring_task_destroy(cfg, &proxy->clienttask); - uring_task_destroy(cfg, &proxy->task); + uring_task_destroy(&proxy->servertask); + uring_task_destroy(&proxy->clienttask); + uring_task_destroy(&proxy->task); } -static void proxy_client_data_in(struct cfg *cfg, struct uring_task *task, int res); +static void proxy_client_data_in(struct uring_task *task, int res); static void -proxy_client_data_out(struct cfg *cfg, struct uring_task *task, int res) +proxy_client_data_out(struct uring_task *task, int res) { struct server_proxy *proxy = container_of(task, struct server_proxy, clienttask); - assert_return(cfg && task); + assert_return(task); assert_task_alive(DBG_PROXY, task); if (res <= 0) { debug(DBG_PROXY, "%s: result was %i", proxy->server->name, res); - uring_task_close_fd(cfg, task); - proxy_delete(cfg, proxy); + uring_task_close_fd(task); + proxy_delete(proxy); return; } proxy->client_bytes += res; uring_task_set_fd(&proxy->clienttask, proxy->cfd); - uring_tbuf_read(cfg, task, proxy_client_data_in); + uring_tbuf_read(task, proxy_client_data_in); } static void -proxy_client_data_in(struct cfg *cfg, struct uring_task *task, int res) +proxy_client_data_in(struct uring_task *task, int res) { struct server_proxy *proxy = container_of(task, struct server_proxy, clienttask); - assert_return(cfg && task); + assert_return(task); assert_task_alive(DBG_PROXY, task); if (res <= 0) { debug(DBG_PROXY, "%s: result was %i", proxy->server->name, res); - uring_task_close_fd(cfg, task); - proxy_delete(cfg, proxy); + uring_task_close_fd(task); + proxy_delete(proxy); return; } uring_task_set_fd(&proxy->clienttask, proxy->sfd); - uring_tbuf_write(cfg, task, proxy_client_data_out); + uring_tbuf_write(task, proxy_client_data_out); } -static void proxy_server_data_in(struct cfg *cfg, struct uring_task *task, +static void proxy_server_data_in(struct uring_task *task, int res); static void -proxy_server_data_out(struct cfg *cfg, struct uring_task *task, int res) +proxy_server_data_out(struct uring_task *task, int res) { struct server_proxy *proxy = container_of(task, struct server_proxy, servertask); - assert_return(cfg && task); + assert_return(task); assert_task_alive(DBG_PROXY, task); if (res <= 0) { debug(DBG_PROXY, "%s: result was %i", proxy->server->name, res); - uring_task_close_fd(cfg, task); - proxy_delete(cfg, proxy); + uring_task_close_fd(task); + proxy_delete(proxy); return; } proxy->server_bytes += res; uring_task_set_fd(&proxy->servertask, proxy->sfd); - uring_tbuf_read(cfg, &proxy->servertask, proxy_server_data_in); + uring_tbuf_read(&proxy->servertask, proxy_server_data_in); } static void -proxy_server_data_in(struct cfg *cfg, struct uring_task *task, int res) +proxy_server_data_in(struct uring_task *task, int res) { struct server_proxy *proxy = container_of(task, struct server_proxy, servertask); - assert_return(cfg && task); + assert_return(task); assert_task_alive(DBG_PROXY, task); if (res <= 0) { debug(DBG_PROXY, "%s: result was %i", proxy->server->name, res); - uring_task_close_fd(cfg, task); - proxy_delete(cfg, proxy); + uring_task_close_fd(task); + proxy_delete(proxy); return; } uring_task_set_fd(&proxy->servertask, proxy->cfd); - uring_tbuf_write(cfg, task, proxy_server_data_out); + uring_tbuf_write(task, proxy_server_data_out); } static void -proxy_connected_cb(struct cfg *cfg, struct connection *conn, bool connected) +proxy_connected_cb(struct connection *conn, bool connected) { struct server_proxy *proxy = container_of(conn, struct server_proxy, server_conn); - assert_return(cfg && conn); + assert_return(conn); assert_task_alive(DBG_PROXY, &proxy->clienttask); assert_task_alive(DBG_PROXY, &proxy->servertask); if (!connected) { error("%s: proxy connection to remote server failed", proxy->server->name); - proxy_delete(cfg, proxy); + proxy_delete(proxy); return; } @@ -235,16 +235,16 @@ proxy_connected_cb(struct cfg *cfg, struct connection *conn, bool connected) proxy->server_conn.remote.addrstr); proxy->begin = time(NULL); - uring_tbuf_read(cfg, &proxy->clienttask, proxy_client_data_in); - uring_tbuf_read(cfg, &proxy->servertask, proxy_server_data_in); + uring_tbuf_read(&proxy->clienttask, proxy_client_data_in); + uring_tbuf_read(&proxy->servertask, proxy_server_data_in); } struct server_proxy * -proxy_new(struct cfg *cfg, struct server *server, struct saddr *client, int fd) +proxy_new(struct server *server, struct saddr *client, int fd) { struct server_proxy *proxy; - assert_return(cfg && server && client && fd > 0, NULL); + assert_return(server && client && fd > 0, NULL); proxy = zmalloc(sizeof(*proxy)); if (!proxy) { @@ -255,22 +255,22 @@ proxy_new(struct cfg *cfg, struct server *server, struct saddr *client, int fd) proxy->sfd = -1; proxy->cfd = fd; proxy->server = server; - uring_task_init(cfg, &proxy->task, "proxy", &server->task, proxy_free); + uring_task_init(&proxy->task, "proxy", &server->task, proxy_free); - connection_set_local(cfg, &proxy->client_conn, fd); - connection_set_remote(cfg, &proxy->client_conn, client); + connection_set_local(&proxy->client_conn, fd); + connection_set_remote(&proxy->client_conn, client); - uring_task_init(cfg, &proxy->clienttask, "proxy_client", &proxy->task, + uring_task_init(&proxy->clienttask, "proxy_client", &proxy->task, proxy_client_free); uring_task_set_buf(&proxy->clienttask, &proxy->clientbuf); uring_task_set_fd(&proxy->clienttask, fd); - uring_task_init(cfg, &proxy->servertask, "proxy_server", &proxy->task, + uring_task_init(&proxy->servertask, "proxy_server", &proxy->task, proxy_server_free); uring_task_set_buf(&proxy->servertask, &proxy->serverbuf); list_add(&proxy->list, &server->proxys); - connect_any(cfg, &proxy->servertask, &server->remotes, + connect_any(&proxy->servertask, &server->remotes, &proxy->server_conn, proxy_connected_cb); return proxy; diff --git a/proxy.h b/proxy.h index d0533e0..0726351 100644 --- a/proxy.h +++ b/proxy.h @@ -23,8 +23,9 @@ struct server_proxy { void proxy_refdump(struct server_proxy *proxy); -void proxy_delete(struct cfg *cfg, struct server_proxy *proxy); +void proxy_delete(struct server_proxy *proxy); -struct server_proxy *proxy_new(struct cfg *cfg, struct server *server, struct saddr *client, int fd); +struct server_proxy *proxy_new(struct server *server, struct saddr *client, + int fd); #endif diff --git a/rcon.c b/rcon.c index dc29364..0588da0 100644 --- a/rcon.c +++ b/rcon.c @@ -22,36 +22,6 @@ struct rcon { struct uring_task_buf tbuf; }; -static void -rcon_free(struct uring_task *task) -{ - struct rcon *rcon = container_of(task, struct rcon, task); - - assert_return(task); - - debug(DBG_RCON, "task %p, idle %p", task, rcon); - rcon->server->rcon = NULL; - xfree(rcon); -} - -void -rcon_refdump(struct rcon *rcon) -{ - assert_return_silent(rcon); - - uring_task_refdump(&rcon->task); -} - -void -rcon_delete(struct cfg *cfg, struct server *server) -{ - assert_return_silent(server->rcon); - - debug(DBG_RCON, "closing fd %i", server->rcon->task.fd); - uring_task_destroy(cfg, &server->rcon->task); - server->rcon = NULL; -} - static int32_t read_int(char **pos, size_t *len) { @@ -134,12 +104,12 @@ enum rcon_packet_type { }; static void -create_packet(struct cfg *cfg, struct rcon *rcon, int32_t reqid, +create_packet(struct rcon *rcon, int32_t reqid, enum rcon_packet_type type, const char *msg) { char *pos; - assert_return(cfg && rcon && !empty_str(msg)); + assert_return(rcon && !empty_str(msg)); /* Body */ pos = &rcon->tbuf.buf[4]; @@ -159,13 +129,13 @@ create_packet(struct cfg *cfg, struct rcon *rcon, int32_t reqid, } static int -packet_complete(struct cfg *cfg, struct uring_task *task, int res) +packet_complete(struct uring_task *task, int res) { char *pos; size_t len; int32_t plen; - assert_return(cfg && task, 0); + assert_return(task, 0); assert_task_alive_or(DBG_RCON, task, return -EINTR); pos = task->tbuf->buf; @@ -184,14 +154,14 @@ packet_complete(struct cfg *cfg, struct uring_task *task, int res) } static bool -rcon_read_packet(struct cfg *cfg, struct rcon *rcon, int32_t *id, +rcon_read_packet(struct rcon *rcon, int32_t *id, int32_t *type, char **rmsg) { char *pos; size_t len; int32_t plen; - assert_return(cfg && rcon && id && type && rmsg, false); + assert_return(rcon && id && type && rmsg, false); pos = rcon->tbuf.buf; len = rcon->tbuf.len; @@ -229,14 +199,14 @@ rcon_read_packet(struct cfg *cfg, struct rcon *rcon, int32_t *id, } static void -rcon_stop_reply(struct cfg *cfg, struct uring_task *task, int res) +rcon_stop_reply(struct uring_task *task, int res) { struct rcon *rcon = container_of(task, struct rcon, task); int32_t id; int32_t type; char *msg; - assert_return(cfg && task); + assert_return(task); assert_task_alive(DBG_RCON, task); if (res < 0) { @@ -245,7 +215,7 @@ rcon_stop_reply(struct cfg *cfg, struct uring_task *task, int res) } debug(DBG_RCON, "packet complete"); - rcon_read_packet(cfg, rcon, &id, &type, &msg); + rcon_read_packet(rcon, &id, &type, &msg); if (id != 2) { error("rcon stop failed - reply id (%" PRIi32 ")", id); @@ -258,36 +228,36 @@ rcon_stop_reply(struct cfg *cfg, struct uring_task *task, int res) verbose("rcon stop successful (%s)", msg); out: - uring_task_put(cfg, &rcon->task); + uring_task_put(&rcon->task); } static void -rcon_stop_sent(struct cfg *cfg, struct uring_task *task, int res) +rcon_stop_sent(struct uring_task *task, int res) { struct rcon *rcon = container_of(task, struct rcon, task); - assert_return(cfg && task); + assert_return(task); assert_task_alive(DBG_RCON, task); if (res < 0) { debug(DBG_RCON, "res: %i", res); - uring_task_put(cfg, &rcon->task); + uring_task_put(&rcon->task); return; } debug(DBG_RCON, "stop cmd sent"); - uring_tbuf_read_until(cfg, &rcon->task, packet_complete, rcon_stop_reply); + uring_tbuf_read_until(&rcon->task, packet_complete, rcon_stop_reply); } static void -rcon_login_reply(struct cfg *cfg, struct uring_task *task, int res) +rcon_login_reply(struct uring_task *task, int res) { struct rcon *rcon = container_of(task, struct rcon, task); int32_t id; int32_t type; char *msg; - assert_return(cfg && task); + assert_return(task); assert_task_alive(DBG_RCON, task); if (res < 0) { @@ -296,7 +266,7 @@ rcon_login_reply(struct cfg *cfg, struct uring_task *task, int res) } debug(DBG_RCON, "packet complete"); - rcon_read_packet(cfg, rcon, &id, &type, &msg); + rcon_read_packet(rcon, &id, &type, &msg); if (id != 1) { error("rcon login failed - unexpected reply id (%" PRIi32 ")", id); @@ -310,66 +280,95 @@ rcon_login_reply(struct cfg *cfg, struct uring_task *task, int res) } debug(DBG_RCON, "rcon login successful"); - create_packet(cfg, rcon, 2, RCON_PACKET_COMMAND, "stop"); - uring_tbuf_write(cfg, &rcon->task, rcon_stop_sent); + create_packet(rcon, 2, RCON_PACKET_COMMAND, "stop"); + uring_tbuf_write(&rcon->task, rcon_stop_sent); return; error: - uring_task_put(cfg, &rcon->task); + uring_task_put(&rcon->task); } static void -rcon_login_sent(struct cfg *cfg, struct uring_task *task, int res) +rcon_login_sent(struct uring_task *task, int res) { struct rcon *rcon = container_of(task, struct rcon, task); - assert_return(cfg && task); + assert_return(task); assert_task_alive(DBG_RCON, task); if (res < 0) { debug(DBG_RCON, "res: %i", res); - uring_task_put(cfg, &rcon->task); + uring_task_put(&rcon->task); return; } debug(DBG_RCON, "login sent"); - uring_tbuf_read_until(cfg, &rcon->task, packet_complete, rcon_login_reply); + uring_tbuf_read_until(&rcon->task, packet_complete, rcon_login_reply); } static void -rcon_connected_cb(struct cfg *cfg, struct connection *conn, bool connected) +rcon_connected_cb(struct connection *conn, bool connected) { struct rcon *rcon = container_of(conn, struct rcon, conn); - assert_return(cfg && conn); + assert_return(conn); assert_task_alive(DBG_RCON, &rcon->task); if (!connected) { error("rcon connection to remote server (%s) failed", rcon->server->name); - uring_task_put(cfg, &rcon->task); + uring_task_put(&rcon->task); return; } - create_packet(cfg, rcon, 1, RCON_PACKET_LOGIN, rcon->server->rcon_password); - uring_tbuf_write(cfg, &rcon->task, rcon_login_sent); + create_packet(rcon, 1, RCON_PACKET_LOGIN, rcon->server->rcon_password); + uring_tbuf_write(&rcon->task, rcon_login_sent); +} + +static void +rcon_free(struct uring_task *task) +{ + struct rcon *rcon = container_of(task, struct rcon, task); + + assert_return(task); + + debug(DBG_RCON, "task %p, idle %p", task, rcon); + xfree(rcon); +} + +void +rcon_refdump(struct rcon *rcon) +{ + assert_return_silent(rcon); + + uring_task_refdump(&rcon->task); +} + +void +rcon_delete(struct server *server) +{ + assert_return_silent(server->rcon); + + debug(DBG_RCON, "closing fd %i", server->rcon->task.fd); + uring_task_destroy(&server->rcon->task); + server->rcon = NULL; } void -rcon_init(struct cfg *cfg, struct server *server) +rcon_init(struct server *server) { struct rcon *rcon; - assert_return(cfg && server && !list_empty(&server->rcons) && !empty_str(server->rcon_password)); + assert_return(server && !server->rcon && !list_empty(&server->rcons) && !empty_str(server->rcon_password)); rcon = zmalloc(sizeof(*rcon)); if (!rcon) die("malloc: %m"); - uring_task_init(cfg, &rcon->task, "rcon", &server->task, rcon_free); + uring_task_init(&rcon->task, "rcon", &server->task, rcon_free); uring_task_set_buf(&rcon->task, &rcon->tbuf); rcon->server = server; server->rcon = rcon; - connect_any(cfg, &rcon->task, &server->rcons, &rcon->conn, rcon_connected_cb); + connect_any(&rcon->task, &server->rcons, &rcon->conn, rcon_connected_cb); } diff --git a/rcon.h b/rcon.h index 8b410e1..6604304 100644 --- a/rcon.h +++ b/rcon.h @@ -3,8 +3,8 @@ void rcon_refdump(struct rcon *rcon); -void rcon_delete(struct cfg *cfg, struct server *server); +void rcon_delete(struct server *server); -void rcon_init(struct cfg *cfg, struct server *server); +void rcon_init(struct server *server); #endif diff --git a/server.c b/server.c index 1151cc4..ddd4ad4 100644 --- a/server.c +++ b/server.c @@ -32,9 +32,9 @@ struct server_local { }; static bool -set_property(struct cfg *cfg, struct server *server, char **property, const char *value) +set_property(struct server *server, char **property, const char *value) { - assert_return(cfg && server && !*property && !empty_str(value), false); + assert_return(server && !*property && !empty_str(value), false); *property = xstrdup(value); if (!*property) { @@ -84,7 +84,7 @@ server_free(struct uring_task *task) } void -server_delete(struct cfg *cfg, struct server *server) +server_delete(struct server *server) { struct server_local *local, *ltmp; struct server_proxy *proxy, *ptmp; @@ -93,18 +93,18 @@ server_delete(struct cfg *cfg, struct server *server) struct saddr *tmp; struct dns_async *dns, *dtmp; - assert_return(cfg && server); + assert_return(server); verbose("Removing server %s", server->name); server->state = SERVER_STATE_DEAD; - rcon_delete(cfg, server); + rcon_delete(server); list_for_each_entry_safe(local, ltmp, &server->locals, list) - uring_task_destroy(cfg, &local->task); + uring_task_destroy(&local->task); list_for_each_entry_safe(proxy, ptmp, &server->proxys, list) - proxy_delete(cfg, proxy); + proxy_delete(proxy); list_for_each_entry_safe(rcon, tmp, &server->rcons, list) { list_del(&rcon->list); @@ -119,23 +119,23 @@ server_delete(struct cfg *cfg, struct server *server) list_for_each_entry_safe(dns, dtmp, &server->dnslookups, list) gai_cancel(&dns->gcb); - uring_task_destroy(cfg, &server->idle_task); - uring_poll_cancel(cfg, &server->exec_task); - uring_task_put(cfg, &server->exec_task); - uring_task_destroy(cfg, &server->task); - uring_task_put(cfg, &server->ann_task); + uring_task_destroy(&server->idle_task); + uring_poll_cancel(&server->exec_task); + uring_task_put(&server->exec_task); + uring_task_destroy(&server->task); + uring_task_put(&server->ann_task); } void -server_delete_by_name(struct cfg *cfg, const char *name) +server_delete_by_name(const char *name) { struct server *server; - assert_return(cfg && !empty_str(name)); + assert_return(!empty_str(name)); list_for_each_entry(server, &cfg->servers, list) { if (streq(server->name, name)) { - server_delete(cfg, server); + server_delete(server); return; } } @@ -199,13 +199,13 @@ server_local_free(struct uring_task *task) } static void -server_local_accept(struct cfg *cfg, struct uring_task *task, int res) +server_local_accept(struct uring_task *task, int res) { struct server_local *local = container_of(task, struct server_local, task); struct server *server = container_of(task->parent, struct server, task); struct server_proxy *proxy; - assert_return(cfg && task); + assert_return(task); assert_task_alive(DBG_SRV, task); debug(DBG_SRV, "task %p, res %i, server %s", task, res, server->name); @@ -223,26 +223,26 @@ server_local_accept(struct cfg *cfg, struct uring_task *task, int res) if (list_empty(&server->remotes)) { /* This shouldn't be possible, checked before opening local */ error("server->remotes empty!"); - uring_close(cfg, &local->task, res); + uring_close(&local->task, res); goto out; } - proxy = proxy_new(cfg, server, &local->client, res); + proxy = proxy_new(server, &local->client, res); if (!proxy) - uring_close(cfg, &local->task, res); + uring_close(&local->task, res); out: - uring_accept(cfg, &local->task, &local->client, server_local_accept); + uring_accept(&local->task, &local->client, server_local_accept); } static bool -server_local_open(struct cfg *cfg, struct server *server, struct server_local *local) +server_local_open(struct server *server, struct server_local *local) { int sfd; int option; int r; - assert_return(cfg && server && local, false); + assert_return(server && local, false); sfd = socket(local->local.storage.ss_family, SOCK_STREAM | SOCK_CLOEXEC, 0); if (sfd < 0) { @@ -267,7 +267,7 @@ server_local_open(struct cfg *cfg, struct server *server, struct server_local *l if (setsockopt(sfd, IPPROTO_IP, IP_FREEBIND, &option, sizeof(option)) < 0) error("setsockopt: %m"); - socket_set_low_latency(cfg, sfd); + socket_set_low_latency(sfd); r = bind(sfd, (struct sockaddr *)&local->local.storage, local->local.addrlen); if (r < 0) { @@ -282,7 +282,7 @@ server_local_open(struct cfg *cfg, struct server *server, struct server_local *l } uring_task_set_fd(&local->task, sfd); - uring_accept(cfg, &local->task, &local->client, server_local_accept); + uring_accept(&local->task, &local->client, server_local_accept); return true; error: @@ -305,13 +305,13 @@ server_exec_free(struct uring_task *task) /* FIXME: update states */ static void -server_exec_done(struct cfg *cfg, struct uring_task *task, int res) +server_exec_done(struct uring_task *task, int res) { struct server *server = container_of(task, struct server, exec_task); int r; siginfo_t info; - assert_return(cfg && task); + assert_return(task); assert_task_alive_or(DBG_SRV, task, goto out); /* Should we leave child processes running? */ @@ -332,7 +332,7 @@ server_exec_done(struct cfg *cfg, struct uring_task *task, int res) error("command failed: %i", info.si_status); out: - uring_task_close_fd(cfg, &server->exec_task); + uring_task_close_fd(&server->exec_task); } static int @@ -351,13 +351,13 @@ server_exec_child(void *ptr) #endif static bool -server_exec(struct cfg *cfg, struct server *server, const char *cmd) +server_exec(struct server *server, const char *cmd) { char stack[4096]; /* Beautiful/horrible hack :) */ int pidfd; int r; - assert_return(cfg && server && cmd && server->exec_task.fd < 1, false); + assert_return(server && cmd && server->exec_task.fd < 1, false); r = clone(server_exec_child, stack + sizeof(stack), CLONE_VM | CLONE_VFORK | CLONE_PIDFD | SIGCHLD, @@ -368,19 +368,19 @@ server_exec(struct cfg *cfg, struct server *server, const char *cmd) } uring_task_set_fd(&server->exec_task, pidfd); - uring_poll(cfg, &server->exec_task, POLLIN, server_exec_done); + uring_poll(&server->exec_task, POLLIN, server_exec_done); return true; } static bool -server_check_running(struct cfg *cfg, struct server *server) +server_check_running(struct server *server) { - assert_return(cfg && server, false); + assert_return(server, false); /* FIXME: other methods, rcon? */ if (server->systemd_service) { verbose("%s: checking if systemd service is running", server->name); - if (systemd_service_running(cfg, server)) { + if (systemd_service_running(server)) { server->state = SERVER_STATE_RUNNING; return true; } else { @@ -393,26 +393,26 @@ server_check_running(struct cfg *cfg, struct server *server) } bool -server_start(struct cfg *cfg, struct server *server) +server_start(struct server *server) { - assert_return(cfg && server, false); + assert_return(server, false); assert_task_alive_or(DBG_SRV, &server->task, return false); switch (server->start_method) { case SERVER_START_METHOD_EXEC: verbose("Starting server %s via external cmd", server->name); - return server_exec(cfg, server, server->start_exec); + return server_exec(server, server->start_exec); case SERVER_START_METHOD_SYSTEMD: verbose("Starting server %s via systemd (%s)", server->name, server->systemd_service); - if (systemd_service_start(cfg, server)) { + if (systemd_service_start(server)) { server->state = SERVER_STATE_RUNNING; return true; } else - return server_check_running(cfg, server); + return server_check_running(server); case SERVER_START_METHOD_UNDEFINED: default: @@ -423,29 +423,29 @@ server_start(struct cfg *cfg, struct server *server) } bool -server_stop(struct cfg *cfg, struct server *server) +server_stop(struct server *server) { - assert_return(cfg && server, false); + assert_return(server, false); assert_task_alive_or(DBG_SRV, &server->task, return false); switch (server->stop_method) { case SERVER_STOP_METHOD_EXEC: verbose("Stopping server %s via external cmd", server->name); - return server_exec(cfg, server, server->stop_exec); + return server_exec(server, server->stop_exec); case SERVER_STOP_METHOD_SYSTEMD: verbose("Stopping server %s via systemd (%s)", server->name, server->systemd_service); - if (systemd_service_stop(cfg, server)) { + if (systemd_service_stop(server)) { server->state = SERVER_STATE_STOPPED; return true; } else - return server_check_running(cfg, server); + return server_check_running(server); case SERVER_STOP_METHOD_RCON: verbose("Stopping server %s via rcon", server->name); - rcon_init(cfg, server); + rcon_init(server); return true; case SERVER_STOP_METHOD_UNDEFINED: @@ -465,9 +465,9 @@ server_idle_free(struct uring_task *task) } void -server_set_active_players(struct cfg *cfg, struct server *server, int count) +server_set_active_players(struct server *server, int count) { - assert_return(cfg && server); + assert_return(server); assert_task_alive(DBG_IDLE, &server->idle_task); debug(DBG_IDLE, "%s: currently %i active players", @@ -484,16 +484,16 @@ server_set_active_players(struct cfg *cfg, struct server *server, int count) if (server->idle_count > server->idle_timeout) { verbose("stopping idle server %s", server->name); - server_stop(cfg, server); + server_stop(server); } } static void -server_idle_connected_cb(struct cfg *cfg, struct connection *conn, bool connected) +server_idle_connected_cb(struct connection *conn, bool connected) { struct server *server = container_of(conn, struct server, idle_conn); - assert_return(cfg && conn); + assert_return(conn); assert_task_alive(DBG_IDLE, &server->idle_task); if (!connected) { @@ -506,13 +506,13 @@ server_idle_connected_cb(struct cfg *cfg, struct connection *conn, bool connecte } debug(DBG_IDLE, "connected to remote %s\n", conn->remote.addrstr); - idle_check_get_player_count(cfg, server, conn); + idle_check_get_player_count(server, conn); } bool -server_idle_check(struct cfg *cfg, struct server *server) +server_idle_check(struct server *server) { - assert_return(cfg && server, false); + assert_return(server, false); if (server->state == SERVER_STATE_INIT || server->state == SERVER_STATE_DEAD) @@ -529,7 +529,7 @@ server_idle_check(struct cfg *cfg, struct server *server) return true; } - connect_any(cfg, &server->idle_task, &server->remotes, + connect_any(&server->idle_task, &server->remotes, &server->idle_conn, server_idle_connected_cb); return true; } @@ -543,11 +543,11 @@ server_announce_free(struct uring_task *task) } static void -server_announce_cb(struct cfg *cfg, struct uring_task *task, int res) +server_announce_cb(struct uring_task *task, int res) { struct server *server = container_of(task, struct server, ann_task); - assert_return(cfg && task); + assert_return(task); if (res < 0) error("failure %i", res); @@ -558,9 +558,9 @@ server_announce_cb(struct cfg *cfg, struct uring_task *task, int res) } bool -server_announce(struct cfg *cfg, struct server *server, int fd) +server_announce(struct server *server, int fd) { - assert_return(cfg && server && fd >= 0, false); + assert_return(server && fd >= 0, false); if (server->state == SERVER_STATE_INIT || server->state == SERVER_STATE_DEAD) @@ -568,18 +568,18 @@ server_announce(struct cfg *cfg, struct server *server, int fd) debug(DBG_ANN, "announcing server: %s", server->name); uring_task_set_fd(&server->ann_task, fd); - uring_tbuf_sendmsg(cfg, &server->ann_task, server_announce_cb); + uring_tbuf_sendmsg(&server->ann_task, server_announce_cb); return true; } bool -server_commit(struct cfg *cfg, struct server *server) +server_commit(struct server *server) { struct server_local *local; uint16_t port; int r; - assert_return(cfg && server && server->name, false); + assert_return(server && server->name, false); assert_task_alive_or(DBG_SRV, &server->task, return false); if (server->state != SERVER_STATE_INIT) { @@ -620,8 +620,7 @@ server_commit(struct cfg *cfg, struct server *server) } if (server->systemd_service && !server->systemd_obj) { - server->systemd_obj = systemd_service_object_path(cfg, - server->systemd_service); + server->systemd_obj = systemd_object_path(server->systemd_service); if (!server->systemd_obj) { error("%s: failed to create systemd object path (%s)", server->name, server->systemd_service); @@ -733,21 +732,21 @@ server_commit(struct cfg *cfg, struct server *server) server_dump(server); list_for_each_entry(local, &server->locals, list) { - server_local_open(cfg, server, local); + server_local_open(server, local); } server->state = SERVER_STATE_CFG_OK; - server_check_running(cfg, server); + server_check_running(server); debug(DBG_SRV, "success"); return true; } bool -server_add_remote(struct cfg *cfg, struct server *server, struct saddr *remote) +server_add_remote(struct server *server, struct saddr *remote) { - assert_return(cfg && server && remote, false); + assert_return(server && remote, false); assert_task_alive_or(DBG_SRV, &server->task, return false); debug(DBG_SRV, "adding remote: %s", remote->addrstr); @@ -756,11 +755,11 @@ server_add_remote(struct cfg *cfg, struct server *server, struct saddr *remote) } bool -server_add_local(struct cfg *cfg, struct server *server, struct saddr *saddr) +server_add_local(struct server *server, struct saddr *saddr) { struct server_local *local; - assert_return(cfg && server && saddr, false); + assert_return(server && saddr, false); assert_task_alive_or(DBG_SRV, &server->task, return false); local = zmalloc(sizeof(*local)); @@ -771,16 +770,16 @@ server_add_local(struct cfg *cfg, struct server *server, struct saddr *saddr) debug(DBG_SRV, "adding local: %s", saddr->addrstr); local->local = *saddr; - uring_task_init(cfg, &local->task, "local", &server->task, server_local_free); + uring_task_init(&local->task, "local", &server->task, server_local_free); list_add(&local->list, &server->locals); xfree(saddr); return true; } bool -server_add_rcon(struct cfg *cfg, struct server *server, struct saddr *rcon) +server_add_rcon(struct server *server, struct saddr *rcon) { - assert_return(cfg && server && rcon, false); + assert_return(server && rcon, false); assert_task_alive_or(DBG_SRV, &server->task, return false); debug(DBG_SRV, "adding rcon: %s", rcon->addrstr); @@ -789,22 +788,20 @@ server_add_rcon(struct cfg *cfg, struct server *server, struct saddr *rcon) } bool -server_set_rcon_password(struct cfg *cfg, struct server *server, - const char *password) +server_set_rcon_password(struct server *server, const char *password) { - assert_return(cfg && server && !empty_str(password), false); + assert_return(server && !empty_str(password), false); - return set_property(cfg, server, &server->rcon_password, password); + return set_property(server, &server->rcon_password, password); } bool -server_set_systemd_service(struct cfg *cfg, struct server *server, - const char *service) +server_set_systemd_service(struct server *server, const char *service) { const char *suffix; char *tmp; - assert_return(cfg && server && !empty_str(service) && !server->systemd_service, false); + assert_return(server && !empty_str(service) && !server->systemd_service, false); suffix = strrchr(service, '.'); if (!suffix || !streq(suffix, ".service")) { @@ -824,7 +821,7 @@ server_set_systemd_service(struct cfg *cfg, struct server *server, } bool -server_set_stop_method(struct cfg *cfg, struct server *server, +server_set_stop_method(struct server *server, enum server_stop_method stop_method) { assert_return(server->stop_method == SERVER_STOP_METHOD_UNDEFINED && @@ -835,7 +832,7 @@ server_set_stop_method(struct cfg *cfg, struct server *server, } bool -server_set_start_method(struct cfg *cfg, struct server *server, +server_set_start_method(struct server *server, enum server_start_method start_method) { assert_return(server->start_method == SERVER_START_METHOD_UNDEFINED && @@ -846,43 +843,43 @@ server_set_start_method(struct cfg *cfg, struct server *server, } bool -server_set_stop_exec(struct cfg *cfg, struct server *server, const char *cmd) +server_set_stop_exec(struct server *server, const char *cmd) { - assert_return(cfg && server && !empty_str(cmd), false); + assert_return(server && !empty_str(cmd), false); - return set_property(cfg, server, &server->stop_exec, cmd); + return set_property(server, &server->stop_exec, cmd); } bool -server_set_start_exec(struct cfg *cfg, struct server *server, const char *cmd) +server_set_start_exec(struct server *server, const char *cmd) { - assert_return(cfg && server && !empty_str(cmd), false); + assert_return(server && !empty_str(cmd), false); - return set_property(cfg, server, &server->start_exec, cmd); + return set_property(server, &server->start_exec, cmd); } bool -server_set_idle_timeout(struct cfg *cfg, struct server *server, uint16_t timeout) +server_set_idle_timeout(struct server *server, uint16_t timeout) { - assert_return(cfg && server && timeout > 0 && server->idle_timeout == 0, false); + assert_return(server && timeout > 0 && server->idle_timeout == 0, false); server->idle_timeout = timeout; return true; } bool -server_set_port(struct cfg *cfg, struct server *server, uint16_t port) +server_set_port(struct server *server, uint16_t port) { - assert_return(cfg && server && port > 0 && server->announce_port == 0, false); + assert_return(server && port > 0 && server->announce_port == 0, false); server->announce_port = htons(port); return true; } bool -server_set_type(struct cfg *cfg, struct server *server, enum server_type type) +server_set_type(struct server *server, enum server_type type) { - assert_return(cfg && server && type != SERVER_TYPE_UNDEFINED, false); + assert_return(server && type != SERVER_TYPE_UNDEFINED, false); switch (type) { case SERVER_TYPE_ANNOUNCE: @@ -899,19 +896,19 @@ server_set_type(struct cfg *cfg, struct server *server, enum server_type type) } bool -server_set_pretty_name(struct cfg *cfg, struct server *server, const char *pretty_name) +server_set_pretty_name(struct server *server, const char *pretty_name) { - assert_return(cfg && server && !empty_str(pretty_name), false); + assert_return(server && !empty_str(pretty_name), false); - return set_property(cfg, server, &server->pretty_name, pretty_name); + return set_property(server, &server->pretty_name, pretty_name); } struct server * -server_new(struct cfg *cfg, const char *name) +server_new(const char *name) { struct server *server; - assert_return(cfg && !empty_str(name), NULL); + assert_return(!empty_str(name), NULL); list_for_each_entry(server, &cfg->servers, list) { if (!streq(name, server->name)) @@ -928,23 +925,22 @@ server_new(struct cfg *cfg, const char *name) } server->state = SERVER_STATE_INIT; - server->cfg = cfg; server->type = SERVER_TYPE_UNDEFINED; server->name = xstrdup(name); server->stop_method = SERVER_STOP_METHOD_UNDEFINED; server->start_method = SERVER_START_METHOD_UNDEFINED; server->idle_timeout = 0; - uring_task_init(cfg, &server->task, server->name, uring_parent(cfg), server_free); + uring_task_init(&server->task, server->name, uring_parent(), server_free); uring_task_set_buf(&server->task, &server->tbuf); - uring_task_init(cfg, &server->ann_task, "announce", &server->task, server_announce_free); + uring_task_init(&server->ann_task, "announce", &server->task, server_announce_free); uring_task_set_buf(&server->ann_task, &server->ann_buf); saddr_set_ipv4(&server->ann_task.saddr, cinet_addr(224,0,2,60), htons(4445)); - uring_task_init(cfg, &server->exec_task, "exec", &server->task, server_exec_free); + uring_task_init(&server->exec_task, "exec", &server->task, server_exec_free); - uring_task_init(cfg, &server->idle_task, "idle", &server->task, server_idle_free); + uring_task_init(&server->idle_task, "idle", &server->task, server_idle_free); uring_task_set_buf(&server->idle_task, &server->idle_buf); list_init(&server->remotes); diff --git a/server.h b/server.h index b104ef1..e5913ae 100644 --- a/server.h +++ b/server.h @@ -39,7 +39,6 @@ struct server { struct list_head rcons; struct list_head dnslookups; enum server_state state; - struct cfg *cfg; enum server_stop_method stop_method; enum server_start_method start_method; @@ -77,59 +76,51 @@ struct server { void server_refdump(struct server *server); -void server_delete(struct cfg *cfg, struct server *server); +void server_delete(struct server *server); -void server_delete_by_name(struct cfg *cfg, const char *name); +void server_delete_by_name(const char *name); -bool server_start(struct cfg *cfg, struct server *server); +bool server_start(struct server *server); -bool server_stop(struct cfg *cfg, struct server *server); +bool server_stop(struct server *server); -void server_set_active_players(struct cfg *cfg, struct server *server, - int count); +void server_set_active_players(struct server *server, int count); -bool server_idle_check(struct cfg *cfg, struct server *server); +bool server_idle_check(struct server *server); -bool server_announce(struct cfg *cfg, struct server *server, int fd); +bool server_announce(struct server *server, int fd); -bool server_commit(struct cfg *cfg, struct server *server); +bool server_commit(struct server *server); -bool server_add_remote(struct cfg *cfg, struct server *server, - struct saddr *remote); +bool server_add_remote(struct server *server, struct saddr *remote); -bool server_add_local(struct cfg *cfg, struct server *server, - struct saddr *saddr); +bool server_add_local(struct server *server, struct saddr *saddr); -bool server_add_rcon(struct cfg *cfg, struct server *server, - struct saddr *rcon); +bool server_add_rcon(struct server *server, struct saddr *rcon); -bool server_set_rcon_password(struct cfg *cfg, struct server *server, - const char *password); +bool server_set_rcon_password(struct server *server, const char *password); -bool server_set_systemd_service(struct cfg *cfg, struct server *server, - const char *service); +bool server_set_systemd_service(struct server *server, const char *service); -bool server_set_stop_method(struct cfg *cfg, struct server *server, +bool server_set_stop_method(struct server *server, enum server_stop_method stop_method); -bool server_set_start_method(struct cfg *cfg, struct server *server, +bool server_set_start_method(struct server *server, enum server_start_method start_method); -bool server_set_stop_exec(struct cfg *cfg, struct server *server, const char *cmd); +bool server_set_stop_exec(struct server *server, const char *cmd); -bool server_set_start_exec(struct cfg *cfg, struct server *server, const char *cmd); +bool server_set_start_exec(struct server *server, const char *cmd); -bool server_set_idle_timeout(struct cfg *cfg, struct server *server, uint16_t timeout); +bool server_set_idle_timeout(struct server *server, uint16_t timeout); -bool server_set_port(struct cfg *cfg, struct server *server, uint16_t port); +bool server_set_port(struct server *server, uint16_t port); -bool server_set_type(struct cfg *cfg, struct server *server, - enum server_type type); +bool server_set_type(struct server *server, enum server_type type); -bool server_set_pretty_name(struct cfg *cfg, struct server *server, - const char *pretty_name); +bool server_set_pretty_name(struct server *server, const char *pretty_name); -struct server *server_new(struct cfg *cfg, const char *name); +struct server *server_new(const char *name); #endif diff --git a/systemd.c b/systemd.c index 8752b4e..c5c62d3 100644 --- a/systemd.c +++ b/systemd.c @@ -30,13 +30,13 @@ tohex(uint8_t val) * Example: minecraft@world1.service -> minecraft_40world1_2eservice */ char * -systemd_service_object_path(struct cfg *cfg, const char *service) +systemd_object_path(const char *service) { char *r; char *d; const char *s; - assert_return(cfg && service && !empty_str(service), NULL); + assert_return(service && !empty_str(service), NULL); r = zmalloc(strlen(SYSTEMD_DBUS_PATH_PREFIX) + strlen(service) * 3 + 1); if (!r) @@ -63,9 +63,8 @@ systemd_service_object_path(struct cfg *cfg, const char *service) } void -systemd_delete(struct cfg *cfg) +systemd_delete() { - assert_return(cfg); assert_return_silent(cfg->sd_bus); sd_bus_unref(cfg->sd_bus); @@ -73,12 +72,10 @@ systemd_delete(struct cfg *cfg) } static sd_bus * -get_bus(struct cfg *cfg) +get_bus() { int r; - assert_return(cfg, NULL); - if (cfg->sd_bus_failed) return NULL; @@ -106,15 +103,15 @@ get_bus(struct cfg *cfg) * "ActiveState" */ bool -systemd_service_running(struct cfg *cfg, struct server *server) +systemd_service_running(struct server *server) { - sd_bus *bus = get_bus(cfg); + sd_bus *bus = get_bus(); sd_bus_error error = SD_BUS_ERROR_NULL; char *status = NULL; bool running = false; int r; - assert_return(cfg && server && bus && server->systemd_service && server->systemd_obj, false); + assert_return(server && bus && server->systemd_service && server->systemd_obj, false); r = sd_bus_get_property_string(bus, SYSTEMD_DBUS_SERVICE, @@ -144,16 +141,16 @@ out: } static bool -systemd_service_action(struct cfg *cfg, struct server *server, const char *action) +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(cfg); + sd_bus *bus = get_bus(); const char *path; bool performed = false; int r; - assert_return(cfg && server && bus && server->systemd_service && server->systemd_obj && action, false); + assert_return(server && bus && server->systemd_service && server->systemd_obj && action, false); r = sd_bus_call_method(bus, SYSTEMD_DBUS_SERVICE, @@ -196,11 +193,11 @@ out: * --method org.freedesktop.systemd1.Unit.Stop "fail" */ bool -systemd_service_stop(struct cfg *cfg, struct server *server) +systemd_service_stop(struct server *server) { - assert_return(cfg && server, false); + assert_return(server, false); - return systemd_service_action(cfg, server, "Stop"); + return systemd_service_action(server, "Stop"); } /* @@ -213,10 +210,10 @@ systemd_service_stop(struct cfg *cfg, struct server *server) * --method org.freedesktop.systemd1.Unit.Start "fail" */ bool -systemd_service_start(struct cfg *cfg, struct server *server) +systemd_service_start(struct server *server) { - assert_return(cfg && server, false); + assert_return(server, false); - return systemd_service_action(cfg, server, "Start"); + return systemd_service_action(server, "Start"); } diff --git a/systemd.h b/systemd.h index ccf63dd..d455044 100644 --- a/systemd.h +++ b/systemd.h @@ -1,22 +1,14 @@ #ifndef foosystemdhfoo #define foosystemdhfoo -char *systemd_service_object_path(struct cfg *cfg, const char *service); +char *systemd_object_path(const char *service); -void systemd_delete(struct cfg *cfg); +void systemd_delete(); -bool systemd_service_running(struct cfg *cfg, struct server *server); +bool systemd_service_running(struct server *server); -bool systemd_service_stop(struct cfg *cfg, struct server *server); +bool systemd_service_stop(struct server *server); -bool systemd_service_start(struct cfg *cfg, struct server *server); - -/* -void idle_refdump(struct idle *idle); - -void idle_delete(struct cfg *cfg, struct server *server); - -void idle_init(struct cfg *cfg, struct server *server); -*/ +bool systemd_service_start(struct server *server); #endif diff --git a/uring.c b/uring.c index 9430ca9..65b59f9 100644 --- a/uring.c +++ b/uring.c @@ -11,7 +11,6 @@ struct uring_ev { struct io_uring uring; struct io_uring_params uring_params; - struct cfg *cfg; struct uring_task task; }; @@ -28,11 +27,11 @@ uint64_t sqe_count = 0; uint64_t cqe_count = 0; static struct io_uring_sqe * -get_sqe(struct cfg *cfg, struct uring_task *task) +get_sqe(struct uring_task *task) { struct io_uring_sqe *sqe; - assert_die(cfg && task, "invalid arguments"); + assert_die(task, "invalid arguments"); sqe = io_uring_get_sqe(&cfg->uev->uring); if (!sqe) { @@ -43,7 +42,7 @@ get_sqe(struct cfg *cfg, struct uring_task *task) } sqe_count++; - uring_task_get(cfg, task); + uring_task_get(task); return sqe; } @@ -89,9 +88,9 @@ uring_task_refdump(struct uring_task *task) * while the task is active. */ void -uring_task_destroy(struct cfg *cfg, struct uring_task *task) +uring_task_destroy(struct uring_task *task) { - assert_return(cfg && task); + assert_return(task); debug(DBG_UR, "task %s (%p), fd %i, refcount %u", task->name, task, task->fd, task->refcount); @@ -99,21 +98,21 @@ uring_task_destroy(struct cfg *cfg, struct uring_task *task) if (task->fd >= 0) { struct io_uring_sqe *sqe; - sqe = get_sqe(cfg, task); + sqe = get_sqe(task); io_uring_prep_cancel(sqe, task, 0); io_uring_sqe_set_data(sqe, (void *)((uintptr_t)task | CQE_TYPE_CANCEL)); } task->dead = true; - uring_task_put(cfg, task); + uring_task_put(task); } void -uring_task_put(struct cfg *cfg, struct uring_task *task) +uring_task_put(struct uring_task *task) { struct uring_task *parent; - assert_return(cfg && task); + assert_return(task); debug(DBG_REF, "task %s (%p), refcount %u -> %u", task->name, task, task->refcount, task->refcount - 1); @@ -127,7 +126,7 @@ uring_task_put(struct cfg *cfg, struct uring_task *task) error("Negative refcount!"); if (task->fd >= 0) { - uring_task_close_fd(cfg, task); + uring_task_close_fd(task); /* We'll be called again once the fd is closed */ return; } @@ -138,14 +137,14 @@ uring_task_put(struct cfg *cfg, struct uring_task *task) if (parent) { debug(DBG_REF, "putting parent %s (%p)", parent->name, parent); - uring_task_put(cfg, parent); + uring_task_put(parent); } } void -uring_task_get(struct cfg *cfg, struct uring_task *task) +uring_task_get(struct uring_task *task) { - assert_return(cfg && task); + assert_return(task); debug(DBG_REF, "task %s (%p), refcount %u -> %u", task->name, task, task->refcount, task->refcount + 1); @@ -187,9 +186,9 @@ uring_task_set_fd(struct uring_task *task, int fd) } void -uring_task_close_fd(struct cfg *cfg, struct uring_task *task) +uring_task_close_fd(struct uring_task *task) { - assert_return(cfg && task); + assert_return(task); debug(DBG_UR, "task %s (%p), fd %i, refcount %u", task->name, task, task->fd, task->refcount); @@ -197,25 +196,25 @@ uring_task_close_fd(struct cfg *cfg, struct uring_task *task) if (task->fd < 0) return; - uring_close(cfg, task, task->fd); + uring_close(task, task->fd); task->fd = -1; } struct uring_task * -uring_parent(struct cfg *cfg) +uring_parent() { - assert_die(cfg && cfg->uev, "invalid arguments"); + assert_die(cfg->uev, "invalid arguments"); return &cfg->uev->task; } void -uring_task_init(struct cfg *cfg, struct uring_task *task, const char *name, +uring_task_init(struct uring_task *task, const char *name, struct uring_task *parent, void (*free)(struct uring_task *)) { static bool first = true; - assert_die(cfg && task && !empty_str(name) && free, "invalid arguments"); + assert_die(task && !empty_str(name) && free, "invalid arguments"); if (first) first = false; @@ -235,31 +234,31 @@ uring_task_init(struct cfg *cfg, struct uring_task *task, const char *name, "getting parent %s (%p), refcount %u", task->name, task, task->refcount, task->parent->name, task->parent, task->parent->refcount); - uring_task_get(cfg, task->parent); + uring_task_get(task->parent); } } void -uring_close(struct cfg *cfg, struct uring_task *task, int fd) +uring_close(struct uring_task *task, int fd) { struct io_uring_sqe *sqe; - assert_return(cfg && task && fd >= 0); + assert_return(task && fd >= 0); debug(DBG_UR, "task %s (%p), fd %i, refcount %u", task->name, task, task->fd, task->refcount); - sqe = get_sqe(cfg, task); + sqe = get_sqe(task); io_uring_prep_close(sqe, fd); io_uring_sqe_set_data(sqe, (void *)((uintptr_t)task | CQE_TYPE_CLOSE)); } static void -uring_tbuf_write_cb(struct cfg *cfg, struct uring_task *task, int res) +uring_tbuf_write_cb(struct uring_task *task, int res) { int r; - assert_return(cfg && task && task->tbuf && task->final_cb); + assert_return(task && task->tbuf && task->final_cb); debug(DBG_UR, "task %s (%p), fd %i, refcount %u", task->name, task, task->fd, task->refcount); @@ -276,50 +275,50 @@ uring_tbuf_write_cb(struct cfg *cfg, struct uring_task *task, int res) goto finished; } - uring_write(cfg, task, task->tbuf->buf + task->tbuf->done, + uring_write(task, task->tbuf->buf + task->tbuf->done, task->tbuf->len - task->tbuf->done, uring_tbuf_write_cb); return; finished: - task->final_cb(cfg, task, r); + task->final_cb(task, r); } void -uring_tbuf_write(struct cfg *cfg, struct uring_task *task, utask_cb_t final_cb) +uring_tbuf_write(struct uring_task *task, utask_cb_t final_cb) { - assert_return(cfg && task && task->fd >= 0 && task->tbuf && task->tbuf->len > 0); + assert_return(task && task->fd >= 0 && task->tbuf && task->tbuf->len > 0); debug(DBG_UR, "task %s (%p), fd %i, refcount %u", task->name, task, task->fd, task->refcount); task->tbuf->done = 0; task->final_cb = final_cb; - uring_write(cfg, task, &task->tbuf->buf, task->tbuf->len, uring_tbuf_write_cb); + uring_write(task, &task->tbuf->buf, task->tbuf->len, uring_tbuf_write_cb); } void -uring_write(struct cfg *cfg, struct uring_task *task, void *buf, size_t len, utask_cb_t cb) +uring_write(struct uring_task *task, void *buf, size_t len, utask_cb_t cb) { struct io_uring_sqe *sqe; - assert_return(cfg && task && buf && len > 0 && cb && task->fd >= 0); + assert_return(task && buf && len > 0 && cb && task->fd >= 0); debug(DBG_UR, "task %s (%p), fd %i, refcount %u", task->name, task, task->fd, task->refcount); - sqe = get_sqe(cfg, task); + sqe = get_sqe(task); task->cb = cb; io_uring_prep_write(sqe, task->fd, buf, len, 0); io_uring_sqe_set_data(sqe, task); } static void -uring_tbuf_read_until_cb(struct cfg *cfg, struct uring_task *task, int res) +uring_tbuf_read_until_cb(struct uring_task *task, int res) { int r; - assert_return(cfg && task && task->tbuf && task->final_cb && task->is_complete_cb); + assert_return(task && task->tbuf && task->final_cb && task->is_complete_cb); debug(DBG_UR, "task %s (%p), fd %i, refcount %u", task->name, task, task->fd, task->refcount); @@ -330,7 +329,7 @@ uring_tbuf_read_until_cb(struct cfg *cfg, struct uring_task *task, int res) } task->tbuf->len += res; - r = task->is_complete_cb(cfg, task, res); + r = task->is_complete_cb(task, res); if (r < 0) { r = res; goto finished; @@ -345,20 +344,20 @@ uring_tbuf_read_until_cb(struct cfg *cfg, struct uring_task *task, int res) goto finished; } - uring_read_offset(cfg, task, task->tbuf->buf + task->tbuf->len, + uring_read_offset(task, task->tbuf->buf + task->tbuf->len, sizeof(task->tbuf->buf) - task->tbuf->len, task->tbuf->len, uring_tbuf_read_until_cb); return; finished: - task->final_cb(cfg, task, r); + task->final_cb(task, r); } void -uring_tbuf_read_until(struct cfg *cfg, struct uring_task *task, +uring_tbuf_read_until(struct uring_task *task, rutask_cb_t is_complete_cb, utask_cb_t final_cb) { - assert_return(cfg && task && task->fd >= 0 && task->tbuf && is_complete_cb && final_cb); + assert_return(task && task->fd >= 0 && task->tbuf && is_complete_cb && final_cb); debug(DBG_UR, "task %s (%p), fd %i, refcount %u", task->name, task, task->fd, task->refcount); @@ -366,14 +365,14 @@ uring_tbuf_read_until(struct cfg *cfg, struct uring_task *task, task->tbuf->len = 0; task->is_complete_cb = is_complete_cb; task->final_cb = final_cb; - uring_read(cfg, task, &task->tbuf->buf, sizeof(task->tbuf->buf), + uring_read(task, &task->tbuf->buf, sizeof(task->tbuf->buf), uring_tbuf_read_until_cb); } static int -uring_tbuf_eof(struct cfg *cfg, struct uring_task *task, int res) +uring_tbuf_eof(struct uring_task *task, int res) { - assert_return(cfg && task && task->tbuf, -EINVAL); + assert_return(task && task->tbuf, -EINVAL); assert_task_alive_or(DBG_UR, task, return -EINTR); if (task->tbuf->len + 1 >= sizeof(task->tbuf->buf)) @@ -388,18 +387,18 @@ uring_tbuf_eof(struct cfg *cfg, struct uring_task *task, int res) } void -uring_tbuf_read_until_eof(struct cfg *cfg, struct uring_task *task, +uring_tbuf_read_until_eof(struct uring_task *task, utask_cb_t final_cb) { - assert_return(cfg && task && task->tbuf && final_cb); + assert_return(task && task->tbuf && final_cb); - uring_tbuf_read_until(cfg, task, uring_tbuf_eof, final_cb); + uring_tbuf_read_until(task, uring_tbuf_eof, final_cb); } static int -uring_tbuf_have_data(struct cfg *cfg, struct uring_task *task, int res) +uring_tbuf_have_data(struct uring_task *task, int res) { - assert_return(cfg && task, -EINVAL); + assert_return(task, -EINVAL); if (res < 0) return res; @@ -408,56 +407,56 @@ uring_tbuf_have_data(struct cfg *cfg, struct uring_task *task, int res) } void -uring_tbuf_read(struct cfg *cfg, struct uring_task *task, utask_cb_t final_cb) +uring_tbuf_read(struct uring_task *task, utask_cb_t final_cb) { - assert_return(cfg && task && final_cb); + assert_return(task && final_cb); - uring_tbuf_read_until(cfg, task, uring_tbuf_have_data, final_cb); + uring_tbuf_read_until(task, uring_tbuf_have_data, final_cb); } void -uring_read_offset(struct cfg *cfg, struct uring_task *task, void *buf, size_t len, off_t offset, utask_cb_t cb) +uring_read_offset(struct uring_task *task, void *buf, size_t len, off_t offset, utask_cb_t cb) { struct io_uring_sqe *sqe; - assert_return(cfg && task && buf && len > 0 && task->fd >= 0); + assert_return(task && buf && len > 0 && task->fd >= 0); debug(DBG_UR, "task %s (%p), fd %i, refcount %u", task->name, task, task->fd, task->refcount); - sqe = get_sqe(cfg, task); + sqe = get_sqe(task); task->cb = cb; io_uring_prep_read(sqe, task->fd, buf, len, offset); io_uring_sqe_set_data(sqe, task); } void -uring_openat(struct cfg *cfg, struct uring_task *task, const char *path, utask_cb_t cb) +uring_openat(struct uring_task *task, const char *path, utask_cb_t cb) { struct io_uring_sqe *sqe; - assert_return(cfg && task && !empty_str(path) && cb); + assert_return(task && !empty_str(path) && cb); debug(DBG_UR, "task %s (%p), fd %i, refcount %u", task->name, task, task->fd, task->refcount); - sqe = get_sqe(cfg, task); + sqe = get_sqe(task); task->cb = cb; io_uring_prep_openat(sqe, AT_FDCWD, path, O_RDONLY | O_CLOEXEC, 0); io_uring_sqe_set_data(sqe, task); } void -uring_tbuf_recvmsg(struct cfg *cfg, struct uring_task *task, utask_cb_t cb) +uring_tbuf_recvmsg(struct uring_task *task, utask_cb_t cb) { struct io_uring_sqe *sqe; - assert_return(cfg && task && task->fd >= 0 && task->tbuf && cb); + assert_return(task && task->fd >= 0 && task->tbuf && cb); debug(DBG_UR, "task %s (%p), fd %i, refcount %u", task->name, task, task->fd, task->refcount); - sqe = get_sqe(cfg, task); + sqe = get_sqe(task); task->tbuf->done = 0; task->tbuf->len = 0; task->tbuf->iov.iov_len = sizeof(task->tbuf->buf); @@ -468,16 +467,16 @@ uring_tbuf_recvmsg(struct cfg *cfg, struct uring_task *task, utask_cb_t cb) } void -uring_tbuf_sendmsg(struct cfg *cfg, struct uring_task *task, utask_cb_t cb) +uring_tbuf_sendmsg(struct uring_task *task, utask_cb_t cb) { struct io_uring_sqe *sqe; - assert_return(cfg && task && task->fd >= 0 && task->tbuf && cb); + assert_return(task && task->fd >= 0 && task->tbuf && cb); debug(DBG_UR, "task %s (%p), fd %i, refcount %u", task->name, task, task->fd, task->refcount); - sqe = get_sqe(cfg, task); + sqe = get_sqe(task); task->tbuf->done = 0; task->tbuf->iov.iov_len = task->tbuf->len; task->tbuf->msg.msg_namelen = task->saddr.addrlen; @@ -487,32 +486,32 @@ uring_tbuf_sendmsg(struct cfg *cfg, struct uring_task *task, utask_cb_t cb) } void -uring_connect(struct cfg *cfg, struct uring_task *task, struct saddr *saddr, utask_cb_t cb) +uring_connect(struct uring_task *task, struct saddr *saddr, utask_cb_t cb) { struct io_uring_sqe *sqe; - assert_return(cfg && task && task->fd >= 0 && saddr && cb); + assert_return(task && task->fd >= 0 && saddr && cb); debug(DBG_UR, "task %s (%p), fd %i, refcount %u", task->name, task, task->fd, task->refcount); - sqe = get_sqe(cfg, task); + sqe = get_sqe(task); task->cb = cb; io_uring_prep_connect(sqe, task->fd, (struct sockaddr *)&saddr->storage, saddr->addrlen); io_uring_sqe_set_data(sqe, task); } void -uring_accept(struct cfg *cfg, struct uring_task *task, struct saddr *saddr, utask_cb_t cb) +uring_accept(struct uring_task *task, struct saddr *saddr, utask_cb_t cb) { struct io_uring_sqe *sqe; - assert_return(cfg && task && task->fd >= 0 && saddr && cb); + assert_return(task && task->fd >= 0 && saddr && cb); debug(DBG_UR, "task %s (%p), fd %i, refcount %u", task->name, task, task->fd, task->refcount); - sqe = get_sqe(cfg, task); + sqe = get_sqe(task); saddr->addrlen = sizeof(saddr->storage); task->cb = cb; io_uring_prep_accept(sqe, task->fd, (struct sockaddr *)&saddr->storage, &saddr->addrlen, SOCK_CLOEXEC); @@ -520,27 +519,27 @@ uring_accept(struct cfg *cfg, struct uring_task *task, struct saddr *saddr, utas } void -uring_poll(struct cfg *cfg, struct uring_task *task, short poll_mask, utask_cb_t cb) +uring_poll(struct uring_task *task, short poll_mask, utask_cb_t cb) { struct io_uring_sqe *sqe; - assert_return(cfg && task && task->fd >= 0 && poll_mask && cb); + assert_return(task && task->fd >= 0 && poll_mask && cb); debug(DBG_UR, "task %s (%p), fd %i, refcount %u", task->name, task, task->fd, task->refcount); - sqe = get_sqe(cfg, task); + sqe = get_sqe(task); task->cb = cb; io_uring_prep_poll_add(sqe, task->fd, poll_mask); io_uring_sqe_set_data(sqe, task); } void -uring_poll_cancel(struct cfg *cfg, struct uring_task *task) +uring_poll_cancel(struct uring_task *task) { struct io_uring_sqe *sqe; - assert_return(cfg && task); + assert_return(task); if (task->fd < 0) { /* not an error, no need to print error msg */ @@ -550,7 +549,7 @@ uring_poll_cancel(struct cfg *cfg, struct uring_task *task) debug(DBG_UR, "task %s (%p), fd %i, refcount %u", task->name, task, task->fd, task->refcount); - sqe = get_sqe(cfg, task); + sqe = get_sqe(task); task->dead = true; io_uring_prep_poll_remove(sqe, task); io_uring_sqe_set_data(sqe, (void *)((uintptr_t)task | CQE_TYPE_POLL_CANCEL)); @@ -567,7 +566,7 @@ uring_free(struct uring_task *task) task->name, task, task->fd, task->refcount); io_uring_queue_exit(&uev->uring); - uev->cfg->uev = NULL; + cfg->uev = NULL; xfree(uev); } @@ -580,26 +579,24 @@ uring_refdump(struct uring_ev *uev) } void -uring_delete(struct cfg *cfg) +uring_delete() { struct uring_task *task; - assert_return(cfg && cfg->uev); + assert_return(cfg->uev); task = &cfg->uev->task; debug(DBG_UR, "task %s (%p), fd %i, refcount %u", task->name, task, task->fd, task->refcount); - uring_task_put(cfg, task); + uring_task_put(task); } void -uring_init(struct cfg *cfg) +uring_init() { struct uring_ev *uev; - assert_return(cfg); - uev = zmalloc(sizeof(*uev)); if (!uev) die("malloc: %m"); @@ -610,16 +607,15 @@ uring_init(struct cfg *cfg) debug(DBG_UR, "uring initialized, features: 0x%08x", uev->uring_params.features); - uring_task_init(cfg, &uev->task, "io_uring", &cfg->task, uring_free); + uring_task_init(&uev->task, "io_uring", &cfg->task, uring_free); cfg->uev = uev; - uev->cfg = cfg; } static inline void -uring_print_cqe(struct cfg *cfg, const char *type, struct uring_task *task, +uring_print_cqe(const char *type, struct uring_task *task, struct io_uring_cqe *cqe) { - assert_return(cfg && !empty_str(type) && task && cqe); + assert_return(!empty_str(type) && task && cqe); debug(DBG_UR, "got CQE " "(type: %s, res: %i (%s), task: %s (%p), fd: %i, cb: %p)", @@ -633,10 +629,8 @@ uring_print_cqe(struct cfg *cfg, const char *type, struct uring_task *task, } void -uring_event_loop(struct cfg *cfg) +uring_event_loop() { - assert_return(cfg); - while (true) { struct io_uring_cqe *cqe; unsigned nr, head; @@ -667,22 +661,22 @@ uring_event_loop(struct cfg *cfg) switch (cqe_type) { case CQE_TYPE_CANCEL: - uring_print_cqe(cfg, "cancel", task, cqe); + uring_print_cqe("cancel", task, cqe); do_cb = false; break; case CQE_TYPE_CLOSE: - uring_print_cqe(cfg, "close", task, cqe); + uring_print_cqe("close", task, cqe); do_cb = false; break; case CQE_TYPE_POLL_CANCEL: - uring_print_cqe(cfg, "poll_cancel", task, cqe); + uring_print_cqe("poll_cancel", task, cqe); do_cb = false; break; case CQE_TYPE_NORMAL: - uring_print_cqe(cfg, "standard", task, cqe); + uring_print_cqe("standard", task, cqe); do_cb = true; break; @@ -691,9 +685,9 @@ uring_event_loop(struct cfg *cfg) } if (do_cb && task->cb) - task->cb(cfg, task, cqe->res); + task->cb(task, cqe->res); - uring_task_put(cfg, task); + uring_task_put(task); if (exiting) return; diff --git a/uring.h b/uring.h index dbf7faf..70c72e5 100644 --- a/uring.h +++ b/uring.h @@ -6,75 +6,66 @@ extern uint64_t cqe_count; void uring_task_refdump(struct uring_task *task); -void uring_task_destroy(struct cfg *cfg, struct uring_task *task); +void uring_task_destroy(struct uring_task *task); -void uring_task_put(struct cfg *cfg, struct uring_task *task); +void uring_task_put(struct uring_task *task); -void uring_task_get(struct cfg *cfg, struct uring_task *task); +void uring_task_get(struct uring_task *task); void uring_task_set_buf(struct uring_task *task, struct uring_task_buf *tbuf); void uring_task_set_fd(struct uring_task *task, int fd); -void uring_task_close_fd(struct cfg *cfg, struct uring_task *task); +void uring_task_close_fd(struct uring_task *task); -struct uring_task *uring_parent(struct cfg *cfg); +struct uring_task *uring_parent(); -void uring_task_init(struct cfg *cfg, struct uring_task *task, const char *name, +void uring_task_init(struct uring_task *task, const char *name, struct uring_task *parent, void (*free)(struct uring_task *)); -void uring_close(struct cfg *cfg, struct uring_task *task, int fd); +void uring_close(struct uring_task *task, int fd); -void uring_tbuf_write(struct cfg *cfg, struct uring_task *task, - utask_cb_t final_cb); +void uring_tbuf_write(struct uring_task *task, utask_cb_t final_cb); -void uring_write(struct cfg *cfg, struct uring_task *task, void *buf, - size_t len, utask_cb_t cb); +void uring_write(struct uring_task *task, void *buf, size_t len, utask_cb_t cb); -void uring_tbuf_read_until(struct cfg *cfg, struct uring_task *task, +void uring_tbuf_read_until(struct uring_task *task, rutask_cb_t is_complete_cb, utask_cb_t final_cb); -void uring_tbuf_read_until_eof(struct cfg *cfg, struct uring_task *task, - utask_cb_t final_cb); +void uring_tbuf_read_until_eof(struct uring_task *task, utask_cb_t final_cb); -void uring_tbuf_read(struct cfg *cfg, struct uring_task *task, - utask_cb_t final_cb); +void uring_tbuf_read(struct uring_task *task, utask_cb_t final_cb); -void uring_read_offset(struct cfg *cfg, struct uring_task *task, void *buf, +void uring_read_offset(struct uring_task *task, void *buf, size_t len, off_t offset, utask_cb_t cb); static inline void -uring_read(struct cfg *cfg, struct uring_task *task, void *buf, size_t len, - utask_cb_t cb) +uring_read(struct uring_task *task, void *buf, size_t len, utask_cb_t cb) { - uring_read_offset(cfg, task, buf, len, 0, cb); + uring_read_offset(task, buf, len, 0, cb); } -void uring_openat(struct cfg *cfg, struct uring_task *task, const char *path, - utask_cb_t cb); +void uring_openat(struct uring_task *task, const char *path, utask_cb_t cb); -void uring_tbuf_recvmsg(struct cfg *cfg, struct uring_task *task, utask_cb_t cb); +void uring_tbuf_recvmsg(struct uring_task *task, utask_cb_t cb); -void uring_tbuf_sendmsg(struct cfg *cfg, struct uring_task *task, utask_cb_t cb); +void uring_tbuf_sendmsg(struct uring_task *task, utask_cb_t cb); -void uring_connect(struct cfg *cfg, struct uring_task *task, - struct saddr *saddr, utask_cb_t cb); +void uring_connect(struct uring_task *task, struct saddr *saddr, utask_cb_t cb); -void uring_accept(struct cfg *cfg, struct uring_task *task, - struct saddr *saddr, utask_cb_t cb); +void uring_accept(struct uring_task *task, struct saddr *saddr, utask_cb_t cb); -void uring_poll(struct cfg *cfg, struct uring_task *task, short poll_mask, - utask_cb_t cb); +void uring_poll(struct uring_task *task, short poll_mask, utask_cb_t cb); -void uring_poll_cancel(struct cfg *cfg, struct uring_task *task); +void uring_poll_cancel(struct uring_task *task); -void uring_delete(struct cfg *cfg); +void uring_delete(); void uring_refdump(struct uring_ev *uev); -void uring_init(struct cfg *cfg); +void uring_init(); -void uring_event_loop(struct cfg *cfg); +void uring_event_loop(); #endif diff --git a/utils.c b/utils.c index ff18fb1..92e607d 100644 --- a/utils.c +++ b/utils.c @@ -169,11 +169,11 @@ debug_resource_usage() } void -socket_set_low_latency(struct cfg *cfg, int sfd) +socket_set_low_latency(int sfd) { int option; - assert_return(cfg && sfd >= 0); + assert_return(sfd >= 0); /* FIXME: could make this configurable */ option = true; @@ -192,9 +192,9 @@ socket_set_low_latency(struct cfg *cfg, int sfd) } void -connection_set_local(struct cfg *cfg, struct connection *conn, int fd) +connection_set_local(struct connection *conn, int fd) { - assert_return(cfg && conn && fd >= 0); + assert_return(conn && fd >= 0); conn->local.addrlen = sizeof(conn->local.storage); if (getsockname(fd, (struct sockaddr *)&conn->local.storage, @@ -205,48 +205,48 @@ connection_set_local(struct cfg *cfg, struct connection *conn, int fd) } void -connection_set_remote(struct cfg *cfg, struct connection *conn, struct saddr *remote) +connection_set_remote(struct connection *conn, struct saddr *remote) { - assert_return(cfg && conn && remote); + assert_return(conn && remote); conn->remote = *remote; saddr_set_addrstr(&conn->remote); } -static void connect_next(struct cfg *cfg, struct uring_task *task, struct connection *conn); +static void connect_next(struct uring_task *task, struct connection *conn); static void -connect_cb(struct cfg *cfg, struct uring_task *task, int res) +connect_cb(struct uring_task *task, int res) { struct connection *conn; - assert_return(cfg && task && task->priv); + assert_return(task && task->priv); conn = task->priv; if (res < 0) { debug(DBG_UR, "%s: connection to %s failed", task->name, conn->remote.addrstr); - uring_task_close_fd(cfg, task); - connect_next(cfg, task, conn); + uring_task_close_fd(task); + connect_next(task, conn); return; } - connection_set_local(cfg, conn, task->fd); + connection_set_local(conn, task->fd); debug(DBG_UR, "%s: connection established %s -> %s", task->name, conn->local.addrstr, conn->remote.addrstr); - conn->callback(cfg, conn, true); + conn->cb(conn, true); } static void -connect_next(struct cfg *cfg, struct uring_task *task, struct connection *conn) +connect_next(struct uring_task *task, struct connection *conn) { struct saddr *remote, *tmp; int sfd; unsigned i; - assert_return(cfg && task && conn && conn->callback); + assert_return(task && conn && conn->cb); again: assert_task_alive_or(DBG_UR, task, goto out); @@ -267,7 +267,7 @@ again: } conn->next_addr++; - connection_set_remote(cfg, conn, remote); + connection_set_remote(conn, remote); debug(DBG_MALLOC, "%s: attempting to connect to %s", task->name, conn->remote.addrstr); @@ -277,28 +277,28 @@ again: goto again; } - socket_set_low_latency(cfg, sfd); + socket_set_low_latency(sfd); task->priv = conn; uring_task_set_fd(task, sfd); - uring_connect(cfg, task, &conn->remote, connect_cb); + uring_connect(task, &conn->remote, connect_cb); return; out: - conn->callback(cfg, conn, false); + conn->cb(conn, false); } void -connect_any(struct cfg *cfg, struct uring_task *task, +connect_any(struct uring_task *task, struct list_head *addrs, struct connection *conn, - void (*callback)(struct cfg *, struct connection *, bool res)) + connection_cb_t cb) { - assert_return(cfg && task && addrs && conn && callback); + assert_return(task && addrs && conn && cb); conn->next_addr = 0; conn->addrs = addrs; - conn->callback = callback; - connect_next(cfg, task, conn); + conn->cb = cb; + connect_next(task, conn); } uint16_t diff --git a/utils.h b/utils.h index 3e807bc..ff29d6c 100644 --- a/utils.h +++ b/utils.h @@ -49,6 +49,10 @@ struct saddr { struct list_head list; }; +struct connection; + +typedef void(*connection_cb_t)(struct connection *, bool); + struct connection { struct saddr remote; struct saddr local; @@ -56,21 +60,20 @@ struct connection { struct list_head *addrs; unsigned next_addr; - void (*callback)(struct cfg *, struct connection *, bool); + connection_cb_t cb; }; struct uring_task; -void socket_set_low_latency(struct cfg *cfg, int sfd); +void socket_set_low_latency(int sfd); -void connection_set_local(struct cfg *cfg, struct connection *conn, int fd); +void connection_set_local(struct connection *conn, int fd); -void connection_set_remote(struct cfg *cfg, struct connection *conn, - struct saddr *remote); +void connection_set_remote(struct connection *conn, struct saddr *remote); -void connect_any(struct cfg *cfg, struct uring_task *task, +void connect_any(struct uring_task *task, struct list_head *addrs, struct connection *conn, - void (*callback)(struct cfg *, struct connection *, bool)); + connection_cb_t cb); char *saddr_addr(struct saddr *saddr, char *buf, size_t len); -- cgit v1.2.3