From nobody Sun Oct 5 17:36:07 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1612548536; cv=none; d=zohomail.com; s=zohoarc; b=YXykwq6TCoU19EdzCMWY8ecZXm/cxijvZePyxjdzkuFMUiSU+bEpr0u2+N12//bB93zrVt9clZLI2RHvzyeE3eDSWZGVImz+uOS7boUdKgpS23drdlek9Zxhf7gWbq3Ooh8AhCBulxFQ0Ok7wVuaW7Uyu17lz+nblqLCcu3ou0s= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1612548536; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To; bh=bs9xW0n/RJktpGPaSJ5YlIrWl+4Rrz5pL183302BLYk=; b=XBuqrGDjV5hnFXXNT61DuS1t4Lo7wzO0zQ69xnmKl7vSKII55ajBAB39FUGyI6jO0LW806BPV6+tUj2J317pvP6bvmJe5q1XXV5OXw//s/17Hm17nP0lBZZW6vXj15xrbmXSAoWaFQm3tb3lqu+iGgq/kEEzFf2HjcTupR3odw8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1612548536686543.2693476654802; Fri, 5 Feb 2021 10:08:56 -0800 (PST) Received: from localhost ([::1]:41120 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1l85XL-0007Gc-Ak for importer@patchew.org; Fri, 05 Feb 2021 13:08:55 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:50676) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1l84jB-000090-G5 for qemu-devel@nongnu.org; Fri, 05 Feb 2021 12:17:05 -0500 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:49178) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1l84j8-0002qw-At for qemu-devel@nongnu.org; Fri, 05 Feb 2021 12:17:04 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-101-ZAS2o4_NNOSYzQ4YL1sRSQ-1; Fri, 05 Feb 2021 12:16:56 -0500 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 mimecast-mx01.redhat.com (Postfix) with ESMTPS id 83F5E107ACE8; Fri, 5 Feb 2021 17:16:41 +0000 (UTC) Received: from blue.redhat.com (ovpn-112-253.phx2.redhat.com [10.3.112.253]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1BCD719CA8; Fri, 5 Feb 2021 17:16:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1612545421; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=bs9xW0n/RJktpGPaSJ5YlIrWl+4Rrz5pL183302BLYk=; b=TOghsxQem3jEjXAV3sGEM72/nOveOMIKshR0AkhGazHk0YPW4mYf08NzrwL+Jk6cbg8Pyi xIdF9pBkGYI4rJpvQBL8nuL99T784NHD02YOMh3DXmMWBmajW1Y256TAEZb7v7rEzElzGB RiMf0pVfZ15NWOqnEkf1C2pRFt5zY10= X-MC-Unique: ZAS2o4_NNOSYzQ4YL1sRSQ-1 From: Eric Blake To: qemu-devel@nongnu.org Subject: [PATCH v5] qga: Utilize QAPI_LIST_APPEND in qmp_guest_network_get_interfaces Date: Fri, 5 Feb 2021 11:16:34 -0600 Message-Id: <20210205171634.1491258-1-eblake@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=eblake@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=216.205.24.124; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -30 X-Spam_score: -3.1 X-Spam_bar: --- X-Spam_report: (-3.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.352, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Michael Roth , armbru@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" I found another spot that can benefit from using our macros instead of open-coding qapi list creation. Signed-off-by: Eric Blake Reviewed-by: Markus Armbruster --- Marked v5 because it missed commit 95b3a8c8a82, which was v4: https://lists.gnu.org/archive/html/qemu-devel/2021-01/msg06574.html qga/commands-posix.c | 75 ++++++++++++++++++-------------------------- 1 file changed, 30 insertions(+), 45 deletions(-) diff --git a/qga/commands-posix.c b/qga/commands-posix.c index 8dd94a33144b..3f18df1bb6d1 100644 --- a/qga/commands-posix.c +++ b/qga/commands-posix.c @@ -2153,17 +2153,17 @@ void qmp_guest_suspend_hybrid(Error **errp) guest_suspend(SUSPEND_MODE_HYBRID, errp); } -static GuestNetworkInterfaceList * +static GuestNetworkInterface * guest_find_interface(GuestNetworkInterfaceList *head, const char *name) { for (; head; head =3D head->next) { if (strcmp(head->value->name, name) =3D=3D 0) { - break; + return head->value; } } - return head; + return NULL; } static int guest_get_network_stats(const char *name, @@ -2232,7 +2232,7 @@ static int guest_get_network_stats(const char *name, */ GuestNetworkInterfaceList *qmp_guest_network_get_interfaces(Error **errp) { - GuestNetworkInterfaceList *head =3D NULL, *cur_item =3D NULL; + GuestNetworkInterfaceList *head =3D NULL, **tail =3D &head; struct ifaddrs *ifap, *ifa; if (getifaddrs(&ifap) < 0) { @@ -2241,9 +2241,10 @@ GuestNetworkInterfaceList *qmp_guest_network_get_int= erfaces(Error **errp) } for (ifa =3D ifap; ifa; ifa =3D ifa->ifa_next) { - GuestNetworkInterfaceList *info; - GuestIpAddressList **address_list =3D NULL, *address_item =3D NULL; - GuestNetworkInterfaceStat *interface_stat =3D NULL; + GuestNetworkInterface *info; + GuestIpAddressList **address_tail; + GuestIpAddress *address_item =3D NULL; + GuestNetworkInterfaceStat *interface_stat =3D NULL; char addr4[INET_ADDRSTRLEN]; char addr6[INET6_ADDRSTRLEN]; int sock; @@ -2257,19 +2258,12 @@ GuestNetworkInterfaceList *qmp_guest_network_get_in= terfaces(Error **errp) if (!info) { info =3D g_malloc0(sizeof(*info)); - info->value =3D g_malloc0(sizeof(*info->value)); - info->value->name =3D g_strdup(ifa->ifa_name); + info->name =3D g_strdup(ifa->ifa_name); - if (!cur_item) { - head =3D cur_item =3D info; - } else { - cur_item->next =3D info; - cur_item =3D info; - } + QAPI_LIST_APPEND(tail, info); } - if (!info->value->has_hardware_address && - ifa->ifa_flags & SIOCGIFHWADDR) { + if (!info->has_hardware_address && ifa->ifa_flags & SIOCGIFHWADDR)= { /* we haven't obtained HW address yet */ sock =3D socket(PF_INET, SOCK_STREAM, 0); if (sock =3D=3D -1) { @@ -2278,7 +2272,7 @@ GuestNetworkInterfaceList *qmp_guest_network_get_inte= rfaces(Error **errp) } memset(&ifr, 0, sizeof(ifr)); - pstrcpy(ifr.ifr_name, IF_NAMESIZE, info->value->name); + pstrcpy(ifr.ifr_name, IF_NAMESIZE, info->name); if (ioctl(sock, SIOCGIFHWADDR, &ifr) =3D=3D -1) { error_setg_errno(errp, errno, "failed to get MAC address of %s", @@ -2290,13 +2284,13 @@ GuestNetworkInterfaceList *qmp_guest_network_get_in= terfaces(Error **errp) close(sock); mac_addr =3D (unsigned char *) &ifr.ifr_hwaddr.sa_data; - info->value->hardware_address =3D + info->hardware_address =3D g_strdup_printf("%02x:%02x:%02x:%02x:%02x:%02x", (int) mac_addr[0], (int) mac_addr[1], (int) mac_addr[2], (int) mac_addr[3], (int) mac_addr[4], (int) mac_addr[5]); - info->value->has_hardware_address =3D true; + info->has_hardware_address =3D true; } if (ifa->ifa_addr && @@ -2309,15 +2303,14 @@ GuestNetworkInterfaceList *qmp_guest_network_get_in= terfaces(Error **errp) } address_item =3D g_malloc0(sizeof(*address_item)); - address_item->value =3D g_malloc0(sizeof(*address_item->value)= ); - address_item->value->ip_address =3D g_strdup(addr4); - address_item->value->ip_address_type =3D GUEST_IP_ADDRESS_TYPE= _IPV4; + address_item->ip_address =3D g_strdup(addr4); + address_item->ip_address_type =3D GUEST_IP_ADDRESS_TYPE_IPV4; if (ifa->ifa_netmask) { /* Count the number of set bits in netmask. * This is safe as '1' and '0' cannot be shuffled in netma= sk. */ p =3D &((struct sockaddr_in *)ifa->ifa_netmask)->sin_addr; - address_item->value->prefix =3D ctpop32(((uint32_t *) p)[0= ]); + address_item->prefix =3D ctpop32(((uint32_t *) p)[0]); } } else if (ifa->ifa_addr && ifa->ifa_addr->sa_family =3D=3D AF_INET6) { @@ -2329,15 +2322,14 @@ GuestNetworkInterfaceList *qmp_guest_network_get_in= terfaces(Error **errp) } address_item =3D g_malloc0(sizeof(*address_item)); - address_item->value =3D g_malloc0(sizeof(*address_item->value)= ); - address_item->value->ip_address =3D g_strdup(addr6); - address_item->value->ip_address_type =3D GUEST_IP_ADDRESS_TYPE= _IPV6; + address_item->ip_address =3D g_strdup(addr6); + address_item->ip_address_type =3D GUEST_IP_ADDRESS_TYPE_IPV6; if (ifa->ifa_netmask) { /* Count the number of set bits in netmask. * This is safe as '1' and '0' cannot be shuffled in netma= sk. */ p =3D &((struct sockaddr_in6 *)ifa->ifa_netmask)->sin6_add= r; - address_item->value->prefix =3D + address_item->prefix =3D ctpop32(((uint32_t *) p)[0]) + ctpop32(((uint32_t *) p)[1]) + ctpop32(((uint32_t *) p)[2]) + @@ -2349,29 +2341,22 @@ GuestNetworkInterfaceList *qmp_guest_network_get_in= terfaces(Error **errp) continue; } - address_list =3D &info->value->ip_addresses; - - while (*address_list && (*address_list)->next) { - address_list =3D &(*address_list)->next; - } - - if (!*address_list) { - *address_list =3D address_item; - } else { - (*address_list)->next =3D address_item; + address_tail =3D &info->ip_addresses; + while (*address_tail) { + address_tail =3D &(*address_tail)->next; } + QAPI_LIST_APPEND(address_tail, address_item); - info->value->has_ip_addresses =3D true; + info->has_ip_addresses =3D true; - if (!info->value->has_statistics) { + if (!info->has_statistics) { interface_stat =3D g_malloc0(sizeof(*interface_stat)); - if (guest_get_network_stats(info->value->name, - interface_stat) =3D=3D -1) { - info->value->has_statistics =3D false; + if (guest_get_network_stats(info->name, interface_stat) =3D=3D= -1) { + info->has_statistics =3D false; g_free(interface_stat); } else { - info->value->statistics =3D interface_stat; - info->value->has_statistics =3D true; + info->statistics =3D interface_stat; + info->has_statistics =3D true; } } } --=20 2.30.0