2 * ircd-ratbox: 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-2005 ircd-ratbox development team
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
24 * $Id: ircd.c 3380 2007-04-03 22:25:11Z jilles $
27 #include "ratbox_lib.h"
38 #include "ircd_signal.h"
39 #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 /* /quote set variables */
69 struct SetOptions GlobalSetOptions
;
71 /* configuration set from ircd.conf */
72 struct config_file_entry ConfigFileEntry
;
73 /* server info set from ircd.conf */
74 struct server_info ServerInfo
;
75 /* admin info set from ircd.conf */
76 struct admin_info AdminInfo
;
79 struct ServerStatistics ServerStats
;
82 struct timeval SystemTime
;
83 struct Client me
; /* That's me */
84 struct LocalUser meLocalUser
; /* That's also part of me */
86 rb_dlink_list lclient_list
= { NULL
, NULL
, 0 };
87 rb_dlink_list global_client_list
= { NULL
, NULL
, 0 };
88 rb_dlink_list global_channel_list
= { NULL
, NULL
, 0 };
90 rb_dlink_list unknown_list
; /* unknown clients ON this server only */
91 rb_dlink_list serv_list
; /* local servers to this server ONLY */
92 rb_dlink_list global_serv_list
; /* global servers on the network */
93 rb_dlink_list local_oper_list
; /* our opers, duplicated in lclient_list */
94 rb_dlink_list oper_list
; /* network opers */
98 int default_server_capabs
= CAP_MASK
;
106 unsigned long initialVMTop
= 0; /* top of virtual memory at init */
107 const char *logFileName
= LPATH
;
108 const char *pidFileName
= PPATH
;
112 int dorehashbans
= 0;
114 int kline_queued
= 0;
115 int server_state_foreground
= 0;
116 int opers_see_all_users
= 0;
120 int testing_conf
= 0;
122 struct config_channel_entry ConfigChannel
;
128 rb_bh
*client_heap
= NULL
;
129 rb_bh
*lclient_heap
= NULL
;
130 rb_bh
*pclient_heap
= NULL
;
132 char current_uid
[IDLEN
];
137 rb_bh
*patricia_heap
;
144 ircd_shutdown(const char *reason
)
146 struct Client
*target_p
;
149 RB_DLINK_FOREACH(ptr
, lclient_list
.head
)
151 target_p
= ptr
->data
;
153 sendto_one(target_p
, ":%s NOTICE %s :Server Terminating. %s",
154 me
.name
, target_p
->name
, reason
);
157 RB_DLINK_FOREACH(ptr
, serv_list
.head
)
159 target_p
= ptr
->data
;
161 sendto_one(target_p
, ":%s ERROR :Terminated by %s",
165 ilog(L_MAIN
, "Server Terminating. %s", reason
);
173 * print_startup - print startup information
176 print_startup(int pid
)
178 inotice("now running in %s mode from %s as pid %d ...",
179 !server_state_foreground
? "background" : "foreground",
180 ConfigFileEntry
.dpath
, pid
);
182 /* let the parent process know the initialization was successful
184 if (!server_state_foreground
)
189 open("/dev/null", O_RDWR
);
195 ircd_log_cb(const char *str
)
197 ilog(L_MAIN
, "%s", str
);
201 ircd_restart_cb(const char *str
)
207 * Why EXIT_FAILURE here?
208 * Because if ircd_die_cb() is called it's because of a fatal
209 * error inside libcharybdis, and we don't know how to handle the
210 * exception, so it is logical to return a FAILURE exit code here.
214 ircd_die_cb(const char *str
)
218 /* Try to get the message out to currently logged in operators. */
219 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
, "Server panic! %s", str
);
220 inotice("server panic: %s", str
);
230 * inputs - boot_daemon flag
232 * side effects - if boot_daemon flag is not set, don't daemonize
237 #if defined(RLIMIT_NOFILE) && defined(HAVE_SYS_RESOURCE_H)
240 if(!getrlimit(RLIMIT_NOFILE
, &limit
))
242 maxconnections
= limit
.rlim_cur
;
243 if(maxconnections
<= MAX_BUFFER
)
245 fprintf(stderr
, "ERROR: Shell FD limits are too low.\n");
246 fprintf(stderr
, "ERROR: ircd-ratbox reserves %d FDs, shell limits must be above this\n", MAX_BUFFER
);
251 #endif /* RLIMIT_FD_MAX */
252 maxconnections
= MAXCONNECTIONS
;
269 if((pid
= fork()) < 0)
277 /* Wait for initialization to finish, successfully or
278 * unsuccessfully. Until this point the child may still
279 * write to stdout/stderr.
281 if (read(pip
[0], &c
, 1) > 0)
296 static int printVersion
= 0;
298 struct lgetopt myopts
[] = {
299 {"dlinefile", &ConfigFileEntry
.dlinefile
,
300 STRING
, "File to use for dlines.conf"},
301 {"configfile", &ConfigFileEntry
.configfile
,
302 STRING
, "File to use for ircd.conf"},
303 {"klinefile", &ConfigFileEntry
.klinefile
,
304 STRING
, "File to use for kline.conf"},
305 {"xlinefile", &ConfigFileEntry
.xlinefile
,
306 STRING
, "File to use for xline.conf"},
307 {"resvfile", &ConfigFileEntry
.resvfile
,
308 STRING
, "File to use for resv.conf"},
309 {"logfile", &logFileName
,
310 STRING
, "File to use for ircd.log"},
311 {"pidfile", &pidFileName
,
312 STRING
, "File to use for process ID"},
313 {"foreground", &server_state_foreground
,
314 YESNO
, "Run in foreground (don't detach)"},
315 {"version", &printVersion
,
316 YESNO
, "Print version and exit"},
317 {"conftest", &testing_conf
,
318 YESNO
, "Test the configuration files and exit"},
319 {"help", NULL
, USAGE
, "Print this text"},
320 {NULL
, NULL
, STRING
, NULL
},
324 check_rehash(void *unused
)
327 * Check to see whether we have to rehash the configuration ..
343 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
344 "Got signal SIGUSR1, reloading ircd motd file");
351 * initalialize_global_set_options
355 * side effects - This sets all global set options needed
358 initialize_global_set_options(void)
360 memset(&GlobalSetOptions
, 0, sizeof(GlobalSetOptions
));
361 /* memset( &ConfigFileEntry, 0, sizeof(ConfigFileEntry)); */
363 GlobalSetOptions
.maxclients
= ServerInfo
.default_max_clients
;
365 if(GlobalSetOptions
.maxclients
> (maxconnections
- MAX_BUFFER
) || (GlobalSetOptions
.maxclients
<= 0))
366 GlobalSetOptions
.maxclients
= maxconnections
- MAX_BUFFER
;
368 GlobalSetOptions
.autoconn
= 1;
370 GlobalSetOptions
.spam_time
= MIN_JOIN_LEAVE_TIME
;
371 GlobalSetOptions
.spam_num
= MAX_JOIN_LEAVE_COUNT
;
373 if(ConfigFileEntry
.default_floodcount
)
374 GlobalSetOptions
.floodcount
= ConfigFileEntry
.default_floodcount
;
376 GlobalSetOptions
.floodcount
= 10;
378 split_servers
= ConfigChannel
.default_split_server_count
;
379 split_users
= ConfigChannel
.default_split_user_count
;
381 if(split_users
&& split_servers
382 && (ConfigChannel
.no_create_on_split
|| ConfigChannel
.no_join_on_split
))
388 GlobalSetOptions
.ident_timeout
= IDENT_TIMEOUT
;
390 rb_strlcpy(GlobalSetOptions
.operstring
,
391 ConfigFileEntry
.default_operstring
,
392 sizeof(GlobalSetOptions
.operstring
));
393 rb_strlcpy(GlobalSetOptions
.adminstring
,
394 ConfigFileEntry
.default_adminstring
,
395 sizeof(GlobalSetOptions
.adminstring
));
397 /* memset( &ConfigChannel, 0, sizeof(ConfigChannel)); */
399 /* End of global set options */
404 * initialize_server_capabs
410 initialize_server_capabs(void)
412 default_server_capabs
&= ~CAP_ZIP
;
419 * inputs - filename+path of pid file
421 * side effects - write the pid of the ircd to filename
424 write_pidfile(const char *filename
)
428 if((fb
= fopen(filename
, "w")))
430 unsigned int pid
= (unsigned int) getpid();
432 rb_snprintf(buff
, sizeof(buff
), "%u\n", pid
);
433 if((fputs(buff
, fb
) == -1))
435 ilog(L_MAIN
, "Error writing %u to pid file %s (%s)",
436 pid
, filename
, strerror(errno
));
443 ilog(L_MAIN
, "Error opening pid file %s", filename
);
450 * inputs - filename+path of pid file
452 * side effects - reads pid from pidfile and checks if ircd is in process
453 * list. if it is, gracefully exits
457 check_pidfile(const char *filename
)
463 /* Don't do logging here, since we don't have log() initialised */
464 if((fb
= fopen(filename
, "r")))
466 if(fgets(buff
, 20, fb
) != NULL
)
468 pidfromfile
= atoi(buff
);
469 if(!kill(pidfromfile
, 0))
471 printf("ircd: daemon is already running\n");
484 * side effects - setups corefile to system limits.
490 #ifdef HAVE_SYS_RESOURCE_H
491 struct rlimit rlim
; /* resource limits */
493 /* Set corefilesize to maximum */
494 if(!getrlimit(RLIMIT_CORE
, &rlim
))
496 rlim
.rlim_cur
= rlim
.rlim_max
;
497 setrlimit(RLIMIT_CORE
, &rlim
);
502 struct ev_entry
*check_splitmode_ev
= NULL
;
505 seed_with_urandom(void)
510 fd
= open("/dev/urandom", O_RDONLY
);
513 if(read(fd
, &seed
, sizeof(seed
)) == sizeof(seed
))
524 seed_with_clock(void)
526 const struct timeval
*tv
;
528 tv
= rb_current_time_tv();
529 srand(tv
->tv_sec
^ (tv
->tv_usec
| (getpid() << 20)));
533 seed_random(void *unused
)
536 if(rb_get_random(&seed
, sizeof(seed
)) == -1)
538 if(!seed_with_urandom())
548 * Initializes the IRCd.
550 * Inputs - number of commandline args, args themselves
552 * Side Effects - this is where the ircd gets going right now
555 main(int argc
, char *argv
[])
559 /* Check to see if the user is running us as root, which is a nono */
562 fprintf(stderr
, "Don't run ircd as root!!!\n");
567 * Setup corefile size immediately after boot -kre
571 /* It ain't random, but it ought to be a little harder to guess */
572 srand(SystemTime
.tv_sec
^ (SystemTime
.tv_usec
| (getpid() << 20)));
573 memset(&me
, 0, sizeof(me
));
574 memset(&meLocalUser
, 0, sizeof(meLocalUser
));
575 me
.localClient
= &meLocalUser
;
577 /* Make sure all lists are zeroed */
578 memset(&unknown_list
, 0, sizeof(unknown_list
));
579 memset(&lclient_list
, 0, sizeof(lclient_list
));
580 memset(&serv_list
, 0, sizeof(serv_list
));
581 memset(&global_serv_list
, 0, sizeof(global_serv_list
));
582 memset(&local_oper_list
, 0, sizeof(local_oper_list
));
583 memset(&oper_list
, 0, sizeof(oper_list
));
585 rb_dlinkAddTail(&me
, &me
.node
, &global_client_list
);
587 memset(&Count
, 0, sizeof(Count
));
588 memset(&ServerInfo
, 0, sizeof(ServerInfo
));
589 memset(&AdminInfo
, 0, sizeof(AdminInfo
));
590 memset(&ServerStats
, 0, sizeof(struct ServerStatistics
));
592 /* Initialise the channel capability usage counts... */
593 init_chcap_usage_counts();
595 ConfigFileEntry
.dpath
= DPATH
;
596 ConfigFileEntry
.configfile
= CPATH
; /* Server configuration file */
597 ConfigFileEntry
.klinefile
= KPATH
; /* Server kline file */
598 ConfigFileEntry
.dlinefile
= DLPATH
; /* dline file */
599 ConfigFileEntry
.xlinefile
= XPATH
;
600 ConfigFileEntry
.resvfile
= RESVPATH
;
601 ConfigFileEntry
.connect_timeout
= 30; /* Default to 30 */
603 umask(077); /* better safe than sorry --SRB */
605 parseargs(&argc
, &argv
, myopts
);
609 printf("ircd: version %s(%s)\n", ircd_version
, serno
);
613 if(chdir(ConfigFileEntry
.dpath
))
615 fprintf(stderr
, "Unable to chdir to %s: %s\n", ConfigFileEntry
.dpath
, strerror(errno
));
622 server_state_foreground
= 1;
626 server_state_foreground
= 1;
628 /* Make sure fd 0, 1 and 2 are in use -- jilles */
631 fd
= open("/dev/null", O_RDWR
);
632 } while (fd
< 2 && fd
!= -1);
638 /* Check if there is pidfile and daemon already running */
641 check_pidfile(pidFileName
);
643 if(!server_state_foreground
)
645 inotice("starting %s ...", ircd_version
);
648 /* Init the event subsystem */
650 rb_lib_init(ircd_log_cb
, ircd_restart_cb
, ircd_die_cb
, !server_state_foreground
, maxconnections
, DNODE_HEAP_SIZE
, FD_HEAP_SIZE
);
651 rb_linebuf_init(LINEBUF_HEAP_SIZE
);
658 clear_scache_hash_table(); /* server cache name table */
672 #ifndef STATIC_MODULES
673 load_core_modules(1);
675 init_auth(); /* Initialise the auth code */
676 init_resolver(); /* Needs to be setup before the io loop */
679 fprintf(stderr
, "\nBeginning config test\n");
680 read_conf_files(YES
); /* cold start init conf files */
682 #ifndef STATIC_MODULES
684 mod_add_path(MODULE_DIR
);
685 mod_add_path(MODULE_DIR
"/autoload");
690 initialize_server_capabs(); /* Set up default_server_capabs */
691 initialize_global_set_options();
693 if(ServerInfo
.name
== NULL
)
695 ierror("no server name specified in serverinfo block.");
698 rb_strlcpy(me
.name
, ServerInfo
.name
, sizeof(me
.name
));
700 if(ServerInfo
.sid
[0] == '\0')
702 ierror("no server sid specified in serverinfo block.");
705 strcpy(me
.id
, ServerInfo
.sid
);
708 /* serverinfo{} description must exist. If not, error out. */
709 if(ServerInfo
.description
== NULL
)
711 ierror("no server description specified in serverinfo block.");
714 rb_strlcpy(me
.info
, ServerInfo
.description
, sizeof(me
.info
));
716 if(ServerInfo
.ssl_cert
!= NULL
&& ServerInfo
.ssl_private_key
!= NULL
)
718 /* just do the rb_setup_ssl_server to validate the config */
719 if(!rb_setup_ssl_server(ServerInfo
.ssl_cert
, ServerInfo
.ssl_private_key
, ServerInfo
.ssl_dh_params
))
721 ilog(L_MAIN
, "WARNING: Unable to setup SSL.");
730 fprintf(stderr
, "\nConfig testing complete.\n");
732 return 0; /* Why? We want the launcher to exit out. */
739 startup_time
= rb_current_time();
740 add_to_client_hash(me
.name
, &me
);
741 add_to_id_hash(me
.id
, &me
);
742 me
.serv
->nameinfo
= scache_connect(me
.name
, me
.info
, 0);
744 rb_dlinkAddAlloc(&me
, &global_serv_list
);
746 construct_umodebuf();
749 write_pidfile(pidFileName
);
753 ilog(L_MAIN
, "Server Ready");
755 /* We want try_connections to be called as soon as possible now! -- adrian */
756 /* No, 'cause after a restart it would cause all sorts of nick collides */
757 /* um. by waiting even longer, that just means we have even *more*
758 * nick collisions. what a stupid idea. set an event for the IO loop --fl
760 rb_event_addish("try_connections", try_connections
, NULL
, STARTUP_CONNECTIONS_TIME
);
761 rb_event_addonce("try_connections_startup", try_connections
, NULL
, 0);
762 rb_event_add("check_rehash", check_rehash
, NULL
, 1);
763 rb_event_addish("reseed_srand", seed_random
, NULL
, 300); /* reseed every 10 minutes */
766 check_splitmode_ev
= rb_event_add("check_splitmode", check_splitmode
, NULL
, 2);
768 print_startup(getpid());