From nobody Mon Dec 30 17:04:51 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 1539865673353438.60300757565074; Thu, 18 Oct 2018 05:27:53 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id B38E1CA38E; Thu, 18 Oct 2018 12:27:51 +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 807B771D66; Thu, 18 Oct 2018 12:27:51 +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 107CF3D63; Thu, 18 Oct 2018 12:27:51 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w9ICRG6N026118 for ; Thu, 18 Oct 2018 08:27:16 -0400 Received: by smtp.corp.redhat.com (Postfix) id BE1C05F7E7; Thu, 18 Oct 2018 12:27:16 +0000 (UTC) Received: from moe.brq.redhat.com (unknown [10.43.2.192]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4043760BF8 for ; Thu, 18 Oct 2018 12:27:16 +0000 (UTC) From: Michal Privoznik To: libvir-list@redhat.com Date: Thu, 18 Oct 2018 14:26:50 +0200 Message-Id: <1356ddaaf8942f38a82baf52fb36fc09143b4894.1539865556.git.mprivozn@redhat.com> In-Reply-To: References: X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-loop: libvir-list@redhat.com Subject: [libvirt] [snmp PATCH 12/20] libvirtSnmp: turn showError() into printf-like function 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.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Thu, 18 Oct 2018 12:27:52 +0000 (UTC) X-ZohoMail: RDMRC_0 RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Currently, the following pattern is used with showError: printf("Some error message\n"); showError(conn); Not only showError does not need connection pointer, those two lines can be merged into a single one: showError("Some error message"); Signed-off-by: Michal Privoznik --- src/libvirtSnmp.c | 79 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 54 insertions(+), 25 deletions(-) diff --git a/src/libvirtSnmp.c b/src/libvirtSnmp.c index 88f2ec6..7aa83a1 100644 --- a/src/libvirtSnmp.c +++ b/src/libvirtSnmp.c @@ -43,6 +43,17 @@ #define ATTRIBUTE_UNUSED __attribute__((__unused__)) #endif =20 +# ifndef ATTRIBUTE_FMT_PRINTF +# ifndef __clang__ +# define ATTRIBUTE_FMT_PRINTF(fmtpos, argpos) \ + __attribute__((__format__ (__gnu_printf__, fmtpos, argpos))) +# else +# define ATTRIBUTE_FMT_PRINTF(fmtpos, argpos) \ + __attribute__((__format__ (__printf__, fmtpos, argpos))) +# endif +# endif + + int verbose =3D 0; virConnectPtr conn; int callbackRet =3D -1; @@ -76,11 +87,31 @@ stop(int sig) } =20 static void -showError(virConnectPtr conn) +ATTRIBUTE_FMT_PRINTF(1,2) +showError(const char *fmt, ...) { - const char *err =3D virGetLastErrorMessage(); + const char *libvirtErr =3D virGetLastErrorMessage(); + char ebuf[1024]; + int rc; + int size =3D 0; + va_list ap; =20 - snmp_log(LOG_ERR, "libvirt reported: \"%s\"\n", err); + va_start(ap, fmt); + rc =3D vsnprintf(ebuf, sizeof(ebuf), fmt, ap); + size +=3D rc; + va_end(ap); + + if (rc < 0 || size >=3D sizeof(ebuf)) + return; + + rc =3D snprintf(ebuf + size, sizeof(ebuf) - size, ": %s\n", libvirtErr= ); + size +=3D rc; + + if (rc < 0 || size >=3D sizeof(ebuf)) + return; + + fputs(ebuf, stderr); + snmp_log(LOG_ERR, "%s", ebuf); =20 virResetLastError(); } @@ -95,8 +126,7 @@ insertGuest(netsnmp_container *container, virDomainPtr d= omain) unsigned char uuid[VIR_UUID_BUFLEN]; =20 if (virDomainGetInfo(domain, &info) < 0) { - printf("Failed to get domain info\n"); - showError(conn); + showError("Failed to get domain info"); goto error; } =20 @@ -108,8 +138,7 @@ insertGuest(netsnmp_container *container, virDomainPtr = domain) =20 /* set the index of the row */ if (virDomainGetUUID(domain, uuid) < 0) { - printf("Failed to get domain UUID\n"); - showError(conn); + showError("Failed to get domain UUID"); goto error; } =20 @@ -122,8 +151,7 @@ insertGuest(netsnmp_container *container, virDomainPtr = domain) =20 /* set the data */ if (!(name =3D virDomainGetName(domain))) { - printf("Failed to get domain name\n"); - showError(conn); + showError("Failed to get domain name"); goto error; } =20 @@ -167,8 +195,7 @@ libvirtSnmpLoadGuests(netsnmp_container *container) virDomainPtr *domains =3D NULL; =20 if ((ndomains =3D virConnectListAllDomains(conn, &domains, 0)) < 0) { - printf("Failed to list all domains\n"); - showError(conn); + showError("Failed to list all domains"); goto cleanup; } =20 @@ -194,7 +221,7 @@ pollingThreadFunc(void *foo) { while (run) { if (virEventRunDefaultImpl() < 0) { - showError(conn); + showError("Failed to run event loop"); pthread_exit(NULL); } } @@ -222,8 +249,10 @@ libvirtRegisterEvents(virConnectPtr conn) { (domainEventCallback), NULL, myFreeFunc); =20 - if (callbackRet =3D=3D -1) - return -1; + if (callbackRet =3D=3D -1) { + showError("Failed to register libvirt event handler"); + goto cleanup; + } =20 /* we need a thread to poll for events */ if (pthread_create(&poll_thread, NULL, pollingThreadFunc, NULL)) @@ -258,8 +287,7 @@ int libvirtSnmpInit(void) /* TODO: configure the URI */ /* Use libvirt env variable LIBVIRT_DEFAULT_URI by default*/ if (!(conn =3D virConnectOpenAuth(NULL, virConnectAuthPtrDefault, 0)))= { - printf("No connection to hypervisor\n"); - showError(conn); + showError("No connection to hypervisor"); return -1; } =20 @@ -298,9 +326,8 @@ void libvirtSnmpShutdown(void) } =20 if ((rc =3D virConnectClose(conn))) { - printf("Failed to disconnect from hypervisor. " - "Leaked references: %d\n", rc); - showError(conn); + showError("Failed to disconnect from hypervisor. " + "Leaked references: %d\n", rc); } } =20 @@ -339,7 +366,7 @@ libvirtSnmpCreate(unsigned char *uuid, int state) } =20 if (virDomainCreateWithFlags(dom, flags) < 0) { - showError(conn); + showError("Failed to create domain: %s", virDomainGetName(dom)); goto cleanup; } =20 @@ -353,6 +380,7 @@ int libvirtSnmpDestroy(unsigned char *uuid) { virDomainPtr dom; + int ret =3D -1; =20 if (!(dom =3D virDomainLookupByUUID(conn, uuid))) { printf("Cannot find domain to destroy\n"); @@ -360,10 +388,12 @@ libvirtSnmpDestroy(unsigned char *uuid) } =20 if (virDomainDestroy(dom) < 0) { - showError(conn); - return -1; + showError("Failed to destroy domain %s", virDomainGetName(dom)); + goto cleanup; } =20 + ret =3D 0; + cleanup: virDomainFree(dom); return 0; } @@ -374,8 +404,7 @@ libvirtSnmpChangeState(unsigned char *uuid, int newstat= e, int oldstate) virDomainPtr dom; int ret =3D -1; =20 - dom =3D virDomainLookupByUUID(conn, uuid); - if (dom =3D=3D NULL) { + if (!(dom =3D virDomainLookupByUUID(conn, uuid))) { printf("Cannot find domain to change\n"); return 1; } @@ -392,7 +421,7 @@ libvirtSnmpChangeState(unsigned char *uuid, int newstat= e, int oldstate) } =20 if (ret < 0) - showError(conn); + showError("Failed to change state of %s", virDomainGetName(dom)); out: virDomainFree(dom); return ret; --=20 2.18.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list