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
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"
72 /* /quote set variables */
73 struct SetOptions GlobalSetOptions
;
75 /* configuration set from ircd.conf */
76 struct config_file_entry ConfigFileEntry
;
77 /* server info set from ircd.conf */
78 struct server_info ServerInfo
;
79 /* admin info set from ircd.conf */
80 struct admin_info AdminInfo
;
83 struct ServerStatistics ServerStats
;
86 struct Client me
; /* That's me */
87 struct LocalUser meLocalUser
; /* That's also part of me */
89 rb_dlink_list global_client_list
;
91 /* unknown/client pointer lists */
92 rb_dlink_list unknown_list
; /* unknown clients ON this server only */
93 rb_dlink_list lclient_list
; /* local clients only ON this server */
94 rb_dlink_list serv_list
; /* local servers to this server ONLY */
95 rb_dlink_list global_serv_list
; /* global servers on the network */
96 rb_dlink_list local_oper_list
; /* our opers, duplicated in lclient_list */
97 rb_dlink_list oper_list
; /* network opers */
99 const char *logFileName
= LPATH
;
100 const char *pidFileName
= PPATH
;
104 int dorehashbans
= 0;
106 int kline_queued
= 0;
107 int server_state_foreground
= 0;
108 int opers_see_all_users
= 0;
112 int testing_conf
= 0;
115 int default_server_capabs
;
124 ircd_shutdown(const char *reason
)
126 struct Client
*target_p
;
129 RB_DLINK_FOREACH(ptr
, lclient_list
.head
)
131 target_p
= ptr
->data
;
133 sendto_one(target_p
, ":%s NOTICE %s :Server Terminating. %s",
134 me
.name
, target_p
->name
, reason
);
137 RB_DLINK_FOREACH(ptr
, serv_list
.head
)
139 target_p
= ptr
->data
;
141 sendto_one(target_p
, ":%s ERROR :Terminated by %s",
145 ilog(L_MAIN
, "Server Terminating. %s", reason
);
153 * print_startup - print startup information
156 print_startup(int pid
)
158 inotice("now running in %s mode from %s as pid %d ...",
159 !server_state_foreground
? "background" : "foreground",
160 ConfigFileEntry
.dpath
, pid
);
162 /* let the parent process know the initialization was successful
164 if (!server_state_foreground
)
169 open("/dev/null", O_RDWR
);
177 * inputs - boot_daemon flag
179 * side effects - if boot_daemon flag is not set, don't daemonize
184 #if defined(RLIMIT_NOFILE) && defined(HAVE_SYS_RESOURCE_H)
187 if(!getrlimit(RLIMIT_NOFILE
, &limit
))
189 maxconnections
= limit
.rlim_cur
;
190 if(maxconnections
<= MAX_BUFFER
)
192 fprintf(stderr
, "ERROR: Shell FD limits are too low.\n");
193 fprintf(stderr
, "ERROR: charybdis reserves %d FDs, shell limits must be above this\n", MAX_BUFFER
);
198 #endif /* RLIMIT_FD_MAX */
199 maxconnections
= MAXCONNECTIONS
;
216 if((pid
= fork()) < 0)
224 /* Wait for initialization to finish, successfully or
225 * unsuccessfully. Until this point the child may still
226 * write to stdout/stderr.
228 if (read(pip
[0], &c
, 1) > 0)
243 static int printVersion
= 0;
245 struct lgetopt myopts
[] = {
246 {"configfile", &ConfigFileEntry
.configfile
,
247 STRING
, "File to use for ircd.conf"},
248 {"logfile", &logFileName
,
249 STRING
, "File to use for ircd.log"},
250 {"pidfile", &pidFileName
,
251 STRING
, "File to use for process ID"},
252 {"foreground", &server_state_foreground
,
253 YESNO
, "Run in foreground (don't detach)"},
254 {"version", &printVersion
,
255 YESNO
, "Print version and exit"},
256 {"conftest", &testing_conf
,
257 YESNO
, "Test the configuration files and exit"},
258 {"help", NULL
, USAGE
, "Print this text"},
259 {NULL
, NULL
, STRING
, NULL
},
263 check_rehash(void *unused
)
266 * Check to see whether we have to rehash the configuration ..
282 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
283 "Got signal SIGUSR1, reloading ircd motd file");
290 * initalialize_global_set_options
294 * side effects - This sets all global set options needed
297 initialize_global_set_options(void)
299 memset(&GlobalSetOptions
, 0, sizeof(GlobalSetOptions
));
300 /* memset( &ConfigFileEntry, 0, sizeof(ConfigFileEntry)); */
302 GlobalSetOptions
.maxclients
= ServerInfo
.default_max_clients
;
304 if(GlobalSetOptions
.maxclients
> (maxconnections
- MAX_BUFFER
) || (GlobalSetOptions
.maxclients
<= 0))
305 GlobalSetOptions
.maxclients
= maxconnections
- MAX_BUFFER
;
307 GlobalSetOptions
.autoconn
= 1;
309 GlobalSetOptions
.spam_time
= MIN_JOIN_LEAVE_TIME
;
310 GlobalSetOptions
.spam_num
= MAX_JOIN_LEAVE_COUNT
;
312 if(ConfigFileEntry
.default_floodcount
)
313 GlobalSetOptions
.floodcount
= ConfigFileEntry
.default_floodcount
;
315 GlobalSetOptions
.floodcount
= 10;
317 split_servers
= ConfigChannel
.default_split_server_count
;
318 split_users
= ConfigChannel
.default_split_user_count
;
320 if(split_users
&& split_servers
321 && (ConfigChannel
.no_create_on_split
|| ConfigChannel
.no_join_on_split
))
327 if(ConfigFileEntry
.default_ident_timeout
)
328 GlobalSetOptions
.ident_timeout
= ConfigFileEntry
.default_ident_timeout
;
330 GlobalSetOptions
.ident_timeout
= IDENT_TIMEOUT
;
332 rb_strlcpy(GlobalSetOptions
.operstring
,
333 ConfigFileEntry
.default_operstring
,
334 sizeof(GlobalSetOptions
.operstring
));
335 rb_strlcpy(GlobalSetOptions
.adminstring
,
336 ConfigFileEntry
.default_adminstring
,
337 sizeof(GlobalSetOptions
.adminstring
));
339 /* memset( &ConfigChannel, 0, sizeof(ConfigChannel)); */
341 /* End of global set options */
346 * initialize_server_capabs
352 initialize_server_capabs(void)
354 default_server_capabs
&= ~CAP_ZIP
;
361 * inputs - filename+path of pid file
363 * side effects - write the pid of the ircd to filename
366 write_pidfile(const char *filename
)
370 if((fb
= fopen(filename
, "w")))
372 unsigned int pid
= (unsigned int) getpid();
374 rb_snprintf(buff
, sizeof(buff
), "%u\n", pid
);
375 if((fputs(buff
, fb
) == -1))
377 ilog(L_MAIN
, "Error writing %u to pid file %s (%s)",
378 pid
, filename
, strerror(errno
));
385 ilog(L_MAIN
, "Error opening pid file %s", filename
);
392 * inputs - filename+path of pid file
394 * side effects - reads pid from pidfile and checks if ircd is in process
395 * list. if it is, gracefully exits
399 check_pidfile(const char *filename
)
405 /* Don't do logging here, since we don't have log() initialised */
406 if((fb
= fopen(filename
, "r")))
408 if(fgets(buff
, 20, fb
) != NULL
)
410 pidfromfile
= atoi(buff
);
411 if(!kill(pidfromfile
, 0))
413 printf("ircd: daemon is already running\n");
426 * side effects - setups corefile to system limits.
432 #ifdef HAVE_SYS_RESOURCE_H
433 struct rlimit rlim
; /* resource limits */
435 /* Set corefilesize to maximum */
436 if(!getrlimit(RLIMIT_CORE
, &rlim
))
438 rlim
.rlim_cur
= rlim
.rlim_max
;
439 setrlimit(RLIMIT_CORE
, &rlim
);
445 ircd_log_cb(const char *str
)
447 ilog(L_MAIN
, "libratbox reports: %s", str
);
451 ircd_restart_cb(const char *str
)
453 inotice("libratbox has called the restart callback: %s", str
);
458 * Why EXIT_FAILURE here?
459 * Because if ircd_die_cb() is called it's because of a fatal
460 * error inside libcharybdis, and we don't know how to handle the
461 * exception, so it is logical to return a FAILURE exit code here.
465 ircd_die_cb(const char *str
)
469 /* Try to get the message out to currently logged in operators. */
470 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
, "libratbox has called the die callback..aborting: %s", str
);
471 inotice("libratbox has called the die callback..aborting: %s", str
);
474 inotice("libratbox has called the die callback..aborting");
480 struct ev_entry
*check_splitmode_ev
= NULL
;
483 seed_with_urandom(void)
488 fd
= open("/dev/urandom", O_RDONLY
);
491 if(read(fd
, &seed
, sizeof(seed
)) == sizeof(seed
))
502 seed_with_clock(void)
504 const struct timeval
*tv
;
506 tv
= rb_current_time_tv();
507 srand(tv
->tv_sec
^ (tv
->tv_usec
| (getpid() << 20)));
511 seed_random(void *unused
)
514 if(rb_get_random(&seed
, sizeof(seed
)) == -1)
516 if(!seed_with_urandom())
526 * Initializes the IRCd.
528 * Inputs - number of commandline args, args themselves
530 * Side Effects - this is where the ircd gets going right now
533 main(int argc
, char *argv
[])
537 /* Check to see if the user is running us as root, which is a nono */
540 fprintf(stderr
, "Don't run ircd as root!!!\n");
546 ConfigFileEntry
.dpath
= DPATH
;
547 ConfigFileEntry
.configfile
= CPATH
; /* Server configuration file */
548 ConfigFileEntry
.connect_timeout
= 30; /* Default to 30 */
550 umask(077); /* better safe than sorry --SRB */
553 parseargs(&argc
, &argv
, myopts
);
555 if(chdir(ConfigFileEntry
.dpath
))
557 fprintf(stderr
, "Unable to chdir to %s: %s\n", ConfigFileEntry
.dpath
, strerror(errno
));
564 * Setup corefile size immediately after boot -kre
568 memset(&me
, 0, sizeof(me
));
569 memset(&meLocalUser
, 0, sizeof(meLocalUser
));
570 me
.localClient
= &meLocalUser
;
572 /* Make sure all lists are zeroed */
573 memset(&unknown_list
, 0, sizeof(unknown_list
));
574 memset(&lclient_list
, 0, sizeof(lclient_list
));
575 memset(&serv_list
, 0, sizeof(serv_list
));
576 memset(&global_serv_list
, 0, sizeof(global_serv_list
));
577 memset(&local_oper_list
, 0, sizeof(local_oper_list
));
578 memset(&oper_list
, 0, sizeof(oper_list
));
580 rb_dlinkAddTail(&me
, &me
.node
, &global_client_list
);
582 memset(&Count
, 0, sizeof(Count
));
583 memset(&ServerInfo
, 0, sizeof(ServerInfo
));
584 memset(&AdminInfo
, 0, sizeof(AdminInfo
));
585 memset(&ServerStats
, 0, sizeof(struct ServerStatistics
));
589 printf("ircd: version %s(%s)\n", ircd_version
, serno
);
590 #ifdef CUSTOM_BRANDING
591 printf("ircd: based on %s-%s\n", PACKAGE_NAME
, PACKAGE_VERSION
);
593 printf("ircd: %s\n", rb_lib_version());
600 server_state_foreground
= 1;
602 /* Make sure fd 0, 1 and 2 are in use -- jilles */
605 fd
= open("/dev/null", O_RDWR
);
606 } while (fd
< 2 && fd
!= -1);
612 /* Check if there is pidfile and daemon already running */
615 check_pidfile(pidFileName
);
617 if(!server_state_foreground
)
619 inotice("starting %s ...", ircd_version
);
620 inotice("%s", rb_lib_version());
623 /* Init the event subsystem */
624 rb_lib_init(ircd_log_cb
, ircd_restart_cb
, ircd_die_cb
, !server_state_foreground
, maxconnections
, DNODE_HEAP_SIZE
, FD_HEAP_SIZE
);
625 rb_linebuf_init(LINEBUF_HEAP_SIZE
);
627 if(ConfigFileEntry
.use_egd
&& (ConfigFileEntry
.egdpool_path
!= NULL
))
629 rb_init_prng(ConfigFileEntry
.egdpool_path
, RB_PRNG_EGD
);
631 rb_init_prng(NULL
, RB_PRNG_DEFAULT
);
635 init_builtin_capabs();
636 default_server_capabs
= CAP_MASK
;
643 clear_scache_hash_table(); /* server cache name table */
655 construct_cflags_strings();
658 #ifndef STATIC_MODULES
659 load_core_modules(1);
661 init_auth(); /* Initialise the auth code */
662 init_resolver(); /* Needs to be setup before the io loop */
663 privilegeset_set_new("default", "", 0);
666 fprintf(stderr
, "\nBeginning config test\n");
667 read_conf_files(YES
); /* cold start init conf files */
668 #ifndef STATIC_MODULES
670 mod_add_path(MODULE_DIR
);
671 mod_add_path(MODULE_DIR
"/autoload");
681 initialize_server_capabs(); /* Set up default_server_capabs */
682 initialize_global_set_options();
684 if(ServerInfo
.name
== NULL
)
686 ierror("no server name specified in serverinfo block.");
689 rb_strlcpy(me
.name
, ServerInfo
.name
, sizeof(me
.name
));
691 if(ServerInfo
.sid
[0] == '\0')
693 ierror("no server sid specified in serverinfo block.");
696 strcpy(me
.id
, ServerInfo
.sid
);
699 /* serverinfo{} description must exist. If not, error out. */
700 if(ServerInfo
.description
== NULL
)
702 ierror("no server description specified in serverinfo block.");
705 rb_strlcpy(me
.info
, ServerInfo
.description
, sizeof(me
.info
));
707 if(ServerInfo
.ssl_cert
!= NULL
&& ServerInfo
.ssl_private_key
!= NULL
)
709 /* just do the rb_setup_ssl_server to validate the config */
710 if(!rb_setup_ssl_server(ServerInfo
.ssl_cert
, ServerInfo
.ssl_private_key
, ServerInfo
.ssl_dh_params
))
712 ilog(L_MAIN
, "WARNING: Unable to setup SSL.");
721 fprintf(stderr
, "\nConfig testing complete.\n");
723 return 0; /* Why? We want the launcher to exit out. */
730 startup_time
= rb_current_time();
731 add_to_client_hash(me
.name
, &me
);
732 add_to_id_hash(me
.id
, &me
);
733 me
.serv
->nameinfo
= scache_connect(me
.name
, me
.info
, 0);
735 rb_dlinkAddAlloc(&me
, &global_serv_list
);
737 construct_umodebuf();
740 write_pidfile(pidFileName
);
744 ilog(L_MAIN
, "Server Ready");
746 /* We want try_connections to be called as soon as possible now! -- adrian */
747 /* No, 'cause after a restart it would cause all sorts of nick collides */
748 /* um. by waiting even longer, that just means we have even *more*
749 * nick collisions. what a stupid idea. set an event for the IO loop --fl
751 rb_event_addish("try_connections", try_connections
, NULL
, STARTUP_CONNECTIONS_TIME
);
752 rb_event_addonce("try_connections_startup", try_connections
, NULL
, 2);
753 rb_event_add("check_rehash", check_rehash
, NULL
, 3);
754 rb_event_addish("reseed_srand", seed_random
, NULL
, 300); /* reseed every 10 minutes */
757 check_splitmode_ev
= rb_event_add("check_splitmode", check_splitmode
, NULL
, 5);
759 print_startup(getpid());