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