]> xenbits.xensource.com Git - libvirt.git/commitdiff
bhyve: monitor: refactor register/unregister
authorRyan Moeller <ryan@ixsystems.com>
Mon, 24 Feb 2020 06:46:17 +0000 (01:46 -0500)
committerDaniel P. Berrangé <berrange@redhat.com>
Mon, 24 Feb 2020 17:25:03 +0000 (17:25 +0000)
Pull the code for registering and unregistering a bhyve monitor object
into separate functions to improve code clarity.

Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
Signed-off-by: Ryan Moeller <ryan@iXsystems.com>
src/bhyve/bhyve_monitor.c

index 58d40e1f70c3125d143e5a2f8d12c2748ee7304c..a1b1a21a6feba06c6068da950118ed15d08c6b8f 100644 (file)
@@ -69,6 +69,37 @@ bhyveMonitorOnceInit(void)
 
 VIR_ONCE_GLOBAL_INIT(bhyveMonitor);
 
+static void bhyveMonitorIO(int, int, int, void *);
+
+static bool
+bhyveMonitorRegister(bhyveMonitorPtr mon)
+{
+    virObjectRef(mon);
+    mon->watch = virEventAddHandle(mon->kq,
+                                   VIR_EVENT_HANDLE_READABLE |
+                                   VIR_EVENT_HANDLE_ERROR |
+                                   VIR_EVENT_HANDLE_HANGUP,
+                                   bhyveMonitorIO,
+                                   mon,
+                                   virObjectFreeCallback);
+    if (mon->watch < 0) {
+        VIR_DEBUG("failed to add event handle for mon %p", mon);
+        virObjectUnref(mon);
+        return false;
+    }
+    return true;
+}
+
+static void
+bhyveMonitorUnregister(bhyveMonitorPtr mon)
+{
+    if (mon->watch < 0)
+        return;
+
+    virEventRemoveHandle(mon->watch);
+    mon->watch = -1;
+}
+
 static void
 bhyveMonitorIO(int watch, int kq, int events G_GNUC_UNUSED, void *opaque)
 {
@@ -166,17 +197,7 @@ bhyveMonitorOpenImpl(virDomainObjPtr vm, bhyveConnPtr driver)
         goto cleanup;
     }
 
-    virObjectRef(mon);
-    mon->watch = virEventAddHandle(mon->kq,
-                                   VIR_EVENT_HANDLE_READABLE |
-                                   VIR_EVENT_HANDLE_ERROR |
-                                   VIR_EVENT_HANDLE_HANGUP,
-                                   bhyveMonitorIO,
-                                   mon,
-                                   virObjectFreeCallback);
-    if (mon->watch < 0) {
-        VIR_DEBUG("failed to add event handle for mon %p", mon);
-        virObjectUnref(mon);
+    if (!bhyveMonitorRegister(mon)) {
         virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                        _("unable to register monitor events"));
         goto cleanup;
@@ -209,11 +230,6 @@ bhyveMonitorClose(bhyveMonitorPtr mon)
 
     VIR_DEBUG("cleaning up bhyveMonitor %p", mon);
 
-    if (mon->watch < 0)
-        return;
-
-    virEventRemoveHandle(mon->watch);
-    mon->watch = -1;
-
+    bhyveMonitorUnregister(mon);
     virObjectUnref(mon);
 }