From nobody Sun Feb 8 18:28:38 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1569604679; cv=none; d=zoho.com; s=zohoarc; b=aSNF+6v2qI3LV2MxUVRU02AKeOCj4yu5zrPm0ipvpzwwM/732HMn3E4fxMUSHMd81yNBudrB8xiMhTVN2jWcQc9F5sOeqkN7y2MYbMy9zj90Piqy+SqpFOFJZKDN2nnPoTAEBG/VlMpbojW8wC18ZECvRcViHfQLZFW6b6AVd4M= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1569604679; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=RLNQA0lunBXXi+M61afis3ZVD2DP1tOzlV0VQQ/lTcA=; b=XAILzyOvn51OwzxFXVshNzLQ1aRB+WesKni+BAh2lODlWpK5GftJLCxa5xdh9wjmmwtSbNO7AjDWU7bjTN2CvRSeM0VUIVEyjgnXdKWtFUAG0iAfRwvLk4XY97A+vkRGyvyG8cedyyVKU6s6iAztVFm0EgAdKMy3AjRw/G+YPcs= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1569604679072175.9744846164789; Fri, 27 Sep 2019 10:17:59 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 89D2910CC20A; Fri, 27 Sep 2019 17:17:56 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 6494E5D9C3; Fri, 27 Sep 2019 17:17:56 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 2C68B4EE70; Fri, 27 Sep 2019 17:17:56 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id x8RHHnbF016581 for ; Fri, 27 Sep 2019 13:17:49 -0400 Received: by smtp.corp.redhat.com (Postfix) id 2519F6012A; Fri, 27 Sep 2019 17:17:49 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-112-55.ams2.redhat.com [10.36.112.55]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7CE45600C6; Fri, 27 Sep 2019 17:17:47 +0000 (UTC) From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: libvir-list@redhat.com Date: Fri, 27 Sep 2019 18:17:29 +0100 Message-Id: <20190927171733.10842-8-berrange@redhat.com> In-Reply-To: <20190927171733.10842-1-berrange@redhat.com> References: <20190927171733.10842-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 07/11] util: convert virIdentity class to use GObject X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.6.2 (mx1.redhat.com [10.5.110.65]); Fri, 27 Sep 2019 17:17:57 +0000 (UTC) Converting from virObject to GObject is reasonably straightforward, as illustrated by this patch for virIdentity In the header file - Remove typedef struct _virIdentity virIdentity - Add #define VIR_TYPE_IDENTITY virIdentity_get_type () G_DECLARE_FINAL_TYPE (virIdentity, virIdentity, VIR, IDENTITY, GObject= ); Which provides the typedef we just removed, and class declaration boilerplate and various other constants/macros. In the source file - Change 'virObject parent' to 'GObject parent' in the struct - Remove the virClass variable and its initializing call - Add G_DEFINE_TYPE(virIdentity, virIdentity, G_TYPE_OBJECT) which declares the instance & class constructor functions - Add an impl of the instance & class constructors wiring up the finalize method to point to our dispose impl In all files - Replace VIR_AUTOUNREF(virIdentityPtr) with g_autoptr(virIdentity) - Replace virObjectRef/Unref with g_object_ref/unref. Note the latter functions do *NOT* accept a NULL object where as libvirt's do. If you replace g_object_unref with g_clear_object it is NULL safe, but also clears the pointer. Signed-off-by: Daniel P. Berrang=C3=A9 --- src/access/viraccessdriverpolkit.c | 21 +++---- src/admin/admin_server.c | 3 +- src/qemu/qemu_process.c | 4 +- src/remote/remote_daemon.c | 3 +- src/remote/remote_daemon_dispatch.c | 35 ++++-------- src/rpc/virnetserverclient.c | 57 ++++++++----------- src/rpc/virnetserverprogram.c | 13 +---- src/util/viridentity.c | 87 ++++++++++++++++------------- src/util/viridentity.h | 7 ++- tests/viridentitytest.c | 45 ++++++--------- tests/virnetserverclienttest.c | 3 +- 11 files changed, 122 insertions(+), 156 deletions(-) diff --git a/src/access/viraccessdriverpolkit.c b/src/access/viraccessdrive= rpolkit.c index e61ac6fa19..b39a28c834 100644 --- a/src/access/viraccessdriverpolkit.c +++ b/src/access/viraccessdriverpolkit.c @@ -78,8 +78,7 @@ virAccessDriverPolkitGetCaller(const char *actionid, unsigned long long *startTime, uid_t *uid) { - virIdentityPtr identity =3D virIdentityGetCurrent(); - int ret =3D -1; + g_autoptr(virIdentity) identity =3D virIdentityGetCurrent(); int rc; =20 if (!identity) { @@ -90,37 +89,33 @@ virAccessDriverPolkitGetCaller(const char *actionid, } =20 if ((rc =3D virIdentityGetProcessID(identity, pid)) < 0) - goto cleanup; + return -1; =20 if (rc =3D=3D 0) { virAccessError(VIR_ERR_INTERNAL_ERROR, "%s", _("No process ID available")); - goto cleanup; + return -1; } =20 if ((rc =3D virIdentityGetProcessTime(identity, startTime)) < 0) - goto cleanup; + return -1; =20 if (rc =3D=3D 0) { virAccessError(VIR_ERR_INTERNAL_ERROR, "%s", _("No process start time available")); - goto cleanup; + return -1; } =20 if ((rc =3D virIdentityGetUNIXUserID(identity, uid)) < 0) - goto cleanup; + return -1; =20 if (rc =3D=3D 0) { virAccessError(VIR_ERR_INTERNAL_ERROR, "%s", _("No UNIX caller UID available")); - goto cleanup; + return -1; } =20 - ret =3D 0; - - cleanup: - virObjectUnref(identity); - return ret; + return 0; } =20 =20 diff --git a/src/admin/admin_server.c b/src/admin/admin_server.c index 248df3f795..7ae819b7b0 100644 --- a/src/admin/admin_server.c +++ b/src/admin/admin_server.c @@ -221,7 +221,7 @@ adminClientGetInfo(virNetServerClientPtr client, char *sock_addr =3D NULL; const char *attr =3D NULL; virTypedParameterPtr tmpparams =3D NULL; - virIdentityPtr identity =3D NULL; + g_autoptr(virIdentity) identity =3D NULL; int rc; =20 virCheckFlags(0, -1); @@ -310,7 +310,6 @@ adminClientGetInfo(virNetServerClientPtr client, ret =3D 0; =20 cleanup: - virObjectUnref(identity); VIR_FREE(sock_addr); return ret; } diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c index a50cd54393..872750ff4d 100644 --- a/src/qemu/qemu_process.c +++ b/src/qemu/qemu_process.c @@ -8051,7 +8051,7 @@ qemuProcessReconnect(void *opaque) bool tryMonReconn =3D false; =20 virIdentitySetCurrent(data->identity); - virObjectUnref(data->identity); + g_clear_object(&data->identity); VIR_FREE(data); =20 qemuDomainObjRestoreJob(obj, &oldjob); @@ -8364,7 +8364,7 @@ qemuProcessReconnectHelper(virDomainObjPtr obj, =20 virDomainObjEndAPI(&obj); virNWFilterUnlockFilterUpdates(); - virObjectUnref(data->identity); + g_clear_object(&data->identity); VIR_FREE(data); return -1; } diff --git a/src/remote/remote_daemon.c b/src/remote/remote_daemon.c index 9281e226c4..7fcaa31c49 100644 --- a/src/remote/remote_daemon.c +++ b/src/remote/remote_daemon.c @@ -826,7 +826,7 @@ handleSystemMessageFunc(DBusConnection *connection ATTR= IBUTE_UNUSED, static void daemonRunStateInit(void *opaque) { virNetDaemonPtr dmn =3D opaque; - virIdentityPtr sysident =3D virIdentityGetSystem(); + g_autoptr(virIdentity) sysident =3D virIdentityGetSystem(); #ifdef MODULE_NAME bool mandatory =3D true; #else /* ! MODULE_NAME */ @@ -879,7 +879,6 @@ static void daemonRunStateInit(void *opaque) cleanup: daemonInhibitCallback(false, dmn); virObjectUnref(dmn); - virObjectUnref(sysident); virIdentitySetCurrent(NULL); } =20 diff --git a/src/remote/remote_daemon_dispatch.c b/src/remote/remote_daemon= _dispatch.c index dbd2985c38..9fc5dc3998 100644 --- a/src/remote/remote_daemon_dispatch.c +++ b/src/remote/remote_daemon_dispatch.c @@ -159,7 +159,7 @@ remoteRelayDomainEventCheckACL(virNetServerClientPtr cl= ient, virConnectPtr conn, virDomainPtr dom) { virDomainDef def; - virIdentityPtr identity =3D NULL; + g_autoptr(virIdentity) identity =3D NULL; bool ret =3D false; =20 /* For now, we just create a virDomainDef with enough contents to @@ -177,7 +177,6 @@ remoteRelayDomainEventCheckACL(virNetServerClientPtr cl= ient, =20 cleanup: ignore_value(virIdentitySetCurrent(NULL)); - virObjectUnref(identity); return ret; } =20 @@ -187,7 +186,7 @@ remoteRelayNetworkEventCheckACL(virNetServerClientPtr c= lient, virConnectPtr conn, virNetworkPtr net) { virNetworkDef def; - virIdentityPtr identity =3D NULL; + g_autoptr(virIdentity) identity =3D NULL; bool ret =3D false; =20 /* For now, we just create a virNetworkDef with enough contents to @@ -204,7 +203,6 @@ remoteRelayNetworkEventCheckACL(virNetServerClientPtr c= lient, =20 cleanup: ignore_value(virIdentitySetCurrent(NULL)); - virObjectUnref(identity); return ret; } =20 @@ -214,7 +212,7 @@ remoteRelayStoragePoolEventCheckACL(virNetServerClientP= tr client, virStoragePoolPtr pool) { virStoragePoolDef def; - virIdentityPtr identity =3D NULL; + g_autoptr(virIdentity) identity =3D NULL; bool ret =3D false; =20 /* For now, we just create a virStoragePoolDef with enough contents to @@ -231,7 +229,6 @@ remoteRelayStoragePoolEventCheckACL(virNetServerClientP= tr client, =20 cleanup: ignore_value(virIdentitySetCurrent(NULL)); - virObjectUnref(identity); return ret; } =20 @@ -241,7 +238,7 @@ remoteRelayNodeDeviceEventCheckACL(virNetServerClientPt= r client, virNodeDevicePtr dev) { virNodeDeviceDef def; - virIdentityPtr identity =3D NULL; + g_autoptr(virIdentity) identity =3D NULL; bool ret =3D false; =20 /* For now, we just create a virNodeDeviceDef with enough contents to @@ -257,7 +254,6 @@ remoteRelayNodeDeviceEventCheckACL(virNetServerClientPt= r client, =20 cleanup: ignore_value(virIdentitySetCurrent(NULL)); - virObjectUnref(identity); return ret; } =20 @@ -267,7 +263,7 @@ remoteRelaySecretEventCheckACL(virNetServerClientPtr cl= ient, virSecretPtr secret) { virSecretDef def; - virIdentityPtr identity =3D NULL; + g_autoptr(virIdentity) identity =3D NULL; bool ret =3D false; =20 /* For now, we just create a virSecretDef with enough contents to @@ -285,7 +281,6 @@ remoteRelaySecretEventCheckACL(virNetServerClientPtr cl= ient, =20 cleanup: ignore_value(virIdentitySetCurrent(NULL)); - virObjectUnref(identity); return ret; } =20 @@ -294,7 +289,7 @@ remoteRelayDomainQemuMonitorEventCheckACL(virNetServerC= lientPtr client, virConnectPtr conn, virDomainPtr= dom) { virDomainDef def; - virIdentityPtr identity =3D NULL; + g_autoptr(virIdentity) identity =3D NULL; bool ret =3D false; =20 /* For now, we just create a virDomainDef with enough contents to @@ -311,7 +306,6 @@ remoteRelayDomainQemuMonitorEventCheckACL(virNetServerC= lientPtr client, =20 cleanup: ignore_value(virIdentitySetCurrent(NULL)); - virObjectUnref(identity); return ret; } =20 @@ -1869,7 +1863,7 @@ void remoteRelayConnectionClosedEvent(virConnectPtr c= onn ATTRIBUTE_UNUSED, int r static void remoteClientFreePrivateCallbacks(struct daemonClientPrivate *priv) { - virIdentityPtr sysident =3D virIdentityGetSystem(); + g_autoptr(virIdentity) sysident =3D virIdentityGetSystem(); virIdentitySetCurrent(sysident); =20 DEREG_CB(priv->conn, priv->domainEventCallbacks, @@ -1898,7 +1892,6 @@ remoteClientFreePrivateCallbacks(struct daemonClientP= rivate *priv) } =20 virIdentitySetCurrent(NULL); - virObjectUnref(sysident); } #undef DEREG_CB =20 @@ -1965,7 +1958,7 @@ remoteOpenConn(const char *uri, } =20 if (preserveIdentity) { - VIR_AUTOUNREF(virIdentityPtr) ident =3D NULL; + g_autoptr(virIdentity) ident =3D NULL; =20 if (!(ident =3D virIdentityGetCurrent())) return -1; @@ -2436,7 +2429,7 @@ remoteDispatchConnectSetIdentity(virNetServerPtr serv= er ATTRIBUTE_UNUSED, int nparams =3D 0; int rv =3D -1; virConnectPtr conn =3D remoteGetHypervisorConn(client); - VIR_AUTOUNREF(virIdentityPtr) ident =3D NULL; + g_autoptr(virIdentity) ident =3D NULL; if (!conn) goto cleanup; =20 @@ -3982,7 +3975,7 @@ static int remoteSASLFinish(virNetServerPtr server, virNetServerClientPtr client) { - virIdentityPtr clnt_identity =3D NULL; + g_autoptr(virIdentity) clnt_identity =3D NULL; const char *identity; struct daemonClientPrivate *priv =3D virNetServerClientGetPrivateData(= client); int ssf; @@ -3990,7 +3983,7 @@ remoteSASLFinish(virNetServerPtr server, /* TLS or UNIX domain sockets trivially OK */ if (!virNetServerClientIsSecure(client)) { if ((ssf =3D virNetSASLSessionGetKeySize(priv->sasl)) < 0) - goto error; + return -1; =20 VIR_DEBUG("negotiated an SSF of %d", ssf); if (ssf < 56) { /* 56 is good for Kerberos */ @@ -4006,7 +3999,7 @@ remoteSASLFinish(virNetServerPtr server, return -2; =20 if (!(clnt_identity =3D virNetServerClientGetIdentity(client))) - goto error; + return -1; =20 virNetServerSetClientAuthenticated(server, client); virNetServerClientSetSASLSession(client, priv->sasl); @@ -4018,14 +4011,10 @@ remoteSASLFinish(virNetServerPtr server, "client=3D%p auth=3D%d identity=3D%s", client, REMOTE_AUTH_SASL, identity); =20 - virObjectUnref(clnt_identity); virObjectUnref(priv->sasl); priv->sasl =3D NULL; =20 return 0; - - error: - return -1; } =20 /* diff --git a/src/rpc/virnetserverclient.c b/src/rpc/virnetserverclient.c index 171ee636dd..8482c5c29c 100644 --- a/src/rpc/virnetserverclient.c +++ b/src/rpc/virnetserverclient.c @@ -759,13 +759,13 @@ int virNetServerClientGetUNIXIdentity(virNetServerCli= entPtr client, static virIdentityPtr virNetServerClientCreateIdentity(virNetServerClientPtr client) { - char *username =3D NULL; - char *groupname =3D NULL; - char *seccontext =3D NULL; - virIdentityPtr ret =3D NULL; + g_autofree char *username =3D NULL; + g_autofree char *groupname =3D NULL; + g_autofree char *seccontext =3D NULL; + g_autoptr(virIdentity) ret =3D NULL; =20 if (!(ret =3D virIdentityNew())) - goto error; + return NULL; =20 if (client->sock && virNetSocketIsLocal(client->sock)) { gid_t gid; @@ -775,59 +775,50 @@ virNetServerClientCreateIdentity(virNetServerClientPt= r client) if (virNetSocketGetUNIXIdentity(client->sock, &uid, &gid, &pid, ×tamp) < 0) - goto error; + return NULL; =20 if (!(username =3D virGetUserName(uid))) - goto error; + return NULL; if (virIdentitySetUserName(ret, username) < 0) - goto error; + return NULL; if (virIdentitySetUNIXUserID(ret, uid) < 0) - goto error; + return NULL; =20 if (!(groupname =3D virGetGroupName(gid))) - goto error; + return NULL; if (virIdentitySetGroupName(ret, groupname) < 0) - goto error; + return NULL; if (virIdentitySetUNIXGroupID(ret, gid) < 0) - goto error; + return NULL; =20 if (virIdentitySetProcessID(ret, pid) < 0) - goto error; + return NULL; if (virIdentitySetProcessTime(ret, timestamp) < 0) - goto error; + return NULL; } =20 #if WITH_SASL if (client->sasl) { const char *identity =3D virNetSASLSessionGetIdentity(client->sasl= ); if (virIdentitySetSASLUserName(ret, identity) < 0) - goto error; + return NULL; } #endif =20 if (client->tls) { const char *identity =3D virNetTLSSessionGetX509DName(client->tls); if (virIdentitySetX509DName(ret, identity) < 0) - goto error; + return NULL; } =20 if (client->sock && virNetSocketGetSELinuxContext(client->sock, &seccontext) < 0) - goto error; + return NULL; if (seccontext && virIdentitySetSELinuxContext(ret, seccontext) < 0) - goto error; - - cleanup: - VIR_FREE(username); - VIR_FREE(groupname); - VIR_FREE(seccontext); - return ret; + return NULL; =20 - error: - virObjectUnref(ret); - ret =3D NULL; - goto cleanup; + return g_steal_pointer(&ret); } =20 =20 @@ -838,7 +829,7 @@ virIdentityPtr virNetServerClientGetIdentity(virNetServ= erClientPtr client) if (!client->identity) client->identity =3D virNetServerClientCreateIdentity(client); if (client->identity) - ret =3D virObjectRef(client->identity); + ret =3D g_object_ref(client->identity); virObjectUnlock(client); return ret; } @@ -848,10 +839,10 @@ void virNetServerClientSetIdentity(virNetServerClient= Ptr client, virIdentityPtr identity) { virObjectLock(client); - virObjectUnref(client->identity); + g_clear_object(&client->identity); client->identity =3D identity; if (client->identity) - virObjectRef(client->identity); + g_object_ref(client->identity); virObjectUnlock(client); } =20 @@ -988,7 +979,7 @@ void virNetServerClientDispose(void *obj) if (client->privateData) client->privateDataFreeFunc(client->privateData); =20 - virObjectUnref(client->identity); + g_clear_object(&client->identity); =20 #if WITH_SASL virObjectUnref(client->sasl); @@ -1683,7 +1674,7 @@ virNetServerClientGetInfo(virNetServerClientPtr clien= t, goto cleanup; } =20 - *identity =3D virObjectRef(client->identity); + *identity =3D g_object_ref(client->identity); =20 ret =3D 0; cleanup: diff --git a/src/rpc/virnetserverprogram.c b/src/rpc/virnetserverprogram.c index 7ae1d2e955..cca820ca5a 100644 --- a/src/rpc/virnetserverprogram.c +++ b/src/rpc/virnetserverprogram.c @@ -370,13 +370,13 @@ virNetServerProgramDispatchCall(virNetServerProgramPt= r prog, virNetServerClientPtr client, virNetMessagePtr msg) { - char *arg =3D NULL; - char *ret =3D NULL; + g_autofree char *arg =3D NULL; + g_autofree char *ret =3D NULL; int rv =3D -1; virNetServerProgramProcPtr dispatcher; virNetMessageError rerr; size_t i; - virIdentityPtr identity =3D NULL; + g_autoptr(virIdentity) identity =3D NULL; =20 memset(&rerr, 0, sizeof(rerr)); =20 @@ -484,10 +484,7 @@ virNetServerProgramDispatchCall(virNetServerProgramPtr= prog, } =20 xdr_free(dispatcher->ret_filter, ret); - VIR_FREE(arg); - VIR_FREE(ret); =20 - virObjectUnref(identity); /* Put reply on end of tx queue to send out */ return virNetServerClientSendMessage(client, msg); =20 @@ -496,10 +493,6 @@ virNetServerProgramDispatchCall(virNetServerProgramPtr= prog, * RPC error message we can send back to the client */ rv =3D virNetServerProgramSendReplyError(prog, client, msg, &rerr, &ms= g->header); =20 - VIR_FREE(arg); - VIR_FREE(ret); - virObjectUnref(identity); - return rv; } =20 diff --git a/src/util/viridentity.c b/src/util/viridentity.c index 22e2644c19..467d953e17 100644 --- a/src/util/viridentity.c +++ b/src/util/viridentity.c @@ -43,25 +43,29 @@ VIR_LOG_INIT("util.identity"); =20 struct _virIdentity { - virObject parent; + GObject parent; =20 int nparams; int maxparams; virTypedParameterPtr params; }; =20 -static virClassPtr virIdentityClass; +G_DEFINE_TYPE(virIdentity, virIdentity, G_TYPE_OBJECT) + static virThreadLocal virIdentityCurrent; =20 -static void virIdentityDispose(void *obj); +static void virIdentityDispose(GObject *obj); =20 -static int virIdentityOnceInit(void) +static void virIdentityCurrentCleanup(void *ident) { - if (!VIR_CLASS_NEW(virIdentity, virClassForObject())) - return -1; + if (ident) + g_object_unref(ident); +} =20 +static int virIdentityOnceInit(void) +{ if (virThreadLocalInit(&virIdentityCurrent, - (virThreadLocalCleanup)virObjectUnref) < 0) { + virIdentityCurrentCleanup) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Cannot initialize thread local for current ident= ity")); return -1; @@ -72,13 +76,24 @@ static int virIdentityOnceInit(void) =20 VIR_ONCE_GLOBAL_INIT(virIdentity); =20 +static void virIdentity_init(virIdentity *ident G_GNUC_UNUSED) +{ +} + +static void virIdentity_class_init(virIdentityClass *klass) +{ + GObjectClass *obj =3D G_OBJECT_CLASS(klass); + + obj->finalize =3D virIdentityDispose; +} + /** * virIdentityGetCurrent: * * Get the current identity associated with this thread. The * caller will own a reference to the returned identity, but * must not modify the object in any way, other than to - * release the reference when done with virObjectUnref + * release the reference when done with g_object_unref * * Returns: a reference to the current identity, or NULL */ @@ -90,7 +105,9 @@ virIdentityPtr virIdentityGetCurrent(void) return NULL; =20 ident =3D virThreadLocalGet(&virIdentityCurrent); - return virObjectRef(ident); + if (ident) + g_object_ref(ident); + return ident; } =20 =20 @@ -105,7 +122,7 @@ virIdentityPtr virIdentityGetCurrent(void) */ int virIdentitySetCurrent(virIdentityPtr ident) { - virIdentityPtr old; + g_autoptr(virIdentity) old =3D NULL; =20 if (virIdentityInitialize() < 0) return -1; @@ -113,15 +130,14 @@ int virIdentitySetCurrent(virIdentityPtr ident) old =3D virThreadLocalGet(&virIdentityCurrent); =20 if (virThreadLocalSet(&virIdentityCurrent, - virObjectRef(ident)) < 0) { + ident ? g_object_ref(ident) : NULL) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Unable to set thread local identity")); - virObjectUnref(ident); + if (ident) + g_object_unref(ident); return -1; } =20 - virObjectUnref(old); - return 0; } =20 @@ -139,36 +155,36 @@ virIdentityPtr virIdentityGetSystem(void) VIR_AUTOFREE(char *) username =3D NULL; VIR_AUTOFREE(char *) groupname =3D NULL; unsigned long long startTime; - virIdentityPtr ret =3D NULL; + g_autoptr(virIdentity) ret =3D NULL; #if WITH_SELINUX security_context_t con; #endif =20 if (!(ret =3D virIdentityNew())) - goto error; + return NULL; =20 if (virIdentitySetProcessID(ret, getpid()) < 0) - goto error; + return NULL; =20 if (virProcessGetStartTime(getpid(), &startTime) < 0) - goto error; + return NULL; if (startTime !=3D 0 && virIdentitySetProcessTime(ret, startTime) < 0) - goto error; + return NULL; =20 if (!(username =3D virGetUserName(geteuid()))) return ret; if (virIdentitySetUserName(ret, username) < 0) - goto error; + return NULL; if (virIdentitySetUNIXUserID(ret, getuid()) < 0) - goto error; + return NULL; =20 if (!(groupname =3D virGetGroupName(getegid()))) return ret; if (virIdentitySetGroupName(ret, groupname) < 0) - goto error; + return NULL; if (virIdentitySetUNIXGroupID(ret, getgid()) < 0) - goto error; + return NULL; =20 #if WITH_SELINUX if (is_selinux_enabled() > 0) { @@ -179,17 +195,13 @@ virIdentityPtr virIdentityGetSystem(void) } if (virIdentitySetSELinuxContext(ret, con) < 0) { freecon(con); - goto error; + return NULL; } freecon(con); } #endif =20 - return ret; - - error: - virObjectUnref(ret); - return NULL; + return g_steal_pointer(&ret); } =20 =20 @@ -203,26 +215,21 @@ virIdentityPtr virIdentityGetSystem(void) */ virIdentityPtr virIdentityNew(void) { - virIdentityPtr ident; - - if (virIdentityInitialize() < 0) - return NULL; - - if (!(ident =3D virObjectNew(virIdentityClass))) - return NULL; - - return ident; + return VIR_IDENTITY(g_object_new(VIR_TYPE_IDENTITY, NULL)); } =20 =20 -static void virIdentityDispose(void *object) +static void virIdentityDispose(GObject *object) { - virIdentityPtr ident =3D object; + virIdentityPtr ident =3D VIR_IDENTITY(object); =20 virTypedParamsFree(ident->params, ident->nparams); + + G_OBJECT_CLASS(virIdentity_parent_class)->finalize(object); } =20 =20 + /* * Returns: 0 if not present, 1 if present, -1 on error */ diff --git a/src/util/viridentity.h b/src/util/viridentity.h index 861ecca736..b63a53e6c4 100644 --- a/src/util/viridentity.h +++ b/src/util/viridentity.h @@ -21,9 +21,12 @@ =20 #pragma once =20 -#include "virobject.h" +#include "internal.h" +#include + +#define VIR_TYPE_IDENTITY virIdentity_get_type () +G_DECLARE_FINAL_TYPE (virIdentity, virIdentity, VIR, IDENTITY, GObject); =20 -typedef struct _virIdentity virIdentity; typedef virIdentity *virIdentityPtr; =20 virIdentityPtr virIdentityGetCurrent(void); diff --git a/tests/viridentitytest.c b/tests/viridentitytest.c index 1eadd6173a..81db049fcb 100644 --- a/tests/viridentitytest.c +++ b/tests/viridentitytest.c @@ -38,58 +38,52 @@ VIR_LOG_INIT("tests.identitytest"); =20 static int testIdentityAttrs(const void *data ATTRIBUTE_UNUSED) { - int ret =3D -1; - virIdentityPtr ident; + g_autoptr(virIdentity) ident =3D NULL; const char *val; int rc; =20 - if (!(ident =3D virIdentityNew())) - goto cleanup; + ident =3D virIdentityNew(); =20 if (virIdentitySetUserName(ident, "fred") < 0) - goto cleanup; + return -1; =20 if ((rc =3D virIdentityGetUserName(ident, &val)) < 0) - goto cleanup; + return -1; =20 if (STRNEQ_NULLABLE(val, "fred") || rc !=3D 1) { VIR_DEBUG("Expected 'fred' got '%s'", NULLSTR(val)); - goto cleanup; + return -1; } =20 if ((rc =3D virIdentityGetGroupName(ident, &val)) < 0) - goto cleanup; + return -1; =20 if (val !=3D NULL || rc !=3D 0) { VIR_DEBUG("Unexpected groupname attribute"); - goto cleanup; + return -1; } =20 if (virIdentitySetUserName(ident, "joe") >=3D 0) { VIR_DEBUG("Unexpectedly overwrote attribute"); - goto cleanup; + return -1; } =20 if ((rc =3D virIdentityGetUserName(ident, &val)) < 0) - goto cleanup; + return -1; =20 if (STRNEQ_NULLABLE(val, "fred") || rc !=3D 1) { VIR_DEBUG("Expected 'fred' got '%s'", NULLSTR(val)); - goto cleanup; + return -1; } =20 - ret =3D 0; - cleanup: - virObjectUnref(ident); - return ret; + return 0; } =20 =20 static int testIdentityGetSystem(const void *data) { const char *context =3D data; - int ret =3D -1; - virIdentityPtr ident =3D NULL; + g_autoptr(virIdentity) ident =3D NULL; const char *val; int rc; =20 @@ -97,35 +91,32 @@ static int testIdentityGetSystem(const void *data) if (context) { VIR_DEBUG("libvirt not compiled with SELinux, skipping this test"); ret =3D EXIT_AM_SKIP; - goto cleanup; + return -1; } #endif =20 if (!(ident =3D virIdentityGetSystem())) { VIR_DEBUG("Unable to get system identity"); - goto cleanup; + return -1; } =20 if ((rc =3D virIdentityGetSELinuxContext(ident, &val)) < 0) - goto cleanup; + return -1; =20 if (context =3D=3D NULL) { if (val !=3D NULL || rc !=3D 0) { VIR_DEBUG("Unexpected SELinux context %s", NULLSTR(val)); - goto cleanup; + return -1; } } else { if (STRNEQ_NULLABLE(val, context) || rc !=3D 1) { VIR_DEBUG("Want SELinux context '%s' got '%s'", context, val); - goto cleanup; + return -1; } } =20 - ret =3D 0; - cleanup: - virObjectUnref(ident); - return ret; + return 0; } =20 static int testSetFakeSELinuxContext(const void *data ATTRIBUTE_UNUSED) diff --git a/tests/virnetserverclienttest.c b/tests/virnetserverclienttest.c index d094de9840..42393d7dbe 100644 --- a/tests/virnetserverclienttest.c +++ b/tests/virnetserverclienttest.c @@ -51,7 +51,7 @@ static int testIdentity(const void *opaque ATTRIBUTE_UNUS= ED) int ret =3D -1; virNetSocketPtr sock =3D NULL; virNetServerClientPtr client =3D NULL; - virIdentityPtr ident =3D NULL; + g_autoptr(virIdentity) ident =3D NULL; const char *gotUsername =3D NULL; uid_t gotUserID; const char *gotGroupname =3D NULL; @@ -141,7 +141,6 @@ static int testIdentity(const void *opaque ATTRIBUTE_UN= USED) if (client) virNetServerClientClose(client); virObjectUnref(client); - virObjectUnref(ident); VIR_FORCE_CLOSE(sv[0]); VIR_FORCE_CLOSE(sv[1]); return ret; --=20 2.21.0 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list