ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/src/ircd.c
Revision: 228
Committed: Thu Nov 3 20:22:19 2005 UTC (19 years, 9 months ago) by db
Content type: text/x-csrc
File size: 18770 byte(s)
Log Message:
- move me initialisation after libio_init() so make_dlink() works


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

Properties

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