From 9bf6dade0cff91c0367e62930c4699b7f483c6bd Mon Sep 17 00:00:00 2001
Message-Id: <9bf6dade0cff91c0367e62930c4699b7f483c6bd.1374754302.git.minovotn@redhat.com>
In-Reply-To: <5d75a8513d08b33975bdf5971871c0c977167cd1.1374754301.git.minovotn@redhat.com>
References: <5d75a8513d08b33975bdf5971871c0c977167cd1.1374754301.git.minovotn@redhat.com>
From: Gerd Hoffmann <kraxel@redhat.com>
Date: Mon, 24 Jun 2013 07:05:40 +0200
Subject: [PATCH 29/65] vnc: add error propagation to vnc_display_open

RH-Author: Gerd Hoffmann <kraxel@redhat.com>
Message-id: <1372057576-26450-30-git-send-email-kraxel@redhat.com>
Patchwork-id: 52132
O-Subject: [RHEL-6.5 qemu-kvm PATCH v2 29/65] vnc: add error propagation to vnc_display_open
Bugzilla: 676568
RH-Acked-by: Laszlo Ersek <lersek@redhat.com>
RH-Acked-by: Hans de Goede <hdegoede@redhat.com>
RH-Acked-by: Luiz Capitulino <lcapitulino@redhat.com>

From: Paolo Bonzini <pbonzini@redhat.com>

Before:

    $ qemu-system-x86_64 -vnc foo.bar:12345
    getaddrinfo(foo.bar,18245): Name or service not known
    Failed to start VNC server on `foo.bar:12345'

    $ qemu-system-x86_64 -vnc localhost:12345,reverse=on
    inet_connect_opts: connect(ipv4,yakj.usersys.redhat.com,127.0.0.1,12345): Connection refused
    Failed to start VNC server on `localhost:12345,reverse=on'

After:

    $ x86_64-softmmu/qemu-system-x86_64 -vnc foo.bar:12345
    Failed to start VNC server on `foo.bar:12345': address resolution failed for foo.bar:18245: Name or service not known

    $ x86_64-softmmu/qemu-system-x86_64 -vnc localhost:12345,reverse=on
    Failed to start VNC server on `localhost:12345,reverse=on': Failed to connect to socket: Connection refused

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
(cherry picked from commit 2d55f0e817b6fa260282f5b5c533bcd470c75a32)

Conflicts:

	vnc.c
---
 console.h |    3 ++-
 monitor.c |    7 +++++--
 vl.c      |    9 ++++++---
 vnc.c     |   44 ++++++++++++++++++++++++--------------------
 4 files changed, 37 insertions(+), 26 deletions(-)

Signed-off-by: Michal Novotny <minovotn@redhat.com>
---
 console.h |  3 ++-
 monitor.c |  7 +++++--
 vl.c      |  9 ++++++---
 vnc.c     | 44 ++++++++++++++++++++++++--------------------
 4 files changed, 37 insertions(+), 26 deletions(-)

diff --git a/console.h b/console.h
index 3e1f82b..3ff90ab 100644
--- a/console.h
+++ b/console.h
@@ -4,6 +4,7 @@
 #include "qemu-char.h"
 #include "qdict.h"
 #include "notify.h"
+#include "error.h"
 
 /* keyboard/mouse support */
 
@@ -370,7 +371,7 @@ void cocoa_display_init(DisplayState *ds, int full_screen);
 /* vnc.c */
 void vnc_display_init(DisplayState *ds);
 void vnc_display_close(DisplayState *ds);
-int vnc_display_open(DisplayState *ds, const char *display);
+void vnc_display_open(DisplayState *ds, const char *display, Error **errp);
 int vnc_display_password(DisplayState *ds, const char *password);
 int vnc_display_pw_expire(DisplayState *ds, time_t expires);
 void do_info_vnc_print(Monitor *mon, const QObject *data);
diff --git a/monitor.c b/monitor.c
index 5bcaa41..dfc2f00 100644
--- a/monitor.c
+++ b/monitor.c
@@ -1264,8 +1264,11 @@ static int do_change_vnc(Monitor *mon, const char *target, const char *arg)
             return monitor_read_password(mon, change_vnc_password_cb, NULL);
         }
     } else {
-        if (vnc_display_open(NULL, target) < 0) {
-            qerror_report(QERR_VNC_SERVER_FAILED, target);
+        Error *local_err = NULL;
+        vnc_display_open(NULL, target, &local_err);
+        if (error_is_set(&local_err)) {
+            qerror_report_err(local_err);
+            error_free(local_err);
             return -1;
         }
     }
diff --git a/vl.c b/vl.c
index 71370e6..657d483 100644
--- a/vl.c
+++ b/vl.c
@@ -6481,10 +6481,13 @@ int main(int argc, char **argv, char **envp)
 
     /* init remote displays */
     if (vnc_display) {
+        Error *local_err = NULL;
         vnc_display_init(ds);
-        if (vnc_display_open(ds, vnc_display) < 0) {
-            fprintf(stderr, "Failed to start VNC server on `%s'\n",
-                    vnc_display);
+        vnc_display_open(ds, vnc_display, &local_err);
+        if (local_err != NULL) {
+            fprintf(stderr, "Failed to start VNC server on `%s': %s\n",
+                    vnc_display, error_get_pretty(local_err));
+            error_free(local_err);
             exit(1);
         }
 
diff --git a/vnc.c b/vnc.c
index 9f16a07..4f685ac 100644
--- a/vnc.c
+++ b/vnc.c
@@ -2701,7 +2701,7 @@ char *vnc_display_local_addr(DisplayState *ds)
     return vnc_socket_local_addr("%s:%s", vs->lsock);
 }
 
-int vnc_display_open(DisplayState *ds, const char *display)
+void vnc_display_open(DisplayState *ds, const char *display, Error **errp)
 {
     VncDisplay *vs = ds ? (VncDisplay *)ds->opaque : vnc_display;
     const char *options;
@@ -2717,11 +2717,13 @@ int vnc_display_open(DisplayState *ds, const char *display)
 #endif
     int acl = 0;
 
-    if (!vnc_display)
-        return -1;
+    if (!vnc_display) {
+        error_setg(errp, "VNC display not active");
+        return;
+    }
     vnc_display_close(ds);
     if (strcmp(display, "none") == 0)
-        return 0;
+        return;
 
     vs->display = g_strdup(display);
     vs->share_policy = VNC_SHARE_POLICY_ALLOW_EXCLUSIVE;
@@ -2731,10 +2733,10 @@ int vnc_display_open(DisplayState *ds, const char *display)
         options++;
         if (strncmp(options, "password", 8) == 0) {
             if (fips_get_state()) {
-                fprintf(stderr,
-                        "VNC password auth disabled due to FIPS mode, "
-                        "consider using the VeNCrypt or SASL authentication "
-                        "methods as an alternative\n");
+                error_setg(errp,
+                           "VNC password auth disabled due to FIPS mode, "
+                           "consider using the VeNCrypt or SASL authentication "
+                           "methods as an alternative");
                 goto fail;
             }
             password = 1; /* Require password auth */
@@ -2765,13 +2767,13 @@ int vnc_display_open(DisplayState *ds, const char *display)
 
                 VNC_DEBUG("Trying certificate path '%s'\n", path);
                 if (vnc_tls_set_x509_creds_dir(vs, path) < 0) {
-                    fprintf(stderr, "Failed to find x509 certificates/keys in %s\n", path);
+                    error_setg(errp, "Failed to find x509 certificates/keys in %s", path);
                     g_free(path);
                     goto fail;
                 }
                 qemu_free(path);
             } else {
-                fprintf(stderr, "No certificate path provided\n");
+                error_setg(errp, "No certificate path provided");
                 goto fail;
             }
 #endif
@@ -2785,7 +2787,7 @@ int vnc_display_open(DisplayState *ds, const char *display)
             } else if (strncmp(options+6, "force-shared", 12) == 0) {
                 vs->share_policy = VNC_SHARE_POLICY_FORCE_SHARED;
             } else {
-                fprintf(stderr, "unknown vnc share= option\n");
+                error_setg(errp, "unknown vnc share= option");
                 goto fail;
             }
         }
@@ -2887,8 +2889,8 @@ int vnc_display_open(DisplayState *ds, const char *display)
 
 #ifdef CONFIG_VNC_SASL
     if ((saslErr = sasl_server_init(NULL, "qemu-kvm")) != SASL_OK) {
-        fprintf(stderr, "Failed to initialize SASL auth %s",
-                sasl_errstring(saslErr, NULL, NULL));
+        error_setg(errp, "Failed to initialize SASL auth: %s",
+                   sasl_errstring(saslErr, NULL, NULL));
         goto fail;
     }
 #endif
@@ -2896,9 +2898,9 @@ int vnc_display_open(DisplayState *ds, const char *display)
     if (reverse) {
         /* connect to viewer */
         if (strncmp(display, "unix:", 5) == 0)
-            vs->lsock = unix_connect(display+5, NULL);
+            vs->lsock = unix_connect(display+5, errp);
         else
-            vs->lsock = inet_connect(display, NULL);
+            vs->lsock = inet_connect(display, errp);
         if (vs->lsock < 0) {
             goto fail;
         } else {
@@ -2906,7 +2908,7 @@ int vnc_display_open(DisplayState *ds, const char *display)
             vs->lsock = -1;
             vnc_connect(vs, csock);
         }
-        return 0;
+        return;
 
     } else {
         /* listen for connects */
@@ -2914,10 +2916,10 @@ int vnc_display_open(DisplayState *ds, const char *display)
         dpy = qemu_malloc(256);
         if (strncmp(display, "unix:", 5) == 0) {
             pstrcpy(dpy, 256, "unix:");
-            vs->lsock = unix_listen(display+5, dpy+5, 256-5, NULL);
+            vs->lsock = unix_listen(display+5, dpy+5, 256-5, errp);
         } else {
             vs->lsock = inet_listen(display, dpy, 256,
-                                    SOCK_STREAM, 5900, NULL);
+                                    SOCK_STREAM, 5900, errp);
         }
         if (vs->lsock < 0) {
             g_free(dpy);
@@ -2927,10 +2929,12 @@ int vnc_display_open(DisplayState *ds, const char *display)
         vs->display = dpy;
         qemu_set_fd_handler2(vs->lsock, NULL, vnc_listen_read, NULL, vs);
     }
-    return 0;
+    return;
 
 fail:
+    if (!error_is_set(errp)) {
+        error_set(errp, QERR_VNC_SERVER_FAILED, display);
+    }
     g_free(vs->display);
     vs->display = NULL;
-    return -1;
 }
-- 
1.7.11.7

