6db248274f77b7177c4fcc8231ea6777fe63d7f9
[srvx.git] / src / main.c
1 /* main.c - srvx
2  * Copyright 2000-2006 srvx Development Team
3  *
4  * This file is part of srvx.
5  *
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.
10  *
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.
15  *
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.
19  */
20
21 #define PID_FILE "srvx.pid"
22
23 #include "conf.h"
24 #include "gline.h"
25 #include "ioset.h"
26 #include "modcmd.h"
27 #include "saxdb.h"
28 #include "mail.h"
29 #include "timeq.h"
30 #include "sar.h"
31
32 #include "chanserv.h"
33 #include "global.h"
34 #include "modules.h"
35 #include "opserv.h"
36
37 #ifdef HAVE_GETOPT_H
38 #include <getopt.h>
39 #else
40 #include "getopt.h"
41 #endif
42 #ifdef HAVE_SYS_RESOURCE_H
43 #include <sys/resource.h>
44 #endif
45 #ifdef HAVE_NETINET_IN_H
46 #include <netinet/in.h>
47 #endif
48 #ifdef HAVE_SYS_SOCKET_H
49 #include <sys/socket.h>
50 #endif
51 #ifdef HAVE_SYS_WAIT_H
52 #include <sys/wait.h>
53 #endif
54
55 #include "main-common.c"
56
57 void sigaction_writedb(int x)
58 {
59 #ifndef HAVE_STRSIGNAL
60     log_module(MAIN_LOG, LOG_INFO, "Signal %d -- writing databases.", x);
61 #else
62     log_module(MAIN_LOG, LOG_INFO, "%s -- writing databases.", strsignal(x));
63 #endif
64     do_write_dbs = 1;
65 }
66
67 void sigaction_exit(int x)
68 {
69 #ifndef HAVE_STRSIGNAL
70     log_module(MAIN_LOG, LOG_INFO, "Signal %d -- exiting.", x);
71 #else
72     log_module(MAIN_LOG, LOG_INFO, "%s -- exiting.", strsignal(x));
73 #endif
74     irc_squit(self, "Exiting on signal from console.", NULL);
75     quit_services = 1;
76 }
77
78 void sigaction_wait(UNUSED_ARG(int x))
79 {
80     int code;
81     wait4(-1, &code, WNOHANG, NULL);
82 }
83
84 void sigaction_rehash(int x)
85 {
86 #ifndef HAVE_STRSIGNAL
87     log_module(MAIN_LOG, LOG_INFO, "Signal %d -- rehashing.", x);
88 #else
89     log_module(MAIN_LOG, LOG_INFO, "%s -- rehashing.", strsignal(x));
90 #endif
91     do_reopen = 1;
92 }
93
94 #if WITH_MALLOC_BOEHM_GC
95 void
96 gc_warn_proc(char *msg, GC_word arg)
97 {
98     log_module(MAIN_LOG, LOG_ERROR, "GC(%p): %s", (void*)arg, msg);
99 }
100 #endif
101
102 int main(int argc, char *argv[])
103 {
104     int run_as_daemon;
105     int debug;
106     pid_t pid = 0;
107     FILE *file_out;
108     struct sigaction sv;
109
110 #if WITH_MALLOC_BOEHM_GC
111     GC_find_leak = 1;
112     GC_set_warn_proc(gc_warn_proc);
113     GC_enable_incremental();
114 #endif
115
116     run_as_daemon = 1;
117     debug = 0;
118     tools_init();
119
120     /* set up some signal handlers */
121     memset(&sv, 0, sizeof(sv));
122     sigemptyset(&sv.sa_mask);
123     sv.sa_handler = SIG_IGN;
124     sigaction(SIGPIPE, &sv, NULL);
125     sv.sa_handler = sigaction_rehash;
126     sigaction(SIGHUP, &sv, NULL);
127     sv.sa_handler = sigaction_writedb;
128     sigaction(SIGINT, &sv, NULL);
129     sv.sa_handler = sigaction_exit;
130     sigaction(SIGQUIT, &sv, NULL);
131     sv.sa_handler = sigaction_wait;
132     sigaction(SIGCHLD, &sv, NULL);
133
134     if (argc > 1) { /* parse command line, if any */
135         int c;
136         struct option options[] =
137         {
138             {"config", 1, 0, 'c'},
139             {"debug", 0, 0, 'd'},
140             {"foreground", 0, 0, 'f'},
141             {"help", 0, 0, 'h'},
142             {"check", 0, 0, 'k'},
143             {"replay", 1, 0, 'r'},
144             {"version", 0, 0, 'v'},
145             {0, 0, 0, 0}
146         };
147
148         while ((c = getopt_long(argc, argv, "c:dfhkr:v", options, NULL)) != -1) {
149             switch (c) {
150             case 'c':
151                 services_config = optarg;
152                 break;
153             case 'k':
154                 if (conf_read(services_config)) {
155                     printf("%s appears to be a valid configuration file.\n", services_config);
156                 } else {
157                     printf("%s is an invalid configuration file.\n", services_config);
158                 }
159                 exit(0);
160             case 'r':
161                 replay_file = fopen(optarg, "r");
162                 if (!replay_file) {
163                     fprintf(stderr, "Could not open %s for reading: %s (%d)\n",
164                             optarg, strerror(errno), errno);
165                     exit(0);
166                 }
167                 break;
168             case 'd':
169                 debug = 1;
170                 break;
171             case 'f':
172                 run_as_daemon = 0;
173                 break;
174             case 'v':
175                 version();
176                 license();
177                 exit(0);
178             case 'h':
179             default:
180                 usage(argv[0]);
181                 exit(0);
182             }
183         }
184     }
185
186     version();
187
188     if (replay_file) {
189         /* We read a line here to "prime" the replay file parser, but
190          * mostly to get the right value of "now" for when we do the
191          * irc_introduce. */
192         replay_read_line();
193     } else {
194         now = time(NULL);
195     }
196     boot_time = now;
197
198     fprintf(stdout, "Initializing daemon...\n");
199     if (!conf_read(services_config)) {
200         fprintf(stderr, "Unable to read %s.\n", services_config);
201         exit(1);
202     }
203
204     conf_register_reload(uplink_compile);
205
206     if (run_as_daemon) {
207         /* Attempt to fork into the background if daemon mode is on. */
208         pid = fork();
209         if (pid < 0) {
210             fprintf(stderr, "Unable to fork: %s\n", strerror(errno));
211         } else if (pid > 0) {
212             fprintf(stdout, "Forking into the background (pid: %d)...\n", pid);
213             exit(0);
214         }
215         setsid();
216     }
217
218     file_out = fopen(PID_FILE, "w");
219     if (file_out == NULL) {
220         /* Create the main process' pid file */
221         fprintf(stderr, "Unable to create PID file: %s", strerror(errno));
222     } else {
223         fprintf(file_out, "%i\n", (int)getpid());
224         fclose(file_out);
225     }
226
227     if (run_as_daemon) {
228         /* Close these since we should not use them from now on. */
229         fclose(stdin);
230         fclose(stdout);
231         fclose(stderr);
232     }
233
234     services_argc = argc;
235     services_argv = argv;
236
237     atexit(call_exit_funcs);
238     reg_exit_func(main_shutdown);
239
240     log_init();
241     MAIN_LOG = log_register_type("srvx", "file:main.log");
242     if (debug)
243         log_debug();
244     ioset_init();
245     init_structs();
246     init_parse();
247     modcmd_init();
248     saxdb_init();
249     sar_init();
250     gline_init();
251     mail_init();
252     helpfile_init();
253     conf_globals(); /* initializes the core services */
254     conf_rlimits();
255     modules_init();
256     message_register_table(msgtab);
257     modcmd_finalize();
258     saxdb_finalize();
259     helpfile_finalize();
260     modules_finalize();
261
262     /* The first exit func to be called *should* be saxdb_write_all(). */
263     reg_exit_func(saxdb_write_all);
264     if (replay_file) {
265         char *msg;
266         log_module(MAIN_LOG, LOG_INFO, "Beginning replay...");
267         srand(now);
268         replay_event_loop();
269         if ((msg = dict_sanity_check(clients))) {
270             log_module(MAIN_LOG, LOG_ERROR, "Clients insanity: %s", msg);
271             free(msg);
272         }
273         if ((msg = dict_sanity_check(channels))) {
274             log_module(MAIN_LOG, LOG_ERROR, "Channels insanity: %s", msg);
275             free(msg);
276         }
277         if ((msg = dict_sanity_check(servers))) {
278             log_module(MAIN_LOG, LOG_ERROR, "Servers insanity: %s", msg);
279             free(msg);
280         }
281     } else {
282         now = time(NULL);
283         srand(now);
284         ioset_run();
285     }
286     return 0;
287 }