Explorar o código

* Port [3207] to 1.2.14
* Fix processing/killing/signalling of possible new localhub when editing binary.


svn: 3208

Bryan Drewery %!s(int64=19) %!d(string=hai) anos
pai
achega
7e13dfdecd
Modificáronse 7 ficheiros con 94 adicións e 46 borrados
  1. 1 0
      doc/UPDATES
  2. 70 31
      src/binary.c
  3. 15 10
      src/conf.c
  4. 3 1
      src/conf.h
  5. 1 1
      src/main.c
  6. 3 2
      src/misc.c
  7. 1 1
      src/mod/server.mod/servmsg.c

+ 1 - 0
doc/UPDATES

@@ -10,6 +10,7 @@ Lines prefixed with '-' were disabled before release and are not finished, or ar
 * Fix bots being removed from binary not being killed. (fixes #338)
 * Fix typo in help for 'set' (fixes #342)
 * Fix cmd_swhois stripping multiple channel modes. (fixes #348)
+* Fix processing/killing/signalling of possible new localhub when editing binary.
 
 1.2.13 - http://wraith.shatow.net/milestone/1.2.13
 * Fix cmd_chanset accepting invalid flags

+ 70 - 31
src/binary.c

@@ -14,6 +14,8 @@
 #include "misc.h"
 #include "main.h"
 #include "misc_file.h"
+#include "tandem.h"
+#include "botnet.h"
 
 #include <sys/wait.h>
 #include <sys/types.h>
@@ -562,52 +564,89 @@ void reload_bin_data() {
   if (bin_checksum(binname, GET_CONF)) {
     putlog(LOG_MISC, "*", "Rehashed config data from binary.");
 
-    conf_bot *oldbots = NULL;
+    conf_bot *oldbots = NULL, *oldbot = NULL;
     bool was_localhub = conf.bot->localhub ? 1 : 0;
     
     /* save the old bots list */
     oldbots = conf_bots_dup(conf.bots);
+
+    /* Save the old conf.bot */
+    oldbot = (conf_bot *) my_calloc(1, sizeof(conf_bot));
+    conf_bot_dup(oldbot, conf.bot);    
+
     /* free up our current conf struct */
     free_conf();
     /* Fill conf[] with binary data from settings[] */
     bin_to_conf();
+
     /* fill up conf.bot using origbotname */
-    fill_conf_bot();
-
-    /* If we don't have conf.bot, then all bots were removed or just our own record */
-    if (oldbots && 
-        (
-         (!conf.bot && was_localhub) || 
-         (conf.bot && !conf.bot->localhub && was_localhub)
-        )) {
-      /* no longer the localhub (or removed), need to alert the new one to rehash */
-
-      conf_bot *localhub = conf_getlocalhub(conf.bots);
-      /* then SIGHUP new localhub or spawn new localhub */
-      if (localhub) {
-        if (localhub->pid)
-          conf_killbot(NULL, localhub, SIGHUP);		//restart the new localhub
-        else
-          spawnbot(localhub->nick);				//spawn the new localhub
+    fill_conf_bot(0); /* 0 to avoid exiting if conf.bot cannot be filled */
+
+    if (conf.bot)
+      free_bot(oldbot);
+
+    /* add any new bots not in userfile */
+    conf_add_userlist_bots();
+
+    /* kill and remove bots removed from conf */
+    if (oldbots) {
+      kill_removed_bots(oldbots, conf.bots);
+
+      /* If we don't have conf.bot, then all bots were removed or just our own record */
+      if ((!conf.bot && was_localhub) || 
+           (conf.bot && !conf.bot->localhub && was_localhub)
+          ) {
+
+        /* no longer the localhub (or removed), need to alert the new one to rehash (or start it) */
+
+        conf_bot *localhub = conf_getlocalhub(conf.bots);
+        /* then SIGHUP new localhub or spawn new localhub */
+        if (localhub) {
+          /* Check for pid again - may be using fork-interval */
+          localhub->pid = checkpid(localhub->nick, localhub, NULL);
+          if (localhub->pid)
+            conf_killbot(NULL, localhub, SIGHUP);		//restart the new localhub
+          /* else
+               start new localhub - done below in spawnbots() */
+        }
       }
     }
-    if (conf.bot && conf.bot->localhub) {
-      /* kill and remove bots removed from conf */
-      if (oldbots)
-        kill_removed_bots(oldbots, conf.bots);
-      /* add any bots not in userfile */
-      conf_add_userlist_bots();
-      /* start/disable new bots as necesary */
-      conf_checkpids();
-      spawnbots(1);		//1 signifies to not start me!
-    } else
+
+    /* start/disable new bots as necesary */
+    conf_checkpids();
+    spawnbots(1);		//1 signifies to not start me!
+
+    if (!conf.bot || !conf.bot->localhub)
       free_conf_bots(conf.bots);
 
-    if (oldbots)
-      free_conf_bots(oldbots);
+    if (conf.bot && conf.bot->disabled) {
+      if (tands > 0) {
+        botnet_send_chat(-1, conf.bot->nick, "Bot disabled in binary.");
+        botnet_send_bye("Bot disabled in binary.");
+      }
+
+      if (server_online)
+        nuke_server("Bot disabled in binary.");
 
-    if (conf.bot->disabled)
       werr(ERR_BOTDISABLED);
+    }
+
+    if (!conf.bot) {
+      conf.bot = oldbot;
+
+      if (tands > 0) {
+        botnet_send_chat(-1, conf.bot->nick, "Bot removed from binary.");
+        botnet_send_bye("Bot removed from binary.");
+      }
+
+      if (server_online)
+        nuke_server("Bot removed from binary.");
+
+      werr(ERR_BADBOT);
+    }
+  
+    if (oldbots)
+      free_conf_bots(oldbots);
   }
 }
 

+ 15 - 10
src/conf.c

@@ -36,8 +36,6 @@ char cfile[DIRMAX] = "";
 #endif /* CYGWIN_HACKS */
 conf_t conf;                    /* global conf struct */
 
-static void conf_bot_dup(conf_bot *dest, conf_bot *src);
-
 static void
 tellconf()
 {
@@ -119,7 +117,8 @@ spawnbots(bool rehashed)
       -if updating and we find our nick, skip
       -if pid exists and not updating, bot is running and we have nothing more to do, skip.
      */
-    } else if ((!strcmp(bot->nick, conf.bot->nick) && (updating == UPDATE_AUTO || rehashed)) || (bot->pid && !updating)) {
+    			/* use origbotname here because conf.bot may not exist (called from rehashing) */
+    } else if ((!strcmp(bot->nick, origbotname) && (updating == UPDATE_AUTO || rehashed)) || (bot->pid && !updating)) {
       sdprintf(" ... skipping. Updating: %d, pid: %d", updating, bot->pid);
       continue;
     } else {
@@ -999,7 +998,7 @@ writeconf(char *filename, FILE * stream, int bits)
   return autowrote;
 }
 
-static void
+void
 conf_bot_dup(conf_bot *dest, conf_bot *src)
 {
   if (dest && src) {
@@ -1049,7 +1048,7 @@ void kill_removed_bots(conf_bot *oldlist, conf_bot *newlist)
           break;
         }
       }
-      if (!found) {
+      if (!found && egg_strcasecmp(botold->nick, origbotname)) {	/* Never kill ME.. will handle it elsewhere */
         botold->pid = checkpid(botold->nick, botold, NULL);
         conf_killbot(NULL, botold, SIGKILL);
         if ((u = get_user_by_handle(userlist, botold->nick))) {
@@ -1069,7 +1068,7 @@ void kill_removed_bots(conf_bot *oldlist, conf_bot *newlist)
 }
 
 void
-fill_conf_bot()
+fill_conf_bot(bool fatal)
 {
   if (!conf.bots || !conf.bots->nick)
     return;
@@ -1090,13 +1089,19 @@ fill_conf_bot()
     if (!egg_strcasecmp(me->nick, mynick))
       break;
 
-  if (!me || (me->nick && egg_strcasecmp(me->nick, mynick)))
+  if (fatal && (!me || (me->nick && egg_strcasecmp(me->nick, mynick))))
     werr(ERR_BADBOT);
 
   free(mynick);
-  /* for future, we may just want to make this a pointer to ->bots if we do an emech style currentbot-> */
-  conf.bot = (conf_bot *) my_calloc(1, sizeof(conf_bot));
-  conf_bot_dup(conf.bot, me);
+
+  if (me) {
+    if (!me->hub && me->localhub)
+      sdprintf("I am localhub!");
+
+    /* for future, we may just want to make this a pointer to ->bots if we do an emech style currentbot-> */
+    conf.bot = (conf_bot *) my_calloc(1, sizeof(conf_bot));
+    conf_bot_dup(conf.bot, me);
+  }
 }
 
 void

+ 3 - 1
src/conf.h

@@ -65,10 +65,11 @@ pid_t checkpid(const char *, conf_bot *, const char *);
 void init_conf();
 void free_conf();
 void free_conf_bots(conf_bot *);
+void free_bot(conf_bot *bot);
 int readconf(const char *, int);
 int parseconf(bool);
 int writeconf(char *, FILE *, int);
-void fill_conf_bot();
+void fill_conf_bot(bool fatal = 1);
 void bin_to_conf(bool error = 0);
 void conf_checkpids(bool all = 1);
 void conf_add_userlist_bots();
@@ -76,6 +77,7 @@ conf_bot *conf_bots_dup(conf_bot *);
 void kill_removed_bots(conf_bot *, conf_bot *);
 conf_bot *conf_getlocalhub(conf_bot *);
 void conf_setmypid(pid_t);
+void conf_bot_dup(conf_bot *dest, conf_bot *src);
 
 #ifdef CYGWIN_HACKS
 extern char		cfile[DIRMAX];

+ 1 - 1
src/main.c

@@ -158,7 +158,7 @@ static char *getfullbinname(const char *argv_zero)
 
 void fatal(const char *s, int recoverable)
 {
-  if (conf.bot && !conf.bot->hub)
+  if (server_online)
     nuke_server((char *) s);
 
   if (s && s[0])

+ 3 - 2
src/misc.c

@@ -556,8 +556,9 @@ int str_isdigit(const char *str)
  */
 void kill_bot(char *s1, char *s2)
 {
-  write_userfile(-1);
-  if (!conf.bot->hub)
+  if (conf.bot->hub)
+    write_userfile(-1);
+  else
     server_die();
   chatout("*** %s\n", s1);
   botnet_send_chat(-1, conf.bot->nick, s1);

+ 1 - 1
src/mod/server.mod/servmsg.c

@@ -304,7 +304,7 @@ void nuke_server(const char *reason)
 {
   if (serv >= 0 && servidx >= 0) {
     if (reason)
-      dprintf(DP_DUMP, "QUIT :%s\n", reason);
+      dprintf(-serv, "QUIT :%s\n", reason);
 
     sleep(1);
     disconnect_server(servidx, DO_LOST);