]>
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 | 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 AC |
55 | #include "reject.h" |
56 | #include "s_conf.h" | |
57 | #include "s_newconf.h" | |
58 | #include "cache.h" | |
59 | #include "monitor.h" | |
212380e3 AC |
60 | #include "patchlevel.h" |
61 | #include "serno.h" | |
c6d72037 | 62 | #include "sslproc.h" |
c53ca1e0 | 63 | #include "wsproc.h" |
efccc22c | 64 | #include "chmode.h" |
39a68b53 | 65 | #include "privilege.h" |
6eebc373 | 66 | #include "bandbi.h" |
64fae260 | 67 | #include "authproc.h" |
b02a913b | 68 | #include "operhash.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 | ||
1aad9782 | 97 | char **myargv; |
33d43d4f MU |
98 | volatile sig_atomic_t dorehash = false; |
99 | volatile sig_atomic_t dorehashbans = false; | |
100 | volatile sig_atomic_t doremotd = false; | |
f66f0baa | 101 | bool kline_queued = false; |
503727d1 EM |
102 | bool server_state_foreground = false; |
103 | bool opers_see_all_users = false; | |
bfc44622 AC |
104 | bool ircd_ssl_ok = false; |
105 | bool ircd_zlib_ok = true; | |
8bd5767b JT |
106 | |
107 | int testing_conf = 0; | |
cd27d99c | 108 | time_t startup_time; |
8bd5767b | 109 | |
346fba92 | 110 | int default_server_capabs; |
8bd5767b | 111 | |
cd27d99c VY |
112 | int splitmode; |
113 | int splitchecking; | |
114 | int split_users; | |
115 | int split_servers; | |
116 | int eob_count; | |
c6d72037 | 117 | |
4d8cfacd AC |
118 | const char *ircd_paths[IRCD_PATH_COUNT] = { |
119 | [IRCD_PATH_PREFIX] = DPATH, | |
120 | [IRCD_PATH_MODULES] = MODPATH, | |
121 | [IRCD_PATH_AUTOLOAD_MODULES] = AUTOMODPATH, | |
122 | [IRCD_PATH_ETC] = ETCPATH, | |
123 | [IRCD_PATH_LOG] = LOGPATH, | |
124 | [IRCD_PATH_USERHELP] = UHPATH, | |
125 | [IRCD_PATH_OPERHELP] = HPATH, | |
126 | [IRCD_PATH_IRCD_EXEC] = SPATH, | |
127 | [IRCD_PATH_IRCD_CONF] = CPATH, | |
128 | [IRCD_PATH_IRCD_MOTD] = MPATH, | |
129 | [IRCD_PATH_IRCD_LOG] = LPATH, | |
130 | [IRCD_PATH_IRCD_PID] = PPATH, | |
131 | [IRCD_PATH_IRCD_OMOTD] = OPATH, | |
132 | [IRCD_PATH_BANDB] = DBPATH, | |
133 | [IRCD_PATH_BIN] = BINPATH, | |
134 | [IRCD_PATH_LIBEXEC] = PKGLIBEXECDIR, | |
135 | }; | |
136 | ||
2f545aad AC |
137 | const char *logFileName = NULL; |
138 | const char *pidFileName = NULL; | |
4d8cfacd | 139 | |
fd5af3d0 VY |
140 | void |
141 | ircd_shutdown(const char *reason) | |
142 | { | |
143 | struct Client *target_p; | |
144 | rb_dlink_node *ptr; | |
145 | ||
146 | RB_DLINK_FOREACH(ptr, lclient_list.head) | |
147 | { | |
148 | target_p = ptr->data; | |
149 | ||
150 | sendto_one(target_p, ":%s NOTICE %s :Server Terminating. %s", | |
151 | me.name, target_p->name, reason); | |
152 | } | |
153 | ||
154 | RB_DLINK_FOREACH(ptr, serv_list.head) | |
155 | { | |
156 | target_p = ptr->data; | |
157 | ||
158 | sendto_one(target_p, ":%s ERROR :Terminated by %s", | |
159 | me.name, reason); | |
160 | } | |
161 | ||
162 | ilog(L_MAIN, "Server Terminating. %s", reason); | |
ea82a3ca | 163 | close_logfiles(); |
fd5af3d0 VY |
164 | |
165 | unlink(pidFileName); | |
166 | exit(0); | |
167 | } | |
168 | ||
212380e3 AC |
169 | /* |
170 | * print_startup - print startup information | |
171 | */ | |
172 | static void | |
173 | print_startup(int pid) | |
174 | { | |
b45b2dae JT |
175 | int fd; |
176 | ||
c1bfea73 | 177 | #ifndef _WIN32 |
b45b2dae JT |
178 | close(1); |
179 | fd = open("/dev/null", O_RDWR); | |
180 | if (fd == -1) { | |
181 | perror("open /dev/null"); | |
182 | exit(EXIT_FAILURE); | |
183 | } | |
184 | if (fd == 0) | |
185 | fd = dup(fd); | |
186 | if (fd != 1) | |
187 | abort(); | |
c1bfea73 | 188 | #endif |
4d383612 | 189 | inotice("runtime path: %s", rb_path_to_self()); |
212380e3 AC |
190 | inotice("now running in %s mode from %s as pid %d ...", |
191 | !server_state_foreground ? "background" : "foreground", | |
192 | ConfigFileEntry.dpath, pid); | |
193 | ||
c1bfea73 | 194 | #ifndef _WIN32 |
212380e3 AC |
195 | /* let the parent process know the initialization was successful |
196 | * -- jilles */ | |
197 | if (!server_state_foreground) | |
c084fcaf | 198 | { |
2ac4ba96 EM |
199 | /* GCC complains on Linux if we don't check the value of write pedantically. |
200 | * Technically you're supposed to check the value, yes, but it probably can't fail. | |
201 | * No, casting to void is of no use to shut the warning up. You HAVE to use the value. | |
202 | * --Elizfaox | |
203 | */ | |
204 | if(write(0, ".", 1) < 1) | |
205 | abort(); | |
c084fcaf | 206 | } |
b45b2dae JT |
207 | if (dup2(1, 0) == -1) |
208 | abort(); | |
209 | if (dup2(1, 2) == -1) | |
210 | abort(); | |
c1bfea73 | 211 | #endif |
212380e3 AC |
212 | } |
213 | ||
212380e3 AC |
214 | /* |
215 | * init_sys | |
216 | * | |
217 | * inputs - boot_daemon flag | |
218 | * output - none | |
219 | * side effects - if boot_daemon flag is not set, don't daemonize | |
220 | */ | |
221 | static void | |
222 | init_sys(void) | |
223 | { | |
c1bfea73 | 224 | #if !defined(_WIN32) && defined(RLIMIT_NOFILE) && defined(HAVE_SYS_RESOURCE_H) |
8bd5767b JT |
225 | struct rlimit limit; |
226 | ||
227 | if(!getrlimit(RLIMIT_NOFILE, &limit)) | |
228 | { | |
229 | maxconnections = limit.rlim_cur; | |
230 | if(maxconnections <= MAX_BUFFER) | |
231 | { | |
232 | fprintf(stderr, "ERROR: Shell FD limits are too low.\n"); | |
ec86417c | 233 | fprintf(stderr, "ERROR: charybdis reserves %d FDs, shell limits must be above this\n", MAX_BUFFER); |
8bd5767b JT |
234 | exit(EXIT_FAILURE); |
235 | } | |
236 | return; | |
237 | } | |
238 | #endif /* RLIMIT_FD_MAX */ | |
101db4c4 | 239 | maxconnections = MAXCONNECTIONS; |
212380e3 AC |
240 | } |
241 | ||
242 | static int | |
243 | make_daemon(void) | |
244 | { | |
538d2089 | 245 | #ifndef _WIN32 |
212380e3 AC |
246 | int pid; |
247 | int pip[2]; | |
248 | char c; | |
249 | ||
250 | if (pipe(pip) < 0) | |
251 | { | |
252 | perror("pipe"); | |
253 | exit(EXIT_FAILURE); | |
254 | } | |
255 | dup2(pip[1], 0); | |
256 | close(pip[1]); | |
257 | if((pid = fork()) < 0) | |
258 | { | |
259 | perror("fork"); | |
260 | exit(EXIT_FAILURE); | |
261 | } | |
262 | else if(pid > 0) | |
263 | { | |
264 | close(0); | |
265 | /* Wait for initialization to finish, successfully or | |
266 | * unsuccessfully. Until this point the child may still | |
267 | * write to stdout/stderr. | |
268 | * -- jilles */ | |
269 | if (read(pip[0], &c, 1) > 0) | |
270 | exit(EXIT_SUCCESS); | |
271 | else | |
272 | exit(EXIT_FAILURE); | |
273 | } | |
274 | ||
275 | close(pip[0]); | |
276 | setsid(); | |
277 | /* fclose(stdin); | |
278 | fclose(stdout); | |
279 | fclose(stderr); */ | |
538d2089 | 280 | #endif |
212380e3 AC |
281 | return 0; |
282 | } | |
283 | ||
284 | static int printVersion = 0; | |
285 | ||
286 | struct lgetopt myopts[] = { | |
212380e3 AC |
287 | {"configfile", &ConfigFileEntry.configfile, |
288 | STRING, "File to use for ircd.conf"}, | |
212380e3 AC |
289 | {"logfile", &logFileName, |
290 | STRING, "File to use for ircd.log"}, | |
291 | {"pidfile", &pidFileName, | |
292 | STRING, "File to use for process ID"}, | |
293 | {"foreground", &server_state_foreground, | |
294 | YESNO, "Run in foreground (don't detach)"}, | |
295 | {"version", &printVersion, | |
296 | YESNO, "Print version and exit"}, | |
297 | {"conftest", &testing_conf, | |
298 | YESNO, "Test the configuration files and exit"}, | |
299 | {"help", NULL, USAGE, "Print this text"}, | |
300 | {NULL, NULL, STRING, NULL}, | |
301 | }; | |
302 | ||
212380e3 AC |
303 | static void |
304 | check_rehash(void *unused) | |
305 | { | |
306 | /* | |
307 | * Check to see whether we have to rehash the configuration .. | |
308 | */ | |
309 | if(dorehash) | |
310 | { | |
ab31d2b0 | 311 | rehash(true); |
1b916de5 | 312 | dorehash = false; |
212380e3 AC |
313 | } |
314 | ||
315 | if(dorehashbans) | |
316 | { | |
ab31d2b0 | 317 | rehash_bans(); |
1b916de5 | 318 | dorehashbans = false; |
212380e3 AC |
319 | } |
320 | ||
321 | if(doremotd) | |
322 | { | |
323 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
324 | "Got signal SIGUSR1, reloading ircd motd file"); | |
041d54ff | 325 | cache_user_motd(); |
1b916de5 | 326 | doremotd = false; |
212380e3 AC |
327 | } |
328 | } | |
329 | ||
212380e3 AC |
330 | /* |
331 | * initalialize_global_set_options | |
332 | * | |
333 | * inputs - none | |
334 | * output - none | |
55abcbb2 | 335 | * side effects - This sets all global set options needed |
212380e3 AC |
336 | */ |
337 | static void | |
338 | initialize_global_set_options(void) | |
339 | { | |
340 | memset(&GlobalSetOptions, 0, sizeof(GlobalSetOptions)); | |
341 | /* memset( &ConfigFileEntry, 0, sizeof(ConfigFileEntry)); */ | |
342 | ||
8bd5767b JT |
343 | GlobalSetOptions.maxclients = ServerInfo.default_max_clients; |
344 | ||
345 | if(GlobalSetOptions.maxclients > (maxconnections - MAX_BUFFER) || (GlobalSetOptions.maxclients <= 0)) | |
101db4c4 VY |
346 | GlobalSetOptions.maxclients = maxconnections - MAX_BUFFER; |
347 | ||
212380e3 AC |
348 | GlobalSetOptions.autoconn = 1; |
349 | ||
350 | GlobalSetOptions.spam_time = MIN_JOIN_LEAVE_TIME; | |
351 | GlobalSetOptions.spam_num = MAX_JOIN_LEAVE_COUNT; | |
352 | ||
7f3382fe | 353 | GlobalSetOptions.floodcount = ConfigFileEntry.default_floodcount; |
212380e3 AC |
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 | ||
7f3382fe | 365 | GlobalSetOptions.ident_timeout = ConfigFileEntry.default_ident_timeout; |
212380e3 | 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 | ||
d6c17e5d AC |
392 | /* |
393 | * relocate_paths | |
394 | * | |
395 | * inputs - none | |
396 | * output - none | |
397 | * side effects - items in ircd_paths[] array are relocated | |
398 | */ | |
0aaa37f7 | 399 | #ifdef _WIN32 |
d6c17e5d AC |
400 | static void |
401 | relocate_paths(void) | |
402 | { | |
403 | char prefix[PATH_MAX], workbuf[PATH_MAX]; | |
404 | char *p; | |
405 | ||
406 | rb_strlcpy(prefix, rb_path_to_self(), sizeof prefix); | |
407 | ||
408 | ircd_paths[IRCD_PATH_IRCD_EXEC] = rb_strdup(prefix); | |
409 | ||
410 | /* if we're running from inside the source tree, we probably do not want to relocate any other paths */ | |
411 | if (strstr(prefix, ".libs") != NULL) | |
412 | return; | |
413 | ||
414 | /* prefix = /home/kaniini/ircd/bin/ircd */ | |
415 | p = strrchr(prefix, RB_PATH_SEPARATOR); | |
416 | if (rb_unlikely(p == NULL)) | |
417 | return; | |
418 | *p = 0; | |
419 | ||
420 | /* prefix = /home/kaniini/ircd/bin */ | |
421 | p = strrchr(prefix, RB_PATH_SEPARATOR); | |
422 | if (rb_unlikely(p == NULL)) | |
423 | return; | |
424 | *p = 0; | |
425 | ||
426 | /* prefix = /home/kaniini/ircd */ | |
427 | ircd_paths[IRCD_PATH_PREFIX] = rb_strdup(prefix); | |
428 | ||
429 | /* now that we have our prefix, we can relocate the other paths... */ | |
430 | snprintf(workbuf, sizeof workbuf, "%s%cmodules", prefix, RB_PATH_SEPARATOR); | |
431 | ircd_paths[IRCD_PATH_MODULES] = rb_strdup(workbuf); | |
432 | ||
433 | snprintf(workbuf, sizeof workbuf, "%s%cmodules%cautoload", prefix, RB_PATH_SEPARATOR, RB_PATH_SEPARATOR); | |
434 | ircd_paths[IRCD_PATH_AUTOLOAD_MODULES] = rb_strdup(workbuf); | |
435 | ||
436 | snprintf(workbuf, sizeof workbuf, "%s%cetc", prefix, RB_PATH_SEPARATOR); | |
437 | ircd_paths[IRCD_PATH_ETC] = rb_strdup(workbuf); | |
438 | ||
439 | snprintf(workbuf, sizeof workbuf, "%s%clog", prefix, RB_PATH_SEPARATOR); | |
440 | ircd_paths[IRCD_PATH_LOG] = rb_strdup(workbuf); | |
441 | ||
442 | snprintf(workbuf, sizeof workbuf, "%s%chelp%cusers", prefix, RB_PATH_SEPARATOR, RB_PATH_SEPARATOR); | |
443 | ircd_paths[IRCD_PATH_USERHELP] = rb_strdup(workbuf); | |
444 | ||
445 | snprintf(workbuf, sizeof workbuf, "%s%chelp%copers", prefix, RB_PATH_SEPARATOR, RB_PATH_SEPARATOR); | |
446 | ircd_paths[IRCD_PATH_OPERHELP] = rb_strdup(workbuf); | |
447 | ||
448 | snprintf(workbuf, sizeof workbuf, "%s%cetc%circd.conf", prefix, RB_PATH_SEPARATOR, RB_PATH_SEPARATOR); | |
449 | ircd_paths[IRCD_PATH_IRCD_CONF] = rb_strdup(workbuf); | |
450 | ||
451 | snprintf(workbuf, sizeof workbuf, "%s%cetc%circd.motd", prefix, RB_PATH_SEPARATOR, RB_PATH_SEPARATOR); | |
452 | ircd_paths[IRCD_PATH_IRCD_MOTD] = rb_strdup(workbuf); | |
453 | ||
454 | snprintf(workbuf, sizeof workbuf, "%s%cetc%copers.motd", prefix, RB_PATH_SEPARATOR, RB_PATH_SEPARATOR); | |
455 | ircd_paths[IRCD_PATH_IRCD_OMOTD] = rb_strdup(workbuf); | |
456 | ||
457 | snprintf(workbuf, sizeof workbuf, "%s%cetc%cban.db", prefix, RB_PATH_SEPARATOR, RB_PATH_SEPARATOR); | |
458 | ircd_paths[IRCD_PATH_BANDB] = rb_strdup(workbuf); | |
459 | ||
460 | snprintf(workbuf, sizeof workbuf, "%s%cetc%circd.pid", prefix, RB_PATH_SEPARATOR, RB_PATH_SEPARATOR); | |
461 | ircd_paths[IRCD_PATH_IRCD_PID] = rb_strdup(workbuf); | |
462 | ||
463 | snprintf(workbuf, sizeof workbuf, "%s%clogs%circd.log", prefix, RB_PATH_SEPARATOR, RB_PATH_SEPARATOR); | |
464 | ircd_paths[IRCD_PATH_IRCD_LOG] = rb_strdup(workbuf); | |
465 | ||
466 | snprintf(workbuf, sizeof workbuf, "%s%cbin", prefix, RB_PATH_SEPARATOR); | |
467 | ircd_paths[IRCD_PATH_BIN] = rb_strdup(workbuf); | |
468 | ircd_paths[IRCD_PATH_LIBEXEC] = rb_strdup(workbuf); | |
469 | } | |
0aaa37f7 | 470 | #endif |
212380e3 AC |
471 | |
472 | /* | |
473 | * write_pidfile | |
474 | * | |
475 | * inputs - filename+path of pid file | |
476 | * output - none | |
477 | * side effects - write the pid of the ircd to filename | |
478 | */ | |
479 | static void | |
480 | write_pidfile(const char *filename) | |
481 | { | |
482 | FILE *fb; | |
483 | char buff[32]; | |
484 | if((fb = fopen(filename, "w"))) | |
485 | { | |
486 | unsigned int pid = (unsigned int) getpid(); | |
487 | ||
5203cba5 | 488 | snprintf(buff, sizeof(buff), "%u\n", pid); |
212380e3 AC |
489 | if((fputs(buff, fb) == -1)) |
490 | { | |
491 | ilog(L_MAIN, "Error writing %u to pid file %s (%s)", | |
492 | pid, filename, strerror(errno)); | |
493 | } | |
494 | fclose(fb); | |
495 | return; | |
496 | } | |
497 | else | |
498 | { | |
499 | ilog(L_MAIN, "Error opening pid file %s", filename); | |
500 | } | |
501 | } | |
502 | ||
503 | /* | |
504 | * check_pidfile | |
505 | * | |
506 | * inputs - filename+path of pid file | |
507 | * output - none | |
508 | * side effects - reads pid from pidfile and checks if ircd is in process | |
509 | * list. if it is, gracefully exits | |
510 | * -kre | |
511 | */ | |
512 | static void | |
513 | check_pidfile(const char *filename) | |
514 | { | |
515 | FILE *fb; | |
516 | char buff[32]; | |
517 | pid_t pidfromfile; | |
518 | ||
519 | /* Don't do logging here, since we don't have log() initialised */ | |
520 | if((fb = fopen(filename, "r"))) | |
521 | { | |
522 | if(fgets(buff, 20, fb) != NULL) | |
523 | { | |
524 | pidfromfile = atoi(buff); | |
538d2089 | 525 | if(!rb_kill(pidfromfile, 0)) |
212380e3 AC |
526 | { |
527 | printf("ircd: daemon is already running\n"); | |
528 | exit(-1); | |
529 | } | |
530 | } | |
531 | fclose(fb); | |
532 | } | |
533 | } | |
534 | ||
535 | /* | |
536 | * setup_corefile | |
537 | * | |
538 | * inputs - nothing | |
539 | * output - nothing | |
540 | * side effects - setups corefile to system limits. | |
541 | * -kre | |
542 | */ | |
543 | static void | |
544 | setup_corefile(void) | |
545 | { | |
546 | #ifdef HAVE_SYS_RESOURCE_H | |
547 | struct rlimit rlim; /* resource limits */ | |
548 | ||
549 | /* Set corefilesize to maximum */ | |
550 | if(!getrlimit(RLIMIT_CORE, &rlim)) | |
551 | { | |
552 | rlim.rlim_cur = rlim.rlim_max; | |
553 | setrlimit(RLIMIT_CORE, &rlim); | |
554 | } | |
555 | #endif | |
556 | } | |
557 | ||
6fce54ff VY |
558 | static void |
559 | ircd_log_cb(const char *str) | |
560 | { | |
fe037171 | 561 | ilog(L_MAIN, "librb reports: %s", str); |
6fce54ff VY |
562 | } |
563 | ||
564 | static void | |
565 | ircd_restart_cb(const char *str) | |
566 | { | |
fe037171 | 567 | inotice("librb has called the restart callback: %s", str); |
6fce54ff VY |
568 | restart(str); |
569 | } | |
570 | ||
571 | /* | |
572 | * Why EXIT_FAILURE here? | |
573 | * Because if ircd_die_cb() is called it's because of a fatal | |
574 | * error inside libcharybdis, and we don't know how to handle the | |
575 | * exception, so it is logical to return a FAILURE exit code here. | |
576 | * --nenolod | |
577 | */ | |
578 | static void | |
579 | ircd_die_cb(const char *str) | |
580 | { | |
581 | if(str != NULL) | |
582 | { | |
583 | /* Try to get the message out to currently logged in operators. */ | |
fe037171 EM |
584 | sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, "librb has called the die callback..aborting: %s", str); |
585 | inotice("librb has called the die callback..aborting: %s", str); | |
6fce54ff | 586 | } |
6643434b | 587 | else |
fe037171 | 588 | inotice("librb has called the die callback..aborting"); |
6fce54ff VY |
589 | |
590 | unlink(pidFileName); | |
591 | exit(EXIT_FAILURE); | |
592 | } | |
593 | ||
fa832850 AC |
594 | struct ev_entry *check_splitmode_ev = NULL; |
595 | ||
d80645d0 VY |
596 | static int |
597 | seed_with_urandom(void) | |
598 | { | |
599 | unsigned int seed; | |
600 | int fd; | |
601 | ||
602 | fd = open("/dev/urandom", O_RDONLY); | |
603 | if(fd >= 0) | |
604 | { | |
605 | if(read(fd, &seed, sizeof(seed)) == sizeof(seed)) | |
606 | { | |
607 | close(fd); | |
608 | srand(seed); | |
609 | return 1; | |
610 | } | |
5457b102 | 611 | close(fd); |
d80645d0 VY |
612 | } |
613 | return 0; | |
614 | } | |
615 | ||
616 | static void | |
617 | seed_with_clock(void) | |
618 | { | |
55abcbb2 | 619 | const struct timeval *tv; |
d80645d0 VY |
620 | rb_set_time(); |
621 | tv = rb_current_time_tv(); | |
622 | srand(tv->tv_sec ^ (tv->tv_usec | (getpid() << 20))); | |
623 | } | |
624 | ||
625 | static void | |
626 | seed_random(void *unused) | |
627 | { | |
628 | unsigned int seed; | |
629 | if(rb_get_random(&seed, sizeof(seed)) == -1) | |
630 | { | |
631 | if(!seed_with_urandom()) | |
632 | seed_with_clock(); | |
633 | return; | |
634 | } | |
635 | srand(seed); | |
636 | } | |
637 | ||
212380e3 AC |
638 | /* |
639 | * main | |
640 | * | |
641 | * Initializes the IRCd. | |
642 | * | |
643 | * Inputs - number of commandline args, args themselves | |
644 | * Outputs - none | |
645 | * Side Effects - this is where the ircd gets going right now | |
646 | */ | |
647 | int | |
bc38c72c | 648 | charybdis_main(int argc, char *argv[]) |
212380e3 AC |
649 | { |
650 | int fd; | |
651 | ||
1ba9eba5 | 652 | #ifndef _WIN32 |
212380e3 AC |
653 | /* Check to see if the user is running us as root, which is a nono */ |
654 | if(geteuid() == 0) | |
655 | { | |
656 | fprintf(stderr, "Don't run ircd as root!!!\n"); | |
657 | return -1; | |
658 | } | |
1ba9eba5 | 659 | #endif |
212380e3 | 660 | |
0aaa37f7 | 661 | #ifdef _WIN32 |
d6c17e5d AC |
662 | relocate_paths(); |
663 | #endif | |
adc7be98 | 664 | |
2f545aad AC |
665 | logFileName = ircd_paths[IRCD_PATH_IRCD_LOG]; |
666 | pidFileName = ircd_paths[IRCD_PATH_IRCD_PID]; | |
667 | ||
4d8cfacd AC |
668 | ConfigFileEntry.dpath = ircd_paths[IRCD_PATH_PREFIX]; |
669 | ConfigFileEntry.configfile = ircd_paths[IRCD_PATH_IRCD_CONF]; /* Server configuration file */ | |
befa24c8 | 670 | ConfigFileEntry.connect_timeout = 30; /* Default to 30 */ |
346fba92 | 671 | |
d6c17e5d AC |
672 | init_sys(); |
673 | ||
befa24c8 | 674 | umask(077); /* better safe than sorry --SRB */ |
adc7be98 VY |
675 | |
676 | myargv = argv; | |
677 | parseargs(&argc, &argv, myopts); | |
678 | ||
679 | if(chdir(ConfigFileEntry.dpath)) | |
680 | { | |
681 | fprintf(stderr, "Unable to chdir to %s: %s\n", ConfigFileEntry.dpath, strerror(errno)); | |
682 | exit(EXIT_FAILURE); | |
683 | } | |
684 | ||
3d3d1a95 VY |
685 | rb_set_time(); |
686 | ||
212380e3 AC |
687 | /* |
688 | * Setup corefile size immediately after boot -kre | |
689 | */ | |
690 | setup_corefile(); | |
691 | ||
b02a913b AC |
692 | /* initialise operhash fairly early. */ |
693 | init_operhash(); | |
694 | ||
212380e3 AC |
695 | memset(&me, 0, sizeof(me)); |
696 | memset(&meLocalUser, 0, sizeof(meLocalUser)); | |
697 | me.localClient = &meLocalUser; | |
698 | ||
699 | /* Make sure all lists are zeroed */ | |
700 | memset(&unknown_list, 0, sizeof(unknown_list)); | |
701 | memset(&lclient_list, 0, sizeof(lclient_list)); | |
702 | memset(&serv_list, 0, sizeof(serv_list)); | |
703 | memset(&global_serv_list, 0, sizeof(global_serv_list)); | |
704 | memset(&local_oper_list, 0, sizeof(local_oper_list)); | |
705 | memset(&oper_list, 0, sizeof(oper_list)); | |
706 | ||
330fc5c1 | 707 | rb_dlinkAddTail(&me, &me.node, &global_client_list); |
212380e3 | 708 | |
2af8c7ff VY |
709 | memset(&Count, 0, sizeof(Count)); |
710 | memset(&ServerInfo, 0, sizeof(ServerInfo)); | |
711 | memset(&AdminInfo, 0, sizeof(AdminInfo)); | |
712 | memset(&ServerStats, 0, sizeof(struct ServerStatistics)); | |
212380e3 | 713 | |
212380e3 AC |
714 | if(printVersion) |
715 | { | |
b3803848 | 716 | printf("ircd: version %s(%s)\n", ircd_version, serno); |
8bd1c8a1 AC |
717 | #ifdef CUSTOM_BRANDING |
718 | printf("ircd: based on %s-%s\n", PACKAGE_NAME, PACKAGE_VERSION); | |
719 | #endif | |
d2b6b9f3 | 720 | printf("ircd: %s\n", rb_lib_version()); |
212380e3 AC |
721 | exit(EXIT_SUCCESS); |
722 | } | |
723 | ||
212380e3 AC |
724 | setup_signals(); |
725 | ||
212380e3 | 726 | if (testing_conf) |
503727d1 | 727 | server_state_foreground = true; |
212380e3 | 728 | |
09f528c2 | 729 | #ifndef _WIN32 |
212380e3 AC |
730 | /* Make sure fd 0, 1 and 2 are in use -- jilles */ |
731 | do | |
732 | { | |
733 | fd = open("/dev/null", O_RDWR); | |
734 | } while (fd < 2 && fd != -1); | |
735 | if (fd > 2) | |
736 | close(fd); | |
737 | else if (fd == -1) | |
738 | exit(1); | |
09f528c2 | 739 | #endif |
212380e3 AC |
740 | |
741 | /* Check if there is pidfile and daemon already running */ | |
742 | if(!testing_conf) | |
743 | { | |
744 | check_pidfile(pidFileName); | |
745 | ||
746 | if(!server_state_foreground) | |
747 | make_daemon(); | |
748 | inotice("starting %s ...", ircd_version); | |
31c86ea8 | 749 | inotice("%s", rb_lib_version()); |
212380e3 AC |
750 | } |
751 | ||
752 | /* Init the event subsystem */ | |
0edb29fa | 753 | rb_lib_init(ircd_log_cb, ircd_restart_cb, ircd_die_cb, !server_state_foreground, maxconnections, DNODE_HEAP_SIZE, FD_HEAP_SIZE); |
150f35f7 | 754 | rb_linebuf_init(LINEBUF_HEAP_SIZE); |
212380e3 | 755 | |
e861902f | 756 | rb_init_prng(NULL, RB_PRNG_DEFAULT); |
37cd159e | 757 | |
e1db84d8 VY |
758 | seed_random(NULL); |
759 | ||
346fba92 AC |
760 | init_builtin_capabs(); |
761 | default_server_capabs = CAP_MASK; | |
762 | ||
212380e3 | 763 | init_main_logfile(); |
212380e3 AC |
764 | newconf_init(); |
765 | init_s_conf(); | |
766 | init_s_newconf(); | |
767 | init_hash(); | |
768 | clear_scache_hash_table(); /* server cache name table */ | |
769 | init_host_hash(); | |
770 | clear_hash_parse(); | |
771 | init_client(); | |
212380e3 AC |
772 | init_hook(); |
773 | init_channels(); | |
774 | initclass(); | |
b47f8a4f | 775 | whowas_init(); |
212380e3 AC |
776 | init_reject(); |
777 | init_cache(); | |
778 | init_monitor(); | |
030cdce7 | 779 | |
19716b9f | 780 | construct_cflags_strings(); |
030cdce7 | 781 | |
6d063f05 | 782 | init_authd(); /* Start up authd. */ |
068c6c4a | 783 | init_dns(); /* Start up DNS query system */ |
8a26cd19 | 784 | |
39a68b53 | 785 | privilegeset_set_new("default", "", 0); |
212380e3 AC |
786 | |
787 | if (testing_conf) | |
788 | fprintf(stderr, "\nBeginning config test\n"); | |
ea111ea5 | 789 | read_conf_files(true); /* cold start init conf files */ |
212380e3 | 790 | |
55abcbb2 KB |
791 | mod_add_path(MODULE_DIR); |
792 | mod_add_path(MODULE_DIR "/autoload"); | |
a19097ba EM |
793 | load_all_modules(1); |
794 | load_core_modules(1); | |
212380e3 | 795 | |
b583faf9 AC |
796 | init_isupport(); |
797 | ||
6eebc373 | 798 | init_bandb(); |
c6d72037 | 799 | init_ssld(); |
c53ca1e0 | 800 | init_wsockd(); |
c6d72037 | 801 | |
ab31d2b0 | 802 | rehash_bans(); |
80c9ac51 | 803 | |
212380e3 AC |
804 | initialize_server_capabs(); /* Set up default_server_capabs */ |
805 | initialize_global_set_options(); | |
806 | ||
807 | if(ServerInfo.name == NULL) | |
808 | { | |
809 | ierror("no server name specified in serverinfo block."); | |
810 | return -1; | |
811 | } | |
f427c8b0 | 812 | rb_strlcpy(me.name, ServerInfo.name, sizeof(me.name)); |
212380e3 AC |
813 | |
814 | if(ServerInfo.sid[0] == '\0') | |
815 | { | |
816 | ierror("no server sid specified in serverinfo block."); | |
817 | return -2; | |
818 | } | |
819 | strcpy(me.id, ServerInfo.sid); | |
820 | init_uid(); | |
821 | ||
822 | /* serverinfo{} description must exist. If not, error out. */ | |
823 | if(ServerInfo.description == NULL) | |
824 | { | |
825 | ierror("no server description specified in serverinfo block."); | |
826 | return -3; | |
827 | } | |
f427c8b0 | 828 | rb_strlcpy(me.info, ServerInfo.description, sizeof(me.info)); |
212380e3 | 829 | |
8bd5767b JT |
830 | if(ServerInfo.ssl_cert != NULL && ServerInfo.ssl_private_key != NULL) |
831 | { | |
832 | /* just do the rb_setup_ssl_server to validate the config */ | |
c1725bda | 833 | if(!rb_setup_ssl_server(ServerInfo.ssl_cert, ServerInfo.ssl_private_key, ServerInfo.ssl_dh_params, ServerInfo.ssl_cipher_list)) |
8bd5767b JT |
834 | { |
835 | ilog(L_MAIN, "WARNING: Unable to setup SSL."); | |
bfc44622 | 836 | ircd_ssl_ok = false; |
8bd5767b JT |
837 | } |
838 | else | |
bfc44622 | 839 | ircd_ssl_ok = true; |
c6d72037 VY |
840 | } |
841 | ||
212380e3 AC |
842 | if (testing_conf) |
843 | { | |
844 | fprintf(stderr, "\nConfig testing complete.\n"); | |
845 | fflush(stderr); | |
846 | return 0; /* Why? We want the launcher to exit out. */ | |
847 | } | |
848 | ||
849 | me.from = &me; | |
850 | me.servptr = &me; | |
851 | SetMe(&me); | |
852 | make_server(&me); | |
e3354945 | 853 | startup_time = rb_current_time(); |
212380e3 AC |
854 | add_to_client_hash(me.name, &me); |
855 | add_to_id_hash(me.id, &me); | |
994544c2 | 856 | me.serv->nameinfo = scache_connect(me.name, me.info, 0); |
212380e3 | 857 | |
330fc5c1 | 858 | rb_dlinkAddAlloc(&me, &global_serv_list); |
212380e3 AC |
859 | |
860 | construct_umodebuf(); | |
861 | ||
862 | check_class(); | |
863 | write_pidfile(pidFileName); | |
864 | load_help(); | |
865 | open_logfiles(); | |
866 | ||
50808796 EM |
867 | configure_authd(); |
868 | ||
212380e3 AC |
869 | ilog(L_MAIN, "Server Ready"); |
870 | ||
212380e3 AC |
871 | /* We want try_connections to be called as soon as possible now! -- adrian */ |
872 | /* No, 'cause after a restart it would cause all sorts of nick collides */ | |
873 | /* um. by waiting even longer, that just means we have even *more* | |
874 | * nick collisions. what a stupid idea. set an event for the IO loop --fl | |
875 | */ | |
12aea5fe | 876 | rb_event_addish("try_connections", try_connections, NULL, STARTUP_CONNECTIONS_TIME); |
36645728 VY |
877 | rb_event_addonce("try_connections_startup", try_connections, NULL, 2); |
878 | rb_event_add("check_rehash", check_rehash, NULL, 3); | |
d80645d0 | 879 | rb_event_addish("reseed_srand", seed_random, NULL, 300); /* reseed every 10 minutes */ |
212380e3 | 880 | |
212380e3 | 881 | if(splitmode) |
36645728 | 882 | check_splitmode_ev = rb_event_add("check_splitmode", check_splitmode, NULL, 5); |
212380e3 | 883 | |
212380e3 AC |
884 | print_startup(getpid()); |
885 | ||
39944e1f | 886 | rb_lib_loop(0); |
212380e3 AC |
887 | |
888 | return 0; | |
889 | } |