1 /*
2 * IRC - Internet Relay Chat, ircd/ircd.c
3 * Copyright (C) 1990 Jarkko Oikarinen and
4 * University of Oulu, Computing Center
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 1, or (at your option)
9 * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20 /** @file
21 * @brief Entry point and other initialization functions for the daemon.
22 * @version $Id$
23 */
24 #include "config.h"
25
26 #include "ircd.h"
27 #include "IPcheck.h"
28 #include "class.h"
29 #include "client.h"
30 #include "crule.h"
31 #include "destruct_event.h"
32 #include "hash.h"
33 #include "ircd_alloc.h"
34 #include "ircd_events.h"
35 #include "ircd_features.h"
36 #include "ircd_log.h"
37 #include "ircd_reply.h"
38 #include "ircd_signal.h"
39 #include "ircd_string.h"
40 #include "ircd_crypt.h"
41 #include "jupe.h"
42 #include "list.h"
43 #include "match.h"
44 #include "motd.h"
45 #include "msg.h"
46 #include "numeric.h"
47 #include "numnicks.h"
48 #include "opercmds.h"
49 #include "parse.h"
50 #include "res.h"
51 #include "s_auth.h"
52 #include "s_bsd.h"
53 #include "s_conf.h"
54 #include "s_debug.h"
55 #include "s_misc.h"
56 #include "s_stats.h"
57 #include "send.h"
58 #include "sys.h"
59 #include "uping.h"
60 #include "userload.h"
61 #include "version.h"
62 #include "whowas.h"
63
64 /* #include <assert.h> -- Now using assert in ircd_log.h */
65 #include <ctype.h>
66 #include <errno.h>
67 #include <fcntl.h>
68 #include <netdb.h>
69 #include <pwd.h>
70 #include <stdio.h>
71 #include <stdlib.h>
72 #include <string.h>
73 #include <sys/time.h>
74 #ifdef HAVE_SYS_RESOURCE_H
75 #include <sys/resource.h>
76 #endif
77 #include <sys/socket.h>
78 #include <sys/stat.h>
79 #include <sys/types.h>
80 #include <unistd.h>
81
82
83
84 /*----------------------------------------------------------------------------
85 * External stuff
86 *--------------------------------------------------------------------------*/
87 extern void init_counters(void);
88 extern void mem_dbg_initialise(void);
89
90 /*----------------------------------------------------------------------------
91 * Constants / Enums
92 *--------------------------------------------------------------------------*/
93 enum {
94 BOOT_DEBUG = 1, /**< Enable debug output. */
95 BOOT_TTY = 2, /**< Stay connected to TTY. */
96 BOOT_CHKCONF = 4 /**< Exit after reading configuration file. */
97 };
98
99
100 /*----------------------------------------------------------------------------
101 * Global data (YUCK!)
102 *--------------------------------------------------------------------------*/
103 struct Client me; /**< That's me */
104 struct Connection me_con; /**< That's me too */
105 struct Client *GlobalClientList = &me; /**< Pointer to beginning of
106 Client list */
107 time_t TSoffset = 0; /**< Offset of timestamps to system clock */
108 time_t CurrentTime; /**< Updated every time we leave select() */
109
110 char *configfile = CPATH; /**< Server configuration file */
111 int debuglevel = -1; /**< Server debug level */
112 char *debugmode = ""; /**< Server debug level */
113 static char *dpath = DPATH; /**< Working directory for daemon */
114 static char *dbg_client; /**< Client specifier for chkconf */
115
116 static struct Timer connect_timer; /**< timer structure for try_connections() */
117 static struct Timer ping_timer; /**< timer structure for check_pings() */
118 static struct Timer destruct_event_timer; /**< timer structure for exec_expired_destruct_events() */
119
120 /** Daemon information. */
121 static struct Daemon thisServer = { 0, 0, 0, 0, 0, 0, -1 };
122
123 /** Non-zero until we want to exit. */
124 int running = 1;
125
126
127 /*----------------------------------------------------------------------------
128 * API: server_die
129 *--------------------------------------------------------------------------*/
130 /** Terminate the server with a message.
131 * @param[in] message Message to log and send to operators.
132 */
server_die(const char * message)133 void server_die(const char *message)
134 {
135 /* log_write will send out message to both log file and as server notice */
136 log_write(LS_SYSTEM, L_CRIT, 0, "Server terminating: %s", message);
137 flush_connections(0);
138 close_connections(1);
139 running = 0;
140 }
141
142 /*----------------------------------------------------------------------------
143 * API: server_panic
144 *--------------------------------------------------------------------------*/
145 /** Immediately terminate the server with a message.
146 * @param[in] message Message to log, but not send to operators.
147 */
server_panic(const char * message)148 void server_panic(const char *message)
149 {
150 /* inhibit sending server notice--we may be panicking due to low memory */
151 log_write(LS_SYSTEM, L_CRIT, LOG_NOSNOTICE, "Server panic: %s", message);
152 flush_connections(0);
153 log_close();
154 close_connections(1);
155 exit(1);
156 }
157
158 /*----------------------------------------------------------------------------
159 * API: server_restart
160 *--------------------------------------------------------------------------*/
161 /** Restart the server with a message.
162 * @param[in] message Message to log and send to operators.
163 */
server_restart(const char * message)164 void server_restart(const char *message)
165 {
166 static int restarting = 0;
167
168 /* inhibit sending any server notices; we may be in a loop */
169 log_write(LS_SYSTEM, L_WARNING, LOG_NOSNOTICE, "Restarting Server: %s",
170 message);
171 if (restarting++) /* increment restarting to prevent looping */
172 return;
173
174 sendto_opmask_butone(0, SNO_OLDSNO, "Restarting server: %s", message);
175 Debug((DEBUG_NOTICE, "Restarting server..."));
176 flush_connections(0);
177
178 log_close();
179
180 close_connections(!(thisServer.bootopt & (BOOT_TTY | BOOT_DEBUG | BOOT_CHKCONF)));
181
182 reap_children();
183
184 execv(SPATH, thisServer.argv);
185
186 /* Have to reopen since it has been closed above */
187 log_reopen();
188
189 log_write(LS_SYSTEM, L_CRIT, 0, "execv(%s,%s) failed: %m", SPATH,
190 *thisServer.argv);
191
192 Debug((DEBUG_FATAL, "Couldn't restart server \"%s\": %s",
193 SPATH, (strerror(errno)) ? strerror(errno) : ""));
194 exit(8);
195 }
196
197
198 /*----------------------------------------------------------------------------
199 * outofmemory: Handler for out of memory conditions...
200 *--------------------------------------------------------------------------*/
201 /** Handle out-of-memory condition. */
outofmemory(void)202 static void outofmemory(void) {
203 Debug((DEBUG_FATAL, "Out of memory: restarting server..."));
204 server_restart("Out of Memory");
205 }
206
207
208 /*----------------------------------------------------------------------------
209 * write_pidfile
210 *--------------------------------------------------------------------------*/
211 /** Write process ID to PID file. */
write_pidfile(void)212 static void write_pidfile(void) {
213 char buff[20];
214
215 if (thisServer.pid_fd >= 0) {
216 memset(buff, 0, sizeof(buff));
217 sprintf(buff, "%5d\n", (int)getpid());
218 if (write(thisServer.pid_fd, buff, strlen(buff)) == -1)
219 Debug((DEBUG_NOTICE, "Error writing to pid file %s: %m",
220 feature_str(FEAT_PPATH)));
221 return;
222 }
223 Debug((DEBUG_NOTICE, "Error opening pid file %s: %m",
224 feature_str(FEAT_PPATH)));
225 }
226
227 /** Try to create the PID file.
228 * @return Zero on success; non-zero on any error.
229 */
check_pid(void)230 static int check_pid(void)
231 {
232 struct flock lock;
233
234 lock.l_type = F_WRLCK;
235 lock.l_start = 0;
236 lock.l_whence = SEEK_SET;
237 lock.l_len = 0;
238
239 if ((thisServer.pid_fd = open(feature_str(FEAT_PPATH), O_CREAT | O_RDWR,
240 0600)) >= 0)
241 return fcntl(thisServer.pid_fd, F_SETLK, &lock) == -1;
242
243 return 1;
244 }
245
246
247 /** Look for any connections that we should try to initiate.
248 * Reschedules itself to run again at the appropriate time.
249 * @param[in] ev Timer event (ignored).
250 */
try_connections(struct Event * ev)251 static void try_connections(struct Event* ev) {
252 struct ConfItem* aconf;
253 struct ConfItem** pconf;
254 time_t next;
255 struct Jupe* ajupe;
256 int hold;
257 int done;
258
259 assert(ET_EXPIRE == ev_type(ev));
260 assert(0 != ev_timer(ev));
261
262 Debug((DEBUG_NOTICE, "Connection check at : %s", myctime(CurrentTime)));
263 next = CurrentTime + feature_int(FEAT_CONNECTFREQUENCY);
264 done = 0;
265
266 for (aconf = GlobalConfList; aconf; aconf = aconf->next) {
267 /* Only consider server items with non-zero port and non-zero
268 * connect times that are not actively juped.
269 */
270 if (!(aconf->status & CONF_SERVER)
271 || aconf->address.port == 0
272 || !(aconf->flags & CONF_AUTOCONNECT)
273 || ((ajupe = jupe_find(aconf->name)) && JupeIsActive(ajupe)))
274 continue;
275
276 /* Do we need to postpone this connection further? */
277 hold = aconf->hold > CurrentTime;
278
279 /* Update next possible connection check time. */
280 if (hold && next > aconf->hold)
281 next = aconf->hold;
282
283 /* Do not try to connect if its use is still on hold until future,
284 * we have already initiated a connection this try_connections(),
285 * too many links in its connection class, it is already linked,
286 * or if connect rules forbid a link now.
287 */
288 if (hold || done
289 || (ConfLinks(aconf) > ConfMaxLinks(aconf))
290 || FindServer(aconf->name)
291 || conf_eval_crule(aconf->name, CRULE_MASK))
292 continue;
293
294 /* Ensure it is at the end of the list for future checks. */
295 if (aconf->next) {
296 /* Find aconf's location in the list and splice it out. */
297 for (pconf = &GlobalConfList; *pconf; pconf = &(*pconf)->next)
298 if (*pconf == aconf)
299 *pconf = aconf->next;
300 /* Reinsert it at the end of the list (where pconf is now). */
301 *pconf = aconf;
302 aconf->next = 0;
303 }
304
305 /* Activate the connection itself. */
306 if (connect_server(aconf, 0))
307 sendto_opmask_butone(0, SNO_OLDSNO, "Connection to %s activated.",
308 aconf->name);
309
310 /* And stop looking for further candidates. */
311 done = 1;
312 }
313
314 Debug((DEBUG_NOTICE, "Next connection check : %s", myctime(next)));
315 timer_add(&connect_timer, try_connections, 0, TT_ABSOLUTE, next);
316 }
317
318
319 /** Check for clients that have not sent a ping response recently.
320 * Reschedules itself to run again at the appropriate time.
321 * @param[in] ev Timer event (ignored).
322 */
check_pings(struct Event * ev)323 static void check_pings(struct Event* ev) {
324 int expire = 0;
325 int next_check = CurrentTime;
326 int max_ping = 0;
327 int i;
328
329 assert(ET_EXPIRE == ev_type(ev));
330 assert(0 != ev_timer(ev));
331
332 next_check += feature_int(FEAT_PINGFREQUENCY);
333
334 /* Scan through the client table */
335 for (i=0; i <= HighestFd; i++) {
336 struct Client *cptr = LocalClientArray[i];
337
338 if (!cptr)
339 continue;
340
341 assert(&me != cptr); /* I should never be in the local client array! */
342
343
344 /* Remove dead clients. */
345 if (IsDead(cptr)) {
346 exit_client(cptr, cptr, &me, cli_info(cptr));
347 continue;
348 }
349
350 Debug((DEBUG_DEBUG, "check_pings(%s)=status:%s current: %d",
351 cli_name(cptr),
352 IsPingSent(cptr) ? "[Ping Sent]" : "[]",
353 (int)(CurrentTime - cli_lasttime(cptr))));
354
355 /* Unregistered clients pingout after max_ping seconds, they don't
356 * get given a second chance - if they were then people could not quite
357 * finish registration and hold resources without being subject to k/g
358 * lines
359 */
360 if (!IsRegistered(cptr)) {
361 assert(!IsServer(cptr));
362 max_ping = feature_int(FEAT_CONNECTTIMEOUT);
363 /* If client authorization time has expired, ask auth whether they
364 * should be checked again later. */
365 if ((CurrentTime-cli_firsttime(cptr) >= max_ping)
366 && auth_ping_timeout(cptr))
367 continue;
368 if (!IsRegistered(cptr)) {
369 /* OK, they still have enough time left, so we'll just skip to the
370 * next client. Set the next check to be when their time is up, if
371 * that's before the currently scheduled next check -- hikari */
372 expire = cli_firsttime(cptr) + max_ping;
373 if (expire < next_check)
374 next_check = expire;
375 continue;
376 }
377 }
378
379 max_ping = client_get_ping(cptr);
380
381 /* If it's a server and we have not sent an AsLL lately, do so. */
382 if (IsServer(cptr)) {
383 if (CurrentTime - cli_serv(cptr)->asll_last >= max_ping) {
384 char *asll_ts;
385
386 SetPingSent(cptr);
387 cli_serv(cptr)->asll_last = CurrentTime;
388 expire = cli_serv(cptr)->asll_last + max_ping;
389 asll_ts = militime_float(NULL);
390 sendcmdto_prio_one(&me, CMD_PING, cptr, "!%s %s %s", asll_ts,
391 cli_name(cptr), asll_ts);
392 }
393
394 expire = cli_serv(cptr)->asll_last + max_ping;
395 if (expire < next_check)
396 next_check = expire;
397 }
398
399 /* Ok, the thing that will happen most frequently, is that someone will
400 * have sent something recently. Cover this first for speed.
401 * --
402 * If it's an unregistered client and hasn't managed to register within
403 * max_ping then it's obviously having problems (broken client) or it's
404 * just up to no good, so we won't skip it, even if its been sending
405 * data to us.
406 * -- hikari
407 */
408 if ((CurrentTime-cli_lasttime(cptr) < max_ping) && IsRegistered(cptr)) {
409 expire = cli_lasttime(cptr) + max_ping;
410 if (expire < next_check)
411 next_check = expire;
412 continue;
413 }
414
415 /* Quit the client after max_ping*2 - they should have answered by now */
416 if (CurrentTime-cli_lasttime(cptr) >= (max_ping*2) )
417 {
418 /* If it was a server, then tell ops about it. */
419 if (IsServer(cptr) || IsConnecting(cptr) || IsHandshake(cptr))
420 sendto_opmask_butone(0, SNO_OLDSNO,
421 "No response from %s, closing link",
422 cli_name(cptr));
423 exit_client_msg(cptr, cptr, &me, "Ping timeout");
424 continue;
425 }
426
427 if (!IsPingSent(cptr))
428 {
429 /* If we haven't PINGed the connection and we haven't heard from it in a
430 * while, PING it to make sure it is still alive.
431 */
432 SetPingSent(cptr);
433
434 /* If we're late in noticing don't hold it against them :) */
435 cli_lasttime(cptr) = CurrentTime - max_ping;
436
437 if (IsUser(cptr))
438 sendrawto_one(cptr, MSG_PING " :%s", cli_name(&me));
439 else
440 sendcmdto_prio_one(&me, CMD_PING, cptr, ":%s", cli_name(&me));
441 }
442
443 expire = cli_lasttime(cptr) + max_ping * 2;
444 if (expire < next_check)
445 next_check=expire;
446 }
447
448 assert(next_check >= CurrentTime);
449
450 Debug((DEBUG_DEBUG, "[%i] check_pings() again in %is",
451 CurrentTime, next_check-CurrentTime));
452
453 timer_add(&ping_timer, check_pings, 0, TT_ABSOLUTE, next_check);
454 }
455
456
457 /** Parse command line arguments.
458 * Global variables are updated to reflect the arguments.
459 * As a side effect, makes sure the process's effective user id is the
460 * same as the real user id.
461 * @param[in] argc Number of arguments on command line.
462 * @param[in,out] argv Command-lne arguments.
463 */
parse_command_line(int argc,char ** argv)464 static void parse_command_line(int argc, char** argv) {
465 const char *options = "d:f:h:nktvx:c:";
466 int opt;
467
468 if (thisServer.euid != thisServer.uid)
469 setuid(thisServer.uid);
470
471 /* Do we really need to sanity check the non-NULLness of optarg? That's
472 * getopt()'s job... Removing those... -zs
473 */
474 while ((opt = getopt(argc, argv, options)) != EOF)
475 switch (opt) {
476 case 'k': thisServer.bootopt |= BOOT_CHKCONF | BOOT_TTY; break;
477 case 'c': dbg_client = optarg; break;
478 case 'n':
479 case 't': thisServer.bootopt |= BOOT_TTY; break;
480 case 'd': dpath = optarg; break;
481 case 'f': configfile = optarg; break;
482 case 'h': ircd_strncpy(cli_name(&me), optarg, HOSTLEN); break;
483 case 'v':
484 printf("ircd %s\n", version);
485 printf("Event engines: ");
486 #ifdef USE_KQUEUE
487 printf("kqueue() ");
488 #endif
489 #ifdef USE_DEVPOLL
490 printf("/dev/poll ");
491 #endif
492 #ifdef USE_EPOLL
493 printf("epoll_*() ");
494 #endif
495 #ifdef USE_POLL
496 printf("poll()");
497 #else
498 printf("select()");
499 #endif
500 printf("\nCompiled for a maximum of %d connections.\n", MAXCONNECTIONS);
501
502
503 exit(0);
504 break;
505
506 case 'x':
507 debuglevel = atoi(optarg);
508 if (debuglevel < 0)
509 debuglevel = 0;
510 debugmode = optarg;
511 thisServer.bootopt |= BOOT_DEBUG;
512 #ifndef DEBUGMODE
513 printf("WARNING: DEBUGMODE disabled; -x has no effect.\n");
514 #endif
515 break;
516
517 default:
518 printf("Usage: ircd [-f config] [-h servername] [-x loglevel] [-ntv] [-k [-c clispec]]\n"
519 "\n -f config\t specify explicit configuration file"
520 "\n -x loglevel\t set debug logging verbosity"
521 "\n -n or -t\t don't detach"
522 "\n -v\t\t display version"
523 "\n -k\t\t exit after checking config"
524 "\n -c clispec\t search for client/kill blocks matching client"
525 "\n\t\t clispec is comma-separated list of user@host,"
526 "\n\t\t user@ip, $Rrealname, and port number"
527 "\n\nServer not started.\n");
528 exit(1);
529 }
530 }
531
532
533 /** Become a daemon.
534 * @param[in] no_fork If non-zero, do not fork into the background.
535 */
daemon_init(int no_fork)536 static void daemon_init(int no_fork) {
537 if (no_fork)
538 return;
539
540 if (fork())
541 exit(0);
542
543 #ifdef TIOCNOTTY
544 {
545 int fd;
546 if ((fd = open("/dev/tty", O_RDWR)) > -1) {
547 ioctl(fd, TIOCNOTTY, 0);
548 close(fd);
549 }
550 }
551 #endif
552
553 setsid();
554 }
555
556 /** Check that we have access to a particular file.
557 * If we do not have access to the file, complain on stderr.
558 * @param[in] path File name to check for access.
559 * @param[in] which Configuration character associated with file.
560 * @param[in] mode Bitwise combination of R_OK, W_OK, X_OK and/or F_OK.
561 * @return Non-zero if we have the necessary access, zero if not.
562 */
check_file_access(const char * path,char which,int mode)563 static char check_file_access(const char *path, char which, int mode) {
564 if (!access(path, mode))
565 return 1;
566
567 fprintf(stderr,
568 "Check on %cPATH (%s) failed: %s\n"
569 "Please create this file and/or rerun `configure' "
570 "using --with-%cpath and recompile to correct this.\n",
571 toupper(which), path, strerror(errno), which);
572
573 return 0;
574 }
575
576
577 /*----------------------------------------------------------------------------
578 * set_core_limit
579 *--------------------------------------------------------------------------*/
580 #if defined(HAVE_SETRLIMIT) && defined(RLIMIT_CORE)
581 /** Set the core size soft limit to the same as the hard limit. */
set_core_limit(void)582 static void set_core_limit(void) {
583 struct rlimit corelim;
584
585 if (getrlimit(RLIMIT_CORE, &corelim)) {
586 fprintf(stderr, "Read of rlimit core size failed: %s\n", strerror(errno));
587 corelim.rlim_max = RLIM_INFINITY; /* Try to recover */
588 }
589
590 corelim.rlim_cur = corelim.rlim_max;
591 if (setrlimit(RLIMIT_CORE, &corelim))
592 fprintf(stderr, "Setting rlimit core size failed: %s\n", strerror(errno));
593 }
594 #endif
595
596
597
598 /** Complain to stderr if any user or group ID belongs to the superuser.
599 * @return Non-zero if all IDs are okay, zero if some are 0.
600 */
set_userid_if_needed(void)601 static int set_userid_if_needed(void) {
602 if (getuid() == 0 || geteuid() == 0 ||
603 getgid() == 0 || getegid() == 0) {
604 fprintf(stderr, "ERROR: This server will not run as superuser.\n");
605 return 0;
606 }
607
608 return 1;
609 }
610
611
612 /*----------------------------------------------------------------------------
613 * main - entrypoint
614 *
615 * TODO: This should set the basic environment up and start the main loop.
616 * we're doing waaaaaaaaay too much server initialization here. I hate
617 * long and ugly control paths... -smd
618 *--------------------------------------------------------------------------*/
619 /** Run the daemon.
620 * @param[in] argc Number of arguments in \a argv.
621 * @param[in] argv Arguments to program execution.
622 */
main(int argc,char ** argv)623 int main(int argc, char **argv) {
624 CurrentTime = time(NULL);
625
626 thisServer.argc = argc;
627 thisServer.argv = argv;
628 thisServer.uid = getuid();
629 thisServer.euid = geteuid();
630
631 #ifdef MDEBUG
632 mem_dbg_initialise();
633 #endif
634
635 #if defined(HAVE_SETRLIMIT) && defined(RLIMIT_CORE)
636 set_core_limit();
637 #endif
638
639 umask(077); /* better safe than sorry --SRB */
640 memset(&me, 0, sizeof(me));
641 memset(&me_con, 0, sizeof(me_con));
642 cli_connect(&me) = &me_con;
643 cli_fd(&me) = -1;
644
645 parse_command_line(argc, argv);
646
647 if (chdir(dpath)) {
648 fprintf(stderr, "Fail: Cannot chdir(%s): %s, check DPATH\n", dpath, strerror(errno));
649 return 2;
650 }
651
652 if (!set_userid_if_needed())
653 return 3;
654
655 /* Check paths for accessibility */
656 if (!check_file_access(SPATH, 'S', X_OK) ||
657 !check_file_access(configfile, 'c', R_OK))
658 return 4;
659
660 if (!init_connection_limits())
661 return 9;
662
663 close_connections(!(thisServer.bootopt & (BOOT_DEBUG | BOOT_TTY | BOOT_CHKCONF)));
664
665 /* daemon_init() must be before event_init() because kqueue() FDs
666 * are, perversely, not inherited across fork().
667 */
668 daemon_init(thisServer.bootopt & BOOT_TTY);
669
670 #ifdef DEBUGMODE
671 /* Must reserve fd 2... */
672 if (debuglevel >= 0 && !(thisServer.bootopt & BOOT_TTY)) {
673 int fd;
674 if ((fd = open("/dev/null", O_WRONLY)) < 0) {
675 fprintf(stderr, "Unable to open /dev/null (to reserve fd 2): %s\n",
676 strerror(errno));
677 return 8;
678 }
679 if (fd != 2 && dup2(fd, 2) < 0) {
680 fprintf(stderr, "Unable to reserve fd 2; dup2 said: %s\n",
681 strerror(errno));
682 return 8;
683 }
684 }
685 #endif
686
687 event_init(MAXCONNECTIONS);
688
689 setup_signals();
690 feature_init(); /* initialize features... */
691 log_init(*argv);
692 set_nomem_handler(outofmemory);
693
694 initload();
695 init_list();
696 init_hash();
697 init_class();
698 initwhowas();
699 initmsgtree();
700 initstats();
701
702 /* we need this for now, when we're modular this
703 should be removed -- hikari */
704 ircd_crypt_init();
705
706 motd_init();
707
708 if (!init_conf()) {
709 log_write(LS_SYSTEM, L_CRIT, 0, "Failed to read configuration file %s",
710 configfile);
711 return 7;
712 }
713
714 if (thisServer.bootopt & BOOT_CHKCONF) {
715 if (dbg_client)
716 conf_debug_iline(dbg_client);
717 fprintf(stderr, "Configuration file %s checked okay.\n", configfile);
718 return 0;
719 }
720
721 debug_init(thisServer.bootopt & BOOT_TTY);
722 if (check_pid()) {
723 Debug((DEBUG_FATAL, "Failed to acquire PID file lock after fork"));
724 exit(2);
725 }
726
727 init_server_identity();
728
729 uping_init();
730
731 stats_init();
732
733 IPcheck_init();
734 timer_add(timer_init(&connect_timer), try_connections, 0, TT_RELATIVE, 1);
735 timer_add(timer_init(&ping_timer), check_pings, 0, TT_RELATIVE, 1);
736 timer_add(timer_init(&destruct_event_timer), exec_expired_destruct_events, 0, TT_PERIODIC, 60);
737
738 CurrentTime = time(NULL);
739
740 SetMe(&me);
741 cli_magic(&me) = CLIENT_MAGIC;
742 cli_from(&me) = &me;
743 make_server(&me);
744
745 cli_serv(&me)->timestamp = TStime(); /* Abuse own link timestamp as start TS */
746 cli_serv(&me)->prot = atoi(MAJOR_PROTOCOL);
747 cli_serv(&me)->up = &me;
748 cli_serv(&me)->down = NULL;
749 cli_handler(&me) = SERVER_HANDLER;
750
751 SetYXXCapacity(&me, MAXCLIENTS);
752
753 cli_lasttime(&me) = cli_since(&me) = cli_firsttime(&me) = CurrentTime;
754
755 hAddClient(&me);
756 SetIPv6(&me);
757
758 write_pidfile();
759 init_counters();
760
761 Debug((DEBUG_NOTICE, "Server ready..."));
762 log_write(LS_SYSTEM, L_NOTICE, 0, "Server Ready");
763
764 event_loop();
765
766 return 0;
767 }
768
769
770