-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.c
68 lines (65 loc) · 1.62 KB
/
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "hypercube.h"
extern hypercube_config cfg;
void mainloop(void)
{
DEBUGF("entering mainloop");
while (1)
{
net_wait_for_events();
#ifdef SOCKET_TIMEOUT
net_periodic();
#endif
tracker_periodic();
}
}
void handle_fatal_sig(int sig)
{
aklogf(LOG_ERROR, "Exiting on signal %d.", sig);
exit(sig);
}
void handle_reload_sig(int sig)
{
aklogf(LOG_INFO, "Got signal %d, reloading config...", sig);
cfg_reload();
}
void final_init(void)
{
struct stat ost, st;
if (stat("/", &ost) != 0) { perror("Couldn't stat /"); exit(1); }
if (cfg.chroot_dir != NULL && (chroot(cfg.chroot_dir) != 0 || chdir("/") != 0)) { perror("Changing root directory"); exit(1); }
if (stat(cfg.default_root, &st) == 0)
{
if (st.st_ino == ost.st_ino)
{
fprintf(stderr, "Refusing to start with filesystem root as default root.\n");
exit(1);
}
} else
{
fprintf(stderr, "Warning: Couldn't access default root (%s): %s\n", cfg.default_root, strerror(errno));
}
if (cfg.run_as_gid != -1 && setgid(cfg.run_as_gid) != 0) { perror("Changing GID"); exit(1); }
if (cfg.run_as_uid != -1 && setuid(cfg.run_as_uid) != 0) { perror("Changing UID"); exit(1); }
if (cfg.background == 1)
{
fclose(stdin); fclose(stdout); fclose(stderr);
if (fork() != 0) exit(0);
}
signal(SIGPIPE, SIG_IGN);
signal(SIGCHLD, SIG_IGN);
signal(SIGTERM, handle_fatal_sig);
signal(SIGINT, handle_fatal_sig);
signal(SIGHUP, handle_reload_sig);
}
int main(int argc, char *argv[])
{
cfg_init();
log_init();
net_init();
http_init();
tracker_init();
net_start_listen();
final_init();
mainloop();
return 0;
}