ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid-8/src/ircd.c
Revision: 896
Committed: Sat Nov 3 08:54:09 2007 UTC (17 years, 9 months ago) by michael
Content type: text/x-csrc
Original Path: ircd-hybrid-7.2/src/ircd.c
File size: 19176 byte(s)
Log Message:
- Killed s_stats.c

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 "send.h"
57 #include "whowas.h"
58 #include "modules.h"
59 #include "memory.h"
60 #include "hook.h"
61 #include "ircd_getopt.h"
62 #include "balloc.h"
63 #include "motd.h"
64 #include "supported.h"
65 #include "watch.h"
66
67 /* Try and find the correct name to use with getrlimit() for setting the max.
68 * number of files allowed to be open by this process.
69 */
70
71 /* /quote set variables */
72 struct SetOptions GlobalSetOptions;
73
74 /* configuration set from ircd.conf */
75 struct config_file_entry ConfigFileEntry;
76 /* server info set from ircd.conf */
77 struct server_info ServerInfo;
78 /* admin info set from ircd.conf */
79 struct admin_info AdminInfo = { NULL, NULL, NULL };
80 struct Counter Count = { 0, 0, 0, 0, 0, 0, 0, 0 };
81 struct ServerState_t server_state = { 0 };
82 struct logging_entry ConfigLoggingEntry = { 1, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0} };
83 struct ServerStatistics ServerStats;
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);
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("EOB", CAP_EOB, 1);
385
386 if (ServerInfo.sid != NULL) /* only enable TS6 if we have an SID */
387 add_capability("TS6", CAP_TS6, 0);
388
389 add_capability("ZIP", CAP_ZIP, 0);
390 add_capability("CLUSTER", CAP_CLUSTER, 1);
391 #ifdef HALFOPS
392 add_capability("HOPS", CAP_HOPS, 1);
393 #endif
394 }
395
396 /* write_pidfile()
397 *
398 * inputs - filename+path of pid file
399 * output - NONE
400 * side effects - write the pid of the ircd to filename
401 */
402 static void
403 write_pidfile(const char *filename)
404 {
405 FBFILE *fb;
406
407 if ((fb = fbopen(filename, "w")))
408 {
409 char buff[32];
410 unsigned int pid = (unsigned int)getpid();
411 size_t nbytes = ircsprintf(buff, "%u\n", pid);
412
413 if ((fbputs(buff, fb, nbytes) == -1))
414 ilog(L_ERROR, "Error writing %u to pid file %s (%s)",
415 pid, filename, strerror(errno));
416
417 fbclose(fb);
418 return;
419 }
420 else
421 {
422 ilog(L_ERROR, "Error opening pid file %s", filename);
423 }
424 }
425
426 /* check_pidfile()
427 *
428 * inputs - filename+path of pid file
429 * output - none
430 * side effects - reads pid from pidfile and checks if ircd is in process
431 * list. if it is, gracefully exits
432 * -kre
433 */
434 static void
435 check_pidfile(const char *filename)
436 {
437 #ifndef _WIN32
438 FBFILE *fb;
439 char buff[32];
440 pid_t pidfromfile;
441
442 /* Don't do logging here, since we don't have log() initialised */
443 if ((fb = fbopen(filename, "r")))
444 {
445 if (fbgets(buff, 20, fb) == NULL)
446 {
447 /* log(L_ERROR, "Error reading from pid file %s (%s)", filename,
448 * strerror(errno));
449 */
450 }
451 else
452 {
453 pidfromfile = atoi(buff);
454
455 if (!kill(pidfromfile, 0))
456 {
457 /* log(L_ERROR, "Server is already running"); */
458 printf("ircd: daemon is already running\n");
459 exit(-1);
460 }
461 }
462
463 fbclose(fb);
464 }
465 else if (errno != ENOENT)
466 {
467 /* log(L_ERROR, "Error opening pid file %s", filename); */
468 }
469 #endif
470 }
471
472 /* setup_corefile()
473 *
474 * inputs - nothing
475 * output - nothing
476 * side effects - setups corefile to system limits.
477 * -kre
478 */
479 static void
480 setup_corefile(void)
481 {
482 #ifdef HAVE_SYS_RESOURCE_H
483 struct rlimit rlim; /* resource limits */
484
485 /* Set corefilesize to maximum */
486 if (!getrlimit(RLIMIT_CORE, &rlim))
487 {
488 rlim.rlim_cur = rlim.rlim_max;
489 setrlimit(RLIMIT_CORE, &rlim);
490 }
491 #endif
492 }
493
494 /* init_ssl()
495 *
496 * inputs - nothing
497 * output - nothing
498 * side effects - setups SSL context.
499 */
500 static void
501 init_ssl(void)
502 {
503 #ifdef HAVE_LIBCRYPTO
504 SSL_load_error_strings();
505 SSLeay_add_ssl_algorithms();
506
507 ServerInfo.ctx = SSL_CTX_new(SSLv23_server_method());
508 if (!ServerInfo.ctx)
509 {
510 const char *s;
511
512 fprintf(stderr, "ERROR: Could not initialize the SSL context -- %s\n",
513 s = ERR_lib_error_string(ERR_get_error()));
514 ilog(L_CRIT, "ERROR: Could not initialize the SSL context -- %s\n", s);
515 }
516
517 SSL_CTX_set_options(ServerInfo.ctx, SSL_OP_NO_SSLv2);
518 SSL_CTX_set_options(ServerInfo.ctx, SSL_OP_TLS_ROLLBACK_BUG|SSL_OP_ALL);
519 SSL_CTX_set_verify(ServerInfo.ctx, SSL_VERIFY_NONE, NULL);
520
521 bio_spare_fd = save_spare_fd("SSL private key validation");
522 #endif /* HAVE_LIBCRYPTO */
523 }
524
525 /* init_callbacks()
526 *
527 * inputs - nothing
528 * output - nothing
529 * side effects - setups standard hook points
530 */
531 static void
532 init_callbacks(void)
533 {
534 iorecv_cb = register_callback("iorecv", iorecv_default);
535 iosend_cb = register_callback("iosend", iosend_default);
536 iorecvctrl_cb = register_callback("iorecvctrl", NULL);
537 iosendctrl_cb = register_callback("iosendctrl", NULL);
538 }
539
540 int
541 main(int argc, char *argv[])
542 {
543 /* Check to see if the user is running
544 * us as root, which is a nono
545 */
546 #ifndef _WIN32
547 if (geteuid() == 0)
548 {
549 fprintf(stderr, "Don't run ircd as root!!!\n");
550 return(-1);
551 }
552
553 /* Setup corefile size immediately after boot -kre */
554 setup_corefile();
555
556 /* set initialVMTop before we allocate any memory */
557 initialVMTop = get_vm_top();
558 #endif
559
560 /* save server boot time right away, so getrusage works correctly */
561 set_time();
562
563 /* It ain't random, but it ought to be a little harder to guess */
564 srand(SystemTime.tv_sec ^ (SystemTime.tv_usec | (getpid() << 20)));
565 memset(&me, 0, sizeof(me));
566 memset(&meLocalUser, 0, sizeof(meLocalUser));
567 me.localClient = &meLocalUser;
568 dlinkAdd(&me, &me.node, &global_client_list); /* Pointer to beginning
569 of Client list */
570
571 memset(&ServerInfo, 0, sizeof(ServerInfo));
572 memset(&ServerStats, 0, sizeof(ServerStats));
573
574 /* Initialise the channel capability usage counts... */
575 init_chcap_usage_counts();
576
577 ConfigFileEntry.dpath = DPATH;
578 ConfigFileEntry.configfile = CPATH; /* Server configuration file */
579 ConfigFileEntry.klinefile = KPATH; /* Server kline file */
580 ConfigFileEntry.xlinefile = XPATH; /* Server xline file */
581 ConfigFileEntry.rxlinefile = RXPATH; /* Server regex xline file */
582 ConfigFileEntry.rklinefile = RKPATH; /* Server regex kline file */
583 ConfigFileEntry.dlinefile = DLPATH; /* dline file */
584 ConfigFileEntry.glinefile = GPATH; /* gline log file */
585 ConfigFileEntry.cresvfile = CRESVPATH; /* channel resv file */
586 ConfigFileEntry.nresvfile = NRESVPATH; /* nick resv file */
587 myargv = argv;
588 umask(077); /* better safe than sorry --SRB */
589
590 parseargs(&argc, &argv, myopts);
591
592 if (printVersion)
593 {
594 printf("ircd: version %s\n", ircd_version);
595 exit(EXIT_SUCCESS);
596 }
597
598 if (chdir(ConfigFileEntry.dpath))
599 {
600 perror("chdir");
601 exit(EXIT_FAILURE);
602 }
603
604 init_ssl();
605
606 #ifndef _WIN32
607 if (!server_state.foreground)
608 {
609 make_daemon();
610 close_standard_fds(); /* this needs to be before init_netio()! */
611 }
612 else
613 print_startup(getpid());
614
615 setup_signals();
616 #endif
617
618 get_ircd_platform(ircd_platform);
619
620 /* Init the event subsystem */
621 eventInit();
622 /* We need this to initialise the fd array before anything else */
623 fdlist_init();
624 init_log(logFileName);
625 check_can_use_v6();
626 init_comm(); /* This needs to be setup early ! -- adrian */
627 /* Check if there is pidfile and daemon already running */
628 check_pidfile(pidFileName);
629
630 #ifndef NOBALLOC
631 initBlockHeap();
632 #endif
633 init_dlink_nodes();
634 init_callbacks();
635 initialize_message_files();
636 dbuf_init();
637 init_hash();
638 init_ip_hash_table(); /* client host ip hash table */
639 init_host_hash(); /* Host-hashtable. */
640 clear_tree_parse();
641 init_client();
642 init_class();
643 init_whowas();
644 watch_init();
645 read_conf_files(1); /* cold start init conf files */
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
662 strlcpy(me.name, ServerInfo.name, sizeof(me.name));
663
664 /* serverinfo{} description must exist. If not, error out.*/
665 if (ServerInfo.description == NULL)
666 {
667 ilog(L_CRIT,
668 "ERROR: No server description specified in serverinfo block.");
669 exit(EXIT_FAILURE);
670 }
671
672 strlcpy(me.info, ServerInfo.description, sizeof(me.info));
673
674 me.from = &me;
675 me.servptr = &me;
676
677 SetMe(&me);
678 make_server(&me);
679
680 me.lasttime = me.since = me.firsttime = CurrentTime;
681 hash_add_client(&me);
682
683 /* add ourselves to global_serv_list */
684 dlinkAdd(&me, make_dlink_node(), &global_serv_list);
685
686 check_class();
687
688 #ifndef STATIC_MODULES
689 if (chdir(MODPATH))
690 {
691 ilog (L_CRIT, "Could not load core modules. Terminating!");
692 exit(EXIT_FAILURE);
693 }
694
695 load_all_modules(1);
696 load_conf_modules();
697 load_core_modules(1);
698 /* Go back to DPATH after checking to see if we can chdir to MODPATH */
699 chdir(ConfigFileEntry.dpath);
700 #else
701 load_all_modules(1);
702 #endif
703 /*
704 * assemble_umode_buffer() has to be called after
705 * reading conf/loading modules.
706 */
707 assemble_umode_buffer();
708
709 write_pidfile(pidFileName);
710
711 ilog(L_NOTICE, "Server Ready");
712
713 eventAddIsh("cleanup_glines", cleanup_glines, NULL, CLEANUP_GLINES_TIME);
714 eventAddIsh("cleanup_tklines", cleanup_tklines, NULL, CLEANUP_TKLINES_TIME);
715
716 /* We want try_connections to be called as soon as possible now! -- adrian */
717 /* No, 'cause after a restart it would cause all sorts of nick collides */
718 eventAddIsh("try_connections", try_connections, NULL, STARTUP_CONNECTIONS_TIME);
719
720 eventAddIsh("collect_zipstats", collect_zipstats, NULL, ZIPSTATS_TIME);
721
722 /* Setup the timeout check. I'll shift it later :) -- adrian */
723 eventAddIsh("comm_checktimeouts", comm_checktimeouts, NULL, 1);
724
725 if (ConfigServerHide.links_delay > 0)
726 eventAddIsh("write_links_file", write_links_file, NULL, ConfigServerHide.links_delay);
727 else
728 ConfigServerHide.links_disabled = 1;
729
730 if (splitmode)
731 eventAddIsh("check_splitmode", check_splitmode, NULL, 60);
732
733 io_loop();
734 return 0;
735 }

Properties

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