[libvirt] [PATCH v2 4/4] tools: console: pass stream/fd errors to user

Nikolay Shirokovskiy nshirokovskiy at virtuozzo.com
Wed Mar 13 07:39:49 UTC 2019


If the console was disconnected due to a connection problem or a problem on the
server side it is convinient to provide the cause to the user. If the error
come from the API then the error is saved in a virsh global variable. However,
since success is returned from virshRunConsole after we reach the waiting stage,
then the error is never reported. Let's track the error in the event loop.

Next after failure we do a cleanup and this cleanup can overwrite
root cause. Thus let's save root cause immediately and then set it to
virsh error after all cleanup is done.

Since we'll be sending the error to the consumer, each failure path from
the event handlers needs to be augmented to provide what error generated
the failure.

Signed-off-by: Nikolay Shirokovskiy <nshirokovskiy at virtuozzo.com>
---
 tools/virsh-console.c | 55 +++++++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 47 insertions(+), 8 deletions(-)

diff --git a/tools/virsh-console.c b/tools/virsh-console.c
index d109734..236933a 100644
--- a/tools/virsh-console.c
+++ b/tools/virsh-console.c
@@ -73,6 +73,7 @@ struct virConsole {
     struct virConsoleBuffer terminalToStream;
 
     char escapeChar;
+    virError error;
 };
 
 static virClassPtr virConsoleClass;
@@ -98,6 +99,11 @@ virConsoleHandleSignal(int sig ATTRIBUTE_UNUSED)
 static void
 virConsoleShutdown(virConsolePtr con)
 {
+    virErrorPtr err = virGetLastError();
+
+    if (con->error.code == VIR_ERR_OK && err && err->code != VIR_ERR_OK)
+        virCopyLastError(&con->error);
+
     if (con->st) {
         virStreamEventRemoveCallback(con->st);
         virStreamAbort(con->st);
@@ -128,6 +134,7 @@ virConsoleDispose(void *obj)
         virStreamFree(con->st);
 
     virCondDestroy(&con->cond);
+    virResetError(&con->error);
 }
 
 
@@ -165,6 +172,10 @@ virConsoleEventOnStream(virStreamPtr st,
         if (got == -2)
             goto cleanup; /* blocking */
         if (got <= 0) {
+            if (got == 0) {
+                virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                               _("console stream EOF"));
+            }
             virConsoleShutdown(con);
             goto cleanup;
         }
@@ -247,11 +258,14 @@ virConsoleEventOnStdin(int watch ATTRIBUTE_UNUSED,
                    con->terminalToStream.offset,
                    avail);
         if (got < 0) {
-            if (errno != EAGAIN)
+            if (errno != EAGAIN) {
+                virReportSystemError(errno, "%s", _("cannot read from stdin"));
                 virConsoleShutdown(con);
+            }
             goto cleanup;
         }
         if (got == 0) {
+            virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("EOF on stdin"));
             virConsoleShutdown(con);
             goto cleanup;
         }
@@ -267,9 +281,16 @@ virConsoleEventOnStdin(int watch ATTRIBUTE_UNUSED,
                                          VIR_STREAM_EVENT_WRITABLE);
     }
 
-    if (events & VIR_EVENT_HANDLE_ERROR ||
-        events & VIR_EVENT_HANDLE_HANGUP) {
+    if (events & VIR_EVENT_HANDLE_ERROR) {
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("IO error on stdin"));
         virConsoleShutdown(con);
+        goto cleanup;
+    }
+
+    if (events & VIR_EVENT_HANDLE_HANGUP) {
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("EOF on stdin"));
+        virConsoleShutdown(con);
+        goto cleanup;
     }
 
  cleanup:
@@ -299,8 +320,10 @@ virConsoleEventOnStdout(int watch ATTRIBUTE_UNUSED,
                      con->streamToTerminal.data,
                      con->streamToTerminal.offset);
         if (done < 0) {
-            if (errno != EAGAIN)
+            if (errno != EAGAIN) {
+                virReportSystemError(errno, "%s", _("cannot write to stdout"));
                 virConsoleShutdown(con);
+            }
             goto cleanup;
         }
         memmove(con->streamToTerminal.data,
@@ -319,9 +342,16 @@ virConsoleEventOnStdout(int watch ATTRIBUTE_UNUSED,
     if (!con->streamToTerminal.offset)
         virEventUpdateHandle(con->stdoutWatch, 0);
 
-    if (events & VIR_EVENT_HANDLE_ERROR ||
-        events & VIR_EVENT_HANDLE_HANGUP) {
+    if (events & VIR_EVENT_HANDLE_ERROR) {
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("IO error stdout"));
         virConsoleShutdown(con);
+        goto cleanup;
+    }
+
+    if (events & VIR_EVENT_HANDLE_HANGUP) {
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("EOF on stdout"));
+        virConsoleShutdown(con);
+        goto cleanup;
     }
 
  cleanup:
@@ -449,15 +479,24 @@ virshRunConsole(vshControl *ctl,
 
     while (!con->quit) {
         if (virCondWait(&con->cond, &con->parent.lock) < 0) {
-            VIR_ERROR(_("unable to wait on console condition"));
+            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                           _("unable to wait on console condition"));
             goto cleanup;
         }
     }
 
-    ret = 0;
+    if (con->error.code == VIR_ERR_OK)
+        ret = 0;
 
  cleanup:
     virConsoleShutdown(con);
+
+    if (ret < 0) {
+        vshResetLibvirtError();
+        virSetError(&con->error);
+        vshSaveLibvirtHelperError();
+    }
+
     virObjectUnlock(con);
     virObjectUnref(con);
 
-- 
1.8.3.1




More information about the libvir-list mailing list