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