]> jfr.im git - solanum.git/blame - src/ircd.c
s_stats workaround
[solanum.git] / src / ircd.c
CommitLineData
212380e3
AC
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
AC
25 */
26
27#include "stdinc.h"
28#include "setup.h"
29#include "config.h"
30
212380e3
AC
31#include "ircd.h"
32#include "channel.h"
33#include "class.h"
34#include "client.h"
35#include "common.h"
212380e3
AC
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
AC
48#include "s_conf.h"
49#include "s_log.h"
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
AC
58#include "hook.h"
59#include "ircd_getopt.h"
212380e3 60#include "newconf.h"
212380e3
AC
61#include "reject.h"
62#include "s_conf.h"
63#include "s_newconf.h"
64#include "cache.h"
65#include "monitor.h"
212380e3
AC
66#include "patchlevel.h"
67#include "serno.h"
68
d56734d6
AC
69#include "ratbox_lib.h"
70
212380e3
AC
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
AC
78extern struct LocalUser meLocalUser;
79extern char **myargv;
80
ba4936e2 81int maxconnections; /* XXX */
f888761c 82int ssl_ok = 0;
734d420e 83
b5cf99a8
VY
84struct ServerStatistics ServerStats;
85
212380e3
AC
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
24e9341a
AC
114static void
115ircd_restart_cb(const char *str)
116{
2847d218 117 restart(str);
24e9341a
AC
118}
119
212380e3
AC
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
AC
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
AC
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
AC
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
e3354945
VY
250 if(newtime.tv_sec < rb_current_time())
251 rb_set_back_events(rb_current_time() - newtime.tv_sec);
212380e3
AC
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
734d420e 296 delay = rb_event_next();
e3354945 297 if(delay <= rb_current_time())
734d420e 298 rb_event_run();
212380e3
AC
299
300
b2f0da88 301 rb_select(250);
212380e3
AC
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
AC
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
b2f0da88 383 rb_snprintf(buff, sizeof(buff), "%u\n", pid);
212380e3
AC
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
fa832850
AC
453struct ev_entry *check_splitmode_ev = NULL;
454
212380e3
AC
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
AC
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
330fc5c1 500 rb_dlinkAddTail(&me, &me.node, &global_client_list);
212380e3
AC
501
502 memset((void *) &Count, 0, sizeof(Count));
503 memset((void *) &ServerInfo, 0, sizeof(ServerInfo));
504 memset((void *) &AdminInfo, 0, sizeof(AdminInfo));
b5cf99a8 505 memset(&ServerStats, 0, sizeof(struct ServerStatistics));
212380e3
AC
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 */
212380e3 564 init_sys();
0edb29fa 565 rb_lib_init(ircd_log_cb, ircd_restart_cb, ircd_die_cb, !server_state_foreground, maxconnections, DNODE_HEAP_SIZE, FD_HEAP_SIZE);
212380e3
AC
566
567 init_main_logfile();
212380e3
AC
568 newconf_init();
569 init_s_conf();
570 init_s_newconf();
571 init_hash();
572 clear_scache_hash_table(); /* server cache name table */
573 init_host_hash();
574 clear_hash_parse();
575 init_client();
576 initUser();
577 init_hook();
578 init_channels();
579 initclass();
580 initwhowas();
581 init_stats();
582 init_reject();
583 init_cache();
584 init_monitor();
585 init_isupport();
586 load_all_modules(1);
587#ifndef STATIC_MODULES
588 load_core_modules(1);
589#endif
590 init_auth(); /* Initialise the auth code */
591 init_resolver(); /* Needs to be setup before the io loop */
592
593 if (testing_conf)
594 fprintf(stderr, "\nBeginning config test\n");
595 read_conf_files(YES); /* cold start init conf files */
596 rehash_bans(0);
597#ifndef STATIC_MODULES
598
599 mod_add_path(MODULE_DIR);
600 mod_add_path(MODULE_DIR "/autoload");
601#endif
602
603 initialize_server_capabs(); /* Set up default_server_capabs */
604 initialize_global_set_options();
605
606 if(ServerInfo.name == NULL)
607 {
608 ierror("no server name specified in serverinfo block.");
609 return -1;
610 }
611 strlcpy(me.name, ServerInfo.name, sizeof(me.name));
612
613 if(ServerInfo.sid[0] == '\0')
614 {
615 ierror("no server sid specified in serverinfo block.");
616 return -2;
617 }
618 strcpy(me.id, ServerInfo.sid);
619 init_uid();
620
621 /* serverinfo{} description must exist. If not, error out. */
622 if(ServerInfo.description == NULL)
623 {
624 ierror("no server description specified in serverinfo block.");
625 return -3;
626 }
627 strlcpy(me.info, ServerInfo.description, sizeof(me.info));
628
629 if (testing_conf)
630 {
631 fprintf(stderr, "\nConfig testing complete.\n");
632 fflush(stderr);
633 return 0; /* Why? We want the launcher to exit out. */
634 }
635
636 me.from = &me;
637 me.servptr = &me;
638 SetMe(&me);
639 make_server(&me);
e3354945 640 startup_time = rb_current_time();
212380e3
AC
641 add_to_client_hash(me.name, &me);
642 add_to_id_hash(me.id, &me);
994544c2 643 me.serv->nameinfo = scache_connect(me.name, me.info, 0);
212380e3 644
330fc5c1 645 rb_dlinkAddAlloc(&me, &global_serv_list);
212380e3
AC
646
647 construct_umodebuf();
648
649 check_class();
650 write_pidfile(pidFileName);
651 load_help();
652 open_logfiles();
653
654 ilog(L_MAIN, "Server Ready");
655
12aea5fe 656 rb_event_addish("cleanup_glines", cleanup_glines, NULL, CLEANUP_GLINES_TIME);
212380e3
AC
657
658 /* We want try_connections to be called as soon as possible now! -- adrian */
659 /* No, 'cause after a restart it would cause all sorts of nick collides */
660 /* um. by waiting even longer, that just means we have even *more*
661 * nick collisions. what a stupid idea. set an event for the IO loop --fl
662 */
12aea5fe
VY
663 rb_event_addish("try_connections", try_connections, NULL, STARTUP_CONNECTIONS_TIME);
664 rb_event_addonce("try_connections_startup", try_connections, NULL, 0);
212380e3 665
12aea5fe 666 rb_event_addish("collect_zipstats", collect_zipstats, NULL, ZIPSTATS_TIME);
212380e3
AC
667
668 /* Setup the timeout check. I'll shift it later :) -- adrian */
12aea5fe 669 rb_event_addish("rb_checktimeouts", rb_checktimeouts, NULL, 1);
212380e3 670
12aea5fe 671 rb_event_add("check_rehash", check_rehash, NULL, 1);
212380e3 672
212380e3 673 if(splitmode)
434028d0 674 check_splitmode_ev = rb_event_add("check_splitmode", check_splitmode, NULL, 2);
212380e3
AC
675
676 ServerRunning = 1;
677
678 print_startup(getpid());
679
680 charybdis_io_loop();
681
682 return 0;
683}