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 */
83 struct ServerStatistics ServerStats
;
86 * print_startup - print startup information
89 print_startup(int pid
)
91 inotice("now running in %s mode from %s as pid %d ...",
92 !server_state_foreground
? "background" : "foreground",
93 ConfigFileEntry
.dpath
, pid
);
95 /* let the parent process know the initialization was successful
97 if (!server_state_foreground
)
102 open("/dev/null", O_RDWR
);
108 ircd_log_cb(const char *str
)
110 ilog(L_MAIN
, "%s", str
);
114 ircd_restart_cb(const char *str
)
120 * Why EXIT_FAILURE here?
121 * Because if ircd_die_cb() is called it's because of a fatal
122 * error inside libcharybdis, and we don't know how to handle the
123 * exception, so it is logical to return a FAILURE exit code here.
127 ircd_die_cb(const char *str
)
129 /* Try to get the message out to currently logged in operators. */
130 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
, "Server panic! %s", str
);
131 inotice("server panic: %s", str
);
140 * inputs - boot_daemon flag
142 * side effects - if boot_daemon flag is not set, don't daemonize
147 #if defined(RLIMIT_NOFILE) && defined(HAVE_SYS_RESOURCE_H)
150 if(!getrlimit(RLIMIT_NOFILE
, &limit
))
152 limit
.rlim_cur
= limit
.rlim_max
; /* make soft limit the max */
153 if(setrlimit(RLIMIT_NOFILE
, &limit
) == -1)
155 fprintf(stderr
, "error setting max fd's to %ld\n", (long) limit
.rlim_cur
);
160 maxconnections
= limit
.rlim_cur
;
161 #endif /* RLIMIT_NOFILE */
178 if((pid
= fork()) < 0)
186 /* Wait for initialization to finish, successfully or
187 * unsuccessfully. Until this point the child may still
188 * write to stdout/stderr.
190 if (read(pip
[0], &c
, 1) > 0)
205 static int printVersion
= 0;
207 struct lgetopt myopts
[] = {
208 {"dlinefile", &ConfigFileEntry
.dlinefile
,
209 STRING
, "File to use for dlines.conf"},
210 {"configfile", &ConfigFileEntry
.configfile
,
211 STRING
, "File to use for ircd.conf"},
212 {"klinefile", &ConfigFileEntry
.klinefile
,
213 STRING
, "File to use for kline.conf"},
214 {"xlinefile", &ConfigFileEntry
.xlinefile
,
215 STRING
, "File to use for xline.conf"},
216 {"resvfile", &ConfigFileEntry
.resvfile
,
217 STRING
, "File to use for resv.conf"},
218 {"logfile", &logFileName
,
219 STRING
, "File to use for ircd.log"},
220 {"pidfile", &pidFileName
,
221 STRING
, "File to use for process ID"},
222 {"foreground", &server_state_foreground
,
223 YESNO
, "Run in foreground (don't detach)"},
224 {"version", &printVersion
,
225 YESNO
, "Print version and exit"},
226 {"conftest", &testing_conf
,
227 YESNO
, "Test the configuration files and exit"},
228 {"help", NULL
, USAGE
, "Print this text"},
229 {NULL
, NULL
, STRING
, NULL
},
233 check_rehash(void *unused
)
236 * Check to see whether we have to rehash the configuration ..
252 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
253 "Got signal SIGUSR1, reloading ircd motd file");
254 free_cachefile(user_motd
);
255 user_motd
= cache_file(MPATH
, "ircd.motd", 0);
261 * initalialize_global_set_options
265 * side effects - This sets all global set options needed
268 initialize_global_set_options(void)
270 memset(&GlobalSetOptions
, 0, sizeof(GlobalSetOptions
));
271 /* memset( &ConfigFileEntry, 0, sizeof(ConfigFileEntry)); */
273 GlobalSetOptions
.maxclients
= ServerInfo
.max_clients
;
274 GlobalSetOptions
.autoconn
= 1;
276 GlobalSetOptions
.spam_time
= MIN_JOIN_LEAVE_TIME
;
277 GlobalSetOptions
.spam_num
= MAX_JOIN_LEAVE_COUNT
;
279 if(ConfigFileEntry
.default_floodcount
)
280 GlobalSetOptions
.floodcount
= ConfigFileEntry
.default_floodcount
;
282 GlobalSetOptions
.floodcount
= 10;
284 split_servers
= ConfigChannel
.default_split_server_count
;
285 split_users
= ConfigChannel
.default_split_user_count
;
287 if(split_users
&& split_servers
288 && (ConfigChannel
.no_create_on_split
|| ConfigChannel
.no_join_on_split
))
294 GlobalSetOptions
.ident_timeout
= IDENT_TIMEOUT
;
296 strlcpy(GlobalSetOptions
.operstring
,
297 ConfigFileEntry
.default_operstring
,
298 sizeof(GlobalSetOptions
.operstring
));
299 strlcpy(GlobalSetOptions
.adminstring
,
300 ConfigFileEntry
.default_adminstring
,
301 sizeof(GlobalSetOptions
.adminstring
));
303 /* memset( &ConfigChannel, 0, sizeof(ConfigChannel)); */
305 /* End of global set options */
310 * initialize_server_capabs
316 initialize_server_capabs(void)
318 default_server_capabs
&= ~CAP_ZIP
;
325 * inputs - filename+path of pid file
327 * side effects - write the pid of the ircd to filename
330 write_pidfile(const char *filename
)
334 if((fb
= fopen(filename
, "w")))
336 unsigned int pid
= (unsigned int) getpid();
338 rb_snprintf(buff
, sizeof(buff
), "%u\n", pid
);
339 if((fputs(buff
, fb
) == -1))
341 ilog(L_MAIN
, "Error writing %u to pid file %s (%s)",
342 pid
, filename
, strerror(errno
));
349 ilog(L_MAIN
, "Error opening pid file %s", filename
);
356 * inputs - filename+path of pid file
358 * side effects - reads pid from pidfile and checks if ircd is in process
359 * list. if it is, gracefully exits
363 check_pidfile(const char *filename
)
369 /* Don't do logging here, since we don't have log() initialised */
370 if((fb
= fopen(filename
, "r")))
372 if(fgets(buff
, 20, fb
) != NULL
)
374 pidfromfile
= atoi(buff
);
375 if(!kill(pidfromfile
, 0))
377 printf("ircd: daemon is already running\n");
390 * side effects - setups corefile to system limits.
396 #ifdef HAVE_SYS_RESOURCE_H
397 struct rlimit rlim
; /* resource limits */
399 /* Set corefilesize to maximum */
400 if(!getrlimit(RLIMIT_CORE
, &rlim
))
402 rlim
.rlim_cur
= rlim
.rlim_max
;
403 setrlimit(RLIMIT_CORE
, &rlim
);
408 struct ev_entry
*check_splitmode_ev
= NULL
;
413 * Initializes the IRCd.
415 * Inputs - number of commandline args, args themselves
417 * Side Effects - this is where the ircd gets going right now
420 main(int argc
, char *argv
[])
424 /* Check to see if the user is running us as root, which is a nono */
427 fprintf(stderr
, "Don't run ircd as root!!!\n");
432 * Setup corefile size immediately after boot -kre
437 /* It ain't random, but it ought to be a little harder to guess */
438 srand(SystemTime
.tv_sec
^ (SystemTime
.tv_usec
| (getpid() << 20)));
439 memset(&me
, 0, sizeof(me
));
440 memset(&meLocalUser
, 0, sizeof(meLocalUser
));
441 me
.localClient
= &meLocalUser
;
443 /* Make sure all lists are zeroed */
444 memset(&unknown_list
, 0, sizeof(unknown_list
));
445 memset(&lclient_list
, 0, sizeof(lclient_list
));
446 memset(&serv_list
, 0, sizeof(serv_list
));
447 memset(&global_serv_list
, 0, sizeof(global_serv_list
));
448 memset(&local_oper_list
, 0, sizeof(local_oper_list
));
449 memset(&oper_list
, 0, sizeof(oper_list
));
451 rb_dlinkAddTail(&me
, &me
.node
, &global_client_list
);
453 memset((void *) &Count
, 0, sizeof(Count
));
454 memset((void *) &ServerInfo
, 0, sizeof(ServerInfo
));
455 memset((void *) &AdminInfo
, 0, sizeof(AdminInfo
));
456 memset((void *) &ServerStats
, 0, sizeof(struct ServerStatistics
));
458 /* Initialise the channel capability usage counts... */
459 init_chcap_usage_counts();
461 ConfigFileEntry
.dpath
= DPATH
;
462 ConfigFileEntry
.configfile
= CPATH
; /* Server configuration file */
463 ConfigFileEntry
.klinefile
= KPATH
; /* Server kline file */
464 ConfigFileEntry
.dlinefile
= DLPATH
; /* dline file */
465 ConfigFileEntry
.xlinefile
= XPATH
;
466 ConfigFileEntry
.resvfile
= RESVPATH
;
467 ConfigFileEntry
.connect_timeout
= 30; /* Default to 30 */
469 umask(077); /* better safe than sorry --SRB */
471 parseargs(&argc
, &argv
, myopts
);
475 printf("ircd: version %s\n", ircd_version
);
479 if(chdir(ConfigFileEntry
.dpath
))
481 fprintf(stderr
, "Unable to chdir to %s: %s\n", ConfigFileEntry
.dpath
, strerror(errno
));
488 server_state_foreground
= 1;
492 server_state_foreground
= 1;
494 /* Make sure fd 0, 1 and 2 are in use -- jilles */
497 fd
= open("/dev/null", O_RDWR
);
498 } while (fd
< 2 && fd
!= -1);
504 /* Check if there is pidfile and daemon already running */
507 check_pidfile(pidFileName
);
509 if(!server_state_foreground
)
511 inotice("starting %s ...", ircd_version
);
514 /* Init the event subsystem */
516 rb_lib_init(ircd_log_cb
, ircd_restart_cb
, ircd_die_cb
, !server_state_foreground
, maxconnections
, DNODE_HEAP_SIZE
, FD_HEAP_SIZE
);
517 rb_linebuf_init(LINEBUF_HEAP_SIZE
);
524 clear_scache_hash_table(); /* server cache name table */
538 #ifndef STATIC_MODULES
539 load_core_modules(1);
541 init_auth(); /* Initialise the auth code */
542 init_resolver(); /* Needs to be setup before the io loop */
545 fprintf(stderr
, "\nBeginning config test\n");
546 read_conf_files(YES
); /* cold start init conf files */
548 #ifndef STATIC_MODULES
550 mod_add_path(MODULE_DIR
);
551 mod_add_path(MODULE_DIR
"/autoload");
554 initialize_server_capabs(); /* Set up default_server_capabs */
555 initialize_global_set_options();
557 if(ServerInfo
.name
== NULL
)
559 ierror("no server name specified in serverinfo block.");
562 strlcpy(me
.name
, ServerInfo
.name
, sizeof(me
.name
));
564 if(ServerInfo
.sid
[0] == '\0')
566 ierror("no server sid specified in serverinfo block.");
569 strcpy(me
.id
, ServerInfo
.sid
);
572 /* serverinfo{} description must exist. If not, error out. */
573 if(ServerInfo
.description
== NULL
)
575 ierror("no server description specified in serverinfo block.");
578 strlcpy(me
.info
, ServerInfo
.description
, sizeof(me
.info
));
582 fprintf(stderr
, "\nConfig testing complete.\n");
584 return 0; /* Why? We want the launcher to exit out. */
591 startup_time
= rb_current_time();
592 add_to_client_hash(me
.name
, &me
);
593 add_to_id_hash(me
.id
, &me
);
594 me
.serv
->nameinfo
= scache_connect(me
.name
, me
.info
, 0);
596 rb_dlinkAddAlloc(&me
, &global_serv_list
);
598 construct_umodebuf();
601 write_pidfile(pidFileName
);
605 ilog(L_MAIN
, "Server Ready");
607 rb_event_addish("cleanup_glines", cleanup_glines
, NULL
, CLEANUP_GLINES_TIME
);
609 /* We want try_connections to be called as soon as possible now! -- adrian */
610 /* No, 'cause after a restart it would cause all sorts of nick collides */
611 /* um. by waiting even longer, that just means we have even *more*
612 * nick collisions. what a stupid idea. set an event for the IO loop --fl
614 rb_event_addish("try_connections", try_connections
, NULL
, STARTUP_CONNECTIONS_TIME
);
615 rb_event_addonce("try_connections_startup", try_connections
, NULL
, 0);
617 rb_event_addish("collect_zipstats", collect_zipstats
, NULL
, ZIPSTATS_TIME
);
619 /* Setup the timeout check. I'll shift it later :) -- adrian */
620 rb_event_addish("rb_checktimeouts", rb_checktimeouts
, NULL
, 1);
622 rb_event_add("check_rehash", check_rehash
, NULL
, 1);
625 check_splitmode_ev
= rb_event_add("check_splitmode", check_splitmode
, NULL
, 2);
629 print_startup(getpid());