2 * Copyright 2000-2006 srvx Development Team
4 * This file is part of srvx.
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 "srvx.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
56 #include "main-common.c"
58 void sigaction_writedb(int x)
60 #ifndef HAVE_STRSIGNAL
61 log_module(MAIN_LOG, LOG_INFO, "Signal %d -- writing databases.", x);
63 log_module(MAIN_LOG, LOG_INFO, "%s -- writing databases.", strsignal(x));
68 void sigaction_exit(int x)
70 #ifndef HAVE_STRSIGNAL
71 log_module(MAIN_LOG, LOG_INFO, "Signal %d -- exiting.", x);
73 log_module(MAIN_LOG, LOG_INFO, "%s -- exiting.", strsignal(x));
75 irc_squit(self, "Exiting on signal from console.", NULL);
79 void sigaction_wait(UNUSED_ARG(int x))
82 wait4(-1, &code, WNOHANG, NULL);
85 void sigaction_rehash(int x)
87 #ifndef HAVE_STRSIGNAL
88 log_module(MAIN_LOG, LOG_INFO, "Signal %d -- rehashing.", x);
90 log_module(MAIN_LOG, LOG_INFO, "%s -- rehashing.", strsignal(x));
95 #if WITH_MALLOC_BOEHM_GC
97 gc_warn_proc(char *msg, GC_word arg)
99 log_module(MAIN_LOG, LOG_ERROR, "GC(%p): %s", (void*)arg, msg);
103 int main(int argc, char *argv[])
111 #if WITH_MALLOC_BOEHM_GC
113 GC_set_warn_proc(gc_warn_proc);
114 GC_enable_incremental();
121 /* set up some signal handlers */
122 memset(&sv, 0, sizeof(sv));
123 sigemptyset(&sv.sa_mask);
124 sv.sa_handler = SIG_IGN;
125 sigaction(SIGPIPE, &sv, NULL);
126 sv.sa_handler = sigaction_rehash;
127 sigaction(SIGHUP, &sv, NULL);
128 sv.sa_handler = sigaction_writedb;
129 sigaction(SIGINT, &sv, NULL);
130 sv.sa_handler = sigaction_exit;
131 sigaction(SIGQUIT, &sv, NULL);
132 sv.sa_handler = sigaction_wait;
133 sigaction(SIGCHLD, &sv, NULL);
135 if (argc > 1) { /* parse command line, if any */
137 struct option options[] =
139 {"config", 1, 0, 'c'},
140 {"debug", 0, 0, 'd'},
141 {"foreground", 0, 0, 'f'},
143 {"check", 0, 0, 'k'},
144 {"replay", 1, 0, 'r'},
145 {"version", 0, 0, 'v'},
149 while ((c = getopt_long(argc, argv, "c:dfhkr:v", options, NULL)) != -1) {
152 services_config = optarg;
155 if (conf_read(services_config)) {
156 printf("%s appears to be a valid configuration file.\n", services_config);
158 printf("%s is an invalid configuration file.\n", services_config);
162 replay_file = fopen(optarg, "r");
164 fprintf(stderr, "Could not open %s for reading: %s (%d)\n",
165 optarg, strerror(errno), errno);
190 /* We read a line here to "prime" the replay file parser, but
191 * mostly to get the right value of "now" for when we do the
199 fprintf(stdout, "Initializing daemon...\n");
200 if (!conf_read(services_config)) {
201 fprintf(stderr, "Unable to read %s.\n", services_config);
205 conf_register_reload(uplink_compile);
208 /* Attempt to fork into the background if daemon mode is on. */
211 fprintf(stderr, "Unable to fork: %s\n", strerror(errno));
212 } else if (pid > 0) {
213 fprintf(stdout, "Forking into the background (pid: %d)...\n", pid);
219 file_out = fopen(PID_FILE, "w");
220 if (file_out == NULL) {
221 /* Create the main process' pid file */
222 fprintf(stderr, "Unable to create PID file: %s", strerror(errno));
224 fprintf(file_out, "%i\n", (int)getpid());
229 /* Close these since we should not use them from now on. */
235 services_argc = argc;
236 services_argv = argv;
238 atexit(call_exit_funcs);
239 reg_exit_func(main_shutdown);
242 MAIN_LOG = log_register_type("srvx", "file:main.log");
254 conf_globals(); /* initializes the core services */
257 message_register_table(msgtab);
263 /* The first exit func to be called *should* be saxdb_write_all(). */
264 reg_exit_func(saxdb_write_all);
267 log_module(MAIN_LOG, LOG_INFO, "Beginning replay...");
270 if ((msg = dict_sanity_check(clients))) {
271 log_module(MAIN_LOG, LOG_ERROR, "Clients insanity: %s", msg);
274 if ((msg = dict_sanity_check(channels))) {
275 log_module(MAIN_LOG, LOG_ERROR, "Channels insanity: %s", msg);
278 if ((msg = dict_sanity_check(servers))) {
279 log_module(MAIN_LOG, LOG_ERROR, "Servers insanity: %s", msg);