VirtualBox

source: vbox/trunk/src/VBox/Main/src-client/GuestCtrlPrivate.cpp@ 63156

Last change on this file since 63156 was 63154, checked in by vboxsync, 8 years ago

Main: More warnings about uninitialized variables (false ones) and size_t vs uint32_t mixup. Some data member renaming too.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 31.3 KB
Line 
1/* $Id: GuestCtrlPrivate.cpp 63154 2016-08-08 12:01:32Z vboxsync $ */
2/** @file
3 * Internal helpers/structures for guest control functionality.
4 */
5
6/*
7 * Copyright (C) 2011-2016 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
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#ifndef VBOX_WITH_GUEST_CONTROL
23# error "VBOX_WITH_GUEST_CONTROL must defined in this file"
24#endif
25#include "GuestCtrlImplPrivate.h"
26#include "GuestSessionImpl.h"
27#include "VMMDev.h"
28
29#include <iprt/asm.h>
30#include <iprt/cpp/utils.h> /* For unconst(). */
31#include <iprt/ctype.h>
32#ifdef DEBUG
33# include <iprt/file.h>
34#endif /* DEBUG */
35
36#ifdef LOG_GROUP
37 #undef LOG_GROUP
38#endif
39#define LOG_GROUP LOG_GROUP_GUEST_CONTROL
40#include <VBox/log.h>
41
42
43
44int GuestFsObjData::FromLs(const GuestProcessStreamBlock &strmBlk)
45{
46 LogFlowFunc(("\n"));
47
48 int rc = VINF_SUCCESS;
49
50 try
51 {
52#ifdef DEBUG
53 strmBlk.DumpToLog();
54#endif
55 /* Object name. */
56 mName = strmBlk.GetString("name");
57 if (mName.isEmpty()) throw VERR_NOT_FOUND;
58 /* Type. */
59 Utf8Str strType(strmBlk.GetString("ftype"));
60 if (strType.equalsIgnoreCase("-"))
61 mType = FsObjType_File;
62 else if (strType.equalsIgnoreCase("d"))
63 mType = FsObjType_Directory;
64 /** @todo Add more types! */
65 else
66 mType = FsObjType_Unknown;
67 /* Object size. */
68 rc = strmBlk.GetInt64Ex("st_size", &mObjectSize);
69 if (RT_FAILURE(rc)) throw rc;
70 /** @todo Add complete ls info! */
71 }
72 catch (int rc2)
73 {
74 rc = rc2;
75 }
76
77 LogFlowFuncLeaveRC(rc);
78 return rc;
79}
80
81int GuestFsObjData::FromMkTemp(const GuestProcessStreamBlock &strmBlk)
82{
83 LogFlowFunc(("\n"));
84
85 int rc;
86
87 try
88 {
89#ifdef DEBUG
90 strmBlk.DumpToLog();
91#endif
92 /* Object name. */
93 mName = strmBlk.GetString("name");
94 if (mName.isEmpty()) throw VERR_NOT_FOUND;
95 /* Assign the stream block's rc. */
96 rc = strmBlk.GetRc();
97 }
98 catch (int rc2)
99 {
100 rc = rc2;
101 }
102
103 LogFlowFuncLeaveRC(rc);
104 return rc;
105}
106
107int GuestFsObjData::FromStat(const GuestProcessStreamBlock &strmBlk)
108{
109 LogFlowFunc(("\n"));
110
111 int rc = VINF_SUCCESS;
112
113 try
114 {
115#ifdef DEBUG
116 strmBlk.DumpToLog();
117#endif
118 /* Node ID, optional because we don't include this
119 * in older VBoxService (< 4.2) versions. */
120 mNodeID = strmBlk.GetInt64("node_id");
121 /* Object name. */
122 mName = strmBlk.GetString("name");
123 if (mName.isEmpty()) throw VERR_NOT_FOUND;
124 /* Type. */
125 Utf8Str strType(strmBlk.GetString("ftype"));
126 if (strType.equalsIgnoreCase("-"))
127 mType = FsObjType_File;
128 else if (strType.equalsIgnoreCase("d"))
129 mType = FsObjType_Directory;
130 /** @todo Add more types! */
131 else
132 mType = FsObjType_Unknown;
133 /* Object size. */
134 rc = strmBlk.GetInt64Ex("st_size", &mObjectSize);
135 if (RT_FAILURE(rc)) throw rc;
136 /** @todo Add complete stat info! */
137 }
138 catch (int rc2)
139 {
140 rc = rc2;
141 }
142
143 LogFlowFuncLeaveRC(rc);
144 return rc;
145}
146
147///////////////////////////////////////////////////////////////////////////////
148
149/** @todo *NOT* thread safe yet! */
150/** @todo Add exception handling for STL stuff! */
151
152GuestProcessStreamBlock::GuestProcessStreamBlock(void)
153{
154
155}
156
157/*
158GuestProcessStreamBlock::GuestProcessStreamBlock(const GuestProcessStreamBlock &otherBlock)
159{
160 for (GuestCtrlStreamPairsIter it = otherBlock.mPairs.begin();
161 it != otherBlock.end(); ++it)
162 {
163 mPairs[it->first] = new
164 if (it->second.pszValue)
165 {
166 RTMemFree(it->second.pszValue);
167 it->second.pszValue = NULL;
168 }
169 }
170}*/
171
172GuestProcessStreamBlock::~GuestProcessStreamBlock()
173{
174 Clear();
175}
176
177/**
178 * Destroys the currently stored stream pairs.
179 *
180 * @return IPRT status code.
181 */
182void GuestProcessStreamBlock::Clear(void)
183{
184 mPairs.clear();
185}
186
187#ifdef DEBUG
188void GuestProcessStreamBlock::DumpToLog(void) const
189{
190 LogFlowFunc(("Dumping contents of stream block=0x%p (%ld items):\n",
191 this, mPairs.size()));
192
193 for (GuestCtrlStreamPairMapIterConst it = mPairs.begin();
194 it != mPairs.end(); ++it)
195 {
196 LogFlowFunc(("\t%s=%s\n", it->first.c_str(), it->second.mValue.c_str()));
197 }
198}
199#endif
200
201/**
202 * Returns a 64-bit signed integer of a specified key.
203 *
204 * @return IPRT status code. VERR_NOT_FOUND if key was not found.
205 * @param pszKey Name of key to get the value for.
206 * @param piVal Pointer to value to return.
207 */
208int GuestProcessStreamBlock::GetInt64Ex(const char *pszKey, int64_t *piVal) const
209{
210 AssertPtrReturn(pszKey, VERR_INVALID_POINTER);
211 AssertPtrReturn(piVal, VERR_INVALID_POINTER);
212 const char *pszValue = GetString(pszKey);
213 if (pszValue)
214 {
215 *piVal = RTStrToInt64(pszValue);
216 return VINF_SUCCESS;
217 }
218 return VERR_NOT_FOUND;
219}
220
221/**
222 * Returns a 64-bit integer of a specified key.
223 *
224 * @return int64_t Value to return, 0 if not found / on failure.
225 * @param pszKey Name of key to get the value for.
226 */
227int64_t GuestProcessStreamBlock::GetInt64(const char *pszKey) const
228{
229 int64_t iVal;
230 if (RT_SUCCESS(GetInt64Ex(pszKey, &iVal)))
231 return iVal;
232 return 0;
233}
234
235/**
236 * Returns the current number of stream pairs.
237 *
238 * @return uint32_t Current number of stream pairs.
239 */
240size_t GuestProcessStreamBlock::GetCount(void) const
241{
242 return mPairs.size();
243}
244
245/**
246 * Gets the return code (name = "rc") of this stream block.
247 *
248 * @return IPRT status code.
249 */
250int GuestProcessStreamBlock::GetRc(void) const
251{
252 const char *pszValue = GetString("rc");
253 if (pszValue)
254 {
255 return RTStrToInt16(pszValue);
256 }
257 return VERR_NOT_FOUND;
258}
259
260/**
261 * Returns a string value of a specified key.
262 *
263 * @return uint32_t Pointer to string to return, NULL if not found / on failure.
264 * @param pszKey Name of key to get the value for.
265 */
266const char* GuestProcessStreamBlock::GetString(const char *pszKey) const
267{
268 AssertPtrReturn(pszKey, NULL);
269
270 try
271 {
272 GuestCtrlStreamPairMapIterConst itPairs = mPairs.find(Utf8Str(pszKey));
273 if (itPairs != mPairs.end())
274 return itPairs->second.mValue.c_str();
275 }
276 catch (const std::exception &ex)
277 {
278 NOREF(ex);
279 }
280 return NULL;
281}
282
283/**
284 * Returns a 32-bit unsigned integer of a specified key.
285 *
286 * @return IPRT status code. VERR_NOT_FOUND if key was not found.
287 * @param pszKey Name of key to get the value for.
288 * @param puVal Pointer to value to return.
289 */
290int GuestProcessStreamBlock::GetUInt32Ex(const char *pszKey, uint32_t *puVal) const
291{
292 AssertPtrReturn(pszKey, VERR_INVALID_POINTER);
293 AssertPtrReturn(puVal, VERR_INVALID_POINTER);
294 const char *pszValue = GetString(pszKey);
295 if (pszValue)
296 {
297 *puVal = RTStrToUInt32(pszValue);
298 return VINF_SUCCESS;
299 }
300 return VERR_NOT_FOUND;
301}
302
303/**
304 * Returns a 32-bit unsigned integer of a specified key.
305 *
306 * @return uint32_t Value to return, 0 if not found / on failure.
307 * @param pszKey Name of key to get the value for.
308 */
309uint32_t GuestProcessStreamBlock::GetUInt32(const char *pszKey) const
310{
311 uint32_t uVal;
312 if (RT_SUCCESS(GetUInt32Ex(pszKey, &uVal)))
313 return uVal;
314 return 0;
315}
316
317/**
318 * Sets a value to a key or deletes a key by setting a NULL value.
319 *
320 * @return IPRT status code.
321 * @param pszKey Key name to process.
322 * @param pszValue Value to set. Set NULL for deleting the key.
323 */
324int GuestProcessStreamBlock::SetValue(const char *pszKey, const char *pszValue)
325{
326 AssertPtrReturn(pszKey, VERR_INVALID_POINTER);
327
328 int rc = VINF_SUCCESS;
329 try
330 {
331 Utf8Str Utf8Key(pszKey);
332
333 /* Take a shortcut and prevent crashes on some funny versions
334 * of STL if map is empty initially. */
335 if (!mPairs.empty())
336 {
337 GuestCtrlStreamPairMapIter it = mPairs.find(Utf8Key);
338 if (it != mPairs.end())
339 mPairs.erase(it);
340 }
341
342 if (pszValue)
343 {
344 GuestProcessStreamValue val(pszValue);
345 mPairs[Utf8Key] = val;
346 }
347 }
348 catch (const std::exception &ex)
349 {
350 NOREF(ex);
351 }
352 return rc;
353}
354
355///////////////////////////////////////////////////////////////////////////////
356
357GuestProcessStream::GuestProcessStream(void)
358 : m_cbAllocated(0),
359 m_cbUsed(0),
360 m_offBuffer(0),
361 m_pbBuffer(NULL)
362{
363
364}
365
366GuestProcessStream::~GuestProcessStream(void)
367{
368 Destroy();
369}
370
371/**
372 * Adds data to the internal parser buffer. Useful if there
373 * are multiple rounds of adding data needed.
374 *
375 * @return IPRT status code.
376 * @param pbData Pointer to data to add.
377 * @param cbData Size (in bytes) of data to add.
378 */
379int GuestProcessStream::AddData(const BYTE *pbData, size_t cbData)
380{
381 AssertPtrReturn(pbData, VERR_INVALID_POINTER);
382 AssertReturn(cbData, VERR_INVALID_PARAMETER);
383
384 int rc = VINF_SUCCESS;
385
386 /* Rewind the buffer if it's empty. */
387 size_t cbInBuf = m_cbUsed - m_offBuffer;
388 bool const fAddToSet = cbInBuf == 0;
389 if (fAddToSet)
390 m_cbUsed = m_offBuffer = 0;
391
392 /* Try and see if we can simply append the data. */
393 if (cbData + m_cbUsed <= m_cbAllocated)
394 {
395 memcpy(&m_pbBuffer[m_cbUsed], pbData, cbData);
396 m_cbUsed += cbData;
397 }
398 else
399 {
400 /* Move any buffered data to the front. */
401 cbInBuf = m_cbUsed - m_offBuffer;
402 if (cbInBuf == 0)
403 m_cbUsed = m_offBuffer = 0;
404 else if (m_offBuffer) /* Do we have something to move? */
405 {
406 memmove(m_pbBuffer, &m_pbBuffer[m_offBuffer], cbInBuf);
407 m_cbUsed = cbInBuf;
408 m_offBuffer = 0;
409 }
410
411 /* Do we need to grow the buffer? */
412 if (cbData + m_cbUsed > m_cbAllocated)
413 {
414/** @todo Put an upper limit on the allocation? */
415 size_t cbAlloc = m_cbUsed + cbData;
416 cbAlloc = RT_ALIGN_Z(cbAlloc, _64K);
417 void *pvNew = RTMemRealloc(m_pbBuffer, cbAlloc);
418 if (pvNew)
419 {
420 m_pbBuffer = (uint8_t *)pvNew;
421 m_cbAllocated = cbAlloc;
422 }
423 else
424 rc = VERR_NO_MEMORY;
425 }
426
427 /* Finally, copy the data. */
428 if (RT_SUCCESS(rc))
429 {
430 if (cbData + m_cbUsed <= m_cbAllocated)
431 {
432 memcpy(&m_pbBuffer[m_cbUsed], pbData, cbData);
433 m_cbUsed += cbData;
434 }
435 else
436 rc = VERR_BUFFER_OVERFLOW;
437 }
438 }
439
440 return rc;
441}
442
443/**
444 * Destroys the internal data buffer.
445 */
446void GuestProcessStream::Destroy(void)
447{
448 if (m_pbBuffer)
449 {
450 RTMemFree(m_pbBuffer);
451 m_pbBuffer = NULL;
452 }
453
454 m_cbAllocated = 0;
455 m_cbUsed = 0;
456 m_offBuffer = 0;
457}
458
459#ifdef DEBUG
460void GuestProcessStream::Dump(const char *pszFile)
461{
462 LogFlowFunc(("Dumping contents of stream=0x%p (cbAlloc=%u, cbSize=%u, cbOff=%u) to %s\n",
463 m_pbBuffer, m_cbAllocated, m_cbUsed, m_offBuffer, pszFile));
464
465 RTFILE hFile;
466 int rc = RTFileOpen(&hFile, pszFile, RTFILE_O_CREATE_REPLACE | RTFILE_O_WRITE | RTFILE_O_DENY_WRITE);
467 if (RT_SUCCESS(rc))
468 {
469 rc = RTFileWrite(hFile, m_pbBuffer, m_cbUsed, NULL /* pcbWritten */);
470 RTFileClose(hFile);
471 }
472}
473#endif
474
475/**
476 * Tries to parse the next upcoming pair block within the internal
477 * buffer.
478 *
479 * Returns VERR_NO_DATA is no data is in internal buffer or buffer has been
480 * completely parsed already.
481 *
482 * Returns VERR_MORE_DATA if current block was parsed (with zero or more pairs
483 * stored in stream block) but still contains incomplete (unterminated)
484 * data.
485 *
486 * Returns VINF_SUCCESS if current block was parsed until the next upcoming
487 * block (with zero or more pairs stored in stream block).
488 *
489 * @return IPRT status code.
490 * @param streamBlock Reference to guest stream block to fill.
491 *
492 */
493int GuestProcessStream::ParseBlock(GuestProcessStreamBlock &streamBlock)
494{
495 if ( !m_pbBuffer
496 || !m_cbUsed)
497 {
498 return VERR_NO_DATA;
499 }
500
501 AssertReturn(m_offBuffer <= m_cbUsed, VERR_INVALID_PARAMETER);
502 if (m_offBuffer == m_cbUsed)
503 return VERR_NO_DATA;
504
505 int rc = VINF_SUCCESS;
506
507 char *pszOff = (char*)&m_pbBuffer[m_offBuffer];
508 char *pszStart = pszOff;
509 uint32_t uDistance;
510 while (*pszStart)
511 {
512 size_t pairLen = strlen(pszStart);
513 uDistance = (pszStart - pszOff);
514 if (m_offBuffer + uDistance + pairLen + 1 >= m_cbUsed)
515 {
516 rc = VERR_MORE_DATA;
517 break;
518 }
519 else
520 {
521 char *pszSep = strchr(pszStart, '=');
522 char *pszVal = NULL;
523 if (pszSep)
524 pszVal = pszSep + 1;
525 if (!pszSep || !pszVal)
526 {
527 rc = VERR_MORE_DATA;
528 break;
529 }
530
531 /* Terminate the separator so that we can
532 * use pszStart as our key from now on. */
533 *pszSep = '\0';
534
535 rc = streamBlock.SetValue(pszStart, pszVal);
536 if (RT_FAILURE(rc))
537 return rc;
538 }
539
540 /* Next pair. */
541 pszStart += pairLen + 1;
542 }
543
544 /* If we did not do any movement but we have stuff left
545 * in our buffer just skip the current termination so that
546 * we can try next time. */
547 uDistance = (pszStart - pszOff);
548 if ( !uDistance
549 && *pszStart == '\0'
550 && m_offBuffer < m_cbUsed)
551 {
552 uDistance++;
553 }
554 m_offBuffer += uDistance;
555
556 return rc;
557}
558
559GuestBase::GuestBase(void)
560 : mConsole(NULL),
561 mNextContextID(0)
562{
563}
564
565GuestBase::~GuestBase(void)
566{
567}
568
569int GuestBase::baseInit(void)
570{
571 int rc = RTCritSectInit(&mWaitEventCritSect);
572
573 LogFlowFuncLeaveRC(rc);
574 return rc;
575}
576
577void GuestBase::baseUninit(void)
578{
579 LogFlowThisFuncEnter();
580
581 int rc = RTCritSectDelete(&mWaitEventCritSect);
582
583 LogFlowFuncLeaveRC(rc);
584 /* No return value. */
585}
586
587int GuestBase::cancelWaitEvents(void)
588{
589 LogFlowThisFuncEnter();
590
591 int rc = RTCritSectEnter(&mWaitEventCritSect);
592 if (RT_SUCCESS(rc))
593 {
594 GuestEventGroup::iterator itEventGroups = mWaitEventGroups.begin();
595 while (itEventGroups != mWaitEventGroups.end())
596 {
597 GuestWaitEvents::iterator itEvents = itEventGroups->second.begin();
598 while (itEvents != itEventGroups->second.end())
599 {
600 GuestWaitEvent *pEvent = itEvents->second;
601 AssertPtr(pEvent);
602
603 /*
604 * Just cancel the event, but don't remove it from the
605 * wait events map. Don't delete it though, this (hopefully)
606 * is done by the caller using unregisterWaitEvent().
607 */
608 int rc2 = pEvent->Cancel();
609 AssertRC(rc2);
610
611 ++itEvents;
612 }
613
614 ++itEventGroups;
615 }
616
617 int rc2 = RTCritSectLeave(&mWaitEventCritSect);
618 if (RT_SUCCESS(rc))
619 rc = rc2;
620 }
621
622 LogFlowFuncLeaveRC(rc);
623 return rc;
624}
625
626int GuestBase::dispatchGeneric(PVBOXGUESTCTRLHOSTCBCTX pCtxCb, PVBOXGUESTCTRLHOSTCALLBACK pSvcCb)
627{
628 LogFlowFunc(("pCtxCb=%p, pSvcCb=%p\n", pCtxCb, pSvcCb));
629
630 AssertPtrReturn(pCtxCb, VERR_INVALID_POINTER);
631 AssertPtrReturn(pSvcCb, VERR_INVALID_POINTER);
632
633 int vrc = VINF_SUCCESS;
634
635 try
636 {
637 LogFlowFunc(("uFunc=%RU32, cParms=%RU32\n",
638 pCtxCb->uFunction, pSvcCb->mParms));
639
640 switch (pCtxCb->uFunction)
641 {
642 case GUEST_MSG_PROGRESS_UPDATE:
643 break;
644
645 case GUEST_MSG_REPLY:
646 {
647 if (pSvcCb->mParms >= 3)
648 {
649 int idx = 1; /* Current parameter index. */
650 CALLBACKDATA_MSG_REPLY dataCb;
651 /* pSvcCb->mpaParms[0] always contains the context ID. */
652 vrc = pSvcCb->mpaParms[idx++].getUInt32(&dataCb.uType);
653 AssertRCReturn(vrc, vrc);
654 vrc = pSvcCb->mpaParms[idx++].getUInt32(&dataCb.rc);
655 AssertRCReturn(vrc, vrc);
656 vrc = pSvcCb->mpaParms[idx++].getPointer(&dataCb.pvPayload, &dataCb.cbPayload);
657 AssertRCReturn(vrc, vrc);
658
659 GuestWaitEventPayload evPayload(dataCb.uType, dataCb.pvPayload, dataCb.cbPayload);
660 int rc2 = signalWaitEventInternal(pCtxCb, dataCb.rc, &evPayload);
661 AssertRC(rc2);
662 }
663 else
664 vrc = VERR_INVALID_PARAMETER;
665 break;
666 }
667
668 default:
669 vrc = VERR_NOT_SUPPORTED;
670 break;
671 }
672 }
673 catch (std::bad_alloc)
674 {
675 vrc = VERR_NO_MEMORY;
676 }
677 catch (int rc)
678 {
679 vrc = rc;
680 }
681
682 LogFlowFuncLeaveRC(vrc);
683 return vrc;
684}
685
686int GuestBase::generateContextID(uint32_t uSessionID, uint32_t uObjectID, uint32_t *puContextID)
687{
688 AssertPtrReturn(puContextID, VERR_INVALID_POINTER);
689
690 if ( uSessionID >= VBOX_GUESTCTRL_MAX_SESSIONS
691 || uObjectID >= VBOX_GUESTCTRL_MAX_OBJECTS)
692 return VERR_INVALID_PARAMETER;
693
694 uint32_t uCount = ASMAtomicIncU32(&mNextContextID);
695 if (uCount == VBOX_GUESTCTRL_MAX_CONTEXTS)
696 uCount = 0;
697
698 uint32_t uNewContextID =
699 VBOX_GUESTCTRL_CONTEXTID_MAKE(uSessionID, uObjectID, uCount);
700
701 *puContextID = uNewContextID;
702
703#if 0
704 LogFlowThisFunc(("mNextContextID=%RU32, uSessionID=%RU32, uObjectID=%RU32, uCount=%RU32, uNewContextID=%RU32\n",
705 mNextContextID, uSessionID, uObjectID, uCount, uNewContextID));
706#endif
707 return VINF_SUCCESS;
708}
709
710int GuestBase::registerWaitEvent(uint32_t uSessionID, uint32_t uObjectID,
711 GuestWaitEvent **ppEvent)
712{
713 GuestEventTypes eventTypesEmpty;
714 return registerWaitEvent(uSessionID, uObjectID, eventTypesEmpty, ppEvent);
715}
716
717int GuestBase::registerWaitEvent(uint32_t uSessionID, uint32_t uObjectID,
718 const GuestEventTypes &lstEvents,
719 GuestWaitEvent **ppEvent)
720{
721 AssertPtrReturn(ppEvent, VERR_INVALID_POINTER);
722
723 uint32_t uContextID;
724 int rc = generateContextID(uSessionID, uObjectID, &uContextID);
725 if (RT_FAILURE(rc))
726 return rc;
727
728 rc = RTCritSectEnter(&mWaitEventCritSect);
729 if (RT_SUCCESS(rc))
730 {
731 try
732 {
733 GuestWaitEvent *pEvent = new GuestWaitEvent(uContextID, lstEvents);
734 AssertPtr(pEvent);
735
736 LogFlowThisFunc(("New event=%p, CID=%RU32\n", pEvent, uContextID));
737
738 /* Insert event into matching event group. This is for faster per-group
739 * lookup of all events later. */
740 for (GuestEventTypes::const_iterator itEvents = lstEvents.begin();
741 itEvents != lstEvents.end(); ++itEvents)
742 {
743 mWaitEventGroups[(*itEvents)].insert(
744 std::pair<uint32_t, GuestWaitEvent*>(uContextID, pEvent));
745 /** @todo Check for key collision. */
746 }
747
748 /* Register event in regular event list. */
749 /** @todo Check for key collisions. */
750 mWaitEvents[uContextID] = pEvent;
751
752 *ppEvent = pEvent;
753 }
754 catch(std::bad_alloc &)
755 {
756 rc = VERR_NO_MEMORY;
757 }
758
759 int rc2 = RTCritSectLeave(&mWaitEventCritSect);
760 if (RT_SUCCESS(rc))
761 rc = rc2;
762 }
763
764 return rc;
765}
766
767int GuestBase::signalWaitEvent(VBoxEventType_T aType, IEvent *aEvent)
768{
769 int rc = RTCritSectEnter(&mWaitEventCritSect);
770#ifdef DEBUG
771 uint32_t cEvents = 0;
772#endif
773 if (RT_SUCCESS(rc))
774 {
775 GuestEventGroup::iterator itGroup = mWaitEventGroups.find(aType);
776 if (itGroup != mWaitEventGroups.end())
777 {
778 GuestWaitEvents::iterator itEvents = itGroup->second.begin();
779 while (itEvents != itGroup->second.end())
780 {
781#ifdef DEBUG
782 LogFlowThisFunc(("Signalling event=%p, type=%ld (CID %RU32: Session=%RU32, Object=%RU32, Count=%RU32) ...\n",
783 itEvents->second, aType, itEvents->first,
784 VBOX_GUESTCTRL_CONTEXTID_GET_SESSION(itEvents->first),
785 VBOX_GUESTCTRL_CONTEXTID_GET_OBJECT(itEvents->first),
786 VBOX_GUESTCTRL_CONTEXTID_GET_COUNT(itEvents->first)));
787#endif
788 ComPtr<IEvent> pThisEvent = aEvent;
789 Assert(!pThisEvent.isNull());
790 int rc2 = itEvents->second->SignalExternal(aEvent);
791 if (RT_SUCCESS(rc))
792 rc = rc2;
793
794 if (RT_SUCCESS(rc2))
795 {
796 /* Remove the event from all other event groups (except the
797 * original one!) because it was signalled. */
798 AssertPtr(itEvents->second);
799 const GuestEventTypes evTypes = itEvents->second->Types();
800 for (GuestEventTypes::const_iterator itType = evTypes.begin();
801 itType != evTypes.end(); ++itType)
802 {
803 if ((*itType) != aType) /* Only remove all other groups. */
804 {
805 /* Get current event group. */
806 GuestEventGroup::iterator evGroup = mWaitEventGroups.find((*itType));
807 Assert(evGroup != mWaitEventGroups.end());
808
809 /* Lookup event in event group. */
810 GuestWaitEvents::iterator evEvent = evGroup->second.find(itEvents->first /* Context ID */);
811 Assert(evEvent != evGroup->second.end());
812
813 LogFlowThisFunc(("Removing event=%p (type %ld)\n", evEvent->second, (*itType)));
814 evGroup->second.erase(evEvent);
815
816 LogFlowThisFunc(("%zu events for type=%ld left\n",
817 evGroup->second.size(), aType));
818 }
819 }
820
821 /* Remove the event from the passed-in event group. */
822 itGroup->second.erase(itEvents++);
823 }
824 else
825 ++itEvents;
826#ifdef DEBUG
827 cEvents++;
828#endif
829 }
830 }
831
832 int rc2 = RTCritSectLeave(&mWaitEventCritSect);
833 if (RT_SUCCESS(rc))
834 rc = rc2;
835 }
836
837#ifdef DEBUG
838 LogFlowThisFunc(("Signalled %RU32 events, rc=%Rrc\n", cEvents, rc));
839#endif
840 return rc;
841}
842
843int GuestBase::signalWaitEventInternal(PVBOXGUESTCTRLHOSTCBCTX pCbCtx,
844 int guestRc, const GuestWaitEventPayload *pPayload)
845{
846 if (RT_SUCCESS(guestRc))
847 return signalWaitEventInternalEx(pCbCtx, VINF_SUCCESS,
848 0 /* Guest rc */, pPayload);
849
850 return signalWaitEventInternalEx(pCbCtx, VERR_GSTCTL_GUEST_ERROR,
851 guestRc, pPayload);
852}
853
854int GuestBase::signalWaitEventInternalEx(PVBOXGUESTCTRLHOSTCBCTX pCbCtx,
855 int rc, int guestRc,
856 const GuestWaitEventPayload *pPayload)
857{
858 AssertPtrReturn(pCbCtx, VERR_INVALID_POINTER);
859 /* pPayload is optional. */
860
861 int rc2;
862 GuestWaitEvents::iterator itEvent = mWaitEvents.find(pCbCtx->uContextID);
863 if (itEvent != mWaitEvents.end())
864 {
865 LogFlowThisFunc(("Signalling event=%p (CID %RU32, rc=%Rrc, guestRc=%Rrc, pPayload=%p) ...\n",
866 itEvent->second, itEvent->first, rc, guestRc, pPayload));
867 GuestWaitEvent *pEvent = itEvent->second;
868 AssertPtr(pEvent);
869 rc2 = pEvent->SignalInternal(rc, guestRc, pPayload);
870 }
871 else
872 rc2 = VERR_NOT_FOUND;
873
874 return rc2;
875}
876
877void GuestBase::unregisterWaitEvent(GuestWaitEvent *pEvent)
878{
879 if (!pEvent) /* Nothing to unregister. */
880 return;
881
882 int rc = RTCritSectEnter(&mWaitEventCritSect);
883 if (RT_SUCCESS(rc))
884 {
885 LogFlowThisFunc(("pEvent=%p\n", pEvent));
886
887 const GuestEventTypes lstTypes = pEvent->Types();
888 for (GuestEventTypes::const_iterator itEvents = lstTypes.begin();
889 itEvents != lstTypes.end(); ++itEvents)
890 {
891 /** @todo Slow O(n) lookup. Optimize this. */
892 GuestWaitEvents::iterator itCurEvent = mWaitEventGroups[(*itEvents)].begin();
893 while (itCurEvent != mWaitEventGroups[(*itEvents)].end())
894 {
895 if (itCurEvent->second == pEvent)
896 {
897 mWaitEventGroups[(*itEvents)].erase(itCurEvent++);
898 break;
899 }
900 else
901 ++itCurEvent;
902 }
903 }
904
905 delete pEvent;
906 pEvent = NULL;
907
908 int rc2 = RTCritSectLeave(&mWaitEventCritSect);
909 if (RT_SUCCESS(rc))
910 rc = rc2;
911 }
912}
913
914/**
915 * Waits for a formerly registered guest event.
916 *
917 * @return IPRT status code.
918 * @param pEvent Pointer to event to wait for.
919 * @param uTimeoutMS Timeout (in ms) for waiting.
920 * @param pType Event type of following IEvent.
921 * Optional.
922 * @param ppEvent Pointer to IEvent which got triggered
923 * for this event. Optional.
924 */
925int GuestBase::waitForEvent(GuestWaitEvent *pEvent, uint32_t uTimeoutMS,
926 VBoxEventType_T *pType, IEvent **ppEvent)
927{
928 AssertPtrReturn(pEvent, VERR_INVALID_POINTER);
929 /* pType is optional. */
930 /* ppEvent is optional. */
931
932 int vrc = pEvent->Wait(uTimeoutMS);
933 if (RT_SUCCESS(vrc))
934 {
935 const ComPtr<IEvent> pThisEvent = pEvent->Event();
936 if (!pThisEvent.isNull()) /* Having a VBoxEventType_ event is optional. */
937 {
938 if (pType)
939 {
940 HRESULT hr = pThisEvent->COMGETTER(Type)(pType);
941 if (FAILED(hr))
942 vrc = VERR_COM_UNEXPECTED;
943 }
944 if ( RT_SUCCESS(vrc)
945 && ppEvent)
946 pThisEvent.queryInterfaceTo(ppEvent);
947
948 unconst(pThisEvent).setNull();
949 }
950 }
951
952 return vrc;
953}
954
955GuestObject::GuestObject(void)
956 : mSession(NULL),
957 mObjectID(0)
958{
959}
960
961GuestObject::~GuestObject(void)
962{
963}
964
965int GuestObject::bindToSession(Console *pConsole, GuestSession *pSession, uint32_t uObjectID)
966{
967 AssertPtrReturn(pConsole, VERR_INVALID_POINTER);
968 AssertPtrReturn(pSession, VERR_INVALID_POINTER);
969
970 mConsole = pConsole;
971 mSession = pSession;
972 mObjectID = uObjectID;
973
974 return VINF_SUCCESS;
975}
976
977int GuestObject::registerWaitEvent(const GuestEventTypes &lstEvents,
978 GuestWaitEvent **ppEvent)
979{
980 AssertPtr(mSession);
981 return GuestBase::registerWaitEvent(mSession->i_getId(), mObjectID, lstEvents, ppEvent);
982}
983
984int GuestObject::sendCommand(uint32_t uFunction,
985 uint32_t uParms, PVBOXHGCMSVCPARM paParms)
986{
987#ifndef VBOX_GUESTCTRL_TEST_CASE
988 ComObjPtr<Console> pConsole = mConsole;
989 Assert(!pConsole.isNull());
990
991 int vrc = VERR_HGCM_SERVICE_NOT_FOUND;
992
993 /* Forward the information to the VMM device. */
994 VMMDev *pVMMDev = pConsole->i_getVMMDev();
995 if (pVMMDev)
996 {
997 LogFlowThisFunc(("uFunction=%RU32, uParms=%RU32\n", uFunction, uParms));
998 vrc = pVMMDev->hgcmHostCall(HGCMSERVICE_NAME, uFunction, uParms, paParms);
999 if (RT_FAILURE(vrc))
1000 {
1001 /** @todo What to do here? */
1002 }
1003 }
1004#else
1005 LogFlowThisFuncEnter();
1006
1007 /* Not needed within testcases. */
1008 int vrc = VINF_SUCCESS;
1009#endif
1010 return vrc;
1011}
1012
1013GuestWaitEventBase::GuestWaitEventBase(void)
1014 : mfAborted(false),
1015 mCID(0),
1016 mEventSem(NIL_RTSEMEVENT),
1017 mRc(VINF_SUCCESS),
1018 mGuestRc(VINF_SUCCESS)
1019{
1020}
1021
1022GuestWaitEventBase::~GuestWaitEventBase(void)
1023{
1024 if (mEventSem != NIL_RTSEMEVENT)
1025 {
1026 RTSemEventDestroy(mEventSem);
1027 mEventSem = NIL_RTSEMEVENT;
1028 }
1029}
1030
1031int GuestWaitEventBase::Init(uint32_t uCID)
1032{
1033 mCID = uCID;
1034
1035 return RTSemEventCreate(&mEventSem);
1036}
1037
1038int GuestWaitEventBase::SignalInternal(int rc, int guestRc,
1039 const GuestWaitEventPayload *pPayload)
1040{
1041 if (ASMAtomicReadBool(&mfAborted))
1042 return VERR_CANCELLED;
1043
1044#ifdef VBOX_STRICT
1045 if (rc == VERR_GSTCTL_GUEST_ERROR)
1046 AssertMsg(RT_FAILURE(guestRc), ("Guest error indicated but no actual guest error set (%Rrc)\n", guestRc));
1047 else
1048 AssertMsg(RT_SUCCESS(guestRc), ("No guest error indicated but actual guest error set (%Rrc)\n", guestRc));
1049#endif
1050
1051 int rc2;
1052 if (pPayload)
1053 rc2 = mPayload.CopyFromDeep(*pPayload);
1054 else
1055 rc2 = VINF_SUCCESS;
1056 if (RT_SUCCESS(rc2))
1057 {
1058 mRc = rc;
1059 mGuestRc = guestRc;
1060
1061 rc2 = RTSemEventSignal(mEventSem);
1062 }
1063
1064 return rc2;
1065}
1066
1067int GuestWaitEventBase::Wait(RTMSINTERVAL uTimeoutMS)
1068{
1069 int rc = VINF_SUCCESS;
1070
1071 if (ASMAtomicReadBool(&mfAborted))
1072 rc = VERR_CANCELLED;
1073
1074 if (RT_SUCCESS(rc))
1075 {
1076 AssertReturn(mEventSem != NIL_RTSEMEVENT, VERR_CANCELLED);
1077
1078 RTMSINTERVAL msInterval = uTimeoutMS;
1079 if (!uTimeoutMS)
1080 msInterval = RT_INDEFINITE_WAIT;
1081 rc = RTSemEventWait(mEventSem, msInterval);
1082 if (ASMAtomicReadBool(&mfAborted))
1083 rc = VERR_CANCELLED;
1084 if (RT_SUCCESS(rc))
1085 {
1086 /* If waiting succeeded, return the overall
1087 * result code. */
1088 rc = mRc;
1089 }
1090 }
1091
1092 return rc;
1093}
1094
1095GuestWaitEvent::GuestWaitEvent(uint32_t uCID,
1096 const GuestEventTypes &lstEvents)
1097{
1098 int rc2 = Init(uCID);
1099 AssertRC(rc2); /** @todo Throw exception here. */
1100
1101 mEventTypes = lstEvents;
1102}
1103
1104GuestWaitEvent::GuestWaitEvent(uint32_t uCID)
1105{
1106 int rc2 = Init(uCID);
1107 AssertRC(rc2); /** @todo Throw exception here. */
1108}
1109
1110GuestWaitEvent::~GuestWaitEvent(void)
1111{
1112
1113}
1114
1115/**
1116 * Cancels the event.
1117 */
1118int GuestWaitEvent::Cancel(void)
1119{
1120 AssertReturn(!mfAborted, VERR_CANCELLED);
1121 ASMAtomicWriteBool(&mfAborted, true);
1122
1123#ifdef DEBUG_andy
1124 LogFlowThisFunc(("Cancelling %p ...\n"));
1125#endif
1126 return RTSemEventSignal(mEventSem);
1127}
1128
1129int GuestWaitEvent::Init(uint32_t uCID)
1130{
1131 return GuestWaitEventBase::Init(uCID);
1132}
1133
1134/**
1135 * Signals the event.
1136 *
1137 * @return IPRT status code.
1138 * @param pEvent Public IEvent to associate.
1139 * Optional.
1140 */
1141int GuestWaitEvent::SignalExternal(IEvent *pEvent)
1142{
1143 AssertReturn(mEventSem != NIL_RTSEMEVENT, VERR_CANCELLED);
1144
1145 if (pEvent)
1146 mEvent = pEvent;
1147
1148 return RTSemEventSignal(mEventSem);
1149}
1150
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