From nobody Sun May 19 02:06:48 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; dmarc=pass(p=none dis=none) header.from=redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1554106222259377.43205920663354; Mon, 1 Apr 2019 01:10:22 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 17463308620C; Mon, 1 Apr 2019 08:10:21 +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 D83881001938; Mon, 1 Apr 2019 08:10:20 +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 91F5F41F3D; Mon, 1 Apr 2019 08:10:20 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id x318AJTQ020843 for ; Mon, 1 Apr 2019 04:10:19 -0400 Received: by smtp.corp.redhat.com (Postfix) id 44F7F5C221; Mon, 1 Apr 2019 08:10:19 +0000 (UTC) Received: from moe.brq.redhat.com (unknown [10.43.2.30]) by smtp.corp.redhat.com (Postfix) with ESMTP id C0EDB5C220 for ; Mon, 1 Apr 2019 08:10:16 +0000 (UTC) From: Michal Privoznik To: libvir-list@redhat.com Date: Mon, 1 Apr 2019 10:10:11 +0200 Message-Id: <08725680ec84dc555ef0e360279673f872cee28e.1554106211.git.mprivozn@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH] virerror: Make error reporting prettier 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-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.42]); Mon, 01 Apr 2019 08:10:21 +0000 (UTC) Content-Type: text/plain; charset="utf-8" So far, if something goes wrong we print an error message, e.g. like this: virsh # start fedora error: Failed to start domain fedora error: internal error: process exited while connecting to monitor: 2019-0= 4-01T08:08:49.753850Z qemu-system-x86_64: -object memory-backend-memfd,id= =3Dram-node0,hugetlb=3Dyes,hugetlbsize=3D0,size=3D8589934592,host-nodes=3D0= ,policy=3Dbind: Property 'memory-backend-memfd.hugetlbsize' doesn't take va= lue '0' This is very boring and usually too low level for users to know what is going on or how to fix the problem. Let's reimplement our error reporting then. After this patch the previous error turns into (my favourite): virsh # start fedora error: Failed to start domain fedora error: operation failed: the printer thinks its a router. Signed-off-by: Michal Privoznik --- src/Makefile.am | 4 +++ src/util/Makefile.inc.am | 2 ++ src/util/virerror.c | 76 ++++++++++++++++++++++++++++++++++------ 3 files changed, 72 insertions(+), 10 deletions(-) diff --git a/src/Makefile.am b/src/Makefile.am index a73f43c483..387f57f288 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -734,6 +734,7 @@ libvirt_setuid_rpc_client_la_LDFLAGS =3D \ $(AM_LDFLAGS) \ $(LIBXML_LIBS) \ $(SECDRIVER_LIBS) \ + $(CURL_LIBS) \ $(NULL) libvirt_setuid_rpc_client_la_CFLAGS =3D \ -DLIBVIRT_SETUID_RPC_CLIENT \ @@ -741,6 +742,7 @@ libvirt_setuid_rpc_client_la_CFLAGS =3D \ -I$(srcdir)/rpc \ $(AM_CFLAGS) \ $(SECDRIVER_CFLAGS) \ + $(CURL_CFLAGS) \ $(XDR_CFLAGS) \ $(NULL) endif WITH_SETUID_RPC_CLIENT @@ -924,6 +926,7 @@ libvirt_nss_la_CFLAGS =3D \ -DLIBVIRT_NSS \ $(AM_CFLAGS) \ $(YAJL_CFLAGS) \ + $(CURL_CFLAGS) \ $(NULL) libvirt_nss_la_LDFLAGS =3D \ $(AM_LDFLAGS) \ @@ -931,6 +934,7 @@ libvirt_nss_la_LDFLAGS =3D \ =20 libvirt_nss_la_LIBADD =3D \ $(YAJL_LIBS) \ + $(CURL_LIBS) \ $(NULL) endif WITH_NSS =20 diff --git a/src/util/Makefile.inc.am b/src/util/Makefile.inc.am index aa5c6cbe03..6852d0105d 100644 --- a/src/util/Makefile.inc.am +++ b/src/util/Makefile.inc.am @@ -271,6 +271,7 @@ libvirt_util_la_CFLAGS =3D \ $(NUMACTL_CFLAGS) \ $(GNUTLS_CFLAGS) \ $(ACL_CFLAGS) \ + $(CURL_CFLAGS) \ $(NULL) libvirt_util_la_LIBADD =3D \ $(CAPNG_LIBS) \ @@ -287,6 +288,7 @@ libvirt_util_la_LIBADD =3D \ $(NUMACTL_LIBS) \ $(ACL_LIBS) \ $(GNUTLS_LIBS) \ + $(CURL_LIBS) \ $(NULL) =20 =20 diff --git a/src/util/virerror.c b/src/util/virerror.c index 05e535d859..2687869049 100644 --- a/src/util/virerror.c +++ b/src/util/virerror.c @@ -21,6 +21,7 @@ #include =20 #include +#include =20 #include "virerror.h" #include "datatypes.h" @@ -1252,6 +1253,66 @@ virErrorMsg(virErrorNumber error, const char *info) } =20 =20 +#define BOFH_BUF_LEN 1024 +#define BOFH_URL "telnet://bofh.jeffballard.us:666" +#define BOFH_PREFIX "Your excuse is: " + +typedef struct { + char *buf; + size_t len; + size_t pos; +} write_func_data; + +static size_t +write_func(void *ptr, size_t size, size_t nmemb, void *opaque) +{ + write_func_data *data =3D opaque; + ssize_t to_write =3D MIN(size * nmemb, data->len - data->pos - 1); + + if (to_write > 1) { + memcpy(data->buf + data->pos, ptr, to_write); + data->pos +=3D to_write; + data->buf[data->pos + 1] =3D '\0'; + } + + return size * nmemb; +} + + +static int +virReportErrorGetBOFH(char *retBuf, + size_t retBufLen) +{ + char buf[BOFH_BUF_LEN] =3D { 0 }; + write_func_data data =3D {.buf =3D buf, .len =3D sizeof(buf), .pos =3D= 0 }; + const char *tmp; + CURL *curl; + CURLcode result; + + if (!(curl =3D curl_easy_init())) + return -1; + + curl_easy_setopt(curl, CURLOPT_URL, BOFH_URL); +#ifdef CURLOPT_MUTE + curl_easy_setopt(curl, CURLOPT_MUTE, 1); +#endif + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_func); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &data); + + result =3D curl_easy_perform(curl); + curl_easy_cleanup(curl); + if (result !=3D CURLE_OK) + return -1; + + if (!(tmp =3D strstr(buf, BOFH_PREFIX))) + return -1; + + tmp +=3D strlen(BOFH_PREFIX); + + return virStrcpy(retBuf, tmp, retBufLen); +} + + /** * virReportErrorHelper: * @@ -1267,26 +1328,21 @@ virErrorMsg(virErrorNumber error, const char *info) * ReportError */ void virReportErrorHelper(int domcode, - int errorcode, + int errorcode ATTRIBUTE_UNUSED, const char *filename, const char *funcname, size_t linenr, - const char *fmt, ...) + const char *fmt ATTRIBUTE_UNUSED, + ...) { int save_errno =3D errno; - va_list args; char errorMessage[VIR_ERROR_MAX_LENGTH]; const char *virerr; =20 - if (fmt) { - va_start(args, fmt); - vsnprintf(errorMessage, sizeof(errorMessage)-1, fmt, args); - va_end(args); - } else { + if (virReportErrorGetBOFH(errorMessage, sizeof(errorMessage)) < 0) errorMessage[0] =3D '\0'; - } =20 - virerr =3D virErrorMsg(errorcode, (errorMessage[0] ? errorMessage : NU= LL)); + virerr =3D virErrorMsg(VIR_ERR_OPERATION_FAILED, (errorMessage[0] ? er= rorMessage : NULL)); virRaiseErrorFull(filename, funcname, linenr, domcode, errorcode, VIR_ERR_ERROR, virerr, errorMessage, NULL, --=20 2.19.2 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list