/[svn]/ircd-hybrid-7.2/src/ircd.c
ViewVC logotype

Contents of /ircd-hybrid-7.2/src/ircd.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 998 - (show annotations)
Sun Aug 23 12:43:17 2009 UTC (10 years, 10 months ago) by michael
File MIME type: text/x-chdr
File size: 18014 byte(s)
- change resolver/auth to use balloc memory pool
- ircd.c: init_resolver() has to be called *before* read_conf_files(), otherwise
  recvfrom() aborts with ENOTSOCK in case there are configured connect{} blocks
  with a host that needs to be resolved on the very first configuration read.

1 /*
2 * ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd).
3 * ircd.c: Starts up and runs the ircd.
4 *
5 * Copyright (C) 2002 by the past and present ircd coders, and others.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 * USA
21 *
22 * $Id$
23 */
24
25 #include "stdinc.h"
26 #include "s_user.h"
27 #include "tools.h"
28 #include "ircd.h"
29 #include "channel.h"
30 #include "channel_mode.h"
31 #include "client.h"
32 #include "common.h"
33 #include "event.h"
34 #include "fdlist.h"
35 #include "hash.h"
36 #include "irc_string.h"
37 #include "sprintf_irc.h"
38 #include "ircd_signal.h"
39 #include "list.h"
40 #include "s_gline.h"
41 #include "motd.h"
42 #include "ircd_handler.h"
43 #include "msg.h" /* msgtab */
44 #include "hostmask.h"
45 #include "numeric.h"
46 #include "packet.h"
47 #include "parse.h"
48 #include "irc_res.h"
49 #include "restart.h"
50 #include "rng_mt.h"
51 #include "s_auth.h"
52 #include "s_bsd.h"
53 #include "s_conf.h"
54 #include "s_log.h"
55 #include "s_misc.h"
56 #include "s_serv.h" /* try_connections */
57 #include "send.h"
58 #include "whowas.h"
59 #include "modules.h"
60 #include "memory.h"
61 #include "hook.h"
62 #include "ircd_getopt.h"
63 #include "balloc.h"
64 #include "motd.h"
65 #include "supported.h"
66 #include "watch.h"
67
68 /* Try and find the correct name to use with getrlimit() for setting the max.
69 * number of files allowed to be open by this process.
70 */
71
72 /* /quote set variables */
73 struct SetOptions GlobalSetOptions;
74
75 /* configuration set from ircd.conf */
76 struct config_file_entry ConfigFileEntry;
77 /* server info set from ircd.conf */
78 struct server_info ServerInfo;
79 /* admin info set from ircd.conf */
80 struct admin_info AdminInfo = { NULL, NULL, NULL };
81 struct Counter Count = { 0, 0, 0, 0, 0, 0, 0, 0 };
82 struct ServerState_t server_state = { 0 };
83 struct logging_entry ConfigLoggingEntry = { 1, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0} };
84 struct ServerStatistics ServerStats;
85 struct timeval SystemTime;
86 struct Client me; /* That's me */
87 struct LocalUser meLocalUser; /* That's also part of me */
88
89 const char *logFileName = LPATH;
90 const char *pidFileName = PPATH;
91
92 char **myargv;
93 char ircd_platform[PLATFORMLEN];
94
95 int dorehash = 0;
96 int doremotd = 0;
97 time_t nextconnect = 1; /* time for next try_connections call */
98
99 /* Set to zero because it should be initialized later using
100 * initialize_server_capabs
101 */
102 int default_server_capabs = 0;
103
104 #ifdef HAVE_LIBCRYPTO
105 int bio_spare_fd = -1;
106 #endif
107
108 int splitmode;
109 int splitchecking;
110 int split_users;
111 unsigned int split_servers;
112
113 /* Do klines the same way hybrid-6 did them, i.e. at the
114 * top of the next io_loop instead of in the same loop as
115 * the klines are being applied.
116 *
117 * This should fix strange CPU starvation as very indirectly reported.
118 * (Why do you people not email bug reports? WHY? WHY?)
119 *
120 * - Dianora
121 */
122
123 int rehashed_klines = 0;
124
125
126 #ifndef _WIN32
127 /*
128 * print_startup - print startup information
129 */
130 static void
131 print_startup(int pid)
132 {
133 printf("ircd: version %s\n", ircd_version);
134 printf("ircd: pid %d\n", pid);
135 printf("ircd: running in %s mode from %s\n", !server_state.foreground ? "background"
136 : "foreground", ConfigFileEntry.dpath);
137 }
138
139 static void
140 make_daemon(void)
141 {
142 int pid;
143
144 if ((pid = fork()) < 0)
145 {
146 perror("fork");
147 exit(EXIT_FAILURE);
148 }
149 else if (pid > 0)
150 {
151 print_startup(pid);
152 exit(EXIT_SUCCESS);
153 }
154
155 setsid();
156 }
157 #endif
158
159 static int printVersion = 0;
160
161 struct lgetopt myopts[] = {
162 {"dlinefile", &ConfigFileEntry.dlinefile,
163 STRING, "File to use for dline.conf"},
164 {"configfile", &ConfigFileEntry.configfile,
165 STRING, "File to use for ircd.conf"},
166 {"klinefile", &ConfigFileEntry.klinefile,
167 STRING, "File to use for kline.conf"},
168 {"xlinefile", &ConfigFileEntry.xlinefile,
169 STRING, "File to use for xline.conf"},
170 {"logfile", &logFileName,
171 STRING, "File to use for ircd.log"},
172 {"pidfile", &pidFileName,
173 STRING, "File to use for process ID"},
174 {"foreground", &server_state.foreground,
175 YESNO, "Run in foreground (don't detach)"},
176 {"version", &printVersion,
177 YESNO, "Print version and exit"},
178 {"help", NULL, USAGE, "Print this text"},
179 {NULL, NULL, STRING, NULL},
180 };
181
182 void
183 set_time(void)
184 {
185 static char to_send[200];
186 struct timeval newtime;
187 #ifdef _WIN32
188 FILETIME ft;
189
190 GetSystemTimeAsFileTime(&ft);
191 if (ft.dwLowDateTime < 0xd53e8000)
192 ft.dwHighDateTime--;
193 ft.dwLowDateTime -= 0xd53e8000;
194 ft.dwHighDateTime -= 0x19db1de;
195
196 newtime.tv_sec = (*(uint64_t *) &ft) / 10000000;
197 newtime.tv_usec = (*(uint64_t *) &ft) / 10 % 1000000;
198 #else
199 newtime.tv_sec = 0;
200 newtime.tv_usec = 0;
201
202 if (gettimeofday(&newtime, NULL) == -1)
203 {
204 ilog(L_ERROR, "Clock Failure (%s), TS can be corrupted",
205 strerror(errno));
206 sendto_realops_flags(UMODE_ALL, L_ALL,
207 "Clock Failure (%s), TS can be corrupted",
208 strerror(errno));
209 restart("Clock Failure");
210 }
211 #endif
212
213 if (newtime.tv_sec < CurrentTime)
214 {
215 ircsprintf(to_send, "System clock is running backwards - (%lu < %lu)",
216 (unsigned long)newtime.tv_sec, (unsigned long)CurrentTime);
217 report_error(L_ALL, to_send, me.name, 0);
218 set_back_events(CurrentTime - newtime.tv_sec);
219 }
220
221 SystemTime.tv_sec = newtime.tv_sec;
222 SystemTime.tv_usec = newtime.tv_usec;
223 }
224
225 static void
226 io_loop(void)
227 {
228 while (1 == 1)
229 {
230 /*
231 * Maybe we want a flags word?
232 * ie. if (REHASHED_KLINES(global_flags))
233 * SET_REHASHED_KLINES(global_flags)
234 * CLEAR_REHASHED_KLINES(global_flags)
235 *
236 * - Dianora
237 */
238 if (rehashed_klines)
239 {
240 check_conf_klines();
241 rehashed_klines = 0;
242 }
243
244 if (listing_client_list.head)
245 {
246 dlink_node *ptr = NULL, *ptr_next = NULL;
247 DLINK_FOREACH_SAFE(ptr, ptr_next, listing_client_list.head)
248 {
249 struct Client *client_p = ptr->data;
250 assert(client_p->localClient->list_task);
251 safe_list_channels(client_p, client_p->localClient->list_task, 0);
252 }
253 }
254
255 /* Run pending events, then get the number of seconds to the next
256 * event
257 */
258 while (eventNextTime() <= CurrentTime)
259 eventRun();
260
261 comm_select();
262 exit_aborted_clients();
263 free_exited_clients();
264 send_queued_all();
265
266 /* Check to see whether we have to rehash the configuration .. */
267 if (dorehash)
268 {
269 rehash(1);
270 dorehash = 0;
271 }
272 if (doremotd)
273 {
274 read_message_file(&ConfigFileEntry.motd);
275 sendto_realops_flags(UMODE_ALL, L_ALL,
276 "Got signal SIGUSR1, reloading ircd motd file");
277 doremotd = 0;
278 }
279 }
280 }
281
282 /* initalialize_global_set_options()
283 *
284 * inputs - none
285 * output - none
286 * side effects - This sets all global set options needed
287 */
288 static void
289 initialize_global_set_options(void)
290 {
291 memset(&GlobalSetOptions, 0, sizeof(GlobalSetOptions));
292
293 GlobalSetOptions.autoconn = 1;
294 GlobalSetOptions.spam_time = MIN_JOIN_LEAVE_TIME;
295 GlobalSetOptions.spam_num = MAX_JOIN_LEAVE_COUNT;
296
297 if (ConfigFileEntry.default_floodcount)
298 GlobalSetOptions.floodcount = ConfigFileEntry.default_floodcount;
299 else
300 GlobalSetOptions.floodcount = 10;
301
302 /* XXX I have no idea what to try here - Dianora */
303 GlobalSetOptions.joinfloodcount = 16;
304 GlobalSetOptions.joinfloodtime = 8;
305
306 split_servers = ConfigChannel.default_split_server_count;
307 split_users = ConfigChannel.default_split_user_count;
308
309 if (split_users && split_servers && (ConfigChannel.no_create_on_split ||
310 ConfigChannel.no_join_on_split))
311 {
312 splitmode = 1;
313 splitchecking = 1;
314 }
315
316 GlobalSetOptions.ident_timeout = IDENT_TIMEOUT;
317 GlobalSetOptions.idletime = ConfigFileEntry.idletime;
318 /* End of global set options */
319 }
320
321 /* initialize_message_files()
322 *
323 * inputs - none
324 * output - none
325 * side effects - Set up all message files needed, motd etc.
326 */
327 static void
328 initialize_message_files(void)
329 {
330 init_message_file(USER_MOTD, MPATH, &ConfigFileEntry.motd);
331 init_message_file(OPER_MOTD, OPATH, &ConfigFileEntry.opermotd);
332 init_message_file(USER_LINKS, LIPATH, &ConfigFileEntry.linksfile);
333
334 read_message_file(&ConfigFileEntry.motd);
335 read_message_file(&ConfigFileEntry.opermotd);
336 read_message_file(&ConfigFileEntry.linksfile);
337
338 init_isupport();
339 }
340
341 /* initialize_server_capabs()
342 *
343 * inputs - none
344 * output - none
345 */
346 static void
347 initialize_server_capabs(void)
348 {
349 add_capability("QS", CAP_QS, 1);
350 add_capability("EOB", CAP_EOB, 1);
351
352 if (ServerInfo.sid != NULL) /* only enable TS6 if we have an SID */
353 add_capability("TS6", CAP_TS6, 0);
354
355 add_capability("ZIP", CAP_ZIP, 0);
356 add_capability("CLUSTER", CAP_CLUSTER, 1);
357 #ifdef HALFOPS
358 add_capability("HOPS", CAP_HOPS, 1);
359 #endif
360 }
361
362 /* write_pidfile()
363 *
364 * inputs - filename+path of pid file
365 * output - NONE
366 * side effects - write the pid of the ircd to filename
367 */
368 static void
369 write_pidfile(const char *filename)
370 {
371 FBFILE *fb;
372
373 if ((fb = fbopen(filename, "w")))
374 {
375 char buff[32];
376 unsigned int pid = (unsigned int)getpid();
377 size_t nbytes = ircsprintf(buff, "%u\n", pid);
378
379 if ((fbputs(buff, fb, nbytes) == -1))
380 ilog(L_ERROR, "Error writing %u to pid file %s (%s)",
381 pid, filename, strerror(errno));
382
383 fbclose(fb);
384 return;
385 }
386 else
387 {
388 ilog(L_ERROR, "Error opening pid file %s", filename);
389 }
390 }
391
392 /* check_pidfile()
393 *
394 * inputs - filename+path of pid file
395 * output - none
396 * side effects - reads pid from pidfile and checks if ircd is in process
397 * list. if it is, gracefully exits
398 * -kre
399 */
400 static void
401 check_pidfile(const char *filename)
402 {
403 #ifndef _WIN32
404 FBFILE *fb;
405 char buff[32];
406 pid_t pidfromfile;
407
408 /* Don't do logging here, since we don't have log() initialised */
409 if ((fb = fbopen(filename, "r")))
410 {
411 if (fbgets(buff, 20, fb) == NULL)
412 {
413 /* log(L_ERROR, "Error reading from pid file %s (%s)", filename,
414 * strerror(errno));
415 */
416 }
417 else
418 {
419 pidfromfile = atoi(buff);
420
421 if (!kill(pidfromfile, 0))
422 {
423 /* log(L_ERROR, "Server is already running"); */
424 printf("ircd: daemon is already running\n");
425 exit(-1);
426 }
427 }
428
429 fbclose(fb);
430 }
431 else if (errno != ENOENT)
432 {
433 /* log(L_ERROR, "Error opening pid file %s", filename); */
434 }
435 #endif
436 }
437
438 /* setup_corefile()
439 *
440 * inputs - nothing
441 * output - nothing
442 * side effects - setups corefile to system limits.
443 * -kre
444 */
445 static void
446 setup_corefile(void)
447 {
448 #ifdef HAVE_SYS_RESOURCE_H
449 struct rlimit rlim; /* resource limits */
450
451 /* Set corefilesize to maximum */
452 if (!getrlimit(RLIMIT_CORE, &rlim))
453 {
454 rlim.rlim_cur = rlim.rlim_max;
455 setrlimit(RLIMIT_CORE, &rlim);
456 }
457 #endif
458 }
459
460 /* init_ssl()
461 *
462 * inputs - nothing
463 * output - nothing
464 * side effects - setups SSL context.
465 */
466 static void
467 init_ssl(void)
468 {
469 #ifdef HAVE_LIBCRYPTO
470 SSL_load_error_strings();
471 SSLeay_add_ssl_algorithms();
472
473 if ((ServerInfo.server_ctx = SSL_CTX_new(SSLv23_server_method())) == NULL)
474 {
475 const char *s;
476
477 fprintf(stderr, "ERROR: Could not initialize the SSL context -- %s\n",
478 s = ERR_lib_error_string(ERR_get_error()));
479 ilog(L_CRIT, "ERROR: Could not initialize the SSL context -- %s\n", s);
480 }
481
482 SSL_CTX_set_options(ServerInfo.server_ctx, SSL_OP_NO_SSLv2);
483 SSL_CTX_set_options(ServerInfo.server_ctx, SSL_OP_TLS_ROLLBACK_BUG|SSL_OP_ALL);
484 SSL_CTX_set_verify(ServerInfo.server_ctx, SSL_VERIFY_NONE, NULL);
485
486 bio_spare_fd = save_spare_fd("SSL private key validation");
487 #endif /* HAVE_LIBCRYPTO */
488 }
489
490 /* init_callbacks()
491 *
492 * inputs - nothing
493 * output - nothing
494 * side effects - setups standard hook points
495 */
496 static void
497 init_callbacks(void)
498 {
499 iorecv_cb = register_callback("iorecv", iorecv_default);
500 iosend_cb = register_callback("iosend", iosend_default);
501 iorecvctrl_cb = register_callback("iorecvctrl", NULL);
502 iosendctrl_cb = register_callback("iosendctrl", NULL);
503 }
504
505 int
506 main(int argc, char *argv[])
507 {
508 /* Check to see if the user is running
509 * us as root, which is a nono
510 */
511 #ifndef _WIN32
512 if (geteuid() == 0)
513 {
514 fprintf(stderr, "Don't run ircd as root!!!\n");
515 return -1;
516 }
517
518 /* Setup corefile size immediately after boot -kre */
519 setup_corefile();
520 #endif
521
522 /* save server boot time right away, so getrusage works correctly */
523 set_time();
524
525 /* It ain't random, but it ought to be a little harder to guess */
526 init_genrand(SystemTime.tv_sec ^ (SystemTime.tv_usec | (getpid() << 20)));
527
528 memset(&me, 0, sizeof(me));
529 memset(&meLocalUser, 0, sizeof(meLocalUser));
530 me.localClient = &meLocalUser;
531 dlinkAdd(&me, &me.node, &global_client_list); /* Pointer to beginning
532 of Client list */
533
534 memset(&ServerInfo, 0, sizeof(ServerInfo));
535 memset(&ServerStats, 0, sizeof(ServerStats));
536
537 /* Initialise the channel capability usage counts... */
538 init_chcap_usage_counts();
539
540 ConfigFileEntry.dpath = DPATH;
541 ConfigFileEntry.configfile = CPATH; /* Server configuration file */
542 ConfigFileEntry.klinefile = KPATH; /* Server kline file */
543 ConfigFileEntry.xlinefile = XPATH; /* Server xline file */
544 ConfigFileEntry.rxlinefile = RXPATH; /* Server regex xline file */
545 ConfigFileEntry.rklinefile = RKPATH; /* Server regex kline file */
546 ConfigFileEntry.dlinefile = DLPATH; /* dline file */
547 ConfigFileEntry.glinefile = GPATH; /* gline log file */
548 ConfigFileEntry.cresvfile = CRESVPATH; /* channel resv file */
549 ConfigFileEntry.nresvfile = NRESVPATH; /* nick resv file */
550 myargv = argv;
551 umask(077); /* better safe than sorry --SRB */
552
553 parseargs(&argc, &argv, myopts);
554
555 if (printVersion)
556 {
557 printf("ircd: version %s\n", ircd_version);
558 exit(EXIT_SUCCESS);
559 }
560
561 if (chdir(ConfigFileEntry.dpath))
562 {
563 perror("chdir");
564 exit(EXIT_FAILURE);
565 }
566
567 init_ssl();
568
569 #ifndef _WIN32
570 if (!server_state.foreground)
571 {
572 make_daemon();
573 close_standard_fds(); /* this needs to be before init_netio()! */
574 }
575 else
576 print_startup(getpid());
577
578 setup_signals();
579 #endif
580
581 get_ircd_platform(ircd_platform);
582
583 /* Init the event subsystem */
584 eventInit();
585 /* We need this to initialise the fd array before anything else */
586 fdlist_init();
587 init_log(logFileName);
588 check_can_use_v6();
589 init_comm(); /* This needs to be setup early ! -- adrian */
590 /* Check if there is pidfile and daemon already running */
591 check_pidfile(pidFileName);
592
593 #ifndef NOBALLOC
594 initBlockHeap();
595 #endif
596 init_dlink_nodes();
597 init_callbacks();
598 initialize_message_files();
599 dbuf_init();
600 init_hash();
601 init_ip_hash_table(); /* client host ip hash table */
602 init_host_hash(); /* Host-hashtable. */
603 clear_tree_parse();
604 init_client();
605 init_class();
606 init_whowas();
607 watch_init();
608 init_auth(); /* Initialise the auth code */
609 init_resolver(); /* Needs to be setup before the io loop */
610 read_conf_files(1); /* cold start init conf files */
611 me.id[0] = '\0';
612 init_uid();
613 initialize_server_capabs(); /* Set up default_server_capabs */
614 initialize_global_set_options();
615 init_channels();
616
617 if (ServerInfo.name == NULL)
618 {
619 ilog(L_CRIT, "No server name specified in serverinfo block.");
620 exit(EXIT_FAILURE);
621 }
622
623 strlcpy(me.name, ServerInfo.name, sizeof(me.name));
624
625 /* serverinfo{} description must exist. If not, error out.*/
626 if (ServerInfo.description == NULL)
627 {
628 ilog(L_CRIT,
629 "ERROR: No server description specified in serverinfo block.");
630 exit(EXIT_FAILURE);
631 }
632
633 strlcpy(me.info, ServerInfo.description, sizeof(me.info));
634
635 me.from = &me;
636 me.servptr = &me;
637
638 SetMe(&me);
639 make_server(&me);
640
641 me.lasttime = me.since = me.firsttime = CurrentTime;
642 hash_add_client(&me);
643
644 /* add ourselves to global_serv_list */
645 dlinkAdd(&me, make_dlink_node(), &global_serv_list);
646
647 check_class();
648
649 #ifndef STATIC_MODULES
650 if (chdir(MODPATH))
651 {
652 ilog (L_CRIT, "Could not load core modules. Terminating!");
653 exit(EXIT_FAILURE);
654 }
655
656 load_all_modules(1);
657 load_conf_modules();
658 load_core_modules(1);
659 /* Go back to DPATH after checking to see if we can chdir to MODPATH */
660 chdir(ConfigFileEntry.dpath);
661 #else
662 load_all_modules(1);
663 #endif
664 /*
665 * assemble_umode_buffer() has to be called after
666 * reading conf/loading modules.
667 */
668 assemble_umode_buffer();
669
670 write_pidfile(pidFileName);
671
672 ilog(L_NOTICE, "Server Ready");
673
674 eventAddIsh("cleanup_glines", cleanup_glines, NULL, CLEANUP_GLINES_TIME);
675 eventAddIsh("cleanup_tklines", cleanup_tklines, NULL, CLEANUP_TKLINES_TIME);
676
677 /* We want try_connections to be called as soon as possible now! -- adrian */
678 /* No, 'cause after a restart it would cause all sorts of nick collides */
679 eventAddIsh("try_connections", try_connections, NULL, STARTUP_CONNECTIONS_TIME);
680
681 eventAddIsh("collect_zipstats", collect_zipstats, NULL, ZIPSTATS_TIME);
682
683 /* Setup the timeout check. I'll shift it later :) -- adrian */
684 eventAddIsh("comm_checktimeouts", comm_checktimeouts, NULL, 1);
685
686 if (ConfigServerHide.links_delay > 0)
687 eventAddIsh("write_links_file", write_links_file, NULL, ConfigServerHide.links_delay);
688 else
689 ConfigServerHide.links_disabled = 1;
690
691 if (splitmode)
692 eventAddIsh("check_splitmode", check_splitmode, NULL, 60);
693
694 io_loop();
695 return 0;
696 }

Properties

Name Value
svn:eol-style native
svn:keywords Id Revision

svnadmin@ircd-hybrid.org
ViewVC Help
Powered by ViewVC 1.1.28