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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
/* SPDX-License-Identifier: GPL-2.0 */
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "shared/utils.h"
#include "minecctl.h"
#include "server.h"
#include "misc.h"
#define INVALID(msg) do { *error = (msg); return false; } while(0)
bool server_read_config(struct cfg *cfg, struct server *server,
unsigned *lineno, const char **error)
{
char buf[4096];
size_t off = 0;
ssize_t r;
int dfd;
int fd;
if (!error)
return false;
if (!server)
INVALID("Invalid argument, server missing");
if (!lineno)
INVALID("Invalid argument, lineno not set");
if (!server->scfg.filename)
INVALID("Invalid argument, filename not set");
if (server->file_read)
return true;
*lineno = 0;
server->file_read = true;
dfd = open(cfg->cfgdir, O_DIRECTORY | O_PATH | O_CLOEXEC);
if (dfd < 0)
INVALID("failed to open configuration directory");
fd = openat(dfd, server->scfg.filename, O_RDONLY | O_CLOEXEC);
if (fd < 0)
INVALID("failed to open configuration file");
close(dfd);
while (true) {
r = read(fd, buf + off, sizeof(buf) - off - 1);
if (r < 0)
INVALID("failed to read configuration file");
else if (r == 0)
break;
off += r;
if (off == sizeof(buf) - 1)
INVALID("invalid, file too large");
}
buf[off] = '\0';
close(fd);
if (!scfg_parse(&server->scfg, buf, NULL, lineno, error))
return false;
if (!server->scfg.rcon_password)
verbose("rcon password not set");
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);
return true;
}
struct server *server_get_default(struct cfg *cfg)
{
struct server *server;
unsigned lineno;
const char *error;
server = list_first_entry_or_null(&cfg->servers, struct server, list);
if (!server)
die("No servers defined");
if (!server_read_config(cfg, server, &lineno, &error)) {
error("server_read_config error: %s", error);
return NULL;
}
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 (!config_valid_server_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;
}
|