[PATCH 2/6] iscsi: Add exit status checking for virISCSIGetSession

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Utilize the exit status parameter for virCommandRunRegex in order to
check the return error from the 'iscsiadm --mode session' command.
Without this enabled, if there are no sessions running then virCommandRun
would have displayed an error such as:

    2016-05-13 15:17:15.165+0000: 10920: error : virCommandWait:2553 :
               internal error: Child process (iscsiadm --mode session)
               unexpected exit status 21: iscsiadm: No active sessions.

It is possible that for certain paths (when probe is true) we only care
whether it's running or not to make certain decisions.  Spitting out
the error for those paths is unnecessary.

If we do have a situation where probe = false and there's an error,
then display the error from iscsiadm if it's there; otherwise, default
to the non descript error.

Signed-off-by: John Ferlan <jferlan@xxxxxxxxxx>
---
 src/util/viriscsi.c | 28 +++++++++++++++++++++++-----
 1 file changed, 23 insertions(+), 5 deletions(-)

diff --git a/src/util/viriscsi.c b/src/util/viriscsi.c
index 846ea68..be296d7 100644
--- a/src/util/viriscsi.c
+++ b/src/util/viriscsi.c
@@ -32,6 +32,7 @@
 #include "virerror.h"
 #include "virfile.h"
 #include "virlog.h"
+#include "virprocess.h"
 #include "virrandom.h"
 #include "virstring.h"
 
@@ -79,21 +80,38 @@ virISCSIGetSession(const char *devpath,
         .session = NULL,
         .devpath = devpath,
     };
+    char *error;
+    int exitstatus = 0;
 
-    virCommandPtr cmd = virCommandNewArgList(ISCSIADM, "--mode", "session", NULL);
+    virCommandPtr cmd = virCommandNewArgList(ISCSIADM, "--mode",
+                                             "session", NULL);
+    virCommandSetErrorBuffer(cmd, &error);
 
     if (virCommandRunRegex(cmd,
                            1,
                            regexes,
                            vars,
                            virISCSIExtractSession,
-                           &cbdata, NULL, NULL) < 0)
+                           &cbdata, NULL, &exitstatus) < 0)
         goto cleanup;
 
     if (cbdata.session == NULL && !probe) {
-        virReportError(VIR_ERR_INTERNAL_ERROR,
-                       "%s", _("cannot find session"));
-        goto cleanup;
+        /* If the command failed, let's give some information as to why */
+        if (exitstatus != 0) {
+            char *str = virCommandToString(cmd);
+            char *st = virProcessTranslateStatus(exitstatus);
+
+            virReportError(VIR_ERR_INTERNAL_ERROR,
+                           _("'%s --mode session' unexpected %s%s%s"),
+                           ISCSIADM, NULLSTR(st),
+                           error ? ": " : "",
+                           error ? error : "");
+            VIR_FREE(str);
+            VIR_FREE(st);
+        } else {
+            virReportError(VIR_ERR_INTERNAL_ERROR,
+                           "%s", _("cannot find session"));
+        }
     }
 
  cleanup:
-- 
2.5.5

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list



[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]