]> jfr.im git - irc/quakenet/newserv.git/blame - core/main.c
Add missing #include.
[irc/quakenet/newserv.git] / core / main.c
CommitLineData
2c5db955 1#include "../lib/sstring.h"
bf7e91f1 2#include "../lib/valgrind.h"
2c5db955
CP
3#include "events.h"
4#include "schedule.h"
5#include "hooks.h"
6#include "modules.h"
7#include "config.h"
8#include "error.h"
010f1c74 9#include "nsmalloc.h"
2c5db955
CP
10
11#include <stdlib.h>
12#include <stdio.h>
e29554df 13#include <string.h>
2c5db955
CP
14#include <time.h>
15#include <sys/time.h>
88c8f330 16#include <signal.h>
82d35fa5 17#include <unistd.h>
2c5db955
CP
18
19void initseed();
280505a5 20void init_logfile();
761decff 21void fini_logfile();
88c8f330 22void siginthandler(int sig);
b8e79109 23void sigusr1handler(int sig);
cdc37146 24void sigsegvhandler(int sig);
f69c0032 25void sighuphandler(int sig);
cdc37146 26void handlecore(void);
f69c0032 27void handlesignals(void);
2c5db955 28
f69c0032 29int newserv_shutdown_pending;
efa44d15 30static int newserv_sigint_pending, newserv_sigusr1_pending, newserv_sighup_pending;
506a636c
CP
31static void (*oldsegv)(int);
32
2c5db955 33int main(int argc, char **argv) {
e29554df
GB
34 char *config = "newserv.conf";
35
bf7e91f1
GB
36 nsinit();
37
2c5db955
CP
38 initseed();
39 inithooks();
40 inithandlers();
41 initschedule();
280505a5 42
43 init_logfile();
2c5db955
CP
44
45 initsstring();
46
47 if (argc>1) {
e29554df
GB
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];
2c5db955
CP
57 }
58
e29554df
GB
59 initconfig(config);
60
2c5db955
CP
61 /* Loading the modules will bring in the bulk of the code */
62 initmodules();
88c8f330 63 signal(SIGINT, siginthandler);
b8e79109 64 signal(SIGUSR1, sigusr1handler);
f69c0032 65 signal(SIGHUP, sighuphandler);
506a636c 66 oldsegv = signal(SIGSEGV, sigsegvhandler);
2c5db955
CP
67
68 /* Main loop */
69 for(;;) {
70 handleevents(10);
71 doscheduledevents(time(NULL));
f69c0032 72
83951d54 73 if (newserv_shutdown_pending) {
74 newserv_shutdown();
75 break;
76 }
b8e79109 77
f69c0032 78 handlesignals();
2c5db955 79 }
707c5824 80
707c5824
CP
81 freeconfig();
82 finisstring();
bd293217 83
4522b71a
P
84 fini_logfile();
85 finischedule();
86 finihandlers();
87
bd293217 88 nsexit();
3efa0380 89
bf7e91f1
GB
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
3efa0380 96 return 0;
2c5db955
CP
97}
98
f69c0032
CP
99void 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) {
d21a13c0 108 signal(SIGHUP, sighuphandler);
f69c0032
CP
109 Error("core", ERR_INFO, "SIGHUP received, rehashing...");
110 triggerhook(HOOK_CORE_REHASH, (void *)1);
111 newserv_sighup_pending=0;
112 }
efa44d15
CP
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 }
f69c0032
CP
120}
121
2c5db955
CP
122/*
123 * seed the pseudo-random number generator, rand()
124 */
125void initseed() {
126 struct timeval t;
127
128 gettimeofday(&t, NULL);
129 srand(t.tv_usec);
130}
88c8f330
CP
131
132void siginthandler(int sig) {
efa44d15 133 newserv_sigint_pending = 1;
88c8f330 134}
b8e79109 135
136void sigusr1handler(int sig) {
137 newserv_sigusr1_pending = 1;
138}
cdc37146 139
f69c0032
CP
140void sighuphandler(int sig) {
141 newserv_sighup_pending = 1;
142}
143
cdc37146
CP
144void sigsegvhandler(int sig) {
145 handlecore();
506a636c
CP
146
147 oldsegv(sig);
cdc37146 148}