]> jfr.im git - irc/evilnet/x3.git/blame_incremental - src/main.c
Couple of srvx updates.
[irc/evilnet/x3.git] / src / main.c
... / ...
CommitLineData
1/* main.c - X3
2 * Copyright 2000-2006 srvx Development Team
3 *
4 * This file is part of X3.
5 *
6 * srvx is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with srvx; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 */
20
21#define PID_FILE "x3.pid"
22
23#include "conf.h"
24#include "gline.h"
25#include "ioset.h"
26#include "modcmd.h"
27#include "saxdb.h"
28#include "mail.h"
29#include "timeq.h"
30#include "sar.h"
31#include "shun.h"
32
33#include "chanserv.h"
34#include "global.h"
35#include "modules.h"
36#include "opserv.h"
37#include "spamserv.h"
38
39#ifdef HAVE_GETOPT_H
40#include <getopt.h>
41#else
42#include "getopt.h"
43#endif
44#ifdef HAVE_SYS_RESOURCE_H
45#include <sys/resource.h>
46#endif
47#ifdef HAVE_NETINET_IN_H
48#include <netinet/in.h>
49#endif
50#ifdef HAVE_SYS_SOCKET_H
51#include <sys/socket.h>
52#endif
53#ifdef HAVE_SYS_WAIT_H
54#include <sys/wait.h>
55#endif
56
57#include "main-common.c"
58
59void sigaction_writedb(int x)
60{
61#ifndef HAVE_STRSIGNAL
62 log_module(MAIN_LOG, LOG_INFO, "Signal %d -- writing databases.", x);
63#else
64 log_module(MAIN_LOG, LOG_INFO, "%s -- writing databases.", strsignal(x));
65#endif
66 do_write_dbs = 1;
67}
68
69void sigaction_exit(int x)
70{
71#ifndef HAVE_STRSIGNAL
72 log_module(MAIN_LOG, LOG_INFO, "Signal %d -- exiting.", x);
73#else
74 log_module(MAIN_LOG, LOG_INFO, "%s -- exiting.", strsignal(x));
75#endif
76 irc_squit(self, "Exiting on signal from console.", NULL);
77 quit_services = 1;
78}
79
80void sigaction_wait(UNUSED_ARG(int x))
81{
82 int code;
83 wait4(-1, &code, WNOHANG, NULL);
84}
85
86void sigaction_rehash(int x)
87{
88#ifndef HAVE_STRSIGNAL
89 log_module(MAIN_LOG, LOG_INFO, "Signal %d -- rehashing.", x);
90#else
91 log_module(MAIN_LOG, LOG_INFO, "%s -- rehashing.", strsignal(x));
92#endif
93 do_reopen = 1;
94}
95
96#if WITH_MALLOC_BOEHM_GC
97void
98gc_warn_proc(char *msg, GC_word arg)
99{
100 log_module(MAIN_LOG, LOG_ERROR, "GC(%p): %s", (void*)arg, msg);
101}
102#endif
103
104int main(int argc, char *argv[])
105{
106 int run_as_daemon;
107 int debug;
108 pid_t pid = 0;
109 FILE *file_out;
110 struct sigaction sv;
111
112#if WITH_MALLOC_BOEHM_GC
113 GC_find_leak = 1;
114 GC_set_warn_proc(gc_warn_proc);
115 GC_enable_incremental();
116#endif
117
118 run_as_daemon = 1;
119 debug = 0;
120 tools_init();
121
122 /* set up some signal handlers */
123 memset(&sv, 0, sizeof(sv));
124 sigemptyset(&sv.sa_mask);
125 sv.sa_handler = SIG_IGN;
126 sigaction(SIGPIPE, &sv, NULL);
127 sv.sa_handler = sigaction_rehash;
128 sigaction(SIGHUP, &sv, NULL);
129 sv.sa_handler = sigaction_writedb;
130 sigaction(SIGINT, &sv, NULL);
131 sv.sa_handler = sigaction_exit;
132 sigaction(SIGQUIT, &sv, NULL);
133 sv.sa_handler = sigaction_wait;
134 sigaction(SIGCHLD, &sv, NULL);
135
136 if (argc > 1) { /* parse command line, if any */
137 int c;
138 struct option options[] =
139 {
140 {"config", 1, 0, 'c'},
141 {"debug", 0, 0, 'd'},
142 {"foreground", 0, 0, 'f'},
143 {"help", 0, 0, 'h'},
144 {"check", 0, 0, 'k'},
145 {"replay", 1, 0, 'r'},
146 {"version", 0, 0, 'v'},
147 {0, 0, 0, 0}
148 };
149
150 while ((c = getopt_long(argc, argv, "c:dfhkr:v", options, NULL)) != -1) {
151 switch (c) {
152 case 'c':
153 services_config = optarg;
154 break;
155 case 'k':
156 if (conf_read(services_config)) {
157 printf("%s appears to be a valid configuration file.\n", services_config);
158 } else {
159 printf("%s is an invalid configuration file.\n", services_config);
160 }
161 exit(0);
162 case 'r':
163 replay_file = fopen(optarg, "r");
164 if (!replay_file) {
165 fprintf(stderr, "Could not open %s for reading: %s (%d)\n",
166 optarg, strerror(errno), errno);
167 exit(0);
168 }
169 break;
170 case 'd':
171 debug = 1;
172 break;
173 case 'f':
174 run_as_daemon = 0;
175 break;
176 case 'v':
177 version();
178 license();
179 exit(0);
180 case 'h':
181 default:
182 usage(argv[0]);
183 exit(0);
184 }
185 }
186 }
187
188 version();
189
190 if (replay_file) {
191 /* We read a line here to "prime" the replay file parser, but
192 * mostly to get the right value of "now" for when we do the
193 * irc_introduce. */
194 replay_read_line();
195 } else {
196 now = time(NULL);
197 }
198 boot_time = now;
199
200 fprintf(stdout, "Initializing daemon...\n");
201 if (!conf_read(services_config)) {
202 fprintf(stderr, "Unable to read %s.\n", services_config);
203 exit(1);
204 }
205
206 conf_register_reload(uplink_compile);
207
208 if (run_as_daemon) {
209 /* Attempt to fork into the background if daemon mode is on. */
210 pid = fork();
211 if (pid < 0) {
212 fprintf(stderr, "Unable to fork: %s\n", strerror(errno));
213 } else if (pid > 0) {
214 fprintf(stdout, "Forking into the background (pid: %d)...\n", pid);
215 exit(0);
216 }
217 setsid();
218 }
219
220 file_out = fopen(PID_FILE, "w");
221 if (file_out == NULL) {
222 /* Create the main process' pid file */
223 fprintf(stderr, "Unable to create PID file: %s", strerror(errno));
224 } else {
225 fprintf(file_out, "%i\n", (int)getpid());
226 fclose(file_out);
227 }
228
229 if (run_as_daemon) {
230 /* Close these since we should not use them from now on. */
231 fclose(stdin);
232 fclose(stdout);
233 fclose(stderr);
234 }
235
236 services_argc = argc;
237 services_argv = argv;
238
239 atexit(call_exit_funcs);
240 reg_exit_func(main_shutdown, NULL);
241
242 log_init();
243 MAIN_LOG = log_register_type("x3", "file:main.log");
244 if (debug)
245 log_debug();
246 ioset_init();
247 init_structs();
248 init_parse();
249 modcmd_init();
250 saxdb_init();
251 sar_init();
252 gline_init();
253 shun_init();
254 mail_init();
255 helpfile_init();
256 conf_globals(); /* initializes the core services */
257 conf_rlimits();
258 modules_init();
259 message_register_table(msgtab);
260 modcmd_finalize();
261 saxdb_finalize();
262 helpfile_finalize();
263 modules_finalize();
264
265 /* The first exit func to be called *should* be saxdb_write_all(). */
266 reg_exit_func(saxdb_write_all, NULL);
267 if (replay_file) {
268 char *msg;
269 log_module(MAIN_LOG, LOG_INFO, "Beginning replay...");
270 srand(now);
271 replay_event_loop();
272 if ((msg = dict_sanity_check(clients))) {
273 log_module(MAIN_LOG, LOG_ERROR, "Clients insanity: %s", msg);
274 free(msg);
275 }
276 if ((msg = dict_sanity_check(channels))) {
277 log_module(MAIN_LOG, LOG_ERROR, "Channels insanity: %s", msg);
278 free(msg);
279 }
280 if ((msg = dict_sanity_check(servers))) {
281 log_module(MAIN_LOG, LOG_ERROR, "Servers insanity: %s", msg);
282 free(msg);
283 }
284 } else {
285 now = time(NULL);
286 srand(now);
287 ioset_run();
288 }
289 return 0;
290}