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