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-2013 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
37 #include "ircd_signal.h"
38 #include "msg.h" /* msgtab */
46 #include "s_serv.h" /* try_connections */
51 #include "supported.h"
55 #include "ircd_getopt.h"
59 #include "s_newconf.h"
62 #include "patchlevel.h"
66 #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
;
102 bool dorehash
= false;
103 bool dorehashbans
= false;
104 bool doremotd
= false;
105 bool kline_queued
= false;
106 bool server_state_foreground
= false;
107 bool opers_see_all_users
= false;
111 int testing_conf
= 0;
114 int default_server_capabs
;
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
)
160 fd
= open("/dev/null", O_RDWR
);
162 perror("open /dev/null");
170 inotice("now running in %s mode from %s as pid %d ...",
171 !server_state_foreground
? "background" : "foreground",
172 ConfigFileEntry
.dpath
, pid
);
174 /* let the parent process know the initialization was successful
176 if (!server_state_foreground
)
178 if (dup2(1, 0) == -1)
180 if (dup2(1, 2) == -1)
187 * inputs - boot_daemon flag
189 * side effects - if boot_daemon flag is not set, don't daemonize
194 #if defined(RLIMIT_NOFILE) && defined(HAVE_SYS_RESOURCE_H)
197 if(!getrlimit(RLIMIT_NOFILE
, &limit
))
199 maxconnections
= limit
.rlim_cur
;
200 if(maxconnections
<= MAX_BUFFER
)
202 fprintf(stderr
, "ERROR: Shell FD limits are too low.\n");
203 fprintf(stderr
, "ERROR: charybdis reserves %d FDs, shell limits must be above this\n", MAX_BUFFER
);
208 #endif /* RLIMIT_FD_MAX */
209 maxconnections
= MAXCONNECTIONS
;
226 if((pid
= fork()) < 0)
234 /* Wait for initialization to finish, successfully or
235 * unsuccessfully. Until this point the child may still
236 * write to stdout/stderr.
238 if (read(pip
[0], &c
, 1) > 0)
253 static int printVersion
= 0;
255 struct lgetopt myopts
[] = {
256 {"configfile", &ConfigFileEntry
.configfile
,
257 STRING
, "File to use for ircd.conf"},
258 {"logfile", &logFileName
,
259 STRING
, "File to use for ircd.log"},
260 {"pidfile", &pidFileName
,
261 STRING
, "File to use for process ID"},
262 {"foreground", &server_state_foreground
,
263 YESNO
, "Run in foreground (don't detach)"},
264 {"version", &printVersion
,
265 YESNO
, "Print version and exit"},
266 {"conftest", &testing_conf
,
267 YESNO
, "Test the configuration files and exit"},
268 {"help", NULL
, USAGE
, "Print this text"},
269 {NULL
, NULL
, STRING
, NULL
},
273 check_rehash(void *unused
)
276 * Check to see whether we have to rehash the configuration ..
287 dorehashbans
= false;
292 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
293 "Got signal SIGUSR1, reloading ircd motd file");
300 * initalialize_global_set_options
304 * side effects - This sets all global set options needed
307 initialize_global_set_options(void)
309 memset(&GlobalSetOptions
, 0, sizeof(GlobalSetOptions
));
310 /* memset( &ConfigFileEntry, 0, sizeof(ConfigFileEntry)); */
312 GlobalSetOptions
.maxclients
= ServerInfo
.default_max_clients
;
314 if(GlobalSetOptions
.maxclients
> (maxconnections
- MAX_BUFFER
) || (GlobalSetOptions
.maxclients
<= 0))
315 GlobalSetOptions
.maxclients
= maxconnections
- MAX_BUFFER
;
317 GlobalSetOptions
.autoconn
= 1;
319 GlobalSetOptions
.spam_time
= MIN_JOIN_LEAVE_TIME
;
320 GlobalSetOptions
.spam_num
= MAX_JOIN_LEAVE_COUNT
;
322 GlobalSetOptions
.floodcount
= ConfigFileEntry
.default_floodcount
;
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
= ConfigFileEntry
.default_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 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
, "librb reports: %s", str
);
455 ircd_restart_cb(const char *str
)
457 inotice("librb 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
, "librb has called the die callback..aborting: %s", str
);
475 inotice("librb has called the die callback..aborting: %s", str
);
478 inotice("librb 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
))
507 seed_with_clock(void)
509 const struct timeval
*tv
;
511 tv
= rb_current_time_tv();
512 srand(tv
->tv_sec
^ (tv
->tv_usec
| (getpid() << 20)));
516 seed_random(void *unused
)
519 if(rb_get_random(&seed
, sizeof(seed
)) == -1)
521 if(!seed_with_urandom())
531 * Initializes the IRCd.
533 * Inputs - number of commandline args, args themselves
535 * Side Effects - this is where the ircd gets going right now
538 charybdis_main(int argc
, char *argv
[])
542 /* Check to see if the user is running us as root, which is a nono */
545 fprintf(stderr
, "Don't run ircd as root!!!\n");
551 ConfigFileEntry
.dpath
= DPATH
;
552 ConfigFileEntry
.configfile
= CPATH
; /* Server configuration file */
553 ConfigFileEntry
.connect_timeout
= 30; /* Default to 30 */
555 umask(077); /* better safe than sorry --SRB */
558 parseargs(&argc
, &argv
, myopts
);
560 if(chdir(ConfigFileEntry
.dpath
))
562 fprintf(stderr
, "Unable to chdir to %s: %s\n", ConfigFileEntry
.dpath
, strerror(errno
));
569 * Setup corefile size immediately after boot -kre
573 /* initialise operhash fairly early. */
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
));
597 printf("ircd: version %s(%s)\n", ircd_version
, serno
);
598 #ifdef CUSTOM_BRANDING
599 printf("ircd: based on %s-%s\n", PACKAGE_NAME
, PACKAGE_VERSION
);
601 printf("ircd: %s\n", rb_lib_version());
608 server_state_foreground
= true;
610 /* Make sure fd 0, 1 and 2 are in use -- jilles */
613 fd
= open("/dev/null", O_RDWR
);
614 } while (fd
< 2 && fd
!= -1);
620 /* Check if there is pidfile and daemon already running */
623 check_pidfile(pidFileName
);
625 if(!server_state_foreground
)
627 inotice("starting %s ...", ircd_version
);
628 inotice("%s", rb_lib_version());
631 /* Init the event subsystem */
632 rb_lib_init(ircd_log_cb
, ircd_restart_cb
, ircd_die_cb
, !server_state_foreground
, maxconnections
, DNODE_HEAP_SIZE
, FD_HEAP_SIZE
);
633 rb_linebuf_init(LINEBUF_HEAP_SIZE
);
635 rb_init_prng(NULL
, RB_PRNG_DEFAULT
);
639 init_builtin_capabs();
640 default_server_capabs
= CAP_MASK
;
647 clear_scache_hash_table(); /* server cache name table */
659 construct_cflags_strings();
662 load_core_modules(1);
664 init_auth(); /* Initialise the auth code */
665 init_authd(); /* Start up authd. */
666 init_nameserver_cache(); /* Get our nameserver cache for STATS a/A */
668 privilegeset_set_new("default", "", 0);
671 fprintf(stderr
, "\nBeginning config test\n");
672 read_conf_files(true); /* cold start init conf files */
674 mod_add_path(MODULE_DIR
);
675 mod_add_path(MODULE_DIR
"/autoload");
684 initialize_server_capabs(); /* Set up default_server_capabs */
685 initialize_global_set_options();
687 if(ServerInfo
.name
== NULL
)
689 ierror("no server name specified in serverinfo block.");
692 rb_strlcpy(me
.name
, ServerInfo
.name
, sizeof(me
.name
));
694 if(ServerInfo
.sid
[0] == '\0')
696 ierror("no server sid specified in serverinfo block.");
699 strcpy(me
.id
, ServerInfo
.sid
);
702 /* serverinfo{} description must exist. If not, error out. */
703 if(ServerInfo
.description
== NULL
)
705 ierror("no server description specified in serverinfo block.");
708 rb_strlcpy(me
.info
, ServerInfo
.description
, sizeof(me
.info
));
710 if(ServerInfo
.ssl_cert
!= NULL
&& ServerInfo
.ssl_private_key
!= NULL
)
712 /* just do the rb_setup_ssl_server to validate the config */
713 if(!rb_setup_ssl_server(ServerInfo
.ssl_cert
, ServerInfo
.ssl_private_key
, ServerInfo
.ssl_dh_params
, ServerInfo
.ssl_cipher_list
))
715 ilog(L_MAIN
, "WARNING: Unable to setup SSL.");
724 fprintf(stderr
, "\nConfig testing complete.\n");
726 return 0; /* Why? We want the launcher to exit out. */
733 startup_time
= rb_current_time();
734 add_to_client_hash(me
.name
, &me
);
735 add_to_id_hash(me
.id
, &me
);
736 me
.serv
->nameinfo
= scache_connect(me
.name
, me
.info
, 0);
738 rb_dlinkAddAlloc(&me
, &global_serv_list
);
740 construct_umodebuf();
743 write_pidfile(pidFileName
);
747 ilog(L_MAIN
, "Server Ready");
749 /* We want try_connections to be called as soon as possible now! -- adrian */
750 /* No, 'cause after a restart it would cause all sorts of nick collides */
751 /* um. by waiting even longer, that just means we have even *more*
752 * nick collisions. what a stupid idea. set an event for the IO loop --fl
754 rb_event_addish("try_connections", try_connections
, NULL
, STARTUP_CONNECTIONS_TIME
);
755 rb_event_addonce("try_connections_startup", try_connections
, NULL
, 2);
756 rb_event_add("check_rehash", check_rehash
, NULL
, 3);
757 rb_event_addish("reseed_srand", seed_random
, NULL
, 300); /* reseed every 10 minutes */
760 check_splitmode_ev
= rb_event_add("check_splitmode", check_splitmode
, NULL
, 5);
762 print_startup(getpid());