]> jfr.im git - solanum.git/blob - src/ircd.c
rb_event_add ;)
[solanum.git] / src / ircd.c
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 *
24 * $Id: ircd.c 3380 2007-04-03 22:25:11Z jilles $
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 #include "ratbox_lib.h"
77
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 */
82 int _charybdis_data_version = CHARYBDIS_DV;
83
84 extern int ServerRunning;
85 extern struct LocalUser meLocalUser;
86 extern char **myargv;
87
88 /*
89 * print_startup - print startup information
90 */
91 static void
92 print_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
110 static void
111 ircd_log_cb(const char *str)
112 {
113 ilog(L_MAIN, "%s", str);
114 }
115
116 static void
117 ircd_restart_cb(const char *str)
118 {
119 ilog(L_MAIN, "%s", str);
120 }
121
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 */
129 static void
130 ircd_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 */
147 static void
148 init_sys(void)
149 {
150 #if defined(RLIMIT_NOFILE) && defined(HAVE_SYS_RESOURCE_H)
151 struct rlimit limit;
152
153 if(!getrlimit(RLIMIT_NOFILE, &limit))
154 {
155 limit.rlim_cur = limit.rlim_max; /* make soft limit the max */
156 if(setrlimit(RLIMIT_NOFILE, &limit) == -1)
157 {
158 fprintf(stderr, "error setting max fd's to %ld\n", (long) limit.rlim_cur);
159 exit(EXIT_FAILURE);
160 }
161 }
162 #endif /* RLIMIT_NOFILE */
163 }
164
165 static int
166 make_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
206 static int printVersion = 0;
207
208 struct 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
233 void
234 set_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
259 static void
260 check_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
287 void
288 charybdis_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
303 rb_select(250);
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 */
314 static void
315 initialize_global_set_options(void)
316 {
317 memset(&GlobalSetOptions, 0, sizeof(GlobalSetOptions));
318 /* memset( &ConfigFileEntry, 0, sizeof(ConfigFileEntry)); */
319
320 GlobalSetOptions.maxclients = ServerInfo.max_clients;
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 */
362 static void
363 initialize_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 */
376 static void
377 write_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
385 rb_snprintf(buff, sizeof(buff), "%u\n", pid);
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 */
409 static void
410 check_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 */
440 static void
441 setup_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 struct ev_entry *check_splitmode_ev = NULL;
456
457 /*
458 * main
459 *
460 * Initializes the IRCd.
461 *
462 * Inputs - number of commandline args, args themselves
463 * Outputs - none
464 * Side Effects - this is where the ircd gets going right now
465 */
466 int
467 main(int argc, char *argv[])
468 {
469 int fd;
470
471 /* Check to see if the user is running us as root, which is a nono */
472 if(geteuid() == 0)
473 {
474 fprintf(stderr, "Don't run ircd as root!!!\n");
475 return -1;
476 }
477
478 /*
479 * save server boot time right away, so getrusage works correctly
480 */
481 set_time();
482 /*
483 * Setup corefile size immediately after boot -kre
484 */
485 setup_corefile();
486
487 ServerRunning = 0;
488 /* It ain't random, but it ought to be a little harder to guess */
489 srand(SystemTime.tv_sec ^ (SystemTime.tv_usec | (getpid() << 20)));
490 memset(&me, 0, sizeof(me));
491 memset(&meLocalUser, 0, sizeof(meLocalUser));
492 me.localClient = &meLocalUser;
493
494 /* Make sure all lists are zeroed */
495 memset(&unknown_list, 0, sizeof(unknown_list));
496 memset(&lclient_list, 0, sizeof(lclient_list));
497 memset(&serv_list, 0, sizeof(serv_list));
498 memset(&global_serv_list, 0, sizeof(global_serv_list));
499 memset(&local_oper_list, 0, sizeof(local_oper_list));
500 memset(&oper_list, 0, sizeof(oper_list));
501
502 rb_dlinkAddTail(&me, &me.node, &global_client_list);
503
504 memset((void *) &Count, 0, sizeof(Count));
505 memset((void *) &ServerInfo, 0, sizeof(ServerInfo));
506 memset((void *) &AdminInfo, 0, sizeof(AdminInfo));
507
508 /* Initialise the channel capability usage counts... */
509 init_chcap_usage_counts();
510
511 ConfigFileEntry.dpath = DPATH;
512 ConfigFileEntry.configfile = CPATH; /* Server configuration file */
513 ConfigFileEntry.klinefile = KPATH; /* Server kline file */
514 ConfigFileEntry.dlinefile = DLPATH; /* dline file */
515 ConfigFileEntry.xlinefile = XPATH;
516 ConfigFileEntry.resvfile = RESVPATH;
517 ConfigFileEntry.connect_timeout = 30; /* Default to 30 */
518 myargv = argv;
519 umask(077); /* better safe than sorry --SRB */
520
521 parseargs(&argc, &argv, myopts);
522
523 if(printVersion)
524 {
525 printf("ircd: version %s\n", ircd_version);
526 exit(EXIT_SUCCESS);
527 }
528
529 if(chdir(ConfigFileEntry.dpath))
530 {
531 fprintf(stderr, "Unable to chdir to %s: %s\n", ConfigFileEntry.dpath, strerror(errno));
532 exit(EXIT_FAILURE);
533 }
534
535 setup_signals();
536
537 #ifdef __CYGWIN__
538 server_state_foreground = 1;
539 #endif
540
541 if (testing_conf)
542 server_state_foreground = 1;
543
544 /* Make sure fd 0, 1 and 2 are in use -- jilles */
545 do
546 {
547 fd = open("/dev/null", O_RDWR);
548 } while (fd < 2 && fd != -1);
549 if (fd > 2)
550 close(fd);
551 else if (fd == -1)
552 exit(1);
553
554 /* Check if there is pidfile and daemon already running */
555 if(!testing_conf)
556 {
557 check_pidfile(pidFileName);
558
559 if(!server_state_foreground)
560 make_daemon();
561 inotice("starting %s ...", ircd_version);
562 }
563
564 /* Init the event subsystem */
565 init_sys();
566 libcharybdis_init(ircd_log_cb, restart, ircd_die_cb);
567 rb_lib_init(ircd_log_cb, restart, ircd_die_cb);
568
569 fdlist_init();
570 if(!server_state_foreground)
571 {
572 rb_close_all();
573 }
574
575 init_main_logfile();
576 init_patricia();
577 newconf_init();
578 init_s_conf();
579 init_s_newconf();
580 init_hash();
581 clear_scache_hash_table(); /* server cache name table */
582 init_host_hash();
583 clear_hash_parse();
584 init_client();
585 initUser();
586 init_hook();
587 init_channels();
588 initclass();
589 initwhowas();
590 init_stats();
591 init_reject();
592 init_cache();
593 init_monitor();
594 init_isupport();
595 load_all_modules(1);
596 #ifndef STATIC_MODULES
597 load_core_modules(1);
598 #endif
599 init_auth(); /* Initialise the auth code */
600 init_resolver(); /* Needs to be setup before the io loop */
601
602 if (testing_conf)
603 fprintf(stderr, "\nBeginning config test\n");
604 read_conf_files(YES); /* cold start init conf files */
605 rehash_bans(0);
606 #ifndef STATIC_MODULES
607
608 mod_add_path(MODULE_DIR);
609 mod_add_path(MODULE_DIR "/autoload");
610 #endif
611
612 initialize_server_capabs(); /* Set up default_server_capabs */
613 initialize_global_set_options();
614
615 if(ServerInfo.name == NULL)
616 {
617 ierror("no server name specified in serverinfo block.");
618 return -1;
619 }
620 strlcpy(me.name, ServerInfo.name, sizeof(me.name));
621
622 if(ServerInfo.sid[0] == '\0')
623 {
624 ierror("no server sid specified in serverinfo block.");
625 return -2;
626 }
627 strcpy(me.id, ServerInfo.sid);
628 init_uid();
629
630 /* serverinfo{} description must exist. If not, error out. */
631 if(ServerInfo.description == NULL)
632 {
633 ierror("no server description specified in serverinfo block.");
634 return -3;
635 }
636 strlcpy(me.info, ServerInfo.description, sizeof(me.info));
637
638 if (testing_conf)
639 {
640 fprintf(stderr, "\nConfig testing complete.\n");
641 fflush(stderr);
642 return 0; /* Why? We want the launcher to exit out. */
643 }
644
645 me.from = &me;
646 me.servptr = &me;
647 SetMe(&me);
648 make_server(&me);
649 startup_time = CurrentTime;
650 add_to_client_hash(me.name, &me);
651 add_to_id_hash(me.id, &me);
652 me.serv->nameinfo = scache_connect(me.name, me.info, 0);
653
654 rb_dlinkAddAlloc(&me, &global_serv_list);
655
656 construct_umodebuf();
657
658 check_class();
659 write_pidfile(pidFileName);
660 load_help();
661 open_logfiles();
662
663 ilog(L_MAIN, "Server Ready");
664
665 eventAddIsh("cleanup_glines", cleanup_glines, NULL, CLEANUP_GLINES_TIME);
666
667 /* We want try_connections to be called as soon as possible now! -- adrian */
668 /* No, 'cause after a restart it would cause all sorts of nick collides */
669 /* um. by waiting even longer, that just means we have even *more*
670 * nick collisions. what a stupid idea. set an event for the IO loop --fl
671 */
672 eventAddIsh("try_connections", try_connections, NULL, STARTUP_CONNECTIONS_TIME);
673 eventAddOnce("try_connections_startup", try_connections, NULL, 0);
674
675 eventAddIsh("collect_zipstats", collect_zipstats, NULL, ZIPSTATS_TIME);
676
677 /* Setup the timeout check. I'll shift it later :) -- adrian */
678 eventAddIsh("rb_checktimeouts", rb_checktimeouts, NULL, 1);
679
680 eventAdd("check_rehash", check_rehash, NULL, 1);
681
682 if(splitmode)
683 check_splitmode_ev = rb_event_add("check_splitmode", check_splitmode, NULL, 2);
684
685 ServerRunning = 1;
686
687 print_startup(getpid());
688
689 charybdis_io_loop();
690
691 return 0;
692 }