[PATCH 05/36] rpc: convert virNetServerProgram to GObject

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Signed-off-by: Rafael Fonseca <r4f4rfs@xxxxxxxxx>
---
 src/locking/lock_daemon.c           | 28 ++++++----------
 src/logging/log_daemon.c            | 28 ++++++----------
 src/lxc/lxc_controller.c            | 13 +++++---
 src/remote/remote_daemon.c          | 52 ++++++++++-------------------
 src/remote/remote_daemon_dispatch.c | 19 ++++++-----
 src/remote/remote_daemon_stream.c   |  5 +--
 src/rpc/virnetserver.c              | 14 ++++----
 src/rpc/virnetserverprogram.c       | 30 ++++++++---------
 src/rpc/virnetserverprogram.h       | 10 ++++--
 9 files changed, 88 insertions(+), 111 deletions(-)

diff --git a/src/locking/lock_daemon.c b/src/locking/lock_daemon.c
index 4eff63014a..260a74a81a 100644
--- a/src/locking/lock_daemon.c
+++ b/src/locking/lock_daemon.c
@@ -860,8 +860,8 @@ virLockDaemonUsage(const char *argv0, bool privileged)
 int main(int argc, char **argv) {
     virNetServerPtr lockSrv = NULL;
     virNetServerPtr adminSrv = NULL;
-    virNetServerProgramPtr lockProgram = NULL;
-    virNetServerProgramPtr adminProgram = NULL;
+    g_autoptr(virNetServerProgram) lockProgram = NULL;
+    g_autoptr(virNetServerProgram) adminProgram = NULL;
     char *remote_config_file = NULL;
     int statuswrite = -1;
     int ret = 1;
@@ -1134,13 +1134,10 @@ int main(int argc, char **argv) {
         goto cleanup;
     }
 
-    if (!(lockProgram = virNetServerProgramNew(VIR_LOCK_SPACE_PROTOCOL_PROGRAM,
-                                               VIR_LOCK_SPACE_PROTOCOL_PROGRAM_VERSION,
-                                               virLockSpaceProtocolProcs,
-                                               virLockSpaceProtocolNProcs))) {
-        ret = VIR_DAEMON_ERR_INIT;
-        goto cleanup;
-    }
+    lockProgram = virNetServerProgramNew(VIR_LOCK_SPACE_PROTOCOL_PROGRAM,
+                                         VIR_LOCK_SPACE_PROTOCOL_PROGRAM_VERSION,
+                                         virLockSpaceProtocolProcs,
+                                         virLockSpaceProtocolNProcs);
 
     if (virNetServerAddProgram(lockSrv, lockProgram) < 0) {
         ret = VIR_DAEMON_ERR_INIT;
@@ -1148,13 +1145,10 @@ int main(int argc, char **argv) {
     }
 
     if (adminSrv != NULL) {
-        if (!(adminProgram = virNetServerProgramNew(ADMIN_PROGRAM,
-                                                    ADMIN_PROTOCOL_VERSION,
-                                                    adminProcs,
-                                                    adminNProcs))) {
-            ret = VIR_DAEMON_ERR_INIT;
-            goto cleanup;
-        }
+        adminProgram = virNetServerProgramNew(ADMIN_PROGRAM,
+                                              ADMIN_PROTOCOL_VERSION,
+                                              adminProcs,
+                                              adminNProcs);
         if (virNetServerAddProgram(adminSrv, adminProgram) < 0) {
             ret = VIR_DAEMON_ERR_INIT;
             goto cleanup;
@@ -1191,8 +1185,6 @@ int main(int argc, char **argv) {
         ret = 0;
 
  cleanup:
-    virObjectUnref(lockProgram);
-    virObjectUnref(adminProgram);
     virObjectUnref(lockSrv);
     virObjectUnref(adminSrv);
     virLockDaemonFree(lockDaemon);
diff --git a/src/logging/log_daemon.c b/src/logging/log_daemon.c
index f37054706e..98f8b9c68b 100644
--- a/src/logging/log_daemon.c
+++ b/src/logging/log_daemon.c
@@ -641,8 +641,8 @@ virLogDaemonUsage(const char *argv0, bool privileged)
 int main(int argc, char **argv) {
     virNetServerPtr logSrv = NULL;
     virNetServerPtr adminSrv = NULL;
-    virNetServerProgramPtr logProgram = NULL;
-    virNetServerProgramPtr adminProgram = NULL;
+    g_autoptr(virNetServerProgram) logProgram = NULL;
+    g_autoptr(virNetServerProgram) adminProgram = NULL;
     char *remote_config_file = NULL;
     int statuswrite = -1;
     int ret = 1;
@@ -916,26 +916,20 @@ int main(int argc, char **argv) {
         goto cleanup;
     }
 
-    if (!(logProgram = virNetServerProgramNew(VIR_LOG_MANAGER_PROTOCOL_PROGRAM,
-                                              VIR_LOG_MANAGER_PROTOCOL_PROGRAM_VERSION,
-                                              virLogManagerProtocolProcs,
-                                              virLogManagerProtocolNProcs))) {
-        ret = VIR_DAEMON_ERR_INIT;
-        goto cleanup;
-    }
+    logProgram = virNetServerProgramNew(VIR_LOG_MANAGER_PROTOCOL_PROGRAM,
+                                        VIR_LOG_MANAGER_PROTOCOL_PROGRAM_VERSION,
+                                        virLogManagerProtocolProcs,
+                                        virLogManagerProtocolNProcs);
     if (virNetServerAddProgram(logSrv, logProgram) < 0) {
         ret = VIR_DAEMON_ERR_INIT;
         goto cleanup;
     }
 
     if (adminSrv != NULL) {
-        if (!(adminProgram = virNetServerProgramNew(ADMIN_PROGRAM,
-                                                    ADMIN_PROTOCOL_VERSION,
-                                                    adminProcs,
-                                                    adminNProcs))) {
-            ret = VIR_DAEMON_ERR_INIT;
-            goto cleanup;
-        }
+        adminProgram = virNetServerProgramNew(ADMIN_PROGRAM,
+                                              ADMIN_PROTOCOL_VERSION,
+                                              adminProcs,
+                                              adminNProcs);
         if (virNetServerAddProgram(adminSrv, adminProgram) < 0) {
             ret = VIR_DAEMON_ERR_INIT;
             goto cleanup;
@@ -971,8 +965,6 @@ int main(int argc, char **argv) {
         ret = 0;
 
  cleanup:
-    virObjectUnref(logProgram);
-    virObjectUnref(adminProgram);
     virObjectUnref(logSrv);
     virObjectUnref(adminSrv);
     virLogDaemonFree(logDaemon);
diff --git a/src/lxc/lxc_controller.c b/src/lxc/lxc_controller.c
index 3d3258eabe..2369704ac6 100644
--- a/src/lxc/lxc_controller.c
+++ b/src/lxc/lxc_controller.c
@@ -317,6 +317,8 @@ static void virLXCControllerFree(virLXCControllerPtr ctrl)
     VIR_FREE(ctrl->nsFDs);
     virCgroupFree(&ctrl->cgroup);
 
+    if (ctrl->prog)
+        g_object_unref(ctrl->prog);
     /* This must always be the last thing to be closed */
     VIR_FORCE_CLOSE(ctrl->handshakeFd);
     VIR_FREE(ctrl);
@@ -992,11 +994,10 @@ static int virLXCControllerSetupServer(virLXCControllerPtr ctrl)
     virObjectUnref(svc);
     svc = NULL;
 
-    if (!(ctrl->prog = virNetServerProgramNew(VIR_LXC_MONITOR_PROGRAM,
-                                              VIR_LXC_MONITOR_PROGRAM_VERSION,
-                                              virLXCMonitorProcs,
-                                              virLXCMonitorNProcs)))
-        goto error;
+    ctrl->prog = virNetServerProgramNew(VIR_LXC_MONITOR_PROGRAM,
+                                        VIR_LXC_MONITOR_PROGRAM_VERSION,
+                                        virLXCMonitorProcs,
+                                        virLXCMonitorNProcs);
 
     if (!(ctrl->daemon = virNetDaemonNew()) ||
         virNetDaemonAddServer(ctrl->daemon, srv) < 0)
@@ -1008,6 +1009,8 @@ static int virLXCControllerSetupServer(virLXCControllerPtr ctrl)
 
  error:
     VIR_FREE(sockpath);
+    if (ctrl->prog)
+        g_clear_object(&ctrl->prog);
     virObjectUnref(srv);
     virObjectUnref(ctrl->daemon);
     ctrl->daemon = NULL;
diff --git a/src/remote/remote_daemon.c b/src/remote/remote_daemon.c
index 7eec599177..46b6b4fdc7 100644
--- a/src/remote/remote_daemon.c
+++ b/src/remote/remote_daemon.c
@@ -760,8 +760,8 @@ int main(int argc, char **argv) {
     virNetDaemonPtr dmn = NULL;
     virNetServerPtr srv = NULL;
     virNetServerPtr srvAdm = NULL;
-    virNetServerProgramPtr adminProgram = NULL;
-    virNetServerProgramPtr lxcProgram = NULL;
+    g_autoptr(virNetServerProgram) adminProgram = NULL;
+    g_autoptr(virNetServerProgram) lxcProgram = NULL;
     char *remote_config_file = NULL;
     int statuswrite = -1;
     int ret = 1;
@@ -1038,37 +1038,28 @@ int main(int argc, char **argv) {
     remoteProcs[REMOTE_PROC_AUTH_SASL_STEP].needAuth = false;
     remoteProcs[REMOTE_PROC_AUTH_SASL_START].needAuth = false;
     remoteProcs[REMOTE_PROC_AUTH_POLKIT].needAuth = false;
-    if (!(remoteProgram = virNetServerProgramNew(REMOTE_PROGRAM,
-                                                 REMOTE_PROTOCOL_VERSION,
-                                                 remoteProcs,
-                                                 remoteNProcs))) {
-        ret = VIR_DAEMON_ERR_INIT;
-        goto cleanup;
-    }
+    remoteProgram = virNetServerProgramNew(REMOTE_PROGRAM,
+                                           REMOTE_PROTOCOL_VERSION,
+                                           remoteProcs,
+                                           remoteNProcs);
     if (virNetServerAddProgram(srv, remoteProgram) < 0) {
         ret = VIR_DAEMON_ERR_INIT;
         goto cleanup;
     }
 
-    if (!(lxcProgram = virNetServerProgramNew(LXC_PROGRAM,
-                                              LXC_PROTOCOL_VERSION,
-                                              lxcProcs,
-                                              lxcNProcs))) {
-        ret = VIR_DAEMON_ERR_INIT;
-        goto cleanup;
-    }
+    lxcProgram = virNetServerProgramNew(LXC_PROGRAM,
+                                        LXC_PROTOCOL_VERSION,
+                                        lxcProcs,
+                                        lxcNProcs);
     if (virNetServerAddProgram(srv, lxcProgram) < 0) {
         ret = VIR_DAEMON_ERR_INIT;
         goto cleanup;
     }
 
-    if (!(qemuProgram = virNetServerProgramNew(QEMU_PROGRAM,
-                                               QEMU_PROTOCOL_VERSION,
-                                               qemuProcs,
-                                               qemuNProcs))) {
-        ret = VIR_DAEMON_ERR_INIT;
-        goto cleanup;
-    }
+    qemuProgram = virNetServerProgramNew(QEMU_PROGRAM,
+                                         QEMU_PROTOCOL_VERSION,
+                                         qemuProcs,
+                                         qemuNProcs);
     if (virNetServerAddProgram(srv, qemuProgram) < 0) {
         ret = VIR_DAEMON_ERR_INIT;
         goto cleanup;
@@ -1095,13 +1086,10 @@ int main(int argc, char **argv) {
         goto cleanup;
     }
 
-    if (!(adminProgram = virNetServerProgramNew(ADMIN_PROGRAM,
-                                                ADMIN_PROTOCOL_VERSION,
-                                                adminProcs,
-                                                adminNProcs))) {
-        ret = VIR_DAEMON_ERR_INIT;
-        goto cleanup;
-    }
+    adminProgram = virNetServerProgramNew(ADMIN_PROGRAM,
+                                          ADMIN_PROTOCOL_VERSION,
+                                          adminProcs,
+                                          adminNProcs);
     if (virNetServerAddProgram(srvAdm, adminProgram) < 0) {
         ret = VIR_DAEMON_ERR_INIT;
         goto cleanup;
@@ -1213,11 +1201,7 @@ int main(int argc, char **argv) {
         virStateCleanup();
     }
 
-    virObjectUnref(adminProgram);
     virObjectUnref(srvAdm);
-    virObjectUnref(qemuProgram);
-    virObjectUnref(lxcProgram);
-    virObjectUnref(remoteProgram);
     virObjectUnref(srv);
     virObjectUnref(dmn);
 
diff --git a/src/remote/remote_daemon_dispatch.c b/src/remote/remote_daemon_dispatch.c
index c5506c2e11..c024211ea5 100644
--- a/src/remote/remote_daemon_dispatch.c
+++ b/src/remote/remote_daemon_dispatch.c
@@ -150,7 +150,8 @@ remoteEventCallbackFree(void *opaque)
     daemonClientEventCallbackPtr callback = opaque;
     if (!callback)
         return;
-    virObjectUnref(callback->program);
+    if (callback->program)
+        g_object_unref(callback->program);
     virObjectUnref(callback->client);
     VIR_FREE(callback);
 }
@@ -4255,7 +4256,7 @@ remoteDispatchConnectDomainEventRegister(virNetServerPtr server G_GNUC_UNUSED,
     if (VIR_ALLOC(callback) < 0)
         goto cleanup;
     callback->client = virObjectRef(client);
-    callback->program = virObjectRef(remoteProgram);
+    callback->program = g_object_ref(remoteProgram);
     callback->eventID = VIR_DOMAIN_EVENT_ID_LIFECYCLE;
     callback->callbackID = -1;
     callback->legacy = true;
@@ -4484,7 +4485,7 @@ remoteDispatchConnectDomainEventRegisterAny(virNetServerPtr server G_GNUC_UNUSED
     if (VIR_ALLOC(callback) < 0)
         goto cleanup;
     callback->client = virObjectRef(client);
-    callback->program = virObjectRef(remoteProgram);
+    callback->program = g_object_ref(remoteProgram);
     callback->eventID = args->eventID;
     callback->callbackID = -1;
     callback->legacy = true;
@@ -4560,7 +4561,7 @@ remoteDispatchConnectDomainEventCallbackRegisterAny(virNetServerPtr server G_GNU
     if (VIR_ALLOC(callback) < 0)
         goto cleanup;
     callback->client = virObjectRef(client);
-    callback->program = virObjectRef(remoteProgram);
+    callback->program = g_object_ref(remoteProgram);
     callback->eventID = args->eventID;
     callback->callbackID = -1;
     ref = callback;
@@ -6043,7 +6044,7 @@ remoteDispatchConnectNetworkEventRegisterAny(virNetServerPtr server G_GNUC_UNUSE
     if (VIR_ALLOC(callback) < 0)
         goto cleanup;
     callback->client = virObjectRef(client);
-    callback->program = virObjectRef(remoteProgram);
+    callback->program = g_object_ref(remoteProgram);
     callback->eventID = args->eventID;
     callback->callbackID = -1;
     ref = callback;
@@ -6164,7 +6165,7 @@ remoteDispatchConnectStoragePoolEventRegisterAny(virNetServerPtr server G_GNUC_U
     if (VIR_ALLOC(callback) < 0)
         goto cleanup;
     callback->client = virObjectRef(client);
-    callback->program = virObjectRef(remoteProgram);
+    callback->program = g_object_ref(remoteProgram);
     callback->eventID = args->eventID;
     callback->callbackID = -1;
     ref = callback;
@@ -6284,7 +6285,7 @@ remoteDispatchConnectNodeDeviceEventRegisterAny(virNetServerPtr server G_GNUC_UN
     if (VIR_ALLOC(callback) < 0)
         goto cleanup;
     callback->client = virObjectRef(client);
-    callback->program = virObjectRef(remoteProgram);
+    callback->program = g_object_ref(remoteProgram);
     callback->eventID = args->eventID;
     callback->callbackID = -1;
     ref = callback;
@@ -6404,7 +6405,7 @@ remoteDispatchConnectSecretEventRegisterAny(virNetServerPtr server G_GNUC_UNUSED
     if (VIR_ALLOC(callback) < 0)
         goto cleanup;
     callback->client = virObjectRef(client);
-    callback->program = virObjectRef(remoteProgram);
+    callback->program = g_object_ref(remoteProgram);
     callback->eventID = args->eventID;
     callback->callbackID = -1;
     ref = callback;
@@ -6519,7 +6520,7 @@ qemuDispatchConnectDomainMonitorEventRegister(virNetServerPtr server G_GNUC_UNUS
     if (VIR_ALLOC(callback) < 0)
         goto cleanup;
     callback->client = virObjectRef(client);
-    callback->program = virObjectRef(qemuProgram);
+    callback->program = g_object_ref(qemuProgram);
     callback->eventID = -1;
     callback->callbackID = -1;
     ref = callback;
diff --git a/src/remote/remote_daemon_stream.c b/src/remote/remote_daemon_stream.c
index 73e4d7befb..ec9667fe56 100644
--- a/src/remote/remote_daemon_stream.c
+++ b/src/remote/remote_daemon_stream.c
@@ -367,7 +367,7 @@ daemonCreateClientStream(virNetServerClientPtr client,
 
     stream->refs = 1;
     stream->priv = priv;
-    stream->prog = virObjectRef(prog);
+    stream->prog = g_object_ref(prog);
     stream->procedure = header->proc;
     stream->serial = header->serial;
     stream->filterID = -1;
@@ -399,7 +399,8 @@ int daemonFreeClientStream(virNetServerClientPtr client,
     VIR_DEBUG("client=%p, proc=%d, serial=%u",
               client, stream->procedure, stream->serial);
 
-    virObjectUnref(stream->prog);
+    if (stream->prog)
+        g_object_unref(stream->prog);
 
     msg = stream->rx;
     while (msg) {
diff --git a/src/rpc/virnetserver.c b/src/rpc/virnetserver.c
index 242052754f..5e9124c7ee 100644
--- a/src/rpc/virnetserver.c
+++ b/src/rpc/virnetserver.c
@@ -154,13 +154,15 @@ static void virNetServerHandleJob(void *jobOpaque, void *opaque)
     if (virNetServerProcessMsg(srv, job->client, job->prog, job->msg) < 0)
         goto error;
 
-    virObjectUnref(job->prog);
+    if (job->prog)
+        g_object_unref(job->prog);
     virObjectUnref(job->client);
     VIR_FREE(job);
     return;
 
  error:
-    virObjectUnref(job->prog);
+    if (job->prog)
+        g_object_unref(job->prog);
     virNetMessageFree(job->msg);
     virNetServerClientClose(job->client);
     virObjectUnref(job->client);
@@ -218,14 +220,14 @@ virNetServerDispatchNewMessage(virNetServerClientPtr client,
         job->msg = msg;
 
         if (prog) {
-            job->prog = virObjectRef(prog);
+            job->prog = g_object_ref(prog);
             priority = virNetServerProgramGetPriority(prog, msg->header.proc);
         }
 
         if (virThreadPoolSendJob(srv->workers, priority, job) < 0) {
             virObjectUnref(client);
             VIR_FREE(job);
-            virObjectUnref(prog);
+            g_object_unref(prog);
             goto error;
         }
     } else {
@@ -825,7 +827,7 @@ int virNetServerAddProgram(virNetServerPtr srv,
     if (VIR_EXPAND_N(srv->programs, srv->nprograms, 1) < 0)
         goto error;
 
-    srv->programs[srv->nprograms-1] = virObjectRef(prog);
+    srv->programs[srv->nprograms-1] = g_object_ref(prog);
 
     virObjectUnlock(srv);
     return 0;
@@ -918,7 +920,7 @@ void virNetServerDispose(void *obj)
     VIR_FREE(srv->services);
 
     for (i = 0; i < srv->nprograms; i++)
-        virObjectUnref(srv->programs[i]);
+        g_object_unref(srv->programs[i]);
     VIR_FREE(srv->programs);
 
     for (i = 0; i < srv->nclients; i++)
diff --git a/src/rpc/virnetserverprogram.c b/src/rpc/virnetserverprogram.c
index 8df92fb8df..56c984ce13 100644
--- a/src/rpc/virnetserverprogram.c
+++ b/src/rpc/virnetserverprogram.c
@@ -35,7 +35,7 @@
 VIR_LOG_INIT("rpc.netserverprogram");
 
 struct _virNetServerProgram {
-    virObject parent;
+    GObject parent;
 
     unsigned program;
     unsigned version;
@@ -44,32 +44,27 @@ struct _virNetServerProgram {
 };
 
 
-static virClassPtr virNetServerProgramClass;
-static void virNetServerProgramDispose(void *obj);
+G_DEFINE_TYPE(virNetServerProgram, vir_net_server_program, G_TYPE_OBJECT);
+static void virNetServerProgramFinalize(GObject *obj);
 
-static int virNetServerProgramOnceInit(void)
+static void vir_net_server_program_init(virNetServerProgram *prog G_GNUC_UNUSED)
 {
-    if (!VIR_CLASS_NEW(virNetServerProgram, virClassForObject()))
-        return -1;
-
-    return 0;
 }
 
-VIR_ONCE_GLOBAL_INIT(virNetServerProgram);
+static void vir_net_server_program_class_init(virNetServerProgramClass *klass)
+{
+    GObjectClass *obj = G_OBJECT_CLASS(klass);
 
+    obj->finalize = virNetServerProgramFinalize;
+}
 
 virNetServerProgramPtr virNetServerProgramNew(unsigned program,
                                               unsigned version,
                                               virNetServerProgramProcPtr procs,
                                               size_t nprocs)
 {
-    virNetServerProgramPtr prog;
-
-    if (virNetServerProgramInitialize() < 0)
-        return NULL;
-
-    if (!(prog = virObjectNew(virNetServerProgramClass)))
-        return NULL;
+    virNetServerProgramPtr prog =
+        VIR_NET_SERVER_PROGRAM(g_object_new(VIR_TYPE_NET_SERVER_PROGRAM, NULL));
 
     prog->program = program;
     prog->version = version;
@@ -569,6 +564,7 @@ int virNetServerProgramSendStreamHole(virNetServerProgramPtr prog,
 }
 
 
-void virNetServerProgramDispose(void *obj G_GNUC_UNUSED)
+void virNetServerProgramFinalize(GObject *obj)
 {
+    G_OBJECT_CLASS(vir_net_server_program_parent_class)->finalize(obj);
 }
diff --git a/src/rpc/virnetserverprogram.h b/src/rpc/virnetserverprogram.h
index 851bbf0183..bcc10809d4 100644
--- a/src/rpc/virnetserverprogram.h
+++ b/src/rpc/virnetserverprogram.h
@@ -23,7 +23,8 @@
 
 #include "virnetmessage.h"
 #include "virnetserverclient.h"
-#include "virobject.h"
+#include "internal.h"
+#include <glib-object.h>
 
 typedef struct _virNetDaemon virNetDaemon;
 typedef virNetDaemon *virNetDaemonPtr;
@@ -31,7 +32,12 @@ typedef virNetDaemon *virNetDaemonPtr;
 typedef struct _virNetServerService virNetServerService;
 typedef virNetServerService *virNetServerServicePtr;
 
-typedef struct _virNetServerProgram virNetServerProgram;
+#define VIR_TYPE_NET_SERVER_PROGRAM vir_net_server_program_get_type()
+G_DECLARE_FINAL_TYPE(virNetServerProgram,
+                     vir_net_server_program,
+                     VIR,
+                     NET_SERVER_PROGRAM,
+                     GObject);
 typedef virNetServerProgram *virNetServerProgramPtr;
 
 typedef struct _virNetServerProgramProc virNetServerProgramProc;
-- 
2.25.1






[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux