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 * print_startup - print startup information
87 print_startup(int pid
)
89 inotice("now running in %s mode from %s as pid %d ...",
90 !server_state_foreground
? "background" : "foreground",
91 ConfigFileEntry
.dpath
, pid
);
93 /* let the parent process know the initialization was successful
95 if (!server_state_foreground
)
100 open("/dev/null", O_RDWR
);
106 ircd_log_cb(const char *str
)
108 ilog(L_MAIN
, "%s", str
);
112 ircd_restart_cb(const char *str
)
118 * Why EXIT_FAILURE here?
119 * Because if ircd_die_cb() is called it's because of a fatal
120 * error inside libcharybdis, and we don't know how to handle the
121 * exception, so it is logical to return a FAILURE exit code here.
125 ircd_die_cb(const char *str
)
127 /* Try to get the message out to currently logged in operators. */
128 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
, "Server panic! %s", str
);
129 inotice("server panic: %s", str
);
138 * inputs - boot_daemon flag
140 * side effects - if boot_daemon flag is not set, don't daemonize
145 #if defined(RLIMIT_NOFILE) && defined(HAVE_SYS_RESOURCE_H)
148 if(!getrlimit(RLIMIT_NOFILE
, &limit
))
150 limit
.rlim_cur
= limit
.rlim_max
; /* make soft limit the max */
151 if(setrlimit(RLIMIT_NOFILE
, &limit
) == -1)
153 fprintf(stderr
, "error setting max fd's to %ld\n", (long) limit
.rlim_cur
);
157 #endif /* RLIMIT_NOFILE */
174 if((pid
= fork()) < 0)
182 /* Wait for initialization to finish, successfully or
183 * unsuccessfully. Until this point the child may still
184 * write to stdout/stderr.
186 if (read(pip
[0], &c
, 1) > 0)
201 static int printVersion
= 0;
203 struct lgetopt myopts
[] = {
204 {"dlinefile", &ConfigFileEntry
.dlinefile
,
205 STRING
, "File to use for dlines.conf"},
206 {"configfile", &ConfigFileEntry
.configfile
,
207 STRING
, "File to use for ircd.conf"},
208 {"klinefile", &ConfigFileEntry
.klinefile
,
209 STRING
, "File to use for kline.conf"},
210 {"xlinefile", &ConfigFileEntry
.xlinefile
,
211 STRING
, "File to use for xline.conf"},
212 {"resvfile", &ConfigFileEntry
.resvfile
,
213 STRING
, "File to use for resv.conf"},
214 {"logfile", &logFileName
,
215 STRING
, "File to use for ircd.log"},
216 {"pidfile", &pidFileName
,
217 STRING
, "File to use for process ID"},
218 {"foreground", &server_state_foreground
,
219 YESNO
, "Run in foreground (don't detach)"},
220 {"version", &printVersion
,
221 YESNO
, "Print version and exit"},
222 {"conftest", &testing_conf
,
223 YESNO
, "Test the configuration files and exit"},
224 {"help", NULL
, USAGE
, "Print this text"},
225 {NULL
, NULL
, STRING
, NULL
},
231 struct timeval newtime
;
235 #ifdef HAVE_GETTIMEOFDAY
236 if(gettimeofday(&newtime
, NULL
) == -1)
238 ilog(L_MAIN
, "Clock Failure (%d)", errno
);
239 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
240 "Clock Failure (%d), TS can be corrupted", errno
);
242 restart("Clock Failure");
245 newtime
.tv_sec
= time(NULL
);
249 SystemTime
.tv_sec
= newtime
.tv_sec
;
250 SystemTime
.tv_usec
= newtime
.tv_usec
;
256 check_rehash(void *unused
)
259 * Check to see whether we have to rehash the configuration ..
275 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
276 "Got signal SIGUSR1, reloading ircd motd file");
277 free_cachefile(user_motd
);
278 user_motd
= cache_file(MPATH
, "ircd.motd", 0);
284 * initalialize_global_set_options
288 * side effects - This sets all global set options needed
291 initialize_global_set_options(void)
293 memset(&GlobalSetOptions
, 0, sizeof(GlobalSetOptions
));
294 /* memset( &ConfigFileEntry, 0, sizeof(ConfigFileEntry)); */
296 GlobalSetOptions
.maxclients
= ServerInfo
.max_clients
;
297 GlobalSetOptions
.autoconn
= 1;
299 GlobalSetOptions
.spam_time
= MIN_JOIN_LEAVE_TIME
;
300 GlobalSetOptions
.spam_num
= MAX_JOIN_LEAVE_COUNT
;
302 if(ConfigFileEntry
.default_floodcount
)
303 GlobalSetOptions
.floodcount
= ConfigFileEntry
.default_floodcount
;
305 GlobalSetOptions
.floodcount
= 10;
307 split_servers
= ConfigChannel
.default_split_server_count
;
308 split_users
= ConfigChannel
.default_split_user_count
;
310 if(split_users
&& split_servers
311 && (ConfigChannel
.no_create_on_split
|| ConfigChannel
.no_join_on_split
))
317 GlobalSetOptions
.ident_timeout
= IDENT_TIMEOUT
;
319 strlcpy(GlobalSetOptions
.operstring
,
320 ConfigFileEntry
.default_operstring
,
321 sizeof(GlobalSetOptions
.operstring
));
322 strlcpy(GlobalSetOptions
.adminstring
,
323 ConfigFileEntry
.default_adminstring
,
324 sizeof(GlobalSetOptions
.adminstring
));
326 /* memset( &ConfigChannel, 0, sizeof(ConfigChannel)); */
328 /* End of global set options */
333 * initialize_server_capabs
339 initialize_server_capabs(void)
341 default_server_capabs
&= ~CAP_ZIP
;
348 * inputs - filename+path of pid file
350 * side effects - write the pid of the ircd to filename
353 write_pidfile(const char *filename
)
357 if((fb
= fopen(filename
, "w")))
359 unsigned int pid
= (unsigned int) getpid();
361 rb_snprintf(buff
, sizeof(buff
), "%u\n", pid
);
362 if((fputs(buff
, fb
) == -1))
364 ilog(L_MAIN
, "Error writing %u to pid file %s (%s)",
365 pid
, filename
, strerror(errno
));
372 ilog(L_MAIN
, "Error opening pid file %s", filename
);
379 * inputs - filename+path of pid file
381 * side effects - reads pid from pidfile and checks if ircd is in process
382 * list. if it is, gracefully exits
386 check_pidfile(const char *filename
)
392 /* Don't do logging here, since we don't have log() initialised */
393 if((fb
= fopen(filename
, "r")))
395 if(fgets(buff
, 20, fb
) != NULL
)
397 pidfromfile
= atoi(buff
);
398 if(!kill(pidfromfile
, 0))
400 printf("ircd: daemon is already running\n");
413 * side effects - setups corefile to system limits.
419 #ifdef HAVE_SYS_RESOURCE_H
420 struct rlimit rlim
; /* resource limits */
422 /* Set corefilesize to maximum */
423 if(!getrlimit(RLIMIT_CORE
, &rlim
))
425 rlim
.rlim_cur
= rlim
.rlim_max
;
426 setrlimit(RLIMIT_CORE
, &rlim
);
431 struct ev_entry
*check_splitmode_ev
= NULL
;
436 * Initializes the IRCd.
438 * Inputs - number of commandline args, args themselves
440 * Side Effects - this is where the ircd gets going right now
443 main(int argc
, char *argv
[])
447 /* Check to see if the user is running us as root, which is a nono */
450 fprintf(stderr
, "Don't run ircd as root!!!\n");
455 * save server boot time right away, so getrusage works correctly
459 * Setup corefile size immediately after boot -kre
464 /* It ain't random, but it ought to be a little harder to guess */
465 srand(SystemTime
.tv_sec
^ (SystemTime
.tv_usec
| (getpid() << 20)));
466 memset(&me
, 0, sizeof(me
));
467 memset(&meLocalUser
, 0, sizeof(meLocalUser
));
468 me
.localClient
= &meLocalUser
;
470 /* Make sure all lists are zeroed */
471 memset(&unknown_list
, 0, sizeof(unknown_list
));
472 memset(&lclient_list
, 0, sizeof(lclient_list
));
473 memset(&serv_list
, 0, sizeof(serv_list
));
474 memset(&global_serv_list
, 0, sizeof(global_serv_list
));
475 memset(&local_oper_list
, 0, sizeof(local_oper_list
));
476 memset(&oper_list
, 0, sizeof(oper_list
));
478 rb_dlinkAddTail(&me
, &me
.node
, &global_client_list
);
480 memset((void *) &Count
, 0, sizeof(Count
));
481 memset((void *) &ServerInfo
, 0, sizeof(ServerInfo
));
482 memset((void *) &AdminInfo
, 0, sizeof(AdminInfo
));
484 /* Initialise the channel capability usage counts... */
485 init_chcap_usage_counts();
487 ConfigFileEntry
.dpath
= DPATH
;
488 ConfigFileEntry
.configfile
= CPATH
; /* Server configuration file */
489 ConfigFileEntry
.klinefile
= KPATH
; /* Server kline file */
490 ConfigFileEntry
.dlinefile
= DLPATH
; /* dline file */
491 ConfigFileEntry
.xlinefile
= XPATH
;
492 ConfigFileEntry
.resvfile
= RESVPATH
;
493 ConfigFileEntry
.connect_timeout
= 30; /* Default to 30 */
495 umask(077); /* better safe than sorry --SRB */
497 parseargs(&argc
, &argv
, myopts
);
501 printf("ircd: version %s\n", ircd_version
);
505 if(chdir(ConfigFileEntry
.dpath
))
507 fprintf(stderr
, "Unable to chdir to %s: %s\n", ConfigFileEntry
.dpath
, strerror(errno
));
514 server_state_foreground
= 1;
518 server_state_foreground
= 1;
520 /* Make sure fd 0, 1 and 2 are in use -- jilles */
523 fd
= open("/dev/null", O_RDWR
);
524 } while (fd
< 2 && fd
!= -1);
530 /* Check if there is pidfile and daemon already running */
533 check_pidfile(pidFileName
);
535 if(!server_state_foreground
)
537 inotice("starting %s ...", ircd_version
);
540 /* Init the event subsystem */
542 rb_lib_init(ircd_log_cb
, ircd_restart_cb
, ircd_die_cb
, !server_state_foreground
, maxconnections
, DNODE_HEAP_SIZE
, FD_HEAP_SIZE
);
543 rb_linebuf_init(LINEBUF_HEAP_SIZE
);
550 clear_scache_hash_table(); /* server cache name table */
565 #ifndef STATIC_MODULES
566 load_core_modules(1);
568 init_auth(); /* Initialise the auth code */
569 init_resolver(); /* Needs to be setup before the io loop */
572 fprintf(stderr
, "\nBeginning config test\n");
573 read_conf_files(YES
); /* cold start init conf files */
575 #ifndef STATIC_MODULES
577 mod_add_path(MODULE_DIR
);
578 mod_add_path(MODULE_DIR
"/autoload");
581 initialize_server_capabs(); /* Set up default_server_capabs */
582 initialize_global_set_options();
584 if(ServerInfo
.name
== NULL
)
586 ierror("no server name specified in serverinfo block.");
589 strlcpy(me
.name
, ServerInfo
.name
, sizeof(me
.name
));
591 if(ServerInfo
.sid
[0] == '\0')
593 ierror("no server sid specified in serverinfo block.");
596 strcpy(me
.id
, ServerInfo
.sid
);
599 /* serverinfo{} description must exist. If not, error out. */
600 if(ServerInfo
.description
== NULL
)
602 ierror("no server description specified in serverinfo block.");
605 strlcpy(me
.info
, ServerInfo
.description
, sizeof(me
.info
));
609 fprintf(stderr
, "\nConfig testing complete.\n");
611 return 0; /* Why? We want the launcher to exit out. */
618 startup_time
= rb_current_time();
619 add_to_client_hash(me
.name
, &me
);
620 add_to_id_hash(me
.id
, &me
);
621 me
.serv
->nameinfo
= scache_connect(me
.name
, me
.info
, 0);
623 rb_dlinkAddAlloc(&me
, &global_serv_list
);
625 construct_umodebuf();
628 write_pidfile(pidFileName
);
632 ilog(L_MAIN
, "Server Ready");
634 rb_event_addish("cleanup_glines", cleanup_glines
, NULL
, CLEANUP_GLINES_TIME
);
636 /* We want try_connections to be called as soon as possible now! -- adrian */
637 /* No, 'cause after a restart it would cause all sorts of nick collides */
638 /* um. by waiting even longer, that just means we have even *more*
639 * nick collisions. what a stupid idea. set an event for the IO loop --fl
641 rb_event_addish("try_connections", try_connections
, NULL
, STARTUP_CONNECTIONS_TIME
);
642 rb_event_addonce("try_connections_startup", try_connections
, NULL
, 0);
644 rb_event_addish("collect_zipstats", collect_zipstats
, NULL
, ZIPSTATS_TIME
);
646 /* Setup the timeout check. I'll shift it later :) -- adrian */
647 rb_event_addish("rb_checktimeouts", rb_checktimeouts
, NULL
, 1);
649 rb_event_add("check_rehash", check_rehash
, NULL
, 1);
652 check_splitmode_ev
= rb_event_add("check_splitmode", check_splitmode
, NULL
, 2);
656 print_startup(getpid());