/[svn]/branches/newio/src/ircd.c
ViewVC logotype

Contents of /branches/newio/src/ircd.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2415 - (show annotations)
Sun Jul 21 14:33:22 2013 UTC (6 years, 11 months ago) by michael
File MIME type: text/x-chdr
File size: 17344 byte(s)
- ioengine changes as of 21JUL13

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 "ioengine.h"
33 #include "hash.h"
34 #include "irc_string.h"
35 #include "ircd_signal.h"
36 #include "s_gline.h"
37 #include "motd.h"
38 #include "conf.h"
39 #include "hostmask.h"
40 #include "numeric.h"
41 #include "packet.h"
42 #include "parse.h"
43 #include "irc_res.h"
44 #include "restart.h"
45 #include "rng_mt.h"
46 #include "s_auth.h"
47 #include "s_bsd.h"
48 #include "log.h"
49 #include "s_misc.h"
50 #include "s_serv.h" /* try_connections */
51 #include "send.h"
52 #include "whowas.h"
53 #include "modules.h"
54 #include "memory.h"
55 #include "mempool.h"
56 #include "hook.h"
57 #include "ircd_getopt.h"
58 #include "supported.h"
59 #include "watch.h"
60 #include "conf_db.h"
61 #include "conf_class.h"
62
63
64 struct Timer save_databases_timer;
65 struct Timer write_links_timer;
66 struct Timer splitmode_timer;
67 struct Timer cleanup_glines_timer;
68 struct Timer cleanup_tklines_timer;
69
70 #ifdef HAVE_LIBGEOIP
71 GeoIP *geoip_ctx;
72 #endif
73 /* /quote set variables */
74 struct SetOptions GlobalSetOptions;
75
76 /* configuration set from ircd.conf */
77 struct config_file_entry ConfigFileEntry;
78 /* server info set from ircd.conf */
79 struct server_info ServerInfo;
80 /* admin info set from ircd.conf */
81 struct admin_info AdminInfo = { NULL, NULL, NULL };
82 struct Counter Count = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
83 struct ServerState_t server_state = { 0 };
84 struct logging_entry ConfigLoggingEntry = { .use_logging = 1 };
85 struct ServerStatistics ServerStats;
86 struct timeval SystemTime;
87 struct Client me; /* That's me */
88 struct Connection meConnection; /* That's also part of me */
89
90 const char *logFileName = LPATH;
91 const char *pidFileName = PPATH;
92
93 char **myargv;
94
95 int dorehash = 0;
96 int doremotd = 0;
97 /** Non-zero until we want to exit. */
98 int running = 1;
99
100 int default_server_capabs;
101 unsigned int splitmode;
102 unsigned int splitchecking;
103 unsigned int split_users;
104 unsigned int split_servers;
105
106 /* Do klines the same way hybrid-6 did them, i.e. at the
107 * top of the next io_loop instead of in the same loop as
108 * the klines are being applied.
109 *
110 * This should fix strange CPU starvation as very indirectly reported.
111 * (Why do you people not email bug reports? WHY? WHY?)
112 *
113 * - Dianora
114 */
115
116 int rehashed_klines = 0;
117
118
119 /*
120 * print_startup - print startup information
121 */
122 static void
123 print_startup(int pid)
124 {
125 printf("ircd: version %s\n", ircd_version);
126 printf("ircd: pid %d\n", pid);
127 printf("ircd: running in %s mode from %s\n", !server_state.foreground ? "background"
128 : "foreground", ConfigFileEntry.dpath);
129 }
130
131 static void
132 make_daemon(void)
133 {
134 int pid;
135
136 if ((pid = fork()) < 0)
137 {
138 perror("fork");
139 exit(EXIT_FAILURE);
140 }
141 else if (pid > 0)
142 {
143 print_startup(pid);
144 exit(EXIT_SUCCESS);
145 }
146
147 setsid();
148 }
149
150 static int printVersion = 0;
151
152 static struct lgetopt myopts[] = {
153 {"configfile", &ConfigFileEntry.configfile,
154 STRING, "File to use for ircd.conf"},
155 {"glinefile", &ConfigFileEntry.glinefile,
156 STRING, "File to use for gline database"},
157 {"klinefile", &ConfigFileEntry.klinefile,
158 STRING, "File to use for kline database"},
159 {"dlinefile", &ConfigFileEntry.dlinefile,
160 STRING, "File to use for dline database"},
161 {"xlinefile", &ConfigFileEntry.xlinefile,
162 STRING, "File to use for xline database"},
163 {"resvfile", &ConfigFileEntry.resvfile,
164 STRING, "File to use for resv database"},
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[IRCD_BUFSIZE];
181 struct timeval newtime = { .tv_sec = 0, .tv_usec = 0 };
182
183 if (gettimeofday(&newtime, NULL) == -1)
184 {
185 ilog(LOG_TYPE_IRCD, "Clock Failure (%s), TS can be corrupted",
186 strerror(errno));
187 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
188 "Clock Failure (%s), TS can be corrupted",
189 strerror(errno));
190 restart("Clock Failure");
191 }
192
193 if (newtime.tv_sec < CurrentTime)
194 {
195 snprintf(to_send, sizeof(to_send),
196 "System clock is running backwards - (%lu < %lu)",
197 (unsigned long)newtime.tv_sec, (unsigned long)CurrentTime);
198 report_error(L_ALL, to_send, me.name, 0);
199 }
200
201 SystemTime.tv_sec = newtime.tv_sec;
202 SystemTime.tv_usec = newtime.tv_usec;
203 }
204
205 #if 0
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);
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 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 motd_recache();
255 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
256 "Got signal SIGUSR1, reloading motd files");
257 doremotd = 0;
258 }
259 }
260 }
261 #endif
262
263 /* initalialize_global_set_options()
264 *
265 * inputs - none
266 * output - none
267 * side effects - This sets all global set options needed
268 */
269 static void
270 initialize_global_set_options(void)
271 {
272 memset(&GlobalSetOptions, 0, sizeof(GlobalSetOptions));
273
274 GlobalSetOptions.autoconn = 1;
275 GlobalSetOptions.spam_time = MIN_JOIN_LEAVE_TIME;
276 GlobalSetOptions.spam_num = MAX_JOIN_LEAVE_COUNT;
277
278 if (ConfigFileEntry.default_floodcount)
279 GlobalSetOptions.floodcount = ConfigFileEntry.default_floodcount;
280 else
281 GlobalSetOptions.floodcount = 10;
282
283 /* XXX I have no idea what to try here - Dianora */
284 GlobalSetOptions.joinfloodcount = 16;
285 GlobalSetOptions.joinfloodtime = 8;
286
287 split_servers = ConfigChannel.default_split_server_count;
288 split_users = ConfigChannel.default_split_user_count;
289
290 if (split_users && split_servers && (ConfigChannel.no_create_on_split ||
291 ConfigChannel.no_join_on_split))
292 {
293 splitmode = 1;
294 splitchecking = 1;
295 }
296
297 GlobalSetOptions.ident_timeout = IDENT_TIMEOUT;
298 /* End of global set options */
299 }
300
301 /* initialize_server_capabs()
302 *
303 * inputs - none
304 * output - none
305 */
306 static void
307 initialize_server_capabs(void)
308 {
309 add_capability("QS", CAP_QS, 1);
310 add_capability("EOB", CAP_EOB, 1);
311 add_capability("TS6", CAP_TS6, 0);
312 add_capability("CLUSTER", CAP_CLUSTER, 1);
313 // add_capability("FAKEHOST", CAP_FAKEHOST, 1);
314 add_capability("SVS", CAP_SVS, 1);
315 #ifdef HALFOPS
316 add_capability("HOPS", CAP_HOPS, 1);
317 #endif
318 }
319
320 /* write_pidfile()
321 *
322 * inputs - filename+path of pid file
323 * output - NONE
324 * side effects - write the pid of the ircd to filename
325 */
326 static void
327 write_pidfile(const char *filename)
328 {
329 FILE *fb;
330
331 if ((fb = fopen(filename, "w")))
332 {
333 char buff[32];
334 unsigned int pid = (unsigned int)getpid();
335
336 snprintf(buff, sizeof(buff), "%u\n", pid);
337
338 if ((fputs(buff, fb) == -1))
339 ilog(LOG_TYPE_IRCD, "Error writing %u to pid file %s (%s)",
340 pid, filename, strerror(errno));
341
342 fclose(fb);
343 }
344 else
345 {
346 ilog(LOG_TYPE_IRCD, "Error opening pid file %s", filename);
347 }
348 }
349
350 /* check_pidfile()
351 *
352 * inputs - filename+path of pid file
353 * output - none
354 * side effects - reads pid from pidfile and checks if ircd is in process
355 * list. if it is, gracefully exits
356 * -kre
357 */
358 static void
359 check_pidfile(const char *filename)
360 {
361 FILE *fb;
362 char buff[32];
363 pid_t pidfromfile;
364
365 /* Don't do logging here, since we don't have log() initialised */
366 if ((fb = fopen(filename, "r")))
367 {
368 if (fgets(buff, 20, fb) == NULL)
369 {
370 /* log(L_ERROR, "Error reading from pid file %s (%s)", filename,
371 * strerror(errno));
372 */
373 }
374 else
375 {
376 pidfromfile = atoi(buff);
377
378 if (!kill(pidfromfile, 0))
379 {
380 /* log(L_ERROR, "Server is already running"); */
381 printf("ircd: daemon is already running\n");
382 exit(-1);
383 }
384 }
385
386 fclose(fb);
387 }
388 else if (errno != ENOENT)
389 {
390 /* log(L_ERROR, "Error opening pid file %s", filename); */
391 }
392 }
393
394 /* setup_corefile()
395 *
396 * inputs - nothing
397 * output - nothing
398 * side effects - setups corefile to system limits.
399 * -kre
400 */
401 static void
402 setup_corefile(void)
403 {
404 #ifdef HAVE_SYS_RESOURCE_H
405 struct rlimit rlim; /* resource limits */
406
407 /* Set corefilesize to maximum */
408 if (!getrlimit(RLIMIT_CORE, &rlim))
409 {
410 rlim.rlim_cur = rlim.rlim_max;
411 setrlimit(RLIMIT_CORE, &rlim);
412 }
413 #endif
414 }
415
416 #ifdef HAVE_LIBCRYPTO
417 static int
418 always_accept_verify_cb(int preverify_ok, X509_STORE_CTX *x509_ctx)
419 {
420 return 1;
421 }
422 #endif
423
424 /* init_ssl()
425 *
426 * inputs - nothing
427 * output - nothing
428 * side effects - setups SSL context.
429 */
430 static void
431 ssl_init(void)
432 {
433 #ifdef HAVE_LIBCRYPTO
434 SSL_load_error_strings();
435 SSLeay_add_ssl_algorithms();
436
437 if ((ServerInfo.server_ctx = SSL_CTX_new(SSLv23_server_method())) == NULL)
438 {
439 const char *s;
440
441 fprintf(stderr, "ERROR: Could not initialize the SSL Server context -- %s\n",
442 s = ERR_lib_error_string(ERR_get_error()));
443 ilog(LOG_TYPE_IRCD, "ERROR: Could not initialize the SSL Server context -- %s\n", s);
444 }
445
446 SSL_CTX_set_options(ServerInfo.server_ctx, SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3|SSL_OP_NO_TLSv1);
447 SSL_CTX_set_options(ServerInfo.server_ctx, SSL_OP_TLS_ROLLBACK_BUG|SSL_OP_ALL);
448 SSL_CTX_set_verify(ServerInfo.server_ctx, SSL_VERIFY_PEER|SSL_VERIFY_CLIENT_ONCE,
449 always_accept_verify_cb);
450
451 if ((ServerInfo.client_ctx = SSL_CTX_new(SSLv23_client_method())) == NULL)
452 {
453 const char *s;
454
455 fprintf(stderr, "ERROR: Could not initialize the SSL Client context -- %s\n",
456 s = ERR_lib_error_string(ERR_get_error()));
457 ilog(LOG_TYPE_IRCD, "ERROR: Could not initialize the SSL Client context -- %s\n", s);
458 }
459
460 SSL_CTX_set_options(ServerInfo.client_ctx, SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3|SSL_OP_NO_TLSv1);
461 SSL_CTX_set_options(ServerInfo.client_ctx, SSL_OP_TLS_ROLLBACK_BUG|SSL_OP_ALL);
462 SSL_CTX_set_verify(ServerInfo.client_ctx, SSL_VERIFY_PEER|SSL_VERIFY_CLIENT_ONCE,
463 always_accept_verify_cb);
464 #endif /* HAVE_LIBCRYPTO */
465 }
466
467 /* Make sure stdio descriptors (0-2) and profiler descriptor (3)
468 * always go somewhere harmless. Use -foreground for profiling
469 * or executing from gdb */
470 static void
471 close_standard_fds(void)
472 {
473 int i;
474
475 for (i = 0; i < LOWEST_SAFE_FD; i++)
476 {
477 close(i);
478 if (open("/dev/null", O_RDWR) < 0)
479 exit(-1); /* we're hosed if we can't even open /dev/null */
480 }
481 }
482
483 int
484 main(int argc, char *argv[])
485 {
486 /* Check to see if the user is running us as root, which is a nono */
487 if (geteuid() == 0)
488 {
489 fprintf(stderr, "Don't run ircd as root!!!\n");
490 return -1;
491 }
492
493 /* Setup corefile size immediately after boot -kre */
494 setup_corefile();
495
496 /* save server boot time right away, so getrusage works correctly */
497 set_time();
498
499 /* It ain't random, but it ought to be a little harder to guess */
500 init_genrand(SystemTime.tv_sec ^ (SystemTime.tv_usec | (getpid() << 20)));
501
502 me.localClient = &meConnection;
503 dlinkAdd(&me, &me.node, &global_client_list); /* Pointer to beginning
504 of Client list */
505 /* Initialise the channel capability usage counts... */
506 init_chcap_usage_counts();
507
508 ConfigFileEntry.dpath = DPATH;
509 ConfigFileEntry.configfile = CPATH; /* Server configuration file */
510 ConfigFileEntry.klinefile = KPATH; /* Server kline file */
511 ConfigFileEntry.glinefile = GPATH; /* Server gline file */
512 ConfigFileEntry.xlinefile = XPATH; /* Server xline file */
513 ConfigFileEntry.dlinefile = DLPATH; /* dline file */
514 ConfigFileEntry.resvfile = RESVPATH; /* resv file */
515
516 myargv = argv;
517 umask(077); /* better safe than sorry --SRB */
518
519 parseargs(&argc, &argv, myopts);
520
521 if (printVersion)
522 {
523 printf("ircd: version %s\n", ircd_version);
524 exit(EXIT_SUCCESS);
525 }
526
527 if (chdir(ConfigFileEntry.dpath))
528 {
529 perror("chdir");
530 exit(EXIT_FAILURE);
531 }
532
533 ssl_init();
534
535 if (!server_state.foreground)
536 {
537 make_daemon();
538 close_standard_fds(); /* this needs to be before init_netio()! */
539 }
540 else
541 print_startup(getpid());
542
543 event_init(1024 /* XXX */);
544 setup_signals();
545
546 log_set_file(LOG_TYPE_IRCD, 0, logFileName);
547
548 /* Check if there is pidfile and daemon already running */
549 check_pidfile(pidFileName);
550
551 mp_pool_init();
552 init_dlink_nodes();
553 init_isupport();
554 hash_init();
555 init_ip_hash_table(); /* client host ip hash table */
556 init_host_hash(); /* Host-hashtable. */
557 client_init();
558 class_init();
559 whowas_init();
560 watch_init();
561 init_resolver(); /* Needs to be setup before the io loop */
562 modules_init();
563 read_conf_files(1); /* cold start init conf files */
564 init_uid();
565 initialize_server_capabs(); /* Set up default_server_capabs */
566 initialize_global_set_options();
567 channel_init();
568 read_links_file();
569 motd_init();
570 #ifdef HAVE_LIBGEOIP
571 geoip_ctx = GeoIP_new(GEOIP_MEMORY_CACHE);
572 #endif
573
574 if (EmptyString(ServerInfo.sid))
575 {
576 ilog(LOG_TYPE_IRCD, "ERROR: No server id specified in serverinfo block.");
577 exit(EXIT_FAILURE);
578 }
579
580 strlcpy(me.id, ServerInfo.sid, sizeof(me.id));
581
582 if (EmptyString(ServerInfo.name))
583 {
584 ilog(LOG_TYPE_IRCD, "ERROR: No server name specified in serverinfo block.");
585 exit(EXIT_FAILURE);
586 }
587
588 strlcpy(me.name, ServerInfo.name, sizeof(me.name));
589
590 /* serverinfo{} description must exist. If not, error out.*/
591 if (EmptyString(ServerInfo.description))
592 {
593 ilog(LOG_TYPE_IRCD, "ERROR: No server description specified in serverinfo block.");
594 exit(EXIT_FAILURE);
595 }
596
597 strlcpy(me.info, ServerInfo.description, sizeof(me.info));
598
599 me.from = &me;
600 me.servptr = &me;
601 me.localClient->lasttime = CurrentTime;
602 me.localClient->since = CurrentTime;
603 me.localClient->firsttime = CurrentTime;
604
605 SetMe(&me);
606 make_server(&me);
607
608 hash_add_id(&me);
609 hash_add_client(&me);
610
611 /* add ourselves to global_serv_list */
612 dlinkAdd(&me, make_dlink_node(), &global_serv_list);
613
614 load_kline_database();
615 load_dline_database();
616 load_gline_database();
617 load_xline_database();
618 load_resv_database();
619
620 if (chdir(MODPATH))
621 {
622 ilog(LOG_TYPE_IRCD, "Could not load core modules. Terminating!");
623 exit(EXIT_FAILURE);
624 }
625
626 load_all_modules(1);
627 load_conf_modules();
628 load_core_modules(1);
629
630 /* Go back to DPATH after checking to see if we can chdir to MODPATH */
631 if (chdir(ConfigFileEntry.dpath))
632 {
633 perror("chdir");
634 exit(EXIT_FAILURE);
635 }
636
637 /*
638 * assemble_umode_buffer() has to be called after
639 * reading conf/loading modules.
640 */
641 assemble_umode_buffer();
642
643 write_pidfile(pidFileName);
644
645 ilog(LOG_TYPE_IRCD, "Server Ready");
646
647 timer_add(timer_init(&cleanup_glines_timer), cleanup_glines, 0, TT_PERIODIC, CLEANUP_GLINES_TIME);
648 timer_add(timer_init(&cleanup_tklines_timer), cleanup_tklines, 0, TT_PERIODIC, CLEANUP_TKLINES_TIME);
649 timer_add(timer_init(&save_databases_timer), save_all_databases, 0, TT_PERIODIC, DATABASE_UPDATE_TIMEOUT);
650 timer_add(timer_init(&connect_timer), try_connections, 0, TT_RELATIVE, 1);
651
652 if (ConfigServerHide.links_delay > 0)
653 timer_add(timer_init(&write_links_timer), write_links_file, 0, TT_PERIODIC, ConfigServerHide.links_delay);
654 else
655 ConfigServerHide.links_disabled = 1;
656
657 if (splitmode)
658 timer_add(timer_init(&splitmode_timer), check_splitmode, 0, TT_PERIODIC, 60);
659
660 event_loop();
661 return 0;
662 }

Properties

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

svnadmin@ircd-hybrid.org
ViewVC Help
Powered by ViewVC 1.1.28