VirtualBox

Changeset 5627 in vbox


Ignore:
Timestamp:
Nov 5, 2007 12:45:05 PM (17 years ago)
Author:
vboxsync
Message:

Main: Improved Machine incapsulation by removing data(), userData(), hwData(), hdData() and other similar methods which also fixed the VBoxSVC crash when changing a setting of a valid VM through VBoxManage if there were one or more inaccessible VMs (#2466).

Location:
trunk/src/VBox/Main
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Main/HostImpl.cpp

    r5368 r5627  
    13801380
    13811381    if (device->state() == USBDeviceState_USBDeviceCaptured)
     1382    {
     1383        /* Machine::name() requires a read lock */
     1384        AutoReaderLock machLock (device->machine());
     1385
    13821386        return setError (E_INVALIDARG,
    13831387            tr ("USB device '%s' with UUID {%Vuuid} is already captured by the virtual "
    13841388                "machine '%ls'"),
    13851389            device->name().raw(), id.raw(),
    1386             aMachine->userData()->mName.raw());
     1390            device->machine()->name());
     1391    }
    13871392
    13881393    /* try to capture the device */
     
    21862191    AssertReturn (aDevice->isStatePending() == false, false);
    21872192
    2188     bool hasMatch = false;
    2189 
    2190     {
    2191         /* We're going to use aMachine which is not our child/parent, add a
    2192          * caller */
    2193         AutoCaller autoCaller (aMachine);
    2194         if (!autoCaller.isOk())
    2195         {
    2196             /* silently return, the machine might be not running any more */
    2197             return false;
    2198         }
    2199 
    2200         /* enter the machine's lock because we want to access its USB controller */
    2201         AutoReaderLock machineLock (aMachine);
    2202         hasMatch = aMachine->usbController()->hasMatchingFilter (aDevice);
    2203     }
     2193    bool hasMatch = aMachine->hasMatchingUSBFilter (aDevice);
    22042194
    22052195    if (hasMatch)
  • trunk/src/VBox/Main/MachineImpl.cpp

    r5559 r5627  
    25152515 *  The returned session machine is null if no direct session is currently open.
    25162516 *
    2517  *  @Note locks this object for reading.
     2517 *  @note locks this object for reading.
    25182518 */
    25192519ComObjPtr <SessionMachine> Machine::sessionMachine()
     
    25212521    ComObjPtr <SessionMachine> sm;
    25222522
    2523     AutoCaller autoCaller (this);
    2524     /* the machine may be inaccessible, so don't assert below */
    2525     if (FAILED (autoCaller.rc()))
     2523    AutoLimitedCaller autoCaller (this);
     2524    AssertComRCReturn (autoCaller.rc(), sm);
     2525
     2526    /* return null for inaccessible machines */
     2527    if (autoCaller.state() != Ready)
    25262528        return sm;
    25272529
     
    25332535
    25342536    return  sm;
     2537}
     2538
     2539/**
     2540 *  Saves the registry entry of this machine to the given configuration node.
     2541 *
     2542 *  @param aEntryNode Node to save the registry entry to.
     2543 *
     2544 *  @note locks this object for reading.
     2545 */
     2546HRESULT Machine::saveRegistryEntry (CFGNODE aEntryNode)
     2547{
     2548    AssertReturn (aEntryNode, E_FAIL);
     2549
     2550    AutoLimitedCaller autoCaller (this);
     2551    AssertComRCReturnRC (autoCaller.rc());
     2552
     2553    AutoReaderLock alock (this);
     2554
     2555    /* UUID */
     2556    CFGLDRSetUUID (aEntryNode, "uuid", mData->mUuid.raw());
     2557    /* settings file name (possibly, relative) */
     2558    CFGLDRSetBSTR (aEntryNode, "src", mData->mConfigFile);
     2559
     2560    return S_OK;
    25352561}
    25362562
     
    26382664bool Machine::isDVDImageUsed (const Guid &aId, ResourceUsage_T aUsage)
    26392665{
    2640     AutoCaller autoCaller (this);
    2641     AssertComRCReturn (autoCaller.rc() || autoCaller.state() == Limited, false);
     2666    AutoLimitedCaller autoCaller (this);
     2667    AssertComRCReturn (autoCaller.rc(), false);
    26422668
    26432669    /* answer 'not attached' if the VM is limited */
     
    26552681    /* first, check the current state */
    26562682    {
    2657         const ComObjPtr <DVDDrive> &dvd = m->dvdDrive();
     2683        const ComObjPtr <DVDDrive> &dvd = m->mDVDDrive;
    26582684        AssertReturn (!dvd.isNull(), false);
    26592685
     
    27152741{
    27162742    AutoCaller autoCaller (this);
    2717     AssertComRCReturn (autoCaller.rc() || autoCaller.state() == Limited, false);
     2743    AssertComRCReturn (autoCaller.rc(), false);
    27182744
    27192745    /* answer 'not attached' if the VM is limited */
     
    27312757    /* first, check the current state */
    27322758    {
    2733         const ComObjPtr <FloppyDrive> &floppy = m->floppyDrive();
     2759        const ComObjPtr <FloppyDrive> &floppy = m->mFloppyDrive;
    27342760        AssertReturn (!floppy.isNull(), false);
    27352761
     
    71677193
    71687194                        const HDData::HDAttachmentList &snapAtts =
    7169                             snap->data().mMachine->hdData()->mHDAttachments;
     7195                            snap->data().mMachine->mHDData->mHDAttachments;
    71707196
    71717197                        HDData::HDAttachmentList::const_iterator foundIt = snapAtts.end();
     
    80178043    Task (SessionMachine *m, Progress *p)
    80188044        : machine (m), progress (p)
    8019         , state (m->data()->mMachineState) // save the current machine state
     8045        , state (m->mData->mMachineState) // save the current machine state
    80208046        , subTask (false), settingsChanged (false)
    80218047    {}
     
    94759501
    94769502    AutoCaller autoCaller (this);
    9477     AssertComRCReturn (autoCaller.rc(), autoCaller.rc());
     9503    AssertComRCReturnRC (autoCaller.rc());
    94789504
    94799505    ComPtr <IInternalSessionControl> directControl;
     
    94889514
    94899515    return directControl->OnSharedFolderChange (FALSE /* aGlobal */);
     9516}
     9517
     9518/**
     9519 *  Returns @c true if this machine's USB controller reports it has a matching
     9520 *  filter for the given USB device and @c false otherwise.
     9521 *
     9522 *  @note Locks this object for reading.
     9523 */
     9524bool SessionMachine::hasMatchingUSBFilter (const ComObjPtr <HostUSBDevice> &aDevice)
     9525{
     9526    AutoCaller autoCaller (this);
     9527    /* silently return if not ready -- this method may be called after the
     9528     * direct machine session has been called */
     9529    if (!autoCaller.isOk())
     9530        return false;
     9531
     9532    AutoReaderLock alock (this);
     9533
     9534    return mUSBController->hasMatchingFilter (aDevice);
    94909535}
    94919536
  • trunk/src/VBox/Main/SnapshotImpl.cpp

    r5218 r5627  
    316316const Bstr &Snapshot::stateFilePath() const
    317317{
    318     return mData.mMachine->ssData()->mStateFilePath;
     318    return mData.mMachine->mSSData->mStateFilePath;
    319319}
    320320
     
    409409
    410410    AssertReturn (!mData.mMachine.isNull(), false);
    411     AssertReturn (!mData.mMachine->dvdDrive().isNull(), false);
    412 
    413     DVDDrive::Data *d = mData.mMachine->dvdDrive()->data().data();
     411    AssertReturn (!mData.mMachine->mDVDDrive.isNull(), false);
     412
     413    DVDDrive::Data *d = mData.mMachine->mDVDDrive->data().data();
    414414
    415415    if (d &&
     
    448448
    449449    AssertReturn (!mData.mMachine.isNull(), false);
    450     AssertReturn (!mData.mMachine->dvdDrive().isNull(), false);
    451 
    452     FloppyDrive::Data *d = mData.mMachine->floppyDrive()->data().data();
     450    AssertReturn (!mData.mMachine->mFloppyDrive.isNull(), false);
     451
     452    FloppyDrive::Data *d = mData.mMachine->mFloppyDrive->data().data();
    453453
    454454    if (d &&
     
    494494    AssertReturnVoid (isReady());
    495495
    496     Utf8Str path = mData.mMachine->ssData()->mStateFilePath;
     496    Utf8Str path = mData.mMachine->mSSData->mStateFilePath;
    497497    LogFlowThisFunc (("Snap[%ls].statePath={%s}\n", mData.mName.raw(), path.raw()));
    498498
     
    501501    {
    502502        path = Utf8StrFmt ("%s%s", aNewPath, path.raw() + strlen (aOldPath));
    503         mData.mMachine->ssData()->mStateFilePath = path;
     503        mData.mMachine->mSSData->mStateFilePath = path;
    504504
    505505        LogFlowThisFunc (("-> updated: {%s}\n", path.raw()));
  • trunk/src/VBox/Main/VirtualBoxImpl.cpp

    r5390 r5627  
    806806    }
    807807
    808     /*
    809      *  Machine::trySetRegistered() will commit and save machine settings, and
    810      *  will also call #registerMachine() on success.
    811      */
     808    AutoCaller machCaller (machine);
     809    ComAssertComRCRetRC (machCaller.rc());
     810
    812811    rc = registerMachine (machine);
    813812
    814813    /* fire an event */
    815814    if (SUCCEEDED (rc))
    816         onMachineRegistered (machine->data()->mUuid, TRUE);
     815        onMachineRegistered (machine->uuid(), TRUE);
    817816
    818817    return rc;
     
    864863         ++ it)
    865864    {
    866         AutoReaderLock machineLock (*it);
    867         if ((*it)->userData()->mName == aName)
    868             machine = *it;
     865        AutoLimitedCaller machCaller (*it);
     866        AssertComRC (machCaller.rc());
     867
     868        /* skip inaccessible machines */
     869        if (machCaller.state() == Machine::Ready)
     870        {
     871            AutoReaderLock machLock (*it);
     872            if ((*it)->name() == aName)
     873                machine = *it;
     874        }
    869875    }
    870876
     
    27752781        {
    27762782            ComObjPtr <Machine> m = *mit;
    2777             if (m->isDVDImageUsed (aId, aUsage))
     2783
     2784            AutoLimitedCaller machCaller (m);
     2785            AssertComRC (machCaller.rc());
     2786
     2787            /* ignore inaccessible machines */
     2788            if (machCaller.state() == Machine::Ready)
    27782789            {
    2779                 /* if not interested in the list, return shortly */
    2780                 if (aMachineIDs == NULL)
    2781                     return true;
    2782 
    2783                 idSet.insert (m->data()->mUuid);
     2790                if (m->isDVDImageUsed (aId, aUsage))
     2791                {
     2792                    /* if not interested in the list, return shortly */
     2793                    if (aMachineIDs == NULL)
     2794                        return true;
     2795
     2796                    idSet.insert (m->uuid());
     2797                }
    27842798            }
    27852799        }
     
    28542868        {
    28552869            ComObjPtr <Machine> m = *mit;
    2856             if (m->isFloppyImageUsed (aId, aUsage))
     2870
     2871            AutoLimitedCaller machCaller (m);
     2872            AssertComRC (machCaller.rc());
     2873
     2874            /* ignore inaccessible machines */
     2875            if (machCaller.state() == Machine::Ready)
    28572876            {
    2858                 /* if not interested in the list, return shortly */
    2859                 if (aMachineIDs == NULL)
    2860                     return true;
    2861 
    2862                 idSet.insert (m->data()->mUuid);
     2877                if (m->isFloppyImageUsed (aId, aUsage))
     2878                {
     2879                    /* if not interested in the list, return shortly */
     2880                    if (aMachineIDs == NULL)
     2881                        return true;
     2882
     2883                    idSet.insert (m->uuid());
     2884                }
    28632885            }
    28642886        }
     
    29612983             ++ it)
    29622984        {
    2963             /* mUuid is constant, no need to lock */
    2964             found = (*it)->data()->mUuid == aId;
     2985            AutoLimitedCaller machCaller (*it);
     2986            AssertComRC (machCaller.rc());
     2987
     2988            found = (*it)->uuid() == aId;
    29652989            if (found && aMachine)
    29662990                *aMachine = *it;
     
    36533677                /// @todo (dmik) move this part to Machine for better incapsulation
    36543678
    3655                 ComObjPtr <Machine> m = *it;
    3656                 AutoReaderLock machineLock (m);
    3657 
    3658                 AssertMsg (m->data(), ("Machine data must not be NULL"));
    3659                 CFGNODE entryNode;
     3679                CFGNODE entryNode = NULL;
    36603680                CFGLDRAppendChildNode (registryNode, Entry, &entryNode);
    3661                 /* UUID + curly brackets */
    3662                 CFGLDRSetUUID (entryNode, "uuid", unconst (m->data()->mUuid).ptr());
    3663                 /* source */
    3664                 CFGLDRSetBSTR (entryNode, "src", m->data()->mConfigFile);
    3665                 /* done */
     3681                rc = (*it)->saveRegistryEntry (entryNode);
    36663682                CFGLDRReleaseNode (entryNode);
     3683                CheckComRCBreakRC (rc);
    36673684            }
    36683685
     
    36703687        }
    36713688        while (0);
    3672         if (FAILED (rc))
    3673             break;
     3689        CheckComRCBreakRC (rc);
    36743690
    36753691        /* disk images */
     
    36913707                rc = saveHardDisks (imagesNode);
    36923708                CFGLDRReleaseNode (imagesNode);
    3693                 if (FAILED (rc))
    3694                     break;
     3709                CheckComRCBreakRC (rc);
    36953710            }
    36963711
     
    37403755        }
    37413756        while (0);
    3742         if (FAILED (rc))
    3743             break;
     3757        CheckComRCBreakRC (rc);
    37443758
    37453759        do
     
    37473761            /* host data (USB filters) */
    37483762            rc = mData.mHost->saveSettings (global);
    3749             if (FAILED (rc))
    3750                 break;
     3763            CheckComRCBreakRC (rc);
    37513764
    37523765            rc = mData.mSystemProperties->saveSettings (global);
    3753             if (FAILED (rc))
    3754                 break;
     3766            CheckComRCBreakRC (rc);
    37553767        }
    37563768        while (0);
     
    38663878 *  saves global settings.
    38673879 *
     3880 *  @note The caller must have added itself as a caller of the @a aMachine
     3881 *  object if calls this method not on VirtualBox startup.
     3882 *
    38683883 *  @param aMachine     machine to register
    38693884 *
     
    38793894    AutoLock alock (this);
    38803895
    3881     ComAssertRet (findMachine (aMachine->data()->mUuid,
     3896    ComAssertRet (findMachine (aMachine->uuid(),
    38823897                  false /* aDoSetError  */, NULL) == E_INVALIDARG,
    38833898                  E_FAIL);
  • trunk/src/VBox/Main/include/MachineImpl.h

    r5390 r5627  
    6060class Snapshot;
    6161class SharedFolder;
     62class HostUSBDevice;
    6263
    6364class SessionMachine;
     
    510511    ComObjPtr <SessionMachine> sessionMachine();
    511512
    512     // Note: the below methods are intended to be called only after adding
    513     // a caller to the Machine instance and, when necessary, from under
    514     // the Machine lock in appropriate mode
    515 
    516     /// @todo (dmik) revise code using these methods: improving incapsulation
    517     //  should make them not necessary
    518 
    519     const ComObjPtr <VirtualBox, ComWeakRef> &virtualBox() { return mParent; }
    520 
    521     const Shareable <Data> &data() const { return mData; }
    522     const Backupable <UserData> &userData() const { return mUserData; }
    523     const Backupable <HDData> &hdData() const { return mHDData; }
    524 
    525     const Shareable <SSData> &ssData() const { return mSSData; }
    526 
    527     const ComObjPtr <DVDDrive> &dvdDrive() { return mDVDDrive; }
    528     const ComObjPtr <FloppyDrive> &floppyDrive() { return mFloppyDrive; }
    529     const ComObjPtr <USBController> &usbController() { return mUSBController; }
    530 
     513    /**
     514     *  Returns the VirtualBox object this machine belongs to.
     515     *
     516     *  @note This method doesn't check this object's readiness as it is
     517     *  intended to be used only by Machine children where it is guaranteed
     518     *  that this object still exists in memory.
     519     */
     520    const ComObjPtr <VirtualBox, ComWeakRef> &virtualBox() const { return mParent; }
     521
     522    /**
     523     *  Returns this machine's name.
     524     *
     525     *  @note This method doesn't check this object's readiness as it is
     526     *  intended to be used only after adding a caller to this object (that
     527     *  guarantees that the object is ready or at least limited).
     528     */
     529    const Guid &uuid() const { return mData->mUuid; }
     530
     531    /**
     532     *  Returns this machine's name.
     533     *
     534     *  @note This method doesn't lock this object or check its readiness as
     535     *  it is intended to be used only after adding a caller to this object
     536     *  (that guarantees that the object is ready) and locking it for reading.
     537     */
     538    const Bstr &name() const { return mUserData->mName; }
     539
     540    // callback handlers
    531541    virtual HRESULT onDVDDriveChange() { return S_OK; }
    532542    virtual HRESULT onFloppyDriveChange() { return S_OK; }
     
    537547    virtual HRESULT onUSBControllerChange() { return S_OK; }
    538548    virtual HRESULT onSharedFolderChange() { return S_OK; }
     549
     550    HRESULT saveRegistryEntry (CFGNODE aEntryNode);
    539551
    540552    int calculateFullPath (const char *aPath, Utf8Str &aResult);
     
    794806    HRESULT onSharedFolderChange();
    795807
     808    bool hasMatchingUSBFilter (const ComObjPtr <HostUSBDevice> &aDevice);
     809
    796810private:
    797811
     
    909923
    910924    Guid mSnapshotId;
     925
     926    friend class Snapshot;
    911927};
    912928
Note: See TracChangeset for help on using the changeset viewer.

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