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"
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
;
102 int dorehashbans
= 0;
104 int kline_queued
= 0;
105 int server_state_foreground
= 0;
106 int opers_see_all_users
= 0;
110 int testing_conf
= 0;
113 int default_server_capabs
= CAP_MASK
;
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
)
156 inotice("now running in %s mode from %s as pid %d ...",
157 !server_state_foreground
? "background" : "foreground",
158 ConfigFileEntry
.dpath
, pid
);
160 /* let the parent process know the initialization was successful
162 if (!server_state_foreground
)
167 open("/dev/null", O_RDWR
);
175 * inputs - boot_daemon flag
177 * side effects - if boot_daemon flag is not set, don't daemonize
182 #if defined(RLIMIT_NOFILE) && defined(HAVE_SYS_RESOURCE_H)
185 if(!getrlimit(RLIMIT_NOFILE
, &limit
))
187 maxconnections
= limit
.rlim_cur
;
188 if(maxconnections
<= MAX_BUFFER
)
190 fprintf(stderr
, "ERROR: Shell FD limits are too low.\n");
191 fprintf(stderr
, "ERROR: ircd-ratbox reserves %d FDs, shell limits must be above this\n", MAX_BUFFER
);
196 #endif /* RLIMIT_FD_MAX */
197 maxconnections
= MAXCONNECTIONS
;
214 if((pid
= fork()) < 0)
222 /* Wait for initialization to finish, successfully or
223 * unsuccessfully. Until this point the child may still
224 * write to stdout/stderr.
226 if (read(pip
[0], &c
, 1) > 0)
241 static int printVersion
= 0;
243 struct lgetopt myopts
[] = {
244 {"dlinefile", &ConfigFileEntry
.dlinefile
,
245 STRING
, "File to use for dlines.conf"},
246 {"configfile", &ConfigFileEntry
.configfile
,
247 STRING
, "File to use for ircd.conf"},
248 {"klinefile", &ConfigFileEntry
.klinefile
,
249 STRING
, "File to use for kline.conf"},
250 {"xlinefile", &ConfigFileEntry
.xlinefile
,
251 STRING
, "File to use for xline.conf"},
252 {"resvfile", &ConfigFileEntry
.resvfile
,
253 STRING
, "File to use for resv.conf"},
254 {"logfile", &logFileName
,
255 STRING
, "File to use for ircd.log"},
256 {"pidfile", &pidFileName
,
257 STRING
, "File to use for process ID"},
258 {"foreground", &server_state_foreground
,
259 YESNO
, "Run in foreground (don't detach)"},
260 {"version", &printVersion
,
261 YESNO
, "Print version and exit"},
262 {"conftest", &testing_conf
,
263 YESNO
, "Test the configuration files and exit"},
264 {"help", NULL
, USAGE
, "Print this text"},
265 {NULL
, NULL
, STRING
, NULL
},
269 check_rehash(void *unused
)
272 * Check to see whether we have to rehash the configuration ..
288 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
289 "Got signal SIGUSR1, reloading ircd motd file");
296 * initalialize_global_set_options
300 * side effects - This sets all global set options needed
303 initialize_global_set_options(void)
305 memset(&GlobalSetOptions
, 0, sizeof(GlobalSetOptions
));
306 /* memset( &ConfigFileEntry, 0, sizeof(ConfigFileEntry)); */
308 GlobalSetOptions
.maxclients
= ServerInfo
.default_max_clients
;
310 if(GlobalSetOptions
.maxclients
> (maxconnections
- MAX_BUFFER
) || (GlobalSetOptions
.maxclients
<= 0))
311 GlobalSetOptions
.maxclients
= maxconnections
- MAX_BUFFER
;
313 GlobalSetOptions
.autoconn
= 1;
315 GlobalSetOptions
.spam_time
= MIN_JOIN_LEAVE_TIME
;
316 GlobalSetOptions
.spam_num
= MAX_JOIN_LEAVE_COUNT
;
318 if(ConfigFileEntry
.default_floodcount
)
319 GlobalSetOptions
.floodcount
= ConfigFileEntry
.default_floodcount
;
321 GlobalSetOptions
.floodcount
= 10;
323 split_servers
= ConfigChannel
.default_split_server_count
;
324 split_users
= ConfigChannel
.default_split_user_count
;
326 if(split_users
&& split_servers
327 && (ConfigChannel
.no_create_on_split
|| ConfigChannel
.no_join_on_split
))
333 GlobalSetOptions
.ident_timeout
= IDENT_TIMEOUT
;
335 rb_strlcpy(GlobalSetOptions
.operstring
,
336 ConfigFileEntry
.default_operstring
,
337 sizeof(GlobalSetOptions
.operstring
));
338 rb_strlcpy(GlobalSetOptions
.adminstring
,
339 ConfigFileEntry
.default_adminstring
,
340 sizeof(GlobalSetOptions
.adminstring
));
342 /* memset( &ConfigChannel, 0, sizeof(ConfigChannel)); */
344 /* End of global set options */
349 * initialize_server_capabs
355 initialize_server_capabs(void)
357 default_server_capabs
&= ~CAP_ZIP
;
364 * inputs - filename+path of pid file
366 * side effects - write the pid of the ircd to filename
369 write_pidfile(const char *filename
)
373 if((fb
= fopen(filename
, "w")))
375 unsigned int pid
= (unsigned int) getpid();
377 rb_snprintf(buff
, sizeof(buff
), "%u\n", pid
);
378 if((fputs(buff
, fb
) == -1))
380 ilog(L_MAIN
, "Error writing %u to pid file %s (%s)",
381 pid
, filename
, strerror(errno
));
388 ilog(L_MAIN
, "Error opening pid file %s", filename
);
395 * inputs - filename+path of pid file
397 * side effects - reads pid from pidfile and checks if ircd is in process
398 * list. if it is, gracefully exits
402 check_pidfile(const char *filename
)
408 /* Don't do logging here, since we don't have log() initialised */
409 if((fb
= fopen(filename
, "r")))
411 if(fgets(buff
, 20, fb
) != NULL
)
413 pidfromfile
= atoi(buff
);
414 if(!kill(pidfromfile
, 0))
416 printf("ircd: daemon is already running\n");
429 * side effects - setups corefile to system limits.
435 #ifdef HAVE_SYS_RESOURCE_H
436 struct rlimit rlim
; /* resource limits */
438 /* Set corefilesize to maximum */
439 if(!getrlimit(RLIMIT_CORE
, &rlim
))
441 rlim
.rlim_cur
= rlim
.rlim_max
;
442 setrlimit(RLIMIT_CORE
, &rlim
);
448 ircd_log_cb(const char *str
)
450 ilog(L_MAIN
, "libratbox reports: %s", str
);
454 ircd_restart_cb(const char *str
)
456 inotice(L_MAIN
, "libratbox has called the restart callback: %s", str
);
461 * Why EXIT_FAILURE here?
462 * Because if ircd_die_cb() is called it's because of a fatal
463 * error inside libcharybdis, and we don't know how to handle the
464 * exception, so it is logical to return a FAILURE exit code here.
468 ircd_die_cb(const char *str
)
472 /* Try to get the message out to currently logged in operators. */
473 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
, "libratbox has called the die callback..aborting: %s", str
);
474 inotice("libratbox has called the die callback..aborting: %s", str
);
477 inotice(L_MAIN
, "libratbox has called the die callback..aborting");
483 struct ev_entry
*check_splitmode_ev
= NULL
;
486 seed_with_urandom(void)
491 fd
= open("/dev/urandom", O_RDONLY
);
494 if(read(fd
, &seed
, sizeof(seed
)) == sizeof(seed
))
505 seed_with_clock(void)
507 const struct timeval
*tv
;
509 tv
= rb_current_time_tv();
510 srand(tv
->tv_sec
^ (tv
->tv_usec
| (getpid() << 20)));
514 seed_random(void *unused
)
517 if(rb_get_random(&seed
, sizeof(seed
)) == -1)
519 if(!seed_with_urandom())
529 * Initializes the IRCd.
531 * Inputs - number of commandline args, args themselves
533 * Side Effects - this is where the ircd gets going right now
536 main(int argc
, char *argv
[])
540 /* Check to see if the user is running us as root, which is a nono */
543 fprintf(stderr
, "Don't run ircd as root!!!\n");
549 ConfigFileEntry
.dpath
= DPATH
;
550 ConfigFileEntry
.configfile
= CPATH
; /* Server configuration file */
551 ConfigFileEntry
.klinefile
= KPATH
; /* Server kline file */
552 ConfigFileEntry
.dlinefile
= DLPATH
; /* dline file */
553 ConfigFileEntry
.xlinefile
= XPATH
;
554 ConfigFileEntry
.resvfile
= RESVPATH
;
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 memset(&me
, 0, sizeof(me
));
576 memset(&meLocalUser
, 0, sizeof(meLocalUser
));
577 me
.localClient
= &meLocalUser
;
579 /* Make sure all lists are zeroed */
580 memset(&unknown_list
, 0, sizeof(unknown_list
));
581 memset(&lclient_list
, 0, sizeof(lclient_list
));
582 memset(&serv_list
, 0, sizeof(serv_list
));
583 memset(&global_serv_list
, 0, sizeof(global_serv_list
));
584 memset(&local_oper_list
, 0, sizeof(local_oper_list
));
585 memset(&oper_list
, 0, sizeof(oper_list
));
587 rb_dlinkAddTail(&me
, &me
.node
, &global_client_list
);
589 memset(&Count
, 0, sizeof(Count
));
590 memset(&ServerInfo
, 0, sizeof(ServerInfo
));
591 memset(&AdminInfo
, 0, sizeof(AdminInfo
));
592 memset(&ServerStats
, 0, sizeof(struct ServerStatistics
));
594 /* Initialise the channel capability usage counts... */
595 init_chcap_usage_counts();
599 printf("ircd: version %s(%s)\n", ircd_version
, serno
);
608 server_state_foreground
= 1;
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
);
630 /* Init the event subsystem */
631 rb_lib_init(ircd_log_cb
, ircd_restart_cb
, ircd_die_cb
, !server_state_foreground
, maxconnections
, DNODE_HEAP_SIZE
, FD_HEAP_SIZE
);
632 rb_linebuf_init(LINEBUF_HEAP_SIZE
);
639 clear_scache_hash_table(); /* server cache name table */
652 #ifndef STATIC_MODULES
653 load_core_modules(1);
655 init_auth(); /* Initialise the auth code */
656 init_resolver(); /* Needs to be setup before the io loop */
659 fprintf(stderr
, "\nBeginning config test\n");
660 read_conf_files(YES
); /* cold start init conf files */
662 #ifndef STATIC_MODULES
664 mod_add_path(MODULE_DIR
);
665 mod_add_path(MODULE_DIR
"/autoload");
670 initialize_server_capabs(); /* Set up default_server_capabs */
671 initialize_global_set_options();
673 if(ServerInfo
.name
== NULL
)
675 ierror("no server name specified in serverinfo block.");
678 rb_strlcpy(me
.name
, ServerInfo
.name
, sizeof(me
.name
));
680 if(ServerInfo
.sid
[0] == '\0')
682 ierror("no server sid specified in serverinfo block.");
685 strcpy(me
.id
, ServerInfo
.sid
);
688 /* serverinfo{} description must exist. If not, error out. */
689 if(ServerInfo
.description
== NULL
)
691 ierror("no server description specified in serverinfo block.");
694 rb_strlcpy(me
.info
, ServerInfo
.description
, sizeof(me
.info
));
696 if(ServerInfo
.ssl_cert
!= NULL
&& ServerInfo
.ssl_private_key
!= NULL
)
698 /* just do the rb_setup_ssl_server to validate the config */
699 if(!rb_setup_ssl_server(ServerInfo
.ssl_cert
, ServerInfo
.ssl_private_key
, ServerInfo
.ssl_dh_params
))
701 ilog(L_MAIN
, "WARNING: Unable to setup SSL.");
710 fprintf(stderr
, "\nConfig testing complete.\n");
712 return 0; /* Why? We want the launcher to exit out. */
719 startup_time
= rb_current_time();
720 add_to_client_hash(me
.name
, &me
);
721 add_to_id_hash(me
.id
, &me
);
722 me
.serv
->nameinfo
= scache_connect(me
.name
, me
.info
, 0);
724 rb_dlinkAddAlloc(&me
, &global_serv_list
);
726 construct_umodebuf();
727 construct_noparam_modes();
730 write_pidfile(pidFileName
);
734 ilog(L_MAIN
, "Server Ready");
736 /* We want try_connections to be called as soon as possible now! -- adrian */
737 /* No, 'cause after a restart it would cause all sorts of nick collides */
738 /* um. by waiting even longer, that just means we have even *more*
739 * nick collisions. what a stupid idea. set an event for the IO loop --fl
741 rb_event_addish("try_connections", try_connections
, NULL
, STARTUP_CONNECTIONS_TIME
);
742 rb_event_addonce("try_connections_startup", try_connections
, NULL
, 2);
743 rb_event_add("check_rehash", check_rehash
, NULL
, 3);
744 rb_event_addish("reseed_srand", seed_random
, NULL
, 300); /* reseed every 10 minutes */
747 check_splitmode_ev
= rb_event_add("check_splitmode", check_splitmode
, NULL
, 5);
749 print_startup(getpid());