VirtualBox

source: vbox/trunk/src/VBox/Main/src-server/MachineImpl.cpp@ 56669

Last change on this file since 56669 was 56610, checked in by vboxsync, 10 years ago

Main/Machine: add stub of a new method which will apply the VM default settings for the guest OS type

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 508.9 KB
Line 
1/* $Id: MachineImpl.cpp 56610 2015-06-23 17:36:23Z vboxsync $ */
2/** @file
3 * Implementation of IMachine in VBoxSVC.
4 */
5
6/*
7 * Copyright (C) 2004-2015 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18/* Make sure all the stdint.h macros are included - must come first! */
19#ifndef __STDC_LIMIT_MACROS
20# define __STDC_LIMIT_MACROS
21#endif
22#ifndef __STDC_CONSTANT_MACROS
23# define __STDC_CONSTANT_MACROS
24#endif
25
26#include "Logging.h"
27#include "VirtualBoxImpl.h"
28#include "MachineImpl.h"
29#include "ClientToken.h"
30#include "ProgressImpl.h"
31#include "ProgressProxyImpl.h"
32#include "MediumAttachmentImpl.h"
33#include "MediumImpl.h"
34#include "MediumLock.h"
35#include "USBControllerImpl.h"
36#include "USBDeviceFiltersImpl.h"
37#include "HostImpl.h"
38#include "SharedFolderImpl.h"
39#include "GuestOSTypeImpl.h"
40#include "VirtualBoxErrorInfoImpl.h"
41#include "StorageControllerImpl.h"
42#include "DisplayImpl.h"
43#include "DisplayUtils.h"
44#include "MachineImplCloneVM.h"
45#include "AutostartDb.h"
46#include "SystemPropertiesImpl.h"
47
48// generated header
49#include "VBoxEvents.h"
50
51#ifdef VBOX_WITH_USB
52# include "USBProxyService.h"
53#endif
54
55#include "AutoCaller.h"
56#include "HashedPw.h"
57#include "Performance.h"
58
59#include <iprt/asm.h>
60#include <iprt/path.h>
61#include <iprt/dir.h>
62#include <iprt/env.h>
63#include <iprt/lockvalidator.h>
64#include <iprt/process.h>
65#include <iprt/cpp/utils.h>
66#include <iprt/cpp/xml.h> /* xml::XmlFileWriter::s_psz*Suff. */
67#include <iprt/sha.h>
68#include <iprt/string.h>
69#include <iprt/base64.h>
70
71#include <VBox/com/array.h>
72#include <VBox/com/list.h>
73
74#include <VBox/err.h>
75#include <VBox/param.h>
76#include <VBox/settings.h>
77#include <VBox/vmm/ssm.h>
78
79#ifdef VBOX_WITH_GUEST_PROPS
80# include <VBox/HostServices/GuestPropertySvc.h>
81# include <VBox/com/array.h>
82#endif
83
84#include "VBox/com/MultiResult.h"
85
86#include <algorithm>
87
88#ifdef VBOX_WITH_DTRACE_R3_MAIN
89# include "dtrace/VBoxAPI.h"
90#endif
91
92#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
93# define HOSTSUFF_EXE ".exe"
94#else /* !RT_OS_WINDOWS */
95# define HOSTSUFF_EXE ""
96#endif /* !RT_OS_WINDOWS */
97
98// defines / prototypes
99/////////////////////////////////////////////////////////////////////////////
100
101/////////////////////////////////////////////////////////////////////////////
102// Machine::Data structure
103/////////////////////////////////////////////////////////////////////////////
104
105Machine::Data::Data()
106{
107 mRegistered = FALSE;
108 pMachineConfigFile = NULL;
109 /* Contains hints on what has changed when the user is using the VM (config
110 * changes, running the VM, ...). This is used to decide if a config needs
111 * to be written to disk. */
112 flModifications = 0;
113 /* VM modification usually also trigger setting the current state to
114 * "Modified". Although this is not always the case. An e.g. is the VM
115 * initialization phase or when snapshot related data is changed. The
116 * actually behavior is controlled by the following flag. */
117 m_fAllowStateModification = false;
118 mAccessible = FALSE;
119 /* mUuid is initialized in Machine::init() */
120
121 mMachineState = MachineState_PoweredOff;
122 RTTimeNow(&mLastStateChange);
123
124 mMachineStateDeps = 0;
125 mMachineStateDepsSem = NIL_RTSEMEVENTMULTI;
126 mMachineStateChangePending = 0;
127
128 mCurrentStateModified = TRUE;
129 mGuestPropertiesModified = FALSE;
130
131 mSession.mPID = NIL_RTPROCESS;
132 mSession.mLockType = LockType_Null;
133 mSession.mState = SessionState_Unlocked;
134}
135
136Machine::Data::~Data()
137{
138 if (mMachineStateDepsSem != NIL_RTSEMEVENTMULTI)
139 {
140 RTSemEventMultiDestroy(mMachineStateDepsSem);
141 mMachineStateDepsSem = NIL_RTSEMEVENTMULTI;
142 }
143 if (pMachineConfigFile)
144 {
145 delete pMachineConfigFile;
146 pMachineConfigFile = NULL;
147 }
148}
149
150/////////////////////////////////////////////////////////////////////////////
151// Machine::HWData structure
152/////////////////////////////////////////////////////////////////////////////
153
154Machine::HWData::HWData()
155{
156 /* default values for a newly created machine */
157 mHWVersion = "2"; /** @todo get the default from the schema if that is possible. */
158 mMemorySize = 128;
159 mCPUCount = 1;
160 mCPUHotPlugEnabled = false;
161 mMemoryBalloonSize = 0;
162 mPageFusionEnabled = false;
163 mGraphicsControllerType = GraphicsControllerType_VBoxVGA;
164 mVRAMSize = 8;
165 mAccelerate3DEnabled = false;
166 mAccelerate2DVideoEnabled = false;
167 mMonitorCount = 1;
168 mVideoCaptureWidth = 1024;
169 mVideoCaptureHeight = 768;
170 mVideoCaptureRate = 512;
171 mVideoCaptureFPS = 25;
172 mVideoCaptureMaxTime = 0;
173 mVideoCaptureMaxFileSize = 0;
174 mVideoCaptureEnabled = false;
175 for (unsigned i = 0; i < RT_ELEMENTS(maVideoCaptureScreens); ++i)
176 maVideoCaptureScreens[i] = true;
177
178 mHWVirtExEnabled = true;
179 mHWVirtExNestedPagingEnabled = true;
180#if HC_ARCH_BITS == 64 && !defined(RT_OS_LINUX)
181 mHWVirtExLargePagesEnabled = true;
182#else
183 /* Not supported on 32 bits hosts. */
184 mHWVirtExLargePagesEnabled = false;
185#endif
186 mHWVirtExVPIDEnabled = true;
187 mHWVirtExUXEnabled = true;
188 mHWVirtExForceEnabled = false;
189#if HC_ARCH_BITS == 64 || defined(RT_OS_WINDOWS) || defined(RT_OS_DARWIN)
190 mPAEEnabled = true;
191#else
192 mPAEEnabled = false;
193#endif
194 mLongMode = HC_ARCH_BITS == 64 ? settings::Hardware::LongMode_Enabled : settings::Hardware::LongMode_Disabled;
195 mTripleFaultReset = false;
196 mHPETEnabled = false;
197 mCpuExecutionCap = 100; /* Maximum CPU execution cap by default. */
198 mCpuIdPortabilityLevel = 0;
199
200 /* default boot order: floppy - DVD - HDD */
201 mBootOrder[0] = DeviceType_Floppy;
202 mBootOrder[1] = DeviceType_DVD;
203 mBootOrder[2] = DeviceType_HardDisk;
204 for (size_t i = 3; i < RT_ELEMENTS(mBootOrder); ++i)
205 mBootOrder[i] = DeviceType_Null;
206
207 mClipboardMode = ClipboardMode_Disabled;
208 mDnDMode = DnDMode_Disabled;
209
210 mFirmwareType = FirmwareType_BIOS;
211 mKeyboardHIDType = KeyboardHIDType_PS2Keyboard;
212 mPointingHIDType = PointingHIDType_PS2Mouse;
213 mChipsetType = ChipsetType_PIIX3;
214 mParavirtProvider = ParavirtProvider_Default;
215 mEmulatedUSBCardReaderEnabled = FALSE;
216
217 for (size_t i = 0; i < RT_ELEMENTS(mCPUAttached); ++i)
218 mCPUAttached[i] = false;
219
220 mIOCacheEnabled = true;
221 mIOCacheSize = 5; /* 5MB */
222}
223
224Machine::HWData::~HWData()
225{
226}
227
228/////////////////////////////////////////////////////////////////////////////
229// Machine::HDData structure
230/////////////////////////////////////////////////////////////////////////////
231
232Machine::MediaData::MediaData()
233{
234}
235
236Machine::MediaData::~MediaData()
237{
238}
239
240/////////////////////////////////////////////////////////////////////////////
241// Machine class
242/////////////////////////////////////////////////////////////////////////////
243
244// constructor / destructor
245/////////////////////////////////////////////////////////////////////////////
246
247Machine::Machine() :
248#ifdef VBOX_WITH_RESOURCE_USAGE_API
249 mCollectorGuest(NULL),
250#endif
251 mPeer(NULL),
252 mParent(NULL),
253 mSerialPorts(),
254 mParallelPorts(),
255 uRegistryNeedsSaving(0)
256{}
257
258Machine::~Machine()
259{}
260
261HRESULT Machine::FinalConstruct()
262{
263 LogFlowThisFunc(("\n"));
264 return BaseFinalConstruct();
265}
266
267void Machine::FinalRelease()
268{
269 LogFlowThisFunc(("\n"));
270 uninit();
271 BaseFinalRelease();
272}
273
274/**
275 * Initializes a new machine instance; this init() variant creates a new, empty machine.
276 * This gets called from VirtualBox::CreateMachine().
277 *
278 * @param aParent Associated parent object
279 * @param strConfigFile Local file system path to the VM settings file (can
280 * be relative to the VirtualBox config directory).
281 * @param strName name for the machine
282 * @param llGroups list of groups for the machine
283 * @param aOsType OS Type of this machine or NULL.
284 * @param aId UUID for the new machine.
285 * @param fForceOverwrite Whether to overwrite an existing machine settings file.
286 *
287 * @return Success indicator. if not S_OK, the machine object is invalid
288 */
289HRESULT Machine::init(VirtualBox *aParent,
290 const Utf8Str &strConfigFile,
291 const Utf8Str &strName,
292 const StringsList &llGroups,
293 GuestOSType *aOsType,
294 const Guid &aId,
295 bool fForceOverwrite,
296 bool fDirectoryIncludesUUID)
297{
298 LogFlowThisFuncEnter();
299 LogFlowThisFunc(("(Init_New) aConfigFile='%s'\n", strConfigFile.c_str()));
300
301 /* Enclose the state transition NotReady->InInit->Ready */
302 AutoInitSpan autoInitSpan(this);
303 AssertReturn(autoInitSpan.isOk(), E_FAIL);
304
305 HRESULT rc = initImpl(aParent, strConfigFile);
306 if (FAILED(rc)) return rc;
307
308 rc = i_tryCreateMachineConfigFile(fForceOverwrite);
309 if (FAILED(rc)) return rc;
310
311 if (SUCCEEDED(rc))
312 {
313 // create an empty machine config
314 mData->pMachineConfigFile = new settings::MachineConfigFile(NULL);
315
316 rc = initDataAndChildObjects();
317 }
318
319 if (SUCCEEDED(rc))
320 {
321 // set to true now to cause uninit() to call uninitDataAndChildObjects() on failure
322 mData->mAccessible = TRUE;
323
324 unconst(mData->mUuid) = aId;
325
326 mUserData->s.strName = strName;
327
328 mUserData->s.llGroups = llGroups;
329
330 mUserData->s.fDirectoryIncludesUUID = fDirectoryIncludesUUID;
331 // the "name sync" flag determines whether the machine directory gets renamed along
332 // with the machine file; say so if the settings file name is the same as the
333 // settings file parent directory (machine directory)
334 mUserData->s.fNameSync = i_isInOwnDir();
335
336 // initialize the default snapshots folder
337 rc = COMSETTER(SnapshotFolder)(NULL);
338 AssertComRC(rc);
339
340 if (aOsType)
341 {
342 /* Store OS type */
343 mUserData->s.strOsType = aOsType->i_id();
344
345 /* Apply BIOS defaults */
346 mBIOSSettings->i_applyDefaults(aOsType);
347
348 /* Apply network adapters defaults */
349 for (ULONG slot = 0; slot < mNetworkAdapters.size(); ++slot)
350 mNetworkAdapters[slot]->i_applyDefaults(aOsType);
351
352 /* Apply serial port defaults */
353 for (ULONG slot = 0; slot < RT_ELEMENTS(mSerialPorts); ++slot)
354 mSerialPorts[slot]->i_applyDefaults(aOsType);
355
356 /* Let the OS type select 64-bit ness. */
357 mHWData->mLongMode = aOsType->i_is64Bit()
358 ? settings::Hardware::LongMode_Enabled : settings::Hardware::LongMode_Disabled;
359 }
360
361 /* At this point the changing of the current state modification
362 * flag is allowed. */
363 i_allowStateModification();
364
365 /* commit all changes made during the initialization */
366 i_commit();
367 }
368
369 /* Confirm a successful initialization when it's the case */
370 if (SUCCEEDED(rc))
371 {
372 if (mData->mAccessible)
373 autoInitSpan.setSucceeded();
374 else
375 autoInitSpan.setLimited();
376 }
377
378 LogFlowThisFunc(("mName='%s', mRegistered=%RTbool, mAccessible=%RTbool, rc=%08X\n",
379 !!mUserData ? mUserData->s.strName.c_str() : "NULL",
380 mData->mRegistered,
381 mData->mAccessible,
382 rc));
383
384 LogFlowThisFuncLeave();
385
386 return rc;
387}
388
389/**
390 * Initializes a new instance with data from machine XML (formerly Init_Registered).
391 * Gets called in two modes:
392 *
393 * -- from VirtualBox::initMachines() during VirtualBox startup; in that case, the
394 * UUID is specified and we mark the machine as "registered";
395 *
396 * -- from the public VirtualBox::OpenMachine() API, in which case the UUID is NULL
397 * and the machine remains unregistered until RegisterMachine() is called.
398 *
399 * @param aParent Associated parent object
400 * @param aConfigFile Local file system path to the VM settings file (can
401 * be relative to the VirtualBox config directory).
402 * @param aId UUID of the machine or NULL (see above).
403 *
404 * @return Success indicator. if not S_OK, the machine object is invalid
405 */
406HRESULT Machine::initFromSettings(VirtualBox *aParent,
407 const Utf8Str &strConfigFile,
408 const Guid *aId)
409{
410 LogFlowThisFuncEnter();
411 LogFlowThisFunc(("(Init_Registered) aConfigFile='%s\n", strConfigFile.c_str()));
412
413 /* Enclose the state transition NotReady->InInit->Ready */
414 AutoInitSpan autoInitSpan(this);
415 AssertReturn(autoInitSpan.isOk(), E_FAIL);
416
417 HRESULT rc = initImpl(aParent, strConfigFile);
418 if (FAILED(rc)) return rc;
419
420 if (aId)
421 {
422 // loading a registered VM:
423 unconst(mData->mUuid) = *aId;
424 mData->mRegistered = TRUE;
425 // now load the settings from XML:
426 rc = i_registeredInit();
427 // this calls initDataAndChildObjects() and loadSettings()
428 }
429 else
430 {
431 // opening an unregistered VM (VirtualBox::OpenMachine()):
432 rc = initDataAndChildObjects();
433
434 if (SUCCEEDED(rc))
435 {
436 // set to true now to cause uninit() to call uninitDataAndChildObjects() on failure
437 mData->mAccessible = TRUE;
438
439 try
440 {
441 // load and parse machine XML; this will throw on XML or logic errors
442 mData->pMachineConfigFile = new settings::MachineConfigFile(&mData->m_strConfigFileFull);
443
444 // reject VM UUID duplicates, they can happen if someone
445 // tries to register an already known VM config again
446 if (aParent->i_findMachine(mData->pMachineConfigFile->uuid,
447 true /* fPermitInaccessible */,
448 false /* aDoSetError */,
449 NULL) != VBOX_E_OBJECT_NOT_FOUND)
450 {
451 throw setError(E_FAIL,
452 tr("Trying to open a VM config '%s' which has the same UUID as an existing virtual machine"),
453 mData->m_strConfigFile.c_str());
454 }
455
456 // use UUID from machine config
457 unconst(mData->mUuid) = mData->pMachineConfigFile->uuid;
458
459 rc = i_loadMachineDataFromSettings(*mData->pMachineConfigFile,
460 NULL /* puuidRegistry */);
461 if (FAILED(rc)) throw rc;
462
463 /* At this point the changing of the current state modification
464 * flag is allowed. */
465 i_allowStateModification();
466
467 i_commit();
468 }
469 catch (HRESULT err)
470 {
471 /* we assume that error info is set by the thrower */
472 rc = err;
473 }
474 catch (...)
475 {
476 rc = VirtualBoxBase::handleUnexpectedExceptions(this, RT_SRC_POS);
477 }
478 }
479 }
480
481 /* Confirm a successful initialization when it's the case */
482 if (SUCCEEDED(rc))
483 {
484 if (mData->mAccessible)
485 autoInitSpan.setSucceeded();
486 else
487 {
488 autoInitSpan.setLimited();
489
490 // uninit media from this machine's media registry, or else
491 // reloading the settings will fail
492 mParent->i_unregisterMachineMedia(i_getId());
493 }
494 }
495
496 LogFlowThisFunc(("mName='%s', mRegistered=%RTbool, mAccessible=%RTbool "
497 "rc=%08X\n",
498 !!mUserData ? mUserData->s.strName.c_str() : "NULL",
499 mData->mRegistered, mData->mAccessible, rc));
500
501 LogFlowThisFuncLeave();
502
503 return rc;
504}
505
506/**
507 * Initializes a new instance from a machine config that is already in memory
508 * (import OVF case). Since we are importing, the UUID in the machine
509 * config is ignored and we always generate a fresh one.
510 *
511 * @param strName Name for the new machine; this overrides what is specified in config and is used
512 * for the settings file as well.
513 * @param config Machine configuration loaded and parsed from XML.
514 *
515 * @return Success indicator. if not S_OK, the machine object is invalid
516 */
517HRESULT Machine::init(VirtualBox *aParent,
518 const Utf8Str &strName,
519 const settings::MachineConfigFile &config)
520{
521 LogFlowThisFuncEnter();
522
523 /* Enclose the state transition NotReady->InInit->Ready */
524 AutoInitSpan autoInitSpan(this);
525 AssertReturn(autoInitSpan.isOk(), E_FAIL);
526
527 Utf8Str strConfigFile;
528 aParent->i_getDefaultMachineFolder(strConfigFile);
529 strConfigFile.append(RTPATH_DELIMITER);
530 strConfigFile.append(strName);
531 strConfigFile.append(RTPATH_DELIMITER);
532 strConfigFile.append(strName);
533 strConfigFile.append(".vbox");
534
535 HRESULT rc = initImpl(aParent, strConfigFile);
536 if (FAILED(rc)) return rc;
537
538 rc = i_tryCreateMachineConfigFile(false /* fForceOverwrite */);
539 if (FAILED(rc)) return rc;
540
541 rc = initDataAndChildObjects();
542
543 if (SUCCEEDED(rc))
544 {
545 // set to true now to cause uninit() to call uninitDataAndChildObjects() on failure
546 mData->mAccessible = TRUE;
547
548 // create empty machine config for instance data
549 mData->pMachineConfigFile = new settings::MachineConfigFile(NULL);
550
551 // generate fresh UUID, ignore machine config
552 unconst(mData->mUuid).create();
553
554 rc = i_loadMachineDataFromSettings(config,
555 &mData->mUuid); // puuidRegistry: initialize media with this registry ID
556
557 // override VM name as well, it may be different
558 mUserData->s.strName = strName;
559
560 if (SUCCEEDED(rc))
561 {
562 /* At this point the changing of the current state modification
563 * flag is allowed. */
564 i_allowStateModification();
565
566 /* commit all changes made during the initialization */
567 i_commit();
568 }
569 }
570
571 /* Confirm a successful initialization when it's the case */
572 if (SUCCEEDED(rc))
573 {
574 if (mData->mAccessible)
575 autoInitSpan.setSucceeded();
576 else
577 {
578 /* Ignore all errors from unregistering, they would destroy
579- * the more interesting error information we already have,
580- * pinpointing the issue with the VM config. */
581 ErrorInfoKeeper eik;
582
583 autoInitSpan.setLimited();
584
585 // uninit media from this machine's media registry, or else
586 // reloading the settings will fail
587 mParent->i_unregisterMachineMedia(i_getId());
588 }
589 }
590
591 LogFlowThisFunc(("mName='%s', mRegistered=%RTbool, mAccessible=%RTbool "
592 "rc=%08X\n",
593 !!mUserData ? mUserData->s.strName.c_str() : "NULL",
594 mData->mRegistered, mData->mAccessible, rc));
595
596 LogFlowThisFuncLeave();
597
598 return rc;
599}
600
601/**
602 * Shared code between the various init() implementations.
603 * @param aParent
604 * @return
605 */
606HRESULT Machine::initImpl(VirtualBox *aParent,
607 const Utf8Str &strConfigFile)
608{
609 LogFlowThisFuncEnter();
610
611 AssertReturn(aParent, E_INVALIDARG);
612 AssertReturn(!strConfigFile.isEmpty(), E_INVALIDARG);
613
614 HRESULT rc = S_OK;
615
616 /* share the parent weakly */
617 unconst(mParent) = aParent;
618
619 /* allocate the essential machine data structure (the rest will be
620 * allocated later by initDataAndChildObjects() */
621 mData.allocate();
622
623 /* memorize the config file name (as provided) */
624 mData->m_strConfigFile = strConfigFile;
625
626 /* get the full file name */
627 int vrc1 = mParent->i_calculateFullPath(strConfigFile, mData->m_strConfigFileFull);
628 if (RT_FAILURE(vrc1))
629 return setError(VBOX_E_FILE_ERROR,
630 tr("Invalid machine settings file name '%s' (%Rrc)"),
631 strConfigFile.c_str(),
632 vrc1);
633
634 LogFlowThisFuncLeave();
635
636 return rc;
637}
638
639/**
640 * Tries to create a machine settings file in the path stored in the machine
641 * instance data. Used when a new machine is created to fail gracefully if
642 * the settings file could not be written (e.g. because machine dir is read-only).
643 * @return
644 */
645HRESULT Machine::i_tryCreateMachineConfigFile(bool fForceOverwrite)
646{
647 HRESULT rc = S_OK;
648
649 // when we create a new machine, we must be able to create the settings file
650 RTFILE f = NIL_RTFILE;
651 int vrc = RTFileOpen(&f, mData->m_strConfigFileFull.c_str(), RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
652 if ( RT_SUCCESS(vrc)
653 || vrc == VERR_SHARING_VIOLATION
654 )
655 {
656 if (RT_SUCCESS(vrc))
657 RTFileClose(f);
658 if (!fForceOverwrite)
659 rc = setError(VBOX_E_FILE_ERROR,
660 tr("Machine settings file '%s' already exists"),
661 mData->m_strConfigFileFull.c_str());
662 else
663 {
664 /* try to delete the config file, as otherwise the creation
665 * of a new settings file will fail. */
666 int vrc2 = RTFileDelete(mData->m_strConfigFileFull.c_str());
667 if (RT_FAILURE(vrc2))
668 rc = setError(VBOX_E_FILE_ERROR,
669 tr("Could not delete the existing settings file '%s' (%Rrc)"),
670 mData->m_strConfigFileFull.c_str(), vrc2);
671 }
672 }
673 else if ( vrc != VERR_FILE_NOT_FOUND
674 && vrc != VERR_PATH_NOT_FOUND
675 )
676 rc = setError(VBOX_E_FILE_ERROR,
677 tr("Invalid machine settings file name '%s' (%Rrc)"),
678 mData->m_strConfigFileFull.c_str(),
679 vrc);
680 return rc;
681}
682
683/**
684 * Initializes the registered machine by loading the settings file.
685 * This method is separated from #init() in order to make it possible to
686 * retry the operation after VirtualBox startup instead of refusing to
687 * startup the whole VirtualBox server in case if the settings file of some
688 * registered VM is invalid or inaccessible.
689 *
690 * @note Must be always called from this object's write lock
691 * (unless called from #init() that doesn't need any locking).
692 * @note Locks the mUSBController method for writing.
693 * @note Subclasses must not call this method.
694 */
695HRESULT Machine::i_registeredInit()
696{
697 AssertReturn(!i_isSessionMachine(), E_FAIL);
698 AssertReturn(!i_isSnapshotMachine(), E_FAIL);
699 AssertReturn(mData->mUuid.isValid(), E_FAIL);
700 AssertReturn(!mData->mAccessible, E_FAIL);
701
702 HRESULT rc = initDataAndChildObjects();
703
704 if (SUCCEEDED(rc))
705 {
706 /* Temporarily reset the registered flag in order to let setters
707 * potentially called from loadSettings() succeed (isMutable() used in
708 * all setters will return FALSE for a Machine instance if mRegistered
709 * is TRUE). */
710 mData->mRegistered = FALSE;
711
712 try
713 {
714 // load and parse machine XML; this will throw on XML or logic errors
715 mData->pMachineConfigFile = new settings::MachineConfigFile(&mData->m_strConfigFileFull);
716
717 if (mData->mUuid != mData->pMachineConfigFile->uuid)
718 throw setError(E_FAIL,
719 tr("Machine UUID {%RTuuid} in '%s' doesn't match its UUID {%s} in the registry file '%s'"),
720 mData->pMachineConfigFile->uuid.raw(),
721 mData->m_strConfigFileFull.c_str(),
722 mData->mUuid.toString().c_str(),
723 mParent->i_settingsFilePath().c_str());
724
725 rc = i_loadMachineDataFromSettings(*mData->pMachineConfigFile,
726 NULL /* const Guid *puuidRegistry */);
727 if (FAILED(rc)) throw rc;
728 }
729 catch (HRESULT err)
730 {
731 /* we assume that error info is set by the thrower */
732 rc = err;
733 }
734 catch (...)
735 {
736 rc = VirtualBoxBase::handleUnexpectedExceptions(this, RT_SRC_POS);
737 }
738
739 /* Restore the registered flag (even on failure) */
740 mData->mRegistered = TRUE;
741 }
742
743 if (SUCCEEDED(rc))
744 {
745 /* Set mAccessible to TRUE only if we successfully locked and loaded
746 * the settings file */
747 mData->mAccessible = TRUE;
748
749 /* commit all changes made during loading the settings file */
750 i_commit(); // @todo r=dj why do we need a commit during init?!? this is very expensive
751 /// @todo r=klaus for some reason the settings loading logic backs up
752 // the settings, and therefore a commit is needed. Should probably be changed.
753 }
754 else
755 {
756 /* If the machine is registered, then, instead of returning a
757 * failure, we mark it as inaccessible and set the result to
758 * success to give it a try later */
759
760 /* fetch the current error info */
761 mData->mAccessError = com::ErrorInfo();
762 Log1Warning(("Machine {%RTuuid} is inaccessible! [%ls]\n", mData->mUuid.raw(), mData->mAccessError.getText().raw()));
763
764 /* rollback all changes */
765 i_rollback(false /* aNotify */);
766
767 // uninit media from this machine's media registry, or else
768 // reloading the settings will fail
769 mParent->i_unregisterMachineMedia(i_getId());
770
771 /* uninitialize the common part to make sure all data is reset to
772 * default (null) values */
773 uninitDataAndChildObjects();
774
775 rc = S_OK;
776 }
777
778 return rc;
779}
780
781/**
782 * Uninitializes the instance.
783 * Called either from FinalRelease() or by the parent when it gets destroyed.
784 *
785 * @note The caller of this method must make sure that this object
786 * a) doesn't have active callers on the current thread and b) is not locked
787 * by the current thread; otherwise uninit() will hang either a) due to
788 * AutoUninitSpan waiting for a number of calls to drop to zero or b) due to
789 * a dead-lock caused by this thread waiting for all callers on the other
790 * threads are done but preventing them from doing so by holding a lock.
791 */
792void Machine::uninit()
793{
794 LogFlowThisFuncEnter();
795
796 Assert(!isWriteLockOnCurrentThread());
797
798 Assert(!uRegistryNeedsSaving);
799 if (uRegistryNeedsSaving)
800 {
801 AutoCaller autoCaller(this);
802 if (SUCCEEDED(autoCaller.rc()))
803 {
804 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
805 i_saveSettings(NULL, Machine::SaveS_Force);
806 }
807 }
808
809 /* Enclose the state transition Ready->InUninit->NotReady */
810 AutoUninitSpan autoUninitSpan(this);
811 if (autoUninitSpan.uninitDone())
812 return;
813
814 Assert(!i_isSnapshotMachine());
815 Assert(!i_isSessionMachine());
816 Assert(!!mData);
817
818 LogFlowThisFunc(("initFailed()=%d\n", autoUninitSpan.initFailed()));
819 LogFlowThisFunc(("mRegistered=%d\n", mData->mRegistered));
820
821 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
822
823 if (!mData->mSession.mMachine.isNull())
824 {
825 /* Theoretically, this can only happen if the VirtualBox server has been
826 * terminated while there were clients running that owned open direct
827 * sessions. Since in this case we are definitely called by
828 * VirtualBox::uninit(), we may be sure that SessionMachine::uninit()
829 * won't happen on the client watcher thread (because it does
830 * VirtualBox::addCaller() for the duration of the
831 * SessionMachine::checkForDeath() call, so that VirtualBox::uninit()
832 * cannot happen until the VirtualBox caller is released). This is
833 * important, because SessionMachine::uninit() cannot correctly operate
834 * after we return from this method (it expects the Machine instance is
835 * still valid). We'll call it ourselves below.
836 */
837 Log1WarningThisFunc(("Session machine is not NULL (%p), the direct session is still open!\n",
838 (SessionMachine*)mData->mSession.mMachine));
839
840 if (Global::IsOnlineOrTransient(mData->mMachineState))
841 {
842 Log1WarningThisFunc(("Setting state to Aborted!\n"));
843 /* set machine state using SessionMachine reimplementation */
844 static_cast<Machine*>(mData->mSession.mMachine)->i_setMachineState(MachineState_Aborted);
845 }
846
847 /*
848 * Uninitialize SessionMachine using public uninit() to indicate
849 * an unexpected uninitialization.
850 */
851 mData->mSession.mMachine->uninit();
852 /* SessionMachine::uninit() must set mSession.mMachine to null */
853 Assert(mData->mSession.mMachine.isNull());
854 }
855
856 // uninit media from this machine's media registry, if they're still there
857 Guid uuidMachine(i_getId());
858
859 /* the lock is no more necessary (SessionMachine is uninitialized) */
860 alock.release();
861
862 /* XXX This will fail with
863 * "cannot be closed because it is still attached to 1 virtual machines"
864 * because at this point we did not call uninitDataAndChildObjects() yet
865 * and therefore also removeBackReference() for all these mediums was not called! */
866
867 if (uuidMachine.isValid() && !uuidMachine.isZero()) // can be empty if we're called from a failure of Machine::init
868 mParent->i_unregisterMachineMedia(uuidMachine);
869
870 // has machine been modified?
871 if (mData->flModifications)
872 {
873 Log1WarningThisFunc(("Discarding unsaved settings changes!\n"));
874 i_rollback(false /* aNotify */);
875 }
876
877 if (mData->mAccessible)
878 uninitDataAndChildObjects();
879
880 /* free the essential data structure last */
881 mData.free();
882
883 LogFlowThisFuncLeave();
884}
885
886// Wrapped IMachine properties
887/////////////////////////////////////////////////////////////////////////////
888HRESULT Machine::getParent(ComPtr<IVirtualBox> &aParent)
889{
890 /* mParent is constant during life time, no need to lock */
891 ComObjPtr<VirtualBox> pVirtualBox(mParent);
892 aParent = pVirtualBox;
893
894 return S_OK;
895}
896
897
898HRESULT Machine::getAccessible(BOOL *aAccessible)
899{
900 /* In some cases (medium registry related), it is necessary to be able to
901 * go through the list of all machines. Happens when an inaccessible VM
902 * has a sensible medium registry. */
903 AutoReadLock mllock(mParent->i_getMachinesListLockHandle() COMMA_LOCKVAL_SRC_POS);
904 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
905
906 HRESULT rc = S_OK;
907
908 if (!mData->mAccessible)
909 {
910 /* try to initialize the VM once more if not accessible */
911
912 AutoReinitSpan autoReinitSpan(this);
913 AssertReturn(autoReinitSpan.isOk(), E_FAIL);
914
915#ifdef DEBUG
916 LogFlowThisFunc(("Dumping media backreferences\n"));
917 mParent->i_dumpAllBackRefs();
918#endif
919
920 if (mData->pMachineConfigFile)
921 {
922 // reset the XML file to force loadSettings() (called from registeredInit())
923 // to parse it again; the file might have changed
924 delete mData->pMachineConfigFile;
925 mData->pMachineConfigFile = NULL;
926 }
927
928 rc = i_registeredInit();
929
930 if (SUCCEEDED(rc) && mData->mAccessible)
931 {
932 autoReinitSpan.setSucceeded();
933
934 /* make sure interesting parties will notice the accessibility
935 * state change */
936 mParent->i_onMachineStateChange(mData->mUuid, mData->mMachineState);
937 mParent->i_onMachineDataChange(mData->mUuid);
938 }
939 }
940
941 if (SUCCEEDED(rc))
942 *aAccessible = mData->mAccessible;
943
944 LogFlowThisFuncLeave();
945
946 return rc;
947}
948
949HRESULT Machine::getAccessError(ComPtr<IVirtualBoxErrorInfo> &aAccessError)
950{
951 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
952
953 if (mData->mAccessible || !mData->mAccessError.isBasicAvailable())
954 {
955 /* return shortly */
956 aAccessError = NULL;
957 return S_OK;
958 }
959
960 HRESULT rc = S_OK;
961
962 ComObjPtr<VirtualBoxErrorInfo> errorInfo;
963 rc = errorInfo.createObject();
964 if (SUCCEEDED(rc))
965 {
966 errorInfo->init(mData->mAccessError.getResultCode(),
967 mData->mAccessError.getInterfaceID().ref(),
968 Utf8Str(mData->mAccessError.getComponent()).c_str(),
969 Utf8Str(mData->mAccessError.getText()));
970 aAccessError = errorInfo;
971 }
972
973 return rc;
974}
975
976HRESULT Machine::getName(com::Utf8Str &aName)
977{
978 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
979
980 aName = mUserData->s.strName;
981
982 return S_OK;
983}
984
985HRESULT Machine::setName(const com::Utf8Str &aName)
986{
987 // prohibit setting a UUID only as the machine name, or else it can
988 // never be found by findMachine()
989 Guid test(aName);
990
991 if (test.isValid())
992 return setError(E_INVALIDARG, tr("A machine cannot have a UUID as its name"));
993
994 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
995
996 HRESULT rc = i_checkStateDependency(MutableStateDep);
997 if (FAILED(rc)) return rc;
998
999 i_setModified(IsModified_MachineData);
1000 mUserData.backup();
1001 mUserData->s.strName = aName;
1002
1003 return S_OK;
1004}
1005
1006HRESULT Machine::getDescription(com::Utf8Str &aDescription)
1007{
1008 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1009
1010 aDescription = mUserData->s.strDescription;
1011
1012 return S_OK;
1013}
1014
1015HRESULT Machine::setDescription(const com::Utf8Str &aDescription)
1016{
1017 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1018
1019 // this can be done in principle in any state as it doesn't affect the VM
1020 // significantly, but play safe by not messing around while complex
1021 // activities are going on
1022 HRESULT rc = i_checkStateDependency(MutableOrSavedOrRunningStateDep);
1023 if (FAILED(rc)) return rc;
1024
1025 i_setModified(IsModified_MachineData);
1026 mUserData.backup();
1027 mUserData->s.strDescription = aDescription;
1028
1029 return S_OK;
1030}
1031
1032HRESULT Machine::getId(com::Guid &aId)
1033{
1034 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1035
1036 aId = mData->mUuid;
1037
1038 return S_OK;
1039}
1040
1041HRESULT Machine::getGroups(std::vector<com::Utf8Str> &aGroups)
1042{
1043 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1044 aGroups.resize(mUserData->s.llGroups.size());
1045 size_t i = 0;
1046 for (StringsList::const_iterator it = mUserData->s.llGroups.begin();
1047 it != mUserData->s.llGroups.end(); ++it, ++i)
1048 aGroups[i] = (*it);
1049
1050 return S_OK;
1051}
1052
1053HRESULT Machine::setGroups(const std::vector<com::Utf8Str> &aGroups)
1054{
1055 StringsList llGroups;
1056 HRESULT rc = mParent->i_convertMachineGroups(aGroups, &llGroups);
1057 if (FAILED(rc))
1058 return rc;
1059
1060 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1061
1062 rc = i_checkStateDependency(MutableOrSavedStateDep);
1063 if (FAILED(rc)) return rc;
1064
1065 i_setModified(IsModified_MachineData);
1066 mUserData.backup();
1067 mUserData->s.llGroups = llGroups;
1068
1069 return S_OK;
1070}
1071
1072HRESULT Machine::getOSTypeId(com::Utf8Str &aOSTypeId)
1073{
1074 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1075
1076 aOSTypeId = mUserData->s.strOsType;
1077
1078 return S_OK;
1079}
1080
1081HRESULT Machine::setOSTypeId(const com::Utf8Str &aOSTypeId)
1082{
1083 /* look up the object by Id to check it is valid */
1084 ComPtr<IGuestOSType> guestOSType;
1085 HRESULT rc = mParent->GetGuestOSType(Bstr(aOSTypeId).raw(), guestOSType.asOutParam());
1086 if (FAILED(rc)) return rc;
1087
1088 /* when setting, always use the "etalon" value for consistency -- lookup
1089 * by ID is case-insensitive and the input value may have different case */
1090 Bstr osTypeId;
1091 rc = guestOSType->COMGETTER(Id)(osTypeId.asOutParam());
1092 if (FAILED(rc)) return rc;
1093
1094 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1095
1096 rc = i_checkStateDependency(MutableStateDep);
1097 if (FAILED(rc)) return rc;
1098
1099 i_setModified(IsModified_MachineData);
1100 mUserData.backup();
1101 mUserData->s.strOsType = osTypeId;
1102
1103 return S_OK;
1104}
1105
1106HRESULT Machine::getFirmwareType(FirmwareType_T *aFirmwareType)
1107{
1108 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1109
1110 *aFirmwareType = mHWData->mFirmwareType;
1111
1112 return S_OK;
1113}
1114
1115HRESULT Machine::setFirmwareType(FirmwareType_T aFirmwareType)
1116{
1117 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1118
1119 HRESULT rc = i_checkStateDependency(MutableStateDep);
1120 if (FAILED(rc)) return rc;
1121
1122 i_setModified(IsModified_MachineData);
1123 mHWData.backup();
1124 mHWData->mFirmwareType = aFirmwareType;
1125
1126 return S_OK;
1127}
1128
1129HRESULT Machine::getKeyboardHIDType(KeyboardHIDType_T *aKeyboardHIDType)
1130{
1131 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1132
1133 *aKeyboardHIDType = mHWData->mKeyboardHIDType;
1134
1135 return S_OK;
1136}
1137
1138HRESULT Machine::setKeyboardHIDType(KeyboardHIDType_T aKeyboardHIDType)
1139{
1140 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1141
1142 HRESULT rc = i_checkStateDependency(MutableStateDep);
1143 if (FAILED(rc)) return rc;
1144
1145 i_setModified(IsModified_MachineData);
1146 mHWData.backup();
1147 mHWData->mKeyboardHIDType = aKeyboardHIDType;
1148
1149 return S_OK;
1150}
1151
1152HRESULT Machine::getPointingHIDType(PointingHIDType_T *aPointingHIDType)
1153{
1154 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1155
1156 *aPointingHIDType = mHWData->mPointingHIDType;
1157
1158 return S_OK;
1159}
1160
1161HRESULT Machine::setPointingHIDType(PointingHIDType_T aPointingHIDType)
1162{
1163 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1164
1165 HRESULT rc = i_checkStateDependency(MutableStateDep);
1166 if (FAILED(rc)) return rc;
1167
1168 i_setModified(IsModified_MachineData);
1169 mHWData.backup();
1170 mHWData->mPointingHIDType = aPointingHIDType;
1171
1172 return S_OK;
1173}
1174
1175HRESULT Machine::getChipsetType(ChipsetType_T *aChipsetType)
1176{
1177 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1178
1179 *aChipsetType = mHWData->mChipsetType;
1180
1181 return S_OK;
1182}
1183
1184HRESULT Machine::setChipsetType(ChipsetType_T aChipsetType)
1185{
1186 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1187
1188 HRESULT rc = i_checkStateDependency(MutableStateDep);
1189 if (FAILED(rc)) return rc;
1190
1191 if (aChipsetType != mHWData->mChipsetType)
1192 {
1193 i_setModified(IsModified_MachineData);
1194 mHWData.backup();
1195 mHWData->mChipsetType = aChipsetType;
1196
1197 // Resize network adapter array, to be finalized on commit/rollback.
1198 // We must not throw away entries yet, otherwise settings are lost
1199 // without a way to roll back.
1200 size_t newCount = Global::getMaxNetworkAdapters(aChipsetType);
1201 size_t oldCount = mNetworkAdapters.size();
1202 if (newCount > oldCount)
1203 {
1204 mNetworkAdapters.resize(newCount);
1205 for (size_t slot = oldCount; slot < mNetworkAdapters.size(); slot++)
1206 {
1207 unconst(mNetworkAdapters[slot]).createObject();
1208 mNetworkAdapters[slot]->init(this, (ULONG)slot);
1209 }
1210 }
1211 }
1212
1213 return S_OK;
1214}
1215
1216HRESULT Machine::getParavirtProvider(ParavirtProvider_T *aParavirtProvider)
1217{
1218 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1219
1220 *aParavirtProvider = mHWData->mParavirtProvider;
1221
1222 return S_OK;
1223}
1224
1225HRESULT Machine::setParavirtProvider(ParavirtProvider_T aParavirtProvider)
1226{
1227 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1228
1229 HRESULT rc = i_checkStateDependency(MutableStateDep);
1230 if (FAILED(rc)) return rc;
1231
1232 if (aParavirtProvider != mHWData->mParavirtProvider)
1233 {
1234 i_setModified(IsModified_MachineData);
1235 mHWData.backup();
1236 mHWData->mParavirtProvider = aParavirtProvider;
1237 }
1238
1239 return S_OK;
1240}
1241
1242HRESULT Machine::getEffectiveParavirtProvider(ParavirtProvider_T *aParavirtProvider)
1243{
1244 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1245
1246 *aParavirtProvider = mHWData->mParavirtProvider;
1247 switch (mHWData->mParavirtProvider)
1248 {
1249 case ParavirtProvider_None:
1250 case ParavirtProvider_HyperV:
1251 case ParavirtProvider_KVM:
1252 case ParavirtProvider_Minimal:
1253 break;
1254
1255 /* Resolve dynamic provider types to the effective types. */
1256 default:
1257 {
1258 ComPtr<IGuestOSType> ptrGuestOSType;
1259 HRESULT hrc2 = mParent->GetGuestOSType(Bstr(mUserData->s.strOsType).raw(), ptrGuestOSType.asOutParam());
1260 AssertMsgReturn(SUCCEEDED(hrc2), ("Failed to get guest OS type. hrc2=%Rhrc\n", hrc2), hrc2);
1261
1262 Bstr guestTypeFamilyId;
1263 hrc2 = ptrGuestOSType->COMGETTER(FamilyId)(guestTypeFamilyId.asOutParam());
1264 AssertMsgReturn(SUCCEEDED(hrc2), ("Failed to get guest family. hrc2=%Rhrc\n", hrc2), hrc2);
1265 BOOL fOsXGuest = guestTypeFamilyId == Bstr("MacOS");
1266
1267 switch (mHWData->mParavirtProvider)
1268 {
1269 case ParavirtProvider_Legacy:
1270 {
1271 if (fOsXGuest)
1272 *aParavirtProvider = ParavirtProvider_Minimal;
1273 else
1274 *aParavirtProvider = ParavirtProvider_None;
1275 break;
1276 }
1277
1278 case ParavirtProvider_Default:
1279 {
1280 if (fOsXGuest)
1281 *aParavirtProvider = ParavirtProvider_Minimal;
1282 else if ( mUserData->s.strOsType == "Windows10"
1283 || mUserData->s.strOsType == "Windows10_64"
1284 || mUserData->s.strOsType == "Windows81"
1285 || mUserData->s.strOsType == "Windows81_64"
1286 || mUserData->s.strOsType == "Windows8"
1287 || mUserData->s.strOsType == "Windows8_64"
1288 || mUserData->s.strOsType == "Windows7"
1289 || mUserData->s.strOsType == "Windows7_64"
1290 || mUserData->s.strOsType == "WindowsVista"
1291 || mUserData->s.strOsType == "WindowsVista_64"
1292 || mUserData->s.strOsType == "Windows2012"
1293 || mUserData->s.strOsType == "Windows2012_64"
1294 || mUserData->s.strOsType == "Windows2008"
1295 || mUserData->s.strOsType == "Windows2008_64")
1296 {
1297 *aParavirtProvider = ParavirtProvider_HyperV;
1298 }
1299 else if ( mUserData->s.strOsType == "Linux26" // Linux22 and Linux24 omitted as they're too old
1300 || mUserData->s.strOsType == "Linux26_64" // for having any KVM paravirtualization support.
1301 || mUserData->s.strOsType == "Linux"
1302 || mUserData->s.strOsType == "Linux_64"
1303 || mUserData->s.strOsType == "ArchLinux"
1304 || mUserData->s.strOsType == "ArchLinux_64"
1305 || mUserData->s.strOsType == "Debian"
1306 || mUserData->s.strOsType == "Debian_64"
1307 || mUserData->s.strOsType == "Fedora"
1308 || mUserData->s.strOsType == "Fedora_64"
1309 || mUserData->s.strOsType == "Gentoo"
1310 || mUserData->s.strOsType == "Gentoo_64"
1311 || mUserData->s.strOsType == "Mandriva"
1312 || mUserData->s.strOsType == "Mandriva_64"
1313 || mUserData->s.strOsType == "OpenSUSE"
1314 || mUserData->s.strOsType == "OpenSUSE_64"
1315 || mUserData->s.strOsType == "Oracle"
1316 || mUserData->s.strOsType == "Oracle_64"
1317 || mUserData->s.strOsType == "RedHat"
1318 || mUserData->s.strOsType == "RedHat_64"
1319 || mUserData->s.strOsType == "Turbolinux"
1320 || mUserData->s.strOsType == "Turbolinux_64"
1321 || mUserData->s.strOsType == "Ubuntu"
1322 || mUserData->s.strOsType == "Ubuntu_64"
1323 || mUserData->s.strOsType == "Xandros"
1324 || mUserData->s.strOsType == "Xandros_64")
1325 {
1326 *aParavirtProvider = ParavirtProvider_KVM;
1327 }
1328 else
1329 *aParavirtProvider = ParavirtProvider_None;
1330 break;
1331 }
1332 }
1333 break;
1334 }
1335 }
1336
1337 Assert( *aParavirtProvider == ParavirtProvider_None
1338 || *aParavirtProvider == ParavirtProvider_Minimal
1339 || *aParavirtProvider == ParavirtProvider_HyperV
1340 || *aParavirtProvider == ParavirtProvider_KVM);
1341 return S_OK;
1342}
1343
1344HRESULT Machine::getHardwareVersion(com::Utf8Str &aHardwareVersion)
1345{
1346 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1347
1348 aHardwareVersion = mHWData->mHWVersion;
1349
1350 return S_OK;
1351}
1352
1353HRESULT Machine::setHardwareVersion(const com::Utf8Str &aHardwareVersion)
1354{
1355 /* check known version */
1356 Utf8Str hwVersion = aHardwareVersion;
1357 if ( hwVersion.compare("1") != 0
1358 && hwVersion.compare("2") != 0)
1359 return setError(E_INVALIDARG,
1360 tr("Invalid hardware version: %s\n"), aHardwareVersion.c_str());
1361
1362 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1363
1364 HRESULT rc = i_checkStateDependency(MutableStateDep);
1365 if (FAILED(rc)) return rc;
1366
1367 i_setModified(IsModified_MachineData);
1368 mHWData.backup();
1369 mHWData->mHWVersion = aHardwareVersion;
1370
1371 return S_OK;
1372}
1373
1374HRESULT Machine::getHardwareUUID(com::Guid &aHardwareUUID)
1375{
1376 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1377
1378 if (!mHWData->mHardwareUUID.isZero())
1379 aHardwareUUID = mHWData->mHardwareUUID;
1380 else
1381 aHardwareUUID = mData->mUuid;
1382
1383 return S_OK;
1384}
1385
1386HRESULT Machine::setHardwareUUID(const com::Guid &aHardwareUUID)
1387{
1388 if (!aHardwareUUID.isValid())
1389 return E_INVALIDARG;
1390
1391 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1392
1393 HRESULT rc = i_checkStateDependency(MutableStateDep);
1394 if (FAILED(rc)) return rc;
1395
1396 i_setModified(IsModified_MachineData);
1397 mHWData.backup();
1398 if (aHardwareUUID == mData->mUuid)
1399 mHWData->mHardwareUUID.clear();
1400 else
1401 mHWData->mHardwareUUID = aHardwareUUID;
1402
1403 return S_OK;
1404}
1405
1406HRESULT Machine::getMemorySize(ULONG *aMemorySize)
1407{
1408 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1409
1410 *aMemorySize = mHWData->mMemorySize;
1411
1412 return S_OK;
1413}
1414
1415HRESULT Machine::setMemorySize(ULONG aMemorySize)
1416{
1417 /* check RAM limits */
1418 if ( aMemorySize < MM_RAM_MIN_IN_MB
1419 || aMemorySize > MM_RAM_MAX_IN_MB
1420 )
1421 return setError(E_INVALIDARG,
1422 tr("Invalid RAM size: %lu MB (must be in range [%lu, %lu] MB)"),
1423 aMemorySize, MM_RAM_MIN_IN_MB, MM_RAM_MAX_IN_MB);
1424
1425 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1426
1427 HRESULT rc = i_checkStateDependency(MutableStateDep);
1428 if (FAILED(rc)) return rc;
1429
1430 i_setModified(IsModified_MachineData);
1431 mHWData.backup();
1432 mHWData->mMemorySize = aMemorySize;
1433
1434 return S_OK;
1435}
1436
1437HRESULT Machine::getCPUCount(ULONG *aCPUCount)
1438{
1439 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1440
1441 *aCPUCount = mHWData->mCPUCount;
1442
1443 return S_OK;
1444}
1445
1446HRESULT Machine::setCPUCount(ULONG aCPUCount)
1447{
1448 /* check CPU limits */
1449 if ( aCPUCount < SchemaDefs::MinCPUCount
1450 || aCPUCount > SchemaDefs::MaxCPUCount
1451 )
1452 return setError(E_INVALIDARG,
1453 tr("Invalid virtual CPU count: %lu (must be in range [%lu, %lu])"),
1454 aCPUCount, SchemaDefs::MinCPUCount, SchemaDefs::MaxCPUCount);
1455
1456 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1457
1458 /* We cant go below the current number of CPUs attached if hotplug is enabled*/
1459 if (mHWData->mCPUHotPlugEnabled)
1460 {
1461 for (unsigned idx = aCPUCount; idx < SchemaDefs::MaxCPUCount; idx++)
1462 {
1463 if (mHWData->mCPUAttached[idx])
1464 return setError(E_INVALIDARG,
1465 tr("There is still a CPU attached to socket %lu."
1466 "Detach the CPU before removing the socket"),
1467 aCPUCount, idx+1);
1468 }
1469 }
1470
1471 HRESULT rc = i_checkStateDependency(MutableStateDep);
1472 if (FAILED(rc)) return rc;
1473
1474 i_setModified(IsModified_MachineData);
1475 mHWData.backup();
1476 mHWData->mCPUCount = aCPUCount;
1477
1478 return S_OK;
1479}
1480
1481HRESULT Machine::getCPUExecutionCap(ULONG *aCPUExecutionCap)
1482{
1483 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1484
1485 *aCPUExecutionCap = mHWData->mCpuExecutionCap;
1486
1487 return S_OK;
1488}
1489
1490HRESULT Machine::setCPUExecutionCap(ULONG aCPUExecutionCap)
1491{
1492 HRESULT rc = S_OK;
1493
1494 /* check throttle limits */
1495 if ( aCPUExecutionCap < 1
1496 || aCPUExecutionCap > 100
1497 )
1498 return setError(E_INVALIDARG,
1499 tr("Invalid CPU execution cap value: %lu (must be in range [%lu, %lu])"),
1500 aCPUExecutionCap, 1, 100);
1501
1502 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1503
1504 alock.release();
1505 rc = i_onCPUExecutionCapChange(aCPUExecutionCap);
1506 alock.acquire();
1507 if (FAILED(rc)) return rc;
1508
1509 i_setModified(IsModified_MachineData);
1510 mHWData.backup();
1511 mHWData->mCpuExecutionCap = aCPUExecutionCap;
1512
1513 /** Save settings if online - @todo why is this required? -- @bugref{6818} */
1514 if (Global::IsOnline(mData->mMachineState))
1515 i_saveSettings(NULL);
1516
1517 return S_OK;
1518}
1519
1520HRESULT Machine::getCPUHotPlugEnabled(BOOL *aCPUHotPlugEnabled)
1521{
1522 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1523
1524 *aCPUHotPlugEnabled = mHWData->mCPUHotPlugEnabled;
1525
1526 return S_OK;
1527}
1528
1529HRESULT Machine::setCPUHotPlugEnabled(BOOL aCPUHotPlugEnabled)
1530{
1531 HRESULT rc = S_OK;
1532
1533 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1534
1535 rc = i_checkStateDependency(MutableStateDep);
1536 if (FAILED(rc)) return rc;
1537
1538 if (mHWData->mCPUHotPlugEnabled != aCPUHotPlugEnabled)
1539 {
1540 if (aCPUHotPlugEnabled)
1541 {
1542 i_setModified(IsModified_MachineData);
1543 mHWData.backup();
1544
1545 /* Add the amount of CPUs currently attached */
1546 for (unsigned i = 0; i < mHWData->mCPUCount; ++i)
1547 mHWData->mCPUAttached[i] = true;
1548 }
1549 else
1550 {
1551 /*
1552 * We can disable hotplug only if the amount of maximum CPUs is equal
1553 * to the amount of attached CPUs
1554 */
1555 unsigned cCpusAttached = 0;
1556 unsigned iHighestId = 0;
1557
1558 for (unsigned i = 0; i < SchemaDefs::MaxCPUCount; ++i)
1559 {
1560 if (mHWData->mCPUAttached[i])
1561 {
1562 cCpusAttached++;
1563 iHighestId = i;
1564 }
1565 }
1566
1567 if ( (cCpusAttached != mHWData->mCPUCount)
1568 || (iHighestId >= mHWData->mCPUCount))
1569 return setError(E_INVALIDARG,
1570 tr("CPU hotplugging can't be disabled because the maximum number of CPUs is not equal to the amount of CPUs attached"));
1571
1572 i_setModified(IsModified_MachineData);
1573 mHWData.backup();
1574 }
1575 }
1576
1577 mHWData->mCPUHotPlugEnabled = aCPUHotPlugEnabled;
1578
1579 return rc;
1580}
1581
1582HRESULT Machine::getCPUIDPortabilityLevel(ULONG *aCPUIDPortabilityLevel)
1583{
1584 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1585
1586 *aCPUIDPortabilityLevel = mHWData->mCpuIdPortabilityLevel;
1587
1588 return S_OK;
1589}
1590
1591HRESULT Machine::setCPUIDPortabilityLevel(ULONG aCPUIDPortabilityLevel)
1592{
1593 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1594
1595 HRESULT hrc = i_checkStateDependency(MutableStateDep);
1596 if (SUCCEEDED(hrc))
1597 {
1598 i_setModified(IsModified_MachineData);
1599 mHWData.backup();
1600 mHWData->mCpuIdPortabilityLevel = aCPUIDPortabilityLevel;
1601 }
1602 return hrc;
1603}
1604
1605HRESULT Machine::getEmulatedUSBCardReaderEnabled(BOOL *aEmulatedUSBCardReaderEnabled)
1606{
1607#ifdef VBOX_WITH_USB_CARDREADER
1608 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1609
1610 *aEmulatedUSBCardReaderEnabled = mHWData->mEmulatedUSBCardReaderEnabled;
1611
1612 return S_OK;
1613#else
1614 NOREF(aEmulatedUSBCardReaderEnabled);
1615 return E_NOTIMPL;
1616#endif
1617}
1618
1619HRESULT Machine::setEmulatedUSBCardReaderEnabled(BOOL aEmulatedUSBCardReaderEnabled)
1620{
1621#ifdef VBOX_WITH_USB_CARDREADER
1622 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1623
1624 HRESULT rc = i_checkStateDependency(MutableOrSavedStateDep);
1625 if (FAILED(rc)) return rc;
1626
1627 i_setModified(IsModified_MachineData);
1628 mHWData.backup();
1629 mHWData->mEmulatedUSBCardReaderEnabled = aEmulatedUSBCardReaderEnabled;
1630
1631 return S_OK;
1632#else
1633 NOREF(aEmulatedUSBCardReaderEnabled);
1634 return E_NOTIMPL;
1635#endif
1636}
1637
1638HRESULT Machine::getHPETEnabled(BOOL *aHPETEnabled)
1639{
1640 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1641
1642 *aHPETEnabled = mHWData->mHPETEnabled;
1643
1644 return S_OK;
1645}
1646
1647HRESULT Machine::setHPETEnabled(BOOL aHPETEnabled)
1648{
1649 HRESULT rc = S_OK;
1650
1651 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1652
1653 rc = i_checkStateDependency(MutableStateDep);
1654 if (FAILED(rc)) return rc;
1655
1656 i_setModified(IsModified_MachineData);
1657 mHWData.backup();
1658
1659 mHWData->mHPETEnabled = aHPETEnabled;
1660
1661 return rc;
1662}
1663
1664HRESULT Machine::getVideoCaptureEnabled(BOOL *aVideoCaptureEnabled)
1665{
1666 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1667
1668 *aVideoCaptureEnabled = mHWData->mVideoCaptureEnabled;
1669 return S_OK;
1670}
1671
1672HRESULT Machine::setVideoCaptureEnabled(BOOL aVideoCaptureEnabled)
1673{
1674 HRESULT rc = S_OK;
1675
1676 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1677
1678 i_setModified(IsModified_MachineData);
1679 mHWData.backup();
1680 mHWData->mVideoCaptureEnabled = aVideoCaptureEnabled;
1681
1682 alock.release();
1683 rc = i_onVideoCaptureChange();
1684 alock.acquire();
1685 if (FAILED(rc))
1686 {
1687 /*
1688 * Normally we would do the actual change _after_ i_onVideoCaptureChange() succeeded.
1689 * We cannot do this because that function uses Machine::GetVideoCaptureEnabled to
1690 * determine if it should start or stop capturing. Therefore we need to manually
1691 * undo change.
1692 */
1693 mHWData->mVideoCaptureEnabled = mHWData.backedUpData()->mVideoCaptureEnabled;
1694 return rc;
1695 }
1696
1697 /** Save settings if online - @todo why is this required? -- @bugref{6818} */
1698 if (Global::IsOnline(mData->mMachineState))
1699 i_saveSettings(NULL);
1700
1701 return rc;
1702}
1703
1704HRESULT Machine::getVideoCaptureScreens(std::vector<BOOL> &aVideoCaptureScreens)
1705{
1706 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1707 aVideoCaptureScreens.resize(mHWData->mMonitorCount);
1708 for (unsigned i = 0; i < mHWData->mMonitorCount; ++i)
1709 aVideoCaptureScreens[i] = mHWData->maVideoCaptureScreens[i];
1710 return S_OK;
1711}
1712
1713HRESULT Machine::setVideoCaptureScreens(const std::vector<BOOL> &aVideoCaptureScreens)
1714{
1715 AssertReturn(aVideoCaptureScreens.size() <= RT_ELEMENTS(mHWData->maVideoCaptureScreens), E_INVALIDARG);
1716 bool fChanged = false;
1717
1718 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1719
1720 for (unsigned i = 0; i < aVideoCaptureScreens.size(); ++i)
1721 {
1722 if (mHWData->maVideoCaptureScreens[i] != RT_BOOL(aVideoCaptureScreens[i]))
1723 {
1724 mHWData->maVideoCaptureScreens[i] = RT_BOOL(aVideoCaptureScreens[i]);
1725 fChanged = true;
1726 }
1727 }
1728 if (fChanged)
1729 {
1730 alock.release();
1731 HRESULT rc = i_onVideoCaptureChange();
1732 alock.acquire();
1733 if (FAILED(rc)) return rc;
1734 i_setModified(IsModified_MachineData);
1735
1736 /** Save settings if online - @todo why is this required? -- @bugref{6818} */
1737 if (Global::IsOnline(mData->mMachineState))
1738 i_saveSettings(NULL);
1739 }
1740
1741 return S_OK;
1742}
1743
1744HRESULT Machine::getVideoCaptureFile(com::Utf8Str &aVideoCaptureFile)
1745{
1746 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1747 if (mHWData->mVideoCaptureFile.isEmpty())
1748 i_getDefaultVideoCaptureFile(aVideoCaptureFile);
1749 else
1750 aVideoCaptureFile = mHWData->mVideoCaptureFile;
1751 return S_OK;
1752}
1753
1754HRESULT Machine::setVideoCaptureFile(const com::Utf8Str &aVideoCaptureFile)
1755{
1756 Utf8Str strFile(aVideoCaptureFile);
1757 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1758
1759 if ( Global::IsOnline(mData->mMachineState)
1760 && mHWData->mVideoCaptureEnabled)
1761 return setError(E_INVALIDARG, tr("Cannot change parameters while capturing is enabled"));
1762
1763 if (!RTPathStartsWithRoot(strFile.c_str()))
1764 return setError(E_INVALIDARG, tr("Video capture file name '%s' is not absolute"), strFile.c_str());
1765
1766 if (!strFile.isEmpty())
1767 {
1768 Utf8Str defaultFile;
1769 i_getDefaultVideoCaptureFile(defaultFile);
1770 if (!RTPathCompare(strFile.c_str(), defaultFile.c_str()))
1771 strFile.setNull();
1772 }
1773
1774 i_setModified(IsModified_MachineData);
1775 mHWData.backup();
1776 mHWData->mVideoCaptureFile = strFile;
1777
1778 return S_OK;
1779}
1780
1781HRESULT Machine::getVideoCaptureWidth(ULONG *aVideoCaptureWidth)
1782{
1783 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1784 *aVideoCaptureWidth = mHWData->mVideoCaptureWidth;
1785 return S_OK;
1786}
1787
1788HRESULT Machine::setVideoCaptureWidth(ULONG aVideoCaptureWidth)
1789{
1790 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1791
1792 if ( Global::IsOnline(mData->mMachineState)
1793 && mHWData->mVideoCaptureEnabled)
1794 return setError(E_INVALIDARG, tr("Cannot change parameters while capturing is enabled"));
1795
1796 i_setModified(IsModified_MachineData);
1797 mHWData.backup();
1798 mHWData->mVideoCaptureWidth = aVideoCaptureWidth;
1799
1800 return S_OK;
1801}
1802
1803HRESULT Machine::getVideoCaptureHeight(ULONG *aVideoCaptureHeight)
1804{
1805 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1806 *aVideoCaptureHeight = mHWData->mVideoCaptureHeight;
1807 return S_OK;
1808}
1809
1810HRESULT Machine::setVideoCaptureHeight(ULONG aVideoCaptureHeight)
1811{
1812 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1813
1814 if ( Global::IsOnline(mData->mMachineState)
1815 && mHWData->mVideoCaptureEnabled)
1816 return setError(E_INVALIDARG, tr("Cannot change parameters while capturing is enabled"));
1817
1818 i_setModified(IsModified_MachineData);
1819 mHWData.backup();
1820 mHWData->mVideoCaptureHeight = aVideoCaptureHeight;
1821
1822 return S_OK;
1823}
1824
1825HRESULT Machine::getVideoCaptureRate(ULONG *aVideoCaptureRate)
1826{
1827 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1828 *aVideoCaptureRate = mHWData->mVideoCaptureRate;
1829 return S_OK;
1830}
1831
1832HRESULT Machine::setVideoCaptureRate(ULONG aVideoCaptureRate)
1833{
1834 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1835
1836 if ( Global::IsOnline(mData->mMachineState)
1837 && mHWData->mVideoCaptureEnabled)
1838 return setError(E_INVALIDARG, tr("Cannot change parameters while capturing is enabled"));
1839
1840 i_setModified(IsModified_MachineData);
1841 mHWData.backup();
1842 mHWData->mVideoCaptureRate = aVideoCaptureRate;
1843
1844 return S_OK;
1845}
1846
1847HRESULT Machine::getVideoCaptureFPS(ULONG *aVideoCaptureFPS)
1848{
1849 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1850 *aVideoCaptureFPS = mHWData->mVideoCaptureFPS;
1851 return S_OK;
1852}
1853
1854HRESULT Machine::setVideoCaptureFPS(ULONG aVideoCaptureFPS)
1855{
1856 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1857
1858 if ( Global::IsOnline(mData->mMachineState)
1859 && mHWData->mVideoCaptureEnabled)
1860 return setError(E_INVALIDARG, tr("Cannot change parameters while capturing is enabled"));
1861
1862 i_setModified(IsModified_MachineData);
1863 mHWData.backup();
1864 mHWData->mVideoCaptureFPS = aVideoCaptureFPS;
1865
1866 return S_OK;
1867}
1868
1869HRESULT Machine::getVideoCaptureMaxTime(ULONG *aVideoCaptureMaxTime)
1870{
1871 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1872 *aVideoCaptureMaxTime = mHWData->mVideoCaptureMaxTime;
1873 return S_OK;
1874}
1875
1876HRESULT Machine::setVideoCaptureMaxTime(ULONG aVideoCaptureMaxTime)
1877{
1878 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1879
1880 if ( Global::IsOnline(mData->mMachineState)
1881 && mHWData->mVideoCaptureEnabled)
1882 return setError(E_INVALIDARG, tr("Cannot change parameters while capturing is enabled"));
1883
1884 i_setModified(IsModified_MachineData);
1885 mHWData.backup();
1886 mHWData->mVideoCaptureMaxTime = aVideoCaptureMaxTime;
1887
1888 return S_OK;
1889}
1890
1891HRESULT Machine::getVideoCaptureMaxFileSize(ULONG *aVideoCaptureMaxFileSize)
1892{
1893 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1894 *aVideoCaptureMaxFileSize = mHWData->mVideoCaptureMaxFileSize;
1895 return S_OK;
1896}
1897
1898HRESULT Machine::setVideoCaptureMaxFileSize(ULONG aVideoCaptureMaxFileSize)
1899{
1900 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1901
1902 if ( Global::IsOnline(mData->mMachineState)
1903 && mHWData->mVideoCaptureEnabled)
1904 return setError(E_INVALIDARG, tr("Cannot change parameters while capturing is enabled"));
1905
1906 i_setModified(IsModified_MachineData);
1907 mHWData.backup();
1908 mHWData->mVideoCaptureMaxFileSize = aVideoCaptureMaxFileSize;
1909
1910 return S_OK;
1911}
1912
1913HRESULT Machine::getVideoCaptureOptions(com::Utf8Str &aVideoCaptureOptions)
1914{
1915 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1916
1917 aVideoCaptureOptions = mHWData->mVideoCaptureOptions;
1918 return S_OK;
1919}
1920
1921HRESULT Machine::setVideoCaptureOptions(const com::Utf8Str &aVideoCaptureOptions)
1922{
1923 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1924
1925 if ( Global::IsOnline(mData->mMachineState)
1926 && mHWData->mVideoCaptureEnabled)
1927 return setError(E_INVALIDARG, tr("Cannot change parameters while capturing is enabled"));
1928
1929 i_setModified(IsModified_MachineData);
1930 mHWData.backup();
1931 mHWData->mVideoCaptureOptions = aVideoCaptureOptions;
1932
1933 return S_OK;
1934}
1935
1936HRESULT Machine::getGraphicsControllerType(GraphicsControllerType_T *aGraphicsControllerType)
1937{
1938 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1939
1940 *aGraphicsControllerType = mHWData->mGraphicsControllerType;
1941
1942 return S_OK;
1943}
1944
1945HRESULT Machine::setGraphicsControllerType(GraphicsControllerType_T aGraphicsControllerType)
1946{
1947 switch (aGraphicsControllerType)
1948 {
1949 case GraphicsControllerType_Null:
1950 case GraphicsControllerType_VBoxVGA:
1951#ifdef VBOX_WITH_VMSVGA
1952 case GraphicsControllerType_VMSVGA:
1953#endif
1954 break;
1955 default:
1956 return setError(E_INVALIDARG, tr("The graphics controller type (%d) is invalid"), aGraphicsControllerType);
1957 }
1958
1959 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1960
1961 HRESULT rc = i_checkStateDependency(MutableStateDep);
1962 if (FAILED(rc)) return rc;
1963
1964 i_setModified(IsModified_MachineData);
1965 mHWData.backup();
1966 mHWData->mGraphicsControllerType = aGraphicsControllerType;
1967
1968 return S_OK;
1969}
1970
1971HRESULT Machine::getVRAMSize(ULONG *aVRAMSize)
1972{
1973 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1974
1975 *aVRAMSize = mHWData->mVRAMSize;
1976
1977 return S_OK;
1978}
1979
1980HRESULT Machine::setVRAMSize(ULONG aVRAMSize)
1981{
1982 /* check VRAM limits */
1983 if (aVRAMSize < SchemaDefs::MinGuestVRAM ||
1984 aVRAMSize > SchemaDefs::MaxGuestVRAM)
1985 return setError(E_INVALIDARG,
1986 tr("Invalid VRAM size: %lu MB (must be in range [%lu, %lu] MB)"),
1987 aVRAMSize, SchemaDefs::MinGuestVRAM, SchemaDefs::MaxGuestVRAM);
1988
1989 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1990
1991 HRESULT rc = i_checkStateDependency(MutableStateDep);
1992 if (FAILED(rc)) return rc;
1993
1994 i_setModified(IsModified_MachineData);
1995 mHWData.backup();
1996 mHWData->mVRAMSize = aVRAMSize;
1997
1998 return S_OK;
1999}
2000
2001/** @todo this method should not be public */
2002HRESULT Machine::getMemoryBalloonSize(ULONG *aMemoryBalloonSize)
2003{
2004 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2005
2006 *aMemoryBalloonSize = mHWData->mMemoryBalloonSize;
2007
2008 return S_OK;
2009}
2010
2011/**
2012 * Set the memory balloon size.
2013 *
2014 * This method is also called from IGuest::COMSETTER(MemoryBalloonSize) so
2015 * we have to make sure that we never call IGuest from here.
2016 */
2017HRESULT Machine::setMemoryBalloonSize(ULONG aMemoryBalloonSize)
2018{
2019 /* This must match GMMR0Init; currently we only support memory ballooning on all 64-bit hosts except Mac OS X */
2020#if HC_ARCH_BITS == 64 && (defined(RT_OS_WINDOWS) || defined(RT_OS_SOLARIS) || defined(RT_OS_LINUX) || defined(RT_OS_FREEBSD))
2021 /* check limits */
2022 if (aMemoryBalloonSize >= VMMDEV_MAX_MEMORY_BALLOON(mHWData->mMemorySize))
2023 return setError(E_INVALIDARG,
2024 tr("Invalid memory balloon size: %lu MB (must be in range [%lu, %lu] MB)"),
2025 aMemoryBalloonSize, 0, VMMDEV_MAX_MEMORY_BALLOON(mHWData->mMemorySize));
2026
2027 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2028
2029 i_setModified(IsModified_MachineData);
2030 mHWData.backup();
2031 mHWData->mMemoryBalloonSize = aMemoryBalloonSize;
2032
2033 return S_OK;
2034#else
2035 NOREF(aMemoryBalloonSize);
2036 return setError(E_NOTIMPL, tr("Memory ballooning is only supported on 64-bit hosts"));
2037#endif
2038}
2039
2040HRESULT Machine::getPageFusionEnabled(BOOL *aPageFusionEnabled)
2041{
2042 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2043
2044 *aPageFusionEnabled = mHWData->mPageFusionEnabled;
2045 return S_OK;
2046}
2047
2048HRESULT Machine::setPageFusionEnabled(BOOL aPageFusionEnabled)
2049{
2050#ifdef VBOX_WITH_PAGE_SHARING
2051 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2052
2053 /** @todo must support changes for running vms and keep this in sync with IGuest. */
2054 i_setModified(IsModified_MachineData);
2055 mHWData.backup();
2056 mHWData->mPageFusionEnabled = aPageFusionEnabled;
2057 return S_OK;
2058#else
2059 NOREF(aPageFusionEnabled);
2060 return setError(E_NOTIMPL, tr("Page fusion is only supported on 64-bit hosts"));
2061#endif
2062}
2063
2064HRESULT Machine::getAccelerate3DEnabled(BOOL *aAccelerate3DEnabled)
2065{
2066 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2067
2068 *aAccelerate3DEnabled = mHWData->mAccelerate3DEnabled;
2069
2070 return S_OK;
2071}
2072
2073HRESULT Machine::setAccelerate3DEnabled(BOOL aAccelerate3DEnabled)
2074{
2075 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2076
2077 HRESULT rc = i_checkStateDependency(MutableStateDep);
2078 if (FAILED(rc)) return rc;
2079
2080 /** @todo check validity! */
2081
2082 i_setModified(IsModified_MachineData);
2083 mHWData.backup();
2084 mHWData->mAccelerate3DEnabled = aAccelerate3DEnabled;
2085
2086 return S_OK;
2087}
2088
2089
2090HRESULT Machine::getAccelerate2DVideoEnabled(BOOL *aAccelerate2DVideoEnabled)
2091{
2092 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2093
2094 *aAccelerate2DVideoEnabled = mHWData->mAccelerate2DVideoEnabled;
2095
2096 return S_OK;
2097}
2098
2099HRESULT Machine::setAccelerate2DVideoEnabled(BOOL aAccelerate2DVideoEnabled)
2100{
2101 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2102
2103 HRESULT rc = i_checkStateDependency(MutableStateDep);
2104 if (FAILED(rc)) return rc;
2105
2106 /** @todo check validity! */
2107 i_setModified(IsModified_MachineData);
2108 mHWData.backup();
2109 mHWData->mAccelerate2DVideoEnabled = aAccelerate2DVideoEnabled;
2110
2111 return S_OK;
2112}
2113
2114HRESULT Machine::getMonitorCount(ULONG *aMonitorCount)
2115{
2116 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2117
2118 *aMonitorCount = mHWData->mMonitorCount;
2119
2120 return S_OK;
2121}
2122
2123HRESULT Machine::setMonitorCount(ULONG aMonitorCount)
2124{
2125 /* make sure monitor count is a sensible number */
2126 if (aMonitorCount < 1 || aMonitorCount > SchemaDefs::MaxGuestMonitors)
2127 return setError(E_INVALIDARG,
2128 tr("Invalid monitor count: %lu (must be in range [%lu, %lu])"),
2129 aMonitorCount, 1, SchemaDefs::MaxGuestMonitors);
2130
2131 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2132
2133 HRESULT rc = i_checkStateDependency(MutableStateDep);
2134 if (FAILED(rc)) return rc;
2135
2136 i_setModified(IsModified_MachineData);
2137 mHWData.backup();
2138 mHWData->mMonitorCount = aMonitorCount;
2139
2140 return S_OK;
2141}
2142
2143HRESULT Machine::getBIOSSettings(ComPtr<IBIOSSettings> &aBIOSSettings)
2144{
2145 /* mBIOSSettings is constant during life time, no need to lock */
2146 aBIOSSettings = mBIOSSettings;
2147
2148 return S_OK;
2149}
2150
2151HRESULT Machine::getCPUProperty(CPUPropertyType_T aProperty, BOOL *aValue)
2152{
2153 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2154
2155 switch (aProperty)
2156 {
2157 case CPUPropertyType_PAE:
2158 *aValue = mHWData->mPAEEnabled;
2159 break;
2160
2161 case CPUPropertyType_LongMode:
2162 if (mHWData->mLongMode == settings::Hardware::LongMode_Enabled)
2163 *aValue = TRUE;
2164 else if (mHWData->mLongMode == settings::Hardware::LongMode_Disabled)
2165 *aValue = FALSE;
2166#if HC_ARCH_BITS == 64
2167 else
2168 *aValue = TRUE;
2169#else
2170 else
2171 {
2172 *aValue = FALSE;
2173
2174 ComPtr<IGuestOSType> ptrGuestOSType;
2175 HRESULT hrc2 = mParent->GetGuestOSType(Bstr(mUserData->s.strOsType).raw(), ptrGuestOSType.asOutParam());
2176 if (SUCCEEDED(hrc2))
2177 {
2178 BOOL fIs64Bit = FALSE;
2179 hrc2 = ptrGuestOSType->COMGETTER(Is64Bit)(&fIs64Bit); AssertComRC(hrc2);
2180 if (SUCCEEDED(hrc2) && fIs64Bit)
2181 {
2182 ComObjPtr<Host> ptrHost = mParent->i_host();
2183 alock.release();
2184
2185 hrc2 = ptrHost->GetProcessorFeature(ProcessorFeature_LongMode, aValue); AssertComRC(hrc2);
2186 if (FAILED(hrc2))
2187 *aValue = FALSE;
2188 }
2189 }
2190 }
2191#endif
2192 break;
2193
2194 case CPUPropertyType_TripleFaultReset:
2195 *aValue = mHWData->mTripleFaultReset;
2196 break;
2197
2198 default:
2199 return E_INVALIDARG;
2200 }
2201 return S_OK;
2202}
2203
2204HRESULT Machine::setCPUProperty(CPUPropertyType_T aProperty, BOOL aValue)
2205{
2206 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2207
2208 HRESULT rc = i_checkStateDependency(MutableStateDep);
2209 if (FAILED(rc)) return rc;
2210
2211 switch (aProperty)
2212 {
2213 case CPUPropertyType_PAE:
2214 i_setModified(IsModified_MachineData);
2215 mHWData.backup();
2216 mHWData->mPAEEnabled = !!aValue;
2217 break;
2218
2219 case CPUPropertyType_LongMode:
2220 i_setModified(IsModified_MachineData);
2221 mHWData.backup();
2222 mHWData->mLongMode = !aValue ? settings::Hardware::LongMode_Disabled : settings::Hardware::LongMode_Enabled;
2223 break;
2224
2225 case CPUPropertyType_TripleFaultReset:
2226 i_setModified(IsModified_MachineData);
2227 mHWData.backup();
2228 mHWData->mTripleFaultReset = !!aValue;
2229 break;
2230
2231 default:
2232 return E_INVALIDARG;
2233 }
2234 return S_OK;
2235}
2236
2237HRESULT Machine::getCPUIDLeaf(ULONG aId, ULONG *aValEax, ULONG *aValEbx, ULONG *aValEcx, ULONG *aValEdx)
2238{
2239 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2240
2241 switch(aId)
2242 {
2243 case 0x0:
2244 case 0x1:
2245 case 0x2:
2246 case 0x3:
2247 case 0x4:
2248 case 0x5:
2249 case 0x6:
2250 case 0x7:
2251 case 0x8:
2252 case 0x9:
2253 case 0xA:
2254 if (mHWData->mCpuIdStdLeafs[aId].ulId != aId)
2255 return E_INVALIDARG;
2256
2257 *aValEax = mHWData->mCpuIdStdLeafs[aId].ulEax;
2258 *aValEbx = mHWData->mCpuIdStdLeafs[aId].ulEbx;
2259 *aValEcx = mHWData->mCpuIdStdLeafs[aId].ulEcx;
2260 *aValEdx = mHWData->mCpuIdStdLeafs[aId].ulEdx;
2261 break;
2262
2263 case 0x80000000:
2264 case 0x80000001:
2265 case 0x80000002:
2266 case 0x80000003:
2267 case 0x80000004:
2268 case 0x80000005:
2269 case 0x80000006:
2270 case 0x80000007:
2271 case 0x80000008:
2272 case 0x80000009:
2273 case 0x8000000A:
2274 if (mHWData->mCpuIdExtLeafs[aId - 0x80000000].ulId != aId)
2275 return E_INVALIDARG;
2276
2277 *aValEax = mHWData->mCpuIdExtLeafs[aId - 0x80000000].ulEax;
2278 *aValEbx = mHWData->mCpuIdExtLeafs[aId - 0x80000000].ulEbx;
2279 *aValEcx = mHWData->mCpuIdExtLeafs[aId - 0x80000000].ulEcx;
2280 *aValEdx = mHWData->mCpuIdExtLeafs[aId - 0x80000000].ulEdx;
2281 break;
2282
2283 default:
2284 return setError(E_INVALIDARG, tr("CpuId override leaf %#x is out of range"), aId);
2285 }
2286 return S_OK;
2287}
2288
2289
2290HRESULT Machine::setCPUIDLeaf(ULONG aId, ULONG aValEax, ULONG aValEbx, ULONG aValEcx, ULONG aValEdx)
2291{
2292 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2293
2294 HRESULT rc = i_checkStateDependency(MutableStateDep);
2295 if (FAILED(rc)) return rc;
2296
2297 switch(aId)
2298 {
2299 case 0x0:
2300 case 0x1:
2301 case 0x2:
2302 case 0x3:
2303 case 0x4:
2304 case 0x5:
2305 case 0x6:
2306 case 0x7:
2307 case 0x8:
2308 case 0x9:
2309 case 0xA:
2310 AssertCompile(RT_ELEMENTS(mHWData->mCpuIdStdLeafs) == 0xB);
2311 AssertRelease(aId < RT_ELEMENTS(mHWData->mCpuIdStdLeafs));
2312 i_setModified(IsModified_MachineData);
2313 mHWData.backup();
2314 mHWData->mCpuIdStdLeafs[aId].ulId = aId;
2315 mHWData->mCpuIdStdLeafs[aId].ulEax = aValEax;
2316 mHWData->mCpuIdStdLeafs[aId].ulEbx = aValEbx;
2317 mHWData->mCpuIdStdLeafs[aId].ulEcx = aValEcx;
2318 mHWData->mCpuIdStdLeafs[aId].ulEdx = aValEdx;
2319 break;
2320
2321 case 0x80000000:
2322 case 0x80000001:
2323 case 0x80000002:
2324 case 0x80000003:
2325 case 0x80000004:
2326 case 0x80000005:
2327 case 0x80000006:
2328 case 0x80000007:
2329 case 0x80000008:
2330 case 0x80000009:
2331 case 0x8000000A:
2332 AssertCompile(RT_ELEMENTS(mHWData->mCpuIdExtLeafs) == 0xB);
2333 AssertRelease(aId - 0x80000000 < RT_ELEMENTS(mHWData->mCpuIdExtLeafs));
2334 i_setModified(IsModified_MachineData);
2335 mHWData.backup();
2336 mHWData->mCpuIdExtLeafs[aId - 0x80000000].ulId = aId;
2337 mHWData->mCpuIdExtLeafs[aId - 0x80000000].ulEax = aValEax;
2338 mHWData->mCpuIdExtLeafs[aId - 0x80000000].ulEbx = aValEbx;
2339 mHWData->mCpuIdExtLeafs[aId - 0x80000000].ulEcx = aValEcx;
2340 mHWData->mCpuIdExtLeafs[aId - 0x80000000].ulEdx = aValEdx;
2341 break;
2342
2343 default:
2344 return setError(E_INVALIDARG, tr("CpuId override leaf %#x is out of range"), aId);
2345 }
2346 return S_OK;
2347}
2348
2349HRESULT Machine::removeCPUIDLeaf(ULONG aId)
2350{
2351 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2352
2353 HRESULT rc = i_checkStateDependency(MutableStateDep);
2354 if (FAILED(rc)) return rc;
2355
2356 switch(aId)
2357 {
2358 case 0x0:
2359 case 0x1:
2360 case 0x2:
2361 case 0x3:
2362 case 0x4:
2363 case 0x5:
2364 case 0x6:
2365 case 0x7:
2366 case 0x8:
2367 case 0x9:
2368 case 0xA:
2369 AssertCompile(RT_ELEMENTS(mHWData->mCpuIdStdLeafs) == 0xB);
2370 AssertRelease(aId < RT_ELEMENTS(mHWData->mCpuIdStdLeafs));
2371 i_setModified(IsModified_MachineData);
2372 mHWData.backup();
2373 /* Invalidate leaf. */
2374 mHWData->mCpuIdStdLeafs[aId].ulId = UINT32_MAX;
2375 break;
2376
2377 case 0x80000000:
2378 case 0x80000001:
2379 case 0x80000002:
2380 case 0x80000003:
2381 case 0x80000004:
2382 case 0x80000005:
2383 case 0x80000006:
2384 case 0x80000007:
2385 case 0x80000008:
2386 case 0x80000009:
2387 case 0x8000000A:
2388 AssertCompile(RT_ELEMENTS(mHWData->mCpuIdExtLeafs) == 0xB);
2389 AssertRelease(aId - 0x80000000 < RT_ELEMENTS(mHWData->mCpuIdExtLeafs));
2390 i_setModified(IsModified_MachineData);
2391 mHWData.backup();
2392 /* Invalidate leaf. */
2393 mHWData->mCpuIdExtLeafs[aId - 0x80000000].ulId = UINT32_MAX;
2394 break;
2395
2396 default:
2397 return setError(E_INVALIDARG, tr("CpuId override leaf %#x is out of range"), aId);
2398 }
2399 return S_OK;
2400}
2401
2402HRESULT Machine::removeAllCPUIDLeaves()
2403{
2404 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2405
2406 HRESULT rc = i_checkStateDependency(MutableStateDep);
2407 if (FAILED(rc)) return rc;
2408
2409 i_setModified(IsModified_MachineData);
2410 mHWData.backup();
2411
2412 /* Invalidate all standard leafs. */
2413 for (unsigned i = 0; i < RT_ELEMENTS(mHWData->mCpuIdStdLeafs); ++i)
2414 mHWData->mCpuIdStdLeafs[i].ulId = UINT32_MAX;
2415
2416 /* Invalidate all extended leafs. */
2417 for (unsigned i = 0; i < RT_ELEMENTS(mHWData->mCpuIdExtLeafs); ++i)
2418 mHWData->mCpuIdExtLeafs[i].ulId = UINT32_MAX;
2419
2420 return S_OK;
2421}
2422HRESULT Machine::getHWVirtExProperty(HWVirtExPropertyType_T aProperty, BOOL *aValue)
2423{
2424 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2425
2426 switch(aProperty)
2427 {
2428 case HWVirtExPropertyType_Enabled:
2429 *aValue = mHWData->mHWVirtExEnabled;
2430 break;
2431
2432 case HWVirtExPropertyType_VPID:
2433 *aValue = mHWData->mHWVirtExVPIDEnabled;
2434 break;
2435
2436 case HWVirtExPropertyType_NestedPaging:
2437 *aValue = mHWData->mHWVirtExNestedPagingEnabled;
2438 break;
2439
2440 case HWVirtExPropertyType_UnrestrictedExecution:
2441 *aValue = mHWData->mHWVirtExUXEnabled;
2442 break;
2443
2444 case HWVirtExPropertyType_LargePages:
2445 *aValue = mHWData->mHWVirtExLargePagesEnabled;
2446#if defined(DEBUG_bird) && defined(RT_OS_LINUX) /* This feature is deadly here */
2447 *aValue = FALSE;
2448#endif
2449 break;
2450
2451 case HWVirtExPropertyType_Force:
2452 *aValue = mHWData->mHWVirtExForceEnabled;
2453 break;
2454
2455 default:
2456 return E_INVALIDARG;
2457 }
2458 return S_OK;
2459}
2460
2461HRESULT Machine::setHWVirtExProperty(HWVirtExPropertyType_T aProperty, BOOL aValue)
2462{
2463 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2464
2465 HRESULT rc = i_checkStateDependency(MutableStateDep);
2466 if (FAILED(rc)) return rc;
2467
2468 switch(aProperty)
2469 {
2470 case HWVirtExPropertyType_Enabled:
2471 i_setModified(IsModified_MachineData);
2472 mHWData.backup();
2473 mHWData->mHWVirtExEnabled = !!aValue;
2474 break;
2475
2476 case HWVirtExPropertyType_VPID:
2477 i_setModified(IsModified_MachineData);
2478 mHWData.backup();
2479 mHWData->mHWVirtExVPIDEnabled = !!aValue;
2480 break;
2481
2482 case HWVirtExPropertyType_NestedPaging:
2483 i_setModified(IsModified_MachineData);
2484 mHWData.backup();
2485 mHWData->mHWVirtExNestedPagingEnabled = !!aValue;
2486 break;
2487
2488 case HWVirtExPropertyType_UnrestrictedExecution:
2489 i_setModified(IsModified_MachineData);
2490 mHWData.backup();
2491 mHWData->mHWVirtExUXEnabled = !!aValue;
2492 break;
2493
2494 case HWVirtExPropertyType_LargePages:
2495 i_setModified(IsModified_MachineData);
2496 mHWData.backup();
2497 mHWData->mHWVirtExLargePagesEnabled = !!aValue;
2498 break;
2499
2500 case HWVirtExPropertyType_Force:
2501 i_setModified(IsModified_MachineData);
2502 mHWData.backup();
2503 mHWData->mHWVirtExForceEnabled = !!aValue;
2504 break;
2505
2506 default:
2507 return E_INVALIDARG;
2508 }
2509
2510 return S_OK;
2511}
2512
2513HRESULT Machine::getSnapshotFolder(com::Utf8Str &aSnapshotFolder)
2514{
2515 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2516
2517 i_calculateFullPath(mUserData->s.strSnapshotFolder, aSnapshotFolder);
2518
2519 return S_OK;
2520}
2521
2522HRESULT Machine::setSnapshotFolder(const com::Utf8Str &aSnapshotFolder)
2523{
2524 /* @todo (r=dmik):
2525 * 1. Allow to change the name of the snapshot folder containing snapshots
2526 * 2. Rename the folder on disk instead of just changing the property
2527 * value (to be smart and not to leave garbage). Note that it cannot be
2528 * done here because the change may be rolled back. Thus, the right
2529 * place is #saveSettings().
2530 */
2531
2532 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2533
2534 HRESULT rc = i_checkStateDependency(MutableStateDep);
2535 if (FAILED(rc)) return rc;
2536
2537 if (!mData->mCurrentSnapshot.isNull())
2538 return setError(E_FAIL,
2539 tr("The snapshot folder of a machine with snapshots cannot be changed (please delete all snapshots first)"));
2540
2541 Utf8Str strSnapshotFolder(aSnapshotFolder); // keep original
2542
2543 if (strSnapshotFolder.isEmpty())
2544 strSnapshotFolder = "Snapshots";
2545 int vrc = i_calculateFullPath(strSnapshotFolder,
2546 strSnapshotFolder);
2547 if (RT_FAILURE(vrc))
2548 return setError(E_FAIL,
2549 tr("Invalid snapshot folder '%s' (%Rrc)"),
2550 strSnapshotFolder.c_str(), vrc);
2551
2552 i_setModified(IsModified_MachineData);
2553 mUserData.backup();
2554
2555 i_copyPathRelativeToMachine(strSnapshotFolder, mUserData->s.strSnapshotFolder);
2556
2557 return S_OK;
2558}
2559
2560HRESULT Machine::getMediumAttachments(std::vector<ComPtr<IMediumAttachment> > &aMediumAttachments)
2561{
2562 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2563
2564 aMediumAttachments.resize(mMediaData->mAttachments.size());
2565 size_t i = 0;
2566 for (MediaData::AttachmentList::iterator it = mMediaData->mAttachments.begin();
2567 it != mMediaData->mAttachments.end(); ++it, ++i)
2568 aMediumAttachments[i] = *it;
2569
2570 return S_OK;
2571}
2572
2573HRESULT Machine::getVRDEServer(ComPtr<IVRDEServer> &aVRDEServer)
2574{
2575 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2576
2577 Assert(!!mVRDEServer);
2578
2579 aVRDEServer = mVRDEServer;
2580
2581 return S_OK;
2582}
2583
2584HRESULT Machine::getAudioAdapter(ComPtr<IAudioAdapter> &aAudioAdapter)
2585{
2586 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2587
2588 aAudioAdapter = mAudioAdapter;
2589
2590 return S_OK;
2591}
2592
2593HRESULT Machine::getUSBControllers(std::vector<ComPtr<IUSBController> > &aUSBControllers)
2594{
2595#ifdef VBOX_WITH_VUSB
2596 clearError();
2597 MultiResult rc(S_OK);
2598
2599# ifdef VBOX_WITH_USB
2600 rc = mParent->i_host()->i_checkUSBProxyService();
2601 if (FAILED(rc)) return rc;
2602# endif
2603
2604 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2605
2606 USBControllerList data = *mUSBControllers.data();
2607 aUSBControllers.resize(data.size());
2608 size_t i = 0;
2609 for (USBControllerList::iterator it = data.begin(); it != data.end(); ++i, ++it)
2610 aUSBControllers[i] = *it;
2611
2612 return S_OK;
2613#else
2614 /* Note: The GUI depends on this method returning E_NOTIMPL with no
2615 * extended error info to indicate that USB is simply not available
2616 * (w/o treating it as a failure), for example, as in OSE */
2617 NOREF(aUSBControllers);
2618 ReturnComNotImplemented();
2619#endif /* VBOX_WITH_VUSB */
2620}
2621
2622HRESULT Machine::getUSBDeviceFilters(ComPtr<IUSBDeviceFilters> &aUSBDeviceFilters)
2623{
2624#ifdef VBOX_WITH_VUSB
2625 clearError();
2626 MultiResult rc(S_OK);
2627
2628# ifdef VBOX_WITH_USB
2629 rc = mParent->i_host()->i_checkUSBProxyService();
2630 if (FAILED(rc)) return rc;
2631# endif
2632
2633 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2634
2635 aUSBDeviceFilters = mUSBDeviceFilters;
2636 return rc;
2637#else
2638 /* Note: The GUI depends on this method returning E_NOTIMPL with no
2639 * extended error info to indicate that USB is simply not available
2640 * (w/o treating it as a failure), for example, as in OSE */
2641 NOREF(aUSBDeviceFilters);
2642 ReturnComNotImplemented();
2643#endif /* VBOX_WITH_VUSB */
2644}
2645
2646HRESULT Machine::getSettingsFilePath(com::Utf8Str &aSettingsFilePath)
2647{
2648 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2649
2650 aSettingsFilePath = mData->m_strConfigFileFull;
2651
2652 return S_OK;
2653}
2654
2655HRESULT Machine::getSettingsModified(BOOL *aSettingsModified)
2656{
2657 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2658
2659 HRESULT rc = i_checkStateDependency(MutableOrSavedOrRunningStateDep);
2660 if (FAILED(rc)) return rc;
2661
2662 if (!mData->pMachineConfigFile->fileExists())
2663 // this is a new machine, and no config file exists yet:
2664 *aSettingsModified = TRUE;
2665 else
2666 *aSettingsModified = (mData->flModifications != 0);
2667
2668 return S_OK;
2669}
2670
2671HRESULT Machine::getSessionState(SessionState_T *aSessionState)
2672{
2673 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2674
2675 *aSessionState = mData->mSession.mState;
2676
2677 return S_OK;
2678}
2679
2680HRESULT Machine::getSessionName(com::Utf8Str &aSessionName)
2681{
2682 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2683
2684 aSessionName = mData->mSession.mName;
2685
2686 return S_OK;
2687}
2688
2689HRESULT Machine::getSessionPID(ULONG *aSessionPID)
2690{
2691 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2692
2693 *aSessionPID = mData->mSession.mPID;
2694
2695 return S_OK;
2696}
2697
2698HRESULT Machine::getState(MachineState_T *aState)
2699{
2700 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2701
2702 *aState = mData->mMachineState;
2703 Assert(mData->mMachineState != MachineState_Null);
2704
2705 return S_OK;
2706}
2707
2708HRESULT Machine::getLastStateChange(LONG64 *aLastStateChange)
2709{
2710 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2711
2712 *aLastStateChange = RTTimeSpecGetMilli(&mData->mLastStateChange);
2713
2714 return S_OK;
2715}
2716
2717HRESULT Machine::getStateFilePath(com::Utf8Str &aStateFilePath)
2718{
2719 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2720
2721 aStateFilePath = mSSData->strStateFilePath;
2722
2723 return S_OK;
2724}
2725
2726HRESULT Machine::getLogFolder(com::Utf8Str &aLogFolder)
2727{
2728 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2729
2730 i_getLogFolder(aLogFolder);
2731
2732 return S_OK;
2733}
2734
2735HRESULT Machine::getCurrentSnapshot(ComPtr<ISnapshot> &aCurrentSnapshot)
2736{
2737 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2738
2739 aCurrentSnapshot = mData->mCurrentSnapshot;
2740
2741 return S_OK;
2742}
2743
2744HRESULT Machine::getSnapshotCount(ULONG *aSnapshotCount)
2745{
2746 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2747
2748 *aSnapshotCount = mData->mFirstSnapshot.isNull()
2749 ? 0
2750 : mData->mFirstSnapshot->i_getAllChildrenCount() + 1;
2751
2752 return S_OK;
2753}
2754
2755HRESULT Machine::getCurrentStateModified(BOOL *aCurrentStateModified)
2756{
2757 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2758
2759 /* Note: for machines with no snapshots, we always return FALSE
2760 * (mData->mCurrentStateModified will be TRUE in this case, for historical
2761 * reasons :) */
2762
2763 *aCurrentStateModified = mData->mFirstSnapshot.isNull()
2764 ? FALSE
2765 : mData->mCurrentStateModified;
2766
2767 return S_OK;
2768}
2769
2770HRESULT Machine::getSharedFolders(std::vector<ComPtr<ISharedFolder> > &aSharedFolders)
2771{
2772 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2773
2774 aSharedFolders.resize(mHWData->mSharedFolders.size());
2775 size_t i = 0;
2776 for (std::list<ComObjPtr<SharedFolder> >::iterator it = mHWData->mSharedFolders.begin();
2777 it != mHWData->mSharedFolders.end(); ++i, ++it)
2778 aSharedFolders[i] = *it;
2779
2780 return S_OK;
2781}
2782
2783HRESULT Machine::getClipboardMode(ClipboardMode_T *aClipboardMode)
2784{
2785 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2786
2787 *aClipboardMode = mHWData->mClipboardMode;
2788
2789 return S_OK;
2790}
2791
2792HRESULT Machine::setClipboardMode(ClipboardMode_T aClipboardMode)
2793{
2794 HRESULT rc = S_OK;
2795
2796 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2797
2798 alock.release();
2799 rc = i_onClipboardModeChange(aClipboardMode);
2800 alock.acquire();
2801 if (FAILED(rc)) return rc;
2802
2803 i_setModified(IsModified_MachineData);
2804 mHWData.backup();
2805 mHWData->mClipboardMode = aClipboardMode;
2806
2807 /** Save settings if online - @todo why is this required? -- @bugref{6818} */
2808 if (Global::IsOnline(mData->mMachineState))
2809 i_saveSettings(NULL);
2810
2811 return S_OK;
2812}
2813
2814HRESULT Machine::getDnDMode(DnDMode_T *aDnDMode)
2815{
2816 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2817
2818 *aDnDMode = mHWData->mDnDMode;
2819
2820 return S_OK;
2821}
2822
2823HRESULT Machine::setDnDMode(DnDMode_T aDnDMode)
2824{
2825 HRESULT rc = S_OK;
2826
2827 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2828
2829 alock.release();
2830 rc = i_onDnDModeChange(aDnDMode);
2831
2832 alock.acquire();
2833 if (FAILED(rc)) return rc;
2834
2835 i_setModified(IsModified_MachineData);
2836 mHWData.backup();
2837 mHWData->mDnDMode = aDnDMode;
2838
2839 /** Save settings if online - @todo why is this required? -- @bugref{6818} */
2840 if (Global::IsOnline(mData->mMachineState))
2841 i_saveSettings(NULL);
2842
2843 return S_OK;
2844}
2845
2846HRESULT Machine::getStorageControllers(std::vector<ComPtr<IStorageController> > &aStorageControllers)
2847{
2848 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2849 StorageControllerList data = *mStorageControllers.data();
2850 size_t i = 0;
2851 aStorageControllers.resize(data.size());
2852 for (StorageControllerList::iterator it = data.begin(); it != data.end(); ++it, ++i)
2853 aStorageControllers[i] = *it;
2854 return S_OK;
2855}
2856
2857HRESULT Machine::getTeleporterEnabled(BOOL *aEnabled)
2858{
2859 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2860
2861 *aEnabled = mUserData->s.fTeleporterEnabled;
2862
2863 return S_OK;
2864}
2865
2866HRESULT Machine::setTeleporterEnabled(BOOL aTeleporterEnabled)
2867{
2868 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2869
2870 /* Only allow it to be set to true when PoweredOff or Aborted.
2871 (Clearing it is always permitted.) */
2872 if ( aTeleporterEnabled
2873 && mData->mRegistered
2874 && ( !i_isSessionMachine()
2875 || ( mData->mMachineState != MachineState_PoweredOff
2876 && mData->mMachineState != MachineState_Teleported
2877 && mData->mMachineState != MachineState_Aborted
2878 )
2879 )
2880 )
2881 return setError(VBOX_E_INVALID_VM_STATE,
2882 tr("The machine is not powered off (state is %s)"),
2883 Global::stringifyMachineState(mData->mMachineState));
2884
2885 i_setModified(IsModified_MachineData);
2886 mUserData.backup();
2887 mUserData->s.fTeleporterEnabled = !! aTeleporterEnabled;
2888
2889 return S_OK;
2890}
2891
2892HRESULT Machine::getTeleporterPort(ULONG *aTeleporterPort)
2893{
2894 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2895
2896 *aTeleporterPort = (ULONG)mUserData->s.uTeleporterPort;
2897
2898 return S_OK;
2899}
2900
2901HRESULT Machine::setTeleporterPort(ULONG aTeleporterPort)
2902{
2903 if (aTeleporterPort >= _64K)
2904 return setError(E_INVALIDARG, tr("Invalid port number %d"), aTeleporterPort);
2905
2906 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2907
2908 HRESULT rc = i_checkStateDependency(MutableOrSavedStateDep);
2909 if (FAILED(rc)) return rc;
2910
2911 i_setModified(IsModified_MachineData);
2912 mUserData.backup();
2913 mUserData->s.uTeleporterPort = (uint32_t)aTeleporterPort;
2914
2915 return S_OK;
2916}
2917
2918HRESULT Machine::getTeleporterAddress(com::Utf8Str &aTeleporterAddress)
2919{
2920 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2921
2922 aTeleporterAddress = mUserData->s.strTeleporterAddress;
2923
2924 return S_OK;
2925}
2926
2927HRESULT Machine::setTeleporterAddress(const com::Utf8Str &aTeleporterAddress)
2928{
2929 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2930
2931 HRESULT rc = i_checkStateDependency(MutableOrSavedStateDep);
2932 if (FAILED(rc)) return rc;
2933
2934 i_setModified(IsModified_MachineData);
2935 mUserData.backup();
2936 mUserData->s.strTeleporterAddress = aTeleporterAddress;
2937
2938 return S_OK;
2939}
2940
2941HRESULT Machine::getTeleporterPassword(com::Utf8Str &aTeleporterPassword)
2942{
2943 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2944 aTeleporterPassword = mUserData->s.strTeleporterPassword;
2945
2946 return S_OK;
2947}
2948
2949HRESULT Machine::setTeleporterPassword(const com::Utf8Str &aTeleporterPassword)
2950{
2951 /*
2952 * Hash the password first.
2953 */
2954 com::Utf8Str aT = aTeleporterPassword;
2955
2956 if (!aT.isEmpty())
2957 {
2958 if (VBoxIsPasswordHashed(&aT))
2959 return setError(E_INVALIDARG, tr("Cannot set an already hashed password, only plain text password please"));
2960 VBoxHashPassword(&aT);
2961 }
2962
2963 /*
2964 * Do the update.
2965 */
2966 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2967 HRESULT hrc = i_checkStateDependency(MutableOrSavedStateDep);
2968 if (SUCCEEDED(hrc))
2969 {
2970 i_setModified(IsModified_MachineData);
2971 mUserData.backup();
2972 mUserData->s.strTeleporterPassword = aT;
2973 }
2974
2975 return hrc;
2976}
2977
2978HRESULT Machine::getFaultToleranceState(FaultToleranceState_T *aFaultToleranceState)
2979{
2980 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2981
2982 *aFaultToleranceState = mUserData->s.enmFaultToleranceState;
2983 return S_OK;
2984}
2985
2986HRESULT Machine::setFaultToleranceState(FaultToleranceState_T aFaultToleranceState)
2987{
2988 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2989
2990 /* @todo deal with running state change. */
2991 HRESULT rc = i_checkStateDependency(MutableOrSavedOrRunningStateDep);
2992 if (FAILED(rc)) return rc;
2993
2994 i_setModified(IsModified_MachineData);
2995 mUserData.backup();
2996 mUserData->s.enmFaultToleranceState = aFaultToleranceState;
2997 return S_OK;
2998}
2999
3000HRESULT Machine::getFaultToleranceAddress(com::Utf8Str &aFaultToleranceAddress)
3001{
3002 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
3003
3004 aFaultToleranceAddress = mUserData->s.strFaultToleranceAddress;
3005 return S_OK;
3006}
3007
3008HRESULT Machine::setFaultToleranceAddress(const com::Utf8Str &aFaultToleranceAddress)
3009{
3010 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
3011
3012 /* @todo deal with running state change. */
3013 HRESULT rc = i_checkStateDependency(MutableOrSavedStateDep);
3014 if (FAILED(rc)) return rc;
3015
3016 i_setModified(IsModified_MachineData);
3017 mUserData.backup();
3018 mUserData->s.strFaultToleranceAddress = aFaultToleranceAddress;
3019 return S_OK;
3020}
3021
3022HRESULT Machine::getFaultTolerancePort(ULONG *aFaultTolerancePort)
3023{
3024 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
3025
3026 *aFaultTolerancePort = mUserData->s.uFaultTolerancePort;
3027 return S_OK;
3028}
3029
3030HRESULT Machine::setFaultTolerancePort(ULONG aFaultTolerancePort)
3031{
3032 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
3033
3034 /* @todo deal with running state change. */
3035 HRESULT rc = i_checkStateDependency(MutableOrSavedStateDep);
3036 if (FAILED(rc)) return rc;
3037
3038 i_setModified(IsModified_MachineData);
3039 mUserData.backup();
3040 mUserData->s.uFaultTolerancePort = aFaultTolerancePort;
3041 return S_OK;
3042}
3043
3044HRESULT Machine::getFaultTolerancePassword(com::Utf8Str &aFaultTolerancePassword)
3045{
3046 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
3047
3048 aFaultTolerancePassword = mUserData->s.strFaultTolerancePassword;
3049
3050 return S_OK;
3051}
3052
3053HRESULT Machine::setFaultTolerancePassword(const com::Utf8Str &aFaultTolerancePassword)
3054{
3055 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
3056
3057 /* @todo deal with running state change. */
3058 HRESULT rc = i_checkStateDependency(MutableOrSavedStateDep);
3059 if (FAILED(rc)) return rc;
3060
3061 i_setModified(IsModified_MachineData);
3062 mUserData.backup();
3063 mUserData->s.strFaultTolerancePassword = aFaultTolerancePassword;
3064
3065 return S_OK;
3066}
3067
3068HRESULT Machine::getFaultToleranceSyncInterval(ULONG *aFaultToleranceSyncInterval)
3069{
3070 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
3071
3072 *aFaultToleranceSyncInterval = mUserData->s.uFaultToleranceInterval;
3073 return S_OK;
3074}
3075
3076HRESULT Machine::setFaultToleranceSyncInterval(ULONG aFaultToleranceSyncInterval)
3077{
3078 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
3079
3080 /* @todo deal with running state change. */
3081 HRESULT rc = i_checkStateDependency(MutableOrSavedStateDep);
3082 if (FAILED(rc)) return rc;
3083
3084 i_setModified(IsModified_MachineData);
3085 mUserData.backup();
3086 mUserData->s.uFaultToleranceInterval = aFaultToleranceSyncInterval;
3087 return S_OK;
3088}
3089
3090HRESULT Machine::getRTCUseUTC(BOOL *aRTCUseUTC)
3091{
3092 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
3093
3094 *aRTCUseUTC = mUserData->s.fRTCUseUTC;
3095
3096 return S_OK;
3097}
3098
3099HRESULT Machine::setRTCUseUTC(BOOL aRTCUseUTC)
3100{
3101 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
3102
3103 /* Only allow it to be set to true when PoweredOff or Aborted.
3104 (Clearing it is always permitted.) */
3105 if ( aRTCUseUTC
3106 && mData->mRegistered
3107 && ( !i_isSessionMachine()
3108 || ( mData->mMachineState != MachineState_PoweredOff
3109 && mData->mMachineState != MachineState_Teleported
3110 && mData->mMachineState != MachineState_Aborted
3111 )
3112 )
3113 )
3114 return setError(VBOX_E_INVALID_VM_STATE,
3115 tr("The machine is not powered off (state is %s)"),
3116 Global::stringifyMachineState(mData->mMachineState));
3117
3118 i_setModified(IsModified_MachineData);
3119 mUserData.backup();
3120 mUserData->s.fRTCUseUTC = !!aRTCUseUTC;
3121
3122 return S_OK;
3123}
3124
3125HRESULT Machine::getIOCacheEnabled(BOOL *aIOCacheEnabled)
3126{
3127 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
3128
3129 *aIOCacheEnabled = mHWData->mIOCacheEnabled;
3130
3131 return S_OK;
3132}
3133
3134HRESULT Machine::setIOCacheEnabled(BOOL aIOCacheEnabled)
3135{
3136 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
3137
3138 HRESULT rc = i_checkStateDependency(MutableStateDep);
3139 if (FAILED(rc)) return rc;
3140
3141 i_setModified(IsModified_MachineData);
3142 mHWData.backup();
3143 mHWData->mIOCacheEnabled = aIOCacheEnabled;
3144
3145 return S_OK;
3146}
3147
3148HRESULT Machine::getIOCacheSize(ULONG *aIOCacheSize)
3149{
3150 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
3151
3152 *aIOCacheSize = mHWData->mIOCacheSize;
3153
3154 return S_OK;
3155}
3156
3157HRESULT Machine::setIOCacheSize(ULONG aIOCacheSize)
3158{
3159 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
3160
3161 HRESULT rc = i_checkStateDependency(MutableStateDep);
3162 if (FAILED(rc)) return rc;
3163
3164 i_setModified(IsModified_MachineData);
3165 mHWData.backup();
3166 mHWData->mIOCacheSize = aIOCacheSize;
3167
3168 return S_OK;
3169}
3170
3171
3172/**
3173 * @note Locks objects!
3174 */
3175HRESULT Machine::lockMachine(const ComPtr<ISession> &aSession,
3176 LockType_T aLockType)
3177{
3178 /* check the session state */
3179 SessionState_T state;
3180 HRESULT rc = aSession->COMGETTER(State)(&state);
3181 if (FAILED(rc)) return rc;
3182
3183 if (state != SessionState_Unlocked)
3184 return setError(VBOX_E_INVALID_OBJECT_STATE,
3185 tr("The given session is busy"));
3186
3187 // get the client's IInternalSessionControl interface
3188 ComPtr<IInternalSessionControl> pSessionControl = aSession;
3189 ComAssertMsgRet(!!pSessionControl, ("No IInternalSessionControl interface"),
3190 E_INVALIDARG);
3191
3192 // session name (only used in some code paths)
3193 Utf8Str strSessionName;
3194
3195 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
3196
3197 if (!mData->mRegistered)
3198 return setError(E_UNEXPECTED,
3199 tr("The machine '%s' is not registered"),
3200 mUserData->s.strName.c_str());
3201
3202 LogFlowThisFunc(("mSession.mState=%s\n", Global::stringifySessionState(mData->mSession.mState)));
3203
3204 SessionState_T oldState = mData->mSession.mState;
3205 /* Hack: in case the session is closing and there is a progress object
3206 * which allows waiting for the session to be closed, take the opportunity
3207 * and do a limited wait (max. 1 second). This helps a lot when the system
3208 * is busy and thus session closing can take a little while. */
3209 if ( mData->mSession.mState == SessionState_Unlocking
3210 && mData->mSession.mProgress)
3211 {
3212 alock.release();
3213 mData->mSession.mProgress->WaitForCompletion(1000);
3214 alock.acquire();
3215 LogFlowThisFunc(("after waiting: mSession.mState=%s\n", Global::stringifySessionState(mData->mSession.mState)));
3216 }
3217
3218 // try again now
3219 if ( (mData->mSession.mState == SessionState_Locked) // machine is write-locked already
3220 // (i.e. session machine exists)
3221 && (aLockType == LockType_Shared) // caller wants a shared link to the
3222 // existing session that holds the write lock:
3223 )
3224 {
3225 // OK, share the session... we are now dealing with three processes:
3226 // 1) VBoxSVC (where this code runs);
3227 // 2) process C: the caller's client process (who wants a shared session);
3228 // 3) process W: the process which already holds the write lock on the machine (write-locking session)
3229
3230 // copy pointers to W (the write-locking session) before leaving lock (these must not be NULL)
3231 ComPtr<IInternalSessionControl> pSessionW = mData->mSession.mDirectControl;
3232 ComAssertRet(!pSessionW.isNull(), E_FAIL);
3233 ComObjPtr<SessionMachine> pSessionMachine = mData->mSession.mMachine;
3234 AssertReturn(!pSessionMachine.isNull(), E_FAIL);
3235
3236 /*
3237 * Release the lock before calling the client process. It's safe here
3238 * since the only thing to do after we get the lock again is to add
3239 * the remote control to the list (which doesn't directly influence
3240 * anything).
3241 */
3242 alock.release();
3243
3244 // get the console of the session holding the write lock (this is a remote call)
3245 ComPtr<IConsole> pConsoleW;
3246 if (mData->mSession.mLockType == LockType_VM)
3247 {
3248 LogFlowThisFunc(("Calling GetRemoteConsole()...\n"));
3249 rc = pSessionW->COMGETTER(RemoteConsole)(pConsoleW.asOutParam());
3250 LogFlowThisFunc(("GetRemoteConsole() returned %08X\n", rc));
3251 if (FAILED(rc))
3252 // the failure may occur w/o any error info (from RPC), so provide one
3253 return setError(VBOX_E_VM_ERROR,
3254 tr("Failed to get a console object from the direct session (%Rhrc)"), rc);
3255 ComAssertRet(!pConsoleW.isNull(), E_FAIL);
3256 }
3257
3258 // share the session machine and W's console with the caller's session
3259 LogFlowThisFunc(("Calling AssignRemoteMachine()...\n"));
3260 rc = pSessionControl->AssignRemoteMachine(pSessionMachine, pConsoleW);
3261 LogFlowThisFunc(("AssignRemoteMachine() returned %08X\n", rc));
3262
3263 if (FAILED(rc))
3264 // the failure may occur w/o any error info (from RPC), so provide one
3265 return setError(VBOX_E_VM_ERROR,
3266 tr("Failed to assign the machine to the session (%Rhrc)"), rc);
3267 alock.acquire();
3268
3269 // need to revalidate the state after acquiring the lock again
3270 if (mData->mSession.mState != SessionState_Locked)
3271 {
3272 pSessionControl->Uninitialize();
3273 return setError(VBOX_E_INVALID_SESSION_STATE,
3274 tr("The machine '%s' was unlocked unexpectedly while attempting to share its session"),
3275 mUserData->s.strName.c_str());
3276 }
3277
3278 // add the caller's session to the list
3279 mData->mSession.mRemoteControls.push_back(pSessionControl);
3280 }
3281 else if ( mData->mSession.mState == SessionState_Locked
3282 || mData->mSession.mState == SessionState_Unlocking
3283 )
3284 {
3285 // sharing not permitted, or machine still unlocking:
3286 return setError(VBOX_E_INVALID_OBJECT_STATE,
3287 tr("The machine '%s' is already locked for a session (or being unlocked)"),
3288 mUserData->s.strName.c_str());
3289 }
3290 else
3291 {
3292 // machine is not locked: then write-lock the machine (create the session machine)
3293
3294 // must not be busy
3295 AssertReturn(!Global::IsOnlineOrTransient(mData->mMachineState), E_FAIL);
3296
3297 // get the caller's session PID
3298 RTPROCESS pid = NIL_RTPROCESS;
3299 AssertCompile(sizeof(ULONG) == sizeof(RTPROCESS));
3300 pSessionControl->COMGETTER(PID)((ULONG*)&pid);
3301 Assert(pid != NIL_RTPROCESS);
3302
3303 bool fLaunchingVMProcess = (mData->mSession.mState == SessionState_Spawning);
3304
3305 if (fLaunchingVMProcess)
3306 {
3307 if (mData->mSession.mPID == NIL_RTPROCESS)
3308 {
3309 // two or more clients racing for a lock, the one which set the
3310 // session state to Spawning will win, the others will get an
3311 // error as we can't decide here if waiting a little would help
3312 // (only for shared locks this would avoid an error)
3313 return setError(VBOX_E_INVALID_OBJECT_STATE,
3314 tr("The machine '%s' already has a lock request pending"),
3315 mUserData->s.strName.c_str());
3316 }
3317
3318 // this machine is awaiting for a spawning session to be opened:
3319 // then the calling process must be the one that got started by
3320 // LaunchVMProcess()
3321
3322 LogFlowThisFunc(("mSession.mPID=%d(0x%x)\n", mData->mSession.mPID, mData->mSession.mPID));
3323 LogFlowThisFunc(("session.pid=%d(0x%x)\n", pid, pid));
3324
3325#if defined(VBOX_WITH_HARDENING) && defined(RT_OS_WINDOWS)
3326 /* Hardened windows builds spawns three processes when a VM is
3327 launched, the 3rd one is the one that will end up here. */
3328 RTPROCESS ppid;
3329 int rc = RTProcQueryParent(pid, &ppid);
3330 if (RT_SUCCESS(rc))
3331 rc = RTProcQueryParent(ppid, &ppid);
3332 if ( (RT_SUCCESS(rc) && mData->mSession.mPID == ppid)
3333 || rc == VERR_ACCESS_DENIED)
3334 {
3335 LogFlowThisFunc(("mSession.mPID => %d(%#x) - windows hardening stub\n", mData->mSession.mPID, pid));
3336 mData->mSession.mPID = pid;
3337 }
3338#endif
3339
3340 if (mData->mSession.mPID != pid)
3341 return setError(E_ACCESSDENIED,
3342 tr("An unexpected process (PID=0x%08X) has tried to lock the "
3343 "machine '%s', while only the process started by LaunchVMProcess (PID=0x%08X) is allowed"),
3344 pid, mUserData->s.strName.c_str(), mData->mSession.mPID);
3345 }
3346
3347 // create the mutable SessionMachine from the current machine
3348 ComObjPtr<SessionMachine> sessionMachine;
3349 sessionMachine.createObject();
3350 rc = sessionMachine->init(this);
3351 AssertComRC(rc);
3352
3353 /* NOTE: doing return from this function after this point but
3354 * before the end is forbidden since it may call SessionMachine::uninit()
3355 * (through the ComObjPtr's destructor) which requests the VirtualBox write
3356 * lock while still holding the Machine lock in alock so that a deadlock
3357 * is possible due to the wrong lock order. */
3358
3359 if (SUCCEEDED(rc))
3360 {
3361 /*
3362 * Set the session state to Spawning to protect against subsequent
3363 * attempts to open a session and to unregister the machine after
3364 * we release the lock.
3365 */
3366 SessionState_T origState = mData->mSession.mState;
3367 mData->mSession.mState = SessionState_Spawning;
3368
3369#ifndef VBOX_WITH_GENERIC_SESSION_WATCHER
3370 /* Get the client token ID to be passed to the client process */
3371 Utf8Str strTokenId;
3372 sessionMachine->i_getTokenId(strTokenId);
3373 Assert(!strTokenId.isEmpty());
3374#else /* VBOX_WITH_GENERIC_SESSION_WATCHER */
3375 /* Get the client token to be passed to the client process */
3376 ComPtr<IToken> pToken(sessionMachine->i_getToken());
3377 /* The token is now "owned" by pToken, fix refcount */
3378 if (!pToken.isNull())
3379 pToken->Release();
3380#endif /* VBOX_WITH_GENERIC_SESSION_WATCHER */
3381
3382 /*
3383 * Release the lock before calling the client process -- it will call
3384 * Machine/SessionMachine methods. Releasing the lock here is quite safe
3385 * because the state is Spawning, so that LaunchVMProcess() and
3386 * LockMachine() calls will fail. This method, called before we
3387 * acquire the lock again, will fail because of the wrong PID.
3388 *
3389 * Note that mData->mSession.mRemoteControls accessed outside
3390 * the lock may not be modified when state is Spawning, so it's safe.
3391 */
3392 alock.release();
3393
3394 LogFlowThisFunc(("Calling AssignMachine()...\n"));
3395#ifndef VBOX_WITH_GENERIC_SESSION_WATCHER
3396 rc = pSessionControl->AssignMachine(sessionMachine, aLockType, Bstr(strTokenId).raw());
3397#else /* VBOX_WITH_GENERIC_SESSION_WATCHER */
3398 rc = pSessionControl->AssignMachine(sessionMachine, aLockType, pToken);
3399 /* Now the token is owned by the client process. */
3400 pToken.setNull();
3401#endif /* VBOX_WITH_GENERIC_SESSION_WATCHER */
3402 LogFlowThisFunc(("AssignMachine() returned %08X\n", rc));
3403
3404 /* The failure may occur w/o any error info (from RPC), so provide one */
3405 if (FAILED(rc))
3406 setError(VBOX_E_VM_ERROR,
3407 tr("Failed to assign the machine to the session (%Rhrc)"), rc);
3408
3409 // get session name, either to remember or to compare against
3410 // the already known session name.
3411 {
3412 Bstr bstrSessionName;
3413 HRESULT rc2 = aSession->COMGETTER(Name)(bstrSessionName.asOutParam());
3414 if (SUCCEEDED(rc2))
3415 strSessionName = bstrSessionName;
3416 }
3417
3418 if ( SUCCEEDED(rc)
3419 && fLaunchingVMProcess
3420 )
3421 {
3422 /* complete the remote session initialization */
3423
3424 /* get the console from the direct session */
3425 ComPtr<IConsole> console;
3426 rc = pSessionControl->COMGETTER(RemoteConsole)(console.asOutParam());
3427 ComAssertComRC(rc);
3428
3429 if (SUCCEEDED(rc) && !console)
3430 {
3431 ComAssert(!!console);
3432 rc = E_FAIL;
3433 }
3434
3435 /* assign machine & console to the remote session */
3436 if (SUCCEEDED(rc))
3437 {
3438 /*
3439 * after LaunchVMProcess(), the first and the only
3440 * entry in remoteControls is that remote session
3441 */
3442 LogFlowThisFunc(("Calling AssignRemoteMachine()...\n"));
3443 rc = mData->mSession.mRemoteControls.front()->AssignRemoteMachine(sessionMachine, console);
3444 LogFlowThisFunc(("AssignRemoteMachine() returned %08X\n", rc));
3445
3446 /* The failure may occur w/o any error info (from RPC), so provide one */
3447 if (FAILED(rc))
3448 setError(VBOX_E_VM_ERROR,
3449 tr("Failed to assign the machine to the remote session (%Rhrc)"), rc);
3450 }
3451
3452 if (FAILED(rc))
3453 pSessionControl->Uninitialize();
3454 }
3455
3456 /* acquire the lock again */
3457 alock.acquire();
3458
3459 /* Restore the session state */
3460 mData->mSession.mState = origState;
3461 }
3462
3463 // finalize spawning anyway (this is why we don't return on errors above)
3464 if (fLaunchingVMProcess)
3465 {
3466 Assert(mData->mSession.mName == strSessionName);
3467 /* Note that the progress object is finalized later */
3468 /** @todo Consider checking mData->mSession.mProgress for cancellation
3469 * around here. */
3470
3471 /* We don't reset mSession.mPID here because it is necessary for
3472 * SessionMachine::uninit() to reap the child process later. */
3473
3474 if (FAILED(rc))
3475 {
3476 /* Close the remote session, remove the remote control from the list
3477 * and reset session state to Closed (@note keep the code in sync
3478 * with the relevant part in checkForSpawnFailure()). */
3479
3480 Assert(mData->mSession.mRemoteControls.size() == 1);
3481 if (mData->mSession.mRemoteControls.size() == 1)
3482 {
3483 ErrorInfoKeeper eik;
3484 mData->mSession.mRemoteControls.front()->Uninitialize();
3485 }
3486
3487 mData->mSession.mRemoteControls.clear();
3488 mData->mSession.mState = SessionState_Unlocked;
3489 }
3490 }
3491 else
3492 {
3493 /* memorize PID of the directly opened session */
3494 if (SUCCEEDED(rc))
3495 mData->mSession.mPID = pid;
3496 }
3497
3498 if (SUCCEEDED(rc))
3499 {
3500 mData->mSession.mLockType = aLockType;
3501 /* memorize the direct session control and cache IUnknown for it */
3502 mData->mSession.mDirectControl = pSessionControl;
3503 mData->mSession.mState = SessionState_Locked;
3504 if (!fLaunchingVMProcess)
3505 mData->mSession.mName = strSessionName;
3506 /* associate the SessionMachine with this Machine */
3507 mData->mSession.mMachine = sessionMachine;
3508
3509 /* request an IUnknown pointer early from the remote party for later
3510 * identity checks (it will be internally cached within mDirectControl
3511 * at least on XPCOM) */
3512 ComPtr<IUnknown> unk = mData->mSession.mDirectControl;
3513 NOREF(unk);
3514 }
3515
3516 /* Release the lock since SessionMachine::uninit() locks VirtualBox which
3517 * would break the lock order */
3518 alock.release();
3519
3520 /* uninitialize the created session machine on failure */
3521 if (FAILED(rc))
3522 sessionMachine->uninit();
3523 }
3524
3525 if (SUCCEEDED(rc))
3526 {
3527 /*
3528 * tell the client watcher thread to update the set of
3529 * machines that have open sessions
3530 */
3531 mParent->i_updateClientWatcher();
3532
3533 if (oldState != SessionState_Locked)
3534 /* fire an event */
3535 mParent->i_onSessionStateChange(i_getId(), SessionState_Locked);
3536 }
3537
3538 return rc;
3539}
3540
3541/**
3542 * @note Locks objects!
3543 */
3544HRESULT Machine::launchVMProcess(const ComPtr<ISession> &aSession,
3545 const com::Utf8Str &aName,
3546 const com::Utf8Str &aEnvironment,
3547 ComPtr<IProgress> &aProgress)
3548{
3549 Utf8Str strFrontend(aName);
3550 /* "emergencystop" doesn't need the session, so skip the checks/interface
3551 * retrieval. This code doesn't quite fit in here, but introducing a
3552 * special API method would be even more effort, and would require explicit
3553 * support by every API client. It's better to hide the feature a bit. */
3554 if (strFrontend != "emergencystop")
3555 CheckComArgNotNull(aSession);
3556
3557 HRESULT rc = S_OK;
3558 if (strFrontend.isEmpty())
3559 {
3560 Bstr bstrFrontend;
3561 rc = COMGETTER(DefaultFrontend)(bstrFrontend.asOutParam());
3562 if (FAILED(rc))
3563 return rc;
3564 strFrontend = bstrFrontend;
3565 if (strFrontend.isEmpty())
3566 {
3567 ComPtr<ISystemProperties> systemProperties;
3568 rc = mParent->COMGETTER(SystemProperties)(systemProperties.asOutParam());
3569 if (FAILED(rc))
3570 return rc;
3571 rc = systemProperties->COMGETTER(DefaultFrontend)(bstrFrontend.asOutParam());
3572 if (FAILED(rc))
3573 return rc;
3574 strFrontend = bstrFrontend;
3575 }
3576 /* paranoia - emergencystop is not a valid default */
3577 if (strFrontend == "emergencystop")
3578 strFrontend = Utf8Str::Empty;
3579 }
3580 /* default frontend: Qt GUI */
3581 if (strFrontend.isEmpty())
3582 strFrontend = "GUI/Qt";
3583
3584 if (strFrontend != "emergencystop")
3585 {
3586 /* check the session state */
3587 SessionState_T state;
3588 rc = aSession->COMGETTER(State)(&state);
3589 if (FAILED(rc))
3590 return rc;
3591
3592 if (state != SessionState_Unlocked)
3593 return setError(VBOX_E_INVALID_OBJECT_STATE,
3594 tr("The given session is busy"));
3595
3596 /* get the IInternalSessionControl interface */
3597 ComPtr<IInternalSessionControl> control(aSession);
3598 ComAssertMsgRet(!control.isNull(),
3599 ("No IInternalSessionControl interface"),
3600 E_INVALIDARG);
3601
3602 /* get the teleporter enable state for the progress object init. */
3603 BOOL fTeleporterEnabled;
3604 rc = COMGETTER(TeleporterEnabled)(&fTeleporterEnabled);
3605 if (FAILED(rc))
3606 return rc;
3607
3608 /* create a progress object */
3609 ComObjPtr<ProgressProxy> progress;
3610 progress.createObject();
3611 rc = progress->init(mParent,
3612 static_cast<IMachine*>(this),
3613 Bstr(tr("Starting VM")).raw(),
3614 TRUE /* aCancelable */,
3615 fTeleporterEnabled ? 20 : 10 /* uTotalOperationsWeight */,
3616 BstrFmt(tr("Creating process for virtual machine \"%s\" (%s)"),
3617 mUserData->s.strName.c_str(), strFrontend.c_str()).raw(),
3618 2 /* uFirstOperationWeight */,
3619 fTeleporterEnabled ? 3 : 1 /* cOtherProgressObjectOperations */);
3620
3621 if (SUCCEEDED(rc))
3622 {
3623 rc = i_launchVMProcess(control, strFrontend, aEnvironment, progress);
3624 if (SUCCEEDED(rc))
3625 {
3626 aProgress = progress;
3627
3628 /* signal the client watcher thread */
3629 mParent->i_updateClientWatcher();
3630
3631 /* fire an event */
3632 mParent->i_onSessionStateChange(i_getId(), SessionState_Spawning);
3633 }
3634 }
3635 }
3636 else
3637 {
3638 /* no progress object - either instant success or failure */
3639 aProgress = NULL;
3640
3641 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
3642
3643 if (mData->mSession.mState != SessionState_Locked)
3644 return setError(VBOX_E_INVALID_OBJECT_STATE,
3645 tr("The machine '%s' is not locked by a session"),
3646 mUserData->s.strName.c_str());
3647
3648 /* must have a VM process associated - do not kill normal API clients
3649 * with an open session */
3650 if (!Global::IsOnline(mData->mMachineState))
3651 return setError(VBOX_E_INVALID_OBJECT_STATE,
3652 tr("The machine '%s' does not have a VM process"),
3653 mUserData->s.strName.c_str());
3654
3655 /* forcibly terminate the VM process */
3656 if (mData->mSession.mPID != NIL_RTPROCESS)
3657 RTProcTerminate(mData->mSession.mPID);
3658
3659 /* signal the client watcher thread, as most likely the client has
3660 * been terminated */
3661 mParent->i_updateClientWatcher();
3662 }
3663
3664 return rc;
3665}
3666
3667HRESULT Machine::setBootOrder(ULONG aPosition, DeviceType_T aDevice)
3668{
3669 if (aPosition < 1 || aPosition > SchemaDefs::MaxBootPosition)
3670 return setError(E_INVALIDARG,
3671 tr("Invalid boot position: %lu (must be in range [1, %lu])"),
3672 aPosition, SchemaDefs::MaxBootPosition);
3673
3674 if (aDevice == DeviceType_USB)
3675 return setError(E_NOTIMPL,
3676 tr("Booting from USB device is currently not supported"));
3677
3678 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
3679
3680 HRESULT rc = i_checkStateDependency(MutableStateDep);
3681 if (FAILED(rc)) return rc;
3682
3683 i_setModified(IsModified_MachineData);
3684 mHWData.backup();
3685 mHWData->mBootOrder[aPosition - 1] = aDevice;
3686
3687 return S_OK;
3688}
3689
3690HRESULT Machine::getBootOrder(ULONG aPosition, DeviceType_T *aDevice)
3691{
3692 if (aPosition < 1 || aPosition > SchemaDefs::MaxBootPosition)
3693 return setError(E_INVALIDARG,
3694 tr("Invalid boot position: %lu (must be in range [1, %lu])"),
3695 aPosition, SchemaDefs::MaxBootPosition);
3696
3697 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
3698
3699 *aDevice = mHWData->mBootOrder[aPosition - 1];
3700
3701 return S_OK;
3702}
3703
3704HRESULT Machine::attachDevice(const com::Utf8Str &aName,
3705 LONG aControllerPort,
3706 LONG aDevice,
3707 DeviceType_T aType,
3708 const ComPtr<IMedium> &aMedium)
3709{
3710 IMedium *aM = aMedium;
3711 LogFlowThisFunc(("aControllerName=\"%s\" aControllerPort=%d aDevice=%d aType=%d aMedium=%p\n",
3712 aName.c_str(), aControllerPort, aDevice, aType, aM));
3713
3714 // request the host lock first, since might be calling Host methods for getting host drives;
3715 // next, protect the media tree all the while we're in here, as well as our member variables
3716 AutoMultiWriteLock2 alock(mParent->i_host(), this COMMA_LOCKVAL_SRC_POS);
3717 AutoWriteLock treeLock(&mParent->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
3718
3719 HRESULT rc = i_checkStateDependency(MutableOrRunningStateDep);
3720 if (FAILED(rc)) return rc;
3721
3722 /// @todo NEWMEDIA implicit machine registration
3723 if (!mData->mRegistered)
3724 return setError(VBOX_E_INVALID_OBJECT_STATE,
3725 tr("Cannot attach storage devices to an unregistered machine"));
3726
3727 AssertReturn(mData->mMachineState != MachineState_Saved, E_FAIL);
3728
3729 /* Check for an existing controller. */
3730 ComObjPtr<StorageController> ctl;
3731 rc = i_getStorageControllerByName(aName, ctl, true /* aSetError */);
3732 if (FAILED(rc)) return rc;
3733
3734 StorageControllerType_T ctrlType;
3735 rc = ctl->COMGETTER(ControllerType)(&ctrlType);
3736 if (FAILED(rc))
3737 return setError(E_FAIL,
3738 tr("Could not get type of controller '%s'"),
3739 aName.c_str());
3740
3741 bool fSilent = false;
3742 Utf8Str strReconfig;
3743
3744 /* Check whether the flag to allow silent storage attachment reconfiguration is set. */
3745 strReconfig = i_getExtraData(Utf8Str("VBoxInternal2/SilentReconfigureWhilePaused"));
3746 if ( mData->mMachineState == MachineState_Paused
3747 && strReconfig == "1")
3748 fSilent = true;
3749
3750 /* Check that the controller can do hotplugging if we detach the device while the VM is running. */
3751 bool fHotplug = false;
3752 if (!fSilent && Global::IsOnlineOrTransient(mData->mMachineState))
3753 fHotplug = true;
3754
3755 if (fHotplug && !i_isControllerHotplugCapable(ctrlType))
3756 return setError(VBOX_E_INVALID_VM_STATE,
3757 tr("Controller '%s' does not support hotplugging"),
3758 aName.c_str());
3759
3760 // check that the port and device are not out of range
3761 rc = ctl->i_checkPortAndDeviceValid(aControllerPort, aDevice);
3762 if (FAILED(rc)) return rc;
3763
3764 /* check if the device slot is already busy */
3765 MediumAttachment *pAttachTemp;
3766 if ((pAttachTemp = i_findAttachment(mMediaData->mAttachments,
3767 Bstr(aName).raw(),
3768 aControllerPort,
3769 aDevice)))
3770 {
3771 Medium *pMedium = pAttachTemp->i_getMedium();
3772 if (pMedium)
3773 {
3774 AutoReadLock mediumLock(pMedium COMMA_LOCKVAL_SRC_POS);
3775 return setError(VBOX_E_OBJECT_IN_USE,
3776 tr("Medium '%s' is already attached to port %d, device %d of controller '%s' of this virtual machine"),
3777 pMedium->i_getLocationFull().c_str(),
3778 aControllerPort,
3779 aDevice,
3780 aName.c_str());
3781 }
3782 else
3783 return setError(VBOX_E_OBJECT_IN_USE,
3784 tr("Device is already attached to port %d, device %d of controller '%s' of this virtual machine"),
3785 aControllerPort, aDevice, aName.c_str());
3786 }
3787
3788 ComObjPtr<Medium> medium = static_cast<Medium*>(aM);
3789 if (aMedium && medium.isNull())
3790 return setError(E_INVALIDARG, "The given medium pointer is invalid");
3791
3792 AutoCaller mediumCaller(medium);
3793 if (FAILED(mediumCaller.rc())) return mediumCaller.rc();
3794
3795 AutoWriteLock mediumLock(medium COMMA_LOCKVAL_SRC_POS);
3796
3797 if ( (pAttachTemp = i_findAttachment(mMediaData->mAttachments, medium))
3798 && !medium.isNull()
3799 )
3800 return setError(VBOX_E_OBJECT_IN_USE,
3801 tr("Medium '%s' is already attached to this virtual machine"),
3802 medium->i_getLocationFull().c_str());
3803
3804 if (!medium.isNull())
3805 {
3806 MediumType_T mtype = medium->i_getType();
3807 // MediumType_Readonly is also new, but only applies to DVDs and floppies.
3808 // For DVDs it's not written to the config file, so needs no global config
3809 // version bump. For floppies it's a new attribute "type", which is ignored
3810 // by older VirtualBox version, so needs no global config version bump either.
3811 // For hard disks this type is not accepted.
3812 if (mtype == MediumType_MultiAttach)
3813 {
3814 // This type is new with VirtualBox 4.0 and therefore requires settings
3815 // version 1.11 in the settings backend. Unfortunately it is not enough to do
3816 // the usual routine in MachineConfigFile::bumpSettingsVersionIfNeeded() for
3817 // two reasons: The medium type is a property of the media registry tree, which
3818 // can reside in the global config file (for pre-4.0 media); we would therefore
3819 // possibly need to bump the global config version. We don't want to do that though
3820 // because that might make downgrading to pre-4.0 impossible.
3821 // As a result, we can only use these two new types if the medium is NOT in the
3822 // global registry:
3823 const Guid &uuidGlobalRegistry = mParent->i_getGlobalRegistryId();
3824 if ( medium->i_isInRegistry(uuidGlobalRegistry)
3825 || !mData->pMachineConfigFile->canHaveOwnMediaRegistry()
3826 )
3827 return setError(VBOX_E_INVALID_OBJECT_STATE,
3828 tr("Cannot attach medium '%s': the media type 'MultiAttach' can only be attached "
3829 "to machines that were created with VirtualBox 4.0 or later"),
3830 medium->i_getLocationFull().c_str());
3831 }
3832 }
3833
3834 bool fIndirect = false;
3835 if (!medium.isNull())
3836 fIndirect = medium->i_isReadOnly();
3837 bool associate = true;
3838
3839 do
3840 {
3841 if ( aType == DeviceType_HardDisk
3842 && mMediaData.isBackedUp())
3843 {
3844 const MediaData::AttachmentList &oldAtts = mMediaData.backedUpData()->mAttachments;
3845
3846 /* check if the medium was attached to the VM before we started
3847 * changing attachments in which case the attachment just needs to
3848 * be restored */
3849 if ((pAttachTemp = i_findAttachment(oldAtts, medium)))
3850 {
3851 AssertReturn(!fIndirect, E_FAIL);
3852
3853 /* see if it's the same bus/channel/device */
3854 if (pAttachTemp->i_matches(Bstr(aName).raw(), aControllerPort, aDevice))
3855 {
3856 /* the simplest case: restore the whole attachment
3857 * and return, nothing else to do */
3858 mMediaData->mAttachments.push_back(pAttachTemp);
3859
3860 /* Reattach the medium to the VM. */
3861 if (fHotplug || fSilent)
3862 {
3863 mediumLock.release();
3864 treeLock.release();
3865 alock.release();
3866
3867 MediumLockList *pMediumLockList(new MediumLockList());
3868
3869 rc = medium->i_createMediumLockList(true /* fFailIfInaccessible */,
3870 true /* fMediumLockWrite */,
3871 false /* fMediumLockWriteAll */,
3872 NULL,
3873 *pMediumLockList);
3874 alock.acquire();
3875 if (FAILED(rc))
3876 delete pMediumLockList;
3877 else
3878 {
3879 mData->mSession.mLockedMedia.Unlock();
3880 alock.release();
3881 rc = mData->mSession.mLockedMedia.Insert(pAttachTemp, pMediumLockList);
3882 mData->mSession.mLockedMedia.Lock();
3883 alock.acquire();
3884 }
3885 alock.release();
3886
3887 if (SUCCEEDED(rc))
3888 {
3889 rc = i_onStorageDeviceChange(pAttachTemp, FALSE /* aRemove */, fSilent);
3890 /* Remove lock list in case of error. */
3891 if (FAILED(rc))
3892 {
3893 mData->mSession.mLockedMedia.Unlock();
3894 mData->mSession.mLockedMedia.Remove(pAttachTemp);
3895 mData->mSession.mLockedMedia.Lock();
3896 }
3897 }
3898 }
3899
3900 return S_OK;
3901 }
3902
3903 /* bus/channel/device differ; we need a new attachment object,
3904 * but don't try to associate it again */
3905 associate = false;
3906 break;
3907 }
3908 }
3909
3910 /* go further only if the attachment is to be indirect */
3911 if (!fIndirect)
3912 break;
3913
3914 /* perform the so called smart attachment logic for indirect
3915 * attachments. Note that smart attachment is only applicable to base
3916 * hard disks. */
3917
3918 if (medium->i_getParent().isNull())
3919 {
3920 /* first, investigate the backup copy of the current hard disk
3921 * attachments to make it possible to re-attach existing diffs to
3922 * another device slot w/o losing their contents */
3923 if (mMediaData.isBackedUp())
3924 {
3925 const MediaData::AttachmentList &oldAtts = mMediaData.backedUpData()->mAttachments;
3926
3927 MediaData::AttachmentList::const_iterator foundIt = oldAtts.end();
3928 uint32_t foundLevel = 0;
3929
3930 for (MediaData::AttachmentList::const_iterator it = oldAtts.begin(); it != oldAtts.end(); ++it)
3931 {
3932 uint32_t level = 0;
3933 MediumAttachment *pAttach = *it;
3934 ComObjPtr<Medium> pMedium = pAttach->i_getMedium();
3935 Assert(!pMedium.isNull() || pAttach->i_getType() != DeviceType_HardDisk);
3936 if (pMedium.isNull())
3937 continue;
3938
3939 if (pMedium->i_getBase(&level) == medium)
3940 {
3941 /* skip the hard disk if its currently attached (we
3942 * cannot attach the same hard disk twice) */
3943 if (i_findAttachment(mMediaData->mAttachments,
3944 pMedium))
3945 continue;
3946
3947 /* matched device, channel and bus (i.e. attached to the
3948 * same place) will win and immediately stop the search;
3949 * otherwise the attachment that has the youngest
3950 * descendant of medium will be used
3951 */
3952 if (pAttach->i_matches(Bstr(aName).raw(), aControllerPort, aDevice))
3953 {
3954 /* the simplest case: restore the whole attachment
3955 * and return, nothing else to do */
3956 mMediaData->mAttachments.push_back(*it);
3957
3958 /* Reattach the medium to the VM. */
3959 if (fHotplug || fSilent)
3960 {
3961 mediumLock.release();
3962 treeLock.release();
3963 alock.release();
3964
3965 MediumLockList *pMediumLockList(new MediumLockList());
3966
3967 rc = medium->i_createMediumLockList(true /* fFailIfInaccessible */,
3968 true /* fMediumLockWrite */,
3969 false /* fMediumLockWriteAll */,
3970 NULL,
3971 *pMediumLockList);
3972 alock.acquire();
3973 if (FAILED(rc))
3974 delete pMediumLockList;
3975 else
3976 {
3977 mData->mSession.mLockedMedia.Unlock();
3978 alock.release();
3979 rc = mData->mSession.mLockedMedia.Insert(pAttachTemp, pMediumLockList);
3980 mData->mSession.mLockedMedia.Lock();
3981 alock.acquire();
3982 }
3983 alock.release();
3984
3985 if (SUCCEEDED(rc))
3986 {
3987 rc = i_onStorageDeviceChange(pAttachTemp, FALSE /* aRemove */, fSilent);
3988 /* Remove lock list in case of error. */
3989 if (FAILED(rc))
3990 {
3991 mData->mSession.mLockedMedia.Unlock();
3992 mData->mSession.mLockedMedia.Remove(pAttachTemp);
3993 mData->mSession.mLockedMedia.Lock();
3994 }
3995 }
3996 }
3997
3998 return S_OK;
3999 }
4000 else if ( foundIt == oldAtts.end()
4001 || level > foundLevel /* prefer younger */
4002 )
4003 {
4004 foundIt = it;
4005 foundLevel = level;
4006 }
4007 }
4008 }
4009
4010 if (foundIt != oldAtts.end())
4011 {
4012 /* use the previously attached hard disk */
4013 medium = (*foundIt)->i_getMedium();
4014 mediumCaller.attach(medium);
4015 if (FAILED(mediumCaller.rc())) return mediumCaller.rc();
4016 mediumLock.attach(medium);
4017 /* not implicit, doesn't require association with this VM */
4018 fIndirect = false;
4019 associate = false;
4020 /* go right to the MediumAttachment creation */
4021 break;
4022 }
4023 }
4024
4025 /* must give up the medium lock and medium tree lock as below we
4026 * go over snapshots, which needs a lock with higher lock order. */
4027 mediumLock.release();
4028 treeLock.release();
4029
4030 /* then, search through snapshots for the best diff in the given
4031 * hard disk's chain to base the new diff on */
4032
4033 ComObjPtr<Medium> base;
4034 ComObjPtr<Snapshot> snap = mData->mCurrentSnapshot;
4035 while (snap)
4036 {
4037 AutoReadLock snapLock(snap COMMA_LOCKVAL_SRC_POS);
4038
4039 const MediaData::AttachmentList &snapAtts = snap->i_getSnapshotMachine()->mMediaData->mAttachments;
4040
4041 MediumAttachment *pAttachFound = NULL;
4042 uint32_t foundLevel = 0;
4043
4044 for (MediaData::AttachmentList::const_iterator it = snapAtts.begin(); it != snapAtts.end(); ++it)
4045 {
4046 MediumAttachment *pAttach = *it;
4047 ComObjPtr<Medium> pMedium = pAttach->i_getMedium();
4048 Assert(!pMedium.isNull() || pAttach->i_getType() != DeviceType_HardDisk);
4049 if (pMedium.isNull())
4050 continue;
4051
4052 uint32_t level = 0;
4053 if (pMedium->i_getBase(&level) == medium)
4054 {
4055 /* matched device, channel and bus (i.e. attached to the
4056 * same place) will win and immediately stop the search;
4057 * otherwise the attachment that has the youngest
4058 * descendant of medium will be used
4059 */
4060 if ( pAttach->i_getDevice() == aDevice
4061 && pAttach->i_getPort() == aControllerPort
4062 && pAttach->i_getControllerName() == aName
4063 )
4064 {
4065 pAttachFound = pAttach;
4066 break;
4067 }
4068 else if ( !pAttachFound
4069 || level > foundLevel /* prefer younger */
4070 )
4071 {
4072 pAttachFound = pAttach;
4073 foundLevel = level;
4074 }
4075 }
4076 }
4077
4078 if (pAttachFound)
4079 {
4080 base = pAttachFound->i_getMedium();
4081 break;
4082 }
4083
4084 snap = snap->i_getParent();
4085 }
4086
4087 /* re-lock medium tree and the medium, as we need it below */
4088 treeLock.acquire();
4089 mediumLock.acquire();
4090
4091 /* found a suitable diff, use it as a base */
4092 if (!base.isNull())
4093 {
4094 medium = base;
4095 mediumCaller.attach(medium);
4096 if (FAILED(mediumCaller.rc())) return mediumCaller.rc();
4097 mediumLock.attach(medium);
4098 }
4099 }
4100
4101 Utf8Str strFullSnapshotFolder;
4102 i_calculateFullPath(mUserData->s.strSnapshotFolder, strFullSnapshotFolder);
4103
4104 ComObjPtr<Medium> diff;
4105 diff.createObject();
4106 // store this diff in the same registry as the parent
4107 Guid uuidRegistryParent;
4108 if (!medium->i_getFirstRegistryMachineId(uuidRegistryParent))
4109 {
4110 // parent image has no registry: this can happen if we're attaching a new immutable
4111 // image that has not yet been attached (medium then points to the base and we're
4112 // creating the diff image for the immutable, and the parent is not yet registered);
4113 // put the parent in the machine registry then
4114 mediumLock.release();
4115 treeLock.release();
4116 alock.release();
4117 i_addMediumToRegistry(medium);
4118 alock.acquire();
4119 treeLock.acquire();
4120 mediumLock.acquire();
4121 medium->i_getFirstRegistryMachineId(uuidRegistryParent);
4122 }
4123 rc = diff->init(mParent,
4124 medium->i_getPreferredDiffFormat(),
4125 strFullSnapshotFolder.append(RTPATH_SLASH_STR),
4126 uuidRegistryParent,
4127 DeviceType_HardDisk);
4128 if (FAILED(rc)) return rc;
4129
4130 /* Apply the normal locking logic to the entire chain. */
4131 MediumLockList *pMediumLockList(new MediumLockList());
4132 mediumLock.release();
4133 treeLock.release();
4134 rc = diff->i_createMediumLockList(true /* fFailIfInaccessible */,
4135 true /* fMediumLockWrite */,
4136 false /* fMediumLockWriteAll */,
4137 medium,
4138 *pMediumLockList);
4139 treeLock.acquire();
4140 mediumLock.acquire();
4141 if (SUCCEEDED(rc))
4142 {
4143 mediumLock.release();
4144 treeLock.release();
4145 rc = pMediumLockList->Lock();
4146 treeLock.acquire();
4147 mediumLock.acquire();
4148 if (FAILED(rc))
4149 setError(rc,
4150 tr("Could not lock medium when creating diff '%s'"),
4151 diff->i_getLocationFull().c_str());
4152 else
4153 {
4154 /* will release the lock before the potentially lengthy
4155 * operation, so protect with the special state */
4156 MachineState_T oldState = mData->mMachineState;
4157 i_setMachineState(MachineState_SettingUp);
4158
4159 mediumLock.release();
4160 treeLock.release();
4161 alock.release();
4162
4163 rc = medium->i_createDiffStorage(diff,
4164 MediumVariant_Standard,
4165 pMediumLockList,
4166 NULL /* aProgress */,
4167 true /* aWait */);
4168
4169 alock.acquire();
4170 treeLock.acquire();
4171 mediumLock.acquire();
4172
4173 i_setMachineState(oldState);
4174 }
4175 }
4176
4177 /* Unlock the media and free the associated memory. */
4178 delete pMediumLockList;
4179
4180 if (FAILED(rc)) return rc;
4181
4182 /* use the created diff for the actual attachment */
4183 medium = diff;
4184 mediumCaller.attach(medium);
4185 if (FAILED(mediumCaller.rc())) return mediumCaller.rc();
4186 mediumLock.attach(medium);
4187 }
4188 while (0);
4189
4190 ComObjPtr<MediumAttachment> attachment;
4191 attachment.createObject();
4192 rc = attachment->init(this,
4193 medium,
4194 aName,
4195 aControllerPort,
4196 aDevice,
4197 aType,
4198 fIndirect,
4199 false /* fPassthrough */,
4200 false /* fTempEject */,
4201 false /* fNonRotational */,
4202 false /* fDiscard */,
4203 fHotplug /* fHotPluggable */,
4204 Utf8Str::Empty);
4205 if (FAILED(rc)) return rc;
4206
4207 if (associate && !medium.isNull())
4208 {
4209 // as the last step, associate the medium to the VM
4210 rc = medium->i_addBackReference(mData->mUuid);
4211 // here we can fail because of Deleting, or being in process of creating a Diff
4212 if (FAILED(rc)) return rc;
4213
4214 mediumLock.release();
4215 treeLock.release();
4216 alock.release();
4217 i_addMediumToRegistry(medium);
4218 alock.acquire();
4219 treeLock.acquire();
4220 mediumLock.acquire();
4221 }
4222
4223 /* success: finally remember the attachment */
4224 i_setModified(IsModified_Storage);
4225 mMediaData.backup();
4226 mMediaData->mAttachments.push_back(attachment);
4227
4228 mediumLock.release();
4229 treeLock.release();
4230 alock.release();
4231
4232 if (fHotplug || fSilent)
4233 {
4234 if (!medium.isNull())
4235 {
4236 MediumLockList *pMediumLockList(new MediumLockList());
4237
4238 rc = medium->i_createMediumLockList(true /* fFailIfInaccessible */,
4239 true /* fMediumLockWrite */,
4240 false /* fMediumLockWriteAll */,
4241 NULL,
4242 *pMediumLockList);
4243 alock.acquire();
4244 if (FAILED(rc))
4245 delete pMediumLockList;
4246 else
4247 {
4248 mData->mSession.mLockedMedia.Unlock();
4249 alock.release();
4250 rc = mData->mSession.mLockedMedia.Insert(attachment, pMediumLockList);
4251 mData->mSession.mLockedMedia.Lock();
4252 alock.acquire();
4253 }
4254 alock.release();
4255 }
4256
4257 if (SUCCEEDED(rc))
4258 {
4259 rc = i_onStorageDeviceChange(attachment, FALSE /* aRemove */, fSilent);
4260 /* Remove lock list in case of error. */
4261 if (FAILED(rc))
4262 {
4263 mData->mSession.mLockedMedia.Unlock();
4264 mData->mSession.mLockedMedia.Remove(attachment);
4265 mData->mSession.mLockedMedia.Lock();
4266 }
4267 }
4268 }
4269
4270 /* Save modified registries, but skip this machine as it's the caller's
4271 * job to save its settings like all other settings changes. */
4272 mParent->i_unmarkRegistryModified(i_getId());
4273 mParent->i_saveModifiedRegistries();
4274
4275 return rc;
4276}
4277
4278HRESULT Machine::detachDevice(const com::Utf8Str &aName, LONG aControllerPort,
4279 LONG aDevice)
4280{
4281 LogFlowThisFunc(("aControllerName=\"%s\" aControllerPort=%d aDevice=%d\n",
4282 aName.c_str(), aControllerPort, aDevice));
4283
4284 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
4285
4286 HRESULT rc = i_checkStateDependency(MutableOrRunningStateDep);
4287 if (FAILED(rc)) return rc;
4288
4289 AssertReturn(mData->mMachineState != MachineState_Saved, E_FAIL);
4290
4291 /* Check for an existing controller. */
4292 ComObjPtr<StorageController> ctl;
4293 rc = i_getStorageControllerByName(aName, ctl, true /* aSetError */);
4294 if (FAILED(rc)) return rc;
4295
4296 StorageControllerType_T ctrlType;
4297 rc = ctl->COMGETTER(ControllerType)(&ctrlType);
4298 if (FAILED(rc))
4299 return setError(E_FAIL,
4300 tr("Could not get type of controller '%s'"),
4301 aName.c_str());
4302
4303 bool fSilent = false;
4304 Utf8Str strReconfig;
4305
4306 /* Check whether the flag to allow silent storage attachment reconfiguration is set. */
4307 strReconfig = i_getExtraData(Utf8Str("VBoxInternal2/SilentReconfigureWhilePaused"));
4308 if ( mData->mMachineState == MachineState_Paused
4309 && strReconfig == "1")
4310 fSilent = true;
4311
4312 /* Check that the controller can do hotplugging if we detach the device while the VM is running. */
4313 bool fHotplug = false;
4314 if (!fSilent && Global::IsOnlineOrTransient(mData->mMachineState))
4315 fHotplug = true;
4316
4317 if (fHotplug && !i_isControllerHotplugCapable(ctrlType))
4318 return setError(VBOX_E_INVALID_VM_STATE,
4319 tr("Controller '%s' does not support hotplugging"),
4320 aName.c_str());
4321
4322 MediumAttachment *pAttach = i_findAttachment(mMediaData->mAttachments,
4323 Bstr(aName).raw(),
4324 aControllerPort,
4325 aDevice);
4326 if (!pAttach)
4327 return setError(VBOX_E_OBJECT_NOT_FOUND,
4328 tr("No storage device attached to device slot %d on port %d of controller '%s'"),
4329 aDevice, aControllerPort, aName.c_str());
4330
4331 if (fHotplug && !pAttach->i_getHotPluggable())
4332 return setError(VBOX_E_NOT_SUPPORTED,
4333 tr("The device slot %d on port %d of controller '%s' does not support hotplugging"),
4334 aDevice, aControllerPort, aName.c_str());
4335
4336 /*
4337 * The VM has to detach the device before we delete any implicit diffs.
4338 * If this fails we can roll back without loosing data.
4339 */
4340 if (fHotplug || fSilent)
4341 {
4342 alock.release();
4343 rc = i_onStorageDeviceChange(pAttach, TRUE /* aRemove */, fSilent);
4344 alock.acquire();
4345 }
4346 if (FAILED(rc)) return rc;
4347
4348 /* If we are here everything went well and we can delete the implicit now. */
4349 rc = i_detachDevice(pAttach, alock, NULL /* pSnapshot */);
4350
4351 alock.release();
4352
4353 /* Save modified registries, but skip this machine as it's the caller's
4354 * job to save its settings like all other settings changes. */
4355 mParent->i_unmarkRegistryModified(i_getId());
4356 mParent->i_saveModifiedRegistries();
4357
4358 return rc;
4359}
4360
4361HRESULT Machine::passthroughDevice(const com::Utf8Str &aName, LONG aControllerPort,
4362 LONG aDevice, BOOL aPassthrough)
4363{
4364 LogFlowThisFunc(("aName=\"%s\" aControllerPort=%d aDevice=%d aPassthrough=%d\n",
4365 aName.c_str(), aControllerPort, aDevice, aPassthrough));
4366
4367 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
4368
4369 HRESULT rc = i_checkStateDependency(MutableStateDep);
4370 if (FAILED(rc)) return rc;
4371
4372 AssertReturn(mData->mMachineState != MachineState_Saved, E_FAIL);
4373
4374 if (Global::IsOnlineOrTransient(mData->mMachineState))
4375 return setError(VBOX_E_INVALID_VM_STATE,
4376 tr("Invalid machine state: %s"),
4377 Global::stringifyMachineState(mData->mMachineState));
4378
4379 MediumAttachment *pAttach = i_findAttachment(mMediaData->mAttachments,
4380 Bstr(aName).raw(),
4381 aControllerPort,
4382 aDevice);
4383 if (!pAttach)
4384 return setError(VBOX_E_OBJECT_NOT_FOUND,
4385 tr("No storage device attached to device slot %d on port %d of controller '%s'"),
4386 aDevice, aControllerPort, aName.c_str());
4387
4388
4389 i_setModified(IsModified_Storage);
4390 mMediaData.backup();
4391
4392 AutoWriteLock attLock(pAttach COMMA_LOCKVAL_SRC_POS);
4393
4394 if (pAttach->i_getType() != DeviceType_DVD)
4395 return setError(E_INVALIDARG,
4396 tr("Setting passthrough rejected as the device attached to device slot %d on port %d of controller '%s' is not a DVD"),
4397 aDevice, aControllerPort, aName.c_str());
4398 pAttach->i_updatePassthrough(!!aPassthrough);
4399
4400 return S_OK;
4401}
4402
4403HRESULT Machine::temporaryEjectDevice(const com::Utf8Str &aName, LONG aControllerPort,
4404 LONG aDevice, BOOL aTemporaryEject)
4405{
4406
4407 LogFlowThisFunc(("aName=\"%s\" aControllerPort=%d aDevice=%d aTemporaryEject=%d\n",
4408 aName.c_str(), aControllerPort, aDevice, aTemporaryEject));
4409
4410 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
4411
4412 HRESULT rc = i_checkStateDependency(MutableOrSavedOrRunningStateDep);
4413 if (FAILED(rc)) return rc;
4414
4415 MediumAttachment *pAttach = i_findAttachment(mMediaData->mAttachments,
4416 Bstr(aName).raw(),
4417 aControllerPort,
4418 aDevice);
4419 if (!pAttach)
4420 return setError(VBOX_E_OBJECT_NOT_FOUND,
4421 tr("No storage device attached to device slot %d on port %d of controller '%s'"),
4422 aDevice, aControllerPort, aName.c_str());
4423
4424
4425 i_setModified(IsModified_Storage);
4426 mMediaData.backup();
4427
4428 AutoWriteLock attLock(pAttach COMMA_LOCKVAL_SRC_POS);
4429
4430 if (pAttach->i_getType() != DeviceType_DVD)
4431 return setError(E_INVALIDARG,
4432 tr("Setting temporary eject flag rejected as the device attached to device slot %d on port %d of controller '%s' is not a DVD"),
4433 aDevice, aControllerPort, aName.c_str());
4434 pAttach->i_updateTempEject(!!aTemporaryEject);
4435
4436 return S_OK;
4437}
4438
4439HRESULT Machine::nonRotationalDevice(const com::Utf8Str &aName, LONG aControllerPort,
4440 LONG aDevice, BOOL aNonRotational)
4441{
4442
4443 LogFlowThisFunc(("aName=\"%s\" aControllerPort=%d aDevice=%d aNonRotational=%d\n",
4444 aName.c_str(), aControllerPort, aDevice, aNonRotational));
4445
4446 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
4447
4448 HRESULT rc = i_checkStateDependency(MutableStateDep);
4449 if (FAILED(rc)) return rc;
4450
4451 AssertReturn(mData->mMachineState != MachineState_Saved, E_FAIL);
4452
4453 if (Global::IsOnlineOrTransient(mData->mMachineState))
4454 return setError(VBOX_E_INVALID_VM_STATE,
4455 tr("Invalid machine state: %s"),
4456 Global::stringifyMachineState(mData->mMachineState));
4457
4458 MediumAttachment *pAttach = i_findAttachment(mMediaData->mAttachments,
4459 Bstr(aName).raw(),
4460 aControllerPort,
4461 aDevice);
4462 if (!pAttach)
4463 return setError(VBOX_E_OBJECT_NOT_FOUND,
4464 tr("No storage device attached to device slot %d on port %d of controller '%s'"),
4465 aDevice, aControllerPort, aName.c_str());
4466
4467
4468 i_setModified(IsModified_Storage);
4469 mMediaData.backup();
4470
4471 AutoWriteLock attLock(pAttach COMMA_LOCKVAL_SRC_POS);
4472
4473 if (pAttach->i_getType() != DeviceType_HardDisk)
4474 return setError(E_INVALIDARG,
4475 tr("Setting the non-rotational medium flag rejected as the device attached to device slot %d on port %d of controller '%s' is not a hard disk"),
4476 aDevice, aControllerPort, aName.c_str());
4477 pAttach->i_updateNonRotational(!!aNonRotational);
4478
4479 return S_OK;
4480}
4481
4482HRESULT Machine::setAutoDiscardForDevice(const com::Utf8Str &aName, LONG aControllerPort,
4483 LONG aDevice, BOOL aDiscard)
4484{
4485
4486 LogFlowThisFunc(("aName=\"%s\" aControllerPort=%d aDevice=%d aDiscard=%d\n",
4487 aName.c_str(), aControllerPort, aDevice, aDiscard));
4488
4489 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
4490
4491 HRESULT rc = i_checkStateDependency(MutableStateDep);
4492 if (FAILED(rc)) return rc;
4493
4494 AssertReturn(mData->mMachineState != MachineState_Saved, E_FAIL);
4495
4496 if (Global::IsOnlineOrTransient(mData->mMachineState))
4497 return setError(VBOX_E_INVALID_VM_STATE,
4498 tr("Invalid machine state: %s"),
4499 Global::stringifyMachineState(mData->mMachineState));
4500
4501 MediumAttachment *pAttach = i_findAttachment(mMediaData->mAttachments,
4502 Bstr(aName).raw(),
4503 aControllerPort,
4504 aDevice);
4505 if (!pAttach)
4506 return setError(VBOX_E_OBJECT_NOT_FOUND,
4507 tr("No storage device attached to device slot %d on port %d of controller '%s'"),
4508 aDevice, aControllerPort, aName.c_str());
4509
4510
4511 i_setModified(IsModified_Storage);
4512 mMediaData.backup();
4513
4514 AutoWriteLock attLock(pAttach COMMA_LOCKVAL_SRC_POS);
4515
4516 if (pAttach->i_getType() != DeviceType_HardDisk)
4517 return setError(E_INVALIDARG,
4518 tr("Setting the discard medium flag rejected as the device attached to device slot %d on port %d of controller '%s' is not a hard disk"),
4519 aDevice, aControllerPort, aName.c_str());
4520 pAttach->i_updateDiscard(!!aDiscard);
4521
4522 return S_OK;
4523}
4524
4525HRESULT Machine::setHotPluggableForDevice(const com::Utf8Str &aName, LONG aControllerPort,
4526 LONG aDevice, BOOL aHotPluggable)
4527{
4528 LogFlowThisFunc(("aName=\"%s\" aControllerPort=%d aDevice=%d aHotPluggable=%d\n",
4529 aName.c_str(), aControllerPort, aDevice, aHotPluggable));
4530
4531 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
4532
4533 HRESULT rc = i_checkStateDependency(MutableStateDep);
4534 if (FAILED(rc)) return rc;
4535
4536 AssertReturn(mData->mMachineState != MachineState_Saved, E_FAIL);
4537
4538 if (Global::IsOnlineOrTransient(mData->mMachineState))
4539 return setError(VBOX_E_INVALID_VM_STATE,
4540 tr("Invalid machine state: %s"),
4541 Global::stringifyMachineState(mData->mMachineState));
4542
4543 MediumAttachment *pAttach = i_findAttachment(mMediaData->mAttachments,
4544 Bstr(aName).raw(),
4545 aControllerPort,
4546 aDevice);
4547 if (!pAttach)
4548 return setError(VBOX_E_OBJECT_NOT_FOUND,
4549 tr("No storage device attached to device slot %d on port %d of controller '%s'"),
4550 aDevice, aControllerPort, aName.c_str());
4551
4552 /* Check for an existing controller. */
4553 ComObjPtr<StorageController> ctl;
4554 rc = i_getStorageControllerByName(aName, ctl, true /* aSetError */);
4555 if (FAILED(rc)) return rc;
4556
4557 StorageControllerType_T ctrlType;
4558 rc = ctl->COMGETTER(ControllerType)(&ctrlType);
4559 if (FAILED(rc))
4560 return setError(E_FAIL,
4561 tr("Could not get type of controller '%s'"),
4562 aName.c_str());
4563
4564 if (!i_isControllerHotplugCapable(ctrlType))
4565 return setError(VBOX_E_NOT_SUPPORTED,
4566 tr("Controller '%s' does not support changing the hot-pluggable device flag"),
4567 aName.c_str());
4568
4569 i_setModified(IsModified_Storage);
4570 mMediaData.backup();
4571
4572 AutoWriteLock attLock(pAttach COMMA_LOCKVAL_SRC_POS);
4573
4574 if (pAttach->i_getType() == DeviceType_Floppy)
4575 return setError(E_INVALIDARG,
4576 tr("Setting the hot-pluggable device flag rejected as the device attached to device slot %d on port %d of controller '%s' is a floppy drive"),
4577 aDevice, aControllerPort, aName.c_str());
4578 pAttach->i_updateHotPluggable(!!aHotPluggable);
4579
4580 return S_OK;
4581}
4582
4583HRESULT Machine::setNoBandwidthGroupForDevice(const com::Utf8Str &aName, LONG aControllerPort,
4584 LONG aDevice)
4585{
4586 int rc = S_OK;
4587 LogFlowThisFunc(("aName=\"%s\" aControllerPort=%d aDevice=%d\n",
4588 aName.c_str(), aControllerPort, aDevice));
4589
4590 rc = setBandwidthGroupForDevice(aName, aControllerPort, aDevice, NULL);
4591
4592 return rc;
4593}
4594
4595HRESULT Machine::setBandwidthGroupForDevice(const com::Utf8Str &aName, LONG aControllerPort,
4596 LONG aDevice, const ComPtr<IBandwidthGroup> &aBandwidthGroup)
4597{
4598 LogFlowThisFunc(("aName=\"%s\" aControllerPort=%d aDevice=%d\n",
4599 aName.c_str(), aControllerPort, aDevice));
4600
4601 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
4602
4603 HRESULT rc = i_checkStateDependency(MutableOrSavedStateDep);
4604 if (FAILED(rc)) return rc;
4605
4606 if (Global::IsOnlineOrTransient(mData->mMachineState))
4607 return setError(VBOX_E_INVALID_VM_STATE,
4608 tr("Invalid machine state: %s"),
4609 Global::stringifyMachineState(mData->mMachineState));
4610
4611 MediumAttachment *pAttach = i_findAttachment(mMediaData->mAttachments,
4612 Bstr(aName).raw(),
4613 aControllerPort,
4614 aDevice);
4615 if (!pAttach)
4616 return setError(VBOX_E_OBJECT_NOT_FOUND,
4617 tr("No storage device attached to device slot %d on port %d of controller '%s'"),
4618 aDevice, aControllerPort, aName.c_str());
4619
4620
4621 i_setModified(IsModified_Storage);
4622 mMediaData.backup();
4623
4624 IBandwidthGroup *iB = aBandwidthGroup;
4625 ComObjPtr<BandwidthGroup> group = static_cast<BandwidthGroup*>(iB);
4626 if (aBandwidthGroup && group.isNull())
4627 return setError(E_INVALIDARG, "The given bandwidth group pointer is invalid");
4628
4629 AutoWriteLock attLock(pAttach COMMA_LOCKVAL_SRC_POS);
4630
4631 const Utf8Str strBandwidthGroupOld = pAttach->i_getBandwidthGroup();
4632 if (strBandwidthGroupOld.isNotEmpty())
4633 {
4634 /* Get the bandwidth group object and release it - this must not fail. */
4635 ComObjPtr<BandwidthGroup> pBandwidthGroupOld;
4636 rc = i_getBandwidthGroup(strBandwidthGroupOld, pBandwidthGroupOld, false);
4637 Assert(SUCCEEDED(rc));
4638
4639 pBandwidthGroupOld->i_release();
4640 pAttach->i_updateBandwidthGroup(Utf8Str::Empty);
4641 }
4642
4643 if (!group.isNull())
4644 {
4645 group->i_reference();
4646 pAttach->i_updateBandwidthGroup(group->i_getName());
4647 }
4648
4649 return S_OK;
4650}
4651
4652HRESULT Machine::attachDeviceWithoutMedium(const com::Utf8Str &aName,
4653 LONG aControllerPort,
4654 LONG aDevice,
4655 DeviceType_T aType)
4656{
4657 HRESULT rc = S_OK;
4658
4659 LogFlowThisFunc(("aName=\"%s\" aControllerPort=%d aDevice=%d aType=%d aMedium=%p\n",
4660 aName.c_str(), aControllerPort, aDevice, aType));
4661
4662 rc = AttachDevice(Bstr(aName).raw(), aControllerPort, aDevice, aType, NULL);
4663
4664 return rc;
4665}
4666
4667
4668HRESULT Machine::unmountMedium(const com::Utf8Str &aName,
4669 LONG aControllerPort,
4670 LONG aDevice,
4671 BOOL aForce)
4672{
4673 int rc = S_OK;
4674 LogFlowThisFunc(("aName=\"%s\" aControllerPort=%d aDevice=%d",
4675 aName.c_str(), aControllerPort, aForce));
4676
4677 rc = mountMedium(aName, aControllerPort, aDevice, NULL, aForce);
4678
4679 return rc;
4680}
4681
4682HRESULT Machine::mountMedium(const com::Utf8Str &aName,
4683 LONG aControllerPort,
4684 LONG aDevice,
4685 const ComPtr<IMedium> &aMedium,
4686 BOOL aForce)
4687{
4688 int rc = S_OK;
4689 LogFlowThisFunc(("aName=\"%s\" aControllerPort=%d aDevice=%d aForce=%d\n",
4690 aName.c_str(), aControllerPort, aDevice, aForce));
4691
4692 // request the host lock first, since might be calling Host methods for getting host drives;
4693 // next, protect the media tree all the while we're in here, as well as our member variables
4694 AutoMultiWriteLock3 multiLock(mParent->i_host()->lockHandle(),
4695 this->lockHandle(),
4696 &mParent->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
4697
4698 ComObjPtr<MediumAttachment> pAttach = i_findAttachment(mMediaData->mAttachments,
4699 Bstr(aName).raw(),
4700 aControllerPort,
4701 aDevice);
4702 if (pAttach.isNull())
4703 return setError(VBOX_E_OBJECT_NOT_FOUND,
4704 tr("No drive attached to device slot %d on port %d of controller '%s'"),
4705 aDevice, aControllerPort, aName.c_str());
4706
4707 /* Remember previously mounted medium. The medium before taking the
4708 * backup is not necessarily the same thing. */
4709 ComObjPtr<Medium> oldmedium;
4710 oldmedium = pAttach->i_getMedium();
4711
4712 IMedium *iM = aMedium;
4713 ComObjPtr<Medium> pMedium = static_cast<Medium*>(iM);
4714 if (aMedium && pMedium.isNull())
4715 return setError(E_INVALIDARG, "The given medium pointer is invalid");
4716
4717 AutoCaller mediumCaller(pMedium);
4718 if (FAILED(mediumCaller.rc())) return mediumCaller.rc();
4719
4720 AutoWriteLock mediumLock(pMedium COMMA_LOCKVAL_SRC_POS);
4721 if (pMedium)
4722 {
4723 DeviceType_T mediumType = pAttach->i_getType();
4724 switch (mediumType)
4725 {
4726 case DeviceType_DVD:
4727 case DeviceType_Floppy:
4728 break;
4729
4730 default:
4731 return setError(VBOX_E_INVALID_OBJECT_STATE,
4732 tr("The device at port %d, device %d of controller '%s' of this virtual machine is not removeable"),
4733 aControllerPort,
4734 aDevice,
4735 aName.c_str());
4736 }
4737 }
4738
4739 i_setModified(IsModified_Storage);
4740 mMediaData.backup();
4741
4742 {
4743 // The backup operation makes the pAttach reference point to the
4744 // old settings. Re-get the correct reference.
4745 pAttach = i_findAttachment(mMediaData->mAttachments,
4746 Bstr(aName).raw(),
4747 aControllerPort,
4748 aDevice);
4749 if (!oldmedium.isNull())
4750 oldmedium->i_removeBackReference(mData->mUuid);
4751 if (!pMedium.isNull())
4752 {
4753 pMedium->i_addBackReference(mData->mUuid);
4754
4755 mediumLock.release();
4756 multiLock.release();
4757 i_addMediumToRegistry(pMedium);
4758 multiLock.acquire();
4759 mediumLock.acquire();
4760 }
4761
4762 AutoWriteLock attLock(pAttach COMMA_LOCKVAL_SRC_POS);
4763 pAttach->i_updateMedium(pMedium);
4764 }
4765
4766 i_setModified(IsModified_Storage);
4767
4768 mediumLock.release();
4769 multiLock.release();
4770 rc = i_onMediumChange(pAttach, aForce);
4771 multiLock.acquire();
4772 mediumLock.acquire();
4773
4774 /* On error roll back this change only. */
4775 if (FAILED(rc))
4776 {
4777 if (!pMedium.isNull())
4778 pMedium->i_removeBackReference(mData->mUuid);
4779 pAttach = i_findAttachment(mMediaData->mAttachments,
4780 Bstr(aName).raw(),
4781 aControllerPort,
4782 aDevice);
4783 /* If the attachment is gone in the meantime, bail out. */
4784 if (pAttach.isNull())
4785 return rc;
4786 AutoWriteLock attLock(pAttach COMMA_LOCKVAL_SRC_POS);
4787 if (!oldmedium.isNull())
4788 oldmedium->i_addBackReference(mData->mUuid);
4789 pAttach->i_updateMedium(oldmedium);
4790 }
4791
4792 mediumLock.release();
4793 multiLock.release();
4794
4795 /* Save modified registries, but skip this machine as it's the caller's
4796 * job to save its settings like all other settings changes. */
4797 mParent->i_unmarkRegistryModified(i_getId());
4798 mParent->i_saveModifiedRegistries();
4799
4800 return rc;
4801}
4802HRESULT Machine::getMedium(const com::Utf8Str &aName,
4803 LONG aControllerPort,
4804 LONG aDevice,
4805 ComPtr<IMedium> &aMedium)
4806{
4807 LogFlowThisFunc(("aName=\"%s\" aControllerPort=%d aDevice=%d\n",
4808 aName.c_str(), aControllerPort, aDevice));
4809
4810 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
4811
4812 aMedium = NULL;
4813
4814 ComObjPtr<MediumAttachment> pAttach = i_findAttachment(mMediaData->mAttachments,
4815 Bstr(aName).raw(),
4816 aControllerPort,
4817 aDevice);
4818 if (pAttach.isNull())
4819 return setError(VBOX_E_OBJECT_NOT_FOUND,
4820 tr("No storage device attached to device slot %d on port %d of controller '%s'"),
4821 aDevice, aControllerPort, aName.c_str());
4822
4823 aMedium = pAttach->i_getMedium();
4824
4825 return S_OK;
4826}
4827
4828HRESULT Machine::getSerialPort(ULONG aSlot, ComPtr<ISerialPort> &aPort)
4829{
4830
4831 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
4832
4833 mSerialPorts[aSlot].queryInterfaceTo(aPort.asOutParam());
4834
4835 return S_OK;
4836}
4837
4838HRESULT Machine::getParallelPort(ULONG aSlot, ComPtr<IParallelPort> &aPort)
4839{
4840 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
4841
4842 mParallelPorts[aSlot].queryInterfaceTo(aPort.asOutParam());
4843
4844 return S_OK;
4845}
4846
4847HRESULT Machine::getNetworkAdapter(ULONG aSlot, ComPtr<INetworkAdapter> &aAdapter)
4848{
4849 /* Do not assert if slot is out of range, just return the advertised
4850 status. testdriver/vbox.py triggers this in logVmInfo. */
4851 if (aSlot >= mNetworkAdapters.size())
4852 return setError(E_INVALIDARG,
4853 tr("No network adapter in slot %RU32 (total %RU32 adapters)"),
4854 aSlot, mNetworkAdapters.size());
4855
4856 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
4857
4858 mNetworkAdapters[aSlot].queryInterfaceTo(aAdapter.asOutParam());
4859
4860 return S_OK;
4861}
4862
4863HRESULT Machine::getExtraDataKeys(std::vector<com::Utf8Str> &aKeys)
4864{
4865 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
4866
4867 aKeys.resize(mData->pMachineConfigFile->mapExtraDataItems.size());
4868 size_t i = 0;
4869 for (settings::StringsMap::const_iterator it = mData->pMachineConfigFile->mapExtraDataItems.begin();
4870 it != mData->pMachineConfigFile->mapExtraDataItems.end();
4871 ++it, ++i)
4872 aKeys[i] = it->first;
4873
4874 return S_OK;
4875}
4876
4877 /**
4878 * @note Locks this object for reading.
4879 */
4880HRESULT Machine::getExtraData(const com::Utf8Str &aKey,
4881 com::Utf8Str &aValue)
4882{
4883 /* start with nothing found */
4884 aValue = "";
4885
4886 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
4887
4888 settings::StringsMap::const_iterator it = mData->pMachineConfigFile->mapExtraDataItems.find(aKey);
4889 if (it != mData->pMachineConfigFile->mapExtraDataItems.end())
4890 // found:
4891 aValue = it->second; // source is a Utf8Str
4892
4893 /* return the result to caller (may be empty) */
4894 return S_OK;
4895}
4896
4897 /**
4898 * @note Locks mParent for writing + this object for writing.
4899 */
4900HRESULT Machine::setExtraData(const com::Utf8Str &aKey, const com::Utf8Str &aValue)
4901{
4902 Utf8Str strOldValue; // empty
4903
4904 // locking note: we only hold the read lock briefly to look up the old value,
4905 // then release it and call the onExtraCanChange callbacks. There is a small
4906 // chance of a race insofar as the callback might be called twice if two callers
4907 // change the same key at the same time, but that's a much better solution
4908 // than the deadlock we had here before. The actual changing of the extradata
4909 // is then performed under the write lock and race-free.
4910
4911 // look up the old value first; if nothing has changed then we need not do anything
4912 {
4913 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS); // hold read lock only while looking up
4914
4915 // For snapshots don't even think about allowing changes, extradata
4916 // is global for a machine, so there is nothing snapshot specific.
4917 if (i_isSnapshotMachine())
4918 return setError(VBOX_E_INVALID_VM_STATE,
4919 tr("Cannot set extradata for a snapshot"));
4920
4921 // check if the right IMachine instance is used
4922 if (mData->mRegistered && !i_isSessionMachine())
4923 return setError(VBOX_E_INVALID_VM_STATE,
4924 tr("Cannot set extradata for an immutable machine"));
4925
4926 settings::StringsMap::const_iterator it = mData->pMachineConfigFile->mapExtraDataItems.find(aKey);
4927 if (it != mData->pMachineConfigFile->mapExtraDataItems.end())
4928 strOldValue = it->second;
4929 }
4930
4931 bool fChanged;
4932 if ((fChanged = (strOldValue != aValue)))
4933 {
4934 // ask for permission from all listeners outside the locks;
4935 // i_onExtraDataCanChange() only briefly requests the VirtualBox
4936 // lock to copy the list of callbacks to invoke
4937 Bstr error;
4938 Bstr bstrValue(aValue);
4939
4940 if (!mParent->i_onExtraDataCanChange(mData->mUuid, Bstr(aKey).raw(), bstrValue.raw(), error))
4941 {
4942 const char *sep = error.isEmpty() ? "" : ": ";
4943 CBSTR err = error.raw();
4944 Log1WarningFunc(("Someone vetoed! Change refused%s%ls\n", sep, err));
4945 return setError(E_ACCESSDENIED,
4946 tr("Could not set extra data because someone refused the requested change of '%s' to '%s'%s%ls"),
4947 aKey.c_str(),
4948 aValue.c_str(),
4949 sep,
4950 err);
4951 }
4952
4953 // data is changing and change not vetoed: then write it out under the lock
4954 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
4955
4956 if (aValue.isEmpty())
4957 mData->pMachineConfigFile->mapExtraDataItems.erase(aKey);
4958 else
4959 mData->pMachineConfigFile->mapExtraDataItems[aKey] = aValue;
4960 // creates a new key if needed
4961
4962 bool fNeedsGlobalSaveSettings = false;
4963 // This saving of settings is tricky: there is no "old state" for the
4964 // extradata items at all (unlike all other settings), so the old/new
4965 // settings comparison would give a wrong result!
4966 i_saveSettings(&fNeedsGlobalSaveSettings, SaveS_Force);
4967
4968 if (fNeedsGlobalSaveSettings)
4969 {
4970 // save the global settings; for that we should hold only the VirtualBox lock
4971 alock.release();
4972 AutoWriteLock vboxlock(mParent COMMA_LOCKVAL_SRC_POS);
4973 mParent->i_saveSettings();
4974 }
4975 }
4976
4977 // fire notification outside the lock
4978 if (fChanged)
4979 mParent->i_onExtraDataChange(mData->mUuid, Bstr(aKey).raw(), Bstr(aValue).raw());
4980
4981 return S_OK;
4982}
4983
4984HRESULT Machine::setSettingsFilePath(const com::Utf8Str &aSettingsFilePath, ComPtr<IProgress> &aProgress)
4985{
4986 aProgress = NULL;
4987 NOREF(aSettingsFilePath);
4988 ReturnComNotImplemented();
4989}
4990
4991HRESULT Machine::saveSettings()
4992{
4993 AutoWriteLock mlock(this COMMA_LOCKVAL_SRC_POS);
4994
4995 HRESULT rc = i_checkStateDependency(MutableOrSavedOrRunningStateDep);
4996 if (FAILED(rc)) return rc;
4997
4998 /* the settings file path may never be null */
4999 ComAssertRet(!mData->m_strConfigFileFull.isEmpty(), E_FAIL);
5000
5001 /* save all VM data excluding snapshots */
5002 bool fNeedsGlobalSaveSettings = false;
5003 rc = i_saveSettings(&fNeedsGlobalSaveSettings);
5004 mlock.release();
5005
5006 if (SUCCEEDED(rc) && fNeedsGlobalSaveSettings)
5007 {
5008 // save the global settings; for that we should hold only the VirtualBox lock
5009 AutoWriteLock vlock(mParent COMMA_LOCKVAL_SRC_POS);
5010 rc = mParent->i_saveSettings();
5011 }
5012
5013 return rc;
5014}
5015
5016
5017HRESULT Machine::discardSettings()
5018{
5019 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
5020
5021 HRESULT rc = i_checkStateDependency(MutableOrSavedOrRunningStateDep);
5022 if (FAILED(rc)) return rc;
5023
5024 /*
5025 * during this rollback, the session will be notified if data has
5026 * been actually changed
5027 */
5028 i_rollback(true /* aNotify */);
5029
5030 return S_OK;
5031}
5032
5033/** @note Locks objects! */
5034HRESULT Machine::unregister(AutoCaller &autoCaller,
5035 CleanupMode_T aCleanupMode,
5036 std::vector<ComPtr<IMedium> > &aMedia)
5037{
5038 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
5039
5040 Guid id(i_getId());
5041
5042 if (mData->mSession.mState != SessionState_Unlocked)
5043 return setError(VBOX_E_INVALID_OBJECT_STATE,
5044 tr("Cannot unregister the machine '%s' while it is locked"),
5045 mUserData->s.strName.c_str());
5046
5047 // wait for state dependents to drop to zero
5048 i_ensureNoStateDependencies();
5049
5050 if (!mData->mAccessible)
5051 {
5052 // inaccessible maschines can only be unregistered; uninitialize ourselves
5053 // here because currently there may be no unregistered that are inaccessible
5054 // (this state combination is not supported). Note releasing the caller and
5055 // leaving the lock before calling uninit()
5056 alock.release();
5057 autoCaller.release();
5058
5059 uninit();
5060
5061 mParent->i_unregisterMachine(this, id);
5062 // calls VirtualBox::i_saveSettings()
5063
5064 return S_OK;
5065 }
5066
5067 HRESULT rc = S_OK;
5068
5069 /// @todo r=klaus this is stupid... why is the saved state always deleted?
5070 // discard saved state
5071 if (mData->mMachineState == MachineState_Saved)
5072 {
5073 // add the saved state file to the list of files the caller should delete
5074 Assert(!mSSData->strStateFilePath.isEmpty());
5075 mData->llFilesToDelete.push_back(mSSData->strStateFilePath);
5076
5077 mSSData->strStateFilePath.setNull();
5078
5079 // unconditionally set the machine state to powered off, we now
5080 // know no session has locked the machine
5081 mData->mMachineState = MachineState_PoweredOff;
5082 }
5083
5084 size_t cSnapshots = 0;
5085 if (mData->mFirstSnapshot)
5086 cSnapshots = mData->mFirstSnapshot->i_getAllChildrenCount() + 1;
5087 if (cSnapshots && aCleanupMode == CleanupMode_UnregisterOnly)
5088 // fail now before we start detaching media
5089 return setError(VBOX_E_INVALID_OBJECT_STATE,
5090 tr("Cannot unregister the machine '%s' because it has %d snapshots"),
5091 mUserData->s.strName.c_str(), cSnapshots);
5092
5093 // This list collects the medium objects from all medium attachments
5094 // which we will detach from the machine and its snapshots, in a specific
5095 // order which allows for closing all media without getting "media in use"
5096 // errors, simply by going through the list from the front to the back:
5097 // 1) first media from machine attachments (these have the "leaf" attachments with snapshots
5098 // and must be closed before the parent media from the snapshots, or closing the parents
5099 // will fail because they still have children);
5100 // 2) media from the youngest snapshots followed by those from the parent snapshots until
5101 // the root ("first") snapshot of the machine.
5102 MediaList llMedia;
5103
5104 if ( !mMediaData.isNull() // can be NULL if machine is inaccessible
5105 && mMediaData->mAttachments.size()
5106 )
5107 {
5108 // we have media attachments: detach them all and add the Medium objects to our list
5109 if (aCleanupMode != CleanupMode_UnregisterOnly)
5110 i_detachAllMedia(alock, NULL /* pSnapshot */, aCleanupMode, llMedia);
5111 else
5112 return setError(VBOX_E_INVALID_OBJECT_STATE,
5113 tr("Cannot unregister the machine '%s' because it has %d media attachments"),
5114 mUserData->s.strName.c_str(), mMediaData->mAttachments.size());
5115 }
5116
5117 if (cSnapshots)
5118 {
5119 // add the media from the medium attachments of the snapshots to llMedia
5120 // as well, after the "main" machine media; Snapshot::uninitRecursively()
5121 // calls Machine::detachAllMedia() for the snapshot machine, recursing
5122 // into the children first
5123
5124 // Snapshot::beginDeletingSnapshot() asserts if the machine state is not this
5125 MachineState_T oldState = mData->mMachineState;
5126 mData->mMachineState = MachineState_DeletingSnapshot;
5127
5128 // make a copy of the first snapshot so the refcount does not drop to 0
5129 // in beginDeletingSnapshot, which sets pFirstSnapshot to 0 (that hangs
5130 // because of the AutoCaller voodoo)
5131 ComObjPtr<Snapshot> pFirstSnapshot = mData->mFirstSnapshot;
5132
5133 // GO!
5134 pFirstSnapshot->i_uninitRecursively(alock, aCleanupMode, llMedia, mData->llFilesToDelete);
5135
5136 mData->mMachineState = oldState;
5137 }
5138
5139 if (FAILED(rc))
5140 {
5141 i_rollbackMedia();
5142 return rc;
5143 }
5144
5145 // commit all the media changes made above
5146 i_commitMedia();
5147
5148 mData->mRegistered = false;
5149
5150 // machine lock no longer needed
5151 alock.release();
5152
5153 // return media to caller
5154 size_t i = 0;
5155 aMedia.resize(llMedia.size());
5156 for (MediaList::iterator it = llMedia.begin(); it != llMedia.end(); ++it, ++i)
5157 (*it).queryInterfaceTo(aMedia[i].asOutParam());
5158
5159 mParent->i_unregisterMachine(this, id);
5160 // calls VirtualBox::i_saveSettings() and VirtualBox::saveModifiedRegistries()
5161
5162 return S_OK;
5163}
5164
5165/**
5166 * Task record for deleting a machine config.
5167 */
5168struct Machine::DeleteConfigTask
5169 : public Machine::Task
5170{
5171 DeleteConfigTask(Machine *m,
5172 Progress *p,
5173 const Utf8Str &t,
5174 const RTCList<ComPtr<IMedium> > &llMediums,
5175 const StringsList &llFilesToDelete)
5176 : Task(m, p, t),
5177 m_llMediums(llMediums),
5178 m_llFilesToDelete(llFilesToDelete)
5179 {}
5180
5181 void handler()
5182 {
5183 m_pMachine->i_deleteConfigHandler(*this);
5184 }
5185
5186 RTCList<ComPtr<IMedium> > m_llMediums;
5187 StringsList m_llFilesToDelete;
5188};
5189
5190/**
5191 * Task thread implementation for SessionMachine::DeleteConfig(), called from
5192 * SessionMachine::taskHandler().
5193 *
5194 * @note Locks this object for writing.
5195 *
5196 * @param task
5197 * @return
5198 */
5199void Machine::i_deleteConfigHandler(DeleteConfigTask &task)
5200{
5201 LogFlowThisFuncEnter();
5202
5203 AutoCaller autoCaller(this);
5204 LogFlowThisFunc(("state=%d\n", getObjectState().getState()));
5205 if (FAILED(autoCaller.rc()))
5206 {
5207 /* we might have been uninitialized because the session was accidentally
5208 * closed by the client, so don't assert */
5209 HRESULT rc = setError(E_FAIL,
5210 tr("The session has been accidentally closed"));
5211 task.m_pProgress->i_notifyComplete(rc);
5212 LogFlowThisFuncLeave();
5213 return;
5214 }
5215
5216 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
5217
5218 HRESULT rc = S_OK;
5219
5220 try
5221 {
5222 ULONG uLogHistoryCount = 3;
5223 ComPtr<ISystemProperties> systemProperties;
5224 rc = mParent->COMGETTER(SystemProperties)(systemProperties.asOutParam());
5225 if (FAILED(rc)) throw rc;
5226
5227 if (!systemProperties.isNull())
5228 {
5229 rc = systemProperties->COMGETTER(LogHistoryCount)(&uLogHistoryCount);
5230 if (FAILED(rc)) throw rc;
5231 }
5232
5233 MachineState_T oldState = mData->mMachineState;
5234 i_setMachineState(MachineState_SettingUp);
5235 alock.release();
5236 for (size_t i = 0; i < task.m_llMediums.size(); ++i)
5237 {
5238 ComObjPtr<Medium> pMedium = (Medium*)(IMedium*)(task.m_llMediums.at(i));
5239 {
5240 AutoCaller mac(pMedium);
5241 if (FAILED(mac.rc())) throw mac.rc();
5242 Utf8Str strLocation = pMedium->i_getLocationFull();
5243 rc = task.m_pProgress->SetNextOperation(BstrFmt(tr("Deleting '%s'"), strLocation.c_str()).raw(), 1);
5244 if (FAILED(rc)) throw rc;
5245 LogFunc(("Deleting file %s\n", strLocation.c_str()));
5246 }
5247 if (pMedium->i_isMediumFormatFile())
5248 {
5249 ComPtr<IProgress> pProgress2;
5250 rc = pMedium->DeleteStorage(pProgress2.asOutParam());
5251 if (FAILED(rc)) throw rc;
5252 rc = task.m_pProgress->WaitForAsyncProgressCompletion(pProgress2);
5253 if (FAILED(rc)) throw rc;
5254 /* Check the result of the asynchronous process. */
5255 LONG iRc;
5256 rc = pProgress2->COMGETTER(ResultCode)(&iRc);
5257 if (FAILED(rc)) throw rc;
5258 /* If the thread of the progress object has an error, then
5259 * retrieve the error info from there, or it'll be lost. */
5260 if (FAILED(iRc))
5261 throw setError(ProgressErrorInfo(pProgress2));
5262 }
5263
5264 /* Close the medium, deliberately without checking the return
5265 * code, and without leaving any trace in the error info, as
5266 * a failure here is a very minor issue, which shouldn't happen
5267 * as above we even managed to delete the medium. */
5268 {
5269 ErrorInfoKeeper eik;
5270 pMedium->Close();
5271 }
5272 }
5273 i_setMachineState(oldState);
5274 alock.acquire();
5275
5276 // delete the files pushed on the task list by Machine::Delete()
5277 // (this includes saved states of the machine and snapshots and
5278 // medium storage files from the IMedium list passed in, and the
5279 // machine XML file)
5280 StringsList::const_iterator it = task.m_llFilesToDelete.begin();
5281 while (it != task.m_llFilesToDelete.end())
5282 {
5283 const Utf8Str &strFile = *it;
5284 LogFunc(("Deleting file %s\n", strFile.c_str()));
5285 int vrc = RTFileDelete(strFile.c_str());
5286 if (RT_FAILURE(vrc))
5287 throw setError(VBOX_E_IPRT_ERROR,
5288 tr("Could not delete file '%s' (%Rrc)"), strFile.c_str(), vrc);
5289
5290 ++it;
5291 if (it == task.m_llFilesToDelete.end())
5292 {
5293 rc = task.m_pProgress->SetNextOperation(Bstr(tr("Cleaning up machine directory")).raw(), 1);
5294 if (FAILED(rc)) throw rc;
5295 break;
5296 }
5297
5298 rc = task.m_pProgress->SetNextOperation(BstrFmt(tr("Deleting '%s'"), it->c_str()).raw(), 1);
5299 if (FAILED(rc)) throw rc;
5300 }
5301
5302 /* delete the settings only when the file actually exists */
5303 if (mData->pMachineConfigFile->fileExists())
5304 {
5305 /* Delete any backup or uncommitted XML files. Ignore failures.
5306 See the fSafe parameter of xml::XmlFileWriter::write for details. */
5307 /** @todo Find a way to avoid referring directly to iprt/xml.h here. */
5308 Utf8Str otherXml = Utf8StrFmt("%s%s", mData->m_strConfigFileFull.c_str(), xml::XmlFileWriter::s_pszTmpSuff);
5309 RTFileDelete(otherXml.c_str());
5310 otherXml = Utf8StrFmt("%s%s", mData->m_strConfigFileFull.c_str(), xml::XmlFileWriter::s_pszPrevSuff);
5311 RTFileDelete(otherXml.c_str());
5312
5313 /* delete the Logs folder, nothing important should be left
5314 * there (we don't check for errors because the user might have
5315 * some private files there that we don't want to delete) */
5316 Utf8Str logFolder;
5317 getLogFolder(logFolder);
5318 Assert(logFolder.length());
5319 if (RTDirExists(logFolder.c_str()))
5320 {
5321 /* Delete all VBox.log[.N] files from the Logs folder
5322 * (this must be in sync with the rotation logic in
5323 * Console::powerUpThread()). Also, delete the VBox.png[.N]
5324 * files that may have been created by the GUI. */
5325 Utf8Str log = Utf8StrFmt("%s%cVBox.log",
5326 logFolder.c_str(), RTPATH_DELIMITER);
5327 RTFileDelete(log.c_str());
5328 log = Utf8StrFmt("%s%cVBox.png",
5329 logFolder.c_str(), RTPATH_DELIMITER);
5330 RTFileDelete(log.c_str());
5331 for (int i = uLogHistoryCount; i > 0; i--)
5332 {
5333 log = Utf8StrFmt("%s%cVBox.log.%d",
5334 logFolder.c_str(), RTPATH_DELIMITER, i);
5335 RTFileDelete(log.c_str());
5336 log = Utf8StrFmt("%s%cVBox.png.%d",
5337 logFolder.c_str(), RTPATH_DELIMITER, i);
5338 RTFileDelete(log.c_str());
5339 }
5340#if defined(RT_OS_WINDOWS)
5341 log = Utf8StrFmt("%s%cVBoxStartup.log",
5342 logFolder.c_str(), RTPATH_DELIMITER);
5343 RTFileDelete(log.c_str());
5344#endif
5345
5346 RTDirRemove(logFolder.c_str());
5347 }
5348
5349 /* delete the Snapshots folder, nothing important should be left
5350 * there (we don't check for errors because the user might have
5351 * some private files there that we don't want to delete) */
5352 Utf8Str strFullSnapshotFolder;
5353 i_calculateFullPath(mUserData->s.strSnapshotFolder, strFullSnapshotFolder);
5354 Assert(!strFullSnapshotFolder.isEmpty());
5355 if (RTDirExists(strFullSnapshotFolder.c_str()))
5356 RTDirRemove(strFullSnapshotFolder.c_str());
5357
5358 // delete the directory that contains the settings file, but only
5359 // if it matches the VM name
5360 Utf8Str settingsDir;
5361 if (i_isInOwnDir(&settingsDir))
5362 RTDirRemove(settingsDir.c_str());
5363 }
5364
5365 alock.release();
5366
5367 mParent->i_saveModifiedRegistries();
5368 }
5369 catch (HRESULT aRC) { rc = aRC; }
5370
5371 task.m_pProgress->i_notifyComplete(rc);
5372
5373 LogFlowThisFuncLeave();
5374}
5375
5376HRESULT Machine::deleteConfig(const std::vector<ComPtr<IMedium> > &aMedia, ComPtr<IProgress> &aProgress)
5377{
5378 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
5379
5380 HRESULT rc = i_checkStateDependency(MutableStateDep);
5381 if (FAILED(rc)) return rc;
5382
5383 if (mData->mRegistered)
5384 return setError(VBOX_E_INVALID_VM_STATE,
5385 tr("Cannot delete settings of a registered machine"));
5386
5387 // collect files to delete
5388 StringsList llFilesToDelete(mData->llFilesToDelete); // saved states pushed here by Unregister()
5389 if (mData->pMachineConfigFile->fileExists())
5390 llFilesToDelete.push_back(mData->m_strConfigFileFull);
5391
5392 RTCList<ComPtr<IMedium> > llMediums;
5393 for (size_t i = 0; i < aMedia.size(); ++i)
5394 {
5395 IMedium *pIMedium(aMedia[i]);
5396 ComObjPtr<Medium> pMedium = static_cast<Medium*>(pIMedium);
5397 if (pMedium.isNull())
5398 return setError(E_INVALIDARG, "The given medium pointer with index %d is invalid", i);
5399 SafeArray<BSTR> ids;
5400 rc = pMedium->COMGETTER(MachineIds)(ComSafeArrayAsOutParam(ids));
5401 if (FAILED(rc)) return rc;
5402 /* At this point the medium should not have any back references
5403 * anymore. If it has it is attached to another VM and *must* not
5404 * deleted. */
5405 if (ids.size() < 1)
5406 llMediums.append(pMedium);
5407 }
5408
5409 ComObjPtr<Progress> pProgress;
5410 pProgress.createObject();
5411 rc = pProgress->init(i_getVirtualBox(),
5412 static_cast<IMachine*>(this) /* aInitiator */,
5413 Bstr(tr("Deleting files")).raw(),
5414 true /* fCancellable */,
5415 (ULONG)(llFilesToDelete.size() + llMediums.size() + 1), // cOperations
5416 BstrFmt(tr("Deleting '%s'"), llFilesToDelete.front().c_str()).raw());
5417 if (FAILED(rc))
5418 return rc;
5419
5420 /* create and start the task on a separate thread (note that it will not
5421 * start working until we release alock) */
5422 DeleteConfigTask *pTask = new DeleteConfigTask(this, pProgress, "DeleteVM", llMediums, llFilesToDelete);
5423 rc = pTask->createThread();
5424 if (FAILED(rc))
5425 return rc;
5426
5427 pProgress.queryInterfaceTo(aProgress.asOutParam());
5428
5429 LogFlowFuncLeave();
5430
5431 return S_OK;
5432}
5433
5434HRESULT Machine::findSnapshot(const com::Utf8Str &aNameOrId, ComPtr<ISnapshot> &aSnapshot)
5435{
5436 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
5437
5438 ComObjPtr<Snapshot> pSnapshot;
5439 HRESULT rc;
5440
5441 if (aNameOrId.isEmpty())
5442 // null case (caller wants root snapshot): i_findSnapshotById() handles this
5443 rc = i_findSnapshotById(Guid(), pSnapshot, true /* aSetError */);
5444 else
5445 {
5446 Guid uuid(aNameOrId);
5447 if (uuid.isValid())
5448 rc = i_findSnapshotById(uuid, pSnapshot, true /* aSetError */);
5449 else
5450 rc = i_findSnapshotByName(aNameOrId, pSnapshot, true /* aSetError */);
5451 }
5452 pSnapshot.queryInterfaceTo(aSnapshot.asOutParam());
5453
5454 return rc;
5455}
5456
5457HRESULT Machine::createSharedFolder(const com::Utf8Str &aName, const com::Utf8Str &aHostPath, BOOL aWritable, BOOL aAutomount)
5458{
5459 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
5460
5461 HRESULT rc = i_checkStateDependency(MutableOrRunningStateDep);
5462 if (FAILED(rc)) return rc;
5463
5464 ComObjPtr<SharedFolder> sharedFolder;
5465 rc = i_findSharedFolder(aName, sharedFolder, false /* aSetError */);
5466 if (SUCCEEDED(rc))
5467 return setError(VBOX_E_OBJECT_IN_USE,
5468 tr("Shared folder named '%s' already exists"),
5469 aName.c_str());
5470
5471 sharedFolder.createObject();
5472 rc = sharedFolder->init(i_getMachine(),
5473 aName,
5474 aHostPath,
5475 !!aWritable,
5476 !!aAutomount,
5477 true /* fFailOnError */);
5478 if (FAILED(rc)) return rc;
5479
5480 i_setModified(IsModified_SharedFolders);
5481 mHWData.backup();
5482 mHWData->mSharedFolders.push_back(sharedFolder);
5483
5484 /* inform the direct session if any */
5485 alock.release();
5486 i_onSharedFolderChange();
5487
5488 return S_OK;
5489}
5490
5491HRESULT Machine::removeSharedFolder(const com::Utf8Str &aName)
5492{
5493 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
5494
5495 HRESULT rc = i_checkStateDependency(MutableOrRunningStateDep);
5496 if (FAILED(rc)) return rc;
5497
5498 ComObjPtr<SharedFolder> sharedFolder;
5499 rc = i_findSharedFolder(aName, sharedFolder, true /* aSetError */);
5500 if (FAILED(rc)) return rc;
5501
5502 i_setModified(IsModified_SharedFolders);
5503 mHWData.backup();
5504 mHWData->mSharedFolders.remove(sharedFolder);
5505
5506 /* inform the direct session if any */
5507 alock.release();
5508 i_onSharedFolderChange();
5509
5510 return S_OK;
5511}
5512
5513HRESULT Machine::canShowConsoleWindow(BOOL *aCanShow)
5514{
5515 /* start with No */
5516 *aCanShow = FALSE;
5517
5518 ComPtr<IInternalSessionControl> directControl;
5519 {
5520 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
5521
5522 if (mData->mSession.mState != SessionState_Locked)
5523 return setError(VBOX_E_INVALID_VM_STATE,
5524 tr("Machine is not locked for session (session state: %s)"),
5525 Global::stringifySessionState(mData->mSession.mState));
5526
5527 if (mData->mSession.mLockType == LockType_VM)
5528 directControl = mData->mSession.mDirectControl;
5529 }
5530
5531 /* ignore calls made after #OnSessionEnd() is called */
5532 if (!directControl)
5533 return S_OK;
5534
5535 LONG64 dummy;
5536 return directControl->OnShowWindow(TRUE /* aCheck */, aCanShow, &dummy);
5537}
5538
5539HRESULT Machine::showConsoleWindow(LONG64 *aWinId)
5540{
5541 ComPtr<IInternalSessionControl> directControl;
5542 {
5543 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
5544
5545 if (mData->mSession.mState != SessionState_Locked)
5546 return setError(E_FAIL,
5547 tr("Machine is not locked for session (session state: %s)"),
5548 Global::stringifySessionState(mData->mSession.mState));
5549
5550 if (mData->mSession.mLockType == LockType_VM)
5551 directControl = mData->mSession.mDirectControl;
5552 }
5553
5554 /* ignore calls made after #OnSessionEnd() is called */
5555 if (!directControl)
5556 return S_OK;
5557
5558 BOOL dummy;
5559 return directControl->OnShowWindow(FALSE /* aCheck */, &dummy, aWinId);
5560}
5561
5562#ifdef VBOX_WITH_GUEST_PROPS
5563/**
5564 * Look up a guest property in VBoxSVC's internal structures.
5565 */
5566HRESULT Machine::i_getGuestPropertyFromService(const com::Utf8Str &aName,
5567 com::Utf8Str &aValue,
5568 LONG64 *aTimestamp,
5569 com::Utf8Str &aFlags) const
5570{
5571 using namespace guestProp;
5572
5573 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
5574 HWData::GuestPropertyMap::const_iterator it = mHWData->mGuestProperties.find(aName);
5575
5576 if (it != mHWData->mGuestProperties.end())
5577 {
5578 char szFlags[MAX_FLAGS_LEN + 1];
5579 aValue = it->second.strValue;
5580 *aTimestamp = it->second.mTimestamp;
5581 writeFlags(it->second.mFlags, szFlags);
5582 aFlags = Utf8Str(szFlags);
5583 }
5584
5585 return S_OK;
5586}
5587
5588/**
5589 * Query the VM that a guest property belongs to for the property.
5590 * @returns E_ACCESSDENIED if the VM process is not available or not
5591 * currently handling queries and the lookup should then be done in
5592 * VBoxSVC.
5593 */
5594HRESULT Machine::i_getGuestPropertyFromVM(const com::Utf8Str &aName,
5595 com::Utf8Str &aValue,
5596 LONG64 *aTimestamp,
5597 com::Utf8Str &aFlags) const
5598{
5599 HRESULT rc = S_OK;
5600 BSTR bValue = NULL;
5601 BSTR bFlags = NULL;
5602
5603 ComPtr<IInternalSessionControl> directControl;
5604 {
5605 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
5606 if (mData->mSession.mLockType == LockType_VM)
5607 directControl = mData->mSession.mDirectControl;
5608 }
5609
5610 /* ignore calls made after #OnSessionEnd() is called */
5611 if (!directControl)
5612 rc = E_ACCESSDENIED;
5613 else
5614 rc = directControl->AccessGuestProperty(Bstr(aName).raw(), Bstr("").raw(), Bstr("").raw(),
5615 0 /* accessMode */,
5616 &bValue, aTimestamp, &bFlags);
5617
5618 aValue = bValue;
5619 aFlags = bFlags;
5620
5621 return rc;
5622}
5623#endif // VBOX_WITH_GUEST_PROPS
5624
5625HRESULT Machine::getGuestProperty(const com::Utf8Str &aName,
5626 com::Utf8Str &aValue,
5627 LONG64 *aTimestamp,
5628 com::Utf8Str &aFlags)
5629{
5630#ifndef VBOX_WITH_GUEST_PROPS
5631 ReturnComNotImplemented();
5632#else // VBOX_WITH_GUEST_PROPS
5633
5634 HRESULT rc = i_getGuestPropertyFromVM(aName, aValue, aTimestamp, aFlags);
5635
5636 if (rc == E_ACCESSDENIED)
5637 /* The VM is not running or the service is not (yet) accessible */
5638 rc = i_getGuestPropertyFromService(aName, aValue, aTimestamp, aFlags);
5639 return rc;
5640#endif // VBOX_WITH_GUEST_PROPS
5641}
5642
5643HRESULT Machine::getGuestPropertyValue(const com::Utf8Str &aProperty, com::Utf8Str &aValue)
5644{
5645 LONG64 dummyTimestamp;
5646 com::Utf8Str dummyFlags;
5647 HRESULT rc = getGuestProperty(aProperty, aValue, &dummyTimestamp, dummyFlags);
5648 return rc;
5649
5650}
5651HRESULT Machine::getGuestPropertyTimestamp(const com::Utf8Str &aProperty, LONG64 *aValue)
5652{
5653 com::Utf8Str dummyFlags;
5654 com::Utf8Str dummyValue;
5655 HRESULT rc = getGuestProperty(aProperty, dummyValue, aValue, dummyFlags);
5656 return rc;
5657}
5658
5659#ifdef VBOX_WITH_GUEST_PROPS
5660/**
5661 * Set a guest property in VBoxSVC's internal structures.
5662 */
5663HRESULT Machine::i_setGuestPropertyToService(const com::Utf8Str &aName, const com::Utf8Str &aValue,
5664 const com::Utf8Str &aFlags, bool fDelete)
5665{
5666 using namespace guestProp;
5667
5668 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
5669 HRESULT rc = S_OK;
5670
5671 rc = i_checkStateDependency(MutableOrSavedStateDep);
5672 if (FAILED(rc)) return rc;
5673
5674 try
5675 {
5676 uint32_t fFlags = NILFLAG;
5677 if (aFlags.length() && RT_FAILURE(validateFlags(aFlags.c_str(), &fFlags)))
5678 return setError(E_INVALIDARG, tr("Invalid guest property flag values: '%s'"), aFlags.c_str());
5679
5680 HWData::GuestPropertyMap::iterator it = mHWData->mGuestProperties.find(aName);
5681 if (it == mHWData->mGuestProperties.end())
5682 {
5683 if (!fDelete)
5684 {
5685 i_setModified(IsModified_MachineData);
5686 mHWData.backupEx();
5687
5688 RTTIMESPEC time;
5689 HWData::GuestProperty prop;
5690 prop.strValue = Bstr(aValue).raw();
5691 prop.mTimestamp = RTTimeSpecGetNano(RTTimeNow(&time));
5692 prop.mFlags = fFlags;
5693 mHWData->mGuestProperties[aName] = prop;
5694 }
5695 }
5696 else
5697 {
5698 if (it->second.mFlags & (RDONLYHOST))
5699 {
5700 rc = setError(E_ACCESSDENIED, tr("The property '%s' cannot be changed by the host"), aName.c_str());
5701 }
5702 else
5703 {
5704 i_setModified(IsModified_MachineData);
5705 mHWData.backupEx();
5706
5707 /* The backupEx() operation invalidates our iterator,
5708 * so get a new one. */
5709 it = mHWData->mGuestProperties.find(aName);
5710 Assert(it != mHWData->mGuestProperties.end());
5711
5712 if (!fDelete)
5713 {
5714 RTTIMESPEC time;
5715 it->second.strValue = aValue;
5716 it->second.mTimestamp = RTTimeSpecGetNano(RTTimeNow(&time));
5717 it->second.mFlags = fFlags;
5718 }
5719 else
5720 mHWData->mGuestProperties.erase(it);
5721 }
5722 }
5723
5724 if (SUCCEEDED(rc))
5725 {
5726 alock.release();
5727
5728 mParent->i_onGuestPropertyChange(mData->mUuid,
5729 Bstr(aName).raw(),
5730 Bstr(aValue).raw(),
5731 Bstr(aFlags).raw());
5732 }
5733 }
5734 catch (std::bad_alloc &)
5735 {
5736 rc = E_OUTOFMEMORY;
5737 }
5738
5739 return rc;
5740}
5741
5742/**
5743 * Set a property on the VM that that property belongs to.
5744 * @returns E_ACCESSDENIED if the VM process is not available or not
5745 * currently handling queries and the setting should then be done in
5746 * VBoxSVC.
5747 */
5748HRESULT Machine::i_setGuestPropertyToVM(const com::Utf8Str &aName, const com::Utf8Str &aValue,
5749 const com::Utf8Str &aFlags, bool fDelete)
5750{
5751 HRESULT rc;
5752
5753 try
5754 {
5755 ComPtr<IInternalSessionControl> directControl;
5756 {
5757 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
5758 if (mData->mSession.mLockType == LockType_VM)
5759 directControl = mData->mSession.mDirectControl;
5760 }
5761
5762 BSTR dummy = NULL; /* will not be changed (setter) */
5763 LONG64 dummy64;
5764 if (!directControl)
5765 rc = E_ACCESSDENIED;
5766 else
5767 /** @todo Fix when adding DeleteGuestProperty(), see defect. */
5768 rc = directControl->AccessGuestProperty(Bstr(aName).raw(), Bstr(aValue).raw(), Bstr(aFlags).raw(),
5769 fDelete? 2: 1 /* accessMode */,
5770 &dummy, &dummy64, &dummy);
5771 }
5772 catch (std::bad_alloc &)
5773 {
5774 rc = E_OUTOFMEMORY;
5775 }
5776
5777 return rc;
5778}
5779#endif // VBOX_WITH_GUEST_PROPS
5780
5781HRESULT Machine::setGuestProperty(const com::Utf8Str &aProperty, const com::Utf8Str &aValue,
5782 const com::Utf8Str &aFlags)
5783{
5784#ifndef VBOX_WITH_GUEST_PROPS
5785 ReturnComNotImplemented();
5786#else // VBOX_WITH_GUEST_PROPS
5787 HRESULT rc = i_setGuestPropertyToVM(aProperty, aValue, aFlags, /* fDelete = */ false);
5788 if (rc == E_ACCESSDENIED)
5789 /* The VM is not running or the service is not (yet) accessible */
5790 rc = i_setGuestPropertyToService(aProperty, aValue, aFlags, /* fDelete = */ false);
5791 return rc;
5792#endif // VBOX_WITH_GUEST_PROPS
5793}
5794
5795HRESULT Machine::setGuestPropertyValue(const com::Utf8Str &aProperty, const com::Utf8Str &aValue)
5796{
5797 return setGuestProperty(aProperty, aValue, "");
5798}
5799
5800HRESULT Machine::deleteGuestProperty(const com::Utf8Str &aName)
5801{
5802#ifndef VBOX_WITH_GUEST_PROPS
5803 ReturnComNotImplemented();
5804#else // VBOX_WITH_GUEST_PROPS
5805 HRESULT rc = i_setGuestPropertyToVM(aName, "", "", /* fDelete = */ true);
5806 if (rc == E_ACCESSDENIED)
5807 /* The VM is not running or the service is not (yet) accessible */
5808 rc = i_setGuestPropertyToService(aName, "", "", /* fDelete = */ true);
5809 return rc;
5810#endif // VBOX_WITH_GUEST_PROPS
5811}
5812
5813#ifdef VBOX_WITH_GUEST_PROPS
5814/**
5815 * Enumerate the guest properties in VBoxSVC's internal structures.
5816 */
5817HRESULT Machine::i_enumerateGuestPropertiesInService(const com::Utf8Str &aPatterns,
5818 std::vector<com::Utf8Str> &aNames,
5819 std::vector<com::Utf8Str> &aValues,
5820 std::vector<LONG64> &aTimestamps,
5821 std::vector<com::Utf8Str> &aFlags)
5822{
5823 using namespace guestProp;
5824
5825 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
5826 Utf8Str strPatterns(aPatterns);
5827
5828 HWData::GuestPropertyMap propMap;
5829
5830 /*
5831 * Look for matching patterns and build up a list.
5832 */
5833 HWData::GuestPropertyMap::const_iterator it = mHWData->mGuestProperties.begin();
5834 while (it != mHWData->mGuestProperties.end())
5835 {
5836 if ( strPatterns.isEmpty()
5837 || RTStrSimplePatternMultiMatch(strPatterns.c_str(),
5838 RTSTR_MAX,
5839 it->first.c_str(),
5840 RTSTR_MAX,
5841 NULL)
5842 )
5843 propMap.insert(*it);
5844 ++it;
5845 }
5846
5847 alock.release();
5848
5849 /*
5850 * And build up the arrays for returning the property information.
5851 */
5852 size_t cEntries = propMap.size();
5853
5854 aNames.resize(cEntries);
5855 aValues.resize(cEntries);
5856 aTimestamps.resize(cEntries);
5857 aFlags.resize(cEntries);
5858
5859 char szFlags[MAX_FLAGS_LEN + 1];
5860 size_t i= 0;
5861 for (it = propMap.begin(); it != propMap.end(); ++i, ++it)
5862 {
5863 aNames[i] = it->first;
5864 aValues[i] = it->second.strValue;
5865 aTimestamps[i] = it->second.mTimestamp;
5866 writeFlags(it->second.mFlags, szFlags);
5867 aFlags[i] = Utf8Str(szFlags);
5868 }
5869
5870 return S_OK;
5871}
5872
5873/**
5874 * Enumerate the properties managed by a VM.
5875 * @returns E_ACCESSDENIED if the VM process is not available or not
5876 * currently handling queries and the setting should then be done in
5877 * VBoxSVC.
5878 */
5879HRESULT Machine::i_enumerateGuestPropertiesOnVM(const com::Utf8Str &aPatterns,
5880 std::vector<com::Utf8Str> &aNames,
5881 std::vector<com::Utf8Str> &aValues,
5882 std::vector<LONG64> &aTimestamps,
5883 std::vector<com::Utf8Str> &aFlags)
5884{
5885 HRESULT rc;
5886 ComPtr<IInternalSessionControl> directControl;
5887 {
5888 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
5889 if (mData->mSession.mLockType == LockType_VM)
5890 directControl = mData->mSession.mDirectControl;
5891 }
5892
5893 com::SafeArray<BSTR> bNames;
5894 com::SafeArray<BSTR> bValues;
5895 com::SafeArray<LONG64> bTimestamps;
5896 com::SafeArray<BSTR> bFlags;
5897
5898 if (!directControl)
5899 rc = E_ACCESSDENIED;
5900 else
5901 rc = directControl->EnumerateGuestProperties(Bstr(aPatterns).raw(),
5902 ComSafeArrayAsOutParam(bNames),
5903 ComSafeArrayAsOutParam(bValues),
5904 ComSafeArrayAsOutParam(bTimestamps),
5905 ComSafeArrayAsOutParam(bFlags));
5906 size_t i;
5907 aNames.resize(bNames.size());
5908 for (i = 0; i < bNames.size(); ++i)
5909 aNames[i] = Utf8Str(bNames[i]);
5910 aValues.resize(bValues.size());
5911 for (i = 0; i < bValues.size(); ++i)
5912 aValues[i] = Utf8Str(bValues[i]);
5913 aTimestamps.resize(bTimestamps.size());
5914 for (i = 0; i < bTimestamps.size(); ++i)
5915 aTimestamps[i] = bTimestamps[i];
5916 aFlags.resize(bFlags.size());
5917 for (i = 0; i < bFlags.size(); ++i)
5918 aFlags[i] = Utf8Str(bFlags[i]);
5919
5920 return rc;
5921}
5922#endif // VBOX_WITH_GUEST_PROPS
5923HRESULT Machine::enumerateGuestProperties(const com::Utf8Str &aPatterns,
5924 std::vector<com::Utf8Str> &aNames,
5925 std::vector<com::Utf8Str> &aValues,
5926 std::vector<LONG64> &aTimestamps,
5927 std::vector<com::Utf8Str> &aFlags)
5928{
5929#ifndef VBOX_WITH_GUEST_PROPS
5930 ReturnComNotImplemented();
5931#else // VBOX_WITH_GUEST_PROPS
5932
5933 HRESULT rc = i_enumerateGuestPropertiesOnVM(aPatterns, aNames, aValues, aTimestamps, aFlags);
5934
5935 if (rc == E_ACCESSDENIED)
5936 /* The VM is not running or the service is not (yet) accessible */
5937 rc = i_enumerateGuestPropertiesInService(aPatterns, aNames, aValues, aTimestamps, aFlags);
5938 return rc;
5939#endif // VBOX_WITH_GUEST_PROPS
5940}
5941
5942HRESULT Machine::getMediumAttachmentsOfController(const com::Utf8Str &aName,
5943 std::vector<ComPtr<IMediumAttachment> > &aMediumAttachments)
5944{
5945 MediaData::AttachmentList atts;
5946
5947 HRESULT rc = i_getMediumAttachmentsOfController(aName, atts);
5948 if (FAILED(rc)) return rc;
5949
5950 size_t i = 0;
5951 aMediumAttachments.resize(atts.size());
5952 for (MediaData::AttachmentList::iterator it = atts.begin(); it != atts.end(); ++it, ++i)
5953 (*it).queryInterfaceTo(aMediumAttachments[i].asOutParam());
5954
5955 return S_OK;
5956}
5957
5958HRESULT Machine::getMediumAttachment(const com::Utf8Str &aName,
5959 LONG aControllerPort,
5960 LONG aDevice,
5961 ComPtr<IMediumAttachment> &aAttachment)
5962{
5963 LogFlowThisFunc(("aControllerName=\"%s\" aControllerPort=%d aDevice=%d\n",
5964 aName.c_str(), aControllerPort, aDevice));
5965
5966 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
5967
5968 aAttachment = NULL;
5969
5970 ComObjPtr<MediumAttachment> pAttach = i_findAttachment(mMediaData->mAttachments,
5971 Bstr(aName).raw(),
5972 aControllerPort,
5973 aDevice);
5974 if (pAttach.isNull())
5975 return setError(VBOX_E_OBJECT_NOT_FOUND,
5976 tr("No storage device attached to device slot %d on port %d of controller '%s'"),
5977 aDevice, aControllerPort, aName.c_str());
5978
5979 pAttach.queryInterfaceTo(aAttachment.asOutParam());
5980
5981 return S_OK;
5982}
5983
5984
5985HRESULT Machine::addStorageController(const com::Utf8Str &aName,
5986 StorageBus_T aConnectionType,
5987 ComPtr<IStorageController> &aController)
5988{
5989 if ( (aConnectionType <= StorageBus_Null)
5990 || (aConnectionType > StorageBus_USB))
5991 return setError(E_INVALIDARG,
5992 tr("Invalid connection type: %d"),
5993 aConnectionType);
5994
5995 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
5996
5997 HRESULT rc = i_checkStateDependency(MutableStateDep);
5998 if (FAILED(rc)) return rc;
5999
6000 /* try to find one with the name first. */
6001 ComObjPtr<StorageController> ctrl;
6002
6003 rc = i_getStorageControllerByName(aName, ctrl, false /* aSetError */);
6004 if (SUCCEEDED(rc))
6005 return setError(VBOX_E_OBJECT_IN_USE,
6006 tr("Storage controller named '%s' already exists"),
6007 aName.c_str());
6008
6009 ctrl.createObject();
6010
6011 /* get a new instance number for the storage controller */
6012 ULONG ulInstance = 0;
6013 bool fBootable = true;
6014 for (StorageControllerList::const_iterator it = mStorageControllers->begin();
6015 it != mStorageControllers->end();
6016 ++it)
6017 {
6018 if ((*it)->i_getStorageBus() == aConnectionType)
6019 {
6020 ULONG ulCurInst = (*it)->i_getInstance();
6021
6022 if (ulCurInst >= ulInstance)
6023 ulInstance = ulCurInst + 1;
6024
6025 /* Only one controller of each type can be marked as bootable. */
6026 if ((*it)->i_getBootable())
6027 fBootable = false;
6028 }
6029 }
6030
6031 rc = ctrl->init(this, aName, aConnectionType, ulInstance, fBootable);
6032 if (FAILED(rc)) return rc;
6033
6034 i_setModified(IsModified_Storage);
6035 mStorageControllers.backup();
6036 mStorageControllers->push_back(ctrl);
6037
6038 ctrl.queryInterfaceTo(aController.asOutParam());
6039
6040 /* inform the direct session if any */
6041 alock.release();
6042 i_onStorageControllerChange();
6043
6044 return S_OK;
6045}
6046
6047HRESULT Machine::getStorageControllerByName(const com::Utf8Str &aName,
6048 ComPtr<IStorageController> &aStorageController)
6049{
6050 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
6051
6052 ComObjPtr<StorageController> ctrl;
6053
6054 HRESULT rc = i_getStorageControllerByName(aName, ctrl, true /* aSetError */);
6055 if (SUCCEEDED(rc))
6056 ctrl.queryInterfaceTo(aStorageController.asOutParam());
6057
6058 return rc;
6059}
6060
6061HRESULT Machine::getStorageControllerByInstance(StorageBus_T aConnectionType,
6062 ULONG aInstance,
6063 ComPtr<IStorageController> &aStorageController)
6064{
6065 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
6066
6067 for (StorageControllerList::const_iterator it = mStorageControllers->begin();
6068 it != mStorageControllers->end();
6069 ++it)
6070 {
6071 if ( (*it)->i_getStorageBus() == aConnectionType
6072 && (*it)->i_getInstance() == aInstance)
6073 {
6074 (*it).queryInterfaceTo(aStorageController.asOutParam());
6075 return S_OK;
6076 }
6077 }
6078
6079 return setError(VBOX_E_OBJECT_NOT_FOUND,
6080 tr("Could not find a storage controller with instance number '%lu'"),
6081 aInstance);
6082}
6083
6084HRESULT Machine::setStorageControllerBootable(const com::Utf8Str &aName, BOOL aBootable)
6085{
6086 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
6087
6088 HRESULT rc = i_checkStateDependency(MutableStateDep);
6089 if (FAILED(rc)) return rc;
6090
6091 ComObjPtr<StorageController> ctrl;
6092
6093 rc = i_getStorageControllerByName(aName, ctrl, true /* aSetError */);
6094 if (SUCCEEDED(rc))
6095 {
6096 /* Ensure that only one controller of each type is marked as bootable. */
6097 if (aBootable == TRUE)
6098 {
6099 for (StorageControllerList::const_iterator it = mStorageControllers->begin();
6100 it != mStorageControllers->end();
6101 ++it)
6102 {
6103 ComObjPtr<StorageController> aCtrl = (*it);
6104
6105 if ( (aCtrl->i_getName() != aName)
6106 && aCtrl->i_getBootable() == TRUE
6107 && aCtrl->i_getStorageBus() == ctrl->i_getStorageBus()
6108 && aCtrl->i_getControllerType() == ctrl->i_getControllerType())
6109 {
6110 aCtrl->i_setBootable(FALSE);
6111 break;
6112 }
6113 }
6114 }
6115
6116 if (SUCCEEDED(rc))
6117 {
6118 ctrl->i_setBootable(aBootable);
6119 i_setModified(IsModified_Storage);
6120 }
6121 }
6122
6123 if (SUCCEEDED(rc))
6124 {
6125 /* inform the direct session if any */
6126 alock.release();
6127 i_onStorageControllerChange();
6128 }
6129
6130 return rc;
6131}
6132
6133HRESULT Machine::removeStorageController(const com::Utf8Str &aName)
6134{
6135 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
6136
6137 HRESULT rc = i_checkStateDependency(MutableStateDep);
6138 if (FAILED(rc)) return rc;
6139
6140 ComObjPtr<StorageController> ctrl;
6141 rc = i_getStorageControllerByName(aName, ctrl, true /* aSetError */);
6142 if (FAILED(rc)) return rc;
6143
6144 {
6145 /* find all attached devices to the appropriate storage controller and detach them all */
6146 // make a temporary list because detachDevice invalidates iterators into
6147 // mMediaData->mAttachments
6148 MediaData::AttachmentList llAttachments2 = mMediaData->mAttachments;
6149
6150 for (MediaData::AttachmentList::iterator it = llAttachments2.begin();
6151 it != llAttachments2.end();
6152 ++it)
6153 {
6154 MediumAttachment *pAttachTemp = *it;
6155
6156 AutoCaller localAutoCaller(pAttachTemp);
6157 if (FAILED(localAutoCaller.rc())) return localAutoCaller.rc();
6158
6159 AutoReadLock local_alock(pAttachTemp COMMA_LOCKVAL_SRC_POS);
6160
6161 if (pAttachTemp->i_getControllerName() == aName)
6162 {
6163 rc = i_detachDevice(pAttachTemp, alock, NULL);
6164 if (FAILED(rc)) return rc;
6165 }
6166 }
6167 }
6168
6169 /* We can remove it now. */
6170 i_setModified(IsModified_Storage);
6171 mStorageControllers.backup();
6172
6173 ctrl->i_unshare();
6174
6175 mStorageControllers->remove(ctrl);
6176
6177 /* inform the direct session if any */
6178 alock.release();
6179 i_onStorageControllerChange();
6180
6181 return S_OK;
6182}
6183
6184HRESULT Machine::addUSBController(const com::Utf8Str &aName, USBControllerType_T aType,
6185 ComPtr<IUSBController> &aController)
6186{
6187 if ( (aType <= USBControllerType_Null)
6188 || (aType >= USBControllerType_Last))
6189 return setError(E_INVALIDARG,
6190 tr("Invalid USB controller type: %d"),
6191 aType);
6192
6193 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
6194
6195 HRESULT rc = i_checkStateDependency(MutableStateDep);
6196 if (FAILED(rc)) return rc;
6197
6198 /* try to find one with the same type first. */
6199 ComObjPtr<USBController> ctrl;
6200
6201 rc = i_getUSBControllerByName(aName, ctrl, false /* aSetError */);
6202 if (SUCCEEDED(rc))
6203 return setError(VBOX_E_OBJECT_IN_USE,
6204 tr("USB controller named '%s' already exists"),
6205 aName.c_str());
6206
6207 /* Check that we don't exceed the maximum number of USB controllers for the given type. */
6208 ULONG maxInstances;
6209 rc = mParent->i_getSystemProperties()->GetMaxInstancesOfUSBControllerType(mHWData->mChipsetType, aType, &maxInstances);
6210 if (FAILED(rc))
6211 return rc;
6212
6213 ULONG cInstances = i_getUSBControllerCountByType(aType);
6214 if (cInstances >= maxInstances)
6215 return setError(E_INVALIDARG,
6216 tr("Too many USB controllers of this type"));
6217
6218 ctrl.createObject();
6219
6220 rc = ctrl->init(this, aName, aType);
6221 if (FAILED(rc)) return rc;
6222
6223 i_setModified(IsModified_USB);
6224 mUSBControllers.backup();
6225 mUSBControllers->push_back(ctrl);
6226
6227 ctrl.queryInterfaceTo(aController.asOutParam());
6228
6229 /* inform the direct session if any */
6230 alock.release();
6231 i_onUSBControllerChange();
6232
6233 return S_OK;
6234}
6235
6236HRESULT Machine::getUSBControllerByName(const com::Utf8Str &aName, ComPtr<IUSBController> &aController)
6237{
6238 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
6239
6240 ComObjPtr<USBController> ctrl;
6241
6242 HRESULT rc = i_getUSBControllerByName(aName, ctrl, true /* aSetError */);
6243 if (SUCCEEDED(rc))
6244 ctrl.queryInterfaceTo(aController.asOutParam());
6245
6246 return rc;
6247}
6248
6249HRESULT Machine::getUSBControllerCountByType(USBControllerType_T aType,
6250 ULONG *aControllers)
6251{
6252 if ( (aType <= USBControllerType_Null)
6253 || (aType >= USBControllerType_Last))
6254 return setError(E_INVALIDARG,
6255 tr("Invalid USB controller type: %d"),
6256 aType);
6257
6258 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
6259
6260 ComObjPtr<USBController> ctrl;
6261
6262 *aControllers = i_getUSBControllerCountByType(aType);
6263
6264 return S_OK;
6265}
6266
6267HRESULT Machine::removeUSBController(const com::Utf8Str &aName)
6268{
6269
6270 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
6271
6272 HRESULT rc = i_checkStateDependency(MutableStateDep);
6273 if (FAILED(rc)) return rc;
6274
6275 ComObjPtr<USBController> ctrl;
6276 rc = i_getUSBControllerByName(aName, ctrl, true /* aSetError */);
6277 if (FAILED(rc)) return rc;
6278
6279 i_setModified(IsModified_USB);
6280 mUSBControllers.backup();
6281
6282 ctrl->i_unshare();
6283
6284 mUSBControllers->remove(ctrl);
6285
6286 /* inform the direct session if any */
6287 alock.release();
6288 i_onUSBControllerChange();
6289
6290 return S_OK;
6291}
6292
6293HRESULT Machine::querySavedGuestScreenInfo(ULONG aScreenId,
6294 ULONG *aOriginX,
6295 ULONG *aOriginY,
6296 ULONG *aWidth,
6297 ULONG *aHeight,
6298 BOOL *aEnabled)
6299{
6300 uint32_t u32OriginX= 0;
6301 uint32_t u32OriginY= 0;
6302 uint32_t u32Width = 0;
6303 uint32_t u32Height = 0;
6304 uint16_t u16Flags = 0;
6305
6306 int vrc = readSavedGuestScreenInfo(mSSData->strStateFilePath, aScreenId,
6307 &u32OriginX, &u32OriginY, &u32Width, &u32Height, &u16Flags);
6308 if (RT_FAILURE(vrc))
6309 {
6310#ifdef RT_OS_WINDOWS
6311 /* HACK: GUI sets *pfEnabled to 'true' and expects it to stay so if the API fails.
6312 * This works with XPCOM. But Windows COM sets all output parameters to zero.
6313 * So just assign fEnable to TRUE again.
6314 * The right fix would be to change GUI API wrappers to make sure that parameters
6315 * are changed only if API succeeds.
6316 */
6317 *aEnabled = TRUE;
6318#endif
6319 return setError(VBOX_E_IPRT_ERROR,
6320 tr("Saved guest size is not available (%Rrc)"),
6321 vrc);
6322 }
6323
6324 *aOriginX = u32OriginX;
6325 *aOriginY = u32OriginY;
6326 *aWidth = u32Width;
6327 *aHeight = u32Height;
6328 *aEnabled = (u16Flags & VBVA_SCREEN_F_DISABLED) == 0;
6329
6330 return S_OK;
6331}
6332
6333HRESULT Machine::readSavedThumbnailToArray(ULONG aScreenId, BitmapFormat_T aBitmapFormat,
6334 ULONG *aWidth, ULONG *aHeight, std::vector<BYTE> &aData)
6335{
6336 if (aScreenId != 0)
6337 return E_NOTIMPL;
6338
6339 if ( aBitmapFormat != BitmapFormat_BGR0
6340 && aBitmapFormat != BitmapFormat_BGRA
6341 && aBitmapFormat != BitmapFormat_RGBA
6342 && aBitmapFormat != BitmapFormat_PNG)
6343 return setError(E_NOTIMPL,
6344 tr("Unsupported saved thumbnail format 0x%08X"), aBitmapFormat);
6345
6346 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
6347
6348 uint8_t *pu8Data = NULL;
6349 uint32_t cbData = 0;
6350 uint32_t u32Width = 0;
6351 uint32_t u32Height = 0;
6352
6353 int vrc = readSavedDisplayScreenshot(mSSData->strStateFilePath, 0 /* u32Type */, &pu8Data, &cbData, &u32Width, &u32Height);
6354
6355 if (RT_FAILURE(vrc))
6356 return setError(VBOX_E_IPRT_ERROR,
6357 tr("Saved thumbnail data is not available (%Rrc)"),
6358 vrc);
6359
6360 HRESULT hr = S_OK;
6361
6362 *aWidth = u32Width;
6363 *aHeight = u32Height;
6364
6365 if (cbData > 0)
6366 {
6367 /* Convert pixels to the format expected by the API caller. */
6368 if (aBitmapFormat == BitmapFormat_BGR0)
6369 {
6370 /* [0] B, [1] G, [2] R, [3] 0. */
6371 aData.resize(cbData);
6372 memcpy(&aData.front(), pu8Data, cbData);
6373 }
6374 else if (aBitmapFormat == BitmapFormat_BGRA)
6375 {
6376 /* [0] B, [1] G, [2] R, [3] A. */
6377 aData.resize(cbData);
6378 for (uint32_t i = 0; i < cbData; i += 4)
6379 {
6380 aData[i] = pu8Data[i];
6381 aData[i + 1] = pu8Data[i + 1];
6382 aData[i + 2] = pu8Data[i + 2];
6383 aData[i + 3] = 0xff;
6384 }
6385 }
6386 else if (aBitmapFormat == BitmapFormat_RGBA)
6387 {
6388 /* [0] R, [1] G, [2] B, [3] A. */
6389 aData.resize(cbData);
6390 for (uint32_t i = 0; i < cbData; i += 4)
6391 {
6392 aData[i] = pu8Data[i + 2];
6393 aData[i + 1] = pu8Data[i + 1];
6394 aData[i + 2] = pu8Data[i];
6395 aData[i + 3] = 0xff;
6396 }
6397 }
6398 else if (aBitmapFormat == BitmapFormat_PNG)
6399 {
6400 uint8_t *pu8PNG = NULL;
6401 uint32_t cbPNG = 0;
6402 uint32_t cxPNG = 0;
6403 uint32_t cyPNG = 0;
6404
6405 vrc = DisplayMakePNG(pu8Data, u32Width, u32Height, &pu8PNG, &cbPNG, &cxPNG, &cyPNG, 0);
6406
6407 if (RT_SUCCESS(vrc))
6408 {
6409 aData.resize(cbPNG);
6410 if (cbPNG)
6411 memcpy(&aData.front(), pu8PNG, cbPNG);
6412 }
6413 else
6414 hr = setError(VBOX_E_IPRT_ERROR,
6415 tr("Could not convert saved thumbnail to PNG (%Rrc)"),
6416 vrc);
6417
6418 RTMemFree(pu8PNG);
6419 }
6420 }
6421
6422 freeSavedDisplayScreenshot(pu8Data);
6423
6424 return hr;
6425}
6426
6427HRESULT Machine::querySavedScreenshotInfo(ULONG aScreenId,
6428 ULONG *aWidth,
6429 ULONG *aHeight,
6430 std::vector<BitmapFormat_T> &aBitmapFormats)
6431{
6432 if (aScreenId != 0)
6433 return E_NOTIMPL;
6434
6435 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
6436
6437 uint8_t *pu8Data = NULL;
6438 uint32_t cbData = 0;
6439 uint32_t u32Width = 0;
6440 uint32_t u32Height = 0;
6441
6442 int vrc = readSavedDisplayScreenshot(mSSData->strStateFilePath, 1 /* u32Type */, &pu8Data, &cbData, &u32Width, &u32Height);
6443
6444 if (RT_FAILURE(vrc))
6445 return setError(VBOX_E_IPRT_ERROR,
6446 tr("Saved screenshot data is not available (%Rrc)"),
6447 vrc);
6448
6449 *aWidth = u32Width;
6450 *aHeight = u32Height;
6451 aBitmapFormats.resize(1);
6452 aBitmapFormats[0] = BitmapFormat_PNG;
6453
6454 freeSavedDisplayScreenshot(pu8Data);
6455
6456 return S_OK;
6457}
6458
6459HRESULT Machine::readSavedScreenshotToArray(ULONG aScreenId,
6460 BitmapFormat_T aBitmapFormat,
6461 ULONG *aWidth,
6462 ULONG *aHeight,
6463 std::vector<BYTE> &aData)
6464{
6465 if (aScreenId != 0)
6466 return E_NOTIMPL;
6467
6468 if (aBitmapFormat != BitmapFormat_PNG)
6469 return E_NOTIMPL;
6470
6471 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
6472
6473 uint8_t *pu8Data = NULL;
6474 uint32_t cbData = 0;
6475 uint32_t u32Width = 0;
6476 uint32_t u32Height = 0;
6477
6478 int vrc = readSavedDisplayScreenshot(mSSData->strStateFilePath, 1 /* u32Type */, &pu8Data, &cbData, &u32Width, &u32Height);
6479
6480 if (RT_FAILURE(vrc))
6481 return setError(VBOX_E_IPRT_ERROR,
6482 tr("Saved screenshot thumbnail data is not available (%Rrc)"),
6483 vrc);
6484
6485 *aWidth = u32Width;
6486 *aHeight = u32Height;
6487
6488 aData.resize(cbData);
6489 if (cbData)
6490 memcpy(&aData.front(), pu8Data, cbData);
6491
6492 freeSavedDisplayScreenshot(pu8Data);
6493
6494 return S_OK;
6495}
6496
6497HRESULT Machine::hotPlugCPU(ULONG aCpu)
6498{
6499 HRESULT rc = S_OK;
6500 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
6501
6502 if (!mHWData->mCPUHotPlugEnabled)
6503 return setError(E_INVALIDARG, tr("CPU hotplug is not enabled"));
6504
6505 if (aCpu >= mHWData->mCPUCount)
6506 return setError(E_INVALIDARG, tr("CPU id exceeds number of possible CPUs [0:%lu]"), mHWData->mCPUCount-1);
6507
6508 if (mHWData->mCPUAttached[aCpu])
6509 return setError(VBOX_E_OBJECT_IN_USE, tr("CPU %lu is already attached"), aCpu);
6510
6511 alock.release();
6512 rc = i_onCPUChange(aCpu, false);
6513 alock.acquire();
6514 if (FAILED(rc)) return rc;
6515
6516 i_setModified(IsModified_MachineData);
6517 mHWData.backup();
6518 mHWData->mCPUAttached[aCpu] = true;
6519
6520 /** Save settings if online - @todo why is this required? -- @bugref{6818} */
6521 if (Global::IsOnline(mData->mMachineState))
6522 i_saveSettings(NULL);
6523
6524 return S_OK;
6525}
6526
6527HRESULT Machine::hotUnplugCPU(ULONG aCpu)
6528{
6529 HRESULT rc = S_OK;
6530
6531 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
6532
6533 if (!mHWData->mCPUHotPlugEnabled)
6534 return setError(E_INVALIDARG, tr("CPU hotplug is not enabled"));
6535
6536 if (aCpu >= SchemaDefs::MaxCPUCount)
6537 return setError(E_INVALIDARG,
6538 tr("CPU index exceeds maximum CPU count (must be in range [0:%lu])"),
6539 SchemaDefs::MaxCPUCount);
6540
6541 if (!mHWData->mCPUAttached[aCpu])
6542 return setError(VBOX_E_OBJECT_NOT_FOUND, tr("CPU %lu is not attached"), aCpu);
6543
6544 /* CPU 0 can't be detached */
6545 if (aCpu == 0)
6546 return setError(E_INVALIDARG, tr("It is not possible to detach CPU 0"));
6547
6548 alock.release();
6549 rc = i_onCPUChange(aCpu, true);
6550 alock.acquire();
6551 if (FAILED(rc)) return rc;
6552
6553 i_setModified(IsModified_MachineData);
6554 mHWData.backup();
6555 mHWData->mCPUAttached[aCpu] = false;
6556
6557 /** Save settings if online - @todo why is this required? -- @bugref{6818} */
6558 if (Global::IsOnline(mData->mMachineState))
6559 i_saveSettings(NULL);
6560
6561 return S_OK;
6562}
6563
6564HRESULT Machine::getCPUStatus(ULONG aCpu, BOOL *aAttached)
6565{
6566 *aAttached = false;
6567
6568 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
6569
6570 /* If hotplug is enabled the CPU is always enabled. */
6571 if (!mHWData->mCPUHotPlugEnabled)
6572 {
6573 if (aCpu < mHWData->mCPUCount)
6574 *aAttached = true;
6575 }
6576 else
6577 {
6578 if (aCpu < SchemaDefs::MaxCPUCount)
6579 *aAttached = mHWData->mCPUAttached[aCpu];
6580 }
6581
6582 return S_OK;
6583}
6584
6585HRESULT Machine::queryLogFilename(ULONG aIdx, com::Utf8Str &aFilename)
6586{
6587 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
6588
6589 Utf8Str log = i_queryLogFilename(aIdx);
6590 if (!RTFileExists(log.c_str()))
6591 log.setNull();
6592 aFilename = log;
6593
6594 return S_OK;
6595}
6596
6597HRESULT Machine::readLog(ULONG aIdx, LONG64 aOffset, LONG64 aSize, std::vector<BYTE> &aData)
6598{
6599 if (aSize < 0)
6600 return setError(E_INVALIDARG, tr("The size argument (%lld) is negative"), aSize);
6601
6602 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
6603
6604 HRESULT rc = S_OK;
6605 Utf8Str log = i_queryLogFilename(aIdx);
6606
6607 /* do not unnecessarily hold the lock while doing something which does
6608 * not need the lock and potentially takes a long time. */
6609 alock.release();
6610
6611 /* Limit the chunk size to 32K for now, as that gives better performance
6612 * over (XP)COM, and keeps the SOAP reply size under 1M for the webservice.
6613 * One byte expands to approx. 25 bytes of breathtaking XML. */
6614 size_t cbData = (size_t)RT_MIN(aSize, 32768);
6615 aData.resize(cbData);
6616
6617 RTFILE LogFile;
6618 int vrc = RTFileOpen(&LogFile, log.c_str(),
6619 RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_DENY_NONE);
6620 if (RT_SUCCESS(vrc))
6621 {
6622 vrc = RTFileReadAt(LogFile, aOffset, cbData? &aData.front(): NULL, cbData, &cbData);
6623 if (RT_SUCCESS(vrc))
6624 aData.resize(cbData);
6625 else
6626 rc = setError(VBOX_E_IPRT_ERROR,
6627 tr("Could not read log file '%s' (%Rrc)"),
6628 log.c_str(), vrc);
6629 RTFileClose(LogFile);
6630 }
6631 else
6632 rc = setError(VBOX_E_IPRT_ERROR,
6633 tr("Could not open log file '%s' (%Rrc)"),
6634 log.c_str(), vrc);
6635
6636 if (FAILED(rc))
6637 aData.resize(0);
6638
6639 return rc;
6640}
6641
6642
6643/**
6644 * Currently this method doesn't attach device to the running VM,
6645 * just makes sure it's plugged on next VM start.
6646 */
6647HRESULT Machine::attachHostPCIDevice(LONG aHostAddress, LONG aDesiredGuestAddress, BOOL /* aTryToUnbind */)
6648{
6649 // lock scope
6650 {
6651 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
6652
6653 HRESULT rc = i_checkStateDependency(MutableStateDep);
6654 if (FAILED(rc)) return rc;
6655
6656 ChipsetType_T aChipset = ChipsetType_PIIX3;
6657 COMGETTER(ChipsetType)(&aChipset);
6658
6659 if (aChipset != ChipsetType_ICH9)
6660 {
6661 return setError(E_INVALIDARG,
6662 tr("Host PCI attachment only supported with ICH9 chipset"));
6663 }
6664
6665 // check if device with this host PCI address already attached
6666 for (HWData::PCIDeviceAssignmentList::iterator it = mHWData->mPCIDeviceAssignments.begin();
6667 it != mHWData->mPCIDeviceAssignments.end();
6668 ++it)
6669 {
6670 LONG iHostAddress = -1;
6671 ComPtr<PCIDeviceAttachment> pAttach;
6672 pAttach = *it;
6673 pAttach->COMGETTER(HostAddress)(&iHostAddress);
6674 if (iHostAddress == aHostAddress)
6675 return setError(E_INVALIDARG,
6676 tr("Device with host PCI address already attached to this VM"));
6677 }
6678
6679 ComObjPtr<PCIDeviceAttachment> pda;
6680 char name[32];
6681
6682 RTStrPrintf(name, sizeof(name), "host%02x:%02x.%x", (aHostAddress>>8) & 0xff,
6683 (aHostAddress & 0xf8) >> 3, aHostAddress & 7);
6684 Bstr bname(name);
6685 pda.createObject();
6686 pda->init(this, bname, aHostAddress, aDesiredGuestAddress, TRUE);
6687 i_setModified(IsModified_MachineData);
6688 mHWData.backup();
6689 mHWData->mPCIDeviceAssignments.push_back(pda);
6690 }
6691
6692 return S_OK;
6693}
6694
6695/**
6696 * Currently this method doesn't detach device from the running VM,
6697 * just makes sure it's not plugged on next VM start.
6698 */
6699HRESULT Machine::detachHostPCIDevice(LONG aHostAddress)
6700{
6701 ComObjPtr<PCIDeviceAttachment> pAttach;
6702 bool fRemoved = false;
6703 HRESULT rc;
6704
6705 // lock scope
6706 {
6707 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
6708
6709 rc = i_checkStateDependency(MutableStateDep);
6710 if (FAILED(rc)) return rc;
6711
6712 for (HWData::PCIDeviceAssignmentList::iterator it = mHWData->mPCIDeviceAssignments.begin();
6713 it != mHWData->mPCIDeviceAssignments.end();
6714 ++it)
6715 {
6716 LONG iHostAddress = -1;
6717 pAttach = *it;
6718 pAttach->COMGETTER(HostAddress)(&iHostAddress);
6719 if (iHostAddress != -1 && iHostAddress == aHostAddress)
6720 {
6721 i_setModified(IsModified_MachineData);
6722 mHWData.backup();
6723 mHWData->mPCIDeviceAssignments.remove(pAttach);
6724 fRemoved = true;
6725 break;
6726 }
6727 }
6728 }
6729
6730
6731 /* Fire event outside of the lock */
6732 if (fRemoved)
6733 {
6734 Assert(!pAttach.isNull());
6735 ComPtr<IEventSource> es;
6736 rc = mParent->COMGETTER(EventSource)(es.asOutParam());
6737 Assert(SUCCEEDED(rc));
6738 Bstr mid;
6739 rc = this->COMGETTER(Id)(mid.asOutParam());
6740 Assert(SUCCEEDED(rc));
6741 fireHostPCIDevicePlugEvent(es, mid.raw(), false /* unplugged */, true /* success */, pAttach, NULL);
6742 }
6743
6744 return fRemoved ? S_OK : setError(VBOX_E_OBJECT_NOT_FOUND,
6745 tr("No host PCI device %08x attached"),
6746 aHostAddress
6747 );
6748}
6749
6750HRESULT Machine::getPCIDeviceAssignments(std::vector<ComPtr<IPCIDeviceAttachment> > &aPCIDeviceAssignments)
6751{
6752 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
6753
6754 aPCIDeviceAssignments.resize(mHWData->mPCIDeviceAssignments.size());
6755
6756 size_t i = 0;
6757 for (std::list<ComObjPtr<PCIDeviceAttachment> >::const_iterator it = mHWData->mPCIDeviceAssignments.begin();
6758 it != mHWData->mPCIDeviceAssignments.end();
6759 ++i, ++it)
6760 (*it).queryInterfaceTo(aPCIDeviceAssignments[i].asOutParam());
6761
6762 return S_OK;
6763}
6764
6765HRESULT Machine::getBandwidthControl(ComPtr<IBandwidthControl> &aBandwidthControl)
6766{
6767 mBandwidthControl.queryInterfaceTo(aBandwidthControl.asOutParam());
6768
6769 return S_OK;
6770}
6771
6772HRESULT Machine::getTracingEnabled(BOOL *aTracingEnabled)
6773{
6774 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
6775
6776 *aTracingEnabled = mHWData->mDebugging.fTracingEnabled;
6777
6778 return S_OK;
6779}
6780
6781HRESULT Machine::setTracingEnabled(BOOL aTracingEnabled)
6782{
6783 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
6784 HRESULT hrc = i_checkStateDependency(MutableStateDep);
6785 if (SUCCEEDED(hrc))
6786 {
6787 hrc = mHWData.backupEx();
6788 if (SUCCEEDED(hrc))
6789 {
6790 i_setModified(IsModified_MachineData);
6791 mHWData->mDebugging.fTracingEnabled = aTracingEnabled != FALSE;
6792 }
6793 }
6794 return hrc;
6795}
6796
6797HRESULT Machine::getTracingConfig(com::Utf8Str &aTracingConfig)
6798{
6799 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
6800 aTracingConfig = mHWData->mDebugging.strTracingConfig;
6801 return S_OK;
6802}
6803
6804HRESULT Machine::setTracingConfig(const com::Utf8Str &aTracingConfig)
6805{
6806 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
6807 HRESULT hrc = i_checkStateDependency(MutableStateDep);
6808 if (SUCCEEDED(hrc))
6809 {
6810 hrc = mHWData.backupEx();
6811 if (SUCCEEDED(hrc))
6812 {
6813 mHWData->mDebugging.strTracingConfig = aTracingConfig;
6814 if (SUCCEEDED(hrc))
6815 i_setModified(IsModified_MachineData);
6816 }
6817 }
6818 return hrc;
6819}
6820
6821HRESULT Machine::getAllowTracingToAccessVM(BOOL *aAllowTracingToAccessVM)
6822{
6823 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
6824
6825 *aAllowTracingToAccessVM = mHWData->mDebugging.fAllowTracingToAccessVM;
6826
6827 return S_OK;
6828}
6829
6830HRESULT Machine::setAllowTracingToAccessVM(BOOL aAllowTracingToAccessVM)
6831{
6832 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
6833 HRESULT hrc = i_checkStateDependency(MutableStateDep);
6834 if (SUCCEEDED(hrc))
6835 {
6836 hrc = mHWData.backupEx();
6837 if (SUCCEEDED(hrc))
6838 {
6839 i_setModified(IsModified_MachineData);
6840 mHWData->mDebugging.fAllowTracingToAccessVM = aAllowTracingToAccessVM != FALSE;
6841 }
6842 }
6843 return hrc;
6844}
6845
6846HRESULT Machine::getAutostartEnabled(BOOL *aAutostartEnabled)
6847{
6848 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
6849
6850 *aAutostartEnabled = mHWData->mAutostart.fAutostartEnabled;
6851
6852 return S_OK;
6853}
6854
6855HRESULT Machine::setAutostartEnabled(BOOL aAutostartEnabled)
6856{
6857 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
6858
6859 HRESULT hrc = i_checkStateDependency(MutableOrSavedOrRunningStateDep);
6860 if ( SUCCEEDED(hrc)
6861 && mHWData->mAutostart.fAutostartEnabled != !!aAutostartEnabled)
6862 {
6863 AutostartDb *autostartDb = mParent->i_getAutostartDb();
6864 int vrc;
6865
6866 if (aAutostartEnabled)
6867 vrc = autostartDb->addAutostartVM(mUserData->s.strName.c_str());
6868 else
6869 vrc = autostartDb->removeAutostartVM(mUserData->s.strName.c_str());
6870
6871 if (RT_SUCCESS(vrc))
6872 {
6873 hrc = mHWData.backupEx();
6874 if (SUCCEEDED(hrc))
6875 {
6876 i_setModified(IsModified_MachineData);
6877 mHWData->mAutostart.fAutostartEnabled = aAutostartEnabled != FALSE;
6878 }
6879 }
6880 else if (vrc == VERR_NOT_SUPPORTED)
6881 hrc = setError(VBOX_E_NOT_SUPPORTED,
6882 tr("The VM autostart feature is not supported on this platform"));
6883 else if (vrc == VERR_PATH_NOT_FOUND)
6884 hrc = setError(E_FAIL,
6885 tr("The path to the autostart database is not set"));
6886 else
6887 hrc = setError(E_UNEXPECTED,
6888 tr("%s machine '%s' to the autostart database failed with %Rrc"),
6889 aAutostartEnabled ? "Adding" : "Removing",
6890 mUserData->s.strName.c_str(), vrc);
6891 }
6892 return hrc;
6893}
6894
6895HRESULT Machine::getAutostartDelay(ULONG *aAutostartDelay)
6896{
6897 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
6898
6899 *aAutostartDelay = mHWData->mAutostart.uAutostartDelay;
6900
6901 return S_OK;
6902}
6903
6904HRESULT Machine::setAutostartDelay(ULONG aAutostartDelay)
6905{
6906 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
6907 HRESULT hrc = i_checkStateDependency(MutableOrSavedOrRunningStateDep);
6908 if (SUCCEEDED(hrc))
6909 {
6910 hrc = mHWData.backupEx();
6911 if (SUCCEEDED(hrc))
6912 {
6913 i_setModified(IsModified_MachineData);
6914 mHWData->mAutostart.uAutostartDelay = aAutostartDelay;
6915 }
6916 }
6917 return hrc;
6918}
6919
6920HRESULT Machine::getAutostopType(AutostopType_T *aAutostopType)
6921{
6922 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
6923
6924 *aAutostopType = mHWData->mAutostart.enmAutostopType;
6925
6926 return S_OK;
6927}
6928
6929HRESULT Machine::setAutostopType(AutostopType_T aAutostopType)
6930{
6931 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
6932 HRESULT hrc = i_checkStateDependency(MutableOrSavedOrRunningStateDep);
6933 if ( SUCCEEDED(hrc)
6934 && mHWData->mAutostart.enmAutostopType != aAutostopType)
6935 {
6936 AutostartDb *autostartDb = mParent->i_getAutostartDb();
6937 int vrc;
6938
6939 if (aAutostopType != AutostopType_Disabled)
6940 vrc = autostartDb->addAutostopVM(mUserData->s.strName.c_str());
6941 else
6942 vrc = autostartDb->removeAutostopVM(mUserData->s.strName.c_str());
6943
6944 if (RT_SUCCESS(vrc))
6945 {
6946 hrc = mHWData.backupEx();
6947 if (SUCCEEDED(hrc))
6948 {
6949 i_setModified(IsModified_MachineData);
6950 mHWData->mAutostart.enmAutostopType = aAutostopType;
6951 }
6952 }
6953 else if (vrc == VERR_NOT_SUPPORTED)
6954 hrc = setError(VBOX_E_NOT_SUPPORTED,
6955 tr("The VM autostop feature is not supported on this platform"));
6956 else if (vrc == VERR_PATH_NOT_FOUND)
6957 hrc = setError(E_FAIL,
6958 tr("The path to the autostart database is not set"));
6959 else
6960 hrc = setError(E_UNEXPECTED,
6961 tr("%s machine '%s' to the autostop database failed with %Rrc"),
6962 aAutostopType != AutostopType_Disabled ? "Adding" : "Removing",
6963 mUserData->s.strName.c_str(), vrc);
6964 }
6965 return hrc;
6966}
6967
6968HRESULT Machine::getDefaultFrontend(com::Utf8Str &aDefaultFrontend)
6969{
6970 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
6971
6972 aDefaultFrontend = mHWData->mDefaultFrontend;
6973
6974 return S_OK;
6975}
6976
6977HRESULT Machine::setDefaultFrontend(const com::Utf8Str &aDefaultFrontend)
6978{
6979 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
6980 HRESULT hrc = i_checkStateDependency(MutableOrSavedStateDep);
6981 if (SUCCEEDED(hrc))
6982 {
6983 hrc = mHWData.backupEx();
6984 if (SUCCEEDED(hrc))
6985 {
6986 i_setModified(IsModified_MachineData);
6987 mHWData->mDefaultFrontend = aDefaultFrontend;
6988 }
6989 }
6990 return hrc;
6991}
6992
6993HRESULT Machine::getIcon(std::vector<BYTE> &aIcon)
6994{
6995 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
6996 size_t cbIcon = mUserData->mIcon.size();
6997 aIcon.resize(cbIcon);
6998 if (cbIcon)
6999 memcpy(&aIcon.front(), &mUserData->mIcon[0], cbIcon);
7000 return S_OK;
7001}
7002
7003HRESULT Machine::setIcon(const std::vector<BYTE> &aIcon)
7004{
7005 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
7006 HRESULT hrc = i_checkStateDependency(MutableOrSavedStateDep);
7007 if (SUCCEEDED(hrc))
7008 {
7009 i_setModified(IsModified_MachineData);
7010 mUserData.backup();
7011 size_t cbIcon = aIcon.size();
7012 mUserData->mIcon.resize(cbIcon);
7013 if (cbIcon)
7014 memcpy(&mUserData->mIcon[0], &aIcon.front(), cbIcon);
7015 }
7016 return hrc;
7017}
7018
7019HRESULT Machine::getUSBProxyAvailable(BOOL *aUSBProxyAvailable)
7020{
7021#ifdef VBOX_WITH_USB
7022 *aUSBProxyAvailable = true;
7023#else
7024 *aUSBProxyAvailable = false;
7025#endif
7026 return S_OK;
7027}
7028
7029HRESULT Machine::cloneTo(const ComPtr<IMachine> &aTarget, CloneMode_T aMode, const std::vector<CloneOptions_T> &aOptions,
7030 ComPtr<IProgress> &aProgress)
7031{
7032 ComObjPtr<Progress> pP;
7033 Progress *ppP = pP;
7034 IProgress *iP = static_cast<IProgress *>(ppP);
7035 IProgress **pProgress = &iP;
7036
7037 IMachine *pTarget = aTarget;
7038
7039 /* Convert the options. */
7040 RTCList<CloneOptions_T> optList;
7041 if (aOptions.size())
7042 for (size_t i = 0; i < aOptions.size(); ++i)
7043 optList.append(aOptions[i]);
7044
7045 if (optList.contains(CloneOptions_Link))
7046 {
7047 if (!i_isSnapshotMachine())
7048 return setError(E_INVALIDARG,
7049 tr("Linked clone can only be created from a snapshot"));
7050 if (aMode != CloneMode_MachineState)
7051 return setError(E_INVALIDARG,
7052 tr("Linked clone can only be created for a single machine state"));
7053 }
7054 AssertReturn(!(optList.contains(CloneOptions_KeepAllMACs) && optList.contains(CloneOptions_KeepNATMACs)), E_INVALIDARG);
7055
7056 MachineCloneVM *pWorker = new MachineCloneVM(this, static_cast<Machine*>(pTarget), aMode, optList);
7057
7058 HRESULT rc = pWorker->start(pProgress);
7059
7060 pP = static_cast<Progress *>(*pProgress);
7061 pP.queryInterfaceTo(aProgress.asOutParam());
7062
7063 return rc;
7064
7065}
7066
7067HRESULT Machine::saveState(ComPtr<IProgress> &aProgress)
7068{
7069 NOREF(aProgress);
7070 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
7071
7072 // This check should always fail.
7073 HRESULT rc = i_checkStateDependency(MutableStateDep);
7074 if (FAILED(rc)) return rc;
7075
7076 AssertFailedReturn(E_NOTIMPL);
7077}
7078
7079HRESULT Machine::adoptSavedState(const com::Utf8Str &aSavedStateFile)
7080{
7081 NOREF(aSavedStateFile);
7082 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
7083
7084 // This check should always fail.
7085 HRESULT rc = i_checkStateDependency(MutableStateDep);
7086 if (FAILED(rc)) return rc;
7087
7088 AssertFailedReturn(E_NOTIMPL);
7089}
7090
7091HRESULT Machine::discardSavedState(BOOL aFRemoveFile)
7092{
7093 NOREF(aFRemoveFile);
7094 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
7095
7096 // This check should always fail.
7097 HRESULT rc = i_checkStateDependency(MutableOrSavedStateDep);
7098 if (FAILED(rc)) return rc;
7099
7100 AssertFailedReturn(E_NOTIMPL);
7101}
7102
7103// public methods for internal purposes
7104/////////////////////////////////////////////////////////////////////////////
7105
7106/**
7107 * Adds the given IsModified_* flag to the dirty flags of the machine.
7108 * This must be called either during i_loadSettings or under the machine write lock.
7109 * @param fl
7110 */
7111void Machine::i_setModified(uint32_t fl, bool fAllowStateModification /* = true */)
7112{
7113 mData->flModifications |= fl;
7114 if (fAllowStateModification && i_isStateModificationAllowed())
7115 mData->mCurrentStateModified = true;
7116}
7117
7118/**
7119 * Adds the given IsModified_* flag to the dirty flags of the machine, taking
7120 * care of the write locking.
7121 *
7122 * @param fModifications The flag to add.
7123 */
7124void Machine::i_setModifiedLock(uint32_t fModification, bool fAllowStateModification /* = true */)
7125{
7126 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
7127 i_setModified(fModification, fAllowStateModification);
7128}
7129
7130/**
7131 * Saves the registry entry of this machine to the given configuration node.
7132 *
7133 * @param aEntryNode Node to save the registry entry to.
7134 *
7135 * @note locks this object for reading.
7136 */
7137HRESULT Machine::i_saveRegistryEntry(settings::MachineRegistryEntry &data)
7138{
7139 AutoLimitedCaller autoCaller(this);
7140 AssertComRCReturnRC(autoCaller.rc());
7141
7142 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
7143
7144 data.uuid = mData->mUuid;
7145 data.strSettingsFile = mData->m_strConfigFile;
7146
7147 return S_OK;
7148}
7149
7150/**
7151 * Calculates the absolute path of the given path taking the directory of the
7152 * machine settings file as the current directory.
7153 *
7154 * @param aPath Path to calculate the absolute path for.
7155 * @param aResult Where to put the result (used only on success, can be the
7156 * same Utf8Str instance as passed in @a aPath).
7157 * @return IPRT result.
7158 *
7159 * @note Locks this object for reading.
7160 */
7161int Machine::i_calculateFullPath(const Utf8Str &strPath, Utf8Str &aResult)
7162{
7163 AutoCaller autoCaller(this);
7164 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
7165
7166 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
7167
7168 AssertReturn(!mData->m_strConfigFileFull.isEmpty(), VERR_GENERAL_FAILURE);
7169
7170 Utf8Str strSettingsDir = mData->m_strConfigFileFull;
7171
7172 strSettingsDir.stripFilename();
7173 char folder[RTPATH_MAX];
7174 int vrc = RTPathAbsEx(strSettingsDir.c_str(), strPath.c_str(), folder, sizeof(folder));
7175 if (RT_SUCCESS(vrc))
7176 aResult = folder;
7177
7178 return vrc;
7179}
7180
7181/**
7182 * Copies strSource to strTarget, making it relative to the machine folder
7183 * if it is a subdirectory thereof, or simply copying it otherwise.
7184 *
7185 * @param strSource Path to evaluate and copy.
7186 * @param strTarget Buffer to receive target path.
7187 *
7188 * @note Locks this object for reading.
7189 */
7190void Machine::i_copyPathRelativeToMachine(const Utf8Str &strSource,
7191 Utf8Str &strTarget)
7192{
7193 AutoCaller autoCaller(this);
7194 AssertComRCReturn(autoCaller.rc(), (void)0);
7195
7196 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
7197
7198 AssertReturnVoid(!mData->m_strConfigFileFull.isEmpty());
7199 // use strTarget as a temporary buffer to hold the machine settings dir
7200 strTarget = mData->m_strConfigFileFull;
7201 strTarget.stripFilename();
7202 if (RTPathStartsWith(strSource.c_str(), strTarget.c_str()))
7203 {
7204 // is relative: then append what's left
7205 strTarget = strSource.substr(strTarget.length() + 1); // skip '/'
7206 // for empty paths (only possible for subdirs) use "." to avoid
7207 // triggering default settings for not present config attributes.
7208 if (strTarget.isEmpty())
7209 strTarget = ".";
7210 }
7211 else
7212 // is not relative: then overwrite
7213 strTarget = strSource;
7214}
7215
7216/**
7217 * Returns the full path to the machine's log folder in the
7218 * \a aLogFolder argument.
7219 */
7220void Machine::i_getLogFolder(Utf8Str &aLogFolder)
7221{
7222 AutoCaller autoCaller(this);
7223 AssertComRCReturnVoid(autoCaller.rc());
7224
7225 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
7226
7227 char szTmp[RTPATH_MAX];
7228 int vrc = RTEnvGetEx(RTENV_DEFAULT, "VBOX_USER_VMLOGDIR", szTmp, sizeof(szTmp), NULL);
7229 if (RT_SUCCESS(vrc))
7230 {
7231 if (szTmp[0] && !mUserData.isNull())
7232 {
7233 char szTmp2[RTPATH_MAX];
7234 vrc = RTPathAbs(szTmp, szTmp2, sizeof(szTmp2));
7235 if (RT_SUCCESS(vrc))
7236 aLogFolder = BstrFmt("%s%c%s",
7237 szTmp2,
7238 RTPATH_DELIMITER,
7239 mUserData->s.strName.c_str()); // path/to/logfolder/vmname
7240 }
7241 else
7242 vrc = VERR_PATH_IS_RELATIVE;
7243 }
7244
7245 if (RT_FAILURE(vrc))
7246 {
7247 // fallback if VBOX_USER_LOGHOME is not set or invalid
7248 aLogFolder = mData->m_strConfigFileFull; // path/to/machinesfolder/vmname/vmname.vbox
7249 aLogFolder.stripFilename(); // path/to/machinesfolder/vmname
7250 aLogFolder.append(RTPATH_DELIMITER);
7251 aLogFolder.append("Logs"); // path/to/machinesfolder/vmname/Logs
7252 }
7253}
7254
7255/**
7256 * Returns the full path to the machine's log file for an given index.
7257 */
7258Utf8Str Machine::i_queryLogFilename(ULONG idx) /** @todo r=bird: Misnamed. Should be i_getLogFilename as it cannot fail.
7259 See VBox-CodingGuidelines.cpp, Compulsory seciont, line 79. */
7260{
7261 Utf8Str logFolder;
7262 getLogFolder(logFolder);
7263 Assert(logFolder.length());
7264 Utf8Str log;
7265 if (idx == 0)
7266 log = Utf8StrFmt("%s%cVBox.log",
7267 logFolder.c_str(), RTPATH_DELIMITER);
7268 else
7269 log = Utf8StrFmt("%s%cVBox.log.%d",
7270 logFolder.c_str(), RTPATH_DELIMITER, idx);
7271 return log;
7272}
7273
7274/**
7275 * Returns the full path to the machine's (hardened) startup log file.
7276 */
7277Utf8Str Machine::i_getStartupLogFilename(void)
7278{
7279 Utf8Str strFilename;
7280 getLogFolder(strFilename);
7281 Assert(strFilename.length());
7282 strFilename.append(RTPATH_SLASH_STR "VBoxStartup.log");
7283 return strFilename;
7284}
7285
7286
7287/**
7288 * Composes a unique saved state filename based on the current system time. The filename is
7289 * granular to the second so this will work so long as no more than one snapshot is taken on
7290 * a machine per second.
7291 *
7292 * Before version 4.1, we used this formula for saved state files:
7293 * Utf8StrFmt("%s%c{%RTuuid}.sav", strFullSnapshotFolder.c_str(), RTPATH_DELIMITER, mData->mUuid.raw())
7294 * which no longer works because saved state files can now be shared between the saved state of the
7295 * "saved" machine and an online snapshot, and the following would cause problems:
7296 * 1) save machine
7297 * 2) create online snapshot from that machine state --> reusing saved state file
7298 * 3) save machine again --> filename would be reused, breaking the online snapshot
7299 *
7300 * So instead we now use a timestamp.
7301 *
7302 * @param str
7303 */
7304
7305void Machine::i_composeSavedStateFilename(Utf8Str &strStateFilePath)
7306{
7307 AutoCaller autoCaller(this);
7308 AssertComRCReturnVoid(autoCaller.rc());
7309
7310 {
7311 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
7312 i_calculateFullPath(mUserData->s.strSnapshotFolder, strStateFilePath);
7313 }
7314
7315 RTTIMESPEC ts;
7316 RTTimeNow(&ts);
7317 RTTIME time;
7318 RTTimeExplode(&time, &ts);
7319
7320 strStateFilePath += RTPATH_DELIMITER;
7321 strStateFilePath += Utf8StrFmt("%04d-%02u-%02uT%02u-%02u-%02u-%09uZ.sav",
7322 time.i32Year, time.u8Month, time.u8MonthDay,
7323 time.u8Hour, time.u8Minute, time.u8Second, time.u32Nanosecond);
7324}
7325
7326/**
7327 * Returns the full path to the default video capture file.
7328 */
7329void Machine::i_getDefaultVideoCaptureFile(Utf8Str &strFile)
7330{
7331 AutoCaller autoCaller(this);
7332 AssertComRCReturnVoid(autoCaller.rc());
7333
7334 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
7335
7336 strFile = mData->m_strConfigFileFull; // path/to/machinesfolder/vmname/vmname.vbox
7337 strFile.stripSuffix(); // path/to/machinesfolder/vmname/vmname
7338 strFile.append(".webm"); // path/to/machinesfolder/vmname/vmname.webm
7339}
7340
7341/**
7342 * Returns whether at least one USB controller is present for the VM.
7343 */
7344bool Machine::i_isUSBControllerPresent()
7345{
7346 AutoCaller autoCaller(this);
7347 AssertComRCReturn(autoCaller.rc(), false);
7348
7349 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
7350
7351 return (mUSBControllers->size() > 0);
7352}
7353
7354/**
7355 * @note Locks this object for writing, calls the client process
7356 * (inside the lock).
7357 */
7358HRESULT Machine::i_launchVMProcess(IInternalSessionControl *aControl,
7359 const Utf8Str &strFrontend,
7360 const Utf8Str &strEnvironment,
7361 ProgressProxy *aProgress)
7362{
7363 LogFlowThisFuncEnter();
7364
7365 AssertReturn(aControl, E_FAIL);
7366 AssertReturn(aProgress, E_FAIL);
7367 AssertReturn(!strFrontend.isEmpty(), E_FAIL);
7368
7369 AutoCaller autoCaller(this);
7370 if (FAILED(autoCaller.rc())) return autoCaller.rc();
7371
7372 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
7373
7374 if (!mData->mRegistered)
7375 return setError(E_UNEXPECTED,
7376 tr("The machine '%s' is not registered"),
7377 mUserData->s.strName.c_str());
7378
7379 LogFlowThisFunc(("mSession.mState=%s\n", Global::stringifySessionState(mData->mSession.mState)));
7380
7381 /* The process started when launching a VM with separate UI/VM processes is always
7382 * the UI process, i.e. needs special handling as it won't claim the session. */
7383 bool fSeparate = strFrontend.endsWith("separate", Utf8Str::CaseInsensitive);
7384
7385 if (fSeparate)
7386 {
7387 if (mData->mSession.mState != SessionState_Unlocked && mData->mSession.mName != "headless")
7388 return setError(VBOX_E_INVALID_OBJECT_STATE,
7389 tr("The machine '%s' is in a state which is incompatible with launching a separate UI process"),
7390 mUserData->s.strName.c_str());
7391 }
7392 else
7393 {
7394 if ( mData->mSession.mState == SessionState_Locked
7395 || mData->mSession.mState == SessionState_Spawning
7396 || mData->mSession.mState == SessionState_Unlocking)
7397 return setError(VBOX_E_INVALID_OBJECT_STATE,
7398 tr("The machine '%s' is already locked by a session (or being locked or unlocked)"),
7399 mUserData->s.strName.c_str());
7400
7401 /* may not be busy */
7402 AssertReturn(!Global::IsOnlineOrTransient(mData->mMachineState), E_FAIL);
7403 }
7404
7405 /* get the path to the executable */
7406 char szPath[RTPATH_MAX];
7407 RTPathAppPrivateArch(szPath, sizeof(szPath) - 1);
7408 size_t cchBufLeft = strlen(szPath);
7409 szPath[cchBufLeft++] = RTPATH_DELIMITER;
7410 szPath[cchBufLeft] = 0;
7411 char *pszNamePart = szPath + cchBufLeft;
7412 cchBufLeft = sizeof(szPath) - cchBufLeft;
7413
7414 int vrc = VINF_SUCCESS;
7415 RTPROCESS pid = NIL_RTPROCESS;
7416
7417 RTENV env = RTENV_DEFAULT;
7418
7419 if (!strEnvironment.isEmpty())
7420 {
7421 char *newEnvStr = NULL;
7422
7423 do
7424 {
7425 /* clone the current environment */
7426 int vrc2 = RTEnvClone(&env, RTENV_DEFAULT);
7427 AssertRCBreakStmt(vrc2, vrc = vrc2);
7428
7429 newEnvStr = RTStrDup(strEnvironment.c_str());
7430 AssertPtrBreakStmt(newEnvStr, vrc = vrc2);
7431
7432 /* put new variables to the environment
7433 * (ignore empty variable names here since RTEnv API
7434 * intentionally doesn't do that) */
7435 char *var = newEnvStr;
7436 for (char *p = newEnvStr; *p; ++p)
7437 {
7438 if (*p == '\n' && (p == newEnvStr || *(p - 1) != '\\'))
7439 {
7440 *p = '\0';
7441 if (*var)
7442 {
7443 char *val = strchr(var, '=');
7444 if (val)
7445 {
7446 *val++ = '\0';
7447 vrc2 = RTEnvSetEx(env, var, val);
7448 }
7449 else
7450 vrc2 = RTEnvUnsetEx(env, var);
7451 if (RT_FAILURE(vrc2))
7452 break;
7453 }
7454 var = p + 1;
7455 }
7456 }
7457 if (RT_SUCCESS(vrc2) && *var)
7458 vrc2 = RTEnvPutEx(env, var);
7459
7460 AssertRCBreakStmt(vrc2, vrc = vrc2);
7461 }
7462 while (0);
7463
7464 if (newEnvStr != NULL)
7465 RTStrFree(newEnvStr);
7466 }
7467
7468 /* Hardened startup logging */
7469#if defined(RT_OS_WINDOWS) && defined(VBOX_WITH_HARDENING)
7470 Utf8Str strSupStartLogArg("--sup-startup-log=");
7471 {
7472 Utf8Str strStartupLogFile = i_getStartupLogFilename();
7473 int vrc2 = RTFileDelete(strStartupLogFile.c_str());
7474 if (vrc2 == VERR_PATH_NOT_FOUND || vrc2 == VERR_FILE_NOT_FOUND)
7475 {
7476 Utf8Str strStartupLogDir = strStartupLogFile;
7477 strStartupLogDir.stripFilename();
7478 RTDirCreateFullPath(strStartupLogDir.c_str(), 0755); /** @todo add a variant for creating the path to a
7479 file without stripping the file. */
7480 }
7481 strSupStartLogArg.append(strStartupLogFile);
7482 }
7483 const char *pszSupStartupLogArg = strSupStartLogArg.c_str();
7484#else
7485 const char *pszSupStartupLogArg = NULL;
7486#endif
7487
7488 Utf8Str strCanonicalName;
7489
7490#ifdef VBOX_WITH_QTGUI
7491 if ( !strFrontend.compare("gui", Utf8Str::CaseInsensitive)
7492 || !strFrontend.compare("GUI/Qt", Utf8Str::CaseInsensitive)
7493 || !strFrontend.compare("separate", Utf8Str::CaseInsensitive)
7494 || !strFrontend.compare("gui/separate", Utf8Str::CaseInsensitive)
7495 || !strFrontend.compare("GUI/Qt/separate", Utf8Str::CaseInsensitive))
7496 {
7497 strCanonicalName = "GUI/Qt";
7498# ifdef RT_OS_DARWIN /* Avoid Launch Services confusing this with the selector by using a helper app. */
7499 /* Modify the base path so that we don't need to use ".." below. */
7500 RTPathStripTrailingSlash(szPath);
7501 RTPathStripFilename(szPath);
7502 cchBufLeft = strlen(szPath);
7503 pszNamePart = szPath + cchBufLeft;
7504 cchBufLeft = sizeof(szPath) - cchBufLeft;
7505
7506# define OSX_APP_NAME "VirtualBoxVM"
7507# define OSX_APP_PATH_FMT "/Resources/%s.app/Contents/MacOS/VirtualBoxVM"
7508
7509 Utf8Str strAppOverride = i_getExtraData(Utf8Str("VBoxInternal2/VirtualBoxVMAppOverride"));
7510 if ( strAppOverride.contains(".")
7511 || strAppOverride.contains("/")
7512 || strAppOverride.contains("\\")
7513 || strAppOverride.contains(":"))
7514 strAppOverride.setNull();
7515 Utf8Str strAppPath;
7516 if (!strAppOverride.isEmpty())
7517 {
7518 strAppPath = Utf8StrFmt(OSX_APP_PATH_FMT, strAppOverride.c_str());
7519 Utf8Str strFullPath(szPath);
7520 strFullPath.append(strAppPath);
7521 /* there is a race, but people using this deserve the failure */
7522 if (!RTFileExists(strFullPath.c_str()))
7523 strAppOverride.setNull();
7524 }
7525 if (strAppOverride.isEmpty())
7526 strAppPath = Utf8StrFmt(OSX_APP_PATH_FMT, OSX_APP_NAME);
7527 AssertReturn(cchBufLeft > strAppPath.length(), E_UNEXPECTED);
7528 strcpy(pszNamePart, strAppPath.c_str());
7529# else
7530 static const char s_szVirtualBox_exe[] = "VirtualBox" HOSTSUFF_EXE;
7531 Assert(cchBufLeft >= sizeof(s_szVirtualBox_exe));
7532 strcpy(pszNamePart, s_szVirtualBox_exe);
7533# endif
7534
7535 Utf8Str idStr = mData->mUuid.toString();
7536 const char *apszArgs[] =
7537 {
7538 szPath,
7539 "--comment", mUserData->s.strName.c_str(),
7540 "--startvm", idStr.c_str(),
7541 "--no-startvm-errormsgbox",
7542 NULL, /* For "--separate". */
7543 NULL, /* For "--sup-startup-log". */
7544 NULL
7545 };
7546 unsigned iArg = 6;
7547 if (fSeparate)
7548 apszArgs[iArg++] = "--separate";
7549 apszArgs[iArg++] = pszSupStartupLogArg;
7550
7551 vrc = RTProcCreate(szPath, apszArgs, env, 0, &pid);
7552 }
7553#else /* !VBOX_WITH_QTGUI */
7554 if (0)
7555 ;
7556#endif /* VBOX_WITH_QTGUI */
7557
7558 else
7559
7560#ifdef VBOX_WITH_VBOXSDL
7561 if ( !strFrontend.compare("sdl", Utf8Str::CaseInsensitive)
7562 || !strFrontend.compare("GUI/SDL", Utf8Str::CaseInsensitive)
7563 || !strFrontend.compare("sdl/separate", Utf8Str::CaseInsensitive)
7564 || !strFrontend.compare("GUI/SDL/separate", Utf8Str::CaseInsensitive))
7565 {
7566 strCanonicalName = "GUI/SDL";
7567 static const char s_szVBoxSDL_exe[] = "VBoxSDL" HOSTSUFF_EXE;
7568 Assert(cchBufLeft >= sizeof(s_szVBoxSDL_exe));
7569 strcpy(pszNamePart, s_szVBoxSDL_exe);
7570
7571 Utf8Str idStr = mData->mUuid.toString();
7572 const char *apszArgs[] =
7573 {
7574 szPath,
7575 "--comment", mUserData->s.strName.c_str(),
7576 "--startvm", idStr.c_str(),
7577 NULL, /* For "--separate". */
7578 NULL, /* For "--sup-startup-log". */
7579 NULL
7580 };
7581 unsigned iArg = 5;
7582 if (fSeparate)
7583 apszArgs[iArg++] = "--separate";
7584 apszArgs[iArg++] = pszSupStartupLogArg;
7585
7586 vrc = RTProcCreate(szPath, apszArgs, env, 0, &pid);
7587 }
7588#else /* !VBOX_WITH_VBOXSDL */
7589 if (0)
7590 ;
7591#endif /* !VBOX_WITH_VBOXSDL */
7592
7593 else
7594
7595#ifdef VBOX_WITH_HEADLESS
7596 if ( !strFrontend.compare("headless", Utf8Str::CaseInsensitive)
7597 || !strFrontend.compare("capture", Utf8Str::CaseInsensitive)
7598 || !strFrontend.compare("vrdp", Utf8Str::CaseInsensitive) /* Deprecated. Same as headless. */
7599 )
7600 {
7601 strCanonicalName = "headless";
7602 /* On pre-4.0 the "headless" type was used for passing "--vrdp off" to VBoxHeadless to let it work in OSE,
7603 * which did not contain VRDP server. In VBox 4.0 the remote desktop server (VRDE) is optional,
7604 * and a VM works even if the server has not been installed.
7605 * So in 4.0 the "headless" behavior remains the same for default VBox installations.
7606 * Only if a VRDE has been installed and the VM enables it, the "headless" will work
7607 * differently in 4.0 and 3.x.
7608 */
7609 static const char s_szVBoxHeadless_exe[] = "VBoxHeadless" HOSTSUFF_EXE;
7610 Assert(cchBufLeft >= sizeof(s_szVBoxHeadless_exe));
7611 strcpy(pszNamePart, s_szVBoxHeadless_exe);
7612
7613 Utf8Str idStr = mData->mUuid.toString();
7614 const char *apszArgs[] =
7615 {
7616 szPath,
7617 "--comment", mUserData->s.strName.c_str(),
7618 "--startvm", idStr.c_str(),
7619 "--vrde", "config",
7620 NULL, /* For "--capture". */
7621 NULL, /* For "--sup-startup-log". */
7622 NULL
7623 };
7624 unsigned iArg = 7;
7625 if (!strFrontend.compare("capture", Utf8Str::CaseInsensitive))
7626 apszArgs[iArg++] = "--capture";
7627 apszArgs[iArg++] = pszSupStartupLogArg;
7628
7629# ifdef RT_OS_WINDOWS
7630 vrc = RTProcCreate(szPath, apszArgs, env, RTPROC_FLAGS_NO_WINDOW, &pid);
7631# else
7632 vrc = RTProcCreate(szPath, apszArgs, env, 0, &pid);
7633# endif
7634 }
7635#else /* !VBOX_WITH_HEADLESS */
7636 if (0)
7637 ;
7638#endif /* !VBOX_WITH_HEADLESS */
7639 else
7640 {
7641 RTEnvDestroy(env);
7642 return setError(E_INVALIDARG,
7643 tr("Invalid frontend name: '%s'"),
7644 strFrontend.c_str());
7645 }
7646
7647 RTEnvDestroy(env);
7648
7649 if (RT_FAILURE(vrc))
7650 return setError(VBOX_E_IPRT_ERROR,
7651 tr("Could not launch a process for the machine '%s' (%Rrc)"),
7652 mUserData->s.strName.c_str(), vrc);
7653
7654 LogFlowThisFunc(("launched.pid=%d(0x%x)\n", pid, pid));
7655
7656 if (!fSeparate)
7657 {
7658 /*
7659 * Note that we don't release the lock here before calling the client,
7660 * because it doesn't need to call us back if called with a NULL argument.
7661 * Releasing the lock here is dangerous because we didn't prepare the
7662 * launch data yet, but the client we've just started may happen to be
7663 * too fast and call LockMachine() that will fail (because of PID, etc.),
7664 * so that the Machine will never get out of the Spawning session state.
7665 */
7666
7667 /* inform the session that it will be a remote one */
7668 LogFlowThisFunc(("Calling AssignMachine (NULL)...\n"));
7669#ifndef VBOX_WITH_GENERIC_SESSION_WATCHER
7670 HRESULT rc = aControl->AssignMachine(NULL, LockType_Write, Bstr::Empty.raw());
7671#else /* VBOX_WITH_GENERIC_SESSION_WATCHER */
7672 HRESULT rc = aControl->AssignMachine(NULL, LockType_Write, NULL);
7673#endif /* VBOX_WITH_GENERIC_SESSION_WATCHER */
7674 LogFlowThisFunc(("AssignMachine (NULL) returned %08X\n", rc));
7675
7676 if (FAILED(rc))
7677 {
7678 /* restore the session state */
7679 mData->mSession.mState = SessionState_Unlocked;
7680 alock.release();
7681 mParent->i_addProcessToReap(pid);
7682 /* The failure may occur w/o any error info (from RPC), so provide one */
7683 return setError(VBOX_E_VM_ERROR,
7684 tr("Failed to assign the machine to the session (%Rhrc)"), rc);
7685 }
7686
7687 /* attach launch data to the machine */
7688 Assert(mData->mSession.mPID == NIL_RTPROCESS);
7689 mData->mSession.mRemoteControls.push_back(aControl);
7690 mData->mSession.mProgress = aProgress;
7691 mData->mSession.mPID = pid;
7692 mData->mSession.mState = SessionState_Spawning;
7693 Assert(strCanonicalName.isNotEmpty());
7694 mData->mSession.mName = strCanonicalName;
7695 }
7696 else
7697 {
7698 /* For separate UI process we declare the launch as completed instantly, as the
7699 * actual headless VM start may or may not come. No point in remembering anything
7700 * yet, as what matters for us is when the headless VM gets started. */
7701 aProgress->i_notifyComplete(S_OK);
7702 }
7703
7704 alock.release();
7705 mParent->i_addProcessToReap(pid);
7706
7707 LogFlowThisFuncLeave();
7708 return S_OK;
7709}
7710
7711/**
7712 * Returns @c true if the given session machine instance has an open direct
7713 * session (and optionally also for direct sessions which are closing) and
7714 * returns the session control machine instance if so.
7715 *
7716 * Note that when the method returns @c false, the arguments remain unchanged.
7717 *
7718 * @param aMachine Session machine object.
7719 * @param aControl Direct session control object (optional).
7720 * @param aAllowClosing If true then additionally a session which is currently
7721 * being closed will also be allowed.
7722 *
7723 * @note locks this object for reading.
7724 */
7725bool Machine::i_isSessionOpen(ComObjPtr<SessionMachine> &aMachine,
7726 ComPtr<IInternalSessionControl> *aControl /*= NULL*/,
7727 bool aAllowClosing /*= false*/)
7728{
7729 AutoLimitedCaller autoCaller(this);
7730 AssertComRCReturn(autoCaller.rc(), false);
7731
7732 /* just return false for inaccessible machines */
7733 if (getObjectState().getState() != ObjectState::Ready)
7734 return false;
7735
7736 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
7737
7738 if ( ( mData->mSession.mState == SessionState_Locked
7739 && mData->mSession.mLockType == LockType_VM)
7740 || (aAllowClosing && mData->mSession.mState == SessionState_Unlocking)
7741 )
7742 {
7743 AssertReturn(!mData->mSession.mMachine.isNull(), false);
7744
7745 aMachine = mData->mSession.mMachine;
7746
7747 if (aControl != NULL)
7748 *aControl = mData->mSession.mDirectControl;
7749
7750 return true;
7751 }
7752
7753 return false;
7754}
7755
7756/**
7757 * Returns @c true if the given machine has an spawning direct session.
7758 *
7759 * @note locks this object for reading.
7760 */
7761bool Machine::i_isSessionSpawning()
7762{
7763 AutoLimitedCaller autoCaller(this);
7764 AssertComRCReturn(autoCaller.rc(), false);
7765
7766 /* just return false for inaccessible machines */
7767 if (getObjectState().getState() != ObjectState::Ready)
7768 return false;
7769
7770 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
7771
7772 if (mData->mSession.mState == SessionState_Spawning)
7773 return true;
7774
7775 return false;
7776}
7777
7778/**
7779 * Called from the client watcher thread to check for unexpected client process
7780 * death during Session_Spawning state (e.g. before it successfully opened a
7781 * direct session).
7782 *
7783 * On Win32 and on OS/2, this method is called only when we've got the
7784 * direct client's process termination notification, so it always returns @c
7785 * true.
7786 *
7787 * On other platforms, this method returns @c true if the client process is
7788 * terminated and @c false if it's still alive.
7789 *
7790 * @note Locks this object for writing.
7791 */
7792bool Machine::i_checkForSpawnFailure()
7793{
7794 AutoCaller autoCaller(this);
7795 if (!autoCaller.isOk())
7796 {
7797 /* nothing to do */
7798 LogFlowThisFunc(("Already uninitialized!\n"));
7799 return true;
7800 }
7801
7802 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
7803
7804 if (mData->mSession.mState != SessionState_Spawning)
7805 {
7806 /* nothing to do */
7807 LogFlowThisFunc(("Not spawning any more!\n"));
7808 return true;
7809 }
7810
7811 HRESULT rc = S_OK;
7812
7813 /* PID not yet initialized, skip check. */
7814 if (mData->mSession.mPID == NIL_RTPROCESS)
7815 return false;
7816
7817 RTPROCSTATUS status;
7818 int vrc = RTProcWait(mData->mSession.mPID, RTPROCWAIT_FLAGS_NOBLOCK, &status);
7819
7820 if (vrc != VERR_PROCESS_RUNNING)
7821 {
7822 Utf8Str strExtraInfo;
7823
7824#if defined(RT_OS_WINDOWS) && defined(VBOX_WITH_HARDENING)
7825 /* If the startup logfile exists and is of non-zero length, tell the
7826 user to look there for more details to encourage them to attach it
7827 when reporting startup issues. */
7828 Utf8Str strStartupLogFile = i_getStartupLogFilename();
7829 uint64_t cbStartupLogFile = 0;
7830 int vrc2 = RTFileQuerySize(strStartupLogFile.c_str(), &cbStartupLogFile);
7831 if (RT_SUCCESS(vrc2) && cbStartupLogFile > 0)
7832 strExtraInfo.append(Utf8StrFmt(tr(". More details may be available in '%s'"), strStartupLogFile.c_str()));
7833#endif
7834
7835 if (RT_SUCCESS(vrc) && status.enmReason == RTPROCEXITREASON_NORMAL)
7836 rc = setError(E_FAIL,
7837 tr("The virtual machine '%s' has terminated unexpectedly during startup with exit code %d (%#x)%s"),
7838 i_getName().c_str(), status.iStatus, status.iStatus, strExtraInfo.c_str());
7839 else if (RT_SUCCESS(vrc) && status.enmReason == RTPROCEXITREASON_SIGNAL)
7840 rc = setError(E_FAIL,
7841 tr("The virtual machine '%s' has terminated unexpectedly during startup because of signal %d%s"),
7842 i_getName().c_str(), status.iStatus, strExtraInfo.c_str());
7843 else if (RT_SUCCESS(vrc) && status.enmReason == RTPROCEXITREASON_ABEND)
7844 rc = setError(E_FAIL,
7845 tr("The virtual machine '%s' has terminated abnormally (iStatus=%#x)%s"),
7846 i_getName().c_str(), status.iStatus, strExtraInfo.c_str());
7847 else
7848 rc = setError(E_FAIL,
7849 tr("The virtual machine '%s' has terminated unexpectedly during startup (%Rrc)%s"),
7850 i_getName().c_str(), vrc, strExtraInfo.c_str());
7851 }
7852
7853 if (FAILED(rc))
7854 {
7855 /* Close the remote session, remove the remote control from the list
7856 * and reset session state to Closed (@note keep the code in sync with
7857 * the relevant part in LockMachine()). */
7858
7859 Assert(mData->mSession.mRemoteControls.size() == 1);
7860 if (mData->mSession.mRemoteControls.size() == 1)
7861 {
7862 ErrorInfoKeeper eik;
7863 mData->mSession.mRemoteControls.front()->Uninitialize();
7864 }
7865
7866 mData->mSession.mRemoteControls.clear();
7867 mData->mSession.mState = SessionState_Unlocked;
7868
7869 /* finalize the progress after setting the state */
7870 if (!mData->mSession.mProgress.isNull())
7871 {
7872 mData->mSession.mProgress->notifyComplete(rc);
7873 mData->mSession.mProgress.setNull();
7874 }
7875
7876 mData->mSession.mPID = NIL_RTPROCESS;
7877
7878 mParent->i_onSessionStateChange(mData->mUuid, SessionState_Unlocked);
7879 return true;
7880 }
7881
7882 return false;
7883}
7884
7885/**
7886 * Checks whether the machine can be registered. If so, commits and saves
7887 * all settings.
7888 *
7889 * @note Must be called from mParent's write lock. Locks this object and
7890 * children for writing.
7891 */
7892HRESULT Machine::i_prepareRegister()
7893{
7894 AssertReturn(mParent->isWriteLockOnCurrentThread(), E_FAIL);
7895
7896 AutoLimitedCaller autoCaller(this);
7897 AssertComRCReturnRC(autoCaller.rc());
7898
7899 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
7900
7901 /* wait for state dependents to drop to zero */
7902 i_ensureNoStateDependencies();
7903
7904 if (!mData->mAccessible)
7905 return setError(VBOX_E_INVALID_OBJECT_STATE,
7906 tr("The machine '%s' with UUID {%s} is inaccessible and cannot be registered"),
7907 mUserData->s.strName.c_str(),
7908 mData->mUuid.toString().c_str());
7909
7910 AssertReturn(getObjectState().getState() == ObjectState::Ready, E_FAIL);
7911
7912 if (mData->mRegistered)
7913 return setError(VBOX_E_INVALID_OBJECT_STATE,
7914 tr("The machine '%s' with UUID {%s} is already registered"),
7915 mUserData->s.strName.c_str(),
7916 mData->mUuid.toString().c_str());
7917
7918 HRESULT rc = S_OK;
7919
7920 // Ensure the settings are saved. If we are going to be registered and
7921 // no config file exists yet, create it by calling i_saveSettings() too.
7922 if ( (mData->flModifications)
7923 || (!mData->pMachineConfigFile->fileExists())
7924 )
7925 {
7926 rc = i_saveSettings(NULL);
7927 // no need to check whether VirtualBox.xml needs saving too since
7928 // we can't have a machine XML file rename pending
7929 if (FAILED(rc)) return rc;
7930 }
7931
7932 /* more config checking goes here */
7933
7934 if (SUCCEEDED(rc))
7935 {
7936 /* we may have had implicit modifications we want to fix on success */
7937 i_commit();
7938
7939 mData->mRegistered = true;
7940 }
7941 else
7942 {
7943 /* we may have had implicit modifications we want to cancel on failure*/
7944 i_rollback(false /* aNotify */);
7945 }
7946
7947 return rc;
7948}
7949
7950/**
7951 * Increases the number of objects dependent on the machine state or on the
7952 * registered state. Guarantees that these two states will not change at least
7953 * until #releaseStateDependency() is called.
7954 *
7955 * Depending on the @a aDepType value, additional state checks may be made.
7956 * These checks will set extended error info on failure. See
7957 * #checkStateDependency() for more info.
7958 *
7959 * If this method returns a failure, the dependency is not added and the caller
7960 * is not allowed to rely on any particular machine state or registration state
7961 * value and may return the failed result code to the upper level.
7962 *
7963 * @param aDepType Dependency type to add.
7964 * @param aState Current machine state (NULL if not interested).
7965 * @param aRegistered Current registered state (NULL if not interested).
7966 *
7967 * @note Locks this object for writing.
7968 */
7969HRESULT Machine::i_addStateDependency(StateDependency aDepType /* = AnyStateDep */,
7970 MachineState_T *aState /* = NULL */,
7971 BOOL *aRegistered /* = NULL */)
7972{
7973 AutoCaller autoCaller(this);
7974 AssertComRCReturnRC(autoCaller.rc());
7975
7976 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
7977
7978 HRESULT rc = i_checkStateDependency(aDepType);
7979 if (FAILED(rc)) return rc;
7980
7981 {
7982 if (mData->mMachineStateChangePending != 0)
7983 {
7984 /* i_ensureNoStateDependencies() is waiting for state dependencies to
7985 * drop to zero so don't add more. It may make sense to wait a bit
7986 * and retry before reporting an error (since the pending state
7987 * transition should be really quick) but let's just assert for
7988 * now to see if it ever happens on practice. */
7989
7990 AssertFailed();
7991
7992 return setError(E_ACCESSDENIED,
7993 tr("Machine state change is in progress. Please retry the operation later."));
7994 }
7995
7996 ++mData->mMachineStateDeps;
7997 Assert(mData->mMachineStateDeps != 0 /* overflow */);
7998 }
7999
8000 if (aState)
8001 *aState = mData->mMachineState;
8002 if (aRegistered)
8003 *aRegistered = mData->mRegistered;
8004
8005 return S_OK;
8006}
8007
8008/**
8009 * Decreases the number of objects dependent on the machine state.
8010 * Must always complete the #addStateDependency() call after the state
8011 * dependency is no more necessary.
8012 */
8013void Machine::i_releaseStateDependency()
8014{
8015 AutoCaller autoCaller(this);
8016 AssertComRCReturnVoid(autoCaller.rc());
8017
8018 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
8019
8020 /* releaseStateDependency() w/o addStateDependency()? */
8021 AssertReturnVoid(mData->mMachineStateDeps != 0);
8022 -- mData->mMachineStateDeps;
8023
8024 if (mData->mMachineStateDeps == 0)
8025 {
8026 /* inform i_ensureNoStateDependencies() that there are no more deps */
8027 if (mData->mMachineStateChangePending != 0)
8028 {
8029 Assert(mData->mMachineStateDepsSem != NIL_RTSEMEVENTMULTI);
8030 RTSemEventMultiSignal (mData->mMachineStateDepsSem);
8031 }
8032 }
8033}
8034
8035Utf8Str Machine::i_getExtraData(const Utf8Str &strKey)
8036{
8037 /* start with nothing found */
8038 Utf8Str strResult("");
8039
8040 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
8041
8042 settings::StringsMap::const_iterator it = mData->pMachineConfigFile->mapExtraDataItems.find(strKey);
8043 if (it != mData->pMachineConfigFile->mapExtraDataItems.end())
8044 // found:
8045 strResult = it->second; // source is a Utf8Str
8046
8047 return strResult;
8048}
8049
8050// protected methods
8051/////////////////////////////////////////////////////////////////////////////
8052
8053/**
8054 * Performs machine state checks based on the @a aDepType value. If a check
8055 * fails, this method will set extended error info, otherwise it will return
8056 * S_OK. It is supposed, that on failure, the caller will immediately return
8057 * the return value of this method to the upper level.
8058 *
8059 * When @a aDepType is AnyStateDep, this method always returns S_OK.
8060 *
8061 * When @a aDepType is MutableStateDep, this method returns S_OK only if the
8062 * current state of this machine object allows to change settings of the
8063 * machine (i.e. the machine is not registered, or registered but not running
8064 * and not saved). It is useful to call this method from Machine setters
8065 * before performing any change.
8066 *
8067 * When @a aDepType is MutableOrSavedStateDep, this method behaves the same
8068 * as for MutableStateDep except that if the machine is saved, S_OK is also
8069 * returned. This is useful in setters which allow changing machine
8070 * properties when it is in the saved state.
8071 *
8072 * When @a aDepType is MutableOrRunningStateDep, this method returns S_OK only
8073 * if the current state of this machine object allows to change runtime
8074 * changeable settings of the machine (i.e. the machine is not registered, or
8075 * registered but either running or not running and not saved). It is useful
8076 * to call this method from Machine setters before performing any changes to
8077 * runtime changeable settings.
8078 *
8079 * When @a aDepType is MutableOrSavedOrRunningStateDep, this method behaves
8080 * the same as for MutableOrRunningStateDep except that if the machine is
8081 * saved, S_OK is also returned. This is useful in setters which allow
8082 * changing runtime and saved state changeable machine properties.
8083 *
8084 * @param aDepType Dependency type to check.
8085 *
8086 * @note Non Machine based classes should use #addStateDependency() and
8087 * #releaseStateDependency() methods or the smart AutoStateDependency
8088 * template.
8089 *
8090 * @note This method must be called from under this object's read or write
8091 * lock.
8092 */
8093HRESULT Machine::i_checkStateDependency(StateDependency aDepType)
8094{
8095 switch (aDepType)
8096 {
8097 case AnyStateDep:
8098 {
8099 break;
8100 }
8101 case MutableStateDep:
8102 {
8103 if ( mData->mRegistered
8104 && ( !i_isSessionMachine()
8105 || ( mData->mMachineState != MachineState_Aborted
8106 && mData->mMachineState != MachineState_Teleported
8107 && mData->mMachineState != MachineState_PoweredOff
8108 )
8109 )
8110 )
8111 return setError(VBOX_E_INVALID_VM_STATE,
8112 tr("The machine is not mutable (state is %s)"),
8113 Global::stringifyMachineState(mData->mMachineState));
8114 break;
8115 }
8116 case MutableOrSavedStateDep:
8117 {
8118 if ( mData->mRegistered
8119 && ( !i_isSessionMachine()
8120 || ( mData->mMachineState != MachineState_Aborted
8121 && mData->mMachineState != MachineState_Teleported
8122 && mData->mMachineState != MachineState_Saved
8123 && mData->mMachineState != MachineState_PoweredOff
8124 )
8125 )
8126 )
8127 return setError(VBOX_E_INVALID_VM_STATE,
8128 tr("The machine is not mutable (state is %s)"),
8129 Global::stringifyMachineState(mData->mMachineState));
8130 break;
8131 }
8132 case MutableOrRunningStateDep:
8133 {
8134 if ( mData->mRegistered
8135 && ( !i_isSessionMachine()
8136 || ( mData->mMachineState != MachineState_Aborted
8137 && mData->mMachineState != MachineState_Teleported
8138 && mData->mMachineState != MachineState_PoweredOff
8139 && !Global::IsOnline(mData->mMachineState)
8140 )
8141 )
8142 )
8143 return setError(VBOX_E_INVALID_VM_STATE,
8144 tr("The machine is not mutable (state is %s)"),
8145 Global::stringifyMachineState(mData->mMachineState));
8146 break;
8147 }
8148 case MutableOrSavedOrRunningStateDep:
8149 {
8150 if ( mData->mRegistered
8151 && ( !i_isSessionMachine()
8152 || ( mData->mMachineState != MachineState_Aborted
8153 && mData->mMachineState != MachineState_Teleported
8154 && mData->mMachineState != MachineState_Saved
8155 && mData->mMachineState != MachineState_PoweredOff
8156 && !Global::IsOnline(mData->mMachineState)
8157 )
8158 )
8159 )
8160 return setError(VBOX_E_INVALID_VM_STATE,
8161 tr("The machine is not mutable (state is %s)"),
8162 Global::stringifyMachineState(mData->mMachineState));
8163 break;
8164 }
8165 }
8166
8167 return S_OK;
8168}
8169
8170/**
8171 * Helper to initialize all associated child objects and allocate data
8172 * structures.
8173 *
8174 * This method must be called as a part of the object's initialization procedure
8175 * (usually done in the #init() method).
8176 *
8177 * @note Must be called only from #init() or from #registeredInit().
8178 */
8179HRESULT Machine::initDataAndChildObjects()
8180{
8181 AutoCaller autoCaller(this);
8182 AssertComRCReturnRC(autoCaller.rc());
8183 AssertComRCReturn( getObjectState().getState() == ObjectState::InInit
8184 || getObjectState().getState() == ObjectState::Limited, E_FAIL);
8185
8186 AssertReturn(!mData->mAccessible, E_FAIL);
8187
8188 /* allocate data structures */
8189 mSSData.allocate();
8190 mUserData.allocate();
8191 mHWData.allocate();
8192 mMediaData.allocate();
8193 mStorageControllers.allocate();
8194 mUSBControllers.allocate();
8195
8196 /* initialize mOSTypeId */
8197 mUserData->s.strOsType = mParent->i_getUnknownOSType()->i_id();
8198
8199 /* create associated BIOS settings object */
8200 unconst(mBIOSSettings).createObject();
8201 mBIOSSettings->init(this);
8202
8203 /* create an associated VRDE object (default is disabled) */
8204 unconst(mVRDEServer).createObject();
8205 mVRDEServer->init(this);
8206
8207 /* create associated serial port objects */
8208 for (ULONG slot = 0; slot < RT_ELEMENTS(mSerialPorts); ++slot)
8209 {
8210 unconst(mSerialPorts[slot]).createObject();
8211 mSerialPorts[slot]->init(this, slot);
8212 }
8213
8214 /* create associated parallel port objects */
8215 for (ULONG slot = 0; slot < RT_ELEMENTS(mParallelPorts); ++slot)
8216 {
8217 unconst(mParallelPorts[slot]).createObject();
8218 mParallelPorts[slot]->init(this, slot);
8219 }
8220
8221 /* create the audio adapter object (always present, default is disabled) */
8222 unconst(mAudioAdapter).createObject();
8223 mAudioAdapter->init(this);
8224
8225 /* create the USB device filters object (always present) */
8226 unconst(mUSBDeviceFilters).createObject();
8227 mUSBDeviceFilters->init(this);
8228
8229 /* create associated network adapter objects */
8230 mNetworkAdapters.resize(Global::getMaxNetworkAdapters(mHWData->mChipsetType));
8231 for (ULONG slot = 0; slot < mNetworkAdapters.size(); ++slot)
8232 {
8233 unconst(mNetworkAdapters[slot]).createObject();
8234 mNetworkAdapters[slot]->init(this, slot);
8235 }
8236
8237 /* create the bandwidth control */
8238 unconst(mBandwidthControl).createObject();
8239 mBandwidthControl->init(this);
8240
8241 return S_OK;
8242}
8243
8244/**
8245 * Helper to uninitialize all associated child objects and to free all data
8246 * structures.
8247 *
8248 * This method must be called as a part of the object's uninitialization
8249 * procedure (usually done in the #uninit() method).
8250 *
8251 * @note Must be called only from #uninit() or from #registeredInit().
8252 */
8253void Machine::uninitDataAndChildObjects()
8254{
8255 AutoCaller autoCaller(this);
8256 AssertComRCReturnVoid(autoCaller.rc());
8257 AssertComRCReturnVoid( getObjectState().getState() == ObjectState::InUninit
8258 || getObjectState().getState() == ObjectState::Limited);
8259
8260 /* tell all our other child objects we've been uninitialized */
8261 if (mBandwidthControl)
8262 {
8263 mBandwidthControl->uninit();
8264 unconst(mBandwidthControl).setNull();
8265 }
8266
8267 for (ULONG slot = 0; slot < mNetworkAdapters.size(); ++slot)
8268 {
8269 if (mNetworkAdapters[slot])
8270 {
8271 mNetworkAdapters[slot]->uninit();
8272 unconst(mNetworkAdapters[slot]).setNull();
8273 }
8274 }
8275
8276 if (mUSBDeviceFilters)
8277 {
8278 mUSBDeviceFilters->uninit();
8279 unconst(mUSBDeviceFilters).setNull();
8280 }
8281
8282 if (mAudioAdapter)
8283 {
8284 mAudioAdapter->uninit();
8285 unconst(mAudioAdapter).setNull();
8286 }
8287
8288 for (ULONG slot = 0; slot < RT_ELEMENTS(mParallelPorts); ++slot)
8289 {
8290 if (mParallelPorts[slot])
8291 {
8292 mParallelPorts[slot]->uninit();
8293 unconst(mParallelPorts[slot]).setNull();
8294 }
8295 }
8296
8297 for (ULONG slot = 0; slot < RT_ELEMENTS(mSerialPorts); ++slot)
8298 {
8299 if (mSerialPorts[slot])
8300 {
8301 mSerialPorts[slot]->uninit();
8302 unconst(mSerialPorts[slot]).setNull();
8303 }
8304 }
8305
8306 if (mVRDEServer)
8307 {
8308 mVRDEServer->uninit();
8309 unconst(mVRDEServer).setNull();
8310 }
8311
8312 if (mBIOSSettings)
8313 {
8314 mBIOSSettings->uninit();
8315 unconst(mBIOSSettings).setNull();
8316 }
8317
8318 /* Deassociate media (only when a real Machine or a SnapshotMachine
8319 * instance is uninitialized; SessionMachine instances refer to real
8320 * Machine media). This is necessary for a clean re-initialization of
8321 * the VM after successfully re-checking the accessibility state. Note
8322 * that in case of normal Machine or SnapshotMachine uninitialization (as
8323 * a result of unregistering or deleting the snapshot), outdated media
8324 * attachments will already be uninitialized and deleted, so this
8325 * code will not affect them. */
8326 if ( !!mMediaData
8327 && (!i_isSessionMachine())
8328 )
8329 {
8330 for (MediaData::AttachmentList::const_iterator it = mMediaData->mAttachments.begin();
8331 it != mMediaData->mAttachments.end();
8332 ++it)
8333 {
8334 ComObjPtr<Medium> pMedium = (*it)->i_getMedium();
8335 if (pMedium.isNull())
8336 continue;
8337 HRESULT rc = pMedium->i_removeBackReference(mData->mUuid, i_getSnapshotId());
8338 AssertComRC(rc);
8339 }
8340 }
8341
8342 if (!i_isSessionMachine() && !i_isSnapshotMachine())
8343 {
8344 // clean up the snapshots list (Snapshot::uninit() will handle the snapshot's children recursively)
8345 if (mData->mFirstSnapshot)
8346 {
8347 // snapshots tree is protected by machine write lock; strictly
8348 // this isn't necessary here since we're deleting the entire
8349 // machine, but otherwise we assert in Snapshot::uninit()
8350 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
8351 mData->mFirstSnapshot->uninit();
8352 mData->mFirstSnapshot.setNull();
8353 }
8354
8355 mData->mCurrentSnapshot.setNull();
8356 }
8357
8358 /* free data structures (the essential mData structure is not freed here
8359 * since it may be still in use) */
8360 mMediaData.free();
8361 mStorageControllers.free();
8362 mUSBControllers.free();
8363 mHWData.free();
8364 mUserData.free();
8365 mSSData.free();
8366}
8367
8368/**
8369 * Returns a pointer to the Machine object for this machine that acts like a
8370 * parent for complex machine data objects such as shared folders, etc.
8371 *
8372 * For primary Machine objects and for SnapshotMachine objects, returns this
8373 * object's pointer itself. For SessionMachine objects, returns the peer
8374 * (primary) machine pointer.
8375 */
8376Machine* Machine::i_getMachine()
8377{
8378 if (i_isSessionMachine())
8379 return (Machine*)mPeer;
8380 return this;
8381}
8382
8383/**
8384 * Makes sure that there are no machine state dependents. If necessary, waits
8385 * for the number of dependents to drop to zero.
8386 *
8387 * Make sure this method is called from under this object's write lock to
8388 * guarantee that no new dependents may be added when this method returns
8389 * control to the caller.
8390 *
8391 * @note Locks this object for writing. The lock will be released while waiting
8392 * (if necessary).
8393 *
8394 * @warning To be used only in methods that change the machine state!
8395 */
8396void Machine::i_ensureNoStateDependencies()
8397{
8398 AssertReturnVoid(isWriteLockOnCurrentThread());
8399
8400 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
8401
8402 /* Wait for all state dependents if necessary */
8403 if (mData->mMachineStateDeps != 0)
8404 {
8405 /* lazy semaphore creation */
8406 if (mData->mMachineStateDepsSem == NIL_RTSEMEVENTMULTI)
8407 RTSemEventMultiCreate(&mData->mMachineStateDepsSem);
8408
8409 LogFlowThisFunc(("Waiting for state deps (%d) to drop to zero...\n",
8410 mData->mMachineStateDeps));
8411
8412 ++mData->mMachineStateChangePending;
8413
8414 /* reset the semaphore before waiting, the last dependent will signal
8415 * it */
8416 RTSemEventMultiReset(mData->mMachineStateDepsSem);
8417
8418 alock.release();
8419
8420 RTSemEventMultiWait(mData->mMachineStateDepsSem, RT_INDEFINITE_WAIT);
8421
8422 alock.acquire();
8423
8424 -- mData->mMachineStateChangePending;
8425 }
8426}
8427
8428/**
8429 * Changes the machine state and informs callbacks.
8430 *
8431 * This method is not intended to fail so it either returns S_OK or asserts (and
8432 * returns a failure).
8433 *
8434 * @note Locks this object for writing.
8435 */
8436HRESULT Machine::i_setMachineState(MachineState_T aMachineState)
8437{
8438 LogFlowThisFuncEnter();
8439 LogFlowThisFunc(("aMachineState=%s\n", Global::stringifyMachineState(aMachineState) ));
8440 Assert(aMachineState != MachineState_Null);
8441
8442 AutoCaller autoCaller(this);
8443 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
8444
8445 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
8446
8447 /* wait for state dependents to drop to zero */
8448 i_ensureNoStateDependencies();
8449
8450 MachineState_T const enmOldState = mData->mMachineState;
8451 if (enmOldState != aMachineState)
8452 {
8453 mData->mMachineState = aMachineState;
8454 RTTimeNow(&mData->mLastStateChange);
8455
8456#ifdef VBOX_WITH_DTRACE_R3_MAIN
8457 VBOXAPI_MACHINE_STATE_CHANGED(this, aMachineState, enmOldState, mData->mUuid.toStringCurly().c_str());
8458#endif
8459 mParent->i_onMachineStateChange(mData->mUuid, aMachineState);
8460 }
8461
8462 LogFlowThisFuncLeave();
8463 return S_OK;
8464}
8465
8466/**
8467 * Searches for a shared folder with the given logical name
8468 * in the collection of shared folders.
8469 *
8470 * @param aName logical name of the shared folder
8471 * @param aSharedFolder where to return the found object
8472 * @param aSetError whether to set the error info if the folder is
8473 * not found
8474 * @return
8475 * S_OK when found or VBOX_E_OBJECT_NOT_FOUND when not found
8476 *
8477 * @note
8478 * must be called from under the object's lock!
8479 */
8480HRESULT Machine::i_findSharedFolder(const Utf8Str &aName,
8481 ComObjPtr<SharedFolder> &aSharedFolder,
8482 bool aSetError /* = false */)
8483{
8484 HRESULT rc = VBOX_E_OBJECT_NOT_FOUND;
8485 for (HWData::SharedFolderList::const_iterator it = mHWData->mSharedFolders.begin();
8486 it != mHWData->mSharedFolders.end();
8487 ++it)
8488 {
8489 SharedFolder *pSF = *it;
8490 AutoCaller autoCaller(pSF);
8491 if (pSF->i_getName() == aName)
8492 {
8493 aSharedFolder = pSF;
8494 rc = S_OK;
8495 break;
8496 }
8497 }
8498
8499 if (aSetError && FAILED(rc))
8500 setError(rc, tr("Could not find a shared folder named '%s'"), aName.c_str());
8501
8502 return rc;
8503}
8504
8505/**
8506 * Initializes all machine instance data from the given settings structures
8507 * from XML. The exception is the machine UUID which needs special handling
8508 * depending on the caller's use case, so the caller needs to set that herself.
8509 *
8510 * This gets called in several contexts during machine initialization:
8511 *
8512 * -- When machine XML exists on disk already and needs to be loaded into memory,
8513 * for example, from registeredInit() to load all registered machines on
8514 * VirtualBox startup. In this case, puuidRegistry is NULL because the media
8515 * attached to the machine should be part of some media registry already.
8516 *
8517 * -- During OVF import, when a machine config has been constructed from an
8518 * OVF file. In this case, puuidRegistry is set to the machine UUID to
8519 * ensure that the media listed as attachments in the config (which have
8520 * been imported from the OVF) receive the correct registry ID.
8521 *
8522 * -- During VM cloning.
8523 *
8524 * @param config Machine settings from XML.
8525 * @param puuidRegistry If != NULL, Medium::setRegistryIdIfFirst() gets called with this registry ID
8526 * for each attached medium in the config.
8527 * @return
8528 */
8529HRESULT Machine::i_loadMachineDataFromSettings(const settings::MachineConfigFile &config,
8530 const Guid *puuidRegistry)
8531{
8532 // copy name, description, OS type, teleporter, UTC etc.
8533 mUserData->s = config.machineUserData;
8534
8535 // Decode the Icon overide data from config userdata and set onto Machine.
8536 #define DECODE_STR_MAX _1M
8537 const char* pszStr = config.machineUserData.ovIcon.c_str();
8538 ssize_t cbOut = RTBase64DecodedSize(pszStr, NULL);
8539 if (cbOut > DECODE_STR_MAX)
8540 return setError(E_FAIL,
8541 tr("Icon Data too long.'%d' > '%d'"),
8542 cbOut,
8543 DECODE_STR_MAX);
8544 mUserData->mIcon.resize(cbOut);
8545 int vrc = VINF_SUCCESS;
8546 if (cbOut)
8547 vrc = RTBase64Decode(pszStr, &mUserData->mIcon.front(), cbOut, NULL, NULL);
8548 if (RT_FAILURE(vrc))
8549 {
8550 mUserData->mIcon.resize(0);
8551 return setError(E_FAIL,
8552 tr("Failure to Decode Icon Data. '%s' (%Rrc)"),
8553 pszStr,
8554 vrc);
8555 }
8556
8557 // look up the object by Id to check it is valid
8558 ComPtr<IGuestOSType> guestOSType;
8559 HRESULT rc = mParent->GetGuestOSType(Bstr(mUserData->s.strOsType).raw(),
8560 guestOSType.asOutParam());
8561 if (FAILED(rc)) return rc;
8562
8563 // stateFile (optional)
8564 if (config.strStateFile.isEmpty())
8565 mSSData->strStateFilePath.setNull();
8566 else
8567 {
8568 Utf8Str stateFilePathFull(config.strStateFile);
8569 vrc = i_calculateFullPath(stateFilePathFull, stateFilePathFull);
8570 if (RT_FAILURE(vrc))
8571 return setError(E_FAIL,
8572 tr("Invalid saved state file path '%s' (%Rrc)"),
8573 config.strStateFile.c_str(),
8574 vrc);
8575 mSSData->strStateFilePath = stateFilePathFull;
8576 }
8577
8578 // snapshot folder needs special processing so set it again
8579 rc = COMSETTER(SnapshotFolder)(Bstr(config.machineUserData.strSnapshotFolder).raw());
8580 if (FAILED(rc)) return rc;
8581
8582 /* Copy the extra data items (Not in any case config is already the same as
8583 * mData->pMachineConfigFile, like when the xml files are read from disk. So
8584 * make sure the extra data map is copied). */
8585 mData->pMachineConfigFile->mapExtraDataItems = config.mapExtraDataItems;
8586
8587 /* currentStateModified (optional, default is true) */
8588 mData->mCurrentStateModified = config.fCurrentStateModified;
8589
8590 mData->mLastStateChange = config.timeLastStateChange;
8591
8592 /*
8593 * note: all mUserData members must be assigned prior this point because
8594 * we need to commit changes in order to let mUserData be shared by all
8595 * snapshot machine instances.
8596 */
8597 mUserData.commitCopy();
8598
8599 // machine registry, if present (must be loaded before snapshots)
8600 if (config.canHaveOwnMediaRegistry())
8601 {
8602 // determine machine folder
8603 Utf8Str strMachineFolder = i_getSettingsFileFull();
8604 strMachineFolder.stripFilename();
8605 rc = mParent->initMedia(i_getId(), // media registry ID == machine UUID
8606 config.mediaRegistry,
8607 strMachineFolder);
8608 if (FAILED(rc)) return rc;
8609 }
8610
8611 /* Snapshot node (optional) */
8612 size_t cRootSnapshots;
8613 if ((cRootSnapshots = config.llFirstSnapshot.size()))
8614 {
8615 // there must be only one root snapshot
8616 Assert(cRootSnapshots == 1);
8617
8618 const settings::Snapshot &snap = config.llFirstSnapshot.front();
8619
8620 rc = i_loadSnapshot(snap,
8621 config.uuidCurrentSnapshot,
8622 NULL); // no parent == first snapshot
8623 if (FAILED(rc)) return rc;
8624 }
8625
8626 // hardware data
8627 rc = i_loadHardware(config.hardwareMachine, &config.debugging, &config.autostart);
8628 if (FAILED(rc)) return rc;
8629
8630 // load storage controllers
8631 rc = i_loadStorageControllers(config.storageMachine,
8632 puuidRegistry,
8633 NULL /* puuidSnapshot */);
8634 if (FAILED(rc)) return rc;
8635
8636 /*
8637 * NOTE: the assignment below must be the last thing to do,
8638 * otherwise it will be not possible to change the settings
8639 * somewhere in the code above because all setters will be
8640 * blocked by i_checkStateDependency(MutableStateDep).
8641 */
8642
8643 /* set the machine state to Aborted or Saved when appropriate */
8644 if (config.fAborted)
8645 {
8646 mSSData->strStateFilePath.setNull();
8647
8648 /* no need to use i_setMachineState() during init() */
8649 mData->mMachineState = MachineState_Aborted;
8650 }
8651 else if (!mSSData->strStateFilePath.isEmpty())
8652 {
8653 /* no need to use i_setMachineState() during init() */
8654 mData->mMachineState = MachineState_Saved;
8655 }
8656
8657 // after loading settings, we are no longer different from the XML on disk
8658 mData->flModifications = 0;
8659
8660 return S_OK;
8661}
8662
8663/**
8664 * Recursively loads all snapshots starting from the given.
8665 *
8666 * @param aNode <Snapshot> node.
8667 * @param aCurSnapshotId Current snapshot ID from the settings file.
8668 * @param aParentSnapshot Parent snapshot.
8669 */
8670HRESULT Machine::i_loadSnapshot(const settings::Snapshot &data,
8671 const Guid &aCurSnapshotId,
8672 Snapshot *aParentSnapshot)
8673{
8674 AssertReturn(!i_isSnapshotMachine(), E_FAIL);
8675 AssertReturn(!i_isSessionMachine(), E_FAIL);
8676
8677 HRESULT rc = S_OK;
8678
8679 Utf8Str strStateFile;
8680 if (!data.strStateFile.isEmpty())
8681 {
8682 /* optional */
8683 strStateFile = data.strStateFile;
8684 int vrc = i_calculateFullPath(strStateFile, strStateFile);
8685 if (RT_FAILURE(vrc))
8686 return setError(E_FAIL,
8687 tr("Invalid saved state file path '%s' (%Rrc)"),
8688 strStateFile.c_str(),
8689 vrc);
8690 }
8691
8692 /* create a snapshot machine object */
8693 ComObjPtr<SnapshotMachine> pSnapshotMachine;
8694 pSnapshotMachine.createObject();
8695 rc = pSnapshotMachine->initFromSettings(this,
8696 data.hardware,
8697 &data.debugging,
8698 &data.autostart,
8699 data.storage,
8700 data.uuid.ref(),
8701 strStateFile);
8702 if (FAILED(rc)) return rc;
8703
8704 /* create a snapshot object */
8705 ComObjPtr<Snapshot> pSnapshot;
8706 pSnapshot.createObject();
8707 /* initialize the snapshot */
8708 rc = pSnapshot->init(mParent, // VirtualBox object
8709 data.uuid,
8710 data.strName,
8711 data.strDescription,
8712 data.timestamp,
8713 pSnapshotMachine,
8714 aParentSnapshot);
8715 if (FAILED(rc)) return rc;
8716
8717 /* memorize the first snapshot if necessary */
8718 if (!mData->mFirstSnapshot)
8719 mData->mFirstSnapshot = pSnapshot;
8720
8721 /* memorize the current snapshot when appropriate */
8722 if ( !mData->mCurrentSnapshot
8723 && pSnapshot->i_getId() == aCurSnapshotId
8724 )
8725 mData->mCurrentSnapshot = pSnapshot;
8726
8727 // now create the children
8728 for (settings::SnapshotsList::const_iterator it = data.llChildSnapshots.begin();
8729 it != data.llChildSnapshots.end();
8730 ++it)
8731 {
8732 const settings::Snapshot &childData = *it;
8733 // recurse
8734 rc = i_loadSnapshot(childData,
8735 aCurSnapshotId,
8736 pSnapshot); // parent = the one we created above
8737 if (FAILED(rc)) return rc;
8738 }
8739
8740 return rc;
8741}
8742
8743/**
8744 * Loads settings into mHWData.
8745 *
8746 * @param data Reference to the hardware settings.
8747 * @param pDbg Pointer to the debugging settings.
8748 * @param pAutostart Pointer to the autostart settings.
8749 */
8750HRESULT Machine::i_loadHardware(const settings::Hardware &data, const settings::Debugging *pDbg,
8751 const settings::Autostart *pAutostart)
8752{
8753 AssertReturn(!i_isSessionMachine(), E_FAIL);
8754
8755 HRESULT rc = S_OK;
8756
8757 try
8758 {
8759 /* The hardware version attribute (optional). */
8760 mHWData->mHWVersion = data.strVersion;
8761 mHWData->mHardwareUUID = data.uuid;
8762
8763 mHWData->mHWVirtExEnabled = data.fHardwareVirt;
8764 mHWData->mHWVirtExNestedPagingEnabled = data.fNestedPaging;
8765 mHWData->mHWVirtExLargePagesEnabled = data.fLargePages;
8766 mHWData->mHWVirtExVPIDEnabled = data.fVPID;
8767 mHWData->mHWVirtExUXEnabled = data.fUnrestrictedExecution;
8768 mHWData->mHWVirtExForceEnabled = data.fHardwareVirtForce;
8769 mHWData->mPAEEnabled = data.fPAE;
8770 mHWData->mLongMode = data.enmLongMode;
8771 mHWData->mTripleFaultReset = data.fTripleFaultReset;
8772 mHWData->mCPUCount = data.cCPUs;
8773 mHWData->mCPUHotPlugEnabled = data.fCpuHotPlug;
8774 mHWData->mCpuExecutionCap = data.ulCpuExecutionCap;
8775 mHWData->mCpuIdPortabilityLevel = data.uCpuIdPortabilityLevel;
8776
8777 // cpu
8778 if (mHWData->mCPUHotPlugEnabled)
8779 {
8780 for (settings::CpuList::const_iterator it = data.llCpus.begin();
8781 it != data.llCpus.end();
8782 ++it)
8783 {
8784 const settings::Cpu &cpu = *it;
8785
8786 mHWData->mCPUAttached[cpu.ulId] = true;
8787 }
8788 }
8789
8790 // cpuid leafs
8791 for (settings::CpuIdLeafsList::const_iterator it = data.llCpuIdLeafs.begin();
8792 it != data.llCpuIdLeafs.end();
8793 ++it)
8794 {
8795 const settings::CpuIdLeaf &leaf = *it;
8796
8797 switch (leaf.ulId)
8798 {
8799 case 0x0:
8800 case 0x1:
8801 case 0x2:
8802 case 0x3:
8803 case 0x4:
8804 case 0x5:
8805 case 0x6:
8806 case 0x7:
8807 case 0x8:
8808 case 0x9:
8809 case 0xA:
8810 mHWData->mCpuIdStdLeafs[leaf.ulId] = leaf;
8811 break;
8812
8813 case 0x80000000:
8814 case 0x80000001:
8815 case 0x80000002:
8816 case 0x80000003:
8817 case 0x80000004:
8818 case 0x80000005:
8819 case 0x80000006:
8820 case 0x80000007:
8821 case 0x80000008:
8822 case 0x80000009:
8823 case 0x8000000A:
8824 mHWData->mCpuIdExtLeafs[leaf.ulId - 0x80000000] = leaf;
8825 break;
8826
8827 default:
8828 /* just ignore */
8829 break;
8830 }
8831 }
8832
8833 mHWData->mMemorySize = data.ulMemorySizeMB;
8834 mHWData->mPageFusionEnabled = data.fPageFusionEnabled;
8835
8836 // boot order
8837 for (unsigned i = 0; i < RT_ELEMENTS(mHWData->mBootOrder); ++i)
8838 {
8839 settings::BootOrderMap::const_iterator it = data.mapBootOrder.find(i);
8840 if (it == data.mapBootOrder.end())
8841 mHWData->mBootOrder[i] = DeviceType_Null;
8842 else
8843 mHWData->mBootOrder[i] = it->second;
8844 }
8845
8846 mHWData->mGraphicsControllerType = data.graphicsControllerType;
8847 mHWData->mVRAMSize = data.ulVRAMSizeMB;
8848 mHWData->mMonitorCount = data.cMonitors;
8849 mHWData->mAccelerate3DEnabled = data.fAccelerate3D;
8850 mHWData->mAccelerate2DVideoEnabled = data.fAccelerate2DVideo;
8851 mHWData->mVideoCaptureWidth = data.ulVideoCaptureHorzRes;
8852 mHWData->mVideoCaptureHeight = data.ulVideoCaptureVertRes;
8853 mHWData->mVideoCaptureEnabled = data.fVideoCaptureEnabled;
8854 for (unsigned i = 0; i < RT_ELEMENTS(mHWData->maVideoCaptureScreens); ++i)
8855 mHWData->maVideoCaptureScreens[i] = ASMBitTest(&data.u64VideoCaptureScreens, i);
8856 AssertCompile(RT_ELEMENTS(mHWData->maVideoCaptureScreens) == sizeof(data.u64VideoCaptureScreens) * 8);
8857 mHWData->mVideoCaptureRate = data.ulVideoCaptureRate;
8858 mHWData->mVideoCaptureFPS = data.ulVideoCaptureFPS;
8859 if (!data.strVideoCaptureFile.isEmpty())
8860 i_calculateFullPath(data.strVideoCaptureFile, mHWData->mVideoCaptureFile);
8861 else
8862 mHWData->mVideoCaptureFile.setNull();
8863 mHWData->mFirmwareType = data.firmwareType;
8864 mHWData->mPointingHIDType = data.pointingHIDType;
8865 mHWData->mKeyboardHIDType = data.keyboardHIDType;
8866 mHWData->mChipsetType = data.chipsetType;
8867 mHWData->mParavirtProvider = data.paravirtProvider;
8868 mHWData->mEmulatedUSBCardReaderEnabled = data.fEmulatedUSBCardReader;
8869 mHWData->mHPETEnabled = data.fHPETEnabled;
8870
8871 /* VRDEServer */
8872 rc = mVRDEServer->i_loadSettings(data.vrdeSettings);
8873 if (FAILED(rc)) return rc;
8874
8875 /* BIOS */
8876 rc = mBIOSSettings->i_loadSettings(data.biosSettings);
8877 if (FAILED(rc)) return rc;
8878
8879 // Bandwidth control (must come before network adapters)
8880 rc = mBandwidthControl->i_loadSettings(data.ioSettings);
8881 if (FAILED(rc)) return rc;
8882
8883 /* Shared folders */
8884 for (settings::USBControllerList::const_iterator it = data.usbSettings.llUSBControllers.begin();
8885 it != data.usbSettings.llUSBControllers.end();
8886 ++it)
8887 {
8888 const settings::USBController &settingsCtrl = *it;
8889 ComObjPtr<USBController> newCtrl;
8890
8891 newCtrl.createObject();
8892 newCtrl->init(this, settingsCtrl.strName, settingsCtrl.enmType);
8893 mUSBControllers->push_back(newCtrl);
8894 }
8895
8896 /* USB device filters */
8897 rc = mUSBDeviceFilters->i_loadSettings(data.usbSettings);
8898 if (FAILED(rc)) return rc;
8899
8900 // network adapters
8901 size_t newCount = Global::getMaxNetworkAdapters(mHWData->mChipsetType);
8902 size_t oldCount = mNetworkAdapters.size();
8903 if (newCount > oldCount)
8904 {
8905 mNetworkAdapters.resize(newCount);
8906 for (size_t slot = oldCount; slot < mNetworkAdapters.size(); ++slot)
8907 {
8908 unconst(mNetworkAdapters[slot]).createObject();
8909 mNetworkAdapters[slot]->init(this, (ULONG)slot);
8910 }
8911 }
8912 else if (newCount < oldCount)
8913 mNetworkAdapters.resize(newCount);
8914 for (settings::NetworkAdaptersList::const_iterator it = data.llNetworkAdapters.begin();
8915 it != data.llNetworkAdapters.end();
8916 ++it)
8917 {
8918 const settings::NetworkAdapter &nic = *it;
8919
8920 /* slot unicity is guaranteed by XML Schema */
8921 AssertBreak(nic.ulSlot < mNetworkAdapters.size());
8922 rc = mNetworkAdapters[nic.ulSlot]->i_loadSettings(mBandwidthControl, nic);
8923 if (FAILED(rc)) return rc;
8924 }
8925
8926 // serial ports
8927 for (settings::SerialPortsList::const_iterator it = data.llSerialPorts.begin();
8928 it != data.llSerialPorts.end();
8929 ++it)
8930 {
8931 const settings::SerialPort &s = *it;
8932
8933 AssertBreak(s.ulSlot < RT_ELEMENTS(mSerialPorts));
8934 rc = mSerialPorts[s.ulSlot]->i_loadSettings(s);
8935 if (FAILED(rc)) return rc;
8936 }
8937
8938 // parallel ports (optional)
8939 for (settings::ParallelPortsList::const_iterator it = data.llParallelPorts.begin();
8940 it != data.llParallelPorts.end();
8941 ++it)
8942 {
8943 const settings::ParallelPort &p = *it;
8944
8945 AssertBreak(p.ulSlot < RT_ELEMENTS(mParallelPorts));
8946 rc = mParallelPorts[p.ulSlot]->i_loadSettings(p);
8947 if (FAILED(rc)) return rc;
8948 }
8949
8950 /* AudioAdapter */
8951 rc = mAudioAdapter->i_loadSettings(data.audioAdapter);
8952 if (FAILED(rc)) return rc;
8953
8954 /* Shared folders */
8955 for (settings::SharedFoldersList::const_iterator it = data.llSharedFolders.begin();
8956 it != data.llSharedFolders.end();
8957 ++it)
8958 {
8959 const settings::SharedFolder &sf = *it;
8960
8961 ComObjPtr<SharedFolder> sharedFolder;
8962 /* Check for double entries. Not allowed! */
8963 rc = i_findSharedFolder(sf.strName, sharedFolder, false /* aSetError */);
8964 if (SUCCEEDED(rc))
8965 return setError(VBOX_E_OBJECT_IN_USE,
8966 tr("Shared folder named '%s' already exists"),
8967 sf.strName.c_str());
8968
8969 /* Create the new shared folder. Don't break on error. This will be
8970 * reported when the machine starts. */
8971 sharedFolder.createObject();
8972 rc = sharedFolder->init(i_getMachine(),
8973 sf.strName,
8974 sf.strHostPath,
8975 RT_BOOL(sf.fWritable),
8976 RT_BOOL(sf.fAutoMount),
8977 false /* fFailOnError */);
8978 if (FAILED(rc)) return rc;
8979 mHWData->mSharedFolders.push_back(sharedFolder);
8980 }
8981
8982 // Clipboard
8983 mHWData->mClipboardMode = data.clipboardMode;
8984
8985 // drag'n'drop
8986 mHWData->mDnDMode = data.dndMode;
8987
8988 // guest settings
8989 mHWData->mMemoryBalloonSize = data.ulMemoryBalloonSize;
8990
8991 // IO settings
8992 mHWData->mIOCacheEnabled = data.ioSettings.fIOCacheEnabled;
8993 mHWData->mIOCacheSize = data.ioSettings.ulIOCacheSize;
8994
8995 // Host PCI devices
8996 for (settings::HostPCIDeviceAttachmentList::const_iterator it = data.pciAttachments.begin();
8997 it != data.pciAttachments.end();
8998 ++it)
8999 {
9000 const settings::HostPCIDeviceAttachment &hpda = *it;
9001 ComObjPtr<PCIDeviceAttachment> pda;
9002
9003 pda.createObject();
9004 pda->i_loadSettings(this, hpda);
9005 mHWData->mPCIDeviceAssignments.push_back(pda);
9006 }
9007
9008 /*
9009 * (The following isn't really real hardware, but it lives in HWData
9010 * for reasons of convenience.)
9011 */
9012
9013#ifdef VBOX_WITH_GUEST_PROPS
9014 /* Guest properties (optional) */
9015
9016 /* Only load transient guest properties for configs which have saved
9017 * state, because there shouldn't be any for powered off VMs. The same
9018 * logic applies for snapshots, as offline snapshots shouldn't have
9019 * any such properties. They confuse the code in various places.
9020 * Note: can't rely on the machine state, as it isn't set yet. */
9021 bool fSkipTransientGuestProperties = mSSData->strStateFilePath.isEmpty();
9022 /* apologies for the hacky unconst() usage, but this needs hacking
9023 * actually inconsistent settings into consistency, otherwise there
9024 * will be some corner cases where the inconsistency survives
9025 * surprisingly long without getting fixed, especially for snapshots
9026 * as there are no config changes. */
9027 settings::GuestPropertiesList &llGuestProperties = unconst(data.llGuestProperties);
9028 for (settings::GuestPropertiesList::iterator it = llGuestProperties.begin();
9029 it != llGuestProperties.end();
9030 /*nothing*/)
9031 {
9032 const settings::GuestProperty &prop = *it;
9033 uint32_t fFlags = guestProp::NILFLAG;
9034 guestProp::validateFlags(prop.strFlags.c_str(), &fFlags);
9035 if ( fSkipTransientGuestProperties
9036 && ( fFlags & guestProp::TRANSIENT
9037 || fFlags & guestProp::TRANSRESET))
9038 {
9039 it = llGuestProperties.erase(it);
9040 continue;
9041 }
9042 HWData::GuestProperty property = { prop.strValue, (LONG64) prop.timestamp, fFlags };
9043 mHWData->mGuestProperties[prop.strName] = property;
9044 ++it;
9045 }
9046#endif /* VBOX_WITH_GUEST_PROPS defined */
9047
9048 rc = i_loadDebugging(pDbg);
9049 if (FAILED(rc))
9050 return rc;
9051
9052 mHWData->mAutostart = *pAutostart;
9053
9054 /* default frontend */
9055 mHWData->mDefaultFrontend = data.strDefaultFrontend;
9056 }
9057 catch(std::bad_alloc &)
9058 {
9059 return E_OUTOFMEMORY;
9060 }
9061
9062 AssertComRC(rc);
9063 return rc;
9064}
9065
9066/**
9067 * Called from Machine::loadHardware() to load the debugging settings of the
9068 * machine.
9069 *
9070 * @param pDbg Pointer to the settings.
9071 */
9072HRESULT Machine::i_loadDebugging(const settings::Debugging *pDbg)
9073{
9074 mHWData->mDebugging = *pDbg;
9075 /* no more processing currently required, this will probably change. */
9076 return S_OK;
9077}
9078
9079/**
9080 * Called from i_loadMachineDataFromSettings() for the storage controller data, including media.
9081 *
9082 * @param data
9083 * @param puuidRegistry media registry ID to set media to or NULL; see Machine::i_loadMachineDataFromSettings()
9084 * @param puuidSnapshot
9085 * @return
9086 */
9087HRESULT Machine::i_loadStorageControllers(const settings::Storage &data,
9088 const Guid *puuidRegistry,
9089 const Guid *puuidSnapshot)
9090{
9091 AssertReturn(!i_isSessionMachine(), E_FAIL);
9092
9093 HRESULT rc = S_OK;
9094
9095 for (settings::StorageControllersList::const_iterator it = data.llStorageControllers.begin();
9096 it != data.llStorageControllers.end();
9097 ++it)
9098 {
9099 const settings::StorageController &ctlData = *it;
9100
9101 ComObjPtr<StorageController> pCtl;
9102 /* Try to find one with the name first. */
9103 rc = i_getStorageControllerByName(ctlData.strName, pCtl, false /* aSetError */);
9104 if (SUCCEEDED(rc))
9105 return setError(VBOX_E_OBJECT_IN_USE,
9106 tr("Storage controller named '%s' already exists"),
9107 ctlData.strName.c_str());
9108
9109 pCtl.createObject();
9110 rc = pCtl->init(this,
9111 ctlData.strName,
9112 ctlData.storageBus,
9113 ctlData.ulInstance,
9114 ctlData.fBootable);
9115 if (FAILED(rc)) return rc;
9116
9117 mStorageControllers->push_back(pCtl);
9118
9119 rc = pCtl->COMSETTER(ControllerType)(ctlData.controllerType);
9120 if (FAILED(rc)) return rc;
9121
9122 rc = pCtl->COMSETTER(PortCount)(ctlData.ulPortCount);
9123 if (FAILED(rc)) return rc;
9124
9125 rc = pCtl->COMSETTER(UseHostIOCache)(ctlData.fUseHostIOCache);
9126 if (FAILED(rc)) return rc;
9127
9128 /* Load the attached devices now. */
9129 rc = i_loadStorageDevices(pCtl,
9130 ctlData,
9131 puuidRegistry,
9132 puuidSnapshot);
9133 if (FAILED(rc)) return rc;
9134 }
9135
9136 return S_OK;
9137}
9138
9139/**
9140 * Called from i_loadStorageControllers for a controller's devices.
9141 *
9142 * @param aStorageController
9143 * @param data
9144 * @param puuidRegistry media registry ID to set media to or NULL; see Machine::i_loadMachineDataFromSettings()
9145 * @param aSnapshotId pointer to the snapshot ID if this is a snapshot machine
9146 * @return
9147 */
9148HRESULT Machine::i_loadStorageDevices(StorageController *aStorageController,
9149 const settings::StorageController &data,
9150 const Guid *puuidRegistry,
9151 const Guid *puuidSnapshot)
9152{
9153 HRESULT rc = S_OK;
9154
9155 /* paranoia: detect duplicate attachments */
9156 for (settings::AttachedDevicesList::const_iterator it = data.llAttachedDevices.begin();
9157 it != data.llAttachedDevices.end();
9158 ++it)
9159 {
9160 const settings::AttachedDevice &ad = *it;
9161
9162 for (settings::AttachedDevicesList::const_iterator it2 = it;
9163 it2 != data.llAttachedDevices.end();
9164 ++it2)
9165 {
9166 if (it == it2)
9167 continue;
9168
9169 const settings::AttachedDevice &ad2 = *it2;
9170
9171 if ( ad.lPort == ad2.lPort
9172 && ad.lDevice == ad2.lDevice)
9173 {
9174 return setError(E_FAIL,
9175 tr("Duplicate attachments for storage controller '%s', port %d, device %d of the virtual machine '%s'"),
9176 aStorageController->i_getName().c_str(),
9177 ad.lPort,
9178 ad.lDevice,
9179 mUserData->s.strName.c_str());
9180 }
9181 }
9182 }
9183
9184 for (settings::AttachedDevicesList::const_iterator it = data.llAttachedDevices.begin();
9185 it != data.llAttachedDevices.end();
9186 ++it)
9187 {
9188 const settings::AttachedDevice &dev = *it;
9189 ComObjPtr<Medium> medium;
9190
9191 switch (dev.deviceType)
9192 {
9193 case DeviceType_Floppy:
9194 case DeviceType_DVD:
9195 if (dev.strHostDriveSrc.isNotEmpty())
9196 rc = mParent->i_host()->i_findHostDriveByName(dev.deviceType, dev.strHostDriveSrc,
9197 false /* fRefresh */, medium);
9198 else
9199 rc = mParent->i_findRemoveableMedium(dev.deviceType,
9200 dev.uuid,
9201 false /* fRefresh */,
9202 false /* aSetError */,
9203 medium);
9204 if (rc == VBOX_E_OBJECT_NOT_FOUND)
9205 // This is not an error. The host drive or UUID might have vanished, so just go
9206 // ahead without this removeable medium attachment
9207 rc = S_OK;
9208 break;
9209
9210 case DeviceType_HardDisk:
9211 {
9212 /* find a hard disk by UUID */
9213 rc = mParent->i_findHardDiskById(dev.uuid, true /* aDoSetError */, &medium);
9214 if (FAILED(rc))
9215 {
9216 if (i_isSnapshotMachine())
9217 {
9218 // wrap another error message around the "cannot find hard disk" set by findHardDisk
9219 // so the user knows that the bad disk is in a snapshot somewhere
9220 com::ErrorInfo info;
9221 return setError(E_FAIL,
9222 tr("A differencing image of snapshot {%RTuuid} could not be found. %ls"),
9223 puuidSnapshot->raw(),
9224 info.getText().raw());
9225 }
9226 else
9227 return rc;
9228 }
9229
9230 AutoWriteLock hdLock(medium COMMA_LOCKVAL_SRC_POS);
9231
9232 if (medium->i_getType() == MediumType_Immutable)
9233 {
9234 if (i_isSnapshotMachine())
9235 return setError(E_FAIL,
9236 tr("Immutable hard disk '%s' with UUID {%RTuuid} cannot be directly attached to snapshot with UUID {%RTuuid} "
9237 "of the virtual machine '%s' ('%s')"),
9238 medium->i_getLocationFull().c_str(),
9239 dev.uuid.raw(),
9240 puuidSnapshot->raw(),
9241 mUserData->s.strName.c_str(),
9242 mData->m_strConfigFileFull.c_str());
9243
9244 return setError(E_FAIL,
9245 tr("Immutable hard disk '%s' with UUID {%RTuuid} cannot be directly attached to the virtual machine '%s' ('%s')"),
9246 medium->i_getLocationFull().c_str(),
9247 dev.uuid.raw(),
9248 mUserData->s.strName.c_str(),
9249 mData->m_strConfigFileFull.c_str());
9250 }
9251
9252 if (medium->i_getType() == MediumType_MultiAttach)
9253 {
9254 if (i_isSnapshotMachine())
9255 return setError(E_FAIL,
9256 tr("Multi-attach hard disk '%s' with UUID {%RTuuid} cannot be directly attached to snapshot with UUID {%RTuuid} "
9257 "of the virtual machine '%s' ('%s')"),
9258 medium->i_getLocationFull().c_str(),
9259 dev.uuid.raw(),
9260 puuidSnapshot->raw(),
9261 mUserData->s.strName.c_str(),
9262 mData->m_strConfigFileFull.c_str());
9263
9264 return setError(E_FAIL,
9265 tr("Multi-attach hard disk '%s' with UUID {%RTuuid} cannot be directly attached to the virtual machine '%s' ('%s')"),
9266 medium->i_getLocationFull().c_str(),
9267 dev.uuid.raw(),
9268 mUserData->s.strName.c_str(),
9269 mData->m_strConfigFileFull.c_str());
9270 }
9271
9272 if ( !i_isSnapshotMachine()
9273 && medium->i_getChildren().size() != 0
9274 )
9275 return setError(E_FAIL,
9276 tr("Hard disk '%s' with UUID {%RTuuid} cannot be directly attached to the virtual machine '%s' ('%s') "
9277 "because it has %d differencing child hard disks"),
9278 medium->i_getLocationFull().c_str(),
9279 dev.uuid.raw(),
9280 mUserData->s.strName.c_str(),
9281 mData->m_strConfigFileFull.c_str(),
9282 medium->i_getChildren().size());
9283
9284 if (i_findAttachment(mMediaData->mAttachments,
9285 medium))
9286 return setError(E_FAIL,
9287 tr("Hard disk '%s' with UUID {%RTuuid} is already attached to the virtual machine '%s' ('%s')"),
9288 medium->i_getLocationFull().c_str(),
9289 dev.uuid.raw(),
9290 mUserData->s.strName.c_str(),
9291 mData->m_strConfigFileFull.c_str());
9292
9293 break;
9294 }
9295
9296 default:
9297 return setError(E_FAIL,
9298 tr("Device '%s' with unknown type is attached to the virtual machine '%s' ('%s')"),
9299 medium->i_getLocationFull().c_str(),
9300 mUserData->s.strName.c_str(),
9301 mData->m_strConfigFileFull.c_str());
9302 }
9303
9304 if (FAILED(rc))
9305 break;
9306
9307 /* Bandwidth groups are loaded at this point. */
9308 ComObjPtr<BandwidthGroup> pBwGroup;
9309
9310 if (!dev.strBwGroup.isEmpty())
9311 {
9312 rc = mBandwidthControl->i_getBandwidthGroupByName(dev.strBwGroup, pBwGroup, false /* aSetError */);
9313 if (FAILED(rc))
9314 return setError(E_FAIL,
9315 tr("Device '%s' with unknown bandwidth group '%s' is attached to the virtual machine '%s' ('%s')"),
9316 medium->i_getLocationFull().c_str(),
9317 dev.strBwGroup.c_str(),
9318 mUserData->s.strName.c_str(),
9319 mData->m_strConfigFileFull.c_str());
9320 pBwGroup->i_reference();
9321 }
9322
9323 const Bstr controllerName = aStorageController->i_getName();
9324 ComObjPtr<MediumAttachment> pAttachment;
9325 pAttachment.createObject();
9326 rc = pAttachment->init(this,
9327 medium,
9328 controllerName,
9329 dev.lPort,
9330 dev.lDevice,
9331 dev.deviceType,
9332 false,
9333 dev.fPassThrough,
9334 dev.fTempEject,
9335 dev.fNonRotational,
9336 dev.fDiscard,
9337 dev.fHotPluggable,
9338 pBwGroup.isNull() ? Utf8Str::Empty : pBwGroup->i_getName());
9339 if (FAILED(rc)) break;
9340
9341 /* associate the medium with this machine and snapshot */
9342 if (!medium.isNull())
9343 {
9344 AutoCaller medCaller(medium);
9345 if (FAILED(medCaller.rc())) return medCaller.rc();
9346 AutoWriteLock mlock(medium COMMA_LOCKVAL_SRC_POS);
9347
9348 if (i_isSnapshotMachine())
9349 rc = medium->i_addBackReference(mData->mUuid, *puuidSnapshot);
9350 else
9351 rc = medium->i_addBackReference(mData->mUuid);
9352 /* If the medium->addBackReference fails it sets an appropriate
9353 * error message, so no need to do any guesswork here. */
9354
9355 if (puuidRegistry)
9356 // caller wants registry ID to be set on all attached media (OVF import case)
9357 medium->i_addRegistry(*puuidRegistry);
9358 }
9359
9360 if (FAILED(rc))
9361 break;
9362
9363 /* back up mMediaData to let registeredInit() properly rollback on failure
9364 * (= limited accessibility) */
9365 i_setModified(IsModified_Storage);
9366 mMediaData.backup();
9367 mMediaData->mAttachments.push_back(pAttachment);
9368 }
9369
9370 return rc;
9371}
9372
9373/**
9374 * Returns the snapshot with the given UUID or fails of no such snapshot exists.
9375 *
9376 * @param aId snapshot UUID to find (empty UUID refers the first snapshot)
9377 * @param aSnapshot where to return the found snapshot
9378 * @param aSetError true to set extended error info on failure
9379 */
9380HRESULT Machine::i_findSnapshotById(const Guid &aId,
9381 ComObjPtr<Snapshot> &aSnapshot,
9382 bool aSetError /* = false */)
9383{
9384 AutoReadLock chlock(this COMMA_LOCKVAL_SRC_POS);
9385
9386 if (!mData->mFirstSnapshot)
9387 {
9388 if (aSetError)
9389 return setError(E_FAIL, tr("This machine does not have any snapshots"));
9390 return E_FAIL;
9391 }
9392
9393 if (aId.isZero())
9394 aSnapshot = mData->mFirstSnapshot;
9395 else
9396 aSnapshot = mData->mFirstSnapshot->i_findChildOrSelf(aId.ref());
9397
9398 if (!aSnapshot)
9399 {
9400 if (aSetError)
9401 return setError(E_FAIL,
9402 tr("Could not find a snapshot with UUID {%s}"),
9403 aId.toString().c_str());
9404 return E_FAIL;
9405 }
9406
9407 return S_OK;
9408}
9409
9410/**
9411 * Returns the snapshot with the given name or fails of no such snapshot.
9412 *
9413 * @param aName snapshot name to find
9414 * @param aSnapshot where to return the found snapshot
9415 * @param aSetError true to set extended error info on failure
9416 */
9417HRESULT Machine::i_findSnapshotByName(const Utf8Str &strName,
9418 ComObjPtr<Snapshot> &aSnapshot,
9419 bool aSetError /* = false */)
9420{
9421 AssertReturn(!strName.isEmpty(), E_INVALIDARG);
9422
9423 AutoReadLock chlock(this COMMA_LOCKVAL_SRC_POS);
9424
9425 if (!mData->mFirstSnapshot)
9426 {
9427 if (aSetError)
9428 return setError(VBOX_E_OBJECT_NOT_FOUND,
9429 tr("This machine does not have any snapshots"));
9430 return VBOX_E_OBJECT_NOT_FOUND;
9431 }
9432
9433 aSnapshot = mData->mFirstSnapshot->i_findChildOrSelf(strName);
9434
9435 if (!aSnapshot)
9436 {
9437 if (aSetError)
9438 return setError(VBOX_E_OBJECT_NOT_FOUND,
9439 tr("Could not find a snapshot named '%s'"), strName.c_str());
9440 return VBOX_E_OBJECT_NOT_FOUND;
9441 }
9442
9443 return S_OK;
9444}
9445
9446/**
9447 * Returns a storage controller object with the given name.
9448 *
9449 * @param aName storage controller name to find
9450 * @param aStorageController where to return the found storage controller
9451 * @param aSetError true to set extended error info on failure
9452 */
9453HRESULT Machine::i_getStorageControllerByName(const Utf8Str &aName,
9454 ComObjPtr<StorageController> &aStorageController,
9455 bool aSetError /* = false */)
9456{
9457 AssertReturn(!aName.isEmpty(), E_INVALIDARG);
9458
9459 for (StorageControllerList::const_iterator it = mStorageControllers->begin();
9460 it != mStorageControllers->end();
9461 ++it)
9462 {
9463 if ((*it)->i_getName() == aName)
9464 {
9465 aStorageController = (*it);
9466 return S_OK;
9467 }
9468 }
9469
9470 if (aSetError)
9471 return setError(VBOX_E_OBJECT_NOT_FOUND,
9472 tr("Could not find a storage controller named '%s'"),
9473 aName.c_str());
9474 return VBOX_E_OBJECT_NOT_FOUND;
9475}
9476
9477/**
9478 * Returns a USB controller object with the given name.
9479 *
9480 * @param aName USB controller name to find
9481 * @param aUSBController where to return the found USB controller
9482 * @param aSetError true to set extended error info on failure
9483 */
9484HRESULT Machine::i_getUSBControllerByName(const Utf8Str &aName,
9485 ComObjPtr<USBController> &aUSBController,
9486 bool aSetError /* = false */)
9487{
9488 AssertReturn(!aName.isEmpty(), E_INVALIDARG);
9489
9490 for (USBControllerList::const_iterator it = mUSBControllers->begin();
9491 it != mUSBControllers->end();
9492 ++it)
9493 {
9494 if ((*it)->i_getName() == aName)
9495 {
9496 aUSBController = (*it);
9497 return S_OK;
9498 }
9499 }
9500
9501 if (aSetError)
9502 return setError(VBOX_E_OBJECT_NOT_FOUND,
9503 tr("Could not find a storage controller named '%s'"),
9504 aName.c_str());
9505 return VBOX_E_OBJECT_NOT_FOUND;
9506}
9507
9508/**
9509 * Returns the number of USB controller instance of the given type.
9510 *
9511 * @param enmType USB controller type.
9512 */
9513ULONG Machine::i_getUSBControllerCountByType(USBControllerType_T enmType)
9514{
9515 ULONG cCtrls = 0;
9516
9517 for (USBControllerList::const_iterator it = mUSBControllers->begin();
9518 it != mUSBControllers->end();
9519 ++it)
9520 {
9521 if ((*it)->i_getControllerType() == enmType)
9522 cCtrls++;
9523 }
9524
9525 return cCtrls;
9526}
9527
9528HRESULT Machine::i_getMediumAttachmentsOfController(const Utf8Str &aName,
9529 MediaData::AttachmentList &atts)
9530{
9531 AutoCaller autoCaller(this);
9532 if (FAILED(autoCaller.rc())) return autoCaller.rc();
9533
9534 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
9535
9536 for (MediaData::AttachmentList::iterator it = mMediaData->mAttachments.begin();
9537 it != mMediaData->mAttachments.end();
9538 ++it)
9539 {
9540 const ComObjPtr<MediumAttachment> &pAtt = *it;
9541 // should never happen, but deal with NULL pointers in the list.
9542 AssertStmt(!pAtt.isNull(), continue);
9543
9544 // getControllerName() needs caller+read lock
9545 AutoCaller autoAttCaller(pAtt);
9546 if (FAILED(autoAttCaller.rc()))
9547 {
9548 atts.clear();
9549 return autoAttCaller.rc();
9550 }
9551 AutoReadLock attLock(pAtt COMMA_LOCKVAL_SRC_POS);
9552
9553 if (pAtt->i_getControllerName() == Bstr(aName).raw())
9554 atts.push_back(pAtt);
9555 }
9556
9557 return S_OK;
9558}
9559
9560
9561/**
9562 * Helper for #i_saveSettings. Cares about renaming the settings directory and
9563 * file if the machine name was changed and about creating a new settings file
9564 * if this is a new machine.
9565 *
9566 * @note Must be never called directly but only from #saveSettings().
9567 */
9568HRESULT Machine::i_prepareSaveSettings(bool *pfNeedsGlobalSaveSettings)
9569{
9570 AssertReturn(isWriteLockOnCurrentThread(), E_FAIL);
9571
9572 HRESULT rc = S_OK;
9573
9574 bool fSettingsFileIsNew = !mData->pMachineConfigFile->fileExists();
9575
9576 /// @todo need to handle primary group change, too
9577
9578 /* attempt to rename the settings file if machine name is changed */
9579 if ( mUserData->s.fNameSync
9580 && mUserData.isBackedUp()
9581 && ( mUserData.backedUpData()->s.strName != mUserData->s.strName
9582 || mUserData.backedUpData()->s.llGroups.front() != mUserData->s.llGroups.front())
9583 )
9584 {
9585 bool dirRenamed = false;
9586 bool fileRenamed = false;
9587
9588 Utf8Str configFile, newConfigFile;
9589 Utf8Str configFilePrev, newConfigFilePrev;
9590 Utf8Str configDir, newConfigDir;
9591
9592 do
9593 {
9594 int vrc = VINF_SUCCESS;
9595
9596 Utf8Str name = mUserData.backedUpData()->s.strName;
9597 Utf8Str newName = mUserData->s.strName;
9598 Utf8Str group = mUserData.backedUpData()->s.llGroups.front();
9599 if (group == "/")
9600 group.setNull();
9601 Utf8Str newGroup = mUserData->s.llGroups.front();
9602 if (newGroup == "/")
9603 newGroup.setNull();
9604
9605 configFile = mData->m_strConfigFileFull;
9606
9607 /* first, rename the directory if it matches the group and machine name */
9608 Utf8Str groupPlusName = Utf8StrFmt("%s%c%s",
9609 group.c_str(), RTPATH_DELIMITER, name.c_str());
9610 /** @todo hack, make somehow use of ComposeMachineFilename */
9611 if (mUserData->s.fDirectoryIncludesUUID)
9612 groupPlusName += Utf8StrFmt(" (%RTuuid)", mData->mUuid.raw());
9613 Utf8Str newGroupPlusName = Utf8StrFmt("%s%c%s",
9614 newGroup.c_str(), RTPATH_DELIMITER, newName.c_str());
9615 /** @todo hack, make somehow use of ComposeMachineFilename */
9616 if (mUserData->s.fDirectoryIncludesUUID)
9617 newGroupPlusName += Utf8StrFmt(" (%RTuuid)", mData->mUuid.raw());
9618 configDir = configFile;
9619 configDir.stripFilename();
9620 newConfigDir = configDir;
9621 if ( configDir.length() >= groupPlusName.length()
9622 && !RTPathCompare(configDir.substr(configDir.length() - groupPlusName.length(), groupPlusName.length()).c_str(),
9623 groupPlusName.c_str()))
9624 {
9625 newConfigDir = newConfigDir.substr(0, configDir.length() - groupPlusName.length());
9626 Utf8Str newConfigBaseDir(newConfigDir);
9627 newConfigDir.append(newGroupPlusName);
9628 /* consistency: use \ if appropriate on the platform */
9629 RTPathChangeToDosSlashes(newConfigDir.mutableRaw(), false);
9630 /* new dir and old dir cannot be equal here because of 'if'
9631 * above and because name != newName */
9632 Assert(configDir != newConfigDir);
9633 if (!fSettingsFileIsNew)
9634 {
9635 /* perform real rename only if the machine is not new */
9636 vrc = RTPathRename(configDir.c_str(), newConfigDir.c_str(), 0);
9637 if ( vrc == VERR_FILE_NOT_FOUND
9638 || vrc == VERR_PATH_NOT_FOUND)
9639 {
9640 /* create the parent directory, then retry renaming */
9641 Utf8Str parent(newConfigDir);
9642 parent.stripFilename();
9643 (void)RTDirCreateFullPath(parent.c_str(), 0700);
9644 vrc = RTPathRename(configDir.c_str(), newConfigDir.c_str(), 0);
9645 }
9646 if (RT_FAILURE(vrc))
9647 {
9648 rc = setError(E_FAIL,
9649 tr("Could not rename the directory '%s' to '%s' to save the settings file (%Rrc)"),
9650 configDir.c_str(),
9651 newConfigDir.c_str(),
9652 vrc);
9653 break;
9654 }
9655 /* delete subdirectories which are no longer needed */
9656 Utf8Str dir(configDir);
9657 dir.stripFilename();
9658 while (dir != newConfigBaseDir && dir != ".")
9659 {
9660 vrc = RTDirRemove(dir.c_str());
9661 if (RT_FAILURE(vrc))
9662 break;
9663 dir.stripFilename();
9664 }
9665 dirRenamed = true;
9666 }
9667 }
9668
9669 newConfigFile = Utf8StrFmt("%s%c%s.vbox",
9670 newConfigDir.c_str(), RTPATH_DELIMITER, newName.c_str());
9671
9672 /* then try to rename the settings file itself */
9673 if (newConfigFile != configFile)
9674 {
9675 /* get the path to old settings file in renamed directory */
9676 configFile = Utf8StrFmt("%s%c%s",
9677 newConfigDir.c_str(),
9678 RTPATH_DELIMITER,
9679 RTPathFilename(configFile.c_str()));
9680 if (!fSettingsFileIsNew)
9681 {
9682 /* perform real rename only if the machine is not new */
9683 vrc = RTFileRename(configFile.c_str(), newConfigFile.c_str(), 0);
9684 if (RT_FAILURE(vrc))
9685 {
9686 rc = setError(E_FAIL,
9687 tr("Could not rename the settings file '%s' to '%s' (%Rrc)"),
9688 configFile.c_str(),
9689 newConfigFile.c_str(),
9690 vrc);
9691 break;
9692 }
9693 fileRenamed = true;
9694 configFilePrev = configFile;
9695 configFilePrev += "-prev";
9696 newConfigFilePrev = newConfigFile;
9697 newConfigFilePrev += "-prev";
9698 RTFileRename(configFilePrev.c_str(), newConfigFilePrev.c_str(), 0);
9699 }
9700 }
9701
9702 // update m_strConfigFileFull amd mConfigFile
9703 mData->m_strConfigFileFull = newConfigFile;
9704 // compute the relative path too
9705 mParent->i_copyPathRelativeToConfig(newConfigFile, mData->m_strConfigFile);
9706
9707 // store the old and new so that VirtualBox::i_saveSettings() can update
9708 // the media registry
9709 if ( mData->mRegistered
9710 && (configDir != newConfigDir || configFile != newConfigFile))
9711 {
9712 mParent->i_rememberMachineNameChangeForMedia(configDir, newConfigDir);
9713
9714 if (pfNeedsGlobalSaveSettings)
9715 *pfNeedsGlobalSaveSettings = true;
9716 }
9717
9718 // in the saved state file path, replace the old directory with the new directory
9719 if (RTPathStartsWith(mSSData->strStateFilePath.c_str(), configDir.c_str()))
9720 mSSData->strStateFilePath = newConfigDir.append(mSSData->strStateFilePath.c_str() + configDir.length());
9721
9722 // and do the same thing for the saved state file paths of all the online snapshots
9723 if (mData->mFirstSnapshot)
9724 mData->mFirstSnapshot->i_updateSavedStatePaths(configDir.c_str(),
9725 newConfigDir.c_str());
9726 }
9727 while (0);
9728
9729 if (FAILED(rc))
9730 {
9731 /* silently try to rename everything back */
9732 if (fileRenamed)
9733 {
9734 RTFileRename(newConfigFilePrev.c_str(), configFilePrev.c_str(), 0);
9735 RTFileRename(newConfigFile.c_str(), configFile.c_str(), 0);
9736 }
9737 if (dirRenamed)
9738 RTPathRename(newConfigDir.c_str(), configDir.c_str(), 0);
9739 }
9740
9741 if (FAILED(rc)) return rc;
9742 }
9743
9744 if (fSettingsFileIsNew)
9745 {
9746 /* create a virgin config file */
9747 int vrc = VINF_SUCCESS;
9748
9749 /* ensure the settings directory exists */
9750 Utf8Str path(mData->m_strConfigFileFull);
9751 path.stripFilename();
9752 if (!RTDirExists(path.c_str()))
9753 {
9754 vrc = RTDirCreateFullPath(path.c_str(), 0700);
9755 if (RT_FAILURE(vrc))
9756 {
9757 return setError(E_FAIL,
9758 tr("Could not create a directory '%s' to save the settings file (%Rrc)"),
9759 path.c_str(),
9760 vrc);
9761 }
9762 }
9763
9764 /* Note: open flags must correlate with RTFileOpen() in lockConfig() */
9765 path = Utf8Str(mData->m_strConfigFileFull);
9766 RTFILE f = NIL_RTFILE;
9767 vrc = RTFileOpen(&f, path.c_str(),
9768 RTFILE_O_READWRITE | RTFILE_O_CREATE | RTFILE_O_DENY_WRITE);
9769 if (RT_FAILURE(vrc))
9770 return setError(E_FAIL,
9771 tr("Could not create the settings file '%s' (%Rrc)"),
9772 path.c_str(),
9773 vrc);
9774 RTFileClose(f);
9775 }
9776
9777 return rc;
9778}
9779
9780/**
9781 * Saves and commits machine data, user data and hardware data.
9782 *
9783 * Note that on failure, the data remains uncommitted.
9784 *
9785 * @a aFlags may combine the following flags:
9786 *
9787 * - SaveS_ResetCurStateModified: Resets mData->mCurrentStateModified to FALSE.
9788 * Used when saving settings after an operation that makes them 100%
9789 * correspond to the settings from the current snapshot.
9790 * - SaveS_InformCallbacksAnyway: Callbacks will be informed even if
9791 * #isReallyModified() returns false. This is necessary for cases when we
9792 * change machine data directly, not through the backup()/commit() mechanism.
9793 * - SaveS_Force: settings will be saved without doing a deep compare of the
9794 * settings structures. This is used when this is called because snapshots
9795 * have changed to avoid the overhead of the deep compare.
9796 *
9797 * @note Must be called from under this object's write lock. Locks children for
9798 * writing.
9799 *
9800 * @param pfNeedsGlobalSaveSettings Optional pointer to a bool that must have been
9801 * initialized to false and that will be set to true by this function if
9802 * the caller must invoke VirtualBox::i_saveSettings() because the global
9803 * settings have changed. This will happen if a machine rename has been
9804 * saved and the global machine and media registries will therefore need
9805 * updating.
9806 */
9807HRESULT Machine::i_saveSettings(bool *pfNeedsGlobalSaveSettings,
9808 int aFlags /*= 0*/)
9809{
9810 LogFlowThisFuncEnter();
9811
9812 AssertReturn(isWriteLockOnCurrentThread(), E_FAIL);
9813
9814 /* make sure child objects are unable to modify the settings while we are
9815 * saving them */
9816 i_ensureNoStateDependencies();
9817
9818 AssertReturn(!i_isSnapshotMachine(),
9819 E_FAIL);
9820
9821 HRESULT rc = S_OK;
9822 bool fNeedsWrite = false;
9823
9824 /* First, prepare to save settings. It will care about renaming the
9825 * settings directory and file if the machine name was changed and about
9826 * creating a new settings file if this is a new machine. */
9827 rc = i_prepareSaveSettings(pfNeedsGlobalSaveSettings);
9828 if (FAILED(rc)) return rc;
9829
9830 // keep a pointer to the current settings structures
9831 settings::MachineConfigFile *pOldConfig = mData->pMachineConfigFile;
9832 settings::MachineConfigFile *pNewConfig = NULL;
9833
9834 try
9835 {
9836 // make a fresh one to have everyone write stuff into
9837 pNewConfig = new settings::MachineConfigFile(NULL);
9838 pNewConfig->copyBaseFrom(*mData->pMachineConfigFile);
9839
9840 // now go and copy all the settings data from COM to the settings structures
9841 // (this calles i_saveSettings() on all the COM objects in the machine)
9842 i_copyMachineDataToSettings(*pNewConfig);
9843
9844 if (aFlags & SaveS_ResetCurStateModified)
9845 {
9846 // this gets set by takeSnapshot() (if offline snapshot) and restoreSnapshot()
9847 mData->mCurrentStateModified = FALSE;
9848 fNeedsWrite = true; // always, no need to compare
9849 }
9850 else if (aFlags & SaveS_Force)
9851 {
9852 fNeedsWrite = true; // always, no need to compare
9853 }
9854 else
9855 {
9856 if (!mData->mCurrentStateModified)
9857 {
9858 // do a deep compare of the settings that we just saved with the settings
9859 // previously stored in the config file; this invokes MachineConfigFile::operator==
9860 // which does a deep compare of all the settings, which is expensive but less expensive
9861 // than writing out XML in vain
9862 bool fAnySettingsChanged = !(*pNewConfig == *pOldConfig);
9863
9864 // could still be modified if any settings changed
9865 mData->mCurrentStateModified = fAnySettingsChanged;
9866
9867 fNeedsWrite = fAnySettingsChanged;
9868 }
9869 else
9870 fNeedsWrite = true;
9871 }
9872
9873 pNewConfig->fCurrentStateModified = !!mData->mCurrentStateModified;
9874
9875 if (fNeedsWrite)
9876 // now spit it all out!
9877 pNewConfig->write(mData->m_strConfigFileFull);
9878
9879 mData->pMachineConfigFile = pNewConfig;
9880 delete pOldConfig;
9881 i_commit();
9882
9883 // after saving settings, we are no longer different from the XML on disk
9884 mData->flModifications = 0;
9885 }
9886 catch (HRESULT err)
9887 {
9888 // we assume that error info is set by the thrower
9889 rc = err;
9890
9891 // restore old config
9892 delete pNewConfig;
9893 mData->pMachineConfigFile = pOldConfig;
9894 }
9895 catch (...)
9896 {
9897 rc = VirtualBoxBase::handleUnexpectedExceptions(this, RT_SRC_POS);
9898 }
9899
9900 if (fNeedsWrite || (aFlags & SaveS_InformCallbacksAnyway))
9901 {
9902 /* Fire the data change event, even on failure (since we've already
9903 * committed all data). This is done only for SessionMachines because
9904 * mutable Machine instances are always not registered (i.e. private
9905 * to the client process that creates them) and thus don't need to
9906 * inform callbacks. */
9907 if (i_isSessionMachine())
9908 mParent->i_onMachineDataChange(mData->mUuid);
9909 }
9910
9911 LogFlowThisFunc(("rc=%08X\n", rc));
9912 LogFlowThisFuncLeave();
9913 return rc;
9914}
9915
9916/**
9917 * Implementation for saving the machine settings into the given
9918 * settings::MachineConfigFile instance. This copies machine extradata
9919 * from the previous machine config file in the instance data, if any.
9920 *
9921 * This gets called from two locations:
9922 *
9923 * -- Machine::i_saveSettings(), during the regular XML writing;
9924 *
9925 * -- Appliance::buildXMLForOneVirtualSystem(), when a machine gets
9926 * exported to OVF and we write the VirtualBox proprietary XML
9927 * into a <vbox:Machine> tag.
9928 *
9929 * This routine fills all the fields in there, including snapshots, *except*
9930 * for the following:
9931 *
9932 * -- fCurrentStateModified. There is some special logic associated with that.
9933 *
9934 * The caller can then call MachineConfigFile::write() or do something else
9935 * with it.
9936 *
9937 * Caller must hold the machine lock!
9938 *
9939 * This throws XML errors and HRESULT, so the caller must have a catch block!
9940 */
9941void Machine::i_copyMachineDataToSettings(settings::MachineConfigFile &config)
9942{
9943 // deep copy extradata
9944 config.mapExtraDataItems = mData->pMachineConfigFile->mapExtraDataItems;
9945
9946 config.uuid = mData->mUuid;
9947
9948 // copy name, description, OS type, teleport, UTC etc.
9949 config.machineUserData = mUserData->s;
9950
9951 // Encode the Icon Override data from Machine and store on config userdata.
9952 std::vector<BYTE> iconByte;
9953 getIcon(iconByte);
9954 ssize_t cbData = iconByte.size();
9955 if (cbData > 0)
9956 {
9957 ssize_t cchOut = RTBase64EncodedLength(cbData);
9958 Utf8Str strIconData;
9959 strIconData.reserve(cchOut+1);
9960 int vrc = RTBase64Encode(&iconByte.front(), cbData,
9961 strIconData.mutableRaw(), strIconData.capacity(),
9962 NULL);
9963 if (RT_FAILURE(vrc))
9964 throw setError(E_FAIL, tr("Failure to Encode Icon Data. '%s' (%Rrc)"), strIconData.mutableRaw(), vrc);
9965 strIconData.jolt();
9966 config.machineUserData.ovIcon = strIconData;
9967 }
9968 else
9969 config.machineUserData.ovIcon.setNull();
9970
9971 if ( mData->mMachineState == MachineState_Saved
9972 || mData->mMachineState == MachineState_Restoring
9973 // when doing certain snapshot operations we may or may not have
9974 // a saved state in the current state, so keep everything as is
9975 || ( ( mData->mMachineState == MachineState_Snapshotting
9976 || mData->mMachineState == MachineState_DeletingSnapshot
9977 || mData->mMachineState == MachineState_RestoringSnapshot)
9978 && (!mSSData->strStateFilePath.isEmpty())
9979 )
9980 )
9981 {
9982 Assert(!mSSData->strStateFilePath.isEmpty());
9983 /* try to make the file name relative to the settings file dir */
9984 i_copyPathRelativeToMachine(mSSData->strStateFilePath, config.strStateFile);
9985 }
9986 else
9987 {
9988 Assert(mSSData->strStateFilePath.isEmpty() || mData->mMachineState == MachineState_Saving);
9989 config.strStateFile.setNull();
9990 }
9991
9992 if (mData->mCurrentSnapshot)
9993 config.uuidCurrentSnapshot = mData->mCurrentSnapshot->i_getId();
9994 else
9995 config.uuidCurrentSnapshot.clear();
9996
9997 config.timeLastStateChange = mData->mLastStateChange;
9998 config.fAborted = (mData->mMachineState == MachineState_Aborted);
9999 /// @todo Live Migration: config.fTeleported = (mData->mMachineState == MachineState_Teleported);
10000
10001 HRESULT rc = i_saveHardware(config.hardwareMachine, &config.debugging, &config.autostart);
10002 if (FAILED(rc)) throw rc;
10003
10004 rc = i_saveStorageControllers(config.storageMachine);
10005 if (FAILED(rc)) throw rc;
10006
10007 // save machine's media registry if this is VirtualBox 4.0 or later
10008 if (config.canHaveOwnMediaRegistry())
10009 {
10010 // determine machine folder
10011 Utf8Str strMachineFolder = i_getSettingsFileFull();
10012 strMachineFolder.stripFilename();
10013 mParent->i_saveMediaRegistry(config.mediaRegistry,
10014 i_getId(), // only media with registry ID == machine UUID
10015 strMachineFolder);
10016 // this throws HRESULT
10017 }
10018
10019 // save snapshots
10020 rc = i_saveAllSnapshots(config);
10021 if (FAILED(rc)) throw rc;
10022}
10023
10024/**
10025 * Saves all snapshots of the machine into the given machine config file. Called
10026 * from Machine::buildMachineXML() and SessionMachine::deleteSnapshotHandler().
10027 * @param config
10028 * @return
10029 */
10030HRESULT Machine::i_saveAllSnapshots(settings::MachineConfigFile &config)
10031{
10032 AssertReturn(isWriteLockOnCurrentThread(), E_FAIL);
10033
10034 HRESULT rc = S_OK;
10035
10036 try
10037 {
10038 config.llFirstSnapshot.clear();
10039
10040 if (mData->mFirstSnapshot)
10041 {
10042 // the settings use a list for "the first snapshot"
10043 config.llFirstSnapshot.push_back(settings::g_SnapshotEmpty);
10044
10045 // get reference to the snapshot on the list and work on that
10046 // element straight in the list to avoid excessive copying later
10047 rc = mData->mFirstSnapshot->i_saveSnapshot(config.llFirstSnapshot.back());
10048 if (FAILED(rc)) throw rc;
10049 }
10050
10051// if (mType == IsSessionMachine)
10052// mParent->onMachineDataChange(mData->mUuid); @todo is this necessary?
10053
10054 }
10055 catch (HRESULT err)
10056 {
10057 /* we assume that error info is set by the thrower */
10058 rc = err;
10059 }
10060 catch (...)
10061 {
10062 rc = VirtualBoxBase::handleUnexpectedExceptions(this, RT_SRC_POS);
10063 }
10064
10065 return rc;
10066}
10067
10068/**
10069 * Saves the VM hardware configuration. It is assumed that the
10070 * given node is empty.
10071 *
10072 * @param data Reference to the settings object for the hardware config.
10073 * @param pDbg Pointer to the settings object for the debugging config
10074 * which happens to live in mHWData.
10075 * @param pAutostart Pointer to the settings object for the autostart config
10076 * which happens to live in mHWData.
10077 */
10078HRESULT Machine::i_saveHardware(settings::Hardware &data, settings::Debugging *pDbg,
10079 settings::Autostart *pAutostart)
10080{
10081 HRESULT rc = S_OK;
10082
10083 try
10084 {
10085 /* The hardware version attribute (optional).
10086 Automatically upgrade from 1 to 2 when there is no saved state. (ugly!) */
10087 if ( mHWData->mHWVersion == "1"
10088 && mSSData->strStateFilePath.isEmpty()
10089 )
10090 mHWData->mHWVersion = "2"; /** @todo Is this safe, to update mHWVersion here? If not some
10091 other point needs to be found where this can be done. */
10092
10093 data.strVersion = mHWData->mHWVersion;
10094 data.uuid = mHWData->mHardwareUUID;
10095
10096 // CPU
10097 data.fHardwareVirt = !!mHWData->mHWVirtExEnabled;
10098 data.fNestedPaging = !!mHWData->mHWVirtExNestedPagingEnabled;
10099 data.fLargePages = !!mHWData->mHWVirtExLargePagesEnabled;
10100 data.fVPID = !!mHWData->mHWVirtExVPIDEnabled;
10101 data.fUnrestrictedExecution = !!mHWData->mHWVirtExUXEnabled;
10102 data.fHardwareVirtForce = !!mHWData->mHWVirtExForceEnabled;
10103 data.fPAE = !!mHWData->mPAEEnabled;
10104 data.enmLongMode = mHWData->mLongMode;
10105 data.fTripleFaultReset = !!mHWData->mTripleFaultReset;
10106 data.cCPUs = mHWData->mCPUCount;
10107 data.fCpuHotPlug = !!mHWData->mCPUHotPlugEnabled;
10108 data.ulCpuExecutionCap = mHWData->mCpuExecutionCap;
10109 data.uCpuIdPortabilityLevel = mHWData->mCpuIdPortabilityLevel;
10110
10111 data.llCpus.clear();
10112 if (data.fCpuHotPlug)
10113 {
10114 for (unsigned idx = 0; idx < data.cCPUs; ++idx)
10115 {
10116 if (mHWData->mCPUAttached[idx])
10117 {
10118 settings::Cpu cpu;
10119 cpu.ulId = idx;
10120 data.llCpus.push_back(cpu);
10121 }
10122 }
10123 }
10124
10125 /* Standard and Extended CPUID leafs. */
10126 data.llCpuIdLeafs.clear();
10127 for (unsigned idx = 0; idx < RT_ELEMENTS(mHWData->mCpuIdStdLeafs); ++idx)
10128 if (mHWData->mCpuIdStdLeafs[idx].ulId != UINT32_MAX)
10129 data.llCpuIdLeafs.push_back(mHWData->mCpuIdStdLeafs[idx]);
10130 for (unsigned idx = 0; idx < RT_ELEMENTS(mHWData->mCpuIdExtLeafs); ++idx)
10131 if (mHWData->mCpuIdExtLeafs[idx].ulId != UINT32_MAX)
10132 data.llCpuIdLeafs.push_back(mHWData->mCpuIdExtLeafs[idx]);
10133
10134 // memory
10135 data.ulMemorySizeMB = mHWData->mMemorySize;
10136 data.fPageFusionEnabled = !!mHWData->mPageFusionEnabled;
10137
10138 // firmware
10139 data.firmwareType = mHWData->mFirmwareType;
10140
10141 // HID
10142 data.pointingHIDType = mHWData->mPointingHIDType;
10143 data.keyboardHIDType = mHWData->mKeyboardHIDType;
10144
10145 // chipset
10146 data.chipsetType = mHWData->mChipsetType;
10147
10148 // paravirt
10149 data.paravirtProvider = mHWData->mParavirtProvider;
10150
10151
10152 data.fEmulatedUSBCardReader = !!mHWData->mEmulatedUSBCardReaderEnabled;
10153
10154 // HPET
10155 data.fHPETEnabled = !!mHWData->mHPETEnabled;
10156
10157 // boot order
10158 data.mapBootOrder.clear();
10159 for (unsigned i = 0; i < RT_ELEMENTS(mHWData->mBootOrder); ++i)
10160 data.mapBootOrder[i] = mHWData->mBootOrder[i];
10161
10162 // display
10163 data.graphicsControllerType = mHWData->mGraphicsControllerType;
10164 data.ulVRAMSizeMB = mHWData->mVRAMSize;
10165 data.cMonitors = mHWData->mMonitorCount;
10166 data.fAccelerate3D = !!mHWData->mAccelerate3DEnabled;
10167 data.fAccelerate2DVideo = !!mHWData->mAccelerate2DVideoEnabled;
10168 data.ulVideoCaptureHorzRes = mHWData->mVideoCaptureWidth;
10169 data.ulVideoCaptureVertRes = mHWData->mVideoCaptureHeight;
10170 data.ulVideoCaptureRate = mHWData->mVideoCaptureRate;
10171 data.ulVideoCaptureFPS = mHWData->mVideoCaptureFPS;
10172 data.fVideoCaptureEnabled = !!mHWData->mVideoCaptureEnabled;
10173 for (unsigned i = 0; i < sizeof(data.u64VideoCaptureScreens) * 8; ++i)
10174 {
10175 if (mHWData->maVideoCaptureScreens[i])
10176 ASMBitSet(&data.u64VideoCaptureScreens, i);
10177 else
10178 ASMBitClear(&data.u64VideoCaptureScreens, i);
10179 }
10180 /* store relative video capture file if possible */
10181 i_copyPathRelativeToMachine(mHWData->mVideoCaptureFile, data.strVideoCaptureFile);
10182
10183 /* VRDEServer settings (optional) */
10184 rc = mVRDEServer->i_saveSettings(data.vrdeSettings);
10185 if (FAILED(rc)) throw rc;
10186
10187 /* BIOS (required) */
10188 rc = mBIOSSettings->i_saveSettings(data.biosSettings);
10189 if (FAILED(rc)) throw rc;
10190
10191 /* USB Controller (required) */
10192 for (USBControllerList::const_iterator it = mUSBControllers->begin(); it != mUSBControllers->end(); ++it)
10193 {
10194 ComObjPtr<USBController> ctrl = *it;
10195 settings::USBController settingsCtrl;
10196
10197 settingsCtrl.strName = ctrl->i_getName();
10198 settingsCtrl.enmType = ctrl->i_getControllerType();
10199
10200 data.usbSettings.llUSBControllers.push_back(settingsCtrl);
10201 }
10202
10203 /* USB device filters (required) */
10204 rc = mUSBDeviceFilters->i_saveSettings(data.usbSettings);
10205 if (FAILED(rc)) throw rc;
10206
10207 /* Network adapters (required) */
10208 size_t uMaxNICs = RT_MIN(Global::getMaxNetworkAdapters(mHWData->mChipsetType), mNetworkAdapters.size());
10209 data.llNetworkAdapters.clear();
10210 /* Write out only the nominal number of network adapters for this
10211 * chipset type. Since Machine::commit() hasn't been called there
10212 * may be extra NIC settings in the vector. */
10213 for (size_t slot = 0; slot < uMaxNICs; ++slot)
10214 {
10215 settings::NetworkAdapter nic;
10216 nic.ulSlot = (uint32_t)slot;
10217 /* paranoia check... must not be NULL, but must not crash either. */
10218 if (mNetworkAdapters[slot])
10219 {
10220 rc = mNetworkAdapters[slot]->i_saveSettings(nic);
10221 if (FAILED(rc)) throw rc;
10222
10223 data.llNetworkAdapters.push_back(nic);
10224 }
10225 }
10226
10227 /* Serial ports */
10228 data.llSerialPorts.clear();
10229 for (ULONG slot = 0; slot < RT_ELEMENTS(mSerialPorts); ++slot)
10230 {
10231 settings::SerialPort s;
10232 s.ulSlot = slot;
10233 rc = mSerialPorts[slot]->i_saveSettings(s);
10234 if (FAILED(rc)) return rc;
10235
10236 data.llSerialPorts.push_back(s);
10237 }
10238
10239 /* Parallel ports */
10240 data.llParallelPorts.clear();
10241 for (ULONG slot = 0; slot < RT_ELEMENTS(mParallelPorts); ++slot)
10242 {
10243 settings::ParallelPort p;
10244 p.ulSlot = slot;
10245 rc = mParallelPorts[slot]->i_saveSettings(p);
10246 if (FAILED(rc)) return rc;
10247
10248 data.llParallelPorts.push_back(p);
10249 }
10250
10251 /* Audio adapter */
10252 rc = mAudioAdapter->i_saveSettings(data.audioAdapter);
10253 if (FAILED(rc)) return rc;
10254
10255 /* Shared folders */
10256 data.llSharedFolders.clear();
10257 for (HWData::SharedFolderList::const_iterator it = mHWData->mSharedFolders.begin();
10258 it != mHWData->mSharedFolders.end();
10259 ++it)
10260 {
10261 SharedFolder *pSF = *it;
10262 AutoCaller sfCaller(pSF);
10263 AutoReadLock sfLock(pSF COMMA_LOCKVAL_SRC_POS);
10264 settings::SharedFolder sf;
10265 sf.strName = pSF->i_getName();
10266 sf.strHostPath = pSF->i_getHostPath();
10267 sf.fWritable = !!pSF->i_isWritable();
10268 sf.fAutoMount = !!pSF->i_isAutoMounted();
10269
10270 data.llSharedFolders.push_back(sf);
10271 }
10272
10273 // clipboard
10274 data.clipboardMode = mHWData->mClipboardMode;
10275
10276 // drag'n'drop
10277 data.dndMode = mHWData->mDnDMode;
10278
10279 /* Guest */
10280 data.ulMemoryBalloonSize = mHWData->mMemoryBalloonSize;
10281
10282 // IO settings
10283 data.ioSettings.fIOCacheEnabled = !!mHWData->mIOCacheEnabled;
10284 data.ioSettings.ulIOCacheSize = mHWData->mIOCacheSize;
10285
10286 /* BandwidthControl (required) */
10287 rc = mBandwidthControl->i_saveSettings(data.ioSettings);
10288 if (FAILED(rc)) throw rc;
10289
10290 /* Host PCI devices */
10291 for (HWData::PCIDeviceAssignmentList::const_iterator it = mHWData->mPCIDeviceAssignments.begin();
10292 it != mHWData->mPCIDeviceAssignments.end();
10293 ++it)
10294 {
10295 ComObjPtr<PCIDeviceAttachment> pda = *it;
10296 settings::HostPCIDeviceAttachment hpda;
10297
10298 rc = pda->i_saveSettings(hpda);
10299 if (FAILED(rc)) throw rc;
10300
10301 data.pciAttachments.push_back(hpda);
10302 }
10303
10304
10305 // guest properties
10306 data.llGuestProperties.clear();
10307#ifdef VBOX_WITH_GUEST_PROPS
10308 for (HWData::GuestPropertyMap::const_iterator it = mHWData->mGuestProperties.begin();
10309 it != mHWData->mGuestProperties.end();
10310 ++it)
10311 {
10312 HWData::GuestProperty property = it->second;
10313
10314 /* Remove transient guest properties at shutdown unless we
10315 * are saving state. Note that restoring snapshot intentionally
10316 * keeps them, they will be removed if appropriate once the final
10317 * machine state is set (as crashes etc. need to work). */
10318 if ( ( mData->mMachineState == MachineState_PoweredOff
10319 || mData->mMachineState == MachineState_Aborted
10320 || mData->mMachineState == MachineState_Teleported)
10321 && ( property.mFlags & guestProp::TRANSIENT
10322 || property.mFlags & guestProp::TRANSRESET))
10323 continue;
10324 settings::GuestProperty prop;
10325 prop.strName = it->first;
10326 prop.strValue = property.strValue;
10327 prop.timestamp = property.mTimestamp;
10328 char szFlags[guestProp::MAX_FLAGS_LEN + 1];
10329 guestProp::writeFlags(property.mFlags, szFlags);
10330 prop.strFlags = szFlags;
10331
10332 data.llGuestProperties.push_back(prop);
10333 }
10334
10335 /* I presume this doesn't require a backup(). */
10336 mData->mGuestPropertiesModified = FALSE;
10337#endif /* VBOX_WITH_GUEST_PROPS defined */
10338
10339 *pDbg = mHWData->mDebugging;
10340 *pAutostart = mHWData->mAutostart;
10341
10342 data.strDefaultFrontend = mHWData->mDefaultFrontend;
10343 }
10344 catch(std::bad_alloc &)
10345 {
10346 return E_OUTOFMEMORY;
10347 }
10348
10349 AssertComRC(rc);
10350 return rc;
10351}
10352
10353/**
10354 * Saves the storage controller configuration.
10355 *
10356 * @param aNode <StorageControllers> node to save the VM hardware configuration to.
10357 */
10358HRESULT Machine::i_saveStorageControllers(settings::Storage &data)
10359{
10360 data.llStorageControllers.clear();
10361
10362 for (StorageControllerList::const_iterator it = mStorageControllers->begin();
10363 it != mStorageControllers->end();
10364 ++it)
10365 {
10366 HRESULT rc;
10367 ComObjPtr<StorageController> pCtl = *it;
10368
10369 settings::StorageController ctl;
10370 ctl.strName = pCtl->i_getName();
10371 ctl.controllerType = pCtl->i_getControllerType();
10372 ctl.storageBus = pCtl->i_getStorageBus();
10373 ctl.ulInstance = pCtl->i_getInstance();
10374 ctl.fBootable = pCtl->i_getBootable();
10375
10376 /* Save the port count. */
10377 ULONG portCount;
10378 rc = pCtl->COMGETTER(PortCount)(&portCount);
10379 ComAssertComRCRet(rc, rc);
10380 ctl.ulPortCount = portCount;
10381
10382 /* Save fUseHostIOCache */
10383 BOOL fUseHostIOCache;
10384 rc = pCtl->COMGETTER(UseHostIOCache)(&fUseHostIOCache);
10385 ComAssertComRCRet(rc, rc);
10386 ctl.fUseHostIOCache = !!fUseHostIOCache;
10387
10388 /* save the devices now. */
10389 rc = i_saveStorageDevices(pCtl, ctl);
10390 ComAssertComRCRet(rc, rc);
10391
10392 data.llStorageControllers.push_back(ctl);
10393 }
10394
10395 return S_OK;
10396}
10397
10398/**
10399 * Saves the hard disk configuration.
10400 */
10401HRESULT Machine::i_saveStorageDevices(ComObjPtr<StorageController> aStorageController,
10402 settings::StorageController &data)
10403{
10404 MediaData::AttachmentList atts;
10405
10406 HRESULT rc = i_getMediumAttachmentsOfController(aStorageController->i_getName(), atts);
10407 if (FAILED(rc)) return rc;
10408
10409 data.llAttachedDevices.clear();
10410 for (MediaData::AttachmentList::const_iterator it = atts.begin();
10411 it != atts.end();
10412 ++it)
10413 {
10414 settings::AttachedDevice dev;
10415 IMediumAttachment *iA = *it;
10416 MediumAttachment *pAttach = static_cast<MediumAttachment *>(iA);
10417 Medium *pMedium = pAttach->i_getMedium();
10418
10419 dev.deviceType = pAttach->i_getType();
10420 dev.lPort = pAttach->i_getPort();
10421 dev.lDevice = pAttach->i_getDevice();
10422 dev.fPassThrough = pAttach->i_getPassthrough();
10423 dev.fHotPluggable = pAttach->i_getHotPluggable();
10424 if (pMedium)
10425 {
10426 if (pMedium->i_isHostDrive())
10427 dev.strHostDriveSrc = pMedium->i_getLocationFull();
10428 else
10429 dev.uuid = pMedium->i_getId();
10430 dev.fTempEject = pAttach->i_getTempEject();
10431 dev.fNonRotational = pAttach->i_getNonRotational();
10432 dev.fDiscard = pAttach->i_getDiscard();
10433 }
10434
10435 dev.strBwGroup = pAttach->i_getBandwidthGroup();
10436
10437 data.llAttachedDevices.push_back(dev);
10438 }
10439
10440 return S_OK;
10441}
10442
10443/**
10444 * Saves machine state settings as defined by aFlags
10445 * (SaveSTS_* values).
10446 *
10447 * @param aFlags Combination of SaveSTS_* flags.
10448 *
10449 * @note Locks objects for writing.
10450 */
10451HRESULT Machine::i_saveStateSettings(int aFlags)
10452{
10453 if (aFlags == 0)
10454 return S_OK;
10455
10456 AutoCaller autoCaller(this);
10457 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
10458
10459 /* This object's write lock is also necessary to serialize file access
10460 * (prevent concurrent reads and writes) */
10461 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
10462
10463 HRESULT rc = S_OK;
10464
10465 Assert(mData->pMachineConfigFile);
10466
10467 try
10468 {
10469 if (aFlags & SaveSTS_CurStateModified)
10470 mData->pMachineConfigFile->fCurrentStateModified = true;
10471
10472 if (aFlags & SaveSTS_StateFilePath)
10473 {
10474 if (!mSSData->strStateFilePath.isEmpty())
10475 /* try to make the file name relative to the settings file dir */
10476 i_copyPathRelativeToMachine(mSSData->strStateFilePath, mData->pMachineConfigFile->strStateFile);
10477 else
10478 mData->pMachineConfigFile->strStateFile.setNull();
10479 }
10480
10481 if (aFlags & SaveSTS_StateTimeStamp)
10482 {
10483 Assert( mData->mMachineState != MachineState_Aborted
10484 || mSSData->strStateFilePath.isEmpty());
10485
10486 mData->pMachineConfigFile->timeLastStateChange = mData->mLastStateChange;
10487
10488 mData->pMachineConfigFile->fAborted = (mData->mMachineState == MachineState_Aborted);
10489//@todo live migration mData->pMachineConfigFile->fTeleported = (mData->mMachineState == MachineState_Teleported);
10490 }
10491
10492 mData->pMachineConfigFile->write(mData->m_strConfigFileFull);
10493 }
10494 catch (...)
10495 {
10496 rc = VirtualBoxBase::handleUnexpectedExceptions(this, RT_SRC_POS);
10497 }
10498
10499 return rc;
10500}
10501
10502/**
10503 * Ensures that the given medium is added to a media registry. If this machine
10504 * was created with 4.0 or later, then the machine registry is used. Otherwise
10505 * the global VirtualBox media registry is used.
10506 *
10507 * Caller must NOT hold machine lock, media tree or any medium locks!
10508 *
10509 * @param pMedium
10510 */
10511void Machine::i_addMediumToRegistry(ComObjPtr<Medium> &pMedium)
10512{
10513 /* Paranoia checks: do not hold machine or media tree locks. */
10514 AssertReturnVoid(!isWriteLockOnCurrentThread());
10515 AssertReturnVoid(!mParent->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
10516
10517 ComObjPtr<Medium> pBase;
10518 {
10519 AutoReadLock treeLock(&mParent->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
10520 pBase = pMedium->i_getBase();
10521 }
10522
10523 /* Paranoia checks: do not hold medium locks. */
10524 AssertReturnVoid(!pMedium->isWriteLockOnCurrentThread());
10525 AssertReturnVoid(!pBase->isWriteLockOnCurrentThread());
10526
10527 // decide which medium registry to use now that the medium is attached:
10528 Guid uuid;
10529 if (mData->pMachineConfigFile->canHaveOwnMediaRegistry())
10530 // machine XML is VirtualBox 4.0 or higher:
10531 uuid = i_getId(); // machine UUID
10532 else
10533 uuid = mParent->i_getGlobalRegistryId(); // VirtualBox global registry UUID
10534
10535 if (pMedium->i_addRegistry(uuid))
10536 mParent->i_markRegistryModified(uuid);
10537
10538 /* For more complex hard disk structures it can happen that the base
10539 * medium isn't yet associated with any medium registry. Do that now. */
10540 if (pMedium != pBase)
10541 {
10542 /* Tree lock needed by Medium::addRegistry when recursing. */
10543 AutoReadLock treeLock(&mParent->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
10544 if (pBase->i_addRegistryRecursive(uuid))
10545 {
10546 treeLock.release();
10547 mParent->i_markRegistryModified(uuid);
10548 }
10549 }
10550}
10551
10552/**
10553 * Creates differencing hard disks for all normal hard disks attached to this
10554 * machine and a new set of attachments to refer to created disks.
10555 *
10556 * Used when taking a snapshot or when deleting the current state. Gets called
10557 * from SessionMachine::BeginTakingSnapshot() and SessionMachine::restoreSnapshotHandler().
10558 *
10559 * This method assumes that mMediaData contains the original hard disk attachments
10560 * it needs to create diffs for. On success, these attachments will be replaced
10561 * with the created diffs. On failure, #deleteImplicitDiffs() is implicitly
10562 * called to delete created diffs which will also rollback mMediaData and restore
10563 * whatever was backed up before calling this method.
10564 *
10565 * Attachments with non-normal hard disks are left as is.
10566 *
10567 * If @a aOnline is @c false then the original hard disks that require implicit
10568 * diffs will be locked for reading. Otherwise it is assumed that they are
10569 * already locked for writing (when the VM was started). Note that in the latter
10570 * case it is responsibility of the caller to lock the newly created diffs for
10571 * writing if this method succeeds.
10572 *
10573 * @param aProgress Progress object to run (must contain at least as
10574 * many operations left as the number of hard disks
10575 * attached).
10576 * @param aOnline Whether the VM was online prior to this operation.
10577 *
10578 * @note The progress object is not marked as completed, neither on success nor
10579 * on failure. This is a responsibility of the caller.
10580 *
10581 * @note Locks this object and the media tree for writing.
10582 */
10583HRESULT Machine::i_createImplicitDiffs(IProgress *aProgress,
10584 ULONG aWeight,
10585 bool aOnline)
10586{
10587 LogFlowThisFunc(("aOnline=%d\n", aOnline));
10588
10589 AutoCaller autoCaller(this);
10590 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
10591
10592 AutoMultiWriteLock2 alock(this->lockHandle(),
10593 &mParent->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
10594
10595 /* must be in a protective state because we release the lock below */
10596 AssertReturn( mData->mMachineState == MachineState_Snapshotting
10597 || mData->mMachineState == MachineState_OnlineSnapshotting
10598 || mData->mMachineState == MachineState_LiveSnapshotting
10599 || mData->mMachineState == MachineState_RestoringSnapshot
10600 || mData->mMachineState == MachineState_DeletingSnapshot
10601 , E_FAIL);
10602
10603 HRESULT rc = S_OK;
10604
10605 // use appropriate locked media map (online or offline)
10606 MediumLockListMap lockedMediaOffline;
10607 MediumLockListMap *lockedMediaMap;
10608 if (aOnline)
10609 lockedMediaMap = &mData->mSession.mLockedMedia;
10610 else
10611 lockedMediaMap = &lockedMediaOffline;
10612
10613 try
10614 {
10615 if (!aOnline)
10616 {
10617 /* lock all attached hard disks early to detect "in use"
10618 * situations before creating actual diffs */
10619 for (MediaData::AttachmentList::const_iterator it = mMediaData->mAttachments.begin();
10620 it != mMediaData->mAttachments.end();
10621 ++it)
10622 {
10623 MediumAttachment* pAtt = *it;
10624 if (pAtt->i_getType() == DeviceType_HardDisk)
10625 {
10626 Medium* pMedium = pAtt->i_getMedium();
10627 Assert(pMedium);
10628
10629 MediumLockList *pMediumLockList(new MediumLockList());
10630 alock.release();
10631 rc = pMedium->i_createMediumLockList(true /* fFailIfInaccessible */,
10632 false /* fMediumLockWrite */,
10633 false /* fMediumLockWriteAll */,
10634 NULL,
10635 *pMediumLockList);
10636 alock.acquire();
10637 if (FAILED(rc))
10638 {
10639 delete pMediumLockList;
10640 throw rc;
10641 }
10642 rc = lockedMediaMap->Insert(pAtt, pMediumLockList);
10643 if (FAILED(rc))
10644 {
10645 throw setError(rc,
10646 tr("Collecting locking information for all attached media failed"));
10647 }
10648 }
10649 }
10650
10651 /* Now lock all media. If this fails, nothing is locked. */
10652 alock.release();
10653 rc = lockedMediaMap->Lock();
10654 alock.acquire();
10655 if (FAILED(rc))
10656 {
10657 throw setError(rc,
10658 tr("Locking of attached media failed"));
10659 }
10660 }
10661
10662 /* remember the current list (note that we don't use backup() since
10663 * mMediaData may be already backed up) */
10664 MediaData::AttachmentList atts = mMediaData->mAttachments;
10665
10666 /* start from scratch */
10667 mMediaData->mAttachments.clear();
10668
10669 /* go through remembered attachments and create diffs for normal hard
10670 * disks and attach them */
10671 for (MediaData::AttachmentList::const_iterator it = atts.begin();
10672 it != atts.end();
10673 ++it)
10674 {
10675 MediumAttachment* pAtt = *it;
10676
10677 DeviceType_T devType = pAtt->i_getType();
10678 Medium* pMedium = pAtt->i_getMedium();
10679
10680 if ( devType != DeviceType_HardDisk
10681 || pMedium == NULL
10682 || pMedium->i_getType() != MediumType_Normal)
10683 {
10684 /* copy the attachment as is */
10685
10686 /** @todo the progress object created in SessionMachine::TakeSnaphot
10687 * only expects operations for hard disks. Later other
10688 * device types need to show up in the progress as well. */
10689 if (devType == DeviceType_HardDisk)
10690 {
10691 if (pMedium == NULL)
10692 aProgress->SetNextOperation(Bstr(tr("Skipping attachment without medium")).raw(),
10693 aWeight); // weight
10694 else
10695 aProgress->SetNextOperation(BstrFmt(tr("Skipping medium '%s'"),
10696 pMedium->i_getBase()->i_getName().c_str()).raw(),
10697 aWeight); // weight
10698 }
10699
10700 mMediaData->mAttachments.push_back(pAtt);
10701 continue;
10702 }
10703
10704 /* need a diff */
10705 aProgress->SetNextOperation(BstrFmt(tr("Creating differencing hard disk for '%s'"),
10706 pMedium->i_getBase()->i_getName().c_str()).raw(),
10707 aWeight); // weight
10708
10709 Utf8Str strFullSnapshotFolder;
10710 i_calculateFullPath(mUserData->s.strSnapshotFolder, strFullSnapshotFolder);
10711
10712 ComObjPtr<Medium> diff;
10713 diff.createObject();
10714 // store the diff in the same registry as the parent
10715 // (this cannot fail here because we can't create implicit diffs for
10716 // unregistered images)
10717 Guid uuidRegistryParent;
10718 bool fInRegistry = pMedium->i_getFirstRegistryMachineId(uuidRegistryParent);
10719 Assert(fInRegistry); NOREF(fInRegistry);
10720 rc = diff->init(mParent,
10721 pMedium->i_getPreferredDiffFormat(),
10722 strFullSnapshotFolder.append(RTPATH_SLASH_STR),
10723 uuidRegistryParent,
10724 DeviceType_HardDisk);
10725 if (FAILED(rc)) throw rc;
10726
10727 /** @todo r=bird: How is the locking and diff image cleaned up if we fail before
10728 * the push_back? Looks like we're going to release medium with the
10729 * wrong kind of lock (general issue with if we fail anywhere at all)
10730 * and an orphaned VDI in the snapshots folder. */
10731
10732 /* update the appropriate lock list */
10733 MediumLockList *pMediumLockList;
10734 rc = lockedMediaMap->Get(pAtt, pMediumLockList);
10735 AssertComRCThrowRC(rc);
10736 if (aOnline)
10737 {
10738 alock.release();
10739 /* The currently attached medium will be read-only, change
10740 * the lock type to read. */
10741 rc = pMediumLockList->Update(pMedium, false);
10742 alock.acquire();
10743 AssertComRCThrowRC(rc);
10744 }
10745
10746 /* release the locks before the potentially lengthy operation */
10747 alock.release();
10748 rc = pMedium->i_createDiffStorage(diff, MediumVariant_Standard,
10749 pMediumLockList,
10750 NULL /* aProgress */,
10751 true /* aWait */);
10752 alock.acquire();
10753 if (FAILED(rc)) throw rc;
10754
10755 /* actual lock list update is done in Medium::commitMedia */
10756
10757 rc = diff->i_addBackReference(mData->mUuid);
10758 AssertComRCThrowRC(rc);
10759
10760 /* add a new attachment */
10761 ComObjPtr<MediumAttachment> attachment;
10762 attachment.createObject();
10763 rc = attachment->init(this,
10764 diff,
10765 pAtt->i_getControllerName(),
10766 pAtt->i_getPort(),
10767 pAtt->i_getDevice(),
10768 DeviceType_HardDisk,
10769 true /* aImplicit */,
10770 false /* aPassthrough */,
10771 false /* aTempEject */,
10772 pAtt->i_getNonRotational(),
10773 pAtt->i_getDiscard(),
10774 pAtt->i_getHotPluggable(),
10775 pAtt->i_getBandwidthGroup());
10776 if (FAILED(rc)) throw rc;
10777
10778 rc = lockedMediaMap->ReplaceKey(pAtt, attachment);
10779 AssertComRCThrowRC(rc);
10780 mMediaData->mAttachments.push_back(attachment);
10781 }
10782 }
10783 catch (HRESULT aRC) { rc = aRC; }
10784
10785 /* unlock all hard disks we locked when there is no VM */
10786 if (!aOnline)
10787 {
10788 ErrorInfoKeeper eik;
10789
10790 HRESULT rc1 = lockedMediaMap->Clear();
10791 AssertComRC(rc1);
10792 }
10793
10794 return rc;
10795}
10796
10797/**
10798 * Deletes implicit differencing hard disks created either by
10799 * #createImplicitDiffs() or by #AttachDevice() and rolls back mMediaData.
10800 *
10801 * Note that to delete hard disks created by #AttachDevice() this method is
10802 * called from #fixupMedia() when the changes are rolled back.
10803 *
10804 * @note Locks this object and the media tree for writing.
10805 */
10806HRESULT Machine::i_deleteImplicitDiffs(bool aOnline)
10807{
10808 LogFlowThisFunc(("aOnline=%d\n", aOnline));
10809
10810 AutoCaller autoCaller(this);
10811 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
10812
10813 AutoMultiWriteLock2 alock(this->lockHandle(),
10814 &mParent->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
10815
10816 /* We absolutely must have backed up state. */
10817 AssertReturn(mMediaData.isBackedUp(), E_FAIL);
10818
10819 /* Check if there are any implicitly created diff images. */
10820 bool fImplicitDiffs = false;
10821 for (MediaData::AttachmentList::const_iterator it = mMediaData->mAttachments.begin();
10822 it != mMediaData->mAttachments.end();
10823 ++it)
10824 {
10825 const ComObjPtr<MediumAttachment> &pAtt = *it;
10826 if (pAtt->i_isImplicit())
10827 {
10828 fImplicitDiffs = true;
10829 break;
10830 }
10831 }
10832 /* If there is nothing to do, leave early. This saves lots of image locking
10833 * effort. It also avoids a MachineStateChanged event without real reason.
10834 * This is important e.g. when loading a VM config, because there should be
10835 * no events. Otherwise API clients can become thoroughly confused for
10836 * inaccessible VMs (the code for loading VM configs uses this method for
10837 * cleanup if the config makes no sense), as they take such events as an
10838 * indication that the VM is alive, and they would force the VM config to
10839 * be reread, leading to an endless loop. */
10840 if (!fImplicitDiffs)
10841 return S_OK;
10842
10843 HRESULT rc = S_OK;
10844 MachineState_T oldState = mData->mMachineState;
10845
10846 /* will release the lock before the potentially lengthy operation,
10847 * so protect with the special state (unless already protected) */
10848 if ( oldState != MachineState_Snapshotting
10849 && oldState != MachineState_OnlineSnapshotting
10850 && oldState != MachineState_LiveSnapshotting
10851 && oldState != MachineState_RestoringSnapshot
10852 && oldState != MachineState_DeletingSnapshot
10853 && oldState != MachineState_DeletingSnapshotOnline
10854 && oldState != MachineState_DeletingSnapshotPaused
10855 )
10856 i_setMachineState(MachineState_SettingUp);
10857
10858 // use appropriate locked media map (online or offline)
10859 MediumLockListMap lockedMediaOffline;
10860 MediumLockListMap *lockedMediaMap;
10861 if (aOnline)
10862 lockedMediaMap = &mData->mSession.mLockedMedia;
10863 else
10864 lockedMediaMap = &lockedMediaOffline;
10865
10866 try
10867 {
10868 if (!aOnline)
10869 {
10870 /* lock all attached hard disks early to detect "in use"
10871 * situations before deleting actual diffs */
10872 for (MediaData::AttachmentList::const_iterator it = mMediaData->mAttachments.begin();
10873 it != mMediaData->mAttachments.end();
10874 ++it)
10875 {
10876 MediumAttachment* pAtt = *it;
10877 if (pAtt->i_getType() == DeviceType_HardDisk)
10878 {
10879 Medium* pMedium = pAtt->i_getMedium();
10880 Assert(pMedium);
10881
10882 MediumLockList *pMediumLockList(new MediumLockList());
10883 alock.release();
10884 rc = pMedium->i_createMediumLockList(true /* fFailIfInaccessible */,
10885 false /* fMediumLockWrite */,
10886 false /* fMediumLockWriteAll */,
10887 NULL,
10888 *pMediumLockList);
10889 alock.acquire();
10890
10891 if (FAILED(rc))
10892 {
10893 delete pMediumLockList;
10894 throw rc;
10895 }
10896
10897 rc = lockedMediaMap->Insert(pAtt, pMediumLockList);
10898 if (FAILED(rc))
10899 throw rc;
10900 }
10901 }
10902
10903 if (FAILED(rc))
10904 throw rc;
10905 } // end of offline
10906
10907 /* Lock lists are now up to date and include implicitly created media */
10908
10909 /* Go through remembered attachments and delete all implicitly created
10910 * diffs and fix up the attachment information */
10911 const MediaData::AttachmentList &oldAtts = mMediaData.backedUpData()->mAttachments;
10912 MediaData::AttachmentList implicitAtts;
10913 for (MediaData::AttachmentList::const_iterator it = mMediaData->mAttachments.begin();
10914 it != mMediaData->mAttachments.end();
10915 ++it)
10916 {
10917 ComObjPtr<MediumAttachment> pAtt = *it;
10918 ComObjPtr<Medium> pMedium = pAtt->i_getMedium();
10919 if (pMedium.isNull())
10920 continue;
10921
10922 // Implicit attachments go on the list for deletion and back references are removed.
10923 if (pAtt->i_isImplicit())
10924 {
10925 /* Deassociate and mark for deletion */
10926 LogFlowThisFunc(("Detaching '%s', pending deletion\n", pAtt->i_getLogName()));
10927 rc = pMedium->i_removeBackReference(mData->mUuid);
10928 if (FAILED(rc))
10929 throw rc;
10930 implicitAtts.push_back(pAtt);
10931 continue;
10932 }
10933
10934 /* Was this medium attached before? */
10935 if (!i_findAttachment(oldAtts, pMedium))
10936 {
10937 /* no: de-associate */
10938 LogFlowThisFunc(("Detaching '%s', no deletion\n", pAtt->i_getLogName()));
10939 rc = pMedium->i_removeBackReference(mData->mUuid);
10940 if (FAILED(rc))
10941 throw rc;
10942 continue;
10943 }
10944 LogFlowThisFunc(("Not detaching '%s'\n", pAtt->i_getLogName()));
10945 }
10946
10947 /* If there are implicit attachments to delete, throw away the lock
10948 * map contents (which will unlock all media) since the medium
10949 * attachments will be rolled back. Below we need to completely
10950 * recreate the lock map anyway since it is infinitely complex to
10951 * do this incrementally (would need reconstructing each attachment
10952 * change, which would be extremely hairy). */
10953 if (implicitAtts.size() != 0)
10954 {
10955 ErrorInfoKeeper eik;
10956
10957 HRESULT rc1 = lockedMediaMap->Clear();
10958 AssertComRC(rc1);
10959 }
10960
10961 /* rollback hard disk changes */
10962 mMediaData.rollback();
10963
10964 MultiResult mrc(S_OK);
10965
10966 // Delete unused implicit diffs.
10967 if (implicitAtts.size() != 0)
10968 {
10969 alock.release();
10970
10971 for (MediaData::AttachmentList::const_iterator it = implicitAtts.begin(); it != implicitAtts.end(); ++it)
10972 {
10973 // Remove medium associated with this attachment.
10974 ComObjPtr<MediumAttachment> pAtt = *it;
10975 Assert(pAtt);
10976 LogFlowThisFunc(("Deleting '%s'\n", pAtt->i_getLogName()));
10977 ComObjPtr<Medium> pMedium = pAtt->i_getMedium();
10978 Assert(pMedium);
10979
10980 rc = pMedium->i_deleteStorage(NULL /*aProgress*/, true /*aWait*/);
10981 // continue on delete failure, just collect error messages
10982 AssertMsg(SUCCEEDED(rc), ("rc=%Rhrc it=%s hd=%s\n", rc, pAtt->i_getLogName(),
10983 pMedium->i_getLocationFull().c_str() ));
10984 mrc = rc;
10985 }
10986 // Clear the list of deleted implicit attachments now, while not
10987 // holding the lock, as it will ultimately trigger Medium::uninit()
10988 // calls which assume that the media tree lock isn't held.
10989 implicitAtts.clear();
10990
10991 alock.acquire();
10992
10993 /* if there is a VM recreate media lock map as mentioned above,
10994 * otherwise it is a waste of time and we leave things unlocked */
10995 if (aOnline)
10996 {
10997 const ComObjPtr<SessionMachine> pMachine = mData->mSession.mMachine;
10998 /* must never be NULL, but better safe than sorry */
10999 if (!pMachine.isNull())
11000 {
11001 alock.release();
11002 rc = mData->mSession.mMachine->i_lockMedia();
11003 alock.acquire();
11004 if (FAILED(rc))
11005 throw rc;
11006 }
11007 }
11008 }
11009 }
11010 catch (HRESULT aRC) {rc = aRC;}
11011
11012 if (mData->mMachineState == MachineState_SettingUp)
11013 i_setMachineState(oldState);
11014
11015 /* unlock all hard disks we locked when there is no VM */
11016 if (!aOnline)
11017 {
11018 ErrorInfoKeeper eik;
11019
11020 HRESULT rc1 = lockedMediaMap->Clear();
11021 AssertComRC(rc1);
11022 }
11023
11024 return rc;
11025}
11026
11027
11028/**
11029 * Looks through the given list of media attachments for one with the given parameters
11030 * and returns it, or NULL if not found. The list is a parameter so that backup lists
11031 * can be searched as well if needed.
11032 *
11033 * @param list
11034 * @param aControllerName
11035 * @param aControllerPort
11036 * @param aDevice
11037 * @return
11038 */
11039MediumAttachment* Machine::i_findAttachment(const MediaData::AttachmentList &ll,
11040 IN_BSTR aControllerName,
11041 LONG aControllerPort,
11042 LONG aDevice)
11043{
11044 for (MediaData::AttachmentList::const_iterator it = ll.begin(); it != ll.end(); ++it)
11045 {
11046 MediumAttachment *pAttach = *it;
11047 if (pAttach->i_matches(aControllerName, aControllerPort, aDevice))
11048 return pAttach;
11049 }
11050
11051 return NULL;
11052}
11053
11054/**
11055 * Looks through the given list of media attachments for one with the given parameters
11056 * and returns it, or NULL if not found. The list is a parameter so that backup lists
11057 * can be searched as well if needed.
11058 *
11059 * @param list
11060 * @param aControllerName
11061 * @param aControllerPort
11062 * @param aDevice
11063 * @return
11064 */
11065MediumAttachment* Machine::i_findAttachment(const MediaData::AttachmentList &ll,
11066 ComObjPtr<Medium> pMedium)
11067{
11068 for (MediaData::AttachmentList::const_iterator it = ll.begin(); it != ll.end(); ++it)
11069 {
11070 MediumAttachment *pAttach = *it;
11071 ComObjPtr<Medium> pMediumThis = pAttach->i_getMedium();
11072 if (pMediumThis == pMedium)
11073 return pAttach;
11074 }
11075
11076 return NULL;
11077}
11078
11079/**
11080 * Looks through the given list of media attachments for one with the given parameters
11081 * and returns it, or NULL if not found. The list is a parameter so that backup lists
11082 * can be searched as well if needed.
11083 *
11084 * @param list
11085 * @param aControllerName
11086 * @param aControllerPort
11087 * @param aDevice
11088 * @return
11089 */
11090MediumAttachment* Machine::i_findAttachment(const MediaData::AttachmentList &ll,
11091 Guid &id)
11092{
11093 for (MediaData::AttachmentList::const_iterator it = ll.begin(); it != ll.end(); ++it)
11094 {
11095 MediumAttachment *pAttach = *it;
11096 ComObjPtr<Medium> pMediumThis = pAttach->i_getMedium();
11097 if (pMediumThis->i_getId() == id)
11098 return pAttach;
11099 }
11100
11101 return NULL;
11102}
11103
11104/**
11105 * Main implementation for Machine::DetachDevice. This also gets called
11106 * from Machine::prepareUnregister() so it has been taken out for simplicity.
11107 *
11108 * @param pAttach Medium attachment to detach.
11109 * @param writeLock Machine write lock which the caller must have locked once. This may be released temporarily in here.
11110 * @param pSnapshot If NULL, then the detachment is for the current machine. Otherwise this is for a
11111 * SnapshotMachine, and this must be its snapshot.
11112 * @return
11113 */
11114HRESULT Machine::i_detachDevice(MediumAttachment *pAttach,
11115 AutoWriteLock &writeLock,
11116 Snapshot *pSnapshot)
11117{
11118 ComObjPtr<Medium> oldmedium = pAttach->i_getMedium();
11119 DeviceType_T mediumType = pAttach->i_getType();
11120
11121 LogFlowThisFunc(("Entering, medium of attachment is %s\n", oldmedium ? oldmedium->i_getLocationFull().c_str() : "NULL"));
11122
11123 if (pAttach->i_isImplicit())
11124 {
11125 /* attempt to implicitly delete the implicitly created diff */
11126
11127 /// @todo move the implicit flag from MediumAttachment to Medium
11128 /// and forbid any hard disk operation when it is implicit. Or maybe
11129 /// a special media state for it to make it even more simple.
11130
11131 Assert(mMediaData.isBackedUp());
11132
11133 /* will release the lock before the potentially lengthy operation, so
11134 * protect with the special state */
11135 MachineState_T oldState = mData->mMachineState;
11136 i_setMachineState(MachineState_SettingUp);
11137
11138 writeLock.release();
11139
11140 HRESULT rc = oldmedium->i_deleteStorage(NULL /*aProgress*/,
11141 true /*aWait*/);
11142
11143 writeLock.acquire();
11144
11145 i_setMachineState(oldState);
11146
11147 if (FAILED(rc)) return rc;
11148 }
11149
11150 i_setModified(IsModified_Storage);
11151 mMediaData.backup();
11152 mMediaData->mAttachments.remove(pAttach);
11153
11154 if (!oldmedium.isNull())
11155 {
11156 // if this is from a snapshot, do not defer detachment to commitMedia()
11157 if (pSnapshot)
11158 oldmedium->i_removeBackReference(mData->mUuid, pSnapshot->i_getId());
11159 // else if non-hard disk media, do not defer detachment to commitMedia() either
11160 else if (mediumType != DeviceType_HardDisk)
11161 oldmedium->i_removeBackReference(mData->mUuid);
11162 }
11163
11164 return S_OK;
11165}
11166
11167/**
11168 * Goes thru all media of the given list and
11169 *
11170 * 1) calls i_detachDevice() on each of them for this machine and
11171 * 2) adds all Medium objects found in the process to the given list,
11172 * depending on cleanupMode.
11173 *
11174 * If cleanupMode is CleanupMode_DetachAllReturnHardDisksOnly, this only
11175 * adds hard disks to the list. If it is CleanupMode_Full, this adds all
11176 * media to the list.
11177 *
11178 * This gets called from Machine::Unregister, both for the actual Machine and
11179 * the SnapshotMachine objects that might be found in the snapshots.
11180 *
11181 * Requires caller and locking. The machine lock must be passed in because it
11182 * will be passed on to i_detachDevice which needs it for temporary unlocking.
11183 *
11184 * @param writeLock Machine lock from top-level caller; this gets passed to i_detachDevice.
11185 * @param pSnapshot Must be NULL when called for a "real" Machine or a snapshot object if called for a SnapshotMachine.
11186 * @param cleanupMode If DetachAllReturnHardDisksOnly, only hard disk media get added to llMedia; if
11187 * Full, then all media get added;
11188 * otherwise no media get added.
11189 * @param llMedia Caller's list to receive Medium objects which got detached so caller can close() them, depending on cleanupMode.
11190 * @return
11191 */
11192HRESULT Machine::i_detachAllMedia(AutoWriteLock &writeLock,
11193 Snapshot *pSnapshot,
11194 CleanupMode_T cleanupMode,
11195 MediaList &llMedia)
11196{
11197 Assert(isWriteLockOnCurrentThread());
11198
11199 HRESULT rc;
11200
11201 // make a temporary list because i_detachDevice invalidates iterators into
11202 // mMediaData->mAttachments
11203 MediaData::AttachmentList llAttachments2 = mMediaData->mAttachments;
11204
11205 for (MediaData::AttachmentList::iterator it = llAttachments2.begin(); it != llAttachments2.end(); ++it)
11206 {
11207 ComObjPtr<MediumAttachment> &pAttach = *it;
11208 ComObjPtr<Medium> pMedium = pAttach->i_getMedium();
11209
11210 if (!pMedium.isNull())
11211 {
11212 AutoCaller mac(pMedium);
11213 if (FAILED(mac.rc())) return mac.rc();
11214 AutoReadLock lock(pMedium COMMA_LOCKVAL_SRC_POS);
11215 DeviceType_T devType = pMedium->i_getDeviceType();
11216 if ( ( cleanupMode == CleanupMode_DetachAllReturnHardDisksOnly
11217 && devType == DeviceType_HardDisk)
11218 || (cleanupMode == CleanupMode_Full)
11219 )
11220 {
11221 llMedia.push_back(pMedium);
11222 ComObjPtr<Medium> pParent = pMedium->i_getParent();
11223 /* Not allowed to keep this lock as below we need the parent
11224 * medium lock, and the lock order is parent to child. */
11225 lock.release();
11226 /*
11227 * Search for medias which are not attached to any machine, but
11228 * in the chain to an attached disk. Mediums are only consided
11229 * if they are:
11230 * - have only one child
11231 * - no references to any machines
11232 * - are of normal medium type
11233 */
11234 while (!pParent.isNull())
11235 {
11236 AutoCaller mac1(pParent);
11237 if (FAILED(mac1.rc())) return mac1.rc();
11238 AutoReadLock lock1(pParent COMMA_LOCKVAL_SRC_POS);
11239 if (pParent->i_getChildren().size() == 1)
11240 {
11241 if ( pParent->i_getMachineBackRefCount() == 0
11242 && pParent->i_getType() == MediumType_Normal
11243 && find(llMedia.begin(), llMedia.end(), pParent) == llMedia.end())
11244 llMedia.push_back(pParent);
11245 }
11246 else
11247 break;
11248 pParent = pParent->i_getParent();
11249 }
11250 }
11251 }
11252
11253 // real machine: then we need to use the proper method
11254 rc = i_detachDevice(pAttach, writeLock, pSnapshot);
11255
11256 if (FAILED(rc))
11257 return rc;
11258 }
11259
11260 return S_OK;
11261}
11262
11263/**
11264 * Perform deferred hard disk detachments.
11265 *
11266 * Does nothing if the hard disk attachment data (mMediaData) is not changed (not
11267 * backed up).
11268 *
11269 * If @a aOnline is @c true then this method will also unlock the old hard disks
11270 * for which the new implicit diffs were created and will lock these new diffs for
11271 * writing.
11272 *
11273 * @param aOnline Whether the VM was online prior to this operation.
11274 *
11275 * @note Locks this object for writing!
11276 */
11277void Machine::i_commitMedia(bool aOnline /*= false*/)
11278{
11279 AutoCaller autoCaller(this);
11280 AssertComRCReturnVoid(autoCaller.rc());
11281
11282 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
11283
11284 LogFlowThisFunc(("Entering, aOnline=%d\n", aOnline));
11285
11286 HRESULT rc = S_OK;
11287
11288 /* no attach/detach operations -- nothing to do */
11289 if (!mMediaData.isBackedUp())
11290 return;
11291
11292 MediaData::AttachmentList &oldAtts = mMediaData.backedUpData()->mAttachments;
11293 bool fMediaNeedsLocking = false;
11294
11295 /* enumerate new attachments */
11296 for (MediaData::AttachmentList::const_iterator it = mMediaData->mAttachments.begin();
11297 it != mMediaData->mAttachments.end();
11298 ++it)
11299 {
11300 MediumAttachment *pAttach = *it;
11301
11302 pAttach->i_commit();
11303
11304 Medium* pMedium = pAttach->i_getMedium();
11305 bool fImplicit = pAttach->i_isImplicit();
11306
11307 LogFlowThisFunc(("Examining current medium '%s' (implicit: %d)\n",
11308 (pMedium) ? pMedium->i_getName().c_str() : "NULL",
11309 fImplicit));
11310
11311 /** @todo convert all this Machine-based voodoo to MediumAttachment
11312 * based commit logic. */
11313 if (fImplicit)
11314 {
11315 /* convert implicit attachment to normal */
11316 pAttach->i_setImplicit(false);
11317
11318 if ( aOnline
11319 && pMedium
11320 && pAttach->i_getType() == DeviceType_HardDisk
11321 )
11322 {
11323 /* update the appropriate lock list */
11324 MediumLockList *pMediumLockList;
11325 rc = mData->mSession.mLockedMedia.Get(pAttach, pMediumLockList);
11326 AssertComRC(rc);
11327 if (pMediumLockList)
11328 {
11329 /* unlock if there's a need to change the locking */
11330 if (!fMediaNeedsLocking)
11331 {
11332 rc = mData->mSession.mLockedMedia.Unlock();
11333 AssertComRC(rc);
11334 fMediaNeedsLocking = true;
11335 }
11336 rc = pMediumLockList->Update(pMedium->i_getParent(), false);
11337 AssertComRC(rc);
11338 rc = pMediumLockList->Append(pMedium, true);
11339 AssertComRC(rc);
11340 }
11341 }
11342
11343 continue;
11344 }
11345
11346 if (pMedium)
11347 {
11348 /* was this medium attached before? */
11349 for (MediaData::AttachmentList::iterator oldIt = oldAtts.begin(); oldIt != oldAtts.end(); ++oldIt)
11350 {
11351 MediumAttachment *pOldAttach = *oldIt;
11352 if (pOldAttach->i_getMedium() == pMedium)
11353 {
11354 LogFlowThisFunc(("--> medium '%s' was attached before, will not remove\n", pMedium->i_getName().c_str()));
11355
11356 /* yes: remove from old to avoid de-association */
11357 oldAtts.erase(oldIt);
11358 break;
11359 }
11360 }
11361 }
11362 }
11363
11364 /* enumerate remaining old attachments and de-associate from the
11365 * current machine state */
11366 for (MediaData::AttachmentList::const_iterator it = oldAtts.begin(); it != oldAtts.end(); ++it)
11367 {
11368 MediumAttachment *pAttach = *it;
11369 Medium* pMedium = pAttach->i_getMedium();
11370
11371 /* Detach only hard disks, since DVD/floppy media is detached
11372 * instantly in MountMedium. */
11373 if (pAttach->i_getType() == DeviceType_HardDisk && pMedium)
11374 {
11375 LogFlowThisFunc(("detaching medium '%s' from machine\n", pMedium->i_getName().c_str()));
11376
11377 /* now de-associate from the current machine state */
11378 rc = pMedium->i_removeBackReference(mData->mUuid);
11379 AssertComRC(rc);
11380
11381 if (aOnline)
11382 {
11383 /* unlock since medium is not used anymore */
11384 MediumLockList *pMediumLockList;
11385 rc = mData->mSession.mLockedMedia.Get(pAttach, pMediumLockList);
11386 if (RT_UNLIKELY(rc == VBOX_E_INVALID_OBJECT_STATE))
11387 {
11388 /* this happens for online snapshots, there the attachment
11389 * is changing, but only to a diff image created under
11390 * the old one, so there is no separate lock list */
11391 Assert(!pMediumLockList);
11392 }
11393 else
11394 {
11395 AssertComRC(rc);
11396 if (pMediumLockList)
11397 {
11398 rc = mData->mSession.mLockedMedia.Remove(pAttach);
11399 AssertComRC(rc);
11400 }
11401 }
11402 }
11403 }
11404 }
11405
11406 /* take media locks again so that the locking state is consistent */
11407 if (fMediaNeedsLocking)
11408 {
11409 Assert(aOnline);
11410 rc = mData->mSession.mLockedMedia.Lock();
11411 AssertComRC(rc);
11412 }
11413
11414 /* commit the hard disk changes */
11415 mMediaData.commit();
11416
11417 if (i_isSessionMachine())
11418 {
11419 /*
11420 * Update the parent machine to point to the new owner.
11421 * This is necessary because the stored parent will point to the
11422 * session machine otherwise and cause crashes or errors later
11423 * when the session machine gets invalid.
11424 */
11425 /** @todo Change the MediumAttachment class to behave like any other
11426 * class in this regard by creating peer MediumAttachment
11427 * objects for session machines and share the data with the peer
11428 * machine.
11429 */
11430 for (MediaData::AttachmentList::const_iterator it = mMediaData->mAttachments.begin();
11431 it != mMediaData->mAttachments.end();
11432 ++it)
11433 (*it)->i_updateParentMachine(mPeer);
11434
11435 /* attach new data to the primary machine and reshare it */
11436 mPeer->mMediaData.attach(mMediaData);
11437 }
11438
11439 return;
11440}
11441
11442/**
11443 * Perform deferred deletion of implicitly created diffs.
11444 *
11445 * Does nothing if the hard disk attachment data (mMediaData) is not changed (not
11446 * backed up).
11447 *
11448 * @note Locks this object for writing!
11449 */
11450void Machine::i_rollbackMedia()
11451{
11452 AutoCaller autoCaller(this);
11453 AssertComRCReturnVoid(autoCaller.rc());
11454
11455 // AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
11456 LogFlowThisFunc(("Entering rollbackMedia\n"));
11457
11458 HRESULT rc = S_OK;
11459
11460 /* no attach/detach operations -- nothing to do */
11461 if (!mMediaData.isBackedUp())
11462 return;
11463
11464 /* enumerate new attachments */
11465 for (MediaData::AttachmentList::const_iterator it = mMediaData->mAttachments.begin();
11466 it != mMediaData->mAttachments.end();
11467 ++it)
11468 {
11469 MediumAttachment *pAttach = *it;
11470 /* Fix up the backrefs for DVD/floppy media. */
11471 if (pAttach->i_getType() != DeviceType_HardDisk)
11472 {
11473 Medium* pMedium = pAttach->i_getMedium();
11474 if (pMedium)
11475 {
11476 rc = pMedium->i_removeBackReference(mData->mUuid);
11477 AssertComRC(rc);
11478 }
11479 }
11480
11481 (*it)->i_rollback();
11482
11483 pAttach = *it;
11484 /* Fix up the backrefs for DVD/floppy media. */
11485 if (pAttach->i_getType() != DeviceType_HardDisk)
11486 {
11487 Medium* pMedium = pAttach->i_getMedium();
11488 if (pMedium)
11489 {
11490 rc = pMedium->i_addBackReference(mData->mUuid);
11491 AssertComRC(rc);
11492 }
11493 }
11494 }
11495
11496 /** @todo convert all this Machine-based voodoo to MediumAttachment
11497 * based rollback logic. */
11498 i_deleteImplicitDiffs(Global::IsOnline(mData->mMachineState));
11499
11500 return;
11501}
11502
11503/**
11504 * Returns true if the settings file is located in the directory named exactly
11505 * as the machine; this means, among other things, that the machine directory
11506 * should be auto-renamed.
11507 *
11508 * @param aSettingsDir if not NULL, the full machine settings file directory
11509 * name will be assigned there.
11510 *
11511 * @note Doesn't lock anything.
11512 * @note Not thread safe (must be called from this object's lock).
11513 */
11514bool Machine::i_isInOwnDir(Utf8Str *aSettingsDir /* = NULL */) const
11515{
11516 Utf8Str strMachineDirName(mData->m_strConfigFileFull); // path/to/machinesfolder/vmname/vmname.vbox
11517 strMachineDirName.stripFilename(); // path/to/machinesfolder/vmname
11518 if (aSettingsDir)
11519 *aSettingsDir = strMachineDirName;
11520 strMachineDirName.stripPath(); // vmname
11521 Utf8Str strConfigFileOnly(mData->m_strConfigFileFull); // path/to/machinesfolder/vmname/vmname.vbox
11522 strConfigFileOnly.stripPath() // vmname.vbox
11523 .stripSuffix(); // vmname
11524 /** @todo hack, make somehow use of ComposeMachineFilename */
11525 if (mUserData->s.fDirectoryIncludesUUID)
11526 strConfigFileOnly += Utf8StrFmt(" (%RTuuid)", mData->mUuid.raw());
11527
11528 AssertReturn(!strMachineDirName.isEmpty(), false);
11529 AssertReturn(!strConfigFileOnly.isEmpty(), false);
11530
11531 return strMachineDirName == strConfigFileOnly;
11532}
11533
11534/**
11535 * Discards all changes to machine settings.
11536 *
11537 * @param aNotify Whether to notify the direct session about changes or not.
11538 *
11539 * @note Locks objects for writing!
11540 */
11541void Machine::i_rollback(bool aNotify)
11542{
11543 AutoCaller autoCaller(this);
11544 AssertComRCReturn(autoCaller.rc(), (void)0);
11545
11546 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
11547
11548 if (!mStorageControllers.isNull())
11549 {
11550 if (mStorageControllers.isBackedUp())
11551 {
11552 /* unitialize all new devices (absent in the backed up list). */
11553 StorageControllerList::const_iterator it = mStorageControllers->begin();
11554 StorageControllerList *backedList = mStorageControllers.backedUpData();
11555 while (it != mStorageControllers->end())
11556 {
11557 if ( std::find(backedList->begin(), backedList->end(), *it)
11558 == backedList->end()
11559 )
11560 {
11561 (*it)->uninit();
11562 }
11563 ++it;
11564 }
11565
11566 /* restore the list */
11567 mStorageControllers.rollback();
11568 }
11569
11570 /* rollback any changes to devices after restoring the list */
11571 if (mData->flModifications & IsModified_Storage)
11572 {
11573 StorageControllerList::const_iterator it = mStorageControllers->begin();
11574 while (it != mStorageControllers->end())
11575 {
11576 (*it)->i_rollback();
11577 ++it;
11578 }
11579 }
11580 }
11581
11582 if (!mUSBControllers.isNull())
11583 {
11584 if (mUSBControllers.isBackedUp())
11585 {
11586 /* unitialize all new devices (absent in the backed up list). */
11587 USBControllerList::const_iterator it = mUSBControllers->begin();
11588 USBControllerList *backedList = mUSBControllers.backedUpData();
11589 while (it != mUSBControllers->end())
11590 {
11591 if ( std::find(backedList->begin(), backedList->end(), *it)
11592 == backedList->end()
11593 )
11594 {
11595 (*it)->uninit();
11596 }
11597 ++it;
11598 }
11599
11600 /* restore the list */
11601 mUSBControllers.rollback();
11602 }
11603
11604 /* rollback any changes to devices after restoring the list */
11605 if (mData->flModifications & IsModified_USB)
11606 {
11607 USBControllerList::const_iterator it = mUSBControllers->begin();
11608 while (it != mUSBControllers->end())
11609 {
11610 (*it)->i_rollback();
11611 ++it;
11612 }
11613 }
11614 }
11615
11616 mUserData.rollback();
11617
11618 mHWData.rollback();
11619
11620 if (mData->flModifications & IsModified_Storage)
11621 i_rollbackMedia();
11622
11623 if (mBIOSSettings)
11624 mBIOSSettings->i_rollback();
11625
11626 if (mVRDEServer && (mData->flModifications & IsModified_VRDEServer))
11627 mVRDEServer->i_rollback();
11628
11629 if (mAudioAdapter)
11630 mAudioAdapter->i_rollback();
11631
11632 if (mUSBDeviceFilters && (mData->flModifications & IsModified_USB))
11633 mUSBDeviceFilters->i_rollback();
11634
11635 if (mBandwidthControl && (mData->flModifications & IsModified_BandwidthControl))
11636 mBandwidthControl->i_rollback();
11637
11638 if (!mHWData.isNull())
11639 mNetworkAdapters.resize(Global::getMaxNetworkAdapters(mHWData->mChipsetType));
11640 NetworkAdapterVector networkAdapters(mNetworkAdapters.size());
11641 ComPtr<ISerialPort> serialPorts[RT_ELEMENTS(mSerialPorts)];
11642 ComPtr<IParallelPort> parallelPorts[RT_ELEMENTS(mParallelPorts)];
11643
11644 if (mData->flModifications & IsModified_NetworkAdapters)
11645 for (ULONG slot = 0; slot < mNetworkAdapters.size(); ++slot)
11646 if ( mNetworkAdapters[slot]
11647 && mNetworkAdapters[slot]->i_isModified())
11648 {
11649 mNetworkAdapters[slot]->i_rollback();
11650 networkAdapters[slot] = mNetworkAdapters[slot];
11651 }
11652
11653 if (mData->flModifications & IsModified_SerialPorts)
11654 for (ULONG slot = 0; slot < RT_ELEMENTS(mSerialPorts); ++slot)
11655 if ( mSerialPorts[slot]
11656 && mSerialPorts[slot]->i_isModified())
11657 {
11658 mSerialPorts[slot]->i_rollback();
11659 serialPorts[slot] = mSerialPorts[slot];
11660 }
11661
11662 if (mData->flModifications & IsModified_ParallelPorts)
11663 for (ULONG slot = 0; slot < RT_ELEMENTS(mParallelPorts); ++slot)
11664 if ( mParallelPorts[slot]
11665 && mParallelPorts[slot]->i_isModified())
11666 {
11667 mParallelPorts[slot]->i_rollback();
11668 parallelPorts[slot] = mParallelPorts[slot];
11669 }
11670
11671 if (aNotify)
11672 {
11673 /* inform the direct session about changes */
11674
11675 ComObjPtr<Machine> that = this;
11676 uint32_t flModifications = mData->flModifications;
11677 alock.release();
11678
11679 if (flModifications & IsModified_SharedFolders)
11680 that->i_onSharedFolderChange();
11681
11682 if (flModifications & IsModified_VRDEServer)
11683 that->i_onVRDEServerChange(/* aRestart */ TRUE);
11684 if (flModifications & IsModified_USB)
11685 that->i_onUSBControllerChange();
11686
11687 for (ULONG slot = 0; slot < networkAdapters.size(); ++slot)
11688 if (networkAdapters[slot])
11689 that->i_onNetworkAdapterChange(networkAdapters[slot], FALSE);
11690 for (ULONG slot = 0; slot < RT_ELEMENTS(serialPorts); ++slot)
11691 if (serialPorts[slot])
11692 that->i_onSerialPortChange(serialPorts[slot]);
11693 for (ULONG slot = 0; slot < RT_ELEMENTS(parallelPorts); ++slot)
11694 if (parallelPorts[slot])
11695 that->i_onParallelPortChange(parallelPorts[slot]);
11696
11697 if (flModifications & IsModified_Storage)
11698 that->i_onStorageControllerChange();
11699
11700#if 0
11701 if (flModifications & IsModified_BandwidthControl)
11702 that->onBandwidthControlChange();
11703#endif
11704 }
11705}
11706
11707/**
11708 * Commits all the changes to machine settings.
11709 *
11710 * Note that this operation is supposed to never fail.
11711 *
11712 * @note Locks this object and children for writing.
11713 */
11714void Machine::i_commit()
11715{
11716 AutoCaller autoCaller(this);
11717 AssertComRCReturnVoid(autoCaller.rc());
11718
11719 AutoCaller peerCaller(mPeer);
11720 AssertComRCReturnVoid(peerCaller.rc());
11721
11722 AutoMultiWriteLock2 alock(mPeer, this COMMA_LOCKVAL_SRC_POS);
11723
11724 /*
11725 * use safe commit to ensure Snapshot machines (that share mUserData)
11726 * will still refer to a valid memory location
11727 */
11728 mUserData.commitCopy();
11729
11730 mHWData.commit();
11731
11732 if (mMediaData.isBackedUp())
11733 i_commitMedia(Global::IsOnline(mData->mMachineState));
11734
11735 mBIOSSettings->i_commit();
11736 mVRDEServer->i_commit();
11737 mAudioAdapter->i_commit();
11738 mUSBDeviceFilters->i_commit();
11739 mBandwidthControl->i_commit();
11740
11741 /* Since mNetworkAdapters is a list which might have been changed (resized)
11742 * without using the Backupable<> template we need to handle the copying
11743 * of the list entries manually, including the creation of peers for the
11744 * new objects. */
11745 bool commitNetworkAdapters = false;
11746 size_t newSize = Global::getMaxNetworkAdapters(mHWData->mChipsetType);
11747 if (mPeer)
11748 {
11749 /* commit everything, even the ones which will go away */
11750 for (size_t slot = 0; slot < mNetworkAdapters.size(); slot++)
11751 mNetworkAdapters[slot]->i_commit();
11752 /* copy over the new entries, creating a peer and uninit the original */
11753 mPeer->mNetworkAdapters.resize(RT_MAX(newSize, mPeer->mNetworkAdapters.size()));
11754 for (size_t slot = 0; slot < newSize; slot++)
11755 {
11756 /* look if this adapter has a peer device */
11757 ComObjPtr<NetworkAdapter> peer = mNetworkAdapters[slot]->i_getPeer();
11758 if (!peer)
11759 {
11760 /* no peer means the adapter is a newly created one;
11761 * create a peer owning data this data share it with */
11762 peer.createObject();
11763 peer->init(mPeer, mNetworkAdapters[slot], true /* aReshare */);
11764 }
11765 mPeer->mNetworkAdapters[slot] = peer;
11766 }
11767 /* uninit any no longer needed network adapters */
11768 for (size_t slot = newSize; slot < mNetworkAdapters.size(); ++slot)
11769 mNetworkAdapters[slot]->uninit();
11770 for (size_t slot = newSize; slot < mPeer->mNetworkAdapters.size(); ++slot)
11771 {
11772 if (mPeer->mNetworkAdapters[slot])
11773 mPeer->mNetworkAdapters[slot]->uninit();
11774 }
11775 /* Keep the original network adapter count until this point, so that
11776 * discarding a chipset type change will not lose settings. */
11777 mNetworkAdapters.resize(newSize);
11778 mPeer->mNetworkAdapters.resize(newSize);
11779 }
11780 else
11781 {
11782 /* we have no peer (our parent is the newly created machine);
11783 * just commit changes to the network adapters */
11784 commitNetworkAdapters = true;
11785 }
11786 if (commitNetworkAdapters)
11787 for (size_t slot = 0; slot < mNetworkAdapters.size(); ++slot)
11788 mNetworkAdapters[slot]->i_commit();
11789
11790 for (ULONG slot = 0; slot < RT_ELEMENTS(mSerialPorts); ++slot)
11791 mSerialPorts[slot]->i_commit();
11792 for (ULONG slot = 0; slot < RT_ELEMENTS(mParallelPorts); ++slot)
11793 mParallelPorts[slot]->i_commit();
11794
11795 bool commitStorageControllers = false;
11796
11797 if (mStorageControllers.isBackedUp())
11798 {
11799 mStorageControllers.commit();
11800
11801 if (mPeer)
11802 {
11803 /* Commit all changes to new controllers (this will reshare data with
11804 * peers for those who have peers) */
11805 StorageControllerList *newList = new StorageControllerList();
11806 StorageControllerList::const_iterator it = mStorageControllers->begin();
11807 while (it != mStorageControllers->end())
11808 {
11809 (*it)->i_commit();
11810
11811 /* look if this controller has a peer device */
11812 ComObjPtr<StorageController> peer = (*it)->i_getPeer();
11813 if (!peer)
11814 {
11815 /* no peer means the device is a newly created one;
11816 * create a peer owning data this device share it with */
11817 peer.createObject();
11818 peer->init(mPeer, *it, true /* aReshare */);
11819 }
11820 else
11821 {
11822 /* remove peer from the old list */
11823 mPeer->mStorageControllers->remove(peer);
11824 }
11825 /* and add it to the new list */
11826 newList->push_back(peer);
11827
11828 ++it;
11829 }
11830
11831 /* uninit old peer's controllers that are left */
11832 it = mPeer->mStorageControllers->begin();
11833 while (it != mPeer->mStorageControllers->end())
11834 {
11835 (*it)->uninit();
11836 ++it;
11837 }
11838
11839 /* attach new list of controllers to our peer */
11840 mPeer->mStorageControllers.attach(newList);
11841 }
11842 else
11843 {
11844 /* we have no peer (our parent is the newly created machine);
11845 * just commit changes to devices */
11846 commitStorageControllers = true;
11847 }
11848 }
11849 else
11850 {
11851 /* the list of controllers itself is not changed,
11852 * just commit changes to controllers themselves */
11853 commitStorageControllers = true;
11854 }
11855
11856 if (commitStorageControllers)
11857 {
11858 StorageControllerList::const_iterator it = mStorageControllers->begin();
11859 while (it != mStorageControllers->end())
11860 {
11861 (*it)->i_commit();
11862 ++it;
11863 }
11864 }
11865
11866 bool commitUSBControllers = false;
11867
11868 if (mUSBControllers.isBackedUp())
11869 {
11870 mUSBControllers.commit();
11871
11872 if (mPeer)
11873 {
11874 /* Commit all changes to new controllers (this will reshare data with
11875 * peers for those who have peers) */
11876 USBControllerList *newList = new USBControllerList();
11877 USBControllerList::const_iterator it = mUSBControllers->begin();
11878 while (it != mUSBControllers->end())
11879 {
11880 (*it)->i_commit();
11881
11882 /* look if this controller has a peer device */
11883 ComObjPtr<USBController> peer = (*it)->i_getPeer();
11884 if (!peer)
11885 {
11886 /* no peer means the device is a newly created one;
11887 * create a peer owning data this device share it with */
11888 peer.createObject();
11889 peer->init(mPeer, *it, true /* aReshare */);
11890 }
11891 else
11892 {
11893 /* remove peer from the old list */
11894 mPeer->mUSBControllers->remove(peer);
11895 }
11896 /* and add it to the new list */
11897 newList->push_back(peer);
11898
11899 ++it;
11900 }
11901
11902 /* uninit old peer's controllers that are left */
11903 it = mPeer->mUSBControllers->begin();
11904 while (it != mPeer->mUSBControllers->end())
11905 {
11906 (*it)->uninit();
11907 ++it;
11908 }
11909
11910 /* attach new list of controllers to our peer */
11911 mPeer->mUSBControllers.attach(newList);
11912 }
11913 else
11914 {
11915 /* we have no peer (our parent is the newly created machine);
11916 * just commit changes to devices */
11917 commitUSBControllers = true;
11918 }
11919 }
11920 else
11921 {
11922 /* the list of controllers itself is not changed,
11923 * just commit changes to controllers themselves */
11924 commitUSBControllers = true;
11925 }
11926
11927 if (commitUSBControllers)
11928 {
11929 USBControllerList::const_iterator it = mUSBControllers->begin();
11930 while (it != mUSBControllers->end())
11931 {
11932 (*it)->i_commit();
11933 ++it;
11934 }
11935 }
11936
11937 if (i_isSessionMachine())
11938 {
11939 /* attach new data to the primary machine and reshare it */
11940 mPeer->mUserData.attach(mUserData);
11941 mPeer->mHWData.attach(mHWData);
11942 /* mMediaData is reshared by fixupMedia */
11943 // mPeer->mMediaData.attach(mMediaData);
11944 Assert(mPeer->mMediaData.data() == mMediaData.data());
11945 }
11946}
11947
11948/**
11949 * Copies all the hardware data from the given machine.
11950 *
11951 * Currently, only called when the VM is being restored from a snapshot. In
11952 * particular, this implies that the VM is not running during this method's
11953 * call.
11954 *
11955 * @note This method must be called from under this object's lock.
11956 *
11957 * @note This method doesn't call #commit(), so all data remains backed up and
11958 * unsaved.
11959 */
11960void Machine::i_copyFrom(Machine *aThat)
11961{
11962 AssertReturnVoid(!i_isSnapshotMachine());
11963 AssertReturnVoid(aThat->i_isSnapshotMachine());
11964
11965 AssertReturnVoid(!Global::IsOnline(mData->mMachineState));
11966
11967 mHWData.assignCopy(aThat->mHWData);
11968
11969 // create copies of all shared folders (mHWData after attaching a copy
11970 // contains just references to original objects)
11971 for (HWData::SharedFolderList::iterator it = mHWData->mSharedFolders.begin();
11972 it != mHWData->mSharedFolders.end();
11973 ++it)
11974 {
11975 ComObjPtr<SharedFolder> folder;
11976 folder.createObject();
11977 HRESULT rc = folder->initCopy(i_getMachine(), *it);
11978 AssertComRC(rc);
11979 *it = folder;
11980 }
11981
11982 mBIOSSettings->i_copyFrom(aThat->mBIOSSettings);
11983 mVRDEServer->i_copyFrom(aThat->mVRDEServer);
11984 mAudioAdapter->i_copyFrom(aThat->mAudioAdapter);
11985 mUSBDeviceFilters->i_copyFrom(aThat->mUSBDeviceFilters);
11986 mBandwidthControl->i_copyFrom(aThat->mBandwidthControl);
11987
11988 /* create private copies of all controllers */
11989 mStorageControllers.backup();
11990 mStorageControllers->clear();
11991 for (StorageControllerList::iterator it = aThat->mStorageControllers->begin();
11992 it != aThat->mStorageControllers->end();
11993 ++it)
11994 {
11995 ComObjPtr<StorageController> ctrl;
11996 ctrl.createObject();
11997 ctrl->initCopy(this, *it);
11998 mStorageControllers->push_back(ctrl);
11999 }
12000
12001 /* create private copies of all USB controllers */
12002 mUSBControllers.backup();
12003 mUSBControllers->clear();
12004 for (USBControllerList::iterator it = aThat->mUSBControllers->begin();
12005 it != aThat->mUSBControllers->end();
12006 ++it)
12007 {
12008 ComObjPtr<USBController> ctrl;
12009 ctrl.createObject();
12010 ctrl->initCopy(this, *it);
12011 mUSBControllers->push_back(ctrl);
12012 }
12013
12014 mNetworkAdapters.resize(aThat->mNetworkAdapters.size());
12015 for (ULONG slot = 0; slot < mNetworkAdapters.size(); ++slot)
12016 mNetworkAdapters[slot]->i_copyFrom(aThat->mNetworkAdapters[slot]);
12017 for (ULONG slot = 0; slot < RT_ELEMENTS(mSerialPorts); ++slot)
12018 mSerialPorts[slot]->i_copyFrom(aThat->mSerialPorts[slot]);
12019 for (ULONG slot = 0; slot < RT_ELEMENTS(mParallelPorts); ++slot)
12020 mParallelPorts[slot]->i_copyFrom(aThat->mParallelPorts[slot]);
12021}
12022
12023/**
12024 * Returns whether the given storage controller is hotplug capable.
12025 *
12026 * @returns true if the controller supports hotplugging
12027 * false otherwise.
12028 * @param enmCtrlType The controller type to check for.
12029 */
12030bool Machine::i_isControllerHotplugCapable(StorageControllerType_T enmCtrlType)
12031{
12032 ComPtr<ISystemProperties> systemProperties;
12033 HRESULT rc = mParent->COMGETTER(SystemProperties)(systemProperties.asOutParam());
12034 if (FAILED(rc))
12035 return false;
12036
12037 BOOL aHotplugCapable = FALSE;
12038 systemProperties->GetStorageControllerHotplugCapable(enmCtrlType, &aHotplugCapable);
12039
12040 return RT_BOOL(aHotplugCapable);
12041}
12042
12043#ifdef VBOX_WITH_RESOURCE_USAGE_API
12044
12045void Machine::i_getDiskList(MediaList &list)
12046{
12047 for (MediaData::AttachmentList::const_iterator it = mMediaData->mAttachments.begin();
12048 it != mMediaData->mAttachments.end();
12049 ++it)
12050 {
12051 MediumAttachment* pAttach = *it;
12052 /* just in case */
12053 AssertStmt(pAttach, continue);
12054
12055 AutoCaller localAutoCallerA(pAttach);
12056 if (FAILED(localAutoCallerA.rc())) continue;
12057
12058 AutoReadLock local_alockA(pAttach COMMA_LOCKVAL_SRC_POS);
12059
12060 if (pAttach->i_getType() == DeviceType_HardDisk)
12061 list.push_back(pAttach->i_getMedium());
12062 }
12063}
12064
12065void Machine::i_registerMetrics(PerformanceCollector *aCollector, Machine *aMachine, RTPROCESS pid)
12066{
12067 AssertReturnVoid(isWriteLockOnCurrentThread());
12068 AssertPtrReturnVoid(aCollector);
12069
12070 pm::CollectorHAL *hal = aCollector->getHAL();
12071 /* Create sub metrics */
12072 pm::SubMetric *cpuLoadUser = new pm::SubMetric("CPU/Load/User",
12073 "Percentage of processor time spent in user mode by the VM process.");
12074 pm::SubMetric *cpuLoadKernel = new pm::SubMetric("CPU/Load/Kernel",
12075 "Percentage of processor time spent in kernel mode by the VM process.");
12076 pm::SubMetric *ramUsageUsed = new pm::SubMetric("RAM/Usage/Used",
12077 "Size of resident portion of VM process in memory.");
12078 pm::SubMetric *diskUsageUsed = new pm::SubMetric("Disk/Usage/Used",
12079 "Actual size of all VM disks combined.");
12080 pm::SubMetric *machineNetRx = new pm::SubMetric("Net/Rate/Rx",
12081 "Network receive rate.");
12082 pm::SubMetric *machineNetTx = new pm::SubMetric("Net/Rate/Tx",
12083 "Network transmit rate.");
12084 /* Create and register base metrics */
12085 pm::BaseMetric *cpuLoad = new pm::MachineCpuLoadRaw(hal, aMachine, pid,
12086 cpuLoadUser, cpuLoadKernel);
12087 aCollector->registerBaseMetric(cpuLoad);
12088 pm::BaseMetric *ramUsage = new pm::MachineRamUsage(hal, aMachine, pid,
12089 ramUsageUsed);
12090 aCollector->registerBaseMetric(ramUsage);
12091 MediaList disks;
12092 i_getDiskList(disks);
12093 pm::BaseMetric *diskUsage = new pm::MachineDiskUsage(hal, aMachine, disks,
12094 diskUsageUsed);
12095 aCollector->registerBaseMetric(diskUsage);
12096
12097 aCollector->registerMetric(new pm::Metric(cpuLoad, cpuLoadUser, 0));
12098 aCollector->registerMetric(new pm::Metric(cpuLoad, cpuLoadUser,
12099 new pm::AggregateAvg()));
12100 aCollector->registerMetric(new pm::Metric(cpuLoad, cpuLoadUser,
12101 new pm::AggregateMin()));
12102 aCollector->registerMetric(new pm::Metric(cpuLoad, cpuLoadUser,
12103 new pm::AggregateMax()));
12104 aCollector->registerMetric(new pm::Metric(cpuLoad, cpuLoadKernel, 0));
12105 aCollector->registerMetric(new pm::Metric(cpuLoad, cpuLoadKernel,
12106 new pm::AggregateAvg()));
12107 aCollector->registerMetric(new pm::Metric(cpuLoad, cpuLoadKernel,
12108 new pm::AggregateMin()));
12109 aCollector->registerMetric(new pm::Metric(cpuLoad, cpuLoadKernel,
12110 new pm::AggregateMax()));
12111
12112 aCollector->registerMetric(new pm::Metric(ramUsage, ramUsageUsed, 0));
12113 aCollector->registerMetric(new pm::Metric(ramUsage, ramUsageUsed,
12114 new pm::AggregateAvg()));
12115 aCollector->registerMetric(new pm::Metric(ramUsage, ramUsageUsed,
12116 new pm::AggregateMin()));
12117 aCollector->registerMetric(new pm::Metric(ramUsage, ramUsageUsed,
12118 new pm::AggregateMax()));
12119
12120 aCollector->registerMetric(new pm::Metric(diskUsage, diskUsageUsed, 0));
12121 aCollector->registerMetric(new pm::Metric(diskUsage, diskUsageUsed,
12122 new pm::AggregateAvg()));
12123 aCollector->registerMetric(new pm::Metric(diskUsage, diskUsageUsed,
12124 new pm::AggregateMin()));
12125 aCollector->registerMetric(new pm::Metric(diskUsage, diskUsageUsed,
12126 new pm::AggregateMax()));
12127
12128
12129 /* Guest metrics collector */
12130 mCollectorGuest = new pm::CollectorGuest(aMachine, pid);
12131 aCollector->registerGuest(mCollectorGuest);
12132 Log7(("{%p} " LOG_FN_FMT ": mCollectorGuest=%p\n", this, __PRETTY_FUNCTION__, mCollectorGuest));
12133
12134 /* Create sub metrics */
12135 pm::SubMetric *guestLoadUser = new pm::SubMetric("Guest/CPU/Load/User",
12136 "Percentage of processor time spent in user mode as seen by the guest.");
12137 pm::SubMetric *guestLoadKernel = new pm::SubMetric("Guest/CPU/Load/Kernel",
12138 "Percentage of processor time spent in kernel mode as seen by the guest.");
12139 pm::SubMetric *guestLoadIdle = new pm::SubMetric("Guest/CPU/Load/Idle",
12140 "Percentage of processor time spent idling as seen by the guest.");
12141
12142 /* The total amount of physical ram is fixed now, but we'll support dynamic guest ram configurations in the future. */
12143 pm::SubMetric *guestMemTotal = new pm::SubMetric("Guest/RAM/Usage/Total", "Total amount of physical guest RAM.");
12144 pm::SubMetric *guestMemFree = new pm::SubMetric("Guest/RAM/Usage/Free", "Free amount of physical guest RAM.");
12145 pm::SubMetric *guestMemBalloon = new pm::SubMetric("Guest/RAM/Usage/Balloon", "Amount of ballooned physical guest RAM.");
12146 pm::SubMetric *guestMemShared = new pm::SubMetric("Guest/RAM/Usage/Shared", "Amount of shared physical guest RAM.");
12147 pm::SubMetric *guestMemCache = new pm::SubMetric(
12148 "Guest/RAM/Usage/Cache", "Total amount of guest (disk) cache memory.");
12149
12150 pm::SubMetric *guestPagedTotal = new pm::SubMetric(
12151 "Guest/Pagefile/Usage/Total", "Total amount of space in the page file.");
12152
12153 /* Create and register base metrics */
12154 pm::BaseMetric *machineNetRate = new pm::MachineNetRate(mCollectorGuest, aMachine,
12155 machineNetRx, machineNetTx);
12156 aCollector->registerBaseMetric(machineNetRate);
12157
12158 pm::BaseMetric *guestCpuLoad = new pm::GuestCpuLoad(mCollectorGuest, aMachine,
12159 guestLoadUser, guestLoadKernel, guestLoadIdle);
12160 aCollector->registerBaseMetric(guestCpuLoad);
12161
12162 pm::BaseMetric *guestCpuMem = new pm::GuestRamUsage(mCollectorGuest, aMachine,
12163 guestMemTotal, guestMemFree,
12164 guestMemBalloon, guestMemShared,
12165 guestMemCache, guestPagedTotal);
12166 aCollector->registerBaseMetric(guestCpuMem);
12167
12168 aCollector->registerMetric(new pm::Metric(machineNetRate, machineNetRx, 0));
12169 aCollector->registerMetric(new pm::Metric(machineNetRate, machineNetRx, new pm::AggregateAvg()));
12170 aCollector->registerMetric(new pm::Metric(machineNetRate, machineNetRx, new pm::AggregateMin()));
12171 aCollector->registerMetric(new pm::Metric(machineNetRate, machineNetRx, new pm::AggregateMax()));
12172
12173 aCollector->registerMetric(new pm::Metric(machineNetRate, machineNetTx, 0));
12174 aCollector->registerMetric(new pm::Metric(machineNetRate, machineNetTx, new pm::AggregateAvg()));
12175 aCollector->registerMetric(new pm::Metric(machineNetRate, machineNetTx, new pm::AggregateMin()));
12176 aCollector->registerMetric(new pm::Metric(machineNetRate, machineNetTx, new pm::AggregateMax()));
12177
12178 aCollector->registerMetric(new pm::Metric(guestCpuLoad, guestLoadUser, 0));
12179 aCollector->registerMetric(new pm::Metric(guestCpuLoad, guestLoadUser, new pm::AggregateAvg()));
12180 aCollector->registerMetric(new pm::Metric(guestCpuLoad, guestLoadUser, new pm::AggregateMin()));
12181 aCollector->registerMetric(new pm::Metric(guestCpuLoad, guestLoadUser, new pm::AggregateMax()));
12182
12183 aCollector->registerMetric(new pm::Metric(guestCpuLoad, guestLoadKernel, 0));
12184 aCollector->registerMetric(new pm::Metric(guestCpuLoad, guestLoadKernel, new pm::AggregateAvg()));
12185 aCollector->registerMetric(new pm::Metric(guestCpuLoad, guestLoadKernel, new pm::AggregateMin()));
12186 aCollector->registerMetric(new pm::Metric(guestCpuLoad, guestLoadKernel, new pm::AggregateMax()));
12187
12188 aCollector->registerMetric(new pm::Metric(guestCpuLoad, guestLoadIdle, 0));
12189 aCollector->registerMetric(new pm::Metric(guestCpuLoad, guestLoadIdle, new pm::AggregateAvg()));
12190 aCollector->registerMetric(new pm::Metric(guestCpuLoad, guestLoadIdle, new pm::AggregateMin()));
12191 aCollector->registerMetric(new pm::Metric(guestCpuLoad, guestLoadIdle, new pm::AggregateMax()));
12192
12193 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemTotal, 0));
12194 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemTotal, new pm::AggregateAvg()));
12195 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemTotal, new pm::AggregateMin()));
12196 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemTotal, new pm::AggregateMax()));
12197
12198 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemFree, 0));
12199 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemFree, new pm::AggregateAvg()));
12200 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemFree, new pm::AggregateMin()));
12201 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemFree, new pm::AggregateMax()));
12202
12203 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemBalloon, 0));
12204 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemBalloon, new pm::AggregateAvg()));
12205 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemBalloon, new pm::AggregateMin()));
12206 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemBalloon, new pm::AggregateMax()));
12207
12208 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemShared, 0));
12209 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemShared, new pm::AggregateAvg()));
12210 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemShared, new pm::AggregateMin()));
12211 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemShared, new pm::AggregateMax()));
12212
12213 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemCache, 0));
12214 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemCache, new pm::AggregateAvg()));
12215 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemCache, new pm::AggregateMin()));
12216 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemCache, new pm::AggregateMax()));
12217
12218 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestPagedTotal, 0));
12219 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestPagedTotal, new pm::AggregateAvg()));
12220 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestPagedTotal, new pm::AggregateMin()));
12221 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestPagedTotal, new pm::AggregateMax()));
12222}
12223
12224void Machine::i_unregisterMetrics(PerformanceCollector *aCollector, Machine *aMachine)
12225{
12226 AssertReturnVoid(isWriteLockOnCurrentThread());
12227
12228 if (aCollector)
12229 {
12230 aCollector->unregisterMetricsFor(aMachine);
12231 aCollector->unregisterBaseMetricsFor(aMachine);
12232 }
12233}
12234
12235#endif /* VBOX_WITH_RESOURCE_USAGE_API */
12236
12237
12238////////////////////////////////////////////////////////////////////////////////
12239
12240DEFINE_EMPTY_CTOR_DTOR(SessionMachine)
12241
12242HRESULT SessionMachine::FinalConstruct()
12243{
12244 LogFlowThisFunc(("\n"));
12245
12246 mClientToken = NULL;
12247
12248 return BaseFinalConstruct();
12249}
12250
12251void SessionMachine::FinalRelease()
12252{
12253 LogFlowThisFunc(("\n"));
12254
12255 Assert(!mClientToken);
12256 /* paranoia, should not hang around any more */
12257 if (mClientToken)
12258 {
12259 delete mClientToken;
12260 mClientToken = NULL;
12261 }
12262
12263 uninit(Uninit::Unexpected);
12264
12265 BaseFinalRelease();
12266}
12267
12268/**
12269 * @note Must be called only by Machine::LockMachine() from its own write lock.
12270 */
12271HRESULT SessionMachine::init(Machine *aMachine)
12272{
12273 LogFlowThisFuncEnter();
12274 LogFlowThisFunc(("mName={%s}\n", aMachine->mUserData->s.strName.c_str()));
12275
12276 AssertReturn(aMachine, E_INVALIDARG);
12277
12278 AssertReturn(aMachine->lockHandle()->isWriteLockOnCurrentThread(), E_FAIL);
12279
12280 /* Enclose the state transition NotReady->InInit->Ready */
12281 AutoInitSpan autoInitSpan(this);
12282 AssertReturn(autoInitSpan.isOk(), E_FAIL);
12283
12284 HRESULT rc = S_OK;
12285
12286 /* create the machine client token */
12287 try
12288 {
12289 mClientToken = new ClientToken(aMachine, this);
12290 if (!mClientToken->isReady())
12291 {
12292 delete mClientToken;
12293 mClientToken = NULL;
12294 rc = E_FAIL;
12295 }
12296 }
12297 catch (std::bad_alloc &)
12298 {
12299 rc = E_OUTOFMEMORY;
12300 }
12301 if (FAILED(rc))
12302 return rc;
12303
12304 /* memorize the peer Machine */
12305 unconst(mPeer) = aMachine;
12306 /* share the parent pointer */
12307 unconst(mParent) = aMachine->mParent;
12308
12309 /* take the pointers to data to share */
12310 mData.share(aMachine->mData);
12311 mSSData.share(aMachine->mSSData);
12312
12313 mUserData.share(aMachine->mUserData);
12314 mHWData.share(aMachine->mHWData);
12315 mMediaData.share(aMachine->mMediaData);
12316
12317 mStorageControllers.allocate();
12318 for (StorageControllerList::const_iterator it = aMachine->mStorageControllers->begin();
12319 it != aMachine->mStorageControllers->end();
12320 ++it)
12321 {
12322 ComObjPtr<StorageController> ctl;
12323 ctl.createObject();
12324 ctl->init(this, *it);
12325 mStorageControllers->push_back(ctl);
12326 }
12327
12328 mUSBControllers.allocate();
12329 for (USBControllerList::const_iterator it = aMachine->mUSBControllers->begin();
12330 it != aMachine->mUSBControllers->end();
12331 ++it)
12332 {
12333 ComObjPtr<USBController> ctl;
12334 ctl.createObject();
12335 ctl->init(this, *it);
12336 mUSBControllers->push_back(ctl);
12337 }
12338
12339 unconst(mBIOSSettings).createObject();
12340 mBIOSSettings->init(this, aMachine->mBIOSSettings);
12341 /* create another VRDEServer object that will be mutable */
12342 unconst(mVRDEServer).createObject();
12343 mVRDEServer->init(this, aMachine->mVRDEServer);
12344 /* create another audio adapter object that will be mutable */
12345 unconst(mAudioAdapter).createObject();
12346 mAudioAdapter->init(this, aMachine->mAudioAdapter);
12347 /* create a list of serial ports that will be mutable */
12348 for (ULONG slot = 0; slot < RT_ELEMENTS(mSerialPorts); ++slot)
12349 {
12350 unconst(mSerialPorts[slot]).createObject();
12351 mSerialPorts[slot]->init(this, aMachine->mSerialPorts[slot]);
12352 }
12353 /* create a list of parallel ports that will be mutable */
12354 for (ULONG slot = 0; slot < RT_ELEMENTS(mParallelPorts); ++slot)
12355 {
12356 unconst(mParallelPorts[slot]).createObject();
12357 mParallelPorts[slot]->init(this, aMachine->mParallelPorts[slot]);
12358 }
12359
12360 /* create another USB device filters object that will be mutable */
12361 unconst(mUSBDeviceFilters).createObject();
12362 mUSBDeviceFilters->init(this, aMachine->mUSBDeviceFilters);
12363
12364 /* create a list of network adapters that will be mutable */
12365 mNetworkAdapters.resize(aMachine->mNetworkAdapters.size());
12366 for (ULONG slot = 0; slot < mNetworkAdapters.size(); ++slot)
12367 {
12368 unconst(mNetworkAdapters[slot]).createObject();
12369 mNetworkAdapters[slot]->init(this, aMachine->mNetworkAdapters[slot]);
12370 }
12371
12372 /* create another bandwidth control object that will be mutable */
12373 unconst(mBandwidthControl).createObject();
12374 mBandwidthControl->init(this, aMachine->mBandwidthControl);
12375
12376 /* default is to delete saved state on Saved -> PoweredOff transition */
12377 mRemoveSavedState = true;
12378
12379 /* Confirm a successful initialization when it's the case */
12380 autoInitSpan.setSucceeded();
12381
12382 miNATNetworksStarted = 0;
12383
12384 LogFlowThisFuncLeave();
12385 return rc;
12386}
12387
12388/**
12389 * Uninitializes this session object. If the reason is other than
12390 * Uninit::Unexpected, then this method MUST be called from #checkForDeath()
12391 * or the client watcher code.
12392 *
12393 * @param aReason uninitialization reason
12394 *
12395 * @note Locks mParent + this object for writing.
12396 */
12397void SessionMachine::uninit(Uninit::Reason aReason)
12398{
12399 LogFlowThisFuncEnter();
12400 LogFlowThisFunc(("reason=%d\n", aReason));
12401
12402 /*
12403 * Strongly reference ourselves to prevent this object deletion after
12404 * mData->mSession.mMachine.setNull() below (which can release the last
12405 * reference and call the destructor). Important: this must be done before
12406 * accessing any members (and before AutoUninitSpan that does it as well).
12407 * This self reference will be released as the very last step on return.
12408 */
12409 ComObjPtr<SessionMachine> selfRef = this;
12410
12411 /* Enclose the state transition Ready->InUninit->NotReady */
12412 AutoUninitSpan autoUninitSpan(this);
12413 if (autoUninitSpan.uninitDone())
12414 {
12415 LogFlowThisFunc(("Already uninitialized\n"));
12416 LogFlowThisFuncLeave();
12417 return;
12418 }
12419
12420 if (autoUninitSpan.initFailed())
12421 {
12422 /* We've been called by init() because it's failed. It's not really
12423 * necessary (nor it's safe) to perform the regular uninit sequence
12424 * below, the following is enough.
12425 */
12426 LogFlowThisFunc(("Initialization failed.\n"));
12427 /* destroy the machine client token */
12428 if (mClientToken)
12429 {
12430 delete mClientToken;
12431 mClientToken = NULL;
12432 }
12433 uninitDataAndChildObjects();
12434 mData.free();
12435 unconst(mParent) = NULL;
12436 unconst(mPeer) = NULL;
12437 LogFlowThisFuncLeave();
12438 return;
12439 }
12440
12441 MachineState_T lastState;
12442 {
12443 AutoReadLock tempLock(this COMMA_LOCKVAL_SRC_POS);
12444 lastState = mData->mMachineState;
12445 }
12446 NOREF(lastState);
12447
12448#ifdef VBOX_WITH_USB
12449 // release all captured USB devices, but do this before requesting the locks below
12450 if (aReason == Uninit::Abnormal && Global::IsOnline(lastState))
12451 {
12452 /* Console::captureUSBDevices() is called in the VM process only after
12453 * setting the machine state to Starting or Restoring.
12454 * Console::detachAllUSBDevices() will be called upon successful
12455 * termination. So, we need to release USB devices only if there was
12456 * an abnormal termination of a running VM.
12457 *
12458 * This is identical to SessionMachine::DetachAllUSBDevices except
12459 * for the aAbnormal argument. */
12460 HRESULT rc = mUSBDeviceFilters->i_notifyProxy(false /* aInsertFilters */);
12461 AssertComRC(rc);
12462 NOREF(rc);
12463
12464 USBProxyService *service = mParent->i_host()->i_usbProxyService();
12465 if (service)
12466 service->detachAllDevicesFromVM(this, true /* aDone */, true /* aAbnormal */);
12467 }
12468#endif /* VBOX_WITH_USB */
12469
12470 // we need to lock this object in uninit() because the lock is shared
12471 // with mPeer (as well as data we modify below). mParent lock is needed
12472 // by several calls to it, and USB needs host lock.
12473 AutoMultiWriteLock3 multilock(mParent, mParent->i_host(), this COMMA_LOCKVAL_SRC_POS);
12474
12475#ifdef VBOX_WITH_RESOURCE_USAGE_API
12476 /*
12477 * It is safe to call Machine::i_unregisterMetrics() here because
12478 * PerformanceCollector::samplerCallback no longer accesses guest methods
12479 * holding the lock.
12480 */
12481 i_unregisterMetrics(mParent->i_performanceCollector(), mPeer);
12482 /* The guest must be unregistered after its metrics (@bugref{5949}). */
12483 Log7(("{%p} " LOG_FN_FMT ": mCollectorGuest=%p\n", this, __PRETTY_FUNCTION__, mCollectorGuest));
12484 if (mCollectorGuest)
12485 {
12486 mParent->i_performanceCollector()->unregisterGuest(mCollectorGuest);
12487 // delete mCollectorGuest; => CollectorGuestManager::destroyUnregistered()
12488 mCollectorGuest = NULL;
12489 }
12490#endif
12491
12492 if (aReason == Uninit::Abnormal)
12493 {
12494 Log1WarningThisFunc(("ABNORMAL client termination! (wasBusy=%d)\n", Global::IsOnlineOrTransient(lastState)));
12495
12496 /* reset the state to Aborted */
12497 if (mData->mMachineState != MachineState_Aborted)
12498 i_setMachineState(MachineState_Aborted);
12499 }
12500
12501 // any machine settings modified?
12502 if (mData->flModifications)
12503 {
12504 Log1WarningThisFunc(("Discarding unsaved settings changes!\n"));
12505 i_rollback(false /* aNotify */);
12506 }
12507
12508 mData->mSession.mPID = NIL_RTPROCESS;
12509
12510 if (aReason == Uninit::Unexpected)
12511 {
12512 /* Uninitialization didn't come from #checkForDeath(), so tell the
12513 * client watcher thread to update the set of machines that have open
12514 * sessions. */
12515 mParent->i_updateClientWatcher();
12516 }
12517
12518 /* uninitialize all remote controls */
12519 if (mData->mSession.mRemoteControls.size())
12520 {
12521 LogFlowThisFunc(("Closing remote sessions (%d):\n",
12522 mData->mSession.mRemoteControls.size()));
12523
12524 Data::Session::RemoteControlList::iterator it =
12525 mData->mSession.mRemoteControls.begin();
12526 while (it != mData->mSession.mRemoteControls.end())
12527 {
12528 LogFlowThisFunc((" Calling remoteControl->Uninitialize()...\n"));
12529 HRESULT rc = (*it)->Uninitialize();
12530 LogFlowThisFunc((" remoteControl->Uninitialize() returned %08X\n", rc));
12531 if (FAILED(rc))
12532 Log1WarningThisFunc(("Forgot to close the remote session?\n"));
12533 ++it;
12534 }
12535 mData->mSession.mRemoteControls.clear();
12536 }
12537
12538 /* Remove all references to the NAT network service. The service will stop
12539 * if all references (also from other VMs) are removed. */
12540 for (; miNATNetworksStarted > 0; miNATNetworksStarted--)
12541 {
12542 for (ULONG slot = 0; slot < mNetworkAdapters.size(); ++slot)
12543 {
12544 NetworkAttachmentType_T type;
12545 HRESULT hrc;
12546
12547 hrc = mNetworkAdapters[slot]->COMGETTER(AttachmentType)(&type);
12548 if ( SUCCEEDED(hrc)
12549 && type == NetworkAttachmentType_NATNetwork)
12550 {
12551 Bstr name;
12552 hrc = mNetworkAdapters[slot]->COMGETTER(NATNetwork)(name.asOutParam());
12553 if (SUCCEEDED(hrc))
12554 {
12555 multilock.release();
12556 LogRel(("VM '%s' stops using NAT network '%ls'\n",
12557 mUserData->s.strName.c_str(), name.raw()));
12558 mParent->i_natNetworkRefDec(name.raw());
12559 multilock.acquire();
12560 }
12561 }
12562 }
12563 }
12564
12565 /*
12566 * An expected uninitialization can come only from #checkForDeath().
12567 * Otherwise it means that something's gone really wrong (for example,
12568 * the Session implementation has released the VirtualBox reference
12569 * before it triggered #OnSessionEnd(), or before releasing IPC semaphore,
12570 * etc). However, it's also possible, that the client releases the IPC
12571 * semaphore correctly (i.e. before it releases the VirtualBox reference),
12572 * but the VirtualBox release event comes first to the server process.
12573 * This case is practically possible, so we should not assert on an
12574 * unexpected uninit, just log a warning.
12575 */
12576
12577 if ((aReason == Uninit::Unexpected))
12578 Log1WarningThisFunc(("Unexpected SessionMachine uninitialization!\n"));
12579
12580 if (aReason != Uninit::Normal)
12581 {
12582 mData->mSession.mDirectControl.setNull();
12583 }
12584 else
12585 {
12586 /* this must be null here (see #OnSessionEnd()) */
12587 Assert(mData->mSession.mDirectControl.isNull());
12588 Assert(mData->mSession.mState == SessionState_Unlocking);
12589 Assert(!mData->mSession.mProgress.isNull());
12590 }
12591 if (mData->mSession.mProgress)
12592 {
12593 if (aReason == Uninit::Normal)
12594 mData->mSession.mProgress->i_notifyComplete(S_OK);
12595 else
12596 mData->mSession.mProgress->i_notifyComplete(E_FAIL,
12597 COM_IIDOF(ISession),
12598 getComponentName(),
12599 tr("The VM session was aborted"));
12600 mData->mSession.mProgress.setNull();
12601 }
12602
12603 /* remove the association between the peer machine and this session machine */
12604 Assert( (SessionMachine*)mData->mSession.mMachine == this
12605 || aReason == Uninit::Unexpected);
12606
12607 /* reset the rest of session data */
12608 mData->mSession.mLockType = LockType_Null;
12609 mData->mSession.mMachine.setNull();
12610 mData->mSession.mState = SessionState_Unlocked;
12611 mData->mSession.mName.setNull();
12612
12613 /* destroy the machine client token before leaving the exclusive lock */
12614 if (mClientToken)
12615 {
12616 delete mClientToken;
12617 mClientToken = NULL;
12618 }
12619
12620 /* fire an event */
12621 mParent->i_onSessionStateChange(mData->mUuid, SessionState_Unlocked);
12622
12623 uninitDataAndChildObjects();
12624
12625 /* free the essential data structure last */
12626 mData.free();
12627
12628 /* release the exclusive lock before setting the below two to NULL */
12629 multilock.release();
12630
12631 unconst(mParent) = NULL;
12632 unconst(mPeer) = NULL;
12633
12634 LogFlowThisFuncLeave();
12635}
12636
12637// util::Lockable interface
12638////////////////////////////////////////////////////////////////////////////////
12639
12640/**
12641 * Overrides VirtualBoxBase::lockHandle() in order to share the lock handle
12642 * with the primary Machine instance (mPeer).
12643 */
12644RWLockHandle *SessionMachine::lockHandle() const
12645{
12646 AssertReturn(mPeer != NULL, NULL);
12647 return mPeer->lockHandle();
12648}
12649
12650// IInternalMachineControl methods
12651////////////////////////////////////////////////////////////////////////////////
12652
12653/**
12654 * Passes collected guest statistics to performance collector object
12655 */
12656HRESULT SessionMachine::reportVmStatistics(ULONG aValidStats, ULONG aCpuUser,
12657 ULONG aCpuKernel, ULONG aCpuIdle,
12658 ULONG aMemTotal, ULONG aMemFree,
12659 ULONG aMemBalloon, ULONG aMemShared,
12660 ULONG aMemCache, ULONG aPageTotal,
12661 ULONG aAllocVMM, ULONG aFreeVMM,
12662 ULONG aBalloonedVMM, ULONG aSharedVMM,
12663 ULONG aVmNetRx, ULONG aVmNetTx)
12664{
12665#ifdef VBOX_WITH_RESOURCE_USAGE_API
12666 if (mCollectorGuest)
12667 mCollectorGuest->updateStats(aValidStats, aCpuUser, aCpuKernel, aCpuIdle,
12668 aMemTotal, aMemFree, aMemBalloon, aMemShared,
12669 aMemCache, aPageTotal, aAllocVMM, aFreeVMM,
12670 aBalloonedVMM, aSharedVMM, aVmNetRx, aVmNetTx);
12671
12672 return S_OK;
12673#else
12674 NOREF(aValidStats);
12675 NOREF(aCpuUser);
12676 NOREF(aCpuKernel);
12677 NOREF(aCpuIdle);
12678 NOREF(aMemTotal);
12679 NOREF(aMemFree);
12680 NOREF(aMemBalloon);
12681 NOREF(aMemShared);
12682 NOREF(aMemCache);
12683 NOREF(aPageTotal);
12684 NOREF(aAllocVMM);
12685 NOREF(aFreeVMM);
12686 NOREF(aBalloonedVMM);
12687 NOREF(aSharedVMM);
12688 NOREF(aVmNetRx);
12689 NOREF(aVmNetTx);
12690 return E_NOTIMPL;
12691#endif
12692}
12693
12694////////////////////////////////////////////////////////////////////////////////
12695//
12696// SessionMachine task records
12697//
12698////////////////////////////////////////////////////////////////////////////////
12699
12700/**
12701 * Task record for saving the machine state.
12702 */
12703struct SessionMachine::SaveStateTask
12704 : public Machine::Task
12705{
12706 SaveStateTask(SessionMachine *m,
12707 Progress *p,
12708 const Utf8Str &t,
12709 Reason_T enmReason,
12710 const Utf8Str &strStateFilePath)
12711 : Task(m, p, t),
12712 m_enmReason(enmReason),
12713 m_strStateFilePath(strStateFilePath)
12714 {}
12715
12716 void handler()
12717 {
12718 ((SessionMachine *)(Machine *)m_pMachine)->i_saveStateHandler(*this);
12719 }
12720
12721 Reason_T m_enmReason;
12722 Utf8Str m_strStateFilePath;
12723};
12724
12725/**
12726 * Task thread implementation for SessionMachine::SaveState(), called from
12727 * SessionMachine::taskHandler().
12728 *
12729 * @note Locks this object for writing.
12730 *
12731 * @param task
12732 * @return
12733 */
12734void SessionMachine::i_saveStateHandler(SaveStateTask &task)
12735{
12736 LogFlowThisFuncEnter();
12737
12738 AutoCaller autoCaller(this);
12739 LogFlowThisFunc(("state=%d\n", getObjectState().getState()));
12740 if (FAILED(autoCaller.rc()))
12741 {
12742 /* we might have been uninitialized because the session was accidentally
12743 * closed by the client, so don't assert */
12744 HRESULT rc = setError(E_FAIL,
12745 tr("The session has been accidentally closed"));
12746 task.m_pProgress->i_notifyComplete(rc);
12747 LogFlowThisFuncLeave();
12748 return;
12749 }
12750
12751 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
12752
12753 HRESULT rc = S_OK;
12754
12755 try
12756 {
12757 ComPtr<IInternalSessionControl> directControl;
12758 if (mData->mSession.mLockType == LockType_VM)
12759 directControl = mData->mSession.mDirectControl;
12760 if (directControl.isNull())
12761 throw setError(VBOX_E_INVALID_VM_STATE,
12762 tr("Trying to save state without a running VM"));
12763 alock.release();
12764 BOOL fSuspendedBySave;
12765 rc = directControl->SaveStateWithReason(task.m_enmReason, task.m_pProgress, Bstr(task.m_strStateFilePath).raw(), task.m_machineStateBackup != MachineState_Paused, &fSuspendedBySave);
12766 Assert(!fSuspendedBySave);
12767 alock.acquire();
12768
12769 AssertStmt( (SUCCEEDED(rc) && mData->mMachineState == MachineState_Saved)
12770 || (FAILED(rc) && mData->mMachineState == MachineState_Saving),
12771 throw E_FAIL);
12772
12773 if (SUCCEEDED(rc))
12774 {
12775 mSSData->strStateFilePath = task.m_strStateFilePath;
12776
12777 /* save all VM settings */
12778 rc = i_saveSettings(NULL);
12779 // no need to check whether VirtualBox.xml needs saving also since
12780 // we can't have a name change pending at this point
12781 }
12782 else
12783 {
12784 // On failure, set the state to the state we had at the beginning.
12785 i_setMachineState(task.m_machineStateBackup);
12786 i_updateMachineStateOnClient();
12787
12788 // Delete the saved state file (might have been already created).
12789 // No need to check whether this is shared with a snapshot here
12790 // because we certainly created a fresh saved state file here.
12791 RTFileDelete(task.m_strStateFilePath.c_str());
12792 }
12793 }
12794 catch (HRESULT aRC) { rc = aRC; }
12795
12796 task.m_pProgress->i_notifyComplete(rc);
12797
12798 LogFlowThisFuncLeave();
12799}
12800
12801/**
12802 * @note Locks this object for writing.
12803 */
12804HRESULT SessionMachine::saveState(ComPtr<IProgress> &aProgress)
12805{
12806 return i_saveStateWithReason(Reason_Unspecified, aProgress);
12807}
12808
12809HRESULT SessionMachine::i_saveStateWithReason(Reason_T aReason, ComPtr<IProgress> &aProgress)
12810{
12811 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
12812
12813 HRESULT rc = i_checkStateDependency(MutableOrRunningStateDep);
12814 if (FAILED(rc)) return rc;
12815
12816 if ( mData->mMachineState != MachineState_Running
12817 && mData->mMachineState != MachineState_Paused
12818 )
12819 return setError(VBOX_E_INVALID_VM_STATE,
12820 tr("Cannot save the execution state as the machine is not running or paused (machine state: %s)"),
12821 Global::stringifyMachineState(mData->mMachineState));
12822
12823 ComObjPtr<Progress> pProgress;
12824 pProgress.createObject();
12825 rc = pProgress->init(i_getVirtualBox(),
12826 static_cast<IMachine *>(this) /* aInitiator */,
12827 Bstr(tr("Saving the execution state of the virtual machine")).raw(),
12828 FALSE /* aCancelable */);
12829 if (FAILED(rc))
12830 return rc;
12831
12832 Utf8Str strStateFilePath;
12833 i_composeSavedStateFilename(strStateFilePath);
12834
12835 /* create and start the task on a separate thread (note that it will not
12836 * start working until we release alock) */
12837 SaveStateTask *pTask = new SaveStateTask(this, pProgress, "SaveState", aReason, strStateFilePath);
12838 rc = pTask->createThread();
12839 if (FAILED(rc))
12840 return rc;
12841
12842 /* set the state to Saving (expected by Session::SaveStateWithReason()) */
12843 i_setMachineState(MachineState_Saving);
12844 i_updateMachineStateOnClient();
12845
12846 pProgress.queryInterfaceTo(aProgress.asOutParam());
12847
12848 return S_OK;
12849}
12850
12851/**
12852 * @note Locks this object for writing.
12853 */
12854HRESULT SessionMachine::adoptSavedState(const com::Utf8Str &aSavedStateFile)
12855{
12856 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
12857
12858 HRESULT rc = i_checkStateDependency(MutableStateDep);
12859 if (FAILED(rc)) return rc;
12860
12861 if ( mData->mMachineState != MachineState_PoweredOff
12862 && mData->mMachineState != MachineState_Teleported
12863 && mData->mMachineState != MachineState_Aborted
12864 )
12865 return setError(VBOX_E_INVALID_VM_STATE,
12866 tr("Cannot adopt the saved machine state as the machine is not in Powered Off, Teleported or Aborted state (machine state: %s)"),
12867 Global::stringifyMachineState(mData->mMachineState));
12868
12869 com::Utf8Str stateFilePathFull;
12870 int vrc = i_calculateFullPath(aSavedStateFile, stateFilePathFull);
12871 if (RT_FAILURE(vrc))
12872 return setError(VBOX_E_FILE_ERROR,
12873 tr("Invalid saved state file path '%s' (%Rrc)"),
12874 aSavedStateFile.c_str(),
12875 vrc);
12876
12877 mSSData->strStateFilePath = stateFilePathFull;
12878
12879 /* The below i_setMachineState() will detect the state transition and will
12880 * update the settings file */
12881
12882 return i_setMachineState(MachineState_Saved);
12883}
12884
12885/**
12886 * @note Locks this object for writing.
12887 */
12888HRESULT SessionMachine::discardSavedState(BOOL aFRemoveFile)
12889{
12890 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
12891
12892 HRESULT rc = i_checkStateDependency(MutableOrSavedStateDep);
12893 if (FAILED(rc)) return rc;
12894
12895 if (mData->mMachineState != MachineState_Saved)
12896 return setError(VBOX_E_INVALID_VM_STATE,
12897 tr("Cannot delete the machine state as the machine is not in the saved state (machine state: %s)"),
12898 Global::stringifyMachineState(mData->mMachineState));
12899
12900 mRemoveSavedState = RT_BOOL(aFRemoveFile);
12901
12902 /*
12903 * Saved -> PoweredOff transition will be detected in the SessionMachine
12904 * and properly handled.
12905 */
12906 rc = i_setMachineState(MachineState_PoweredOff);
12907 return rc;
12908}
12909
12910
12911/**
12912 * @note Locks the same as #i_setMachineState() does.
12913 */
12914HRESULT SessionMachine::updateState(MachineState_T aState)
12915{
12916 return i_setMachineState(aState);
12917}
12918
12919/**
12920 * @note Locks this object for writing.
12921 */
12922HRESULT SessionMachine::beginPowerUp(const ComPtr<IProgress> &aProgress)
12923{
12924 IProgress* pProgress(aProgress);
12925
12926 LogFlowThisFunc(("aProgress=%p\n", pProgress));
12927
12928 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
12929
12930 if (mData->mSession.mState != SessionState_Locked)
12931 return VBOX_E_INVALID_OBJECT_STATE;
12932
12933 if (!mData->mSession.mProgress.isNull())
12934 mData->mSession.mProgress->setOtherProgressObject(pProgress);
12935
12936 /* If we didn't reference the NAT network service yet, add a reference to
12937 * force a start */
12938 if (miNATNetworksStarted < 1)
12939 {
12940 for (ULONG slot = 0; slot < mNetworkAdapters.size(); ++slot)
12941 {
12942 NetworkAttachmentType_T type;
12943 HRESULT hrc;
12944 hrc = mNetworkAdapters[slot]->COMGETTER(AttachmentType)(&type);
12945 if ( SUCCEEDED(hrc)
12946 && type == NetworkAttachmentType_NATNetwork)
12947 {
12948 Bstr name;
12949 hrc = mNetworkAdapters[slot]->COMGETTER(NATNetwork)(name.asOutParam());
12950 if (SUCCEEDED(hrc))
12951 {
12952 LogRel(("VM '%s' starts using NAT network '%ls'\n",
12953 mUserData->s.strName.c_str(), name.raw()));
12954 mPeer->lockHandle()->unlockWrite();
12955 mParent->i_natNetworkRefInc(name.raw());
12956#ifdef RT_LOCK_STRICT
12957 mPeer->lockHandle()->lockWrite(RT_SRC_POS);
12958#else
12959 mPeer->lockHandle()->lockWrite();
12960#endif
12961 }
12962 }
12963 }
12964 miNATNetworksStarted++;
12965 }
12966
12967 LogFlowThisFunc(("returns S_OK.\n"));
12968 return S_OK;
12969}
12970
12971/**
12972 * @note Locks this object for writing.
12973 */
12974HRESULT SessionMachine::endPowerUp(LONG aResult)
12975{
12976 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
12977
12978 if (mData->mSession.mState != SessionState_Locked)
12979 return VBOX_E_INVALID_OBJECT_STATE;
12980
12981 /* Finalize the LaunchVMProcess progress object. */
12982 if (mData->mSession.mProgress)
12983 {
12984 mData->mSession.mProgress->notifyComplete((HRESULT)aResult);
12985 mData->mSession.mProgress.setNull();
12986 }
12987
12988 if (SUCCEEDED((HRESULT)aResult))
12989 {
12990#ifdef VBOX_WITH_RESOURCE_USAGE_API
12991 /* The VM has been powered up successfully, so it makes sense
12992 * now to offer the performance metrics for a running machine
12993 * object. Doing it earlier wouldn't be safe. */
12994 i_registerMetrics(mParent->i_performanceCollector(), mPeer,
12995 mData->mSession.mPID);
12996#endif /* VBOX_WITH_RESOURCE_USAGE_API */
12997 }
12998
12999 return S_OK;
13000}
13001
13002/**
13003 * @note Locks this object for writing.
13004 */
13005HRESULT SessionMachine::beginPoweringDown(ComPtr<IProgress> &aProgress)
13006{
13007 LogFlowThisFuncEnter();
13008
13009 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
13010
13011 AssertReturn(mConsoleTaskData.mLastState == MachineState_Null,
13012 E_FAIL);
13013
13014 /* create a progress object to track operation completion */
13015 ComObjPtr<Progress> pProgress;
13016 pProgress.createObject();
13017 pProgress->init(i_getVirtualBox(),
13018 static_cast<IMachine *>(this) /* aInitiator */,
13019 Bstr(tr("Stopping the virtual machine")).raw(),
13020 FALSE /* aCancelable */);
13021
13022 /* fill in the console task data */
13023 mConsoleTaskData.mLastState = mData->mMachineState;
13024 mConsoleTaskData.mProgress = pProgress;
13025
13026 /* set the state to Stopping (this is expected by Console::PowerDown()) */
13027 i_setMachineState(MachineState_Stopping);
13028
13029 pProgress.queryInterfaceTo(aProgress.asOutParam());
13030
13031 return S_OK;
13032}
13033
13034/**
13035 * @note Locks this object for writing.
13036 */
13037HRESULT SessionMachine::endPoweringDown(LONG aResult,
13038 const com::Utf8Str &aErrMsg)
13039{
13040 LogFlowThisFuncEnter();
13041
13042 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
13043
13044 AssertReturn( ( (SUCCEEDED(aResult) && mData->mMachineState == MachineState_PoweredOff)
13045 || (FAILED(aResult) && mData->mMachineState == MachineState_Stopping))
13046 && mConsoleTaskData.mLastState != MachineState_Null,
13047 E_FAIL);
13048
13049 /*
13050 * On failure, set the state to the state we had when BeginPoweringDown()
13051 * was called (this is expected by Console::PowerDown() and the associated
13052 * task). On success the VM process already changed the state to
13053 * MachineState_PoweredOff, so no need to do anything.
13054 */
13055 if (FAILED(aResult))
13056 i_setMachineState(mConsoleTaskData.mLastState);
13057
13058 /* notify the progress object about operation completion */
13059 Assert(mConsoleTaskData.mProgress);
13060 if (SUCCEEDED(aResult))
13061 mConsoleTaskData.mProgress->i_notifyComplete(S_OK);
13062 else
13063 {
13064 if (aErrMsg.length())
13065 mConsoleTaskData.mProgress->i_notifyComplete(aResult,
13066 COM_IIDOF(ISession),
13067 getComponentName(),
13068 aErrMsg.c_str());
13069 else
13070 mConsoleTaskData.mProgress->i_notifyComplete(aResult);
13071 }
13072
13073 /* clear out the temporary saved state data */
13074 mConsoleTaskData.mLastState = MachineState_Null;
13075 mConsoleTaskData.mProgress.setNull();
13076
13077 LogFlowThisFuncLeave();
13078 return S_OK;
13079}
13080
13081
13082/**
13083 * Goes through the USB filters of the given machine to see if the given
13084 * device matches any filter or not.
13085 *
13086 * @note Locks the same as USBController::hasMatchingFilter() does.
13087 */
13088HRESULT SessionMachine::runUSBDeviceFilters(const ComPtr<IUSBDevice> &aDevice,
13089 BOOL *aMatched,
13090 ULONG *aMaskedInterfaces)
13091{
13092 LogFlowThisFunc(("\n"));
13093
13094#ifdef VBOX_WITH_USB
13095 *aMatched = mUSBDeviceFilters->i_hasMatchingFilter(aDevice, aMaskedInterfaces);
13096#else
13097 NOREF(aDevice);
13098 NOREF(aMaskedInterfaces);
13099 *aMatched = FALSE;
13100#endif
13101
13102 return S_OK;
13103}
13104
13105/**
13106 * @note Locks the same as Host::captureUSBDevice() does.
13107 */
13108HRESULT SessionMachine::captureUSBDevice(const com::Guid &aId, const com::Utf8Str &aCaptureFilename)
13109{
13110 LogFlowThisFunc(("\n"));
13111
13112#ifdef VBOX_WITH_USB
13113 /* if captureDeviceForVM() fails, it must have set extended error info */
13114 clearError();
13115 MultiResult rc = mParent->i_host()->i_checkUSBProxyService();
13116 if (FAILED(rc)) return rc;
13117
13118 USBProxyService *service = mParent->i_host()->i_usbProxyService();
13119 AssertReturn(service, E_FAIL);
13120 return service->captureDeviceForVM(this, aId.ref(), aCaptureFilename);
13121#else
13122 NOREF(aId);
13123 return E_NOTIMPL;
13124#endif
13125}
13126
13127/**
13128 * @note Locks the same as Host::detachUSBDevice() does.
13129 */
13130HRESULT SessionMachine::detachUSBDevice(const com::Guid &aId,
13131 BOOL aDone)
13132{
13133 LogFlowThisFunc(("\n"));
13134
13135#ifdef VBOX_WITH_USB
13136 USBProxyService *service = mParent->i_host()->i_usbProxyService();
13137 AssertReturn(service, E_FAIL);
13138 return service->detachDeviceFromVM(this, aId.ref(), !!aDone);
13139#else
13140 NOREF(aId);
13141 NOREF(aDone);
13142 return E_NOTIMPL;
13143#endif
13144}
13145
13146/**
13147 * Inserts all machine filters to the USB proxy service and then calls
13148 * Host::autoCaptureUSBDevices().
13149 *
13150 * Called by Console from the VM process upon VM startup.
13151 *
13152 * @note Locks what called methods lock.
13153 */
13154HRESULT SessionMachine::autoCaptureUSBDevices()
13155{
13156 LogFlowThisFunc(("\n"));
13157
13158#ifdef VBOX_WITH_USB
13159 HRESULT rc = mUSBDeviceFilters->i_notifyProxy(true /* aInsertFilters */);
13160 AssertComRC(rc);
13161 NOREF(rc);
13162
13163 USBProxyService *service = mParent->i_host()->i_usbProxyService();
13164 AssertReturn(service, E_FAIL);
13165 return service->autoCaptureDevicesForVM(this);
13166#else
13167 return S_OK;
13168#endif
13169}
13170
13171/**
13172 * Removes all machine filters from the USB proxy service and then calls
13173 * Host::detachAllUSBDevices().
13174 *
13175 * Called by Console from the VM process upon normal VM termination or by
13176 * SessionMachine::uninit() upon abnormal VM termination (from under the
13177 * Machine/SessionMachine lock).
13178 *
13179 * @note Locks what called methods lock.
13180 */
13181HRESULT SessionMachine::detachAllUSBDevices(BOOL aDone)
13182{
13183 LogFlowThisFunc(("\n"));
13184
13185#ifdef VBOX_WITH_USB
13186 HRESULT rc = mUSBDeviceFilters->i_notifyProxy(false /* aInsertFilters */);
13187 AssertComRC(rc);
13188 NOREF(rc);
13189
13190 USBProxyService *service = mParent->i_host()->i_usbProxyService();
13191 AssertReturn(service, E_FAIL);
13192 return service->detachAllDevicesFromVM(this, !!aDone, false /* aAbnormal */);
13193#else
13194 NOREF(aDone);
13195 return S_OK;
13196#endif
13197}
13198
13199/**
13200 * @note Locks this object for writing.
13201 */
13202HRESULT SessionMachine::onSessionEnd(const ComPtr<ISession> &aSession,
13203 ComPtr<IProgress> &aProgress)
13204{
13205 LogFlowThisFuncEnter();
13206
13207 LogFlowThisFunc(("callerstate=%d\n", getObjectState().getState()));
13208 /*
13209 * We don't assert below because it might happen that a non-direct session
13210 * informs us it is closed right after we've been uninitialized -- it's ok.
13211 */
13212
13213 /* get IInternalSessionControl interface */
13214 ComPtr<IInternalSessionControl> control(aSession);
13215
13216 ComAssertRet(!control.isNull(), E_INVALIDARG);
13217
13218 /* Creating a Progress object requires the VirtualBox lock, and
13219 * thus locking it here is required by the lock order rules. */
13220 AutoMultiWriteLock2 alock(mParent, this COMMA_LOCKVAL_SRC_POS);
13221
13222 if (control == mData->mSession.mDirectControl)
13223 {
13224 /* The direct session is being normally closed by the client process
13225 * ----------------------------------------------------------------- */
13226
13227 /* go to the closing state (essential for all open*Session() calls and
13228 * for #checkForDeath()) */
13229 Assert(mData->mSession.mState == SessionState_Locked);
13230 mData->mSession.mState = SessionState_Unlocking;
13231
13232 /* set direct control to NULL to release the remote instance */
13233 mData->mSession.mDirectControl.setNull();
13234 LogFlowThisFunc(("Direct control is set to NULL\n"));
13235
13236 if (mData->mSession.mProgress)
13237 {
13238 /* finalize the progress, someone might wait if a frontend
13239 * closes the session before powering on the VM. */
13240 mData->mSession.mProgress->notifyComplete(E_FAIL,
13241 COM_IIDOF(ISession),
13242 getComponentName(),
13243 tr("The VM session was closed before any attempt to power it on"));
13244 mData->mSession.mProgress.setNull();
13245 }
13246
13247 /* Create the progress object the client will use to wait until
13248 * #checkForDeath() is called to uninitialize this session object after
13249 * it releases the IPC semaphore.
13250 * Note! Because we're "reusing" mProgress here, this must be a proxy
13251 * object just like for LaunchVMProcess. */
13252 Assert(mData->mSession.mProgress.isNull());
13253 ComObjPtr<ProgressProxy> progress;
13254 progress.createObject();
13255 ComPtr<IUnknown> pPeer(mPeer);
13256 progress->init(mParent, pPeer,
13257 Bstr(tr("Closing session")).raw(),
13258 FALSE /* aCancelable */);
13259 progress.queryInterfaceTo(aProgress.asOutParam());
13260 mData->mSession.mProgress = progress;
13261 }
13262 else
13263 {
13264 /* the remote session is being normally closed */
13265 Data::Session::RemoteControlList::iterator it =
13266 mData->mSession.mRemoteControls.begin();
13267 while (it != mData->mSession.mRemoteControls.end())
13268 {
13269 if (control == *it)
13270 break;
13271 ++it;
13272 }
13273 BOOL found = it != mData->mSession.mRemoteControls.end();
13274 ComAssertMsgRet(found, ("The session is not found in the session list!"),
13275 E_INVALIDARG);
13276 // This MUST be erase(it), not remove(*it) as the latter triggers a
13277 // very nasty use after free due to the place where the value "lives".
13278 mData->mSession.mRemoteControls.erase(it);
13279 }
13280
13281 /* signal the client watcher thread, because the client is going away */
13282 mParent->i_updateClientWatcher();
13283
13284 LogFlowThisFuncLeave();
13285 return S_OK;
13286}
13287
13288HRESULT SessionMachine::pullGuestProperties(std::vector<com::Utf8Str> &aNames,
13289 std::vector<com::Utf8Str> &aValues,
13290 std::vector<LONG64> &aTimestamps,
13291 std::vector<com::Utf8Str> &aFlags)
13292{
13293 LogFlowThisFunc(("\n"));
13294
13295#ifdef VBOX_WITH_GUEST_PROPS
13296 using namespace guestProp;
13297
13298 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
13299
13300 size_t cEntries = mHWData->mGuestProperties.size();
13301 aNames.resize(cEntries);
13302 aValues.resize(cEntries);
13303 aTimestamps.resize(cEntries);
13304 aFlags.resize(cEntries);
13305
13306 size_t i = 0;
13307 for (HWData::GuestPropertyMap::iterator it = mHWData->mGuestProperties.begin();
13308 it != mHWData->mGuestProperties.end();
13309 ++it, ++i)
13310 {
13311 char szFlags[MAX_FLAGS_LEN + 1];
13312 aNames[i] = it->first;
13313 aValues[i] = it->second.strValue;
13314 aTimestamps[i] = it->second.mTimestamp;
13315
13316 /* If it is NULL, keep it NULL. */
13317 if (it->second.mFlags)
13318 {
13319 writeFlags(it->second.mFlags, szFlags);
13320 aFlags[i] = szFlags;
13321 }
13322 else
13323 aFlags[i] = "";
13324 }
13325 return S_OK;
13326#else
13327 ReturnComNotImplemented();
13328#endif
13329}
13330
13331HRESULT SessionMachine::pushGuestProperty(const com::Utf8Str &aName,
13332 const com::Utf8Str &aValue,
13333 LONG64 aTimestamp,
13334 const com::Utf8Str &aFlags)
13335{
13336 LogFlowThisFunc(("\n"));
13337
13338#ifdef VBOX_WITH_GUEST_PROPS
13339 using namespace guestProp;
13340
13341 try
13342 {
13343 /*
13344 * Convert input up front.
13345 */
13346 uint32_t fFlags = NILFLAG;
13347 if (aFlags.length())
13348 {
13349 int vrc = validateFlags(aFlags.c_str(), &fFlags);
13350 AssertRCReturn(vrc, E_INVALIDARG);
13351 }
13352
13353 /*
13354 * Now grab the object lock, validate the state and do the update.
13355 */
13356
13357 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
13358
13359 switch (mData->mMachineState)
13360 {
13361 case MachineState_Paused:
13362 case MachineState_Running:
13363 case MachineState_Teleporting:
13364 case MachineState_TeleportingPausedVM:
13365 case MachineState_OnlineSnapshotting:
13366 case MachineState_LiveSnapshotting:
13367 case MachineState_DeletingSnapshotOnline:
13368 case MachineState_DeletingSnapshotPaused:
13369 case MachineState_Saving:
13370 case MachineState_Stopping:
13371 break;
13372
13373 default:
13374 AssertMsgFailedReturn(("%s\n", Global::stringifyMachineState(mData->mMachineState)),
13375 VBOX_E_INVALID_VM_STATE);
13376 }
13377
13378 i_setModified(IsModified_MachineData);
13379 mHWData.backup();
13380
13381 bool fDelete = !aValue.length();
13382 HWData::GuestPropertyMap::iterator it = mHWData->mGuestProperties.find(aName);
13383 if (it != mHWData->mGuestProperties.end())
13384 {
13385 if (!fDelete)
13386 {
13387 it->second.strValue = aValue;
13388 it->second.mTimestamp = aTimestamp;
13389 it->second.mFlags = fFlags;
13390 }
13391 else
13392 mHWData->mGuestProperties.erase(it);
13393
13394 mData->mGuestPropertiesModified = TRUE;
13395 }
13396 else if (!fDelete)
13397 {
13398 HWData::GuestProperty prop;
13399 prop.strValue = aValue;
13400 prop.mTimestamp = aTimestamp;
13401 prop.mFlags = fFlags;
13402
13403 mHWData->mGuestProperties[aName] = prop;
13404 mData->mGuestPropertiesModified = TRUE;
13405 }
13406
13407 alock.release();
13408
13409 mParent->i_onGuestPropertyChange(mData->mUuid,
13410 Bstr(aName).raw(),
13411 Bstr(aValue).raw(),
13412 Bstr(aFlags).raw());
13413 }
13414 catch (...)
13415 {
13416 return VirtualBoxBase::handleUnexpectedExceptions(this, RT_SRC_POS);
13417 }
13418 return S_OK;
13419#else
13420 ReturnComNotImplemented();
13421#endif
13422}
13423
13424
13425HRESULT SessionMachine::lockMedia()
13426{
13427 AutoMultiWriteLock2 alock(this->lockHandle(),
13428 &mParent->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
13429
13430 AssertReturn( mData->mMachineState == MachineState_Starting
13431 || mData->mMachineState == MachineState_Restoring
13432 || mData->mMachineState == MachineState_TeleportingIn, E_FAIL);
13433
13434 clearError();
13435 alock.release();
13436 return i_lockMedia();
13437}
13438
13439HRESULT SessionMachine::unlockMedia()
13440{
13441 HRESULT hrc = i_unlockMedia();
13442 return hrc;
13443}
13444
13445HRESULT SessionMachine::ejectMedium(const ComPtr<IMediumAttachment> &aAttachment,
13446 ComPtr<IMediumAttachment> &aNewAttachment)
13447{
13448 // request the host lock first, since might be calling Host methods for getting host drives;
13449 // next, protect the media tree all the while we're in here, as well as our member variables
13450 AutoMultiWriteLock3 multiLock(mParent->i_host()->lockHandle(),
13451 this->lockHandle(),
13452 &mParent->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
13453
13454 IMediumAttachment *iAttach = aAttachment;
13455 ComObjPtr<MediumAttachment> pAttach = static_cast<MediumAttachment *>(iAttach);
13456
13457 Bstr ctrlName;
13458 LONG lPort;
13459 LONG lDevice;
13460 bool fTempEject;
13461 {
13462 AutoReadLock attLock(pAttach COMMA_LOCKVAL_SRC_POS);
13463
13464 /* Need to query the details first, as the IMediumAttachment reference
13465 * might be to the original settings, which we are going to change. */
13466 ctrlName = pAttach->i_getControllerName();
13467 lPort = pAttach->i_getPort();
13468 lDevice = pAttach->i_getDevice();
13469 fTempEject = pAttach->i_getTempEject();
13470 }
13471
13472 if (!fTempEject)
13473 {
13474 /* Remember previously mounted medium. The medium before taking the
13475 * backup is not necessarily the same thing. */
13476 ComObjPtr<Medium> oldmedium;
13477 oldmedium = pAttach->i_getMedium();
13478
13479 i_setModified(IsModified_Storage);
13480 mMediaData.backup();
13481
13482 // The backup operation makes the pAttach reference point to the
13483 // old settings. Re-get the correct reference.
13484 pAttach = i_findAttachment(mMediaData->mAttachments,
13485 ctrlName.raw(),
13486 lPort,
13487 lDevice);
13488
13489 {
13490 AutoCaller autoAttachCaller(this);
13491 if (FAILED(autoAttachCaller.rc())) return autoAttachCaller.rc();
13492
13493 AutoWriteLock attLock(pAttach COMMA_LOCKVAL_SRC_POS);
13494 if (!oldmedium.isNull())
13495 oldmedium->i_removeBackReference(mData->mUuid);
13496
13497 pAttach->i_updateMedium(NULL);
13498 pAttach->i_updateEjected();
13499 }
13500
13501 i_setModified(IsModified_Storage);
13502 }
13503 else
13504 {
13505 {
13506 AutoWriteLock attLock(pAttach COMMA_LOCKVAL_SRC_POS);
13507 pAttach->i_updateEjected();
13508 }
13509 }
13510
13511 pAttach.queryInterfaceTo(aNewAttachment.asOutParam());
13512
13513 return S_OK;
13514}
13515
13516// public methods only for internal purposes
13517/////////////////////////////////////////////////////////////////////////////
13518
13519#ifndef VBOX_WITH_GENERIC_SESSION_WATCHER
13520/**
13521 * Called from the client watcher thread to check for expected or unexpected
13522 * death of the client process that has a direct session to this machine.
13523 *
13524 * On Win32 and on OS/2, this method is called only when we've got the
13525 * mutex (i.e. the client has either died or terminated normally) so it always
13526 * returns @c true (the client is terminated, the session machine is
13527 * uninitialized).
13528 *
13529 * On other platforms, the method returns @c true if the client process has
13530 * terminated normally or abnormally and the session machine was uninitialized,
13531 * and @c false if the client process is still alive.
13532 *
13533 * @note Locks this object for writing.
13534 */
13535bool SessionMachine::i_checkForDeath()
13536{
13537 Uninit::Reason reason;
13538 bool terminated = false;
13539
13540 /* Enclose autoCaller with a block because calling uninit() from under it
13541 * will deadlock. */
13542 {
13543 AutoCaller autoCaller(this);
13544 if (!autoCaller.isOk())
13545 {
13546 /* return true if not ready, to cause the client watcher to exclude
13547 * the corresponding session from watching */
13548 LogFlowThisFunc(("Already uninitialized!\n"));
13549 return true;
13550 }
13551
13552 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
13553
13554 /* Determine the reason of death: if the session state is Closing here,
13555 * everything is fine. Otherwise it means that the client did not call
13556 * OnSessionEnd() before it released the IPC semaphore. This may happen
13557 * either because the client process has abnormally terminated, or
13558 * because it simply forgot to call ISession::Close() before exiting. We
13559 * threat the latter also as an abnormal termination (see
13560 * Session::uninit() for details). */
13561 reason = mData->mSession.mState == SessionState_Unlocking ?
13562 Uninit::Normal :
13563 Uninit::Abnormal;
13564
13565 if (mClientToken)
13566 terminated = mClientToken->release();
13567 } /* AutoCaller block */
13568
13569 if (terminated)
13570 uninit(reason);
13571
13572 return terminated;
13573}
13574
13575void SessionMachine::i_getTokenId(Utf8Str &strTokenId)
13576{
13577 LogFlowThisFunc(("\n"));
13578
13579 strTokenId.setNull();
13580
13581 AutoCaller autoCaller(this);
13582 AssertComRCReturnVoid(autoCaller.rc());
13583
13584 Assert(mClientToken);
13585 if (mClientToken)
13586 mClientToken->getId(strTokenId);
13587}
13588#else /* VBOX_WITH_GENERIC_SESSION_WATCHER */
13589IToken *SessionMachine::i_getToken()
13590{
13591 LogFlowThisFunc(("\n"));
13592
13593 AutoCaller autoCaller(this);
13594 AssertComRCReturn(autoCaller.rc(), NULL);
13595
13596 Assert(mClientToken);
13597 if (mClientToken)
13598 return mClientToken->getToken();
13599 else
13600 return NULL;
13601}
13602#endif /* VBOX_WITH_GENERIC_SESSION_WATCHER */
13603
13604Machine::ClientToken *SessionMachine::i_getClientToken()
13605{
13606 LogFlowThisFunc(("\n"));
13607
13608 AutoCaller autoCaller(this);
13609 AssertComRCReturn(autoCaller.rc(), NULL);
13610
13611 return mClientToken;
13612}
13613
13614
13615/**
13616 * @note Locks this object for reading.
13617 */
13618HRESULT SessionMachine::i_onNetworkAdapterChange(INetworkAdapter *networkAdapter, BOOL changeAdapter)
13619{
13620 LogFlowThisFunc(("\n"));
13621
13622 AutoCaller autoCaller(this);
13623 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
13624
13625 ComPtr<IInternalSessionControl> directControl;
13626 {
13627 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
13628 if (mData->mSession.mLockType == LockType_VM)
13629 directControl = mData->mSession.mDirectControl;
13630 }
13631
13632 /* ignore notifications sent after #OnSessionEnd() is called */
13633 if (!directControl)
13634 return S_OK;
13635
13636 return directControl->OnNetworkAdapterChange(networkAdapter, changeAdapter);
13637}
13638
13639/**
13640 * @note Locks this object for reading.
13641 */
13642HRESULT SessionMachine::i_onNATRedirectRuleChange(ULONG ulSlot, BOOL aNatRuleRemove, IN_BSTR aRuleName,
13643 NATProtocol_T aProto, IN_BSTR aHostIp, LONG aHostPort,
13644 IN_BSTR aGuestIp, LONG aGuestPort)
13645{
13646 LogFlowThisFunc(("\n"));
13647
13648 AutoCaller autoCaller(this);
13649 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
13650
13651 ComPtr<IInternalSessionControl> directControl;
13652 {
13653 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
13654 if (mData->mSession.mLockType == LockType_VM)
13655 directControl = mData->mSession.mDirectControl;
13656 }
13657
13658 /* ignore notifications sent after #OnSessionEnd() is called */
13659 if (!directControl)
13660 return S_OK;
13661 /*
13662 * instead acting like callback we ask IVirtualBox deliver corresponding event
13663 */
13664
13665 mParent->i_onNatRedirectChange(i_getId(), ulSlot, RT_BOOL(aNatRuleRemove), aRuleName, aProto, aHostIp,
13666 (uint16_t)aHostPort, aGuestIp, (uint16_t)aGuestPort);
13667 return S_OK;
13668}
13669
13670/**
13671 * @note Locks this object for reading.
13672 */
13673HRESULT SessionMachine::i_onSerialPortChange(ISerialPort *serialPort)
13674{
13675 LogFlowThisFunc(("\n"));
13676
13677 AutoCaller autoCaller(this);
13678 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
13679
13680 ComPtr<IInternalSessionControl> directControl;
13681 {
13682 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
13683 if (mData->mSession.mLockType == LockType_VM)
13684 directControl = mData->mSession.mDirectControl;
13685 }
13686
13687 /* ignore notifications sent after #OnSessionEnd() is called */
13688 if (!directControl)
13689 return S_OK;
13690
13691 return directControl->OnSerialPortChange(serialPort);
13692}
13693
13694/**
13695 * @note Locks this object for reading.
13696 */
13697HRESULT SessionMachine::i_onParallelPortChange(IParallelPort *parallelPort)
13698{
13699 LogFlowThisFunc(("\n"));
13700
13701 AutoCaller autoCaller(this);
13702 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
13703
13704 ComPtr<IInternalSessionControl> directControl;
13705 {
13706 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
13707 if (mData->mSession.mLockType == LockType_VM)
13708 directControl = mData->mSession.mDirectControl;
13709 }
13710
13711 /* ignore notifications sent after #OnSessionEnd() is called */
13712 if (!directControl)
13713 return S_OK;
13714
13715 return directControl->OnParallelPortChange(parallelPort);
13716}
13717
13718/**
13719 * @note Locks this object for reading.
13720 */
13721HRESULT SessionMachine::i_onStorageControllerChange()
13722{
13723 LogFlowThisFunc(("\n"));
13724
13725 AutoCaller autoCaller(this);
13726 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
13727
13728 ComPtr<IInternalSessionControl> directControl;
13729 {
13730 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
13731 if (mData->mSession.mLockType == LockType_VM)
13732 directControl = mData->mSession.mDirectControl;
13733 }
13734
13735 /* ignore notifications sent after #OnSessionEnd() is called */
13736 if (!directControl)
13737 return S_OK;
13738
13739 return directControl->OnStorageControllerChange();
13740}
13741
13742/**
13743 * @note Locks this object for reading.
13744 */
13745HRESULT SessionMachine::i_onMediumChange(IMediumAttachment *aAttachment, BOOL aForce)
13746{
13747 LogFlowThisFunc(("\n"));
13748
13749 AutoCaller autoCaller(this);
13750 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
13751
13752 ComPtr<IInternalSessionControl> directControl;
13753 {
13754 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
13755 if (mData->mSession.mLockType == LockType_VM)
13756 directControl = mData->mSession.mDirectControl;
13757 }
13758
13759 /* ignore notifications sent after #OnSessionEnd() is called */
13760 if (!directControl)
13761 return S_OK;
13762
13763 return directControl->OnMediumChange(aAttachment, aForce);
13764}
13765
13766/**
13767 * @note Locks this object for reading.
13768 */
13769HRESULT SessionMachine::i_onCPUChange(ULONG aCPU, BOOL aRemove)
13770{
13771 LogFlowThisFunc(("\n"));
13772
13773 AutoCaller autoCaller(this);
13774 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
13775
13776 ComPtr<IInternalSessionControl> directControl;
13777 {
13778 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
13779 if (mData->mSession.mLockType == LockType_VM)
13780 directControl = mData->mSession.mDirectControl;
13781 }
13782
13783 /* ignore notifications sent after #OnSessionEnd() is called */
13784 if (!directControl)
13785 return S_OK;
13786
13787 return directControl->OnCPUChange(aCPU, aRemove);
13788}
13789
13790HRESULT SessionMachine::i_onCPUExecutionCapChange(ULONG aExecutionCap)
13791{
13792 LogFlowThisFunc(("\n"));
13793
13794 AutoCaller autoCaller(this);
13795 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
13796
13797 ComPtr<IInternalSessionControl> directControl;
13798 {
13799 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
13800 if (mData->mSession.mLockType == LockType_VM)
13801 directControl = mData->mSession.mDirectControl;
13802 }
13803
13804 /* ignore notifications sent after #OnSessionEnd() is called */
13805 if (!directControl)
13806 return S_OK;
13807
13808 return directControl->OnCPUExecutionCapChange(aExecutionCap);
13809}
13810
13811/**
13812 * @note Locks this object for reading.
13813 */
13814HRESULT SessionMachine::i_onVRDEServerChange(BOOL aRestart)
13815{
13816 LogFlowThisFunc(("\n"));
13817
13818 AutoCaller autoCaller(this);
13819 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
13820
13821 ComPtr<IInternalSessionControl> directControl;
13822 {
13823 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
13824 if (mData->mSession.mLockType == LockType_VM)
13825 directControl = mData->mSession.mDirectControl;
13826 }
13827
13828 /* ignore notifications sent after #OnSessionEnd() is called */
13829 if (!directControl)
13830 return S_OK;
13831
13832 return directControl->OnVRDEServerChange(aRestart);
13833}
13834
13835/**
13836 * @note Locks this object for reading.
13837 */
13838HRESULT SessionMachine::i_onVideoCaptureChange()
13839{
13840 LogFlowThisFunc(("\n"));
13841
13842 AutoCaller autoCaller(this);
13843 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
13844
13845 ComPtr<IInternalSessionControl> directControl;
13846 {
13847 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
13848 if (mData->mSession.mLockType == LockType_VM)
13849 directControl = mData->mSession.mDirectControl;
13850 }
13851
13852 /* ignore notifications sent after #OnSessionEnd() is called */
13853 if (!directControl)
13854 return S_OK;
13855
13856 return directControl->OnVideoCaptureChange();
13857}
13858
13859/**
13860 * @note Locks this object for reading.
13861 */
13862HRESULT SessionMachine::i_onUSBControllerChange()
13863{
13864 LogFlowThisFunc(("\n"));
13865
13866 AutoCaller autoCaller(this);
13867 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
13868
13869 ComPtr<IInternalSessionControl> directControl;
13870 {
13871 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
13872 if (mData->mSession.mLockType == LockType_VM)
13873 directControl = mData->mSession.mDirectControl;
13874 }
13875
13876 /* ignore notifications sent after #OnSessionEnd() is called */
13877 if (!directControl)
13878 return S_OK;
13879
13880 return directControl->OnUSBControllerChange();
13881}
13882
13883/**
13884 * @note Locks this object for reading.
13885 */
13886HRESULT SessionMachine::i_onSharedFolderChange()
13887{
13888 LogFlowThisFunc(("\n"));
13889
13890 AutoCaller autoCaller(this);
13891 AssertComRCReturnRC(autoCaller.rc());
13892
13893 ComPtr<IInternalSessionControl> directControl;
13894 {
13895 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
13896 if (mData->mSession.mLockType == LockType_VM)
13897 directControl = mData->mSession.mDirectControl;
13898 }
13899
13900 /* ignore notifications sent after #OnSessionEnd() is called */
13901 if (!directControl)
13902 return S_OK;
13903
13904 return directControl->OnSharedFolderChange(FALSE /* aGlobal */);
13905}
13906
13907/**
13908 * @note Locks this object for reading.
13909 */
13910HRESULT SessionMachine::i_onClipboardModeChange(ClipboardMode_T aClipboardMode)
13911{
13912 LogFlowThisFunc(("\n"));
13913
13914 AutoCaller autoCaller(this);
13915 AssertComRCReturnRC(autoCaller.rc());
13916
13917 ComPtr<IInternalSessionControl> directControl;
13918 {
13919 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
13920 if (mData->mSession.mLockType == LockType_VM)
13921 directControl = mData->mSession.mDirectControl;
13922 }
13923
13924 /* ignore notifications sent after #OnSessionEnd() is called */
13925 if (!directControl)
13926 return S_OK;
13927
13928 return directControl->OnClipboardModeChange(aClipboardMode);
13929}
13930
13931/**
13932 * @note Locks this object for reading.
13933 */
13934HRESULT SessionMachine::i_onDnDModeChange(DnDMode_T aDnDMode)
13935{
13936 LogFlowThisFunc(("\n"));
13937
13938 AutoCaller autoCaller(this);
13939 AssertComRCReturnRC(autoCaller.rc());
13940
13941 ComPtr<IInternalSessionControl> directControl;
13942 {
13943 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
13944 if (mData->mSession.mLockType == LockType_VM)
13945 directControl = mData->mSession.mDirectControl;
13946 }
13947
13948 /* ignore notifications sent after #OnSessionEnd() is called */
13949 if (!directControl)
13950 return S_OK;
13951
13952 return directControl->OnDnDModeChange(aDnDMode);
13953}
13954
13955/**
13956 * @note Locks this object for reading.
13957 */
13958HRESULT SessionMachine::i_onBandwidthGroupChange(IBandwidthGroup *aBandwidthGroup)
13959{
13960 LogFlowThisFunc(("\n"));
13961
13962 AutoCaller autoCaller(this);
13963 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
13964
13965 ComPtr<IInternalSessionControl> directControl;
13966 {
13967 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
13968 if (mData->mSession.mLockType == LockType_VM)
13969 directControl = mData->mSession.mDirectControl;
13970 }
13971
13972 /* ignore notifications sent after #OnSessionEnd() is called */
13973 if (!directControl)
13974 return S_OK;
13975
13976 return directControl->OnBandwidthGroupChange(aBandwidthGroup);
13977}
13978
13979/**
13980 * @note Locks this object for reading.
13981 */
13982HRESULT SessionMachine::i_onStorageDeviceChange(IMediumAttachment *aAttachment, BOOL aRemove, BOOL aSilent)
13983{
13984 LogFlowThisFunc(("\n"));
13985
13986 AutoCaller autoCaller(this);
13987 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
13988
13989 ComPtr<IInternalSessionControl> directControl;
13990 {
13991 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
13992 if (mData->mSession.mLockType == LockType_VM)
13993 directControl = mData->mSession.mDirectControl;
13994 }
13995
13996 /* ignore notifications sent after #OnSessionEnd() is called */
13997 if (!directControl)
13998 return S_OK;
13999
14000 return directControl->OnStorageDeviceChange(aAttachment, aRemove, aSilent);
14001}
14002
14003/**
14004 * Returns @c true if this machine's USB controller reports it has a matching
14005 * filter for the given USB device and @c false otherwise.
14006 *
14007 * @note locks this object for reading.
14008 */
14009bool SessionMachine::i_hasMatchingUSBFilter(const ComObjPtr<HostUSBDevice> &aDevice, ULONG *aMaskedIfs)
14010{
14011 AutoCaller autoCaller(this);
14012 /* silently return if not ready -- this method may be called after the
14013 * direct machine session has been called */
14014 if (!autoCaller.isOk())
14015 return false;
14016
14017#ifdef VBOX_WITH_USB
14018 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
14019
14020 switch (mData->mMachineState)
14021 {
14022 case MachineState_Starting:
14023 case MachineState_Restoring:
14024 case MachineState_TeleportingIn:
14025 case MachineState_Paused:
14026 case MachineState_Running:
14027 /** @todo Live Migration: snapshoting & teleporting. Need to fend things of
14028 * elsewhere... */
14029 alock.release();
14030 return mUSBDeviceFilters->i_hasMatchingFilter(aDevice, aMaskedIfs);
14031 default: break;
14032 }
14033#else
14034 NOREF(aDevice);
14035 NOREF(aMaskedIfs);
14036#endif
14037 return false;
14038}
14039
14040/**
14041 * @note The calls shall hold no locks. Will temporarily lock this object for reading.
14042 */
14043HRESULT SessionMachine::i_onUSBDeviceAttach(IUSBDevice *aDevice,
14044 IVirtualBoxErrorInfo *aError,
14045 ULONG aMaskedIfs,
14046 const com::Utf8Str &aCaptureFilename)
14047{
14048 LogFlowThisFunc(("\n"));
14049
14050 AutoCaller autoCaller(this);
14051
14052 /* This notification may happen after the machine object has been
14053 * uninitialized (the session was closed), so don't assert. */
14054 if (FAILED(autoCaller.rc())) return autoCaller.rc();
14055
14056 ComPtr<IInternalSessionControl> directControl;
14057 {
14058 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
14059 if (mData->mSession.mLockType == LockType_VM)
14060 directControl = mData->mSession.mDirectControl;
14061 }
14062
14063 /* fail on notifications sent after #OnSessionEnd() is called, it is
14064 * expected by the caller */
14065 if (!directControl)
14066 return E_FAIL;
14067
14068 /* No locks should be held at this point. */
14069 AssertMsg(RTLockValidatorWriteLockGetCount(RTThreadSelf()) == 0, ("%d\n", RTLockValidatorWriteLockGetCount(RTThreadSelf())));
14070 AssertMsg(RTLockValidatorReadLockGetCount(RTThreadSelf()) == 0, ("%d\n", RTLockValidatorReadLockGetCount(RTThreadSelf())));
14071
14072 return directControl->OnUSBDeviceAttach(aDevice, aError, aMaskedIfs, Bstr(aCaptureFilename).raw());
14073}
14074
14075/**
14076 * @note The calls shall hold no locks. Will temporarily lock this object for reading.
14077 */
14078HRESULT SessionMachine::i_onUSBDeviceDetach(IN_BSTR aId,
14079 IVirtualBoxErrorInfo *aError)
14080{
14081 LogFlowThisFunc(("\n"));
14082
14083 AutoCaller autoCaller(this);
14084
14085 /* This notification may happen after the machine object has been
14086 * uninitialized (the session was closed), so don't assert. */
14087 if (FAILED(autoCaller.rc())) return autoCaller.rc();
14088
14089 ComPtr<IInternalSessionControl> directControl;
14090 {
14091 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
14092 if (mData->mSession.mLockType == LockType_VM)
14093 directControl = mData->mSession.mDirectControl;
14094 }
14095
14096 /* fail on notifications sent after #OnSessionEnd() is called, it is
14097 * expected by the caller */
14098 if (!directControl)
14099 return E_FAIL;
14100
14101 /* No locks should be held at this point. */
14102 AssertMsg(RTLockValidatorWriteLockGetCount(RTThreadSelf()) == 0, ("%d\n", RTLockValidatorWriteLockGetCount(RTThreadSelf())));
14103 AssertMsg(RTLockValidatorReadLockGetCount(RTThreadSelf()) == 0, ("%d\n", RTLockValidatorReadLockGetCount(RTThreadSelf())));
14104
14105 return directControl->OnUSBDeviceDetach(aId, aError);
14106}
14107
14108// protected methods
14109/////////////////////////////////////////////////////////////////////////////
14110
14111/**
14112 * Deletes the given file if it is no longer in use by either the current machine state
14113 * (if the machine is "saved") or any of the machine's snapshots.
14114 *
14115 * Note: This checks mSSData->strStateFilePath, which is shared by the Machine and SessionMachine
14116 * but is different for each SnapshotMachine. When calling this, the order of calling this
14117 * function on the one hand and changing that variable OR the snapshots tree on the other hand
14118 * is therefore critical. I know, it's all rather messy.
14119 *
14120 * @param strStateFile
14121 * @param pSnapshotToIgnore Passed to Snapshot::sharesSavedStateFile(); this snapshot is ignored in
14122 * the test for whether the saved state file is in use.
14123 */
14124void SessionMachine::i_releaseSavedStateFile(const Utf8Str &strStateFile,
14125 Snapshot *pSnapshotToIgnore)
14126{
14127 // it is safe to delete this saved state file if it is not currently in use by the machine ...
14128 if ( (strStateFile.isNotEmpty())
14129 && (strStateFile != mSSData->strStateFilePath) // session machine's saved state
14130 )
14131 // ... and it must also not be shared with other snapshots
14132 if ( !mData->mFirstSnapshot
14133 || !mData->mFirstSnapshot->i_sharesSavedStateFile(strStateFile, pSnapshotToIgnore)
14134 // this checks the SnapshotMachine's state file paths
14135 )
14136 RTFileDelete(strStateFile.c_str());
14137}
14138
14139/**
14140 * Locks the attached media.
14141 *
14142 * All attached hard disks are locked for writing and DVD/floppy are locked for
14143 * reading. Parents of attached hard disks (if any) are locked for reading.
14144 *
14145 * This method also performs accessibility check of all media it locks: if some
14146 * media is inaccessible, the method will return a failure and a bunch of
14147 * extended error info objects per each inaccessible medium.
14148 *
14149 * Note that this method is atomic: if it returns a success, all media are
14150 * locked as described above; on failure no media is locked at all (all
14151 * succeeded individual locks will be undone).
14152 *
14153 * The caller is responsible for doing the necessary state sanity checks.
14154 *
14155 * The locks made by this method must be undone by calling #unlockMedia() when
14156 * no more needed.
14157 */
14158HRESULT SessionMachine::i_lockMedia()
14159{
14160 AutoCaller autoCaller(this);
14161 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
14162
14163 AutoMultiWriteLock2 alock(this->lockHandle(),
14164 &mParent->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
14165
14166 /* bail out if trying to lock things with already set up locking */
14167 AssertReturn(mData->mSession.mLockedMedia.IsEmpty(), E_FAIL);
14168
14169 MultiResult mrc(S_OK);
14170
14171 /* Collect locking information for all medium objects attached to the VM. */
14172 for (MediaData::AttachmentList::const_iterator it = mMediaData->mAttachments.begin();
14173 it != mMediaData->mAttachments.end();
14174 ++it)
14175 {
14176 MediumAttachment* pAtt = *it;
14177 DeviceType_T devType = pAtt->i_getType();
14178 Medium *pMedium = pAtt->i_getMedium();
14179
14180 MediumLockList *pMediumLockList(new MediumLockList());
14181 // There can be attachments without a medium (floppy/dvd), and thus
14182 // it's impossible to create a medium lock list. It still makes sense
14183 // to have the empty medium lock list in the map in case a medium is
14184 // attached later.
14185 if (pMedium != NULL)
14186 {
14187 MediumType_T mediumType = pMedium->i_getType();
14188 bool fIsReadOnlyLock = mediumType == MediumType_Readonly
14189 || mediumType == MediumType_Shareable;
14190 bool fIsVitalImage = (devType == DeviceType_HardDisk);
14191
14192 alock.release();
14193 mrc = pMedium->i_createMediumLockList(fIsVitalImage /* fFailIfInaccessible */,
14194 !fIsReadOnlyLock /* fMediumLockWrite */,
14195 false /* fMediumLockWriteAll */,
14196 NULL,
14197 *pMediumLockList);
14198 alock.acquire();
14199 if (FAILED(mrc))
14200 {
14201 delete pMediumLockList;
14202 mData->mSession.mLockedMedia.Clear();
14203 break;
14204 }
14205 }
14206
14207 HRESULT rc = mData->mSession.mLockedMedia.Insert(pAtt, pMediumLockList);
14208 if (FAILED(rc))
14209 {
14210 mData->mSession.mLockedMedia.Clear();
14211 mrc = setError(rc,
14212 tr("Collecting locking information for all attached media failed"));
14213 break;
14214 }
14215 }
14216
14217 if (SUCCEEDED(mrc))
14218 {
14219 /* Now lock all media. If this fails, nothing is locked. */
14220 alock.release();
14221 HRESULT rc = mData->mSession.mLockedMedia.Lock();
14222 alock.acquire();
14223 if (FAILED(rc))
14224 {
14225 mrc = setError(rc,
14226 tr("Locking of attached media failed. A possible reason is that one of the media is attached to a running VM"));
14227 }
14228 }
14229
14230 return mrc;
14231}
14232
14233/**
14234 * Undoes the locks made by by #lockMedia().
14235 */
14236HRESULT SessionMachine::i_unlockMedia()
14237{
14238 AutoCaller autoCaller(this);
14239 AssertComRCReturn(autoCaller.rc(),autoCaller.rc());
14240
14241 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
14242
14243 /* we may be holding important error info on the current thread;
14244 * preserve it */
14245 ErrorInfoKeeper eik;
14246
14247 HRESULT rc = mData->mSession.mLockedMedia.Clear();
14248 AssertComRC(rc);
14249 return rc;
14250}
14251
14252/**
14253 * Helper to change the machine state (reimplementation).
14254 *
14255 * @note Locks this object for writing.
14256 * @note This method must not call i_saveSettings or SaveSettings, otherwise
14257 * it can cause crashes in random places due to unexpectedly committing
14258 * the current settings. The caller is responsible for that. The call
14259 * to saveStateSettings is fine, because this method does not commit.
14260 */
14261HRESULT SessionMachine::i_setMachineState(MachineState_T aMachineState)
14262{
14263 LogFlowThisFuncEnter();
14264 LogFlowThisFunc(("aMachineState=%s\n", Global::stringifyMachineState(aMachineState) ));
14265
14266 AutoCaller autoCaller(this);
14267 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
14268
14269 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
14270
14271 MachineState_T oldMachineState = mData->mMachineState;
14272
14273 AssertMsgReturn(oldMachineState != aMachineState,
14274 ("oldMachineState=%s, aMachineState=%s\n",
14275 Global::stringifyMachineState(oldMachineState), Global::stringifyMachineState(aMachineState)),
14276 E_FAIL);
14277
14278 HRESULT rc = S_OK;
14279
14280 int stsFlags = 0;
14281 bool deleteSavedState = false;
14282
14283 /* detect some state transitions */
14284
14285 if ( ( oldMachineState == MachineState_Saved
14286 && aMachineState == MachineState_Restoring)
14287 || ( ( oldMachineState == MachineState_PoweredOff
14288 || oldMachineState == MachineState_Teleported
14289 || oldMachineState == MachineState_Aborted
14290 )
14291 && ( aMachineState == MachineState_TeleportingIn
14292 || aMachineState == MachineState_Starting
14293 )
14294 )
14295 )
14296 {
14297 /* The EMT thread is about to start */
14298
14299 /* Nothing to do here for now... */
14300
14301 /// @todo NEWMEDIA don't let mDVDDrive and other children
14302 /// change anything when in the Starting/Restoring state
14303 }
14304 else if ( ( oldMachineState == MachineState_Running
14305 || oldMachineState == MachineState_Paused
14306 || oldMachineState == MachineState_Teleporting
14307 || oldMachineState == MachineState_OnlineSnapshotting
14308 || oldMachineState == MachineState_LiveSnapshotting
14309 || oldMachineState == MachineState_Stuck
14310 || oldMachineState == MachineState_Starting
14311 || oldMachineState == MachineState_Stopping
14312 || oldMachineState == MachineState_Saving
14313 || oldMachineState == MachineState_Restoring
14314 || oldMachineState == MachineState_TeleportingPausedVM
14315 || oldMachineState == MachineState_TeleportingIn
14316 )
14317 && ( aMachineState == MachineState_PoweredOff
14318 || aMachineState == MachineState_Saved
14319 || aMachineState == MachineState_Teleported
14320 || aMachineState == MachineState_Aborted
14321 )
14322 )
14323 {
14324 /* The EMT thread has just stopped, unlock attached media. Note that as
14325 * opposed to locking that is done from Console, we do unlocking here
14326 * because the VM process may have aborted before having a chance to
14327 * properly unlock all media it locked. */
14328
14329 unlockMedia();
14330 }
14331
14332 if (oldMachineState == MachineState_Restoring)
14333 {
14334 if (aMachineState != MachineState_Saved)
14335 {
14336 /*
14337 * delete the saved state file once the machine has finished
14338 * restoring from it (note that Console sets the state from
14339 * Restoring to Saved if the VM couldn't restore successfully,
14340 * to give the user an ability to fix an error and retry --
14341 * we keep the saved state file in this case)
14342 */
14343 deleteSavedState = true;
14344 }
14345 }
14346 else if ( oldMachineState == MachineState_Saved
14347 && ( aMachineState == MachineState_PoweredOff
14348 || aMachineState == MachineState_Aborted
14349 || aMachineState == MachineState_Teleported
14350 )
14351 )
14352 {
14353 /*
14354 * delete the saved state after SessionMachine::ForgetSavedState() is called
14355 * or if the VM process (owning a direct VM session) crashed while the
14356 * VM was Saved
14357 */
14358
14359 /// @todo (dmik)
14360 // Not sure that deleting the saved state file just because of the
14361 // client death before it attempted to restore the VM is a good
14362 // thing. But when it crashes we need to go to the Aborted state
14363 // which cannot have the saved state file associated... The only
14364 // way to fix this is to make the Aborted condition not a VM state
14365 // but a bool flag: i.e., when a crash occurs, set it to true and
14366 // change the state to PoweredOff or Saved depending on the
14367 // saved state presence.
14368
14369 deleteSavedState = true;
14370 mData->mCurrentStateModified = TRUE;
14371 stsFlags |= SaveSTS_CurStateModified;
14372 }
14373
14374 if ( aMachineState == MachineState_Starting
14375 || aMachineState == MachineState_Restoring
14376 || aMachineState == MachineState_TeleportingIn
14377 )
14378 {
14379 /* set the current state modified flag to indicate that the current
14380 * state is no more identical to the state in the
14381 * current snapshot */
14382 if (!mData->mCurrentSnapshot.isNull())
14383 {
14384 mData->mCurrentStateModified = TRUE;
14385 stsFlags |= SaveSTS_CurStateModified;
14386 }
14387 }
14388
14389 if (deleteSavedState)
14390 {
14391 if (mRemoveSavedState)
14392 {
14393 Assert(!mSSData->strStateFilePath.isEmpty());
14394
14395 // it is safe to delete the saved state file if ...
14396 if ( !mData->mFirstSnapshot // ... we have no snapshots or
14397 || !mData->mFirstSnapshot->i_sharesSavedStateFile(mSSData->strStateFilePath, NULL /* pSnapshotToIgnore */)
14398 // ... none of the snapshots share the saved state file
14399 )
14400 RTFileDelete(mSSData->strStateFilePath.c_str());
14401 }
14402
14403 mSSData->strStateFilePath.setNull();
14404 stsFlags |= SaveSTS_StateFilePath;
14405 }
14406
14407 /* redirect to the underlying peer machine */
14408 mPeer->i_setMachineState(aMachineState);
14409
14410 if ( oldMachineState != MachineState_RestoringSnapshot
14411 && ( aMachineState == MachineState_PoweredOff
14412 || aMachineState == MachineState_Teleported
14413 || aMachineState == MachineState_Aborted
14414 || aMachineState == MachineState_Saved))
14415 {
14416 /* the machine has stopped execution
14417 * (or the saved state file was adopted) */
14418 stsFlags |= SaveSTS_StateTimeStamp;
14419 }
14420
14421 if ( ( oldMachineState == MachineState_PoweredOff
14422 || oldMachineState == MachineState_Aborted
14423 || oldMachineState == MachineState_Teleported
14424 )
14425 && aMachineState == MachineState_Saved)
14426 {
14427 /* the saved state file was adopted */
14428 Assert(!mSSData->strStateFilePath.isEmpty());
14429 stsFlags |= SaveSTS_StateFilePath;
14430 }
14431
14432#ifdef VBOX_WITH_GUEST_PROPS
14433 if ( aMachineState == MachineState_PoweredOff
14434 || aMachineState == MachineState_Aborted
14435 || aMachineState == MachineState_Teleported)
14436 {
14437 /* Make sure any transient guest properties get removed from the
14438 * property store on shutdown. */
14439 BOOL fNeedsSaving = mData->mGuestPropertiesModified;
14440
14441 /* remove it from the settings representation */
14442 settings::GuestPropertiesList &llGuestProperties = mData->pMachineConfigFile->hardwareMachine.llGuestProperties;
14443 for (settings::GuestPropertiesList::iterator it = llGuestProperties.begin();
14444 it != llGuestProperties.end();
14445 /*nothing*/)
14446 {
14447 const settings::GuestProperty &prop = *it;
14448 if ( prop.strFlags.contains("TRANSRESET", Utf8Str::CaseInsensitive)
14449 || prop.strFlags.contains("TRANSIENT", Utf8Str::CaseInsensitive))
14450 {
14451 it = llGuestProperties.erase(it);
14452 fNeedsSaving = true;
14453 }
14454 else
14455 {
14456 ++it;
14457 }
14458 }
14459
14460 /* Additionally remove it from the HWData representation. Required to
14461 * keep everything in sync, as this is what the API keeps using. */
14462 HWData::GuestPropertyMap &llHWGuestProperties = mHWData->mGuestProperties;
14463 for (HWData::GuestPropertyMap::iterator it = llHWGuestProperties.begin();
14464 it != llHWGuestProperties.end();
14465 /*nothing*/)
14466 {
14467 uint32_t fFlags = it->second.mFlags;
14468 if ( fFlags & guestProp::TRANSIENT
14469 || fFlags & guestProp::TRANSRESET)
14470 {
14471 /* iterator where we need to continue after the erase call
14472 * (C++03 is a fact still, and it doesn't return the iterator
14473 * which would allow continuing) */
14474 HWData::GuestPropertyMap::iterator it2 = it;
14475 ++it2;
14476 llHWGuestProperties.erase(it);
14477 it = it2;
14478 fNeedsSaving = true;
14479 }
14480 else
14481 {
14482 ++it;
14483 }
14484 }
14485
14486 if (fNeedsSaving)
14487 {
14488 mData->mCurrentStateModified = TRUE;
14489 stsFlags |= SaveSTS_CurStateModified;
14490 }
14491 }
14492#endif /* VBOX_WITH_GUEST_PROPS */
14493
14494 rc = i_saveStateSettings(stsFlags);
14495
14496 if ( ( oldMachineState != MachineState_PoweredOff
14497 && oldMachineState != MachineState_Aborted
14498 && oldMachineState != MachineState_Teleported
14499 )
14500 && ( aMachineState == MachineState_PoweredOff
14501 || aMachineState == MachineState_Aborted
14502 || aMachineState == MachineState_Teleported
14503 )
14504 )
14505 {
14506 /* we've been shut down for any reason */
14507 /* no special action so far */
14508 }
14509
14510 LogFlowThisFunc(("rc=%Rhrc [%s]\n", rc, Global::stringifyMachineState(mData->mMachineState) ));
14511 LogFlowThisFuncLeave();
14512 return rc;
14513}
14514
14515/**
14516 * Sends the current machine state value to the VM process.
14517 *
14518 * @note Locks this object for reading, then calls a client process.
14519 */
14520HRESULT SessionMachine::i_updateMachineStateOnClient()
14521{
14522 AutoCaller autoCaller(this);
14523 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
14524
14525 ComPtr<IInternalSessionControl> directControl;
14526 {
14527 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
14528 AssertReturn(!!mData, E_FAIL);
14529 if (mData->mSession.mLockType == LockType_VM)
14530 directControl = mData->mSession.mDirectControl;
14531
14532 /* directControl may be already set to NULL here in #OnSessionEnd()
14533 * called too early by the direct session process while there is still
14534 * some operation (like deleting the snapshot) in progress. The client
14535 * process in this case is waiting inside Session::close() for the
14536 * "end session" process object to complete, while #uninit() called by
14537 * #checkForDeath() on the Watcher thread is waiting for the pending
14538 * operation to complete. For now, we accept this inconsistent behavior
14539 * and simply do nothing here. */
14540
14541 if (mData->mSession.mState == SessionState_Unlocking)
14542 return S_OK;
14543 }
14544
14545 /* ignore notifications sent after #OnSessionEnd() is called */
14546 if (!directControl)
14547 return S_OK;
14548
14549 return directControl->UpdateMachineState(mData->mMachineState);
14550}
14551
14552
14553/**
14554 * Static Machine method that can get passed to RTThreadCreate to
14555 * have a thread started for a Task. See Machine::Task.
14556 */
14557/* static */ DECLCALLBACK(int) Machine::taskHandler(RTTHREAD /* thread */, void *pvUser)
14558{
14559 AssertReturn(pvUser, VERR_INVALID_POINTER);
14560
14561 Task *pTask = static_cast<Task *>(pvUser);
14562 pTask->handler();
14563 /** @todo r=klaus it would be safer to update the progress object here,
14564 * as it avoids possible races due to scoping issues/tricks in the handler */
14565 // it's our responsibility to delete the task
14566 delete pTask;
14567
14568 return 0;
14569}
14570
14571/*static*/
14572HRESULT Machine::i_setErrorStatic(HRESULT aResultCode, const char *pcszMsg, ...)
14573{
14574 va_list args;
14575 va_start(args, pcszMsg);
14576 HRESULT rc = setErrorInternal(aResultCode,
14577 getStaticClassIID(),
14578 getStaticComponentName(),
14579 Utf8Str(pcszMsg, args),
14580 false /* aWarning */,
14581 true /* aLogIt */);
14582 va_end(args);
14583 return rc;
14584}
14585
14586
14587HRESULT Machine::updateState(MachineState_T aState)
14588{
14589 NOREF(aState);
14590 ReturnComNotImplemented();
14591}
14592
14593HRESULT Machine::beginPowerUp(const ComPtr<IProgress> &aProgress)
14594{
14595 NOREF(aProgress);
14596 ReturnComNotImplemented();
14597}
14598
14599HRESULT Machine::endPowerUp(LONG aResult)
14600{
14601 NOREF(aResult);
14602 ReturnComNotImplemented();
14603}
14604
14605HRESULT Machine::beginPoweringDown(ComPtr<IProgress> &aProgress)
14606{
14607 NOREF(aProgress);
14608 ReturnComNotImplemented();
14609}
14610
14611HRESULT Machine::endPoweringDown(LONG aResult,
14612 const com::Utf8Str &aErrMsg)
14613{
14614 NOREF(aResult);
14615 NOREF(aErrMsg);
14616 ReturnComNotImplemented();
14617}
14618
14619HRESULT Machine::runUSBDeviceFilters(const ComPtr<IUSBDevice> &aDevice,
14620 BOOL *aMatched,
14621 ULONG *aMaskedInterfaces)
14622{
14623 NOREF(aDevice);
14624 NOREF(aMatched);
14625 NOREF(aMaskedInterfaces);
14626 ReturnComNotImplemented();
14627
14628}
14629
14630HRESULT Machine::captureUSBDevice(const com::Guid &aId, const com::Utf8Str &aCaptureFilename)
14631{
14632 NOREF(aId); NOREF(aCaptureFilename);
14633 ReturnComNotImplemented();
14634}
14635
14636HRESULT Machine::detachUSBDevice(const com::Guid &aId,
14637 BOOL aDone)
14638{
14639 NOREF(aId);
14640 NOREF(aDone);
14641 ReturnComNotImplemented();
14642}
14643
14644HRESULT Machine::autoCaptureUSBDevices()
14645{
14646 ReturnComNotImplemented();
14647}
14648
14649HRESULT Machine::detachAllUSBDevices(BOOL aDone)
14650{
14651 NOREF(aDone);
14652 ReturnComNotImplemented();
14653}
14654
14655HRESULT Machine::onSessionEnd(const ComPtr<ISession> &aSession,
14656 ComPtr<IProgress> &aProgress)
14657{
14658 NOREF(aSession);
14659 NOREF(aProgress);
14660 ReturnComNotImplemented();
14661}
14662
14663HRESULT Machine::finishOnlineMergeMedium()
14664{
14665 ReturnComNotImplemented();
14666}
14667
14668HRESULT Machine::pullGuestProperties(std::vector<com::Utf8Str> &aNames,
14669 std::vector<com::Utf8Str> &aValues,
14670 std::vector<LONG64> &aTimestamps,
14671 std::vector<com::Utf8Str> &aFlags)
14672{
14673 NOREF(aNames);
14674 NOREF(aValues);
14675 NOREF(aTimestamps);
14676 NOREF(aFlags);
14677 ReturnComNotImplemented();
14678}
14679
14680HRESULT Machine::pushGuestProperty(const com::Utf8Str &aName,
14681 const com::Utf8Str &aValue,
14682 LONG64 aTimestamp,
14683 const com::Utf8Str &aFlags)
14684{
14685 NOREF(aName);
14686 NOREF(aValue);
14687 NOREF(aTimestamp);
14688 NOREF(aFlags);
14689 ReturnComNotImplemented();
14690}
14691
14692HRESULT Machine::lockMedia()
14693{
14694 ReturnComNotImplemented();
14695}
14696
14697HRESULT Machine::unlockMedia()
14698{
14699 ReturnComNotImplemented();
14700}
14701
14702HRESULT Machine::ejectMedium(const ComPtr<IMediumAttachment> &aAttachment,
14703 ComPtr<IMediumAttachment> &aNewAttachment)
14704{
14705 NOREF(aAttachment);
14706 NOREF(aNewAttachment);
14707 ReturnComNotImplemented();
14708}
14709
14710HRESULT Machine::reportVmStatistics(ULONG aValidStats,
14711 ULONG aCpuUser,
14712 ULONG aCpuKernel,
14713 ULONG aCpuIdle,
14714 ULONG aMemTotal,
14715 ULONG aMemFree,
14716 ULONG aMemBalloon,
14717 ULONG aMemShared,
14718 ULONG aMemCache,
14719 ULONG aPagedTotal,
14720 ULONG aMemAllocTotal,
14721 ULONG aMemFreeTotal,
14722 ULONG aMemBalloonTotal,
14723 ULONG aMemSharedTotal,
14724 ULONG aVmNetRx,
14725 ULONG aVmNetTx)
14726{
14727 NOREF(aValidStats);
14728 NOREF(aCpuUser);
14729 NOREF(aCpuKernel);
14730 NOREF(aCpuIdle);
14731 NOREF(aMemTotal);
14732 NOREF(aMemFree);
14733 NOREF(aMemBalloon);
14734 NOREF(aMemShared);
14735 NOREF(aMemCache);
14736 NOREF(aPagedTotal);
14737 NOREF(aMemAllocTotal);
14738 NOREF(aMemFreeTotal);
14739 NOREF(aMemBalloonTotal);
14740 NOREF(aMemSharedTotal);
14741 NOREF(aVmNetRx);
14742 NOREF(aVmNetTx);
14743 ReturnComNotImplemented();
14744}
14745
14746HRESULT Machine::applyDefaults(const com::Utf8Str &aFlags)
14747{
14748 NOREF(aFlags);
14749 ReturnComNotImplemented();
14750}
14751
14752/* This isn't handled entirely by the wrapper generator yet. */
14753#ifdef VBOX_WITH_XPCOM
14754NS_DECL_CLASSINFO(SessionMachine)
14755NS_IMPL_THREADSAFE_ISUPPORTS2_CI(SessionMachine, IMachine, IInternalMachineControl)
14756
14757NS_DECL_CLASSINFO(SnapshotMachine)
14758NS_IMPL_THREADSAFE_ISUPPORTS1_CI(SnapshotMachine, IMachine)
14759#endif
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