-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
49 lines (42 loc) · 1.07 KB
/
main.cpp
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
#include "optparse.h"
#include "webserver.h"
#include <cstdio>
#include <unistd.h>
#include <signal.h>
#include <memory.h>
#include <pthread.h>
extern pthread_cond_t g_condvar;
int main(int argc, char **argv) {
int ret = 0;
options opts;
ret = parse_args(argc, argv, opts);
if (ret < 0) {
show_usage(argv[0]);
return 1;
}
//Daemonize
if (opts.daemonize) {
printf("Info: Daemonize...\n");
daemon(1, 1);
}
//Process termination signals
sigset_t term_signals;
sigemptyset(&term_signals);
sigaddset(&term_signals, SIGINT);
sigaddset(&term_signals, SIGTERM);
struct sigaction act;
act.sa_handler = [](int signo) {
printf("Info: Got signal %s\n", strsignal(signo));
printf("Info: Trying to exit\n");
g_web_server_stop = true;
pthread_cond_broadcast(&g_condvar);
};
act.sa_mask = term_signals;
sigaction(SIGINT, &act, nullptr);
sigaction(SIGTERM, &act, nullptr);
//Main work loop
ret = web_server_run(opts);
if (ret < 0) {
return 1;
}
}