/
main.c
48 lines (43 loc) · 1014 Bytes
/
main.c
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
#include <sys/signalfd.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include "amt.h"
#include "config.h"
#include "macro.h"
#include "server.h"
int setup_signalfd() {
sigset_t mask;
sigemptyset(&mask);
sigaddset(&mask, SIGINT);
sigaddset(&mask, SIGPIPE);
sigaddset(&mask, SIGTERM);
int rv = -1;
CHECK(SYSCALL(sigprocmask(SIG_BLOCK, &mask, NULL)),
perror("sigprocmask(SIG_BLOCK, ...) failed"), goto ret);
sigdelset(&mask, SIGPIPE);
CHECK(SYSCALL(rv = signalfd(-1, &mask, 0)),
perror("signalfd() failed"), goto ret);
ret:
return rv;
}
int main(int ac, char *av[]) {
int rv = 0;
int sfd = setup_signalfd();
struct config *config = parse_config("amtredird.ini");
if (config && validate_config(config)) {
if (init_amt(config)) {
if (sfd != -1) {
rv = (run_server(config, sfd) == 0);
} else {
rv = 1;
}
teardown_amt(config);
}
} else {
rv = 1;
}
close(sfd);
free_config(config);
return rv;
}