qga: move qga_get_host_name()

The function is specific to qemu-ga, no need to share it in QEMU.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
Reviewed-by: Konstantin Kostiuk <kkostiuk@redhat.com>
Message-Id: <20220420132624.2439741-32-marcandre.lureau@redhat.com>
This commit is contained in:
Marc-André Lureau 2022-04-20 17:26:14 +04:00
parent 756a98dd70
commit 548fb0da73
7 changed files with 60 additions and 59 deletions

View File

@ -656,16 +656,6 @@ int qemu_fdatasync(int fd);
*/
int qemu_msync(void *addr, size_t length, int fd);
/**
* 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:
*

View File

@ -18,4 +18,15 @@ GuestFileHandle *guest_file_handle_find(int64_t id, Error **errp);
GuestFileRead *guest_file_read_unsafe(GuestFileHandle *gfh,
int64_t count, Error **errp);
/**
* qga_get_host_name:
* @errp: Error object
*
* Operating system agnostic way of querying host name.
* Compared to g_get_host_name(), it doesn't cache the result.
*
* Returns allocated hostname (caller should free), NULL on failure.
*/
char *qga_get_host_name(Error **errp);
#endif

View File

@ -3278,3 +3278,38 @@ GuestDeviceInfoList *qmp_guest_get_devices(Error **errp)
return 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 *qga_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);
}

View File

@ -2519,3 +2519,16 @@ GuestDeviceInfoList *qmp_guest_get_devices(Error **errp)
}
return head;
}
char *qga_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);
}

View File

@ -511,7 +511,7 @@ int ga_parse_whence(GuestFileWhence *whence, Error **errp)
GuestHostName *qmp_guest_get_host_name(Error **errp)
{
GuestHostName *result = NULL;
g_autofree char *hostname = qemu_get_host_name(errp);
g_autofree char *hostname = qga_get_host_name(errp);
/*
* We want to avoid using g_get_host_name() because that

View File

@ -886,41 +886,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

View File

@ -573,19 +573,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;