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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
|
#define _GNU_SOURCE
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <inttypes.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sched.h>
#include <poll.h>
#include <errno.h>
#include <sched.h>
#include "main.h"
#include "uring.h"
#include "server.h"
#include "server-proxy.h"
#include "utils.h"
#include "config-parser.h"
#include "idle.h"
#include "rcon.h"
#include "systemd.h"
static bool
set_property(struct server *server, char **property, const char *value)
{
assert_return(server && !*property && !empty_str(value), false);
*property = xstrdup(value);
if (!*property) {
error("strdup: %m");
return false;
}
return true;
}
void
server_refdump(struct server *server)
{
struct server_local *local;
struct server_proxy *proxy;
assert_return(server);
uring_task_refdump(&server->task);
uring_task_refdump(&server->exec_task);
uring_task_refdump(&server->ann_task);
uring_task_refdump(&server->idle_task);
list_for_each_entry(local, &server->locals, list)
local_refdump(local);
list_for_each_entry(proxy, &server->proxys, list)
proxy_refdump(proxy);
rcon_refdump(server->rcon);
}
static void
server_free(struct uring_task *task)
{
struct server *server = container_of(task, struct server, task);
assert_return(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 server *server)
{
struct server_local *local, *ltmp;
struct server_proxy *proxy, *ptmp;
struct saddr *remote;
struct saddr *rcon;
struct saddr *tmp;
struct dns_async *dns, *dtmp;
assert_return(server);
verbose("Removing server %s", server->name);
server->state = SERVER_STATE_DEAD;
rcon_delete(server);
list_for_each_entry_safe(local, ltmp, &server->locals, list)
local_delete(local);
list_for_each_entry_safe(proxy, ptmp, &server->proxys, list)
proxy_delete(proxy);
list_for_each_entry_safe(rcon, tmp, &server->rcons, list) {
list_del(&rcon->list);
xfree(rcon);
}
list_for_each_entry_safe(remote, tmp, &server->remotes, list) {
list_del(&remote->list);
xfree(remote);
}
list_for_each_entry_safe(dns, dtmp, &server->dnslookups, list)
gai_cancel(&dns->gcb);
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(const char *name)
{
struct server *server;
assert_return(!empty_str(name));
list_for_each_entry(server, &cfg->servers, list) {
if (streq(server->name, name)) {
server_delete(server);
return;
}
}
}
static void
server_dump(struct server *server)
{
struct server_local *local;
struct saddr *remote;
struct saddr *rcon;
assert_return(server);
verbose("Server %s:", server->name);
switch (server->type) {
case SERVER_TYPE_ANNOUNCE:
verbose(" * Type: announce");
break;
case SERVER_TYPE_PROXY:
verbose(" * Type: proxy");
break;
default:
verbose(" * Type: unknown");
break;
}
verbose(" * Name: %s", server->pretty_name ? server->pretty_name : "<undefined>");
verbose(" * Announce port: %" PRIu16, server->announce_port);
if (!list_empty(&server->locals)) {
verbose(" * Local:");
list_for_each_entry(local, &server->locals, list)
verbose(" * %s", local->local.addrstr);
}
if (!list_empty(&server->remotes)) {
verbose(" * Remote:");
list_for_each_entry(remote, &server->remotes, list)
verbose(" * %s", remote->addrstr);
}
if (!list_empty(&server->rcons)) {
verbose(" * RCon:");
list_for_each_entry(rcon, &server->rcons, list)
verbose(" * %s", rcon->addrstr);
}
verbose("");
}
static void
server_exec_free(struct uring_task *task)
{
assert_return(task);
debug(DBG_SRV, "called");
}
#ifndef P_PIDFD
#define P_PIDFD 3
#endif
/* FIXME: update states */
static void
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(task);
assert_task_alive_or(DBG_SRV, task, goto out);
/* Should we leave child processes running? */
if (!(res & POLLIN)) {
error("unexpected result: %i", res);
goto out;
}
r = waitid(P_PIDFD, server->exec_task.fd, &info, WEXITED);
if (r < 0) {
error("waitid: %m");
goto out;
}
if (info.si_status == 0)
debug(DBG_SRV, "command successfully executed");
else
error("command failed: %i", info.si_status);
out:
uring_task_close_fd(&server->exec_task);
}
static int
server_exec_child(void *ptr)
{
const char *cmd = ptr;
assert_return(ptr, EINVAL);
execl(cmd, cmd, NULL);
return errno;
}
#ifndef CLONE_PIDFD
#define CLONE_PIDFD 0x00001000
#endif
static bool
server_exec(struct server *server, const char *cmd)
{
char stack[4096]; /* Beautiful/horrible hack :) */
int pidfd;
int r;
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,
(void *)cmd, &pidfd);
if (r < 0) {
error("clone: %m: %i", r);
return false;
}
uring_task_set_fd(&server->exec_task, pidfd);
uring_poll(&server->exec_task, POLLIN, server_exec_done);
return true;
}
static bool
server_check_running(struct server *server)
{
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(server)) {
server->state = SERVER_STATE_RUNNING;
return true;
} else {
server->state = SERVER_STATE_STOPPED;
return false;
}
}
return false;
}
bool
server_start(struct server *server)
{
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(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(server)) {
server->state = SERVER_STATE_RUNNING;
return true;
} else
return server_check_running(server);
case SERVER_START_METHOD_UNDEFINED:
default:
break;
}
return false;
}
bool
server_stop(struct server *server)
{
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(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(server)) {
server->state = SERVER_STATE_STOPPED;
return true;
} else
return server_check_running(server);
case SERVER_STOP_METHOD_RCON:
verbose("Stopping server %s via rcon", server->name);
rcon_init(server);
return true;
case SERVER_STOP_METHOD_UNDEFINED:
default:
break;
}
return false;
}
static void
server_idle_free(struct uring_task *task)
{
assert_return(task);
debug(DBG_ANN, "called");
}
void
server_set_active_players(struct server *server, int count)
{
assert_return(server);
assert_task_alive(DBG_IDLE, &server->idle_task);
debug(DBG_IDLE, "%s: currently %i active players",
server->name, count);
if (count < 0)
return;
server->state = SERVER_STATE_RUNNING;
if (count > 0)
server->idle_count = 0;
else if (count == 0)
server->idle_count++;
if (server->idle_count > server->idle_timeout) {
verbose("stopping idle server %s", server->name);
server_stop(server);
}
}
static void
server_idle_connected_cb(struct connection *conn, bool connected)
{
struct server *server = container_of(conn, struct server, idle_conn);
assert_return(conn);
assert_task_alive(DBG_IDLE, &server->idle_task);
if (!connected) {
debug(DBG_IDLE,
"idle check connection to remote server (%s) failed",
server->name);
server->idle_count = 0;
server->state = SERVER_STATE_STOPPED;
return;
}
debug(DBG_IDLE, "connected to remote %s\n", conn->remote.addrstr);
idle_check_get_player_count(server, conn);
}
bool
server_idle_check(struct server *server)
{
assert_return(server, false);
if (server->state == SERVER_STATE_INIT ||
server->state == SERVER_STATE_DEAD)
return false;
if (server->idle_timeout < 1)
return false;
if (list_empty(&server->remotes))
return false;
if (!list_empty(&server->proxys)) {
server->idle_count = 0;
return true;
}
connect_any(&server->idle_task, &server->remotes,
&server->idle_conn, server_idle_connected_cb);
return true;
}
static void
server_announce_free(struct uring_task *task)
{
assert_return(task);
debug(DBG_ANN, "called");
}
static void
server_announce_cb(struct uring_task *task, int res)
{
struct server *server = container_of(task, struct server, ann_task);
assert_return(task);
if (res < 0)
error("%s: failure %i", server->name, res);
else if (res == server->ann_buf.len)
debug(DBG_ANN, "%s: ok (%i)", server->name, res);
else
debug(DBG_ANN, "%s: unexpected result: %i", server->name, res);
uring_task_set_fd(&server->ann_task, -1);
}
bool
server_announce(struct server *server, int fd)
{
assert_return(server && fd >= 0, false);
if (server->state == SERVER_STATE_INIT ||
server->state == SERVER_STATE_DEAD)
return false;
debug(DBG_ANN, "announcing server: %s", server->name);
uring_task_set_fd(&server->ann_task, fd);
uring_tbuf_sendmsg(&server->ann_task, server_announce_cb);
return true;
}
bool
server_commit(struct server *server)
{
struct server_local *local;
uint16_t port;
int r;
assert_return(server && server->name, false);
assert_task_alive_or(DBG_SRV, &server->task, return false);
if (server->state != SERVER_STATE_INIT) {
error("called in wrong state");
return false;
}
if (!list_empty(&server->proxys)) {
error("%s: proxys not empty?", server->name);
return false;
}
if (!list_empty(&server->dnslookups)) {
debug(DBG_SRV, "called with pending DNS requests");
return true;
}
if (server->stop_method == SERVER_STOP_METHOD_RCON &&
list_empty(&server->rcons)) {
error("%s: rcon stop method missing rcon address",
server->name);
return false;
}
if (server->stop_method == SERVER_STOP_METHOD_RCON &&
!server->rcon_password) {
error("%s: rcon stop method missing rcon password",
server->name);
return false;
}
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",
server->name);
return false;
}
if (server->systemd_service && !server->systemd_obj) {
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);
return false;
}
}
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 (server->type) {
case SERVER_TYPE_ANNOUNCE:
if (server->announce_port < 1) {
error("%s: missing announce port", server->name);
return false;
}
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(&server->locals)) {
error("%s: can't set local addresses for announce server", server->name);
return false;
}
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 (server->announce_port >= 1) {
error("%s: can't set announce port for proxy server", server->name);
return false;
}
if (list_empty(&server->locals)) {
error("%s: missing local addresses for proxy server", server->name);
return false;
}
if (list_empty(&server->remotes)) {
error("%s: missing remote addresses for proxy server", server->name);
return false;
}
list_for_each_entry(local, &server->locals, list) {
port = saddr_port(&local->local);
if (port == 0) {
error("%s: invalid local port", server->name);
return false;
}
if (server->announce_port < 1)
server->announce_port = port;
if (server->announce_port != port) {
error("%s: multiple local ports", server->name);
return false;
}
}
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", server->name);
return false;
}
if (!server->pretty_name) {
char *suffix;
suffix = strrchr(server->name, '.');
if (!suffix || suffix == server->name) {
error("invalid server name: %s", server->name);
return false;
}
server->pretty_name = xstrndup(server->name, suffix - server->name);
if (!server->pretty_name) {
error("failed to create display name: %s", server->name);
return false;
}
}
r = snprintf(server->ann_buf.buf, sizeof(server->ann_buf.buf),
"[MOTD]%s[/MOTD][AD]%" PRIu16 "[/AD]",
server->pretty_name, server->announce_port);
if (r < 1 || r >= sizeof(server->ann_buf.buf)) {
error("%s: unable to create announce msg: %i\n", server->name, r);
return false;
}
server->ann_buf.len = r;
/* FIXME: config, dont reread config if server running, make sure fd is available before this is called */
server_dump(server);
list_for_each_entry(local, &server->locals, list)
local_open(local);
server->state = SERVER_STATE_CFG_OK;
server_check_running(server);
debug(DBG_SRV, "success");
return true;
}
bool
server_add_remote(struct server *server, struct saddr *remote)
{
assert_return(server && remote, false);
assert_task_alive_or(DBG_SRV, &server->task, return false);
debug(DBG_SRV, "adding remote: %s", remote->addrstr);
list_add(&remote->list, &server->remotes);
return true;
}
bool
server_add_local(struct server *server, struct saddr *saddr)
{
struct server_local *local;
assert_return(server && saddr, false);
assert_task_alive_or(DBG_SRV, &server->task, return false);
local = local_new(server, saddr);
if (!local)
return false;
list_add(&local->list, &server->locals);
return true;
}
bool
server_add_rcon(struct server *server, struct saddr *rcon)
{
assert_return(server && rcon, false);
assert_task_alive_or(DBG_SRV, &server->task, return false);
debug(DBG_SRV, "adding rcon: %s", rcon->addrstr);
list_add(&rcon->list, &server->rcons);
return true;
}
bool
server_set_rcon_password(struct server *server, const char *password)
{
assert_return(server && !empty_str(password), false);
return set_property(server, &server->rcon_password, password);
}
bool
server_set_systemd_service(struct server *server, const char *service)
{
const char *suffix;
char *tmp;
assert_return(server && !empty_str(service) && !server->systemd_service, false);
suffix = strrchr(service, '.');
if (!suffix || !streq(suffix, ".service")) {
tmp = zmalloc(strlen(service) + strlen(".service") + 1);
if (tmp)
sprintf(tmp, "%s.service", service);
} else
tmp = xstrdup(service);
if (!tmp) {
error("malloc/strdup: %m");
return false;
}
server->systemd_service = tmp;
return true;
}
bool
server_set_stop_method(struct server *server,
enum server_stop_method stop_method)
{
assert_return(server->stop_method == SERVER_STOP_METHOD_UNDEFINED &&
stop_method != SERVER_STOP_METHOD_UNDEFINED, false);
server->stop_method = stop_method;
return true;
}
bool
server_set_start_method(struct server *server,
enum server_start_method start_method)
{
assert_return(server->start_method == SERVER_START_METHOD_UNDEFINED &&
start_method != SERVER_START_METHOD_UNDEFINED, false);
server->start_method = start_method;
return true;
}
bool
server_set_stop_exec(struct server *server, const char *cmd)
{
assert_return(server && !empty_str(cmd), false);
return set_property(server, &server->stop_exec, cmd);
}
bool
server_set_start_exec(struct server *server, const char *cmd)
{
assert_return(server && !empty_str(cmd), false);
return set_property(server, &server->start_exec, cmd);
}
bool
server_set_idle_timeout(struct server *server, uint16_t timeout)
{
assert_return(server && timeout > 0 && server->idle_timeout == 0, false);
server->idle_timeout = timeout;
return true;
}
bool
server_set_port(struct server *server, uint16_t port)
{
assert_return(server && port > 0 && server->announce_port == 0, false);
server->announce_port = htons(port);
return true;
}
bool
server_set_type(struct server *server, enum server_type type)
{
assert_return(server && type != SERVER_TYPE_UNDEFINED, false);
switch (type) {
case SERVER_TYPE_ANNOUNCE:
server->type = SERVER_TYPE_ANNOUNCE;
break;
case SERVER_TYPE_PROXY:
server->type = SERVER_TYPE_PROXY;
break;
default:
return false;
}
return true;
}
bool
server_set_pretty_name(struct server *server, const char *pretty_name)
{
assert_return(server && !empty_str(pretty_name), false);
return set_property(server, &server->pretty_name, pretty_name);
}
struct server *
server_new(const char *name)
{
struct server *server;
assert_return(!empty_str(name), NULL);
list_for_each_entry(server, &cfg->servers, list) {
if (!streq(name, server->name))
continue;
error("attempt to add duplicate server: %s", name);
return server;
}
verbose("Adding server %s", name);
server = zmalloc(sizeof(*server));
if (!server) {
error("malloc: %m");
return NULL;
}
server->state = SERVER_STATE_INIT;
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(&server->task, server->name, uring_parent(), server_free);
uring_task_set_buf(&server->task, &server->tbuf);
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(&server->exec_task, "exec", &server->task, server_exec_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);
list_init(&server->locals);
list_init(&server->proxys);
list_init(&server->rcons);
list_init(&server->dnslookups);
list_add(&server->list, &cfg->servers);
return server;
}
|