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"
71 * Try and find the correct name to use with getrlimit() for setting the max.
72 * number of files allowed to be open by this process.
74 int _charybdis_data_version
= CHARYBDIS_DV
;
76 extern int ServerRunning
;
77 extern struct LocalUser meLocalUser
;
82 /* /quote set variables */
83 struct SetOptions GlobalSetOptions
;
85 /* configuration set from ircd.conf */
86 struct config_file_entry ConfigFileEntry
;
87 /* server info set from ircd.conf */
88 struct server_info ServerInfo
;
89 /* admin info set from ircd.conf */
90 struct admin_info AdminInfo
;
93 struct ServerStatistics ServerStats
;
99 * print_startup - print startup information
102 print_startup(int pid
)
104 inotice("now running in %s mode from %s as pid %d ...",
105 !server_state_foreground
? "background" : "foreground",
106 ConfigFileEntry
.dpath
, pid
);
108 /* let the parent process know the initialization was successful
110 if (!server_state_foreground
)
115 open("/dev/null", O_RDWR
);
121 ircd_log_cb(const char *str
)
123 ilog(L_MAIN
, "%s", str
);
127 ircd_restart_cb(const char *str
)
133 * Why EXIT_FAILURE here?
134 * Because if ircd_die_cb() is called it's because of a fatal
135 * error inside libcharybdis, and we don't know how to handle the
136 * exception, so it is logical to return a FAILURE exit code here.
140 ircd_die_cb(const char *str
)
142 /* Try to get the message out to currently logged in operators. */
143 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
, "Server panic! %s", str
);
144 inotice("server panic: %s", str
);
153 * inputs - boot_daemon flag
155 * side effects - if boot_daemon flag is not set, don't daemonize
160 #if defined(RLIMIT_NOFILE) && defined(HAVE_SYS_RESOURCE_H)
163 if(!getrlimit(RLIMIT_NOFILE
, &limit
))
165 maxconnections
= limit
.rlim_cur
;
166 if(maxconnections
<= MAX_BUFFER
)
168 fprintf(stderr
, "ERROR: Shell FD limits are too low.\n");
169 fprintf(stderr
, "ERROR: ircd-ratbox reserves %d FDs, shell limits must be above this\n", MAX_BUFFER
);
174 #endif /* RLIMIT_FD_MAX */
175 maxconnections
= MAXCONNECTIONS
;
192 if((pid
= fork()) < 0)
200 /* Wait for initialization to finish, successfully or
201 * unsuccessfully. Until this point the child may still
202 * write to stdout/stderr.
204 if (read(pip
[0], &c
, 1) > 0)
219 static int printVersion
= 0;
221 struct lgetopt myopts
[] = {
222 {"dlinefile", &ConfigFileEntry
.dlinefile
,
223 STRING
, "File to use for dlines.conf"},
224 {"configfile", &ConfigFileEntry
.configfile
,
225 STRING
, "File to use for ircd.conf"},
226 {"klinefile", &ConfigFileEntry
.klinefile
,
227 STRING
, "File to use for kline.conf"},
228 {"xlinefile", &ConfigFileEntry
.xlinefile
,
229 STRING
, "File to use for xline.conf"},
230 {"resvfile", &ConfigFileEntry
.resvfile
,
231 STRING
, "File to use for resv.conf"},
232 {"logfile", &logFileName
,
233 STRING
, "File to use for ircd.log"},
234 {"pidfile", &pidFileName
,
235 STRING
, "File to use for process ID"},
236 {"foreground", &server_state_foreground
,
237 YESNO
, "Run in foreground (don't detach)"},
238 {"version", &printVersion
,
239 YESNO
, "Print version and exit"},
240 {"conftest", &testing_conf
,
241 YESNO
, "Test the configuration files and exit"},
242 {"help", NULL
, USAGE
, "Print this text"},
243 {NULL
, NULL
, STRING
, NULL
},
247 check_rehash(void *unused
)
250 * Check to see whether we have to rehash the configuration ..
266 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
267 "Got signal SIGUSR1, reloading ircd motd file");
268 free_cachefile(user_motd
);
269 user_motd
= cache_file(MPATH
, "ircd.motd", 0);
275 * initalialize_global_set_options
279 * side effects - This sets all global set options needed
282 initialize_global_set_options(void)
284 memset(&GlobalSetOptions
, 0, sizeof(GlobalSetOptions
));
285 /* memset( &ConfigFileEntry, 0, sizeof(ConfigFileEntry)); */
287 GlobalSetOptions
.maxclients
= ServerInfo
.default_max_clients
;
289 if(GlobalSetOptions
.maxclients
> (maxconnections
- MAX_BUFFER
))
290 GlobalSetOptions
.maxclients
= maxconnections
- MAX_BUFFER
;
292 GlobalSetOptions
.autoconn
= 1;
294 GlobalSetOptions
.spam_time
= MIN_JOIN_LEAVE_TIME
;
295 GlobalSetOptions
.spam_num
= MAX_JOIN_LEAVE_COUNT
;
297 if(ConfigFileEntry
.default_floodcount
)
298 GlobalSetOptions
.floodcount
= ConfigFileEntry
.default_floodcount
;
300 GlobalSetOptions
.floodcount
= 10;
302 split_servers
= ConfigChannel
.default_split_server_count
;
303 split_users
= ConfigChannel
.default_split_user_count
;
305 if(split_users
&& split_servers
306 && (ConfigChannel
.no_create_on_split
|| ConfigChannel
.no_join_on_split
))
312 GlobalSetOptions
.ident_timeout
= IDENT_TIMEOUT
;
314 strlcpy(GlobalSetOptions
.operstring
,
315 ConfigFileEntry
.default_operstring
,
316 sizeof(GlobalSetOptions
.operstring
));
317 strlcpy(GlobalSetOptions
.adminstring
,
318 ConfigFileEntry
.default_adminstring
,
319 sizeof(GlobalSetOptions
.adminstring
));
321 /* memset( &ConfigChannel, 0, sizeof(ConfigChannel)); */
323 /* End of global set options */
328 * initialize_server_capabs
334 initialize_server_capabs(void)
336 default_server_capabs
&= ~CAP_ZIP
;
343 * inputs - filename+path of pid file
345 * side effects - write the pid of the ircd to filename
348 write_pidfile(const char *filename
)
352 if((fb
= fopen(filename
, "w")))
354 unsigned int pid
= (unsigned int) getpid();
356 rb_snprintf(buff
, sizeof(buff
), "%u\n", pid
);
357 if((fputs(buff
, fb
) == -1))
359 ilog(L_MAIN
, "Error writing %u to pid file %s (%s)",
360 pid
, filename
, strerror(errno
));
367 ilog(L_MAIN
, "Error opening pid file %s", filename
);
374 * inputs - filename+path of pid file
376 * side effects - reads pid from pidfile and checks if ircd is in process
377 * list. if it is, gracefully exits
381 check_pidfile(const char *filename
)
387 /* Don't do logging here, since we don't have log() initialised */
388 if((fb
= fopen(filename
, "r")))
390 if(fgets(buff
, 20, fb
) != NULL
)
392 pidfromfile
= atoi(buff
);
393 if(!kill(pidfromfile
, 0))
395 printf("ircd: daemon is already running\n");
408 * side effects - setups corefile to system limits.
414 #ifdef HAVE_SYS_RESOURCE_H
415 struct rlimit rlim
; /* resource limits */
417 /* Set corefilesize to maximum */
418 if(!getrlimit(RLIMIT_CORE
, &rlim
))
420 rlim
.rlim_cur
= rlim
.rlim_max
;
421 setrlimit(RLIMIT_CORE
, &rlim
);
426 struct ev_entry
*check_splitmode_ev
= NULL
;
431 * Initializes the IRCd.
433 * Inputs - number of commandline args, args themselves
435 * Side Effects - this is where the ircd gets going right now
438 main(int argc
, char *argv
[])
442 /* Check to see if the user is running us as root, which is a nono */
445 fprintf(stderr
, "Don't run ircd as root!!!\n");
450 * Setup corefile size immediately after boot -kre
455 /* It ain't random, but it ought to be a little harder to guess */
456 srand(SystemTime
.tv_sec
^ (SystemTime
.tv_usec
| (getpid() << 20)));
457 memset(&me
, 0, sizeof(me
));
458 memset(&meLocalUser
, 0, sizeof(meLocalUser
));
459 me
.localClient
= &meLocalUser
;
461 /* Make sure all lists are zeroed */
462 memset(&unknown_list
, 0, sizeof(unknown_list
));
463 memset(&lclient_list
, 0, sizeof(lclient_list
));
464 memset(&serv_list
, 0, sizeof(serv_list
));
465 memset(&global_serv_list
, 0, sizeof(global_serv_list
));
466 memset(&local_oper_list
, 0, sizeof(local_oper_list
));
467 memset(&oper_list
, 0, sizeof(oper_list
));
469 rb_dlinkAddTail(&me
, &me
.node
, &global_client_list
);
471 memset(&Count
, 0, sizeof(Count
));
472 memset(&ServerInfo
, 0, sizeof(ServerInfo
));
473 memset(&AdminInfo
, 0, sizeof(AdminInfo
));
474 memset(&ServerStats
, 0, sizeof(struct ServerStatistics
));
476 /* Initialise the channel capability usage counts... */
477 init_chcap_usage_counts();
479 ConfigFileEntry
.dpath
= DPATH
;
480 ConfigFileEntry
.configfile
= CPATH
; /* Server configuration file */
481 ConfigFileEntry
.klinefile
= KPATH
; /* Server kline file */
482 ConfigFileEntry
.dlinefile
= DLPATH
; /* dline file */
483 ConfigFileEntry
.xlinefile
= XPATH
;
484 ConfigFileEntry
.resvfile
= RESVPATH
;
485 ConfigFileEntry
.connect_timeout
= 30; /* Default to 30 */
487 umask(077); /* better safe than sorry --SRB */
489 parseargs(&argc
, &argv
, myopts
);
493 printf("ircd: version %s\n", ircd_version
);
497 if(chdir(ConfigFileEntry
.dpath
))
499 fprintf(stderr
, "Unable to chdir to %s: %s\n", ConfigFileEntry
.dpath
, strerror(errno
));
506 server_state_foreground
= 1;
510 server_state_foreground
= 1;
512 /* Make sure fd 0, 1 and 2 are in use -- jilles */
515 fd
= open("/dev/null", O_RDWR
);
516 } while (fd
< 2 && fd
!= -1);
522 /* Check if there is pidfile and daemon already running */
525 check_pidfile(pidFileName
);
527 if(!server_state_foreground
)
529 inotice("starting %s ...", ircd_version
);
532 /* Init the event subsystem */
534 rb_lib_init(ircd_log_cb
, ircd_restart_cb
, ircd_die_cb
, !server_state_foreground
, maxconnections
, DNODE_HEAP_SIZE
, FD_HEAP_SIZE
);
535 rb_linebuf_init(LINEBUF_HEAP_SIZE
);
542 clear_scache_hash_table(); /* server cache name table */
556 #ifndef STATIC_MODULES
557 load_core_modules(1);
559 init_auth(); /* Initialise the auth code */
560 init_resolver(); /* Needs to be setup before the io loop */
563 fprintf(stderr
, "\nBeginning config test\n");
564 read_conf_files(YES
); /* cold start init conf files */
566 #ifndef STATIC_MODULES
568 mod_add_path(MODULE_DIR
);
569 mod_add_path(MODULE_DIR
"/autoload");
574 initialize_server_capabs(); /* Set up default_server_capabs */
575 initialize_global_set_options();
577 if(ServerInfo
.name
== NULL
)
579 ierror("no server name specified in serverinfo block.");
582 strlcpy(me
.name
, ServerInfo
.name
, sizeof(me
.name
));
584 if(ServerInfo
.sid
[0] == '\0')
586 ierror("no server sid specified in serverinfo block.");
589 strcpy(me
.id
, ServerInfo
.sid
);
592 /* serverinfo{} description must exist. If not, error out. */
593 if(ServerInfo
.description
== NULL
)
595 ierror("no server description specified in serverinfo block.");
598 strlcpy(me
.info
, ServerInfo
.description
, sizeof(me
.info
));
600 if(ServerInfo
.ssl_cert
!= NULL
&& ServerInfo
.ssl_private_key
!= NULL
)
602 /* just do the rb_setup_ssl_server to validate the config */
603 if(!rb_setup_ssl_server(ServerInfo
.ssl_cert
, ServerInfo
.ssl_private_key
, ServerInfo
.ssl_dh_params
))
605 ilog(L_MAIN
, "WARNING: Unable to setup SSL.");
614 fprintf(stderr
, "\nConfig testing complete.\n");
616 return 0; /* Why? We want the launcher to exit out. */
623 startup_time
= rb_current_time();
624 add_to_client_hash(me
.name
, &me
);
625 add_to_id_hash(me
.id
, &me
);
626 me
.serv
->nameinfo
= scache_connect(me
.name
, me
.info
, 0);
628 rb_dlinkAddAlloc(&me
, &global_serv_list
);
630 construct_umodebuf();
633 write_pidfile(pidFileName
);
637 ilog(L_MAIN
, "Server Ready");
639 rb_event_addish("cleanup_glines", cleanup_glines
, NULL
, CLEANUP_GLINES_TIME
);
641 /* We want try_connections to be called as soon as possible now! -- adrian */
642 /* No, 'cause after a restart it would cause all sorts of nick collides */
643 /* um. by waiting even longer, that just means we have even *more*
644 * nick collisions. what a stupid idea. set an event for the IO loop --fl
646 rb_event_addish("try_connections", try_connections
, NULL
, STARTUP_CONNECTIONS_TIME
);
647 rb_event_addonce("try_connections_startup", try_connections
, NULL
, 0);
649 /* Setup the timeout check. I'll shift it later :) -- adrian */
650 rb_event_addish("rb_checktimeouts", rb_checktimeouts
, NULL
, 1);
652 rb_event_add("check_rehash", check_rehash
, NULL
, 1);
655 check_splitmode_ev
= rb_event_add("check_splitmode", check_splitmode
, NULL
, 2);
659 print_startup(getpid());