diff options
author | Marc-André Lureau | 2022-04-20 15:26:14 +0200 |
---|---|---|
committer | Marc-André Lureau | 2022-04-21 15:09:09 +0200 |
commit | 548fb0da730072d8c96b40ce29b024dd16cbab92 (patch) | |
tree | 97f39a8685323b6050c51d545299de81b1be1290 | |
parent | Move error_printf_unless_qmp() with monitor unit (diff) | |
download | qemu-548fb0da730072d8c96b40ce29b024dd16cbab92.tar.gz qemu-548fb0da730072d8c96b40ce29b024dd16cbab92.tar.xz qemu-548fb0da730072d8c96b40ce29b024dd16cbab92.zip |
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>
-rw-r--r-- | include/qemu/osdep.h | 10 | ||||
-rw-r--r-- | qga/commands-common.h | 11 | ||||
-rw-r--r-- | qga/commands-posix.c | 35 | ||||
-rw-r--r-- | qga/commands-win32.c | 13 | ||||
-rw-r--r-- | qga/commands.c | 2 | ||||
-rw-r--r-- | util/oslib-posix.c | 35 | ||||
-rw-r--r-- | util/oslib-win32.c | 13 |
7 files changed, 60 insertions, 59 deletions
diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h index a87f1b7f32..4bf2883a60 100644 --- a/include/qemu/osdep.h +++ b/include/qemu/osdep.h @@ -657,16 +657,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: * * Operating system agnostic way of querying host memory. diff --git a/qga/commands-common.h b/qga/commands-common.h index 90785ed4bb..d0e4a9696f 100644 --- a/qga/commands-common.h +++ b/qga/commands-common.h @@ -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 diff --git a/qga/commands-posix.c b/qga/commands-posix.c index 390c1560e1..77f4672ca2 100644 --- a/qga/commands-posix.c +++ b/qga/commands-posix.c @@ -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); +} diff --git a/qga/commands-win32.c b/qga/commands-win32.c index 406e4072a0..d56b5fd2a7 100644 --- a/qga/commands-win32.c +++ b/qga/commands-win32.c @@ -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); +} diff --git a/qga/commands.c b/qga/commands.c index 4e9ce25b2e..690da0073d 100644 --- a/qga/commands.c +++ b/qga/commands.c @@ -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 diff --git a/util/oslib-posix.c b/util/oslib-posix.c index 161f112325..fd2bdc9ac7 100644 --- a/util/oslib-posix.c +++ b/util/oslib-posix.c @@ -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 diff --git a/util/oslib-win32.c b/util/oslib-win32.c index 1e05c316b3..b897d75936 100644 --- a/util/oslib-win32.c +++ b/util/oslib-win32.c @@ -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; |