]>
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"
42 #ifdef HAVE_SYS_RESOURCE_H
43 #include <sys/resource.h>
45 #ifdef HAVE_NETINET_IN_H
46 #include <netinet/in.h>
48 #ifdef HAVE_SYS_SOCKET_H
49 #include <sys/socket.h>
51 #ifdef HAVE_SYS_WAIT_H
56 #define SIGCHLD SIGCLD
59 extern FILE *replay_file
;
61 time_t boot_time
, burst_begin
, now
;
62 unsigned long burst_length
;
63 struct log_type
*MAIN_LOG
;
65 int quit_services
, max_cycles
;
67 char *services_config
= "x3.conf";
72 struct cManagerNode cManager
;
74 struct policer_params
*oper_policer_params
, *luser_policer_params
, *god_policer_params
;
76 static const struct message_entry msgtab
[] = {
77 { "MSG_NONE", "None" },
80 { "MSG_NEVER", "Never" },
81 { "MSG_BAR", "----------------------------------------" },
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'." },
99 { "MSG_INVALID_SHUN", "Invalid Shun '%s'." },
100 { "MSG_INVALID_DURATION", "Invalid time span '%s'." },
101 { "MSG_NOT_TARGET_NAME", "You must provide the name of a channel or user." },
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." },
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." },
113 { "MSG_MATCH_COUNT", "-----------Found $b%3u$b Matches------------" },
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)." },
124 { "MSG_0_SECONDS", "0 seconds" },
126 { "MSG_YEARS", "y" },
128 { "MSG_WEEKS", "w" },
132 { "MSG_HOURS", "h" },
133 { "MSG_MINUTE", "m" },
134 { "MSG_MINUTES", "m" },
135 { "MSG_SECOND", "s" },
136 { "MSG_SECONDS", "s" },
140 void uplink_select(char *name
);
143 uplink_insert(const char *key
, void *data
, UNUSED_ARG(void *extra
))
145 struct uplinkNode
*uplink
= malloc(sizeof(struct uplinkNode
));
146 struct record_data
*rd
= data
;
149 struct sockaddr_in
*sin
;
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 uplink
->bind_addr_len
= sizeof(*sin
);
181 if (str
&& getipbyname(str
, &addr
))
183 sin
= calloc(1, uplink
->bind_addr_len
);
184 sin
->sin_family
= AF_INET
;
185 sin
->sin_addr
.s_addr
= addr
;
186 uplink
->bind_addr
= sin
;
190 uplink
->bind_addr
= NULL
;
191 uplink
->bind_addr_len
= 0;
194 uplink
->next
= cManager
.uplinks
;
199 cManager
.uplinks
->prev
= uplink
;
202 cManager
.uplinks
= uplink
;
204 /* If the configuration is being reloaded, set the current uplink
205 to the reloaded equivalent, if possible. */
208 && !irccasecmp(uplink
->host
, cManager
.uplink
->host
)
209 && uplink
->port
== cManager
.uplink
->port
)
211 uplink
->state
= cManager
.uplink
->state
;
212 uplink
->tries
= cManager
.uplink
->tries
;
213 cManager
.uplink
= uplink
;
224 struct uplinkNode
*oldUplinks
= NULL
, *oldUplink
= NULL
;
226 /* Save the old uplinks, we'll remove them later. */
227 oldUplink
= cManager
.uplink
;
228 oldUplinks
= cManager
.uplinks
;
230 cycles
= conf_get_data("server/max_cycles", RECDB_QSTRING
);
231 max_cycles
= cycles
? atoi(cycles
) : 30;
232 if(!(conf_node
= conf_get_data("uplinks", RECDB_OBJECT
)))
234 log_module(MAIN_LOG
, LOG_FATAL
, "No uplinks configured; giving up.");
238 cManager
.enabled
= 0;
239 dict_foreach(conf_node
, uplink_insert
, NULL
);
241 /* Remove the old uplinks, if any. It doesn't matter if oldUplink (below)
242 is a reference to one of these, because it won't get dereferenced. */
245 struct uplinkNode
*uplink
, *next
;
247 oldUplinks
->prev
->next
= NULL
;
249 for(uplink
= oldUplinks
; uplink
; uplink
= next
)
252 free(uplink
->bind_addr
);
257 /* If the uplink hasn't changed, it's either NULL or pointing at
258 an uplink that was just deleted, select a new one. */
259 if(cManager
.uplink
== oldUplink
)
263 irc_squit(self
, "Uplinks updated; selecting new uplink.", NULL
);
266 cManager
.uplink
= NULL
;
272 uplink_find(char *name
)
274 struct uplinkNode
*uplink
;
276 if(!cManager
.enabled
|| !cManager
.uplinks
)
281 for(uplink
= cManager
.uplinks
; uplink
; uplink
= uplink
->next
)
283 if(!strcasecmp(uplink
->name
, name
))
293 uplink_select(char *name
)
295 struct uplinkNode
*start
, *uplink
, *next
;
298 if(!cManager
.enabled
|| !cManager
.uplinks
)
300 log_module(MAIN_LOG
, LOG_FATAL
, "No uplinks enabled; giving up.");
306 start
= cManager
.uplinks
;
310 start
= cManager
.uplink
->next
;
313 start
= cManager
.uplinks
;
318 for(uplink
= start
; uplink
; uplink
= next
)
320 next
= uplink
->next
? uplink
->next
: cManager
.uplinks
;
328 /* We've wrapped around the list. */
331 sleep((cManager
.cycles
>> 1) * 5);
334 if(max_cycles
&& (cManager
.cycles
>= max_cycles
))
336 log_module(MAIN_LOG
, LOG_FATAL
, "Maximum uplink list cycles exceeded; giving up.");
340 /* Give the uplink currently in 'uplink' consideration,
341 and if not selected, break on the next iteration. */
345 /* Skip bad uplinks. */
346 if(uplink
->flags
& UPLINK_UNAVAILABLE
)
351 if(name
&& irccasecmp(uplink
->name
, name
))
353 /* If we were told to connect to a specific uplink, don't stop
359 /* It would be possible to track uplink health through a variety
360 of statistics and only break on the best uplink. For now, break
361 on the first available one.
369 /* We are shit outta luck if every single uplink has been passed
370 over. Use the current uplink if possible. */
371 if(!cManager
.uplink
|| cManager
.uplink
->flags
& UPLINK_UNAVAILABLE
)
373 log_module(MAIN_LOG
, LOG_FATAL
, "All available uplinks exhausted; giving up.");
380 cManager
.uplink
= uplink
;
386 struct uplinkNode
*uplink
= cManager
.uplink
;
388 if(uplink
->state
!= DISCONNECTED
)
393 if(uplink
->flags
& UPLINK_UNAVAILABLE
)
396 uplink
= cManager
.uplink
;
401 /* This delay could scale with the number of tries. */
405 if(!create_socket_client(uplink
))
407 if(uplink
->max_tries
&& (uplink
->tries
>= uplink
->max_tries
))
409 /* This is a bad uplink, move on. */
410 uplink
->flags
|= UPLINK_UNAVAILABLE
;
418 uplink
->state
= AUTHENTICATING
;
419 irc_introduce(uplink
->password
);
428 /* This function is called when a ping is received. Take it as
429 a sign of link health and reset the connection manager
435 void sigaction_writedb(int x
)
437 #ifndef HAVE_STRSIGNAL
438 log_module(MAIN_LOG
, LOG_INFO
, "Signal %d -- writing databases.", x
);
440 log_module(MAIN_LOG
, LOG_INFO
, "%s -- writing databases.", strsignal(x
));
445 void sigaction_exit(int x
)
447 #ifndef HAVE_STRSIGNAL
448 log_module(MAIN_LOG
, LOG_INFO
, "Signal %d -- exiting.", x
);
450 log_module(MAIN_LOG
, LOG_INFO
, "%s -- exiting.", strsignal(x
));
452 irc_squit(self
, "Exiting on signal from console.", NULL
);
456 void sigaction_wait(UNUSED_ARG(int x
))
459 wait4(-1, &code
, WNOHANG
, NULL
);
462 void sigaction_rehash(int x
)
464 #ifndef HAVE_STRSIGNAL
465 log_module(MAIN_LOG
, LOG_INFO
, "Signal %d -- rehashing.", x
);
467 log_module(MAIN_LOG
, LOG_INFO
, "%s -- rehashing.", strsignal(x
));
472 static exit_func_t
*ef_list
;
473 static unsigned int ef_size
= 0, ef_used
= 0;
475 void reg_exit_func(exit_func_t handler
)
477 if (ef_used
== ef_size
) {
480 ef_list
= realloc(ef_list
, ef_size
*sizeof(exit_func_t
));
483 ef_list
= malloc(ef_size
*sizeof(exit_func_t
));
486 ef_list
[ef_used
++] = handler
;
489 void call_exit_funcs(void)
491 unsigned int n
= ef_used
;
493 /* Call them in reverse order because we initialize logs, then
494 * nickserv, then chanserv, etc., and they register their exit
495 * funcs in that order, and there are some dependencies (for
496 * example, ChanServ requires NickServ to not have cleaned up).
503 ef_used
= ef_size
= 0;
507 set_policer_param(const char *param
, void *data
, void *extra
)
509 struct record_data
*rd
= data
;
510 const char *str
= GET_RECORD_QSTRING(rd
);
512 policer_params_set(extra
, param
, str
);
523 info
= conf_get_data("services/global/nick", RECDB_QSTRING
);
524 if (info
&& (info
[0] == '.'))
528 info
= conf_get_data("services/nickserv/nick", RECDB_QSTRING
);
529 if (info
&& (info
[0] == '.'))
533 info
= conf_get_data("services/chanserv/nick", RECDB_QSTRING
);
534 if (info
&& (info
[0] == '.'))
538 god_policer_params
= policer_params_new();
539 if ((dict
= conf_get_data("policers/commands-god", RECDB_OBJECT
))) {
540 dict_foreach(dict
, set_policer_param
, god_policer_params
);
542 policer_params_set(god_policer_params
, "size", "30");
543 policer_params_set(god_policer_params
, "drain-rate", "1");
545 oper_policer_params
= policer_params_new();
546 if ((dict
= conf_get_data("policers/commands-oper", RECDB_OBJECT
))) {
547 dict_foreach(dict
, set_policer_param
, oper_policer_params
);
549 policer_params_set(oper_policer_params
, "size", "10");
550 policer_params_set(oper_policer_params
, "drain-rate", "1");
552 luser_policer_params
= policer_params_new();
553 if ((dict
= conf_get_data("policers/commands-luser", RECDB_OBJECT
))) {
554 dict_foreach(dict
, set_policer_param
, luser_policer_params
);
556 policer_params_set(luser_policer_params
, "size", "5");
557 policer_params_set(luser_policer_params
, "drain-rate", "0.50");
560 info
= conf_get_data("services/opserv/nick", RECDB_QSTRING
);
561 if (info
&& (info
[0] == '.'))
566 #ifdef HAVE_SYS_RESOURCE_H
569 set_item_rlimit(const char *name
, void *data
, void *extra
)
572 struct record_data
*rd
= data
;
576 rsrc
= (int)dict_find(extra
, name
, &found
);
578 log_module(MAIN_LOG
, LOG_ERROR
, "Invalid rlimit \"%s\" in rlimits section.", name
);
581 if (!(str
= GET_RECORD_QSTRING(rd
))) {
582 log_module(MAIN_LOG
, LOG_ERROR
, "Missing or invalid parameter type for rlimit \"%s\".", name
);
585 if (getrlimit(rsrc
, &rlim
) < 0) {
586 log_module(MAIN_LOG
, LOG_ERROR
, "Couldn't get rlimit \"%s\": errno %d: %s", name
, errno
, strerror(errno
));
589 rlim
.rlim_cur
= ParseVolume(str
);
590 if (setrlimit(rsrc
, &rlim
) < 0) {
591 log_module(MAIN_LOG
, LOG_ERROR
, "Couldn't set rlimit \"%s\": errno %d: %s", name
, errno
, strerror(errno
));
602 dict_insert(values
, "data", (void*)RLIMIT_DATA
);
603 dict_insert(values
, "stack", (void*)RLIMIT_STACK
);
605 dict_insert(values
, "vmem", (void*)RLIMIT_VMEM
);
608 dict_insert(values
, "vmem", (void*)RLIMIT_AS
);
611 if ((dict
= conf_get_data("rlimits", RECDB_OBJECT
))) {
612 dict_foreach(dict
, set_item_rlimit
, values
);
626 void main_shutdown(void)
628 struct uplinkNode
*ul
, *ul_next
;
630 for (ul
= cManager
.uplinks
; ul
; ul
= ul_next
) {
638 policer_params_delete(god_policer_params
);
639 policer_params_delete(oper_policer_params
);
640 policer_params_delete(luser_policer_params
);
645 void usage(char *self
) {
646 /* We can assume we have getopt_long(). */
647 printf("Usage: %s [-c config] [-r log] [-d] [-f] [-v|-h]\n"
648 "-c, --config selects a different configuration file.\n"
649 "-d, --debug enables debug mode.\n"
650 "-f, --foreground run X3 in the foreground.\n"
651 "-h, --help prints this usage message.\n"
652 "-k, --check checks the configuration file's syntax.\n"
653 "-r, --replay replay a log file (for debugging)\n"
654 "-v, --version prints this program's version.\n"
659 printf(" --------------------------------------------------\n"
660 " - "PACKAGE_STRING
", Built: " __DATE__
", " __TIME__
".\n"
661 " - Copyright (C) 2000 - 2005, srvx Development Team\n"
662 " - Copyright (C) 2004 - 2005, X3 Development Team\n"
663 " --------------------------------------------------\n");
668 "This program is free software; you can redistribute it and/or modify\n"
669 "it under the terms of the GNU General Public License as published by\n"
670 "the Free Software Foundation; either version 2 of the License, or\n"
671 "(at your option) any later version.\n"
673 "This program is distributed in the hope that it will be useful,\n"
674 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
675 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
676 "GNU General Public License for more details.\n"
678 "You should have received a copy of the GNU General Public License\n"
679 "along with this program; if not, write to the Free Software\n"
680 "Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\n\n");
683 #if WITH_MALLOC_BOEHM_GC
685 gc_warn_proc(char *msg
, GC_word arg
)
687 log_module(MAIN_LOG
, LOG_ERROR
, "GC(%p): %s", (void*)arg
, msg
);
691 int main(int argc
, char *argv
[])
698 #if WITH_MALLOC_BOEHM_GC
700 GC_set_warn_proc(gc_warn_proc
);
701 GC_enable_incremental();
708 /* set up some signal handlers */
709 memset(&sv
, 0, sizeof(sv
));
710 sigemptyset(&sv
.sa_mask
);
711 sv
.sa_handler
= SIG_IGN
;
712 sigaction(SIGPIPE
, &sv
, NULL
);
713 sv
.sa_handler
= sigaction_rehash
;
714 sigaction(SIGHUP
, &sv
, NULL
);
715 sv
.sa_handler
= sigaction_writedb
;
716 sigaction(SIGINT
, &sv
, NULL
);
717 sv
.sa_handler
= sigaction_exit
;
718 sigaction(SIGQUIT
, &sv
, NULL
);
719 sv
.sa_handler
= sigaction_wait
;
720 sigaction(SIGCHLD
, &sv
, NULL
);
722 if (argc
> 1) { /* parse command line, if any */
724 struct option options
[] =
726 {"config", 1, 0, 'c'},
727 {"debug", 0, 0, 'd'},
728 {"foreground", 0, 0, 'f'},
730 {"check", 0, 0, 'k'},
731 {"replay", 1, 0, 'r'},
732 {"version", 0, 0, 'v'},
733 {"verbose", 0, 0, 'V'},
737 while ((c
= getopt_long(argc
, argv
, "c:kr:dfvVh", options
, NULL
)) != -1) {
740 services_config
= optarg
;
743 if (conf_read(services_config
)) {
744 printf("%s appears to be a valid configuration file.\n", services_config
);
746 printf("%s is an invalid configuration file.\n", services_config
);
750 replay_file
= fopen(optarg
, "r");
752 fprintf(stderr
, "Could not open %s for reading: %s (%d)\n",
753 optarg
, strerror(errno
), errno
);
778 /* We read a line here to "prime" the replay file parser, but
779 * mostly to get the right value of "now" for when we do the
784 boot_time
= time(&now
);
787 log_module(MAIN_LOG
, LOG_INFO
, "Initializing daemon...");
788 if (!conf_read(services_config
)) {
789 log_module(MAIN_LOG
, LOG_FATAL
, "Unable to read %s.", services_config
);
793 conf_register_reload(uplink_compile
);
796 /* Attempt to fork into the background if daemon mode is on. */
799 log_module(MAIN_LOG
, LOG_FATAL
, "Unable to fork: %s", strerror(errno
));
800 } else if (pid
> 0) {
801 log_module(MAIN_LOG
, LOG_INFO
, "Forking into the background (pid: %i)...", pid
);
805 /* Close these since we should not use them from now on. */
811 if ((file_out
= fopen(PID_FILE
, "w")) == NULL
) {
812 /* Create the main process' pid file */
813 log_module(MAIN_LOG
, LOG_ERROR
, "Unable to create PID file: %s", strerror(errno
));
815 fprintf(file_out
, "%i\n", (int)getpid());
819 services_argc
= argc
;
820 services_argv
= argv
;
822 atexit(call_exit_funcs
);
823 reg_exit_func(main_shutdown
);
826 MAIN_LOG
= log_register_type("x3", "file:main.log");
838 conf_globals(); /* initializes the core services */
841 message_register_table(msgtab
);
847 /* The first exit func to be called *should* be saxdb_write_all(). */
848 reg_exit_func(saxdb_write_all
);
851 log_module(MAIN_LOG
, LOG_INFO
, "Beginning replay...");
854 if ((msg
= dict_sanity_check(clients
))) {
855 log_module(MAIN_LOG
, LOG_ERROR
, "Clients insanity: %s", msg
);
858 if ((msg
= dict_sanity_check(channels
))) {
859 log_module(MAIN_LOG
, LOG_ERROR
, "Channels insanity: %s", msg
);
862 if ((msg
= dict_sanity_check(servers
))) {
863 log_module(MAIN_LOG
, LOG_ERROR
, "Servers insanity: %s", msg
);