]> jfr.im git - irc/quakenet/newserv.git/blob - core/main.c
Add missing #include.
[irc/quakenet/newserv.git] / core / main.c
1 #include "../lib/sstring.h"
2 #include "../lib/valgrind.h"
3 #include "events.h"
4 #include "schedule.h"
5 #include "hooks.h"
6 #include "modules.h"
7 #include "config.h"
8 #include "error.h"
9 #include "nsmalloc.h"
10
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <string.h>
14 #include <time.h>
15 #include <sys/time.h>
16 #include <signal.h>
17 #include <unistd.h>
18
19 void initseed();
20 void init_logfile();
21 void fini_logfile();
22 void siginthandler(int sig);
23 void sigusr1handler(int sig);
24 void sigsegvhandler(int sig);
25 void sighuphandler(int sig);
26 void handlecore(void);
27 void handlesignals(void);
28
29 int newserv_shutdown_pending;
30 static int newserv_sigint_pending, newserv_sigusr1_pending, newserv_sighup_pending;
31 static void (*oldsegv)(int);
32
33 int main(int argc, char **argv) {
34 char *config = "newserv.conf";
35
36 nsinit();
37
38 initseed();
39 inithooks();
40 inithandlers();
41 initschedule();
42
43 init_logfile();
44
45 initsstring();
46
47 if (argc>1) {
48 if (strcmp(argv[1], "--help")==0) {
49 printf("Syntax: %s [config]\n", argv[0]);
50 puts("");
51 printf("Default configuration file unless specified: %s\n", config);
52
53 return 0;
54 }
55
56 config = argv[1];
57 }
58
59 initconfig(config);
60
61 /* Loading the modules will bring in the bulk of the code */
62 initmodules();
63 signal(SIGINT, siginthandler);
64 signal(SIGUSR1, sigusr1handler);
65 signal(SIGHUP, sighuphandler);
66 oldsegv = signal(SIGSEGV, sigsegvhandler);
67
68 /* Main loop */
69 for(;;) {
70 handleevents(10);
71 doscheduledevents(time(NULL));
72
73 if (newserv_shutdown_pending) {
74 newserv_shutdown();
75 break;
76 }
77
78 handlesignals();
79 }
80
81 freeconfig();
82 finisstring();
83
84 fini_logfile();
85 finischedule();
86 finihandlers();
87
88 nsexit();
89
90 if (RUNNING_ON_VALGRIND) {
91 /* We've already manually called _fini for each of the modules. Make sure
92 * it's not getting called again when the libraries are unloaded. */
93 _exit(0);
94 }
95
96 return 0;
97 }
98
99 void handlesignals(void) {
100 if (newserv_sigusr1_pending) {
101 signal(SIGUSR1, sigusr1handler);
102 Error("core", ERR_INFO, "SIGUSR1 received.");
103 triggerhook(HOOK_CORE_SIGUSR1, NULL);
104 newserv_sigusr1_pending=0;
105 }
106
107 if (newserv_sighup_pending) {
108 signal(SIGHUP, sighuphandler);
109 Error("core", ERR_INFO, "SIGHUP received, rehashing...");
110 triggerhook(HOOK_CORE_REHASH, (void *)1);
111 newserv_sighup_pending=0;
112 }
113
114 if (newserv_sigint_pending) {
115 Error("core", ERR_INFO, "SIGINT received, terminating.");
116 triggerhook(HOOK_CORE_SIGINT, NULL);
117 newserv_sigint_pending=0;
118 newserv_shutdown_pending=1;
119 }
120 }
121
122 /*
123 * seed the pseudo-random number generator, rand()
124 */
125 void initseed() {
126 struct timeval t;
127
128 gettimeofday(&t, NULL);
129 srand(t.tv_usec);
130 }
131
132 void siginthandler(int sig) {
133 newserv_sigint_pending = 1;
134 }
135
136 void sigusr1handler(int sig) {
137 newserv_sigusr1_pending = 1;
138 }
139
140 void sighuphandler(int sig) {
141 newserv_sighup_pending = 1;
142 }
143
144 void sigsegvhandler(int sig) {
145 handlecore();
146
147 oldsegv(sig);
148 }