]> jfr.im git - irc/quakenet/newserv.git/blob - core/main.c
Implement --help parameter.
[irc/quakenet/newserv.git] / core / main.c
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 <string.h>
13 #include <time.h>
14 #include <sys/time.h>
15 #include <signal.h>
16
17 void initseed();
18 void init_logfile();
19 void fini_logfile();
20 void siginthandler(int sig);
21 void sigusr1handler(int sig);
22 void sigsegvhandler(int sig);
23 void sighuphandler(int sig);
24 void handlecore(void);
25 void handlesignals(void);
26
27 int newserv_shutdown_pending;
28 static int newserv_sigint_pending, newserv_sigusr1_pending, newserv_sighup_pending;
29 static void (*oldsegv)(int);
30
31 int main(int argc, char **argv) {
32 char *config = "newserv.conf";
33
34 initseed();
35 inithooks();
36 inithandlers();
37 initschedule();
38
39 init_logfile();
40
41 initsstring();
42
43 if (argc>1) {
44 if (strcmp(argv[1], "--help")==0) {
45 printf("Syntax: %s [config]\n", argv[0]);
46 puts("");
47 printf("Default configuration file unless specified: %s\n", config);
48
49 return 0;
50 }
51
52 config = argv[1];
53 }
54
55 initconfig(config);
56
57 /* Loading the modules will bring in the bulk of the code */
58 initmodules();
59 signal(SIGINT, siginthandler);
60 signal(SIGUSR1, sigusr1handler);
61 signal(SIGHUP, sighuphandler);
62 oldsegv = signal(SIGSEGV, sigsegvhandler);
63
64 /* Main loop */
65 for(;;) {
66 handleevents(10);
67 doscheduledevents(time(NULL));
68
69 if (newserv_shutdown_pending) {
70 newserv_shutdown();
71 break;
72 }
73
74 handlesignals();
75 }
76
77 freeconfig();
78 finisstring();
79
80 fini_logfile();
81 finischedule();
82 finihandlers();
83
84 nsexit();
85
86 return 0;
87 }
88
89 void handlesignals(void) {
90 if (newserv_sigusr1_pending) {
91 signal(SIGUSR1, sigusr1handler);
92 Error("core", ERR_INFO, "SIGUSR1 received.");
93 triggerhook(HOOK_CORE_SIGUSR1, NULL);
94 newserv_sigusr1_pending=0;
95 }
96
97 if (newserv_sighup_pending) {
98 signal(SIGHUP, sighuphandler);
99 Error("core", ERR_INFO, "SIGHUP received, rehashing...");
100 triggerhook(HOOK_CORE_REHASH, (void *)1);
101 newserv_sighup_pending=0;
102 }
103
104 if (newserv_sigint_pending) {
105 Error("core", ERR_INFO, "SIGINT received, terminating.");
106 triggerhook(HOOK_CORE_SIGINT, NULL);
107 newserv_sigint_pending=0;
108 newserv_shutdown_pending=1;
109 }
110 }
111
112 /*
113 * seed the pseudo-random number generator, rand()
114 */
115 void initseed() {
116 struct timeval t;
117
118 gettimeofday(&t, NULL);
119 srand(t.tv_usec);
120 }
121
122 void siginthandler(int sig) {
123 newserv_sigint_pending = 1;
124 }
125
126 void sigusr1handler(int sig) {
127 newserv_sigusr1_pending = 1;
128 }
129
130 void sighuphandler(int sig) {
131 newserv_sighup_pending = 1;
132 }
133
134 void sigsegvhandler(int sig) {
135 handlecore();
136
137 oldsegv(sig);
138 }