]>
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 * 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 2 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"
41 #ifdef HAVE_SYS_RESOURCE_H
42 #include <sys/resource.h>
44 #ifdef HAVE_NETINET_IN_H
45 #include <netinet/in.h>
47 #ifdef HAVE_SYS_SOCKET_H
48 #include <sys/socket.h>
50 #ifdef HAVE_SYS_WAIT_H
55 #define SIGCHLD SIGCLD
58 extern FILE *replay_file
;
60 time_t boot_time
, burst_begin
, now
;
61 unsigned long burst_length
;
62 struct log_type
*MAIN_LOG
;
64 int quit_services
, max_cycles
;
66 char *services_config
= "x3.conf";
71 struct cManagerNode cManager
;
73 struct policer_params
*oper_policer_params
, *luser_policer_params
, *god_policer_params
;
75 static const struct message_entry msgtab
[] = {
76 { "MSG_NONE", "None" },
79 { "MSG_NEVER", "Never" },
80 { "MSG_BAR", "----------------------------------------" },
81 { "MSG_SERVICE_IMMUNE", "$b%s$b may not be kicked, killed, banned, or deopped." },
82 { "MSG_SERVICE_PRIVILEGED", "$b%s$b is a privileged service." },
83 { "MSG_NOT_A_SERVICE", "$b%s$b is not a service bot." },
84 { "MSG_COMMAND_UNKNOWN", "$b%s$b is an unknown command." },
85 { "MSG_COMMAND_PRIVILEGED", "$b%s$b is a privileged command." },
86 { "MSG_COMMAND_DISABLED", "$b%s$b is a disabled command." },
87 { "MSG_SETTING_PRIVILEGED", "$b%s$b is a privileged setting." },
88 { "MSG_AUTHENTICATE", "You must first authenticate with $b$N$b." },
89 { "MSG_USER_AUTHENTICATE", "%s must first authenticate with $b$N$b." },
90 { "MSG_SET_EMAIL_ADDR", "You must first set your account's email address. (Contact network staff if you cannot auth to your account.)" },
91 { "MSG_HANDLE_UNKNOWN", "Account $b%s$b has not been registered." },
92 { "MSG_NICK_UNKNOWN", "User with nick $b%s$b does not exist." },
93 { "MSG_CHANNEL_UNKNOWN", "Channel with name $b%s$b does not exist." },
94 { "MSG_SERVER_UNKNOWN", "Server with name $b%s$b does not exist or is not linked." },
95 { "MSG_MODULE_UNKNOWN", "No module has been registered with name $b%s$b." },
96 { "MSG_INVALID_MODES", "$b%s$b is an invalid set of channel modes." },
97 { "MSG_INVALID_GLINE", "Invalid G-line '%s'." },
98 { "MSG_INVALID_DURATION", "Invalid time span '%s'." },
99 { "MSG_NOT_TARGET_NAME", "You must provide the name of a channel or user." },
100 { "MSG_NOT_CHANNEL_NAME", "The channel name you specified is not a valid channel name." },
101 { "MSG_INVALID_CHANNEL", "The channel name you specified does not exist." },
102 { "MSG_CHANNEL_ABSENT", "You aren't currently in $b%s$b." },
103 { "MSG_CHANNEL_USER_ABSENT", "$b%s$b isn't currently in $b%s$b." },
104 { "MSG_MISSING_PARAMS", "$b%s$b requires more parameters." },
105 { "MSG_DEPRECATED_COMMAND", "The $b%s$b command has been deprecated, and will be removed in the future; please use $b%s$b instead." },
106 { "MSG_OPER_SUSPENDED", "Your $b$O$b access has been suspended." },
107 { "MSG_USER_OUTRANKED", "$b%s$b outranks you (command has no effect)." },
108 { "MSG_STUPID_ACCESS_CHANGE", "Please ask someone $belse$b to demote you." },
109 { "MSG_NO_SEARCH_ACCESS", "You do not have enough access to search based on $b%s$b." },
110 { "MSG_INVALID_CRITERIA", "$b%s$b is an invalid search criteria." },
111 { "MSG_MATCH_COUNT", "-----------Found $b%3u$b Matches------------" },
112 { "MSG_NO_MATCHES", "Nothing matched the criteria of your search." },
113 { "MSG_TOPIC_UNKNOWN", "No help on that topic." },
114 { "MSG_INVALID_BINARY", "$b%s$b is an invalid binary value." },
115 { "MSG_INTERNAL_FAILURE", "Your command could not be processed due to an internal failure." },
116 { "MSG_DB_UNKNOWN", "I do not know of a database named %s." },
117 { "MSG_DB_IS_MONDO", "Database %s is in the \"mondo\" database and cannot be written separately." },
118 { "MSG_DB_WRITE_ERROR", "Error while writing database %s." },
119 { "MSG_DB_WROTE_DB", "Wrote database %s (in "FMT_TIME_T
".%06lu seconds)." },
120 { "MSG_DB_WROTE_ALL", "Wrote all databases (in "FMT_TIME_T
".%06lu seconds)." },
122 { "MSG_0_SECONDS", "0 seconds" },
124 { "MSG_YEARS", "y" },
126 { "MSG_WEEKS", "w" },
130 { "MSG_HOURS", "h" },
131 { "MSG_MINUTE", "m" },
132 { "MSG_MINUTES", "m" },
133 { "MSG_SECOND", "s" },
134 { "MSG_SECONDS", "s" },
138 void uplink_select(char *name
);
141 uplink_insert(const char *key
, void *data
, UNUSED_ARG(void *extra
))
143 struct uplinkNode
*uplink
= malloc(sizeof(struct uplinkNode
));
144 struct record_data
*rd
= data
;
147 struct sockaddr_in
*sin
;
155 uplink
->name
= (char *)key
;
156 uplink
->host
= database_get_data(rd
->d
.object
, "address", RECDB_QSTRING
);
158 str
= database_get_data(rd
->d
.object
, "port", RECDB_QSTRING
);
159 uplink
->port
= str
? atoi(str
) : 6667;
160 uplink
->password
= database_get_data(rd
->d
.object
, "password", RECDB_QSTRING
);
161 uplink
->their_password
= database_get_data(rd
->d
.object
, "uplink_password", RECDB_QSTRING
);
163 str
= database_get_data(rd
->d
.object
, "enabled", RECDB_QSTRING
);
166 enabled
= atoi(str
) ? 1 : 0;
169 cManager
.enabled
+= enabled
;
171 str
= database_get_data(rd
->d
.object
, "max_tries", RECDB_QSTRING
);
172 uplink
->max_tries
= str
? atoi(str
) : 3;
173 uplink
->flags
= enabled
? 0 : UPLINK_UNAVAILABLE
;
174 uplink
->state
= DISCONNECTED
;
177 str
= database_get_data(rd
->d
.object
, "bind_address", RECDB_QSTRING
);
178 uplink
->bind_addr_len
= sizeof(*sin
);
179 if (str
&& getipbyname(str
, &addr
))
181 sin
= calloc(1, uplink
->bind_addr_len
);
182 sin
->sin_family
= AF_INET
;
183 sin
->sin_addr
.s_addr
= addr
;
184 uplink
->bind_addr
= sin
;
188 uplink
->bind_addr
= NULL
;
189 uplink
->bind_addr_len
= 0;
192 uplink
->next
= cManager
.uplinks
;
197 cManager
.uplinks
->prev
= uplink
;
200 cManager
.uplinks
= uplink
;
202 /* If the configuration is being reloaded, set the current uplink
203 to the reloaded equivalent, if possible. */
206 && !irccasecmp(uplink
->host
, cManager
.uplink
->host
)
207 && uplink
->port
== cManager
.uplink
->port
)
209 uplink
->state
= cManager
.uplink
->state
;
210 uplink
->tries
= cManager
.uplink
->tries
;
211 cManager
.uplink
= uplink
;
222 struct uplinkNode
*oldUplinks
= NULL
, *oldUplink
= NULL
;
224 /* Save the old uplinks, we'll remove them later. */
225 oldUplink
= cManager
.uplink
;
226 oldUplinks
= cManager
.uplinks
;
228 cycles
= conf_get_data("server/max_cycles", RECDB_QSTRING
);
229 max_cycles
= cycles
? atoi(cycles
) : 30;
230 if(!(conf_node
= conf_get_data("uplinks", RECDB_OBJECT
)))
232 log_module(MAIN_LOG
, LOG_FATAL
, "No uplinks configured; giving up.");
236 cManager
.enabled
= 0;
237 dict_foreach(conf_node
, uplink_insert
, NULL
);
239 /* Remove the old uplinks, if any. It doesn't matter if oldUplink (below)
240 is a reference to one of these, because it won't get dereferenced. */
243 struct uplinkNode
*uplink
, *next
;
245 oldUplinks
->prev
->next
= NULL
;
247 for(uplink
= oldUplinks
; uplink
; uplink
= next
)
250 free(uplink
->bind_addr
);
255 /* If the uplink hasn't changed, it's either NULL or pointing at
256 an uplink that was just deleted, select a new one. */
257 if(cManager
.uplink
== oldUplink
)
261 irc_squit(self
, "Uplinks updated; selecting new uplink.", NULL
);
264 cManager
.uplink
= NULL
;
270 uplink_find(char *name
)
272 struct uplinkNode
*uplink
;
274 if(!cManager
.enabled
|| !cManager
.uplinks
)
279 for(uplink
= cManager
.uplinks
; uplink
; uplink
= uplink
->next
)
281 if(!strcasecmp(uplink
->name
, name
))
291 uplink_select(char *name
)
293 struct uplinkNode
*start
, *uplink
, *next
;
296 if(!cManager
.enabled
|| !cManager
.uplinks
)
298 log_module(MAIN_LOG
, LOG_FATAL
, "No uplinks enabled; giving up.");
304 start
= cManager
.uplinks
;
308 start
= cManager
.uplink
->next
;
311 start
= cManager
.uplinks
;
316 for(uplink
= start
; uplink
; uplink
= next
)
318 next
= uplink
->next
? uplink
->next
: cManager
.uplinks
;
326 /* We've wrapped around the list. */
329 sleep((cManager
.cycles
>> 1) * 5);
332 if(max_cycles
&& (cManager
.cycles
>= max_cycles
))
334 log_module(MAIN_LOG
, LOG_FATAL
, "Maximum uplink list cycles exceeded; giving up.");
338 /* Give the uplink currently in 'uplink' consideration,
339 and if not selected, break on the next iteration. */
343 /* Skip bad uplinks. */
344 if(uplink
->flags
& UPLINK_UNAVAILABLE
)
349 if(name
&& irccasecmp(uplink
->name
, name
))
351 /* If we were told to connect to a specific uplink, don't stop
357 /* It would be possible to track uplink health through a variety
358 of statistics and only break on the best uplink. For now, break
359 on the first available one.
367 /* We are shit outta luck if every single uplink has been passed
368 over. Use the current uplink if possible. */
369 if(!cManager
.uplink
|| cManager
.uplink
->flags
& UPLINK_UNAVAILABLE
)
371 log_module(MAIN_LOG
, LOG_FATAL
, "All available uplinks exhausted; giving up.");
378 cManager
.uplink
= uplink
;
384 struct uplinkNode
*uplink
= cManager
.uplink
;
386 if(uplink
->state
!= DISCONNECTED
)
391 if(uplink
->flags
& UPLINK_UNAVAILABLE
)
394 uplink
= cManager
.uplink
;
399 /* This delay could scale with the number of tries. */
403 if(!create_socket_client(uplink
))
405 if(uplink
->max_tries
&& (uplink
->tries
>= uplink
->max_tries
))
407 /* This is a bad uplink, move on. */
408 uplink
->flags
|= UPLINK_UNAVAILABLE
;
416 uplink
->state
= AUTHENTICATING
;
417 irc_introduce(uplink
->password
);
426 /* This function is called when a ping is received. Take it as
427 a sign of link health and reset the connection manager
433 void sigaction_writedb(int x
)
435 #ifndef HAVE_STRSIGNAL
436 log_module(MAIN_LOG
, LOG_INFO
, "Signal %d -- writing databases.", x
);
438 log_module(MAIN_LOG
, LOG_INFO
, "%s -- writing databases.", strsignal(x
));
443 void sigaction_exit(int x
)
445 #ifndef HAVE_STRSIGNAL
446 log_module(MAIN_LOG
, LOG_INFO
, "Signal %d -- exiting.", x
);
448 log_module(MAIN_LOG
, LOG_INFO
, "%s -- exiting.", strsignal(x
));
450 irc_squit(self
, "Exiting on signal from console.", NULL
);
454 void sigaction_wait(UNUSED_ARG(int x
))
457 wait4(-1, &code
, WNOHANG
, NULL
);
460 void sigaction_rehash(int x
)
462 #ifndef HAVE_STRSIGNAL
463 log_module(MAIN_LOG
, LOG_INFO
, "Signal %d -- rehashing.", x
);
465 log_module(MAIN_LOG
, LOG_INFO
, "%s -- rehashing.", strsignal(x
));
470 static exit_func_t
*ef_list
;
471 static unsigned int ef_size
= 0, ef_used
= 0;
473 void reg_exit_func(exit_func_t handler
)
475 if (ef_used
== ef_size
) {
478 ef_list
= realloc(ef_list
, ef_size
*sizeof(exit_func_t
));
481 ef_list
= malloc(ef_size
*sizeof(exit_func_t
));
484 ef_list
[ef_used
++] = handler
;
487 void call_exit_funcs(void)
489 unsigned int n
= ef_used
;
491 /* Call them in reverse order because we initialize logs, then
492 * nickserv, then chanserv, etc., and they register their exit
493 * funcs in that order, and there are some dependencies (for
494 * example, ChanServ requires NickServ to not have cleaned up).
501 ef_used
= ef_size
= 0;
505 set_policer_param(const char *param
, void *data
, void *extra
)
507 struct record_data
*rd
= data
;
508 const char *str
= GET_RECORD_QSTRING(rd
);
510 policer_params_set(extra
, param
, str
);
521 info
= conf_get_data("services/global/nick", RECDB_QSTRING
);
522 if (info
&& (info
[0] == '.'))
526 info
= conf_get_data("services/nickserv/nick", RECDB_QSTRING
);
527 if (info
&& (info
[0] == '.'))
531 info
= conf_get_data("services/chanserv/nick", RECDB_QSTRING
);
532 if (info
&& (info
[0] == '.'))
536 god_policer_params
= policer_params_new();
537 if ((dict
= conf_get_data("policers/commands-god", RECDB_OBJECT
))) {
538 dict_foreach(dict
, set_policer_param
, god_policer_params
);
540 policer_params_set(god_policer_params
, "size", "30");
541 policer_params_set(god_policer_params
, "drain-rate", "1");
543 oper_policer_params
= policer_params_new();
544 if ((dict
= conf_get_data("policers/commands-oper", RECDB_OBJECT
))) {
545 dict_foreach(dict
, set_policer_param
, oper_policer_params
);
547 policer_params_set(oper_policer_params
, "size", "10");
548 policer_params_set(oper_policer_params
, "drain-rate", "1");
550 luser_policer_params
= policer_params_new();
551 if ((dict
= conf_get_data("policers/commands-luser", RECDB_OBJECT
))) {
552 dict_foreach(dict
, set_policer_param
, luser_policer_params
);
554 policer_params_set(luser_policer_params
, "size", "5");
555 policer_params_set(luser_policer_params
, "drain-rate", "0.50");
558 info
= conf_get_data("services/opserv/nick", RECDB_QSTRING
);
559 if (info
&& (info
[0] == '.'))
564 #ifdef HAVE_SYS_RESOURCE_H
567 set_item_rlimit(const char *name
, void *data
, void *extra
)
570 struct record_data
*rd
= data
;
574 rsrc
= (int)dict_find(extra
, name
, &found
);
576 log_module(MAIN_LOG
, LOG_ERROR
, "Invalid rlimit \"%s\" in rlimits section.", name
);
579 if (!(str
= GET_RECORD_QSTRING(rd
))) {
580 log_module(MAIN_LOG
, LOG_ERROR
, "Missing or invalid parameter type for rlimit \"%s\".", name
);
583 if (getrlimit(rsrc
, &rlim
) < 0) {
584 log_module(MAIN_LOG
, LOG_ERROR
, "Couldn't get rlimit \"%s\": errno %d: %s", name
, errno
, strerror(errno
));
587 rlim
.rlim_cur
= ParseVolume(str
);
588 if (setrlimit(rsrc
, &rlim
) < 0) {
589 log_module(MAIN_LOG
, LOG_ERROR
, "Couldn't set rlimit \"%s\": errno %d: %s", name
, errno
, strerror(errno
));
600 dict_insert(values
, "data", (void*)RLIMIT_DATA
);
601 dict_insert(values
, "stack", (void*)RLIMIT_STACK
);
603 dict_insert(values
, "vmem", (void*)RLIMIT_VMEM
);
606 dict_insert(values
, "vmem", (void*)RLIMIT_AS
);
609 if ((dict
= conf_get_data("rlimits", RECDB_OBJECT
))) {
610 dict_foreach(dict
, set_item_rlimit
, values
);
624 void main_shutdown(void)
626 struct uplinkNode
*ul
, *ul_next
;
628 for (ul
= cManager
.uplinks
; ul
; ul
= ul_next
) {
636 policer_params_delete(god_policer_params
);
637 policer_params_delete(oper_policer_params
);
638 policer_params_delete(luser_policer_params
);
643 void usage(char *self
) {
644 /* We can assume we have getopt_long(). */
645 printf("Usage: %s [-c config] [-r log] [-d] [-f] [-v|-h]\n"
646 "-c, --config selects a different configuration file.\n"
647 "-d, --debug enables debug mode.\n"
648 "-f, --foreground run X3 in the foreground.\n"
649 "-h, --help prints this usage message.\n"
650 "-k, --check checks the configuration file's syntax.\n"
651 "-r, --replay replay a log file (for debugging)\n"
652 "-v, --version prints this program's version.\n"
657 printf(" --------------------------------------------------\n"
658 " - "PACKAGE_STRING
", Built: " __DATE__
", " __TIME__
".\n"
659 " - Copyright (C) 2000 - 2005, srvx Development Team\n"
660 " - Copyright (C) 2004 - 2005, X3 Development Team\n"
661 " --------------------------------------------------\n");
666 "This program is free software; you can redistribute it and/or modify\n"
667 "it under the terms of the GNU General Public License as published by\n"
668 "the Free Software Foundation; either version 2 of the License, or\n"
669 "(at your option) any later version.\n"
671 "This program is distributed in the hope that it will be useful,\n"
672 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
673 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
674 "GNU General Public License for more details.\n"
676 "You should have received a copy of the GNU General Public License\n"
677 "along with this program; if not, write to the Free Software\n"
678 "Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\n\n");
681 #if WITH_MALLOC_BOEHM_GC
683 gc_warn_proc(char *msg
, GC_word arg
)
685 log_module(MAIN_LOG
, LOG_ERROR
, "GC(%p): %s", (void*)arg
, msg
);
689 int main(int argc
, char *argv
[])
696 #if WITH_MALLOC_BOEHM_GC
698 GC_set_warn_proc(gc_warn_proc
);
699 GC_enable_incremental();
706 /* set up some signal handlers */
707 memset(&sv
, 0, sizeof(sv
));
708 sigemptyset(&sv
.sa_mask
);
709 sv
.sa_handler
= SIG_IGN
;
710 sigaction(SIGPIPE
, &sv
, NULL
);
711 sv
.sa_handler
= sigaction_rehash
;
712 sigaction(SIGHUP
, &sv
, NULL
);
713 sv
.sa_handler
= sigaction_writedb
;
714 sigaction(SIGINT
, &sv
, NULL
);
715 sv
.sa_handler
= sigaction_exit
;
716 sigaction(SIGQUIT
, &sv
, NULL
);
717 sv
.sa_handler
= sigaction_wait
;
718 sigaction(SIGCHLD
, &sv
, NULL
);
720 if (argc
> 1) { /* parse command line, if any */
722 struct option options
[] =
724 {"config", 1, 0, 'c'},
725 {"debug", 0, 0, 'd'},
726 {"foreground", 0, 0, 'f'},
728 {"check", 0, 0, 'k'},
729 {"replay", 1, 0, 'r'},
730 {"version", 0, 0, 'v'},
731 {"verbose", 0, 0, 'V'},
735 while ((c
= getopt_long(argc
, argv
, "c:kr:dfvVh", options
, NULL
)) != -1) {
738 services_config
= optarg
;
741 if (conf_read(services_config
)) {
742 printf("%s appears to be a valid configuration file.\n", services_config
);
744 printf("%s is an invalid configuration file.\n", services_config
);
748 replay_file
= fopen(optarg
, "r");
750 fprintf(stderr
, "Could not open %s for reading: %s (%d)\n",
751 optarg
, strerror(errno
), errno
);
776 /* We read a line here to "prime" the replay file parser, but
777 * mostly to get the right value of "now" for when we do the
782 boot_time
= time(&now
);
785 log_module(MAIN_LOG
, LOG_INFO
, "Initializing daemon...");
786 if (!conf_read(services_config
)) {
787 log_module(MAIN_LOG
, LOG_FATAL
, "Unable to read %s.", services_config
);
791 conf_register_reload(uplink_compile
);
794 /* Attempt to fork into the background if daemon mode is on. */
797 log_module(MAIN_LOG
, LOG_FATAL
, "Unable to fork: %s", strerror(errno
));
798 } else if (pid
> 0) {
799 log_module(MAIN_LOG
, LOG_INFO
, "Forking into the background (pid: %i)...", pid
);
803 /* Close these since we should not use them from now on. */
809 if ((file_out
= fopen(PID_FILE
, "w")) == NULL
) {
810 /* Create the main process' pid file */
811 log_module(MAIN_LOG
, LOG_ERROR
, "Unable to create PID file: %s", strerror(errno
));
813 fprintf(file_out
, "%i\n", (int)getpid());
817 services_argc
= argc
;
818 services_argv
= argv
;
820 atexit(call_exit_funcs
);
821 reg_exit_func(main_shutdown
);
824 MAIN_LOG
= log_register_type("x3", "file:main.log");
835 conf_globals(); /* initializes the core services */
838 message_register_table(msgtab
);
844 /* The first exit func to be called *should* be saxdb_write_all(). */
845 reg_exit_func(saxdb_write_all
);
848 log_module(MAIN_LOG
, LOG_INFO
, "Beginning replay...");
851 if ((msg
= dict_sanity_check(clients
))) {
852 log_module(MAIN_LOG
, LOG_ERROR
, "Clients insanity: %s", msg
);
855 if ((msg
= dict_sanity_check(channels
))) {
856 log_module(MAIN_LOG
, LOG_ERROR
, "Channels insanity: %s", msg
);
859 if ((msg
= dict_sanity_check(servers
))) {
860 log_module(MAIN_LOG
, LOG_ERROR
, "Servers insanity: %s", msg
);