]> jfr.im git - irc/quakenet/newserv.git/blame - core/main.c
treat ::ffff:0.0.0.0/96 as ipv4 0/0
[irc/quakenet/newserv.git] / core / main.c
CommitLineData
2c5db955
CP
1#include "../lib/sstring.h"
2#include "events.h"
3#include "schedule.h"
4#include "hooks.h"
5#include "modules.h"
6#include "config.h"
7#include "error.h"
010f1c74 8#include "nsmalloc.h"
2c5db955
CP
9
10#include <stdlib.h>
11#include <stdio.h>
12#include <time.h>
13#include <sys/time.h>
88c8f330 14#include <signal.h>
2c5db955
CP
15
16void initseed();
280505a5 17void init_logfile();
88c8f330 18void siginthandler(int sig);
b8e79109 19void sigusr1handler(int sig);
cdc37146 20void sigsegvhandler(int sig);
f69c0032 21void sighuphandler(int sig);
cdc37146 22void handlecore(void);
f69c0032 23void handlesignals(void);
2c5db955 24
f69c0032 25int newserv_shutdown_pending;
efa44d15 26static int newserv_sigint_pending, newserv_sigusr1_pending, newserv_sighup_pending;
506a636c
CP
27static void (*oldsegv)(int);
28
2c5db955
CP
29int main(int argc, char **argv) {
30 initseed();
31 inithooks();
32 inithandlers();
33 initschedule();
280505a5 34
35 init_logfile();
2c5db955
CP
36
37 initsstring();
38
39 if (argc>1) {
40 initconfig(argv[1]);
41 } else {
42 initconfig("newserv.conf");
43 }
44
45 /* Loading the modules will bring in the bulk of the code */
46 initmodules();
88c8f330 47 signal(SIGINT, siginthandler);
b8e79109 48 signal(SIGUSR1, sigusr1handler);
f69c0032 49 signal(SIGHUP, sighuphandler);
506a636c 50 oldsegv = signal(SIGSEGV, sigsegvhandler);
2c5db955
CP
51
52 /* Main loop */
53 for(;;) {
54 handleevents(10);
55 doscheduledevents(time(NULL));
f69c0032 56
83951d54 57 if (newserv_shutdown_pending) {
58 newserv_shutdown();
59 break;
60 }
b8e79109 61
f69c0032 62 handlesignals();
2c5db955 63 }
707c5824 64
707c5824
CP
65 freeconfig();
66 finisstring();
bd293217
P
67
68 nsexit();
2c5db955
CP
69}
70
f69c0032
CP
71void handlesignals(void) {
72 if (newserv_sigusr1_pending) {
73 signal(SIGUSR1, sigusr1handler);
74 Error("core", ERR_INFO, "SIGUSR1 received.");
75 triggerhook(HOOK_CORE_SIGUSR1, NULL);
76 newserv_sigusr1_pending=0;
77 }
78
79 if (newserv_sighup_pending) {
d21a13c0 80 signal(SIGHUP, sighuphandler);
f69c0032
CP
81 Error("core", ERR_INFO, "SIGHUP received, rehashing...");
82 triggerhook(HOOK_CORE_REHASH, (void *)1);
83 newserv_sighup_pending=0;
84 }
efa44d15
CP
85
86 if (newserv_sigint_pending) {
87 Error("core", ERR_INFO, "SIGINT received, terminating.");
88 triggerhook(HOOK_CORE_SIGINT, NULL);
89 newserv_sigint_pending=0;
90 newserv_shutdown_pending=1;
91 }
f69c0032
CP
92}
93
2c5db955
CP
94/*
95 * seed the pseudo-random number generator, rand()
96 */
97void initseed() {
98 struct timeval t;
99
100 gettimeofday(&t, NULL);
101 srand(t.tv_usec);
102}
88c8f330
CP
103
104void siginthandler(int sig) {
efa44d15 105 newserv_sigint_pending = 1;
88c8f330 106}
b8e79109 107
108void sigusr1handler(int sig) {
109 newserv_sigusr1_pending = 1;
110}
cdc37146 111
f69c0032
CP
112void sighuphandler(int sig) {
113 newserv_sighup_pending = 1;
114}
115
cdc37146
CP
116void sigsegvhandler(int sig) {
117 handlecore();
506a636c
CP
118
119 oldsegv(sig);
cdc37146 120}