]> jfr.im git - irc/quakenet/newserv.git/blame - core/main.c
Add SIGHUP handler, by default it triggers HOOK_CORE_REHASH.
[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
CP
25int newserv_shutdown_pending;
26static int 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
103521a1 65 nsexit();
707c5824
CP
66
67 freeconfig();
68 finisstring();
2c5db955
CP
69}
70
f69c0032
CP
71void handlesignals(void) {
72 if (newserv_sigusr1_pending) {
73 signal(SIGUSR1, sigusr1handler);
74 Error("core", ERR_INFO, "SIGUSR1 received.");
75 triggerhook(HOOK_CORE_SIGUSR1, NULL);
76 newserv_sigusr1_pending=0;
77 }
78
79 if (newserv_sighup_pending) {
80 signal(SIGHUP, sigusr1handler);
81 Error("core", ERR_INFO, "SIGHUP received, rehashing...");
82 triggerhook(HOOK_CORE_REHASH, (void *)1);
83 newserv_sighup_pending=0;
84 }
85}
86
2c5db955
CP
87/*
88 * seed the pseudo-random number generator, rand()
89 */
90void initseed() {
91 struct timeval t;
92
93 gettimeofday(&t, NULL);
94 srand(t.tv_usec);
95}
88c8f330
CP
96
97void siginthandler(int sig) {
98 newserv_shutdown_pending = 1;
99}
b8e79109 100
101void sigusr1handler(int sig) {
102 newserv_sigusr1_pending = 1;
103}
cdc37146 104
f69c0032
CP
105void sighuphandler(int sig) {
106 newserv_sighup_pending = 1;
107}
108
cdc37146
CP
109void sigsegvhandler(int sig) {
110 handlecore();
506a636c
CP
111
112 oldsegv(sig);
cdc37146 113}