ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/branches/newio/src/ircd.c
Revision: 2396
Committed: Mon Jul 15 19:51:03 2013 UTC (13 years ago) by michael
Content type: text/x-csrc
File size: 16991 byte(s)
Log Message:
- ioengine changes as of 15JUL13

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 "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
98 int default_server_capabs;
99 unsigned int splitmode;
100 unsigned int splitchecking;
101 unsigned int split_users;
102 unsigned int split_servers;
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 /*
118 * print_startup - print startup information
119 */
120 static void
121 print_startup(int pid)
122 {
123 printf("ircd: version %s\n", ircd_version);
124 printf("ircd: pid %d\n", pid);
125 printf("ircd: running in %s mode from %s\n", !server_state.foreground ? "background"
126 : "foreground", ConfigFileEntry.dpath);
127 }
128
129 static void
130 make_daemon(void)
131 {
132 int pid;
133
134 if ((pid = fork()) < 0)
135 {
136 perror("fork");
137 exit(EXIT_FAILURE);
138 }
139 else if (pid > 0)
140 {
141 print_startup(pid);
142 exit(EXIT_SUCCESS);
143 }
144
145 setsid();
146 }
147
148 static int printVersion = 0;
149
150 static struct lgetopt myopts[] = {
151 {"configfile", &ConfigFileEntry.configfile,
152 STRING, "File to use for ircd.conf"},
153 {"glinefile", &ConfigFileEntry.glinefile,
154 STRING, "File to use for gline database"},
155 {"klinefile", &ConfigFileEntry.klinefile,
156 STRING, "File to use for kline database"},
157 {"dlinefile", &ConfigFileEntry.dlinefile,
158 STRING, "File to use for dline database"},
159 {"xlinefile", &ConfigFileEntry.xlinefile,
160 STRING, "File to use for xline database"},
161 {"resvfile", &ConfigFileEntry.resvfile,
162 STRING, "File to use for resv database"},
163 {"logfile", &logFileName,
164 STRING, "File to use for ircd.log"},
165 {"pidfile", &pidFileName,
166 STRING, "File to use for process ID"},
167 {"foreground", &server_state.foreground,
168 YESNO, "Run in foreground (don't detach)"},
169 {"version", &printVersion,
170 YESNO, "Print version and exit"},
171 {"help", NULL, USAGE, "Print this text"},
172 {NULL, NULL, STRING, NULL},
173 };
174
175 void
176 set_time(void)
177 {
178 static char to_send[IRCD_BUFSIZE];
179 struct timeval newtime = { .tv_sec = 0, .tv_usec = 0 };
180
181 if (gettimeofday(&newtime, NULL) == -1)
182 {
183 ilog(LOG_TYPE_IRCD, "Clock Failure (%s), TS can be corrupted",
184 strerror(errno));
185 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
186 "Clock Failure (%s), TS can be corrupted",
187 strerror(errno));
188 restart("Clock Failure");
189 }
190
191 if (newtime.tv_sec < CurrentTime)
192 {
193 snprintf(to_send, sizeof(to_send),
194 "System clock is running backwards - (%lu < %lu)",
195 (unsigned long)newtime.tv_sec, (unsigned long)CurrentTime);
196 report_error(L_ALL, to_send, me.name, 0);
197 }
198
199 SystemTime.tv_sec = newtime.tv_sec;
200 SystemTime.tv_usec = newtime.tv_usec;
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);
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 motd_recache();
253 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
254 "Got signal SIGUSR1, reloading motd files");
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 /* End of global set options */
296 }
297
298 /* initialize_server_capabs()
299 *
300 * inputs - none
301 * output - none
302 */
303 static void
304 initialize_server_capabs(void)
305 {
306 add_capability("QS", CAP_QS, 1);
307 add_capability("EOB", CAP_EOB, 1);
308 add_capability("TS6", CAP_TS6, 0);
309 add_capability("CLUSTER", CAP_CLUSTER, 1);
310 // add_capability("FAKEHOST", CAP_FAKEHOST, 1);
311 add_capability("SVS", CAP_SVS, 1);
312 #ifdef HALFOPS
313 add_capability("HOPS", CAP_HOPS, 1);
314 #endif
315 }
316
317 /* write_pidfile()
318 *
319 * inputs - filename+path of pid file
320 * output - NONE
321 * side effects - write the pid of the ircd to filename
322 */
323 static void
324 write_pidfile(const char *filename)
325 {
326 FILE *fb;
327
328 if ((fb = fopen(filename, "w")))
329 {
330 char buff[32];
331 unsigned int pid = (unsigned int)getpid();
332
333 snprintf(buff, sizeof(buff), "%u\n", pid);
334
335 if ((fputs(buff, fb) == -1))
336 ilog(LOG_TYPE_IRCD, "Error writing %u to pid file %s (%s)",
337 pid, filename, strerror(errno));
338
339 fclose(fb);
340 }
341 else
342 {
343 ilog(LOG_TYPE_IRCD, "Error opening pid file %s", filename);
344 }
345 }
346
347 /* check_pidfile()
348 *
349 * inputs - filename+path of pid file
350 * output - none
351 * side effects - reads pid from pidfile and checks if ircd is in process
352 * list. if it is, gracefully exits
353 * -kre
354 */
355 static void
356 check_pidfile(const char *filename)
357 {
358 FILE *fb;
359 char buff[32];
360 pid_t pidfromfile;
361
362 /* Don't do logging here, since we don't have log() initialised */
363 if ((fb = fopen(filename, "r")))
364 {
365 if (fgets(buff, 20, fb) == NULL)
366 {
367 /* log(L_ERROR, "Error reading from pid file %s (%s)", filename,
368 * strerror(errno));
369 */
370 }
371 else
372 {
373 pidfromfile = atoi(buff);
374
375 if (!kill(pidfromfile, 0))
376 {
377 /* log(L_ERROR, "Server is already running"); */
378 printf("ircd: daemon is already running\n");
379 exit(-1);
380 }
381 }
382
383 fclose(fb);
384 }
385 else if (errno != ENOENT)
386 {
387 /* log(L_ERROR, "Error opening pid file %s", filename); */
388 }
389 }
390
391 /* setup_corefile()
392 *
393 * inputs - nothing
394 * output - nothing
395 * side effects - setups corefile to system limits.
396 * -kre
397 */
398 static void
399 setup_corefile(void)
400 {
401 #ifdef HAVE_SYS_RESOURCE_H
402 struct rlimit rlim; /* resource limits */
403
404 /* Set corefilesize to maximum */
405 if (!getrlimit(RLIMIT_CORE, &rlim))
406 {
407 rlim.rlim_cur = rlim.rlim_max;
408 setrlimit(RLIMIT_CORE, &rlim);
409 }
410 #endif
411 }
412
413 #ifdef HAVE_LIBCRYPTO
414 static int
415 always_accept_verify_cb(int preverify_ok, X509_STORE_CTX *x509_ctx)
416 {
417 return 1;
418 }
419 #endif
420
421 /* init_ssl()
422 *
423 * inputs - nothing
424 * output - nothing
425 * side effects - setups SSL context.
426 */
427 static void
428 ssl_init(void)
429 {
430 #ifdef HAVE_LIBCRYPTO
431 SSL_load_error_strings();
432 SSLeay_add_ssl_algorithms();
433
434 if ((ServerInfo.server_ctx = SSL_CTX_new(SSLv23_server_method())) == NULL)
435 {
436 const char *s;
437
438 fprintf(stderr, "ERROR: Could not initialize the SSL Server context -- %s\n",
439 s = ERR_lib_error_string(ERR_get_error()));
440 ilog(LOG_TYPE_IRCD, "ERROR: Could not initialize the SSL Server context -- %s\n", s);
441 }
442
443 SSL_CTX_set_options(ServerInfo.server_ctx, SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3|SSL_OP_NO_TLSv1);
444 SSL_CTX_set_options(ServerInfo.server_ctx, SSL_OP_TLS_ROLLBACK_BUG|SSL_OP_ALL);
445 SSL_CTX_set_verify(ServerInfo.server_ctx, SSL_VERIFY_PEER|SSL_VERIFY_CLIENT_ONCE,
446 always_accept_verify_cb);
447
448 if ((ServerInfo.client_ctx = SSL_CTX_new(SSLv23_client_method())) == NULL)
449 {
450 const char *s;
451
452 fprintf(stderr, "ERROR: Could not initialize the SSL Client context -- %s\n",
453 s = ERR_lib_error_string(ERR_get_error()));
454 ilog(LOG_TYPE_IRCD, "ERROR: Could not initialize the SSL Client context -- %s\n", s);
455 }
456
457 SSL_CTX_set_options(ServerInfo.client_ctx, SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3|SSL_OP_NO_TLSv1);
458 SSL_CTX_set_options(ServerInfo.client_ctx, SSL_OP_TLS_ROLLBACK_BUG|SSL_OP_ALL);
459 SSL_CTX_set_verify(ServerInfo.client_ctx, SSL_VERIFY_PEER|SSL_VERIFY_CLIENT_ONCE,
460 always_accept_verify_cb);
461 #endif /* HAVE_LIBCRYPTO */
462 }
463
464 int
465 main(int argc, char *argv[])
466 {
467 /* Check to see if the user is running us as root, which is a nono */
468 if (geteuid() == 0)
469 {
470 fprintf(stderr, "Don't run ircd as root!!!\n");
471 return -1;
472 }
473
474 /* Setup corefile size immediately after boot -kre */
475 setup_corefile();
476
477 /* save server boot time right away, so getrusage works correctly */
478 set_time();
479
480 /* It ain't random, but it ought to be a little harder to guess */
481 init_genrand(SystemTime.tv_sec ^ (SystemTime.tv_usec | (getpid() << 20)));
482
483 me.localClient = &meConnection;
484 dlinkAdd(&me, &me.node, &global_client_list); /* Pointer to beginning
485 of Client list */
486 /* Initialise the channel capability usage counts... */
487 init_chcap_usage_counts();
488
489 ConfigFileEntry.dpath = DPATH;
490 ConfigFileEntry.configfile = CPATH; /* Server configuration file */
491 ConfigFileEntry.klinefile = KPATH; /* Server kline file */
492 ConfigFileEntry.glinefile = GPATH; /* Server gline file */
493 ConfigFileEntry.xlinefile = XPATH; /* Server xline file */
494 ConfigFileEntry.dlinefile = DLPATH; /* dline file */
495 ConfigFileEntry.resvfile = RESVPATH; /* resv file */
496
497 myargv = argv;
498 umask(077); /* better safe than sorry --SRB */
499
500 parseargs(&argc, &argv, myopts);
501
502 if (printVersion)
503 {
504 printf("ircd: version %s\n", ircd_version);
505 exit(EXIT_SUCCESS);
506 }
507
508 if (chdir(ConfigFileEntry.dpath))
509 {
510 perror("chdir");
511 exit(EXIT_FAILURE);
512 }
513
514 ssl_init();
515
516 if (!server_state.foreground)
517 {
518 make_daemon();
519 close_standard_fds(); /* this needs to be before init_netio()! */
520 }
521 else
522 print_startup(getpid());
523
524 event_init(MAXCONNECTIONS);
525 setup_signals();
526
527 /* Init the event subsystem */
528 eventInit();
529
530 log_set_file(LOG_TYPE_IRCD, 0, logFileName);
531
532 /* Check if there is pidfile and daemon already running */
533 check_pidfile(pidFileName);
534
535 mp_pool_init();
536 init_dlink_nodes();
537 init_isupport();
538 dbuf_init();
539 hash_init();
540 init_ip_hash_table(); /* client host ip hash table */
541 init_host_hash(); /* Host-hashtable. */
542 client_init();
543 class_init();
544 whowas_init();
545 watch_init();
546 init_resolver(); /* Needs to be setup before the io loop */
547 modules_init();
548 read_conf_files(1); /* cold start init conf files */
549 init_uid();
550 initialize_server_capabs(); /* Set up default_server_capabs */
551 initialize_global_set_options();
552 channel_init();
553 read_links_file();
554 motd_init();
555 #ifdef HAVE_LIBGEOIP
556 geoip_ctx = GeoIP_new(GEOIP_MEMORY_CACHE);
557 #endif
558
559 if (EmptyString(ServerInfo.sid))
560 {
561 ilog(LOG_TYPE_IRCD, "ERROR: No server id specified in serverinfo block.");
562 exit(EXIT_FAILURE);
563 }
564
565 strlcpy(me.id, ServerInfo.sid, sizeof(me.id));
566
567 if (EmptyString(ServerInfo.name))
568 {
569 ilog(LOG_TYPE_IRCD, "ERROR: No server name specified in serverinfo block.");
570 exit(EXIT_FAILURE);
571 }
572
573 strlcpy(me.name, ServerInfo.name, sizeof(me.name));
574
575 /* serverinfo{} description must exist. If not, error out.*/
576 if (EmptyString(ServerInfo.description))
577 {
578 ilog(LOG_TYPE_IRCD, "ERROR: No server description specified in serverinfo block.");
579 exit(EXIT_FAILURE);
580 }
581
582 strlcpy(me.info, ServerInfo.description, sizeof(me.info));
583
584 me.from = &me;
585 me.servptr = &me;
586 me.localClient->lasttime = CurrentTime;
587 me.localClient->since = CurrentTime;
588 me.localClient->firsttime = CurrentTime;
589
590 SetMe(&me);
591 make_server(&me);
592
593 hash_add_id(&me);
594 hash_add_client(&me);
595
596 /* add ourselves to global_serv_list */
597 dlinkAdd(&me, make_dlink_node(), &global_serv_list);
598
599 load_kline_database();
600 load_dline_database();
601 load_gline_database();
602 load_xline_database();
603 load_resv_database();
604
605 if (chdir(MODPATH))
606 {
607 ilog(LOG_TYPE_IRCD, "Could not load core modules. Terminating!");
608 exit(EXIT_FAILURE);
609 }
610
611 load_all_modules(1);
612 load_conf_modules();
613 load_core_modules(1);
614
615 /* Go back to DPATH after checking to see if we can chdir to MODPATH */
616 if (chdir(ConfigFileEntry.dpath))
617 {
618 perror("chdir");
619 exit(EXIT_FAILURE);
620 }
621
622 /*
623 * assemble_umode_buffer() has to be called after
624 * reading conf/loading modules.
625 */
626 assemble_umode_buffer();
627
628 write_pidfile(pidFileName);
629
630 ilog(LOG_TYPE_IRCD, "Server Ready");
631
632 timer_add(timer_init(&cleanup_glines_timer), cleanup_glines, 0, TT_PERIODIC, CLEANUP_GLINES_TIME);
633 timer_add(timer_init(&cleanup_tklines_timer), cleanup_tklines, 0, TT_PERIODIC, CLEANUP_TKLINES_TIME);
634 timer_add(timer_init(&save_databases_timer), save_all_databases, 0, TT_PERIODIC, DATABASE_UPDATE_TIMEOUT);
635 timer_add(timer_init(&connect_timer), try_connections, 0, TT_RELATIVE, 1);
636
637 if (ConfigServerHide.links_delay > 0)
638 timer_add(timer_init(&write_links_timer), write_links_file, 0, TT_PERIODIC, ConfigServerHide.links_delay);
639 else
640 ConfigServerHide.links_disabled = 1;
641
642 if (splitmode)
643 timer_add(timer_init(&splitmode_timer), check_splitmode, 0, TT_PERIODIC, 60);
644
645 event_loop();
646 return 0;
647 }

Properties

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