]>
Commit | Line | Data |
---|---|---|
d76ed9a9 AS |
1 | /* main.c - srvx |
2 | * Copyright 2000-2004 srvx Development Team | |
3 | * | |
83ff05c3 | 4 | * This file is part of x3. |
d76ed9a9 AS |
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 | ||
ceafd592 | 21 | #define PID_FILE "x3.pid" |
d76ed9a9 AS |
22 | |
23 | #include "conf.h" | |
24 | #include "gline.h" | |
25 | #include "ioset.h" | |
26 | #include "modcmd.h" | |
27 | #include "saxdb.h" | |
28 | #include "sendmail.h" | |
d914d1cb | 29 | #include "shun.h" |
d76ed9a9 AS |
30 | #include "timeq.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 | #ifndef SIGCHLD | |
56 | #define SIGCHLD SIGCLD | |
57 | #endif | |
58 | ||
59 | extern FILE *replay_file; | |
60 | ||
61 | time_t boot_time, burst_begin, now; | |
62 | unsigned long burst_length; | |
63 | struct log_type *MAIN_LOG; | |
64 | ||
65 | int quit_services, max_cycles; | |
66 | ||
ceafd592 | 67 | char *services_config = "x3.conf"; |
d76ed9a9 AS |
68 | |
69 | char **services_argv; | |
70 | int services_argc; | |
71 | ||
72 | struct cManagerNode cManager; | |
73 | ||
74 | struct policer_params *oper_policer_params, *luser_policer_params, *god_policer_params; | |
75 | ||
76 | static const struct message_entry msgtab[] = { | |
77 | { "MSG_NONE", "None" }, | |
78 | { "MSG_ON", "On" }, | |
79 | { "MSG_OFF", "Off" }, | |
80 | { "MSG_NEVER", "Never" }, | |
de9510bc | 81 | { "MSG_BAR", "----------------------------------------" }, |
d76ed9a9 AS |
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'." }, | |
d914d1cb | 99 | { "MSG_INVALID_SHUN", "Invalid Shun '%s'." }, |
d76ed9a9 AS |
100 | { "MSG_INVALID_DURATION", "Invalid time span '%s'." }, |
101 | { "MSG_NOT_TARGET_NAME", "You must provide the name of a channel or user." }, | |
fc8798ec AS |
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." }, | |
d76ed9a9 AS |
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." }, | |
de9510bc | 113 | { "MSG_MATCH_COUNT", "-----------Found $b%3u$b Matches------------" }, |
d76ed9a9 AS |
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)." }, | |
de9510bc | 123 | { "MSG_AND", "," }, |
d76ed9a9 | 124 | { "MSG_0_SECONDS", "0 seconds" }, |
de9510bc AS |
125 | { "MSG_YEAR", "y" }, |
126 | { "MSG_YEARS", "y" }, | |
127 | { "MSG_WEEK", "w" }, | |
128 | { "MSG_WEEKS", "w" }, | |
129 | { "MSG_DAY", "d" }, | |
130 | { "MSG_DAYS", "d" }, | |
131 | { "MSG_HOUR", "h" }, | |
132 | { "MSG_HOURS", "h" }, | |
133 | { "MSG_MINUTE", "m" }, | |
134 | { "MSG_MINUTES", "m" }, | |
135 | { "MSG_SECOND", "s" }, | |
136 | { "MSG_SECONDS", "s" }, | |
d76ed9a9 AS |
137 | { NULL, NULL } |
138 | }; | |
139 | ||
140 | void uplink_select(char *name); | |
141 | ||
142 | static int | |
143 | uplink_insert(const char *key, void *data, UNUSED_ARG(void *extra)) | |
144 | { | |
145 | struct uplinkNode *uplink = malloc(sizeof(struct uplinkNode)); | |
146 | struct record_data *rd = data; | |
147 | int enabled = 1; | |
148 | char *str; | |
149 | struct sockaddr_in *sin; | |
150 | unsigned long addr; | |
151 | ||
152 | if(!uplink) | |
153 | { | |
154 | return 0; | |
155 | } | |
156 | ||
157 | uplink->name = (char *)key; | |
158 | uplink->host = database_get_data(rd->d.object, "address", RECDB_QSTRING); | |
159 | ||
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); | |
164 | ||
165 | str = database_get_data(rd->d.object, "enabled", RECDB_QSTRING); | |
166 | if(str) | |
167 | { | |
168 | enabled = atoi(str) ? 1 : 0; | |
169 | } | |
170 | ||
171 | cManager.enabled += enabled; | |
172 | ||
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; | |
177 | uplink->tries = 0; | |
178 | ||
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)) | |
182 | { | |
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; | |
187 | } | |
188 | else | |
189 | { | |
190 | uplink->bind_addr = NULL; | |
191 | uplink->bind_addr_len = 0; | |
192 | } | |
193 | ||
194 | uplink->next = cManager.uplinks; | |
195 | uplink->prev = NULL; | |
196 | ||
197 | if(cManager.uplinks) | |
198 | { | |
199 | cManager.uplinks->prev = uplink; | |
200 | } | |
201 | ||
202 | cManager.uplinks = uplink; | |
203 | ||
204 | /* If the configuration is being reloaded, set the current uplink | |
205 | to the reloaded equivalent, if possible. */ | |
206 | if(cManager.uplink | |
207 | && enabled | |
208 | && !irccasecmp(uplink->host, cManager.uplink->host) | |
209 | && uplink->port == cManager.uplink->port) | |
210 | { | |
211 | uplink->state = cManager.uplink->state; | |
212 | uplink->tries = cManager.uplink->tries; | |
213 | cManager.uplink = uplink; | |
214 | } | |
215 | ||
216 | return 0; | |
217 | } | |
218 | ||
219 | void | |
220 | uplink_compile(void) | |
221 | { | |
222 | const char *cycles; | |
223 | dict_t conf_node; | |
224 | struct uplinkNode *oldUplinks = NULL, *oldUplink = NULL; | |
225 | ||
226 | /* Save the old uplinks, we'll remove them later. */ | |
227 | oldUplink = cManager.uplink; | |
228 | oldUplinks = cManager.uplinks; | |
229 | ||
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))) | |
233 | { | |
234 | log_module(MAIN_LOG, LOG_FATAL, "No uplinks configured; giving up."); | |
235 | exit(1); | |
236 | } | |
237 | ||
238 | cManager.enabled = 0; | |
239 | dict_foreach(conf_node, uplink_insert, NULL); | |
240 | ||
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. */ | |
243 | if(oldUplinks) | |
244 | { | |
245 | struct uplinkNode *uplink, *next; | |
246 | ||
247 | oldUplinks->prev->next = NULL; | |
248 | ||
249 | for(uplink = oldUplinks; uplink; uplink = next) | |
250 | { | |
251 | next = uplink->next; | |
252 | free(uplink->bind_addr); | |
253 | free(uplink); | |
254 | } | |
255 | } | |
256 | ||
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) | |
260 | { | |
261 | if(oldUplink) | |
262 | { | |
263 | irc_squit(self, "Uplinks updated; selecting new uplink.", NULL); | |
264 | } | |
265 | ||
266 | cManager.uplink = NULL; | |
267 | uplink_select(NULL); | |
268 | } | |
269 | } | |
270 | ||
271 | struct uplinkNode * | |
272 | uplink_find(char *name) | |
273 | { | |
274 | struct uplinkNode *uplink; | |
275 | ||
276 | if(!cManager.enabled || !cManager.uplinks) | |
277 | { | |
278 | return NULL; | |
279 | } | |
280 | ||
281 | for(uplink = cManager.uplinks; uplink; uplink = uplink->next) | |
282 | { | |
283 | if(!strcasecmp(uplink->name, name)) | |
284 | { | |
285 | return uplink; | |
286 | } | |
287 | } | |
288 | ||
289 | return NULL; | |
290 | } | |
291 | ||
292 | void | |
293 | uplink_select(char *name) | |
294 | { | |
295 | struct uplinkNode *start, *uplink, *next; | |
296 | int stop; | |
297 | ||
298 | if(!cManager.enabled || !cManager.uplinks) | |
299 | { | |
300 | log_module(MAIN_LOG, LOG_FATAL, "No uplinks enabled; giving up."); | |
301 | exit(1); | |
302 | } | |
303 | ||
304 | if(!cManager.uplink) | |
305 | { | |
306 | start = cManager.uplinks; | |
307 | } | |
308 | else | |
309 | { | |
310 | start = cManager.uplink->next; | |
311 | if(!start) | |
312 | { | |
313 | start = cManager.uplinks; | |
314 | } | |
315 | } | |
316 | ||
317 | stop = 0; | |
318 | for(uplink = start; uplink; uplink = next) | |
319 | { | |
320 | next = uplink->next ? uplink->next : cManager.uplinks; | |
321 | ||
322 | if(stop) | |
323 | { | |
324 | uplink = NULL; | |
325 | break; | |
326 | } | |
327 | ||
328 | /* We've wrapped around the list. */ | |
329 | if(next == start) | |
330 | { | |
331 | sleep((cManager.cycles >> 1) * 5); | |
332 | cManager.cycles++; | |
333 | ||
334 | if(max_cycles && (cManager.cycles >= max_cycles)) | |
335 | { | |
336 | log_module(MAIN_LOG, LOG_FATAL, "Maximum uplink list cycles exceeded; giving up."); | |
337 | exit(1); | |
338 | } | |
339 | ||
340 | /* Give the uplink currently in 'uplink' consideration, | |
341 | and if not selected, break on the next iteration. */ | |
342 | stop = 1; | |
343 | } | |
344 | ||
345 | /* Skip bad uplinks. */ | |
346 | if(uplink->flags & UPLINK_UNAVAILABLE) | |
347 | { | |
348 | continue; | |
349 | } | |
350 | ||
351 | if(name && irccasecmp(uplink->name, name)) | |
352 | { | |
353 | /* If we were told to connect to a specific uplink, don't stop | |
354 | until we find it. | |
355 | */ | |
356 | continue; | |
357 | } | |
358 | ||
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. | |
362 | */ | |
363 | ||
364 | break; | |
365 | } | |
366 | ||
367 | if(!uplink) | |
368 | { | |
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) | |
372 | { | |
373 | log_module(MAIN_LOG, LOG_FATAL, "All available uplinks exhausted; giving up."); | |
374 | exit(1); | |
375 | } | |
376 | ||
377 | return; | |
378 | } | |
379 | ||
380 | cManager.uplink = uplink; | |
381 | } | |
382 | ||
383 | int | |
384 | uplink_connect(void) | |
385 | { | |
386 | struct uplinkNode *uplink = cManager.uplink; | |
387 | ||
388 | if(uplink->state != DISCONNECTED) | |
389 | { | |
390 | return 0; | |
391 | } | |
392 | ||
393 | if(uplink->flags & UPLINK_UNAVAILABLE) | |
394 | { | |
395 | uplink_select(NULL); | |
396 | uplink = cManager.uplink; | |
397 | } | |
398 | ||
399 | if(uplink->tries) | |
400 | { | |
401 | /* This delay could scale with the number of tries. */ | |
402 | sleep(2); | |
403 | } | |
404 | ||
405 | if(!create_socket_client(uplink)) | |
406 | { | |
407 | if(uplink->max_tries && (uplink->tries >= uplink->max_tries)) | |
408 | { | |
409 | /* This is a bad uplink, move on. */ | |
410 | uplink->flags |= UPLINK_UNAVAILABLE; | |
411 | uplink_select(NULL); | |
412 | } | |
413 | ||
414 | return 0; | |
415 | } | |
416 | else | |
417 | { | |
418 | uplink->state = AUTHENTICATING; | |
419 | irc_introduce(uplink->password); | |
420 | } | |
421 | ||
422 | return 1; | |
423 | } | |
424 | ||
425 | void | |
426 | received_ping(void) | |
427 | { | |
428 | /* This function is called when a ping is received. Take it as | |
429 | a sign of link health and reset the connection manager | |
430 | information. */ | |
431 | ||
432 | cManager.cycles = 0; | |
433 | } | |
434 | ||
435 | void sigaction_writedb(int x) | |
436 | { | |
437 | #ifndef HAVE_STRSIGNAL | |
438 | log_module(MAIN_LOG, LOG_INFO, "Signal %d -- writing databases.", x); | |
439 | #else | |
440 | log_module(MAIN_LOG, LOG_INFO, "%s -- writing databases.", strsignal(x)); | |
441 | #endif | |
442 | do_write_dbs = 1; | |
443 | } | |
444 | ||
445 | void sigaction_exit(int x) | |
446 | { | |
447 | #ifndef HAVE_STRSIGNAL | |
448 | log_module(MAIN_LOG, LOG_INFO, "Signal %d -- exiting.", x); | |
449 | #else | |
450 | log_module(MAIN_LOG, LOG_INFO, "%s -- exiting.", strsignal(x)); | |
451 | #endif | |
452 | irc_squit(self, "Exiting on signal from console.", NULL); | |
453 | quit_services = 1; | |
454 | } | |
455 | ||
456 | void sigaction_wait(UNUSED_ARG(int x)) | |
457 | { | |
458 | int code; | |
459 | wait4(-1, &code, WNOHANG, NULL); | |
460 | } | |
461 | ||
462 | void sigaction_rehash(int x) | |
463 | { | |
464 | #ifndef HAVE_STRSIGNAL | |
465 | log_module(MAIN_LOG, LOG_INFO, "Signal %d -- rehashing.", x); | |
466 | #else | |
467 | log_module(MAIN_LOG, LOG_INFO, "%s -- rehashing.", strsignal(x)); | |
468 | #endif | |
469 | do_reopen = 1; | |
470 | } | |
471 | ||
472 | static exit_func_t *ef_list; | |
473 | static unsigned int ef_size = 0, ef_used = 0; | |
474 | ||
475 | void reg_exit_func(exit_func_t handler) | |
476 | { | |
477 | if (ef_used == ef_size) { | |
478 | if (ef_size) { | |
479 | ef_size <<= 1; | |
480 | ef_list = realloc(ef_list, ef_size*sizeof(exit_func_t)); | |
481 | } else { | |
482 | ef_size = 8; | |
483 | ef_list = malloc(ef_size*sizeof(exit_func_t)); | |
484 | } | |
485 | } | |
486 | ef_list[ef_used++] = handler; | |
487 | } | |
488 | ||
489 | void call_exit_funcs(void) | |
490 | { | |
491 | unsigned int n = ef_used; | |
492 | ||
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). | |
497 | */ | |
498 | ||
499 | while (n > 0) { | |
500 | ef_list[--n](); | |
501 | } | |
502 | free(ef_list); | |
503 | ef_used = ef_size = 0; | |
504 | } | |
505 | ||
506 | int | |
507 | set_policer_param(const char *param, void *data, void *extra) | |
508 | { | |
509 | struct record_data *rd = data; | |
510 | const char *str = GET_RECORD_QSTRING(rd); | |
511 | if (str) { | |
512 | policer_params_set(extra, param, str); | |
513 | } | |
514 | return 0; | |
515 | } | |
516 | ||
517 | static void | |
518 | conf_globals(void) | |
519 | { | |
520 | const char *info; | |
521 | dict_t dict; | |
522 | ||
523 | info = conf_get_data("services/global/nick", RECDB_QSTRING); | |
524 | if (info && (info[0] == '.')) | |
525 | info = NULL; | |
526 | init_global(info); | |
527 | ||
528 | info = conf_get_data("services/nickserv/nick", RECDB_QSTRING); | |
529 | if (info && (info[0] == '.')) | |
530 | info = NULL; | |
531 | init_nickserv(info); | |
532 | ||
533 | info = conf_get_data("services/chanserv/nick", RECDB_QSTRING); | |
534 | if (info && (info[0] == '.')) | |
535 | info = NULL; | |
536 | init_chanserv(info); | |
537 | ||
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); | |
541 | } else { | |
542 | policer_params_set(god_policer_params, "size", "30"); | |
543 | policer_params_set(god_policer_params, "drain-rate", "1"); | |
544 | } | |
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); | |
548 | } else { | |
549 | policer_params_set(oper_policer_params, "size", "10"); | |
550 | policer_params_set(oper_policer_params, "drain-rate", "1"); | |
551 | } | |
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); | |
555 | } else { | |
556 | policer_params_set(luser_policer_params, "size", "5"); | |
557 | policer_params_set(luser_policer_params, "drain-rate", "0.50"); | |
558 | } | |
559 | ||
560 | info = conf_get_data("services/opserv/nick", RECDB_QSTRING); | |
561 | if (info && (info[0] == '.')) | |
562 | info = NULL; | |
563 | init_opserv(info); | |
564 | } | |
565 | ||
566 | #ifdef HAVE_SYS_RESOURCE_H | |
567 | ||
568 | static int | |
569 | set_item_rlimit(const char *name, void *data, void *extra) | |
570 | { | |
571 | int rsrc, found; | |
572 | struct record_data *rd = data; | |
573 | struct rlimit rlim; | |
574 | const char *str; | |
575 | ||
576 | rsrc = (int)dict_find(extra, name, &found); | |
577 | if (!found) { | |
578 | log_module(MAIN_LOG, LOG_ERROR, "Invalid rlimit \"%s\" in rlimits section.", name); | |
579 | return 0; | |
580 | } | |
581 | if (!(str = GET_RECORD_QSTRING(rd))) { | |
582 | log_module(MAIN_LOG, LOG_ERROR, "Missing or invalid parameter type for rlimit \"%s\".", name); | |
583 | return 0; | |
584 | } | |
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)); | |
587 | return 0; | |
588 | } | |
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)); | |
592 | } | |
593 | return 0; | |
594 | } | |
595 | ||
596 | static void | |
597 | conf_rlimits(void) | |
598 | { | |
599 | dict_t dict, values; | |
600 | ||
601 | values = dict_new(); | |
602 | dict_insert(values, "data", (void*)RLIMIT_DATA); | |
603 | dict_insert(values, "stack", (void*)RLIMIT_STACK); | |
604 | #ifdef RLIMIT_VMEM | |
605 | dict_insert(values, "vmem", (void*)RLIMIT_VMEM); | |
606 | #else | |
607 | #ifdef RLIMIT_AS | |
608 | dict_insert(values, "vmem", (void*)RLIMIT_AS); | |
609 | #endif | |
610 | #endif | |
611 | if ((dict = conf_get_data("rlimits", RECDB_OBJECT))) { | |
612 | dict_foreach(dict, set_item_rlimit, values); | |
613 | } | |
614 | dict_delete(values); | |
615 | } | |
616 | ||
617 | #else | |
618 | ||
619 | static void | |
620 | conf_rlimits(void) | |
621 | { | |
622 | } | |
623 | ||
624 | #endif | |
625 | ||
626 | void main_shutdown(void) | |
627 | { | |
628 | struct uplinkNode *ul, *ul_next; | |
629 | ioset_cleanup(); | |
630 | for (ul = cManager.uplinks; ul; ul = ul_next) { | |
631 | ul_next = ul->next; | |
632 | free(ul->bind_addr); | |
633 | free(ul); | |
634 | } | |
635 | tools_cleanup(); | |
636 | conf_close(); | |
637 | remove(PID_FILE); | |
638 | policer_params_delete(god_policer_params); | |
639 | policer_params_delete(oper_policer_params); | |
640 | policer_params_delete(luser_policer_params); | |
641 | if (replay_file) | |
642 | fclose(replay_file); | |
643 | } | |
644 | ||
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" | |
ceafd592 | 650 | "-f, --foreground run X3 in the foreground.\n" |
d76ed9a9 AS |
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" | |
655 | , self); | |
656 | } | |
657 | ||
658 | void version() { | |
659 | printf(" --------------------------------------------------\n" | |
ceafd592 | 660 | " - "PACKAGE_STRING", Built: " __DATE__ ", " __TIME__".\n" |
0d16e639 | 661 | " - Copyright (C) 2000 - 2005, srvx Development Team\n" |
ceafd592 | 662 | " - Copyright (C) 2004 - 2005, X3 Development Team\n" |
d76ed9a9 AS |
663 | " --------------------------------------------------\n"); |
664 | } | |
665 | ||
666 | void license() { | |
667 | printf("\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" | |
672 | "\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" | |
677 | "\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"); | |
681 | } | |
682 | ||
683 | #if WITH_MALLOC_BOEHM_GC | |
684 | void | |
685 | gc_warn_proc(char *msg, GC_word arg) | |
686 | { | |
687 | log_module(MAIN_LOG, LOG_ERROR, "GC(%p): %s", (void*)arg, msg); | |
688 | } | |
689 | #endif | |
690 | ||
691 | int main(int argc, char *argv[]) | |
692 | { | |
693 | int daemon, debug; | |
694 | pid_t pid = 0; | |
695 | FILE *file_out; | |
696 | struct sigaction sv; | |
697 | ||
0d16e639 | 698 | #if WITH_MALLOC_BOEHM_GC |
699 | GC_find_leak = 1; | |
700 | GC_set_warn_proc(gc_warn_proc); | |
701 | GC_enable_incremental(); | |
702 | #endif | |
703 | ||
d76ed9a9 AS |
704 | daemon = 1; |
705 | debug = 0; | |
706 | tools_init(); | |
707 | ||
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); | |
721 | ||
722 | if (argc > 1) { /* parse command line, if any */ | |
723 | int c; | |
724 | struct option options[] = | |
725 | { | |
726 | {"config", 1, 0, 'c'}, | |
727 | {"debug", 0, 0, 'd'}, | |
728 | {"foreground", 0, 0, 'f'}, | |
729 | {"help", 0, 0, 'h'}, | |
730 | {"check", 0, 0, 'k'}, | |
731 | {"replay", 1, 0, 'r'}, | |
732 | {"version", 0, 0, 'v'}, | |
733 | {"verbose", 0, 0, 'V'}, | |
734 | {0, 0, 0, 0} | |
735 | }; | |
736 | ||
737 | while ((c = getopt_long(argc, argv, "c:kr:dfvVh", options, NULL)) != -1) { | |
738 | switch(c) { | |
739 | case 'c': | |
740 | services_config = optarg; | |
741 | break; | |
742 | case 'k': | |
743 | if (conf_read(services_config)) { | |
744 | printf("%s appears to be a valid configuration file.\n", services_config); | |
745 | } else { | |
746 | printf("%s is an invalid configuration file.\n", services_config); | |
747 | } | |
748 | exit(0); | |
749 | case 'r': | |
750 | replay_file = fopen(optarg, "r"); | |
751 | if (!replay_file) { | |
752 | fprintf(stderr, "Could not open %s for reading: %s (%d)\n", | |
753 | optarg, strerror(errno), errno); | |
754 | exit(0); | |
755 | } | |
756 | break; | |
757 | case 'd': | |
758 | debug = 1; | |
759 | break; | |
760 | case 'f': | |
761 | daemon = 0; | |
762 | break; | |
763 | case 'v': | |
764 | version(); | |
765 | license(); | |
766 | exit(0); | |
767 | case 'h': | |
768 | default: | |
769 | usage(argv[0]); | |
770 | exit(0); | |
771 | } | |
772 | } | |
773 | } | |
774 | ||
775 | version(); | |
776 | ||
777 | if (replay_file) { | |
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 | |
780 | * irc_introduce. */ | |
781 | replay_read_line(); | |
782 | boot_time = now; | |
783 | } else { | |
784 | boot_time = time(&now); | |
785 | } | |
786 | ||
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); | |
790 | exit(0); | |
791 | } | |
792 | ||
793 | conf_register_reload(uplink_compile); | |
794 | ||
795 | if (daemon) { | |
796 | /* Attempt to fork into the background if daemon mode is on. */ | |
797 | pid = fork(); | |
798 | if (pid < 0) { | |
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); | |
802 | exit(0); | |
803 | } | |
804 | setsid(); | |
805 | /* Close these since we should not use them from now on. */ | |
806 | fclose(stdin); | |
807 | fclose(stdout); | |
808 | fclose(stderr); | |
809 | } | |
810 | ||
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)); | |
814 | } else { | |
815 | fprintf(file_out, "%i\n", (int)getpid()); | |
816 | fclose(file_out); | |
817 | } | |
818 | ||
819 | services_argc = argc; | |
820 | services_argv = argv; | |
821 | ||
822 | atexit(call_exit_funcs); | |
823 | reg_exit_func(main_shutdown); | |
824 | ||
825 | log_init(); | |
ceafd592 | 826 | MAIN_LOG = log_register_type("x3", "file:main.log"); |
d76ed9a9 AS |
827 | if (debug) |
828 | log_debug(); | |
d76ed9a9 AS |
829 | timeq_init(); |
830 | init_structs(); | |
831 | init_parse(); | |
832 | modcmd_init(); | |
833 | saxdb_init(); | |
834 | gline_init(); | |
d914d1cb | 835 | shun_init(); |
d76ed9a9 AS |
836 | sendmail_init(); |
837 | helpfile_init(); | |
838 | conf_globals(); /* initializes the core services */ | |
839 | conf_rlimits(); | |
840 | modules_init(); | |
841 | message_register_table(msgtab); | |
842 | modcmd_finalize(); | |
843 | saxdb_finalize(); | |
844 | helpfile_finalize(); | |
845 | modules_finalize(); | |
846 | ||
847 | /* The first exit func to be called *should* be saxdb_write_all(). */ | |
848 | reg_exit_func(saxdb_write_all); | |
849 | if (replay_file) { | |
850 | char *msg; | |
851 | log_module(MAIN_LOG, LOG_INFO, "Beginning replay..."); | |
852 | srand(now); | |
853 | replay_event_loop(); | |
854 | if ((msg = dict_sanity_check(clients))) { | |
855 | log_module(MAIN_LOG, LOG_ERROR, "Clients insanity: %s", msg); | |
856 | free(msg); | |
857 | } | |
858 | if ((msg = dict_sanity_check(channels))) { | |
859 | log_module(MAIN_LOG, LOG_ERROR, "Channels insanity: %s", msg); | |
860 | free(msg); | |
861 | } | |
862 | if ((msg = dict_sanity_check(servers))) { | |
863 | log_module(MAIN_LOG, LOG_ERROR, "Servers insanity: %s", msg); | |
864 | free(msg); | |
865 | } | |
866 | } else { | |
867 | srand(time(&now)); | |
868 | ioset_run(); | |
869 | } | |
870 | return 0; | |
871 | } |