As the consensus in: https://www.redhat.com/archives/libvir-list/2012-July/msg01692.html, this patch is to destroy conf/virdomainlist.[ch], foldering the helpers into conf/domain_conf.[ch]. * src/Makefile.am: - Various indentions fixes incidentally - Add macro DATATYPES_SOURCES (datatypes.[ch]) - Link datatypes.[ch] for libvirt_lxc * src/conf/domain_conf.c: - Move all the stuffs from virdomainlist.c into it - Use virUnrefDomain and virUnrefDomainSnapshot instead of virDomainFree and virDomainSnapshotFree, which are defined in libvirt.c, and we don't want to link to it. * src/conf/domain_conf.h: - Move all the stuffs from virdomainlist.h into it - s/LIST_FILTER/LIST_DOMAINS_FILTER/ * src/libxl/libxl_driver.c: - s/LIST_FILTER/LIST_DOMAINS_FILTER/ - no (include "virdomainlist.h") * src/libxl/libxl_driver.c: Likewise * src/lxc/lxc_driver.c: Likewise * src/openvz/openvz_driver.c: Likewise * src/parallels/parallels_driver.c: Likewise * src/qemu/qemu_driver.c: Likewise * src/test/test_driver.c: Likewise * src/uml/uml_driver.c: Likewise * src/vbox/vbox_tmpl.c: Likewise * src/vmware/vmware_driver.c: Likewise * tools/virsh-domain-monitor.c: Likewise * tools/virsh.c: Likewise --- src/Makefile.am | 125 ++++++++++----------- src/conf/domain_conf.c | 189 ++++++++++++++++++++++++++++++++- src/conf/domain_conf.h | 56 ++++++++++ src/conf/virdomainlist.c | 222 -------------------------------------- src/conf/virdomainlist.h | 84 -------------- src/libxl/libxl_driver.c | 3 +- src/lxc/lxc_driver.c | 3 +- src/openvz/openvz_driver.c | 3 +- src/parallels/parallels_driver.c | 3 +- src/qemu/qemu_driver.c | 3 +- src/test/test_driver.c | 3 +- src/uml/uml_driver.c | 3 +- src/vbox/vbox_tmpl.c | 9 +- src/vmware/vmware_driver.c | 3 +- tools/virsh-domain-monitor.c | 14 ++-- tools/virsh.c | 1 - 16 files changed, 323 insertions(+), 401 deletions(-) delete mode 100644 src/conf/virdomainlist.c delete mode 100644 src/conf/virdomainlist.h diff --git a/src/Makefile.am b/src/Makefile.am index d4c198e..251771e 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -123,16 +123,17 @@ EXTRA_DIST += util/threads-pthread.c util/threads-win32.c # Internal generic driver infrastructure NODE_INFO_SOURCES = nodeinfo.h nodeinfo.c -DRIVER_SOURCES = \ - driver.c driver.h \ - internal.h \ - datatypes.c datatypes.h \ - fdstream.c fdstream.h \ - $(NODE_INFO_SOURCES) \ - libvirt.c libvirt_internal.h \ - locking/lock_manager.c locking/lock_manager.h \ - locking/lock_driver.h \ - locking/lock_driver_nop.h locking/lock_driver_nop.c \ +DATATYPES_SOURCES = datatypes.h datatypes.c +DRIVER_SOURCES = \ + driver.c driver.h \ + internal.h \ + $(DATATYPES_SOURCES) \ + fdstream.c fdstream.h \ + $(NODE_INFO_SOURCES) \ + libvirt.c libvirt_internal.h \ + locking/lock_manager.c locking/lock_manager.h \ + locking/lock_driver.h \ + locking/lock_driver_nop.h locking/lock_driver_nop.c \ locking/domain_lock.h locking/domain_lock.c LOCK_DRIVER_SANLOCK_SOURCES = \ @@ -195,10 +196,6 @@ CPU_CONF_SOURCES = \ CONSOLE_CONF_SOURCES = \ conf/virconsole.c conf/virconsole.h -# Domain listing helpers -DOMAIN_LIST_SOURCES = \ - conf/virdomainlist.c conf/virdomainlist.h - CONF_SOURCES = \ $(NETDEV_CONF_SOURCES) \ $(DOMAIN_CONF_SOURCES) \ @@ -211,8 +208,7 @@ CONF_SOURCES = \ $(INTERFACE_CONF_SOURCES) \ $(SECRET_CONF_SOURCES) \ $(CPU_CONF_SOURCES) \ - $(CONSOLE_CONF_SOURCES) \ - $(DOMAIN_LIST_SOURCES) + $(CONSOLE_CONF_SOURCES) # The remote RPC driver, covering domains, storage, networks, etc REMOTE_DRIVER_GENERATED = \ @@ -437,29 +433,29 @@ VBOX_DRIVER_EXTRA_DIST = \ vbox/vbox_MSCOMGlue.c vbox/vbox_MSCOMGlue.h \ vbox/vbox_XPCOMCGlue.c vbox/vbox_XPCOMCGlue.h -QEMU_DRIVER_SOURCES = \ - qemu/qemu_agent.c qemu/qemu_agent.h \ - qemu/qemu_capabilities.c qemu/qemu_capabilities.h\ - qemu/qemu_command.c qemu/qemu_command.h \ - qemu/qemu_domain.c qemu/qemu_domain.h \ - qemu/qemu_cgroup.c qemu/qemu_cgroup.h \ - qemu/qemu_hostdev.c qemu/qemu_hostdev.h \ - qemu/qemu_hotplug.c qemu/qemu_hotplug.h \ - qemu/qemu_conf.c qemu/qemu_conf.h \ - qemu/qemu_process.c qemu/qemu_process.h \ - qemu/qemu_migration.c qemu/qemu_migration.h \ - qemu/qemu_monitor.c qemu/qemu_monitor.h \ - qemu/qemu_monitor_text.c \ - qemu/qemu_monitor_text.h \ - qemu/qemu_monitor_json.c \ - qemu/qemu_monitor_json.h \ - qemu/qemu_driver.c qemu/qemu_driver.h \ - qemu/qemu_bridge_filter.c \ +QEMU_DRIVER_SOURCES = \ + qemu/qemu_agent.c qemu/qemu_agent.h \ + qemu/qemu_capabilities.c qemu/qemu_capabilities.h \ + qemu/qemu_command.c qemu/qemu_command.h \ + qemu/qemu_domain.c qemu/qemu_domain.h \ + qemu/qemu_cgroup.c qemu/qemu_cgroup.h \ + qemu/qemu_hostdev.c qemu/qemu_hostdev.h \ + qemu/qemu_hotplug.c qemu/qemu_hotplug.h \ + qemu/qemu_conf.c qemu/qemu_conf.h \ + qemu/qemu_process.c qemu/qemu_process.h \ + qemu/qemu_migration.c qemu/qemu_migration.h \ + qemu/qemu_monitor.c qemu/qemu_monitor.h \ + qemu/qemu_monitor_text.c \ + qemu/qemu_monitor_text.h \ + qemu/qemu_monitor_json.c \ + qemu/qemu_monitor_json.h \ + qemu/qemu_driver.c qemu/qemu_driver.h \ + qemu/qemu_bridge_filter.c \ qemu/qemu_bridge_filter.h -XENAPI_DRIVER_SOURCES = \ +XENAPI_DRIVER_SOURCES = \ xenapi/xenapi_driver.c xenapi/xenapi_driver.h \ - xenapi/xenapi_driver_private.h \ + xenapi/xenapi_driver_private.h \ xenapi/xenapi_utils.c xenapi/xenapi_utils.h LIBXL_DRIVER_SOURCES = \ @@ -470,18 +466,18 @@ UML_DRIVER_SOURCES = \ uml/uml_conf.c uml/uml_conf.h \ uml/uml_driver.c uml/uml_driver.h -ESX_DRIVER_SOURCES = \ - esx/esx_private.h \ - esx/esx_driver.c esx/esx_driver.h \ - esx/esx_interface_driver.c esx/esx_interface_driver.h \ - esx/esx_network_driver.c esx/esx_network_driver.h \ - esx/esx_storage_driver.c esx/esx_storage_driver.h \ - esx/esx_device_monitor.c esx/esx_device_monitor.h \ +ESX_DRIVER_SOURCES = \ + esx/esx_private.h \ + esx/esx_driver.c esx/esx_driver.h \ + esx/esx_interface_driver.c esx/esx_interface_driver.h \ + esx/esx_network_driver.c esx/esx_network_driver.h \ + esx/esx_storage_driver.c esx/esx_storage_driver.h \ + esx/esx_device_monitor.c esx/esx_device_monitor.h \ esx/esx_secret_driver.c esx/esx_secret_driver.h \ - esx/esx_nwfilter_driver.c esx/esx_nwfilter_driver.h \ - esx/esx_util.c esx/esx_util.h \ - esx/esx_vi.c esx/esx_vi.h \ - esx/esx_vi_methods.c esx/esx_vi_methods.h \ + esx/esx_nwfilter_driver.c esx/esx_nwfilter_driver.h \ + esx/esx_util.c esx/esx_util.h \ + esx/esx_vi.c esx/esx_vi.h \ + esx/esx_vi_methods.c esx/esx_vi_methods.h \ esx/esx_vi_types.c esx/esx_vi_types.h ESX_DRIVER_GENERATED = \ @@ -497,13 +493,13 @@ ESX_DRIVER_GENERATED = \ esx/esx_vi.generated.c \ esx/esx_vi.generated.h -ESX_DRIVER_EXTRA_DIST = \ +ESX_DRIVER_EXTRA_DIST = \ esx/README \ esx/esx_vi_generator.input \ esx/esx_vi_generator.py \ $(ESX_DRIVER_GENERATED) -HYPERV_DRIVER_SOURCES = \ +HYPERV_DRIVER_SOURCES = \ hyperv/hyperv_private.h \ hyperv/hyperv_driver.c hyperv/hyperv_driver.h \ hyperv/hyperv_interface_driver.c hyperv/hyperv_interface_driver.h \ @@ -546,15 +542,15 @@ SECRET_DRIVER_SOURCES = \ secret/secret_driver.h secret/secret_driver.c # Storage backend specific impls -STORAGE_DRIVER_SOURCES = \ - storage/storage_driver.h storage/storage_driver.c \ +STORAGE_DRIVER_SOURCES = \ + storage/storage_driver.h storage/storage_driver.c \ storage/storage_backend.h storage/storage_backend.c STORAGE_DRIVER_FS_SOURCES = \ storage/storage_backend_fs.h storage/storage_backend_fs.c STORAGE_DRIVER_LVM_SOURCES = \ - storage/storage_backend_logical.h \ + storage/storage_backend_logical.h \ storage/storage_backend_logical.c STORAGE_DRIVER_ISCSI_SOURCES = \ @@ -582,7 +578,7 @@ UTIL_IO_HELPER_SOURCES = \ util/iohelper.c # Network filters -NWFILTER_DRIVER_SOURCES = \ +NWFILTER_DRIVER_SOURCES = \ nwfilter/nwfilter_driver.h nwfilter/nwfilter_driver.c \ nwfilter/nwfilter_gentech_driver.c \ nwfilter/nwfilter_gentech_driver.h \ @@ -595,11 +591,11 @@ NWFILTER_DRIVER_SOURCES = \ # Security framework and drivers for various models -SECURITY_DRIVER_SOURCES = \ - security/security_driver.h security/security_driver.c \ - security/security_nop.h security/security_nop.c \ - security/security_stack.h security/security_stack.c \ - security/security_dac.h security/security_dac.c \ +SECURITY_DRIVER_SOURCES = \ + security/security_driver.h security/security_driver.c \ + security/security_nop.h security/security_nop.c \ + security/security_stack.h security/security_stack.c \ + security/security_dac.h security/security_dac.c \ security/security_manager.h security/security_manager.c SECURITY_DRIVER_SELINUX_SOURCES = \ @@ -610,8 +606,8 @@ SECURITY_DRIVER_APPARMOR_SOURCES = \ NODE_DEVICE_DRIVER_SOURCES = \ - node_device/node_device_driver.c \ - node_device/node_device_driver.h \ + node_device/node_device_driver.c \ + node_device/node_device_driver.h \ node_device/node_device_linux_sysfs.c NODE_DEVICE_DRIVER_HAL_SOURCES = \ @@ -1004,8 +1000,6 @@ endif EXTRA_DIST += network/default.xml - - if WITH_NETCF if WITH_DRIVER_MODULES mod_LTLIBRARIES += libvirt_driver_interface.la @@ -1189,12 +1183,12 @@ EXTRA_DIST += \ $(PHYP_DRIVER_SOURCES) \ $(VBOX_DRIVER_SOURCES) \ $(XENAPI_DRIVER_SOURCES) \ - $(LIBXL_DRIVER_SOURCES) \ + $(LIBXL_DRIVER_SOURCES) \ $(ESX_DRIVER_SOURCES) \ $(ESX_DRIVER_EXTRA_DIST) \ $(HYPERV_DRIVER_SOURCES) \ $(HYPERV_DRIVER_EXTRA_DIST) \ - $(PARALLELS_DRIVER_SOURCES) \ + $(PARALLELS_DRIVER_SOURCES) \ $(NETWORK_DRIVER_SOURCES) \ $(INTERFACE_DRIVER_SOURCES) \ $(STORAGE_DRIVER_SOURCES) \ @@ -1599,7 +1593,8 @@ libexec_PROGRAMS += libvirt_lxc libvirt_lxc_SOURCES = \ $(LXC_CONTROLLER_SOURCES) \ - $(NODE_INFO_SOURCES) + $(NODE_INFO_SOURCES) \ + $(DATATYPES_SOURCES) libvirt_lxc_LDFLAGS = $(WARN_CFLAGS) $(AM_LDFLAGS) libvirt_lxc_LDADD = \ $(NUMACTL_LIBS) \ diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 58603a3..e107da8 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -51,7 +51,6 @@ #include "secret_conf.h" #include "netdev_vport_profile_conf.h" #include "netdev_bandwidth_conf.h" -#include "virdomainlist.h" #define VIR_FROM_THIS VIR_FROM_DOMAIN @@ -15402,3 +15401,191 @@ cleanup: VIR_FREE(xmlStr); return ret; } + +struct virDomainListData { + virConnectPtr conn; + virDomainPtr *domains; + unsigned int flags; + int ndomains; + bool error; +}; + +#define MATCH(FLAG) (data->flags & (FLAG)) +static void +virDomainListPopulate(void *payload, + const void *name ATTRIBUTE_UNUSED, + void *opaque) +{ + struct virDomainListData *data = opaque; + virDomainObjPtr vm = payload; + virDomainPtr dom; + + if (data->error) + return; + + virDomainObjLock(vm); + /* check if the domain matches the filter */ + + /* filter by active state */ + if (MATCH(VIR_CONNECT_LIST_DOMAINS_FILTERS_ACTIVE) && + !((MATCH(VIR_CONNECT_LIST_DOMAINS_ACTIVE) && + virDomainObjIsActive(vm)) || + (MATCH(VIR_CONNECT_LIST_DOMAINS_INACTIVE) && + !virDomainObjIsActive(vm)))) + goto cleanup; + + /* filter by persistence */ + if (MATCH(VIR_CONNECT_LIST_DOMAINS_FILTERS_PERSISTENT) && + !((MATCH(VIR_CONNECT_LIST_DOMAINS_PERSISTENT) && + vm->persistent) || + (MATCH(VIR_CONNECT_LIST_DOMAINS_TRANSIENT) && + !vm->persistent))) + goto cleanup; + + /* filter by domain state */ + if (MATCH(VIR_CONNECT_LIST_DOMAINS_FILTERS_STATE)) { + int st = virDomainObjGetState(vm, NULL); + if (!((MATCH(VIR_CONNECT_LIST_DOMAINS_RUNNING) && + st == VIR_DOMAIN_RUNNING) || + (MATCH(VIR_CONNECT_LIST_DOMAINS_PAUSED) && + st == VIR_DOMAIN_PAUSED) || + (MATCH(VIR_CONNECT_LIST_DOMAINS_SHUTOFF) && + st == VIR_DOMAIN_SHUTOFF) || + (MATCH(VIR_CONNECT_LIST_DOMAINS_OTHER) && + (st != VIR_DOMAIN_RUNNING && + st != VIR_DOMAIN_PAUSED && + st != VIR_DOMAIN_SHUTOFF)))) + goto cleanup; + } + + /* filter by existence of managed save state */ + if (MATCH(VIR_CONNECT_LIST_DOMAINS_FILTERS_MANAGEDSAVE) && + !((MATCH(VIR_CONNECT_LIST_DOMAINS_MANAGEDSAVE) && + vm->hasManagedSave) || + (MATCH(VIR_CONNECT_LIST_DOMAINS_NO_MANAGEDSAVE) && + !vm->hasManagedSave))) + goto cleanup; + + /* filter by autostart option */ + if (MATCH(VIR_CONNECT_LIST_DOMAINS_FILTERS_AUTOSTART) && + !((MATCH(VIR_CONNECT_LIST_DOMAINS_AUTOSTART) && vm->autostart) || + (MATCH(VIR_CONNECT_LIST_DOMAINS_NO_AUTOSTART) && !vm->autostart))) + goto cleanup; + + /* filter by snapshot existence */ + if (MATCH(VIR_CONNECT_LIST_DOMAINS_FILTERS_SNAPSHOT)) { + int nsnap = virDomainSnapshotObjListNum(&vm->snapshots, NULL, 0); + if (!((MATCH(VIR_CONNECT_LIST_DOMAINS_HAS_SNAPSHOT) && nsnap > 0) || + (MATCH(VIR_CONNECT_LIST_DOMAINS_NO_SNAPSHOT) && nsnap <= 0))) + goto cleanup; + } + + /* just count the machines */ + if (!data->domains) { + data->ndomains++; + return; + } + + if (!(dom = virGetDomain(data->conn, vm->def->name, vm->def->uuid))) { + data->error = true; + goto cleanup; + } + + dom->id = vm->def->id; + + data->domains[data->ndomains++] = dom; + +cleanup: + virDomainObjUnlock(vm); + return; +} +#undef MATCH + +int +virDomainList(virConnectPtr conn, + virHashTablePtr domobjs, + virDomainPtr **domains, + unsigned int flags) +{ + int ret = -1; + int i; + + struct virDomainListData data = { conn, NULL, flags, 0, false }; + + if (domains) { + if (VIR_ALLOC_N(data.domains, virHashSize(domobjs) + 1) < 0) { + virReportOOMError(); + goto cleanup; + } + } + + virHashForEach(domobjs, virDomainListPopulate, &data); + + if (data.error) + goto cleanup; + + if (data.domains) { + /* trim the array to the final size */ + ignore_value(VIR_REALLOC_N(data.domains, data.ndomains + 1)); + *domains = data.domains; + data.domains = NULL; + } + + ret = data.ndomains; + +cleanup: + if (data.domains) { + int count = virHashSize(domobjs); + for (i = 0; i < count; i++) { + if (data.domains[i]) + virUnrefDomain(data.domains[i]); + } + } + + VIR_FREE(data.domains); + return ret; +} + +int +virDomainListSnapshots(virDomainSnapshotObjListPtr snapshots, + virDomainSnapshotObjPtr from, + virDomainPtr dom, + virDomainSnapshotPtr **snaps, + unsigned int flags) +{ + int count = virDomainSnapshotObjListNum(snapshots, from, flags); + virDomainSnapshotPtr *list; + char **names; + int ret = -1; + int i; + + if (!snaps) + return count; + if (VIR_ALLOC_N(names, count) < 0 || + VIR_ALLOC_N(list, count + 1) < 0) { + virReportOOMError(); + goto cleanup; + } + + if (virDomainSnapshotObjListGetNames(snapshots, from, names, count, + flags) < 0) + goto cleanup; + for (i = 0; i < count; i++) + if ((list[i] = virGetDomainSnapshot(dom, names[i])) == NULL) + goto cleanup; + + ret = count; + *snaps = list; + +cleanup: + for (i = 0; i < count; i++) + VIR_FREE(names[i]); + VIR_FREE(names); + if (ret < 0 && list) { + for (i = 0; i < count; i++) + if (list[i]) + virUnrefDomainSnapshot(list[i]); + VIR_FREE(list); + } + return ret; +} diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h index f4c43c6..3fc77d8 100644 --- a/src/conf/domain_conf.h +++ b/src/conf/domain_conf.h @@ -2256,4 +2256,60 @@ VIR_ENUM_DECL(virDomainStartupPolicy) virDomainNetDefPtr virDomainNetFind(virDomainDefPtr def, const char *device); + +# define VIR_CONNECT_LIST_DOMAINS_FILTERS_ACTIVE \ + (VIR_CONNECT_LIST_DOMAINS_ACTIVE | \ + VIR_CONNECT_LIST_DOMAINS_INACTIVE) + +# define VIR_CONNECT_LIST_DOMAINS_FILTERS_PERSISTENT \ + (VIR_CONNECT_LIST_DOMAINS_PERSISTENT | \ + VIR_CONNECT_LIST_DOMAINS_TRANSIENT) + +# define VIR_CONNECT_LIST_DOMAINS_FILTERS_STATE \ + (VIR_CONNECT_LIST_DOMAINS_RUNNING | \ + VIR_CONNECT_LIST_DOMAINS_PAUSED | \ + VIR_CONNECT_LIST_DOMAINS_SHUTOFF | \ + VIR_CONNECT_LIST_DOMAINS_OTHER) + +# define VIR_CONNECT_LIST_DOMAINS_FILTERS_MANAGEDSAVE \ + (VIR_CONNECT_LIST_DOMAINS_MANAGEDSAVE | \ + VIR_CONNECT_LIST_DOMAINS_NO_MANAGEDSAVE) + +# define VIR_CONNECT_LIST_DOMAINS_FILTERS_AUTOSTART \ + (VIR_CONNECT_LIST_DOMAINS_AUTOSTART | \ + VIR_CONNECT_LIST_DOMAINS_NO_AUTOSTART) + +# define VIR_CONNECT_LIST_DOMAINS_FILTERS_SNAPSHOT \ + (VIR_CONNECT_LIST_DOMAINS_HAS_SNAPSHOT | \ + VIR_CONNECT_LIST_DOMAINS_NO_SNAPSHOT) + +# define VIR_CONNECT_LIST_DOMAINS_FILTERS_ALL \ + (VIR_CONNECT_LIST_DOMAINS_FILTERS_ACTIVE | \ + VIR_CONNECT_LIST_DOMAINS_FILTERS_PERSISTENT | \ + VIR_CONNECT_LIST_DOMAINS_FILTERS_STATE | \ + VIR_CONNECT_LIST_DOMAINS_FILTERS_MANAGEDSAVE | \ + VIR_CONNECT_LIST_DOMAINS_FILTERS_AUTOSTART | \ + VIR_CONNECT_LIST_DOMAINS_FILTERS_SNAPSHOT) + +# define VIR_DOMAIN_SNAPSHOT_FILTERS_METADATA \ + (VIR_DOMAIN_SNAPSHOT_LIST_METADATA | \ + VIR_DOMAIN_SNAPSHOT_LIST_NO_METADATA) + +# define VIR_DOMAIN_SNAPSHOT_FILTERS_LEAVES \ + (VIR_DOMAIN_SNAPSHOT_LIST_LEAVES | \ + VIR_DOMAIN_SNAPSHOT_LIST_NO_LEAVES) + +# define VIR_DOMAIN_SNAPSHOT_FILTERS_ALL \ + (VIR_DOMAIN_SNAPSHOT_FILTERS_METADATA | \ + VIR_DOMAIN_SNAPSHOT_FILTERS_LEAVES) + +int virDomainList(virConnectPtr conn, virHashTablePtr domobjs, + virDomainPtr **domains, unsigned int flags); + +int virDomainListSnapshots(virDomainSnapshotObjListPtr snapshots, + virDomainSnapshotObjPtr from, + virDomainPtr dom, + virDomainSnapshotPtr **snaps, + unsigned int flags); + #endif /* __DOMAIN_CONF_H */ diff --git a/src/conf/virdomainlist.c b/src/conf/virdomainlist.c deleted file mode 100644 index c882bc2..0000000 --- a/src/conf/virdomainlist.c +++ /dev/null @@ -1,222 +0,0 @@ -/** - * virdomainlist.c: Helpers for listing and filtering domains. - * - * Copyright (C) 2012 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 - * <http://www.gnu.org/licenses/>. - * - * Author: Peter Krempa <pkrempa@xxxxxxxxxx> - */ - -#include <config.h> - -#include "virdomainlist.h" - -#include "internal.h" -#include "virhash.h" -#include "domain_conf.h" -#include "memory.h" -#include "datatypes.h" -#include "virterror_internal.h" - -#define VIR_FROM_THIS VIR_FROM_DOMAIN - -struct virDomainListData { - virConnectPtr conn; - virDomainPtr *domains; - unsigned int flags; - int ndomains; - bool error; -}; - -#define MATCH(FLAG) (data->flags & (FLAG)) -static void -virDomainListPopulate(void *payload, - const void *name ATTRIBUTE_UNUSED, - void *opaque) -{ - struct virDomainListData *data = opaque; - virDomainObjPtr vm = payload; - virDomainPtr dom; - - if (data->error) - return; - - virDomainObjLock(vm); - /* check if the domain matches the filter */ - - /* filter by active state */ - if (MATCH(VIR_CONNECT_LIST_FILTERS_ACTIVE) && - !((MATCH(VIR_CONNECT_LIST_DOMAINS_ACTIVE) && - virDomainObjIsActive(vm)) || - (MATCH(VIR_CONNECT_LIST_DOMAINS_INACTIVE) && - !virDomainObjIsActive(vm)))) - goto cleanup; - - /* filter by persistence */ - if (MATCH(VIR_CONNECT_LIST_FILTERS_PERSISTENT) && - !((MATCH(VIR_CONNECT_LIST_DOMAINS_PERSISTENT) && - vm->persistent) || - (MATCH(VIR_CONNECT_LIST_DOMAINS_TRANSIENT) && - !vm->persistent))) - goto cleanup; - - /* filter by domain state */ - if (MATCH(VIR_CONNECT_LIST_FILTERS_STATE)) { - int st = virDomainObjGetState(vm, NULL); - if (!((MATCH(VIR_CONNECT_LIST_DOMAINS_RUNNING) && - st == VIR_DOMAIN_RUNNING) || - (MATCH(VIR_CONNECT_LIST_DOMAINS_PAUSED) && - st == VIR_DOMAIN_PAUSED) || - (MATCH(VIR_CONNECT_LIST_DOMAINS_SHUTOFF) && - st == VIR_DOMAIN_SHUTOFF) || - (MATCH(VIR_CONNECT_LIST_DOMAINS_OTHER) && - (st != VIR_DOMAIN_RUNNING && - st != VIR_DOMAIN_PAUSED && - st != VIR_DOMAIN_SHUTOFF)))) - goto cleanup; - } - - /* filter by existence of managed save state */ - if (MATCH(VIR_CONNECT_LIST_FILTERS_MANAGEDSAVE) && - !((MATCH(VIR_CONNECT_LIST_DOMAINS_MANAGEDSAVE) && - vm->hasManagedSave) || - (MATCH(VIR_CONNECT_LIST_DOMAINS_NO_MANAGEDSAVE) && - !vm->hasManagedSave))) - goto cleanup; - - /* filter by autostart option */ - if (MATCH(VIR_CONNECT_LIST_FILTERS_AUTOSTART) && - !((MATCH(VIR_CONNECT_LIST_DOMAINS_AUTOSTART) && vm->autostart) || - (MATCH(VIR_CONNECT_LIST_DOMAINS_NO_AUTOSTART) && !vm->autostart))) - goto cleanup; - - /* filter by snapshot existence */ - if (MATCH(VIR_CONNECT_LIST_FILTERS_SNAPSHOT)) { - int nsnap = virDomainSnapshotObjListNum(&vm->snapshots, NULL, 0); - if (!((MATCH(VIR_CONNECT_LIST_DOMAINS_HAS_SNAPSHOT) && nsnap > 0) || - (MATCH(VIR_CONNECT_LIST_DOMAINS_NO_SNAPSHOT) && nsnap <= 0))) - goto cleanup; - } - - /* just count the machines */ - if (!data->domains) { - data->ndomains++; - return; - } - - if (!(dom = virGetDomain(data->conn, vm->def->name, vm->def->uuid))) { - data->error = true; - goto cleanup; - } - - dom->id = vm->def->id; - - data->domains[data->ndomains++] = dom; - -cleanup: - virDomainObjUnlock(vm); - return; -} -#undef MATCH - -int -virDomainList(virConnectPtr conn, - virHashTablePtr domobjs, - virDomainPtr **domains, - unsigned int flags) -{ - int ret = -1; - int i; - - struct virDomainListData data = { conn, NULL, flags, 0, false }; - - if (domains) { - if (VIR_ALLOC_N(data.domains, virHashSize(domobjs) + 1) < 0) { - virReportOOMError(); - goto cleanup; - } - } - - virHashForEach(domobjs, virDomainListPopulate, &data); - - if (data.error) - goto cleanup; - - if (data.domains) { - /* trim the array to the final size */ - ignore_value(VIR_REALLOC_N(data.domains, data.ndomains + 1)); - *domains = data.domains; - data.domains = NULL; - } - - ret = data.ndomains; - -cleanup: - if (data.domains) { - int count = virHashSize(domobjs); - for (i = 0; i < count; i++) { - if (data.domains[i]) - virDomainFree(data.domains[i]); - } - } - - VIR_FREE(data.domains); - return ret; -} - -int -virDomainListSnapshots(virDomainSnapshotObjListPtr snapshots, - virDomainSnapshotObjPtr from, - virDomainPtr dom, - virDomainSnapshotPtr **snaps, - unsigned int flags) -{ - int count = virDomainSnapshotObjListNum(snapshots, from, flags); - virDomainSnapshotPtr *list; - char **names; - int ret = -1; - int i; - - if (!snaps) - return count; - if (VIR_ALLOC_N(names, count) < 0 || - VIR_ALLOC_N(list, count + 1) < 0) { - virReportOOMError(); - goto cleanup; - } - - if (virDomainSnapshotObjListGetNames(snapshots, from, names, count, - flags) < 0) - goto cleanup; - for (i = 0; i < count; i++) - if ((list[i] = virGetDomainSnapshot(dom, names[i])) == NULL) - goto cleanup; - - ret = count; - *snaps = list; - -cleanup: - for (i = 0; i < count; i++) - VIR_FREE(names[i]); - VIR_FREE(names); - if (ret < 0 && list) { - for (i = 0; i < count; i++) - if (list[i]) - virDomainSnapshotFree(list[i]); - VIR_FREE(list); - } - return ret; -} diff --git a/src/conf/virdomainlist.h b/src/conf/virdomainlist.h deleted file mode 100644 index 322e5b8..0000000 --- a/src/conf/virdomainlist.h +++ /dev/null @@ -1,84 +0,0 @@ -/** - * virdomainlist.h: Helpers for listing and filtering domains. - * - * Copyright (C) 2012 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 - * <http://www.gnu.org/licenses/>. - * - * Author: Peter Krempa <pkrempa@xxxxxxxxxx> - */ -#ifndef __VIR_DOMAIN_LIST_H__ -# define __VIR_DOMAIN_LIST_H__ - -# include "internal.h" -# include "virhash.h" -# include "domain_conf.h" - -# define VIR_CONNECT_LIST_FILTERS_ACTIVE \ - (VIR_CONNECT_LIST_DOMAINS_ACTIVE | \ - VIR_CONNECT_LIST_DOMAINS_INACTIVE) - -# define VIR_CONNECT_LIST_FILTERS_PERSISTENT \ - (VIR_CONNECT_LIST_DOMAINS_PERSISTENT | \ - VIR_CONNECT_LIST_DOMAINS_TRANSIENT) - -# define VIR_CONNECT_LIST_FILTERS_STATE \ - (VIR_CONNECT_LIST_DOMAINS_RUNNING | \ - VIR_CONNECT_LIST_DOMAINS_PAUSED | \ - VIR_CONNECT_LIST_DOMAINS_SHUTOFF | \ - VIR_CONNECT_LIST_DOMAINS_OTHER) - -# define VIR_CONNECT_LIST_FILTERS_MANAGEDSAVE \ - (VIR_CONNECT_LIST_DOMAINS_MANAGEDSAVE | \ - VIR_CONNECT_LIST_DOMAINS_NO_MANAGEDSAVE) - -# define VIR_CONNECT_LIST_FILTERS_AUTOSTART \ - (VIR_CONNECT_LIST_DOMAINS_AUTOSTART | \ - VIR_CONNECT_LIST_DOMAINS_NO_AUTOSTART) - -# define VIR_CONNECT_LIST_FILTERS_SNAPSHOT \ - (VIR_CONNECT_LIST_DOMAINS_HAS_SNAPSHOT | \ - VIR_CONNECT_LIST_DOMAINS_NO_SNAPSHOT) - -# define VIR_CONNECT_LIST_FILTERS_ALL \ - (VIR_CONNECT_LIST_FILTERS_ACTIVE | \ - VIR_CONNECT_LIST_FILTERS_PERSISTENT | \ - VIR_CONNECT_LIST_FILTERS_STATE | \ - VIR_CONNECT_LIST_FILTERS_MANAGEDSAVE | \ - VIR_CONNECT_LIST_FILTERS_AUTOSTART | \ - VIR_CONNECT_LIST_FILTERS_SNAPSHOT) - -# define VIR_DOMAIN_SNAPSHOT_FILTERS_METADATA \ - (VIR_DOMAIN_SNAPSHOT_LIST_METADATA | \ - VIR_DOMAIN_SNAPSHOT_LIST_NO_METADATA) - -# define VIR_DOMAIN_SNAPSHOT_FILTERS_LEAVES \ - (VIR_DOMAIN_SNAPSHOT_LIST_LEAVES | \ - VIR_DOMAIN_SNAPSHOT_LIST_NO_LEAVES) - -# define VIR_DOMAIN_SNAPSHOT_FILTERS_ALL \ - (VIR_DOMAIN_SNAPSHOT_FILTERS_METADATA | \ - VIR_DOMAIN_SNAPSHOT_FILTERS_LEAVES) - -int virDomainList(virConnectPtr conn, virHashTablePtr domobjs, - virDomainPtr **domains, unsigned int flags); - -int virDomainListSnapshots(virDomainSnapshotObjListPtr snapshots, - virDomainSnapshotObjPtr from, - virDomainPtr dom, - virDomainSnapshotPtr **snaps, - unsigned int flags); - -#endif /* __VIR_DOMAIN_LIST_H__ */ diff --git a/src/libxl/libxl_driver.c b/src/libxl/libxl_driver.c index 873f973..c2d264b 100644 --- a/src/libxl/libxl_driver.c +++ b/src/libxl/libxl_driver.c @@ -45,7 +45,6 @@ #include "xen_xm.h" #include "virtypedparam.h" #include "viruri.h" -#include "virdomainlist.h" #define VIR_FROM_THIS VIR_FROM_LIBXL @@ -3869,7 +3868,7 @@ libxlListAllDomains(virConnectPtr conn, libxlDriverPrivatePtr driver = conn->privateData; int ret = -1; - virCheckFlags(VIR_CONNECT_LIST_FILTERS_ALL, -1); + virCheckFlags(VIR_CONNECT_LIST_DOMAINS_FILTERS_ALL, -1); libxlDriverLock(driver); ret = virDomainList(conn, driver->domains.objs, domains, flags); diff --git a/src/lxc/lxc_driver.c b/src/lxc/lxc_driver.c index d118dd2..13a2759 100644 --- a/src/lxc/lxc_driver.c +++ b/src/lxc/lxc_driver.c @@ -63,7 +63,6 @@ #include "virtime.h" #include "virtypedparam.h" #include "viruri.h" -#include "virdomainlist.h" #define VIR_FROM_THIS VIR_FROM_LXC @@ -2645,7 +2644,7 @@ lxcListAllDomains(virConnectPtr conn, virLXCDriverPtr driver = conn->privateData; int ret = -1; - virCheckFlags(VIR_CONNECT_LIST_FILTERS_ALL, -1); + virCheckFlags(VIR_CONNECT_LIST_DOMAINS_FILTERS_ALL, -1); lxcDriverLock(driver); ret = virDomainList(conn, driver->domains.objs, domains, flags); diff --git a/src/openvz/openvz_driver.c b/src/openvz/openvz_driver.c index 48b5219..8257ed5 100644 --- a/src/openvz/openvz_driver.c +++ b/src/openvz/openvz_driver.c @@ -60,7 +60,6 @@ #include "command.h" #include "viruri.h" #include "stats_linux.h" -#include "virdomainlist.h" #define VIR_FROM_THIS VIR_FROM_OPENVZ @@ -2104,7 +2103,7 @@ openvzListAllDomains(virConnectPtr conn, struct openvz_driver *driver = conn->privateData; int ret = -1; - virCheckFlags(VIR_CONNECT_LIST_FILTERS_ALL, -1); + virCheckFlags(VIR_CONNECT_LIST_DOMAINS_FILTERS_ALL, -1); openvzDriverLock(driver); ret = virDomainList(conn, driver->domains.objs, domains, flags); diff --git a/src/parallels/parallels_driver.c b/src/parallels/parallels_driver.c index b57ac96..7d8869a 100644 --- a/src/parallels/parallels_driver.c +++ b/src/parallels/parallels_driver.c @@ -50,7 +50,6 @@ #include "configmake.h" #include "storage_file.h" #include "nodeinfo.h" -#include "virdomainlist.h" #include "parallels_driver.h" #include "parallels_utils.h" @@ -810,7 +809,7 @@ parallelsListAllDomains(virConnectPtr conn, parallelsConnPtr privconn = conn->privateData; int ret = -1; - virCheckFlags(VIR_CONNECT_LIST_FILTERS_ALL, -1); + virCheckFlags(VIR_CONNECT_LIST_DOMAINS_FILTERS_ALL, -1); parallelsDriverLock(privconn); ret = virDomainList(conn, privconn->domains.objs, domains, flags); parallelsDriverUnlock(privconn); diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index 369e8ed..e53d801 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -92,7 +92,6 @@ #include "virnodesuspend.h" #include "virtime.h" #include "virtypedparam.h" -#include "virdomainlist.h" #define VIR_FROM_THIS VIR_FROM_QEMU @@ -13195,7 +13194,7 @@ qemuListAllDomains(virConnectPtr conn, struct qemud_driver *driver = conn->privateData; int ret = -1; - virCheckFlags(VIR_CONNECT_LIST_FILTERS_ALL, -1); + virCheckFlags(VIR_CONNECT_LIST_DOMAINS_FILTERS_ALL, -1); qemuDriverLock(driver); ret = virDomainList(conn, driver->domains.objs, domains, flags); diff --git a/src/test/test_driver.c b/src/test/test_driver.c index 01e515a..a767e21 100644 --- a/src/test/test_driver.c +++ b/src/test/test_driver.c @@ -52,7 +52,6 @@ #include "virfile.h" #include "virtypedparam.h" #include "virrandom.h" -#include "virdomainlist.h" #define VIR_FROM_THIS VIR_FROM_TEST @@ -5526,7 +5525,7 @@ static int testListAllDomains(virConnectPtr conn, testConnPtr privconn = conn->privateData; int ret; - virCheckFlags(VIR_CONNECT_LIST_FILTERS_ALL, -1); + virCheckFlags(VIR_CONNECT_LIST_DOMAINS_FILTERS_ALL, -1); testDriverLock(privconn); ret = virDomainList(conn, privconn->domains.objs, domains, flags); diff --git a/src/uml/uml_driver.c b/src/uml/uml_driver.c index a4a9258..c493bf7 100644 --- a/src/uml/uml_driver.c +++ b/src/uml/uml_driver.c @@ -64,7 +64,6 @@ #include "virnetdevtap.h" #include "virnodesuspend.h" #include "viruri.h" -#include "virdomainlist.h" #define VIR_FROM_THIS VIR_FROM_UML @@ -2525,7 +2524,7 @@ static int umlListAllDomains(virConnectPtr conn, struct uml_driver *driver = conn->privateData; int ret = -1; - virCheckFlags(VIR_CONNECT_LIST_FILTERS_ALL, -1); + virCheckFlags(VIR_CONNECT_LIST_DOMAINS_FILTERS_ALL, -1); umlDriverLock(driver); ret = virDomainList(conn, driver->domains.objs, domains, flags); diff --git a/src/vbox/vbox_tmpl.c b/src/vbox/vbox_tmpl.c index b672b24..dd1e376 100644 --- a/src/vbox/vbox_tmpl.c +++ b/src/vbox/vbox_tmpl.c @@ -57,7 +57,6 @@ #include "virfile.h" #include "fdstream.h" #include "viruri.h" -#include "virdomainlist.h" /* This one changes from version to version. */ #if VBOX_API_VERSION == 2002 @@ -9254,7 +9253,7 @@ vboxListAllDomains(virConnectPtr conn, bool active; PRUint32 snapshotCount; - virCheckFlags(VIR_CONNECT_LIST_FILTERS_ALL, -1); + virCheckFlags(VIR_CONNECT_LIST_DOMAINS_FILTERS_ALL, -1); /* filter out flag options that will produce 0 results in vbox driver: * - managed save: vbox guests don't have managed save images @@ -9302,13 +9301,13 @@ vboxListAllDomains(virConnectPtr conn, active = false; /* filter by active state */ - if (MATCH(VIR_CONNECT_LIST_FILTERS_ACTIVE) && + if (MATCH(VIR_CONNECT_LIST_DOMAINS_FILTERS_ACTIVE) && !((MATCH(VIR_CONNECT_LIST_DOMAINS_ACTIVE) && active) || (MATCH(VIR_CONNECT_LIST_DOMAINS_INACTIVE) && !active))) continue; /* filter by snapshot existence */ - if (MATCH(VIR_CONNECT_LIST_FILTERS_SNAPSHOT)) { + if (MATCH(VIR_CONNECT_LIST_DOMAINS_FILTERS_SNAPSHOT)) { rc = machine->vtbl->GetSnapshotCount(machine, &snapshotCount); if (NS_FAILED(rc)) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", @@ -9323,7 +9322,7 @@ vboxListAllDomains(virConnectPtr conn, } /* filter by machine state */ - if (MATCH(VIR_CONNECT_LIST_FILTERS_STATE) && + if (MATCH(VIR_CONNECT_LIST_DOMAINS_FILTERS_STATE) && !((MATCH(VIR_CONNECT_LIST_DOMAINS_RUNNING) && state == MachineState_Running) || (MATCH(VIR_CONNECT_LIST_DOMAINS_PAUSED) && diff --git a/src/vmware/vmware_driver.c b/src/vmware/vmware_driver.c index 6c6347a..1607018 100644 --- a/src/vmware/vmware_driver.c +++ b/src/vmware/vmware_driver.c @@ -34,7 +34,6 @@ #include "vmx.h" #include "vmware_conf.h" #include "vmware_driver.h" -#include "virdomainlist.h" static const char *vmw_types[] = { "player", "ws" }; @@ -1098,7 +1097,7 @@ vmwareListAllDomains(virConnectPtr conn, struct vmware_driver *driver = conn->privateData; int ret = -1; - virCheckFlags(VIR_CONNECT_LIST_FILTERS_ALL, -1); + virCheckFlags(VIR_CONNECT_LIST_DOMAINS_FILTERS_ALL, -1); vmwareDriverLock(driver); vmwareDomainObjListUpdateAll(&driver->domains, driver); diff --git a/tools/virsh-domain-monitor.c b/tools/virsh-domain-monitor.c index 151a8d0..b57c390 100644 --- a/tools/virsh-domain-monitor.c +++ b/tools/virsh-domain-monitor.c @@ -1365,7 +1365,7 @@ fallback: vshResetLibvirtError(); /* list active domains, if necessary */ - if (!MATCH(VIR_CONNECT_LIST_FILTERS_ACTIVE) || + if (!MATCH(VIR_CONNECT_LIST_DOMAINS_FILTERS_ACTIVE) || MATCH(VIR_CONNECT_LIST_DOMAINS_ACTIVE)) { if ((nids = virConnectNumOfDomains(ctl->conn)) < 0) { vshError(ctl, "%s", _("Failed to list active domains")); @@ -1382,7 +1382,7 @@ fallback: } } - if (!MATCH(VIR_CONNECT_LIST_FILTERS_ACTIVE) || + if (!MATCH(VIR_CONNECT_LIST_DOMAINS_FILTERS_ACTIVE) || MATCH(VIR_CONNECT_LIST_DOMAINS_INACTIVE)) { if ((nnames = virConnectNumOfDefinedDomains(ctl->conn)) < 0) { vshError(ctl, "%s", _("Failed to list inactive domains")); @@ -1426,7 +1426,7 @@ filter: dom = list->domains[i]; /* persistence filter */ - if (MATCH(VIR_CONNECT_LIST_FILTERS_PERSISTENT)) { + if (MATCH(VIR_CONNECT_LIST_DOMAINS_FILTERS_PERSISTENT)) { if ((persistent = virDomainIsPersistent(dom)) < 0) { vshError(ctl, "%s", _("Failed to get domain persistence info")); goto cleanup; @@ -1438,7 +1438,7 @@ filter: } /* domain state filter */ - if (MATCH(VIR_CONNECT_LIST_FILTERS_STATE)) { + if (MATCH(VIR_CONNECT_LIST_DOMAINS_FILTERS_STATE)) { if (virDomainGetState(dom, &state, NULL, 0) < 0) { vshError(ctl, "%s", _("Failed to get domain state")); goto cleanup; @@ -1458,7 +1458,7 @@ filter: } /* autostart filter */ - if (MATCH(VIR_CONNECT_LIST_FILTERS_AUTOSTART)) { + if (MATCH(VIR_CONNECT_LIST_DOMAINS_FILTERS_AUTOSTART)) { if (virDomainGetAutostart(dom, &autostart) < 0) { vshError(ctl, "%s", _("Failed to get domain autostart state")); goto cleanup; @@ -1470,7 +1470,7 @@ filter: } /* managed save filter */ - if (MATCH(VIR_CONNECT_LIST_FILTERS_MANAGEDSAVE)) { + if (MATCH(VIR_CONNECT_LIST_DOMAINS_FILTERS_MANAGEDSAVE)) { if ((mansave = virDomainHasManagedSaveImage(dom, 0)) < 0) { vshError(ctl, "%s", _("Failed to check for managed save image")); @@ -1483,7 +1483,7 @@ filter: } /* snapshot filter */ - if (MATCH(VIR_CONNECT_LIST_FILTERS_SNAPSHOT)) { + if (MATCH(VIR_CONNECT_LIST_DOMAINS_FILTERS_SNAPSHOT)) { if ((nsnap = virDomainSnapshotNum(dom, 0)) < 0) { vshError(ctl, "%s", _("Failed to get snapshot count")); goto cleanup; diff --git a/tools/virsh.c b/tools/virsh.c index b95a008..c57b229 100644 --- a/tools/virsh.c +++ b/tools/virsh.c @@ -76,7 +76,6 @@ #include "util/bitmap.h" #include "conf/domain_conf.h" #include "virtypedparam.h" -#include "conf/virdomainlist.h" static char *progname; -- 1.7.7.3 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list