From nobody Sun May 19 01:15:26 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 207.211.31.81 as permitted sender) client-ip=207.211.31.81; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-1.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 207.211.31.81 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=1579185317; cv=none; d=zohomail.com; s=zohoarc; b=KnGGxjDKsVHG0wE47UF8cZXcrr/rhiCS8V3uGUTibbyHofp2oEj551QZ4yR+T1WzO7MFhleCClvZuGQQ5fnMDekB/YH4xmwQ3gtbkfTD/KcM95HKgz1ktmqDNGvSHMC2IE9wPpY3r+m4CqZPElyVbGhLQbc11UT+i5bc/qPNsgo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1579185317; 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; bh=brJg7KXTCDypnx+NYaUaTwuGIbDCtoHP/hH18TzeXFk=; b=iZAFUPIqQ4kacjMw59BVUO3hAyPTzy4tOOQsfnPv8EWnO9vU3wpdkNMNps93zHykhNJ8WOG3xPgG8SuBWd+BIymXj/G5IiAe3gaRZsJqRyVVy/XjnbsxkUDJndY6t/uLKdtkoLUIgsujpTFINO57cYtJtQlVQKmLQGDfkwoPlzM= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 207.211.31.81 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-1.mimecast.com (us-smtp-1.mimecast.com [207.211.31.81]) by mx.zohomail.com with SMTPS id 1579185317170353.8192125184273; Thu, 16 Jan 2020 06:35:17 -0800 (PST) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-137-3upL9bCOMIy7IRiR-pOiVQ-1; Thu, 16 Jan 2020 09:35:14 -0500 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 mimecast-mx01.redhat.com (Postfix) with ESMTPS id 4F001106BC24; Thu, 16 Jan 2020 14:35:08 +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 D221660C85; Thu, 16 Jan 2020 14:35:07 +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 5BD4C87085; Thu, 16 Jan 2020 14:35:07 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 00GEZ6HV021946 for ; Thu, 16 Jan 2020 09:35:06 -0500 Received: by smtp.corp.redhat.com (Postfix) id 3CB65385; Thu, 16 Jan 2020 14:35:06 +0000 (UTC) Received: from angien.redhat.com (unknown [10.43.2.48]) by smtp.corp.redhat.com (Postfix) with ESMTP id B6145386 for ; Thu, 16 Jan 2020 14:34:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1579185316; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=brJg7KXTCDypnx+NYaUaTwuGIbDCtoHP/hH18TzeXFk=; b=LXdLn6aFxzMjh0EA39L98ybl+H0JPjOAehHNFMSJY2K1ZhO1T7DXhSIxlJ5vQhnm4srE84 Wn/xlvptz2FX59tE8fJ0DpdOcGVbQ1k1CHVnUvB+uJLfez3BO86WIgv5sOskfGLkOhD5lT NHdrA9nZfDaKU8a6jaggN206jMta61I= From: Peter Krempa To: libvir-list@redhat.com Subject: [PATCH 1/2] util: hash: Improve debugability of "Duplicate key" error message Date: Thu, 16 Jan 2020 15:34:30 +0100 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-loop: libvir-list@redhat.com 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: , 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-MC-Unique: 3upL9bCOMIy7IRiR-pOiVQ-1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" If we get an user reporting this error message being shown it's pretty useless in terms of actually debugging it since we don't know which hash and which key are actually subject to the error. This patch adds a new hash table callback which formats the user-readable version of the hash key and reports it in the new message which will look like: "Duplicate hash table key 'blah'" That way we will at least have an anchor point where to start the search. There are two special implementations of keys which are numeric so we add specific printer functions for them. Signed-off-by: Peter Krempa Reviewed-by: Michal Privoznik --- src/conf/domain_addr.c | 9 +++++++++ src/util/vircgroup.c | 8 ++++++++ src/util/virhash.c | 22 ++++++++++++++++++++-- src/util/virhash.h | 13 +++++++++++++ 4 files changed, 50 insertions(+), 2 deletions(-) diff --git a/src/conf/domain_addr.c b/src/conf/domain_addr.c index ef7ee80e6a..607ba56efd 100644 --- a/src/conf/domain_addr.c +++ b/src/conf/domain_addr.c @@ -1007,6 +1007,13 @@ virZPCIAddrKeyCopy(const void *name) } +static char * +virZPCIAddrKeyPrintHuman(const void *name) +{ + return g_strdup_printf("%u", *((unsigned int *)name)); +} + + static void virZPCIAddrKeyFree(void *name) { @@ -1041,6 +1048,7 @@ virDomainPCIAddressSetExtensionAlloc(virDomainPCIAddr= essSetPtr addrs, virZPCIAddrKeyCode, virZPCIAddrKeyEqual, virZPCIAddrKeyCopy, + virZPCIAddrKeyPrint= Human, virZPCIAddrKeyFree)= )) goto error; @@ -1048,6 +1056,7 @@ virDomainPCIAddressSetExtensionAlloc(virDomainPCIAddr= essSetPtr addrs, virZPCIAddrKeyCode, virZPCIAddrKeyEqual, virZPCIAddrKeyCopy, + virZPCIAddrKeyPrint= Human, virZPCIAddrKeyFree)= )) goto error; } diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c index 87ed9f4565..dff2f6fd3a 100644 --- a/src/util/vircgroup.c +++ b/src/util/vircgroup.c @@ -2502,6 +2502,13 @@ virCgroupPidCopy(const void *name) } +static char * +virCgroupPidPrintHuman(const void *name) +{ + return g_strdup_printf("%ld", (const long)name); +} + + int virCgroupKillRecursiveInternal(virCgroupPtr group, int signum, @@ -2587,6 +2594,7 @@ virCgroupKillRecursive(virCgroupPtr group, int signum) virCgroupPidCode, virCgroupPidEqual, virCgroupPidCopy, + virCgroupPidPrintHuman, NULL); VIR_DEBUG("group=3D%p path=3D%s signum=3D%d", group, group->path, sign= um); diff --git a/src/util/virhash.c b/src/util/virhash.c index 313ca57a54..edf11e8b7a 100644 --- a/src/util/virhash.c +++ b/src/util/virhash.c @@ -59,6 +59,7 @@ struct _virHashTable { virHashKeyCode keyCode; virHashKeyEqual keyEqual; virHashKeyCopy keyCopy; + virHashKeyPrintHuman keyPrint; virHashKeyFree keyFree; }; @@ -98,6 +99,14 @@ static void *virHashStrCopy(const void *name) return ret; } + +static char * +virHashStrPrintHuman(const void *name) +{ + return g_strdup(name); +} + + static void virHashStrFree(void *name) { VIR_FREE(name); @@ -136,6 +145,7 @@ virHashTablePtr virHashCreateFull(ssize_t size, virHashKeyCode keyCode, virHashKeyEqual keyEqual, virHashKeyCopy keyCopy, + virHashKeyPrintHuman keyPrint, virHashKeyFree keyFree) { virHashTablePtr table =3D NULL; @@ -153,6 +163,7 @@ virHashTablePtr virHashCreateFull(ssize_t size, table->keyCode =3D keyCode; table->keyEqual =3D keyEqual; table->keyCopy =3D keyCopy; + table->keyPrint =3D keyPrint; table->keyFree =3D keyFree; if (VIR_ALLOC_N(table->table, size) < 0) { @@ -180,6 +191,7 @@ virHashNew(virHashDataFree dataFree) virHashStrCode, virHashStrEqual, virHashStrCopy, + virHashStrPrintHuman, virHashStrFree); } @@ -200,6 +212,7 @@ virHashTablePtr virHashCreate(ssize_t size, virHashData= Free dataFree) virHashStrCode, virHashStrEqual, virHashStrCopy, + virHashStrPrintHuman, virHashStrFree); } @@ -353,8 +366,13 @@ virHashAddOrUpdateEntry(virHashTablePtr table, const v= oid *name, entry->payload =3D userdata; return 0; } else { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("Duplicate key")); + g_autofree char *keystr =3D NULL; + + if (table->keyPrint) + keystr =3D table->keyPrint(name); + + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Duplicate hash table key '%s'"), NULLSTR= (keystr)); return -1; } } diff --git a/src/util/virhash.h b/src/util/virhash.h index 6318c0b3cd..08f99d8a3d 100644 --- a/src/util/virhash.h +++ b/src/util/virhash.h @@ -86,6 +86,18 @@ typedef bool (*virHashKeyEqual)(const void *namea, const= void *nameb); * Returns a newly allocated copy of @name */ typedef void *(*virHashKeyCopy)(const void *name); +/** + * virHashKeyPrintHuman: + * @name: the hash key + * + * Get a human readable version of the key for error messages. Caller + * will free the returned string. + * + * Returns a string representation of the key for use in error messages. C= aller + * promises to always free the returned string. + */ +typedef char *(*virHashKeyPrintHuman) (const void *name); + /** * virHashKeyFree: * @name: the hash key @@ -108,6 +120,7 @@ virHashTablePtr virHashCreateFull(ssize_t size, virHashKeyCode keyCode, virHashKeyEqual keyEqual, virHashKeyCopy keyCopy, + virHashKeyPrintHuman keyPrint, virHashKeyFree keyFree); void virHashFree(virHashTablePtr table); ssize_t virHashSize(const virHashTable *table); --=20 2.24.1 From nobody Sun May 19 01:15:26 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 207.211.31.120 as permitted sender) client-ip=207.211.31.120; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-1.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 207.211.31.120 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=1579185318; cv=none; d=zohomail.com; s=zohoarc; b=kUTmxuuY8uuubLHrNHM+qBLRwwknzIFSRjTbgmY67WvoClP2u1VHZ5bgKJxhYnppdpvwaMwh4Nh3jTrqdmPhwLBXPibrk7oIRW8tcHYArdm3pd2sTkI0d72KGTZyVnqa7w/VTA+mWVwhOCBLBncWB4eUO0sR/Oq8NPeKvJNvwRQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1579185318; 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; bh=/6t6GKI7UZvXXrIsWoiOO2evw/Qy/7h+DPVhOQlBGRQ=; b=d377gshdKzC+9BseC/ZkzacafBrZagslZWMFYPv16sJH5icG4h+Idb4ab67N7SIPLQOL8LnFJVzlbfkB7UNihtAyLv08nCZPuIjNa0DOWNcR4kpUHU11BM+k1BeWpe2Wiqnt/60Dg0lw+CrORCeCK//JQI4+zotdffPO4//QNWs= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 207.211.31.120 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [207.211.31.120]) by mx.zohomail.com with SMTPS id 157918531883976.8826746860583; Thu, 16 Jan 2020 06:35:18 -0800 (PST) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-273-Zf_OPkVkPXerEvqJn2dM8Q-1; Thu, 16 Jan 2020 09:35:16 -0500 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 21C28109E307; Thu, 16 Jan 2020 14:35:10 +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 AD48728D2B; Thu, 16 Jan 2020 14:35: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 5972618034EA; Thu, 16 Jan 2020 14:35:09 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 00GEZ7cb021951 for ; Thu, 16 Jan 2020 09:35:07 -0500 Received: by smtp.corp.redhat.com (Postfix) id 4087D28D0C; Thu, 16 Jan 2020 14:35:07 +0000 (UTC) Received: from angien.redhat.com (unknown [10.43.2.48]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9892F28D00 for ; Thu, 16 Jan 2020 14:35:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1579185317; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=/6t6GKI7UZvXXrIsWoiOO2evw/Qy/7h+DPVhOQlBGRQ=; b=ayQpz6/Uze8rhmu9cS8UQw1oDjtajRrFv4UYz9qUeL9FjssSxbciWlXULeBCdIEduLQbI3 8Y5hTLLRZUuBzM17ZIdIESkAoMb/3m3X+90k6nUeEIG5AkPDBtEb5fwDWTZ6IAf10i8gMI KWNS7nIORCYiMaZOrekgfh/OI9b1T3A= From: Peter Krempa To: libvir-list@redhat.com Subject: [PATCH 2/2] tests: hash: Test case for adding duplicate hash entry Date: Thu, 16 Jan 2020 15:34:31 +0100 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-loop: libvir-list@redhat.com 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: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-MC-Unique: Zf_OPkVkPXerEvqJn2dM8Q-1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" Test that adding a duplicate entry is rejected properly. This also allows to see the error message of the duplicate key addition in verbose mode. Signed-off-by: Peter Krempa Reviewed-by: Michal Privoznik --- tests/virhashtest.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tests/virhashtest.c b/tests/virhashtest.c index 66fa3a428e..4d05cbb0f8 100644 --- a/tests/virhashtest.c +++ b/tests/virhashtest.c @@ -510,6 +510,28 @@ testHashEqual(const void *data G_GNUC_UNUSED) } +static int +testHashDuplicate(const void *data G_GNUC_UNUSED) +{ + g_autoptr(virHashTable) hash =3D NULL; + + if (!(hash =3D virHashCreate(0, NULL))) + return -1; + + if (virHashAddEntry(hash, "a", NULL) < 0) { + VIR_TEST_VERBOSE("\nfailed to add key 'a' to hash"); + return -1; + } + + if (virHashAddEntry(hash, "a", NULL) >=3D 0) { + VIR_TEST_VERBOSE("\nadding of key 'a' should have failed"); + return -1; + } + + return 0; +} + + static int mymain(void) { @@ -546,6 +568,7 @@ mymain(void) DO_TEST("Search", Search); DO_TEST("GetItems", GetItems); DO_TEST("Equal", Equal); + DO_TEST("Duplicate entry", Duplicate); return (ret =3D=3D 0) ? EXIT_SUCCESS : EXIT_FAILURE; } --=20 2.24.1