Просмотр исходного кода

* Added cmdline option '-r <botname>' for restarting/starting specified bot.

svn: 1769
Bryan Drewery 21 лет назад
Родитель
Сommit
bbbc309c0b
4 измененных файлов с 43 добавлено и 24 удалено
  1. 1 0
      doc/UPDATES
  2. 19 15
      src/conf.c
  3. 2 1
      src/conf.h
  4. 21 8
      src/main.c

+ 1 - 0
doc/UPDATES

@@ -9,6 +9,7 @@ Lines prefixxed with '-' were disabled before release and are not finishsed.
 * If a binary is already initialized when updating, the pack data will not be overwritten.
 * cmd_conf (delbot) now also removes the bot from the userfile
 * cmd_mns_user cosmetic change
+* Added cmdline option '-r <botname>' for restarting/starting specified bot.
 
 1.2.2
 * Don't sanity check flags for users on DCC CHAT if they are on the bot via .botcmd.

+ 19 - 15
src/conf.c

@@ -68,6 +68,20 @@ tellconf(conf_t * inconf)
   }
 }
 
+void spawnbot(const char *nick)
+{
+  size_t size = strlen(nick) + strlen(binname) + 20;
+  char *run = (char *) my_calloc(1, size);
+  int status = 0;
+
+  egg_snprintf(run, size, "%s -B %s", binname, nick);
+  sdprintf("Spawning '%s': %s", nick, run);
+  status = system(run);
+  if (status == -1 || WEXITSTATUS(status))
+    sdprintf("Failed to spawn '%s': %s", nick, strerror(errno));
+  free(run);
+}
+
 #ifdef LEAF
 /* spawn and kill bots accordingly
  * bots prefixxed with '/' will be killed auto if running.
@@ -79,6 +93,7 @@ spawnbots()
 
   for (bot = conffile.bots; bot && bot->nick; bot = bot->next) {
     sdprintf("checking bot: %s", bot->nick);
+
     if (bot->nick[0] == '/') {
       /* kill it if running */
       if (bot->pid)
@@ -93,10 +108,6 @@ spawnbots()
       sdprintf(" ... skipping. Updating: %d, pid: %d", updating, bot->pid);
       continue;
     } else {
-      int status = 0;
-      char *run = NULL;
-      size_t size = 0;
-
       /* if we are updating with -L -P, then we need to restart ALL bots */
       if (updating == UPDATE_AUTO && bot->pid) {
         kill(bot->pid, SIGKILL);
@@ -104,20 +115,13 @@ spawnbots()
         unlink(bot->pid_file);
       }
 
-      size = strlen(bot->nick) + strlen(binname) + 20;
-      run = (char *) my_calloc(1, size);
-      egg_snprintf(run, size, "%s -B %s", binname, bot->nick);
-      sdprintf("Spawning '%s': %s", bot->nick, run);
-      status = system(run);
-      if (status == -1 || WEXITSTATUS(status))
-        sdprintf("Failed to spawn '%s': %s", bot->nick, strerror(errno));
-      free(run);
+      spawnbot(bot->nick);
     }
   }
 }
 
 int
-killbot(char *botnick)
+killbot(char *botnick, int signal)
 {
   conf_bot *bot = NULL;
 
@@ -126,7 +130,7 @@ killbot(char *botnick)
       continue;
     else if (!egg_strcasecmp(botnick, bot->nick)) {
       if (bot->pid)
-        return kill(bot->pid, SIGKILL);
+        return kill(bot->pid, signal);
     }
   }
   return -1;
@@ -436,7 +440,7 @@ conf_delbot(char *botn)
   for (bot = conffile.bots; bot && bot->nick; bot = bot->next) {
     if (!strcmp(bot->nick, botn)) {     /* found it! */
       bot->pid = checkpid(bot->nick, bot);
-      killbot(bot->nick);
+      killbot(bot->nick, SIGKILL);
       free_bot(bot->nick);
       return 0;
     }

+ 2 - 1
src/conf.h

@@ -54,9 +54,10 @@ enum {
 };
 
 
+void spawnbot(const char *);
 #ifdef LEAF
 void spawnbots();
-int killbot(char *);
+int killbot(char *, int);
 #endif /* LEAF */
 void confedit() __attribute__((noreturn));
 #ifdef LEAF

+ 21 - 8
src/main.c

@@ -95,6 +95,7 @@ static bool do_confedit = 0;		/* show conf menu if -C */
 #ifdef LEAF
 static char do_killbot[21] = "";
 #endif /* LEAF */
+static int kill_sig;
 static char *update_bin = NULL;
 static bool checktrace = 1;		/* Check for trace when starting up? */
 
@@ -259,8 +260,9 @@ static void show_help()
   printf(format, STR("-G <file>"), STR("Generates a custom config for the box"));
 */
   printf(format, "-h", "Display this help listing");
-  printf(format, STR("-k <botname>"), STR("Terminates (botname) with kill -9"));
+  printf(format, STR("-k <botname>"), STR("Terminates (botname) with kill -9 (see also: -r)"));
   printf(format, STR("-n"), STR("Disables backgrounding bot (requires -B)"));
+  printf(format, STR("-r <botname>"), STR("Restarts the specified bot (see also: -k)"));
   printf(format, STR("-s"), STR("Disables checking for ptrace/strace during startup (no pass needed)"));
   printf(format, STR("-t"), STR("Enables \"Partyline\" emulation (requires -nB)"));
   printf(format, STR("-u <binary>"), STR("Update binary, Automatically kill/respawn bots"));
@@ -270,11 +272,11 @@ static void show_help()
 }
 
 #ifdef LEAF
-# define PARSE_FLAGS "0234:B:Cd:De:Eg:G:k:L:P:hnstu:U:v"
+# define PARSE_FLAGS "0234:B:Cd:De:Eg:G:k:L:P:hnr:stu:U:v"
 #else /* !LEAF */
-# define PARSE_FLAGS "0234:Cd:De:Eg:G:hnstu:U:v"
+# define PARSE_FLAGS "0234:Cd:De:Eg:G:hnr:stu:U:v"
 #endif /* HUB */
-#define FLAGS_CHECKPASS "CdDeEgGhkntuUv"
+#define FLAGS_CHECKPASS "CdDeEgGhknrtuUv"
 static void dtx_arg(int argc, char *argv[])
 {
   int i = 0;
@@ -313,8 +315,14 @@ static void dtx_arg(int argc, char *argv[])
         show_help();
 #ifdef LEAF
       case 'k':		/* kill bot */
+        kill_sig = SIGKILL;
         strlcpy(do_killbot, optarg, sizeof do_killbot);
+        break;
 #endif /* LEAF */
+      case 'r':
+        kill_sig = SIGHUP;
+        strlcpy(do_killbot, optarg, sizeof do_killbot);
+        break;
       case 'n':
 	backgrd = 0;
 	break;
@@ -617,10 +625,15 @@ static void startup_checks(int hack) {
 #ifdef LEAF
   if (localhub && !used_B) {
     if (do_killbot[0]) {
-      if (killbot(do_killbot) == 0)
-        printf("'%s' successfully killed.\n", do_killbot);
-      else
-        printf("Error killing '%s'\n", do_killbot);
+      const char *what = (kill_sig == SIGKILL ? "kill" : "restart");
+
+      if (killbot(do_killbot, kill_sig) == 0)
+        printf("'%s' successfully %sed.\n", do_killbot, what);
+      else {
+        printf("Error %sing '%s'\n", what, do_killbot);
+        if (kill_sig == SIGHUP)
+          spawnbot(do_killbot);
+      }
       exit(0);
     } else {
 #endif /* LEAF */