Signed-off-by: Rafael Fonseca <r4f4rfs@xxxxxxxxx> --- src/lxc/lxc_controller.c | 5 +- src/rpc/virnetserver.c | 7 +-- src/rpc/virnetserverprogram.h | 7 ++- src/rpc/virnetserverservice.c | 103 +++++++++++++++++----------------- tests/virnetdaemontest.c | 4 +- 5 files changed, 61 insertions(+), 65 deletions(-) diff --git a/src/lxc/lxc_controller.c b/src/lxc/lxc_controller.c index 0aa56f6d24..a8a8b413f7 100644 --- a/src/lxc/lxc_controller.c +++ b/src/lxc/lxc_controller.c @@ -989,8 +989,7 @@ static int virLXCControllerSetupServer(virLXCControllerPtr ctrl) if (virNetServerAddService(srv, svc) < 0) goto error; - virObjectUnref(svc); - svc = NULL; + g_clear_object(&svc); ctrl->prog = virNetServerProgramNew(VIR_LXC_MONITOR_PROGRAM, VIR_LXC_MONITOR_PROGRAM_VERSION, @@ -1011,7 +1010,7 @@ static int virLXCControllerSetupServer(virLXCControllerPtr ctrl) virObjectUnref(srv); virObjectUnref(ctrl->daemon); ctrl->daemon = NULL; - virObjectUnref(svc); + g_clear_object(&svc); return -1; } diff --git a/src/rpc/virnetserver.c b/src/rpc/virnetserver.c index 284d68a88a..bb645bd103 100644 --- a/src/rpc/virnetserver.c +++ b/src/rpc/virnetserver.c @@ -658,7 +658,7 @@ int virNetServerAddService(virNetServerPtr srv, if (VIR_EXPAND_N(srv->services, srv->nservices, 1) < 0) goto error; - srv->services[srv->nservices-1] = virObjectRef(svc); + srv->services[srv->nservices-1] = g_object_ref(svc); virNetServerServiceSetDispatcher(svc, virNetServerDispatchNewClient, @@ -913,10 +913,7 @@ void virNetServerDispose(void *obj) virThreadPoolFree(srv->workers); - for (i = 0; i < srv->nservices; i++) - virObjectUnref(srv->services[i]); - VIR_FREE(srv->services); - + virGObjectListFreeCount(srv->services, srv->nservices); virGObjectListFreeCount(srv->programs, srv->nprograms); for (i = 0; i < srv->nclients; i++) diff --git a/src/rpc/virnetserverprogram.h b/src/rpc/virnetserverprogram.h index bcc10809d4..72dff3b9cb 100644 --- a/src/rpc/virnetserverprogram.h +++ b/src/rpc/virnetserverprogram.h @@ -29,7 +29,12 @@ typedef struct _virNetDaemon virNetDaemon; typedef virNetDaemon *virNetDaemonPtr; -typedef struct _virNetServerService virNetServerService; +#define VIR_TYPE_NET_SERVER_SERVICE vir_net_server_service_get_type() +G_DECLARE_FINAL_TYPE(virNetServerService, + vir_net_server_service, + VIR, + NET_SERVER_SERVICE, + GObject); typedef virNetServerService *virNetServerServicePtr; #define VIR_TYPE_NET_SERVER_PROGRAM vir_net_server_program_get_type() diff --git a/src/rpc/virnetserverservice.c b/src/rpc/virnetserverservice.c index 9d5df456a8..946db729c5 100644 --- a/src/rpc/virnetserverservice.c +++ b/src/rpc/virnetserverservice.c @@ -35,7 +35,7 @@ VIR_LOG_INIT("rpc.netserverservice"); struct _virNetServerService { - virObject parent; + GObject parent; size_t nsocks; virNetSocketPtr *socks; @@ -51,19 +51,21 @@ struct _virNetServerService { }; -static virClassPtr virNetServerServiceClass; -static void virNetServerServiceDispose(void *obj); +G_DEFINE_TYPE(virNetServerService, vir_net_server_service, G_TYPE_OBJECT); +static void virNetServerServiceDispose(GObject *obj); +static void virNetServerServiceFinalize(GObject *obj); -static int virNetServerServiceOnceInit(void) +static void vir_net_server_service_init(virNetServerService *svc G_GNUC_UNUSED) { - if (!VIR_CLASS_NEW(virNetServerService, virClassForObject())) - return -1; - - return 0; } -VIR_ONCE_GLOBAL_INIT(virNetServerService); +static void vir_net_server_service_class_init(virNetServerServiceClass *klass) +{ + GObjectClass *obj = G_OBJECT_CLASS(klass); + obj->dispose = virNetServerServiceDispose; + obj->finalize = virNetServerServiceFinalize; +} static void virNetServerServiceAccept(virNetSocketPtr sock, int events G_GNUC_UNUSED, @@ -97,17 +99,13 @@ virNetServerServiceNewSocket(virNetSocketPtr *socks, size_t max_queued_clients, size_t nrequests_client_max) { - virNetServerServicePtr svc; + g_autoptr(virNetServerService) svc = NULL; size_t i; - if (virNetServerServiceInitialize() < 0) - return NULL; - - if (!(svc = virObjectNew(virNetServerServiceClass))) - return NULL; + svc = VIR_NET_SERVER_SERVICE(g_object_new(VIR_TYPE_NET_SERVER_SERVICE, NULL)); if (VIR_ALLOC_N(svc->socks, nsocks) < 0) - goto error; + return NULL; svc->nsocks = nsocks; for (i = 0; i < svc->nsocks; i++) { svc->socks[i] = socks[i]; @@ -120,27 +118,22 @@ virNetServerServiceNewSocket(virNetSocketPtr *socks, for (i = 0; i < svc->nsocks; i++) { if (virNetSocketListen(svc->socks[i], max_queued_clients) < 0) - goto error; + return NULL; /* IO callback is initially disabled, until we're ready * to deal with incoming clients */ - virObjectRef(svc); + g_object_ref(svc); if (virNetSocketAddIOCallback(svc->socks[i], 0, virNetServerServiceAccept, svc, - virObjectFreeCallback) < 0) { - virObjectUnref(svc); - goto error; + g_object_unref) < 0) { + g_object_unref(svc); + return NULL; } } - - return svc; - - error: - virObjectUnref(svc); - return NULL; + return g_steal_pointer(&svc); } @@ -258,51 +251,47 @@ virNetServerServicePtr virNetServerServiceNewFDs(int *fds, virNetServerServicePtr virNetServerServiceNewPostExecRestart(virJSONValuePtr object) { - virNetServerServicePtr svc; + g_autoptr(virNetServerService) svc = NULL; virJSONValuePtr socks; size_t i; size_t n; unsigned int max; - if (virNetServerServiceInitialize() < 0) - return NULL; - - if (!(svc = virObjectNew(virNetServerServiceClass))) - return NULL; + svc = VIR_NET_SERVER_SERVICE(g_object_new(VIR_TYPE_NET_SERVER_SERVICE, NULL)); if (virJSONValueObjectGetNumberInt(object, "auth", &svc->auth) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Missing auth field in JSON state document")); - goto error; + return NULL; } if (virJSONValueObjectGetBoolean(object, "readonly", &svc->readonly) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Missing readonly field in JSON state document")); - goto error; + return NULL; } if (virJSONValueObjectGetNumberUint(object, "nrequests_client_max", &max) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Missing nrequests_client_max field in JSON state document")); - goto error; + return NULL; } svc->nrequests_client_max = max; if (!(socks = virJSONValueObjectGet(object, "socks"))) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Missing socks field in JSON state document")); - goto error; + return NULL; } if (!virJSONValueIsArray(socks)) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Malformed socks array")); - goto error; + return NULL; } n = virJSONValueArraySize(socks); if (VIR_ALLOC_N(svc->socks, n) < 0) - goto error; + return NULL; svc->nsocks = n; for (i = 0; i < svc->nsocks; i++) { @@ -311,29 +300,25 @@ virNetServerServicePtr virNetServerServiceNewPostExecRestart(virJSONValuePtr obj if (!(sock = virNetSocketNewPostExecRestart(child))) { virObjectUnref(sock); - goto error; + return NULL; } svc->socks[i] = sock; /* IO callback is initially disabled, until we're ready * to deal with incoming clients */ - virObjectRef(svc); + g_object_ref(svc); if (virNetSocketAddIOCallback(sock, 0, virNetServerServiceAccept, svc, - virObjectFreeCallback) < 0) { - virObjectUnref(svc); - goto error; + g_object_unref) < 0) { + g_object_unref(svc); + return NULL; } } - return svc; - - error: - virObjectUnref(svc); - return NULL; + return g_steal_pointer(&svc); } @@ -415,16 +400,28 @@ void virNetServerServiceSetDispatcher(virNetServerServicePtr svc, } -void virNetServerServiceDispose(void *obj) +void virNetServerServiceDispose(GObject *obj) { - virNetServerServicePtr svc = obj; + virNetServerServicePtr svc = VIR_NET_SERVER_SERVICE(obj); size_t i; - for (i = 0; i < svc->nsocks; i++) + for (i = 0; i < svc->nsocks; i++) { virObjectUnref(svc->socks[i]); - VIR_FREE(svc->socks); + svc->socks[i] = NULL; + } virObjectUnref(svc->tls); + + G_OBJECT_CLASS(vir_net_server_service_parent_class)->dispose(obj); +} + +void virNetServerServiceFinalize(GObject *obj) +{ + virNetServerServicePtr svc = VIR_NET_SERVER_SERVICE(obj); + + VIR_FREE(svc->socks); + + G_OBJECT_CLASS(vir_net_server_service_parent_class)->finalize(obj); } void virNetServerServiceToggle(virNetServerServicePtr svc, diff --git a/tests/virnetdaemontest.c b/tests/virnetdaemontest.c index 09d268627c..3107fb6747 100644 --- a/tests/virnetdaemontest.c +++ b/tests/virnetdaemontest.c @@ -85,7 +85,7 @@ static virNetServerPtr testCreateServer(const char *server_name, const char *host, int family) { virNetServerPtr srv = NULL; - virNetServerServicePtr svc1 = NULL, svc2 = NULL; + g_autoptr(virNetServerService) svc1 = NULL, svc2 = NULL; virNetServerClientPtr cln1 = NULL, cln2 = NULL; virNetSocketPtr sk1 = NULL, sk2 = NULL; int fdclient[2]; @@ -170,8 +170,6 @@ testCreateServer(const char *server_name, const char *host, int family) virDispatchError(NULL); virObjectUnref(cln1); virObjectUnref(cln2); - virObjectUnref(svc1); - virObjectUnref(svc2); virObjectUnref(sk1); virObjectUnref(sk2); return srv; -- 2.25.3