Author: Kev <klmitch@mit.edu>
[ircu2.10.12-pk.git] / ircd / ircd.c
index d6085344441600988cc0412cbb60c8adbbecd8c5..5888513a7b965406965457d9533caf4b67322276 100644 (file)
@@ -26,6 +26,7 @@
 #include "class.h"
 #include "client.h"
 #include "crule.h"
+#include "destruct_event.h"
 #include "hash.h"
 #include "ircd_alloc.h"
 #include "ircd_events.h"
@@ -41,6 +42,7 @@
 #include "msg.h"
 #include "numeric.h"
 #include "numnicks.h"
+#include "opercmds.h"
 #include "parse.h"
 #include "res.h"
 #include "s_auth.h"
@@ -48,6 +50,7 @@
 #include "s_conf.h"
 #include "s_debug.h"
 #include "s_misc.h"
+#include "s_stats.h"
 #include "send.h"
 #include "sys.h"
 #include "uping.h"
@@ -74,6 +77,7 @@
  * External stuff
  *--------------------------------------------------------------------------*/
 extern void init_counters(void);
+extern void mem_dbg_initialise(void);
 
 /*----------------------------------------------------------------------------
  * Constants / Enums
@@ -103,6 +107,7 @@ static char   *dpath             = DPATH;
 
 static struct Timer connect_timer; /* timer structure for try_connections() */
 static struct Timer ping_timer; /* timer structure for check_pings() */
+static struct Timer destruct_event_timer; /* timer structure for exec_expired_destruct_events() */
 
 static struct Daemon thisServer  = { 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0 };
 
@@ -112,7 +117,8 @@ int running = 1;
 /*----------------------------------------------------------------------------
  * API: server_die
  *--------------------------------------------------------------------------*/
-void server_die(const char* message) {
+void server_die(const char *message)
+{
   /* log_write will send out message to both log file and as server notice */
   log_write(LS_SYSTEM, L_CRIT, 0, "Server terminating: %s", message);
   flush_connections(0);
@@ -120,11 +126,24 @@ void server_die(const char* message) {
   running = 0;
 }
 
+/*----------------------------------------------------------------------------
+ * API: server_panic
+ *--------------------------------------------------------------------------*/
+void server_panic(const char *message)
+{
+  /* inhibit sending server notice--we may be panicing due to low memory */
+  log_write(LS_SYSTEM, L_CRIT, LOG_NOSNOTICE, "Server panic: %s", message);
+  flush_connections(0);
+  log_close();
+  close_connections(1);
+  exit(1);
+}
 
 /*----------------------------------------------------------------------------
  * API: server_restart
  *--------------------------------------------------------------------------*/
-void server_restart(const char* message) {
+void server_restart(const char *message)
+{
   static int restarting = 0;
 
   /* inhibit sending any server notices; we may be in a loop */
@@ -227,7 +246,7 @@ static void try_connections(struct Event* ev) {
   struct ConnectionClass* cltmp;
   struct ConfItem*  con_conf    = 0;
   struct Jupe*      ajupe;
-  unsigned int      con_class   = 0;
+  const char*       con_class   = NULL;
 
   assert(ET_EXPIRE == ev_type(ev));
   assert(0 != ev_timer(ev));
@@ -236,7 +255,7 @@ static void try_connections(struct Event* ev) {
   Debug((DEBUG_NOTICE, "Connection check at   : %s", myctime(CurrentTime)));
   for (aconf = GlobalConfList; aconf; aconf = aconf->next) {
     /* Also when already connecting! (update holdtimes) --SRB */
-    if (!(aconf->status & CONF_SERVER) || aconf->port == 0)
+    if (!(aconf->status & CONF_SERVER) || aconf->port == 0 || aconf->hold == 0)
       continue;
 
     /* Also skip juped servers */
@@ -255,7 +274,10 @@ static void try_connections(struct Event* ev) {
 
     cltmp = aconf->conn_class;
     confrq = get_con_freq(cltmp);
-    aconf->hold = CurrentTime + confrq;
+    if(confrq == 0)
+      aconf->hold = next = 0;
+    else
+      aconf->hold = CurrentTime + confrq;
 
     /* Found a CONNECT config with port specified, scan clients and see if
      * this server is already connected?
@@ -263,7 +285,7 @@ static void try_connections(struct Event* ev) {
     cptr = FindServer(aconf->name);
 
     if (!cptr && (Links(cltmp) < MaxLinks(cltmp)) &&
-        (!connecting || (ConClass(cltmp) > con_class))) {
+        (!connecting /*|| (ConClass(cltmp) > con_class)*/)) {
       /*
        * Check connect rules to see if we're allowed to try
        */
@@ -339,7 +361,7 @@ static void check_pings(struct Event* ev) {
    
     Debug((DEBUG_DEBUG, "check_pings(%s)=status:%s limit: %d current: %d",
           cli_name(cptr),
-          (cli_flags(cptr) & FLAGS_PINGSENT) ? "[Ping Sent]" : "[]", 
+          IsPingSent(cptr) ? "[Ping Sent]" : "[]", 
           max_ping, (int)(CurrentTime - cli_lasttime(cptr))));
           
 
@@ -354,49 +376,57 @@ static void check_pings(struct Event* ev) {
     }
 
     /* Quit the client after max_ping*2 - they should have answered by now */
-    if (CurrentTime-cli_lasttime(cptr) >= (max_ping*2) ) {
+    if (CurrentTime-cli_lasttime(cptr) >= (max_ping*2) )
+    {
       /* If it was a server, then tell ops about it. */
       if (IsServer(cptr) || IsConnecting(cptr) || IsHandshake(cptr))
-       sendto_opmask_butone(0, SNO_OLDSNO,
-                            "No response from %s, closing link",
-                            cli_name(cptr));
+        sendto_opmask_butone(0, SNO_OLDSNO,
+                             "No response from %s, closing link",
+                             cli_name(cptr));
       exit_client_msg(cptr, cptr, &me, "Ping timeout");
       continue;
     }
-    
+
     /* Unregistered clients pingout after max_ping seconds, they don't
      * get given a second chance - if they were then people could not quite
      * finish registration and hold resources without being subject to k/g
      * lines
      */
-    if (!IsRegistered(cptr)) {
+    if (!IsRegistered(cptr))
+    {
       /* Display message if they have sent a NICK and a USER but no
        * nospoof PONG.
        */
-      if (*(cli_name(cptr)) && cli_user(cptr) && *(cli_user(cptr))->username) {
+      if (*(cli_name(cptr)) && cli_user(cptr) && *(cli_user(cptr))->username)
+      {
        send_reply(cptr, SND_EXPLICIT | ERR_BADPING,
                   ":Your client may not be compatible with this server.");
        send_reply(cptr, SND_EXPLICIT | ERR_BADPING,
-                  ":Compatible clients are available at "
-                  "ftp://ftp.undernet.org/pub/irc/clients");
+                   ":Compatible clients are available at %s",
+                   feature_str(FEAT_URL_CLIENTS));
       }    
       exit_client_msg(cptr,cptr,&me, "Ping Timeout");
       continue;
     }
     
-    if (!(cli_flags(cptr) & FLAGS_PINGSENT)) {
+    if (!IsPingSent(cptr))
+    {
       /* If we havent PINGed the connection and we havent heard from it in a
        * while, PING it to make sure it is still alive.
        */
-      cli_flags(cptr) |= FLAGS_PINGSENT;
+      SetPingSent(cptr);
 
       /* If we're late in noticing don't hold it against them :) */
       cli_lasttime(cptr) = CurrentTime - max_ping;
       
       if (IsUser(cptr))
-       sendrawto_one(cptr, MSG_PING " :%s", cli_name(&me));
+        sendrawto_one(cptr, MSG_PING " :%s", cli_name(&me));
       else
-       sendcmdto_one(&me, CMD_PING, cptr, ":%s", cli_name(&me));
+      {
+        char *asll_ts = militime_float(NULL);
+        sendcmdto_one(&me, CMD_PING, cptr, "!%s %s %s", asll_ts,
+                      cli_name(cptr), asll_ts);
+      }
     }
     
     expire = cli_lasttime(cptr) + max_ping * 2;
@@ -435,7 +465,25 @@ static void parse_command_line(int argc, char** argv) {
     case 'd':  dpath      = optarg;                    break;
     case 'f':  configfile = optarg;                    break;
     case 'h':  ircd_strncpy(cli_name(&me), optarg, HOSTLEN); break;
-    case 'v':  printf("ircd %s\n", version);           exit(0);
+    case 'v':
+      printf("ircd %s\n", version);
+      printf("Event engines: ");
+#ifdef USE_KQUEUE
+      printf("kqueue() ");
+#endif
+#ifdef USE_DEVPOLL
+      printf("/dev/poll ");
+#endif
+#ifdef USE_POLL
+      printf("poll()");
+#else
+      printf("select()");
+#endif
+      printf("\nCompiled for a maximum of %d connections.\n", MAXCONNECTIONS);
+
+
+      exit(0);
+      break;
       
     case 'x':
       debuglevel = atoi(optarg);
@@ -492,9 +540,9 @@ static char check_file_access(const char *path, char which, int mode) {
 
   fprintf(stderr, 
          "Check on %cPATH (%s) failed: %s\n"
-         "Please create file and/or rerun `make config' and "
-         "recompile to correct this.\n",
-         which, path, strerror(errno));
+         "Please create this file and/or rerun `configure' "
+         "using --with-%cpath and recompile to correct this.\n",
+         which, path, strerror(errno), which);
 
   return 0;
 }
@@ -549,6 +597,10 @@ int main(int argc, char **argv) {
   thisServer.uid  = getuid();
   thisServer.euid = geteuid();
 
+#ifdef MDEBUG
+  mem_dbg_initialise();
+#endif
+
 #if defined(HAVE_SETRLIMIT) && defined(RLIMIT_CORE)
   set_core_limit();
 #endif
@@ -573,11 +625,6 @@ int main(int argc, char **argv) {
   if (!check_file_access(SPATH, 'S', X_OK) ||
       !check_file_access(configfile, 'C', R_OK))
     return 4;
-      
-#ifdef DEBUGMODE
-  if (!check_file_access(LPATH, 'L', W_OK))
-    return 5;
-#endif
 
   debug_init(thisServer.bootopt & BOOT_TTY);
   daemon_init(thisServer.bootopt & BOOT_TTY);
@@ -619,9 +666,12 @@ int main(int argc, char **argv) {
 
   uping_init();
 
+  stats_init();
+
   IPcheck_init();
-  timer_add(&connect_timer, try_connections, 0, TT_RELATIVE, 1);
-  timer_add(&ping_timer, check_pings, 0, TT_RELATIVE, 1);
+  timer_add(timer_init(&connect_timer), try_connections, 0, TT_RELATIVE, 1);
+  timer_add(timer_init(&ping_timer), check_pings, 0, TT_RELATIVE, 1);
+  timer_add(timer_init(&destruct_event_timer), exec_expired_destruct_events, 0, TT_PERIODIC, 60);
 
   CurrentTime = time(NULL);