]>
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
;
147 struct addrinfo hints
, *ai
;
156 uplink
->name
= (char *)key
;
157 uplink
->host
= database_get_data(rd
->d
.object
, "address", RECDB_QSTRING
);
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
);
164 str
= database_get_data(rd
->d
.object
, "enabled", RECDB_QSTRING
);
167 enabled
= atoi(str
) ? 1 : 0;
170 cManager
.enabled
+= enabled
;
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
;
178 str
= database_get_data(rd
->d
.object
, "bind_address", RECDB_QSTRING
);
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
))
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
);
191 uplink
->bind_addr
= NULL
;
192 uplink
->bind_addr_len
= 0;
195 uplink
->next
= cManager
.uplinks
;
200 cManager
.uplinks
->prev
= uplink
;
203 cManager
.uplinks
= uplink
;
205 /* If the configuration is being reloaded, set the current uplink
206 to the reloaded equivalent, if possible. */
209 && !irccasecmp(uplink
->host
, cManager
.uplink
->host
)
210 && uplink
->port
== cManager
.uplink
->port
)
212 uplink
->state
= cManager
.uplink
->state
;
213 uplink
->tries
= cManager
.uplink
->tries
;
214 cManager
.uplink
= uplink
;
225 struct uplinkNode
*oldUplinks
= NULL
, *oldUplink
= NULL
;
227 /* Save the old uplinks, we'll remove them later. */
228 oldUplink
= cManager
.uplink
;
229 oldUplinks
= cManager
.uplinks
;
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
)))
235 log_module(MAIN_LOG
, LOG_FATAL
, "No uplinks configured; giving up.");
239 cManager
.enabled
= 0;
240 dict_foreach(conf_node
, uplink_insert
, NULL
);
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. */
246 struct uplinkNode
*uplink
, *next
;
248 oldUplinks
->prev
->next
= NULL
;
250 for(uplink
= oldUplinks
; uplink
; uplink
= next
)
253 free(uplink
->bind_addr
);
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
)
264 irc_squit(self
, "Uplinks updated; selecting new uplink.", NULL
);
267 cManager
.uplink
= NULL
;
273 uplink_find(char *name
)
275 struct uplinkNode
*uplink
;
277 if(!cManager
.enabled
|| !cManager
.uplinks
)
282 for(uplink
= cManager
.uplinks
; uplink
; uplink
= uplink
->next
)
284 if(!strcasecmp(uplink
->name
, name
))
294 uplink_select(char *name
)
296 struct uplinkNode
*start
, *uplink
, *next
;
299 if(!cManager
.enabled
|| !cManager
.uplinks
)
301 log_module(MAIN_LOG
, LOG_FATAL
, "No uplinks enabled; giving up.");
307 start
= cManager
.uplinks
;
311 start
= cManager
.uplink
->next
;
314 start
= cManager
.uplinks
;
319 for(uplink
= start
; uplink
; uplink
= next
)
321 next
= uplink
->next
? uplink
->next
: cManager
.uplinks
;
329 /* We've wrapped around the list. */
332 sleep((cManager
.cycles
>> 1) * 5);
335 if(max_cycles
&& (cManager
.cycles
>= max_cycles
))
337 log_module(MAIN_LOG
, LOG_FATAL
, "Maximum uplink list cycles exceeded; giving up.");
341 /* Give the uplink currently in 'uplink' consideration,
342 and if not selected, break on the next iteration. */
346 /* Skip bad uplinks. */
347 if(uplink
->flags
& UPLINK_UNAVAILABLE
)
352 if(name
&& irccasecmp(uplink
->name
, name
))
354 /* If we were told to connect to a specific uplink, don't stop
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.
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
)
374 log_module(MAIN_LOG
, LOG_FATAL
, "All available uplinks exhausted; giving up.");
381 cManager
.uplink
= uplink
;
387 struct uplinkNode
*uplink
= cManager
.uplink
;
389 if(uplink
->state
!= DISCONNECTED
)
394 if(uplink
->flags
& UPLINK_UNAVAILABLE
)
397 uplink
= cManager
.uplink
;
402 /* This delay could scale with the number of tries. */
406 if(!create_socket_client(uplink
))
408 if(uplink
->max_tries
&& (uplink
->tries
>= uplink
->max_tries
))
410 /* This is a bad uplink, move on. */
411 uplink
->flags
|= UPLINK_UNAVAILABLE
;
419 uplink
->state
= AUTHENTICATING
;
420 irc_introduce(uplink
->password
);
429 /* This function is called when a ping is received. Take it as
430 a sign of link health and reset the connection manager
436 void sigaction_writedb(int x
)
438 #ifndef HAVE_STRSIGNAL
439 log_module(MAIN_LOG
, LOG_INFO
, "Signal %d -- writing databases.", x
);
441 log_module(MAIN_LOG
, LOG_INFO
, "%s -- writing databases.", strsignal(x
));
446 void sigaction_exit(int x
)
448 #ifndef HAVE_STRSIGNAL
449 log_module(MAIN_LOG
, LOG_INFO
, "Signal %d -- exiting.", x
);
451 log_module(MAIN_LOG
, LOG_INFO
, "%s -- exiting.", strsignal(x
));
453 irc_squit(self
, "Exiting on signal from console.", NULL
);
457 void sigaction_wait(UNUSED_ARG(int x
))
460 wait4(-1, &code
, WNOHANG
, NULL
);
463 void sigaction_rehash(int x
)
465 #ifndef HAVE_STRSIGNAL
466 log_module(MAIN_LOG
, LOG_INFO
, "Signal %d -- rehashing.", x
);
468 log_module(MAIN_LOG
, LOG_INFO
, "%s -- rehashing.", strsignal(x
));
473 static exit_func_t
*ef_list
;
474 static unsigned int ef_size
= 0, ef_used
= 0;
476 void reg_exit_func(exit_func_t handler
)
478 if (ef_used
== ef_size
) {
481 ef_list
= realloc(ef_list
, ef_size
*sizeof(exit_func_t
));
484 ef_list
= malloc(ef_size
*sizeof(exit_func_t
));
487 ef_list
[ef_used
++] = handler
;
490 void call_exit_funcs(void)
492 unsigned int n
= ef_used
;
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).
504 ef_used
= ef_size
= 0;
508 set_policer_param(const char *param
, void *data
, void *extra
)
510 struct record_data
*rd
= data
;
511 const char *str
= GET_RECORD_QSTRING(rd
);
513 policer_params_set(extra
, param
, str
);
524 info
= conf_get_data("services/global/nick", RECDB_QSTRING
);
525 if (info
&& (info
[0] == '.'))
529 info
= conf_get_data("services/nickserv/nick", RECDB_QSTRING
);
530 if (info
&& (info
[0] == '.'))
534 info
= conf_get_data("services/chanserv/nick", RECDB_QSTRING
);
535 if (info
&& (info
[0] == '.'))
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
);
543 policer_params_set(god_policer_params
, "size", "30");
544 policer_params_set(god_policer_params
, "drain-rate", "1");
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
);
550 policer_params_set(oper_policer_params
, "size", "10");
551 policer_params_set(oper_policer_params
, "drain-rate", "1");
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
);
557 policer_params_set(luser_policer_params
, "size", "5");
558 policer_params_set(luser_policer_params
, "drain-rate", "0.50");
561 info
= conf_get_data("services/opserv/nick", RECDB_QSTRING
);
562 if (info
&& (info
[0] == '.'))
567 #ifdef HAVE_SYS_RESOURCE_H
570 set_item_rlimit(const char *name
, void *data
, void *extra
)
574 struct record_data
*rd
= data
;
578 rsrc
= (long)dict_find(extra
, name
, &found
);
580 log_module(MAIN_LOG
, LOG_ERROR
, "Invalid rlimit \"%s\" in rlimits section.", name
);
583 if (!(str
= GET_RECORD_QSTRING(rd
))) {
584 log_module(MAIN_LOG
, LOG_ERROR
, "Missing or invalid parameter type for rlimit \"%s\".", name
);
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
));
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
));
604 dict_insert(values
, "data", (void*)RLIMIT_DATA
);
605 dict_insert(values
, "stack", (void*)RLIMIT_STACK
);
607 dict_insert(values
, "vmem", (void*)RLIMIT_VMEM
);
610 dict_insert(values
, "vmem", (void*)RLIMIT_AS
);
613 if ((dict
= conf_get_data("rlimits", RECDB_OBJECT
))) {
614 dict_foreach(dict
, set_item_rlimit
, values
);
628 void main_shutdown(void)
630 struct uplinkNode
*ul
, *ul_next
;
632 for (ul
= cManager
.uplinks
; ul
; ul
= ul_next
) {
640 policer_params_delete(god_policer_params
);
641 policer_params_delete(oper_policer_params
);
642 policer_params_delete(luser_policer_params
);
647 void 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"
652 "-f, --foreground run X3 in the foreground.\n"
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"
661 printf(" --------------------------------------------------\n"
662 " - "PACKAGE_STRING
", Built: " __DATE__
", " __TIME__
".\n"
663 " - Copyright (C) 2000 - 2005, srvx Development Team\n"
664 " - Copyright (C) 2004 - 2005, X3 Development Team\n"
665 " --------------------------------------------------\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"
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"
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");
685 #if WITH_MALLOC_BOEHM_GC
687 gc_warn_proc(char *msg
, GC_word arg
)
689 log_module(MAIN_LOG
, LOG_ERROR
, "GC(%p): %s", (void*)arg
, msg
);
693 int main(int argc
, char *argv
[])
700 #if WITH_MALLOC_BOEHM_GC
702 GC_set_warn_proc(gc_warn_proc
);
703 GC_enable_incremental();
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
);
724 if (argc
> 1) { /* parse command line, if any */
726 struct option options
[] =
728 {"config", 1, 0, 'c'},
729 {"debug", 0, 0, 'd'},
730 {"foreground", 0, 0, 'f'},
732 {"check", 0, 0, 'k'},
733 {"replay", 1, 0, 'r'},
734 {"version", 0, 0, 'v'},
735 {"verbose", 0, 0, 'V'},
739 while ((c
= getopt_long(argc
, argv
, "c:kr:dfvVh", options
, NULL
)) != -1) {
742 services_config
= optarg
;
745 if (conf_read(services_config
)) {
746 printf("%s appears to be a valid configuration file.\n", services_config
);
748 printf("%s is an invalid configuration file.\n", services_config
);
752 replay_file
= fopen(optarg
, "r");
754 fprintf(stderr
, "Could not open %s for reading: %s (%d)\n",
755 optarg
, strerror(errno
), errno
);
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
786 boot_time
= time(&now
);
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
);
795 conf_register_reload(uplink_compile
);
798 /* Attempt to fork into the background if daemon mode is on. */
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
);
807 /* Close these since we should not use them from now on. */
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
));
817 fprintf(file_out
, "%i\n", (int)getpid());
821 services_argc
= argc
;
822 services_argv
= argv
;
824 atexit(call_exit_funcs
);
825 reg_exit_func(main_shutdown
);
828 MAIN_LOG
= log_register_type("x3", "file:main.log");
840 conf_globals(); /* initializes the core services */
843 message_register_table(msgtab
);
849 /* The first exit func to be called *should* be saxdb_write_all(). */
850 reg_exit_func(saxdb_write_all
);
853 log_module(MAIN_LOG
, LOG_INFO
, "Beginning replay...");
856 if ((msg
= dict_sanity_check(clients
))) {
857 log_module(MAIN_LOG
, LOG_ERROR
, "Clients insanity: %s", msg
);
860 if ((msg
= dict_sanity_check(channels
))) {
861 log_module(MAIN_LOG
, LOG_ERROR
, "Channels insanity: %s", msg
);
864 if ((msg
= dict_sanity_check(servers
))) {
865 log_module(MAIN_LOG
, LOG_ERROR
, "Servers insanity: %s", msg
);