]> jfr.im git - solanum.git/blob - src/ircd.c
80b18b3080e60664a76cff9a29e30c00efbf6bf6
[solanum.git] / src / ircd.c
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 *
24 * $Id: ircd.c 3380 2007-04-03 22:25:11Z jilles $
25 */
26
27 #include "stdinc.h"
28 #include "setup.h"
29 #include "config.h"
30
31 #include "ircd.h"
32 #include "channel.h"
33 #include "class.h"
34 #include "client.h"
35 #include "common.h"
36 #include "hash.h"
37 #include "irc_string.h"
38 #include "ircd_signal.h"
39 #include "sprintf_irc.h"
40 #include "s_gline.h"
41 #include "msg.h" /* msgtab */
42 #include "hostmask.h"
43 #include "numeric.h"
44 #include "parse.h"
45 #include "res.h"
46 #include "restart.h"
47 #include "s_auth.h"
48 #include "s_conf.h"
49 #include "logger.h"
50 #include "s_serv.h" /* try_connections */
51 #include "s_user.h"
52 #include "s_stats.h"
53 #include "scache.h"
54 #include "send.h"
55 #include "supported.h"
56 #include "whowas.h"
57 #include "modules.h"
58 #include "hook.h"
59 #include "ircd_getopt.h"
60 #include "newconf.h"
61 #include "reject.h"
62 #include "s_conf.h"
63 #include "s_newconf.h"
64 #include "cache.h"
65 #include "monitor.h"
66 #include "patchlevel.h"
67 #include "serno.h"
68
69 #include "ratbox_lib.h"
70
71 /*
72 * Try and find the correct name to use with getrlimit() for setting the max.
73 * number of files allowed to be open by this process.
74 */
75 int _charybdis_data_version = CHARYBDIS_DV;
76
77 extern int ServerRunning;
78 extern struct LocalUser meLocalUser;
79 extern char **myargv;
80
81 int maxconnections; /* XXX */
82
83 struct ServerStatistics ServerStats;
84
85 /*
86 * print_startup - print startup information
87 */
88 static void
89 print_startup(int pid)
90 {
91 inotice("now running in %s mode from %s as pid %d ...",
92 !server_state_foreground ? "background" : "foreground",
93 ConfigFileEntry.dpath, pid);
94
95 /* let the parent process know the initialization was successful
96 * -- jilles */
97 if (!server_state_foreground)
98 write(0, ".", 1);
99 fclose(stdin);
100 fclose(stdout);
101 fclose(stderr);
102 open("/dev/null", O_RDWR);
103 dup2(0, 1);
104 dup2(0, 2);
105 }
106
107 static void
108 ircd_log_cb(const char *str)
109 {
110 ilog(L_MAIN, "%s", str);
111 }
112
113 static void
114 ircd_restart_cb(const char *str)
115 {
116 restart(str);
117 }
118
119 /*
120 * Why EXIT_FAILURE here?
121 * Because if ircd_die_cb() is called it's because of a fatal
122 * error inside libcharybdis, and we don't know how to handle the
123 * exception, so it is logical to return a FAILURE exit code here.
124 * --nenolod
125 */
126 static void
127 ircd_die_cb(const char *str)
128 {
129 /* Try to get the message out to currently logged in operators. */
130 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, "Server panic! %s", str);
131 inotice("server panic: %s", str);
132
133 unlink(pidFileName);
134 exit(EXIT_FAILURE);
135 }
136
137 /*
138 * init_sys
139 *
140 * inputs - boot_daemon flag
141 * output - none
142 * side effects - if boot_daemon flag is not set, don't daemonize
143 */
144 static void
145 init_sys(void)
146 {
147 #if defined(RLIMIT_NOFILE) && defined(HAVE_SYS_RESOURCE_H)
148 struct rlimit limit;
149
150 if(!getrlimit(RLIMIT_NOFILE, &limit))
151 {
152 limit.rlim_cur = limit.rlim_max; /* make soft limit the max */
153 if(setrlimit(RLIMIT_NOFILE, &limit) == -1)
154 {
155 fprintf(stderr, "error setting max fd's to %ld\n", (long) limit.rlim_cur);
156 exit(EXIT_FAILURE);
157 }
158 }
159
160 maxconnections = limit.rlim_cur;
161 #endif /* RLIMIT_NOFILE */
162 }
163
164 static int
165 make_daemon(void)
166 {
167 int pid;
168 int pip[2];
169 char c;
170
171 if (pipe(pip) < 0)
172 {
173 perror("pipe");
174 exit(EXIT_FAILURE);
175 }
176 dup2(pip[1], 0);
177 close(pip[1]);
178 if((pid = fork()) < 0)
179 {
180 perror("fork");
181 exit(EXIT_FAILURE);
182 }
183 else if(pid > 0)
184 {
185 close(0);
186 /* Wait for initialization to finish, successfully or
187 * unsuccessfully. Until this point the child may still
188 * write to stdout/stderr.
189 * -- jilles */
190 if (read(pip[0], &c, 1) > 0)
191 exit(EXIT_SUCCESS);
192 else
193 exit(EXIT_FAILURE);
194 }
195
196 close(pip[0]);
197 setsid();
198 /* fclose(stdin);
199 fclose(stdout);
200 fclose(stderr); */
201
202 return 0;
203 }
204
205 static int printVersion = 0;
206
207 struct lgetopt myopts[] = {
208 {"dlinefile", &ConfigFileEntry.dlinefile,
209 STRING, "File to use for dlines.conf"},
210 {"configfile", &ConfigFileEntry.configfile,
211 STRING, "File to use for ircd.conf"},
212 {"klinefile", &ConfigFileEntry.klinefile,
213 STRING, "File to use for kline.conf"},
214 {"xlinefile", &ConfigFileEntry.xlinefile,
215 STRING, "File to use for xline.conf"},
216 {"resvfile", &ConfigFileEntry.resvfile,
217 STRING, "File to use for resv.conf"},
218 {"logfile", &logFileName,
219 STRING, "File to use for ircd.log"},
220 {"pidfile", &pidFileName,
221 STRING, "File to use for process ID"},
222 {"foreground", &server_state_foreground,
223 YESNO, "Run in foreground (don't detach)"},
224 {"version", &printVersion,
225 YESNO, "Print version and exit"},
226 {"conftest", &testing_conf,
227 YESNO, "Test the configuration files and exit"},
228 {"help", NULL, USAGE, "Print this text"},
229 {NULL, NULL, STRING, NULL},
230 };
231
232 static void
233 check_rehash(void *unused)
234 {
235 /*
236 * Check to see whether we have to rehash the configuration ..
237 */
238 if(dorehash)
239 {
240 rehash(1);
241 dorehash = 0;
242 }
243
244 if(dorehashbans)
245 {
246 rehash_bans(1);
247 dorehashbans = 0;
248 }
249
250 if(doremotd)
251 {
252 sendto_realops_snomask(SNO_GENERAL, L_ALL,
253 "Got signal SIGUSR1, reloading ircd motd file");
254 free_cachefile(user_motd);
255 user_motd = cache_file(MPATH, "ircd.motd", 0);
256 doremotd = 0;
257 }
258 }
259
260 /*
261 * initalialize_global_set_options
262 *
263 * inputs - none
264 * output - none
265 * side effects - This sets all global set options needed
266 */
267 static void
268 initialize_global_set_options(void)
269 {
270 memset(&GlobalSetOptions, 0, sizeof(GlobalSetOptions));
271 /* memset( &ConfigFileEntry, 0, sizeof(ConfigFileEntry)); */
272
273 GlobalSetOptions.maxclients = ServerInfo.max_clients;
274 GlobalSetOptions.autoconn = 1;
275
276 GlobalSetOptions.spam_time = MIN_JOIN_LEAVE_TIME;
277 GlobalSetOptions.spam_num = MAX_JOIN_LEAVE_COUNT;
278
279 if(ConfigFileEntry.default_floodcount)
280 GlobalSetOptions.floodcount = ConfigFileEntry.default_floodcount;
281 else
282 GlobalSetOptions.floodcount = 10;
283
284 split_servers = ConfigChannel.default_split_server_count;
285 split_users = ConfigChannel.default_split_user_count;
286
287 if(split_users && split_servers
288 && (ConfigChannel.no_create_on_split || ConfigChannel.no_join_on_split))
289 {
290 splitmode = 1;
291 splitchecking = 1;
292 }
293
294 GlobalSetOptions.ident_timeout = IDENT_TIMEOUT;
295
296 strlcpy(GlobalSetOptions.operstring,
297 ConfigFileEntry.default_operstring,
298 sizeof(GlobalSetOptions.operstring));
299 strlcpy(GlobalSetOptions.adminstring,
300 ConfigFileEntry.default_adminstring,
301 sizeof(GlobalSetOptions.adminstring));
302
303 /* memset( &ConfigChannel, 0, sizeof(ConfigChannel)); */
304
305 /* End of global set options */
306
307 }
308
309 /*
310 * initialize_server_capabs
311 *
312 * inputs - none
313 * output - none
314 */
315 static void
316 initialize_server_capabs(void)
317 {
318 default_server_capabs &= ~CAP_ZIP;
319 }
320
321
322 /*
323 * write_pidfile
324 *
325 * inputs - filename+path of pid file
326 * output - none
327 * side effects - write the pid of the ircd to filename
328 */
329 static void
330 write_pidfile(const char *filename)
331 {
332 FILE *fb;
333 char buff[32];
334 if((fb = fopen(filename, "w")))
335 {
336 unsigned int pid = (unsigned int) getpid();
337
338 rb_snprintf(buff, sizeof(buff), "%u\n", pid);
339 if((fputs(buff, fb) == -1))
340 {
341 ilog(L_MAIN, "Error writing %u to pid file %s (%s)",
342 pid, filename, strerror(errno));
343 }
344 fclose(fb);
345 return;
346 }
347 else
348 {
349 ilog(L_MAIN, "Error opening pid file %s", filename);
350 }
351 }
352
353 /*
354 * check_pidfile
355 *
356 * inputs - filename+path of pid file
357 * output - none
358 * side effects - reads pid from pidfile and checks if ircd is in process
359 * list. if it is, gracefully exits
360 * -kre
361 */
362 static void
363 check_pidfile(const char *filename)
364 {
365 FILE *fb;
366 char buff[32];
367 pid_t pidfromfile;
368
369 /* Don't do logging here, since we don't have log() initialised */
370 if((fb = fopen(filename, "r")))
371 {
372 if(fgets(buff, 20, fb) != NULL)
373 {
374 pidfromfile = atoi(buff);
375 if(!kill(pidfromfile, 0))
376 {
377 printf("ircd: daemon is already running\n");
378 exit(-1);
379 }
380 }
381 fclose(fb);
382 }
383 }
384
385 /*
386 * setup_corefile
387 *
388 * inputs - nothing
389 * output - nothing
390 * side effects - setups corefile to system limits.
391 * -kre
392 */
393 static void
394 setup_corefile(void)
395 {
396 #ifdef HAVE_SYS_RESOURCE_H
397 struct rlimit rlim; /* resource limits */
398
399 /* Set corefilesize to maximum */
400 if(!getrlimit(RLIMIT_CORE, &rlim))
401 {
402 rlim.rlim_cur = rlim.rlim_max;
403 setrlimit(RLIMIT_CORE, &rlim);
404 }
405 #endif
406 }
407
408 struct ev_entry *check_splitmode_ev = NULL;
409
410 /*
411 * main
412 *
413 * Initializes the IRCd.
414 *
415 * Inputs - number of commandline args, args themselves
416 * Outputs - none
417 * Side Effects - this is where the ircd gets going right now
418 */
419 int
420 main(int argc, char *argv[])
421 {
422 int fd;
423
424 /* Check to see if the user is running us as root, which is a nono */
425 if(geteuid() == 0)
426 {
427 fprintf(stderr, "Don't run ircd as root!!!\n");
428 return -1;
429 }
430
431 /*
432 * Setup corefile size immediately after boot -kre
433 */
434 setup_corefile();
435
436 ServerRunning = 0;
437 /* It ain't random, but it ought to be a little harder to guess */
438 srand(SystemTime.tv_sec ^ (SystemTime.tv_usec | (getpid() << 20)));
439 memset(&me, 0, sizeof(me));
440 memset(&meLocalUser, 0, sizeof(meLocalUser));
441 me.localClient = &meLocalUser;
442
443 /* Make sure all lists are zeroed */
444 memset(&unknown_list, 0, sizeof(unknown_list));
445 memset(&lclient_list, 0, sizeof(lclient_list));
446 memset(&serv_list, 0, sizeof(serv_list));
447 memset(&global_serv_list, 0, sizeof(global_serv_list));
448 memset(&local_oper_list, 0, sizeof(local_oper_list));
449 memset(&oper_list, 0, sizeof(oper_list));
450
451 rb_dlinkAddTail(&me, &me.node, &global_client_list);
452
453 memset((void *) &Count, 0, sizeof(Count));
454 memset((void *) &ServerInfo, 0, sizeof(ServerInfo));
455 memset((void *) &AdminInfo, 0, sizeof(AdminInfo));
456 memset((void *) &ServerStats, 0, sizeof(struct ServerStatistics));
457
458 /* Initialise the channel capability usage counts... */
459 init_chcap_usage_counts();
460
461 ConfigFileEntry.dpath = DPATH;
462 ConfigFileEntry.configfile = CPATH; /* Server configuration file */
463 ConfigFileEntry.klinefile = KPATH; /* Server kline file */
464 ConfigFileEntry.dlinefile = DLPATH; /* dline file */
465 ConfigFileEntry.xlinefile = XPATH;
466 ConfigFileEntry.resvfile = RESVPATH;
467 ConfigFileEntry.connect_timeout = 30; /* Default to 30 */
468 myargv = argv;
469 umask(077); /* better safe than sorry --SRB */
470
471 parseargs(&argc, &argv, myopts);
472
473 if(printVersion)
474 {
475 printf("ircd: version %s\n", ircd_version);
476 exit(EXIT_SUCCESS);
477 }
478
479 if(chdir(ConfigFileEntry.dpath))
480 {
481 fprintf(stderr, "Unable to chdir to %s: %s\n", ConfigFileEntry.dpath, strerror(errno));
482 exit(EXIT_FAILURE);
483 }
484
485 setup_signals();
486
487 #ifdef __CYGWIN__
488 server_state_foreground = 1;
489 #endif
490
491 if (testing_conf)
492 server_state_foreground = 1;
493
494 /* Make sure fd 0, 1 and 2 are in use -- jilles */
495 do
496 {
497 fd = open("/dev/null", O_RDWR);
498 } while (fd < 2 && fd != -1);
499 if (fd > 2)
500 close(fd);
501 else if (fd == -1)
502 exit(1);
503
504 /* Check if there is pidfile and daemon already running */
505 if(!testing_conf)
506 {
507 check_pidfile(pidFileName);
508
509 if(!server_state_foreground)
510 make_daemon();
511 inotice("starting %s ...", ircd_version);
512 }
513
514 /* Init the event subsystem */
515 init_sys();
516 rb_lib_init(ircd_log_cb, ircd_restart_cb, ircd_die_cb, !server_state_foreground, maxconnections, DNODE_HEAP_SIZE, FD_HEAP_SIZE);
517 rb_linebuf_init(LINEBUF_HEAP_SIZE);
518
519 init_main_logfile();
520 newconf_init();
521 init_s_conf();
522 init_s_newconf();
523 init_hash();
524 clear_scache_hash_table(); /* server cache name table */
525 init_host_hash();
526 clear_hash_parse();
527 init_client();
528 initUser();
529 init_hook();
530 init_channels();
531 initclass();
532 initwhowas();
533 init_reject();
534 init_cache();
535 init_monitor();
536 init_isupport();
537 load_all_modules(1);
538 #ifndef STATIC_MODULES
539 load_core_modules(1);
540 #endif
541 init_auth(); /* Initialise the auth code */
542 init_resolver(); /* Needs to be setup before the io loop */
543
544 if (testing_conf)
545 fprintf(stderr, "\nBeginning config test\n");
546 read_conf_files(YES); /* cold start init conf files */
547 rehash_bans(0);
548 #ifndef STATIC_MODULES
549
550 mod_add_path(MODULE_DIR);
551 mod_add_path(MODULE_DIR "/autoload");
552 #endif
553
554 initialize_server_capabs(); /* Set up default_server_capabs */
555 initialize_global_set_options();
556
557 if(ServerInfo.name == NULL)
558 {
559 ierror("no server name specified in serverinfo block.");
560 return -1;
561 }
562 strlcpy(me.name, ServerInfo.name, sizeof(me.name));
563
564 if(ServerInfo.sid[0] == '\0')
565 {
566 ierror("no server sid specified in serverinfo block.");
567 return -2;
568 }
569 strcpy(me.id, ServerInfo.sid);
570 init_uid();
571
572 /* serverinfo{} description must exist. If not, error out. */
573 if(ServerInfo.description == NULL)
574 {
575 ierror("no server description specified in serverinfo block.");
576 return -3;
577 }
578 strlcpy(me.info, ServerInfo.description, sizeof(me.info));
579
580 if (testing_conf)
581 {
582 fprintf(stderr, "\nConfig testing complete.\n");
583 fflush(stderr);
584 return 0; /* Why? We want the launcher to exit out. */
585 }
586
587 me.from = &me;
588 me.servptr = &me;
589 SetMe(&me);
590 make_server(&me);
591 startup_time = rb_current_time();
592 add_to_client_hash(me.name, &me);
593 add_to_id_hash(me.id, &me);
594 me.serv->nameinfo = scache_connect(me.name, me.info, 0);
595
596 rb_dlinkAddAlloc(&me, &global_serv_list);
597
598 construct_umodebuf();
599
600 check_class();
601 write_pidfile(pidFileName);
602 load_help();
603 open_logfiles();
604
605 ilog(L_MAIN, "Server Ready");
606
607 rb_event_addish("cleanup_glines", cleanup_glines, NULL, CLEANUP_GLINES_TIME);
608
609 /* We want try_connections to be called as soon as possible now! -- adrian */
610 /* No, 'cause after a restart it would cause all sorts of nick collides */
611 /* um. by waiting even longer, that just means we have even *more*
612 * nick collisions. what a stupid idea. set an event for the IO loop --fl
613 */
614 rb_event_addish("try_connections", try_connections, NULL, STARTUP_CONNECTIONS_TIME);
615 rb_event_addonce("try_connections_startup", try_connections, NULL, 0);
616
617 rb_event_addish("collect_zipstats", collect_zipstats, NULL, ZIPSTATS_TIME);
618
619 /* Setup the timeout check. I'll shift it later :) -- adrian */
620 rb_event_addish("rb_checktimeouts", rb_checktimeouts, NULL, 1);
621
622 rb_event_add("check_rehash", check_rehash, NULL, 1);
623
624 if(splitmode)
625 check_splitmode_ev = rb_event_add("check_splitmode", check_splitmode, NULL, 2);
626
627 ServerRunning = 1;
628
629 print_startup(getpid());
630
631 rb_lib_loop(250);
632
633 return 0;
634 }