2 * charybdis: 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-2008 ircd-ratbox development team
8 * Copyright (C) 2005-2008 charybdis development team
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
28 #include "ratbox_lib.h"
39 #include "ircd_signal.h"
40 #include "msg.h" /* msgtab */
49 #include "s_serv.h" /* try_connections */
54 #include "supported.h"
58 #include "ircd_getopt.h"
62 #include "s_newconf.h"
65 #include "patchlevel.h"
69 #include "privilege.h"
71 /* /quote set variables */
72 struct SetOptions GlobalSetOptions
;
74 /* configuration set from ircd.conf */
75 struct config_file_entry ConfigFileEntry
;
76 /* server info set from ircd.conf */
77 struct server_info ServerInfo
;
78 /* admin info set from ircd.conf */
79 struct admin_info AdminInfo
;
82 struct ServerStatistics ServerStats
;
85 struct Client me
; /* That's me */
86 struct LocalUser meLocalUser
; /* That's also part of me */
88 rb_dlink_list global_client_list
;
90 /* unknown/client pointer lists */
91 rb_dlink_list unknown_list
; /* unknown clients ON this server only */
92 rb_dlink_list lclient_list
; /* local clients only ON this server */
93 rb_dlink_list serv_list
; /* local servers to this server ONLY */
94 rb_dlink_list global_serv_list
; /* global servers on the network */
95 rb_dlink_list local_oper_list
; /* our opers, duplicated in lclient_list */
96 rb_dlink_list oper_list
; /* network opers */
98 const char *logFileName
= LPATH
;
99 const char *pidFileName
= PPATH
;
103 int dorehashbans
= 0;
105 int kline_queued
= 0;
106 int server_state_foreground
= 0;
107 int opers_see_all_users
= 0;
111 int testing_conf
= 0;
114 int default_server_capabs
= CAP_MASK
;
123 ircd_shutdown(const char *reason
)
125 struct Client
*target_p
;
128 RB_DLINK_FOREACH(ptr
, lclient_list
.head
)
130 target_p
= ptr
->data
;
132 sendto_one(target_p
, ":%s NOTICE %s :Server Terminating. %s",
133 me
.name
, target_p
->name
, reason
);
136 RB_DLINK_FOREACH(ptr
, serv_list
.head
)
138 target_p
= ptr
->data
;
140 sendto_one(target_p
, ":%s ERROR :Terminated by %s",
144 ilog(L_MAIN
, "Server Terminating. %s", reason
);
152 * print_startup - print startup information
155 print_startup(int pid
)
157 inotice("now running in %s mode from %s as pid %d ...",
158 !server_state_foreground
? "background" : "foreground",
159 ConfigFileEntry
.dpath
, pid
);
161 /* let the parent process know the initialization was successful
163 if (!server_state_foreground
)
168 open("/dev/null", O_RDWR
);
176 * inputs - boot_daemon flag
178 * side effects - if boot_daemon flag is not set, don't daemonize
183 #if defined(RLIMIT_NOFILE) && defined(HAVE_SYS_RESOURCE_H)
186 if(!getrlimit(RLIMIT_NOFILE
, &limit
))
188 maxconnections
= limit
.rlim_cur
;
189 if(maxconnections
<= MAX_BUFFER
)
191 fprintf(stderr
, "ERROR: Shell FD limits are too low.\n");
192 fprintf(stderr
, "ERROR: charybdis reserves %d FDs, shell limits must be above this\n", MAX_BUFFER
);
197 #endif /* RLIMIT_FD_MAX */
198 maxconnections
= MAXCONNECTIONS
;
215 if((pid
= fork()) < 0)
223 /* Wait for initialization to finish, successfully or
224 * unsuccessfully. Until this point the child may still
225 * write to stdout/stderr.
227 if (read(pip
[0], &c
, 1) > 0)
242 static int printVersion
= 0;
244 struct lgetopt myopts
[] = {
245 {"dlinefile", &ConfigFileEntry
.dlinefile
,
246 STRING
, "File to use for dlines.conf"},
247 {"configfile", &ConfigFileEntry
.configfile
,
248 STRING
, "File to use for ircd.conf"},
249 {"klinefile", &ConfigFileEntry
.klinefile
,
250 STRING
, "File to use for kline.conf"},
251 {"xlinefile", &ConfigFileEntry
.xlinefile
,
252 STRING
, "File to use for xline.conf"},
253 {"resvfile", &ConfigFileEntry
.resvfile
,
254 STRING
, "File to use for resv.conf"},
255 {"logfile", &logFileName
,
256 STRING
, "File to use for ircd.log"},
257 {"pidfile", &pidFileName
,
258 STRING
, "File to use for process ID"},
259 {"foreground", &server_state_foreground
,
260 YESNO
, "Run in foreground (don't detach)"},
261 {"version", &printVersion
,
262 YESNO
, "Print version and exit"},
263 {"conftest", &testing_conf
,
264 YESNO
, "Test the configuration files and exit"},
265 {"help", NULL
, USAGE
, "Print this text"},
266 {NULL
, NULL
, STRING
, NULL
},
270 check_rehash(void *unused
)
273 * Check to see whether we have to rehash the configuration ..
289 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
290 "Got signal SIGUSR1, reloading ircd motd file");
297 * initalialize_global_set_options
301 * side effects - This sets all global set options needed
304 initialize_global_set_options(void)
306 memset(&GlobalSetOptions
, 0, sizeof(GlobalSetOptions
));
307 /* memset( &ConfigFileEntry, 0, sizeof(ConfigFileEntry)); */
309 GlobalSetOptions
.maxclients
= ServerInfo
.default_max_clients
;
311 if(GlobalSetOptions
.maxclients
> (maxconnections
- MAX_BUFFER
) || (GlobalSetOptions
.maxclients
<= 0))
312 GlobalSetOptions
.maxclients
= maxconnections
- MAX_BUFFER
;
314 GlobalSetOptions
.autoconn
= 1;
316 GlobalSetOptions
.spam_time
= MIN_JOIN_LEAVE_TIME
;
317 GlobalSetOptions
.spam_num
= MAX_JOIN_LEAVE_COUNT
;
319 if(ConfigFileEntry
.default_floodcount
)
320 GlobalSetOptions
.floodcount
= ConfigFileEntry
.default_floodcount
;
322 GlobalSetOptions
.floodcount
= 10;
324 split_servers
= ConfigChannel
.default_split_server_count
;
325 split_users
= ConfigChannel
.default_split_user_count
;
327 if(split_users
&& split_servers
328 && (ConfigChannel
.no_create_on_split
|| ConfigChannel
.no_join_on_split
))
334 GlobalSetOptions
.ident_timeout
= IDENT_TIMEOUT
;
336 rb_strlcpy(GlobalSetOptions
.operstring
,
337 ConfigFileEntry
.default_operstring
,
338 sizeof(GlobalSetOptions
.operstring
));
339 rb_strlcpy(GlobalSetOptions
.adminstring
,
340 ConfigFileEntry
.default_adminstring
,
341 sizeof(GlobalSetOptions
.adminstring
));
343 /* memset( &ConfigChannel, 0, sizeof(ConfigChannel)); */
345 /* End of global set options */
350 * initialize_server_capabs
356 initialize_server_capabs(void)
358 default_server_capabs
&= ~CAP_ZIP
;
365 * inputs - filename+path of pid file
367 * side effects - write the pid of the ircd to filename
370 write_pidfile(const char *filename
)
374 if((fb
= fopen(filename
, "w")))
376 unsigned int pid
= (unsigned int) getpid();
378 rb_snprintf(buff
, sizeof(buff
), "%u\n", pid
);
379 if((fputs(buff
, fb
) == -1))
381 ilog(L_MAIN
, "Error writing %u to pid file %s (%s)",
382 pid
, filename
, strerror(errno
));
389 ilog(L_MAIN
, "Error opening pid file %s", filename
);
396 * inputs - filename+path of pid file
398 * side effects - reads pid from pidfile and checks if ircd is in process
399 * list. if it is, gracefully exits
403 check_pidfile(const char *filename
)
409 /* Don't do logging here, since we don't have log() initialised */
410 if((fb
= fopen(filename
, "r")))
412 if(fgets(buff
, 20, fb
) != NULL
)
414 pidfromfile
= atoi(buff
);
415 if(!kill(pidfromfile
, 0))
417 printf("ircd: daemon is already running\n");
430 * side effects - setups corefile to system limits.
436 #ifdef HAVE_SYS_RESOURCE_H
437 struct rlimit rlim
; /* resource limits */
439 /* Set corefilesize to maximum */
440 if(!getrlimit(RLIMIT_CORE
, &rlim
))
442 rlim
.rlim_cur
= rlim
.rlim_max
;
443 setrlimit(RLIMIT_CORE
, &rlim
);
449 ircd_log_cb(const char *str
)
451 ilog(L_MAIN
, "libratbox reports: %s", str
);
455 ircd_restart_cb(const char *str
)
457 inotice("libratbox has called the restart callback: %s", str
);
462 * Why EXIT_FAILURE here?
463 * Because if ircd_die_cb() is called it's because of a fatal
464 * error inside libcharybdis, and we don't know how to handle the
465 * exception, so it is logical to return a FAILURE exit code here.
469 ircd_die_cb(const char *str
)
473 /* Try to get the message out to currently logged in operators. */
474 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
, "libratbox has called the die callback..aborting: %s", str
);
475 inotice("libratbox has called the die callback..aborting: %s", str
);
478 inotice("libratbox has called the die callback..aborting");
484 struct ev_entry
*check_splitmode_ev
= NULL
;
487 seed_with_urandom(void)
492 fd
= open("/dev/urandom", O_RDONLY
);
495 if(read(fd
, &seed
, sizeof(seed
)) == sizeof(seed
))
506 seed_with_clock(void)
508 const struct timeval
*tv
;
510 tv
= rb_current_time_tv();
511 srand(tv
->tv_sec
^ (tv
->tv_usec
| (getpid() << 20)));
515 seed_random(void *unused
)
518 if(rb_get_random(&seed
, sizeof(seed
)) == -1)
520 if(!seed_with_urandom())
530 * Initializes the IRCd.
532 * Inputs - number of commandline args, args themselves
534 * Side Effects - this is where the ircd gets going right now
537 main(int argc
, char *argv
[])
541 /* Check to see if the user is running us as root, which is a nono */
544 fprintf(stderr
, "Don't run ircd as root!!!\n");
550 ConfigFileEntry
.dpath
= DPATH
;
551 ConfigFileEntry
.configfile
= CPATH
; /* Server configuration file */
552 ConfigFileEntry
.klinefile
= KPATH
; /* Server kline file */
553 ConfigFileEntry
.dlinefile
= DLPATH
; /* dline file */
554 ConfigFileEntry
.xlinefile
= XPATH
;
555 ConfigFileEntry
.resvfile
= RESVPATH
;
556 ConfigFileEntry
.connect_timeout
= 30; /* Default to 30 */
558 umask(077); /* better safe than sorry --SRB */
561 parseargs(&argc
, &argv
, myopts
);
563 if(chdir(ConfigFileEntry
.dpath
))
565 fprintf(stderr
, "Unable to chdir to %s: %s\n", ConfigFileEntry
.dpath
, strerror(errno
));
572 * Setup corefile size immediately after boot -kre
576 memset(&me
, 0, sizeof(me
));
577 memset(&meLocalUser
, 0, sizeof(meLocalUser
));
578 me
.localClient
= &meLocalUser
;
580 /* Make sure all lists are zeroed */
581 memset(&unknown_list
, 0, sizeof(unknown_list
));
582 memset(&lclient_list
, 0, sizeof(lclient_list
));
583 memset(&serv_list
, 0, sizeof(serv_list
));
584 memset(&global_serv_list
, 0, sizeof(global_serv_list
));
585 memset(&local_oper_list
, 0, sizeof(local_oper_list
));
586 memset(&oper_list
, 0, sizeof(oper_list
));
588 rb_dlinkAddTail(&me
, &me
.node
, &global_client_list
);
590 memset(&Count
, 0, sizeof(Count
));
591 memset(&ServerInfo
, 0, sizeof(ServerInfo
));
592 memset(&AdminInfo
, 0, sizeof(AdminInfo
));
593 memset(&ServerStats
, 0, sizeof(struct ServerStatistics
));
595 /* Initialise the channel capability usage counts... */
596 init_chcap_usage_counts();
600 printf("ircd: version %s(%s)\n", ircd_version
, serno
);
601 printf("ircd: %s\n", rb_lib_version());
610 server_state_foreground
= 1;
612 /* Make sure fd 0, 1 and 2 are in use -- jilles */
615 fd
= open("/dev/null", O_RDWR
);
616 } while (fd
< 2 && fd
!= -1);
622 /* Check if there is pidfile and daemon already running */
625 check_pidfile(pidFileName
);
627 if(!server_state_foreground
)
629 inotice("starting %s ...", ircd_version
);
630 inotice("%s", rb_lib_version());
633 /* Init the event subsystem */
634 rb_lib_init(ircd_log_cb
, ircd_restart_cb
, ircd_die_cb
, !server_state_foreground
, maxconnections
, DNODE_HEAP_SIZE
, FD_HEAP_SIZE
);
635 rb_linebuf_init(LINEBUF_HEAP_SIZE
);
637 if(ConfigFileEntry
.use_egd
&& (ConfigFileEntry
.egdpool_path
!= NULL
))
639 rb_init_prng(ConfigFileEntry
.egdpool_path
, RB_PRNG_EGD
);
641 rb_init_prng(NULL
, RB_PRNG_DEFAULT
);
650 clear_scache_hash_table(); /* server cache name table */
663 #ifndef STATIC_MODULES
664 load_core_modules(1);
666 init_auth(); /* Initialise the auth code */
667 init_resolver(); /* Needs to be setup before the io loop */
668 privilegeset_set_new("default", "", 0);
671 fprintf(stderr
, "\nBeginning config test\n");
672 read_conf_files(YES
); /* cold start init conf files */
674 #ifndef STATIC_MODULES
676 mod_add_path(MODULE_DIR
);
677 mod_add_path(MODULE_DIR
"/autoload");
682 initialize_server_capabs(); /* Set up default_server_capabs */
683 initialize_global_set_options();
685 if(ServerInfo
.name
== NULL
)
687 ierror("no server name specified in serverinfo block.");
690 rb_strlcpy(me
.name
, ServerInfo
.name
, sizeof(me
.name
));
692 if(ServerInfo
.sid
[0] == '\0')
694 ierror("no server sid specified in serverinfo block.");
697 strcpy(me
.id
, ServerInfo
.sid
);
700 /* serverinfo{} description must exist. If not, error out. */
701 if(ServerInfo
.description
== NULL
)
703 ierror("no server description specified in serverinfo block.");
706 rb_strlcpy(me
.info
, ServerInfo
.description
, sizeof(me
.info
));
708 if(ServerInfo
.ssl_cert
!= NULL
&& ServerInfo
.ssl_private_key
!= NULL
)
710 /* just do the rb_setup_ssl_server to validate the config */
711 if(!rb_setup_ssl_server(ServerInfo
.ssl_cert
, ServerInfo
.ssl_private_key
, ServerInfo
.ssl_dh_params
))
713 ilog(L_MAIN
, "WARNING: Unable to setup SSL.");
722 fprintf(stderr
, "\nConfig testing complete.\n");
724 return 0; /* Why? We want the launcher to exit out. */
731 startup_time
= rb_current_time();
732 add_to_client_hash(me
.name
, &me
);
733 add_to_id_hash(me
.id
, &me
);
734 me
.serv
->nameinfo
= scache_connect(me
.name
, me
.info
, 0);
736 rb_dlinkAddAlloc(&me
, &global_serv_list
);
738 construct_umodebuf();
739 construct_noparam_modes();
742 write_pidfile(pidFileName
);
746 ilog(L_MAIN
, "Server Ready");
748 /* We want try_connections to be called as soon as possible now! -- adrian */
749 /* No, 'cause after a restart it would cause all sorts of nick collides */
750 /* um. by waiting even longer, that just means we have even *more*
751 * nick collisions. what a stupid idea. set an event for the IO loop --fl
753 rb_event_addish("try_connections", try_connections
, NULL
, STARTUP_CONNECTIONS_TIME
);
754 rb_event_addonce("try_connections_startup", try_connections
, NULL
, 2);
755 rb_event_add("check_rehash", check_rehash
, NULL
, 3);
756 rb_event_addish("reseed_srand", seed_random
, NULL
, 300); /* reseed every 10 minutes */
759 check_splitmode_ev
= rb_event_add("check_splitmode", check_splitmode
, NULL
, 5);
761 print_startup(getpid());