VirtualBox

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

Last change on this file since 46446 was 46446, checked in by vboxsync, 12 years ago

Guest::updateStats: Note on using STAMR3Enum like that...

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

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