/** * Performs the required actions when a device has been added. * * This means things like running filters and subsequent capturing and * VM attaching. This may result in IPC and temporary lock abandonment. * * @param aDevice The device in question. * @param aUSBDevice The USB device structure. */ void USBProxyService::deviceAdded(ComObjPtr<HostUSBDevice> &aDevice, SessionMachinesList &llOpenedMachines, PUSBDEVICE aUSBDevice) { /* * Validate preconditions. */ AssertReturnVoid(!isWriteLockOnCurrentThread()); AssertReturnVoid(!aDevice->isWriteLockOnCurrentThread()); AutoReadLock devLock(aDevice COMMA_LOCKVAL_SRC_POS); LogFlowThisFunc(("aDevice=%p name={%s} state=%s id={%RTuuid}\n", (HostUSBDevice *)aDevice, aDevice->getName().c_str(), aDevice->getStateName(), aDevice->getId().raw())); /* * Run filters on the device. */ if (aDevice->isCapturableOrHeld()) { devLock.release(); HRESULT rc = runAllFiltersOnDevice(aDevice, llOpenedMachines, NULL /* aIgnoreMachine */); AssertComRC(rc); } NOREF(aUSBDevice); }
/** * Handle a device which state changed in some significant way. * * This means things like running filters and subsequent capturing and * VM attaching. This may result in IPC and temporary lock abandonment. * * @param aDevice The device. * @param pllOpenedMachines list of running session machines (VirtualBox::getOpenedMachines()); if NULL, we don't run filters * @param aIgnoreMachine Machine to ignore when running filters. */ void USBProxyService::deviceChanged(ComObjPtr<HostUSBDevice> &aDevice, SessionMachinesList *pllOpenedMachines, SessionMachine *aIgnoreMachine) { /* * Validate preconditions. */ AssertReturnVoid(!isWriteLockOnCurrentThread()); AssertReturnVoid(!aDevice->isWriteLockOnCurrentThread()); AutoReadLock devLock(aDevice COMMA_LOCKVAL_SRC_POS); LogFlowThisFunc(("aDevice=%p name={%s} state=%s id={%RTuuid} aRunFilters=%RTbool aIgnoreMachine=%p\n", (HostUSBDevice *)aDevice, aDevice->getName().c_str(), aDevice->getStateName(), aDevice->getId().raw(), (pllOpenedMachines != NULL), // used to be "bool aRunFilters" aIgnoreMachine)); devLock.release(); /* * Run filters if requested to do so. */ if (pllOpenedMachines) { HRESULT rc = runAllFiltersOnDevice(aDevice, *pllOpenedMachines, aIgnoreMachine); AssertComRC(rc); } }
/** * Detach all USB devices currently attached to a VM. * * This is in an interface for SessionMachine::DetachAllUSBDevices(), which * is an internal worker used by Console::powerDown() from the VM process * at VM startup, and SessionMachine::uninit() at VM abend. * * This is, like #detachDeviceFromVM(), normally a two stage journey * where \a aDone indicates where we are. In addition we may be called * to clean up VMs that have abended, in which case there will be no * preparatory call. Filters will be applied to the devices in the final * call with the risk that we have to do some IPC when attaching them * to other VMs. * * @param aMachine The machine to detach devices from. * * @returns COM status code, perhaps with error info. * * @remarks Write locks the host object and may temporarily abandon * its locks to perform IPC. */ HRESULT USBProxyService::detachAllDevicesFromVM(SessionMachine *aMachine, bool aDone, bool aAbnormal) { // get a list of all running machines while we're outside the lock // (getOpenedMachines requests locks which are incompatible with the host object lock) SessionMachinesList llOpenedMachines; mHost->i_parent()->i_getOpenedMachines(llOpenedMachines); AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); /* * Make a copy of the device list (not the HostUSBDevice objects, just * the list) since we may end up performing IPC and temporarily have * to abandon locks when applying filters. */ HostUSBDeviceList ListCopy = mDevices; for (HostUSBDeviceList::iterator it = ListCopy.begin(); it != ListCopy.end(); ++it) { ComObjPtr<HostUSBDevice> pHostDevice = *it; AutoWriteLock devLock(pHostDevice COMMA_LOCKVAL_SRC_POS); if (pHostDevice->i_getMachine() == aMachine) { /* * Same procedure as in detachUSBDevice(). */ bool fRunFilters = false; HRESULT hrc = pHostDevice->i_onDetachFromVM(aMachine, aDone, &fRunFilters, aAbnormal); if ( SUCCEEDED(hrc) && fRunFilters) { Assert( aDone && pHostDevice->i_getUnistate() == kHostUSBDeviceState_HeldByProxy && pHostDevice->i_getMachine().isNull()); devLock.release(); alock.release(); HRESULT hrc2 = runAllFiltersOnDevice(pHostDevice, llOpenedMachines, aMachine); ComAssertComRC(hrc2); alock.acquire(); } } } return S_OK; }
/** * Notification from VM process about USB device detaching progress. * * This is in an interface for SessionMachine::DetachUSBDevice(), which is * an internal worker used by Console::DetachUSBDevice() from the VM process. * * @param aMachine The machine which is sending the notification. * @param aId The UUID of the USB device is concerns. * @param aDone \a false for the pre-action notification (necessary * for advancing the device state to avoid confusing * the guest). * \a true for the post-action notification. The device * will be subjected to all filters except those of * of \a Machine. * * @returns COM status code. * * @remarks When \a aDone is \a true this method may end up doing IPC to other * VMs when running filters. In these cases it will temporarily * abandon its locks. */ HRESULT USBProxyService::detachDeviceFromVM(SessionMachine *aMachine, IN_GUID aId, bool aDone) { LogFlowThisFunc(("aMachine=%p{%s} aId={%RTuuid} aDone=%RTbool\n", aMachine, aMachine->getName().c_str(), Guid(aId).raw(), aDone)); // get a list of all running machines while we're outside the lock // (getOpenedMachines requests locks which are incompatible with the lock of the machines list) SessionMachinesList llOpenedMachines; mHost->parent()->getOpenedMachines(llOpenedMachines); AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); ComObjPtr<HostUSBDevice> pHostDevice = findDeviceById(aId); ComAssertRet(!pHostDevice.isNull(), E_FAIL); AutoWriteLock devLock(pHostDevice COMMA_LOCKVAL_SRC_POS); /* * Work the state machine. */ LogFlowThisFunc(("id={%RTuuid} state=%s aDone=%RTbool name={%s}\n", pHostDevice->getId().raw(), pHostDevice->getStateName(), aDone, pHostDevice->getName().c_str())); bool fRunFilters = false; HRESULT hrc = pHostDevice->onDetachFromVM(aMachine, aDone, &fRunFilters); /* * Run filters if necessary. */ if ( SUCCEEDED(hrc) && fRunFilters) { Assert(aDone && pHostDevice->getUnistate() == kHostUSBDeviceState_HeldByProxy && pHostDevice->getMachine().isNull()); devLock.release(); alock.release(); HRESULT hrc2 = runAllFiltersOnDevice(pHostDevice, llOpenedMachines, aMachine); ComAssertComRC(hrc2); } return hrc; }