ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid-8/src/ircd.c
Revision: 876
Committed: Wed Oct 24 21:51:21 2007 UTC (17 years, 10 months ago) by michael
Content type: text/x-csrc
Original Path: ircd-hybrid-7.2/src/ircd.c
File size: 19183 byte(s)
Log Message:
Backported WATCH

File Contents

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

Properties

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