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"
37 #include "irc_string.h"
38 #include "ircd_signal.h"
39 #include "sprintf_irc.h"
41 #include "msg.h" /* msgtab */
50 #include "s_serv.h" /* try_connections */
55 #include "supported.h"
59 #include "ircd_getopt.h"
63 #include "s_newconf.h"
66 #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 timeval SystemTime
;
85 struct Client me
; /* That's me */
86 struct LocalUser meLocalUser
; /* That's also part of me */
88 rb_dlink_list lclient_list
= { NULL
, NULL
, 0 };
89 rb_dlink_list global_client_list
= { NULL
, NULL
, 0 };
90 rb_dlink_list global_channel_list
= { NULL
, NULL
, 0 };
92 rb_dlink_list unknown_list
; /* unknown clients ON this server only */
93 rb_dlink_list serv_list
; /* local servers to this server ONLY */
94 rb_dlink_list global_serv_list
; /* global servers on the network */
95 rb_dlink_list local_oper_list
; /* our opers, duplicated in lclient_list */
96 rb_dlink_list oper_list
; /* network opers */
100 int default_server_capabs
= CAP_MASK
;
108 unsigned long initialVMTop
= 0; /* top of virtual memory at init */
109 const char *logFileName
= LPATH
;
110 const char *pidFileName
= PPATH
;
114 int dorehashbans
= 0;
116 int kline_queued
= 0;
117 int server_state_foreground
= 0;
118 int opers_see_all_users
= 0;
122 int testing_conf
= 0;
124 struct config_channel_entry ConfigChannel
;
130 rb_bh
*client_heap
= NULL
;
131 rb_bh
*lclient_heap
= NULL
;
132 rb_bh
*pclient_heap
= NULL
;
134 char current_uid
[IDLEN
];
139 rb_bh
*patricia_heap
;
146 * print_startup - print startup information
149 print_startup(int pid
)
151 inotice("now running in %s mode from %s as pid %d ...",
152 !server_state_foreground
? "background" : "foreground",
153 ConfigFileEntry
.dpath
, pid
);
155 /* let the parent process know the initialization was successful
157 if (!server_state_foreground
)
162 open("/dev/null", O_RDWR
);
168 ircd_log_cb(const char *str
)
170 ilog(L_MAIN
, "%s", str
);
174 ircd_restart_cb(const char *str
)
180 * Why EXIT_FAILURE here?
181 * Because if ircd_die_cb() is called it's because of a fatal
182 * error inside libcharybdis, and we don't know how to handle the
183 * exception, so it is logical to return a FAILURE exit code here.
187 ircd_die_cb(const char *str
)
191 /* Try to get the message out to currently logged in operators. */
192 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
, "Server panic! %s", str
);
193 inotice("server panic: %s", str
);
203 * inputs - boot_daemon flag
205 * side effects - if boot_daemon flag is not set, don't daemonize
210 #if defined(RLIMIT_NOFILE) && defined(HAVE_SYS_RESOURCE_H)
213 if(!getrlimit(RLIMIT_NOFILE
, &limit
))
215 maxconnections
= limit
.rlim_cur
;
216 if(maxconnections
<= MAX_BUFFER
)
218 fprintf(stderr
, "ERROR: Shell FD limits are too low.\n");
219 fprintf(stderr
, "ERROR: ircd-ratbox reserves %d FDs, shell limits must be above this\n", MAX_BUFFER
);
224 #endif /* RLIMIT_FD_MAX */
225 maxconnections
= MAXCONNECTIONS
;
242 if((pid
= fork()) < 0)
250 /* Wait for initialization to finish, successfully or
251 * unsuccessfully. Until this point the child may still
252 * write to stdout/stderr.
254 if (read(pip
[0], &c
, 1) > 0)
269 static int printVersion
= 0;
271 struct lgetopt myopts
[] = {
272 {"dlinefile", &ConfigFileEntry
.dlinefile
,
273 STRING
, "File to use for dlines.conf"},
274 {"configfile", &ConfigFileEntry
.configfile
,
275 STRING
, "File to use for ircd.conf"},
276 {"klinefile", &ConfigFileEntry
.klinefile
,
277 STRING
, "File to use for kline.conf"},
278 {"xlinefile", &ConfigFileEntry
.xlinefile
,
279 STRING
, "File to use for xline.conf"},
280 {"resvfile", &ConfigFileEntry
.resvfile
,
281 STRING
, "File to use for resv.conf"},
282 {"logfile", &logFileName
,
283 STRING
, "File to use for ircd.log"},
284 {"pidfile", &pidFileName
,
285 STRING
, "File to use for process ID"},
286 {"foreground", &server_state_foreground
,
287 YESNO
, "Run in foreground (don't detach)"},
288 {"version", &printVersion
,
289 YESNO
, "Print version and exit"},
290 {"conftest", &testing_conf
,
291 YESNO
, "Test the configuration files and exit"},
292 {"help", NULL
, USAGE
, "Print this text"},
293 {NULL
, NULL
, STRING
, NULL
},
297 check_rehash(void *unused
)
300 * Check to see whether we have to rehash the configuration ..
316 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
317 "Got signal SIGUSR1, reloading ircd motd file");
318 free_cachefile(user_motd
);
319 user_motd
= cache_file(MPATH
, "ircd.motd", 0);
325 * initalialize_global_set_options
329 * side effects - This sets all global set options needed
332 initialize_global_set_options(void)
334 memset(&GlobalSetOptions
, 0, sizeof(GlobalSetOptions
));
335 /* memset( &ConfigFileEntry, 0, sizeof(ConfigFileEntry)); */
337 GlobalSetOptions
.maxclients
= ServerInfo
.default_max_clients
;
339 if(GlobalSetOptions
.maxclients
> (maxconnections
- MAX_BUFFER
))
340 GlobalSetOptions
.maxclients
= maxconnections
- MAX_BUFFER
;
342 GlobalSetOptions
.autoconn
= 1;
344 GlobalSetOptions
.spam_time
= MIN_JOIN_LEAVE_TIME
;
345 GlobalSetOptions
.spam_num
= MAX_JOIN_LEAVE_COUNT
;
347 if(ConfigFileEntry
.default_floodcount
)
348 GlobalSetOptions
.floodcount
= ConfigFileEntry
.default_floodcount
;
350 GlobalSetOptions
.floodcount
= 10;
352 split_servers
= ConfigChannel
.default_split_server_count
;
353 split_users
= ConfigChannel
.default_split_user_count
;
355 if(split_users
&& split_servers
356 && (ConfigChannel
.no_create_on_split
|| ConfigChannel
.no_join_on_split
))
362 GlobalSetOptions
.ident_timeout
= IDENT_TIMEOUT
;
364 strlcpy(GlobalSetOptions
.operstring
,
365 ConfigFileEntry
.default_operstring
,
366 sizeof(GlobalSetOptions
.operstring
));
367 strlcpy(GlobalSetOptions
.adminstring
,
368 ConfigFileEntry
.default_adminstring
,
369 sizeof(GlobalSetOptions
.adminstring
));
371 /* memset( &ConfigChannel, 0, sizeof(ConfigChannel)); */
373 /* End of global set options */
378 * initialize_server_capabs
384 initialize_server_capabs(void)
386 default_server_capabs
&= ~CAP_ZIP
;
393 * inputs - filename+path of pid file
395 * side effects - write the pid of the ircd to filename
398 write_pidfile(const char *filename
)
402 if((fb
= fopen(filename
, "w")))
404 unsigned int pid
= (unsigned int) getpid();
406 rb_snprintf(buff
, sizeof(buff
), "%u\n", pid
);
407 if((fputs(buff
, fb
) == -1))
409 ilog(L_MAIN
, "Error writing %u to pid file %s (%s)",
410 pid
, filename
, strerror(errno
));
417 ilog(L_MAIN
, "Error opening pid file %s", filename
);
424 * inputs - filename+path of pid file
426 * side effects - reads pid from pidfile and checks if ircd is in process
427 * list. if it is, gracefully exits
431 check_pidfile(const char *filename
)
437 /* Don't do logging here, since we don't have log() initialised */
438 if((fb
= fopen(filename
, "r")))
440 if(fgets(buff
, 20, fb
) != NULL
)
442 pidfromfile
= atoi(buff
);
443 if(!kill(pidfromfile
, 0))
445 printf("ircd: daemon is already running\n");
458 * side effects - setups corefile to system limits.
464 #ifdef HAVE_SYS_RESOURCE_H
465 struct rlimit rlim
; /* resource limits */
467 /* Set corefilesize to maximum */
468 if(!getrlimit(RLIMIT_CORE
, &rlim
))
470 rlim
.rlim_cur
= rlim
.rlim_max
;
471 setrlimit(RLIMIT_CORE
, &rlim
);
476 struct ev_entry
*check_splitmode_ev
= NULL
;
481 * Initializes the IRCd.
483 * Inputs - number of commandline args, args themselves
485 * Side Effects - this is where the ircd gets going right now
488 main(int argc
, char *argv
[])
492 /* Check to see if the user is running us as root, which is a nono */
495 fprintf(stderr
, "Don't run ircd as root!!!\n");
500 * Setup corefile size immediately after boot -kre
504 /* It ain't random, but it ought to be a little harder to guess */
505 srand(SystemTime
.tv_sec
^ (SystemTime
.tv_usec
| (getpid() << 20)));
506 memset(&me
, 0, sizeof(me
));
507 memset(&meLocalUser
, 0, sizeof(meLocalUser
));
508 me
.localClient
= &meLocalUser
;
510 /* Make sure all lists are zeroed */
511 memset(&unknown_list
, 0, sizeof(unknown_list
));
512 memset(&lclient_list
, 0, sizeof(lclient_list
));
513 memset(&serv_list
, 0, sizeof(serv_list
));
514 memset(&global_serv_list
, 0, sizeof(global_serv_list
));
515 memset(&local_oper_list
, 0, sizeof(local_oper_list
));
516 memset(&oper_list
, 0, sizeof(oper_list
));
518 rb_dlinkAddTail(&me
, &me
.node
, &global_client_list
);
520 memset(&Count
, 0, sizeof(Count
));
521 memset(&ServerInfo
, 0, sizeof(ServerInfo
));
522 memset(&AdminInfo
, 0, sizeof(AdminInfo
));
523 memset(&ServerStats
, 0, sizeof(struct ServerStatistics
));
525 /* Initialise the channel capability usage counts... */
526 init_chcap_usage_counts();
528 ConfigFileEntry
.dpath
= DPATH
;
529 ConfigFileEntry
.configfile
= CPATH
; /* Server configuration file */
530 ConfigFileEntry
.klinefile
= KPATH
; /* Server kline file */
531 ConfigFileEntry
.dlinefile
= DLPATH
; /* dline file */
532 ConfigFileEntry
.xlinefile
= XPATH
;
533 ConfigFileEntry
.resvfile
= RESVPATH
;
534 ConfigFileEntry
.connect_timeout
= 30; /* Default to 30 */
536 umask(077); /* better safe than sorry --SRB */
538 parseargs(&argc
, &argv
, myopts
);
542 printf("ircd: version %s\n", ircd_version
);
546 if(chdir(ConfigFileEntry
.dpath
))
548 fprintf(stderr
, "Unable to chdir to %s: %s\n", ConfigFileEntry
.dpath
, strerror(errno
));
555 server_state_foreground
= 1;
559 server_state_foreground
= 1;
561 /* Make sure fd 0, 1 and 2 are in use -- jilles */
564 fd
= open("/dev/null", O_RDWR
);
565 } while (fd
< 2 && fd
!= -1);
571 /* Check if there is pidfile and daemon already running */
574 check_pidfile(pidFileName
);
576 if(!server_state_foreground
)
578 inotice("starting %s ...", ircd_version
);
581 /* Init the event subsystem */
583 rb_lib_init(ircd_log_cb
, ircd_restart_cb
, ircd_die_cb
, !server_state_foreground
, maxconnections
, DNODE_HEAP_SIZE
, FD_HEAP_SIZE
);
584 rb_linebuf_init(LINEBUF_HEAP_SIZE
);
591 clear_scache_hash_table(); /* server cache name table */
605 #ifndef STATIC_MODULES
606 load_core_modules(1);
608 init_auth(); /* Initialise the auth code */
609 init_resolver(); /* Needs to be setup before the io loop */
612 fprintf(stderr
, "\nBeginning config test\n");
613 read_conf_files(YES
); /* cold start init conf files */
615 #ifndef STATIC_MODULES
617 mod_add_path(MODULE_DIR
);
618 mod_add_path(MODULE_DIR
"/autoload");
623 initialize_server_capabs(); /* Set up default_server_capabs */
624 initialize_global_set_options();
626 if(ServerInfo
.name
== NULL
)
628 ierror("no server name specified in serverinfo block.");
631 strlcpy(me
.name
, ServerInfo
.name
, sizeof(me
.name
));
633 if(ServerInfo
.sid
[0] == '\0')
635 ierror("no server sid specified in serverinfo block.");
638 strcpy(me
.id
, ServerInfo
.sid
);
641 /* serverinfo{} description must exist. If not, error out. */
642 if(ServerInfo
.description
== NULL
)
644 ierror("no server description specified in serverinfo block.");
647 strlcpy(me
.info
, ServerInfo
.description
, sizeof(me
.info
));
649 if(ServerInfo
.ssl_cert
!= NULL
&& ServerInfo
.ssl_private_key
!= NULL
)
651 /* just do the rb_setup_ssl_server to validate the config */
652 if(!rb_setup_ssl_server(ServerInfo
.ssl_cert
, ServerInfo
.ssl_private_key
, ServerInfo
.ssl_dh_params
))
654 ilog(L_MAIN
, "WARNING: Unable to setup SSL.");
663 fprintf(stderr
, "\nConfig testing complete.\n");
665 return 0; /* Why? We want the launcher to exit out. */
672 startup_time
= rb_current_time();
673 add_to_client_hash(me
.name
, &me
);
674 add_to_id_hash(me
.id
, &me
);
675 me
.serv
->nameinfo
= scache_connect(me
.name
, me
.info
, 0);
677 rb_dlinkAddAlloc(&me
, &global_serv_list
);
679 construct_umodebuf();
682 write_pidfile(pidFileName
);
686 ilog(L_MAIN
, "Server Ready");
688 rb_event_addish("cleanup_glines", cleanup_glines
, NULL
, CLEANUP_GLINES_TIME
);
690 /* We want try_connections to be called as soon as possible now! -- adrian */
691 /* No, 'cause after a restart it would cause all sorts of nick collides */
692 /* um. by waiting even longer, that just means we have even *more*
693 * nick collisions. what a stupid idea. set an event for the IO loop --fl
695 rb_event_addish("try_connections", try_connections
, NULL
, STARTUP_CONNECTIONS_TIME
);
696 rb_event_addonce("try_connections_startup", try_connections
, NULL
, 0);
698 /* Setup the timeout check. I'll shift it later :) -- adrian */
699 rb_event_addish("rb_checktimeouts", rb_checktimeouts
, NULL
, 1);
701 rb_event_add("check_rehash", check_rehash
, NULL
, 1);
704 check_splitmode_ev
= rb_event_add("check_splitmode", check_splitmode
, NULL
, 2);
706 print_startup(getpid());