From: Marc-André Lureau <marcandre.lur...@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.lur...@redhat.com> --- include/qemu/osdep.h | 10 ---------- qga/commands.c | 20 ++++---------------- tests/unit/test-qga.c | 2 ++ util/oslib-posix.c | 35 ----------------------------------- util/oslib-win32.c | 13 ------------- Cargo.lock | 40 ++++++++++++++++++++++++++++++++++++++++ qga/Cargo.toml | 1 + qga/lib.rs | 3 +++ qga/qmp/hostname.rs | 9 +++++++++ qga/qmp/mod.rs | 10 ++++++++++ 10 files changed, 69 insertions(+), 74 deletions(-) create mode 100644 qga/qmp/hostname.rs create mode 100644 qga/qmp/mod.rs diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h index 60718fc342..ca8f3465d2 100644 --- a/include/qemu/osdep.h +++ b/include/qemu/osdep.h @@ -740,16 +740,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/commands.c b/qga/commands.c index 80501e4a73..117c219ac4 100644 --- a/qga/commands.c +++ b/qga/commands.c @@ -508,25 +508,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/tests/unit/test-qga.c b/tests/unit/test-qga.c index 5cb140d1b5..a22ab8c82c 100644 --- a/tests/unit/test-qga.c +++ b/tests/unit/test-qga.c @@ -864,6 +864,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; @@ -875,6 +876,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 e8bdb02e1d..1498e0b7f5 100644 --- a/util/oslib-posix.c +++ b/util/oslib-posix.c @@ -809,41 +809,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 af559ef339..575f697815 100644 --- a/util/oslib-win32.c +++ b/util/oslib-win32.c @@ -629,19 +629,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; diff --git a/Cargo.lock b/Cargo.lock index ad5bb47762..8752dbf2ac 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -34,12 +34,29 @@ dependencies = [ "nix", ] +[[package]] +name = "hostname" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c731c3e10504cc8ed35cfe2f1db4c9274c3d35fa486e3b31df46f068ef3e867" +dependencies = [ + "libc", + "match_cfg", + "winapi", +] + [[package]] name = "libc" version = "0.2.101" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3cb00336871be5ed2c8ed44b60ae9959dc5b9f08539422ed43f09e34ecaeba21" +[[package]] +name = "match_cfg" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ffbee8634e0d45d258acb448e7eaab3fce7a0a467395d4d9f228e3c1f01fb2e4" + [[package]] name = "memoffset" version = "0.6.4" @@ -74,4 +91,27 @@ name = "qga" version = "0.1.0" dependencies = [ "common", + "hostname", ] + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/qga/Cargo.toml b/qga/Cargo.toml index d262b847fa..387af59a30 100644 --- a/qga/Cargo.toml +++ b/qga/Cargo.toml @@ -6,6 +6,7 @@ publish = false [dependencies] common = { path = "../rust/common" } +hostname = "0.3.1" [lib] path = "lib.rs" diff --git a/qga/lib.rs b/qga/lib.rs index 7f62788ff6..e51e211006 100644 --- a/qga/lib.rs +++ b/qga/lib.rs @@ -1,2 +1,5 @@ +pub use common::{err, libc, Error, Result}; + mod qapi; mod qapi_ffi; +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 new file mode 100644 index 0000000000..c192e4247d --- /dev/null +++ b/qga/qmp/mod.rs @@ -0,0 +1,10 @@ +use common::*; + +use crate::qapi_ffi; + +mod hostname; + +#[no_mangle] +extern "C" fn qmp_guest_get_host_name(errp: *mut *mut ffi::Error) -> *mut qapi_ffi::GuestHostName { + qmp!(hostname::get(), errp) +} -- 2.33.0.113.g6c40894d24