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

Julio Faracco posted 2 patches 7 years, 5 months ago
There is a newer version of this series
[libvirt] [PATCH 1/2] qemu: implementing qemuAgentGetHostname() function.
Posted by Julio Faracco 7 years, 5 months ago
This commit implements the function qemuAgentGetHostname() that uses the
QEMU command 'guest-get-host-name' to retrieve the guest hostname of
Virtual Machine. It is a possibility where QEMU-GA is running.

Signed-off-by: Julio Faracco <jcfaracco@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..2c7f98378e 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;
+
+    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) {
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                       _("'host-name' missing in reply of guest-get-host-name"));
+        goto cleanup;
+    }
+
+    ret = 0;
+
+ cleanup:
+    virJSONValueFree(cmd);
+    virJSONValueFree(reply);
+    return ret;
+}
 
 int
 qemuAgentGetTime(qemuAgentPtr mon,
diff --git a/src/qemu/qemu_agent.h b/src/qemu/qemu_agent.h
index 6dd9c702dd..4354b7e0cf 100644
--- a/src/qemu/qemu_agent.h
+++ b/src/qemu/qemu_agent.h
@@ -105,6 +105,10 @@ int qemuAgentUpdateCPUInfo(unsigned int nvcpus,
                            qemuAgentCPUInfoPtr cpuinfo,
                            int ncpuinfo);
 
+int
+qemuAgentGetHostname(qemuAgentPtr mon,
+                     char **hostname);
+
 int qemuAgentGetTime(qemuAgentPtr mon,
                      long long *seconds,
                      unsigned int *nseconds);
-- 
2.17.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH 1/2] qemu: implementing qemuAgentGetHostname() function.
Posted by Peter Krempa 7 years, 5 months ago
On Fri, Aug 17, 2018 at 23:25:18 -0300, Julio Faracco wrote:
> This commit implements the function qemuAgentGetHostname() that uses the
> QEMU command 'guest-get-host-name' to retrieve the guest hostname of
> Virtual Machine. It is a possibility where QEMU-GA is running.
> 
> Signed-off-by: Julio Faracco <jcfaracco@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..2c7f98378e 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;
> +
> +    if (!(data = virJSONValueObjectGet(reply, "return"))) {
> +        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
> +                       _("malformed return value"));

does the GA report errors? If yes the 'return' object will not be
present but the error, where there might be more data than this.

> +        goto cleanup;
> +    }
> +
> +    if (VIR_STRDUP(*hostname,
> +                   virJSONValueObjectGetString(data, "host-name")) < 0) {
> +        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
> +                       _("'host-name' missing in reply of guest-get-host-name"));

This is wrong. VIR_STRDUP returns 0 if the 'src' argument was NULL. So
this would effectively report the error only on OOM error where
VIR_STRDUP reports it's own error.

> +        goto cleanup;
> +    }
--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list