VirtualBox

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

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

Main/MachineImpl: add a few missing null medium checks

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