@@ -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:
*
@@ -7,6 +7,7 @@ license = "GPLv2"
[dependencies]
common = { path = "../rust/common" }
libc = "^0.2.76"
+hostname = "^0.3.1"
[lib]
path = "lib.rs"
@@ -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)
{
@@ -1,3 +1,5 @@
+pub use common::{err, Error, Result};
+
mod qapi;
mod qapi_sys;
mod qmp;
new file mode 100644
@@ -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 })
+}
@@ -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)
+}
@@ -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)
@@ -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
@@ -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;