diff options
Diffstat (limited to 'server.c')
-rw-r--r-- | server.c | 422 |
1 files changed, 211 insertions, 211 deletions
@@ -32,9 +32,9 @@ struct server_local { }; static bool -set_property(struct cfg *cfg, struct server *scfg, char **property, const char *value) +set_property(struct cfg *cfg, struct server *server, char **property, const char *value) { - if (!cfg || !scfg || empty_str(value) || *property) + if (!cfg || !server || empty_str(value) || *property) return false; *property = xstrdup(value); @@ -64,22 +64,22 @@ server_refdump(struct server *server) static void server_free(struct uring_task *task) { - struct server *scfg = container_of(task, struct server, task); - - debug(DBG_SRV, "freeing server %s (%p)", scfg->name, scfg); - list_del(&scfg->list); - xfree(scfg->pretty_name); - xfree(scfg->start_exec); - xfree(scfg->stop_exec); - xfree(scfg->systemd_service); - xfree(scfg->systemd_obj); - xfree(scfg->rcon_password); - xfree(scfg->name); - xfree(scfg); + struct server *server = container_of(task, struct server, task); + + debug(DBG_SRV, "freeing server %s (%p)", server->name, server); + list_del(&server->list); + xfree(server->pretty_name); + xfree(server->start_exec); + xfree(server->stop_exec); + xfree(server->systemd_service); + xfree(server->systemd_obj); + xfree(server->rcon_password); + xfree(server->name); + xfree(server); } void -server_delete(struct cfg *cfg, struct server *scfg) +server_delete(struct cfg *cfg, struct server *server) { struct server_local *local, *ltmp; struct server_proxy *proxy, *ptmp; @@ -88,60 +88,60 @@ server_delete(struct cfg *cfg, struct server *scfg) struct saddr *tmp; struct dns_async *dns, *dtmp; - verbose("Removing server %s", scfg->name); + verbose("Removing server %s", server->name); - idle_delete(cfg, scfg); - rcon_delete(cfg, scfg); + idle_delete(cfg, server); + rcon_delete(cfg, server); - list_for_each_entry_safe(local, ltmp, &scfg->locals, list) + list_for_each_entry_safe(local, ltmp, &server->locals, list) uring_task_destroy(cfg, &local->task); - list_for_each_entry_safe(proxy, ptmp, &scfg->proxys, list) + list_for_each_entry_safe(proxy, ptmp, &server->proxys, list) proxy_delete(cfg, proxy); - list_for_each_entry_safe(rcon, tmp, &scfg->rcons, list) { + list_for_each_entry_safe(rcon, tmp, &server->rcons, list) { list_del(&rcon->list); xfree(rcon); } - list_for_each_entry_safe(remote, tmp, &scfg->remotes, list) { + list_for_each_entry_safe(remote, tmp, &server->remotes, list) { list_del(&remote->list); xfree(remote); } - list_for_each_entry_safe(dns, dtmp, &scfg->dnslookups, list) + list_for_each_entry_safe(dns, dtmp, &server->dnslookups, list) gai_cancel(&dns->gcb); - uring_poll_cancel(cfg, &scfg->exec_task); - uring_task_put(cfg, &scfg->exec_task); - uring_task_destroy(cfg, &scfg->task); + uring_poll_cancel(cfg, &server->exec_task); + uring_task_put(cfg, &server->exec_task); + uring_task_destroy(cfg, &server->task); } void server_delete_by_name(struct cfg *cfg, const char *name) { - struct server *scfg; + struct server *server; if (!cfg || empty_str(name)) return; - list_for_each_entry(scfg, &cfg->servers, list) { - if (!strcmp(scfg->name, name)) { - server_delete(cfg, scfg); + list_for_each_entry(server, &cfg->servers, list) { + if (!strcmp(server->name, name)) { + server_delete(cfg, server); return; } } } static void -server_dump(struct server *scfg) +server_dump(struct server *server) { struct server_local *local; struct saddr *remote; struct saddr *rcon; - verbose("Server %s:", scfg->name); - switch (scfg->type) { + verbose("Server %s:", server->name); + switch (server->type) { case SERVER_TYPE_ANNOUNCE: verbose(" * Type: announce"); break; @@ -152,24 +152,24 @@ server_dump(struct server *scfg) verbose(" * Type: unknown"); break; } - verbose(" * Name: %s", scfg->pretty_name ? scfg->pretty_name : "<undefined>"); - verbose(" * Announce port: %" PRIu16, scfg->announce_port); + verbose(" * Name: %s", server->pretty_name ? server->pretty_name : "<undefined>"); + verbose(" * Announce port: %" PRIu16, server->announce_port); - if (!list_empty(&scfg->locals)) { + if (!list_empty(&server->locals)) { verbose(" * Local:"); - list_for_each_entry(local, &scfg->locals, list) + list_for_each_entry(local, &server->locals, list) verbose(" * %s", local->local.addrstr); } - if (!list_empty(&scfg->remotes)) { + if (!list_empty(&server->remotes)) { verbose(" * Remote:"); - list_for_each_entry(remote, &scfg->remotes, list) + list_for_each_entry(remote, &server->remotes, list) verbose(" * %s", remote->addrstr); } - if (!list_empty(&scfg->rcons)) { + if (!list_empty(&server->rcons)) { verbose(" * RCon:"); - list_for_each_entry(rcon, &scfg->rcons, list) + list_for_each_entry(rcon, &server->rcons, list) verbose(" * %s", rcon->addrstr); } @@ -190,10 +190,10 @@ static void server_local_accept(struct cfg *cfg, struct uring_task *task, int res) { struct server_local *local = container_of(task, struct server_local, task); - struct server *scfg = container_of(task->parent, struct server, task); + struct server *server = container_of(task->parent, struct server, task); struct server_proxy *proxy; - debug(DBG_SRV, "task %p, res %i, scfg %s", task, res, scfg->name); + debug(DBG_SRV, "task %p, res %i, server %s", task, res, server->name); assert_task_alive(DBG_SRV, task); @@ -205,16 +205,16 @@ server_local_accept(struct cfg *cfg, struct uring_task *task, int res) saddr_set_addrstr(&local->client); verbose("%s: incoming proxy connection: %s -> %s", - scfg->name, local->client.addrstr, local->local.addrstr); + server->name, local->client.addrstr, local->local.addrstr); - if (list_empty(&scfg->remotes)) { + if (list_empty(&server->remotes)) { /* This shouldn't be possible, checked before opening local */ - error("scfg->remotes empty!"); + error("server->remotes empty!"); uring_close(cfg, &local->task, res); goto out; } - proxy = proxy_new(cfg, scfg, &local->client, res); + proxy = proxy_new(cfg, server, &local->client, res); if (!proxy) uring_close(cfg, &local->task, res); @@ -223,7 +223,7 @@ out: } static bool -server_local_open(struct cfg *cfg, struct server *scfg, struct server_local *local) +server_local_open(struct cfg *cfg, struct server *server, struct server_local *local) { int sfd; int option; @@ -279,7 +279,7 @@ error: static void server_exec_free(struct uring_task *task) { - //struct server *scfg = container_of(task, struct server, exec_task); + //struct server *server = container_of(task, struct server, exec_task); debug(DBG_SRV, "called"); } @@ -292,7 +292,7 @@ server_exec_free(struct uring_task *task) static void server_exec_done(struct cfg *cfg, struct uring_task *task, int res) { - struct server *scfg = container_of(task, struct server, exec_task); + struct server *server = container_of(task, struct server, exec_task); int r; siginfo_t info; @@ -304,7 +304,7 @@ server_exec_done(struct cfg *cfg, struct uring_task *task, int res) goto out; } - r = waitid(P_PIDFD, scfg->exec_task.fd, &info, WEXITED); + r = waitid(P_PIDFD, server->exec_task.fd, &info, WEXITED); if (r < 0) { error("waitid: %m"); goto out; @@ -316,7 +316,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, &scfg->exec_task); + uring_task_close_fd(cfg, &server->exec_task); } static int @@ -333,16 +333,16 @@ server_exec_child(void *ptr) #endif static bool -server_exec(struct cfg *cfg, struct server *scfg, const char *cmd) +server_exec(struct cfg *cfg, struct server *server, const char *cmd) { char stack[4096]; /* Beautiful/horrible hack :) */ int pidfd; int r; - if (!cfg || !scfg || !cmd) + if (!cfg || !server || !cmd) return false; - if (scfg->exec_task.fd >= 0) + if (server->exec_task.fd >= 0) return false; r = clone(server_exec_child, stack + sizeof(stack), @@ -353,22 +353,22 @@ server_exec(struct cfg *cfg, struct server *scfg, const char *cmd) return false; } - uring_task_set_fd(&scfg->exec_task, pidfd); - uring_poll(cfg, &scfg->exec_task, POLLIN, server_exec_done); + uring_task_set_fd(&server->exec_task, pidfd); + uring_poll(cfg, &server->exec_task, POLLIN, server_exec_done); return true; } static bool -server_check_running(struct cfg *cfg, struct server *scfg) +server_check_running(struct cfg *cfg, struct server *server) { /* FIXME: other methods, rcon? */ - if (scfg->systemd_service) { - verbose("%s: checking if systemd service is running", scfg->name); - if (systemd_service_running(cfg, scfg)) { - scfg->state = SERVER_STATE_RUNNING; + if (server->systemd_service) { + verbose("%s: checking if systemd service is running", server->name); + if (systemd_service_running(cfg, server)) { + server->state = SERVER_STATE_RUNNING; return true; } else { - scfg->state = SERVER_STATE_STOPPED; + server->state = SERVER_STATE_STOPPED; return false; } } @@ -377,29 +377,29 @@ server_check_running(struct cfg *cfg, struct server *scfg) } bool -server_start(struct cfg *cfg, struct server *scfg) +server_start(struct cfg *cfg, struct server *server) { - if (!cfg || !scfg) + if (!cfg || !server) return false; - if (scfg->task.dead) + if (server->task.dead) return false; - switch (scfg->start_method) { + switch (server->start_method) { case SERVER_START_METHOD_EXEC: - verbose("Starting server %s via external cmd", scfg->name); - return server_exec(cfg, scfg, scfg->start_exec); + verbose("Starting server %s via external cmd", server->name); + return server_exec(cfg, server, server->start_exec); case SERVER_START_METHOD_SYSTEMD: verbose("Starting server %s via systemd (%s)", - scfg->name, scfg->systemd_service); + server->name, server->systemd_service); - if (systemd_service_start(cfg, scfg)) { - scfg->state = SERVER_STATE_RUNNING; + if (systemd_service_start(cfg, server)) { + server->state = SERVER_STATE_RUNNING; return true; } else - return server_check_running(cfg, scfg); + return server_check_running(cfg, server); case SERVER_START_METHOD_UNDEFINED: default: @@ -410,32 +410,32 @@ server_start(struct cfg *cfg, struct server *scfg) } bool -server_stop(struct cfg *cfg, struct server *scfg) +server_stop(struct cfg *cfg, struct server *server) { - if (!cfg || !scfg) + if (!cfg || !server) return false; - if (scfg->task.dead) + if (server->task.dead) return false; - switch (scfg->stop_method) { + switch (server->stop_method) { case SERVER_STOP_METHOD_EXEC: - verbose("Stopping server %s via external cmd", scfg->name); - return server_exec(cfg, scfg, scfg->stop_exec); + verbose("Stopping server %s via external cmd", server->name); + return server_exec(cfg, server, server->stop_exec); case SERVER_STOP_METHOD_SYSTEMD: verbose("Stopping server %s via systemd (%s)", - scfg->name, scfg->systemd_service); - if (systemd_service_stop(cfg, scfg)) { - scfg->state = SERVER_STATE_STOPPED; + server->name, server->systemd_service); + if (systemd_service_stop(cfg, server)) { + server->state = SERVER_STATE_STOPPED; return true; } else - return server_check_running(cfg, scfg); + return server_check_running(cfg, server); case SERVER_STOP_METHOD_RCON: - verbose("Stopping server %s via rcon", scfg->name); - rcon_init(cfg, scfg); + verbose("Stopping server %s via rcon", server->name); + rcon_init(cfg, server); return true; case SERVER_STOP_METHOD_UNDEFINED: @@ -447,198 +447,198 @@ server_stop(struct cfg *cfg, struct server *scfg) } bool -server_commit(struct cfg *cfg, struct server *scfg) +server_commit(struct cfg *cfg, struct server *server) { struct server_local *local; uint16_t port; - if (!scfg || !scfg->name) { + if (!server || !server->name) { error("called with invalid parameters"); return false; } - if (scfg->task.dead) + if (server->task.dead) return false; - if (scfg->state != SERVER_STATE_INIT) { + if (server->state != SERVER_STATE_INIT) { error("called in wrong state"); return false; } - if (!list_empty(&scfg->proxys)) { - error("%s: proxys not empty?", scfg->name); + if (!list_empty(&server->proxys)) { + error("%s: proxys not empty?", server->name); return false; } - if (!list_empty(&scfg->dnslookups)) { + if (!list_empty(&server->dnslookups)) { debug(DBG_SRV, "called with pending DNS requests"); return true; } - if (scfg->stop_method == SERVER_STOP_METHOD_RCON && - list_empty(&scfg->rcons)) { + if (server->stop_method == SERVER_STOP_METHOD_RCON && + list_empty(&server->rcons)) { error("%s: rcon stop method missing rcon address", - scfg->name); + server->name); return false; } - if (scfg->stop_method == SERVER_STOP_METHOD_RCON && - !scfg->rcon_password) { + if (server->stop_method == SERVER_STOP_METHOD_RCON && + !server->rcon_password) { error("%s: rcon stop method missing rcon password", - scfg->name); + server->name); return false; } - if ((scfg->start_method == SERVER_START_METHOD_SYSTEMD || - scfg->stop_method == SERVER_STOP_METHOD_SYSTEMD) && - !scfg->systemd_service) { + if ((server->start_method == SERVER_START_METHOD_SYSTEMD || + server->stop_method == SERVER_STOP_METHOD_SYSTEMD) && + !server->systemd_service) { error("%s: systemd start/stop method missing systemd service", - scfg->name); + server->name); return false; } - if (scfg->systemd_service && !scfg->systemd_obj) { - scfg->systemd_obj = systemd_service_object_path(cfg, - scfg->systemd_service); - if (!scfg->systemd_obj) { + if (server->systemd_service && !server->systemd_obj) { + server->systemd_obj = systemd_service_object_path(cfg, + server->systemd_service); + if (!server->systemd_obj) { error("%s: failed to create systemd object path (%s)", - scfg->name, scfg->systemd_service); + server->name, server->systemd_service); return false; } } - if (scfg->idle_timeout > 0 && - scfg->stop_method == SERVER_STOP_METHOD_UNDEFINED) { - error("%s: idle_timeout set but missing stop method", scfg->name); + if (server->idle_timeout > 0 && + server->stop_method == SERVER_STOP_METHOD_UNDEFINED) { + error("%s: idle_timeout set but missing stop method", server->name); return false; } - switch (scfg->type) { + switch (server->type) { case SERVER_TYPE_ANNOUNCE: - if (scfg->announce_port < 1) { - error("%s: missing announce port", scfg->name); + if (server->announce_port < 1) { + error("%s: missing announce port", server->name); return false; } - if (scfg->start_method != SERVER_START_METHOD_UNDEFINED) { - error("%s: can't set start_method for announce server", scfg->name); + if (server->start_method != SERVER_START_METHOD_UNDEFINED) { + error("%s: can't set start_method for announce server", server->name); return false; } - if (!list_empty(&scfg->locals)) { - error("%s: can't set local addresses for announce server", scfg->name); + if (!list_empty(&server->locals)) { + error("%s: can't set local addresses for announce server", server->name); return false; } - if (!list_empty(&scfg->remotes)) { - error("%s: can't set remote addresses for announce server", scfg->name); + if (!list_empty(&server->remotes)) { + error("%s: can't set remote addresses for announce server", server->name); return false; } break; case SERVER_TYPE_PROXY: - if (scfg->announce_port >= 1) { - error("%s: can't set announce port for proxy server", scfg->name); + if (server->announce_port >= 1) { + error("%s: can't set announce port for proxy server", server->name); return false; } - if (list_empty(&scfg->locals)) { - error("%s: missing local addresses for proxy server", scfg->name); + if (list_empty(&server->locals)) { + error("%s: missing local addresses for proxy server", server->name); return false; } - if (list_empty(&scfg->remotes)) { - error("%s: missing remote addresses for proxy server", scfg->name); + if (list_empty(&server->remotes)) { + error("%s: missing remote addresses for proxy server", server->name); return false; } - list_for_each_entry(local, &scfg->locals, list) { + list_for_each_entry(local, &server->locals, list) { port = saddr_port(&local->local); if (port == 0) { - error("%s: invalid local port", scfg->name); + error("%s: invalid local port", server->name); return false; } - if (scfg->announce_port < 1) - scfg->announce_port = port; + if (server->announce_port < 1) + server->announce_port = port; - if (scfg->announce_port != port) { - error("%s: multiple local ports", scfg->name); + if (server->announce_port != port) { + error("%s: multiple local ports", server->name); return false; } } - if (scfg->announce_port < 1) { - error("%s: can't determine which port to announce", scfg->name); + if (server->announce_port < 1) { + error("%s: can't determine which port to announce", server->name); return false; } break; default: - error("%s: can't determine server type", scfg->name); + error("%s: can't determine server type", server->name); return false; } - if (!scfg->pretty_name) { + if (!server->pretty_name) { char *suffix; - suffix = strrchr(scfg->name, '.'); - if (!suffix || suffix == scfg->name) { - error("invalid server name: %s", scfg->name); + suffix = strrchr(server->name, '.'); + if (!suffix || suffix == server->name) { + error("invalid server name: %s", server->name); return false; } - scfg->pretty_name = xstrndup(scfg->name, suffix - scfg->name); - if (!scfg->pretty_name) { - error("failed to create display name: %s", scfg->name); + server->pretty_name = xstrndup(server->name, suffix - server->name); + if (!server->pretty_name) { + error("failed to create display name: %s", server->name); return false; } } /* FIXME: config, dont reread config if server running, make sure fd is available before this is called */ - server_dump(scfg); + server_dump(server); - list_for_each_entry(local, &scfg->locals, list) { - server_local_open(cfg, scfg, local); + list_for_each_entry(local, &server->locals, list) { + server_local_open(cfg, server, local); } - idle_init(cfg, scfg); - scfg->state = SERVER_STATE_CFG_OK; + idle_init(cfg, server); + server->state = SERVER_STATE_CFG_OK; - server_check_running(cfg, scfg); + server_check_running(cfg, server); debug(DBG_SRV, "success"); return true; } bool -server_add_remote(struct cfg *cfg, struct server *scfg, struct saddr *remote) +server_add_remote(struct cfg *cfg, struct server *server, struct saddr *remote) { - if (!scfg || !remote) + if (!server || !remote) return false; - if (scfg->task.dead) + if (server->task.dead) return false; debug(DBG_SRV, "adding remote: %s", remote->addrstr); - list_add(&remote->list, &scfg->remotes); + list_add(&remote->list, &server->remotes); return true; } bool -server_add_local(struct cfg *cfg, struct server *scfg, struct saddr *saddr) +server_add_local(struct cfg *cfg, struct server *server, struct saddr *saddr) { struct server_local *local; - if (!scfg || !saddr) { + if (!server || !saddr) { error("missing arguments"); return false; } - if (scfg->task.dead) + if (server->task.dead) return false; local = zmalloc(sizeof(*local)); @@ -648,43 +648,43 @@ server_add_local(struct cfg *cfg, struct server *scfg, struct saddr *saddr) } local->local = *saddr; - uring_task_init(&local->task, "local", &scfg->task, server_local_free); + uring_task_init(&local->task, "local", &server->task, server_local_free); debug(DBG_SRV, "adding local: %s", saddr->addrstr); - list_add(&local->list, &scfg->locals); + list_add(&local->list, &server->locals); xfree(saddr); return true; } bool -server_add_rcon(struct cfg *cfg, struct server *scfg, struct saddr *rcon) +server_add_rcon(struct cfg *cfg, struct server *server, struct saddr *rcon) { - if (!scfg || !rcon) + if (!server || !rcon) return false; - if (scfg->task.dead) + if (server->task.dead) return false; debug(DBG_SRV, "adding rcon: %s", rcon->addrstr); - list_add(&rcon->list, &scfg->rcons); + list_add(&rcon->list, &server->rcons); return true; } bool -server_set_rcon_password(struct cfg *cfg, struct server *scfg, +server_set_rcon_password(struct cfg *cfg, struct server *server, const char *password) { - return set_property(cfg, scfg, &scfg->rcon_password, password); + return set_property(cfg, server, &server->rcon_password, password); } bool -server_set_systemd_service(struct cfg *cfg, struct server *scfg, +server_set_systemd_service(struct cfg *cfg, struct server *server, const char *service) { const char *suffix; char *tmp; - if (!cfg || !scfg || empty_str(service) || scfg->systemd_service) + if (!cfg || !server || empty_str(service) || server->systemd_service) return false; suffix = strrchr(service, '.'); @@ -700,78 +700,78 @@ server_set_systemd_service(struct cfg *cfg, struct server *scfg, return false; } - scfg->systemd_service = tmp; + server->systemd_service = tmp; return true; } bool -server_set_stop_method(struct cfg *cfg, struct server *scfg, +server_set_stop_method(struct cfg *cfg, struct server *server, enum server_stop_method stop_method) { - if (scfg->stop_method != SERVER_STOP_METHOD_UNDEFINED || + if (server->stop_method != SERVER_STOP_METHOD_UNDEFINED || stop_method == SERVER_STOP_METHOD_UNDEFINED) return false; - scfg->stop_method = stop_method; + server->stop_method = stop_method; return true; } bool -server_set_start_method(struct cfg *cfg, struct server *scfg, +server_set_start_method(struct cfg *cfg, struct server *server, enum server_start_method start_method) { - if (scfg->start_method != SERVER_START_METHOD_UNDEFINED || + if (server->start_method != SERVER_START_METHOD_UNDEFINED || start_method == SERVER_START_METHOD_UNDEFINED) return false; - scfg->start_method = start_method; + server->start_method = start_method; return true; } bool -server_set_stop_exec(struct cfg *cfg, struct server *scfg, const char *cmd) +server_set_stop_exec(struct cfg *cfg, struct server *server, const char *cmd) { - return set_property(cfg, scfg, &scfg->stop_exec, cmd); + return set_property(cfg, server, &server->stop_exec, cmd); } bool -server_set_start_exec(struct cfg *cfg, struct server *scfg, const char *cmd) +server_set_start_exec(struct cfg *cfg, struct server *server, const char *cmd) { - return set_property(cfg, scfg, &scfg->start_exec, cmd); + return set_property(cfg, server, &server->start_exec, cmd); } bool -server_set_idle_timeout(struct cfg *cfg, struct server *scfg, uint16_t timeout) +server_set_idle_timeout(struct cfg *cfg, struct server *server, uint16_t timeout) { - if (!scfg || scfg->idle_timeout != 0) + if (!server || server->idle_timeout != 0) return false; - scfg->idle_timeout = timeout; + server->idle_timeout = timeout; return true; } bool -server_set_port(struct cfg *cfg, struct server *scfg, uint16_t port) +server_set_port(struct cfg *cfg, struct server *server, uint16_t port) { - if (!scfg || scfg->announce_port != 0) + if (!server || server->announce_port != 0) return false; - scfg->announce_port = htons(port); + server->announce_port = htons(port); return true; } bool -server_set_type(struct cfg *cfg, struct server *scfg, enum server_type type) +server_set_type(struct cfg *cfg, struct server *server, enum server_type type) { - if (!scfg || scfg->type != SERVER_TYPE_UNDEFINED) + if (!server || server->type != SERVER_TYPE_UNDEFINED) return false; switch (type) { case SERVER_TYPE_ANNOUNCE: - scfg->type = SERVER_TYPE_ANNOUNCE; + server->type = SERVER_TYPE_ANNOUNCE; break; case SERVER_TYPE_PROXY: - scfg->type = SERVER_TYPE_PROXY; + server->type = SERVER_TYPE_PROXY; break; default: return false; @@ -781,46 +781,46 @@ server_set_type(struct cfg *cfg, struct server *scfg, enum server_type type) } bool -server_set_pretty_name(struct cfg *cfg, struct server *scfg, const char *pretty_name) +server_set_pretty_name(struct cfg *cfg, struct server *server, const char *pretty_name) { - return set_property(cfg, scfg, &scfg->pretty_name, pretty_name); + return set_property(cfg, server, &server->pretty_name, pretty_name); } struct server * server_new(struct cfg *cfg, const char *name) { - struct server *scfg; + struct server *server; - list_for_each_entry(scfg, &cfg->servers, list) { - if (strcmp(name, scfg->name)) + list_for_each_entry(server, &cfg->servers, list) { + if (strcmp(name, server->name)) continue; error("attempt to add duplicate server: %s", name); - return scfg; + return server; } verbose("Adding server %s", name); - scfg = zmalloc(sizeof(*scfg)); - if (!scfg) { + server = zmalloc(sizeof(*server)); + if (!server) { error("malloc"); return NULL; } - scfg->state = SERVER_STATE_INIT; - scfg->cfg = cfg; - scfg->type = SERVER_TYPE_UNDEFINED; - scfg->name = xstrdup(name); - scfg->stop_method = SERVER_STOP_METHOD_UNDEFINED; - scfg->start_method = SERVER_START_METHOD_UNDEFINED; - uring_task_init(&scfg->task, "scfg", uring_parent(cfg), server_free); - uring_task_set_buf(&scfg->task, &scfg->tbuf); - uring_task_init(&scfg->exec_task, "exec", &scfg->task, server_exec_free); - list_init(&scfg->remotes); - list_init(&scfg->locals); - list_init(&scfg->proxys); - list_init(&scfg->rcons); - list_init(&scfg->dnslookups); - scfg->idle_timeout = 0; - list_add(&scfg->list, &cfg->servers); - - return scfg; + 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; + uring_task_init(&server->task, "server", uring_parent(cfg), server_free); + uring_task_set_buf(&server->task, &server->tbuf); + uring_task_init(&server->exec_task, "exec", &server->task, server_exec_free); + list_init(&server->remotes); + list_init(&server->locals); + list_init(&server->proxys); + list_init(&server->rcons); + list_init(&server->dnslookups); + server->idle_timeout = 0; + list_add(&server->list, &cfg->servers); + + return server; } |