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