]> jfr.im git - solanum.git/blob - src/ircd.c
Some global structs cleaned up a bit in their initalization and declarations.
[solanum.git] / src / ircd.c
1 /*
2 * ircd-ratbox: A slightly useful ircd.
3 * ircd.c: Starts up and runs the ircd.
4 *
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
8 *
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.
13 *
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.
18 *
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
22 * USA
23 *
24 * $Id: ircd.c 3380 2007-04-03 22:25:11Z jilles $
25 */
26
27 #include "ratbox_lib.h"
28 #include "stdinc.h"
29 #include "setup.h"
30 #include "config.h"
31 #include "ircd.h"
32 #include "channel.h"
33 #include "class.h"
34 #include "client.h"
35 #include "common.h"
36 #include "hash.h"
37 #include "irc_string.h"
38 #include "ircd_signal.h"
39 #include "sprintf_irc.h"
40 #include "s_gline.h"
41 #include "msg.h" /* msgtab */
42 #include "hostmask.h"
43 #include "numeric.h"
44 #include "parse.h"
45 #include "res.h"
46 #include "restart.h"
47 #include "s_auth.h"
48 #include "s_conf.h"
49 #include "logger.h"
50 #include "s_serv.h" /* try_connections */
51 #include "s_user.h"
52 #include "s_stats.h"
53 #include "scache.h"
54 #include "send.h"
55 #include "supported.h"
56 #include "whowas.h"
57 #include "modules.h"
58 #include "hook.h"
59 #include "ircd_getopt.h"
60 #include "newconf.h"
61 #include "reject.h"
62 #include "s_conf.h"
63 #include "s_newconf.h"
64 #include "cache.h"
65 #include "monitor.h"
66 #include "patchlevel.h"
67 #include "serno.h"
68
69 /*
70 * Try and find the correct name to use with getrlimit() for setting the max.
71 * number of files allowed to be open by this process.
72 */
73 int _charybdis_data_version = CHARYBDIS_DV;
74
75 extern int ServerRunning;
76 extern struct LocalUser meLocalUser;
77 extern char **myargv;
78
79 int maxconnections; /* XXX */
80
81 /* /quote set variables */
82 struct SetOptions GlobalSetOptions;
83
84 /* configuration set from ircd.conf */
85 struct config_file_entry ConfigFileEntry;
86 /* server info set from ircd.conf */
87 struct server_info ServerInfo;
88 /* admin info set from ircd.conf */
89 struct admin_info AdminInfo;
90
91 struct Counter Count;
92 struct ServerStatistics ServerStats;
93
94 /*
95 * print_startup - print startup information
96 */
97 static void
98 print_startup(int pid)
99 {
100 inotice("now running in %s mode from %s as pid %d ...",
101 !server_state_foreground ? "background" : "foreground",
102 ConfigFileEntry.dpath, pid);
103
104 /* let the parent process know the initialization was successful
105 * -- jilles */
106 if (!server_state_foreground)
107 write(0, ".", 1);
108 fclose(stdin);
109 fclose(stdout);
110 fclose(stderr);
111 open("/dev/null", O_RDWR);
112 dup2(0, 1);
113 dup2(0, 2);
114 }
115
116 static void
117 ircd_log_cb(const char *str)
118 {
119 ilog(L_MAIN, "%s", str);
120 }
121
122 static void
123 ircd_restart_cb(const char *str)
124 {
125 restart(str);
126 }
127
128 /*
129 * Why EXIT_FAILURE here?
130 * Because if ircd_die_cb() is called it's because of a fatal
131 * error inside libcharybdis, and we don't know how to handle the
132 * exception, so it is logical to return a FAILURE exit code here.
133 * --nenolod
134 */
135 static void
136 ircd_die_cb(const char *str)
137 {
138 /* Try to get the message out to currently logged in operators. */
139 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, "Server panic! %s", str);
140 inotice("server panic: %s", str);
141
142 unlink(pidFileName);
143 exit(EXIT_FAILURE);
144 }
145
146 /*
147 * init_sys
148 *
149 * inputs - boot_daemon flag
150 * output - none
151 * side effects - if boot_daemon flag is not set, don't daemonize
152 */
153 static void
154 init_sys(void)
155 {
156 #if defined(RLIMIT_NOFILE) && defined(HAVE_SYS_RESOURCE_H)
157 struct rlimit limit;
158
159 if(!getrlimit(RLIMIT_NOFILE, &limit))
160 {
161 limit.rlim_cur = limit.rlim_max; /* make soft limit the max */
162 if(setrlimit(RLIMIT_NOFILE, &limit) == -1)
163 {
164 fprintf(stderr, "error setting max fd's to %ld\n", (long) limit.rlim_cur);
165 exit(EXIT_FAILURE);
166 }
167 }
168
169 maxconnections = limit.rlim_cur;
170 #endif /* RLIMIT_NOFILE */
171 }
172
173 static int
174 make_daemon(void)
175 {
176 int pid;
177 int pip[2];
178 char c;
179
180 if (pipe(pip) < 0)
181 {
182 perror("pipe");
183 exit(EXIT_FAILURE);
184 }
185 dup2(pip[1], 0);
186 close(pip[1]);
187 if((pid = fork()) < 0)
188 {
189 perror("fork");
190 exit(EXIT_FAILURE);
191 }
192 else if(pid > 0)
193 {
194 close(0);
195 /* Wait for initialization to finish, successfully or
196 * unsuccessfully. Until this point the child may still
197 * write to stdout/stderr.
198 * -- jilles */
199 if (read(pip[0], &c, 1) > 0)
200 exit(EXIT_SUCCESS);
201 else
202 exit(EXIT_FAILURE);
203 }
204
205 close(pip[0]);
206 setsid();
207 /* fclose(stdin);
208 fclose(stdout);
209 fclose(stderr); */
210
211 return 0;
212 }
213
214 static int printVersion = 0;
215
216 struct lgetopt myopts[] = {
217 {"dlinefile", &ConfigFileEntry.dlinefile,
218 STRING, "File to use for dlines.conf"},
219 {"configfile", &ConfigFileEntry.configfile,
220 STRING, "File to use for ircd.conf"},
221 {"klinefile", &ConfigFileEntry.klinefile,
222 STRING, "File to use for kline.conf"},
223 {"xlinefile", &ConfigFileEntry.xlinefile,
224 STRING, "File to use for xline.conf"},
225 {"resvfile", &ConfigFileEntry.resvfile,
226 STRING, "File to use for resv.conf"},
227 {"logfile", &logFileName,
228 STRING, "File to use for ircd.log"},
229 {"pidfile", &pidFileName,
230 STRING, "File to use for process ID"},
231 {"foreground", &server_state_foreground,
232 YESNO, "Run in foreground (don't detach)"},
233 {"version", &printVersion,
234 YESNO, "Print version and exit"},
235 {"conftest", &testing_conf,
236 YESNO, "Test the configuration files and exit"},
237 {"help", NULL, USAGE, "Print this text"},
238 {NULL, NULL, STRING, NULL},
239 };
240
241 static void
242 check_rehash(void *unused)
243 {
244 /*
245 * Check to see whether we have to rehash the configuration ..
246 */
247 if(dorehash)
248 {
249 rehash(1);
250 dorehash = 0;
251 }
252
253 if(dorehashbans)
254 {
255 rehash_bans(1);
256 dorehashbans = 0;
257 }
258
259 if(doremotd)
260 {
261 sendto_realops_snomask(SNO_GENERAL, L_ALL,
262 "Got signal SIGUSR1, reloading ircd motd file");
263 free_cachefile(user_motd);
264 user_motd = cache_file(MPATH, "ircd.motd", 0);
265 doremotd = 0;
266 }
267 }
268
269 /*
270 * initalialize_global_set_options
271 *
272 * inputs - none
273 * output - none
274 * side effects - This sets all global set options needed
275 */
276 static void
277 initialize_global_set_options(void)
278 {
279 memset(&GlobalSetOptions, 0, sizeof(GlobalSetOptions));
280 /* memset( &ConfigFileEntry, 0, sizeof(ConfigFileEntry)); */
281
282 GlobalSetOptions.maxclients = ServerInfo.max_clients;
283 GlobalSetOptions.autoconn = 1;
284
285 GlobalSetOptions.spam_time = MIN_JOIN_LEAVE_TIME;
286 GlobalSetOptions.spam_num = MAX_JOIN_LEAVE_COUNT;
287
288 if(ConfigFileEntry.default_floodcount)
289 GlobalSetOptions.floodcount = ConfigFileEntry.default_floodcount;
290 else
291 GlobalSetOptions.floodcount = 10;
292
293 split_servers = ConfigChannel.default_split_server_count;
294 split_users = ConfigChannel.default_split_user_count;
295
296 if(split_users && split_servers
297 && (ConfigChannel.no_create_on_split || ConfigChannel.no_join_on_split))
298 {
299 splitmode = 1;
300 splitchecking = 1;
301 }
302
303 GlobalSetOptions.ident_timeout = IDENT_TIMEOUT;
304
305 strlcpy(GlobalSetOptions.operstring,
306 ConfigFileEntry.default_operstring,
307 sizeof(GlobalSetOptions.operstring));
308 strlcpy(GlobalSetOptions.adminstring,
309 ConfigFileEntry.default_adminstring,
310 sizeof(GlobalSetOptions.adminstring));
311
312 /* memset( &ConfigChannel, 0, sizeof(ConfigChannel)); */
313
314 /* End of global set options */
315
316 }
317
318 /*
319 * initialize_server_capabs
320 *
321 * inputs - none
322 * output - none
323 */
324 static void
325 initialize_server_capabs(void)
326 {
327 default_server_capabs &= ~CAP_ZIP;
328 }
329
330
331 /*
332 * write_pidfile
333 *
334 * inputs - filename+path of pid file
335 * output - none
336 * side effects - write the pid of the ircd to filename
337 */
338 static void
339 write_pidfile(const char *filename)
340 {
341 FILE *fb;
342 char buff[32];
343 if((fb = fopen(filename, "w")))
344 {
345 unsigned int pid = (unsigned int) getpid();
346
347 rb_snprintf(buff, sizeof(buff), "%u\n", pid);
348 if((fputs(buff, fb) == -1))
349 {
350 ilog(L_MAIN, "Error writing %u to pid file %s (%s)",
351 pid, filename, strerror(errno));
352 }
353 fclose(fb);
354 return;
355 }
356 else
357 {
358 ilog(L_MAIN, "Error opening pid file %s", filename);
359 }
360 }
361
362 /*
363 * check_pidfile
364 *
365 * inputs - filename+path of pid file
366 * output - none
367 * side effects - reads pid from pidfile and checks if ircd is in process
368 * list. if it is, gracefully exits
369 * -kre
370 */
371 static void
372 check_pidfile(const char *filename)
373 {
374 FILE *fb;
375 char buff[32];
376 pid_t pidfromfile;
377
378 /* Don't do logging here, since we don't have log() initialised */
379 if((fb = fopen(filename, "r")))
380 {
381 if(fgets(buff, 20, fb) != NULL)
382 {
383 pidfromfile = atoi(buff);
384 if(!kill(pidfromfile, 0))
385 {
386 printf("ircd: daemon is already running\n");
387 exit(-1);
388 }
389 }
390 fclose(fb);
391 }
392 }
393
394 /*
395 * setup_corefile
396 *
397 * inputs - nothing
398 * output - nothing
399 * side effects - setups corefile to system limits.
400 * -kre
401 */
402 static void
403 setup_corefile(void)
404 {
405 #ifdef HAVE_SYS_RESOURCE_H
406 struct rlimit rlim; /* resource limits */
407
408 /* Set corefilesize to maximum */
409 if(!getrlimit(RLIMIT_CORE, &rlim))
410 {
411 rlim.rlim_cur = rlim.rlim_max;
412 setrlimit(RLIMIT_CORE, &rlim);
413 }
414 #endif
415 }
416
417 struct ev_entry *check_splitmode_ev = NULL;
418
419 /*
420 * main
421 *
422 * Initializes the IRCd.
423 *
424 * Inputs - number of commandline args, args themselves
425 * Outputs - none
426 * Side Effects - this is where the ircd gets going right now
427 */
428 int
429 main(int argc, char *argv[])
430 {
431 int fd;
432
433 /* Check to see if the user is running us as root, which is a nono */
434 if(geteuid() == 0)
435 {
436 fprintf(stderr, "Don't run ircd as root!!!\n");
437 return -1;
438 }
439
440 /*
441 * Setup corefile size immediately after boot -kre
442 */
443 setup_corefile();
444
445 ServerRunning = 0;
446 /* It ain't random, but it ought to be a little harder to guess */
447 srand(SystemTime.tv_sec ^ (SystemTime.tv_usec | (getpid() << 20)));
448 memset(&me, 0, sizeof(me));
449 memset(&meLocalUser, 0, sizeof(meLocalUser));
450 me.localClient = &meLocalUser;
451
452 /* Make sure all lists are zeroed */
453 memset(&unknown_list, 0, sizeof(unknown_list));
454 memset(&lclient_list, 0, sizeof(lclient_list));
455 memset(&serv_list, 0, sizeof(serv_list));
456 memset(&global_serv_list, 0, sizeof(global_serv_list));
457 memset(&local_oper_list, 0, sizeof(local_oper_list));
458 memset(&oper_list, 0, sizeof(oper_list));
459
460 rb_dlinkAddTail(&me, &me.node, &global_client_list);
461
462 memset(&Count, 0, sizeof(Count));
463 memset(&ServerInfo, 0, sizeof(ServerInfo));
464 memset(&AdminInfo, 0, sizeof(AdminInfo));
465 memset(&ServerStats, 0, sizeof(struct ServerStatistics));
466
467 /* Initialise the channel capability usage counts... */
468 init_chcap_usage_counts();
469
470 ConfigFileEntry.dpath = DPATH;
471 ConfigFileEntry.configfile = CPATH; /* Server configuration file */
472 ConfigFileEntry.klinefile = KPATH; /* Server kline file */
473 ConfigFileEntry.dlinefile = DLPATH; /* dline file */
474 ConfigFileEntry.xlinefile = XPATH;
475 ConfigFileEntry.resvfile = RESVPATH;
476 ConfigFileEntry.connect_timeout = 30; /* Default to 30 */
477 myargv = argv;
478 umask(077); /* better safe than sorry --SRB */
479
480 parseargs(&argc, &argv, myopts);
481
482 if(printVersion)
483 {
484 printf("ircd: version %s\n", ircd_version);
485 exit(EXIT_SUCCESS);
486 }
487
488 if(chdir(ConfigFileEntry.dpath))
489 {
490 fprintf(stderr, "Unable to chdir to %s: %s\n", ConfigFileEntry.dpath, strerror(errno));
491 exit(EXIT_FAILURE);
492 }
493
494 setup_signals();
495
496 #ifdef __CYGWIN__
497 server_state_foreground = 1;
498 #endif
499
500 if (testing_conf)
501 server_state_foreground = 1;
502
503 /* Make sure fd 0, 1 and 2 are in use -- jilles */
504 do
505 {
506 fd = open("/dev/null", O_RDWR);
507 } while (fd < 2 && fd != -1);
508 if (fd > 2)
509 close(fd);
510 else if (fd == -1)
511 exit(1);
512
513 /* Check if there is pidfile and daemon already running */
514 if(!testing_conf)
515 {
516 check_pidfile(pidFileName);
517
518 if(!server_state_foreground)
519 make_daemon();
520 inotice("starting %s ...", ircd_version);
521 }
522
523 /* Init the event subsystem */
524 init_sys();
525 rb_lib_init(ircd_log_cb, ircd_restart_cb, ircd_die_cb, !server_state_foreground, maxconnections, DNODE_HEAP_SIZE, FD_HEAP_SIZE);
526 rb_linebuf_init(LINEBUF_HEAP_SIZE);
527
528 init_main_logfile();
529 newconf_init();
530 init_s_conf();
531 init_s_newconf();
532 init_hash();
533 clear_scache_hash_table(); /* server cache name table */
534 init_host_hash();
535 clear_hash_parse();
536 init_client();
537 initUser();
538 init_hook();
539 init_channels();
540 initclass();
541 initwhowas();
542 init_reject();
543 init_cache();
544 init_monitor();
545 init_isupport();
546 load_all_modules(1);
547 #ifndef STATIC_MODULES
548 load_core_modules(1);
549 #endif
550 init_auth(); /* Initialise the auth code */
551 init_resolver(); /* Needs to be setup before the io loop */
552
553 if (testing_conf)
554 fprintf(stderr, "\nBeginning config test\n");
555 read_conf_files(YES); /* cold start init conf files */
556 rehash_bans(0);
557 #ifndef STATIC_MODULES
558
559 mod_add_path(MODULE_DIR);
560 mod_add_path(MODULE_DIR "/autoload");
561 #endif
562
563 initialize_server_capabs(); /* Set up default_server_capabs */
564 initialize_global_set_options();
565
566 if(ServerInfo.name == NULL)
567 {
568 ierror("no server name specified in serverinfo block.");
569 return -1;
570 }
571 strlcpy(me.name, ServerInfo.name, sizeof(me.name));
572
573 if(ServerInfo.sid[0] == '\0')
574 {
575 ierror("no server sid specified in serverinfo block.");
576 return -2;
577 }
578 strcpy(me.id, ServerInfo.sid);
579 init_uid();
580
581 /* serverinfo{} description must exist. If not, error out. */
582 if(ServerInfo.description == NULL)
583 {
584 ierror("no server description specified in serverinfo block.");
585 return -3;
586 }
587 strlcpy(me.info, ServerInfo.description, sizeof(me.info));
588
589 if (testing_conf)
590 {
591 fprintf(stderr, "\nConfig testing complete.\n");
592 fflush(stderr);
593 return 0; /* Why? We want the launcher to exit out. */
594 }
595
596 me.from = &me;
597 me.servptr = &me;
598 SetMe(&me);
599 make_server(&me);
600 startup_time = rb_current_time();
601 add_to_client_hash(me.name, &me);
602 add_to_id_hash(me.id, &me);
603 me.serv->nameinfo = scache_connect(me.name, me.info, 0);
604
605 rb_dlinkAddAlloc(&me, &global_serv_list);
606
607 construct_umodebuf();
608
609 check_class();
610 write_pidfile(pidFileName);
611 load_help();
612 open_logfiles();
613
614 ilog(L_MAIN, "Server Ready");
615
616 rb_event_addish("cleanup_glines", cleanup_glines, NULL, CLEANUP_GLINES_TIME);
617
618 /* We want try_connections to be called as soon as possible now! -- adrian */
619 /* No, 'cause after a restart it would cause all sorts of nick collides */
620 /* um. by waiting even longer, that just means we have even *more*
621 * nick collisions. what a stupid idea. set an event for the IO loop --fl
622 */
623 rb_event_addish("try_connections", try_connections, NULL, STARTUP_CONNECTIONS_TIME);
624 rb_event_addonce("try_connections_startup", try_connections, NULL, 0);
625
626 rb_event_addish("collect_zipstats", collect_zipstats, NULL, ZIPSTATS_TIME);
627
628 /* Setup the timeout check. I'll shift it later :) -- adrian */
629 rb_event_addish("rb_checktimeouts", rb_checktimeouts, NULL, 1);
630
631 rb_event_add("check_rehash", check_rehash, NULL, 1);
632
633 if(splitmode)
634 check_splitmode_ev = rb_event_add("check_splitmode", check_splitmode, NULL, 2);
635
636 ServerRunning = 1;
637
638 print_startup(getpid());
639
640 rb_lib_loop(250);
641
642 return 0;
643 }