From nobody Mon Feb 9 19:25:53 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) client-ip=170.10.129.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.129.124 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=1674466626; cv=none; d=zohomail.com; s=zohoarc; b=a0D51yA2q3li++k3RzW8iiFwouhOjetkp6aZVmPu4/0BD9Kxt6o38sb6lgCdfVvctHZ5xNtrP1jWNbw+0zaU1m5DglMgpQSghS4fP9ff37ck83lWjIZnXH9y3hreUHLE8FSTTwAUvOOifctoN6ahOR9lz03dtkhnoGN61sjLz4M= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1674466626; 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=GOgcvk/TgoL2UsFkvGwwzTffy+wx0njNvVJsQ3qObV4=; b=i2zyuhCk2/sc59RWTM7T6F2tPuI8up4m19CML9ZhipMvPPvIcluydAs4Bu3jJ+4YZdRCtGF3uLz6jhRXQb6Ni1ieQmAIc1b8OFvvoqjaMEKL6BhOIdO//o3LO1l55gk04Yo+n9jAogsRcA4OxRl5IunevUfJ4cqWPB5DjeT9FTw= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by mx.zohomail.com with SMTPS id 1674466626205190.43850423156061; Mon, 23 Jan 2023 01:37:06 -0800 (PST) Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-658-ReOLINXtPC2yjZi8XuCAhA-1; Mon, 23 Jan 2023 04:36:04 -0500 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id D5493886475; Mon, 23 Jan 2023 09:36:00 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com [10.30.29.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id C2564C15BAD; Mon, 23 Jan 2023 09:36:00 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (localhost [IPv6:::1]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id B10C8194658D; Mon, 23 Jan 2023 09:36:00 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 89DBA1946589 for ; Mon, 23 Jan 2023 09:35:59 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id 7F2B1492B03; Mon, 23 Jan 2023 09:35:59 +0000 (UTC) Received: from maggie.redhat.com (unknown [10.43.2.39]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2667A492B02 for ; Mon, 23 Jan 2023 09:35:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1674466625; 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=GOgcvk/TgoL2UsFkvGwwzTffy+wx0njNvVJsQ3qObV4=; b=JtMgrEE6e8ctlVmZm+86uk2KpvIWEua2MVmbSFRLNXH0G1BhvXLix2cmJ5RRBz2arJPEt+ f0TVF4LQnBvwS6cvf3vzYv4QaQb5WgED85O6jPUpCRA1VXTKGWq2BW6xsvVQ+GeV2aaZNu rT5qnTOzIRkhpYY73MNzRCFvCeWIN28= X-MC-Unique: ReOLINXtPC2yjZi8XuCAhA-1 X-Original-To: libvir-list@listman.corp.redhat.com From: Michal Privoznik To: libvir-list@redhat.com Subject: [PATCH 5/8] vbox: Introduce vboxReportError() Date: Mon, 23 Jan 2023 10:35:52 +0100 Message-Id: <0beca945f3cdaeb05c65de14ed358547c5bd6fa4.1674466489.git.mprivozn@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.10 X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libvir-list-bounces@redhat.com Sender: "libvir-list" X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1674466628065100005 Content-Type: text/plain; charset="utf-8"; x-default="true" When a VirtualBox API fails it produced an exception. Until now, we did not have correct APIs wired up to get the exception and its error message. Thus, we were left with plain: virReportError("virtualbox API failed, rc=3D%08x", rc); This is not very user friendly because those rc values are hard to parse (e.g. some values are defined as a sum of a base value and some other value) and also it expects users to know where to look. But now that we have all machinery needed for querying exceptions, vboxReportError() can be introduced. The aim is to query VirtualBox exceptions and append them after the error message we intent to report. If the exception can't be queried successfully, this behaves exactly like virReportError(). Signed-off-by: Michal Privoznik --- src/vbox/vbox_common.c | 113 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) diff --git a/src/vbox/vbox_common.c b/src/vbox/vbox_common.c index bd77641d39..2a913e5975 100644 --- a/src/vbox/vbox_common.c +++ b/src/vbox/vbox_common.c @@ -63,6 +63,119 @@ static struct _vboxDriver *vbox_driver; static struct _vboxDriver *vboxDriverObjNew(void); static __thread bool vboxDriverDisposed; =20 +#define vboxReportError(errcode, ...) \ + vboxReportErrorHelper(data, errcode, __FILE__, \ + __FUNCTION__, __LINE__, __VA_ARGS__) + +static void G_GNUC_PRINTF(6, 7) G_GNUC_UNUSED +vboxReportErrorHelper(struct _vboxDriver *data, + int errcode, + const char *filename, + const char *funcname, + size_t linenr, + const char *fmt, ...) +{ + int save_errno =3D errno; + g_auto(virBuffer) errBuf =3D VIR_BUFFER_INITIALIZER; + nsIException *ex =3D NULL; + IVirtualBoxErrorInfo *ei =3D NULL; + const nsID *vboxErrorInfoIID =3D NULL; + bool multipleLines =3D false; + nsresult rc; + g_autofree char *detail =3D NULL; + + if (fmt) { + va_list args; + + va_start(args, fmt); + detail =3D g_strdup_vprintf(fmt, args); + va_end(args); + } + + rc =3D gVBoxAPI.UPFN.GetException(data->pFuncs, &ex); + if (NS_FAILED(rc) || !ex) { + VIR_WARN("failed to get exception object"); + goto report; + } + + vboxErrorInfoIID =3D gVBoxAPI.UIVirtualBoxErrorInfo.GetIID(); + rc =3D VBOX_QUERY_INTERFACE(ex, vboxErrorInfoIID, (void **)&ei); + if (NS_FAILED(rc) || !ei) { + VIR_WARN("unable to typecast exception object"); + goto report; + } + + while (ei) { + IVirtualBoxErrorInfo *ei_next =3D NULL; + PRUnichar *componentUtf16 =3D NULL; + char *componentUtf8 =3D NULL; + PRUnichar *textUtf16 =3D NULL; + char *textUtf8 =3D NULL; + + rc =3D gVBoxAPI.UIVirtualBoxErrorInfo.GetComponent(ei, &componentU= tf16); + if (NS_FAILED(rc)) { + VIR_WARN("failed to get error text"); + goto report; + } + + rc =3D gVBoxAPI.UIVirtualBoxErrorInfo.GetText(ei, &textUtf16); + if (NS_FAILED(rc)) { + VBOX_UTF16_FREE(componentUtf16); + VIR_WARN("failed to get error text"); + goto report; + } + + VBOX_UTF16_TO_UTF8(componentUtf16, &componentUtf8); + VBOX_UTF16_FREE(componentUtf16); + + VBOX_UTF16_TO_UTF8(textUtf16, &textUtf8); + VBOX_UTF16_FREE(textUtf16); + + virBufferAsprintf(&errBuf, "%s: %s", componentUtf8, textUtf8); + VBOX_UTF8_FREE(componentUtf8); + VBOX_UTF8_FREE(textUtf8); + + if (multipleLines) + virBufferAddChar(&errBuf, '\n'); + else + multipleLines =3D true; + + rc =3D gVBoxAPI.UIVirtualBoxErrorInfo.GetNext(ei, &ei_next); + if (NS_FAILED(rc)) { + break; + } + + VBOX_RELEASE(ei); + ei =3D ei_next; + } + + report: + if (virBufferUse(&errBuf)) { + const char *vboxErr =3D virBufferCurrentContent(&errBuf); + g_autofree char *newDetail =3D NULL; + + if (!detail || STREQ(detail, "")) { + newDetail =3D g_strdup(vboxErr); + } else { + newDetail =3D g_strdup_printf("%s: %s", detail, vboxErr); + } + + VIR_FREE(detail); + detail =3D g_steal_pointer(&newDetail); + } + + virReportErrorHelper(VIR_FROM_THIS, errcode, filename, funcname, linen= r, "%s", detail); + + rc =3D gVBoxAPI.UPFN.ClearException(data->pFuncs); + if (NS_FAILED(rc)) { + VIR_WARN("failed to clear exception"); + } + + VBOX_RELEASE(ei); + VBOX_RELEASE(ex); + errno =3D save_errno; +} + static int vboxDomainDevicesDefPostParse(virDomainDeviceDef *dev G_GNUC_UNUSED, const virDomainDef *def G_GNUC_UNUSED, --=20 2.39.1