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