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 */
48 #include "s_serv.h" /* try_connections */
53 #include "supported.h"
57 #include "ircd_getopt.h"
61 #include "s_newconf.h"
64 #include "patchlevel.h"
68 #include "privilege.h"
73 /* /quote set variables */
74 struct SetOptions GlobalSetOptions
;
76 /* configuration set from ircd.conf */
77 struct config_file_entry ConfigFileEntry
;
78 /* server info set from ircd.conf */
79 struct server_info ServerInfo
;
80 /* admin info set from ircd.conf */
81 struct admin_info AdminInfo
;
84 struct ServerStatistics ServerStats
;
87 struct Client me
; /* That's me */
88 struct LocalUser meLocalUser
; /* That's also part of me */
90 rb_dlink_list global_client_list
;
92 /* unknown/client pointer lists */
93 rb_dlink_list unknown_list
; /* unknown clients ON this server only */
94 rb_dlink_list lclient_list
; /* local clients only ON this server */
95 rb_dlink_list serv_list
; /* local servers to this server ONLY */
96 rb_dlink_list global_serv_list
; /* global servers on the network */
97 rb_dlink_list local_oper_list
; /* our opers, duplicated in lclient_list */
98 rb_dlink_list oper_list
; /* network opers */
100 const char *logFileName
= LPATH
;
101 const char *pidFileName
= PPATH
;
105 int dorehashbans
= 0;
107 int kline_queued
= 0;
108 int server_state_foreground
= 0;
109 int opers_see_all_users
= 0;
113 int testing_conf
= 0;
116 int default_server_capabs
;
125 ircd_shutdown(const char *reason
)
127 struct Client
*target_p
;
130 RB_DLINK_FOREACH(ptr
, lclient_list
.head
)
132 target_p
= ptr
->data
;
134 sendto_one(target_p
, ":%s NOTICE %s :Server Terminating. %s",
135 me
.name
, target_p
->name
, reason
);
138 RB_DLINK_FOREACH(ptr
, serv_list
.head
)
140 target_p
= ptr
->data
;
142 sendto_one(target_p
, ":%s ERROR :Terminated by %s",
146 ilog(L_MAIN
, "Server Terminating. %s", reason
);
154 * print_startup - print startup information
157 print_startup(int pid
)
162 fd
= open("/dev/null", O_RDWR
);
164 perror("open /dev/null");
172 inotice("now running in %s mode from %s as pid %d ...",
173 !server_state_foreground
? "background" : "foreground",
174 ConfigFileEntry
.dpath
, pid
);
176 /* let the parent process know the initialization was successful
178 if (!server_state_foreground
)
180 if (dup2(1, 0) == -1)
182 if (dup2(1, 2) == -1)
189 * inputs - boot_daemon flag
191 * side effects - if boot_daemon flag is not set, don't daemonize
196 #if defined(RLIMIT_NOFILE) && defined(HAVE_SYS_RESOURCE_H)
199 if(!getrlimit(RLIMIT_NOFILE
, &limit
))
201 maxconnections
= limit
.rlim_cur
;
202 if(maxconnections
<= MAX_BUFFER
)
204 fprintf(stderr
, "ERROR: Shell FD limits are too low.\n");
205 fprintf(stderr
, "ERROR: charybdis reserves %d FDs, shell limits must be above this\n", MAX_BUFFER
);
210 #endif /* RLIMIT_FD_MAX */
211 maxconnections
= MAXCONNECTIONS
;
228 if((pid
= fork()) < 0)
236 /* Wait for initialization to finish, successfully or
237 * unsuccessfully. Until this point the child may still
238 * write to stdout/stderr.
240 if (read(pip
[0], &c
, 1) > 0)
255 static int printVersion
= 0;
257 struct lgetopt myopts
[] = {
258 {"configfile", &ConfigFileEntry
.configfile
,
259 STRING
, "File to use for ircd.conf"},
260 {"logfile", &logFileName
,
261 STRING
, "File to use for ircd.log"},
262 {"pidfile", &pidFileName
,
263 STRING
, "File to use for process ID"},
264 {"foreground", &server_state_foreground
,
265 YESNO
, "Run in foreground (don't detach)"},
266 {"version", &printVersion
,
267 YESNO
, "Print version and exit"},
268 {"conftest", &testing_conf
,
269 YESNO
, "Test the configuration files and exit"},
270 {"help", NULL
, USAGE
, "Print this text"},
271 {NULL
, NULL
, STRING
, NULL
},
275 check_rehash(void *unused
)
278 * Check to see whether we have to rehash the configuration ..
294 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
295 "Got signal SIGUSR1, reloading ircd motd file");
302 * initalialize_global_set_options
306 * side effects - This sets all global set options needed
309 initialize_global_set_options(void)
311 memset(&GlobalSetOptions
, 0, sizeof(GlobalSetOptions
));
312 /* memset( &ConfigFileEntry, 0, sizeof(ConfigFileEntry)); */
314 GlobalSetOptions
.maxclients
= ServerInfo
.default_max_clients
;
316 if(GlobalSetOptions
.maxclients
> (maxconnections
- MAX_BUFFER
) || (GlobalSetOptions
.maxclients
<= 0))
317 GlobalSetOptions
.maxclients
= maxconnections
- MAX_BUFFER
;
319 GlobalSetOptions
.autoconn
= 1;
321 GlobalSetOptions
.spam_time
= MIN_JOIN_LEAVE_TIME
;
322 GlobalSetOptions
.spam_num
= MAX_JOIN_LEAVE_COUNT
;
324 GlobalSetOptions
.floodcount
= ConfigFileEntry
.default_floodcount
;
326 split_servers
= ConfigChannel
.default_split_server_count
;
327 split_users
= ConfigChannel
.default_split_user_count
;
329 if(split_users
&& split_servers
330 && (ConfigChannel
.no_create_on_split
|| ConfigChannel
.no_join_on_split
))
336 GlobalSetOptions
.ident_timeout
= ConfigFileEntry
.default_ident_timeout
;
338 rb_strlcpy(GlobalSetOptions
.operstring
,
339 ConfigFileEntry
.default_operstring
,
340 sizeof(GlobalSetOptions
.operstring
));
341 rb_strlcpy(GlobalSetOptions
.adminstring
,
342 ConfigFileEntry
.default_adminstring
,
343 sizeof(GlobalSetOptions
.adminstring
));
345 /* memset( &ConfigChannel, 0, sizeof(ConfigChannel)); */
347 /* End of global set options */
352 * initialize_server_capabs
358 initialize_server_capabs(void)
360 default_server_capabs
&= ~CAP_ZIP
;
367 * inputs - filename+path of pid file
369 * side effects - write the pid of the ircd to filename
372 write_pidfile(const char *filename
)
376 if((fb
= fopen(filename
, "w")))
378 unsigned int pid
= (unsigned int) getpid();
380 snprintf(buff
, sizeof(buff
), "%u\n", pid
);
381 if((fputs(buff
, fb
) == -1))
383 ilog(L_MAIN
, "Error writing %u to pid file %s (%s)",
384 pid
, filename
, strerror(errno
));
391 ilog(L_MAIN
, "Error opening pid file %s", filename
);
398 * inputs - filename+path of pid file
400 * side effects - reads pid from pidfile and checks if ircd is in process
401 * list. if it is, gracefully exits
405 check_pidfile(const char *filename
)
411 /* Don't do logging here, since we don't have log() initialised */
412 if((fb
= fopen(filename
, "r")))
414 if(fgets(buff
, 20, fb
) != NULL
)
416 pidfromfile
= atoi(buff
);
417 if(!kill(pidfromfile
, 0))
419 printf("ircd: daemon is already running\n");
432 * side effects - setups corefile to system limits.
438 #ifdef HAVE_SYS_RESOURCE_H
439 struct rlimit rlim
; /* resource limits */
441 /* Set corefilesize to maximum */
442 if(!getrlimit(RLIMIT_CORE
, &rlim
))
444 rlim
.rlim_cur
= rlim
.rlim_max
;
445 setrlimit(RLIMIT_CORE
, &rlim
);
451 ircd_log_cb(const char *str
)
453 ilog(L_MAIN
, "libratbox reports: %s", str
);
457 ircd_restart_cb(const char *str
)
459 inotice("libratbox has called the restart callback: %s", str
);
464 * Why EXIT_FAILURE here?
465 * Because if ircd_die_cb() is called it's because of a fatal
466 * error inside libcharybdis, and we don't know how to handle the
467 * exception, so it is logical to return a FAILURE exit code here.
471 ircd_die_cb(const char *str
)
475 /* Try to get the message out to currently logged in operators. */
476 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
, "libratbox has called the die callback..aborting: %s", str
);
477 inotice("libratbox has called the die callback..aborting: %s", str
);
480 inotice("libratbox has called the die callback..aborting");
486 struct ev_entry
*check_splitmode_ev
= NULL
;
489 seed_with_urandom(void)
494 fd
= open("/dev/urandom", O_RDONLY
);
497 if(read(fd
, &seed
, sizeof(seed
)) == sizeof(seed
))
509 seed_with_clock(void)
511 const struct timeval
*tv
;
513 tv
= rb_current_time_tv();
514 srand(tv
->tv_sec
^ (tv
->tv_usec
| (getpid() << 20)));
518 seed_random(void *unused
)
521 if(rb_get_random(&seed
, sizeof(seed
)) == -1)
523 if(!seed_with_urandom())
533 * Initializes the IRCd.
535 * Inputs - number of commandline args, args themselves
537 * Side Effects - this is where the ircd gets going right now
540 charybdis_main(int argc
, char *argv
[])
544 /* Check to see if the user is running us as root, which is a nono */
547 fprintf(stderr
, "Don't run ircd as root!!!\n");
553 ConfigFileEntry
.dpath
= DPATH
;
554 ConfigFileEntry
.configfile
= CPATH
; /* Server configuration file */
555 ConfigFileEntry
.connect_timeout
= 30; /* Default to 30 */
557 umask(077); /* better safe than sorry --SRB */
560 parseargs(&argc
, &argv
, myopts
);
562 if(chdir(ConfigFileEntry
.dpath
))
564 fprintf(stderr
, "Unable to chdir to %s: %s\n", ConfigFileEntry
.dpath
, strerror(errno
));
571 * Setup corefile size immediately after boot -kre
575 /* initialise operhash fairly early. */
578 memset(&me
, 0, sizeof(me
));
579 memset(&meLocalUser
, 0, sizeof(meLocalUser
));
580 me
.localClient
= &meLocalUser
;
582 /* Make sure all lists are zeroed */
583 memset(&unknown_list
, 0, sizeof(unknown_list
));
584 memset(&lclient_list
, 0, sizeof(lclient_list
));
585 memset(&serv_list
, 0, sizeof(serv_list
));
586 memset(&global_serv_list
, 0, sizeof(global_serv_list
));
587 memset(&local_oper_list
, 0, sizeof(local_oper_list
));
588 memset(&oper_list
, 0, sizeof(oper_list
));
590 rb_dlinkAddTail(&me
, &me
.node
, &global_client_list
);
592 memset(&Count
, 0, sizeof(Count
));
593 memset(&ServerInfo
, 0, sizeof(ServerInfo
));
594 memset(&AdminInfo
, 0, sizeof(AdminInfo
));
595 memset(&ServerStats
, 0, sizeof(struct ServerStatistics
));
599 printf("ircd: version %s(%s)\n", ircd_version
, serno
);
600 #ifdef CUSTOM_BRANDING
601 printf("ircd: based on %s-%s\n", PACKAGE_NAME
, PACKAGE_VERSION
);
603 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 rb_init_prng(NULL
, RB_PRNG_DEFAULT
);
641 init_builtin_capabs();
642 default_server_capabs
= CAP_MASK
;
649 clear_scache_hash_table(); /* server cache name table */
661 construct_cflags_strings();
664 load_core_modules(1);
666 init_auth(); /* Initialise the auth code */
667 init_authd(); /* Start up authd. */
668 privilegeset_set_new("default", "", 0);
671 fprintf(stderr
, "\nBeginning config test\n");
672 read_conf_files(YES
); /* 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());