]> jfr.im git - irc/evilnet/x3.git/blame - src/main.c
Fixed header comments
[irc/evilnet/x3.git] / src / main.c
CommitLineData
d76ed9a9 1/* main.c - srvx
2 * Copyright 2000-2004 srvx Development Team
3 *
83ff05c3 4 * This file is part of x3.
d76ed9a9 5 *
d0f04f71 6 * x3 is free software; you can redistribute it and/or modify
d76ed9a9 7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 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
ceafd592 21#define PID_FILE "x3.pid"
d76ed9a9 22
23#include "conf.h"
24#include "gline.h"
25#include "ioset.h"
26#include "modcmd.h"
27#include "saxdb.h"
28#include "sendmail.h"
d914d1cb 29#include "shun.h"
d76ed9a9 30#include "timeq.h"
31
32#include "chanserv.h"
33#include "global.h"
34#include "modules.h"
35#include "opserv.h"
36
37#ifdef HAVE_GETOPT_H
38#include <getopt.h>
39#else
40#include "getopt.h"
41#endif
42#ifdef HAVE_SYS_RESOURCE_H
43#include <sys/resource.h>
44#endif
45#ifdef HAVE_NETINET_IN_H
46#include <netinet/in.h>
47#endif
48#ifdef HAVE_SYS_SOCKET_H
49#include <sys/socket.h>
50#endif
51#ifdef HAVE_SYS_WAIT_H
52#include <sys/wait.h>
53#endif
54
55#ifndef SIGCHLD
56#define SIGCHLD SIGCLD
57#endif
58
59extern FILE *replay_file;
60
61time_t boot_time, burst_begin, now;
62unsigned long burst_length;
63struct log_type *MAIN_LOG;
64
65int quit_services, max_cycles;
66
ceafd592 67char *services_config = "x3.conf";
d76ed9a9 68
69char **services_argv;
70int services_argc;
71
72struct cManagerNode cManager;
73
74struct policer_params *oper_policer_params, *luser_policer_params, *god_policer_params;
75
76static const struct message_entry msgtab[] = {
77 { "MSG_NONE", "None" },
78 { "MSG_ON", "On" },
79 { "MSG_OFF", "Off" },
80 { "MSG_NEVER", "Never" },
de9510bc 81 { "MSG_BAR", "----------------------------------------" },
d76ed9a9 82 { "MSG_SERVICE_IMMUNE", "$b%s$b may not be kicked, killed, banned, or deopped." },
83 { "MSG_SERVICE_PRIVILEGED", "$b%s$b is a privileged service." },
84 { "MSG_NOT_A_SERVICE", "$b%s$b is not a service bot." },
85 { "MSG_COMMAND_UNKNOWN", "$b%s$b is an unknown command." },
86 { "MSG_COMMAND_PRIVILEGED", "$b%s$b is a privileged command." },
87 { "MSG_COMMAND_DISABLED", "$b%s$b is a disabled command." },
88 { "MSG_SETTING_PRIVILEGED", "$b%s$b is a privileged setting." },
89 { "MSG_AUTHENTICATE", "You must first authenticate with $b$N$b." },
90 { "MSG_USER_AUTHENTICATE", "%s must first authenticate with $b$N$b." },
91 { "MSG_SET_EMAIL_ADDR", "You must first set your account's email address. (Contact network staff if you cannot auth to your account.)" },
92 { "MSG_HANDLE_UNKNOWN", "Account $b%s$b has not been registered." },
93 { "MSG_NICK_UNKNOWN", "User with nick $b%s$b does not exist." },
94 { "MSG_CHANNEL_UNKNOWN", "Channel with name $b%s$b does not exist." },
95 { "MSG_SERVER_UNKNOWN", "Server with name $b%s$b does not exist or is not linked." },
96 { "MSG_MODULE_UNKNOWN", "No module has been registered with name $b%s$b." },
97 { "MSG_INVALID_MODES", "$b%s$b is an invalid set of channel modes." },
98 { "MSG_INVALID_GLINE", "Invalid G-line '%s'." },
d914d1cb 99 { "MSG_INVALID_SHUN", "Invalid Shun '%s'." },
d76ed9a9 100 { "MSG_INVALID_DURATION", "Invalid time span '%s'." },
101 { "MSG_NOT_TARGET_NAME", "You must provide the name of a channel or user." },
fc8798ec 102 { "MSG_NOT_CHANNEL_NAME", "The channel name you specified is not a valid channel name." },
103 { "MSG_INVALID_CHANNEL", "The channel name you specified does not exist." },
d76ed9a9 104 { "MSG_CHANNEL_ABSENT", "You aren't currently in $b%s$b." },
105 { "MSG_CHANNEL_USER_ABSENT", "$b%s$b isn't currently in $b%s$b." },
106 { "MSG_MISSING_PARAMS", "$b%s$b requires more parameters." },
107 { "MSG_DEPRECATED_COMMAND", "The $b%s$b command has been deprecated, and will be removed in the future; please use $b%s$b instead." },
108 { "MSG_OPER_SUSPENDED", "Your $b$O$b access has been suspended." },
109 { "MSG_USER_OUTRANKED", "$b%s$b outranks you (command has no effect)." },
110 { "MSG_STUPID_ACCESS_CHANGE", "Please ask someone $belse$b to demote you." },
111 { "MSG_NO_SEARCH_ACCESS", "You do not have enough access to search based on $b%s$b." },
112 { "MSG_INVALID_CRITERIA", "$b%s$b is an invalid search criteria." },
de9510bc 113 { "MSG_MATCH_COUNT", "-----------Found $b%3u$b Matches------------" },
d76ed9a9 114 { "MSG_NO_MATCHES", "Nothing matched the criteria of your search." },
115 { "MSG_TOPIC_UNKNOWN", "No help on that topic." },
116 { "MSG_INVALID_BINARY", "$b%s$b is an invalid binary value." },
117 { "MSG_INTERNAL_FAILURE", "Your command could not be processed due to an internal failure." },
118 { "MSG_DB_UNKNOWN", "I do not know of a database named %s." },
119 { "MSG_DB_IS_MONDO", "Database %s is in the \"mondo\" database and cannot be written separately." },
120 { "MSG_DB_WRITE_ERROR", "Error while writing database %s." },
121 { "MSG_DB_WROTE_DB", "Wrote database %s (in "FMT_TIME_T".%06lu seconds)." },
122 { "MSG_DB_WROTE_ALL", "Wrote all databases (in "FMT_TIME_T".%06lu seconds)." },
de9510bc 123 { "MSG_AND", "," },
d76ed9a9 124 { "MSG_0_SECONDS", "0 seconds" },
de9510bc 125 { "MSG_YEAR", "y" },
126 { "MSG_YEARS", "y" },
127 { "MSG_WEEK", "w" },
128 { "MSG_WEEKS", "w" },
129 { "MSG_DAY", "d" },
130 { "MSG_DAYS", "d" },
131 { "MSG_HOUR", "h" },
132 { "MSG_HOURS", "h" },
133 { "MSG_MINUTE", "m" },
134 { "MSG_MINUTES", "m" },
135 { "MSG_SECOND", "s" },
136 { "MSG_SECONDS", "s" },
d76ed9a9 137 { NULL, NULL }
138};
139
140void uplink_select(char *name);
141
142static int
143uplink_insert(const char *key, void *data, UNUSED_ARG(void *extra))
144{
145 struct uplinkNode *uplink = malloc(sizeof(struct uplinkNode));
146 struct record_data *rd = data;
2f61d1d7 147 struct addrinfo hints, *ai;
d76ed9a9 148 int enabled = 1;
149 char *str;
d76ed9a9 150
151 if(!uplink)
152 {
153 return 0;
154 }
155
156 uplink->name = (char *)key;
157 uplink->host = database_get_data(rd->d.object, "address", RECDB_QSTRING);
158
159 str = database_get_data(rd->d.object, "port", RECDB_QSTRING);
160 uplink->port = str ? atoi(str) : 6667;
161 uplink->password = database_get_data(rd->d.object, "password", RECDB_QSTRING);
162 uplink->their_password = database_get_data(rd->d.object, "uplink_password", RECDB_QSTRING);
163
164 str = database_get_data(rd->d.object, "enabled", RECDB_QSTRING);
165 if(str)
166 {
167 enabled = atoi(str) ? 1 : 0;
168 }
169
170 cManager.enabled += enabled;
171
172 str = database_get_data(rd->d.object, "max_tries", RECDB_QSTRING);
173 uplink->max_tries = str ? atoi(str) : 3;
174 uplink->flags = enabled ? 0 : UPLINK_UNAVAILABLE;
175 uplink->state = DISCONNECTED;
176 uplink->tries = 0;
177
178 str = database_get_data(rd->d.object, "bind_address", RECDB_QSTRING);
2f61d1d7 179 memset(&hints, 0, sizeof(hints));
180 hints.ai_flags = AI_PASSIVE;
181 hints.ai_socktype = SOCK_STREAM;
182 if (!getaddrinfo(str, NULL, &hints, &ai))
d76ed9a9 183 {
2f61d1d7 184 uplink->bind_addr_len = ai->ai_addrlen;
185 uplink->bind_addr = calloc(1, ai->ai_addrlen);
186 memcpy(uplink->bind_addr, ai->ai_addr, ai->ai_addrlen);
187 freeaddrinfo(ai);
188 }
189 else
d76ed9a9 190 {
191 uplink->bind_addr = NULL;
192 uplink->bind_addr_len = 0;
193 }
194
195 uplink->next = cManager.uplinks;
196 uplink->prev = NULL;
197
198 if(cManager.uplinks)
199 {
200 cManager.uplinks->prev = uplink;
201 }
202
203 cManager.uplinks = uplink;
204
205 /* If the configuration is being reloaded, set the current uplink
206 to the reloaded equivalent, if possible. */
207 if(cManager.uplink
208 && enabled
209 && !irccasecmp(uplink->host, cManager.uplink->host)
210 && uplink->port == cManager.uplink->port)
211 {
212 uplink->state = cManager.uplink->state;
213 uplink->tries = cManager.uplink->tries;
214 cManager.uplink = uplink;
215 }
216
217 return 0;
218}
219
220void
221uplink_compile(void)
222{
223 const char *cycles;
224 dict_t conf_node;
225 struct uplinkNode *oldUplinks = NULL, *oldUplink = NULL;
226
227 /* Save the old uplinks, we'll remove them later. */
228 oldUplink = cManager.uplink;
229 oldUplinks = cManager.uplinks;
230
231 cycles = conf_get_data("server/max_cycles", RECDB_QSTRING);
232 max_cycles = cycles ? atoi(cycles) : 30;
233 if(!(conf_node = conf_get_data("uplinks", RECDB_OBJECT)))
234 {
235 log_module(MAIN_LOG, LOG_FATAL, "No uplinks configured; giving up.");
236 exit(1);
237 }
238
239 cManager.enabled = 0;
240 dict_foreach(conf_node, uplink_insert, NULL);
241
242 /* Remove the old uplinks, if any. It doesn't matter if oldUplink (below)
243 is a reference to one of these, because it won't get dereferenced. */
244 if(oldUplinks)
245 {
246 struct uplinkNode *uplink, *next;
247
248 oldUplinks->prev->next = NULL;
249
250 for(uplink = oldUplinks; uplink; uplink = next)
251 {
252 next = uplink->next;
253 free(uplink->bind_addr);
254 free(uplink);
255 }
256 }
257
258 /* If the uplink hasn't changed, it's either NULL or pointing at
259 an uplink that was just deleted, select a new one. */
260 if(cManager.uplink == oldUplink)
261 {
262 if(oldUplink)
263 {
264 irc_squit(self, "Uplinks updated; selecting new uplink.", NULL);
265 }
266
267 cManager.uplink = NULL;
268 uplink_select(NULL);
269 }
270}
271
272struct uplinkNode *
273uplink_find(char *name)
274{
275 struct uplinkNode *uplink;
276
277 if(!cManager.enabled || !cManager.uplinks)
278 {
279 return NULL;
280 }
281
282 for(uplink = cManager.uplinks; uplink; uplink = uplink->next)
283 {
284 if(!strcasecmp(uplink->name, name))
285 {
286 return uplink;
287 }
288 }
289
290 return NULL;
291}
292
293void
294uplink_select(char *name)
295{
296 struct uplinkNode *start, *uplink, *next;
297 int stop;
298
299 if(!cManager.enabled || !cManager.uplinks)
300 {
301 log_module(MAIN_LOG, LOG_FATAL, "No uplinks enabled; giving up.");
302 exit(1);
303 }
304
305 if(!cManager.uplink)
306 {
307 start = cManager.uplinks;
308 }
309 else
310 {
311 start = cManager.uplink->next;
312 if(!start)
313 {
314 start = cManager.uplinks;
315 }
316 }
317
318 stop = 0;
319 for(uplink = start; uplink; uplink = next)
320 {
321 next = uplink->next ? uplink->next : cManager.uplinks;
322
323 if(stop)
324 {
325 uplink = NULL;
326 break;
327 }
328
329 /* We've wrapped around the list. */
330 if(next == start)
331 {
332 sleep((cManager.cycles >> 1) * 5);
333 cManager.cycles++;
334
335 if(max_cycles && (cManager.cycles >= max_cycles))
336 {
337 log_module(MAIN_LOG, LOG_FATAL, "Maximum uplink list cycles exceeded; giving up.");
338 exit(1);
339 }
340
341 /* Give the uplink currently in 'uplink' consideration,
342 and if not selected, break on the next iteration. */
343 stop = 1;
344 }
345
346 /* Skip bad uplinks. */
347 if(uplink->flags & UPLINK_UNAVAILABLE)
348 {
349 continue;
350 }
351
352 if(name && irccasecmp(uplink->name, name))
353 {
354 /* If we were told to connect to a specific uplink, don't stop
355 until we find it.
356 */
357 continue;
358 }
359
360 /* It would be possible to track uplink health through a variety
361 of statistics and only break on the best uplink. For now, break
362 on the first available one.
363 */
364
365 break;
366 }
367
368 if(!uplink)
369 {
370 /* We are shit outta luck if every single uplink has been passed
371 over. Use the current uplink if possible. */
372 if(!cManager.uplink || cManager.uplink->flags & UPLINK_UNAVAILABLE)
373 {
374 log_module(MAIN_LOG, LOG_FATAL, "All available uplinks exhausted; giving up.");
375 exit(1);
376 }
377
378 return;
379 }
380
381 cManager.uplink = uplink;
382}
383
384int
385uplink_connect(void)
386{
387 struct uplinkNode *uplink = cManager.uplink;
388
389 if(uplink->state != DISCONNECTED)
390 {
391 return 0;
392 }
393
394 if(uplink->flags & UPLINK_UNAVAILABLE)
395 {
396 uplink_select(NULL);
397 uplink = cManager.uplink;
398 }
399
400 if(uplink->tries)
401 {
402 /* This delay could scale with the number of tries. */
403 sleep(2);
404 }
405
406 if(!create_socket_client(uplink))
407 {
408 if(uplink->max_tries && (uplink->tries >= uplink->max_tries))
409 {
410 /* This is a bad uplink, move on. */
411 uplink->flags |= UPLINK_UNAVAILABLE;
412 uplink_select(NULL);
413 }
414
415 return 0;
416 }
417 else
418 {
419 uplink->state = AUTHENTICATING;
420 irc_introduce(uplink->password);
421 }
422
423 return 1;
424}
425
426void
427received_ping(void)
428{
429 /* This function is called when a ping is received. Take it as
430 a sign of link health and reset the connection manager
431 information. */
432
433 cManager.cycles = 0;
434}
435
436void sigaction_writedb(int x)
437{
438#ifndef HAVE_STRSIGNAL
439 log_module(MAIN_LOG, LOG_INFO, "Signal %d -- writing databases.", x);
440#else
441 log_module(MAIN_LOG, LOG_INFO, "%s -- writing databases.", strsignal(x));
442#endif
443 do_write_dbs = 1;
444}
445
446void sigaction_exit(int x)
447{
448#ifndef HAVE_STRSIGNAL
449 log_module(MAIN_LOG, LOG_INFO, "Signal %d -- exiting.", x);
450#else
451 log_module(MAIN_LOG, LOG_INFO, "%s -- exiting.", strsignal(x));
452#endif
453 irc_squit(self, "Exiting on signal from console.", NULL);
454 quit_services = 1;
455}
456
457void sigaction_wait(UNUSED_ARG(int x))
458{
459 int code;
460 wait4(-1, &code, WNOHANG, NULL);
461}
462
463void sigaction_rehash(int x)
464{
465#ifndef HAVE_STRSIGNAL
466 log_module(MAIN_LOG, LOG_INFO, "Signal %d -- rehashing.", x);
467#else
468 log_module(MAIN_LOG, LOG_INFO, "%s -- rehashing.", strsignal(x));
469#endif
470 do_reopen = 1;
471}
472
473static exit_func_t *ef_list;
474static unsigned int ef_size = 0, ef_used = 0;
475
476void reg_exit_func(exit_func_t handler)
477{
478 if (ef_used == ef_size) {
479 if (ef_size) {
480 ef_size <<= 1;
481 ef_list = realloc(ef_list, ef_size*sizeof(exit_func_t));
482 } else {
483 ef_size = 8;
484 ef_list = malloc(ef_size*sizeof(exit_func_t));
485 }
486 }
487 ef_list[ef_used++] = handler;
488}
489
490void call_exit_funcs(void)
491{
492 unsigned int n = ef_used;
493
494 /* Call them in reverse order because we initialize logs, then
495 * nickserv, then chanserv, etc., and they register their exit
496 * funcs in that order, and there are some dependencies (for
497 * example, ChanServ requires NickServ to not have cleaned up).
498 */
499
500 while (n > 0) {
501 ef_list[--n]();
502 }
503 free(ef_list);
504 ef_used = ef_size = 0;
505}
506
507int
508set_policer_param(const char *param, void *data, void *extra)
509{
510 struct record_data *rd = data;
511 const char *str = GET_RECORD_QSTRING(rd);
512 if (str) {
513 policer_params_set(extra, param, str);
514 }
515 return 0;
516}
517
518static void
519conf_globals(void)
520{
521 const char *info;
522 dict_t dict;
523
524 info = conf_get_data("services/global/nick", RECDB_QSTRING);
525 if (info && (info[0] == '.'))
526 info = NULL;
527 init_global(info);
528
529 info = conf_get_data("services/nickserv/nick", RECDB_QSTRING);
530 if (info && (info[0] == '.'))
531 info = NULL;
532 init_nickserv(info);
533
534 info = conf_get_data("services/chanserv/nick", RECDB_QSTRING);
535 if (info && (info[0] == '.'))
536 info = NULL;
537 init_chanserv(info);
538
539 god_policer_params = policer_params_new();
540 if ((dict = conf_get_data("policers/commands-god", RECDB_OBJECT))) {
541 dict_foreach(dict, set_policer_param, god_policer_params);
542 } else {
543 policer_params_set(god_policer_params, "size", "30");
544 policer_params_set(god_policer_params, "drain-rate", "1");
545 }
546 oper_policer_params = policer_params_new();
547 if ((dict = conf_get_data("policers/commands-oper", RECDB_OBJECT))) {
548 dict_foreach(dict, set_policer_param, oper_policer_params);
549 } else {
550 policer_params_set(oper_policer_params, "size", "10");
551 policer_params_set(oper_policer_params, "drain-rate", "1");
552 }
553 luser_policer_params = policer_params_new();
554 if ((dict = conf_get_data("policers/commands-luser", RECDB_OBJECT))) {
555 dict_foreach(dict, set_policer_param, luser_policer_params);
556 } else {
557 policer_params_set(luser_policer_params, "size", "5");
558 policer_params_set(luser_policer_params, "drain-rate", "0.50");
559 }
560
561 info = conf_get_data("services/opserv/nick", RECDB_QSTRING);
562 if (info && (info[0] == '.'))
563 info = NULL;
564 init_opserv(info);
565}
566
567#ifdef HAVE_SYS_RESOURCE_H
568
569static int
570set_item_rlimit(const char *name, void *data, void *extra)
571{
2f61d1d7 572 long rsrc;
573 int found;
d76ed9a9 574 struct record_data *rd = data;
575 struct rlimit rlim;
576 const char *str;
577
2f61d1d7 578 rsrc = (long)dict_find(extra, name, &found);
d76ed9a9 579 if (!found) {
580 log_module(MAIN_LOG, LOG_ERROR, "Invalid rlimit \"%s\" in rlimits section.", name);
581 return 0;
582 }
583 if (!(str = GET_RECORD_QSTRING(rd))) {
584 log_module(MAIN_LOG, LOG_ERROR, "Missing or invalid parameter type for rlimit \"%s\".", name);
585 return 0;
586 }
587 if (getrlimit(rsrc, &rlim) < 0) {
588 log_module(MAIN_LOG, LOG_ERROR, "Couldn't get rlimit \"%s\": errno %d: %s", name, errno, strerror(errno));
589 return 0;
590 }
591 rlim.rlim_cur = ParseVolume(str);
592 if (setrlimit(rsrc, &rlim) < 0) {
593 log_module(MAIN_LOG, LOG_ERROR, "Couldn't set rlimit \"%s\": errno %d: %s", name, errno, strerror(errno));
594 }
595 return 0;
596}
597
598static void
599conf_rlimits(void)
600{
601 dict_t dict, values;
602
603 values = dict_new();
604 dict_insert(values, "data", (void*)RLIMIT_DATA);
605 dict_insert(values, "stack", (void*)RLIMIT_STACK);
606#ifdef RLIMIT_VMEM
607 dict_insert(values, "vmem", (void*)RLIMIT_VMEM);
608#else
609#ifdef RLIMIT_AS
610 dict_insert(values, "vmem", (void*)RLIMIT_AS);
611#endif
612#endif
613 if ((dict = conf_get_data("rlimits", RECDB_OBJECT))) {
614 dict_foreach(dict, set_item_rlimit, values);
615 }
616 dict_delete(values);
617}
618
619#else
620
621static void
622conf_rlimits(void)
623{
624}
625
626#endif
627
628void main_shutdown(void)
629{
630 struct uplinkNode *ul, *ul_next;
631 ioset_cleanup();
632 for (ul = cManager.uplinks; ul; ul = ul_next) {
633 ul_next = ul->next;
634 free(ul->bind_addr);
635 free(ul);
636 }
637 tools_cleanup();
638 conf_close();
639 remove(PID_FILE);
640 policer_params_delete(god_policer_params);
641 policer_params_delete(oper_policer_params);
642 policer_params_delete(luser_policer_params);
643 if (replay_file)
644 fclose(replay_file);
645}
646
647void usage(char *self) {
648 /* We can assume we have getopt_long(). */
649 printf("Usage: %s [-c config] [-r log] [-d] [-f] [-v|-h]\n"
650 "-c, --config selects a different configuration file.\n"
651 "-d, --debug enables debug mode.\n"
ceafd592 652 "-f, --foreground run X3 in the foreground.\n"
d76ed9a9 653 "-h, --help prints this usage message.\n"
654 "-k, --check checks the configuration file's syntax.\n"
655 "-r, --replay replay a log file (for debugging)\n"
656 "-v, --version prints this program's version.\n"
657 , self);
658}
659
660void version() {
661 printf(" --------------------------------------------------\n"
ceafd592 662 " - "PACKAGE_STRING", Built: " __DATE__ ", " __TIME__".\n"
0d16e639 663 " - Copyright (C) 2000 - 2005, srvx Development Team\n"
ceafd592 664 " - Copyright (C) 2004 - 2005, X3 Development Team\n"
d76ed9a9 665 " --------------------------------------------------\n");
666}
667
668void license() {
669 printf("\n"
670 "This program is free software; you can redistribute it and/or modify\n"
671 "it under the terms of the GNU General Public License as published by\n"
672 "the Free Software Foundation; either version 2 of the License, or\n"
673 "(at your option) any later version.\n"
674 "\n"
675 "This program is distributed in the hope that it will be useful,\n"
676 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
677 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
678 "GNU General Public License for more details.\n"
679 "\n"
680 "You should have received a copy of the GNU General Public License\n"
681 "along with this program; if not, write to the Free Software\n"
682 "Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\n\n");
683}
684
685#if WITH_MALLOC_BOEHM_GC
686void
687gc_warn_proc(char *msg, GC_word arg)
688{
689 log_module(MAIN_LOG, LOG_ERROR, "GC(%p): %s", (void*)arg, msg);
690}
691#endif
692
693int main(int argc, char *argv[])
694{
695 int daemon, debug;
696 pid_t pid = 0;
697 FILE *file_out;
698 struct sigaction sv;
699
0d16e639 700#if WITH_MALLOC_BOEHM_GC
701 GC_find_leak = 1;
702 GC_set_warn_proc(gc_warn_proc);
703 GC_enable_incremental();
704#endif
705
d76ed9a9 706 daemon = 1;
707 debug = 0;
708 tools_init();
709
710 /* set up some signal handlers */
711 memset(&sv, 0, sizeof(sv));
712 sigemptyset(&sv.sa_mask);
713 sv.sa_handler = SIG_IGN;
714 sigaction(SIGPIPE, &sv, NULL);
715 sv.sa_handler = sigaction_rehash;
716 sigaction(SIGHUP, &sv, NULL);
717 sv.sa_handler = sigaction_writedb;
718 sigaction(SIGINT, &sv, NULL);
719 sv.sa_handler = sigaction_exit;
720 sigaction(SIGQUIT, &sv, NULL);
721 sv.sa_handler = sigaction_wait;
722 sigaction(SIGCHLD, &sv, NULL);
723
724 if (argc > 1) { /* parse command line, if any */
725 int c;
726 struct option options[] =
727 {
728 {"config", 1, 0, 'c'},
729 {"debug", 0, 0, 'd'},
730 {"foreground", 0, 0, 'f'},
731 {"help", 0, 0, 'h'},
732 {"check", 0, 0, 'k'},
733 {"replay", 1, 0, 'r'},
734 {"version", 0, 0, 'v'},
735 {"verbose", 0, 0, 'V'},
736 {0, 0, 0, 0}
737 };
738
739 while ((c = getopt_long(argc, argv, "c:kr:dfvVh", options, NULL)) != -1) {
740 switch(c) {
741 case 'c':
742 services_config = optarg;
743 break;
744 case 'k':
745 if (conf_read(services_config)) {
746 printf("%s appears to be a valid configuration file.\n", services_config);
747 } else {
748 printf("%s is an invalid configuration file.\n", services_config);
749 }
750 exit(0);
751 case 'r':
752 replay_file = fopen(optarg, "r");
753 if (!replay_file) {
754 fprintf(stderr, "Could not open %s for reading: %s (%d)\n",
755 optarg, strerror(errno), errno);
756 exit(0);
757 }
758 break;
759 case 'd':
760 debug = 1;
761 break;
762 case 'f':
763 daemon = 0;
764 break;
765 case 'v':
766 version();
767 license();
768 exit(0);
769 case 'h':
770 default:
771 usage(argv[0]);
772 exit(0);
773 }
774 }
775 }
776
777 version();
778
779 if (replay_file) {
780 /* We read a line here to "prime" the replay file parser, but
781 * mostly to get the right value of "now" for when we do the
782 * irc_introduce. */
783 replay_read_line();
784 boot_time = now;
785 } else {
786 boot_time = time(&now);
787 }
788
789 log_module(MAIN_LOG, LOG_INFO, "Initializing daemon...");
790 if (!conf_read(services_config)) {
791 log_module(MAIN_LOG, LOG_FATAL, "Unable to read %s.", services_config);
792 exit(0);
793 }
794
795 conf_register_reload(uplink_compile);
796
797 if (daemon) {
798 /* Attempt to fork into the background if daemon mode is on. */
799 pid = fork();
800 if (pid < 0) {
801 log_module(MAIN_LOG, LOG_FATAL, "Unable to fork: %s", strerror(errno));
802 } else if (pid > 0) {
803 log_module(MAIN_LOG, LOG_INFO, "Forking into the background (pid: %i)...", pid);
804 exit(0);
805 }
806 setsid();
807 /* Close these since we should not use them from now on. */
808 fclose(stdin);
809 fclose(stdout);
810 fclose(stderr);
811 }
812
813 if ((file_out = fopen(PID_FILE, "w")) == NULL) {
814 /* Create the main process' pid file */
815 log_module(MAIN_LOG, LOG_ERROR, "Unable to create PID file: %s", strerror(errno));
816 } else {
817 fprintf(file_out, "%i\n", (int)getpid());
818 fclose(file_out);
819 }
820
821 services_argc = argc;
822 services_argv = argv;
823
824 atexit(call_exit_funcs);
825 reg_exit_func(main_shutdown);
826
827 log_init();
ceafd592 828 MAIN_LOG = log_register_type("x3", "file:main.log");
d76ed9a9 829 if (debug)
830 log_debug();
d76ed9a9 831 timeq_init();
832 init_structs();
833 init_parse();
834 modcmd_init();
835 saxdb_init();
836 gline_init();
d914d1cb 837 shun_init();
d76ed9a9 838 sendmail_init();
839 helpfile_init();
840 conf_globals(); /* initializes the core services */
841 conf_rlimits();
842 modules_init();
843 message_register_table(msgtab);
844 modcmd_finalize();
845 saxdb_finalize();
846 helpfile_finalize();
847 modules_finalize();
848
849 /* The first exit func to be called *should* be saxdb_write_all(). */
850 reg_exit_func(saxdb_write_all);
851 if (replay_file) {
852 char *msg;
853 log_module(MAIN_LOG, LOG_INFO, "Beginning replay...");
854 srand(now);
855 replay_event_loop();
856 if ((msg = dict_sanity_check(clients))) {
857 log_module(MAIN_LOG, LOG_ERROR, "Clients insanity: %s", msg);
858 free(msg);
859 }
860 if ((msg = dict_sanity_check(channels))) {
861 log_module(MAIN_LOG, LOG_ERROR, "Channels insanity: %s", msg);
862 free(msg);
863 }
864 if ((msg = dict_sanity_check(servers))) {
865 log_module(MAIN_LOG, LOG_ERROR, "Servers insanity: %s", msg);
866 free(msg);
867 }
868 } else {
869 srand(time(&now));
870 ioset_run();
871 }
872 return 0;
873}