diff mbox series

[PoCv2,12/15] qga: implement get-host-name in Rust

Message ID 20201011203513.1621355-13-marcandre.lureau@redhat.com
State New
Headers show
Series Rust binding for QAPI (qemu-ga only, for now) | expand

Commit Message

Marc-André Lureau Oct. 11, 2020, 8:35 p.m. UTC
From: Marc-André Lureau <marcandre.lureau@redhat.com>

Use the "hostname" crate (https://github.com/svartalf/hostname)

(notice the wrong error message in our win32 implementation)

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
---
 include/qemu/osdep.h | 10 ----------
 qga/Cargo.toml       |  1 +
 qga/commands.c       | 20 ++++----------------
 qga/lib.rs           |  2 ++
 qga/qmp/hostname.rs  |  9 +++++++++
 qga/qmp/mod.rs       |  7 +++++++
 tests/test-qga.c     |  2 ++
 util/oslib-posix.c   | 35 -----------------------------------
 util/oslib-win32.c   | 13 -------------
 9 files changed, 25 insertions(+), 74 deletions(-)
 create mode 100644 qga/qmp/hostname.rs
diff mbox series

Patch

diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h
index f9ec8c84e9..1ea244fc06 100644
--- a/include/qemu/osdep.h
+++ b/include/qemu/osdep.h
@@ -664,16 +664,6 @@  static inline void qemu_reset_optind(void)
 #endif
 }
 
-/**
- * qemu_get_host_name:
- * @errp: Error object
- *
- * Operating system agnostic way of querying host name.
- *
- * Returns allocated hostname (caller should free), NULL on failure.
- */
-char *qemu_get_host_name(Error **errp);
-
 /**
  * qemu_get_host_physmem:
  *
diff --git a/qga/Cargo.toml b/qga/Cargo.toml
index 9966057594..63a419255d 100644
--- a/qga/Cargo.toml
+++ b/qga/Cargo.toml
@@ -7,6 +7,7 @@  license = "GPLv2"
 [dependencies]
 common = { path = "../rust/common" }
 libc = "^0.2.76"
+hostname = "^0.3.1"
 
 [lib]
 path = "lib.rs"
diff --git a/qga/commands.c b/qga/commands.c
index 3dcd5fbe5c..15478a16e7 100644
--- a/qga/commands.c
+++ b/qga/commands.c
@@ -512,25 +512,13 @@  int ga_parse_whence(GuestFileWhence *whence, Error **errp)
     return -1;
 }
 
+#ifndef CONFIG_WITH_RUST
 GuestHostName *qmp_guest_get_host_name(Error **errp)
 {
-    GuestHostName *result = NULL;
-    g_autofree char *hostname = qemu_get_host_name(errp);
-
-    /*
-     * We want to avoid using g_get_host_name() because that
-     * caches the result and we wouldn't reflect changes in the
-     * host name.
-     */
-
-    if (!hostname) {
-        hostname = g_strdup("localhost");
-    }
-
-    result = g_new0(GuestHostName, 1);
-    result->host_name = g_steal_pointer(&hostname);
-    return result;
+    error_setg(errp, QERR_UNSUPPORTED);
+    return NULL;
 }
+#endif
 
 GuestTimezone *qmp_guest_get_timezone(Error **errp)
 {
diff --git a/qga/lib.rs b/qga/lib.rs
index 5fe08c25a3..f4967f59e5 100644
--- a/qga/lib.rs
+++ b/qga/lib.rs
@@ -1,3 +1,5 @@ 
+pub use common::{err, Error, Result};
+
 mod qapi;
 mod qapi_sys;
 mod qmp;
diff --git a/qga/qmp/hostname.rs b/qga/qmp/hostname.rs
new file mode 100644
index 0000000000..c3eb1f6fd2
--- /dev/null
+++ b/qga/qmp/hostname.rs
@@ -0,0 +1,9 @@ 
+use crate::*;
+
+pub(crate) fn get() -> Result<qapi::GuestHostName> {
+    let host_name = hostname::get()?
+        .into_string()
+        .or_else(|_| err!("Invalid hostname"))?;
+
+    Ok(qapi::GuestHostName { host_name })
+}
diff --git a/qga/qmp/mod.rs b/qga/qmp/mod.rs
index 38060100af..e855aa4bd7 100644
--- a/qga/qmp/mod.rs
+++ b/qga/qmp/mod.rs
@@ -34,3 +34,10 @@  macro_rules! qmp {
         }
     }};
 }
+
+mod hostname;
+
+#[no_mangle]
+extern "C" fn qmp_guest_get_host_name(errp: *mut *mut sys::Error) -> *mut qapi_sys::GuestHostName {
+    qmp!(hostname::get(), errp)
+}
diff --git a/tests/test-qga.c b/tests/test-qga.c
index c1b173b3cb..6190e93e0e 100644
--- a/tests/test-qga.c
+++ b/tests/test-qga.c
@@ -863,6 +863,7 @@  static void test_qga_guest_exec_invalid(gconstpointer fix)
 
 static void test_qga_guest_get_host_name(gconstpointer fix)
 {
+#ifdef CONFIG_WITH_RUST
     const TestFixture *fixture = fix;
     QDict *ret, *val;
 
@@ -874,6 +875,7 @@  static void test_qga_guest_get_host_name(gconstpointer fix)
     g_assert(qdict_haskey(val, "host-name"));
 
     qobject_unref(ret);
+#endif
 }
 
 static void test_qga_guest_get_timezone(gconstpointer fix)
diff --git a/util/oslib-posix.c b/util/oslib-posix.c
index f15234b5c0..1722c269b8 100644
--- a/util/oslib-posix.c
+++ b/util/oslib-posix.c
@@ -804,41 +804,6 @@  void sigaction_invoke(struct sigaction *action,
     action->sa_sigaction(info->ssi_signo, &si, NULL);
 }
 
-#ifndef HOST_NAME_MAX
-# ifdef _POSIX_HOST_NAME_MAX
-#  define HOST_NAME_MAX _POSIX_HOST_NAME_MAX
-# else
-#  define HOST_NAME_MAX 255
-# endif
-#endif
-
-char *qemu_get_host_name(Error **errp)
-{
-    long len = -1;
-    g_autofree char *hostname = NULL;
-
-#ifdef _SC_HOST_NAME_MAX
-    len = sysconf(_SC_HOST_NAME_MAX);
-#endif /* _SC_HOST_NAME_MAX */
-
-    if (len < 0) {
-        len = HOST_NAME_MAX;
-    }
-
-    /* Unfortunately, gethostname() below does not guarantee a
-     * NULL terminated string. Therefore, allocate one byte more
-     * to be sure. */
-    hostname = g_new0(char, len + 1);
-
-    if (gethostname(hostname, len) < 0) {
-        error_setg_errno(errp, errno,
-                         "cannot get hostname");
-        return NULL;
-    }
-
-    return g_steal_pointer(&hostname);
-}
-
 size_t qemu_get_host_physmem(void)
 {
 #ifdef _SC_PHYS_PAGES
diff --git a/util/oslib-win32.c b/util/oslib-win32.c
index 051afb217b..804e303c4f 100644
--- a/util/oslib-win32.c
+++ b/util/oslib-win32.c
@@ -822,19 +822,6 @@  bool qemu_write_pidfile(const char *filename, Error **errp)
     return true;
 }
 
-char *qemu_get_host_name(Error **errp)
-{
-    wchar_t tmp[MAX_COMPUTERNAME_LENGTH + 1];
-    DWORD size = G_N_ELEMENTS(tmp);
-
-    if (GetComputerNameW(tmp, &size) == 0) {
-        error_setg_win32(errp, GetLastError(), "failed close handle");
-        return NULL;
-    }
-
-    return g_utf16_to_utf8(tmp, size, NULL, NULL, NULL);
-}
-
 size_t qemu_get_host_physmem(void)
 {
     MEMORYSTATUSEX statex;