From nobody Fri Apr 26 06:06:34 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 1539865828470998.3645331218162; Thu, 18 Oct 2018 05:30:28 -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 66472C0B0204; Thu, 18 Oct 2018 12:30:26 +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 2AEEE78996; Thu, 18 Oct 2018 12:30:26 +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 CC14D3D63; Thu, 18 Oct 2018 12:30:25 +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 w9ICRD0C025941 for ; Thu, 18 Oct 2018 08:27:13 -0400 Received: by smtp.corp.redhat.com (Postfix) id 659BA60922; Thu, 18 Oct 2018 12:27:13 +0000 (UTC) Received: from moe.brq.redhat.com (unknown [10.43.2.192]) by smtp.corp.redhat.com (Postfix) with ESMTP id DA4DB60BF8 for ; Thu, 18 Oct 2018 12:27:12 +0000 (UTC) From: Michal Privoznik To: libvir-list@redhat.com Date: Thu, 18 Oct 2018 14:26:46 +0200 Message-Id: <9597696c96ac75dd6f6dce85b936cec321a7fd25.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 08/20] libvirtSnmp: Rewrite some functions 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.31]); Thu, 18 Oct 2018 12:30:27 +0000 (UTC) X-ZohoMail: RDMRC_0 RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Some function initialize retval to 0 and then for every error path they reset it to -1. Signed-off-by: Michal Privoznik --- src/libvirtSnmp.c | 50 +++++++++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 26 deletions(-) diff --git a/src/libvirtSnmp.c b/src/libvirtSnmp.c index 1e7e345..4ad6fab 100644 --- a/src/libvirtSnmp.c +++ b/src/libvirtSnmp.c @@ -258,9 +258,7 @@ int libvirtSnmpInit(void) =20 /* TODO: configure the URI */ /* Use libvirt env variable LIBVIRT_DEFAULT_URI by default*/ - conn =3D virConnectOpenAuth(NULL, virConnectAuthPtrDefault, 0); - - if (NULL =3D=3D conn) { + if (!(conn =3D virConnectOpenAuth(NULL, virConnectAuthPtrDefault, 0)))= { printf("No connection to hypervisor\n"); showError(conn); return -1; @@ -283,6 +281,7 @@ libvirtDeinitTimer(int timer ATTRIBUTE_UNUSED, void *op= aque ATTRIBUTE_UNUSED) void libvirtSnmpShutdown(void) { int timer; + int rc; =20 /* in case server is being stopped, run is still 1, so let's * shutdown the thread in a clean way */ @@ -299,8 +298,9 @@ void libvirtSnmpShutdown(void) printf("Failed to unregister domain events\n"); } =20 - if (0 !=3D virConnectClose(conn)) { - printf("Failed to disconnect from hypervisor\n"); + if ((rc =3D virConnectClose(conn))) { + printf("Failed to disconnect from hypervisor. " + "Leaked references: %d\n", rc); showError(conn); } } @@ -320,18 +320,11 @@ int libvirtSnmpCreate(unsigned char *uuid, int state) { virDomainPtr dom; - int ret; + int ret =3D -1; unsigned int flags =3D 0; =20 - dom =3D virDomainLookupByUUID(conn, uuid); - if (dom =3D=3D NULL) { - printf("Cannot find domain to create\n"); - return -1; - } - switch(state) { case VIR_DOMAIN_RUNNING: - flags =3D 0; break; case VIR_DOMAIN_PAUSED: flags =3D VIR_DOMAIN_START_PAUSED; @@ -341,10 +334,18 @@ libvirtSnmpCreate(unsigned char *uuid, int state) return -1; } =20 - ret =3D virDomainCreateWithFlags(dom, flags); - if (ret) { - showError(conn); + if (!(dom =3D virDomainLookupByUUID(conn, uuid))) { + printf("Cannot find domain to create\n"); + return -1; } + + if (virDomainCreateWithFlags(dom, flags) < 0) { + showError(conn); + goto cleanup; + } + + ret =3D 0; + cleanup: virDomainFree(dom); return ret; } @@ -353,27 +354,26 @@ int libvirtSnmpDestroy(unsigned char *uuid) { virDomainPtr dom; - int ret; =20 - dom =3D virDomainLookupByUUID(conn, uuid); - if (dom =3D=3D NULL) { + if (!(dom =3D virDomainLookupByUUID(conn, uuid))) { printf("Cannot find domain to destroy\n"); return -1; } =20 - ret =3D virDomainDestroy(dom); - if (ret) { + if (virDomainDestroy(dom) < 0) { showError(conn); + return -1; } + virDomainFree(dom); - return ret; + return 0; } =20 int libvirtSnmpChangeState(unsigned char *uuid, int newstate, int oldstate) { virDomainPtr dom; - int ret =3D 0; + int ret =3D -1; =20 dom =3D virDomainLookupByUUID(conn, uuid); if (dom =3D=3D NULL) { @@ -389,13 +389,11 @@ libvirtSnmpChangeState(unsigned char *uuid, int newst= ate, int oldstate) ret =3D virDomainShutdown(dom); else { printf("Wrong state transition from %d to %d\n", oldstate, newstat= e); - ret =3D -1; goto out; } =20 - if (ret !=3D 0) { + if (ret < 0) showError(conn); - } 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