Also introduces a G_DEFINE_AUTOPTR_CLEANUP_FUNC for virCHMonitor.

Signed-off-by: William Douglas <william.doug...@intel.com>
---
 src/ch/ch_monitor.c | 22 +++++++---------------
 src/ch/ch_monitor.h |  1 +
 2 files changed, 8 insertions(+), 15 deletions(-)

diff --git a/src/ch/ch_monitor.c b/src/ch/ch_monitor.c
index bb49c70069..376404033e 100644
--- a/src/ch/ch_monitor.c
+++ b/src/ch/ch_monitor.c
@@ -442,9 +442,8 @@ chMonitorCreateSocket(const char *socket_path)
 virCHMonitor *
 virCHMonitorNew(virDomainObj *vm, const char *socketdir)
 {
-    virCHMonitor *ret = NULL;
-    virCHMonitor *mon = NULL;
-    virCommand *cmd = NULL;
+    g_autoptr(virCHMonitor) mon = NULL;
+    g_autoptr(virCommand) cmd = NULL;
     int socket_fd = 0;
 
     if (virCHMonitorInitialize() < 0)
@@ -456,7 +455,7 @@ virCHMonitorNew(virDomainObj *vm, const char *socketdir)
     if (!vm->def) {
         virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                        _("VM is not defined"));
-        goto cleanup;
+        return NULL;
     }
 
     /* prepare to launch Cloud-Hypervisor socket */
@@ -465,7 +464,7 @@ virCHMonitorNew(virDomainObj *vm, const char *socketdir)
         virReportSystemError(errno,
                              _("Cannot create socket directory '%s'"),
                              socketdir);
-        goto cleanup;
+        return NULL;
     }
 
     cmd = virCommandNew(vm->def->emulator);
@@ -475,7 +474,7 @@ virCHMonitorNew(virDomainObj *vm, const char *socketdir)
         virReportSystemError(errno,
                              _("Cannot create socket '%s'"),
                              mon->socketpath);
-        goto cleanup;
+        return NULL;
     }
 
     virCommandAddArg(cmd, "--api-socket");
@@ -484,7 +483,7 @@ virCHMonitorNew(virDomainObj *vm, const char *socketdir)
 
     /* launch Cloud-Hypervisor socket */
     if (virCommandRunAsync(cmd, &mon->pid) < 0)
-        goto cleanup;
+        return NULL;
 
     /* get a curl handle */
     mon->handle = curl_easy_init();
@@ -492,14 +491,7 @@ virCHMonitorNew(virDomainObj *vm, const char *socketdir)
     /* now has its own reference */
     mon->vm = virObjectRef(vm);
 
-    ret = mon;
-    mon = NULL;
-
- cleanup:
-    if (mon)
-        virCHMonitorClose(mon);
-    virCommandFree(cmd);
-    return ret;
+    return g_steal_pointer(&mon);
 }
 
 static void virCHMonitorDispose(void *opaque)
diff --git a/src/ch/ch_monitor.h b/src/ch/ch_monitor.h
index e39b4eb8b2..0f684ca583 100644
--- a/src/ch/ch_monitor.h
+++ b/src/ch/ch_monitor.h
@@ -53,6 +53,7 @@ struct _virCHMonitor {
 
 virCHMonitor *virCHMonitorNew(virDomainObj *vm, const char *socketdir);
 void virCHMonitorClose(virCHMonitor *mon);
+G_DEFINE_AUTOPTR_CLEANUP_FUNC(virCHMonitor, virCHMonitorClose);
 
 int virCHMonitorCreateVM(virCHMonitor *mon);
 int virCHMonitorBootVM(virCHMonitor *mon);
-- 
2.33.0

Reply via email to