1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "utils.h"
#include "minecctl.h"
#include "server.h"
#include "misc.h"
void server_read_config(struct cfg *cfg, struct server *server)
{
char buf[4096];
size_t off = 0;
ssize_t r;
int dfd;
int fd;
if (!server || !server->scfg.filename || server->file_read)
return;
server->file_read = true;
dfd = open(cfg->cfgdir, O_DIRECTORY | O_PATH | O_CLOEXEC);
if (dfd < 0)
die("Failed to open %s: %m", cfg->cfgdir);
fd = openat(dfd, server->scfg.filename, O_RDONLY | O_CLOEXEC);
if (fd < 0)
die("Failed to open %s: %m", server->scfg.filename);
close(dfd);
while (true) {
r = read(fd, buf + off, sizeof(buf) - off - 1);
if (r < 0)
die("Failed to read %s: %m", server->scfg.filename);
else if (r == 0)
break;
off += r;
if (off == sizeof(buf) - 1)
die("Failed to read %s: file too large",
server->scfg.filename);
}
buf[off] = '\0';
close(fd);
if (!scfg_parse(&server->scfg, buf, NULL))
die("Unable to parse %s", server->scfg.filename);
if (!server->scfg.rcon_password)
verbose("rcon password not found in %s", server->scfg.filename);
if (list_empty(&server->scfg.rcons))
verbose("rcon address not found in %s", server->scfg.filename);
if (list_empty(&server->scfg.remotes))
verbose("mc server address not found in %s", server->scfg.filename);
}
struct server *server_get_default(struct cfg *cfg)
{
struct server *server;
server = list_first_entry_or_null(&cfg->servers, struct server, list);
if (!server)
die("No servers defined");
server_read_config(cfg, server);
return server;
}
bool server_set_default(struct cfg *cfg, const char *name)
{
struct server *server;
assert_die(cfg, "invalid arguments");
list_for_each_entry(server, &cfg->servers, list) {
if (streq(name, server->name)) {
list_rotate_to_front(&server->list, &cfg->servers);
return true;
}
}
return false;
}
void server_load_all_known(struct cfg *cfg)
{
struct dirent *dent;
DIR *dir;
dir = opendir(cfg->cfgdir);
if (!dir) {
info("Can't open config directory %s: %m", cfg->cfgdir);
return;
}
while ((dent = readdir(dir))) {
struct server *server;
char *suffix;
if (!is_valid_server_config_filename(dent, NULL))
continue;
server = server_new(dent->d_name);
suffix = strrchr(dent->d_name, '.');
assert_die(suffix, "Error parsing filename");
*suffix = '\0';
server->name = xstrdup(dent->d_name);
list_add(&server->list, &cfg->servers);
}
closedir(dir);
}
void server_free_all(struct cfg *cfg)
{
struct server *server, *tmp;
list_for_each_entry_safe(server, tmp, &cfg->servers, list)
server_free(server);
}
void server_free(struct server *server)
{
scfg_delete(&server->scfg);
xfree(server->name);
xfree(server);
}
struct server *server_new(const char *filename)
{
struct server *server;
server = zmalloc(sizeof(*server));
scfg_init(&server->scfg, filename);
INIT_LIST_HEAD(&server->list);
return server;
}
|