int GuestFile::i_writeDataAt(uint64_t uOffset, uint32_t uTimeoutMS, void *pvData, uint32_t cbData, uint32_t *pcbWritten) { AssertPtrReturn(pvData, VERR_INVALID_POINTER); AssertReturn(cbData, VERR_INVALID_PARAMETER); LogFlowThisFunc(("uOffset=%RU64, uTimeoutMS=%RU32, pvData=%p, cbData=%zu\n", uOffset, uTimeoutMS, pvData, cbData)); AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); int vrc; GuestWaitEvent *pEvent = NULL; GuestEventTypes eventTypes; try { eventTypes.push_back(VBoxEventType_OnGuestFileStateChanged); eventTypes.push_back(VBoxEventType_OnGuestFileWrite); vrc = registerWaitEvent(eventTypes, &pEvent); } catch (std::bad_alloc) { vrc = VERR_NO_MEMORY; } if (RT_FAILURE(vrc)) return vrc; /* Prepare HGCM call. */ VBOXHGCMSVCPARM paParms[8]; int i = 0; paParms[i++].setUInt32(pEvent->ContextID()); paParms[i++].setUInt32(mData.mID /* File handle */); paParms[i++].setUInt64(uOffset /* Offset where to starting writing */); paParms[i++].setUInt32(cbData /* Size (in bytes) to write */); paParms[i++].setPointer(pvData, cbData); alock.release(); /* Drop write lock before sending. */ vrc = sendCommand(HOST_FILE_WRITE_AT, i, paParms); if (RT_SUCCESS(vrc)) { uint32_t cbWritten = 0; vrc = i_waitForWrite(pEvent, uTimeoutMS, &cbWritten); if (RT_SUCCESS(vrc)) { LogFlowThisFunc(("cbWritten=%RU32\n", cbWritten)); if (cbWritten) *pcbWritten = cbWritten; } } unregisterWaitEvent(pEvent); LogFlowFuncLeaveRC(vrc); return vrc; }
int GuestFile::i_readData(uint32_t uSize, uint32_t uTimeoutMS, void* pvData, uint32_t cbData, uint32_t* pcbRead) { AssertPtrReturn(pvData, VERR_INVALID_POINTER); AssertReturn(cbData, VERR_INVALID_PARAMETER); LogFlowThisFunc(("uSize=%RU32, uTimeoutMS=%RU32, pvData=%p, cbData=%zu\n", uSize, uTimeoutMS, pvData, cbData)); AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); int vrc; GuestWaitEvent *pEvent = NULL; GuestEventTypes eventTypes; try { eventTypes.push_back(VBoxEventType_OnGuestFileStateChanged); eventTypes.push_back(VBoxEventType_OnGuestFileRead); vrc = registerWaitEvent(eventTypes, &pEvent); } catch (std::bad_alloc) { vrc = VERR_NO_MEMORY; } if (RT_FAILURE(vrc)) return vrc; /* Prepare HGCM call. */ VBOXHGCMSVCPARM paParms[4]; int i = 0; paParms[i++].setUInt32(pEvent->ContextID()); paParms[i++].setUInt32(mData.mID /* File handle */); paParms[i++].setUInt32(uSize /* Size (in bytes) to read */); alock.release(); /* Drop write lock before sending. */ vrc = sendCommand(HOST_FILE_READ, i, paParms); if (RT_SUCCESS(vrc)) { uint32_t cbRead = 0; vrc = i_waitForRead(pEvent, uTimeoutMS, pvData, cbData, &cbRead); if (RT_SUCCESS(vrc)) { LogFlowThisFunc(("cbRead=%RU32\n", cbRead)); if (pcbRead) *pcbRead = cbRead; } } unregisterWaitEvent(pEvent); LogFlowFuncLeaveRC(vrc); return vrc; }
int GuestFile::openFile(uint32_t uTimeoutMS, int *pGuestRc) { LogFlowThisFuncEnter(); AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); LogFlowThisFunc(("strFile=%s, strOpenMode=%s, strDisposition=%s, uCreationMode=%RU32, uOffset=%RU64\n", mData.mOpenInfo.mFileName.c_str(), mData.mOpenInfo.mOpenMode.c_str(), mData.mOpenInfo.mDisposition.c_str(), mData.mOpenInfo.mCreationMode, mData.mOpenInfo.mInitialOffset)); int vrc; GuestWaitEvent *pEvent = NULL; GuestEventTypes eventTypes; try { eventTypes.push_back(VBoxEventType_OnGuestFileStateChanged); vrc = registerWaitEvent(eventTypes, &pEvent); } catch (std::bad_alloc) { vrc = VERR_NO_MEMORY; } if (RT_FAILURE(vrc)) return vrc; /* Prepare HGCM call. */ VBOXHGCMSVCPARM paParms[8]; int i = 0; paParms[i++].setUInt32(pEvent->ContextID()); paParms[i++].setPointer((void*)mData.mOpenInfo.mFileName.c_str(), (ULONG)mData.mOpenInfo.mFileName.length() + 1); paParms[i++].setPointer((void*)mData.mOpenInfo.mOpenMode.c_str(), (ULONG)mData.mOpenInfo.mOpenMode.length() + 1); paParms[i++].setPointer((void*)mData.mOpenInfo.mDisposition.c_str(), (ULONG)mData.mOpenInfo.mDisposition.length() + 1); paParms[i++].setPointer((void*)mData.mOpenInfo.mSharingMode.c_str(), (ULONG)mData.mOpenInfo.mSharingMode.length() + 1); paParms[i++].setUInt32(mData.mOpenInfo.mCreationMode); paParms[i++].setUInt64(mData.mOpenInfo.mInitialOffset); alock.release(); /* Drop write lock before sending. */ vrc = sendCommand(HOST_FILE_OPEN, i, paParms); if (RT_SUCCESS(vrc)) vrc = waitForStatusChange(pEvent, uTimeoutMS, NULL /* FileStatus */, pGuestRc); unregisterWaitEvent(pEvent); LogFlowFuncLeaveRC(vrc); return vrc; }
int GuestFile::i_openFile(uint32_t uTimeoutMS, int *pGuestRc) { LogFlowThisFuncEnter(); AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); LogFlowThisFunc(("strFile=%s, enmAccessMode=%d (%s) enmOpenAction=%d (%s) uCreationMode=%RU32, mfOpenEx=%RU32\n", mData.mOpenInfo.mFileName.c_str(), mData.mOpenInfo.mAccessMode, mData.mOpenInfo.mpszAccessMode, mData.mOpenInfo.mOpenAction, mData.mOpenInfo.mpszOpenAction, mData.mOpenInfo.mCreationMode, mData.mOpenInfo.mfOpenEx)); int vrc; GuestWaitEvent *pEvent = NULL; GuestEventTypes eventTypes; try { eventTypes.push_back(VBoxEventType_OnGuestFileStateChanged); vrc = registerWaitEvent(eventTypes, &pEvent); } catch (std::bad_alloc) { vrc = VERR_NO_MEMORY; } if (RT_FAILURE(vrc)) return vrc; /* Prepare HGCM call. */ VBOXHGCMSVCPARM paParms[8]; int i = 0; paParms[i++].setUInt32(pEvent->ContextID()); paParms[i++].setPointer((void*)mData.mOpenInfo.mFileName.c_str(), (ULONG)mData.mOpenInfo.mFileName.length() + 1); paParms[i++].setString(mData.mOpenInfo.mpszAccessMode); paParms[i++].setString(mData.mOpenInfo.mpszOpenAction); paParms[i++].setString(""); /** @todo sharing mode. */ paParms[i++].setUInt32(mData.mOpenInfo.mCreationMode); paParms[i++].setUInt64(0 /* initial offset */); /** @todo Next protocol version: add flags, replace strings, remove initial offset. */ alock.release(); /* Drop write lock before sending. */ vrc = sendCommand(HOST_FILE_OPEN, i, paParms); if (RT_SUCCESS(vrc)) vrc = i_waitForStatusChange(pEvent, uTimeoutMS, NULL /* FileStatus */, pGuestRc); unregisterWaitEvent(pEvent); LogFlowFuncLeaveRC(vrc); return vrc; }
int GuestBase::registerWaitEvent(uint32_t uSessionID, uint32_t uObjectID, const GuestEventTypes &lstEvents, GuestWaitEvent **ppEvent) { AssertPtrReturn(ppEvent, VERR_INVALID_POINTER); uint32_t uContextID; int rc = generateContextID(uSessionID, uObjectID, &uContextID); if (RT_FAILURE(rc)) return rc; rc = RTCritSectEnter(&mWaitEventCritSect); if (RT_SUCCESS(rc)) { try { GuestWaitEvent *pEvent = new GuestWaitEvent(uContextID, lstEvents); AssertPtr(pEvent); LogFlowThisFunc(("New event=%p, CID=%RU32\n", pEvent, uContextID)); /* Insert event into matching event group. This is for faster per-group * lookup of all events later. */ for (GuestEventTypes::const_iterator itEvents = lstEvents.begin(); itEvents != lstEvents.end(); itEvents++) { mWaitEventGroups[(*itEvents)].insert( std::pair<uint32_t, GuestWaitEvent*>(uContextID, pEvent)); /** @todo Check for key collision. */ } /* Register event in regular event list. */ /** @todo Check for key collisions. */ mWaitEvents[uContextID] = pEvent; *ppEvent = pEvent; } catch(std::bad_alloc &) { rc = VERR_NO_MEMORY; } int rc2 = RTCritSectLeave(&mWaitEventCritSect); if (RT_SUCCESS(rc)) rc = rc2; } return rc; }
int GuestFile::i_seekAt(int64_t iOffset, GUEST_FILE_SEEKTYPE eSeekType, uint32_t uTimeoutMS, uint64_t *puOffset) { LogFlowThisFunc(("iOffset=%RI64, uTimeoutMS=%RU32\n", iOffset, uTimeoutMS)); AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); int vrc; GuestWaitEvent *pEvent = NULL; GuestEventTypes eventTypes; try { eventTypes.push_back(VBoxEventType_OnGuestFileStateChanged); eventTypes.push_back(VBoxEventType_OnGuestFileOffsetChanged); vrc = registerWaitEvent(eventTypes, &pEvent); } catch (std::bad_alloc) { vrc = VERR_NO_MEMORY; } if (RT_FAILURE(vrc)) return vrc; /* Prepare HGCM call. */ VBOXHGCMSVCPARM paParms[4]; int i = 0; paParms[i++].setUInt32(pEvent->ContextID()); paParms[i++].setUInt32(mData.mID /* File handle */); paParms[i++].setUInt32(eSeekType /* Seek method */); /** @todo uint64_t vs. int64_t! */ paParms[i++].setUInt64((uint64_t)iOffset /* Offset (in bytes) to start reading */); alock.release(); /* Drop write lock before sending. */ vrc = sendCommand(HOST_FILE_SEEK, i, paParms); if (RT_SUCCESS(vrc)) vrc = i_waitForOffsetChange(pEvent, uTimeoutMS, puOffset); unregisterWaitEvent(pEvent); LogFlowFuncLeaveRC(vrc); return vrc; }
int GuestFile::i_closeFile(int *pGuestRc) { LogFlowThisFunc(("strFile=%s\n", mData.mOpenInfo.mFileName.c_str())); int vrc; GuestWaitEvent *pEvent = NULL; GuestEventTypes eventTypes; try { eventTypes.push_back(VBoxEventType_OnGuestFileStateChanged); vrc = registerWaitEvent(eventTypes, &pEvent); } catch (std::bad_alloc) { vrc = VERR_NO_MEMORY; } if (RT_FAILURE(vrc)) return vrc; /* Prepare HGCM call. */ VBOXHGCMSVCPARM paParms[4]; int i = 0; paParms[i++].setUInt32(pEvent->ContextID()); paParms[i++].setUInt32(mData.mID /* Guest file ID */); vrc = sendCommand(HOST_FILE_CLOSE, i, paParms); if (RT_SUCCESS(vrc)) vrc = i_waitForStatusChange(pEvent, 30 * 1000 /* Timeout in ms */, NULL /* FileStatus */, pGuestRc); unregisterWaitEvent(pEvent); LogFlowFuncLeaveRC(vrc); return vrc; }
void GuestBase::unregisterWaitEvent(GuestWaitEvent *pEvent) { if (!pEvent) /* Nothing to unregister. */ return; int rc = RTCritSectEnter(&mWaitEventCritSect); if (RT_SUCCESS(rc)) { LogFlowThisFunc(("pEvent=%p\n", pEvent)); const GuestEventTypes lstTypes = pEvent->Types(); for (GuestEventTypes::const_iterator itEvents = lstTypes.begin(); itEvents != lstTypes.end(); itEvents++) { /** @todo Slow O(n) lookup. Optimize this. */ GuestWaitEvents::iterator itCurEvent = mWaitEventGroups[(*itEvents)].begin(); while (itCurEvent != mWaitEventGroups[(*itEvents)].end()) { if (itCurEvent->second == pEvent) { mWaitEventGroups[(*itEvents)].erase(itCurEvent++); break; } else itCurEvent++; } } delete pEvent; pEvent = NULL; int rc2 = RTCritSectLeave(&mWaitEventCritSect); if (RT_SUCCESS(rc)) rc = rc2; } }
int GuestBase::signalWaitEvent(VBoxEventType_T aType, IEvent *aEvent) { int rc = RTCritSectEnter(&mWaitEventCritSect); #ifdef DEBUG uint32_t cEvents = 0; #endif if (RT_SUCCESS(rc)) { GuestEventGroup::iterator itGroup = mWaitEventGroups.find(aType); if (itGroup != mWaitEventGroups.end()) { GuestWaitEvents::iterator itEvents = itGroup->second.begin(); while (itEvents != itGroup->second.end()) { #ifdef DEBUG LogFlowThisFunc(("Signalling event=%p, type=%ld (CID %RU32: Session=%RU32, Object=%RU32, Count=%RU32) ...\n", itEvents->second, aType, itEvents->first, VBOX_GUESTCTRL_CONTEXTID_GET_SESSION(itEvents->first), VBOX_GUESTCTRL_CONTEXTID_GET_OBJECT(itEvents->first), VBOX_GUESTCTRL_CONTEXTID_GET_COUNT(itEvents->first))); #endif ComPtr<IEvent> pThisEvent = aEvent; Assert(!pThisEvent.isNull()); int rc2 = itEvents->second->SignalExternal(aEvent); if (RT_SUCCESS(rc)) rc = rc2; if (RT_SUCCESS(rc2)) { /* Remove the event from all other event groups (except the * original one!) because it was signalled. */ AssertPtr(itEvents->second); const GuestEventTypes evTypes = itEvents->second->Types(); for (GuestEventTypes::const_iterator itType = evTypes.begin(); itType != evTypes.end(); itType++) { if ((*itType) != aType) /* Only remove all other groups. */ { /* Get current event group. */ GuestEventGroup::iterator evGroup = mWaitEventGroups.find((*itType)); Assert(evGroup != mWaitEventGroups.end()); /* Lookup event in event group. */ GuestWaitEvents::iterator evEvent = evGroup->second.find(itEvents->first /* Context ID */); Assert(evEvent != evGroup->second.end()); LogFlowThisFunc(("Removing event=%p (type %ld)\n", evEvent->second, (*itType))); evGroup->second.erase(evEvent); LogFlowThisFunc(("%zu events for type=%ld left\n", evGroup->second.size(), aType)); } } /* Remove the event from the passed-in event group. */ itGroup->second.erase(itEvents++); } else itEvents++; #ifdef DEBUG cEvents++; #endif } } int rc2 = RTCritSectLeave(&mWaitEventCritSect); if (RT_SUCCESS(rc)) rc = rc2; } #ifdef DEBUG LogFlowThisFunc(("Signalled %RU32 events, rc=%Rrc\n", cEvents, rc)); #endif return rc; }