[13659] | 1 | /* $Id: VirtualBoxClientImpl.cpp 73003 2018-07-09 11:09:32Z vboxsync $ */
|
---|
[1] | 2 | /** @file
|
---|
| 3 | * VirtualBox COM class implementation
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[69500] | 7 | * Copyright (C) 2010-2017 Oracle Corporation
|
---|
[1] | 8 | *
|
---|
| 9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
| 10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
| 11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
[5999] | 12 | * General Public License (GPL) as published by the Free Software
|
---|
| 13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
| 14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
| 15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
[1] | 16 | */
|
---|
| 17 |
|
---|
[67914] | 18 | #define LOG_GROUP LOG_GROUP_MAIN_VIRTUALBOXCLIENT
|
---|
| 19 | #include "LoggingNew.h"
|
---|
| 20 |
|
---|
[34417] | 21 | #include "VirtualBoxClientImpl.h"
|
---|
[25860] | 22 |
|
---|
| 23 | #include "AutoCaller.h"
|
---|
[34417] | 24 | #include "VBoxEvents.h"
|
---|
[44970] | 25 | #include "VBox/com/ErrorInfo.h"
|
---|
[1] | 26 |
|
---|
[34642] | 27 | #include <iprt/asm.h>
|
---|
[34417] | 28 | #include <iprt/thread.h>
|
---|
| 29 | #include <iprt/critsect.h>
|
---|
| 30 | #include <iprt/semaphore.h>
|
---|
[30681] | 31 | #include <iprt/cpp/utils.h>
|
---|
[66411] | 32 | #include <iprt/utf16.h>
|
---|
[63814] | 33 | #ifdef RT_OS_WINDOWS
|
---|
| 34 | # include <iprt/ldr.h>
|
---|
| 35 | # include <msi.h>
|
---|
[66411] | 36 | # include <WbemIdl.h>
|
---|
[63814] | 37 | #endif
|
---|
[30681] | 38 |
|
---|
[34417] | 39 |
|
---|
| 40 | /** Waiting time between probing whether VBoxSVC is alive. */
|
---|
| 41 | #define VBOXCLIENT_DEFAULT_INTERVAL 30000
|
---|
| 42 |
|
---|
| 43 |
|
---|
[34642] | 44 | /** Initialize instance counter class variable */
|
---|
| 45 | uint32_t VirtualBoxClient::g_cInstances = 0;
|
---|
| 46 |
|
---|
[63639] | 47 | LONG VirtualBoxClient::s_cUnnecessaryAtlModuleLocks = 0;
|
---|
[34642] | 48 |
|
---|
[1] | 49 | // constructor / destructor
|
---|
| 50 | /////////////////////////////////////////////////////////////////////////////
|
---|
| 51 |
|
---|
[34417] | 52 | HRESULT VirtualBoxClient::FinalConstruct()
|
---|
[1] | 53 | {
|
---|
[35638] | 54 | HRESULT rc = init();
|
---|
| 55 | BaseFinalConstruct();
|
---|
| 56 | return rc;
|
---|
[1] | 57 | }
|
---|
| 58 |
|
---|
[34417] | 59 | void VirtualBoxClient::FinalRelease()
|
---|
[1] | 60 | {
|
---|
[34417] | 61 | uninit();
|
---|
[35638] | 62 | BaseFinalRelease();
|
---|
[1] | 63 | }
|
---|
| 64 |
|
---|
[66274] | 65 |
|
---|
[1] | 66 | // public initializer/uninitializer for internal purposes only
|
---|
| 67 | /////////////////////////////////////////////////////////////////////////////
|
---|
| 68 |
|
---|
| 69 | /**
|
---|
[34417] | 70 | * Initializes the VirtualBoxClient object.
|
---|
[1] | 71 | *
|
---|
| 72 | * @returns COM result indicator
|
---|
| 73 | */
|
---|
[34417] | 74 | HRESULT VirtualBoxClient::init()
|
---|
[1] | 75 | {
|
---|
[66274] | 76 |
|
---|
[66563] | 77 | #if defined(RT_OS_WINDOWS) && defined(VBOX_WITH_SDS)
|
---|
[66274] | 78 | // setup COM Security to enable impersonation
|
---|
[69729] | 79 | // This works for console VirtualBox clients, GUI has own security settings
|
---|
[71179] | 80 | // For GUI VirtualBox it will be second call so can return TOO_LATE error
|
---|
[66274] | 81 | HRESULT hrGUICoInitializeSecurity = CoInitializeSecurity(NULL,
|
---|
[66279] | 82 | -1,
|
---|
| 83 | NULL,
|
---|
| 84 | NULL,
|
---|
| 85 | RPC_C_AUTHN_LEVEL_DEFAULT,
|
---|
| 86 | RPC_C_IMP_LEVEL_IMPERSONATE,
|
---|
| 87 | NULL,
|
---|
| 88 | EOAC_NONE,
|
---|
| 89 | NULL);
|
---|
[66512] | 90 | NOREF(hrGUICoInitializeSecurity);
|
---|
[66274] | 91 | Assert(SUCCEEDED(hrGUICoInitializeSecurity) || hrGUICoInitializeSecurity == RPC_E_TOO_LATE);
|
---|
| 92 | #endif
|
---|
| 93 |
|
---|
[60065] | 94 | LogFlowThisFuncEnter();
|
---|
[1] | 95 |
|
---|
[13659] | 96 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
[21878] | 97 | AutoInitSpan autoInitSpan(this);
|
---|
| 98 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
[13659] | 99 |
|
---|
[60065] | 100 | /* Important: DO NOT USE any kind of "early return" (except the single
|
---|
| 101 | * one above, checking the init span success) in this method. It is vital
|
---|
| 102 | * for correct error handling that it has only one point of return, which
|
---|
| 103 | * does all the magic on COM to signal object creation success and
|
---|
| 104 | * reporting the error later for every API method. COM translates any
|
---|
| 105 | * unsuccessful object creation to REGDB_E_CLASSNOTREG errors or similar
|
---|
| 106 | * unhelpful ones which cause us a lot of grief with troubleshooting. */
|
---|
[34642] | 107 |
|
---|
[60065] | 108 | HRESULT rc = S_OK;
|
---|
| 109 | try
|
---|
| 110 | {
|
---|
| 111 | if (ASMAtomicIncU32(&g_cInstances) != 1)
|
---|
[69791] | 112 | AssertFailedStmt(throw setError(E_FAIL, tr("Attempted to create more than one VirtualBoxClient instance")));
|
---|
[34642] | 113 |
|
---|
[60065] | 114 | mData.m_ThreadWatcher = NIL_RTTHREAD;
|
---|
| 115 | mData.m_SemEvWatcher = NIL_RTSEMEVENT;
|
---|
[66279] | 116 |
|
---|
[60065] | 117 | rc = mData.m_pVirtualBox.createLocalObject(CLSID_VirtualBox);
|
---|
| 118 | if (FAILED(rc))
|
---|
[63814] | 119 | #ifdef RT_OS_WINDOWS
|
---|
| 120 | throw i_investigateVirtualBoxObjectCreationFailure(rc);
|
---|
| 121 | #else
|
---|
[60065] | 122 | throw rc;
|
---|
[63814] | 123 | #endif
|
---|
[59996] | 124 |
|
---|
[60065] | 125 | /* VirtualBox error return is postponed to method calls, fetch it. */
|
---|
| 126 | ULONG rev;
|
---|
| 127 | rc = mData.m_pVirtualBox->COMGETTER(Revision)(&rev);
|
---|
| 128 | if (FAILED(rc))
|
---|
| 129 | throw rc;
|
---|
[1] | 130 |
|
---|
[60065] | 131 | rc = unconst(mData.m_pEventSource).createObject();
|
---|
[69791] | 132 | AssertComRCThrow(rc, setError(rc, tr("Could not create EventSource for VirtualBoxClient")));
|
---|
[60065] | 133 | rc = mData.m_pEventSource->init();
|
---|
[69791] | 134 | AssertComRCThrow(rc, setError(rc, tr("Could not initialize EventSource for VirtualBoxClient")));
|
---|
[60065] | 135 |
|
---|
[63639] | 136 | /* HACK ALERT! This is for DllCanUnloadNow(). */
|
---|
| 137 | s_cUnnecessaryAtlModuleLocks++;
|
---|
| 138 | AssertMsg(s_cUnnecessaryAtlModuleLocks == 1, ("%d\n", s_cUnnecessaryAtlModuleLocks));
|
---|
| 139 |
|
---|
[60065] | 140 | /* Setting up the VBoxSVC watcher thread. If anything goes wrong here it
|
---|
| 141 | * is not considered important enough to cause any sort of visible
|
---|
| 142 | * failure. The monitoring will not be done, but that's all. */
|
---|
| 143 | int vrc = RTSemEventCreate(&mData.m_SemEvWatcher);
|
---|
| 144 | if (RT_FAILURE(vrc))
|
---|
| 145 | {
|
---|
| 146 | mData.m_SemEvWatcher = NIL_RTSEMEVENT;
|
---|
[73003] | 147 | AssertRCStmt(vrc, throw setErrorBoth(VBOX_E_IPRT_ERROR, vrc, tr("Failed to create semaphore (rc=%Rrc)"), vrc));
|
---|
[60065] | 148 | }
|
---|
| 149 |
|
---|
| 150 | vrc = RTThreadCreate(&mData.m_ThreadWatcher, SVCWatcherThread, this, 0,
|
---|
| 151 | RTTHREADTYPE_INFREQUENT_POLLER, RTTHREADFLAGS_WAITABLE, "VBoxSVCWatcher");
|
---|
| 152 | if (RT_FAILURE(vrc))
|
---|
| 153 | {
|
---|
| 154 | RTSemEventDestroy(mData.m_SemEvWatcher);
|
---|
| 155 | mData.m_SemEvWatcher = NIL_RTSEMEVENT;
|
---|
[73003] | 156 | AssertRCStmt(vrc, throw setErrorBoth(VBOX_E_IPRT_ERROR, vrc, tr("Failed to create watcher thread (rc=%Rrc)"), vrc));
|
---|
[60065] | 157 | }
|
---|
| 158 | }
|
---|
| 159 | catch (HRESULT err)
|
---|
[34417] | 160 | {
|
---|
[60065] | 161 | /* we assume that error info is set by the thrower */
|
---|
| 162 | rc = err;
|
---|
[34417] | 163 | }
|
---|
[60065] | 164 | catch (...)
|
---|
[34417] | 165 | {
|
---|
[60065] | 166 | rc = VirtualBoxBase::handleUnexpectedExceptions(this, RT_SRC_POS);
|
---|
[34417] | 167 | }
|
---|
[1] | 168 |
|
---|
[60546] | 169 | /* Confirm a successful initialization when it's the case. Must be last,
|
---|
[60065] | 170 | * as on failure it will uninitialize the object. */
|
---|
| 171 | if (SUCCEEDED(rc))
|
---|
| 172 | autoInitSpan.setSucceeded();
|
---|
| 173 | else
|
---|
| 174 | autoInitSpan.setFailed(rc);
|
---|
[13659] | 175 |
|
---|
[61117] | 176 | LogFlowThisFunc(("rc=%Rhrc\n", rc));
|
---|
[60065] | 177 | LogFlowThisFuncLeave();
|
---|
| 178 | /* Unconditionally return success, because the error return is delayed to
|
---|
| 179 | * the attribute/method calls through the InitFailed object state. */
|
---|
| 180 | return S_OK;
|
---|
[1] | 181 | }
|
---|
| 182 |
|
---|
[63814] | 183 | #ifdef RT_OS_WINDOWS
|
---|
[69791] | 184 |
|
---|
[13659] | 185 | /**
|
---|
[63814] | 186 | * Looks into why we failed to create the VirtualBox object.
|
---|
| 187 | *
|
---|
| 188 | * @returns hrcCaller thru setError.
|
---|
| 189 | * @param hrcCaller The failure status code.
|
---|
| 190 | */
|
---|
| 191 | HRESULT VirtualBoxClient::i_investigateVirtualBoxObjectCreationFailure(HRESULT hrcCaller)
|
---|
| 192 | {
|
---|
[69792] | 193 | HRESULT hrc;
|
---|
| 194 |
|
---|
[69791] | 195 | # ifdef VBOX_WITH_SDS
|
---|
[66629] | 196 | /*
|
---|
[69791] | 197 | * Check that the VBoxSDS service is configured to run as LocalSystem and is enabled.
|
---|
| 198 | */
|
---|
[69804] | 199 | WCHAR wszBuffer[256];
|
---|
| 200 | uint32_t uStartType;
|
---|
| 201 | int vrc = i_getServiceAccountAndStartType(L"VBoxSDS", wszBuffer, RT_ELEMENTS(wszBuffer), &uStartType);
|
---|
[69791] | 202 | if (RT_SUCCESS(vrc))
|
---|
| 203 | {
|
---|
[69804] | 204 | LogRelFunc(("VBoxSDS service is running under the '%ls' account with start type %u.\n", wszBuffer, uStartType));
|
---|
[69791] | 205 | if (RTUtf16Cmp(wszBuffer, L"LocalSystem") != 0)
|
---|
| 206 | return setError(hrcCaller,
|
---|
[69804] | 207 | tr("VBoxSDS is misconfigured to run under the '%ls' account instead of the SYSTEM one.\n"
|
---|
[70585] | 208 | "Reinstall VirtualBox to fix it. Alternatively you can fix it using the Windows Service Control "
|
---|
| 209 | "Manager or by running 'qc config VBoxSDS obj=LocalSystem' on a command line."), wszBuffer);
|
---|
[69804] | 210 | if (uStartType == SERVICE_DISABLED)
|
---|
| 211 | return setError(hrcCaller,
|
---|
| 212 | tr("The VBoxSDS windows service is disabled.\n"
|
---|
[70585] | 213 | "Reinstall VirtualBox to fix it. Alternatively try reenable the service by setting it to "
|
---|
| 214 | " 'Manual' startup type in the Windows Service management console, or by runing "
|
---|
| 215 | "'sc config VBoxSDS start=demand' on the command line."));
|
---|
[69791] | 216 | }
|
---|
[70585] | 217 | else if (vrc == VERR_NOT_FOUND)
|
---|
| 218 | return setError(hrcCaller,
|
---|
| 219 | tr("The VBoxSDS windows service was not found.\n"
|
---|
| 220 | "Reinstall VirtualBox to fix it. Alternatively you can try start VirtualBox as Administrator, this "
|
---|
| 221 | "should automatically reinstall the service, or you can run "
|
---|
| 222 | "'VBoxSDS.exe --regservice' command from an elevated Administrator command line."));
|
---|
[69791] | 223 | else
|
---|
| 224 | LogRelFunc(("VirtualBoxClient::i_getServiceAccount failed: %Rrc\n", vrc));
|
---|
[69804] | 225 | # endif
|
---|
[69791] | 226 |
|
---|
| 227 | /*
|
---|
[63814] | 228 | * First step is to try get an IUnknown interface of the VirtualBox object.
|
---|
| 229 | *
|
---|
| 230 | * This will succeed even when oleaut32.msm (see @bugref{8016}, @ticketref{12087})
|
---|
| 231 | * is accidentally installed and messes up COM. It may also succeed when the COM
|
---|
| 232 | * registration is partially broken (though that's unlikely to happen these days).
|
---|
| 233 | */
|
---|
| 234 | IUnknown *pUnknown = NULL;
|
---|
[69792] | 235 | hrc = CoCreateInstance(CLSID_VirtualBox, NULL, CLSCTX_LOCAL_SERVER, IID_IUnknown, (void **)&pUnknown);
|
---|
[63814] | 236 | if (FAILED(hrc))
|
---|
| 237 | {
|
---|
| 238 | if (hrc == hrcCaller)
|
---|
| 239 | return setError(hrcCaller, tr("Completely failed to instantiate CLSID_VirtualBox: %Rhrc"), hrcCaller);
|
---|
| 240 | return setError(hrcCaller, tr("Completely failed to instantiate CLSID_VirtualBox: %Rhrc & %Rhrc"), hrcCaller, hrc);
|
---|
| 241 | }
|
---|
| 242 |
|
---|
| 243 | /*
|
---|
| 244 | * Try query the IVirtualBox interface (should fail), if it succeed we return
|
---|
| 245 | * straight away so we have more columns to spend on long messages below.
|
---|
| 246 | */
|
---|
| 247 | IVirtualBox *pVirtualBox;
|
---|
| 248 | hrc = pUnknown->QueryInterface(IID_IVirtualBox, (void **)&pVirtualBox);
|
---|
| 249 | if (SUCCEEDED(hrc))
|
---|
| 250 | {
|
---|
| 251 | pVirtualBox->Release();
|
---|
| 252 | pUnknown->Release();
|
---|
| 253 | return setError(hrcCaller,
|
---|
| 254 | tr("Failed to instantiate CLSID_VirtualBox the first time, but worked when checking out why ... weird"));
|
---|
| 255 | }
|
---|
| 256 |
|
---|
| 257 | /*
|
---|
| 258 | * Check for oleaut32.msm traces in the registry.
|
---|
| 259 | */
|
---|
| 260 | HKEY hKey;
|
---|
| 261 | LSTATUS lrc = RegOpenKeyExW(HKEY_CLASSES_ROOT, L"CLSID\\{00020420-0000-0000-C000-000000000046}\\InprocServer32",
|
---|
| 262 | 0 /*fFlags*/, KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS | STANDARD_RIGHTS_READ, &hKey);
|
---|
| 263 | if (lrc == ERROR_SUCCESS)
|
---|
| 264 | {
|
---|
| 265 | wchar_t wszBuf[8192];
|
---|
| 266 | DWORD cbBuf = sizeof(wszBuf) - sizeof(wchar_t);
|
---|
| 267 | DWORD dwType = 0;
|
---|
| 268 | lrc = RegQueryValueExW(hKey, L"InprocServer32", NULL /*pvReserved*/, &dwType, (BYTE *)&wszBuf[0], &cbBuf);
|
---|
| 269 | if (lrc == ERROR_SUCCESS)
|
---|
| 270 | {
|
---|
| 271 | wszBuf[cbBuf / sizeof(wchar_t)] = '\0';
|
---|
| 272 | bool fSetError = false;
|
---|
| 273 |
|
---|
| 274 | /*
|
---|
| 275 | * Try decode the string and improve the message.
|
---|
| 276 | */
|
---|
| 277 | typedef UINT (WINAPI *PFNMSIDECOMPOSEDESCRIPTORW)(PCWSTR pwszDescriptor,
|
---|
| 278 | LPWSTR pwszProductCode /*[40]*/,
|
---|
| 279 | LPWSTR pwszFeatureId /*[40]*/,
|
---|
| 280 | LPWSTR pwszComponentCode /*[40]*/,
|
---|
| 281 | DWORD *poffArguments);
|
---|
| 282 | PFNMSIDECOMPOSEDESCRIPTORW pfnMsiDecomposeDescriptorW;
|
---|
| 283 | pfnMsiDecomposeDescriptorW = (PFNMSIDECOMPOSEDESCRIPTORW)RTLdrGetSystemSymbol("msi.dll", "MsiDecomposeDescriptorW");
|
---|
| 284 | if ( pfnMsiDecomposeDescriptorW
|
---|
| 285 | && ( dwType == REG_SZ
|
---|
| 286 | || dwType == REG_MULTI_SZ))
|
---|
| 287 | {
|
---|
| 288 | wchar_t wszProductCode[RTUUID_STR_LENGTH + 2 + 16] = { 0 };
|
---|
| 289 | wchar_t wszFeatureId[RTUUID_STR_LENGTH + 2 + 16] = { 0 };
|
---|
| 290 | wchar_t wszComponentCode[RTUUID_STR_LENGTH + 2 + 16] = { 0 };
|
---|
| 291 | DWORD offArguments = ~(DWORD)0;
|
---|
| 292 | UINT uRc = pfnMsiDecomposeDescriptorW(wszBuf, wszProductCode, wszFeatureId, wszComponentCode, &offArguments);
|
---|
| 293 | if (uRc == 0)
|
---|
| 294 | {
|
---|
| 295 | /*
|
---|
| 296 | * Can we resolve the product code into a name?
|
---|
| 297 | */
|
---|
| 298 | typedef UINT (WINAPI *PFNMSIOPENPRODUCTW)(PCWSTR, MSIHANDLE *);
|
---|
| 299 | PFNMSIOPENPRODUCTW pfnMsiOpenProductW;
|
---|
| 300 | pfnMsiOpenProductW = (PFNMSIOPENPRODUCTW)RTLdrGetSystemSymbol("msi.dll", "MsiOpenProductW");
|
---|
| 301 |
|
---|
| 302 | typedef UINT (WINAPI *PFNMSICLOSEHANDLE)(MSIHANDLE);
|
---|
| 303 | PFNMSICLOSEHANDLE pfnMsiCloseHandle;
|
---|
| 304 | pfnMsiCloseHandle = (PFNMSICLOSEHANDLE)RTLdrGetSystemSymbol("msi.dll", "MsiCloseHandle");
|
---|
| 305 |
|
---|
| 306 | typedef UINT (WINAPI *PFNGETPRODUCTPROPERTYW)(MSIHANDLE, PCWSTR, PWSTR, PDWORD);
|
---|
| 307 | PFNGETPRODUCTPROPERTYW pfnMsiGetProductPropertyW;
|
---|
| 308 | pfnMsiGetProductPropertyW = (PFNGETPRODUCTPROPERTYW)RTLdrGetSystemSymbol("msi.dll", "MsiGetProductPropertyW");
|
---|
| 309 | if ( pfnMsiGetProductPropertyW
|
---|
| 310 | && pfnMsiCloseHandle
|
---|
| 311 | && pfnMsiOpenProductW)
|
---|
| 312 | {
|
---|
| 313 | MSIHANDLE hMsi = 0;
|
---|
| 314 | uRc = pfnMsiOpenProductW(wszProductCode, &hMsi);
|
---|
| 315 | if (uRc == 0)
|
---|
| 316 | {
|
---|
| 317 | static wchar_t const * const s_apwszProps[] =
|
---|
| 318 | {
|
---|
| 319 | INSTALLPROPERTY_INSTALLEDPRODUCTNAME,
|
---|
| 320 | INSTALLPROPERTY_PRODUCTNAME,
|
---|
| 321 | INSTALLPROPERTY_PACKAGENAME,
|
---|
| 322 | };
|
---|
| 323 |
|
---|
| 324 | wchar_t wszProductName[1024];
|
---|
| 325 | DWORD cwcProductName;
|
---|
| 326 | unsigned i = 0;
|
---|
| 327 | do
|
---|
| 328 | {
|
---|
| 329 | cwcProductName = RT_ELEMENTS(wszProductName) - 1;
|
---|
| 330 | uRc = pfnMsiGetProductPropertyW(hMsi, s_apwszProps[i], wszProductName, &cwcProductName);
|
---|
| 331 | }
|
---|
| 332 | while ( ++i < RT_ELEMENTS(s_apwszProps)
|
---|
| 333 | && ( uRc != 0
|
---|
| 334 | || cwcProductName < 2
|
---|
| 335 | || cwcProductName >= RT_ELEMENTS(wszProductName)) );
|
---|
| 336 | uRc = pfnMsiCloseHandle(hMsi);
|
---|
| 337 | if (uRc == 0 && cwcProductName >= 2)
|
---|
| 338 | {
|
---|
| 339 | wszProductName[RT_MIN(cwcProductName, RT_ELEMENTS(wszProductName) - 1)] = '\0';
|
---|
| 340 | setError(hrcCaller,
|
---|
| 341 | tr("Failed to instantiate CLSID_VirtualBox w/ IVirtualBox, but CLSID_VirtualBox w/ IUnknown works.\n"
|
---|
| 342 | "PSDispatch looks broken by the '%ls' (%ls) program, suspecting that it features the broken oleaut32.msm module as component %ls.\n"
|
---|
| 343 | "\n"
|
---|
| 344 | "We suggest you try uninstall '%ls'.\n"
|
---|
| 345 | "\n"
|
---|
| 346 | "See also https://support.microsoft.com/en-us/kb/316911 "),
|
---|
| 347 | wszProductName, wszProductCode, wszComponentCode, wszProductName);
|
---|
| 348 | fSetError = true;
|
---|
| 349 | }
|
---|
| 350 | }
|
---|
| 351 | }
|
---|
| 352 |
|
---|
| 353 | /* MSI uses COM and may mess up our stuff. So, we wait with the fallback till afterwards in this case. */
|
---|
| 354 | if (!fSetError)
|
---|
| 355 | {
|
---|
| 356 | setError(hrcCaller,
|
---|
| 357 | tr("Failed to instantiate CLSID_VirtualBox w/ IVirtualBox, CLSID_VirtualBox w/ IUnknown works.\n"
|
---|
| 358 | "PSDispatch looks broken by installer %ls featuring the broken oleaut32.msm module as component %ls.\n"
|
---|
| 359 | "\n"
|
---|
| 360 | "See also https://support.microsoft.com/en-us/kb/316911 "),
|
---|
| 361 | wszProductCode, wszComponentCode);
|
---|
| 362 | fSetError = true;
|
---|
| 363 | }
|
---|
| 364 | }
|
---|
| 365 | }
|
---|
| 366 | if (!fSetError)
|
---|
| 367 | setError(hrcCaller, tr("Failed to instantiate CLSID_VirtualBox w/ IVirtualBox, CLSID_VirtualBox w/ IUnknown works.\n"
|
---|
| 368 | "PSDispatch looks broken by some installer featuring the broken oleaut32.msm module as a component.\n"
|
---|
| 369 | "\n"
|
---|
| 370 | "See also https://support.microsoft.com/en-us/kb/316911 "));
|
---|
| 371 | }
|
---|
| 372 | else if (lrc == ERROR_FILE_NOT_FOUND)
|
---|
| 373 | setError(hrcCaller, tr("Failed to instantiate CLSID_VirtualBox w/ IVirtualBox, but CLSID_VirtualBox w/ IUnknown works.\n"
|
---|
| 374 | "PSDispatch looks fine. Weird"));
|
---|
| 375 | else
|
---|
| 376 | setError(hrcCaller, tr("Failed to instantiate CLSID_VirtualBox w/ IVirtualBox, but CLSID_VirtualBox w/ IUnknown works.\n"
|
---|
| 377 | "Checking out PSDispatch registration ended with error: %u (%#x)"), lrc, lrc);
|
---|
| 378 | RegCloseKey(hKey);
|
---|
| 379 | }
|
---|
| 380 |
|
---|
| 381 | pUnknown->Release();
|
---|
| 382 | return hrcCaller;
|
---|
| 383 | }
|
---|
[69791] | 384 |
|
---|
| 385 | # ifdef VBOX_WITH_SDS
|
---|
[69806] | 386 | /**
|
---|
| 387 | * Gets the service account name and start type for the given service.
|
---|
| 388 | *
|
---|
| 389 | * @returns IPRT status code (for some reason).
|
---|
| 390 | * @param pwszServiceName The name of the service.
|
---|
| 391 | * @param pwszAccountName Where to return the account name.
|
---|
| 392 | * @param cwcAccountName The length of the account name buffer (in WCHARs).
|
---|
| 393 | * @param puStartType Where to return the start type.
|
---|
| 394 | */
|
---|
[69804] | 395 | int VirtualBoxClient::i_getServiceAccountAndStartType(const wchar_t *pwszServiceName,
|
---|
| 396 | wchar_t *pwszAccountName, size_t cwcAccountName, uint32_t *puStartType)
|
---|
[69791] | 397 | {
|
---|
| 398 | AssertPtr(pwszServiceName);
|
---|
| 399 | AssertPtr(pwszAccountName);
|
---|
| 400 | Assert(cwcAccountName);
|
---|
| 401 | *pwszAccountName = '\0';
|
---|
[69804] | 402 | *puStartType = SERVICE_DEMAND_START;
|
---|
[69791] | 403 |
|
---|
| 404 | int vrc;
|
---|
| 405 |
|
---|
| 406 | // Get a handle to the SCM database.
|
---|
[69804] | 407 | SC_HANDLE hSCManager = OpenSCManagerW(NULL /*pwszMachineName*/, NULL /*pwszDatabaseName*/, SC_MANAGER_CONNECT);
|
---|
[69791] | 408 | if (hSCManager != NULL)
|
---|
| 409 | {
|
---|
| 410 | SC_HANDLE hService = OpenServiceW(hSCManager, pwszServiceName, SERVICE_QUERY_CONFIG);
|
---|
| 411 | if (hService != NULL)
|
---|
| 412 | {
|
---|
| 413 | DWORD cbNeeded = sizeof(QUERY_SERVICE_CONFIGW) + _1K;
|
---|
| 414 | if (!QueryServiceConfigW(hService, NULL, 0, &cbNeeded))
|
---|
| 415 | {
|
---|
| 416 | Assert(GetLastError() == ERROR_INSUFFICIENT_BUFFER);
|
---|
[69804] | 417 | LPQUERY_SERVICE_CONFIGW pSc = (LPQUERY_SERVICE_CONFIGW)RTMemTmpAllocZ(cbNeeded + _1K);
|
---|
[69791] | 418 | if (pSc)
|
---|
| 419 | {
|
---|
| 420 | DWORD cbNeeded2 = 0;
|
---|
[69804] | 421 | if (QueryServiceConfigW(hService, pSc, cbNeeded + _1K, &cbNeeded2))
|
---|
[69791] | 422 | {
|
---|
[69804] | 423 | *puStartType = pSc->dwStartType;
|
---|
[69791] | 424 | vrc = RTUtf16Copy(pwszAccountName, cwcAccountName, pSc->lpServiceStartName);
|
---|
| 425 | if (RT_FAILURE(vrc))
|
---|
| 426 | LogRel(("Error: SDS service name is too long (%Rrc): %ls\n", vrc, pSc->lpServiceStartName));
|
---|
| 427 | }
|
---|
| 428 | else
|
---|
| 429 | {
|
---|
| 430 | int dwError = GetLastError();
|
---|
| 431 | vrc = RTErrConvertFromWin32(dwError);
|
---|
[70296] | 432 | LogRel(("Error: Failed querying '%ls' service config: %Rwc (%u) -> %Rrc; cbNeeded=%d cbNeeded2=%d\n",
|
---|
[70585] | 433 | pwszServiceName, dwError, dwError, vrc, cbNeeded, cbNeeded2));
|
---|
[69791] | 434 | }
|
---|
| 435 | RTMemTmpFree(pSc);
|
---|
| 436 | }
|
---|
| 437 | else
|
---|
| 438 | {
|
---|
[69804] | 439 | LogRel(("Error: Failed allocating %#x bytes of memory for service config!\n", cbNeeded + _1K));
|
---|
[69791] | 440 | vrc = VERR_NO_TMP_MEMORY;
|
---|
| 441 | }
|
---|
| 442 | }
|
---|
| 443 | else
|
---|
| 444 | {
|
---|
| 445 | AssertLogRelMsgFailed(("Error: QueryServiceConfigW returns success with zero buffer!\n"));
|
---|
| 446 | vrc = VERR_IPE_UNEXPECTED_STATUS;
|
---|
| 447 | }
|
---|
| 448 | CloseServiceHandle(hService);
|
---|
| 449 | }
|
---|
| 450 | else
|
---|
| 451 | {
|
---|
| 452 | int dwError = GetLastError();
|
---|
| 453 | vrc = RTErrConvertFromWin32(dwError);
|
---|
[70296] | 454 | LogRel(("Error: Could not open service '%ls': %Rwc (%u) -> %Rrc\n", pwszServiceName, dwError, dwError, vrc));
|
---|
[69791] | 455 | }
|
---|
| 456 | CloseServiceHandle(hSCManager);
|
---|
| 457 | }
|
---|
| 458 | else
|
---|
| 459 | {
|
---|
| 460 | int dwError = GetLastError();
|
---|
| 461 | vrc = RTErrConvertFromWin32(dwError);
|
---|
| 462 | LogRel(("Error: Could not open SCM: %Rwc (%u) -> %Rrc\n", dwError, dwError, vrc));
|
---|
| 463 | }
|
---|
| 464 | return vrc;
|
---|
| 465 | }
|
---|
| 466 | # endif /* VBOX_WITH_SDS */
|
---|
| 467 |
|
---|
[63814] | 468 | #endif /* RT_OS_WINDOWS */
|
---|
| 469 |
|
---|
| 470 | /**
|
---|
[13659] | 471 | * Uninitializes the instance and sets the ready flag to FALSE.
|
---|
| 472 | * Called either from FinalRelease() or by the parent when it gets destroyed.
|
---|
| 473 | */
|
---|
[34417] | 474 | void VirtualBoxClient::uninit()
|
---|
[13659] | 475 | {
|
---|
[21878] | 476 | LogFlowThisFunc(("\n"));
|
---|
[1] | 477 |
|
---|
[13659] | 478 | /* Enclose the state transition Ready->InUninit->NotReady */
|
---|
[21878] | 479 | AutoUninitSpan autoUninitSpan(this);
|
---|
[13659] | 480 | if (autoUninitSpan.uninitDone())
|
---|
| 481 | return;
|
---|
| 482 |
|
---|
[34417] | 483 | if (mData.m_ThreadWatcher != NIL_RTTHREAD)
|
---|
| 484 | {
|
---|
| 485 | /* Signal the event semaphore and wait for the thread to terminate.
|
---|
| 486 | * if it hangs for some reason exit anyway, this can cause a crash
|
---|
| 487 | * though as the object will no longer be available. */
|
---|
| 488 | RTSemEventSignal(mData.m_SemEvWatcher);
|
---|
| 489 | RTThreadWait(mData.m_ThreadWatcher, 30000, NULL);
|
---|
| 490 | mData.m_ThreadWatcher = NIL_RTTHREAD;
|
---|
| 491 | RTSemEventDestroy(mData.m_SemEvWatcher);
|
---|
| 492 | mData.m_SemEvWatcher = NIL_RTSEMEVENT;
|
---|
| 493 | }
|
---|
[34642] | 494 |
|
---|
[66902] | 495 | mData.m_pToken.setNull();
|
---|
[35135] | 496 | mData.m_pVirtualBox.setNull();
|
---|
[34642] | 497 |
|
---|
| 498 | ASMAtomicDecU32(&g_cInstances);
|
---|
[13659] | 499 | }
|
---|
| 500 |
|
---|
[34417] | 501 | // IVirtualBoxClient properties
|
---|
[1] | 502 | /////////////////////////////////////////////////////////////////////////////
|
---|
| 503 |
|
---|
| 504 | /**
|
---|
[34417] | 505 | * Returns a reference to the VirtualBox object.
|
---|
[1] | 506 | *
|
---|
| 507 | * @returns COM status code
|
---|
[34417] | 508 | * @param aVirtualBox Address of result variable.
|
---|
[1] | 509 | */
|
---|
[52442] | 510 | HRESULT VirtualBoxClient::getVirtualBox(ComPtr<IVirtualBox> &aVirtualBox)
|
---|
[1] | 511 | {
|
---|
[35135] | 512 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
[52442] | 513 | aVirtualBox = mData.m_pVirtualBox;
|
---|
[1] | 514 | return S_OK;
|
---|
| 515 | }
|
---|
| 516 |
|
---|
| 517 | /**
|
---|
[34417] | 518 | * Create a new Session object and return a reference to it.
|
---|
[1] | 519 | *
|
---|
| 520 | * @returns COM status code
|
---|
[34417] | 521 | * @param aSession Address of result variable.
|
---|
[1] | 522 | */
|
---|
[52442] | 523 | HRESULT VirtualBoxClient::getSession(ComPtr<ISession> &aSession)
|
---|
[1] | 524 | {
|
---|
[34417] | 525 | /* this is not stored in this object, no need to lock */
|
---|
| 526 | ComPtr<ISession> pSession;
|
---|
[52442] | 527 | HRESULT rc = pSession.createInprocObject(CLSID_Session);
|
---|
[34417] | 528 | if (SUCCEEDED(rc))
|
---|
[52442] | 529 | aSession = pSession;
|
---|
[34417] | 530 | return rc;
|
---|
[1] | 531 | }
|
---|
| 532 |
|
---|
| 533 | /**
|
---|
[34417] | 534 | * Return reference to the EventSource associated with this object.
|
---|
[1] | 535 | *
|
---|
| 536 | * @returns COM status code
|
---|
[34417] | 537 | * @param aEventSource Address of result variable.
|
---|
[1] | 538 | */
|
---|
[52442] | 539 | HRESULT VirtualBoxClient::getEventSource(ComPtr<IEventSource> &aEventSource)
|
---|
[1] | 540 | {
|
---|
[13659] | 541 | /* this is const, no need to lock */
|
---|
[52442] | 542 | aEventSource = mData.m_pEventSource;
|
---|
| 543 | return aEventSource.isNull() ? E_FAIL : S_OK;
|
---|
[1] | 544 | }
|
---|
| 545 |
|
---|
[52442] | 546 | // IVirtualBoxClient methods
|
---|
| 547 | /////////////////////////////////////////////////////////////////////////////
|
---|
| 548 |
|
---|
[44970] | 549 | /**
|
---|
| 550 | * Checks a Machine object for any pending errors.
|
---|
| 551 | *
|
---|
| 552 | * @returns COM status code
|
---|
| 553 | * @param aMachine Machine object to check.
|
---|
| 554 | */
|
---|
[52442] | 555 | HRESULT VirtualBoxClient::checkMachineError(const ComPtr<IMachine> &aMachine)
|
---|
[44970] | 556 | {
|
---|
| 557 | BOOL fAccessible = FALSE;
|
---|
[52442] | 558 | HRESULT rc = aMachine->COMGETTER(Accessible)(&fAccessible);
|
---|
[44970] | 559 | if (FAILED(rc))
|
---|
| 560 | return setError(rc, tr("Could not check the accessibility status of the VM"));
|
---|
| 561 | else if (!fAccessible)
|
---|
| 562 | {
|
---|
| 563 | ComPtr<IVirtualBoxErrorInfo> pAccessError;
|
---|
| 564 | rc = aMachine->COMGETTER(AccessError)(pAccessError.asOutParam());
|
---|
| 565 | if (FAILED(rc))
|
---|
| 566 | return setError(rc, tr("Could not get the access error message of the VM"));
|
---|
| 567 | else
|
---|
| 568 | {
|
---|
| 569 | ErrorInfo info(pAccessError);
|
---|
| 570 | ErrorInfoKeeper eik(info);
|
---|
| 571 | return info.getResultCode();
|
---|
| 572 | }
|
---|
| 573 | }
|
---|
| 574 | return S_OK;
|
---|
| 575 | }
|
---|
| 576 |
|
---|
[34417] | 577 | // private methods
|
---|
| 578 | /////////////////////////////////////////////////////////////////////////////
|
---|
[1] | 579 |
|
---|
[66411] | 580 |
|
---|
[69240] | 581 | /// @todo AM Add pinging of VBoxSDS
|
---|
[34417] | 582 | /*static*/
|
---|
| 583 | DECLCALLBACK(int) VirtualBoxClient::SVCWatcherThread(RTTHREAD ThreadSelf,
|
---|
| 584 | void *pvUser)
|
---|
[1] | 585 | {
|
---|
[34417] | 586 | NOREF(ThreadSelf);
|
---|
| 587 | Assert(pvUser);
|
---|
| 588 | VirtualBoxClient *pThis = (VirtualBoxClient *)pvUser;
|
---|
| 589 | RTSEMEVENT sem = pThis->mData.m_SemEvWatcher;
|
---|
| 590 | RTMSINTERVAL cMillies = VBOXCLIENT_DEFAULT_INTERVAL;
|
---|
| 591 | int vrc;
|
---|
[1] | 592 |
|
---|
[34417] | 593 | /* The likelihood of early crashes are high, so start with a short wait. */
|
---|
| 594 | vrc = RTSemEventWait(sem, cMillies / 2);
|
---|
[1] | 595 |
|
---|
[34417] | 596 | /* As long as the waiting times out keep retrying the wait. */
|
---|
| 597 | while (RT_FAILURE(vrc))
|
---|
| 598 | {
|
---|
| 599 | {
|
---|
| 600 | HRESULT rc = S_OK;
|
---|
[35135] | 601 | ComPtr<IVirtualBox> pV;
|
---|
| 602 | {
|
---|
| 603 | AutoReadLock alock(pThis COMMA_LOCKVAL_SRC_POS);
|
---|
| 604 | pV = pThis->mData.m_pVirtualBox;
|
---|
| 605 | }
|
---|
[34417] | 606 | if (!pV.isNull())
|
---|
| 607 | {
|
---|
| 608 | ULONG rev;
|
---|
| 609 | rc = pV->COMGETTER(Revision)(&rev);
|
---|
| 610 | if (FAILED_DEAD_INTERFACE(rc))
|
---|
| 611 | {
|
---|
[34423] | 612 | LogRel(("VirtualBoxClient: detected unresponsive VBoxSVC (rc=%Rhrc)\n", rc));
|
---|
[35135] | 613 | {
|
---|
| 614 | AutoWriteLock alock(pThis COMMA_LOCKVAL_SRC_POS);
|
---|
| 615 | /* Throw away the VirtualBox reference, it's no longer
|
---|
| 616 | * usable as VBoxSVC terminated in the mean time. */
|
---|
| 617 | pThis->mData.m_pVirtualBox.setNull();
|
---|
| 618 | }
|
---|
[35172] | 619 | fireVBoxSVCAvailabilityChangedEvent(pThis->mData.m_pEventSource, FALSE);
|
---|
[34417] | 620 | }
|
---|
| 621 | }
|
---|
| 622 | else
|
---|
| 623 | {
|
---|
| 624 | /* Try to get a new VirtualBox reference straight away, and if
|
---|
| 625 | * this fails use an increased waiting time as very frequent
|
---|
| 626 | * restart attempts in some wedged config can cause high CPU
|
---|
| 627 | * and disk load. */
|
---|
[50174] | 628 | ComPtr<IVirtualBox> pVirtualBox;
|
---|
[66902] | 629 | ComPtr<IToken> pToken;
|
---|
[50174] | 630 | rc = pVirtualBox.createLocalObject(CLSID_VirtualBox);
|
---|
[34417] | 631 | if (FAILED(rc))
|
---|
| 632 | cMillies = 3 * VBOXCLIENT_DEFAULT_INTERVAL;
|
---|
| 633 | else
|
---|
[35135] | 634 | {
|
---|
| 635 | LogRel(("VirtualBoxClient: detected working VBoxSVC (rc=%Rhrc)\n", rc));
|
---|
| 636 | {
|
---|
| 637 | AutoWriteLock alock(pThis COMMA_LOCKVAL_SRC_POS);
|
---|
| 638 | /* Update the VirtualBox reference, there's a working
|
---|
| 639 | * VBoxSVC again from now on. */
|
---|
[50174] | 640 | pThis->mData.m_pVirtualBox = pVirtualBox;
|
---|
[66902] | 641 | pThis->mData.m_pToken = pToken;
|
---|
[35135] | 642 | }
|
---|
[35172] | 643 | fireVBoxSVCAvailabilityChangedEvent(pThis->mData.m_pEventSource, TRUE);
|
---|
[34417] | 644 | cMillies = VBOXCLIENT_DEFAULT_INTERVAL;
|
---|
[35135] | 645 | }
|
---|
[34417] | 646 | }
|
---|
| 647 | }
|
---|
| 648 | vrc = RTSemEventWait(sem, cMillies);
|
---|
| 649 | }
|
---|
| 650 | return 0;
|
---|
[1] | 651 | }
|
---|
| 652 |
|
---|
[14772] | 653 | /* vi: set tabstop=4 shiftwidth=4 expandtab: */
|
---|