From nobody Sat Dec 21 11:55:25 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 15398658111541018.7585574307196; Thu, 18 Oct 2018 05:30:11 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 20FDD30C9EEF; Thu, 18 Oct 2018 12:30:09 +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 D14BB6152A; Thu, 18 Oct 2018 12:30:08 +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 6333B3D62; Thu, 18 Oct 2018 12:30:08 +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 w9ICRBJi025926 for ; Thu, 18 Oct 2018 08:27:11 -0400 Received: by smtp.corp.redhat.com (Postfix) id B6F0F611C5; Thu, 18 Oct 2018 12:27:11 +0000 (UTC) Received: from moe.brq.redhat.com (unknown [10.43.2.192]) by smtp.corp.redhat.com (Postfix) with ESMTP id 3452160D58 for ; Thu, 18 Oct 2018 12:27:11 +0000 (UTC) From: Michal Privoznik To: libvir-list@redhat.com Date: Thu, 18 Oct 2018 14:26:44 +0200 Message-Id: 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 06/20] libvirtSnmp: Modernize libvirtSnmpLoadGuests 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.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.42]); Thu, 18 Oct 2018 12:30:09 +0000 (UTC) X-ZohoMail: RDMRC_0 RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Since libvirt-0.9.13 we have this atomic API for listing domains. Use that instead of the old ones. Signed-off-by: Michal Privoznik --- src/libvirtSnmp.c | 107 +++++++--------------------------------------- 1 file changed, 16 insertions(+), 91 deletions(-) diff --git a/src/libvirtSnmp.c b/src/libvirtSnmp.c index 22cf8f0..589fd03 100644 --- a/src/libvirtSnmp.c +++ b/src/libvirtSnmp.c @@ -165,105 +165,30 @@ insertGuest(netsnmp_container *container, virDomainP= tr domain) int libvirtSnmpLoadGuests(netsnmp_container *container) { - int ret =3D 0, i, numIds, numActiveDomains; - int numNames, numDefinedDomains; - int *idList =3D NULL; - char **nameList =3D NULL; - virDomainPtr domain =3D NULL; + int ret =3D -1; + int i; + int ndomains; + virDomainPtr *domains =3D NULL; =20 - numActiveDomains =3D virConnectNumOfDomains(conn); - if (-1 =3D=3D numActiveDomains) { - ret =3D -1; - printf("Failed to get number of active domains\n"); + if ((ndomains =3D virConnectListAllDomains(conn, &domains, 0)) < 0) { + printf("Failed to list all domains\n"); showError(conn); - goto out; + goto cleanup; } =20 - idList =3D malloc(sizeof(*idList) * numActiveDomains); =20 - if (NULL =3D=3D idList) { - ret =3D -1; - printf("Could not allocate memory for list of active domains\n"); - goto out; + for (i =3D 0; i < ndomains; i++) { + if (insertGuest(container, domains[i]) < 0) + goto cleanup; } =20 - numIds =3D virConnectListDomains(conn, - idList, - numActiveDomains); - - if (-1 =3D=3D numIds) { - ret =3D -1; - printf("Could not get list of defined domains from hypervisor\n"); - showError(conn); - goto out; + ret =3D 0; + cleanup: + if (domains) { + for (i =3D 0; i < ndomains; i++) + virDomainFree(domains[i]); + free(domains); } - - for (i =3D 0 ; i < numIds ; i++) { - domain =3D virDomainLookupByID(conn, *(idList + i)); - if (NULL =3D=3D domain) { - printf("Failed to lookup domain\n"); - showError(conn); - ret =3D -1; - goto out; - } - - ret =3D insertGuest(container, domain); - - virDomainFree(domain); - - if (-1 =3D=3D ret) - goto out; - } - - /* Inactive domains */ - numDefinedDomains =3D virConnectNumOfDefinedDomains(conn); - if (-1 =3D=3D numDefinedDomains) { - ret =3D -1; - printf("Failed to get number of defined domains\n"); - showError(conn); - goto out; - } - - nameList =3D malloc(sizeof(*nameList) * numDefinedDomains); - - if (NULL =3D=3D nameList) { - ret =3D -1; - printf("Could not allocate memory for list of defined domains\n"); - goto out_inact; - } - - numNames =3D virConnectListDefinedDomains(conn, - nameList, - numDefinedDomains); - - if (-1 =3D=3D numNames) { - ret =3D -1; - printf("Could not get list of defined domains from hypervisor\n"); - showError(conn); - goto out_inact; - } - - for (i =3D 0 ; i < numNames ; i++) { - domain =3D virDomainLookupByName(conn, *(nameList + i)); - if (NULL =3D=3D domain) { - printf("Failed to lookup domain\n"); - showError(conn); - ret =3D -1; - goto out_inact; - } - - ret =3D insertGuest(container, domain); - - virDomainFree(domain); - - if (-1 =3D=3D ret) - goto out_inact; - } - - out_inact: - free(nameList); - out: - free(idList); return ret; } =20 --=20 2.18.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list