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