]> jfr.im git - irc/quakenet/newserv.git/blame - core/main.c
fix some format string errors and dergister some hooks, also do some (pointless)...
[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 67
f074aada
P
68 fini_logfile();
69 finischedule();
70 finihandlers();
71
bd293217 72 nsexit();
2c5db955
CP
73}
74
f69c0032
CP
75void 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) {
d21a13c0 84 signal(SIGHUP, sighuphandler);
f69c0032
CP
85 Error("core", ERR_INFO, "SIGHUP received, rehashing...");
86 triggerhook(HOOK_CORE_REHASH, (void *)1);
87 newserv_sighup_pending=0;
88 }
efa44d15
CP
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 }
f69c0032
CP
96}
97
2c5db955
CP
98/*
99 * seed the pseudo-random number generator, rand()
100 */
101void initseed() {
102 struct timeval t;
103
104 gettimeofday(&t, NULL);
105 srand(t.tv_usec);
106}
88c8f330
CP
107
108void siginthandler(int sig) {
efa44d15 109 newserv_sigint_pending = 1;
88c8f330 110}
b8e79109 111
112void sigusr1handler(int sig) {
113 newserv_sigusr1_pending = 1;
114}
cdc37146 115
f69c0032
CP
116void sighuphandler(int sig) {
117 newserv_sighup_pending = 1;
118}
119
cdc37146
CP
120void sigsegvhandler(int sig) {
121 handlecore();
506a636c
CP
122
123 oldsegv(sig);
cdc37146 124}