VirtualBox

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

Last change on this file since 9902 was 9900, checked in by vboxsync, 17 years ago

Turned this annoying error into a warning. (needs to be properly fixed!)

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