Author: Kev <klmitch@mit.edu>
[ircu2.10.12-pk.git] / ircd / ircd.c
index 8e90c19c274e00f978237648c5fc5a7e650313ee..97ff9f1f8f11c41b93e320e4cab8229b40bde258 100644 (file)
@@ -19,6 +19,8 @@
  *
  * $Id$
  */
+#include "config.h"
+
 #include "ircd.h"
 #include "IPcheck.h"
 #include "class.h"
@@ -26,6 +28,7 @@
 #include "crule.h"
 #include "hash.h"
 #include "ircd_alloc.h"
+#include "ircd_features.h"
 #include "ircd_log.h"
 #include "ircd_reply.h"
 #include "ircd_signal.h"
@@ -61,6 +64,7 @@
 #include <string.h>
 #include <sys/socket.h>
 #include <sys/stat.h>
+#include <sys/types.h>
 #include <unistd.h>
 
 
@@ -82,22 +86,24 @@ enum {
 /*----------------------------------------------------------------------------
  * Global data (YUCK!)
  *--------------------------------------------------------------------------*/
-struct Client  me;                      // That's me
-struct Client *GlobalClientList  = &me; // Pointer to beginning of Client list
-time_t         TSoffset          = 0;   // Offset of timestamps to system clock
-int            GlobalRehashFlag  = 0;   // do a rehash if set
-int            GlobalRestartFlag = 0;   // do a restart if set
-time_t         CurrentTime;             // Updated every time we leave select()
-
-char          *configfile        = CPATH; // Server configuration file
-int            debuglevel        = -1;    // Server debug level 
-char          *debugmode         = "";    // Server debug level
+struct Client  me;                      /* That's me */
+struct Connection me_con;              /* That's me too */
+struct Client *GlobalClientList  = &me; /* Pointer to beginning of
+                                          Client list */
+time_t         TSoffset          = 0;/* Offset of timestamps to system clock */
+int            GlobalRehashFlag  = 0;   /* do a rehash if set */
+int            GlobalRestartFlag = 0;   /* do a restart if set */
+time_t         CurrentTime;          /* Updated every time we leave select() */
+
+char          *configfile        = CPATH; /* Server configuration file */
+int            debuglevel        = -1;    /* Server debug level  */
+char          *debugmode         = "";    /* Server debug level */
 static char   *dpath             = DPATH;
 
-time_t         nextconnect       = 1; // time for next try_connections call
-time_t         nextping          = 1; // same as above for check_pings()
+time_t         nextconnect       = 1; /* time for next try_connections call */
+time_t         nextping          = 1; /* same as above for check_pings() */
 
-static struct Daemon thisServer  = { 0 };     // server process info 
+static struct Daemon thisServer  = { 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0 };
 
 
 
@@ -160,20 +166,45 @@ static void outofmemory(void) {
  * write_pidfile
  *--------------------------------------------------------------------------*/
 static void write_pidfile(void) {
-  FILE *pidf;
-
-  if (!(pidf = fopen(PPATH, "w+"))) {
-    Debug((DEBUG_NOTICE, 
-          "Error opening pid file \"%s\": %s", PPATH, strerror(errno)));
+  char buff[20];
+
+  if (thisServer.pid_fd >= 0) {
+    memset(buff, 0, sizeof(buff));
+    sprintf(buff, "%5d\n", (int)getpid());
+    if (write(thisServer.pid_fd, buff, strlen(buff)) == -1)
+      Debug((DEBUG_NOTICE, "Error writing to pid file %s: %m",
+            feature_str(FEAT_PPATH)));
     return;
   }
-    
-  if (fprintf(pidf, "%5d\n", getpid()) < 5)
-    Debug((DEBUG_NOTICE, "Error writing to pid file %s", PPATH));
-
-  fclose(pidf);
+  Debug((DEBUG_NOTICE, "Error opening pid file %s: %m",
+        feature_str(FEAT_PPATH)));
 }
 
+/* check_pid
+ * 
+ * inputs: 
+ *   none
+ * returns:
+ *   true - if the pid file exists (and is readable), and the pid refered
+ *          to in the file is still running.
+ *   false - otherwise.
+ */
+static int check_pid(void)
+{
+  struct flock lock;
+
+  lock.l_type = F_WRLCK;
+  lock.l_start = 0;
+  lock.l_whence = SEEK_SET;
+  lock.l_len = 0;
+
+  if ((thisServer.pid_fd = open(feature_str(FEAT_PPATH), O_CREAT | O_RDWR,
+                               0600)) >= 0)
+    return fcntl(thisServer.pid_fd, F_SETLK, &lock);
+
+  return 0;
+}
+  
 
 /*----------------------------------------------------------------------------
  * try_connections
@@ -269,9 +300,11 @@ static time_t try_connections(void) {
  *--------------------------------------------------------------------------*/
 static time_t check_pings(void) {
   int expire     = 0; 
-  int next_check = CurrentTime + PINGFREQUENCY;
+  int next_check = CurrentTime;
   int max_ping   = 0;
   int i;
+
+  next_check += feature_int(FEAT_PINGFREQUENCY);
   
   /* Scan through the client table */
   for (i=0; i <= HighestFd; i++) {
@@ -285,33 +318,34 @@ static time_t check_pings(void) {
 
     /* Remove dead clients. */
     if (IsDead(cptr)) {
-      exit_client(cptr, cptr, &me, cptr->info);
+      exit_client(cptr, cptr, &me, cli_info(cptr));
       continue;
     }
 
-    max_ping = IsRegistered(cptr) ? client_get_ping(cptr) : CONNECTTIMEOUT;
+    max_ping = IsRegistered(cptr) ? client_get_ping(cptr) :
+      feature_int(FEAT_CONNECTTIMEOUT);
    
     Debug((DEBUG_DEBUG, "check_pings(%s)=status:%s limit: %d current: %d",
-          cptr->name, (cptr->flags & FLAGS_PINGSENT) ? "[Ping Sent]" : "[]", 
-          max_ping, (int)(CurrentTime - cptr->lasttime)));
+          cli_name(cptr), (cli_flags(cptr) & FLAGS_PINGSENT) ? "[Ping Sent]" : "[]", 
+          max_ping, (int)(CurrentTime - cli_lasttime(cptr))));
           
 
     /* Ok, the thing that will happen most frequently, is that someone will
      * have sent something recently.  Cover this first for speed.
      */
-    if (CurrentTime-cptr->lasttime < max_ping) {
-      expire = cptr->lasttime + max_ping;
+    if (CurrentTime-cli_lasttime(cptr) < max_ping) {
+      expire = cli_lasttime(cptr) + max_ping;
       if (expire < next_check) 
        next_check = expire;
       continue;
     }
 
     /* Quit the client after max_ping*2 - they should have answered by now */
-    if (CurrentTime-cptr->lasttime >= (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", cptr->name);
+                            "No response from %s, closing link", cli_name(cptr));
       exit_client_msg(cptr, cptr, &me, "Ping timeout");
       continue;
     }
@@ -325,7 +359,7 @@ static time_t check_pings(void) {
       /* Display message if they have sent a NICK and a USER but no
        * nospoof PONG.
        */
-      if (*cptr->name && cptr->user && *cptr->user->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,
@@ -336,22 +370,22 @@ static time_t check_pings(void) {
       continue;
     }
     
-    if (!(cptr->flags & FLAGS_PINGSENT)) {
+    if (!(cli_flags(cptr) & FLAGS_PINGSENT)) {
       /* If we havent PINGed the connection and we havent heard from it in a
        * while, PING it to make sure it is still alive.
        */
-      cptr->flags |= FLAGS_PINGSENT;
+      cli_flags(cptr) |= FLAGS_PINGSENT;
 
       /* If we're late in noticing don't hold it against them :) */
-      cptr->lasttime = CurrentTime - max_ping;
+      cli_lasttime(cptr) = CurrentTime - max_ping;
       
       if (IsUser(cptr))
-       sendrawto_one(cptr, MSG_PING " :%s", me.name);
+       sendrawto_one(cptr, MSG_PING " :%s", cli_name(&me));
       else
-       sendcmdto_one(&me, CMD_PING, cptr, ":%s", me.name);
+       sendcmdto_one(&me, CMD_PING, cptr, ":%s", cli_name(&me));
     }
     
-    expire = cptr->lasttime + max_ping * 2;
+    expire = cli_lasttime(cptr) + max_ping * 2;
     if (expire < next_check)
       next_check=expire;
   }
@@ -386,7 +420,7 @@ static void parse_command_line(int argc, char** argv) {
     case 't':  thisServer.bootopt |= BOOT_TTY;         break;
     case 'd':  dpath      = optarg;                    break;
     case 'f':  configfile = optarg;                    break;
-    case 'h':  ircd_strncpy(me.name, optarg, HOSTLEN); break;
+    case 'h':  ircd_strncpy(cli_name(&me), optarg, HOSTLEN); break;
     case 'v':  printf("ircd %s\n", version);           exit(0);
       
     case 'x':
@@ -474,7 +508,7 @@ static void event_loop(void) {
     if (delay < 1)
       read_message(1);
     else
-      read_message(IRCD_MIN(delay, TIMESEC));
+      read_message(IRCD_MIN(delay, feature_int(FEAT_TIMESEC)));
 
     /* ...perhaps should not do these loops every time, but only if there is
      * some chance of something happening (but, note that conf->hold times may
@@ -612,7 +646,9 @@ int main(int argc, char **argv) {
 
   umask(077);                   /* better safe than sorry --SRB */
   memset(&me, 0, sizeof(me));
-  me.fd = -1;
+  memset(&me_con, 0, sizeof(me_con));
+  cli_connect(&me) = &me_con;
+  cli_fd(&me) = -1;
 
   parse_command_line(argc, argv);
 
@@ -626,9 +662,7 @@ int main(int argc, char **argv) {
 
   /* Check paths for accessibility */
   if (!check_file_access(SPATH, 'S', X_OK) ||
-      !check_file_access(configfile, 'C', R_OK) ||
-      !check_file_access(MPATH, 'M', R_OK) ||
-      !check_file_access(RPATH, 'R', R_OK))
+      !check_file_access(configfile, 'C', R_OK))
     return 4;
       
 #ifdef DEBUG
@@ -640,8 +674,12 @@ int main(int argc, char **argv) {
   daemon_init(thisServer.bootopt & BOOT_TTY);
 
   setup_signals();
+  feature_init(); /* initialize features... */
   log_init(*argv);
-
+  if (check_pid()) {
+    Debug((DEBUG_FATAL, "Failed to acquire PID file lock after fork"));
+    exit(2);
+  }
   set_nomem_handler(outofmemory);
   
   if (!init_string()) {
@@ -674,18 +712,18 @@ int main(int argc, char **argv) {
   CurrentTime = time(NULL);
 
   SetMe(&me);
-  me.from = &me;
+  cli_from(&me) = &me;
   make_server(&me);
 
-  me.serv->timestamp = TStime();  /* Abuse own link timestamp as start TS */
-  me.serv->prot      = atoi(MAJOR_PROTOCOL);
-  me.serv->up        = &me;
-  me.serv->down      = NULL;
-  me.handler         = SERVER_HANDLER;
+  cli_serv(&me)->timestamp = TStime();  /* Abuse own link timestamp as start TS */
+  cli_serv(&me)->prot      = atoi(MAJOR_PROTOCOL);
+  cli_serv(&me)->up        = &me;
+  cli_serv(&me)->down      = NULL;
+  cli_handler(&me)         = SERVER_HANDLER;
 
   SetYXXCapacity(&me, MAXCLIENTS);
 
-  me.lasttime = me.since = me.firsttime = CurrentTime;
+  cli_lasttime(&me) = cli_since(&me) = cli_firsttime(&me) = CurrentTime;
 
   hAddClient(&me);