summaryrefslogtreecommitdiff
path: root/minecctl/misc-commands.c
diff options
context:
space:
mode:
Diffstat (limited to 'minecctl/misc-commands.c')
-rw-r--r--minecctl/misc-commands.c136
1 files changed, 62 insertions, 74 deletions
diff --git a/minecctl/misc-commands.c b/minecctl/misc-commands.c
index f3ddff2..8a54e64 100644
--- a/minecctl/misc-commands.c
+++ b/minecctl/misc-commands.c
@@ -21,11 +21,11 @@
#include "examples/eula.txt.h"
#include "examples/example.mcserver.h"
+#include "examples/minecctl.conf.h"
#include "examples/minecproxy.conf.h"
#include "examples/minecproxy.service.h"
#include "examples/minecserver@.service.h"
#include "examples/README.TXT.h"
-#include "examples/server.properties.h"
static bool create_link(int dfd, const char *source, const char *target)
{
@@ -99,7 +99,7 @@ static bool find_user_service(int xfd, const char *service)
return true;
}
- dfd = open_xdg_shared_dir();
+ dfd = open_xdg_data_dir(false);
if (dfd >= 0) {
if (faccessat(dfd, sub_path, R_OK, 0) == 0) {
info("User service %s already installed in "
@@ -142,105 +142,92 @@ static bool create_user_service(int xfd, const char *service,
return true;
}
-static bool create_server_cfg(int sfd, int cfd, const char *name,
+static bool create_server_cfg(struct cfg *cfg, const char *name,
const char *filename,
const unsigned char *properties,
size_t properties_len)
{
- _cleanup_close_ int efd = -1;
+ _cleanup_close_ int sfd = -1;
+ int cfd, dfd;
- efd = open_subdir(sfd, name, true);
- if (efd < 0) {
- error("Failed to create configuration directory \"%s\"", name);
+ if (!cfg->cfg_dir || !cfg->data_dir)
return false;
- }
- if (!write_cfg_file(efd, "eula.txt", ___examples_eula_txt,
- ___examples_eula_txt_len))
- return false;
-
- if (!write_cfg_file(efd, "server.properties",
- properties, properties_len))
- return false;
-
- if (!create_link(efd, "server.jar", "../server.jar"))
- return false;
+ cfd = dirfd(cfg->cfg_dir);
+ dfd = dirfd(cfg->data_dir);
if (!write_cfg_file(cfd, filename,
___examples_example_mcserver,
___examples_example_mcserver_len))
return false;
- return true;
-}
-
-static bool create_config_tree(int xfd)
-{
- _cleanup_close_ int mfd = -1;
- _cleanup_close_ int sfd = -1;
- _cleanup_close_ int cfd = -1;
-
- mfd = open_subdir(xfd, "minecproxy", true);
- if (mfd < 0)
- return false;
-
- sfd = open_subdir(mfd, "servers", true);
- if (sfd < 0)
- return false;
-
- if (!write_cfg_file(sfd, "README.TXT", ___examples_README_TXT,
- ___examples_README_TXT_len))
+ sfd = open_subdir(dfd, name, true);
+ if (sfd < 0) {
+ error("Failed to create server directory \"%s\"", name);
return false;
+ }
- cfd = open_subdir(mfd, "config", true);
- if (cfd < 0)
+ if (!write_cfg_file(sfd, "eula.txt", ___examples_eula_txt,
+ ___examples_eula_txt_len))
return false;
- if (!write_cfg_file(cfd, "minecproxy.conf", ___examples_minecproxy_conf,
- ___examples_minecproxy_conf_len))
+ if (!write_cfg_file(sfd, "server.properties",
+ properties, properties_len))
return false;
- if (!create_server_cfg(sfd, cfd, "example", "example.mcserver",
- ___examples_server_properties,
- ___examples_server_properties_len))
+ if (!create_link(sfd, "server.jar", "../server.jar"))
return false;
- info("Created configuration in $XDG_CONFIG_HOME/minecproxy");
return true;
}
-bool do_init(struct cfg *cfg)
+bool do_init(_unused_ struct cfg *cfg)
{
- _cleanup_close_ int xfd = -1;
+ _cleanup_close_ int xcfd = -1;
+ _cleanup_close_ int mcfd = -1;
+ _cleanup_close_ int xdfd = -1;
+ _cleanup_close_ int mdfd = -1;
- if (cfg->dir_path) {
- xfd = open(cfg->dir_path, O_PATH | O_CLOEXEC | O_DIRECTORY);
- if (xfd < 0)
- error("Failed to open dir %s: %m", cfg->dir_path);
- } else
- xfd = open_xdg_cfg_dir(true);
+ xcfd = open_xdg_cfg_dir(true);
+ if (xcfd < 0)
+ return false;
- if (xfd < 0)
+ mcfd = open_subdir(xcfd, "minecproxy", true);
+ if (mcfd < 0)
return false;
- if (!create_config_tree(xfd))
+ if (!write_cfg_file(mcfd, "minecproxy.conf",
+ ___examples_minecproxy_conf,
+ ___examples_minecproxy_conf_len))
return false;
- if (cfg->dir_path) {
- info("Option -c used, not creating systemd services");
- return true;
- }
+ if (!write_cfg_file(mcfd, "minecctl.conf",
+ ___examples_minecctl_conf,
+ ___examples_minecctl_conf_len))
+ return false;
- if (!create_user_service(xfd, "minecserver@.service",
+ if (!create_user_service(xcfd, "minecserver@.service",
___examples_minecserver__service,
___examples_minecserver__service_len))
return false;
- if (!create_user_service(xfd, "minecproxy.service",
+ if (!create_user_service(xcfd, "minecproxy.service",
___examples_minecproxy_service,
___examples_minecproxy_service_len))
return false;
+ xdfd = open_xdg_data_dir(true);
+ if (xdfd < 0)
+ return false;
+
+ mdfd = open_subdir(xdfd, "minecproxy", true);
+ if (mdfd < 0)
+ return false;
+
+ if (!write_cfg_file(mdfd, "README.TXT", ___examples_README_TXT,
+ ___examples_README_TXT_len))
+ return false;
+
return true;
}
@@ -390,12 +377,12 @@ static bool saddr_port_match(struct list_head *list, uint16_t port)
list_for_each_entry(a, list, list) {
switch (a->st.ss_family) {
case AF_INET:
- if (a->in4.sin_port == port)
+ if (htons(a->in4.sin_port) == port)
return true;
break;
case AF_INET6:
- if (a->in6.sin6_port == port)
+ if (htons(a->in6.sin6_port) == port)
return true;
break;
@@ -446,7 +433,9 @@ static bool select_free_ports(struct cfg *cfg, uint16_t *listen_port,
}
}
- if (!used ) {
+ if (!used) {
+ error("Found unused port, %" PRIu16, lport);
+
if (listen_port && *listen_port == 0)
*listen_port = lport;
@@ -470,7 +459,6 @@ bool do_new(struct cfg *cfg)
struct server *defserver = NULL;
_cleanup_free_ unsigned char *properties = NULL;
size_t properties_len;
- _cleanup_close_ int sfd = -1;
uint16_t rcon_port = 0, mc_port = 0;
const char *rcon_password = NULL;
@@ -480,9 +468,13 @@ bool do_new(struct cfg *cfg)
char filename[strlen(name) + STRLEN(".mcserver") + 1];
sprintf(filename, "%s.mcserver", name);
- server_load_all_known(cfg);
+ if (!server_read_all_configs(cfg, false)) {
+ error("Failed to read all existing server configurations, "
+ "try running the \"lint\" command.");
+ return false;
+ }
- if (!cfg->dir)
+ if (!cfg->cfg_dir || !cfg->data_dir)
return false;
list_for_each_entry(server, &cfg->servers, list) {
@@ -505,17 +497,13 @@ bool do_new(struct cfg *cfg)
} else if (cfg->rcon_password)
rcon_password = cfg->rcon_password;
+ dump_config(cfg);
+
if (!select_free_ports(cfg, NULL, &mc_port, &rcon_port)) {
error("Failed to find a free port");
return false;
}
- sfd = open_subdir(dirfd(cfg->dir), "../servers", false);
- if (sfd < 0) {
- error("Failed to open configuration directory \"servers\"");
- return false;
- }
-
properties = create_mc_properties(name, mc_port, rcon_port,
rcon_password, &properties_len);
if (!properties)
@@ -523,7 +511,7 @@ bool do_new(struct cfg *cfg)
error("Created config file (%zu):\n%s", properties_len, properties);
- if (!create_server_cfg(sfd, dirfd(cfg->dir), name, filename, properties,
+ if (!create_server_cfg(cfg, name, filename, properties,
properties_len))
return false;