]> jfr.im git - irc/ircd-hybrid/bopm.git/commitdiff
Added main_restart function to restart bopm process.
authorstrtok <redacted>
Sun, 22 Dec 2002 23:24:57 +0000 (23:24 +0000)
committerstrtok <redacted>
Sun, 22 Dec 2002 23:24:57 +0000 (23:24 +0000)
Call main_restart on m_kill

src/irc.c
src/log.c
src/main.c
src/main.h [new file with mode: 0644]

index 64160304902dcdd5076d71d4703b4847ec9e1f29..62fb62c192f8af0a69a76b9a1002abf5dfe2459b 100644 (file)
--- a/src/irc.c
+++ b/src/irc.c
@@ -71,6 +71,7 @@
 #include "compat.h"
 #include "negcache.h"
 #include "malloc.h"
+#include "main.h"
 
 static void irc_init(void);
 static void irc_connect(void);
@@ -91,6 +92,7 @@ static void m_notice(char **, unsigned int, char *, struct UserInfo *);
 static void m_perform(char **, unsigned int, char *, struct UserInfo *);
 static void m_userhost(char **, unsigned int, char *, struct UserInfo *);
 static void m_cannot_join(char **, unsigned int, char *, struct UserInfo *);
+static void m_kill(char **, unsigned int, char *, struct UserInfo *);
 
 extern struct cnode *nc_head;
 
@@ -104,7 +106,7 @@ char                 IRC_SENDBUFF[MSGLENMAX];    /* Send buffer
 char                 IRC_CHANNELS[MSGLENMAX];    /* Stores comma delim list of channels   */
 int                  IRC_RAW_LEN    = 0;         /* Position of IRC_RAW                   */
 
-int                  IRC_FD         = -1;        /* File descriptor for IRC client        */
+int                  IRC_FD         = 0;        /* File descriptor for IRC client        */
 
 struct bopm_sockaddr IRC_SVR;                   /* Sock Address Struct for IRC server    */
 struct bopm_ircaddr  IRC_LOCAL;                 /* Sock Address Struct for Bind          */
@@ -115,6 +117,7 @@ struct timeval       IRC_TIMEOUT;                /* timeval struct for select()
 
 time_t               IRC_LAST = 0;               /* Last full line of data from irc server*/
 time_t               IRC_LASTRECONNECT = 0;      /* Time of last reconnection */
+
 /* Table should be ordered with most occuring (or priority)
    commands at the top of the list. */
 
@@ -129,6 +132,7 @@ static struct CommandHash COMMAND_TABLE[] = {
          {"473",                  m_cannot_join    },
          {"474",                  m_cannot_join    },
          {"475",                  m_cannot_join    },
+         {"KILL",                 m_kill           }
       };
 
 /* irc_cycle
@@ -244,9 +248,8 @@ static void irc_init(void)
       exit(EXIT_FAILURE);
    }
 
-   IRC_FD = socket(AF_INET, SOCK_STREAM, 0);
-
    /* Request file desc for IRC client socket */
+   IRC_FD = socket(AF_INET, SOCK_STREAM, 0);
 
    if (IRC_FD == -1)
    {
@@ -1102,3 +1105,19 @@ static void m_cannot_join(char **parv, unsigned int parc, char *msg, struct User
    irc_send("%s", channel->invite);
 }
 
+
+/* m_kill
+ *
+ * parv[0]  = source
+ * parv[1]  = numeric
+ * parv[2]  = target (bopm)
+ * parv[3]  = channel
+ * parv[4]  = error text
+ *
+ */
+
+static void m_kill(char **parv, unsigned int parc, char *msg, struct UserInfo *source_p)
+{
+   /* Restart bopm to rehash */
+   main_restart();
+}
index 20bc20f28e77e769bdb463544a043374978a3743..932fa2505a5ad2681e7bd776110e16c526df0c37 100644 (file)
--- a/src/log.c
+++ b/src/log.c
@@ -52,6 +52,7 @@ void log_open(char *filename)
       perror("Cannot open log file. Aborting.");
       exit(EXIT_FAILURE);
    }
+
 }
 
 void log_close(void)
index d93f74ee4866673f2212afbc089c5eb117c4136d..356493e87b4690a47a4d52465c39ffea42c1219b 100644 (file)
@@ -33,6 +33,9 @@ along with this program; if not, write to the Free Software
 #include <netdb.h>
 #include <signal.h>
 #include <unistd.h>
+#include <errno.h>
+#include <sys/resource.h> /* getrlimit */
+#include <fcntl.h>
 
 #ifdef STDC_HEADERS
 #include <stdlib.h>
@@ -53,8 +56,9 @@ along with this program; if not, write to the Free Software
 
 static RETSIGTYPE do_signal(int signum);
 
-int ALARMED = 0;
-unsigned int OPT_DEBUG = 0;
+int RESTART = 0;             /* Flagged to restart on next cycle */
+int ALARMED = 0;             /* Flagged to call timer functions on next cycle */
+unsigned int OPT_DEBUG = 0;  /* Debug level */
 
 char *CONFNAME = DEFAULTNAME;
 char *CONFDIR = BOPM_ETCDIR;
@@ -69,8 +73,9 @@ int main(int argc, char **argv)
    char spid[16];
    pid_t pid;
    int c, lenc, lenl, lenp;
-   unsigned int nc_counter;
+   unsigned int nc_counter, i;
    FILE *pidout;
+   struct rlimit rlim;
 
    nc_counter = 0;
 
@@ -177,10 +182,44 @@ int main(int argc, char **argv)
 
    while (1)
    {
+      
+
+      /* Main cycles */
       irc_cycle();
       scan_cycle();
 
-      if (ALARMED)
+
+      /* Restart bopm if main_restart() was called (usually happens by m_kill in irc.c) */
+      if(RESTART)
+      {
+         log("MAIN -> Restarting process");
+
+         /* Get upper file descriptor limit */
+         if(getrlimit(RLIMIT_NOFILE, &rlim) == -1) 
+         {
+            log("MAIN RESTART -> getrlimit() error retrieving RLIMIT_NOFILE (%s)", strerror(errno));
+            return; 
+         }
+
+         if(OPT_DEBUG)
+            log("MAIN RESTART -> Setting file descriptors %d-%d F_SETFL", STDERR_FILENO + 1,
+                  rlim.rlim_cur);
+
+         /* Set file descriptors strderr-rlim_cur close on exec */
+         for(i = STDERR_FILENO + 1; i < rlim.rlim_cur; i++)
+            fcntl(i, F_SETFD, FD_CLOEXEC);
+
+         /* execute new process */
+         if(execve(argv[0], argv, NULL) == -1)
+            log("MAIN RESTART -> Execution of \"%s\" failed. ERROR: %s", strerror(errno));
+
+         /* Should only get here if execve failed */
+         RESTART = 0;
+      }
+
+
+      /* Call 1 second timers */
+      if(ALARMED)
       {
          irc_timer();
          scan_timer();
@@ -188,6 +227,8 @@ int main(int argc, char **argv)
 
          ALARMED = 0;
       }
+
+
    }
 
    if (!OPT_DEBUG)
@@ -213,3 +254,9 @@ static void do_signal(int signum)
          break;
    }
 }
+
+
+void main_restart()
+{
+   RESTART = 1;
+}
diff --git a/src/main.h b/src/main.h
new file mode 100644 (file)
index 0000000..237b63a
--- /dev/null
@@ -0,0 +1,6 @@
+#ifndef MAIN_H
+#define MAIN_H
+
+extern void main_restart();
+
+#endif /* MAIN_H */