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