]> jfr.im git - irc/quakenet/newserv.git/blame - core/main.c
BUILD: add require-all build mode
[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>
5c4fea01
GB
16#include <sys/stat.h>
17#include <sys/types.h>
18#include <errno.h>
88c8f330 19#include <signal.h>
82d35fa5 20#include <unistd.h>
2c5db955
CP
21
22void initseed();
280505a5 23void init_logfile();
761decff 24void fini_logfile();
88c8f330 25void siginthandler(int sig);
b8e79109 26void sigusr1handler(int sig);
cdc37146 27void sigsegvhandler(int sig);
f69c0032 28void sighuphandler(int sig);
cdc37146 29void handlecore(void);
f69c0032 30void handlesignals(void);
2c5db955 31
f69c0032 32int newserv_shutdown_pending;
efa44d15 33static int newserv_sigint_pending, newserv_sigusr1_pending, newserv_sighup_pending;
506a636c
CP
34static void (*oldsegv)(int);
35
2c5db955 36int main(int argc, char **argv) {
e29554df
GB
37 char *config = "newserv.conf";
38
bf7e91f1
GB
39 nsinit();
40
2c5db955
CP
41 initseed();
42 inithooks();
43 inithandlers();
44 initschedule();
280505a5 45
46 init_logfile();
2c5db955 47
2c5db955 48 if (argc>1) {
e29554df
GB
49 if (strcmp(argv[1], "--help")==0) {
50 printf("Syntax: %s [config]\n", argv[0]);
51 puts("");
52 printf("Default configuration file unless specified: %s\n", config);
53
54 return 0;
55 }
56
57 config = argv[1];
2c5db955
CP
58 }
59
e29554df
GB
60 initconfig(config);
61
5c4fea01
GB
62 /* modules can rely on this directory always being there */
63 if (mkdir("data", 0700) < 0 && errno != EEXIST) {
64 perror("mkdir");
65 return 1;
66 }
67
2c5db955
CP
68 /* Loading the modules will bring in the bulk of the code */
69 initmodules();
6c510e06 70 signal(SIGPIPE, SIG_IGN);
88c8f330 71 signal(SIGINT, siginthandler);
b8e79109 72 signal(SIGUSR1, sigusr1handler);
f69c0032 73 signal(SIGHUP, sighuphandler);
506a636c 74 oldsegv = signal(SIGSEGV, sigsegvhandler);
2c5db955
CP
75
76 /* Main loop */
77 for(;;) {
78 handleevents(10);
79 doscheduledevents(time(NULL));
f69c0032 80
83951d54 81 if (newserv_shutdown_pending) {
82 newserv_shutdown();
83 break;
84 }
b8e79109 85
f69c0032 86 handlesignals();
2c5db955 87 }
707c5824 88
707c5824 89 freeconfig();
bd293217 90
4522b71a
P
91 fini_logfile();
92 finischedule();
93 finihandlers();
94
bd293217 95 nsexit();
3efa0380 96
bf7e91f1
GB
97 if (RUNNING_ON_VALGRIND) {
98 /* We've already manually called _fini for each of the modules. Make sure
99 * it's not getting called again when the libraries are unloaded. */
100 _exit(0);
101 }
102
3efa0380 103 return 0;
2c5db955
CP
104}
105
f69c0032
CP
106void handlesignals(void) {
107 if (newserv_sigusr1_pending) {
108 signal(SIGUSR1, sigusr1handler);
109 Error("core", ERR_INFO, "SIGUSR1 received.");
110 triggerhook(HOOK_CORE_SIGUSR1, NULL);
111 newserv_sigusr1_pending=0;
112 }
113
114 if (newserv_sighup_pending) {
d21a13c0 115 signal(SIGHUP, sighuphandler);
f69c0032
CP
116 Error("core", ERR_INFO, "SIGHUP received, rehashing...");
117 triggerhook(HOOK_CORE_REHASH, (void *)1);
118 newserv_sighup_pending=0;
119 }
efa44d15
CP
120
121 if (newserv_sigint_pending) {
122 Error("core", ERR_INFO, "SIGINT received, terminating.");
123 triggerhook(HOOK_CORE_SIGINT, NULL);
124 newserv_sigint_pending=0;
125 newserv_shutdown_pending=1;
126 }
f69c0032
CP
127}
128
2c5db955
CP
129/*
130 * seed the pseudo-random number generator, rand()
131 */
132void initseed() {
133 struct timeval t;
134
135 gettimeofday(&t, NULL);
136 srand(t.tv_usec);
137}
88c8f330
CP
138
139void siginthandler(int sig) {
efa44d15 140 newserv_sigint_pending = 1;
88c8f330 141}
b8e79109 142
143void sigusr1handler(int sig) {
144 newserv_sigusr1_pending = 1;
145}
cdc37146 146
f69c0032
CP
147void sighuphandler(int sig) {
148 newserv_sighup_pending = 1;
149}
150
cdc37146
CP
151void sigsegvhandler(int sig) {
152 handlecore();
506a636c
CP
153
154 oldsegv(sig);
cdc37146 155}