VirtualBox

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

Last change on this file since 23330 was 23328, checked in by vboxsync, 15 years ago

Main: fix assert + crash attaching disk

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