VirtualBox

source: vbox/trunk/src/VBox/Main/MachineImpl.cpp@ 815

Last change on this file since 815 was 815, checked in by vboxsync, 18 years ago

Main:

  • Return E_NOTIMPL for global USB filters and all other stuff when no VBOX_WITH_USB is defined (as in OSE).
  • Moved the USB Proxy Service check to Host to make it reusable both for global-related USB methods and for VM-related methods.

FE/Qt:

  • Don't show Global USB UI when USB is not available.
  • Show the "USB Proxy Service is unavailable" message box when opening both global and VM settings.
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 303.8 KB
Line 
1/** @file
2 *
3 * VirtualBox COM class implementation
4 */
5
6/*
7 * Copyright (C) 2006 InnoTek Systemberatung GmbH
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 as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * If you received this file as part of a commercial VirtualBox
18 * distribution, then only the terms of your commercial VirtualBox
19 * license agreement apply instead of the previous paragraph.
20 */
21
22#if defined(__WIN__)
23#elif defined(__LINUX__)
24#endif
25
26#ifdef VBOX_WITH_SYS_V_IPC_SESSION_WATCHER
27# include <errno.h>
28# include <sys/types.h>
29# include <sys/stat.h>
30# include <sys/ipc.h>
31# include <sys/sem.h>
32#endif
33
34#include "VirtualBoxImpl.h"
35#include "MachineImpl.h"
36#include "HardDiskImpl.h"
37#include "HostDVDDriveImpl.h"
38#include "HostFloppyDriveImpl.h"
39#include "ProgressImpl.h"
40#include "HardDiskAttachmentImpl.h"
41#include "USBControllerImpl.h"
42#include "HostImpl.h"
43#include "SystemPropertiesImpl.h"
44#include "SharedFolderImpl.h"
45#include "GuestOSTypeImpl.h"
46#include "VirtualBoxErrorInfoImpl.h"
47
48#include "USBProxyService.h"
49
50#include "Logging.h"
51
52#include <stdio.h>
53#include <stdlib.h>
54#include <VBox/err.h>
55#include <VBox/cfgldr.h>
56#include <iprt/path.h>
57#include <iprt/dir.h>
58#include <iprt/asm.h>
59#include <iprt/process.h>
60#include <VBox/param.h>
61
62#include <algorithm>
63
64#if defined(__WIN__) || defined(__OS2__)
65#define HOSTSUFF_EXE ".exe"
66#else /* !__WIN__ */
67#define HOSTSUFF_EXE ""
68#endif /* !__WIN__ */
69
70// defines / prototypes
71/////////////////////////////////////////////////////////////////////////////
72
73/**
74 * Local mutability check macro for Machine implementation only.
75 */
76#define CHECK_SETTER() \
77 if (!isMutable()) \
78 return setError (E_ACCESSDENIED, tr ("The machine is not mutable"));
79
80// globals
81/////////////////////////////////////////////////////////////////////////////
82
83/**
84 * @note The template is NOT completely valid according to VBOX_XML_SCHEMA
85 * (when loading a newly created settings file, validation will be turned off)
86 */
87static const char DefaultMachineConfig[] =
88{
89 "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" RTFILE_LINEFEED
90 "<!-- InnoTek VirtualBox Machine Configuration -->" RTFILE_LINEFEED
91 "<VirtualBox xmlns=\"" VBOX_XML_NAMESPACE "\" "
92 "version=\"" VBOX_XML_VERSION "-" VBOX_XML_PLATFORM "\">" RTFILE_LINEFEED
93 "</VirtualBox>" RTFILE_LINEFEED
94};
95
96/**
97 * Progress callback handler for lengthy operations
98 * (corresponds to the FNRTPROGRESS typedef).
99 *
100 * @param uPercentage Completetion precentage (0-100).
101 * @param pvUser Pointer to the Progress instance.
102 */
103static DECLCALLBACK(int) progressCallback (unsigned uPercentage, void *pvUser)
104{
105 Progress *progress = static_cast <Progress *> (pvUser);
106
107 /* update the progress object */
108 if (progress)
109 progress->notifyProgress (uPercentage);
110
111 return VINF_SUCCESS;
112}
113
114/////////////////////////////////////////////////////////////////////////////
115// Machine::Data structure
116/////////////////////////////////////////////////////////////////////////////
117
118Machine::Data::Data()
119{
120 mRegistered = FALSE;
121 /* mUuid is initialized in Machine::init() */
122
123 mMachineState = MachineState_PoweredOff;
124 RTTIMESPEC time;
125 mLastStateChange = RTTimeSpecGetMilli (RTTimeNow (&time));
126 mCurrentStateModified = TRUE;
127 mHandleCfgFile = NIL_RTFILE;
128
129 mSession.mPid = NIL_RTPROCESS;
130 mSession.mState = SessionState_SessionClosed;
131}
132
133Machine::Data::~Data()
134{
135}
136
137/////////////////////////////////////////////////////////////////////////////
138// Machine::UserData structure
139/////////////////////////////////////////////////////////////////////////////
140
141Machine::UserData::UserData()
142{
143 /* default values for a newly created machine */
144
145 mNameSync = TRUE;
146
147 /* mName, mOSType, mSnapshotFolder, mSnapshotFolderFull are initialized in
148 * Machine::init() */
149}
150
151Machine::UserData::~UserData()
152{
153}
154
155/////////////////////////////////////////////////////////////////////////////
156// Machine::HWData structure
157/////////////////////////////////////////////////////////////////////////////
158
159Machine::HWData::HWData()
160{
161 /* default values for a newly created machine */
162 mMemorySize = 128;
163 mVRAMSize = 8;
164 mHWVirtExEnabled = TriStateBool_False;
165
166 /* default boot order: floppy - DVD - HDD */
167 mBootOrder [0] = DeviceType_FloppyDevice;
168 mBootOrder [1] = DeviceType_DVDDevice;
169 mBootOrder [2] = DeviceType_HardDiskDevice;
170 for (size_t i = 3; i < ELEMENTS (mBootOrder); i++)
171 mBootOrder [i] = DeviceType_NoDevice;
172
173 mClipboardMode = ClipboardMode_ClipDisabled;
174}
175
176Machine::HWData::~HWData()
177{
178}
179
180bool Machine::HWData::operator== (const HWData &that) const
181{
182 if (this == &that)
183 return true;
184
185 if (mMemorySize != that.mMemorySize ||
186 mVRAMSize != that.mVRAMSize ||
187 mHWVirtExEnabled != that.mHWVirtExEnabled ||
188 mClipboardMode != that.mClipboardMode)
189 return false;
190
191 for (size_t i = 0; i < ELEMENTS (mBootOrder); ++ i)
192 if (mBootOrder [i] != that.mBootOrder [i])
193 return false;
194
195 if (mSharedFolders.size() != that.mSharedFolders.size())
196 return false;
197
198 if (mSharedFolders.size() == 0)
199 return true;
200
201 /* Make copies to speed up comparison */
202 SharedFolderList folders = mSharedFolders;
203 SharedFolderList thatFolders = that.mSharedFolders;
204
205 SharedFolderList::iterator it = folders.begin();
206 while (it != folders.end())
207 {
208 bool found = false;
209 SharedFolderList::iterator thatIt = thatFolders.begin();
210 while (thatIt != thatFolders.end())
211 {
212 if ((*it)->name() == (*thatIt)->name() &&
213 RTPathCompare (Utf8Str ((*it)->hostPath()),
214 Utf8Str ((*thatIt)->hostPath())) == 0)
215 {
216 thatFolders.erase (thatIt);
217 found = true;
218 break;
219 }
220 else
221 ++ thatIt;
222 }
223 if (found)
224 it = folders.erase (it);
225 else
226 return false;
227 }
228
229 Assert (folders.size() == 0 && thatFolders.size() == 0);
230
231 return true;
232}
233
234/////////////////////////////////////////////////////////////////////////////
235// Machine::HDData structure
236/////////////////////////////////////////////////////////////////////////////
237
238Machine::HDData::HDData()
239{
240 /* default values for a newly created machine */
241 mHDAttachmentsChanged = false;
242}
243
244Machine::HDData::~HDData()
245{
246}
247
248bool Machine::HDData::operator== (const HDData &that) const
249{
250 if (this == &that)
251 return true;
252
253 if (mHDAttachments.size() != that.mHDAttachments.size())
254 return false;
255
256 if (mHDAttachments.size() == 0)
257 return true;
258
259 /* Make copies to speed up comparison */
260 HDAttachmentList atts = mHDAttachments;
261 HDAttachmentList thatAtts = that.mHDAttachments;
262
263 HDAttachmentList::iterator it = atts.begin();
264 while (it != atts.end())
265 {
266 bool found = false;
267 HDAttachmentList::iterator thatIt = thatAtts.begin();
268 while (thatIt != thatAtts.end())
269 {
270 if ((*it)->deviceNumber() == (*thatIt)->deviceNumber() &&
271 (*it)->controller() == (*thatIt)->controller() &&
272 (*it)->hardDisk().equalsTo ((*thatIt)->hardDisk()))
273 {
274 thatAtts.erase (thatIt);
275 found = true;
276 break;
277 }
278 else
279 ++ thatIt;
280 }
281 if (found)
282 it = atts.erase (it);
283 else
284 return false;
285 }
286
287 Assert (atts.size() == 0 && thatAtts.size() == 0);
288
289 return true;
290}
291
292/////////////////////////////////////////////////////////////////////////////
293// Machine class
294/////////////////////////////////////////////////////////////////////////////
295
296// constructor / destructor
297/////////////////////////////////////////////////////////////////////////////
298
299Machine::Machine() : mType (IsMachine) {}
300
301Machine::~Machine() {}
302
303HRESULT Machine::FinalConstruct()
304{
305 LogFlowThisFunc (("\n"));
306 return S_OK;
307}
308
309void Machine::FinalRelease()
310{
311 LogFlowThisFunc (("\n"));
312 uninit();
313}
314
315/**
316 * Initializes the instance.
317 *
318 * @param aParent Associated parent object
319 * @param aConfigFile Local file system path to the VM settings file (can
320 * be relative to the VirtualBox config directory).
321 * @param aMode Init_New, Init_Existing or Init_Registered
322 * @param aName name for the machine when aMode is Init_New
323 * (ignored otherwise)
324 * @param aNameSync |TRUE| to authomatically sync settings dir and file
325 * name with the machine name. |FALSE| is used for legacy
326 * machines where the file name is specified by the
327 * user and should never change. Used only in Init_New
328 * mode (ignored otherwise).
329 * @param aId UUID of the machine (used only for consistency
330 * check when aMode is Init_Registered; must match UUID
331 * stored in the settings file).
332 *
333 * @return Success indicator. if not S_OK, the machine object is invalid
334 */
335HRESULT Machine::init (VirtualBox *aParent, const BSTR aConfigFile,
336 InitMode aMode, const BSTR aName /* = NULL */,
337 BOOL aNameSync /* = TRUE */,
338 const Guid *aId /* = NULL */)
339{
340 LogFlowThisFuncEnter();
341 LogFlowThisFunc (("aConfigFile='%ls', aMode=%d\n", aConfigFile, aMode));
342
343 AssertReturn (aParent, E_INVALIDARG);
344 AssertReturn (aConfigFile, E_INVALIDARG);
345 AssertReturn (aMode != Init_New || (aName != NULL && *aName != '\0'),
346 E_INVALIDARG);
347 AssertReturn (aMode != Init_Registered || aId != NULL, E_FAIL);
348
349 /* Enclose the state transition NotReady->InInit->Ready */
350 AutoInitSpan autoInitSpan (this);
351 AssertReturn (autoInitSpan.isOk(), E_UNEXPECTED);
352
353 HRESULT rc = S_OK;
354
355 /* share the parent weakly */
356 unconst (mParent) = aParent;
357
358 /* register with parent early, since uninit() will unconditionally
359 * unregister on failure */
360 mParent->addDependentChild (this);
361
362 /* create machine data structures */
363 mData.allocate();
364 mSSData.allocate();
365
366 mUserData.allocate();
367 mHWData.allocate();
368 mHDData.allocate();
369
370 char configFileFull [RTPATH_MAX] = {0};
371
372 /* memorize the config file name (as provided) */
373 mData->mConfigFile = aConfigFile;
374
375 /* get the full file name */
376 int vrc = RTPathAbsEx (mParent->homeDir(), Utf8Str (aConfigFile),
377 configFileFull, sizeof (configFileFull));
378 if (VBOX_FAILURE (vrc))
379 return setError (E_FAIL,
380 tr ("Invalid settings file name: '%ls' (%Vrc)"),
381 aConfigFile, vrc);
382 mData->mConfigFileFull = configFileFull;
383
384 mData->mAccessible = TRUE;
385
386 if (aMode != Init_New)
387 {
388 /* lock the settings file */
389 rc = lockConfig();
390
391 if (aMode == Init_Registered && FAILED (rc))
392 {
393 /* If the machine is registered, then, instead of returning a
394 * failure, we mark it as inaccessible and set the result to
395 * success to give it a try later */
396 mData->mAccessible = FALSE;
397 /* fetch the current error info */
398 mData->mAccessError = com::ErrorInfo();
399 LogWarning (("Machine {%Vuuid} is inaccessible! [%ls]\n",
400 mData->mUuid.raw(),
401 mData->mAccessError.getText().raw()));
402 rc = S_OK;
403 }
404 }
405 else
406 {
407 /* check for the file existence */
408 RTFILE f = NIL_RTFILE;
409 int vrc = RTFileOpen (&f, configFileFull, RTFILE_O_READ);
410 if (VBOX_SUCCESS (vrc) || vrc == VERR_SHARING_VIOLATION)
411 {
412 rc = setError (E_FAIL,
413 tr ("Settings file '%s' already exists"), configFileFull);
414 if (VBOX_SUCCESS (vrc))
415 RTFileClose (f);
416 }
417 else
418 {
419 if (vrc != VERR_FILE_NOT_FOUND && vrc != VERR_PATH_NOT_FOUND)
420 rc = setError (E_FAIL,
421 tr ("Invalid settings file name: '%ls' (%Vrc)"),
422 mData->mConfigFileFull.raw(), vrc);
423 }
424 }
425
426 CheckComRCReturnRC (rc);
427
428 /* initialize mOSType */
429 mUserData->mOSType = mParent->getUnknownOSType();
430
431 /* create associated BIOS settings object */
432 unconst (mBIOSSettings).createObject();
433 mBIOSSettings->init(this);
434
435#ifdef VBOX_VRDP
436 /* create an associated VRDPServer object (default is disabled) */
437 unconst (mVRDPServer).createObject();
438 mVRDPServer->init(this);
439#endif
440
441 /* create an associated DVD drive object */
442 unconst (mDVDDrive).createObject();
443 mDVDDrive->init (this);
444
445 /* create an associated floppy drive object */
446 unconst (mFloppyDrive).createObject();
447 mFloppyDrive->init (this);
448
449 /* create the audio adapter object (always present, default is disabled) */
450 unconst (mAudioAdapter).createObject();
451 mAudioAdapter->init(this);
452
453 /* create the USB controller object (always present, default is disabled) */
454 unconst (mUSBController).createObject();
455 mUSBController->init(this);
456
457 /* create associated network adapter objects */
458 for (ULONG slot = 0; slot < ELEMENTS (mNetworkAdapters); slot ++)
459 {
460 unconst (mNetworkAdapters [slot]).createObject();
461 mNetworkAdapters [slot]->init (this, slot);
462 }
463
464 if (aMode == Init_Registered)
465 {
466 /* store the supplied UUID (will be used to check for UUID consistency
467 * in loadSettings() */
468 unconst (mData->mUuid) = *aId;
469 /* try to load settings only if the settings file is accessible */
470 if (mData->mAccessible)
471 rc = registeredInit();
472 }
473 else
474 {
475 if (aMode != Init_New)
476 {
477 rc = loadSettings (false /* aRegistered */);
478 }
479 else
480 {
481 /* create the machine UUID */
482 unconst (mData->mUuid).create();
483
484 /* initialize the default snapshots folder */
485 rc = COMSETTER(SnapshotFolder) (NULL);
486 AssertComRC (rc);
487
488 /* memorize the provided new machine's name */
489 mUserData->mName = aName;
490 mUserData->mNameSync = aNameSync;
491 }
492
493 /* commit all changes made during the initialization */
494 if (SUCCEEDED (rc))
495 commit();
496 }
497
498 /* Confirm a successful initialization when it's the case */
499 if (SUCCEEDED (rc))
500 {
501 if (mData->mAccessible)
502 autoInitSpan.setSucceeded();
503 else
504 autoInitSpan.setLimited();
505 }
506
507 LogFlowThisFunc (("mName='%ls', mRegistered=%RTbool, mAccessible=%RTbool "
508 "rc=%08X\n",
509 mUserData->mName.raw(), mData->mRegistered,
510 mData->mAccessible, rc));
511
512 LogFlowThisFuncLeave();
513
514 return rc;
515}
516
517/**
518 * Initializes the registered machine by loading the settings file.
519 * This method is separated from #init() in order to make it possible to
520 * retry the operation after VirtualBox startup instead of refusing to
521 * startup the whole VirtualBox server in case if the settings file of some
522 * registered VM is invalid or inaccessible.
523 *
524 * @note Must be always called from this object's write lock
525 * (unless called from #init() that doesn't need any locking).
526 * @note Locks the mUSBController method for writing.
527 * @note Subclasses must not call this method.
528 */
529HRESULT Machine::registeredInit()
530{
531 AssertReturn (mType == IsMachine, E_FAIL);
532 AssertReturn (!mData->mUuid.isEmpty(), E_FAIL);
533
534 HRESULT rc = S_OK;
535
536 if (!mData->mAccessible)
537 rc = lockConfig();
538
539 /* Temporarily reset the registered flag in order to let setters potentially
540 * called from loadSettings() succeed (isMutable() used in all setters
541 * will return FALSE for a Machine instance if mRegistered is TRUE). */
542 mData->mRegistered = FALSE;
543
544 if (SUCCEEDED (rc))
545 {
546 rc = loadSettings (true /* aRegistered */);
547
548 if (FAILED (rc))
549 unlockConfig();
550 }
551
552 if (SUCCEEDED (rc))
553 {
554 mData->mAccessible = TRUE;
555
556 /* commit all changes made during loading the settings file */
557 commit();
558
559 /* VirtualBox will not call trySetRegistered(), so
560 * inform the USB proxy about all attached USB filters */
561 mUSBController->onMachineRegistered (TRUE);
562 }
563 else
564 {
565 /* If the machine is registered, then, instead of returning a
566 * failure, we mark it as inaccessible and set the result to
567 * success to give it a try later */
568 mData->mAccessible = FALSE;
569 /* fetch the current error info */
570 mData->mAccessError = com::ErrorInfo();
571 LogWarning (("Machine {%Vuuid} is inaccessible! [%ls]\n",
572 mData->mUuid.raw(),
573 mData->mAccessError.getText().raw()));
574
575 /* rollback all changes */
576 rollback (false /* aNotify */);
577
578 rc = S_OK;
579 }
580
581 /* Restore the registered flag (even on failure) */
582 mData->mRegistered = TRUE;
583
584 return rc;
585}
586
587/**
588 * Uninitializes the instance.
589 * Called either from FinalRelease() or by the parent when it gets destroyed.
590 *
591 * @note The caller of this method must make sure that this object
592 * a) doesn't have active callers on the current thread and b) is not locked
593 * by the current thread; otherwise uninit() will hang either a) due to
594 * AutoUninitSpan waiting for a number of calls to drop to zero or b) due to
595 * a dead-lock caused by this thread waiting for all callers on the other
596 * threads are are done but preventing them from doing so by holding a lock.
597 */
598void Machine::uninit()
599{
600 LogFlowThisFuncEnter();
601
602 Assert (!isLockedOnCurrentThread());
603
604 /* Enclose the state transition Ready->InUninit->NotReady */
605 AutoUninitSpan autoUninitSpan (this);
606 if (autoUninitSpan.uninitDone())
607 return;
608
609 Assert (mType == IsMachine);
610 Assert (!!mData && !!mUserData && !!mHWData && !!mHDData && !!mSSData);
611
612 LogFlowThisFunc (("initFailed()=%d\n", autoUninitSpan.initFailed()));
613 LogFlowThisFunc (("mRegistered=%d\n", mData->mRegistered));
614
615 /*
616 * Enter this object's lock because there may be a SessionMachine instance
617 * somewhere around, that shares our data and lock but doesn't use our
618 * addCaller()/removeCaller(), and it may be also accessing the same
619 * data members. mParent lock is necessary as well because of
620 * SessionMachine::uninit(), etc.
621 */
622 AutoMultiLock <2> alock (mParent->wlock(), this->wlock());
623
624 if (!mData->mSession.mMachine.isNull())
625 {
626 /*
627 * Theoretically, this can only happen if the VirtualBox server has
628 * been terminated while there were clients running that owned open
629 * direct sessions. Since in this case we are definitely called by
630 * VirtualBox::uninit(), we may be sure that SessionMachine::uninit()
631 * won't happen on the client watcher thread (because it does
632 * VirtualBox::addCaller() for the duration of the
633 * SessionMachine::checkForDeath() call, so that VirtualBox::uninit()
634 * cannot happen until the VirtualBox caller is released). This is
635 * important, because SessionMachine::uninit() cannot correctly operate
636 * after we return from this method (it expects the Machine instance
637 * is still valid). We'll call it ourselves below.
638 */
639 LogWarningThisFunc (("Session machine is not NULL (%p), "
640 "the direct session is still open!\n",
641 (SessionMachine *) mData->mSession.mMachine));
642
643 if (mData->mMachineState >= MachineState_Running)
644 {
645 LogWarningThisFunc (("Setting state to Aborted!\n"));
646 /* set machine state using SessionMachine reimplementation */
647 static_cast <Machine *> (mData->mSession.mMachine)
648 ->setMachineState (MachineState_Aborted);
649 }
650
651 /*
652 * Uninitialize SessionMachine using public uninit() to indicate
653 * an unexpected uninitialization.
654 */
655 mData->mSession.mMachine->uninit();
656 /* SessionMachine::uninit() must set mSession.mMachine to null */
657 Assert (mData->mSession.mMachine.isNull());
658 }
659
660 /* the lock is no more necessary (SessionMachine is uninitialized) */
661 alock.leave();
662
663 /* make sure the configuration is unlocked */
664 unlockConfig();
665
666 if (isModified())
667 {
668 LogWarningThisFunc (("Discarding unsaved settings changes!\n"));
669 rollback (false /* aNotify */);
670 }
671
672 uninitDataAndChildObjects();
673
674 mParent->removeDependentChild (this);
675
676 LogFlowThisFuncLeave();
677}
678
679// IMachine properties
680/////////////////////////////////////////////////////////////////////////////
681
682STDMETHODIMP Machine::COMGETTER(Parent) (IVirtualBox **aParent)
683{
684 if (!aParent)
685 return E_POINTER;
686
687 AutoLimitedCaller autoCaller (this);
688 CheckComRCReturnRC (autoCaller.rc());
689
690 /* mParent is constant during life time, no need to lock */
691 mParent.queryInterfaceTo (aParent);
692
693 return S_OK;
694}
695
696STDMETHODIMP Machine::COMGETTER(Accessible) (BOOL *aAccessible)
697{
698 if (!aAccessible)
699 return E_POINTER;
700
701 AutoLimitedCaller autoCaller (this);
702 CheckComRCReturnRC (autoCaller.rc());
703
704 AutoLock alock (this);
705
706 HRESULT rc = S_OK;
707
708 if (!mData->mAccessible)
709 {
710 /* try to initialize the VM once more if not accessible */
711
712 AutoReadySpan autoReadySpan (this);
713 AssertReturn (autoReadySpan.isOk(), E_FAIL);
714
715 rc = registeredInit();
716
717 if (mData->mAccessible)
718 autoReadySpan.setSucceeded();
719 }
720
721 if (SUCCEEDED (rc))
722 *aAccessible = mData->mAccessible;
723
724 return rc;
725}
726
727STDMETHODIMP Machine::COMGETTER(AccessError) (IVirtualBoxErrorInfo **aAccessError)
728{
729 if (!aAccessError)
730 return E_POINTER;
731
732 AutoLimitedCaller autoCaller (this);
733 CheckComRCReturnRC (autoCaller.rc());
734
735 AutoReaderLock alock (this);
736
737 if (mData->mAccessible || !mData->mAccessError.isBasicAvailable())
738 {
739 /* return shortly */
740 aAccessError = NULL;
741 return S_OK;
742 }
743
744 HRESULT rc = S_OK;
745
746 ComObjPtr <VirtualBoxErrorInfo> errorInfo;
747 rc = errorInfo.createObject();
748 if (SUCCEEDED (rc))
749 {
750 errorInfo->init (mData->mAccessError.getResultCode(),
751 mData->mAccessError.getInterfaceID(),
752 mData->mAccessError.getComponent(),
753 mData->mAccessError.getText());
754 rc = errorInfo.queryInterfaceTo (aAccessError);
755 }
756
757 return rc;
758}
759
760STDMETHODIMP Machine::COMGETTER(Name) (BSTR *aName)
761{
762 if (!aName)
763 return E_POINTER;
764
765 AutoCaller autoCaller (this);
766 CheckComRCReturnRC (autoCaller.rc());
767
768 AutoReaderLock alock (this);
769
770 mUserData->mName.cloneTo (aName);
771
772 return S_OK;
773}
774
775STDMETHODIMP Machine::COMSETTER(Name) (INPTR BSTR aName)
776{
777 if (!aName)
778 return E_INVALIDARG;
779
780 if (!*aName)
781 return setError (E_INVALIDARG,
782 tr ("Machine name cannot be empty"));
783
784 AutoCaller autoCaller (this);
785 CheckComRCReturnRC (autoCaller.rc());
786
787 AutoLock alock (this);
788
789 CHECK_SETTER();
790
791 mUserData.backup();
792 mUserData->mName = aName;
793
794 return S_OK;
795}
796
797STDMETHODIMP Machine::COMGETTER(Id) (GUIDPARAMOUT aId)
798{
799 if (!aId)
800 return E_POINTER;
801
802 AutoLimitedCaller autoCaller (this);
803 CheckComRCReturnRC (autoCaller.rc());
804
805 AutoReaderLock alock (this);
806
807 mData->mUuid.cloneTo (aId);
808
809 return S_OK;
810}
811
812STDMETHODIMP Machine::COMGETTER(OSType) (IGuestOSType **aOSType)
813{
814 if (!aOSType)
815 return E_POINTER;
816
817 AutoCaller autoCaller (this);
818 CheckComRCReturnRC (autoCaller.rc());
819
820 AutoReaderLock alock (this);
821
822 mUserData->mOSType.queryInterfaceTo (aOSType);
823
824 return S_OK;
825}
826
827STDMETHODIMP Machine::COMSETTER(OSType) (IGuestOSType *aOSType)
828{
829 if (!aOSType)
830 return E_INVALIDARG;
831
832 AutoCaller autoCaller (this);
833 CheckComRCReturnRC (autoCaller.rc());
834
835 AutoLock alock (this);
836
837 CHECK_SETTER();
838
839 mUserData.backup();
840 mUserData->mOSType = aOSType;
841
842 return S_OK;
843}
844
845STDMETHODIMP Machine::COMGETTER(MemorySize) (ULONG *memorySize)
846{
847 if (!memorySize)
848 return E_POINTER;
849
850 AutoCaller autoCaller (this);
851 CheckComRCReturnRC (autoCaller.rc());
852
853 AutoReaderLock alock (this);
854
855 *memorySize = mHWData->mMemorySize;
856
857 return S_OK;
858}
859
860STDMETHODIMP Machine::COMSETTER(MemorySize) (ULONG memorySize)
861{
862 /* check RAM limits */
863 if (memorySize < SchemaDefs::MinGuestRAM ||
864 memorySize > SchemaDefs::MaxGuestRAM)
865 return setError (E_INVALIDARG,
866 tr ("Invalid RAM size: %lu MB (must be in range [%lu, %lu] MB)"),
867 memorySize, SchemaDefs::MinGuestRAM, SchemaDefs::MaxGuestRAM);
868
869 AutoCaller autoCaller (this);
870 CheckComRCReturnRC (autoCaller.rc());
871
872 AutoLock alock (this);
873
874 CHECK_SETTER();
875
876 mHWData.backup();
877 mHWData->mMemorySize = memorySize;
878
879 return S_OK;
880}
881
882STDMETHODIMP Machine::COMGETTER(VRAMSize) (ULONG *memorySize)
883{
884 if (!memorySize)
885 return E_POINTER;
886
887 AutoCaller autoCaller (this);
888 CheckComRCReturnRC (autoCaller.rc());
889
890 AutoReaderLock alock (this);
891
892 *memorySize = mHWData->mVRAMSize;
893
894 return S_OK;
895}
896
897STDMETHODIMP Machine::COMSETTER(VRAMSize) (ULONG memorySize)
898{
899 /* check VRAM limits */
900 if (memorySize < SchemaDefs::MinGuestVRAM ||
901 memorySize > SchemaDefs::MaxGuestVRAM)
902 return setError (E_INVALIDARG,
903 tr ("Invalid VRAM size: %lu MB (must be in range [%lu, %lu] MB)"),
904 memorySize, SchemaDefs::MinGuestVRAM, SchemaDefs::MaxGuestVRAM);
905
906 AutoCaller autoCaller (this);
907 CheckComRCReturnRC (autoCaller.rc());
908
909 AutoLock alock (this);
910
911 CHECK_SETTER();
912
913 mHWData.backup();
914 mHWData->mVRAMSize = memorySize;
915
916 return S_OK;
917}
918
919STDMETHODIMP Machine::COMGETTER(BIOSSettings)(IBIOSSettings **biosSettings)
920{
921 if (!biosSettings)
922 return E_POINTER;
923
924 AutoCaller autoCaller (this);
925 CheckComRCReturnRC (autoCaller.rc());
926
927 /* mBIOSSettings is constant during life time, no need to lock */
928 mBIOSSettings.queryInterfaceTo (biosSettings);
929
930 return S_OK;
931}
932
933STDMETHODIMP Machine::COMGETTER(HWVirtExEnabled)(TriStateBool_T *enabled)
934{
935 if (!enabled)
936 return E_POINTER;
937
938 AutoCaller autoCaller (this);
939 CheckComRCReturnRC (autoCaller.rc());
940
941 AutoReaderLock alock (this);
942
943 *enabled = mHWData->mHWVirtExEnabled;
944
945 return S_OK;
946}
947
948STDMETHODIMP Machine::COMSETTER(HWVirtExEnabled)(TriStateBool_T enable)
949{
950 AutoCaller autoCaller (this);
951 CheckComRCReturnRC (autoCaller.rc());
952
953 AutoLock alock (this);
954
955 CHECK_SETTER();
956
957 /** @todo check validity! */
958
959 mHWData.backup();
960 mHWData->mHWVirtExEnabled = enable;
961
962 return S_OK;
963}
964
965STDMETHODIMP Machine::COMGETTER(SnapshotFolder) (BSTR *aSnapshotFolder)
966{
967 if (!aSnapshotFolder)
968 return E_POINTER;
969
970 AutoCaller autoCaller (this);
971 CheckComRCReturnRC (autoCaller.rc());
972
973 AutoReaderLock alock (this);
974
975 mUserData->mSnapshotFolderFull.cloneTo (aSnapshotFolder);
976
977 return S_OK;
978}
979
980STDMETHODIMP Machine::COMSETTER(SnapshotFolder) (INPTR BSTR aSnapshotFolder)
981{
982 /// @todo (r=dmik):
983 // 1. Allow to change the name of the snapshot folder containing snapshots
984 // 2. Rename the folder on disk instead of just changing the property
985 // value (to be smart and not to leave garbage). Note that it cannot be
986 // done here because the change may be rolled back. Thus, the right
987 // place is #saveSettings().
988
989 AutoCaller autoCaller (this);
990 CheckComRCReturnRC (autoCaller.rc());
991
992 AutoLock alock (this);
993
994 CHECK_SETTER();
995
996 if (!mData->mCurrentSnapshot.isNull())
997 return setError (E_FAIL,
998 tr ("The snapshot folder of a machine with snapshots cannot "
999 "be changed (please discard all snapshots first)"));
1000
1001 Utf8Str snapshotFolder = aSnapshotFolder;
1002
1003 if (snapshotFolder.isEmpty())
1004 {
1005 if (isInOwnDir())
1006 {
1007 /* the default snapshots folder is 'Snapshots' in the machine dir */
1008 snapshotFolder = Utf8Str ("Snapshots");
1009 }
1010 else
1011 {
1012 /* the default snapshots folder is {UUID}, for backwards
1013 * compatibility and to resolve conflicts */
1014 snapshotFolder = Utf8StrFmt ("{%Vuuid}", mData->mUuid.raw());
1015 }
1016 }
1017
1018 int vrc = calculateFullPath (snapshotFolder, snapshotFolder);
1019 if (VBOX_FAILURE (vrc))
1020 return setError (E_FAIL,
1021 tr ("Invalid snapshot folder: '%ls' (%Vrc)"),
1022 aSnapshotFolder, vrc);
1023
1024 mUserData.backup();
1025 mUserData->mSnapshotFolder = aSnapshotFolder;
1026 mUserData->mSnapshotFolderFull = snapshotFolder;
1027
1028 return S_OK;
1029}
1030
1031STDMETHODIMP Machine::COMGETTER(HardDiskAttachments) (IHardDiskAttachmentCollection **attachments)
1032{
1033 if (!attachments)
1034 return E_POINTER;
1035
1036 AutoCaller autoCaller (this);
1037 CheckComRCReturnRC (autoCaller.rc());
1038
1039 AutoReaderLock alock (this);
1040
1041 ComObjPtr <HardDiskAttachmentCollection> collection;
1042 collection.createObject();
1043 collection->init (mHDData->mHDAttachments);
1044 collection.queryInterfaceTo (attachments);
1045
1046 return S_OK;
1047}
1048
1049STDMETHODIMP Machine::COMGETTER(VRDPServer)(IVRDPServer **vrdpServer)
1050{
1051#ifdef VBOX_VRDP
1052 if (!vrdpServer)
1053 return E_POINTER;
1054
1055 AutoCaller autoCaller (this);
1056 CheckComRCReturnRC (autoCaller.rc());
1057
1058 AutoReaderLock alock (this);
1059
1060 Assert (!!mVRDPServer);
1061 mVRDPServer.queryInterfaceTo (vrdpServer);
1062
1063 return S_OK;
1064#else
1065 return E_NOTIMPL;
1066#endif
1067}
1068
1069STDMETHODIMP Machine::COMGETTER(DVDDrive) (IDVDDrive **dvdDrive)
1070{
1071 if (!dvdDrive)
1072 return E_POINTER;
1073
1074 AutoCaller autoCaller (this);
1075 CheckComRCReturnRC (autoCaller.rc());
1076
1077 AutoReaderLock alock (this);
1078
1079 Assert (!!mDVDDrive);
1080 mDVDDrive.queryInterfaceTo (dvdDrive);
1081 return S_OK;
1082}
1083
1084STDMETHODIMP Machine::COMGETTER(FloppyDrive) (IFloppyDrive **floppyDrive)
1085{
1086 if (!floppyDrive)
1087 return E_POINTER;
1088
1089 AutoCaller autoCaller (this);
1090 CheckComRCReturnRC (autoCaller.rc());
1091
1092 AutoReaderLock alock (this);
1093
1094 Assert (!!mFloppyDrive);
1095 mFloppyDrive.queryInterfaceTo (floppyDrive);
1096 return S_OK;
1097}
1098
1099STDMETHODIMP Machine::COMGETTER(AudioAdapter)(IAudioAdapter **audioAdapter)
1100{
1101 if (!audioAdapter)
1102 return E_POINTER;
1103
1104 AutoCaller autoCaller (this);
1105 CheckComRCReturnRC (autoCaller.rc());
1106
1107 AutoReaderLock alock (this);
1108
1109 mAudioAdapter.queryInterfaceTo (audioAdapter);
1110 return S_OK;
1111}
1112
1113STDMETHODIMP Machine::COMGETTER(USBController)(IUSBController * *a_ppUSBController)
1114{
1115#ifdef VBOX_WITH_USB
1116 if (!a_ppUSBController)
1117 return E_POINTER;
1118
1119 AutoCaller autoCaller (this);
1120 CheckComRCReturnRC (autoCaller.rc());
1121
1122 HRESULT rc = mParent->host()->checkUSBProxyService();
1123 CheckComRCReturnRC (rc);
1124
1125 AutoReaderLock alock (this);
1126
1127 mUSBController.queryInterfaceTo (a_ppUSBController);
1128 return S_OK;
1129#else
1130 /* Note: The GUI depends on this method returning E_NOTIMPL with no
1131 * extended error info to indicate that USB is simply not available
1132 * (w/o treting it as a failure), for example, as in OSE */
1133 return E_NOTIMPL;
1134#endif
1135}
1136
1137STDMETHODIMP Machine::COMGETTER(SettingsFilePath) (BSTR *filePath)
1138{
1139 if (!filePath)
1140 return E_POINTER;
1141
1142 AutoLimitedCaller autoCaller (this);
1143 CheckComRCReturnRC (autoCaller.rc());
1144
1145 AutoReaderLock alock (this);
1146
1147 mData->mConfigFileFull.cloneTo (filePath);
1148 return S_OK;
1149}
1150
1151STDMETHODIMP Machine::COMGETTER(SettingsModified) (BOOL *modified)
1152{
1153 if (!modified)
1154 return E_POINTER;
1155
1156 AutoCaller autoCaller (this);
1157 CheckComRCReturnRC (autoCaller.rc());
1158
1159 AutoLock alock (this);
1160
1161 CHECK_SETTER();
1162
1163 if (!isConfigLocked())
1164 {
1165 /*
1166 * if we're ready and isConfigLocked() is FALSE then it means
1167 * that no config file exists yet, so always return TRUE
1168 */
1169 *modified = TRUE;
1170 }
1171 else
1172 {
1173 *modified = isModified();
1174 }
1175
1176 return S_OK;
1177}
1178
1179STDMETHODIMP Machine::COMGETTER(SessionState) (SessionState_T *sessionState)
1180{
1181 if (!sessionState)
1182 return E_POINTER;
1183
1184 AutoCaller autoCaller (this);
1185 CheckComRCReturnRC (autoCaller.rc());
1186
1187 AutoReaderLock alock (this);
1188
1189 *sessionState = mData->mSession.mState;
1190
1191 return S_OK;
1192}
1193
1194STDMETHODIMP Machine::COMGETTER(State) (MachineState_T *machineState)
1195{
1196 if (!machineState)
1197 return E_POINTER;
1198
1199 AutoCaller autoCaller (this);
1200 CheckComRCReturnRC (autoCaller.rc());
1201
1202 AutoReaderLock alock (this);
1203
1204 *machineState = mData->mMachineState;
1205
1206 return S_OK;
1207}
1208
1209STDMETHODIMP Machine::COMGETTER(LastStateChange) (LONG64 *aLastStateChange)
1210{
1211 if (!aLastStateChange)
1212 return E_POINTER;
1213
1214 AutoCaller autoCaller (this);
1215 CheckComRCReturnRC (autoCaller.rc());
1216
1217 AutoReaderLock alock (this);
1218
1219 *aLastStateChange = mData->mLastStateChange;
1220
1221 return S_OK;
1222}
1223
1224STDMETHODIMP Machine::COMGETTER(StateFilePath) (BSTR *aStateFilePath)
1225{
1226 if (!aStateFilePath)
1227 return E_POINTER;
1228
1229 AutoCaller autoCaller (this);
1230 CheckComRCReturnRC (autoCaller.rc());
1231
1232 AutoReaderLock alock (this);
1233
1234 mSSData->mStateFilePath.cloneTo (aStateFilePath);
1235
1236 return S_OK;
1237}
1238
1239STDMETHODIMP Machine::COMGETTER(CurrentSnapshot) (ISnapshot **aCurrentSnapshot)
1240{
1241 if (!aCurrentSnapshot)
1242 return E_POINTER;
1243
1244 AutoCaller autoCaller (this);
1245 CheckComRCReturnRC (autoCaller.rc());
1246
1247 AutoReaderLock alock (this);
1248
1249 mData->mCurrentSnapshot.queryInterfaceTo (aCurrentSnapshot);
1250
1251 return S_OK;
1252}
1253
1254STDMETHODIMP Machine::COMGETTER(SnapshotCount) (ULONG *aSnapshotCount)
1255{
1256 if (!aSnapshotCount)
1257 return E_POINTER;
1258
1259 AutoCaller autoCaller (this);
1260 CheckComRCReturnRC (autoCaller.rc());
1261
1262 AutoReaderLock alock (this);
1263
1264 *aSnapshotCount = !mData->mFirstSnapshot ? 0 :
1265 mData->mFirstSnapshot->descendantCount() + 1 /* self */;
1266
1267 return S_OK;
1268}
1269
1270STDMETHODIMP Machine::COMGETTER(CurrentStateModified) (BOOL *aCurrentStateModified)
1271{
1272 if (!aCurrentStateModified)
1273 return E_POINTER;
1274
1275 AutoCaller autoCaller (this);
1276 CheckComRCReturnRC (autoCaller.rc());
1277
1278 AutoReaderLock alock (this);
1279
1280 /*
1281 * Note: for machines with no snapshots, we always return FALSE
1282 * (mData->mCurrentStateModified will be TRUE in this case, for historical
1283 * reasons :)
1284 */
1285
1286 *aCurrentStateModified = !mData->mFirstSnapshot ? FALSE :
1287 mData->mCurrentStateModified;
1288
1289 return S_OK;
1290}
1291
1292STDMETHODIMP
1293Machine::COMGETTER(SharedFolders) (ISharedFolderCollection **aSharedFolders)
1294{
1295 if (!aSharedFolders)
1296 return E_POINTER;
1297
1298 AutoCaller autoCaller (this);
1299 CheckComRCReturnRC (autoCaller.rc());
1300
1301 AutoReaderLock alock (this);
1302
1303 ComObjPtr <SharedFolderCollection> coll;
1304 coll.createObject();
1305 coll->init (mHWData->mSharedFolders);
1306 coll.queryInterfaceTo (aSharedFolders);
1307
1308 return S_OK;
1309}
1310
1311STDMETHODIMP
1312Machine::COMGETTER(ClipboardMode) (ClipboardMode_T *aClipboardMode)
1313{
1314 if (!aClipboardMode)
1315 return E_POINTER;
1316
1317 AutoCaller autoCaller (this);
1318 CheckComRCReturnRC (autoCaller.rc());
1319
1320 AutoReaderLock alock (this);
1321
1322 *aClipboardMode = mHWData->mClipboardMode;
1323
1324 return S_OK;
1325}
1326
1327STDMETHODIMP
1328Machine::COMSETTER(ClipboardMode) (ClipboardMode_T aClipboardMode)
1329{
1330 AutoCaller autoCaller (this);
1331 CheckComRCReturnRC (autoCaller.rc());
1332
1333 AutoLock alock (this);
1334
1335 CHECK_SETTER();
1336
1337 mHWData.backup();
1338 mHWData->mClipboardMode = aClipboardMode;
1339
1340 return S_OK;
1341}
1342
1343// IMachine methods
1344/////////////////////////////////////////////////////////////////////////////
1345
1346STDMETHODIMP Machine::SetBootOrder (ULONG aPosition, DeviceType_T aDevice)
1347{
1348 if (aPosition < 1 || aPosition > SchemaDefs::MaxBootPosition)
1349 return setError (E_INVALIDARG,
1350 tr ("Invalid boot position: %lu (must be in range [1, %lu])"),
1351 aPosition, SchemaDefs::MaxBootPosition);
1352
1353 if (aDevice == DeviceType_USBDevice)
1354 return setError (E_FAIL,
1355 tr ("Booting from USB devices is not currently supported"));
1356
1357 AutoCaller autoCaller (this);
1358 CheckComRCReturnRC (autoCaller.rc());
1359
1360 AutoLock alock (this);
1361
1362 CHECK_SETTER();
1363
1364 mHWData.backup();
1365 mHWData->mBootOrder [aPosition - 1] = aDevice;
1366
1367 return S_OK;
1368}
1369
1370STDMETHODIMP Machine::GetBootOrder (ULONG aPosition, DeviceType_T *aDevice)
1371{
1372 if (aPosition < 1 || aPosition > SchemaDefs::MaxBootPosition)
1373 return setError (E_INVALIDARG,
1374 tr ("Invalid boot position: %lu (must be in range [1, %lu])"),
1375 aPosition, SchemaDefs::MaxBootPosition);
1376
1377 AutoCaller autoCaller (this);
1378 CheckComRCReturnRC (autoCaller.rc());
1379
1380 AutoReaderLock alock (this);
1381
1382 *aDevice = mHWData->mBootOrder [aPosition - 1];
1383
1384 return S_OK;
1385}
1386
1387STDMETHODIMP Machine::AttachHardDisk (INPTR GUIDPARAM aId,
1388 DiskControllerType_T aCtl, LONG aDev)
1389{
1390 Guid id = aId;
1391
1392 if (id.isEmpty() ||
1393 aCtl == DiskControllerType_InvalidController ||
1394 aDev < 0 || aDev > 1)
1395 return E_INVALIDARG;
1396
1397 AutoCaller autoCaller (this);
1398 CheckComRCReturnRC (autoCaller.rc());
1399
1400 AutoLock alock (this);
1401
1402 CHECK_SETTER();
1403
1404 if (!mData->mRegistered)
1405 return setError (E_FAIL,
1406 tr ("Cannot attach hard disks to an unregistered machine"));
1407
1408 AssertReturn (mData->mMachineState != MachineState_Saved, E_FAIL);
1409
1410 if (mData->mMachineState >= MachineState_Running)
1411 return setError (E_FAIL,
1412 tr ("Invalid machine state: %d"), mData->mMachineState);
1413
1414 /* see if the device on the controller is already busy */
1415 for (HDData::HDAttachmentList::const_iterator it = mHDData->mHDAttachments.begin();
1416 it != mHDData->mHDAttachments.end(); ++ it)
1417 {
1418 ComObjPtr <HardDiskAttachment> hda = *it;
1419 if (hda->controller() == aCtl && hda->deviceNumber() == aDev)
1420 {
1421 ComObjPtr <HardDisk> hd = hda->hardDisk();
1422 AutoLock hdLock (hd);
1423 return setError (E_FAIL,
1424 tr ("Hard disk '%ls' is already attached to device slot %d "
1425 "on controller %d"),
1426 hd->toString().raw(), aDev, aCtl);
1427 }
1428 }
1429
1430 /* find a hard disk by UUID */
1431 ComObjPtr <HardDisk> hd;
1432 HRESULT rc = mParent->getHardDisk (id, hd);
1433 if (FAILED (rc))
1434 return rc;
1435
1436 AutoLock hdLock (hd);
1437
1438 if (hd->isDifferencing())
1439 return setError (E_FAIL,
1440 tr ("Cannot attach the differencing hard disk '%ls'"),
1441 hd->toString().raw());
1442
1443 bool dirty = false;
1444
1445 switch (hd->type())
1446 {
1447 case HardDiskType_ImmutableHardDisk:
1448 {
1449 Assert (hd->machineId().isEmpty());
1450 /*
1451 * increase readers to protect from unregistration
1452 * until rollback()/commit() is done
1453 */
1454 hd->addReader();
1455 // LogTraceMsg (("A: %ls proteced\n", hd->toString().raw()));
1456 dirty = true;
1457 break;
1458 }
1459 case HardDiskType_WritethroughHardDisk:
1460 {
1461 Assert (hd->children().size() == 0);
1462 Assert (hd->snapshotId().isEmpty());
1463 /* fall through */
1464 }
1465 case HardDiskType_NormalHardDisk:
1466 {
1467 if (hd->machineId().isEmpty())
1468 {
1469 /* attach directly */
1470 hd->setMachineId (mData->mUuid);
1471 // LogTraceMsg (("A: %ls associated with %Vuuid\n",
1472 // hd->toString().raw(), mData->mUuid.raw()));
1473 dirty = true;
1474 }
1475 else
1476 {
1477 /* determine what the hard disk is already attached to */
1478 if (hd->snapshotId().isEmpty())
1479 {
1480 /* attached to some VM in its current state */
1481 if (hd->machineId() == mData->mUuid)
1482 {
1483 /*
1484 * attached to us, either in the backed up list of the
1485 * attachments or in the current one; the former is ok
1486 * (reattachment takes place within the same
1487 * "transaction") the latter is an error so check for it
1488 */
1489 for (HDData::HDAttachmentList::const_iterator it =
1490 mHDData->mHDAttachments.begin();
1491 it != mHDData->mHDAttachments.end(); ++ it)
1492 {
1493 if ((*it)->hardDisk().equalsTo (hd))
1494 {
1495 return setError (E_FAIL,
1496 tr ("Normal/Writethrough hard disk '%ls' is "
1497 "currently attached to device slot %d "
1498 "on controller %d of this machine"),
1499 hd->toString().raw(),
1500 (*it)->deviceNumber(), (*it)->controller());
1501 }
1502 }
1503 /*
1504 * dirty = false to indicate we didn't set machineId
1505 * and prevent it from being reset in DetachHardDisk()
1506 */
1507 // LogTraceMsg (("A: %ls found in old\n", hd->toString().raw()));
1508 }
1509 else
1510 {
1511 /* attached to other VM */
1512 return setError (E_FAIL,
1513 tr ("Normal/Writethrough hard disk '%ls' is "
1514 "currently attached to a machine with "
1515 "UUID {%Vuuid}"),
1516 hd->toString().raw(), hd->machineId().raw());
1517 }
1518 }
1519 else
1520 {
1521 /*
1522 * here we go when the HardDiskType_NormalHardDisk
1523 * is attached to some VM (probably to this one, too)
1524 * at some particular snapshot, so we can create a diff
1525 * based on it
1526 */
1527 Assert (!hd->machineId().isEmpty());
1528 /*
1529 * increase readers to protect from unregistration
1530 * until rollback()/commit() is done
1531 */
1532 hd->addReader();
1533 // LogTraceMsg (("A: %ls proteced\n", hd->toString().raw()));
1534 dirty = true;
1535 }
1536 }
1537
1538 break;
1539 }
1540 }
1541
1542 ComObjPtr <HardDiskAttachment> attachment;
1543 attachment.createObject();
1544 attachment->init (hd, aCtl, aDev, dirty);
1545
1546 mHDData.backup();
1547 mHDData->mHDAttachments.push_back (attachment);
1548 // LogTraceMsg (("A: %ls attached\n", hd->toString().raw()));
1549
1550 /* note: diff images are actually created only in commit() */
1551
1552 return S_OK;
1553}
1554
1555STDMETHODIMP Machine::GetHardDisk (DiskControllerType_T aCtl,
1556 LONG aDev, IHardDisk **aHardDisk)
1557{
1558 if (aCtl == DiskControllerType_InvalidController ||
1559 aDev < 0 || aDev > 1)
1560 return E_INVALIDARG;
1561
1562 AutoCaller autoCaller (this);
1563 CheckComRCReturnRC (autoCaller.rc());
1564
1565 AutoReaderLock alock (this);
1566
1567 *aHardDisk = NULL;
1568
1569 for (HDData::HDAttachmentList::const_iterator it = mHDData->mHDAttachments.begin();
1570 it != mHDData->mHDAttachments.end(); ++ it)
1571 {
1572 ComObjPtr <HardDiskAttachment> hda = *it;
1573 if (hda->controller() == aCtl && hda->deviceNumber() == aDev)
1574 {
1575 hda->hardDisk().queryInterfaceTo (aHardDisk);
1576 return S_OK;
1577 }
1578 }
1579
1580 return setError (E_INVALIDARG,
1581 tr ("No hard disk attached to device slot %d on controller %d"),
1582 aDev, aCtl);
1583}
1584
1585STDMETHODIMP Machine::DetachHardDisk (DiskControllerType_T aCtl, LONG aDev)
1586{
1587 if (aCtl == DiskControllerType_InvalidController ||
1588 aDev < 0 || aDev > 1)
1589 return E_INVALIDARG;
1590
1591 AutoCaller autoCaller (this);
1592 CheckComRCReturnRC (autoCaller.rc());
1593
1594 AutoLock alock (this);
1595
1596 CHECK_SETTER();
1597
1598 AssertReturn (mData->mMachineState != MachineState_Saved, E_FAIL);
1599
1600 if (mData->mMachineState >= MachineState_Running)
1601 return setError (E_FAIL,
1602 tr ("Invalid machine state: %d"), mData->mMachineState);
1603
1604 for (HDData::HDAttachmentList::iterator it = mHDData->mHDAttachments.begin();
1605 it != mHDData->mHDAttachments.end(); ++ it)
1606 {
1607 ComObjPtr <HardDiskAttachment> hda = *it;
1608 if (hda->controller() == aCtl && hda->deviceNumber() == aDev)
1609 {
1610 ComObjPtr <HardDisk> hd = hda->hardDisk();
1611 AutoLock hdLock (hd);
1612
1613 ComAssertRet (hd->children().size() == 0 &&
1614 hd->machineId() == mData->mUuid, E_FAIL);
1615
1616 if (hda->isDirty())
1617 {
1618 switch (hd->type())
1619 {
1620 case HardDiskType_ImmutableHardDisk:
1621 {
1622 /* decrease readers increased in AttachHardDisk() */
1623 hd->releaseReader();
1624 // LogTraceMsg (("D: %ls released\n", hd->toString().raw()));
1625 break;
1626 }
1627 case HardDiskType_WritethroughHardDisk:
1628 {
1629 /* deassociate from this machine */
1630 hd->setMachineId (Guid());
1631 // LogTraceMsg (("D: %ls deassociated\n", hd->toString().raw()));
1632 break;
1633 }
1634 case HardDiskType_NormalHardDisk:
1635 {
1636 if (hd->snapshotId().isEmpty())
1637 {
1638 /* deassociate from this machine */
1639 hd->setMachineId (Guid());
1640 // LogTraceMsg (("D: %ls deassociated\n", hd->toString().raw()));
1641 }
1642 else
1643 {
1644 /* decrease readers increased in AttachHardDisk() */
1645 hd->releaseReader();
1646 // LogTraceMsg (("%ls released\n", hd->toString().raw()));
1647 }
1648
1649 break;
1650 }
1651 }
1652 }
1653
1654 mHDData.backup();
1655 /*
1656 * we cannot use erase (it) below because backup() above will create
1657 * a copy of the list and make this copy active, but the iterator
1658 * still refers to the original and is not valid for a copy
1659 */
1660 mHDData->mHDAttachments.remove (hda);
1661 // LogTraceMsg (("D: %ls detached\n", hd->toString().raw()));
1662
1663 /*
1664 * note: Non-dirty hard disks are actually deassociated
1665 * and diff images are deleted only in commit()
1666 */
1667
1668 return S_OK;
1669 }
1670 }
1671
1672 return setError (E_INVALIDARG,
1673 tr ("No hard disk attached to device slot %d on controller %d"),
1674 aDev, aCtl);
1675}
1676
1677STDMETHODIMP Machine::GetNetworkAdapter (ULONG slot, INetworkAdapter **adapter)
1678{
1679 if (!adapter)
1680 return E_POINTER;
1681 if (slot >= ELEMENTS (mNetworkAdapters))
1682 return setError (E_INVALIDARG, tr ("Invalid slot number: %d"), slot);
1683
1684 AutoCaller autoCaller (this);
1685 CheckComRCReturnRC (autoCaller.rc());
1686
1687 AutoReaderLock alock (this);
1688
1689 mNetworkAdapters [slot].queryInterfaceTo (adapter);
1690
1691 return S_OK;
1692}
1693
1694/**
1695 * Returns the extra data key name following the given key. If the key
1696 * is not found, an error is returned. If NULL is supplied, the first
1697 * key will be returned. If key is the last item, NULL will be returned.
1698 *
1699 * @returns COM status code
1700 * @param key extra data key name
1701 * @param nextKey name of the key following "key". NULL if "key" is the last.
1702 * @param nextValue value of the key following "key". Optional parameter.
1703 */
1704STDMETHODIMP Machine::GetNextExtraDataKey(INPTR BSTR key, BSTR *nextKey, BSTR *nextValue)
1705{
1706 if (!nextKey)
1707 return E_POINTER;
1708
1709 AutoCaller autoCaller (this);
1710 CheckComRCReturnRC (autoCaller.rc());
1711
1712 AutoReaderLock alock (this);
1713
1714 /* start with nothing found */
1715 *nextKey = NULL;
1716
1717 /*
1718 * if we're ready and isConfigLocked() is FALSE then it means
1719 * that no config file exists yet, so return shortly
1720 */
1721 if (!isConfigLocked())
1722 return S_OK;
1723
1724 HRESULT rc = S_OK;
1725
1726 /* load the config file */
1727 CFGHANDLE configLoader = 0;
1728 rc = openConfigLoader (&configLoader);
1729 if (FAILED (rc))
1730 return E_FAIL;
1731
1732 CFGNODE machineNode;
1733 CFGNODE extraDataNode;
1734
1735 /* navigate to the right position */
1736 if (VBOX_SUCCESS(CFGLDRGetNode(configLoader, "VirtualBox/Machine", 0, &machineNode)) &&
1737 VBOX_SUCCESS(CFGLDRGetChildNode(machineNode, "ExtraData", 0, &extraDataNode)))
1738 {
1739 /* check if it exists */
1740 bool found = false;
1741 unsigned count;
1742 CFGNODE extraDataItemNode;
1743 CFGLDRCountChildren(extraDataNode, "ExtraDataItem", &count);
1744 for (unsigned i = 0; (i < count) && (found == false); i++)
1745 {
1746 Bstr name;
1747 CFGLDRGetChildNode(extraDataNode, "ExtraDataItem", i, &extraDataItemNode);
1748 CFGLDRQueryBSTR(extraDataItemNode, "name", name.asOutParam());
1749
1750 /* if we're supposed to return the first one */
1751 if (key == NULL)
1752 {
1753 name.cloneTo(nextKey);
1754 if (nextValue)
1755 CFGLDRQueryBSTR(extraDataItemNode, "value", nextValue);
1756 found = true;
1757 }
1758 /* did we find the key we're looking for? */
1759 else if (name == key)
1760 {
1761 found = true;
1762 /* is there another item? */
1763 if (i + 1 < count)
1764 {
1765 CFGLDRGetChildNode(extraDataNode, "ExtraDataItem", i + 1, &extraDataItemNode);
1766 CFGLDRQueryBSTR(extraDataItemNode, "name", name.asOutParam());
1767 name.cloneTo(nextKey);
1768 if (nextValue)
1769 CFGLDRQueryBSTR(extraDataItemNode, "value", nextValue);
1770 found = true;
1771 }
1772 else
1773 {
1774 /* it's the last one */
1775 *nextKey = NULL;
1776 }
1777 }
1778 CFGLDRReleaseNode(extraDataItemNode);
1779 }
1780
1781 /* if we haven't found the key, it's an error */
1782 if (!found)
1783 rc = setError(E_FAIL, tr("Could not find extra data key"));
1784
1785 CFGLDRReleaseNode(extraDataNode);
1786 CFGLDRReleaseNode(machineNode);
1787 }
1788
1789 closeConfigLoader (configLoader, false /* aSaveBeforeClose */);
1790
1791 return rc;
1792}
1793
1794/**
1795 * Returns associated extra data from the configuration. If the key does
1796 * not exist, NULL will be stored in the output pointer.
1797 *
1798 * @returns COM status code
1799 * @param key extra data key
1800 * @param value address of result pointer
1801 */
1802STDMETHODIMP Machine::GetExtraData(INPTR BSTR key, BSTR *value)
1803{
1804 if (!key)
1805 return E_INVALIDARG;
1806 if (!value)
1807 return E_POINTER;
1808
1809 AutoCaller autoCaller (this);
1810 CheckComRCReturnRC (autoCaller.rc());
1811
1812 AutoReaderLock alock (this);
1813
1814 /* start with nothing found */
1815 *value = NULL;
1816
1817 /*
1818 * if we're ready and isConfigLocked() is FALSE then it means
1819 * that no config file exists yet, so return shortly
1820 */
1821 if (!isConfigLocked())
1822 return S_OK;
1823
1824 HRESULT rc = S_OK;
1825
1826 /* load the config file */
1827 CFGHANDLE configLoader = 0;
1828 rc = openConfigLoader (&configLoader);
1829 if (FAILED (rc))
1830 return E_FAIL;
1831
1832 CFGNODE machineNode;
1833 CFGNODE extraDataNode;
1834
1835 /* navigate to the right position */
1836 if (VBOX_SUCCESS(CFGLDRGetNode(configLoader, "VirtualBox/Machine", 0, &machineNode)) &&
1837 VBOX_SUCCESS(CFGLDRGetChildNode(machineNode, "ExtraData", 0, &extraDataNode)))
1838 {
1839 /* check if it exists */
1840 bool found = false;
1841 unsigned count;
1842 CFGNODE extraDataItemNode;
1843 CFGLDRCountChildren(extraDataNode, "ExtraDataItem", &count);
1844 for (unsigned i = 0; (i < count) && (found == false); i++)
1845 {
1846 Bstr name;
1847 CFGLDRGetChildNode(extraDataNode, "ExtraDataItem", i, &extraDataItemNode);
1848 CFGLDRQueryBSTR(extraDataItemNode, "name", name.asOutParam());
1849 if (name == key)
1850 {
1851 found = true;
1852 CFGLDRQueryBSTR(extraDataItemNode, "value", value);
1853 }
1854 CFGLDRReleaseNode(extraDataItemNode);
1855 }
1856
1857 CFGLDRReleaseNode(extraDataNode);
1858 CFGLDRReleaseNode(machineNode);
1859 }
1860
1861 rc = closeConfigLoader (configLoader, false /* aSaveBeforeClose */);
1862
1863 return rc;
1864}
1865
1866/**
1867 * Stores associated extra data in the configuration. If the data value is NULL
1868 * then the corresponding extra data item is deleted. This method can be called
1869 * outside a session and therefore belongs to the non protected machine data.
1870 *
1871 * @param key extra data key
1872 * @param value extra data value
1873 *
1874 * @note Locks mParent for reading + this object for writing.
1875 */
1876STDMETHODIMP Machine::SetExtraData (INPTR BSTR key, INPTR BSTR value)
1877{
1878 if (!key)
1879 return E_INVALIDARG;
1880
1881 AutoCaller autoCaller (this);
1882 CheckComRCReturnRC (autoCaller.rc());
1883
1884 /* VirtualBox::onExtraDataCanChange() needs mParent lock */
1885 AutoMultiLock <2> alock (mParent->rlock(), this->wlock());
1886
1887 if (mType == IsSnapshotMachine)
1888 CHECK_SETTER();
1889
1890 bool changed = false;
1891 HRESULT rc = S_OK;
1892
1893 /*
1894 * if we're ready and isConfigLocked() is FALSE then it means
1895 * that no config file exists yet, so call saveSettings() to create one
1896 */
1897 if (!isConfigLocked())
1898 {
1899 rc = saveSettings (false /* aMarkCurStateAsModified */);
1900 if (FAILED (rc))
1901 return rc;
1902 }
1903
1904 /* load the config file */
1905 CFGHANDLE configLoader = 0;
1906 rc = openConfigLoader (&configLoader);
1907 if (FAILED (rc))
1908 return rc;
1909
1910 CFGNODE machineNode = 0;
1911 CFGNODE extraDataNode = 0;
1912
1913 int vrc = CFGLDRGetNode (configLoader, "VirtualBox/Machine", 0, &machineNode);
1914 if (VBOX_FAILURE (vrc))
1915 vrc = CFGLDRCreateNode (configLoader, "VirtualBox/Machine", &machineNode);
1916
1917 vrc = CFGLDRGetChildNode (machineNode, "ExtraData", 0, &extraDataNode);
1918 if (VBOX_FAILURE (vrc) && value)
1919 vrc = CFGLDRCreateChildNode (machineNode, "ExtraData", &extraDataNode);
1920
1921 if (extraDataNode)
1922 {
1923 CFGNODE extraDataItemNode = 0;
1924 Bstr oldVal;
1925
1926 unsigned count;
1927 CFGLDRCountChildren (extraDataNode, "ExtraDataItem", &count);
1928
1929 for (unsigned i = 0; i < count; i++)
1930 {
1931 CFGLDRGetChildNode (extraDataNode, "ExtraDataItem", i, &extraDataItemNode);
1932 Bstr name;
1933 CFGLDRQueryBSTR (extraDataItemNode, "name", name.asOutParam());
1934 if (name == key)
1935 {
1936 CFGLDRQueryBSTR (extraDataItemNode, "value", oldVal.asOutParam());
1937 break;
1938 }
1939 CFGLDRReleaseNode (extraDataItemNode);
1940 extraDataItemNode = 0;
1941 }
1942
1943 /*
1944 * When no key is found, oldVal is null
1945 * Note:
1946 * 1. when oldVal is null, |oldVal == (BSTR) NULL| is true
1947 * 2. we cannot do |oldVal != value| because it will compare
1948 * BSTR pointers instead of strings (due to type conversion ops)
1949 */
1950 changed = !(oldVal == value);
1951
1952 if (changed)
1953 {
1954 /* ask for permission from all listeners */
1955 if (!mParent->onExtraDataCanChange (mData->mUuid, key, value))
1956 {
1957 LogWarningFunc (("Someone vetoed! Change refused!\n"));
1958 rc = setError (E_ACCESSDENIED,
1959 tr ("Could not set extra data because someone refused "
1960 "the requested change of '%ls' to '%ls'"), key, value);
1961 }
1962 else
1963 {
1964 if (value)
1965 {
1966 if (!extraDataItemNode)
1967 {
1968 /* create a new item */
1969 CFGLDRAppendChildNode (extraDataNode, "ExtraDataItem",
1970 &extraDataItemNode);
1971 CFGLDRSetBSTR (extraDataItemNode, "name", key);
1972 }
1973 CFGLDRSetBSTR (extraDataItemNode, "value", value);
1974 }
1975 else
1976 {
1977 /* an old value does for sure exist here */
1978 CFGLDRDeleteNode (extraDataItemNode);
1979 extraDataItemNode = 0;
1980 }
1981 }
1982 }
1983
1984 if (extraDataItemNode)
1985 CFGLDRReleaseNode (extraDataItemNode);
1986
1987 CFGLDRReleaseNode (extraDataNode);
1988 }
1989
1990 CFGLDRReleaseNode (machineNode);
1991
1992 if (SUCCEEDED (rc) && changed)
1993 rc = closeConfigLoader (configLoader, true /* aSaveBeforeClose */);
1994 else
1995 closeConfigLoader (configLoader, false /* aSaveBeforeClose */);
1996
1997 /* fire an event */
1998 if (SUCCEEDED (rc) && changed)
1999 {
2000 mParent->onExtraDataChange (mData->mUuid, key, value);
2001 }
2002
2003 return rc;
2004}
2005
2006STDMETHODIMP Machine::SaveSettings()
2007{
2008 AutoCaller autoCaller (this);
2009 CheckComRCReturnRC (autoCaller.rc());
2010
2011 /* Under some circumstancies, saveSettings() needs mParent lock */
2012 AutoMultiLock <2> alock (mParent->wlock(), this->wlock());
2013
2014 CHECK_SETTER();
2015
2016 /* the settings file path may never be null */
2017 ComAssertRet (mData->mConfigFileFull, E_FAIL);
2018
2019 /* save all VM data excluding snapshots */
2020 return saveSettings();
2021}
2022
2023STDMETHODIMP Machine::DiscardSettings()
2024{
2025 AutoCaller autoCaller (this);
2026 CheckComRCReturnRC (autoCaller.rc());
2027
2028 AutoLock alock (this);
2029
2030 CHECK_SETTER();
2031
2032 /*
2033 * during this rollback, the session will be notified if data has
2034 * been actually changed
2035 */
2036 rollback (true /* aNotify */);
2037
2038 return S_OK;
2039}
2040
2041STDMETHODIMP Machine::DeleteSettings()
2042{
2043 AutoCaller autoCaller (this);
2044 CheckComRCReturnRC (autoCaller.rc());
2045
2046 AutoLock alock (this);
2047
2048 CHECK_SETTER();
2049
2050 if (mData->mRegistered)
2051 return setError (E_FAIL,
2052 tr ("Cannot delete settings of a registered machine"));
2053
2054 /* delete the settings only when the file actually exists */
2055 if (isConfigLocked())
2056 {
2057 unlockConfig();
2058 int vrc = RTFileDelete (Utf8Str (mData->mConfigFileFull));
2059 if (VBOX_FAILURE (vrc))
2060 return setError (E_FAIL,
2061 tr ("Could not delete the settings file '%ls' (%Vrc)"),
2062 mData->mConfigFileFull.raw(), vrc);
2063
2064 /* delete the Logs folder, nothing important should be left
2065 * there (we don't check for errors because the user might have
2066 * some private files there that we don't want to delete) */
2067 Utf8Str logFolder;
2068 getLogFolder (logFolder);
2069 Assert (!logFolder.isEmpty());
2070 if (RTDirExists (logFolder))
2071 {
2072 /* delete all VBox.log[.N] files from the Logs folder
2073 * (this must be in sync with the rotation logic in
2074 * Console::powerUpThread()) */
2075 Utf8Str log = Utf8StrFmt ("%s/VBox.log", logFolder.raw());
2076 RTFileDelete (log);
2077 for (int i = 3; i >= 0; i--)
2078 {
2079 log = Utf8StrFmt ("%s/VBox.log.%d", logFolder.raw(), i);
2080 RTFileDelete (log);
2081 }
2082
2083 RTDirRemove (logFolder);
2084 }
2085
2086 /* delete the Snapshots folder, nothing important should be left
2087 * there (we don't check for errors because the user might have
2088 * some private files there that we don't want to delete) */
2089 Utf8Str snapshotFolder = mUserData->mSnapshotFolderFull;
2090 Assert (!snapshotFolder.isEmpty());
2091 if (RTDirExists (snapshotFolder))
2092 RTDirRemove (snapshotFolder);
2093
2094 /* delete the directory that contains the settings file, but only
2095 * if it matches the VM name (i.e. a structure created by default in
2096 * openConfigLoader()) */
2097 {
2098 Utf8Str settingsDir;
2099 if (isInOwnDir (&settingsDir))
2100 RTDirRemove (settingsDir);
2101 }
2102 }
2103
2104 return S_OK;
2105}
2106
2107STDMETHODIMP Machine::GetSnapshot (INPTR GUIDPARAM aId, ISnapshot **aSnapshot)
2108{
2109 if (!aSnapshot)
2110 return E_POINTER;
2111
2112 AutoCaller autoCaller (this);
2113 CheckComRCReturnRC (autoCaller.rc());
2114
2115 AutoReaderLock alock (this);
2116
2117 Guid id = aId;
2118 ComObjPtr <Snapshot> snapshot;
2119
2120 HRESULT rc = findSnapshot (id, snapshot, true /* aSetError */);
2121 snapshot.queryInterfaceTo (aSnapshot);
2122
2123 return rc;
2124}
2125
2126STDMETHODIMP Machine::FindSnapshot (INPTR BSTR aName, ISnapshot **aSnapshot)
2127{
2128 if (!aName)
2129 return E_INVALIDARG;
2130 if (!aSnapshot)
2131 return E_POINTER;
2132
2133 AutoCaller autoCaller (this);
2134 CheckComRCReturnRC (autoCaller.rc());
2135
2136 AutoReaderLock alock (this);
2137
2138 ComObjPtr <Snapshot> snapshot;
2139
2140 HRESULT rc = findSnapshot (aName, snapshot, true /* aSetError */);
2141 snapshot.queryInterfaceTo (aSnapshot);
2142
2143 return rc;
2144}
2145
2146STDMETHODIMP Machine::SetCurrentSnapshot (INPTR GUIDPARAM aId)
2147{
2148 /// @todo (dmik) don't forget to set
2149 // mData->mCurrentStateModified to FALSE
2150
2151 return setError (E_NOTIMPL, "Not implemented");
2152}
2153
2154STDMETHODIMP
2155Machine::CreateSharedFolder (INPTR BSTR aName, INPTR BSTR aHostPath)
2156{
2157 if (!aName || !aHostPath)
2158 return E_INVALIDARG;
2159
2160 AutoCaller autoCaller (this);
2161 CheckComRCReturnRC (autoCaller.rc());
2162
2163 AutoLock alock (this);
2164
2165 CHECK_SETTER();
2166
2167 /// @todo (dmik) check global shared folders when they are done
2168
2169 ComObjPtr <SharedFolder> sharedFolder;
2170 HRESULT rc = findSharedFolder (aName, sharedFolder, false /* aSetError */);
2171 if (SUCCEEDED (rc))
2172 return setError (E_FAIL,
2173 tr ("Shared folder named '%ls' already exists"), aName);
2174
2175 sharedFolder.createObject();
2176 rc = sharedFolder->init (machine(), aName, aHostPath);
2177 if (FAILED (rc))
2178 return rc;
2179
2180 BOOL accessible = FALSE;
2181 rc = sharedFolder->COMGETTER(Accessible) (&accessible);
2182 if (FAILED (rc))
2183 return rc;
2184
2185 if (!accessible)
2186 return setError (E_FAIL,
2187 tr ("Shared folder path '%ls' is not accessible"), aHostPath);
2188
2189 mHWData.backup();
2190 mHWData->mSharedFolders.push_back (sharedFolder);
2191
2192 return S_OK;
2193}
2194
2195STDMETHODIMP Machine::RemoveSharedFolder (INPTR BSTR aName)
2196{
2197 if (!aName)
2198 return E_INVALIDARG;
2199
2200 AutoCaller autoCaller (this);
2201 CheckComRCReturnRC (autoCaller.rc());
2202
2203 AutoReaderLock alock (this);
2204
2205 CHECK_SETTER();
2206
2207 ComObjPtr <SharedFolder> sharedFolder;
2208 HRESULT rc = findSharedFolder (aName, sharedFolder, true /* aSetError */);
2209 if (FAILED (rc))
2210 return rc;
2211
2212 mHWData.backup();
2213 mHWData->mSharedFolders.remove (sharedFolder);
2214
2215 return S_OK;
2216}
2217
2218// public methods for internal purposes
2219/////////////////////////////////////////////////////////////////////////////
2220
2221/**
2222 * Returns the session machine object associated with the this machine.
2223 * The returned session machine is null if no direct session is currently open.
2224 *
2225 * @Note locks this object for reading.
2226 */
2227ComObjPtr <SessionMachine> Machine::sessionMachine()
2228{
2229 ComObjPtr <SessionMachine> sm;
2230
2231 AutoCaller autoCaller (this);
2232 /* the machine may be inaccessible, so don't assert below */
2233 if (FAILED (autoCaller.rc()))
2234 return sm;
2235
2236 AutoReaderLock alock (this);
2237
2238 sm = mData->mSession.mMachine;
2239 Assert (!sm.isNull() ||
2240 mData->mSession.mState != SessionState_SessionOpen);
2241
2242 return sm;
2243}
2244
2245/**
2246 * Calculates the absolute path of the given path taking the directory of
2247 * the machine settings file as the current directory.
2248 *
2249 * @param aPath path to calculate the absolute path for
2250 * @param aResult where to put the result (used only on success,
2251 * so can be the same Utf8Str instance as passed as \a aPath)
2252 * @return VirtualBox result
2253 *
2254 * @note Locks this object for reading.
2255 */
2256int Machine::calculateFullPath (const char *aPath, Utf8Str &aResult)
2257{
2258 AutoCaller autoCaller (this);
2259 AssertComRCReturn (autoCaller.rc(), autoCaller.rc());
2260
2261 AutoReaderLock alock (this);
2262
2263 AssertReturn (!mData->mConfigFileFull.isNull(), VERR_GENERAL_FAILURE);
2264
2265 Utf8Str settingsDir = mData->mConfigFileFull;
2266
2267 RTPathStripFilename (settingsDir.mutableRaw());
2268 char folder [RTPATH_MAX];
2269 int vrc = RTPathAbsEx (settingsDir, aPath,
2270 folder, sizeof (folder));
2271 if (VBOX_SUCCESS (vrc))
2272 aResult = folder;
2273
2274 return vrc;
2275}
2276
2277/**
2278 * Tries to calculate the relative path of the given absolute path using the
2279 * directory of the machine settings file as the base directory.
2280 *
2281 * @param aPath absolute path to calculate the relative path for
2282 * @param aResult where to put the result (used only when it's possible to
2283 * make a relative path from the given absolute path;
2284 * otherwise left untouched)
2285 *
2286 * @note Locks this object for reading.
2287 */
2288void Machine::calculateRelativePath (const char *aPath, Utf8Str &aResult)
2289{
2290 AutoCaller autoCaller (this);
2291 AssertComRCReturn (autoCaller.rc(), (void) 0);
2292
2293 AutoReaderLock alock (this);
2294
2295 AssertReturnVoid (!mData->mConfigFileFull.isNull());
2296
2297 Utf8Str settingsDir = mData->mConfigFileFull;
2298
2299 RTPathStripFilename (settingsDir.mutableRaw());
2300 if (RTPathStartsWith (aPath, settingsDir))
2301 {
2302 /* when assigning, we create a separate Utf8Str instance because both
2303 * aPath and aResult can point to the same memory location when this
2304 * func is called (if we just do aResult = aPath, aResult will be freed
2305 * first, and since its the same as aPath, an attempt to copy garbage
2306 * will be made. */
2307 aResult = Utf8Str (aPath + settingsDir.length() + 1);
2308 }
2309}
2310
2311/**
2312 * Returns the full path to the machine's log folder in the
2313 * \a aLogFolder argument.
2314 */
2315void Machine::getLogFolder (Utf8Str &aLogFolder)
2316{
2317 AutoCaller autoCaller (this);
2318 AssertComRCReturn (autoCaller.rc(), (void) 0);
2319
2320 AutoReaderLock alock (this);
2321
2322 Utf8Str settingsDir;
2323 if (isInOwnDir (&settingsDir))
2324 {
2325 /* Log folder is <Machines>/<VM_Name>/Logs */
2326 aLogFolder = Utf8StrFmt ("%s%cLogs", settingsDir.raw(), RTPATH_DELIMITER);
2327 }
2328 else
2329 {
2330 /* Log folder is <Machines>/<VM_SnapshotFolder>/Logs */
2331 Assert (!mUserData->mSnapshotFolderFull.isEmpty());
2332 aLogFolder = Utf8StrFmt ("%ls%cLogs", mUserData->mSnapshotFolderFull.raw(),
2333 RTPATH_DELIMITER);
2334 }
2335}
2336
2337/**
2338 * @note Locks mParent and this object for writing,
2339 * calls the client process (outside the lock).
2340 */
2341HRESULT Machine::openSession (IInternalSessionControl *aControl)
2342{
2343 LogFlowThisFuncEnter();
2344
2345 AssertReturn (aControl, E_FAIL);
2346
2347 AutoCaller autoCaller (this);
2348 CheckComRCReturnRC (autoCaller.rc());
2349
2350 /* We need VirtualBox lock because of Progress::notifyComplete() */
2351 AutoMultiLock <2> alock (mParent->wlock(), this->wlock());
2352
2353 if (!mData->mRegistered)
2354 return setError (E_UNEXPECTED,
2355 tr ("The machine '%ls' is not registered"), mUserData->mName.raw());
2356
2357 LogFlowThisFunc (("mSession.mState=%d\n", mData->mSession.mState));
2358
2359 if (mData->mSession.mState == SessionState_SessionOpen ||
2360 mData->mSession.mState == SessionState_SessionClosing)
2361 return setError (E_ACCESSDENIED,
2362 tr ("A session for the machine '%ls' is currently open "
2363 "(or being closed)"),
2364 mUserData->mName.raw());
2365
2366 /* may not be Running */
2367 AssertReturn (mData->mMachineState < MachineState_Running, E_FAIL);
2368
2369 if (mData->mSession.mState == SessionState_SessionSpawning)
2370 {
2371 /*
2372 * this machine awaits for a spawning session to be opened,
2373 * so reject any other open attempts from processes other than
2374 * one started by #openRemoteSession().
2375 */
2376
2377 RTPROCESS pid = NIL_RTPROCESS; AssertCompile (sizeof (ULONG) == sizeof (RTPROCESS));
2378 aControl->GetPID ((ULONG *)&pid);
2379
2380 LogFlowThisFunc (("mSession.mPid=%d(0x%x)\n",
2381 mData->mSession.mPid, mData->mSession.mPid));
2382 LogFlowThisFunc (("session.pid=%d(0x%x)\n", pid, pid));
2383
2384 if (mData->mSession.mPid != pid)
2385 return setError (E_ACCESSDENIED,
2386 tr ("An unexpected process (PID=0x%08X) has tried to open a direct "
2387 "session with the machine named '%ls', while only a process "
2388 "started by OpenRemoteSession (PID=0x%08X) is allowed"),
2389 pid, mUserData->mName.raw(), mData->mSession.mPid);
2390 }
2391
2392 /* create a SessionMachine object */
2393 ComObjPtr <SessionMachine> sessionMachine;
2394 sessionMachine.createObject();
2395 HRESULT rc = sessionMachine->init (this);
2396 AssertComRC (rc);
2397
2398 if (SUCCEEDED (rc))
2399 {
2400 /*
2401 * Set the session state to Spawning to protect against subsequent
2402 * attempts to open a session and to unregister the machine after
2403 * we leave the lock.
2404 */
2405 SessionState_T origState = mData->mSession.mState;
2406 mData->mSession.mState = SessionState_SessionSpawning;
2407
2408 /*
2409 * Leave the lock before calling the client process -- it will call
2410 * Machine/SessionMachine methods. Leaving the lock here is quite safe
2411 * because the state is Spawning, so that openRemotesession() and
2412 * openExistingSession() calls will fail. This method, called before we
2413 * enter the lock again, will fail because of the wrong PID.
2414 *
2415 * Note that mData->mSession.mRemoteControls accessed outside
2416 * the lock may not be modified when state is Spawning, so it's safe.
2417 */
2418 alock.leave();
2419
2420 LogFlowThisFunc (("Calling AssignMachine()...\n"));
2421 rc = aControl->AssignMachine (sessionMachine);
2422 LogFlowThisFunc (("AssignMachine() returned %08X\n", rc));
2423
2424 /* The failure may w/o any error info (from RPC), so provide one */
2425 if (FAILED (rc))
2426 setError (rc,
2427 tr ("Failed to assign the machine to the session"));
2428
2429 if (SUCCEEDED (rc) && origState == SessionState_SessionSpawning)
2430 {
2431 /* complete the remote session initialization */
2432
2433 /* get the console from the direct session */
2434 ComPtr <IConsole> console;
2435 rc = aControl->GetRemoteConsole (console.asOutParam());
2436 ComAssertComRC (rc);
2437
2438 if (SUCCEEDED (rc) && !console)
2439 {
2440 ComAssert (!!console);
2441 rc = E_FAIL;
2442 }
2443
2444 /* assign machine & console to the remote sesion */
2445 if (SUCCEEDED (rc))
2446 {
2447 /*
2448 * after openRemoteSession(), the first and the only
2449 * entry in remoteControls is that remote session
2450 */
2451 LogFlowThisFunc (("Calling AssignRemoteMachine()...\n"));
2452 rc = mData->mSession.mRemoteControls.front()->
2453 AssignRemoteMachine (sessionMachine, console);
2454 LogFlowThisFunc (("AssignRemoteMachine() returned %08X\n", rc));
2455
2456 /* The failure may w/o any error info (from RPC), so provide one */
2457 if (FAILED (rc))
2458 setError (rc,
2459 tr ("Failed to assign the machine to the remote session"));
2460 }
2461
2462 if (FAILED (rc))
2463 aControl->Uninitialize();
2464 }
2465
2466 /* enter the lock again */
2467 alock.enter();
2468
2469 /* Restore the session state */
2470 mData->mSession.mState = origState;
2471 }
2472
2473 /* finalize spawning amyway (this is why we don't return on errors above) */
2474 if (mData->mSession.mState == SessionState_SessionSpawning)
2475 {
2476 /* Note that the progress object is finalized later */
2477
2478 /* We don't reset mSession.mPid here because it is necessary for
2479 * SessionMachine::uninit() to reap the child process later. */
2480
2481 if (FAILED (rc))
2482 {
2483 /* Remove the remote control from the list on failure
2484 * and reset session state to Closed. */
2485 mData->mSession.mRemoteControls.clear();
2486 mData->mSession.mState = SessionState_SessionClosed;
2487 }
2488 }
2489
2490 if (SUCCEEDED (rc))
2491 {
2492 /* memorize the direct session control */
2493 mData->mSession.mDirectControl = aControl;
2494 mData->mSession.mState = SessionState_SessionOpen;
2495 /* associate the SessionMachine with this Machine */
2496 mData->mSession.mMachine = sessionMachine;
2497 }
2498
2499 if (mData->mSession.mProgress)
2500 {
2501 /* finalize the progress after setting the state, for consistency */
2502 mData->mSession.mProgress->notifyComplete (rc);
2503 mData->mSession.mProgress.setNull();
2504 }
2505
2506 /* uninitialize the created session machine on failure */
2507 if (FAILED (rc))
2508 sessionMachine->uninit();
2509
2510 LogFlowThisFunc (("rc=%08X\n", rc));
2511 LogFlowThisFuncLeave();
2512 return rc;
2513}
2514
2515/**
2516 * @note Locks this object for writing, calls the client process
2517 * (inside the lock).
2518 */
2519HRESULT Machine::openRemoteSession (IInternalSessionControl *aControl,
2520 INPTR BSTR aType, Progress *aProgress)
2521{
2522 LogFlowThisFuncEnter();
2523
2524 AssertReturn (aControl, E_FAIL);
2525 AssertReturn (aProgress, E_FAIL);
2526
2527 AutoCaller autoCaller (this);
2528 CheckComRCReturnRC (autoCaller.rc());
2529
2530 AutoLock alock (this);
2531
2532 if (!mData->mRegistered)
2533 return setError (E_UNEXPECTED,
2534 tr ("The machine '%ls' is not registered"), mUserData->mName.raw());
2535
2536 LogFlowThisFunc (("mSession.mState=%d\n", mData->mSession.mState));
2537
2538 if (mData->mSession.mState == SessionState_SessionOpen ||
2539 mData->mSession.mState == SessionState_SessionSpawning ||
2540 mData->mSession.mState == SessionState_SessionClosing)
2541 return setError (E_ACCESSDENIED,
2542 tr ("A session for the machine '%ls' is currently open "
2543 "(or being opened or closed)"),
2544 mUserData->mName.raw());
2545
2546 /* may not be Running */
2547 AssertReturn (mData->mMachineState < MachineState_Running, E_FAIL);
2548
2549 /* get the path to the executable */
2550 char path [RTPATH_MAX];
2551 RTPathProgram (path, RTPATH_MAX);
2552 size_t sz = strlen (path);
2553 path [sz++] = RTPATH_DELIMITER;
2554 path [sz] = 0;
2555 char *cmd = path + sz;
2556 sz = RTPATH_MAX - sz;
2557
2558 int vrc = VINF_SUCCESS;
2559 RTPROCESS pid = NIL_RTPROCESS;
2560
2561 Bstr type (aType);
2562 if (type == "gui")
2563 {
2564 const char VirtualBox_exe[] = "VirtualBox" HOSTSUFF_EXE;
2565 Assert (sz >= sizeof (VirtualBox_exe));
2566 strcpy (cmd, VirtualBox_exe);
2567
2568 Utf8Str idStr = mData->mUuid.toString();
2569 const char * args[] = {path, "-startvm", idStr, 0 };
2570 vrc = RTProcCreate (path, args, NULL, 0, &pid);
2571 }
2572 else
2573#ifdef VBOX_VRDP
2574 if (type == "vrdp")
2575 {
2576 const char VBoxVRDP_exe[] = "VBoxVRDP" HOSTSUFF_EXE;
2577 Assert (sz >= sizeof (VBoxVRDP_exe));
2578 strcpy (cmd, VBoxVRDP_exe);
2579
2580 Utf8Str idStr = mData->mUuid.toString();
2581 const char * args[] = {path, "-startvm", idStr, 0 };
2582 vrc = RTProcCreate (path, args, NULL, 0, &pid);
2583 }
2584 else
2585#endif /* VBOX_VRDP */
2586 if (type == "capture")
2587 {
2588 const char VBoxVRDP_exe[] = "VBoxVRDP" HOSTSUFF_EXE;
2589 Assert (sz >= sizeof (VBoxVRDP_exe));
2590 strcpy (cmd, VBoxVRDP_exe);
2591
2592 Utf8Str idStr = mData->mUuid.toString();
2593 const char * args[] = {path, "-startvm", idStr, "-capture", 0 };
2594 vrc = RTProcCreate (path, args, NULL, 0, &pid);
2595 }
2596 else
2597 {
2598 return setError (E_INVALIDARG,
2599 tr ("Invalid session type: '%ls'"), aType);
2600 }
2601
2602 if (VBOX_FAILURE (vrc))
2603 return setError (E_FAIL,
2604 tr ("Could not launch a process for the machine '%ls' (%Vrc)"),
2605 mUserData->mName.raw(), vrc);
2606
2607 LogFlowThisFunc (("launched.pid=%d(0x%x)\n", pid, pid));
2608
2609 /*
2610 * Note that we don't leave the lock here before calling the client,
2611 * because it doesn't need to call us back if called with a NULL argument.
2612 * Leaving the lock herer is dangerous because we didn't prepare the
2613 * launch data yet, but the client we've just started may happen to be
2614 * too fast and call openSession() that will fail (because of PID, etc.),
2615 * so that the Machine will never get out of the Spawning session state.
2616 */
2617
2618 /* inform the session that it will be a remote one */
2619 LogFlowThisFunc (("Calling AssignMachine (NULL)...\n"));
2620 HRESULT rc = aControl->AssignMachine (NULL);
2621 LogFlowThisFunc (("AssignMachine (NULL) returned %08X\n", rc));
2622
2623 if (FAILED (rc))
2624 {
2625 /* restore the session state */
2626 mData->mSession.mState = SessionState_SessionClosed;
2627 /* The failure may w/o any error info (from RPC), so provide one */
2628 return setError (rc,
2629 tr ("Failed to assign the machine to the session"));
2630 }
2631
2632 /* attach launch data to the machine */
2633 Assert (mData->mSession.mPid == NIL_RTPROCESS);
2634 mData->mSession.mRemoteControls.push_back (aControl);
2635 mData->mSession.mProgress = aProgress;
2636 mData->mSession.mPid = pid;
2637 mData->mSession.mState = SessionState_SessionSpawning;
2638
2639 LogFlowThisFuncLeave();
2640 return S_OK;
2641}
2642
2643/**
2644 * @note Locks this object for writing, calls the client process
2645 * (outside the lock).
2646 */
2647HRESULT Machine::openExistingSession (IInternalSessionControl *aControl)
2648{
2649 LogFlowThisFuncEnter();
2650
2651 AssertReturn (aControl, E_FAIL);
2652
2653 AutoCaller autoCaller (this);
2654 CheckComRCReturnRC (autoCaller.rc());
2655
2656 AutoLock alock (this);
2657
2658 if (!mData->mRegistered)
2659 return setError (E_UNEXPECTED,
2660 tr ("The machine '%ls' is not registered"), mUserData->mName.raw());
2661
2662 LogFlowThisFunc (("mSession.state=%d\n", mData->mSession.mState));
2663
2664 if (mData->mSession.mState != SessionState_SessionOpen)
2665 return setError (E_ACCESSDENIED,
2666 tr ("The machine '%ls' does not have an open session"),
2667 mUserData->mName.raw());
2668
2669 ComAssertRet (!mData->mSession.mDirectControl.isNull(), E_FAIL);
2670
2671 /*
2672 * Get the console from the direct session (note that we don't leave the
2673 * lock here because GetRemoteConsole must not call us back).
2674 */
2675 ComPtr <IConsole> console;
2676 HRESULT rc = mData->mSession.mDirectControl->
2677 GetRemoteConsole (console.asOutParam());
2678 if (FAILED (rc))
2679 {
2680 /* The failure may w/o any error info (from RPC), so provide one */
2681 return setError (rc,
2682 tr ("Failed to get a console object from the direct session"));
2683 }
2684
2685 ComAssertRet (!console.isNull(), E_FAIL);
2686
2687 ComObjPtr <SessionMachine> sessionMachine = mData->mSession.mMachine;
2688 AssertReturn (!sessionMachine.isNull(), E_FAIL);
2689
2690 /*
2691 * Leave the lock before calling the client process. It's safe here
2692 * since the only thing to do after we get the lock again is to add
2693 * the remote control to the list (which doesn't directly influence
2694 * anything).
2695 */
2696 alock.leave();
2697
2698 /* attach the remote session to the machine */
2699 LogFlowThisFunc (("Calling AssignRemoteMachine()...\n"));
2700 rc = aControl->AssignRemoteMachine (sessionMachine, console);
2701 LogFlowThisFunc (("AssignRemoteMachine() returned %08X\n", rc));
2702
2703 /* The failure may w/o any error info (from RPC), so provide one */
2704 if (FAILED (rc))
2705 return setError (rc,
2706 tr ("Failed to assign the machine to the session"));
2707
2708 alock.enter();
2709
2710 /* need to revalidate the state after entering the lock again */
2711 if (mData->mSession.mState != SessionState_SessionOpen)
2712 {
2713 aControl->Uninitialize();
2714
2715 return setError (E_ACCESSDENIED,
2716 tr ("The machine '%ls' does not have an open session"),
2717 mUserData->mName.raw());
2718 }
2719
2720 /* store the control in the list */
2721 mData->mSession.mRemoteControls.push_back (aControl);
2722
2723 LogFlowThisFuncLeave();
2724 return S_OK;
2725}
2726
2727/**
2728 * Checks that the registered flag of the machine can be set according to
2729 * the argument and sets it. On success, commits and saves all settings.
2730 *
2731 * @note When this machine is inaccessible, the only valid value for \a
2732 * aRegistered is FALSE (i.e. unregister the machine) because unregistered
2733 * inaccessible machines are not currently supported. Note that unregistering
2734 * an inaccessible machine will \b uninitialize this machine object. Therefore,
2735 * the caller must make sure there are no active Machine::addCaller() calls
2736 * on the current thread because this will block Machine::uninit().
2737 *
2738 * @note Locks this object and children for writing!
2739 */
2740HRESULT Machine::trySetRegistered (BOOL aRegistered)
2741{
2742 AutoLimitedCaller autoCaller (this);
2743 AssertComRCReturnRC (autoCaller.rc());
2744
2745 AutoLock alock (this);
2746
2747 ComAssertRet (mData->mRegistered != aRegistered, E_FAIL);
2748
2749 if (!mData->mAccessible)
2750 {
2751 /* A special case: the machine is not accessible. */
2752
2753 /* inaccessible machines can only be unregistered */
2754 AssertReturn (!aRegistered, E_FAIL);
2755
2756 /* Uninitialize ourselves here because currently there may be no
2757 * unregistered that are inaccessible (this state combination is not
2758 * supported). Note releasing the caller and leaving the lock before
2759 * calling uninit() */
2760
2761 alock.leave();
2762 autoCaller.release();
2763
2764 uninit();
2765
2766 return S_OK;
2767 }
2768
2769 AssertReturn (autoCaller.state() == Ready, E_FAIL);
2770
2771 if (aRegistered)
2772 {
2773 if (mData->mRegistered)
2774 return setError (E_FAIL,
2775 tr ("The machine '%ls' with UUID {%s} is already registered"),
2776 mUserData->mName.raw(),
2777 mData->mUuid.toString().raw());
2778 }
2779 else
2780 {
2781 if (mData->mMachineState == MachineState_Saved)
2782 return setError (E_FAIL,
2783 tr ("Cannot unregister the machine '%ls' because it "
2784 "is in the Saved state"),
2785 mUserData->mName.raw());
2786
2787 size_t snapshotCount = 0;
2788 if (mData->mFirstSnapshot)
2789 snapshotCount = mData->mFirstSnapshot->descendantCount() + 1;
2790 if (snapshotCount)
2791 return setError (E_FAIL,
2792 tr ("Cannot unregister the machine '%ls' because it "
2793 "has %d snapshots"),
2794 mUserData->mName.raw(), snapshotCount);
2795
2796 if (mData->mSession.mState != SessionState_SessionClosed)
2797 return setError (E_FAIL,
2798 tr ("Cannot unregister the machine '%ls' because it has an "
2799 "open session"),
2800 mUserData->mName.raw());
2801
2802 if (mHDData->mHDAttachments.size() != 0)
2803 return setError (E_FAIL,
2804 tr ("Cannot unregister the machine '%ls' because it "
2805 "has %d hard disks attached"),
2806 mUserData->mName.raw(), mHDData->mHDAttachments.size());
2807 }
2808
2809 /* Ensure the settings are saved. If we are going to be registered and
2810 * isConfigLocked() is FALSE then it means that no config file exists yet,
2811 * so create it. */
2812 if (isModified() || (aRegistered && !isConfigLocked()))
2813 {
2814 HRESULT rc = saveSettings();
2815 CheckComRCReturnRC (rc);
2816 }
2817
2818 mData->mRegistered = aRegistered;
2819
2820 /* inform the USB proxy about all attached/detached USB filters */
2821 mUSBController->onMachineRegistered (aRegistered);
2822
2823 return S_OK;
2824}
2825
2826// protected methods
2827/////////////////////////////////////////////////////////////////////////////
2828
2829/**
2830 * Helper to uninitialize all associated child objects
2831 * and to free all data structures.
2832 *
2833 * This method must be called as a part of the object's uninitialization
2834 * procedure (usually done in the uninit() method).
2835 *
2836 * @note Must be called only from uninit().
2837 */
2838void Machine::uninitDataAndChildObjects()
2839{
2840 AutoCaller autoCaller (this);
2841 AssertComRCReturn (autoCaller.rc(), (void) 0);
2842 AssertComRCReturn (autoCaller.state( ) == InUninit, (void) 0);
2843
2844 /* tell all our child objects we've been uninitialized */
2845
2846 /*
2847 * uninit all children using addDependentChild()/removeDependentChild()
2848 * in their init()/uninit() methods
2849 */
2850 uninitDependentChildren();
2851
2852 for (ULONG slot = 0; slot < ELEMENTS (mNetworkAdapters); slot ++)
2853 {
2854 if (mNetworkAdapters [slot])
2855 {
2856 mNetworkAdapters [slot]->uninit();
2857 unconst (mNetworkAdapters [slot]).setNull();
2858 }
2859 }
2860
2861 if (mUSBController)
2862 {
2863 mUSBController->uninit();
2864 unconst (mUSBController).setNull();
2865 }
2866
2867 if (mAudioAdapter)
2868 {
2869 mAudioAdapter->uninit();
2870 unconst (mAudioAdapter).setNull();
2871 }
2872
2873 if (mFloppyDrive)
2874 {
2875 mFloppyDrive->uninit();
2876 unconst (mFloppyDrive).setNull();
2877 }
2878
2879 if (mDVDDrive)
2880 {
2881 mDVDDrive->uninit();
2882 unconst (mDVDDrive).setNull();
2883 }
2884
2885#ifdef VBOX_VRDP
2886 if (mVRDPServer)
2887 {
2888 mVRDPServer->uninit();
2889 unconst (mVRDPServer).setNull();
2890 }
2891#endif
2892
2893 if (mBIOSSettings)
2894 {
2895 mBIOSSettings->uninit();
2896 unconst (mBIOSSettings).setNull();
2897 }
2898
2899 /* free data structures */
2900 mSSData.free();
2901 mHDData.free();
2902 mHWData.free();
2903 mUserData.free();
2904 mData.free();
2905}
2906
2907/**
2908 * Helper to change the machine state.
2909 *
2910 * @note Locks this object for writing.
2911 */
2912HRESULT Machine::setMachineState (MachineState_T aMachineState)
2913{
2914 LogFlowThisFuncEnter();
2915 LogFlowThisFunc (("aMachineState=%d\n", aMachineState));
2916
2917 AutoCaller autoCaller (this);
2918 AssertComRCReturn (autoCaller.rc(), autoCaller.rc());
2919
2920 AutoLock alock (this);
2921
2922 if (mData->mMachineState != aMachineState)
2923 {
2924 mData->mMachineState = aMachineState;
2925
2926 RTTIMESPEC time;
2927 mData->mLastStateChange = RTTimeSpecGetMilli(RTTimeNow(&time));
2928
2929 mParent->onMachineStateChange (mData->mUuid, aMachineState);
2930 }
2931
2932 LogFlowThisFuncLeave();
2933 return S_OK;
2934}
2935
2936/**
2937 * Searches for a shared folder with the given logical name
2938 * in the collection of shared folders.
2939 *
2940 * @param aName logical name of the shared folder
2941 * @param aSharedFolder where to return the found object
2942 * @param aSetError whether to set the error info if the folder is
2943 * not found
2944 * @return
2945 * S_OK when found or E_INVALIDARG when not found
2946 *
2947 * @note
2948 * must be called from under the object's lock!
2949 */
2950HRESULT Machine::findSharedFolder (const BSTR aName,
2951 ComObjPtr <SharedFolder> &aSharedFolder,
2952 bool aSetError /* = false */)
2953{
2954 bool found = false;
2955 for (HWData::SharedFolderList::const_iterator it = mHWData->mSharedFolders.begin();
2956 !found && it != mHWData->mSharedFolders.end();
2957 ++ it)
2958 {
2959 AutoLock alock (*it);
2960 found = (*it)->name() == aName;
2961 if (found)
2962 aSharedFolder = *it;
2963 }
2964
2965 HRESULT rc = found ? S_OK : E_INVALIDARG;
2966
2967 if (aSetError && !found)
2968 setError (rc, tr ("Could not find a shared folder named '%ls'"), aName);
2969
2970 return rc;
2971}
2972
2973/**
2974 * Loads all the VM settings by walking down the <Machine> node.
2975 *
2976 * @param aRegistered true when the machine is being loaded on VirtualBox
2977 * startup
2978 *
2979 * @note This method is intended to be called only from init(), so it assumes
2980 * all machine data fields have appropriate default values when it is called.
2981 *
2982 * @note Doesn't lock any objects.
2983 */
2984HRESULT Machine::loadSettings (bool aRegistered)
2985{
2986 LogFlowThisFuncEnter();
2987 AssertReturn (mType == IsMachine, E_FAIL);
2988
2989 AutoCaller autoCaller (this);
2990 AssertReturn (autoCaller.state() == InInit, E_FAIL);
2991
2992 HRESULT rc = S_OK;
2993
2994 CFGHANDLE configLoader = NULL;
2995 char *loaderError = NULL;
2996 int vrc = CFGLDRLoad (&configLoader,
2997 Utf8Str (mData->mConfigFileFull), mData->mHandleCfgFile,
2998 XmlSchemaNS, true, cfgLdrEntityResolver,
2999 &loaderError);
3000 if (VBOX_FAILURE (vrc))
3001 {
3002 rc = setError (E_FAIL,
3003 tr ("Could not load the settings file '%ls' (%Vrc)%s%s"),
3004 mData->mConfigFileFull.raw(), vrc,
3005 loaderError ? ".\n" : "", loaderError ? loaderError : "");
3006
3007 if (loaderError)
3008 RTMemTmpFree (loaderError);
3009
3010 LogFlowThisFuncLeave();
3011 return rc;
3012 }
3013
3014 /*
3015 * When reading the XML, we assume it has been validated, so we don't
3016 * do any structural checks here, Just Assert() some things.
3017 */
3018
3019 CFGNODE machineNode = 0;
3020 CFGLDRGetNode (configLoader, "VirtualBox/Machine", 0, &machineNode);
3021
3022 do
3023 {
3024 ComAssertBreak (machineNode, rc = E_FAIL);
3025
3026 /* uuid (required) */
3027 Guid id;
3028 CFGLDRQueryUUID (machineNode, "uuid", id.ptr());
3029
3030 /* If the stored UUID is not empty, it means the registered machine
3031 * is being loaded. Compare the loaded UUID with the stored one taken
3032 * from the global registry. */
3033 if (!mData->mUuid.isEmpty())
3034 {
3035 if (mData->mUuid != id)
3036 {
3037 rc = setError (E_FAIL,
3038 tr ("Machine UUID {%Vuuid} in '%ls' doesn't match its "
3039 "UUID {%s} in the registry file '%ls'"),
3040 id.raw(), mData->mConfigFileFull.raw(),
3041 mData->mUuid.toString().raw(),
3042 mParent->settingsFileName().raw());
3043 break;
3044 }
3045 }
3046 else
3047 unconst (mData->mUuid) = id;
3048
3049 /* name (required) */
3050 CFGLDRQueryBSTR (machineNode, "name", mUserData->mName.asOutParam());
3051
3052 /* nameSync (optional, default is true) */
3053 {
3054 bool nameSync = true;
3055 CFGLDRQueryBool (machineNode, "nameSync", &nameSync);
3056 mUserData->mNameSync = nameSync;
3057 }
3058
3059 /* OSType (required) */
3060 {
3061 Bstr osTypeId;
3062 CFGLDRQueryBSTR (machineNode, "OSType", osTypeId.asOutParam());
3063
3064 /* look up the object in our list */
3065 ComPtr <IGuestOSType> guestOSType;
3066 rc = mParent->FindGuestOSType (osTypeId, guestOSType.asOutParam());
3067 if (FAILED (rc))
3068 break;
3069
3070 mUserData->mOSType = guestOSType;
3071 }
3072
3073 /* stateFile (optional) */
3074 {
3075 Bstr stateFilePath;
3076 CFGLDRQueryBSTR (machineNode, "stateFile", stateFilePath.asOutParam());
3077 if (stateFilePath)
3078 {
3079 Utf8Str stateFilePathFull = stateFilePath;
3080 int vrc = calculateFullPath (stateFilePathFull, stateFilePathFull);
3081 if (VBOX_FAILURE (vrc))
3082 {
3083 rc = setError (E_FAIL,
3084 tr ("Invalid saved state file path: '%ls' (%Vrc)"),
3085 stateFilePath.raw(), vrc);
3086 break;
3087 }
3088 mSSData->mStateFilePath = stateFilePathFull;
3089 }
3090 else
3091 mSSData->mStateFilePath.setNull();
3092 }
3093
3094 /*
3095 * currentSnapshot ID (optional)
3096 * Note that due to XML Schema constaraints this attribute, when present,
3097 * will guaranteedly refer to an existing snapshot definition in XML
3098 */
3099 Guid currentSnapshotId;
3100 CFGLDRQueryUUID (machineNode, "currentSnapshot", currentSnapshotId.ptr());
3101
3102 /* snapshotFolder (optional) */
3103 {
3104 Bstr folder;
3105 CFGLDRQueryBSTR (machineNode, "snapshotFolder", folder.asOutParam());
3106 rc = COMSETTER(SnapshotFolder) (folder);
3107 if (FAILED (rc))
3108 break;
3109 }
3110
3111 /* lastStateChange (optional, for compatiblity) */
3112 {
3113 int64_t lastStateChange = 0;
3114 CFGLDRQueryDateTime (machineNode, "lastStateChange", &lastStateChange);
3115 if (lastStateChange == 0)
3116 {
3117 /// @todo (dmik) until lastStateChange is the required attribute,
3118 // we simply set it to the current time if missing in the config
3119 RTTIMESPEC time;
3120 lastStateChange = RTTimeSpecGetMilli (RTTimeNow (&time));
3121 }
3122 mData->mLastStateChange = lastStateChange;
3123 }
3124
3125 /* aborted (optional) */
3126 bool aborted = false;
3127 CFGLDRQueryBool (machineNode, "aborted", &aborted);
3128
3129 /* currentStateModified (optional, default is true) */
3130 mData->mCurrentStateModified = TRUE;
3131 {
3132 bool val = true;
3133 CFGLDRQueryBool (machineNode, "currentStateModified", &val);
3134 mData->mCurrentStateModified = val;
3135 }
3136
3137 /*
3138 * note: all mUserData members must be assigned prior this point because
3139 * we need to commit changes in order to let mUserData be shared by all
3140 * snapshot machine instances.
3141 */
3142 mUserData.commitCopy();
3143
3144 /* Snapshot node (optional) */
3145 {
3146 CFGNODE snapshotNode = 0;
3147 CFGLDRGetChildNode (machineNode, "Snapshot", 0, &snapshotNode);
3148 if (snapshotNode)
3149 {
3150 /* read all snapshots recursively */
3151 rc = loadSnapshot (snapshotNode, currentSnapshotId, NULL);
3152 CFGLDRReleaseNode (snapshotNode);
3153 if (FAILED (rc))
3154 break;
3155 }
3156 }
3157
3158 /* Hardware node (required) */
3159 {
3160 CFGNODE hardwareNode = 0;
3161 CFGLDRGetChildNode (machineNode, "Hardware", 0, &hardwareNode);
3162 ComAssertBreak (hardwareNode, rc = E_FAIL);
3163 rc = loadHardware (hardwareNode);
3164 CFGLDRReleaseNode (hardwareNode);
3165 if (FAILED (rc))
3166 break;
3167 }
3168
3169 /* HardDiskAttachments node (required) */
3170 {
3171 CFGNODE hdasNode = 0;
3172 CFGLDRGetChildNode (machineNode, "HardDiskAttachments", 0, &hdasNode);
3173 ComAssertBreak (hdasNode, rc = E_FAIL);
3174
3175 rc = loadHardDisks (hdasNode, aRegistered);
3176 CFGLDRReleaseNode (hdasNode);
3177 if (FAILED (rc))
3178 break;
3179 }
3180
3181 /*
3182 * NOTE: the assignment below must be the last thing to do,
3183 * otherwise it will be not possible to change the settings
3184 * somewehere in the code above because all setters will be
3185 * blocked by CHECK_SETTER()
3186 */
3187
3188 /* set the machine state to Aborted or Saved when appropriate */
3189 if (aborted)
3190 {
3191 Assert (!mSSData->mStateFilePath);
3192 mSSData->mStateFilePath.setNull();
3193
3194 mData->mMachineState = MachineState_Aborted;
3195 }
3196 else if (mSSData->mStateFilePath)
3197 {
3198 mData->mMachineState = MachineState_Saved;
3199 }
3200 }
3201 while (0);
3202
3203 if (machineNode)
3204 CFGLDRReleaseNode (machineNode);
3205
3206 CFGLDRFree (configLoader);
3207
3208 LogFlowThisFuncLeave();
3209 return rc;
3210}
3211
3212/**
3213 * Recursively loads all snapshots starting from the given.
3214 *
3215 * @param aNode <Snapshot> node
3216 * @param aCurSnapshotId current snapshot ID from the settings file
3217 * @param aParentSnapshot parent snapshot
3218 */
3219HRESULT Machine::loadSnapshot (CFGNODE aNode, const Guid &aCurSnapshotId,
3220 Snapshot *aParentSnapshot)
3221{
3222 AssertReturn (aNode, E_INVALIDARG);
3223 AssertReturn (mType == IsMachine, E_FAIL);
3224
3225 // create a snapshot machine object
3226 ComObjPtr <SnapshotMachine> snapshotMachine;
3227 snapshotMachine.createObject();
3228
3229 HRESULT rc = S_OK;
3230
3231 Guid uuid; // required
3232 CFGLDRQueryUUID (aNode, "uuid", uuid.ptr());
3233
3234 Bstr stateFilePath; // optional
3235 CFGLDRQueryBSTR (aNode, "stateFile", stateFilePath.asOutParam());
3236 if (stateFilePath)
3237 {
3238 Utf8Str stateFilePathFull = stateFilePath;
3239 int vrc = calculateFullPath (stateFilePathFull, stateFilePathFull);
3240 if (VBOX_FAILURE (vrc))
3241 return setError (E_FAIL,
3242 tr ("Invalid saved state file path: '%ls' (%Vrc)"),
3243 stateFilePath.raw(), vrc);
3244
3245 stateFilePath = stateFilePathFull;
3246 }
3247
3248 do
3249 {
3250 // Hardware node (required)
3251 CFGNODE hardwareNode = 0;
3252 CFGLDRGetChildNode (aNode, "Hardware", 0, &hardwareNode);
3253 ComAssertBreak (hardwareNode, rc = E_FAIL);
3254
3255 do
3256 {
3257 // HardDiskAttachments node (required)
3258 CFGNODE hdasNode = 0;
3259 CFGLDRGetChildNode (aNode, "HardDiskAttachments", 0, &hdasNode);
3260 ComAssertBreak (hdasNode, rc = E_FAIL);
3261
3262 // initialize the snapshot machine
3263 rc = snapshotMachine->init (this, hardwareNode, hdasNode,
3264 uuid, stateFilePath);
3265
3266 CFGLDRReleaseNode (hdasNode);
3267 }
3268 while (0);
3269
3270 CFGLDRReleaseNode (hardwareNode);
3271 }
3272 while (0);
3273
3274 if (FAILED (rc))
3275 return rc;
3276
3277 // create a snapshot object
3278 ComObjPtr <Snapshot> snapshot;
3279 snapshot.createObject();
3280
3281 {
3282 Bstr name; // required
3283 CFGLDRQueryBSTR (aNode, "name", name.asOutParam());
3284
3285 LONG64 timeStamp = 0; // required
3286 CFGLDRQueryDateTime (aNode, "timeStamp", &timeStamp);
3287
3288 Bstr description; // optional
3289 {
3290 CFGNODE descNode = 0;
3291 CFGLDRGetChildNode (aNode, "Description", 0, &descNode);
3292 CFGLDRQueryBSTR (descNode, NULL, description.asOutParam());
3293 CFGLDRReleaseNode (descNode);
3294 }
3295
3296 // initialize the snapshot
3297 rc = snapshot->init (uuid, name, description, timeStamp,
3298 snapshotMachine, aParentSnapshot);
3299 if (FAILED (rc))
3300 return rc;
3301 }
3302
3303 // memorize the first snapshot if necessary
3304 if (!mData->mFirstSnapshot)
3305 mData->mFirstSnapshot = snapshot;
3306
3307 // memorize the current snapshot when appropriate
3308 if (!mData->mCurrentSnapshot && snapshot->data().mId == aCurSnapshotId)
3309 mData->mCurrentSnapshot = snapshot;
3310
3311 // Snapshots node (optional)
3312 {
3313 CFGNODE snapshotsNode = 0;
3314 CFGLDRGetChildNode (aNode, "Snapshots", 0, &snapshotsNode);
3315 if (snapshotsNode)
3316 {
3317 unsigned cbDisks = 0;
3318 CFGLDRCountChildren (snapshotsNode, "Snapshot", &cbDisks);
3319 for (unsigned i = 0; i < cbDisks && SUCCEEDED (rc); i++)
3320 {
3321 CFGNODE snapshotNode;
3322 CFGLDRGetChildNode (snapshotsNode, "Snapshot", i, &snapshotNode);
3323 ComAssertBreak (snapshotNode, rc = E_FAIL);
3324
3325 rc = loadSnapshot (snapshotNode, aCurSnapshotId, snapshot);
3326
3327 CFGLDRReleaseNode (snapshotNode);
3328 }
3329
3330 CFGLDRReleaseNode (snapshotsNode);
3331 }
3332 }
3333
3334 return rc;
3335}
3336
3337/**
3338 * @param aNode <Hardware> node
3339 */
3340HRESULT Machine::loadHardware (CFGNODE aNode)
3341{
3342 AssertReturn (aNode, E_INVALIDARG);
3343 AssertReturn (mType == IsMachine || mType == IsSnapshotMachine, E_FAIL);
3344
3345 /* CPU node (currently not required) */
3346 {
3347 /* default value in case the node is not there */
3348 mHWData->mHWVirtExEnabled = TriStateBool_Default;
3349
3350 CFGNODE cpuNode = 0;
3351 CFGLDRGetChildNode (aNode, "CPU", 0, &cpuNode);
3352 if (cpuNode)
3353 {
3354 CFGNODE hwVirtExNode = 0;
3355 CFGLDRGetChildNode (cpuNode, "HardwareVirtEx", 0, &hwVirtExNode);
3356 if (hwVirtExNode)
3357 {
3358 Bstr hwVirtExEnabled;
3359 CFGLDRQueryBSTR (hwVirtExNode, "enabled", hwVirtExEnabled.asOutParam());
3360 if (hwVirtExEnabled == L"false")
3361 mHWData->mHWVirtExEnabled = TriStateBool_False;
3362 else if (hwVirtExEnabled == L"true")
3363 mHWData->mHWVirtExEnabled = TriStateBool_True;
3364 else
3365 mHWData->mHWVirtExEnabled = TriStateBool_Default;
3366 CFGLDRReleaseNode (hwVirtExNode);
3367 }
3368 CFGLDRReleaseNode (cpuNode);
3369 }
3370 }
3371
3372 /* Memory node (required) */
3373 {
3374 CFGNODE memoryNode = 0;
3375 CFGLDRGetChildNode (aNode, "Memory", 0, &memoryNode);
3376 ComAssertRet (memoryNode, E_FAIL);
3377
3378 uint32_t RAMSize;
3379 CFGLDRQueryUInt32 (memoryNode, "RAMSize", &RAMSize);
3380 mHWData->mMemorySize = RAMSize;
3381 CFGLDRReleaseNode (memoryNode);
3382 }
3383
3384 /* Boot node (required) */
3385 {
3386 /* reset all boot order positions to NoDevice */
3387 for (size_t i = 0; i < ELEMENTS (mHWData->mBootOrder); i++)
3388 mHWData->mBootOrder [i] = DeviceType_NoDevice;
3389
3390 CFGNODE bootNode = 0;
3391 CFGLDRGetChildNode (aNode, "Boot", 0, &bootNode);
3392 ComAssertRet (bootNode, E_FAIL);
3393
3394 HRESULT rc = S_OK;
3395
3396 unsigned cOrder;
3397 CFGLDRCountChildren (bootNode, "Order", &cOrder);
3398 for (unsigned i = 0; i < cOrder; i++)
3399 {
3400 CFGNODE orderNode = 0;
3401 CFGLDRGetChildNode (bootNode, "Order", i, &orderNode);
3402 ComAssertBreak (orderNode, rc = E_FAIL);
3403
3404 /* position (required) */
3405 /* position unicity is guaranteed by XML Schema */
3406 uint32_t position = 0;
3407 CFGLDRQueryUInt32 (orderNode, "position", &position);
3408 -- position;
3409 Assert (position < ELEMENTS (mHWData->mBootOrder));
3410
3411 /* device (required) */
3412 Bstr device;
3413 CFGLDRQueryBSTR (orderNode, "device", device.asOutParam());
3414 if (device == L"None")
3415 mHWData->mBootOrder [position] = DeviceType_NoDevice;
3416 else if (device == L"Floppy")
3417 mHWData->mBootOrder [position] = DeviceType_FloppyDevice;
3418 else if (device == L"DVD")
3419 mHWData->mBootOrder [position] = DeviceType_DVDDevice;
3420 else if (device == L"HardDisk")
3421 mHWData->mBootOrder [position] = DeviceType_HardDiskDevice;
3422 else if (device == L"Network")
3423 mHWData->mBootOrder [position] = DeviceType_NetworkDevice;
3424 else
3425 ComAssertMsgFailed (("Invalid device: %ls\n", device.raw()));
3426
3427 CFGLDRReleaseNode (orderNode);
3428 }
3429
3430 CFGLDRReleaseNode (bootNode);
3431 if (FAILED (rc))
3432 return rc;
3433 }
3434
3435 /* Display node (required) */
3436 {
3437 CFGNODE displayNode = 0;
3438 CFGLDRGetChildNode (aNode, "Display", 0, &displayNode);
3439 ComAssertRet (displayNode, E_FAIL);
3440
3441 uint32_t VRAMSize;
3442 CFGLDRQueryUInt32 (displayNode, "VRAMSize", &VRAMSize);
3443 mHWData->mVRAMSize = VRAMSize;
3444 CFGLDRReleaseNode (displayNode);
3445 }
3446
3447#ifdef VBOX_VRDP
3448 /* RemoteDisplay node (optional) */
3449 /// @todo (dmik) move the code to VRDPServer
3450 /// @todo r=sunlover: moved. dmik, please review.
3451 {
3452 CFGNODE remoteDisplayNode = 0;
3453 CFGLDRGetChildNode (aNode, "RemoteDisplay", 0, &remoteDisplayNode);
3454 if (remoteDisplayNode)
3455 {
3456 mVRDPServer->loadConfig (remoteDisplayNode);
3457 CFGLDRReleaseNode (remoteDisplayNode);
3458 }
3459 }
3460#endif
3461
3462 /* BIOS node (required) */
3463 {
3464 CFGNODE biosNode = 0;
3465 CFGLDRGetChildNode (aNode, "BIOS", 0, &biosNode);
3466 ComAssertRet (biosNode, E_FAIL);
3467
3468 HRESULT rc = S_OK;
3469
3470 do
3471 {
3472 /* ACPI */
3473 {
3474 CFGNODE acpiNode = 0;
3475 CFGLDRGetChildNode (biosNode, "ACPI", 0, &acpiNode);
3476 ComAssertBreak (acpiNode, rc = E_FAIL);
3477
3478 bool enabled;
3479 CFGLDRQueryBool (acpiNode, "enabled", &enabled);
3480 mBIOSSettings->COMSETTER(ACPIEnabled)(enabled);
3481 CFGLDRReleaseNode (acpiNode);
3482 }
3483
3484 /* IOAPIC */
3485 {
3486 CFGNODE ioapicNode = 0;
3487 CFGLDRGetChildNode (biosNode, "IOAPIC", 0, &ioapicNode);
3488 if (ioapicNode)
3489 {
3490 bool enabled;
3491 CFGLDRQueryBool (ioapicNode, "enabled", &enabled);
3492 mBIOSSettings->COMSETTER(IOAPICEnabled)(enabled);
3493 CFGLDRReleaseNode (ioapicNode);
3494 }
3495 }
3496
3497 /* Logo (optional) */
3498 {
3499 CFGNODE logoNode = 0;
3500 CFGLDRGetChildNode (biosNode, "Logo", 0, &logoNode);
3501 if (logoNode)
3502 {
3503 bool enabled = false;
3504 CFGLDRQueryBool (logoNode, "fadeIn", &enabled);
3505 mBIOSSettings->COMSETTER(LogoFadeIn)(enabled);
3506 CFGLDRQueryBool (logoNode, "fadeOut", &enabled);
3507 mBIOSSettings->COMSETTER(LogoFadeOut)(enabled);
3508
3509 uint32_t BIOSLogoDisplayTime;
3510 CFGLDRQueryUInt32 (logoNode, "displayTime", &BIOSLogoDisplayTime);
3511 mBIOSSettings->COMSETTER(LogoDisplayTime)(BIOSLogoDisplayTime);
3512
3513 Bstr logoPath;
3514 CFGLDRQueryBSTR (logoNode, "imagePath", logoPath.asOutParam());
3515 mBIOSSettings->COMSETTER(LogoImagePath)(logoPath);
3516
3517 CFGLDRReleaseNode (logoNode);
3518 }
3519 }
3520
3521 /* boot menu (optional) */
3522 {
3523 CFGNODE bootMenuNode = 0;
3524 CFGLDRGetChildNode (biosNode, "BootMenu", 0, &bootMenuNode);
3525 if (bootMenuNode)
3526 {
3527 Bstr modeStr;
3528 BIOSBootMenuMode_T mode;
3529 CFGLDRQueryBSTR (bootMenuNode, "mode", modeStr.asOutParam());
3530 if (modeStr == L"disabled")
3531 mode = BIOSBootMenuMode_Disabled;
3532 else if (modeStr == L"menuonly")
3533 mode = BIOSBootMenuMode_MenuOnly;
3534 else
3535 mode = BIOSBootMenuMode_MessageAndMenu;
3536 mBIOSSettings->COMSETTER(BootMenuMode)(mode);
3537
3538 CFGLDRReleaseNode (bootMenuNode);
3539 }
3540 }
3541 }
3542 while (0);
3543
3544 CFGLDRReleaseNode (biosNode);
3545 if (FAILED (rc))
3546 return rc;
3547 }
3548
3549 /* DVD drive (contains either Image or HostDrive or nothing) */
3550 /// @todo (dmik) move the code to DVDDrive
3551 {
3552 HRESULT rc = S_OK;
3553
3554 CFGNODE dvdDriveNode = 0;
3555 CFGLDRGetChildNode (aNode, "DVDDrive", 0, &dvdDriveNode);
3556 ComAssertRet (dvdDriveNode, E_FAIL);
3557
3558 bool fPassthrough;
3559 CFGLDRQueryBool(dvdDriveNode, "passthrough", &fPassthrough);
3560 mDVDDrive->COMSETTER(Passthrough)(fPassthrough);
3561
3562 CFGNODE typeNode = 0;
3563
3564 do
3565 {
3566 CFGLDRGetChildNode (dvdDriveNode, "Image", 0, &typeNode);
3567 if (typeNode)
3568 {
3569 Guid uuid;
3570 CFGLDRQueryUUID (typeNode, "uuid", uuid.ptr());
3571 rc = mDVDDrive->MountImage (uuid);
3572 }
3573 else
3574 {
3575 CFGLDRGetChildNode (dvdDriveNode, "HostDrive", 0, &typeNode);
3576 if (typeNode)
3577 {
3578 Bstr src;
3579 CFGLDRQueryBSTR (typeNode, "src", src.asOutParam());
3580
3581 /* find the correspoding object */
3582 ComPtr <IHost> host;
3583 rc = mParent->COMGETTER(Host) (host.asOutParam());
3584 ComAssertComRCBreak (rc, rc = rc);
3585
3586 ComPtr <IHostDVDDriveCollection> coll;
3587 rc = host->COMGETTER(DVDDrives) (coll.asOutParam());
3588 ComAssertComRCBreak (rc, rc = rc);
3589
3590 ComPtr <IHostDVDDrive> drive;
3591 rc = coll->FindByName (src, drive.asOutParam());
3592 if (SUCCEEDED (rc))
3593 rc = mDVDDrive->CaptureHostDrive (drive);
3594 else if (rc == E_INVALIDARG)
3595 {
3596 /* the host DVD drive is not currently available. we
3597 * assume it will be available later and create an
3598 * extra object now */
3599 ComObjPtr <HostDVDDrive> hostDrive;
3600 hostDrive.createObject();
3601 rc = hostDrive->init (src);
3602 ComAssertComRCBreak (rc, rc = rc);
3603 rc = mDVDDrive->CaptureHostDrive (hostDrive);
3604 }
3605 else
3606 ComAssertComRCBreak (rc, rc = rc);
3607 }
3608 }
3609 }
3610 while (0);
3611
3612 if (typeNode)
3613 CFGLDRReleaseNode (typeNode);
3614 CFGLDRReleaseNode (dvdDriveNode);
3615
3616 if (FAILED (rc))
3617 return rc;
3618 }
3619
3620 /* Floppy drive (contains either Image or HostDrive or nothing) */
3621 /// @todo (dmik) move the code to FloppyDrive
3622 {
3623 HRESULT rc = S_OK;
3624
3625 CFGNODE driveNode = 0;
3626 CFGLDRGetChildNode (aNode, "FloppyDrive", 0, &driveNode);
3627 ComAssertRet (driveNode, E_FAIL);
3628
3629 BOOL fFloppyEnabled = TRUE;
3630 CFGLDRQueryBool (driveNode, "enabled", (bool*)&fFloppyEnabled);
3631 rc = mFloppyDrive->COMSETTER(Enabled)(fFloppyEnabled);
3632
3633 CFGNODE typeNode = 0;
3634 do
3635 {
3636 CFGLDRGetChildNode (driveNode, "Image", 0, &typeNode);
3637 if (typeNode)
3638 {
3639 Guid uuid;
3640 CFGLDRQueryUUID (typeNode, "uuid", uuid.ptr());
3641 rc = mFloppyDrive->MountImage (uuid);
3642 }
3643 else
3644 {
3645 CFGLDRGetChildNode (driveNode, "HostDrive", 0, &typeNode);
3646 if (typeNode)
3647 {
3648 Bstr src;
3649 CFGLDRQueryBSTR (typeNode, "src", src.asOutParam());
3650
3651 /* find the correspoding object */
3652 ComPtr <IHost> host;
3653 rc = mParent->COMGETTER(Host) (host.asOutParam());
3654 ComAssertComRCBreak (rc, rc = rc);
3655
3656 ComPtr <IHostFloppyDriveCollection> coll;
3657 rc = host->COMGETTER(FloppyDrives) (coll.asOutParam());
3658 ComAssertComRCBreak (rc, rc = rc);
3659
3660 ComPtr <IHostFloppyDrive> drive;
3661 rc = coll->FindByName (src, drive.asOutParam());
3662 if (SUCCEEDED (rc))
3663 rc = mFloppyDrive->CaptureHostDrive (drive);
3664 else if (rc == E_INVALIDARG)
3665 {
3666 /* the host Floppy drive is not currently available. we
3667 * assume it will be available later and create an
3668 * extra object now */
3669 ComObjPtr <HostFloppyDrive> hostDrive;
3670 hostDrive.createObject();
3671 rc = hostDrive->init (src);
3672 ComAssertComRCBreak (rc, rc = rc);
3673 rc = mFloppyDrive->CaptureHostDrive (hostDrive);
3674 }
3675 else
3676 ComAssertComRCBreak (rc, rc = rc);
3677 }
3678 }
3679 }
3680 while (0);
3681
3682 if (typeNode)
3683 CFGLDRReleaseNode (typeNode);
3684 CFGLDRReleaseNode (driveNode);
3685
3686 if (FAILED (rc))
3687 return rc;
3688 }
3689
3690 /* USB Controller */
3691 {
3692 HRESULT rc = mUSBController->loadSettings (aNode);
3693 if (FAILED (rc))
3694 return rc;
3695 }
3696
3697 /* Network node (required) */
3698 /// @todo (dmik) move the code to NetworkAdapter
3699 {
3700 /* we assume that all network adapters are initially disabled
3701 * and detached */
3702
3703 CFGNODE networkNode = 0;
3704 CFGLDRGetChildNode (aNode, "Network", 0, &networkNode);
3705 ComAssertRet (networkNode, E_FAIL);
3706
3707 HRESULT rc = S_OK;
3708
3709 unsigned cAdapters = 0;
3710 CFGLDRCountChildren (networkNode, "Adapter", &cAdapters);
3711 for (unsigned i = 0; i < cAdapters; i++)
3712 {
3713 CFGNODE adapterNode = 0;
3714 CFGLDRGetChildNode (networkNode, "Adapter", i, &adapterNode);
3715 ComAssertBreak (adapterNode, rc = E_FAIL);
3716
3717 /* slot number (required) */
3718 /* slot unicity is guaranteed by XML Schema */
3719 uint32_t slot = 0;
3720 CFGLDRQueryUInt32 (adapterNode, "slot", &slot);
3721 Assert (slot < ELEMENTS (mNetworkAdapters));
3722
3723 /* type */
3724 Bstr adapterType;
3725 CFGLDRQueryBSTR (adapterNode, "type", adapterType.asOutParam());
3726 ComAssertBreak (adapterType, rc = E_FAIL);
3727
3728 /* enabled (required) */
3729 bool enabled = false;
3730 CFGLDRQueryBool (adapterNode, "enabled", &enabled);
3731 /* MAC address (can be null) */
3732 Bstr macAddr;
3733 CFGLDRQueryBSTR (adapterNode, "MACAddress", macAddr.asOutParam());
3734 /* cable (required) */
3735 bool cableConnected;
3736 CFGLDRQueryBool (adapterNode, "cable", &cableConnected);
3737 /* tracing (defaults to false) */
3738 bool traceEnabled;
3739 CFGLDRQueryBool (adapterNode, "trace", &traceEnabled);
3740 Bstr traceFile;
3741 CFGLDRQueryBSTR (adapterNode, "tracefile", traceFile.asOutParam());
3742
3743 mNetworkAdapters [slot]->COMSETTER(Enabled) (enabled);
3744 mNetworkAdapters [slot]->COMSETTER(MACAddress) (macAddr);
3745 mNetworkAdapters [slot]->COMSETTER(CableConnected) (cableConnected);
3746 mNetworkAdapters [slot]->COMSETTER(TraceEnabled) (traceEnabled);
3747 mNetworkAdapters [slot]->COMSETTER(TraceFile) (traceFile);
3748
3749 if (adapterType.compare(Bstr("Am79C970A")) == 0)
3750 mNetworkAdapters [slot]->COMSETTER(AdapterType)(NetworkAdapterType_NetworkAdapterAm79C970A);
3751 else if (adapterType.compare(Bstr("Am79C973")) == 0)
3752 mNetworkAdapters [slot]->COMSETTER(AdapterType)(NetworkAdapterType_NetworkAdapterAm79C973);
3753 else
3754 ComAssertBreak (0, rc = E_FAIL);
3755
3756 CFGNODE attachmentNode = 0;
3757 if (CFGLDRGetChildNode (adapterNode, "NAT", 0, &attachmentNode), attachmentNode)
3758 {
3759 mNetworkAdapters [slot]->AttachToNAT();
3760 }
3761 else
3762 if (CFGLDRGetChildNode (adapterNode, "HostInterface", 0, &attachmentNode), attachmentNode)
3763 {
3764 /* Host Interface Networking */
3765 Bstr name;
3766 CFGLDRQueryBSTR (attachmentNode, "name", name.asOutParam());
3767#ifdef __WIN__
3768 /* @name can be empty on Win32, but not null */
3769 ComAssertBreak (!name.isNull(), rc = E_FAIL);
3770#endif
3771 mNetworkAdapters [slot]->COMSETTER(HostInterface) (name);
3772#ifdef VBOX_WITH_UNIXY_TAP_NETWORKING
3773 Bstr tapSetupApp;
3774 CFGLDRQueryBSTR (attachmentNode, "TAPSetup", tapSetupApp.asOutParam());
3775 Bstr tapTerminateApp;
3776 CFGLDRQueryBSTR (attachmentNode, "TAPTerminate", tapTerminateApp.asOutParam());
3777
3778 mNetworkAdapters [slot]->COMSETTER(TAPSetupApplication) (tapSetupApp);
3779 mNetworkAdapters [slot]->COMSETTER(TAPTerminateApplication) (tapTerminateApp);
3780#endif // VBOX_WITH_UNIXY_TAP_NETWORKING
3781 mNetworkAdapters [slot]->AttachToHostInterface();
3782 }
3783 else
3784 if (CFGLDRGetChildNode(adapterNode, "InternalNetwork", 0, &attachmentNode), attachmentNode)
3785 {
3786 /* Internal Networking */
3787 Bstr name;
3788 CFGLDRQueryBSTR (attachmentNode, "name", name.asOutParam());
3789 ComAssertBreak (!name.isNull(), rc = E_FAIL);
3790 mNetworkAdapters[slot]->AttachToInternalNetwork();
3791 mNetworkAdapters[slot]->COMSETTER(InternalNetwork) (name);
3792 }
3793 else
3794 {
3795 /* Adapter has no children */
3796 mNetworkAdapters [slot]->Detach();
3797 }
3798 if (attachmentNode)
3799 CFGLDRReleaseNode (attachmentNode);
3800
3801 CFGLDRReleaseNode (adapterNode);
3802 }
3803
3804 CFGLDRReleaseNode (networkNode);
3805 if (FAILED (rc))
3806 return rc;
3807 }
3808
3809 /* AudioAdapter node (required) */
3810 /// @todo (dmik) move the code to AudioAdapter
3811 {
3812 CFGNODE audioAdapterNode = 0;
3813 CFGLDRGetChildNode (aNode, "AudioAdapter", 0, &audioAdapterNode);
3814 ComAssertRet (audioAdapterNode, E_FAIL);
3815
3816 // is the adapter enabled?
3817 bool enabled = false;
3818 CFGLDRQueryBool (audioAdapterNode, "enabled", &enabled);
3819 mAudioAdapter->COMSETTER(Enabled) (enabled);
3820 // now check the audio driver
3821 Bstr driver;
3822 CFGLDRQueryBSTR (audioAdapterNode, "driver", driver.asOutParam());
3823 AudioDriverType_T audioDriver;
3824 audioDriver = AudioDriverType_NullAudioDriver;
3825 if (driver == L"null")
3826 ; // Null has been set above
3827#ifdef __WIN__
3828 else if (driver == L"winmm")
3829 audioDriver = AudioDriverType_WINMMAudioDriver;
3830 else if (driver == L"dsound")
3831 audioDriver = AudioDriverType_DSOUNDAudioDriver;
3832#endif // __WIN__
3833#ifdef __LINUX__
3834 else if (driver == L"oss")
3835 audioDriver = AudioDriverType_OSSAudioDriver;
3836 else if (driver == L"alsa")
3837#ifdef VBOX_WITH_ALSA
3838 audioDriver = AudioDriverType_ALSAAudioDriver;
3839#else
3840 // fall back to OSS
3841 audioDriver = AudioDriverType_OSSAudioDriver;
3842#endif
3843#endif // __LINUX__
3844 else
3845 AssertMsgFailed (("Invalid driver: %ls\n", driver.raw()));
3846 mAudioAdapter->COMSETTER(AudioDriver) (audioDriver);
3847
3848 CFGLDRReleaseNode (audioAdapterNode);
3849 }
3850
3851 /* Shared folders (optional) */
3852 /// @todo (dmik) make required on next format change!
3853 do
3854 {
3855 CFGNODE sharedFoldersNode = 0;
3856 CFGLDRGetChildNode (aNode, "SharedFolders", 0, &sharedFoldersNode);
3857
3858 if (!sharedFoldersNode)
3859 break;
3860
3861 HRESULT rc = S_OK;
3862
3863 unsigned cFolders = 0;
3864 CFGLDRCountChildren (sharedFoldersNode, "SharedFolder", &cFolders);
3865
3866 for (unsigned i = 0; i < cFolders; i++)
3867 {
3868 CFGNODE folderNode = 0;
3869 CFGLDRGetChildNode (sharedFoldersNode, "SharedFolder", i, &folderNode);
3870 ComAssertBreak (folderNode, rc = E_FAIL);
3871
3872 // folder logical name (required)
3873 Bstr name;
3874 CFGLDRQueryBSTR (folderNode, "name", name.asOutParam());
3875
3876 // folder host path (required)
3877 Bstr hostPath;
3878 CFGLDRQueryBSTR (folderNode, "hostPath", hostPath.asOutParam());
3879
3880 rc = CreateSharedFolder (name, hostPath);
3881 if (FAILED (rc))
3882 break;
3883
3884 CFGLDRReleaseNode (folderNode);
3885 }
3886
3887 CFGLDRReleaseNode (sharedFoldersNode);
3888 if (FAILED (rc))
3889 return rc;
3890 }
3891 while (0);
3892
3893 /* Clipboard node (currently not required) */
3894 /// @todo (dmik) make required on next format change!
3895 {
3896 /* default value in case the node is not there */
3897 mHWData->mClipboardMode = ClipboardMode_ClipDisabled;
3898
3899 CFGNODE clipNode = 0;
3900 CFGLDRGetChildNode (aNode, "Clipboard", 0, &clipNode);
3901 if (clipNode)
3902 {
3903 Bstr mode;
3904 CFGLDRQueryBSTR (clipNode, "mode", mode.asOutParam());
3905 if (mode == L"Disabled")
3906 mHWData->mClipboardMode = ClipboardMode_ClipDisabled;
3907 else if (mode == L"HostToGuest")
3908 mHWData->mClipboardMode = ClipboardMode_ClipHostToGuest;
3909 else if (mode == L"GuestToHost")
3910 mHWData->mClipboardMode = ClipboardMode_ClipGuestToHost;
3911 else if (mode == L"Bidirectional")
3912 mHWData->mClipboardMode = ClipboardMode_ClipBidirectional;
3913 else
3914 AssertMsgFailed (("%ls clipboard mode is invalid\n", mode.raw()));
3915 CFGLDRReleaseNode (clipNode);
3916 }
3917 }
3918
3919 return S_OK;
3920}
3921
3922/**
3923 * @param aNode <HardDiskAttachments> node
3924 * @param aRegistered true when the machine is being loaded on VirtualBox
3925 * startup, or when a snapshot is being loaded (wchich
3926 * currently can happen on startup only)
3927 * @param aSnapshotId pointer to the snapshot ID if this is a snapshot machine
3928 */
3929HRESULT Machine::loadHardDisks (CFGNODE aNode, bool aRegistered,
3930 const Guid *aSnapshotId /* = NULL */)
3931{
3932 AssertReturn (aNode, E_INVALIDARG);
3933 AssertReturn ((mType == IsMachine && aSnapshotId == NULL) ||
3934 (mType == IsSnapshotMachine && aSnapshotId != NULL), E_FAIL);
3935
3936 HRESULT rc = S_OK;
3937
3938 unsigned cbDisks = 0;
3939 CFGLDRCountChildren (aNode, "HardDiskAttachment", &cbDisks);
3940
3941 if (!aRegistered && cbDisks > 0)
3942 {
3943 /* when the machine is being loaded (opened) from a file, it cannot
3944 * have hard disks attached (this should not happen normally,
3945 * because we don't allow to attach hard disks to an unregistered
3946 * VM at all */
3947 return setError (E_FAIL,
3948 tr ("Unregistered machine '%ls' cannot have hard disks attached "
3949 "(found %d hard disk attachments)"),
3950 mUserData->mName.raw(), cbDisks);
3951 }
3952
3953 for (unsigned i = 0; i < cbDisks && SUCCEEDED (rc); ++ i)
3954 {
3955 CFGNODE hdNode;
3956 CFGLDRGetChildNode (aNode, "HardDiskAttachment", i, &hdNode);
3957 ComAssertRet (hdNode, E_FAIL);
3958
3959 do
3960 {
3961 /* hardDisk uuid (required) */
3962 Guid uuid;
3963 CFGLDRQueryUUID (hdNode, "hardDisk", uuid.ptr());
3964 /* bus (controller) type (required) */
3965 Bstr bus;
3966 CFGLDRQueryBSTR (hdNode, "bus", bus.asOutParam());
3967 /* device (required) */
3968 Bstr device;
3969 CFGLDRQueryBSTR (hdNode, "device", device.asOutParam());
3970
3971 /* find a hard disk by UUID */
3972 ComObjPtr <HardDisk> hd;
3973 rc = mParent->getHardDisk (uuid, hd);
3974 if (FAILED (rc))
3975 break;
3976
3977 AutoLock hdLock (hd);
3978
3979 if (!hd->machineId().isEmpty())
3980 {
3981 rc = setError (E_FAIL,
3982 tr ("Hard disk '%ls' with UUID {%s} is already "
3983 "attached to a machine with UUID {%s} (see '%ls')"),
3984 hd->toString().raw(), uuid.toString().raw(),
3985 hd->machineId().toString().raw(),
3986 mData->mConfigFileFull.raw());
3987 break;
3988 }
3989
3990 if (hd->type() == HardDiskType_ImmutableHardDisk)
3991 {
3992 rc = setError (E_FAIL,
3993 tr ("Immutable hard disk '%ls' with UUID {%s} cannot be "
3994 "directly attached to a machine (see '%ls')"),
3995 hd->toString().raw(), uuid.toString().raw(),
3996 mData->mConfigFileFull.raw());
3997 break;
3998 }
3999
4000 /* attach the device */
4001 DiskControllerType_T ctl = DiskControllerType_InvalidController;
4002 LONG dev = -1;
4003
4004 if (bus == L"ide0")
4005 {
4006 ctl = DiskControllerType_IDE0Controller;
4007 if (device == L"master")
4008 dev = 0;
4009 else if (device == L"slave")
4010 dev = 1;
4011 else
4012 ComAssertMsgFailedBreak (("Invalid device: %ls\n", device.raw()),
4013 rc = E_FAIL);
4014 }
4015 else if (bus == L"ide1")
4016 {
4017 ctl = DiskControllerType_IDE1Controller;
4018 if (device == L"master")
4019 rc = setError (E_FAIL, tr("Could not attach a disk as a master "
4020 "device on the secondary controller"));
4021 else if (device == L"slave")
4022 dev = 1;
4023 else
4024 ComAssertMsgFailedBreak (("Invalid device: %ls\n", device.raw()),
4025 rc = E_FAIL);
4026 }
4027 else
4028 ComAssertMsgFailedBreak (("Invalid bus: %ls\n", bus.raw()),
4029 rc = E_FAIL);
4030
4031 ComObjPtr <HardDiskAttachment> attachment;
4032 attachment.createObject();
4033 rc = attachment->init (hd, ctl, dev, false /* aDirty */);
4034 if (FAILED (rc))
4035 break;
4036
4037 /* associate the hard disk with this machine */
4038 hd->setMachineId (mData->mUuid);
4039
4040 /* associate the hard disk with the given snapshot ID */
4041 if (mType == IsSnapshotMachine)
4042 hd->setSnapshotId (*aSnapshotId);
4043
4044 mHDData->mHDAttachments.push_back (attachment);
4045 }
4046 while (0);
4047
4048 CFGLDRReleaseNode (hdNode);
4049 }
4050
4051 return rc;
4052}
4053
4054/**
4055 * Creates a config loader and loads the settings file.
4056 *
4057 * @param aIsNew |true| if a newly created settings file is to be opened
4058 * (must be the case only when called from #saveSettings())
4059 *
4060 * @note
4061 * XML Schema errors are not detected by this method because
4062 * it assumes that it will load settings from an exclusively locked
4063 * file (using a file handle) that was previously validated when opened
4064 * for the first time. Thus, this method should be used only when
4065 * it's necessary to modify (save) the settings file.
4066 *
4067 * @note The object must be locked at least for reading before calling
4068 * this method.
4069 */
4070HRESULT Machine::openConfigLoader (CFGHANDLE *aLoader, bool aIsNew /* = false */)
4071{
4072 AssertReturn (aLoader, E_FAIL);
4073
4074 /* The settings file must be created and locked at this point */
4075 ComAssertRet (isConfigLocked(), E_FAIL);
4076
4077 /* load the config file */
4078 int vrc = CFGLDRLoad (aLoader,
4079 Utf8Str (mData->mConfigFileFull), mData->mHandleCfgFile,
4080 aIsNew ? NULL : XmlSchemaNS, true, cfgLdrEntityResolver,
4081 NULL);
4082 ComAssertRCRet (vrc, E_FAIL);
4083
4084 return S_OK;
4085}
4086
4087/**
4088 * Closes the config loader previously created by #openConfigLoader().
4089 * If \a aSaveBeforeClose is true, then the config is saved to the settings file
4090 * before closing. If saving fails, a proper error message is set.
4091 *
4092 * @param aSaveBeforeClose whether to save the config before closing or not
4093 */
4094HRESULT Machine::closeConfigLoader (CFGHANDLE aLoader, bool aSaveBeforeClose)
4095{
4096 HRESULT rc = S_OK;
4097
4098 if (aSaveBeforeClose)
4099 {
4100 char *loaderError = NULL;
4101 int vrc = CFGLDRSave (aLoader, &loaderError);
4102 if (VBOX_FAILURE (vrc))
4103 {
4104 rc = setError (E_FAIL,
4105 tr ("Could not save the settings file '%ls' (%Vrc)%s%s"),
4106 mData->mConfigFileFull.raw(), vrc,
4107 loaderError ? ".\n" : "", loaderError ? loaderError : "");
4108 if (loaderError)
4109 RTMemTmpFree (loaderError);
4110 }
4111 }
4112
4113 CFGLDRFree (aLoader);
4114
4115 return rc;
4116}
4117
4118/**
4119 * Searches for a <Snapshot> node for the given snapshot.
4120 * If the search is successful, \a aSnapshotNode will contain the found node.
4121 * In this case, \a aSnapshotsNode can be NULL meaning the found node is a
4122 * direct child of \a aMachineNode.
4123 *
4124 * If the search fails, a failure is returned and both \a aSnapshotsNode and
4125 * \a aSnapshotNode are set to 0.
4126 *
4127 * @param aSnapshot snapshot to search for
4128 * @param aMachineNode <Machine> node to start from
4129 * @param aSnapshotsNode <Snapshots> node containing the found <Snapshot> node
4130 * (may be NULL if the caller is not interested)
4131 * @param aSnapshotNode found <Snapshot> node
4132 */
4133HRESULT Machine::findSnapshotNode (Snapshot *aSnapshot, CFGNODE aMachineNode,
4134 CFGNODE *aSnapshotsNode, CFGNODE *aSnapshotNode)
4135{
4136 AssertReturn (aSnapshot && aMachineNode && aSnapshotNode, E_FAIL);
4137
4138 if (aSnapshotsNode)
4139 *aSnapshotsNode = 0;
4140 *aSnapshotNode = 0;
4141
4142 // build the full uuid path (from the fist parent to the given snapshot)
4143 std::list <Guid> path;
4144 {
4145 ComObjPtr <Snapshot> parent = aSnapshot;
4146 while (parent)
4147 {
4148 path.push_front (parent->data().mId);
4149 parent = parent->parent();
4150 }
4151 }
4152
4153 CFGNODE snapshotsNode = aMachineNode;
4154 CFGNODE snapshotNode = 0;
4155
4156 for (std::list <Guid>::const_iterator it = path.begin();
4157 it != path.end();
4158 ++ it)
4159 {
4160 if (snapshotNode)
4161 {
4162 // proceed to the nested <Snapshots> node
4163 Assert (snapshotsNode);
4164 if (snapshotsNode != aMachineNode)
4165 {
4166 CFGLDRReleaseNode (snapshotsNode);
4167 snapshotsNode = 0;
4168 }
4169 CFGLDRGetChildNode (snapshotNode, "Snapshots", 0, &snapshotsNode);
4170 CFGLDRReleaseNode (snapshotNode);
4171 snapshotNode = 0;
4172 }
4173
4174 AssertReturn (snapshotsNode, E_FAIL);
4175
4176 unsigned count = 0, i = 0;
4177 CFGLDRCountChildren (snapshotsNode, "Snapshot", &count);
4178 for (; i < count; ++ i)
4179 {
4180 snapshotNode = 0;
4181 CFGLDRGetChildNode (snapshotsNode, "Snapshot", i, &snapshotNode);
4182 Guid id;
4183 CFGLDRQueryUUID (snapshotNode, "uuid", id.ptr());
4184 if (id == (*it))
4185 {
4186 // we keep (don't release) snapshotNode and snapshotsNode
4187 break;
4188 }
4189 CFGLDRReleaseNode (snapshotNode);
4190 snapshotNode = 0;
4191 }
4192
4193 if (i == count)
4194 {
4195 // the next uuid is not found, no need to continue...
4196 AssertFailed();
4197 if (snapshotsNode != aMachineNode)
4198 {
4199 CFGLDRReleaseNode (snapshotsNode);
4200 snapshotsNode = 0;
4201 }
4202 break;
4203 }
4204 }
4205
4206 // we must always succesfully find the node
4207 AssertReturn (snapshotNode, E_FAIL);
4208 AssertReturn (snapshotsNode, E_FAIL);
4209
4210 if (aSnapshotsNode)
4211 *aSnapshotsNode = snapshotsNode != aMachineNode ? snapshotsNode : 0;
4212 *aSnapshotNode = snapshotNode;
4213
4214 return S_OK;
4215}
4216
4217/**
4218 * Returns the snapshot with the given UUID or fails of no such snapshot.
4219 *
4220 * @param aId snapshot UUID to find (empty UUID refers the first snapshot)
4221 * @param aSnapshot where to return the found snapshot
4222 * @param aSetError true to set extended error info on failure
4223 */
4224HRESULT Machine::findSnapshot (const Guid &aId, ComObjPtr <Snapshot> &aSnapshot,
4225 bool aSetError /* = false */)
4226{
4227 if (!mData->mFirstSnapshot)
4228 {
4229 if (aSetError)
4230 return setError (E_FAIL,
4231 tr ("This machine does not have any snapshots"));
4232 return E_FAIL;
4233 }
4234
4235 if (aId.isEmpty())
4236 aSnapshot = mData->mFirstSnapshot;
4237 else
4238 aSnapshot = mData->mFirstSnapshot->findChildOrSelf (aId);
4239
4240 if (!aSnapshot)
4241 {
4242 if (aSetError)
4243 return setError (E_FAIL,
4244 tr ("Could not find a snapshot with UUID {%s}"),
4245 aId.toString().raw());
4246 return E_FAIL;
4247 }
4248
4249 return S_OK;
4250}
4251
4252/**
4253 * Returns the snapshot with the given name or fails of no such snapshot.
4254 *
4255 * @param aName snapshot name to find
4256 * @param aSnapshot where to return the found snapshot
4257 * @param aSetError true to set extended error info on failure
4258 */
4259HRESULT Machine::findSnapshot (const BSTR aName, ComObjPtr <Snapshot> &aSnapshot,
4260 bool aSetError /* = false */)
4261{
4262 AssertReturn (aName, E_INVALIDARG);
4263
4264 if (!mData->mFirstSnapshot)
4265 {
4266 if (aSetError)
4267 return setError (E_FAIL,
4268 tr ("This machine does not have any snapshots"));
4269 return E_FAIL;
4270 }
4271
4272 aSnapshot = mData->mFirstSnapshot->findChildOrSelf (aName);
4273
4274 if (!aSnapshot)
4275 {
4276 if (aSetError)
4277 return setError (E_FAIL,
4278 tr ("Could not find a snapshot named '%ls'"), aName);
4279 return E_FAIL;
4280 }
4281
4282 return S_OK;
4283}
4284
4285/**
4286 * Searches for an attachment that contains the given hard disk.
4287 * The hard disk must be associated with some VM and can be optionally
4288 * associated with some snapshot. If the attachment is stored in the snapshot
4289 * (i.e. the hard disk is associated with some snapshot), @a aSnapshot
4290 * will point to a non-null object on output.
4291 *
4292 * @param aHd hard disk to search an attachment for
4293 * @param aMachine where to store the hard disk's machine (can be NULL)
4294 * @param aSnapshot where to store the hard disk's snapshot (can be NULL)
4295 * @param aHda where to store the hard disk's attachment (can be NULL)
4296 *
4297 *
4298 * @note
4299 * It is assumed that the machine where the attachment is found,
4300 * is already placed to the Discarding state, when this method is called.
4301 * @note
4302 * The object returned in @a aHda is the attachment from the snapshot
4303 * machine if the hard disk is associated with the snapshot, not from the
4304 * primary machine object returned returned in @a aMachine.
4305 */
4306HRESULT Machine::findHardDiskAttachment (const ComObjPtr <HardDisk> &aHd,
4307 ComObjPtr <Machine> *aMachine,
4308 ComObjPtr <Snapshot> *aSnapshot,
4309 ComObjPtr <HardDiskAttachment> *aHda)
4310{
4311 AssertReturn (!aHd.isNull(), E_INVALIDARG);
4312
4313 Guid mid = aHd->machineId();
4314 Guid sid = aHd->snapshotId();
4315
4316 AssertReturn (!mid.isEmpty(), E_INVALIDARG);
4317
4318 ComObjPtr <Machine> m;
4319 mParent->getMachine (mid, m);
4320 ComAssertRet (!m.isNull(), E_FAIL);
4321
4322 HDData::HDAttachmentList *attachments = &m->mHDData->mHDAttachments;
4323
4324 ComObjPtr <Snapshot> s;
4325 if (!sid.isEmpty())
4326 {
4327 m->findSnapshot (sid, s);
4328 ComAssertRet (!s.isNull(), E_FAIL);
4329 attachments = &s->data().mMachine->mHDData->mHDAttachments;
4330 }
4331
4332 AssertReturn (attachments, E_FAIL);
4333
4334 for (HDData::HDAttachmentList::const_iterator it = attachments->begin();
4335 it != attachments->end();
4336 ++ it)
4337 {
4338 if ((*it)->hardDisk() == aHd)
4339 {
4340 if (aMachine) *aMachine = m;
4341 if (aSnapshot) *aSnapshot = s;
4342 if (aHda) *aHda = (*it);
4343 return S_OK;
4344 }
4345 }
4346
4347 ComAssertFailed();
4348 return E_FAIL;
4349}
4350
4351/**
4352 * Helper for #saveSettings. Cares about renaming the settings directory and
4353 * file if the machine name was changed and about creating a new settings file
4354 * if this is a new machine.
4355 *
4356 * @note Must be never called directly.
4357 *
4358 * @param aRenamed receives |true| if the name was changed and the settings
4359 * file was renamed as a result, or |false| otherwise. The
4360 * value makes sense only on success.
4361 * @param aNew receives |true| if a virgin settings file was created.
4362 */
4363HRESULT Machine::prepareSaveSettings (bool &aRenamed, bool &aNew)
4364{
4365 HRESULT rc = S_OK;
4366
4367 aRenamed = false;
4368
4369 /* if we're ready and isConfigLocked() is FALSE then it means
4370 * that no config file exists yet (we will create a virgin one) */
4371 aNew = !isConfigLocked();
4372
4373 /* attempt to rename the settings file if machine name is changed */
4374 if (mUserData->mNameSync &&
4375 mUserData.isBackedUp() &&
4376 mUserData.backedUpData()->mName != mUserData->mName)
4377 {
4378 aRenamed = true;
4379
4380 if (!aNew)
4381 {
4382 /* unlock the old config file */
4383 rc = unlockConfig();
4384 CheckComRCReturnRC (rc);
4385 }
4386
4387 bool dirRenamed = false;
4388 bool fileRenamed = false;
4389
4390 Utf8Str configFile, newConfigFile;
4391 Utf8Str configDir, newConfigDir;
4392
4393 do
4394 {
4395 int vrc = VINF_SUCCESS;
4396
4397 Utf8Str name = mUserData.backedUpData()->mName;
4398 Utf8Str newName = mUserData->mName;
4399
4400 configFile = mData->mConfigFileFull;
4401
4402 /* first, rename the directory if it matches the machine name */
4403 configDir = configFile;
4404 RTPathStripFilename (configDir.mutableRaw());
4405 newConfigDir = configDir;
4406 if (RTPathFilename (configDir) == name)
4407 {
4408 RTPathStripFilename (newConfigDir.mutableRaw());
4409 newConfigDir = Utf8StrFmt ("%s%c%s",
4410 newConfigDir.raw(), RTPATH_DELIMITER, newName.raw());
4411 /* new dir and old dir cannot be equal here because of 'if'
4412 * above and because name != newName */
4413 Assert (configDir != newConfigDir);
4414 if (!aNew)
4415 {
4416 /* perform real rename only if the machine is not new */
4417 vrc = RTPathRename (configDir.raw(), newConfigDir.raw(), 0);
4418 if (VBOX_FAILURE (vrc))
4419 {
4420 rc = setError (E_FAIL,
4421 tr ("Could not rename the directory '%s' to '%s' "
4422 "to save the settings file (%Vrc)"),
4423 configDir.raw(), newConfigDir.raw(), vrc);
4424 break;
4425 }
4426 dirRenamed = true;
4427 }
4428 }
4429
4430 newConfigFile = Utf8StrFmt ("%s%c%s.xml",
4431 newConfigDir.raw(), RTPATH_DELIMITER, newName.raw());
4432
4433 /* then try to rename the settings file itself */
4434 if (newConfigFile != configFile)
4435 {
4436 /* get the path to old settings file in renamed directory */
4437 configFile = Utf8StrFmt ("%s%c%s",
4438 newConfigDir.raw(), RTPATH_DELIMITER,
4439 RTPathFilename (configFile));
4440 if (!aNew)
4441 {
4442 /* perform real rename only if the machine is not new */
4443 vrc = RTFileRename (configFile.raw(), newConfigFile.raw(), 0);
4444 if (VBOX_FAILURE (vrc))
4445 {
4446 rc = setError (E_FAIL,
4447 tr ("Could not rename the settings file '%s' to '%s' "
4448 "(%Vrc)"),
4449 configFile.raw(), newConfigFile.raw(), vrc);
4450 break;
4451 }
4452 fileRenamed = true;
4453 }
4454 }
4455
4456 /* update mConfigFileFull amd mConfigFile */
4457 Bstr oldConfigFileFull = mData->mConfigFileFull;
4458 Bstr oldConfigFile = mData->mConfigFile;
4459 mData->mConfigFileFull = newConfigFile;
4460 /* try to get the relative path for mConfigFile */
4461 Utf8Str path = newConfigFile;
4462 mParent->calculateRelativePath (path, path);
4463 mData->mConfigFile = path;
4464
4465 /* last, try to update the global settings with the new path */
4466 if (mData->mRegistered)
4467 {
4468 rc = mParent->updateSettings (configDir, newConfigDir);
4469 if (FAILED (rc))
4470 {
4471 /* revert to old values */
4472 mData->mConfigFileFull = oldConfigFileFull;
4473 mData->mConfigFile = oldConfigFile;
4474 break;
4475 }
4476 }
4477
4478 /* update the snapshot folder */
4479 path = mUserData->mSnapshotFolderFull;
4480 if (RTPathStartsWith (path, configDir))
4481 {
4482 path = Utf8StrFmt ("%s%s", newConfigDir.raw(),
4483 path.raw() + configDir.length());
4484 mUserData->mSnapshotFolderFull = path;
4485 calculateRelativePath (path, path);
4486 mUserData->mSnapshotFolder = path;
4487 }
4488
4489 /* update the saved state file path */
4490 path = mSSData->mStateFilePath;
4491 if (RTPathStartsWith (path, configDir))
4492 {
4493 path = Utf8StrFmt ("%s%s", newConfigDir.raw(),
4494 path.raw() + configDir.length());
4495 mSSData->mStateFilePath = path;
4496 }
4497
4498 /* Update saved state file paths of all online snapshots.
4499 * Note that saveSettings() will recognize name change
4500 * and will save all snapshots in this case. */
4501 if (mData->mFirstSnapshot)
4502 mData->mFirstSnapshot->updateSavedStatePaths (configDir,
4503 newConfigDir);
4504 }
4505 while (0);
4506
4507 if (FAILED (rc))
4508 {
4509 /* silently try to rename everything back */
4510 if (fileRenamed)
4511 RTFileRename (newConfigFile.raw(), configFile.raw(), 0);
4512 if (dirRenamed)
4513 RTPathRename (newConfigDir.raw(), configDir.raw(), 0);
4514 }
4515
4516 if (!aNew)
4517 {
4518 /* lock the config again */
4519 HRESULT rc2 = lockConfig();
4520 if (SUCCEEDED (rc))
4521 rc = rc2;
4522 }
4523
4524 CheckComRCReturnRC (rc);
4525 }
4526
4527 if (aNew)
4528 {
4529 /* create a virgin config file */
4530 int vrc = VINF_SUCCESS;
4531
4532 /* ensure the settings directory exists */
4533 Utf8Str path = mData->mConfigFileFull;
4534 RTPathStripFilename (path.mutableRaw());
4535 if (!RTDirExists (path))
4536 {
4537 vrc = RTDirCreateFullPath (path, 0777);
4538 if (VBOX_FAILURE (vrc))
4539 {
4540 return setError (E_FAIL,
4541 tr ("Could not create a directory '%s' "
4542 "to save the settings file (%Vrc)"),
4543 path.raw(), vrc);
4544 }
4545 }
4546
4547 /* Note: open flags must correlated with RTFileOpen() in lockConfig() */
4548 path = Utf8Str (mData->mConfigFileFull);
4549 vrc = RTFileOpen (&mData->mHandleCfgFile, path,
4550 RTFILE_O_READWRITE | RTFILE_O_CREATE |
4551 RTFILE_O_DENY_WRITE);
4552 if (VBOX_SUCCESS (vrc))
4553 {
4554 vrc = RTFileWrite (mData->mHandleCfgFile,
4555 (void *) DefaultMachineConfig,
4556 sizeof (DefaultMachineConfig), NULL);
4557 }
4558 if (VBOX_FAILURE (vrc))
4559 {
4560 mData->mHandleCfgFile = NIL_RTFILE;
4561 return setError (E_FAIL,
4562 tr ("Could not create the settings file '%s' (%Vrc)"),
4563 path.raw(), vrc);
4564 }
4565 /* we do not close the file to simulate lockConfig() */
4566 }
4567
4568 return rc;
4569}
4570
4571/**
4572 * Saves machine data, user data and hardware data.
4573 *
4574 * @param aMarkCurStateAsModified
4575 * if true (default), mData->mCurrentStateModified will be set to
4576 * what #isReallyModified() returns prior to saving settings to a file,
4577 * otherwise the current value of mData->mCurrentStateModified will be
4578 * saved.
4579 * @param aInformCallbacksAnyway
4580 * if true, callbacks will be informed even if #isReallyModified()
4581 * returns false. This is necessary for cases when we change machine data
4582 * diectly, not through the backup()/commit() mechanism.
4583 *
4584 * @note Locks mParent (only in some cases, and only when #isConfigLocked() is
4585 * |TRUE|, see the #prepareSaveSettings() code for details) +
4586 * this object + children for writing.
4587 */
4588HRESULT Machine::saveSettings (bool aMarkCurStateAsModified /* = true */,
4589 bool aInformCallbacksAnyway /* = false */)
4590{
4591 /// @todo (dmik) I guess we should lock all our child objects here
4592 // (such as mVRDPServer etc.) to ensure they are not changed
4593 // until completely saved to disk and committed
4594
4595 /// @todo (dmik) also, we need to delegate saving child objects' settings
4596 // to objects themselves to ensure operations 'commit + save changes'
4597 // are atomic (amd done from the object's lock so that nobody can change
4598 // settings again until completely saved).
4599
4600 AssertReturn (mType == IsMachine || mType == IsSessionMachine, E_FAIL);
4601
4602 bool wasModified;
4603
4604 if (aMarkCurStateAsModified)
4605 {
4606 /*
4607 * We ignore changes to user data when setting mCurrentStateModified
4608 * because the current state will not differ from the current snapshot
4609 * if only user data has been changed (user data is shared by all
4610 * snapshots).
4611 */
4612 mData->mCurrentStateModified = isReallyModified (true /* aIgnoreUserData */);
4613 wasModified = mUserData.hasActualChanges() || mData->mCurrentStateModified;
4614 }
4615 else
4616 {
4617 wasModified = isReallyModified();
4618 }
4619
4620 HRESULT rc = S_OK;
4621
4622 /* First, prepare to save settings. It will will care about renaming the
4623 * settings directory and file if the machine name was changed and about
4624 * creating a new settings file if this is a new machine. */
4625 bool isRenamed = false;
4626 bool isNew = false;
4627 rc = prepareSaveSettings (isRenamed, isNew);
4628 CheckComRCReturnRC (rc);
4629
4630 /* then, open the settings file */
4631 CFGHANDLE configLoader = 0;
4632 rc = openConfigLoader (&configLoader, isNew);
4633 CheckComRCReturnRC (rc);
4634
4635 /* save all snapshots when the machine name was changed since
4636 * it may affect saved state file paths for online snapshots (see
4637 * #openConfigLoader() for details) */
4638 bool updateAllSnapshots = isRenamed;
4639
4640 /* commit before saving, since it may change settings
4641 * (for example, perform fixup of lazy hard disk changes) */
4642 rc = commit();
4643 if (FAILED (rc))
4644 {
4645 closeConfigLoader (configLoader, false /* aSaveBeforeClose */);
4646 return rc;
4647 }
4648
4649 /* include hard disk changes to the modified flag */
4650 wasModified |= mHDData->mHDAttachmentsChanged;
4651 if (aMarkCurStateAsModified)
4652 mData->mCurrentStateModified |= BOOL (mHDData->mHDAttachmentsChanged);
4653
4654
4655 CFGNODE machineNode = 0;
4656 /* create if not exists */
4657 CFGLDRCreateNode (configLoader, "VirtualBox/Machine", &machineNode);
4658
4659 do
4660 {
4661 ComAssertBreak (machineNode, rc = E_FAIL);
4662
4663 /* uuid (required) */
4664 Assert (mData->mUuid);
4665 CFGLDRSetUUID (machineNode, "uuid", mData->mUuid.raw());
4666
4667 /* name (required) */
4668 Assert (!mUserData->mName.isEmpty());
4669 CFGLDRSetBSTR (machineNode, "name", mUserData->mName);
4670
4671 /* nameSync (optional, default is true) */
4672 if (!mUserData->mNameSync)
4673 CFGLDRSetBool (machineNode, "nameSync", false);
4674 else
4675 CFGLDRDeleteAttribute (machineNode, "nameSync");
4676
4677 /* OSType (required) */
4678 {
4679 Bstr osTypeID;
4680 rc = mUserData->mOSType->COMGETTER(Id) (osTypeID.asOutParam());
4681 ComAssertComRCBreak (rc, rc = rc);
4682 Assert (!osTypeID.isNull());
4683 CFGLDRSetBSTR (machineNode, "OSType", osTypeID);
4684 }
4685
4686 /* stateFile (optional) */
4687 if (mData->mMachineState == MachineState_Saved)
4688 {
4689 Assert (!mSSData->mStateFilePath.isEmpty());
4690 /* try to make the file name relative to the settings file dir */
4691 Utf8Str stateFilePath = mSSData->mStateFilePath;
4692 calculateRelativePath (stateFilePath, stateFilePath);
4693 CFGLDRSetString (machineNode, "stateFile", stateFilePath);
4694 }
4695 else
4696 {
4697 Assert (mSSData->mStateFilePath.isNull());
4698 CFGLDRDeleteAttribute (machineNode, "stateFile");
4699 }
4700
4701 /* currentSnapshot ID (optional) */
4702 if (!mData->mCurrentSnapshot.isNull())
4703 {
4704 Assert (!mData->mFirstSnapshot.isNull());
4705 CFGLDRSetUUID (machineNode, "currentSnapshot",
4706 mData->mCurrentSnapshot->data().mId);
4707 }
4708 else
4709 {
4710 Assert (mData->mFirstSnapshot.isNull());
4711 CFGLDRDeleteAttribute (machineNode, "currentSnapshot");
4712 }
4713
4714 /* snapshotFolder (optional) */
4715 if (mUserData->mSnapshotFolder)
4716 CFGLDRSetBSTR (machineNode, "snapshotFolder", mUserData->mSnapshotFolder);
4717 else
4718 CFGLDRDeleteAttribute (machineNode, "snapshotFolder");
4719
4720 /* currentStateModified (optional, default is yes) */
4721 if (!mData->mCurrentStateModified)
4722 CFGLDRSetBool (machineNode, "currentStateModified", false);
4723 else
4724 CFGLDRDeleteAttribute (machineNode, "currentStateModified");
4725
4726 /* lastStateChange */
4727 CFGLDRSetDateTime (machineNode, "lastStateChange",
4728 mData->mLastStateChange);
4729
4730 /* Hardware node (required) */
4731 {
4732 CFGNODE hwNode = 0;
4733 CFGLDRGetChildNode (machineNode, "Hardware", 0, &hwNode);
4734 /* first, delete the entire node if exists */
4735 if (hwNode)
4736 CFGLDRDeleteNode (hwNode);
4737 /* then recreate it */
4738 hwNode = 0;
4739 CFGLDRCreateChildNode (machineNode, "Hardware", &hwNode);
4740 ComAssertBreak (hwNode, rc = E_FAIL);
4741
4742 rc = saveHardware (hwNode);
4743
4744 CFGLDRReleaseNode (hwNode);
4745 if (FAILED (rc))
4746 break;
4747 }
4748
4749 /* HardDiskAttachments node (required) */
4750 {
4751 CFGNODE hdasNode = 0;
4752 CFGLDRGetChildNode (machineNode, "HardDiskAttachments", 0, &hdasNode);
4753 /* first, delete the entire node if exists */
4754 if (hdasNode)
4755 CFGLDRDeleteNode (hdasNode);
4756 /* then recreate it */
4757 hdasNode = 0;
4758 CFGLDRCreateChildNode (machineNode, "HardDiskAttachments", &hdasNode);
4759 ComAssertBreak (hdasNode, rc = E_FAIL);
4760
4761 rc = saveHardDisks (hdasNode);
4762
4763 CFGLDRReleaseNode (hdasNode);
4764 if (FAILED (rc))
4765 break;
4766 }
4767
4768 /* update all snapshots if requested */
4769 if (updateAllSnapshots)
4770 rc = saveSnapshotSettingsWorker (machineNode, NULL,
4771 SaveSS_UpdateAllOp);
4772 }
4773 while (0);
4774
4775 if (machineNode)
4776 CFGLDRReleaseNode (machineNode);
4777
4778 if (SUCCEEDED (rc))
4779 rc = closeConfigLoader (configLoader, true /* aSaveBeforeClose */);
4780 else
4781 closeConfigLoader (configLoader, false /* aSaveBeforeClose */);
4782
4783 if (FAILED (rc))
4784 {
4785 /*
4786 * backup arbitrary data item to cause #isModified() to still return
4787 * true in case of any error
4788 */
4789 mHWData.backup();
4790 }
4791
4792 if (wasModified || aInformCallbacksAnyway)
4793 {
4794 /*
4795 * Fire the data change event, even on failure (since we've already
4796 * committed all data). This is done only for SessionMachines because
4797 * mutable Machine instances are always not registered (i.e. private
4798 * to the client process that creates them) and thus don't need to
4799 * inform callbacks.
4800 */
4801 if (mType == IsSessionMachine)
4802 mParent->onMachineDataChange (mData->mUuid);
4803 }
4804
4805 return rc;
4806}
4807
4808/**
4809 * Wrapper for #saveSnapshotSettingsWorker() that opens the settings file
4810 * and locates the <Machine> node in there. See #saveSnapshotSettingsWorker()
4811 * for more details.
4812 *
4813 * @param aSnapshot Snapshot to operate on
4814 * @param aOpFlags Operation to perform, one of SaveSS_NoOp, SaveSS_AddOp
4815 * or SaveSS_UpdateAttrsOp possibly combined with
4816 * SaveSS_UpdateCurrentId.
4817 *
4818 * @note Locks this object for writing + other child objects.
4819 */
4820HRESULT Machine::saveSnapshotSettings (Snapshot *aSnapshot, int aOpFlags)
4821{
4822 AutoCaller autoCaller (this);
4823 AssertComRCReturn (autoCaller.rc(), autoCaller.rc());
4824
4825 AssertReturn (mType == IsMachine || mType == IsSessionMachine, E_FAIL);
4826
4827 AutoLock alock (this);
4828
4829 AssertReturn (isConfigLocked(), E_FAIL);
4830
4831 HRESULT rc = S_OK;
4832
4833 /* load the config file */
4834 CFGHANDLE configLoader = 0;
4835 rc = openConfigLoader (&configLoader);
4836 if (FAILED (rc))
4837 return rc;
4838
4839 CFGNODE machineNode = 0;
4840 CFGLDRGetNode (configLoader, "VirtualBox/Machine", 0, &machineNode);
4841
4842 do
4843 {
4844 ComAssertBreak (machineNode, rc = E_FAIL);
4845
4846 rc = saveSnapshotSettingsWorker (machineNode, aSnapshot, aOpFlags);
4847
4848 CFGLDRReleaseNode (machineNode);
4849 }
4850 while (0);
4851
4852 if (SUCCEEDED (rc))
4853 rc = closeConfigLoader (configLoader, true /* aSaveBeforeClose */);
4854 else
4855 closeConfigLoader (configLoader, false /* aSaveBeforeClose */);
4856
4857 return rc;
4858}
4859
4860/**
4861 * Performs the specified operation on the given snapshot
4862 * in the settings file represented by \a aMachineNode.
4863 *
4864 * If \a aOpFlags = SaveSS_UpdateAllOp, \a aSnapshot can be NULL to indicate
4865 * that the whole tree of the snapshots should be updated in <Machine>.
4866 * One particular case is when the last (and the only) snapshot should be
4867 * removed (it is so when both mCurrentSnapshot and mFirstSnapshot are NULL).
4868 *
4869 * \a aOp may be just SaveSS_UpdateCurrentId if only the currentSnapshot
4870 * attribute of <Machine> needs to be updated.
4871 *
4872 * @param aMachineNode <Machine> node in the opened settings file
4873 * @param aSnapshot Snapshot to operate on
4874 * @param aOpFlags Operation to perform, one of SaveSS_NoOp, SaveSS_AddOp
4875 * or SaveSS_UpdateAttrsOp possibly combined with
4876 * SaveSS_UpdateCurrentId.
4877 *
4878 * @note Must be called with this object locked for writing.
4879 * Locks child objects.
4880 */
4881HRESULT Machine::saveSnapshotSettingsWorker (CFGNODE aMachineNode,
4882 Snapshot *aSnapshot, int aOpFlags)
4883{
4884 AssertReturn (aMachineNode, E_FAIL);
4885
4886 AssertReturn (isLockedOnCurrentThread(), E_FAIL);
4887
4888 int op = aOpFlags & SaveSS_OpMask;
4889 AssertReturn (
4890 (aSnapshot && (op == SaveSS_AddOp || op == SaveSS_UpdateAttrsOp ||
4891 op == SaveSS_UpdateAllOp)) ||
4892 (!aSnapshot && ((op == SaveSS_NoOp && (aOpFlags & SaveSS_UpdateCurrentId)) ||
4893 op == SaveSS_UpdateAllOp)),
4894 E_FAIL);
4895
4896 HRESULT rc = S_OK;
4897
4898 bool recreateWholeTree = false;
4899
4900 do
4901 {
4902 if (op == SaveSS_NoOp)
4903 break;
4904
4905 /* quick path: recreate the whole tree of the snapshots */
4906 if (op == SaveSS_UpdateAllOp && !aSnapshot)
4907 {
4908 /* first, delete the entire root snapshot node if it exists */
4909 CFGNODE snapshotNode = 0;
4910 CFGLDRGetChildNode (aMachineNode, "Snapshot", 0, &snapshotNode);
4911 if (snapshotNode)
4912 CFGLDRDeleteNode (snapshotNode);
4913
4914 /*
4915 * second, if we have any snapshots left, substitute aSnapshot with
4916 * the first snapshot to recreate the whole tree, otherwise break
4917 */
4918 if (mData->mFirstSnapshot)
4919 {
4920 aSnapshot = mData->mFirstSnapshot;
4921 recreateWholeTree = true;
4922 }
4923 else
4924 break;
4925 }
4926
4927 Assert (!!aSnapshot);
4928 ComObjPtr <Snapshot> parent = aSnapshot->parent();
4929
4930 if (op == SaveSS_AddOp)
4931 {
4932 CFGNODE parentNode = 0;
4933
4934 if (parent)
4935 {
4936 rc = findSnapshotNode (parent, aMachineNode, NULL, &parentNode);
4937 if (FAILED (rc))
4938 break;
4939 ComAssertBreak (parentNode, rc = E_FAIL);
4940 }
4941
4942 do
4943 {
4944 CFGNODE snapshotsNode = 0;
4945
4946 if (parentNode)
4947 {
4948 CFGLDRCreateChildNode (parentNode, "Snapshots", &snapshotsNode);
4949 ComAssertBreak (snapshotsNode, rc = E_FAIL);
4950 }
4951 else
4952 snapshotsNode = aMachineNode;
4953 do
4954 {
4955 CFGNODE snapshotNode = 0;
4956 CFGLDRAppendChildNode (snapshotsNode, "Snapshot", &snapshotNode);
4957 ComAssertBreak (snapshotNode, rc = E_FAIL);
4958 rc = saveSnapshot (snapshotNode, aSnapshot, false /* aAttrsOnly */);
4959 CFGLDRReleaseNode (snapshotNode);
4960
4961 if (FAILED (rc))
4962 break;
4963
4964 /*
4965 * when a new snapshot is added, this means diffs were created
4966 * for every normal/immutable hard disk of the VM, so we need to
4967 * save the current hard disk attachments
4968 */
4969
4970 CFGNODE hdasNode = 0;
4971 CFGLDRGetChildNode (aMachineNode, "HardDiskAttachments", 0, &hdasNode);
4972 if (hdasNode)
4973 CFGLDRDeleteNode (hdasNode);
4974 CFGLDRCreateChildNode (aMachineNode, "HardDiskAttachments", &hdasNode);
4975 ComAssertBreak (hdasNode, rc = E_FAIL);
4976
4977 rc = saveHardDisks (hdasNode);
4978
4979 if (mHDData->mHDAttachments.size() != 0)
4980 {
4981 /*
4982 * If we have one or more attachments then we definitely
4983 * created diffs for them and associated new diffs with
4984 * current settngs. So, since we don't use saveSettings(),
4985 * we need to inform callbacks manually.
4986 */
4987 if (mType == IsSessionMachine)
4988 mParent->onMachineDataChange (mData->mUuid);
4989 }
4990
4991 CFGLDRReleaseNode (hdasNode);
4992 }
4993 while (0);
4994
4995 if (snapshotsNode != aMachineNode)
4996 CFGLDRReleaseNode (snapshotsNode);
4997 }
4998 while (0);
4999
5000 if (parentNode)
5001 CFGLDRReleaseNode (parentNode);
5002
5003 break;
5004 }
5005
5006 Assert (op == SaveSS_UpdateAttrsOp && !recreateWholeTree ||
5007 op == SaveSS_UpdateAllOp);
5008
5009 CFGNODE snapshotsNode = 0;
5010 CFGNODE snapshotNode = 0;
5011
5012 if (!recreateWholeTree)
5013 {
5014 rc = findSnapshotNode (aSnapshot, aMachineNode,
5015 &snapshotsNode, &snapshotNode);
5016 if (FAILED (rc))
5017 break;
5018 ComAssertBreak (snapshotNode, rc = E_FAIL);
5019 }
5020
5021 if (!snapshotsNode)
5022 snapshotsNode = aMachineNode;
5023
5024 if (op == SaveSS_UpdateAttrsOp)
5025 rc = saveSnapshot (snapshotNode, aSnapshot, true /* aAttrsOnly */);
5026 else do
5027 {
5028 if (snapshotNode)
5029 {
5030 CFGLDRDeleteNode (snapshotNode);
5031 snapshotNode = 0;
5032 }
5033 CFGLDRAppendChildNode (snapshotsNode, "Snapshot", &snapshotNode);
5034 ComAssertBreak (snapshotNode, rc = E_FAIL);
5035 rc = saveSnapshot (snapshotNode, aSnapshot, false /* aAttrsOnly */);
5036 }
5037 while (0);
5038
5039 CFGLDRReleaseNode (snapshotNode);
5040 if (snapshotsNode != aMachineNode)
5041 CFGLDRReleaseNode (snapshotsNode);
5042 }
5043 while (0);
5044
5045 if (SUCCEEDED (rc))
5046 {
5047 /* update currentSnapshot when appropriate */
5048 if (aOpFlags & SaveSS_UpdateCurrentId)
5049 {
5050 if (!mData->mCurrentSnapshot.isNull())
5051 CFGLDRSetUUID (aMachineNode, "currentSnapshot",
5052 mData->mCurrentSnapshot->data().mId);
5053 else
5054 CFGLDRDeleteAttribute (aMachineNode, "currentSnapshot");
5055 }
5056 if (aOpFlags & SaveSS_UpdateCurStateModified)
5057 {
5058 if (!mData->mCurrentStateModified)
5059 CFGLDRSetBool (aMachineNode, "currentStateModified", false);
5060 else
5061 CFGLDRDeleteAttribute (aMachineNode, "currentStateModified");
5062 }
5063 }
5064
5065 return rc;
5066}
5067
5068/**
5069 * Saves the given snapshot and all its children (unless \a aAttrsOnly is true).
5070 * It is assumed that the given node is empty (unless \a aAttrsOnly is true).
5071 *
5072 * @param aNode <Snapshot> node to save the snapshot to
5073 * @param aSnapshot snapshot to save
5074 * @param aAttrsOnly if true, only updatge user-changeable attrs
5075 */
5076HRESULT Machine::saveSnapshot (CFGNODE aNode, Snapshot *aSnapshot, bool aAttrsOnly)
5077{
5078 AssertReturn (aNode && aSnapshot, E_INVALIDARG);
5079 AssertReturn (mType == IsMachine || mType == IsSessionMachine, E_FAIL);
5080
5081 /* uuid (required) */
5082 if (!aAttrsOnly)
5083 CFGLDRSetUUID (aNode, "uuid", aSnapshot->data().mId);
5084
5085 /* name (required) */
5086 CFGLDRSetBSTR (aNode, "name", aSnapshot->data().mName);
5087
5088 /* timeStamp (required) */
5089 CFGLDRSetDateTime (aNode, "timeStamp", aSnapshot->data().mTimeStamp);
5090
5091 /* Description node (optional) */
5092 {
5093 CFGNODE descNode = 0;
5094 CFGLDRCreateChildNode (aNode, "Description", &descNode);
5095 CFGLDRSetBSTR (descNode, NULL, aSnapshot->data().mDescription);
5096 CFGLDRReleaseNode (descNode);
5097 }
5098
5099 if (aAttrsOnly)
5100 return S_OK;
5101
5102 /* stateFile (optional) */
5103 if (aSnapshot->stateFilePath())
5104 {
5105 /* try to make the file name relative to the settings file dir */
5106 Utf8Str stateFilePath = aSnapshot->stateFilePath();
5107 calculateRelativePath (stateFilePath, stateFilePath);
5108 CFGLDRSetString (aNode, "stateFile", stateFilePath);
5109 }
5110
5111 {
5112 ComObjPtr <SnapshotMachine> snapshotMachine = aSnapshot->data().mMachine;
5113 ComAssertRet (!snapshotMachine.isNull(), E_FAIL);
5114
5115 /* save hardware */
5116 {
5117 CFGNODE hwNode = 0;
5118 CFGLDRCreateChildNode (aNode, "Hardware", &hwNode);
5119
5120 HRESULT rc = snapshotMachine->saveHardware (hwNode);
5121
5122 CFGLDRReleaseNode (hwNode);
5123 if (FAILED (rc))
5124 return rc;
5125 }
5126
5127 /* save hard disks */
5128 {
5129 CFGNODE hdasNode = 0;
5130 CFGLDRCreateChildNode (aNode, "HardDiskAttachments", &hdasNode);
5131
5132 HRESULT rc = snapshotMachine->saveHardDisks (hdasNode);
5133
5134 CFGLDRReleaseNode (hdasNode);
5135 if (FAILED (rc))
5136 return rc;
5137 }
5138 }
5139
5140 /* save children */
5141 {
5142 AutoLock listLock (aSnapshot->childrenLock());
5143
5144 if (aSnapshot->children().size())
5145 {
5146 CFGNODE snapshotsNode = 0;
5147 CFGLDRCreateChildNode (aNode, "Snapshots", &snapshotsNode);
5148
5149 HRESULT rc = S_OK;
5150
5151 for (Snapshot::SnapshotList::const_iterator it = aSnapshot->children().begin();
5152 it != aSnapshot->children().end() && SUCCEEDED (rc);
5153 ++ it)
5154 {
5155 CFGNODE snapshotNode = 0;
5156 CFGLDRCreateChildNode (snapshotsNode, "Snapshot", &snapshotNode);
5157
5158 rc = saveSnapshot (snapshotNode, (*it), aAttrsOnly);
5159
5160 CFGLDRReleaseNode (snapshotNode);
5161 }
5162
5163 CFGLDRReleaseNode (snapshotsNode);
5164 if (FAILED (rc))
5165 return rc;
5166 }
5167 }
5168
5169 return S_OK;
5170}
5171
5172/**
5173 * Creates Saves the VM hardware configuration.
5174 * It is assumed that the given node is empty.
5175 *
5176 * @param aNode <Hardware> node to save the VM hardware confguration to
5177 */
5178HRESULT Machine::saveHardware (CFGNODE aNode)
5179{
5180 AssertReturn (aNode, E_INVALIDARG);
5181
5182 HRESULT rc = S_OK;
5183
5184 /* CPU */
5185 {
5186 CFGNODE cpuNode = 0;
5187 CFGLDRCreateChildNode (aNode, "CPU", &cpuNode);
5188 CFGNODE hwVirtExNode = 0;
5189 CFGLDRCreateChildNode (cpuNode, "HardwareVirtEx", &hwVirtExNode);
5190 char *value = NULL;
5191 switch (mHWData->mHWVirtExEnabled)
5192 {
5193 case TriStateBool_False:
5194 value = "false";
5195 break;
5196 case TriStateBool_True:
5197 value = "true";
5198 break;
5199 default:
5200 value = "default";
5201 }
5202 CFGLDRSetString (hwVirtExNode, "enabled", value);
5203 CFGLDRReleaseNode (hwVirtExNode);
5204 CFGLDRReleaseNode (cpuNode);
5205 }
5206
5207 /* memory (required) */
5208 {
5209 CFGNODE memoryNode = 0;
5210 CFGLDRCreateChildNode (aNode, "Memory", &memoryNode);
5211 CFGLDRSetUInt32 (memoryNode, "RAMSize", mHWData->mMemorySize);
5212 CFGLDRReleaseNode (memoryNode);
5213 }
5214
5215 /* boot (required) */
5216 do
5217 {
5218 CFGNODE bootNode = 0;
5219 CFGLDRCreateChildNode (aNode, "Boot", &bootNode);
5220
5221 for (ULONG pos = 0; pos < ELEMENTS (mHWData->mBootOrder); pos ++)
5222 {
5223 const char *device = NULL;
5224 switch (mHWData->mBootOrder [pos])
5225 {
5226 case DeviceType_NoDevice:
5227 /* skip, this is allowed for <Order> nodes
5228 * when loading, the default value NoDevice will remain */
5229 continue;
5230 case DeviceType_FloppyDevice: device = "Floppy"; break;
5231 case DeviceType_DVDDevice: device = "DVD"; break;
5232 case DeviceType_HardDiskDevice: device = "HardDisk"; break;
5233 case DeviceType_NetworkDevice: device = "Network"; break;
5234 default:
5235 ComAssertMsgFailedBreak (("Invalid boot device: %d\n",
5236 mHWData->mBootOrder [pos]),
5237 rc = E_FAIL);
5238 }
5239 if (FAILED (rc))
5240 break;
5241
5242 CFGNODE orderNode = 0;
5243 CFGLDRAppendChildNode (bootNode, "Order", &orderNode);
5244
5245 CFGLDRSetUInt32 (orderNode, "position", pos + 1);
5246 CFGLDRSetString (orderNode, "device", device);
5247
5248 CFGLDRReleaseNode (orderNode);
5249 }
5250
5251 CFGLDRReleaseNode (bootNode);
5252 }
5253 while (0);
5254
5255 if (FAILED (rc))
5256 return rc;
5257
5258 /* display (required) */
5259 {
5260 CFGNODE displayNode = 0;
5261 CFGLDRCreateChildNode (aNode, "Display", &displayNode);
5262 CFGLDRSetUInt32 (displayNode, "VRAMSize", mHWData->mVRAMSize);
5263 CFGLDRReleaseNode (displayNode);
5264 }
5265
5266#ifdef VBOX_VRDP
5267 /* VRDP settings (optional) */
5268 /// @todo (dmik) move the code to VRDPServer
5269 /// @todo r=sunlover: moved. dmik, please review.
5270 {
5271 CFGNODE remoteDisplayNode = 0;
5272 CFGLDRCreateChildNode (aNode, "RemoteDisplay", &remoteDisplayNode);
5273
5274 if (remoteDisplayNode)
5275 {
5276 mVRDPServer->saveConfig (remoteDisplayNode);
5277 CFGLDRReleaseNode (remoteDisplayNode);
5278 }
5279 }
5280#endif
5281
5282 /* BIOS (required) */
5283 {
5284 CFGNODE biosNode = 0;
5285 CFGLDRCreateChildNode (aNode, "BIOS", &biosNode);
5286 {
5287 BOOL fSet;
5288 /* ACPI */
5289 CFGNODE acpiNode = 0;
5290 CFGLDRCreateChildNode (biosNode, "ACPI", &acpiNode);
5291 mBIOSSettings->COMGETTER(ACPIEnabled)(&fSet);
5292 CFGLDRSetBool (acpiNode, "enabled", !!fSet);
5293 CFGLDRReleaseNode (acpiNode);
5294
5295 /* IOAPIC */
5296 CFGNODE ioapicNode = 0;
5297 CFGLDRCreateChildNode (biosNode, "IOAPIC", &ioapicNode);
5298 mBIOSSettings->COMGETTER(IOAPICEnabled)(&fSet);
5299 CFGLDRSetBool (ioapicNode, "enabled", !!fSet);
5300 CFGLDRReleaseNode (ioapicNode);
5301
5302 /* BIOS logo (optional) **/
5303 CFGNODE logoNode = 0;
5304 CFGLDRCreateChildNode (biosNode, "Logo", &logoNode);
5305 mBIOSSettings->COMGETTER(LogoFadeIn)(&fSet);
5306 CFGLDRSetBool (logoNode, "fadeIn", !!fSet);
5307 mBIOSSettings->COMGETTER(LogoFadeOut)(&fSet);
5308 CFGLDRSetBool (logoNode, "fadeOut", !!fSet);
5309 ULONG ulDisplayTime;
5310 mBIOSSettings->COMGETTER(LogoDisplayTime)(&ulDisplayTime);
5311 CFGLDRSetUInt32 (logoNode, "displayTime", ulDisplayTime);
5312 Bstr logoPath;
5313 mBIOSSettings->COMGETTER(LogoImagePath)(logoPath.asOutParam());
5314 if (logoPath)
5315 CFGLDRSetBSTR (logoNode, "imagePath", logoPath);
5316 else
5317 CFGLDRDeleteAttribute (logoNode, "imagePath");
5318 CFGLDRReleaseNode (logoNode);
5319
5320 /* boot menu (optional) */
5321 CFGNODE bootMenuNode = 0;
5322 CFGLDRCreateChildNode (biosNode, "BootMenu", &bootMenuNode);
5323 BIOSBootMenuMode_T bootMenuMode;
5324 Bstr bootMenuModeStr;
5325 mBIOSSettings->COMGETTER(BootMenuMode)(&bootMenuMode);
5326 switch (bootMenuMode)
5327 {
5328 case BIOSBootMenuMode_Disabled:
5329 bootMenuModeStr = "disabled";
5330 break;
5331 case BIOSBootMenuMode_MenuOnly:
5332 bootMenuModeStr = "menuonly";
5333 break;
5334 default:
5335 bootMenuModeStr = "messageandmenu";
5336 }
5337 CFGLDRSetBSTR (bootMenuNode, "mode", bootMenuModeStr);
5338 CFGLDRReleaseNode (bootMenuNode);
5339 }
5340 CFGLDRReleaseNode(biosNode);
5341 }
5342
5343 /* DVD drive (required) */
5344 /// @todo (dmik) move the code to DVDDrive
5345 do
5346 {
5347 CFGNODE dvdNode = 0;
5348 CFGLDRCreateChildNode (aNode, "DVDDrive", &dvdNode);
5349
5350 BOOL fPassthrough;
5351 mDVDDrive->COMGETTER(Passthrough)(&fPassthrough);
5352 CFGLDRSetBool(dvdNode, "passthrough", !!fPassthrough);
5353
5354 switch (mDVDDrive->data()->mDriveState)
5355 {
5356 case DriveState_ImageMounted:
5357 {
5358 Assert (!mDVDDrive->data()->mDVDImage.isNull());
5359
5360 Guid id;
5361 rc = mDVDDrive->data()->mDVDImage->COMGETTER(Id) (id.asOutParam());
5362 Assert (!id.isEmpty());
5363
5364 CFGNODE imageNode = 0;
5365 CFGLDRCreateChildNode (dvdNode, "Image", &imageNode);
5366 CFGLDRSetUUID (imageNode, "uuid", id.ptr());
5367 CFGLDRReleaseNode (imageNode);
5368 break;
5369 }
5370 case DriveState_HostDriveCaptured:
5371 {
5372 Assert (!mDVDDrive->data()->mHostDrive.isNull());
5373
5374 Bstr name;
5375 rc = mDVDDrive->data()->mHostDrive->COMGETTER(Name) (name.asOutParam());
5376 Assert (!name.isEmpty());
5377
5378 CFGNODE hostDriveNode = 0;
5379 CFGLDRCreateChildNode (dvdNode, "HostDrive", &hostDriveNode);
5380 CFGLDRSetBSTR (hostDriveNode, "src", name);
5381 CFGLDRReleaseNode (hostDriveNode);
5382 break;
5383 }
5384 case DriveState_NotMounted:
5385 /* do nothing, i.e.leave the DVD drive node empty */
5386 break;
5387 default:
5388 ComAssertMsgFailedBreak (("Invalid DVD drive state: %d\n",
5389 mDVDDrive->data()->mDriveState),
5390 rc = E_FAIL);
5391 }
5392
5393 CFGLDRReleaseNode (dvdNode);
5394 }
5395 while (0);
5396
5397 if (FAILED (rc))
5398 return rc;
5399
5400 /* Flooppy drive (required) */
5401 /// @todo (dmik) move the code to DVDDrive
5402 do
5403 {
5404 CFGNODE floppyNode = 0;
5405 CFGLDRCreateChildNode (aNode, "FloppyDrive", &floppyNode);
5406
5407 BOOL fFloppyEnabled;
5408 rc = mFloppyDrive->COMGETTER(Enabled)(&fFloppyEnabled);
5409 CFGLDRSetBool (floppyNode, "enabled", !!fFloppyEnabled);
5410
5411 switch (mFloppyDrive->data()->mDriveState)
5412 {
5413 case DriveState_ImageMounted:
5414 {
5415 Assert (!mFloppyDrive->data()->mFloppyImage.isNull());
5416
5417 Guid id;
5418 rc = mFloppyDrive->data()->mFloppyImage->COMGETTER(Id) (id.asOutParam());
5419 Assert (!id.isEmpty());
5420
5421 CFGNODE imageNode = 0;
5422 CFGLDRCreateChildNode (floppyNode, "Image", &imageNode);
5423 CFGLDRSetUUID (imageNode, "uuid", id.ptr());
5424 CFGLDRReleaseNode (imageNode);
5425 break;
5426 }
5427 case DriveState_HostDriveCaptured:
5428 {
5429 Assert (!mFloppyDrive->data()->mHostDrive.isNull());
5430
5431 Bstr name;
5432 rc = mFloppyDrive->data()->mHostDrive->COMGETTER(Name) (name.asOutParam());
5433 Assert (!name.isEmpty());
5434
5435 CFGNODE hostDriveNode = 0;
5436 CFGLDRCreateChildNode (floppyNode, "HostDrive", &hostDriveNode);
5437 CFGLDRSetBSTR (hostDriveNode, "src", name);
5438 CFGLDRReleaseNode (hostDriveNode);
5439 break;
5440 }
5441 case DriveState_NotMounted:
5442 /* do nothing, i.e.leave the Floppy drive node empty */
5443 break;
5444 default:
5445 ComAssertMsgFailedBreak (("Invalid Floppy drive state: %d\n",
5446 mFloppyDrive->data()->mDriveState),
5447 rc = E_FAIL);
5448 }
5449
5450 CFGLDRReleaseNode (floppyNode);
5451 }
5452 while (0);
5453
5454 if (FAILED (rc))
5455 return rc;
5456
5457
5458 /* USB Controller (required) */
5459 rc = mUSBController->saveSettings (aNode);
5460 if (FAILED (rc))
5461 return rc;
5462
5463 /* Network adapters (required) */
5464 do
5465 {
5466 CFGNODE nwNode = 0;
5467 CFGLDRCreateChildNode (aNode, "Network", &nwNode);
5468
5469 for (ULONG slot = 0; slot < ELEMENTS (mNetworkAdapters); slot ++)
5470 {
5471 CFGNODE networkAdapterNode = 0;
5472 CFGLDRAppendChildNode (nwNode, "Adapter", &networkAdapterNode);
5473
5474 CFGLDRSetUInt32 (networkAdapterNode, "slot", slot);
5475 CFGLDRSetBool (networkAdapterNode, "enabled",
5476 !!mNetworkAdapters [slot]->data()->mEnabled);
5477 CFGLDRSetBSTR (networkAdapterNode, "MACAddress",
5478 mNetworkAdapters [slot]->data()->mMACAddress);
5479 CFGLDRSetBool (networkAdapterNode, "cable",
5480 !!mNetworkAdapters [slot]->data()->mCableConnected);
5481
5482 if (mNetworkAdapters [slot]->data()->mTraceEnabled)
5483 CFGLDRSetBool (networkAdapterNode, "trace", true);
5484
5485 CFGLDRSetBSTR (networkAdapterNode, "tracefile",
5486 mNetworkAdapters [slot]->data()->mTraceFile);
5487
5488 switch (mNetworkAdapters [slot]->data()->mAdapterType)
5489 {
5490 case NetworkAdapterType_NetworkAdapterAm79C970A:
5491 CFGLDRSetString (networkAdapterNode, "type", "Am79C970A");
5492 break;
5493 case NetworkAdapterType_NetworkAdapterAm79C973:
5494 CFGLDRSetString (networkAdapterNode, "type", "Am79C973");
5495 break;
5496 default:
5497 ComAssertMsgFailedBreak (("Invalid network adapter type: %d\n",
5498 mNetworkAdapters [slot]->data()->mAdapterType),
5499 rc = E_FAIL);
5500 }
5501
5502 CFGNODE attachmentNode = 0;
5503 switch (mNetworkAdapters [slot]->data()->mAttachmentType)
5504 {
5505 case NetworkAttachmentType_NoNetworkAttachment:
5506 {
5507 /* do nothing -- empty content */
5508 break;
5509 }
5510 case NetworkAttachmentType_NATNetworkAttachment:
5511 {
5512 CFGLDRAppendChildNode (networkAdapterNode, "NAT", &attachmentNode);
5513 break;
5514 }
5515 case NetworkAttachmentType_HostInterfaceNetworkAttachment:
5516 {
5517 CFGLDRAppendChildNode (networkAdapterNode, "HostInterface", &attachmentNode);
5518 const Bstr &name = mNetworkAdapters [slot]->data()->mHostInterface;
5519#ifdef __WIN__
5520 Assert (!name.isNull());
5521#endif
5522#ifdef VBOX_WITH_UNIXY_TAP_NETWORKING
5523 if (!name.isEmpty())
5524#endif
5525 CFGLDRSetBSTR (attachmentNode, "name", name);
5526#ifdef VBOX_WITH_UNIXY_TAP_NETWORKING
5527 const Bstr &tapSetupApp =
5528 mNetworkAdapters [slot]->data()->mTAPSetupApplication;
5529 if (!tapSetupApp.isEmpty())
5530 CFGLDRSetBSTR (attachmentNode, "TAPSetup", tapSetupApp);
5531 const Bstr &tapTerminateApp =
5532 mNetworkAdapters [slot]->data()->mTAPTerminateApplication;
5533 if (!tapTerminateApp.isEmpty())
5534 CFGLDRSetBSTR (attachmentNode, "TAPTerminate", tapTerminateApp);
5535#endif /* VBOX_WITH_UNIXY_TAP_NETWORKING */
5536 break;
5537 }
5538 case NetworkAttachmentType_InternalNetworkAttachment:
5539 {
5540 CFGLDRAppendChildNode (networkAdapterNode, "InternalNetwork", &attachmentNode);
5541 const Bstr &name = mNetworkAdapters[slot]->data()->mInternalNetwork;
5542 Assert(!name.isNull());
5543 CFGLDRSetBSTR (attachmentNode, "name", name);
5544 break;
5545 }
5546 default:
5547 {
5548 ComAssertFailedBreak (rc = E_FAIL);
5549 break;
5550 }
5551 }
5552 if (attachmentNode)
5553 CFGLDRReleaseNode (attachmentNode);
5554
5555 CFGLDRReleaseNode (networkAdapterNode);
5556 }
5557
5558 CFGLDRReleaseNode (nwNode);
5559 }
5560 while (0);
5561
5562 if (FAILED (rc))
5563 return rc;
5564
5565 /* Audio adapter */
5566 do
5567 {
5568 CFGNODE adapterNode = 0;
5569 CFGLDRCreateChildNode (aNode, "AudioAdapter", &adapterNode);
5570
5571 switch (mAudioAdapter->data()->mAudioDriver)
5572 {
5573 case AudioDriverType_NullAudioDriver:
5574 {
5575 CFGLDRSetString (adapterNode, "driver", "null");
5576 break;
5577 }
5578#ifdef __WIN__
5579 case AudioDriverType_WINMMAudioDriver:
5580 {
5581 CFGLDRSetString (adapterNode, "driver", "winmm");
5582 break;
5583 }
5584 case AudioDriverType_DSOUNDAudioDriver:
5585 {
5586 CFGLDRSetString (adapterNode, "driver", "dsound");
5587 break;
5588 }
5589#endif /* __WIN__ */
5590#ifdef VBOX_WITH_ALSA
5591 case AudioDriverType_ALSAAudioDriver:
5592 {
5593 CFGLDRSetString (adapterNode, "driver", "alsa");
5594 break;
5595 }
5596#else
5597 /* fall back to OSS */
5598 case AudioDriverType_ALSAAudioDriver:
5599#endif
5600#ifdef __LINUX__
5601 case AudioDriverType_OSSAudioDriver:
5602 {
5603 CFGLDRSetString (adapterNode, "driver", "oss");
5604 break;
5605 }
5606#endif /* __LINUX__ */
5607 default:
5608 ComAssertMsgFailedBreak (("Wrong audio driver type! driver = %d\n",
5609 mAudioAdapter->data()->mAudioDriver),
5610 rc = E_FAIL);
5611 }
5612
5613 CFGLDRSetBool (adapterNode, "enabled", !!mAudioAdapter->data()->mEnabled);
5614
5615 CFGLDRReleaseNode (adapterNode);
5616 }
5617 while (0);
5618
5619 if (FAILED (rc))
5620 return rc;
5621
5622 /* Shared folders */
5623 do
5624 {
5625 CFGNODE sharedFoldersNode = 0;
5626 CFGLDRCreateChildNode (aNode, "SharedFolders", &sharedFoldersNode);
5627
5628 for (HWData::SharedFolderList::const_iterator it = mHWData->mSharedFolders.begin();
5629 it != mHWData->mSharedFolders.end();
5630 ++ it)
5631 {
5632 ComObjPtr <SharedFolder> folder = *it;
5633
5634 CFGNODE folderNode = 0;
5635 CFGLDRAppendChildNode (sharedFoldersNode, "SharedFolder", &folderNode);
5636
5637 /* all are mandatory */
5638 CFGLDRSetBSTR (folderNode, "name", folder->name());
5639 CFGLDRSetBSTR (folderNode, "hostPath", folder->hostPath());
5640
5641 CFGLDRReleaseNode (folderNode);
5642 }
5643
5644 CFGLDRReleaseNode (sharedFoldersNode);
5645 }
5646 while (0);
5647
5648 /* Clipboard */
5649 {
5650 CFGNODE clipNode = 0;
5651 CFGLDRCreateChildNode (aNode, "Clipboard", &clipNode);
5652
5653 char *mode = "Disabled";
5654 switch (mHWData->mClipboardMode)
5655 {
5656 case ClipboardMode_ClipDisabled:
5657 /* already assigned */
5658 break;
5659 case ClipboardMode_ClipHostToGuest:
5660 mode = "HostToGuest";
5661 break;
5662 case ClipboardMode_ClipGuestToHost:
5663 mode = "GuestToHost";
5664 break;
5665 case ClipboardMode_ClipBidirectional:
5666 mode = "Bidirectional";
5667 break;
5668 default:
5669 AssertMsgFailed (("Clipboard mode %d is invalid",
5670 mHWData->mClipboardMode));
5671 break;
5672 }
5673 CFGLDRSetString (clipNode, "mode", mode);
5674
5675 CFGLDRReleaseNode (clipNode);
5676 }
5677
5678 return rc;
5679}
5680
5681/**
5682 * Saves the hard disk confguration.
5683 * It is assumed that the given node is empty.
5684 *
5685 * @param aNode <HardDiskAttachments> node to save the hard disk confguration to
5686 */
5687HRESULT Machine::saveHardDisks (CFGNODE aNode)
5688{
5689 AssertReturn (aNode, E_INVALIDARG);
5690
5691 HRESULT rc = S_OK;
5692
5693 for (HDData::HDAttachmentList::const_iterator it = mHDData->mHDAttachments.begin();
5694 it != mHDData->mHDAttachments.end() && SUCCEEDED (rc);
5695 ++ it)
5696 {
5697 ComObjPtr <HardDiskAttachment> att = *it;
5698
5699 CFGNODE hdNode = 0;
5700 CFGLDRAppendChildNode (aNode, "HardDiskAttachment", &hdNode);
5701
5702 do
5703 {
5704 const char *bus = NULL;
5705 switch (att->controller())
5706 {
5707 case DiskControllerType_IDE0Controller: bus = "ide0"; break;
5708 case DiskControllerType_IDE1Controller: bus = "ide1"; break;
5709 default:
5710 ComAssertFailedBreak (rc = E_FAIL);
5711 }
5712 if (FAILED (rc))
5713 break;
5714
5715 const char *dev = NULL;
5716 switch (att->deviceNumber())
5717 {
5718 case 0: dev = "master"; break;
5719 case 1: dev = "slave"; break;
5720 default:
5721 ComAssertFailedBreak (rc = E_FAIL);
5722 }
5723 if (FAILED (rc))
5724 break;
5725
5726 CFGLDRSetUUID (hdNode, "hardDisk", att->hardDisk()->id());
5727 CFGLDRSetString (hdNode, "bus", bus);
5728 CFGLDRSetString (hdNode, "device", dev);
5729 }
5730 while (0);
5731
5732 CFGLDRReleaseNode (hdNode);
5733 }
5734
5735 return rc;
5736}
5737
5738/**
5739 * Saves machine state settings as defined by aFlags
5740 * (SaveSTS_* values).
5741 *
5742 * @param aFlags a combination of SaveSTS_* flags
5743 *
5744 * @note Locks objects!
5745 */
5746HRESULT Machine::saveStateSettings (int aFlags)
5747{
5748 if (aFlags == 0)
5749 return S_OK;
5750
5751 AutoCaller autoCaller (this);
5752 AssertComRCReturn (autoCaller.rc(), autoCaller.rc());
5753
5754 AutoLock alock (this);
5755
5756 /* load the config file */
5757 CFGHANDLE configLoader = 0;
5758 HRESULT rc = openConfigLoader (&configLoader);
5759 if (FAILED (rc))
5760 return rc;
5761
5762 CFGNODE machineNode = 0;
5763 CFGLDRGetNode (configLoader, "VirtualBox/Machine", 0, &machineNode);
5764
5765 do
5766 {
5767 ComAssertBreak (machineNode, rc = E_FAIL);
5768
5769 if (aFlags & SaveSTS_CurStateModified)
5770 {
5771 if (!mData->mCurrentStateModified)
5772 CFGLDRSetBool (machineNode, "currentStateModified", false);
5773 else
5774 CFGLDRDeleteAttribute (machineNode, "currentStateModified");
5775 }
5776
5777 if (aFlags & SaveSTS_StateFilePath)
5778 {
5779 if (mSSData->mStateFilePath)
5780 CFGLDRSetBSTR (machineNode, "stateFile", mSSData->mStateFilePath);
5781 else
5782 CFGLDRDeleteAttribute (machineNode, "stateFile");
5783 }
5784
5785 if (aFlags & SaveSTS_StateTimeStamp)
5786 {
5787 Assert (mData->mMachineState != MachineState_Aborted ||
5788 mSSData->mStateFilePath.isNull());
5789
5790 CFGLDRSetDateTime (machineNode, "lastStateChange",
5791 mData->mLastStateChange);
5792
5793 // set the aborted attribute when appropriate
5794 if (mData->mMachineState == MachineState_Aborted)
5795 CFGLDRSetBool (machineNode, "aborted", true);
5796 else
5797 CFGLDRDeleteAttribute (machineNode, "aborted");
5798 }
5799 }
5800 while (0);
5801
5802 if (machineNode)
5803 CFGLDRReleaseNode (machineNode);
5804
5805 if (SUCCEEDED (rc))
5806 rc = closeConfigLoader (configLoader, true /* aSaveBeforeClose */);
5807 else
5808 closeConfigLoader (configLoader, false /* aSaveBeforeClose */);
5809
5810 return rc;
5811}
5812
5813/**
5814 * Cleans up all differencing hard disks based on immutable hard disks.
5815 *
5816 * @note Locks objects!
5817 */
5818HRESULT Machine::wipeOutImmutableDiffs()
5819{
5820 AutoCaller autoCaller (this);
5821 AssertComRCReturn (autoCaller.rc(), autoCaller.rc());
5822
5823 AutoReaderLock alock (this);
5824
5825 AssertReturn (mData->mMachineState == MachineState_PoweredOff ||
5826 mData->mMachineState == MachineState_Aborted, E_FAIL);
5827
5828 for (HDData::HDAttachmentList::const_iterator it = mHDData->mHDAttachments.begin();
5829 it != mHDData->mHDAttachments.end();
5830 ++ it)
5831 {
5832 ComObjPtr <HardDisk> hd = (*it)->hardDisk();
5833 AutoLock hdLock (hd);
5834
5835 if(hd->isParentImmutable())
5836 {
5837 /// @todo (dmik) no error handling for now
5838 // (need async error reporting for this)
5839 hd->asVDI()->wipeOutImage();
5840 }
5841 }
5842
5843 return S_OK;
5844}
5845
5846/**
5847 * Fixes up lazy hard disk attachments by creating or deleting differencing
5848 * hard disks when machine settings are being committed.
5849 * Must be called only from #commit().
5850 *
5851 * @note Locks objects!
5852 */
5853HRESULT Machine::fixupHardDisks (bool aCommit)
5854{
5855 AutoCaller autoCaller (this);
5856 AssertComRCReturn (autoCaller.rc(), autoCaller.rc());
5857
5858 AutoLock alock (this);
5859
5860 /* no attac/detach operations -- nothing to do */
5861 if (!mHDData.isBackedUp())
5862 {
5863 mHDData->mHDAttachmentsChanged = false;
5864 return S_OK;
5865 }
5866
5867 AssertReturn (mData->mRegistered, E_FAIL);
5868
5869 if (aCommit)
5870 {
5871 /*
5872 * changes are being committed,
5873 * perform actual diff image creation, deletion etc.
5874 */
5875
5876 /* take a copy of backed up attachments (will modify it) */
5877 HDData::HDAttachmentList backedUp = mHDData.backedUpData()->mHDAttachments;
5878 /* list of new diffs created */
5879 std::list <ComObjPtr <HardDisk> > newDiffs;
5880
5881 HRESULT rc = S_OK;
5882
5883 /* go through current attachments */
5884 for (HDData::HDAttachmentList::const_iterator
5885 it = mHDData->mHDAttachments.begin();
5886 it != mHDData->mHDAttachments.end();
5887 ++ it)
5888 {
5889 ComObjPtr <HardDiskAttachment> hda = *it;
5890 ComObjPtr <HardDisk> hd = hda->hardDisk();
5891 AutoLock hdLock (hd);
5892
5893 if (!hda->isDirty())
5894 {
5895 /*
5896 * not dirty, therefore was either attached before backing up
5897 * or doesn't need any fixup (already fixed up); try to locate
5898 * this hard disk among backed up attachments and remove from
5899 * there to prevent it from being deassociated/deleted
5900 */
5901 HDData::HDAttachmentList::iterator oldIt;
5902 for (oldIt = backedUp.begin(); oldIt != backedUp.end(); ++ oldIt)
5903 if ((*oldIt)->hardDisk().equalsTo (hd))
5904 break;
5905 if (oldIt != backedUp.end())
5906 {
5907 /* remove from there */
5908 backedUp.erase (oldIt);
5909 // LogTraceMsg (("FC: %ls found in old\n", hd->toString().raw()));
5910 }
5911 }
5912 else
5913 {
5914 /* dirty, determine what to do */
5915
5916 bool needDiff = false;
5917 bool searchAmongSnapshots = false;
5918
5919 switch (hd->type())
5920 {
5921 case HardDiskType_ImmutableHardDisk:
5922 {
5923 /* decrease readers increased in AttachHardDisk() */
5924 hd->releaseReader();
5925 // LogTraceMsg (("FC: %ls released\n", hd->toString().raw()));
5926 /* indicate we need a diff (indirect attachment) */
5927 needDiff = true;
5928 break;
5929 }
5930 case HardDiskType_WritethroughHardDisk:
5931 {
5932 /* reset the dirty flag */
5933 hda->updateHardDisk (hd, false /* aDirty */);
5934 // LogTraceMsg (("FC: %ls updated\n", hd->toString().raw()));
5935 break;
5936 }
5937 case HardDiskType_NormalHardDisk:
5938 {
5939 if (hd->snapshotId().isEmpty())
5940 {
5941 /* reset the dirty flag */
5942 hda->updateHardDisk (hd, false /* aDirty */);
5943 // LogTraceMsg (("FC: %ls updated\n", hd->toString().raw()));
5944 }
5945 else
5946 {
5947 /* decrease readers increased in AttachHardDisk() */
5948 hd->releaseReader();
5949 // LogTraceMsg (("FC: %ls released\n", hd->toString().raw()));
5950 /* indicate we need a diff (indirect attachment) */
5951 needDiff = true;
5952 /* search for the most recent base among snapshots */
5953 searchAmongSnapshots = true;
5954 }
5955 break;
5956 }
5957 }
5958
5959 if (!needDiff)
5960 continue;
5961
5962 bool createDiff = false;
5963
5964 /*
5965 * see whether any previously attached hard disk has the
5966 * the currently attached one (Normal or Independent) as
5967 * the root
5968 */
5969
5970 HDData::HDAttachmentList::iterator foundIt = backedUp.end();
5971
5972 for (HDData::HDAttachmentList::iterator it = backedUp.begin();
5973 it != backedUp.end();
5974 ++ it)
5975 {
5976 if ((*it)->hardDisk()->root().equalsTo (hd))
5977 {
5978 /*
5979 * matched dev and ctl (i.e. attached to the same place)
5980 * will win and immediately stop the search; otherwise
5981 * the first attachment that matched the hd only will
5982 * be used
5983 */
5984 if ((*it)->deviceNumber() == hda->deviceNumber() &&
5985 (*it)->controller() == hda->controller())
5986 {
5987 foundIt = it;
5988 break;
5989 }
5990 else
5991 if (foundIt == backedUp.end())
5992 {
5993 /*
5994 * not an exact match; ensure there is no exact match
5995 * among other current attachments referring the same
5996 * root (to prevent this attachmend from reusing the
5997 * hard disk of the other attachment that will later
5998 * give the exact match or already gave it before)
5999 */
6000 bool canReuse = true;
6001 for (HDData::HDAttachmentList::const_iterator
6002 it2 = mHDData->mHDAttachments.begin();
6003 it2 != mHDData->mHDAttachments.end();
6004 ++ it2)
6005 {
6006 if ((*it2)->deviceNumber() == (*it)->deviceNumber() &&
6007 (*it2)->controller() == (*it)->controller() &&
6008 (*it2)->hardDisk()->root().equalsTo (hd))
6009 {
6010 /*
6011 * the exact match, either non-dirty or dirty
6012 * one refers the same root: in both cases
6013 * we cannot reuse the hard disk, so break
6014 */
6015 canReuse = false;
6016 break;
6017 }
6018 }
6019
6020 if (canReuse)
6021 foundIt = it;
6022 }
6023 }
6024 }
6025
6026 if (foundIt != backedUp.end())
6027 {
6028 /* found either one or another, reuse the diff */
6029 hda->updateHardDisk ((*foundIt)->hardDisk(),
6030 false /* aDirty */);
6031 // LogTraceMsg (("FC: %ls reused as %ls\n", hd->toString().raw(),
6032 // (*foundIt)->hardDisk()->toString().raw()));
6033 /* remove from there */
6034 backedUp.erase (foundIt);
6035 }
6036 else
6037 {
6038 /* was not attached, need a diff */
6039 createDiff = true;
6040 }
6041
6042 if (!createDiff)
6043 continue;
6044
6045 ComObjPtr <HardDisk> baseHd = hd;
6046
6047 if (searchAmongSnapshots)
6048 {
6049 /*
6050 * find the most recent diff based on the currently
6051 * attached root (Normal hard disk) among snapshots
6052 */
6053
6054 ComObjPtr <Snapshot> snap = mData->mCurrentSnapshot;
6055
6056 while (snap)
6057 {
6058 AutoLock snapLock (snap);
6059
6060 const HDData::HDAttachmentList &snapAtts =
6061 snap->data().mMachine->hdData()->mHDAttachments;
6062
6063 HDData::HDAttachmentList::const_iterator foundIt = snapAtts.end();
6064
6065 for (HDData::HDAttachmentList::const_iterator
6066 it = snapAtts.begin(); it != snapAtts.end(); ++ it)
6067 {
6068 if ((*it)->hardDisk()->root().equalsTo (hd))
6069 {
6070 /*
6071 * matched dev and ctl (i.e. attached to the same place)
6072 * will win and immediately stop the search; otherwise
6073 * the first attachment that matched the hd only will
6074 * be used
6075 */
6076 if ((*it)->deviceNumber() == hda->deviceNumber() &&
6077 (*it)->controller() == hda->controller())
6078 {
6079 foundIt = it;
6080 break;
6081 }
6082 else
6083 if (foundIt == snapAtts.end())
6084 foundIt = it;
6085 }
6086 }
6087
6088 if (foundIt != snapAtts.end())
6089 {
6090 /* the most recent diff has been found, use as a base */
6091 baseHd = (*foundIt)->hardDisk();
6092 // LogTraceMsg (("FC: %ls: recent found %ls\n",
6093 // hd->toString().raw(), baseHd->toString().raw()));
6094 break;
6095 }
6096
6097 snap = snap->parent();
6098 }
6099 }
6100
6101 /* create a new diff for the hard disk being indirectly attached */
6102
6103 AutoLock baseHdLock (baseHd);
6104 baseHd->addReader();
6105
6106 ComObjPtr <HVirtualDiskImage> vdi;
6107 rc = baseHd->createDiffHardDisk (mUserData->mSnapshotFolderFull,
6108 mData->mUuid, vdi, NULL);
6109 baseHd->releaseReader();
6110 CheckComRCBreakRC (rc);
6111
6112 newDiffs.push_back (ComObjPtr <HardDisk> (vdi));
6113
6114 /* update the attachment and reset the dirty flag */
6115 hda->updateHardDisk (ComObjPtr <HardDisk> (vdi),
6116 false /* aDirty */);
6117 // LogTraceMsg (("FC: %ls: diff created %ls\n",
6118 // baseHd->toString().raw(), vdi->toString().raw()));
6119 }
6120 }
6121
6122 if (FAILED (rc))
6123 {
6124 /* delete diffs we created */
6125 for (std::list <ComObjPtr <HardDisk> >::const_iterator
6126 it = newDiffs.begin(); it != newDiffs.end(); ++ it)
6127 {
6128 /*
6129 * unregisterDiffHardDisk() is supposed to delete and uninit
6130 * the differencing hard disk
6131 */
6132 mParent->unregisterDiffHardDisk (*it);
6133 /* too bad if we fail here, but nothing to do, just continue */
6134 }
6135
6136 /* the best is to rollback the changes... */
6137 mHDData.rollback();
6138 mHDData->mHDAttachmentsChanged = false;
6139 // LogTraceMsg (("FC: ROLLED BACK\n"));
6140 return rc;
6141 }
6142
6143 /*
6144 * go through the rest of old attachments and delete diffs
6145 * or deassociate hard disks from machines (they will become detached)
6146 */
6147 for (HDData::HDAttachmentList::iterator
6148 it = backedUp.begin(); it != backedUp.end(); ++ it)
6149 {
6150 ComObjPtr <HardDiskAttachment> hda = *it;
6151 ComObjPtr <HardDisk> hd = hda->hardDisk();
6152 AutoLock hdLock (hd);
6153
6154 if (hd->isDifferencing())
6155 {
6156 /*
6157 * unregisterDiffHardDisk() is supposed to delete and uninit
6158 * the differencing hard disk
6159 */
6160 // LogTraceMsg (("FC: %ls diff deleted\n", hd->toString().raw()));
6161 rc = mParent->unregisterDiffHardDisk (hd);
6162 /*
6163 * too bad if we fail here, but nothing to do, just continue
6164 * (the last rc will be returned to the caller though)
6165 */
6166 }
6167 else
6168 {
6169 /* deassociate from this machine */
6170 // LogTraceMsg (("FC: %ls deassociated\n", hd->toString().raw()));
6171 hd->setMachineId (Guid());
6172 }
6173 }
6174
6175 /* commit all the changes */
6176 mHDData->mHDAttachmentsChanged = mHDData.hasActualChanges();
6177 mHDData.commit();
6178 // LogTraceMsg (("FC: COMMITTED\n"));
6179
6180 return rc;
6181 }
6182
6183 /*
6184 * changes are being rolled back,
6185 * go trhough all current attachments and fix up dirty ones
6186 * the way it is done in DetachHardDisk()
6187 */
6188
6189 for (HDData::HDAttachmentList::iterator it = mHDData->mHDAttachments.begin();
6190 it != mHDData->mHDAttachments.end();
6191 ++ it)
6192 {
6193 ComObjPtr <HardDiskAttachment> hda = *it;
6194 ComObjPtr <HardDisk> hd = hda->hardDisk();
6195 AutoLock hdLock (hd);
6196
6197 if (hda->isDirty())
6198 {
6199 switch (hd->type())
6200 {
6201 case HardDiskType_ImmutableHardDisk:
6202 {
6203 /* decrease readers increased in AttachHardDisk() */
6204 hd->releaseReader();
6205 // LogTraceMsg (("FR: %ls released\n", hd->toString().raw()));
6206 break;
6207 }
6208 case HardDiskType_WritethroughHardDisk:
6209 {
6210 /* deassociate from this machine */
6211 hd->setMachineId (Guid());
6212 // LogTraceMsg (("FR: %ls deassociated\n", hd->toString().raw()));
6213 break;
6214 }
6215 case HardDiskType_NormalHardDisk:
6216 {
6217 if (hd->snapshotId().isEmpty())
6218 {
6219 /* deassociate from this machine */
6220 hd->setMachineId (Guid());
6221 // LogTraceMsg (("FR: %ls deassociated\n", hd->toString().raw()));
6222 }
6223 else
6224 {
6225 /* decrease readers increased in AttachHardDisk() */
6226 hd->releaseReader();
6227 // LogTraceMsg (("FR: %ls released\n", hd->toString().raw()));
6228 }
6229
6230 break;
6231 }
6232 }
6233 }
6234 }
6235
6236 /* rollback all the changes */
6237 mHDData.rollback();
6238 // LogTraceMsg (("FR: ROLLED BACK\n"));
6239
6240 return S_OK;
6241}
6242
6243/**
6244 * Creates differencing hard disks for all normal hard disks
6245 * and replaces attachments to refer to created disks.
6246 * Used when taking a snapshot or when discarding the current state.
6247 *
6248 * @param aSnapshotId ID of the snapshot being taken
6249 * or NULL if the current state is being discarded
6250 * @param aFolder folder where to create diff. hard disks
6251 * @param aProgress progress object to run (must contain at least as
6252 * many operations left as the number of VDIs attached)
6253 * @param aOnline whether the machine is online (i.e., when the EMT
6254 * thread is paused, OR when current hard disks are
6255 * marked as busy for some other reason)
6256 *
6257 * @note
6258 * The progress object is not marked as completed, neither on success
6259 * nor on failure. This is a responsibility of the caller.
6260 *
6261 * @note Locks mParent + this object for writing
6262 */
6263HRESULT Machine::createSnapshotDiffs (const Guid *aSnapshotId,
6264 const Bstr &aFolder,
6265 const ComObjPtr <Progress> &aProgress,
6266 bool aOnline)
6267{
6268 AssertReturn (!aFolder.isEmpty(), E_FAIL);
6269
6270 AutoCaller autoCaller (this);
6271 AssertComRCReturn (autoCaller.rc(), autoCaller.rc());
6272
6273 /* accessing mParent methods below needs mParent lock */
6274 AutoMultiLock <2> alock (mParent->wlock(), this->wlock());
6275
6276 HRESULT rc = S_OK;
6277
6278 // first pass: check accessibility before performing changes
6279 if (!aOnline)
6280 {
6281 for (HDData::HDAttachmentList::const_iterator it = mHDData->mHDAttachments.begin();
6282 it != mHDData->mHDAttachments.end();
6283 ++ it)
6284 {
6285 ComObjPtr <HardDiskAttachment> hda = *it;
6286 ComObjPtr <HardDisk> hd = hda->hardDisk();
6287 AutoLock hdLock (hd);
6288
6289 ComAssertMsgBreak (hd->type() == HardDiskType_NormalHardDisk,
6290 ("Invalid hard disk type %d\n", hd->type()),
6291 rc = E_FAIL);
6292
6293 ComAssertMsgBreak (!hd->isParentImmutable() ||
6294 hd->storageType() == HardDiskStorageType_VirtualDiskImage,
6295 ("Invalid hard disk storage type %d\n", hd->storageType()),
6296 rc = E_FAIL);
6297
6298 Bstr accessError;
6299 rc = hd->getAccessible (accessError);
6300 CheckComRCBreakRC (rc);
6301
6302 if (!accessError.isNull())
6303 {
6304 rc = setError (E_FAIL,
6305 tr ("Hard disk '%ls' is not accessible (%ls)"),
6306 hd->toString().raw(), accessError.raw());
6307 break;
6308 }
6309 }
6310 CheckComRCReturnRC (rc);
6311 }
6312
6313 HDData::HDAttachmentList attachments;
6314
6315 // second pass: perform changes
6316 for (HDData::HDAttachmentList::const_iterator it = mHDData->mHDAttachments.begin();
6317 it != mHDData->mHDAttachments.end();
6318 ++ it)
6319 {
6320 ComObjPtr <HardDiskAttachment> hda = *it;
6321 ComObjPtr <HardDisk> hd = hda->hardDisk();
6322 AutoLock hdLock (hd);
6323
6324 ComObjPtr <HardDisk> parent = hd->parent();
6325 AutoLock parentHdLock (parent);
6326
6327 ComObjPtr <HardDisk> newHd;
6328
6329 // clear busy flag if the VM is online
6330 if (aOnline)
6331 hd->clearBusy();
6332 // increase readers
6333 hd->addReader();
6334
6335 if (hd->isParentImmutable())
6336 {
6337 aProgress->advanceOperation (Bstr (Utf8StrFmt (
6338 tr ("Preserving immutable hard disk '%ls'"),
6339 parent->toString (true /* aShort */).raw())));
6340
6341 parentHdLock.unlock();
6342 alock.leave();
6343
6344 // create a copy of the independent diff
6345 ComObjPtr <HVirtualDiskImage> vdi;
6346 rc = hd->asVDI()->cloneDiffImage (aFolder, mData->mUuid, vdi,
6347 aProgress);
6348 newHd = vdi;
6349
6350 alock.enter();
6351 parentHdLock.lock();
6352
6353 // decrease readers (hd is no more used for reading in any case)
6354 hd->releaseReader();
6355 }
6356 else
6357 {
6358 // checked in the first pass
6359 Assert (hd->type() == HardDiskType_NormalHardDisk);
6360
6361 aProgress->advanceOperation (Bstr (Utf8StrFmt (
6362 tr ("Creating a differencing hard disk for '%ls'"),
6363 hd->root()->toString (true /* aShort */).raw())));
6364
6365 parentHdLock.unlock();
6366 alock.leave();
6367
6368 // create a new diff for the image being attached
6369 ComObjPtr <HVirtualDiskImage> vdi;
6370 rc = hd->createDiffHardDisk (aFolder, mData->mUuid, vdi, aProgress);
6371 newHd = vdi;
6372
6373 alock.enter();
6374 parentHdLock.lock();
6375
6376 if (SUCCEEDED (rc))
6377 {
6378 // if online, hd must keep a reader referece
6379 if (!aOnline)
6380 hd->releaseReader();
6381 }
6382 else
6383 {
6384 // decrease readers
6385 hd->releaseReader();
6386 }
6387 }
6388
6389 if (SUCCEEDED (rc))
6390 {
6391 ComObjPtr <HardDiskAttachment> newHda;
6392 newHda.createObject();
6393 rc = newHda->init (newHd, hda->controller(), hda->deviceNumber(),
6394 false /* aDirty */);
6395
6396 if (SUCCEEDED (rc))
6397 {
6398 // associate the snapshot id with the old hard disk
6399 if (hd->type() != HardDiskType_WritethroughHardDisk && aSnapshotId)
6400 hd->setSnapshotId (*aSnapshotId);
6401
6402 // add the new attachment
6403 attachments.push_back (newHda);
6404
6405 // if online, newHd must be marked as busy
6406 if (aOnline)
6407 newHd->setBusy();
6408 }
6409 }
6410
6411 if (FAILED (rc))
6412 {
6413 // set busy flag back if the VM is online
6414 if (aOnline)
6415 hd->setBusy();
6416 break;
6417 }
6418 }
6419
6420 if (SUCCEEDED (rc))
6421 {
6422 // replace the whole list of attachments with the new one
6423 mHDData->mHDAttachments = attachments;
6424 }
6425 else
6426 {
6427 // delete those diffs we've just created
6428 for (HDData::HDAttachmentList::const_iterator it = attachments.begin();
6429 it != attachments.end();
6430 ++ it)
6431 {
6432 ComObjPtr <HardDisk> hd = (*it)->hardDisk();
6433 AutoLock hdLock (hd);
6434 Assert (hd->children().size() == 0);
6435 Assert (hd->isDifferencing());
6436 // unregisterDiffHardDisk() is supposed to delete and uninit
6437 // the differencing hard disk
6438 mParent->unregisterDiffHardDisk (hd);
6439 }
6440 }
6441
6442 return rc;
6443}
6444
6445/**
6446 * Deletes differencing hard disks created by createSnapshotDiffs() in case
6447 * if snapshot creation was failed.
6448 *
6449 * @param aSnapshot failed snapshot
6450 *
6451 * @note Locks mParent + this object for writing.
6452 */
6453HRESULT Machine::deleteSnapshotDiffs (const ComObjPtr <Snapshot> &aSnapshot)
6454{
6455 AssertReturn (!aSnapshot.isNull(), E_FAIL);
6456
6457 AutoCaller autoCaller (this);
6458 AssertComRCReturn (autoCaller.rc(), autoCaller.rc());
6459
6460 /* accessing mParent methods below needs mParent lock */
6461 AutoMultiLock <2> alock (mParent->wlock(), this->wlock());
6462
6463 /* short cut: check whether attachments are all the same */
6464 if (mHDData->mHDAttachments == aSnapshot->data().mMachine->mHDData->mHDAttachments)
6465 return S_OK;
6466
6467 HRESULT rc = S_OK;
6468
6469 for (HDData::HDAttachmentList::const_iterator it = mHDData->mHDAttachments.begin();
6470 it != mHDData->mHDAttachments.end();
6471 ++ it)
6472 {
6473 ComObjPtr <HardDiskAttachment> hda = *it;
6474 ComObjPtr <HardDisk> hd = hda->hardDisk();
6475 AutoLock hdLock (hd);
6476
6477 ComObjPtr <HardDisk> parent = hd->parent();
6478 AutoLock parentHdLock (parent);
6479
6480 if (!parent || parent->snapshotId() != aSnapshot->data().mId)
6481 continue;
6482
6483 /* must not have children */
6484 ComAssertRet (hd->children().size() == 0, E_FAIL);
6485
6486 /* deassociate the old hard disk from the given snapshot's ID */
6487 parent->setSnapshotId (Guid());
6488
6489 /* unregisterDiffHardDisk() is supposed to delete and uninit
6490 * the differencing hard disk */
6491 rc = mParent->unregisterDiffHardDisk (hd);
6492 /* continue on error */
6493 }
6494
6495 /* restore the whole list of attachments from the failed snapshot */
6496 mHDData->mHDAttachments = aSnapshot->data().mMachine->mHDData->mHDAttachments;
6497
6498 return rc;
6499}
6500
6501/**
6502 * Helper to lock the machine configuration for write access.
6503 *
6504 * @return S_OK or E_FAIL and sets error info on failure
6505 *
6506 * @note Doesn't lock anything (must be called from this object's lock)
6507 */
6508HRESULT Machine::lockConfig()
6509{
6510 HRESULT rc = S_OK;
6511
6512 if (!isConfigLocked())
6513 {
6514 /* open the associated config file */
6515 int vrc = RTFileOpen (&mData->mHandleCfgFile,
6516 Utf8Str (mData->mConfigFileFull),
6517 RTFILE_O_READWRITE | RTFILE_O_OPEN |
6518 RTFILE_O_DENY_WRITE);
6519 if (VBOX_FAILURE (vrc))
6520 mData->mHandleCfgFile = NIL_RTFILE;
6521 }
6522
6523 LogFlowThisFunc (("mConfigFile={%ls}, mHandleCfgFile=%d, rc=%08X\n",
6524 mData->mConfigFileFull.raw(), mData->mHandleCfgFile, rc));
6525 return rc;
6526}
6527
6528/**
6529 * Helper to unlock the machine configuration from write access
6530 *
6531 * @return S_OK
6532 *
6533 * @note Doesn't lock anything.
6534 * @note Not thread safe (must be called from this object's lock).
6535 */
6536HRESULT Machine::unlockConfig()
6537{
6538 HRESULT rc = S_OK;
6539
6540 if (isConfigLocked())
6541 {
6542 RTFileClose(mData->mHandleCfgFile);
6543 mData->mHandleCfgFile = NIL_RTFILE;
6544 }
6545
6546 LogFlowThisFunc (("\n"));
6547
6548 return rc;
6549}
6550
6551/**
6552 * Returns true if the settings file is located in the directory named exactly
6553 * as the machine. This will be true if the machine settings structure was
6554 * created by default in #openConfigLoader().
6555 *
6556 * @param aSettingsDir if not NULL, the full machine settings file directory
6557 * name will be assigned there.
6558 *
6559 * @note Doesn't lock anything.
6560 * @note Not thread safe (must be called from this object's lock).
6561 */
6562bool Machine::isInOwnDir (Utf8Str *aSettingsDir /* = NULL */)
6563{
6564 Utf8Str settingsDir = mData->mConfigFileFull;
6565 RTPathStripFilename (settingsDir.mutableRaw());
6566 char *dirName = RTPathFilename (settingsDir);
6567
6568 AssertReturn (dirName, false);
6569
6570 /* if we don't rename anything on name change, return false shorlty */
6571 if (!mUserData->mNameSync)
6572 return false;
6573
6574 if (aSettingsDir)
6575 *aSettingsDir = settingsDir;
6576
6577 return Bstr (dirName) == mUserData->mName;
6578}
6579
6580/**
6581 * @note Locks objects for reading!
6582 */
6583bool Machine::isModified()
6584{
6585 AutoCaller autoCaller (this);
6586 AssertComRCReturn (autoCaller.rc(), false);
6587
6588 AutoReaderLock alock (this);
6589
6590 for (ULONG slot = 0; slot < ELEMENTS (mNetworkAdapters); slot ++)
6591 if (mNetworkAdapters [slot] && mNetworkAdapters [slot]->isModified())
6592 return true;
6593
6594 return
6595 mUserData.isBackedUp() ||
6596 mHWData.isBackedUp() ||
6597 mHDData.isBackedUp() ||
6598#ifdef VBOX_VRDP
6599 (mVRDPServer && mVRDPServer->isModified()) ||
6600#endif
6601 (mDVDDrive && mDVDDrive->isModified()) ||
6602 (mFloppyDrive && mFloppyDrive->isModified()) ||
6603 (mAudioAdapter && mAudioAdapter->isModified()) ||
6604 (mUSBController && mUSBController->isModified()) ||
6605 (mBIOSSettings && mBIOSSettings->isModified());
6606}
6607
6608/**
6609 * @note This method doesn't check (ignores) actual changes to mHDData.
6610 * Use mHDData.mHDAttachmentsChanged right after #commit() instead.
6611 *
6612 * @param aIgnoreUserData |true| to ignore changes to mUserData
6613 *
6614 * @note Locks objects for reading!
6615 */
6616bool Machine::isReallyModified (bool aIgnoreUserData /* = false */)
6617{
6618 AutoCaller autoCaller (this);
6619 AssertComRCReturn (autoCaller.rc(), false);
6620
6621 AutoReaderLock alock (this);
6622
6623 for (ULONG slot = 0; slot < ELEMENTS (mNetworkAdapters); slot ++)
6624 if (mNetworkAdapters [slot] && mNetworkAdapters [slot]->isReallyModified())
6625 return true;
6626
6627 return
6628 (!aIgnoreUserData && mUserData.hasActualChanges()) ||
6629 mHWData.hasActualChanges() ||
6630 /* ignore mHDData */
6631 //mHDData.hasActualChanges() ||
6632#ifdef VBOX_VRDP
6633 (mVRDPServer && mVRDPServer->isReallyModified()) ||
6634#endif
6635 (mDVDDrive && mDVDDrive->isReallyModified()) ||
6636 (mFloppyDrive && mFloppyDrive->isReallyModified()) ||
6637 (mAudioAdapter && mAudioAdapter->isReallyModified()) ||
6638 (mUSBController && mUSBController->isReallyModified()) ||
6639 (mBIOSSettings && mBIOSSettings->isReallyModified());
6640}
6641
6642/**
6643 * Discards all changes to machine settings.
6644 *
6645 * @param aNotify whether to notify the direct session about changes or not
6646 *
6647 * @note Locks objects!
6648 */
6649void Machine::rollback (bool aNotify)
6650{
6651 AutoCaller autoCaller (this);
6652 AssertComRCReturn (autoCaller.rc(), (void) 0);
6653
6654 AutoLock alock (this);
6655
6656 mUserData.rollback();
6657
6658 mHWData.rollback();
6659
6660 if (mHDData.isBackedUp())
6661 fixupHardDisks (false /* aCommit */);
6662
6663 bool vrdpChanged = false, dvdChanged = false, floppyChanged = false,
6664 usbChanged = false;
6665 ComPtr <INetworkAdapter> networkAdapters [ELEMENTS (mNetworkAdapters)];
6666
6667 if (mBIOSSettings)
6668 mBIOSSettings->rollback();
6669
6670#ifdef VBOX_VRDP
6671 if (mVRDPServer)
6672 vrdpChanged = mVRDPServer->rollback();
6673#endif
6674
6675 if (mDVDDrive)
6676 dvdChanged = mDVDDrive->rollback();
6677
6678 if (mFloppyDrive)
6679 floppyChanged = mFloppyDrive->rollback();
6680
6681 if (mAudioAdapter)
6682 mAudioAdapter->rollback();
6683
6684 if (mUSBController)
6685 usbChanged = mUSBController->rollback();
6686
6687 for (ULONG slot = 0; slot < ELEMENTS (mNetworkAdapters); slot ++)
6688 if (mNetworkAdapters [slot])
6689 if (mNetworkAdapters [slot]->rollback())
6690 networkAdapters [slot] = mNetworkAdapters [slot];
6691
6692 if (aNotify)
6693 {
6694 // inform the direct session about changes
6695
6696 ComObjPtr <Machine> that = this;
6697 alock.leave();
6698
6699 if (vrdpChanged)
6700 that->onVRDPServerChange();
6701 if (dvdChanged)
6702 that->onDVDDriveChange();
6703 if (floppyChanged)
6704 that->onFloppyDriveChange();
6705 if (usbChanged)
6706 that->onUSBControllerChange();
6707 for (ULONG slot = 0; slot < ELEMENTS (networkAdapters); slot ++)
6708 if (networkAdapters [slot])
6709 that->onNetworkAdapterChange (networkAdapters [slot]);
6710 }
6711}
6712
6713/**
6714 * Commits all the changes to machine settings.
6715 *
6716 * Note that when committing fails at some stage, it still continues
6717 * until the end. So, all data will either be actually committed or rolled
6718 * back (for failed cases) and the returned result code will describe the
6719 * first failure encountered. However, #isModified() will still return true
6720 * in case of failure, to indicade that settings in memory and on disk are
6721 * out of sync.
6722 *
6723 * @note Locks objects!
6724 */
6725HRESULT Machine::commit()
6726{
6727 AutoCaller autoCaller (this);
6728 AssertComRCReturn (autoCaller.rc(), autoCaller.rc());
6729
6730 AutoLock alock (this);
6731
6732 HRESULT rc = S_OK;
6733
6734 /*
6735 * use safe commit to ensure Snapshot machines (that share mUserData)
6736 * will still refer to a valid memory location
6737 */
6738 mUserData.commitCopy();
6739
6740 mHWData.commit();
6741
6742 if (mHDData.isBackedUp())
6743 rc = fixupHardDisks (true /* aCommit */);
6744
6745 mBIOSSettings->commit();
6746#ifdef VBOX_VRDP
6747 mVRDPServer->commit();
6748#endif
6749 mDVDDrive->commit();
6750 mFloppyDrive->commit();
6751 mAudioAdapter->commit();
6752 mUSBController->commit();
6753
6754 for (ULONG slot = 0; slot < ELEMENTS (mNetworkAdapters); slot ++)
6755 mNetworkAdapters [slot]->commit();
6756
6757 if (mType == IsSessionMachine)
6758 {
6759 /* attach new data to the primary machine and reshare it */
6760 mPeer->mUserData.attach (mUserData);
6761 mPeer->mHWData.attach (mHWData);
6762 mPeer->mHDData.attach (mHDData);
6763 }
6764
6765 if (FAILED (rc))
6766 {
6767 /*
6768 * backup arbitrary data item to cause #isModified() to still return
6769 * true in case of any error
6770 */
6771 mHWData.backup();
6772 }
6773
6774 return rc;
6775}
6776
6777/**
6778 * Copies all the hardware data from the given machine.
6779 *
6780 * @note
6781 * This method must be called from under this object's lock.
6782 * @note
6783 * This method doesn't call #commit(), so all data remains backed up
6784 * and unsaved.
6785 */
6786void Machine::copyFrom (Machine *aThat)
6787{
6788 AssertReturn (mType == IsMachine || mType == IsSessionMachine, (void) 0);
6789 AssertReturn (aThat->mType == IsSnapshotMachine, (void) 0);
6790
6791 mHWData.assignCopy (aThat->mHWData);
6792
6793 // create copies of all shared folders (mHWData after attiching a copy
6794 // contains just references to original objects)
6795 for (HWData::SharedFolderList::iterator it = mHWData->mSharedFolders.begin();
6796 it != mHWData->mSharedFolders.end();
6797 ++ it)
6798 {
6799 ComObjPtr <SharedFolder> folder;
6800 folder.createObject();
6801 HRESULT rc = folder->initCopy (machine(), *it);
6802 AssertComRC (rc);
6803 *it = folder;
6804 }
6805
6806 mBIOSSettings->copyFrom (aThat->mBIOSSettings);
6807#ifdef VBOX_VRDP
6808 mVRDPServer->copyFrom (aThat->mVRDPServer);
6809#endif
6810 mDVDDrive->copyFrom (aThat->mDVDDrive);
6811 mFloppyDrive->copyFrom (aThat->mFloppyDrive);
6812 mAudioAdapter->copyFrom (aThat->mAudioAdapter);
6813 mUSBController->copyFrom (aThat->mUSBController);
6814
6815 for (ULONG slot = 0; slot < ELEMENTS (mNetworkAdapters); slot ++)
6816 mNetworkAdapters [slot]->copyFrom (aThat->mNetworkAdapters [slot]);
6817}
6818
6819/////////////////////////////////////////////////////////////////////////////
6820// SessionMachine class
6821/////////////////////////////////////////////////////////////////////////////
6822
6823/** Task structure for asynchronous VM operations */
6824struct SessionMachine::Task
6825{
6826 Task (SessionMachine *m, Progress *p)
6827 : machine (m), progress (p)
6828 , state (m->data()->mMachineState) // save the current machine state
6829 , subTask (false), settingsChanged (false)
6830 {}
6831
6832 void modifyLastState (MachineState_T s)
6833 {
6834 *const_cast <MachineState_T *> (&state) = s;
6835 }
6836
6837 virtual void handler() = 0;
6838
6839 const ComObjPtr <SessionMachine> machine;
6840 const ComObjPtr <Progress> progress;
6841 const MachineState_T state;
6842
6843 bool subTask : 1;
6844 bool settingsChanged : 1;
6845};
6846
6847/** Take snapshot task */
6848struct SessionMachine::TakeSnapshotTask : public SessionMachine::Task
6849{
6850 TakeSnapshotTask (SessionMachine *m)
6851 : Task (m, NULL) {}
6852
6853 void handler() { machine->takeSnapshotHandler (*this); }
6854};
6855
6856/** Discard snapshot task */
6857struct SessionMachine::DiscardSnapshotTask : public SessionMachine::Task
6858{
6859 DiscardSnapshotTask (SessionMachine *m, Progress *p, Snapshot *s)
6860 : Task (m, p)
6861 , snapshot (s) {}
6862
6863 DiscardSnapshotTask (const Task &task, Snapshot *s)
6864 : Task (task)
6865 , snapshot (s) {}
6866
6867 void handler() { machine->discardSnapshotHandler (*this); }
6868
6869 const ComObjPtr <Snapshot> snapshot;
6870};
6871
6872/** Discard current state task */
6873struct SessionMachine::DiscardCurrentStateTask : public SessionMachine::Task
6874{
6875 DiscardCurrentStateTask (SessionMachine *m, Progress *p,
6876 bool discardCurSnapshot)
6877 : Task (m, p), discardCurrentSnapshot (discardCurSnapshot) {}
6878
6879 void handler() { machine->discardCurrentStateHandler (*this); }
6880
6881 const bool discardCurrentSnapshot;
6882};
6883
6884////////////////////////////////////////////////////////////////////////////////
6885
6886DEFINE_EMPTY_CTOR_DTOR (SessionMachine)
6887
6888HRESULT SessionMachine::FinalConstruct()
6889{
6890 LogFlowThisFunc (("\n"));
6891
6892 /* set the proper type to indicate we're the SessionMachine instance */
6893 unconst (mType) = IsSessionMachine;
6894
6895#if defined(__WIN__)
6896 mIPCSem = NULL;
6897#elif defined(VBOX_WITH_SYS_V_IPC_SESSION_WATCHER)
6898 mIPCSem = -1;
6899#endif
6900
6901 return S_OK;
6902}
6903
6904void SessionMachine::FinalRelease()
6905{
6906 LogFlowThisFunc (("\n"));
6907
6908 uninit (Uninit::Unexpected);
6909}
6910
6911/**
6912 * @note Must be called only by Machine::openSession() from its own write lock.
6913 */
6914HRESULT SessionMachine::init (Machine *aMachine)
6915{
6916 LogFlowThisFuncEnter();
6917 LogFlowThisFunc (("mName={%ls}\n", aMachine->mUserData->mName.raw()));
6918
6919 AssertReturn (aMachine, E_INVALIDARG);
6920
6921 AssertReturn (aMachine->lockHandle()->isLockedOnCurrentThread(), E_FAIL);
6922
6923 /* Enclose the state transition NotReady->InInit->Ready */
6924 AutoInitSpan autoInitSpan (this);
6925 AssertReturn (autoInitSpan.isOk(), E_UNEXPECTED);
6926
6927 /* create the interprocess semaphore */
6928#if defined(__WIN__)
6929 mIPCSemName = aMachine->mData->mConfigFileFull;
6930 for (size_t i = 0; i < mIPCSemName.length(); i++)
6931 if (mIPCSemName[i] == '\\')
6932 mIPCSemName[i] = '/';
6933 mIPCSem = ::CreateMutex (NULL, FALSE, mIPCSemName);
6934 ComAssertMsgRet (mIPCSem, ("Cannot create IPC mutex, err=0x%08X", ::GetLastError()),
6935 E_FAIL);
6936#elif defined(VBOX_WITH_SYS_V_IPC_SESSION_WATCHER)
6937 Utf8Str configFile = aMachine->mData->mConfigFileFull;
6938 char *pszConfigFile = NULL;
6939 RTStrUtf8ToCurrentCP (&pszConfigFile, configFile);
6940 key_t key = ::ftok (pszConfigFile, 0);
6941 RTStrFree (pszConfigFile);
6942 mIPCSem = ::semget (key, 1, S_IRWXU | S_IRWXG | S_IRWXO | IPC_CREAT);
6943 ComAssertMsgRet (mIPCSem >= 0, ("Cannot create IPC semaphore, errno=%d", errno),
6944 E_FAIL);
6945 /* set the initial value to 1 */
6946 int rv = ::semctl (mIPCSem, 0, SETVAL, 1);
6947 ComAssertMsgRet (rv == 0, ("Cannot init IPC semaphore, errno=%d", errno),
6948 E_FAIL);
6949#endif
6950
6951 /* memorize the peer Machine */
6952 unconst (mPeer) = aMachine;
6953 /* share the parent pointer */
6954 unconst (mParent) = aMachine->mParent;
6955
6956 /* take the pointers to data to share */
6957 mData.share (aMachine->mData);
6958 mSSData.share (aMachine->mSSData);
6959
6960 mUserData.share (aMachine->mUserData);
6961 mHWData.share (aMachine->mHWData);
6962 mHDData.share (aMachine->mHDData);
6963
6964 unconst (mBIOSSettings).createObject();
6965 mBIOSSettings->init (this, aMachine->mBIOSSettings);
6966#ifdef VBOX_VRDP
6967 /* create another VRDPServer object that will be mutable */
6968 unconst (mVRDPServer).createObject();
6969 mVRDPServer->init (this, aMachine->mVRDPServer);
6970#endif
6971 /* create another DVD drive object that will be mutable */
6972 unconst (mDVDDrive).createObject();
6973 mDVDDrive->init (this, aMachine->mDVDDrive);
6974 /* create another floppy drive object that will be mutable */
6975 unconst (mFloppyDrive).createObject();
6976 mFloppyDrive->init (this, aMachine->mFloppyDrive);
6977 /* create another audio adapter object that will be mutable */
6978 unconst (mAudioAdapter).createObject();
6979 mAudioAdapter->init (this, aMachine->mAudioAdapter);
6980 /* create another USB controller object that will be mutable */
6981 unconst (mUSBController).createObject();
6982 mUSBController->init (this, aMachine->mUSBController);
6983 /* create a list of network adapters that will be mutable */
6984 for (ULONG slot = 0; slot < ELEMENTS (mNetworkAdapters); slot ++)
6985 {
6986 unconst (mNetworkAdapters [slot]).createObject();
6987 mNetworkAdapters [slot]->init (this, aMachine->mNetworkAdapters [slot]);
6988 }
6989
6990 /* Confirm a successful initialization when it's the case */
6991 autoInitSpan.setSucceeded();
6992
6993 LogFlowThisFuncLeave();
6994 return S_OK;
6995}
6996
6997/**
6998 * Uninitializes this session object. If the reason is other than
6999 * Uninit::Unexpected, then this method MUST be called from #checkForDeath().
7000 *
7001 * @param aReason uninitialization reason
7002 *
7003 * @note Locks mParent + this object for writing.
7004 */
7005void SessionMachine::uninit (Uninit::Reason aReason)
7006{
7007 LogFlowThisFuncEnter();
7008 LogFlowThisFunc (("reason=%d\n", aReason));
7009
7010 /*
7011 * Strongly reference ourselves to prevent this object deletion after
7012 * mData->mSession.mMachine.setNull() below (which can release the last
7013 * reference and call the destructor). Important: this must be done before
7014 * accessing any members (and before AutoUninitSpan that does it as well).
7015 * This self reference will be released as the very last step on return.
7016 */
7017 ComObjPtr <SessionMachine> selfRef = this;
7018
7019 /* Enclose the state transition Ready->InUninit->NotReady */
7020 AutoUninitSpan autoUninitSpan (this);
7021 if (autoUninitSpan.uninitDone())
7022 {
7023 LogFlowThisFunc (("Already uninitialized\n"));
7024 LogFlowThisFuncLeave();
7025 return;
7026 }
7027
7028 if (autoUninitSpan.initFailed())
7029 {
7030 /*
7031 * We've been called by init() because it's failed. It's not really
7032 * necessary (nor it's safe) to perform the regular uninit sequence
7033 * below, the following is enough.
7034 */
7035 LogFlowThisFunc (("Initialization failed\n"));
7036#if defined(__WIN__)
7037 if (mIPCSem)
7038 ::CloseHandle (mIPCSem);
7039 mIPCSem = NULL;
7040#elif defined(VBOX_WITH_SYS_V_IPC_SESSION_WATCHER)
7041 if (mIPCSem >= 0)
7042 ::semctl (mIPCSem, 0, IPC_RMID);
7043 mIPCSem = -1;
7044#endif
7045 uninitDataAndChildObjects();
7046 unconst (mParent).setNull();
7047 unconst (mPeer).setNull();
7048 LogFlowThisFuncLeave();
7049 return;
7050 }
7051
7052 /*
7053 * We need to lock this object in uninit() because the lock is shared
7054 * with mPeer (as well as data we modify below).
7055 * mParent->addProcessToReap() and others need mParent lock.
7056 */
7057 AutoMultiLock <2> alock (mParent->wlock(), this->wlock());
7058
7059 if (isModified())
7060 {
7061 LogWarningThisFunc (("Discarding unsaved settings changes!\n"));
7062 rollback (false /* aNotify */);
7063 }
7064
7065 Assert (!mSnapshotData.mStateFilePath || !mSnapshotData.mSnapshot);
7066 if (mSnapshotData.mStateFilePath)
7067 {
7068 LogWarningThisFunc (("canceling failed save state request!\n"));
7069 endSavingState (FALSE /* aSuccess */);
7070 }
7071 else if (!!mSnapshotData.mSnapshot)
7072 {
7073 LogWarningThisFunc (("canceling untaken snapshot!\n"));
7074 endTakingSnapshot (FALSE /* aSuccess */);
7075 }
7076
7077 /* release all captured USB devices */
7078 mParent->host()->releaseAllUSBDevices (this);
7079
7080 if (mData->mSession.mPid != NIL_RTPROCESS)
7081 {
7082 /*
7083 * pid is not NIL, meaning this machine's process has been started
7084 * using VirtualBox::OpenRemoteSession(), thus it is our child.
7085 * we need to queue this pid to be reaped (to avoid zombies on Linux)
7086 */
7087 mParent->addProcessToReap (mData->mSession.mPid);
7088 mData->mSession.mPid = NIL_RTPROCESS;
7089 }
7090
7091 if (aReason == Uninit::Unexpected)
7092 {
7093 /*
7094 * uninitialization didn't come from #checkForDeath(), so tell the
7095 * client watcher thread to update the set of machines that have
7096 * open sessions.
7097 */
7098 mParent->updateClientWatcher();
7099 }
7100
7101 /* uninitialize all remote controls */
7102 if (mData->mSession.mRemoteControls.size())
7103 {
7104 LogFlowThisFunc (("Closing remote sessions (%d):\n",
7105 mData->mSession.mRemoteControls.size()));
7106
7107 Data::Session::RemoteControlList::iterator it =
7108 mData->mSession.mRemoteControls.begin();
7109 while (it != mData->mSession.mRemoteControls.end())
7110 {
7111 LogFlowThisFunc ((" Calling remoteControl->Uninitialize()...\n"));
7112 HRESULT rc = (*it)->Uninitialize();
7113 LogFlowThisFunc ((" remoteControl->Uninitialize() returned %08X\n", rc));
7114 if (FAILED (rc))
7115 LogWarningThisFunc (("Forgot to close the remote session?\n"));
7116 ++ it;
7117 }
7118 mData->mSession.mRemoteControls.clear();
7119 }
7120
7121 /*
7122 * An expected uninitialization can come only from #checkForDeath().
7123 * Otherwise it means that something's got really wrong (for examlple,
7124 * the Session implementation has released the VirtualBox reference
7125 * before it triggered #OnSessionEnd(), or before releasing IPC semaphore,
7126 * etc). However, it's also possible, that the client releases the IPC
7127 * semaphore correctly (i.e. before it releases the VirtualBox reference),
7128 * but but the VirtualBox release event comes first to the server process.
7129 * This case is practically possible, so we should not assert on an
7130 * unexpected uninit, just log a warning.
7131 */
7132
7133 if ((aReason == Uninit::Unexpected))
7134 LogWarningThisFunc (("Unexpected SessionMachine uninitialization!\n"));
7135
7136 if (aReason != Uninit::Normal)
7137 mData->mSession.mDirectControl.setNull();
7138 else
7139 {
7140 /* this must be null here (see #OnSessionEnd()) */
7141 Assert (mData->mSession.mDirectControl.isNull());
7142 Assert (mData->mSession.mState == SessionState_SessionClosing);
7143 Assert (!mData->mSession.mProgress.isNull());
7144
7145 mData->mSession.mProgress->notifyComplete (S_OK);
7146 mData->mSession.mProgress.setNull();
7147 }
7148
7149 /* remove the association between the peer machine and this session machine */
7150 Assert (mData->mSession.mMachine == this ||
7151 aReason == Uninit::Unexpected);
7152
7153 /* reset the rest of session data */
7154 mData->mSession.mMachine.setNull();
7155 mData->mSession.mState = SessionState_SessionClosed;
7156
7157 /* close the interprocess semaphore before leaving the shared lock */
7158#if defined(__WIN__)
7159 if (mIPCSem)
7160 ::CloseHandle (mIPCSem);
7161 mIPCSem = NULL;
7162#elif defined(VBOX_WITH_SYS_V_IPC_SESSION_WATCHER)
7163 if (mIPCSem >= 0)
7164 ::semctl (mIPCSem, 0, IPC_RMID);
7165 mIPCSem = -1;
7166#endif
7167
7168 /* fire an event */
7169 mParent->onSessionStateChange (mData->mUuid, SessionState_SessionClosed);
7170
7171 uninitDataAndChildObjects();
7172
7173 /* leave the shared lock before setting the above two to NULL */
7174 alock.leave();
7175
7176 unconst (mParent).setNull();
7177 unconst (mPeer).setNull();
7178
7179 LogFlowThisFuncLeave();
7180}
7181
7182// AutoLock::Lockable interface
7183////////////////////////////////////////////////////////////////////////////////
7184
7185/**
7186 * Overrides VirtualBoxBase::lockHandle() in order to share the lock handle
7187 * with the primary Machine instance (mPeer).
7188 */
7189AutoLock::Handle *SessionMachine::lockHandle() const
7190{
7191 AssertReturn (!mPeer.isNull(), NULL);
7192 return mPeer->lockHandle();
7193}
7194
7195// IInternalMachineControl methods
7196////////////////////////////////////////////////////////////////////////////////
7197
7198/**
7199 * @note Locks the same as #setMachineState() does.
7200 */
7201STDMETHODIMP SessionMachine::UpdateState (MachineState_T machineState)
7202{
7203 return setMachineState (machineState);
7204}
7205
7206/**
7207 * @note Locks this object for reading.
7208 */
7209STDMETHODIMP SessionMachine::GetIPCId (BSTR *id)
7210{
7211 AutoCaller autoCaller (this);
7212 AssertComRCReturn (autoCaller.rc(), autoCaller.rc());
7213
7214 AutoReaderLock alock (this);
7215
7216#if defined(__WIN__)
7217 mIPCSemName.cloneTo (id);
7218 return S_OK;
7219#elif defined(VBOX_WITH_SYS_V_IPC_SESSION_WATCHER)
7220 mData->mConfigFileFull.cloneTo (id);
7221 return S_OK;
7222#else
7223 return S_FAIL;
7224#endif
7225}
7226
7227/**
7228 * @note Locks this object for reading.
7229 */
7230STDMETHODIMP SessionMachine::GetLogFolder (BSTR *aLogFolder)
7231{
7232 AutoCaller autoCaller (this);
7233 AssertComRCReturn (autoCaller.rc(), autoCaller.rc());
7234
7235 AutoReaderLock alock (this);
7236
7237 Utf8Str logFolder;
7238 getLogFolder (logFolder);
7239
7240 Bstr (logFolder).cloneTo (aLogFolder);
7241
7242 return S_OK;
7243}
7244
7245/**
7246 * Goes through the USB filters of the given machine to see if the given
7247 * device matches any filter or not.
7248 *
7249 * @note Locks the same as USBController::hasMatchingFilter() does.
7250 */
7251STDMETHODIMP SessionMachine::RunUSBDeviceFilters (IUSBDevice *aUSBDevice,
7252 BOOL *aMatched)
7253{
7254 if (!aUSBDevice)
7255 return E_INVALIDARG;
7256 if (!aMatched)
7257 return E_POINTER;
7258
7259 AutoCaller autoCaller (this);
7260 AssertComRCReturn (autoCaller.rc(), autoCaller.rc());
7261
7262 *aMatched = mUSBController->hasMatchingFilter (aUSBDevice);
7263
7264 return S_OK;
7265}
7266
7267/**
7268 * @note Locks the same as Host::captureUSBDevice() does.
7269 */
7270STDMETHODIMP SessionMachine::CaptureUSBDevice (INPTR GUIDPARAM aId,
7271 IUSBDevice **aHostDevice)
7272{
7273 if (!aHostDevice)
7274 return E_POINTER;
7275
7276 AutoCaller autoCaller (this);
7277 AssertComRCReturn (autoCaller.rc(), autoCaller.rc());
7278
7279 // if cautureUSBDevice() fails, it must have set extended error info
7280 return mParent->host()->captureUSBDevice (this, aId, aHostDevice);
7281}
7282
7283/**
7284 * @note Locks the same as Host::releaseUSBDevice() does.
7285 */
7286STDMETHODIMP SessionMachine::ReleaseUSBDevice (INPTR GUIDPARAM aId)
7287{
7288 AutoCaller autoCaller (this);
7289 AssertComRCReturn (autoCaller.rc(), autoCaller.rc());
7290
7291 return mParent->host()->releaseUSBDevice (this, aId);
7292}
7293
7294/**
7295 * @note Locks the same as Host::autoCaptureUSBDevices() does.
7296 */
7297STDMETHODIMP SessionMachine::AutoCaptureUSBDevices (IUSBDeviceCollection **aHostDevices)
7298{
7299 AutoCaller autoCaller (this);
7300 AssertComRCReturn (autoCaller.rc(), autoCaller.rc());
7301
7302 return mParent->host()->autoCaptureUSBDevices (this, aHostDevices);
7303}
7304
7305/**
7306 * @note Locks the same as Host::releaseAllUSBDevices() does.
7307 */
7308STDMETHODIMP SessionMachine::ReleaseAllUSBDevices()
7309{
7310 AutoCaller autoCaller (this);
7311 AssertComRCReturn (autoCaller.rc(), autoCaller.rc());
7312
7313 return mParent->host()->releaseAllUSBDevices (this);
7314}
7315
7316/**
7317 * @note Locks mParent + this object for writing.
7318 */
7319STDMETHODIMP SessionMachine::OnSessionEnd (ISession *aSession,
7320 IProgress **aProgress)
7321{
7322 LogFlowThisFuncEnter();
7323
7324 AssertReturn (aSession, E_INVALIDARG);
7325 AssertReturn (aProgress, E_INVALIDARG);
7326
7327 AutoCaller autoCaller (this);
7328
7329 LogFlowThisFunc (("state=%d\n", autoCaller.state()));
7330 /*
7331 * We don't assert below because it might happen that a non-direct session
7332 * informs us it is closed right after we've been uninitialized -- it's ok.
7333 */
7334 CheckComRCReturnRC (autoCaller.rc());
7335
7336 /* get IInternalSessionControl interface */
7337 ComPtr <IInternalSessionControl> control (aSession);
7338
7339 ComAssertRet (!control.isNull(), E_INVALIDARG);
7340
7341 /* Progress::init() needs mParent lock */
7342 AutoMultiLock <2> alock (mParent->wlock(), this->wlock());
7343
7344 if (control.equalsTo (mData->mSession.mDirectControl))
7345 {
7346 ComAssertRet (aProgress, E_POINTER);
7347
7348 /* The direct session is being normally closed by the client process
7349 * ----------------------------------------------------------------- */
7350
7351 /* go to the closing state (essential for all open*Session() calls and
7352 * for #checkForDeath()) */
7353 Assert (mData->mSession.mState == SessionState_SessionOpen);
7354 mData->mSession.mState = SessionState_SessionClosing;
7355
7356 /* set direct control to NULL to release the remote instance */
7357 mData->mSession.mDirectControl.setNull();
7358 LogFlowThisFunc (("Direct control is set to NULL\n"));
7359
7360 /*
7361 * Create the progress object the client will use to wait until
7362 * #checkForDeath() is called to uninitialize this session object
7363 * after it releases the IPC semaphore.
7364 */
7365 ComObjPtr <Progress> progress;
7366 progress.createObject();
7367 progress->init (mParent, (IMachine *) mPeer, Bstr (tr ("Closing session")),
7368 FALSE /* aCancelable */);
7369 progress.queryInterfaceTo (aProgress);
7370 mData->mSession.mProgress = progress;
7371 }
7372 else
7373 {
7374 /* the remote session is being normally closed */
7375 Data::Session::RemoteControlList::iterator it =
7376 mData->mSession.mRemoteControls.begin();
7377 while (it != mData->mSession.mRemoteControls.end())
7378 {
7379 if (control.equalsTo (*it))
7380 break;
7381 ++it;
7382 }
7383 BOOL found = it != mData->mSession.mRemoteControls.end();
7384 ComAssertMsgRet (found, ("The session is not found in the session list!"),
7385 E_INVALIDARG);
7386 mData->mSession.mRemoteControls.remove (*it);
7387 }
7388
7389 LogFlowThisFuncLeave();
7390 return S_OK;
7391}
7392
7393/**
7394 * @note Locks mParent + this object for writing.
7395 */
7396STDMETHODIMP SessionMachine::BeginSavingState (IProgress *aProgress, BSTR *aStateFilePath)
7397{
7398 LogFlowThisFuncEnter();
7399
7400 AssertReturn (aProgress, E_INVALIDARG);
7401 AssertReturn (aStateFilePath, E_POINTER);
7402
7403 AutoCaller autoCaller (this);
7404 AssertComRCReturn (autoCaller.rc(), autoCaller.rc());
7405
7406 /* mParent->addProgress() needs mParent lock */
7407 AutoMultiLock <2> alock (mParent->wlock(), this->wlock());
7408
7409 AssertReturn (mData->mMachineState == MachineState_Paused &&
7410 mSnapshotData.mLastState == MachineState_InvalidMachineState &&
7411 mSnapshotData.mProgressId.isEmpty() &&
7412 mSnapshotData.mStateFilePath.isNull(),
7413 E_FAIL);
7414
7415 /* memorize the progress ID and add it to the global collection */
7416 Guid progressId;
7417 HRESULT rc = aProgress->COMGETTER(Id) (progressId.asOutParam());
7418 AssertComRCReturn (rc, rc);
7419 rc = mParent->addProgress (aProgress);
7420 AssertComRCReturn (rc, rc);
7421
7422 Bstr stateFilePath;
7423 /* stateFilePath is null when the machine is not running */
7424 if (mData->mMachineState == MachineState_Paused)
7425 {
7426 stateFilePath = Utf8StrFmt ("%ls%c{%Vuuid}.sav",
7427 mUserData->mSnapshotFolderFull.raw(),
7428 RTPATH_DELIMITER, mData->mUuid.raw());
7429 }
7430
7431 /* fill in the snapshot data */
7432 mSnapshotData.mLastState = mData->mMachineState;
7433 mSnapshotData.mProgressId = progressId;
7434 mSnapshotData.mStateFilePath = stateFilePath;
7435
7436 /* set the state to Saving (this is expected by Console::SaveState()) */
7437 setMachineState (MachineState_Saving);
7438
7439 stateFilePath.cloneTo (aStateFilePath);
7440
7441 return S_OK;
7442}
7443
7444/**
7445 * @note Locks mParent + this objects for writing.
7446 */
7447STDMETHODIMP SessionMachine::EndSavingState (BOOL aSuccess)
7448{
7449 LogFlowThisFunc (("\n"));
7450
7451 AutoCaller autoCaller (this);
7452 AssertComRCReturn (autoCaller.rc(), autoCaller.rc());
7453
7454 /* endSavingState() need mParent lock */
7455 AutoMultiLock <2> alock (mParent->wlock(), this->wlock());
7456
7457 AssertReturn (mData->mMachineState == MachineState_Saving &&
7458 mSnapshotData.mLastState != MachineState_InvalidMachineState &&
7459 !mSnapshotData.mProgressId.isEmpty() &&
7460 !mSnapshotData.mStateFilePath.isNull(),
7461 E_FAIL);
7462
7463 /*
7464 * on success, set the state to Saved;
7465 * on failure, set the state to the state we had when BeginSavingState() was
7466 * called (this is expected by Console::SaveState() and
7467 * Console::saveStateThread())
7468 */
7469 if (aSuccess)
7470 setMachineState (MachineState_Saved);
7471 else
7472 setMachineState (mSnapshotData.mLastState);
7473
7474 return endSavingState (aSuccess);
7475}
7476
7477/**
7478 * @note Locks mParent + this objects for writing.
7479 */
7480STDMETHODIMP SessionMachine::BeginTakingSnapshot (
7481 IConsole *aInitiator, INPTR BSTR aName, INPTR BSTR aDescription,
7482 IProgress *aProgress, BSTR *aStateFilePath,
7483 IProgress **aServerProgress)
7484{
7485 LogFlowThisFuncEnter();
7486
7487 AssertReturn (aInitiator && aName, E_INVALIDARG);
7488 AssertReturn (aStateFilePath && aServerProgress, E_POINTER);
7489
7490 LogFlowThisFunc (("aName='%ls'\n", aName));
7491
7492 AutoCaller autoCaller (this);
7493 AssertComRCReturn (autoCaller.rc(), autoCaller.rc());
7494
7495 /* Progress::init() needs mParent lock */
7496 AutoMultiLock <2> alock (mParent->wlock(), this->wlock());
7497
7498 AssertReturn ((mData->mMachineState < MachineState_Running ||
7499 mData->mMachineState == MachineState_Paused) &&
7500 mSnapshotData.mLastState == MachineState_InvalidMachineState &&
7501 mSnapshotData.mSnapshot.isNull() &&
7502 mSnapshotData.mServerProgress.isNull() &&
7503 mSnapshotData.mCombinedProgress.isNull(),
7504 E_FAIL);
7505
7506 bool takingSnapshotOnline = mData->mMachineState == MachineState_Paused;
7507
7508 if (!takingSnapshotOnline && mData->mMachineState != MachineState_Saved)
7509 {
7510 /*
7511 * save all current settings to ensure current changes are committed
7512 * and hard disks are fixed up
7513 */
7514 HRESULT rc = saveSettings();
7515 CheckComRCReturnRC (rc);
7516 }
7517
7518 /* check that there are no Writethrough hard disks attached */
7519 for (HDData::HDAttachmentList::const_iterator
7520 it = mHDData->mHDAttachments.begin();
7521 it != mHDData->mHDAttachments.end();
7522 ++ it)
7523 {
7524 ComObjPtr <HardDisk> hd = (*it)->hardDisk();
7525 AutoLock hdLock (hd);
7526 if (hd->type() == HardDiskType_WritethroughHardDisk)
7527 return setError (E_FAIL,
7528 tr ("Cannot take a snapshot when there is a Writethrough hard "
7529 " disk attached ('%ls')"), hd->toString().raw());
7530 }
7531
7532 AssertReturn (aProgress || !takingSnapshotOnline, E_FAIL);
7533
7534 /* create an ID for the snapshot */
7535 Guid snapshotId;
7536 snapshotId.create();
7537
7538 Bstr stateFilePath;
7539 /* stateFilePath is null when the machine is not online nor saved */
7540 if (takingSnapshotOnline || mData->mMachineState == MachineState_Saved)
7541 stateFilePath = Utf8StrFmt ("%ls%c{%Vuuid}.sav",
7542 mUserData->mSnapshotFolderFull.raw(),
7543 RTPATH_DELIMITER,
7544 snapshotId.ptr());
7545
7546 /* ensure the directory for the saved state file exists */
7547 if (stateFilePath)
7548 {
7549 Utf8Str dir = stateFilePath;
7550 RTPathStripFilename (dir.mutableRaw());
7551 if (!RTDirExists (dir))
7552 {
7553 int vrc = RTDirCreateFullPath (dir, 0777);
7554 if (VBOX_FAILURE (vrc))
7555 return setError (E_FAIL,
7556 tr ("Could not create a directory '%s' to save the "
7557 "VM state to (%Vrc)"),
7558 dir.raw(), vrc);
7559 }
7560 }
7561
7562 /* create a snapshot machine object */
7563 ComObjPtr <SnapshotMachine> snapshotMachine;
7564 snapshotMachine.createObject();
7565 HRESULT rc = snapshotMachine->init (this, snapshotId, stateFilePath);
7566 AssertComRCReturn (rc, rc);
7567
7568 Bstr progressDesc = Bstr (tr ("Taking snapshot of virtual machine"));
7569 Bstr firstOpDesc = Bstr (tr ("Preparing to take snapshot"));
7570
7571 /*
7572 * create a server-side progress object (it will be descriptionless
7573 * when we need to combine it with the VM-side progress, i.e. when we're
7574 * taking a snapshot online). The number of operations is:
7575 * 1 (preparing) + # of VDIs + 1 (if the state is saved so we need to copy it)
7576 */
7577 ComObjPtr <Progress> serverProgress;
7578 {
7579 ULONG opCount = 1 + mHDData->mHDAttachments.size();
7580 if (mData->mMachineState == MachineState_Saved)
7581 opCount ++;
7582 serverProgress.createObject();
7583 if (takingSnapshotOnline)
7584 rc = serverProgress->init (FALSE, opCount, firstOpDesc);
7585 else
7586 rc = serverProgress->init (mParent, aInitiator, progressDesc, FALSE,
7587 opCount, firstOpDesc);
7588 AssertComRCReturn (rc, rc);
7589 }
7590
7591 /* create a combined server-side progress object when necessary */
7592 ComObjPtr <CombinedProgress> combinedProgress;
7593 if (takingSnapshotOnline)
7594 {
7595 combinedProgress.createObject();
7596 rc = combinedProgress->init (mParent, aInitiator, progressDesc,
7597 serverProgress, aProgress);
7598 AssertComRCReturn (rc, rc);
7599 }
7600
7601 /* create a snapshot object */
7602 RTTIMESPEC time;
7603 ComObjPtr <Snapshot> snapshot;
7604 snapshot.createObject();
7605 rc = snapshot->init (snapshotId, aName, aDescription,
7606 RTTimeSpecGetMilli (RTTimeNow (&time)),
7607 snapshotMachine, mData->mCurrentSnapshot);
7608 AssertComRCReturn (rc, rc);
7609
7610 /*
7611 * create and start the task on a separate thread
7612 * (note that it will not start working until we release alock)
7613 */
7614 TakeSnapshotTask *task = new TakeSnapshotTask (this);
7615 int vrc = RTThreadCreate (NULL, taskHandler,
7616 (void *) task,
7617 0, RTTHREADTYPE_MAIN_WORKER, 0, "TakeSnapshot");
7618 if (VBOX_FAILURE (vrc))
7619 {
7620 snapshot->uninit();
7621 delete task;
7622 ComAssertFailedRet (E_FAIL);
7623 }
7624
7625 /* fill in the snapshot data */
7626 mSnapshotData.mLastState = mData->mMachineState;
7627 mSnapshotData.mSnapshot = snapshot;
7628 mSnapshotData.mServerProgress = serverProgress;
7629 mSnapshotData.mCombinedProgress = combinedProgress;
7630
7631 /* set the state to Saving (this is expected by Console::TakeSnapshot()) */
7632 setMachineState (MachineState_Saving);
7633
7634 if (takingSnapshotOnline)
7635 stateFilePath.cloneTo (aStateFilePath);
7636 else
7637 *aStateFilePath = NULL;
7638
7639 serverProgress.queryInterfaceTo (aServerProgress);
7640
7641 LogFlowThisFuncLeave();
7642 return S_OK;
7643}
7644
7645/**
7646 * @note Locks mParent + this objects for writing.
7647 */
7648STDMETHODIMP SessionMachine::EndTakingSnapshot (BOOL aSuccess)
7649{
7650 LogFlowThisFunc (("\n"));
7651
7652 AutoCaller autoCaller (this);
7653 AssertComRCReturn (autoCaller.rc(), autoCaller.rc());
7654
7655 /* Lock mParent because of endTakingSnapshot() */
7656 AutoMultiLock <2> alock (mParent->wlock(), this->wlock());
7657
7658 AssertReturn (!aSuccess ||
7659 (mData->mMachineState == MachineState_Saving &&
7660 mSnapshotData.mLastState != MachineState_InvalidMachineState &&
7661 !mSnapshotData.mSnapshot.isNull() &&
7662 !mSnapshotData.mServerProgress.isNull() &&
7663 !mSnapshotData.mCombinedProgress.isNull()),
7664 E_FAIL);
7665
7666 /*
7667 * set the state to the state we had when BeginTakingSnapshot() was called
7668 * (this is expected by Console::TakeSnapshot() and
7669 * Console::saveStateThread())
7670 */
7671 setMachineState (mSnapshotData.mLastState);
7672
7673 return endTakingSnapshot (aSuccess);
7674}
7675
7676/**
7677 * @note Locks mParent + this + children objects for writing!
7678 */
7679STDMETHODIMP SessionMachine::DiscardSnapshot (
7680 IConsole *aInitiator, INPTR GUIDPARAM aId,
7681 MachineState_T *aMachineState, IProgress **aProgress)
7682{
7683 LogFlowThisFunc (("\n"));
7684
7685 Guid id = aId;
7686 AssertReturn (aInitiator && !id.isEmpty(), E_INVALIDARG);
7687 AssertReturn (aMachineState && aProgress, E_POINTER);
7688
7689 AutoCaller autoCaller (this);
7690 AssertComRCReturn (autoCaller.rc(), autoCaller.rc());
7691
7692 /* Progress::init() needs mParent lock */
7693 AutoMultiLock <2> alock (mParent->wlock(), this->wlock());
7694
7695 ComAssertRet (mData->mMachineState < MachineState_Running, E_FAIL);
7696
7697 ComObjPtr <Snapshot> snapshot;
7698 HRESULT rc = findSnapshot (id, snapshot, true /* aSetError */);
7699 CheckComRCReturnRC (rc);
7700
7701 AutoLock snapshotLock (snapshot);
7702 if (snapshot == mData->mFirstSnapshot)
7703 {
7704 AutoLock chLock (mData->mFirstSnapshot->childrenLock());
7705 size_t childrenCount = mData->mFirstSnapshot->children().size();
7706 if (childrenCount > 1)
7707 return setError (E_FAIL,
7708 tr ("Cannot discard the snapshot '%ls' because it is the first "
7709 "snapshot of the machine '%ls' and it has more than one "
7710 "child snapshot (%d)"),
7711 snapshot->data().mName.raw(), mUserData->mName.raw(),
7712 childrenCount);
7713 }
7714
7715 /*
7716 * If the snapshot being discarded is the current one, ensure current
7717 * settings are committed and saved.
7718 */
7719 if (snapshot == mData->mCurrentSnapshot)
7720 {
7721 if (isModified())
7722 {
7723 rc = saveSettings();
7724 CheckComRCReturnRC (rc);
7725 }
7726 }
7727
7728 /*
7729 * create a progress object. The number of operations is:
7730 * 1 (preparing) + # of VDIs
7731 */
7732 ComObjPtr <Progress> progress;
7733 progress.createObject();
7734 rc = progress->init (mParent, aInitiator,
7735 Bstr (Utf8StrFmt (tr ("Discarding snapshot '%ls'"),
7736 snapshot->data().mName.raw())),
7737 FALSE /* aCancelable */,
7738 1 + snapshot->data().mMachine->mHDData->mHDAttachments.size(),
7739 Bstr (tr ("Preparing to discard snapshot")));
7740 AssertComRCReturn (rc, rc);
7741
7742 /* create and start the task on a separate thread */
7743 DiscardSnapshotTask *task = new DiscardSnapshotTask (this, progress, snapshot);
7744 int vrc = RTThreadCreate (NULL, taskHandler,
7745 (void *) task,
7746 0, RTTHREADTYPE_MAIN_WORKER, 0, "DiscardSnapshot");
7747 if (VBOX_FAILURE (vrc))
7748 delete task;
7749 ComAssertRCRet (vrc, E_FAIL);
7750
7751 /* set the proper machine state (note: after creating a Task instance) */
7752 setMachineState (MachineState_Discarding);
7753
7754 /* return the progress to the caller */
7755 progress.queryInterfaceTo (aProgress);
7756
7757 /* return the new state to the caller */
7758 *aMachineState = mData->mMachineState;
7759
7760 return S_OK;
7761}
7762
7763/**
7764 * @note Locks mParent + this + children objects for writing!
7765 */
7766STDMETHODIMP SessionMachine::DiscardCurrentState (
7767 IConsole *aInitiator, MachineState_T *aMachineState, IProgress **aProgress)
7768{
7769 LogFlowThisFunc (("\n"));
7770
7771 AssertReturn (aInitiator, E_INVALIDARG);
7772 AssertReturn (aMachineState && aProgress, E_POINTER);
7773
7774 AutoCaller autoCaller (this);
7775 AssertComRCReturn (autoCaller.rc(), autoCaller.rc());
7776
7777 /* Progress::init() needs mParent lock */
7778 AutoMultiLock <2> alock (mParent->wlock(), this->wlock());
7779
7780 ComAssertRet (mData->mMachineState < MachineState_Running, E_FAIL);
7781
7782 if (mData->mCurrentSnapshot.isNull())
7783 return setError (E_FAIL,
7784 tr ("Could not discard the current state of the machine '%ls' "
7785 "because it doesn't have any snapshots"),
7786 mUserData->mName.raw());
7787
7788 /*
7789 * create a progress object. The number of operations is:
7790 * 1 (preparing) + # of VDIs + 1 (if we need to copy the saved state file)
7791 */
7792 ComObjPtr <Progress> progress;
7793 progress.createObject();
7794 {
7795 ULONG opCount = 1 + mData->mCurrentSnapshot->data()
7796 .mMachine->mHDData->mHDAttachments.size();
7797 if (mData->mCurrentSnapshot->stateFilePath())
7798 ++ opCount;
7799 progress->init (mParent, aInitiator,
7800 Bstr (tr ("Discarding current machine state")),
7801 FALSE /* aCancelable */, opCount,
7802 Bstr (tr ("Preparing to discard current state")));
7803 }
7804
7805 /* create and start the task on a separate thread */
7806 DiscardCurrentStateTask *task =
7807 new DiscardCurrentStateTask (this, progress, false /* discardCurSnapshot */);
7808 int vrc = RTThreadCreate (NULL, taskHandler,
7809 (void *) task,
7810 0, RTTHREADTYPE_MAIN_WORKER, 0, "DiscardCurState");
7811 if (VBOX_FAILURE (vrc))
7812 delete task;
7813 ComAssertRCRet (vrc, E_FAIL);
7814
7815 /* set the proper machine state (note: after creating a Task instance) */
7816 setMachineState (MachineState_Discarding);
7817
7818 /* return the progress to the caller */
7819 progress.queryInterfaceTo (aProgress);
7820
7821 /* return the new state to the caller */
7822 *aMachineState = mData->mMachineState;
7823
7824 return S_OK;
7825}
7826
7827/**
7828 * @note Locks mParent + other objects for writing!
7829 */
7830STDMETHODIMP SessionMachine::DiscardCurrentSnapshotAndState (
7831 IConsole *aInitiator, MachineState_T *aMachineState, IProgress **aProgress)
7832{
7833 LogFlowThisFunc (("\n"));
7834
7835 AssertReturn (aInitiator, E_INVALIDARG);
7836 AssertReturn (aMachineState && aProgress, E_POINTER);
7837
7838 AutoCaller autoCaller (this);
7839 AssertComRCReturn (autoCaller.rc(), autoCaller.rc());
7840
7841 /* Progress::init() needs mParent lock */
7842 AutoMultiLock <2> alock (mParent->wlock(), this->wlock());
7843
7844 ComAssertRet (mData->mMachineState < MachineState_Running, E_FAIL);
7845
7846 if (mData->mCurrentSnapshot.isNull())
7847 return setError (E_FAIL,
7848 tr ("Could not discard the current state of the machine '%ls' "
7849 "because it doesn't have any snapshots"),
7850 mUserData->mName.raw());
7851
7852 /*
7853 * create a progress object. The number of operations is:
7854 * 1 (preparing) + # of VDIs in the current snapshot +
7855 * # of VDIs in the previous snapshot +
7856 * 1 (if we need to copy the saved state file of the previous snapshot)
7857 * or (if there is no previous snapshot):
7858 * 1 (preparing) + # of VDIs in the current snapshot * 2 +
7859 * 1 (if we need to copy the saved state file of the current snapshot)
7860 */
7861 ComObjPtr <Progress> progress;
7862 progress.createObject();
7863 {
7864 ComObjPtr <Snapshot> curSnapshot = mData->mCurrentSnapshot;
7865 ComObjPtr <Snapshot> prevSnapshot = mData->mCurrentSnapshot->parent();
7866
7867 ULONG opCount = 1;
7868 if (prevSnapshot)
7869 {
7870 opCount += curSnapshot->data().mMachine->mHDData->mHDAttachments.size();
7871 opCount += prevSnapshot->data().mMachine->mHDData->mHDAttachments.size();
7872 if (prevSnapshot->stateFilePath())
7873 ++ opCount;
7874 }
7875 else
7876 {
7877 opCount += curSnapshot->data().mMachine->mHDData->mHDAttachments.size() * 2;
7878 if (curSnapshot->stateFilePath())
7879 ++ opCount;
7880 }
7881
7882 progress->init (mParent, aInitiator,
7883 Bstr (tr ("Discarding current machine snapshot and state")),
7884 FALSE /* aCancelable */, opCount,
7885 Bstr (tr ("Preparing to discard current snapshot and state")));
7886 }
7887
7888 /* create and start the task on a separate thread */
7889 DiscardCurrentStateTask *task =
7890 new DiscardCurrentStateTask (this, progress, true /* discardCurSnapshot */);
7891 int vrc = RTThreadCreate (NULL, taskHandler,
7892 (void *) task,
7893 0, RTTHREADTYPE_MAIN_WORKER, 0, "DiscardCurState");
7894 if (VBOX_FAILURE (vrc))
7895 delete task;
7896 ComAssertRCRet (vrc, E_FAIL);
7897
7898 /* set the proper machine state (note: after creating a Task instance) */
7899 setMachineState (MachineState_Discarding);
7900
7901 /* return the progress to the caller */
7902 progress.queryInterfaceTo (aProgress);
7903
7904 /* return the new state to the caller */
7905 *aMachineState = mData->mMachineState;
7906
7907 return S_OK;
7908}
7909
7910// public methods only for internal purposes
7911/////////////////////////////////////////////////////////////////////////////
7912
7913/**
7914 * Called from the client watcher thread to check for unexpected client
7915 * process death.
7916 *
7917 * @note On Win32, this method is called only when we've got the semaphore
7918 * (i.e. it has been signaled when we were waiting for it).
7919 *
7920 * On Win32, this method always returns true.
7921 *
7922 * On Linux, the method returns true if the client process has terminated
7923 * abnormally (and/or the session has been uninitialized) and false if it is
7924 * still alive.
7925 *
7926 * @note Locks this object for writing.
7927 */
7928bool SessionMachine::checkForDeath()
7929{
7930 Uninit::Reason reason;
7931 bool doUninit = false;
7932 bool rc = false;
7933
7934 /*
7935 * Enclose autoCaller with a block because calling uninit()
7936 * from under it will deadlock.
7937 */
7938 {
7939 AutoCaller autoCaller (this);
7940 if (!autoCaller.isOk())
7941 {
7942 /*
7943 * return true if not ready, to cause the client watcher to exclude
7944 * the corresponding session from watching
7945 */
7946 LogFlowThisFunc (("Already uninitialized!"));
7947 return true;
7948 }
7949
7950 AutoLock alock (this);
7951
7952 /*
7953 * Determine the reason of death: if the session state is Closing here,
7954 * everything is fine. Otherwise it means that the client did not call
7955 * OnSessionEnd() before it released the IPC semaphore.
7956 * This may happen either because the client process has abnormally
7957 * terminated, or because it simply forgot to call ISession::Close()
7958 * before exiting. We threat the latter also as an abnormal termination
7959 * (see Session::uninit() for details).
7960 */
7961 reason = mData->mSession.mState == SessionState_SessionClosing ?
7962 Uninit::Normal :
7963 Uninit::Abnormal;
7964
7965#if defined(__WIN__)
7966
7967 AssertMsg (mIPCSem, ("semaphore must be created"));
7968
7969 if (reason == Uninit::Abnormal)
7970 {
7971 LogWarningThisFunc (("ABNORMAL client termination! (wasRunning=%d)\n",
7972 mData->mMachineState >= MachineState_Running));
7973
7974 /* reset the state to Aborted */
7975 if (mData->mMachineState != MachineState_Aborted)
7976 setMachineState (MachineState_Aborted);
7977 }
7978
7979 /* release the IPC mutex */
7980 ::ReleaseMutex (mIPCSem);
7981
7982 doUninit = true;
7983
7984 rc = true;
7985
7986#elif defined(VBOX_WITH_SYS_V_IPC_SESSION_WATCHER)
7987
7988 AssertMsg (mIPCSem >= 0, ("semaphore must be created"));
7989
7990 int val = ::semctl (mIPCSem, 0, GETVAL);
7991 if (val > 0)
7992 {
7993 /* the semaphore is signaled, meaning the session is terminated */
7994
7995 if (reason == Uninit::Abnormal)
7996 {
7997 LogWarningThisFunc (("ABNORMAL client termination! (wasRunning=%d)\n",
7998 mData->mMachineState >= MachineState_Running));
7999
8000 /* reset the state to Aborted */
8001 if (mData->mMachineState != MachineState_Aborted)
8002 setMachineState (MachineState_Aborted);
8003 }
8004
8005 doUninit = true;
8006 }
8007
8008 rc = val > 0;
8009
8010#endif
8011
8012 } /* AutoCaller block */
8013
8014 if (doUninit)
8015 uninit (reason);
8016
8017 return rc;
8018}
8019
8020/**
8021 * @note Locks this object for reading.
8022 */
8023HRESULT SessionMachine::onDVDDriveChange()
8024{
8025 LogFlowThisFunc (("\n"));
8026
8027 AutoCaller autoCaller (this);
8028 AssertComRCReturn (autoCaller.rc(), autoCaller.rc());
8029
8030 ComPtr <IInternalSessionControl> directControl;
8031 {
8032 AutoReaderLock alock (this);
8033 directControl = mData->mSession.mDirectControl;
8034 }
8035
8036 /* ignore notifications sent after #OnSessionEnd() is called */
8037 if (!directControl)
8038 return S_OK;
8039
8040 return directControl->OnDVDDriveChange();
8041}
8042
8043/**
8044 * @note Locks this object for reading.
8045 */
8046HRESULT SessionMachine::onFloppyDriveChange()
8047{
8048 LogFlowThisFunc (("\n"));
8049
8050 AutoCaller autoCaller (this);
8051 AssertComRCReturn (autoCaller.rc(), autoCaller.rc());
8052
8053 ComPtr <IInternalSessionControl> directControl;
8054 {
8055 AutoReaderLock alock (this);
8056 directControl = mData->mSession.mDirectControl;
8057 }
8058
8059 /* ignore notifications sent after #OnSessionEnd() is called */
8060 if (!directControl)
8061 return S_OK;
8062
8063 return directControl->OnFloppyDriveChange();
8064}
8065
8066/**
8067 * @note Locks this object for reading.
8068 */
8069HRESULT SessionMachine::onNetworkAdapterChange(INetworkAdapter *networkAdapter)
8070{
8071 LogFlowThisFunc (("\n"));
8072
8073 AutoCaller autoCaller (this);
8074 AssertComRCReturn (autoCaller.rc(), autoCaller.rc());
8075
8076 ComPtr <IInternalSessionControl> directControl;
8077 {
8078 AutoReaderLock alock (this);
8079 directControl = mData->mSession.mDirectControl;
8080 }
8081
8082 /* ignore notifications sent after #OnSessionEnd() is called */
8083 if (!directControl)
8084 return S_OK;
8085
8086 return directControl->OnNetworkAdapterChange(networkAdapter);
8087}
8088
8089/**
8090 * @note Locks this object for reading.
8091 */
8092HRESULT SessionMachine::onVRDPServerChange()
8093{
8094 LogFlowThisFunc (("\n"));
8095
8096 AutoCaller autoCaller (this);
8097 AssertComRCReturn (autoCaller.rc(), autoCaller.rc());
8098
8099 ComPtr <IInternalSessionControl> directControl;
8100 {
8101 AutoReaderLock alock (this);
8102 directControl = mData->mSession.mDirectControl;
8103 }
8104
8105 /* ignore notifications sent after #OnSessionEnd() is called */
8106 if (!directControl)
8107 return S_OK;
8108
8109 return directControl->OnVRDPServerChange();
8110}
8111
8112/**
8113 * @note Locks this object for reading.
8114 */
8115HRESULT SessionMachine::onUSBControllerChange()
8116{
8117 LogFlowThisFunc (("\n"));
8118
8119 AutoCaller autoCaller (this);
8120 AssertComRCReturn (autoCaller.rc(), autoCaller.rc());
8121
8122 ComPtr <IInternalSessionControl> directControl;
8123 {
8124 AutoReaderLock alock (this);
8125 directControl = mData->mSession.mDirectControl;
8126 }
8127
8128 /* ignore notifications sent after #OnSessionEnd() is called */
8129 if (!directControl)
8130 return S_OK;
8131
8132 return directControl->OnUSBControllerChange();
8133}
8134
8135/**
8136 * @note Locks this object for reading.
8137 */
8138HRESULT SessionMachine::onUSBDeviceAttach (IUSBDevice *aDevice)
8139{
8140 LogFlowThisFunc (("\n"));
8141
8142 AutoCaller autoCaller (this);
8143 AssertComRCReturn (autoCaller.rc(), autoCaller.rc());
8144
8145 ComPtr <IInternalSessionControl> directControl;
8146 {
8147 AutoReaderLock alock (this);
8148 directControl = mData->mSession.mDirectControl;
8149 }
8150
8151 /* ignore notifications sent after #OnSessionEnd() is called */
8152 if (!directControl)
8153 return S_OK;
8154
8155 return directControl->OnUSBDeviceAttach (aDevice);
8156}
8157
8158/**
8159 * @note Locks this object for reading.
8160 */
8161HRESULT SessionMachine::onUSBDeviceDetach (INPTR GUIDPARAM aId)
8162{
8163 LogFlowThisFunc (("\n"));
8164
8165 AutoCaller autoCaller (this);
8166 AssertComRCReturn (autoCaller.rc(), autoCaller.rc());
8167
8168 ComPtr <IInternalSessionControl> directControl;
8169 {
8170 AutoReaderLock alock (this);
8171 directControl = mData->mSession.mDirectControl;
8172 }
8173
8174 /* ignore notifications sent after #OnSessionEnd() is called */
8175 if (!directControl)
8176 return S_OK;
8177
8178 return directControl->OnUSBDeviceDetach (aId);
8179}
8180
8181// protected methods
8182/////////////////////////////////////////////////////////////////////////////
8183
8184/**
8185 * Helper method to finalize saving the state.
8186 *
8187 * @note Must be called from under this object's lock.
8188 *
8189 * @param aSuccess TRUE if the snapshot has been taken successfully
8190 *
8191 * @note Locks mParent + this objects for writing.
8192 */
8193HRESULT SessionMachine::endSavingState (BOOL aSuccess)
8194{
8195 LogFlowThisFuncEnter();
8196
8197 AutoCaller autoCaller (this);
8198 AssertComRCReturn (autoCaller.rc(), autoCaller.rc());
8199
8200 /* mParent->removeProgress() needs mParent lock */
8201 AutoMultiLock <2> alock (mParent->wlock(), this->wlock());
8202
8203 HRESULT rc = S_OK;
8204
8205 if (aSuccess)
8206 {
8207 mSSData->mStateFilePath = mSnapshotData.mStateFilePath;
8208
8209 /* save all VM settings */
8210 rc = saveSettings();
8211 }
8212 else
8213 {
8214 /* delete the saved state file (it might have been already created) */
8215 RTFileDelete (Utf8Str (mSnapshotData.mStateFilePath));
8216 }
8217
8218 /* remove the completed progress object */
8219 mParent->removeProgress (mSnapshotData.mProgressId);
8220
8221 /* clear out the temporary saved state data */
8222 mSnapshotData.mLastState = MachineState_InvalidMachineState;
8223 mSnapshotData.mProgressId.clear();
8224 mSnapshotData.mStateFilePath.setNull();
8225
8226 LogFlowThisFuncLeave();
8227 return rc;
8228}
8229
8230/**
8231 * Helper method to finalize taking a snapshot.
8232 * Gets called only from #EndTakingSnapshot() that is expected to
8233 * be called by the VM process when it finishes *all* the tasks related to
8234 * taking a snapshot, either scucessfully or unsuccessfilly.
8235 *
8236 * @param aSuccess TRUE if the snapshot has been taken successfully
8237 *
8238 * @note Locks mParent + this objects for writing.
8239 */
8240HRESULT SessionMachine::endTakingSnapshot (BOOL aSuccess)
8241{
8242 LogFlowThisFuncEnter();
8243
8244 AutoCaller autoCaller (this);
8245 AssertComRCReturn (autoCaller.rc(), autoCaller.rc());
8246
8247 /* Progress object uninitialization needs mParent lock */
8248 AutoMultiLock <2> alock (mParent->wlock(), this->wlock());
8249
8250 HRESULT rc = S_OK;
8251
8252 if (aSuccess)
8253 {
8254 /* the server progress must be completed on success */
8255 Assert (mSnapshotData.mServerProgress->completed());
8256
8257 mData->mCurrentSnapshot = mSnapshotData.mSnapshot;
8258 /* memorize the first snapshot if necessary */
8259 if (!mData->mFirstSnapshot)
8260 mData->mFirstSnapshot = mData->mCurrentSnapshot;
8261
8262 int opFlags = SaveSS_AddOp | SaveSS_UpdateCurrentId;
8263 if (mSnapshotData.mLastState != MachineState_Paused && !isModified())
8264 {
8265 /*
8266 * the machine was powered off or saved when taking a snapshot,
8267 * so reset the mCurrentStateModified flag
8268 */
8269 mData->mCurrentStateModified = FALSE;
8270 opFlags |= SaveSS_UpdateCurStateModified;
8271 }
8272
8273 rc = saveSnapshotSettings (mSnapshotData.mSnapshot, opFlags);
8274 }
8275
8276 if (!aSuccess || FAILED (rc))
8277 {
8278 if (mSnapshotData.mSnapshot)
8279 {
8280 /* wait for the completion of the server progress (diff VDI creation) */
8281 /// @todo (dmik) later, we will definitely want to cancel it instead
8282 // (when the cancel function is implemented)
8283 mSnapshotData.mServerProgress->WaitForCompletion (-1);
8284
8285 /*
8286 * delete all differencing VDIs created
8287 * (this will attach their parents back)
8288 */
8289 rc = deleteSnapshotDiffs (mSnapshotData.mSnapshot);
8290 /* continue cleanup on error */
8291
8292 /* delete the saved state file (it might have been already created) */
8293 if (mSnapshotData.mSnapshot->stateFilePath())
8294 RTFileDelete (Utf8Str (mSnapshotData.mSnapshot->stateFilePath()));
8295
8296 mSnapshotData.mSnapshot->uninit();
8297 }
8298 }
8299
8300 /* inform callbacks */
8301 if (aSuccess && SUCCEEDED (rc))
8302 mParent->onSnapshotTaken (mData->mUuid, mSnapshotData.mSnapshot->data().mId);
8303
8304 /* clear out the snapshot data */
8305 mSnapshotData.mLastState = MachineState_InvalidMachineState;
8306 mSnapshotData.mSnapshot.setNull();
8307 mSnapshotData.mServerProgress.setNull();
8308 /* uninitialize the combined progress (to remove it from the VBox collection) */
8309 if (!mSnapshotData.mCombinedProgress.isNull())
8310 {
8311 mSnapshotData.mCombinedProgress->uninit();
8312 mSnapshotData.mCombinedProgress.setNull();
8313 }
8314
8315 LogFlowThisFuncLeave();
8316 return rc;
8317}
8318
8319/**
8320 * Take snapshot task handler.
8321 * Must be called only by TakeSnapshotTask::handler()!
8322 *
8323 * The sole purpose of this task is to asynchronously create differencing VDIs
8324 * and copy the saved state file (when necessary). The VM process will wait
8325 * for this task to complete using the mSnapshotData.mServerProgress
8326 * returned to it.
8327 *
8328 * @note Locks mParent + this objects for writing.
8329 */
8330void SessionMachine::takeSnapshotHandler (TakeSnapshotTask &aTask)
8331{
8332 LogFlowThisFuncEnter();
8333
8334 AutoCaller autoCaller (this);
8335
8336 LogFlowThisFunc (("state=%d\n", autoCaller.state()));
8337 if (!autoCaller.isOk())
8338 {
8339 /*
8340 * we might have been uninitialized because the session was
8341 * accidentally closed by the client, so don't assert
8342 */
8343 LogFlowThisFuncLeave();
8344 return;
8345 }
8346
8347 /* endTakingSnapshot() needs mParent lock */
8348 AutoMultiLock <2> alock (mParent->wlock(), this->wlock());
8349
8350 HRESULT rc = S_OK;
8351
8352 LogFlowThisFunc (("Creating differencing VDIs...\n"));
8353
8354 /* create new differencing hard disks and attach them to this machine */
8355 rc = createSnapshotDiffs (&mSnapshotData.mSnapshot->data().mId,
8356 mUserData->mSnapshotFolderFull,
8357 mSnapshotData.mServerProgress,
8358 true /* aOnline */);
8359
8360 if (SUCCEEDED (rc) && mSnapshotData.mLastState == MachineState_Saved)
8361 {
8362 Utf8Str stateFrom = mSSData->mStateFilePath;
8363 Utf8Str stateTo = mSnapshotData.mSnapshot->stateFilePath();
8364
8365 LogFlowThisFunc (("Copying the execution state from '%s' to '%s'...\n",
8366 stateFrom.raw(), stateTo.raw()));
8367
8368 mSnapshotData.mServerProgress->advanceOperation (
8369 Bstr (tr ("Copying the execution state")));
8370
8371 /*
8372 * We can safely leave the lock here:
8373 * mMachineState is MachineState_Saving here
8374 */
8375 alock.leave();
8376
8377 /* copy the state file */
8378 int vrc = RTFileCopyEx (stateFrom, stateTo, progressCallback,
8379 static_cast <Progress *> (mSnapshotData.mServerProgress));
8380
8381 alock.enter();
8382
8383 if (VBOX_FAILURE (vrc))
8384 rc = setError (E_FAIL,
8385 tr ("Could not copy the state file '%ls' to '%ls' (%Vrc)"),
8386 stateFrom.raw(), stateTo.raw());
8387 }
8388
8389 /*
8390 * we have to call endTakingSnapshot() here if the snapshot was taken
8391 * offline, because the VM process will not do it in this case
8392 */
8393 if (mSnapshotData.mLastState != MachineState_Paused)
8394 {
8395 LogFlowThisFunc (("Finalizing the taken snapshot (rc=%08X)...\n", rc));
8396
8397 setMachineState (mSnapshotData.mLastState);
8398 updateMachineStateOnClient();
8399
8400 /* finalize the progress after setting the state, for consistency */
8401 mSnapshotData.mServerProgress->notifyComplete (rc);
8402
8403 endTakingSnapshot (SUCCEEDED (rc));
8404 }
8405 else
8406 {
8407 mSnapshotData.mServerProgress->notifyComplete (rc);
8408 }
8409
8410 LogFlowThisFuncLeave();
8411}
8412
8413/**
8414 * Discard snapshot task handler.
8415 * Must be called only by DiscardSnapshotTask::handler()!
8416 *
8417 * When aTask.subTask is true, the associated progress object is left
8418 * uncompleted on success. On failure, the progress is marked as completed
8419 * regardless of this parameter.
8420 *
8421 * @note Locks mParent + this + child objects for writing!
8422 */
8423void SessionMachine::discardSnapshotHandler (DiscardSnapshotTask &aTask)
8424{
8425 LogFlowThisFuncEnter();
8426
8427 AutoCaller autoCaller (this);
8428
8429 LogFlowThisFunc (("state=%d\n", autoCaller.state()));
8430 if (!autoCaller.isOk())
8431 {
8432 /*
8433 * we might have been uninitialized because the session was
8434 * accidentally closed by the client, so don't assert
8435 */
8436 aTask.progress->notifyComplete (
8437 E_FAIL, COM_IIDOF (IMachine), getComponentName(),
8438 tr ("The session has been accidentally closed"));
8439
8440 LogFlowThisFuncLeave();
8441 return;
8442 }
8443
8444 ComObjPtr <SnapshotMachine> sm = aTask.snapshot->data().mMachine;
8445
8446 /* mParent is locked because of Progress::notifyComplete(), etc. */
8447 AutoMultiLock <3> alock (mParent->wlock(), this->wlock(), sm->rlock());
8448
8449 /* Safe locking in the direction parent->child */
8450 AutoLock snapshotLock (aTask.snapshot);
8451 AutoLock snapshotChildrenLock (aTask.snapshot->childrenLock());
8452
8453 HRESULT rc = S_OK;
8454
8455 /* save the snapshot ID (for callbacks) */
8456 Guid snapshotId = aTask.snapshot->data().mId;
8457
8458 do
8459 {
8460 /* first pass: */
8461 LogFlowThisFunc (("Check hard disk accessibility and affected machines...\n"));
8462
8463 HDData::HDAttachmentList::const_iterator it;
8464 for (it = sm->mHDData->mHDAttachments.begin();
8465 it != sm->mHDData->mHDAttachments.end();
8466 ++ it)
8467 {
8468 ComObjPtr <HardDiskAttachment> hda = *it;
8469 ComObjPtr <HardDisk> hd = hda->hardDisk();
8470 ComObjPtr <HardDisk> parent = hd->parent();
8471
8472 AutoLock hdLock (hd);
8473
8474 if (hd->hasForeignChildren())
8475 {
8476 rc = setError (E_FAIL,
8477 tr ("One or more hard disks belonging to other machines are "
8478 "based on the hard disk '%ls' stored in the snapshot '%ls'"),
8479 hd->toString().raw(), aTask.snapshot->data().mName.raw());
8480 break;
8481 }
8482
8483 if (hd->type() == HardDiskType_NormalHardDisk)
8484 {
8485 AutoLock hdChildrenLock (hd->childrenLock());
8486 size_t childrenCount = hd->children().size();
8487 if (childrenCount > 1)
8488 {
8489 rc = setError (E_FAIL,
8490 tr ("Normal hard disk '%ls' stored in the snapshot '%ls' "
8491 "has more than one child hard disk (%d)"),
8492 hd->toString().raw(), aTask.snapshot->data().mName.raw(),
8493 childrenCount);
8494 break;
8495 }
8496 }
8497 else
8498 {
8499 ComAssertMsgFailedBreak (("Invalid hard disk type %d\n", hd->type()),
8500 rc = E_FAIL);
8501 }
8502
8503 Bstr accessError;
8504 rc = hd->getAccessibleWithChildren (accessError);
8505 CheckComRCBreakRC (rc);
8506
8507 if (!accessError.isNull())
8508 {
8509 rc = setError (E_FAIL,
8510 tr ("Hard disk '%ls' stored in the snapshot '%ls' is not "
8511 "accessible (%ls)"),
8512 hd->toString().raw(), aTask.snapshot->data().mName.raw(),
8513 accessError.raw());
8514 break;
8515 }
8516
8517 rc = hd->setBusyWithChildren();
8518 if (FAILED (rc))
8519 {
8520 /* reset the busy flag of all previous hard disks */
8521 while (it != sm->mHDData->mHDAttachments.begin())
8522 (*(-- it))->hardDisk()->clearBusyWithChildren();
8523 break;
8524 }
8525 }
8526
8527 CheckComRCBreakRC (rc);
8528
8529 /* second pass: */
8530 LogFlowThisFunc (("Performing actual vdi merging...\n"));
8531
8532 for (it = sm->mHDData->mHDAttachments.begin();
8533 it != sm->mHDData->mHDAttachments.end();
8534 ++ it)
8535 {
8536 ComObjPtr <HardDiskAttachment> hda = *it;
8537 ComObjPtr <HardDisk> hd = hda->hardDisk();
8538 ComObjPtr <HardDisk> parent = hd->parent();
8539
8540 AutoLock hdLock (hd);
8541
8542 Bstr hdRootString = hd->root()->toString (true /* aShort */);
8543
8544 if (parent)
8545 {
8546 if (hd->isParentImmutable())
8547 {
8548 aTask.progress->advanceOperation (Bstr (Utf8StrFmt (
8549 tr ("Discarding changes to immutable hard disk '%ls'"),
8550 hdRootString.raw())));
8551
8552 /* clear the busy flag before unregistering */
8553 hd->clearBusy();
8554
8555 /*
8556 * unregisterDiffHardDisk() is supposed to delete and uninit
8557 * the differencing hard disk
8558 */
8559 rc = mParent->unregisterDiffHardDisk (hd);
8560 CheckComRCBreakRC (rc);
8561 continue;
8562 }
8563 else
8564 {
8565 /*
8566 * differencing VDI:
8567 * merge this image to all its children
8568 */
8569
8570 aTask.progress->advanceOperation (Bstr (Utf8StrFmt (
8571 tr ("Merging changes to normal hard disk '%ls' to children"),
8572 hdRootString.raw())));
8573
8574 snapshotChildrenLock.unlock();
8575 snapshotLock.unlock();
8576 alock.leave();
8577
8578 rc = hd->asVDI()->mergeImageToChildren (aTask.progress);
8579
8580 alock.enter();
8581 snapshotLock.lock();
8582 snapshotChildrenLock.lock();
8583
8584 // debug code
8585 // if (it != sm->mHDData->mHDAttachments.begin())
8586 // {
8587 // rc = setError (E_FAIL, "Simulated failure");
8588 // break;
8589 //}
8590
8591 if (SUCCEEDED (rc))
8592 rc = mParent->unregisterDiffHardDisk (hd);
8593 else
8594 hd->clearBusyWithChildren();
8595
8596 CheckComRCBreakRC (rc);
8597 }
8598 }
8599 else if (hd->type() == HardDiskType_NormalHardDisk)
8600 {
8601 /*
8602 * normal vdi has the only child or none
8603 * (checked in the first pass)
8604 */
8605
8606 ComObjPtr <HardDisk> child;
8607 {
8608 AutoLock hdChildrenLock (hd->childrenLock());
8609 if (hd->children().size())
8610 child = hd->children().front();
8611 }
8612
8613 if (child.isNull())
8614 {
8615 aTask.progress->advanceOperation (Bstr (Utf8StrFmt (
8616 tr ("Detaching normal hard disk '%ls'"),
8617 hdRootString.raw())));
8618
8619 /* just deassociate the normal image from this machine */
8620 hd->setMachineId (Guid());
8621 hd->setSnapshotId (Guid());
8622
8623 /* clear the busy flag */
8624 hd->clearBusy();
8625 }
8626 else
8627 {
8628 AutoLock childLock (child);
8629
8630 aTask.progress->advanceOperation (Bstr (Utf8StrFmt (
8631 tr ("Preserving changes to normal hard disk '%ls'"),
8632 hdRootString.raw())));
8633
8634 ComObjPtr <Machine> cm;
8635 ComObjPtr <Snapshot> cs;
8636 ComObjPtr <HardDiskAttachment> childHda;
8637 rc = findHardDiskAttachment (child, &cm, &cs, &childHda);
8638 CheckComRCBreakRC (rc);
8639 /* must be the same machine (checked in the first pass) */
8640 ComAssertBreak (cm->mData->mUuid == mData->mUuid, rc = E_FAIL);
8641
8642 /* merge the child to this basic image */
8643
8644 snapshotChildrenLock.unlock();
8645 snapshotLock.unlock();
8646 alock.leave();
8647
8648 rc = child->asVDI()->mergeImageToParent (aTask.progress);
8649
8650 alock.enter();
8651 snapshotLock.lock();
8652 snapshotChildrenLock.lock();
8653
8654 if (SUCCEEDED (rc))
8655 rc = mParent->unregisterDiffHardDisk (child);
8656 else
8657 hd->clearBusyWithChildren();
8658
8659 CheckComRCBreakRC (rc);
8660
8661 /* replace the child image in the appropriate place */
8662 childHda->updateHardDisk (hd, FALSE /* aDirty */);
8663
8664 if (!cs)
8665 {
8666 aTask.settingsChanged = true;
8667 }
8668 else
8669 {
8670 rc = cm->saveSnapshotSettings (cs, SaveSS_UpdateAllOp);
8671 CheckComRCBreakRC (rc);
8672 }
8673 }
8674 }
8675 else
8676 {
8677 ComAssertMsgFailedBreak (("Invalid hard disk type %d\n", hd->type()),
8678 rc = E_FAIL);
8679 }
8680 }
8681
8682 /* fetch the current error info */
8683 ErrorInfo mergeEi;
8684 HRESULT mergeRc = rc;
8685
8686 if (FAILED (rc))
8687 {
8688 /* clear the busy flag on the rest of hard disks */
8689 for (++ it; it != sm->mHDData->mHDAttachments.end(); ++ it)
8690 (*it)->hardDisk()->clearBusyWithChildren();
8691 }
8692
8693 /*
8694 * we have to try to discard the snapshot even if merging failed
8695 * because some images might have been already merged (and deleted)
8696 */
8697
8698 do
8699 {
8700 LogFlowThisFunc (("Discarding the snapshot (reparenting children)...\n"));
8701
8702 ComObjPtr <Snapshot> parentSnapshot = aTask.snapshot->parent();
8703
8704 /// @todo (dmik):
8705 // when we introduce clones later, discarding the snapshot
8706 // will affect the current and first snapshots of clones, if they are
8707 // direct children of this snapshot. So we will need to lock machines
8708 // associated with child snapshots as well and update mCurrentSnapshot
8709 // and/or mFirstSnapshot fields.
8710
8711 if (aTask.snapshot == mData->mCurrentSnapshot)
8712 {
8713 /* currently, the parent snapshot must refer to the same machine */
8714 ComAssertBreak (
8715 !parentSnapshot ||
8716 parentSnapshot->data().mMachine->mData->mUuid == mData->mUuid,
8717 rc = E_FAIL);
8718 mData->mCurrentSnapshot = parentSnapshot;
8719 /* mark the current state as modified */
8720 mData->mCurrentStateModified = TRUE;
8721 }
8722
8723 if (aTask.snapshot == mData->mFirstSnapshot)
8724 {
8725 /*
8726 * the first snapshot must have only one child when discarded,
8727 * or no children at all
8728 */
8729 ComAssertBreak (aTask.snapshot->children().size() <= 1, rc = E_FAIL);
8730
8731 if (aTask.snapshot->children().size() == 1)
8732 {
8733 ComObjPtr <Snapshot> childSnapshot = aTask.snapshot->children().front();
8734 ComAssertBreak (
8735 childSnapshot->data().mMachine->mData->mUuid == mData->mUuid,
8736 rc = E_FAIL);
8737 mData->mFirstSnapshot = childSnapshot;
8738 }
8739 else
8740 mData->mFirstSnapshot.setNull();
8741 }
8742
8743 /// @todo (dmik)
8744 // if we implement some warning mechanism later, we'll have
8745 // to return a warning if the state file path cannot be deleted
8746 Bstr stateFilePath = aTask.snapshot->stateFilePath();
8747 if (stateFilePath)
8748 RTFileDelete (Utf8Str (stateFilePath));
8749
8750 aTask.snapshot->discard();
8751
8752 rc = saveSnapshotSettings (parentSnapshot,
8753 SaveSS_UpdateAllOp | SaveSS_UpdateCurrentId);
8754 }
8755 while (0);
8756
8757 /* restore the merge error if any */
8758 if (FAILED (mergeRc))
8759 {
8760 rc = mergeRc;
8761 setError (mergeEi);
8762 }
8763 }
8764 while (0);
8765
8766 if (!aTask.subTask || FAILED (rc))
8767 {
8768 if (!aTask.subTask)
8769 {
8770 /* save current error info */
8771 ErrorInfo ei;
8772
8773 /* restore the machine state */
8774 setMachineState (aTask.state);
8775 updateMachineStateOnClient();
8776
8777 /*
8778 * save settings anyway, since we've already changed the current
8779 * machine configuration
8780 */
8781 if (aTask.settingsChanged)
8782 {
8783 saveSettings (true /* aMarkCurStateAsModified */,
8784 true /* aInformCallbacksAnyway */);
8785 }
8786
8787 /* restore current error info */
8788 setError (ei);
8789 }
8790
8791 /* set the result (this will try to fetch current error info on failure) */
8792 aTask.progress->notifyComplete (rc);
8793 }
8794
8795 if (SUCCEEDED (rc))
8796 mParent->onSnapshotDiscarded (mData->mUuid, snapshotId);
8797
8798 LogFlowThisFunc (("Done discarding snapshot (rc=%08X)\n", rc));
8799 LogFlowThisFuncLeave();
8800}
8801
8802/**
8803 * Discard current state task handler.
8804 * Must be called only by DiscardCurrentStateTask::handler()!
8805 *
8806 * @note Locks mParent + this object for writing.
8807 */
8808void SessionMachine::discardCurrentStateHandler (DiscardCurrentStateTask &aTask)
8809{
8810 LogFlowThisFuncEnter();
8811
8812 AutoCaller autoCaller (this);
8813
8814 LogFlowThisFunc (("state=%d\n", autoCaller.state()));
8815 if (!autoCaller.isOk())
8816 {
8817 /*
8818 * we might have been uninitialized because the session was
8819 * accidentally closed by the client, so don't assert
8820 */
8821 aTask.progress->notifyComplete (
8822 E_FAIL, COM_IIDOF (IMachine), getComponentName(),
8823 tr ("The session has been accidentally closed"));
8824
8825 LogFlowThisFuncLeave();
8826 return;
8827 }
8828
8829 /* mParent is locked because of Progress::notifyComplete(), etc. */
8830 AutoMultiLock <2> alock (mParent->wlock(), this->wlock());
8831
8832 /*
8833 * discard all current changes to mUserData (name, OSType etc.)
8834 * (note that the machine is powered off, so there is no need
8835 * to inform the direct session)
8836 */
8837 if (isModified())
8838 rollback (false /* aNotify */);
8839
8840 HRESULT rc = S_OK;
8841
8842 bool errorInSubtask = false;
8843 bool stateRestored = false;
8844
8845 const bool isLastSnapshot = mData->mCurrentSnapshot->parent().isNull();
8846
8847 do
8848 {
8849 /*
8850 * discard the saved state file if the machine was Saved prior
8851 * to this operation
8852 */
8853 if (aTask.state == MachineState_Saved)
8854 {
8855 Assert (!mSSData->mStateFilePath.isEmpty());
8856 RTFileDelete (Utf8Str (mSSData->mStateFilePath));
8857 mSSData->mStateFilePath.setNull();
8858 aTask.modifyLastState (MachineState_PoweredOff);
8859 rc = saveStateSettings (SaveSTS_StateFilePath);
8860 CheckComRCBreakRC (rc);
8861 }
8862
8863 if (aTask.discardCurrentSnapshot && !isLastSnapshot)
8864 {
8865 /*
8866 * the "discard current snapshot and state" task is in action,
8867 * the current snapshot is not the last one.
8868 * Discard the current snapshot first.
8869 */
8870
8871 DiscardSnapshotTask subTask (aTask, mData->mCurrentSnapshot);
8872 subTask.subTask = true;
8873 discardSnapshotHandler (subTask);
8874 aTask.settingsChanged = subTask.settingsChanged;
8875 if (aTask.progress->completed())
8876 {
8877 /*
8878 * the progress can be completed by a subtask only if there was
8879 * a failure
8880 */
8881 Assert (FAILED (aTask.progress->resultCode()));
8882 errorInSubtask = true;
8883 rc = aTask.progress->resultCode();
8884 break;
8885 }
8886 }
8887
8888 LONG64 snapshotTimeStamp = 0;
8889
8890 {
8891 ComObjPtr <Snapshot> curSnapshot = mData->mCurrentSnapshot;
8892 AutoLock snapshotLock (curSnapshot);
8893
8894 /* remember the timestamp of the snapshot we're restoring from */
8895 snapshotTimeStamp = curSnapshot->data().mTimeStamp;
8896
8897 /* copy all hardware data from the current snapshot */
8898 copyFrom (curSnapshot->data().mMachine);
8899
8900 LogFlowThisFunc (("Restoring VDIs from the snapshot...\n"));
8901
8902 /* restore the attachmends from the snapshot */
8903 mHDData.backup();
8904 mHDData->mHDAttachments =
8905 curSnapshot->data().mMachine->mHDData->mHDAttachments;
8906
8907 snapshotLock.unlock();
8908 alock.leave();
8909 rc = createSnapshotDiffs (NULL, mUserData->mSnapshotFolderFull,
8910 aTask.progress,
8911 false /* aOnline */);
8912 alock.enter();
8913 snapshotLock.lock();
8914
8915 if (FAILED (rc))
8916 {
8917 /* here we can still safely rollback, so do it */
8918 ErrorInfo ei;
8919 /* undo all changes */
8920 rollback (false /* aNotify */);
8921 setError (ei);
8922 break;
8923 }
8924
8925 /*
8926 * note: old VDIs will be deassociated/deleted on #commit() called
8927 * either from #saveSettings() or directly at the end
8928 */
8929
8930 /* should not have a saved state file associated at this point */
8931 Assert (mSSData->mStateFilePath.isNull());
8932
8933 if (curSnapshot->stateFilePath())
8934 {
8935 Utf8Str snapStateFilePath = curSnapshot->stateFilePath();
8936
8937 Utf8Str stateFilePath = Utf8StrFmt ("%ls%c{%Vuuid}.sav",
8938 mUserData->mSnapshotFolderFull.raw(),
8939 RTPATH_DELIMITER, mData->mUuid.raw());
8940
8941 LogFlowThisFunc (("Copying saved state file from '%s' to '%s'...\n",
8942 snapStateFilePath.raw(), stateFilePath.raw()));
8943
8944 aTask.progress->advanceOperation (
8945 Bstr (tr ("Restoring the execution state")));
8946
8947 /* copy the state file */
8948 snapshotLock.unlock();
8949 alock.leave();
8950 int vrc = RTFileCopyEx (snapStateFilePath, stateFilePath,
8951 progressCallback, aTask.progress);
8952 alock.enter();
8953 snapshotLock.lock();
8954
8955 if (VBOX_SUCCESS (vrc))
8956 {
8957 mSSData->mStateFilePath = stateFilePath;
8958 }
8959 else
8960 {
8961 rc = setError (E_FAIL,
8962 tr ("Could not copy the state file '%s' to '%s' (%Vrc)"),
8963 snapStateFilePath.raw(), stateFilePath.raw(), vrc);
8964 break;
8965 }
8966 }
8967 }
8968
8969 bool informCallbacks = false;
8970
8971 if (aTask.discardCurrentSnapshot && isLastSnapshot)
8972 {
8973 /*
8974 * discard the current snapshot and state task is in action,
8975 * the current snapshot is the last one.
8976 * Discard the current snapshot after discarding the current state.
8977 */
8978
8979 /* commit changes to fixup hard disks before discarding */
8980 rc = commit();
8981 if (SUCCEEDED (rc))
8982 {
8983 DiscardSnapshotTask subTask (aTask, mData->mCurrentSnapshot);
8984 subTask.subTask = true;
8985 discardSnapshotHandler (subTask);
8986 aTask.settingsChanged = subTask.settingsChanged;
8987 if (aTask.progress->completed())
8988 {
8989 /*
8990 * the progress can be completed by a subtask only if there
8991 * was a failure
8992 */
8993 Assert (FAILED (aTask.progress->resultCode()));
8994 errorInSubtask = true;
8995 rc = aTask.progress->resultCode();
8996 }
8997 }
8998
8999 /*
9000 * we've committed already, so inform callbacks anyway to ensure
9001 * they don't miss some change
9002 */
9003 informCallbacks = true;
9004 }
9005
9006 /*
9007 * we have already discarded the current state, so set the
9008 * execution state accordingly no matter of the discard snapshot result
9009 */
9010 if (mSSData->mStateFilePath)
9011 setMachineState (MachineState_Saved);
9012 else
9013 setMachineState (MachineState_PoweredOff);
9014
9015 updateMachineStateOnClient();
9016 stateRestored = true;
9017
9018 if (errorInSubtask)
9019 break;
9020
9021 /* assign the timestamp from the snapshot */
9022 Assert (snapshotTimeStamp != 0);
9023 mData->mLastStateChange = snapshotTimeStamp;
9024
9025 /* mark the current state as not modified */
9026 mData->mCurrentStateModified = FALSE;
9027
9028 /* save all settings and commit */
9029 rc = saveSettings (false /* aMarkCurStateAsModified */,
9030 informCallbacks);
9031 aTask.settingsChanged = false;
9032 }
9033 while (0);
9034
9035 if (FAILED (rc))
9036 {
9037 ErrorInfo ei;
9038
9039 if (!stateRestored)
9040 {
9041 /* restore the machine state */
9042 setMachineState (aTask.state);
9043 updateMachineStateOnClient();
9044 }
9045
9046 /*
9047 * save all settings and commit if still modified (there is no way to
9048 * rollback properly). Note that isModified() will return true after
9049 * copyFrom(). Also save the settings if requested by the subtask.
9050 */
9051 if (isModified() || aTask.settingsChanged)
9052 {
9053 if (aTask.settingsChanged)
9054 saveSettings (true /* aMarkCurStateAsModified */,
9055 true /* aInformCallbacksAnyway */);
9056 else
9057 saveSettings();
9058 }
9059
9060 setError (ei);
9061 }
9062
9063 if (!errorInSubtask)
9064 {
9065 /* set the result (this will try to fetch current error info on failure) */
9066 aTask.progress->notifyComplete (rc);
9067 }
9068
9069 if (SUCCEEDED (rc))
9070 mParent->onSnapshotDiscarded (mData->mUuid, Guid());
9071
9072 LogFlowThisFunc (("Done discarding current state (rc=%08X)\n", rc));
9073
9074 LogFlowThisFuncLeave();
9075}
9076
9077/**
9078 * Helper to change the machine state (reimplementation).
9079 *
9080 * @note Locks this object for writing.
9081 */
9082HRESULT SessionMachine::setMachineState (MachineState_T aMachineState)
9083{
9084 LogFlowThisFuncEnter();
9085 LogFlowThisFunc (("aMachineState=%d\n", aMachineState));
9086
9087 AutoCaller autoCaller (this);
9088 AssertComRCReturn (autoCaller.rc(), autoCaller.rc());
9089
9090 AutoLock alock (this);
9091
9092 MachineState_T oldMachineState = mData->mMachineState;
9093
9094 AssertMsgReturn (oldMachineState != aMachineState,
9095 ("oldMachineState=%d, aMachineState=%d\n",
9096 oldMachineState, aMachineState), E_FAIL);
9097
9098 HRESULT rc = S_OK;
9099
9100 int stsFlags = 0;
9101 bool deleteSavedState = false;
9102
9103 /* detect some state transitions */
9104
9105 if (oldMachineState < MachineState_Running &&
9106 aMachineState >= MachineState_Running &&
9107 aMachineState != MachineState_Discarding)
9108 {
9109 /*
9110 * the EMT thread is about to start, so mark attached HDDs as busy
9111 * and all its ancestors as being in use
9112 */
9113 for (HDData::HDAttachmentList::const_iterator it =
9114 mHDData->mHDAttachments.begin();
9115 it != mHDData->mHDAttachments.end();
9116 ++ it)
9117 {
9118 ComObjPtr <HardDisk> hd = (*it)->hardDisk();
9119 AutoLock hdLock (hd);
9120 hd->setBusy();
9121 hd->addReaderOnAncestors();
9122 }
9123 }
9124 else
9125 if (oldMachineState >= MachineState_Running &&
9126 oldMachineState != MachineState_Discarding &&
9127 aMachineState < MachineState_Running)
9128 {
9129 /*
9130 * the EMT thread stopped, so mark attached HDDs as no more busy
9131 * and remove the in-use flag from all its ancestors
9132 */
9133 for (HDData::HDAttachmentList::const_iterator it =
9134 mHDData->mHDAttachments.begin();
9135 it != mHDData->mHDAttachments.end();
9136 ++ it)
9137 {
9138 ComObjPtr <HardDisk> hd = (*it)->hardDisk();
9139 AutoLock hdLock (hd);
9140 hd->releaseReaderOnAncestors();
9141 hd->clearBusy();
9142 }
9143 }
9144
9145 if (oldMachineState == MachineState_Restoring)
9146 {
9147 if (aMachineState != MachineState_Saved)
9148 {
9149 /*
9150 * delete the saved state file once the machine has finished
9151 * restoring from it (note that Console sets the state from
9152 * Restoring to Saved if the VM couldn't restore successfully,
9153 * to give the user an ability to fix an error and retry --
9154 * we keep the saved state file in this case)
9155 */
9156 deleteSavedState = true;
9157 }
9158 }
9159 else
9160 if (oldMachineState == MachineState_Saved &&
9161 (aMachineState == MachineState_PoweredOff ||
9162 aMachineState == MachineState_Aborted))
9163 {
9164 /*
9165 * delete the saved state after Console::DiscardSavedState() is called
9166 * or if the VM process (owning a direct VM session) crashed while the
9167 * VM was Saved
9168 */
9169
9170 /// @todo (dmik)
9171 // Not sure that deleting the saved state file just because of the
9172 // client death before it attempted to restore the VM is a good
9173 // thing. But when it crashes we need to go to the Aborted state
9174 // which cannot have the saved state file associated... The only
9175 // way to fix this is to make the Aborted condition not a VM state
9176 // but a bool flag: i.e., when a crash occurs, set it to true and
9177 // change the state to PoweredOff or Saved depending on the
9178 // saved state presence.
9179
9180 deleteSavedState = true;
9181 mData->mCurrentStateModified = TRUE;
9182 stsFlags |= SaveSTS_CurStateModified;
9183 }
9184
9185 if (aMachineState == MachineState_Starting ||
9186 aMachineState == MachineState_Restoring)
9187 {
9188 /*
9189 * set the current state modified flag to indicate that the
9190 * current state is no more identical to the state in the
9191 * current snapshot
9192 */
9193 if (!mData->mCurrentSnapshot.isNull())
9194 {
9195 mData->mCurrentStateModified = TRUE;
9196 stsFlags |= SaveSTS_CurStateModified;
9197 }
9198 }
9199
9200 if (deleteSavedState == true)
9201 {
9202 Assert (!mSSData->mStateFilePath.isEmpty());
9203 RTFileDelete (Utf8Str (mSSData->mStateFilePath));
9204 mSSData->mStateFilePath.setNull();
9205 stsFlags |= SaveSTS_StateFilePath;
9206 }
9207
9208 /* redirect to the underlying peer machine */
9209 mPeer->setMachineState (aMachineState);
9210
9211 if (aMachineState == MachineState_PoweredOff ||
9212 aMachineState == MachineState_Aborted ||
9213 aMachineState == MachineState_Saved)
9214 {
9215 stsFlags |= SaveSTS_StateTimeStamp;
9216 }
9217
9218 rc = saveStateSettings (stsFlags);
9219
9220 if ((oldMachineState != MachineState_PoweredOff &&
9221 oldMachineState != MachineState_Aborted) &&
9222 (aMachineState == MachineState_PoweredOff ||
9223 aMachineState == MachineState_Aborted))
9224 {
9225 /*
9226 * clear differencing hard disks based on immutable hard disks
9227 * once we've been shut down for any reason
9228 */
9229 rc = wipeOutImmutableDiffs();
9230 }
9231
9232 LogFlowThisFunc (("rc=%08X\n", rc));
9233 LogFlowThisFuncLeave();
9234 return rc;
9235}
9236
9237/**
9238 * Sends the current machine state value to the VM process.
9239 *
9240 * @note Locks this object for reading, then calls a client process.
9241 */
9242HRESULT SessionMachine::updateMachineStateOnClient()
9243{
9244 AutoCaller autoCaller (this);
9245 AssertComRCReturn (autoCaller.rc(), autoCaller.rc());
9246
9247 ComPtr <IInternalSessionControl> directControl;
9248 {
9249 AutoReaderLock alock (this);
9250 AssertReturn (!!mData, E_FAIL);
9251 directControl = mData->mSession.mDirectControl;
9252
9253 /* directControl may be already set to NULL here in #OnSessionEnd()
9254 * called too early by the direct session process while there is still
9255 * some operation (like discarding the snapshot) in progress. The client
9256 * process in this case is waiting inside Session::close() for the
9257 * "end session" process object to complete, while #uninit() called by
9258 * #checkForDeath() on the Watcher thread is waiting for the pending
9259 * operation to complete. For now, we accept this inconsitent behavior
9260 * and simply do nothing here. */
9261
9262 if (mData->mSession.mState == SessionState_SessionClosing)
9263 return S_OK;
9264
9265 AssertReturn (!directControl.isNull(), E_FAIL);
9266 }
9267
9268 return directControl->UpdateMachineState (mData->mMachineState);
9269}
9270
9271/* static */
9272DECLCALLBACK(int) SessionMachine::taskHandler (RTTHREAD thread, void *pvUser)
9273{
9274 AssertReturn (pvUser, VERR_INVALID_POINTER);
9275
9276 Task *task = static_cast <Task *> (pvUser);
9277 task->handler();
9278
9279 // it's our responsibility to delete the task
9280 delete task;
9281
9282 return 0;
9283}
9284
9285/////////////////////////////////////////////////////////////////////////////
9286// SnapshotMachine class
9287/////////////////////////////////////////////////////////////////////////////
9288
9289DEFINE_EMPTY_CTOR_DTOR (SnapshotMachine)
9290
9291HRESULT SnapshotMachine::FinalConstruct()
9292{
9293 LogFlowThisFunc (("\n"));
9294
9295 /* set the proper type to indicate we're the SnapshotMachine instance */
9296 unconst (mType) = IsSnapshotMachine;
9297
9298 return S_OK;
9299}
9300
9301void SnapshotMachine::FinalRelease()
9302{
9303 LogFlowThisFunc (("\n"));
9304
9305 uninit();
9306}
9307
9308/**
9309 * Initializes the SnapshotMachine object when taking a snapshot.
9310 *
9311 * @param aSessionMachine machine to take a snapshot from
9312 * @param aSnapshotId snapshot ID of this snapshot machine
9313 * @param aStateFilePath file where the execution state will be later saved
9314 * (or NULL for the offline snapshot)
9315 *
9316 * @note Locks aSessionMachine object for reading.
9317 */
9318HRESULT SnapshotMachine::init (SessionMachine *aSessionMachine,
9319 INPTR GUIDPARAM aSnapshotId,
9320 INPTR BSTR aStateFilePath)
9321{
9322 LogFlowThisFuncEnter();
9323 LogFlowThisFunc (("mName={%ls}\n", aSessionMachine->mUserData->mName.raw()));
9324
9325 AssertReturn (aSessionMachine && !Guid (aSnapshotId).isEmpty(), E_INVALIDARG);
9326
9327 /* Enclose the state transition NotReady->InInit->Ready */
9328 AutoInitSpan autoInitSpan (this);
9329 AssertReturn (autoInitSpan.isOk(), E_UNEXPECTED);
9330
9331 mSnapshotId = aSnapshotId;
9332
9333 AutoReaderLock alock (aSessionMachine);
9334
9335 /* memorize the primary Machine instance (i.e. not SessionMachine!) */
9336 unconst (mPeer) = aSessionMachine->mPeer;
9337 /* share the parent pointer */
9338 unconst (mParent) = mPeer->mParent;
9339
9340 /* take the pointer to Data to share */
9341 mData.share (mPeer->mData);
9342 /*
9343 * take the pointer to UserData to share
9344 * (our UserData must always be the same as Machine's data)
9345 */
9346 mUserData.share (mPeer->mUserData);
9347 /* make a private copy of all other data (recent changes from SessionMachine) */
9348 mHWData.attachCopy (aSessionMachine->mHWData);
9349 mHDData.attachCopy (aSessionMachine->mHDData);
9350
9351 /* SSData is always unique for SnapshotMachine */
9352 mSSData.allocate();
9353 mSSData->mStateFilePath = aStateFilePath;
9354
9355 /*
9356 * create copies of all shared folders (mHWData after attiching a copy
9357 * contains just references to original objects)
9358 */
9359 for (HWData::SharedFolderList::iterator it = mHWData->mSharedFolders.begin();
9360 it != mHWData->mSharedFolders.end();
9361 ++ it)
9362 {
9363 ComObjPtr <SharedFolder> folder;
9364 folder.createObject();
9365 HRESULT rc = folder->initCopy (this, *it);
9366 CheckComRCReturnRC (rc);
9367 *it = folder;
9368 }
9369
9370 /* create all other child objects that will be immutable private copies */
9371
9372 unconst (mBIOSSettings).createObject();
9373 mBIOSSettings->initCopy (this, mPeer->mBIOSSettings);
9374
9375#ifdef VBOX_VRDP
9376 unconst (mVRDPServer).createObject();
9377 mVRDPServer->initCopy (this, mPeer->mVRDPServer);
9378#endif
9379
9380 unconst (mDVDDrive).createObject();
9381 mDVDDrive->initCopy (this, mPeer->mDVDDrive);
9382
9383 unconst (mFloppyDrive).createObject();
9384 mFloppyDrive->initCopy (this, mPeer->mFloppyDrive);
9385
9386 unconst (mAudioAdapter).createObject();
9387 mAudioAdapter->initCopy (this, mPeer->mAudioAdapter);
9388
9389 unconst (mUSBController).createObject();
9390 mUSBController->initCopy (this, mPeer->mUSBController);
9391
9392 for (ULONG slot = 0; slot < ELEMENTS (mNetworkAdapters); slot ++)
9393 {
9394 unconst (mNetworkAdapters [slot]).createObject();
9395 mNetworkAdapters [slot]->initCopy (this, mPeer->mNetworkAdapters [slot]);
9396 }
9397
9398 /* Confirm a successful initialization when it's the case */
9399 autoInitSpan.setSucceeded();
9400
9401 LogFlowThisFuncLeave();
9402 return S_OK;
9403}
9404
9405/**
9406 * Initializes the SnapshotMachine object when loading from the settings file.
9407 *
9408 * @param aMachine machine the snapshot belngs to
9409 * @param aHWNode <Hardware> node
9410 * @param aHDAsNode <HardDiskAttachments> node
9411 * @param aSnapshotId snapshot ID of this snapshot machine
9412 * @param aStateFilePath file where the execution state is saved
9413 * (or NULL for the offline snapshot)
9414 *
9415 * @note Locks aMachine object for reading.
9416 */
9417HRESULT SnapshotMachine::init (Machine *aMachine, CFGNODE aHWNode, CFGNODE aHDAsNode,
9418 INPTR GUIDPARAM aSnapshotId, INPTR BSTR aStateFilePath)
9419{
9420 LogFlowThisFuncEnter();
9421 LogFlowThisFunc (("mName={%ls}\n", aMachine->mUserData->mName.raw()));
9422
9423 AssertReturn (aMachine && aHWNode && aHDAsNode && !Guid (aSnapshotId).isEmpty(),
9424 E_INVALIDARG);
9425
9426 /* Enclose the state transition NotReady->InInit->Ready */
9427 AutoInitSpan autoInitSpan (this);
9428 AssertReturn (autoInitSpan.isOk(), E_UNEXPECTED);
9429
9430 mSnapshotId = aSnapshotId;
9431
9432 AutoReaderLock alock (aMachine);
9433
9434 /* memorize the primary Machine instance */
9435 unconst (mPeer) = aMachine;
9436 /* share the parent pointer */
9437 unconst (mParent) = mPeer->mParent;
9438
9439 /* take the pointer to Data to share */
9440 mData.share (mPeer->mData);
9441 /*
9442 * take the pointer to UserData to share
9443 * (our UserData must always be the same as Machine's data)
9444 */
9445 mUserData.share (mPeer->mUserData);
9446 /* allocate private copies of all other data (will be loaded from settings) */
9447 mHWData.allocate();
9448 mHDData.allocate();
9449
9450 /* SSData is always unique for SnapshotMachine */
9451 mSSData.allocate();
9452 mSSData->mStateFilePath = aStateFilePath;
9453
9454 /* create all other child objects that will be immutable private copies */
9455
9456 unconst (mBIOSSettings).createObject();
9457 mBIOSSettings->init (this);
9458
9459#ifdef VBOX_VRDP
9460 unconst (mVRDPServer).createObject();
9461 mVRDPServer->init (this);
9462#endif
9463
9464 unconst (mDVDDrive).createObject();
9465 mDVDDrive->init (this);
9466
9467 unconst (mFloppyDrive).createObject();
9468 mFloppyDrive->init (this);
9469
9470 unconst (mAudioAdapter).createObject();
9471 mAudioAdapter->init (this);
9472
9473 unconst (mUSBController).createObject();
9474 mUSBController->init (this);
9475
9476 for (ULONG slot = 0; slot < ELEMENTS (mNetworkAdapters); slot ++)
9477 {
9478 unconst (mNetworkAdapters [slot]).createObject();
9479 mNetworkAdapters [slot]->init (this, slot);
9480 }
9481
9482 /* load hardware and harddisk settings */
9483
9484 HRESULT rc = loadHardware (aHWNode);
9485 if (SUCCEEDED (rc))
9486 rc = loadHardDisks (aHDAsNode, true /* aRegistered */, &mSnapshotId);
9487
9488 if (SUCCEEDED (rc))
9489 {
9490 /* commit all changes made during the initialization */
9491 commit();
9492 }
9493
9494 /* Confirm a successful initialization when it's the case */
9495 if (SUCCEEDED (rc))
9496 autoInitSpan.setSucceeded();
9497
9498 LogFlowThisFuncLeave();
9499 return rc;
9500}
9501
9502/**
9503 * Uninitializes this SnapshotMachine object.
9504 */
9505void SnapshotMachine::uninit()
9506{
9507 LogFlowThisFuncEnter();
9508
9509 /* Enclose the state transition Ready->InUninit->NotReady */
9510 AutoUninitSpan autoUninitSpan (this);
9511 if (autoUninitSpan.uninitDone())
9512 return;
9513
9514 uninitDataAndChildObjects();
9515
9516 unconst (mParent).setNull();
9517 unconst (mPeer).setNull();
9518
9519 LogFlowThisFuncLeave();
9520}
9521
9522// AutoLock::Lockable interface
9523////////////////////////////////////////////////////////////////////////////////
9524
9525/**
9526 * Overrides VirtualBoxBase::lockHandle() in order to share the lock handle
9527 * with the primary Machine instance (mPeer).
9528 */
9529AutoLock::Handle *SnapshotMachine::lockHandle() const
9530{
9531 AssertReturn (!mPeer.isNull(), NULL);
9532 return mPeer->lockHandle();
9533}
9534
9535// public methods only for internal purposes
9536////////////////////////////////////////////////////////////////////////////////
9537
9538/**
9539 * Called by the snapshot object associated with this SnapshotMachine when
9540 * snapshot data such as name or description is changed.
9541 *
9542 * @note Locks this object for writing.
9543 */
9544HRESULT SnapshotMachine::onSnapshotChange (Snapshot *aSnapshot)
9545{
9546 AutoLock alock (this);
9547
9548 mPeer->saveSnapshotSettings (aSnapshot, SaveSS_UpdateAttrsOp);
9549
9550 /* inform callbacks */
9551 mParent->onSnapshotChange (mData->mUuid, aSnapshot->data().mId);
9552
9553 return S_OK;
9554}
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