]> jfr.im git - irc/quakenet/newserv.git/blob - core/main.c
fix some format string errors and dergister some hooks, also do some (pointless)...
[irc/quakenet/newserv.git] / core / main.c
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"
8 #include "nsmalloc.h"
9
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <time.h>
13 #include <sys/time.h>
14 #include <signal.h>
15
16 void initseed();
17 void init_logfile();
18 void siginthandler(int sig);
19 void sigusr1handler(int sig);
20 void sigsegvhandler(int sig);
21 void sighuphandler(int sig);
22 void handlecore(void);
23 void handlesignals(void);
24
25 int newserv_shutdown_pending;
26 static int newserv_sigint_pending, newserv_sigusr1_pending, newserv_sighup_pending;
27 static void (*oldsegv)(int);
28
29 int main(int argc, char **argv) {
30 initseed();
31 inithooks();
32 inithandlers();
33 initschedule();
34
35 init_logfile();
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();
47 signal(SIGINT, siginthandler);
48 signal(SIGUSR1, sigusr1handler);
49 signal(SIGHUP, sighuphandler);
50 oldsegv = signal(SIGSEGV, sigsegvhandler);
51
52 /* Main loop */
53 for(;;) {
54 handleevents(10);
55 doscheduledevents(time(NULL));
56
57 if (newserv_shutdown_pending) {
58 newserv_shutdown();
59 break;
60 }
61
62 handlesignals();
63 }
64
65 freeconfig();
66 finisstring();
67
68 fini_logfile();
69 finischedule();
70 finihandlers();
71
72 nsexit();
73 }
74
75 void handlesignals(void) {
76 if (newserv_sigusr1_pending) {
77 signal(SIGUSR1, sigusr1handler);
78 Error("core", ERR_INFO, "SIGUSR1 received.");
79 triggerhook(HOOK_CORE_SIGUSR1, NULL);
80 newserv_sigusr1_pending=0;
81 }
82
83 if (newserv_sighup_pending) {
84 signal(SIGHUP, sighuphandler);
85 Error("core", ERR_INFO, "SIGHUP received, rehashing...");
86 triggerhook(HOOK_CORE_REHASH, (void *)1);
87 newserv_sighup_pending=0;
88 }
89
90 if (newserv_sigint_pending) {
91 Error("core", ERR_INFO, "SIGINT received, terminating.");
92 triggerhook(HOOK_CORE_SIGINT, NULL);
93 newserv_sigint_pending=0;
94 newserv_shutdown_pending=1;
95 }
96 }
97
98 /*
99 * seed the pseudo-random number generator, rand()
100 */
101 void initseed() {
102 struct timeval t;
103
104 gettimeofday(&t, NULL);
105 srand(t.tv_usec);
106 }
107
108 void siginthandler(int sig) {
109 newserv_sigint_pending = 1;
110 }
111
112 void sigusr1handler(int sig) {
113 newserv_sigusr1_pending = 1;
114 }
115
116 void sighuphandler(int sig) {
117 newserv_sighup_pending = 1;
118 }
119
120 void sigsegvhandler(int sig) {
121 handlecore();
122
123 oldsegv(sig);
124 }