ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/src/ircd.c
Revision: 77
Committed: Tue Oct 4 20:45:31 2005 UTC (18 years, 5 months ago) by adx
Content type: text/x-csrc
File size: 18503 byte(s)
Log Message:
- misc win32 fixes

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

Properties

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