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 $
27 #include "ratbox_lib.h"
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"
70 extern int ServerRunning
;
71 extern struct LocalUser meLocalUser
;
76 /* /quote set variables */
77 struct SetOptions GlobalSetOptions
;
79 /* configuration set from ircd.conf */
80 struct config_file_entry ConfigFileEntry
;
81 /* server info set from ircd.conf */
82 struct server_info ServerInfo
;
83 /* admin info set from ircd.conf */
84 struct admin_info AdminInfo
;
87 struct ServerStatistics ServerStats
;
93 * print_startup - print startup information
96 print_startup(int pid
)
98 inotice("now running in %s mode from %s as pid %d ...",
99 !server_state_foreground
? "background" : "foreground",
100 ConfigFileEntry
.dpath
, pid
);
102 /* let the parent process know the initialization was successful
104 if (!server_state_foreground
)
109 open("/dev/null", O_RDWR
);
115 ircd_log_cb(const char *str
)
117 ilog(L_MAIN
, "%s", str
);
121 ircd_restart_cb(const char *str
)
127 * Why EXIT_FAILURE here?
128 * Because if ircd_die_cb() is called it's because of a fatal
129 * error inside libcharybdis, and we don't know how to handle the
130 * exception, so it is logical to return a FAILURE exit code here.
134 ircd_die_cb(const char *str
)
136 /* Try to get the message out to currently logged in operators. */
137 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
, "Server panic! %s", str
);
138 inotice("server panic: %s", str
);
147 * inputs - boot_daemon flag
149 * side effects - if boot_daemon flag is not set, don't daemonize
154 #if defined(RLIMIT_NOFILE) && defined(HAVE_SYS_RESOURCE_H)
157 if(!getrlimit(RLIMIT_NOFILE
, &limit
))
159 maxconnections
= limit
.rlim_cur
;
160 if(maxconnections
<= MAX_BUFFER
)
162 fprintf(stderr
, "ERROR: Shell FD limits are too low.\n");
163 fprintf(stderr
, "ERROR: ircd-ratbox reserves %d FDs, shell limits must be above this\n", MAX_BUFFER
);
168 #endif /* RLIMIT_FD_MAX */
169 maxconnections
= MAXCONNECTIONS
;
186 if((pid
= fork()) < 0)
194 /* Wait for initialization to finish, successfully or
195 * unsuccessfully. Until this point the child may still
196 * write to stdout/stderr.
198 if (read(pip
[0], &c
, 1) > 0)
213 static int printVersion
= 0;
215 struct lgetopt myopts
[] = {
216 {"dlinefile", &ConfigFileEntry
.dlinefile
,
217 STRING
, "File to use for dlines.conf"},
218 {"configfile", &ConfigFileEntry
.configfile
,
219 STRING
, "File to use for ircd.conf"},
220 {"klinefile", &ConfigFileEntry
.klinefile
,
221 STRING
, "File to use for kline.conf"},
222 {"xlinefile", &ConfigFileEntry
.xlinefile
,
223 STRING
, "File to use for xline.conf"},
224 {"resvfile", &ConfigFileEntry
.resvfile
,
225 STRING
, "File to use for resv.conf"},
226 {"logfile", &logFileName
,
227 STRING
, "File to use for ircd.log"},
228 {"pidfile", &pidFileName
,
229 STRING
, "File to use for process ID"},
230 {"foreground", &server_state_foreground
,
231 YESNO
, "Run in foreground (don't detach)"},
232 {"version", &printVersion
,
233 YESNO
, "Print version and exit"},
234 {"conftest", &testing_conf
,
235 YESNO
, "Test the configuration files and exit"},
236 {"help", NULL
, USAGE
, "Print this text"},
237 {NULL
, NULL
, STRING
, NULL
},
241 check_rehash(void *unused
)
244 * Check to see whether we have to rehash the configuration ..
260 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
261 "Got signal SIGUSR1, reloading ircd motd file");
262 free_cachefile(user_motd
);
263 user_motd
= cache_file(MPATH
, "ircd.motd", 0);
269 * initalialize_global_set_options
273 * side effects - This sets all global set options needed
276 initialize_global_set_options(void)
278 memset(&GlobalSetOptions
, 0, sizeof(GlobalSetOptions
));
279 /* memset( &ConfigFileEntry, 0, sizeof(ConfigFileEntry)); */
281 GlobalSetOptions
.maxclients
= ServerInfo
.default_max_clients
;
283 if(GlobalSetOptions
.maxclients
> (maxconnections
- MAX_BUFFER
))
284 GlobalSetOptions
.maxclients
= maxconnections
- MAX_BUFFER
;
286 GlobalSetOptions
.autoconn
= 1;
288 GlobalSetOptions
.spam_time
= MIN_JOIN_LEAVE_TIME
;
289 GlobalSetOptions
.spam_num
= MAX_JOIN_LEAVE_COUNT
;
291 if(ConfigFileEntry
.default_floodcount
)
292 GlobalSetOptions
.floodcount
= ConfigFileEntry
.default_floodcount
;
294 GlobalSetOptions
.floodcount
= 10;
296 split_servers
= ConfigChannel
.default_split_server_count
;
297 split_users
= ConfigChannel
.default_split_user_count
;
299 if(split_users
&& split_servers
300 && (ConfigChannel
.no_create_on_split
|| ConfigChannel
.no_join_on_split
))
306 GlobalSetOptions
.ident_timeout
= IDENT_TIMEOUT
;
308 strlcpy(GlobalSetOptions
.operstring
,
309 ConfigFileEntry
.default_operstring
,
310 sizeof(GlobalSetOptions
.operstring
));
311 strlcpy(GlobalSetOptions
.adminstring
,
312 ConfigFileEntry
.default_adminstring
,
313 sizeof(GlobalSetOptions
.adminstring
));
315 /* memset( &ConfigChannel, 0, sizeof(ConfigChannel)); */
317 /* End of global set options */
322 * initialize_server_capabs
328 initialize_server_capabs(void)
330 default_server_capabs
&= ~CAP_ZIP
;
337 * inputs - filename+path of pid file
339 * side effects - write the pid of the ircd to filename
342 write_pidfile(const char *filename
)
346 if((fb
= fopen(filename
, "w")))
348 unsigned int pid
= (unsigned int) getpid();
350 rb_snprintf(buff
, sizeof(buff
), "%u\n", pid
);
351 if((fputs(buff
, fb
) == -1))
353 ilog(L_MAIN
, "Error writing %u to pid file %s (%s)",
354 pid
, filename
, strerror(errno
));
361 ilog(L_MAIN
, "Error opening pid file %s", filename
);
368 * inputs - filename+path of pid file
370 * side effects - reads pid from pidfile and checks if ircd is in process
371 * list. if it is, gracefully exits
375 check_pidfile(const char *filename
)
381 /* Don't do logging here, since we don't have log() initialised */
382 if((fb
= fopen(filename
, "r")))
384 if(fgets(buff
, 20, fb
) != NULL
)
386 pidfromfile
= atoi(buff
);
387 if(!kill(pidfromfile
, 0))
389 printf("ircd: daemon is already running\n");
402 * side effects - setups corefile to system limits.
408 #ifdef HAVE_SYS_RESOURCE_H
409 struct rlimit rlim
; /* resource limits */
411 /* Set corefilesize to maximum */
412 if(!getrlimit(RLIMIT_CORE
, &rlim
))
414 rlim
.rlim_cur
= rlim
.rlim_max
;
415 setrlimit(RLIMIT_CORE
, &rlim
);
420 struct ev_entry
*check_splitmode_ev
= NULL
;
425 * Initializes the IRCd.
427 * Inputs - number of commandline args, args themselves
429 * Side Effects - this is where the ircd gets going right now
432 main(int argc
, char *argv
[])
436 /* Check to see if the user is running us as root, which is a nono */
439 fprintf(stderr
, "Don't run ircd as root!!!\n");
444 * Setup corefile size immediately after boot -kre
449 /* It ain't random, but it ought to be a little harder to guess */
450 srand(SystemTime
.tv_sec
^ (SystemTime
.tv_usec
| (getpid() << 20)));
451 memset(&me
, 0, sizeof(me
));
452 memset(&meLocalUser
, 0, sizeof(meLocalUser
));
453 me
.localClient
= &meLocalUser
;
455 /* Make sure all lists are zeroed */
456 memset(&unknown_list
, 0, sizeof(unknown_list
));
457 memset(&lclient_list
, 0, sizeof(lclient_list
));
458 memset(&serv_list
, 0, sizeof(serv_list
));
459 memset(&global_serv_list
, 0, sizeof(global_serv_list
));
460 memset(&local_oper_list
, 0, sizeof(local_oper_list
));
461 memset(&oper_list
, 0, sizeof(oper_list
));
463 rb_dlinkAddTail(&me
, &me
.node
, &global_client_list
);
465 memset(&Count
, 0, sizeof(Count
));
466 memset(&ServerInfo
, 0, sizeof(ServerInfo
));
467 memset(&AdminInfo
, 0, sizeof(AdminInfo
));
468 memset(&ServerStats
, 0, sizeof(struct ServerStatistics
));
470 /* Initialise the channel capability usage counts... */
471 init_chcap_usage_counts();
473 ConfigFileEntry
.dpath
= DPATH
;
474 ConfigFileEntry
.configfile
= CPATH
; /* Server configuration file */
475 ConfigFileEntry
.klinefile
= KPATH
; /* Server kline file */
476 ConfigFileEntry
.dlinefile
= DLPATH
; /* dline file */
477 ConfigFileEntry
.xlinefile
= XPATH
;
478 ConfigFileEntry
.resvfile
= RESVPATH
;
479 ConfigFileEntry
.connect_timeout
= 30; /* Default to 30 */
481 umask(077); /* better safe than sorry --SRB */
483 parseargs(&argc
, &argv
, myopts
);
487 printf("ircd: version %s\n", ircd_version
);
491 if(chdir(ConfigFileEntry
.dpath
))
493 fprintf(stderr
, "Unable to chdir to %s: %s\n", ConfigFileEntry
.dpath
, strerror(errno
));
500 server_state_foreground
= 1;
504 server_state_foreground
= 1;
506 /* Make sure fd 0, 1 and 2 are in use -- jilles */
509 fd
= open("/dev/null", O_RDWR
);
510 } while (fd
< 2 && fd
!= -1);
516 /* Check if there is pidfile and daemon already running */
519 check_pidfile(pidFileName
);
521 if(!server_state_foreground
)
523 inotice("starting %s ...", ircd_version
);
526 /* Init the event subsystem */
528 rb_lib_init(ircd_log_cb
, ircd_restart_cb
, ircd_die_cb
, !server_state_foreground
, maxconnections
, DNODE_HEAP_SIZE
, FD_HEAP_SIZE
);
529 rb_linebuf_init(LINEBUF_HEAP_SIZE
);
536 clear_scache_hash_table(); /* server cache name table */
550 #ifndef STATIC_MODULES
551 load_core_modules(1);
553 init_auth(); /* Initialise the auth code */
554 init_resolver(); /* Needs to be setup before the io loop */
557 fprintf(stderr
, "\nBeginning config test\n");
558 read_conf_files(YES
); /* cold start init conf files */
560 #ifndef STATIC_MODULES
562 mod_add_path(MODULE_DIR
);
563 mod_add_path(MODULE_DIR
"/autoload");
568 initialize_server_capabs(); /* Set up default_server_capabs */
569 initialize_global_set_options();
571 if(ServerInfo
.name
== NULL
)
573 ierror("no server name specified in serverinfo block.");
576 strlcpy(me
.name
, ServerInfo
.name
, sizeof(me
.name
));
578 if(ServerInfo
.sid
[0] == '\0')
580 ierror("no server sid specified in serverinfo block.");
583 strcpy(me
.id
, ServerInfo
.sid
);
586 /* serverinfo{} description must exist. If not, error out. */
587 if(ServerInfo
.description
== NULL
)
589 ierror("no server description specified in serverinfo block.");
592 strlcpy(me
.info
, ServerInfo
.description
, sizeof(me
.info
));
594 if(ServerInfo
.ssl_cert
!= NULL
&& ServerInfo
.ssl_private_key
!= NULL
)
596 /* just do the rb_setup_ssl_server to validate the config */
597 if(!rb_setup_ssl_server(ServerInfo
.ssl_cert
, ServerInfo
.ssl_private_key
, ServerInfo
.ssl_dh_params
))
599 ilog(L_MAIN
, "WARNING: Unable to setup SSL.");
608 fprintf(stderr
, "\nConfig testing complete.\n");
610 return 0; /* Why? We want the launcher to exit out. */
617 startup_time
= rb_current_time();
618 add_to_client_hash(me
.name
, &me
);
619 add_to_id_hash(me
.id
, &me
);
620 me
.serv
->nameinfo
= scache_connect(me
.name
, me
.info
, 0);
622 rb_dlinkAddAlloc(&me
, &global_serv_list
);
624 construct_umodebuf();
627 write_pidfile(pidFileName
);
631 ilog(L_MAIN
, "Server Ready");
633 rb_event_addish("cleanup_glines", cleanup_glines
, NULL
, CLEANUP_GLINES_TIME
);
635 /* We want try_connections to be called as soon as possible now! -- adrian */
636 /* No, 'cause after a restart it would cause all sorts of nick collides */
637 /* um. by waiting even longer, that just means we have even *more*
638 * nick collisions. what a stupid idea. set an event for the IO loop --fl
640 rb_event_addish("try_connections", try_connections
, NULL
, STARTUP_CONNECTIONS_TIME
);
641 rb_event_addonce("try_connections_startup", try_connections
, NULL
, 0);
643 /* Setup the timeout check. I'll shift it later :) -- adrian */
644 rb_event_addish("rb_checktimeouts", rb_checktimeouts
, NULL
, 1);
646 rb_event_add("check_rehash", check_rehash
, NULL
, 1);
649 check_splitmode_ev
= rb_event_add("check_splitmode", check_splitmode
, NULL
, 2);
653 print_startup(getpid());