From nobody Sun Apr 28 00:38:29 2024 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 Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1501259951742321.9527584154557; Fri, 28 Jul 2017 09:39:11 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 70B35C08EA46; Fri, 28 Jul 2017 16:39:09 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 3F84853CFB; Fri, 28 Jul 2017 16:39:09 +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 A0AFB1805977; Fri, 28 Jul 2017 16:39:08 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v6SGd5pf010331 for ; Fri, 28 Jul 2017 12:39:05 -0400 Received: by smtp.corp.redhat.com (Postfix) id DC22F5D759; Fri, 28 Jul 2017 16:39:05 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-117-46.phx2.redhat.com [10.3.117.46]) by smtp.corp.redhat.com (Postfix) with ESMTP id A158E5D747 for ; Fri, 28 Jul 2017 16:39:05 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 70B35C08EA46 Authentication-Results: ext-mx07.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx07.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=libvir-list-bounces@redhat.com From: John Ferlan To: libvir-list@redhat.com Date: Fri, 28 Jul 2017 12:38:55 -0400 Message-Id: <20170728163901.21444-2-jferlan@redhat.com> In-Reply-To: <20170728163901.21444-1-jferlan@redhat.com> References: <20170728163901.21444-1-jferlan@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 1/7] util: Alter virObjectLockRead to return status 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: , MIME-Version: 1.0 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.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Fri, 28 Jul 2017 16:39:10 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Rather than ignore errors, let's have virObjectLockRead check for the correct usage and issue an error when not properly used so so that we don't run into situations where the resource we think we're locking really isn't locked because the void input parameter wasn't valid. Signed-off-by: John Ferlan --- src/conf/virdomainobjlist.c | 27 ++++++++++++++++++--------- src/util/virobject.c | 6 +++++- src/util/virobject.h | 2 +- 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/conf/virdomainobjlist.c b/src/conf/virdomainobjlist.c index 1d027a4..fed4029 100644 --- a/src/conf/virdomainobjlist.c +++ b/src/conf/virdomainobjlist.c @@ -118,7 +118,8 @@ virDomainObjListFindByIDInternal(virDomainObjListPtr do= ms, bool ref) { virDomainObjPtr obj; - virObjectLockRead(doms); + if (virObjectLockRead(doms) < 0) + return NULL; obj =3D virHashSearch(doms->objs, virDomainObjListSearchID, &id, NULL); if (ref) { virObjectRef(obj); @@ -160,7 +161,8 @@ virDomainObjListFindByUUIDInternal(virDomainObjListPtr = doms, char uuidstr[VIR_UUID_STRING_BUFLEN]; virDomainObjPtr obj; =20 - virObjectLockRead(doms); + if (virObjectLockRead(doms) < 0) + return NULL; virUUIDFormat(uuid, uuidstr); =20 obj =3D virHashLookup(doms->objs, uuidstr); @@ -204,7 +206,8 @@ virDomainObjPtr virDomainObjListFindByName(virDomainObj= ListPtr doms, { virDomainObjPtr obj; =20 - virObjectLockRead(doms); + if (virObjectLockRead(doms) < 0) + return NULL; obj =3D virHashLookup(doms->objsName, name); virObjectRef(obj); virObjectUnlock(doms); @@ -653,7 +656,8 @@ virDomainObjListNumOfDomains(virDomainObjListPtr doms, virConnectPtr conn) { struct virDomainObjListData data =3D { filter, conn, active, 0 }; - virObjectLockRead(doms); + if (virObjectLockRead(doms) < 0) + return -1; virHashForEach(doms->objs, virDomainObjListCount, &data); virObjectUnlock(doms); return data.count; @@ -697,7 +701,8 @@ virDomainObjListGetActiveIDs(virDomainObjListPtr doms, { struct virDomainIDData data =3D { filter, conn, 0, maxids, ids }; - virObjectLockRead(doms); + if (virObjectLockRead(doms) < 0) + return -1; virHashForEach(doms->objs, virDomainObjListCopyActiveIDs, &data); virObjectUnlock(doms); return data.numids; @@ -751,7 +756,8 @@ virDomainObjListGetInactiveNames(virDomainObjListPtr do= ms, struct virDomainNameData data =3D { filter, conn, 0, 0, maxnames, names }; size_t i; - virObjectLockRead(doms); + if (virObjectLockRead(doms) < 0) + return -1; virHashForEach(doms->objs, virDomainObjListCopyInactiveNames, &data); virObjectUnlock(doms); if (data.oom) { @@ -792,7 +798,8 @@ virDomainObjListForEach(virDomainObjListPtr doms, struct virDomainListIterData data =3D { callback, opaque, 0, }; - virObjectLockRead(doms); + if (virObjectLockRead(doms) < 0) + return -1; virHashForEach(doms->objs, virDomainObjListHelper, &data); virObjectUnlock(doms); return data.ret; @@ -925,7 +932,8 @@ virDomainObjListCollect(virDomainObjListPtr domlist, { struct virDomainListData data =3D { NULL, 0 }; =20 - virObjectLockRead(domlist); + if (virObjectLockRead(domlist) < 0) + return -1; sa_assert(domlist->objs); if (VIR_ALLOC_N(data.vms, virHashSize(domlist->objs)) < 0) { virObjectUnlock(domlist); @@ -962,7 +970,8 @@ virDomainObjListConvert(virDomainObjListPtr domlist, *nvms =3D 0; *vms =3D NULL; =20 - virObjectLockRead(domlist); + if (virObjectLockRead(domlist) < 0) + return -1; for (i =3D 0; i < ndoms; i++) { virDomainPtr dom =3D doms[i]; =20 diff --git a/src/util/virobject.c b/src/util/virobject.c index b1bb378..73de4d3 100644 --- a/src/util/virobject.c +++ b/src/util/virobject.c @@ -410,17 +410,21 @@ virObjectLock(void *anyobj) * The object must be unlocked before releasing this * reference. */ -void +int virObjectLockRead(void *anyobj) { if (virObjectIsClass(anyobj, virObjectRWLockableClass)) { virObjectRWLockablePtr obj =3D anyobj; virRWLockRead(&obj->lock); + return 0; } else { virObjectPtr obj =3D anyobj; VIR_WARN("Object %p (%s) is not a virObjectRWLockable instance", anyobj, obj ? obj->klass->name : "(unknown)"); + virReportError(VIR_ERR_INTERNAL_ERROR, + _("unable to obtain rwlock for object=3D%p"), anyob= j); } + return -1; } =20 =20 diff --git a/src/util/virobject.h b/src/util/virobject.h index 5985fad..99910e0 100644 --- a/src/util/virobject.h +++ b/src/util/virobject.h @@ -124,7 +124,7 @@ void virObjectLock(void *lockableobj) ATTRIBUTE_NONNULL(1); =20 -void +int virObjectLockRead(void *lockableobj) ATTRIBUTE_NONNULL(1); =20 --=20 2.9.4 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Sun Apr 28 00:38:29 2024 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 Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1501259961529520.8676776258937; Fri, 28 Jul 2017 09:39:21 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 4BE0263772; Fri, 28 Jul 2017 16:39:18 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id EE6DB7E2F4; Fri, 28 Jul 2017 16:39:17 +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 9BF02180B467; Fri, 28 Jul 2017 16:39:17 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v6SGd6Yq010336 for ; Fri, 28 Jul 2017 12:39:06 -0400 Received: by smtp.corp.redhat.com (Postfix) id 3D4E25D747; Fri, 28 Jul 2017 16:39:06 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-117-46.phx2.redhat.com [10.3.117.46]) by smtp.corp.redhat.com (Postfix) with ESMTP id 063EC5E254 for ; Fri, 28 Jul 2017 16:39:05 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 4BE0263772 Authentication-Results: ext-mx06.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx06.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=libvir-list-bounces@redhat.com From: John Ferlan To: libvir-list@redhat.com Date: Fri, 28 Jul 2017 12:38:56 -0400 Message-Id: <20170728163901.21444-3-jferlan@redhat.com> In-Reply-To: <20170728163901.21444-1-jferlan@redhat.com> References: <20170728163901.21444-1-jferlan@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 2/7] util: Introduce and use virObjectLockWrite 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: , MIME-Version: 1.0 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.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Fri, 28 Jul 2017 16:39:19 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Instead of making virObjectLock be the entry point for two different types of locks, let's create a virObjectLockWrite API which will be able to return status and force the (new) consumers of the RWLock to make sure the lock is really obtained when the "right" object type is passed. Signed-off-by: John Ferlan --- src/conf/virdomainobjlist.c | 18 ++++++++++++++---- src/libvirt_private.syms | 1 + src/util/virobject.c | 32 ++++++++++++++++++++++++++++++++ src/util/virobject.h | 4 ++++ 4 files changed, 51 insertions(+), 4 deletions(-) diff --git a/src/conf/virdomainobjlist.c b/src/conf/virdomainobjlist.c index fed4029..08a51cc 100644 --- a/src/conf/virdomainobjlist.c +++ b/src/conf/virdomainobjlist.c @@ -330,7 +330,8 @@ virDomainObjPtr virDomainObjListAdd(virDomainObjListPtr= doms, { virDomainObjPtr ret; =20 - virObjectLock(doms); + if (virObjectLockWrite(doms) < 0) + return NULL; ret =3D virDomainObjListAddLocked(doms, def, xmlopt, flags, oldDef); virObjectUnlock(doms); return ret; @@ -352,7 +353,9 @@ void virDomainObjListRemove(virDomainObjListPtr doms, virObjectRef(dom); virObjectUnlock(dom); =20 - virObjectLock(doms); + /* We really shouldn't ignore this, + * but that ship sailed a long time ago */ + ignore_value(virObjectLockWrite(doms)); virObjectLock(dom); virHashRemoveEntry(doms->objs, uuidstr); virHashRemoveEntry(doms->objsName, dom->def->name); @@ -397,9 +400,13 @@ virDomainObjListRename(virDomainObjListPtr doms, * hold a lock on dom but not refcount it. */ virObjectRef(dom); virObjectUnlock(dom); - virObjectLock(doms); + rc =3D virObjectLockWrite(doms); virObjectLock(dom); virObjectUnref(dom); + if (rc < 0) { + VIR_FREE(old_name); + return ret; + } =20 if (virHashLookup(doms->objsName, new_name) !=3D NULL) { virReportError(VIR_ERR_OPERATION_INVALID, @@ -576,7 +583,10 @@ virDomainObjListLoadAllConfigs(virDomainObjListPtr dom= s, if ((rc =3D virDirOpenIfExists(&dir, configDir)) <=3D 0) return rc; =20 - virObjectLock(doms); + if (virObjectLockWrite(doms) < 0) { + VIR_DIR_CLOSE(dir); + return -1; + } =20 while ((ret =3D virDirRead(dir, &entry, configDir)) > 0) { virDomainObjPtr dom; diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 37b815c..f1a6510 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2306,6 +2306,7 @@ virObjectListFreeCount; virObjectLock; virObjectLockableNew; virObjectLockRead; +virObjectLockWrite; virObjectNew; virObjectRef; virObjectRWLockableNew; diff --git a/src/util/virobject.c b/src/util/virobject.c index 73de4d3..c48f88c 100644 --- a/src/util/virobject.c +++ b/src/util/virobject.c @@ -429,6 +429,38 @@ virObjectLockRead(void *anyobj) =20 =20 /** + * virObjectLockWrite: + * @anyobj: any instance of virObjectRWLockable + * + * Acquire a write lock on @anyobj. The lock must be + * released by virObjectUnlock. + * + * The caller is expected to have acquired a reference + * on the object before locking it (eg virObjectRef). + * The object must be unlocked before releasing this + * reference. + * + * Returns 0 on success, -1 on failure + */ +int +virObjectLockWrite(void *anyobj) +{ + if (virObjectIsClass(anyobj, virObjectRWLockableClass)) { + virObjectRWLockablePtr obj =3D anyobj; + virRWLockWrite(&obj->lock); + return 0; + } else { + virObjectPtr obj =3D anyobj; + VIR_WARN("Object %p (%s) is not a virObjectRWLockable instance", + anyobj, obj ? obj->klass->name : "(unknown)"); + virReportError(VIR_ERR_INTERNAL_ERROR, + _("unable to obtain rwlock for object=3D%p"), anyob= j); + } + return -1; +} + + +/** * virObjectUnlock: * @anyobj: any instance of virObjectLockable or virObjectRWLockable * diff --git a/src/util/virobject.h b/src/util/virobject.h index 99910e0..f0d1f97 100644 --- a/src/util/virobject.h +++ b/src/util/virobject.h @@ -128,6 +128,10 @@ int virObjectLockRead(void *lockableobj) ATTRIBUTE_NONNULL(1); =20 +int +virObjectLockWrite(void *lockableobj) + ATTRIBUTE_NONNULL(1); + void virObjectUnlock(void *lockableobj) ATTRIBUTE_NONNULL(1); --=20 2.9.4 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Sun Apr 28 00:38:29 2024 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 Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1501259965161410.9783102724325; Fri, 28 Jul 2017 09:39:25 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 5B1E1A641D; Fri, 28 Jul 2017 16:39:21 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 359896F458; Fri, 28 Jul 2017 16:39:21 +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 DFB10180BA80; Fri, 28 Jul 2017 16:39:20 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v6SGd6gX010344 for ; Fri, 28 Jul 2017 12:39:06 -0400 Received: by smtp.corp.redhat.com (Postfix) id A244F5D747; Fri, 28 Jul 2017 16:39:06 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-117-46.phx2.redhat.com [10.3.117.46]) by smtp.corp.redhat.com (Postfix) with ESMTP id 67A1E5D759 for ; Fri, 28 Jul 2017 16:39:06 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 5B1E1A641D Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=libvir-list-bounces@redhat.com From: John Ferlan To: libvir-list@redhat.com Date: Fri, 28 Jul 2017 12:38:57 -0400 Message-Id: <20170728163901.21444-4-jferlan@redhat.com> In-Reply-To: <20170728163901.21444-1-jferlan@redhat.com> References: <20170728163901.21444-1-jferlan@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 3/7] util: Only have virObjectLock handle virObjectLockable 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: , MIME-Version: 1.0 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.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Fri, 28 Jul 2017 16:39:21 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Now that virObjectLockWrite exists to handle the virObjectRWLockable objects, let's restore virObjectLock to only handle virObjectLockable type locks. There still exists the possibility that the input @anyobj isn't a valid object and the resource isn't truly locked, but that also exists before commit id '77f4593b'. This also restores some logic that commit id '77f4593b' removed with respect to a common code path that commit id '10c2bb2b' had introduced as virObjectGetLockableObj. Signed-off-by: John Ferlan --- src/util/virobject.c | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/src/util/virobject.c b/src/util/virobject.c index c48f88c..f9047a1 100644 --- a/src/util/virobject.c +++ b/src/util/virobject.c @@ -367,13 +367,28 @@ virObjectRef(void *anyobj) } =20 =20 +static virObjectLockablePtr +virObjectGetLockableObj(void *anyobj) +{ + virObjectPtr obj; + + if (virObjectIsClass(anyobj, virObjectLockableClass)) + return anyobj; + + obj =3D anyobj; + VIR_WARN("Object %p (%s) is not a virObjectLockable instance", + anyobj, obj ? obj->klass->name : "(unknown)"); + + return NULL; +} + + /** * virObjectLock: * @anyobj: any instance of virObjectLockable or virObjectRWLockable * * Acquire a lock on @anyobj. The lock must be released by - * virObjectUnlock. In case the passed object is instance of - * virObjectRWLockable a write lock is acquired. + * virObjectUnlock. * * The caller is expected to have acquired a reference * on the object before locking it (eg virObjectRef). @@ -383,18 +398,12 @@ virObjectRef(void *anyobj) void virObjectLock(void *anyobj) { - if (virObjectIsClass(anyobj, virObjectLockableClass)) { - virObjectLockablePtr obj =3D anyobj; - virMutexLock(&obj->lock); - } else if (virObjectIsClass(anyobj, virObjectRWLockableClass)) { - virObjectRWLockablePtr obj =3D anyobj; - virRWLockWrite(&obj->lock); - } else { - virObjectPtr obj =3D anyobj; - VIR_WARN("Object %p (%s) is not a virObjectLockable " - "nor virObjectRWLockable instance", - anyobj, obj ? obj->klass->name : "(unknown)"); - } + virObjectLockablePtr obj =3D virObjectGetLockableObj(anyobj); + + if (!obj) + return; + + virMutexLock(&obj->lock); } =20 =20 --=20 2.9.4 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Sun Apr 28 00:38:29 2024 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 Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1501259951903956.9020142552446; Fri, 28 Jul 2017 09:39:11 -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 7278BB8BA4; Fri, 28 Jul 2017 16:39:09 +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 50B3B5D759; Fri, 28 Jul 2017 16:39:09 +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 E786E4A468; Fri, 28 Jul 2017 16:39:08 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v6SGd7Nu010349 for ; Fri, 28 Jul 2017 12:39:07 -0400 Received: by smtp.corp.redhat.com (Postfix) id 02FB15D759; Fri, 28 Jul 2017 16:39:07 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-117-46.phx2.redhat.com [10.3.117.46]) by smtp.corp.redhat.com (Postfix) with ESMTP id C0B7E5D747 for ; Fri, 28 Jul 2017 16:39:06 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 7278BB8BA4 Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=libvir-list-bounces@redhat.com From: John Ferlan To: libvir-list@redhat.com Date: Fri, 28 Jul 2017 12:38:58 -0400 Message-Id: <20170728163901.21444-5-jferlan@redhat.com> In-Reply-To: <20170728163901.21444-1-jferlan@redhat.com> References: <20170728163901.21444-1-jferlan@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 4/7] util: Introduce virObjectGetRWLockableObj 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: , MIME-Version: 1.0 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.5.16 (mx1.redhat.com [10.5.110.25]); Fri, 28 Jul 2017 16:39:10 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Introduce a helper to handle the error path more cleanly. The same as virObjectGetLockableObj. Signed-off-by: John Ferlan --- src/util/virobject.c | 51 ++++++++++++++++++++++++++++++------------------= --- 1 file changed, 30 insertions(+), 21 deletions(-) diff --git a/src/util/virobject.c b/src/util/virobject.c index f9047a1..0db98c3 100644 --- a/src/util/virobject.c +++ b/src/util/virobject.c @@ -383,6 +383,22 @@ virObjectGetLockableObj(void *anyobj) } =20 =20 +static virObjectRWLockablePtr +virObjectGetRWLockableObj(void *anyobj) +{ + virObjectPtr obj; + + if (virObjectIsClass(anyobj, virObjectRWLockableClass)) + return anyobj; + + obj =3D anyobj; + VIR_WARN("Object %p (%s) is not a virObjectRWLockable instance", + anyobj, obj ? obj->klass->name : "(unknown)"); + + return NULL; +} + + /** * virObjectLock: * @anyobj: any instance of virObjectLockable or virObjectRWLockable @@ -422,18 +438,13 @@ virObjectLock(void *anyobj) int virObjectLockRead(void *anyobj) { - if (virObjectIsClass(anyobj, virObjectRWLockableClass)) { - virObjectRWLockablePtr obj =3D anyobj; - virRWLockRead(&obj->lock); - return 0; - } else { - virObjectPtr obj =3D anyobj; - VIR_WARN("Object %p (%s) is not a virObjectRWLockable instance", - anyobj, obj ? obj->klass->name : "(unknown)"); - virReportError(VIR_ERR_INTERNAL_ERROR, - _("unable to obtain rwlock for object=3D%p"), anyob= j); - } - return -1; + virObjectRWLockablePtr obj =3D virObjectGetRWLockableObj(anyobj); + + if (!obj) + return -1; + + virRWLockRead(&obj->lock); + return 0; } =20 =20 @@ -454,18 +465,16 @@ virObjectLockRead(void *anyobj) int virObjectLockWrite(void *anyobj) { - if (virObjectIsClass(anyobj, virObjectRWLockableClass)) { - virObjectRWLockablePtr obj =3D anyobj; - virRWLockWrite(&obj->lock); - return 0; - } else { - virObjectPtr obj =3D anyobj; - VIR_WARN("Object %p (%s) is not a virObjectRWLockable instance", - anyobj, obj ? obj->klass->name : "(unknown)"); + virObjectRWLockablePtr obj =3D virObjectGetRWLockableObj(anyobj); + + if (!obj) { virReportError(VIR_ERR_INTERNAL_ERROR, _("unable to obtain rwlock for object=3D%p"), anyob= j); + return -1; } - return -1; + + virRWLockWrite(&obj->lock); + return 0; } =20 =20 --=20 2.9.4 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Sun Apr 28 00:38:29 2024 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 Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1501259962334626.0959672974101; Fri, 28 Jul 2017 09:39:22 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 493692ED72F; Fri, 28 Jul 2017 16:39:18 +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 ED62F7E2E7; Fri, 28 Jul 2017 16:39:17 +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 A1D76410A9; Fri, 28 Jul 2017 16:39:17 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v6SGd7K4010356 for ; Fri, 28 Jul 2017 12:39:07 -0400 Received: by smtp.corp.redhat.com (Postfix) id 5FF235D759; Fri, 28 Jul 2017 16:39:07 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-117-46.phx2.redhat.com [10.3.117.46]) by smtp.corp.redhat.com (Postfix) with ESMTP id 259AC5D747 for ; Fri, 28 Jul 2017 16:39:07 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 493692ED72F Authentication-Results: ext-mx05.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx05.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=libvir-list-bounces@redhat.com From: John Ferlan To: libvir-list@redhat.com Date: Fri, 28 Jul 2017 12:38:59 -0400 Message-Id: <20170728163901.21444-6-jferlan@redhat.com> In-Reply-To: <20170728163901.21444-1-jferlan@redhat.com> References: <20170728163901.21444-1-jferlan@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 5/7] util: Introduce and use virObjectRWUnlock 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: , MIME-Version: 1.0 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.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.29]); Fri, 28 Jul 2017 16:39:19 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Rather than overload virObjectUnlock as commit id '77f4593b' has done, create a separate virObjectRWUnlock API that will force the consumers to make the proper decision regarding unlocking the RWLock's. This restores the virObjectUnlock code to using the virObjectGetLockableObj as well. Signed-off-by: John Ferlan --- src/conf/virdomainobjlist.c | 36 ++++++++++++++++++------------------ src/libvirt_private.syms | 1 + src/util/virobject.c | 41 +++++++++++++++++++++++++++-------------- src/util/virobject.h | 4 ++++ 4 files changed, 50 insertions(+), 32 deletions(-) diff --git a/src/conf/virdomainobjlist.c b/src/conf/virdomainobjlist.c index 08a51cc..85f5434 100644 --- a/src/conf/virdomainobjlist.c +++ b/src/conf/virdomainobjlist.c @@ -123,7 +123,7 @@ virDomainObjListFindByIDInternal(virDomainObjListPtr do= ms, obj =3D virHashSearch(doms->objs, virDomainObjListSearchID, &id, NULL); if (ref) { virObjectRef(obj); - virObjectUnlock(doms); + virObjectRWUnlock(doms); } if (obj) { virObjectLock(obj); @@ -135,7 +135,7 @@ virDomainObjListFindByIDInternal(virDomainObjListPtr do= ms, } } if (!ref) - virObjectUnlock(doms); + virObjectRWUnlock(doms); return obj; } =20 @@ -168,7 +168,7 @@ virDomainObjListFindByUUIDInternal(virDomainObjListPtr = doms, obj =3D virHashLookup(doms->objs, uuidstr); if (ref) { virObjectRef(obj); - virObjectUnlock(doms); + virObjectRWUnlock(doms); } if (obj) { virObjectLock(obj); @@ -180,7 +180,7 @@ virDomainObjListFindByUUIDInternal(virDomainObjListPtr = doms, } } if (!ref) - virObjectUnlock(doms); + virObjectRWUnlock(doms); return obj; } =20 @@ -210,7 +210,7 @@ virDomainObjPtr virDomainObjListFindByName(virDomainObj= ListPtr doms, return NULL; obj =3D virHashLookup(doms->objsName, name); virObjectRef(obj); - virObjectUnlock(doms); + virObjectRWUnlock(doms); if (obj) { virObjectLock(obj); if (obj->removing) { @@ -333,7 +333,7 @@ virDomainObjPtr virDomainObjListAdd(virDomainObjListPtr= doms, if (virObjectLockWrite(doms) < 0) return NULL; ret =3D virDomainObjListAddLocked(doms, def, xmlopt, flags, oldDef); - virObjectUnlock(doms); + virObjectRWUnlock(doms); return ret; } =20 @@ -361,7 +361,7 @@ void virDomainObjListRemove(virDomainObjListPtr doms, virHashRemoveEntry(doms->objsName, dom->def->name); virObjectUnlock(dom); virObjectUnref(dom); - virObjectUnlock(doms); + virObjectRWUnlock(doms); } =20 =20 @@ -430,7 +430,7 @@ virDomainObjListRename(virDomainObjListPtr doms, =20 ret =3D 0; cleanup: - virObjectUnlock(doms); + virObjectRWUnlock(doms); VIR_FREE(old_name); return ret; } @@ -622,7 +622,7 @@ virDomainObjListLoadAllConfigs(virDomainObjListPtr doms, } =20 VIR_DIR_CLOSE(dir); - virObjectUnlock(doms); + virObjectRWUnlock(doms); return ret; } =20 @@ -669,7 +669,7 @@ virDomainObjListNumOfDomains(virDomainObjListPtr doms, if (virObjectLockRead(doms) < 0) return -1; virHashForEach(doms->objs, virDomainObjListCount, &data); - virObjectUnlock(doms); + virObjectRWUnlock(doms); return data.count; } =20 @@ -714,7 +714,7 @@ virDomainObjListGetActiveIDs(virDomainObjListPtr doms, if (virObjectLockRead(doms) < 0) return -1; virHashForEach(doms->objs, virDomainObjListCopyActiveIDs, &data); - virObjectUnlock(doms); + virObjectRWUnlock(doms); return data.numids; } =20 @@ -769,7 +769,7 @@ virDomainObjListGetInactiveNames(virDomainObjListPtr do= ms, if (virObjectLockRead(doms) < 0) return -1; virHashForEach(doms->objs, virDomainObjListCopyInactiveNames, &data); - virObjectUnlock(doms); + virObjectRWUnlock(doms); if (data.oom) { for (i =3D 0; i < data.numnames; i++) VIR_FREE(data.names[i]); @@ -811,7 +811,7 @@ virDomainObjListForEach(virDomainObjListPtr doms, if (virObjectLockRead(doms) < 0) return -1; virHashForEach(doms->objs, virDomainObjListHelper, &data); - virObjectUnlock(doms); + virObjectRWUnlock(doms); return data.ret; } =20 @@ -946,12 +946,12 @@ virDomainObjListCollect(virDomainObjListPtr domlist, return -1; sa_assert(domlist->objs); if (VIR_ALLOC_N(data.vms, virHashSize(domlist->objs)) < 0) { - virObjectUnlock(domlist); + virObjectRWUnlock(domlist); return -1; } =20 virHashForEach(domlist->objs, virDomainObjListCollectIterator, &data); - virObjectUnlock(domlist); + virObjectRWUnlock(domlist); =20 virDomainObjListFilter(&data.vms, &data.nvms, conn, filter, flags); =20 @@ -991,7 +991,7 @@ virDomainObjListConvert(virDomainObjListPtr domlist, if (skip_missing) continue; =20 - virObjectUnlock(domlist); + virObjectRWUnlock(domlist); virReportError(VIR_ERR_NO_DOMAIN, _("no domain with matching uuid '%s' (%s)"), uuidstr, dom->name); @@ -1001,12 +1001,12 @@ virDomainObjListConvert(virDomainObjListPtr domlist, virObjectRef(vm); =20 if (VIR_APPEND_ELEMENT(*vms, *nvms, vm) < 0) { - virObjectUnlock(domlist); + virObjectRWUnlock(domlist); virObjectUnref(vm); goto error; } } - virObjectUnlock(domlist); + virObjectRWUnlock(domlist); =20 sa_assert(*vms); virDomainObjListFilter(vms, nvms, conn, filter, flags); diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index f1a6510..5e6b58c 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2310,6 +2310,7 @@ virObjectLockWrite; virObjectNew; virObjectRef; virObjectRWLockableNew; +virObjectRWUnlock; virObjectUnlock; virObjectUnref; =20 diff --git a/src/util/virobject.c b/src/util/virobject.c index 0db98c3..6f786ce 100644 --- a/src/util/virobject.c +++ b/src/util/virobject.c @@ -480,26 +480,39 @@ virObjectLockWrite(void *anyobj) =20 /** * virObjectUnlock: - * @anyobj: any instance of virObjectLockable or virObjectRWLockable + * @anyobj: any instance of virObjectLockable * * Release a lock on @anyobj. The lock must have been acquired by - * virObjectLock or virObjectLockRead. + * virObjectLock. */ void virObjectUnlock(void *anyobj) { - if (virObjectIsClass(anyobj, virObjectLockableClass)) { - virObjectLockablePtr obj =3D anyobj; - virMutexUnlock(&obj->lock); - } else if (virObjectIsClass(anyobj, virObjectRWLockableClass)) { - virObjectRWLockablePtr obj =3D anyobj; - virRWLockUnlock(&obj->lock); - } else { - virObjectPtr obj =3D anyobj; - VIR_WARN("Object %p (%s) is not a virObjectLockable " - "nor virObjectRWLockable instance", - anyobj, obj ? obj->klass->name : "(unknown)"); - } + virObjectLockablePtr obj =3D virObjectGetLockableObj(anyobj); + + if (!obj) + return; + + virMutexUnlock(&obj->lock); +} + + +/** + * virObjectRWUnlock: + * @anyobj: any instance of virObjectRWLockable + * + * Release a lock on @anyobj. The lock must have been acquired by + * virObjectLockRead or virObjectLockWrite. + */ +void +virObjectRWUnlock(void *anyobj) +{ + virObjectRWLockablePtr obj =3D virObjectGetRWLockableObj(anyobj); + + if (!obj) + return; + + virRWLockUnlock(&obj->lock); } =20 =20 diff --git a/src/util/virobject.h b/src/util/virobject.h index f0d1f97..bd0fbb8 100644 --- a/src/util/virobject.h +++ b/src/util/virobject.h @@ -137,6 +137,10 @@ virObjectUnlock(void *lockableobj) ATTRIBUTE_NONNULL(1); =20 void +virObjectRWUnlock(void *lockableobj) + ATTRIBUTE_NONNULL(1); + +void virObjectListFree(void *list); =20 void --=20 2.9.4 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Sun Apr 28 00:38:29 2024 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 Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1501259963749285.4554355629904; Fri, 28 Jul 2017 09:39:23 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id CF3FF5B483; Fri, 28 Jul 2017 16:39:21 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id A63696F920; Fri, 28 Jul 2017 16:39:21 +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 5CDE2180BA80; Fri, 28 Jul 2017 16:39:21 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v6SGd7Ac010361 for ; Fri, 28 Jul 2017 12:39:07 -0400 Received: by smtp.corp.redhat.com (Postfix) id B513E5D759; Fri, 28 Jul 2017 16:39:07 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-117-46.phx2.redhat.com [10.3.117.46]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7E5F65D747 for ; Fri, 28 Jul 2017 16:39:07 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com CF3FF5B483 Authentication-Results: ext-mx03.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx03.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=libvir-list-bounces@redhat.com From: John Ferlan To: libvir-list@redhat.com Date: Fri, 28 Jul 2017 12:39:00 -0400 Message-Id: <20170728163901.21444-7-jferlan@redhat.com> In-Reply-To: <20170728163901.21444-1-jferlan@redhat.com> References: <20170728163901.21444-1-jferlan@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 6/7] util: Create common error path for invalid object 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: , MIME-Version: 1.0 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.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Fri, 28 Jul 2017 16:39:22 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" If virObjectIsClass fails "internally" to virobject.c, create a macro to generate the VIR_WARN describing what the problem is. Also improve the checks and message a bit to indicate which was the failure - whether the obj was NULL or just not the right class Signed-off-by: John Ferlan --- src/util/virobject.c | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/util/virobject.c b/src/util/virobject.c index 6f786ce..2cf4743 100644 --- a/src/util/virobject.c +++ b/src/util/virobject.c @@ -47,6 +47,17 @@ struct _virClass { virObjectDisposeCallback dispose; }; =20 +#define VIR_OBJECT_USAGE_PRINT_WARNING(anyobj, objclass) = \ + do { = \ + virObjectPtr obj =3D anyobj; = \ + if (!obj) = \ + VIR_WARN("Object cannot be NULL"); = \ + else = \ + VIR_WARN("Object %p (%s) is not a %s instance", = \ + anyobj, obj->klass->name, #objclass); = \ + } while (0) + + static virClassPtr virObjectClass; static virClassPtr virObjectLockableClass; static virClassPtr virObjectRWLockableClass; @@ -370,15 +381,10 @@ virObjectRef(void *anyobj) static virObjectLockablePtr virObjectGetLockableObj(void *anyobj) { - virObjectPtr obj; - if (virObjectIsClass(anyobj, virObjectLockableClass)) return anyobj; =20 - obj =3D anyobj; - VIR_WARN("Object %p (%s) is not a virObjectLockable instance", - anyobj, obj ? obj->klass->name : "(unknown)"); - + VIR_OBJECT_USAGE_PRINT_WARNING(anyobj, virObjectLockable); return NULL; } =20 @@ -386,15 +392,10 @@ virObjectGetLockableObj(void *anyobj) static virObjectRWLockablePtr virObjectGetRWLockableObj(void *anyobj) { - virObjectPtr obj; - if (virObjectIsClass(anyobj, virObjectRWLockableClass)) return anyobj; =20 - obj =3D anyobj; - VIR_WARN("Object %p (%s) is not a virObjectRWLockable instance", - anyobj, obj ? obj->klass->name : "(unknown)"); - + VIR_OBJECT_USAGE_PRINT_WARNING(anyobj, virObjectRWLockable); return NULL; } =20 --=20 2.9.4 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Sun Apr 28 00:38:29 2024 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 Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 15012599873381010.8265155538973; Fri, 28 Jul 2017 09:39:47 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 5C7B55C14C; Fri, 28 Jul 2017 16:39:44 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 1908770943; Fri, 28 Jul 2017 16:39:44 +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 A9DEA180597D; Fri, 28 Jul 2017 16:39:43 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v6SGd8qE010366 for ; Fri, 28 Jul 2017 12:39:08 -0400 Received: by smtp.corp.redhat.com (Postfix) id 1C4B95D759; Fri, 28 Jul 2017 16:39:08 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-117-46.phx2.redhat.com [10.3.117.46]) by smtp.corp.redhat.com (Postfix) with ESMTP id D74A85D747 for ; Fri, 28 Jul 2017 16:39:07 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 5C7B55C14C Authentication-Results: ext-mx03.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx03.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=libvir-list-bounces@redhat.com From: John Ferlan To: libvir-list@redhat.com Date: Fri, 28 Jul 2017 12:39:01 -0400 Message-Id: <20170728163901.21444-8-jferlan@redhat.com> In-Reply-To: <20170728163901.21444-1-jferlan@redhat.com> References: <20170728163901.21444-1-jferlan@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 7/7] util: Add safety net of checks to ensure valid object 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: , MIME-Version: 1.0 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.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Fri, 28 Jul 2017 16:39:45 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" The virObject logic "assumes" that whatever is passed to its API's would be some sort of virObjectPtr; however, if it is not then some really bad things can happen. So far there's been only virObject{Ref|Unref}, virObject{Lock|Unlock}, and virObjectIsClass and the virObject and virObjectLockable class consumers have been well behaved and code well tested. Soon there will be more consumers and one such consumer tripped over this during testing by passing a virHashTablePtr to virObjectIsClass which ends up calling virClassIsDerivedFrom using "obj->klass", which wasn't really a klass object causing one of those bad things to happen. To avoid the future possibility that a non virObject class memory was passed to some virObject* API, this patch adds two new checks - one to validate that the object has the 0xCAFExxxx value in obj->->u.s.magic and the other to ensure obj->u.s.magic doesn't "wrap" some day to 0xCAFF0000 if we ever get that many object classes. The check is also moved before the name VIR_STRDUP to avoid the extra VIR_FREE that would be required on the failure path. It is still left up to the caller to handle the failed API calls just as it would be if it passed a NULL opaque pointer anyobj. Signed-off-by: John Ferlan --- src/util/virobject.c | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/src/util/virobject.c b/src/util/virobject.c index 2cf4743..dd4c39a 100644 --- a/src/util/virobject.c +++ b/src/util/virobject.c @@ -47,14 +47,21 @@ struct _virClass { virObjectDisposeCallback dispose; }; =20 +#define VIR_OBJECT_NOTVALID(obj) (!obj || ((obj->u.s.magic & 0xCAFE0000) != =3D 0xCAFE0000)) + #define VIR_OBJECT_USAGE_PRINT_WARNING(anyobj, objclass) = \ do { = \ virObjectPtr obj =3D anyobj; = \ - if (!obj) = \ - VIR_WARN("Object cannot be NULL"); = \ - else = \ + if (VIR_OBJECT_NOTVALID(obj)) { = \ + if (!obj) = \ + VIR_WARN("Object cannot be NULL"); = \ + else = \ + VIR_WARN("Object %p has a bad magic number %X", = \ + obj, obj->u.s.magic); = \ + } else { = \ VIR_WARN("Object %p (%s) is not a %s instance", = \ anyobj, obj->klass->name, #objclass); = \ + } = \ } while (0) =20 =20 @@ -177,9 +184,14 @@ virClassNew(virClassPtr parent, goto error; =20 klass->parent =3D parent; + klass->magic =3D virAtomicIntInc(&magicCounter); + if (klass->magic > 0xCAFEFFFF) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("too many object classes defined")); + goto error; + } if (VIR_STRDUP(klass->name, name) < 0) goto error; - klass->magic =3D virAtomicIntInc(&magicCounter); klass->objectSize =3D objectSize; klass->dispose =3D dispose; =20 @@ -331,7 +343,7 @@ virObjectUnref(void *anyobj) { virObjectPtr obj =3D anyobj; =20 - if (!obj) + if (VIR_OBJECT_NOTVALID(obj)) return false; =20 bool lastRef =3D virAtomicIntDecAndTest(&obj->u.s.refs); @@ -370,7 +382,7 @@ virObjectRef(void *anyobj) { virObjectPtr obj =3D anyobj; =20 - if (!obj) + if (VIR_OBJECT_NOTVALID(obj)) return NULL; virAtomicIntInc(&obj->u.s.refs); PROBE(OBJECT_REF, "obj=3D%p", obj); @@ -532,7 +544,7 @@ virObjectIsClass(void *anyobj, virClassPtr klass) { virObjectPtr obj =3D anyobj; - if (!obj) + if (VIR_OBJECT_NOTVALID(obj)) return false; =20 return virClassIsDerivedFrom(obj->klass, klass); --=20 2.9.4 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list