virnetserver: Use autoptr for virNetServer and virNetServerClient

Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
This commit is contained in:
Jiri Denemark 2021-11-26 17:21:46 +01:00
parent a44738231e
commit a8efdb4eed
2 changed files with 28 additions and 38 deletions

View File

@ -342,7 +342,7 @@ virNetServerDispatchNewClient(virNetServerService *svc,
void *opaque) void *opaque)
{ {
virNetServer *srv = opaque; virNetServer *srv = opaque;
virNetServerClient *client; g_autoptr(virNetServerClient) client = NULL;
if (!(client = virNetServerClientNew(virNetServerNextClientID(srv), if (!(client = virNetServerClientNew(virNetServerNextClientID(srv),
clientsock, clientsock,
@ -358,10 +358,8 @@ virNetServerDispatchNewClient(virNetServerService *svc,
if (virNetServerAddClient(srv, client) < 0) { if (virNetServerAddClient(srv, client) < 0) {
virNetServerClientClose(client); virNetServerClientClose(client);
virObjectUnref(client);
return -1; return -1;
} }
virObjectUnref(client);
return 0; return 0;
} }
@ -381,7 +379,7 @@ virNetServerNew(const char *name,
virFreeCallback clientPrivFree, virFreeCallback clientPrivFree,
void *clientPrivOpaque) void *clientPrivOpaque)
{ {
virNetServer *srv; g_autoptr(virNetServer) srv = NULL;
if (virNetServerInitialize() < 0) if (virNetServerInitialize() < 0)
return NULL; return NULL;
@ -395,7 +393,7 @@ virNetServerNew(const char *name,
"rpc-worker", "rpc-worker",
NULL, NULL,
srv))) srv)))
goto error; return NULL;
srv->name = g_strdup(name); srv->name = g_strdup(name);
@ -409,10 +407,7 @@ virNetServerNew(const char *name,
srv->clientPrivFree = clientPrivFree; srv->clientPrivFree = clientPrivFree;
srv->clientPrivOpaque = clientPrivOpaque; srv->clientPrivOpaque = clientPrivOpaque;
return srv; return g_steal_pointer(&srv);
error:
virObjectUnref(srv);
return NULL;
} }
@ -425,7 +420,7 @@ virNetServerNewPostExecRestart(virJSONValue *object,
virFreeCallback clientPrivFree, virFreeCallback clientPrivFree,
void *clientPrivOpaque) void *clientPrivOpaque)
{ {
virNetServer *srv = NULL; g_autoptr(virNetServer) srv = NULL;
virJSONValue *clients; virJSONValue *clients;
virJSONValue *services; virJSONValue *services;
size_t i; size_t i;
@ -441,29 +436,29 @@ virNetServerNewPostExecRestart(virJSONValue *object,
if (virJSONValueObjectGetNumberUint(object, "min_workers", &min_workers) < 0) { if (virJSONValueObjectGetNumberUint(object, "min_workers", &min_workers) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("Missing min_workers data in JSON document")); _("Missing min_workers data in JSON document"));
goto error; return NULL;
} }
if (virJSONValueObjectGetNumberUint(object, "max_workers", &max_workers) < 0) { if (virJSONValueObjectGetNumberUint(object, "max_workers", &max_workers) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("Missing max_workers data in JSON document")); _("Missing max_workers data in JSON document"));
goto error; return NULL;
} }
if (virJSONValueObjectGetNumberUint(object, "priority_workers", &priority_workers) < 0) { if (virJSONValueObjectGetNumberUint(object, "priority_workers", &priority_workers) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("Missing priority_workers data in JSON document")); _("Missing priority_workers data in JSON document"));
goto error; return NULL;
} }
if (virJSONValueObjectGetNumberUint(object, "max_clients", &max_clients) < 0) { if (virJSONValueObjectGetNumberUint(object, "max_clients", &max_clients) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("Missing max_clients data in JSON document")); _("Missing max_clients data in JSON document"));
goto error; return NULL;
} }
if (virJSONValueObjectHasKey(object, "max_anonymous_clients")) { if (virJSONValueObjectHasKey(object, "max_anonymous_clients")) {
if (virJSONValueObjectGetNumberUint(object, "max_anonymous_clients", if (virJSONValueObjectGetNumberUint(object, "max_anonymous_clients",
&max_anonymous_clients) < 0) { &max_anonymous_clients) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("Malformed max_anonymous_clients data in JSON document")); _("Malformed max_anonymous_clients data in JSON document"));
goto error; return NULL;
} }
} else { } else {
max_anonymous_clients = max_clients; max_anonymous_clients = max_clients;
@ -471,12 +466,12 @@ virNetServerNewPostExecRestart(virJSONValue *object,
if (virJSONValueObjectGetNumberUint(object, "keepaliveInterval", &keepaliveInterval) < 0) { if (virJSONValueObjectGetNumberUint(object, "keepaliveInterval", &keepaliveInterval) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("Missing keepaliveInterval data in JSON document")); _("Missing keepaliveInterval data in JSON document"));
goto error; return NULL;
} }
if (virJSONValueObjectGetNumberUint(object, "keepaliveCount", &keepaliveCount) < 0) { if (virJSONValueObjectGetNumberUint(object, "keepaliveCount", &keepaliveCount) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("Missing keepaliveCount data in JSON document")); _("Missing keepaliveCount data in JSON document"));
goto error; return NULL;
} }
if (virJSONValueObjectGetNumberUlong(object, "next_client_id", if (virJSONValueObjectGetNumberUlong(object, "next_client_id",
@ -492,18 +487,18 @@ virNetServerNewPostExecRestart(virJSONValue *object,
keepaliveInterval, keepaliveCount, keepaliveInterval, keepaliveCount,
clientPrivNew, clientPrivPreExecRestart, clientPrivNew, clientPrivPreExecRestart,
clientPrivFree, clientPrivOpaque))) clientPrivFree, clientPrivOpaque)))
goto error; return NULL;
if (!(services = virJSONValueObjectGet(object, "services"))) { if (!(services = virJSONValueObjectGet(object, "services"))) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("Missing services data in JSON document")); _("Missing services data in JSON document"));
goto error; return NULL;
} }
if (!virJSONValueIsArray(services)) { if (!virJSONValueIsArray(services)) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("Malformed services array")); _("Malformed services array"));
goto error; return NULL;
} }
for (i = 0; i < virJSONValueArraySize(services); i++) { for (i = 0; i < virJSONValueArraySize(services); i++) {
@ -512,15 +507,15 @@ virNetServerNewPostExecRestart(virJSONValue *object,
if (!child) { if (!child) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("Missing service data in JSON document")); _("Missing service data in JSON document"));
goto error; return NULL;
} }
if (!(service = virNetServerServiceNewPostExecRestart(child))) if (!(service = virNetServerServiceNewPostExecRestart(child)))
goto error; return NULL;
if (virNetServerAddService(srv, service) < 0) { if (virNetServerAddService(srv, service) < 0) {
virObjectUnref(service); virObjectUnref(service);
goto error; return NULL;
} }
} }
@ -528,22 +523,22 @@ virNetServerNewPostExecRestart(virJSONValue *object,
if (!(clients = virJSONValueObjectGet(object, "clients"))) { if (!(clients = virJSONValueObjectGet(object, "clients"))) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("Missing clients data in JSON document")); _("Missing clients data in JSON document"));
goto error; return NULL;
} }
if (!virJSONValueIsArray(clients)) { if (!virJSONValueIsArray(clients)) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("Malformed clients array")); _("Malformed clients array"));
goto error; return NULL;
} }
for (i = 0; i < virJSONValueArraySize(clients); i++) { for (i = 0; i < virJSONValueArraySize(clients); i++) {
virNetServerClient *client; g_autoptr(virNetServerClient) client = NULL;
virJSONValue *child = virJSONValueArrayGet(clients, i); virJSONValue *child = virJSONValueArrayGet(clients, i);
if (!child) { if (!child) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("Missing client data in JSON document")); _("Missing client data in JSON document"));
goto error; return NULL;
} }
if (!(client = virNetServerClientNewPostExecRestart(srv, if (!(client = virNetServerClientNewPostExecRestart(srv,
@ -552,20 +547,13 @@ virNetServerNewPostExecRestart(virJSONValue *object,
clientPrivPreExecRestart, clientPrivPreExecRestart,
clientPrivFree, clientPrivFree,
clientPrivOpaque))) clientPrivOpaque)))
goto error; return NULL;
if (virNetServerAddClient(srv, client) < 0) { if (virNetServerAddClient(srv, client) < 0)
virObjectUnref(client); return NULL;
goto error;
}
virObjectUnref(client);
} }
return srv; return g_steal_pointer(&srv);
error:
virObjectUnref(srv);
return NULL;
} }

View File

@ -28,8 +28,10 @@
#include "virjson.h" #include "virjson.h"
typedef struct _virNetServer virNetServer; typedef struct _virNetServer virNetServer;
G_DEFINE_AUTOPTR_CLEANUP_FUNC(virNetServer, virObjectUnref);
typedef struct _virNetServerClient virNetServerClient; typedef struct _virNetServerClient virNetServerClient;
G_DEFINE_AUTOPTR_CLEANUP_FUNC(virNetServerClient, virObjectUnref);
/* This function owns the "msg" pointer it is passed and /* This function owns the "msg" pointer it is passed and
* must arrange for virNetMessageFree to be called on it * must arrange for virNetMessageFree to be called on it