2 * ircd-ratbox: A slightly useful ircd.
3 * ircd.c: Starts up and runs the ircd.
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
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.
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.
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
24 * $Id: ircd.c 3380 2007-04-03 22:25:11Z jilles $
37 #include "irc_string.h"
38 #include "ircd_signal.h"
39 #include "sprintf_irc.h"
41 #include "msg.h" /* msgtab */
50 #include "s_serv.h" /* try_connections */
55 #include "supported.h"
59 #include "ircd_getopt.h"
63 #include "s_newconf.h"
66 #include "patchlevel.h"
69 #include "ratbox_lib.h"
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.
75 int _charybdis_data_version
= CHARYBDIS_DV
;
77 extern int ServerRunning
;
78 extern struct LocalUser meLocalUser
;
81 int maxconnections
; /* XXX */
84 struct ServerStatistics ServerStats
;
87 * print_startup - print startup information
90 print_startup(int pid
)
92 inotice("now running in %s mode from %s as pid %d ...",
93 !server_state_foreground
? "background" : "foreground",
94 ConfigFileEntry
.dpath
, pid
);
96 /* let the parent process know the initialization was successful
98 if (!server_state_foreground
)
103 open("/dev/null", O_RDWR
);
109 ircd_log_cb(const char *str
)
111 ilog(L_MAIN
, "%s", str
);
115 ircd_restart_cb(const char *str
)
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.
128 ircd_die_cb(const char *str
)
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
);
141 * inputs - boot_daemon flag
143 * side effects - if boot_daemon flag is not set, don't daemonize
148 #if defined(RLIMIT_NOFILE) && defined(HAVE_SYS_RESOURCE_H)
151 if(!getrlimit(RLIMIT_NOFILE
, &limit
))
153 limit
.rlim_cur
= limit
.rlim_max
; /* make soft limit the max */
154 if(setrlimit(RLIMIT_NOFILE
, &limit
) == -1)
156 fprintf(stderr
, "error setting max fd's to %ld\n", (long) limit
.rlim_cur
);
160 #endif /* RLIMIT_NOFILE */
177 if((pid
= fork()) < 0)
185 /* Wait for initialization to finish, successfully or
186 * unsuccessfully. Until this point the child may still
187 * write to stdout/stderr.
189 if (read(pip
[0], &c
, 1) > 0)
204 static int printVersion
= 0;
206 struct 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
},
234 struct timeval newtime
;
237 #ifdef HAVE_GETTIMEOFDAY
238 if(gettimeofday(&newtime
, NULL
) == -1)
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
);
244 restart("Clock Failure");
247 newtime
.tv_sec
= time(NULL
);
250 if(newtime
.tv_sec
< rb_current_time())
251 rb_set_back_events(rb_current_time() - newtime
.tv_sec
);
253 SystemTime
.tv_sec
= newtime
.tv_sec
;
254 SystemTime
.tv_usec
= newtime
.tv_usec
;
258 check_rehash(void *unused
)
261 * Check to see whether we have to rehash the configuration ..
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);
286 charybdis_io_loop(void)
290 while (ServerRunning
)
292 /* Run pending events, then get the number of seconds to the next
296 delay
= rb_event_next();
297 if(delay
<= rb_current_time())
306 * initalialize_global_set_options
310 * side effects - This sets all global set options needed
313 initialize_global_set_options(void)
315 memset(&GlobalSetOptions
, 0, sizeof(GlobalSetOptions
));
316 /* memset( &ConfigFileEntry, 0, sizeof(ConfigFileEntry)); */
318 GlobalSetOptions
.maxclients
= ServerInfo
.max_clients
;
319 GlobalSetOptions
.autoconn
= 1;
321 GlobalSetOptions
.spam_time
= MIN_JOIN_LEAVE_TIME
;
322 GlobalSetOptions
.spam_num
= MAX_JOIN_LEAVE_COUNT
;
324 if(ConfigFileEntry
.default_floodcount
)
325 GlobalSetOptions
.floodcount
= ConfigFileEntry
.default_floodcount
;
327 GlobalSetOptions
.floodcount
= 10;
329 split_servers
= ConfigChannel
.default_split_server_count
;
330 split_users
= ConfigChannel
.default_split_user_count
;
332 if(split_users
&& split_servers
333 && (ConfigChannel
.no_create_on_split
|| ConfigChannel
.no_join_on_split
))
339 GlobalSetOptions
.ident_timeout
= IDENT_TIMEOUT
;
341 strlcpy(GlobalSetOptions
.operstring
,
342 ConfigFileEntry
.default_operstring
,
343 sizeof(GlobalSetOptions
.operstring
));
344 strlcpy(GlobalSetOptions
.adminstring
,
345 ConfigFileEntry
.default_adminstring
,
346 sizeof(GlobalSetOptions
.adminstring
));
348 /* memset( &ConfigChannel, 0, sizeof(ConfigChannel)); */
350 /* End of global set options */
355 * initialize_server_capabs
361 initialize_server_capabs(void)
363 default_server_capabs
&= ~CAP_ZIP
;
370 * inputs - filename+path of pid file
372 * side effects - write the pid of the ircd to filename
375 write_pidfile(const char *filename
)
379 if((fb
= fopen(filename
, "w")))
381 unsigned int pid
= (unsigned int) getpid();
383 rb_snprintf(buff
, sizeof(buff
), "%u\n", pid
);
384 if((fputs(buff
, fb
) == -1))
386 ilog(L_MAIN
, "Error writing %u to pid file %s (%s)",
387 pid
, filename
, strerror(errno
));
394 ilog(L_MAIN
, "Error opening pid file %s", filename
);
401 * inputs - filename+path of pid file
403 * side effects - reads pid from pidfile and checks if ircd is in process
404 * list. if it is, gracefully exits
408 check_pidfile(const char *filename
)
414 /* Don't do logging here, since we don't have log() initialised */
415 if((fb
= fopen(filename
, "r")))
417 if(fgets(buff
, 20, fb
) != NULL
)
419 pidfromfile
= atoi(buff
);
420 if(!kill(pidfromfile
, 0))
422 printf("ircd: daemon is already running\n");
435 * side effects - setups corefile to system limits.
441 #ifdef HAVE_SYS_RESOURCE_H
442 struct rlimit rlim
; /* resource limits */
444 /* Set corefilesize to maximum */
445 if(!getrlimit(RLIMIT_CORE
, &rlim
))
447 rlim
.rlim_cur
= rlim
.rlim_max
;
448 setrlimit(RLIMIT_CORE
, &rlim
);
453 struct ev_entry
*check_splitmode_ev
= NULL
;
458 * Initializes the IRCd.
460 * Inputs - number of commandline args, args themselves
462 * Side Effects - this is where the ircd gets going right now
465 main(int argc
, char *argv
[])
469 /* Check to see if the user is running us as root, which is a nono */
472 fprintf(stderr
, "Don't run ircd as root!!!\n");
477 * save server boot time right away, so getrusage works correctly
481 * Setup corefile size immediately after boot -kre
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
;
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
));
500 rb_dlinkAddTail(&me
, &me
.node
, &global_client_list
);
502 memset((void *) &Count
, 0, sizeof(Count
));
503 memset((void *) &ServerInfo
, 0, sizeof(ServerInfo
));
504 memset((void *) &AdminInfo
, 0, sizeof(AdminInfo
));
505 memset(&ServerStats
, 0, sizeof(struct ServerStatistics
));
507 /* Initialise the channel capability usage counts... */
508 init_chcap_usage_counts();
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 */
518 umask(077); /* better safe than sorry --SRB */
520 parseargs(&argc
, &argv
, myopts
);
524 printf("ircd: version %s\n", ircd_version
);
528 if(chdir(ConfigFileEntry
.dpath
))
530 fprintf(stderr
, "Unable to chdir to %s: %s\n", ConfigFileEntry
.dpath
, strerror(errno
));
537 server_state_foreground
= 1;
541 server_state_foreground
= 1;
543 /* Make sure fd 0, 1 and 2 are in use -- jilles */
546 fd
= open("/dev/null", O_RDWR
);
547 } while (fd
< 2 && fd
!= -1);
553 /* Check if there is pidfile and daemon already running */
556 check_pidfile(pidFileName
);
558 if(!server_state_foreground
)
560 inotice("starting %s ...", ircd_version
);
563 /* Init the event subsystem */
565 rb_lib_init(ircd_log_cb
, ircd_restart_cb
, ircd_die_cb
, !server_state_foreground
, maxconnections
, DNODE_HEAP_SIZE
, FD_HEAP_SIZE
);
572 clear_scache_hash_table(); /* server cache name table */
586 #ifndef STATIC_MODULES
587 load_core_modules(1);
589 init_auth(); /* Initialise the auth code */
590 init_resolver(); /* Needs to be setup before the io loop */
593 fprintf(stderr
, "\nBeginning config test\n");
594 read_conf_files(YES
); /* cold start init conf files */
596 #ifndef STATIC_MODULES
598 mod_add_path(MODULE_DIR
);
599 mod_add_path(MODULE_DIR
"/autoload");
602 initialize_server_capabs(); /* Set up default_server_capabs */
603 initialize_global_set_options();
605 if(ServerInfo
.name
== NULL
)
607 ierror("no server name specified in serverinfo block.");
610 strlcpy(me
.name
, ServerInfo
.name
, sizeof(me
.name
));
612 if(ServerInfo
.sid
[0] == '\0')
614 ierror("no server sid specified in serverinfo block.");
617 strcpy(me
.id
, ServerInfo
.sid
);
620 /* serverinfo{} description must exist. If not, error out. */
621 if(ServerInfo
.description
== NULL
)
623 ierror("no server description specified in serverinfo block.");
626 strlcpy(me
.info
, ServerInfo
.description
, sizeof(me
.info
));
630 fprintf(stderr
, "\nConfig testing complete.\n");
632 return 0; /* Why? We want the launcher to exit out. */
639 startup_time
= rb_current_time();
640 add_to_client_hash(me
.name
, &me
);
641 add_to_id_hash(me
.id
, &me
);
642 me
.serv
->nameinfo
= scache_connect(me
.name
, me
.info
, 0);
644 rb_dlinkAddAlloc(&me
, &global_serv_list
);
646 construct_umodebuf();
649 write_pidfile(pidFileName
);
653 ilog(L_MAIN
, "Server Ready");
655 rb_event_addish("cleanup_glines", cleanup_glines
, NULL
, CLEANUP_GLINES_TIME
);
657 /* We want try_connections to be called as soon as possible now! -- adrian */
658 /* No, 'cause after a restart it would cause all sorts of nick collides */
659 /* um. by waiting even longer, that just means we have even *more*
660 * nick collisions. what a stupid idea. set an event for the IO loop --fl
662 rb_event_addish("try_connections", try_connections
, NULL
, STARTUP_CONNECTIONS_TIME
);
663 rb_event_addonce("try_connections_startup", try_connections
, NULL
, 0);
665 rb_event_addish("collect_zipstats", collect_zipstats
, NULL
, ZIPSTATS_TIME
);
667 /* Setup the timeout check. I'll shift it later :) -- adrian */
668 rb_event_addish("rb_checktimeouts", rb_checktimeouts
, NULL
, 1);
670 rb_event_add("check_rehash", check_rehash
, NULL
, 1);
673 check_splitmode_ev
= rb_event_add("check_splitmode", check_splitmode
, NULL
, 2);
677 print_startup(getpid());