[libvirt] [PATCH] qemu: Rewrite LOOKUP_PTYS macro into a function

Jiri Denemark jdenemar at redhat.com
Mon Apr 4 13:46:31 UTC 2011


The macro is huge and gives us nothing but headache when maintaining it.
---
The reason for this patch is not that I'm going to modify anything in
the code. It's that I was working in the area and noticed this huge
macro.

 src/qemu/qemu_process.c |  102 ++++++++++++++++++++++++++++-------------------
 1 files changed, 61 insertions(+), 41 deletions(-)

diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c
index 90fcea0..53a42a5 100644
--- a/src/qemu/qemu_process.c
+++ b/src/qemu/qemu_process.c
@@ -895,51 +895,71 @@ qemuProcessExtractTTYPath(const char *haystack,
 }
 
 static int
+qemuProcessLookupPTYs(virDomainChrDefPtr *devices,
+                      int count,
+                      const char *prefix,
+                      virHashTablePtr paths)
+{
+    int i;
+
+    for (i = 0 ; i < count ; i++) {
+        virDomainChrDefPtr chr = devices[i];
+        if (chr->source.type == VIR_DOMAIN_CHR_TYPE_PTY) {
+            char id[16];
+            const char *path;
+
+            if (snprintf(id, sizeof(id), "%s%d", prefix, i) >= sizeof(id))
+                return -1;
+
+            path = (const char *) virHashLookup(paths, id);
+            if (path == NULL) {
+                if (chr->source.data.file.path == NULL) {
+                    /* neither the log output nor 'info chardev' had a
+                     * pty path for this chardev, report an error
+                     */
+                    qemuReportError(VIR_ERR_INTERNAL_ERROR,
+                                    _("no assigned pty for device %s"), id);
+                    return -1;
+                } else {
+                    /* 'info chardev' had no pty path for this chardev,
+                     * but the log output had, so we're fine
+                     */
+                    continue;
+                }
+            }
+
+            VIR_FREE(chr->source.data.file.path);
+            chr->source.data.file.path = strdup(path);
+
+            if (chr->source.data.file.path == NULL) {
+                virReportOOMError();
+                return -1;
+            }
+        }
+    }
+
+    return 0;
+}
+
+static int
 qemuProcessFindCharDevicePTYsMonitor(virDomainObjPtr vm,
                                      virHashTablePtr paths)
 {
-    int i;
+    if (qemuProcessLookupPTYs(vm->def->serials, vm->def->nserials,
+                              "serial", paths) < 0)
+        return -1;
+
+    if (qemuProcessLookupPTYs(vm->def->parallels, vm->def->nparallels,
+                              "parallel", paths) < 0)
+        return -1;
 
-#define LOOKUP_PTYS(array, arraylen, idprefix)                            \
-    for (i = 0 ; i < (arraylen) ; i++) {                                  \
-        virDomainChrDefPtr chr = (array)[i];                              \
-        if (chr->source.type == VIR_DOMAIN_CHR_TYPE_PTY) {                \
-            char id[16];                                                  \
-                                                                          \
-            if (snprintf(id, sizeof(id), idprefix "%i", i) >= sizeof(id)) \
-                return -1;                                                \
-                                                                          \
-            const char *path = (const char *) virHashLookup(paths, id);   \
-            if (path == NULL) {                                           \
-                if (chr->source.data.file.path == NULL) {                 \
-                    /* neither the log output nor 'info chardev' had a */ \
-                    /* pty path for this chardev, report an error */      \
-                    qemuReportError(VIR_ERR_INTERNAL_ERROR,               \
-                                    _("no assigned pty for device %s"), id); \
-                    return -1;                                            \
-                } else {                                                  \
-                    /* 'info chardev' had no pty path for this chardev, */\
-                    /* but the log output had, so we're fine */           \
-                    continue;                                             \
-                }                                                         \
-            }                                                             \
-                                                                          \
-            VIR_FREE(chr->source.data.file.path);                         \
-            chr->source.data.file.path = strdup(path);                    \
-                                                                          \
-            if (chr->source.data.file.path == NULL) {                     \
-                virReportOOMError();                                      \
-                return -1;                                                \
-            }                                                             \
-        }                                                                 \
-    }
-
-    LOOKUP_PTYS(vm->def->serials,   vm->def->nserials,   "serial");
-    LOOKUP_PTYS(vm->def->parallels, vm->def->nparallels, "parallel");
-    LOOKUP_PTYS(vm->def->channels,  vm->def->nchannels,  "channel");
-    if (vm->def->console)
-        LOOKUP_PTYS(&vm->def->console, 1,  "console");
-#undef LOOKUP_PTYS
+    if (qemuProcessLookupPTYs(vm->def->channels, vm->def->nchannels,
+                              "channel", paths) < 0)
+        return -1;
+
+    if (vm->def->console &&
+        qemuProcessLookupPTYs(&vm->def->console, 1, "console", paths) < 0)
+        return -1;
 
     return 0;
 }
-- 
1.7.4.1




More information about the libvir-list mailing list