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
36 #include "ircd_signal.h"
37 #include "msg.h" /* msgtab */
45 #include "s_serv.h" /* try_connections */
50 #include "supported.h"
54 #include "ircd_getopt.h"
58 #include "s_newconf.h"
61 #include "patchlevel.h"
65 #include "privilege.h"
70 /* /quote set variables */
71 struct SetOptions GlobalSetOptions
;
73 /* configuration set from ircd.conf */
74 struct config_file_entry ConfigFileEntry
;
75 /* server info set from ircd.conf */
76 struct server_info ServerInfo
;
77 /* admin info set from ircd.conf */
78 struct admin_info AdminInfo
;
81 struct ServerStatistics ServerStats
;
84 struct Client me
; /* That's me */
85 struct LocalUser meLocalUser
; /* That's also part of me */
87 rb_dlink_list global_client_list
;
89 /* unknown/client pointer lists */
90 rb_dlink_list unknown_list
; /* unknown clients ON this server only */
91 rb_dlink_list lclient_list
; /* local clients only ON this server */
92 rb_dlink_list serv_list
; /* local servers to this server ONLY */
93 rb_dlink_list global_serv_list
; /* global servers on the network */
94 rb_dlink_list local_oper_list
; /* our opers, duplicated in lclient_list */
95 rb_dlink_list oper_list
; /* network opers */
97 const char *logFileName
= LPATH
;
98 const char *pidFileName
= PPATH
;
101 bool dorehash
= false;
102 bool dorehashbans
= false;
103 bool doremotd
= false;
104 bool kline_queued
= false;
105 bool server_state_foreground
= false;
106 bool opers_see_all_users
= false;
107 bool ircd_ssl_ok
= false;
108 bool ircd_zlib_ok
= true;
110 int testing_conf
= 0;
113 int default_server_capabs
;
122 ircd_shutdown(const char *reason
)
124 struct Client
*target_p
;
127 RB_DLINK_FOREACH(ptr
, lclient_list
.head
)
129 target_p
= ptr
->data
;
131 sendto_one(target_p
, ":%s NOTICE %s :Server Terminating. %s",
132 me
.name
, target_p
->name
, reason
);
135 RB_DLINK_FOREACH(ptr
, serv_list
.head
)
137 target_p
= ptr
->data
;
139 sendto_one(target_p
, ":%s ERROR :Terminated by %s",
143 ilog(L_MAIN
, "Server Terminating. %s", reason
);
151 * print_startup - print startup information
154 print_startup(int pid
)
160 fd
= open("/dev/null", O_RDWR
);
162 perror("open /dev/null");
170 inotice("runtime path: %s", rb_path_to_self());
171 inotice("now running in %s mode from %s as pid %d ...",
172 !server_state_foreground
? "background" : "foreground",
173 ConfigFileEntry
.dpath
, pid
);
176 /* let the parent process know the initialization was successful
178 if (!server_state_foreground
)
180 /* GCC complains on Linux if we don't check the value of write pedantically.
181 * Technically you're supposed to check the value, yes, but it probably can't fail.
182 * No, casting to void is of no use to shut the warning up. You HAVE to use the value.
185 if(write(0, ".", 1) < 1)
188 if (dup2(1, 0) == -1)
190 if (dup2(1, 2) == -1)
198 * inputs - boot_daemon flag
200 * side effects - if boot_daemon flag is not set, don't daemonize
205 #if !defined(_WIN32) && defined(RLIMIT_NOFILE) && defined(HAVE_SYS_RESOURCE_H)
208 if(!getrlimit(RLIMIT_NOFILE
, &limit
))
210 maxconnections
= limit
.rlim_cur
;
211 if(maxconnections
<= MAX_BUFFER
)
213 fprintf(stderr
, "ERROR: Shell FD limits are too low.\n");
214 fprintf(stderr
, "ERROR: charybdis reserves %d FDs, shell limits must be above this\n", MAX_BUFFER
);
219 #endif /* RLIMIT_FD_MAX */
220 maxconnections
= MAXCONNECTIONS
;
238 if((pid
= fork()) < 0)
246 /* Wait for initialization to finish, successfully or
247 * unsuccessfully. Until this point the child may still
248 * write to stdout/stderr.
250 if (read(pip
[0], &c
, 1) > 0)
265 static int printVersion
= 0;
267 struct lgetopt myopts
[] = {
268 {"configfile", &ConfigFileEntry
.configfile
,
269 STRING
, "File to use for ircd.conf"},
270 {"logfile", &logFileName
,
271 STRING
, "File to use for ircd.log"},
272 {"pidfile", &pidFileName
,
273 STRING
, "File to use for process ID"},
274 {"foreground", &server_state_foreground
,
275 YESNO
, "Run in foreground (don't detach)"},
276 {"version", &printVersion
,
277 YESNO
, "Print version and exit"},
278 {"conftest", &testing_conf
,
279 YESNO
, "Test the configuration files and exit"},
280 {"help", NULL
, USAGE
, "Print this text"},
281 {NULL
, NULL
, STRING
, NULL
},
285 check_rehash(void *unused
)
288 * Check to see whether we have to rehash the configuration ..
299 dorehashbans
= false;
304 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
305 "Got signal SIGUSR1, reloading ircd motd file");
312 * initalialize_global_set_options
316 * side effects - This sets all global set options needed
319 initialize_global_set_options(void)
321 memset(&GlobalSetOptions
, 0, sizeof(GlobalSetOptions
));
322 /* memset( &ConfigFileEntry, 0, sizeof(ConfigFileEntry)); */
324 GlobalSetOptions
.maxclients
= ServerInfo
.default_max_clients
;
326 if(GlobalSetOptions
.maxclients
> (maxconnections
- MAX_BUFFER
) || (GlobalSetOptions
.maxclients
<= 0))
327 GlobalSetOptions
.maxclients
= maxconnections
- MAX_BUFFER
;
329 GlobalSetOptions
.autoconn
= 1;
331 GlobalSetOptions
.spam_time
= MIN_JOIN_LEAVE_TIME
;
332 GlobalSetOptions
.spam_num
= MAX_JOIN_LEAVE_COUNT
;
334 GlobalSetOptions
.floodcount
= ConfigFileEntry
.default_floodcount
;
336 split_servers
= ConfigChannel
.default_split_server_count
;
337 split_users
= ConfigChannel
.default_split_user_count
;
339 if(split_users
&& split_servers
340 && (ConfigChannel
.no_create_on_split
|| ConfigChannel
.no_join_on_split
))
346 GlobalSetOptions
.ident_timeout
= ConfigFileEntry
.default_ident_timeout
;
348 rb_strlcpy(GlobalSetOptions
.operstring
,
349 ConfigFileEntry
.default_operstring
,
350 sizeof(GlobalSetOptions
.operstring
));
351 rb_strlcpy(GlobalSetOptions
.adminstring
,
352 ConfigFileEntry
.default_adminstring
,
353 sizeof(GlobalSetOptions
.adminstring
));
355 /* memset( &ConfigChannel, 0, sizeof(ConfigChannel)); */
357 /* End of global set options */
362 * initialize_server_capabs
368 initialize_server_capabs(void)
370 default_server_capabs
&= ~CAP_ZIP
;
377 * inputs - filename+path of pid file
379 * side effects - write the pid of the ircd to filename
382 write_pidfile(const char *filename
)
386 if((fb
= fopen(filename
, "w")))
388 unsigned int pid
= (unsigned int) getpid();
390 snprintf(buff
, sizeof(buff
), "%u\n", pid
);
391 if((fputs(buff
, fb
) == -1))
393 ilog(L_MAIN
, "Error writing %u to pid file %s (%s)",
394 pid
, filename
, strerror(errno
));
401 ilog(L_MAIN
, "Error opening pid file %s", filename
);
408 * inputs - filename+path of pid file
410 * side effects - reads pid from pidfile and checks if ircd is in process
411 * list. if it is, gracefully exits
415 check_pidfile(const char *filename
)
421 /* Don't do logging here, since we don't have log() initialised */
422 if((fb
= fopen(filename
, "r")))
424 if(fgets(buff
, 20, fb
) != NULL
)
426 pidfromfile
= atoi(buff
);
427 if(!rb_kill(pidfromfile
, 0))
429 printf("ircd: daemon is already running\n");
442 * side effects - setups corefile to system limits.
448 #ifdef HAVE_SYS_RESOURCE_H
449 struct rlimit rlim
; /* resource limits */
451 /* Set corefilesize to maximum */
452 if(!getrlimit(RLIMIT_CORE
, &rlim
))
454 rlim
.rlim_cur
= rlim
.rlim_max
;
455 setrlimit(RLIMIT_CORE
, &rlim
);
461 ircd_log_cb(const char *str
)
463 ilog(L_MAIN
, "librb reports: %s", str
);
467 ircd_restart_cb(const char *str
)
469 inotice("librb has called the restart callback: %s", str
);
474 * Why EXIT_FAILURE here?
475 * Because if ircd_die_cb() is called it's because of a fatal
476 * error inside libcharybdis, and we don't know how to handle the
477 * exception, so it is logical to return a FAILURE exit code here.
481 ircd_die_cb(const char *str
)
485 /* Try to get the message out to currently logged in operators. */
486 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
, "librb has called the die callback..aborting: %s", str
);
487 inotice("librb has called the die callback..aborting: %s", str
);
490 inotice("librb has called the die callback..aborting");
496 struct ev_entry
*check_splitmode_ev
= NULL
;
499 seed_with_urandom(void)
504 fd
= open("/dev/urandom", O_RDONLY
);
507 if(read(fd
, &seed
, sizeof(seed
)) == sizeof(seed
))
519 seed_with_clock(void)
521 const struct timeval
*tv
;
523 tv
= rb_current_time_tv();
524 srand(tv
->tv_sec
^ (tv
->tv_usec
| (getpid() << 20)));
528 seed_random(void *unused
)
531 if(rb_get_random(&seed
, sizeof(seed
)) == -1)
533 if(!seed_with_urandom())
543 * Initializes the IRCd.
545 * Inputs - number of commandline args, args themselves
547 * Side Effects - this is where the ircd gets going right now
550 charybdis_main(int argc
, char *argv
[])
555 /* Check to see if the user is running us as root, which is a nono */
558 fprintf(stderr
, "Don't run ircd as root!!!\n");
565 ConfigFileEntry
.dpath
= DPATH
;
566 ConfigFileEntry
.configfile
= CPATH
; /* Server configuration file */
567 ConfigFileEntry
.connect_timeout
= 30; /* Default to 30 */
569 umask(077); /* better safe than sorry --SRB */
572 parseargs(&argc
, &argv
, myopts
);
574 if(chdir(ConfigFileEntry
.dpath
))
576 fprintf(stderr
, "Unable to chdir to %s: %s\n", ConfigFileEntry
.dpath
, strerror(errno
));
583 * Setup corefile size immediately after boot -kre
587 /* initialise operhash fairly early. */
590 memset(&me
, 0, sizeof(me
));
591 memset(&meLocalUser
, 0, sizeof(meLocalUser
));
592 me
.localClient
= &meLocalUser
;
594 /* Make sure all lists are zeroed */
595 memset(&unknown_list
, 0, sizeof(unknown_list
));
596 memset(&lclient_list
, 0, sizeof(lclient_list
));
597 memset(&serv_list
, 0, sizeof(serv_list
));
598 memset(&global_serv_list
, 0, sizeof(global_serv_list
));
599 memset(&local_oper_list
, 0, sizeof(local_oper_list
));
600 memset(&oper_list
, 0, sizeof(oper_list
));
602 rb_dlinkAddTail(&me
, &me
.node
, &global_client_list
);
604 memset(&Count
, 0, sizeof(Count
));
605 memset(&ServerInfo
, 0, sizeof(ServerInfo
));
606 memset(&AdminInfo
, 0, sizeof(AdminInfo
));
607 memset(&ServerStats
, 0, sizeof(struct ServerStatistics
));
611 printf("ircd: version %s(%s)\n", ircd_version
, serno
);
612 #ifdef CUSTOM_BRANDING
613 printf("ircd: based on %s-%s\n", PACKAGE_NAME
, PACKAGE_VERSION
);
615 printf("ircd: %s\n", rb_lib_version());
622 server_state_foreground
= true;
625 /* Make sure fd 0, 1 and 2 are in use -- jilles */
628 fd
= open("/dev/null", O_RDWR
);
629 } while (fd
< 2 && fd
!= -1);
636 /* Check if there is pidfile and daemon already running */
639 check_pidfile(pidFileName
);
641 if(!server_state_foreground
)
643 inotice("starting %s ...", ircd_version
);
644 inotice("%s", rb_lib_version());
647 /* Init the event subsystem */
648 rb_lib_init(ircd_log_cb
, ircd_restart_cb
, ircd_die_cb
, !server_state_foreground
, maxconnections
, DNODE_HEAP_SIZE
, FD_HEAP_SIZE
);
649 rb_linebuf_init(LINEBUF_HEAP_SIZE
);
651 rb_init_prng(NULL
, RB_PRNG_DEFAULT
);
655 init_builtin_capabs();
656 default_server_capabs
= CAP_MASK
;
663 clear_scache_hash_table(); /* server cache name table */
675 construct_cflags_strings();
678 load_core_modules(1);
680 init_auth(); /* Initialise the auth code */
681 init_authd(); /* Start up authd. */
682 init_dns(); /* Start up DNS query system */
684 privilegeset_set_new("default", "", 0);
687 fprintf(stderr
, "\nBeginning config test\n");
688 read_conf_files(true); /* cold start init conf files */
690 mod_add_path(MODULE_DIR
);
691 mod_add_path(MODULE_DIR
"/autoload");
700 initialize_server_capabs(); /* Set up default_server_capabs */
701 initialize_global_set_options();
703 if(ServerInfo
.name
== NULL
)
705 ierror("no server name specified in serverinfo block.");
708 rb_strlcpy(me
.name
, ServerInfo
.name
, sizeof(me
.name
));
710 if(ServerInfo
.sid
[0] == '\0')
712 ierror("no server sid specified in serverinfo block.");
715 strcpy(me
.id
, ServerInfo
.sid
);
718 /* serverinfo{} description must exist. If not, error out. */
719 if(ServerInfo
.description
== NULL
)
721 ierror("no server description specified in serverinfo block.");
724 rb_strlcpy(me
.info
, ServerInfo
.description
, sizeof(me
.info
));
726 if(ServerInfo
.ssl_cert
!= NULL
&& ServerInfo
.ssl_private_key
!= NULL
)
728 /* just do the rb_setup_ssl_server to validate the config */
729 if(!rb_setup_ssl_server(ServerInfo
.ssl_cert
, ServerInfo
.ssl_private_key
, ServerInfo
.ssl_dh_params
, ServerInfo
.ssl_cipher_list
))
731 ilog(L_MAIN
, "WARNING: Unable to setup SSL.");
740 fprintf(stderr
, "\nConfig testing complete.\n");
742 return 0; /* Why? We want the launcher to exit out. */
749 startup_time
= rb_current_time();
750 add_to_client_hash(me
.name
, &me
);
751 add_to_id_hash(me
.id
, &me
);
752 me
.serv
->nameinfo
= scache_connect(me
.name
, me
.info
, 0);
754 rb_dlinkAddAlloc(&me
, &global_serv_list
);
756 construct_umodebuf();
759 write_pidfile(pidFileName
);
763 ilog(L_MAIN
, "Server Ready");
765 /* We want try_connections to be called as soon as possible now! -- adrian */
766 /* No, 'cause after a restart it would cause all sorts of nick collides */
767 /* um. by waiting even longer, that just means we have even *more*
768 * nick collisions. what a stupid idea. set an event for the IO loop --fl
770 rb_event_addish("try_connections", try_connections
, NULL
, STARTUP_CONNECTIONS_TIME
);
771 rb_event_addonce("try_connections_startup", try_connections
, NULL
, 2);
772 rb_event_add("check_rehash", check_rehash
, NULL
, 3);
773 rb_event_addish("reseed_srand", seed_random
, NULL
, 300); /* reseed every 10 minutes */
776 check_splitmode_ev
= rb_event_add("check_splitmode", check_splitmode
, NULL
, 5);
778 print_startup(getpid());