VirtualBox

source: vbox/trunk/src/VBox/Main/src-client/GuestImpl.cpp@ 96391

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

Main/src-client/GuestImpl.cpp: Adjust to the new rules wrt. to rc -> hrc,vrc usage, ​bugref:10223

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 42.4 KB
Line 
1/* $Id: GuestImpl.cpp 94954 2022-05-09 12:03:01Z vboxsync $ */
2/** @file
3 * VirtualBox COM class implementation: Guest features.
4 */
5
6/*
7 * Copyright (C) 2006-2022 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_GUEST
19#include "LoggingNew.h"
20
21#include "GuestImpl.h"
22#ifdef VBOX_WITH_GUEST_CONTROL
23# include "GuestSessionImpl.h"
24#endif
25#include "Global.h"
26#include "ConsoleImpl.h"
27#include "ProgressImpl.h"
28#ifdef VBOX_WITH_DRAG_AND_DROP
29# include "GuestDnDPrivate.h"
30#endif
31#include "VMMDev.h"
32
33#include "AutoCaller.h"
34#include "Performance.h"
35#include "VBoxEvents.h"
36
37#include <VBox/VMMDev.h>
38#include <iprt/cpp/utils.h>
39#include <iprt/ctype.h>
40#include <iprt/stream.h>
41#include <iprt/timer.h>
42#include <VBox/vmm/pgm.h>
43#include <VBox/vmm/vmmr3vtable.h>
44#include <VBox/version.h>
45
46// defines
47/////////////////////////////////////////////////////////////////////////////
48
49// constructor / destructor
50/////////////////////////////////////////////////////////////////////////////
51
52DEFINE_EMPTY_CTOR_DTOR(Guest)
53
54HRESULT Guest::FinalConstruct()
55{
56 return BaseFinalConstruct();
57}
58
59void Guest::FinalRelease()
60{
61 uninit();
62 BaseFinalRelease();
63}
64
65// public methods only for internal purposes
66/////////////////////////////////////////////////////////////////////////////
67
68/**
69 * Initializes the guest object.
70 */
71HRESULT Guest::init(Console *aParent)
72{
73 LogFlowThisFunc(("aParent=%p\n", aParent));
74
75 ComAssertRet(aParent, E_INVALIDARG);
76
77 /* Enclose the state transition NotReady->InInit->Ready */
78 AutoInitSpan autoInitSpan(this);
79 AssertReturn(autoInitSpan.isOk(), E_FAIL);
80
81 unconst(mParent) = aParent;
82
83 ULONG aMemoryBalloonSize = 0;
84 HRESULT hr = mParent->i_machine()->COMGETTER(MemoryBalloonSize)(&aMemoryBalloonSize);
85 if (SUCCEEDED(hr))
86 mMemoryBalloonSize = aMemoryBalloonSize;
87 else
88 mMemoryBalloonSize = 0; /* Default is no ballooning */
89
90 BOOL fPageFusionEnabled = FALSE;
91 hr = mParent->i_machine()->COMGETTER(PageFusionEnabled)(&fPageFusionEnabled);
92 if (SUCCEEDED(hr))
93 mfPageFusionEnabled = fPageFusionEnabled;
94 else
95 mfPageFusionEnabled = false; /* Default is no page fusion*/
96
97 mStatUpdateInterval = 0; /* Default is not to report guest statistics at all */
98 mCollectVMMStats = false;
99
100 /* Clear statistics. */
101 mNetStatRx = mNetStatTx = 0;
102 mNetStatLastTs = RTTimeNanoTS();
103 for (unsigned i = 0 ; i < GUESTSTATTYPE_MAX; i++)
104 mCurrentGuestStat[i] = 0;
105 mVmValidStats = pm::VMSTATMASK_NONE;
106 RT_ZERO(mCurrentGuestCpuUserStat);
107 RT_ZERO(mCurrentGuestCpuKernelStat);
108 RT_ZERO(mCurrentGuestCpuIdleStat);
109
110 mMagic = GUEST_MAGIC;
111 mStatTimer = NIL_RTTIMERLR;
112
113 hr = unconst(mEventSource).createObject();
114 if (SUCCEEDED(hr))
115 hr = mEventSource->init();
116
117 mCpus = 1;
118
119#ifdef VBOX_WITH_DRAG_AND_DROP
120 if (SUCCEEDED(hr))
121 {
122 try
123 {
124 GuestDnD::createInstance(this /* pGuest */);
125 hr = unconst(mDnDSource).createObject();
126 if (SUCCEEDED(hr))
127 hr = mDnDSource->init(this /* pGuest */);
128 if (SUCCEEDED(hr))
129 {
130 hr = unconst(mDnDTarget).createObject();
131 if (SUCCEEDED(hr))
132 hr = mDnDTarget->init(this /* pGuest */);
133 }
134
135 LogFlowFunc(("Drag and drop initializied with hr=%Rhrc\n", hr));
136 }
137 catch (std::bad_alloc &)
138 {
139 hr = E_OUTOFMEMORY;
140 }
141 }
142#endif
143
144 /* Confirm a successful initialization when it's the case: */
145 if (SUCCEEDED(hr))
146 autoInitSpan.setSucceeded();
147 else
148 autoInitSpan.setFailed();
149
150 LogFlowFunc(("hr=%Rhrc\n", hr));
151 return hr;
152}
153
154/**
155 * Uninitializes the instance and sets the ready flag to FALSE.
156 * Called either from FinalRelease() or by the parent when it gets destroyed.
157 */
158void Guest::uninit()
159{
160 LogFlowThisFunc(("\n"));
161
162 /* Enclose the state transition Ready->InUninit->NotReady */
163 AutoUninitSpan autoUninitSpan(this);
164 if (autoUninitSpan.uninitDone())
165 return;
166
167 /* Destroy stat update timer */
168 int vrc = RTTimerLRDestroy(mStatTimer);
169 AssertMsgRC(vrc, ("Failed to create guest statistics update timer(%Rra)\n", vrc));
170 mStatTimer = NIL_RTTIMERLR;
171 mMagic = 0;
172
173#ifdef VBOX_WITH_GUEST_CONTROL
174 LogFlowThisFunc(("Closing sessions (%RU64 total)\n",
175 mData.mGuestSessions.size()));
176 GuestSessions::iterator itSessions = mData.mGuestSessions.begin();
177 while (itSessions != mData.mGuestSessions.end())
178 {
179# ifdef DEBUG
180/** @todo r=bird: hit a use-after-free situation here while debugging the
181 * 0xcccccccc status code issue in copyto. My bet is that this happens
182 * because of an uninit race, where GuestSession::close(), or someone, does
183 * not ensure that the parent object (Guest) is okay to use (in the AutoCaller
184 * sense), only their own object. */
185 ULONG cRefs = itSessions->second->AddRef();
186 LogFlowThisFunc(("sessionID=%RU32, cRefs=%RU32\n", itSessions->first, cRefs > 1 ? cRefs - 1 : 0));
187 itSessions->second->Release();
188# endif
189 itSessions->second->uninit();
190 ++itSessions;
191 }
192 mData.mGuestSessions.clear();
193#endif
194
195#ifdef VBOX_WITH_DRAG_AND_DROP
196 GuestDnD::destroyInstance();
197 unconst(mDnDSource).setNull();
198 unconst(mDnDTarget).setNull();
199#endif
200
201 unconst(mEventSource).setNull();
202 unconst(mParent) = NULL;
203
204 LogFlowFuncLeave();
205}
206
207/* static */
208DECLCALLBACK(void) Guest::i_staticUpdateStats(RTTIMERLR hTimerLR, void *pvUser, uint64_t iTick)
209{
210 AssertReturnVoid(pvUser != NULL);
211 Guest *guest = static_cast<Guest *>(pvUser);
212 Assert(guest->mMagic == GUEST_MAGIC);
213 if (guest->mMagic == GUEST_MAGIC)
214 guest->i_updateStats(iTick);
215
216 NOREF(hTimerLR);
217}
218
219/* static */
220DECLCALLBACK(int) Guest::i_staticEnumStatsCallback(const char *pszName, STAMTYPE enmType, void *pvSample,
221 STAMUNIT enmUnit, const char *pszUnit, STAMVISIBILITY enmVisiblity,
222 const char *pszDesc, void *pvUser)
223{
224 RT_NOREF(enmVisiblity, pszDesc, pszUnit);
225 AssertLogRelMsgReturn(enmType == STAMTYPE_COUNTER, ("Unexpected sample type %d ('%s')\n", enmType, pszName), VINF_SUCCESS);
226 AssertLogRelMsgReturn(enmUnit == STAMUNIT_BYTES, ("Unexpected sample unit %d ('%s')\n", enmUnit, pszName), VINF_SUCCESS);
227
228 /* Get the base name w/ slash. */
229 const char *pszLastSlash = strrchr(pszName, '/');
230 AssertLogRelMsgReturn(pszLastSlash, ("Unexpected sample '%s'\n", pszName), VINF_SUCCESS);
231
232 /* Receive or transmit? */
233 bool fRx;
234 if (!strcmp(pszLastSlash, "/BytesReceived"))
235 fRx = true;
236 else if (!strcmp(pszLastSlash, "/BytesTransmitted"))
237 fRx = false;
238 else
239 AssertLogRelMsgFailedReturn(("Unexpected sample '%s'\n", pszName), VINF_SUCCESS);
240
241#if 0 /* not used for anything, so don't bother parsing it. */
242 /* Find start of instance number. ASSUMES '/Public/Net/Name<Instance digits>/Bytes...' */
243 do
244 --pszLastSlash;
245 while (pszLastSlash > pszName && RT_C_IS_DIGIT(*pszLastSlash));
246 pszLastSlash++;
247
248 uint8_t uInstance;
249 int vrc = RTStrToUInt8Ex(pszLastSlash, NULL, 10, &uInstance);
250 AssertLogRelMsgReturn(RT_SUCCESS(vrc) && vrc != VWRN_NUMBER_TOO_BIG && vrc != VWRN_NEGATIVE_UNSIGNED,
251 ("%Rrc '%s'\n", vrc, pszName), VINF_SUCCESS)
252#endif
253
254 /* Add the bytes to our counters. */
255 PSTAMCOUNTER pCnt = (PSTAMCOUNTER)pvSample;
256 Guest *pGuest = (Guest *)pvUser;
257 uint64_t cb = pCnt->c;
258#if 0
259 LogFlowFunc(("%s i=%u d=%s %llu bytes\n", pszName, uInstance, fRx ? "RX" : "TX", cb));
260#else
261 LogFlowFunc(("%s d=%s %llu bytes\n", pszName, fRx ? "RX" : "TX", cb));
262#endif
263 if (fRx)
264 pGuest->mNetStatRx += cb;
265 else
266 pGuest->mNetStatTx += cb;
267
268 return VINF_SUCCESS;
269}
270
271void Guest::i_updateStats(uint64_t iTick)
272{
273 RT_NOREF(iTick);
274
275 uint64_t cbFreeTotal = 0;
276 uint64_t cbAllocTotal = 0;
277 uint64_t cbBalloonedTotal = 0;
278 uint64_t cbSharedTotal = 0;
279 uint64_t cbSharedMem = 0;
280 ULONG uNetStatRx = 0;
281 ULONG uNetStatTx = 0;
282 ULONG aGuestStats[GUESTSTATTYPE_MAX];
283 RT_ZERO(aGuestStats);
284
285 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
286
287 ULONG validStats = mVmValidStats;
288 /* Check if we have anything to report */
289 if (validStats)
290 {
291 mVmValidStats = pm::VMSTATMASK_NONE;
292 memcpy(aGuestStats, mCurrentGuestStat, sizeof(aGuestStats));
293 }
294 alock.release();
295
296 /*
297 * Calling SessionMachine may take time as the object resides in VBoxSVC
298 * process. This is why we took a snapshot of currently collected stats
299 * and released the lock.
300 */
301 Console::SafeVMPtrQuiet ptrVM(mParent);
302 if (ptrVM.isOk())
303 {
304 int vrc;
305
306 /*
307 * There is no point in collecting VM shared memory if other memory
308 * statistics are not available yet. Or is there?
309 */
310 if (validStats)
311 {
312 /* Query the missing per-VM memory statistics. */
313 uint64_t cbTotalMemIgn, cbPrivateMemIgn, cbZeroMemIgn;
314 vrc = ptrVM.vtable()->pfnPGMR3QueryMemoryStats(ptrVM.rawUVM(), &cbTotalMemIgn, &cbPrivateMemIgn,
315 &cbSharedMem, &cbZeroMemIgn);
316 if (vrc == VINF_SUCCESS)
317 validStats |= pm::VMSTATMASK_GUEST_MEMSHARED;
318 }
319
320 if (mCollectVMMStats)
321 {
322 vrc = ptrVM.vtable()->pfnPGMR3QueryGlobalMemoryStats(ptrVM.rawUVM(), &cbAllocTotal, &cbFreeTotal,
323 &cbBalloonedTotal, &cbSharedTotal);
324 AssertRC(vrc);
325 if (vrc == VINF_SUCCESS)
326 validStats |= pm::VMSTATMASK_VMM_ALLOC | pm::VMSTATMASK_VMM_FREE
327 | pm::VMSTATMASK_VMM_BALOON | pm::VMSTATMASK_VMM_SHARED;
328 }
329
330 uint64_t uRxPrev = mNetStatRx;
331 uint64_t uTxPrev = mNetStatTx;
332 mNetStatRx = mNetStatTx = 0;
333 vrc = ptrVM.vtable()->pfnSTAMR3Enum(ptrVM.rawUVM(), "/Public/Net/*/Bytes*", i_staticEnumStatsCallback, this);
334 AssertRC(vrc);
335
336 uint64_t uTsNow = RTTimeNanoTS();
337 uint64_t cNsPassed = uTsNow - mNetStatLastTs;
338 if (cNsPassed >= 1000)
339 {
340 mNetStatLastTs = uTsNow;
341
342 uNetStatRx = (ULONG)((mNetStatRx - uRxPrev) * 1000000 / (cNsPassed / 1000)); /* in bytes per second */
343 uNetStatTx = (ULONG)((mNetStatTx - uTxPrev) * 1000000 / (cNsPassed / 1000)); /* in bytes per second */
344 validStats |= pm::VMSTATMASK_NET_RX | pm::VMSTATMASK_NET_TX;
345 LogFlowThisFunc(("Net Rx=%llu Tx=%llu Ts=%llu Delta=%llu\n", mNetStatRx, mNetStatTx, uTsNow, cNsPassed));
346 }
347 else
348 {
349 /* Can happen on resume or if we're using a non-monotonic clock
350 source for the timer and the time is adjusted. */
351 mNetStatRx = uRxPrev;
352 mNetStatTx = uTxPrev;
353 LogThisFunc(("Net Ts=%llu cNsPassed=%llu - too small interval\n", uTsNow, cNsPassed));
354 }
355 }
356
357 mParent->i_reportVmStatistics(validStats,
358 aGuestStats[GUESTSTATTYPE_CPUUSER],
359 aGuestStats[GUESTSTATTYPE_CPUKERNEL],
360 aGuestStats[GUESTSTATTYPE_CPUIDLE],
361 /* Convert the units for RAM usage stats: page (4K) -> 1KB units */
362 mCurrentGuestStat[GUESTSTATTYPE_MEMTOTAL] * (_4K/_1K),
363 mCurrentGuestStat[GUESTSTATTYPE_MEMFREE] * (_4K/_1K),
364 mCurrentGuestStat[GUESTSTATTYPE_MEMBALLOON] * (_4K/_1K),
365 (ULONG)(cbSharedMem / _1K), /* bytes -> KB */
366 mCurrentGuestStat[GUESTSTATTYPE_MEMCACHE] * (_4K/_1K),
367 mCurrentGuestStat[GUESTSTATTYPE_PAGETOTAL] * (_4K/_1K),
368 (ULONG)(cbAllocTotal / _1K), /* bytes -> KB */
369 (ULONG)(cbFreeTotal / _1K),
370 (ULONG)(cbBalloonedTotal / _1K),
371 (ULONG)(cbSharedTotal / _1K),
372 uNetStatRx,
373 uNetStatTx);
374}
375
376// IGuest properties
377/////////////////////////////////////////////////////////////////////////////
378
379HRESULT Guest::getOSTypeId(com::Utf8Str &aOSTypeId)
380{
381 HRESULT hrc = S_OK;
382 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
383 if (!mData.mInterfaceVersion.isEmpty())
384 aOSTypeId = mData.mOSTypeId;
385 else
386 {
387 /* Redirect the call to IMachine if no additions are installed. */
388 ComPtr<IMachine> ptrMachine(mParent->i_machine());
389 alock.release();
390 Bstr bstr;
391 hrc = ptrMachine->COMGETTER(OSTypeId)(bstr.asOutParam());
392 aOSTypeId = bstr;
393 }
394 return hrc;
395}
396
397HRESULT Guest::getAdditionsRunLevel(AdditionsRunLevelType_T *aAdditionsRunLevel)
398{
399 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
400
401 *aAdditionsRunLevel = mData.mAdditionsRunLevel;
402
403 return S_OK;
404}
405
406HRESULT Guest::getAdditionsVersion(com::Utf8Str &aAdditionsVersion)
407{
408 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
409 HRESULT hrc = S_OK;
410
411 /*
412 * Return the ReportGuestInfo2 version info if available.
413 */
414 if ( !mData.mAdditionsVersionNew.isEmpty()
415 || mData.mAdditionsRunLevel <= AdditionsRunLevelType_None)
416 aAdditionsVersion = mData.mAdditionsVersionNew;
417 else
418 {
419 /*
420 * If we're running older Guest Additions (< 3.2.0) try get it from
421 * the guest properties. Detected switched around Version and
422 * Revision in early 3.1.x releases (see r57115).
423 */
424 ComPtr<IMachine> ptrMachine = mParent->i_machine();
425 alock.release(); /* No need to hold this during the IPC fun. */
426
427 Bstr bstr;
428 hrc = ptrMachine->GetGuestPropertyValue(Bstr("/VirtualBox/GuestAdd/Version").raw(), bstr.asOutParam());
429 if ( SUCCEEDED(hrc)
430 && !bstr.isEmpty())
431 {
432 Utf8Str str(bstr);
433 if (str.count('.') == 0)
434 hrc = ptrMachine->GetGuestPropertyValue(Bstr("/VirtualBox/GuestAdd/Revision").raw(), bstr.asOutParam());
435 str = bstr;
436 if (str.count('.') != 2)
437 hrc = E_FAIL;
438 }
439
440 if (SUCCEEDED(hrc))
441 aAdditionsVersion = bstr;
442 else
443 {
444 /* Returning 1.4 is better than nothing. */
445 alock.acquire();
446 aAdditionsVersion = mData.mInterfaceVersion;
447 hrc = S_OK;
448 }
449 }
450 return hrc;
451}
452
453HRESULT Guest::getAdditionsRevision(ULONG *aAdditionsRevision)
454{
455 HRESULT hrc = S_OK;
456 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
457
458 /*
459 * Return the ReportGuestInfo2 version info if available.
460 */
461 if ( !mData.mAdditionsVersionNew.isEmpty()
462 || mData.mAdditionsRunLevel <= AdditionsRunLevelType_None)
463 *aAdditionsRevision = mData.mAdditionsRevision;
464 else
465 {
466 /*
467 * If we're running older Guest Additions (< 3.2.0) try get it from
468 * the guest properties. Detected switched around Version and
469 * Revision in early 3.1.x releases (see r57115).
470 */
471 ComPtr<IMachine> ptrMachine = mParent->i_machine();
472 alock.release(); /* No need to hold this during the IPC fun. */
473
474 Bstr bstr;
475 hrc = ptrMachine->GetGuestPropertyValue(Bstr("/VirtualBox/GuestAdd/Revision").raw(), bstr.asOutParam());
476 if (SUCCEEDED(hrc))
477 {
478 Utf8Str str(bstr);
479 uint32_t uRevision;
480 int vrc = RTStrToUInt32Full(str.c_str(), 0, &uRevision);
481 if (vrc != VINF_SUCCESS && str.count('.') == 2)
482 {
483 hrc = ptrMachine->GetGuestPropertyValue(Bstr("/VirtualBox/GuestAdd/Version").raw(), bstr.asOutParam());
484 if (SUCCEEDED(hrc))
485 {
486 str = bstr;
487 vrc = RTStrToUInt32Full(str.c_str(), 0, &uRevision);
488 }
489 }
490 if (vrc == VINF_SUCCESS)
491 *aAdditionsRevision = uRevision;
492 else
493 hrc = VBOX_E_IPRT_ERROR;
494 }
495 if (FAILED(hrc))
496 {
497 /* Return 0 if we don't know. */
498 *aAdditionsRevision = 0;
499 hrc = S_OK;
500 }
501 }
502 return hrc;
503}
504
505HRESULT Guest::getDnDSource(ComPtr<IGuestDnDSource> &aDnDSource)
506{
507#ifndef VBOX_WITH_DRAG_AND_DROP
508 RT_NOREF(aDnDSource);
509 ReturnComNotImplemented();
510#else
511 LogFlowThisFuncEnter();
512
513 /* No need to lock - lifetime constant. */
514 HRESULT hr = mDnDSource.queryInterfaceTo(aDnDSource.asOutParam());
515
516 LogFlowFuncLeaveRC(hr);
517 return hr;
518#endif /* VBOX_WITH_DRAG_AND_DROP */
519}
520
521HRESULT Guest::getDnDTarget(ComPtr<IGuestDnDTarget> &aDnDTarget)
522{
523#ifndef VBOX_WITH_DRAG_AND_DROP
524 RT_NOREF(aDnDTarget);
525 ReturnComNotImplemented();
526#else
527 LogFlowThisFuncEnter();
528
529 /* No need to lock - lifetime constant. */
530 HRESULT hr = mDnDTarget.queryInterfaceTo(aDnDTarget.asOutParam());
531
532 LogFlowFuncLeaveRC(hr);
533 return hr;
534#endif /* VBOX_WITH_DRAG_AND_DROP */
535}
536
537HRESULT Guest::getEventSource(ComPtr<IEventSource> &aEventSource)
538{
539 LogFlowThisFuncEnter();
540
541 /* No need to lock - lifetime constant. */
542 mEventSource.queryInterfaceTo(aEventSource.asOutParam());
543
544 LogFlowFuncLeaveRC(S_OK);
545 return S_OK;
546}
547
548HRESULT Guest::getFacilities(std::vector<ComPtr<IAdditionsFacility> > &aFacilities)
549{
550 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
551
552 aFacilities.resize(mData.mFacilityMap.size());
553 size_t i = 0;
554 for (FacilityMapIter it = mData.mFacilityMap.begin(); it != mData.mFacilityMap.end(); ++it, ++i)
555 it->second.queryInterfaceTo(aFacilities[i].asOutParam());
556
557 return S_OK;
558}
559
560HRESULT Guest::getSessions(std::vector<ComPtr<IGuestSession> > &aSessions)
561{
562#ifdef VBOX_WITH_GUEST_CONTROL
563 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
564
565 aSessions.resize(mData.mGuestSessions.size());
566 size_t i = 0;
567 for (GuestSessions::iterator it = mData.mGuestSessions.begin(); it != mData.mGuestSessions.end(); ++it, ++i)
568 it->second.queryInterfaceTo(aSessions[i].asOutParam());
569
570 return S_OK;
571#else
572 ReturnComNotImplemented();
573#endif
574}
575
576BOOL Guest::i_isPageFusionEnabled()
577{
578 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
579
580 return mfPageFusionEnabled;
581}
582
583HRESULT Guest::getMemoryBalloonSize(ULONG *aMemoryBalloonSize)
584{
585 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
586
587 *aMemoryBalloonSize = mMemoryBalloonSize;
588
589 return S_OK;
590}
591
592HRESULT Guest::setMemoryBalloonSize(ULONG aMemoryBalloonSize)
593{
594 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
595
596 /* We must be 100% sure that IMachine::COMSETTER(MemoryBalloonSize)
597 * does not call us back in any way! */
598 HRESULT ret = mParent->i_machine()->COMSETTER(MemoryBalloonSize)(aMemoryBalloonSize);
599 if (ret == S_OK)
600 {
601 mMemoryBalloonSize = aMemoryBalloonSize;
602 /* forward the information to the VMM device */
603 VMMDev *pVMMDev = mParent->i_getVMMDev();
604 /* MUST release all locks before calling VMM device as its critsect
605 * has higher lock order than anything in Main. */
606 alock.release();
607 if (pVMMDev)
608 {
609 PPDMIVMMDEVPORT pVMMDevPort = pVMMDev->getVMMDevPort();
610 if (pVMMDevPort)
611 pVMMDevPort->pfnSetMemoryBalloon(pVMMDevPort, aMemoryBalloonSize);
612 }
613 }
614
615 return ret;
616}
617
618HRESULT Guest::getStatisticsUpdateInterval(ULONG *aStatisticsUpdateInterval)
619{
620 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
621
622 *aStatisticsUpdateInterval = mStatUpdateInterval;
623 return S_OK;
624}
625
626HRESULT Guest::setStatisticsUpdateInterval(ULONG aStatisticsUpdateInterval)
627{
628 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
629
630 /* Update the timer, creating it the first time we're called with a non-zero value. */
631 int vrc;
632 HRESULT hrc = S_OK;
633 if (aStatisticsUpdateInterval > 0)
634 {
635 if (mStatTimer == NIL_RTTIMERLR)
636 {
637 vrc = RTTimerLRCreate(&mStatTimer, aStatisticsUpdateInterval * RT_MS_1SEC, &Guest::i_staticUpdateStats, this);
638 AssertRCStmt(vrc, hrc = setErrorVrc(vrc, tr("Failed to create guest statistics update timer (%Rrc)"), vrc));
639 }
640 else if (aStatisticsUpdateInterval != mStatUpdateInterval)
641 {
642 vrc = RTTimerLRChangeInterval(mStatTimer, aStatisticsUpdateInterval * RT_NS_1SEC_64);
643 AssertRCStmt(vrc, hrc = setErrorVrc(vrc, tr("Failed to change guest statistics update timer interval from %u to %u failed (%Rrc)"),
644 mStatUpdateInterval, aStatisticsUpdateInterval, vrc));
645 if (mStatUpdateInterval == 0)
646 {
647 vrc = RTTimerLRStart(mStatTimer, 0);
648 AssertRCStmt(vrc, hrc = setErrorVrc(vrc, tr("Failed to start the guest statistics update timer (%Rrc)"), vrc));
649 }
650 }
651 }
652 /* Setting interval to zero - stop the update timer if needed: */
653 else if (mStatUpdateInterval > 0 && mStatTimer != NIL_RTTIMERLR)
654 {
655 vrc = RTTimerLRStop(mStatTimer);
656 AssertRCStmt(vrc, hrc = setErrorVrc(vrc, tr("Failed to stop the guest statistics update timer (%Rrc)"), vrc));
657 }
658
659 /* Update the interval now that the timer is in sync. */
660 mStatUpdateInterval = aStatisticsUpdateInterval;
661
662 /* Forward the information to the VMM device.
663 MUST release all locks before calling VMM device as its critsect
664 has higher lock order than anything in Main. */
665 VMMDev *pVMMDev = mParent->i_getVMMDev();
666 alock.release();
667 if (pVMMDev)
668 {
669 PPDMIVMMDEVPORT pVMMDevPort = pVMMDev->getVMMDevPort();
670 if (pVMMDevPort)
671 pVMMDevPort->pfnSetStatisticsInterval(pVMMDevPort, aStatisticsUpdateInterval);
672 }
673
674 return hrc;
675}
676
677
678HRESULT Guest::internalGetStatistics(ULONG *aCpuUser, ULONG *aCpuKernel, ULONG *aCpuIdle,
679 ULONG *aMemTotal, ULONG *aMemFree, ULONG *aMemBalloon,
680 ULONG *aMemShared, ULONG *aMemCache, ULONG *aPageTotal,
681 ULONG *aMemAllocTotal, ULONG *aMemFreeTotal,
682 ULONG *aMemBalloonTotal, ULONG *aMemSharedTotal)
683{
684 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
685
686 *aCpuUser = mCurrentGuestStat[GUESTSTATTYPE_CPUUSER];
687 *aCpuKernel = mCurrentGuestStat[GUESTSTATTYPE_CPUKERNEL];
688 *aCpuIdle = mCurrentGuestStat[GUESTSTATTYPE_CPUIDLE];
689 *aMemTotal = mCurrentGuestStat[GUESTSTATTYPE_MEMTOTAL] * (_4K/_1K); /* page (4K) -> 1KB units */
690 *aMemFree = mCurrentGuestStat[GUESTSTATTYPE_MEMFREE] * (_4K/_1K); /* page (4K) -> 1KB units */
691 *aMemBalloon = mCurrentGuestStat[GUESTSTATTYPE_MEMBALLOON] * (_4K/_1K); /* page (4K) -> 1KB units */
692 *aMemCache = mCurrentGuestStat[GUESTSTATTYPE_MEMCACHE] * (_4K/_1K); /* page (4K) -> 1KB units */
693 *aPageTotal = mCurrentGuestStat[GUESTSTATTYPE_PAGETOTAL] * (_4K/_1K); /* page (4K) -> 1KB units */
694
695 /* Play safe or smth? */
696 *aMemAllocTotal = 0;
697 *aMemFreeTotal = 0;
698 *aMemBalloonTotal = 0;
699 *aMemSharedTotal = 0;
700 *aMemShared = 0;
701
702 /* MUST release all locks before calling any PGM statistics queries,
703 * as they are executed by EMT and that might deadlock us by VMM device
704 * activity which waits for the Guest object lock. */
705 alock.release();
706 Console::SafeVMPtr ptrVM(mParent);
707 if (!ptrVM.isOk())
708 return E_FAIL;
709
710 uint64_t cbFreeTotal, cbAllocTotal, cbBalloonedTotal, cbSharedTotal;
711 int vrc = ptrVM.vtable()->pfnPGMR3QueryGlobalMemoryStats(ptrVM.rawUVM(), &cbAllocTotal, &cbFreeTotal,
712 &cbBalloonedTotal, &cbSharedTotal);
713 AssertRCReturn(vrc, E_FAIL);
714
715 *aMemAllocTotal = (ULONG)(cbAllocTotal / _1K); /* bytes -> KB */
716 *aMemFreeTotal = (ULONG)(cbFreeTotal / _1K);
717 *aMemBalloonTotal = (ULONG)(cbBalloonedTotal / _1K);
718 *aMemSharedTotal = (ULONG)(cbSharedTotal / _1K);
719
720 /* Query the missing per-VM memory statistics. */
721 uint64_t cbTotalMemIgn, cbPrivateMemIgn, cbSharedMem, cbZeroMemIgn;
722 vrc = ptrVM.vtable()->pfnPGMR3QueryMemoryStats(ptrVM.rawUVM(), &cbTotalMemIgn, &cbPrivateMemIgn, &cbSharedMem, &cbZeroMemIgn);
723 AssertRCReturn(vrc, E_FAIL);
724 *aMemShared = (ULONG)(cbSharedMem / _1K);
725
726 return S_OK;
727}
728
729HRESULT Guest::i_setStatistic(ULONG aCpuId, GUESTSTATTYPE enmType, ULONG aVal)
730{
731 static ULONG indexToPerfMask[] =
732 {
733 pm::VMSTATMASK_GUEST_CPUUSER,
734 pm::VMSTATMASK_GUEST_CPUKERNEL,
735 pm::VMSTATMASK_GUEST_CPUIDLE,
736 pm::VMSTATMASK_GUEST_MEMTOTAL,
737 pm::VMSTATMASK_GUEST_MEMFREE,
738 pm::VMSTATMASK_GUEST_MEMBALLOON,
739 pm::VMSTATMASK_GUEST_MEMCACHE,
740 pm::VMSTATMASK_GUEST_PAGETOTAL,
741 pm::VMSTATMASK_NONE
742 };
743 AutoCaller autoCaller(this);
744 if (FAILED(autoCaller.rc())) return autoCaller.rc();
745
746 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
747
748 if (enmType >= GUESTSTATTYPE_MAX)
749 return E_INVALIDARG;
750
751 if (aCpuId < VMM_MAX_CPU_COUNT)
752 {
753 ULONG *paCpuStats;
754 switch (enmType)
755 {
756 case GUESTSTATTYPE_CPUUSER: paCpuStats = mCurrentGuestCpuUserStat; break;
757 case GUESTSTATTYPE_CPUKERNEL: paCpuStats = mCurrentGuestCpuKernelStat; break;
758 case GUESTSTATTYPE_CPUIDLE: paCpuStats = mCurrentGuestCpuIdleStat; break;
759 default: paCpuStats = NULL; break;
760 }
761 if (paCpuStats)
762 {
763 paCpuStats[aCpuId] = aVal;
764 aVal = 0;
765 for (uint32_t i = 0; i < mCpus && i < VMM_MAX_CPU_COUNT; i++)
766 aVal += paCpuStats[i];
767 aVal /= mCpus;
768 }
769 }
770
771 mCurrentGuestStat[enmType] = aVal;
772 mVmValidStats |= indexToPerfMask[enmType];
773 return S_OK;
774}
775
776/**
777 * Returns the status of a specified Guest Additions facility.
778 *
779 * @return COM status code
780 * @param aFacility Facility to get the status from.
781 * @param aTimestamp Timestamp of last facility status update in ms (optional).
782 * @param aStatus Current status of the specified facility.
783 */
784HRESULT Guest::getFacilityStatus(AdditionsFacilityType_T aFacility, LONG64 *aTimestamp, AdditionsFacilityStatus_T *aStatus)
785{
786 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
787
788 /* Not checking for aTimestamp is intentional; it's optional. */
789 FacilityMapIterConst it = mData.mFacilityMap.find(aFacility);
790 if (it != mData.mFacilityMap.end())
791 {
792 AdditionsFacility *pFacility = it->second;
793 ComAssert(pFacility);
794 *aStatus = pFacility->i_getStatus();
795 if (aTimestamp)
796 *aTimestamp = pFacility->i_getLastUpdated();
797 }
798 else
799 {
800 /*
801 * Do not fail here -- could be that the facility never has been brought up (yet) but
802 * the host wants to have its status anyway. So just tell we don't know at this point.
803 */
804 *aStatus = AdditionsFacilityStatus_Unknown;
805 if (aTimestamp)
806 *aTimestamp = RTTimeMilliTS();
807 }
808 return S_OK;
809}
810
811HRESULT Guest::getAdditionsStatus(AdditionsRunLevelType_T aLevel, BOOL *aActive)
812{
813 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
814
815 HRESULT hrc = S_OK;
816 switch (aLevel)
817 {
818 case AdditionsRunLevelType_System:
819 *aActive = (mData.mAdditionsRunLevel > AdditionsRunLevelType_None);
820 break;
821
822 case AdditionsRunLevelType_Userland:
823 *aActive = (mData.mAdditionsRunLevel >= AdditionsRunLevelType_Userland);
824 break;
825
826 case AdditionsRunLevelType_Desktop:
827 *aActive = (mData.mAdditionsRunLevel >= AdditionsRunLevelType_Desktop);
828 break;
829
830 default:
831 hrc = setError(VBOX_E_NOT_SUPPORTED,
832 tr("Invalid status level defined: %u"), aLevel);
833 break;
834 }
835
836 return hrc;
837}
838
839HRESULT Guest::setCredentials(const com::Utf8Str &aUserName, const com::Utf8Str &aPassword,
840 const com::Utf8Str &aDomain, BOOL aAllowInteractiveLogon)
841{
842 /* Check for magic domain names which are used to pass encryption keys to the disk. */
843 if (Utf8Str(aDomain) == "@@disk")
844 return mParent->i_setDiskEncryptionKeys(aPassword);
845 if (Utf8Str(aDomain) == "@@mem")
846 {
847 /** @todo */
848 return E_NOTIMPL;
849 }
850
851 /* forward the information to the VMM device */
852 VMMDev *pVMMDev = mParent->i_getVMMDev();
853 if (pVMMDev)
854 {
855 PPDMIVMMDEVPORT pVMMDevPort = pVMMDev->getVMMDevPort();
856 if (pVMMDevPort)
857 {
858 uint32_t u32Flags = VMMDEV_SETCREDENTIALS_GUESTLOGON;
859 if (!aAllowInteractiveLogon)
860 u32Flags = VMMDEV_SETCREDENTIALS_NOLOCALLOGON;
861
862 pVMMDevPort->pfnSetCredentials(pVMMDevPort,
863 aUserName.c_str(),
864 aPassword.c_str(),
865 aDomain.c_str(),
866 u32Flags);
867 return S_OK;
868 }
869 }
870
871 return setError(VBOX_E_VM_ERROR, tr("VMM device is not available (is the VM running?)"));
872}
873
874// public methods only for internal purposes
875/////////////////////////////////////////////////////////////////////////////
876
877/**
878 * Sets the general Guest Additions information like
879 * API (interface) version and OS type. Gets called by
880 * vmmdevUpdateGuestInfo.
881 *
882 * @param aInterfaceVersion
883 * @param aOsType
884 */
885void Guest::i_setAdditionsInfo(const com::Utf8Str &aInterfaceVersion, VBOXOSTYPE aOsType)
886{
887 RTTIMESPEC TimeSpecTS;
888 RTTimeNow(&TimeSpecTS);
889
890 AutoCaller autoCaller(this);
891 AssertComRCReturnVoid(autoCaller.rc());
892
893 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
894
895 /*
896 * Note: The Guest Additions API (interface) version is deprecated
897 * and will not be used anymore! We might need it to at least report
898 * something as version number if *really* ancient Guest Additions are
899 * installed (without the guest version + revision properties having set).
900 */
901 mData.mInterfaceVersion = aInterfaceVersion;
902
903 /*
904 * Older Additions rely on the Additions API version whether they
905 * are assumed to be active or not. Since newer Additions do report
906 * the Additions version *before* calling this function (by calling
907 * VMMDevReportGuestInfo2, VMMDevReportGuestStatus, VMMDevReportGuestInfo,
908 * in that order) we can tell apart old and new Additions here. Old
909 * Additions never would set VMMDevReportGuestInfo2 (which set mData.mAdditionsVersion)
910 * so they just rely on the aInterfaceVersion string (which gets set by
911 * VMMDevReportGuestInfo).
912 *
913 * So only mark the Additions as being active (run level = system) when we
914 * don't have the Additions version set.
915 */
916 if (mData.mAdditionsVersionNew.isEmpty())
917 {
918 if (aInterfaceVersion.isEmpty())
919 mData.mAdditionsRunLevel = AdditionsRunLevelType_None;
920 else
921 {
922 mData.mAdditionsRunLevel = AdditionsRunLevelType_System;
923
924 /*
925 * To keep it compatible with the old Guest Additions behavior we need to set the
926 * "graphics" (feature) facility to active as soon as we got the Guest Additions
927 * interface version.
928 */
929 i_facilityUpdate(VBoxGuestFacilityType_Graphics, VBoxGuestFacilityStatus_Active, 0 /*fFlags*/, &TimeSpecTS);
930 }
931 }
932
933 /*
934 * Older Additions didn't have this finer grained capability bit,
935 * so enable it by default. Newer Additions will not enable this here
936 * and use the setSupportedFeatures function instead.
937 */
938 /** @todo r=bird: I don't get the above comment nor the code below...
939 * One talks about capability bits, the one always does something to a facility.
940 * Then there is the comment below it all, which is placed like it addresses the
941 * mOSTypeId, but talks about something which doesn't remotely like mOSTypeId...
942 *
943 * Andy, could you please try clarify and make the comments shorter and more
944 * coherent! Also, explain why this is important and what depends on it.
945 *
946 * PS. There is the VMMDEV_GUEST_SUPPORTS_GRAPHICS capability* report... It
947 * should come in pretty quickly after this update, normally.
948 */
949 i_facilityUpdate(VBoxGuestFacilityType_Graphics,
950 i_facilityIsActive(VBoxGuestFacilityType_VBoxGuestDriver)
951 ? VBoxGuestFacilityStatus_Active : VBoxGuestFacilityStatus_Inactive,
952 0 /*fFlags*/, &TimeSpecTS); /** @todo the timestamp isn't gonna be right here on saved state restore. */
953
954 /*
955 * Note! There is a race going on between setting mAdditionsRunLevel and
956 * mSupportsGraphics here and disabling/enabling it later according to
957 * its real status when using new(er) Guest Additions.
958 */
959 mData.mOSType = aOsType;
960 mData.mOSTypeId = Global::OSTypeId(aOsType);
961
962 /*
963 * Always fire an event here.
964 */
965 AdditionsRunLevelType_T const enmRunLevel = mData.mAdditionsRunLevel;
966 alock.release();
967 ::FireGuestAdditionsStatusChangedEvent(mEventSource, AdditionsFacilityType_None, AdditionsFacilityStatus_Active,
968 enmRunLevel, RTTimeSpecGetMilli(&TimeSpecTS));
969}
970
971/**
972 * Sets the Guest Additions version information details.
973 *
974 * Gets called by vmmdevUpdateGuestInfo2 and vmmdevUpdateGuestInfo (to clear the
975 * state).
976 *
977 * @param a_uFullVersion VBoxGuestInfo2::additionsMajor,
978 * VBoxGuestInfo2::additionsMinor and
979 * VBoxGuestInfo2::additionsBuild combined into
980 * one value by VBOX_FULL_VERSION_MAKE.
981 *
982 * When this is 0, it's vmmdevUpdateGuestInfo
983 * calling to reset the state.
984 *
985 * @param a_pszName Build type tag and/or publisher tag, empty
986 * string if neiter of those are present.
987 * @param a_uRevision See VBoxGuestInfo2::additionsRevision.
988 * @param a_fFeatures See VBoxGuestInfo2::additionsFeatures.
989 */
990void Guest::i_setAdditionsInfo2(uint32_t a_uFullVersion, const char *a_pszName, uint32_t a_uRevision, uint32_t a_fFeatures)
991{
992 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
993
994 if (a_uFullVersion)
995 {
996 mData.mAdditionsVersionNew = Utf8StrFmt(*a_pszName ? "%u.%u.%u_%s" : "%u.%u.%u",
997 VBOX_FULL_VERSION_GET_MAJOR(a_uFullVersion),
998 VBOX_FULL_VERSION_GET_MINOR(a_uFullVersion),
999 VBOX_FULL_VERSION_GET_BUILD(a_uFullVersion),
1000 a_pszName);
1001 mData.mAdditionsVersionFull = a_uFullVersion;
1002 mData.mAdditionsRevision = a_uRevision;
1003 mData.mAdditionsFeatures = a_fFeatures;
1004 }
1005 else
1006 {
1007 Assert(!a_fFeatures && !a_uRevision && !*a_pszName);
1008 mData.mAdditionsVersionNew.setNull();
1009 mData.mAdditionsVersionFull = 0;
1010 mData.mAdditionsRevision = 0;
1011 mData.mAdditionsFeatures = 0;
1012 }
1013}
1014
1015bool Guest::i_facilityIsActive(VBoxGuestFacilityType enmFacility)
1016{
1017 Assert(enmFacility < INT32_MAX);
1018 FacilityMapIterConst it = mData.mFacilityMap.find((AdditionsFacilityType_T)enmFacility);
1019 if (it != mData.mFacilityMap.end())
1020 {
1021 AdditionsFacility *pFac = it->second;
1022 return (pFac->i_getStatus() == AdditionsFacilityStatus_Active);
1023 }
1024 return false;
1025}
1026
1027bool Guest::i_facilityUpdate(VBoxGuestFacilityType a_enmFacility, VBoxGuestFacilityStatus a_enmStatus,
1028 uint32_t a_fFlags, PCRTTIMESPEC a_pTimeSpecTS)
1029{
1030 AssertReturn( a_enmFacility < VBoxGuestFacilityType_All
1031 && a_enmFacility > VBoxGuestFacilityType_Unknown, false);
1032
1033 bool fChanged;
1034 FacilityMapIter it = mData.mFacilityMap.find((AdditionsFacilityType_T)a_enmFacility);
1035 if (it != mData.mFacilityMap.end())
1036 {
1037 AdditionsFacility *pFac = it->second;
1038 fChanged = pFac->i_update((AdditionsFacilityStatus_T)a_enmStatus, a_fFlags, a_pTimeSpecTS);
1039 }
1040 else
1041 {
1042 if (mData.mFacilityMap.size() > 64)
1043 {
1044 /* The easy way out for now. We could automatically destroy
1045 inactive facilities like VMMDev does if we like... */
1046 AssertFailedReturn(false);
1047 }
1048
1049 ComObjPtr<AdditionsFacility> ptrFac;
1050 HRESULT hrc = ptrFac.createObject();
1051 AssertComRCReturn(hrc, false);
1052 Assert(ptrFac);
1053
1054 hrc = ptrFac->init(this, (AdditionsFacilityType_T)a_enmFacility, (AdditionsFacilityStatus_T)a_enmStatus,
1055 a_fFlags, a_pTimeSpecTS);
1056 AssertComRCReturn(hrc, false);
1057 try
1058 {
1059 mData.mFacilityMap.insert(std::make_pair((AdditionsFacilityType_T)a_enmFacility, ptrFac));
1060 fChanged = true;
1061 }
1062 catch (std::bad_alloc &)
1063 {
1064 fChanged = false;
1065 }
1066 }
1067 return fChanged;
1068}
1069
1070/**
1071 * Issued by the guest when a guest user changed its state.
1072 *
1073 * @return IPRT status code.
1074 * @param aUser Guest user name.
1075 * @param aDomain Domain of guest user account. Optional.
1076 * @param enmState New state to indicate.
1077 * @param pbDetails Pointer to state details. Optional.
1078 * @param cbDetails Size (in bytes) of state details. Pass 0 if not used.
1079 */
1080void Guest::i_onUserStateChanged(const Utf8Str &aUser, const Utf8Str &aDomain, VBoxGuestUserState enmState,
1081 const uint8_t *pbDetails, uint32_t cbDetails)
1082{
1083 RT_NOREF(pbDetails, cbDetails);
1084 LogFlowThisFunc(("\n"));
1085
1086 AutoCaller autoCaller(this);
1087 AssertComRCReturnVoid(autoCaller.rc());
1088
1089 Utf8Str strDetails; /** @todo Implement state details here. */
1090
1091 ::FireGuestUserStateChangedEvent(mEventSource, aUser, aDomain, (GuestUserState_T)enmState, strDetails);
1092 LogFlowFuncLeave();
1093}
1094
1095/**
1096 * Sets the status of a certain Guest Additions facility.
1097 *
1098 * Gets called by vmmdevUpdateGuestStatus, which just passes the report along.
1099 *
1100 * @param a_enmFacility The facility.
1101 * @param a_enmStatus The status.
1102 * @param a_fFlags Flags assoicated with the update. Currently
1103 * reserved and should be ignored.
1104 * @param a_pTimeSpecTS Pointer to the timestamp of this report.
1105 * @sa PDMIVMMDEVCONNECTOR::pfnUpdateGuestStatus, vmmdevUpdateGuestStatus
1106 * @thread The emulation thread.
1107 */
1108void Guest::i_setAdditionsStatus(VBoxGuestFacilityType a_enmFacility, VBoxGuestFacilityStatus a_enmStatus,
1109 uint32_t a_fFlags, PCRTTIMESPEC a_pTimeSpecTS)
1110{
1111 Assert( a_enmFacility > VBoxGuestFacilityType_Unknown
1112 && a_enmFacility <= VBoxGuestFacilityType_All); /* Paranoia, VMMDev checks for this. */
1113
1114 AutoCaller autoCaller(this);
1115 AssertComRCReturnVoid(autoCaller.rc());
1116
1117 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1118
1119 /*
1120 * Set a specific facility status.
1121 */
1122 bool fFireEvent = false;
1123 if (a_enmFacility == VBoxGuestFacilityType_All)
1124 for (FacilityMapIter it = mData.mFacilityMap.begin(); it != mData.mFacilityMap.end(); ++it)
1125 fFireEvent |= i_facilityUpdate((VBoxGuestFacilityType)it->first, a_enmStatus, a_fFlags, a_pTimeSpecTS);
1126 else /* Update one facility only. */
1127 fFireEvent = i_facilityUpdate(a_enmFacility, a_enmStatus, a_fFlags, a_pTimeSpecTS);
1128
1129 /*
1130 * Recalc the runlevel.
1131 */
1132 AdditionsRunLevelType_T const enmOldRunLevel = mData.mAdditionsRunLevel;
1133 if (i_facilityIsActive(VBoxGuestFacilityType_VBoxTrayClient))
1134 mData.mAdditionsRunLevel = AdditionsRunLevelType_Desktop;
1135 else if (i_facilityIsActive(VBoxGuestFacilityType_VBoxService))
1136 mData.mAdditionsRunLevel = AdditionsRunLevelType_Userland;
1137 else if (i_facilityIsActive(VBoxGuestFacilityType_VBoxGuestDriver))
1138 mData.mAdditionsRunLevel = AdditionsRunLevelType_System;
1139 else
1140 mData.mAdditionsRunLevel = AdditionsRunLevelType_None;
1141
1142 /*
1143 * Fire event if something actually changed.
1144 */
1145 AdditionsRunLevelType_T const enmNewRunLevel = mData.mAdditionsRunLevel;
1146 if (fFireEvent || enmNewRunLevel != enmOldRunLevel)
1147 {
1148 alock.release();
1149 ::FireGuestAdditionsStatusChangedEvent(mEventSource, (AdditionsFacilityType_T)a_enmFacility,
1150 (AdditionsFacilityStatus_T)a_enmStatus, enmNewRunLevel,
1151 RTTimeSpecGetMilli(a_pTimeSpecTS));
1152 }
1153}
1154
1155/**
1156 * Sets the supported features (and whether they are active or not).
1157 *
1158 * @param aCaps Guest capability bit mask (VMMDEV_GUEST_SUPPORTS_XXX).
1159 */
1160void Guest::i_setSupportedFeatures(uint32_t aCaps)
1161{
1162 AutoCaller autoCaller(this);
1163 AssertComRCReturnVoid(autoCaller.rc());
1164
1165 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1166
1167 /** @todo A nit: The timestamp is wrong on saved state restore. Would be better
1168 * to move the graphics and seamless capability -> facility translation to
1169 * VMMDev so this could be saved. */
1170 RTTIMESPEC TimeSpecTS;
1171 RTTimeNow(&TimeSpecTS);
1172
1173 bool fFireEvent = i_facilityUpdate(VBoxGuestFacilityType_Seamless,
1174 aCaps & VMMDEV_GUEST_SUPPORTS_SEAMLESS
1175 ? VBoxGuestFacilityStatus_Active : VBoxGuestFacilityStatus_Inactive,
1176 0 /*fFlags*/, &TimeSpecTS);
1177 /** @todo Add VMMDEV_GUEST_SUPPORTS_GUEST_HOST_WINDOW_MAPPING */
1178
1179 /*
1180 * Fire event if the state actually changed.
1181 */
1182 if (fFireEvent)
1183 {
1184 AdditionsRunLevelType_T const enmRunLevel = mData.mAdditionsRunLevel;
1185 alock.release();
1186 ::FireGuestAdditionsStatusChangedEvent(mEventSource, AdditionsFacilityType_Seamless,
1187 aCaps & VMMDEV_GUEST_SUPPORTS_SEAMLESS
1188 ? AdditionsFacilityStatus_Active : AdditionsFacilityStatus_Inactive,
1189 enmRunLevel, RTTimeSpecGetMilli(&TimeSpecTS));
1190 }
1191}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette