/[svn]/ircd-hybrid/branches/8.2.x/src/ircd.c
ViewVC logotype

Contents of /ircd-hybrid/branches/8.2.x/src/ircd.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5565 - (show annotations)
Sat Feb 14 20:15:28 2015 UTC (5 years, 5 months ago) by michael
File MIME type: text/x-chdr
File size: 16628 byte(s)
- ircd.c:write_pidfile(): cosmetical fixes to fopen/fputs error messages to be consistent with all other error messages

1 /*
2 * ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3 *
4 * Copyright (c) 1997-2015 ircd-hybrid development team
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
19 * USA
20 */
21
22 /*! \file ircd.c
23 * \brief Starts up and runs the ircd.
24 * \version $Id$
25 */
26
27 #include "stdinc.h"
28 #include "user.h"
29 #include "list.h"
30 #include "ircd.h"
31 #include "channel.h"
32 #include "client.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 "gline.h"
39 #include "motd.h"
40 #include "conf.h"
41 #include "hostmask.h"
42 #include "parse.h"
43 #include "res.h"
44 #include "restart.h"
45 #include "rng_mt.h"
46 #include "auth.h"
47 #include "s_bsd.h"
48 #include "log.h"
49 #include "server.h" /* try_connections */
50 #include "send.h"
51 #include "whowas.h"
52 #include "modules.h"
53 #include "memory.h"
54 #include "mempool.h"
55 #include "ircd_getopt.h"
56 #include "watch.h"
57 #include "conf_db.h"
58 #include "conf_class.h"
59 #include "ipcache.h"
60
61
62 #ifdef HAVE_LIBGEOIP
63 GeoIP *geoip_ctx;
64 #endif
65
66 struct timeval SystemTime;
67 struct Connection meConnection; /* That's also part of me */
68 struct Client me = { .connection = &meConnection }; /* That's me */
69
70 char **myargv;
71 const char *logFileName = LPATH;
72 const char *pidFileName = PPATH;
73
74 unsigned int dorehash;
75 unsigned int doremotd;
76 unsigned int default_server_capabs;
77 unsigned int splitmode;
78 unsigned int splitchecking;
79 unsigned int split_users;
80 unsigned int split_servers;
81
82 static struct event event_cleanup_glines =
83 {
84 .name = "cleanup_glines",
85 .handler = cleanup_glines,
86 .when = CLEANUP_GLINES_TIME
87 };
88
89 static struct event event_cleanup_tklines =
90 {
91 .name = "cleanup_tklines",
92 .handler = cleanup_tklines,
93 .when = CLEANUP_TKLINES_TIME
94 };
95
96 static struct event event_try_connections =
97 {
98 .name = "try_connections",
99 .handler = try_connections,
100 .when = STARTUP_CONNECTIONS_TIME
101 };
102
103 static struct event event_comm_checktimeouts =
104 {
105 .name = "comm_checktimeouts",
106 .handler = comm_checktimeouts,
107 .when = 1
108 };
109
110 static struct event event_save_all_databases =
111 {
112 .name = "save_all_databases",
113 .handler = save_all_databases,
114 .when = DATABASE_UPDATE_TIMEOUT
115 };
116
117 struct event event_write_links_file =
118 {
119 .name = "write_links_file",
120 .handler = write_links_file,
121 };
122
123
124 /*
125 * print_startup - print startup information
126 */
127 static void
128 print_startup(int pid)
129 {
130 printf("ircd: version %s(%s)\n", ircd_version, serno);
131 printf("ircd: pid %d\n", pid);
132 printf("ircd: running in %s mode from %s\n", !server_state.foreground ? "background"
133 : "foreground", ConfigGeneral.dpath);
134 }
135
136 static void
137 make_daemon(void)
138 {
139 int pid;
140
141 if ((pid = fork()) < 0)
142 {
143 perror("fork");
144 exit(EXIT_FAILURE);
145 }
146 else if (pid > 0)
147 {
148 print_startup(pid);
149 exit(EXIT_SUCCESS);
150 }
151
152 setsid();
153 }
154
155 static int printVersion = 0;
156
157 static struct lgetopt myopts[] =
158 {
159 {"configfile", &ConfigGeneral.configfile,
160 STRING, "File to use for ircd.conf"},
161 {"glinefile", &ConfigGeneral.glinefile,
162 STRING, "File to use for gline database"},
163 {"klinefile", &ConfigGeneral.klinefile,
164 STRING, "File to use for kline database"},
165 {"dlinefile", &ConfigGeneral.dlinefile,
166 STRING, "File to use for dline database"},
167 {"xlinefile", &ConfigGeneral.xlinefile,
168 STRING, "File to use for xline database"},
169 {"resvfile", &ConfigGeneral.resvfile,
170 STRING, "File to use for resv database"},
171 {"logfile", &logFileName,
172 STRING, "File to use for ircd.log"},
173 {"pidfile", &pidFileName,
174 STRING, "File to use for process ID"},
175 {"foreground", &server_state.foreground,
176 YESNO, "Run in foreground (don't detach)"},
177 {"version", &printVersion,
178 YESNO, "Print version and exit"},
179 {"help", NULL, USAGE, "Print this text"},
180 {NULL, NULL, STRING, NULL},
181 };
182
183 void
184 set_time(void)
185 {
186 struct timeval newtime = { .tv_sec = 0, .tv_usec = 0 };
187
188 if (gettimeofday(&newtime, NULL) == -1)
189 {
190 ilog(LOG_TYPE_IRCD, "Clock Failure (%s), TS can be corrupted",
191 strerror(errno));
192 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
193 "Clock Failure (%s), TS can be corrupted",
194 strerror(errno));
195 server_die("Clock Failure", 1);
196 }
197
198 if (newtime.tv_sec < CurrentTime)
199 {
200 ilog(LOG_TYPE_IRCD, "System clock is running backwards - (%lu < %lu)",
201 (unsigned long)newtime.tv_sec, (unsigned long)CurrentTime);
202 sendto_realops_flags(UMODE_DEBUG, L_ALL, SEND_NOTICE,
203 "System clock is running backwards - (%lu < %lu)",
204 (unsigned long)newtime.tv_sec,
205 (unsigned long)CurrentTime);
206 set_back_events(CurrentTime - newtime.tv_sec);
207 }
208
209 SystemTime.tv_sec = newtime.tv_sec;
210 SystemTime.tv_usec = newtime.tv_usec;
211 }
212
213 static void
214 io_loop(void)
215 {
216 while (1)
217 {
218 if (listing_client_list.head)
219 {
220 dlink_node *node = NULL, *node_next = NULL;
221 DLINK_FOREACH_SAFE(node, node_next, listing_client_list.head)
222 safe_list_channels(node->data, 0);
223 }
224
225 /* Run pending events */
226 event_run();
227
228 comm_select();
229 exit_aborted_clients();
230 free_exited_clients();
231
232 /* Check to see whether we have to rehash the configuration .. */
233 if (dorehash)
234 {
235 conf_rehash(1);
236 dorehash = 0;
237 }
238
239 if (doremotd)
240 {
241 motd_recache();
242 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
243 "Got signal SIGUSR1, reloading motd file(s)");
244 doremotd = 0;
245 }
246 }
247 }
248
249 /* initalialize_global_set_options()
250 *
251 * inputs - none
252 * output - none
253 * side effects - This sets all global set options needed
254 */
255 static void
256 initialize_global_set_options(void)
257 {
258 GlobalSetOptions.maxclients = ConfigServerInfo.default_max_clients;
259 GlobalSetOptions.autoconn = 1;
260 GlobalSetOptions.spam_time = MIN_JOIN_LEAVE_TIME;
261 GlobalSetOptions.spam_num = MAX_JOIN_LEAVE_COUNT;
262 GlobalSetOptions.floodcount = ConfigGeneral.default_floodcount;
263 GlobalSetOptions.joinfloodcount = ConfigChannel.default_join_flood_count;
264 GlobalSetOptions.joinfloodtime = ConfigChannel.default_join_flood_time;
265
266 split_servers = ConfigChannel.default_split_server_count;
267 split_users = ConfigChannel.default_split_user_count;
268
269 if (split_users && split_servers && (ConfigChannel.no_create_on_split ||
270 ConfigChannel.no_join_on_split))
271 {
272 splitmode = 1;
273 splitchecking = 1;
274 }
275
276 GlobalSetOptions.ident_timeout = IDENT_TIMEOUT;
277 }
278
279 /* initialize_server_capabs()
280 *
281 * inputs - none
282 * output - none
283 */
284 static void
285 initialize_server_capabs(void)
286 {
287 add_capability("QS", CAP_QS, 1);
288 add_capability("EOB", CAP_EOB, 1);
289 add_capability("TS6", CAP_TS6, 0);
290 add_capability("CLUSTER", CAP_CLUSTER, 1);
291 add_capability("SVS", CAP_SVS, 1);
292 add_capability("CHW", CAP_CHW, 1);
293 add_capability("HOPS", CAP_HOPS, 1);
294 }
295
296 /* write_pidfile()
297 *
298 * inputs - filename+path of pid file
299 * output - NONE
300 * side effects - write the pid of the ircd to filename
301 */
302 static void
303 write_pidfile(const char *filename)
304 {
305 FILE *fb;
306
307 if ((fb = fopen(filename, "w")))
308 {
309 char buff[IRCD_BUFSIZE];
310 unsigned int pid = (unsigned int)getpid();
311
312 snprintf(buff, sizeof(buff), "%u\n", pid);
313
314 if (fputs(buff, fb) == -1)
315 ilog(LOG_TYPE_IRCD, "Error writing %u to pid file %s: %s",
316 pid, filename, strerror(errno));
317
318 fclose(fb);
319 }
320 else
321 ilog(LOG_TYPE_IRCD, "Error opening pid file %s: %s",
322 filename, strerror(errno));
323 }
324
325 /* check_pidfile()
326 *
327 * inputs - filename+path of pid file
328 * output - none
329 * side effects - reads pid from pidfile and checks if ircd is in process
330 * list. if it is, gracefully exits
331 * -kre
332 */
333 static void
334 check_pidfile(const char *filename)
335 {
336 FILE *fb;
337 char buff[IRCD_BUFSIZE];
338 pid_t pidfromfile;
339
340 /* Don't do logging here, since we don't have log() initialised */
341 if ((fb = fopen(filename, "r")))
342 {
343 if (!fgets(buff, 20, fb))
344 {
345 /* log(L_ERROR, "Error reading from pid file %s (%s)", filename,
346 * strerror(errno));
347 */
348 }
349 else
350 {
351 pidfromfile = atoi(buff);
352
353 if (!kill(pidfromfile, 0))
354 {
355 /* log(L_ERROR, "Server is already running"); */
356 printf("ircd: daemon is already running\n");
357 exit(-1);
358 }
359 }
360
361 fclose(fb);
362 }
363 else if (errno != ENOENT)
364 {
365 /* log(L_ERROR, "Error opening pid file %s", filename); */
366 }
367 }
368
369 /* setup_corefile()
370 *
371 * inputs - nothing
372 * output - nothing
373 * side effects - setups corefile to system limits.
374 * -kre
375 */
376 static void
377 setup_corefile(void)
378 {
379 #ifdef HAVE_SYS_RESOURCE_H
380 struct rlimit rlim; /* resource limits */
381
382 /* Set corefilesize to maximum */
383 if (!getrlimit(RLIMIT_CORE, &rlim))
384 {
385 rlim.rlim_cur = rlim.rlim_max;
386 setrlimit(RLIMIT_CORE, &rlim);
387 }
388 #endif
389 }
390
391 #ifdef HAVE_LIBCRYPTO
392 static int
393 always_accept_verify_cb(int preverify_ok, X509_STORE_CTX *x509_ctx)
394 {
395 return 1;
396 }
397 #endif
398
399 /* init_ssl()
400 *
401 * inputs - nothing
402 * output - nothing
403 * side effects - setups SSL context.
404 */
405 static void
406 ssl_init(void)
407 {
408 #ifdef HAVE_LIBCRYPTO
409 SSL_load_error_strings();
410 SSLeay_add_ssl_algorithms();
411
412 if (!(ConfigServerInfo.server_ctx = SSL_CTX_new(SSLv23_server_method())))
413 {
414 const char *s = ERR_lib_error_string(ERR_get_error());
415
416 fprintf(stderr, "ERROR: Could not initialize the SSL Server context -- %s\n", s);
417 ilog(LOG_TYPE_IRCD, "ERROR: Could not initialize the SSL Server context -- %s", s);
418 exit(EXIT_FAILURE);
419 return; /* Not reached */
420 }
421
422 SSL_CTX_set_options(ConfigServerInfo.server_ctx, SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3|SSL_OP_NO_TICKET);
423 SSL_CTX_set_options(ConfigServerInfo.server_ctx, SSL_OP_SINGLE_DH_USE|SSL_OP_CIPHER_SERVER_PREFERENCE);
424 SSL_CTX_set_verify(ConfigServerInfo.server_ctx, SSL_VERIFY_PEER|SSL_VERIFY_CLIENT_ONCE,
425 always_accept_verify_cb);
426 SSL_CTX_set_session_cache_mode(ConfigServerInfo.server_ctx, SSL_SESS_CACHE_OFF);
427 SSL_CTX_set_cipher_list(ConfigServerInfo.server_ctx, "EECDH+HIGH:EDH+HIGH:HIGH:!aNULL");
428
429 #if OPENSSL_VERSION_NUMBER >= 0x009080FFL && !defined(OPENSSL_NO_ECDH)
430 {
431 EC_KEY *key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
432
433 if (key)
434 {
435 SSL_CTX_set_tmp_ecdh(ConfigServerInfo.server_ctx, key);
436 EC_KEY_free(key);
437 }
438 }
439
440 SSL_CTX_set_options(ConfigServerInfo.server_ctx, SSL_OP_SINGLE_ECDH_USE);
441 #endif
442
443 if (!(ConfigServerInfo.client_ctx = SSL_CTX_new(SSLv23_client_method())))
444 {
445 const char *s = ERR_lib_error_string(ERR_get_error());
446
447 fprintf(stderr, "ERROR: Could not initialize the SSL Client context -- %s\n", s);
448 ilog(LOG_TYPE_IRCD, "ERROR: Could not initialize the SSL Client context -- %s", s);
449 exit(EXIT_FAILURE);
450 return; /* Not reached */
451 }
452
453 SSL_CTX_set_options(ConfigServerInfo.client_ctx, SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3|SSL_OP_NO_TICKET);
454 SSL_CTX_set_options(ConfigServerInfo.client_ctx, SSL_OP_SINGLE_DH_USE);
455 SSL_CTX_set_verify(ConfigServerInfo.client_ctx, SSL_VERIFY_PEER|SSL_VERIFY_CLIENT_ONCE,
456 always_accept_verify_cb);
457 SSL_CTX_set_session_cache_mode(ConfigServerInfo.client_ctx, SSL_SESS_CACHE_OFF);
458 #endif /* HAVE_LIBCRYPTO */
459 }
460
461 int
462 main(int argc, char *argv[])
463 {
464 /* Check to see if the user is running us as root, which is a nono */
465 if (!geteuid())
466 {
467 fprintf(stderr, "ERROR: This server won't run as root/superuser\n");
468 return -1;
469 }
470
471 /* Setup corefile size immediately after boot -kre */
472 setup_corefile();
473
474 /* Save server boot time right away, so getrusage works correctly */
475 set_time();
476
477 /* It's not random, but it ought to be a little harder to guess */
478 init_genrand(SystemTime.tv_sec ^ (SystemTime.tv_usec | (getpid() << 20)));
479
480 dlinkAdd(&me, &me.node, &global_client_list);
481
482 ConfigLog.use_logging = 1;
483 ConfigGeneral.dpath = DPATH;
484 ConfigGeneral.spath = SPATH;
485 ConfigGeneral.mpath = MPATH;
486 ConfigGeneral.configfile = CPATH; /* Server configuration file */
487 ConfigGeneral.klinefile = KPATH; /* Server kline file */
488 ConfigGeneral.glinefile = GPATH; /* Server gline file */
489 ConfigGeneral.xlinefile = XPATH; /* Server xline file */
490 ConfigGeneral.dlinefile = DLPATH; /* dline file */
491 ConfigGeneral.resvfile = RESVPATH; /* resv file */
492
493 myargv = argv;
494 umask(077); /* better safe than sorry --SRB */
495
496 parseargs(&argc, &argv, myopts);
497
498 if (printVersion)
499 {
500 printf("ircd: version %s(%s)\n", ircd_version, serno);
501 exit(EXIT_SUCCESS);
502 }
503
504 if (chdir(ConfigGeneral.dpath))
505 {
506 perror("chdir");
507 exit(EXIT_FAILURE);
508 }
509
510 ssl_init();
511
512 if (!server_state.foreground)
513 {
514 make_daemon();
515 close_standard_fds(); /* this needs to be before init_netio()! */
516 }
517 else
518 print_startup(getpid());
519
520 setup_signals();
521
522 /* We need this to initialise the fd array before anything else */
523 fdlist_init();
524 log_set_file(LOG_TYPE_IRCD, 0, logFileName);
525
526 init_netio(); /* This needs to be setup early ! -- adrian */
527
528 /* Check if there is pidfile and daemon already running */
529 check_pidfile(pidFileName);
530
531 mp_pool_init();
532 init_dlink_nodes();
533 init_isupport();
534 dbuf_init();
535 hash_init();
536 ipcache_init();
537 client_init();
538 class_init();
539 whowas_init();
540 watch_init();
541 auth_init(); /* Initialise the auth code */
542 init_resolver(); /* Needs to be setup before the io loop */
543 modules_init();
544 read_conf_files(1); /* cold start init conf files */
545 init_uid();
546 initialize_server_capabs(); /* Set up default_server_capabs */
547 initialize_global_set_options(); /* Has to be called after read_conf_files() */
548 channel_init();
549 read_links_file();
550 motd_init();
551 user_usermodes_init();
552 #ifdef HAVE_LIBGEOIP
553 geoip_ctx = GeoIP_new(GEOIP_MEMORY_CACHE);
554 #endif
555
556 if (EmptyString(ConfigServerInfo.sid))
557 {
558 ilog(LOG_TYPE_IRCD, "ERROR: No server id specified in serverinfo block.");
559 exit(EXIT_FAILURE);
560 }
561
562 strlcpy(me.id, ConfigServerInfo.sid, sizeof(me.id));
563
564 if (EmptyString(ConfigServerInfo.name))
565 {
566 ilog(LOG_TYPE_IRCD, "ERROR: No server name specified in serverinfo block.");
567 exit(EXIT_FAILURE);
568 }
569
570 strlcpy(me.name, ConfigServerInfo.name, sizeof(me.name));
571
572 /* serverinfo{} description must exist. If not, error out.*/
573 if (EmptyString(ConfigServerInfo.description))
574 {
575 ilog(LOG_TYPE_IRCD, "ERROR: No server description specified in serverinfo block.");
576 exit(EXIT_FAILURE);
577 }
578
579 strlcpy(me.info, ConfigServerInfo.description, sizeof(me.info));
580
581 me.from = &me;
582 me.servptr = &me;
583 me.connection->lasttime = CurrentTime;
584 me.connection->since = CurrentTime;
585 me.connection->firsttime = CurrentTime;
586
587 SetMe(&me);
588 make_server(&me);
589
590 hash_add_id(&me);
591 hash_add_client(&me);
592
593 dlinkAdd(&me, make_dlink_node(), &global_server_list);
594
595 load_kline_database();
596 load_dline_database();
597 load_gline_database();
598 load_xline_database();
599 load_resv_database();
600
601 load_all_modules(1);
602 load_conf_modules();
603 load_core_modules(1);
604
605 write_pidfile(pidFileName);
606
607 ilog(LOG_TYPE_IRCD, "Server Ready");
608
609 event_addish(&event_cleanup_glines, NULL);
610 event_addish(&event_cleanup_tklines, NULL);
611
612 /* We want try_connections to be called as soon as possible now! -- adrian */
613 /* No, 'cause after a restart it would cause all sorts of nick collides */
614 event_addish(&event_try_connections, NULL);
615
616 /* Setup the timeout check. I'll shift it later :) -- adrian */
617 event_add(&event_comm_checktimeouts, NULL);
618
619 event_addish(&event_save_all_databases, NULL);
620
621 if (ConfigServerHide.links_delay > 0)
622 {
623 event_write_links_file.when = ConfigServerHide.links_delay;
624 event_addish(&event_write_links_file, NULL);
625 }
626 else
627 ConfigServerHide.links_disabled = 1;
628
629 if (splitmode)
630 event_addish(&splitmode_event, NULL);
631
632 io_loop();
633 return 0;
634 }

Properties

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

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