ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/src/ircd.c
Revision: 185
Committed: Sun Oct 23 15:39:00 2005 UTC (19 years, 10 months ago) by adx
Content type: text/x-csrc
File size: 18506 byte(s)
Log Message:
* dynamic module support for win32

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

Properties

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