]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
vshCommandRun: Convert to directly return the exit code
authorPeter Krempa <pkrempa@redhat.com>
Tue, 3 Jun 2025 15:18:02 +0000 (17:18 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Thu, 12 Jun 2025 08:17:03 +0000 (10:17 +0200)
Currently the handler functions in the virt shells return only a boolean
signalling if the command was successful or not. In preparation for a
command which will want to return another value (timeout) convert
vshCommand run to actually return the requested exit code instead and
document the conversion from boolean.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
tools/virsh.c
tools/virt-admin.c
tools/vsh.c
tools/vsh.h

index 244ca655ee997439d044278b0ac0d1a5f2dd3d2a..643ef6b4538c21afa50159dccf5acfb0eee1e233 100644 (file)
@@ -836,7 +836,7 @@ main(int argc, char **argv)
     vshControl _ctl = { 0 };
     vshControl *ctl = &_ctl;
     virshControl virshCtl = { 0 };
-    bool ret = true;
+    int ret = EXIT_SUCCESS;
 
     ctl->name = "virsh";        /* hardcoded name of the binary */
     ctl->env_prefix = "VIRSH";
@@ -930,5 +930,5 @@ main(int argc, char **argv)
     }
 
     virshDeinit(ctl);
-    exit(ret ? EXIT_SUCCESS : EXIT_FAILURE);
+    exit(ret);
 }
index b701ed1fe4dddf3d41a548c01a6fd7c4f6226f74..ac8f3202b6e6993c051bce68e9a5c1d40d32f2cf 100644 (file)
@@ -1530,7 +1530,7 @@ main(int argc, char **argv)
     vshControl _ctl = { 0 };
     vshControl *ctl = &_ctl;
     vshAdmControl virtAdminCtl = { 0 };
-    bool ret = true;
+    int ret = EXIT_SUCCESS;
 
     ctl->name = "virt-admin";        /* hardcoded name of the binary */
     ctl->env_prefix = "VIRT_ADMIN";
@@ -1612,5 +1612,5 @@ main(int argc, char **argv)
     }
 
     vshAdmDeinit(ctl);
-    exit(ret ? EXIT_SUCCESS : EXIT_FAILURE);
+    exit(ret);
 }
index e892cbca22951ec72e48fd568b0a47fe8de50314..497b7ec63177e3cfca038b6b324405a4b9ea4bc2 100644 (file)
@@ -1341,14 +1341,22 @@ vshBlockJobOptionBandwidth(vshControl *ctl,
 }
 
 
-/*
- * Executes command(s) and returns return code from last command
+/**
+ * vshCommandRun:
+ * @ctl: virt shell data
+ * @cmd: command to execute
+ *
+ * Returns return code from last command. Return values from command handlers
+ * which return boolean are converted as:
+ *   true -> EXIT_SUCCESS
+ *   false -> EXIT_FAILURE
  */
-bool
-vshCommandRun(vshControl *ctl, const vshCmd *cmd)
+int
+vshCommandRun(vshControl *ctl,
+              const vshCmd *cmd)
 {
     const vshClientHooks *hooks = ctl->hooks;
-    bool ret = true;
+    int ret = EXIT_SUCCESS;
 
     while (cmd) {
         gint64 before, after;
@@ -1358,16 +1366,19 @@ vshCommandRun(vshControl *ctl, const vshCmd *cmd)
 
         if ((cmd->def->flags & VSH_CMD_FLAG_NOCONNECT) ||
             (hooks && hooks->connHandler && hooks->connHandler(ctl))) {
-            ret = cmd->def->handler(ctl, cmd);
+            if (cmd->def->handler(ctl, cmd))
+                ret = EXIT_SUCCESS;
+            else
+                ret = EXIT_FAILURE;
         } else {
             /* connection is not usable, return error */
-            ret = false;
+            ret = EXIT_FAILURE;
         }
 
         after = g_get_real_time();
 
         /* try to automatically catch disconnections */
-        if (!ret &&
+        if (ret != EXIT_SUCCESS &&
             ((last_error != NULL) &&
              (((last_error->code == VIR_ERR_SYSTEM_ERROR) &&
                (last_error->domain == VIR_FROM_REMOTE)) ||
@@ -1376,7 +1387,7 @@ vshCommandRun(vshControl *ctl, const vshCmd *cmd)
               (last_error->code == VIR_ERR_INVALID_CONN))))
             disconnected++;
 
-        if (!ret)
+        if (ret != EXIT_SUCCESS)
             vshReportError(ctl);
 
         if (STREQ(cmd->def->name, "quit") ||
index 3b75216e1107d60ba2fecf42a53dc1db56c97405..284da36e3274549560502be89e0ba88addbc15ff 100644 (file)
@@ -287,7 +287,7 @@ int vshBlockJobOptionBandwidth(vshControl *ctl,
                                bool bytes,
                                unsigned long *bandwidth);
 bool vshCommandOptBool(const vshCmd *cmd, const char *name);
-bool vshCommandRun(vshControl *ctl, const vshCmd *cmd);
+int vshCommandRun(vshControl *ctl, const vshCmd *cmd);
 bool vshCommandStringParse(vshControl *ctl, char *cmdstr,
                            vshCmd **partial);