c1c534
From 08c0c0562f8444859cc110e4fc2199d2c4254a53 Mon Sep 17 00:00:00 2001
c1c534
Message-Id: <08c0c0562f8444859cc110e4fc2199d2c4254a53@dist-git>
c1c534
From: Andrea Bolognani <abologna@redhat.com>
c1c534
Date: Wed, 29 Nov 2017 16:22:54 +0100
c1c534
Subject: [PATCH] conf, qemu: Use type-aware switches where possible
c1c534
c1c534
The compiler can warn us if we add a value to the
c1c534
virDomainChrSerialTargetType enumeration but forget to handle
c1c534
it properly in the code. Let's take advantage of that.
c1c534
c1c534
This commit is best viewed with 'git diff -w'.
c1c534
c1c534
Signed-off-by: Andrea Bolognani <abologna@redhat.com>
c1c534
Reviewed-by: Pavel Hrdina <phrdina@redhat.com>
c1c534
(cherry picked from commit 18dfc0014501c3811ee32669ea96583131dca9b3)
c1c534
c1c534
https://bugzilla.redhat.com/show_bug.cgi?id=1449265
c1c534
https://bugzilla.redhat.com/show_bug.cgi?id=1511421
c1c534
https://bugzilla.redhat.com/show_bug.cgi?id=1512929
c1c534
https://bugzilla.redhat.com/show_bug.cgi?id=1512934
c1c534
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
c1c534
---
c1c534
 src/conf/domain_conf.c  | 47 ++++++++++++++++++++++++++++++-----------------
c1c534
 src/qemu/qemu_command.c |  7 ++++++-
c1c534
 2 files changed, 36 insertions(+), 18 deletions(-)
c1c534
c1c534
diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
c1c534
index 530fae9d9d..4b5e7c214f 100644
c1c534
--- a/src/conf/domain_conf.c
c1c534
+++ b/src/conf/domain_conf.c
c1c534
@@ -4046,26 +4046,39 @@ virDomainDefAddConsoleCompat(virDomainDefPtr def)
c1c534
             def->consoles[0]->targetType = VIR_DOMAIN_CHR_CONSOLE_TARGET_TYPE_SERIAL;
c1c534
         }
c1c534
     } else if (def->os.type == VIR_DOMAIN_OSTYPE_HVM && def->nserials > 0 &&
c1c534
-               def->serials[0]->deviceType == VIR_DOMAIN_CHR_DEVICE_TYPE_SERIAL &&
c1c534
-               def->serials[0]->targetType == VIR_DOMAIN_CHR_SERIAL_TARGET_TYPE_ISA) {
c1c534
-        /* Create a stub console to match the serial port.
c1c534
-         * console[0] either does not exist
c1c534
-         *                or has a different type than SERIAL or NONE.
c1c534
-         */
c1c534
-        virDomainChrDefPtr chr;
c1c534
-        if (!(chr = virDomainChrDefNew(NULL)))
c1c534
-            return -1;
c1c534
+               def->serials[0]->deviceType == VIR_DOMAIN_CHR_DEVICE_TYPE_SERIAL) {
c1c534
 
c1c534
-        if (VIR_INSERT_ELEMENT(def->consoles,
c1c534
-                               0,
c1c534
-                               def->nconsoles,
c1c534
-                               chr) < 0) {
c1c534
-            virDomainChrDefFree(chr);
c1c534
-            return -1;
c1c534
+        switch ((virDomainChrSerialTargetType) def->serials[0]->targetType) {
c1c534
+        case VIR_DOMAIN_CHR_SERIAL_TARGET_TYPE_ISA: {
c1c534
+
c1c534
+            /* Create a stub console to match the serial port.
c1c534
+             * console[0] either does not exist
c1c534
+             *                or has a different type than SERIAL or NONE.
c1c534
+             */
c1c534
+            virDomainChrDefPtr chr;
c1c534
+            if (!(chr = virDomainChrDefNew(NULL)))
c1c534
+                return -1;
c1c534
+
c1c534
+            if (VIR_INSERT_ELEMENT(def->consoles,
c1c534
+                                   0,
c1c534
+                                   def->nconsoles,
c1c534
+                                   chr) < 0) {
c1c534
+                virDomainChrDefFree(chr);
c1c534
+                return -1;
c1c534
+            }
c1c534
+
c1c534
+            def->consoles[0]->deviceType = VIR_DOMAIN_CHR_DEVICE_TYPE_CONSOLE;
c1c534
+            def->consoles[0]->targetType = VIR_DOMAIN_CHR_CONSOLE_TARGET_TYPE_SERIAL;
c1c534
+
c1c534
+            break;
c1c534
         }
c1c534
 
c1c534
-        def->consoles[0]->deviceType = VIR_DOMAIN_CHR_DEVICE_TYPE_CONSOLE;
c1c534
-        def->consoles[0]->targetType = VIR_DOMAIN_CHR_CONSOLE_TARGET_TYPE_SERIAL;
c1c534
+        case VIR_DOMAIN_CHR_SERIAL_TARGET_TYPE_PCI:
c1c534
+        case VIR_DOMAIN_CHR_SERIAL_TARGET_TYPE_USB:
c1c534
+        case VIR_DOMAIN_CHR_SERIAL_TARGET_TYPE_LAST:
c1c534
+            /* Nothing to do */
c1c534
+            break;
c1c534
+        }
c1c534
     }
c1c534
 
c1c534
     return 0;
c1c534
diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c
c1c534
index 19a819a3aa..3d970ed9d1 100644
c1c534
--- a/src/qemu/qemu_command.c
c1c534
+++ b/src/qemu/qemu_command.c
c1c534
@@ -10338,7 +10338,7 @@ qemuBuildSerialChrDeviceStr(char **deviceStr,
c1c534
                               serial->info.alias);
c1c534
         }
c1c534
     } else {
c1c534
-        switch (serial->targetType) {
c1c534
+        switch ((virDomainChrSerialTargetType) serial->targetType) {
c1c534
         case VIR_DOMAIN_CHR_SERIAL_TARGET_TYPE_USB:
c1c534
             if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE_USB_SERIAL)) {
c1c534
                 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
c1c534
@@ -10377,6 +10377,11 @@ qemuBuildSerialChrDeviceStr(char **deviceStr,
c1c534
                 goto error;
c1c534
             }
c1c534
             break;
c1c534
+
c1c534
+        case VIR_DOMAIN_CHR_SERIAL_TARGET_TYPE_LAST:
c1c534
+            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
c1c534
+                           _("Invalid target type for serial device"));
c1c534
+            goto error;
c1c534
         }
c1c534
 
c1c534
         virBufferAsprintf(&cmd, "%s,chardev=char%s,id=%s",
c1c534
-- 
c1c534
2.15.1
c1c534