VirtualBox

source: vbox/trunk/src/VBox/Main/src-client/VirtualBoxClientImpl.cpp@ 90828

Last change on this file since 90828 was 90828, checked in by vboxsync, 3 years ago

Main: bugref:1909: Added API localization

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

© 2023 Oracle
ContactPrivacy policyTerms of Use