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