]>
jfr.im git - irc/evilnet/x3.git/blob - src/main.c
2 * Copyright 2000-2004 srvx Development Team
4 * This file is part of x3.
6 * x3 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.
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.
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.
21 #define PID_FILE "x3.pid"
43 #ifdef HAVE_SYS_RESOURCE_H
44 #include <sys/resource.h>
46 #ifdef HAVE_NETINET_IN_H
47 #include <netinet/in.h>
49 #ifdef HAVE_SYS_SOCKET_H
50 #include <sys/socket.h>
52 #ifdef HAVE_SYS_WAIT_H
57 #define SIGCHLD SIGCLD
60 extern FILE *replay_file
;
62 time_t boot_time
, burst_begin
, now
;
63 unsigned long burst_length
;
64 struct log_type
*MAIN_LOG
;
66 int quit_services
, max_cycles
;
68 char *services_config
= "x3.conf";
73 struct cManagerNode cManager
;
75 struct policer_params
*oper_policer_params
, *luser_policer_params
, *god_policer_params
;
77 static const struct message_entry msgtab
[] = {
78 { "MSG_NONE", "None" },
81 { "MSG_NEVER", "Never" },
82 { "MSG_BAR", "----------------------------------------" },
83 { "MSG_SERVICE_IMMUNE", "$b%s$b may not be kicked, killed, banned, or deopped." },
84 { "MSG_SERVICE_PRIVILEGED", "$b%s$b is a privileged service." },
85 { "MSG_NOT_A_SERVICE", "$b%s$b is not a service bot." },
86 { "MSG_COMMAND_UNKNOWN", "$b%s$b is an unknown command." },
87 { "MSG_COMMAND_PRIVILEGED", "$b%s$b is a privileged command." },
88 { "MSG_COMMAND_DISABLED", "$b%s$b is a disabled command." },
89 { "MSG_SETTING_PRIVILEGED", "$b%s$b is a privileged setting." },
90 { "MSG_AUTHENTICATE", "You must first authenticate with $b$N$b." },
91 { "MSG_USER_AUTHENTICATE", "%s must first authenticate with $b$N$b." },
92 { "MSG_SET_EMAIL_ADDR", "You must first set your account's email address. (Contact network staff if you cannot auth to your account.)" },
93 { "MSG_HANDLE_UNKNOWN", "Account $b%s$b has not been registered." },
94 { "MSG_NICK_UNKNOWN", "User with nick $b%s$b does not exist." },
95 { "MSG_CHANNEL_UNKNOWN", "Channel with name $b%s$b does not exist." },
96 { "MSG_SERVER_UNKNOWN", "Server with name $b%s$b does not exist or is not linked." },
97 { "MSG_MODULE_UNKNOWN", "No module has been registered with name $b%s$b." },
98 { "MSG_INVALID_MODES", "$b%s$b is an invalid set of channel modes." },
99 { "MSG_INVALID_GLINE", "Invalid G-line '%s'." },
100 { "MSG_INVALID_SHUN", "Invalid Shun '%s'." },
101 { "MSG_INVALID_DURATION", "Invalid time span '%s'." },
102 { "MSG_NOT_TARGET_NAME", "You must provide the name of a channel or user." },
103 { "MSG_NOT_CHANNEL_NAME", "The channel name you specified is not a valid channel name." },
104 { "MSG_INVALID_CHANNEL", "The channel name you specified does not exist." },
105 { "MSG_CHANNEL_ABSENT", "You aren't currently in $b%s$b." },
106 { "MSG_CHANNEL_USER_ABSENT", "$b%s$b isn't currently in $b%s$b." },
107 { "MSG_MISSING_PARAMS", "$b%s$b requires more parameters." },
108 { "MSG_DEPRECATED_COMMAND", "The $b%s$b command has been deprecated, and will be removed in the future; please use $b%s$b instead." },
109 { "MSG_OPER_SUSPENDED", "Your $b$O$b access has been suspended." },
110 { "MSG_USER_OUTRANKED", "$b%s$b outranks you (command has no effect)." },
111 { "MSG_STUPID_ACCESS_CHANGE", "Please ask someone $belse$b to demote you." },
112 { "MSG_NO_SEARCH_ACCESS", "You do not have enough access to search based on $b%s$b." },
113 { "MSG_INVALID_CRITERIA", "$b%s$b is an invalid search criteria." },
114 { "MSG_MATCH_COUNT", "-----------Found $b%3u$b Matches------------" },
115 { "MSG_NO_MATCHES", "Nothing matched the criteria of your search." },
116 { "MSG_TOPIC_UNKNOWN", "No help on that topic." },
117 { "MSG_INVALID_BINARY", "$b%s$b is an invalid binary value." },
118 { "MSG_INTERNAL_FAILURE", "Your command could not be processed due to an internal failure." },
119 { "MSG_DB_UNKNOWN", "I do not know of a database named %s." },
120 { "MSG_DB_IS_MONDO", "Database %s is in the \"mondo\" database and cannot be written separately." },
121 { "MSG_DB_WRITE_ERROR", "Error while writing database %s." },
122 { "MSG_DB_WROTE_DB", "Wrote database %s (in "FMT_TIME_T
".%06lu seconds)." },
123 { "MSG_DB_WROTE_ALL", "Wrote all databases (in "FMT_TIME_T
".%06lu seconds)." },
125 { "MSG_0_SECONDS", "0 seconds" },
127 { "MSG_YEARS", "y" },
129 { "MSG_WEEKS", "w" },
133 { "MSG_HOURS", "h" },
134 { "MSG_MINUTE", "m" },
135 { "MSG_MINUTES", "m" },
136 { "MSG_SECOND", "s" },
137 { "MSG_SECONDS", "s" },
141 void uplink_select(char *name
);
144 uplink_insert(const char *key
, void *data
, UNUSED_ARG(void *extra
))
146 struct uplinkNode
*uplink
= malloc(sizeof(struct uplinkNode
));
147 struct record_data
*rd
= data
;
148 struct addrinfo hints
, *ai
;
157 uplink
->name
= (char *)key
;
158 uplink
->host
= database_get_data(rd
->d
.object
, "address", RECDB_QSTRING
);
160 str
= database_get_data(rd
->d
.object
, "port", RECDB_QSTRING
);
161 uplink
->port
= str
? atoi(str
) : 6667;
162 uplink
->password
= database_get_data(rd
->d
.object
, "password", RECDB_QSTRING
);
163 uplink
->their_password
= database_get_data(rd
->d
.object
, "uplink_password", RECDB_QSTRING
);
165 str
= database_get_data(rd
->d
.object
, "enabled", RECDB_QSTRING
);
168 enabled
= atoi(str
) ? 1 : 0;
171 cManager
.enabled
+= enabled
;
173 str
= database_get_data(rd
->d
.object
, "max_tries", RECDB_QSTRING
);
174 uplink
->max_tries
= str
? atoi(str
) : 3;
175 uplink
->flags
= enabled
? 0 : UPLINK_UNAVAILABLE
;
176 uplink
->state
= DISCONNECTED
;
179 str
= database_get_data(rd
->d
.object
, "bind_address", RECDB_QSTRING
);
180 memset(&hints
, 0, sizeof(hints
));
181 hints
.ai_flags
= AI_PASSIVE
;
182 hints
.ai_socktype
= SOCK_STREAM
;
183 if (!getaddrinfo(str
, NULL
, &hints
, &ai
))
185 uplink
->bind_addr_len
= ai
->ai_addrlen
;
186 uplink
->bind_addr
= calloc(1, ai
->ai_addrlen
);
187 memcpy(uplink
->bind_addr
, ai
->ai_addr
, ai
->ai_addrlen
);
192 uplink
->bind_addr
= NULL
;
193 uplink
->bind_addr_len
= 0;
196 uplink
->next
= cManager
.uplinks
;
201 cManager
.uplinks
->prev
= uplink
;
204 cManager
.uplinks
= uplink
;
206 /* If the configuration is being reloaded, set the current uplink
207 to the reloaded equivalent, if possible. */
210 && !irccasecmp(uplink
->host
, cManager
.uplink
->host
)
211 && uplink
->port
== cManager
.uplink
->port
)
213 uplink
->state
= cManager
.uplink
->state
;
214 uplink
->tries
= cManager
.uplink
->tries
;
215 cManager
.uplink
= uplink
;
226 struct uplinkNode
*oldUplinks
= NULL
, *oldUplink
= NULL
;
228 /* Save the old uplinks, we'll remove them later. */
229 oldUplink
= cManager
.uplink
;
230 oldUplinks
= cManager
.uplinks
;
232 cycles
= conf_get_data("server/max_cycles", RECDB_QSTRING
);
233 max_cycles
= cycles
? atoi(cycles
) : 30;
234 if(!(conf_node
= conf_get_data("uplinks", RECDB_OBJECT
)))
236 log_module(MAIN_LOG
, LOG_FATAL
, "No uplinks configured; giving up.");
240 cManager
.enabled
= 0;
241 dict_foreach(conf_node
, uplink_insert
, NULL
);
243 /* Remove the old uplinks, if any. It doesn't matter if oldUplink (below)
244 is a reference to one of these, because it won't get dereferenced. */
247 struct uplinkNode
*uplink
, *next
;
249 oldUplinks
->prev
->next
= NULL
;
251 for(uplink
= oldUplinks
; uplink
; uplink
= next
)
254 free(uplink
->bind_addr
);
259 /* If the uplink hasn't changed, it's either NULL or pointing at
260 an uplink that was just deleted, select a new one. */
261 if(cManager
.uplink
== oldUplink
)
265 irc_squit(self
, "Uplinks updated; selecting new uplink.", NULL
);
268 cManager
.uplink
= NULL
;
274 uplink_find(char *name
)
276 struct uplinkNode
*uplink
;
278 if(!cManager
.enabled
|| !cManager
.uplinks
)
283 for(uplink
= cManager
.uplinks
; uplink
; uplink
= uplink
->next
)
285 if(!strcasecmp(uplink
->name
, name
))
295 uplink_select(char *name
)
297 struct uplinkNode
*start
, *uplink
, *next
;
300 if(!cManager
.enabled
|| !cManager
.uplinks
)
302 log_module(MAIN_LOG
, LOG_FATAL
, "No uplinks enabled; giving up.");
308 start
= cManager
.uplinks
;
312 start
= cManager
.uplink
->next
;
315 start
= cManager
.uplinks
;
320 for(uplink
= start
; uplink
; uplink
= next
)
322 next
= uplink
->next
? uplink
->next
: cManager
.uplinks
;
330 /* We've wrapped around the list. */
333 sleep((cManager
.cycles
>> 1) * 5);
336 if(max_cycles
&& (cManager
.cycles
>= max_cycles
))
338 log_module(MAIN_LOG
, LOG_FATAL
, "Maximum uplink list cycles exceeded; giving up.");
342 /* Give the uplink currently in 'uplink' consideration,
343 and if not selected, break on the next iteration. */
347 /* Skip bad uplinks. */
348 if(uplink
->flags
& UPLINK_UNAVAILABLE
)
353 if(name
&& irccasecmp(uplink
->name
, name
))
355 /* If we were told to connect to a specific uplink, don't stop
361 /* It would be possible to track uplink health through a variety
362 of statistics and only break on the best uplink. For now, break
363 on the first available one.
371 /* We are shit outta luck if every single uplink has been passed
372 over. Use the current uplink if possible. */
373 if(!cManager
.uplink
|| cManager
.uplink
->flags
& UPLINK_UNAVAILABLE
)
375 log_module(MAIN_LOG
, LOG_FATAL
, "All available uplinks exhausted; giving up.");
382 cManager
.uplink
= uplink
;
388 struct uplinkNode
*uplink
= cManager
.uplink
;
390 if(uplink
->state
!= DISCONNECTED
)
395 if(uplink
->flags
& UPLINK_UNAVAILABLE
)
398 uplink
= cManager
.uplink
;
403 /* This delay could scale with the number of tries. */
407 if(!create_socket_client(uplink
))
409 if(uplink
->max_tries
&& (uplink
->tries
>= uplink
->max_tries
))
411 /* This is a bad uplink, move on. */
412 uplink
->flags
|= UPLINK_UNAVAILABLE
;
420 uplink
->state
= AUTHENTICATING
;
421 irc_introduce(uplink
->password
);
430 /* This function is called when a ping is received. Take it as
431 a sign of link health and reset the connection manager
437 void sigaction_writedb(int x
)
439 #ifndef HAVE_STRSIGNAL
440 log_module(MAIN_LOG
, LOG_INFO
, "Signal %d -- writing databases.", x
);
442 log_module(MAIN_LOG
, LOG_INFO
, "%s -- writing databases.", strsignal(x
));
447 void sigaction_exit(int x
)
449 #ifndef HAVE_STRSIGNAL
450 log_module(MAIN_LOG
, LOG_INFO
, "Signal %d -- exiting.", x
);
452 log_module(MAIN_LOG
, LOG_INFO
, "%s -- exiting.", strsignal(x
));
454 irc_squit(self
, "Exiting on signal from console.", NULL
);
458 void sigaction_wait(UNUSED_ARG(int x
))
461 wait4(-1, &code
, WNOHANG
, NULL
);
464 void sigaction_rehash(int x
)
466 #ifndef HAVE_STRSIGNAL
467 log_module(MAIN_LOG
, LOG_INFO
, "Signal %d -- rehashing.", x
);
469 log_module(MAIN_LOG
, LOG_INFO
, "%s -- rehashing.", strsignal(x
));
474 static exit_func_t
*ef_list
;
475 static unsigned int ef_size
= 0, ef_used
= 0;
477 void reg_exit_func(exit_func_t handler
)
479 if (ef_used
== ef_size
) {
482 ef_list
= realloc(ef_list
, ef_size
*sizeof(exit_func_t
));
485 ef_list
= malloc(ef_size
*sizeof(exit_func_t
));
488 ef_list
[ef_used
++] = handler
;
491 void call_exit_funcs(void)
493 unsigned int n
= ef_used
;
495 /* Call them in reverse order because we initialize logs, then
496 * nickserv, then chanserv, etc., and they register their exit
497 * funcs in that order, and there are some dependencies (for
498 * example, ChanServ requires NickServ to not have cleaned up).
505 ef_used
= ef_size
= 0;
509 set_policer_param(const char *param
, void *data
, void *extra
)
511 struct record_data
*rd
= data
;
512 const char *str
= GET_RECORD_QSTRING(rd
);
514 policer_params_set(extra
, param
, str
);
525 god_policer_params
= policer_params_new();
526 if ((dict
= conf_get_data("policers/commands-god", RECDB_OBJECT
))) {
527 dict_foreach(dict
, set_policer_param
, god_policer_params
);
529 policer_params_set(god_policer_params
, "size", "30");
530 policer_params_set(god_policer_params
, "drain-rate", "1");
532 oper_policer_params
= policer_params_new();
533 if ((dict
= conf_get_data("policers/commands-oper", RECDB_OBJECT
))) {
534 dict_foreach(dict
, set_policer_param
, oper_policer_params
);
536 policer_params_set(oper_policer_params
, "size", "10");
537 policer_params_set(oper_policer_params
, "drain-rate", "1");
539 luser_policer_params
= policer_params_new();
540 if ((dict
= conf_get_data("policers/commands-luser", RECDB_OBJECT
))) {
541 dict_foreach(dict
, set_policer_param
, luser_policer_params
);
543 policer_params_set(luser_policer_params
, "size", "5");
544 policer_params_set(luser_policer_params
, "drain-rate", "0.50");
547 info
= conf_get_data("services/opserv/nick", RECDB_QSTRING
);
548 if (info
&& (info
[0] == '.'))
552 info
= conf_get_data("services/global/nick", RECDB_QSTRING
);
553 if (info
&& (info
[0] == '.'))
557 info
= conf_get_data("services/nickserv/nick", RECDB_QSTRING
);
558 if (info
&& (info
[0] == '.'))
562 info
= conf_get_data("services/chanserv/nick", RECDB_QSTRING
);
563 if (info
&& (info
[0] == '.'))
567 info
= conf_get_data("services/spamserv/nick", RECDB_QSTRING
);
568 if (info
&& (info
[0] == '.'))
573 #ifdef HAVE_SYS_RESOURCE_H
576 set_item_rlimit(const char *name
, void *data
, void *extra
)
580 struct record_data
*rd
= data
;
584 rsrc
= (long)dict_find(extra
, name
, &found
);
586 log_module(MAIN_LOG
, LOG_ERROR
, "Invalid rlimit \"%s\" in rlimits section.", name
);
589 if (!(str
= GET_RECORD_QSTRING(rd
))) {
590 log_module(MAIN_LOG
, LOG_ERROR
, "Missing or invalid parameter type for rlimit \"%s\".", name
);
593 if (getrlimit(rsrc
, &rlim
) < 0) {
594 log_module(MAIN_LOG
, LOG_ERROR
, "Couldn't get rlimit \"%s\": errno %d: %s", name
, errno
, strerror(errno
));
597 rlim
.rlim_cur
= ParseVolume(str
);
598 if (setrlimit(rsrc
, &rlim
) < 0) {
599 log_module(MAIN_LOG
, LOG_ERROR
, "Couldn't set rlimit \"%s\": errno %d: %s", name
, errno
, strerror(errno
));
610 dict_insert(values
, "data", (void*)RLIMIT_DATA
);
611 dict_insert(values
, "stack", (void*)RLIMIT_STACK
);
613 dict_insert(values
, "vmem", (void*)RLIMIT_VMEM
);
616 dict_insert(values
, "vmem", (void*)RLIMIT_AS
);
619 if ((dict
= conf_get_data("rlimits", RECDB_OBJECT
))) {
620 dict_foreach(dict
, set_item_rlimit
, values
);
634 void main_shutdown(void)
636 struct uplinkNode
*ul
, *ul_next
;
638 for (ul
= cManager
.uplinks
; ul
; ul
= ul_next
) {
646 policer_params_delete(god_policer_params
);
647 policer_params_delete(oper_policer_params
);
648 policer_params_delete(luser_policer_params
);
653 void usage(char *self
) {
654 /* We can assume we have getopt_long(). */
655 printf("Usage: %s [-c config] [-r log] [-d] [-f] [-v|-h]\n"
656 "-c, --config selects a different configuration file.\n"
657 "-d, --debug enables debug mode.\n"
658 "-f, --foreground run X3 in the foreground.\n"
659 "-h, --help prints this usage message.\n"
660 "-k, --check checks the configuration file's syntax.\n"
661 "-r, --replay replay a log file (for debugging)\n"
662 "-v, --version prints this program's version.\n"
667 printf(" --------------------------------------------------\n"
668 " - "PACKAGE_STRING
", Built: " __DATE__
", " __TIME__
".\n"
669 " - Copyright (C) 2000 - 2005, srvx Development Team\n"
670 " - Copyright (C) 2004 - 2005, X3 Development Team\n"
671 " --------------------------------------------------\n");
676 "This program is free software; you can redistribute it and/or modify\n"
677 "it under the terms of the GNU General Public License as published by\n"
678 "the Free Software Foundation; either version 3 of the License, or\n"
679 "(at your option) any later version.\n"
681 "This program is distributed in the hope that it will be useful,\n"
682 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
683 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
684 "GNU General Public License for more details.\n"
686 "You should have received a copy of the GNU General Public License\n"
687 "along with this program; if not, write to the Free Software\n"
688 "Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\n\n");
691 #if WITH_MALLOC_BOEHM_GC
693 gc_warn_proc(char *msg
, GC_word arg
)
695 log_module(MAIN_LOG
, LOG_ERROR
, "GC(%p): %s", (void*)arg
, msg
);
699 int main(int argc
, char *argv
[])
706 #if WITH_MALLOC_BOEHM_GC
708 GC_set_warn_proc(gc_warn_proc
);
709 GC_enable_incremental();
716 /* set up some signal handlers */
717 memset(&sv
, 0, sizeof(sv
));
718 sigemptyset(&sv
.sa_mask
);
719 sv
.sa_handler
= SIG_IGN
;
720 sigaction(SIGPIPE
, &sv
, NULL
);
721 sv
.sa_handler
= sigaction_rehash
;
722 sigaction(SIGHUP
, &sv
, NULL
);
723 sv
.sa_handler
= sigaction_writedb
;
724 sigaction(SIGINT
, &sv
, NULL
);
725 sv
.sa_handler
= sigaction_exit
;
726 sigaction(SIGQUIT
, &sv
, NULL
);
727 sv
.sa_handler
= sigaction_wait
;
728 sigaction(SIGCHLD
, &sv
, NULL
);
730 if (argc
> 1) { /* parse command line, if any */
732 struct option options
[] =
734 {"config", 1, 0, 'c'},
735 {"debug", 0, 0, 'd'},
736 {"foreground", 0, 0, 'f'},
738 {"check", 0, 0, 'k'},
739 {"replay", 1, 0, 'r'},
740 {"version", 0, 0, 'v'},
741 {"verbose", 0, 0, 'V'},
745 while ((c
= getopt_long(argc
, argv
, "c:kr:dfvVh", options
, NULL
)) != -1) {
748 services_config
= optarg
;
751 if (conf_read(services_config
)) {
752 printf("%s appears to be a valid configuration file.\n", services_config
);
754 printf("%s is an invalid configuration file.\n", services_config
);
758 replay_file
= fopen(optarg
, "r");
760 fprintf(stderr
, "Could not open %s for reading: %s (%d)\n",
761 optarg
, strerror(errno
), errno
);
786 /* We read a line here to "prime" the replay file parser, but
787 * mostly to get the right value of "now" for when we do the
792 boot_time
= time(&now
);
795 log_module(MAIN_LOG
, LOG_INFO
, "Initializing daemon...");
796 if (!conf_read(services_config
)) {
797 log_module(MAIN_LOG
, LOG_FATAL
, "Unable to read %s.", services_config
);
801 conf_register_reload(uplink_compile
);
804 /* Attempt to fork into the background if daemon mode is on. */
807 log_module(MAIN_LOG
, LOG_FATAL
, "Unable to fork: %s", strerror(errno
));
808 } else if (pid
> 0) {
809 log_module(MAIN_LOG
, LOG_INFO
, "Forking into the background (pid: %i)...", pid
);
813 /* Close these since we should not use them from now on. */
819 if ((file_out
= fopen(PID_FILE
, "w")) == NULL
) {
820 /* Create the main process' pid file */
821 log_module(MAIN_LOG
, LOG_ERROR
, "Unable to create PID file: %s", strerror(errno
));
823 fprintf(file_out
, "%i\n", (int)getpid());
827 services_argc
= argc
;
828 services_argv
= argv
;
830 atexit(call_exit_funcs
);
831 reg_exit_func(main_shutdown
);
834 MAIN_LOG
= log_register_type("x3", "file:main.log");
846 conf_globals(); /* initializes the core services */
849 message_register_table(msgtab
);
855 /* The first exit func to be called *should* be saxdb_write_all(). */
856 reg_exit_func(saxdb_write_all
);
859 log_module(MAIN_LOG
, LOG_INFO
, "Beginning replay...");
862 if ((msg
= dict_sanity_check(clients
))) {
863 log_module(MAIN_LOG
, LOG_ERROR
, "Clients insanity: %s", msg
);
866 if ((msg
= dict_sanity_check(channels
))) {
867 log_module(MAIN_LOG
, LOG_ERROR
, "Channels insanity: %s", msg
);
870 if ((msg
= dict_sanity_check(servers
))) {
871 log_module(MAIN_LOG
, LOG_ERROR
, "Servers insanity: %s", msg
);