]> jfr.im git - irc/quakenet/newserv.git/blame_incremental - core/main.c
CORE: Fix compiler warnings
[irc/quakenet/newserv.git] / core / main.c
... / ...
CommitLineData
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
16void initseed();
17void init_logfile();
18void fini_logfile();
19void siginthandler(int sig);
20void sigusr1handler(int sig);
21void sigsegvhandler(int sig);
22void sighuphandler(int sig);
23void handlecore(void);
24void handlesignals(void);
25
26int newserv_shutdown_pending;
27static int newserv_sigint_pending, newserv_sigusr1_pending, newserv_sighup_pending;
28static void (*oldsegv)(int);
29
30int main(int argc, char **argv) {
31 initseed();
32 inithooks();
33 inithandlers();
34 initschedule();
35
36 init_logfile();
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();
48 signal(SIGINT, siginthandler);
49 signal(SIGUSR1, sigusr1handler);
50 signal(SIGHUP, sighuphandler);
51 oldsegv = signal(SIGSEGV, sigsegvhandler);
52
53 /* Main loop */
54 for(;;) {
55 handleevents(10);
56 doscheduledevents(time(NULL));
57
58 if (newserv_shutdown_pending) {
59 newserv_shutdown();
60 break;
61 }
62
63 handlesignals();
64 }
65
66 freeconfig();
67 finisstring();
68
69 fini_logfile();
70 finischedule();
71 finihandlers();
72
73 nsexit();
74}
75
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) {
85 signal(SIGHUP, sighuphandler);
86 Error("core", ERR_INFO, "SIGHUP received, rehashing...");
87 triggerhook(HOOK_CORE_REHASH, (void *)1);
88 newserv_sighup_pending=0;
89 }
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 }
97}
98
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}
108
109void siginthandler(int sig) {
110 newserv_sigint_pending = 1;
111}
112
113void sigusr1handler(int sig) {
114 newserv_sigusr1_pending = 1;
115}
116
117void sighuphandler(int sig) {
118 newserv_sighup_pending = 1;
119}
120
121void sigsegvhandler(int sig) {
122 handlecore();
123
124 oldsegv(sig);
125}