Introduce qemuDomainDeviceBackendChardevForeach(One) which calls the
callback if either given device has a chardev backend or for all chardev
backends of all devices.
Signed-off-by: Peter Krempa <pkrempa(a)redhat.com>
---
src/qemu/qemu_domain.c | 117 +++++++++++++++++++++++++++++++++++++++++
src/qemu/qemu_domain.h | 12 +++++
2 files changed, 129 insertions(+)
diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c
index 2c10f185f8..4f0d266c73 100644
--- a/src/qemu/qemu_domain.c
+++ b/src/qemu/qemu_domain.c
@@ -11455,3 +11455,120 @@ qemuDomainGetVHostUserFSSocketPath(qemuDomainObjPrivate *priv,
return virFileBuildPath(priv->libDir, fs->info.alias, "-fs.sock");
}
+
+
+/**
+ * qemuDomainDeviceBackendChardevForeachOne:
+ * @dev: device definition
+ * @cb: callback
+ * @opaque: data for @cb
+ *
+ * Calls @cb with the char device backend data if @dev is a device which has a
+ * chardev backend.
+ */
+int
+qemuDomainDeviceBackendChardevForeachOne(virDomainDeviceDef *dev,
+ qemuDomainDeviceBackendChardevForeachCallback
cb,
+ void *opaque)
+{
+ switch ((virDomainDeviceType) dev->type) {
+ case VIR_DOMAIN_DEVICE_NET:
+ if (virDomainNetGetActualType(dev->data.net) !=
VIR_DOMAIN_NET_TYPE_VHOSTUSER)
+ return 0;
+
+ return cb(dev, dev->data.net->data.vhostuser, opaque);
+
+ case VIR_DOMAIN_DEVICE_REDIRDEV:
+ return cb(dev, dev->data.redirdev->source, opaque);
+
+ case VIR_DOMAIN_DEVICE_SHMEM:
+ if (!dev->data.shmem->server.enabled)
+ return 0;
+
+ return cb(dev, dev->data.shmem->server.chr, opaque);
+
+ case VIR_DOMAIN_DEVICE_CHR:
+ return cb(dev, dev->data.chr->source, opaque);
+
+ case VIR_DOMAIN_DEVICE_SMARTCARD:
+ if (dev->data.smartcard->type != VIR_DOMAIN_SMARTCARD_TYPE_PASSTHROUGH)
+ return 0;
+
+ return cb(dev, dev->data.smartcard->data.passthru, opaque);
+
+ case VIR_DOMAIN_DEVICE_RNG:
+ if (dev->data.rng->backend != VIR_DOMAIN_RNG_BACKEND_EGD)
+ return 0;
+
+ return cb(dev, dev->data.rng->source.chardev, opaque);
+
+ case VIR_DOMAIN_DEVICE_DISK:
+ case VIR_DOMAIN_DEVICE_LEASE:
+ case VIR_DOMAIN_DEVICE_FS:
+ case VIR_DOMAIN_DEVICE_INPUT:
+ case VIR_DOMAIN_DEVICE_SOUND:
+ case VIR_DOMAIN_DEVICE_VIDEO:
+ case VIR_DOMAIN_DEVICE_HOSTDEV:
+ case VIR_DOMAIN_DEVICE_WATCHDOG:
+ case VIR_DOMAIN_DEVICE_CONTROLLER:
+ case VIR_DOMAIN_DEVICE_GRAPHICS:
+ case VIR_DOMAIN_DEVICE_HUB:
+ case VIR_DOMAIN_DEVICE_NONE:
+ case VIR_DOMAIN_DEVICE_MEMBALLOON:
+ case VIR_DOMAIN_DEVICE_NVRAM:
+ case VIR_DOMAIN_DEVICE_TPM:
+ case VIR_DOMAIN_DEVICE_PANIC:
+ case VIR_DOMAIN_DEVICE_LAST:
+ case VIR_DOMAIN_DEVICE_MEMORY:
+ case VIR_DOMAIN_DEVICE_IOMMU:
+ case VIR_DOMAIN_DEVICE_VSOCK:
+ case VIR_DOMAIN_DEVICE_AUDIO:
+ /* no chardev backend */
+ break;
+ }
+
+ return 0;
+}
+
+struct qemuDomainDeviceBackendChardevIterData {
+ qemuDomainDeviceBackendChardevForeachCallback cb;
+ void *cbdata;
+};
+
+
+static int
+qemuDomainDeviceBackendChardevIter(virDomainDef *def G_GNUC_UNUSED,
+ virDomainDeviceDef *dev,
+ virDomainDeviceInfo *info G_GNUC_UNUSED,
+ void *opaque)
+{
+ struct qemuDomainDeviceBackendChardevIterData *data = opaque;
+
+ return qemuDomainDeviceBackendChardevForeachOne(dev, data->cb, data->cbdata);
+}
+
+
+/**
+ * qemuDomainDeviceBackendChardevForeach:a
+ * @def: domain definition
+ * @cb: callback
+ * @opqaue: data for @cb
+ *
+ * Same as qemuDomainDeviceBackendChardevForeachOne called for every device in
+ * @def.
+ */
+int
+qemuDomainDeviceBackendChardevForeach(virDomainDef *def,
+ qemuDomainDeviceBackendChardevForeachCallback cb,
+ void *opaque)
+{
+ struct qemuDomainDeviceBackendChardevIterData data = {
+ .cb = cb,
+ .cbdata = opaque,
+ };
+
+ return virDomainDeviceInfoIterateFlags(def,
+ qemuDomainDeviceBackendChardevIter,
+ DOMAIN_DEVICE_ITERATE_MISSING_INFO,
+ &data);
+}
diff --git a/src/qemu/qemu_domain.h b/src/qemu/qemu_domain.h
index f2a303d8a7..3de3f70b94 100644
--- a/src/qemu/qemu_domain.h
+++ b/src/qemu/qemu_domain.h
@@ -1032,3 +1032,15 @@ qemuDomainNamePathsCleanup(virQEMUDriverConfig *cfg,
char *
qemuDomainGetVHostUserFSSocketPath(qemuDomainObjPrivate *priv,
const virDomainFSDef *fs);
+
+typedef int (*qemuDomainDeviceBackendChardevForeachCallback)(virDomainDeviceDef *dev,
+ virDomainChrSourceDef
*charsrc,
+ void *opaque);
+int
+qemuDomainDeviceBackendChardevForeachOne(virDomainDeviceDef *dev,
+ qemuDomainDeviceBackendChardevForeachCallback
cb,
+ void *opaque);
+int
+qemuDomainDeviceBackendChardevForeach(virDomainDef *def,
+ qemuDomainDeviceBackendChardevForeachCallback cb,
+ void *opaque);
--
2.31.1