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

Properties

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