]>
Commit | Line | Data |
---|---|---|
212380e3 | 1 | /* |
6fce54ff | 2 | * charybdis: A slightly useful ircd. |
212380e3 AC |
3 | * ircd.c: Starts up and runs the ircd. |
4 | * | |
5 | * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center | |
6 | * Copyright (C) 1996-2002 Hybrid Development Team | |
6fce54ff VY |
7 | * Copyright (C) 2002-2008 ircd-ratbox development team |
8 | * Copyright (C) 2005-2008 charybdis development team | |
212380e3 AC |
9 | * |
10 | * This program is free software; you can redistribute it and/or modify | |
11 | * it under the terms of the GNU General Public License as published by | |
12 | * the Free Software Foundation; either version 2 of the License, or | |
13 | * (at your option) any later version. | |
14 | * | |
15 | * This program is distributed in the hope that it will be useful, | |
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | * GNU General Public License for more details. | |
19 | * | |
20 | * You should have received a copy of the GNU General Public License | |
21 | * along with this program; if not, write to the Free Software | |
22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 | |
23 | * USA | |
24 | * | |
6fce54ff | 25 | * $Id$ |
212380e3 AC |
26 | */ |
27 | ||
2af8c7ff | 28 | #include "ratbox_lib.h" |
212380e3 AC |
29 | #include "stdinc.h" |
30 | #include "setup.h" | |
31 | #include "config.h" | |
212380e3 AC |
32 | #include "ircd.h" |
33 | #include "channel.h" | |
34 | #include "class.h" | |
35 | #include "client.h" | |
36 | #include "common.h" | |
212380e3 | 37 | #include "hash.h" |
4562c604 | 38 | #include "match.h" |
212380e3 | 39 | #include "ircd_signal.h" |
212380e3 AC |
40 | #include "msg.h" /* msgtab */ |
41 | #include "hostmask.h" | |
42 | #include "numeric.h" | |
43 | #include "parse.h" | |
44 | #include "res.h" | |
45 | #include "restart.h" | |
46 | #include "s_auth.h" | |
212380e3 | 47 | #include "s_conf.h" |
4016731b | 48 | #include "logger.h" |
212380e3 AC |
49 | #include "s_serv.h" /* try_connections */ |
50 | #include "s_user.h" | |
51 | #include "s_stats.h" | |
52 | #include "scache.h" | |
53 | #include "send.h" | |
54 | #include "supported.h" | |
55 | #include "whowas.h" | |
56 | #include "modules.h" | |
212380e3 AC |
57 | #include "hook.h" |
58 | #include "ircd_getopt.h" | |
212380e3 | 59 | #include "newconf.h" |
212380e3 AC |
60 | #include "reject.h" |
61 | #include "s_conf.h" | |
62 | #include "s_newconf.h" | |
63 | #include "cache.h" | |
64 | #include "monitor.h" | |
212380e3 AC |
65 | #include "patchlevel.h" |
66 | #include "serno.h" | |
c6d72037 | 67 | #include "sslproc.h" |
efccc22c | 68 | #include "chmode.h" |
212380e3 | 69 | |
8bd5767b | 70 | /* /quote set variables */ |
2af8c7ff | 71 | struct SetOptions GlobalSetOptions; |
8bd5767b JT |
72 | |
73 | /* configuration set from ircd.conf */ | |
74 | struct config_file_entry ConfigFileEntry; | |
75 | /* server info set from ircd.conf */ | |
76 | struct server_info ServerInfo; | |
77 | /* admin info set from ircd.conf */ | |
1aad9782 VY |
78 | struct admin_info AdminInfo; |
79 | ||
8bd5767b | 80 | struct Counter Count; |
47adde3d VY |
81 | struct ServerStatistics ServerStats; |
82 | ||
1aad9782 | 83 | int maxconnections; |
8bd5767b | 84 | struct Client me; /* That's me */ |
1aad9782 VY |
85 | struct LocalUser meLocalUser; /* That's also part of me */ |
86 | ||
3d3d1a95 | 87 | rb_dlink_list global_client_list; |
8bd5767b | 88 | |
3d3d1a95 | 89 | /* unknown/client pointer lists */ |
8bd5767b | 90 | rb_dlink_list unknown_list; /* unknown clients ON this server only */ |
3d3d1a95 | 91 | rb_dlink_list lclient_list; /* local clients only ON this server */ |
8bd5767b JT |
92 | rb_dlink_list serv_list; /* local servers to this server ONLY */ |
93 | rb_dlink_list global_serv_list; /* global servers on the network */ | |
94 | rb_dlink_list local_oper_list; /* our opers, duplicated in lclient_list */ | |
b5b84cad VY |
95 | rb_dlink_list oper_list; /* network opers */ |
96 | ||
8bd5767b | 97 | const char *logFileName = LPATH; |
b5b84cad VY |
98 | const char *pidFileName = PPATH; |
99 | ||
1aad9782 | 100 | char **myargv; |
8bd5767b JT |
101 | int dorehash = 0; |
102 | int dorehashbans = 0; | |
103 | int doremotd = 0; | |
104 | int kline_queued = 0; | |
105 | int server_state_foreground = 0; | |
106 | int opers_see_all_users = 0; | |
107 | int ssl_ok = 0; | |
108 | int zlib_ok = 1; | |
109 | ||
110 | int testing_conf = 0; | |
cd27d99c | 111 | time_t startup_time; |
8bd5767b | 112 | |
cd27d99c | 113 | int default_server_capabs = CAP_MASK; |
8bd5767b | 114 | |
cd27d99c VY |
115 | int splitmode; |
116 | int splitchecking; | |
117 | int split_users; | |
118 | int split_servers; | |
119 | int eob_count; | |
c6d72037 | 120 | |
fd5af3d0 VY |
121 | void |
122 | ircd_shutdown(const char *reason) | |
123 | { | |
124 | struct Client *target_p; | |
125 | rb_dlink_node *ptr; | |
126 | ||
127 | RB_DLINK_FOREACH(ptr, lclient_list.head) | |
128 | { | |
129 | target_p = ptr->data; | |
130 | ||
131 | sendto_one(target_p, ":%s NOTICE %s :Server Terminating. %s", | |
132 | me.name, target_p->name, reason); | |
133 | } | |
134 | ||
135 | RB_DLINK_FOREACH(ptr, serv_list.head) | |
136 | { | |
137 | target_p = ptr->data; | |
138 | ||
139 | sendto_one(target_p, ":%s ERROR :Terminated by %s", | |
140 | me.name, reason); | |
141 | } | |
142 | ||
143 | ilog(L_MAIN, "Server Terminating. %s", reason); | |
ea82a3ca | 144 | close_logfiles(); |
fd5af3d0 VY |
145 | |
146 | unlink(pidFileName); | |
147 | exit(0); | |
148 | } | |
149 | ||
212380e3 AC |
150 | /* |
151 | * print_startup - print startup information | |
152 | */ | |
153 | static void | |
154 | print_startup(int pid) | |
155 | { | |
156 | inotice("now running in %s mode from %s as pid %d ...", | |
157 | !server_state_foreground ? "background" : "foreground", | |
158 | ConfigFileEntry.dpath, pid); | |
159 | ||
160 | /* let the parent process know the initialization was successful | |
161 | * -- jilles */ | |
162 | if (!server_state_foreground) | |
163 | write(0, ".", 1); | |
164 | fclose(stdin); | |
165 | fclose(stdout); | |
166 | fclose(stderr); | |
167 | open("/dev/null", O_RDWR); | |
168 | dup2(0, 1); | |
169 | dup2(0, 2); | |
170 | } | |
171 | ||
212380e3 AC |
172 | /* |
173 | * init_sys | |
174 | * | |
175 | * inputs - boot_daemon flag | |
176 | * output - none | |
177 | * side effects - if boot_daemon flag is not set, don't daemonize | |
178 | */ | |
179 | static void | |
180 | init_sys(void) | |
181 | { | |
8bd5767b JT |
182 | #if defined(RLIMIT_NOFILE) && defined(HAVE_SYS_RESOURCE_H) |
183 | struct rlimit limit; | |
184 | ||
185 | if(!getrlimit(RLIMIT_NOFILE, &limit)) | |
186 | { | |
187 | maxconnections = limit.rlim_cur; | |
188 | if(maxconnections <= MAX_BUFFER) | |
189 | { | |
190 | fprintf(stderr, "ERROR: Shell FD limits are too low.\n"); | |
ec86417c | 191 | fprintf(stderr, "ERROR: charybdis reserves %d FDs, shell limits must be above this\n", MAX_BUFFER); |
8bd5767b JT |
192 | exit(EXIT_FAILURE); |
193 | } | |
194 | return; | |
195 | } | |
196 | #endif /* RLIMIT_FD_MAX */ | |
101db4c4 | 197 | maxconnections = MAXCONNECTIONS; |
212380e3 AC |
198 | } |
199 | ||
200 | static int | |
201 | make_daemon(void) | |
202 | { | |
203 | int pid; | |
204 | int pip[2]; | |
205 | char c; | |
206 | ||
207 | if (pipe(pip) < 0) | |
208 | { | |
209 | perror("pipe"); | |
210 | exit(EXIT_FAILURE); | |
211 | } | |
212 | dup2(pip[1], 0); | |
213 | close(pip[1]); | |
214 | if((pid = fork()) < 0) | |
215 | { | |
216 | perror("fork"); | |
217 | exit(EXIT_FAILURE); | |
218 | } | |
219 | else if(pid > 0) | |
220 | { | |
221 | close(0); | |
222 | /* Wait for initialization to finish, successfully or | |
223 | * unsuccessfully. Until this point the child may still | |
224 | * write to stdout/stderr. | |
225 | * -- jilles */ | |
226 | if (read(pip[0], &c, 1) > 0) | |
227 | exit(EXIT_SUCCESS); | |
228 | else | |
229 | exit(EXIT_FAILURE); | |
230 | } | |
231 | ||
232 | close(pip[0]); | |
233 | setsid(); | |
234 | /* fclose(stdin); | |
235 | fclose(stdout); | |
236 | fclose(stderr); */ | |
237 | ||
238 | return 0; | |
239 | } | |
240 | ||
241 | static int printVersion = 0; | |
242 | ||
243 | struct lgetopt myopts[] = { | |
244 | {"dlinefile", &ConfigFileEntry.dlinefile, | |
245 | STRING, "File to use for dlines.conf"}, | |
246 | {"configfile", &ConfigFileEntry.configfile, | |
247 | STRING, "File to use for ircd.conf"}, | |
248 | {"klinefile", &ConfigFileEntry.klinefile, | |
249 | STRING, "File to use for kline.conf"}, | |
250 | {"xlinefile", &ConfigFileEntry.xlinefile, | |
251 | STRING, "File to use for xline.conf"}, | |
252 | {"resvfile", &ConfigFileEntry.resvfile, | |
253 | STRING, "File to use for resv.conf"}, | |
254 | {"logfile", &logFileName, | |
255 | STRING, "File to use for ircd.log"}, | |
256 | {"pidfile", &pidFileName, | |
257 | STRING, "File to use for process ID"}, | |
258 | {"foreground", &server_state_foreground, | |
259 | YESNO, "Run in foreground (don't detach)"}, | |
260 | {"version", &printVersion, | |
261 | YESNO, "Print version and exit"}, | |
262 | {"conftest", &testing_conf, | |
263 | YESNO, "Test the configuration files and exit"}, | |
264 | {"help", NULL, USAGE, "Print this text"}, | |
265 | {NULL, NULL, STRING, NULL}, | |
266 | }; | |
267 | ||
212380e3 AC |
268 | static void |
269 | check_rehash(void *unused) | |
270 | { | |
271 | /* | |
272 | * Check to see whether we have to rehash the configuration .. | |
273 | */ | |
274 | if(dorehash) | |
275 | { | |
276 | rehash(1); | |
277 | dorehash = 0; | |
278 | } | |
279 | ||
280 | if(dorehashbans) | |
281 | { | |
282 | rehash_bans(1); | |
283 | dorehashbans = 0; | |
284 | } | |
285 | ||
286 | if(doremotd) | |
287 | { | |
288 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
289 | "Got signal SIGUSR1, reloading ircd motd file"); | |
041d54ff | 290 | cache_user_motd(); |
212380e3 AC |
291 | doremotd = 0; |
292 | } | |
293 | } | |
294 | ||
212380e3 AC |
295 | /* |
296 | * initalialize_global_set_options | |
297 | * | |
298 | * inputs - none | |
299 | * output - none | |
300 | * side effects - This sets all global set options needed | |
301 | */ | |
302 | static void | |
303 | initialize_global_set_options(void) | |
304 | { | |
305 | memset(&GlobalSetOptions, 0, sizeof(GlobalSetOptions)); | |
306 | /* memset( &ConfigFileEntry, 0, sizeof(ConfigFileEntry)); */ | |
307 | ||
8bd5767b JT |
308 | GlobalSetOptions.maxclients = ServerInfo.default_max_clients; |
309 | ||
310 | if(GlobalSetOptions.maxclients > (maxconnections - MAX_BUFFER) || (GlobalSetOptions.maxclients <= 0)) | |
101db4c4 VY |
311 | GlobalSetOptions.maxclients = maxconnections - MAX_BUFFER; |
312 | ||
212380e3 AC |
313 | GlobalSetOptions.autoconn = 1; |
314 | ||
315 | GlobalSetOptions.spam_time = MIN_JOIN_LEAVE_TIME; | |
316 | GlobalSetOptions.spam_num = MAX_JOIN_LEAVE_COUNT; | |
317 | ||
318 | if(ConfigFileEntry.default_floodcount) | |
319 | GlobalSetOptions.floodcount = ConfigFileEntry.default_floodcount; | |
320 | else | |
321 | GlobalSetOptions.floodcount = 10; | |
322 | ||
323 | split_servers = ConfigChannel.default_split_server_count; | |
324 | split_users = ConfigChannel.default_split_user_count; | |
325 | ||
326 | if(split_users && split_servers | |
327 | && (ConfigChannel.no_create_on_split || ConfigChannel.no_join_on_split)) | |
328 | { | |
329 | splitmode = 1; | |
330 | splitchecking = 1; | |
331 | } | |
332 | ||
333 | GlobalSetOptions.ident_timeout = IDENT_TIMEOUT; | |
334 | ||
f427c8b0 | 335 | rb_strlcpy(GlobalSetOptions.operstring, |
212380e3 AC |
336 | ConfigFileEntry.default_operstring, |
337 | sizeof(GlobalSetOptions.operstring)); | |
f427c8b0 | 338 | rb_strlcpy(GlobalSetOptions.adminstring, |
212380e3 AC |
339 | ConfigFileEntry.default_adminstring, |
340 | sizeof(GlobalSetOptions.adminstring)); | |
341 | ||
342 | /* memset( &ConfigChannel, 0, sizeof(ConfigChannel)); */ | |
343 | ||
344 | /* End of global set options */ | |
345 | ||
346 | } | |
347 | ||
348 | /* | |
349 | * initialize_server_capabs | |
350 | * | |
351 | * inputs - none | |
352 | * output - none | |
353 | */ | |
354 | static void | |
355 | initialize_server_capabs(void) | |
356 | { | |
357 | default_server_capabs &= ~CAP_ZIP; | |
358 | } | |
359 | ||
360 | ||
361 | /* | |
362 | * write_pidfile | |
363 | * | |
364 | * inputs - filename+path of pid file | |
365 | * output - none | |
366 | * side effects - write the pid of the ircd to filename | |
367 | */ | |
368 | static void | |
369 | write_pidfile(const char *filename) | |
370 | { | |
371 | FILE *fb; | |
372 | char buff[32]; | |
373 | if((fb = fopen(filename, "w"))) | |
374 | { | |
375 | unsigned int pid = (unsigned int) getpid(); | |
376 | ||
b2f0da88 | 377 | rb_snprintf(buff, sizeof(buff), "%u\n", pid); |
212380e3 AC |
378 | if((fputs(buff, fb) == -1)) |
379 | { | |
380 | ilog(L_MAIN, "Error writing %u to pid file %s (%s)", | |
381 | pid, filename, strerror(errno)); | |
382 | } | |
383 | fclose(fb); | |
384 | return; | |
385 | } | |
386 | else | |
387 | { | |
388 | ilog(L_MAIN, "Error opening pid file %s", filename); | |
389 | } | |
390 | } | |
391 | ||
392 | /* | |
393 | * check_pidfile | |
394 | * | |
395 | * inputs - filename+path of pid file | |
396 | * output - none | |
397 | * side effects - reads pid from pidfile and checks if ircd is in process | |
398 | * list. if it is, gracefully exits | |
399 | * -kre | |
400 | */ | |
401 | static void | |
402 | check_pidfile(const char *filename) | |
403 | { | |
404 | FILE *fb; | |
405 | char buff[32]; | |
406 | pid_t pidfromfile; | |
407 | ||
408 | /* Don't do logging here, since we don't have log() initialised */ | |
409 | if((fb = fopen(filename, "r"))) | |
410 | { | |
411 | if(fgets(buff, 20, fb) != NULL) | |
412 | { | |
413 | pidfromfile = atoi(buff); | |
414 | if(!kill(pidfromfile, 0)) | |
415 | { | |
416 | printf("ircd: daemon is already running\n"); | |
417 | exit(-1); | |
418 | } | |
419 | } | |
420 | fclose(fb); | |
421 | } | |
422 | } | |
423 | ||
424 | /* | |
425 | * setup_corefile | |
426 | * | |
427 | * inputs - nothing | |
428 | * output - nothing | |
429 | * side effects - setups corefile to system limits. | |
430 | * -kre | |
431 | */ | |
432 | static void | |
433 | setup_corefile(void) | |
434 | { | |
435 | #ifdef HAVE_SYS_RESOURCE_H | |
436 | struct rlimit rlim; /* resource limits */ | |
437 | ||
438 | /* Set corefilesize to maximum */ | |
439 | if(!getrlimit(RLIMIT_CORE, &rlim)) | |
440 | { | |
441 | rlim.rlim_cur = rlim.rlim_max; | |
442 | setrlimit(RLIMIT_CORE, &rlim); | |
443 | } | |
444 | #endif | |
445 | } | |
446 | ||
6fce54ff VY |
447 | static void |
448 | ircd_log_cb(const char *str) | |
449 | { | |
6643434b | 450 | ilog(L_MAIN, "libratbox reports: %s", str); |
6fce54ff VY |
451 | } |
452 | ||
453 | static void | |
454 | ircd_restart_cb(const char *str) | |
455 | { | |
b1daa815 | 456 | inotice("libratbox has called the restart callback: %s", str); |
6fce54ff VY |
457 | restart(str); |
458 | } | |
459 | ||
460 | /* | |
461 | * Why EXIT_FAILURE here? | |
462 | * Because if ircd_die_cb() is called it's because of a fatal | |
463 | * error inside libcharybdis, and we don't know how to handle the | |
464 | * exception, so it is logical to return a FAILURE exit code here. | |
465 | * --nenolod | |
466 | */ | |
467 | static void | |
468 | ircd_die_cb(const char *str) | |
469 | { | |
470 | if(str != NULL) | |
471 | { | |
472 | /* Try to get the message out to currently logged in operators. */ | |
6643434b VY |
473 | sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, "libratbox has called the die callback..aborting: %s", str); |
474 | inotice("libratbox has called the die callback..aborting: %s", str); | |
6fce54ff | 475 | } |
6643434b | 476 | else |
b1daa815 | 477 | inotice("libratbox has called the die callback..aborting"); |
6fce54ff VY |
478 | |
479 | unlink(pidFileName); | |
480 | exit(EXIT_FAILURE); | |
481 | } | |
482 | ||
fa832850 AC |
483 | struct ev_entry *check_splitmode_ev = NULL; |
484 | ||
d80645d0 VY |
485 | static int |
486 | seed_with_urandom(void) | |
487 | { | |
488 | unsigned int seed; | |
489 | int fd; | |
490 | ||
491 | fd = open("/dev/urandom", O_RDONLY); | |
492 | if(fd >= 0) | |
493 | { | |
494 | if(read(fd, &seed, sizeof(seed)) == sizeof(seed)) | |
495 | { | |
496 | close(fd); | |
497 | srand(seed); | |
498 | return 1; | |
499 | } | |
500 | } | |
501 | return 0; | |
502 | } | |
503 | ||
504 | static void | |
505 | seed_with_clock(void) | |
506 | { | |
507 | const struct timeval *tv; | |
508 | rb_set_time(); | |
509 | tv = rb_current_time_tv(); | |
510 | srand(tv->tv_sec ^ (tv->tv_usec | (getpid() << 20))); | |
511 | } | |
512 | ||
513 | static void | |
514 | seed_random(void *unused) | |
515 | { | |
516 | unsigned int seed; | |
517 | if(rb_get_random(&seed, sizeof(seed)) == -1) | |
518 | { | |
519 | if(!seed_with_urandom()) | |
520 | seed_with_clock(); | |
521 | return; | |
522 | } | |
523 | srand(seed); | |
524 | } | |
525 | ||
212380e3 AC |
526 | /* |
527 | * main | |
528 | * | |
529 | * Initializes the IRCd. | |
530 | * | |
531 | * Inputs - number of commandline args, args themselves | |
532 | * Outputs - none | |
533 | * Side Effects - this is where the ircd gets going right now | |
534 | */ | |
535 | int | |
536 | main(int argc, char *argv[]) | |
537 | { | |
538 | int fd; | |
539 | ||
540 | /* Check to see if the user is running us as root, which is a nono */ | |
541 | if(geteuid() == 0) | |
542 | { | |
543 | fprintf(stderr, "Don't run ircd as root!!!\n"); | |
544 | return -1; | |
545 | } | |
546 | ||
adc7be98 VY |
547 | init_sys(); |
548 | ||
befa24c8 JT |
549 | ConfigFileEntry.dpath = DPATH; |
550 | ConfigFileEntry.configfile = CPATH; /* Server configuration file */ | |
551 | ConfigFileEntry.klinefile = KPATH; /* Server kline file */ | |
552 | ConfigFileEntry.dlinefile = DLPATH; /* dline file */ | |
553 | ConfigFileEntry.xlinefile = XPATH; | |
554 | ConfigFileEntry.resvfile = RESVPATH; | |
555 | ConfigFileEntry.connect_timeout = 30; /* Default to 30 */ | |
556 | ||
557 | umask(077); /* better safe than sorry --SRB */ | |
adc7be98 VY |
558 | |
559 | myargv = argv; | |
560 | parseargs(&argc, &argv, myopts); | |
561 | ||
562 | if(chdir(ConfigFileEntry.dpath)) | |
563 | { | |
564 | fprintf(stderr, "Unable to chdir to %s: %s\n", ConfigFileEntry.dpath, strerror(errno)); | |
565 | exit(EXIT_FAILURE); | |
566 | } | |
567 | ||
3d3d1a95 VY |
568 | rb_set_time(); |
569 | ||
212380e3 AC |
570 | /* |
571 | * Setup corefile size immediately after boot -kre | |
572 | */ | |
573 | setup_corefile(); | |
574 | ||
212380e3 AC |
575 | memset(&me, 0, sizeof(me)); |
576 | memset(&meLocalUser, 0, sizeof(meLocalUser)); | |
577 | me.localClient = &meLocalUser; | |
578 | ||
579 | /* Make sure all lists are zeroed */ | |
580 | memset(&unknown_list, 0, sizeof(unknown_list)); | |
581 | memset(&lclient_list, 0, sizeof(lclient_list)); | |
582 | memset(&serv_list, 0, sizeof(serv_list)); | |
583 | memset(&global_serv_list, 0, sizeof(global_serv_list)); | |
584 | memset(&local_oper_list, 0, sizeof(local_oper_list)); | |
585 | memset(&oper_list, 0, sizeof(oper_list)); | |
586 | ||
330fc5c1 | 587 | rb_dlinkAddTail(&me, &me.node, &global_client_list); |
212380e3 | 588 | |
2af8c7ff VY |
589 | memset(&Count, 0, sizeof(Count)); |
590 | memset(&ServerInfo, 0, sizeof(ServerInfo)); | |
591 | memset(&AdminInfo, 0, sizeof(AdminInfo)); | |
592 | memset(&ServerStats, 0, sizeof(struct ServerStatistics)); | |
212380e3 AC |
593 | |
594 | /* Initialise the channel capability usage counts... */ | |
595 | init_chcap_usage_counts(); | |
596 | ||
212380e3 AC |
597 | if(printVersion) |
598 | { | |
b3803848 | 599 | printf("ircd: version %s(%s)\n", ircd_version, serno); |
212380e3 AC |
600 | exit(EXIT_SUCCESS); |
601 | } | |
602 | ||
adc7be98 | 603 | |
212380e3 AC |
604 | |
605 | setup_signals(); | |
606 | ||
212380e3 AC |
607 | if (testing_conf) |
608 | server_state_foreground = 1; | |
609 | ||
610 | /* Make sure fd 0, 1 and 2 are in use -- jilles */ | |
611 | do | |
612 | { | |
613 | fd = open("/dev/null", O_RDWR); | |
614 | } while (fd < 2 && fd != -1); | |
615 | if (fd > 2) | |
616 | close(fd); | |
617 | else if (fd == -1) | |
618 | exit(1); | |
619 | ||
620 | /* Check if there is pidfile and daemon already running */ | |
621 | if(!testing_conf) | |
622 | { | |
623 | check_pidfile(pidFileName); | |
624 | ||
625 | if(!server_state_foreground) | |
626 | make_daemon(); | |
627 | inotice("starting %s ...", ircd_version); | |
628 | } | |
629 | ||
630 | /* Init the event subsystem */ | |
0edb29fa | 631 | rb_lib_init(ircd_log_cb, ircd_restart_cb, ircd_die_cb, !server_state_foreground, maxconnections, DNODE_HEAP_SIZE, FD_HEAP_SIZE); |
150f35f7 | 632 | rb_linebuf_init(LINEBUF_HEAP_SIZE); |
212380e3 | 633 | |
37cd159e VY |
634 | if(ConfigFileEntry.use_egd && (ConfigFileEntry.egdpool_path != NULL)) |
635 | { | |
636 | rb_init_prng(ConfigFileEntry.egdpool_path, RB_PRNG_EGD); | |
637 | } else | |
638 | rb_init_prng(NULL, RB_PRNG_DEFAULT); | |
639 | ||
e1db84d8 VY |
640 | seed_random(NULL); |
641 | ||
212380e3 | 642 | init_main_logfile(); |
212380e3 AC |
643 | newconf_init(); |
644 | init_s_conf(); | |
645 | init_s_newconf(); | |
646 | init_hash(); | |
647 | clear_scache_hash_table(); /* server cache name table */ | |
648 | init_host_hash(); | |
649 | clear_hash_parse(); | |
650 | init_client(); | |
212380e3 AC |
651 | init_hook(); |
652 | init_channels(); | |
653 | initclass(); | |
654 | initwhowas(); | |
212380e3 AC |
655 | init_reject(); |
656 | init_cache(); | |
657 | init_monitor(); | |
658 | init_isupport(); | |
659 | load_all_modules(1); | |
660 | #ifndef STATIC_MODULES | |
661 | load_core_modules(1); | |
662 | #endif | |
663 | init_auth(); /* Initialise the auth code */ | |
664 | init_resolver(); /* Needs to be setup before the io loop */ | |
665 | ||
666 | if (testing_conf) | |
667 | fprintf(stderr, "\nBeginning config test\n"); | |
668 | read_conf_files(YES); /* cold start init conf files */ | |
669 | rehash_bans(0); | |
670 | #ifndef STATIC_MODULES | |
671 | ||
672 | mod_add_path(MODULE_DIR); | |
673 | mod_add_path(MODULE_DIR "/autoload"); | |
674 | #endif | |
675 | ||
c6d72037 VY |
676 | init_ssld(); |
677 | ||
212380e3 AC |
678 | initialize_server_capabs(); /* Set up default_server_capabs */ |
679 | initialize_global_set_options(); | |
680 | ||
681 | if(ServerInfo.name == NULL) | |
682 | { | |
683 | ierror("no server name specified in serverinfo block."); | |
684 | return -1; | |
685 | } | |
f427c8b0 | 686 | rb_strlcpy(me.name, ServerInfo.name, sizeof(me.name)); |
212380e3 AC |
687 | |
688 | if(ServerInfo.sid[0] == '\0') | |
689 | { | |
690 | ierror("no server sid specified in serverinfo block."); | |
691 | return -2; | |
692 | } | |
693 | strcpy(me.id, ServerInfo.sid); | |
694 | init_uid(); | |
695 | ||
696 | /* serverinfo{} description must exist. If not, error out. */ | |
697 | if(ServerInfo.description == NULL) | |
698 | { | |
699 | ierror("no server description specified in serverinfo block."); | |
700 | return -3; | |
701 | } | |
f427c8b0 | 702 | rb_strlcpy(me.info, ServerInfo.description, sizeof(me.info)); |
212380e3 | 703 | |
8bd5767b JT |
704 | if(ServerInfo.ssl_cert != NULL && ServerInfo.ssl_private_key != NULL) |
705 | { | |
706 | /* just do the rb_setup_ssl_server to validate the config */ | |
707 | if(!rb_setup_ssl_server(ServerInfo.ssl_cert, ServerInfo.ssl_private_key, ServerInfo.ssl_dh_params)) | |
708 | { | |
709 | ilog(L_MAIN, "WARNING: Unable to setup SSL."); | |
710 | ssl_ok = 0; | |
711 | } | |
712 | else | |
713 | ssl_ok = 1; | |
c6d72037 VY |
714 | } |
715 | ||
212380e3 AC |
716 | if (testing_conf) |
717 | { | |
718 | fprintf(stderr, "\nConfig testing complete.\n"); | |
719 | fflush(stderr); | |
720 | return 0; /* Why? We want the launcher to exit out. */ | |
721 | } | |
722 | ||
723 | me.from = &me; | |
724 | me.servptr = &me; | |
725 | SetMe(&me); | |
726 | make_server(&me); | |
e3354945 | 727 | startup_time = rb_current_time(); |
212380e3 AC |
728 | add_to_client_hash(me.name, &me); |
729 | add_to_id_hash(me.id, &me); | |
994544c2 | 730 | me.serv->nameinfo = scache_connect(me.name, me.info, 0); |
212380e3 | 731 | |
330fc5c1 | 732 | rb_dlinkAddAlloc(&me, &global_serv_list); |
212380e3 AC |
733 | |
734 | construct_umodebuf(); | |
efccc22c | 735 | construct_noparam_modes(); |
212380e3 AC |
736 | |
737 | check_class(); | |
738 | write_pidfile(pidFileName); | |
739 | load_help(); | |
740 | open_logfiles(); | |
741 | ||
742 | ilog(L_MAIN, "Server Ready"); | |
743 | ||
212380e3 AC |
744 | /* We want try_connections to be called as soon as possible now! -- adrian */ |
745 | /* No, 'cause after a restart it would cause all sorts of nick collides */ | |
746 | /* um. by waiting even longer, that just means we have even *more* | |
747 | * nick collisions. what a stupid idea. set an event for the IO loop --fl | |
748 | */ | |
12aea5fe | 749 | rb_event_addish("try_connections", try_connections, NULL, STARTUP_CONNECTIONS_TIME); |
36645728 VY |
750 | rb_event_addonce("try_connections_startup", try_connections, NULL, 2); |
751 | rb_event_add("check_rehash", check_rehash, NULL, 3); | |
d80645d0 | 752 | rb_event_addish("reseed_srand", seed_random, NULL, 300); /* reseed every 10 minutes */ |
212380e3 | 753 | |
212380e3 | 754 | if(splitmode) |
36645728 | 755 | check_splitmode_ev = rb_event_add("check_splitmode", check_splitmode, NULL, 5); |
212380e3 | 756 | |
212380e3 AC |
757 | print_startup(getpid()); |
758 | ||
39944e1f | 759 | rb_lib_loop(0); |
212380e3 AC |
760 | |
761 | return 0; | |
762 | } |