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