VirtualBox

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

Last change on this file since 66875 was 66875, checked in by vboxsync, 8 years ago

Main: bugref:8855: when renaming settings, don't change the config directory inappropriately

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 518.3 KB
Line 
1/* $Id: MachineImpl.cpp 66875 2017-05-11 17:06:28Z vboxsync $ */
2/** @file
3 * Implementation of IMachine in VBoxSVC.
4 */
5
6/*
7 * Copyright (C) 2004-2017 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
70#include <VBox/com/array.h>
71#include <VBox/com/list.h>
72
73#include <VBox/err.h>
74#include <VBox/param.h>
75#include <VBox/settings.h>
76#include <VBox/vmm/ssm.h>
77
78#ifdef VBOX_WITH_GUEST_PROPS
79# include <VBox/HostServices/GuestPropertySvc.h>
80# include <VBox/com/array.h>
81#endif
82
83#include "VBox/com/MultiResult.h"
84
85#include <algorithm>
86
87#ifdef VBOX_WITH_DTRACE_R3_MAIN
88# include "dtrace/VBoxAPI.h"
89#endif
90
91#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
92# define HOSTSUFF_EXE ".exe"
93#else /* !RT_OS_WINDOWS */
94# define HOSTSUFF_EXE ""
95#endif /* !RT_OS_WINDOWS */
96
97// defines / prototypes
98/////////////////////////////////////////////////////////////////////////////
99
100/////////////////////////////////////////////////////////////////////////////
101// Machine::Data structure
102/////////////////////////////////////////////////////////////////////////////
103
104Machine::Data::Data()
105{
106 mRegistered = FALSE;
107 pMachineConfigFile = NULL;
108 /* Contains hints on what has changed when the user is using the VM (config
109 * changes, running the VM, ...). This is used to decide if a config needs
110 * to be written to disk. */
111 flModifications = 0;
112 /* VM modification usually also trigger setting the current state to
113 * "Modified". Although this is not always the case. An e.g. is the VM
114 * initialization phase or when snapshot related data is changed. The
115 * actually behavior is controlled by the following flag. */
116 m_fAllowStateModification = false;
117 mAccessible = FALSE;
118 /* mUuid is initialized in Machine::init() */
119
120 mMachineState = MachineState_PoweredOff;
121 RTTimeNow(&mLastStateChange);
122
123 mMachineStateDeps = 0;
124 mMachineStateDepsSem = NIL_RTSEMEVENTMULTI;
125 mMachineStateChangePending = 0;
126
127 mCurrentStateModified = TRUE;
128 mGuestPropertiesModified = FALSE;
129
130 mSession.mPID = NIL_RTPROCESS;
131 mSession.mLockType = LockType_Null;
132 mSession.mState = SessionState_Unlocked;
133}
134
135Machine::Data::~Data()
136{
137 if (mMachineStateDepsSem != NIL_RTSEMEVENTMULTI)
138 {
139 RTSemEventMultiDestroy(mMachineStateDepsSem);
140 mMachineStateDepsSem = NIL_RTSEMEVENTMULTI;
141 }
142 if (pMachineConfigFile)
143 {
144 delete pMachineConfigFile;
145 pMachineConfigFile = NULL;
146 }
147}
148
149/////////////////////////////////////////////////////////////////////////////
150// Machine::HWData structure
151/////////////////////////////////////////////////////////////////////////////
152
153Machine::HWData::HWData()
154{
155 /* default values for a newly created machine */
156 mHWVersion = "2"; /** @todo get the default from the schema if that is possible. */
157 mMemorySize = 128;
158 mCPUCount = 1;
159 mCPUHotPlugEnabled = false;
160 mMemoryBalloonSize = 0;
161 mPageFusionEnabled = false;
162 mGraphicsControllerType = GraphicsControllerType_VBoxVGA;
163 mVRAMSize = 8;
164 mAccelerate3DEnabled = false;
165 mAccelerate2DVideoEnabled = false;
166 mMonitorCount = 1;
167 mVideoCaptureWidth = 1024;
168 mVideoCaptureHeight = 768;
169 mVideoCaptureRate = 512;
170 mVideoCaptureFPS = 25;
171 mVideoCaptureMaxTime = 0;
172 mVideoCaptureMaxFileSize = 0;
173 mVideoCaptureEnabled = false;
174 for (unsigned i = 0; i < RT_ELEMENTS(maVideoCaptureScreens); ++i)
175 maVideoCaptureScreens[i] = true;
176
177 mHWVirtExEnabled = true;
178 mHWVirtExNestedPagingEnabled = true;
179#if HC_ARCH_BITS == 64 && !defined(RT_OS_LINUX)
180 mHWVirtExLargePagesEnabled = true;
181#else
182 /* Not supported on 32 bits hosts. */
183 mHWVirtExLargePagesEnabled = false;
184#endif
185 mHWVirtExVPIDEnabled = true;
186 mHWVirtExUXEnabled = true;
187 mHWVirtExForceEnabled = false;
188#if HC_ARCH_BITS == 64 || defined(RT_OS_WINDOWS) || defined(RT_OS_DARWIN)
189 mPAEEnabled = true;
190#else
191 mPAEEnabled = false;
192#endif
193 mLongMode = HC_ARCH_BITS == 64 ? settings::Hardware::LongMode_Enabled : settings::Hardware::LongMode_Disabled;
194 mTripleFaultReset = false;
195 mAPIC = true;
196 mX2APIC = false;
197 mHPETEnabled = false;
198 mCpuExecutionCap = 100; /* Maximum CPU execution cap by default. */
199 mCpuIdPortabilityLevel = 0;
200 mCpuProfile = "host";
201
202 /* default boot order: floppy - DVD - HDD */
203 mBootOrder[0] = DeviceType_Floppy;
204 mBootOrder[1] = DeviceType_DVD;
205 mBootOrder[2] = DeviceType_HardDisk;
206 for (size_t i = 3; i < RT_ELEMENTS(mBootOrder); ++i)
207 mBootOrder[i] = DeviceType_Null;
208
209 mClipboardMode = ClipboardMode_Disabled;
210 mDnDMode = DnDMode_Disabled;
211
212 mFirmwareType = FirmwareType_BIOS;
213 mKeyboardHIDType = KeyboardHIDType_PS2Keyboard;
214 mPointingHIDType = PointingHIDType_PS2Mouse;
215 mChipsetType = ChipsetType_PIIX3;
216 mParavirtProvider = ParavirtProvider_Default;
217 mEmulatedUSBCardReaderEnabled = FALSE;
218
219 for (size_t i = 0; i < RT_ELEMENTS(mCPUAttached); ++i)
220 mCPUAttached[i] = false;
221
222 mIOCacheEnabled = true;
223 mIOCacheSize = 5; /* 5MB */
224}
225
226Machine::HWData::~HWData()
227{
228}
229
230/////////////////////////////////////////////////////////////////////////////
231// Machine class
232/////////////////////////////////////////////////////////////////////////////
233
234// constructor / destructor
235/////////////////////////////////////////////////////////////////////////////
236
237Machine::Machine() :
238#ifdef VBOX_WITH_RESOURCE_USAGE_API
239 mCollectorGuest(NULL),
240#endif
241 mPeer(NULL),
242 mParent(NULL),
243 mSerialPorts(),
244 mParallelPorts(),
245 uRegistryNeedsSaving(0)
246{}
247
248Machine::~Machine()
249{}
250
251HRESULT Machine::FinalConstruct()
252{
253 LogFlowThisFunc(("\n"));
254 return BaseFinalConstruct();
255}
256
257void Machine::FinalRelease()
258{
259 LogFlowThisFunc(("\n"));
260 uninit();
261 BaseFinalRelease();
262}
263
264/**
265 * Initializes a new machine instance; this init() variant creates a new, empty machine.
266 * This gets called from VirtualBox::CreateMachine().
267 *
268 * @param aParent Associated parent object
269 * @param strConfigFile Local file system path to the VM settings file (can
270 * be relative to the VirtualBox config directory).
271 * @param strName name for the machine
272 * @param llGroups list of groups for the machine
273 * @param aOsType OS Type of this machine or NULL.
274 * @param aId UUID for the new machine.
275 * @param fForceOverwrite Whether to overwrite an existing machine settings file.
276 * @param fDirectoryIncludesUUID Whether the use a special VM directory naming
277 * scheme (includes the UUID).
278 *
279 * @return Success indicator. if not S_OK, the machine object is invalid
280 */
281HRESULT Machine::init(VirtualBox *aParent,
282 const Utf8Str &strConfigFile,
283 const Utf8Str &strName,
284 const StringsList &llGroups,
285 GuestOSType *aOsType,
286 const Guid &aId,
287 bool fForceOverwrite,
288 bool fDirectoryIncludesUUID)
289{
290 LogFlowThisFuncEnter();
291 LogFlowThisFunc(("(Init_New) aConfigFile='%s'\n", strConfigFile.c_str()));
292
293 /* Enclose the state transition NotReady->InInit->Ready */
294 AutoInitSpan autoInitSpan(this);
295 AssertReturn(autoInitSpan.isOk(), E_FAIL);
296
297 HRESULT rc = initImpl(aParent, strConfigFile);
298 if (FAILED(rc)) return rc;
299
300 rc = i_tryCreateMachineConfigFile(fForceOverwrite);
301 if (FAILED(rc)) return rc;
302
303 if (SUCCEEDED(rc))
304 {
305 // create an empty machine config
306 mData->pMachineConfigFile = new settings::MachineConfigFile(NULL);
307
308 rc = initDataAndChildObjects();
309 }
310
311 if (SUCCEEDED(rc))
312 {
313 // set to true now to cause uninit() to call uninitDataAndChildObjects() on failure
314 mData->mAccessible = TRUE;
315
316 unconst(mData->mUuid) = aId;
317
318 mUserData->s.strName = strName;
319
320 mUserData->s.llGroups = llGroups;
321
322 mUserData->s.fDirectoryIncludesUUID = fDirectoryIncludesUUID;
323 // the "name sync" flag determines whether the machine directory gets renamed along
324 // with the machine file; say so if the settings file name is the same as the
325 // settings file parent directory (machine directory)
326 mUserData->s.fNameSync = i_isInOwnDir();
327
328 // initialize the default snapshots folder
329 rc = COMSETTER(SnapshotFolder)(NULL);
330 AssertComRC(rc);
331
332 if (aOsType)
333 {
334 /* Store OS type */
335 mUserData->s.strOsType = aOsType->i_id();
336
337 /* Apply BIOS defaults */
338 mBIOSSettings->i_applyDefaults(aOsType);
339
340 /* Apply network adapters defaults */
341 for (ULONG slot = 0; slot < mNetworkAdapters.size(); ++slot)
342 mNetworkAdapters[slot]->i_applyDefaults(aOsType);
343
344 /* Apply serial port defaults */
345 for (ULONG slot = 0; slot < RT_ELEMENTS(mSerialPorts); ++slot)
346 mSerialPorts[slot]->i_applyDefaults(aOsType);
347
348 /* Let the OS type select 64-bit ness. */
349 mHWData->mLongMode = aOsType->i_is64Bit()
350 ? settings::Hardware::LongMode_Enabled : settings::Hardware::LongMode_Disabled;
351
352 /* Let the OS type enable the X2APIC */
353 mHWData->mX2APIC = aOsType->i_recommendedX2APIC();
354 }
355
356 /* Apply parallel port defaults */
357 for (ULONG slot = 0; slot < RT_ELEMENTS(mParallelPorts); ++slot)
358 mParallelPorts[slot]->i_applyDefaults();
359
360 /* At this point the changing of the current state modification
361 * flag is allowed. */
362 i_allowStateModification();
363
364 /* commit all changes made during the initialization */
365 i_commit();
366 }
367
368 /* Confirm a successful initialization when it's the case */
369 if (SUCCEEDED(rc))
370 {
371 if (mData->mAccessible)
372 autoInitSpan.setSucceeded();
373 else
374 autoInitSpan.setLimited();
375 }
376
377 LogFlowThisFunc(("mName='%s', mRegistered=%RTbool, mAccessible=%RTbool, rc=%08X\n",
378 !!mUserData ? mUserData->s.strName.c_str() : "NULL",
379 mData->mRegistered,
380 mData->mAccessible,
381 rc));
382
383 LogFlowThisFuncLeave();
384
385 return rc;
386}
387
388/**
389 * Initializes a new instance with data from machine XML (formerly Init_Registered).
390 * Gets called in two modes:
391 *
392 * -- from VirtualBox::initMachines() during VirtualBox startup; in that case, the
393 * UUID is specified and we mark the machine as "registered";
394 *
395 * -- from the public VirtualBox::OpenMachine() API, in which case the UUID is NULL
396 * and the machine remains unregistered until RegisterMachine() is called.
397 *
398 * @param aParent Associated parent object
399 * @param strConfigFile Local file system path to the VM settings file (can
400 * be relative to the VirtualBox config directory).
401 * @param aId UUID of the machine or NULL (see above).
402 *
403 * @return Success indicator. if not S_OK, the machine object is invalid
404 */
405HRESULT Machine::initFromSettings(VirtualBox *aParent,
406 const Utf8Str &strConfigFile,
407 const Guid *aId)
408{
409 LogFlowThisFuncEnter();
410 LogFlowThisFunc(("(Init_Registered) aConfigFile='%s\n", strConfigFile.c_str()));
411
412 /* Enclose the state transition NotReady->InInit->Ready */
413 AutoInitSpan autoInitSpan(this);
414 AssertReturn(autoInitSpan.isOk(), E_FAIL);
415
416 HRESULT rc = initImpl(aParent, strConfigFile);
417 if (FAILED(rc)) return rc;
418
419 if (aId)
420 {
421 // loading a registered VM:
422 unconst(mData->mUuid) = *aId;
423 mData->mRegistered = TRUE;
424 // now load the settings from XML:
425 rc = i_registeredInit();
426 // this calls initDataAndChildObjects() and loadSettings()
427 }
428 else
429 {
430 // opening an unregistered VM (VirtualBox::OpenMachine()):
431 rc = initDataAndChildObjects();
432
433 if (SUCCEEDED(rc))
434 {
435 // set to true now to cause uninit() to call uninitDataAndChildObjects() on failure
436 mData->mAccessible = TRUE;
437
438 try
439 {
440 // load and parse machine XML; this will throw on XML or logic errors
441 mData->pMachineConfigFile = new settings::MachineConfigFile(&mData->m_strConfigFileFull);
442
443 // reject VM UUID duplicates, they can happen if someone
444 // tries to register an already known VM config again
445 if (aParent->i_findMachine(mData->pMachineConfigFile->uuid,
446 true /* fPermitInaccessible */,
447 false /* aDoSetError */,
448 NULL) != VBOX_E_OBJECT_NOT_FOUND)
449 {
450 throw setError(E_FAIL,
451 tr("Trying to open a VM config '%s' which has the same UUID as an existing virtual machine"),
452 mData->m_strConfigFile.c_str());
453 }
454
455 // use UUID from machine config
456 unconst(mData->mUuid) = mData->pMachineConfigFile->uuid;
457
458 rc = i_loadMachineDataFromSettings(*mData->pMachineConfigFile,
459 NULL /* puuidRegistry */);
460 if (FAILED(rc)) throw rc;
461
462 /* At this point the changing of the current state modification
463 * flag is allowed. */
464 i_allowStateModification();
465
466 i_commit();
467 }
468 catch (HRESULT err)
469 {
470 /* we assume that error info is set by the thrower */
471 rc = err;
472 }
473 catch (...)
474 {
475 rc = VirtualBoxBase::handleUnexpectedExceptions(this, RT_SRC_POS);
476 }
477 }
478 }
479
480 /* Confirm a successful initialization when it's the case */
481 if (SUCCEEDED(rc))
482 {
483 if (mData->mAccessible)
484 autoInitSpan.setSucceeded();
485 else
486 {
487 autoInitSpan.setLimited();
488
489 // uninit media from this machine's media registry, or else
490 // reloading the settings will fail
491 mParent->i_unregisterMachineMedia(i_getId());
492 }
493 }
494
495 LogFlowThisFunc(("mName='%s', mRegistered=%RTbool, mAccessible=%RTbool "
496 "rc=%08X\n",
497 !!mUserData ? mUserData->s.strName.c_str() : "NULL",
498 mData->mRegistered, mData->mAccessible, rc));
499
500 LogFlowThisFuncLeave();
501
502 return rc;
503}
504
505/**
506 * Initializes a new instance from a machine config that is already in memory
507 * (import OVF case). Since we are importing, the UUID in the machine
508 * config is ignored and we always generate a fresh one.
509 *
510 * @param aParent Associated parent object.
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 The VirtualBox object.
604 * @param strConfigFile Settings file.
605 * @return
606 */
607HRESULT Machine::initImpl(VirtualBox *aParent,
608 const Utf8Str &strConfigFile)
609{
610 LogFlowThisFuncEnter();
611
612 AssertReturn(aParent, E_INVALIDARG);
613 AssertReturn(!strConfigFile.isEmpty(), E_INVALIDARG);
614
615 HRESULT rc = S_OK;
616
617 /* share the parent weakly */
618 unconst(mParent) = aParent;
619
620 /* allocate the essential machine data structure (the rest will be
621 * allocated later by initDataAndChildObjects() */
622 mData.allocate();
623
624 /* memorize the config file name (as provided) */
625 mData->m_strConfigFile = strConfigFile;
626
627 /* get the full file name */
628 int vrc1 = mParent->i_calculateFullPath(strConfigFile, mData->m_strConfigFileFull);
629 if (RT_FAILURE(vrc1))
630 return setError(VBOX_E_FILE_ERROR,
631 tr("Invalid machine settings file name '%s' (%Rrc)"),
632 strConfigFile.c_str(),
633 vrc1);
634
635 LogFlowThisFuncLeave();
636
637 return rc;
638}
639
640/**
641 * Tries to create a machine settings file in the path stored in the machine
642 * instance data. Used when a new machine is created to fail gracefully if
643 * the settings file could not be written (e.g. because machine dir is read-only).
644 * @return
645 */
646HRESULT Machine::i_tryCreateMachineConfigFile(bool fForceOverwrite)
647{
648 HRESULT rc = S_OK;
649
650 // when we create a new machine, we must be able to create the settings file
651 RTFILE f = NIL_RTFILE;
652 int vrc = RTFileOpen(&f, mData->m_strConfigFileFull.c_str(), RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
653 if ( RT_SUCCESS(vrc)
654 || vrc == VERR_SHARING_VIOLATION
655 )
656 {
657 if (RT_SUCCESS(vrc))
658 RTFileClose(f);
659 if (!fForceOverwrite)
660 rc = setError(VBOX_E_FILE_ERROR,
661 tr("Machine settings file '%s' already exists"),
662 mData->m_strConfigFileFull.c_str());
663 else
664 {
665 /* try to delete the config file, as otherwise the creation
666 * of a new settings file will fail. */
667 int vrc2 = RTFileDelete(mData->m_strConfigFileFull.c_str());
668 if (RT_FAILURE(vrc2))
669 rc = setError(VBOX_E_FILE_ERROR,
670 tr("Could not delete the existing settings file '%s' (%Rrc)"),
671 mData->m_strConfigFileFull.c_str(), vrc2);
672 }
673 }
674 else if ( vrc != VERR_FILE_NOT_FOUND
675 && vrc != VERR_PATH_NOT_FOUND
676 )
677 rc = setError(VBOX_E_FILE_ERROR,
678 tr("Invalid machine settings file name '%s' (%Rrc)"),
679 mData->m_strConfigFileFull.c_str(),
680 vrc);
681 return rc;
682}
683
684/**
685 * Initializes the registered machine by loading the settings file.
686 * This method is separated from #init() in order to make it possible to
687 * retry the operation after VirtualBox startup instead of refusing to
688 * startup the whole VirtualBox server in case if the settings file of some
689 * registered VM is invalid or inaccessible.
690 *
691 * @note Must be always called from this object's write lock
692 * (unless called from #init() that doesn't need any locking).
693 * @note Locks the mUSBController method for writing.
694 * @note Subclasses must not call this method.
695 */
696HRESULT Machine::i_registeredInit()
697{
698 AssertReturn(!i_isSessionMachine(), E_FAIL);
699 AssertReturn(!i_isSnapshotMachine(), E_FAIL);
700 AssertReturn(mData->mUuid.isValid(), E_FAIL);
701 AssertReturn(!mData->mAccessible, E_FAIL);
702
703 HRESULT rc = initDataAndChildObjects();
704
705 if (SUCCEEDED(rc))
706 {
707 /* Temporarily reset the registered flag in order to let setters
708 * potentially called from loadSettings() succeed (isMutable() used in
709 * all setters will return FALSE for a Machine instance if mRegistered
710 * is TRUE). */
711 mData->mRegistered = FALSE;
712
713 try
714 {
715 // load and parse machine XML; this will throw on XML or logic errors
716 mData->pMachineConfigFile = new settings::MachineConfigFile(&mData->m_strConfigFileFull);
717
718 if (mData->mUuid != mData->pMachineConfigFile->uuid)
719 throw setError(E_FAIL,
720 tr("Machine UUID {%RTuuid} in '%s' doesn't match its UUID {%s} in the registry file '%s'"),
721 mData->pMachineConfigFile->uuid.raw(),
722 mData->m_strConfigFileFull.c_str(),
723 mData->mUuid.toString().c_str(),
724 mParent->i_settingsFilePath().c_str());
725
726 rc = i_loadMachineDataFromSettings(*mData->pMachineConfigFile,
727 NULL /* const Guid *puuidRegistry */);
728 if (FAILED(rc)) throw rc;
729 }
730 catch (HRESULT err)
731 {
732 /* we assume that error info is set by the thrower */
733 rc = err;
734 }
735 catch (...)
736 {
737 rc = VirtualBoxBase::handleUnexpectedExceptions(this, RT_SRC_POS);
738 }
739
740 /* Restore the registered flag (even on failure) */
741 mData->mRegistered = TRUE;
742 }
743
744 if (SUCCEEDED(rc))
745 {
746 /* Set mAccessible to TRUE only if we successfully locked and loaded
747 * the settings file */
748 mData->mAccessible = TRUE;
749
750 /* commit all changes made during loading the settings file */
751 i_commit(); /// @todo r=dj why do we need a commit during init?!? this is very expensive
752 /// @todo r=klaus for some reason the settings loading logic backs up
753 // the settings, and therefore a commit is needed. Should probably be changed.
754 }
755 else
756 {
757 /* If the machine is registered, then, instead of returning a
758 * failure, we mark it as inaccessible and set the result to
759 * success to give it a try later */
760
761 /* fetch the current error info */
762 mData->mAccessError = com::ErrorInfo();
763 Log1Warning(("Machine {%RTuuid} is inaccessible! [%ls]\n", mData->mUuid.raw(), mData->mAccessError.getText().raw()));
764
765 /* rollback all changes */
766 i_rollback(false /* aNotify */);
767
768 // uninit media from this machine's media registry, or else
769 // reloading the settings will fail
770 mParent->i_unregisterMachineMedia(i_getId());
771
772 /* uninitialize the common part to make sure all data is reset to
773 * default (null) values */
774 uninitDataAndChildObjects();
775
776 rc = S_OK;
777 }
778
779 return rc;
780}
781
782/**
783 * Uninitializes the instance.
784 * Called either from FinalRelease() or by the parent when it gets destroyed.
785 *
786 * @note The caller of this method must make sure that this object
787 * a) doesn't have active callers on the current thread and b) is not locked
788 * by the current thread; otherwise uninit() will hang either a) due to
789 * AutoUninitSpan waiting for a number of calls to drop to zero or b) due to
790 * a dead-lock caused by this thread waiting for all callers on the other
791 * threads are done but preventing them from doing so by holding a lock.
792 */
793void Machine::uninit()
794{
795 LogFlowThisFuncEnter();
796
797 Assert(!isWriteLockOnCurrentThread());
798
799 Assert(!uRegistryNeedsSaving);
800 if (uRegistryNeedsSaving)
801 {
802 AutoCaller autoCaller(this);
803 if (SUCCEEDED(autoCaller.rc()))
804 {
805 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
806 i_saveSettings(NULL, Machine::SaveS_Force);
807 }
808 }
809
810 /* Enclose the state transition Ready->InUninit->NotReady */
811 AutoUninitSpan autoUninitSpan(this);
812 if (autoUninitSpan.uninitDone())
813 return;
814
815 Assert(!i_isSnapshotMachine());
816 Assert(!i_isSessionMachine());
817 Assert(!!mData);
818
819 LogFlowThisFunc(("initFailed()=%d\n", autoUninitSpan.initFailed()));
820 LogFlowThisFunc(("mRegistered=%d\n", mData->mRegistered));
821
822 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
823
824 if (!mData->mSession.mMachine.isNull())
825 {
826 /* Theoretically, this can only happen if the VirtualBox server has been
827 * terminated while there were clients running that owned open direct
828 * sessions. Since in this case we are definitely called by
829 * VirtualBox::uninit(), we may be sure that SessionMachine::uninit()
830 * won't happen on the client watcher thread (because it has a
831 * VirtualBox caller for the duration of the
832 * SessionMachine::i_checkForDeath() call, so that VirtualBox::uninit()
833 * cannot happen until the VirtualBox caller is released). This is
834 * important, because SessionMachine::uninit() cannot correctly operate
835 * after we return from this method (it expects the Machine instance is
836 * still valid). We'll call it ourselves below.
837 */
838 Log1WarningThisFunc(("Session machine is not NULL (%p), the direct session is still open!\n",
839 (SessionMachine*)mData->mSession.mMachine));
840
841 if (Global::IsOnlineOrTransient(mData->mMachineState))
842 {
843 Log1WarningThisFunc(("Setting state to Aborted!\n"));
844 /* set machine state using SessionMachine reimplementation */
845 static_cast<Machine*>(mData->mSession.mMachine)->i_setMachineState(MachineState_Aborted);
846 }
847
848 /*
849 * Uninitialize SessionMachine using public uninit() to indicate
850 * an unexpected uninitialization.
851 */
852 mData->mSession.mMachine->uninit();
853 /* SessionMachine::uninit() must set mSession.mMachine to null */
854 Assert(mData->mSession.mMachine.isNull());
855 }
856
857 // uninit media from this machine's media registry, if they're still there
858 Guid uuidMachine(i_getId());
859
860 /* the lock is no more necessary (SessionMachine is uninitialized) */
861 alock.release();
862
863 /* XXX This will fail with
864 * "cannot be closed because it is still attached to 1 virtual machines"
865 * because at this point we did not call uninitDataAndChildObjects() yet
866 * and therefore also removeBackReference() for all these mediums was not called! */
867
868 if (uuidMachine.isValid() && !uuidMachine.isZero()) // can be empty if we're called from a failure of Machine::init
869 mParent->i_unregisterMachineMedia(uuidMachine);
870
871 // has machine been modified?
872 if (mData->flModifications)
873 {
874 Log1WarningThisFunc(("Discarding unsaved settings changes!\n"));
875 i_rollback(false /* aNotify */);
876 }
877
878 if (mData->mAccessible)
879 uninitDataAndChildObjects();
880
881 /* free the essential data structure last */
882 mData.free();
883
884 LogFlowThisFuncLeave();
885}
886
887// Wrapped IMachine properties
888/////////////////////////////////////////////////////////////////////////////
889HRESULT Machine::getParent(ComPtr<IVirtualBox> &aParent)
890{
891 /* mParent is constant during life time, no need to lock */
892 ComObjPtr<VirtualBox> pVirtualBox(mParent);
893 aParent = pVirtualBox;
894
895 return S_OK;
896}
897
898
899HRESULT Machine::getAccessible(BOOL *aAccessible)
900{
901 /* In some cases (medium registry related), it is necessary to be able to
902 * go through the list of all machines. Happens when an inaccessible VM
903 * has a sensible medium registry. */
904 AutoReadLock mllock(mParent->i_getMachinesListLockHandle() COMMA_LOCKVAL_SRC_POS);
905 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
906
907 HRESULT rc = S_OK;
908
909 if (!mData->mAccessible)
910 {
911 /* try to initialize the VM once more if not accessible */
912
913 AutoReinitSpan autoReinitSpan(this);
914 AssertReturn(autoReinitSpan.isOk(), E_FAIL);
915
916#ifdef DEBUG
917 LogFlowThisFunc(("Dumping media backreferences\n"));
918 mParent->i_dumpAllBackRefs();
919#endif
920
921 if (mData->pMachineConfigFile)
922 {
923 // reset the XML file to force loadSettings() (called from i_registeredInit())
924 // to parse it again; the file might have changed
925 delete mData->pMachineConfigFile;
926 mData->pMachineConfigFile = NULL;
927 }
928
929 rc = i_registeredInit();
930
931 if (SUCCEEDED(rc) && mData->mAccessible)
932 {
933 autoReinitSpan.setSucceeded();
934
935 /* make sure interesting parties will notice the accessibility
936 * state change */
937 mParent->i_onMachineStateChange(mData->mUuid, mData->mMachineState);
938 mParent->i_onMachineDataChange(mData->mUuid);
939 }
940 }
941
942 if (SUCCEEDED(rc))
943 *aAccessible = mData->mAccessible;
944
945 LogFlowThisFuncLeave();
946
947 return rc;
948}
949
950HRESULT Machine::getAccessError(ComPtr<IVirtualBoxErrorInfo> &aAccessError)
951{
952 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
953
954 if (mData->mAccessible || !mData->mAccessError.isBasicAvailable())
955 {
956 /* return shortly */
957 aAccessError = NULL;
958 return S_OK;
959 }
960
961 HRESULT rc = S_OK;
962
963 ComObjPtr<VirtualBoxErrorInfo> errorInfo;
964 rc = errorInfo.createObject();
965 if (SUCCEEDED(rc))
966 {
967 errorInfo->init(mData->mAccessError.getResultCode(),
968 mData->mAccessError.getInterfaceID().ref(),
969 Utf8Str(mData->mAccessError.getComponent()).c_str(),
970 Utf8Str(mData->mAccessError.getText()));
971 aAccessError = errorInfo;
972 }
973
974 return rc;
975}
976
977HRESULT Machine::getName(com::Utf8Str &aName)
978{
979 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
980
981 aName = mUserData->s.strName;
982
983 return S_OK;
984}
985
986HRESULT Machine::setName(const com::Utf8Str &aName)
987{
988 // prohibit setting a UUID only as the machine name, or else it can
989 // never be found by findMachine()
990 Guid test(aName);
991
992 if (test.isValid())
993 return setError(E_INVALIDARG, tr("A machine cannot have a UUID as its name"));
994
995 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
996
997 HRESULT rc = i_checkStateDependency(MutableStateDep);
998 if (FAILED(rc)) return rc;
999
1000 i_setModified(IsModified_MachineData);
1001 mUserData.backup();
1002 mUserData->s.strName = aName;
1003
1004 return S_OK;
1005}
1006
1007HRESULT Machine::getDescription(com::Utf8Str &aDescription)
1008{
1009 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1010
1011 aDescription = mUserData->s.strDescription;
1012
1013 return S_OK;
1014}
1015
1016HRESULT Machine::setDescription(const com::Utf8Str &aDescription)
1017{
1018 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1019
1020 // this can be done in principle in any state as it doesn't affect the VM
1021 // significantly, but play safe by not messing around while complex
1022 // activities are going on
1023 HRESULT rc = i_checkStateDependency(MutableOrSavedOrRunningStateDep);
1024 if (FAILED(rc)) return rc;
1025
1026 i_setModified(IsModified_MachineData);
1027 mUserData.backup();
1028 mUserData->s.strDescription = aDescription;
1029
1030 return S_OK;
1031}
1032
1033HRESULT Machine::getId(com::Guid &aId)
1034{
1035 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1036
1037 aId = mData->mUuid;
1038
1039 return S_OK;
1040}
1041
1042HRESULT Machine::getGroups(std::vector<com::Utf8Str> &aGroups)
1043{
1044 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1045 aGroups.resize(mUserData->s.llGroups.size());
1046 size_t i = 0;
1047 for (StringsList::const_iterator
1048 it = mUserData->s.llGroups.begin();
1049 it != mUserData->s.llGroups.end();
1050 ++it, ++i)
1051 aGroups[i] = (*it);
1052
1053 return S_OK;
1054}
1055
1056HRESULT Machine::setGroups(const std::vector<com::Utf8Str> &aGroups)
1057{
1058 StringsList llGroups;
1059 HRESULT rc = mParent->i_convertMachineGroups(aGroups, &llGroups);
1060 if (FAILED(rc))
1061 return rc;
1062
1063 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1064
1065 rc = i_checkStateDependency(MutableOrSavedStateDep);
1066 if (FAILED(rc)) return rc;
1067
1068 i_setModified(IsModified_MachineData);
1069 mUserData.backup();
1070 mUserData->s.llGroups = llGroups;
1071
1072 return S_OK;
1073}
1074
1075HRESULT Machine::getOSTypeId(com::Utf8Str &aOSTypeId)
1076{
1077 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1078
1079 aOSTypeId = mUserData->s.strOsType;
1080
1081 return S_OK;
1082}
1083
1084HRESULT Machine::setOSTypeId(const com::Utf8Str &aOSTypeId)
1085{
1086 /* look up the object by Id to check it is valid */
1087 ComPtr<IGuestOSType> guestOSType;
1088 HRESULT rc = mParent->GetGuestOSType(Bstr(aOSTypeId).raw(), guestOSType.asOutParam());
1089 if (FAILED(rc)) return rc;
1090
1091 /* when setting, always use the "etalon" value for consistency -- lookup
1092 * by ID is case-insensitive and the input value may have different case */
1093 Bstr osTypeId;
1094 rc = guestOSType->COMGETTER(Id)(osTypeId.asOutParam());
1095 if (FAILED(rc)) return rc;
1096
1097 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1098
1099 rc = i_checkStateDependency(MutableStateDep);
1100 if (FAILED(rc)) return rc;
1101
1102 i_setModified(IsModified_MachineData);
1103 mUserData.backup();
1104 mUserData->s.strOsType = osTypeId;
1105
1106 return S_OK;
1107}
1108
1109HRESULT Machine::getFirmwareType(FirmwareType_T *aFirmwareType)
1110{
1111 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1112
1113 *aFirmwareType = mHWData->mFirmwareType;
1114
1115 return S_OK;
1116}
1117
1118HRESULT Machine::setFirmwareType(FirmwareType_T aFirmwareType)
1119{
1120 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1121
1122 HRESULT rc = i_checkStateDependency(MutableStateDep);
1123 if (FAILED(rc)) return rc;
1124
1125 i_setModified(IsModified_MachineData);
1126 mHWData.backup();
1127 mHWData->mFirmwareType = aFirmwareType;
1128
1129 return S_OK;
1130}
1131
1132HRESULT Machine::getKeyboardHIDType(KeyboardHIDType_T *aKeyboardHIDType)
1133{
1134 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1135
1136 *aKeyboardHIDType = mHWData->mKeyboardHIDType;
1137
1138 return S_OK;
1139}
1140
1141HRESULT Machine::setKeyboardHIDType(KeyboardHIDType_T aKeyboardHIDType)
1142{
1143 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1144
1145 HRESULT rc = i_checkStateDependency(MutableStateDep);
1146 if (FAILED(rc)) return rc;
1147
1148 i_setModified(IsModified_MachineData);
1149 mHWData.backup();
1150 mHWData->mKeyboardHIDType = aKeyboardHIDType;
1151
1152 return S_OK;
1153}
1154
1155HRESULT Machine::getPointingHIDType(PointingHIDType_T *aPointingHIDType)
1156{
1157 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1158
1159 *aPointingHIDType = mHWData->mPointingHIDType;
1160
1161 return S_OK;
1162}
1163
1164HRESULT Machine::setPointingHIDType(PointingHIDType_T aPointingHIDType)
1165{
1166 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1167
1168 HRESULT rc = i_checkStateDependency(MutableStateDep);
1169 if (FAILED(rc)) return rc;
1170
1171 i_setModified(IsModified_MachineData);
1172 mHWData.backup();
1173 mHWData->mPointingHIDType = aPointingHIDType;
1174
1175 return S_OK;
1176}
1177
1178HRESULT Machine::getChipsetType(ChipsetType_T *aChipsetType)
1179{
1180 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1181
1182 *aChipsetType = mHWData->mChipsetType;
1183
1184 return S_OK;
1185}
1186
1187HRESULT Machine::setChipsetType(ChipsetType_T aChipsetType)
1188{
1189 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1190
1191 HRESULT rc = i_checkStateDependency(MutableStateDep);
1192 if (FAILED(rc)) return rc;
1193
1194 if (aChipsetType != mHWData->mChipsetType)
1195 {
1196 i_setModified(IsModified_MachineData);
1197 mHWData.backup();
1198 mHWData->mChipsetType = aChipsetType;
1199
1200 // Resize network adapter array, to be finalized on commit/rollback.
1201 // We must not throw away entries yet, otherwise settings are lost
1202 // without a way to roll back.
1203 size_t newCount = Global::getMaxNetworkAdapters(aChipsetType);
1204 size_t oldCount = mNetworkAdapters.size();
1205 if (newCount > oldCount)
1206 {
1207 mNetworkAdapters.resize(newCount);
1208 for (size_t slot = oldCount; slot < mNetworkAdapters.size(); slot++)
1209 {
1210 unconst(mNetworkAdapters[slot]).createObject();
1211 mNetworkAdapters[slot]->init(this, (ULONG)slot);
1212 }
1213 }
1214 }
1215
1216 return S_OK;
1217}
1218
1219HRESULT Machine::getParavirtDebug(com::Utf8Str &aParavirtDebug)
1220{
1221 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1222
1223 aParavirtDebug = mHWData->mParavirtDebug;
1224 return S_OK;
1225}
1226
1227HRESULT Machine::setParavirtDebug(const com::Utf8Str &aParavirtDebug)
1228{
1229 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1230
1231 HRESULT rc = i_checkStateDependency(MutableStateDep);
1232 if (FAILED(rc)) return rc;
1233
1234 /** @todo Parse/validate options? */
1235 if (aParavirtDebug != mHWData->mParavirtDebug)
1236 {
1237 i_setModified(IsModified_MachineData);
1238 mHWData.backup();
1239 mHWData->mParavirtDebug = aParavirtDebug;
1240 }
1241
1242 return S_OK;
1243}
1244
1245HRESULT Machine::getParavirtProvider(ParavirtProvider_T *aParavirtProvider)
1246{
1247 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1248
1249 *aParavirtProvider = mHWData->mParavirtProvider;
1250
1251 return S_OK;
1252}
1253
1254HRESULT Machine::setParavirtProvider(ParavirtProvider_T aParavirtProvider)
1255{
1256 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1257
1258 HRESULT rc = i_checkStateDependency(MutableStateDep);
1259 if (FAILED(rc)) return rc;
1260
1261 if (aParavirtProvider != mHWData->mParavirtProvider)
1262 {
1263 i_setModified(IsModified_MachineData);
1264 mHWData.backup();
1265 mHWData->mParavirtProvider = aParavirtProvider;
1266 }
1267
1268 return S_OK;
1269}
1270
1271HRESULT Machine::getEffectiveParavirtProvider(ParavirtProvider_T *aParavirtProvider)
1272{
1273 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1274
1275 *aParavirtProvider = mHWData->mParavirtProvider;
1276 switch (mHWData->mParavirtProvider)
1277 {
1278 case ParavirtProvider_None:
1279 case ParavirtProvider_HyperV:
1280 case ParavirtProvider_KVM:
1281 case ParavirtProvider_Minimal:
1282 break;
1283
1284 /* Resolve dynamic provider types to the effective types. */
1285 default:
1286 {
1287 ComPtr<IGuestOSType> ptrGuestOSType;
1288 HRESULT hrc2 = mParent->GetGuestOSType(Bstr(mUserData->s.strOsType).raw(), ptrGuestOSType.asOutParam());
1289 AssertMsgReturn(SUCCEEDED(hrc2), ("Failed to get guest OS type. hrc2=%Rhrc\n", hrc2), hrc2);
1290
1291 Bstr guestTypeFamilyId;
1292 hrc2 = ptrGuestOSType->COMGETTER(FamilyId)(guestTypeFamilyId.asOutParam());
1293 AssertMsgReturn(SUCCEEDED(hrc2), ("Failed to get guest family. hrc2=%Rhrc\n", hrc2), hrc2);
1294 BOOL fOsXGuest = guestTypeFamilyId == Bstr("MacOS");
1295
1296 switch (mHWData->mParavirtProvider)
1297 {
1298 case ParavirtProvider_Legacy:
1299 {
1300 if (fOsXGuest)
1301 *aParavirtProvider = ParavirtProvider_Minimal;
1302 else
1303 *aParavirtProvider = ParavirtProvider_None;
1304 break;
1305 }
1306
1307 case ParavirtProvider_Default:
1308 {
1309 if (fOsXGuest)
1310 *aParavirtProvider = ParavirtProvider_Minimal;
1311 else if ( mUserData->s.strOsType == "Windows10"
1312 || mUserData->s.strOsType == "Windows10_64"
1313 || mUserData->s.strOsType == "Windows81"
1314 || mUserData->s.strOsType == "Windows81_64"
1315 || mUserData->s.strOsType == "Windows8"
1316 || mUserData->s.strOsType == "Windows8_64"
1317 || mUserData->s.strOsType == "Windows7"
1318 || mUserData->s.strOsType == "Windows7_64"
1319 || mUserData->s.strOsType == "WindowsVista"
1320 || mUserData->s.strOsType == "WindowsVista_64"
1321 || mUserData->s.strOsType == "Windows2012"
1322 || mUserData->s.strOsType == "Windows2012_64"
1323 || mUserData->s.strOsType == "Windows2008"
1324 || mUserData->s.strOsType == "Windows2008_64")
1325 {
1326 *aParavirtProvider = ParavirtProvider_HyperV;
1327 }
1328 else if ( mUserData->s.strOsType == "Linux26" // Linux22 and Linux24 omitted as they're too old
1329 || mUserData->s.strOsType == "Linux26_64" // for having any KVM paravirtualization support.
1330 || mUserData->s.strOsType == "Linux"
1331 || mUserData->s.strOsType == "Linux_64"
1332 || mUserData->s.strOsType == "ArchLinux"
1333 || mUserData->s.strOsType == "ArchLinux_64"
1334 || mUserData->s.strOsType == "Debian"
1335 || mUserData->s.strOsType == "Debian_64"
1336 || mUserData->s.strOsType == "Fedora"
1337 || mUserData->s.strOsType == "Fedora_64"
1338 || mUserData->s.strOsType == "Gentoo"
1339 || mUserData->s.strOsType == "Gentoo_64"
1340 || mUserData->s.strOsType == "Mandriva"
1341 || mUserData->s.strOsType == "Mandriva_64"
1342 || mUserData->s.strOsType == "OpenSUSE"
1343 || mUserData->s.strOsType == "OpenSUSE_64"
1344 || mUserData->s.strOsType == "Oracle"
1345 || mUserData->s.strOsType == "Oracle_64"
1346 || mUserData->s.strOsType == "RedHat"
1347 || mUserData->s.strOsType == "RedHat_64"
1348 || mUserData->s.strOsType == "Turbolinux"
1349 || mUserData->s.strOsType == "Turbolinux_64"
1350 || mUserData->s.strOsType == "Ubuntu"
1351 || mUserData->s.strOsType == "Ubuntu_64"
1352 || mUserData->s.strOsType == "Xandros"
1353 || mUserData->s.strOsType == "Xandros_64")
1354 {
1355 *aParavirtProvider = ParavirtProvider_KVM;
1356 }
1357 else
1358 *aParavirtProvider = ParavirtProvider_None;
1359 break;
1360 }
1361
1362 default: AssertFailedBreak(); /* Shut up MSC. */
1363 }
1364 break;
1365 }
1366 }
1367
1368 Assert( *aParavirtProvider == ParavirtProvider_None
1369 || *aParavirtProvider == ParavirtProvider_Minimal
1370 || *aParavirtProvider == ParavirtProvider_HyperV
1371 || *aParavirtProvider == ParavirtProvider_KVM);
1372 return S_OK;
1373}
1374
1375HRESULT Machine::getHardwareVersion(com::Utf8Str &aHardwareVersion)
1376{
1377 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1378
1379 aHardwareVersion = mHWData->mHWVersion;
1380
1381 return S_OK;
1382}
1383
1384HRESULT Machine::setHardwareVersion(const com::Utf8Str &aHardwareVersion)
1385{
1386 /* check known version */
1387 Utf8Str hwVersion = aHardwareVersion;
1388 if ( hwVersion.compare("1") != 0
1389 && hwVersion.compare("2") != 0)
1390 return setError(E_INVALIDARG,
1391 tr("Invalid hardware version: %s\n"), aHardwareVersion.c_str());
1392
1393 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1394
1395 HRESULT rc = i_checkStateDependency(MutableStateDep);
1396 if (FAILED(rc)) return rc;
1397
1398 i_setModified(IsModified_MachineData);
1399 mHWData.backup();
1400 mHWData->mHWVersion = aHardwareVersion;
1401
1402 return S_OK;
1403}
1404
1405HRESULT Machine::getHardwareUUID(com::Guid &aHardwareUUID)
1406{
1407 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1408
1409 if (!mHWData->mHardwareUUID.isZero())
1410 aHardwareUUID = mHWData->mHardwareUUID;
1411 else
1412 aHardwareUUID = mData->mUuid;
1413
1414 return S_OK;
1415}
1416
1417HRESULT Machine::setHardwareUUID(const com::Guid &aHardwareUUID)
1418{
1419 if (!aHardwareUUID.isValid())
1420 return E_INVALIDARG;
1421
1422 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1423
1424 HRESULT rc = i_checkStateDependency(MutableStateDep);
1425 if (FAILED(rc)) return rc;
1426
1427 i_setModified(IsModified_MachineData);
1428 mHWData.backup();
1429 if (aHardwareUUID == mData->mUuid)
1430 mHWData->mHardwareUUID.clear();
1431 else
1432 mHWData->mHardwareUUID = aHardwareUUID;
1433
1434 return S_OK;
1435}
1436
1437HRESULT Machine::getMemorySize(ULONG *aMemorySize)
1438{
1439 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1440
1441 *aMemorySize = mHWData->mMemorySize;
1442
1443 return S_OK;
1444}
1445
1446HRESULT Machine::setMemorySize(ULONG aMemorySize)
1447{
1448 /* check RAM limits */
1449 if ( aMemorySize < MM_RAM_MIN_IN_MB
1450 || aMemorySize > MM_RAM_MAX_IN_MB
1451 )
1452 return setError(E_INVALIDARG,
1453 tr("Invalid RAM size: %lu MB (must be in range [%lu, %lu] MB)"),
1454 aMemorySize, MM_RAM_MIN_IN_MB, MM_RAM_MAX_IN_MB);
1455
1456 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1457
1458 HRESULT rc = i_checkStateDependency(MutableStateDep);
1459 if (FAILED(rc)) return rc;
1460
1461 i_setModified(IsModified_MachineData);
1462 mHWData.backup();
1463 mHWData->mMemorySize = aMemorySize;
1464
1465 return S_OK;
1466}
1467
1468HRESULT Machine::getCPUCount(ULONG *aCPUCount)
1469{
1470 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1471
1472 *aCPUCount = mHWData->mCPUCount;
1473
1474 return S_OK;
1475}
1476
1477HRESULT Machine::setCPUCount(ULONG aCPUCount)
1478{
1479 /* check CPU limits */
1480 if ( aCPUCount < SchemaDefs::MinCPUCount
1481 || aCPUCount > SchemaDefs::MaxCPUCount
1482 )
1483 return setError(E_INVALIDARG,
1484 tr("Invalid virtual CPU count: %lu (must be in range [%lu, %lu])"),
1485 aCPUCount, SchemaDefs::MinCPUCount, SchemaDefs::MaxCPUCount);
1486
1487 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1488
1489 /* We cant go below the current number of CPUs attached if hotplug is enabled*/
1490 if (mHWData->mCPUHotPlugEnabled)
1491 {
1492 for (unsigned idx = aCPUCount; idx < SchemaDefs::MaxCPUCount; idx++)
1493 {
1494 if (mHWData->mCPUAttached[idx])
1495 return setError(E_INVALIDARG,
1496 tr("There is still a CPU attached to socket %lu."
1497 "Detach the CPU before removing the socket"),
1498 aCPUCount, idx+1);
1499 }
1500 }
1501
1502 HRESULT rc = i_checkStateDependency(MutableStateDep);
1503 if (FAILED(rc)) return rc;
1504
1505 i_setModified(IsModified_MachineData);
1506 mHWData.backup();
1507 mHWData->mCPUCount = aCPUCount;
1508
1509 return S_OK;
1510}
1511
1512HRESULT Machine::getCPUExecutionCap(ULONG *aCPUExecutionCap)
1513{
1514 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1515
1516 *aCPUExecutionCap = mHWData->mCpuExecutionCap;
1517
1518 return S_OK;
1519}
1520
1521HRESULT Machine::setCPUExecutionCap(ULONG aCPUExecutionCap)
1522{
1523 HRESULT rc = S_OK;
1524
1525 /* check throttle limits */
1526 if ( aCPUExecutionCap < 1
1527 || aCPUExecutionCap > 100
1528 )
1529 return setError(E_INVALIDARG,
1530 tr("Invalid CPU execution cap value: %lu (must be in range [%lu, %lu])"),
1531 aCPUExecutionCap, 1, 100);
1532
1533 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1534
1535 alock.release();
1536 rc = i_onCPUExecutionCapChange(aCPUExecutionCap);
1537 alock.acquire();
1538 if (FAILED(rc)) return rc;
1539
1540 i_setModified(IsModified_MachineData);
1541 mHWData.backup();
1542 mHWData->mCpuExecutionCap = aCPUExecutionCap;
1543
1544 /** Save settings if online - @todo why is this required? -- @bugref{6818} */
1545 if (Global::IsOnline(mData->mMachineState))
1546 i_saveSettings(NULL);
1547
1548 return S_OK;
1549}
1550
1551HRESULT Machine::getCPUHotPlugEnabled(BOOL *aCPUHotPlugEnabled)
1552{
1553 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1554
1555 *aCPUHotPlugEnabled = mHWData->mCPUHotPlugEnabled;
1556
1557 return S_OK;
1558}
1559
1560HRESULT Machine::setCPUHotPlugEnabled(BOOL aCPUHotPlugEnabled)
1561{
1562 HRESULT rc = S_OK;
1563
1564 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1565
1566 rc = i_checkStateDependency(MutableStateDep);
1567 if (FAILED(rc)) return rc;
1568
1569 if (mHWData->mCPUHotPlugEnabled != aCPUHotPlugEnabled)
1570 {
1571 if (aCPUHotPlugEnabled)
1572 {
1573 i_setModified(IsModified_MachineData);
1574 mHWData.backup();
1575
1576 /* Add the amount of CPUs currently attached */
1577 for (unsigned i = 0; i < mHWData->mCPUCount; ++i)
1578 mHWData->mCPUAttached[i] = true;
1579 }
1580 else
1581 {
1582 /*
1583 * We can disable hotplug only if the amount of maximum CPUs is equal
1584 * to the amount of attached CPUs
1585 */
1586 unsigned cCpusAttached = 0;
1587 unsigned iHighestId = 0;
1588
1589 for (unsigned i = 0; i < SchemaDefs::MaxCPUCount; ++i)
1590 {
1591 if (mHWData->mCPUAttached[i])
1592 {
1593 cCpusAttached++;
1594 iHighestId = i;
1595 }
1596 }
1597
1598 if ( (cCpusAttached != mHWData->mCPUCount)
1599 || (iHighestId >= mHWData->mCPUCount))
1600 return setError(E_INVALIDARG,
1601 tr("CPU hotplugging can't be disabled because the maximum number of CPUs is not equal to the amount of CPUs attached"));
1602
1603 i_setModified(IsModified_MachineData);
1604 mHWData.backup();
1605 }
1606 }
1607
1608 mHWData->mCPUHotPlugEnabled = aCPUHotPlugEnabled;
1609
1610 return rc;
1611}
1612
1613HRESULT Machine::getCPUIDPortabilityLevel(ULONG *aCPUIDPortabilityLevel)
1614{
1615 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1616
1617 *aCPUIDPortabilityLevel = mHWData->mCpuIdPortabilityLevel;
1618
1619 return S_OK;
1620}
1621
1622HRESULT Machine::setCPUIDPortabilityLevel(ULONG aCPUIDPortabilityLevel)
1623{
1624 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1625
1626 HRESULT hrc = i_checkStateDependency(MutableStateDep);
1627 if (SUCCEEDED(hrc))
1628 {
1629 i_setModified(IsModified_MachineData);
1630 mHWData.backup();
1631 mHWData->mCpuIdPortabilityLevel = aCPUIDPortabilityLevel;
1632 }
1633 return hrc;
1634}
1635
1636HRESULT Machine::getCPUProfile(com::Utf8Str &aCPUProfile)
1637{
1638 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1639 aCPUProfile = mHWData->mCpuProfile;
1640 return S_OK;
1641}
1642
1643HRESULT Machine::setCPUProfile(const com::Utf8Str &aCPUProfile)
1644{
1645 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1646 HRESULT hrc = i_checkStateDependency(MutableStateDep);
1647 if (SUCCEEDED(hrc))
1648 {
1649 i_setModified(IsModified_MachineData);
1650 mHWData.backup();
1651 /* Empty equals 'host'. */
1652 if (aCPUProfile.isNotEmpty())
1653 mHWData->mCpuProfile = aCPUProfile;
1654 else
1655 mHWData->mCpuProfile = "host";
1656 }
1657 return hrc;
1658}
1659
1660HRESULT Machine::getEmulatedUSBCardReaderEnabled(BOOL *aEmulatedUSBCardReaderEnabled)
1661{
1662#ifdef VBOX_WITH_USB_CARDREADER
1663 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1664
1665 *aEmulatedUSBCardReaderEnabled = mHWData->mEmulatedUSBCardReaderEnabled;
1666
1667 return S_OK;
1668#else
1669 NOREF(aEmulatedUSBCardReaderEnabled);
1670 return E_NOTIMPL;
1671#endif
1672}
1673
1674HRESULT Machine::setEmulatedUSBCardReaderEnabled(BOOL aEmulatedUSBCardReaderEnabled)
1675{
1676#ifdef VBOX_WITH_USB_CARDREADER
1677 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1678
1679 HRESULT rc = i_checkStateDependency(MutableOrSavedStateDep);
1680 if (FAILED(rc)) return rc;
1681
1682 i_setModified(IsModified_MachineData);
1683 mHWData.backup();
1684 mHWData->mEmulatedUSBCardReaderEnabled = aEmulatedUSBCardReaderEnabled;
1685
1686 return S_OK;
1687#else
1688 NOREF(aEmulatedUSBCardReaderEnabled);
1689 return E_NOTIMPL;
1690#endif
1691}
1692
1693HRESULT Machine::getHPETEnabled(BOOL *aHPETEnabled)
1694{
1695 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1696
1697 *aHPETEnabled = mHWData->mHPETEnabled;
1698
1699 return S_OK;
1700}
1701
1702HRESULT Machine::setHPETEnabled(BOOL aHPETEnabled)
1703{
1704 HRESULT rc = S_OK;
1705
1706 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1707
1708 rc = i_checkStateDependency(MutableStateDep);
1709 if (FAILED(rc)) return rc;
1710
1711 i_setModified(IsModified_MachineData);
1712 mHWData.backup();
1713
1714 mHWData->mHPETEnabled = aHPETEnabled;
1715
1716 return rc;
1717}
1718
1719HRESULT Machine::getVideoCaptureEnabled(BOOL *aVideoCaptureEnabled)
1720{
1721 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1722
1723 *aVideoCaptureEnabled = mHWData->mVideoCaptureEnabled;
1724 return S_OK;
1725}
1726
1727HRESULT Machine::setVideoCaptureEnabled(BOOL aVideoCaptureEnabled)
1728{
1729 HRESULT rc = S_OK;
1730
1731 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1732
1733 i_setModified(IsModified_MachineData);
1734 mHWData.backup();
1735 mHWData->mVideoCaptureEnabled = aVideoCaptureEnabled;
1736
1737 alock.release();
1738 rc = i_onVideoCaptureChange();
1739 alock.acquire();
1740 if (FAILED(rc))
1741 {
1742 /*
1743 * Normally we would do the actual change _after_ i_onVideoCaptureChange() succeeded.
1744 * We cannot do this because that function uses Machine::GetVideoCaptureEnabled to
1745 * determine if it should start or stop capturing. Therefore we need to manually
1746 * undo change.
1747 */
1748 mHWData->mVideoCaptureEnabled = mHWData.backedUpData()->mVideoCaptureEnabled;
1749 return rc;
1750 }
1751
1752 /** Save settings if online - @todo why is this required? -- @bugref{6818} */
1753 if (Global::IsOnline(mData->mMachineState))
1754 i_saveSettings(NULL);
1755
1756 return rc;
1757}
1758
1759HRESULT Machine::getVideoCaptureScreens(std::vector<BOOL> &aVideoCaptureScreens)
1760{
1761 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1762 aVideoCaptureScreens.resize(mHWData->mMonitorCount);
1763 for (unsigned i = 0; i < mHWData->mMonitorCount; ++i)
1764 aVideoCaptureScreens[i] = mHWData->maVideoCaptureScreens[i];
1765 return S_OK;
1766}
1767
1768HRESULT Machine::setVideoCaptureScreens(const std::vector<BOOL> &aVideoCaptureScreens)
1769{
1770 AssertReturn(aVideoCaptureScreens.size() <= RT_ELEMENTS(mHWData->maVideoCaptureScreens), E_INVALIDARG);
1771 bool fChanged = false;
1772
1773 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1774
1775 for (unsigned i = 0; i < aVideoCaptureScreens.size(); ++i)
1776 {
1777 if (mHWData->maVideoCaptureScreens[i] != RT_BOOL(aVideoCaptureScreens[i]))
1778 {
1779 mHWData->maVideoCaptureScreens[i] = RT_BOOL(aVideoCaptureScreens[i]);
1780 fChanged = true;
1781 }
1782 }
1783 if (fChanged)
1784 {
1785 alock.release();
1786 HRESULT rc = i_onVideoCaptureChange();
1787 alock.acquire();
1788 if (FAILED(rc)) return rc;
1789 i_setModified(IsModified_MachineData);
1790
1791 /** Save settings if online - @todo why is this required? -- @bugref{6818} */
1792 if (Global::IsOnline(mData->mMachineState))
1793 i_saveSettings(NULL);
1794 }
1795
1796 return S_OK;
1797}
1798
1799HRESULT Machine::getVideoCaptureFile(com::Utf8Str &aVideoCaptureFile)
1800{
1801 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1802 if (mHWData->mVideoCaptureFile.isEmpty())
1803 i_getDefaultVideoCaptureFile(aVideoCaptureFile);
1804 else
1805 aVideoCaptureFile = mHWData->mVideoCaptureFile;
1806 return S_OK;
1807}
1808
1809HRESULT Machine::setVideoCaptureFile(const com::Utf8Str &aVideoCaptureFile)
1810{
1811 Utf8Str strFile(aVideoCaptureFile);
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 if (!RTPathStartsWithRoot(strFile.c_str()))
1819 return setError(E_INVALIDARG, tr("Video capture file name '%s' is not absolute"), strFile.c_str());
1820
1821 if (!strFile.isEmpty())
1822 {
1823 Utf8Str defaultFile;
1824 i_getDefaultVideoCaptureFile(defaultFile);
1825 if (!RTPathCompare(strFile.c_str(), defaultFile.c_str()))
1826 strFile.setNull();
1827 }
1828
1829 i_setModified(IsModified_MachineData);
1830 mHWData.backup();
1831 mHWData->mVideoCaptureFile = strFile;
1832
1833 return S_OK;
1834}
1835
1836HRESULT Machine::getVideoCaptureWidth(ULONG *aVideoCaptureWidth)
1837{
1838 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1839 *aVideoCaptureWidth = mHWData->mVideoCaptureWidth;
1840 return S_OK;
1841}
1842
1843HRESULT Machine::setVideoCaptureWidth(ULONG aVideoCaptureWidth)
1844{
1845 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1846
1847 if ( Global::IsOnline(mData->mMachineState)
1848 && mHWData->mVideoCaptureEnabled)
1849 return setError(E_INVALIDARG, tr("Cannot change parameters while capturing is enabled"));
1850
1851 i_setModified(IsModified_MachineData);
1852 mHWData.backup();
1853 mHWData->mVideoCaptureWidth = aVideoCaptureWidth;
1854
1855 return S_OK;
1856}
1857
1858HRESULT Machine::getVideoCaptureHeight(ULONG *aVideoCaptureHeight)
1859{
1860 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1861 *aVideoCaptureHeight = mHWData->mVideoCaptureHeight;
1862 return S_OK;
1863}
1864
1865HRESULT Machine::setVideoCaptureHeight(ULONG aVideoCaptureHeight)
1866{
1867 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1868
1869 if ( Global::IsOnline(mData->mMachineState)
1870 && mHWData->mVideoCaptureEnabled)
1871 return setError(E_INVALIDARG, tr("Cannot change parameters while capturing is enabled"));
1872
1873 i_setModified(IsModified_MachineData);
1874 mHWData.backup();
1875 mHWData->mVideoCaptureHeight = aVideoCaptureHeight;
1876
1877 return S_OK;
1878}
1879
1880HRESULT Machine::getVideoCaptureRate(ULONG *aVideoCaptureRate)
1881{
1882 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1883 *aVideoCaptureRate = mHWData->mVideoCaptureRate;
1884 return S_OK;
1885}
1886
1887HRESULT Machine::setVideoCaptureRate(ULONG aVideoCaptureRate)
1888{
1889 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1890
1891 if ( Global::IsOnline(mData->mMachineState)
1892 && mHWData->mVideoCaptureEnabled)
1893 return setError(E_INVALIDARG, tr("Cannot change parameters while capturing is enabled"));
1894
1895 i_setModified(IsModified_MachineData);
1896 mHWData.backup();
1897 mHWData->mVideoCaptureRate = aVideoCaptureRate;
1898
1899 return S_OK;
1900}
1901
1902HRESULT Machine::getVideoCaptureFPS(ULONG *aVideoCaptureFPS)
1903{
1904 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1905 *aVideoCaptureFPS = mHWData->mVideoCaptureFPS;
1906 return S_OK;
1907}
1908
1909HRESULT Machine::setVideoCaptureFPS(ULONG aVideoCaptureFPS)
1910{
1911 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1912
1913 if ( Global::IsOnline(mData->mMachineState)
1914 && mHWData->mVideoCaptureEnabled)
1915 return setError(E_INVALIDARG, tr("Cannot change parameters while capturing is enabled"));
1916
1917 i_setModified(IsModified_MachineData);
1918 mHWData.backup();
1919 mHWData->mVideoCaptureFPS = aVideoCaptureFPS;
1920
1921 return S_OK;
1922}
1923
1924HRESULT Machine::getVideoCaptureMaxTime(ULONG *aVideoCaptureMaxTime)
1925{
1926 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1927 *aVideoCaptureMaxTime = mHWData->mVideoCaptureMaxTime;
1928 return S_OK;
1929}
1930
1931HRESULT Machine::setVideoCaptureMaxTime(ULONG aVideoCaptureMaxTime)
1932{
1933 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1934
1935 if ( Global::IsOnline(mData->mMachineState)
1936 && mHWData->mVideoCaptureEnabled)
1937 return setError(E_INVALIDARG, tr("Cannot change parameters while capturing is enabled"));
1938
1939 i_setModified(IsModified_MachineData);
1940 mHWData.backup();
1941 mHWData->mVideoCaptureMaxTime = aVideoCaptureMaxTime;
1942
1943 return S_OK;
1944}
1945
1946HRESULT Machine::getVideoCaptureMaxFileSize(ULONG *aVideoCaptureMaxFileSize)
1947{
1948 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1949 *aVideoCaptureMaxFileSize = mHWData->mVideoCaptureMaxFileSize;
1950 return S_OK;
1951}
1952
1953HRESULT Machine::setVideoCaptureMaxFileSize(ULONG aVideoCaptureMaxFileSize)
1954{
1955 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1956
1957 if ( Global::IsOnline(mData->mMachineState)
1958 && mHWData->mVideoCaptureEnabled)
1959 return setError(E_INVALIDARG, tr("Cannot change parameters while capturing is enabled"));
1960
1961 i_setModified(IsModified_MachineData);
1962 mHWData.backup();
1963 mHWData->mVideoCaptureMaxFileSize = aVideoCaptureMaxFileSize;
1964
1965 return S_OK;
1966}
1967
1968HRESULT Machine::getVideoCaptureOptions(com::Utf8Str &aVideoCaptureOptions)
1969{
1970 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1971
1972 aVideoCaptureOptions = mHWData->mVideoCaptureOptions;
1973 return S_OK;
1974}
1975
1976HRESULT Machine::setVideoCaptureOptions(const com::Utf8Str &aVideoCaptureOptions)
1977{
1978 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1979
1980 if ( Global::IsOnline(mData->mMachineState)
1981 && mHWData->mVideoCaptureEnabled)
1982 return setError(E_INVALIDARG, tr("Cannot change parameters while capturing is enabled"));
1983
1984 i_setModified(IsModified_MachineData);
1985 mHWData.backup();
1986 mHWData->mVideoCaptureOptions = aVideoCaptureOptions;
1987
1988 return S_OK;
1989}
1990
1991HRESULT Machine::getGraphicsControllerType(GraphicsControllerType_T *aGraphicsControllerType)
1992{
1993 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1994
1995 *aGraphicsControllerType = mHWData->mGraphicsControllerType;
1996
1997 return S_OK;
1998}
1999
2000HRESULT Machine::setGraphicsControllerType(GraphicsControllerType_T aGraphicsControllerType)
2001{
2002 switch (aGraphicsControllerType)
2003 {
2004 case GraphicsControllerType_Null:
2005 case GraphicsControllerType_VBoxVGA:
2006#ifdef VBOX_WITH_VMSVGA
2007 case GraphicsControllerType_VMSVGA:
2008#endif
2009 break;
2010 default:
2011 return setError(E_INVALIDARG, tr("The graphics controller type (%d) is invalid"), aGraphicsControllerType);
2012 }
2013
2014 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2015
2016 HRESULT rc = i_checkStateDependency(MutableStateDep);
2017 if (FAILED(rc)) return rc;
2018
2019 i_setModified(IsModified_MachineData);
2020 mHWData.backup();
2021 mHWData->mGraphicsControllerType = aGraphicsControllerType;
2022
2023 return S_OK;
2024}
2025
2026HRESULT Machine::getVRAMSize(ULONG *aVRAMSize)
2027{
2028 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2029
2030 *aVRAMSize = mHWData->mVRAMSize;
2031
2032 return S_OK;
2033}
2034
2035HRESULT Machine::setVRAMSize(ULONG aVRAMSize)
2036{
2037 /* check VRAM limits */
2038 if (aVRAMSize > SchemaDefs::MaxGuestVRAM)
2039 return setError(E_INVALIDARG,
2040 tr("Invalid VRAM size: %lu MB (must be in range [%lu, %lu] MB)"),
2041 aVRAMSize, SchemaDefs::MinGuestVRAM, SchemaDefs::MaxGuestVRAM);
2042
2043 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2044
2045 HRESULT rc = i_checkStateDependency(MutableStateDep);
2046 if (FAILED(rc)) return rc;
2047
2048 i_setModified(IsModified_MachineData);
2049 mHWData.backup();
2050 mHWData->mVRAMSize = aVRAMSize;
2051
2052 return S_OK;
2053}
2054
2055/** @todo this method should not be public */
2056HRESULT Machine::getMemoryBalloonSize(ULONG *aMemoryBalloonSize)
2057{
2058 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2059
2060 *aMemoryBalloonSize = mHWData->mMemoryBalloonSize;
2061
2062 return S_OK;
2063}
2064
2065/**
2066 * Set the memory balloon size.
2067 *
2068 * This method is also called from IGuest::COMSETTER(MemoryBalloonSize) so
2069 * we have to make sure that we never call IGuest from here.
2070 */
2071HRESULT Machine::setMemoryBalloonSize(ULONG aMemoryBalloonSize)
2072{
2073 /* This must match GMMR0Init; currently we only support memory ballooning on all 64-bit hosts except Mac OS X */
2074#if HC_ARCH_BITS == 64 && (defined(RT_OS_WINDOWS) || defined(RT_OS_SOLARIS) || defined(RT_OS_LINUX) || defined(RT_OS_FREEBSD))
2075 /* check limits */
2076 if (aMemoryBalloonSize >= VMMDEV_MAX_MEMORY_BALLOON(mHWData->mMemorySize))
2077 return setError(E_INVALIDARG,
2078 tr("Invalid memory balloon size: %lu MB (must be in range [%lu, %lu] MB)"),
2079 aMemoryBalloonSize, 0, VMMDEV_MAX_MEMORY_BALLOON(mHWData->mMemorySize));
2080
2081 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2082
2083 i_setModified(IsModified_MachineData);
2084 mHWData.backup();
2085 mHWData->mMemoryBalloonSize = aMemoryBalloonSize;
2086
2087 return S_OK;
2088#else
2089 NOREF(aMemoryBalloonSize);
2090 return setError(E_NOTIMPL, tr("Memory ballooning is only supported on 64-bit hosts"));
2091#endif
2092}
2093
2094HRESULT Machine::getPageFusionEnabled(BOOL *aPageFusionEnabled)
2095{
2096 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2097
2098 *aPageFusionEnabled = mHWData->mPageFusionEnabled;
2099 return S_OK;
2100}
2101
2102HRESULT Machine::setPageFusionEnabled(BOOL aPageFusionEnabled)
2103{
2104#ifdef VBOX_WITH_PAGE_SHARING
2105 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2106
2107 /** @todo must support changes for running vms and keep this in sync with IGuest. */
2108 i_setModified(IsModified_MachineData);
2109 mHWData.backup();
2110 mHWData->mPageFusionEnabled = aPageFusionEnabled;
2111 return S_OK;
2112#else
2113 NOREF(aPageFusionEnabled);
2114 return setError(E_NOTIMPL, tr("Page fusion is only supported on 64-bit hosts"));
2115#endif
2116}
2117
2118HRESULT Machine::getAccelerate3DEnabled(BOOL *aAccelerate3DEnabled)
2119{
2120 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2121
2122 *aAccelerate3DEnabled = mHWData->mAccelerate3DEnabled;
2123
2124 return S_OK;
2125}
2126
2127HRESULT Machine::setAccelerate3DEnabled(BOOL aAccelerate3DEnabled)
2128{
2129 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2130
2131 HRESULT rc = i_checkStateDependency(MutableStateDep);
2132 if (FAILED(rc)) return rc;
2133
2134 /** @todo check validity! */
2135
2136 i_setModified(IsModified_MachineData);
2137 mHWData.backup();
2138 mHWData->mAccelerate3DEnabled = aAccelerate3DEnabled;
2139
2140 return S_OK;
2141}
2142
2143
2144HRESULT Machine::getAccelerate2DVideoEnabled(BOOL *aAccelerate2DVideoEnabled)
2145{
2146 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2147
2148 *aAccelerate2DVideoEnabled = mHWData->mAccelerate2DVideoEnabled;
2149
2150 return S_OK;
2151}
2152
2153HRESULT Machine::setAccelerate2DVideoEnabled(BOOL aAccelerate2DVideoEnabled)
2154{
2155 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2156
2157 HRESULT rc = i_checkStateDependency(MutableStateDep);
2158 if (FAILED(rc)) return rc;
2159
2160 /** @todo check validity! */
2161 i_setModified(IsModified_MachineData);
2162 mHWData.backup();
2163 mHWData->mAccelerate2DVideoEnabled = aAccelerate2DVideoEnabled;
2164
2165 return S_OK;
2166}
2167
2168HRESULT Machine::getMonitorCount(ULONG *aMonitorCount)
2169{
2170 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2171
2172 *aMonitorCount = mHWData->mMonitorCount;
2173
2174 return S_OK;
2175}
2176
2177HRESULT Machine::setMonitorCount(ULONG aMonitorCount)
2178{
2179 /* make sure monitor count is a sensible number */
2180 if (aMonitorCount < 1 || aMonitorCount > SchemaDefs::MaxGuestMonitors)
2181 return setError(E_INVALIDARG,
2182 tr("Invalid monitor count: %lu (must be in range [%lu, %lu])"),
2183 aMonitorCount, 1, SchemaDefs::MaxGuestMonitors);
2184
2185 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2186
2187 HRESULT rc = i_checkStateDependency(MutableStateDep);
2188 if (FAILED(rc)) return rc;
2189
2190 i_setModified(IsModified_MachineData);
2191 mHWData.backup();
2192 mHWData->mMonitorCount = aMonitorCount;
2193
2194 return S_OK;
2195}
2196
2197HRESULT Machine::getBIOSSettings(ComPtr<IBIOSSettings> &aBIOSSettings)
2198{
2199 /* mBIOSSettings is constant during life time, no need to lock */
2200 aBIOSSettings = mBIOSSettings;
2201
2202 return S_OK;
2203}
2204
2205HRESULT Machine::getCPUProperty(CPUPropertyType_T aProperty, BOOL *aValue)
2206{
2207 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2208
2209 switch (aProperty)
2210 {
2211 case CPUPropertyType_PAE:
2212 *aValue = mHWData->mPAEEnabled;
2213 break;
2214
2215 case CPUPropertyType_LongMode:
2216 if (mHWData->mLongMode == settings::Hardware::LongMode_Enabled)
2217 *aValue = TRUE;
2218 else if (mHWData->mLongMode == settings::Hardware::LongMode_Disabled)
2219 *aValue = FALSE;
2220#if HC_ARCH_BITS == 64
2221 else
2222 *aValue = TRUE;
2223#else
2224 else
2225 {
2226 *aValue = FALSE;
2227
2228 ComPtr<IGuestOSType> ptrGuestOSType;
2229 HRESULT hrc2 = mParent->GetGuestOSType(Bstr(mUserData->s.strOsType).raw(), ptrGuestOSType.asOutParam());
2230 if (SUCCEEDED(hrc2))
2231 {
2232 BOOL fIs64Bit = FALSE;
2233 hrc2 = ptrGuestOSType->COMGETTER(Is64Bit)(&fIs64Bit); AssertComRC(hrc2);
2234 if (SUCCEEDED(hrc2) && fIs64Bit)
2235 {
2236 ComObjPtr<Host> ptrHost = mParent->i_host();
2237 alock.release();
2238
2239 hrc2 = ptrHost->GetProcessorFeature(ProcessorFeature_LongMode, aValue); AssertComRC(hrc2);
2240 if (FAILED(hrc2))
2241 *aValue = FALSE;
2242 }
2243 }
2244 }
2245#endif
2246 break;
2247
2248 case CPUPropertyType_TripleFaultReset:
2249 *aValue = mHWData->mTripleFaultReset;
2250 break;
2251
2252 case CPUPropertyType_APIC:
2253 *aValue = mHWData->mAPIC;
2254 break;
2255
2256 case CPUPropertyType_X2APIC:
2257 *aValue = mHWData->mX2APIC;
2258 break;
2259
2260 default:
2261 return E_INVALIDARG;
2262 }
2263 return S_OK;
2264}
2265
2266HRESULT Machine::setCPUProperty(CPUPropertyType_T aProperty, BOOL aValue)
2267{
2268 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2269
2270 HRESULT rc = i_checkStateDependency(MutableStateDep);
2271 if (FAILED(rc)) return rc;
2272
2273 switch (aProperty)
2274 {
2275 case CPUPropertyType_PAE:
2276 i_setModified(IsModified_MachineData);
2277 mHWData.backup();
2278 mHWData->mPAEEnabled = !!aValue;
2279 break;
2280
2281 case CPUPropertyType_LongMode:
2282 i_setModified(IsModified_MachineData);
2283 mHWData.backup();
2284 mHWData->mLongMode = !aValue ? settings::Hardware::LongMode_Disabled : settings::Hardware::LongMode_Enabled;
2285 break;
2286
2287 case CPUPropertyType_TripleFaultReset:
2288 i_setModified(IsModified_MachineData);
2289 mHWData.backup();
2290 mHWData->mTripleFaultReset = !!aValue;
2291 break;
2292
2293 case CPUPropertyType_APIC:
2294 if (mHWData->mX2APIC)
2295 aValue = TRUE;
2296 i_setModified(IsModified_MachineData);
2297 mHWData.backup();
2298 mHWData->mAPIC = !!aValue;
2299 break;
2300
2301 case CPUPropertyType_X2APIC:
2302 i_setModified(IsModified_MachineData);
2303 mHWData.backup();
2304 mHWData->mX2APIC = !!aValue;
2305 if (aValue)
2306 mHWData->mAPIC = !!aValue;
2307 break;
2308
2309 default:
2310 return E_INVALIDARG;
2311 }
2312 return S_OK;
2313}
2314
2315HRESULT Machine::getCPUIDLeaf(ULONG aId, ULONG *aValEax, ULONG *aValEbx, ULONG *aValEcx, ULONG *aValEdx)
2316{
2317 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2318
2319 switch(aId)
2320 {
2321 case 0x0:
2322 case 0x1:
2323 case 0x2:
2324 case 0x3:
2325 case 0x4:
2326 case 0x5:
2327 case 0x6:
2328 case 0x7:
2329 case 0x8:
2330 case 0x9:
2331 case 0xA:
2332 if (mHWData->mCpuIdStdLeafs[aId].ulId != aId)
2333 return E_INVALIDARG;
2334
2335 *aValEax = mHWData->mCpuIdStdLeafs[aId].ulEax;
2336 *aValEbx = mHWData->mCpuIdStdLeafs[aId].ulEbx;
2337 *aValEcx = mHWData->mCpuIdStdLeafs[aId].ulEcx;
2338 *aValEdx = mHWData->mCpuIdStdLeafs[aId].ulEdx;
2339 break;
2340
2341 case 0x80000000:
2342 case 0x80000001:
2343 case 0x80000002:
2344 case 0x80000003:
2345 case 0x80000004:
2346 case 0x80000005:
2347 case 0x80000006:
2348 case 0x80000007:
2349 case 0x80000008:
2350 case 0x80000009:
2351 case 0x8000000A:
2352 if (mHWData->mCpuIdExtLeafs[aId - 0x80000000].ulId != aId)
2353 return E_INVALIDARG;
2354
2355 *aValEax = mHWData->mCpuIdExtLeafs[aId - 0x80000000].ulEax;
2356 *aValEbx = mHWData->mCpuIdExtLeafs[aId - 0x80000000].ulEbx;
2357 *aValEcx = mHWData->mCpuIdExtLeafs[aId - 0x80000000].ulEcx;
2358 *aValEdx = mHWData->mCpuIdExtLeafs[aId - 0x80000000].ulEdx;
2359 break;
2360
2361 default:
2362 return setError(E_INVALIDARG, tr("CpuId override leaf %#x is out of range"), aId);
2363 }
2364 return S_OK;
2365}
2366
2367
2368HRESULT Machine::setCPUIDLeaf(ULONG aId, ULONG aValEax, ULONG aValEbx, ULONG aValEcx, ULONG aValEdx)
2369{
2370 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2371
2372 HRESULT rc = i_checkStateDependency(MutableStateDep);
2373 if (FAILED(rc)) return rc;
2374
2375 switch(aId)
2376 {
2377 case 0x0:
2378 case 0x1:
2379 case 0x2:
2380 case 0x3:
2381 case 0x4:
2382 case 0x5:
2383 case 0x6:
2384 case 0x7:
2385 case 0x8:
2386 case 0x9:
2387 case 0xA:
2388 AssertCompile(RT_ELEMENTS(mHWData->mCpuIdStdLeafs) == 0xB);
2389 AssertRelease(aId < RT_ELEMENTS(mHWData->mCpuIdStdLeafs));
2390 i_setModified(IsModified_MachineData);
2391 mHWData.backup();
2392 mHWData->mCpuIdStdLeafs[aId].ulId = aId;
2393 mHWData->mCpuIdStdLeafs[aId].ulEax = aValEax;
2394 mHWData->mCpuIdStdLeafs[aId].ulEbx = aValEbx;
2395 mHWData->mCpuIdStdLeafs[aId].ulEcx = aValEcx;
2396 mHWData->mCpuIdStdLeafs[aId].ulEdx = aValEdx;
2397 break;
2398
2399 case 0x80000000:
2400 case 0x80000001:
2401 case 0x80000002:
2402 case 0x80000003:
2403 case 0x80000004:
2404 case 0x80000005:
2405 case 0x80000006:
2406 case 0x80000007:
2407 case 0x80000008:
2408 case 0x80000009:
2409 case 0x8000000A:
2410 AssertCompile(RT_ELEMENTS(mHWData->mCpuIdExtLeafs) == 0xB);
2411 AssertRelease(aId - 0x80000000 < RT_ELEMENTS(mHWData->mCpuIdExtLeafs));
2412 i_setModified(IsModified_MachineData);
2413 mHWData.backup();
2414 mHWData->mCpuIdExtLeafs[aId - 0x80000000].ulId = aId;
2415 mHWData->mCpuIdExtLeafs[aId - 0x80000000].ulEax = aValEax;
2416 mHWData->mCpuIdExtLeafs[aId - 0x80000000].ulEbx = aValEbx;
2417 mHWData->mCpuIdExtLeafs[aId - 0x80000000].ulEcx = aValEcx;
2418 mHWData->mCpuIdExtLeafs[aId - 0x80000000].ulEdx = aValEdx;
2419 break;
2420
2421 default:
2422 return setError(E_INVALIDARG, tr("CpuId override leaf %#x is out of range"), aId);
2423 }
2424 return S_OK;
2425}
2426
2427HRESULT Machine::removeCPUIDLeaf(ULONG aId)
2428{
2429 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2430
2431 HRESULT rc = i_checkStateDependency(MutableStateDep);
2432 if (FAILED(rc)) return rc;
2433
2434 switch(aId)
2435 {
2436 case 0x0:
2437 case 0x1:
2438 case 0x2:
2439 case 0x3:
2440 case 0x4:
2441 case 0x5:
2442 case 0x6:
2443 case 0x7:
2444 case 0x8:
2445 case 0x9:
2446 case 0xA:
2447 AssertCompile(RT_ELEMENTS(mHWData->mCpuIdStdLeafs) == 0xB);
2448 AssertRelease(aId < RT_ELEMENTS(mHWData->mCpuIdStdLeafs));
2449 i_setModified(IsModified_MachineData);
2450 mHWData.backup();
2451 /* Invalidate leaf. */
2452 mHWData->mCpuIdStdLeafs[aId].ulId = UINT32_MAX;
2453 break;
2454
2455 case 0x80000000:
2456 case 0x80000001:
2457 case 0x80000002:
2458 case 0x80000003:
2459 case 0x80000004:
2460 case 0x80000005:
2461 case 0x80000006:
2462 case 0x80000007:
2463 case 0x80000008:
2464 case 0x80000009:
2465 case 0x8000000A:
2466 AssertCompile(RT_ELEMENTS(mHWData->mCpuIdExtLeafs) == 0xB);
2467 AssertRelease(aId - 0x80000000 < RT_ELEMENTS(mHWData->mCpuIdExtLeafs));
2468 i_setModified(IsModified_MachineData);
2469 mHWData.backup();
2470 /* Invalidate leaf. */
2471 mHWData->mCpuIdExtLeafs[aId - 0x80000000].ulId = UINT32_MAX;
2472 break;
2473
2474 default:
2475 return setError(E_INVALIDARG, tr("CpuId override leaf %#x is out of range"), aId);
2476 }
2477 return S_OK;
2478}
2479
2480HRESULT Machine::removeAllCPUIDLeaves()
2481{
2482 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2483
2484 HRESULT rc = i_checkStateDependency(MutableStateDep);
2485 if (FAILED(rc)) return rc;
2486
2487 i_setModified(IsModified_MachineData);
2488 mHWData.backup();
2489
2490 /* Invalidate all standard leafs. */
2491 for (unsigned i = 0; i < RT_ELEMENTS(mHWData->mCpuIdStdLeafs); ++i)
2492 mHWData->mCpuIdStdLeafs[i].ulId = UINT32_MAX;
2493
2494 /* Invalidate all extended leafs. */
2495 for (unsigned i = 0; i < RT_ELEMENTS(mHWData->mCpuIdExtLeafs); ++i)
2496 mHWData->mCpuIdExtLeafs[i].ulId = UINT32_MAX;
2497
2498 return S_OK;
2499}
2500HRESULT Machine::getHWVirtExProperty(HWVirtExPropertyType_T aProperty, BOOL *aValue)
2501{
2502 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2503
2504 switch(aProperty)
2505 {
2506 case HWVirtExPropertyType_Enabled:
2507 *aValue = mHWData->mHWVirtExEnabled;
2508 break;
2509
2510 case HWVirtExPropertyType_VPID:
2511 *aValue = mHWData->mHWVirtExVPIDEnabled;
2512 break;
2513
2514 case HWVirtExPropertyType_NestedPaging:
2515 *aValue = mHWData->mHWVirtExNestedPagingEnabled;
2516 break;
2517
2518 case HWVirtExPropertyType_UnrestrictedExecution:
2519 *aValue = mHWData->mHWVirtExUXEnabled;
2520 break;
2521
2522 case HWVirtExPropertyType_LargePages:
2523 *aValue = mHWData->mHWVirtExLargePagesEnabled;
2524#if defined(DEBUG_bird) && defined(RT_OS_LINUX) /* This feature is deadly here */
2525 *aValue = FALSE;
2526#endif
2527 break;
2528
2529 case HWVirtExPropertyType_Force:
2530 *aValue = mHWData->mHWVirtExForceEnabled;
2531 break;
2532
2533 default:
2534 return E_INVALIDARG;
2535 }
2536 return S_OK;
2537}
2538
2539HRESULT Machine::setHWVirtExProperty(HWVirtExPropertyType_T aProperty, BOOL aValue)
2540{
2541 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2542
2543 HRESULT rc = i_checkStateDependency(MutableStateDep);
2544 if (FAILED(rc)) return rc;
2545
2546 switch(aProperty)
2547 {
2548 case HWVirtExPropertyType_Enabled:
2549 i_setModified(IsModified_MachineData);
2550 mHWData.backup();
2551 mHWData->mHWVirtExEnabled = !!aValue;
2552 break;
2553
2554 case HWVirtExPropertyType_VPID:
2555 i_setModified(IsModified_MachineData);
2556 mHWData.backup();
2557 mHWData->mHWVirtExVPIDEnabled = !!aValue;
2558 break;
2559
2560 case HWVirtExPropertyType_NestedPaging:
2561 i_setModified(IsModified_MachineData);
2562 mHWData.backup();
2563 mHWData->mHWVirtExNestedPagingEnabled = !!aValue;
2564 break;
2565
2566 case HWVirtExPropertyType_UnrestrictedExecution:
2567 i_setModified(IsModified_MachineData);
2568 mHWData.backup();
2569 mHWData->mHWVirtExUXEnabled = !!aValue;
2570 break;
2571
2572 case HWVirtExPropertyType_LargePages:
2573 i_setModified(IsModified_MachineData);
2574 mHWData.backup();
2575 mHWData->mHWVirtExLargePagesEnabled = !!aValue;
2576 break;
2577
2578 case HWVirtExPropertyType_Force:
2579 i_setModified(IsModified_MachineData);
2580 mHWData.backup();
2581 mHWData->mHWVirtExForceEnabled = !!aValue;
2582 break;
2583
2584 default:
2585 return E_INVALIDARG;
2586 }
2587
2588 return S_OK;
2589}
2590
2591HRESULT Machine::getSnapshotFolder(com::Utf8Str &aSnapshotFolder)
2592{
2593 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2594
2595 i_calculateFullPath(mUserData->s.strSnapshotFolder, aSnapshotFolder);
2596
2597 return S_OK;
2598}
2599
2600HRESULT Machine::setSnapshotFolder(const com::Utf8Str &aSnapshotFolder)
2601{
2602 /** @todo (r=dmik):
2603 * 1. Allow to change the name of the snapshot folder containing snapshots
2604 * 2. Rename the folder on disk instead of just changing the property
2605 * value (to be smart and not to leave garbage). Note that it cannot be
2606 * done here because the change may be rolled back. Thus, the right
2607 * place is #saveSettings().
2608 */
2609
2610 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2611
2612 HRESULT rc = i_checkStateDependency(MutableStateDep);
2613 if (FAILED(rc)) return rc;
2614
2615 if (!mData->mCurrentSnapshot.isNull())
2616 return setError(E_FAIL,
2617 tr("The snapshot folder of a machine with snapshots cannot be changed (please delete all snapshots first)"));
2618
2619 Utf8Str strSnapshotFolder(aSnapshotFolder); // keep original
2620
2621 if (strSnapshotFolder.isEmpty())
2622 strSnapshotFolder = "Snapshots";
2623 int vrc = i_calculateFullPath(strSnapshotFolder,
2624 strSnapshotFolder);
2625 if (RT_FAILURE(vrc))
2626 return setError(E_FAIL,
2627 tr("Invalid snapshot folder '%s' (%Rrc)"),
2628 strSnapshotFolder.c_str(), vrc);
2629
2630 i_setModified(IsModified_MachineData);
2631 mUserData.backup();
2632
2633 i_copyPathRelativeToMachine(strSnapshotFolder, mUserData->s.strSnapshotFolder);
2634
2635 return S_OK;
2636}
2637
2638HRESULT Machine::getMediumAttachments(std::vector<ComPtr<IMediumAttachment> > &aMediumAttachments)
2639{
2640 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2641
2642 aMediumAttachments.resize(mMediumAttachments->size());
2643 size_t i = 0;
2644 for (MediumAttachmentList::const_iterator
2645 it = mMediumAttachments->begin();
2646 it != mMediumAttachments->end();
2647 ++it, ++i)
2648 aMediumAttachments[i] = *it;
2649
2650 return S_OK;
2651}
2652
2653HRESULT Machine::getVRDEServer(ComPtr<IVRDEServer> &aVRDEServer)
2654{
2655 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2656
2657 Assert(!!mVRDEServer);
2658
2659 aVRDEServer = mVRDEServer;
2660
2661 return S_OK;
2662}
2663
2664HRESULT Machine::getAudioAdapter(ComPtr<IAudioAdapter> &aAudioAdapter)
2665{
2666 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2667
2668 aAudioAdapter = mAudioAdapter;
2669
2670 return S_OK;
2671}
2672
2673HRESULT Machine::getUSBControllers(std::vector<ComPtr<IUSBController> > &aUSBControllers)
2674{
2675#ifdef VBOX_WITH_VUSB
2676 clearError();
2677 MultiResult rc(S_OK);
2678
2679# ifdef VBOX_WITH_USB
2680 rc = mParent->i_host()->i_checkUSBProxyService();
2681 if (FAILED(rc)) return rc;
2682# endif
2683
2684 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2685
2686 aUSBControllers.resize(mUSBControllers->size());
2687 size_t i = 0;
2688 for (USBControllerList::const_iterator
2689 it = mUSBControllers->begin();
2690 it != mUSBControllers->end();
2691 ++it, ++i)
2692 aUSBControllers[i] = *it;
2693
2694 return S_OK;
2695#else
2696 /* Note: The GUI depends on this method returning E_NOTIMPL with no
2697 * extended error info to indicate that USB is simply not available
2698 * (w/o treating it as a failure), for example, as in OSE */
2699 NOREF(aUSBControllers);
2700 ReturnComNotImplemented();
2701#endif /* VBOX_WITH_VUSB */
2702}
2703
2704HRESULT Machine::getUSBDeviceFilters(ComPtr<IUSBDeviceFilters> &aUSBDeviceFilters)
2705{
2706#ifdef VBOX_WITH_VUSB
2707 clearError();
2708 MultiResult rc(S_OK);
2709
2710# ifdef VBOX_WITH_USB
2711 rc = mParent->i_host()->i_checkUSBProxyService();
2712 if (FAILED(rc)) return rc;
2713# endif
2714
2715 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2716
2717 aUSBDeviceFilters = mUSBDeviceFilters;
2718 return rc;
2719#else
2720 /* Note: The GUI depends on this method returning E_NOTIMPL with no
2721 * extended error info to indicate that USB is simply not available
2722 * (w/o treating it as a failure), for example, as in OSE */
2723 NOREF(aUSBDeviceFilters);
2724 ReturnComNotImplemented();
2725#endif /* VBOX_WITH_VUSB */
2726}
2727
2728HRESULT Machine::getSettingsFilePath(com::Utf8Str &aSettingsFilePath)
2729{
2730 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2731
2732 aSettingsFilePath = mData->m_strConfigFileFull;
2733
2734 return S_OK;
2735}
2736
2737HRESULT Machine::getSettingsAuxFilePath(com::Utf8Str &aSettingsFilePath)
2738{
2739 RT_NOREF(aSettingsFilePath);
2740 ReturnComNotImplemented();
2741}
2742
2743HRESULT Machine::getSettingsModified(BOOL *aSettingsModified)
2744{
2745 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2746
2747 HRESULT rc = i_checkStateDependency(MutableOrSavedOrRunningStateDep);
2748 if (FAILED(rc)) return rc;
2749
2750 if (!mData->pMachineConfigFile->fileExists())
2751 // this is a new machine, and no config file exists yet:
2752 *aSettingsModified = TRUE;
2753 else
2754 *aSettingsModified = (mData->flModifications != 0);
2755
2756 return S_OK;
2757}
2758
2759HRESULT Machine::getSessionState(SessionState_T *aSessionState)
2760{
2761 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2762
2763 *aSessionState = mData->mSession.mState;
2764
2765 return S_OK;
2766}
2767
2768HRESULT Machine::getSessionName(com::Utf8Str &aSessionName)
2769{
2770 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2771
2772 aSessionName = mData->mSession.mName;
2773
2774 return S_OK;
2775}
2776
2777HRESULT Machine::getSessionPID(ULONG *aSessionPID)
2778{
2779 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2780
2781 *aSessionPID = mData->mSession.mPID;
2782
2783 return S_OK;
2784}
2785
2786HRESULT Machine::getState(MachineState_T *aState)
2787{
2788 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2789
2790 *aState = mData->mMachineState;
2791 Assert(mData->mMachineState != MachineState_Null);
2792
2793 return S_OK;
2794}
2795
2796HRESULT Machine::getLastStateChange(LONG64 *aLastStateChange)
2797{
2798 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2799
2800 *aLastStateChange = RTTimeSpecGetMilli(&mData->mLastStateChange);
2801
2802 return S_OK;
2803}
2804
2805HRESULT Machine::getStateFilePath(com::Utf8Str &aStateFilePath)
2806{
2807 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2808
2809 aStateFilePath = mSSData->strStateFilePath;
2810
2811 return S_OK;
2812}
2813
2814HRESULT Machine::getLogFolder(com::Utf8Str &aLogFolder)
2815{
2816 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2817
2818 i_getLogFolder(aLogFolder);
2819
2820 return S_OK;
2821}
2822
2823HRESULT Machine::getCurrentSnapshot(ComPtr<ISnapshot> &aCurrentSnapshot)
2824{
2825 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2826
2827 aCurrentSnapshot = mData->mCurrentSnapshot;
2828
2829 return S_OK;
2830}
2831
2832HRESULT Machine::getSnapshotCount(ULONG *aSnapshotCount)
2833{
2834 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2835
2836 *aSnapshotCount = mData->mFirstSnapshot.isNull()
2837 ? 0
2838 : mData->mFirstSnapshot->i_getAllChildrenCount() + 1;
2839
2840 return S_OK;
2841}
2842
2843HRESULT Machine::getCurrentStateModified(BOOL *aCurrentStateModified)
2844{
2845 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2846
2847 /* Note: for machines with no snapshots, we always return FALSE
2848 * (mData->mCurrentStateModified will be TRUE in this case, for historical
2849 * reasons :) */
2850
2851 *aCurrentStateModified = mData->mFirstSnapshot.isNull()
2852 ? FALSE
2853 : mData->mCurrentStateModified;
2854
2855 return S_OK;
2856}
2857
2858HRESULT Machine::getSharedFolders(std::vector<ComPtr<ISharedFolder> > &aSharedFolders)
2859{
2860 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2861
2862 aSharedFolders.resize(mHWData->mSharedFolders.size());
2863 size_t i = 0;
2864 for (std::list<ComObjPtr<SharedFolder> >::const_iterator
2865 it = mHWData->mSharedFolders.begin();
2866 it != mHWData->mSharedFolders.end();
2867 ++it, ++i)
2868 aSharedFolders[i] = *it;
2869
2870 return S_OK;
2871}
2872
2873HRESULT Machine::getClipboardMode(ClipboardMode_T *aClipboardMode)
2874{
2875 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2876
2877 *aClipboardMode = mHWData->mClipboardMode;
2878
2879 return S_OK;
2880}
2881
2882HRESULT Machine::setClipboardMode(ClipboardMode_T aClipboardMode)
2883{
2884 HRESULT rc = S_OK;
2885
2886 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2887
2888 alock.release();
2889 rc = i_onClipboardModeChange(aClipboardMode);
2890 alock.acquire();
2891 if (FAILED(rc)) return rc;
2892
2893 i_setModified(IsModified_MachineData);
2894 mHWData.backup();
2895 mHWData->mClipboardMode = aClipboardMode;
2896
2897 /** Save settings if online - @todo why is this required? -- @bugref{6818} */
2898 if (Global::IsOnline(mData->mMachineState))
2899 i_saveSettings(NULL);
2900
2901 return S_OK;
2902}
2903
2904HRESULT Machine::getDnDMode(DnDMode_T *aDnDMode)
2905{
2906 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2907
2908 *aDnDMode = mHWData->mDnDMode;
2909
2910 return S_OK;
2911}
2912
2913HRESULT Machine::setDnDMode(DnDMode_T aDnDMode)
2914{
2915 HRESULT rc = S_OK;
2916
2917 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2918
2919 alock.release();
2920 rc = i_onDnDModeChange(aDnDMode);
2921
2922 alock.acquire();
2923 if (FAILED(rc)) return rc;
2924
2925 i_setModified(IsModified_MachineData);
2926 mHWData.backup();
2927 mHWData->mDnDMode = aDnDMode;
2928
2929 /** Save settings if online - @todo why is this required? -- @bugref{6818} */
2930 if (Global::IsOnline(mData->mMachineState))
2931 i_saveSettings(NULL);
2932
2933 return S_OK;
2934}
2935
2936HRESULT Machine::getStorageControllers(std::vector<ComPtr<IStorageController> > &aStorageControllers)
2937{
2938 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2939
2940 aStorageControllers.resize(mStorageControllers->size());
2941 size_t i = 0;
2942 for (StorageControllerList::const_iterator
2943 it = mStorageControllers->begin();
2944 it != mStorageControllers->end();
2945 ++it, ++i)
2946 aStorageControllers[i] = *it;
2947
2948 return S_OK;
2949}
2950
2951HRESULT Machine::getTeleporterEnabled(BOOL *aEnabled)
2952{
2953 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2954
2955 *aEnabled = mUserData->s.fTeleporterEnabled;
2956
2957 return S_OK;
2958}
2959
2960HRESULT Machine::setTeleporterEnabled(BOOL aTeleporterEnabled)
2961{
2962 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2963
2964 /* Only allow it to be set to true when PoweredOff or Aborted.
2965 (Clearing it is always permitted.) */
2966 if ( aTeleporterEnabled
2967 && mData->mRegistered
2968 && ( !i_isSessionMachine()
2969 || ( mData->mMachineState != MachineState_PoweredOff
2970 && mData->mMachineState != MachineState_Teleported
2971 && mData->mMachineState != MachineState_Aborted
2972 )
2973 )
2974 )
2975 return setError(VBOX_E_INVALID_VM_STATE,
2976 tr("The machine is not powered off (state is %s)"),
2977 Global::stringifyMachineState(mData->mMachineState));
2978
2979 i_setModified(IsModified_MachineData);
2980 mUserData.backup();
2981 mUserData->s.fTeleporterEnabled = !! aTeleporterEnabled;
2982
2983 return S_OK;
2984}
2985
2986HRESULT Machine::getTeleporterPort(ULONG *aTeleporterPort)
2987{
2988 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2989
2990 *aTeleporterPort = (ULONG)mUserData->s.uTeleporterPort;
2991
2992 return S_OK;
2993}
2994
2995HRESULT Machine::setTeleporterPort(ULONG aTeleporterPort)
2996{
2997 if (aTeleporterPort >= _64K)
2998 return setError(E_INVALIDARG, tr("Invalid port number %d"), aTeleporterPort);
2999
3000 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
3001
3002 HRESULT rc = i_checkStateDependency(MutableOrSavedStateDep);
3003 if (FAILED(rc)) return rc;
3004
3005 i_setModified(IsModified_MachineData);
3006 mUserData.backup();
3007 mUserData->s.uTeleporterPort = (uint32_t)aTeleporterPort;
3008
3009 return S_OK;
3010}
3011
3012HRESULT Machine::getTeleporterAddress(com::Utf8Str &aTeleporterAddress)
3013{
3014 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
3015
3016 aTeleporterAddress = mUserData->s.strTeleporterAddress;
3017
3018 return S_OK;
3019}
3020
3021HRESULT Machine::setTeleporterAddress(const com::Utf8Str &aTeleporterAddress)
3022{
3023 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
3024
3025 HRESULT rc = i_checkStateDependency(MutableOrSavedStateDep);
3026 if (FAILED(rc)) return rc;
3027
3028 i_setModified(IsModified_MachineData);
3029 mUserData.backup();
3030 mUserData->s.strTeleporterAddress = aTeleporterAddress;
3031
3032 return S_OK;
3033}
3034
3035HRESULT Machine::getTeleporterPassword(com::Utf8Str &aTeleporterPassword)
3036{
3037 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
3038 aTeleporterPassword = mUserData->s.strTeleporterPassword;
3039
3040 return S_OK;
3041}
3042
3043HRESULT Machine::setTeleporterPassword(const com::Utf8Str &aTeleporterPassword)
3044{
3045 /*
3046 * Hash the password first.
3047 */
3048 com::Utf8Str aT = aTeleporterPassword;
3049
3050 if (!aT.isEmpty())
3051 {
3052 if (VBoxIsPasswordHashed(&aT))
3053 return setError(E_INVALIDARG, tr("Cannot set an already hashed password, only plain text password please"));
3054 VBoxHashPassword(&aT);
3055 }
3056
3057 /*
3058 * Do the update.
3059 */
3060 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
3061 HRESULT hrc = i_checkStateDependency(MutableOrSavedStateDep);
3062 if (SUCCEEDED(hrc))
3063 {
3064 i_setModified(IsModified_MachineData);
3065 mUserData.backup();
3066 mUserData->s.strTeleporterPassword = aT;
3067 }
3068
3069 return hrc;
3070}
3071
3072HRESULT Machine::getFaultToleranceState(FaultToleranceState_T *aFaultToleranceState)
3073{
3074 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
3075
3076 *aFaultToleranceState = mUserData->s.enmFaultToleranceState;
3077 return S_OK;
3078}
3079
3080HRESULT Machine::setFaultToleranceState(FaultToleranceState_T aFaultToleranceState)
3081{
3082 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
3083
3084 /** @todo deal with running state change. */
3085 HRESULT rc = i_checkStateDependency(MutableOrSavedOrRunningStateDep);
3086 if (FAILED(rc)) return rc;
3087
3088 i_setModified(IsModified_MachineData);
3089 mUserData.backup();
3090 mUserData->s.enmFaultToleranceState = aFaultToleranceState;
3091 return S_OK;
3092}
3093
3094HRESULT Machine::getFaultToleranceAddress(com::Utf8Str &aFaultToleranceAddress)
3095{
3096 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
3097
3098 aFaultToleranceAddress = mUserData->s.strFaultToleranceAddress;
3099 return S_OK;
3100}
3101
3102HRESULT Machine::setFaultToleranceAddress(const com::Utf8Str &aFaultToleranceAddress)
3103{
3104 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
3105
3106 /** @todo deal with running state change. */
3107 HRESULT rc = i_checkStateDependency(MutableOrSavedStateDep);
3108 if (FAILED(rc)) return rc;
3109
3110 i_setModified(IsModified_MachineData);
3111 mUserData.backup();
3112 mUserData->s.strFaultToleranceAddress = aFaultToleranceAddress;
3113 return S_OK;
3114}
3115
3116HRESULT Machine::getFaultTolerancePort(ULONG *aFaultTolerancePort)
3117{
3118 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
3119
3120 *aFaultTolerancePort = mUserData->s.uFaultTolerancePort;
3121 return S_OK;
3122}
3123
3124HRESULT Machine::setFaultTolerancePort(ULONG aFaultTolerancePort)
3125{
3126 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
3127
3128 /** @todo deal with running state change. */
3129 HRESULT rc = i_checkStateDependency(MutableOrSavedStateDep);
3130 if (FAILED(rc)) return rc;
3131
3132 i_setModified(IsModified_MachineData);
3133 mUserData.backup();
3134 mUserData->s.uFaultTolerancePort = aFaultTolerancePort;
3135 return S_OK;
3136}
3137
3138HRESULT Machine::getFaultTolerancePassword(com::Utf8Str &aFaultTolerancePassword)
3139{
3140 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
3141
3142 aFaultTolerancePassword = mUserData->s.strFaultTolerancePassword;
3143
3144 return S_OK;
3145}
3146
3147HRESULT Machine::setFaultTolerancePassword(const com::Utf8Str &aFaultTolerancePassword)
3148{
3149 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
3150
3151 /** @todo deal with running state change. */
3152 HRESULT rc = i_checkStateDependency(MutableOrSavedStateDep);
3153 if (FAILED(rc)) return rc;
3154
3155 i_setModified(IsModified_MachineData);
3156 mUserData.backup();
3157 mUserData->s.strFaultTolerancePassword = aFaultTolerancePassword;
3158
3159 return S_OK;
3160}
3161
3162HRESULT Machine::getFaultToleranceSyncInterval(ULONG *aFaultToleranceSyncInterval)
3163{
3164 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
3165
3166 *aFaultToleranceSyncInterval = mUserData->s.uFaultToleranceInterval;
3167 return S_OK;
3168}
3169
3170HRESULT Machine::setFaultToleranceSyncInterval(ULONG aFaultToleranceSyncInterval)
3171{
3172 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
3173
3174 /** @todo deal with running state change. */
3175 HRESULT rc = i_checkStateDependency(MutableOrSavedStateDep);
3176 if (FAILED(rc)) return rc;
3177
3178 i_setModified(IsModified_MachineData);
3179 mUserData.backup();
3180 mUserData->s.uFaultToleranceInterval = aFaultToleranceSyncInterval;
3181 return S_OK;
3182}
3183
3184HRESULT Machine::getRTCUseUTC(BOOL *aRTCUseUTC)
3185{
3186 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
3187
3188 *aRTCUseUTC = mUserData->s.fRTCUseUTC;
3189
3190 return S_OK;
3191}
3192
3193HRESULT Machine::setRTCUseUTC(BOOL aRTCUseUTC)
3194{
3195 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
3196
3197 /* Only allow it to be set to true when PoweredOff or Aborted.
3198 (Clearing it is always permitted.) */
3199 if ( aRTCUseUTC
3200 && mData->mRegistered
3201 && ( !i_isSessionMachine()
3202 || ( mData->mMachineState != MachineState_PoweredOff
3203 && mData->mMachineState != MachineState_Teleported
3204 && mData->mMachineState != MachineState_Aborted
3205 )
3206 )
3207 )
3208 return setError(VBOX_E_INVALID_VM_STATE,
3209 tr("The machine is not powered off (state is %s)"),
3210 Global::stringifyMachineState(mData->mMachineState));
3211
3212 i_setModified(IsModified_MachineData);
3213 mUserData.backup();
3214 mUserData->s.fRTCUseUTC = !!aRTCUseUTC;
3215
3216 return S_OK;
3217}
3218
3219HRESULT Machine::getIOCacheEnabled(BOOL *aIOCacheEnabled)
3220{
3221 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
3222
3223 *aIOCacheEnabled = mHWData->mIOCacheEnabled;
3224
3225 return S_OK;
3226}
3227
3228HRESULT Machine::setIOCacheEnabled(BOOL aIOCacheEnabled)
3229{
3230 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
3231
3232 HRESULT rc = i_checkStateDependency(MutableStateDep);
3233 if (FAILED(rc)) return rc;
3234
3235 i_setModified(IsModified_MachineData);
3236 mHWData.backup();
3237 mHWData->mIOCacheEnabled = aIOCacheEnabled;
3238
3239 return S_OK;
3240}
3241
3242HRESULT Machine::getIOCacheSize(ULONG *aIOCacheSize)
3243{
3244 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
3245
3246 *aIOCacheSize = mHWData->mIOCacheSize;
3247
3248 return S_OK;
3249}
3250
3251HRESULT Machine::setIOCacheSize(ULONG aIOCacheSize)
3252{
3253 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
3254
3255 HRESULT rc = i_checkStateDependency(MutableStateDep);
3256 if (FAILED(rc)) return rc;
3257
3258 i_setModified(IsModified_MachineData);
3259 mHWData.backup();
3260 mHWData->mIOCacheSize = aIOCacheSize;
3261
3262 return S_OK;
3263}
3264
3265
3266/**
3267 * @note Locks objects!
3268 */
3269HRESULT Machine::lockMachine(const ComPtr<ISession> &aSession,
3270 LockType_T aLockType)
3271{
3272 /* check the session state */
3273 SessionState_T state;
3274 HRESULT rc = aSession->COMGETTER(State)(&state);
3275 if (FAILED(rc)) return rc;
3276
3277 if (state != SessionState_Unlocked)
3278 return setError(VBOX_E_INVALID_OBJECT_STATE,
3279 tr("The given session is busy"));
3280
3281 // get the client's IInternalSessionControl interface
3282 ComPtr<IInternalSessionControl> pSessionControl = aSession;
3283 ComAssertMsgRet(!!pSessionControl, ("No IInternalSessionControl interface"),
3284 E_INVALIDARG);
3285
3286 // session name (only used in some code paths)
3287 Utf8Str strSessionName;
3288
3289 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
3290
3291 if (!mData->mRegistered)
3292 return setError(E_UNEXPECTED,
3293 tr("The machine '%s' is not registered"),
3294 mUserData->s.strName.c_str());
3295
3296 LogFlowThisFunc(("mSession.mState=%s\n", Global::stringifySessionState(mData->mSession.mState)));
3297
3298 SessionState_T oldState = mData->mSession.mState;
3299 /* Hack: in case the session is closing and there is a progress object
3300 * which allows waiting for the session to be closed, take the opportunity
3301 * and do a limited wait (max. 1 second). This helps a lot when the system
3302 * is busy and thus session closing can take a little while. */
3303 if ( mData->mSession.mState == SessionState_Unlocking
3304 && mData->mSession.mProgress)
3305 {
3306 alock.release();
3307 mData->mSession.mProgress->WaitForCompletion(1000);
3308 alock.acquire();
3309 LogFlowThisFunc(("after waiting: mSession.mState=%s\n", Global::stringifySessionState(mData->mSession.mState)));
3310 }
3311
3312 // try again now
3313 if ( (mData->mSession.mState == SessionState_Locked) // machine is write-locked already
3314 // (i.e. session machine exists)
3315 && (aLockType == LockType_Shared) // caller wants a shared link to the
3316 // existing session that holds the write lock:
3317 )
3318 {
3319 // OK, share the session... we are now dealing with three processes:
3320 // 1) VBoxSVC (where this code runs);
3321 // 2) process C: the caller's client process (who wants a shared session);
3322 // 3) process W: the process which already holds the write lock on the machine (write-locking session)
3323
3324 // copy pointers to W (the write-locking session) before leaving lock (these must not be NULL)
3325 ComPtr<IInternalSessionControl> pSessionW = mData->mSession.mDirectControl;
3326 ComAssertRet(!pSessionW.isNull(), E_FAIL);
3327 ComObjPtr<SessionMachine> pSessionMachine = mData->mSession.mMachine;
3328 AssertReturn(!pSessionMachine.isNull(), E_FAIL);
3329
3330 /*
3331 * Release the lock before calling the client process. It's safe here
3332 * since the only thing to do after we get the lock again is to add
3333 * the remote control to the list (which doesn't directly influence
3334 * anything).
3335 */
3336 alock.release();
3337
3338 // get the console of the session holding the write lock (this is a remote call)
3339 ComPtr<IConsole> pConsoleW;
3340 if (mData->mSession.mLockType == LockType_VM)
3341 {
3342 LogFlowThisFunc(("Calling GetRemoteConsole()...\n"));
3343 rc = pSessionW->COMGETTER(RemoteConsole)(pConsoleW.asOutParam());
3344 LogFlowThisFunc(("GetRemoteConsole() returned %08X\n", rc));
3345 if (FAILED(rc))
3346 // the failure may occur w/o any error info (from RPC), so provide one
3347 return setError(VBOX_E_VM_ERROR,
3348 tr("Failed to get a console object from the direct session (%Rhrc)"), rc);
3349 ComAssertRet(!pConsoleW.isNull(), E_FAIL);
3350 }
3351
3352 // share the session machine and W's console with the caller's session
3353 LogFlowThisFunc(("Calling AssignRemoteMachine()...\n"));
3354 rc = pSessionControl->AssignRemoteMachine(pSessionMachine, pConsoleW);
3355 LogFlowThisFunc(("AssignRemoteMachine() returned %08X\n", rc));
3356
3357 if (FAILED(rc))
3358 // the failure may occur w/o any error info (from RPC), so provide one
3359 return setError(VBOX_E_VM_ERROR,
3360 tr("Failed to assign the machine to the session (%Rhrc)"), rc);
3361 alock.acquire();
3362
3363 // need to revalidate the state after acquiring the lock again
3364 if (mData->mSession.mState != SessionState_Locked)
3365 {
3366 pSessionControl->Uninitialize();
3367 return setError(VBOX_E_INVALID_SESSION_STATE,
3368 tr("The machine '%s' was unlocked unexpectedly while attempting to share its session"),
3369 mUserData->s.strName.c_str());
3370 }
3371
3372 // add the caller's session to the list
3373 mData->mSession.mRemoteControls.push_back(pSessionControl);
3374 }
3375 else if ( mData->mSession.mState == SessionState_Locked
3376 || mData->mSession.mState == SessionState_Unlocking
3377 )
3378 {
3379 // sharing not permitted, or machine still unlocking:
3380 return setError(VBOX_E_INVALID_OBJECT_STATE,
3381 tr("The machine '%s' is already locked for a session (or being unlocked)"),
3382 mUserData->s.strName.c_str());
3383 }
3384 else
3385 {
3386 // machine is not locked: then write-lock the machine (create the session machine)
3387
3388 // must not be busy
3389 AssertReturn(!Global::IsOnlineOrTransient(mData->mMachineState), E_FAIL);
3390
3391 // get the caller's session PID
3392 RTPROCESS pid = NIL_RTPROCESS;
3393 AssertCompile(sizeof(ULONG) == sizeof(RTPROCESS));
3394 pSessionControl->COMGETTER(PID)((ULONG*)&pid);
3395 Assert(pid != NIL_RTPROCESS);
3396
3397 bool fLaunchingVMProcess = (mData->mSession.mState == SessionState_Spawning);
3398
3399 if (fLaunchingVMProcess)
3400 {
3401 if (mData->mSession.mPID == NIL_RTPROCESS)
3402 {
3403 // two or more clients racing for a lock, the one which set the
3404 // session state to Spawning will win, the others will get an
3405 // error as we can't decide here if waiting a little would help
3406 // (only for shared locks this would avoid an error)
3407 return setError(VBOX_E_INVALID_OBJECT_STATE,
3408 tr("The machine '%s' already has a lock request pending"),
3409 mUserData->s.strName.c_str());
3410 }
3411
3412 // this machine is awaiting for a spawning session to be opened:
3413 // then the calling process must be the one that got started by
3414 // LaunchVMProcess()
3415
3416 LogFlowThisFunc(("mSession.mPID=%d(0x%x)\n", mData->mSession.mPID, mData->mSession.mPID));
3417 LogFlowThisFunc(("session.pid=%d(0x%x)\n", pid, pid));
3418
3419#if defined(VBOX_WITH_HARDENING) && defined(RT_OS_WINDOWS)
3420 /* Hardened windows builds spawns three processes when a VM is
3421 launched, the 3rd one is the one that will end up here. */
3422 RTPROCESS ppid;
3423 int rc = RTProcQueryParent(pid, &ppid);
3424 if (RT_SUCCESS(rc))
3425 rc = RTProcQueryParent(ppid, &ppid);
3426 if ( (RT_SUCCESS(rc) && mData->mSession.mPID == ppid)
3427 || rc == VERR_ACCESS_DENIED)
3428 {
3429 LogFlowThisFunc(("mSession.mPID => %d(%#x) - windows hardening stub\n", mData->mSession.mPID, pid));
3430 mData->mSession.mPID = pid;
3431 }
3432#endif
3433
3434 if (mData->mSession.mPID != pid)
3435 return setError(E_ACCESSDENIED,
3436 tr("An unexpected process (PID=0x%08X) has tried to lock the "
3437 "machine '%s', while only the process started by LaunchVMProcess (PID=0x%08X) is allowed"),
3438 pid, mUserData->s.strName.c_str(), mData->mSession.mPID);
3439 }
3440
3441 // create the mutable SessionMachine from the current machine
3442 ComObjPtr<SessionMachine> sessionMachine;
3443 sessionMachine.createObject();
3444 rc = sessionMachine->init(this);
3445 AssertComRC(rc);
3446
3447 /* NOTE: doing return from this function after this point but
3448 * before the end is forbidden since it may call SessionMachine::uninit()
3449 * (through the ComObjPtr's destructor) which requests the VirtualBox write
3450 * lock while still holding the Machine lock in alock so that a deadlock
3451 * is possible due to the wrong lock order. */
3452
3453 if (SUCCEEDED(rc))
3454 {
3455 /*
3456 * Set the session state to Spawning to protect against subsequent
3457 * attempts to open a session and to unregister the machine after
3458 * we release the lock.
3459 */
3460 SessionState_T origState = mData->mSession.mState;
3461 mData->mSession.mState = SessionState_Spawning;
3462
3463#ifndef VBOX_WITH_GENERIC_SESSION_WATCHER
3464 /* Get the client token ID to be passed to the client process */
3465 Utf8Str strTokenId;
3466 sessionMachine->i_getTokenId(strTokenId);
3467 Assert(!strTokenId.isEmpty());
3468#else /* VBOX_WITH_GENERIC_SESSION_WATCHER */
3469 /* Get the client token to be passed to the client process */
3470 ComPtr<IToken> pToken(sessionMachine->i_getToken());
3471 /* The token is now "owned" by pToken, fix refcount */
3472 if (!pToken.isNull())
3473 pToken->Release();
3474#endif /* VBOX_WITH_GENERIC_SESSION_WATCHER */
3475
3476 /*
3477 * Release the lock before calling the client process -- it will call
3478 * Machine/SessionMachine methods. Releasing the lock here is quite safe
3479 * because the state is Spawning, so that LaunchVMProcess() and
3480 * LockMachine() calls will fail. This method, called before we
3481 * acquire the lock again, will fail because of the wrong PID.
3482 *
3483 * Note that mData->mSession.mRemoteControls accessed outside
3484 * the lock may not be modified when state is Spawning, so it's safe.
3485 */
3486 alock.release();
3487
3488 LogFlowThisFunc(("Calling AssignMachine()...\n"));
3489#ifndef VBOX_WITH_GENERIC_SESSION_WATCHER
3490 rc = pSessionControl->AssignMachine(sessionMachine, aLockType, Bstr(strTokenId).raw());
3491#else /* VBOX_WITH_GENERIC_SESSION_WATCHER */
3492 rc = pSessionControl->AssignMachine(sessionMachine, aLockType, pToken);
3493 /* Now the token is owned by the client process. */
3494 pToken.setNull();
3495#endif /* VBOX_WITH_GENERIC_SESSION_WATCHER */
3496 LogFlowThisFunc(("AssignMachine() returned %08X\n", rc));
3497
3498 /* The failure may occur w/o any error info (from RPC), so provide one */
3499 if (FAILED(rc))
3500 setError(VBOX_E_VM_ERROR,
3501 tr("Failed to assign the machine to the session (%Rhrc)"), rc);
3502
3503 // get session name, either to remember or to compare against
3504 // the already known session name.
3505 {
3506 Bstr bstrSessionName;
3507 HRESULT rc2 = aSession->COMGETTER(Name)(bstrSessionName.asOutParam());
3508 if (SUCCEEDED(rc2))
3509 strSessionName = bstrSessionName;
3510 }
3511
3512 if ( SUCCEEDED(rc)
3513 && fLaunchingVMProcess
3514 )
3515 {
3516 /* complete the remote session initialization */
3517
3518 /* get the console from the direct session */
3519 ComPtr<IConsole> console;
3520 rc = pSessionControl->COMGETTER(RemoteConsole)(console.asOutParam());
3521 ComAssertComRC(rc);
3522
3523 if (SUCCEEDED(rc) && !console)
3524 {
3525 ComAssert(!!console);
3526 rc = E_FAIL;
3527 }
3528
3529 /* assign machine & console to the remote session */
3530 if (SUCCEEDED(rc))
3531 {
3532 /*
3533 * after LaunchVMProcess(), the first and the only
3534 * entry in remoteControls is that remote session
3535 */
3536 LogFlowThisFunc(("Calling AssignRemoteMachine()...\n"));
3537 rc = mData->mSession.mRemoteControls.front()->AssignRemoteMachine(sessionMachine, console);
3538 LogFlowThisFunc(("AssignRemoteMachine() returned %08X\n", rc));
3539
3540 /* The failure may occur w/o any error info (from RPC), so provide one */
3541 if (FAILED(rc))
3542 setError(VBOX_E_VM_ERROR,
3543 tr("Failed to assign the machine to the remote session (%Rhrc)"), rc);
3544 }
3545
3546 if (FAILED(rc))
3547 pSessionControl->Uninitialize();
3548 }
3549
3550 /* acquire the lock again */
3551 alock.acquire();
3552
3553 /* Restore the session state */
3554 mData->mSession.mState = origState;
3555 }
3556
3557 // finalize spawning anyway (this is why we don't return on errors above)
3558 if (fLaunchingVMProcess)
3559 {
3560 Assert(mData->mSession.mName == strSessionName);
3561 /* Note that the progress object is finalized later */
3562 /** @todo Consider checking mData->mSession.mProgress for cancellation
3563 * around here. */
3564
3565 /* We don't reset mSession.mPID here because it is necessary for
3566 * SessionMachine::uninit() to reap the child process later. */
3567
3568 if (FAILED(rc))
3569 {
3570 /* Close the remote session, remove the remote control from the list
3571 * and reset session state to Closed (@note keep the code in sync
3572 * with the relevant part in checkForSpawnFailure()). */
3573
3574 Assert(mData->mSession.mRemoteControls.size() == 1);
3575 if (mData->mSession.mRemoteControls.size() == 1)
3576 {
3577 ErrorInfoKeeper eik;
3578 mData->mSession.mRemoteControls.front()->Uninitialize();
3579 }
3580
3581 mData->mSession.mRemoteControls.clear();
3582 mData->mSession.mState = SessionState_Unlocked;
3583 }
3584 }
3585 else
3586 {
3587 /* memorize PID of the directly opened session */
3588 if (SUCCEEDED(rc))
3589 mData->mSession.mPID = pid;
3590 }
3591
3592 if (SUCCEEDED(rc))
3593 {
3594 mData->mSession.mLockType = aLockType;
3595 /* memorize the direct session control and cache IUnknown for it */
3596 mData->mSession.mDirectControl = pSessionControl;
3597 mData->mSession.mState = SessionState_Locked;
3598 if (!fLaunchingVMProcess)
3599 mData->mSession.mName = strSessionName;
3600 /* associate the SessionMachine with this Machine */
3601 mData->mSession.mMachine = sessionMachine;
3602
3603 /* request an IUnknown pointer early from the remote party for later
3604 * identity checks (it will be internally cached within mDirectControl
3605 * at least on XPCOM) */
3606 ComPtr<IUnknown> unk = mData->mSession.mDirectControl;
3607 NOREF(unk);
3608 }
3609
3610 /* Release the lock since SessionMachine::uninit() locks VirtualBox which
3611 * would break the lock order */
3612 alock.release();
3613
3614 /* uninitialize the created session machine on failure */
3615 if (FAILED(rc))
3616 sessionMachine->uninit();
3617 }
3618
3619 if (SUCCEEDED(rc))
3620 {
3621 /*
3622 * tell the client watcher thread to update the set of
3623 * machines that have open sessions
3624 */
3625 mParent->i_updateClientWatcher();
3626
3627 if (oldState != SessionState_Locked)
3628 /* fire an event */
3629 mParent->i_onSessionStateChange(i_getId(), SessionState_Locked);
3630 }
3631
3632 return rc;
3633}
3634
3635/**
3636 * @note Locks objects!
3637 */
3638HRESULT Machine::launchVMProcess(const ComPtr<ISession> &aSession,
3639 const com::Utf8Str &aName,
3640 const com::Utf8Str &aEnvironment,
3641 ComPtr<IProgress> &aProgress)
3642{
3643 Utf8Str strFrontend(aName);
3644 /* "emergencystop" doesn't need the session, so skip the checks/interface
3645 * retrieval. This code doesn't quite fit in here, but introducing a
3646 * special API method would be even more effort, and would require explicit
3647 * support by every API client. It's better to hide the feature a bit. */
3648 if (strFrontend != "emergencystop")
3649 CheckComArgNotNull(aSession);
3650
3651 HRESULT rc = S_OK;
3652 if (strFrontend.isEmpty())
3653 {
3654 Bstr bstrFrontend;
3655 rc = COMGETTER(DefaultFrontend)(bstrFrontend.asOutParam());
3656 if (FAILED(rc))
3657 return rc;
3658 strFrontend = bstrFrontend;
3659 if (strFrontend.isEmpty())
3660 {
3661 ComPtr<ISystemProperties> systemProperties;
3662 rc = mParent->COMGETTER(SystemProperties)(systemProperties.asOutParam());
3663 if (FAILED(rc))
3664 return rc;
3665 rc = systemProperties->COMGETTER(DefaultFrontend)(bstrFrontend.asOutParam());
3666 if (FAILED(rc))
3667 return rc;
3668 strFrontend = bstrFrontend;
3669 }
3670 /* paranoia - emergencystop is not a valid default */
3671 if (strFrontend == "emergencystop")
3672 strFrontend = Utf8Str::Empty;
3673 }
3674 /* default frontend: Qt GUI */
3675 if (strFrontend.isEmpty())
3676 strFrontend = "GUI/Qt";
3677
3678 if (strFrontend != "emergencystop")
3679 {
3680 /* check the session state */
3681 SessionState_T state;
3682 rc = aSession->COMGETTER(State)(&state);
3683 if (FAILED(rc))
3684 return rc;
3685
3686 if (state != SessionState_Unlocked)
3687 return setError(VBOX_E_INVALID_OBJECT_STATE,
3688 tr("The given session is busy"));
3689
3690 /* get the IInternalSessionControl interface */
3691 ComPtr<IInternalSessionControl> control(aSession);
3692 ComAssertMsgRet(!control.isNull(),
3693 ("No IInternalSessionControl interface"),
3694 E_INVALIDARG);
3695
3696 /* get the teleporter enable state for the progress object init. */
3697 BOOL fTeleporterEnabled;
3698 rc = COMGETTER(TeleporterEnabled)(&fTeleporterEnabled);
3699 if (FAILED(rc))
3700 return rc;
3701
3702 /* create a progress object */
3703 ComObjPtr<ProgressProxy> progress;
3704 progress.createObject();
3705 rc = progress->init(mParent,
3706 static_cast<IMachine*>(this),
3707 Bstr(tr("Starting VM")).raw(),
3708 TRUE /* aCancelable */,
3709 fTeleporterEnabled ? 20 : 10 /* uTotalOperationsWeight */,
3710 BstrFmt(tr("Creating process for virtual machine \"%s\" (%s)"),
3711 mUserData->s.strName.c_str(), strFrontend.c_str()).raw(),
3712 2 /* uFirstOperationWeight */,
3713 fTeleporterEnabled ? 3 : 1 /* cOtherProgressObjectOperations */);
3714
3715 if (SUCCEEDED(rc))
3716 {
3717 rc = i_launchVMProcess(control, strFrontend, aEnvironment, progress);
3718 if (SUCCEEDED(rc))
3719 {
3720 aProgress = progress;
3721
3722 /* signal the client watcher thread */
3723 mParent->i_updateClientWatcher();
3724
3725 /* fire an event */
3726 mParent->i_onSessionStateChange(i_getId(), SessionState_Spawning);
3727 }
3728 }
3729 }
3730 else
3731 {
3732 /* no progress object - either instant success or failure */
3733 aProgress = NULL;
3734
3735 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
3736
3737 if (mData->mSession.mState != SessionState_Locked)
3738 return setError(VBOX_E_INVALID_OBJECT_STATE,
3739 tr("The machine '%s' is not locked by a session"),
3740 mUserData->s.strName.c_str());
3741
3742 /* must have a VM process associated - do not kill normal API clients
3743 * with an open session */
3744 if (!Global::IsOnline(mData->mMachineState))
3745 return setError(VBOX_E_INVALID_OBJECT_STATE,
3746 tr("The machine '%s' does not have a VM process"),
3747 mUserData->s.strName.c_str());
3748
3749 /* forcibly terminate the VM process */
3750 if (mData->mSession.mPID != NIL_RTPROCESS)
3751 RTProcTerminate(mData->mSession.mPID);
3752
3753 /* signal the client watcher thread, as most likely the client has
3754 * been terminated */
3755 mParent->i_updateClientWatcher();
3756 }
3757
3758 return rc;
3759}
3760
3761HRESULT Machine::setBootOrder(ULONG aPosition, DeviceType_T aDevice)
3762{
3763 if (aPosition < 1 || aPosition > SchemaDefs::MaxBootPosition)
3764 return setError(E_INVALIDARG,
3765 tr("Invalid boot position: %lu (must be in range [1, %lu])"),
3766 aPosition, SchemaDefs::MaxBootPosition);
3767
3768 if (aDevice == DeviceType_USB)
3769 return setError(E_NOTIMPL,
3770 tr("Booting from USB device is currently not supported"));
3771
3772 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
3773
3774 HRESULT rc = i_checkStateDependency(MutableStateDep);
3775 if (FAILED(rc)) return rc;
3776
3777 i_setModified(IsModified_MachineData);
3778 mHWData.backup();
3779 mHWData->mBootOrder[aPosition - 1] = aDevice;
3780
3781 return S_OK;
3782}
3783
3784HRESULT Machine::getBootOrder(ULONG aPosition, DeviceType_T *aDevice)
3785{
3786 if (aPosition < 1 || aPosition > SchemaDefs::MaxBootPosition)
3787 return setError(E_INVALIDARG,
3788 tr("Invalid boot position: %lu (must be in range [1, %lu])"),
3789 aPosition, SchemaDefs::MaxBootPosition);
3790
3791 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
3792
3793 *aDevice = mHWData->mBootOrder[aPosition - 1];
3794
3795 return S_OK;
3796}
3797
3798HRESULT Machine::attachDevice(const com::Utf8Str &aName,
3799 LONG aControllerPort,
3800 LONG aDevice,
3801 DeviceType_T aType,
3802 const ComPtr<IMedium> &aMedium)
3803{
3804 IMedium *aM = aMedium;
3805 LogFlowThisFunc(("aControllerName=\"%s\" aControllerPort=%d aDevice=%d aType=%d aMedium=%p\n",
3806 aName.c_str(), aControllerPort, aDevice, aType, aM));
3807
3808 // request the host lock first, since might be calling Host methods for getting host drives;
3809 // next, protect the media tree all the while we're in here, as well as our member variables
3810 AutoMultiWriteLock2 alock(mParent->i_host(), this COMMA_LOCKVAL_SRC_POS);
3811 AutoWriteLock treeLock(&mParent->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
3812
3813 HRESULT rc = i_checkStateDependency(MutableOrRunningStateDep);
3814 if (FAILED(rc)) return rc;
3815
3816 /// @todo NEWMEDIA implicit machine registration
3817 if (!mData->mRegistered)
3818 return setError(VBOX_E_INVALID_OBJECT_STATE,
3819 tr("Cannot attach storage devices to an unregistered machine"));
3820
3821 AssertReturn(mData->mMachineState != MachineState_Saved, E_FAIL);
3822
3823 /* Check for an existing controller. */
3824 ComObjPtr<StorageController> ctl;
3825 rc = i_getStorageControllerByName(aName, ctl, true /* aSetError */);
3826 if (FAILED(rc)) return rc;
3827
3828 StorageControllerType_T ctrlType;
3829 rc = ctl->COMGETTER(ControllerType)(&ctrlType);
3830 if (FAILED(rc))
3831 return setError(E_FAIL,
3832 tr("Could not get type of controller '%s'"),
3833 aName.c_str());
3834
3835 bool fSilent = false;
3836 Utf8Str strReconfig;
3837
3838 /* Check whether the flag to allow silent storage attachment reconfiguration is set. */
3839 strReconfig = i_getExtraData(Utf8Str("VBoxInternal2/SilentReconfigureWhilePaused"));
3840 if ( mData->mMachineState == MachineState_Paused
3841 && strReconfig == "1")
3842 fSilent = true;
3843
3844 /* Check that the controller can do hotplugging if we detach the device while the VM is running. */
3845 bool fHotplug = false;
3846 if (!fSilent && Global::IsOnlineOrTransient(mData->mMachineState))
3847 fHotplug = true;
3848
3849 if (fHotplug && !i_isControllerHotplugCapable(ctrlType))
3850 return setError(VBOX_E_INVALID_VM_STATE,
3851 tr("Controller '%s' does not support hotplugging"),
3852 aName.c_str());
3853
3854 // check that the port and device are not out of range
3855 rc = ctl->i_checkPortAndDeviceValid(aControllerPort, aDevice);
3856 if (FAILED(rc)) return rc;
3857
3858 /* check if the device slot is already busy */
3859 MediumAttachment *pAttachTemp;
3860 if ((pAttachTemp = i_findAttachment(*mMediumAttachments.data(),
3861 Bstr(aName).raw(),
3862 aControllerPort,
3863 aDevice)))
3864 {
3865 Medium *pMedium = pAttachTemp->i_getMedium();
3866 if (pMedium)
3867 {
3868 AutoReadLock mediumLock(pMedium COMMA_LOCKVAL_SRC_POS);
3869 return setError(VBOX_E_OBJECT_IN_USE,
3870 tr("Medium '%s' is already attached to port %d, device %d of controller '%s' of this virtual machine"),
3871 pMedium->i_getLocationFull().c_str(),
3872 aControllerPort,
3873 aDevice,
3874 aName.c_str());
3875 }
3876 else
3877 return setError(VBOX_E_OBJECT_IN_USE,
3878 tr("Device is already attached to port %d, device %d of controller '%s' of this virtual machine"),
3879 aControllerPort, aDevice, aName.c_str());
3880 }
3881
3882 ComObjPtr<Medium> medium = static_cast<Medium*>(aM);
3883 if (aMedium && medium.isNull())
3884 return setError(E_INVALIDARG, "The given medium pointer is invalid");
3885
3886 AutoCaller mediumCaller(medium);
3887 if (FAILED(mediumCaller.rc())) return mediumCaller.rc();
3888
3889 AutoWriteLock mediumLock(medium COMMA_LOCKVAL_SRC_POS);
3890
3891 if ( (pAttachTemp = i_findAttachment(*mMediumAttachments.data(), medium))
3892 && !medium.isNull()
3893 )
3894 return setError(VBOX_E_OBJECT_IN_USE,
3895 tr("Medium '%s' is already attached to this virtual machine"),
3896 medium->i_getLocationFull().c_str());
3897
3898 if (!medium.isNull())
3899 {
3900 MediumType_T mtype = medium->i_getType();
3901 // MediumType_Readonly is also new, but only applies to DVDs and floppies.
3902 // For DVDs it's not written to the config file, so needs no global config
3903 // version bump. For floppies it's a new attribute "type", which is ignored
3904 // by older VirtualBox version, so needs no global config version bump either.
3905 // For hard disks this type is not accepted.
3906 if (mtype == MediumType_MultiAttach)
3907 {
3908 // This type is new with VirtualBox 4.0 and therefore requires settings
3909 // version 1.11 in the settings backend. Unfortunately it is not enough to do
3910 // the usual routine in MachineConfigFile::bumpSettingsVersionIfNeeded() for
3911 // two reasons: The medium type is a property of the media registry tree, which
3912 // can reside in the global config file (for pre-4.0 media); we would therefore
3913 // possibly need to bump the global config version. We don't want to do that though
3914 // because that might make downgrading to pre-4.0 impossible.
3915 // As a result, we can only use these two new types if the medium is NOT in the
3916 // global registry:
3917 const Guid &uuidGlobalRegistry = mParent->i_getGlobalRegistryId();
3918 if ( medium->i_isInRegistry(uuidGlobalRegistry)
3919 || !mData->pMachineConfigFile->canHaveOwnMediaRegistry()
3920 )
3921 return setError(VBOX_E_INVALID_OBJECT_STATE,
3922 tr("Cannot attach medium '%s': the media type 'MultiAttach' can only be attached "
3923 "to machines that were created with VirtualBox 4.0 or later"),
3924 medium->i_getLocationFull().c_str());
3925 }
3926 }
3927
3928 bool fIndirect = false;
3929 if (!medium.isNull())
3930 fIndirect = medium->i_isReadOnly();
3931 bool associate = true;
3932
3933 do
3934 {
3935 if ( aType == DeviceType_HardDisk
3936 && mMediumAttachments.isBackedUp())
3937 {
3938 const MediumAttachmentList &oldAtts = *mMediumAttachments.backedUpData();
3939
3940 /* check if the medium was attached to the VM before we started
3941 * changing attachments in which case the attachment just needs to
3942 * be restored */
3943 if ((pAttachTemp = i_findAttachment(oldAtts, medium)))
3944 {
3945 AssertReturn(!fIndirect, E_FAIL);
3946
3947 /* see if it's the same bus/channel/device */
3948 if (pAttachTemp->i_matches(Bstr(aName).raw(), aControllerPort, aDevice))
3949 {
3950 /* the simplest case: restore the whole attachment
3951 * and return, nothing else to do */
3952 mMediumAttachments->push_back(pAttachTemp);
3953
3954 /* Reattach the medium to the VM. */
3955 if (fHotplug || fSilent)
3956 {
3957 mediumLock.release();
3958 treeLock.release();
3959 alock.release();
3960
3961 MediumLockList *pMediumLockList(new MediumLockList());
3962
3963 rc = medium->i_createMediumLockList(true /* fFailIfInaccessible */,
3964 medium /* pToLockWrite */,
3965 false /* fMediumLockWriteAll */,
3966 NULL,
3967 *pMediumLockList);
3968 alock.acquire();
3969 if (FAILED(rc))
3970 delete pMediumLockList;
3971 else
3972 {
3973 mData->mSession.mLockedMedia.Unlock();
3974 alock.release();
3975 rc = mData->mSession.mLockedMedia.Insert(pAttachTemp, pMediumLockList);
3976 mData->mSession.mLockedMedia.Lock();
3977 alock.acquire();
3978 }
3979 alock.release();
3980
3981 if (SUCCEEDED(rc))
3982 {
3983 rc = i_onStorageDeviceChange(pAttachTemp, FALSE /* aRemove */, fSilent);
3984 /* Remove lock list in case of error. */
3985 if (FAILED(rc))
3986 {
3987 mData->mSession.mLockedMedia.Unlock();
3988 mData->mSession.mLockedMedia.Remove(pAttachTemp);
3989 mData->mSession.mLockedMedia.Lock();
3990 }
3991 }
3992 }
3993
3994 return S_OK;
3995 }
3996
3997 /* bus/channel/device differ; we need a new attachment object,
3998 * but don't try to associate it again */
3999 associate = false;
4000 break;
4001 }
4002 }
4003
4004 /* go further only if the attachment is to be indirect */
4005 if (!fIndirect)
4006 break;
4007
4008 /* perform the so called smart attachment logic for indirect
4009 * attachments. Note that smart attachment is only applicable to base
4010 * hard disks. */
4011
4012 if (medium->i_getParent().isNull())
4013 {
4014 /* first, investigate the backup copy of the current hard disk
4015 * attachments to make it possible to re-attach existing diffs to
4016 * another device slot w/o losing their contents */
4017 if (mMediumAttachments.isBackedUp())
4018 {
4019 const MediumAttachmentList &oldAtts = *mMediumAttachments.backedUpData();
4020
4021 MediumAttachmentList::const_iterator foundIt = oldAtts.end();
4022 uint32_t foundLevel = 0;
4023
4024 for (MediumAttachmentList::const_iterator
4025 it = oldAtts.begin();
4026 it != oldAtts.end();
4027 ++it)
4028 {
4029 uint32_t level = 0;
4030 MediumAttachment *pAttach = *it;
4031 ComObjPtr<Medium> pMedium = pAttach->i_getMedium();
4032 Assert(!pMedium.isNull() || pAttach->i_getType() != DeviceType_HardDisk);
4033 if (pMedium.isNull())
4034 continue;
4035
4036 if (pMedium->i_getBase(&level) == medium)
4037 {
4038 /* skip the hard disk if its currently attached (we
4039 * cannot attach the same hard disk twice) */
4040 if (i_findAttachment(*mMediumAttachments.data(),
4041 pMedium))
4042 continue;
4043
4044 /* matched device, channel and bus (i.e. attached to the
4045 * same place) will win and immediately stop the search;
4046 * otherwise the attachment that has the youngest
4047 * descendant of medium will be used
4048 */
4049 if (pAttach->i_matches(Bstr(aName).raw(), aControllerPort, aDevice))
4050 {
4051 /* the simplest case: restore the whole attachment
4052 * and return, nothing else to do */
4053 mMediumAttachments->push_back(*it);
4054
4055 /* Reattach the medium to the VM. */
4056 if (fHotplug || fSilent)
4057 {
4058 mediumLock.release();
4059 treeLock.release();
4060 alock.release();
4061
4062 MediumLockList *pMediumLockList(new MediumLockList());
4063
4064 rc = medium->i_createMediumLockList(true /* fFailIfInaccessible */,
4065 medium /* pToLockWrite */,
4066 false /* fMediumLockWriteAll */,
4067 NULL,
4068 *pMediumLockList);
4069 alock.acquire();
4070 if (FAILED(rc))
4071 delete pMediumLockList;
4072 else
4073 {
4074 mData->mSession.mLockedMedia.Unlock();
4075 alock.release();
4076 rc = mData->mSession.mLockedMedia.Insert(pAttachTemp, pMediumLockList);
4077 mData->mSession.mLockedMedia.Lock();
4078 alock.acquire();
4079 }
4080 alock.release();
4081
4082 if (SUCCEEDED(rc))
4083 {
4084 rc = i_onStorageDeviceChange(pAttachTemp, FALSE /* aRemove */, fSilent);
4085 /* Remove lock list in case of error. */
4086 if (FAILED(rc))
4087 {
4088 mData->mSession.mLockedMedia.Unlock();
4089 mData->mSession.mLockedMedia.Remove(pAttachTemp);
4090 mData->mSession.mLockedMedia.Lock();
4091 }
4092 }
4093 }
4094
4095 return S_OK;
4096 }
4097 else if ( foundIt == oldAtts.end()
4098 || level > foundLevel /* prefer younger */
4099 )
4100 {
4101 foundIt = it;
4102 foundLevel = level;
4103 }
4104 }
4105 }
4106
4107 if (foundIt != oldAtts.end())
4108 {
4109 /* use the previously attached hard disk */
4110 medium = (*foundIt)->i_getMedium();
4111 mediumCaller.attach(medium);
4112 if (FAILED(mediumCaller.rc())) return mediumCaller.rc();
4113 mediumLock.attach(medium);
4114 /* not implicit, doesn't require association with this VM */
4115 fIndirect = false;
4116 associate = false;
4117 /* go right to the MediumAttachment creation */
4118 break;
4119 }
4120 }
4121
4122 /* must give up the medium lock and medium tree lock as below we
4123 * go over snapshots, which needs a lock with higher lock order. */
4124 mediumLock.release();
4125 treeLock.release();
4126
4127 /* then, search through snapshots for the best diff in the given
4128 * hard disk's chain to base the new diff on */
4129
4130 ComObjPtr<Medium> base;
4131 ComObjPtr<Snapshot> snap = mData->mCurrentSnapshot;
4132 while (snap)
4133 {
4134 AutoReadLock snapLock(snap COMMA_LOCKVAL_SRC_POS);
4135
4136 const MediumAttachmentList &snapAtts = *snap->i_getSnapshotMachine()->mMediumAttachments.data();
4137
4138 MediumAttachment *pAttachFound = NULL;
4139 uint32_t foundLevel = 0;
4140
4141 for (MediumAttachmentList::const_iterator
4142 it = snapAtts.begin();
4143 it != snapAtts.end();
4144 ++it)
4145 {
4146 MediumAttachment *pAttach = *it;
4147 ComObjPtr<Medium> pMedium = pAttach->i_getMedium();
4148 Assert(!pMedium.isNull() || pAttach->i_getType() != DeviceType_HardDisk);
4149 if (pMedium.isNull())
4150 continue;
4151
4152 uint32_t level = 0;
4153 if (pMedium->i_getBase(&level) == medium)
4154 {
4155 /* matched device, channel and bus (i.e. attached to the
4156 * same place) will win and immediately stop the search;
4157 * otherwise the attachment that has the youngest
4158 * descendant of medium will be used
4159 */
4160 if ( pAttach->i_getDevice() == aDevice
4161 && pAttach->i_getPort() == aControllerPort
4162 && pAttach->i_getControllerName() == aName
4163 )
4164 {
4165 pAttachFound = pAttach;
4166 break;
4167 }
4168 else if ( !pAttachFound
4169 || level > foundLevel /* prefer younger */
4170 )
4171 {
4172 pAttachFound = pAttach;
4173 foundLevel = level;
4174 }
4175 }
4176 }
4177
4178 if (pAttachFound)
4179 {
4180 base = pAttachFound->i_getMedium();
4181 break;
4182 }
4183
4184 snap = snap->i_getParent();
4185 }
4186
4187 /* re-lock medium tree and the medium, as we need it below */
4188 treeLock.acquire();
4189 mediumLock.acquire();
4190
4191 /* found a suitable diff, use it as a base */
4192 if (!base.isNull())
4193 {
4194 medium = base;
4195 mediumCaller.attach(medium);
4196 if (FAILED(mediumCaller.rc())) return mediumCaller.rc();
4197 mediumLock.attach(medium);
4198 }
4199 }
4200
4201 Utf8Str strFullSnapshotFolder;
4202 i_calculateFullPath(mUserData->s.strSnapshotFolder, strFullSnapshotFolder);
4203
4204 ComObjPtr<Medium> diff;
4205 diff.createObject();
4206 // store this diff in the same registry as the parent
4207 Guid uuidRegistryParent;
4208 if (!medium->i_getFirstRegistryMachineId(uuidRegistryParent))
4209 {
4210 // parent image has no registry: this can happen if we're attaching a new immutable
4211 // image that has not yet been attached (medium then points to the base and we're
4212 // creating the diff image for the immutable, and the parent is not yet registered);
4213 // put the parent in the machine registry then
4214 mediumLock.release();
4215 treeLock.release();
4216 alock.release();
4217 i_addMediumToRegistry(medium);
4218 alock.acquire();
4219 treeLock.acquire();
4220 mediumLock.acquire();
4221 medium->i_getFirstRegistryMachineId(uuidRegistryParent);
4222 }
4223 rc = diff->init(mParent,
4224 medium->i_getPreferredDiffFormat(),
4225 strFullSnapshotFolder.append(RTPATH_SLASH_STR),
4226 uuidRegistryParent,
4227 DeviceType_HardDisk);
4228 if (FAILED(rc)) return rc;
4229
4230 /* Apply the normal locking logic to the entire chain. */
4231 MediumLockList *pMediumLockList(new MediumLockList());
4232 mediumLock.release();
4233 treeLock.release();
4234 rc = diff->i_createMediumLockList(true /* fFailIfInaccessible */,
4235 diff /* pToLockWrite */,
4236 false /* fMediumLockWriteAll */,
4237 medium,
4238 *pMediumLockList);
4239 treeLock.acquire();
4240 mediumLock.acquire();
4241 if (SUCCEEDED(rc))
4242 {
4243 mediumLock.release();
4244 treeLock.release();
4245 rc = pMediumLockList->Lock();
4246 treeLock.acquire();
4247 mediumLock.acquire();
4248 if (FAILED(rc))
4249 setError(rc,
4250 tr("Could not lock medium when creating diff '%s'"),
4251 diff->i_getLocationFull().c_str());
4252 else
4253 {
4254 /* will release the lock before the potentially lengthy
4255 * operation, so protect with the special state */
4256 MachineState_T oldState = mData->mMachineState;
4257 i_setMachineState(MachineState_SettingUp);
4258
4259 mediumLock.release();
4260 treeLock.release();
4261 alock.release();
4262
4263 rc = medium->i_createDiffStorage(diff,
4264 medium->i_getPreferredDiffVariant(),
4265 pMediumLockList,
4266 NULL /* aProgress */,
4267 true /* aWait */);
4268
4269 alock.acquire();
4270 treeLock.acquire();
4271 mediumLock.acquire();
4272
4273 i_setMachineState(oldState);
4274 }
4275 }
4276
4277 /* Unlock the media and free the associated memory. */
4278 delete pMediumLockList;
4279
4280 if (FAILED(rc)) return rc;
4281
4282 /* use the created diff for the actual attachment */
4283 medium = diff;
4284 mediumCaller.attach(medium);
4285 if (FAILED(mediumCaller.rc())) return mediumCaller.rc();
4286 mediumLock.attach(medium);
4287 }
4288 while (0);
4289
4290 ComObjPtr<MediumAttachment> attachment;
4291 attachment.createObject();
4292 rc = attachment->init(this,
4293 medium,
4294 aName,
4295 aControllerPort,
4296 aDevice,
4297 aType,
4298 fIndirect,
4299 false /* fPassthrough */,
4300 false /* fTempEject */,
4301 false /* fNonRotational */,
4302 false /* fDiscard */,
4303 fHotplug /* fHotPluggable */,
4304 Utf8Str::Empty);
4305 if (FAILED(rc)) return rc;
4306
4307 if (associate && !medium.isNull())
4308 {
4309 // as the last step, associate the medium to the VM
4310 rc = medium->i_addBackReference(mData->mUuid);
4311 // here we can fail because of Deleting, or being in process of creating a Diff
4312 if (FAILED(rc)) return rc;
4313
4314 mediumLock.release();
4315 treeLock.release();
4316 alock.release();
4317 i_addMediumToRegistry(medium);
4318 alock.acquire();
4319 treeLock.acquire();
4320 mediumLock.acquire();
4321 }
4322
4323 /* success: finally remember the attachment */
4324 i_setModified(IsModified_Storage);
4325 mMediumAttachments.backup();
4326 mMediumAttachments->push_back(attachment);
4327
4328 mediumLock.release();
4329 treeLock.release();
4330 alock.release();
4331
4332 if (fHotplug || fSilent)
4333 {
4334 if (!medium.isNull())
4335 {
4336 MediumLockList *pMediumLockList(new MediumLockList());
4337
4338 rc = medium->i_createMediumLockList(true /* fFailIfInaccessible */,
4339 medium /* pToLockWrite */,
4340 false /* fMediumLockWriteAll */,
4341 NULL,
4342 *pMediumLockList);
4343 alock.acquire();
4344 if (FAILED(rc))
4345 delete pMediumLockList;
4346 else
4347 {
4348 mData->mSession.mLockedMedia.Unlock();
4349 alock.release();
4350 rc = mData->mSession.mLockedMedia.Insert(attachment, pMediumLockList);
4351 mData->mSession.mLockedMedia.Lock();
4352 alock.acquire();
4353 }
4354 alock.release();
4355 }
4356
4357 if (SUCCEEDED(rc))
4358 {
4359 rc = i_onStorageDeviceChange(attachment, FALSE /* aRemove */, fSilent);
4360 /* Remove lock list in case of error. */
4361 if (FAILED(rc))
4362 {
4363 mData->mSession.mLockedMedia.Unlock();
4364 mData->mSession.mLockedMedia.Remove(attachment);
4365 mData->mSession.mLockedMedia.Lock();
4366 }
4367 }
4368 }
4369
4370 /* Save modified registries, but skip this machine as it's the caller's
4371 * job to save its settings like all other settings changes. */
4372 mParent->i_unmarkRegistryModified(i_getId());
4373 mParent->i_saveModifiedRegistries();
4374
4375 return rc;
4376}
4377
4378HRESULT Machine::detachDevice(const com::Utf8Str &aName, LONG aControllerPort,
4379 LONG aDevice)
4380{
4381 LogFlowThisFunc(("aControllerName=\"%s\" aControllerPort=%d aDevice=%d\n",
4382 aName.c_str(), aControllerPort, aDevice));
4383
4384 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
4385
4386 HRESULT rc = i_checkStateDependency(MutableOrRunningStateDep);
4387 if (FAILED(rc)) return rc;
4388
4389 AssertReturn(mData->mMachineState != MachineState_Saved, E_FAIL);
4390
4391 /* Check for an existing controller. */
4392 ComObjPtr<StorageController> ctl;
4393 rc = i_getStorageControllerByName(aName, ctl, true /* aSetError */);
4394 if (FAILED(rc)) return rc;
4395
4396 StorageControllerType_T ctrlType;
4397 rc = ctl->COMGETTER(ControllerType)(&ctrlType);
4398 if (FAILED(rc))
4399 return setError(E_FAIL,
4400 tr("Could not get type of controller '%s'"),
4401 aName.c_str());
4402
4403 bool fSilent = false;
4404 Utf8Str strReconfig;
4405
4406 /* Check whether the flag to allow silent storage attachment reconfiguration is set. */
4407 strReconfig = i_getExtraData(Utf8Str("VBoxInternal2/SilentReconfigureWhilePaused"));
4408 if ( mData->mMachineState == MachineState_Paused
4409 && strReconfig == "1")
4410 fSilent = true;
4411
4412 /* Check that the controller can do hotplugging if we detach the device while the VM is running. */
4413 bool fHotplug = false;
4414 if (!fSilent && Global::IsOnlineOrTransient(mData->mMachineState))
4415 fHotplug = true;
4416
4417 if (fHotplug && !i_isControllerHotplugCapable(ctrlType))
4418 return setError(VBOX_E_INVALID_VM_STATE,
4419 tr("Controller '%s' does not support hotplugging"),
4420 aName.c_str());
4421
4422 MediumAttachment *pAttach = i_findAttachment(*mMediumAttachments.data(),
4423 Bstr(aName).raw(),
4424 aControllerPort,
4425 aDevice);
4426 if (!pAttach)
4427 return setError(VBOX_E_OBJECT_NOT_FOUND,
4428 tr("No storage device attached to device slot %d on port %d of controller '%s'"),
4429 aDevice, aControllerPort, aName.c_str());
4430
4431 if (fHotplug && !pAttach->i_getHotPluggable())
4432 return setError(VBOX_E_NOT_SUPPORTED,
4433 tr("The device slot %d on port %d of controller '%s' does not support hotplugging"),
4434 aDevice, aControllerPort, aName.c_str());
4435
4436 /*
4437 * The VM has to detach the device before we delete any implicit diffs.
4438 * If this fails we can roll back without loosing data.
4439 */
4440 if (fHotplug || fSilent)
4441 {
4442 alock.release();
4443 rc = i_onStorageDeviceChange(pAttach, TRUE /* aRemove */, fSilent);
4444 alock.acquire();
4445 }
4446 if (FAILED(rc)) return rc;
4447
4448 /* If we are here everything went well and we can delete the implicit now. */
4449 rc = i_detachDevice(pAttach, alock, NULL /* pSnapshot */);
4450
4451 alock.release();
4452
4453 /* Save modified registries, but skip this machine as it's the caller's
4454 * job to save its settings like all other settings changes. */
4455 mParent->i_unmarkRegistryModified(i_getId());
4456 mParent->i_saveModifiedRegistries();
4457
4458 return rc;
4459}
4460
4461HRESULT Machine::passthroughDevice(const com::Utf8Str &aName, LONG aControllerPort,
4462 LONG aDevice, BOOL aPassthrough)
4463{
4464 LogFlowThisFunc(("aName=\"%s\" aControllerPort=%d aDevice=%d aPassthrough=%d\n",
4465 aName.c_str(), aControllerPort, aDevice, aPassthrough));
4466
4467 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
4468
4469 HRESULT rc = i_checkStateDependency(MutableStateDep);
4470 if (FAILED(rc)) return rc;
4471
4472 AssertReturn(mData->mMachineState != MachineState_Saved, E_FAIL);
4473
4474 if (Global::IsOnlineOrTransient(mData->mMachineState))
4475 return setError(VBOX_E_INVALID_VM_STATE,
4476 tr("Invalid machine state: %s"),
4477 Global::stringifyMachineState(mData->mMachineState));
4478
4479 MediumAttachment *pAttach = i_findAttachment(*mMediumAttachments.data(),
4480 Bstr(aName).raw(),
4481 aControllerPort,
4482 aDevice);
4483 if (!pAttach)
4484 return setError(VBOX_E_OBJECT_NOT_FOUND,
4485 tr("No storage device attached to device slot %d on port %d of controller '%s'"),
4486 aDevice, aControllerPort, aName.c_str());
4487
4488
4489 i_setModified(IsModified_Storage);
4490 mMediumAttachments.backup();
4491
4492 AutoWriteLock attLock(pAttach COMMA_LOCKVAL_SRC_POS);
4493
4494 if (pAttach->i_getType() != DeviceType_DVD)
4495 return setError(E_INVALIDARG,
4496 tr("Setting passthrough rejected as the device attached to device slot %d on port %d of controller '%s' is not a DVD"),
4497 aDevice, aControllerPort, aName.c_str());
4498 pAttach->i_updatePassthrough(!!aPassthrough);
4499
4500 return S_OK;
4501}
4502
4503HRESULT Machine::temporaryEjectDevice(const com::Utf8Str &aName, LONG aControllerPort,
4504 LONG aDevice, BOOL aTemporaryEject)
4505{
4506
4507 LogFlowThisFunc(("aName=\"%s\" aControllerPort=%d aDevice=%d aTemporaryEject=%d\n",
4508 aName.c_str(), aControllerPort, aDevice, aTemporaryEject));
4509
4510 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
4511
4512 HRESULT rc = i_checkStateDependency(MutableOrSavedOrRunningStateDep);
4513 if (FAILED(rc)) return rc;
4514
4515 MediumAttachment *pAttach = i_findAttachment(*mMediumAttachments.data(),
4516 Bstr(aName).raw(),
4517 aControllerPort,
4518 aDevice);
4519 if (!pAttach)
4520 return setError(VBOX_E_OBJECT_NOT_FOUND,
4521 tr("No storage device attached to device slot %d on port %d of controller '%s'"),
4522 aDevice, aControllerPort, aName.c_str());
4523
4524
4525 i_setModified(IsModified_Storage);
4526 mMediumAttachments.backup();
4527
4528 AutoWriteLock attLock(pAttach COMMA_LOCKVAL_SRC_POS);
4529
4530 if (pAttach->i_getType() != DeviceType_DVD)
4531 return setError(E_INVALIDARG,
4532 tr("Setting temporary eject flag rejected as the device attached to device slot %d on port %d of controller '%s' is not a DVD"),
4533 aDevice, aControllerPort, aName.c_str());
4534 pAttach->i_updateTempEject(!!aTemporaryEject);
4535
4536 return S_OK;
4537}
4538
4539HRESULT Machine::nonRotationalDevice(const com::Utf8Str &aName, LONG aControllerPort,
4540 LONG aDevice, BOOL aNonRotational)
4541{
4542
4543 LogFlowThisFunc(("aName=\"%s\" aControllerPort=%d aDevice=%d aNonRotational=%d\n",
4544 aName.c_str(), aControllerPort, aDevice, aNonRotational));
4545
4546 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
4547
4548 HRESULT rc = i_checkStateDependency(MutableStateDep);
4549 if (FAILED(rc)) return rc;
4550
4551 AssertReturn(mData->mMachineState != MachineState_Saved, E_FAIL);
4552
4553 if (Global::IsOnlineOrTransient(mData->mMachineState))
4554 return setError(VBOX_E_INVALID_VM_STATE,
4555 tr("Invalid machine state: %s"),
4556 Global::stringifyMachineState(mData->mMachineState));
4557
4558 MediumAttachment *pAttach = i_findAttachment(*mMediumAttachments.data(),
4559 Bstr(aName).raw(),
4560 aControllerPort,
4561 aDevice);
4562 if (!pAttach)
4563 return setError(VBOX_E_OBJECT_NOT_FOUND,
4564 tr("No storage device attached to device slot %d on port %d of controller '%s'"),
4565 aDevice, aControllerPort, aName.c_str());
4566
4567
4568 i_setModified(IsModified_Storage);
4569 mMediumAttachments.backup();
4570
4571 AutoWriteLock attLock(pAttach COMMA_LOCKVAL_SRC_POS);
4572
4573 if (pAttach->i_getType() != DeviceType_HardDisk)
4574 return setError(E_INVALIDARG,
4575 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"),
4576 aDevice, aControllerPort, aName.c_str());
4577 pAttach->i_updateNonRotational(!!aNonRotational);
4578
4579 return S_OK;
4580}
4581
4582HRESULT Machine::setAutoDiscardForDevice(const com::Utf8Str &aName, LONG aControllerPort,
4583 LONG aDevice, BOOL aDiscard)
4584{
4585
4586 LogFlowThisFunc(("aName=\"%s\" aControllerPort=%d aDevice=%d aDiscard=%d\n",
4587 aName.c_str(), aControllerPort, aDevice, aDiscard));
4588
4589 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
4590
4591 HRESULT rc = i_checkStateDependency(MutableStateDep);
4592 if (FAILED(rc)) return rc;
4593
4594 AssertReturn(mData->mMachineState != MachineState_Saved, E_FAIL);
4595
4596 if (Global::IsOnlineOrTransient(mData->mMachineState))
4597 return setError(VBOX_E_INVALID_VM_STATE,
4598 tr("Invalid machine state: %s"),
4599 Global::stringifyMachineState(mData->mMachineState));
4600
4601 MediumAttachment *pAttach = i_findAttachment(*mMediumAttachments.data(),
4602 Bstr(aName).raw(),
4603 aControllerPort,
4604 aDevice);
4605 if (!pAttach)
4606 return setError(VBOX_E_OBJECT_NOT_FOUND,
4607 tr("No storage device attached to device slot %d on port %d of controller '%s'"),
4608 aDevice, aControllerPort, aName.c_str());
4609
4610
4611 i_setModified(IsModified_Storage);
4612 mMediumAttachments.backup();
4613
4614 AutoWriteLock attLock(pAttach COMMA_LOCKVAL_SRC_POS);
4615
4616 if (pAttach->i_getType() != DeviceType_HardDisk)
4617 return setError(E_INVALIDARG,
4618 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"),
4619 aDevice, aControllerPort, aName.c_str());
4620 pAttach->i_updateDiscard(!!aDiscard);
4621
4622 return S_OK;
4623}
4624
4625HRESULT Machine::setHotPluggableForDevice(const com::Utf8Str &aName, LONG aControllerPort,
4626 LONG aDevice, BOOL aHotPluggable)
4627{
4628 LogFlowThisFunc(("aName=\"%s\" aControllerPort=%d aDevice=%d aHotPluggable=%d\n",
4629 aName.c_str(), aControllerPort, aDevice, aHotPluggable));
4630
4631 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
4632
4633 HRESULT rc = i_checkStateDependency(MutableStateDep);
4634 if (FAILED(rc)) return rc;
4635
4636 AssertReturn(mData->mMachineState != MachineState_Saved, E_FAIL);
4637
4638 if (Global::IsOnlineOrTransient(mData->mMachineState))
4639 return setError(VBOX_E_INVALID_VM_STATE,
4640 tr("Invalid machine state: %s"),
4641 Global::stringifyMachineState(mData->mMachineState));
4642
4643 MediumAttachment *pAttach = i_findAttachment(*mMediumAttachments.data(),
4644 Bstr(aName).raw(),
4645 aControllerPort,
4646 aDevice);
4647 if (!pAttach)
4648 return setError(VBOX_E_OBJECT_NOT_FOUND,
4649 tr("No storage device attached to device slot %d on port %d of controller '%s'"),
4650 aDevice, aControllerPort, aName.c_str());
4651
4652 /* Check for an existing controller. */
4653 ComObjPtr<StorageController> ctl;
4654 rc = i_getStorageControllerByName(aName, ctl, true /* aSetError */);
4655 if (FAILED(rc)) return rc;
4656
4657 StorageControllerType_T ctrlType;
4658 rc = ctl->COMGETTER(ControllerType)(&ctrlType);
4659 if (FAILED(rc))
4660 return setError(E_FAIL,
4661 tr("Could not get type of controller '%s'"),
4662 aName.c_str());
4663
4664 if (!i_isControllerHotplugCapable(ctrlType))
4665 return setError(VBOX_E_NOT_SUPPORTED,
4666 tr("Controller '%s' does not support changing the hot-pluggable device flag"),
4667 aName.c_str());
4668
4669 i_setModified(IsModified_Storage);
4670 mMediumAttachments.backup();
4671
4672 AutoWriteLock attLock(pAttach COMMA_LOCKVAL_SRC_POS);
4673
4674 if (pAttach->i_getType() == DeviceType_Floppy)
4675 return setError(E_INVALIDARG,
4676 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"),
4677 aDevice, aControllerPort, aName.c_str());
4678 pAttach->i_updateHotPluggable(!!aHotPluggable);
4679
4680 return S_OK;
4681}
4682
4683HRESULT Machine::setNoBandwidthGroupForDevice(const com::Utf8Str &aName, LONG aControllerPort,
4684 LONG aDevice)
4685{
4686 int rc = S_OK;
4687 LogFlowThisFunc(("aName=\"%s\" aControllerPort=%d aDevice=%d\n",
4688 aName.c_str(), aControllerPort, aDevice));
4689
4690 rc = setBandwidthGroupForDevice(aName, aControllerPort, aDevice, NULL);
4691
4692 return rc;
4693}
4694
4695HRESULT Machine::setBandwidthGroupForDevice(const com::Utf8Str &aName, LONG aControllerPort,
4696 LONG aDevice, const ComPtr<IBandwidthGroup> &aBandwidthGroup)
4697{
4698 LogFlowThisFunc(("aName=\"%s\" aControllerPort=%d aDevice=%d\n",
4699 aName.c_str(), aControllerPort, aDevice));
4700
4701 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
4702
4703 HRESULT rc = i_checkStateDependency(MutableOrSavedStateDep);
4704 if (FAILED(rc)) return rc;
4705
4706 if (Global::IsOnlineOrTransient(mData->mMachineState))
4707 return setError(VBOX_E_INVALID_VM_STATE,
4708 tr("Invalid machine state: %s"),
4709 Global::stringifyMachineState(mData->mMachineState));
4710
4711 MediumAttachment *pAttach = i_findAttachment(*mMediumAttachments.data(),
4712 Bstr(aName).raw(),
4713 aControllerPort,
4714 aDevice);
4715 if (!pAttach)
4716 return setError(VBOX_E_OBJECT_NOT_FOUND,
4717 tr("No storage device attached to device slot %d on port %d of controller '%s'"),
4718 aDevice, aControllerPort, aName.c_str());
4719
4720
4721 i_setModified(IsModified_Storage);
4722 mMediumAttachments.backup();
4723
4724 IBandwidthGroup *iB = aBandwidthGroup;
4725 ComObjPtr<BandwidthGroup> group = static_cast<BandwidthGroup*>(iB);
4726 if (aBandwidthGroup && group.isNull())
4727 return setError(E_INVALIDARG, "The given bandwidth group pointer is invalid");
4728
4729 AutoWriteLock attLock(pAttach COMMA_LOCKVAL_SRC_POS);
4730
4731 const Utf8Str strBandwidthGroupOld = pAttach->i_getBandwidthGroup();
4732 if (strBandwidthGroupOld.isNotEmpty())
4733 {
4734 /* Get the bandwidth group object and release it - this must not fail. */
4735 ComObjPtr<BandwidthGroup> pBandwidthGroupOld;
4736 rc = i_getBandwidthGroup(strBandwidthGroupOld, pBandwidthGroupOld, false);
4737 Assert(SUCCEEDED(rc));
4738
4739 pBandwidthGroupOld->i_release();
4740 pAttach->i_updateBandwidthGroup(Utf8Str::Empty);
4741 }
4742
4743 if (!group.isNull())
4744 {
4745 group->i_reference();
4746 pAttach->i_updateBandwidthGroup(group->i_getName());
4747 }
4748
4749 return S_OK;
4750}
4751
4752HRESULT Machine::attachDeviceWithoutMedium(const com::Utf8Str &aName,
4753 LONG aControllerPort,
4754 LONG aDevice,
4755 DeviceType_T aType)
4756{
4757 HRESULT rc = S_OK;
4758
4759 LogFlowThisFunc(("aName=\"%s\" aControllerPort=%d aDevice=%d aType=%d\n",
4760 aName.c_str(), aControllerPort, aDevice, aType));
4761
4762 rc = AttachDevice(Bstr(aName).raw(), aControllerPort, aDevice, aType, NULL);
4763
4764 return rc;
4765}
4766
4767
4768HRESULT Machine::unmountMedium(const com::Utf8Str &aName,
4769 LONG aControllerPort,
4770 LONG aDevice,
4771 BOOL aForce)
4772{
4773 int rc = S_OK;
4774 LogFlowThisFunc(("aName=\"%s\" aControllerPort=%d aDevice=%d",
4775 aName.c_str(), aControllerPort, aForce));
4776
4777 rc = mountMedium(aName, aControllerPort, aDevice, NULL, aForce);
4778
4779 return rc;
4780}
4781
4782HRESULT Machine::mountMedium(const com::Utf8Str &aName,
4783 LONG aControllerPort,
4784 LONG aDevice,
4785 const ComPtr<IMedium> &aMedium,
4786 BOOL aForce)
4787{
4788 int rc = S_OK;
4789 LogFlowThisFunc(("aName=\"%s\" aControllerPort=%d aDevice=%d aForce=%d\n",
4790 aName.c_str(), aControllerPort, aDevice, aForce));
4791
4792 // request the host lock first, since might be calling Host methods for getting host drives;
4793 // next, protect the media tree all the while we're in here, as well as our member variables
4794 AutoMultiWriteLock3 multiLock(mParent->i_host()->lockHandle(),
4795 this->lockHandle(),
4796 &mParent->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
4797
4798 ComObjPtr<MediumAttachment> pAttach = i_findAttachment(*mMediumAttachments.data(),
4799 Bstr(aName).raw(),
4800 aControllerPort,
4801 aDevice);
4802 if (pAttach.isNull())
4803 return setError(VBOX_E_OBJECT_NOT_FOUND,
4804 tr("No drive attached to device slot %d on port %d of controller '%s'"),
4805 aDevice, aControllerPort, aName.c_str());
4806
4807 /* Remember previously mounted medium. The medium before taking the
4808 * backup is not necessarily the same thing. */
4809 ComObjPtr<Medium> oldmedium;
4810 oldmedium = pAttach->i_getMedium();
4811
4812 IMedium *iM = aMedium;
4813 ComObjPtr<Medium> pMedium = static_cast<Medium*>(iM);
4814 if (aMedium && pMedium.isNull())
4815 return setError(E_INVALIDARG, "The given medium pointer is invalid");
4816
4817 AutoCaller mediumCaller(pMedium);
4818 if (FAILED(mediumCaller.rc())) return mediumCaller.rc();
4819
4820 AutoWriteLock mediumLock(pMedium COMMA_LOCKVAL_SRC_POS);
4821 if (pMedium)
4822 {
4823 DeviceType_T mediumType = pAttach->i_getType();
4824 switch (mediumType)
4825 {
4826 case DeviceType_DVD:
4827 case DeviceType_Floppy:
4828 break;
4829
4830 default:
4831 return setError(VBOX_E_INVALID_OBJECT_STATE,
4832 tr("The device at port %d, device %d of controller '%s' of this virtual machine is not removeable"),
4833 aControllerPort,
4834 aDevice,
4835 aName.c_str());
4836 }
4837 }
4838
4839 i_setModified(IsModified_Storage);
4840 mMediumAttachments.backup();
4841
4842 {
4843 // The backup operation makes the pAttach reference point to the
4844 // old settings. Re-get the correct reference.
4845 pAttach = i_findAttachment(*mMediumAttachments.data(),
4846 Bstr(aName).raw(),
4847 aControllerPort,
4848 aDevice);
4849 if (!oldmedium.isNull())
4850 oldmedium->i_removeBackReference(mData->mUuid);
4851 if (!pMedium.isNull())
4852 {
4853 pMedium->i_addBackReference(mData->mUuid);
4854
4855 mediumLock.release();
4856 multiLock.release();
4857 i_addMediumToRegistry(pMedium);
4858 multiLock.acquire();
4859 mediumLock.acquire();
4860 }
4861
4862 AutoWriteLock attLock(pAttach COMMA_LOCKVAL_SRC_POS);
4863 pAttach->i_updateMedium(pMedium);
4864 }
4865
4866 i_setModified(IsModified_Storage);
4867
4868 mediumLock.release();
4869 multiLock.release();
4870 rc = i_onMediumChange(pAttach, aForce);
4871 multiLock.acquire();
4872 mediumLock.acquire();
4873
4874 /* On error roll back this change only. */
4875 if (FAILED(rc))
4876 {
4877 if (!pMedium.isNull())
4878 pMedium->i_removeBackReference(mData->mUuid);
4879 pAttach = i_findAttachment(*mMediumAttachments.data(),
4880 Bstr(aName).raw(),
4881 aControllerPort,
4882 aDevice);
4883 /* If the attachment is gone in the meantime, bail out. */
4884 if (pAttach.isNull())
4885 return rc;
4886 AutoWriteLock attLock(pAttach COMMA_LOCKVAL_SRC_POS);
4887 if (!oldmedium.isNull())
4888 oldmedium->i_addBackReference(mData->mUuid);
4889 pAttach->i_updateMedium(oldmedium);
4890 }
4891
4892 mediumLock.release();
4893 multiLock.release();
4894
4895 /* Save modified registries, but skip this machine as it's the caller's
4896 * job to save its settings like all other settings changes. */
4897 mParent->i_unmarkRegistryModified(i_getId());
4898 mParent->i_saveModifiedRegistries();
4899
4900 return rc;
4901}
4902HRESULT Machine::getMedium(const com::Utf8Str &aName,
4903 LONG aControllerPort,
4904 LONG aDevice,
4905 ComPtr<IMedium> &aMedium)
4906{
4907 LogFlowThisFunc(("aName=\"%s\" aControllerPort=%d aDevice=%d\n",
4908 aName.c_str(), aControllerPort, aDevice));
4909
4910 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
4911
4912 aMedium = NULL;
4913
4914 ComObjPtr<MediumAttachment> pAttach = i_findAttachment(*mMediumAttachments.data(),
4915 Bstr(aName).raw(),
4916 aControllerPort,
4917 aDevice);
4918 if (pAttach.isNull())
4919 return setError(VBOX_E_OBJECT_NOT_FOUND,
4920 tr("No storage device attached to device slot %d on port %d of controller '%s'"),
4921 aDevice, aControllerPort, aName.c_str());
4922
4923 aMedium = pAttach->i_getMedium();
4924
4925 return S_OK;
4926}
4927
4928HRESULT Machine::getSerialPort(ULONG aSlot, ComPtr<ISerialPort> &aPort)
4929{
4930
4931 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
4932
4933 mSerialPorts[aSlot].queryInterfaceTo(aPort.asOutParam());
4934
4935 return S_OK;
4936}
4937
4938HRESULT Machine::getParallelPort(ULONG aSlot, ComPtr<IParallelPort> &aPort)
4939{
4940 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
4941
4942 mParallelPorts[aSlot].queryInterfaceTo(aPort.asOutParam());
4943
4944 return S_OK;
4945}
4946
4947HRESULT Machine::getNetworkAdapter(ULONG aSlot, ComPtr<INetworkAdapter> &aAdapter)
4948{
4949 /* Do not assert if slot is out of range, just return the advertised
4950 status. testdriver/vbox.py triggers this in logVmInfo. */
4951 if (aSlot >= mNetworkAdapters.size())
4952 return setError(E_INVALIDARG,
4953 tr("No network adapter in slot %RU32 (total %RU32 adapters)"),
4954 aSlot, mNetworkAdapters.size());
4955
4956 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
4957
4958 mNetworkAdapters[aSlot].queryInterfaceTo(aAdapter.asOutParam());
4959
4960 return S_OK;
4961}
4962
4963HRESULT Machine::getExtraDataKeys(std::vector<com::Utf8Str> &aKeys)
4964{
4965 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
4966
4967 aKeys.resize(mData->pMachineConfigFile->mapExtraDataItems.size());
4968 size_t i = 0;
4969 for (settings::StringsMap::const_iterator
4970 it = mData->pMachineConfigFile->mapExtraDataItems.begin();
4971 it != mData->pMachineConfigFile->mapExtraDataItems.end();
4972 ++it, ++i)
4973 aKeys[i] = it->first;
4974
4975 return S_OK;
4976}
4977
4978 /**
4979 * @note Locks this object for reading.
4980 */
4981HRESULT Machine::getExtraData(const com::Utf8Str &aKey,
4982 com::Utf8Str &aValue)
4983{
4984 /* start with nothing found */
4985 aValue = "";
4986
4987 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
4988
4989 settings::StringsMap::const_iterator it = mData->pMachineConfigFile->mapExtraDataItems.find(aKey);
4990 if (it != mData->pMachineConfigFile->mapExtraDataItems.end())
4991 // found:
4992 aValue = it->second; // source is a Utf8Str
4993
4994 /* return the result to caller (may be empty) */
4995 return S_OK;
4996}
4997
4998 /**
4999 * @note Locks mParent for writing + this object for writing.
5000 */
5001HRESULT Machine::setExtraData(const com::Utf8Str &aKey, const com::Utf8Str &aValue)
5002{
5003 Utf8Str strOldValue; // empty
5004
5005 // locking note: we only hold the read lock briefly to look up the old value,
5006 // then release it and call the onExtraCanChange callbacks. There is a small
5007 // chance of a race insofar as the callback might be called twice if two callers
5008 // change the same key at the same time, but that's a much better solution
5009 // than the deadlock we had here before. The actual changing of the extradata
5010 // is then performed under the write lock and race-free.
5011
5012 // look up the old value first; if nothing has changed then we need not do anything
5013 {
5014 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS); // hold read lock only while looking up
5015
5016 // For snapshots don't even think about allowing changes, extradata
5017 // is global for a machine, so there is nothing snapshot specific.
5018 if (i_isSnapshotMachine())
5019 return setError(VBOX_E_INVALID_VM_STATE,
5020 tr("Cannot set extradata for a snapshot"));
5021
5022 // check if the right IMachine instance is used
5023 if (mData->mRegistered && !i_isSessionMachine())
5024 return setError(VBOX_E_INVALID_VM_STATE,
5025 tr("Cannot set extradata for an immutable machine"));
5026
5027 settings::StringsMap::const_iterator it = mData->pMachineConfigFile->mapExtraDataItems.find(aKey);
5028 if (it != mData->pMachineConfigFile->mapExtraDataItems.end())
5029 strOldValue = it->second;
5030 }
5031
5032 bool fChanged;
5033 if ((fChanged = (strOldValue != aValue)))
5034 {
5035 // ask for permission from all listeners outside the locks;
5036 // i_onExtraDataCanChange() only briefly requests the VirtualBox
5037 // lock to copy the list of callbacks to invoke
5038 Bstr error;
5039 Bstr bstrValue(aValue);
5040
5041 if (!mParent->i_onExtraDataCanChange(mData->mUuid, Bstr(aKey).raw(), bstrValue.raw(), error))
5042 {
5043 const char *sep = error.isEmpty() ? "" : ": ";
5044 CBSTR err = error.raw();
5045 Log1WarningFunc(("Someone vetoed! Change refused%s%ls\n", sep, err));
5046 return setError(E_ACCESSDENIED,
5047 tr("Could not set extra data because someone refused the requested change of '%s' to '%s'%s%ls"),
5048 aKey.c_str(),
5049 aValue.c_str(),
5050 sep,
5051 err);
5052 }
5053
5054 // data is changing and change not vetoed: then write it out under the lock
5055 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
5056
5057 if (aValue.isEmpty())
5058 mData->pMachineConfigFile->mapExtraDataItems.erase(aKey);
5059 else
5060 mData->pMachineConfigFile->mapExtraDataItems[aKey] = aValue;
5061 // creates a new key if needed
5062
5063 bool fNeedsGlobalSaveSettings = false;
5064 // This saving of settings is tricky: there is no "old state" for the
5065 // extradata items at all (unlike all other settings), so the old/new
5066 // settings comparison would give a wrong result!
5067 i_saveSettings(&fNeedsGlobalSaveSettings, SaveS_Force);
5068
5069 if (fNeedsGlobalSaveSettings)
5070 {
5071 // save the global settings; for that we should hold only the VirtualBox lock
5072 alock.release();
5073 AutoWriteLock vboxlock(mParent COMMA_LOCKVAL_SRC_POS);
5074 mParent->i_saveSettings();
5075 }
5076 }
5077
5078 // fire notification outside the lock
5079 if (fChanged)
5080 mParent->i_onExtraDataChange(mData->mUuid, Bstr(aKey).raw(), Bstr(aValue).raw());
5081
5082 return S_OK;
5083}
5084
5085HRESULT Machine::setSettingsFilePath(const com::Utf8Str &aSettingsFilePath, ComPtr<IProgress> &aProgress)
5086{
5087 aProgress = NULL;
5088 NOREF(aSettingsFilePath);
5089 ReturnComNotImplemented();
5090}
5091
5092HRESULT Machine::saveSettings()
5093{
5094 AutoWriteLock mlock(this COMMA_LOCKVAL_SRC_POS);
5095
5096 HRESULT rc = i_checkStateDependency(MutableOrSavedOrRunningStateDep);
5097 if (FAILED(rc)) return rc;
5098
5099 /* the settings file path may never be null */
5100 ComAssertRet(!mData->m_strConfigFileFull.isEmpty(), E_FAIL);
5101
5102 /* save all VM data excluding snapshots */
5103 bool fNeedsGlobalSaveSettings = false;
5104 rc = i_saveSettings(&fNeedsGlobalSaveSettings);
5105 mlock.release();
5106
5107 if (SUCCEEDED(rc) && fNeedsGlobalSaveSettings)
5108 {
5109 // save the global settings; for that we should hold only the VirtualBox lock
5110 AutoWriteLock vlock(mParent COMMA_LOCKVAL_SRC_POS);
5111 rc = mParent->i_saveSettings();
5112 }
5113
5114 return rc;
5115}
5116
5117
5118HRESULT Machine::discardSettings()
5119{
5120 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
5121
5122 HRESULT rc = i_checkStateDependency(MutableOrSavedOrRunningStateDep);
5123 if (FAILED(rc)) return rc;
5124
5125 /*
5126 * during this rollback, the session will be notified if data has
5127 * been actually changed
5128 */
5129 i_rollback(true /* aNotify */);
5130
5131 return S_OK;
5132}
5133
5134/** @note Locks objects! */
5135HRESULT Machine::unregister(AutoCaller &autoCaller,
5136 CleanupMode_T aCleanupMode,
5137 std::vector<ComPtr<IMedium> > &aMedia)
5138{
5139 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
5140
5141 Guid id(i_getId());
5142
5143 if (mData->mSession.mState != SessionState_Unlocked)
5144 return setError(VBOX_E_INVALID_OBJECT_STATE,
5145 tr("Cannot unregister the machine '%s' while it is locked"),
5146 mUserData->s.strName.c_str());
5147
5148 // wait for state dependents to drop to zero
5149 i_ensureNoStateDependencies();
5150
5151 if (!mData->mAccessible)
5152 {
5153 // inaccessible maschines can only be unregistered; uninitialize ourselves
5154 // here because currently there may be no unregistered that are inaccessible
5155 // (this state combination is not supported). Note releasing the caller and
5156 // leaving the lock before calling uninit()
5157 alock.release();
5158 autoCaller.release();
5159
5160 uninit();
5161
5162 mParent->i_unregisterMachine(this, id);
5163 // calls VirtualBox::i_saveSettings()
5164
5165 return S_OK;
5166 }
5167
5168 HRESULT rc = S_OK;
5169
5170 /// @todo r=klaus this is stupid... why is the saved state always deleted?
5171 // discard saved state
5172 if (mData->mMachineState == MachineState_Saved)
5173 {
5174 // add the saved state file to the list of files the caller should delete
5175 Assert(!mSSData->strStateFilePath.isEmpty());
5176 mData->llFilesToDelete.push_back(mSSData->strStateFilePath);
5177
5178 mSSData->strStateFilePath.setNull();
5179
5180 // unconditionally set the machine state to powered off, we now
5181 // know no session has locked the machine
5182 mData->mMachineState = MachineState_PoweredOff;
5183 }
5184
5185 size_t cSnapshots = 0;
5186 if (mData->mFirstSnapshot)
5187 cSnapshots = mData->mFirstSnapshot->i_getAllChildrenCount() + 1;
5188 if (cSnapshots && aCleanupMode == CleanupMode_UnregisterOnly)
5189 // fail now before we start detaching media
5190 return setError(VBOX_E_INVALID_OBJECT_STATE,
5191 tr("Cannot unregister the machine '%s' because it has %d snapshots"),
5192 mUserData->s.strName.c_str(), cSnapshots);
5193
5194 // This list collects the medium objects from all medium attachments
5195 // which we will detach from the machine and its snapshots, in a specific
5196 // order which allows for closing all media without getting "media in use"
5197 // errors, simply by going through the list from the front to the back:
5198 // 1) first media from machine attachments (these have the "leaf" attachments with snapshots
5199 // and must be closed before the parent media from the snapshots, or closing the parents
5200 // will fail because they still have children);
5201 // 2) media from the youngest snapshots followed by those from the parent snapshots until
5202 // the root ("first") snapshot of the machine.
5203 MediaList llMedia;
5204
5205 if ( !mMediumAttachments.isNull() // can be NULL if machine is inaccessible
5206 && mMediumAttachments->size()
5207 )
5208 {
5209 // we have media attachments: detach them all and add the Medium objects to our list
5210 if (aCleanupMode != CleanupMode_UnregisterOnly)
5211 i_detachAllMedia(alock, NULL /* pSnapshot */, aCleanupMode, llMedia);
5212 else
5213 return setError(VBOX_E_INVALID_OBJECT_STATE,
5214 tr("Cannot unregister the machine '%s' because it has %d media attachments"),
5215 mUserData->s.strName.c_str(), mMediumAttachments->size());
5216 }
5217
5218 if (cSnapshots)
5219 {
5220 // add the media from the medium attachments of the snapshots to llMedia
5221 // as well, after the "main" machine media; Snapshot::uninitRecursively()
5222 // calls Machine::detachAllMedia() for the snapshot machine, recursing
5223 // into the children first
5224
5225 // Snapshot::beginDeletingSnapshot() asserts if the machine state is not this
5226 MachineState_T oldState = mData->mMachineState;
5227 mData->mMachineState = MachineState_DeletingSnapshot;
5228
5229 // make a copy of the first snapshot so the refcount does not drop to 0
5230 // in beginDeletingSnapshot, which sets pFirstSnapshot to 0 (that hangs
5231 // because of the AutoCaller voodoo)
5232 ComObjPtr<Snapshot> pFirstSnapshot = mData->mFirstSnapshot;
5233
5234 // GO!
5235 pFirstSnapshot->i_uninitRecursively(alock, aCleanupMode, llMedia, mData->llFilesToDelete);
5236
5237 mData->mMachineState = oldState;
5238 }
5239
5240 if (FAILED(rc))
5241 {
5242 i_rollbackMedia();
5243 return rc;
5244 }
5245
5246 // commit all the media changes made above
5247 i_commitMedia();
5248
5249 mData->mRegistered = false;
5250
5251 // machine lock no longer needed
5252 alock.release();
5253
5254 // return media to caller
5255 aMedia.resize(llMedia.size());
5256 size_t i = 0;
5257 for (MediaList::const_iterator
5258 it = llMedia.begin();
5259 it != llMedia.end();
5260 ++it, ++i)
5261 (*it).queryInterfaceTo(aMedia[i].asOutParam());
5262
5263 mParent->i_unregisterMachine(this, id);
5264 // calls VirtualBox::i_saveSettings() and VirtualBox::saveModifiedRegistries()
5265
5266 return S_OK;
5267}
5268
5269/**
5270 * Task record for deleting a machine config.
5271 */
5272class Machine::DeleteConfigTask
5273 : public Machine::Task
5274{
5275public:
5276 DeleteConfigTask(Machine *m,
5277 Progress *p,
5278 const Utf8Str &t,
5279 const RTCList<ComPtr<IMedium> > &llMediums,
5280 const StringsList &llFilesToDelete)
5281 : Task(m, p, t),
5282 m_llMediums(llMediums),
5283 m_llFilesToDelete(llFilesToDelete)
5284 {}
5285
5286private:
5287 void handler()
5288 {
5289 try
5290 {
5291 m_pMachine->i_deleteConfigHandler(*this);
5292 }
5293 catch (...)
5294 {
5295 LogRel(("Some exception in the function Machine::i_deleteConfigHandler()\n"));
5296 }
5297 }
5298
5299 RTCList<ComPtr<IMedium> > m_llMediums;
5300 StringsList m_llFilesToDelete;
5301
5302 friend void Machine::i_deleteConfigHandler(DeleteConfigTask &task);
5303};
5304
5305/**
5306 * Task thread implementation for SessionMachine::DeleteConfig(), called from
5307 * SessionMachine::taskHandler().
5308 *
5309 * @note Locks this object for writing.
5310 *
5311 * @param task
5312 * @return
5313 */
5314void Machine::i_deleteConfigHandler(DeleteConfigTask &task)
5315{
5316 LogFlowThisFuncEnter();
5317
5318 AutoCaller autoCaller(this);
5319 LogFlowThisFunc(("state=%d\n", getObjectState().getState()));
5320 if (FAILED(autoCaller.rc()))
5321 {
5322 /* we might have been uninitialized because the session was accidentally
5323 * closed by the client, so don't assert */
5324 HRESULT rc = setError(E_FAIL,
5325 tr("The session has been accidentally closed"));
5326 task.m_pProgress->i_notifyComplete(rc);
5327 LogFlowThisFuncLeave();
5328 return;
5329 }
5330
5331 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
5332
5333 HRESULT rc = S_OK;
5334
5335 try
5336 {
5337 ULONG uLogHistoryCount = 3;
5338 ComPtr<ISystemProperties> systemProperties;
5339 rc = mParent->COMGETTER(SystemProperties)(systemProperties.asOutParam());
5340 if (FAILED(rc)) throw rc;
5341
5342 if (!systemProperties.isNull())
5343 {
5344 rc = systemProperties->COMGETTER(LogHistoryCount)(&uLogHistoryCount);
5345 if (FAILED(rc)) throw rc;
5346 }
5347
5348 MachineState_T oldState = mData->mMachineState;
5349 i_setMachineState(MachineState_SettingUp);
5350 alock.release();
5351 for (size_t i = 0; i < task.m_llMediums.size(); ++i)
5352 {
5353 ComObjPtr<Medium> pMedium = (Medium*)(IMedium*)(task.m_llMediums.at(i));
5354 {
5355 AutoCaller mac(pMedium);
5356 if (FAILED(mac.rc())) throw mac.rc();
5357 Utf8Str strLocation = pMedium->i_getLocationFull();
5358 LogFunc(("Deleting file %s\n", strLocation.c_str()));
5359 rc = task.m_pProgress->SetNextOperation(BstrFmt(tr("Deleting '%s'"), strLocation.c_str()).raw(), 1);
5360 if (FAILED(rc)) throw rc;
5361 }
5362 if (pMedium->i_isMediumFormatFile())
5363 {
5364 ComPtr<IProgress> pProgress2;
5365 rc = pMedium->DeleteStorage(pProgress2.asOutParam());
5366 if (FAILED(rc)) throw rc;
5367 rc = task.m_pProgress->WaitForAsyncProgressCompletion(pProgress2);
5368 if (FAILED(rc)) throw rc;
5369 /* Check the result of the asynchronous process. */
5370 LONG iRc;
5371 rc = pProgress2->COMGETTER(ResultCode)(&iRc);
5372 if (FAILED(rc)) throw rc;
5373 /* If the thread of the progress object has an error, then
5374 * retrieve the error info from there, or it'll be lost. */
5375 if (FAILED(iRc))
5376 throw setError(ProgressErrorInfo(pProgress2));
5377 }
5378
5379 /* Close the medium, deliberately without checking the return
5380 * code, and without leaving any trace in the error info, as
5381 * a failure here is a very minor issue, which shouldn't happen
5382 * as above we even managed to delete the medium. */
5383 {
5384 ErrorInfoKeeper eik;
5385 pMedium->Close();
5386 }
5387 }
5388 i_setMachineState(oldState);
5389 alock.acquire();
5390
5391 // delete the files pushed on the task list by Machine::Delete()
5392 // (this includes saved states of the machine and snapshots and
5393 // medium storage files from the IMedium list passed in, and the
5394 // machine XML file)
5395 for (StringsList::const_iterator
5396 it = task.m_llFilesToDelete.begin();
5397 it != task.m_llFilesToDelete.end();
5398 ++it)
5399 {
5400 const Utf8Str &strFile = *it;
5401 LogFunc(("Deleting file %s\n", strFile.c_str()));
5402 rc = task.m_pProgress->SetNextOperation(BstrFmt(tr("Deleting '%s'"), it->c_str()).raw(), 1);
5403 if (FAILED(rc)) throw rc;
5404
5405 int vrc = RTFileDelete(strFile.c_str());
5406 if (RT_FAILURE(vrc))
5407 throw setError(VBOX_E_IPRT_ERROR,
5408 tr("Could not delete file '%s' (%Rrc)"), strFile.c_str(), vrc);
5409 }
5410
5411 rc = task.m_pProgress->SetNextOperation(Bstr(tr("Cleaning up machine directory")).raw(), 1);
5412 if (FAILED(rc)) throw rc;
5413
5414 /* delete the settings only when the file actually exists */
5415 if (mData->pMachineConfigFile->fileExists())
5416 {
5417 /* Delete any backup or uncommitted XML files. Ignore failures.
5418 See the fSafe parameter of xml::XmlFileWriter::write for details. */
5419 /** @todo Find a way to avoid referring directly to iprt/xml.h here. */
5420 Utf8Str otherXml = Utf8StrFmt("%s%s", mData->m_strConfigFileFull.c_str(), xml::XmlFileWriter::s_pszTmpSuff);
5421 RTFileDelete(otherXml.c_str());
5422 otherXml = Utf8StrFmt("%s%s", mData->m_strConfigFileFull.c_str(), xml::XmlFileWriter::s_pszPrevSuff);
5423 RTFileDelete(otherXml.c_str());
5424
5425 /* delete the Logs folder, nothing important should be left
5426 * there (we don't check for errors because the user might have
5427 * some private files there that we don't want to delete) */
5428 Utf8Str logFolder;
5429 getLogFolder(logFolder);
5430 Assert(logFolder.length());
5431 if (RTDirExists(logFolder.c_str()))
5432 {
5433 /* Delete all VBox.log[.N] files from the Logs folder
5434 * (this must be in sync with the rotation logic in
5435 * Console::powerUpThread()). Also, delete the VBox.png[.N]
5436 * files that may have been created by the GUI. */
5437 Utf8Str log = Utf8StrFmt("%s%cVBox.log",
5438 logFolder.c_str(), RTPATH_DELIMITER);
5439 RTFileDelete(log.c_str());
5440 log = Utf8StrFmt("%s%cVBox.png",
5441 logFolder.c_str(), RTPATH_DELIMITER);
5442 RTFileDelete(log.c_str());
5443 for (int i = uLogHistoryCount; i > 0; i--)
5444 {
5445 log = Utf8StrFmt("%s%cVBox.log.%d",
5446 logFolder.c_str(), RTPATH_DELIMITER, i);
5447 RTFileDelete(log.c_str());
5448 log = Utf8StrFmt("%s%cVBox.png.%d",
5449 logFolder.c_str(), RTPATH_DELIMITER, i);
5450 RTFileDelete(log.c_str());
5451 }
5452#if defined(RT_OS_WINDOWS)
5453 log = Utf8StrFmt("%s%cVBoxStartup.log", logFolder.c_str(), RTPATH_DELIMITER);
5454 RTFileDelete(log.c_str());
5455 log = Utf8StrFmt("%s%cVBoxHardening.log", logFolder.c_str(), RTPATH_DELIMITER);
5456 RTFileDelete(log.c_str());
5457#endif
5458
5459 RTDirRemove(logFolder.c_str());
5460 }
5461
5462 /* delete the Snapshots folder, nothing important should be left
5463 * there (we don't check for errors because the user might have
5464 * some private files there that we don't want to delete) */
5465 Utf8Str strFullSnapshotFolder;
5466 i_calculateFullPath(mUserData->s.strSnapshotFolder, strFullSnapshotFolder);
5467 Assert(!strFullSnapshotFolder.isEmpty());
5468 if (RTDirExists(strFullSnapshotFolder.c_str()))
5469 RTDirRemove(strFullSnapshotFolder.c_str());
5470
5471 // delete the directory that contains the settings file, but only
5472 // if it matches the VM name
5473 Utf8Str settingsDir;
5474 if (i_isInOwnDir(&settingsDir))
5475 RTDirRemove(settingsDir.c_str());
5476 }
5477
5478 alock.release();
5479
5480 mParent->i_saveModifiedRegistries();
5481 }
5482 catch (HRESULT aRC) { rc = aRC; }
5483
5484 task.m_pProgress->i_notifyComplete(rc);
5485
5486 LogFlowThisFuncLeave();
5487}
5488
5489HRESULT Machine::deleteConfig(const std::vector<ComPtr<IMedium> > &aMedia, ComPtr<IProgress> &aProgress)
5490{
5491 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
5492
5493 HRESULT rc = i_checkStateDependency(MutableStateDep);
5494 if (FAILED(rc)) return rc;
5495
5496 if (mData->mRegistered)
5497 return setError(VBOX_E_INVALID_VM_STATE,
5498 tr("Cannot delete settings of a registered machine"));
5499
5500 // collect files to delete
5501 StringsList llFilesToDelete(mData->llFilesToDelete); // saved states pushed here by Unregister()
5502 if (mData->pMachineConfigFile->fileExists())
5503 llFilesToDelete.push_back(mData->m_strConfigFileFull);
5504
5505 RTCList<ComPtr<IMedium> > llMediums;
5506 for (size_t i = 0; i < aMedia.size(); ++i)
5507 {
5508 IMedium *pIMedium(aMedia[i]);
5509 ComObjPtr<Medium> pMedium = static_cast<Medium*>(pIMedium);
5510 if (pMedium.isNull())
5511 return setError(E_INVALIDARG, "The given medium pointer with index %d is invalid", i);
5512 SafeArray<BSTR> ids;
5513 rc = pMedium->COMGETTER(MachineIds)(ComSafeArrayAsOutParam(ids));
5514 if (FAILED(rc)) return rc;
5515 /* At this point the medium should not have any back references
5516 * anymore. If it has it is attached to another VM and *must* not
5517 * deleted. */
5518 if (ids.size() < 1)
5519 llMediums.append(pMedium);
5520 }
5521
5522 ComObjPtr<Progress> pProgress;
5523 pProgress.createObject();
5524 rc = pProgress->init(i_getVirtualBox(),
5525 static_cast<IMachine*>(this) /* aInitiator */,
5526 Bstr(tr("Deleting files")).raw(),
5527 true /* fCancellable */,
5528 (ULONG)(1 + llMediums.size() + llFilesToDelete.size() + 1), // cOperations
5529 Bstr(tr("Collecting file inventory")).raw());
5530 if (FAILED(rc))
5531 return rc;
5532
5533 /* create and start the task on a separate thread (note that it will not
5534 * start working until we release alock) */
5535 DeleteConfigTask *pTask = new DeleteConfigTask(this, pProgress, "DeleteVM", llMediums, llFilesToDelete);
5536 rc = pTask->createThread();
5537 if (FAILED(rc))
5538 return rc;
5539
5540 pProgress.queryInterfaceTo(aProgress.asOutParam());
5541
5542 LogFlowFuncLeave();
5543
5544 return S_OK;
5545}
5546
5547HRESULT Machine::findSnapshot(const com::Utf8Str &aNameOrId, ComPtr<ISnapshot> &aSnapshot)
5548{
5549 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
5550
5551 ComObjPtr<Snapshot> pSnapshot;
5552 HRESULT rc;
5553
5554 if (aNameOrId.isEmpty())
5555 // null case (caller wants root snapshot): i_findSnapshotById() handles this
5556 rc = i_findSnapshotById(Guid(), pSnapshot, true /* aSetError */);
5557 else
5558 {
5559 Guid uuid(aNameOrId);
5560 if (uuid.isValid())
5561 rc = i_findSnapshotById(uuid, pSnapshot, true /* aSetError */);
5562 else
5563 rc = i_findSnapshotByName(aNameOrId, pSnapshot, true /* aSetError */);
5564 }
5565 pSnapshot.queryInterfaceTo(aSnapshot.asOutParam());
5566
5567 return rc;
5568}
5569
5570HRESULT Machine::createSharedFolder(const com::Utf8Str &aName, const com::Utf8Str &aHostPath, BOOL aWritable, BOOL aAutomount)
5571{
5572 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
5573
5574 HRESULT rc = i_checkStateDependency(MutableOrRunningStateDep);
5575 if (FAILED(rc)) return rc;
5576
5577 ComObjPtr<SharedFolder> sharedFolder;
5578 rc = i_findSharedFolder(aName, sharedFolder, false /* aSetError */);
5579 if (SUCCEEDED(rc))
5580 return setError(VBOX_E_OBJECT_IN_USE,
5581 tr("Shared folder named '%s' already exists"),
5582 aName.c_str());
5583
5584 sharedFolder.createObject();
5585 rc = sharedFolder->init(i_getMachine(),
5586 aName,
5587 aHostPath,
5588 !!aWritable,
5589 !!aAutomount,
5590 true /* fFailOnError */);
5591 if (FAILED(rc)) return rc;
5592
5593 i_setModified(IsModified_SharedFolders);
5594 mHWData.backup();
5595 mHWData->mSharedFolders.push_back(sharedFolder);
5596
5597 /* inform the direct session if any */
5598 alock.release();
5599 i_onSharedFolderChange();
5600
5601 return S_OK;
5602}
5603
5604HRESULT Machine::removeSharedFolder(const com::Utf8Str &aName)
5605{
5606 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
5607
5608 HRESULT rc = i_checkStateDependency(MutableOrRunningStateDep);
5609 if (FAILED(rc)) return rc;
5610
5611 ComObjPtr<SharedFolder> sharedFolder;
5612 rc = i_findSharedFolder(aName, sharedFolder, true /* aSetError */);
5613 if (FAILED(rc)) return rc;
5614
5615 i_setModified(IsModified_SharedFolders);
5616 mHWData.backup();
5617 mHWData->mSharedFolders.remove(sharedFolder);
5618
5619 /* inform the direct session if any */
5620 alock.release();
5621 i_onSharedFolderChange();
5622
5623 return S_OK;
5624}
5625
5626HRESULT Machine::canShowConsoleWindow(BOOL *aCanShow)
5627{
5628 /* start with No */
5629 *aCanShow = FALSE;
5630
5631 ComPtr<IInternalSessionControl> directControl;
5632 {
5633 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
5634
5635 if (mData->mSession.mState != SessionState_Locked)
5636 return setError(VBOX_E_INVALID_VM_STATE,
5637 tr("Machine is not locked for session (session state: %s)"),
5638 Global::stringifySessionState(mData->mSession.mState));
5639
5640 if (mData->mSession.mLockType == LockType_VM)
5641 directControl = mData->mSession.mDirectControl;
5642 }
5643
5644 /* ignore calls made after #OnSessionEnd() is called */
5645 if (!directControl)
5646 return S_OK;
5647
5648 LONG64 dummy;
5649 return directControl->OnShowWindow(TRUE /* aCheck */, aCanShow, &dummy);
5650}
5651
5652HRESULT Machine::showConsoleWindow(LONG64 *aWinId)
5653{
5654 ComPtr<IInternalSessionControl> directControl;
5655 {
5656 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
5657
5658 if (mData->mSession.mState != SessionState_Locked)
5659 return setError(E_FAIL,
5660 tr("Machine is not locked for session (session state: %s)"),
5661 Global::stringifySessionState(mData->mSession.mState));
5662
5663 if (mData->mSession.mLockType == LockType_VM)
5664 directControl = mData->mSession.mDirectControl;
5665 }
5666
5667 /* ignore calls made after #OnSessionEnd() is called */
5668 if (!directControl)
5669 return S_OK;
5670
5671 BOOL dummy;
5672 return directControl->OnShowWindow(FALSE /* aCheck */, &dummy, aWinId);
5673}
5674
5675#ifdef VBOX_WITH_GUEST_PROPS
5676/**
5677 * Look up a guest property in VBoxSVC's internal structures.
5678 */
5679HRESULT Machine::i_getGuestPropertyFromService(const com::Utf8Str &aName,
5680 com::Utf8Str &aValue,
5681 LONG64 *aTimestamp,
5682 com::Utf8Str &aFlags) const
5683{
5684 using namespace guestProp;
5685
5686 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
5687 HWData::GuestPropertyMap::const_iterator it = mHWData->mGuestProperties.find(aName);
5688
5689 if (it != mHWData->mGuestProperties.end())
5690 {
5691 char szFlags[MAX_FLAGS_LEN + 1];
5692 aValue = it->second.strValue;
5693 *aTimestamp = it->second.mTimestamp;
5694 writeFlags(it->second.mFlags, szFlags);
5695 aFlags = Utf8Str(szFlags);
5696 }
5697
5698 return S_OK;
5699}
5700
5701/**
5702 * Query the VM that a guest property belongs to for the property.
5703 * @returns E_ACCESSDENIED if the VM process is not available or not
5704 * currently handling queries and the lookup should then be done in
5705 * VBoxSVC.
5706 */
5707HRESULT Machine::i_getGuestPropertyFromVM(const com::Utf8Str &aName,
5708 com::Utf8Str &aValue,
5709 LONG64 *aTimestamp,
5710 com::Utf8Str &aFlags) const
5711{
5712 HRESULT rc = S_OK;
5713 BSTR bValue = NULL;
5714 BSTR bFlags = NULL;
5715
5716 ComPtr<IInternalSessionControl> directControl;
5717 {
5718 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
5719 if (mData->mSession.mLockType == LockType_VM)
5720 directControl = mData->mSession.mDirectControl;
5721 }
5722
5723 /* ignore calls made after #OnSessionEnd() is called */
5724 if (!directControl)
5725 rc = E_ACCESSDENIED;
5726 else
5727 rc = directControl->AccessGuestProperty(Bstr(aName).raw(), Bstr("").raw(), Bstr("").raw(),
5728 0 /* accessMode */,
5729 &bValue, aTimestamp, &bFlags);
5730
5731 aValue = bValue;
5732 aFlags = bFlags;
5733
5734 return rc;
5735}
5736#endif // VBOX_WITH_GUEST_PROPS
5737
5738HRESULT Machine::getGuestProperty(const com::Utf8Str &aName,
5739 com::Utf8Str &aValue,
5740 LONG64 *aTimestamp,
5741 com::Utf8Str &aFlags)
5742{
5743#ifndef VBOX_WITH_GUEST_PROPS
5744 ReturnComNotImplemented();
5745#else // VBOX_WITH_GUEST_PROPS
5746
5747 HRESULT rc = i_getGuestPropertyFromVM(aName, aValue, aTimestamp, aFlags);
5748
5749 if (rc == E_ACCESSDENIED)
5750 /* The VM is not running or the service is not (yet) accessible */
5751 rc = i_getGuestPropertyFromService(aName, aValue, aTimestamp, aFlags);
5752 return rc;
5753#endif // VBOX_WITH_GUEST_PROPS
5754}
5755
5756HRESULT Machine::getGuestPropertyValue(const com::Utf8Str &aProperty, com::Utf8Str &aValue)
5757{
5758 LONG64 dummyTimestamp;
5759 com::Utf8Str dummyFlags;
5760 HRESULT rc = getGuestProperty(aProperty, aValue, &dummyTimestamp, dummyFlags);
5761 return rc;
5762
5763}
5764HRESULT Machine::getGuestPropertyTimestamp(const com::Utf8Str &aProperty, LONG64 *aValue)
5765{
5766 com::Utf8Str dummyFlags;
5767 com::Utf8Str dummyValue;
5768 HRESULT rc = getGuestProperty(aProperty, dummyValue, aValue, dummyFlags);
5769 return rc;
5770}
5771
5772#ifdef VBOX_WITH_GUEST_PROPS
5773/**
5774 * Set a guest property in VBoxSVC's internal structures.
5775 */
5776HRESULT Machine::i_setGuestPropertyToService(const com::Utf8Str &aName, const com::Utf8Str &aValue,
5777 const com::Utf8Str &aFlags, bool fDelete)
5778{
5779 using namespace guestProp;
5780
5781 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
5782 HRESULT rc = S_OK;
5783
5784 rc = i_checkStateDependency(MutableOrSavedStateDep);
5785 if (FAILED(rc)) return rc;
5786
5787 try
5788 {
5789 uint32_t fFlags = NILFLAG;
5790 if (aFlags.length() && RT_FAILURE(validateFlags(aFlags.c_str(), &fFlags)))
5791 return setError(E_INVALIDARG, tr("Invalid guest property flag values: '%s'"), aFlags.c_str());
5792
5793 HWData::GuestPropertyMap::iterator it = mHWData->mGuestProperties.find(aName);
5794 if (it == mHWData->mGuestProperties.end())
5795 {
5796 if (!fDelete)
5797 {
5798 i_setModified(IsModified_MachineData);
5799 mHWData.backupEx();
5800
5801 RTTIMESPEC time;
5802 HWData::GuestProperty prop;
5803 prop.strValue = Bstr(aValue).raw();
5804 prop.mTimestamp = RTTimeSpecGetNano(RTTimeNow(&time));
5805 prop.mFlags = fFlags;
5806 mHWData->mGuestProperties[aName] = prop;
5807 }
5808 }
5809 else
5810 {
5811 if (it->second.mFlags & (RDONLYHOST))
5812 {
5813 rc = setError(E_ACCESSDENIED, tr("The property '%s' cannot be changed by the host"), aName.c_str());
5814 }
5815 else
5816 {
5817 i_setModified(IsModified_MachineData);
5818 mHWData.backupEx();
5819
5820 /* The backupEx() operation invalidates our iterator,
5821 * so get a new one. */
5822 it = mHWData->mGuestProperties.find(aName);
5823 Assert(it != mHWData->mGuestProperties.end());
5824
5825 if (!fDelete)
5826 {
5827 RTTIMESPEC time;
5828 it->second.strValue = aValue;
5829 it->second.mTimestamp = RTTimeSpecGetNano(RTTimeNow(&time));
5830 it->second.mFlags = fFlags;
5831 }
5832 else
5833 mHWData->mGuestProperties.erase(it);
5834 }
5835 }
5836
5837 if (SUCCEEDED(rc))
5838 {
5839 alock.release();
5840
5841 mParent->i_onGuestPropertyChange(mData->mUuid,
5842 Bstr(aName).raw(),
5843 Bstr(aValue).raw(),
5844 Bstr(aFlags).raw());
5845 }
5846 }
5847 catch (std::bad_alloc &)
5848 {
5849 rc = E_OUTOFMEMORY;
5850 }
5851
5852 return rc;
5853}
5854
5855/**
5856 * Set a property on the VM that that property belongs to.
5857 * @returns E_ACCESSDENIED if the VM process is not available or not
5858 * currently handling queries and the setting should then be done in
5859 * VBoxSVC.
5860 */
5861HRESULT Machine::i_setGuestPropertyToVM(const com::Utf8Str &aName, const com::Utf8Str &aValue,
5862 const com::Utf8Str &aFlags, bool fDelete)
5863{
5864 HRESULT rc;
5865
5866 try
5867 {
5868 ComPtr<IInternalSessionControl> directControl;
5869 {
5870 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
5871 if (mData->mSession.mLockType == LockType_VM)
5872 directControl = mData->mSession.mDirectControl;
5873 }
5874
5875 BSTR dummy = NULL; /* will not be changed (setter) */
5876 LONG64 dummy64;
5877 if (!directControl)
5878 rc = E_ACCESSDENIED;
5879 else
5880 /** @todo Fix when adding DeleteGuestProperty(), see defect. */
5881 rc = directControl->AccessGuestProperty(Bstr(aName).raw(), Bstr(aValue).raw(), Bstr(aFlags).raw(),
5882 fDelete? 2: 1 /* accessMode */,
5883 &dummy, &dummy64, &dummy);
5884 }
5885 catch (std::bad_alloc &)
5886 {
5887 rc = E_OUTOFMEMORY;
5888 }
5889
5890 return rc;
5891}
5892#endif // VBOX_WITH_GUEST_PROPS
5893
5894HRESULT Machine::setGuestProperty(const com::Utf8Str &aProperty, const com::Utf8Str &aValue,
5895 const com::Utf8Str &aFlags)
5896{
5897#ifndef VBOX_WITH_GUEST_PROPS
5898 ReturnComNotImplemented();
5899#else // VBOX_WITH_GUEST_PROPS
5900 HRESULT rc = i_setGuestPropertyToVM(aProperty, aValue, aFlags, /* fDelete = */ false);
5901 if (rc == E_ACCESSDENIED)
5902 /* The VM is not running or the service is not (yet) accessible */
5903 rc = i_setGuestPropertyToService(aProperty, aValue, aFlags, /* fDelete = */ false);
5904 return rc;
5905#endif // VBOX_WITH_GUEST_PROPS
5906}
5907
5908HRESULT Machine::setGuestPropertyValue(const com::Utf8Str &aProperty, const com::Utf8Str &aValue)
5909{
5910 return setGuestProperty(aProperty, aValue, "");
5911}
5912
5913HRESULT Machine::deleteGuestProperty(const com::Utf8Str &aName)
5914{
5915#ifndef VBOX_WITH_GUEST_PROPS
5916 ReturnComNotImplemented();
5917#else // VBOX_WITH_GUEST_PROPS
5918 HRESULT rc = i_setGuestPropertyToVM(aName, "", "", /* fDelete = */ true);
5919 if (rc == E_ACCESSDENIED)
5920 /* The VM is not running or the service is not (yet) accessible */
5921 rc = i_setGuestPropertyToService(aName, "", "", /* fDelete = */ true);
5922 return rc;
5923#endif // VBOX_WITH_GUEST_PROPS
5924}
5925
5926#ifdef VBOX_WITH_GUEST_PROPS
5927/**
5928 * Enumerate the guest properties in VBoxSVC's internal structures.
5929 */
5930HRESULT Machine::i_enumerateGuestPropertiesInService(const com::Utf8Str &aPatterns,
5931 std::vector<com::Utf8Str> &aNames,
5932 std::vector<com::Utf8Str> &aValues,
5933 std::vector<LONG64> &aTimestamps,
5934 std::vector<com::Utf8Str> &aFlags)
5935{
5936 using namespace guestProp;
5937
5938 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
5939 Utf8Str strPatterns(aPatterns);
5940
5941 HWData::GuestPropertyMap propMap;
5942
5943 /*
5944 * Look for matching patterns and build up a list.
5945 */
5946 for (HWData::GuestPropertyMap::const_iterator
5947 it = mHWData->mGuestProperties.begin();
5948 it != mHWData->mGuestProperties.end();
5949 ++it)
5950 {
5951 if ( strPatterns.isEmpty()
5952 || RTStrSimplePatternMultiMatch(strPatterns.c_str(),
5953 RTSTR_MAX,
5954 it->first.c_str(),
5955 RTSTR_MAX,
5956 NULL)
5957 )
5958 propMap.insert(*it);
5959 }
5960
5961 alock.release();
5962
5963 /*
5964 * And build up the arrays for returning the property information.
5965 */
5966 size_t cEntries = propMap.size();
5967
5968 aNames.resize(cEntries);
5969 aValues.resize(cEntries);
5970 aTimestamps.resize(cEntries);
5971 aFlags.resize(cEntries);
5972
5973 char szFlags[MAX_FLAGS_LEN + 1];
5974 size_t i = 0;
5975 for (HWData::GuestPropertyMap::const_iterator
5976 it = propMap.begin();
5977 it != propMap.end();
5978 ++it, ++i)
5979 {
5980 aNames[i] = it->first;
5981 aValues[i] = it->second.strValue;
5982 aTimestamps[i] = it->second.mTimestamp;
5983 writeFlags(it->second.mFlags, szFlags);
5984 aFlags[i] = Utf8Str(szFlags);
5985 }
5986
5987 return S_OK;
5988}
5989
5990/**
5991 * Enumerate the properties managed by a VM.
5992 * @returns E_ACCESSDENIED if the VM process is not available or not
5993 * currently handling queries and the setting should then be done in
5994 * VBoxSVC.
5995 */
5996HRESULT Machine::i_enumerateGuestPropertiesOnVM(const com::Utf8Str &aPatterns,
5997 std::vector<com::Utf8Str> &aNames,
5998 std::vector<com::Utf8Str> &aValues,
5999 std::vector<LONG64> &aTimestamps,
6000 std::vector<com::Utf8Str> &aFlags)
6001{
6002 HRESULT rc;
6003 ComPtr<IInternalSessionControl> directControl;
6004 {
6005 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
6006 if (mData->mSession.mLockType == LockType_VM)
6007 directControl = mData->mSession.mDirectControl;
6008 }
6009
6010 com::SafeArray<BSTR> bNames;
6011 com::SafeArray<BSTR> bValues;
6012 com::SafeArray<LONG64> bTimestamps;
6013 com::SafeArray<BSTR> bFlags;
6014
6015 if (!directControl)
6016 rc = E_ACCESSDENIED;
6017 else
6018 rc = directControl->EnumerateGuestProperties(Bstr(aPatterns).raw(),
6019 ComSafeArrayAsOutParam(bNames),
6020 ComSafeArrayAsOutParam(bValues),
6021 ComSafeArrayAsOutParam(bTimestamps),
6022 ComSafeArrayAsOutParam(bFlags));
6023 size_t i;
6024 aNames.resize(bNames.size());
6025 for (i = 0; i < bNames.size(); ++i)
6026 aNames[i] = Utf8Str(bNames[i]);
6027 aValues.resize(bValues.size());
6028 for (i = 0; i < bValues.size(); ++i)
6029 aValues[i] = Utf8Str(bValues[i]);
6030 aTimestamps.resize(bTimestamps.size());
6031 for (i = 0; i < bTimestamps.size(); ++i)
6032 aTimestamps[i] = bTimestamps[i];
6033 aFlags.resize(bFlags.size());
6034 for (i = 0; i < bFlags.size(); ++i)
6035 aFlags[i] = Utf8Str(bFlags[i]);
6036
6037 return rc;
6038}
6039#endif // VBOX_WITH_GUEST_PROPS
6040HRESULT Machine::enumerateGuestProperties(const com::Utf8Str &aPatterns,
6041 std::vector<com::Utf8Str> &aNames,
6042 std::vector<com::Utf8Str> &aValues,
6043 std::vector<LONG64> &aTimestamps,
6044 std::vector<com::Utf8Str> &aFlags)
6045{
6046#ifndef VBOX_WITH_GUEST_PROPS
6047 ReturnComNotImplemented();
6048#else // VBOX_WITH_GUEST_PROPS
6049
6050 HRESULT rc = i_enumerateGuestPropertiesOnVM(aPatterns, aNames, aValues, aTimestamps, aFlags);
6051
6052 if (rc == E_ACCESSDENIED)
6053 /* The VM is not running or the service is not (yet) accessible */
6054 rc = i_enumerateGuestPropertiesInService(aPatterns, aNames, aValues, aTimestamps, aFlags);
6055 return rc;
6056#endif // VBOX_WITH_GUEST_PROPS
6057}
6058
6059HRESULT Machine::getMediumAttachmentsOfController(const com::Utf8Str &aName,
6060 std::vector<ComPtr<IMediumAttachment> > &aMediumAttachments)
6061{
6062 MediumAttachmentList atts;
6063
6064 HRESULT rc = i_getMediumAttachmentsOfController(aName, atts);
6065 if (FAILED(rc)) return rc;
6066
6067 aMediumAttachments.resize(atts.size());
6068 size_t i = 0;
6069 for (MediumAttachmentList::const_iterator
6070 it = atts.begin();
6071 it != atts.end();
6072 ++it, ++i)
6073 (*it).queryInterfaceTo(aMediumAttachments[i].asOutParam());
6074
6075 return S_OK;
6076}
6077
6078HRESULT Machine::getMediumAttachment(const com::Utf8Str &aName,
6079 LONG aControllerPort,
6080 LONG aDevice,
6081 ComPtr<IMediumAttachment> &aAttachment)
6082{
6083 LogFlowThisFunc(("aControllerName=\"%s\" aControllerPort=%d aDevice=%d\n",
6084 aName.c_str(), aControllerPort, aDevice));
6085
6086 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
6087
6088 aAttachment = NULL;
6089
6090 ComObjPtr<MediumAttachment> pAttach = i_findAttachment(*mMediumAttachments.data(),
6091 Bstr(aName).raw(),
6092 aControllerPort,
6093 aDevice);
6094 if (pAttach.isNull())
6095 return setError(VBOX_E_OBJECT_NOT_FOUND,
6096 tr("No storage device attached to device slot %d on port %d of controller '%s'"),
6097 aDevice, aControllerPort, aName.c_str());
6098
6099 pAttach.queryInterfaceTo(aAttachment.asOutParam());
6100
6101 return S_OK;
6102}
6103
6104
6105HRESULT Machine::addStorageController(const com::Utf8Str &aName,
6106 StorageBus_T aConnectionType,
6107 ComPtr<IStorageController> &aController)
6108{
6109 if ( (aConnectionType <= StorageBus_Null)
6110 || (aConnectionType > StorageBus_PCIe))
6111 return setError(E_INVALIDARG,
6112 tr("Invalid connection type: %d"),
6113 aConnectionType);
6114
6115 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
6116
6117 HRESULT rc = i_checkStateDependency(MutableStateDep);
6118 if (FAILED(rc)) return rc;
6119
6120 /* try to find one with the name first. */
6121 ComObjPtr<StorageController> ctrl;
6122
6123 rc = i_getStorageControllerByName(aName, ctrl, false /* aSetError */);
6124 if (SUCCEEDED(rc))
6125 return setError(VBOX_E_OBJECT_IN_USE,
6126 tr("Storage controller named '%s' already exists"),
6127 aName.c_str());
6128
6129 ctrl.createObject();
6130
6131 /* get a new instance number for the storage controller */
6132 ULONG ulInstance = 0;
6133 bool fBootable = true;
6134 for (StorageControllerList::const_iterator
6135 it = mStorageControllers->begin();
6136 it != mStorageControllers->end();
6137 ++it)
6138 {
6139 if ((*it)->i_getStorageBus() == aConnectionType)
6140 {
6141 ULONG ulCurInst = (*it)->i_getInstance();
6142
6143 if (ulCurInst >= ulInstance)
6144 ulInstance = ulCurInst + 1;
6145
6146 /* Only one controller of each type can be marked as bootable. */
6147 if ((*it)->i_getBootable())
6148 fBootable = false;
6149 }
6150 }
6151
6152 rc = ctrl->init(this, aName, aConnectionType, ulInstance, fBootable);
6153 if (FAILED(rc)) return rc;
6154
6155 i_setModified(IsModified_Storage);
6156 mStorageControllers.backup();
6157 mStorageControllers->push_back(ctrl);
6158
6159 ctrl.queryInterfaceTo(aController.asOutParam());
6160
6161 /* inform the direct session if any */
6162 alock.release();
6163 i_onStorageControllerChange();
6164
6165 return S_OK;
6166}
6167
6168HRESULT Machine::getStorageControllerByName(const com::Utf8Str &aName,
6169 ComPtr<IStorageController> &aStorageController)
6170{
6171 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
6172
6173 ComObjPtr<StorageController> ctrl;
6174
6175 HRESULT rc = i_getStorageControllerByName(aName, ctrl, true /* aSetError */);
6176 if (SUCCEEDED(rc))
6177 ctrl.queryInterfaceTo(aStorageController.asOutParam());
6178
6179 return rc;
6180}
6181
6182HRESULT Machine::getStorageControllerByInstance(StorageBus_T aConnectionType,
6183 ULONG aInstance,
6184 ComPtr<IStorageController> &aStorageController)
6185{
6186 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
6187
6188 for (StorageControllerList::const_iterator
6189 it = mStorageControllers->begin();
6190 it != mStorageControllers->end();
6191 ++it)
6192 {
6193 if ( (*it)->i_getStorageBus() == aConnectionType
6194 && (*it)->i_getInstance() == aInstance)
6195 {
6196 (*it).queryInterfaceTo(aStorageController.asOutParam());
6197 return S_OK;
6198 }
6199 }
6200
6201 return setError(VBOX_E_OBJECT_NOT_FOUND,
6202 tr("Could not find a storage controller with instance number '%lu'"),
6203 aInstance);
6204}
6205
6206HRESULT Machine::setStorageControllerBootable(const com::Utf8Str &aName, BOOL aBootable)
6207{
6208 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
6209
6210 HRESULT rc = i_checkStateDependency(MutableStateDep);
6211 if (FAILED(rc)) return rc;
6212
6213 ComObjPtr<StorageController> ctrl;
6214
6215 rc = i_getStorageControllerByName(aName, ctrl, true /* aSetError */);
6216 if (SUCCEEDED(rc))
6217 {
6218 /* Ensure that only one controller of each type is marked as bootable. */
6219 if (aBootable == TRUE)
6220 {
6221 for (StorageControllerList::const_iterator
6222 it = mStorageControllers->begin();
6223 it != mStorageControllers->end();
6224 ++it)
6225 {
6226 ComObjPtr<StorageController> aCtrl = (*it);
6227
6228 if ( (aCtrl->i_getName() != aName)
6229 && aCtrl->i_getBootable() == TRUE
6230 && aCtrl->i_getStorageBus() == ctrl->i_getStorageBus()
6231 && aCtrl->i_getControllerType() == ctrl->i_getControllerType())
6232 {
6233 aCtrl->i_setBootable(FALSE);
6234 break;
6235 }
6236 }
6237 }
6238
6239 if (SUCCEEDED(rc))
6240 {
6241 ctrl->i_setBootable(aBootable);
6242 i_setModified(IsModified_Storage);
6243 }
6244 }
6245
6246 if (SUCCEEDED(rc))
6247 {
6248 /* inform the direct session if any */
6249 alock.release();
6250 i_onStorageControllerChange();
6251 }
6252
6253 return rc;
6254}
6255
6256HRESULT Machine::removeStorageController(const com::Utf8Str &aName)
6257{
6258 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
6259
6260 HRESULT rc = i_checkStateDependency(MutableStateDep);
6261 if (FAILED(rc)) return rc;
6262
6263 ComObjPtr<StorageController> ctrl;
6264 rc = i_getStorageControllerByName(aName, ctrl, true /* aSetError */);
6265 if (FAILED(rc)) return rc;
6266
6267 {
6268 /* find all attached devices to the appropriate storage controller and detach them all */
6269 // make a temporary list because detachDevice invalidates iterators into
6270 // mMediumAttachments
6271 MediumAttachmentList llAttachments2 = *mMediumAttachments.data();
6272
6273 for (MediumAttachmentList::const_iterator
6274 it = llAttachments2.begin();
6275 it != llAttachments2.end();
6276 ++it)
6277 {
6278 MediumAttachment *pAttachTemp = *it;
6279
6280 AutoCaller localAutoCaller(pAttachTemp);
6281 if (FAILED(localAutoCaller.rc())) return localAutoCaller.rc();
6282
6283 AutoReadLock local_alock(pAttachTemp COMMA_LOCKVAL_SRC_POS);
6284
6285 if (pAttachTemp->i_getControllerName() == aName)
6286 {
6287 rc = i_detachDevice(pAttachTemp, alock, NULL);
6288 if (FAILED(rc)) return rc;
6289 }
6290 }
6291 }
6292
6293 /* We can remove it now. */
6294 i_setModified(IsModified_Storage);
6295 mStorageControllers.backup();
6296
6297 ctrl->i_unshare();
6298
6299 mStorageControllers->remove(ctrl);
6300
6301 /* inform the direct session if any */
6302 alock.release();
6303 i_onStorageControllerChange();
6304
6305 return S_OK;
6306}
6307
6308HRESULT Machine::addUSBController(const com::Utf8Str &aName, USBControllerType_T aType,
6309 ComPtr<IUSBController> &aController)
6310{
6311 if ( (aType <= USBControllerType_Null)
6312 || (aType >= USBControllerType_Last))
6313 return setError(E_INVALIDARG,
6314 tr("Invalid USB controller type: %d"),
6315 aType);
6316
6317 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
6318
6319 HRESULT rc = i_checkStateDependency(MutableStateDep);
6320 if (FAILED(rc)) return rc;
6321
6322 /* try to find one with the same type first. */
6323 ComObjPtr<USBController> ctrl;
6324
6325 rc = i_getUSBControllerByName(aName, ctrl, false /* aSetError */);
6326 if (SUCCEEDED(rc))
6327 return setError(VBOX_E_OBJECT_IN_USE,
6328 tr("USB controller named '%s' already exists"),
6329 aName.c_str());
6330
6331 /* Check that we don't exceed the maximum number of USB controllers for the given type. */
6332 ULONG maxInstances;
6333 rc = mParent->i_getSystemProperties()->GetMaxInstancesOfUSBControllerType(mHWData->mChipsetType, aType, &maxInstances);
6334 if (FAILED(rc))
6335 return rc;
6336
6337 ULONG cInstances = i_getUSBControllerCountByType(aType);
6338 if (cInstances >= maxInstances)
6339 return setError(E_INVALIDARG,
6340 tr("Too many USB controllers of this type"));
6341
6342 ctrl.createObject();
6343
6344 rc = ctrl->init(this, aName, aType);
6345 if (FAILED(rc)) return rc;
6346
6347 i_setModified(IsModified_USB);
6348 mUSBControllers.backup();
6349 mUSBControllers->push_back(ctrl);
6350
6351 ctrl.queryInterfaceTo(aController.asOutParam());
6352
6353 /* inform the direct session if any */
6354 alock.release();
6355 i_onUSBControllerChange();
6356
6357 return S_OK;
6358}
6359
6360HRESULT Machine::getUSBControllerByName(const com::Utf8Str &aName, ComPtr<IUSBController> &aController)
6361{
6362 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
6363
6364 ComObjPtr<USBController> ctrl;
6365
6366 HRESULT rc = i_getUSBControllerByName(aName, ctrl, true /* aSetError */);
6367 if (SUCCEEDED(rc))
6368 ctrl.queryInterfaceTo(aController.asOutParam());
6369
6370 return rc;
6371}
6372
6373HRESULT Machine::getUSBControllerCountByType(USBControllerType_T aType,
6374 ULONG *aControllers)
6375{
6376 if ( (aType <= USBControllerType_Null)
6377 || (aType >= USBControllerType_Last))
6378 return setError(E_INVALIDARG,
6379 tr("Invalid USB controller type: %d"),
6380 aType);
6381
6382 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
6383
6384 ComObjPtr<USBController> ctrl;
6385
6386 *aControllers = i_getUSBControllerCountByType(aType);
6387
6388 return S_OK;
6389}
6390
6391HRESULT Machine::removeUSBController(const com::Utf8Str &aName)
6392{
6393
6394 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
6395
6396 HRESULT rc = i_checkStateDependency(MutableStateDep);
6397 if (FAILED(rc)) return rc;
6398
6399 ComObjPtr<USBController> ctrl;
6400 rc = i_getUSBControllerByName(aName, ctrl, true /* aSetError */);
6401 if (FAILED(rc)) return rc;
6402
6403 i_setModified(IsModified_USB);
6404 mUSBControllers.backup();
6405
6406 ctrl->i_unshare();
6407
6408 mUSBControllers->remove(ctrl);
6409
6410 /* inform the direct session if any */
6411 alock.release();
6412 i_onUSBControllerChange();
6413
6414 return S_OK;
6415}
6416
6417HRESULT Machine::querySavedGuestScreenInfo(ULONG aScreenId,
6418 ULONG *aOriginX,
6419 ULONG *aOriginY,
6420 ULONG *aWidth,
6421 ULONG *aHeight,
6422 BOOL *aEnabled)
6423{
6424 uint32_t u32OriginX= 0;
6425 uint32_t u32OriginY= 0;
6426 uint32_t u32Width = 0;
6427 uint32_t u32Height = 0;
6428 uint16_t u16Flags = 0;
6429
6430 int vrc = readSavedGuestScreenInfo(mSSData->strStateFilePath, aScreenId,
6431 &u32OriginX, &u32OriginY, &u32Width, &u32Height, &u16Flags);
6432 if (RT_FAILURE(vrc))
6433 {
6434#ifdef RT_OS_WINDOWS
6435 /* HACK: GUI sets *pfEnabled to 'true' and expects it to stay so if the API fails.
6436 * This works with XPCOM. But Windows COM sets all output parameters to zero.
6437 * So just assign fEnable to TRUE again.
6438 * The right fix would be to change GUI API wrappers to make sure that parameters
6439 * are changed only if API succeeds.
6440 */
6441 *aEnabled = TRUE;
6442#endif
6443 return setError(VBOX_E_IPRT_ERROR,
6444 tr("Saved guest size is not available (%Rrc)"),
6445 vrc);
6446 }
6447
6448 *aOriginX = u32OriginX;
6449 *aOriginY = u32OriginY;
6450 *aWidth = u32Width;
6451 *aHeight = u32Height;
6452 *aEnabled = (u16Flags & VBVA_SCREEN_F_DISABLED) == 0;
6453
6454 return S_OK;
6455}
6456
6457HRESULT Machine::readSavedThumbnailToArray(ULONG aScreenId, BitmapFormat_T aBitmapFormat,
6458 ULONG *aWidth, ULONG *aHeight, std::vector<BYTE> &aData)
6459{
6460 if (aScreenId != 0)
6461 return E_NOTIMPL;
6462
6463 if ( aBitmapFormat != BitmapFormat_BGR0
6464 && aBitmapFormat != BitmapFormat_BGRA
6465 && aBitmapFormat != BitmapFormat_RGBA
6466 && aBitmapFormat != BitmapFormat_PNG)
6467 return setError(E_NOTIMPL,
6468 tr("Unsupported saved thumbnail format 0x%08X"), aBitmapFormat);
6469
6470 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
6471
6472 uint8_t *pu8Data = NULL;
6473 uint32_t cbData = 0;
6474 uint32_t u32Width = 0;
6475 uint32_t u32Height = 0;
6476
6477 int vrc = readSavedDisplayScreenshot(mSSData->strStateFilePath, 0 /* u32Type */, &pu8Data, &cbData, &u32Width, &u32Height);
6478
6479 if (RT_FAILURE(vrc))
6480 return setError(VBOX_E_IPRT_ERROR,
6481 tr("Saved thumbnail data is not available (%Rrc)"),
6482 vrc);
6483
6484 HRESULT hr = S_OK;
6485
6486 *aWidth = u32Width;
6487 *aHeight = u32Height;
6488
6489 if (cbData > 0)
6490 {
6491 /* Convert pixels to the format expected by the API caller. */
6492 if (aBitmapFormat == BitmapFormat_BGR0)
6493 {
6494 /* [0] B, [1] G, [2] R, [3] 0. */
6495 aData.resize(cbData);
6496 memcpy(&aData.front(), pu8Data, cbData);
6497 }
6498 else if (aBitmapFormat == BitmapFormat_BGRA)
6499 {
6500 /* [0] B, [1] G, [2] R, [3] A. */
6501 aData.resize(cbData);
6502 for (uint32_t i = 0; i < cbData; i += 4)
6503 {
6504 aData[i] = pu8Data[i];
6505 aData[i + 1] = pu8Data[i + 1];
6506 aData[i + 2] = pu8Data[i + 2];
6507 aData[i + 3] = 0xff;
6508 }
6509 }
6510 else if (aBitmapFormat == BitmapFormat_RGBA)
6511 {
6512 /* [0] R, [1] G, [2] B, [3] A. */
6513 aData.resize(cbData);
6514 for (uint32_t i = 0; i < cbData; i += 4)
6515 {
6516 aData[i] = pu8Data[i + 2];
6517 aData[i + 1] = pu8Data[i + 1];
6518 aData[i + 2] = pu8Data[i];
6519 aData[i + 3] = 0xff;
6520 }
6521 }
6522 else if (aBitmapFormat == BitmapFormat_PNG)
6523 {
6524 uint8_t *pu8PNG = NULL;
6525 uint32_t cbPNG = 0;
6526 uint32_t cxPNG = 0;
6527 uint32_t cyPNG = 0;
6528
6529 vrc = DisplayMakePNG(pu8Data, u32Width, u32Height, &pu8PNG, &cbPNG, &cxPNG, &cyPNG, 0);
6530
6531 if (RT_SUCCESS(vrc))
6532 {
6533 aData.resize(cbPNG);
6534 if (cbPNG)
6535 memcpy(&aData.front(), pu8PNG, cbPNG);
6536 }
6537 else
6538 hr = setError(VBOX_E_IPRT_ERROR,
6539 tr("Could not convert saved thumbnail to PNG (%Rrc)"),
6540 vrc);
6541
6542 RTMemFree(pu8PNG);
6543 }
6544 }
6545
6546 freeSavedDisplayScreenshot(pu8Data);
6547
6548 return hr;
6549}
6550
6551HRESULT Machine::querySavedScreenshotInfo(ULONG aScreenId,
6552 ULONG *aWidth,
6553 ULONG *aHeight,
6554 std::vector<BitmapFormat_T> &aBitmapFormats)
6555{
6556 if (aScreenId != 0)
6557 return E_NOTIMPL;
6558
6559 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
6560
6561 uint8_t *pu8Data = NULL;
6562 uint32_t cbData = 0;
6563 uint32_t u32Width = 0;
6564 uint32_t u32Height = 0;
6565
6566 int vrc = readSavedDisplayScreenshot(mSSData->strStateFilePath, 1 /* u32Type */, &pu8Data, &cbData, &u32Width, &u32Height);
6567
6568 if (RT_FAILURE(vrc))
6569 return setError(VBOX_E_IPRT_ERROR,
6570 tr("Saved screenshot data is not available (%Rrc)"),
6571 vrc);
6572
6573 *aWidth = u32Width;
6574 *aHeight = u32Height;
6575 aBitmapFormats.resize(1);
6576 aBitmapFormats[0] = BitmapFormat_PNG;
6577
6578 freeSavedDisplayScreenshot(pu8Data);
6579
6580 return S_OK;
6581}
6582
6583HRESULT Machine::readSavedScreenshotToArray(ULONG aScreenId,
6584 BitmapFormat_T aBitmapFormat,
6585 ULONG *aWidth,
6586 ULONG *aHeight,
6587 std::vector<BYTE> &aData)
6588{
6589 if (aScreenId != 0)
6590 return E_NOTIMPL;
6591
6592 if (aBitmapFormat != BitmapFormat_PNG)
6593 return E_NOTIMPL;
6594
6595 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
6596
6597 uint8_t *pu8Data = NULL;
6598 uint32_t cbData = 0;
6599 uint32_t u32Width = 0;
6600 uint32_t u32Height = 0;
6601
6602 int vrc = readSavedDisplayScreenshot(mSSData->strStateFilePath, 1 /* u32Type */, &pu8Data, &cbData, &u32Width, &u32Height);
6603
6604 if (RT_FAILURE(vrc))
6605 return setError(VBOX_E_IPRT_ERROR,
6606 tr("Saved screenshot thumbnail data is not available (%Rrc)"),
6607 vrc);
6608
6609 *aWidth = u32Width;
6610 *aHeight = u32Height;
6611
6612 aData.resize(cbData);
6613 if (cbData)
6614 memcpy(&aData.front(), pu8Data, cbData);
6615
6616 freeSavedDisplayScreenshot(pu8Data);
6617
6618 return S_OK;
6619}
6620
6621HRESULT Machine::hotPlugCPU(ULONG aCpu)
6622{
6623 HRESULT rc = S_OK;
6624 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
6625
6626 if (!mHWData->mCPUHotPlugEnabled)
6627 return setError(E_INVALIDARG, tr("CPU hotplug is not enabled"));
6628
6629 if (aCpu >= mHWData->mCPUCount)
6630 return setError(E_INVALIDARG, tr("CPU id exceeds number of possible CPUs [0:%lu]"), mHWData->mCPUCount-1);
6631
6632 if (mHWData->mCPUAttached[aCpu])
6633 return setError(VBOX_E_OBJECT_IN_USE, tr("CPU %lu is already attached"), aCpu);
6634
6635 alock.release();
6636 rc = i_onCPUChange(aCpu, false);
6637 alock.acquire();
6638 if (FAILED(rc)) return rc;
6639
6640 i_setModified(IsModified_MachineData);
6641 mHWData.backup();
6642 mHWData->mCPUAttached[aCpu] = true;
6643
6644 /** Save settings if online - @todo why is this required? -- @bugref{6818} */
6645 if (Global::IsOnline(mData->mMachineState))
6646 i_saveSettings(NULL);
6647
6648 return S_OK;
6649}
6650
6651HRESULT Machine::hotUnplugCPU(ULONG aCpu)
6652{
6653 HRESULT rc = S_OK;
6654
6655 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
6656
6657 if (!mHWData->mCPUHotPlugEnabled)
6658 return setError(E_INVALIDARG, tr("CPU hotplug is not enabled"));
6659
6660 if (aCpu >= SchemaDefs::MaxCPUCount)
6661 return setError(E_INVALIDARG,
6662 tr("CPU index exceeds maximum CPU count (must be in range [0:%lu])"),
6663 SchemaDefs::MaxCPUCount);
6664
6665 if (!mHWData->mCPUAttached[aCpu])
6666 return setError(VBOX_E_OBJECT_NOT_FOUND, tr("CPU %lu is not attached"), aCpu);
6667
6668 /* CPU 0 can't be detached */
6669 if (aCpu == 0)
6670 return setError(E_INVALIDARG, tr("It is not possible to detach CPU 0"));
6671
6672 alock.release();
6673 rc = i_onCPUChange(aCpu, true);
6674 alock.acquire();
6675 if (FAILED(rc)) return rc;
6676
6677 i_setModified(IsModified_MachineData);
6678 mHWData.backup();
6679 mHWData->mCPUAttached[aCpu] = false;
6680
6681 /** Save settings if online - @todo why is this required? -- @bugref{6818} */
6682 if (Global::IsOnline(mData->mMachineState))
6683 i_saveSettings(NULL);
6684
6685 return S_OK;
6686}
6687
6688HRESULT Machine::getCPUStatus(ULONG aCpu, BOOL *aAttached)
6689{
6690 *aAttached = false;
6691
6692 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
6693
6694 /* If hotplug is enabled the CPU is always enabled. */
6695 if (!mHWData->mCPUHotPlugEnabled)
6696 {
6697 if (aCpu < mHWData->mCPUCount)
6698 *aAttached = true;
6699 }
6700 else
6701 {
6702 if (aCpu < SchemaDefs::MaxCPUCount)
6703 *aAttached = mHWData->mCPUAttached[aCpu];
6704 }
6705
6706 return S_OK;
6707}
6708
6709HRESULT Machine::queryLogFilename(ULONG aIdx, com::Utf8Str &aFilename)
6710{
6711 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
6712
6713 Utf8Str log = i_getLogFilename(aIdx);
6714 if (!RTFileExists(log.c_str()))
6715 log.setNull();
6716 aFilename = log;
6717
6718 return S_OK;
6719}
6720
6721HRESULT Machine::readLog(ULONG aIdx, LONG64 aOffset, LONG64 aSize, std::vector<BYTE> &aData)
6722{
6723 if (aSize < 0)
6724 return setError(E_INVALIDARG, tr("The size argument (%lld) is negative"), aSize);
6725
6726 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
6727
6728 HRESULT rc = S_OK;
6729 Utf8Str log = i_getLogFilename(aIdx);
6730
6731 /* do not unnecessarily hold the lock while doing something which does
6732 * not need the lock and potentially takes a long time. */
6733 alock.release();
6734
6735 /* Limit the chunk size to 32K for now, as that gives better performance
6736 * over (XP)COM, and keeps the SOAP reply size under 1M for the webservice.
6737 * One byte expands to approx. 25 bytes of breathtaking XML. */
6738 size_t cbData = (size_t)RT_MIN(aSize, 32768);
6739 aData.resize(cbData);
6740
6741 RTFILE LogFile;
6742 int vrc = RTFileOpen(&LogFile, log.c_str(),
6743 RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_DENY_NONE);
6744 if (RT_SUCCESS(vrc))
6745 {
6746 vrc = RTFileReadAt(LogFile, aOffset, cbData? &aData.front(): NULL, cbData, &cbData);
6747 if (RT_SUCCESS(vrc))
6748 aData.resize(cbData);
6749 else
6750 rc = setError(VBOX_E_IPRT_ERROR,
6751 tr("Could not read log file '%s' (%Rrc)"),
6752 log.c_str(), vrc);
6753 RTFileClose(LogFile);
6754 }
6755 else
6756 rc = setError(VBOX_E_IPRT_ERROR,
6757 tr("Could not open log file '%s' (%Rrc)"),
6758 log.c_str(), vrc);
6759
6760 if (FAILED(rc))
6761 aData.resize(0);
6762
6763 return rc;
6764}
6765
6766
6767/**
6768 * Currently this method doesn't attach device to the running VM,
6769 * just makes sure it's plugged on next VM start.
6770 */
6771HRESULT Machine::attachHostPCIDevice(LONG aHostAddress, LONG aDesiredGuestAddress, BOOL /* aTryToUnbind */)
6772{
6773 // lock scope
6774 {
6775 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
6776
6777 HRESULT rc = i_checkStateDependency(MutableStateDep);
6778 if (FAILED(rc)) return rc;
6779
6780 ChipsetType_T aChipset = ChipsetType_PIIX3;
6781 COMGETTER(ChipsetType)(&aChipset);
6782
6783 if (aChipset != ChipsetType_ICH9)
6784 {
6785 return setError(E_INVALIDARG,
6786 tr("Host PCI attachment only supported with ICH9 chipset"));
6787 }
6788
6789 // check if device with this host PCI address already attached
6790 for (HWData::PCIDeviceAssignmentList::const_iterator
6791 it = mHWData->mPCIDeviceAssignments.begin();
6792 it != mHWData->mPCIDeviceAssignments.end();
6793 ++it)
6794 {
6795 LONG iHostAddress = -1;
6796 ComPtr<PCIDeviceAttachment> pAttach;
6797 pAttach = *it;
6798 pAttach->COMGETTER(HostAddress)(&iHostAddress);
6799 if (iHostAddress == aHostAddress)
6800 return setError(E_INVALIDARG,
6801 tr("Device with host PCI address already attached to this VM"));
6802 }
6803
6804 ComObjPtr<PCIDeviceAttachment> pda;
6805 char name[32];
6806
6807 RTStrPrintf(name, sizeof(name), "host%02x:%02x.%x", (aHostAddress>>8) & 0xff,
6808 (aHostAddress & 0xf8) >> 3, aHostAddress & 7);
6809 Bstr bname(name);
6810 pda.createObject();
6811 pda->init(this, bname, aHostAddress, aDesiredGuestAddress, TRUE);
6812 i_setModified(IsModified_MachineData);
6813 mHWData.backup();
6814 mHWData->mPCIDeviceAssignments.push_back(pda);
6815 }
6816
6817 return S_OK;
6818}
6819
6820/**
6821 * Currently this method doesn't detach device from the running VM,
6822 * just makes sure it's not plugged on next VM start.
6823 */
6824HRESULT Machine::detachHostPCIDevice(LONG aHostAddress)
6825{
6826 ComObjPtr<PCIDeviceAttachment> pAttach;
6827 bool fRemoved = false;
6828 HRESULT rc;
6829
6830 // lock scope
6831 {
6832 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
6833
6834 rc = i_checkStateDependency(MutableStateDep);
6835 if (FAILED(rc)) return rc;
6836
6837 for (HWData::PCIDeviceAssignmentList::const_iterator
6838 it = mHWData->mPCIDeviceAssignments.begin();
6839 it != mHWData->mPCIDeviceAssignments.end();
6840 ++it)
6841 {
6842 LONG iHostAddress = -1;
6843 pAttach = *it;
6844 pAttach->COMGETTER(HostAddress)(&iHostAddress);
6845 if (iHostAddress != -1 && iHostAddress == aHostAddress)
6846 {
6847 i_setModified(IsModified_MachineData);
6848 mHWData.backup();
6849 mHWData->mPCIDeviceAssignments.remove(pAttach);
6850 fRemoved = true;
6851 break;
6852 }
6853 }
6854 }
6855
6856
6857 /* Fire event outside of the lock */
6858 if (fRemoved)
6859 {
6860 Assert(!pAttach.isNull());
6861 ComPtr<IEventSource> es;
6862 rc = mParent->COMGETTER(EventSource)(es.asOutParam());
6863 Assert(SUCCEEDED(rc));
6864 Bstr mid;
6865 rc = this->COMGETTER(Id)(mid.asOutParam());
6866 Assert(SUCCEEDED(rc));
6867 fireHostPCIDevicePlugEvent(es, mid.raw(), false /* unplugged */, true /* success */, pAttach, NULL);
6868 }
6869
6870 return fRemoved ? S_OK : setError(VBOX_E_OBJECT_NOT_FOUND,
6871 tr("No host PCI device %08x attached"),
6872 aHostAddress
6873 );
6874}
6875
6876HRESULT Machine::getPCIDeviceAssignments(std::vector<ComPtr<IPCIDeviceAttachment> > &aPCIDeviceAssignments)
6877{
6878 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
6879
6880 aPCIDeviceAssignments.resize(mHWData->mPCIDeviceAssignments.size());
6881 size_t i = 0;
6882 for (std::list<ComObjPtr<PCIDeviceAttachment> >::const_iterator
6883 it = mHWData->mPCIDeviceAssignments.begin();
6884 it != mHWData->mPCIDeviceAssignments.end();
6885 ++it, ++i)
6886 (*it).queryInterfaceTo(aPCIDeviceAssignments[i].asOutParam());
6887
6888 return S_OK;
6889}
6890
6891HRESULT Machine::getBandwidthControl(ComPtr<IBandwidthControl> &aBandwidthControl)
6892{
6893 mBandwidthControl.queryInterfaceTo(aBandwidthControl.asOutParam());
6894
6895 return S_OK;
6896}
6897
6898HRESULT Machine::getTracingEnabled(BOOL *aTracingEnabled)
6899{
6900 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
6901
6902 *aTracingEnabled = mHWData->mDebugging.fTracingEnabled;
6903
6904 return S_OK;
6905}
6906
6907HRESULT Machine::setTracingEnabled(BOOL aTracingEnabled)
6908{
6909 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
6910 HRESULT hrc = i_checkStateDependency(MutableStateDep);
6911 if (SUCCEEDED(hrc))
6912 {
6913 hrc = mHWData.backupEx();
6914 if (SUCCEEDED(hrc))
6915 {
6916 i_setModified(IsModified_MachineData);
6917 mHWData->mDebugging.fTracingEnabled = aTracingEnabled != FALSE;
6918 }
6919 }
6920 return hrc;
6921}
6922
6923HRESULT Machine::getTracingConfig(com::Utf8Str &aTracingConfig)
6924{
6925 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
6926 aTracingConfig = mHWData->mDebugging.strTracingConfig;
6927 return S_OK;
6928}
6929
6930HRESULT Machine::setTracingConfig(const com::Utf8Str &aTracingConfig)
6931{
6932 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
6933 HRESULT hrc = i_checkStateDependency(MutableStateDep);
6934 if (SUCCEEDED(hrc))
6935 {
6936 hrc = mHWData.backupEx();
6937 if (SUCCEEDED(hrc))
6938 {
6939 mHWData->mDebugging.strTracingConfig = aTracingConfig;
6940 if (SUCCEEDED(hrc))
6941 i_setModified(IsModified_MachineData);
6942 }
6943 }
6944 return hrc;
6945}
6946
6947HRESULT Machine::getAllowTracingToAccessVM(BOOL *aAllowTracingToAccessVM)
6948{
6949 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
6950
6951 *aAllowTracingToAccessVM = mHWData->mDebugging.fAllowTracingToAccessVM;
6952
6953 return S_OK;
6954}
6955
6956HRESULT Machine::setAllowTracingToAccessVM(BOOL aAllowTracingToAccessVM)
6957{
6958 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
6959 HRESULT hrc = i_checkStateDependency(MutableStateDep);
6960 if (SUCCEEDED(hrc))
6961 {
6962 hrc = mHWData.backupEx();
6963 if (SUCCEEDED(hrc))
6964 {
6965 i_setModified(IsModified_MachineData);
6966 mHWData->mDebugging.fAllowTracingToAccessVM = aAllowTracingToAccessVM != FALSE;
6967 }
6968 }
6969 return hrc;
6970}
6971
6972HRESULT Machine::getAutostartEnabled(BOOL *aAutostartEnabled)
6973{
6974 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
6975
6976 *aAutostartEnabled = mHWData->mAutostart.fAutostartEnabled;
6977
6978 return S_OK;
6979}
6980
6981HRESULT Machine::setAutostartEnabled(BOOL aAutostartEnabled)
6982{
6983 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
6984
6985 HRESULT hrc = i_checkStateDependency(MutableOrSavedOrRunningStateDep);
6986 if ( SUCCEEDED(hrc)
6987 && mHWData->mAutostart.fAutostartEnabled != !!aAutostartEnabled)
6988 {
6989 AutostartDb *autostartDb = mParent->i_getAutostartDb();
6990 int vrc;
6991
6992 if (aAutostartEnabled)
6993 vrc = autostartDb->addAutostartVM(mUserData->s.strName.c_str());
6994 else
6995 vrc = autostartDb->removeAutostartVM(mUserData->s.strName.c_str());
6996
6997 if (RT_SUCCESS(vrc))
6998 {
6999 hrc = mHWData.backupEx();
7000 if (SUCCEEDED(hrc))
7001 {
7002 i_setModified(IsModified_MachineData);
7003 mHWData->mAutostart.fAutostartEnabled = aAutostartEnabled != FALSE;
7004 }
7005 }
7006 else if (vrc == VERR_NOT_SUPPORTED)
7007 hrc = setError(VBOX_E_NOT_SUPPORTED,
7008 tr("The VM autostart feature is not supported on this platform"));
7009 else if (vrc == VERR_PATH_NOT_FOUND)
7010 hrc = setError(E_FAIL,
7011 tr("The path to the autostart database is not set"));
7012 else
7013 hrc = setError(E_UNEXPECTED,
7014 tr("%s machine '%s' to the autostart database failed with %Rrc"),
7015 aAutostartEnabled ? "Adding" : "Removing",
7016 mUserData->s.strName.c_str(), vrc);
7017 }
7018 return hrc;
7019}
7020
7021HRESULT Machine::getAutostartDelay(ULONG *aAutostartDelay)
7022{
7023 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
7024
7025 *aAutostartDelay = mHWData->mAutostart.uAutostartDelay;
7026
7027 return S_OK;
7028}
7029
7030HRESULT Machine::setAutostartDelay(ULONG aAutostartDelay)
7031{
7032 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
7033 HRESULT hrc = i_checkStateDependency(MutableOrSavedOrRunningStateDep);
7034 if (SUCCEEDED(hrc))
7035 {
7036 hrc = mHWData.backupEx();
7037 if (SUCCEEDED(hrc))
7038 {
7039 i_setModified(IsModified_MachineData);
7040 mHWData->mAutostart.uAutostartDelay = aAutostartDelay;
7041 }
7042 }
7043 return hrc;
7044}
7045
7046HRESULT Machine::getAutostopType(AutostopType_T *aAutostopType)
7047{
7048 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
7049
7050 *aAutostopType = mHWData->mAutostart.enmAutostopType;
7051
7052 return S_OK;
7053}
7054
7055HRESULT Machine::setAutostopType(AutostopType_T aAutostopType)
7056{
7057 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
7058 HRESULT hrc = i_checkStateDependency(MutableOrSavedOrRunningStateDep);
7059 if ( SUCCEEDED(hrc)
7060 && mHWData->mAutostart.enmAutostopType != aAutostopType)
7061 {
7062 AutostartDb *autostartDb = mParent->i_getAutostartDb();
7063 int vrc;
7064
7065 if (aAutostopType != AutostopType_Disabled)
7066 vrc = autostartDb->addAutostopVM(mUserData->s.strName.c_str());
7067 else
7068 vrc = autostartDb->removeAutostopVM(mUserData->s.strName.c_str());
7069
7070 if (RT_SUCCESS(vrc))
7071 {
7072 hrc = mHWData.backupEx();
7073 if (SUCCEEDED(hrc))
7074 {
7075 i_setModified(IsModified_MachineData);
7076 mHWData->mAutostart.enmAutostopType = aAutostopType;
7077 }
7078 }
7079 else if (vrc == VERR_NOT_SUPPORTED)
7080 hrc = setError(VBOX_E_NOT_SUPPORTED,
7081 tr("The VM autostop feature is not supported on this platform"));
7082 else if (vrc == VERR_PATH_NOT_FOUND)
7083 hrc = setError(E_FAIL,
7084 tr("The path to the autostart database is not set"));
7085 else
7086 hrc = setError(E_UNEXPECTED,
7087 tr("%s machine '%s' to the autostop database failed with %Rrc"),
7088 aAutostopType != AutostopType_Disabled ? "Adding" : "Removing",
7089 mUserData->s.strName.c_str(), vrc);
7090 }
7091 return hrc;
7092}
7093
7094HRESULT Machine::getDefaultFrontend(com::Utf8Str &aDefaultFrontend)
7095{
7096 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
7097
7098 aDefaultFrontend = mHWData->mDefaultFrontend;
7099
7100 return S_OK;
7101}
7102
7103HRESULT Machine::setDefaultFrontend(const com::Utf8Str &aDefaultFrontend)
7104{
7105 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
7106 HRESULT hrc = i_checkStateDependency(MutableOrSavedStateDep);
7107 if (SUCCEEDED(hrc))
7108 {
7109 hrc = mHWData.backupEx();
7110 if (SUCCEEDED(hrc))
7111 {
7112 i_setModified(IsModified_MachineData);
7113 mHWData->mDefaultFrontend = aDefaultFrontend;
7114 }
7115 }
7116 return hrc;
7117}
7118
7119HRESULT Machine::getIcon(std::vector<BYTE> &aIcon)
7120{
7121 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
7122 size_t cbIcon = mUserData->s.ovIcon.size();
7123 aIcon.resize(cbIcon);
7124 if (cbIcon)
7125 memcpy(&aIcon.front(), &mUserData->s.ovIcon[0], cbIcon);
7126 return S_OK;
7127}
7128
7129HRESULT Machine::setIcon(const std::vector<BYTE> &aIcon)
7130{
7131 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
7132 HRESULT hrc = i_checkStateDependency(MutableOrSavedStateDep);
7133 if (SUCCEEDED(hrc))
7134 {
7135 i_setModified(IsModified_MachineData);
7136 mUserData.backup();
7137 size_t cbIcon = aIcon.size();
7138 mUserData->s.ovIcon.resize(cbIcon);
7139 if (cbIcon)
7140 memcpy(&mUserData->s.ovIcon[0], &aIcon.front(), cbIcon);
7141 }
7142 return hrc;
7143}
7144
7145HRESULT Machine::getUSBProxyAvailable(BOOL *aUSBProxyAvailable)
7146{
7147#ifdef VBOX_WITH_USB
7148 *aUSBProxyAvailable = true;
7149#else
7150 *aUSBProxyAvailable = false;
7151#endif
7152 return S_OK;
7153}
7154
7155HRESULT Machine::getVMProcessPriority(com::Utf8Str &aVMProcessPriority)
7156{
7157 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
7158
7159 aVMProcessPriority = mUserData->s.strVMPriority;
7160
7161 return S_OK;
7162}
7163
7164HRESULT Machine::setVMProcessPriority(const com::Utf8Str &aVMProcessPriority)
7165{
7166 RT_NOREF(aVMProcessPriority);
7167 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
7168 HRESULT hrc = i_checkStateDependency(MutableOrSavedOrRunningStateDep);
7169 if (SUCCEEDED(hrc))
7170 {
7171 /** @todo r=klaus: currently this is marked as not implemented, as
7172 * the code for setting the priority of the process is not there
7173 * (neither when starting the VM nor at runtime). */
7174 ReturnComNotImplemented();
7175#if 0
7176 hrc = mUserData.backupEx();
7177 if (SUCCEEDED(hrc))
7178 {
7179 i_setModified(IsModified_MachineData);
7180 mUserData->s.strVMPriority = aVMProcessPriority;
7181 }
7182#endif
7183 }
7184 return hrc;
7185}
7186
7187HRESULT Machine::getUnattended(ComPtr<IUnattended> &aUnattended)
7188{
7189#ifdef VBOX_WITH_UNATTENDED
7190 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
7191
7192 aUnattended = mUnattended;
7193
7194 return S_OK;
7195#else
7196 NOREF(aUnattended);
7197 return E_NOTIMPL;
7198#endif
7199}
7200
7201HRESULT Machine::cloneTo(const ComPtr<IMachine> &aTarget, CloneMode_T aMode, const std::vector<CloneOptions_T> &aOptions,
7202 ComPtr<IProgress> &aProgress)
7203{
7204 ComObjPtr<Progress> pP;
7205 Progress *ppP = pP;
7206 IProgress *iP = static_cast<IProgress *>(ppP);
7207 IProgress **pProgress = &iP;
7208
7209 IMachine *pTarget = aTarget;
7210
7211 /* Convert the options. */
7212 RTCList<CloneOptions_T> optList;
7213 if (aOptions.size())
7214 for (size_t i = 0; i < aOptions.size(); ++i)
7215 optList.append(aOptions[i]);
7216
7217 if (optList.contains(CloneOptions_Link))
7218 {
7219 if (!i_isSnapshotMachine())
7220 return setError(E_INVALIDARG,
7221 tr("Linked clone can only be created from a snapshot"));
7222 if (aMode != CloneMode_MachineState)
7223 return setError(E_INVALIDARG,
7224 tr("Linked clone can only be created for a single machine state"));
7225 }
7226 AssertReturn(!(optList.contains(CloneOptions_KeepAllMACs) && optList.contains(CloneOptions_KeepNATMACs)), E_INVALIDARG);
7227
7228 MachineCloneVM *pWorker = new MachineCloneVM(this, static_cast<Machine*>(pTarget), aMode, optList);
7229
7230 HRESULT rc = pWorker->start(pProgress);
7231
7232 pP = static_cast<Progress *>(*pProgress);
7233 pP.queryInterfaceTo(aProgress.asOutParam());
7234
7235 return rc;
7236
7237}
7238
7239HRESULT Machine::saveState(ComPtr<IProgress> &aProgress)
7240{
7241 NOREF(aProgress);
7242 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
7243
7244 // This check should always fail.
7245 HRESULT rc = i_checkStateDependency(MutableStateDep);
7246 if (FAILED(rc)) return rc;
7247
7248 AssertFailedReturn(E_NOTIMPL);
7249}
7250
7251HRESULT Machine::adoptSavedState(const com::Utf8Str &aSavedStateFile)
7252{
7253 NOREF(aSavedStateFile);
7254 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
7255
7256 // This check should always fail.
7257 HRESULT rc = i_checkStateDependency(MutableStateDep);
7258 if (FAILED(rc)) return rc;
7259
7260 AssertFailedReturn(E_NOTIMPL);
7261}
7262
7263HRESULT Machine::discardSavedState(BOOL aFRemoveFile)
7264{
7265 NOREF(aFRemoveFile);
7266 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
7267
7268 // This check should always fail.
7269 HRESULT rc = i_checkStateDependency(MutableOrSavedStateDep);
7270 if (FAILED(rc)) return rc;
7271
7272 AssertFailedReturn(E_NOTIMPL);
7273}
7274
7275// public methods for internal purposes
7276/////////////////////////////////////////////////////////////////////////////
7277
7278/**
7279 * Adds the given IsModified_* flag to the dirty flags of the machine.
7280 * This must be called either during i_loadSettings or under the machine write lock.
7281 * @param fl Flag
7282 * @param fAllowStateModification If state modifications are allowed.
7283 */
7284void Machine::i_setModified(uint32_t fl, bool fAllowStateModification /* = true */)
7285{
7286 mData->flModifications |= fl;
7287 if (fAllowStateModification && i_isStateModificationAllowed())
7288 mData->mCurrentStateModified = true;
7289}
7290
7291/**
7292 * Adds the given IsModified_* flag to the dirty flags of the machine, taking
7293 * care of the write locking.
7294 *
7295 * @param fModification The flag to add.
7296 * @param fAllowStateModification If state modifications are allowed.
7297 */
7298void Machine::i_setModifiedLock(uint32_t fModification, bool fAllowStateModification /* = true */)
7299{
7300 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
7301 i_setModified(fModification, fAllowStateModification);
7302}
7303
7304/**
7305 * Saves the registry entry of this machine to the given configuration node.
7306 *
7307 * @param data Machine registry data.
7308 *
7309 * @note locks this object for reading.
7310 */
7311HRESULT Machine::i_saveRegistryEntry(settings::MachineRegistryEntry &data)
7312{
7313 AutoLimitedCaller autoCaller(this);
7314 AssertComRCReturnRC(autoCaller.rc());
7315
7316 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
7317
7318 data.uuid = mData->mUuid;
7319 data.strSettingsFile = mData->m_strConfigFile;
7320
7321 return S_OK;
7322}
7323
7324/**
7325 * Calculates the absolute path of the given path taking the directory of the
7326 * machine settings file as the current directory.
7327 *
7328 * @param strPath Path to calculate the absolute path for.
7329 * @param aResult Where to put the result (used only on success, can be the
7330 * same Utf8Str instance as passed in @a aPath).
7331 * @return IPRT result.
7332 *
7333 * @note Locks this object for reading.
7334 */
7335int Machine::i_calculateFullPath(const Utf8Str &strPath, Utf8Str &aResult)
7336{
7337 AutoCaller autoCaller(this);
7338 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
7339
7340 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
7341
7342 AssertReturn(!mData->m_strConfigFileFull.isEmpty(), VERR_GENERAL_FAILURE);
7343
7344 Utf8Str strSettingsDir = mData->m_strConfigFileFull;
7345
7346 strSettingsDir.stripFilename();
7347 char folder[RTPATH_MAX];
7348 int vrc = RTPathAbsEx(strSettingsDir.c_str(), strPath.c_str(), folder, sizeof(folder));
7349 if (RT_SUCCESS(vrc))
7350 aResult = folder;
7351
7352 return vrc;
7353}
7354
7355/**
7356 * Copies strSource to strTarget, making it relative to the machine folder
7357 * if it is a subdirectory thereof, or simply copying it otherwise.
7358 *
7359 * @param strSource Path to evaluate and copy.
7360 * @param strTarget Buffer to receive target path.
7361 *
7362 * @note Locks this object for reading.
7363 */
7364void Machine::i_copyPathRelativeToMachine(const Utf8Str &strSource,
7365 Utf8Str &strTarget)
7366{
7367 AutoCaller autoCaller(this);
7368 AssertComRCReturn(autoCaller.rc(), (void)0);
7369
7370 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
7371
7372 AssertReturnVoid(!mData->m_strConfigFileFull.isEmpty());
7373 // use strTarget as a temporary buffer to hold the machine settings dir
7374 strTarget = mData->m_strConfigFileFull;
7375 strTarget.stripFilename();
7376 if (RTPathStartsWith(strSource.c_str(), strTarget.c_str()))
7377 {
7378 // is relative: then append what's left
7379 strTarget = strSource.substr(strTarget.length() + 1); // skip '/'
7380 // for empty paths (only possible for subdirs) use "." to avoid
7381 // triggering default settings for not present config attributes.
7382 if (strTarget.isEmpty())
7383 strTarget = ".";
7384 }
7385 else
7386 // is not relative: then overwrite
7387 strTarget = strSource;
7388}
7389
7390/**
7391 * Returns the full path to the machine's log folder in the
7392 * \a aLogFolder argument.
7393 */
7394void Machine::i_getLogFolder(Utf8Str &aLogFolder)
7395{
7396 AutoCaller autoCaller(this);
7397 AssertComRCReturnVoid(autoCaller.rc());
7398
7399 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
7400
7401 char szTmp[RTPATH_MAX];
7402 int vrc = RTEnvGetEx(RTENV_DEFAULT, "VBOX_USER_VMLOGDIR", szTmp, sizeof(szTmp), NULL);
7403 if (RT_SUCCESS(vrc))
7404 {
7405 if (szTmp[0] && !mUserData.isNull())
7406 {
7407 char szTmp2[RTPATH_MAX];
7408 vrc = RTPathAbs(szTmp, szTmp2, sizeof(szTmp2));
7409 if (RT_SUCCESS(vrc))
7410 aLogFolder = BstrFmt("%s%c%s",
7411 szTmp2,
7412 RTPATH_DELIMITER,
7413 mUserData->s.strName.c_str()); // path/to/logfolder/vmname
7414 }
7415 else
7416 vrc = VERR_PATH_IS_RELATIVE;
7417 }
7418
7419 if (RT_FAILURE(vrc))
7420 {
7421 // fallback if VBOX_USER_LOGHOME is not set or invalid
7422 aLogFolder = mData->m_strConfigFileFull; // path/to/machinesfolder/vmname/vmname.vbox
7423 aLogFolder.stripFilename(); // path/to/machinesfolder/vmname
7424 aLogFolder.append(RTPATH_DELIMITER);
7425 aLogFolder.append("Logs"); // path/to/machinesfolder/vmname/Logs
7426 }
7427}
7428
7429/**
7430 * Returns the full path to the machine's log file for an given index.
7431 */
7432Utf8Str Machine::i_getLogFilename(ULONG idx)
7433{
7434 Utf8Str logFolder;
7435 getLogFolder(logFolder);
7436 Assert(logFolder.length());
7437
7438 Utf8Str log;
7439 if (idx == 0)
7440 log = Utf8StrFmt("%s%cVBox.log", logFolder.c_str(), RTPATH_DELIMITER);
7441#if defined(RT_OS_WINDOWS) && defined(VBOX_WITH_HARDENING)
7442 else if (idx == 1)
7443 log = Utf8StrFmt("%s%cVBoxHardening.log", logFolder.c_str(), RTPATH_DELIMITER);
7444 else
7445 log = Utf8StrFmt("%s%cVBox.log.%u", logFolder.c_str(), RTPATH_DELIMITER, idx - 1);
7446#else
7447 else
7448 log = Utf8StrFmt("%s%cVBox.log.%u", logFolder.c_str(), RTPATH_DELIMITER, idx);
7449#endif
7450 return log;
7451}
7452
7453/**
7454 * Returns the full path to the machine's hardened log file.
7455 */
7456Utf8Str Machine::i_getHardeningLogFilename(void)
7457{
7458 Utf8Str strFilename;
7459 getLogFolder(strFilename);
7460 Assert(strFilename.length());
7461 strFilename.append(RTPATH_SLASH_STR "VBoxHardening.log");
7462 return strFilename;
7463}
7464
7465
7466/**
7467 * Composes a unique saved state filename based on the current system time. The filename is
7468 * granular to the second so this will work so long as no more than one snapshot is taken on
7469 * a machine per second.
7470 *
7471 * Before version 4.1, we used this formula for saved state files:
7472 * Utf8StrFmt("%s%c{%RTuuid}.sav", strFullSnapshotFolder.c_str(), RTPATH_DELIMITER, mData->mUuid.raw())
7473 * which no longer works because saved state files can now be shared between the saved state of the
7474 * "saved" machine and an online snapshot, and the following would cause problems:
7475 * 1) save machine
7476 * 2) create online snapshot from that machine state --> reusing saved state file
7477 * 3) save machine again --> filename would be reused, breaking the online snapshot
7478 *
7479 * So instead we now use a timestamp.
7480 *
7481 * @param strStateFilePath
7482 */
7483
7484void Machine::i_composeSavedStateFilename(Utf8Str &strStateFilePath)
7485{
7486 AutoCaller autoCaller(this);
7487 AssertComRCReturnVoid(autoCaller.rc());
7488
7489 {
7490 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
7491 i_calculateFullPath(mUserData->s.strSnapshotFolder, strStateFilePath);
7492 }
7493
7494 RTTIMESPEC ts;
7495 RTTimeNow(&ts);
7496 RTTIME time;
7497 RTTimeExplode(&time, &ts);
7498
7499 strStateFilePath += RTPATH_DELIMITER;
7500 strStateFilePath += Utf8StrFmt("%04d-%02u-%02uT%02u-%02u-%02u-%09uZ.sav",
7501 time.i32Year, time.u8Month, time.u8MonthDay,
7502 time.u8Hour, time.u8Minute, time.u8Second, time.u32Nanosecond);
7503}
7504
7505/**
7506 * Returns the full path to the default video capture file.
7507 */
7508void Machine::i_getDefaultVideoCaptureFile(Utf8Str &strFile)
7509{
7510 AutoCaller autoCaller(this);
7511 AssertComRCReturnVoid(autoCaller.rc());
7512
7513 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
7514
7515 strFile = mData->m_strConfigFileFull; // path/to/machinesfolder/vmname/vmname.vbox
7516 strFile.stripSuffix(); // path/to/machinesfolder/vmname/vmname
7517 strFile.append(".webm"); // path/to/machinesfolder/vmname/vmname.webm
7518}
7519
7520/**
7521 * Returns whether at least one USB controller is present for the VM.
7522 */
7523bool Machine::i_isUSBControllerPresent()
7524{
7525 AutoCaller autoCaller(this);
7526 AssertComRCReturn(autoCaller.rc(), false);
7527
7528 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
7529
7530 return (mUSBControllers->size() > 0);
7531}
7532
7533/**
7534 * @note Locks this object for writing, calls the client process
7535 * (inside the lock).
7536 */
7537HRESULT Machine::i_launchVMProcess(IInternalSessionControl *aControl,
7538 const Utf8Str &strFrontend,
7539 const Utf8Str &strEnvironment,
7540 ProgressProxy *aProgress)
7541{
7542 LogFlowThisFuncEnter();
7543
7544 AssertReturn(aControl, E_FAIL);
7545 AssertReturn(aProgress, E_FAIL);
7546 AssertReturn(!strFrontend.isEmpty(), E_FAIL);
7547
7548 AutoCaller autoCaller(this);
7549 if (FAILED(autoCaller.rc())) return autoCaller.rc();
7550
7551 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
7552
7553 if (!mData->mRegistered)
7554 return setError(E_UNEXPECTED,
7555 tr("The machine '%s' is not registered"),
7556 mUserData->s.strName.c_str());
7557
7558 LogFlowThisFunc(("mSession.mState=%s\n", Global::stringifySessionState(mData->mSession.mState)));
7559
7560 /* The process started when launching a VM with separate UI/VM processes is always
7561 * the UI process, i.e. needs special handling as it won't claim the session. */
7562 bool fSeparate = strFrontend.endsWith("separate", Utf8Str::CaseInsensitive);
7563
7564 if (fSeparate)
7565 {
7566 if (mData->mSession.mState != SessionState_Unlocked && mData->mSession.mName != "headless")
7567 return setError(VBOX_E_INVALID_OBJECT_STATE,
7568 tr("The machine '%s' is in a state which is incompatible with launching a separate UI process"),
7569 mUserData->s.strName.c_str());
7570 }
7571 else
7572 {
7573 if ( mData->mSession.mState == SessionState_Locked
7574 || mData->mSession.mState == SessionState_Spawning
7575 || mData->mSession.mState == SessionState_Unlocking)
7576 return setError(VBOX_E_INVALID_OBJECT_STATE,
7577 tr("The machine '%s' is already locked by a session (or being locked or unlocked)"),
7578 mUserData->s.strName.c_str());
7579
7580 /* may not be busy */
7581 AssertReturn(!Global::IsOnlineOrTransient(mData->mMachineState), E_FAIL);
7582 }
7583
7584 /* get the path to the executable */
7585 char szPath[RTPATH_MAX];
7586 RTPathAppPrivateArch(szPath, sizeof(szPath) - 1);
7587 size_t cchBufLeft = strlen(szPath);
7588 szPath[cchBufLeft++] = RTPATH_DELIMITER;
7589 szPath[cchBufLeft] = 0;
7590 char *pszNamePart = szPath + cchBufLeft;
7591 cchBufLeft = sizeof(szPath) - cchBufLeft;
7592
7593 int vrc = VINF_SUCCESS;
7594 RTPROCESS pid = NIL_RTPROCESS;
7595
7596 RTENV env = RTENV_DEFAULT;
7597
7598 if (!strEnvironment.isEmpty())
7599 {
7600 char *newEnvStr = NULL;
7601
7602 do
7603 {
7604 /* clone the current environment */
7605 int vrc2 = RTEnvClone(&env, RTENV_DEFAULT);
7606 AssertRCBreakStmt(vrc2, vrc = vrc2);
7607
7608 newEnvStr = RTStrDup(strEnvironment.c_str());
7609 AssertPtrBreakStmt(newEnvStr, vrc = vrc2);
7610
7611 /* put new variables to the environment
7612 * (ignore empty variable names here since RTEnv API
7613 * intentionally doesn't do that) */
7614 char *var = newEnvStr;
7615 for (char *p = newEnvStr; *p; ++p)
7616 {
7617 if (*p == '\n' && (p == newEnvStr || *(p - 1) != '\\'))
7618 {
7619 *p = '\0';
7620 if (*var)
7621 {
7622 char *val = strchr(var, '=');
7623 if (val)
7624 {
7625 *val++ = '\0';
7626 vrc2 = RTEnvSetEx(env, var, val);
7627 }
7628 else
7629 vrc2 = RTEnvUnsetEx(env, var);
7630 if (RT_FAILURE(vrc2))
7631 break;
7632 }
7633 var = p + 1;
7634 }
7635 }
7636 if (RT_SUCCESS(vrc2) && *var)
7637 vrc2 = RTEnvPutEx(env, var);
7638
7639 AssertRCBreakStmt(vrc2, vrc = vrc2);
7640 }
7641 while (0);
7642
7643 if (newEnvStr != NULL)
7644 RTStrFree(newEnvStr);
7645 }
7646
7647 /* Hardening logging */
7648#if defined(RT_OS_WINDOWS) && defined(VBOX_WITH_HARDENING)
7649 Utf8Str strSupHardeningLogArg("--sup-hardening-log=");
7650 {
7651 Utf8Str strHardeningLogFile = i_getHardeningLogFilename();
7652 int vrc2 = RTFileDelete(strHardeningLogFile.c_str());
7653 if (vrc2 == VERR_PATH_NOT_FOUND || vrc2 == VERR_FILE_NOT_FOUND)
7654 {
7655 Utf8Str strStartupLogDir = strHardeningLogFile;
7656 strStartupLogDir.stripFilename();
7657 RTDirCreateFullPath(strStartupLogDir.c_str(), 0755); /** @todo add a variant for creating the path to a
7658 file without stripping the file. */
7659 }
7660 strSupHardeningLogArg.append(strHardeningLogFile);
7661
7662 /* Remove legacy log filename to avoid confusion. */
7663 Utf8Str strOldStartupLogFile;
7664 getLogFolder(strOldStartupLogFile);
7665 strOldStartupLogFile.append(RTPATH_SLASH_STR "VBoxStartup.log");
7666 RTFileDelete(strOldStartupLogFile.c_str());
7667 }
7668 const char *pszSupHardeningLogArg = strSupHardeningLogArg.c_str();
7669#else
7670 const char *pszSupHardeningLogArg = NULL;
7671#endif
7672
7673 Utf8Str strCanonicalName;
7674
7675#ifdef VBOX_WITH_QTGUI
7676 if ( !strFrontend.compare("gui", Utf8Str::CaseInsensitive)
7677 || !strFrontend.compare("GUI/Qt", Utf8Str::CaseInsensitive)
7678 || !strFrontend.compare("separate", Utf8Str::CaseInsensitive)
7679 || !strFrontend.compare("gui/separate", Utf8Str::CaseInsensitive)
7680 || !strFrontend.compare("GUI/Qt/separate", Utf8Str::CaseInsensitive))
7681 {
7682 strCanonicalName = "GUI/Qt";
7683# ifdef RT_OS_DARWIN /* Avoid Launch Services confusing this with the selector by using a helper app. */
7684 /* Modify the base path so that we don't need to use ".." below. */
7685 RTPathStripTrailingSlash(szPath);
7686 RTPathStripFilename(szPath);
7687 cchBufLeft = strlen(szPath);
7688 pszNamePart = szPath + cchBufLeft;
7689 cchBufLeft = sizeof(szPath) - cchBufLeft;
7690
7691# define OSX_APP_NAME "VirtualBoxVM"
7692# define OSX_APP_PATH_FMT "/Resources/%s.app/Contents/MacOS/VirtualBoxVM"
7693
7694 Utf8Str strAppOverride = i_getExtraData(Utf8Str("VBoxInternal2/VirtualBoxVMAppOverride"));
7695 if ( strAppOverride.contains(".")
7696 || strAppOverride.contains("/")
7697 || strAppOverride.contains("\\")
7698 || strAppOverride.contains(":"))
7699 strAppOverride.setNull();
7700 Utf8Str strAppPath;
7701 if (!strAppOverride.isEmpty())
7702 {
7703 strAppPath = Utf8StrFmt(OSX_APP_PATH_FMT, strAppOverride.c_str());
7704 Utf8Str strFullPath(szPath);
7705 strFullPath.append(strAppPath);
7706 /* there is a race, but people using this deserve the failure */
7707 if (!RTFileExists(strFullPath.c_str()))
7708 strAppOverride.setNull();
7709 }
7710 if (strAppOverride.isEmpty())
7711 strAppPath = Utf8StrFmt(OSX_APP_PATH_FMT, OSX_APP_NAME);
7712 AssertReturn(cchBufLeft > strAppPath.length(), E_UNEXPECTED);
7713 strcpy(pszNamePart, strAppPath.c_str());
7714# else
7715 static const char s_szVirtualBox_exe[] = "VirtualBox" HOSTSUFF_EXE;
7716 Assert(cchBufLeft >= sizeof(s_szVirtualBox_exe));
7717 strcpy(pszNamePart, s_szVirtualBox_exe);
7718# endif
7719
7720 Utf8Str idStr = mData->mUuid.toString();
7721 const char *apszArgs[] =
7722 {
7723 szPath,
7724 "--comment", mUserData->s.strName.c_str(),
7725 "--startvm", idStr.c_str(),
7726 "--no-startvm-errormsgbox",
7727 NULL, /* For "--separate". */
7728 NULL, /* For "--sup-startup-log". */
7729 NULL
7730 };
7731 unsigned iArg = 6;
7732 if (fSeparate)
7733 apszArgs[iArg++] = "--separate";
7734 apszArgs[iArg++] = pszSupHardeningLogArg;
7735
7736 vrc = RTProcCreate(szPath, apszArgs, env, 0, &pid);
7737 }
7738#else /* !VBOX_WITH_QTGUI */
7739 if (0)
7740 ;
7741#endif /* VBOX_WITH_QTGUI */
7742
7743 else
7744
7745#ifdef VBOX_WITH_VBOXSDL
7746 if ( !strFrontend.compare("sdl", Utf8Str::CaseInsensitive)
7747 || !strFrontend.compare("GUI/SDL", Utf8Str::CaseInsensitive)
7748 || !strFrontend.compare("sdl/separate", Utf8Str::CaseInsensitive)
7749 || !strFrontend.compare("GUI/SDL/separate", Utf8Str::CaseInsensitive))
7750 {
7751 strCanonicalName = "GUI/SDL";
7752 static const char s_szVBoxSDL_exe[] = "VBoxSDL" HOSTSUFF_EXE;
7753 Assert(cchBufLeft >= sizeof(s_szVBoxSDL_exe));
7754 strcpy(pszNamePart, s_szVBoxSDL_exe);
7755
7756 Utf8Str idStr = mData->mUuid.toString();
7757 const char *apszArgs[] =
7758 {
7759 szPath,
7760 "--comment", mUserData->s.strName.c_str(),
7761 "--startvm", idStr.c_str(),
7762 NULL, /* For "--separate". */
7763 NULL, /* For "--sup-startup-log". */
7764 NULL
7765 };
7766 unsigned iArg = 5;
7767 if (fSeparate)
7768 apszArgs[iArg++] = "--separate";
7769 apszArgs[iArg++] = pszSupHardeningLogArg;
7770
7771 vrc = RTProcCreate(szPath, apszArgs, env, 0, &pid);
7772 }
7773#else /* !VBOX_WITH_VBOXSDL */
7774 if (0)
7775 ;
7776#endif /* !VBOX_WITH_VBOXSDL */
7777
7778 else
7779
7780#ifdef VBOX_WITH_HEADLESS
7781 if ( !strFrontend.compare("headless", Utf8Str::CaseInsensitive)
7782 || !strFrontend.compare("capture", Utf8Str::CaseInsensitive)
7783 || !strFrontend.compare("vrdp", Utf8Str::CaseInsensitive) /* Deprecated. Same as headless. */
7784 )
7785 {
7786 strCanonicalName = "headless";
7787 /* On pre-4.0 the "headless" type was used for passing "--vrdp off" to VBoxHeadless to let it work in OSE,
7788 * which did not contain VRDP server. In VBox 4.0 the remote desktop server (VRDE) is optional,
7789 * and a VM works even if the server has not been installed.
7790 * So in 4.0 the "headless" behavior remains the same for default VBox installations.
7791 * Only if a VRDE has been installed and the VM enables it, the "headless" will work
7792 * differently in 4.0 and 3.x.
7793 */
7794 static const char s_szVBoxHeadless_exe[] = "VBoxHeadless" HOSTSUFF_EXE;
7795 Assert(cchBufLeft >= sizeof(s_szVBoxHeadless_exe));
7796 strcpy(pszNamePart, s_szVBoxHeadless_exe);
7797
7798 Utf8Str idStr = mData->mUuid.toString();
7799 const char *apszArgs[] =
7800 {
7801 szPath,
7802 "--comment", mUserData->s.strName.c_str(),
7803 "--startvm", idStr.c_str(),
7804 "--vrde", "config",
7805 NULL, /* For "--capture". */
7806 NULL, /* For "--sup-startup-log". */
7807 NULL
7808 };
7809 unsigned iArg = 7;
7810 if (!strFrontend.compare("capture", Utf8Str::CaseInsensitive))
7811 apszArgs[iArg++] = "--capture";
7812 apszArgs[iArg++] = pszSupHardeningLogArg;
7813
7814# ifdef RT_OS_WINDOWS
7815 vrc = RTProcCreate(szPath, apszArgs, env, RTPROC_FLAGS_NO_WINDOW, &pid);
7816# else
7817 vrc = RTProcCreate(szPath, apszArgs, env, 0, &pid);
7818# endif
7819 }
7820#else /* !VBOX_WITH_HEADLESS */
7821 if (0)
7822 ;
7823#endif /* !VBOX_WITH_HEADLESS */
7824 else
7825 {
7826 RTEnvDestroy(env);
7827 return setError(E_INVALIDARG,
7828 tr("Invalid frontend name: '%s'"),
7829 strFrontend.c_str());
7830 }
7831
7832 RTEnvDestroy(env);
7833
7834 if (RT_FAILURE(vrc))
7835 return setError(VBOX_E_IPRT_ERROR,
7836 tr("Could not launch a process for the machine '%s' (%Rrc)"),
7837 mUserData->s.strName.c_str(), vrc);
7838
7839 LogFlowThisFunc(("launched.pid=%d(0x%x)\n", pid, pid));
7840
7841 if (!fSeparate)
7842 {
7843 /*
7844 * Note that we don't release the lock here before calling the client,
7845 * because it doesn't need to call us back if called with a NULL argument.
7846 * Releasing the lock here is dangerous because we didn't prepare the
7847 * launch data yet, but the client we've just started may happen to be
7848 * too fast and call LockMachine() that will fail (because of PID, etc.),
7849 * so that the Machine will never get out of the Spawning session state.
7850 */
7851
7852 /* inform the session that it will be a remote one */
7853 LogFlowThisFunc(("Calling AssignMachine (NULL)...\n"));
7854#ifndef VBOX_WITH_GENERIC_SESSION_WATCHER
7855 HRESULT rc = aControl->AssignMachine(NULL, LockType_Write, Bstr::Empty.raw());
7856#else /* VBOX_WITH_GENERIC_SESSION_WATCHER */
7857 HRESULT rc = aControl->AssignMachine(NULL, LockType_Write, NULL);
7858#endif /* VBOX_WITH_GENERIC_SESSION_WATCHER */
7859 LogFlowThisFunc(("AssignMachine (NULL) returned %08X\n", rc));
7860
7861 if (FAILED(rc))
7862 {
7863 /* restore the session state */
7864 mData->mSession.mState = SessionState_Unlocked;
7865 alock.release();
7866 mParent->i_addProcessToReap(pid);
7867 /* The failure may occur w/o any error info (from RPC), so provide one */
7868 return setError(VBOX_E_VM_ERROR,
7869 tr("Failed to assign the machine to the session (%Rhrc)"), rc);
7870 }
7871
7872 /* attach launch data to the machine */
7873 Assert(mData->mSession.mPID == NIL_RTPROCESS);
7874 mData->mSession.mRemoteControls.push_back(aControl);
7875 mData->mSession.mProgress = aProgress;
7876 mData->mSession.mPID = pid;
7877 mData->mSession.mState = SessionState_Spawning;
7878 Assert(strCanonicalName.isNotEmpty());
7879 mData->mSession.mName = strCanonicalName;
7880 }
7881 else
7882 {
7883 /* For separate UI process we declare the launch as completed instantly, as the
7884 * actual headless VM start may or may not come. No point in remembering anything
7885 * yet, as what matters for us is when the headless VM gets started. */
7886 aProgress->i_notifyComplete(S_OK);
7887 }
7888
7889 alock.release();
7890 mParent->i_addProcessToReap(pid);
7891
7892 LogFlowThisFuncLeave();
7893 return S_OK;
7894}
7895
7896/**
7897 * Returns @c true if the given session machine instance has an open direct
7898 * session (and optionally also for direct sessions which are closing) and
7899 * returns the session control machine instance if so.
7900 *
7901 * Note that when the method returns @c false, the arguments remain unchanged.
7902 *
7903 * @param aMachine Session machine object.
7904 * @param aControl Direct session control object (optional).
7905 * @param aRequireVM If true then only allow VM sessions.
7906 * @param aAllowClosing If true then additionally a session which is currently
7907 * being closed will also be allowed.
7908 *
7909 * @note locks this object for reading.
7910 */
7911bool Machine::i_isSessionOpen(ComObjPtr<SessionMachine> &aMachine,
7912 ComPtr<IInternalSessionControl> *aControl /*= NULL*/,
7913 bool aRequireVM /*= false*/,
7914 bool aAllowClosing /*= false*/)
7915{
7916 AutoLimitedCaller autoCaller(this);
7917 AssertComRCReturn(autoCaller.rc(), false);
7918
7919 /* just return false for inaccessible machines */
7920 if (getObjectState().getState() != ObjectState::Ready)
7921 return false;
7922
7923 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
7924
7925 if ( ( mData->mSession.mState == SessionState_Locked
7926 && (!aRequireVM || mData->mSession.mLockType == LockType_VM))
7927 || (aAllowClosing && mData->mSession.mState == SessionState_Unlocking)
7928 )
7929 {
7930 AssertReturn(!mData->mSession.mMachine.isNull(), false);
7931
7932 aMachine = mData->mSession.mMachine;
7933
7934 if (aControl != NULL)
7935 *aControl = mData->mSession.mDirectControl;
7936
7937 return true;
7938 }
7939
7940 return false;
7941}
7942
7943/**
7944 * Returns @c true if the given machine has an spawning direct session.
7945 *
7946 * @note locks this object for reading.
7947 */
7948bool Machine::i_isSessionSpawning()
7949{
7950 AutoLimitedCaller autoCaller(this);
7951 AssertComRCReturn(autoCaller.rc(), false);
7952
7953 /* just return false for inaccessible machines */
7954 if (getObjectState().getState() != ObjectState::Ready)
7955 return false;
7956
7957 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
7958
7959 if (mData->mSession.mState == SessionState_Spawning)
7960 return true;
7961
7962 return false;
7963}
7964
7965/**
7966 * Called from the client watcher thread to check for unexpected client process
7967 * death during Session_Spawning state (e.g. before it successfully opened a
7968 * direct session).
7969 *
7970 * On Win32 and on OS/2, this method is called only when we've got the
7971 * direct client's process termination notification, so it always returns @c
7972 * true.
7973 *
7974 * On other platforms, this method returns @c true if the client process is
7975 * terminated and @c false if it's still alive.
7976 *
7977 * @note Locks this object for writing.
7978 */
7979bool Machine::i_checkForSpawnFailure()
7980{
7981 AutoCaller autoCaller(this);
7982 if (!autoCaller.isOk())
7983 {
7984 /* nothing to do */
7985 LogFlowThisFunc(("Already uninitialized!\n"));
7986 return true;
7987 }
7988
7989 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
7990
7991 if (mData->mSession.mState != SessionState_Spawning)
7992 {
7993 /* nothing to do */
7994 LogFlowThisFunc(("Not spawning any more!\n"));
7995 return true;
7996 }
7997
7998 HRESULT rc = S_OK;
7999
8000 /* PID not yet initialized, skip check. */
8001 if (mData->mSession.mPID == NIL_RTPROCESS)
8002 return false;
8003
8004 RTPROCSTATUS status;
8005 int vrc = RTProcWait(mData->mSession.mPID, RTPROCWAIT_FLAGS_NOBLOCK, &status);
8006
8007 if (vrc != VERR_PROCESS_RUNNING)
8008 {
8009 Utf8Str strExtraInfo;
8010
8011#if defined(RT_OS_WINDOWS) && defined(VBOX_WITH_HARDENING)
8012 /* If the startup logfile exists and is of non-zero length, tell the
8013 user to look there for more details to encourage them to attach it
8014 when reporting startup issues. */
8015 Utf8Str strHardeningLogFile = i_getHardeningLogFilename();
8016 uint64_t cbStartupLogFile = 0;
8017 int vrc2 = RTFileQuerySize(strHardeningLogFile.c_str(), &cbStartupLogFile);
8018 if (RT_SUCCESS(vrc2) && cbStartupLogFile > 0)
8019 strExtraInfo.append(Utf8StrFmt(tr(". More details may be available in '%s'"), strHardeningLogFile.c_str()));
8020#endif
8021
8022 if (RT_SUCCESS(vrc) && status.enmReason == RTPROCEXITREASON_NORMAL)
8023 rc = setError(E_FAIL,
8024 tr("The virtual machine '%s' has terminated unexpectedly during startup with exit code %d (%#x)%s"),
8025 i_getName().c_str(), status.iStatus, status.iStatus, strExtraInfo.c_str());
8026 else if (RT_SUCCESS(vrc) && status.enmReason == RTPROCEXITREASON_SIGNAL)
8027 rc = setError(E_FAIL,
8028 tr("The virtual machine '%s' has terminated unexpectedly during startup because of signal %d%s"),
8029 i_getName().c_str(), status.iStatus, strExtraInfo.c_str());
8030 else if (RT_SUCCESS(vrc) && status.enmReason == RTPROCEXITREASON_ABEND)
8031 rc = setError(E_FAIL,
8032 tr("The virtual machine '%s' has terminated abnormally (iStatus=%#x)%s"),
8033 i_getName().c_str(), status.iStatus, strExtraInfo.c_str());
8034 else
8035 rc = setError(E_FAIL,
8036 tr("The virtual machine '%s' has terminated unexpectedly during startup (%Rrc)%s"),
8037 i_getName().c_str(), vrc, strExtraInfo.c_str());
8038 }
8039
8040 if (FAILED(rc))
8041 {
8042 /* Close the remote session, remove the remote control from the list
8043 * and reset session state to Closed (@note keep the code in sync with
8044 * the relevant part in LockMachine()). */
8045
8046 Assert(mData->mSession.mRemoteControls.size() == 1);
8047 if (mData->mSession.mRemoteControls.size() == 1)
8048 {
8049 ErrorInfoKeeper eik;
8050 mData->mSession.mRemoteControls.front()->Uninitialize();
8051 }
8052
8053 mData->mSession.mRemoteControls.clear();
8054 mData->mSession.mState = SessionState_Unlocked;
8055
8056 /* finalize the progress after setting the state */
8057 if (!mData->mSession.mProgress.isNull())
8058 {
8059 mData->mSession.mProgress->notifyComplete(rc);
8060 mData->mSession.mProgress.setNull();
8061 }
8062
8063 mData->mSession.mPID = NIL_RTPROCESS;
8064
8065 mParent->i_onSessionStateChange(mData->mUuid, SessionState_Unlocked);
8066 return true;
8067 }
8068
8069 return false;
8070}
8071
8072/**
8073 * Checks whether the machine can be registered. If so, commits and saves
8074 * all settings.
8075 *
8076 * @note Must be called from mParent's write lock. Locks this object and
8077 * children for writing.
8078 */
8079HRESULT Machine::i_prepareRegister()
8080{
8081 AssertReturn(mParent->isWriteLockOnCurrentThread(), E_FAIL);
8082
8083 AutoLimitedCaller autoCaller(this);
8084 AssertComRCReturnRC(autoCaller.rc());
8085
8086 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
8087
8088 /* wait for state dependents to drop to zero */
8089 i_ensureNoStateDependencies();
8090
8091 if (!mData->mAccessible)
8092 return setError(VBOX_E_INVALID_OBJECT_STATE,
8093 tr("The machine '%s' with UUID {%s} is inaccessible and cannot be registered"),
8094 mUserData->s.strName.c_str(),
8095 mData->mUuid.toString().c_str());
8096
8097 AssertReturn(getObjectState().getState() == ObjectState::Ready, E_FAIL);
8098
8099 if (mData->mRegistered)
8100 return setError(VBOX_E_INVALID_OBJECT_STATE,
8101 tr("The machine '%s' with UUID {%s} is already registered"),
8102 mUserData->s.strName.c_str(),
8103 mData->mUuid.toString().c_str());
8104
8105 HRESULT rc = S_OK;
8106
8107 // Ensure the settings are saved. If we are going to be registered and
8108 // no config file exists yet, create it by calling i_saveSettings() too.
8109 if ( (mData->flModifications)
8110 || (!mData->pMachineConfigFile->fileExists())
8111 )
8112 {
8113 rc = i_saveSettings(NULL);
8114 // no need to check whether VirtualBox.xml needs saving too since
8115 // we can't have a machine XML file rename pending
8116 if (FAILED(rc)) return rc;
8117 }
8118
8119 /* more config checking goes here */
8120
8121 if (SUCCEEDED(rc))
8122 {
8123 /* we may have had implicit modifications we want to fix on success */
8124 i_commit();
8125
8126 mData->mRegistered = true;
8127 }
8128 else
8129 {
8130 /* we may have had implicit modifications we want to cancel on failure*/
8131 i_rollback(false /* aNotify */);
8132 }
8133
8134 return rc;
8135}
8136
8137/**
8138 * Increases the number of objects dependent on the machine state or on the
8139 * registered state. Guarantees that these two states will not change at least
8140 * until #i_releaseStateDependency() is called.
8141 *
8142 * Depending on the @a aDepType value, additional state checks may be made.
8143 * These checks will set extended error info on failure. See
8144 * #i_checkStateDependency() for more info.
8145 *
8146 * If this method returns a failure, the dependency is not added and the caller
8147 * is not allowed to rely on any particular machine state or registration state
8148 * value and may return the failed result code to the upper level.
8149 *
8150 * @param aDepType Dependency type to add.
8151 * @param aState Current machine state (NULL if not interested).
8152 * @param aRegistered Current registered state (NULL if not interested).
8153 *
8154 * @note Locks this object for writing.
8155 */
8156HRESULT Machine::i_addStateDependency(StateDependency aDepType /* = AnyStateDep */,
8157 MachineState_T *aState /* = NULL */,
8158 BOOL *aRegistered /* = NULL */)
8159{
8160 AutoCaller autoCaller(this);
8161 AssertComRCReturnRC(autoCaller.rc());
8162
8163 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
8164
8165 HRESULT rc = i_checkStateDependency(aDepType);
8166 if (FAILED(rc)) return rc;
8167
8168 {
8169 if (mData->mMachineStateChangePending != 0)
8170 {
8171 /* i_ensureNoStateDependencies() is waiting for state dependencies to
8172 * drop to zero so don't add more. It may make sense to wait a bit
8173 * and retry before reporting an error (since the pending state
8174 * transition should be really quick) but let's just assert for
8175 * now to see if it ever happens on practice. */
8176
8177 AssertFailed();
8178
8179 return setError(E_ACCESSDENIED,
8180 tr("Machine state change is in progress. Please retry the operation later."));
8181 }
8182
8183 ++mData->mMachineStateDeps;
8184 Assert(mData->mMachineStateDeps != 0 /* overflow */);
8185 }
8186
8187 if (aState)
8188 *aState = mData->mMachineState;
8189 if (aRegistered)
8190 *aRegistered = mData->mRegistered;
8191
8192 return S_OK;
8193}
8194
8195/**
8196 * Decreases the number of objects dependent on the machine state.
8197 * Must always complete the #i_addStateDependency() call after the state
8198 * dependency is no more necessary.
8199 */
8200void Machine::i_releaseStateDependency()
8201{
8202 AutoCaller autoCaller(this);
8203 AssertComRCReturnVoid(autoCaller.rc());
8204
8205 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
8206
8207 /* releaseStateDependency() w/o addStateDependency()? */
8208 AssertReturnVoid(mData->mMachineStateDeps != 0);
8209 -- mData->mMachineStateDeps;
8210
8211 if (mData->mMachineStateDeps == 0)
8212 {
8213 /* inform i_ensureNoStateDependencies() that there are no more deps */
8214 if (mData->mMachineStateChangePending != 0)
8215 {
8216 Assert(mData->mMachineStateDepsSem != NIL_RTSEMEVENTMULTI);
8217 RTSemEventMultiSignal (mData->mMachineStateDepsSem);
8218 }
8219 }
8220}
8221
8222Utf8Str Machine::i_getExtraData(const Utf8Str &strKey)
8223{
8224 /* start with nothing found */
8225 Utf8Str strResult("");
8226
8227 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
8228
8229 settings::StringsMap::const_iterator it = mData->pMachineConfigFile->mapExtraDataItems.find(strKey);
8230 if (it != mData->pMachineConfigFile->mapExtraDataItems.end())
8231 // found:
8232 strResult = it->second; // source is a Utf8Str
8233
8234 return strResult;
8235}
8236
8237// protected methods
8238/////////////////////////////////////////////////////////////////////////////
8239
8240/**
8241 * Performs machine state checks based on the @a aDepType value. If a check
8242 * fails, this method will set extended error info, otherwise it will return
8243 * S_OK. It is supposed, that on failure, the caller will immediately return
8244 * the return value of this method to the upper level.
8245 *
8246 * When @a aDepType is AnyStateDep, this method always returns S_OK.
8247 *
8248 * When @a aDepType is MutableStateDep, this method returns S_OK only if the
8249 * current state of this machine object allows to change settings of the
8250 * machine (i.e. the machine is not registered, or registered but not running
8251 * and not saved). It is useful to call this method from Machine setters
8252 * before performing any change.
8253 *
8254 * When @a aDepType is MutableOrSavedStateDep, this method behaves the same
8255 * as for MutableStateDep except that if the machine is saved, S_OK is also
8256 * returned. This is useful in setters which allow changing machine
8257 * properties when it is in the saved state.
8258 *
8259 * When @a aDepType is MutableOrRunningStateDep, this method returns S_OK only
8260 * if the current state of this machine object allows to change runtime
8261 * changeable settings of the machine (i.e. the machine is not registered, or
8262 * registered but either running or not running and not saved). It is useful
8263 * to call this method from Machine setters before performing any changes to
8264 * runtime changeable settings.
8265 *
8266 * When @a aDepType is MutableOrSavedOrRunningStateDep, this method behaves
8267 * the same as for MutableOrRunningStateDep except that if the machine is
8268 * saved, S_OK is also returned. This is useful in setters which allow
8269 * changing runtime and saved state changeable machine properties.
8270 *
8271 * @param aDepType Dependency type to check.
8272 *
8273 * @note Non Machine based classes should use #i_addStateDependency() and
8274 * #i_releaseStateDependency() methods or the smart AutoStateDependency
8275 * template.
8276 *
8277 * @note This method must be called from under this object's read or write
8278 * lock.
8279 */
8280HRESULT Machine::i_checkStateDependency(StateDependency aDepType)
8281{
8282 switch (aDepType)
8283 {
8284 case AnyStateDep:
8285 {
8286 break;
8287 }
8288 case MutableStateDep:
8289 {
8290 if ( mData->mRegistered
8291 && ( !i_isSessionMachine()
8292 || ( mData->mMachineState != MachineState_Aborted
8293 && mData->mMachineState != MachineState_Teleported
8294 && mData->mMachineState != MachineState_PoweredOff
8295 )
8296 )
8297 )
8298 return setError(VBOX_E_INVALID_VM_STATE,
8299 tr("The machine is not mutable (state is %s)"),
8300 Global::stringifyMachineState(mData->mMachineState));
8301 break;
8302 }
8303 case MutableOrSavedStateDep:
8304 {
8305 if ( mData->mRegistered
8306 && ( !i_isSessionMachine()
8307 || ( mData->mMachineState != MachineState_Aborted
8308 && mData->mMachineState != MachineState_Teleported
8309 && mData->mMachineState != MachineState_Saved
8310 && mData->mMachineState != MachineState_PoweredOff
8311 )
8312 )
8313 )
8314 return setError(VBOX_E_INVALID_VM_STATE,
8315 tr("The machine is not mutable or saved (state is %s)"),
8316 Global::stringifyMachineState(mData->mMachineState));
8317 break;
8318 }
8319 case MutableOrRunningStateDep:
8320 {
8321 if ( mData->mRegistered
8322 && ( !i_isSessionMachine()
8323 || ( mData->mMachineState != MachineState_Aborted
8324 && mData->mMachineState != MachineState_Teleported
8325 && mData->mMachineState != MachineState_PoweredOff
8326 && !Global::IsOnline(mData->mMachineState)
8327 )
8328 )
8329 )
8330 return setError(VBOX_E_INVALID_VM_STATE,
8331 tr("The machine is not mutable or running (state is %s)"),
8332 Global::stringifyMachineState(mData->mMachineState));
8333 break;
8334 }
8335 case MutableOrSavedOrRunningStateDep:
8336 {
8337 if ( mData->mRegistered
8338 && ( !i_isSessionMachine()
8339 || ( mData->mMachineState != MachineState_Aborted
8340 && mData->mMachineState != MachineState_Teleported
8341 && mData->mMachineState != MachineState_Saved
8342 && mData->mMachineState != MachineState_PoweredOff
8343 && !Global::IsOnline(mData->mMachineState)
8344 )
8345 )
8346 )
8347 return setError(VBOX_E_INVALID_VM_STATE,
8348 tr("The machine is not mutable, saved or running (state is %s)"),
8349 Global::stringifyMachineState(mData->mMachineState));
8350 break;
8351 }
8352 }
8353
8354 return S_OK;
8355}
8356
8357/**
8358 * Helper to initialize all associated child objects and allocate data
8359 * structures.
8360 *
8361 * This method must be called as a part of the object's initialization procedure
8362 * (usually done in the #init() method).
8363 *
8364 * @note Must be called only from #init() or from #i_registeredInit().
8365 */
8366HRESULT Machine::initDataAndChildObjects()
8367{
8368 AutoCaller autoCaller(this);
8369 AssertComRCReturnRC(autoCaller.rc());
8370 AssertComRCReturn( getObjectState().getState() == ObjectState::InInit
8371 || getObjectState().getState() == ObjectState::Limited, E_FAIL);
8372
8373 AssertReturn(!mData->mAccessible, E_FAIL);
8374
8375 /* allocate data structures */
8376 mSSData.allocate();
8377 mUserData.allocate();
8378 mHWData.allocate();
8379 mMediumAttachments.allocate();
8380 mStorageControllers.allocate();
8381 mUSBControllers.allocate();
8382
8383 /* initialize mOSTypeId */
8384 mUserData->s.strOsType = mParent->i_getUnknownOSType()->i_id();
8385
8386 /* create associated BIOS settings object */
8387 unconst(mBIOSSettings).createObject();
8388 mBIOSSettings->init(this);
8389
8390 /* create an associated VRDE object (default is disabled) */
8391 unconst(mVRDEServer).createObject();
8392 mVRDEServer->init(this);
8393
8394 /* create associated serial port objects */
8395 for (ULONG slot = 0; slot < RT_ELEMENTS(mSerialPorts); ++slot)
8396 {
8397 unconst(mSerialPorts[slot]).createObject();
8398 mSerialPorts[slot]->init(this, slot);
8399 }
8400
8401 /* create associated parallel port objects */
8402 for (ULONG slot = 0; slot < RT_ELEMENTS(mParallelPorts); ++slot)
8403 {
8404 unconst(mParallelPorts[slot]).createObject();
8405 mParallelPorts[slot]->init(this, slot);
8406 }
8407
8408 /* create the audio adapter object (always present, default is disabled) */
8409 unconst(mAudioAdapter).createObject();
8410 mAudioAdapter->init(this);
8411
8412 /* create the USB device filters object (always present) */
8413 unconst(mUSBDeviceFilters).createObject();
8414 mUSBDeviceFilters->init(this);
8415
8416 /* create associated network adapter objects */
8417 mNetworkAdapters.resize(Global::getMaxNetworkAdapters(mHWData->mChipsetType));
8418 for (ULONG slot = 0; slot < mNetworkAdapters.size(); ++slot)
8419 {
8420 unconst(mNetworkAdapters[slot]).createObject();
8421 mNetworkAdapters[slot]->init(this, slot);
8422 }
8423
8424 /* create the bandwidth control */
8425 unconst(mBandwidthControl).createObject();
8426 mBandwidthControl->init(this);
8427
8428#ifdef VBOX_WITH_UNATTENDED
8429 /* create the unattended object (always present) */
8430 unconst(mUnattended).createObject();
8431 mUnattended->init(this);
8432#endif
8433
8434 return S_OK;
8435}
8436
8437/**
8438 * Helper to uninitialize all associated child objects and to free all data
8439 * structures.
8440 *
8441 * This method must be called as a part of the object's uninitialization
8442 * procedure (usually done in the #uninit() method).
8443 *
8444 * @note Must be called only from #uninit() or from #i_registeredInit().
8445 */
8446void Machine::uninitDataAndChildObjects()
8447{
8448 AutoCaller autoCaller(this);
8449 AssertComRCReturnVoid(autoCaller.rc());
8450 AssertComRCReturnVoid( getObjectState().getState() == ObjectState::InUninit
8451 || getObjectState().getState() == ObjectState::Limited);
8452
8453 /* tell all our other child objects we've been uninitialized */
8454 if (mBandwidthControl)
8455 {
8456 mBandwidthControl->uninit();
8457 unconst(mBandwidthControl).setNull();
8458 }
8459
8460 for (ULONG slot = 0; slot < mNetworkAdapters.size(); ++slot)
8461 {
8462 if (mNetworkAdapters[slot])
8463 {
8464 mNetworkAdapters[slot]->uninit();
8465 unconst(mNetworkAdapters[slot]).setNull();
8466 }
8467 }
8468
8469 if (mUSBDeviceFilters)
8470 {
8471 mUSBDeviceFilters->uninit();
8472 unconst(mUSBDeviceFilters).setNull();
8473 }
8474
8475 if (mAudioAdapter)
8476 {
8477 mAudioAdapter->uninit();
8478 unconst(mAudioAdapter).setNull();
8479 }
8480
8481 for (ULONG slot = 0; slot < RT_ELEMENTS(mParallelPorts); ++slot)
8482 {
8483 if (mParallelPorts[slot])
8484 {
8485 mParallelPorts[slot]->uninit();
8486 unconst(mParallelPorts[slot]).setNull();
8487 }
8488 }
8489
8490 for (ULONG slot = 0; slot < RT_ELEMENTS(mSerialPorts); ++slot)
8491 {
8492 if (mSerialPorts[slot])
8493 {
8494 mSerialPorts[slot]->uninit();
8495 unconst(mSerialPorts[slot]).setNull();
8496 }
8497 }
8498
8499 if (mVRDEServer)
8500 {
8501 mVRDEServer->uninit();
8502 unconst(mVRDEServer).setNull();
8503 }
8504
8505 if (mBIOSSettings)
8506 {
8507 mBIOSSettings->uninit();
8508 unconst(mBIOSSettings).setNull();
8509 }
8510
8511#ifdef VBOX_WITH_UNATTENDED
8512 if (mUnattended)
8513 {
8514 mUnattended->uninit();
8515 unconst(mUnattended).setNull();
8516 }
8517#endif
8518
8519 /* Deassociate media (only when a real Machine or a SnapshotMachine
8520 * instance is uninitialized; SessionMachine instances refer to real
8521 * Machine media). This is necessary for a clean re-initialization of
8522 * the VM after successfully re-checking the accessibility state. Note
8523 * that in case of normal Machine or SnapshotMachine uninitialization (as
8524 * a result of unregistering or deleting the snapshot), outdated media
8525 * attachments will already be uninitialized and deleted, so this
8526 * code will not affect them. */
8527 if ( !mMediumAttachments.isNull()
8528 && !i_isSessionMachine()
8529 )
8530 {
8531 for (MediumAttachmentList::const_iterator
8532 it = mMediumAttachments->begin();
8533 it != mMediumAttachments->end();
8534 ++it)
8535 {
8536 ComObjPtr<Medium> pMedium = (*it)->i_getMedium();
8537 if (pMedium.isNull())
8538 continue;
8539 HRESULT rc = pMedium->i_removeBackReference(mData->mUuid, i_getSnapshotId());
8540 AssertComRC(rc);
8541 }
8542 }
8543
8544 if (!i_isSessionMachine() && !i_isSnapshotMachine())
8545 {
8546 // clean up the snapshots list (Snapshot::uninit() will handle the snapshot's children recursively)
8547 if (mData->mFirstSnapshot)
8548 {
8549 // snapshots tree is protected by machine write lock; strictly
8550 // this isn't necessary here since we're deleting the entire
8551 // machine, but otherwise we assert in Snapshot::uninit()
8552 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
8553 mData->mFirstSnapshot->uninit();
8554 mData->mFirstSnapshot.setNull();
8555 }
8556
8557 mData->mCurrentSnapshot.setNull();
8558 }
8559
8560 /* free data structures (the essential mData structure is not freed here
8561 * since it may be still in use) */
8562 mMediumAttachments.free();
8563 mStorageControllers.free();
8564 mUSBControllers.free();
8565 mHWData.free();
8566 mUserData.free();
8567 mSSData.free();
8568}
8569
8570/**
8571 * Returns a pointer to the Machine object for this machine that acts like a
8572 * parent for complex machine data objects such as shared folders, etc.
8573 *
8574 * For primary Machine objects and for SnapshotMachine objects, returns this
8575 * object's pointer itself. For SessionMachine objects, returns the peer
8576 * (primary) machine pointer.
8577 */
8578Machine *Machine::i_getMachine()
8579{
8580 if (i_isSessionMachine())
8581 return (Machine*)mPeer;
8582 return this;
8583}
8584
8585/**
8586 * Makes sure that there are no machine state dependents. If necessary, waits
8587 * for the number of dependents to drop to zero.
8588 *
8589 * Make sure this method is called from under this object's write lock to
8590 * guarantee that no new dependents may be added when this method returns
8591 * control to the caller.
8592 *
8593 * @note Locks this object for writing. The lock will be released while waiting
8594 * (if necessary).
8595 *
8596 * @warning To be used only in methods that change the machine state!
8597 */
8598void Machine::i_ensureNoStateDependencies()
8599{
8600 AssertReturnVoid(isWriteLockOnCurrentThread());
8601
8602 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
8603
8604 /* Wait for all state dependents if necessary */
8605 if (mData->mMachineStateDeps != 0)
8606 {
8607 /* lazy semaphore creation */
8608 if (mData->mMachineStateDepsSem == NIL_RTSEMEVENTMULTI)
8609 RTSemEventMultiCreate(&mData->mMachineStateDepsSem);
8610
8611 LogFlowThisFunc(("Waiting for state deps (%d) to drop to zero...\n",
8612 mData->mMachineStateDeps));
8613
8614 ++mData->mMachineStateChangePending;
8615
8616 /* reset the semaphore before waiting, the last dependent will signal
8617 * it */
8618 RTSemEventMultiReset(mData->mMachineStateDepsSem);
8619
8620 alock.release();
8621
8622 RTSemEventMultiWait(mData->mMachineStateDepsSem, RT_INDEFINITE_WAIT);
8623
8624 alock.acquire();
8625
8626 -- mData->mMachineStateChangePending;
8627 }
8628}
8629
8630/**
8631 * Changes the machine state and informs callbacks.
8632 *
8633 * This method is not intended to fail so it either returns S_OK or asserts (and
8634 * returns a failure).
8635 *
8636 * @note Locks this object for writing.
8637 */
8638HRESULT Machine::i_setMachineState(MachineState_T aMachineState)
8639{
8640 LogFlowThisFuncEnter();
8641 LogFlowThisFunc(("aMachineState=%s\n", Global::stringifyMachineState(aMachineState) ));
8642 Assert(aMachineState != MachineState_Null);
8643
8644 AutoCaller autoCaller(this);
8645 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
8646
8647 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
8648
8649 /* wait for state dependents to drop to zero */
8650 i_ensureNoStateDependencies();
8651
8652 MachineState_T const enmOldState = mData->mMachineState;
8653 if (enmOldState != aMachineState)
8654 {
8655 mData->mMachineState = aMachineState;
8656 RTTimeNow(&mData->mLastStateChange);
8657
8658#ifdef VBOX_WITH_DTRACE_R3_MAIN
8659 VBOXAPI_MACHINE_STATE_CHANGED(this, aMachineState, enmOldState, mData->mUuid.toStringCurly().c_str());
8660#endif
8661 mParent->i_onMachineStateChange(mData->mUuid, aMachineState);
8662 }
8663
8664 LogFlowThisFuncLeave();
8665 return S_OK;
8666}
8667
8668/**
8669 * Searches for a shared folder with the given logical name
8670 * in the collection of shared folders.
8671 *
8672 * @param aName logical name of the shared folder
8673 * @param aSharedFolder where to return the found object
8674 * @param aSetError whether to set the error info if the folder is
8675 * not found
8676 * @return
8677 * S_OK when found or VBOX_E_OBJECT_NOT_FOUND when not found
8678 *
8679 * @note
8680 * must be called from under the object's lock!
8681 */
8682HRESULT Machine::i_findSharedFolder(const Utf8Str &aName,
8683 ComObjPtr<SharedFolder> &aSharedFolder,
8684 bool aSetError /* = false */)
8685{
8686 HRESULT rc = VBOX_E_OBJECT_NOT_FOUND;
8687 for (HWData::SharedFolderList::const_iterator
8688 it = mHWData->mSharedFolders.begin();
8689 it != mHWData->mSharedFolders.end();
8690 ++it)
8691 {
8692 SharedFolder *pSF = *it;
8693 AutoCaller autoCaller(pSF);
8694 if (pSF->i_getName() == aName)
8695 {
8696 aSharedFolder = pSF;
8697 rc = S_OK;
8698 break;
8699 }
8700 }
8701
8702 if (aSetError && FAILED(rc))
8703 setError(rc, tr("Could not find a shared folder named '%s'"), aName.c_str());
8704
8705 return rc;
8706}
8707
8708/**
8709 * Initializes all machine instance data from the given settings structures
8710 * from XML. The exception is the machine UUID which needs special handling
8711 * depending on the caller's use case, so the caller needs to set that herself.
8712 *
8713 * This gets called in several contexts during machine initialization:
8714 *
8715 * -- When machine XML exists on disk already and needs to be loaded into memory,
8716 * for example, from #i_registeredInit() to load all registered machines on
8717 * VirtualBox startup. In this case, puuidRegistry is NULL because the media
8718 * attached to the machine should be part of some media registry already.
8719 *
8720 * -- During OVF import, when a machine config has been constructed from an
8721 * OVF file. In this case, puuidRegistry is set to the machine UUID to
8722 * ensure that the media listed as attachments in the config (which have
8723 * been imported from the OVF) receive the correct registry ID.
8724 *
8725 * -- During VM cloning.
8726 *
8727 * @param config Machine settings from XML.
8728 * @param puuidRegistry If != NULL, Medium::setRegistryIdIfFirst() gets called with this registry ID
8729 * for each attached medium in the config.
8730 * @return
8731 */
8732HRESULT Machine::i_loadMachineDataFromSettings(const settings::MachineConfigFile &config,
8733 const Guid *puuidRegistry)
8734{
8735 // copy name, description, OS type, teleporter, UTC etc.
8736 mUserData->s = config.machineUserData;
8737
8738 // look up the object by Id to check it is valid
8739 ComPtr<IGuestOSType> guestOSType;
8740 HRESULT rc = mParent->GetGuestOSType(Bstr(mUserData->s.strOsType).raw(),
8741 guestOSType.asOutParam());
8742 if (FAILED(rc)) return rc;
8743
8744 // stateFile (optional)
8745 if (config.strStateFile.isEmpty())
8746 mSSData->strStateFilePath.setNull();
8747 else
8748 {
8749 Utf8Str stateFilePathFull(config.strStateFile);
8750 int vrc = i_calculateFullPath(stateFilePathFull, stateFilePathFull);
8751 if (RT_FAILURE(vrc))
8752 return setError(E_FAIL,
8753 tr("Invalid saved state file path '%s' (%Rrc)"),
8754 config.strStateFile.c_str(),
8755 vrc);
8756 mSSData->strStateFilePath = stateFilePathFull;
8757 }
8758
8759 // snapshot folder needs special processing so set it again
8760 rc = COMSETTER(SnapshotFolder)(Bstr(config.machineUserData.strSnapshotFolder).raw());
8761 if (FAILED(rc)) return rc;
8762
8763 /* Copy the extra data items (config may or may not be the same as
8764 * mData->pMachineConfigFile) if necessary. When loading the XML files
8765 * from disk they are the same, but not for OVF import. */
8766 if (mData->pMachineConfigFile != &config)
8767 mData->pMachineConfigFile->mapExtraDataItems = config.mapExtraDataItems;
8768
8769 /* currentStateModified (optional, default is true) */
8770 mData->mCurrentStateModified = config.fCurrentStateModified;
8771
8772 mData->mLastStateChange = config.timeLastStateChange;
8773
8774 /*
8775 * note: all mUserData members must be assigned prior this point because
8776 * we need to commit changes in order to let mUserData be shared by all
8777 * snapshot machine instances.
8778 */
8779 mUserData.commitCopy();
8780
8781 // machine registry, if present (must be loaded before snapshots)
8782 if (config.canHaveOwnMediaRegistry())
8783 {
8784 // determine machine folder
8785 Utf8Str strMachineFolder = i_getSettingsFileFull();
8786 strMachineFolder.stripFilename();
8787 rc = mParent->initMedia(i_getId(), // media registry ID == machine UUID
8788 config.mediaRegistry,
8789 strMachineFolder);
8790 if (FAILED(rc)) return rc;
8791 }
8792
8793 /* Snapshot node (optional) */
8794 size_t cRootSnapshots;
8795 if ((cRootSnapshots = config.llFirstSnapshot.size()))
8796 {
8797 // there must be only one root snapshot
8798 Assert(cRootSnapshots == 1);
8799
8800 const settings::Snapshot &snap = config.llFirstSnapshot.front();
8801
8802 rc = i_loadSnapshot(snap,
8803 config.uuidCurrentSnapshot,
8804 NULL); // no parent == first snapshot
8805 if (FAILED(rc)) return rc;
8806 }
8807
8808 // hardware data
8809 rc = i_loadHardware(puuidRegistry, NULL, config.hardwareMachine, &config.debugging, &config.autostart);
8810 if (FAILED(rc)) return rc;
8811
8812 /*
8813 * NOTE: the assignment below must be the last thing to do,
8814 * otherwise it will be not possible to change the settings
8815 * somewhere in the code above because all setters will be
8816 * blocked by i_checkStateDependency(MutableStateDep).
8817 */
8818
8819 /* set the machine state to Aborted or Saved when appropriate */
8820 if (config.fAborted)
8821 {
8822 mSSData->strStateFilePath.setNull();
8823
8824 /* no need to use i_setMachineState() during init() */
8825 mData->mMachineState = MachineState_Aborted;
8826 }
8827 else if (!mSSData->strStateFilePath.isEmpty())
8828 {
8829 /* no need to use i_setMachineState() during init() */
8830 mData->mMachineState = MachineState_Saved;
8831 }
8832
8833 // after loading settings, we are no longer different from the XML on disk
8834 mData->flModifications = 0;
8835
8836 return S_OK;
8837}
8838
8839/**
8840 * Recursively loads all snapshots starting from the given.
8841 *
8842 * @param data snapshot settings.
8843 * @param aCurSnapshotId Current snapshot ID from the settings file.
8844 * @param aParentSnapshot Parent snapshot.
8845 */
8846HRESULT Machine::i_loadSnapshot(const settings::Snapshot &data,
8847 const Guid &aCurSnapshotId,
8848 Snapshot *aParentSnapshot)
8849{
8850 AssertReturn(!i_isSnapshotMachine(), E_FAIL);
8851 AssertReturn(!i_isSessionMachine(), E_FAIL);
8852
8853 HRESULT rc = S_OK;
8854
8855 Utf8Str strStateFile;
8856 if (!data.strStateFile.isEmpty())
8857 {
8858 /* optional */
8859 strStateFile = data.strStateFile;
8860 int vrc = i_calculateFullPath(strStateFile, strStateFile);
8861 if (RT_FAILURE(vrc))
8862 return setError(E_FAIL,
8863 tr("Invalid saved state file path '%s' (%Rrc)"),
8864 strStateFile.c_str(),
8865 vrc);
8866 }
8867
8868 /* create a snapshot machine object */
8869 ComObjPtr<SnapshotMachine> pSnapshotMachine;
8870 pSnapshotMachine.createObject();
8871 rc = pSnapshotMachine->initFromSettings(this,
8872 data.hardware,
8873 &data.debugging,
8874 &data.autostart,
8875 data.uuid.ref(),
8876 strStateFile);
8877 if (FAILED(rc)) return rc;
8878
8879 /* create a snapshot object */
8880 ComObjPtr<Snapshot> pSnapshot;
8881 pSnapshot.createObject();
8882 /* initialize the snapshot */
8883 rc = pSnapshot->init(mParent, // VirtualBox object
8884 data.uuid,
8885 data.strName,
8886 data.strDescription,
8887 data.timestamp,
8888 pSnapshotMachine,
8889 aParentSnapshot);
8890 if (FAILED(rc)) return rc;
8891
8892 /* memorize the first snapshot if necessary */
8893 if (!mData->mFirstSnapshot)
8894 mData->mFirstSnapshot = pSnapshot;
8895
8896 /* memorize the current snapshot when appropriate */
8897 if ( !mData->mCurrentSnapshot
8898 && pSnapshot->i_getId() == aCurSnapshotId
8899 )
8900 mData->mCurrentSnapshot = pSnapshot;
8901
8902 // now create the children
8903 for (settings::SnapshotsList::const_iterator
8904 it = data.llChildSnapshots.begin();
8905 it != data.llChildSnapshots.end();
8906 ++it)
8907 {
8908 const settings::Snapshot &childData = *it;
8909 // recurse
8910 rc = i_loadSnapshot(childData,
8911 aCurSnapshotId,
8912 pSnapshot); // parent = the one we created above
8913 if (FAILED(rc)) return rc;
8914 }
8915
8916 return rc;
8917}
8918
8919/**
8920 * Loads settings into mHWData.
8921 *
8922 * @param puuidRegistry Registry ID.
8923 * @param puuidSnapshot Snapshot ID
8924 * @param data Reference to the hardware settings.
8925 * @param pDbg Pointer to the debugging settings.
8926 * @param pAutostart Pointer to the autostart settings.
8927 */
8928HRESULT Machine::i_loadHardware(const Guid *puuidRegistry,
8929 const Guid *puuidSnapshot,
8930 const settings::Hardware &data,
8931 const settings::Debugging *pDbg,
8932 const settings::Autostart *pAutostart)
8933{
8934 AssertReturn(!i_isSessionMachine(), E_FAIL);
8935
8936 HRESULT rc = S_OK;
8937
8938 try
8939 {
8940 /* The hardware version attribute (optional). */
8941 mHWData->mHWVersion = data.strVersion;
8942 mHWData->mHardwareUUID = data.uuid;
8943
8944 mHWData->mHWVirtExEnabled = data.fHardwareVirt;
8945 mHWData->mHWVirtExNestedPagingEnabled = data.fNestedPaging;
8946 mHWData->mHWVirtExLargePagesEnabled = data.fLargePages;
8947 mHWData->mHWVirtExVPIDEnabled = data.fVPID;
8948 mHWData->mHWVirtExUXEnabled = data.fUnrestrictedExecution;
8949 mHWData->mHWVirtExForceEnabled = data.fHardwareVirtForce;
8950 mHWData->mPAEEnabled = data.fPAE;
8951 mHWData->mLongMode = data.enmLongMode;
8952 mHWData->mTripleFaultReset = data.fTripleFaultReset;
8953 mHWData->mAPIC = data.fAPIC;
8954 mHWData->mX2APIC = data.fX2APIC;
8955 mHWData->mCPUCount = data.cCPUs;
8956 mHWData->mCPUHotPlugEnabled = data.fCpuHotPlug;
8957 mHWData->mCpuExecutionCap = data.ulCpuExecutionCap;
8958 mHWData->mCpuIdPortabilityLevel = data.uCpuIdPortabilityLevel;
8959 mHWData->mCpuProfile = data.strCpuProfile;
8960
8961 // cpu
8962 if (mHWData->mCPUHotPlugEnabled)
8963 {
8964 for (settings::CpuList::const_iterator
8965 it = data.llCpus.begin();
8966 it != data.llCpus.end();
8967 ++it)
8968 {
8969 const settings::Cpu &cpu = *it;
8970
8971 mHWData->mCPUAttached[cpu.ulId] = true;
8972 }
8973 }
8974
8975 // cpuid leafs
8976 for (settings::CpuIdLeafsList::const_iterator
8977 it = data.llCpuIdLeafs.begin();
8978 it != data.llCpuIdLeafs.end();
8979 ++it)
8980 {
8981 const settings::CpuIdLeaf &leaf = *it;
8982
8983 switch (leaf.ulId)
8984 {
8985 case 0x0:
8986 case 0x1:
8987 case 0x2:
8988 case 0x3:
8989 case 0x4:
8990 case 0x5:
8991 case 0x6:
8992 case 0x7:
8993 case 0x8:
8994 case 0x9:
8995 case 0xA:
8996 mHWData->mCpuIdStdLeafs[leaf.ulId] = leaf;
8997 break;
8998
8999 case 0x80000000:
9000 case 0x80000001:
9001 case 0x80000002:
9002 case 0x80000003:
9003 case 0x80000004:
9004 case 0x80000005:
9005 case 0x80000006:
9006 case 0x80000007:
9007 case 0x80000008:
9008 case 0x80000009:
9009 case 0x8000000A:
9010 mHWData->mCpuIdExtLeafs[leaf.ulId - 0x80000000] = leaf;
9011 break;
9012
9013 default:
9014 /* just ignore */
9015 break;
9016 }
9017 }
9018
9019 mHWData->mMemorySize = data.ulMemorySizeMB;
9020 mHWData->mPageFusionEnabled = data.fPageFusionEnabled;
9021
9022 // boot order
9023 for (unsigned i = 0; i < RT_ELEMENTS(mHWData->mBootOrder); ++i)
9024 {
9025 settings::BootOrderMap::const_iterator it = data.mapBootOrder.find(i);
9026 if (it == data.mapBootOrder.end())
9027 mHWData->mBootOrder[i] = DeviceType_Null;
9028 else
9029 mHWData->mBootOrder[i] = it->second;
9030 }
9031
9032 mHWData->mGraphicsControllerType = data.graphicsControllerType;
9033 mHWData->mVRAMSize = data.ulVRAMSizeMB;
9034 mHWData->mMonitorCount = data.cMonitors;
9035 mHWData->mAccelerate3DEnabled = data.fAccelerate3D;
9036 mHWData->mAccelerate2DVideoEnabled = data.fAccelerate2DVideo;
9037 mHWData->mVideoCaptureWidth = data.ulVideoCaptureHorzRes;
9038 mHWData->mVideoCaptureHeight = data.ulVideoCaptureVertRes;
9039 mHWData->mVideoCaptureEnabled = data.fVideoCaptureEnabled;
9040 for (unsigned i = 0; i < RT_ELEMENTS(mHWData->maVideoCaptureScreens); ++i)
9041 mHWData->maVideoCaptureScreens[i] = ASMBitTest(&data.u64VideoCaptureScreens, i);
9042 AssertCompile(RT_ELEMENTS(mHWData->maVideoCaptureScreens) == sizeof(data.u64VideoCaptureScreens) * 8);
9043 mHWData->mVideoCaptureRate = data.ulVideoCaptureRate;
9044 mHWData->mVideoCaptureFPS = data.ulVideoCaptureFPS;
9045 if (!data.strVideoCaptureFile.isEmpty())
9046 i_calculateFullPath(data.strVideoCaptureFile, mHWData->mVideoCaptureFile);
9047 else
9048 mHWData->mVideoCaptureFile.setNull();
9049 mHWData->mFirmwareType = data.firmwareType;
9050 mHWData->mPointingHIDType = data.pointingHIDType;
9051 mHWData->mKeyboardHIDType = data.keyboardHIDType;
9052 mHWData->mChipsetType = data.chipsetType;
9053 mHWData->mParavirtProvider = data.paravirtProvider;
9054 mHWData->mParavirtDebug = data.strParavirtDebug;
9055 mHWData->mEmulatedUSBCardReaderEnabled = data.fEmulatedUSBCardReader;
9056 mHWData->mHPETEnabled = data.fHPETEnabled;
9057
9058 /* VRDEServer */
9059 rc = mVRDEServer->i_loadSettings(data.vrdeSettings);
9060 if (FAILED(rc)) return rc;
9061
9062 /* BIOS */
9063 rc = mBIOSSettings->i_loadSettings(data.biosSettings);
9064 if (FAILED(rc)) return rc;
9065
9066 // Bandwidth control (must come before network adapters)
9067 rc = mBandwidthControl->i_loadSettings(data.ioSettings);
9068 if (FAILED(rc)) return rc;
9069
9070 /* Shared folders */
9071 for (settings::USBControllerList::const_iterator
9072 it = data.usbSettings.llUSBControllers.begin();
9073 it != data.usbSettings.llUSBControllers.end();
9074 ++it)
9075 {
9076 const settings::USBController &settingsCtrl = *it;
9077 ComObjPtr<USBController> newCtrl;
9078
9079 newCtrl.createObject();
9080 newCtrl->init(this, settingsCtrl.strName, settingsCtrl.enmType);
9081 mUSBControllers->push_back(newCtrl);
9082 }
9083
9084 /* USB device filters */
9085 rc = mUSBDeviceFilters->i_loadSettings(data.usbSettings);
9086 if (FAILED(rc)) return rc;
9087
9088 // network adapters
9089 size_t newCount = Global::getMaxNetworkAdapters(mHWData->mChipsetType);
9090 size_t oldCount = mNetworkAdapters.size();
9091 if (newCount > oldCount)
9092 {
9093 mNetworkAdapters.resize(newCount);
9094 for (size_t slot = oldCount; slot < mNetworkAdapters.size(); ++slot)
9095 {
9096 unconst(mNetworkAdapters[slot]).createObject();
9097 mNetworkAdapters[slot]->init(this, (ULONG)slot);
9098 }
9099 }
9100 else if (newCount < oldCount)
9101 mNetworkAdapters.resize(newCount);
9102 for (settings::NetworkAdaptersList::const_iterator
9103 it = data.llNetworkAdapters.begin();
9104 it != data.llNetworkAdapters.end();
9105 ++it)
9106 {
9107 const settings::NetworkAdapter &nic = *it;
9108
9109 /* slot unicity is guaranteed by XML Schema */
9110 AssertBreak(nic.ulSlot < mNetworkAdapters.size());
9111 rc = mNetworkAdapters[nic.ulSlot]->i_loadSettings(mBandwidthControl, nic);
9112 if (FAILED(rc)) return rc;
9113 }
9114
9115 // serial ports
9116 for (settings::SerialPortsList::const_iterator
9117 it = data.llSerialPorts.begin();
9118 it != data.llSerialPorts.end();
9119 ++it)
9120 {
9121 const settings::SerialPort &s = *it;
9122
9123 AssertBreak(s.ulSlot < RT_ELEMENTS(mSerialPorts));
9124 rc = mSerialPorts[s.ulSlot]->i_loadSettings(s);
9125 if (FAILED(rc)) return rc;
9126 }
9127
9128 // parallel ports (optional)
9129 for (settings::ParallelPortsList::const_iterator
9130 it = data.llParallelPorts.begin();
9131 it != data.llParallelPorts.end();
9132 ++it)
9133 {
9134 const settings::ParallelPort &p = *it;
9135
9136 AssertBreak(p.ulSlot < RT_ELEMENTS(mParallelPorts));
9137 rc = mParallelPorts[p.ulSlot]->i_loadSettings(p);
9138 if (FAILED(rc)) return rc;
9139 }
9140
9141 /* AudioAdapter */
9142 rc = mAudioAdapter->i_loadSettings(data.audioAdapter);
9143 if (FAILED(rc)) return rc;
9144
9145 /* storage controllers */
9146 rc = i_loadStorageControllers(data.storage,
9147 puuidRegistry,
9148 puuidSnapshot);
9149 if (FAILED(rc)) return rc;
9150
9151 /* Shared folders */
9152 for (settings::SharedFoldersList::const_iterator
9153 it = data.llSharedFolders.begin();
9154 it != data.llSharedFolders.end();
9155 ++it)
9156 {
9157 const settings::SharedFolder &sf = *it;
9158
9159 ComObjPtr<SharedFolder> sharedFolder;
9160 /* Check for double entries. Not allowed! */
9161 rc = i_findSharedFolder(sf.strName, sharedFolder, false /* aSetError */);
9162 if (SUCCEEDED(rc))
9163 return setError(VBOX_E_OBJECT_IN_USE,
9164 tr("Shared folder named '%s' already exists"),
9165 sf.strName.c_str());
9166
9167 /* Create the new shared folder. Don't break on error. This will be
9168 * reported when the machine starts. */
9169 sharedFolder.createObject();
9170 rc = sharedFolder->init(i_getMachine(),
9171 sf.strName,
9172 sf.strHostPath,
9173 RT_BOOL(sf.fWritable),
9174 RT_BOOL(sf.fAutoMount),
9175 false /* fFailOnError */);
9176 if (FAILED(rc)) return rc;
9177 mHWData->mSharedFolders.push_back(sharedFolder);
9178 }
9179
9180 // Clipboard
9181 mHWData->mClipboardMode = data.clipboardMode;
9182
9183 // drag'n'drop
9184 mHWData->mDnDMode = data.dndMode;
9185
9186 // guest settings
9187 mHWData->mMemoryBalloonSize = data.ulMemoryBalloonSize;
9188
9189 // IO settings
9190 mHWData->mIOCacheEnabled = data.ioSettings.fIOCacheEnabled;
9191 mHWData->mIOCacheSize = data.ioSettings.ulIOCacheSize;
9192
9193 // Host PCI devices
9194 for (settings::HostPCIDeviceAttachmentList::const_iterator
9195 it = data.pciAttachments.begin();
9196 it != data.pciAttachments.end();
9197 ++it)
9198 {
9199 const settings::HostPCIDeviceAttachment &hpda = *it;
9200 ComObjPtr<PCIDeviceAttachment> pda;
9201
9202 pda.createObject();
9203 pda->i_loadSettings(this, hpda);
9204 mHWData->mPCIDeviceAssignments.push_back(pda);
9205 }
9206
9207 /*
9208 * (The following isn't really real hardware, but it lives in HWData
9209 * for reasons of convenience.)
9210 */
9211
9212#ifdef VBOX_WITH_GUEST_PROPS
9213 /* Guest properties (optional) */
9214
9215 /* Only load transient guest properties for configs which have saved
9216 * state, because there shouldn't be any for powered off VMs. The same
9217 * logic applies for snapshots, as offline snapshots shouldn't have
9218 * any such properties. They confuse the code in various places.
9219 * Note: can't rely on the machine state, as it isn't set yet. */
9220 bool fSkipTransientGuestProperties = mSSData->strStateFilePath.isEmpty();
9221 /* apologies for the hacky unconst() usage, but this needs hacking
9222 * actually inconsistent settings into consistency, otherwise there
9223 * will be some corner cases where the inconsistency survives
9224 * surprisingly long without getting fixed, especially for snapshots
9225 * as there are no config changes. */
9226 settings::GuestPropertiesList &llGuestProperties = unconst(data.llGuestProperties);
9227 for (settings::GuestPropertiesList::iterator
9228 it = llGuestProperties.begin();
9229 it != llGuestProperties.end();
9230 /*nothing*/)
9231 {
9232 const settings::GuestProperty &prop = *it;
9233 uint32_t fFlags = guestProp::NILFLAG;
9234 guestProp::validateFlags(prop.strFlags.c_str(), &fFlags);
9235 if ( fSkipTransientGuestProperties
9236 && ( fFlags & guestProp::TRANSIENT
9237 || fFlags & guestProp::TRANSRESET))
9238 {
9239 it = llGuestProperties.erase(it);
9240 continue;
9241 }
9242 HWData::GuestProperty property = { prop.strValue, (LONG64) prop.timestamp, fFlags };
9243 mHWData->mGuestProperties[prop.strName] = property;
9244 ++it;
9245 }
9246#endif /* VBOX_WITH_GUEST_PROPS defined */
9247
9248 rc = i_loadDebugging(pDbg);
9249 if (FAILED(rc))
9250 return rc;
9251
9252 mHWData->mAutostart = *pAutostart;
9253
9254 /* default frontend */
9255 mHWData->mDefaultFrontend = data.strDefaultFrontend;
9256 }
9257 catch (std::bad_alloc &)
9258 {
9259 return E_OUTOFMEMORY;
9260 }
9261
9262 AssertComRC(rc);
9263 return rc;
9264}
9265
9266/**
9267 * Called from Machine::loadHardware() to load the debugging settings of the
9268 * machine.
9269 *
9270 * @param pDbg Pointer to the settings.
9271 */
9272HRESULT Machine::i_loadDebugging(const settings::Debugging *pDbg)
9273{
9274 mHWData->mDebugging = *pDbg;
9275 /* no more processing currently required, this will probably change. */
9276 return S_OK;
9277}
9278
9279/**
9280 * Called from i_loadMachineDataFromSettings() for the storage controller data, including media.
9281 *
9282 * @param data storage settings.
9283 * @param puuidRegistry media registry ID to set media to or NULL;
9284 * see Machine::i_loadMachineDataFromSettings()
9285 * @param puuidSnapshot snapshot ID
9286 * @return
9287 */
9288HRESULT Machine::i_loadStorageControllers(const settings::Storage &data,
9289 const Guid *puuidRegistry,
9290 const Guid *puuidSnapshot)
9291{
9292 AssertReturn(!i_isSessionMachine(), E_FAIL);
9293
9294 HRESULT rc = S_OK;
9295
9296 for (settings::StorageControllersList::const_iterator
9297 it = data.llStorageControllers.begin();
9298 it != data.llStorageControllers.end();
9299 ++it)
9300 {
9301 const settings::StorageController &ctlData = *it;
9302
9303 ComObjPtr<StorageController> pCtl;
9304 /* Try to find one with the name first. */
9305 rc = i_getStorageControllerByName(ctlData.strName, pCtl, false /* aSetError */);
9306 if (SUCCEEDED(rc))
9307 return setError(VBOX_E_OBJECT_IN_USE,
9308 tr("Storage controller named '%s' already exists"),
9309 ctlData.strName.c_str());
9310
9311 pCtl.createObject();
9312 rc = pCtl->init(this,
9313 ctlData.strName,
9314 ctlData.storageBus,
9315 ctlData.ulInstance,
9316 ctlData.fBootable);
9317 if (FAILED(rc)) return rc;
9318
9319 mStorageControllers->push_back(pCtl);
9320
9321 rc = pCtl->COMSETTER(ControllerType)(ctlData.controllerType);
9322 if (FAILED(rc)) return rc;
9323
9324 rc = pCtl->COMSETTER(PortCount)(ctlData.ulPortCount);
9325 if (FAILED(rc)) return rc;
9326
9327 rc = pCtl->COMSETTER(UseHostIOCache)(ctlData.fUseHostIOCache);
9328 if (FAILED(rc)) return rc;
9329
9330 /* Load the attached devices now. */
9331 rc = i_loadStorageDevices(pCtl,
9332 ctlData,
9333 puuidRegistry,
9334 puuidSnapshot);
9335 if (FAILED(rc)) return rc;
9336 }
9337
9338 return S_OK;
9339}
9340
9341/**
9342 * Called from i_loadStorageControllers for a controller's devices.
9343 *
9344 * @param aStorageController
9345 * @param data
9346 * @param puuidRegistry media registry ID to set media to or NULL; see
9347 * Machine::i_loadMachineDataFromSettings()
9348 * @param puuidSnapshot pointer to the snapshot ID if this is a snapshot machine
9349 * @return
9350 */
9351HRESULT Machine::i_loadStorageDevices(StorageController *aStorageController,
9352 const settings::StorageController &data,
9353 const Guid *puuidRegistry,
9354 const Guid *puuidSnapshot)
9355{
9356 HRESULT rc = S_OK;
9357
9358 /* paranoia: detect duplicate attachments */
9359 for (settings::AttachedDevicesList::const_iterator
9360 it = data.llAttachedDevices.begin();
9361 it != data.llAttachedDevices.end();
9362 ++it)
9363 {
9364 const settings::AttachedDevice &ad = *it;
9365
9366 for (settings::AttachedDevicesList::const_iterator it2 = it;
9367 it2 != data.llAttachedDevices.end();
9368 ++it2)
9369 {
9370 if (it == it2)
9371 continue;
9372
9373 const settings::AttachedDevice &ad2 = *it2;
9374
9375 if ( ad.lPort == ad2.lPort
9376 && ad.lDevice == ad2.lDevice)
9377 {
9378 return setError(E_FAIL,
9379 tr("Duplicate attachments for storage controller '%s', port %d, device %d of the virtual machine '%s'"),
9380 aStorageController->i_getName().c_str(),
9381 ad.lPort,
9382 ad.lDevice,
9383 mUserData->s.strName.c_str());
9384 }
9385 }
9386 }
9387
9388 for (settings::AttachedDevicesList::const_iterator
9389 it = data.llAttachedDevices.begin();
9390 it != data.llAttachedDevices.end();
9391 ++it)
9392 {
9393 const settings::AttachedDevice &dev = *it;
9394 ComObjPtr<Medium> medium;
9395
9396 switch (dev.deviceType)
9397 {
9398 case DeviceType_Floppy:
9399 case DeviceType_DVD:
9400 if (dev.strHostDriveSrc.isNotEmpty())
9401 rc = mParent->i_host()->i_findHostDriveByName(dev.deviceType, dev.strHostDriveSrc,
9402 false /* fRefresh */, medium);
9403 else
9404 rc = mParent->i_findRemoveableMedium(dev.deviceType,
9405 dev.uuid,
9406 false /* fRefresh */,
9407 false /* aSetError */,
9408 medium);
9409 if (rc == VBOX_E_OBJECT_NOT_FOUND)
9410 // This is not an error. The host drive or UUID might have vanished, so just go
9411 // ahead without this removeable medium attachment
9412 rc = S_OK;
9413 break;
9414
9415 case DeviceType_HardDisk:
9416 {
9417 /* find a hard disk by UUID */
9418 rc = mParent->i_findHardDiskById(dev.uuid, true /* aDoSetError */, &medium);
9419 if (FAILED(rc))
9420 {
9421 if (i_isSnapshotMachine())
9422 {
9423 // wrap another error message around the "cannot find hard disk" set by findHardDisk
9424 // so the user knows that the bad disk is in a snapshot somewhere
9425 com::ErrorInfo info;
9426 return setError(E_FAIL,
9427 tr("A differencing image of snapshot {%RTuuid} could not be found. %ls"),
9428 puuidSnapshot->raw(),
9429 info.getText().raw());
9430 }
9431 else
9432 return rc;
9433 }
9434
9435 AutoWriteLock hdLock(medium COMMA_LOCKVAL_SRC_POS);
9436
9437 if (medium->i_getType() == MediumType_Immutable)
9438 {
9439 if (i_isSnapshotMachine())
9440 return setError(E_FAIL,
9441 tr("Immutable hard disk '%s' with UUID {%RTuuid} cannot be directly attached to snapshot with UUID {%RTuuid} "
9442 "of the virtual machine '%s' ('%s')"),
9443 medium->i_getLocationFull().c_str(),
9444 dev.uuid.raw(),
9445 puuidSnapshot->raw(),
9446 mUserData->s.strName.c_str(),
9447 mData->m_strConfigFileFull.c_str());
9448
9449 return setError(E_FAIL,
9450 tr("Immutable hard disk '%s' with UUID {%RTuuid} cannot be directly attached to the virtual machine '%s' ('%s')"),
9451 medium->i_getLocationFull().c_str(),
9452 dev.uuid.raw(),
9453 mUserData->s.strName.c_str(),
9454 mData->m_strConfigFileFull.c_str());
9455 }
9456
9457 if (medium->i_getType() == MediumType_MultiAttach)
9458 {
9459 if (i_isSnapshotMachine())
9460 return setError(E_FAIL,
9461 tr("Multi-attach hard disk '%s' with UUID {%RTuuid} cannot be directly attached to snapshot with UUID {%RTuuid} "
9462 "of the virtual machine '%s' ('%s')"),
9463 medium->i_getLocationFull().c_str(),
9464 dev.uuid.raw(),
9465 puuidSnapshot->raw(),
9466 mUserData->s.strName.c_str(),
9467 mData->m_strConfigFileFull.c_str());
9468
9469 return setError(E_FAIL,
9470 tr("Multi-attach hard disk '%s' with UUID {%RTuuid} cannot be directly attached to the virtual machine '%s' ('%s')"),
9471 medium->i_getLocationFull().c_str(),
9472 dev.uuid.raw(),
9473 mUserData->s.strName.c_str(),
9474 mData->m_strConfigFileFull.c_str());
9475 }
9476
9477 if ( !i_isSnapshotMachine()
9478 && medium->i_getChildren().size() != 0
9479 )
9480 return setError(E_FAIL,
9481 tr("Hard disk '%s' with UUID {%RTuuid} cannot be directly attached to the virtual machine '%s' ('%s') "
9482 "because it has %d differencing child hard disks"),
9483 medium->i_getLocationFull().c_str(),
9484 dev.uuid.raw(),
9485 mUserData->s.strName.c_str(),
9486 mData->m_strConfigFileFull.c_str(),
9487 medium->i_getChildren().size());
9488
9489 if (i_findAttachment(*mMediumAttachments.data(),
9490 medium))
9491 return setError(E_FAIL,
9492 tr("Hard disk '%s' with UUID {%RTuuid} is already attached to the virtual machine '%s' ('%s')"),
9493 medium->i_getLocationFull().c_str(),
9494 dev.uuid.raw(),
9495 mUserData->s.strName.c_str(),
9496 mData->m_strConfigFileFull.c_str());
9497
9498 break;
9499 }
9500
9501 default:
9502 return setError(E_FAIL,
9503 tr("Device '%s' with unknown type is attached to the virtual machine '%s' ('%s')"),
9504 medium->i_getLocationFull().c_str(),
9505 mUserData->s.strName.c_str(),
9506 mData->m_strConfigFileFull.c_str());
9507 }
9508
9509 if (FAILED(rc))
9510 break;
9511
9512 /* Bandwidth groups are loaded at this point. */
9513 ComObjPtr<BandwidthGroup> pBwGroup;
9514
9515 if (!dev.strBwGroup.isEmpty())
9516 {
9517 rc = mBandwidthControl->i_getBandwidthGroupByName(dev.strBwGroup, pBwGroup, false /* aSetError */);
9518 if (FAILED(rc))
9519 return setError(E_FAIL,
9520 tr("Device '%s' with unknown bandwidth group '%s' is attached to the virtual machine '%s' ('%s')"),
9521 medium->i_getLocationFull().c_str(),
9522 dev.strBwGroup.c_str(),
9523 mUserData->s.strName.c_str(),
9524 mData->m_strConfigFileFull.c_str());
9525 pBwGroup->i_reference();
9526 }
9527
9528 const Bstr controllerName = aStorageController->i_getName();
9529 ComObjPtr<MediumAttachment> pAttachment;
9530 pAttachment.createObject();
9531 rc = pAttachment->init(this,
9532 medium,
9533 controllerName,
9534 dev.lPort,
9535 dev.lDevice,
9536 dev.deviceType,
9537 false,
9538 dev.fPassThrough,
9539 dev.fTempEject,
9540 dev.fNonRotational,
9541 dev.fDiscard,
9542 dev.fHotPluggable,
9543 pBwGroup.isNull() ? Utf8Str::Empty : pBwGroup->i_getName());
9544 if (FAILED(rc)) break;
9545
9546 /* associate the medium with this machine and snapshot */
9547 if (!medium.isNull())
9548 {
9549 AutoCaller medCaller(medium);
9550 if (FAILED(medCaller.rc())) return medCaller.rc();
9551 AutoWriteLock mlock(medium COMMA_LOCKVAL_SRC_POS);
9552
9553 if (i_isSnapshotMachine())
9554 rc = medium->i_addBackReference(mData->mUuid, *puuidSnapshot);
9555 else
9556 rc = medium->i_addBackReference(mData->mUuid);
9557 /* If the medium->addBackReference fails it sets an appropriate
9558 * error message, so no need to do any guesswork here. */
9559
9560 if (puuidRegistry)
9561 // caller wants registry ID to be set on all attached media (OVF import case)
9562 medium->i_addRegistry(*puuidRegistry);
9563 }
9564
9565 if (FAILED(rc))
9566 break;
9567
9568 /* back up mMediumAttachments to let registeredInit() properly rollback
9569 * on failure (= limited accessibility) */
9570 i_setModified(IsModified_Storage);
9571 mMediumAttachments.backup();
9572 mMediumAttachments->push_back(pAttachment);
9573 }
9574
9575 return rc;
9576}
9577
9578/**
9579 * Returns the snapshot with the given UUID or fails of no such snapshot exists.
9580 *
9581 * @param aId snapshot UUID to find (empty UUID refers the first snapshot)
9582 * @param aSnapshot where to return the found snapshot
9583 * @param aSetError true to set extended error info on failure
9584 */
9585HRESULT Machine::i_findSnapshotById(const Guid &aId,
9586 ComObjPtr<Snapshot> &aSnapshot,
9587 bool aSetError /* = false */)
9588{
9589 AutoReadLock chlock(this COMMA_LOCKVAL_SRC_POS);
9590
9591 if (!mData->mFirstSnapshot)
9592 {
9593 if (aSetError)
9594 return setError(E_FAIL, tr("This machine does not have any snapshots"));
9595 return E_FAIL;
9596 }
9597
9598 if (aId.isZero())
9599 aSnapshot = mData->mFirstSnapshot;
9600 else
9601 aSnapshot = mData->mFirstSnapshot->i_findChildOrSelf(aId.ref());
9602
9603 if (!aSnapshot)
9604 {
9605 if (aSetError)
9606 return setError(E_FAIL,
9607 tr("Could not find a snapshot with UUID {%s}"),
9608 aId.toString().c_str());
9609 return E_FAIL;
9610 }
9611
9612 return S_OK;
9613}
9614
9615/**
9616 * Returns the snapshot with the given name or fails of no such snapshot.
9617 *
9618 * @param strName snapshot name to find
9619 * @param aSnapshot where to return the found snapshot
9620 * @param aSetError true to set extended error info on failure
9621 */
9622HRESULT Machine::i_findSnapshotByName(const Utf8Str &strName,
9623 ComObjPtr<Snapshot> &aSnapshot,
9624 bool aSetError /* = false */)
9625{
9626 AssertReturn(!strName.isEmpty(), E_INVALIDARG);
9627
9628 AutoReadLock chlock(this COMMA_LOCKVAL_SRC_POS);
9629
9630 if (!mData->mFirstSnapshot)
9631 {
9632 if (aSetError)
9633 return setError(VBOX_E_OBJECT_NOT_FOUND,
9634 tr("This machine does not have any snapshots"));
9635 return VBOX_E_OBJECT_NOT_FOUND;
9636 }
9637
9638 aSnapshot = mData->mFirstSnapshot->i_findChildOrSelf(strName);
9639
9640 if (!aSnapshot)
9641 {
9642 if (aSetError)
9643 return setError(VBOX_E_OBJECT_NOT_FOUND,
9644 tr("Could not find a snapshot named '%s'"), strName.c_str());
9645 return VBOX_E_OBJECT_NOT_FOUND;
9646 }
9647
9648 return S_OK;
9649}
9650
9651/**
9652 * Returns a storage controller object with the given name.
9653 *
9654 * @param aName storage controller name to find
9655 * @param aStorageController where to return the found storage controller
9656 * @param aSetError true to set extended error info on failure
9657 */
9658HRESULT Machine::i_getStorageControllerByName(const Utf8Str &aName,
9659 ComObjPtr<StorageController> &aStorageController,
9660 bool aSetError /* = false */)
9661{
9662 AssertReturn(!aName.isEmpty(), E_INVALIDARG);
9663
9664 for (StorageControllerList::const_iterator
9665 it = mStorageControllers->begin();
9666 it != mStorageControllers->end();
9667 ++it)
9668 {
9669 if ((*it)->i_getName() == aName)
9670 {
9671 aStorageController = (*it);
9672 return S_OK;
9673 }
9674 }
9675
9676 if (aSetError)
9677 return setError(VBOX_E_OBJECT_NOT_FOUND,
9678 tr("Could not find a storage controller named '%s'"),
9679 aName.c_str());
9680 return VBOX_E_OBJECT_NOT_FOUND;
9681}
9682
9683/**
9684 * Returns a USB controller object with the given name.
9685 *
9686 * @param aName USB controller name to find
9687 * @param aUSBController where to return the found USB controller
9688 * @param aSetError true to set extended error info on failure
9689 */
9690HRESULT Machine::i_getUSBControllerByName(const Utf8Str &aName,
9691 ComObjPtr<USBController> &aUSBController,
9692 bool aSetError /* = false */)
9693{
9694 AssertReturn(!aName.isEmpty(), E_INVALIDARG);
9695
9696 for (USBControllerList::const_iterator
9697 it = mUSBControllers->begin();
9698 it != mUSBControllers->end();
9699 ++it)
9700 {
9701 if ((*it)->i_getName() == aName)
9702 {
9703 aUSBController = (*it);
9704 return S_OK;
9705 }
9706 }
9707
9708 if (aSetError)
9709 return setError(VBOX_E_OBJECT_NOT_FOUND,
9710 tr("Could not find a storage controller named '%s'"),
9711 aName.c_str());
9712 return VBOX_E_OBJECT_NOT_FOUND;
9713}
9714
9715/**
9716 * Returns the number of USB controller instance of the given type.
9717 *
9718 * @param enmType USB controller type.
9719 */
9720ULONG Machine::i_getUSBControllerCountByType(USBControllerType_T enmType)
9721{
9722 ULONG cCtrls = 0;
9723
9724 for (USBControllerList::const_iterator
9725 it = mUSBControllers->begin();
9726 it != mUSBControllers->end();
9727 ++it)
9728 {
9729 if ((*it)->i_getControllerType() == enmType)
9730 cCtrls++;
9731 }
9732
9733 return cCtrls;
9734}
9735
9736HRESULT Machine::i_getMediumAttachmentsOfController(const Utf8Str &aName,
9737 MediumAttachmentList &atts)
9738{
9739 AutoCaller autoCaller(this);
9740 if (FAILED(autoCaller.rc())) return autoCaller.rc();
9741
9742 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
9743
9744 for (MediumAttachmentList::const_iterator
9745 it = mMediumAttachments->begin();
9746 it != mMediumAttachments->end();
9747 ++it)
9748 {
9749 const ComObjPtr<MediumAttachment> &pAtt = *it;
9750 // should never happen, but deal with NULL pointers in the list.
9751 AssertContinue(!pAtt.isNull());
9752
9753 // getControllerName() needs caller+read lock
9754 AutoCaller autoAttCaller(pAtt);
9755 if (FAILED(autoAttCaller.rc()))
9756 {
9757 atts.clear();
9758 return autoAttCaller.rc();
9759 }
9760 AutoReadLock attLock(pAtt COMMA_LOCKVAL_SRC_POS);
9761
9762 if (pAtt->i_getControllerName() == aName)
9763 atts.push_back(pAtt);
9764 }
9765
9766 return S_OK;
9767}
9768
9769
9770/**
9771 * Helper for #i_saveSettings. Cares about renaming the settings directory and
9772 * file if the machine name was changed and about creating a new settings file
9773 * if this is a new machine.
9774 *
9775 * @note Must be never called directly but only from #saveSettings().
9776 */
9777HRESULT Machine::i_prepareSaveSettings(bool *pfNeedsGlobalSaveSettings)
9778{
9779 AssertReturn(isWriteLockOnCurrentThread(), E_FAIL);
9780
9781 HRESULT rc = S_OK;
9782
9783 bool fSettingsFileIsNew = !mData->pMachineConfigFile->fileExists();
9784
9785 /// @todo need to handle primary group change, too
9786
9787 /* attempt to rename the settings file if machine name is changed */
9788 if ( mUserData->s.fNameSync
9789 && mUserData.isBackedUp()
9790 && ( mUserData.backedUpData()->s.strName != mUserData->s.strName
9791 || mUserData.backedUpData()->s.llGroups.front() != mUserData->s.llGroups.front())
9792 )
9793 {
9794 bool dirRenamed = false;
9795 bool fileRenamed = false;
9796
9797 Utf8Str configFile, newConfigFile;
9798 Utf8Str configFilePrev, newConfigFilePrev;
9799 Utf8Str configDir, newConfigDir;
9800
9801 do
9802 {
9803 int vrc = VINF_SUCCESS;
9804
9805 Utf8Str name = mUserData.backedUpData()->s.strName;
9806 Utf8Str newName = mUserData->s.strName;
9807 Utf8Str group = mUserData.backedUpData()->s.llGroups.front();
9808 if (group == "/")
9809 group.setNull();
9810 Utf8Str newGroup = mUserData->s.llGroups.front();
9811 if (newGroup == "/")
9812 newGroup.setNull();
9813
9814 configFile = mData->m_strConfigFileFull;
9815
9816 /* first, rename the directory if it matches the group and machine name */
9817 Utf8Str groupPlusName = Utf8StrFmt("%s%c%s",
9818 group.c_str(), RTPATH_DELIMITER, name.c_str());
9819 /** @todo hack, make somehow use of ComposeMachineFilename */
9820 if (mUserData->s.fDirectoryIncludesUUID)
9821 groupPlusName += Utf8StrFmt(" (%RTuuid)", mData->mUuid.raw());
9822 Utf8Str newGroupPlusName = Utf8StrFmt("%s%c%s",
9823 newGroup.c_str(), RTPATH_DELIMITER, newName.c_str());
9824 /** @todo hack, make somehow use of ComposeMachineFilename */
9825 if (mUserData->s.fDirectoryIncludesUUID)
9826 newGroupPlusName += Utf8StrFmt(" (%RTuuid)", mData->mUuid.raw());
9827 configDir = configFile;
9828 configDir.stripFilename();
9829 newConfigDir = configDir;
9830 if ( configDir.length() >= groupPlusName.length()
9831 && !RTPathCompare(configDir.substr(configDir.length() - groupPlusName.length(), groupPlusName.length()).c_str(),
9832 groupPlusName.c_str()))
9833 {
9834 newConfigDir = newConfigDir.substr(0, configDir.length() - groupPlusName.length());
9835 Utf8Str newConfigBaseDir(newConfigDir);
9836 newConfigDir.append(newGroupPlusName);
9837 /* consistency: use \ if appropriate on the platform */
9838 RTPathChangeToDosSlashes(newConfigDir.mutableRaw(), false);
9839 /* new dir and old dir cannot be equal here because of 'if'
9840 * above and because name != newName */
9841 Assert(configDir != newConfigDir);
9842 if (!fSettingsFileIsNew)
9843 {
9844 /* perform real rename only if the machine is not new */
9845 vrc = RTPathRename(configDir.c_str(), newConfigDir.c_str(), 0);
9846 if ( vrc == VERR_FILE_NOT_FOUND
9847 || vrc == VERR_PATH_NOT_FOUND)
9848 {
9849 /* create the parent directory, then retry renaming */
9850 Utf8Str parent(newConfigDir);
9851 parent.stripFilename();
9852 (void)RTDirCreateFullPath(parent.c_str(), 0700);
9853 vrc = RTPathRename(configDir.c_str(), newConfigDir.c_str(), 0);
9854 }
9855 if (RT_FAILURE(vrc))
9856 {
9857 rc = setError(E_FAIL,
9858 tr("Could not rename the directory '%s' to '%s' to save the settings file (%Rrc)"),
9859 configDir.c_str(),
9860 newConfigDir.c_str(),
9861 vrc);
9862 break;
9863 }
9864 /* delete subdirectories which are no longer needed */
9865 Utf8Str dir(configDir);
9866 dir.stripFilename();
9867 while (dir != newConfigBaseDir && dir != ".")
9868 {
9869 vrc = RTDirRemove(dir.c_str());
9870 if (RT_FAILURE(vrc))
9871 break;
9872 dir.stripFilename();
9873 }
9874 dirRenamed = true;
9875 }
9876 }
9877
9878 newConfigFile = Utf8StrFmt("%s%c%s.vbox",
9879 newConfigDir.c_str(), RTPATH_DELIMITER, newName.c_str());
9880
9881 /* then try to rename the settings file itself */
9882 if (newConfigFile != configFile)
9883 {
9884 /* get the path to old settings file in renamed directory */
9885 configFile = Utf8StrFmt("%s%c%s",
9886 newConfigDir.c_str(),
9887 RTPATH_DELIMITER,
9888 RTPathFilename(configFile.c_str()));
9889 if (!fSettingsFileIsNew)
9890 {
9891 /* perform real rename only if the machine is not new */
9892 vrc = RTFileRename(configFile.c_str(), newConfigFile.c_str(), 0);
9893 if (RT_FAILURE(vrc))
9894 {
9895 rc = setError(E_FAIL,
9896 tr("Could not rename the settings file '%s' to '%s' (%Rrc)"),
9897 configFile.c_str(),
9898 newConfigFile.c_str(),
9899 vrc);
9900 break;
9901 }
9902 fileRenamed = true;
9903 configFilePrev = configFile;
9904 configFilePrev += "-prev";
9905 newConfigFilePrev = newConfigFile;
9906 newConfigFilePrev += "-prev";
9907 RTFileRename(configFilePrev.c_str(), newConfigFilePrev.c_str(), 0);
9908 }
9909 }
9910
9911 // update m_strConfigFileFull amd mConfigFile
9912 mData->m_strConfigFileFull = newConfigFile;
9913 // compute the relative path too
9914 mParent->i_copyPathRelativeToConfig(newConfigFile, mData->m_strConfigFile);
9915
9916 // store the old and new so that VirtualBox::i_saveSettings() can update
9917 // the media registry
9918 if ( mData->mRegistered
9919 && (configDir != newConfigDir || configFile != newConfigFile))
9920 {
9921 mParent->i_rememberMachineNameChangeForMedia(configDir, newConfigDir);
9922
9923 if (pfNeedsGlobalSaveSettings)
9924 *pfNeedsGlobalSaveSettings = true;
9925 }
9926
9927 // in the saved state file path, replace the old directory with the new directory
9928 if (RTPathStartsWith(mSSData->strStateFilePath.c_str(), configDir.c_str()))
9929 {
9930 // do not change newConfigDir!
9931 Utf8Str strAppend = mSSData->strStateFilePath.c_str() + configDir.length();
9932 mSSData->strStateFilePath = newConfigDir;
9933 mSSData->strStateFilePath.append(strAppend);
9934 }
9935
9936 // and do the same thing for the saved state file paths of all the online snapshots
9937 if (mData->mFirstSnapshot)
9938 mData->mFirstSnapshot->i_updateSavedStatePaths(configDir.c_str(),
9939 newConfigDir.c_str());
9940 }
9941 while (0);
9942
9943 if (FAILED(rc))
9944 {
9945 /* silently try to rename everything back */
9946 if (fileRenamed)
9947 {
9948 RTFileRename(newConfigFilePrev.c_str(), configFilePrev.c_str(), 0);
9949 RTFileRename(newConfigFile.c_str(), configFile.c_str(), 0);
9950 }
9951 if (dirRenamed)
9952 RTPathRename(newConfigDir.c_str(), configDir.c_str(), 0);
9953 }
9954
9955 if (FAILED(rc)) return rc;
9956 }
9957
9958 if (fSettingsFileIsNew)
9959 {
9960 /* create a virgin config file */
9961 int vrc = VINF_SUCCESS;
9962
9963 /* ensure the settings directory exists */
9964 Utf8Str path(mData->m_strConfigFileFull);
9965 path.stripFilename();
9966 if (!RTDirExists(path.c_str()))
9967 {
9968 vrc = RTDirCreateFullPath(path.c_str(), 0700);
9969 if (RT_FAILURE(vrc))
9970 {
9971 return setError(E_FAIL,
9972 tr("Could not create a directory '%s' to save the settings file (%Rrc)"),
9973 path.c_str(),
9974 vrc);
9975 }
9976 }
9977
9978 /* Note: open flags must correlate with RTFileOpen() in lockConfig() */
9979 path = Utf8Str(mData->m_strConfigFileFull);
9980 RTFILE f = NIL_RTFILE;
9981 vrc = RTFileOpen(&f, path.c_str(),
9982 RTFILE_O_READWRITE | RTFILE_O_CREATE | RTFILE_O_DENY_WRITE);
9983 if (RT_FAILURE(vrc))
9984 return setError(E_FAIL,
9985 tr("Could not create the settings file '%s' (%Rrc)"),
9986 path.c_str(),
9987 vrc);
9988 RTFileClose(f);
9989 }
9990
9991 return rc;
9992}
9993
9994/**
9995 * Saves and commits machine data, user data and hardware data.
9996 *
9997 * Note that on failure, the data remains uncommitted.
9998 *
9999 * @a aFlags may combine the following flags:
10000 *
10001 * - SaveS_ResetCurStateModified: Resets mData->mCurrentStateModified to FALSE.
10002 * Used when saving settings after an operation that makes them 100%
10003 * correspond to the settings from the current snapshot.
10004 * - SaveS_Force: settings will be saved without doing a deep compare of the
10005 * settings structures. This is used when this is called because snapshots
10006 * have changed to avoid the overhead of the deep compare.
10007 *
10008 * @note Must be called from under this object's write lock. Locks children for
10009 * writing.
10010 *
10011 * @param pfNeedsGlobalSaveSettings Optional pointer to a bool that must have been
10012 * initialized to false and that will be set to true by this function if
10013 * the caller must invoke VirtualBox::i_saveSettings() because the global
10014 * settings have changed. This will happen if a machine rename has been
10015 * saved and the global machine and media registries will therefore need
10016 * updating.
10017 * @param aFlags Flags.
10018 */
10019HRESULT Machine::i_saveSettings(bool *pfNeedsGlobalSaveSettings,
10020 int aFlags /*= 0*/)
10021{
10022 LogFlowThisFuncEnter();
10023
10024 AssertReturn(isWriteLockOnCurrentThread(), E_FAIL);
10025
10026 /* make sure child objects are unable to modify the settings while we are
10027 * saving them */
10028 i_ensureNoStateDependencies();
10029
10030 AssertReturn(!i_isSnapshotMachine(),
10031 E_FAIL);
10032
10033 HRESULT rc = S_OK;
10034 bool fNeedsWrite = false;
10035
10036 /* First, prepare to save settings. It will care about renaming the
10037 * settings directory and file if the machine name was changed and about
10038 * creating a new settings file if this is a new machine. */
10039 rc = i_prepareSaveSettings(pfNeedsGlobalSaveSettings);
10040 if (FAILED(rc)) return rc;
10041
10042 // keep a pointer to the current settings structures
10043 settings::MachineConfigFile *pOldConfig = mData->pMachineConfigFile;
10044 settings::MachineConfigFile *pNewConfig = NULL;
10045
10046 try
10047 {
10048 // make a fresh one to have everyone write stuff into
10049 pNewConfig = new settings::MachineConfigFile(NULL);
10050 pNewConfig->copyBaseFrom(*mData->pMachineConfigFile);
10051
10052 // now go and copy all the settings data from COM to the settings structures
10053 // (this calls i_saveSettings() on all the COM objects in the machine)
10054 i_copyMachineDataToSettings(*pNewConfig);
10055
10056 if (aFlags & SaveS_ResetCurStateModified)
10057 {
10058 // this gets set by takeSnapshot() (if offline snapshot) and restoreSnapshot()
10059 mData->mCurrentStateModified = FALSE;
10060 fNeedsWrite = true; // always, no need to compare
10061 }
10062 else if (aFlags & SaveS_Force)
10063 {
10064 fNeedsWrite = true; // always, no need to compare
10065 }
10066 else
10067 {
10068 if (!mData->mCurrentStateModified)
10069 {
10070 // do a deep compare of the settings that we just saved with the settings
10071 // previously stored in the config file; this invokes MachineConfigFile::operator==
10072 // which does a deep compare of all the settings, which is expensive but less expensive
10073 // than writing out XML in vain
10074 bool fAnySettingsChanged = !(*pNewConfig == *pOldConfig);
10075
10076 // could still be modified if any settings changed
10077 mData->mCurrentStateModified = fAnySettingsChanged;
10078
10079 fNeedsWrite = fAnySettingsChanged;
10080 }
10081 else
10082 fNeedsWrite = true;
10083 }
10084
10085 pNewConfig->fCurrentStateModified = !!mData->mCurrentStateModified;
10086
10087 if (fNeedsWrite)
10088 // now spit it all out!
10089 pNewConfig->write(mData->m_strConfigFileFull);
10090
10091 mData->pMachineConfigFile = pNewConfig;
10092 delete pOldConfig;
10093 i_commit();
10094
10095 // after saving settings, we are no longer different from the XML on disk
10096 mData->flModifications = 0;
10097 }
10098 catch (HRESULT err)
10099 {
10100 // we assume that error info is set by the thrower
10101 rc = err;
10102
10103 // restore old config
10104 delete pNewConfig;
10105 mData->pMachineConfigFile = pOldConfig;
10106 }
10107 catch (...)
10108 {
10109 rc = VirtualBoxBase::handleUnexpectedExceptions(this, RT_SRC_POS);
10110 }
10111
10112 if (fNeedsWrite)
10113 {
10114 /* Fire the data change event, even on failure (since we've already
10115 * committed all data). This is done only for SessionMachines because
10116 * mutable Machine instances are always not registered (i.e. private
10117 * to the client process that creates them) and thus don't need to
10118 * inform callbacks. */
10119 if (i_isSessionMachine())
10120 mParent->i_onMachineDataChange(mData->mUuid);
10121 }
10122
10123 LogFlowThisFunc(("rc=%08X\n", rc));
10124 LogFlowThisFuncLeave();
10125 return rc;
10126}
10127
10128/**
10129 * Implementation for saving the machine settings into the given
10130 * settings::MachineConfigFile instance. This copies machine extradata
10131 * from the previous machine config file in the instance data, if any.
10132 *
10133 * This gets called from two locations:
10134 *
10135 * -- Machine::i_saveSettings(), during the regular XML writing;
10136 *
10137 * -- Appliance::buildXMLForOneVirtualSystem(), when a machine gets
10138 * exported to OVF and we write the VirtualBox proprietary XML
10139 * into a <vbox:Machine> tag.
10140 *
10141 * This routine fills all the fields in there, including snapshots, *except*
10142 * for the following:
10143 *
10144 * -- fCurrentStateModified. There is some special logic associated with that.
10145 *
10146 * The caller can then call MachineConfigFile::write() or do something else
10147 * with it.
10148 *
10149 * Caller must hold the machine lock!
10150 *
10151 * This throws XML errors and HRESULT, so the caller must have a catch block!
10152 */
10153void Machine::i_copyMachineDataToSettings(settings::MachineConfigFile &config)
10154{
10155 // deep copy extradata, being extra careful with self assignment (the STL
10156 // map assignment on Mac OS X clang based Xcode isn't checking)
10157 if (&config != mData->pMachineConfigFile)
10158 config.mapExtraDataItems = mData->pMachineConfigFile->mapExtraDataItems;
10159
10160 config.uuid = mData->mUuid;
10161
10162 // copy name, description, OS type, teleport, UTC etc.
10163 config.machineUserData = mUserData->s;
10164
10165 if ( mData->mMachineState == MachineState_Saved
10166 || mData->mMachineState == MachineState_Restoring
10167 // when doing certain snapshot operations we may or may not have
10168 // a saved state in the current state, so keep everything as is
10169 || ( ( mData->mMachineState == MachineState_Snapshotting
10170 || mData->mMachineState == MachineState_DeletingSnapshot
10171 || mData->mMachineState == MachineState_RestoringSnapshot)
10172 && (!mSSData->strStateFilePath.isEmpty())
10173 )
10174 )
10175 {
10176 Assert(!mSSData->strStateFilePath.isEmpty());
10177 /* try to make the file name relative to the settings file dir */
10178 i_copyPathRelativeToMachine(mSSData->strStateFilePath, config.strStateFile);
10179 }
10180 else
10181 {
10182 Assert(mSSData->strStateFilePath.isEmpty() || mData->mMachineState == MachineState_Saving);
10183 config.strStateFile.setNull();
10184 }
10185
10186 if (mData->mCurrentSnapshot)
10187 config.uuidCurrentSnapshot = mData->mCurrentSnapshot->i_getId();
10188 else
10189 config.uuidCurrentSnapshot.clear();
10190
10191 config.timeLastStateChange = mData->mLastStateChange;
10192 config.fAborted = (mData->mMachineState == MachineState_Aborted);
10193 /// @todo Live Migration: config.fTeleported = (mData->mMachineState == MachineState_Teleported);
10194
10195 HRESULT rc = i_saveHardware(config.hardwareMachine, &config.debugging, &config.autostart);
10196 if (FAILED(rc)) throw rc;
10197
10198 // save machine's media registry if this is VirtualBox 4.0 or later
10199 if (config.canHaveOwnMediaRegistry())
10200 {
10201 // determine machine folder
10202 Utf8Str strMachineFolder = i_getSettingsFileFull();
10203 strMachineFolder.stripFilename();
10204 mParent->i_saveMediaRegistry(config.mediaRegistry,
10205 i_getId(), // only media with registry ID == machine UUID
10206 strMachineFolder);
10207 // this throws HRESULT
10208 }
10209
10210 // save snapshots
10211 rc = i_saveAllSnapshots(config);
10212 if (FAILED(rc)) throw rc;
10213}
10214
10215/**
10216 * Saves all snapshots of the machine into the given machine config file. Called
10217 * from Machine::buildMachineXML() and SessionMachine::deleteSnapshotHandler().
10218 * @param config
10219 * @return
10220 */
10221HRESULT Machine::i_saveAllSnapshots(settings::MachineConfigFile &config)
10222{
10223 AssertReturn(isWriteLockOnCurrentThread(), E_FAIL);
10224
10225 HRESULT rc = S_OK;
10226
10227 try
10228 {
10229 config.llFirstSnapshot.clear();
10230
10231 if (mData->mFirstSnapshot)
10232 {
10233 // the settings use a list for "the first snapshot"
10234 config.llFirstSnapshot.push_back(settings::Snapshot::Empty);
10235
10236 // get reference to the snapshot on the list and work on that
10237 // element straight in the list to avoid excessive copying later
10238 rc = mData->mFirstSnapshot->i_saveSnapshot(config.llFirstSnapshot.back());
10239 if (FAILED(rc)) throw rc;
10240 }
10241
10242// if (mType == IsSessionMachine)
10243// mParent->onMachineDataChange(mData->mUuid); @todo is this necessary?
10244
10245 }
10246 catch (HRESULT err)
10247 {
10248 /* we assume that error info is set by the thrower */
10249 rc = err;
10250 }
10251 catch (...)
10252 {
10253 rc = VirtualBoxBase::handleUnexpectedExceptions(this, RT_SRC_POS);
10254 }
10255
10256 return rc;
10257}
10258
10259/**
10260 * Saves the VM hardware configuration. It is assumed that the
10261 * given node is empty.
10262 *
10263 * @param data Reference to the settings object for the hardware config.
10264 * @param pDbg Pointer to the settings object for the debugging config
10265 * which happens to live in mHWData.
10266 * @param pAutostart Pointer to the settings object for the autostart config
10267 * which happens to live in mHWData.
10268 */
10269HRESULT Machine::i_saveHardware(settings::Hardware &data, settings::Debugging *pDbg,
10270 settings::Autostart *pAutostart)
10271{
10272 HRESULT rc = S_OK;
10273
10274 try
10275 {
10276 /* The hardware version attribute (optional).
10277 Automatically upgrade from 1 to 2 when there is no saved state. (ugly!) */
10278 if ( mHWData->mHWVersion == "1"
10279 && mSSData->strStateFilePath.isEmpty()
10280 )
10281 mHWData->mHWVersion = "2"; /** @todo Is this safe, to update mHWVersion here? If not some
10282 other point needs to be found where this can be done. */
10283
10284 data.strVersion = mHWData->mHWVersion;
10285 data.uuid = mHWData->mHardwareUUID;
10286
10287 // CPU
10288 data.fHardwareVirt = !!mHWData->mHWVirtExEnabled;
10289 data.fNestedPaging = !!mHWData->mHWVirtExNestedPagingEnabled;
10290 data.fLargePages = !!mHWData->mHWVirtExLargePagesEnabled;
10291 data.fVPID = !!mHWData->mHWVirtExVPIDEnabled;
10292 data.fUnrestrictedExecution = !!mHWData->mHWVirtExUXEnabled;
10293 data.fHardwareVirtForce = !!mHWData->mHWVirtExForceEnabled;
10294 data.fPAE = !!mHWData->mPAEEnabled;
10295 data.enmLongMode = mHWData->mLongMode;
10296 data.fTripleFaultReset = !!mHWData->mTripleFaultReset;
10297 data.fAPIC = !!mHWData->mAPIC;
10298 data.fX2APIC = !!mHWData->mX2APIC;
10299 data.cCPUs = mHWData->mCPUCount;
10300 data.fCpuHotPlug = !!mHWData->mCPUHotPlugEnabled;
10301 data.ulCpuExecutionCap = mHWData->mCpuExecutionCap;
10302 data.uCpuIdPortabilityLevel = mHWData->mCpuIdPortabilityLevel;
10303 data.strCpuProfile = mHWData->mCpuProfile;
10304
10305 data.llCpus.clear();
10306 if (data.fCpuHotPlug)
10307 {
10308 for (unsigned idx = 0; idx < data.cCPUs; ++idx)
10309 {
10310 if (mHWData->mCPUAttached[idx])
10311 {
10312 settings::Cpu cpu;
10313 cpu.ulId = idx;
10314 data.llCpus.push_back(cpu);
10315 }
10316 }
10317 }
10318
10319 /* Standard and Extended CPUID leafs. */
10320 data.llCpuIdLeafs.clear();
10321 for (unsigned idx = 0; idx < RT_ELEMENTS(mHWData->mCpuIdStdLeafs); ++idx)
10322 if (mHWData->mCpuIdStdLeafs[idx].ulId != UINT32_MAX)
10323 data.llCpuIdLeafs.push_back(mHWData->mCpuIdStdLeafs[idx]);
10324 for (unsigned idx = 0; idx < RT_ELEMENTS(mHWData->mCpuIdExtLeafs); ++idx)
10325 if (mHWData->mCpuIdExtLeafs[idx].ulId != UINT32_MAX)
10326 data.llCpuIdLeafs.push_back(mHWData->mCpuIdExtLeafs[idx]);
10327
10328 // memory
10329 data.ulMemorySizeMB = mHWData->mMemorySize;
10330 data.fPageFusionEnabled = !!mHWData->mPageFusionEnabled;
10331
10332 // firmware
10333 data.firmwareType = mHWData->mFirmwareType;
10334
10335 // HID
10336 data.pointingHIDType = mHWData->mPointingHIDType;
10337 data.keyboardHIDType = mHWData->mKeyboardHIDType;
10338
10339 // chipset
10340 data.chipsetType = mHWData->mChipsetType;
10341
10342 // paravirt
10343 data.paravirtProvider = mHWData->mParavirtProvider;
10344 data.strParavirtDebug = mHWData->mParavirtDebug;
10345
10346 // emulated USB card reader
10347 data.fEmulatedUSBCardReader = !!mHWData->mEmulatedUSBCardReaderEnabled;
10348
10349 // HPET
10350 data.fHPETEnabled = !!mHWData->mHPETEnabled;
10351
10352 // boot order
10353 data.mapBootOrder.clear();
10354 for (unsigned i = 0; i < RT_ELEMENTS(mHWData->mBootOrder); ++i)
10355 data.mapBootOrder[i] = mHWData->mBootOrder[i];
10356
10357 // display
10358 data.graphicsControllerType = mHWData->mGraphicsControllerType;
10359 data.ulVRAMSizeMB = mHWData->mVRAMSize;
10360 data.cMonitors = mHWData->mMonitorCount;
10361 data.fAccelerate3D = !!mHWData->mAccelerate3DEnabled;
10362 data.fAccelerate2DVideo = !!mHWData->mAccelerate2DVideoEnabled;
10363 data.ulVideoCaptureHorzRes = mHWData->mVideoCaptureWidth;
10364 data.ulVideoCaptureVertRes = mHWData->mVideoCaptureHeight;
10365 data.ulVideoCaptureRate = mHWData->mVideoCaptureRate;
10366 data.ulVideoCaptureFPS = mHWData->mVideoCaptureFPS;
10367 data.fVideoCaptureEnabled = !!mHWData->mVideoCaptureEnabled;
10368 for (unsigned i = 0; i < sizeof(data.u64VideoCaptureScreens) * 8; ++i)
10369 {
10370 if (mHWData->maVideoCaptureScreens[i])
10371 ASMBitSet(&data.u64VideoCaptureScreens, i);
10372 else
10373 ASMBitClear(&data.u64VideoCaptureScreens, i);
10374 }
10375 /* store relative video capture file if possible */
10376 i_copyPathRelativeToMachine(mHWData->mVideoCaptureFile, data.strVideoCaptureFile);
10377
10378 /* VRDEServer settings (optional) */
10379 rc = mVRDEServer->i_saveSettings(data.vrdeSettings);
10380 if (FAILED(rc)) throw rc;
10381
10382 /* BIOS (required) */
10383 rc = mBIOSSettings->i_saveSettings(data.biosSettings);
10384 if (FAILED(rc)) throw rc;
10385
10386 /* USB Controller (required) */
10387 data.usbSettings.llUSBControllers.clear();
10388 for (USBControllerList::const_iterator
10389 it = mUSBControllers->begin();
10390 it != mUSBControllers->end();
10391 ++it)
10392 {
10393 ComObjPtr<USBController> ctrl = *it;
10394 settings::USBController settingsCtrl;
10395
10396 settingsCtrl.strName = ctrl->i_getName();
10397 settingsCtrl.enmType = ctrl->i_getControllerType();
10398
10399 data.usbSettings.llUSBControllers.push_back(settingsCtrl);
10400 }
10401
10402 /* USB device filters (required) */
10403 rc = mUSBDeviceFilters->i_saveSettings(data.usbSettings);
10404 if (FAILED(rc)) throw rc;
10405
10406 /* Network adapters (required) */
10407 size_t uMaxNICs = RT_MIN(Global::getMaxNetworkAdapters(mHWData->mChipsetType), mNetworkAdapters.size());
10408 data.llNetworkAdapters.clear();
10409 /* Write out only the nominal number of network adapters for this
10410 * chipset type. Since Machine::commit() hasn't been called there
10411 * may be extra NIC settings in the vector. */
10412 for (size_t slot = 0; slot < uMaxNICs; ++slot)
10413 {
10414 settings::NetworkAdapter nic;
10415 nic.ulSlot = (uint32_t)slot;
10416 /* paranoia check... must not be NULL, but must not crash either. */
10417 if (mNetworkAdapters[slot])
10418 {
10419 rc = mNetworkAdapters[slot]->i_saveSettings(nic);
10420 if (FAILED(rc)) throw rc;
10421
10422 data.llNetworkAdapters.push_back(nic);
10423 }
10424 }
10425
10426 /* Serial ports */
10427 data.llSerialPorts.clear();
10428 for (ULONG slot = 0; slot < RT_ELEMENTS(mSerialPorts); ++slot)
10429 {
10430 if (mSerialPorts[slot]->i_hasDefaults())
10431 continue;
10432
10433 settings::SerialPort s;
10434 s.ulSlot = slot;
10435 rc = mSerialPorts[slot]->i_saveSettings(s);
10436 if (FAILED(rc)) return rc;
10437
10438 data.llSerialPorts.push_back(s);
10439 }
10440
10441 /* Parallel ports */
10442 data.llParallelPorts.clear();
10443 for (ULONG slot = 0; slot < RT_ELEMENTS(mParallelPorts); ++slot)
10444 {
10445 if (mParallelPorts[slot]->i_hasDefaults())
10446 continue;
10447
10448 settings::ParallelPort p;
10449 p.ulSlot = slot;
10450 rc = mParallelPorts[slot]->i_saveSettings(p);
10451 if (FAILED(rc)) return rc;
10452
10453 data.llParallelPorts.push_back(p);
10454 }
10455
10456 /* Audio adapter */
10457 rc = mAudioAdapter->i_saveSettings(data.audioAdapter);
10458 if (FAILED(rc)) return rc;
10459
10460 rc = i_saveStorageControllers(data.storage);
10461 if (FAILED(rc)) return rc;
10462
10463 /* Shared folders */
10464 data.llSharedFolders.clear();
10465 for (HWData::SharedFolderList::const_iterator
10466 it = mHWData->mSharedFolders.begin();
10467 it != mHWData->mSharedFolders.end();
10468 ++it)
10469 {
10470 SharedFolder *pSF = *it;
10471 AutoCaller sfCaller(pSF);
10472 AutoReadLock sfLock(pSF COMMA_LOCKVAL_SRC_POS);
10473 settings::SharedFolder sf;
10474 sf.strName = pSF->i_getName();
10475 sf.strHostPath = pSF->i_getHostPath();
10476 sf.fWritable = !!pSF->i_isWritable();
10477 sf.fAutoMount = !!pSF->i_isAutoMounted();
10478
10479 data.llSharedFolders.push_back(sf);
10480 }
10481
10482 // clipboard
10483 data.clipboardMode = mHWData->mClipboardMode;
10484
10485 // drag'n'drop
10486 data.dndMode = mHWData->mDnDMode;
10487
10488 /* Guest */
10489 data.ulMemoryBalloonSize = mHWData->mMemoryBalloonSize;
10490
10491 // IO settings
10492 data.ioSettings.fIOCacheEnabled = !!mHWData->mIOCacheEnabled;
10493 data.ioSettings.ulIOCacheSize = mHWData->mIOCacheSize;
10494
10495 /* BandwidthControl (required) */
10496 rc = mBandwidthControl->i_saveSettings(data.ioSettings);
10497 if (FAILED(rc)) throw rc;
10498
10499 /* Host PCI devices */
10500 data.pciAttachments.clear();
10501 for (HWData::PCIDeviceAssignmentList::const_iterator
10502 it = mHWData->mPCIDeviceAssignments.begin();
10503 it != mHWData->mPCIDeviceAssignments.end();
10504 ++it)
10505 {
10506 ComObjPtr<PCIDeviceAttachment> pda = *it;
10507 settings::HostPCIDeviceAttachment hpda;
10508
10509 rc = pda->i_saveSettings(hpda);
10510 if (FAILED(rc)) throw rc;
10511
10512 data.pciAttachments.push_back(hpda);
10513 }
10514
10515 // guest properties
10516 data.llGuestProperties.clear();
10517#ifdef VBOX_WITH_GUEST_PROPS
10518 for (HWData::GuestPropertyMap::const_iterator
10519 it = mHWData->mGuestProperties.begin();
10520 it != mHWData->mGuestProperties.end();
10521 ++it)
10522 {
10523 HWData::GuestProperty property = it->second;
10524
10525 /* Remove transient guest properties at shutdown unless we
10526 * are saving state. Note that restoring snapshot intentionally
10527 * keeps them, they will be removed if appropriate once the final
10528 * machine state is set (as crashes etc. need to work). */
10529 if ( ( mData->mMachineState == MachineState_PoweredOff
10530 || mData->mMachineState == MachineState_Aborted
10531 || mData->mMachineState == MachineState_Teleported)
10532 && ( property.mFlags & guestProp::TRANSIENT
10533 || property.mFlags & guestProp::TRANSRESET))
10534 continue;
10535 settings::GuestProperty prop;
10536 prop.strName = it->first;
10537 prop.strValue = property.strValue;
10538 prop.timestamp = property.mTimestamp;
10539 char szFlags[guestProp::MAX_FLAGS_LEN + 1];
10540 guestProp::writeFlags(property.mFlags, szFlags);
10541 prop.strFlags = szFlags;
10542
10543 data.llGuestProperties.push_back(prop);
10544 }
10545
10546 /* I presume this doesn't require a backup(). */
10547 mData->mGuestPropertiesModified = FALSE;
10548#endif /* VBOX_WITH_GUEST_PROPS defined */
10549
10550 *pDbg = mHWData->mDebugging;
10551 *pAutostart = mHWData->mAutostart;
10552
10553 data.strDefaultFrontend = mHWData->mDefaultFrontend;
10554 }
10555 catch (std::bad_alloc &)
10556 {
10557 return E_OUTOFMEMORY;
10558 }
10559
10560 AssertComRC(rc);
10561 return rc;
10562}
10563
10564/**
10565 * Saves the storage controller configuration.
10566 *
10567 * @param data storage settings.
10568 */
10569HRESULT Machine::i_saveStorageControllers(settings::Storage &data)
10570{
10571 data.llStorageControllers.clear();
10572
10573 for (StorageControllerList::const_iterator
10574 it = mStorageControllers->begin();
10575 it != mStorageControllers->end();
10576 ++it)
10577 {
10578 HRESULT rc;
10579 ComObjPtr<StorageController> pCtl = *it;
10580
10581 settings::StorageController ctl;
10582 ctl.strName = pCtl->i_getName();
10583 ctl.controllerType = pCtl->i_getControllerType();
10584 ctl.storageBus = pCtl->i_getStorageBus();
10585 ctl.ulInstance = pCtl->i_getInstance();
10586 ctl.fBootable = pCtl->i_getBootable();
10587
10588 /* Save the port count. */
10589 ULONG portCount;
10590 rc = pCtl->COMGETTER(PortCount)(&portCount);
10591 ComAssertComRCRet(rc, rc);
10592 ctl.ulPortCount = portCount;
10593
10594 /* Save fUseHostIOCache */
10595 BOOL fUseHostIOCache;
10596 rc = pCtl->COMGETTER(UseHostIOCache)(&fUseHostIOCache);
10597 ComAssertComRCRet(rc, rc);
10598 ctl.fUseHostIOCache = !!fUseHostIOCache;
10599
10600 /* save the devices now. */
10601 rc = i_saveStorageDevices(pCtl, ctl);
10602 ComAssertComRCRet(rc, rc);
10603
10604 data.llStorageControllers.push_back(ctl);
10605 }
10606
10607 return S_OK;
10608}
10609
10610/**
10611 * Saves the hard disk configuration.
10612 */
10613HRESULT Machine::i_saveStorageDevices(ComObjPtr<StorageController> aStorageController,
10614 settings::StorageController &data)
10615{
10616 MediumAttachmentList atts;
10617
10618 HRESULT rc = i_getMediumAttachmentsOfController(aStorageController->i_getName(), atts);
10619 if (FAILED(rc)) return rc;
10620
10621 data.llAttachedDevices.clear();
10622 for (MediumAttachmentList::const_iterator
10623 it = atts.begin();
10624 it != atts.end();
10625 ++it)
10626 {
10627 settings::AttachedDevice dev;
10628 IMediumAttachment *iA = *it;
10629 MediumAttachment *pAttach = static_cast<MediumAttachment *>(iA);
10630 Medium *pMedium = pAttach->i_getMedium();
10631
10632 dev.deviceType = pAttach->i_getType();
10633 dev.lPort = pAttach->i_getPort();
10634 dev.lDevice = pAttach->i_getDevice();
10635 dev.fPassThrough = pAttach->i_getPassthrough();
10636 dev.fHotPluggable = pAttach->i_getHotPluggable();
10637 if (pMedium)
10638 {
10639 if (pMedium->i_isHostDrive())
10640 dev.strHostDriveSrc = pMedium->i_getLocationFull();
10641 else
10642 dev.uuid = pMedium->i_getId();
10643 dev.fTempEject = pAttach->i_getTempEject();
10644 dev.fNonRotational = pAttach->i_getNonRotational();
10645 dev.fDiscard = pAttach->i_getDiscard();
10646 }
10647
10648 dev.strBwGroup = pAttach->i_getBandwidthGroup();
10649
10650 data.llAttachedDevices.push_back(dev);
10651 }
10652
10653 return S_OK;
10654}
10655
10656/**
10657 * Saves machine state settings as defined by aFlags
10658 * (SaveSTS_* values).
10659 *
10660 * @param aFlags Combination of SaveSTS_* flags.
10661 *
10662 * @note Locks objects for writing.
10663 */
10664HRESULT Machine::i_saveStateSettings(int aFlags)
10665{
10666 if (aFlags == 0)
10667 return S_OK;
10668
10669 AutoCaller autoCaller(this);
10670 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
10671
10672 /* This object's write lock is also necessary to serialize file access
10673 * (prevent concurrent reads and writes) */
10674 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
10675
10676 HRESULT rc = S_OK;
10677
10678 Assert(mData->pMachineConfigFile);
10679
10680 try
10681 {
10682 if (aFlags & SaveSTS_CurStateModified)
10683 mData->pMachineConfigFile->fCurrentStateModified = true;
10684
10685 if (aFlags & SaveSTS_StateFilePath)
10686 {
10687 if (!mSSData->strStateFilePath.isEmpty())
10688 /* try to make the file name relative to the settings file dir */
10689 i_copyPathRelativeToMachine(mSSData->strStateFilePath, mData->pMachineConfigFile->strStateFile);
10690 else
10691 mData->pMachineConfigFile->strStateFile.setNull();
10692 }
10693
10694 if (aFlags & SaveSTS_StateTimeStamp)
10695 {
10696 Assert( mData->mMachineState != MachineState_Aborted
10697 || mSSData->strStateFilePath.isEmpty());
10698
10699 mData->pMachineConfigFile->timeLastStateChange = mData->mLastStateChange;
10700
10701 mData->pMachineConfigFile->fAborted = (mData->mMachineState == MachineState_Aborted);
10702/// @todo live migration mData->pMachineConfigFile->fTeleported = (mData->mMachineState == MachineState_Teleported);
10703 }
10704
10705 mData->pMachineConfigFile->write(mData->m_strConfigFileFull);
10706 }
10707 catch (...)
10708 {
10709 rc = VirtualBoxBase::handleUnexpectedExceptions(this, RT_SRC_POS);
10710 }
10711
10712 return rc;
10713}
10714
10715/**
10716 * Ensures that the given medium is added to a media registry. If this machine
10717 * was created with 4.0 or later, then the machine registry is used. Otherwise
10718 * the global VirtualBox media registry is used.
10719 *
10720 * Caller must NOT hold machine lock, media tree or any medium locks!
10721 *
10722 * @param pMedium
10723 */
10724void Machine::i_addMediumToRegistry(ComObjPtr<Medium> &pMedium)
10725{
10726 /* Paranoia checks: do not hold machine or media tree locks. */
10727 AssertReturnVoid(!isWriteLockOnCurrentThread());
10728 AssertReturnVoid(!mParent->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
10729
10730 ComObjPtr<Medium> pBase;
10731 {
10732 AutoReadLock treeLock(&mParent->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
10733 pBase = pMedium->i_getBase();
10734 }
10735
10736 /* Paranoia checks: do not hold medium locks. */
10737 AssertReturnVoid(!pMedium->isWriteLockOnCurrentThread());
10738 AssertReturnVoid(!pBase->isWriteLockOnCurrentThread());
10739
10740 // decide which medium registry to use now that the medium is attached:
10741 Guid uuid;
10742 if (mData->pMachineConfigFile->canHaveOwnMediaRegistry())
10743 // machine XML is VirtualBox 4.0 or higher:
10744 uuid = i_getId(); // machine UUID
10745 else
10746 uuid = mParent->i_getGlobalRegistryId(); // VirtualBox global registry UUID
10747
10748 if (pMedium->i_addRegistry(uuid))
10749 mParent->i_markRegistryModified(uuid);
10750
10751 /* For more complex hard disk structures it can happen that the base
10752 * medium isn't yet associated with any medium registry. Do that now. */
10753 if (pMedium != pBase)
10754 {
10755 /* Tree lock needed by Medium::addRegistry when recursing. */
10756 AutoReadLock treeLock(&mParent->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
10757 if (pBase->i_addRegistryRecursive(uuid))
10758 {
10759 treeLock.release();
10760 mParent->i_markRegistryModified(uuid);
10761 }
10762 }
10763}
10764
10765/**
10766 * Creates differencing hard disks for all normal hard disks attached to this
10767 * machine and a new set of attachments to refer to created disks.
10768 *
10769 * Used when taking a snapshot or when deleting the current state. Gets called
10770 * from SessionMachine::BeginTakingSnapshot() and SessionMachine::restoreSnapshotHandler().
10771 *
10772 * This method assumes that mMediumAttachments contains the original hard disk
10773 * attachments it needs to create diffs for. On success, these attachments will
10774 * be replaced with the created diffs.
10775 *
10776 * Attachments with non-normal hard disks are left as is.
10777 *
10778 * If @a aOnline is @c false then the original hard disks that require implicit
10779 * diffs will be locked for reading. Otherwise it is assumed that they are
10780 * already locked for writing (when the VM was started). Note that in the latter
10781 * case it is responsibility of the caller to lock the newly created diffs for
10782 * writing if this method succeeds.
10783 *
10784 * @param aProgress Progress object to run (must contain at least as
10785 * many operations left as the number of hard disks
10786 * attached).
10787 * @param aWeight Weight of this operation.
10788 * @param aOnline Whether the VM was online prior to this operation.
10789 *
10790 * @note The progress object is not marked as completed, neither on success nor
10791 * on failure. This is a responsibility of the caller.
10792 *
10793 * @note Locks this object and the media tree for writing.
10794 */
10795HRESULT Machine::i_createImplicitDiffs(IProgress *aProgress,
10796 ULONG aWeight,
10797 bool aOnline)
10798{
10799 LogFlowThisFunc(("aOnline=%d\n", aOnline));
10800
10801 AutoCaller autoCaller(this);
10802 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
10803
10804 AutoMultiWriteLock2 alock(this->lockHandle(),
10805 &mParent->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
10806
10807 /* must be in a protective state because we release the lock below */
10808 AssertReturn( mData->mMachineState == MachineState_Snapshotting
10809 || mData->mMachineState == MachineState_OnlineSnapshotting
10810 || mData->mMachineState == MachineState_LiveSnapshotting
10811 || mData->mMachineState == MachineState_RestoringSnapshot
10812 || mData->mMachineState == MachineState_DeletingSnapshot
10813 , E_FAIL);
10814
10815 HRESULT rc = S_OK;
10816
10817 // use appropriate locked media map (online or offline)
10818 MediumLockListMap lockedMediaOffline;
10819 MediumLockListMap *lockedMediaMap;
10820 if (aOnline)
10821 lockedMediaMap = &mData->mSession.mLockedMedia;
10822 else
10823 lockedMediaMap = &lockedMediaOffline;
10824
10825 try
10826 {
10827 if (!aOnline)
10828 {
10829 /* lock all attached hard disks early to detect "in use"
10830 * situations before creating actual diffs */
10831 for (MediumAttachmentList::const_iterator
10832 it = mMediumAttachments->begin();
10833 it != mMediumAttachments->end();
10834 ++it)
10835 {
10836 MediumAttachment *pAtt = *it;
10837 if (pAtt->i_getType() == DeviceType_HardDisk)
10838 {
10839 Medium *pMedium = pAtt->i_getMedium();
10840 Assert(pMedium);
10841
10842 MediumLockList *pMediumLockList(new MediumLockList());
10843 alock.release();
10844 rc = pMedium->i_createMediumLockList(true /* fFailIfInaccessible */,
10845 NULL /* pToLockWrite */,
10846 false /* fMediumLockWriteAll */,
10847 NULL,
10848 *pMediumLockList);
10849 alock.acquire();
10850 if (FAILED(rc))
10851 {
10852 delete pMediumLockList;
10853 throw rc;
10854 }
10855 rc = lockedMediaMap->Insert(pAtt, pMediumLockList);
10856 if (FAILED(rc))
10857 {
10858 throw setError(rc,
10859 tr("Collecting locking information for all attached media failed"));
10860 }
10861 }
10862 }
10863
10864 /* Now lock all media. If this fails, nothing is locked. */
10865 alock.release();
10866 rc = lockedMediaMap->Lock();
10867 alock.acquire();
10868 if (FAILED(rc))
10869 {
10870 throw setError(rc,
10871 tr("Locking of attached media failed"));
10872 }
10873 }
10874
10875 /* remember the current list (note that we don't use backup() since
10876 * mMediumAttachments may be already backed up) */
10877 MediumAttachmentList atts = *mMediumAttachments.data();
10878
10879 /* start from scratch */
10880 mMediumAttachments->clear();
10881
10882 /* go through remembered attachments and create diffs for normal hard
10883 * disks and attach them */
10884 for (MediumAttachmentList::const_iterator
10885 it = atts.begin();
10886 it != atts.end();
10887 ++it)
10888 {
10889 MediumAttachment *pAtt = *it;
10890
10891 DeviceType_T devType = pAtt->i_getType();
10892 Medium *pMedium = pAtt->i_getMedium();
10893
10894 if ( devType != DeviceType_HardDisk
10895 || pMedium == NULL
10896 || pMedium->i_getType() != MediumType_Normal)
10897 {
10898 /* copy the attachment as is */
10899
10900 /** @todo the progress object created in SessionMachine::TakeSnaphot
10901 * only expects operations for hard disks. Later other
10902 * device types need to show up in the progress as well. */
10903 if (devType == DeviceType_HardDisk)
10904 {
10905 if (pMedium == NULL)
10906 aProgress->SetNextOperation(Bstr(tr("Skipping attachment without medium")).raw(),
10907 aWeight); // weight
10908 else
10909 aProgress->SetNextOperation(BstrFmt(tr("Skipping medium '%s'"),
10910 pMedium->i_getBase()->i_getName().c_str()).raw(),
10911 aWeight); // weight
10912 }
10913
10914 mMediumAttachments->push_back(pAtt);
10915 continue;
10916 }
10917
10918 /* need a diff */
10919 aProgress->SetNextOperation(BstrFmt(tr("Creating differencing hard disk for '%s'"),
10920 pMedium->i_getBase()->i_getName().c_str()).raw(),
10921 aWeight); // weight
10922
10923 Utf8Str strFullSnapshotFolder;
10924 i_calculateFullPath(mUserData->s.strSnapshotFolder, strFullSnapshotFolder);
10925
10926 ComObjPtr<Medium> diff;
10927 diff.createObject();
10928 // store the diff in the same registry as the parent
10929 // (this cannot fail here because we can't create implicit diffs for
10930 // unregistered images)
10931 Guid uuidRegistryParent;
10932 bool fInRegistry = pMedium->i_getFirstRegistryMachineId(uuidRegistryParent);
10933 Assert(fInRegistry); NOREF(fInRegistry);
10934 rc = diff->init(mParent,
10935 pMedium->i_getPreferredDiffFormat(),
10936 strFullSnapshotFolder.append(RTPATH_SLASH_STR),
10937 uuidRegistryParent,
10938 DeviceType_HardDisk);
10939 if (FAILED(rc)) throw rc;
10940
10941 /** @todo r=bird: How is the locking and diff image cleaned up if we fail before
10942 * the push_back? Looks like we're going to release medium with the
10943 * wrong kind of lock (general issue with if we fail anywhere at all)
10944 * and an orphaned VDI in the snapshots folder. */
10945
10946 /* update the appropriate lock list */
10947 MediumLockList *pMediumLockList;
10948 rc = lockedMediaMap->Get(pAtt, pMediumLockList);
10949 AssertComRCThrowRC(rc);
10950 if (aOnline)
10951 {
10952 alock.release();
10953 /* The currently attached medium will be read-only, change
10954 * the lock type to read. */
10955 rc = pMediumLockList->Update(pMedium, false);
10956 alock.acquire();
10957 AssertComRCThrowRC(rc);
10958 }
10959
10960 /* release the locks before the potentially lengthy operation */
10961 alock.release();
10962 rc = pMedium->i_createDiffStorage(diff,
10963 pMedium->i_getPreferredDiffVariant(),
10964 pMediumLockList,
10965 NULL /* aProgress */,
10966 true /* aWait */);
10967 alock.acquire();
10968 if (FAILED(rc)) throw rc;
10969
10970 /* actual lock list update is done in Machine::i_commitMedia */
10971
10972 rc = diff->i_addBackReference(mData->mUuid);
10973 AssertComRCThrowRC(rc);
10974
10975 /* add a new attachment */
10976 ComObjPtr<MediumAttachment> attachment;
10977 attachment.createObject();
10978 rc = attachment->init(this,
10979 diff,
10980 pAtt->i_getControllerName(),
10981 pAtt->i_getPort(),
10982 pAtt->i_getDevice(),
10983 DeviceType_HardDisk,
10984 true /* aImplicit */,
10985 false /* aPassthrough */,
10986 false /* aTempEject */,
10987 pAtt->i_getNonRotational(),
10988 pAtt->i_getDiscard(),
10989 pAtt->i_getHotPluggable(),
10990 pAtt->i_getBandwidthGroup());
10991 if (FAILED(rc)) throw rc;
10992
10993 rc = lockedMediaMap->ReplaceKey(pAtt, attachment);
10994 AssertComRCThrowRC(rc);
10995 mMediumAttachments->push_back(attachment);
10996 }
10997 }
10998 catch (HRESULT aRC) { rc = aRC; }
10999
11000 /* unlock all hard disks we locked when there is no VM */
11001 if (!aOnline)
11002 {
11003 ErrorInfoKeeper eik;
11004
11005 HRESULT rc1 = lockedMediaMap->Clear();
11006 AssertComRC(rc1);
11007 }
11008
11009 return rc;
11010}
11011
11012/**
11013 * Deletes implicit differencing hard disks created either by
11014 * #i_createImplicitDiffs() or by #attachDevice() and rolls back
11015 * mMediumAttachments.
11016 *
11017 * Note that to delete hard disks created by #attachDevice() this method is
11018 * called from #i_rollbackMedia() when the changes are rolled back.
11019 *
11020 * @note Locks this object and the media tree for writing.
11021 */
11022HRESULT Machine::i_deleteImplicitDiffs(bool aOnline)
11023{
11024 LogFlowThisFunc(("aOnline=%d\n", aOnline));
11025
11026 AutoCaller autoCaller(this);
11027 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
11028
11029 AutoMultiWriteLock2 alock(this->lockHandle(),
11030 &mParent->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
11031
11032 /* We absolutely must have backed up state. */
11033 AssertReturn(mMediumAttachments.isBackedUp(), E_FAIL);
11034
11035 /* Check if there are any implicitly created diff images. */
11036 bool fImplicitDiffs = false;
11037 for (MediumAttachmentList::const_iterator
11038 it = mMediumAttachments->begin();
11039 it != mMediumAttachments->end();
11040 ++it)
11041 {
11042 const ComObjPtr<MediumAttachment> &pAtt = *it;
11043 if (pAtt->i_isImplicit())
11044 {
11045 fImplicitDiffs = true;
11046 break;
11047 }
11048 }
11049 /* If there is nothing to do, leave early. This saves lots of image locking
11050 * effort. It also avoids a MachineStateChanged event without real reason.
11051 * This is important e.g. when loading a VM config, because there should be
11052 * no events. Otherwise API clients can become thoroughly confused for
11053 * inaccessible VMs (the code for loading VM configs uses this method for
11054 * cleanup if the config makes no sense), as they take such events as an
11055 * indication that the VM is alive, and they would force the VM config to
11056 * be reread, leading to an endless loop. */
11057 if (!fImplicitDiffs)
11058 return S_OK;
11059
11060 HRESULT rc = S_OK;
11061 MachineState_T oldState = mData->mMachineState;
11062
11063 /* will release the lock before the potentially lengthy operation,
11064 * so protect with the special state (unless already protected) */
11065 if ( oldState != MachineState_Snapshotting
11066 && oldState != MachineState_OnlineSnapshotting
11067 && oldState != MachineState_LiveSnapshotting
11068 && oldState != MachineState_RestoringSnapshot
11069 && oldState != MachineState_DeletingSnapshot
11070 && oldState != MachineState_DeletingSnapshotOnline
11071 && oldState != MachineState_DeletingSnapshotPaused
11072 )
11073 i_setMachineState(MachineState_SettingUp);
11074
11075 // use appropriate locked media map (online or offline)
11076 MediumLockListMap lockedMediaOffline;
11077 MediumLockListMap *lockedMediaMap;
11078 if (aOnline)
11079 lockedMediaMap = &mData->mSession.mLockedMedia;
11080 else
11081 lockedMediaMap = &lockedMediaOffline;
11082
11083 try
11084 {
11085 if (!aOnline)
11086 {
11087 /* lock all attached hard disks early to detect "in use"
11088 * situations before deleting actual diffs */
11089 for (MediumAttachmentList::const_iterator
11090 it = mMediumAttachments->begin();
11091 it != mMediumAttachments->end();
11092 ++it)
11093 {
11094 MediumAttachment *pAtt = *it;
11095 if (pAtt->i_getType() == DeviceType_HardDisk)
11096 {
11097 Medium *pMedium = pAtt->i_getMedium();
11098 Assert(pMedium);
11099
11100 MediumLockList *pMediumLockList(new MediumLockList());
11101 alock.release();
11102 rc = pMedium->i_createMediumLockList(true /* fFailIfInaccessible */,
11103 NULL /* pToLockWrite */,
11104 false /* fMediumLockWriteAll */,
11105 NULL,
11106 *pMediumLockList);
11107 alock.acquire();
11108
11109 if (FAILED(rc))
11110 {
11111 delete pMediumLockList;
11112 throw rc;
11113 }
11114
11115 rc = lockedMediaMap->Insert(pAtt, pMediumLockList);
11116 if (FAILED(rc))
11117 throw rc;
11118 }
11119 }
11120
11121 if (FAILED(rc))
11122 throw rc;
11123 } // end of offline
11124
11125 /* Lock lists are now up to date and include implicitly created media */
11126
11127 /* Go through remembered attachments and delete all implicitly created
11128 * diffs and fix up the attachment information */
11129 const MediumAttachmentList &oldAtts = *mMediumAttachments.backedUpData();
11130 MediumAttachmentList implicitAtts;
11131 for (MediumAttachmentList::const_iterator
11132 it = mMediumAttachments->begin();
11133 it != mMediumAttachments->end();
11134 ++it)
11135 {
11136 ComObjPtr<MediumAttachment> pAtt = *it;
11137 ComObjPtr<Medium> pMedium = pAtt->i_getMedium();
11138 if (pMedium.isNull())
11139 continue;
11140
11141 // Implicit attachments go on the list for deletion and back references are removed.
11142 if (pAtt->i_isImplicit())
11143 {
11144 /* Deassociate and mark for deletion */
11145 LogFlowThisFunc(("Detaching '%s', pending deletion\n", pAtt->i_getLogName()));
11146 rc = pMedium->i_removeBackReference(mData->mUuid);
11147 if (FAILED(rc))
11148 throw rc;
11149 implicitAtts.push_back(pAtt);
11150 continue;
11151 }
11152
11153 /* Was this medium attached before? */
11154 if (!i_findAttachment(oldAtts, pMedium))
11155 {
11156 /* no: de-associate */
11157 LogFlowThisFunc(("Detaching '%s', no deletion\n", pAtt->i_getLogName()));
11158 rc = pMedium->i_removeBackReference(mData->mUuid);
11159 if (FAILED(rc))
11160 throw rc;
11161 continue;
11162 }
11163 LogFlowThisFunc(("Not detaching '%s'\n", pAtt->i_getLogName()));
11164 }
11165
11166 /* If there are implicit attachments to delete, throw away the lock
11167 * map contents (which will unlock all media) since the medium
11168 * attachments will be rolled back. Below we need to completely
11169 * recreate the lock map anyway since it is infinitely complex to
11170 * do this incrementally (would need reconstructing each attachment
11171 * change, which would be extremely hairy). */
11172 if (implicitAtts.size() != 0)
11173 {
11174 ErrorInfoKeeper eik;
11175
11176 HRESULT rc1 = lockedMediaMap->Clear();
11177 AssertComRC(rc1);
11178 }
11179
11180 /* rollback hard disk changes */
11181 mMediumAttachments.rollback();
11182
11183 MultiResult mrc(S_OK);
11184
11185 // Delete unused implicit diffs.
11186 if (implicitAtts.size() != 0)
11187 {
11188 alock.release();
11189
11190 for (MediumAttachmentList::const_iterator
11191 it = implicitAtts.begin();
11192 it != implicitAtts.end();
11193 ++it)
11194 {
11195 // Remove medium associated with this attachment.
11196 ComObjPtr<MediumAttachment> pAtt = *it;
11197 Assert(pAtt);
11198 LogFlowThisFunc(("Deleting '%s'\n", pAtt->i_getLogName()));
11199 ComObjPtr<Medium> pMedium = pAtt->i_getMedium();
11200 Assert(pMedium);
11201
11202 rc = pMedium->i_deleteStorage(NULL /*aProgress*/, true /*aWait*/);
11203 // continue on delete failure, just collect error messages
11204 AssertMsg(SUCCEEDED(rc), ("rc=%Rhrc it=%s hd=%s\n", rc, pAtt->i_getLogName(),
11205 pMedium->i_getLocationFull().c_str() ));
11206 mrc = rc;
11207 }
11208 // Clear the list of deleted implicit attachments now, while not
11209 // holding the lock, as it will ultimately trigger Medium::uninit()
11210 // calls which assume that the media tree lock isn't held.
11211 implicitAtts.clear();
11212
11213 alock.acquire();
11214
11215 /* if there is a VM recreate media lock map as mentioned above,
11216 * otherwise it is a waste of time and we leave things unlocked */
11217 if (aOnline)
11218 {
11219 const ComObjPtr<SessionMachine> pMachine = mData->mSession.mMachine;
11220 /* must never be NULL, but better safe than sorry */
11221 if (!pMachine.isNull())
11222 {
11223 alock.release();
11224 rc = mData->mSession.mMachine->i_lockMedia();
11225 alock.acquire();
11226 if (FAILED(rc))
11227 throw rc;
11228 }
11229 }
11230 }
11231 }
11232 catch (HRESULT aRC) {rc = aRC;}
11233
11234 if (mData->mMachineState == MachineState_SettingUp)
11235 i_setMachineState(oldState);
11236
11237 /* unlock all hard disks we locked when there is no VM */
11238 if (!aOnline)
11239 {
11240 ErrorInfoKeeper eik;
11241
11242 HRESULT rc1 = lockedMediaMap->Clear();
11243 AssertComRC(rc1);
11244 }
11245
11246 return rc;
11247}
11248
11249
11250/**
11251 * Looks through the given list of media attachments for one with the given parameters
11252 * and returns it, or NULL if not found. The list is a parameter so that backup lists
11253 * can be searched as well if needed.
11254 *
11255 * @param ll
11256 * @param aControllerName
11257 * @param aControllerPort
11258 * @param aDevice
11259 * @return
11260 */
11261MediumAttachment *Machine::i_findAttachment(const MediumAttachmentList &ll,
11262 const Utf8Str &aControllerName,
11263 LONG aControllerPort,
11264 LONG aDevice)
11265{
11266 for (MediumAttachmentList::const_iterator
11267 it = ll.begin();
11268 it != ll.end();
11269 ++it)
11270 {
11271 MediumAttachment *pAttach = *it;
11272 if (pAttach->i_matches(aControllerName, aControllerPort, aDevice))
11273 return pAttach;
11274 }
11275
11276 return NULL;
11277}
11278
11279/**
11280 * Looks through the given list of media attachments for one with the given parameters
11281 * and returns it, or NULL if not found. The list is a parameter so that backup lists
11282 * can be searched as well if needed.
11283 *
11284 * @param ll
11285 * @param pMedium
11286 * @return
11287 */
11288MediumAttachment *Machine::i_findAttachment(const MediumAttachmentList &ll,
11289 ComObjPtr<Medium> pMedium)
11290{
11291 for (MediumAttachmentList::const_iterator
11292 it = ll.begin();
11293 it != ll.end();
11294 ++it)
11295 {
11296 MediumAttachment *pAttach = *it;
11297 ComObjPtr<Medium> pMediumThis = pAttach->i_getMedium();
11298 if (pMediumThis == pMedium)
11299 return pAttach;
11300 }
11301
11302 return NULL;
11303}
11304
11305/**
11306 * Looks through the given list of media attachments for one with the given parameters
11307 * and returns it, or NULL if not found. The list is a parameter so that backup lists
11308 * can be searched as well if needed.
11309 *
11310 * @param ll
11311 * @param id
11312 * @return
11313 */
11314MediumAttachment *Machine::i_findAttachment(const MediumAttachmentList &ll,
11315 Guid &id)
11316{
11317 for (MediumAttachmentList::const_iterator
11318 it = ll.begin();
11319 it != ll.end();
11320 ++it)
11321 {
11322 MediumAttachment *pAttach = *it;
11323 ComObjPtr<Medium> pMediumThis = pAttach->i_getMedium();
11324 if (pMediumThis->i_getId() == id)
11325 return pAttach;
11326 }
11327
11328 return NULL;
11329}
11330
11331/**
11332 * Main implementation for Machine::DetachDevice. This also gets called
11333 * from Machine::prepareUnregister() so it has been taken out for simplicity.
11334 *
11335 * @param pAttach Medium attachment to detach.
11336 * @param writeLock Machine write lock which the caller must have locked once.
11337 * This may be released temporarily in here.
11338 * @param pSnapshot If NULL, then the detachment is for the current machine.
11339 * Otherwise this is for a SnapshotMachine, and this must be
11340 * its snapshot.
11341 * @return
11342 */
11343HRESULT Machine::i_detachDevice(MediumAttachment *pAttach,
11344 AutoWriteLock &writeLock,
11345 Snapshot *pSnapshot)
11346{
11347 ComObjPtr<Medium> oldmedium = pAttach->i_getMedium();
11348 DeviceType_T mediumType = pAttach->i_getType();
11349
11350 LogFlowThisFunc(("Entering, medium of attachment is %s\n", oldmedium ? oldmedium->i_getLocationFull().c_str() : "NULL"));
11351
11352 if (pAttach->i_isImplicit())
11353 {
11354 /* attempt to implicitly delete the implicitly created diff */
11355
11356 /// @todo move the implicit flag from MediumAttachment to Medium
11357 /// and forbid any hard disk operation when it is implicit. Or maybe
11358 /// a special media state for it to make it even more simple.
11359
11360 Assert(mMediumAttachments.isBackedUp());
11361
11362 /* will release the lock before the potentially lengthy operation, so
11363 * protect with the special state */
11364 MachineState_T oldState = mData->mMachineState;
11365 i_setMachineState(MachineState_SettingUp);
11366
11367 writeLock.release();
11368
11369 HRESULT rc = oldmedium->i_deleteStorage(NULL /*aProgress*/,
11370 true /*aWait*/);
11371
11372 writeLock.acquire();
11373
11374 i_setMachineState(oldState);
11375
11376 if (FAILED(rc)) return rc;
11377 }
11378
11379 i_setModified(IsModified_Storage);
11380 mMediumAttachments.backup();
11381 mMediumAttachments->remove(pAttach);
11382
11383 if (!oldmedium.isNull())
11384 {
11385 // if this is from a snapshot, do not defer detachment to i_commitMedia()
11386 if (pSnapshot)
11387 oldmedium->i_removeBackReference(mData->mUuid, pSnapshot->i_getId());
11388 // else if non-hard disk media, do not defer detachment to i_commitMedia() either
11389 else if (mediumType != DeviceType_HardDisk)
11390 oldmedium->i_removeBackReference(mData->mUuid);
11391 }
11392
11393 return S_OK;
11394}
11395
11396/**
11397 * Goes thru all media of the given list and
11398 *
11399 * 1) calls i_detachDevice() on each of them for this machine and
11400 * 2) adds all Medium objects found in the process to the given list,
11401 * depending on cleanupMode.
11402 *
11403 * If cleanupMode is CleanupMode_DetachAllReturnHardDisksOnly, this only
11404 * adds hard disks to the list. If it is CleanupMode_Full, this adds all
11405 * media to the list.
11406 *
11407 * This gets called from Machine::Unregister, both for the actual Machine and
11408 * the SnapshotMachine objects that might be found in the snapshots.
11409 *
11410 * Requires caller and locking. The machine lock must be passed in because it
11411 * will be passed on to i_detachDevice which needs it for temporary unlocking.
11412 *
11413 * @param writeLock Machine lock from top-level caller; this gets passed to
11414 * i_detachDevice.
11415 * @param pSnapshot Must be NULL when called for a "real" Machine or a snapshot
11416 * object if called for a SnapshotMachine.
11417 * @param cleanupMode If DetachAllReturnHardDisksOnly, only hard disk media get
11418 * added to llMedia; if Full, then all media get added;
11419 * otherwise no media get added.
11420 * @param llMedia Caller's list to receive Medium objects which got detached so
11421 * caller can close() them, depending on cleanupMode.
11422 * @return
11423 */
11424HRESULT Machine::i_detachAllMedia(AutoWriteLock &writeLock,
11425 Snapshot *pSnapshot,
11426 CleanupMode_T cleanupMode,
11427 MediaList &llMedia)
11428{
11429 Assert(isWriteLockOnCurrentThread());
11430
11431 HRESULT rc;
11432
11433 // make a temporary list because i_detachDevice invalidates iterators into
11434 // mMediumAttachments
11435 MediumAttachmentList llAttachments2 = *mMediumAttachments.data();
11436
11437 for (MediumAttachmentList::iterator
11438 it = llAttachments2.begin();
11439 it != llAttachments2.end();
11440 ++it)
11441 {
11442 ComObjPtr<MediumAttachment> &pAttach = *it;
11443 ComObjPtr<Medium> pMedium = pAttach->i_getMedium();
11444
11445 if (!pMedium.isNull())
11446 {
11447 AutoCaller mac(pMedium);
11448 if (FAILED(mac.rc())) return mac.rc();
11449 AutoReadLock lock(pMedium COMMA_LOCKVAL_SRC_POS);
11450 DeviceType_T devType = pMedium->i_getDeviceType();
11451 if ( ( cleanupMode == CleanupMode_DetachAllReturnHardDisksOnly
11452 && devType == DeviceType_HardDisk)
11453 || (cleanupMode == CleanupMode_Full)
11454 )
11455 {
11456 llMedia.push_back(pMedium);
11457 ComObjPtr<Medium> pParent = pMedium->i_getParent();
11458 /* Not allowed to keep this lock as below we need the parent
11459 * medium lock, and the lock order is parent to child. */
11460 lock.release();
11461 /*
11462 * Search for medias which are not attached to any machine, but
11463 * in the chain to an attached disk. Mediums are only consided
11464 * if they are:
11465 * - have only one child
11466 * - no references to any machines
11467 * - are of normal medium type
11468 */
11469 while (!pParent.isNull())
11470 {
11471 AutoCaller mac1(pParent);
11472 if (FAILED(mac1.rc())) return mac1.rc();
11473 AutoReadLock lock1(pParent COMMA_LOCKVAL_SRC_POS);
11474 if (pParent->i_getChildren().size() == 1)
11475 {
11476 if ( pParent->i_getMachineBackRefCount() == 0
11477 && pParent->i_getType() == MediumType_Normal
11478 && find(llMedia.begin(), llMedia.end(), pParent) == llMedia.end())
11479 llMedia.push_back(pParent);
11480 }
11481 else
11482 break;
11483 pParent = pParent->i_getParent();
11484 }
11485 }
11486 }
11487
11488 // real machine: then we need to use the proper method
11489 rc = i_detachDevice(pAttach, writeLock, pSnapshot);
11490
11491 if (FAILED(rc))
11492 return rc;
11493 }
11494
11495 return S_OK;
11496}
11497
11498/**
11499 * Perform deferred hard disk detachments.
11500 *
11501 * Does nothing if the hard disk attachment data (mMediumAttachments) is not
11502 * changed (not backed up).
11503 *
11504 * If @a aOnline is @c true then this method will also unlock the old hard
11505 * disks for which the new implicit diffs were created and will lock these new
11506 * diffs for writing.
11507 *
11508 * @param aOnline Whether the VM was online prior to this operation.
11509 *
11510 * @note Locks this object for writing!
11511 */
11512void Machine::i_commitMedia(bool aOnline /*= false*/)
11513{
11514 AutoCaller autoCaller(this);
11515 AssertComRCReturnVoid(autoCaller.rc());
11516
11517 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
11518
11519 LogFlowThisFunc(("Entering, aOnline=%d\n", aOnline));
11520
11521 HRESULT rc = S_OK;
11522
11523 /* no attach/detach operations -- nothing to do */
11524 if (!mMediumAttachments.isBackedUp())
11525 return;
11526
11527 MediumAttachmentList &oldAtts = *mMediumAttachments.backedUpData();
11528 bool fMediaNeedsLocking = false;
11529
11530 /* enumerate new attachments */
11531 for (MediumAttachmentList::const_iterator
11532 it = mMediumAttachments->begin();
11533 it != mMediumAttachments->end();
11534 ++it)
11535 {
11536 MediumAttachment *pAttach = *it;
11537
11538 pAttach->i_commit();
11539
11540 Medium *pMedium = pAttach->i_getMedium();
11541 bool fImplicit = pAttach->i_isImplicit();
11542
11543 LogFlowThisFunc(("Examining current medium '%s' (implicit: %d)\n",
11544 (pMedium) ? pMedium->i_getName().c_str() : "NULL",
11545 fImplicit));
11546
11547 /** @todo convert all this Machine-based voodoo to MediumAttachment
11548 * based commit logic. */
11549 if (fImplicit)
11550 {
11551 /* convert implicit attachment to normal */
11552 pAttach->i_setImplicit(false);
11553
11554 if ( aOnline
11555 && pMedium
11556 && pAttach->i_getType() == DeviceType_HardDisk
11557 )
11558 {
11559 /* update the appropriate lock list */
11560 MediumLockList *pMediumLockList;
11561 rc = mData->mSession.mLockedMedia.Get(pAttach, pMediumLockList);
11562 AssertComRC(rc);
11563 if (pMediumLockList)
11564 {
11565 /* unlock if there's a need to change the locking */
11566 if (!fMediaNeedsLocking)
11567 {
11568 rc = mData->mSession.mLockedMedia.Unlock();
11569 AssertComRC(rc);
11570 fMediaNeedsLocking = true;
11571 }
11572 rc = pMediumLockList->Update(pMedium->i_getParent(), false);
11573 AssertComRC(rc);
11574 rc = pMediumLockList->Append(pMedium, true);
11575 AssertComRC(rc);
11576 }
11577 }
11578
11579 continue;
11580 }
11581
11582 if (pMedium)
11583 {
11584 /* was this medium attached before? */
11585 for (MediumAttachmentList::iterator
11586 oldIt = oldAtts.begin();
11587 oldIt != oldAtts.end();
11588 ++oldIt)
11589 {
11590 MediumAttachment *pOldAttach = *oldIt;
11591 if (pOldAttach->i_getMedium() == pMedium)
11592 {
11593 LogFlowThisFunc(("--> medium '%s' was attached before, will not remove\n", pMedium->i_getName().c_str()));
11594
11595 /* yes: remove from old to avoid de-association */
11596 oldAtts.erase(oldIt);
11597 break;
11598 }
11599 }
11600 }
11601 }
11602
11603 /* enumerate remaining old attachments and de-associate from the
11604 * current machine state */
11605 for (MediumAttachmentList::const_iterator
11606 it = oldAtts.begin();
11607 it != oldAtts.end();
11608 ++it)
11609 {
11610 MediumAttachment *pAttach = *it;
11611 Medium *pMedium = pAttach->i_getMedium();
11612
11613 /* Detach only hard disks, since DVD/floppy media is detached
11614 * instantly in MountMedium. */
11615 if (pAttach->i_getType() == DeviceType_HardDisk && pMedium)
11616 {
11617 LogFlowThisFunc(("detaching medium '%s' from machine\n", pMedium->i_getName().c_str()));
11618
11619 /* now de-associate from the current machine state */
11620 rc = pMedium->i_removeBackReference(mData->mUuid);
11621 AssertComRC(rc);
11622
11623 if (aOnline)
11624 {
11625 /* unlock since medium is not used anymore */
11626 MediumLockList *pMediumLockList;
11627 rc = mData->mSession.mLockedMedia.Get(pAttach, pMediumLockList);
11628 if (RT_UNLIKELY(rc == VBOX_E_INVALID_OBJECT_STATE))
11629 {
11630 /* this happens for online snapshots, there the attachment
11631 * is changing, but only to a diff image created under
11632 * the old one, so there is no separate lock list */
11633 Assert(!pMediumLockList);
11634 }
11635 else
11636 {
11637 AssertComRC(rc);
11638 if (pMediumLockList)
11639 {
11640 rc = mData->mSession.mLockedMedia.Remove(pAttach);
11641 AssertComRC(rc);
11642 }
11643 }
11644 }
11645 }
11646 }
11647
11648 /* take media locks again so that the locking state is consistent */
11649 if (fMediaNeedsLocking)
11650 {
11651 Assert(aOnline);
11652 rc = mData->mSession.mLockedMedia.Lock();
11653 AssertComRC(rc);
11654 }
11655
11656 /* commit the hard disk changes */
11657 mMediumAttachments.commit();
11658
11659 if (i_isSessionMachine())
11660 {
11661 /*
11662 * Update the parent machine to point to the new owner.
11663 * This is necessary because the stored parent will point to the
11664 * session machine otherwise and cause crashes or errors later
11665 * when the session machine gets invalid.
11666 */
11667 /** @todo Change the MediumAttachment class to behave like any other
11668 * class in this regard by creating peer MediumAttachment
11669 * objects for session machines and share the data with the peer
11670 * machine.
11671 */
11672 for (MediumAttachmentList::const_iterator
11673 it = mMediumAttachments->begin();
11674 it != mMediumAttachments->end();
11675 ++it)
11676 (*it)->i_updateParentMachine(mPeer);
11677
11678 /* attach new data to the primary machine and reshare it */
11679 mPeer->mMediumAttachments.attach(mMediumAttachments);
11680 }
11681
11682 return;
11683}
11684
11685/**
11686 * Perform deferred deletion of implicitly created diffs.
11687 *
11688 * Does nothing if the hard disk attachment data (mMediumAttachments) is not
11689 * changed (not backed up).
11690 *
11691 * @note Locks this object for writing!
11692 */
11693void Machine::i_rollbackMedia()
11694{
11695 AutoCaller autoCaller(this);
11696 AssertComRCReturnVoid(autoCaller.rc());
11697
11698 // AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
11699 LogFlowThisFunc(("Entering rollbackMedia\n"));
11700
11701 HRESULT rc = S_OK;
11702
11703 /* no attach/detach operations -- nothing to do */
11704 if (!mMediumAttachments.isBackedUp())
11705 return;
11706
11707 /* enumerate new attachments */
11708 for (MediumAttachmentList::const_iterator
11709 it = mMediumAttachments->begin();
11710 it != mMediumAttachments->end();
11711 ++it)
11712 {
11713 MediumAttachment *pAttach = *it;
11714 /* Fix up the backrefs for DVD/floppy media. */
11715 if (pAttach->i_getType() != DeviceType_HardDisk)
11716 {
11717 Medium *pMedium = pAttach->i_getMedium();
11718 if (pMedium)
11719 {
11720 rc = pMedium->i_removeBackReference(mData->mUuid);
11721 AssertComRC(rc);
11722 }
11723 }
11724
11725 (*it)->i_rollback();
11726
11727 pAttach = *it;
11728 /* Fix up the backrefs for DVD/floppy media. */
11729 if (pAttach->i_getType() != DeviceType_HardDisk)
11730 {
11731 Medium *pMedium = pAttach->i_getMedium();
11732 if (pMedium)
11733 {
11734 rc = pMedium->i_addBackReference(mData->mUuid);
11735 AssertComRC(rc);
11736 }
11737 }
11738 }
11739
11740 /** @todo convert all this Machine-based voodoo to MediumAttachment
11741 * based rollback logic. */
11742 i_deleteImplicitDiffs(Global::IsOnline(mData->mMachineState));
11743
11744 return;
11745}
11746
11747/**
11748 * Returns true if the settings file is located in the directory named exactly
11749 * as the machine; this means, among other things, that the machine directory
11750 * should be auto-renamed.
11751 *
11752 * @param aSettingsDir if not NULL, the full machine settings file directory
11753 * name will be assigned there.
11754 *
11755 * @note Doesn't lock anything.
11756 * @note Not thread safe (must be called from this object's lock).
11757 */
11758bool Machine::i_isInOwnDir(Utf8Str *aSettingsDir /* = NULL */) const
11759{
11760 Utf8Str strMachineDirName(mData->m_strConfigFileFull); // path/to/machinesfolder/vmname/vmname.vbox
11761 strMachineDirName.stripFilename(); // path/to/machinesfolder/vmname
11762 if (aSettingsDir)
11763 *aSettingsDir = strMachineDirName;
11764 strMachineDirName.stripPath(); // vmname
11765 Utf8Str strConfigFileOnly(mData->m_strConfigFileFull); // path/to/machinesfolder/vmname/vmname.vbox
11766 strConfigFileOnly.stripPath() // vmname.vbox
11767 .stripSuffix(); // vmname
11768 /** @todo hack, make somehow use of ComposeMachineFilename */
11769 if (mUserData->s.fDirectoryIncludesUUID)
11770 strConfigFileOnly += Utf8StrFmt(" (%RTuuid)", mData->mUuid.raw());
11771
11772 AssertReturn(!strMachineDirName.isEmpty(), false);
11773 AssertReturn(!strConfigFileOnly.isEmpty(), false);
11774
11775 return strMachineDirName == strConfigFileOnly;
11776}
11777
11778/**
11779 * Discards all changes to machine settings.
11780 *
11781 * @param aNotify Whether to notify the direct session about changes or not.
11782 *
11783 * @note Locks objects for writing!
11784 */
11785void Machine::i_rollback(bool aNotify)
11786{
11787 AutoCaller autoCaller(this);
11788 AssertComRCReturn(autoCaller.rc(), (void)0);
11789
11790 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
11791
11792 if (!mStorageControllers.isNull())
11793 {
11794 if (mStorageControllers.isBackedUp())
11795 {
11796 /* unitialize all new devices (absent in the backed up list). */
11797 StorageControllerList *backedList = mStorageControllers.backedUpData();
11798 for (StorageControllerList::const_iterator
11799 it = mStorageControllers->begin();
11800 it != mStorageControllers->end();
11801 ++it)
11802 {
11803 if ( std::find(backedList->begin(), backedList->end(), *it)
11804 == backedList->end()
11805 )
11806 {
11807 (*it)->uninit();
11808 }
11809 }
11810
11811 /* restore the list */
11812 mStorageControllers.rollback();
11813 }
11814
11815 /* rollback any changes to devices after restoring the list */
11816 if (mData->flModifications & IsModified_Storage)
11817 {
11818 for (StorageControllerList::const_iterator
11819 it = mStorageControllers->begin();
11820 it != mStorageControllers->end();
11821 ++it)
11822 {
11823 (*it)->i_rollback();
11824 }
11825 }
11826 }
11827
11828 if (!mUSBControllers.isNull())
11829 {
11830 if (mUSBControllers.isBackedUp())
11831 {
11832 /* unitialize all new devices (absent in the backed up list). */
11833 USBControllerList *backedList = mUSBControllers.backedUpData();
11834 for (USBControllerList::const_iterator
11835 it = mUSBControllers->begin();
11836 it != mUSBControllers->end();
11837 ++it)
11838 {
11839 if ( std::find(backedList->begin(), backedList->end(), *it)
11840 == backedList->end()
11841 )
11842 {
11843 (*it)->uninit();
11844 }
11845 }
11846
11847 /* restore the list */
11848 mUSBControllers.rollback();
11849 }
11850
11851 /* rollback any changes to devices after restoring the list */
11852 if (mData->flModifications & IsModified_USB)
11853 {
11854 for (USBControllerList::const_iterator
11855 it = mUSBControllers->begin();
11856 it != mUSBControllers->end();
11857 ++it)
11858 {
11859 (*it)->i_rollback();
11860 }
11861 }
11862 }
11863
11864 mUserData.rollback();
11865
11866 mHWData.rollback();
11867
11868 if (mData->flModifications & IsModified_Storage)
11869 i_rollbackMedia();
11870
11871 if (mBIOSSettings)
11872 mBIOSSettings->i_rollback();
11873
11874 if (mVRDEServer && (mData->flModifications & IsModified_VRDEServer))
11875 mVRDEServer->i_rollback();
11876
11877 if (mAudioAdapter)
11878 mAudioAdapter->i_rollback();
11879
11880 if (mUSBDeviceFilters && (mData->flModifications & IsModified_USB))
11881 mUSBDeviceFilters->i_rollback();
11882
11883 if (mBandwidthControl && (mData->flModifications & IsModified_BandwidthControl))
11884 mBandwidthControl->i_rollback();
11885
11886 if (!mHWData.isNull())
11887 mNetworkAdapters.resize(Global::getMaxNetworkAdapters(mHWData->mChipsetType));
11888 NetworkAdapterVector networkAdapters(mNetworkAdapters.size());
11889 ComPtr<ISerialPort> serialPorts[RT_ELEMENTS(mSerialPorts)];
11890 ComPtr<IParallelPort> parallelPorts[RT_ELEMENTS(mParallelPorts)];
11891
11892 if (mData->flModifications & IsModified_NetworkAdapters)
11893 for (ULONG slot = 0; slot < mNetworkAdapters.size(); ++slot)
11894 if ( mNetworkAdapters[slot]
11895 && mNetworkAdapters[slot]->i_isModified())
11896 {
11897 mNetworkAdapters[slot]->i_rollback();
11898 networkAdapters[slot] = mNetworkAdapters[slot];
11899 }
11900
11901 if (mData->flModifications & IsModified_SerialPorts)
11902 for (ULONG slot = 0; slot < RT_ELEMENTS(mSerialPorts); ++slot)
11903 if ( mSerialPorts[slot]
11904 && mSerialPorts[slot]->i_isModified())
11905 {
11906 mSerialPorts[slot]->i_rollback();
11907 serialPorts[slot] = mSerialPorts[slot];
11908 }
11909
11910 if (mData->flModifications & IsModified_ParallelPorts)
11911 for (ULONG slot = 0; slot < RT_ELEMENTS(mParallelPorts); ++slot)
11912 if ( mParallelPorts[slot]
11913 && mParallelPorts[slot]->i_isModified())
11914 {
11915 mParallelPorts[slot]->i_rollback();
11916 parallelPorts[slot] = mParallelPorts[slot];
11917 }
11918
11919 if (aNotify)
11920 {
11921 /* inform the direct session about changes */
11922
11923 ComObjPtr<Machine> that = this;
11924 uint32_t flModifications = mData->flModifications;
11925 alock.release();
11926
11927 if (flModifications & IsModified_SharedFolders)
11928 that->i_onSharedFolderChange();
11929
11930 if (flModifications & IsModified_VRDEServer)
11931 that->i_onVRDEServerChange(/* aRestart */ TRUE);
11932 if (flModifications & IsModified_USB)
11933 that->i_onUSBControllerChange();
11934
11935 for (ULONG slot = 0; slot < networkAdapters.size(); ++slot)
11936 if (networkAdapters[slot])
11937 that->i_onNetworkAdapterChange(networkAdapters[slot], FALSE);
11938 for (ULONG slot = 0; slot < RT_ELEMENTS(serialPorts); ++slot)
11939 if (serialPorts[slot])
11940 that->i_onSerialPortChange(serialPorts[slot]);
11941 for (ULONG slot = 0; slot < RT_ELEMENTS(parallelPorts); ++slot)
11942 if (parallelPorts[slot])
11943 that->i_onParallelPortChange(parallelPorts[slot]);
11944
11945 if (flModifications & IsModified_Storage)
11946 that->i_onStorageControllerChange();
11947
11948#if 0
11949 if (flModifications & IsModified_BandwidthControl)
11950 that->onBandwidthControlChange();
11951#endif
11952 }
11953}
11954
11955/**
11956 * Commits all the changes to machine settings.
11957 *
11958 * Note that this operation is supposed to never fail.
11959 *
11960 * @note Locks this object and children for writing.
11961 */
11962void Machine::i_commit()
11963{
11964 AutoCaller autoCaller(this);
11965 AssertComRCReturnVoid(autoCaller.rc());
11966
11967 AutoCaller peerCaller(mPeer);
11968 AssertComRCReturnVoid(peerCaller.rc());
11969
11970 AutoMultiWriteLock2 alock(mPeer, this COMMA_LOCKVAL_SRC_POS);
11971
11972 /*
11973 * use safe commit to ensure Snapshot machines (that share mUserData)
11974 * will still refer to a valid memory location
11975 */
11976 mUserData.commitCopy();
11977
11978 mHWData.commit();
11979
11980 if (mMediumAttachments.isBackedUp())
11981 i_commitMedia(Global::IsOnline(mData->mMachineState));
11982
11983 mBIOSSettings->i_commit();
11984 mVRDEServer->i_commit();
11985 mAudioAdapter->i_commit();
11986 mUSBDeviceFilters->i_commit();
11987 mBandwidthControl->i_commit();
11988
11989 /* Since mNetworkAdapters is a list which might have been changed (resized)
11990 * without using the Backupable<> template we need to handle the copying
11991 * of the list entries manually, including the creation of peers for the
11992 * new objects. */
11993 bool commitNetworkAdapters = false;
11994 size_t newSize = Global::getMaxNetworkAdapters(mHWData->mChipsetType);
11995 if (mPeer)
11996 {
11997 /* commit everything, even the ones which will go away */
11998 for (size_t slot = 0; slot < mNetworkAdapters.size(); slot++)
11999 mNetworkAdapters[slot]->i_commit();
12000 /* copy over the new entries, creating a peer and uninit the original */
12001 mPeer->mNetworkAdapters.resize(RT_MAX(newSize, mPeer->mNetworkAdapters.size()));
12002 for (size_t slot = 0; slot < newSize; slot++)
12003 {
12004 /* look if this adapter has a peer device */
12005 ComObjPtr<NetworkAdapter> peer = mNetworkAdapters[slot]->i_getPeer();
12006 if (!peer)
12007 {
12008 /* no peer means the adapter is a newly created one;
12009 * create a peer owning data this data share it with */
12010 peer.createObject();
12011 peer->init(mPeer, mNetworkAdapters[slot], true /* aReshare */);
12012 }
12013 mPeer->mNetworkAdapters[slot] = peer;
12014 }
12015 /* uninit any no longer needed network adapters */
12016 for (size_t slot = newSize; slot < mNetworkAdapters.size(); ++slot)
12017 mNetworkAdapters[slot]->uninit();
12018 for (size_t slot = newSize; slot < mPeer->mNetworkAdapters.size(); ++slot)
12019 {
12020 if (mPeer->mNetworkAdapters[slot])
12021 mPeer->mNetworkAdapters[slot]->uninit();
12022 }
12023 /* Keep the original network adapter count until this point, so that
12024 * discarding a chipset type change will not lose settings. */
12025 mNetworkAdapters.resize(newSize);
12026 mPeer->mNetworkAdapters.resize(newSize);
12027 }
12028 else
12029 {
12030 /* we have no peer (our parent is the newly created machine);
12031 * just commit changes to the network adapters */
12032 commitNetworkAdapters = true;
12033 }
12034 if (commitNetworkAdapters)
12035 for (size_t slot = 0; slot < mNetworkAdapters.size(); ++slot)
12036 mNetworkAdapters[slot]->i_commit();
12037
12038 for (ULONG slot = 0; slot < RT_ELEMENTS(mSerialPorts); ++slot)
12039 mSerialPorts[slot]->i_commit();
12040 for (ULONG slot = 0; slot < RT_ELEMENTS(mParallelPorts); ++slot)
12041 mParallelPorts[slot]->i_commit();
12042
12043 bool commitStorageControllers = false;
12044
12045 if (mStorageControllers.isBackedUp())
12046 {
12047 mStorageControllers.commit();
12048
12049 if (mPeer)
12050 {
12051 /* Commit all changes to new controllers (this will reshare data with
12052 * peers for those who have peers) */
12053 StorageControllerList *newList = new StorageControllerList();
12054 for (StorageControllerList::const_iterator
12055 it = mStorageControllers->begin();
12056 it != mStorageControllers->end();
12057 ++it)
12058 {
12059 (*it)->i_commit();
12060
12061 /* look if this controller has a peer device */
12062 ComObjPtr<StorageController> peer = (*it)->i_getPeer();
12063 if (!peer)
12064 {
12065 /* no peer means the device is a newly created one;
12066 * create a peer owning data this device share it with */
12067 peer.createObject();
12068 peer->init(mPeer, *it, true /* aReshare */);
12069 }
12070 else
12071 {
12072 /* remove peer from the old list */
12073 mPeer->mStorageControllers->remove(peer);
12074 }
12075 /* and add it to the new list */
12076 newList->push_back(peer);
12077 }
12078
12079 /* uninit old peer's controllers that are left */
12080 for (StorageControllerList::const_iterator
12081 it = mPeer->mStorageControllers->begin();
12082 it != mPeer->mStorageControllers->end();
12083 ++it)
12084 {
12085 (*it)->uninit();
12086 }
12087
12088 /* attach new list of controllers to our peer */
12089 mPeer->mStorageControllers.attach(newList);
12090 }
12091 else
12092 {
12093 /* we have no peer (our parent is the newly created machine);
12094 * just commit changes to devices */
12095 commitStorageControllers = true;
12096 }
12097 }
12098 else
12099 {
12100 /* the list of controllers itself is not changed,
12101 * just commit changes to controllers themselves */
12102 commitStorageControllers = true;
12103 }
12104
12105 if (commitStorageControllers)
12106 {
12107 for (StorageControllerList::const_iterator
12108 it = mStorageControllers->begin();
12109 it != mStorageControllers->end();
12110 ++it)
12111 {
12112 (*it)->i_commit();
12113 }
12114 }
12115
12116 bool commitUSBControllers = false;
12117
12118 if (mUSBControllers.isBackedUp())
12119 {
12120 mUSBControllers.commit();
12121
12122 if (mPeer)
12123 {
12124 /* Commit all changes to new controllers (this will reshare data with
12125 * peers for those who have peers) */
12126 USBControllerList *newList = new USBControllerList();
12127 for (USBControllerList::const_iterator
12128 it = mUSBControllers->begin();
12129 it != mUSBControllers->end();
12130 ++it)
12131 {
12132 (*it)->i_commit();
12133
12134 /* look if this controller has a peer device */
12135 ComObjPtr<USBController> peer = (*it)->i_getPeer();
12136 if (!peer)
12137 {
12138 /* no peer means the device is a newly created one;
12139 * create a peer owning data this device share it with */
12140 peer.createObject();
12141 peer->init(mPeer, *it, true /* aReshare */);
12142 }
12143 else
12144 {
12145 /* remove peer from the old list */
12146 mPeer->mUSBControllers->remove(peer);
12147 }
12148 /* and add it to the new list */
12149 newList->push_back(peer);
12150 }
12151
12152 /* uninit old peer's controllers that are left */
12153 for (USBControllerList::const_iterator
12154 it = mPeer->mUSBControllers->begin();
12155 it != mPeer->mUSBControllers->end();
12156 ++it)
12157 {
12158 (*it)->uninit();
12159 }
12160
12161 /* attach new list of controllers to our peer */
12162 mPeer->mUSBControllers.attach(newList);
12163 }
12164 else
12165 {
12166 /* we have no peer (our parent is the newly created machine);
12167 * just commit changes to devices */
12168 commitUSBControllers = true;
12169 }
12170 }
12171 else
12172 {
12173 /* the list of controllers itself is not changed,
12174 * just commit changes to controllers themselves */
12175 commitUSBControllers = true;
12176 }
12177
12178 if (commitUSBControllers)
12179 {
12180 for (USBControllerList::const_iterator
12181 it = mUSBControllers->begin();
12182 it != mUSBControllers->end();
12183 ++it)
12184 {
12185 (*it)->i_commit();
12186 }
12187 }
12188
12189 if (i_isSessionMachine())
12190 {
12191 /* attach new data to the primary machine and reshare it */
12192 mPeer->mUserData.attach(mUserData);
12193 mPeer->mHWData.attach(mHWData);
12194 /* mmMediumAttachments is reshared by fixupMedia */
12195 // mPeer->mMediumAttachments.attach(mMediumAttachments);
12196 Assert(mPeer->mMediumAttachments.data() == mMediumAttachments.data());
12197 }
12198}
12199
12200/**
12201 * Copies all the hardware data from the given machine.
12202 *
12203 * Currently, only called when the VM is being restored from a snapshot. In
12204 * particular, this implies that the VM is not running during this method's
12205 * call.
12206 *
12207 * @note This method must be called from under this object's lock.
12208 *
12209 * @note This method doesn't call #i_commit(), so all data remains backed up and
12210 * unsaved.
12211 */
12212void Machine::i_copyFrom(Machine *aThat)
12213{
12214 AssertReturnVoid(!i_isSnapshotMachine());
12215 AssertReturnVoid(aThat->i_isSnapshotMachine());
12216
12217 AssertReturnVoid(!Global::IsOnline(mData->mMachineState));
12218
12219 mHWData.assignCopy(aThat->mHWData);
12220
12221 // create copies of all shared folders (mHWData after attaching a copy
12222 // contains just references to original objects)
12223 for (HWData::SharedFolderList::iterator
12224 it = mHWData->mSharedFolders.begin();
12225 it != mHWData->mSharedFolders.end();
12226 ++it)
12227 {
12228 ComObjPtr<SharedFolder> folder;
12229 folder.createObject();
12230 HRESULT rc = folder->initCopy(i_getMachine(), *it);
12231 AssertComRC(rc);
12232 *it = folder;
12233 }
12234
12235 mBIOSSettings->i_copyFrom(aThat->mBIOSSettings);
12236 mVRDEServer->i_copyFrom(aThat->mVRDEServer);
12237 mAudioAdapter->i_copyFrom(aThat->mAudioAdapter);
12238 mUSBDeviceFilters->i_copyFrom(aThat->mUSBDeviceFilters);
12239 mBandwidthControl->i_copyFrom(aThat->mBandwidthControl);
12240
12241 /* create private copies of all controllers */
12242 mStorageControllers.backup();
12243 mStorageControllers->clear();
12244 for (StorageControllerList::const_iterator
12245 it = aThat->mStorageControllers->begin();
12246 it != aThat->mStorageControllers->end();
12247 ++it)
12248 {
12249 ComObjPtr<StorageController> ctrl;
12250 ctrl.createObject();
12251 ctrl->initCopy(this, *it);
12252 mStorageControllers->push_back(ctrl);
12253 }
12254
12255 /* create private copies of all USB controllers */
12256 mUSBControllers.backup();
12257 mUSBControllers->clear();
12258 for (USBControllerList::const_iterator
12259 it = aThat->mUSBControllers->begin();
12260 it != aThat->mUSBControllers->end();
12261 ++it)
12262 {
12263 ComObjPtr<USBController> ctrl;
12264 ctrl.createObject();
12265 ctrl->initCopy(this, *it);
12266 mUSBControllers->push_back(ctrl);
12267 }
12268
12269 mNetworkAdapters.resize(aThat->mNetworkAdapters.size());
12270 for (ULONG slot = 0; slot < mNetworkAdapters.size(); ++slot)
12271 {
12272 if (mNetworkAdapters[slot].isNotNull())
12273 mNetworkAdapters[slot]->i_copyFrom(aThat->mNetworkAdapters[slot]);
12274 else
12275 {
12276 unconst(mNetworkAdapters[slot]).createObject();
12277 mNetworkAdapters[slot]->initCopy(this, aThat->mNetworkAdapters[slot]);
12278 }
12279 }
12280 for (ULONG slot = 0; slot < RT_ELEMENTS(mSerialPorts); ++slot)
12281 mSerialPorts[slot]->i_copyFrom(aThat->mSerialPorts[slot]);
12282 for (ULONG slot = 0; slot < RT_ELEMENTS(mParallelPorts); ++slot)
12283 mParallelPorts[slot]->i_copyFrom(aThat->mParallelPorts[slot]);
12284}
12285
12286/**
12287 * Returns whether the given storage controller is hotplug capable.
12288 *
12289 * @returns true if the controller supports hotplugging
12290 * false otherwise.
12291 * @param enmCtrlType The controller type to check for.
12292 */
12293bool Machine::i_isControllerHotplugCapable(StorageControllerType_T enmCtrlType)
12294{
12295 ComPtr<ISystemProperties> systemProperties;
12296 HRESULT rc = mParent->COMGETTER(SystemProperties)(systemProperties.asOutParam());
12297 if (FAILED(rc))
12298 return false;
12299
12300 BOOL aHotplugCapable = FALSE;
12301 systemProperties->GetStorageControllerHotplugCapable(enmCtrlType, &aHotplugCapable);
12302
12303 return RT_BOOL(aHotplugCapable);
12304}
12305
12306#ifdef VBOX_WITH_RESOURCE_USAGE_API
12307
12308void Machine::i_getDiskList(MediaList &list)
12309{
12310 for (MediumAttachmentList::const_iterator
12311 it = mMediumAttachments->begin();
12312 it != mMediumAttachments->end();
12313 ++it)
12314 {
12315 MediumAttachment *pAttach = *it;
12316 /* just in case */
12317 AssertContinue(pAttach);
12318
12319 AutoCaller localAutoCallerA(pAttach);
12320 if (FAILED(localAutoCallerA.rc())) continue;
12321
12322 AutoReadLock local_alockA(pAttach COMMA_LOCKVAL_SRC_POS);
12323
12324 if (pAttach->i_getType() == DeviceType_HardDisk)
12325 list.push_back(pAttach->i_getMedium());
12326 }
12327}
12328
12329void Machine::i_registerMetrics(PerformanceCollector *aCollector, Machine *aMachine, RTPROCESS pid)
12330{
12331 AssertReturnVoid(isWriteLockOnCurrentThread());
12332 AssertPtrReturnVoid(aCollector);
12333
12334 pm::CollectorHAL *hal = aCollector->getHAL();
12335 /* Create sub metrics */
12336 pm::SubMetric *cpuLoadUser = new pm::SubMetric("CPU/Load/User",
12337 "Percentage of processor time spent in user mode by the VM process.");
12338 pm::SubMetric *cpuLoadKernel = new pm::SubMetric("CPU/Load/Kernel",
12339 "Percentage of processor time spent in kernel mode by the VM process.");
12340 pm::SubMetric *ramUsageUsed = new pm::SubMetric("RAM/Usage/Used",
12341 "Size of resident portion of VM process in memory.");
12342 pm::SubMetric *diskUsageUsed = new pm::SubMetric("Disk/Usage/Used",
12343 "Actual size of all VM disks combined.");
12344 pm::SubMetric *machineNetRx = new pm::SubMetric("Net/Rate/Rx",
12345 "Network receive rate.");
12346 pm::SubMetric *machineNetTx = new pm::SubMetric("Net/Rate/Tx",
12347 "Network transmit rate.");
12348 /* Create and register base metrics */
12349 pm::BaseMetric *cpuLoad = new pm::MachineCpuLoadRaw(hal, aMachine, pid,
12350 cpuLoadUser, cpuLoadKernel);
12351 aCollector->registerBaseMetric(cpuLoad);
12352 pm::BaseMetric *ramUsage = new pm::MachineRamUsage(hal, aMachine, pid,
12353 ramUsageUsed);
12354 aCollector->registerBaseMetric(ramUsage);
12355 MediaList disks;
12356 i_getDiskList(disks);
12357 pm::BaseMetric *diskUsage = new pm::MachineDiskUsage(hal, aMachine, disks,
12358 diskUsageUsed);
12359 aCollector->registerBaseMetric(diskUsage);
12360
12361 aCollector->registerMetric(new pm::Metric(cpuLoad, cpuLoadUser, 0));
12362 aCollector->registerMetric(new pm::Metric(cpuLoad, cpuLoadUser,
12363 new pm::AggregateAvg()));
12364 aCollector->registerMetric(new pm::Metric(cpuLoad, cpuLoadUser,
12365 new pm::AggregateMin()));
12366 aCollector->registerMetric(new pm::Metric(cpuLoad, cpuLoadUser,
12367 new pm::AggregateMax()));
12368 aCollector->registerMetric(new pm::Metric(cpuLoad, cpuLoadKernel, 0));
12369 aCollector->registerMetric(new pm::Metric(cpuLoad, cpuLoadKernel,
12370 new pm::AggregateAvg()));
12371 aCollector->registerMetric(new pm::Metric(cpuLoad, cpuLoadKernel,
12372 new pm::AggregateMin()));
12373 aCollector->registerMetric(new pm::Metric(cpuLoad, cpuLoadKernel,
12374 new pm::AggregateMax()));
12375
12376 aCollector->registerMetric(new pm::Metric(ramUsage, ramUsageUsed, 0));
12377 aCollector->registerMetric(new pm::Metric(ramUsage, ramUsageUsed,
12378 new pm::AggregateAvg()));
12379 aCollector->registerMetric(new pm::Metric(ramUsage, ramUsageUsed,
12380 new pm::AggregateMin()));
12381 aCollector->registerMetric(new pm::Metric(ramUsage, ramUsageUsed,
12382 new pm::AggregateMax()));
12383
12384 aCollector->registerMetric(new pm::Metric(diskUsage, diskUsageUsed, 0));
12385 aCollector->registerMetric(new pm::Metric(diskUsage, diskUsageUsed,
12386 new pm::AggregateAvg()));
12387 aCollector->registerMetric(new pm::Metric(diskUsage, diskUsageUsed,
12388 new pm::AggregateMin()));
12389 aCollector->registerMetric(new pm::Metric(diskUsage, diskUsageUsed,
12390 new pm::AggregateMax()));
12391
12392
12393 /* Guest metrics collector */
12394 mCollectorGuest = new pm::CollectorGuest(aMachine, pid);
12395 aCollector->registerGuest(mCollectorGuest);
12396 Log7(("{%p} " LOG_FN_FMT ": mCollectorGuest=%p\n", this, __PRETTY_FUNCTION__, mCollectorGuest));
12397
12398 /* Create sub metrics */
12399 pm::SubMetric *guestLoadUser = new pm::SubMetric("Guest/CPU/Load/User",
12400 "Percentage of processor time spent in user mode as seen by the guest.");
12401 pm::SubMetric *guestLoadKernel = new pm::SubMetric("Guest/CPU/Load/Kernel",
12402 "Percentage of processor time spent in kernel mode as seen by the guest.");
12403 pm::SubMetric *guestLoadIdle = new pm::SubMetric("Guest/CPU/Load/Idle",
12404 "Percentage of processor time spent idling as seen by the guest.");
12405
12406 /* The total amount of physical ram is fixed now, but we'll support dynamic guest ram configurations in the future. */
12407 pm::SubMetric *guestMemTotal = new pm::SubMetric("Guest/RAM/Usage/Total", "Total amount of physical guest RAM.");
12408 pm::SubMetric *guestMemFree = new pm::SubMetric("Guest/RAM/Usage/Free", "Free amount of physical guest RAM.");
12409 pm::SubMetric *guestMemBalloon = new pm::SubMetric("Guest/RAM/Usage/Balloon", "Amount of ballooned physical guest RAM.");
12410 pm::SubMetric *guestMemShared = new pm::SubMetric("Guest/RAM/Usage/Shared", "Amount of shared physical guest RAM.");
12411 pm::SubMetric *guestMemCache = new pm::SubMetric(
12412 "Guest/RAM/Usage/Cache", "Total amount of guest (disk) cache memory.");
12413
12414 pm::SubMetric *guestPagedTotal = new pm::SubMetric(
12415 "Guest/Pagefile/Usage/Total", "Total amount of space in the page file.");
12416
12417 /* Create and register base metrics */
12418 pm::BaseMetric *machineNetRate = new pm::MachineNetRate(mCollectorGuest, aMachine,
12419 machineNetRx, machineNetTx);
12420 aCollector->registerBaseMetric(machineNetRate);
12421
12422 pm::BaseMetric *guestCpuLoad = new pm::GuestCpuLoad(mCollectorGuest, aMachine,
12423 guestLoadUser, guestLoadKernel, guestLoadIdle);
12424 aCollector->registerBaseMetric(guestCpuLoad);
12425
12426 pm::BaseMetric *guestCpuMem = new pm::GuestRamUsage(mCollectorGuest, aMachine,
12427 guestMemTotal, guestMemFree,
12428 guestMemBalloon, guestMemShared,
12429 guestMemCache, guestPagedTotal);
12430 aCollector->registerBaseMetric(guestCpuMem);
12431
12432 aCollector->registerMetric(new pm::Metric(machineNetRate, machineNetRx, 0));
12433 aCollector->registerMetric(new pm::Metric(machineNetRate, machineNetRx, new pm::AggregateAvg()));
12434 aCollector->registerMetric(new pm::Metric(machineNetRate, machineNetRx, new pm::AggregateMin()));
12435 aCollector->registerMetric(new pm::Metric(machineNetRate, machineNetRx, new pm::AggregateMax()));
12436
12437 aCollector->registerMetric(new pm::Metric(machineNetRate, machineNetTx, 0));
12438 aCollector->registerMetric(new pm::Metric(machineNetRate, machineNetTx, new pm::AggregateAvg()));
12439 aCollector->registerMetric(new pm::Metric(machineNetRate, machineNetTx, new pm::AggregateMin()));
12440 aCollector->registerMetric(new pm::Metric(machineNetRate, machineNetTx, new pm::AggregateMax()));
12441
12442 aCollector->registerMetric(new pm::Metric(guestCpuLoad, guestLoadUser, 0));
12443 aCollector->registerMetric(new pm::Metric(guestCpuLoad, guestLoadUser, new pm::AggregateAvg()));
12444 aCollector->registerMetric(new pm::Metric(guestCpuLoad, guestLoadUser, new pm::AggregateMin()));
12445 aCollector->registerMetric(new pm::Metric(guestCpuLoad, guestLoadUser, new pm::AggregateMax()));
12446
12447 aCollector->registerMetric(new pm::Metric(guestCpuLoad, guestLoadKernel, 0));
12448 aCollector->registerMetric(new pm::Metric(guestCpuLoad, guestLoadKernel, new pm::AggregateAvg()));
12449 aCollector->registerMetric(new pm::Metric(guestCpuLoad, guestLoadKernel, new pm::AggregateMin()));
12450 aCollector->registerMetric(new pm::Metric(guestCpuLoad, guestLoadKernel, new pm::AggregateMax()));
12451
12452 aCollector->registerMetric(new pm::Metric(guestCpuLoad, guestLoadIdle, 0));
12453 aCollector->registerMetric(new pm::Metric(guestCpuLoad, guestLoadIdle, new pm::AggregateAvg()));
12454 aCollector->registerMetric(new pm::Metric(guestCpuLoad, guestLoadIdle, new pm::AggregateMin()));
12455 aCollector->registerMetric(new pm::Metric(guestCpuLoad, guestLoadIdle, new pm::AggregateMax()));
12456
12457 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemTotal, 0));
12458 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemTotal, new pm::AggregateAvg()));
12459 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemTotal, new pm::AggregateMin()));
12460 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemTotal, new pm::AggregateMax()));
12461
12462 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemFree, 0));
12463 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemFree, new pm::AggregateAvg()));
12464 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemFree, new pm::AggregateMin()));
12465 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemFree, new pm::AggregateMax()));
12466
12467 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemBalloon, 0));
12468 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemBalloon, new pm::AggregateAvg()));
12469 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemBalloon, new pm::AggregateMin()));
12470 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemBalloon, new pm::AggregateMax()));
12471
12472 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemShared, 0));
12473 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemShared, new pm::AggregateAvg()));
12474 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemShared, new pm::AggregateMin()));
12475 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemShared, new pm::AggregateMax()));
12476
12477 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemCache, 0));
12478 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemCache, new pm::AggregateAvg()));
12479 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemCache, new pm::AggregateMin()));
12480 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemCache, new pm::AggregateMax()));
12481
12482 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestPagedTotal, 0));
12483 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestPagedTotal, new pm::AggregateAvg()));
12484 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestPagedTotal, new pm::AggregateMin()));
12485 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestPagedTotal, new pm::AggregateMax()));
12486}
12487
12488void Machine::i_unregisterMetrics(PerformanceCollector *aCollector, Machine *aMachine)
12489{
12490 AssertReturnVoid(isWriteLockOnCurrentThread());
12491
12492 if (aCollector)
12493 {
12494 aCollector->unregisterMetricsFor(aMachine);
12495 aCollector->unregisterBaseMetricsFor(aMachine);
12496 }
12497}
12498
12499#endif /* VBOX_WITH_RESOURCE_USAGE_API */
12500
12501
12502////////////////////////////////////////////////////////////////////////////////
12503
12504DEFINE_EMPTY_CTOR_DTOR(SessionMachine)
12505
12506HRESULT SessionMachine::FinalConstruct()
12507{
12508 LogFlowThisFunc(("\n"));
12509
12510 mClientToken = NULL;
12511
12512 return BaseFinalConstruct();
12513}
12514
12515void SessionMachine::FinalRelease()
12516{
12517 LogFlowThisFunc(("\n"));
12518
12519 Assert(!mClientToken);
12520 /* paranoia, should not hang around any more */
12521 if (mClientToken)
12522 {
12523 delete mClientToken;
12524 mClientToken = NULL;
12525 }
12526
12527 uninit(Uninit::Unexpected);
12528
12529 BaseFinalRelease();
12530}
12531
12532/**
12533 * @note Must be called only by Machine::LockMachine() from its own write lock.
12534 */
12535HRESULT SessionMachine::init(Machine *aMachine)
12536{
12537 LogFlowThisFuncEnter();
12538 LogFlowThisFunc(("mName={%s}\n", aMachine->mUserData->s.strName.c_str()));
12539
12540 AssertReturn(aMachine, E_INVALIDARG);
12541
12542 AssertReturn(aMachine->lockHandle()->isWriteLockOnCurrentThread(), E_FAIL);
12543
12544 /* Enclose the state transition NotReady->InInit->Ready */
12545 AutoInitSpan autoInitSpan(this);
12546 AssertReturn(autoInitSpan.isOk(), E_FAIL);
12547
12548 HRESULT rc = S_OK;
12549
12550 RT_ZERO(mAuthLibCtx);
12551
12552 /* create the machine client token */
12553 try
12554 {
12555 mClientToken = new ClientToken(aMachine, this);
12556 if (!mClientToken->isReady())
12557 {
12558 delete mClientToken;
12559 mClientToken = NULL;
12560 rc = E_FAIL;
12561 }
12562 }
12563 catch (std::bad_alloc &)
12564 {
12565 rc = E_OUTOFMEMORY;
12566 }
12567 if (FAILED(rc))
12568 return rc;
12569
12570 /* memorize the peer Machine */
12571 unconst(mPeer) = aMachine;
12572 /* share the parent pointer */
12573 unconst(mParent) = aMachine->mParent;
12574
12575 /* take the pointers to data to share */
12576 mData.share(aMachine->mData);
12577 mSSData.share(aMachine->mSSData);
12578
12579 mUserData.share(aMachine->mUserData);
12580 mHWData.share(aMachine->mHWData);
12581 mMediumAttachments.share(aMachine->mMediumAttachments);
12582
12583 mStorageControllers.allocate();
12584 for (StorageControllerList::const_iterator
12585 it = aMachine->mStorageControllers->begin();
12586 it != aMachine->mStorageControllers->end();
12587 ++it)
12588 {
12589 ComObjPtr<StorageController> ctl;
12590 ctl.createObject();
12591 ctl->init(this, *it);
12592 mStorageControllers->push_back(ctl);
12593 }
12594
12595 mUSBControllers.allocate();
12596 for (USBControllerList::const_iterator
12597 it = aMachine->mUSBControllers->begin();
12598 it != aMachine->mUSBControllers->end();
12599 ++it)
12600 {
12601 ComObjPtr<USBController> ctl;
12602 ctl.createObject();
12603 ctl->init(this, *it);
12604 mUSBControllers->push_back(ctl);
12605 }
12606
12607 unconst(mBIOSSettings).createObject();
12608 mBIOSSettings->init(this, aMachine->mBIOSSettings);
12609 /* create another VRDEServer object that will be mutable */
12610 unconst(mVRDEServer).createObject();
12611 mVRDEServer->init(this, aMachine->mVRDEServer);
12612 /* create another audio adapter object that will be mutable */
12613 unconst(mAudioAdapter).createObject();
12614 mAudioAdapter->init(this, aMachine->mAudioAdapter);
12615 /* create a list of serial ports that will be mutable */
12616 for (ULONG slot = 0; slot < RT_ELEMENTS(mSerialPorts); ++slot)
12617 {
12618 unconst(mSerialPorts[slot]).createObject();
12619 mSerialPorts[slot]->init(this, aMachine->mSerialPorts[slot]);
12620 }
12621 /* create a list of parallel ports that will be mutable */
12622 for (ULONG slot = 0; slot < RT_ELEMENTS(mParallelPorts); ++slot)
12623 {
12624 unconst(mParallelPorts[slot]).createObject();
12625 mParallelPorts[slot]->init(this, aMachine->mParallelPorts[slot]);
12626 }
12627
12628 /* create another USB device filters object that will be mutable */
12629 unconst(mUSBDeviceFilters).createObject();
12630 mUSBDeviceFilters->init(this, aMachine->mUSBDeviceFilters);
12631
12632 /* create a list of network adapters that will be mutable */
12633 mNetworkAdapters.resize(aMachine->mNetworkAdapters.size());
12634 for (ULONG slot = 0; slot < mNetworkAdapters.size(); ++slot)
12635 {
12636 unconst(mNetworkAdapters[slot]).createObject();
12637 mNetworkAdapters[slot]->init(this, aMachine->mNetworkAdapters[slot]);
12638 }
12639
12640 /* create another bandwidth control object that will be mutable */
12641 unconst(mBandwidthControl).createObject();
12642 mBandwidthControl->init(this, aMachine->mBandwidthControl);
12643
12644#ifdef VBOX_WITH_UNATTENDED
12645 /* create another unattended object that will be mutable */
12646 unconst(mUnattended).createObject();
12647 mUnattended->init(this, aMachine->mUnattended);
12648#endif
12649
12650 /* default is to delete saved state on Saved -> PoweredOff transition */
12651 mRemoveSavedState = true;
12652
12653 /* Confirm a successful initialization when it's the case */
12654 autoInitSpan.setSucceeded();
12655
12656 miNATNetworksStarted = 0;
12657
12658 LogFlowThisFuncLeave();
12659 return rc;
12660}
12661
12662/**
12663 * Uninitializes this session object. If the reason is other than
12664 * Uninit::Unexpected, then this method MUST be called from #i_checkForDeath()
12665 * or the client watcher code.
12666 *
12667 * @param aReason uninitialization reason
12668 *
12669 * @note Locks mParent + this object for writing.
12670 */
12671void SessionMachine::uninit(Uninit::Reason aReason)
12672{
12673 LogFlowThisFuncEnter();
12674 LogFlowThisFunc(("reason=%d\n", aReason));
12675
12676 /*
12677 * Strongly reference ourselves to prevent this object deletion after
12678 * mData->mSession.mMachine.setNull() below (which can release the last
12679 * reference and call the destructor). Important: this must be done before
12680 * accessing any members (and before AutoUninitSpan that does it as well).
12681 * This self reference will be released as the very last step on return.
12682 */
12683 ComObjPtr<SessionMachine> selfRef;
12684 if (aReason != Uninit::Unexpected)
12685 selfRef = this;
12686
12687 /* Enclose the state transition Ready->InUninit->NotReady */
12688 AutoUninitSpan autoUninitSpan(this);
12689 if (autoUninitSpan.uninitDone())
12690 {
12691 LogFlowThisFunc(("Already uninitialized\n"));
12692 LogFlowThisFuncLeave();
12693 return;
12694 }
12695
12696 if (autoUninitSpan.initFailed())
12697 {
12698 /* We've been called by init() because it's failed. It's not really
12699 * necessary (nor it's safe) to perform the regular uninit sequence
12700 * below, the following is enough.
12701 */
12702 LogFlowThisFunc(("Initialization failed.\n"));
12703 /* destroy the machine client token */
12704 if (mClientToken)
12705 {
12706 delete mClientToken;
12707 mClientToken = NULL;
12708 }
12709 uninitDataAndChildObjects();
12710 mData.free();
12711 unconst(mParent) = NULL;
12712 unconst(mPeer) = NULL;
12713 LogFlowThisFuncLeave();
12714 return;
12715 }
12716
12717 MachineState_T lastState;
12718 {
12719 AutoReadLock tempLock(this COMMA_LOCKVAL_SRC_POS);
12720 lastState = mData->mMachineState;
12721 }
12722 NOREF(lastState);
12723
12724#ifdef VBOX_WITH_USB
12725 // release all captured USB devices, but do this before requesting the locks below
12726 if (aReason == Uninit::Abnormal && Global::IsOnline(lastState))
12727 {
12728 /* Console::captureUSBDevices() is called in the VM process only after
12729 * setting the machine state to Starting or Restoring.
12730 * Console::detachAllUSBDevices() will be called upon successful
12731 * termination. So, we need to release USB devices only if there was
12732 * an abnormal termination of a running VM.
12733 *
12734 * This is identical to SessionMachine::DetachAllUSBDevices except
12735 * for the aAbnormal argument. */
12736 HRESULT rc = mUSBDeviceFilters->i_notifyProxy(false /* aInsertFilters */);
12737 AssertComRC(rc);
12738 NOREF(rc);
12739
12740 USBProxyService *service = mParent->i_host()->i_usbProxyService();
12741 if (service)
12742 service->detachAllDevicesFromVM(this, true /* aDone */, true /* aAbnormal */);
12743 }
12744#endif /* VBOX_WITH_USB */
12745
12746 // we need to lock this object in uninit() because the lock is shared
12747 // with mPeer (as well as data we modify below). mParent lock is needed
12748 // by several calls to it.
12749 AutoMultiWriteLock2 multilock(mParent, this COMMA_LOCKVAL_SRC_POS);
12750
12751#ifdef VBOX_WITH_RESOURCE_USAGE_API
12752 /*
12753 * It is safe to call Machine::i_unregisterMetrics() here because
12754 * PerformanceCollector::samplerCallback no longer accesses guest methods
12755 * holding the lock.
12756 */
12757 i_unregisterMetrics(mParent->i_performanceCollector(), mPeer);
12758 /* The guest must be unregistered after its metrics (@bugref{5949}). */
12759 Log7(("{%p} " LOG_FN_FMT ": mCollectorGuest=%p\n", this, __PRETTY_FUNCTION__, mCollectorGuest));
12760 if (mCollectorGuest)
12761 {
12762 mParent->i_performanceCollector()->unregisterGuest(mCollectorGuest);
12763 // delete mCollectorGuest; => CollectorGuestManager::destroyUnregistered()
12764 mCollectorGuest = NULL;
12765 }
12766#endif
12767
12768 if (aReason == Uninit::Abnormal)
12769 {
12770 Log1WarningThisFunc(("ABNORMAL client termination! (wasBusy=%d)\n", Global::IsOnlineOrTransient(lastState)));
12771
12772 /* reset the state to Aborted */
12773 if (mData->mMachineState != MachineState_Aborted)
12774 i_setMachineState(MachineState_Aborted);
12775 }
12776
12777 // any machine settings modified?
12778 if (mData->flModifications)
12779 {
12780 Log1WarningThisFunc(("Discarding unsaved settings changes!\n"));
12781 i_rollback(false /* aNotify */);
12782 }
12783
12784 mData->mSession.mPID = NIL_RTPROCESS;
12785
12786 if (aReason == Uninit::Unexpected)
12787 {
12788 /* Uninitialization didn't come from #i_checkForDeath(), so tell the
12789 * client watcher thread to update the set of machines that have open
12790 * sessions. */
12791 mParent->i_updateClientWatcher();
12792 }
12793
12794 /* uninitialize all remote controls */
12795 if (mData->mSession.mRemoteControls.size())
12796 {
12797 LogFlowThisFunc(("Closing remote sessions (%d):\n",
12798 mData->mSession.mRemoteControls.size()));
12799
12800 /* Always restart a the beginning, since the iterator is invalidated
12801 * by using erase(). */
12802 for (Data::Session::RemoteControlList::iterator
12803 it = mData->mSession.mRemoteControls.begin();
12804 it != mData->mSession.mRemoteControls.end();
12805 it = mData->mSession.mRemoteControls.begin())
12806 {
12807 ComPtr<IInternalSessionControl> pControl = *it;
12808 mData->mSession.mRemoteControls.erase(it);
12809 multilock.release();
12810 LogFlowThisFunc((" Calling remoteControl->Uninitialize()...\n"));
12811 HRESULT rc = pControl->Uninitialize();
12812 LogFlowThisFunc((" remoteControl->Uninitialize() returned %08X\n", rc));
12813 if (FAILED(rc))
12814 Log1WarningThisFunc(("Forgot to close the remote session?\n"));
12815 multilock.acquire();
12816 }
12817 mData->mSession.mRemoteControls.clear();
12818 }
12819
12820 /* Remove all references to the NAT network service. The service will stop
12821 * if all references (also from other VMs) are removed. */
12822 for (; miNATNetworksStarted > 0; miNATNetworksStarted--)
12823 {
12824 for (ULONG slot = 0; slot < mNetworkAdapters.size(); ++slot)
12825 {
12826 BOOL enabled;
12827 HRESULT hrc = mNetworkAdapters[slot]->COMGETTER(Enabled)(&enabled);
12828 if ( FAILED(hrc)
12829 || !enabled)
12830 continue;
12831
12832 NetworkAttachmentType_T type;
12833 hrc = mNetworkAdapters[slot]->COMGETTER(AttachmentType)(&type);
12834 if ( SUCCEEDED(hrc)
12835 && type == NetworkAttachmentType_NATNetwork)
12836 {
12837 Bstr name;
12838 hrc = mNetworkAdapters[slot]->COMGETTER(NATNetwork)(name.asOutParam());
12839 if (SUCCEEDED(hrc))
12840 {
12841 multilock.release();
12842 LogRel(("VM '%s' stops using NAT network '%ls'\n",
12843 mUserData->s.strName.c_str(), name.raw()));
12844 mParent->i_natNetworkRefDec(name.raw());
12845 multilock.acquire();
12846 }
12847 }
12848 }
12849 }
12850
12851 /*
12852 * An expected uninitialization can come only from #i_checkForDeath().
12853 * Otherwise it means that something's gone really wrong (for example,
12854 * the Session implementation has released the VirtualBox reference
12855 * before it triggered #OnSessionEnd(), or before releasing IPC semaphore,
12856 * etc). However, it's also possible, that the client releases the IPC
12857 * semaphore correctly (i.e. before it releases the VirtualBox reference),
12858 * but the VirtualBox release event comes first to the server process.
12859 * This case is practically possible, so we should not assert on an
12860 * unexpected uninit, just log a warning.
12861 */
12862
12863 if (aReason == Uninit::Unexpected)
12864 Log1WarningThisFunc(("Unexpected SessionMachine uninitialization!\n"));
12865
12866 if (aReason != Uninit::Normal)
12867 {
12868 mData->mSession.mDirectControl.setNull();
12869 }
12870 else
12871 {
12872 /* this must be null here (see #OnSessionEnd()) */
12873 Assert(mData->mSession.mDirectControl.isNull());
12874 Assert(mData->mSession.mState == SessionState_Unlocking);
12875 Assert(!mData->mSession.mProgress.isNull());
12876 }
12877 if (mData->mSession.mProgress)
12878 {
12879 if (aReason == Uninit::Normal)
12880 mData->mSession.mProgress->i_notifyComplete(S_OK);
12881 else
12882 mData->mSession.mProgress->i_notifyComplete(E_FAIL,
12883 COM_IIDOF(ISession),
12884 getComponentName(),
12885 tr("The VM session was aborted"));
12886 mData->mSession.mProgress.setNull();
12887 }
12888
12889 if (mConsoleTaskData.mProgress)
12890 {
12891 Assert(aReason == Uninit::Abnormal);
12892 mConsoleTaskData.mProgress->i_notifyComplete(E_FAIL,
12893 COM_IIDOF(ISession),
12894 getComponentName(),
12895 tr("The VM session was aborted"));
12896 mConsoleTaskData.mProgress.setNull();
12897 }
12898
12899 /* remove the association between the peer machine and this session machine */
12900 Assert( (SessionMachine*)mData->mSession.mMachine == this
12901 || aReason == Uninit::Unexpected);
12902
12903 /* reset the rest of session data */
12904 mData->mSession.mLockType = LockType_Null;
12905 mData->mSession.mMachine.setNull();
12906 mData->mSession.mState = SessionState_Unlocked;
12907 mData->mSession.mName.setNull();
12908
12909 /* destroy the machine client token before leaving the exclusive lock */
12910 if (mClientToken)
12911 {
12912 delete mClientToken;
12913 mClientToken = NULL;
12914 }
12915
12916 /* fire an event */
12917 mParent->i_onSessionStateChange(mData->mUuid, SessionState_Unlocked);
12918
12919 uninitDataAndChildObjects();
12920
12921 /* free the essential data structure last */
12922 mData.free();
12923
12924 /* release the exclusive lock before setting the below two to NULL */
12925 multilock.release();
12926
12927 unconst(mParent) = NULL;
12928 unconst(mPeer) = NULL;
12929
12930 AuthLibUnload(&mAuthLibCtx);
12931
12932 LogFlowThisFuncLeave();
12933}
12934
12935// util::Lockable interface
12936////////////////////////////////////////////////////////////////////////////////
12937
12938/**
12939 * Overrides VirtualBoxBase::lockHandle() in order to share the lock handle
12940 * with the primary Machine instance (mPeer).
12941 */
12942RWLockHandle *SessionMachine::lockHandle() const
12943{
12944 AssertReturn(mPeer != NULL, NULL);
12945 return mPeer->lockHandle();
12946}
12947
12948// IInternalMachineControl methods
12949////////////////////////////////////////////////////////////////////////////////
12950
12951/**
12952 * Passes collected guest statistics to performance collector object
12953 */
12954HRESULT SessionMachine::reportVmStatistics(ULONG aValidStats, ULONG aCpuUser,
12955 ULONG aCpuKernel, ULONG aCpuIdle,
12956 ULONG aMemTotal, ULONG aMemFree,
12957 ULONG aMemBalloon, ULONG aMemShared,
12958 ULONG aMemCache, ULONG aPageTotal,
12959 ULONG aAllocVMM, ULONG aFreeVMM,
12960 ULONG aBalloonedVMM, ULONG aSharedVMM,
12961 ULONG aVmNetRx, ULONG aVmNetTx)
12962{
12963#ifdef VBOX_WITH_RESOURCE_USAGE_API
12964 if (mCollectorGuest)
12965 mCollectorGuest->updateStats(aValidStats, aCpuUser, aCpuKernel, aCpuIdle,
12966 aMemTotal, aMemFree, aMemBalloon, aMemShared,
12967 aMemCache, aPageTotal, aAllocVMM, aFreeVMM,
12968 aBalloonedVMM, aSharedVMM, aVmNetRx, aVmNetTx);
12969
12970 return S_OK;
12971#else
12972 NOREF(aValidStats);
12973 NOREF(aCpuUser);
12974 NOREF(aCpuKernel);
12975 NOREF(aCpuIdle);
12976 NOREF(aMemTotal);
12977 NOREF(aMemFree);
12978 NOREF(aMemBalloon);
12979 NOREF(aMemShared);
12980 NOREF(aMemCache);
12981 NOREF(aPageTotal);
12982 NOREF(aAllocVMM);
12983 NOREF(aFreeVMM);
12984 NOREF(aBalloonedVMM);
12985 NOREF(aSharedVMM);
12986 NOREF(aVmNetRx);
12987 NOREF(aVmNetTx);
12988 return E_NOTIMPL;
12989#endif
12990}
12991
12992////////////////////////////////////////////////////////////////////////////////
12993//
12994// SessionMachine task records
12995//
12996////////////////////////////////////////////////////////////////////////////////
12997
12998/**
12999 * Task record for saving the machine state.
13000 */
13001class SessionMachine::SaveStateTask
13002 : public Machine::Task
13003{
13004public:
13005 SaveStateTask(SessionMachine *m,
13006 Progress *p,
13007 const Utf8Str &t,
13008 Reason_T enmReason,
13009 const Utf8Str &strStateFilePath)
13010 : Task(m, p, t),
13011 m_enmReason(enmReason),
13012 m_strStateFilePath(strStateFilePath)
13013 {}
13014
13015private:
13016 void handler()
13017 {
13018 ((SessionMachine *)(Machine *)m_pMachine)->i_saveStateHandler(*this);
13019 }
13020
13021 Reason_T m_enmReason;
13022 Utf8Str m_strStateFilePath;
13023
13024 friend class SessionMachine;
13025};
13026
13027/**
13028 * Task thread implementation for SessionMachine::SaveState(), called from
13029 * SessionMachine::taskHandler().
13030 *
13031 * @note Locks this object for writing.
13032 *
13033 * @param task
13034 * @return
13035 */
13036void SessionMachine::i_saveStateHandler(SaveStateTask &task)
13037{
13038 LogFlowThisFuncEnter();
13039
13040 AutoCaller autoCaller(this);
13041 LogFlowThisFunc(("state=%d\n", getObjectState().getState()));
13042 if (FAILED(autoCaller.rc()))
13043 {
13044 /* we might have been uninitialized because the session was accidentally
13045 * closed by the client, so don't assert */
13046 HRESULT rc = setError(E_FAIL,
13047 tr("The session has been accidentally closed"));
13048 task.m_pProgress->i_notifyComplete(rc);
13049 LogFlowThisFuncLeave();
13050 return;
13051 }
13052
13053 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
13054
13055 HRESULT rc = S_OK;
13056
13057 try
13058 {
13059 ComPtr<IInternalSessionControl> directControl;
13060 if (mData->mSession.mLockType == LockType_VM)
13061 directControl = mData->mSession.mDirectControl;
13062 if (directControl.isNull())
13063 throw setError(VBOX_E_INVALID_VM_STATE,
13064 tr("Trying to save state without a running VM"));
13065 alock.release();
13066 BOOL fSuspendedBySave;
13067 rc = directControl->SaveStateWithReason(task.m_enmReason, task.m_pProgress, Bstr(task.m_strStateFilePath).raw(), task.m_machineStateBackup != MachineState_Paused, &fSuspendedBySave);
13068 Assert(!fSuspendedBySave);
13069 alock.acquire();
13070
13071 AssertStmt( (SUCCEEDED(rc) && mData->mMachineState == MachineState_Saved)
13072 || (FAILED(rc) && mData->mMachineState == MachineState_Saving),
13073 throw E_FAIL);
13074
13075 if (SUCCEEDED(rc))
13076 {
13077 mSSData->strStateFilePath = task.m_strStateFilePath;
13078
13079 /* save all VM settings */
13080 rc = i_saveSettings(NULL);
13081 // no need to check whether VirtualBox.xml needs saving also since
13082 // we can't have a name change pending at this point
13083 }
13084 else
13085 {
13086 // On failure, set the state to the state we had at the beginning.
13087 i_setMachineState(task.m_machineStateBackup);
13088 i_updateMachineStateOnClient();
13089
13090 // Delete the saved state file (might have been already created).
13091 // No need to check whether this is shared with a snapshot here
13092 // because we certainly created a fresh saved state file here.
13093 RTFileDelete(task.m_strStateFilePath.c_str());
13094 }
13095 }
13096 catch (HRESULT aRC) { rc = aRC; }
13097
13098 task.m_pProgress->i_notifyComplete(rc);
13099
13100 LogFlowThisFuncLeave();
13101}
13102
13103/**
13104 * @note Locks this object for writing.
13105 */
13106HRESULT SessionMachine::saveState(ComPtr<IProgress> &aProgress)
13107{
13108 return i_saveStateWithReason(Reason_Unspecified, aProgress);
13109}
13110
13111HRESULT SessionMachine::i_saveStateWithReason(Reason_T aReason, ComPtr<IProgress> &aProgress)
13112{
13113 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
13114
13115 HRESULT rc = i_checkStateDependency(MutableOrRunningStateDep);
13116 if (FAILED(rc)) return rc;
13117
13118 if ( mData->mMachineState != MachineState_Running
13119 && mData->mMachineState != MachineState_Paused
13120 )
13121 return setError(VBOX_E_INVALID_VM_STATE,
13122 tr("Cannot save the execution state as the machine is not running or paused (machine state: %s)"),
13123 Global::stringifyMachineState(mData->mMachineState));
13124
13125 ComObjPtr<Progress> pProgress;
13126 pProgress.createObject();
13127 rc = pProgress->init(i_getVirtualBox(),
13128 static_cast<IMachine *>(this) /* aInitiator */,
13129 Bstr(tr("Saving the execution state of the virtual machine")).raw(),
13130 FALSE /* aCancelable */);
13131 if (FAILED(rc))
13132 return rc;
13133
13134 Utf8Str strStateFilePath;
13135 i_composeSavedStateFilename(strStateFilePath);
13136
13137 /* create and start the task on a separate thread (note that it will not
13138 * start working until we release alock) */
13139 SaveStateTask *pTask = new SaveStateTask(this, pProgress, "SaveState", aReason, strStateFilePath);
13140 rc = pTask->createThread();
13141 if (FAILED(rc))
13142 return rc;
13143
13144 /* set the state to Saving (expected by Session::SaveStateWithReason()) */
13145 i_setMachineState(MachineState_Saving);
13146 i_updateMachineStateOnClient();
13147
13148 pProgress.queryInterfaceTo(aProgress.asOutParam());
13149
13150 return S_OK;
13151}
13152
13153/**
13154 * @note Locks this object for writing.
13155 */
13156HRESULT SessionMachine::adoptSavedState(const com::Utf8Str &aSavedStateFile)
13157{
13158 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
13159
13160 HRESULT rc = i_checkStateDependency(MutableStateDep);
13161 if (FAILED(rc)) return rc;
13162
13163 if ( mData->mMachineState != MachineState_PoweredOff
13164 && mData->mMachineState != MachineState_Teleported
13165 && mData->mMachineState != MachineState_Aborted
13166 )
13167 return setError(VBOX_E_INVALID_VM_STATE,
13168 tr("Cannot adopt the saved machine state as the machine is not in Powered Off, Teleported or Aborted state (machine state: %s)"),
13169 Global::stringifyMachineState(mData->mMachineState));
13170
13171 com::Utf8Str stateFilePathFull;
13172 int vrc = i_calculateFullPath(aSavedStateFile, stateFilePathFull);
13173 if (RT_FAILURE(vrc))
13174 return setError(VBOX_E_FILE_ERROR,
13175 tr("Invalid saved state file path '%s' (%Rrc)"),
13176 aSavedStateFile.c_str(),
13177 vrc);
13178
13179 mSSData->strStateFilePath = stateFilePathFull;
13180
13181 /* The below i_setMachineState() will detect the state transition and will
13182 * update the settings file */
13183
13184 return i_setMachineState(MachineState_Saved);
13185}
13186
13187/**
13188 * @note Locks this object for writing.
13189 */
13190HRESULT SessionMachine::discardSavedState(BOOL aFRemoveFile)
13191{
13192 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
13193
13194 HRESULT rc = i_checkStateDependency(MutableOrSavedStateDep);
13195 if (FAILED(rc)) return rc;
13196
13197 if (mData->mMachineState != MachineState_Saved)
13198 return setError(VBOX_E_INVALID_VM_STATE,
13199 tr("Cannot delete the machine state as the machine is not in the saved state (machine state: %s)"),
13200 Global::stringifyMachineState(mData->mMachineState));
13201
13202 mRemoveSavedState = RT_BOOL(aFRemoveFile);
13203
13204 /*
13205 * Saved -> PoweredOff transition will be detected in the SessionMachine
13206 * and properly handled.
13207 */
13208 rc = i_setMachineState(MachineState_PoweredOff);
13209 return rc;
13210}
13211
13212
13213/**
13214 * @note Locks the same as #i_setMachineState() does.
13215 */
13216HRESULT SessionMachine::updateState(MachineState_T aState)
13217{
13218 return i_setMachineState(aState);
13219}
13220
13221/**
13222 * @note Locks this object for writing.
13223 */
13224HRESULT SessionMachine::beginPowerUp(const ComPtr<IProgress> &aProgress)
13225{
13226 IProgress *pProgress(aProgress);
13227
13228 LogFlowThisFunc(("aProgress=%p\n", pProgress));
13229
13230 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
13231
13232 if (mData->mSession.mState != SessionState_Locked)
13233 return VBOX_E_INVALID_OBJECT_STATE;
13234
13235 if (!mData->mSession.mProgress.isNull())
13236 mData->mSession.mProgress->setOtherProgressObject(pProgress);
13237
13238 /* If we didn't reference the NAT network service yet, add a reference to
13239 * force a start */
13240 if (miNATNetworksStarted < 1)
13241 {
13242 for (ULONG slot = 0; slot < mNetworkAdapters.size(); ++slot)
13243 {
13244 BOOL enabled;
13245 HRESULT hrc = mNetworkAdapters[slot]->COMGETTER(Enabled)(&enabled);
13246 if ( FAILED(hrc)
13247 || !enabled)
13248 continue;
13249
13250 NetworkAttachmentType_T type;
13251 hrc = mNetworkAdapters[slot]->COMGETTER(AttachmentType)(&type);
13252 if ( SUCCEEDED(hrc)
13253 && type == NetworkAttachmentType_NATNetwork)
13254 {
13255 Bstr name;
13256 hrc = mNetworkAdapters[slot]->COMGETTER(NATNetwork)(name.asOutParam());
13257 if (SUCCEEDED(hrc))
13258 {
13259 LogRel(("VM '%s' starts using NAT network '%ls'\n",
13260 mUserData->s.strName.c_str(), name.raw()));
13261 mPeer->lockHandle()->unlockWrite();
13262 mParent->i_natNetworkRefInc(name.raw());
13263#ifdef RT_LOCK_STRICT
13264 mPeer->lockHandle()->lockWrite(RT_SRC_POS);
13265#else
13266 mPeer->lockHandle()->lockWrite();
13267#endif
13268 }
13269 }
13270 }
13271 miNATNetworksStarted++;
13272 }
13273
13274 LogFlowThisFunc(("returns S_OK.\n"));
13275 return S_OK;
13276}
13277
13278/**
13279 * @note Locks this object for writing.
13280 */
13281HRESULT SessionMachine::endPowerUp(LONG aResult)
13282{
13283 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
13284
13285 if (mData->mSession.mState != SessionState_Locked)
13286 return VBOX_E_INVALID_OBJECT_STATE;
13287
13288 /* Finalize the LaunchVMProcess progress object. */
13289 if (mData->mSession.mProgress)
13290 {
13291 mData->mSession.mProgress->notifyComplete((HRESULT)aResult);
13292 mData->mSession.mProgress.setNull();
13293 }
13294
13295 if (SUCCEEDED((HRESULT)aResult))
13296 {
13297#ifdef VBOX_WITH_RESOURCE_USAGE_API
13298 /* The VM has been powered up successfully, so it makes sense
13299 * now to offer the performance metrics for a running machine
13300 * object. Doing it earlier wouldn't be safe. */
13301 i_registerMetrics(mParent->i_performanceCollector(), mPeer,
13302 mData->mSession.mPID);
13303#endif /* VBOX_WITH_RESOURCE_USAGE_API */
13304 }
13305
13306 return S_OK;
13307}
13308
13309/**
13310 * @note Locks this object for writing.
13311 */
13312HRESULT SessionMachine::beginPoweringDown(ComPtr<IProgress> &aProgress)
13313{
13314 LogFlowThisFuncEnter();
13315
13316 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
13317
13318 AssertReturn(mConsoleTaskData.mLastState == MachineState_Null,
13319 E_FAIL);
13320
13321 /* create a progress object to track operation completion */
13322 ComObjPtr<Progress> pProgress;
13323 pProgress.createObject();
13324 pProgress->init(i_getVirtualBox(),
13325 static_cast<IMachine *>(this) /* aInitiator */,
13326 Bstr(tr("Stopping the virtual machine")).raw(),
13327 FALSE /* aCancelable */);
13328
13329 /* fill in the console task data */
13330 mConsoleTaskData.mLastState = mData->mMachineState;
13331 mConsoleTaskData.mProgress = pProgress;
13332
13333 /* set the state to Stopping (this is expected by Console::PowerDown()) */
13334 i_setMachineState(MachineState_Stopping);
13335
13336 pProgress.queryInterfaceTo(aProgress.asOutParam());
13337
13338 return S_OK;
13339}
13340
13341/**
13342 * @note Locks this object for writing.
13343 */
13344HRESULT SessionMachine::endPoweringDown(LONG aResult,
13345 const com::Utf8Str &aErrMsg)
13346{
13347 LogFlowThisFuncEnter();
13348
13349 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
13350
13351 AssertReturn( ( (SUCCEEDED(aResult) && mData->mMachineState == MachineState_PoweredOff)
13352 || (FAILED(aResult) && mData->mMachineState == MachineState_Stopping))
13353 && mConsoleTaskData.mLastState != MachineState_Null,
13354 E_FAIL);
13355
13356 /*
13357 * On failure, set the state to the state we had when BeginPoweringDown()
13358 * was called (this is expected by Console::PowerDown() and the associated
13359 * task). On success the VM process already changed the state to
13360 * MachineState_PoweredOff, so no need to do anything.
13361 */
13362 if (FAILED(aResult))
13363 i_setMachineState(mConsoleTaskData.mLastState);
13364
13365 /* notify the progress object about operation completion */
13366 Assert(mConsoleTaskData.mProgress);
13367 if (SUCCEEDED(aResult))
13368 mConsoleTaskData.mProgress->i_notifyComplete(S_OK);
13369 else
13370 {
13371 if (aErrMsg.length())
13372 mConsoleTaskData.mProgress->i_notifyComplete(aResult,
13373 COM_IIDOF(ISession),
13374 getComponentName(),
13375 aErrMsg.c_str());
13376 else
13377 mConsoleTaskData.mProgress->i_notifyComplete(aResult);
13378 }
13379
13380 /* clear out the temporary saved state data */
13381 mConsoleTaskData.mLastState = MachineState_Null;
13382 mConsoleTaskData.mProgress.setNull();
13383
13384 LogFlowThisFuncLeave();
13385 return S_OK;
13386}
13387
13388
13389/**
13390 * Goes through the USB filters of the given machine to see if the given
13391 * device matches any filter or not.
13392 *
13393 * @note Locks the same as USBController::hasMatchingFilter() does.
13394 */
13395HRESULT SessionMachine::runUSBDeviceFilters(const ComPtr<IUSBDevice> &aDevice,
13396 BOOL *aMatched,
13397 ULONG *aMaskedInterfaces)
13398{
13399 LogFlowThisFunc(("\n"));
13400
13401#ifdef VBOX_WITH_USB
13402 *aMatched = mUSBDeviceFilters->i_hasMatchingFilter(aDevice, aMaskedInterfaces);
13403#else
13404 NOREF(aDevice);
13405 NOREF(aMaskedInterfaces);
13406 *aMatched = FALSE;
13407#endif
13408
13409 return S_OK;
13410}
13411
13412/**
13413 * @note Locks the same as Host::captureUSBDevice() does.
13414 */
13415HRESULT SessionMachine::captureUSBDevice(const com::Guid &aId, const com::Utf8Str &aCaptureFilename)
13416{
13417 LogFlowThisFunc(("\n"));
13418
13419#ifdef VBOX_WITH_USB
13420 /* if captureDeviceForVM() fails, it must have set extended error info */
13421 clearError();
13422 MultiResult rc = mParent->i_host()->i_checkUSBProxyService();
13423 if (FAILED(rc)) return rc;
13424
13425 USBProxyService *service = mParent->i_host()->i_usbProxyService();
13426 AssertReturn(service, E_FAIL);
13427 return service->captureDeviceForVM(this, aId.ref(), aCaptureFilename);
13428#else
13429 NOREF(aId);
13430 return E_NOTIMPL;
13431#endif
13432}
13433
13434/**
13435 * @note Locks the same as Host::detachUSBDevice() does.
13436 */
13437HRESULT SessionMachine::detachUSBDevice(const com::Guid &aId,
13438 BOOL aDone)
13439{
13440 LogFlowThisFunc(("\n"));
13441
13442#ifdef VBOX_WITH_USB
13443 USBProxyService *service = mParent->i_host()->i_usbProxyService();
13444 AssertReturn(service, E_FAIL);
13445 return service->detachDeviceFromVM(this, aId.ref(), !!aDone);
13446#else
13447 NOREF(aId);
13448 NOREF(aDone);
13449 return E_NOTIMPL;
13450#endif
13451}
13452
13453/**
13454 * Inserts all machine filters to the USB proxy service and then calls
13455 * Host::autoCaptureUSBDevices().
13456 *
13457 * Called by Console from the VM process upon VM startup.
13458 *
13459 * @note Locks what called methods lock.
13460 */
13461HRESULT SessionMachine::autoCaptureUSBDevices()
13462{
13463 LogFlowThisFunc(("\n"));
13464
13465#ifdef VBOX_WITH_USB
13466 HRESULT rc = mUSBDeviceFilters->i_notifyProxy(true /* aInsertFilters */);
13467 AssertComRC(rc);
13468 NOREF(rc);
13469
13470 USBProxyService *service = mParent->i_host()->i_usbProxyService();
13471 AssertReturn(service, E_FAIL);
13472 return service->autoCaptureDevicesForVM(this);
13473#else
13474 return S_OK;
13475#endif
13476}
13477
13478/**
13479 * Removes all machine filters from the USB proxy service and then calls
13480 * Host::detachAllUSBDevices().
13481 *
13482 * Called by Console from the VM process upon normal VM termination or by
13483 * SessionMachine::uninit() upon abnormal VM termination (from under the
13484 * Machine/SessionMachine lock).
13485 *
13486 * @note Locks what called methods lock.
13487 */
13488HRESULT SessionMachine::detachAllUSBDevices(BOOL aDone)
13489{
13490 LogFlowThisFunc(("\n"));
13491
13492#ifdef VBOX_WITH_USB
13493 HRESULT rc = mUSBDeviceFilters->i_notifyProxy(false /* aInsertFilters */);
13494 AssertComRC(rc);
13495 NOREF(rc);
13496
13497 USBProxyService *service = mParent->i_host()->i_usbProxyService();
13498 AssertReturn(service, E_FAIL);
13499 return service->detachAllDevicesFromVM(this, !!aDone, false /* aAbnormal */);
13500#else
13501 NOREF(aDone);
13502 return S_OK;
13503#endif
13504}
13505
13506/**
13507 * @note Locks this object for writing.
13508 */
13509HRESULT SessionMachine::onSessionEnd(const ComPtr<ISession> &aSession,
13510 ComPtr<IProgress> &aProgress)
13511{
13512 LogFlowThisFuncEnter();
13513
13514 LogFlowThisFunc(("callerstate=%d\n", getObjectState().getState()));
13515 /*
13516 * We don't assert below because it might happen that a non-direct session
13517 * informs us it is closed right after we've been uninitialized -- it's ok.
13518 */
13519
13520 /* get IInternalSessionControl interface */
13521 ComPtr<IInternalSessionControl> control(aSession);
13522
13523 ComAssertRet(!control.isNull(), E_INVALIDARG);
13524
13525 /* Creating a Progress object requires the VirtualBox lock, and
13526 * thus locking it here is required by the lock order rules. */
13527 AutoMultiWriteLock2 alock(mParent, this COMMA_LOCKVAL_SRC_POS);
13528
13529 if (control == mData->mSession.mDirectControl)
13530 {
13531 /* The direct session is being normally closed by the client process
13532 * ----------------------------------------------------------------- */
13533
13534 /* go to the closing state (essential for all open*Session() calls and
13535 * for #i_checkForDeath()) */
13536 Assert(mData->mSession.mState == SessionState_Locked);
13537 mData->mSession.mState = SessionState_Unlocking;
13538
13539 /* set direct control to NULL to release the remote instance */
13540 mData->mSession.mDirectControl.setNull();
13541 LogFlowThisFunc(("Direct control is set to NULL\n"));
13542
13543 if (mData->mSession.mProgress)
13544 {
13545 /* finalize the progress, someone might wait if a frontend
13546 * closes the session before powering on the VM. */
13547 mData->mSession.mProgress->notifyComplete(E_FAIL,
13548 COM_IIDOF(ISession),
13549 getComponentName(),
13550 tr("The VM session was closed before any attempt to power it on"));
13551 mData->mSession.mProgress.setNull();
13552 }
13553
13554 /* Create the progress object the client will use to wait until
13555 * #i_checkForDeath() is called to uninitialize this session object after
13556 * it releases the IPC semaphore.
13557 * Note! Because we're "reusing" mProgress here, this must be a proxy
13558 * object just like for LaunchVMProcess. */
13559 Assert(mData->mSession.mProgress.isNull());
13560 ComObjPtr<ProgressProxy> progress;
13561 progress.createObject();
13562 ComPtr<IUnknown> pPeer(mPeer);
13563 progress->init(mParent, pPeer,
13564 Bstr(tr("Closing session")).raw(),
13565 FALSE /* aCancelable */);
13566 progress.queryInterfaceTo(aProgress.asOutParam());
13567 mData->mSession.mProgress = progress;
13568 }
13569 else
13570 {
13571 /* the remote session is being normally closed */
13572 bool found = false;
13573 for (Data::Session::RemoteControlList::iterator
13574 it = mData->mSession.mRemoteControls.begin();
13575 it != mData->mSession.mRemoteControls.end();
13576 ++it)
13577 {
13578 if (control == *it)
13579 {
13580 found = true;
13581 // This MUST be erase(it), not remove(*it) as the latter
13582 // triggers a very nasty use after free due to the place where
13583 // the value "lives".
13584 mData->mSession.mRemoteControls.erase(it);
13585 break;
13586 }
13587 }
13588 ComAssertMsgRet(found, ("The session is not found in the session list!"),
13589 E_INVALIDARG);
13590 }
13591
13592 /* signal the client watcher thread, because the client is going away */
13593 mParent->i_updateClientWatcher();
13594
13595 LogFlowThisFuncLeave();
13596 return S_OK;
13597}
13598
13599HRESULT SessionMachine::pullGuestProperties(std::vector<com::Utf8Str> &aNames,
13600 std::vector<com::Utf8Str> &aValues,
13601 std::vector<LONG64> &aTimestamps,
13602 std::vector<com::Utf8Str> &aFlags)
13603{
13604 LogFlowThisFunc(("\n"));
13605
13606#ifdef VBOX_WITH_GUEST_PROPS
13607 using namespace guestProp;
13608
13609 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
13610
13611 size_t cEntries = mHWData->mGuestProperties.size();
13612 aNames.resize(cEntries);
13613 aValues.resize(cEntries);
13614 aTimestamps.resize(cEntries);
13615 aFlags.resize(cEntries);
13616
13617 size_t i = 0;
13618 for (HWData::GuestPropertyMap::const_iterator
13619 it = mHWData->mGuestProperties.begin();
13620 it != mHWData->mGuestProperties.end();
13621 ++it, ++i)
13622 {
13623 char szFlags[MAX_FLAGS_LEN + 1];
13624 aNames[i] = it->first;
13625 aValues[i] = it->second.strValue;
13626 aTimestamps[i] = it->second.mTimestamp;
13627
13628 /* If it is NULL, keep it NULL. */
13629 if (it->second.mFlags)
13630 {
13631 writeFlags(it->second.mFlags, szFlags);
13632 aFlags[i] = szFlags;
13633 }
13634 else
13635 aFlags[i] = "";
13636 }
13637 return S_OK;
13638#else
13639 ReturnComNotImplemented();
13640#endif
13641}
13642
13643HRESULT SessionMachine::pushGuestProperty(const com::Utf8Str &aName,
13644 const com::Utf8Str &aValue,
13645 LONG64 aTimestamp,
13646 const com::Utf8Str &aFlags)
13647{
13648 LogFlowThisFunc(("\n"));
13649
13650#ifdef VBOX_WITH_GUEST_PROPS
13651 using namespace guestProp;
13652
13653 try
13654 {
13655 /*
13656 * Convert input up front.
13657 */
13658 uint32_t fFlags = NILFLAG;
13659 if (aFlags.length())
13660 {
13661 int vrc = validateFlags(aFlags.c_str(), &fFlags);
13662 AssertRCReturn(vrc, E_INVALIDARG);
13663 }
13664
13665 /*
13666 * Now grab the object lock, validate the state and do the update.
13667 */
13668
13669 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
13670
13671 if (!Global::IsOnline(mData->mMachineState))
13672 {
13673 AssertMsgFailedReturn(("%s\n", Global::stringifyMachineState(mData->mMachineState)),
13674 VBOX_E_INVALID_VM_STATE);
13675 }
13676
13677 i_setModified(IsModified_MachineData);
13678 mHWData.backup();
13679
13680 bool fDelete = !aValue.length();
13681 HWData::GuestPropertyMap::iterator it = mHWData->mGuestProperties.find(aName);
13682 if (it != mHWData->mGuestProperties.end())
13683 {
13684 if (!fDelete)
13685 {
13686 it->second.strValue = aValue;
13687 it->second.mTimestamp = aTimestamp;
13688 it->second.mFlags = fFlags;
13689 }
13690 else
13691 mHWData->mGuestProperties.erase(it);
13692
13693 mData->mGuestPropertiesModified = TRUE;
13694 }
13695 else if (!fDelete)
13696 {
13697 HWData::GuestProperty prop;
13698 prop.strValue = aValue;
13699 prop.mTimestamp = aTimestamp;
13700 prop.mFlags = fFlags;
13701
13702 mHWData->mGuestProperties[aName] = prop;
13703 mData->mGuestPropertiesModified = TRUE;
13704 }
13705
13706 alock.release();
13707
13708 mParent->i_onGuestPropertyChange(mData->mUuid,
13709 Bstr(aName).raw(),
13710 Bstr(aValue).raw(),
13711 Bstr(aFlags).raw());
13712 }
13713 catch (...)
13714 {
13715 return VirtualBoxBase::handleUnexpectedExceptions(this, RT_SRC_POS);
13716 }
13717 return S_OK;
13718#else
13719 ReturnComNotImplemented();
13720#endif
13721}
13722
13723
13724HRESULT SessionMachine::lockMedia()
13725{
13726 AutoMultiWriteLock2 alock(this->lockHandle(),
13727 &mParent->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
13728
13729 AssertReturn( mData->mMachineState == MachineState_Starting
13730 || mData->mMachineState == MachineState_Restoring
13731 || mData->mMachineState == MachineState_TeleportingIn, E_FAIL);
13732
13733 clearError();
13734 alock.release();
13735 return i_lockMedia();
13736}
13737
13738HRESULT SessionMachine::unlockMedia()
13739{
13740 HRESULT hrc = i_unlockMedia();
13741 return hrc;
13742}
13743
13744HRESULT SessionMachine::ejectMedium(const ComPtr<IMediumAttachment> &aAttachment,
13745 ComPtr<IMediumAttachment> &aNewAttachment)
13746{
13747 // request the host lock first, since might be calling Host methods for getting host drives;
13748 // next, protect the media tree all the while we're in here, as well as our member variables
13749 AutoMultiWriteLock3 multiLock(mParent->i_host()->lockHandle(),
13750 this->lockHandle(),
13751 &mParent->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
13752
13753 IMediumAttachment *iAttach = aAttachment;
13754 ComObjPtr<MediumAttachment> pAttach = static_cast<MediumAttachment *>(iAttach);
13755
13756 Bstr ctrlName;
13757 LONG lPort;
13758 LONG lDevice;
13759 bool fTempEject;
13760 {
13761 AutoReadLock attLock(pAttach COMMA_LOCKVAL_SRC_POS);
13762
13763 /* Need to query the details first, as the IMediumAttachment reference
13764 * might be to the original settings, which we are going to change. */
13765 ctrlName = pAttach->i_getControllerName();
13766 lPort = pAttach->i_getPort();
13767 lDevice = pAttach->i_getDevice();
13768 fTempEject = pAttach->i_getTempEject();
13769 }
13770
13771 if (!fTempEject)
13772 {
13773 /* Remember previously mounted medium. The medium before taking the
13774 * backup is not necessarily the same thing. */
13775 ComObjPtr<Medium> oldmedium;
13776 oldmedium = pAttach->i_getMedium();
13777
13778 i_setModified(IsModified_Storage);
13779 mMediumAttachments.backup();
13780
13781 // The backup operation makes the pAttach reference point to the
13782 // old settings. Re-get the correct reference.
13783 pAttach = i_findAttachment(*mMediumAttachments.data(),
13784 ctrlName.raw(),
13785 lPort,
13786 lDevice);
13787
13788 {
13789 AutoCaller autoAttachCaller(this);
13790 if (FAILED(autoAttachCaller.rc())) return autoAttachCaller.rc();
13791
13792 AutoWriteLock attLock(pAttach COMMA_LOCKVAL_SRC_POS);
13793 if (!oldmedium.isNull())
13794 oldmedium->i_removeBackReference(mData->mUuid);
13795
13796 pAttach->i_updateMedium(NULL);
13797 pAttach->i_updateEjected();
13798 }
13799
13800 i_setModified(IsModified_Storage);
13801 }
13802 else
13803 {
13804 {
13805 AutoWriteLock attLock(pAttach COMMA_LOCKVAL_SRC_POS);
13806 pAttach->i_updateEjected();
13807 }
13808 }
13809
13810 pAttach.queryInterfaceTo(aNewAttachment.asOutParam());
13811
13812 return S_OK;
13813}
13814
13815HRESULT SessionMachine::authenticateExternal(const std::vector<com::Utf8Str> &aAuthParams,
13816 com::Utf8Str &aResult)
13817{
13818 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
13819
13820 HRESULT hr = S_OK;
13821
13822 if (!mAuthLibCtx.hAuthLibrary)
13823 {
13824 /* Load the external authentication library. */
13825 Bstr authLibrary;
13826 mVRDEServer->COMGETTER(AuthLibrary)(authLibrary.asOutParam());
13827
13828 Utf8Str filename = authLibrary;
13829
13830 int rc = AuthLibLoad(&mAuthLibCtx, filename.c_str());
13831 if (RT_FAILURE(rc))
13832 {
13833 hr = setError(E_FAIL,
13834 tr("Could not load the external authentication library '%s' (%Rrc)"),
13835 filename.c_str(), rc);
13836 }
13837 }
13838
13839 /* The auth library might need the machine lock. */
13840 alock.release();
13841
13842 if (FAILED(hr))
13843 return hr;
13844
13845 if (aAuthParams[0] == "VRDEAUTH" && aAuthParams.size() == 7)
13846 {
13847 enum VRDEAuthParams
13848 {
13849 parmUuid = 1,
13850 parmGuestJudgement,
13851 parmUser,
13852 parmPassword,
13853 parmDomain,
13854 parmClientId
13855 };
13856
13857 AuthResult result = AuthResultAccessDenied;
13858
13859 Guid uuid(aAuthParams[parmUuid]);
13860 AuthGuestJudgement guestJudgement = (AuthGuestJudgement)aAuthParams[parmGuestJudgement].toUInt32();
13861 uint32_t u32ClientId = aAuthParams[parmClientId].toUInt32();
13862
13863 result = AuthLibAuthenticate(&mAuthLibCtx,
13864 uuid.raw(), guestJudgement,
13865 aAuthParams[parmUser].c_str(),
13866 aAuthParams[parmPassword].c_str(),
13867 aAuthParams[parmDomain].c_str(),
13868 u32ClientId);
13869
13870 /* Hack: aAuthParams[parmPassword] is const but the code believes in writable memory. */
13871 size_t cbPassword = aAuthParams[parmPassword].length();
13872 if (cbPassword)
13873 {
13874 RTMemWipeThoroughly((void *)aAuthParams[parmPassword].c_str(), cbPassword, 10 /* cPasses */);
13875 memset((void *)aAuthParams[parmPassword].c_str(), 'x', cbPassword);
13876 }
13877
13878 if (result == AuthResultAccessGranted)
13879 aResult = "granted";
13880 else
13881 aResult = "denied";
13882
13883 LogRel(("AUTH: VRDE authentification for user '%s' result '%s'\n",
13884 aAuthParams[parmUser].c_str(), aResult.c_str()));
13885 }
13886 else if (aAuthParams[0] == "VRDEAUTHDISCONNECT" && aAuthParams.size() == 3)
13887 {
13888 enum VRDEAuthDisconnectParams
13889 {
13890 parmUuid = 1,
13891 parmClientId
13892 };
13893
13894 Guid uuid(aAuthParams[parmUuid]);
13895 uint32_t u32ClientId = 0;
13896 AuthLibDisconnect(&mAuthLibCtx, uuid.raw(), u32ClientId);
13897 }
13898 else
13899 {
13900 hr = E_INVALIDARG;
13901 }
13902
13903 return hr;
13904}
13905
13906// public methods only for internal purposes
13907/////////////////////////////////////////////////////////////////////////////
13908
13909#ifndef VBOX_WITH_GENERIC_SESSION_WATCHER
13910/**
13911 * Called from the client watcher thread to check for expected or unexpected
13912 * death of the client process that has a direct session to this machine.
13913 *
13914 * On Win32 and on OS/2, this method is called only when we've got the
13915 * mutex (i.e. the client has either died or terminated normally) so it always
13916 * returns @c true (the client is terminated, the session machine is
13917 * uninitialized).
13918 *
13919 * On other platforms, the method returns @c true if the client process has
13920 * terminated normally or abnormally and the session machine was uninitialized,
13921 * and @c false if the client process is still alive.
13922 *
13923 * @note Locks this object for writing.
13924 */
13925bool SessionMachine::i_checkForDeath()
13926{
13927 Uninit::Reason reason;
13928 bool terminated = false;
13929
13930 /* Enclose autoCaller with a block because calling uninit() from under it
13931 * will deadlock. */
13932 {
13933 AutoCaller autoCaller(this);
13934 if (!autoCaller.isOk())
13935 {
13936 /* return true if not ready, to cause the client watcher to exclude
13937 * the corresponding session from watching */
13938 LogFlowThisFunc(("Already uninitialized!\n"));
13939 return true;
13940 }
13941
13942 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
13943
13944 /* Determine the reason of death: if the session state is Closing here,
13945 * everything is fine. Otherwise it means that the client did not call
13946 * OnSessionEnd() before it released the IPC semaphore. This may happen
13947 * either because the client process has abnormally terminated, or
13948 * because it simply forgot to call ISession::Close() before exiting. We
13949 * threat the latter also as an abnormal termination (see
13950 * Session::uninit() for details). */
13951 reason = mData->mSession.mState == SessionState_Unlocking ?
13952 Uninit::Normal :
13953 Uninit::Abnormal;
13954
13955 if (mClientToken)
13956 terminated = mClientToken->release();
13957 } /* AutoCaller block */
13958
13959 if (terminated)
13960 uninit(reason);
13961
13962 return terminated;
13963}
13964
13965void SessionMachine::i_getTokenId(Utf8Str &strTokenId)
13966{
13967 LogFlowThisFunc(("\n"));
13968
13969 strTokenId.setNull();
13970
13971 AutoCaller autoCaller(this);
13972 AssertComRCReturnVoid(autoCaller.rc());
13973
13974 Assert(mClientToken);
13975 if (mClientToken)
13976 mClientToken->getId(strTokenId);
13977}
13978#else /* VBOX_WITH_GENERIC_SESSION_WATCHER */
13979IToken *SessionMachine::i_getToken()
13980{
13981 LogFlowThisFunc(("\n"));
13982
13983 AutoCaller autoCaller(this);
13984 AssertComRCReturn(autoCaller.rc(), NULL);
13985
13986 Assert(mClientToken);
13987 if (mClientToken)
13988 return mClientToken->getToken();
13989 else
13990 return NULL;
13991}
13992#endif /* VBOX_WITH_GENERIC_SESSION_WATCHER */
13993
13994Machine::ClientToken *SessionMachine::i_getClientToken()
13995{
13996 LogFlowThisFunc(("\n"));
13997
13998 AutoCaller autoCaller(this);
13999 AssertComRCReturn(autoCaller.rc(), NULL);
14000
14001 return mClientToken;
14002}
14003
14004
14005/**
14006 * @note Locks this object for reading.
14007 */
14008HRESULT SessionMachine::i_onNetworkAdapterChange(INetworkAdapter *networkAdapter, BOOL changeAdapter)
14009{
14010 LogFlowThisFunc(("\n"));
14011
14012 AutoCaller autoCaller(this);
14013 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
14014
14015 ComPtr<IInternalSessionControl> directControl;
14016 {
14017 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
14018 if (mData->mSession.mLockType == LockType_VM)
14019 directControl = mData->mSession.mDirectControl;
14020 }
14021
14022 /* ignore notifications sent after #OnSessionEnd() is called */
14023 if (!directControl)
14024 return S_OK;
14025
14026 return directControl->OnNetworkAdapterChange(networkAdapter, changeAdapter);
14027}
14028
14029/**
14030 * @note Locks this object for reading.
14031 */
14032HRESULT SessionMachine::i_onNATRedirectRuleChange(ULONG ulSlot, BOOL aNatRuleRemove, IN_BSTR aRuleName,
14033 NATProtocol_T aProto, IN_BSTR aHostIp, LONG aHostPort,
14034 IN_BSTR aGuestIp, LONG aGuestPort)
14035{
14036 LogFlowThisFunc(("\n"));
14037
14038 AutoCaller autoCaller(this);
14039 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
14040
14041 ComPtr<IInternalSessionControl> directControl;
14042 {
14043 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
14044 if (mData->mSession.mLockType == LockType_VM)
14045 directControl = mData->mSession.mDirectControl;
14046 }
14047
14048 /* ignore notifications sent after #OnSessionEnd() is called */
14049 if (!directControl)
14050 return S_OK;
14051 /*
14052 * instead acting like callback we ask IVirtualBox deliver corresponding event
14053 */
14054
14055 mParent->i_onNatRedirectChange(i_getId(), ulSlot, RT_BOOL(aNatRuleRemove), aRuleName, aProto, aHostIp,
14056 (uint16_t)aHostPort, aGuestIp, (uint16_t)aGuestPort);
14057 return S_OK;
14058}
14059
14060/**
14061 * @note Locks this object for reading.
14062 */
14063HRESULT SessionMachine::i_onSerialPortChange(ISerialPort *serialPort)
14064{
14065 LogFlowThisFunc(("\n"));
14066
14067 AutoCaller autoCaller(this);
14068 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
14069
14070 ComPtr<IInternalSessionControl> directControl;
14071 {
14072 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
14073 if (mData->mSession.mLockType == LockType_VM)
14074 directControl = mData->mSession.mDirectControl;
14075 }
14076
14077 /* ignore notifications sent after #OnSessionEnd() is called */
14078 if (!directControl)
14079 return S_OK;
14080
14081 return directControl->OnSerialPortChange(serialPort);
14082}
14083
14084/**
14085 * @note Locks this object for reading.
14086 */
14087HRESULT SessionMachine::i_onParallelPortChange(IParallelPort *parallelPort)
14088{
14089 LogFlowThisFunc(("\n"));
14090
14091 AutoCaller autoCaller(this);
14092 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
14093
14094 ComPtr<IInternalSessionControl> directControl;
14095 {
14096 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
14097 if (mData->mSession.mLockType == LockType_VM)
14098 directControl = mData->mSession.mDirectControl;
14099 }
14100
14101 /* ignore notifications sent after #OnSessionEnd() is called */
14102 if (!directControl)
14103 return S_OK;
14104
14105 return directControl->OnParallelPortChange(parallelPort);
14106}
14107
14108/**
14109 * @note Locks this object for reading.
14110 */
14111HRESULT SessionMachine::i_onStorageControllerChange()
14112{
14113 LogFlowThisFunc(("\n"));
14114
14115 AutoCaller autoCaller(this);
14116 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
14117
14118 ComPtr<IInternalSessionControl> directControl;
14119 {
14120 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
14121 if (mData->mSession.mLockType == LockType_VM)
14122 directControl = mData->mSession.mDirectControl;
14123 }
14124
14125 /* ignore notifications sent after #OnSessionEnd() is called */
14126 if (!directControl)
14127 return S_OK;
14128
14129 return directControl->OnStorageControllerChange();
14130}
14131
14132/**
14133 * @note Locks this object for reading.
14134 */
14135HRESULT SessionMachine::i_onMediumChange(IMediumAttachment *aAttachment, BOOL aForce)
14136{
14137 LogFlowThisFunc(("\n"));
14138
14139 AutoCaller autoCaller(this);
14140 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
14141
14142 ComPtr<IInternalSessionControl> directControl;
14143 {
14144 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
14145 if (mData->mSession.mLockType == LockType_VM)
14146 directControl = mData->mSession.mDirectControl;
14147 }
14148
14149 /* ignore notifications sent after #OnSessionEnd() is called */
14150 if (!directControl)
14151 return S_OK;
14152
14153 return directControl->OnMediumChange(aAttachment, aForce);
14154}
14155
14156/**
14157 * @note Locks this object for reading.
14158 */
14159HRESULT SessionMachine::i_onCPUChange(ULONG aCPU, BOOL aRemove)
14160{
14161 LogFlowThisFunc(("\n"));
14162
14163 AutoCaller autoCaller(this);
14164 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
14165
14166 ComPtr<IInternalSessionControl> directControl;
14167 {
14168 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
14169 if (mData->mSession.mLockType == LockType_VM)
14170 directControl = mData->mSession.mDirectControl;
14171 }
14172
14173 /* ignore notifications sent after #OnSessionEnd() is called */
14174 if (!directControl)
14175 return S_OK;
14176
14177 return directControl->OnCPUChange(aCPU, aRemove);
14178}
14179
14180HRESULT SessionMachine::i_onCPUExecutionCapChange(ULONG aExecutionCap)
14181{
14182 LogFlowThisFunc(("\n"));
14183
14184 AutoCaller autoCaller(this);
14185 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
14186
14187 ComPtr<IInternalSessionControl> directControl;
14188 {
14189 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
14190 if (mData->mSession.mLockType == LockType_VM)
14191 directControl = mData->mSession.mDirectControl;
14192 }
14193
14194 /* ignore notifications sent after #OnSessionEnd() is called */
14195 if (!directControl)
14196 return S_OK;
14197
14198 return directControl->OnCPUExecutionCapChange(aExecutionCap);
14199}
14200
14201/**
14202 * @note Locks this object for reading.
14203 */
14204HRESULT SessionMachine::i_onVRDEServerChange(BOOL aRestart)
14205{
14206 LogFlowThisFunc(("\n"));
14207
14208 AutoCaller autoCaller(this);
14209 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
14210
14211 ComPtr<IInternalSessionControl> directControl;
14212 {
14213 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
14214 if (mData->mSession.mLockType == LockType_VM)
14215 directControl = mData->mSession.mDirectControl;
14216 }
14217
14218 /* ignore notifications sent after #OnSessionEnd() is called */
14219 if (!directControl)
14220 return S_OK;
14221
14222 return directControl->OnVRDEServerChange(aRestart);
14223}
14224
14225/**
14226 * @note Locks this object for reading.
14227 */
14228HRESULT SessionMachine::i_onVideoCaptureChange()
14229{
14230 LogFlowThisFunc(("\n"));
14231
14232 AutoCaller autoCaller(this);
14233 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
14234
14235 ComPtr<IInternalSessionControl> directControl;
14236 {
14237 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
14238 if (mData->mSession.mLockType == LockType_VM)
14239 directControl = mData->mSession.mDirectControl;
14240 }
14241
14242 /* ignore notifications sent after #OnSessionEnd() is called */
14243 if (!directControl)
14244 return S_OK;
14245
14246 return directControl->OnVideoCaptureChange();
14247}
14248
14249/**
14250 * @note Locks this object for reading.
14251 */
14252HRESULT SessionMachine::i_onUSBControllerChange()
14253{
14254 LogFlowThisFunc(("\n"));
14255
14256 AutoCaller autoCaller(this);
14257 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
14258
14259 ComPtr<IInternalSessionControl> directControl;
14260 {
14261 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
14262 if (mData->mSession.mLockType == LockType_VM)
14263 directControl = mData->mSession.mDirectControl;
14264 }
14265
14266 /* ignore notifications sent after #OnSessionEnd() is called */
14267 if (!directControl)
14268 return S_OK;
14269
14270 return directControl->OnUSBControllerChange();
14271}
14272
14273/**
14274 * @note Locks this object for reading.
14275 */
14276HRESULT SessionMachine::i_onSharedFolderChange()
14277{
14278 LogFlowThisFunc(("\n"));
14279
14280 AutoCaller autoCaller(this);
14281 AssertComRCReturnRC(autoCaller.rc());
14282
14283 ComPtr<IInternalSessionControl> directControl;
14284 {
14285 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
14286 if (mData->mSession.mLockType == LockType_VM)
14287 directControl = mData->mSession.mDirectControl;
14288 }
14289
14290 /* ignore notifications sent after #OnSessionEnd() is called */
14291 if (!directControl)
14292 return S_OK;
14293
14294 return directControl->OnSharedFolderChange(FALSE /* aGlobal */);
14295}
14296
14297/**
14298 * @note Locks this object for reading.
14299 */
14300HRESULT SessionMachine::i_onClipboardModeChange(ClipboardMode_T aClipboardMode)
14301{
14302 LogFlowThisFunc(("\n"));
14303
14304 AutoCaller autoCaller(this);
14305 AssertComRCReturnRC(autoCaller.rc());
14306
14307 ComPtr<IInternalSessionControl> directControl;
14308 {
14309 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
14310 if (mData->mSession.mLockType == LockType_VM)
14311 directControl = mData->mSession.mDirectControl;
14312 }
14313
14314 /* ignore notifications sent after #OnSessionEnd() is called */
14315 if (!directControl)
14316 return S_OK;
14317
14318 return directControl->OnClipboardModeChange(aClipboardMode);
14319}
14320
14321/**
14322 * @note Locks this object for reading.
14323 */
14324HRESULT SessionMachine::i_onDnDModeChange(DnDMode_T aDnDMode)
14325{
14326 LogFlowThisFunc(("\n"));
14327
14328 AutoCaller autoCaller(this);
14329 AssertComRCReturnRC(autoCaller.rc());
14330
14331 ComPtr<IInternalSessionControl> directControl;
14332 {
14333 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
14334 if (mData->mSession.mLockType == LockType_VM)
14335 directControl = mData->mSession.mDirectControl;
14336 }
14337
14338 /* ignore notifications sent after #OnSessionEnd() is called */
14339 if (!directControl)
14340 return S_OK;
14341
14342 return directControl->OnDnDModeChange(aDnDMode);
14343}
14344
14345/**
14346 * @note Locks this object for reading.
14347 */
14348HRESULT SessionMachine::i_onBandwidthGroupChange(IBandwidthGroup *aBandwidthGroup)
14349{
14350 LogFlowThisFunc(("\n"));
14351
14352 AutoCaller autoCaller(this);
14353 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
14354
14355 ComPtr<IInternalSessionControl> directControl;
14356 {
14357 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
14358 if (mData->mSession.mLockType == LockType_VM)
14359 directControl = mData->mSession.mDirectControl;
14360 }
14361
14362 /* ignore notifications sent after #OnSessionEnd() is called */
14363 if (!directControl)
14364 return S_OK;
14365
14366 return directControl->OnBandwidthGroupChange(aBandwidthGroup);
14367}
14368
14369/**
14370 * @note Locks this object for reading.
14371 */
14372HRESULT SessionMachine::i_onStorageDeviceChange(IMediumAttachment *aAttachment, BOOL aRemove, BOOL aSilent)
14373{
14374 LogFlowThisFunc(("\n"));
14375
14376 AutoCaller autoCaller(this);
14377 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
14378
14379 ComPtr<IInternalSessionControl> directControl;
14380 {
14381 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
14382 if (mData->mSession.mLockType == LockType_VM)
14383 directControl = mData->mSession.mDirectControl;
14384 }
14385
14386 /* ignore notifications sent after #OnSessionEnd() is called */
14387 if (!directControl)
14388 return S_OK;
14389
14390 return directControl->OnStorageDeviceChange(aAttachment, aRemove, aSilent);
14391}
14392
14393/**
14394 * Returns @c true if this machine's USB controller reports it has a matching
14395 * filter for the given USB device and @c false otherwise.
14396 *
14397 * @note locks this object for reading.
14398 */
14399bool SessionMachine::i_hasMatchingUSBFilter(const ComObjPtr<HostUSBDevice> &aDevice, ULONG *aMaskedIfs)
14400{
14401 AutoCaller autoCaller(this);
14402 /* silently return if not ready -- this method may be called after the
14403 * direct machine session has been called */
14404 if (!autoCaller.isOk())
14405 return false;
14406
14407#ifdef VBOX_WITH_USB
14408 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
14409
14410 switch (mData->mMachineState)
14411 {
14412 case MachineState_Starting:
14413 case MachineState_Restoring:
14414 case MachineState_TeleportingIn:
14415 case MachineState_Paused:
14416 case MachineState_Running:
14417 /** @todo Live Migration: snapshoting & teleporting. Need to fend things of
14418 * elsewhere... */
14419 alock.release();
14420 return mUSBDeviceFilters->i_hasMatchingFilter(aDevice, aMaskedIfs);
14421 default: break;
14422 }
14423#else
14424 NOREF(aDevice);
14425 NOREF(aMaskedIfs);
14426#endif
14427 return false;
14428}
14429
14430/**
14431 * @note The calls shall hold no locks. Will temporarily lock this object for reading.
14432 */
14433HRESULT SessionMachine::i_onUSBDeviceAttach(IUSBDevice *aDevice,
14434 IVirtualBoxErrorInfo *aError,
14435 ULONG aMaskedIfs,
14436 const com::Utf8Str &aCaptureFilename)
14437{
14438 LogFlowThisFunc(("\n"));
14439
14440 AutoCaller autoCaller(this);
14441
14442 /* This notification may happen after the machine object has been
14443 * uninitialized (the session was closed), so don't assert. */
14444 if (FAILED(autoCaller.rc())) return autoCaller.rc();
14445
14446 ComPtr<IInternalSessionControl> directControl;
14447 {
14448 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
14449 if (mData->mSession.mLockType == LockType_VM)
14450 directControl = mData->mSession.mDirectControl;
14451 }
14452
14453 /* fail on notifications sent after #OnSessionEnd() is called, it is
14454 * expected by the caller */
14455 if (!directControl)
14456 return E_FAIL;
14457
14458 /* No locks should be held at this point. */
14459 AssertMsg(RTLockValidatorWriteLockGetCount(RTThreadSelf()) == 0, ("%d\n", RTLockValidatorWriteLockGetCount(RTThreadSelf())));
14460 AssertMsg(RTLockValidatorReadLockGetCount(RTThreadSelf()) == 0, ("%d\n", RTLockValidatorReadLockGetCount(RTThreadSelf())));
14461
14462 return directControl->OnUSBDeviceAttach(aDevice, aError, aMaskedIfs, Bstr(aCaptureFilename).raw());
14463}
14464
14465/**
14466 * @note The calls shall hold no locks. Will temporarily lock this object for reading.
14467 */
14468HRESULT SessionMachine::i_onUSBDeviceDetach(IN_BSTR aId,
14469 IVirtualBoxErrorInfo *aError)
14470{
14471 LogFlowThisFunc(("\n"));
14472
14473 AutoCaller autoCaller(this);
14474
14475 /* This notification may happen after the machine object has been
14476 * uninitialized (the session was closed), so don't assert. */
14477 if (FAILED(autoCaller.rc())) return autoCaller.rc();
14478
14479 ComPtr<IInternalSessionControl> directControl;
14480 {
14481 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
14482 if (mData->mSession.mLockType == LockType_VM)
14483 directControl = mData->mSession.mDirectControl;
14484 }
14485
14486 /* fail on notifications sent after #OnSessionEnd() is called, it is
14487 * expected by the caller */
14488 if (!directControl)
14489 return E_FAIL;
14490
14491 /* No locks should be held at this point. */
14492 AssertMsg(RTLockValidatorWriteLockGetCount(RTThreadSelf()) == 0, ("%d\n", RTLockValidatorWriteLockGetCount(RTThreadSelf())));
14493 AssertMsg(RTLockValidatorReadLockGetCount(RTThreadSelf()) == 0, ("%d\n", RTLockValidatorReadLockGetCount(RTThreadSelf())));
14494
14495 return directControl->OnUSBDeviceDetach(aId, aError);
14496}
14497
14498// protected methods
14499/////////////////////////////////////////////////////////////////////////////
14500
14501/**
14502 * Deletes the given file if it is no longer in use by either the current machine state
14503 * (if the machine is "saved") or any of the machine's snapshots.
14504 *
14505 * Note: This checks mSSData->strStateFilePath, which is shared by the Machine and SessionMachine
14506 * but is different for each SnapshotMachine. When calling this, the order of calling this
14507 * function on the one hand and changing that variable OR the snapshots tree on the other hand
14508 * is therefore critical. I know, it's all rather messy.
14509 *
14510 * @param strStateFile
14511 * @param pSnapshotToIgnore Passed to Snapshot::sharesSavedStateFile(); this snapshot is ignored in
14512 * the test for whether the saved state file is in use.
14513 */
14514void SessionMachine::i_releaseSavedStateFile(const Utf8Str &strStateFile,
14515 Snapshot *pSnapshotToIgnore)
14516{
14517 // it is safe to delete this saved state file if it is not currently in use by the machine ...
14518 if ( (strStateFile.isNotEmpty())
14519 && (strStateFile != mSSData->strStateFilePath) // session machine's saved state
14520 )
14521 // ... and it must also not be shared with other snapshots
14522 if ( !mData->mFirstSnapshot
14523 || !mData->mFirstSnapshot->i_sharesSavedStateFile(strStateFile, pSnapshotToIgnore)
14524 // this checks the SnapshotMachine's state file paths
14525 )
14526 RTFileDelete(strStateFile.c_str());
14527}
14528
14529/**
14530 * Locks the attached media.
14531 *
14532 * All attached hard disks are locked for writing and DVD/floppy are locked for
14533 * reading. Parents of attached hard disks (if any) are locked for reading.
14534 *
14535 * This method also performs accessibility check of all media it locks: if some
14536 * media is inaccessible, the method will return a failure and a bunch of
14537 * extended error info objects per each inaccessible medium.
14538 *
14539 * Note that this method is atomic: if it returns a success, all media are
14540 * locked as described above; on failure no media is locked at all (all
14541 * succeeded individual locks will be undone).
14542 *
14543 * The caller is responsible for doing the necessary state sanity checks.
14544 *
14545 * The locks made by this method must be undone by calling #unlockMedia() when
14546 * no more needed.
14547 */
14548HRESULT SessionMachine::i_lockMedia()
14549{
14550 AutoCaller autoCaller(this);
14551 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
14552
14553 AutoMultiWriteLock2 alock(this->lockHandle(),
14554 &mParent->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
14555
14556 /* bail out if trying to lock things with already set up locking */
14557 AssertReturn(mData->mSession.mLockedMedia.IsEmpty(), E_FAIL);
14558
14559 MultiResult mrc(S_OK);
14560
14561 /* Collect locking information for all medium objects attached to the VM. */
14562 for (MediumAttachmentList::const_iterator
14563 it = mMediumAttachments->begin();
14564 it != mMediumAttachments->end();
14565 ++it)
14566 {
14567 MediumAttachment *pAtt = *it;
14568 DeviceType_T devType = pAtt->i_getType();
14569 Medium *pMedium = pAtt->i_getMedium();
14570
14571 MediumLockList *pMediumLockList(new MediumLockList());
14572 // There can be attachments without a medium (floppy/dvd), and thus
14573 // it's impossible to create a medium lock list. It still makes sense
14574 // to have the empty medium lock list in the map in case a medium is
14575 // attached later.
14576 if (pMedium != NULL)
14577 {
14578 MediumType_T mediumType = pMedium->i_getType();
14579 bool fIsReadOnlyLock = mediumType == MediumType_Readonly
14580 || mediumType == MediumType_Shareable;
14581 bool fIsVitalImage = (devType == DeviceType_HardDisk);
14582
14583 alock.release();
14584 mrc = pMedium->i_createMediumLockList(fIsVitalImage /* fFailIfInaccessible */,
14585 !fIsReadOnlyLock ? pMedium : NULL /* pToLockWrite */,
14586 false /* fMediumLockWriteAll */,
14587 NULL,
14588 *pMediumLockList);
14589 alock.acquire();
14590 if (FAILED(mrc))
14591 {
14592 delete pMediumLockList;
14593 mData->mSession.mLockedMedia.Clear();
14594 break;
14595 }
14596 }
14597
14598 HRESULT rc = mData->mSession.mLockedMedia.Insert(pAtt, pMediumLockList);
14599 if (FAILED(rc))
14600 {
14601 mData->mSession.mLockedMedia.Clear();
14602 mrc = setError(rc,
14603 tr("Collecting locking information for all attached media failed"));
14604 break;
14605 }
14606 }
14607
14608 if (SUCCEEDED(mrc))
14609 {
14610 /* Now lock all media. If this fails, nothing is locked. */
14611 alock.release();
14612 HRESULT rc = mData->mSession.mLockedMedia.Lock();
14613 alock.acquire();
14614 if (FAILED(rc))
14615 {
14616 mrc = setError(rc,
14617 tr("Locking of attached media failed. A possible reason is that one of the media is attached to a running VM"));
14618 }
14619 }
14620
14621 return mrc;
14622}
14623
14624/**
14625 * Undoes the locks made by by #lockMedia().
14626 */
14627HRESULT SessionMachine::i_unlockMedia()
14628{
14629 AutoCaller autoCaller(this);
14630 AssertComRCReturn(autoCaller.rc(),autoCaller.rc());
14631
14632 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
14633
14634 /* we may be holding important error info on the current thread;
14635 * preserve it */
14636 ErrorInfoKeeper eik;
14637
14638 HRESULT rc = mData->mSession.mLockedMedia.Clear();
14639 AssertComRC(rc);
14640 return rc;
14641}
14642
14643/**
14644 * Helper to change the machine state (reimplementation).
14645 *
14646 * @note Locks this object for writing.
14647 * @note This method must not call i_saveSettings or SaveSettings, otherwise
14648 * it can cause crashes in random places due to unexpectedly committing
14649 * the current settings. The caller is responsible for that. The call
14650 * to saveStateSettings is fine, because this method does not commit.
14651 */
14652HRESULT SessionMachine::i_setMachineState(MachineState_T aMachineState)
14653{
14654 LogFlowThisFuncEnter();
14655 LogFlowThisFunc(("aMachineState=%s\n", Global::stringifyMachineState(aMachineState) ));
14656
14657 AutoCaller autoCaller(this);
14658 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
14659
14660 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
14661
14662 MachineState_T oldMachineState = mData->mMachineState;
14663
14664 AssertMsgReturn(oldMachineState != aMachineState,
14665 ("oldMachineState=%s, aMachineState=%s\n",
14666 Global::stringifyMachineState(oldMachineState), Global::stringifyMachineState(aMachineState)),
14667 E_FAIL);
14668
14669 HRESULT rc = S_OK;
14670
14671 int stsFlags = 0;
14672 bool deleteSavedState = false;
14673
14674 /* detect some state transitions */
14675
14676 if ( ( oldMachineState == MachineState_Saved
14677 && aMachineState == MachineState_Restoring)
14678 || ( ( oldMachineState == MachineState_PoweredOff
14679 || oldMachineState == MachineState_Teleported
14680 || oldMachineState == MachineState_Aborted
14681 )
14682 && ( aMachineState == MachineState_TeleportingIn
14683 || aMachineState == MachineState_Starting
14684 )
14685 )
14686 )
14687 {
14688 /* The EMT thread is about to start */
14689
14690 /* Nothing to do here for now... */
14691
14692 /// @todo NEWMEDIA don't let mDVDDrive and other children
14693 /// change anything when in the Starting/Restoring state
14694 }
14695 else if ( ( oldMachineState == MachineState_Running
14696 || oldMachineState == MachineState_Paused
14697 || oldMachineState == MachineState_Teleporting
14698 || oldMachineState == MachineState_OnlineSnapshotting
14699 || oldMachineState == MachineState_LiveSnapshotting
14700 || oldMachineState == MachineState_Stuck
14701 || oldMachineState == MachineState_Starting
14702 || oldMachineState == MachineState_Stopping
14703 || oldMachineState == MachineState_Saving
14704 || oldMachineState == MachineState_Restoring
14705 || oldMachineState == MachineState_TeleportingPausedVM
14706 || oldMachineState == MachineState_TeleportingIn
14707 )
14708 && ( aMachineState == MachineState_PoweredOff
14709 || aMachineState == MachineState_Saved
14710 || aMachineState == MachineState_Teleported
14711 || aMachineState == MachineState_Aborted
14712 )
14713 )
14714 {
14715 /* The EMT thread has just stopped, unlock attached media. Note that as
14716 * opposed to locking that is done from Console, we do unlocking here
14717 * because the VM process may have aborted before having a chance to
14718 * properly unlock all media it locked. */
14719
14720 unlockMedia();
14721 }
14722
14723 if (oldMachineState == MachineState_Restoring)
14724 {
14725 if (aMachineState != MachineState_Saved)
14726 {
14727 /*
14728 * delete the saved state file once the machine has finished
14729 * restoring from it (note that Console sets the state from
14730 * Restoring to Saved if the VM couldn't restore successfully,
14731 * to give the user an ability to fix an error and retry --
14732 * we keep the saved state file in this case)
14733 */
14734 deleteSavedState = true;
14735 }
14736 }
14737 else if ( oldMachineState == MachineState_Saved
14738 && ( aMachineState == MachineState_PoweredOff
14739 || aMachineState == MachineState_Aborted
14740 || aMachineState == MachineState_Teleported
14741 )
14742 )
14743 {
14744 /*
14745 * delete the saved state after SessionMachine::ForgetSavedState() is called
14746 * or if the VM process (owning a direct VM session) crashed while the
14747 * VM was Saved
14748 */
14749
14750 /// @todo (dmik)
14751 // Not sure that deleting the saved state file just because of the
14752 // client death before it attempted to restore the VM is a good
14753 // thing. But when it crashes we need to go to the Aborted state
14754 // which cannot have the saved state file associated... The only
14755 // way to fix this is to make the Aborted condition not a VM state
14756 // but a bool flag: i.e., when a crash occurs, set it to true and
14757 // change the state to PoweredOff or Saved depending on the
14758 // saved state presence.
14759
14760 deleteSavedState = true;
14761 mData->mCurrentStateModified = TRUE;
14762 stsFlags |= SaveSTS_CurStateModified;
14763 }
14764
14765 if ( aMachineState == MachineState_Starting
14766 || aMachineState == MachineState_Restoring
14767 || aMachineState == MachineState_TeleportingIn
14768 )
14769 {
14770 /* set the current state modified flag to indicate that the current
14771 * state is no more identical to the state in the
14772 * current snapshot */
14773 if (!mData->mCurrentSnapshot.isNull())
14774 {
14775 mData->mCurrentStateModified = TRUE;
14776 stsFlags |= SaveSTS_CurStateModified;
14777 }
14778 }
14779
14780 if (deleteSavedState)
14781 {
14782 if (mRemoveSavedState)
14783 {
14784 Assert(!mSSData->strStateFilePath.isEmpty());
14785
14786 // it is safe to delete the saved state file if ...
14787 if ( !mData->mFirstSnapshot // ... we have no snapshots or
14788 || !mData->mFirstSnapshot->i_sharesSavedStateFile(mSSData->strStateFilePath, NULL /* pSnapshotToIgnore */)
14789 // ... none of the snapshots share the saved state file
14790 )
14791 RTFileDelete(mSSData->strStateFilePath.c_str());
14792 }
14793
14794 mSSData->strStateFilePath.setNull();
14795 stsFlags |= SaveSTS_StateFilePath;
14796 }
14797
14798 /* redirect to the underlying peer machine */
14799 mPeer->i_setMachineState(aMachineState);
14800
14801 if ( oldMachineState != MachineState_RestoringSnapshot
14802 && ( aMachineState == MachineState_PoweredOff
14803 || aMachineState == MachineState_Teleported
14804 || aMachineState == MachineState_Aborted
14805 || aMachineState == MachineState_Saved))
14806 {
14807 /* the machine has stopped execution
14808 * (or the saved state file was adopted) */
14809 stsFlags |= SaveSTS_StateTimeStamp;
14810 }
14811
14812 if ( ( oldMachineState == MachineState_PoweredOff
14813 || oldMachineState == MachineState_Aborted
14814 || oldMachineState == MachineState_Teleported
14815 )
14816 && aMachineState == MachineState_Saved)
14817 {
14818 /* the saved state file was adopted */
14819 Assert(!mSSData->strStateFilePath.isEmpty());
14820 stsFlags |= SaveSTS_StateFilePath;
14821 }
14822
14823#ifdef VBOX_WITH_GUEST_PROPS
14824 if ( aMachineState == MachineState_PoweredOff
14825 || aMachineState == MachineState_Aborted
14826 || aMachineState == MachineState_Teleported)
14827 {
14828 /* Make sure any transient guest properties get removed from the
14829 * property store on shutdown. */
14830 BOOL fNeedsSaving = mData->mGuestPropertiesModified;
14831
14832 /* remove it from the settings representation */
14833 settings::GuestPropertiesList &llGuestProperties = mData->pMachineConfigFile->hardwareMachine.llGuestProperties;
14834 for (settings::GuestPropertiesList::iterator
14835 it = llGuestProperties.begin();
14836 it != llGuestProperties.end();
14837 /*nothing*/)
14838 {
14839 const settings::GuestProperty &prop = *it;
14840 if ( prop.strFlags.contains("TRANSRESET", Utf8Str::CaseInsensitive)
14841 || prop.strFlags.contains("TRANSIENT", Utf8Str::CaseInsensitive))
14842 {
14843 it = llGuestProperties.erase(it);
14844 fNeedsSaving = true;
14845 }
14846 else
14847 {
14848 ++it;
14849 }
14850 }
14851
14852 /* Additionally remove it from the HWData representation. Required to
14853 * keep everything in sync, as this is what the API keeps using. */
14854 HWData::GuestPropertyMap &llHWGuestProperties = mHWData->mGuestProperties;
14855 for (HWData::GuestPropertyMap::iterator
14856 it = llHWGuestProperties.begin();
14857 it != llHWGuestProperties.end();
14858 /*nothing*/)
14859 {
14860 uint32_t fFlags = it->second.mFlags;
14861 if ( fFlags & guestProp::TRANSIENT
14862 || fFlags & guestProp::TRANSRESET)
14863 {
14864 /* iterator where we need to continue after the erase call
14865 * (C++03 is a fact still, and it doesn't return the iterator
14866 * which would allow continuing) */
14867 HWData::GuestPropertyMap::iterator it2 = it;
14868 ++it2;
14869 llHWGuestProperties.erase(it);
14870 it = it2;
14871 fNeedsSaving = true;
14872 }
14873 else
14874 {
14875 ++it;
14876 }
14877 }
14878
14879 if (fNeedsSaving)
14880 {
14881 mData->mCurrentStateModified = TRUE;
14882 stsFlags |= SaveSTS_CurStateModified;
14883 }
14884 }
14885#endif /* VBOX_WITH_GUEST_PROPS */
14886
14887 rc = i_saveStateSettings(stsFlags);
14888
14889 if ( ( oldMachineState != MachineState_PoweredOff
14890 && oldMachineState != MachineState_Aborted
14891 && oldMachineState != MachineState_Teleported
14892 )
14893 && ( aMachineState == MachineState_PoweredOff
14894 || aMachineState == MachineState_Aborted
14895 || aMachineState == MachineState_Teleported
14896 )
14897 )
14898 {
14899 /* we've been shut down for any reason */
14900 /* no special action so far */
14901 }
14902
14903 LogFlowThisFunc(("rc=%Rhrc [%s]\n", rc, Global::stringifyMachineState(mData->mMachineState) ));
14904 LogFlowThisFuncLeave();
14905 return rc;
14906}
14907
14908/**
14909 * Sends the current machine state value to the VM process.
14910 *
14911 * @note Locks this object for reading, then calls a client process.
14912 */
14913HRESULT SessionMachine::i_updateMachineStateOnClient()
14914{
14915 AutoCaller autoCaller(this);
14916 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
14917
14918 ComPtr<IInternalSessionControl> directControl;
14919 {
14920 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
14921 AssertReturn(!!mData, E_FAIL);
14922 if (mData->mSession.mLockType == LockType_VM)
14923 directControl = mData->mSession.mDirectControl;
14924
14925 /* directControl may be already set to NULL here in #OnSessionEnd()
14926 * called too early by the direct session process while there is still
14927 * some operation (like deleting the snapshot) in progress. The client
14928 * process in this case is waiting inside Session::close() for the
14929 * "end session" process object to complete, while #uninit() called by
14930 * #i_checkForDeath() on the Watcher thread is waiting for the pending
14931 * operation to complete. For now, we accept this inconsistent behavior
14932 * and simply do nothing here. */
14933
14934 if (mData->mSession.mState == SessionState_Unlocking)
14935 return S_OK;
14936 }
14937
14938 /* ignore notifications sent after #OnSessionEnd() is called */
14939 if (!directControl)
14940 return S_OK;
14941
14942 return directControl->UpdateMachineState(mData->mMachineState);
14943}
14944
14945
14946/*static*/
14947HRESULT Machine::i_setErrorStatic(HRESULT aResultCode, const char *pcszMsg, ...)
14948{
14949 va_list args;
14950 va_start(args, pcszMsg);
14951 HRESULT rc = setErrorInternal(aResultCode,
14952 getStaticClassIID(),
14953 getStaticComponentName(),
14954 Utf8Str(pcszMsg, args),
14955 false /* aWarning */,
14956 true /* aLogIt */);
14957 va_end(args);
14958 return rc;
14959}
14960
14961
14962HRESULT Machine::updateState(MachineState_T aState)
14963{
14964 NOREF(aState);
14965 ReturnComNotImplemented();
14966}
14967
14968HRESULT Machine::beginPowerUp(const ComPtr<IProgress> &aProgress)
14969{
14970 NOREF(aProgress);
14971 ReturnComNotImplemented();
14972}
14973
14974HRESULT Machine::endPowerUp(LONG aResult)
14975{
14976 NOREF(aResult);
14977 ReturnComNotImplemented();
14978}
14979
14980HRESULT Machine::beginPoweringDown(ComPtr<IProgress> &aProgress)
14981{
14982 NOREF(aProgress);
14983 ReturnComNotImplemented();
14984}
14985
14986HRESULT Machine::endPoweringDown(LONG aResult,
14987 const com::Utf8Str &aErrMsg)
14988{
14989 NOREF(aResult);
14990 NOREF(aErrMsg);
14991 ReturnComNotImplemented();
14992}
14993
14994HRESULT Machine::runUSBDeviceFilters(const ComPtr<IUSBDevice> &aDevice,
14995 BOOL *aMatched,
14996 ULONG *aMaskedInterfaces)
14997{
14998 NOREF(aDevice);
14999 NOREF(aMatched);
15000 NOREF(aMaskedInterfaces);
15001 ReturnComNotImplemented();
15002
15003}
15004
15005HRESULT Machine::captureUSBDevice(const com::Guid &aId, const com::Utf8Str &aCaptureFilename)
15006{
15007 NOREF(aId); NOREF(aCaptureFilename);
15008 ReturnComNotImplemented();
15009}
15010
15011HRESULT Machine::detachUSBDevice(const com::Guid &aId,
15012 BOOL aDone)
15013{
15014 NOREF(aId);
15015 NOREF(aDone);
15016 ReturnComNotImplemented();
15017}
15018
15019HRESULT Machine::autoCaptureUSBDevices()
15020{
15021 ReturnComNotImplemented();
15022}
15023
15024HRESULT Machine::detachAllUSBDevices(BOOL aDone)
15025{
15026 NOREF(aDone);
15027 ReturnComNotImplemented();
15028}
15029
15030HRESULT Machine::onSessionEnd(const ComPtr<ISession> &aSession,
15031 ComPtr<IProgress> &aProgress)
15032{
15033 NOREF(aSession);
15034 NOREF(aProgress);
15035 ReturnComNotImplemented();
15036}
15037
15038HRESULT Machine::finishOnlineMergeMedium()
15039{
15040 ReturnComNotImplemented();
15041}
15042
15043HRESULT Machine::pullGuestProperties(std::vector<com::Utf8Str> &aNames,
15044 std::vector<com::Utf8Str> &aValues,
15045 std::vector<LONG64> &aTimestamps,
15046 std::vector<com::Utf8Str> &aFlags)
15047{
15048 NOREF(aNames);
15049 NOREF(aValues);
15050 NOREF(aTimestamps);
15051 NOREF(aFlags);
15052 ReturnComNotImplemented();
15053}
15054
15055HRESULT Machine::pushGuestProperty(const com::Utf8Str &aName,
15056 const com::Utf8Str &aValue,
15057 LONG64 aTimestamp,
15058 const com::Utf8Str &aFlags)
15059{
15060 NOREF(aName);
15061 NOREF(aValue);
15062 NOREF(aTimestamp);
15063 NOREF(aFlags);
15064 ReturnComNotImplemented();
15065}
15066
15067HRESULT Machine::lockMedia()
15068{
15069 ReturnComNotImplemented();
15070}
15071
15072HRESULT Machine::unlockMedia()
15073{
15074 ReturnComNotImplemented();
15075}
15076
15077HRESULT Machine::ejectMedium(const ComPtr<IMediumAttachment> &aAttachment,
15078 ComPtr<IMediumAttachment> &aNewAttachment)
15079{
15080 NOREF(aAttachment);
15081 NOREF(aNewAttachment);
15082 ReturnComNotImplemented();
15083}
15084
15085HRESULT Machine::reportVmStatistics(ULONG aValidStats,
15086 ULONG aCpuUser,
15087 ULONG aCpuKernel,
15088 ULONG aCpuIdle,
15089 ULONG aMemTotal,
15090 ULONG aMemFree,
15091 ULONG aMemBalloon,
15092 ULONG aMemShared,
15093 ULONG aMemCache,
15094 ULONG aPagedTotal,
15095 ULONG aMemAllocTotal,
15096 ULONG aMemFreeTotal,
15097 ULONG aMemBalloonTotal,
15098 ULONG aMemSharedTotal,
15099 ULONG aVmNetRx,
15100 ULONG aVmNetTx)
15101{
15102 NOREF(aValidStats);
15103 NOREF(aCpuUser);
15104 NOREF(aCpuKernel);
15105 NOREF(aCpuIdle);
15106 NOREF(aMemTotal);
15107 NOREF(aMemFree);
15108 NOREF(aMemBalloon);
15109 NOREF(aMemShared);
15110 NOREF(aMemCache);
15111 NOREF(aPagedTotal);
15112 NOREF(aMemAllocTotal);
15113 NOREF(aMemFreeTotal);
15114 NOREF(aMemBalloonTotal);
15115 NOREF(aMemSharedTotal);
15116 NOREF(aVmNetRx);
15117 NOREF(aVmNetTx);
15118 ReturnComNotImplemented();
15119}
15120
15121HRESULT Machine::authenticateExternal(const std::vector<com::Utf8Str> &aAuthParams,
15122 com::Utf8Str &aResult)
15123{
15124 NOREF(aAuthParams);
15125 NOREF(aResult);
15126 ReturnComNotImplemented();
15127}
15128
15129HRESULT Machine::applyDefaults(const com::Utf8Str &aFlags)
15130{
15131 NOREF(aFlags);
15132 ReturnComNotImplemented();
15133}
15134
15135/* This isn't handled entirely by the wrapper generator yet. */
15136#ifdef VBOX_WITH_XPCOM
15137NS_DECL_CLASSINFO(SessionMachine)
15138NS_IMPL_THREADSAFE_ISUPPORTS2_CI(SessionMachine, IMachine, IInternalMachineControl)
15139
15140NS_DECL_CLASSINFO(SnapshotMachine)
15141NS_IMPL_THREADSAFE_ISUPPORTS1_CI(SnapshotMachine, IMachine)
15142#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