Skip to content

Commit c920fdb

Browse files
committed
Merge remote-tracking branch 'remotes/mdroth/tags/qga-pull-2020-07-13-tag' into staging
qemu-ga patch queue for hard-freeze * fix erroneously reporting stale hostname in guest-get-host-name * fix regression where guest-shutdown asserts when called * fix race condition with guest-fs-freeze/thaw on w32 # gpg: Signature made Tue 14 Jul 2020 05:47:11 BST # gpg: using RSA key CEACC9E15534EBABB82D3FA03353C9CEF108B584 # gpg: issuer "[email protected]" # gpg: Good signature from "Michael Roth <[email protected]>" [full] # gpg: aka "Michael Roth <[email protected]>" [full] # gpg: aka "Michael Roth <[email protected]>" [full] # Primary key fingerprint: CEAC C9E1 5534 EBAB B82D 3FA0 3353 C9CE F108 B584 * remotes/mdroth/tags/qga-pull-2020-07-13-tag: qga: Use qemu_get_host_name() instead of g_get_host_name() util: Introduce qemu_get_host_name() qga: fix assert regression on guest-shutdown qga-win: Fix QGA VSS Provider service stop failure Signed-off-by: Peter Maydell <[email protected]>
2 parents 8bfa25a + 0d3a8f3 commit c920fdb

File tree

6 files changed

+96
-18
lines changed

6 files changed

+96
-18
lines changed

include/qemu/osdep.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,4 +655,14 @@ static inline void qemu_reset_optind(void)
655655
#endif
656656
}
657657

658+
/**
659+
* qemu_get_host_name:
660+
* @errp: Error object
661+
*
662+
* Operating system agnostic way of querying host name.
663+
*
664+
* Returns allocated hostname (caller should free), NULL on failure.
665+
*/
666+
char *qemu_get_host_name(Error **errp);
667+
658668
#endif

qga/commands.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -515,11 +515,20 @@ int ga_parse_whence(GuestFileWhence *whence, Error **errp)
515515
GuestHostName *qmp_guest_get_host_name(Error **errp)
516516
{
517517
GuestHostName *result = NULL;
518-
gchar const *hostname = g_get_host_name();
519-
if (hostname != NULL) {
520-
result = g_new0(GuestHostName, 1);
521-
result->host_name = g_strdup(hostname);
518+
g_autofree char *hostname = qemu_get_host_name(errp);
519+
520+
/*
521+
* We want to avoid using g_get_host_name() because that
522+
* caches the result and we wouldn't reflect changes in the
523+
* host name.
524+
*/
525+
526+
if (!hostname) {
527+
hostname = g_strdup("localhost");
522528
}
529+
530+
result = g_new0(GuestHostName, 1);
531+
result->host_name = g_steal_pointer(&hostname);
523532
return result;
524533
}
525534

qga/main.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,11 @@ static int send_response(GAState *s, const QDict *rsp)
531531
QString *payload_qstr, *response_qstr;
532532
GIOStatus status;
533533

534-
g_assert(rsp && s->channel);
534+
g_assert(s->channel);
535+
536+
if (!rsp) {
537+
return 0;
538+
}
535539

536540
payload_qstr = qobject_to_json(QOBJECT(rsp));
537541
if (!payload_qstr) {

qga/vss-win32/install.cpp

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <comdef.h>
2020
#include <comutil.h>
2121
#include <sddl.h>
22+
#include <winsvc.h>
2223

2324
#define BUFFER_SIZE 1024
2425

@@ -509,26 +510,32 @@ namespace _com_util
509510
}
510511
}
511512

512-
/* Stop QGA VSS provider service from COM+ Application Admin Catalog */
513-
513+
/* Stop QGA VSS provider service using Winsvc API */
514514
STDAPI StopService(void)
515515
{
516516
HRESULT hr;
517-
COMInitializer initializer;
518-
COMPointer<IUnknown> pUnknown;
519-
COMPointer<ICOMAdminCatalog2> pCatalog;
517+
SC_HANDLE manager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
518+
SC_HANDLE service = NULL;
520519

521-
int count = 0;
520+
if (!manager) {
521+
errmsg(E_FAIL, "Failed to open service manager");
522+
hr = E_FAIL;
523+
goto out;
524+
}
525+
service = OpenService(manager, QGA_PROVIDER_NAME, SC_MANAGER_ALL_ACCESS);
522526

523-
chk(QGAProviderFind(QGAProviderCount, (void *)&count));
524-
if (count) {
525-
chk(CoCreateInstance(CLSID_COMAdminCatalog, NULL, CLSCTX_INPROC_SERVER,
526-
IID_IUnknown, (void **)pUnknown.replace()));
527-
chk(pUnknown->QueryInterface(IID_ICOMAdminCatalog2,
528-
(void **)pCatalog.replace()));
529-
chk(pCatalog->ShutdownApplication(_bstr_t(QGA_PROVIDER_LNAME)));
527+
if (!service) {
528+
errmsg(E_FAIL, "Failed to open service");
529+
hr = E_FAIL;
530+
goto out;
531+
}
532+
if (!(ControlService(service, SERVICE_CONTROL_STOP, NULL))) {
533+
errmsg(E_FAIL, "Failed to stop service");
534+
hr = E_FAIL;
530535
}
531536

532537
out:
538+
CloseServiceHandle(service);
539+
CloseServiceHandle(manager);
533540
return hr;
534541
}

util/oslib-posix.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -794,3 +794,38 @@ void sigaction_invoke(struct sigaction *action,
794794
}
795795
action->sa_sigaction(info->ssi_signo, &si, NULL);
796796
}
797+
798+
#ifndef HOST_NAME_MAX
799+
# ifdef _POSIX_HOST_NAME_MAX
800+
# define HOST_NAME_MAX _POSIX_HOST_NAME_MAX
801+
# else
802+
# define HOST_NAME_MAX 255
803+
# endif
804+
#endif
805+
806+
char *qemu_get_host_name(Error **errp)
807+
{
808+
long len = -1;
809+
g_autofree char *hostname = NULL;
810+
811+
#ifdef _SC_HOST_NAME_MAX
812+
len = sysconf(_SC_HOST_NAME_MAX);
813+
#endif /* _SC_HOST_NAME_MAX */
814+
815+
if (len < 0) {
816+
len = HOST_NAME_MAX;
817+
}
818+
819+
/* Unfortunately, gethostname() below does not guarantee a
820+
* NULL terminated string. Therefore, allocate one byte more
821+
* to be sure. */
822+
hostname = g_new0(char, len + 1);
823+
824+
if (gethostname(hostname, len) < 0) {
825+
error_setg_errno(errp, errno,
826+
"cannot get hostname");
827+
return NULL;
828+
}
829+
830+
return g_steal_pointer(&hostname);
831+
}

util/oslib-win32.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -808,3 +808,16 @@ bool qemu_write_pidfile(const char *filename, Error **errp)
808808
}
809809
return true;
810810
}
811+
812+
char *qemu_get_host_name(Error **errp)
813+
{
814+
wchar_t tmp[MAX_COMPUTERNAME_LENGTH + 1];
815+
DWORD size = G_N_ELEMENTS(tmp);
816+
817+
if (GetComputerNameW(tmp, &size) == 0) {
818+
error_setg_win32(errp, GetLastError(), "failed close handle");
819+
return NULL;
820+
}
821+
822+
return g_utf16_to_utf8(tmp, size, NULL, NULL, NULL);
823+
}

0 commit comments

Comments
 (0)