]> jfr.im git - irc/rqf/shadowircd.git/blame - src/ircd.c
[svn] - add config option for setting max_clients.
[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 *
c2d96fcb 24 * $Id: ircd.c 3251 2007-03-05 18:58:38Z nenolod $
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
114/*
115 * Why EXIT_FAILURE here?
116 * Because if ircd_die_cb() is called it's because of a fatal
117 * error inside libcharybdis, and we don't know how to handle the
118 * exception, so it is logical to return a FAILURE exit code here.
119 * --nenolod
120 */
121static void
122ircd_die_cb(const char *str)
123{
124 /* Try to get the message out to currently logged in operators. */
125 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, "Server panic! %s", str);
126 inotice("server panic: %s", str);
127
128 unlink(pidFileName);
129 exit(EXIT_FAILURE);
130}
131
132/*
133 * init_sys
134 *
135 * inputs - boot_daemon flag
136 * output - none
137 * side effects - if boot_daemon flag is not set, don't daemonize
138 */
139static void
140init_sys(void)
141{
142#if defined(RLIMIT_FD_MAX) && defined(HAVE_SYS_RLIMIT_H)
143 struct rlimit limit;
144
145 if(!getrlimit(RLIMIT_FD_MAX, &limit))
146 {
212380e3 147 if(limit.rlim_max < MAXCONNECTIONS)
148 {
68ff929f 149 fprintf(stderr, "ircd's bootstrap fd table is too big\n");
150 fprintf(stderr, "Hard Limit: %ld bootstrap size: %d\n",
212380e3 151 (long) limit.rlim_max, MAXCONNECTIONS);
152 fprintf(stderr, "Fix MAXCONNECTIONS\n");
153 exit(-1);
154 }
155
156 limit.rlim_cur = limit.rlim_max; /* make soft limit the max */
157 if(setrlimit(RLIMIT_FD_MAX, &limit) == -1)
158 {
159 fprintf(stderr, "error setting max fd's to %ld\n", (long) limit.rlim_cur);
160 exit(EXIT_FAILURE);
161 }
162 }
163#endif /* RLIMIT_FD_MAX */
164}
165
166static int
167make_daemon(void)
168{
169 int pid;
170 int pip[2];
171 char c;
172
173 if (pipe(pip) < 0)
174 {
175 perror("pipe");
176 exit(EXIT_FAILURE);
177 }
178 dup2(pip[1], 0);
179 close(pip[1]);
180 if((pid = fork()) < 0)
181 {
182 perror("fork");
183 exit(EXIT_FAILURE);
184 }
185 else if(pid > 0)
186 {
187 close(0);
188 /* Wait for initialization to finish, successfully or
189 * unsuccessfully. Until this point the child may still
190 * write to stdout/stderr.
191 * -- jilles */
192 if (read(pip[0], &c, 1) > 0)
193 exit(EXIT_SUCCESS);
194 else
195 exit(EXIT_FAILURE);
196 }
197
198 close(pip[0]);
199 setsid();
200/* fclose(stdin);
201 fclose(stdout);
202 fclose(stderr); */
203
204 return 0;
205}
206
207static int printVersion = 0;
208
209struct lgetopt myopts[] = {
210 {"dlinefile", &ConfigFileEntry.dlinefile,
211 STRING, "File to use for dlines.conf"},
212 {"configfile", &ConfigFileEntry.configfile,
213 STRING, "File to use for ircd.conf"},
214 {"klinefile", &ConfigFileEntry.klinefile,
215 STRING, "File to use for kline.conf"},
216 {"xlinefile", &ConfigFileEntry.xlinefile,
217 STRING, "File to use for xline.conf"},
218 {"resvfile", &ConfigFileEntry.resvfile,
219 STRING, "File to use for resv.conf"},
220 {"logfile", &logFileName,
221 STRING, "File to use for ircd.log"},
222 {"pidfile", &pidFileName,
223 STRING, "File to use for process ID"},
224 {"foreground", &server_state_foreground,
225 YESNO, "Run in foreground (don't detach)"},
226 {"version", &printVersion,
227 YESNO, "Print version and exit"},
228 {"conftest", &testing_conf,
229 YESNO, "Test the configuration files and exit"},
230 {"help", NULL, USAGE, "Print this text"},
231 {NULL, NULL, STRING, NULL},
232};
233
234void
235set_time(void)
236{
237 struct timeval newtime;
238 newtime.tv_sec = 0;
239 newtime.tv_usec = 0;
240#ifdef HAVE_GETTIMEOFDAY
241 if(gettimeofday(&newtime, NULL) == -1)
242 {
243 ilog(L_MAIN, "Clock Failure (%d)", errno);
244 sendto_realops_snomask(SNO_GENERAL, L_ALL,
245 "Clock Failure (%d), TS can be corrupted", errno);
246
247 restart("Clock Failure");
248 }
249#else
250 newtime.tv_sec = time(NULL);
251
252#endif
253 if(newtime.tv_sec < CurrentTime)
254 set_back_events(CurrentTime - newtime.tv_sec);
255
256 SystemTime.tv_sec = newtime.tv_sec;
257 SystemTime.tv_usec = newtime.tv_usec;
258}
259
260static void
261check_rehash(void *unused)
262{
263 /*
264 * Check to see whether we have to rehash the configuration ..
265 */
266 if(dorehash)
267 {
268 rehash(1);
269 dorehash = 0;
270 }
271
272 if(dorehashbans)
273 {
274 rehash_bans(1);
275 dorehashbans = 0;
276 }
277
278 if(doremotd)
279 {
280 sendto_realops_snomask(SNO_GENERAL, L_ALL,
281 "Got signal SIGUSR1, reloading ircd motd file");
282 free_cachefile(user_motd);
283 user_motd = cache_file(MPATH, "ircd.motd", 0);
284 doremotd = 0;
285 }
286}
287
288void
289charybdis_io_loop(void)
290{
291 time_t delay;
292
293 while (ServerRunning)
294 {
295 /* Run pending events, then get the number of seconds to the next
296 * event
297 */
298
299 delay = eventNextTime();
300 if(delay <= CurrentTime)
301 eventRun();
302
303
304 comm_select(250);
305 }
306}
307
308/*
309 * initalialize_global_set_options
310 *
311 * inputs - none
312 * output - none
313 * side effects - This sets all global set options needed
314 */
315static void
316initialize_global_set_options(void)
317{
318 memset(&GlobalSetOptions, 0, sizeof(GlobalSetOptions));
319 /* memset( &ConfigFileEntry, 0, sizeof(ConfigFileEntry)); */
320
c2d96fcb 321 GlobalSetOptions.maxclients = ServerInfo.max_clients;
212380e3 322 GlobalSetOptions.autoconn = 1;
323
324 GlobalSetOptions.spam_time = MIN_JOIN_LEAVE_TIME;
325 GlobalSetOptions.spam_num = MAX_JOIN_LEAVE_COUNT;
326
327 if(ConfigFileEntry.default_floodcount)
328 GlobalSetOptions.floodcount = ConfigFileEntry.default_floodcount;
329 else
330 GlobalSetOptions.floodcount = 10;
331
332 split_servers = ConfigChannel.default_split_server_count;
333 split_users = ConfigChannel.default_split_user_count;
334
335 if(split_users && split_servers
336 && (ConfigChannel.no_create_on_split || ConfigChannel.no_join_on_split))
337 {
338 splitmode = 1;
339 splitchecking = 1;
340 }
341
342 GlobalSetOptions.ident_timeout = IDENT_TIMEOUT;
343
344 strlcpy(GlobalSetOptions.operstring,
345 ConfigFileEntry.default_operstring,
346 sizeof(GlobalSetOptions.operstring));
347 strlcpy(GlobalSetOptions.adminstring,
348 ConfigFileEntry.default_adminstring,
349 sizeof(GlobalSetOptions.adminstring));
350
351 /* memset( &ConfigChannel, 0, sizeof(ConfigChannel)); */
352
353 /* End of global set options */
354
355}
356
357/*
358 * initialize_server_capabs
359 *
360 * inputs - none
361 * output - none
362 */
363static void
364initialize_server_capabs(void)
365{
366 default_server_capabs &= ~CAP_ZIP;
367}
368
369
370/*
371 * write_pidfile
372 *
373 * inputs - filename+path of pid file
374 * output - none
375 * side effects - write the pid of the ircd to filename
376 */
377static void
378write_pidfile(const char *filename)
379{
380 FILE *fb;
381 char buff[32];
382 if((fb = fopen(filename, "w")))
383 {
384 unsigned int pid = (unsigned int) getpid();
385
386 ircsnprintf(buff, sizeof(buff), "%u\n", pid);
387 if((fputs(buff, fb) == -1))
388 {
389 ilog(L_MAIN, "Error writing %u to pid file %s (%s)",
390 pid, filename, strerror(errno));
391 }
392 fclose(fb);
393 return;
394 }
395 else
396 {
397 ilog(L_MAIN, "Error opening pid file %s", filename);
398 }
399}
400
401/*
402 * check_pidfile
403 *
404 * inputs - filename+path of pid file
405 * output - none
406 * side effects - reads pid from pidfile and checks if ircd is in process
407 * list. if it is, gracefully exits
408 * -kre
409 */
410static void
411check_pidfile(const char *filename)
412{
413 FILE *fb;
414 char buff[32];
415 pid_t pidfromfile;
416
417 /* Don't do logging here, since we don't have log() initialised */
418 if((fb = fopen(filename, "r")))
419 {
420 if(fgets(buff, 20, fb) != NULL)
421 {
422 pidfromfile = atoi(buff);
423 if(!kill(pidfromfile, 0))
424 {
425 printf("ircd: daemon is already running\n");
426 exit(-1);
427 }
428 }
429 fclose(fb);
430 }
431}
432
433/*
434 * setup_corefile
435 *
436 * inputs - nothing
437 * output - nothing
438 * side effects - setups corefile to system limits.
439 * -kre
440 */
441static void
442setup_corefile(void)
443{
444#ifdef HAVE_SYS_RESOURCE_H
445 struct rlimit rlim; /* resource limits */
446
447 /* Set corefilesize to maximum */
448 if(!getrlimit(RLIMIT_CORE, &rlim))
449 {
450 rlim.rlim_cur = rlim.rlim_max;
451 setrlimit(RLIMIT_CORE, &rlim);
452 }
453#endif
454}
455
456/*
457 * main
458 *
459 * Initializes the IRCd.
460 *
461 * Inputs - number of commandline args, args themselves
462 * Outputs - none
463 * Side Effects - this is where the ircd gets going right now
464 */
465int
466main(int argc, char *argv[])
467{
468 int fd;
469
470 /* Check to see if the user is running us as root, which is a nono */
471 if(geteuid() == 0)
472 {
473 fprintf(stderr, "Don't run ircd as root!!!\n");
474 return -1;
475 }
476
477 /*
478 * save server boot time right away, so getrusage works correctly
479 */
480 set_time();
481 /*
482 * Setup corefile size immediately after boot -kre
483 */
484 setup_corefile();
485
212380e3 486 ServerRunning = 0;
487 /* It ain't random, but it ought to be a little harder to guess */
488 srand(SystemTime.tv_sec ^ (SystemTime.tv_usec | (getpid() << 20)));
489 memset(&me, 0, sizeof(me));
490 memset(&meLocalUser, 0, sizeof(meLocalUser));
491 me.localClient = &meLocalUser;
492
493 /* Make sure all lists are zeroed */
494 memset(&unknown_list, 0, sizeof(unknown_list));
495 memset(&lclient_list, 0, sizeof(lclient_list));
496 memset(&serv_list, 0, sizeof(serv_list));
497 memset(&global_serv_list, 0, sizeof(global_serv_list));
498 memset(&local_oper_list, 0, sizeof(local_oper_list));
499 memset(&oper_list, 0, sizeof(oper_list));
500
501 dlinkAddTail(&me, &me.node, &global_client_list);
502
503 memset((void *) &Count, 0, sizeof(Count));
504 memset((void *) &ServerInfo, 0, sizeof(ServerInfo));
505 memset((void *) &AdminInfo, 0, sizeof(AdminInfo));
506
507 /* Initialise the channel capability usage counts... */
508 init_chcap_usage_counts();
509
510 ConfigFileEntry.dpath = DPATH;
511 ConfigFileEntry.configfile = CPATH; /* Server configuration file */
512 ConfigFileEntry.klinefile = KPATH; /* Server kline file */
513 ConfigFileEntry.dlinefile = DLPATH; /* dline file */
514 ConfigFileEntry.xlinefile = XPATH;
515 ConfigFileEntry.resvfile = RESVPATH;
516 ConfigFileEntry.connect_timeout = 30; /* Default to 30 */
517 myargv = argv;
518 umask(077); /* better safe than sorry --SRB */
519
520 parseargs(&argc, &argv, myopts);
521
522 if(printVersion)
523 {
524 printf("ircd: version %s\n", ircd_version);
525 exit(EXIT_SUCCESS);
526 }
527
528 if(chdir(ConfigFileEntry.dpath))
529 {
530 fprintf(stderr, "Unable to chdir to %s: %s\n", ConfigFileEntry.dpath, strerror(errno));
531 exit(EXIT_FAILURE);
532 }
533
534 setup_signals();
535
536#ifdef __CYGWIN__
537 server_state_foreground = 1;
538#endif
539
540 if (testing_conf)
541 server_state_foreground = 1;
542
543 /* Make sure fd 0, 1 and 2 are in use -- jilles */
544 do
545 {
546 fd = open("/dev/null", O_RDWR);
547 } while (fd < 2 && fd != -1);
548 if (fd > 2)
549 close(fd);
550 else if (fd == -1)
551 exit(1);
552
553 /* Check if there is pidfile and daemon already running */
554 if(!testing_conf)
555 {
556 check_pidfile(pidFileName);
557
558 if(!server_state_foreground)
559 make_daemon();
560 inotice("starting %s ...", ircd_version);
561 }
562
563 /* Init the event subsystem */
564 libcharybdis_init(ircd_log_cb, restart, ircd_die_cb);
565 init_sys();
566
567 fdlist_init();
568 if(!server_state_foreground)
569 {
570 comm_close_all();
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);
647 me.serv->up = me.name;
648 startup_time = CurrentTime;
649 add_to_client_hash(me.name, &me);
650 add_to_id_hash(me.id, &me);
651
652 dlinkAddAlloc(&me, &global_serv_list);
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 */
676 eventAddIsh("comm_checktimeouts", comm_checktimeouts, NULL, 1);
677
678 eventAdd("check_rehash", check_rehash, NULL, 1);
679
680 if(ConfigServerHide.links_delay > 0)
681 eventAdd("cache_links", cache_links, NULL,
682 ConfigServerHide.links_delay);
683 else
684 ConfigServerHide.links_disabled = 1;
685
686 if(splitmode)
687 eventAdd("check_splitmode", check_splitmode, NULL, 2);
688
689 ServerRunning = 1;
690
691 print_startup(getpid());
692
693 charybdis_io_loop();
694
695 return 0;
696}