From nobody Sat May 4 20:55:56 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) client-ip=216.205.24.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1600331595; cv=none; d=zohomail.com; s=zohoarc; b=NDZqM2OIvRMFagvuaSF6iBsAydvKd6fNILrqtzruf4NuVZWHdOHKp3QiSFrOSkF/pv/m12p+iNFlsFVIC14sslSMYvGryEhoOR25mhz8oNLbe/C7zp9hf/ZnbgFj41/gzHIHI/JiIsEqXNVOOQkeySElB50GT2eZj5ISu+CPZqE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1600331595; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=CSokFSDZjRJ0N40KyPmC/MwVRXFszSOHWWOe4MDUue0=; b=HsK2DSWC6lG4qZhcs0E/bFTwOq293QpjmTwOlhTDEeMIcHDFr1T0sSIWHy58pH9sy4+CHH2153h+8KBlXlVg021luZUcdL7ulMP19lQp9etLRuulilUUV6ZtgO3bwI438Jmuq4k9T4F2kb5AeN0VV/n1s253JfvsMbjXFmu9XI8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by mx.zohomail.com with SMTPS id 1600331595556911.7568772641353; Thu, 17 Sep 2020 01:33:15 -0700 (PDT) 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-42-5Wr_ZLH7OJWu6XQ3F-nTPA-1; Thu, 17 Sep 2020 04:31:40 -0400 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 mimecast-mx01.redhat.com (Postfix) with ESMTPS id 9979F188C12A; Thu, 17 Sep 2020 08:31:35 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 772477514A; Thu, 17 Sep 2020 08:31:35 +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 1CF41183D048; Thu, 17 Sep 2020 08:31:35 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 08H8TtZ0004055 for ; Thu, 17 Sep 2020 04:29:55 -0400 Received: by smtp.corp.redhat.com (Postfix) id 862F15DEC7; Thu, 17 Sep 2020 08:29:55 +0000 (UTC) Received: from antique-work.redhat.com (unknown [10.40.195.159]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9D6875DEC4 for ; Thu, 17 Sep 2020 08:29:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1600331594; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=CSokFSDZjRJ0N40KyPmC/MwVRXFszSOHWWOe4MDUue0=; b=JQttps/lrMAQZVm/xEEBbiz3AHGITtV+NLA35lAeoHu9wLv6OYgxgiafvATweCE59v/FNa WRIGP4+86/MsfWRVCXaUDDi2x6si7Bvl3aI4oTgMejAPHKpN9sBh0doO+uDuUMGQp3aFKc sazCds65nSkYSHaup1VRbjURbcRa+Es= X-MC-Unique: 5Wr_ZLH7OJWu6XQ3F-nTPA-1 From: Pavel Hrdina To: libvir-list@redhat.com Subject: [libvirt PATCH 01/14] remove HAL node device driver Date: Thu, 17 Sep 2020 10:29:36 +0200 Message-Id: <5b5ac4b348fea2a84ed8d76500f2a89ef40ca7e1.1600331276.git.phrdina@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com 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: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" There was one attempt a year ago done by me to drop HAL [1] but it was never resolved. There was another time when Dan suggested to drop HAL driver [2] but it was decided to keep it around in case device assignment will be implemented for FreeBSD and the fact that virt-manager uses node device driver [3]. I checked git history and code and it doesn't look like bhyve supports device assignment so from that POV it should not block removing HAL. The argument about virt-manager is not strong as well because libvirt installed from FreeBSD packages doesn't have HAL support so it will not affect these users as well [4]. The only users affected by this change would be the ones compiling libvirt from GIT on FreeBSD. I looked into alternatives and there is libudev-devd package on FreeBSD but unfortunately it doesn't work as it doesn't list any devices when used with libvirt. It provides libudev APIs using devd. I also looked into devd directly and it provides some APIs but there are no APIs for device monitoring and events so that would have to be somehow done by libvirt. Main motivation for dropping HAL support is to replace libdbus with GLib dbus implementation and it cannot be done with HAL driver present in libvirt because HAL APIs heavily depends on symbols provided by libdbus. [1] [2] [3] [4] Signed-off-by: Pavel Hrdina Reviewed-by: Michal Privoznik Reviewed-by: Neal Gompa --- meson.build | 9 +- meson_options.txt | 1 - po/POTFILES.in | 1 - src/node_device/meson.build | 5 - src/node_device/node_device_driver.c | 10 +- src/node_device/node_device_driver.h | 5 - src/node_device/node_device_hal.c | 843 --------------------------- src/node_device/node_device_hal.h | 22 - 8 files changed, 3 insertions(+), 893 deletions(-) delete mode 100644 src/node_device/node_device_hal.c delete mode 100644 src/node_device/node_device_hal.h diff --git a/meson.build b/meson.build index 195d7cd784..3d15b4ee34 100644 --- a/meson.build +++ b/meson.build @@ -1077,12 +1077,6 @@ glusterfs_dep =3D dependency('glusterfs-api', versio= n: '>=3D' + glusterfs_version, r gnutls_version =3D '3.2.0' gnutls_dep =3D dependency('gnutls', version: '>=3D' + gnutls_version) =20 -hal_version =3D '0.5.0' -hal_dep =3D dependency('hal', version: '>=3D' + hal_version, required: get= _option('hal')) -if hal_dep.found() - conf.set('WITH_HAL', 1) -endif - # Check for BSD kvm (kernel memory interface) if host_machine.system() =3D=3D 'freebsd' kvm_dep =3D cc.find_library('kvm') @@ -1728,7 +1722,7 @@ if not get_option('driver_network').disabled() and co= nf.has('WITH_LIBVIRTD') and conf.set('WITH_NETWORK', 1) endif =20 -if hal_dep.found() or udev_dep.found() +if udev_dep.found() conf.set('WITH_NODE_DEVICES', 1) endif =20 @@ -2433,7 +2427,6 @@ libs_summary =3D { 'glib_dep': glib_dep.found(), 'glusterfs': glusterfs_dep.found(), 'gnutls': gnutls_dep.found(), - 'hal': hal_dep.found(), 'libiscsi': libiscsi_dep.found(), 'libnl': libnl_dep.found(), 'libpcap': libpcap_dep.found(), diff --git a/meson_options.txt b/meson_options.txt index 7838630c1e..c8886e1430 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -23,7 +23,6 @@ option('firewalld', type: 'feature', value: 'auto', descr= iption: 'firewalld supp option('firewalld_zone', type: 'feature', value: 'auto', description: 'whe= ther to install firewalld libvirt zone') option('fuse', type: 'feature', value: 'auto', description: 'fuse support') option('glusterfs', type: 'feature', value: 'auto', description: 'glusterf= s support') -option('hal', type: 'feature', value: 'auto', description: 'hal support') option('libiscsi', type: 'feature', value: 'auto', description: 'libiscsi = support') option('libpcap', type: 'feature', value: 'auto', description: 'libpcap su= pport') option('libssh', type: 'feature', value: 'auto', description: 'libssh supp= ort') diff --git a/po/POTFILES.in b/po/POTFILES.in index 471af30b89..4ab8832b37 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -130,7 +130,6 @@ @SRCDIR@src/network/bridge_driver_linux.c @SRCDIR@src/network/leaseshelper.c @SRCDIR@src/node_device/node_device_driver.c -@SRCDIR@src/node_device/node_device_hal.c @SRCDIR@src/node_device/node_device_udev.c @SRCDIR@src/nwfilter/nwfilter_dhcpsnoop.c @SRCDIR@src/nwfilter/nwfilter_driver.c diff --git a/src/node_device/meson.build b/src/node_device/meson.build index 5953c6b8ed..c4e4c3906b 100644 --- a/src/node_device/meson.build +++ b/src/node_device/meson.build @@ -4,10 +4,6 @@ node_device_driver_sources =3D [ =20 stateful_driver_source_files +=3D files(node_device_driver_sources) =20 -if conf.has('WITH_HAL') - node_device_driver_sources +=3D 'node_device_hal.c' -endif - if conf.has('WITH_UDEV') node_device_driver_sources +=3D 'node_device_udev.c' endif @@ -22,7 +18,6 @@ if conf.has('WITH_NODE_DEVICES') ], dependencies: [ access_dep, - hal_dep, libnl_dep, pciaccess_dep, src_dep, diff --git a/src/node_device/node_device_driver.c b/src/node_device/node_de= vice_driver.c index 375c1fff32..f4b140bef4 100644 --- a/src/node_device/node_device_driver.c +++ b/src/node_device/node_device_driver.c @@ -36,7 +36,6 @@ #include "node_device_conf.h" #include "node_device_event.h" #include "node_device_driver.h" -#include "node_device_hal.h" #include "node_device_util.h" #include "virvhba.h" #include "viraccessapicheck.h" @@ -98,14 +97,13 @@ int nodeConnectIsAlive(virConnectPtr conn G_GNUC_UNUSED) return 1; } =20 -#if defined (__linux__) && ( defined (WITH_HAL) || defined(WITH_UDEV)) +#if defined (__linux__) && defined(WITH_UDEV) /* NB: It was previously believed that changes in driver name were * relayed to libvirt as "change" events by udev, and the udev event * notification is setup to recognize such events and effectively * recreate the device entry in the cache. However, neither the kernel * nor udev sends such an event, so it is necessary to manually update - * the driver name for a device each time its entry is used, both for - * udev *and* HAL backends. + * the driver name for a device each time its entry is used. */ static int nodeDeviceUpdateDriverName(virNodeDeviceDefPtr def) @@ -935,9 +933,5 @@ nodedevRegister(void) { #ifdef WITH_UDEV return udevNodeRegister(); -#else -# ifdef WITH_HAL - return halNodeRegister(); -# endif #endif } diff --git a/src/node_device/node_device_driver.h b/src/node_device/node_de= vice_driver.h index be5d397828..2113d2b0a5 100644 --- a/src/node_device/node_device_driver.h +++ b/src/node_device/node_device_driver.h @@ -28,11 +28,6 @@ =20 #define LINUX_NEW_DEVICE_WAIT_TIME 60 =20 -#ifdef WITH_HAL -int -halNodeRegister(void); -#endif - #ifdef WITH_UDEV int udevNodeRegister(void); diff --git a/src/node_device/node_device_hal.c b/src/node_device/node_devic= e_hal.c deleted file mode 100644 index 53a49ba2aa..0000000000 --- a/src/node_device/node_device_hal.c +++ /dev/null @@ -1,843 +0,0 @@ -/* - * node_device_hal.c: node device enumeration - HAL-based implementation - * - * Copyright (C) 2011-2015 Red Hat, Inc. - * Copyright (C) 2008 Virtual Iron Software, Inc. - * Copyright (C) 2008 David F. Lively - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library. If not, see - * . - */ - -#include - -#include - -#include "node_device_conf.h" -#include "node_device_driver.h" -#include "node_device_hal.h" -#include "virerror.h" -#include "driver.h" -#include "datatypes.h" -#include "viralloc.h" -#include "viruuid.h" -#include "virfile.h" -#include "virpci.h" -#include "virpidfile.h" -#include "virlog.h" -#include "virdbus.h" -#include "virstring.h" -#include "virutil.h" - -#include "configmake.h" - -#define VIR_FROM_THIS VIR_FROM_NODEDEV - -VIR_LOG_INIT("node_device.node_device_hal"); - -/* - * Host device enumeration (HAL implementation) - */ - -#define DRV_STATE_HAL_CTX(ds) ((LibHalContext *)((ds)->privateData)) - - -static const char * -hal_name(const char *udi) -{ - const char *name =3D strrchr(udi, '/'); - if (name) - return name + 1; - return udi; -} - - -static int -get_str_prop(LibHalContext *ctxt, const char *udi, - const char *prop, char **val_p) -{ - char *val =3D libhal_device_get_property_string(ctxt, udi, prop, NULL); - - if (val) { - if (*val) { - *val_p =3D val; - return 0; - } else { - /* Treat empty strings as NULL values */ - VIR_FREE(val); - } - } - - return -1; -} - -static int -get_int_prop(LibHalContext *ctxt, const char *udi, - const char *prop, int *val_p) -{ - DBusError err; - int val; - int rv; - - dbus_error_init(&err); - val =3D libhal_device_get_property_int(ctxt, udi, prop, &err); - rv =3D dbus_error_is_set(&err); - dbus_error_free(&err); - if (rv =3D=3D 0) - *val_p =3D val; - - return rv; -} - -static int -get_bool_prop(LibHalContext *ctxt, const char *udi, - const char *prop, int *val_p) -{ - DBusError err; - int val; - int rv; - - dbus_error_init(&err); - val =3D libhal_device_get_property_bool(ctxt, udi, prop, &err); - rv =3D dbus_error_is_set(&err); - dbus_error_free(&err); - if (rv =3D=3D 0) - *val_p =3D val; - - return rv; -} - -static int -get_uint64_prop(LibHalContext *ctxt, const char *udi, - const char *prop, unsigned long long *val_p) -{ - DBusError err; - unsigned long long val; - int rv; - - dbus_error_init(&err); - val =3D libhal_device_get_property_uint64(ctxt, udi, prop, &err); - rv =3D dbus_error_is_set(&err); - dbus_error_free(&err); - if (rv =3D=3D 0) - *val_p =3D val; - - return rv; -} - -static int -gather_pci_cap(LibHalContext *ctx, const char *udi, - virNodeDevCapDataPtr d) -{ - char *sysfs_path; - - if (get_str_prop(ctx, udi, "pci.linux.sysfs_path", &sysfs_path) =3D=3D= 0) { - char *p =3D strrchr(sysfs_path, '/'); - if (p) { - ignore_value(virStrToLong_ui(p+1, &p, 16, &d->pci_dev.domain)); - ignore_value(virStrToLong_ui(p+1, &p, 16, &d->pci_dev.bus)); - ignore_value(virStrToLong_ui(p+1, &p, 16, &d->pci_dev.slot)); - ignore_value(virStrToLong_ui(p+1, &p, 16, &d->pci_dev.function= )); - } - - if (virNodeDeviceGetPCIDynamicCaps(sysfs_path, &d->pci_dev) < 0) { - VIR_FREE(sysfs_path); - return -1; - } - VIR_FREE(sysfs_path); - } - - (void)get_int_prop(ctx, udi, "pci.vendor_id", (int *)&d->pci_dev.vendo= r); - if (get_str_prop(ctx, udi, "pci.vendor", &d->pci_dev.vendor_name) !=3D= 0) - (void)get_str_prop(ctx, udi, "info.vendor", &d->pci_dev.vendor_nam= e); - (void)get_int_prop(ctx, udi, "pci.product_id", (int *)&d->pci_dev.prod= uct); - if (get_str_prop(ctx, udi, "pci.product", &d->pci_dev.product_name) != =3D 0) - (void)get_str_prop(ctx, udi, "info.product", &d->pci_dev.product_n= ame); - - return 0; -} - - -static int -gather_usb_cap(LibHalContext *ctx, const char *udi, - virNodeDevCapDataPtr d) -{ - (void)get_int_prop(ctx, udi, "usb.interface.number", - (int *)&d->usb_if.number); - (void)get_int_prop(ctx, udi, "usb.interface.class", - (int *)&d->usb_if.klass); - (void)get_int_prop(ctx, udi, "usb.interface.subclass", - (int *)&d->usb_if.subclass); - (void)get_int_prop(ctx, udi, "usb.interface.protocol", - (int *)&d->usb_if.protocol); - (void)get_str_prop(ctx, udi, "usb.interface.description", - &d->usb_if.description); - return 0; -} - - -static int -gather_usb_device_cap(LibHalContext *ctx, const char *udi, - virNodeDevCapDataPtr d) -{ - (void)get_int_prop(ctx, udi, "usb_device.bus_number", - (int *)&d->usb_dev.bus); - (void)get_int_prop(ctx, udi, "usb_device.linux.device_number", - (int *)&d->usb_dev.device); - (void)get_int_prop(ctx, udi, "usb_device.vendor_id", - (int *)&d->usb_dev.vendor); - if (get_str_prop(ctx, udi, "usb_device.vendor", - &d->usb_dev.vendor_name) !=3D 0) - (void)get_str_prop(ctx, udi, "info.vendor", &d->usb_dev.vendor_nam= e); - (void)get_int_prop(ctx, udi, "usb_device.product_id", - (int *)&d->usb_dev.product); - if (get_str_prop(ctx, udi, "usb_device.product", - &d->usb_dev.product_name) !=3D 0) - (void)get_str_prop(ctx, udi, "info.product", &d->usb_dev.product_n= ame); - return 0; -} - - -static int -gather_net_cap(LibHalContext *ctx, const char *udi, - virNodeDevCapDataPtr d) -{ - unsigned long long dummy; - (void)get_str_prop(ctx, udi, "net.interface", &d->net.ifname); - (void)get_str_prop(ctx, udi, "net.address", &d->net.address); - if (get_uint64_prop(ctx, udi, "net.80203.mac_address", - &dummy) =3D=3D 0) - d->net.subtype =3D VIR_NODE_DEV_CAP_NET_80203; - else if (get_uint64_prop(ctx, udi, "net.80211.mac_address", - &dummy) =3D=3D 0) - d->net.subtype =3D VIR_NODE_DEV_CAP_NET_80211; - else - d->net.subtype =3D VIR_NODE_DEV_CAP_NET_LAST; - - return 0; -} - - -static int -gather_scsi_host_cap(LibHalContext *ctx, const char *udi, - virNodeDevCapDataPtr d) -{ - (void)get_int_prop(ctx, udi, "scsi_host.host", (int *)&d->scsi_host.ho= st); - - return virNodeDeviceGetSCSIHostCaps(&d->scsi_host); -} - - -static int -gather_scsi_cap(LibHalContext *ctx, const char *udi, - virNodeDevCapDataPtr d) -{ - (void)get_int_prop(ctx, udi, "scsi.host", (int *)&d->scsi.host); - (void)get_int_prop(ctx, udi, "scsi.bus", (int *)&d->scsi.bus); - (void)get_int_prop(ctx, udi, "scsi.target", (int *)&d->scsi.target); - (void)get_int_prop(ctx, udi, "scsi.lun", (int *)&d->scsi.lun); - (void)get_str_prop(ctx, udi, "scsi.type", &d->scsi.type); - return 0; -} - - -static int -gather_storage_cap(LibHalContext *ctx, const char *udi, - virNodeDevCapDataPtr d) -{ - int val; - (void)get_str_prop(ctx, udi, "block.device", &d->storage.block); - (void)get_str_prop(ctx, udi, "storage.bus", &d->storage.bus); - (void)get_str_prop(ctx, udi, "storage.drive_type", &d->storage.drive_t= ype); - (void)get_str_prop(ctx, udi, "storage.model", &d->storage.model); - (void)get_str_prop(ctx, udi, "storage.vendor", &d->storage.vendor); - (void)get_str_prop(ctx, udi, "storage.serial", &d->storage.serial); - if (get_bool_prop(ctx, udi, "storage.removable", &val) =3D=3D 0 && val= ) { - d->storage.flags |=3D VIR_NODE_DEV_CAP_STORAGE_REMOVABLE; - if (get_bool_prop(ctx, udi, "storage.removable.media_available", - &val) =3D=3D 0 && val) { - d->storage.flags |=3D - VIR_NODE_DEV_CAP_STORAGE_REMOVABLE_MEDIA_AVAILABLE; - (void)get_uint64_prop(ctx, udi, "storage.removable.media_size", - &d->storage.removable_media_size); - } - } else { - (void)get_uint64_prop(ctx, udi, "storage.size", &d->storage.size); - } - if (get_bool_prop(ctx, udi, "storage.hotpluggable", &val) =3D=3D 0 && = val) - d->storage.flags |=3D VIR_NODE_DEV_CAP_STORAGE_HOTPLUGGABLE; - return 0; -} - -static int -gather_scsi_generic_cap(LibHalContext *ctx, const char *udi, - virNodeDevCapDataPtr d) -{ - (void)get_str_prop(ctx, udi, "scsi_generic.device", &d->sg.path); - return 0; -} - - -static int -gather_system_cap(LibHalContext *ctx, const char *udi, - virNodeDevCapDataPtr d) -{ - char *uuidstr; - - (void)get_str_prop(ctx, udi, "system.product", &d->system.product_name= ); - (void)get_str_prop(ctx, udi, "system.hardware.vendor", - &d->system.hardware.vendor_name); - (void)get_str_prop(ctx, udi, "system.hardware.version", - &d->system.hardware.version); - (void)get_str_prop(ctx, udi, "system.hardware.serial", - &d->system.hardware.serial); - if (get_str_prop(ctx, udi, "system.hardware.uuid", &uuidstr) =3D=3D 0)= { - ignore_value(virUUIDParse(uuidstr, d->system.hardware.uuid)); - VIR_FREE(uuidstr); - } - (void)get_str_prop(ctx, udi, "system.firmware.vendor", - &d->system.firmware.vendor_name); - (void)get_str_prop(ctx, udi, "system.firmware.version", - &d->system.firmware.version); - (void)get_str_prop(ctx, udi, "system.firmware.release_date", - &d->system.firmware.release_date); - return 0; -} - - -struct _caps_tbl_entry { - const char *cap_name; - virNodeDevCapType type; - int (*gather_fn)(LibHalContext *ctx, - const char *udi, - virNodeDevCapDataPtr data); -}; - -typedef struct _caps_tbl_entry caps_tbl_entry; - -static caps_tbl_entry caps_tbl[] =3D { - { "system", VIR_NODE_DEV_CAP_SYSTEM, gather_system_cap }, - { "pci", VIR_NODE_DEV_CAP_PCI_DEV, gather_pci_cap }, - { "usb", VIR_NODE_DEV_CAP_USB_INTERFACE, gather_usb_cap }, - { "usb_device", VIR_NODE_DEV_CAP_USB_DEV, gather_usb_device_cap = }, - { "net", VIR_NODE_DEV_CAP_NET, gather_net_cap }, - { "scsi_host", VIR_NODE_DEV_CAP_SCSI_HOST, gather_scsi_host_cap }, - { "scsi", VIR_NODE_DEV_CAP_SCSI, gather_scsi_cap }, - { "storage", VIR_NODE_DEV_CAP_STORAGE, gather_storage_cap }, - { "scsi_generic", VIR_NODE_DEV_CAP_SCSI_GENERIC, gather_scsi_generic_c= ap }, -}; - - -/* qsort/bsearch string comparator */ -static int -cmpstringp(const void *p1, const void *p2) -{ - /* from man 3 qsort */ - return strcmp(* (char * const *) p1, * (char * const *) p2); -} - - -static int -gather_capability(LibHalContext *ctx, const char *udi, - const char *cap_name, - virNodeDevCapsDefPtr *caps_p) -{ - caps_tbl_entry *entry; - - entry =3D bsearch(&cap_name, caps_tbl, G_N_ELEMENTS(caps_tbl), - sizeof(caps_tbl[0]), cmpstringp); - - if (entry) { - virNodeDevCapsDefPtr caps; - if (VIR_ALLOC(caps) < 0) - return ENOMEM; - caps->data.type =3D entry->type; - if (entry->gather_fn) { - int rv =3D (*entry->gather_fn)(ctx, udi, &caps->data); - if (rv !=3D 0) { - virNodeDevCapsDefFree(caps); - return rv; - } - } - caps->next =3D *caps_p; - *caps_p =3D caps; - } - - return 0; -} - - -static int -gather_capabilities(LibHalContext *ctx, const char *udi, - virNodeDevCapsDefPtr *caps_p) -{ - char *bus_name =3D NULL; - virNodeDevCapsDefPtr caps =3D NULL; - char **hal_cap_names =3D NULL; - int rv; - size_t i; - - if (STREQ(udi, "/org/freedesktop/Hal/devices/computer")) { - rv =3D gather_capability(ctx, udi, "system", &caps); - if (rv !=3D 0) - goto failure; - } - - if (get_str_prop(ctx, udi, "info.subsystem", &bus_name) =3D=3D 0 || - get_str_prop(ctx, udi, "linux.subsystem", &bus_name) =3D=3D 0) { - rv =3D gather_capability(ctx, udi, bus_name, &caps); - if (rv !=3D 0) - goto failure; - } - - hal_cap_names =3D libhal_device_get_property_strlist(ctx, udi, - "info.capabilities", - NULL); - if (hal_cap_names) { - for (i =3D 0; hal_cap_names[i]; i++) { - if (! (bus_name && STREQ(hal_cap_names[i], bus_name))) { - rv =3D gather_capability(ctx, udi, hal_cap_names[i], &caps= ); - if (rv !=3D 0) - goto failure; - } - } - for (i =3D 0; hal_cap_names[i]; i++) - VIR_FREE(hal_cap_names[i]); - VIR_FREE(hal_cap_names); - } - VIR_FREE(bus_name); - - *caps_p =3D caps; - return 0; - - failure: - VIR_FREE(bus_name); - if (hal_cap_names) { - for (i =3D 0; hal_cap_names[i]; i++) - VIR_FREE(hal_cap_names[i]); - VIR_FREE(hal_cap_names); - } - while (caps) { - virNodeDevCapsDefPtr next =3D caps->next; - virNodeDevCapsDefFree(caps); - caps =3D next; - } - return rv; -} - -static void -dev_create(const char *udi) -{ - LibHalContext *ctx; - char *parent_key =3D NULL; - virNodeDeviceObjPtr obj =3D NULL; - virNodeDeviceDefPtr def =3D NULL; - virNodeDeviceDefPtr objdef; - const char *name =3D hal_name(udi); - int rv; - char *devicePath =3D NULL; - - nodeDeviceLock(); - ctx =3D DRV_STATE_HAL_CTX(driver); - - if (VIR_ALLOC(def) < 0) - goto failure; - - def->name =3D g_strdup(name); - - if (get_str_prop(ctx, udi, "info.parent", &parent_key) =3D=3D 0) { - def->parent =3D g_strdup(hal_name(parent_key)); - VIR_FREE(parent_key); - } - - rv =3D gather_capabilities(ctx, udi, &def->caps); - if (rv !=3D 0) goto failure; - - if (def->caps =3D=3D NULL) - goto cleanup; - - /* Some devices don't have a path in sysfs, so ignore failure */ - (void)get_str_prop(ctx, udi, "linux.sysfs_path", &devicePath); - - if (!(obj =3D virNodeDeviceObjListAssignDef(driver->devs, def))) { - VIR_FREE(devicePath); - goto failure; - } - objdef =3D virNodeDeviceObjGetDef(obj); - - objdef->sysfs_path =3D devicePath; - - virNodeDeviceObjEndAPI(&obj); - - nodeDeviceUnlock(); - return; - - failure: - VIR_DEBUG("FAILED TO ADD dev %s", name); - cleanup: - virNodeDeviceDefFree(def); - nodeDeviceUnlock(); -} - -static void -dev_refresh(const char *udi) -{ - const char *name =3D hal_name(udi); - virNodeDeviceObjPtr obj; - - if ((obj =3D virNodeDeviceObjListFindByName(driver->devs, name))) { - /* Simply "rediscover" device -- incrementally handling changes - * to sub-capabilities (like net.80203) is nasty ... so avoid it. - */ - virNodeDeviceObjListRemove(driver->devs, obj); - virObjectUnref(obj); - dev_create(udi); - } else { - VIR_DEBUG("no device named %s", name); - } -} - -static void -device_added(LibHalContext *ctx G_GNUC_UNUSED, - const char *udi) -{ - VIR_DEBUG("%s", hal_name(udi)); - dev_create(udi); -} - - -static void -device_removed(LibHalContext *ctx G_GNUC_UNUSED, - const char *udi) -{ - const char *name =3D hal_name(udi); - virNodeDeviceObjPtr obj; - - obj =3D virNodeDeviceObjListFindByName(driver->devs, name); - VIR_DEBUG("%s", name); - if (obj) - virNodeDeviceObjListRemove(driver->devs, obj); - else - VIR_DEBUG("no device named %s", name); - virObjectUnref(obj); -} - - -static void -device_cap_added(LibHalContext *ctx, - const char *udi, const char *cap) -{ - const char *name =3D hal_name(udi); - virNodeDeviceObjPtr obj; - virNodeDeviceDefPtr def; - - VIR_DEBUG("%s %s", cap, name); - if ((obj =3D virNodeDeviceObjListFindByName(driver->devs, name))) { - def =3D virNodeDeviceObjGetDef(obj); - (void)gather_capability(ctx, udi, cap, &def->caps); - virNodeDeviceObjEndAPI(&obj); - } else { - VIR_DEBUG("no device named %s", name); - } -} - - -static void -device_cap_lost(LibHalContext *ctx G_GNUC_UNUSED, - const char *udi, - const char *cap) -{ - const char *name =3D hal_name(udi); - VIR_DEBUG("%s %s", cap, name); - - dev_refresh(udi); -} - - -static void -device_prop_modified(LibHalContext *ctx G_GNUC_UNUSED, - const char *udi, - const char *key, - dbus_bool_t is_removed G_GNUC_UNUSED, - dbus_bool_t is_added G_GNUC_UNUSED) -{ - const char *name =3D hal_name(udi); - VIR_DEBUG("%s %s", name, key); - - dev_refresh(udi); -} - - -static int -nodeStateInitialize(bool privileged G_GNUC_UNUSED, - const char *root, - virStateInhibitCallback callback G_GNUC_UNUSED, - void *opaque G_GNUC_UNUSED) -{ - LibHalContext *hal_ctx =3D NULL; - char **udi =3D NULL; - int num_devs; - size_t i; - int ret =3D VIR_DRV_STATE_INIT_ERROR; - DBusConnection *sysbus; - DBusError err; - - if (root !=3D NULL) { - virReportError(VIR_ERR_INVALID_ARG, "%s", - _("Driver does not support embedded mode")); - return -1; - } - - /* Ensure caps_tbl is sorted by capability name */ - qsort(caps_tbl, G_N_ELEMENTS(caps_tbl), sizeof(caps_tbl[0]), - cmpstringp); - - if (VIR_ALLOC(driver) < 0) - return VIR_DRV_STATE_INIT_ERROR; - - driver->lockFD =3D -1; - if (virMutexInit(&driver->lock) < 0) { - VIR_FREE(driver); - return VIR_DRV_STATE_INIT_ERROR; - } - - if (virCondInit(&driver->initCond) < 0) { - virReportSystemError(errno, "%s", - _("Unable to initialize condition variable")); - virMutexDestroy(&driver->lock); - VIR_FREE(driver); - return VIR_DRV_STATE_INIT_ERROR; - } - - nodeDeviceLock(); - - if (privileged) { - driver->stateDir =3D g_strdup_printf("%s/libvirt/nodedev", RUNSTAT= EDIR); - } else { - g_autofree char *rundir =3D NULL; - - rundir =3D virGetUserRuntimeDirectory(); - driver->stateDir =3D g_strdup_printf("%s/nodedev/run", rundir); - } - - if (virFileMakePathWithMode(driver->stateDir, S_IRWXU) < 0) { - virReportSystemError(errno, _("cannot create state directory '%s'"= ), - driver->stateDir); - goto failure; - } - - if ((driver->lockFD =3D - virPidFileAcquire(driver->stateDir, "driver", false, getpid())) <= 0) - goto failure; - - if (!(driver->devs =3D virNodeDeviceObjListNew())) - goto failure; - - dbus_error_init(&err); - if (!(sysbus =3D virDBusGetSystemBus())) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("DBus not available, disabling HAL driver: %s"), - virGetLastErrorMessage()); - ret =3D VIR_DRV_STATE_INIT_SKIPPED; - goto failure; - } - - hal_ctx =3D libhal_ctx_new(); - if (hal_ctx =3D=3D NULL) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("libhal_ctx_new returned NULL")); - goto failure; - } - - if (!libhal_ctx_set_dbus_connection(hal_ctx, sysbus)) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("libhal_ctx_set_dbus_connection failed")); - goto failure; - } - if (!libhal_ctx_init(hal_ctx, &err)) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("libhal_ctx_init failed, haldaemon is probably " - "not running")); - /* We don't want to show a fatal error here, - otherwise entire libvirtd shuts down when - hald isn't running */ - ret =3D VIR_DRV_STATE_INIT_SKIPPED; - goto failure; - } - - /* Populate with known devices */ - driver->privateData =3D hal_ctx; - - /* We need to unlock state now, since setting these callbacks cause - * a dbus RPC call, and while this call is waiting for the reply, - * a signal may already arrive, triggering the callback and thus - * requiring the lock ! - */ - nodeDeviceUnlock(); - - /* Register HAL event callbacks */ - if (!libhal_ctx_set_device_added(hal_ctx, device_added) || - !libhal_ctx_set_device_removed(hal_ctx, device_removed) || - !libhal_ctx_set_device_new_capability(hal_ctx, device_cap_added) || - !libhal_ctx_set_device_lost_capability(hal_ctx, device_cap_lost) || - !libhal_ctx_set_device_property_modified(hal_ctx, device_prop_modi= fied) || - !libhal_device_property_watch_all(hal_ctx, &err)) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("setting up HAL callbacks failed")); - goto failure; - } - - udi =3D libhal_get_all_devices(hal_ctx, &num_devs, &err); - if (udi =3D=3D NULL) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("libhal_get_all_devices failed")); - goto failure; - } - for (i =3D 0; i < num_devs; i++) { - dev_create(udi[i]); - VIR_FREE(udi[i]); - } - VIR_FREE(udi); - - nodeDeviceLock(); - driver->initialized =3D true; - nodeDeviceUnlock(); - virCondBroadcast(&driver->initCond); - - return VIR_DRV_STATE_INIT_COMPLETE; - - failure: - if (dbus_error_is_set(&err)) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("%s: %s"), err.name, err.message); - dbus_error_free(&err); - } - virNodeDeviceObjListFree(driver->devs); - if (hal_ctx) - (void)libhal_ctx_free(hal_ctx); - nodeDeviceUnlock(); - VIR_FREE(driver); - - return ret; -} - - -static int -nodeStateCleanup(void) -{ - if (driver) { - nodeDeviceLock(); - LibHalContext *hal_ctx =3D DRV_STATE_HAL_CTX(driver); - virNodeDeviceObjListFree(driver->devs); - (void)libhal_ctx_shutdown(hal_ctx, NULL); - (void)libhal_ctx_free(hal_ctx); - if (driver->lockFD !=3D -1) - virPidFileRelease(driver->stateDir, "driver", driver->lockFD); - - VIR_FREE(driver->stateDir); - nodeDeviceUnlock(); - virCondDestroy(&driver->initCond); - virMutexDestroy(&driver->lock); - VIR_FREE(driver); - return 0; - } - return -1; -} - - -static int -nodeStateReload(void) -{ - DBusError err; - char **udi =3D NULL; - int num_devs; - size_t i; - LibHalContext *hal_ctx; - - VIR_INFO("Reloading HAL device state"); - nodeDeviceLock(); - VIR_INFO("Removing existing objects"); - virNodeDeviceObjListFree(driver->devs); - nodeDeviceUnlock(); - - hal_ctx =3D DRV_STATE_HAL_CTX(driver); - VIR_INFO("Creating new objects"); - dbus_error_init(&err); - udi =3D libhal_get_all_devices(hal_ctx, &num_devs, &err); - if (udi =3D=3D NULL) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("libhal_get_all_devices failed")); - return -1; - } - for (i =3D 0; i < num_devs; i++) { - dev_create(udi[i]); - VIR_FREE(udi[i]); - } - VIR_FREE(udi); - VIR_INFO("HAL device reload complete"); - - return 0; -} - - -static virNodeDeviceDriver halNodeDeviceDriver =3D { - .name =3D "HAL", - .nodeNumOfDevices =3D nodeNumOfDevices, /* 0.5.0 */ - .nodeListDevices =3D nodeListDevices, /* 0.5.0 */ - .connectListAllNodeDevices =3D nodeConnectListAllNodeDevices, /* 0.10.= 2 */ - .nodeDeviceLookupByName =3D nodeDeviceLookupByName, /* 0.5.0 */ - .nodeDeviceLookupSCSIHostByWWN =3D nodeDeviceLookupSCSIHostByWWN, /* 1= .0.2 */ - .nodeDeviceGetXMLDesc =3D nodeDeviceGetXMLDesc, /* 0.5.0 */ - .nodeDeviceGetParent =3D nodeDeviceGetParent, /* 0.5.0 */ - .nodeDeviceNumOfCaps =3D nodeDeviceNumOfCaps, /* 0.5.0 */ - .nodeDeviceListCaps =3D nodeDeviceListCaps, /* 0.5.0 */ - .nodeDeviceCreateXML =3D nodeDeviceCreateXML, /* 0.6.5 */ - .nodeDeviceDestroy =3D nodeDeviceDestroy, /* 0.6.5 */ -}; - - -static virHypervisorDriver halHypervisorDriver =3D { - .name =3D "nodedev", - .connectOpen =3D nodeConnectOpen, /* 4.1.0 */ - .connectClose =3D nodeConnectClose, /* 4.1.0 */ - .connectIsEncrypted =3D nodeConnectIsEncrypted, /* 4.1.0 */ - .connectIsSecure =3D nodeConnectIsSecure, /* 4.1.0 */ - .connectIsAlive =3D nodeConnectIsAlive, /* 4.1.0 */ -}; - - -static virConnectDriver halConnectDriver =3D { - .localOnly =3D true, - .uriSchemes =3D (const char *[]){ "nodedev", NULL }, - .hypervisorDriver =3D &halHypervisorDriver, - .nodeDeviceDriver =3D &halNodeDeviceDriver, -}; - - -static virStateDriver halStateDriver =3D { - .name =3D "HAL", - .stateInitialize =3D nodeStateInitialize, /* 0.5.0 */ - .stateCleanup =3D nodeStateCleanup, /* 0.5.0 */ - .stateReload =3D nodeStateReload, /* 0.5.0 */ -}; - -int -halNodeRegister(void) -{ - if (virRegisterConnectDriver(&halConnectDriver, false) < 0) - return -1; - if (virSetSharedNodeDeviceDriver(&halNodeDeviceDriver) < 0) - return -1; - return virRegisterStateDriver(&halStateDriver); -} diff --git a/src/node_device/node_device_hal.h b/src/node_device/node_devic= e_hal.h deleted file mode 100644 index 5e9c25ae34..0000000000 --- a/src/node_device/node_device_hal.h +++ /dev/null @@ -1,22 +0,0 @@ -/* - * node_device_hal.h: node device enumeration - HAL-based implementation - * - * Copyright (C) 2009 Red Hat, Inc. - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library. If not, see - * . - * - */ - -#pragma once --=20 2.26.2 From nobody Sat May 4 20:55:56 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) client-ip=216.205.24.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1600331510; cv=none; d=zohomail.com; s=zohoarc; b=ZDEY6iYSgdKnE3xilEXvuJIaiNuuzOaLbm5NaD4qeM2R0pXMIEBBRcN7GWw3A7jQZnG7beqZdoy6IdpxPJwM426rDJ6E6LHzmunE646z3hzXU8vNKoF7AowSObUFqtFPssfAMHWUmuoYhGdlKk+DZRc/IINVZiEF60K8OslMEcE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1600331510; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=kiOtE+L37SYeO8gKlaD91eoI0KvXo+WER6a8rKS5lw4=; b=l0sHOSGhk62DVU3Ovzhws1Kjddwp0LkPSS6Hq0I5HE6PI3IsWIugPpHI1oG9JcYY59CHr+AGkbC1U+dLup7vBywO63qGE7NiDPRp4exD+/unEYfk52cYll819o/D0penFdqIxrnfjXA/1mQKAYF5f1wxKoAx7bx4lO4hsSvH3WM= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by mx.zohomail.com with SMTPS id 1600331510814697.8507408651496; Thu, 17 Sep 2020 01:31:50 -0700 (PDT) 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-51-xZeH-UehPNW7UXhPYm5Vrg-1; Thu, 17 Sep 2020 04:31:44 -0400 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id ADEF9188C137; Thu, 17 Sep 2020 08:31:38 +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 86EFC19728; Thu, 17 Sep 2020 08:31:38 +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 4D6DD44A7C; Thu, 17 Sep 2020 08:31:38 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 08H8Tumt004064 for ; Thu, 17 Sep 2020 04:29:56 -0400 Received: by smtp.corp.redhat.com (Postfix) id B97EC5DA30; Thu, 17 Sep 2020 08:29:56 +0000 (UTC) Received: from antique-work.redhat.com (unknown [10.40.195.159]) by smtp.corp.redhat.com (Postfix) with ESMTP id DB0A65DEC4 for ; Thu, 17 Sep 2020 08:29:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1600331506; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=kiOtE+L37SYeO8gKlaD91eoI0KvXo+WER6a8rKS5lw4=; b=Ky4ewY83NQkcHtvLs7TOKf+oGluKNSTXAcgd3x9fTNgVSbACpohanlNnGlEIbVTMBMd+eP RYZGTpv/qYbMLe0KVqsWQ3cEp5gwHE+k+BUCTmEyLQW4+0nst8Jpr/pD9ZwjaD2YTthPed /r5py613a0EgXIoG/4ayvP5tzTotTZg= X-MC-Unique: xZeH-UehPNW7UXhPYm5Vrg-1 From: Pavel Hrdina To: libvir-list@redhat.com Subject: [libvirt PATCH 02/14] tests: mock libdbus in networkxml2firewalltest Date: Thu, 17 Sep 2020 10:29:37 +0200 Message-Id: <99b42fda8046355e1b9f08c9bd14335ce9e300a3.1600331276.git.phrdina@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com 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: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" This test calls into src/util/virfirewalld.c where it uses DBus to figure out if firewalld is registered. Without the mock it luckily fails and the test works correctly. To isolate the tests from host environment we should mock the DBus calls. Signed-off-by: Pavel Hrdina Reviewed-by: Michal Privoznik Reviewed-by: Neal Gompa --- tests/meson.build | 2 +- tests/networkxml2firewalltest.c | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/tests/meson.build b/tests/meson.build index 0a204c46e4..f0f3d8c1ef 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -404,7 +404,7 @@ endif if conf.has('WITH_NETWORK') tests +=3D [ { 'name': 'networkxml2conftest', 'link_with': [ network_driver_impl ] = }, - { 'name': 'networkxml2firewalltest', 'link_with': [ network_driver_imp= l ] }, + { 'name': 'networkxml2firewalltest', 'link_with': [ network_driver_imp= l ], 'deps': [ dbus_dep ] }, { 'name': 'networkxml2xmltest', 'link_with': [ network_driver_impl ] }, ] endif diff --git a/tests/networkxml2firewalltest.c b/tests/networkxml2firewalltes= t.c index 29e7d8bc38..80e2d6a035 100644 --- a/tests/networkxml2firewalltest.c +++ b/tests/networkxml2firewalltest.c @@ -26,8 +26,13 @@ =20 #if defined (__linux__) =20 +# if WITH_DBUS +# include +# endif + # include "network/bridge_driver_platform.h" # include "virbuffer.h" +# include "virmock.h" =20 # define LIBVIRT_VIRFIREWALLPRIV_H_ALLOW # include "virfirewallpriv.h" @@ -43,6 +48,22 @@ # error "test case not ported to this platform" # endif =20 +# if WITH_DBUS +VIR_MOCK_WRAP_RET_ARGS(dbus_connection_send_with_reply_and_block, + DBusMessage *, + DBusConnection *, connection, + DBusMessage *, message, + int, timeout_milliseconds, + DBusError *, error) +{ + VIR_MOCK_REAL_INIT(dbus_connection_send_with_reply_and_block); + + dbus_set_error_const(error, "org.freedesktop.error", "dbus is disabled= "); + + return NULL; +} +# endif + static void testCommandDryRun(const char *const*args G_GNUC_UNUSED, const char *const*env G_GNUC_UNUSED, @@ -176,7 +197,11 @@ mymain(void) return ret =3D=3D 0 ? EXIT_SUCCESS : EXIT_FAILURE; } =20 +# if WITH_DBUS +VIR_TEST_MAIN_PRELOAD(mymain, VIR_TEST_MOCK("virdbus")) +# else VIR_TEST_MAIN(mymain) +# endif =20 #else /* ! defined (__linux__) */ =20 --=20 2.26.2 From nobody Sat May 4 20:55:56 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 63.128.21.124 as permitted sender) client-ip=63.128.21.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 63.128.21.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1600331652; cv=none; d=zohomail.com; s=zohoarc; b=LrhrXYCx/QzvW8ovPEbr+ipHbr2zqvacHpvO9jqDjj6f2aEvThdH1PJ4ZUUkCfS4Xp29HAc8bbIyvE6J2CwkTbdex8UPdzxlrekPijW1oku7p4EZuibPRi9Q+VM2wyIG6dIQYg2FTAIXjCzVLaSGTWs+Ese6xRbzF7vGWaR0K4c= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1600331652; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=r+L7IoIasDecm1T3OPWchWbbnCAXBaoGWhbgm46HfYI=; b=h7fLYpYBCYueHkx+hoaNR17KsFWD5aj0FN45BVDfWMem6tJetlRuv2F0gfehXkY62NlV9zP/QC6OJRpEo4hiS3POVlP5FeJHjxMNs+RRDhqriLGY6gc/ZsDdvHFqBPTZBeiwsbncBNAKJNdOK7xsRFdjfn8qAxL/uBvjwMIfibc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 63.128.21.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [63.128.21.124]) by mx.zohomail.com with SMTPS id 1600331652400614.493394380283; Thu, 17 Sep 2020 01:34:12 -0700 (PDT) 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-487-8f-WCZkZOAy8Zvo0HdSR3A-1; Thu, 17 Sep 2020 04:31:51 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 5DF551017DD4; Thu, 17 Sep 2020 08:31:41 +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 397A76111F; Thu, 17 Sep 2020 08:31:41 +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 014E98C7D8; Thu, 17 Sep 2020 08:31:40 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 08H8Tv8s004076 for ; Thu, 17 Sep 2020 04:29:57 -0400 Received: by smtp.corp.redhat.com (Postfix) id A0EE05DA30; Thu, 17 Sep 2020 08:29:57 +0000 (UTC) Received: from antique-work.redhat.com (unknown [10.40.195.159]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1DB725DEC4 for ; Thu, 17 Sep 2020 08:29:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1600331651; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=r+L7IoIasDecm1T3OPWchWbbnCAXBaoGWhbgm46HfYI=; b=cokXQTfQoOFhR56FoG2K7//cerAm62EM3FMpknXzp+/1BqfpLlHrMRAszWu9bz04FARI1G Ir62WgRn0bgFka3q0NSHHwiNsic6EwHsDI7ZoRM7kh8KZMSa6Uh2OVhL2ZArFCxgE6Vi1k 2XPKcrDzmFWSSaJ6nZoF9WHRopQPI7M= X-MC-Unique: 8f-WCZkZOAy8Zvo0HdSR3A-1 From: Pavel Hrdina To: libvir-list@redhat.com Subject: [libvirt PATCH 03/14] util: introduce helpers for GLib DBus implementation Date: Thu, 17 Sep 2020 10:29:38 +0200 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com 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: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0.001 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" With libdbus our wrappers had a special syntax to create the DBus messages by defining the DBus message signature followed by list of arguments providing data based on the signature. There will be no similar helper with GLib implementation as they provide same functionality via GVariant APIs. The syntax is slightly different mostly for how arrays, variadic types and dictionaries are created/parsed. Additional difference is that with GLib DBus everything is wrapped in extra tuple (struct). For more details refer to the documentation [1]. [1] Signed-off-by: Pavel Hrdina Reviewed-by: Michal Privoznik Reviewed-by: Neal Gompa --- meson.build | 6 +- po/POTFILES.in | 1 + src/libvirt_private.syms | 14 ++ src/util/meson.build | 1 + src/util/virgdbus.c | 425 +++++++++++++++++++++++++++++++++++++++ src/util/virgdbus.h | 79 ++++++++ 6 files changed, 525 insertions(+), 1 deletion(-) create mode 100644 src/util/virgdbus.c create mode 100644 src/util/virgdbus.h diff --git a/meson.build b/meson.build index 3d15b4ee34..b5b1223227 100644 --- a/meson.build +++ b/meson.build @@ -1066,7 +1066,11 @@ endif glib_version =3D '2.48.0' glib_dep =3D dependency('glib-2.0', version: '>=3D' + glib_version) gobject_dep =3D dependency('gobject-2.0', version: '>=3D' + glib_version) -gio_dep =3D dependency('gio-2.0', version: '>=3D' + glib_version) +if host_machine.system() =3D=3D 'windows' + gio_dep =3D dependency('gio-2.0', version: '>=3D' + glib_version) +else + gio_dep =3D dependency('gio-unix-2.0', version: '>=3D' + glib_version) +endif glib_dep =3D declare_dependency( dependencies: [ glib_dep, gobject_dep, gio_dep ], ) diff --git a/po/POTFILES.in b/po/POTFILES.in index 4ab8832b37..d87425a64c 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -257,6 +257,7 @@ @SRCDIR@src/util/virfirewall.c @SRCDIR@src/util/virfirewalld.c @SRCDIR@src/util/virfirmware.c +@SRCDIR@src/util/virgdbus.c @SRCDIR@src/util/virhash.c @SRCDIR@src/util/virhook.c @SRCDIR@src/util/virhostcpu.c diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 5842b8d23d..fea5a49e55 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2190,6 +2190,20 @@ virFirmwareParse; virFirmwareParseList; =20 =20 +# util/virgdbus.h +virGDBusCallMethod; +virGDBusCallMethodWithFD; +virGDBusCloseSystemBus; +virGDBusErrorIsUnknownMethod; +virGDBusGetSessionBus; +virGDBusGetSystemBus; +virGDBusHasSystemBus; +virGDBusIsServiceEnabled; +virGDBusIsServiceRegistered; +virGDBusMessageIsSignal; +virGDBusSetSharedBus; + + # util/virgettext.h virGettextInitialize; =20 diff --git a/src/util/meson.build b/src/util/meson.build index bf556e7ae6..8a9dcac053 100644 --- a/src/util/meson.build +++ b/src/util/meson.build @@ -33,6 +33,7 @@ util_sources =3D [ 'virfirewall.c', 'virfirewalld.c', 'virfirmware.c', + 'virgdbus.c', 'virgettext.c', 'virgic.c', 'virhash.c', diff --git a/src/util/virgdbus.c b/src/util/virgdbus.c new file mode 100644 index 0000000000..535b19f0a4 --- /dev/null +++ b/src/util/virgdbus.c @@ -0,0 +1,425 @@ +/* + * virgdbus.c: helper for using GDBus + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * . + * + */ + +#include + +#include "virerror.h" +#include "virlog.h" +#include "virgdbus.h" +#include "virthread.h" + + +#define VIR_FROM_THIS VIR_FROM_DBUS + +VIR_LOG_INIT("util.dbus"); + + +static bool sharedBus =3D true; +static GDBusConnection *systemBus; +static GDBusConnection *sessionBus; +static virOnceControl systemOnce =3D VIR_ONCE_CONTROL_INITIALIZER; +static virOnceControl sessionOnce =3D VIR_ONCE_CONTROL_INITIALIZER; +static GError *systemError; +static GError *sessionError; + + +void +virGDBusSetSharedBus(bool shared) +{ + sharedBus =3D shared; +} + + +static GDBusConnection * +virGDBusBusInit(GBusType type, GError **error) +{ + g_autofree char *address =3D NULL; + + if (sharedBus) { + return g_bus_get_sync(type, NULL, error); + } else { + address =3D g_dbus_address_get_for_bus_sync(type, NULL, error); + if (error) + return NULL; + return g_dbus_connection_new_for_address_sync(address, + G_DBUS_CONNECTION_FL= AGS_NONE, + NULL, + NULL, + error); + } +} + + +static void +virGDBusSystemBusInit(void) +{ + systemBus =3D virGDBusBusInit(G_BUS_TYPE_SYSTEM, &systemError); +} + + +static GDBusConnection * +virGDBusGetSystemBusInternal(void) +{ + if (virOnce(&systemOnce, virGDBusSystemBusInit) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Unable to run one time GDBus initializer")); + return NULL; + } + + return systemBus; +} + + +GDBusConnection * +virGDBusGetSystemBus(void) +{ + GDBusConnection *bus =3D virGDBusGetSystemBusInternal(); + + if (!bus) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Unable to get system bus connection: %s"), + systemError->message); + return NULL; + } + + return bus; +} + + +static void +virGDBusSessionBusInit(void) +{ + sessionBus =3D virGDBusBusInit(G_BUS_TYPE_SESSION, &sessionError); +} + + +GDBusConnection * +virGDBusGetSessionBus(void) +{ + if (virOnce(&sessionOnce, virGDBusSessionBusInit) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Unable to run one time GDBus initializer")); + return NULL; + } + + if (!sessionBus) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Unable to get session bus connection: %s"), + sessionError->message); + return NULL; + } + + return sessionBus; +} + + +/** + * virGDBusHasSystemBus: + * + * Check if DBus system bus is running. This does not imply that we have + * a connection. DBus might be running and refusing connections due to its + * client limit. The latter must be treated as a fatal error. + * + * Return false if dbus is not available, true if probably available. + */ +bool +virGDBusHasSystemBus(void) +{ + g_autofree char *name =3D NULL; + + if (virGDBusGetSystemBusInternal()) + return true; + + if (!g_dbus_error_is_remote_error(systemError)) + return false; + + name =3D g_dbus_error_get_remote_error(systemError); + + if (name && + (STREQ(name, "org.freedesktop.DBus.Error.FileNotFound") || + STREQ(name, "org.freedesktop.DBus.Error.NoServer"))) { + VIR_DEBUG("System bus not available: %s", NULLSTR(systemError->mes= sage)); + return false; + } + + return true; +} + + +void +virGDBusCloseSystemBus(void) +{ + if (!systemBus || sharedBus) + return; + + g_dbus_connection_flush_sync(systemBus, NULL, NULL); + g_dbus_connection_close_sync(systemBus, NULL, NULL); + g_object_unref(systemBus); + systemBus =3D NULL; +} + + +#define VIR_DBUS_METHOD_CALL_TIMEOUT_MILIS 30 * 1000 + +/** + * virGDBusCallMethod: + * @conn: a DBus connection + * @reply: pointer to receive reply message, or NULL + * @error: libvirt error pointer or NULL + * @busName: bus identifier of the target service + * @objectPath: object path of the target service + * @ifaceName: the interface of the object + * @method: the name of the method in the interface + * @data: pointer to data passed to DBus method + * + * If @error is NULL then a libvirt error will be raised when a DBus error + * is received and the return value will be -1. If @error is non-NULL then + * any DBus error will be saved into that object and the return value will + * be 0. + * + * Returns 0 on success, or -1 upon error. + */ +int +virGDBusCallMethod(GDBusConnection *conn, + GVariant **reply, + virErrorPtr error, + const char *busName, + const char *objectPath, + const char *ifaceName, + const char *method, + GVariant *data) +{ + g_autoptr(GVariant) ret =3D NULL; + g_autoptr(GError) gerror =3D NULL; + + if (error) + memset(error, 0, sizeof(*error)); + + if (data) + g_variant_ref_sink(data); + + ret =3D g_dbus_connection_call_sync(conn, + busName, + objectPath, + ifaceName, + method, + data, + NULL, + G_DBUS_CALL_FLAGS_NONE, + VIR_DBUS_METHOD_CALL_TIMEOUT_MILIS, + NULL, + &gerror); + + if (!ret) { + if (error && g_dbus_error_is_remote_error(gerror)) { + error->level =3D VIR_ERR_ERROR; + error->code =3D VIR_ERR_DBUS_SERVICE; + error->domain =3D VIR_FROM_DBUS; + error->str1 =3D g_dbus_error_get_remote_error(gerror); + error->message =3D g_strdup(gerror->message); + } else { + virReportError(VIR_ERR_DBUS_SERVICE, "%s", gerror->message); + return -1; + } + } + + if (reply) + *reply =3D g_steal_pointer(&ret); + + return 0; +} + + +#ifdef G_OS_UNIX +int +virGDBusCallMethodWithFD(GDBusConnection *conn, + GVariant **reply, + GUnixFDList **replyFD, + virErrorPtr error, + const char *busName, + const char *objectPath, + const char *ifaceName, + const char *method, + GVariant *data, + GUnixFDList *dataFD) +{ + g_autoptr(GVariant) ret =3D NULL; + g_autoptr(GError) gerror =3D NULL; + + if (error) + memset(error, 0, sizeof(*error)); + + if (data) + g_variant_ref_sink(data); + + ret =3D g_dbus_connection_call_with_unix_fd_list_sync(conn, + busName, + objectPath, + ifaceName, + method, + data, + NULL, + G_DBUS_CALL_FLAGS_= NONE, + VIR_DBUS_METHOD_CA= LL_TIMEOUT_MILIS, + dataFD, + replyFD, + NULL, + &gerror); + + if (!ret) { + if (error && g_dbus_error_is_remote_error(gerror)) { + error->level =3D VIR_ERR_ERROR; + error->code =3D VIR_ERR_DBUS_SERVICE; + error->domain =3D VIR_FROM_DBUS; + error->str1 =3D g_dbus_error_get_remote_error(gerror); + error->message =3D g_strdup(gerror->message); + + if (!error->str1 || !error->message) + return -1; + } else { + virReportError(VIR_ERR_DBUS_SERVICE, "%s", gerror->message); + return -1; + } + } + + if (reply) + *reply =3D g_steal_pointer(&ret); + + return 0; +} +#else +int +virGDBusCallMethodWithFD(GDBusConnection *conn G_GNUC_UNUSED, + GVariant **reply G_GNUC_UNUSED, + GUnixFDList **replyFD G_GNUC_UNUSED, + virErrorPtr error G_GNUC_UNUSED, + const char *busName G_GNUC_UNUSED, + const char *objectPath G_GNUC_UNUSED, + const char *ifaceName G_GNUC_UNUSED, + const char *method G_GNUC_UNUSED, + GVariant *data G_GNUC_UNUSED, + GUnixFDList *dataFD G_GNUC_UNUSED) +{ + virReportSystemError(ENOSYS, "%s", + _("Unix file descriptors not supported on this pl= atform")); + return -1; +} +#endif + + +static int +virGDBusIsServiceInList(const char *listMethod, + const char *name) +{ + GDBusConnection *conn; + g_autoptr(GVariant) reply =3D NULL; + g_autoptr(GVariantIter) iter =3D NULL; + char *str; + int rc; + + if (!virGDBusHasSystemBus()) + return -2; + + conn =3D virGDBusGetSystemBus(); + if (!conn) + return -1; + + rc =3D virGDBusCallMethod(conn, + &reply, + NULL, + "org.freedesktop.DBus", + "/org/freedesktop/DBus", + "org.freedesktop.DBus", + listMethod, + NULL); + + if (rc < 0) + return -1; + + g_variant_get(reply, "(as)", &iter); + while (g_variant_iter_loop(iter, "s", &str)) { + if (STREQ(str, name)) + return 0; + } + + return -2; +} + + +/** + * virGDBusIsServiceEnabled: + * @name: service name + * + * Returns 0 if service is available, -1 on fatal error, or -2 if service = is not available + */ +int +virGDBusIsServiceEnabled(const char *name) +{ + int ret =3D virGDBusIsServiceInList("ListActivatableNames", name); + + VIR_DEBUG("Service %s is %s", name, ret ? "unavailable" : "available"); + + return ret; +} + + +/** + * virGDBusIsServiceRegistered: + * @name: service name + * + * Returns 0 if service is registered, -1 on fatal error, or -2 if service= is not registered + */ +int +virGDBusIsServiceRegistered(const char *name) +{ + int ret =3D virGDBusIsServiceInList("ListNames", name); + + VIR_DEBUG("Service %s is %s", name, ret ? "not registered" : "register= ed"); + + return ret; +} + + +bool +virGDBusErrorIsUnknownMethod(virErrorPtr err) +{ + return err->domain =3D=3D VIR_FROM_DBUS && + err->code =3D=3D VIR_ERR_DBUS_SERVICE && + err->level =3D=3D VIR_ERR_ERROR && + STREQ_NULLABLE("org.freedesktop.DBus.Error.UnknownMethod", + err->str1); +} + + +bool +virGDBusMessageIsSignal(GDBusMessage *message, + const char *iface, + const char *signal) +{ + GDBusMessageType type =3D g_dbus_message_get_message_type(message); + + if (type =3D=3D G_DBUS_MESSAGE_TYPE_SIGNAL) { + const char *interface =3D g_dbus_message_get_interface(message); + const char *member =3D g_dbus_message_get_member(message); + + return STREQ(interface, iface) && STREQ(member, signal); + } + + return false; +} diff --git a/src/util/virgdbus.h b/src/util/virgdbus.h new file mode 100644 index 0000000000..6ea717eea2 --- /dev/null +++ b/src/util/virgdbus.h @@ -0,0 +1,79 @@ +/* + * virgdbus.h: helper for using GDBus + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * . + * + */ + +#pragma once + +#include + +#ifdef G_OS_UNIX +# include +#endif + +#include "internal.h" + +void +virGDBusSetSharedBus(bool shared); + +GDBusConnection * +virGDBusGetSystemBus(void); + +GDBusConnection * +virGDBusGetSessionBus(void); + +bool +virGDBusHasSystemBus(void); + +void +virGDBusCloseSystemBus(void); + +int +virGDBusCallMethod(GDBusConnection *conn, + GVariant **reply, + virErrorPtr error, + const char *busName, + const char *objectPath, + const char *ifaceName, + const char *method, + GVariant *data); + +int +virGDBusCallMethodWithFD(GDBusConnection *conn, + GVariant **reply, + GUnixFDList **replyFD, + virErrorPtr error, + const char *busName, + const char *objectPath, + const char *ifaceName, + const char *method, + GVariant *data, + GUnixFDList *dataFD); + +int +virGDBusIsServiceEnabled(const char *name); + +int +virGDBusIsServiceRegistered(const char *name); + +bool +virGDBusErrorIsUnknownMethod(virErrorPtr err); + +bool +virGDBusMessageIsSignal(GDBusMessage *message, + const char *iface, + const char *signal); --=20 2.26.2 From nobody Sat May 4 20:55:56 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 63.128.21.124 as permitted sender) client-ip=63.128.21.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 63.128.21.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1600331560; cv=none; d=zohomail.com; s=zohoarc; b=akBcHhlLyBzkVYq2XR1P5c7SXeV49hCinBFjOTLs3D9DHcOLoa4+gThS/0rOLqc4gUC/gJqXyfgNFsShrvFUjwXx6BUP9gOOBsAaVNYlgMTM4eIZNA0A7XerXH8kogh36T1qpTFRGxorsZeEzoaiO8PpSlnZpu1HEF6yyBYuCwE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1600331560; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=QRdYIuyxMgiQ0m47u9Hh8jj18mXmY20b0DVGQfMNFNo=; b=e43z8U2G1vl6YR0Cu/X1qCcxtsPa6dIGNV/jfbguY08o+0tRxfaGcJfdTJaNp+F93pVWG//cec+6EKpOEHHUjFkk9a9nJAK4qOnozBd4hYem+tZP6qOyBDPKmOc0ntHS0y+CPh89qgw+XSDP47LVMDk+LUzpoaA5hh+I0hvmJ5w= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 63.128.21.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [63.128.21.124]) by mx.zohomail.com with SMTPS id 1600331560934803.4264621735289; Thu, 17 Sep 2020 01:32:40 -0700 (PDT) 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-484-5fE1Udf-OM-0_dI2ZV7VyA-1; Thu, 17 Sep 2020 04:32:37 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 78BF61891EA2; Thu, 17 Sep 2020 08:32:32 +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 57CA681C45; Thu, 17 Sep 2020 08:32:32 +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 1BB8497319; Thu, 17 Sep 2020 08:32:32 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 08H8TwX8004083 for ; Thu, 17 Sep 2020 04:29:58 -0400 Received: by smtp.corp.redhat.com (Postfix) id 83A7C5DEBF; Thu, 17 Sep 2020 08:29:58 +0000 (UTC) Received: from antique-work.redhat.com (unknown [10.40.195.159]) by smtp.corp.redhat.com (Postfix) with ESMTP id 00C5E5DA30 for ; Thu, 17 Sep 2020 08:29:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1600331559; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=QRdYIuyxMgiQ0m47u9Hh8jj18mXmY20b0DVGQfMNFNo=; b=OzqgSwP34Xymr/sXM74fPon3gvcJH5MPcww8YksgiFikMY5jF71SxVstQSidtyaU7QHtdf O109WqFqdtCib56Y79Py0ibl1wgiqwGqtUJOja6o1Mwh4E04pjzADt/vNkadGO+fax8oWU QGTErPAWkCZ5pMRxQdHwBlT0RZ3A2ZM= X-MC-Unique: 5fE1Udf-OM-0_dI2ZV7VyA-1 From: Pavel Hrdina To: libvir-list@redhat.com Subject: [libvirt PATCH 04/14] tests/virmock: extend number of arguments Date: Thu, 17 Sep 2020 10:29:39 +0200 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com 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: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0.001 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" Rewrite to use GLib DBus instead of libdbus will introduce function with large number of arguments that we will have to mock for our tests so we need to extend the number of arguments for our macros. Signed-off-by: Pavel Hrdina Reviewed-by: Michal Privoznik Reviewed-by: Neal Gompa --- tests/virmock.h | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/tests/virmock.h b/tests/virmock.h index a812cfe08b..dea5feb80f 100644 --- a/tests/virmock.h +++ b/tests/virmock.h @@ -27,8 +27,8 @@ =20 #include "internal.h" =20 -#define VIR_MOCK_COUNT_ARGS(...) VIR_MOCK_ARG21(__VA_ARGS__, 20, 19, 18, 1= 7, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1) -#define VIR_MOCK_ARG21(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, = _13, _14, _15, _16, _17, _18, _19, _20, _21, ...) _21 +#define VIR_MOCK_COUNT_ARGS(...) VIR_MOCK_ARG27(__VA_ARGS__, 26, 25, 24, 2= 3, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3,= 2, 1) +#define VIR_MOCK_ARG27(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, = _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, = ...) _27 #define VIR_MOCK_ARG_PASTE(a, b, ...) a##b(__VA_ARGS__) =20 #define VIR_MOCK_ARGNAME(a, b) b @@ -56,6 +56,12 @@ #define VIR_MOCK_GET_ARG19(z, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o,= p, q, r, s) z(a, b), z(c, d), z(e, f), z(g, h), z(i, j), z(k, l), z(m, n)= , z(o, p), z(q, r) #define VIR_MOCK_GET_ARG20(z, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o,= p, q, r, s, t) z(a, b), z(c, d), z(e, f), z(g, h), z(i, j), z(k, l), z(m,= n), z(o, p), z(q, r), z(s, t) #define VIR_MOCK_GET_ARG21(z, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o,= p, q, r, s, t, u) z(a, b), z(c, d), z(e, f), z(g, h), z(i, j), z(k, l), z= (m, n), z(o, p), z(q, r), z(s, t) +#define VIR_MOCK_GET_ARG22(z, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o,= p, q, r, s, t, u, v) z(a, b), z(c, d), z(e, f), z(g, h), z(i, j), z(k, l)= , z(m, n), z(o, p), z(q, r), z(s, t), z(u, v) +#define VIR_MOCK_GET_ARG23(z, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o,= p, q, r, s, t, u, v, w) z(a, b), z(c, d), z(e, f), z(g, h), z(i, j), z(k,= l), z(m, n), z(o, p), z(q, r), z(s, t), z(u, v) +#define VIR_MOCK_GET_ARG24(z, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o,= p, q, r, s, t, u, v, w, x) z(a, b), z(c, d), z(e, f), z(g, h), z(i, j), z= (k, l), z(m, n), z(o, p), z(q, r), z(s, t), z(u, v), z(w, x) +#define VIR_MOCK_GET_ARG25(z, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o,= p, q, r, s, t, u, v, w, x, y) z(a, b), z(c, d), z(e, f), z(g, h), z(i, j)= , z(k, l), z(m, n), z(o, p), z(q, r), z(s, t), z(u, v), z(w, x) +#define VIR_MOCK_GET_ARG26(z, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o,= p, q, r, s, t, u, v, w, x, y, aa) z(a, b), z(c, d), z(e, f), z(g, h), z(i= , j), z(k, l), z(m, n), z(o, p), z(q, r), z(s, t), z(u, v), z(w, x), z(y, a= a) +#define VIR_MOCK_GET_ARG27(z, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o,= p, q, r, s, t, u, v, w, x, y, aa, ab) z(a, b), z(c, d), z(e, f), z(g, h),= z(i, j), z(k, l), z(m, n), z(o, p), z(q, r), z(s, t), z(u, v), z(w, x), z(= y, aa) =20 =20 #define VIR_MOCK_ARGNAMES_EXPAND(a, b, ...) VIR_MOCK_ARG_PASTE(a, b, __VA_= ARGS__) --=20 2.26.2 From nobody Sat May 4 20:55:56 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) client-ip=216.205.24.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1600331513; cv=none; d=zohomail.com; s=zohoarc; b=K29Yz9x4TODa9378ou7aoYMOQVcXg80akYCXThr3p1N37poN9/OzUVL4LT0yys+V9Y11taUUABzVLeeAEuudUjMx7sWeXhKrgD1PYz1g5JR05f/AqeKiLbIPdTZ9eE8EOLjOOUU8XX9vztG3Kr1nr+O99Eaj5ZQeL3lAa4s9r9w= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1600331513; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=z2nxiU5pI8e6aI6PtbpamA8E3VgL48UC+W140zN5VKI=; b=WCzrrYzAcquihPXyeIzHQ3U/9/+PRgnAyltv7AUgPwremXcMjnRXsQlHzuBHOQA4MHniEnF4aDQicmPEJL8BZPH4rKDPUc79JoiFcYSrcaMoXtlHHz8PHfHSbVofpfYBFbU14+zh4Z0p2VsPNsbj/UiLfrT7V8fPRJbQSYBVfHU= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by mx.zohomail.com with SMTPS id 1600331513931609.199099380684; Thu, 17 Sep 2020 01:31:53 -0700 (PDT) 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-193-MT6ivHePOZyqkKzFrtM_0w-1; Thu, 17 Sep 2020 04:31:50 -0400 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 mimecast-mx01.redhat.com (Postfix) with ESMTPS id 0F09F188C126; Thu, 17 Sep 2020 08:31:44 +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 DE07168872; Thu, 17 Sep 2020 08:31:43 +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 A67748C7D9; Thu, 17 Sep 2020 08:31:43 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 08H8TxIw004095 for ; Thu, 17 Sep 2020 04:29:59 -0400 Received: by smtp.corp.redhat.com (Postfix) id 670235DA30; Thu, 17 Sep 2020 08:29:59 +0000 (UTC) Received: from antique-work.redhat.com (unknown [10.40.195.159]) by smtp.corp.redhat.com (Postfix) with ESMTP id D7F075DEBF for ; Thu, 17 Sep 2020 08:29:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1600331512; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=z2nxiU5pI8e6aI6PtbpamA8E3VgL48UC+W140zN5VKI=; b=MycEC173GwWnSnItuOsmds+ULX8/XSwRCoy9CTxjNduovHUVTDDbry+YfmwpFHu4uQsBWL iUuGqEmsZJVjsmiGCds8uGQK0simPn0yQ1YMEebdFDJ56QoC+nRTgrofDBbXvzM3jOIS6j lj8My8RM+zK/CLLtNAcWXNKatXGuyrw= X-MC-Unique: MT6ivHePOZyqkKzFrtM_0w-1 From: Pavel Hrdina To: libvir-list@redhat.com Subject: [libvirt PATCH 05/14] tests: introduce virgdbusmock to mock GLib DBus functions Date: Thu, 17 Sep 2020 10:29:40 +0200 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com 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: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0.001 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" Signed-off-by: Pavel Hrdina Reviewed-by: Michal Privoznik Reviewed-by: Neal Gompa --- tests/meson.build | 1 + tests/virgdbusmock.c | 85 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 tests/virgdbusmock.c diff --git a/tests/meson.build b/tests/meson.build index f0f3d8c1ef..0f3e4bfdd7 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -57,6 +57,7 @@ mock_libs =3D [ { 'name': 'vircgroupmock' }, { 'name': 'virdeterministichashmock' }, { 'name': 'virfilecachemock' }, + { 'name': 'virgdbusmock' }, { 'name': 'virhostcpumock' }, { 'name': 'virhostdevmock' }, { 'name': 'virnetdaemonmock' }, diff --git a/tests/virgdbusmock.c b/tests/virgdbusmock.c new file mode 100644 index 0000000000..7a378a616a --- /dev/null +++ b/tests/virgdbusmock.c @@ -0,0 +1,85 @@ +/* + * virgdbusmock.c: mocking of dbus message send/reply + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * . + */ + +#include + +#include + +#include "virmock.h" + +VIR_MOCK_STUB_RET_ARGS(g_bus_get_sync, + GDBusConnection *, (GDBusConnection *)0x1, + GBusType, type, + GCancellable, *cancellable, + GError, **error) + +VIR_MOCK_STUB_RET_ARGS(g_dbus_address_get_for_bus_sync, + gchar *, (gchar *)0x1, + GBusType, type, + GCancellable *, cancellable, + GError **, error) + +VIR_MOCK_STUB_RET_ARGS(g_dbus_connection_new_for_address_sync, + GDBusConnection *, (GDBusConnection *)0x1, + const gchar *, address, + GDBusConnectionFlags, flags, + GDBusAuthObserver *, observer, + GCancellable *, cancellable, + GError **, error) + +VIR_MOCK_STUB_RET_ARGS(g_dbus_connection_flush_sync, + gboolean, true, + GDBusConnection *, connection, + GCancellable *, cancellable, + GError **, error) + +VIR_MOCK_STUB_RET_ARGS(g_dbus_connection_close_sync, + gboolean, true, + GDBusConnection *, connection, + GCancellable *, cancellable, + GError **, error) + +VIR_MOCK_LINK_RET_ARGS(g_dbus_connection_call_sync, + GVariant *, + GDBusConnection *, connection, + const gchar *, bus_name, + const gchar *, object_path, + const gchar *, interface_name, + const gchar *, method_name, + GVariant *, parameters, + const GVariantType *, reply_type, + GDBusCallFlags, flags, + gint, timeout_msec, + GCancellable *, cancellable, + GError **, error) + +VIR_MOCK_LINK_RET_ARGS(g_dbus_connection_call_with_unix_fd_list_sync, + GVariant *, + GDBusConnection *, connection, + const gchar *, bus_name, + const gchar *, object_path, + const gchar *, interface_name, + const gchar *, method_name, + GVariant *, parameters, + const GVariantType *, reply_type, + GDBusCallFlags, flags, + gint, timeout_msec, + GUnixFDList *, fd_list, + GUnixFDList **, out_fd_list, + GCancellable *, cancellable, + GError **, error) --=20 2.26.2 From nobody Sat May 4 20:55:56 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) client-ip=216.205.24.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1600331565; cv=none; d=zohomail.com; s=zohoarc; b=MYwxuobiLJz9xDvxYgqx2NfqUa/XeDS0bdlmWIZbUlVY3G73Bs8u8IwVG1UE6BVJ7ENvQBbreMavAx3dhwyQgAW65nARNIUnW1EcxloN8k6I6G1yy8AD8VPwOmAxxOOc8TUu66RF6GC9DcbA3lLj4y/MnLPTxxnTDlbN5QKcZdY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1600331565; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=9bZ3mx5D3NUhB/8kMuCHvb7CMW1MHpmMhMopF0gjuWA=; b=OfAU09IOUZoFJOZ6kYF1pD5sUmuQljVScLf7f8KDU4tJGEXXV5rTh5mupr4UudQfhgYjuCoDC/u5BH/yyJQoGkQRVJYhofyPdF7XSYyOTJKHl3XNL/hXYHTTXN+telDO4fXrOxZSMP2dh7cyXArQNfKEdLREah7v4MKMwTsIuN4= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by mx.zohomail.com with SMTPS id 1600331565224414.6620968156798; Thu, 17 Sep 2020 01:32:45 -0700 (PDT) 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-37-PgHP0VihMJOmWVYq327HCw-1; Thu, 17 Sep 2020 04:32:41 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 4F7A885B685; Thu, 17 Sep 2020 08:32:36 +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 ECD3D5DEC2; Thu, 17 Sep 2020 08:32:35 +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 B6CBB8C7CF; Thu, 17 Sep 2020 08:32:35 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 08H8U0o9004100 for ; Thu, 17 Sep 2020 04:30:00 -0400 Received: by smtp.corp.redhat.com (Postfix) id 4A6105DEBF; Thu, 17 Sep 2020 08:30:00 +0000 (UTC) Received: from antique-work.redhat.com (unknown [10.40.195.159]) by smtp.corp.redhat.com (Postfix) with ESMTP id BBD1A5DA30 for ; Thu, 17 Sep 2020 08:29:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1600331564; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=9bZ3mx5D3NUhB/8kMuCHvb7CMW1MHpmMhMopF0gjuWA=; b=OaNBGiW3IfIbyUzDcsuLE6XKFOyl/5Pmt3s4OeL7FQVAa00d/YugKhvjM2JlQc4YCBu33Q tc5gyEEoX8uGcsaKqXlYOGEMDGDtcIk2ts/vxgAmeJL8xvhR1dEt24hUJuq+8Z2Ig46BLk /og1BKoLl52iq1UYeqAU8qCuXqzmIwA= X-MC-Unique: PgHP0VihMJOmWVYq327HCw-1 From: Pavel Hrdina To: libvir-list@redhat.com Subject: [libvirt PATCH 06/14] src/util/virpolkit: convert to use GLib DBus Date: Thu, 17 Sep 2020 10:29:41 +0200 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com 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: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" Signed-off-by: Pavel Hrdina Reviewed-by: Michal Privoznik Reviewed-by: Neal Gompa --- src/util/virpolkit.c | 115 +++++++++++++++++++++--------------------- tests/meson.build | 11 ++-- tests/virpolkittest.c | 112 +++++++++++++++------------------------- 3 files changed, 104 insertions(+), 134 deletions(-) diff --git a/src/util/virpolkit.c b/src/util/virpolkit.c index 1570d667ee..2ad00fd206 100644 --- a/src/util/virpolkit.c +++ b/src/util/virpolkit.c @@ -28,7 +28,7 @@ #include "virstring.h" #include "virprocess.h" #include "viralloc.h" -#include "virdbus.h" +#include "virgdbus.h" #include "virfile.h" #include "virutil.h" =20 @@ -63,80 +63,81 @@ int virPolkitCheckAuth(const char *actionid, const char **details, bool allowInteraction) { - DBusConnection *sysbus; - DBusMessage *reply =3D NULL; - char **retdetails =3D NULL; - size_t nretdetails =3D 0; - bool is_authorized; - bool is_challenge; + GDBusConnection *sysbus; + GVariantBuilder builder; + GVariant *gprocess =3D NULL; + GVariant *gdetails =3D NULL; + g_autoptr(GVariant) message =3D NULL; + g_autoptr(GVariant) reply =3D NULL; + g_autoptr(GVariantIter) iter =3D NULL; + char *retkey; + char *retval; + gboolean is_authorized; + gboolean is_challenge; bool is_dismissed =3D false; size_t i; - int ret =3D -1; =20 - if (!(sysbus =3D virDBusGetSystemBus())) - goto cleanup; + if (!(sysbus =3D virGDBusGetSystemBus())) + return -1; =20 VIR_INFO("Checking PID %lld running as %d", (long long) pid, uid); =20 - if (virDBusCallMethod(sysbus, - &reply, - NULL, - "org.freedesktop.PolicyKit1", - "/org/freedesktop/PolicyKit1/Authority", - "org.freedesktop.PolicyKit1.Authority", - "CheckAuthorization", - "(sa{sv})sa&{ss}us", - "unix-process", - 3, - "pid", "u", (unsigned int)pid, - "start-time", "t", startTime, - "uid", "i", (int)uid, - actionid, - virStringListLength(details) / 2, - details, - allowInteraction, - "" /* cancellation ID */) < 0) - goto cleanup; + g_variant_builder_init(&builder, G_VARIANT_TYPE("a{sv}")); + g_variant_builder_add(&builder, "{sv}", "pid", g_variant_new_uint32(pi= d)); + g_variant_builder_add(&builder, "{sv}", "start-time", g_variant_new_ui= nt64(startTime)); + g_variant_builder_add(&builder, "{sv}", "uid", g_variant_new_int32(uid= )); + gprocess =3D g_variant_builder_end(&builder); =20 - if (virDBusMessageDecode(reply, - "(bba&{ss})", - &is_authorized, - &is_challenge, - &nretdetails, - &retdetails) < 0) - goto cleanup; + g_variant_builder_init(&builder, G_VARIANT_TYPE("a{ss}")); + for (i =3D 0; i < virStringListLength(details); i +=3D 2) + g_variant_builder_add(&builder, "{ss}", details[i], details[i + 1]= ); + gdetails =3D g_variant_builder_end(&builder); =20 - for (i =3D 0; i < (nretdetails / 2); i++) { - if (STREQ(retdetails[(i * 2)], "polkit.dismissed") && - STREQ(retdetails[(i * 2) + 1], "true")) + message =3D g_variant_new("((s@a{sv})s@a{ss}us)", + "unix-process", + gprocess, + actionid, + gdetails, + allowInteraction, + "" /* cancellation ID */); + + if (virGDBusCallMethod(sysbus, + &reply, + NULL, + "org.freedesktop.PolicyKit1", + "/org/freedesktop/PolicyKit1/Authority", + "org.freedesktop.PolicyKit1.Authority", + "CheckAuthorization", + message) < 0) + return -1; + + g_variant_get(reply, "((bba{ss}))", &is_authorized, &is_challenge, &it= er); + + while (g_variant_iter_loop(iter, "{ss}", &retkey, &retval)) { + if (STREQ(retkey, "polkit.dismissed") && STREQ(retval, "true")) is_dismissed =3D true; } =20 VIR_DEBUG("is auth %d is challenge %d", is_authorized, is_challenge); =20 - if (is_authorized) { - ret =3D 0; + if (is_authorized) + return 0; + + if (is_dismissed) { + virReportError(VIR_ERR_AUTH_CANCELLED, "%s", + _("user cancelled authentication process")); + } else if (is_challenge) { + virReportError(VIR_ERR_AUTH_UNAVAILABLE, + _("no polkit agent available to authenticate action= '%s'"), + actionid); } else { - ret =3D -2; - if (is_dismissed) - virReportError(VIR_ERR_AUTH_CANCELLED, "%s", - _("user cancelled authentication process")); - else if (is_challenge) - virReportError(VIR_ERR_AUTH_UNAVAILABLE, - _("no polkit agent available to authenticate " - "action '%s'"), - actionid); - else - virReportError(VIR_ERR_AUTH_FAILED, "%s", - _("access denied by policy")); + virReportError(VIR_ERR_AUTH_FAILED, "%s", + _("access denied by policy")); } =20 - cleanup: - virStringListFreeCount(retdetails, nretdetails); - virDBusMessageUnref(reply); - return ret; + return -2; } =20 =20 diff --git a/tests/meson.build b/tests/meson.build index 0f3e4bfdd7..75bfb3effe 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -365,11 +365,6 @@ if conf.has('WITH_DBUS') { 'name': 'virsystemdtest', 'deps': [ dbus_dep ] }, ] =20 - if conf.has('WITH_POLKIT') - tests +=3D [ - { 'name': 'virpolkittest', 'deps': [ dbus_dep ] }, - ] - endif endif =20 if conf.has('WITH_ESX') @@ -446,6 +441,12 @@ if conf.has('WITH_OPENVZ') ] endif =20 +if conf.has('WITH_POLKIT') + tests +=3D [ + { 'name': 'virpolkittest' }, + ] +endif + if conf.has('WITH_QEMU') tests +=3D [ { 'name': 'qemuagenttest', 'link_with': [ test_qemu_driver_lib, test_u= tils_qemu_monitor_lib ], 'link_whole': [ test_utils_qemu_lib ] }, diff --git a/tests/virpolkittest.c b/tests/virpolkittest.c index fe7a3b5b91..011d83a506 100644 --- a/tests/virpolkittest.c +++ b/tests/virpolkittest.c @@ -22,10 +22,8 @@ =20 #if defined(__ELF__) =20 -# include - # include "virpolkit.h" -# include "virdbus.h" +# include "virgdbus.h" # include "virlog.h" # include "virmock.h" # define VIR_FROM_THIS VIR_FROM_NONE @@ -37,54 +35,43 @@ VIR_LOG_INIT("tests.systemdtest"); # define THE_TIME 11011000001 # define THE_UID 1729 =20 -VIR_MOCK_WRAP_RET_ARGS(dbus_connection_send_with_reply_and_block, - DBusMessage *, - DBusConnection *, connection, - DBusMessage *, message, - int, timeout_milliseconds, - DBusError *, error) +VIR_MOCK_WRAP_RET_ARGS(g_dbus_connection_call_sync, + GVariant *, + GDBusConnection *, connection, + const gchar *, bus_name, + const gchar *, object_path, + const gchar *, interface_name, + const gchar *, method_name, + GVariant *, parameters, + const GVariantType *, reply_type, + GDBusCallFlags, flags, + gint, timeout_msec, + GCancellable *, cancellable, + GError **, error) { - DBusMessage *reply =3D NULL; - const char *service =3D dbus_message_get_destination(message); - const char *member =3D dbus_message_get_member(message); + GVariant *reply =3D NULL; + g_autoptr(GVariant) params =3D parameters; =20 - VIR_MOCK_REAL_INIT(dbus_connection_send_with_reply_and_block); + VIR_MOCK_REAL_INIT(g_dbus_connection_call_sync); =20 - if (STREQ(service, "org.freedesktop.PolicyKit1") && - STREQ(member, "CheckAuthorization")) { + if (STREQ(bus_name, "org.freedesktop.PolicyKit1") && + STREQ(method_name, "CheckAuthorization")) { + g_autoptr(GVariantIter) iter =3D NULL; + GVariantBuilder builder; char *type; - char *pidkey; - unsigned int pidval; - char *timekey; - unsigned long long timeval; - char *uidkey; - int uidval; char *actionid; - char **details; - size_t detailslen; - int allowInteraction; - char *cancellationId; - const char **retdetails =3D NULL; - size_t retdetailslen =3D 0; - const char *retdetailscancelled[] =3D { - "polkit.dismissed", "true", - }; int is_authorized =3D 1; int is_challenge =3D 0; =20 - if (virDBusMessageDecode(message, - "(sa{sv})sa&{ss}us", - &type, - 3, - &pidkey, "u", &pidval, - &timekey, "t", &timeval, - &uidkey, "i", &uidval, - &actionid, - &detailslen, - &details, - &allowInteraction, - &cancellationId) < 0) - goto error; + g_variant_get(params, "((&s@a{sv})&sa{ss}@u@s)", + &type, + NULL, + &actionid, + &iter, + NULL, + NULL); + + g_variant_builder_init(&builder, G_VARIANT_TYPE("a{ss}")); =20 if (STREQ(actionid, "org.libvirt.test.success")) { is_authorized =3D 1; @@ -95,17 +82,15 @@ VIR_MOCK_WRAP_RET_ARGS(dbus_connection_send_with_reply_= and_block, } else if (STREQ(actionid, "org.libvirt.test.cancelled")) { is_authorized =3D 0; is_challenge =3D 0; - retdetails =3D retdetailscancelled; - retdetailslen =3D G_N_ELEMENTS(retdetailscancelled) / 2; + g_variant_builder_add(&builder, "{ss}", "polkit.dismissed", "t= rue"); } else if (STREQ(actionid, "org.libvirt.test.details")) { - size_t i; + char *key; + char *val; is_authorized =3D 0; is_challenge =3D 0; - for (i =3D 0; i < detailslen / 2; i++) { - if (STREQ(details[i * 2], - "org.libvirt.test.person") && - STREQ(details[(i * 2) + 1], - "Fred")) { + + while (g_variant_iter_loop(iter, "{ss}", &key, &val)) { + if (STREQ(key, "org.libvirt.test.person") && STREQ(val, "F= red")) { is_authorized =3D 1; is_challenge =3D 0; } @@ -115,30 +100,13 @@ VIR_MOCK_WRAP_RET_ARGS(dbus_connection_send_with_repl= y_and_block, is_challenge =3D 0; } =20 - VIR_FREE(type); - VIR_FREE(pidkey); - VIR_FREE(timekey); - VIR_FREE(uidkey); - VIR_FREE(actionid); - VIR_FREE(cancellationId); - virStringListFreeCount(details, detailslen); - - if (virDBusCreateReply(&reply, - "(bba&{ss})", - is_authorized, - is_challenge, - retdetailslen, - retdetails) < 0) - goto error; + reply =3D g_variant_new("((bb@a{ss}))", is_authorized, is_challeng= e, + g_variant_builder_end(&builder)); } else { - reply =3D dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_RETURN); + reply =3D g_variant_new("()"); } =20 return reply; - - error: - virDBusMessageUnref(reply); - return NULL; } =20 =20 @@ -322,7 +290,7 @@ mymain(void) return ret =3D=3D 0 ? EXIT_SUCCESS : EXIT_FAILURE; } =20 -VIR_TEST_MAIN_PRELOAD(mymain, VIR_TEST_MOCK("virdbus")) +VIR_TEST_MAIN_PRELOAD(mymain, VIR_TEST_MOCK("virgdbus")) =20 #else /* ! __ELF__ */ int --=20 2.26.2 From nobody Sat May 4 20:55:56 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 63.128.21.124 as permitted sender) client-ip=63.128.21.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 63.128.21.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1600331492; cv=none; d=zohomail.com; s=zohoarc; b=AqGHrtYJwPOL3c9lsShZBgyPngg731vjahr7/uw0DYB3/IEZUAGwaGG8CdyirGFataqaC00lDluABzYMcZmnTE2gvMMskLLrZIcsePbU/sKE9T8aVBXcVPjiI9pmAfw/3Akq18XHw/VGSfYWOmFw39T5gXYKE3WnYCMLAj1Wzqs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1600331492; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=FJbvcRbjnrjZTeNN3sIWhjxt8uKKguq+7ZS3oxO/wHM=; b=ecC44yWwt7ehfhjO99PT2rceZGP5Iwls7iDuLy5kMrs3PTge1fna76wW8Qd2PS8HR7WhSIhlkK9tYbvr3TEy8SuEK4cmgEhFZjFWADkZzxWBihK/SmPnCVyWN9ItZwqg0+b1+q84fjQRe/qpiQBYedzI2OWIQVTGmhyhoDRzmLc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 63.128.21.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [63.128.21.124]) by mx.zohomail.com with SMTPS id 1600331492582467.52782497012413; Thu, 17 Sep 2020 01:31:32 -0700 (PDT) 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-26-Ak2X7TlDMFGneQmPOz_2hw-1; Thu, 17 Sep 2020 04:31:29 -0400 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 mimecast-mx01.redhat.com (Postfix) with ESMTPS id 2800185C732; Thu, 17 Sep 2020 08:31:24 +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 EC2AD10027AB; Thu, 17 Sep 2020 08:31:23 +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 396208C7B9; Thu, 17 Sep 2020 08:31:23 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 08H8U1Pr004117 for ; Thu, 17 Sep 2020 04:30:01 -0400 Received: by smtp.corp.redhat.com (Postfix) id 3191A5DEBF; Thu, 17 Sep 2020 08:30:01 +0000 (UTC) Received: from antique-work.redhat.com (unknown [10.40.195.159]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9F5395DA30 for ; Thu, 17 Sep 2020 08:30:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1600331491; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=FJbvcRbjnrjZTeNN3sIWhjxt8uKKguq+7ZS3oxO/wHM=; b=RaHga3nqTeXwbed3uEjNu6l/gOExlmy4YwPHGhmd8XlOsRM6JcIgm30IaDUn30V0Cy//iA rwtQD2L8xutjJC2XKJmgwrY0h4VJHGblUyhQndI17/Sxv5XqBnUQYUg1aSMa+w2evd/BKG M+LkrOsFgxFB/Pti49o79vsimpaXsE4= X-MC-Unique: Ak2X7TlDMFGneQmPOz_2hw-1 From: Pavel Hrdina To: libvir-list@redhat.com Subject: [libvirt PATCH 07/14] src/util/virfirewalld: convert to use GLib DBus Date: Thu, 17 Sep 2020 10:29:42 +0200 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com 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: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" Signed-off-by: Pavel Hrdina Reviewed-by: Michal Privoznik Reviewed-by: Neal Gompa --- src/util/virfirewalld.c | 197 ++++++++++++++++---------------- tests/meson.build | 4 +- tests/networkxml2firewalltest.c | 39 ++++--- tests/virfirewalltest.c | 154 ++++++++++--------------- 4 files changed, 180 insertions(+), 214 deletions(-) diff --git a/src/util/virfirewalld.c b/src/util/virfirewalld.c index c14a6b6e65..69c8b73da0 100644 --- a/src/util/virfirewalld.c +++ b/src/util/virfirewalld.c @@ -30,7 +30,7 @@ #include "virerror.h" #include "virutil.h" #include "virlog.h" -#include "virdbus.h" +#include "virgdbus.h" #include "virenum.h" =20 #define VIR_FROM_THIS VIR_FROM_FIREWALLD @@ -66,7 +66,7 @@ VIR_ENUM_IMPL(virFirewallDBackend, int virFirewallDIsRegistered(void) { - return virDBusIsServiceRegistered(VIR_FIREWALL_FIREWALLD_SERVICE); + return virGDBusIsServiceRegistered(VIR_FIREWALL_FIREWALLD_SERVICE); } =20 /** @@ -82,42 +82,40 @@ virFirewallDIsRegistered(void) int virFirewallDGetVersion(unsigned long *version) { - int ret =3D -1; - DBusConnection *sysbus =3D virDBusGetSystemBus(); - DBusMessage *reply =3D NULL; - g_autofree char *versionStr =3D NULL; + GDBusConnection *sysbus =3D virGDBusGetSystemBus(); + g_autoptr(GVariant) message =3D NULL; + g_autoptr(GVariant) reply =3D NULL; + g_autoptr(GVariant) gvar =3D NULL; + char *versionStr; =20 if (!sysbus) return -1; =20 - if (virDBusCallMethod(sysbus, - &reply, - NULL, - VIR_FIREWALL_FIREWALLD_SERVICE, - "/org/fedoraproject/FirewallD1", - "org.freedesktop.DBus.Properties", - "Get", - "ss", - "org.fedoraproject.FirewallD1", - "version") < 0) - goto cleanup; + message =3D g_variant_new("(ss)", "org.fedoraproject.FirewallD1", "ver= sion"); =20 - if (virDBusMessageDecode(reply, "v", "s", &versionStr) < 0) - goto cleanup; + if (virGDBusCallMethod(sysbus, + &reply, + NULL, + VIR_FIREWALL_FIREWALLD_SERVICE, + "/org/fedoraproject/FirewallD1", + "org.freedesktop.DBus.Properties", + "Get", + message) < 0) + return -1; + + g_variant_get(reply, "(v)", &gvar); + g_variant_get(gvar, "&s", &versionStr); =20 if (virParseVersionString(versionStr, version, false) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, _("Failed to parse firewalld version '%s'"), versionStr); - goto cleanup; + return -1; } =20 VIR_DEBUG("FirewallD version: %s - %lu", versionStr, *version); =20 - ret =3D 0; - cleanup: - virDBusMessageUnref(reply); - return ret; + return 0; } =20 /** @@ -129,42 +127,46 @@ virFirewallDGetVersion(unsigned long *version) int virFirewallDGetBackend(void) { - DBusConnection *sysbus =3D virDBusGetSystemBus(); - DBusMessage *reply =3D NULL; - virError error; - g_autofree char *backendStr =3D NULL; + GDBusConnection *sysbus =3D virGDBusGetSystemBus(); + g_autoptr(GVariant) message =3D NULL; + g_autoptr(GVariant) reply =3D NULL; + g_autoptr(GVariant) gvar =3D NULL; + g_autoptr(virError) error =3D NULL; + char *backendStr =3D NULL; int backend =3D -1; =20 if (!sysbus) return -1; =20 - memset(&error, 0, sizeof(error)); + if (VIR_ALLOC(error) < 0) + return -1; =20 - if (virDBusCallMethod(sysbus, - &reply, - &error, - VIR_FIREWALL_FIREWALLD_SERVICE, - "/org/fedoraproject/FirewallD1/config", - "org.freedesktop.DBus.Properties", - "Get", - "ss", - "org.fedoraproject.FirewallD1.config", - "FirewallBackend") < 0) - goto cleanup; + message =3D g_variant_new("(ss)", + "org.fedoraproject.FirewallD1.config", + "FirewallBackend"); =20 - if (error.level =3D=3D VIR_ERR_ERROR) { + if (virGDBusCallMethod(sysbus, + &reply, + error, + VIR_FIREWALL_FIREWALLD_SERVICE, + "/org/fedoraproject/FirewallD1/config", + "org.freedesktop.DBus.Properties", + "Get", + message) < 0) + return -1; + + if (error->level =3D=3D VIR_ERR_ERROR) { /* we don't want to log any error in the case that * FirewallBackend isn't implemented in this firewalld, since * that just means that it is an old version, and only has an * iptables backend. */ VIR_DEBUG("Failed to get FirewallBackend setting, assuming 'iptabl= es'"); - backend =3D VIR_FIREWALLD_BACKEND_IPTABLES; - goto cleanup; + return VIR_FIREWALLD_BACKEND_IPTABLES; } =20 - if (virDBusMessageDecode(reply, "v", "s", &backendStr) < 0) - goto cleanup; + g_variant_get(reply, "(v)", &gvar); + g_variant_get(gvar, "&s", &backendStr); =20 VIR_DEBUG("FirewallD backend: %s", backendStr); =20 @@ -172,12 +174,9 @@ virFirewallDGetBackend(void) virReportError(VIR_ERR_INTERNAL_ERROR, _("Unrecognized firewalld backend type: %s"), backendStr); - goto cleanup; + return -1; } =20 - cleanup: - virResetError(&error); - virDBusMessageUnref(reply); return backend; } =20 @@ -196,9 +195,9 @@ virFirewallDGetBackend(void) int virFirewallDGetZones(char ***zones, size_t *nzones) { - DBusConnection *sysbus =3D virDBusGetSystemBus(); - DBusMessage *reply =3D NULL; - int ret =3D -1; + GDBusConnection *sysbus =3D virGDBusGetSystemBus(); + g_autoptr(GVariant) reply =3D NULL; + g_autoptr(GVariant) array =3D NULL; =20 *nzones =3D 0; *zones =3D NULL; @@ -206,23 +205,20 @@ virFirewallDGetZones(char ***zones, size_t *nzones) if (!sysbus) return -1; =20 - if (virDBusCallMethod(sysbus, - &reply, - NULL, - VIR_FIREWALL_FIREWALLD_SERVICE, - "/org/fedoraproject/FirewallD1", - "org.fedoraproject.FirewallD1.zone", - "getZones", - NULL) < 0) - goto cleanup; + if (virGDBusCallMethod(sysbus, + &reply, + NULL, + VIR_FIREWALL_FIREWALLD_SERVICE, + "/org/fedoraproject/FirewallD1", + "org.fedoraproject.FirewallD1.zone", + "getZones", + NULL) < 0) + return -1; =20 - if (virDBusMessageDecode(reply, "a&s", nzones, zones) < 0) - goto cleanup; + g_variant_get(reply, "(@as)", array); + *zones =3D g_variant_dup_strv(array, nzones); =20 - ret =3D 0; - cleanup: - virDBusMessageUnref(reply); - return ret; + return 0; } =20 =20 @@ -273,10 +269,10 @@ virFirewallDApplyRule(virFirewallLayer layer, char **output) { const char *ipv =3D virFirewallLayerFirewallDTypeToString(layer); - DBusConnection *sysbus =3D virDBusGetSystemBus(); - DBusMessage *reply =3D NULL; - virError error; - int ret =3D -1; + GDBusConnection *sysbus =3D virGDBusGetSystemBus(); + g_autoptr(GVariant) message =3D NULL; + g_autoptr(GVariant) reply =3D NULL; + g_autoptr(virError) error =3D NULL; =20 if (!sysbus) return -1; @@ -287,23 +283,27 @@ virFirewallDApplyRule(virFirewallLayer layer, virReportError(VIR_ERR_INTERNAL_ERROR, _("Unknown firewall layer %d"), layer); - goto cleanup; + return -1; } =20 - if (virDBusCallMethod(sysbus, - &reply, - &error, - VIR_FIREWALL_FIREWALLD_SERVICE, - "/org/fedoraproject/FirewallD1", - "org.fedoraproject.FirewallD1.direct", - "passthrough", - "sa&s", - ipv, - (int)argsLen, - args) < 0) - goto cleanup; + if (VIR_ALLOC(error) < 0) + return -1; =20 - if (error.level =3D=3D VIR_ERR_ERROR) { + message =3D g_variant_new("(s@as)", + ipv, + g_variant_new_strv((const char * const*)args, = argsLen)); + + if (virGDBusCallMethod(sysbus, + &reply, + error, + VIR_FIREWALL_FIREWALLD_SERVICE, + "/org/fedoraproject/FirewallD1", + "org.fedoraproject.FirewallD1.direct", + "passthrough", + message) < 0) + return -1; + + if (error->level =3D=3D VIR_ERR_ERROR) { /* * As of firewalld-0.3.9.3-1.fc20.noarch the name and * message fields in the error look like @@ -331,22 +331,16 @@ virFirewallDApplyRule(virFirewallLayer layer, */ if (ignoreErrors) { VIR_DEBUG("Ignoring error '%s': '%s'", - error.str1, error.message); + error->str1, error->message); } else { - virReportErrorObject(&error); - goto cleanup; + virReportErrorObject(error); + return -1; } } else { - if (virDBusMessageDecode(reply, "s", output) < 0) - goto cleanup; + g_variant_get(reply, "(s)", output); } =20 - ret =3D 0; - - cleanup: - virResetError(&error); - virDBusMessageUnref(reply); - return ret; + return 0; } =20 =20 @@ -354,19 +348,20 @@ int virFirewallDInterfaceSetZone(const char *iface, const char *zone) { - DBusConnection *sysbus =3D virDBusGetSystemBus(); + GDBusConnection *sysbus =3D virGDBusGetSystemBus(); + g_autoptr(GVariant) message =3D NULL; =20 if (!sysbus) return -1; =20 - return virDBusCallMethod(sysbus, + message =3D g_variant_new("(ss)", zone, iface); + + return virGDBusCallMethod(sysbus, NULL, NULL, VIR_FIREWALL_FIREWALLD_SERVICE, "/org/fedoraproject/FirewallD1", "org.fedoraproject.FirewallD1.zone", "changeZoneOfInterface", - "ss", - zone, - iface); + message); } diff --git a/tests/meson.build b/tests/meson.build index 75bfb3effe..2c4f044d30 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -314,7 +314,7 @@ tests +=3D [ { 'name': 'virerrortest' }, { 'name': 'virfilecachetest' }, { 'name': 'virfiletest' }, - { 'name': 'virfirewalltest', 'deps': [ dbus_dep ] }, + { 'name': 'virfirewalltest' }, { 'name': 'virhashtest' }, { 'name': 'virhostcputest', 'link_whole': [ test_file_wrapper_lib ] }, { 'name': 'virhostdevtest' }, @@ -400,7 +400,7 @@ endif if conf.has('WITH_NETWORK') tests +=3D [ { 'name': 'networkxml2conftest', 'link_with': [ network_driver_impl ] = }, - { 'name': 'networkxml2firewalltest', 'link_with': [ network_driver_imp= l ], 'deps': [ dbus_dep ] }, + { 'name': 'networkxml2firewalltest', 'link_with': [ network_driver_imp= l ] }, { 'name': 'networkxml2xmltest', 'link_with': [ network_driver_impl ] }, ] endif diff --git a/tests/networkxml2firewalltest.c b/tests/networkxml2firewalltes= t.c index 80e2d6a035..e0244f508e 100644 --- a/tests/networkxml2firewalltest.c +++ b/tests/networkxml2firewalltest.c @@ -26,9 +26,7 @@ =20 #if defined (__linux__) =20 -# if WITH_DBUS -# include -# endif +# include =20 # include "network/bridge_driver_platform.h" # include "virbuffer.h" @@ -48,21 +46,30 @@ # error "test case not ported to this platform" # endif =20 -# if WITH_DBUS -VIR_MOCK_WRAP_RET_ARGS(dbus_connection_send_with_reply_and_block, - DBusMessage *, - DBusConnection *, connection, - DBusMessage *, message, - int, timeout_milliseconds, - DBusError *, error) +VIR_MOCK_WRAP_RET_ARGS(g_dbus_connection_call_sync, + GVariant *, + GDBusConnection *, connection, + const gchar *, bus_name, + const gchar *, object_path, + const gchar *, interface_name, + const gchar *, method_name, + GVariant *, parameters, + const GVariantType *, reply_type, + GDBusCallFlags, flags, + gint, timeout_msec, + GCancellable *, cancellable, + GError **, error) { - VIR_MOCK_REAL_INIT(dbus_connection_send_with_reply_and_block); + if (parameters) + g_variant_unref(parameters); =20 - dbus_set_error_const(error, "org.freedesktop.error", "dbus is disabled= "); + VIR_MOCK_REAL_INIT(g_dbus_connection_call_sync); + + *error =3D g_dbus_error_new_for_dbus_error("org.freedesktop.error", + "dbus is disabled"); =20 return NULL; } -# endif =20 static void testCommandDryRun(const char *const*args G_GNUC_UNUSED, @@ -197,11 +204,7 @@ mymain(void) return ret =3D=3D 0 ? EXIT_SUCCESS : EXIT_FAILURE; } =20 -# if WITH_DBUS -VIR_TEST_MAIN_PRELOAD(mymain, VIR_TEST_MOCK("virdbus")) -# else -VIR_TEST_MAIN(mymain) -# endif +VIR_TEST_MAIN_PRELOAD(mymain, VIR_TEST_MOCK("virgdbus")) =20 #else /* ! defined (__linux__) */ =20 diff --git a/tests/virfirewalltest.c b/tests/virfirewalltest.c index ce252bd0e0..607638e9d0 100644 --- a/tests/virfirewalltest.c +++ b/tests/virfirewalltest.c @@ -22,6 +22,8 @@ =20 #if defined(__linux__) =20 +# include + # include "virbuffer.h" # define LIBVIRT_VIRCOMMANDPRIV_H_ALLOW # include "vircommandpriv.h" @@ -30,15 +32,9 @@ # define LIBVIRT_VIRFIREWALLDPRIV_H_ALLOW # include "virfirewalldpriv.h" # include "virmock.h" -# define LIBVIRT_VIRDBUSPRIV_H_ALLOW -# include "virdbuspriv.h" =20 # define VIR_FROM_THIS VIR_FROM_FIREWALL =20 -# if WITH_DBUS -# include -# endif - static bool fwDisabled =3D true; static virBufferPtr fwBuf; static bool fwError; @@ -66,64 +62,64 @@ static bool fwError; "Chain POSTROUTING (policy ACCEPT)\n" \ "target prot opt source destination\n" =20 -# if WITH_DBUS -VIR_MOCK_WRAP_RET_ARGS(dbus_connection_send_with_reply_and_block, - DBusMessage *, - DBusConnection *, connection, - DBusMessage *, message, - int, timeout_milliseconds, - DBusError *, error) +VIR_MOCK_WRAP_RET_ARGS(g_dbus_connection_call_sync, + GVariant *, + GDBusConnection *, connection, + const gchar *, bus_name, + const gchar *, object_path, + const gchar *, interface_name, + const gchar *, method_name, + GVariant *, parameters, + const GVariantType *, reply_type, + GDBusCallFlags, flags, + gint, timeout_msec, + GCancellable *, cancellable, + GError **, error) { - DBusMessage *reply =3D NULL; - const char *service =3D dbus_message_get_destination(message); - const char *member =3D dbus_message_get_member(message); - size_t i; - size_t nargs =3D 0; - char **args =3D NULL; - char *type =3D NULL; + GVariant *reply =3D NULL; + g_autoptr(GVariant) params =3D parameters; =20 - VIR_MOCK_REAL_INIT(dbus_connection_send_with_reply_and_block); + VIR_MOCK_REAL_INIT(g_dbus_connection_call_sync); =20 - if (STREQ(service, "org.freedesktop.DBus") && - STREQ(member, "ListNames")) { - const char *svc1 =3D "org.foo.bar.wizz"; - const char *svc2 =3D VIR_FIREWALL_FIREWALLD_SERVICE; - DBusMessageIter iter; - DBusMessageIter sub; - reply =3D dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_RETURN); - dbus_message_iter_init_append(reply, &iter); - dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, - "s", &sub); + if (STREQ(bus_name, "org.freedesktop.DBus") && + STREQ(method_name, "ListNames")) { + GVariantBuilder builder; =20 - if (!dbus_message_iter_append_basic(&sub, - DBUS_TYPE_STRING, - &svc1)) - goto error; - if (!fwDisabled && - !dbus_message_iter_append_basic(&sub, - DBUS_TYPE_STRING, - &svc2)) - goto error; - dbus_message_iter_close_container(&iter, &sub); - } else if (STREQ(service, VIR_FIREWALL_FIREWALLD_SERVICE) && - STREQ(member, "passthrough")) { + g_variant_builder_init(&builder, G_VARIANT_TYPE("(as)")); + g_variant_builder_open(&builder, G_VARIANT_TYPE("as")); + + g_variant_builder_add(&builder, "s", "org.foo.bar.wizz"); + + if (!fwDisabled) + g_variant_builder_add(&builder, "s", VIR_FIREWALL_FIREWALLD_SE= RVICE); + + g_variant_builder_close(&builder); + + reply =3D g_variant_builder_end(&builder); + } else if (STREQ(bus_name, VIR_FIREWALL_FIREWALLD_SERVICE) && + STREQ(method_name, "passthrough")) { + g_autoptr(GVariantIter) iter =3D NULL; + VIR_AUTOSTRINGLIST args =3D NULL; + size_t nargs =3D 0; + char *type =3D NULL; + char *item =3D NULL; bool isAdd =3D false; bool doError =3D false; + size_t i; =20 - if (virDBusMessageDecode(message, - "sa&s", - &type, - &nargs, - &args) < 0) - goto error; + g_variant_get(params, "(&sas)", &type, &iter); =20 - for (i =3D 0; i < nargs; i++) { + nargs =3D g_variant_iter_n_children(iter); + + while (g_variant_iter_loop(iter, "s", &item)) { /* Fake failure on the command with this IP addr */ - if (STREQ(args[i], "-A")) { + if (STREQ(item, "-A")) { isAdd =3D true; - } else if (isAdd && STREQ(args[i], "192.168.122.255")) { + } else if (isAdd && STREQ(item, "192.168.122.255")) { doError =3D true; } + + virStringListAdd(&args, g_strdup(item)); } =20 if (fwBuf) { @@ -134,61 +130,42 @@ VIR_MOCK_WRAP_RET_ARGS(dbus_connection_send_with_repl= y_and_block, else virBufferAddLit(fwBuf, EBTABLES_PATH); } + for (i =3D 0; i < nargs; i++) { if (fwBuf) { virBufferAddLit(fwBuf, " "); virBufferEscapeShell(fwBuf, args[i]); } } + if (fwBuf) virBufferAddLit(fwBuf, "\n"); + if (doError) { - dbus_set_error_const(error, - "org.firewalld.error", - "something bad happened"); + if (error) + *error =3D g_dbus_error_new_for_dbus_error("org.firewalld.= error", + "something bad ha= ppened"); } else { if (nargs =3D=3D 1 && STREQ(type, "ipv4") && STREQ(args[0], "-L")) { - if (virDBusCreateReply(&reply, - "s", TEST_FILTER_TABLE_LIST) < 0) - goto error; + reply =3D g_variant_new("(s)", TEST_FILTER_TABLE_LIST); } else if (nargs =3D=3D 3 && STREQ(type, "ipv4") && STREQ(args[0], "-t") && STREQ(args[1], "nat") && STREQ(args[2], "-L")) { - if (virDBusCreateReply(&reply, - "s", TEST_NAT_TABLE_LIST) < 0) - goto error; + reply =3D g_variant_new("(s)", TEST_NAT_TABLE_LIST); } else { - if (virDBusCreateReply(&reply, - "s", "success") < 0) - goto error; + reply =3D g_variant_new("(s)", "success"); } } } else { - reply =3D dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_RETURN); + reply =3D g_variant_new("()"); } =20 - cleanup: - VIR_FREE(type); - for (i =3D 0; i < nargs; i++) - VIR_FREE(args[i]); - VIR_FREE(args); return reply; - - error: - virDBusMessageUnref(reply); - reply =3D NULL; - if (error && !dbus_error_is_set(error)) - dbus_set_error_const(error, - "org.firewalld.error", - "something unexpected happened"); - - goto cleanup; } -# endif =20 struct testFirewallData { virFirewallBackend tryBackend; @@ -1073,8 +1050,7 @@ mymain(void) ret =3D -1; \ } while (0) =20 -# if WITH_DBUS -# define RUN_TEST_FIREWALLD(name, method) \ +# define RUN_TEST_FIREWALLD(name, method) \ do { \ struct testFirewallData data; \ data.tryBackend =3D VIR_FIREWALL_BACKEND_AUTOMATIC; \ @@ -1089,13 +1065,9 @@ mymain(void) ret =3D -1; \ } while (0) =20 -# define RUN_TEST(name, method) \ +# define RUN_TEST(name, method) \ RUN_TEST_DIRECT(name, method); \ RUN_TEST_FIREWALLD(name, method) -# else /* ! WITH_DBUS */ -# define RUN_TEST(name, method) \ - RUN_TEST_DIRECT(name, method) -# endif /* ! WITH_DBUS */ =20 virFirewallSetLockOverride(true); =20 @@ -1113,11 +1085,7 @@ mymain(void) return ret =3D=3D 0 ? EXIT_SUCCESS : EXIT_FAILURE; } =20 -# if WITH_DBUS -VIR_TEST_MAIN_PRELOAD(mymain, VIR_TEST_MOCK("virdbus")) -# else -VIR_TEST_MAIN(mymain) -# endif +VIR_TEST_MAIN_PRELOAD(mymain, VIR_TEST_MOCK("virgdbus")) =20 #else /* ! defined (__linux__) */ =20 --=20 2.26.2 From nobody Sat May 4 20:55:56 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) client-ip=216.205.24.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1600331505; cv=none; d=zohomail.com; s=zohoarc; b=SsCbdBVo1tA7Z5Jva4vzxgfggR11Na85KhwB0Y/PGLkbpBxxGRNOFPAOthS+RH4rtZKAF5hh7dJe9/9WsyVA7czl8T9FY4h0dUGrH3SxgDJaWZkwkWoQTyLoqeJNKqADe6JKRCop3CFhdev+9yaZm1L0UEpRXroLKGntNTnrSNo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1600331505; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=/pZzuB4uZkPREHCfH/x1rY44tIXpkc/fZpT0/p84KoM=; b=OH24qfKGG6WGTP+cx8qhDtaVIl9hL2X+6U9+JvK4wTGwD1ugDUbQL7u/qGGffrbyqesrriKWK5D8IngJH9xHEoetH4P9JLABxVMAZFP7CZNYdtoZO+F+pGkxWsueFziQrKlqfcmVjP8hDfYkrb5293F5mFVqQh8JcbYPTTxZuWM= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by mx.zohomail.com with SMTPS id 1600331505378554.6827311042401; Thu, 17 Sep 2020 01:31:45 -0700 (PDT) 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-151-smW9a5xkPSKzAfni6HxY-g-1; Thu, 17 Sep 2020 04:31:41 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id F15DF83DBBE; Thu, 17 Sep 2020 08:31:35 +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 CF1B47E46E; Thu, 17 Sep 2020 08:31:35 +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 94CEA8C7CF; Thu, 17 Sep 2020 08:31:35 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 08H8U8h8004146 for ; Thu, 17 Sep 2020 04:30:08 -0400 Received: by smtp.corp.redhat.com (Postfix) id 417D05DA30; Thu, 17 Sep 2020 08:30:08 +0000 (UTC) Received: from antique-work.redhat.com (unknown [10.40.195.159]) by smtp.corp.redhat.com (Postfix) with ESMTP id AE52B5DEFE for ; Thu, 17 Sep 2020 08:30:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1600331504; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=/pZzuB4uZkPREHCfH/x1rY44tIXpkc/fZpT0/p84KoM=; b=UIyw8dXyxURquBnZ59nz8BB8jDs9/JkSxue1buxkQEwVzAdV1f0PONv6Ds6lgb0MJuWgpz 5qWAWgfrDOgqc/irRUjGhJuOFH7D3Fq8GvSajdTsVeuqj/Zeu2O5uS5nwIXA3onKdL9kc+ syugnPpggFQgJ1AqgM3+R8Sh+NaauEs= X-MC-Unique: smW9a5xkPSKzAfni6HxY-g-1 From: Pavel Hrdina To: libvir-list@redhat.com Subject: [libvirt PATCH 08/14] src/util/virsystemd: convert to use GLib DBus Date: Thu, 17 Sep 2020 10:29:43 +0200 Message-Id: <3de301b3915b091156cd1510f17c20ce7846bf13.1600331276.git.phrdina@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com 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: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0.001 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" Signed-off-by: Pavel Hrdina Reviewed-by: Michal Privoznik Reviewed-by: Neal Gompa --- src/util/virsystemd.c | 371 +++++++++++++++++++++-------------------- tests/meson.build | 2 +- tests/virsystemdtest.c | 173 +++++++------------ 3 files changed, 254 insertions(+), 292 deletions(-) diff --git a/src/util/virsystemd.c b/src/util/virsystemd.c index 0ae896f4a3..32c830c002 100644 --- a/src/util/virsystemd.c +++ b/src/util/virsystemd.c @@ -26,7 +26,7 @@ =20 #include "virsystemd.h" #include "virbuffer.h" -#include "virdbus.h" +#include "virgdbus.h" #include "virstring.h" #include "viralloc.h" #include "virutil.h" @@ -157,13 +157,13 @@ virSystemdHasMachined(void) if (val !=3D -1) return val; =20 - if ((ret =3D virDBusIsServiceEnabled("org.freedesktop.machine1")) < 0)= { + if ((ret =3D virGDBusIsServiceEnabled("org.freedesktop.machine1")) < 0= ) { if (ret =3D=3D -2) g_atomic_int_set(&virSystemdHasMachinedCachedValue, -2); return ret; } =20 - if ((ret =3D virDBusIsServiceRegistered("org.freedesktop.systemd1")) = =3D=3D -1) + if ((ret =3D virGDBusIsServiceRegistered("org.freedesktop.systemd1")) = =3D=3D -1) return ret; g_atomic_int_set(&virSystemdHasMachinedCachedValue, ret); return ret; @@ -179,14 +179,14 @@ virSystemdHasLogind(void) if (val !=3D -1) return val; =20 - ret =3D virDBusIsServiceEnabled("org.freedesktop.login1"); + ret =3D virGDBusIsServiceEnabled("org.freedesktop.login1"); if (ret < 0) { if (ret =3D=3D -2) g_atomic_int_set(&virSystemdHasLogindCachedValue, -2); return ret; } =20 - if ((ret =3D virDBusIsServiceRegistered("org.freedesktop.login1")) =3D= =3D -1) + if ((ret =3D virGDBusIsServiceRegistered("org.freedesktop.login1")) = =3D=3D -1) return ret; =20 g_atomic_int_set(&virSystemdHasLogindCachedValue, ret); @@ -197,53 +197,59 @@ virSystemdHasLogind(void) char * virSystemdGetMachineNameByPID(pid_t pid) { - DBusConnection *conn; - DBusMessage *reply =3D NULL; - char *name =3D NULL, *object =3D NULL; + GDBusConnection *conn; + g_autoptr(GVariant) message =3D NULL; + g_autoptr(GVariant) reply =3D NULL; + g_autoptr(GVariant) gvar =3D NULL; + g_autofree char *object =3D NULL; + char *name =3D NULL; =20 if (virSystemdHasMachined() < 0) - goto cleanup; + return NULL; =20 - if (!(conn =3D virDBusGetSystemBus())) - goto cleanup; + if (!(conn =3D virGDBusGetSystemBus())) + return NULL; =20 - if (virDBusCallMethod(conn, &reply, NULL, - "org.freedesktop.machine1", - "/org/freedesktop/machine1", - "org.freedesktop.machine1.Manager", - "GetMachineByPID", - "u", pid) < 0) - goto cleanup; + message =3D g_variant_new("(u)", pid); =20 - if (virDBusMessageDecode(reply, "o", &object) < 0) - goto cleanup; + if (virGDBusCallMethod(conn, + &reply, + NULL, + "org.freedesktop.machine1", + "/org/freedesktop/machine1", + "org.freedesktop.machine1.Manager", + "GetMachineByPID", + message) < 0) + return NULL; =20 - virDBusMessageUnref(reply); + g_variant_get(reply, "(o)", &object); + + g_variant_unref(reply); reply =3D NULL; =20 VIR_DEBUG("Domain with pid %lld has object path '%s'", (long long) pid, object); =20 - if (virDBusCallMethod(conn, &reply, NULL, - "org.freedesktop.machine1", - object, - "org.freedesktop.DBus.Properties", - "Get", - "ss", - "org.freedesktop.machine1.Machine", - "Name") < 0) - goto cleanup; + g_variant_unref(message); + message =3D g_variant_new("(ss)", + "org.freedesktop.machine1.Machine", "Name"); =20 - if (virDBusMessageDecode(reply, "v", "s", &name) < 0) - goto cleanup; + if (virGDBusCallMethod(conn, + &reply, + NULL, + "org.freedesktop.machine1", + object, + "org.freedesktop.DBus.Properties", + "Get", + message) < 0) + return NULL; + + g_variant_get(reply, "(v)", &gvar); + g_variant_get(gvar, "s", &name); =20 VIR_DEBUG("Domain with pid %lld has machine name '%s'", (long long) pid, name); =20 - cleanup: - VIR_FREE(object); - virDBusMessageUnref(reply); - return name; } =20 @@ -274,26 +280,28 @@ int virSystemdCreateMachine(const char *name, const char *partition, unsigned int maxthreads) { - int ret; - DBusConnection *conn; - char *creatorname =3D NULL; - char *slicename =3D NULL; - char *scopename =3D NULL; + int rc; + GDBusConnection *conn; + GVariant *guuid; + GVariant *gnicindexes; + GVariant *gprops; + GVariant *message; + g_autofree char *creatorname =3D NULL; + g_autofree char *slicename =3D NULL; + g_autofree char *scopename =3D NULL; static int hasCreateWithNetwork =3D 1; =20 - if ((ret =3D virSystemdHasMachined()) < 0) - return ret; + if ((rc =3D virSystemdHasMachined()) < 0) + return rc; =20 - if (!(conn =3D virDBusGetSystemBus())) + if (!(conn =3D virGDBusGetSystemBus())) return -1; =20 - ret =3D -1; - creatorname =3D g_strdup_printf("libvirt-%s", drivername); =20 if (partition) { if (!(slicename =3D virSystemdMakeSliceName(partition))) - goto cleanup; + return -1; } else { slicename =3D g_strdup(""); } @@ -352,122 +360,138 @@ int virSystemdCreateMachine(const char *name, =20 VIR_DEBUG("Attempting to create machine via systemd"); if (g_atomic_int_get(&hasCreateWithNetwork)) { - virError error; - memset(&error, 0, sizeof(error)); + g_autoptr(virError) error =3D NULL; =20 - if (virDBusCallMethod(conn, - NULL, - &error, - "org.freedesktop.machine1", - "/org/freedesktop/machine1", - "org.freedesktop.machine1.Manager", - "CreateMachineWithNetwork", - "sayssusa&ia(sv)", - name, - 16, - uuid[0], uuid[1], uuid[2], uuid[3], - uuid[4], uuid[5], uuid[6], uuid[7], - uuid[8], uuid[9], uuid[10], uuid[11], - uuid[12], uuid[13], uuid[14], uuid[15], - creatorname, - iscontainer ? "container" : "vm", - (unsigned int)pidleader, - NULLSTR_EMPTY(rootdir), - nnicindexes, nicindexes, - 3, - "Slice", "s", slicename, - "After", "as", 1, "libvirtd.service", - "Before", "as", 1, "virt-guest-shutdown.targ= et") < 0) - goto cleanup; + if (VIR_ALLOC(error) < 0) + return -1; =20 - if (error.level =3D=3D VIR_ERR_ERROR) { - if (virDBusErrorIsUnknownMethod(&error)) { + guuid =3D g_variant_new_fixed_array(G_VARIANT_TYPE("y"), + uuid, 16, sizeof(unsigned char)); + gnicindexes =3D g_variant_new_fixed_array(G_VARIANT_TYPE("i"), + nicindexes, nnicindexes, s= izeof(int)); + gprops =3D g_variant_new_parsed("[('Slice', <%s>)," + " ('After', <['libvirtd.service']>)," + " ('Before', <['virt-guest-shutdown.= target']>)]", + slicename); + message =3D g_variant_new("(s@ayssus@ai@a(sv))", + name, + guuid, + creatorname, + iscontainer ? "container" : "vm", + (unsigned int)pidleader, + NULLSTR_EMPTY(rootdir), + gnicindexes, + gprops); + + rc =3D virGDBusCallMethod(conn, + NULL, + error, + "org.freedesktop.machine1", + "/org/freedesktop/machine1", + "org.freedesktop.machine1.Manager", + "CreateMachineWithNetwork", + message); + + g_variant_unref(message); + + if (rc < 0) + return -1; + + if (error->level =3D=3D VIR_ERR_ERROR) { + if (virGDBusErrorIsUnknownMethod(error)) { VIR_INFO("CreateMachineWithNetwork isn't supported, switch= ing " "to legacy CreateMachine method for systemd-machi= ned"); - virResetError(&error); + virResetError(error); g_atomic_int_set(&hasCreateWithNetwork, 0); /* Could re-structure without Using goto, but this * avoids another atomic read which would trigger * another memory barrier */ goto fallback; } - virReportErrorObject(&error); - virResetError(&error); - goto cleanup; + virReportErrorObject(error); + virResetError(error); + return -1; } } else { fallback: - if (virDBusCallMethod(conn, - NULL, - NULL, - "org.freedesktop.machine1", - "/org/freedesktop/machine1", - "org.freedesktop.machine1.Manager", - "CreateMachine", - "sayssusa(sv)", - name, - 16, - uuid[0], uuid[1], uuid[2], uuid[3], - uuid[4], uuid[5], uuid[6], uuid[7], - uuid[8], uuid[9], uuid[10], uuid[11], - uuid[12], uuid[13], uuid[14], uuid[15], - creatorname, - iscontainer ? "container" : "vm", - (unsigned int)pidleader, - NULLSTR_EMPTY(rootdir), - 3, - "Slice", "s", slicename, - "After", "as", 1, "libvirtd.service", - "Before", "as", 1, "virt-guest-shutdown.targ= et") < 0) - goto cleanup; + guuid =3D g_variant_new_fixed_array(G_VARIANT_TYPE("y"), + uuid, 16, sizeof(unsigned char)); + gprops =3D g_variant_new_parsed("[('Slice', <%s>)," + " ('After', <['libvirtd.service']>)," + " ('Before', <['virt-guest-shutdown.= target']>)]", + slicename); + message =3D g_variant_new("(s@ayssus@a(sv))", + name, + guuid, + creatorname, + iscontainer ? "container" : "vm", + (unsigned int)pidleader, + NULLSTR_EMPTY(rootdir), + gprops); + + rc =3D virGDBusCallMethod(conn, + NULL, + NULL, + "org.freedesktop.machine1", + "/org/freedesktop/machine1", + "org.freedesktop.machine1.Manager", + "CreateMachine", + message); + + g_variant_unref(message); + + if (rc < 0) + return -1; } =20 if (maxthreads > 0) { if (!(scopename =3D virSystemdMakeScopeName(name, drivername, fals= e))) - goto cleanup; - - if (virDBusCallMethod(conn, - NULL, - NULL, - "org.freedesktop.systemd1", - "/org/freedesktop/systemd1", - "org.freedesktop.systemd1.Manager", - "SetUnitProperties", - "sba(sv)", - scopename, - true, - 1, - "TasksMax", "t", (uint64_t)maxthreads) < 0) - goto cleanup; + return -1; + + gprops =3D g_variant_new_parsed("[('TasksMax', <%llu>)]", + (uint64_t)maxthreads); + + message =3D g_variant_new("(sb@a(sv))", + scopename, + true, + gprops); + + rc =3D virGDBusCallMethod(conn, + NULL, + NULL, + "org.freedesktop.systemd1", + "/org/freedesktop/systemd1", + "org.freedesktop.systemd1.Manager", + "SetUnitProperties", + message); + + g_variant_unref(message); + + if (rc < 0) + return -1; } =20 - ret =3D 0; - - cleanup: - VIR_FREE(creatorname); - VIR_FREE(slicename); - VIR_FREE(scopename); - return ret; + return 0; } =20 int virSystemdTerminateMachine(const char *name) { - int ret; - DBusConnection *conn; - virError error; + int rc; + GDBusConnection *conn; + g_autoptr(GVariant) message =3D NULL; + g_autoptr(virError) error =3D NULL; =20 if (!name) return 0; =20 - memset(&error, 0, sizeof(error)); + if ((rc =3D virSystemdHasMachined()) < 0) + return rc; =20 - if ((ret =3D virSystemdHasMachined()) < 0) - goto cleanup; + if (!(conn =3D virGDBusGetSystemBus())) + return -1; =20 - ret =3D -1; - - if (!(conn =3D virDBusGetSystemBus())) - goto cleanup; + if (VIR_ALLOC(error) < 0) + return -1; =20 /* * The systemd DBus API we're invoking has the @@ -479,31 +503,27 @@ int virSystemdTerminateMachine(const char *name) * in 'ps' listing & similar */ =20 + message =3D g_variant_new("(s)", name); + VIR_DEBUG("Attempting to terminate machine via systemd"); - if (virDBusCallMethod(conn, - NULL, - &error, - "org.freedesktop.machine1", - "/org/freedesktop/machine1", - "org.freedesktop.machine1.Manager", - "TerminateMachine", - "s", - name) < 0) - goto cleanup; + if (virGDBusCallMethod(conn, + NULL, + error, + "org.freedesktop.machine1", + "/org/freedesktop/machine1", + "org.freedesktop.machine1.Manager", + "TerminateMachine", + message) < 0) + return -1; =20 - if (error.level =3D=3D VIR_ERR_ERROR && + if (error->level =3D=3D VIR_ERR_ERROR && STRNEQ_NULLABLE("org.freedesktop.machine1.NoSuchMachine", - error.str1)) { - virReportErrorObject(&error); - goto cleanup; + error->str1)) { + virReportErrorObject(error); + return -1; } =20 - ret =3D 0; - - cleanup: - virResetError(&error); - - return ret; + return 0; } =20 void @@ -559,41 +579,32 @@ virSystemdNotifyStartup(void) static int virSystemdPMSupportTarget(const char *methodName, bool *result) { - int ret; - DBusConnection *conn; - DBusMessage *message =3D NULL; + int rc; + GDBusConnection *conn; + g_autoptr(GVariant) reply =3D NULL; char *response; =20 - if ((ret =3D virSystemdHasLogind()) < 0) - return ret; + if ((rc =3D virSystemdHasLogind()) < 0) + return rc; =20 - if (!(conn =3D virDBusGetSystemBus())) + if (!(conn =3D virGDBusGetSystemBus())) return -1; =20 - ret =3D -1; - - if (virDBusCallMethod(conn, - &message, - NULL, - "org.freedesktop.login1", - "/org/freedesktop/login1", - "org.freedesktop.login1.Manager", - methodName, - NULL) < 0) - return ret; + if (virGDBusCallMethod(conn, + &reply, + NULL, + "org.freedesktop.login1", + "/org/freedesktop/login1", + "org.freedesktop.login1.Manager", + methodName, + NULL) < 0) + return -1; =20 - if ((ret =3D virDBusMessageDecode(message, "s", &response)) < 0) - goto cleanup; + g_variant_get(reply, "(&s)", &response); =20 *result =3D STREQ("yes", response) || STREQ("challenge", response); =20 - ret =3D 0; - - cleanup: - virDBusMessageUnref(message); - VIR_FREE(response); - - return ret; + return 0; } =20 int virSystemdCanSuspend(bool *result) diff --git a/tests/meson.build b/tests/meson.build index 2c4f044d30..356286839a 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -332,6 +332,7 @@ tests +=3D [ { 'name': 'virschematest' }, { 'name': 'virshtest' }, { 'name': 'virstringtest' }, + { 'name': 'virsystemdtest' }, { 'name': 'virtimetest' }, { 'name': 'virtypedparamtest' }, { 'name': 'viruritest' }, @@ -362,7 +363,6 @@ endif if conf.has('WITH_DBUS') tests +=3D [ { 'name': 'virdbustest', 'deps': [ dbus_dep ] }, - { 'name': 'virsystemdtest', 'deps': [ dbus_dep ] }, ] =20 endif diff --git a/tests/virsystemdtest.c b/tests/virsystemdtest.c index e7dcdea8e9..c1411d7c05 100644 --- a/tests/virsystemdtest.c +++ b/tests/virsystemdtest.c @@ -20,9 +20,8 @@ =20 #include "testutils.h" =20 -#if defined(WITH_DBUS) && defined(__linux__) +#if defined(__linux__) =20 -# include # include # include =20 @@ -30,7 +29,7 @@ # include "virsystemdpriv.h" =20 # include "virsystemd.h" -# include "virdbus.h" +# include "virgdbus.h" # include "virlog.h" # include "virmock.h" # include "rpc/virnetsocket.h" @@ -39,125 +38,77 @@ =20 VIR_LOG_INIT("tests.systemdtest"); =20 -VIR_MOCK_WRAP_RET_ARGS(dbus_connection_send_with_reply_and_block, - DBusMessage *, - DBusConnection *, connection, - DBusMessage *, message, - int, timeout_milliseconds, - DBusError *, error) +VIR_MOCK_WRAP_RET_ARGS(g_dbus_connection_call_sync, + GVariant *, + GDBusConnection *, connection, + const gchar *, bus_name, + const gchar *, object_path, + const gchar *, interface_name, + const gchar *, method_name, + GVariant *, parameters, + const GVariantType *, reply_type, + GDBusCallFlags, flags, + gint, timeout_msec, + GCancellable *, cancellable, + GError **, error) { - DBusMessage *reply =3D NULL; - const char *service =3D dbus_message_get_destination(message); - const char *member =3D dbus_message_get_member(message); + GVariant *reply =3D NULL; =20 - VIR_MOCK_REAL_INIT(dbus_connection_send_with_reply_and_block); + if (parameters) + g_variant_unref(parameters); =20 - if (STREQ(service, "org.freedesktop.machine1")) { + VIR_MOCK_REAL_INIT(g_dbus_connection_call_sync); + + if (STREQ(bus_name, "org.freedesktop.machine1")) { if (getenv("FAIL_BAD_SERVICE")) { - dbus_set_error_const(error, - "org.freedesktop.systemd.badthing", - "Something went wrong creating the machin= e"); + *error =3D g_dbus_error_new_for_dbus_error( + "org.freedesktop.systemd.badthing", + "Something went wrong creating the machine"); } else { - reply =3D dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_RETURN); - - if (STREQ(member, "GetMachineByPID")) { - const char *object_path =3D "/org/freedesktop/machine1/mac= hine/qemu_2ddemo"; - DBusMessageIter iter; - - dbus_message_iter_init_append(reply, &iter); - if (!dbus_message_iter_append_basic(&iter, - DBUS_TYPE_OBJECT_PATH, - &object_path)) - goto error; - } else if (STREQ(member, "Get")) { - const char *name =3D "qemu-demo"; - DBusMessageIter iter; - DBusMessageIter sub; - - dbus_message_iter_init_append(reply, &iter); - dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT, - "s", &sub); - - if (!dbus_message_iter_append_basic(&sub, - DBUS_TYPE_STRING, - &name)) - goto error; - dbus_message_iter_close_container(&iter, &sub); + if (STREQ(method_name, "GetMachineByPID")) { + reply =3D g_variant_new("(o)", + "/org/freedesktop/machine1/machine/q= emu_2ddemo"); + } else if (STREQ(method_name, "Get")) { + reply =3D g_variant_new("(v)", g_variant_new_string("qemu-= demo")); + } else { + reply =3D g_variant_new("()"); } } - } else if (STREQ(service, "org.freedesktop.login1")) { - char *supported =3D getenv("RESULT_SUPPORT"); - DBusMessageIter iter; - reply =3D dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_RETURN); - dbus_message_iter_init_append(reply, &iter); + } else if (STREQ(bus_name, "org.freedesktop.login1")) { + reply =3D g_variant_new("(s)", getenv("RESULT_SUPPORT")); + } else if (STREQ(bus_name, "org.freedesktop.DBus") && + STREQ(method_name, "ListActivatableNames")) { + GVariantBuilder builder; =20 - if (!dbus_message_iter_append_basic(&iter, - DBUS_TYPE_STRING, - &supported)) - goto error; - } else if (STREQ(service, "org.freedesktop.DBus") && - STREQ(member, "ListActivatableNames")) { - const char *svc1 =3D "org.foo.bar.wizz"; - const char *svc2 =3D "org.freedesktop.machine1"; - const char *svc3 =3D "org.freedesktop.login1"; - DBusMessageIter iter; - DBusMessageIter sub; - reply =3D dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_RETURN); - dbus_message_iter_init_append(reply, &iter); - dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, - "s", &sub); + g_variant_builder_init(&builder, G_VARIANT_TYPE("as")); =20 - if (!dbus_message_iter_append_basic(&sub, - DBUS_TYPE_STRING, - &svc1)) - goto error; - if (!getenv("FAIL_NO_SERVICE") && - !dbus_message_iter_append_basic(&sub, - DBUS_TYPE_STRING, - &svc2)) - goto error; - if (!getenv("FAIL_NO_SERVICE") && - !dbus_message_iter_append_basic(&sub, - DBUS_TYPE_STRING, - &svc3)) - goto error; - dbus_message_iter_close_container(&iter, &sub); - } else if (STREQ(service, "org.freedesktop.DBus") && - STREQ(member, "ListNames")) { - const char *svc1 =3D "org.foo.bar.wizz"; - const char *svc2 =3D "org.freedesktop.systemd1"; - const char *svc3 =3D "org.freedesktop.login1"; - DBusMessageIter iter; - DBusMessageIter sub; - reply =3D dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_RETURN); - dbus_message_iter_init_append(reply, &iter); - dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, - "s", &sub); + g_variant_builder_add(&builder, "s", "org.foo.bar.wizz"); =20 - if (!dbus_message_iter_append_basic(&sub, - DBUS_TYPE_STRING, - &svc1)) - goto error; - if ((!getenv("FAIL_NO_SERVICE") && !getenv("FAIL_NOT_REGISTERED"))= && - !dbus_message_iter_append_basic(&sub, - DBUS_TYPE_STRING, - &svc2)) - goto error; - if ((!getenv("FAIL_NO_SERVICE") && !getenv("FAIL_NOT_REGISTERED"))= && - !dbus_message_iter_append_basic(&sub, - DBUS_TYPE_STRING, - &svc3)) - goto error; - dbus_message_iter_close_container(&iter, &sub); + if (!getenv("FAIL_NO_SERVICE")) { + g_variant_builder_add(&builder, "s", "org.freedesktop.machine1= "); + g_variant_builder_add(&builder, "s", "org.freedesktop.login1"); + } + + reply =3D g_variant_new("(@as)", g_variant_builder_end(&builder)); + } else if (STREQ(bus_name, "org.freedesktop.DBus") && + STREQ(method_name, "ListNames")) { + GVariantBuilder builder; + + g_variant_builder_init(&builder, G_VARIANT_TYPE("as")); + + g_variant_builder_add(&builder, "s", "org.foo.bar.wizz"); + + if (!getenv("FAIL_NO_SERVICE") && !getenv("FAIL_NOT_REGISTERED")) { + g_variant_builder_add(&builder, "s", "org.freedesktop.systemd1= "); + g_variant_builder_add(&builder, "s", "org.freedesktop.login1"); + } + + reply =3D g_variant_new("(@as)", g_variant_builder_end(&builder)); } else { - reply =3D dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_RETURN); + reply =3D g_variant_new("()"); } =20 return reply; - - error: - virDBusMessageUnref(reply); - return NULL; } =20 =20 @@ -794,12 +745,12 @@ mymain(void) return ret =3D=3D 0 ? EXIT_SUCCESS : EXIT_FAILURE; } =20 -VIR_TEST_MAIN_PRELOAD(mymain, VIR_TEST_MOCK("virdbus")) +VIR_TEST_MAIN_PRELOAD(mymain, VIR_TEST_MOCK("virgdbus")) =20 -#else /* ! (WITH_DBUS && __linux__) */ +#else /* ! __linux__ */ int main(void) { return EXIT_AM_SKIP; } -#endif /* ! WITH_DBUS */ +#endif /* ! __linux__ */ --=20 2.26.2 From nobody Sat May 4 20:55:56 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 63.128.21.124 as permitted sender) client-ip=63.128.21.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 63.128.21.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1600331526; cv=none; d=zohomail.com; s=zohoarc; b=l2dtrA8eqIpGAqAfDv5YqHYKMvuv51yNeDZ+4BRYFBCIJggedXCJOCwDcjOzQvWELF80bajhTxXe0Tm8L+JAdjtNPrpvm4as1pibmZBxGjtsNNmNPYqeFOfTez5usvJt+QN3tYjGdMKBJjjExGkKghHmzd7YtYZB76AaCqrGG/0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1600331526; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=gisbda94fcW86Fz2QwiJ+2eFBsOx3sK0EKoyy77Kn1o=; b=KbWCuAotHj7W+46xZL8bT6MhwLY4w0HXBreoVBdOsDkD1qS+6hPwaNGVHOuncPOanCc+n8OrC/fWNgFtypQe5iHQR9+ER6IZpOy1XuSlI4amd9sItt8gKZ9uhMoKDDDV/xAIoyqVWsCKZOMykNHzduuDkrrhl2pTiEU6rcqniv8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 63.128.21.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [63.128.21.124]) by mx.zohomail.com with SMTPS id 1600331526624493.4374881137651; Thu, 17 Sep 2020 01:32:06 -0700 (PDT) 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-103-6PB5PewxOQi88TMVIo7lyg-1; Thu, 17 Sep 2020 04:31:44 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 96C061084D7C; Thu, 17 Sep 2020 08:31:38 +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 7368E60BE5; Thu, 17 Sep 2020 08:31:38 +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 3C4668C7D1; Thu, 17 Sep 2020 08:31:38 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 08H8UEnr004168 for ; Thu, 17 Sep 2020 04:30:14 -0400 Received: by smtp.corp.redhat.com (Postfix) id C1CAF5DA30; Thu, 17 Sep 2020 08:30:14 +0000 (UTC) Received: from antique-work.redhat.com (unknown [10.40.195.159]) by smtp.corp.redhat.com (Postfix) with ESMTP id 3D5B45DECD for ; Thu, 17 Sep 2020 08:30:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1600331525; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=gisbda94fcW86Fz2QwiJ+2eFBsOx3sK0EKoyy77Kn1o=; b=DWstjLzTxKx6yXW8HUpJEloA0MeOwkMhXcfr3eRiT9gp2LThbfk3+r9nGoFztsTStSdkW5 oLRbuwJqyK4MxBmpxKxgYepGJHec1hd8/aqB8LCE8I/6AqMduXw88pSkxbziZW0Kwfg9Sv m9517xgjanwvkll3JSlA7Am1Dvg3RaM= X-MC-Unique: 6PB5PewxOQi88TMVIo7lyg-1 From: Pavel Hrdina To: libvir-list@redhat.com Subject: [libvirt PATCH 09/14] src/lxc/lxc_controller: convert to use GLib DBus Date: Thu, 17 Sep 2020 10:29:44 +0200 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com 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: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" Signed-off-by: Pavel Hrdina Reviewed-by: Michal Privoznik Reviewed-by: Neal Gompa --- src/lxc/lxc_controller.c | 6 +++--- src/lxc/meson.build | 1 - 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/lxc/lxc_controller.c b/src/lxc/lxc_controller.c index 37a28ac2f3..c3cf485e2c 100644 --- a/src/lxc/lxc_controller.c +++ b/src/lxc/lxc_controller.c @@ -54,13 +54,13 @@ #include "virnetdevveth.h" #include "viralloc.h" #include "virfile.h" +#include "virgdbus.h" #include "virpidfile.h" #include "vircommand.h" #include "virhostcpu.h" #include "virrandom.h" #include "virprocess.h" #include "virnuma.h" -#include "virdbus.h" #include "rpc/virnetdaemon.h" #include "virstring.h" #include "virgettext.h" @@ -2430,7 +2430,7 @@ virLXCControllerRun(virLXCControllerPtr ctrl) * of LXC instance, since dbus-daemon is limited to * only a few 100 connections by default */ - virDBusCloseSystemBus(); + virGDBusCloseSystemBus(); =20 rc =3D virLXCControllerMain(ctrl); =20 @@ -2612,7 +2612,7 @@ int main(int argc, char *argv[]) =20 virEventRegisterDefaultImpl(); =20 - virDBusSetSharedBus(false); + virGDBusSetSharedBus(false); =20 if (!(ctrl =3D virLXCControllerNew(name))) goto cleanup; diff --git a/src/lxc/meson.build b/src/lxc/meson.build index 2810da7604..f8e2a8852a 100644 --- a/src/lxc/meson.build +++ b/src/lxc/meson.build @@ -139,7 +139,6 @@ if conf.has('WITH_LXC') 'deps': [ blkid_dep, capng_dep, - dbus_dep, fuse_dep, libnl_dep, rpc_dep, --=20 2.26.2 From nobody Sat May 4 20:55:56 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 63.128.21.124 as permitted sender) client-ip=63.128.21.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 63.128.21.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1600331532; cv=none; d=zohomail.com; s=zohoarc; b=LHmMddq8gGhP9cF1tjoxMgnAjwZnVwVpRDkxZU6nN5D9s3W9k8uKL+C+xqOQs2WVKIIVB2prbUx97w8NkhAa+sqPsmLXUlYoZmA4CIC8u2najauO/UKpFYLYqw1DCaCvFIW9uBZGVuP64OIrPJpjiPYBf/7L5LG0+j0AHyjqOMg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1600331532; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=IiHCa0MMrnZALvqG6E3eI6GA+e2vtyAKf07D5GDZYAI=; b=mP7mXNT/VCMmcuyUCoIT0JYTCHh7TgnoGz1Ujau25Ma6zBPePXQSMpiXWaE7nCNIhOeqHNLjTghVXIVUSRuYMHbAmyywzu4pIdwdzAWGdDN5wcwleDoPY/dFIbVxlBQdq/jj+aJvnX8IC40B1SJIrWOmRfrwLoHeUQNxHFIaWrc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 63.128.21.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [63.128.21.124]) by mx.zohomail.com with SMTPS id 1600331532940335.3767426134966; Thu, 17 Sep 2020 01:32:12 -0700 (PDT) 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-196-aMlNI7qKOKaoanXUUzQCDg-1; Thu, 17 Sep 2020 04:31:46 -0400 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 mimecast-mx01.redhat.com (Postfix) with ESMTPS id 4EBCB188C12F; Thu, 17 Sep 2020 08:31:41 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 297797514F; Thu, 17 Sep 2020 08:31:41 +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 E05BB183D049; Thu, 17 Sep 2020 08:31:40 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 08H8UFZq004177 for ; Thu, 17 Sep 2020 04:30:15 -0400 Received: by smtp.corp.redhat.com (Postfix) id A59A35DEBF; Thu, 17 Sep 2020 08:30:15 +0000 (UTC) Received: from antique-work.redhat.com (unknown [10.40.195.159]) by smtp.corp.redhat.com (Postfix) with ESMTP id 22A805DA30 for ; Thu, 17 Sep 2020 08:30:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1600331531; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=IiHCa0MMrnZALvqG6E3eI6GA+e2vtyAKf07D5GDZYAI=; b=ht7MXza7302WRE9Kano8D2C7+70e3u3p0SVyaKQSYmu5YU+6Ski7oXE69czUIRa4RI6x53 VMd/+FqSmQ6BSLEBQNfc5DYSwSKd53kGuJx8nC4MbMQADhNj1vAvLAEbEWzMhzKDn29pWn Jw8/9al5z2lOY6UEEYZSZCaJ4pZVwOY= X-MC-Unique: aMlNI7qKOKaoanXUUzQCDg-1 From: Pavel Hrdina To: libvir-list@redhat.com Subject: [libvirt PATCH 10/14] src/network/bridge_driver: convert to use GLib DBus Date: Thu, 17 Sep 2020 10:29:45 +0200 Message-Id: <05f1bba152440a26bb8ca96b27e578623408b4ea.1600331276.git.phrdina@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com 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: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0.001 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" Signed-off-by: Pavel Hrdina Reviewed-by: Michal Privoznik Reviewed-by: Neal Gompa --- src/network/bridge_driver.c | 81 ++++++++++++++++++------------------- src/network/meson.build | 2 - 2 files changed, 39 insertions(+), 44 deletions(-) diff --git a/src/network/bridge_driver.c b/src/network/bridge_driver.c index 87d7acab06..5d9b9eaa4f 100644 --- a/src/network/bridge_driver.c +++ b/src/network/bridge_driver.c @@ -59,7 +59,7 @@ #include "virnetdevtap.h" #include "virnetdevvportprofile.h" #include "virpci.h" -#include "virdbus.h" +#include "virgdbus.h" #include "virfile.h" #include "virstring.h" #include "viraccessapicheck.h" @@ -638,33 +638,29 @@ networkAutostartConfig(virNetworkObjPtr obj, =20 =20 #ifdef WITH_FIREWALLD -static DBusHandlerResult -firewalld_dbus_filter_bridge(DBusConnection *connection G_GNUC_UNUSED, - DBusMessage *message, - void *user_data) +static void +firewalld_dbus_signal_callback(GDBusConnection *connection G_GNUC_UNUSED, + const char *senderName G_GNUC_UNUSED, + const char *objectPath G_GNUC_UNUSED, + const char *interfaceName, + const char *signalName, + GVariant *parameters, + gpointer user_data) { virNetworkDriverStatePtr driver =3D user_data; bool reload =3D false; =20 - if (dbus_message_is_signal(message, - "org.fedoraproject.FirewallD1", "Reloaded")= ) { + if (STREQ(interfaceName, "org.fedoraproject.FirewallD1") && + STREQ(signalName, "Reloaded")) { reload =3D true; + } else if (STREQ(interfaceName, "org.freedesktop.DBus") && + STREQ(signalName, "NameOwnerChanged")) { + char *name =3D NULL; + char *old_owner =3D NULL; + char *new_owner =3D NULL; =20 - } else if (dbus_message_is_signal(message, - DBUS_INTERFACE_DBUS, "NameOwnerChang= ed")) { + g_variant_get(parameters, "(&s&s&s)", &name, &old_owner, &new_owne= r); =20 - g_autofree char *name =3D NULL; - g_autofree char *old_owner =3D NULL; - g_autofree char *new_owner =3D NULL; - - if (virDBusMessageDecode(message, "sss", &name, &old_owner, &new_o= wner) < 0) { - VIR_WARN("Failed to decode DBus NameOwnerChanged message"); - return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; - } - /* - * if new_owner is empty, firewalld is shutting down. If it is - * non-empty, then it is starting - */ if (new_owner && *new_owner) reload =3D true; } @@ -673,8 +669,6 @@ firewalld_dbus_filter_bridge(DBusConnection *connection= G_GNUC_UNUSED, VIR_DEBUG("Reload in bridge_driver because of firewalld."); networkReloadFirewallRules(driver, false, true); } - - return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; } #endif =20 @@ -694,7 +688,7 @@ networkStateInitialize(bool privileged, g_autofree char *rundir =3D NULL; bool autostart =3D true; #ifdef WITH_FIREWALLD - DBusConnection *sysbus =3D NULL; + GDBusConnection *sysbus =3D NULL; #endif =20 if (root !=3D NULL) { @@ -793,27 +787,30 @@ networkStateInitialize(bool privileged, network_driver->networkEventState =3D virObjectEventStateNew(); =20 #ifdef WITH_FIREWALLD - if (!(sysbus =3D virDBusGetSystemBus())) { + if (!(sysbus =3D virGDBusGetSystemBus())) { VIR_WARN("DBus not available, disabling firewalld support " "in bridge_network_driver: %s", virGetLastErrorMessage()); } else { - /* add matches for - * NameOwnerChanged on org.freedesktop.DBus for firewalld start/st= op - * Reloaded on org.fedoraproject.FirewallD1 for firewalld reload - */ - dbus_bus_add_match(sysbus, - "type=3D'signal'" - ",interface=3D'"DBUS_INTERFACE_DBUS"'" - ",member=3D'NameOwnerChanged'" - ",arg0=3D'org.fedoraproject.FirewallD1'", - NULL); - dbus_bus_add_match(sysbus, - "type=3D'signal'" - ",interface=3D'org.fedoraproject.FirewallD1'" - ",member=3D'Reloaded'", - NULL); - dbus_connection_add_filter(sysbus, firewalld_dbus_filter_bridge, - network_driver, NULL); + g_dbus_connection_signal_subscribe(sysbus, + NULL, + "org.freedesktop.DBus", + "NameOwnerChanged", + NULL, + "org.fedoraproject.FirewallD1", + G_DBUS_SIGNAL_FLAGS_NONE, + firewalld_dbus_signal_callback, + network_driver, + NULL); + g_dbus_connection_signal_subscribe(sysbus, + NULL, + "org.fedoraproject.FirewallD1", + "Reloaded", + NULL, + NULL, + G_DBUS_SIGNAL_FLAGS_NONE, + firewalld_dbus_signal_callback, + network_driver, + NULL); } #endif =20 diff --git a/src/network/meson.build b/src/network/meson.build index ae2848697a..13dd2c26b2 100644 --- a/src/network/meson.build +++ b/src/network/meson.build @@ -18,7 +18,6 @@ if conf.has('WITH_NETWORK') ], dependencies: [ access_dep, - dbus_dep, libnl_dep, src_dep, ], @@ -30,7 +29,6 @@ if conf.has('WITH_NETWORK') virt_modules +=3D { 'name': 'virt_driver_network', 'deps': [ - dbus_dep, libnl_dep, ], 'link_whole': [ --=20 2.26.2 From nobody Sat May 4 20:55:56 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) client-ip=216.205.24.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1600331514; cv=none; d=zohomail.com; s=zohoarc; b=O84aY3pbN0N7azhdM48+EvYCSb7MuokRs09wylYkKII8vbz6pRCZaXr4rlXzNFLrOddYnM4mn4utn/ikMCRs/HsAfrMCr0D/YUMdj5+1J/lYbYjRR0sepJnDCL9jc27tDUNVfH16Dxyj7Mx2zgjQ1AtDO3InkSy/L1ZTmQZlffY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1600331514; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=UTD/M+ZVxXik2tJVzF9F9lKe2C8AZFQOFa2jJnRVaFo=; b=DGU+p2ogF5HmCTL/jOk+V0f1RC6Uk56jEX7iBIluIChHgAT50z2NDRi1Mz3EfPR/e6BwsxL9A/Pu+1Q7M8wIDXBzSDtseoNjKyG3RS7EIRfO7Hr0IH4o0m577CK+w0Rzli/gC1i6EulZXNfLSYsrQIi0ZCs3v5jxzLmA7rkF7X4= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by mx.zohomail.com with SMTPS id 1600331514546240.53360205181139; Thu, 17 Sep 2020 01:31:54 -0700 (PDT) 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-27-gSSEy8RUNa-PyysC89JKVQ-1; Thu, 17 Sep 2020 04:31:50 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id EAC15188C125; Thu, 17 Sep 2020 08:31:43 +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 CA5CB7EEB4; Thu, 17 Sep 2020 08:31:43 +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 9176A8C7DA; Thu, 17 Sep 2020 08:31:43 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 08H8UJqp004192 for ; Thu, 17 Sep 2020 04:30:19 -0400 Received: by smtp.corp.redhat.com (Postfix) id 7FE745DA30; Thu, 17 Sep 2020 08:30:19 +0000 (UTC) Received: from antique-work.redhat.com (unknown [10.40.195.159]) by smtp.corp.redhat.com (Postfix) with ESMTP id F0CD75DEC4 for ; Thu, 17 Sep 2020 08:30:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1600331513; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=UTD/M+ZVxXik2tJVzF9F9lKe2C8AZFQOFa2jJnRVaFo=; b=fmabqsGaCP6monTC1sukMWdL1DupaTjld6puU6Qm7DIjwxmYBnXBA8K/LFrsnepFBWZSF+ hbp8K6ZOVkRz5OVeD1HSEzYTKyS2xIwPP5LAmigXRxRxpsDchalfJ6i8hYSADI7Bet7DRk oxuxWMMNwFA5AGv3qpQONHaADq50xeY= X-MC-Unique: gSSEy8RUNa-PyysC89JKVQ-1 From: Pavel Hrdina To: libvir-list@redhat.com Subject: [libvirt PATCH 11/14] src/nwfilter/nwfilter_driver: convert to use GLib DBus Date: Thu, 17 Sep 2020 10:29:46 +0200 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com 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: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" Signed-off-by: Pavel Hrdina Reviewed-by: Michal Privoznik Reviewed-by: Neal Gompa --- src/nwfilter/meson.build | 1 - src/nwfilter/nwfilter_driver.c | 127 ++++++++++++--------------------- 2 files changed, 46 insertions(+), 82 deletions(-) diff --git a/src/nwfilter/meson.build b/src/nwfilter/meson.build index dcdc30f373..715282ee77 100644 --- a/src/nwfilter/meson.build +++ b/src/nwfilter/meson.build @@ -17,7 +17,6 @@ if conf.has('WITH_NWFILTER') ], dependencies: [ access_dep, - dbus_dep, libnl_dep, libpcap_dep, src_dep, diff --git a/src/nwfilter/nwfilter_driver.c b/src/nwfilter/nwfilter_driver.c index 77c4467816..1b8e3dbad3 100644 --- a/src/nwfilter/nwfilter_driver.c +++ b/src/nwfilter/nwfilter_driver.c @@ -24,7 +24,7 @@ =20 #include =20 -#include "virdbus.h" +#include "virgdbus.h" #include "virlog.h" =20 #include "internal.h" @@ -50,17 +50,6 @@ =20 VIR_LOG_INIT("nwfilter.nwfilter_driver"); =20 -#define DBUS_RULE_FWD_NAMEOWNERCHANGED \ - "type=3D'signal'" \ - ",interface=3D'"DBUS_INTERFACE_DBUS"'" \ - ",member=3D'NameOwnerChanged'" \ - ",arg0=3D'org.fedoraproject.FirewallD1'" - -#define DBUS_RULE_FWD_RELOADED \ - "type=3D'signal'" \ - ",interface=3D'org.fedoraproject.FirewallD1'" \ - ",member=3D'Reloaded'" - =20 static virNWFilterDriverStatePtr driver; =20 @@ -79,36 +68,30 @@ static void nwfilterDriverUnlock(void) =20 #ifdef WITH_FIREWALLD =20 -static DBusHandlerResult -nwfilterFirewalldDBusFilter(DBusConnection *connection G_GNUC_UNUSED, - DBusMessage *message, - void *user_data G_GNUC_UNUSED) +static void +nwfilterFirewalldDBusSignalCallback(GDBusConnection *connection G_GNUC_UNU= SED, + const char *senderName G_GNUC_UNUSED, + const char *objectPath G_GNUC_UNUSED, + const char *interfaceName G_GNUC_UNUSE= D, + const char *signalName G_GNUC_UNUSED, + GVariant *parameters G_GNUC_UNUSED, + gpointer user_data G_GNUC_UNUSED) { - if (dbus_message_is_signal(message, DBUS_INTERFACE_DBUS, - "NameOwnerChanged") || - dbus_message_is_signal(message, "org.fedoraproject.FirewallD1", - "Reloaded")) { - VIR_DEBUG("Reload in nwfilter_driver because of firewalld."); - nwfilterStateReload(); - } - - return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; + nwfilterStateReload(); } =20 +static unsigned int restartID; +static unsigned int reloadID; + static void nwfilterDriverRemoveDBusMatches(void) { - DBusConnection *sysbus; + GDBusConnection *sysbus; =20 - sysbus =3D virDBusGetSystemBus(); + sysbus =3D virGDBusGetSystemBus(); if (sysbus) { - dbus_bus_remove_match(sysbus, - DBUS_RULE_FWD_NAMEOWNERCHANGED, - NULL); - dbus_bus_remove_match(sysbus, - DBUS_RULE_FWD_RELOADED, - NULL); - dbus_connection_remove_filter(sysbus, nwfilterFirewalldDBusFilter,= NULL); + g_dbus_connection_signal_unsubscribe(sysbus, restartID); + g_dbus_connection_signal_unsubscribe(sysbus, reloadID); } } =20 @@ -117,33 +100,29 @@ nwfilterDriverRemoveDBusMatches(void) * * Startup DBus matches for monitoring the state of firewalld */ -static int -nwfilterDriverInstallDBusMatches(DBusConnection *sysbus) +static void +nwfilterDriverInstallDBusMatches(GDBusConnection *sysbus) { - int ret =3D 0; - - if (!sysbus) { - ret =3D -1; - } else { - /* add matches for - * NameOwnerChanged on org.freedesktop.DBus for firewalld start/st= op - * Reloaded on org.fedoraproject.FirewallD1 for firewalld reload - */ - dbus_bus_add_match(sysbus, - DBUS_RULE_FWD_NAMEOWNERCHANGED, - NULL); - dbus_bus_add_match(sysbus, - DBUS_RULE_FWD_RELOADED, - NULL); - if (!dbus_connection_add_filter(sysbus, nwfilterFirewalldDBusFilte= r, - NULL, NULL)) { - VIR_WARN(("Adding a filter to the DBus connection failed")); - nwfilterDriverRemoveDBusMatches(); - ret =3D -1; - } - } - - return ret; + restartID =3D g_dbus_connection_signal_subscribe(sysbus, + NULL, + "org.freedesktop.DBus", + "NameOwnerChanged", + NULL, + "org.fedoraproject.Fire= wallD1", + G_DBUS_SIGNAL_FLAGS_NON= E, + nwfilterFirewalldDBusSi= gnalCallback, + NULL, + NULL); + reloadID =3D g_dbus_connection_signal_subscribe(sysbus, + NULL, + "org.fedoraproject.Firew= allD1", + "Reloaded", + NULL, + NULL, + G_DBUS_SIGNAL_FLAGS_NONE, + nwfilterFirewalldDBusSig= nalCallback, + NULL, + NULL); } =20 #else /* WITH_FIREWALLD */ @@ -153,10 +132,9 @@ nwfilterDriverRemoveDBusMatches(void) { } =20 -static int -nwfilterDriverInstallDBusMatches(DBusConnection *sysbus G_GNUC_UNUSED) +static void +nwfilterDriverInstallDBusMatches(GDBusConnection *sysbus G_GNUC_UNUSED) { - return 0; } =20 #endif /* WITH_FIREWALLD */ @@ -181,7 +159,7 @@ nwfilterStateInitialize(bool privileged, virStateInhibitCallback callback G_GNUC_UNUSED, void *opaque G_GNUC_UNUSED) { - DBusConnection *sysbus =3D NULL; + GDBusConnection *sysbus =3D NULL; =20 if (root !=3D NULL) { virReportError(VIR_ERR_INVALID_ARG, "%s", @@ -189,8 +167,8 @@ nwfilterStateInitialize(bool privileged, return -1; } =20 - if (virDBusHasSystemBus() && - !(sysbus =3D virDBusGetSystemBus())) + if (virGDBusHasSystemBus() && + !(sysbus =3D virGDBusGetSystemBus())) return VIR_DRV_STATE_INIT_ERROR; =20 driver =3D g_new0(virNWFilterDriverState, 1); @@ -241,21 +219,8 @@ nwfilterStateInitialize(bool privileged, * startup the DBus late so we don't get a reload signal while * initializing */ - if (sysbus && - nwfilterDriverInstallDBusMatches(sysbus) < 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("DBus matches could not be installed. " - "Disabling nwfilter driver")); - /* - * unfortunately this is fatal since virNWFilterTechDriversInit - * may have caused the ebiptables driver to use the firewall tool - * but now that the watches don't work, we just disable the nwfilt= er - * driver - * - * This may only happen if the system bus is available. - */ - goto error; - } + if (sysbus) + nwfilterDriverInstallDBusMatches(sysbus); =20 driver->configDir =3D g_strdup(SYSCONFDIR "/libvirt/nwfilter"); =20 --=20 2.26.2 From nobody Sat May 4 20:55:56 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 207.211.31.81 as permitted sender) client-ip=207.211.31.81; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-1.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 207.211.31.81 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1600331570; cv=none; d=zohomail.com; s=zohoarc; b=lafJeSmLtu4LeU0xt9jvXi4Ep1Of0ztqqOrBpv4UAcH4UDyb4ungbXvy1UweeaoRPKMwDTXk4wo9yHaJJLVTUH4IVNPgxkgxhxZq9cQ01/MNLajqoll0t6d+Y5PY/mfKAAslMYmyPoRkxM+Mh/4GWdaESk/o9HBXvCyRGJEL508= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1600331570; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=s+xssJp2nrCctxI9IHeGNsPiERVHcB+C4U9Ip5h9lZY=; b=PrOkiGI7JpKx18CAFBvMRd957Jb8LER8sN8008WknQGo3Haa78XtrAvWFrobnsQ81KR65lg1svN/Mwey7i0ppiKSGcLiEY0DvP28AEQwZD5KONk/6RSRGXtPxR5wIU567i/5ZPvKpvcpVbf+jwoX/ETcAB5V9DTAK5pltU1MI+Q= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 207.211.31.81 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-1.mimecast.com (us-smtp-2.mimecast.com [207.211.31.81]) by mx.zohomail.com with SMTPS id 160033157071185.22378271785783; Thu, 17 Sep 2020 01:32:50 -0700 (PDT) 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-419-Lu4zZ9DAOm-5PTVM-NvJ3g-1; Thu, 17 Sep 2020 04:32:47 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 1037281A3FF; Thu, 17 Sep 2020 08:32:40 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id E06505DEC2; Thu, 17 Sep 2020 08:32:39 +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 AC17E183D046; Thu, 17 Sep 2020 08:32:39 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 08H8UKNM004199 for ; Thu, 17 Sep 2020 04:30:20 -0400 Received: by smtp.corp.redhat.com (Postfix) id 636C05DA30; Thu, 17 Sep 2020 08:30:20 +0000 (UTC) Received: from antique-work.redhat.com (unknown [10.40.195.159]) by smtp.corp.redhat.com (Postfix) with ESMTP id D43785DEC4 for ; Thu, 17 Sep 2020 08:30:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1600331569; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=s+xssJp2nrCctxI9IHeGNsPiERVHcB+C4U9Ip5h9lZY=; b=I1N/akcwOuuLk1y3+3LSLLEbTMnMYzyyZLSnrYv44yTnHdHganb01h76jjOxIKFAKhrJMU kdN4Tp4MQTUEkOuALO2LLLsAVDF9Ddkki7MUd7BphE8CJXE6cbRVBA+YwUe1Cn8Kzig4Vq TMWrMTX61ZvASaNz3321n//+pomkslo= X-MC-Unique: Lu4zZ9DAOm-5PTVM-NvJ3g-1 From: Pavel Hrdina To: libvir-list@redhat.com Subject: [libvirt PATCH 12/14] src/remote/remote_daemon: convert to use GLib DBus Date: Thu, 17 Sep 2020 10:29:47 +0200 Message-Id: <9d93e9e5871b79af868dfecb3a217c59a5a0ffb0.1600331276.git.phrdina@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com 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: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0.001 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" Signed-off-by: Pavel Hrdina Reviewed-by: Michal Privoznik Reviewed-by: Neal Gompa --- src/remote/remote_daemon.c | 73 +++++++++++++++++++------------------- 1 file changed, 37 insertions(+), 36 deletions(-) diff --git a/src/remote/remote_daemon.c b/src/remote/remote_daemon.c index 2ac4f6cd2e..9eb2c2bc0d 100644 --- a/src/remote/remote_daemon.c +++ b/src/remote/remote_daemon.c @@ -63,7 +63,7 @@ =20 #include "configmake.h" =20 -#include "virdbus.h" +#include "virgdbus.h" =20 VIR_LOG_INIT("daemon." DAEMON_NAME); =20 @@ -508,9 +508,8 @@ static void daemonInhibitCallback(bool inhibit, void *o= paque) } =20 =20 -#ifdef WITH_DBUS -static DBusConnection *sessionBus; -static DBusConnection *systemBus; +static GDBusConnection *sessionBus; +static GDBusConnection *systemBus; =20 static void daemonStopWorker(void *opaque) { @@ -538,41 +537,40 @@ static void daemonStop(virNetDaemonPtr dmn) } =20 =20 -static DBusHandlerResult -handleSessionMessageFunc(DBusConnection *connection G_GNUC_UNUSED, - DBusMessage *message, - void *opaque) +static GDBusMessage * +handleSessionMessageFunc(GDBusConnection *connection G_GNUC_UNUSED, + GDBusMessage *message, + gboolean incoming G_GNUC_UNUSED, + gpointer opaque) { virNetDaemonPtr dmn =3D opaque; =20 VIR_DEBUG("dmn=3D%p", dmn); =20 - if (dbus_message_is_signal(message, - DBUS_INTERFACE_LOCAL, - "Disconnected")) + if (virGDBusMessageIsSignal(message, + "org.freedesktop.DBus.Local", + "Disconnected")) daemonStop(dmn); =20 - return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; + return message; } =20 =20 -static DBusHandlerResult -handleSystemMessageFunc(DBusConnection *connection G_GNUC_UNUSED, - DBusMessage *message, - void *opaque) +static void +handleSystemMessageFunc(GDBusConnection *connection G_GNUC_UNUSED, + const char *senderName G_GNUC_UNUSED, + const char *objectPath G_GNUC_UNUSED, + const char *interfaceName G_GNUC_UNUSED, + const char *signalName G_GNUC_UNUSED, + GVariant *parameters G_GNUC_UNUSED, + gpointer opaque) { virNetDaemonPtr dmn =3D opaque; =20 VIR_DEBUG("dmn=3D%p", dmn); =20 - if (dbus_message_is_signal(message, - "org.freedesktop.login1.Manager", - "PrepareForShutdown")) - daemonStop(dmn); - - return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; + daemonStop(dmn); } -#endif =20 =20 static void daemonRunStateInit(void *opaque) @@ -608,25 +606,28 @@ static void daemonRunStateInit(void *opaque) =20 driversInitialized =3D true; =20 -#ifdef WITH_DBUS /* Tie the non-privileged daemons to the session/shutdown lifecycle */ if (!virNetDaemonIsPrivileged(dmn)) { =20 - sessionBus =3D virDBusGetSessionBus(); + sessionBus =3D virGDBusGetSessionBus(); if (sessionBus !=3D NULL) - dbus_connection_add_filter(sessionBus, - handleSessionMessageFunc, dmn, NULL= ); + g_dbus_connection_add_filter(sessionBus, + handleSessionMessageFunc, dmn, NU= LL); =20 - systemBus =3D virDBusGetSystemBus(); - if (systemBus !=3D NULL) { - dbus_connection_add_filter(systemBus, - handleSystemMessageFunc, dmn, NULL); - dbus_bus_add_match(systemBus, - "type=3D'signal',sender=3D'org.freedesktop.= login1', interface=3D'org.freedesktop.login1.Manager'", - NULL); - } + systemBus =3D virGDBusGetSystemBus(); + if (systemBus !=3D NULL) + g_dbus_connection_signal_subscribe(systemBus, + "org.freedesktop.login1", + "org.freedesktop.login1.Man= ager", + "PrepareForShutdown", + NULL, + NULL, + G_DBUS_SIGNAL_FLAGS_NONE, + handleSystemMessageFunc, + dmn, + NULL); } -#endif + /* Only now accept clients from network */ virNetDaemonUpdateServices(dmn, true); cleanup: --=20 2.26.2 From nobody Sat May 4 20:55:56 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) client-ip=216.205.24.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1600331651; cv=none; d=zohomail.com; s=zohoarc; b=B1A+YOVTl+78SKvu4Ct7jhjvRJ4TM4ZL/x9vmRaaVNVqMdwF5a+D9DmOY6H071G0bsfW7V5H8FnAdwBrj/kRVDhFns4eeP7WgtyCt/m/yKHC9OkG5vuaCdN2/TF4y55BeW5okVfbZ0ro/4+wD9a0WyqRH0J6FSORHUFGf4I7iM0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1600331651; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=4seTrn3WCVDmAdbuChzGm3YYOn3ITl8aX5Rgmkl5zz4=; b=AXqkm6ExrAOkNLbDr1fH5qmcKdgF/ErcYBGrTJD0bcGMu0cgXjQ53abSz/nXYOck2ZeNUPJIkXHIKa1ZBQQVurkBS2gTeTpJ4vJwWF3V74EXZ/+oDBt5ZyS3PR3oXAQ1HmepGpkQutlgD96QvGb+2uUDn8eSVdF/vuNEmHNqhyc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by mx.zohomail.com with SMTPS id 1600331651830230.88467005858092; Thu, 17 Sep 2020 01:34:11 -0700 (PDT) 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-596-H4XyKI2UNaqsOgDAe8RRrw-1; Thu, 17 Sep 2020 04:31:51 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id B846956C89; Thu, 17 Sep 2020 08:31:46 +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 950625DEFD; Thu, 17 Sep 2020 08:31:46 +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 5D7EA44A7C; Thu, 17 Sep 2020 08:31:46 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 08H8ULe1004212 for ; Thu, 17 Sep 2020 04:30:21 -0400 Received: by smtp.corp.redhat.com (Postfix) id 46E575DEBF; Thu, 17 Sep 2020 08:30:21 +0000 (UTC) Received: from antique-work.redhat.com (unknown [10.40.195.159]) by smtp.corp.redhat.com (Postfix) with ESMTP id B83875DA30 for ; Thu, 17 Sep 2020 08:30:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1600331650; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=4seTrn3WCVDmAdbuChzGm3YYOn3ITl8aX5Rgmkl5zz4=; b=da69wcHLe8mRUq0QePQXjNNBk4e4LHUP9lVzAYvqmHqrKSZZ2e3bqgFcmUs0QC7OFZVdFu uyrgVYDXBx9mm8850szHkc+VoA53YUCWroKIhllEYXfbT2TFVODUdqPNSnx7vyJmMmbBEL KloltdRSITX6RLOaU5JAWSPmAaxL1VU= X-MC-Unique: H4XyKI2UNaqsOgDAe8RRrw-1 From: Pavel Hrdina To: libvir-list@redhat.com Subject: [libvirt PATCH 13/14] src/rpc/virnetdaemon: convert to use GLib DBus Date: Thu, 17 Sep 2020 10:29:48 +0200 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com 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: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0.001 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" Signed-off-by: Pavel Hrdina Reviewed-by: Michal Privoznik Reviewed-by: Neal Gompa --- src/rpc/meson.build | 1 - src/rpc/virnetdaemon.c | 115 +++++++++++++---------------------------- 2 files changed, 35 insertions(+), 81 deletions(-) diff --git a/src/rpc/meson.build b/src/rpc/meson.build index e249b9d534..cc1424140a 100644 --- a/src/rpc/meson.build +++ b/src/rpc/meson.build @@ -98,7 +98,6 @@ virt_rpc_server_lib =3D static_library( rpc_gen_headers, ], dependencies: [ - dbus_dep, sasl_dep, src_dep, xdr_dep, diff --git a/src/rpc/virnetdaemon.c b/src/rpc/virnetdaemon.c index b6c32ee277..12d4d9bf87 100644 --- a/src/rpc/virnetdaemon.c +++ b/src/rpc/virnetdaemon.c @@ -32,7 +32,7 @@ #include "virutil.h" #include "virfile.h" #include "virnetserver.h" -#include "virdbus.h" +#include "virgdbus.h" #include "virhash.h" #include "virstring.h" #include "virsystemd.h" @@ -78,7 +78,6 @@ struct _virNetDaemon { =20 unsigned int autoShutdownTimeout; size_t autoShutdownInhibitions; - bool autoShutdownCallingInhibit; int autoShutdownInhibitFd; }; =20 @@ -459,53 +458,7 @@ virNetDaemonAutoShutdown(virNetDaemonPtr dmn, } =20 =20 -#if defined(WITH_DBUS) && defined(DBUS_TYPE_UNIX_FD) -static void -virNetDaemonGotInhibitReplyLocked(DBusPendingCall *pending, - virNetDaemonPtr dmn) -{ - DBusMessage *reply; - int fd; - - dmn->autoShutdownCallingInhibit =3D false; - - VIR_DEBUG("dmn=3D%p", dmn); - - reply =3D dbus_pending_call_steal_reply(pending); - if (reply =3D=3D NULL) - goto cleanup; - - if (dbus_message_get_args(reply, NULL, - DBUS_TYPE_UNIX_FD, &fd, - DBUS_TYPE_INVALID)) { - if (dmn->autoShutdownInhibitions) { - dmn->autoShutdownInhibitFd =3D fd; - VIR_DEBUG("Got inhibit FD %d", fd); - } else { - /* We stopped the last VM since we made the inhibit call */ - VIR_DEBUG("Closing inhibit FD %d", fd); - VIR_FORCE_CLOSE(fd); - } - } - virDBusMessageUnref(reply); - - cleanup: - dbus_pending_call_unref(pending); -} - - -static void -virNetDaemonGotInhibitReply(DBusPendingCall *pending, - void *opaque) -{ - virNetDaemonPtr dmn =3D opaque; - - virObjectLock(dmn); - virNetDaemonGotInhibitReplyLocked(pending, dmn); - virObjectUnlock(dmn); -} - - +#ifdef G_OS_UNIX /* As per: https://www.freedesktop.org/wiki/Software/systemd/inhibit */ static void virNetDaemonCallInhibit(virNetDaemonPtr dmn, @@ -514,9 +467,12 @@ virNetDaemonCallInhibit(virNetDaemonPtr dmn, const char *why, const char *mode) { - DBusMessage *message; - DBusPendingCall *pendingReply =3D NULL; - DBusConnection *systemBus; + g_autoptr(GVariant) reply =3D NULL; + g_autoptr(GUnixFDList) replyFD =3D NULL; + GVariant *message =3D NULL; + GDBusConnection *systemBus; + int fd; + int rc; =20 VIR_DEBUG("dmn=3D%p what=3D%s who=3D%s why=3D%s mode=3D%s", dmn, NULLSTR(what), NULLSTR(who), NULLSTR(why), NULLSTR(mode= )); @@ -524,41 +480,40 @@ virNetDaemonCallInhibit(virNetDaemonPtr dmn, if (virSystemdHasLogind() < 0) return; =20 - if (!(systemBus =3D virDBusGetSystemBus())) + if (!(systemBus =3D virGDBusGetSystemBus())) return; =20 - /* Only one outstanding call at a time */ - if (dmn->autoShutdownCallingInhibit) + message =3D g_variant_new("(ssss)", what, who, why, mode); + + rc =3D virGDBusCallMethodWithFD(systemBus, + &reply, + &replyFD, + NULL, + "org.freedesktop.login1", + "/org/freedesktop/login1", + "org.freedesktop.login1.Manager", + "Inhibit", + message, + NULL); + + if (rc < 0) return; =20 - message =3D dbus_message_new_method_call("org.freedesktop.login1", - "/org/freedesktop/login1", - "org.freedesktop.login1.Manager= ", - "Inhibit"); - if (message =3D=3D NULL) + if (g_unix_fd_list_get_length(replyFD) <=3D 0) return; =20 - dbus_message_append_args(message, - DBUS_TYPE_STRING, &what, - DBUS_TYPE_STRING, &who, - DBUS_TYPE_STRING, &why, - DBUS_TYPE_STRING, &mode, - DBUS_TYPE_INVALID); + fd =3D g_unix_fd_list_get(replyFD, 0, NULL); + if (fd < 0) + return; =20 - if (dbus_connection_send_with_reply(systemBus, message, - &pendingReply, - 25 * 1000) && - pendingReply) { - if (dbus_pending_call_get_completed(pendingReply)) { - virNetDaemonGotInhibitReplyLocked(pendingReply, dmn); - } else { - dbus_pending_call_set_notify(pendingReply, - virNetDaemonGotInhibitReply, - dmn, NULL); - } - dmn->autoShutdownCallingInhibit =3D true; + if (dmn->autoShutdownInhibitions) { + dmn->autoShutdownInhibitFd =3D fd; + VIR_DEBUG("Got inhibit FD %d", fd); + } else { + /* We stopped the last VM since we made the inhibit call */ + VIR_DEBUG("Closing inhibit FD %d", fd); + VIR_FORCE_CLOSE(fd); } - virDBusMessageUnref(message); } #endif =20 @@ -570,7 +525,7 @@ virNetDaemonAddShutdownInhibition(virNetDaemonPtr dmn) =20 VIR_DEBUG("dmn=3D%p inhibitions=3D%zu", dmn, dmn->autoShutdownInhibiti= ons); =20 -#if defined(WITH_DBUS) && defined(DBUS_TYPE_UNIX_FD) +#ifdef G_OS_UNIX if (dmn->autoShutdownInhibitions =3D=3D 1) virNetDaemonCallInhibit(dmn, "shutdown", --=20 2.26.2 From nobody Sat May 4 20:55:56 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 63.128.21.124 as permitted sender) client-ip=63.128.21.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 63.128.21.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1600331593; cv=none; d=zohomail.com; s=zohoarc; b=kXDJa641jtStF2IKDx598ipfD8zCT2b8cd2qyRr+5uGW4LQuCdXapyp4sW43OpbFHZ1kwJKokS8sAnwPf+0ZtkNuRdnr3pVdUo6EIDcrwzSFrQ8TUYhHSWHCOroKRRBLYFAS8LwGf0U+6LqZYqBqHrjmwI8/1YOCo7ox2fBtNKU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1600331593; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=cMs0I7DyjN8iXEtoG/Exf4R3DIj1qZVcPDY5/Ydty70=; b=hAUvwAocUXpV+AV66nNPttBLrKFsgrgsN5K9hTW/D6HlHTue2AiXYbh0no07gkkyoFlbHmMr86XMjcLNu4gIyHsB3gCenqOnACdnKrbsPmWbkXg5cjGwCLA3CSrKih0SP18k/N3C2fbckD6YA0FBfnSdQFhKEPaWxzhWWibo3zc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 63.128.21.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [63.128.21.124]) by mx.zohomail.com with SMTPS id 1600331593936656.6276301002914; Thu, 17 Sep 2020 01:33:13 -0700 (PDT) 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-343-sRUZFO-8NJWFaaQjKyV29g-1; Thu, 17 Sep 2020 04:32:48 -0400 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 mimecast-mx01.redhat.com (Postfix) with ESMTPS id 1C03E64147; Thu, 17 Sep 2020 08:32:43 +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 E5FD7101416E; Thu, 17 Sep 2020 08:32:42 +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 AA4C244A7C; Thu, 17 Sep 2020 08:32:42 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 08H8UMoK004226 for ; Thu, 17 Sep 2020 04:30:22 -0400 Received: by smtp.corp.redhat.com (Postfix) id 88A2D5DA30; Thu, 17 Sep 2020 08:30:22 +0000 (UTC) Received: from antique-work.redhat.com (unknown [10.40.195.159]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9E9D55DEBF for ; Thu, 17 Sep 2020 08:30:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1600331592; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=cMs0I7DyjN8iXEtoG/Exf4R3DIj1qZVcPDY5/Ydty70=; b=NNIBrm4W/33iOZIatmi1kBe4exvtUMsrew/a9iU+HKqna9e0o0T9bFf4h1KlfwRuJCP5qW qW8mf8DhuQPDAKsgF71+0+ejyFTB5wFstHpJFjs6On+VFUJR7fShXwaVu+Jsa3g9xT5bfV /k8O50C15Y823pJGr2mn7+jhBC1D8lU= X-MC-Unique: sRUZFO-8NJWFaaQjKyV29g-1 From: Pavel Hrdina To: libvir-list@redhat.com Subject: [libvirt PATCH 14/14] drop libdbus from libvirt Date: Thu, 17 Sep 2020 10:29:49 +0200 Message-Id: <4cbe87b5b6b5410b92ef164a5956991a4eb0903a.1600331276.git.phrdina@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com 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: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0.104 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" Signed-off-by: Pavel Hrdina Reviewed-by: Michal Privoznik Reviewed-by: Neal Gompa --- libvirt.spec.in | 3 - meson.build | 27 +- meson_options.txt | 1 - po/POTFILES.in | 1 - src/libvirt_private.syms | 17 - src/libvirt_probes.d | 6 - src/meson.build | 1 - src/remote/remote_daemon_dispatch.c | 1 - src/util/meson.build | 2 - src/util/virdbus.c | 1871 --------------------------- src/util/virdbus.h | 76 -- src/util/virdbuspriv.h | 56 - tests/meson.build | 13 - tests/virdbusmock.c | 62 - tests/virdbustest.c | 728 ----------- 15 files changed, 3 insertions(+), 2862 deletions(-) delete mode 100644 src/util/virdbus.c delete mode 100644 src/util/virdbus.h delete mode 100644 src/util/virdbuspriv.h delete mode 100644 tests/virdbusmock.c delete mode 100644 tests/virdbustest.c diff --git a/libvirt.spec.in b/libvirt.spec.in index 62b401bd08..264932e6d5 100644 --- a/libvirt.spec.in +++ b/libvirt.spec.in @@ -374,9 +374,6 @@ BuildRequires: util-linux # For showmount in FS driver (netfs discovery) BuildRequires: nfs-utils =20 -# Communication with the firewall and polkit daemons use DBus -BuildRequires: dbus-devel - # Fedora build root suckage BuildRequires: gawk =20 diff --git a/meson.build b/meson.build index b5b1223227..24535a403c 100644 --- a/meson.build +++ b/meson.build @@ -1019,22 +1019,6 @@ if curl_dep.found() conf.set('WITH_CURL', 1) endif =20 -dbus_version =3D '1.0.0' -dbus_dep =3D dependency('dbus-1', version: '>=3D' + dbus_version, required= : get_option('dbus')) -if dbus_dep.found() - conf.set('WITH_DBUS', 1) - - function =3D 'dbus_watch_get_unix_fd' - if cc.has_function(function, dependencies: dbus_dep) - conf.set('WITH_@0@'.format(function.to_upper()), 1) - endif - - type =3D 'DBusBasicValue' - if cc.has_type(type, dependencies: dbus_dep, prefix: '#include ') - conf.set('WITH_@0@'.format(type.to_upper()), 1) - endif -endif - devmapper_version =3D '1.0.0' devmapper_dep =3D dependency('devmapper', version: '>=3D' + devmapper_vers= ion, required: false) if not devmapper_dep.found() @@ -1536,10 +1520,8 @@ if bash_completion_dep.found() endif =20 if host_machine.system() !=3D 'freebsd' - if not get_option('firewalld').disabled() and dbus_dep.found() + if not get_option('firewalld').disabled() conf.set('WITH_FIREWALLD', 1) - elif get_option('firewalld').enabled() - error('You must have dbus enabled for firewalld support') endif endif =20 @@ -1553,10 +1535,8 @@ if conf.has('WITH_MACVTAP') and not conf.has('WITH_L= IBNL') error('libnl3-devel is required for macvtap support') endif =20 -if (pkcheck_prog.found() or get_option('polkit').enabled()) and dbus_dep.f= ound() +if (pkcheck_prog.found() or get_option('polkit').enabled()) conf.set('WITH_POLKIT', 1) -elif get_option('polkit').enabled() - error('You must install dbus to compile libvirt with polkit-1') endif =20 if udev_dep.found() and not pciaccess_dep.found() @@ -2203,7 +2183,7 @@ endif =20 if not get_option('pm_utils').disabled() use_pm_utils =3D true - if dbus_dep.found() and init_script =3D=3D 'systemd' + if init_script =3D=3D 'systemd' use_pm_utils =3D false endif =20 @@ -2423,7 +2403,6 @@ libs_summary =3D { 'blkid': blkid_dep.found(), 'capng': capng_dep.found(), 'curl': curl_dep.found(), - 'dbus': dbus_dep.found(), 'dlopen': dlopen_dep.found(), 'firewalld': conf.has('WITH_FIREWALLD'), 'firewalld-zone': conf.has('WITH_FIREWALLD_ZONE'), diff --git a/meson_options.txt b/meson_options.txt index c8886e1430..f92c80553c 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -18,7 +18,6 @@ option('bash_completion_dir', type: 'string', value: '', = description: 'directory option('blkid', type: 'feature', value: 'auto', description: 'blkid suppor= t') option('capng', type: 'feature', value: 'auto', description: 'cap-ng suppo= rt') option('curl', type: 'feature', value: 'auto', description: 'curl support') -option('dbus', type: 'feature', value: 'auto', description: 'dbus-1 suppor= t') option('firewalld', type: 'feature', value: 'auto', description: 'firewall= d support') option('firewalld_zone', type: 'feature', value: 'auto', description: 'whe= ther to install firewalld libvirt zone') option('fuse', type: 'feature', value: 'auto', description: 'fuse support') diff --git a/po/POTFILES.in b/po/POTFILES.in index d87425a64c..54f78e7861 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -243,7 +243,6 @@ @SRCDIR@src/util/virconf.c @SRCDIR@src/util/vircrypto.c @SRCDIR@src/util/virdaemon.c -@SRCDIR@src/util/virdbus.c @SRCDIR@src/util/virdevmapper.c @SRCDIR@src/util/virdnsmasq.c @SRCDIR@src/util/virerror.c diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index fea5a49e55..bdbe3431b8 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1948,23 +1948,6 @@ virDaemonSetupLogging; virDaemonUnixSocketPaths; =20 =20 -# util/virdbus.h -virDBusCallMethod; -virDBusCloseSystemBus; -virDBusCreateMethod; -virDBusCreateMethodV; -virDBusCreateReply; -virDBusCreateReplyV; -virDBusErrorIsUnknownMethod; -virDBusGetSessionBus; -virDBusGetSystemBus; -virDBusHasSystemBus; -virDBusMessageDecode; -virDBusMessageEncode; -virDBusMessageUnref; -virDBusSetSharedBus; - - # util/virdevmapper.h virDevMapperGetTargets; virIsDevMapperDevice; diff --git a/src/libvirt_probes.d b/src/libvirt_probes.d index 5e4faa2f5b..6fac10a2bf 100644 --- a/src/libvirt_probes.d +++ b/src/libvirt_probes.d @@ -13,12 +13,6 @@ provider libvirt { probe event_glib_remove_timeout_idle(int timer, void *ff, void *opaque); probe event_glib_dispatch_timeout(int timer, void *cb, void *opaque); =20 - # file: src/util/virdbus.c - # prefix: dbus - probe dbus_method_call(const char *interface, const char *member, const c= har *object, const char *destination); - probe dbus_method_error(const char *interface, const char *member, const = char *object, const char *destination, const char *name, const char *messag= e); - probe dbus_method_reply(const char *interface, const char *member, const = char *object, const char *destination); - # file: src/util/virobject.c # prefix: object probe object_new(void *obj, const char *klassname); diff --git a/src/meson.build b/src/meson.build index 897b5ecbca..29c8210ab2 100644 --- a/src/meson.build +++ b/src/meson.build @@ -614,7 +614,6 @@ foreach daemon : virt_daemons dependencies: [ admin_dep, access_dep, - dbus_dep, gnutls_dep, libnl_dep, rpc_dep, diff --git a/src/remote/remote_daemon_dispatch.c b/src/remote/remote_daemon= _dispatch.c index 53d17a8f4a..c187932a3c 100644 --- a/src/remote/remote_daemon_dispatch.c +++ b/src/remote/remote_daemon_dispatch.c @@ -35,7 +35,6 @@ #include "virnetserver.h" #include "virfile.h" #include "virtypedparam.h" -#include "virdbus.h" #include "virprocess.h" #include "remote_protocol.h" #include "qemu_protocol.h" diff --git a/src/util/meson.build b/src/util/meson.build index 8a9dcac053..4d63173cbd 100644 --- a/src/util/meson.build +++ b/src/util/meson.build @@ -18,7 +18,6 @@ util_sources =3D [ 'virconf.c', 'vircrypto.c', 'virdaemon.c', - 'virdbus.c', 'virdevmapper.c', 'virdnsmasq.c', 'virebtables.c', @@ -185,7 +184,6 @@ virt_util_lib =3D static_library( acl_dep, audit_dep, capng_dep, - dbus_dep, devmapper_dep, gnutls_dep, intl_dep, diff --git a/src/util/virdbus.c b/src/util/virdbus.c deleted file mode 100644 index 459b57abc6..0000000000 --- a/src/util/virdbus.c +++ /dev/null @@ -1,1871 +0,0 @@ -/* - * virdbus.c: helper for using DBus - * - * Copyright (C) 2012-2014 Red Hat, Inc. - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library. If not, see - * . - * - */ - -#include - -#define LIBVIRT_VIRDBUSPRIV_H_ALLOW -#include "virdbuspriv.h" -#include "viralloc.h" -#include "virerror.h" -#include "virlog.h" -#include "virthread.h" -#include "virstring.h" -#include "virprobe.h" - -#define VIR_FROM_THIS VIR_FROM_DBUS - -VIR_LOG_INIT("util.dbus"); - -#ifdef WITH_DBUS - -static bool sharedBus =3D true; -static DBusConnection *systembus; -static DBusConnection *sessionbus; -static virOnceControl systemonce =3D VIR_ONCE_CONTROL_INITIALIZER; -static virOnceControl sessiononce =3D VIR_ONCE_CONTROL_INITIALIZER; -static DBusError systemdbuserr; -static DBusError sessiondbuserr; - -static dbus_bool_t virDBusAddWatch(DBusWatch *watch, void *data); -static void virDBusRemoveWatch(DBusWatch *watch, void *data); -static void virDBusToggleWatch(DBusWatch *watch, void *data); - -void virDBusSetSharedBus(bool shared) -{ - sharedBus =3D shared; -} - -static DBusConnection *virDBusBusInit(DBusBusType type, DBusError *dbuserr) -{ - DBusConnection *bus; - - /* Allocate and initialize a new HAL context */ - dbus_connection_set_change_sigpipe(FALSE); - dbus_threads_init_default(); - - dbus_error_init(dbuserr); - bus =3D sharedBus ? - dbus_bus_get(type, dbuserr) : - dbus_bus_get_private(type, dbuserr); - if (!bus) - return NULL; - - dbus_connection_set_exit_on_disconnect(bus, FALSE); - - /* Register dbus watch callbacks */ - if (!dbus_connection_set_watch_functions(bus, - virDBusAddWatch, - virDBusRemoveWatch, - virDBusToggleWatch, - bus, NULL)) { - return NULL; - } - return bus; -} - -static void virDBusSystemBusInit(void) -{ - systembus =3D virDBusBusInit(DBUS_BUS_SYSTEM, &systemdbuserr); -} - -static DBusConnection * -virDBusGetSystemBusInternal(void) -{ - if (virOnce(&systemonce, virDBusSystemBusInit) < 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("Unable to run one time DBus initializer")); - return NULL; - } - - return systembus; -} - - -DBusConnection * -virDBusGetSystemBus(void) -{ - DBusConnection *bus; - - if (!(bus =3D virDBusGetSystemBusInternal())) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Unable to get DBus system bus connection: %s"), - systemdbuserr.message ? systemdbuserr.message : "wa= tch setup failed"); - return NULL; - } - - return bus; -} - - -/** - * virDBusHasSystemBus: - * - * Check if dbus system bus is running. This does not - * imply that we have a connection. DBus might be running - * and refusing connections due to its client limit. The - * latter must be treated as a fatal error. - * - * Return false if dbus is not available, true if probably available. - */ -bool -virDBusHasSystemBus(void) -{ - if (virDBusGetSystemBusInternal()) - return true; - - if (systemdbuserr.name && - (STREQ(systemdbuserr.name, "org.freedesktop.DBus.Error.FileNotFoun= d") || - STREQ(systemdbuserr.name, "org.freedesktop.DBus.Error.NoServer"))= ) { - VIR_DEBUG("System DBus not available: %s", NULLSTR(systemdbuserr.m= essage)); - return false; - } - return true; -} - - -void virDBusCloseSystemBus(void) -{ - if (systembus && !sharedBus) { - dbus_connection_close(systembus); - dbus_connection_unref(systembus); - systembus =3D NULL; - } -} - -static void virDBusSessionBusInit(void) -{ - sessionbus =3D virDBusBusInit(DBUS_BUS_SESSION, &sessiondbuserr); -} - -DBusConnection *virDBusGetSessionBus(void) -{ - if (virOnce(&sessiononce, virDBusSessionBusInit) < 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("Unable to run one time DBus initializer")); - return NULL; - } - - if (!sessionbus) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Unable to get DBus session bus connection: %s"), - sessiondbuserr.message ? sessiondbuserr.message : "= watch setup failed"); - return NULL; - } - - return sessionbus; -} - -struct virDBusWatch -{ - int watch; - DBusConnection *bus; -}; - -static void virDBusWatchCallback(int fdatch G_GNUC_UNUSED, - int fd G_GNUC_UNUSED, - int events, void *opaque) -{ - DBusWatch *watch =3D opaque; - struct virDBusWatch *info; - int dbus_flags =3D 0; - - info =3D dbus_watch_get_data(watch); - - if (events & VIR_EVENT_HANDLE_READABLE) - dbus_flags |=3D DBUS_WATCH_READABLE; - if (events & VIR_EVENT_HANDLE_WRITABLE) - dbus_flags |=3D DBUS_WATCH_WRITABLE; - if (events & VIR_EVENT_HANDLE_ERROR) - dbus_flags |=3D DBUS_WATCH_ERROR; - if (events & VIR_EVENT_HANDLE_HANGUP) - dbus_flags |=3D DBUS_WATCH_HANGUP; - - if (dbus_watch_handle(watch, dbus_flags) =3D=3D FALSE) - VIR_DEBUG("dbus_watch_handle() returned FALSE"); - - dbus_connection_ref(info->bus); - while (dbus_connection_dispatch(info->bus) =3D=3D DBUS_DISPATCH_DATA_R= EMAINS) - /* keep dispatching while data remains */; - dbus_connection_unref(info->bus); -} - - -static int virDBusTranslateWatchFlags(int dbus_flags) -{ - unsigned int flags =3D 0; - if (dbus_flags & DBUS_WATCH_READABLE) - flags |=3D VIR_EVENT_HANDLE_READABLE; - if (dbus_flags & DBUS_WATCH_WRITABLE) - flags |=3D VIR_EVENT_HANDLE_WRITABLE; - if (dbus_flags & DBUS_WATCH_ERROR) - flags |=3D VIR_EVENT_HANDLE_ERROR; - if (dbus_flags & DBUS_WATCH_HANGUP) - flags |=3D VIR_EVENT_HANDLE_HANGUP; - return flags; -} - - -static void virDBusWatchFree(void *data) -{ - struct virDBusWatch *info =3D data; - VIR_FREE(info); -} - -static dbus_bool_t virDBusAddWatch(DBusWatch *watch, - void *data) -{ - int flags =3D 0; - int fd; - struct virDBusWatch *info; - - if (VIR_ALLOC(info) < 0) - return FALSE; - - if (dbus_watch_get_enabled(watch)) - flags =3D virDBusTranslateWatchFlags(dbus_watch_get_flags(watch)); - -# if WITH_DBUS_WATCH_GET_UNIX_FD - fd =3D dbus_watch_get_unix_fd(watch); -# else - fd =3D dbus_watch_get_fd(watch); -# endif - dbus_watch_set_data(watch, info, virDBusWatchFree); - info->bus =3D (DBusConnection *)data; - info->watch =3D virEventAddHandle(fd, flags, - virDBusWatchCallback, - watch, NULL); - if (info->watch < 0) { - dbus_watch_set_data(watch, NULL, NULL); - return FALSE; - } - - return TRUE; -} - - -static void virDBusRemoveWatch(DBusWatch *watch, - void *data G_GNUC_UNUSED) -{ - struct virDBusWatch *info; - - info =3D dbus_watch_get_data(watch); - - (void)virEventRemoveHandle(info->watch); -} - - -static void virDBusToggleWatch(DBusWatch *watch, - void *data G_GNUC_UNUSED) -{ - int flags =3D 0; - struct virDBusWatch *info; - - if (dbus_watch_get_enabled(watch)) - flags =3D virDBusTranslateWatchFlags(dbus_watch_get_flags(watch)); - - info =3D dbus_watch_get_data(watch); - - (void)virEventUpdateHandle(info->watch, flags); -} - -# define VIR_DBUS_TYPE_STACK_MAX_DEPTH 32 - -static const char virDBusBasicTypes[] =3D { - DBUS_TYPE_BYTE, - DBUS_TYPE_BOOLEAN, - DBUS_TYPE_INT16, - DBUS_TYPE_UINT16, - DBUS_TYPE_INT32, - DBUS_TYPE_UINT32, - DBUS_TYPE_INT64, - DBUS_TYPE_UINT64, - DBUS_TYPE_DOUBLE, - DBUS_TYPE_STRING, - DBUS_TYPE_OBJECT_PATH, - DBUS_TYPE_SIGNATURE, -}; - -static bool virDBusIsBasicType(char c) -{ - return !!memchr(virDBusBasicTypes, c, G_N_ELEMENTS(virDBusBasicTypes)); -} - -/* - * All code related to virDBusMessageIterEncode and - * virDBusMessageIterDecode is derived from systemd - * bus_message_append_ap()/message_read_ap() in - * bus-message.c under the terms of the LGPLv2+ - */ -static int -virDBusSignatureLengthInternal(const char *s, - bool allowDict, - unsigned arrayDepth, - unsigned structDepth, - size_t *skiplen, - size_t *siglen) -{ - if (virDBusIsBasicType(*s) || *s =3D=3D DBUS_TYPE_VARIANT) { - *skiplen =3D *siglen =3D 1; - return 0; - } - - if (*s =3D=3D DBUS_TYPE_ARRAY) { - size_t skiplencont; - size_t siglencont; - bool arrayref =3D false; - - if (arrayDepth >=3D VIR_DBUS_TYPE_STACK_MAX_DEPTH) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Signature '%s' too deeply nested"), - s); - return -1; - } - - if (*(s + 1) =3D=3D '&') { - arrayref =3D true; - s++; - } - - if (virDBusSignatureLengthInternal(s + 1, - true, - arrayDepth + 1, - structDepth, - &skiplencont, - &siglencont) < 0) - return -1; - - *skiplen =3D skiplencont + 1; - *siglen =3D siglencont + 1; - if (arrayref) - (*skiplen)++; - return 0; - } - - if (*s =3D=3D DBUS_STRUCT_BEGIN_CHAR) { - const char *p =3D s + 1; - - if (structDepth >=3D VIR_DBUS_TYPE_STACK_MAX_DEPTH) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Signature '%s' too deeply nested"), - s); - return -1; - } - - *skiplen =3D *siglen =3D 2; - - while (*p !=3D DBUS_STRUCT_END_CHAR) { - size_t skiplencont; - size_t siglencont; - - if (virDBusSignatureLengthInternal(p, - false, - arrayDepth, - structDepth + 1, - &skiplencont, - &siglencont) < 0) - return -1; - - p +=3D skiplencont; - *skiplen +=3D skiplencont; - *siglen +=3D siglencont; - } - - return 0; - } - - if (*s =3D=3D DBUS_DICT_ENTRY_BEGIN_CHAR && allowDict) { - const char *p =3D s + 1; - unsigned n =3D 0; - if (structDepth >=3D VIR_DBUS_TYPE_STACK_MAX_DEPTH) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Signature '%s' too deeply nested"), - s); - return -1; - } - - *skiplen =3D *siglen =3D 2; - - while (*p !=3D DBUS_DICT_ENTRY_END_CHAR) { - size_t skiplencont; - size_t siglencont; - - if (n =3D=3D 0 && !virDBusIsBasicType(*p)) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Dict entry in signature '%s' must be a b= asic type"), - s); - return -1; - } - - if (virDBusSignatureLengthInternal(p, - false, - arrayDepth, - structDepth + 1, - &skiplencont, - &siglencont) < 0) - return -1; - - p +=3D skiplencont; - *skiplen +=3D skiplencont; - *siglen +=3D siglencont; - n++; - } - - if (n !=3D 2) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Dict entry in signature '%s' is wrong size"), - s); - return -1; - } - - return 0; - } - - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Unexpected signature '%s'"), s); - return -1; -} - - -static int virDBusSignatureLength(const char *s, size_t *skiplen, size_t *= siglen) -{ - return virDBusSignatureLengthInternal(s, true, 0, 0, skiplen, siglen); -} - - -static char *virDBusCopyContainerSignature(const char *sig, - size_t *skiplen, - size_t *siglen) -{ - size_t i, j; - char *contsig; - bool isGroup; - - isGroup =3D (sig[0] =3D=3D DBUS_STRUCT_BEGIN_CHAR || - sig[0] =3D=3D DBUS_DICT_ENTRY_BEGIN_CHAR); - - if (virDBusSignatureLength(isGroup ? sig : sig + 1, skiplen, siglen) <= 0) - return NULL; - - if (VIR_ALLOC_N(contsig, *siglen + 1) < 0) - return NULL; - - for (i =3D 0, j =3D 0; i < *skiplen && j < *siglen; i++) { - if (sig[i + 1] =3D=3D '&') - continue; - contsig[j] =3D sig[i + 1]; - j++; - } - contsig[*siglen] =3D '\0'; - VIR_DEBUG("Extracted '%s' from '%s'", contsig, sig); - return contsig; -} - - -/* Ideally, we'd just call ourselves recursively on every - * complex type. However, the state of a va_list that is - * passed to a function is undefined after that function - * returns. This means we need to decode the va_list linearly - * in a single stackframe. We hence implement our own - * home-grown stack in an array. */ - -typedef struct _virDBusTypeStack virDBusTypeStack; -struct _virDBusTypeStack { - const char *types; - size_t nstruct; - size_t narray; - DBusMessageIter *iter; -}; - -static int virDBusTypeStackPush(virDBusTypeStack **stack, - size_t *nstack, - DBusMessageIter *iter, - const char *types, - size_t nstruct, - size_t narray) -{ - if (*nstack >=3D VIR_DBUS_TYPE_STACK_MAX_DEPTH) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("DBus type too deeply nested")); - return -1; - } - - if (VIR_EXPAND_N(*stack, *nstack, 1) < 0) - return -1; - - (*stack)[(*nstack) - 1].iter =3D iter; - (*stack)[(*nstack) - 1].types =3D types; - (*stack)[(*nstack) - 1].nstruct =3D nstruct; - (*stack)[(*nstack) - 1].narray =3D narray; - VIR_DEBUG("Pushed types=3D'%s' nstruct=3D%zu narray=3D%zd", - types, nstruct, (ssize_t)narray); - return 0; -} - - -static int virDBusTypeStackPop(virDBusTypeStack **stack, - size_t *nstack, - DBusMessageIter **iter, - const char **types, - size_t *nstruct, - size_t *narray) -{ - if (*nstack =3D=3D 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("DBus type stack is empty")); - return -1; - } - - *iter =3D (*stack)[(*nstack) - 1].iter; - *types =3D (*stack)[(*nstack) - 1].types; - *nstruct =3D (*stack)[(*nstack) - 1].nstruct; - *narray =3D (*stack)[(*nstack) - 1].narray; - VIR_DEBUG("Popped types=3D'%s' nstruct=3D%zu narray=3D%zd", - *types, *nstruct, (ssize_t)*narray); - VIR_SHRINK_N(*stack, *nstack, 1); - - return 0; -} - - -static void virDBusTypeStackFree(virDBusTypeStack **stack, - size_t *nstack) -{ - size_t i; - - if (!*stack) - return; - - /* The iter in the first level of the stack is the - * root iter which must not be freed - */ - for (i =3D 1; i < *nstack; i++) - VIR_FREE((*stack)[i].iter); - VIR_FREE(*stack); -} - - -static bool -virDBusIsAllowedRefType(const char *sig) -{ - if (*sig =3D=3D '{') { - if (strlen(sig) !=3D 4) - return false; - if (!virDBusIsBasicType(sig[1]) || - !virDBusIsBasicType(sig[2]) || - sig[1] !=3D sig[2]) - return false; - if (sig[3] !=3D '}') - return false; - } else { - if (strlen(sig) !=3D 1) - return false; - if (!virDBusIsBasicType(sig[0])) - return false; - } - return true; -} - - -# define SET_NEXT_VAL(dbustype, vargtype, arrtype, sigtype, fmt) \ - do { \ - dbustype x; \ - if (arrayref) { \ - arrtype valarray =3D arrayptr; \ - x =3D (dbustype)*valarray; \ - valarray++; \ - arrayptr =3D valarray; \ - } else { \ - x =3D (dbustype)va_arg(args, vargtype); \ - } \ - if (!dbus_message_iter_append_basic(iter, sigtype, &x)) { \ - virReportError(VIR_ERR_INTERNAL_ERROR, \ - _("Cannot append basic type %s"), #vargtype);\ - goto cleanup; \ - } \ - VIR_DEBUG("Appended basic type '" #dbustype "' varg '" #vargtype\ - "' sig '%c' val '" fmt "'", sigtype, (vargtype)x); \ - } while (0) - - -static int -virDBusMessageIterEncode(DBusMessageIter *rootiter, - const char *types, - va_list args) -{ - int ret =3D -1; - size_t narray; - size_t nstruct; - bool arrayref =3D false; - void *arrayptr =3D NULL; - virDBusTypeStack *stack =3D NULL; - size_t nstack =3D 0; - size_t siglen; - size_t skiplen; - char *contsig =3D NULL; - const char *vsig; - DBusMessageIter *newiter =3D NULL; - DBusMessageIter *iter =3D rootiter; - - VIR_DEBUG("rootiter=3D%p types=3D%s", rootiter, types); - - if (!types) - return 0; - - narray =3D (size_t)-1; - nstruct =3D strlen(types); - - for (;;) { - const char *t; - - VIR_DEBUG("Loop nstack=3D%zu narray=3D%zd nstruct=3D%zu types=3D'%= s'", - nstack, (ssize_t)narray, nstruct, types); - if (narray =3D=3D 0 || - (narray =3D=3D (size_t)-1 && - nstruct =3D=3D 0)) { - DBusMessageIter *thisiter =3D iter; - if (*types !=3D '}') { - VIR_DEBUG("Reset array ref"); - arrayref =3D false; - arrayptr =3D NULL; - } - VIR_DEBUG("Popping iter=3D%p", iter); - if (nstack =3D=3D 0) - break; - if (virDBusTypeStackPop(&stack, &nstack, &iter, - &types, &nstruct, &narray) < 0) - goto cleanup; - VIR_DEBUG("Popped iter=3D%p", iter); - - if (!dbus_message_iter_close_container(iter, thisiter)) { - if (thisiter !=3D rootiter) - VIR_FREE(thisiter); - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("Cannot close container iterator")); - goto cleanup; - } - if (thisiter !=3D rootiter) - VIR_FREE(thisiter); - continue; - } - - t =3D types; - if (narray !=3D (size_t)-1) { - narray--; - } else { - types++; - nstruct--; - } - - switch (*t) { - case DBUS_TYPE_BYTE: - SET_NEXT_VAL(unsigned char, int, unsigned char *, *t, "%d"); - break; - - case DBUS_TYPE_BOOLEAN: - SET_NEXT_VAL(dbus_bool_t, int, bool *, *t, "%d"); - break; - - case DBUS_TYPE_INT16: - SET_NEXT_VAL(dbus_int16_t, int, short *, *t, "%d"); - break; - - case DBUS_TYPE_UINT16: - SET_NEXT_VAL(dbus_uint16_t, unsigned int, unsigned short *, - *t, "%d"); - break; - - case DBUS_TYPE_INT32: - SET_NEXT_VAL(dbus_int32_t, int, int *, *t, "%d"); - break; - - case DBUS_TYPE_UINT32: - SET_NEXT_VAL(dbus_uint32_t, unsigned int, unsigned int *, - *t, "%u"); - break; - - case DBUS_TYPE_INT64: - SET_NEXT_VAL(dbus_int64_t, long long, long long *, *t, "%lld"); - break; - - case DBUS_TYPE_UINT64: - SET_NEXT_VAL(dbus_uint64_t, unsigned long long, - unsigned long long *, *t, "%llu"); - break; - - case DBUS_TYPE_DOUBLE: - SET_NEXT_VAL(double, double, double *, *t, "%lf"); - break; - - case DBUS_TYPE_STRING: - case DBUS_TYPE_OBJECT_PATH: - case DBUS_TYPE_SIGNATURE: - SET_NEXT_VAL(char *, char *, char **, *t, "%s"); - break; - - case DBUS_TYPE_ARRAY: - arrayptr =3D NULL; - if (t[1] =3D=3D '&') { - VIR_DEBUG("Got array ref"); - t++; - types++; - nstruct--; - arrayref =3D true; - } else { - VIR_DEBUG("Got array non-ref"); - arrayref =3D false; - } - - if (!(contsig =3D virDBusCopyContainerSignature(t, &skiplen, &= siglen))) - goto cleanup; - - if (arrayref && !virDBusIsAllowedRefType(contsig)) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Got array ref but '%s' is not a single b= asic type " - "or dict with matching key+value type"), - contsig); - goto cleanup; - } - - if (narray =3D=3D (size_t)-1) { - types +=3D skiplen; - nstruct -=3D skiplen; - } - - if (VIR_ALLOC(newiter) < 0) - goto cleanup; - VIR_DEBUG("Contsig '%s' skip=3D'%zu' len=3D'%zu'", contsig, sk= iplen, siglen); - if (!dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY, - contsig, newiter)) - goto cleanup; - if (virDBusTypeStackPush(&stack, &nstack, - iter, types, - nstruct, narray) < 0) { - VIR_FREE(newiter); - goto cleanup; - } - VIR_FREE(contsig); - iter =3D g_steal_pointer(&newiter); - types =3D t + 1; - nstruct =3D skiplen; - narray =3D (size_t)va_arg(args, int); - if (arrayref) - arrayptr =3D va_arg(args, void *); - break; - - case DBUS_TYPE_VARIANT: - vsig =3D va_arg(args, const char *); - if (!vsig) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("Missing variant type signature")); - goto cleanup; - } - if (VIR_ALLOC(newiter) < 0) - goto cleanup; - if (!dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT, - vsig, newiter)) - goto cleanup; - if (virDBusTypeStackPush(&stack, &nstack, - iter, types, - nstruct, narray) < 0) { - VIR_FREE(newiter); - goto cleanup; - } - iter =3D g_steal_pointer(&newiter); - types =3D vsig; - nstruct =3D strlen(types); - narray =3D (size_t)-1; - break; - - case DBUS_STRUCT_BEGIN_CHAR: - case DBUS_DICT_ENTRY_BEGIN_CHAR: - if (!(contsig =3D virDBusCopyContainerSignature(t, &skiplen, &= siglen))) - goto cleanup; - - if (VIR_ALLOC(newiter) < 0) - goto cleanup; - VIR_DEBUG("Contsig '%s' skip=3D'%zu' len=3D'%zu'", contsig, sk= iplen, siglen); - if (!dbus_message_iter_open_container(iter, - *t =3D=3D DBUS_STRUCT_BE= GIN_CHAR ? - DBUS_TYPE_STRUCT : DBUS_= TYPE_DICT_ENTRY, - NULL, newiter)) - goto cleanup; - if (narray =3D=3D (size_t)-1) { - types +=3D skiplen - 1; - nstruct -=3D skiplen - 1; - } - - if (virDBusTypeStackPush(&stack, &nstack, - iter, types, - nstruct, narray) < 0) { - VIR_FREE(newiter); - goto cleanup; - } - VIR_FREE(contsig); - iter =3D g_steal_pointer(&newiter); - types =3D t + 1; - nstruct =3D skiplen - 2; - narray =3D (size_t)-1; - - break; - - default: - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Unknown type '%x' in signature '%s'"), - (int)*t, types); - goto cleanup; - } - } - - ret =3D 0; - - cleanup: - while (nstack > 0) { - DBusMessageIter *thisiter =3D iter; - VIR_DEBUG("Popping iter=3D%p", iter); - ignore_value(virDBusTypeStackPop(&stack, &nstack, &iter, - &types, &nstruct, &narray)); - VIR_DEBUG("Popped iter=3D%p", iter); - - if (thisiter !=3D rootiter) - VIR_FREE(thisiter); - } - - virDBusTypeStackFree(&stack, &nstack); - VIR_FREE(contsig); - VIR_FREE(newiter); - return ret; -} -# undef SET_NEXT_VAL - - -# define GET_NEXT_VAL(dbustype, member, vargtype, fmt) \ - do { \ - DBusBasicValue v; \ - dbustype *x =3D (dbustype *)&v.member; \ - vargtype *y; \ - if (arrayref) { \ - vargtype **xptrptr =3D arrayptr; \ - VIR_DEBUG("Use arrayref"); \ - if (VIR_EXPAND_N(*xptrptr, *narrayptr, 1) < 0) \ - goto cleanup; \ - y =3D (*xptrptr + (*narrayptr - 1)); \ - VIR_DEBUG("Expanded to %zu", *narrayptr); \ - } else { \ - y =3D va_arg(args, vargtype *); \ - } \ - dbus_message_iter_get_basic(iter, x); \ - *y =3D *x; \ - VIR_DEBUG("Read basic type '" #dbustype "' varg '" #vargtype \ - "' val '" fmt "'", (vargtype)*y); \ - } while (0) - - -static int -virDBusMessageIterDecode(DBusMessageIter *rootiter, - const char *types, - va_list args) -{ - int ret =3D -1; - size_t narray; - size_t nstruct; - bool arrayref =3D false; - void *arrayptr =3D NULL; - size_t *narrayptr =3D 0; - virDBusTypeStack *stack =3D NULL; - size_t nstack =3D 0; - size_t skiplen; - size_t siglen; - char *contsig =3D NULL; - const char *vsig; - DBusMessageIter *newiter =3D NULL; - DBusMessageIter *iter =3D rootiter; - - VIR_DEBUG("rootiter=3D%p types=3D%s", rootiter, types); - - if (!types) - return 0; - - narray =3D (size_t)-1; - nstruct =3D strlen(types); - - for (;;) { - const char *t; - bool advanceiter =3D true; - - VIR_DEBUG("Loop nstack=3D%zu narray=3D%zd nstruct=3D%zu type=3D'%s= '", - nstack, (ssize_t)narray, nstruct, types); - if (narray =3D=3D 0 || - (narray =3D=3D (size_t)-1 && - nstruct =3D=3D 0)) { - DBusMessageIter *thisiter =3D iter; - VIR_DEBUG("Popping iter=3D%p", iter); - if (nstack =3D=3D 0) - break; - if (virDBusTypeStackPop(&stack, &nstack, &iter, - &types, &nstruct, &narray) < 0) - goto cleanup; - VIR_DEBUG("Popped iter=3D%p types=3D%s", iter, types); - if (strchr(types, '}') =3D=3D NULL) { - arrayref =3D false; - arrayptr =3D NULL; - VIR_DEBUG("Clear array ref flag"); - } - if (thisiter !=3D rootiter) - VIR_FREE(thisiter); - if (arrayref) { - if (!dbus_message_iter_has_next(iter)) - narray =3D 0; - else - narray =3D 1; - VIR_DEBUG("Pop set narray=3D%zd", (ssize_t)narray); - } - if (!(narray =3D=3D 0 || - (narray =3D=3D (size_t)-1 && - nstruct =3D=3D 0)) && - !dbus_message_iter_next(iter)) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("Not enough fields in message for signatu= re")); - goto cleanup; - } - continue; - } - - t =3D types; - if (narray !=3D (size_t)-1) { - if (!arrayref) - narray--; - } else { - types++; - nstruct--; - } - - switch (*t) { - case DBUS_TYPE_BYTE: - GET_NEXT_VAL(unsigned char, byt, unsigned char, "%d"); - break; - - case DBUS_TYPE_BOOLEAN: - GET_NEXT_VAL(dbus_bool_t, bool_val, bool, "%d"); - break; - - case DBUS_TYPE_INT16: - GET_NEXT_VAL(dbus_int16_t, i16, short, "%d"); - break; - - case DBUS_TYPE_UINT16: - GET_NEXT_VAL(dbus_uint16_t, u16, unsigned short, "%d"); - break; - - case DBUS_TYPE_INT32: - GET_NEXT_VAL(dbus_uint32_t, i32, int, "%d"); - break; - - case DBUS_TYPE_UINT32: - GET_NEXT_VAL(dbus_uint32_t, u32, unsigned int, "%u"); - break; - - case DBUS_TYPE_INT64: - GET_NEXT_VAL(dbus_uint64_t, i64, long long, "%lld"); - break; - - case DBUS_TYPE_UINT64: - GET_NEXT_VAL(dbus_uint64_t, u64, unsigned long long, "%llu"); - break; - - case DBUS_TYPE_DOUBLE: - GET_NEXT_VAL(double, dbl, double, "%lf"); - break; - - case DBUS_TYPE_STRING: - case DBUS_TYPE_OBJECT_PATH: - case DBUS_TYPE_SIGNATURE: - do { - char **x; - char *s; - if (arrayref) { - char ***xptrptr =3D arrayptr; - if (VIR_EXPAND_N(*xptrptr, *narrayptr, 1) < 0) - goto cleanup; - x =3D (char **)(*xptrptr + (*narrayptr - 1)); - VIR_DEBUG("Expanded to %zu", *narrayptr); - } else { - x =3D (char **)va_arg(args, char **); - } - dbus_message_iter_get_basic(iter, &s); - *x =3D g_strdup(s); - VIR_DEBUG("Read basic type 'char *' varg 'char **'" - "' val '%s'", *x); - } while (0); - break; - - case DBUS_TYPE_ARRAY: - arrayptr =3D NULL; - if (t[1] =3D=3D '&') { - VIR_DEBUG("Got array ref"); - t++; - types++; - nstruct--; - arrayref =3D true; - } else { - VIR_DEBUG("Got array non-ref"); - arrayref =3D false; - } - - advanceiter =3D false; - if (!(contsig =3D virDBusCopyContainerSignature(t, &skiplen, &= siglen))) - goto cleanup; - - if (arrayref && !virDBusIsAllowedRefType(contsig)) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Got array ref but '%s' is not a single b= asic type / dict"), - contsig); - goto cleanup; - } - - if (narray =3D=3D (size_t)-1) { - types +=3D skiplen; - nstruct -=3D skiplen; - } - - if (VIR_ALLOC(newiter) < 0) - goto cleanup; - VIR_DEBUG("Array contsig=3D'%s' skip=3D%'zu' len=3D'%zu' types= =3D'%s'", - contsig, skiplen, siglen, types); - dbus_message_iter_recurse(iter, newiter); - if (virDBusTypeStackPush(&stack, &nstack, - iter, types, - nstruct, narray) < 0) - goto cleanup; - VIR_FREE(contsig); - iter =3D g_steal_pointer(&newiter); - types =3D t + 1; - nstruct =3D skiplen; - if (arrayref) { - narrayptr =3D va_arg(args, size_t *); - arrayptr =3D va_arg(args, void *); - *narrayptr =3D 0; - *(char **)arrayptr =3D NULL; - } else { - narray =3D va_arg(args, int); - } - break; - - case DBUS_TYPE_VARIANT: - advanceiter =3D false; - vsig =3D va_arg(args, const char *); - if (!vsig) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("Missing variant type signature")); - goto cleanup; - } - if (VIR_ALLOC(newiter) < 0) - goto cleanup; - dbus_message_iter_recurse(iter, newiter); - if (virDBusTypeStackPush(&stack, &nstack, - iter, types, - nstruct, narray) < 0) { - VIR_DEBUG("Push failed"); - goto cleanup; - } - iter =3D g_steal_pointer(&newiter); - types =3D vsig; - nstruct =3D strlen(types); - narray =3D (size_t)-1; - break; - - case DBUS_STRUCT_BEGIN_CHAR: - case DBUS_DICT_ENTRY_BEGIN_CHAR: - advanceiter =3D false; - if (!(contsig =3D virDBusCopyContainerSignature(t, &skiplen, &= siglen))) - goto cleanup; - - if (VIR_ALLOC(newiter) < 0) - goto cleanup; - VIR_DEBUG("Dict/struct contsig=3D'%s' skip=3D'%zu' len=3D'%zu'= types=3D'%s'", - contsig, skiplen, siglen, types); - dbus_message_iter_recurse(iter, newiter); - if (narray =3D=3D (size_t)-1) { - types +=3D skiplen - 1; - nstruct -=3D skiplen - 1; - } - - if (virDBusTypeStackPush(&stack, &nstack, - iter, types, - nstruct, narray) < 0) - goto cleanup; - VIR_FREE(contsig); - iter =3D g_steal_pointer(&newiter); - types =3D t + 1; - nstruct =3D skiplen - 2; - narray =3D (size_t)-1; - - break; - - default: - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Unknown type '%c' in signature '%s'"), - *t, types); - goto cleanup; - } - - VIR_DEBUG("After nstack=3D%zu narray=3D%zd nstruct=3D%zu types=3D'= %s'", - nstack, (ssize_t)narray, nstruct, types); - - if (arrayref) { - if (dbus_message_iter_get_arg_type(iter) =3D=3D DBUS_TYPE_INVA= LID) { - narray =3D 0; - } else { - if (advanceiter) - dbus_message_iter_next(iter); - if (dbus_message_iter_get_arg_type(iter) =3D=3D DBUS_TYPE_= INVALID) { - narray =3D 0; - } else { - narray =3D 1; - } - } - VIR_DEBUG("Set narray=3D%zd", (ssize_t)narray); - } else { - if (advanceiter && - !(narray =3D=3D 0 || - (narray =3D=3D (size_t)-1 && - nstruct =3D=3D 0)) && - !dbus_message_iter_next(iter)) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("Not enough fields in message for signatu= re")); - goto cleanup; - } - } - } - - if (dbus_message_iter_has_next(iter)) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("Too many fields in message for signature")); - goto cleanup; - } - - ret =3D 0; - - cleanup: - virDBusTypeStackFree(&stack, &nstack); - VIR_FREE(contsig); - VIR_FREE(newiter); - return ret; -} -# undef GET_NEXT_VAL - -int -virDBusMessageEncodeArgs(DBusMessage* msg, - const char *types, - va_list args) -{ - DBusMessageIter iter; - - memset(&iter, 0, sizeof(iter)); - - dbus_message_iter_init_append(msg, &iter); - - return virDBusMessageIterEncode(&iter, types, args); -} - - -int virDBusMessageDecodeArgs(DBusMessage* msg, - const char *types, - va_list args) -{ - DBusMessageIter iter; - - if (!dbus_message_iter_init(msg, &iter)) { - if (*types !=3D '\0') { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("No args present for signature %s"), - types); - } else { - return 0; - } - return -1; - } - - return virDBusMessageIterDecode(&iter, types, args); -} - - -int virDBusMessageEncode(DBusMessage* msg, - const char *types, - ...) -{ - int ret; - va_list args; - va_start(args, types); - ret =3D virDBusMessageEncodeArgs(msg, types, args); - va_end(args); - return ret; -} - - -/** - * virDBusMessageDecode: - * @msg: the reply to decode - * @types: type signature for following return values - * @...: pointers in which to store return values - * - * The @types type signature is the same format as - * that used for the virDBusCallMethod. The difference - * is that each variadic parameter must be a pointer to - * be filled with the values. eg instead of passing an - * 'int', pass an 'int *'. - * - */ -int -virDBusMessageDecode(DBusMessage* msg, - const char *types, - ...) -{ - int ret; - va_list args; - va_start(args, types); - ret =3D virDBusMessageDecodeArgs(msg, types, args); - va_end(args); - return ret; -} - -# define VIR_DBUS_METHOD_CALL_TIMEOUT_MILLIS 30 * 1000 - -/** - * virDBusCreateMethodV: - * @call: pointer to be filled with a method call message - * @destination: bus identifier of the target service - * @path: object path of the target service - * @iface: the interface of the object - * @member: the name of the method in the interface - * @types: type signature for following method arguments - * @args: method arguments - * - * This creates a DBus method call message and saves a - * pointer to it in @call. The @destination, @path, @iface - * and @member parameters identify the object method to - * be invoked. The optional @replyout parameter will be - * filled with any reply to the method call. The method - * can be later invoked using virDBusCall. - * - * The @types parameter is a DBus signature describing - * the method call parameters which will be provided - * as variadic args. Each character in @types must - * correspond to one of the following DBus codes for - * basic types: - * - * 'y' - 8-bit byte, promoted to an 'int' - * 'b' - bool value, promoted to an 'int' - * 'n' - 16-bit signed integer, promoted to an 'int' - * 'q' - 16-bit unsigned integer, promoted to an 'int' - * 'i' - 32-bit signed integer, passed as an 'int' - * 'u' - 32-bit unsigned integer, passed as an 'int' - * 'x' - 64-bit signed integer, passed as a 'long long' - * 't' - 64-bit unsigned integer, passed as an 'unsigned long long' - * 'd' - 8-byte floating point, passed as a 'double' - * 's' - NUL-terminated string, in UTF-8 - * 'o' - NUL-terminated string, representing a valid object path - * 'g' - NUL-terminated string, representing a valid type signature - * - * or use one of the compound types - * - * 'a' - array of values - * 'v' - a variadic type. - * '(' - start of a struct - * ')' - end of a struct - * '{' - start of a dictionary entry (pair of types) - * '}' - start of a dictionary entry (pair of types) - * - * At this time, there is no support for Unix fd's ('h'), which only - * newer DBus supports. - * - * Passing values in variadic args for basic types is - * simple, the value is just passed directly using the - * corresponding C type listed against the type code - * above. Note how any integer value smaller than an - * 'int' is promoted to an 'int' by the C rules for - * variadic args. - * - * Passing values in variadic args for compound types - * requires a little further explanation. - * - * - Variant: the first arg is a string containing - * the type signature for the values to be stored - * inside the variant. This is then followed by - * the values corresponding to the type signature - * in the normal manner. - * - * - Array: when 'a' appears in a type signature, it - * must be followed by a single type describing the - * array element type. For example 'as' is an array - * of strings. 'a(is)' is an array of structs, each - * struct containing an int and a string. - * - * The first variadic arg for an array, is an 'int' - * specifying the number of elements in the array. - * This is then followed by additional variadic args, - * one for each element of the array. - * - * - Array reference: when 'a' appears in a type signature, - * followed by '&', this signifies an array passed by - * reference. - * - * Array references may only be used when the - * element values are basic types, or a dict - * entry where both keys and values are using - * the same basic type. - * - * The first variadic arg for an array, is an 'int' - * specifying the number of elements in the array. - * When the element is a basic type, the second - * variadic arg is a pointer to an array containing - * the element values. When the element is a dict - * entry, the second variadic arg is a pointer to - * an array containing the dict keys, and the - * third variadic arg is a pointer to an array - * containing the dict values. - * - * - Struct: when a '(' appears in a type signature, - * it must be followed by one or more types describing - * the elements in the array, terminated by a ')'. - * - * - Dict entry: when a '{' appears in a type signature it - * must be followed by exactly two types, one describing - * the type of the hash key, the other describing the - * type of the hash entry. The hash key type must be - * a basic type, not a compound type. - * - * Example signatures, with their corresponding variadic - * args: - * - * - "biiss" - some basic types - * - * (true, 7, 42, "hello", "world") - * - * - "as" - an array with a basic type element - * - * (3, "one", "two", "three") - * - * - "a(is)" - an array with a struct element - * - * (3, 1, "one", 2, "two", 3, "three") - * - * - "svs" - some basic types with a variant as an int - * - * ("hello", "i", 3, "world") - * - * - "svs" - some basic types with a variant as an array of ints - * - * ("hello", "ai", 4, 1, 2, 3, 4, "world") - * - * - "a{ss}" - a hash table (aka array + dict entry) - * - * (3, "title", "Mr", "forename", "Joe", "surname", "Bloggs") - * - * - "a{sv}" - a hash table (aka array + dict entry) - * - * (3, "email", "s", "joe@blogs.com", "age", "i", 35, - * "address", "as", 3, "Some house", "Some road", "some city") - */ -int virDBusCreateMethodV(DBusMessage **call, - const char *destination, - const char *path, - const char *iface, - const char *member, - const char *types, - va_list args) -{ - if (!(*call =3D dbus_message_new_method_call(destination, - path, - iface, - member))) { - virReportOOMError(); - return -1; - } - - if (virDBusMessageEncodeArgs(*call, types, args) < 0) { - virDBusMessageUnref(*call); - *call =3D NULL; - return -1; - } - - return 0; -} - - -/** - * virDBusCreateMethod: - * @call: pointer to be filled with a method call message - * @destination: bus identifier of the target service - * @path: object path of the target service - * @iface: the interface of the object - * @member: the name of the method in the interface - * @types: type signature for following method arguments - * @...: method arguments - * - * See virDBusCreateMethodV for a description of the - * behaviour of this method. - */ -int virDBusCreateMethod(DBusMessage **call, - const char *destination, - const char *path, - const char *iface, - const char *member, - const char *types, ...) -{ - va_list args; - int ret; - - va_start(args, types); - ret =3D virDBusCreateMethodV(call, destination, path, - iface, member, types, args); - va_end(args); - - return ret; -} - - -/** - * virDBusCreateReplyV: - * @reply: pointer to be filled with a method reply message - * @types: type signature for following method arguments - * @args: method arguments - * - * This creates a DBus method reply message and saves a - * pointer to it in @reply. - * - * The @types parameter is a DBus signature describing - * the method call parameters which will be provided - * as variadic args. See virDBusCreateMethodV for a - * description of this parameter. - */ -int virDBusCreateReplyV(DBusMessage **reply, - const char *types, - va_list args) -{ - if (!(*reply =3D dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_RETURN))) { - virReportOOMError(); - return -1; - } - - if (virDBusMessageEncodeArgs(*reply, types, args) < 0) { - virDBusMessageUnref(*reply); - *reply =3D NULL; - return -1; - } - - return 0; -} - - -/** - * virDBusCreateReply: - * @reply: pointer to be filled with a method reply message - * @types: type signature for following method arguments - * @...: method arguments - * - * See virDBusCreateReplyV for a description of the - * behaviour of this method. - */ -int virDBusCreateReply(DBusMessage **reply, - const char *types, ...) -{ - va_list args; - int ret; - - va_start(args, types); - ret =3D virDBusCreateReplyV(reply, types, args); - va_end(args); - - return ret; -} - - -/** - * virDBusCall: - * @conn: a DBus connection - * @call: pointer to a message to send - * @replyout: pointer to receive reply message, or NULL - * @error: pointer to receive error message - * - * This invokes a method encoded in @call on a remote - * service on the DBus bus @conn. The optional @replyout - * parameter will be filled with any reply to the method - * call. The virDBusMethodReply method can be used to - * decode the return values. - * - * If @error is NULL then a libvirt error will be raised - * when a DBus error is received and the return value will - * be -1. If @error is non-NULL then any DBus error will - * be saved into that object and the return value will - * be 0. - * - * Returns 0 on success, or -1 upon error - */ -static int -virDBusCall(DBusConnection *conn, - DBusMessage *call, - DBusMessage **replyout, - virErrorPtr error) - -{ - DBusMessage *reply =3D NULL; - DBusError localerror; - int ret =3D -1; - const char *iface, *member, *path, *dest; - - dbus_error_init(&localerror); - if (error) - memset(error, 0, sizeof(*error)); - - iface =3D dbus_message_get_interface(call); - member =3D dbus_message_get_member(call); - path =3D dbus_message_get_path(call); - dest =3D dbus_message_get_destination(call); - - PROBE(DBUS_METHOD_CALL, - "'%s.%s' on '%s' at '%s'", - iface, member, path, dest); - - if (!(reply =3D dbus_connection_send_with_reply_and_block(conn, - call, - VIR_DBUS_METHO= D_CALL_TIMEOUT_MILLIS, - &localerror)))= { - PROBE(DBUS_METHOD_ERROR, - "'%s.%s' on '%s' at '%s' error %s: %s", - iface, member, path, dest, - localerror.name, - localerror.message); - if (error) { - error->level =3D VIR_ERR_ERROR; - error->code =3D VIR_ERR_DBUS_SERVICE; - error->domain =3D VIR_FROM_DBUS; - error->message =3D g_strdup(localerror.message); - error->str1 =3D g_strdup(localerror.name); - ret =3D 0; - } else { - virReportError(VIR_ERR_DBUS_SERVICE, _("%s: %s"), member, - localerror.message ? : _("unknown error")); - } - goto cleanup; - } - - PROBE(DBUS_METHOD_REPLY, - "'%s.%s' on '%s' at '%s'", - iface, member, path, dest); - - ret =3D 0; - - cleanup: - if (ret < 0 && error) - virResetError(error); - dbus_error_free(&localerror); - if (reply) { - if (ret =3D=3D 0 && replyout) - *replyout =3D reply; - else - virDBusMessageUnref(reply); - } - return ret; -} - - -/** - * virDBusCallMethod: - * @conn: a DBus connection - * @replyout: pointer to receive reply message, or NULL - * @destination: bus identifier of the target service - * @path: object path of the target service - * @iface: the interface of the object - * @member: the name of the method in the interface - * @types: type signature for following method arguments - * @...: method arguments - * - * This invokes a method on a remote service on the - * DBus bus @conn. The @destination, @path, @iface - * and @member parameters identify the object method to - * be invoked. The optional @replyout parameter will be - * filled with any reply to the method call. The - * virDBusMethodReply method can be used to decode the - * return values. - * - * The @types parameter is a DBus signature describing - * the method call parameters which will be provided - * as variadic args. See virDBusCreateMethodV for a - * description of this parameter. - * - * - * If @error is NULL then a libvirt error will be raised - * when a DBus error is received and the return value will - * be -1. If @error is non-NULL then any DBus error will - * be saved into that object and the return value will - * be 0. If an error occurs while encoding method args - * the return value will always be -1 regardless of whether - * @error is set. - * - * Returns 0 on success, or -1 upon error - */ -int virDBusCallMethod(DBusConnection *conn, - DBusMessage **replyout, - virErrorPtr error, - const char *destination, - const char *path, - const char *iface, - const char *member, - const char *types, ...) -{ - DBusMessage *call =3D NULL; - int ret =3D -1; - va_list args; - - va_start(args, types); - ret =3D virDBusCreateMethodV(&call, destination, path, - iface, member, types, args); - va_end(args); - if (ret < 0) - goto cleanup; - - ret =3D virDBusCall(conn, call, replyout, error); - - cleanup: - virDBusMessageUnref(call); - return ret; -} - - -static int virDBusIsServiceInList(const char *listMethod, const char *name) -{ - DBusConnection *conn; - DBusMessage *reply =3D NULL; - DBusMessageIter iter, sub; - int ret =3D -1; - - if (!virDBusHasSystemBus()) - return -2; - - if (!(conn =3D virDBusGetSystemBus())) - return -1; - - if (virDBusCallMethod(conn, - &reply, - NULL, - "org.freedesktop.DBus", - "/org/freedesktop/DBus", - "org.freedesktop.DBus", - listMethod, - NULL) < 0) - return ret; - - if (!dbus_message_iter_init(reply, &iter) || - dbus_message_iter_get_arg_type(&iter) !=3D DBUS_TYPE_ARRAY) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("Reply message incorrect")); - goto cleanup; - } - - ret =3D -2; - dbus_message_iter_recurse(&iter, &sub); - while (dbus_message_iter_get_arg_type(&sub) =3D=3D DBUS_TYPE_STRING) { - const char *service =3D NULL; - - dbus_message_iter_get_basic(&sub, &service); - dbus_message_iter_next(&sub); - - if (STREQ(service, name)) { - ret =3D 0; - break; - } - } - - cleanup: - virDBusMessageUnref(reply); - return ret; -} - -/** - * virDBusIsServiceEnabled: - * @name: service name - * - * Returns 0 if service is available, -1 on fatal error, or -2 if service = is not available - */ -int virDBusIsServiceEnabled(const char *name) -{ - int ret =3D virDBusIsServiceInList("ListActivatableNames", name); - - VIR_DEBUG("Service %s is %s", name, ret ? "unavailable" : "available"); - - return ret; -} - -/** - * virDBusIsServiceRegistered - * @name: service name - * - * Returns 0 if service is registered, -1 on fatal error, or -2 if service= is not registered - */ -int virDBusIsServiceRegistered(const char *name) -{ - int ret =3D virDBusIsServiceInList("ListNames", name); - - VIR_DEBUG("Service %s is %s", name, ret ? "not registered" : "register= ed"); - - return ret; -} - -void virDBusMessageUnref(DBusMessage *msg) -{ - if (msg) - dbus_message_unref(msg); -} - -#else /* ! WITH_DBUS */ -void virDBusSetSharedBus(bool shared G_GNUC_UNUSED) -{ - /* nothing */ -} - -DBusConnection *virDBusGetSystemBus(void) -{ - virReportError(VIR_ERR_INTERNAL_ERROR, - "%s", _("DBus support not compiled into this binary")); - return NULL; -} - - -bool -virDBusHasSystemBus(void) -{ - VIR_DEBUG("DBus support not compiled into this binary"); - return false; -} - -void virDBusCloseSystemBus(void) -{ - /* nothing */ -} - -DBusConnection *virDBusGetSessionBus(void) -{ - virReportError(VIR_ERR_INTERNAL_ERROR, - "%s", _("DBus support not compiled into this binary")); - return NULL; -} - -int virDBusCreateMethod(DBusMessage **call G_GNUC_UNUSED, - const char *destination G_GNUC_UNUSED, - const char *path G_GNUC_UNUSED, - const char *iface G_GNUC_UNUSED, - const char *member G_GNUC_UNUSED, - const char *types G_GNUC_UNUSED, ...) -{ - virReportError(VIR_ERR_INTERNAL_ERROR, - "%s", _("DBus support not compiled into this binary")); - return -1; -} - -int virDBusCreateMethodV(DBusMessage **call G_GNUC_UNUSED, - const char *destination G_GNUC_UNUSED, - const char *path G_GNUC_UNUSED, - const char *iface G_GNUC_UNUSED, - const char *member G_GNUC_UNUSED, - const char *types G_GNUC_UNUSED, - va_list args G_GNUC_UNUSED) -{ - virReportError(VIR_ERR_INTERNAL_ERROR, - "%s", _("DBus support not compiled into this binary")); - return -1; -} - -int virDBusCreateReplyV(DBusMessage **reply G_GNUC_UNUSED, - const char *types G_GNUC_UNUSED, - va_list args G_GNUC_UNUSED) -{ - virReportError(VIR_ERR_INTERNAL_ERROR, - "%s", _("DBus support not compiled into this binary")); - return -1; -} - -int virDBusCreateReply(DBusMessage **reply G_GNUC_UNUSED, - const char *types G_GNUC_UNUSED, ...) -{ - virReportError(VIR_ERR_INTERNAL_ERROR, - "%s", _("DBus support not compiled into this binary")); - return -1; -} - -int virDBusCallMethod(DBusConnection *conn G_GNUC_UNUSED, - DBusMessage **reply G_GNUC_UNUSED, - virErrorPtr error G_GNUC_UNUSED, - const char *destination G_GNUC_UNUSED, - const char *path G_GNUC_UNUSED, - const char *iface G_GNUC_UNUSED, - const char *member G_GNUC_UNUSED, - const char *types G_GNUC_UNUSED, ...) -{ - virReportError(VIR_ERR_INTERNAL_ERROR, - "%s", _("DBus support not compiled into this binary")); - return -1; -} - - -int virDBusMessageEncode(DBusMessage* msg G_GNUC_UNUSED, - const char *types G_GNUC_UNUSED, - ...) -{ - virReportError(VIR_ERR_INTERNAL_ERROR, - "%s", _("DBus support not compiled into this binary")); - return -1; -} - -int virDBusMessageDecode(DBusMessage* msg G_GNUC_UNUSED, - const char *types G_GNUC_UNUSED, - ...) -{ - virReportError(VIR_ERR_INTERNAL_ERROR, - "%s", _("DBus support not compiled into this binary")); - return -1; -} - -int virDBusIsServiceEnabled(const char *name G_GNUC_UNUSED) -{ - VIR_DEBUG("DBus support not compiled into this binary"); - return -2; -} - -int virDBusIsServiceRegistered(const char *name G_GNUC_UNUSED) -{ - VIR_DEBUG("DBus support not compiled into this binary"); - return -2; -} - -void virDBusMessageUnref(DBusMessage *msg G_GNUC_UNUSED) -{ - /* nothing */ -} -#endif /* ! WITH_DBUS */ - -bool virDBusErrorIsUnknownMethod(virErrorPtr err) -{ - return err->domain =3D=3D VIR_FROM_DBUS && - err->code =3D=3D VIR_ERR_DBUS_SERVICE && - err->level =3D=3D VIR_ERR_ERROR && - STREQ_NULLABLE("org.freedesktop.DBus.Error.UnknownMethod", - err->str1); -} diff --git a/src/util/virdbus.h b/src/util/virdbus.h deleted file mode 100644 index 083c074d59..0000000000 --- a/src/util/virdbus.h +++ /dev/null @@ -1,76 +0,0 @@ -/* - * virdbus.h: helper for using DBus - * - * Copyright (C) 2012-2013 Red Hat, Inc. - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library. If not, see - * . - * - */ - -#pragma once - -#ifdef WITH_DBUS -# undef interface /* Work around namespace pollution in mingw's rpc.h */ -# include -#else -# define DBusConnection void -# define DBusMessage void -#endif -#include "internal.h" - -#include - -void virDBusSetSharedBus(bool shared); - -DBusConnection *virDBusGetSystemBus(void); -bool virDBusHasSystemBus(void); -void virDBusCloseSystemBus(void); -DBusConnection *virDBusGetSessionBus(void); - -int virDBusCreateMethod(DBusMessage **call, - const char *destination, - const char *path, - const char *iface, - const char *member, - const char *types, ...); -int virDBusCreateMethodV(DBusMessage **call, - const char *destination, - const char *path, - const char *iface, - const char *member, - const char *types, - va_list args); -int virDBusCreateReply(DBusMessage **reply, - const char *types, ...); -int virDBusCreateReplyV(DBusMessage **reply, - const char *types, - va_list args); - -int virDBusCallMethod(DBusConnection *conn, - DBusMessage **reply, - virErrorPtr error, - const char *destination, - const char *path, - const char *iface, - const char *member, - const char *types, ...); -int virDBusMessageDecode(DBusMessage *msg, - const char *types, ...); -void virDBusMessageUnref(DBusMessage *msg); - -int virDBusIsServiceEnabled(const char *name); -int virDBusIsServiceRegistered(const char *name); - -bool virDBusErrorIsUnknownMethod(virErrorPtr err); diff --git a/src/util/virdbuspriv.h b/src/util/virdbuspriv.h deleted file mode 100644 index 5b4fe217f9..0000000000 --- a/src/util/virdbuspriv.h +++ /dev/null @@ -1,56 +0,0 @@ -/* - * virdbuspriv.h: internal APIs for testing DBus code - * - * Copyright (C) 2012-2014 Red Hat, Inc. - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library. If not, see - * . - * - */ - -#ifndef LIBVIRT_VIRDBUSPRIV_H_ALLOW -# error "virdbuspriv.h may only be included by virdbus.c or test suites" -#endif /* LIBVIRT_VIRDBUSPRIV_H_ALLOW */ - -#pragma once - -#include "virdbus.h" - -#if defined(WITH_DBUS) && !WITH_DBUSBASICVALUE -/* Copied (and simplified) from dbus 1.6.12, for use with older dbus heade= rs */ -typedef union -{ - dbus_int16_t i16; /**< as int16 */ - dbus_uint16_t u16; /**< as int16 */ - dbus_int32_t i32; /**< as int32 */ - dbus_uint32_t u32; /**< as int32 */ - dbus_bool_t bool_val; /**< as boolean */ - dbus_int64_t i64; /**< as int64 */ - dbus_uint64_t u64; /**< as int64 */ - double dbl; /**< as double */ - unsigned char byt; /**< as byte */ -} DBusBasicValue; -#endif - -int virDBusMessageEncodeArgs(DBusMessage* msg, - const char *types, - va_list args); - -int virDBusMessageDecodeArgs(DBusMessage* msg, - const char *types, - va_list args); - -int virDBusMessageEncode(DBusMessage* msg, - const char *types, - ...); diff --git a/tests/meson.build b/tests/meson.build index 356286839a..f4fbb25e66 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -86,12 +86,6 @@ if conf.has('WITH_BHYVE') ] endif =20 -if conf.has('WITH_DBUS') - mock_libs +=3D [ - { 'name': 'virdbusmock', 'deps': [ dbus_dep ] }, - ] -endif - if conf.has('WITH_LIBXL') mock_libs +=3D [ { 'name': 'xlmock', 'sources': [ 'libxlmock.c' ], 'deps': [ libxl_dep = ] }, @@ -360,13 +354,6 @@ if conf.has('WITH_BHYVE') ] endif =20 -if conf.has('WITH_DBUS') - tests +=3D [ - { 'name': 'virdbustest', 'deps': [ dbus_dep ] }, - ] - -endif - if conf.has('WITH_ESX') tests +=3D [ { 'name': 'esxutilstest', 'include': [ esx_inc_dir ] }, diff --git a/tests/virdbusmock.c b/tests/virdbusmock.c deleted file mode 100644 index 0673f7292a..0000000000 --- a/tests/virdbusmock.c +++ /dev/null @@ -1,62 +0,0 @@ -/* - * virdbusmock.c: mocking of dbus message send/reply - * - * Copyright (C) 2013-2014 Red Hat, Inc. - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library. If not, see - * . - */ - -#include - -#if defined(WITH_DBUS) && !defined(WIN32) -# include "virmock.h" -# include - -VIR_MOCK_STUB_VOID_ARGS(dbus_connection_set_change_sigpipe, - dbus_bool_t, will_modify_sigpipe) - - -VIR_MOCK_STUB_RET_ARGS(dbus_bus_get, - DBusConnection *, (DBusConnection *)0x1, - DBusBusType, type, - DBusError *, error) - -VIR_MOCK_STUB_VOID_ARGS(dbus_connection_set_exit_on_disconnect, - DBusConnection *, connection, - dbus_bool_t, exit_on_disconnect) - -VIR_MOCK_STUB_RET_ARGS(dbus_connection_set_watch_functions, - dbus_bool_t, 1, - DBusConnection *, connection, - DBusAddWatchFunction, add_function, - DBusRemoveWatchFunction, remove_function, - DBusWatchToggledFunction, toggled_function, - void *, data, - DBusFreeFunction, free_data_function) - -VIR_MOCK_STUB_RET_ARGS(dbus_message_set_reply_serial, - dbus_bool_t, 1, - DBusMessage *, message, - dbus_uint32_t, serial) - - -VIR_MOCK_LINK_RET_ARGS(dbus_connection_send_with_reply_and_block, - DBusMessage *, - DBusConnection *, connection, - DBusMessage *, message, - int, timeout_milliseconds, - DBusError *, error) - -#endif /* WITH_DBUS && !WIN32 */ diff --git a/tests/virdbustest.c b/tests/virdbustest.c deleted file mode 100644 index f9c2fb9587..0000000000 --- a/tests/virdbustest.c +++ /dev/null @@ -1,728 +0,0 @@ -/* - * Copyright (C) 2013, 2014 Red Hat, Inc. - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library. If not, see - * . - */ - -#include - -#define LIBVIRT_VIRDBUSPRIV_H_ALLOW -#include "virdbuspriv.h" -#include "virlog.h" -#include "testutils.h" - -VIR_LOG_INIT("tests.dbustest"); - -#define VERIFY(typname, valorig, valnew, fmt) \ - do { \ - VIR_DEBUG("Compare " typname " '" fmt "' to '" \ - fmt "'", valorig, valnew); \ - if (valorig !=3D valnew) { \ - fprintf(stderr, "Failed to round-trip " typname " '" \ - fmt "' to '" fmt "'\n", valorig, valnew); \ - goto cleanup; \ - } \ - } while (0) - -#define VERIFY_STR(typname, valorig, valnew, fmt) \ - do { \ - VIR_DEBUG("Compare " typname " '" fmt "' to '" \ - fmt "'", valorig, valnew); \ - if (STRNEQ(valorig, valnew)) { \ - fprintf(stderr, "Failed to round-trip " typname " '" \ - fmt "' to '" fmt "'\n", valorig, valnew); \ - goto cleanup; \ - } \ - } while (0) - -static int testMessageSimple(const void *args G_GNUC_UNUSED) -{ - DBusMessage *msg =3D NULL; - int ret =3D -1; - unsigned char in_byte =3D 200, out_byte =3D 0; - bool in_bool =3D true, out_bool =3D false; - short in_int16 =3D 0xfefe, out_int16 =3D 0; - unsigned short in_uint16 =3D 32000, out_uint16 =3D 0; - int in_int32 =3D 100000000, out_int32 =3D 0; - unsigned int in_uint32 =3D 200000000, out_uint32 =3D 0; - long long in_int64 =3D 1000000000000LL, out_int64 =3D 0; - unsigned long long in_uint64 =3D 2000000000000LL, out_uint64 =3D 0; - double in_double =3D 3.14159265359, out_double =3D 0; - const char *in_string =3D "Hello World"; - char *out_string =3D NULL; - const char *in_objectpath =3D "/org/libvirt/test"; - char *out_objectpath =3D NULL; - const char *in_signature =3D "ybnqiuxtdsog"; - char *out_signature =3D NULL; - - if (!(msg =3D dbus_message_new_method_call("org.libvirt.test", - "/org/libvirt/test", - "org.libvirt.test.astrochicke= n", - "cluck"))) { - VIR_DEBUG("Failed to allocate method call"); - goto cleanup; - } - - if (virDBusMessageEncode(msg, - "ybnqiuxtdsog", - in_byte, in_bool, - in_int16, in_uint16, - in_int32, in_uint32, - in_int64, in_uint64, - in_double, in_string, - in_objectpath, in_signature) < 0) { - VIR_DEBUG("Failed to encode arguments"); - goto cleanup; - } - - if (virDBusMessageDecode(msg, - "ybnqiuxtdsog", - &out_byte, &out_bool, - &out_int16, &out_uint16, - &out_int32, &out_uint32, - &out_int64, &out_uint64, - &out_double, &out_string, - &out_objectpath, &out_signature) < 0) { - VIR_DEBUG("Failed to decode arguments"); - goto cleanup; - } - - VERIFY("byte", in_byte, out_byte, "%d"); - VERIFY("bool", in_bool, out_bool, "%d"); - VERIFY("int16", in_int16, out_int16, "%d"); - VERIFY("uint16", in_int16, out_int16, "%d"); - VERIFY("int32", in_int32, out_int32, "%d"); - VERIFY("uint32", in_int32, out_int32, "%d"); - VERIFY("int64", in_int64, out_int64, "%lld"); - VERIFY("uint64", in_int64, out_int64, "%lld"); - VERIFY("double", in_double, out_double, "%lf"); - VERIFY_STR("string", in_string, out_string, "%s"); - VERIFY_STR("objectpath", in_objectpath, out_objectpath, "%s"); - VERIFY_STR("signature", in_signature, out_signature, "%s"); - - ret =3D 0; - - cleanup: - VIR_FREE(out_string); - VIR_FREE(out_signature); - VIR_FREE(out_objectpath); - virDBusMessageUnref(msg); - return ret; -} - - -static int testMessageVariant(const void *args G_GNUC_UNUSED) -{ - DBusMessage *msg =3D NULL; - int ret =3D -1; - const char *in_str1 =3D "Hello"; - int in_int32 =3D 100000000, out_int32 =3D 0; - const char *in_str2 =3D "World"; - char *out_str1 =3D NULL, *out_str2 =3D NULL; - - if (!(msg =3D dbus_message_new_method_call("org.libvirt.test", - "/org/libvirt/test", - "org.libvirt.test.astrochicke= n", - "cluck"))) { - VIR_DEBUG("Failed to allocate method call"); - goto cleanup; - } - - if (virDBusMessageEncode(msg, - "svs", - in_str1, - "i", in_int32, - in_str2) < 0) { - VIR_DEBUG("Failed to encode arguments"); - goto cleanup; - } - - if (virDBusMessageDecode(msg, - "svs", - &out_str1, - "i", &out_int32, - &out_str2) < 0) { - VIR_DEBUG("Failed to decode arguments"); - goto cleanup; - } - - - VERIFY_STR("str1", in_str1, out_str1, "%s"); - VERIFY("int32", in_int32, out_int32, "%d"); - VERIFY_STR("str2", in_str2, out_str2, "%s"); - - ret =3D 0; - - cleanup: - VIR_FREE(out_str1); - VIR_FREE(out_str2); - virDBusMessageUnref(msg); - return ret; -} - -static int testMessageArray(const void *args G_GNUC_UNUSED) -{ - DBusMessage *msg =3D NULL; - int ret =3D -1; - const char *in_str1 =3D "Hello"; - int in_int32a =3D 1000000000, out_int32a =3D 0; - int in_int32b =3D 2000000000, out_int32b =3D 0; - int in_int32c =3D -2000000000, out_int32c =3D 0; - bool in_bool[] =3D { true, false, true }, out_bool[] =3D { false, true= , false}; - const char *in_str2 =3D "World"; - char *out_str1 =3D NULL, *out_str2 =3D NULL; - - if (!(msg =3D dbus_message_new_method_call("org.libvirt.test", - "/org/libvirt/test", - "org.libvirt.test.astrochicke= n", - "cluck"))) { - VIR_DEBUG("Failed to allocate method call"); - goto cleanup; - } - - if (virDBusMessageEncode(msg, - "saiabs", - in_str1, - 3, in_int32a, in_int32b, in_int32c, - 3, in_bool[0], in_bool[1], in_bool[2], - in_str2) < 0) { - VIR_DEBUG("Failed to encode arguments"); - goto cleanup; - } - - if (virDBusMessageDecode(msg, - "saiabs", - &out_str1, - 3, &out_int32a, &out_int32b, &out_int32c, - 3, &out_bool[0], &out_bool[1], &out_bool[2], - &out_str2) < 0) { - VIR_DEBUG("Failed to decode arguments"); - goto cleanup; - } - - - VERIFY_STR("str1", in_str1, out_str1, "%s"); - VERIFY("int32a", in_int32a, out_int32a, "%d"); - VERIFY("int32b", in_int32b, out_int32b, "%d"); - VERIFY("int32c", in_int32c, out_int32c, "%d"); - VERIFY("bool[0]", in_bool[0], out_bool[0], "%d"); - VERIFY("bool[1]", in_bool[1], out_bool[1], "%d"); - VERIFY("bool[2]", in_bool[2], out_bool[2], "%d"); - VERIFY_STR("str2", in_str2, out_str2, "%s"); - - ret =3D 0; - - cleanup: - VIR_FREE(out_str1); - VIR_FREE(out_str2); - virDBusMessageUnref(msg); - return ret; -} - -static int testMessageEmptyArrayRef(const void *args G_GNUC_UNUSED) -{ - DBusMessage *msg =3D NULL; - int ret =3D -1; - const char *in_strv1[] =3D {}; - size_t out_nstrv1; - char **out_strv1 =3D NULL; - - if (!(msg =3D dbus_message_new_method_call("org.libvirt.test", - "/org/libvirt/test", - "org.libvirt.test.astrochicke= n", - "cluck"))) { - VIR_DEBUG("Failed to allocate method call"); - goto cleanup; - } - - if (virDBusMessageEncode(msg, - "a&s", - 0, in_strv1) < 0) { - VIR_DEBUG("Failed to encode arguments"); - goto cleanup; - } - - if (virDBusMessageDecode(msg, - "a&s", - &out_nstrv1, &out_strv1) < 0) { - VIR_DEBUG("Failed to decode arguments"); - goto cleanup; - } - - - if (out_nstrv1 !=3D 0) { - fprintf(stderr, "Expected 0 string, but got %zu\n", - out_nstrv1); - goto cleanup; - } - - ret =3D 0; - - cleanup: - virDBusMessageUnref(msg); - return ret; -} - -static int testMessageSingleArrayRef(const void *args G_GNUC_UNUSED) -{ - DBusMessage *msg =3D NULL; - int ret =3D -1; - const char *in_strv1[] =3D { - "Fishfood", - }; - char **out_strv1 =3D NULL; - size_t out_nstrv1 =3D 0; - - if (!(msg =3D dbus_message_new_method_call("org.libvirt.test", - "/org/libvirt/test", - "org.libvirt.test.astrochicke= n", - "cluck"))) { - VIR_DEBUG("Failed to allocate method call"); - goto cleanup; - } - - if (virDBusMessageEncode(msg, - "a&s", - 1, in_strv1) < 0) { - VIR_DEBUG("Failed to encode arguments"); - goto cleanup; - } - - if (virDBusMessageDecode(msg, - "a&s", - &out_nstrv1, &out_strv1) < 0) { - VIR_DEBUG("Failed to decode arguments"); - goto cleanup; - } - - - if (out_nstrv1 !=3D 1) { - fprintf(stderr, "Expected 1 string, but got %zu\n", - out_nstrv1); - goto cleanup; - } - VERIFY_STR("strv1[0]", in_strv1[0], out_strv1[0], "%s"); - - ret =3D 0; - - cleanup: - if (out_strv1) - VIR_FREE(out_strv1[0]); - VIR_FREE(out_strv1); - virDBusMessageUnref(msg); - return ret; -} - -static int testMessageArrayRef(const void *args G_GNUC_UNUSED) -{ - DBusMessage *msg =3D NULL; - int ret =3D -1; - const char *in_str1 =3D "Hello"; - int in_int32[] =3D { - 100000000, 2000000000, -2000000000 - }; - bool in_bool[] =3D { true, false, true }; - const char *in_strv1[] =3D { - "Fishfood", - }; - const char *in_strv2[] =3D { - "Hello", "World", - }; - int *out_int32 =3D NULL; - size_t out_nint32 =3D 0; - bool *out_bool =3D NULL; - size_t out_nbool =3D 0; - char **out_strv1 =3D NULL; - char **out_strv2 =3D NULL; - size_t out_nstrv1 =3D 0; - size_t out_nstrv2 =3D 0; - const char *in_str2 =3D "World"; - char *out_str1 =3D NULL, *out_str2 =3D NULL; - size_t i; - - if (!(msg =3D dbus_message_new_method_call("org.libvirt.test", - "/org/libvirt/test", - "org.libvirt.test.astrochicke= n", - "cluck"))) { - VIR_DEBUG("Failed to allocate method call"); - goto cleanup; - } - - if (virDBusMessageEncode(msg, - "sa&sa&ia&ba&ss", - in_str1, - 1, in_strv1, - 3, in_int32, - 3, in_bool, - 2, in_strv2, - in_str2) < 0) { - VIR_DEBUG("Failed to encode arguments"); - goto cleanup; - } - - if (virDBusMessageDecode(msg, - "sa&sa&ia&ba&ss", - &out_str1, - &out_nstrv1, &out_strv1, - &out_nint32, &out_int32, - &out_nbool, &out_bool, - &out_nstrv2, &out_strv2, - &out_str2) < 0) { - VIR_DEBUG("Failed to decode arguments"); - goto cleanup; - } - - - VERIFY_STR("str1", in_str1, out_str1, "%s"); - if (out_nstrv1 !=3D 1) { - fprintf(stderr, "Expected 1 string, but got %zu\n", - out_nstrv1); - goto cleanup; - } - VERIFY_STR("strv1[0]", in_strv1[0], out_strv1[0], "%s"); - - if (out_nint32 !=3D 3) { - fprintf(stderr, "Expected 3 integers, but got %zu\n", - out_nint32); - goto cleanup; - } - VERIFY("int32a", in_int32[0], out_int32[0], "%d"); - VERIFY("int32b", in_int32[1], out_int32[1], "%d"); - VERIFY("int32c", in_int32[2], out_int32[2], "%d"); - - if (out_nbool !=3D 3) { - fprintf(stderr, "Expected 3 bools, but got %zu\n", - out_nbool); - goto cleanup; - } - VERIFY("bool[0]", in_bool[0], out_bool[0], "%d"); - VERIFY("bool[1]", in_bool[1], out_bool[1], "%d"); - VERIFY("bool[2]", in_bool[2], out_bool[2], "%d"); - - if (out_nstrv2 !=3D 2) { - fprintf(stderr, "Expected 2 strings, but got %zu\n", - out_nstrv2); - goto cleanup; - } - VERIFY_STR("strv2[0]", in_strv2[0], out_strv2[0], "%s"); - VERIFY_STR("strv2[1]", in_strv2[1], out_strv2[1], "%s"); - - VERIFY_STR("str2", in_str2, out_str2, "%s"); - - ret =3D 0; - - cleanup: - VIR_FREE(out_int32); - VIR_FREE(out_bool); - VIR_FREE(out_str1); - VIR_FREE(out_str2); - for (i =3D 0; i < out_nstrv1; i++) - VIR_FREE(out_strv1[i]); - VIR_FREE(out_strv1); - for (i =3D 0; i < out_nstrv2; i++) - VIR_FREE(out_strv2[i]); - VIR_FREE(out_strv2); - virDBusMessageUnref(msg); - return ret; -} - -static int testMessageStruct(const void *args G_GNUC_UNUSED) -{ - DBusMessage *msg =3D NULL; - int ret =3D -1; - unsigned char in_byte =3D 200, out_byte =3D 0; - bool in_bool =3D true, out_bool =3D false; - short in_int16 =3D 12000, out_int16 =3D 0; - unsigned short in_uint16 =3D 32000, out_uint16 =3D 0; - int in_int32 =3D 100000000, out_int32 =3D 0; - unsigned int in_uint32 =3D 200000000, out_uint32 =3D 0; - long long in_int64 =3D -1000000000000LL, out_int64 =3D 0; - unsigned long long in_uint64 =3D 2000000000000LL, out_uint64 =3D 0; - double in_double =3D 3.14159265359, out_double =3D 0; - const char *in_string =3D "Hello World"; - char *out_string =3D NULL; - const char *in_objectpath =3D "/org/libvirt/test"; - char *out_objectpath =3D NULL; - const char *in_signature =3D "ybnqiuxtdsog"; - char *out_signature =3D NULL; - - if (!(msg =3D dbus_message_new_method_call("org.libvirt.test", - "/org/libvirt/test", - "org.libvirt.test.astrochicke= n", - "cluck"))) { - VIR_DEBUG("Failed to allocate method call"); - goto cleanup; - } - - if (virDBusMessageEncode(msg, - "ybn(qiuxtds)og", - in_byte, in_bool, - in_int16, in_uint16, - in_int32, in_uint32, - in_int64, in_uint64, - in_double, in_string, - in_objectpath, in_signature) < 0) { - VIR_DEBUG("Failed to encode arguments"); - goto cleanup; - } - - if (virDBusMessageDecode(msg, - "ybn(qiuxtds)og", - &out_byte, &out_bool, - &out_int16, &out_uint16, - &out_int32, &out_uint32, - &out_int64, &out_uint64, - &out_double, &out_string, - &out_objectpath, &out_signature) < 0) { - VIR_DEBUG("Failed to decode arguments"); - goto cleanup; - } - - VERIFY("byte", in_byte, out_byte, "%d"); - VERIFY("bool", in_bool, out_bool, "%d"); - VERIFY("int16", in_int16, out_int16, "%d"); - VERIFY("uint16", in_int16, out_int16, "%d"); - VERIFY("int32", in_int32, out_int32, "%d"); - VERIFY("uint32", in_int32, out_int32, "%d"); - VERIFY("int64", in_int64, out_int64, "%lld"); - VERIFY("uint64", in_int64, out_int64, "%lld"); - VERIFY("double", in_double, out_double, "%lf"); - VERIFY_STR("string", in_string, out_string, "%s"); - VERIFY_STR("objectpath", in_objectpath, out_objectpath, "%s"); - VERIFY_STR("signature", in_signature, out_signature, "%s"); - - ret =3D 0; - - cleanup: - VIR_FREE(out_string); - VIR_FREE(out_signature); - VIR_FREE(out_objectpath); - virDBusMessageUnref(msg); - return ret; -} - - -static int testMessageDict(const void *args G_GNUC_UNUSED) -{ - DBusMessage *msg =3D NULL; - int ret =3D -1; - const char *in_str1 =3D "Hello"; - int in_int32a =3D 100000000, out_int32a =3D 0; - const char *in_key1 =3D "turnover"; - int in_int32b =3D 200000000, out_int32b =3D 0; - const char *in_key2 =3D "revenue"; - int in_int32c =3D 300000000, out_int32c =3D 0; - const char *in_key3 =3D "debt"; - const char *in_str2 =3D "World"; - char *out_str1 =3D NULL, *out_str2 =3D NULL; - char *out_key1 =3D NULL, *out_key2 =3D NULL, *out_key3 =3D NULL; - - if (!(msg =3D dbus_message_new_method_call("org.libvirt.test", - "/org/libvirt/test", - "org.libvirt.test.astrochicke= n", - "cluck"))) { - VIR_DEBUG("Failed to allocate method call"); - goto cleanup; - } - - if (virDBusMessageEncode(msg, - "(sa{si}s)", - in_str1, - 3, - in_key1, in_int32a, - in_key2, in_int32b, - in_key3, in_int32c, - in_str2) < 0) { - VIR_DEBUG("Failed to encode arguments"); - goto cleanup; - } - - if (virDBusMessageDecode(msg, - "(sa{si}s)", - &out_str1, - 3, - &out_key1, &out_int32a, - &out_key2, &out_int32b, - &out_key3, &out_int32c, - &out_str2) < 0) { - VIR_DEBUG("Failed to decode arguments"); - goto cleanup; - } - - - VERIFY_STR("str1", in_str1, out_str1, "%s"); - VERIFY("int32a", in_int32a, out_int32a, "%d"); - VERIFY("int32b", in_int32b, out_int32b, "%d"); - VERIFY("int32c", in_int32c, out_int32c, "%d"); - VERIFY_STR("key1", in_key1, out_key1, "%s"); - VERIFY_STR("key1", in_key2, out_key2, "%s"); - VERIFY_STR("key1", in_key3, out_key3, "%s"); - VERIFY_STR("str2", in_str2, out_str2, "%s"); - - ret =3D 0; - - cleanup: - VIR_FREE(out_str1); - VIR_FREE(out_str2); - VIR_FREE(out_key1); - VIR_FREE(out_key2); - VIR_FREE(out_key3); - virDBusMessageUnref(msg); - return ret; -} - -static int testMessageDictRef(const void *args G_GNUC_UNUSED) -{ - DBusMessage *msg =3D NULL; - int ret =3D -1; - const char *in_str1 =3D "Hello"; - const char *in_strv1[] =3D { - "Fruit1", "Apple", - "Fruit2", "Orange", - "Fruit3", "Kiwi", - }; - const char *in_str2 =3D "World"; - char *out_str1 =3D NULL; - size_t out_nint32 =3D 0; - char **out_strv1 =3D NULL; - char *out_str2 =3D NULL; - - if (!(msg =3D dbus_message_new_method_call("org.libvirt.test", - "/org/libvirt/test", - "org.libvirt.test.astrochicke= n", - "cluck"))) { - VIR_DEBUG("Failed to allocate method call"); - goto cleanup; - } - - if (virDBusMessageEncode(msg, - "(sa&{ss}s)", - in_str1, - 3, in_strv1, - in_str2) < 0) { - VIR_DEBUG("Failed to encode arguments"); - goto cleanup; - } - - if (virDBusMessageDecode(msg, - "(sa&{ss}s)", - &out_str1, - &out_nint32, - &out_strv1, - &out_str2) < 0) { - VIR_DEBUG("Failed to decode arguments: '%s'", virGetLastErrorMessa= ge()); - goto cleanup; - } - - - VERIFY_STR("str1", in_str1, out_str1, "%s"); - VERIFY_STR("strv1[0]", in_strv1[0], out_strv1[0], "%s"); - VERIFY_STR("strv1[1]", in_strv1[1], out_strv1[1], "%s"); - VERIFY_STR("strv1[2]", in_strv1[2], out_strv1[2], "%s"); - VERIFY_STR("strv1[3]", in_strv1[3], out_strv1[3], "%s"); - VERIFY_STR("strv1[4]", in_strv1[4], out_strv1[4], "%s"); - VERIFY_STR("strv1[5]", in_strv1[5], out_strv1[5], "%s"); - VERIFY_STR("str2", in_str2, out_str2, "%s"); - - ret =3D 0; - - cleanup: - VIR_FREE(out_str1); - VIR_FREE(out_str2); - if (out_strv1) { - VIR_FREE(out_strv1[0]); - VIR_FREE(out_strv1[1]); - VIR_FREE(out_strv1[2]); - VIR_FREE(out_strv1[3]); - VIR_FREE(out_strv1[4]); - VIR_FREE(out_strv1[5]); - } - VIR_FREE(out_strv1); - virDBusMessageUnref(msg); - return ret; -} - -static int testMessageEmptyDictRef(const void *args G_GNUC_UNUSED) -{ - DBusMessage *msg =3D NULL; - int ret =3D -1; - const char *in_strv1[] =3D {}; - size_t out_nint32 =3D 0; - char **out_strv1 =3D NULL; - - if (!(msg =3D dbus_message_new_method_call("org.libvirt.test", - "/org/libvirt/test", - "org.libvirt.test.astrochicke= n", - "cluck"))) { - VIR_DEBUG("Failed to allocate method call"); - goto cleanup; - } - - if (virDBusMessageEncode(msg, - "a&{ss}", - 0, in_strv1) < 0) { - VIR_DEBUG("Failed to encode arguments"); - goto cleanup; - } - - if (virDBusMessageDecode(msg, - "a&{ss}", - &out_nint32, - &out_strv1) < 0) { - VIR_DEBUG("Failed to decode arguments: '%s'", virGetLastErrorMessa= ge()); - goto cleanup; - } - - if (out_nint32 !=3D 0) { - fprintf(stderr, "Unexpected dict entries\n"); - goto cleanup; - } - - ret =3D 0; - - cleanup: - virDBusMessageUnref(msg); - return ret; -} - -static int -mymain(void) -{ - int ret =3D 0; - - if (virTestRun("Test message simple ", testMessageSimple, NULL) < 0) - ret =3D -1; - if (virTestRun("Test message variant ", testMessageVariant, NULL) < 0) - ret =3D -1; - if (virTestRun("Test message array ", testMessageArray, NULL) < 0) - ret =3D -1; - if (virTestRun("Test message array empty ref ", testMessageEmptyArrayR= ef, NULL) < 0) - ret =3D -1; - if (virTestRun("Test message array single ref ", testMessageSingleArra= yRef, NULL) < 0) - ret =3D -1; - if (virTestRun("Test message array ref ", testMessageArrayRef, NULL) <= 0) - ret =3D -1; - if (virTestRun("Test message struct ", testMessageStruct, NULL) < 0) - ret =3D -1; - if (virTestRun("Test message dict ", testMessageDict, NULL) < 0) - ret =3D -1; - if (virTestRun("Test message dict empty ref ", testMessageEmptyDictRef= , NULL) < 0) - ret =3D -1; - if (virTestRun("Test message dict ref ", testMessageDictRef, NULL) < 0) - ret =3D -1; - return ret =3D=3D 0 ? EXIT_SUCCESS : EXIT_FAILURE; -} - -VIR_TEST_MAIN(mymain) --=20 2.26.2