util: Introduce qemu_get_host_name()

This function offers operating system agnostic way to fetch host
name. It is implemented for both POSIX-like and Windows systems.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
Cc: qemu-stable@nongnu.org
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
This commit is contained in:
Michal Privoznik 2020-06-22 20:19:35 +02:00 committed by Michael Roth
parent 844bd70b56
commit e47f4765af
3 changed files with 58 additions and 0 deletions

View file

@ -808,3 +808,16 @@ 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);
}