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"
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
55 #include "main-common.c"
57 void sigaction_writedb(int x)
59 #ifndef HAVE_STRSIGNAL
60 log_module(MAIN_LOG, LOG_INFO, "Signal %d -- writing databases.", x);
62 log_module(MAIN_LOG, LOG_INFO, "%s -- writing databases.", strsignal(x));
67 void sigaction_exit(int x)
69 #ifndef HAVE_STRSIGNAL
70 log_module(MAIN_LOG, LOG_INFO, "Signal %d -- exiting.", x);
72 log_module(MAIN_LOG, LOG_INFO, "%s -- exiting.", strsignal(x));
74 irc_squit(self, "Exiting on signal from console.", NULL);
78 void sigaction_wait(UNUSED_ARG(int x))
81 wait4(-1, &code, WNOHANG, NULL);
84 void sigaction_rehash(int x)
86 #ifndef HAVE_STRSIGNAL
87 log_module(MAIN_LOG, LOG_INFO, "Signal %d -- rehashing.", x);
89 log_module(MAIN_LOG, LOG_INFO, "%s -- rehashing.", strsignal(x));
94 #if WITH_MALLOC_BOEHM_GC
96 gc_warn_proc(char *msg, GC_word arg)
98 log_module(MAIN_LOG, LOG_ERROR, "GC(%p): %s", (void*)arg, msg);
102 int main(int argc, char *argv[])
109 #if WITH_MALLOC_BOEHM_GC
111 GC_set_warn_proc(gc_warn_proc);
112 GC_enable_incremental();
119 /* set up some signal handlers */
120 memset(&sv, 0, sizeof(sv));
121 sigemptyset(&sv.sa_mask);
122 sv.sa_handler = SIG_IGN;
123 sigaction(SIGPIPE, &sv, NULL);
124 sv.sa_handler = sigaction_rehash;
125 sigaction(SIGHUP, &sv, NULL);
126 sv.sa_handler = sigaction_writedb;
127 sigaction(SIGINT, &sv, NULL);
128 sv.sa_handler = sigaction_exit;
129 sigaction(SIGQUIT, &sv, NULL);
130 sv.sa_handler = sigaction_wait;
131 sigaction(SIGCHLD, &sv, NULL);
133 if (argc > 1) { /* parse command line, if any */
135 struct option options[] =
137 {"config", 1, 0, 'c'},
138 {"debug", 0, 0, 'd'},
139 {"foreground", 0, 0, 'f'},
141 {"check", 0, 0, 'k'},
142 {"replay", 1, 0, 'r'},
143 {"version", 0, 0, 'v'},
147 while ((c = getopt_long(argc, argv, "c:dfhkr:v", options, NULL)) != -1) {
150 services_config = optarg;
153 if (conf_read(services_config)) {
154 printf("%s appears to be a valid configuration file.\n", services_config);
156 printf("%s is an invalid configuration file.\n", services_config);
160 replay_file = fopen(optarg, "r");
162 fprintf(stderr, "Could not open %s for reading: %s (%d)\n",
163 optarg, strerror(errno), errno);
188 /* We read a line here to "prime" the replay file parser, but
189 * mostly to get the right value of "now" for when we do the
197 fprintf(stdout, "Initializing daemon...\n");
198 if (!conf_read(services_config)) {
199 fprintf(stderr, "Unable to read %s.\n", services_config);
203 conf_register_reload(uplink_compile);
206 /* Attempt to fork into the background if daemon mode is on. */
209 fprintf(stderr, "Unable to fork: %s\n", strerror(errno));
210 } else if (pid > 0) {
211 fprintf(stdout, "Forking into the background (pid: %d)...\n", pid);
217 file_out = fopen(PID_FILE, "w");
218 if (file_out == NULL) {
219 /* Create the main process' pid file */
220 fprintf(stderr, "Unable to create PID file: %s", strerror(errno));
222 fprintf(file_out, "%i\n", (int)getpid());
227 /* Close these since we should not use them from now on. */
233 services_argc = argc;
234 services_argv = argv;
236 atexit(call_exit_funcs);
237 reg_exit_func(main_shutdown);
240 MAIN_LOG = log_register_type("srvx", "file:main.log");
252 conf_globals(); /* initializes the core services */
255 message_register_table(msgtab);
261 /* The first exit func to be called *should* be saxdb_write_all(). */
262 reg_exit_func(saxdb_write_all);
265 log_module(MAIN_LOG, LOG_INFO, "Beginning replay...");
268 if ((msg = dict_sanity_check(clients))) {
269 log_module(MAIN_LOG, LOG_ERROR, "Clients insanity: %s", msg);
272 if ((msg = dict_sanity_check(channels))) {
273 log_module(MAIN_LOG, LOG_ERROR, "Channels insanity: %s", msg);
276 if ((msg = dict_sanity_check(servers))) {
277 log_module(MAIN_LOG, LOG_ERROR, "Servers insanity: %s", msg);