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