]> xenbits.xensource.com Git - libvirt.git/commitdiff
qemu: Rewrite LOOKUP_PTYS macro into a function
authorJiri Denemark <jdenemar@redhat.com>
Wed, 30 Mar 2011 09:07:59 +0000 (11:07 +0200)
committerJiri Denemark <jdenemar@redhat.com>
Tue, 5 Apr 2011 12:02:55 +0000 (14:02 +0200)
The macro is huge and gives us nothing but headache when maintaining it.

src/qemu/qemu_process.c

index 11fad500110e7f170e9c7200fced77d0f0be6f3c..48ecd5c3c6b0a85e5f1f07e8a6fd41d610518f6d 100644 (file)
@@ -894,52 +894,72 @@ qemuProcessExtractTTYPath(const char *haystack,
     return 1;
 }
 
+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;
 }