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 $
39 #include "irc_string.h"
40 #include "ircd_signal.h"
41 #include "sprintf_irc.h"
43 #include "msg.h" /* msgtab */
53 #include "s_serv.h" /* try_connections */
58 #include "supported.h"
63 #include "ircd_getopt.h"
69 #include "s_newconf.h"
72 #include "libcharybdis.h"
73 #include "patchlevel.h"
76 #include "ratbox_lib.h"
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.
82 int _charybdis_data_version
= CHARYBDIS_DV
;
84 extern int ServerRunning
;
85 extern struct LocalUser meLocalUser
;
89 * print_startup - print startup information
92 print_startup(int pid
)
94 inotice("now running in %s mode from %s as pid %d ...",
95 !server_state_foreground
? "background" : "foreground",
96 ConfigFileEntry
.dpath
, pid
);
98 /* let the parent process know the initialization was successful
100 if (!server_state_foreground
)
105 open("/dev/null", O_RDWR
);
111 ircd_log_cb(const char *str
)
113 ilog(L_MAIN
, "%s", str
);
117 ircd_restart_cb(const char *str
)
119 ilog(L_MAIN
, "%s", str
);
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.
130 ircd_die_cb(const char *str
)
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
);
143 * inputs - boot_daemon flag
145 * side effects - if boot_daemon flag is not set, don't daemonize
150 #if defined(RLIMIT_NOFILE) && defined(HAVE_SYS_RESOURCE_H)
153 if(!getrlimit(RLIMIT_NOFILE
, &limit
))
155 limit
.rlim_cur
= limit
.rlim_max
; /* make soft limit the max */
156 if(setrlimit(RLIMIT_NOFILE
, &limit
) == -1)
158 fprintf(stderr
, "error setting max fd's to %ld\n", (long) limit
.rlim_cur
);
162 #endif /* RLIMIT_NOFILE */
179 if((pid
= fork()) < 0)
187 /* Wait for initialization to finish, successfully or
188 * unsuccessfully. Until this point the child may still
189 * write to stdout/stderr.
191 if (read(pip
[0], &c
, 1) > 0)
206 static int printVersion
= 0;
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
},
236 struct timeval newtime
;
239 #ifdef HAVE_GETTIMEOFDAY
240 if(gettimeofday(&newtime
, NULL
) == -1)
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
);
246 restart("Clock Failure");
249 newtime
.tv_sec
= time(NULL
);
252 if(newtime
.tv_sec
< CurrentTime
)
253 set_back_events(CurrentTime
- newtime
.tv_sec
);
255 SystemTime
.tv_sec
= newtime
.tv_sec
;
256 SystemTime
.tv_usec
= newtime
.tv_usec
;
260 check_rehash(void *unused
)
263 * Check to see whether we have to rehash the configuration ..
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);
288 charybdis_io_loop(void)
292 while (ServerRunning
)
294 /* Run pending events, then get the number of seconds to the next
298 delay
= eventNextTime();
299 if(delay
<= CurrentTime
)
308 * initalialize_global_set_options
312 * side effects - This sets all global set options needed
315 initialize_global_set_options(void)
317 memset(&GlobalSetOptions
, 0, sizeof(GlobalSetOptions
));
318 /* memset( &ConfigFileEntry, 0, sizeof(ConfigFileEntry)); */
320 GlobalSetOptions
.maxclients
= ServerInfo
.max_clients
;
321 GlobalSetOptions
.autoconn
= 1;
323 GlobalSetOptions
.spam_time
= MIN_JOIN_LEAVE_TIME
;
324 GlobalSetOptions
.spam_num
= MAX_JOIN_LEAVE_COUNT
;
326 if(ConfigFileEntry
.default_floodcount
)
327 GlobalSetOptions
.floodcount
= ConfigFileEntry
.default_floodcount
;
329 GlobalSetOptions
.floodcount
= 10;
331 split_servers
= ConfigChannel
.default_split_server_count
;
332 split_users
= ConfigChannel
.default_split_user_count
;
334 if(split_users
&& split_servers
335 && (ConfigChannel
.no_create_on_split
|| ConfigChannel
.no_join_on_split
))
341 GlobalSetOptions
.ident_timeout
= IDENT_TIMEOUT
;
343 strlcpy(GlobalSetOptions
.operstring
,
344 ConfigFileEntry
.default_operstring
,
345 sizeof(GlobalSetOptions
.operstring
));
346 strlcpy(GlobalSetOptions
.adminstring
,
347 ConfigFileEntry
.default_adminstring
,
348 sizeof(GlobalSetOptions
.adminstring
));
350 /* memset( &ConfigChannel, 0, sizeof(ConfigChannel)); */
352 /* End of global set options */
357 * initialize_server_capabs
363 initialize_server_capabs(void)
365 default_server_capabs
&= ~CAP_ZIP
;
372 * inputs - filename+path of pid file
374 * side effects - write the pid of the ircd to filename
377 write_pidfile(const char *filename
)
381 if((fb
= fopen(filename
, "w")))
383 unsigned int pid
= (unsigned int) getpid();
385 rb_snprintf(buff
, sizeof(buff
), "%u\n", pid
);
386 if((fputs(buff
, fb
) == -1))
388 ilog(L_MAIN
, "Error writing %u to pid file %s (%s)",
389 pid
, filename
, strerror(errno
));
396 ilog(L_MAIN
, "Error opening pid file %s", filename
);
403 * inputs - filename+path of pid file
405 * side effects - reads pid from pidfile and checks if ircd is in process
406 * list. if it is, gracefully exits
410 check_pidfile(const char *filename
)
416 /* Don't do logging here, since we don't have log() initialised */
417 if((fb
= fopen(filename
, "r")))
419 if(fgets(buff
, 20, fb
) != NULL
)
421 pidfromfile
= atoi(buff
);
422 if(!kill(pidfromfile
, 0))
424 printf("ircd: daemon is already running\n");
437 * side effects - setups corefile to system limits.
443 #ifdef HAVE_SYS_RESOURCE_H
444 struct rlimit rlim
; /* resource limits */
446 /* Set corefilesize to maximum */
447 if(!getrlimit(RLIMIT_CORE
, &rlim
))
449 rlim
.rlim_cur
= rlim
.rlim_max
;
450 setrlimit(RLIMIT_CORE
, &rlim
);
455 struct ev_entry
*check_splitmode_ev
= NULL
;
460 * Initializes the IRCd.
462 * Inputs - number of commandline args, args themselves
464 * Side Effects - this is where the ircd gets going right now
467 main(int argc
, char *argv
[])
471 /* Check to see if the user is running us as root, which is a nono */
474 fprintf(stderr
, "Don't run ircd as root!!!\n");
479 * save server boot time right away, so getrusage works correctly
483 * Setup corefile size immediately after boot -kre
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
;
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
));
502 rb_dlinkAddTail(&me
, &me
.node
, &global_client_list
);
504 memset((void *) &Count
, 0, sizeof(Count
));
505 memset((void *) &ServerInfo
, 0, sizeof(ServerInfo
));
506 memset((void *) &AdminInfo
, 0, sizeof(AdminInfo
));
508 /* Initialise the channel capability usage counts... */
509 init_chcap_usage_counts();
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 */
519 umask(077); /* better safe than sorry --SRB */
521 parseargs(&argc
, &argv
, myopts
);
525 printf("ircd: version %s\n", ircd_version
);
529 if(chdir(ConfigFileEntry
.dpath
))
531 fprintf(stderr
, "Unable to chdir to %s: %s\n", ConfigFileEntry
.dpath
, strerror(errno
));
538 server_state_foreground
= 1;
542 server_state_foreground
= 1;
544 /* Make sure fd 0, 1 and 2 are in use -- jilles */
547 fd
= open("/dev/null", O_RDWR
);
548 } while (fd
< 2 && fd
!= -1);
554 /* Check if there is pidfile and daemon already running */
557 check_pidfile(pidFileName
);
559 if(!server_state_foreground
)
561 inotice("starting %s ...", ircd_version
);
564 /* Init the event subsystem */
566 libcharybdis_init(ircd_log_cb
, restart
, ircd_die_cb
);
567 rb_lib_init(ircd_log_cb
, restart
, ircd_die_cb
);
570 if(!server_state_foreground
)
581 clear_scache_hash_table(); /* server cache name table */
596 #ifndef STATIC_MODULES
597 load_core_modules(1);
599 init_auth(); /* Initialise the auth code */
600 init_resolver(); /* Needs to be setup before the io loop */
603 fprintf(stderr
, "\nBeginning config test\n");
604 read_conf_files(YES
); /* cold start init conf files */
606 #ifndef STATIC_MODULES
608 mod_add_path(MODULE_DIR
);
609 mod_add_path(MODULE_DIR
"/autoload");
612 initialize_server_capabs(); /* Set up default_server_capabs */
613 initialize_global_set_options();
615 if(ServerInfo
.name
== NULL
)
617 ierror("no server name specified in serverinfo block.");
620 strlcpy(me
.name
, ServerInfo
.name
, sizeof(me
.name
));
622 if(ServerInfo
.sid
[0] == '\0')
624 ierror("no server sid specified in serverinfo block.");
627 strcpy(me
.id
, ServerInfo
.sid
);
630 /* serverinfo{} description must exist. If not, error out. */
631 if(ServerInfo
.description
== NULL
)
633 ierror("no server description specified in serverinfo block.");
636 strlcpy(me
.info
, ServerInfo
.description
, sizeof(me
.info
));
640 fprintf(stderr
, "\nConfig testing complete.\n");
642 return 0; /* Why? We want the launcher to exit out. */
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);
654 rb_dlinkAddAlloc(&me
, &global_serv_list
);
656 construct_umodebuf();
659 write_pidfile(pidFileName
);
663 ilog(L_MAIN
, "Server Ready");
665 eventAddIsh("cleanup_glines", cleanup_glines
, NULL
, CLEANUP_GLINES_TIME
);
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
672 eventAddIsh("try_connections", try_connections
, NULL
, STARTUP_CONNECTIONS_TIME
);
673 eventAddOnce("try_connections_startup", try_connections
, NULL
, 0);
675 eventAddIsh("collect_zipstats", collect_zipstats
, NULL
, ZIPSTATS_TIME
);
677 /* Setup the timeout check. I'll shift it later :) -- adrian */
678 eventAddIsh("rb_checktimeouts", rb_checktimeouts
, NULL
, 1);
680 eventAdd("check_rehash", check_rehash
, NULL
, 1);
683 check_splitmode_ev
= rb_event_add("check_splitmode", check_splitmode
, NULL
, 2);
687 print_startup(getpid());