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"
69 /* /quote set variables */
70 struct SetOptions GlobalSetOptions
;
72 /* configuration set from ircd.conf */
73 struct config_file_entry ConfigFileEntry
;
74 /* server info set from ircd.conf */
75 struct server_info ServerInfo
;
76 /* admin info set from ircd.conf */
77 struct admin_info AdminInfo
;
80 struct ServerStatistics ServerStats
;
83 struct timeval SystemTime
;
84 struct Client me
; /* That's me */
85 struct LocalUser meLocalUser
; /* That's also part of me */
87 rb_dlink_list lclient_list
= { NULL
, NULL
, 0 };
88 rb_dlink_list global_client_list
= { NULL
, NULL
, 0 };
89 rb_dlink_list global_channel_list
= { NULL
, NULL
, 0 };
91 rb_dlink_list unknown_list
; /* unknown clients ON this server only */
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 */
99 int default_server_capabs
= CAP_MASK
;
107 unsigned long initialVMTop
= 0; /* top of virtual memory at init */
108 const char *logFileName
= LPATH
;
109 const char *pidFileName
= PPATH
;
113 int dorehashbans
= 0;
115 int kline_queued
= 0;
116 int server_state_foreground
= 0;
117 int opers_see_all_users
= 0;
121 int testing_conf
= 0;
123 struct config_channel_entry ConfigChannel
;
129 rb_bh
*client_heap
= NULL
;
130 rb_bh
*lclient_heap
= NULL
;
131 rb_bh
*pclient_heap
= NULL
;
133 char current_uid
[IDLEN
];
138 rb_bh
*patricia_heap
;
145 ircd_shutdown(const char *reason
)
147 struct Client
*target_p
;
150 RB_DLINK_FOREACH(ptr
, lclient_list
.head
)
152 target_p
= ptr
->data
;
154 sendto_one(target_p
, ":%s NOTICE %s :Server Terminating. %s",
155 me
.name
, target_p
->name
, reason
);
158 RB_DLINK_FOREACH(ptr
, serv_list
.head
)
160 target_p
= ptr
->data
;
162 sendto_one(target_p
, ":%s ERROR :Terminated by %s",
166 ilog(L_MAIN
, "Server Terminating. %s", reason
);
174 * print_startup - print startup information
177 print_startup(int pid
)
179 inotice("now running in %s mode from %s as pid %d ...",
180 !server_state_foreground
? "background" : "foreground",
181 ConfigFileEntry
.dpath
, pid
);
183 /* let the parent process know the initialization was successful
185 if (!server_state_foreground
)
190 open("/dev/null", O_RDWR
);
196 ircd_log_cb(const char *str
)
198 ilog(L_MAIN
, "%s", str
);
202 ircd_restart_cb(const char *str
)
208 * Why EXIT_FAILURE here?
209 * Because if ircd_die_cb() is called it's because of a fatal
210 * error inside libcharybdis, and we don't know how to handle the
211 * exception, so it is logical to return a FAILURE exit code here.
215 ircd_die_cb(const char *str
)
219 /* Try to get the message out to currently logged in operators. */
220 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
, "Server panic! %s", str
);
221 inotice("server panic: %s", str
);
231 * inputs - boot_daemon flag
233 * side effects - if boot_daemon flag is not set, don't daemonize
238 #if defined(RLIMIT_NOFILE) && defined(HAVE_SYS_RESOURCE_H)
241 if(!getrlimit(RLIMIT_NOFILE
, &limit
))
243 maxconnections
= limit
.rlim_cur
;
244 if(maxconnections
<= MAX_BUFFER
)
246 fprintf(stderr
, "ERROR: Shell FD limits are too low.\n");
247 fprintf(stderr
, "ERROR: ircd-ratbox reserves %d FDs, shell limits must be above this\n", MAX_BUFFER
);
252 #endif /* RLIMIT_FD_MAX */
253 maxconnections
= MAXCONNECTIONS
;
270 if((pid
= fork()) < 0)
278 /* Wait for initialization to finish, successfully or
279 * unsuccessfully. Until this point the child may still
280 * write to stdout/stderr.
282 if (read(pip
[0], &c
, 1) > 0)
297 static int printVersion
= 0;
299 struct lgetopt myopts
[] = {
300 {"dlinefile", &ConfigFileEntry
.dlinefile
,
301 STRING
, "File to use for dlines.conf"},
302 {"configfile", &ConfigFileEntry
.configfile
,
303 STRING
, "File to use for ircd.conf"},
304 {"klinefile", &ConfigFileEntry
.klinefile
,
305 STRING
, "File to use for kline.conf"},
306 {"xlinefile", &ConfigFileEntry
.xlinefile
,
307 STRING
, "File to use for xline.conf"},
308 {"resvfile", &ConfigFileEntry
.resvfile
,
309 STRING
, "File to use for resv.conf"},
310 {"logfile", &logFileName
,
311 STRING
, "File to use for ircd.log"},
312 {"pidfile", &pidFileName
,
313 STRING
, "File to use for process ID"},
314 {"foreground", &server_state_foreground
,
315 YESNO
, "Run in foreground (don't detach)"},
316 {"version", &printVersion
,
317 YESNO
, "Print version and exit"},
318 {"conftest", &testing_conf
,
319 YESNO
, "Test the configuration files and exit"},
320 {"help", NULL
, USAGE
, "Print this text"},
321 {NULL
, NULL
, STRING
, NULL
},
325 check_rehash(void *unused
)
328 * Check to see whether we have to rehash the configuration ..
344 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
345 "Got signal SIGUSR1, reloading ircd motd file");
352 * initalialize_global_set_options
356 * side effects - This sets all global set options needed
359 initialize_global_set_options(void)
361 memset(&GlobalSetOptions
, 0, sizeof(GlobalSetOptions
));
362 /* memset( &ConfigFileEntry, 0, sizeof(ConfigFileEntry)); */
364 GlobalSetOptions
.maxclients
= ServerInfo
.default_max_clients
;
366 if(GlobalSetOptions
.maxclients
> (maxconnections
- MAX_BUFFER
) || (GlobalSetOptions
.maxclients
<= 0))
367 GlobalSetOptions
.maxclients
= maxconnections
- MAX_BUFFER
;
369 GlobalSetOptions
.autoconn
= 1;
371 GlobalSetOptions
.spam_time
= MIN_JOIN_LEAVE_TIME
;
372 GlobalSetOptions
.spam_num
= MAX_JOIN_LEAVE_COUNT
;
374 if(ConfigFileEntry
.default_floodcount
)
375 GlobalSetOptions
.floodcount
= ConfigFileEntry
.default_floodcount
;
377 GlobalSetOptions
.floodcount
= 10;
379 split_servers
= ConfigChannel
.default_split_server_count
;
380 split_users
= ConfigChannel
.default_split_user_count
;
382 if(split_users
&& split_servers
383 && (ConfigChannel
.no_create_on_split
|| ConfigChannel
.no_join_on_split
))
389 GlobalSetOptions
.ident_timeout
= IDENT_TIMEOUT
;
391 rb_strlcpy(GlobalSetOptions
.operstring
,
392 ConfigFileEntry
.default_operstring
,
393 sizeof(GlobalSetOptions
.operstring
));
394 rb_strlcpy(GlobalSetOptions
.adminstring
,
395 ConfigFileEntry
.default_adminstring
,
396 sizeof(GlobalSetOptions
.adminstring
));
398 /* memset( &ConfigChannel, 0, sizeof(ConfigChannel)); */
400 /* End of global set options */
405 * initialize_server_capabs
411 initialize_server_capabs(void)
413 default_server_capabs
&= ~CAP_ZIP
;
420 * inputs - filename+path of pid file
422 * side effects - write the pid of the ircd to filename
425 write_pidfile(const char *filename
)
429 if((fb
= fopen(filename
, "w")))
431 unsigned int pid
= (unsigned int) getpid();
433 rb_snprintf(buff
, sizeof(buff
), "%u\n", pid
);
434 if((fputs(buff
, fb
) == -1))
436 ilog(L_MAIN
, "Error writing %u to pid file %s (%s)",
437 pid
, filename
, strerror(errno
));
444 ilog(L_MAIN
, "Error opening pid file %s", filename
);
451 * inputs - filename+path of pid file
453 * side effects - reads pid from pidfile and checks if ircd is in process
454 * list. if it is, gracefully exits
458 check_pidfile(const char *filename
)
464 /* Don't do logging here, since we don't have log() initialised */
465 if((fb
= fopen(filename
, "r")))
467 if(fgets(buff
, 20, fb
) != NULL
)
469 pidfromfile
= atoi(buff
);
470 if(!kill(pidfromfile
, 0))
472 printf("ircd: daemon is already running\n");
485 * side effects - setups corefile to system limits.
491 #ifdef HAVE_SYS_RESOURCE_H
492 struct rlimit rlim
; /* resource limits */
494 /* Set corefilesize to maximum */
495 if(!getrlimit(RLIMIT_CORE
, &rlim
))
497 rlim
.rlim_cur
= rlim
.rlim_max
;
498 setrlimit(RLIMIT_CORE
, &rlim
);
503 struct ev_entry
*check_splitmode_ev
= NULL
;
506 seed_with_urandom(void)
511 fd
= open("/dev/urandom", O_RDONLY
);
514 if(read(fd
, &seed
, sizeof(seed
)) == sizeof(seed
))
525 seed_with_clock(void)
527 const struct timeval
*tv
;
529 tv
= rb_current_time_tv();
530 srand(tv
->tv_sec
^ (tv
->tv_usec
| (getpid() << 20)));
534 seed_random(void *unused
)
537 if(rb_get_random(&seed
, sizeof(seed
)) == -1)
539 if(!seed_with_urandom())
549 * Initializes the IRCd.
551 * Inputs - number of commandline args, args themselves
553 * Side Effects - this is where the ircd gets going right now
556 main(int argc
, char *argv
[])
560 /* Check to see if the user is running us as root, which is a nono */
563 fprintf(stderr
, "Don't run ircd as root!!!\n");
568 * Setup corefile size immediately after boot -kre
572 /* It ain't random, but it ought to be a little harder to guess */
573 srand(SystemTime
.tv_sec
^ (SystemTime
.tv_usec
| (getpid() << 20)));
574 memset(&me
, 0, sizeof(me
));
575 memset(&meLocalUser
, 0, sizeof(meLocalUser
));
576 me
.localClient
= &meLocalUser
;
578 /* Make sure all lists are zeroed */
579 memset(&unknown_list
, 0, sizeof(unknown_list
));
580 memset(&lclient_list
, 0, sizeof(lclient_list
));
581 memset(&serv_list
, 0, sizeof(serv_list
));
582 memset(&global_serv_list
, 0, sizeof(global_serv_list
));
583 memset(&local_oper_list
, 0, sizeof(local_oper_list
));
584 memset(&oper_list
, 0, sizeof(oper_list
));
586 rb_dlinkAddTail(&me
, &me
.node
, &global_client_list
);
588 memset(&Count
, 0, sizeof(Count
));
589 memset(&ServerInfo
, 0, sizeof(ServerInfo
));
590 memset(&AdminInfo
, 0, sizeof(AdminInfo
));
591 memset(&ServerStats
, 0, sizeof(struct ServerStatistics
));
593 /* Initialise the channel capability usage counts... */
594 init_chcap_usage_counts();
596 ConfigFileEntry
.dpath
= DPATH
;
597 ConfigFileEntry
.configfile
= CPATH
; /* Server configuration file */
598 ConfigFileEntry
.klinefile
= KPATH
; /* Server kline file */
599 ConfigFileEntry
.dlinefile
= DLPATH
; /* dline file */
600 ConfigFileEntry
.xlinefile
= XPATH
;
601 ConfigFileEntry
.resvfile
= RESVPATH
;
602 ConfigFileEntry
.connect_timeout
= 30; /* Default to 30 */
604 umask(077); /* better safe than sorry --SRB */
606 parseargs(&argc
, &argv
, myopts
);
610 printf("ircd: version %s(%s)\n", ircd_version
, serno
);
614 if(chdir(ConfigFileEntry
.dpath
))
616 fprintf(stderr
, "Unable to chdir to %s: %s\n", ConfigFileEntry
.dpath
, strerror(errno
));
623 server_state_foreground
= 1;
627 server_state_foreground
= 1;
629 /* Make sure fd 0, 1 and 2 are in use -- jilles */
632 fd
= open("/dev/null", O_RDWR
);
633 } while (fd
< 2 && fd
!= -1);
639 /* Check if there is pidfile and daemon already running */
642 check_pidfile(pidFileName
);
644 if(!server_state_foreground
)
646 inotice("starting %s ...", ircd_version
);
649 /* Init the event subsystem */
651 rb_lib_init(ircd_log_cb
, ircd_restart_cb
, ircd_die_cb
, !server_state_foreground
, maxconnections
, DNODE_HEAP_SIZE
, FD_HEAP_SIZE
);
652 rb_linebuf_init(LINEBUF_HEAP_SIZE
);
659 clear_scache_hash_table(); /* server cache name table */
673 #ifndef STATIC_MODULES
674 load_core_modules(1);
676 init_auth(); /* Initialise the auth code */
677 init_resolver(); /* Needs to be setup before the io loop */
680 fprintf(stderr
, "\nBeginning config test\n");
681 read_conf_files(YES
); /* cold start init conf files */
683 #ifndef STATIC_MODULES
685 mod_add_path(MODULE_DIR
);
686 mod_add_path(MODULE_DIR
"/autoload");
691 initialize_server_capabs(); /* Set up default_server_capabs */
692 initialize_global_set_options();
694 if(ServerInfo
.name
== NULL
)
696 ierror("no server name specified in serverinfo block.");
699 rb_strlcpy(me
.name
, ServerInfo
.name
, sizeof(me
.name
));
701 if(ServerInfo
.sid
[0] == '\0')
703 ierror("no server sid specified in serverinfo block.");
706 strcpy(me
.id
, ServerInfo
.sid
);
709 /* serverinfo{} description must exist. If not, error out. */
710 if(ServerInfo
.description
== NULL
)
712 ierror("no server description specified in serverinfo block.");
715 rb_strlcpy(me
.info
, ServerInfo
.description
, sizeof(me
.info
));
717 if(ServerInfo
.ssl_cert
!= NULL
&& ServerInfo
.ssl_private_key
!= NULL
)
719 /* just do the rb_setup_ssl_server to validate the config */
720 if(!rb_setup_ssl_server(ServerInfo
.ssl_cert
, ServerInfo
.ssl_private_key
, ServerInfo
.ssl_dh_params
))
722 ilog(L_MAIN
, "WARNING: Unable to setup SSL.");
731 fprintf(stderr
, "\nConfig testing complete.\n");
733 return 0; /* Why? We want the launcher to exit out. */
740 startup_time
= rb_current_time();
741 add_to_client_hash(me
.name
, &me
);
742 add_to_id_hash(me
.id
, &me
);
743 me
.serv
->nameinfo
= scache_connect(me
.name
, me
.info
, 0);
745 rb_dlinkAddAlloc(&me
, &global_serv_list
);
747 construct_umodebuf();
748 construct_noparam_modes();
751 write_pidfile(pidFileName
);
755 ilog(L_MAIN
, "Server Ready");
757 /* We want try_connections to be called as soon as possible now! -- adrian */
758 /* No, 'cause after a restart it would cause all sorts of nick collides */
759 /* um. by waiting even longer, that just means we have even *more*
760 * nick collisions. what a stupid idea. set an event for the IO loop --fl
762 rb_event_addish("try_connections", try_connections
, NULL
, STARTUP_CONNECTIONS_TIME
);
763 rb_event_addonce("try_connections_startup", try_connections
, NULL
, 0);
764 rb_event_add("check_rehash", check_rehash
, NULL
, 1);
765 rb_event_addish("reseed_srand", seed_random
, NULL
, 300); /* reseed every 10 minutes */
768 check_splitmode_ev
= rb_event_add("check_splitmode", check_splitmode
, NULL
, 2);
770 print_startup(getpid());