[libvirt] [PATCH v3 1/2] qemu: implementing qemuAgentGetHostname() function.

John Ferlan jferlan at redhat.com
Wed Aug 29 20:13:41 UTC 2018



On 08/21/2018 10:39 PM, Julio Faracco wrote:
> This commit implements the function qemuAgentGetHostname() that uses
> the QEMU command 'guest-get-host-name' to retrieve the guest hostname

QEMU guest agent

> of Virtual Machine. It is a possibility where QEMU-GA is running.

of the virtual machine running the QEMU-GA.

> 
> Signed-off-by: Julio Faracco <jcfaracco at gmail.com>
> ---
>  src/qemu/qemu_agent.c | 39 +++++++++++++++++++++++++++++++++++++++
>  src/qemu/qemu_agent.h |  4 ++++
>  2 files changed, 43 insertions(+)
> 
> diff --git a/src/qemu/qemu_agent.c b/src/qemu/qemu_agent.c
> index bf08871f18..7aba4ed366 100644
> --- a/src/qemu/qemu_agent.c
> +++ b/src/qemu/qemu_agent.c
> @@ -1682,6 +1682,45 @@ qemuAgentUpdateCPUInfo(unsigned int nvcpus,
>      return 0;
>  }
>  
> +int
> +qemuAgentGetHostname(qemuAgentPtr mon,
> +                     char **hostname)
> +{
> +    int ret = -1;
> +    virJSONValuePtr cmd;
> +    virJSONValuePtr reply = NULL;
> +    virJSONValuePtr data = NULL;
> +
> +    cmd = qemuAgentMakeCommand("guest-get-host-name",
> +                               NULL);
> +
> +    if (!cmd)
> +        return ret;
> +
> +    if (qemuAgentCommand(mon, cmd, &reply, true,
> +                         VIR_DOMAIN_QEMU_AGENT_COMMAND_BLOCK) < 0)
> +        goto cleanup;

FWIW: The v2 dropped the qemuAgentCheckError... But I note two others
exist - perhaps worthy of a followup patch or two?

> +
> +    if (!(data = virJSONValueObjectGet(reply, "return"))) {
> +        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
> +                       _("malformed return value"));
> +        goto cleanup;
> +    }
> +
> +    if (VIR_STRDUP(*hostname,
> +                   virJSONValueObjectGetString(data, "host-name")) <= 0) {

In this case the error would be overwritten in the case were VIR_STRDUP
returns -1.  Let's separate them and fetch into a local const char * -
there's more examples that take that option... and yes, I think
qemuAgentGetFSInfo is wrong for the same reason (yet another possible
followup patch)...

IOW:

    if (!(field = virJSONValueObjectGetString(data, "host-name"))) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("'host-name' missing in guest-get-host-name reply"));
        goto cleanup;
    }

    if (VIR_STRDUP(*hostname, field) < 0)
        goto cleanup;

As long as you're OK with it, I can merge something like this in and
push after the 4.7.0 freeze into 4.8.0.

Reviewed-by: John Ferlan <jferlan at redhat.com>

John

[...]




More information about the libvir-list mailing list