]>
Commit | Line | Data |
---|---|---|
1 | /* main.c - srvx | |
2 | * Copyright 2000-2004 srvx Development Team | |
3 | * | |
4 | * This file is part of x3. | |
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 "x3.pid" | |
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" | |
29 | #include "timeq.h" | |
30 | ||
31 | #include "chanserv.h" | |
32 | #include "global.h" | |
33 | #include "modules.h" | |
34 | #include "opserv.h" | |
35 | ||
36 | #ifdef HAVE_GETOPT_H | |
37 | #include <getopt.h> | |
38 | #else | |
39 | #include "getopt.h" | |
40 | #endif | |
41 | #ifdef HAVE_SYS_RESOURCE_H | |
42 | #include <sys/resource.h> | |
43 | #endif | |
44 | #ifdef HAVE_NETINET_IN_H | |
45 | #include <netinet/in.h> | |
46 | #endif | |
47 | #ifdef HAVE_SYS_SOCKET_H | |
48 | #include <sys/socket.h> | |
49 | #endif | |
50 | #ifdef HAVE_SYS_WAIT_H | |
51 | #include <sys/wait.h> | |
52 | #endif | |
53 | ||
54 | #ifndef SIGCHLD | |
55 | #define SIGCHLD SIGCLD | |
56 | #endif | |
57 | ||
58 | extern FILE *replay_file; | |
59 | ||
60 | time_t boot_time, burst_begin, now; | |
61 | unsigned long burst_length; | |
62 | struct log_type *MAIN_LOG; | |
63 | ||
64 | int quit_services, max_cycles; | |
65 | ||
66 | char *services_config = "x3.conf"; | |
67 | ||
68 | char **services_argv; | |
69 | int services_argc; | |
70 | ||
71 | struct cManagerNode cManager; | |
72 | ||
73 | struct policer_params *oper_policer_params, *luser_policer_params, *god_policer_params; | |
74 | ||
75 | static const struct message_entry msgtab[] = { | |
76 | { "MSG_NONE", "None" }, | |
77 | { "MSG_ON", "On" }, | |
78 | { "MSG_OFF", "Off" }, | |
79 | { "MSG_NEVER", "Never" }, | |
80 | { "MSG_BAR", "----------------------------------------" }, | |
81 | { "MSG_SERVICE_IMMUNE", "$b%s$b may not be kicked, killed, banned, or deopped." }, | |
82 | { "MSG_SERVICE_PRIVILEGED", "$b%s$b is a privileged service." }, | |
83 | { "MSG_NOT_A_SERVICE", "$b%s$b is not a service bot." }, | |
84 | { "MSG_COMMAND_UNKNOWN", "$b%s$b is an unknown command." }, | |
85 | { "MSG_COMMAND_PRIVILEGED", "$b%s$b is a privileged command." }, | |
86 | { "MSG_COMMAND_DISABLED", "$b%s$b is a disabled command." }, | |
87 | { "MSG_SETTING_PRIVILEGED", "$b%s$b is a privileged setting." }, | |
88 | { "MSG_AUTHENTICATE", "You must first authenticate with $b$N$b." }, | |
89 | { "MSG_USER_AUTHENTICATE", "%s must first authenticate with $b$N$b." }, | |
90 | { "MSG_SET_EMAIL_ADDR", "You must first set your account's email address. (Contact network staff if you cannot auth to your account.)" }, | |
91 | { "MSG_HANDLE_UNKNOWN", "Account $b%s$b has not been registered." }, | |
92 | { "MSG_NICK_UNKNOWN", "User with nick $b%s$b does not exist." }, | |
93 | { "MSG_CHANNEL_UNKNOWN", "Channel with name $b%s$b does not exist." }, | |
94 | { "MSG_SERVER_UNKNOWN", "Server with name $b%s$b does not exist or is not linked." }, | |
95 | { "MSG_MODULE_UNKNOWN", "No module has been registered with name $b%s$b." }, | |
96 | { "MSG_INVALID_MODES", "$b%s$b is an invalid set of channel modes." }, | |
97 | { "MSG_INVALID_GLINE", "Invalid G-line '%s'." }, | |
98 | { "MSG_INVALID_DURATION", "Invalid time span '%s'." }, | |
99 | { "MSG_NOT_TARGET_NAME", "You must provide the name of a channel or user." }, | |
100 | { "MSG_NOT_CHANNEL_NAME", "The channel name you specified is not a valid channel name." }, | |
101 | { "MSG_INVALID_CHANNEL", "The channel name you specified does not exist." }, | |
102 | { "MSG_CHANNEL_ABSENT", "You aren't currently in $b%s$b." }, | |
103 | { "MSG_CHANNEL_USER_ABSENT", "$b%s$b isn't currently in $b%s$b." }, | |
104 | { "MSG_MISSING_PARAMS", "$b%s$b requires more parameters." }, | |
105 | { "MSG_DEPRECATED_COMMAND", "The $b%s$b command has been deprecated, and will be removed in the future; please use $b%s$b instead." }, | |
106 | { "MSG_OPER_SUSPENDED", "Your $b$O$b access has been suspended." }, | |
107 | { "MSG_USER_OUTRANKED", "$b%s$b outranks you (command has no effect)." }, | |
108 | { "MSG_STUPID_ACCESS_CHANGE", "Please ask someone $belse$b to demote you." }, | |
109 | { "MSG_NO_SEARCH_ACCESS", "You do not have enough access to search based on $b%s$b." }, | |
110 | { "MSG_INVALID_CRITERIA", "$b%s$b is an invalid search criteria." }, | |
111 | { "MSG_MATCH_COUNT", "-----------Found $b%3u$b Matches------------" }, | |
112 | { "MSG_NO_MATCHES", "Nothing matched the criteria of your search." }, | |
113 | { "MSG_TOPIC_UNKNOWN", "No help on that topic." }, | |
114 | { "MSG_INVALID_BINARY", "$b%s$b is an invalid binary value." }, | |
115 | { "MSG_INTERNAL_FAILURE", "Your command could not be processed due to an internal failure." }, | |
116 | { "MSG_DB_UNKNOWN", "I do not know of a database named %s." }, | |
117 | { "MSG_DB_IS_MONDO", "Database %s is in the \"mondo\" database and cannot be written separately." }, | |
118 | { "MSG_DB_WRITE_ERROR", "Error while writing database %s." }, | |
119 | { "MSG_DB_WROTE_DB", "Wrote database %s (in "FMT_TIME_T".%06lu seconds)." }, | |
120 | { "MSG_DB_WROTE_ALL", "Wrote all databases (in "FMT_TIME_T".%06lu seconds)." }, | |
121 | { "MSG_AND", "," }, | |
122 | { "MSG_0_SECONDS", "0 seconds" }, | |
123 | { "MSG_YEAR", "y" }, | |
124 | { "MSG_YEARS", "y" }, | |
125 | { "MSG_WEEK", "w" }, | |
126 | { "MSG_WEEKS", "w" }, | |
127 | { "MSG_DAY", "d" }, | |
128 | { "MSG_DAYS", "d" }, | |
129 | { "MSG_HOUR", "h" }, | |
130 | { "MSG_HOURS", "h" }, | |
131 | { "MSG_MINUTE", "m" }, | |
132 | { "MSG_MINUTES", "m" }, | |
133 | { "MSG_SECOND", "s" }, | |
134 | { "MSG_SECONDS", "s" }, | |
135 | { NULL, NULL } | |
136 | }; | |
137 | ||
138 | void uplink_select(char *name); | |
139 | ||
140 | static int | |
141 | uplink_insert(const char *key, void *data, UNUSED_ARG(void *extra)) | |
142 | { | |
143 | struct uplinkNode *uplink = malloc(sizeof(struct uplinkNode)); | |
144 | struct record_data *rd = data; | |
145 | int enabled = 1; | |
146 | char *str; | |
147 | struct sockaddr_in *sin; | |
148 | unsigned long addr; | |
149 | ||
150 | if(!uplink) | |
151 | { | |
152 | return 0; | |
153 | } | |
154 | ||
155 | uplink->name = (char *)key; | |
156 | uplink->host = database_get_data(rd->d.object, "address", RECDB_QSTRING); | |
157 | ||
158 | str = database_get_data(rd->d.object, "port", RECDB_QSTRING); | |
159 | uplink->port = str ? atoi(str) : 6667; | |
160 | uplink->password = database_get_data(rd->d.object, "password", RECDB_QSTRING); | |
161 | uplink->their_password = database_get_data(rd->d.object, "uplink_password", RECDB_QSTRING); | |
162 | ||
163 | str = database_get_data(rd->d.object, "enabled", RECDB_QSTRING); | |
164 | if(str) | |
165 | { | |
166 | enabled = atoi(str) ? 1 : 0; | |
167 | } | |
168 | ||
169 | cManager.enabled += enabled; | |
170 | ||
171 | str = database_get_data(rd->d.object, "max_tries", RECDB_QSTRING); | |
172 | uplink->max_tries = str ? atoi(str) : 3; | |
173 | uplink->flags = enabled ? 0 : UPLINK_UNAVAILABLE; | |
174 | uplink->state = DISCONNECTED; | |
175 | uplink->tries = 0; | |
176 | ||
177 | str = database_get_data(rd->d.object, "bind_address", RECDB_QSTRING); | |
178 | uplink->bind_addr_len = sizeof(*sin); | |
179 | if (str && getipbyname(str, &addr)) | |
180 | { | |
181 | sin = calloc(1, uplink->bind_addr_len); | |
182 | sin->sin_family = AF_INET; | |
183 | sin->sin_addr.s_addr = addr; | |
184 | uplink->bind_addr = sin; | |
185 | } | |
186 | else | |
187 | { | |
188 | uplink->bind_addr = NULL; | |
189 | uplink->bind_addr_len = 0; | |
190 | } | |
191 | ||
192 | uplink->next = cManager.uplinks; | |
193 | uplink->prev = NULL; | |
194 | ||
195 | if(cManager.uplinks) | |
196 | { | |
197 | cManager.uplinks->prev = uplink; | |
198 | } | |
199 | ||
200 | cManager.uplinks = uplink; | |
201 | ||
202 | /* If the configuration is being reloaded, set the current uplink | |
203 | to the reloaded equivalent, if possible. */ | |
204 | if(cManager.uplink | |
205 | && enabled | |
206 | && !irccasecmp(uplink->host, cManager.uplink->host) | |
207 | && uplink->port == cManager.uplink->port) | |
208 | { | |
209 | uplink->state = cManager.uplink->state; | |
210 | uplink->tries = cManager.uplink->tries; | |
211 | cManager.uplink = uplink; | |
212 | } | |
213 | ||
214 | return 0; | |
215 | } | |
216 | ||
217 | void | |
218 | uplink_compile(void) | |
219 | { | |
220 | const char *cycles; | |
221 | dict_t conf_node; | |
222 | struct uplinkNode *oldUplinks = NULL, *oldUplink = NULL; | |
223 | ||
224 | /* Save the old uplinks, we'll remove them later. */ | |
225 | oldUplink = cManager.uplink; | |
226 | oldUplinks = cManager.uplinks; | |
227 | ||
228 | cycles = conf_get_data("server/max_cycles", RECDB_QSTRING); | |
229 | max_cycles = cycles ? atoi(cycles) : 30; | |
230 | if(!(conf_node = conf_get_data("uplinks", RECDB_OBJECT))) | |
231 | { | |
232 | log_module(MAIN_LOG, LOG_FATAL, "No uplinks configured; giving up."); | |
233 | exit(1); | |
234 | } | |
235 | ||
236 | cManager.enabled = 0; | |
237 | dict_foreach(conf_node, uplink_insert, NULL); | |
238 | ||
239 | /* Remove the old uplinks, if any. It doesn't matter if oldUplink (below) | |
240 | is a reference to one of these, because it won't get dereferenced. */ | |
241 | if(oldUplinks) | |
242 | { | |
243 | struct uplinkNode *uplink, *next; | |
244 | ||
245 | oldUplinks->prev->next = NULL; | |
246 | ||
247 | for(uplink = oldUplinks; uplink; uplink = next) | |
248 | { | |
249 | next = uplink->next; | |
250 | free(uplink->bind_addr); | |
251 | free(uplink); | |
252 | } | |
253 | } | |
254 | ||
255 | /* If the uplink hasn't changed, it's either NULL or pointing at | |
256 | an uplink that was just deleted, select a new one. */ | |
257 | if(cManager.uplink == oldUplink) | |
258 | { | |
259 | if(oldUplink) | |
260 | { | |
261 | irc_squit(self, "Uplinks updated; selecting new uplink.", NULL); | |
262 | } | |
263 | ||
264 | cManager.uplink = NULL; | |
265 | uplink_select(NULL); | |
266 | } | |
267 | } | |
268 | ||
269 | struct uplinkNode * | |
270 | uplink_find(char *name) | |
271 | { | |
272 | struct uplinkNode *uplink; | |
273 | ||
274 | if(!cManager.enabled || !cManager.uplinks) | |
275 | { | |
276 | return NULL; | |
277 | } | |
278 | ||
279 | for(uplink = cManager.uplinks; uplink; uplink = uplink->next) | |
280 | { | |
281 | if(!strcasecmp(uplink->name, name)) | |
282 | { | |
283 | return uplink; | |
284 | } | |
285 | } | |
286 | ||
287 | return NULL; | |
288 | } | |
289 | ||
290 | void | |
291 | uplink_select(char *name) | |
292 | { | |
293 | struct uplinkNode *start, *uplink, *next; | |
294 | int stop; | |
295 | ||
296 | if(!cManager.enabled || !cManager.uplinks) | |
297 | { | |
298 | log_module(MAIN_LOG, LOG_FATAL, "No uplinks enabled; giving up."); | |
299 | exit(1); | |
300 | } | |
301 | ||
302 | if(!cManager.uplink) | |
303 | { | |
304 | start = cManager.uplinks; | |
305 | } | |
306 | else | |
307 | { | |
308 | start = cManager.uplink->next; | |
309 | if(!start) | |
310 | { | |
311 | start = cManager.uplinks; | |
312 | } | |
313 | } | |
314 | ||
315 | stop = 0; | |
316 | for(uplink = start; uplink; uplink = next) | |
317 | { | |
318 | next = uplink->next ? uplink->next : cManager.uplinks; | |
319 | ||
320 | if(stop) | |
321 | { | |
322 | uplink = NULL; | |
323 | break; | |
324 | } | |
325 | ||
326 | /* We've wrapped around the list. */ | |
327 | if(next == start) | |
328 | { | |
329 | sleep((cManager.cycles >> 1) * 5); | |
330 | cManager.cycles++; | |
331 | ||
332 | if(max_cycles && (cManager.cycles >= max_cycles)) | |
333 | { | |
334 | log_module(MAIN_LOG, LOG_FATAL, "Maximum uplink list cycles exceeded; giving up."); | |
335 | exit(1); | |
336 | } | |
337 | ||
338 | /* Give the uplink currently in 'uplink' consideration, | |
339 | and if not selected, break on the next iteration. */ | |
340 | stop = 1; | |
341 | } | |
342 | ||
343 | /* Skip bad uplinks. */ | |
344 | if(uplink->flags & UPLINK_UNAVAILABLE) | |
345 | { | |
346 | continue; | |
347 | } | |
348 | ||
349 | if(name && irccasecmp(uplink->name, name)) | |
350 | { | |
351 | /* If we were told to connect to a specific uplink, don't stop | |
352 | until we find it. | |
353 | */ | |
354 | continue; | |
355 | } | |
356 | ||
357 | /* It would be possible to track uplink health through a variety | |
358 | of statistics and only break on the best uplink. For now, break | |
359 | on the first available one. | |
360 | */ | |
361 | ||
362 | break; | |
363 | } | |
364 | ||
365 | if(!uplink) | |
366 | { | |
367 | /* We are shit outta luck if every single uplink has been passed | |
368 | over. Use the current uplink if possible. */ | |
369 | if(!cManager.uplink || cManager.uplink->flags & UPLINK_UNAVAILABLE) | |
370 | { | |
371 | log_module(MAIN_LOG, LOG_FATAL, "All available uplinks exhausted; giving up."); | |
372 | exit(1); | |
373 | } | |
374 | ||
375 | return; | |
376 | } | |
377 | ||
378 | cManager.uplink = uplink; | |
379 | } | |
380 | ||
381 | int | |
382 | uplink_connect(void) | |
383 | { | |
384 | struct uplinkNode *uplink = cManager.uplink; | |
385 | ||
386 | if(uplink->state != DISCONNECTED) | |
387 | { | |
388 | return 0; | |
389 | } | |
390 | ||
391 | if(uplink->flags & UPLINK_UNAVAILABLE) | |
392 | { | |
393 | uplink_select(NULL); | |
394 | uplink = cManager.uplink; | |
395 | } | |
396 | ||
397 | if(uplink->tries) | |
398 | { | |
399 | /* This delay could scale with the number of tries. */ | |
400 | sleep(2); | |
401 | } | |
402 | ||
403 | if(!create_socket_client(uplink)) | |
404 | { | |
405 | if(uplink->max_tries && (uplink->tries >= uplink->max_tries)) | |
406 | { | |
407 | /* This is a bad uplink, move on. */ | |
408 | uplink->flags |= UPLINK_UNAVAILABLE; | |
409 | uplink_select(NULL); | |
410 | } | |
411 | ||
412 | return 0; | |
413 | } | |
414 | else | |
415 | { | |
416 | uplink->state = AUTHENTICATING; | |
417 | irc_introduce(uplink->password); | |
418 | } | |
419 | ||
420 | return 1; | |
421 | } | |
422 | ||
423 | void | |
424 | received_ping(void) | |
425 | { | |
426 | /* This function is called when a ping is received. Take it as | |
427 | a sign of link health and reset the connection manager | |
428 | information. */ | |
429 | ||
430 | cManager.cycles = 0; | |
431 | } | |
432 | ||
433 | void sigaction_writedb(int x) | |
434 | { | |
435 | #ifndef HAVE_STRSIGNAL | |
436 | log_module(MAIN_LOG, LOG_INFO, "Signal %d -- writing databases.", x); | |
437 | #else | |
438 | log_module(MAIN_LOG, LOG_INFO, "%s -- writing databases.", strsignal(x)); | |
439 | #endif | |
440 | do_write_dbs = 1; | |
441 | } | |
442 | ||
443 | void sigaction_exit(int x) | |
444 | { | |
445 | #ifndef HAVE_STRSIGNAL | |
446 | log_module(MAIN_LOG, LOG_INFO, "Signal %d -- exiting.", x); | |
447 | #else | |
448 | log_module(MAIN_LOG, LOG_INFO, "%s -- exiting.", strsignal(x)); | |
449 | #endif | |
450 | irc_squit(self, "Exiting on signal from console.", NULL); | |
451 | quit_services = 1; | |
452 | } | |
453 | ||
454 | void sigaction_wait(UNUSED_ARG(int x)) | |
455 | { | |
456 | int code; | |
457 | wait4(-1, &code, WNOHANG, NULL); | |
458 | } | |
459 | ||
460 | void sigaction_rehash(int x) | |
461 | { | |
462 | #ifndef HAVE_STRSIGNAL | |
463 | log_module(MAIN_LOG, LOG_INFO, "Signal %d -- rehashing.", x); | |
464 | #else | |
465 | log_module(MAIN_LOG, LOG_INFO, "%s -- rehashing.", strsignal(x)); | |
466 | #endif | |
467 | do_reopen = 1; | |
468 | } | |
469 | ||
470 | static exit_func_t *ef_list; | |
471 | static unsigned int ef_size = 0, ef_used = 0; | |
472 | ||
473 | void reg_exit_func(exit_func_t handler) | |
474 | { | |
475 | if (ef_used == ef_size) { | |
476 | if (ef_size) { | |
477 | ef_size <<= 1; | |
478 | ef_list = realloc(ef_list, ef_size*sizeof(exit_func_t)); | |
479 | } else { | |
480 | ef_size = 8; | |
481 | ef_list = malloc(ef_size*sizeof(exit_func_t)); | |
482 | } | |
483 | } | |
484 | ef_list[ef_used++] = handler; | |
485 | } | |
486 | ||
487 | void call_exit_funcs(void) | |
488 | { | |
489 | unsigned int n = ef_used; | |
490 | ||
491 | /* Call them in reverse order because we initialize logs, then | |
492 | * nickserv, then chanserv, etc., and they register their exit | |
493 | * funcs in that order, and there are some dependencies (for | |
494 | * example, ChanServ requires NickServ to not have cleaned up). | |
495 | */ | |
496 | ||
497 | while (n > 0) { | |
498 | ef_list[--n](); | |
499 | } | |
500 | free(ef_list); | |
501 | ef_used = ef_size = 0; | |
502 | } | |
503 | ||
504 | int | |
505 | set_policer_param(const char *param, void *data, void *extra) | |
506 | { | |
507 | struct record_data *rd = data; | |
508 | const char *str = GET_RECORD_QSTRING(rd); | |
509 | if (str) { | |
510 | policer_params_set(extra, param, str); | |
511 | } | |
512 | return 0; | |
513 | } | |
514 | ||
515 | static void | |
516 | conf_globals(void) | |
517 | { | |
518 | const char *info; | |
519 | dict_t dict; | |
520 | ||
521 | info = conf_get_data("services/global/nick", RECDB_QSTRING); | |
522 | if (info && (info[0] == '.')) | |
523 | info = NULL; | |
524 | init_global(info); | |
525 | ||
526 | info = conf_get_data("services/nickserv/nick", RECDB_QSTRING); | |
527 | if (info && (info[0] == '.')) | |
528 | info = NULL; | |
529 | init_nickserv(info); | |
530 | ||
531 | info = conf_get_data("services/chanserv/nick", RECDB_QSTRING); | |
532 | if (info && (info[0] == '.')) | |
533 | info = NULL; | |
534 | init_chanserv(info); | |
535 | ||
536 | god_policer_params = policer_params_new(); | |
537 | if ((dict = conf_get_data("policers/commands-god", RECDB_OBJECT))) { | |
538 | dict_foreach(dict, set_policer_param, god_policer_params); | |
539 | } else { | |
540 | policer_params_set(god_policer_params, "size", "30"); | |
541 | policer_params_set(god_policer_params, "drain-rate", "1"); | |
542 | } | |
543 | oper_policer_params = policer_params_new(); | |
544 | if ((dict = conf_get_data("policers/commands-oper", RECDB_OBJECT))) { | |
545 | dict_foreach(dict, set_policer_param, oper_policer_params); | |
546 | } else { | |
547 | policer_params_set(oper_policer_params, "size", "10"); | |
548 | policer_params_set(oper_policer_params, "drain-rate", "1"); | |
549 | } | |
550 | luser_policer_params = policer_params_new(); | |
551 | if ((dict = conf_get_data("policers/commands-luser", RECDB_OBJECT))) { | |
552 | dict_foreach(dict, set_policer_param, luser_policer_params); | |
553 | } else { | |
554 | policer_params_set(luser_policer_params, "size", "5"); | |
555 | policer_params_set(luser_policer_params, "drain-rate", "0.50"); | |
556 | } | |
557 | ||
558 | info = conf_get_data("services/opserv/nick", RECDB_QSTRING); | |
559 | if (info && (info[0] == '.')) | |
560 | info = NULL; | |
561 | init_opserv(info); | |
562 | } | |
563 | ||
564 | #ifdef HAVE_SYS_RESOURCE_H | |
565 | ||
566 | static int | |
567 | set_item_rlimit(const char *name, void *data, void *extra) | |
568 | { | |
569 | int rsrc, found; | |
570 | struct record_data *rd = data; | |
571 | struct rlimit rlim; | |
572 | const char *str; | |
573 | ||
574 | rsrc = (int)dict_find(extra, name, &found); | |
575 | if (!found) { | |
576 | log_module(MAIN_LOG, LOG_ERROR, "Invalid rlimit \"%s\" in rlimits section.", name); | |
577 | return 0; | |
578 | } | |
579 | if (!(str = GET_RECORD_QSTRING(rd))) { | |
580 | log_module(MAIN_LOG, LOG_ERROR, "Missing or invalid parameter type for rlimit \"%s\".", name); | |
581 | return 0; | |
582 | } | |
583 | if (getrlimit(rsrc, &rlim) < 0) { | |
584 | log_module(MAIN_LOG, LOG_ERROR, "Couldn't get rlimit \"%s\": errno %d: %s", name, errno, strerror(errno)); | |
585 | return 0; | |
586 | } | |
587 | rlim.rlim_cur = ParseVolume(str); | |
588 | if (setrlimit(rsrc, &rlim) < 0) { | |
589 | log_module(MAIN_LOG, LOG_ERROR, "Couldn't set rlimit \"%s\": errno %d: %s", name, errno, strerror(errno)); | |
590 | } | |
591 | return 0; | |
592 | } | |
593 | ||
594 | static void | |
595 | conf_rlimits(void) | |
596 | { | |
597 | dict_t dict, values; | |
598 | ||
599 | values = dict_new(); | |
600 | dict_insert(values, "data", (void*)RLIMIT_DATA); | |
601 | dict_insert(values, "stack", (void*)RLIMIT_STACK); | |
602 | #ifdef RLIMIT_VMEM | |
603 | dict_insert(values, "vmem", (void*)RLIMIT_VMEM); | |
604 | #else | |
605 | #ifdef RLIMIT_AS | |
606 | dict_insert(values, "vmem", (void*)RLIMIT_AS); | |
607 | #endif | |
608 | #endif | |
609 | if ((dict = conf_get_data("rlimits", RECDB_OBJECT))) { | |
610 | dict_foreach(dict, set_item_rlimit, values); | |
611 | } | |
612 | dict_delete(values); | |
613 | } | |
614 | ||
615 | #else | |
616 | ||
617 | static void | |
618 | conf_rlimits(void) | |
619 | { | |
620 | } | |
621 | ||
622 | #endif | |
623 | ||
624 | void main_shutdown(void) | |
625 | { | |
626 | struct uplinkNode *ul, *ul_next; | |
627 | ioset_cleanup(); | |
628 | for (ul = cManager.uplinks; ul; ul = ul_next) { | |
629 | ul_next = ul->next; | |
630 | free(ul->bind_addr); | |
631 | free(ul); | |
632 | } | |
633 | tools_cleanup(); | |
634 | conf_close(); | |
635 | remove(PID_FILE); | |
636 | policer_params_delete(god_policer_params); | |
637 | policer_params_delete(oper_policer_params); | |
638 | policer_params_delete(luser_policer_params); | |
639 | if (replay_file) | |
640 | fclose(replay_file); | |
641 | } | |
642 | ||
643 | void usage(char *self) { | |
644 | /* We can assume we have getopt_long(). */ | |
645 | printf("Usage: %s [-c config] [-r log] [-d] [-f] [-v|-h]\n" | |
646 | "-c, --config selects a different configuration file.\n" | |
647 | "-d, --debug enables debug mode.\n" | |
648 | "-f, --foreground run X3 in the foreground.\n" | |
649 | "-h, --help prints this usage message.\n" | |
650 | "-k, --check checks the configuration file's syntax.\n" | |
651 | "-r, --replay replay a log file (for debugging)\n" | |
652 | "-v, --version prints this program's version.\n" | |
653 | , self); | |
654 | } | |
655 | ||
656 | void version() { | |
657 | printf(" --------------------------------------------------\n" | |
658 | " - "PACKAGE_STRING", Built: " __DATE__ ", " __TIME__".\n" | |
659 | " - Copyright (C) 2000 - 2005, srvx Development Team\n" | |
660 | " - Copyright (C) 2004 - 2005, X3 Development Team\n" | |
661 | " --------------------------------------------------\n"); | |
662 | } | |
663 | ||
664 | void license() { | |
665 | printf("\n" | |
666 | "This program is free software; you can redistribute it and/or modify\n" | |
667 | "it under the terms of the GNU General Public License as published by\n" | |
668 | "the Free Software Foundation; either version 2 of the License, or\n" | |
669 | "(at your option) any later version.\n" | |
670 | "\n" | |
671 | "This program is distributed in the hope that it will be useful,\n" | |
672 | "but WITHOUT ANY WARRANTY; without even the implied warranty of\n" | |
673 | "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" | |
674 | "GNU General Public License for more details.\n" | |
675 | "\n" | |
676 | "You should have received a copy of the GNU General Public License\n" | |
677 | "along with this program; if not, write to the Free Software\n" | |
678 | "Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\n\n"); | |
679 | } | |
680 | ||
681 | #if WITH_MALLOC_BOEHM_GC | |
682 | void | |
683 | gc_warn_proc(char *msg, GC_word arg) | |
684 | { | |
685 | log_module(MAIN_LOG, LOG_ERROR, "GC(%p): %s", (void*)arg, msg); | |
686 | } | |
687 | #endif | |
688 | ||
689 | int main(int argc, char *argv[]) | |
690 | { | |
691 | int daemon, debug; | |
692 | pid_t pid = 0; | |
693 | FILE *file_out; | |
694 | struct sigaction sv; | |
695 | ||
696 | #if WITH_MALLOC_BOEHM_GC | |
697 | GC_find_leak = 1; | |
698 | GC_set_warn_proc(gc_warn_proc); | |
699 | GC_enable_incremental(); | |
700 | #endif | |
701 | ||
702 | daemon = 1; | |
703 | debug = 0; | |
704 | tools_init(); | |
705 | ||
706 | /* set up some signal handlers */ | |
707 | memset(&sv, 0, sizeof(sv)); | |
708 | sigemptyset(&sv.sa_mask); | |
709 | sv.sa_handler = SIG_IGN; | |
710 | sigaction(SIGPIPE, &sv, NULL); | |
711 | sv.sa_handler = sigaction_rehash; | |
712 | sigaction(SIGHUP, &sv, NULL); | |
713 | sv.sa_handler = sigaction_writedb; | |
714 | sigaction(SIGINT, &sv, NULL); | |
715 | sv.sa_handler = sigaction_exit; | |
716 | sigaction(SIGQUIT, &sv, NULL); | |
717 | sv.sa_handler = sigaction_wait; | |
718 | sigaction(SIGCHLD, &sv, NULL); | |
719 | ||
720 | if (argc > 1) { /* parse command line, if any */ | |
721 | int c; | |
722 | struct option options[] = | |
723 | { | |
724 | {"config", 1, 0, 'c'}, | |
725 | {"debug", 0, 0, 'd'}, | |
726 | {"foreground", 0, 0, 'f'}, | |
727 | {"help", 0, 0, 'h'}, | |
728 | {"check", 0, 0, 'k'}, | |
729 | {"replay", 1, 0, 'r'}, | |
730 | {"version", 0, 0, 'v'}, | |
731 | {"verbose", 0, 0, 'V'}, | |
732 | {0, 0, 0, 0} | |
733 | }; | |
734 | ||
735 | while ((c = getopt_long(argc, argv, "c:kr:dfvVh", options, NULL)) != -1) { | |
736 | switch(c) { | |
737 | case 'c': | |
738 | services_config = optarg; | |
739 | break; | |
740 | case 'k': | |
741 | if (conf_read(services_config)) { | |
742 | printf("%s appears to be a valid configuration file.\n", services_config); | |
743 | } else { | |
744 | printf("%s is an invalid configuration file.\n", services_config); | |
745 | } | |
746 | exit(0); | |
747 | case 'r': | |
748 | replay_file = fopen(optarg, "r"); | |
749 | if (!replay_file) { | |
750 | fprintf(stderr, "Could not open %s for reading: %s (%d)\n", | |
751 | optarg, strerror(errno), errno); | |
752 | exit(0); | |
753 | } | |
754 | break; | |
755 | case 'd': | |
756 | debug = 1; | |
757 | break; | |
758 | case 'f': | |
759 | daemon = 0; | |
760 | break; | |
761 | case 'v': | |
762 | version(); | |
763 | license(); | |
764 | exit(0); | |
765 | case 'h': | |
766 | default: | |
767 | usage(argv[0]); | |
768 | exit(0); | |
769 | } | |
770 | } | |
771 | } | |
772 | ||
773 | version(); | |
774 | ||
775 | if (replay_file) { | |
776 | /* We read a line here to "prime" the replay file parser, but | |
777 | * mostly to get the right value of "now" for when we do the | |
778 | * irc_introduce. */ | |
779 | replay_read_line(); | |
780 | boot_time = now; | |
781 | } else { | |
782 | boot_time = time(&now); | |
783 | } | |
784 | ||
785 | log_module(MAIN_LOG, LOG_INFO, "Initializing daemon..."); | |
786 | if (!conf_read(services_config)) { | |
787 | log_module(MAIN_LOG, LOG_FATAL, "Unable to read %s.", services_config); | |
788 | exit(0); | |
789 | } | |
790 | ||
791 | conf_register_reload(uplink_compile); | |
792 | ||
793 | if (daemon) { | |
794 | /* Attempt to fork into the background if daemon mode is on. */ | |
795 | pid = fork(); | |
796 | if (pid < 0) { | |
797 | log_module(MAIN_LOG, LOG_FATAL, "Unable to fork: %s", strerror(errno)); | |
798 | } else if (pid > 0) { | |
799 | log_module(MAIN_LOG, LOG_INFO, "Forking into the background (pid: %i)...", pid); | |
800 | exit(0); | |
801 | } | |
802 | setsid(); | |
803 | /* Close these since we should not use them from now on. */ | |
804 | fclose(stdin); | |
805 | fclose(stdout); | |
806 | fclose(stderr); | |
807 | } | |
808 | ||
809 | if ((file_out = fopen(PID_FILE, "w")) == NULL) { | |
810 | /* Create the main process' pid file */ | |
811 | log_module(MAIN_LOG, LOG_ERROR, "Unable to create PID file: %s", strerror(errno)); | |
812 | } else { | |
813 | fprintf(file_out, "%i\n", (int)getpid()); | |
814 | fclose(file_out); | |
815 | } | |
816 | ||
817 | services_argc = argc; | |
818 | services_argv = argv; | |
819 | ||
820 | atexit(call_exit_funcs); | |
821 | reg_exit_func(main_shutdown); | |
822 | ||
823 | log_init(); | |
824 | MAIN_LOG = log_register_type("x3", "file:main.log"); | |
825 | if (debug) | |
826 | log_debug(); | |
827 | timeq_init(); | |
828 | init_structs(); | |
829 | init_parse(); | |
830 | modcmd_init(); | |
831 | saxdb_init(); | |
832 | gline_init(); | |
833 | sendmail_init(); | |
834 | helpfile_init(); | |
835 | conf_globals(); /* initializes the core services */ | |
836 | conf_rlimits(); | |
837 | modules_init(); | |
838 | message_register_table(msgtab); | |
839 | modcmd_finalize(); | |
840 | saxdb_finalize(); | |
841 | helpfile_finalize(); | |
842 | modules_finalize(); | |
843 | ||
844 | /* The first exit func to be called *should* be saxdb_write_all(). */ | |
845 | reg_exit_func(saxdb_write_all); | |
846 | if (replay_file) { | |
847 | char *msg; | |
848 | log_module(MAIN_LOG, LOG_INFO, "Beginning replay..."); | |
849 | srand(now); | |
850 | replay_event_loop(); | |
851 | if ((msg = dict_sanity_check(clients))) { | |
852 | log_module(MAIN_LOG, LOG_ERROR, "Clients insanity: %s", msg); | |
853 | free(msg); | |
854 | } | |
855 | if ((msg = dict_sanity_check(channels))) { | |
856 | log_module(MAIN_LOG, LOG_ERROR, "Channels insanity: %s", msg); | |
857 | free(msg); | |
858 | } | |
859 | if ((msg = dict_sanity_check(servers))) { | |
860 | log_module(MAIN_LOG, LOG_ERROR, "Servers insanity: %s", msg); | |
861 | free(msg); | |
862 | } | |
863 | } else { | |
864 | srand(time(&now)); | |
865 | ioset_run(); | |
866 | } | |
867 | return 0; | |
868 | } |