Eliminate all the code re-use which checks for priv->agentError or priv->agent. Signed-off-by: Martin Kletzander <mkletzan@xxxxxxxxxx> --- src/qemu/qemu_domain.c | 22 +++++++++ src/qemu/qemu_domain.h | 4 ++ src/qemu/qemu_driver.c | 121 ++++++------------------------------------------- 3 files changed, 40 insertions(+), 107 deletions(-) diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c index bc0b8f7..4465bef 100644 --- a/src/qemu/qemu_domain.c +++ b/src/qemu/qemu_domain.c @@ -2465,3 +2465,25 @@ cleanup: virDomainDefFree(migratableDefDst); return ret; } + +bool +qemuDomainAgentAvailable(qemuDomainObjPrivatePtr priv, + bool reportError) +{ + if (priv->agentError) { + if (reportError) { + virReportError(VIR_ERR_AGENT_UNRESPONSIVE, "%s", + _("QEMU guest agent is not " + "available due to an error")); + } + return false; + } + if (!priv->agent) { + if (reportError) { + virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s", + _("QEMU guest agent is not configured")); + } + return false; + } + return true; +} diff --git a/src/qemu/qemu_domain.h b/src/qemu/qemu_domain.h index 0bed50b..b2830c4 100644 --- a/src/qemu/qemu_domain.h +++ b/src/qemu/qemu_domain.h @@ -376,4 +376,8 @@ int qemuDomainUpdateDeviceList(virQEMUDriverPtr driver, bool qemuDomainDefCheckABIStability(virQEMUDriverPtr driver, virDomainDefPtr src, virDomainDefPtr dst); + +bool qemuDomainAgentAvailable(qemuDomainObjPrivatePtr priv, + bool reportError); + #endif /* __QEMU_DOMAIN_H__ */ diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index 89f443f..99a2840 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -1798,6 +1798,7 @@ static int qemuDomainShutdownFlags(virDomainPtr dom, unsigned int flags) { qemuDomainObjPrivatePtr priv; bool useAgent = false, agentRequested, acpiRequested; bool isReboot = false; + bool agentForced; int agentFlag = QEMU_AGENT_SHUTDOWN_POWERDOWN; virCheckFlags(VIR_DOMAIN_SHUTDOWN_ACPI_POWER_BTN | @@ -1824,25 +1825,11 @@ static int qemuDomainShutdownFlags(virDomainPtr dom, unsigned int flags) { if (virDomainShutdownFlagsEnsureACL(dom->conn, vm->def, flags) < 0) goto cleanup; - if (priv->agentError) { - if (agentRequested && !acpiRequested) { - virReportError(VIR_ERR_AGENT_UNRESPONSIVE, "%s", - _("QEMU guest agent is not " - "available due to an error")); + agentForced = agentRequested && !acpiRequested; + if (!qemuDomainAgentAvailable(priv, agentForced)) { + if (agentForced) goto cleanup; - } else { - useAgent = false; - } - } - - if (!priv->agent) { - if (agentRequested && !acpiRequested) { - virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s", - _("QEMU guest agent is not configured")); - goto cleanup; - } else { - useAgent = false; - } + useAgent = false; } if (qemuDomainObjBeginJob(driver, vm, QEMU_JOB_MODIFY) < 0) @@ -1930,18 +1917,8 @@ qemuDomainReboot(virDomainPtr dom, unsigned int flags) priv->agent)) useAgent = true; - if (useAgent) { - if (priv->agentError) { - virReportError(VIR_ERR_AGENT_UNRESPONSIVE, "%s", - _("QEMU guest agent is not " - "available due to an error")); - goto cleanup; - } - if (!priv->agent) { - virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s", - _("QEMU guest agent is not configured")); - goto cleanup; - } + if (useAgent && !qemuDomainAgentAvailable(priv, true)) { + goto cleanup; } else { #if WITH_YAJL if (virQEMUCapsGet(priv->qemuCaps, QEMU_CAPS_MONITOR_JSON)) { @@ -4187,18 +4164,8 @@ qemuDomainSetVcpusFlags(virDomainPtr dom, unsigned int nvcpus, goto endjob; } - if (priv->agentError) { - virReportError(VIR_ERR_AGENT_UNRESPONSIVE, "%s", - _("QEMU guest agent is not " - "available due to an error")); - goto endjob; - } - - if (!priv->agent) { - virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s", - _("QEMU guest agent is not configured")); + if (!qemuDomainAgentAvailable(priv, true)) goto endjob; - } if (nvcpus > vm->def->vcpus) { virReportError(VIR_ERR_INVALID_ARG, @@ -4925,18 +4892,8 @@ qemuDomainGetVcpusFlags(virDomainPtr dom, unsigned int flags) if (qemuDomainObjBeginJob(driver, vm, QEMU_JOB_QUERY) < 0) goto cleanup; - if (priv->agentError) { - virReportError(VIR_ERR_AGENT_UNRESPONSIVE, "%s", - _("QEMU guest agent is not " - "available due to an error")); - goto endjob; - } - - if (!priv->agent) { - virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s", - _("QEMU guest agent is not configured")); + if (!qemuDomainAgentAvailable(priv, true)) goto endjob; - } if (!virDomainObjIsActive(vm)) { virReportError(VIR_ERR_OPERATION_INVALID, "%s", @@ -12044,17 +12001,8 @@ qemuDomainSnapshotFSFreeze(virDomainObjPtr vm) { qemuDomainObjPrivatePtr priv = vm->privateData; int freezed; - if (priv->agentError) { - virReportError(VIR_ERR_AGENT_UNRESPONSIVE, "%s", - _("QEMU guest agent is not " - "available due to an error")); - return -1; - } - if (!priv->agent) { - virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s", - _("QEMU guest agent is not configured")); + if (!qemuDomainAgentAvailable(priv, true)) return -1; - } qemuDomainObjEnterAgent(vm); freezed = qemuAgentFSFreeze(priv->agent); @@ -12070,19 +12018,8 @@ qemuDomainSnapshotFSThaw(virDomainObjPtr vm, bool report) int thawed; virErrorPtr err = NULL; - if (priv->agentError) { - if (report) - virReportError(VIR_ERR_AGENT_UNRESPONSIVE, "%s", - _("QEMU guest agent is not " - "available due to an error")); - return -1; - } - if (!priv->agent) { - if (report) - virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s", - _("QEMU guest agent is not configured")); + if (!qemuDomainAgentAvailable(priv, report)) return -1; - } qemuDomainObjEnterAgent(vm); if (!report) @@ -16227,18 +16164,8 @@ qemuDomainPMSuspendForDuration(virDomainPtr dom, } } - if (priv->agentError) { - virReportError(VIR_ERR_AGENT_UNRESPONSIVE, "%s", - _("QEMU guest agent is not " - "available due to an error")); - goto cleanup; - } - - if (!priv->agent) { - virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s", - _("QEMU guest agent is not configured")); + if (!qemuDomainAgentAvailable(priv, true)) goto cleanup; - } if (qemuDomainObjBeginJob(driver, vm, QEMU_JOB_MODIFY) < 0) goto cleanup; @@ -16360,18 +16287,8 @@ qemuDomainQemuAgentCommand(virDomainPtr domain, goto cleanup; } - if (priv->agentError) { - virReportError(VIR_ERR_AGENT_UNRESPONSIVE, "%s", - _("QEMU guest agent is not " - "available due to an error")); - goto cleanup; - } - - if (!priv->agent) { - virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s", - _("QEMU guest agent is not configured")); + if (!qemuDomainAgentAvailable(priv, true)) goto cleanup; - } if (qemuDomainObjBeginJob(driver, vm, QEMU_JOB_MODIFY) < 0) goto cleanup; @@ -16432,18 +16349,8 @@ qemuDomainFSTrim(virDomainPtr dom, goto cleanup; } - if (!priv->agent) { - virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s", - _("QEMU guest agent is not configured")); - goto cleanup; - } - - if (priv->agentError) { - virReportError(VIR_ERR_AGENT_UNRESPONSIVE, "%s", - _("QEMU guest agent is not " - "available due to an error")); + if (!qemuDomainAgentAvailable(priv, true)) goto cleanup; - } if (qemuDomainObjBeginJob(driver, vm, QEMU_JOB_MODIFY) < 0) goto cleanup; -- 1.9.0 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list