VirtualBox

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

Last change on this file since 52492 was 52492, checked in by vboxsync, 11 years ago

MachineImpl: warnings

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 498.7 KB
Line 
1/* $Id: MachineImpl.cpp 52492 2014-08-25 11:40:49Z vboxsync $ */
2/** @file
3 * Implementation of IMachine in VBoxSVC.
4 */
5
6/*
7 * Copyright (C) 2004-2014 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.215389.xyz. 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 "GuestImpl.h"
42#include "StorageControllerImpl.h"
43#include "DisplayImpl.h"
44#include "DisplayUtils.h"
45#include "MachineImplCloneVM.h"
46#include "AutostartDb.h"
47#include "SystemPropertiesImpl.h"
48
49// generated header
50#include "VBoxEvents.h"
51
52#ifdef VBOX_WITH_USB
53# include "USBProxyService.h"
54#endif
55
56#include "AutoCaller.h"
57#include "HashedPw.h"
58#include "Performance.h"
59
60#include <iprt/asm.h>
61#include <iprt/path.h>
62#include <iprt/dir.h>
63#include <iprt/env.h>
64#include <iprt/lockvalidator.h>
65#include <iprt/process.h>
66#include <iprt/cpp/utils.h>
67#include <iprt/cpp/xml.h> /* xml::XmlFileWriter::s_psz*Suff. */
68#include <iprt/sha.h>
69#include <iprt/string.h>
70#include <iprt/base64.h>
71
72#include <VBox/com/array.h>
73#include <VBox/com/list.h>
74
75#include <VBox/err.h>
76#include <VBox/param.h>
77#include <VBox/settings.h>
78#include <VBox/vmm/ssm.h>
79
80#ifdef VBOX_WITH_GUEST_PROPS
81# include <VBox/HostServices/GuestPropertySvc.h>
82# include <VBox/com/array.h>
83#endif
84
85#include "VBox/com/MultiResult.h"
86
87#include <algorithm>
88
89#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
90# define HOSTSUFF_EXE ".exe"
91#else /* !RT_OS_WINDOWS */
92# define HOSTSUFF_EXE ""
93#endif /* !RT_OS_WINDOWS */
94
95// defines / prototypes
96/////////////////////////////////////////////////////////////////////////////
97
98/////////////////////////////////////////////////////////////////////////////
99// Machine::Data structure
100/////////////////////////////////////////////////////////////////////////////
101
102Machine::Data::Data()
103{
104 mRegistered = FALSE;
105 pMachineConfigFile = NULL;
106 /* Contains hints on what has changed when the user is using the VM (config
107 * changes, running the VM, ...). This is used to decide if a config needs
108 * to be written to disk. */
109 flModifications = 0;
110 /* VM modification usually also trigger setting the current state to
111 * "Modified". Although this is not always the case. An e.g. is the VM
112 * initialization phase or when snapshot related data is changed. The
113 * actually behavior is controlled by the following flag. */
114 m_fAllowStateModification = false;
115 mAccessible = FALSE;
116 /* mUuid is initialized in Machine::init() */
117
118 mMachineState = MachineState_PoweredOff;
119 RTTimeNow(&mLastStateChange);
120
121 mMachineStateDeps = 0;
122 mMachineStateDepsSem = NIL_RTSEMEVENTMULTI;
123 mMachineStateChangePending = 0;
124
125 mCurrentStateModified = TRUE;
126 mGuestPropertiesModified = FALSE;
127
128 mSession.mPID = NIL_RTPROCESS;
129 mSession.mState = SessionState_Unlocked;
130}
131
132Machine::Data::~Data()
133{
134 if (mMachineStateDepsSem != NIL_RTSEMEVENTMULTI)
135 {
136 RTSemEventMultiDestroy(mMachineStateDepsSem);
137 mMachineStateDepsSem = NIL_RTSEMEVENTMULTI;
138 }
139 if (pMachineConfigFile)
140 {
141 delete pMachineConfigFile;
142 pMachineConfigFile = NULL;
143 }
144}
145
146/////////////////////////////////////////////////////////////////////////////
147// Machine::HWData structure
148/////////////////////////////////////////////////////////////////////////////
149
150Machine::HWData::HWData()
151{
152 /* default values for a newly created machine */
153 mHWVersion = "2"; /** @todo get the default from the schema if that is possible. */
154 mMemorySize = 128;
155 mCPUCount = 1;
156 mCPUHotPlugEnabled = false;
157 mMemoryBalloonSize = 0;
158 mPageFusionEnabled = false;
159 mGraphicsControllerType = GraphicsControllerType_VBoxVGA;
160 mVRAMSize = 8;
161 mAccelerate3DEnabled = false;
162 mAccelerate2DVideoEnabled = false;
163 mMonitorCount = 1;
164 mVideoCaptureWidth = 1024;
165 mVideoCaptureHeight = 768;
166 mVideoCaptureRate = 512;
167 mVideoCaptureFPS = 25;
168 mVideoCaptureMaxTime = 0;
169 mVideoCaptureMaxFileSize = 0;
170 mVideoCaptureEnabled = false;
171 for (unsigned i = 0; i < RT_ELEMENTS(maVideoCaptureScreens); ++i)
172 maVideoCaptureScreens[i] = true;
173
174 mHWVirtExEnabled = true;
175 mHWVirtExNestedPagingEnabled = true;
176#if HC_ARCH_BITS == 64 && !defined(RT_OS_LINUX)
177 mHWVirtExLargePagesEnabled = true;
178#else
179 /* Not supported on 32 bits hosts. */
180 mHWVirtExLargePagesEnabled = false;
181#endif
182 mHWVirtExVPIDEnabled = true;
183 mHWVirtExUXEnabled = true;
184 mHWVirtExForceEnabled = false;
185#if HC_ARCH_BITS == 64 || defined(RT_OS_WINDOWS) || defined(RT_OS_DARWIN)
186 mPAEEnabled = true;
187#else
188 mPAEEnabled = false;
189#endif
190 mLongMode = HC_ARCH_BITS == 64 ? settings::Hardware::LongMode_Enabled : settings::Hardware::LongMode_Disabled;
191 mSyntheticCpu = false;
192 mTripleFaultReset = false;
193 mHPETEnabled = false;
194
195 /* default boot order: floppy - DVD - HDD */
196 mBootOrder[0] = DeviceType_Floppy;
197 mBootOrder[1] = DeviceType_DVD;
198 mBootOrder[2] = DeviceType_HardDisk;
199 for (size_t i = 3; i < RT_ELEMENTS(mBootOrder); ++i)
200 mBootOrder[i] = DeviceType_Null;
201
202 mClipboardMode = ClipboardMode_Disabled;
203 mDnDMode = DnDMode_Disabled;
204 mGuestPropertyNotificationPatterns = "";
205
206 mFirmwareType = FirmwareType_BIOS;
207 mKeyboardHIDType = KeyboardHIDType_PS2Keyboard;
208 mPointingHIDType = PointingHIDType_PS2Mouse;
209 mChipsetType = ChipsetType_PIIX3;
210 mParavirtProvider = ParavirtProvider_Default;
211 mEmulatedUSBCardReaderEnabled = FALSE;
212
213 for (size_t i = 0; i < RT_ELEMENTS(mCPUAttached); ++i)
214 mCPUAttached[i] = false;
215
216 mIOCacheEnabled = true;
217 mIOCacheSize = 5; /* 5MB */
218
219 /* Maximum CPU execution cap by default. */
220 mCpuExecutionCap = 100;
221}
222
223Machine::HWData::~HWData()
224{
225}
226
227/////////////////////////////////////////////////////////////////////////////
228// Machine::HDData structure
229/////////////////////////////////////////////////////////////////////////////
230
231Machine::MediaData::MediaData()
232{
233}
234
235Machine::MediaData::~MediaData()
236{
237}
238
239/////////////////////////////////////////////////////////////////////////////
240// Machine class
241/////////////////////////////////////////////////////////////////////////////
242
243// constructor / destructor
244/////////////////////////////////////////////////////////////////////////////
245
246Machine::Machine() :
247#ifdef VBOX_WITH_RESOURCE_USAGE_API
248 mCollectorGuest(NULL),
249#endif
250 mPeer(NULL),
251 mParent(NULL),
252 mSerialPorts(),
253 mParallelPorts(),
254 uRegistryNeedsSaving(0)
255{}
256
257Machine::~Machine()
258{}
259
260HRESULT Machine::FinalConstruct()
261{
262 LogFlowThisFunc(("\n"));
263 return BaseFinalConstruct();
264}
265
266void Machine::FinalRelease()
267{
268 LogFlowThisFunc(("\n"));
269 uninit();
270 BaseFinalRelease();
271}
272
273/**
274 * Initializes a new machine instance; this init() variant creates a new, empty machine.
275 * This gets called from VirtualBox::CreateMachine().
276 *
277 * @param aParent Associated parent object
278 * @param strConfigFile Local file system path to the VM settings file (can
279 * be relative to the VirtualBox config directory).
280 * @param strName name for the machine
281 * @param llGroups list of groups for the machine
282 * @param aOsType OS Type of this machine or NULL.
283 * @param aId UUID for the new machine.
284 * @param fForceOverwrite Whether to overwrite an existing machine settings file.
285 *
286 * @return Success indicator. if not S_OK, the machine object is invalid
287 */
288HRESULT Machine::init(VirtualBox *aParent,
289 const Utf8Str &strConfigFile,
290 const Utf8Str &strName,
291 const StringsList &llGroups,
292 GuestOSType *aOsType,
293 const Guid &aId,
294 bool fForceOverwrite,
295 bool fDirectoryIncludesUUID)
296{
297 LogFlowThisFuncEnter();
298 LogFlowThisFunc(("(Init_New) aConfigFile='%s'\n", strConfigFile.c_str()));
299
300 /* Enclose the state transition NotReady->InInit->Ready */
301 AutoInitSpan autoInitSpan(this);
302 AssertReturn(autoInitSpan.isOk(), E_FAIL);
303
304 HRESULT rc = initImpl(aParent, strConfigFile);
305 if (FAILED(rc)) return rc;
306
307 rc = i_tryCreateMachineConfigFile(fForceOverwrite);
308 if (FAILED(rc)) return rc;
309
310 if (SUCCEEDED(rc))
311 {
312 // create an empty machine config
313 mData->pMachineConfigFile = new settings::MachineConfigFile(NULL);
314
315 rc = initDataAndChildObjects();
316 }
317
318 if (SUCCEEDED(rc))
319 {
320 // set to true now to cause uninit() to call uninitDataAndChildObjects() on failure
321 mData->mAccessible = TRUE;
322
323 unconst(mData->mUuid) = aId;
324
325 mUserData->s.strName = strName;
326
327 mUserData->s.llGroups = llGroups;
328
329 mUserData->s.fDirectoryIncludesUUID = fDirectoryIncludesUUID;
330 // the "name sync" flag determines whether the machine directory gets renamed along
331 // with the machine file; say so if the settings file name is the same as the
332 // settings file parent directory (machine directory)
333 mUserData->s.fNameSync = i_isInOwnDir();
334
335 // initialize the default snapshots folder
336 rc = COMSETTER(SnapshotFolder)(NULL);
337 AssertComRC(rc);
338
339 if (aOsType)
340 {
341 /* Store OS type */
342 mUserData->s.strOsType = aOsType->i_id();
343
344 /* Apply BIOS defaults */
345 mBIOSSettings->i_applyDefaults(aOsType);
346
347 /* Apply network adapters defaults */
348 for (ULONG slot = 0; slot < mNetworkAdapters.size(); ++slot)
349 mNetworkAdapters[slot]->i_applyDefaults(aOsType);
350
351 /* Apply serial port defaults */
352 for (ULONG slot = 0; slot < RT_ELEMENTS(mSerialPorts); ++slot)
353 mSerialPorts[slot]->i_applyDefaults(aOsType);
354
355 /* Let the OS type select 64-bit ness. */
356 mHWData->mLongMode = aOsType->i_is64Bit()
357 ? settings::Hardware::LongMode_Enabled : settings::Hardware::LongMode_Disabled;
358 }
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 aConfigFile 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 strName Name for the new machine; this overrides what is specified in config and is used
511 * for the settings file as well.
512 * @param config Machine configuration loaded and parsed from XML.
513 *
514 * @return Success indicator. if not S_OK, the machine object is invalid
515 */
516HRESULT Machine::init(VirtualBox *aParent,
517 const Utf8Str &strName,
518 const settings::MachineConfigFile &config)
519{
520 LogFlowThisFuncEnter();
521
522 /* Enclose the state transition NotReady->InInit->Ready */
523 AutoInitSpan autoInitSpan(this);
524 AssertReturn(autoInitSpan.isOk(), E_FAIL);
525
526 Utf8Str strConfigFile;
527 aParent->i_getDefaultMachineFolder(strConfigFile);
528 strConfigFile.append(RTPATH_DELIMITER);
529 strConfigFile.append(strName);
530 strConfigFile.append(RTPATH_DELIMITER);
531 strConfigFile.append(strName);
532 strConfigFile.append(".vbox");
533
534 HRESULT rc = initImpl(aParent, strConfigFile);
535 if (FAILED(rc)) return rc;
536
537 rc = i_tryCreateMachineConfigFile(false /* fForceOverwrite */);
538 if (FAILED(rc)) return rc;
539
540 rc = initDataAndChildObjects();
541
542 if (SUCCEEDED(rc))
543 {
544 // set to true now to cause uninit() to call uninitDataAndChildObjects() on failure
545 mData->mAccessible = TRUE;
546
547 // create empty machine config for instance data
548 mData->pMachineConfigFile = new settings::MachineConfigFile(NULL);
549
550 // generate fresh UUID, ignore machine config
551 unconst(mData->mUuid).create();
552
553 rc = i_loadMachineDataFromSettings(config,
554 &mData->mUuid); // puuidRegistry: initialize media with this registry ID
555
556 // override VM name as well, it may be different
557 mUserData->s.strName = strName;
558
559 if (SUCCEEDED(rc))
560 {
561 /* At this point the changing of the current state modification
562 * flag is allowed. */
563 i_allowStateModification();
564
565 /* commit all changes made during the initialization */
566 i_commit();
567 }
568 }
569
570 /* Confirm a successful initialization when it's the case */
571 if (SUCCEEDED(rc))
572 {
573 if (mData->mAccessible)
574 autoInitSpan.setSucceeded();
575 else
576 {
577 /* Ignore all errors from unregistering, they would destroy
578- * the more interesting error information we already have,
579- * pinpointing the issue with the VM config. */
580 ErrorInfoKeeper eik;
581
582 autoInitSpan.setLimited();
583
584 // uninit media from this machine's media registry, or else
585 // reloading the settings will fail
586 mParent->i_unregisterMachineMedia(i_getId());
587 }
588 }
589
590 LogFlowThisFunc(("mName='%s', mRegistered=%RTbool, mAccessible=%RTbool "
591 "rc=%08X\n",
592 !!mUserData ? mUserData->s.strName.c_str() : "NULL",
593 mData->mRegistered, mData->mAccessible, rc));
594
595 LogFlowThisFuncLeave();
596
597 return rc;
598}
599
600/**
601 * Shared code between the various init() implementations.
602 * @param aParent
603 * @return
604 */
605HRESULT Machine::initImpl(VirtualBox *aParent,
606 const Utf8Str &strConfigFile)
607{
608 LogFlowThisFuncEnter();
609
610 AssertReturn(aParent, E_INVALIDARG);
611 AssertReturn(!strConfigFile.isEmpty(), E_INVALIDARG);
612
613 HRESULT rc = S_OK;
614
615 /* share the parent weakly */
616 unconst(mParent) = aParent;
617
618 /* allocate the essential machine data structure (the rest will be
619 * allocated later by initDataAndChildObjects() */
620 mData.allocate();
621
622 /* memorize the config file name (as provided) */
623 mData->m_strConfigFile = strConfigFile;
624
625 /* get the full file name */
626 int vrc1 = mParent->i_calculateFullPath(strConfigFile, mData->m_strConfigFileFull);
627 if (RT_FAILURE(vrc1))
628 return setError(VBOX_E_FILE_ERROR,
629 tr("Invalid machine settings file name '%s' (%Rrc)"),
630 strConfigFile.c_str(),
631 vrc1);
632
633 LogFlowThisFuncLeave();
634
635 return rc;
636}
637
638/**
639 * Tries to create a machine settings file in the path stored in the machine
640 * instance data. Used when a new machine is created to fail gracefully if
641 * the settings file could not be written (e.g. because machine dir is read-only).
642 * @return
643 */
644HRESULT Machine::i_tryCreateMachineConfigFile(bool fForceOverwrite)
645{
646 HRESULT rc = S_OK;
647
648 // when we create a new machine, we must be able to create the settings file
649 RTFILE f = NIL_RTFILE;
650 int vrc = RTFileOpen(&f, mData->m_strConfigFileFull.c_str(), RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
651 if ( RT_SUCCESS(vrc)
652 || vrc == VERR_SHARING_VIOLATION
653 )
654 {
655 if (RT_SUCCESS(vrc))
656 RTFileClose(f);
657 if (!fForceOverwrite)
658 rc = setError(VBOX_E_FILE_ERROR,
659 tr("Machine settings file '%s' already exists"),
660 mData->m_strConfigFileFull.c_str());
661 else
662 {
663 /* try to delete the config file, as otherwise the creation
664 * of a new settings file will fail. */
665 int vrc2 = RTFileDelete(mData->m_strConfigFileFull.c_str());
666 if (RT_FAILURE(vrc2))
667 rc = setError(VBOX_E_FILE_ERROR,
668 tr("Could not delete the existing settings file '%s' (%Rrc)"),
669 mData->m_strConfigFileFull.c_str(), vrc2);
670 }
671 }
672 else if ( vrc != VERR_FILE_NOT_FOUND
673 && vrc != VERR_PATH_NOT_FOUND
674 )
675 rc = setError(VBOX_E_FILE_ERROR,
676 tr("Invalid machine settings file name '%s' (%Rrc)"),
677 mData->m_strConfigFileFull.c_str(),
678 vrc);
679 return rc;
680}
681
682/**
683 * Initializes the registered machine by loading the settings file.
684 * This method is separated from #init() in order to make it possible to
685 * retry the operation after VirtualBox startup instead of refusing to
686 * startup the whole VirtualBox server in case if the settings file of some
687 * registered VM is invalid or inaccessible.
688 *
689 * @note Must be always called from this object's write lock
690 * (unless called from #init() that doesn't need any locking).
691 * @note Locks the mUSBController method for writing.
692 * @note Subclasses must not call this method.
693 */
694HRESULT Machine::i_registeredInit()
695{
696 AssertReturn(!i_isSessionMachine(), E_FAIL);
697 AssertReturn(!i_isSnapshotMachine(), E_FAIL);
698 AssertReturn(mData->mUuid.isValid(), E_FAIL);
699 AssertReturn(!mData->mAccessible, E_FAIL);
700
701 HRESULT rc = initDataAndChildObjects();
702
703 if (SUCCEEDED(rc))
704 {
705 /* Temporarily reset the registered flag in order to let setters
706 * potentially called from loadSettings() succeed (isMutable() used in
707 * all setters will return FALSE for a Machine instance if mRegistered
708 * is TRUE). */
709 mData->mRegistered = FALSE;
710
711 try
712 {
713 // load and parse machine XML; this will throw on XML or logic errors
714 mData->pMachineConfigFile = new settings::MachineConfigFile(&mData->m_strConfigFileFull);
715
716 if (mData->mUuid != mData->pMachineConfigFile->uuid)
717 throw setError(E_FAIL,
718 tr("Machine UUID {%RTuuid} in '%s' doesn't match its UUID {%s} in the registry file '%s'"),
719 mData->pMachineConfigFile->uuid.raw(),
720 mData->m_strConfigFileFull.c_str(),
721 mData->mUuid.toString().c_str(),
722 mParent->i_settingsFilePath().c_str());
723
724 rc = i_loadMachineDataFromSettings(*mData->pMachineConfigFile,
725 NULL /* const Guid *puuidRegistry */);
726 if (FAILED(rc)) throw rc;
727 }
728 catch (HRESULT err)
729 {
730 /* we assume that error info is set by the thrower */
731 rc = err;
732 }
733 catch (...)
734 {
735 rc = VirtualBoxBase::handleUnexpectedExceptions(this, RT_SRC_POS);
736 }
737
738 /* Restore the registered flag (even on failure) */
739 mData->mRegistered = TRUE;
740 }
741
742 if (SUCCEEDED(rc))
743 {
744 /* Set mAccessible to TRUE only if we successfully locked and loaded
745 * the settings file */
746 mData->mAccessible = TRUE;
747
748 /* commit all changes made during loading the settings file */
749 i_commit(); // @todo r=dj why do we need a commit during init?!? this is very expensive
750 /// @todo r=klaus for some reason the settings loading logic backs up
751 // the settings, and therefore a commit is needed. Should probably be changed.
752 }
753 else
754 {
755 /* If the machine is registered, then, instead of returning a
756 * failure, we mark it as inaccessible and set the result to
757 * success to give it a try later */
758
759 /* fetch the current error info */
760 mData->mAccessError = com::ErrorInfo();
761 LogWarning(("Machine {%RTuuid} is inaccessible! [%ls]\n",
762 mData->mUuid.raw(),
763 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 does
831 * VirtualBox::addCaller() for the duration of the
832 * SessionMachine::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 LogWarningThisFunc(("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 LogWarningThisFunc(("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 LogWarningThisFunc(("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 pVirtualBox.queryInterfaceTo(aParent.asOutParam());
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 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 rc = errorInfo.queryInterfaceTo(aAccessError.asOutParam());
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(MutableOrSavedStateDep);
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 it = mUserData->s.llGroups.begin();
1048 it != mUserData->s.llGroups.end(); ++it, ++i)
1049 aGroups[i] = (*it);
1050
1051 return S_OK;
1052}
1053
1054HRESULT Machine::setGroups(const std::vector<com::Utf8Str> &aGroups)
1055{
1056 StringsList llGroups;
1057 HRESULT rc = mParent->i_convertMachineGroups(aGroups, &llGroups);
1058 if (FAILED(rc))
1059 return rc;
1060
1061 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1062
1063 // changing machine groups is possible while the VM is offline
1064 rc = i_checkStateDependency(OfflineStateDep);
1065 if (FAILED(rc)) return rc;
1066
1067 i_setModified(IsModified_MachineData);
1068 mUserData.backup();
1069 mUserData->s.llGroups = llGroups;
1070
1071 return S_OK;
1072}
1073
1074HRESULT Machine::getOSTypeId(com::Utf8Str &aOSTypeId)
1075{
1076 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1077
1078 aOSTypeId = mUserData->s.strOsType;
1079
1080 return S_OK;
1081}
1082
1083HRESULT Machine::setOSTypeId(const com::Utf8Str &aOSTypeId)
1084{
1085 /* look up the object by Id to check it is valid */
1086 ComPtr<IGuestOSType> guestOSType;
1087 HRESULT rc = mParent->GetGuestOSType(Bstr(aOSTypeId).raw(), guestOSType.asOutParam());
1088 if (FAILED(rc)) return rc;
1089
1090 /* when setting, always use the "etalon" value for consistency -- lookup
1091 * by ID is case-insensitive and the input value may have different case */
1092 Bstr osTypeId;
1093 rc = guestOSType->COMGETTER(Id)(osTypeId.asOutParam());
1094 if (FAILED(rc)) return rc;
1095
1096 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1097
1098 rc = i_checkStateDependency(MutableStateDep);
1099 if (FAILED(rc)) return rc;
1100
1101 i_setModified(IsModified_MachineData);
1102 mUserData.backup();
1103 mUserData->s.strOsType = osTypeId;
1104
1105 return S_OK;
1106}
1107
1108HRESULT Machine::getFirmwareType(FirmwareType_T *aFirmwareType)
1109{
1110 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1111
1112 *aFirmwareType = mHWData->mFirmwareType;
1113
1114 return S_OK;
1115}
1116
1117HRESULT Machine::setFirmwareType(FirmwareType_T aFirmwareType)
1118{
1119 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1120
1121 HRESULT rc = i_checkStateDependency(MutableStateDep);
1122 if (FAILED(rc)) return rc;
1123
1124 i_setModified(IsModified_MachineData);
1125 mHWData.backup();
1126 mHWData->mFirmwareType = aFirmwareType;
1127
1128 return S_OK;
1129}
1130
1131HRESULT Machine::getKeyboardHIDType(KeyboardHIDType_T *aKeyboardHIDType)
1132{
1133 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1134
1135 *aKeyboardHIDType = mHWData->mKeyboardHIDType;
1136
1137 return S_OK;
1138}
1139
1140HRESULT Machine::setKeyboardHIDType(KeyboardHIDType_T aKeyboardHIDType)
1141{
1142 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1143
1144 HRESULT rc = i_checkStateDependency(MutableStateDep);
1145 if (FAILED(rc)) return rc;
1146
1147 i_setModified(IsModified_MachineData);
1148 mHWData.backup();
1149 mHWData->mKeyboardHIDType = aKeyboardHIDType;
1150
1151 return S_OK;
1152}
1153
1154HRESULT Machine::getPointingHIDType(PointingHIDType_T *aPointingHIDType)
1155{
1156 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1157
1158 *aPointingHIDType = mHWData->mPointingHIDType;
1159
1160 return S_OK;
1161}
1162
1163HRESULT Machine::setPointingHIDType(PointingHIDType_T aPointingHIDType)
1164{
1165 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1166
1167 HRESULT rc = i_checkStateDependency(MutableStateDep);
1168 if (FAILED(rc)) return rc;
1169
1170 i_setModified(IsModified_MachineData);
1171 mHWData.backup();
1172 mHWData->mPointingHIDType = aPointingHIDType;
1173
1174 return S_OK;
1175}
1176
1177HRESULT Machine::getChipsetType(ChipsetType_T *aChipsetType)
1178{
1179 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1180
1181 *aChipsetType = mHWData->mChipsetType;
1182
1183 return S_OK;
1184}
1185
1186HRESULT Machine::setChipsetType(ChipsetType_T aChipsetType)
1187{
1188 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1189
1190 HRESULT rc = i_checkStateDependency(MutableStateDep);
1191 if (FAILED(rc)) return rc;
1192
1193 if (aChipsetType != mHWData->mChipsetType)
1194 {
1195 i_setModified(IsModified_MachineData);
1196 mHWData.backup();
1197 mHWData->mChipsetType = aChipsetType;
1198
1199 // Resize network adapter array, to be finalized on commit/rollback.
1200 // We must not throw away entries yet, otherwise settings are lost
1201 // without a way to roll back.
1202 size_t newCount = Global::getMaxNetworkAdapters(aChipsetType);
1203 size_t oldCount = mNetworkAdapters.size();
1204 if (newCount > oldCount)
1205 {
1206 mNetworkAdapters.resize(newCount);
1207 for (size_t slot = oldCount; slot < mNetworkAdapters.size(); slot++)
1208 {
1209 unconst(mNetworkAdapters[slot]).createObject();
1210 mNetworkAdapters[slot]->init(this, (ULONG)slot);
1211 }
1212 }
1213 }
1214
1215 return S_OK;
1216}
1217
1218HRESULT Machine::getParavirtProvider(ParavirtProvider_T *aParavirtProvider)
1219{
1220 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1221
1222 *aParavirtProvider = mHWData->mParavirtProvider;
1223
1224 return S_OK;
1225}
1226
1227HRESULT Machine::setParavirtProvider(ParavirtProvider_T aParavirtProvider)
1228{
1229 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1230
1231 HRESULT rc = i_checkStateDependency(MutableStateDep);
1232 if (FAILED(rc)) return rc;
1233
1234 if (aParavirtProvider != mHWData->mParavirtProvider)
1235 {
1236 i_setModified(IsModified_MachineData);
1237 mHWData.backup();
1238 mHWData->mParavirtProvider = aParavirtProvider;
1239 }
1240
1241 return S_OK;
1242}
1243
1244HRESULT Machine::getEffectiveParavirtProvider(ParavirtProvider_T *aParavirtProvider)
1245{
1246 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1247
1248 *aParavirtProvider = mHWData->mParavirtProvider;
1249 switch (mHWData->mParavirtProvider)
1250 {
1251 case ParavirtProvider_None:
1252 case ParavirtProvider_HyperV:
1253 case ParavirtProvider_Minimal:
1254 break;
1255
1256 /* Resolve dynamic provider types to the effective types. */
1257 default:
1258 {
1259 ComPtr<IGuestOSType> ptrGuestOSType;
1260 HRESULT hrc2 = mParent->GetGuestOSType(Bstr(mUserData->s.strOsType).raw(), ptrGuestOSType.asOutParam());
1261 AssertMsgReturn(SUCCEEDED(hrc2), ("Failed to get guest OS type. hrc2=%Rhrc\n", hrc2), hrc2);
1262
1263 Bstr guestTypeFamilyId;
1264 hrc2 = ptrGuestOSType->COMGETTER(FamilyId)(guestTypeFamilyId.asOutParam());
1265 AssertMsgReturn(SUCCEEDED(hrc2), ("Failed to get guest family. hrc2=%Rhrc\n", hrc2), hrc2);
1266 BOOL fOsXGuest = guestTypeFamilyId == Bstr("MacOS");
1267
1268 switch (mHWData->mParavirtProvider)
1269 {
1270 case ParavirtProvider_Legacy:
1271 {
1272 if (fOsXGuest)
1273 *aParavirtProvider = ParavirtProvider_Minimal;
1274 else
1275 *aParavirtProvider = ParavirtProvider_None;
1276 break;
1277 }
1278
1279 case ParavirtProvider_Default:
1280 {
1281 if (fOsXGuest)
1282 *aParavirtProvider = ParavirtProvider_Minimal;
1283 else if ( mUserData->s.strOsType == "Windows81"
1284 || mUserData->s.strOsType == "Windows81_64"
1285 || mUserData->s.strOsType == "Windows8"
1286 || mUserData->s.strOsType == "Windows8_64"
1287 || mUserData->s.strOsType == "Windows7"
1288 || mUserData->s.strOsType == "Windows7_64"
1289 || mUserData->s.strOsType == "WindowsVista"
1290 || mUserData->s.strOsType == "WindowsVista_64"
1291 || mUserData->s.strOsType == "Windows2012"
1292 || mUserData->s.strOsType == "Windows2012_64"
1293 || mUserData->s.strOsType == "Windows2008"
1294 || mUserData->s.strOsType == "Windows2008_64")
1295 {
1296 *aParavirtProvider = ParavirtProvider_HyperV;
1297 }
1298 else
1299 *aParavirtProvider = ParavirtProvider_None;
1300 break;
1301 }
1302 }
1303 break;
1304 }
1305 }
1306
1307 Assert( *aParavirtProvider == ParavirtProvider_None
1308 || *aParavirtProvider == ParavirtProvider_Minimal
1309 || *aParavirtProvider == ParavirtProvider_HyperV);
1310 return S_OK;
1311}
1312
1313HRESULT Machine::getHardwareVersion(com::Utf8Str &aHardwareVersion)
1314{
1315 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1316
1317 aHardwareVersion = mHWData->mHWVersion;
1318
1319 return S_OK;
1320}
1321
1322HRESULT Machine::setHardwareVersion(const com::Utf8Str &aHardwareVersion)
1323{
1324 /* check known version */
1325 Utf8Str hwVersion = aHardwareVersion;
1326 if ( hwVersion.compare("1") != 0
1327 && hwVersion.compare("2") != 0)
1328 return setError(E_INVALIDARG,
1329 tr("Invalid hardware version: %s\n"), aHardwareVersion.c_str());
1330
1331 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1332
1333 HRESULT rc = i_checkStateDependency(MutableStateDep);
1334 if (FAILED(rc)) return rc;
1335
1336 i_setModified(IsModified_MachineData);
1337 mHWData.backup();
1338 mHWData->mHWVersion = aHardwareVersion;
1339
1340 return S_OK;
1341}
1342
1343HRESULT Machine::getHardwareUUID(com::Guid &aHardwareUUID)
1344{
1345 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1346
1347 if (!mHWData->mHardwareUUID.isZero())
1348 aHardwareUUID = mHWData->mHardwareUUID;
1349 else
1350 aHardwareUUID = mData->mUuid;
1351
1352 return S_OK;
1353}
1354
1355HRESULT Machine::setHardwareUUID(const com::Guid &aHardwareUUID)
1356{
1357 if (!aHardwareUUID.isValid())
1358 return E_INVALIDARG;
1359
1360 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1361
1362 HRESULT rc = i_checkStateDependency(MutableStateDep);
1363 if (FAILED(rc)) return rc;
1364
1365 i_setModified(IsModified_MachineData);
1366 mHWData.backup();
1367 if (aHardwareUUID == mData->mUuid)
1368 mHWData->mHardwareUUID.clear();
1369 else
1370 mHWData->mHardwareUUID = aHardwareUUID;
1371
1372 return S_OK;
1373}
1374
1375HRESULT Machine::getMemorySize(ULONG *aMemorySize)
1376{
1377 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1378
1379 *aMemorySize = mHWData->mMemorySize;
1380
1381 return S_OK;
1382}
1383
1384HRESULT Machine::setMemorySize(ULONG aMemorySize)
1385{
1386 /* check RAM limits */
1387 if ( aMemorySize < MM_RAM_MIN_IN_MB
1388 || aMemorySize > MM_RAM_MAX_IN_MB
1389 )
1390 return setError(E_INVALIDARG,
1391 tr("Invalid RAM size: %lu MB (must be in range [%lu, %lu] MB)"),
1392 aMemorySize, MM_RAM_MIN_IN_MB, MM_RAM_MAX_IN_MB);
1393
1394 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1395
1396 HRESULT rc = i_checkStateDependency(MutableStateDep);
1397 if (FAILED(rc)) return rc;
1398
1399 i_setModified(IsModified_MachineData);
1400 mHWData.backup();
1401 mHWData->mMemorySize = aMemorySize;
1402
1403 return S_OK;
1404}
1405
1406HRESULT Machine::getCPUCount(ULONG *aCPUCount)
1407{
1408 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1409
1410 *aCPUCount = mHWData->mCPUCount;
1411
1412 return S_OK;
1413}
1414
1415HRESULT Machine::setCPUCount(ULONG aCPUCount)
1416{
1417 /* check CPU limits */
1418 if ( aCPUCount < SchemaDefs::MinCPUCount
1419 || aCPUCount > SchemaDefs::MaxCPUCount
1420 )
1421 return setError(E_INVALIDARG,
1422 tr("Invalid virtual CPU count: %lu (must be in range [%lu, %lu])"),
1423 aCPUCount, SchemaDefs::MinCPUCount, SchemaDefs::MaxCPUCount);
1424
1425 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1426
1427 /* We cant go below the current number of CPUs attached if hotplug is enabled*/
1428 if (mHWData->mCPUHotPlugEnabled)
1429 {
1430 for (unsigned idx = aCPUCount; idx < SchemaDefs::MaxCPUCount; idx++)
1431 {
1432 if (mHWData->mCPUAttached[idx])
1433 return setError(E_INVALIDARG,
1434 tr("There is still a CPU attached to socket %lu."
1435 "Detach the CPU before removing the socket"),
1436 aCPUCount, idx+1);
1437 }
1438 }
1439
1440 HRESULT rc = i_checkStateDependency(MutableStateDep);
1441 if (FAILED(rc)) return rc;
1442
1443 i_setModified(IsModified_MachineData);
1444 mHWData.backup();
1445 mHWData->mCPUCount = aCPUCount;
1446
1447 return S_OK;
1448}
1449
1450HRESULT Machine::getCPUExecutionCap(ULONG *aCPUExecutionCap)
1451{
1452 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1453
1454 *aCPUExecutionCap = mHWData->mCpuExecutionCap;
1455
1456 return S_OK;
1457}
1458
1459HRESULT Machine::setCPUExecutionCap(ULONG aCPUExecutionCap)
1460{
1461 HRESULT rc = S_OK;
1462
1463 /* check throttle limits */
1464 if ( aCPUExecutionCap < 1
1465 || aCPUExecutionCap > 100
1466 )
1467 return setError(E_INVALIDARG,
1468 tr("Invalid CPU execution cap value: %lu (must be in range [%lu, %lu])"),
1469 aCPUExecutionCap, 1, 100);
1470
1471 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1472
1473 alock.release();
1474 rc = i_onCPUExecutionCapChange(aCPUExecutionCap);
1475 alock.acquire();
1476 if (FAILED(rc)) return rc;
1477
1478 i_setModified(IsModified_MachineData);
1479 mHWData.backup();
1480 mHWData->mCpuExecutionCap = aCPUExecutionCap;
1481
1482 /** Save settings if online - @todo why is this required? -- @bugref{6818} */
1483 if (Global::IsOnline(mData->mMachineState))
1484 i_saveSettings(NULL);
1485
1486 return S_OK;
1487}
1488
1489HRESULT Machine::getCPUHotPlugEnabled(BOOL *aCPUHotPlugEnabled)
1490{
1491 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1492
1493 *aCPUHotPlugEnabled = mHWData->mCPUHotPlugEnabled;
1494
1495 return S_OK;
1496}
1497
1498HRESULT Machine::setCPUHotPlugEnabled(BOOL aCPUHotPlugEnabled)
1499{
1500 HRESULT rc = S_OK;
1501
1502 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1503
1504 rc = i_checkStateDependency(MutableStateDep);
1505 if (FAILED(rc)) return rc;
1506
1507 if (mHWData->mCPUHotPlugEnabled != aCPUHotPlugEnabled)
1508 {
1509 if (aCPUHotPlugEnabled)
1510 {
1511 i_setModified(IsModified_MachineData);
1512 mHWData.backup();
1513
1514 /* Add the amount of CPUs currently attached */
1515 for (unsigned i = 0; i < mHWData->mCPUCount; ++i)
1516 mHWData->mCPUAttached[i] = true;
1517 }
1518 else
1519 {
1520 /*
1521 * We can disable hotplug only if the amount of maximum CPUs is equal
1522 * to the amount of attached CPUs
1523 */
1524 unsigned cCpusAttached = 0;
1525 unsigned iHighestId = 0;
1526
1527 for (unsigned i = 0; i < SchemaDefs::MaxCPUCount; ++i)
1528 {
1529 if (mHWData->mCPUAttached[i])
1530 {
1531 cCpusAttached++;
1532 iHighestId = i;
1533 }
1534 }
1535
1536 if ( (cCpusAttached != mHWData->mCPUCount)
1537 || (iHighestId >= mHWData->mCPUCount))
1538 return setError(E_INVALIDARG,
1539 tr("CPU hotplugging can't be disabled because the maximum number of CPUs is not equal to the amount of CPUs attached"));
1540
1541 i_setModified(IsModified_MachineData);
1542 mHWData.backup();
1543 }
1544 }
1545
1546 mHWData->mCPUHotPlugEnabled = aCPUHotPlugEnabled;
1547
1548 return rc;
1549}
1550
1551HRESULT Machine::getEmulatedUSBCardReaderEnabled(BOOL *aEmulatedUSBCardReaderEnabled)
1552{
1553#ifdef VBOX_WITH_USB_CARDREADER
1554 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1555
1556 *aEmulatedUSBCardReaderEnabled = mHWData->mEmulatedUSBCardReaderEnabled;
1557
1558 return S_OK;
1559#else
1560 NOREF(aEmulatedUSBCardReaderEnabled);
1561 return E_NOTIMPL;
1562#endif
1563}
1564
1565HRESULT Machine::setEmulatedUSBCardReaderEnabled(BOOL aEmulatedUSBCardReaderEnabled)
1566{
1567#ifdef VBOX_WITH_USB_CARDREADER
1568 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1569
1570 HRESULT rc = i_checkStateDependency(MutableStateDep);
1571 if (FAILED(rc)) return rc;
1572
1573 i_setModified(IsModified_MachineData);
1574 mHWData.backup();
1575 mHWData->mEmulatedUSBCardReaderEnabled = aEmulatedUSBCardReaderEnabled;
1576
1577 return S_OK;
1578#else
1579 NOREF(aEmulatedUSBCardReaderEnabled);
1580 return E_NOTIMPL;
1581#endif
1582}
1583
1584HRESULT Machine::getHPETEnabled(BOOL *aHPETEnabled)
1585{
1586 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1587
1588 *aHPETEnabled = mHWData->mHPETEnabled;
1589
1590 return S_OK;
1591}
1592
1593HRESULT Machine::setHPETEnabled(BOOL aHPETEnabled)
1594{
1595 HRESULT rc = S_OK;
1596
1597 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1598
1599 rc = i_checkStateDependency(MutableStateDep);
1600 if (FAILED(rc)) return rc;
1601
1602 i_setModified(IsModified_MachineData);
1603 mHWData.backup();
1604
1605 mHWData->mHPETEnabled = aHPETEnabled;
1606
1607 return rc;
1608}
1609
1610HRESULT Machine::getVideoCaptureEnabled(BOOL *aVideoCaptureEnabled)
1611{
1612 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1613
1614 *aVideoCaptureEnabled = mHWData->mVideoCaptureEnabled;
1615 return S_OK;
1616}
1617
1618HRESULT Machine::setVideoCaptureEnabled(BOOL aVideoCaptureEnabled)
1619{
1620 HRESULT rc = S_OK;
1621
1622 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1623
1624 i_setModified(IsModified_MachineData);
1625 mHWData.backup();
1626 mHWData->mVideoCaptureEnabled = aVideoCaptureEnabled;
1627
1628 alock.release();
1629 rc = i_onVideoCaptureChange();
1630 alock.acquire();
1631 if (FAILED(rc))
1632 {
1633 /*
1634 * Normally we would do the actual change _after_ i_onVideoCaptureChange() succeeded.
1635 * We cannot do this because that function uses Machine::GetVideoCaptureEnabled to
1636 * determine if it should start or stop capturing. Therefore we need to manually
1637 * undo change.
1638 */
1639 mHWData->mVideoCaptureEnabled = mHWData.backedUpData()->mVideoCaptureEnabled;
1640 return rc;
1641 }
1642
1643 /** Save settings if online - @todo why is this required? -- @bugref{6818} */
1644 if (Global::IsOnline(mData->mMachineState))
1645 i_saveSettings(NULL);
1646
1647 return rc;
1648}
1649
1650HRESULT Machine::getVideoCaptureScreens(std::vector<BOOL> &aVideoCaptureScreens)
1651{
1652 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1653 aVideoCaptureScreens.resize(mHWData->mMonitorCount);
1654 for (unsigned i = 0; i < mHWData->mMonitorCount; ++i)
1655 aVideoCaptureScreens[i] = mHWData->maVideoCaptureScreens[i];
1656 return S_OK;
1657}
1658
1659HRESULT Machine::setVideoCaptureScreens(const std::vector<BOOL> &aVideoCaptureScreens)
1660{
1661 SafeArray<BOOL> screens(aVideoCaptureScreens);
1662 AssertReturn(screens.size() <= RT_ELEMENTS(mHWData->maVideoCaptureScreens), E_INVALIDARG);
1663 bool fChanged = false;
1664
1665 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1666
1667 for (unsigned i = 0; i < screens.size(); ++i)
1668 {
1669 if (mHWData->maVideoCaptureScreens[i] != RT_BOOL(screens[i]))
1670 {
1671 mHWData->maVideoCaptureScreens[i] = RT_BOOL(screens[i]);
1672 fChanged = true;
1673 }
1674 }
1675 if (fChanged)
1676 {
1677 alock.release();
1678 HRESULT rc = i_onVideoCaptureChange();
1679 alock.acquire();
1680 if (FAILED(rc)) return rc;
1681 i_setModified(IsModified_MachineData);
1682
1683 /** Save settings if online - @todo why is this required? -- @bugref{6818} */
1684 if (Global::IsOnline(mData->mMachineState))
1685 i_saveSettings(NULL);
1686 }
1687
1688 return S_OK;
1689}
1690
1691HRESULT Machine::getVideoCaptureFile(com::Utf8Str &aVideoCaptureFile)
1692{
1693 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1694 if (mHWData->mVideoCaptureFile.isEmpty())
1695 i_getDefaultVideoCaptureFile(aVideoCaptureFile);
1696 else
1697 aVideoCaptureFile = mHWData->mVideoCaptureFile;
1698 return S_OK;
1699}
1700
1701HRESULT Machine::setVideoCaptureFile(const com::Utf8Str &aVideoCaptureFile)
1702{
1703 Utf8Str strFile(aVideoCaptureFile);
1704 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1705
1706 if ( Global::IsOnline(mData->mMachineState)
1707 && mHWData->mVideoCaptureEnabled)
1708 return setError(E_INVALIDARG, tr("Cannot change parameters while capturing is enabled"));
1709
1710 if (!RTPathStartsWithRoot(strFile.c_str()))
1711 return setError(E_INVALIDARG, tr("Video capture file name '%s' is not absolute"), strFile.c_str());
1712
1713 if (!strFile.isEmpty())
1714 {
1715 Utf8Str defaultFile;
1716 i_getDefaultVideoCaptureFile(defaultFile);
1717 if (!RTPathCompare(strFile.c_str(), defaultFile.c_str()))
1718 strFile.setNull();
1719 }
1720
1721 i_setModified(IsModified_MachineData);
1722 mHWData.backup();
1723 mHWData->mVideoCaptureFile = strFile;
1724
1725 return S_OK;
1726}
1727
1728HRESULT Machine::getVideoCaptureWidth(ULONG *aVideoCaptureWidth)
1729{
1730 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1731 *aVideoCaptureWidth = mHWData->mVideoCaptureWidth;
1732 return S_OK;
1733}
1734
1735HRESULT Machine::setVideoCaptureWidth(ULONG aVideoCaptureWidth)
1736{
1737 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1738
1739 if ( Global::IsOnline(mData->mMachineState)
1740 && mHWData->mVideoCaptureEnabled)
1741 return setError(E_INVALIDARG, tr("Cannot change parameters while capturing is enabled"));
1742
1743 i_setModified(IsModified_MachineData);
1744 mHWData.backup();
1745 mHWData->mVideoCaptureWidth = aVideoCaptureWidth;
1746
1747 return S_OK;
1748}
1749
1750HRESULT Machine::getVideoCaptureHeight(ULONG *aVideoCaptureHeight)
1751{
1752 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1753 *aVideoCaptureHeight = mHWData->mVideoCaptureHeight;
1754 return S_OK;
1755}
1756
1757HRESULT Machine::setVideoCaptureHeight(ULONG aVideoCaptureHeight)
1758{
1759 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1760
1761 if ( Global::IsOnline(mData->mMachineState)
1762 && mHWData->mVideoCaptureEnabled)
1763 return setError(E_INVALIDARG, tr("Cannot change parameters while capturing is enabled"));
1764
1765 i_setModified(IsModified_MachineData);
1766 mHWData.backup();
1767 mHWData->mVideoCaptureHeight = aVideoCaptureHeight;
1768
1769 return S_OK;
1770}
1771
1772HRESULT Machine::getVideoCaptureRate(ULONG *aVideoCaptureRate)
1773{
1774 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1775 *aVideoCaptureRate = mHWData->mVideoCaptureRate;
1776 return S_OK;
1777}
1778
1779HRESULT Machine::setVideoCaptureRate(ULONG aVideoCaptureRate)
1780{
1781 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1782
1783 if ( Global::IsOnline(mData->mMachineState)
1784 && mHWData->mVideoCaptureEnabled)
1785 return setError(E_INVALIDARG, tr("Cannot change parameters while capturing is enabled"));
1786
1787 i_setModified(IsModified_MachineData);
1788 mHWData.backup();
1789 mHWData->mVideoCaptureRate = aVideoCaptureRate;
1790
1791 return S_OK;
1792}
1793
1794HRESULT Machine::getVideoCaptureFPS(ULONG *aVideoCaptureFPS)
1795{
1796 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1797 *aVideoCaptureFPS = mHWData->mVideoCaptureFPS;
1798 return S_OK;
1799}
1800
1801HRESULT Machine::setVideoCaptureFPS(ULONG aVideoCaptureFPS)
1802{
1803 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1804
1805 if ( Global::IsOnline(mData->mMachineState)
1806 && mHWData->mVideoCaptureEnabled)
1807 return setError(E_INVALIDARG, tr("Cannot change parameters while capturing is enabled"));
1808
1809 i_setModified(IsModified_MachineData);
1810 mHWData.backup();
1811 mHWData->mVideoCaptureFPS = aVideoCaptureFPS;
1812
1813 return S_OK;
1814}
1815
1816HRESULT Machine::getVideoCaptureMaxTime(ULONG *aVideoCaptureMaxTime)
1817{
1818 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1819 *aVideoCaptureMaxTime = mHWData->mVideoCaptureMaxTime;
1820 return S_OK;
1821}
1822
1823HRESULT Machine::setVideoCaptureMaxTime(ULONG aVideoCaptureMaxTime)
1824{
1825 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1826
1827 if ( Global::IsOnline(mData->mMachineState)
1828 && mHWData->mVideoCaptureEnabled)
1829 return setError(E_INVALIDARG, tr("Cannot change parameters while capturing is enabled"));
1830
1831 i_setModified(IsModified_MachineData);
1832 mHWData.backup();
1833 mHWData->mVideoCaptureMaxTime = aVideoCaptureMaxTime;
1834
1835 return S_OK;
1836}
1837
1838HRESULT Machine::getVideoCaptureMaxFileSize(ULONG *aVideoCaptureMaxFileSize)
1839{
1840 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1841 *aVideoCaptureMaxFileSize = mHWData->mVideoCaptureMaxFileSize;
1842 return S_OK;
1843}
1844
1845HRESULT Machine::setVideoCaptureMaxFileSize(ULONG aVideoCaptureMaxFileSize)
1846{
1847 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1848
1849 if ( Global::IsOnline(mData->mMachineState)
1850 && mHWData->mVideoCaptureEnabled)
1851 return setError(E_INVALIDARG, tr("Cannot change parameters while capturing is enabled"));
1852
1853 i_setModified(IsModified_MachineData);
1854 mHWData.backup();
1855 mHWData->mVideoCaptureMaxFileSize = aVideoCaptureMaxFileSize;
1856
1857 return S_OK;
1858}
1859
1860HRESULT Machine::getVideoCaptureOptions(com::Utf8Str &aVideoCaptureOptions)
1861{
1862 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1863
1864 aVideoCaptureOptions = mHWData->mVideoCaptureOptions;
1865 return S_OK;
1866}
1867
1868HRESULT Machine::setVideoCaptureOptions(const com::Utf8Str &aVideoCaptureOptions)
1869{
1870 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1871
1872 if ( Global::IsOnline(mData->mMachineState)
1873 && mHWData->mVideoCaptureEnabled)
1874 return setError(E_INVALIDARG, tr("Cannot change parameters while capturing is enabled"));
1875
1876 i_setModified(IsModified_MachineData);
1877 mHWData.backup();
1878 mHWData->mVideoCaptureOptions = aVideoCaptureOptions;
1879
1880 return S_OK;
1881}
1882
1883HRESULT Machine::getGraphicsControllerType(GraphicsControllerType_T *aGraphicsControllerType)
1884{
1885 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1886
1887 *aGraphicsControllerType = mHWData->mGraphicsControllerType;
1888
1889 return S_OK;
1890}
1891
1892HRESULT Machine::setGraphicsControllerType(GraphicsControllerType_T aGraphicsControllerType)
1893{
1894 switch (aGraphicsControllerType)
1895 {
1896 case GraphicsControllerType_Null:
1897 case GraphicsControllerType_VBoxVGA:
1898#ifdef VBOX_WITH_VMSVGA
1899 case GraphicsControllerType_VMSVGA:
1900#endif
1901 break;
1902 default:
1903 return setError(E_INVALIDARG, tr("The graphics controller type (%d) is invalid"), aGraphicsControllerType);
1904 }
1905
1906 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1907
1908 HRESULT rc = i_checkStateDependency(MutableStateDep);
1909 if (FAILED(rc)) return rc;
1910
1911 i_setModified(IsModified_MachineData);
1912 mHWData.backup();
1913 mHWData->mGraphicsControllerType = aGraphicsControllerType;
1914
1915 return S_OK;
1916}
1917
1918HRESULT Machine::getVRAMSize(ULONG *aVRAMSize)
1919{
1920 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1921
1922 *aVRAMSize = mHWData->mVRAMSize;
1923
1924 return S_OK;
1925}
1926
1927HRESULT Machine::setVRAMSize(ULONG aVRAMSize)
1928{
1929 /* check VRAM limits */
1930 if (aVRAMSize < SchemaDefs::MinGuestVRAM ||
1931 aVRAMSize > SchemaDefs::MaxGuestVRAM)
1932 return setError(E_INVALIDARG,
1933 tr("Invalid VRAM size: %lu MB (must be in range [%lu, %lu] MB)"),
1934 aVRAMSize, SchemaDefs::MinGuestVRAM, SchemaDefs::MaxGuestVRAM);
1935
1936 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1937
1938 HRESULT rc = i_checkStateDependency(MutableStateDep);
1939 if (FAILED(rc)) return rc;
1940
1941 i_setModified(IsModified_MachineData);
1942 mHWData.backup();
1943 mHWData->mVRAMSize = aVRAMSize;
1944
1945 return S_OK;
1946}
1947
1948/** @todo this method should not be public */
1949HRESULT Machine::getMemoryBalloonSize(ULONG *aMemoryBalloonSize)
1950{
1951 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1952
1953 *aMemoryBalloonSize = mHWData->mMemoryBalloonSize;
1954
1955 return S_OK;
1956}
1957
1958/**
1959 * Set the memory balloon size.
1960 *
1961 * This method is also called from IGuest::COMSETTER(MemoryBalloonSize) so
1962 * we have to make sure that we never call IGuest from here.
1963 */
1964HRESULT Machine::setMemoryBalloonSize(ULONG aMemoryBalloonSize)
1965{
1966 /* This must match GMMR0Init; currently we only support memory ballooning on all 64-bit hosts except Mac OS X */
1967#if HC_ARCH_BITS == 64 && (defined(RT_OS_WINDOWS) || defined(RT_OS_SOLARIS) || defined(RT_OS_LINUX) || defined(RT_OS_FREEBSD))
1968 /* check limits */
1969 if (aMemoryBalloonSize >= VMMDEV_MAX_MEMORY_BALLOON(mHWData->mMemorySize))
1970 return setError(E_INVALIDARG,
1971 tr("Invalid memory balloon size: %lu MB (must be in range [%lu, %lu] MB)"),
1972 aMemoryBalloonSize, 0, VMMDEV_MAX_MEMORY_BALLOON(mHWData->mMemorySize));
1973
1974 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1975
1976 i_setModified(IsModified_MachineData);
1977 mHWData.backup();
1978 mHWData->mMemoryBalloonSize = aMemoryBalloonSize;
1979
1980 return S_OK;
1981#else
1982 NOREF(aMemoryBalloonSize);
1983 return setError(E_NOTIMPL, tr("Memory ballooning is only supported on 64-bit hosts"));
1984#endif
1985}
1986
1987HRESULT Machine::getPageFusionEnabled(BOOL *aPageFusionEnabled)
1988{
1989 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1990
1991 *aPageFusionEnabled = mHWData->mPageFusionEnabled;
1992 return S_OK;
1993}
1994
1995HRESULT Machine::setPageFusionEnabled(BOOL aPageFusionEnabled)
1996{
1997#ifdef VBOX_WITH_PAGE_SHARING
1998 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1999
2000 /** @todo must support changes for running vms and keep this in sync with IGuest. */
2001 i_setModified(IsModified_MachineData);
2002 mHWData.backup();
2003 mHWData->mPageFusionEnabled = aPageFusionEnabled;
2004 return S_OK;
2005#else
2006 NOREF(aPageFusionEnabled);
2007 return setError(E_NOTIMPL, tr("Page fusion is only supported on 64-bit hosts"));
2008#endif
2009}
2010
2011HRESULT Machine::getAccelerate3DEnabled(BOOL *aAccelerate3DEnabled)
2012{
2013 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2014
2015 *aAccelerate3DEnabled = mHWData->mAccelerate3DEnabled;
2016
2017 return S_OK;
2018}
2019
2020HRESULT Machine::setAccelerate3DEnabled(BOOL aAccelerate3DEnabled)
2021{
2022 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2023
2024 HRESULT rc = i_checkStateDependency(MutableStateDep);
2025 if (FAILED(rc)) return rc;
2026
2027 /** @todo check validity! */
2028
2029 i_setModified(IsModified_MachineData);
2030 mHWData.backup();
2031 mHWData->mAccelerate3DEnabled = aAccelerate3DEnabled;
2032
2033 return S_OK;
2034}
2035
2036
2037HRESULT Machine::getAccelerate2DVideoEnabled(BOOL *aAccelerate2DVideoEnabled)
2038{
2039 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2040
2041 *aAccelerate2DVideoEnabled = mHWData->mAccelerate2DVideoEnabled;
2042
2043 return S_OK;
2044}
2045
2046HRESULT Machine::setAccelerate2DVideoEnabled(BOOL aAccelerate2DVideoEnabled)
2047{
2048 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2049
2050 HRESULT rc = i_checkStateDependency(MutableStateDep);
2051 if (FAILED(rc)) return rc;
2052
2053 /** @todo check validity! */
2054 i_setModified(IsModified_MachineData);
2055 mHWData.backup();
2056 mHWData->mAccelerate2DVideoEnabled = aAccelerate2DVideoEnabled;
2057
2058 return S_OK;
2059}
2060
2061HRESULT Machine::getMonitorCount(ULONG *aMonitorCount)
2062{
2063 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2064
2065 *aMonitorCount = mHWData->mMonitorCount;
2066
2067 return S_OK;
2068}
2069
2070HRESULT Machine::setMonitorCount(ULONG aMonitorCount)
2071{
2072 /* make sure monitor count is a sensible number */
2073 if (aMonitorCount < 1 || aMonitorCount > SchemaDefs::MaxGuestMonitors)
2074 return setError(E_INVALIDARG,
2075 tr("Invalid monitor count: %lu (must be in range [%lu, %lu])"),
2076 aMonitorCount, 1, SchemaDefs::MaxGuestMonitors);
2077
2078 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2079
2080 HRESULT rc = i_checkStateDependency(MutableStateDep);
2081 if (FAILED(rc)) return rc;
2082
2083 i_setModified(IsModified_MachineData);
2084 mHWData.backup();
2085 mHWData->mMonitorCount = aMonitorCount;
2086
2087 return S_OK;
2088}
2089
2090HRESULT Machine::getBIOSSettings(ComPtr<IBIOSSettings> &aBIOSSettings)
2091{
2092 /* mBIOSSettings is constant during life time, no need to lock */
2093 mBIOSSettings.queryInterfaceTo(aBIOSSettings.asOutParam());
2094
2095 return S_OK;
2096}
2097
2098HRESULT Machine::getCPUProperty(CPUPropertyType_T aProperty, BOOL *aValue)
2099{
2100 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2101
2102 switch (aProperty)
2103 {
2104 case CPUPropertyType_PAE:
2105 *aValue = mHWData->mPAEEnabled;
2106 break;
2107
2108 case CPUPropertyType_Synthetic:
2109 *aValue = mHWData->mSyntheticCpu;
2110 break;
2111
2112 case CPUPropertyType_LongMode:
2113 if (mHWData->mLongMode == settings::Hardware::LongMode_Enabled)
2114 *aValue = TRUE;
2115 else if (mHWData->mLongMode == settings::Hardware::LongMode_Disabled)
2116 *aValue = FALSE;
2117#if HC_ARCH_BITS == 64
2118 else
2119 *aValue = TRUE;
2120#else
2121 else
2122 {
2123 *aValue = FALSE;
2124
2125 ComPtr<IGuestOSType> ptrGuestOSType;
2126 HRESULT hrc2 = mParent->GetGuestOSType(Bstr(mUserData->s.strOsType).raw(), ptrGuestOSType.asOutParam());
2127 if (SUCCEEDED(hrc2))
2128 {
2129 BOOL fIs64Bit = FALSE;
2130 hrc2 = ptrGuestOSType->COMGETTER(Is64Bit)(&fIs64Bit); AssertComRC(hrc2);
2131 if (SUCCEEDED(hrc2) && fIs64Bit)
2132 {
2133 ComObjPtr<Host> ptrHost = mParent->i_host();
2134 alock.release();
2135
2136 hrc2 = ptrHost->GetProcessorFeature(ProcessorFeature_LongMode, aValue); AssertComRC(hrc2);
2137 if (FAILED(hrc2))
2138 *aValue = FALSE;
2139 }
2140 }
2141 }
2142#endif
2143 break;
2144
2145 case CPUPropertyType_TripleFaultReset:
2146 *aValue = mHWData->mTripleFaultReset;
2147 break;
2148
2149 default:
2150 return E_INVALIDARG;
2151 }
2152 return S_OK;
2153}
2154
2155HRESULT Machine::setCPUProperty(CPUPropertyType_T aProperty, BOOL aValue)
2156{
2157 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2158
2159 HRESULT rc = i_checkStateDependency(MutableStateDep);
2160 if (FAILED(rc)) return rc;
2161
2162 switch (aProperty)
2163 {
2164 case CPUPropertyType_PAE:
2165 i_setModified(IsModified_MachineData);
2166 mHWData.backup();
2167 mHWData->mPAEEnabled = !!aValue;
2168 break;
2169
2170 case CPUPropertyType_Synthetic:
2171 i_setModified(IsModified_MachineData);
2172 mHWData.backup();
2173 mHWData->mSyntheticCpu = !!aValue;
2174 break;
2175
2176 case CPUPropertyType_LongMode:
2177 i_setModified(IsModified_MachineData);
2178 mHWData.backup();
2179 mHWData->mLongMode = !aValue ? settings::Hardware::LongMode_Disabled : settings::Hardware::LongMode_Enabled;
2180 break;
2181
2182 case CPUPropertyType_TripleFaultReset:
2183 i_setModified(IsModified_MachineData);
2184 mHWData.backup();
2185 mHWData->mTripleFaultReset = !!aValue;
2186 break;
2187
2188 default:
2189 return E_INVALIDARG;
2190 }
2191 return S_OK;
2192}
2193
2194HRESULT Machine::getCPUIDLeaf(ULONG aId, ULONG *aValEax, ULONG *aValEbx, ULONG *aValEcx, ULONG *aValEdx)
2195{
2196 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2197
2198 switch(aId)
2199 {
2200 case 0x0:
2201 case 0x1:
2202 case 0x2:
2203 case 0x3:
2204 case 0x4:
2205 case 0x5:
2206 case 0x6:
2207 case 0x7:
2208 case 0x8:
2209 case 0x9:
2210 case 0xA:
2211 if (mHWData->mCpuIdStdLeafs[aId].ulId != aId)
2212 return E_INVALIDARG;
2213
2214 *aValEax = mHWData->mCpuIdStdLeafs[aId].ulEax;
2215 *aValEbx = mHWData->mCpuIdStdLeafs[aId].ulEbx;
2216 *aValEcx = mHWData->mCpuIdStdLeafs[aId].ulEcx;
2217 *aValEdx = mHWData->mCpuIdStdLeafs[aId].ulEdx;
2218 break;
2219
2220 case 0x80000000:
2221 case 0x80000001:
2222 case 0x80000002:
2223 case 0x80000003:
2224 case 0x80000004:
2225 case 0x80000005:
2226 case 0x80000006:
2227 case 0x80000007:
2228 case 0x80000008:
2229 case 0x80000009:
2230 case 0x8000000A:
2231 if (mHWData->mCpuIdExtLeafs[aId - 0x80000000].ulId != aId)
2232 return E_INVALIDARG;
2233
2234 *aValEax = mHWData->mCpuIdExtLeafs[aId - 0x80000000].ulEax;
2235 *aValEbx = mHWData->mCpuIdExtLeafs[aId - 0x80000000].ulEbx;
2236 *aValEcx = mHWData->mCpuIdExtLeafs[aId - 0x80000000].ulEcx;
2237 *aValEdx = mHWData->mCpuIdExtLeafs[aId - 0x80000000].ulEdx;
2238 break;
2239
2240 default:
2241 return setError(E_INVALIDARG, tr("CpuId override leaf %#x is out of range"), aId);
2242 }
2243 return S_OK;
2244}
2245
2246
2247HRESULT Machine::setCPUIDLeaf(ULONG aId, ULONG aValEax, ULONG aValEbx, ULONG aValEcx, ULONG aValEdx)
2248{
2249 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2250
2251 HRESULT rc = i_checkStateDependency(MutableStateDep);
2252 if (FAILED(rc)) return rc;
2253
2254 switch(aId)
2255 {
2256 case 0x0:
2257 case 0x1:
2258 case 0x2:
2259 case 0x3:
2260 case 0x4:
2261 case 0x5:
2262 case 0x6:
2263 case 0x7:
2264 case 0x8:
2265 case 0x9:
2266 case 0xA:
2267 AssertCompile(RT_ELEMENTS(mHWData->mCpuIdStdLeafs) == 0xB);
2268 AssertRelease(aId < RT_ELEMENTS(mHWData->mCpuIdStdLeafs));
2269 i_setModified(IsModified_MachineData);
2270 mHWData.backup();
2271 mHWData->mCpuIdStdLeafs[aId].ulId = aId;
2272 mHWData->mCpuIdStdLeafs[aId].ulEax = aValEax;
2273 mHWData->mCpuIdStdLeafs[aId].ulEbx = aValEbx;
2274 mHWData->mCpuIdStdLeafs[aId].ulEcx = aValEcx;
2275 mHWData->mCpuIdStdLeafs[aId].ulEdx = aValEdx;
2276 break;
2277
2278 case 0x80000000:
2279 case 0x80000001:
2280 case 0x80000002:
2281 case 0x80000003:
2282 case 0x80000004:
2283 case 0x80000005:
2284 case 0x80000006:
2285 case 0x80000007:
2286 case 0x80000008:
2287 case 0x80000009:
2288 case 0x8000000A:
2289 AssertCompile(RT_ELEMENTS(mHWData->mCpuIdExtLeafs) == 0xB);
2290 AssertRelease(aId - 0x80000000 < RT_ELEMENTS(mHWData->mCpuIdExtLeafs));
2291 i_setModified(IsModified_MachineData);
2292 mHWData.backup();
2293 mHWData->mCpuIdExtLeafs[aId - 0x80000000].ulId = aId;
2294 mHWData->mCpuIdExtLeafs[aId - 0x80000000].ulEax = aValEax;
2295 mHWData->mCpuIdExtLeafs[aId - 0x80000000].ulEbx = aValEbx;
2296 mHWData->mCpuIdExtLeafs[aId - 0x80000000].ulEcx = aValEcx;
2297 mHWData->mCpuIdExtLeafs[aId - 0x80000000].ulEdx = aValEdx;
2298 break;
2299
2300 default:
2301 return setError(E_INVALIDARG, tr("CpuId override leaf %#x is out of range"), aId);
2302 }
2303 return S_OK;
2304}
2305
2306HRESULT Machine::removeCPUIDLeaf(ULONG aId)
2307{
2308 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2309
2310 HRESULT rc = i_checkStateDependency(MutableStateDep);
2311 if (FAILED(rc)) return rc;
2312
2313 switch(aId)
2314 {
2315 case 0x0:
2316 case 0x1:
2317 case 0x2:
2318 case 0x3:
2319 case 0x4:
2320 case 0x5:
2321 case 0x6:
2322 case 0x7:
2323 case 0x8:
2324 case 0x9:
2325 case 0xA:
2326 AssertCompile(RT_ELEMENTS(mHWData->mCpuIdStdLeafs) == 0xB);
2327 AssertRelease(aId < RT_ELEMENTS(mHWData->mCpuIdStdLeafs));
2328 i_setModified(IsModified_MachineData);
2329 mHWData.backup();
2330 /* Invalidate leaf. */
2331 mHWData->mCpuIdStdLeafs[aId].ulId = UINT32_MAX;
2332 break;
2333
2334 case 0x80000000:
2335 case 0x80000001:
2336 case 0x80000002:
2337 case 0x80000003:
2338 case 0x80000004:
2339 case 0x80000005:
2340 case 0x80000006:
2341 case 0x80000007:
2342 case 0x80000008:
2343 case 0x80000009:
2344 case 0x8000000A:
2345 AssertCompile(RT_ELEMENTS(mHWData->mCpuIdExtLeafs) == 0xB);
2346 AssertRelease(aId - 0x80000000 < RT_ELEMENTS(mHWData->mCpuIdExtLeafs));
2347 i_setModified(IsModified_MachineData);
2348 mHWData.backup();
2349 /* Invalidate leaf. */
2350 mHWData->mCpuIdExtLeafs[aId - 0x80000000].ulId = UINT32_MAX;
2351 break;
2352
2353 default:
2354 return setError(E_INVALIDARG, tr("CpuId override leaf %#x is out of range"), aId);
2355 }
2356 return S_OK;
2357}
2358
2359HRESULT Machine::removeAllCPUIDLeaves()
2360{
2361 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2362
2363 HRESULT rc = i_checkStateDependency(MutableStateDep);
2364 if (FAILED(rc)) return rc;
2365
2366 i_setModified(IsModified_MachineData);
2367 mHWData.backup();
2368
2369 /* Invalidate all standard leafs. */
2370 for (unsigned i = 0; i < RT_ELEMENTS(mHWData->mCpuIdStdLeafs); ++i)
2371 mHWData->mCpuIdStdLeafs[i].ulId = UINT32_MAX;
2372
2373 /* Invalidate all extended leafs. */
2374 for (unsigned i = 0; i < RT_ELEMENTS(mHWData->mCpuIdExtLeafs); ++i)
2375 mHWData->mCpuIdExtLeafs[i].ulId = UINT32_MAX;
2376
2377 return S_OK;
2378}
2379HRESULT Machine::getHWVirtExProperty(HWVirtExPropertyType_T aProperty, BOOL *aValue)
2380{
2381 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2382
2383 switch(aProperty)
2384 {
2385 case HWVirtExPropertyType_Enabled:
2386 *aValue = mHWData->mHWVirtExEnabled;
2387 break;
2388
2389 case HWVirtExPropertyType_VPID:
2390 *aValue = mHWData->mHWVirtExVPIDEnabled;
2391 break;
2392
2393 case HWVirtExPropertyType_NestedPaging:
2394 *aValue = mHWData->mHWVirtExNestedPagingEnabled;
2395 break;
2396
2397 case HWVirtExPropertyType_UnrestrictedExecution:
2398 *aValue = mHWData->mHWVirtExUXEnabled;
2399 break;
2400
2401 case HWVirtExPropertyType_LargePages:
2402 *aValue = mHWData->mHWVirtExLargePagesEnabled;
2403#if defined(DEBUG_bird) && defined(RT_OS_LINUX) /* This feature is deadly here */
2404 *aValue = FALSE;
2405#endif
2406 break;
2407
2408 case HWVirtExPropertyType_Force:
2409 *aValue = mHWData->mHWVirtExForceEnabled;
2410 break;
2411
2412 default:
2413 return E_INVALIDARG;
2414 }
2415 return S_OK;
2416}
2417
2418HRESULT Machine::setHWVirtExProperty(HWVirtExPropertyType_T aProperty, BOOL aValue)
2419{
2420 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2421
2422 HRESULT rc = i_checkStateDependency(MutableStateDep);
2423 if (FAILED(rc)) return rc;
2424
2425 switch(aProperty)
2426 {
2427 case HWVirtExPropertyType_Enabled:
2428 i_setModified(IsModified_MachineData);
2429 mHWData.backup();
2430 mHWData->mHWVirtExEnabled = !!aValue;
2431 break;
2432
2433 case HWVirtExPropertyType_VPID:
2434 i_setModified(IsModified_MachineData);
2435 mHWData.backup();
2436 mHWData->mHWVirtExVPIDEnabled = !!aValue;
2437 break;
2438
2439 case HWVirtExPropertyType_NestedPaging:
2440 i_setModified(IsModified_MachineData);
2441 mHWData.backup();
2442 mHWData->mHWVirtExNestedPagingEnabled = !!aValue;
2443 break;
2444
2445 case HWVirtExPropertyType_UnrestrictedExecution:
2446 i_setModified(IsModified_MachineData);
2447 mHWData.backup();
2448 mHWData->mHWVirtExUXEnabled = !!aValue;
2449 break;
2450
2451 case HWVirtExPropertyType_LargePages:
2452 i_setModified(IsModified_MachineData);
2453 mHWData.backup();
2454 mHWData->mHWVirtExLargePagesEnabled = !!aValue;
2455 break;
2456
2457 case HWVirtExPropertyType_Force:
2458 i_setModified(IsModified_MachineData);
2459 mHWData.backup();
2460 mHWData->mHWVirtExForceEnabled = !!aValue;
2461 break;
2462
2463 default:
2464 return E_INVALIDARG;
2465 }
2466
2467 return S_OK;
2468}
2469
2470HRESULT Machine::getSnapshotFolder(com::Utf8Str &aSnapshotFolder)
2471{
2472 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2473
2474 i_calculateFullPath(mUserData->s.strSnapshotFolder, aSnapshotFolder);
2475
2476 return S_OK;
2477}
2478
2479HRESULT Machine::setSnapshotFolder(const com::Utf8Str &aSnapshotFolder)
2480{
2481 /* @todo (r=dmik):
2482 * 1. Allow to change the name of the snapshot folder containing snapshots
2483 * 2. Rename the folder on disk instead of just changing the property
2484 * value (to be smart and not to leave garbage). Note that it cannot be
2485 * done here because the change may be rolled back. Thus, the right
2486 * place is #saveSettings().
2487 */
2488
2489 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2490
2491 HRESULT rc = i_checkStateDependency(MutableStateDep);
2492 if (FAILED(rc)) return rc;
2493
2494 if (!mData->mCurrentSnapshot.isNull())
2495 return setError(E_FAIL,
2496 tr("The snapshot folder of a machine with snapshots cannot be changed (please delete all snapshots first)"));
2497
2498 Utf8Str strSnapshotFolder(aSnapshotFolder); // keep original
2499
2500 if (strSnapshotFolder.isEmpty())
2501 strSnapshotFolder = "Snapshots";
2502 int vrc = i_calculateFullPath(strSnapshotFolder,
2503 strSnapshotFolder);
2504 if (RT_FAILURE(vrc))
2505 return setError(E_FAIL,
2506 tr("Invalid snapshot folder '%s' (%Rrc)"),
2507 strSnapshotFolder.c_str(), vrc);
2508
2509 i_setModified(IsModified_MachineData);
2510 mUserData.backup();
2511
2512 i_copyPathRelativeToMachine(strSnapshotFolder, mUserData->s.strSnapshotFolder);
2513
2514 return S_OK;
2515}
2516
2517HRESULT Machine::getMediumAttachments(std::vector<ComPtr<IMediumAttachment> > &aMediumAttachments)
2518{
2519 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2520
2521 aMediumAttachments.resize(mMediaData->mAttachments.size());
2522 size_t i = 0;
2523 for (MediaData::AttachmentList::iterator it = mMediaData->mAttachments.begin();
2524 it != mMediaData->mAttachments.end(); ++it, ++i)
2525 (*it).queryInterfaceTo(aMediumAttachments[i].asOutParam());
2526
2527 return S_OK;
2528}
2529
2530HRESULT Machine::getVRDEServer(ComPtr<IVRDEServer> &aVRDEServer)
2531{
2532 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2533
2534 Assert(!!mVRDEServer);
2535
2536 mVRDEServer.queryInterfaceTo(aVRDEServer.asOutParam());
2537
2538 return S_OK;
2539}
2540
2541HRESULT Machine::getAudioAdapter(ComPtr<IAudioAdapter> &aAudioAdapter)
2542{
2543 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2544
2545 mAudioAdapter.queryInterfaceTo(aAudioAdapter.asOutParam());
2546
2547 return S_OK;
2548}
2549
2550HRESULT Machine::getUSBControllers(std::vector<ComPtr<IUSBController> > &aUSBControllers)
2551{
2552#ifdef VBOX_WITH_VUSB
2553 clearError();
2554 MultiResult rc(S_OK);
2555
2556# ifdef VBOX_WITH_USB
2557 rc = mParent->i_host()->i_checkUSBProxyService();
2558 if (FAILED(rc)) return rc;
2559# endif
2560
2561 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2562
2563 USBControllerList data = *mUSBControllers.data();
2564 aUSBControllers.resize(data.size());
2565 size_t i = 0;
2566 for (USBControllerList::iterator it = data.begin(); it != data.end(); ++i, ++it)
2567 (*it).queryInterfaceTo(aUSBControllers[i].asOutParam());
2568
2569 return S_OK;
2570#else
2571 /* Note: The GUI depends on this method returning E_NOTIMPL with no
2572 * extended error info to indicate that USB is simply not available
2573 * (w/o treating it as a failure), for example, as in OSE */
2574 NOREF(aUSBControllers);
2575 ReturnComNotImplemented();
2576#endif /* VBOX_WITH_VUSB */
2577}
2578
2579HRESULT Machine::getUSBDeviceFilters(ComPtr<IUSBDeviceFilters> &aUSBDeviceFilters)
2580{
2581#ifdef VBOX_WITH_VUSB
2582 clearError();
2583 MultiResult rc(S_OK);
2584
2585# ifdef VBOX_WITH_USB
2586 rc = mParent->i_host()->i_checkUSBProxyService();
2587 if (FAILED(rc)) return rc;
2588# endif
2589
2590 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2591
2592 return rc = mUSBDeviceFilters.queryInterfaceTo(aUSBDeviceFilters.asOutParam());
2593#else
2594 /* Note: The GUI depends on this method returning E_NOTIMPL with no
2595 * extended error info to indicate that USB is simply not available
2596 * (w/o treating it as a failure), for example, as in OSE */
2597 NOREF(aUSBDeviceFilters);
2598 ReturnComNotImplemented();
2599#endif /* VBOX_WITH_VUSB */
2600}
2601
2602HRESULT Machine::getSettingsFilePath(com::Utf8Str &aSettingsFilePath)
2603{
2604 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2605
2606 aSettingsFilePath = mData->m_strConfigFileFull;
2607
2608 return S_OK;
2609}
2610
2611HRESULT Machine::getSettingsModified(BOOL *aSettingsModified)
2612{
2613 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2614
2615 HRESULT rc = i_checkStateDependency(MutableStateDep);
2616 if (FAILED(rc)) return rc;
2617
2618 if (!mData->pMachineConfigFile->fileExists())
2619 // this is a new machine, and no config file exists yet:
2620 *aSettingsModified = TRUE;
2621 else
2622 *aSettingsModified = (mData->flModifications != 0);
2623
2624 return S_OK;
2625}
2626
2627HRESULT Machine::getSessionState(SessionState_T *aSessionState)
2628{
2629
2630 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2631
2632 *aSessionState = mData->mSession.mState;
2633
2634 return S_OK;
2635}
2636
2637HRESULT Machine::getSessionType(com::Utf8Str &aSessionType)
2638{
2639 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2640
2641 aSessionType = mData->mSession.mType;
2642
2643 return S_OK;
2644}
2645
2646HRESULT Machine::getSessionPID(ULONG *aSessionPID)
2647{
2648 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2649
2650 *aSessionPID = mData->mSession.mPID;
2651
2652 return S_OK;
2653}
2654
2655HRESULT Machine::getState(MachineState_T *aState)
2656{
2657 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2658
2659 *aState = mData->mMachineState;
2660
2661 return S_OK;
2662}
2663
2664HRESULT Machine::getLastStateChange(LONG64 *aLastStateChange)
2665{
2666 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2667
2668 *aLastStateChange = RTTimeSpecGetMilli(&mData->mLastStateChange);
2669
2670 return S_OK;
2671}
2672
2673HRESULT Machine::getStateFilePath(com::Utf8Str &aStateFilePath)
2674{
2675 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2676
2677 aStateFilePath = mSSData->strStateFilePath;
2678
2679 return S_OK;
2680}
2681
2682HRESULT Machine::getLogFolder(com::Utf8Str &aLogFolder)
2683{
2684 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2685
2686 i_getLogFolder(aLogFolder);
2687
2688 return S_OK;
2689}
2690
2691HRESULT Machine::getCurrentSnapshot(ComPtr<ISnapshot> &aCurrentSnapshot)
2692{
2693 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2694
2695 mData->mCurrentSnapshot.queryInterfaceTo(aCurrentSnapshot.asOutParam());
2696
2697 return S_OK;
2698}
2699
2700HRESULT Machine::getSnapshotCount(ULONG *aSnapshotCount)
2701{
2702 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2703
2704 *aSnapshotCount = mData->mFirstSnapshot.isNull()
2705 ? 0
2706 : mData->mFirstSnapshot->i_getAllChildrenCount() + 1;
2707
2708 return S_OK;
2709}
2710
2711HRESULT Machine::getCurrentStateModified(BOOL *aCurrentStateModified)
2712{
2713 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2714
2715 /* Note: for machines with no snapshots, we always return FALSE
2716 * (mData->mCurrentStateModified will be TRUE in this case, for historical
2717 * reasons :) */
2718
2719 *aCurrentStateModified = mData->mFirstSnapshot.isNull()
2720 ? FALSE
2721 : mData->mCurrentStateModified;
2722
2723 return S_OK;
2724}
2725
2726HRESULT Machine::getSharedFolders(std::vector<ComPtr<ISharedFolder> > &aSharedFolders)
2727{
2728 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2729
2730 aSharedFolders.resize(mHWData->mSharedFolders.size());
2731 size_t i = 0;
2732 for (std::list<ComObjPtr<SharedFolder> >::iterator it = mHWData->mSharedFolders.begin();
2733 it != mHWData->mSharedFolders.end(); ++i, ++it)
2734 (*it).queryInterfaceTo(aSharedFolders[i].asOutParam());
2735
2736 return S_OK;
2737}
2738
2739HRESULT Machine::getClipboardMode(ClipboardMode_T *aClipboardMode)
2740{
2741 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2742
2743 *aClipboardMode = mHWData->mClipboardMode;
2744
2745 return S_OK;
2746}
2747
2748HRESULT Machine::setClipboardMode(ClipboardMode_T aClipboardMode)
2749{
2750 HRESULT rc = S_OK;
2751
2752 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2753
2754 alock.release();
2755 rc = i_onClipboardModeChange(aClipboardMode);
2756 alock.acquire();
2757 if (FAILED(rc)) return rc;
2758
2759 i_setModified(IsModified_MachineData);
2760 mHWData.backup();
2761 mHWData->mClipboardMode = aClipboardMode;
2762
2763 /** Save settings if online - @todo why is this required? -- @bugref{6818} */
2764 if (Global::IsOnline(mData->mMachineState))
2765 i_saveSettings(NULL);
2766
2767 return S_OK;
2768}
2769
2770HRESULT Machine::getDnDMode(DnDMode_T *aDnDMode)
2771{
2772 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2773
2774 *aDnDMode = mHWData->mDnDMode;
2775
2776 return S_OK;
2777}
2778
2779HRESULT Machine::setDnDMode(DnDMode_T aDnDMode)
2780{
2781 HRESULT rc = S_OK;
2782
2783 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2784
2785 alock.release();
2786 rc = i_onDnDModeChange(aDnDMode);
2787
2788 alock.acquire();
2789 if (FAILED(rc)) return rc;
2790
2791 i_setModified(IsModified_MachineData);
2792 mHWData.backup();
2793 mHWData->mDnDMode = aDnDMode;
2794
2795 /** Save settings if online - @todo why is this required? -- @bugref{6818} */
2796 if (Global::IsOnline(mData->mMachineState))
2797 i_saveSettings(NULL);
2798
2799 return S_OK;
2800}
2801
2802HRESULT Machine::getGuestPropertyNotificationPatterns(com::Utf8Str &aGuestPropertyNotificationPatterns)
2803{
2804 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2805
2806 try
2807 {
2808 aGuestPropertyNotificationPatterns = mHWData->mGuestPropertyNotificationPatterns;
2809 }
2810 catch (...)
2811 {
2812 return VirtualBoxBase::handleUnexpectedExceptions(this, RT_SRC_POS);
2813 }
2814
2815 return S_OK;
2816}
2817
2818HRESULT Machine::setGuestPropertyNotificationPatterns(const com::Utf8Str &aGuestPropertyNotificationPatterns)
2819{
2820 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2821
2822 HRESULT rc = i_checkStateDependency(MutableStateDep);
2823 if (FAILED(rc)) return rc;
2824
2825 i_setModified(IsModified_MachineData);
2826 mHWData.backup();
2827 mHWData->mGuestPropertyNotificationPatterns = aGuestPropertyNotificationPatterns;
2828 return rc;
2829}
2830
2831HRESULT Machine::getStorageControllers(std::vector<ComPtr<IStorageController> > &aStorageControllers)
2832{
2833 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2834 StorageControllerList data = *mStorageControllers.data();
2835 size_t i = 0;
2836 aStorageControllers.resize(data.size());
2837 for (StorageControllerList::iterator it = data.begin(); it != data.end(); ++it, ++i)
2838 (*it).queryInterfaceTo(aStorageControllers[i].asOutParam());
2839 return S_OK;
2840}
2841
2842HRESULT Machine::getTeleporterEnabled(BOOL *aEnabled)
2843{
2844 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2845
2846 *aEnabled = mUserData->s.fTeleporterEnabled;
2847
2848 return S_OK;
2849}
2850
2851HRESULT Machine::setTeleporterEnabled(BOOL aTeleporterEnabled)
2852{
2853 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2854
2855 /* Only allow it to be set to true when PoweredOff or Aborted.
2856 (Clearing it is always permitted.) */
2857 if ( aTeleporterEnabled
2858 && mData->mRegistered
2859 && ( !i_isSessionMachine()
2860 || ( mData->mMachineState != MachineState_PoweredOff
2861 && mData->mMachineState != MachineState_Teleported
2862 && mData->mMachineState != MachineState_Aborted
2863 )
2864 )
2865 )
2866 return setError(VBOX_E_INVALID_VM_STATE,
2867 tr("The machine is not powered off (state is %s)"),
2868 Global::stringifyMachineState(mData->mMachineState));
2869
2870 i_setModified(IsModified_MachineData);
2871 mUserData.backup();
2872 mUserData->s.fTeleporterEnabled = !! aTeleporterEnabled;
2873
2874 return S_OK;
2875}
2876
2877HRESULT Machine::getTeleporterPort(ULONG *aTeleporterPort)
2878{
2879 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2880
2881 *aTeleporterPort = (ULONG)mUserData->s.uTeleporterPort;
2882
2883 return S_OK;
2884}
2885
2886HRESULT Machine::setTeleporterPort(ULONG aTeleporterPort)
2887{
2888 if (aTeleporterPort >= _64K)
2889 return setError(E_INVALIDARG, tr("Invalid port number %d"), aTeleporterPort);
2890
2891 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2892
2893 HRESULT rc = i_checkStateDependency(MutableStateDep);
2894 if (FAILED(rc)) return rc;
2895
2896 i_setModified(IsModified_MachineData);
2897 mUserData.backup();
2898 mUserData->s.uTeleporterPort = (uint32_t)aTeleporterPort;
2899
2900 return S_OK;
2901}
2902
2903HRESULT Machine::getTeleporterAddress(com::Utf8Str &aTeleporterAddress)
2904{
2905 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2906
2907 aTeleporterAddress = mUserData->s.strTeleporterAddress;
2908
2909 return S_OK;
2910}
2911
2912HRESULT Machine::setTeleporterAddress(const com::Utf8Str &aTeleporterAddress)
2913{
2914 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2915
2916 HRESULT rc = i_checkStateDependency(MutableStateDep);
2917 if (FAILED(rc)) return rc;
2918
2919 i_setModified(IsModified_MachineData);
2920 mUserData.backup();
2921 mUserData->s.strTeleporterAddress = aTeleporterAddress;
2922
2923 return S_OK;
2924}
2925
2926HRESULT Machine::getTeleporterPassword(com::Utf8Str &aTeleporterPassword)
2927{
2928 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2929 aTeleporterPassword = mUserData->s.strTeleporterPassword;
2930
2931 return S_OK;
2932}
2933
2934HRESULT Machine::setTeleporterPassword(const com::Utf8Str &aTeleporterPassword)
2935{
2936 /*
2937 * Hash the password first.
2938 */
2939 com::Utf8Str aT = aTeleporterPassword;
2940
2941 if (!aT.isEmpty())
2942 {
2943 if (VBoxIsPasswordHashed(&aT))
2944 return setError(E_INVALIDARG, tr("Cannot set an already hashed password, only plain text password please"));
2945 VBoxHashPassword(&aT);
2946 }
2947
2948 /*
2949 * Do the update.
2950 */
2951 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2952 HRESULT hrc = i_checkStateDependency(MutableStateDep);
2953 if (SUCCEEDED(hrc))
2954 {
2955 i_setModified(IsModified_MachineData);
2956 mUserData.backup();
2957 mUserData->s.strTeleporterPassword = aT;
2958 }
2959
2960 return hrc;
2961}
2962
2963HRESULT Machine::getFaultToleranceState(FaultToleranceState_T *aFaultToleranceState)
2964{
2965 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2966
2967 *aFaultToleranceState = mUserData->s.enmFaultToleranceState;
2968 return S_OK;
2969}
2970
2971HRESULT Machine::setFaultToleranceState(FaultToleranceState_T aFaultToleranceState)
2972{
2973 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2974
2975 /* @todo deal with running state change. */
2976 HRESULT rc = i_checkStateDependency(MutableStateDep);
2977 if (FAILED(rc)) return rc;
2978
2979 i_setModified(IsModified_MachineData);
2980 mUserData.backup();
2981 mUserData->s.enmFaultToleranceState = aFaultToleranceState;
2982 return S_OK;
2983}
2984
2985HRESULT Machine::getFaultToleranceAddress(com::Utf8Str &aFaultToleranceAddress)
2986{
2987 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2988
2989 aFaultToleranceAddress = mUserData->s.strFaultToleranceAddress;
2990 return S_OK;
2991}
2992
2993HRESULT Machine::setFaultToleranceAddress(const com::Utf8Str &aFaultToleranceAddress)
2994{
2995 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2996
2997 /* @todo deal with running state change. */
2998 HRESULT rc = i_checkStateDependency(MutableStateDep);
2999 if (FAILED(rc)) return rc;
3000
3001 i_setModified(IsModified_MachineData);
3002 mUserData.backup();
3003 mUserData->s.strFaultToleranceAddress = aFaultToleranceAddress;
3004 return S_OK;
3005}
3006
3007HRESULT Machine::getFaultTolerancePort(ULONG *aFaultTolerancePort)
3008{
3009 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
3010
3011 *aFaultTolerancePort = mUserData->s.uFaultTolerancePort;
3012 return S_OK;
3013}
3014
3015HRESULT Machine::setFaultTolerancePort(ULONG aFaultTolerancePort)
3016{
3017 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
3018
3019 /* @todo deal with running state change. */
3020 HRESULT rc = i_checkStateDependency(MutableStateDep);
3021 if (FAILED(rc)) return rc;
3022
3023 i_setModified(IsModified_MachineData);
3024 mUserData.backup();
3025 mUserData->s.uFaultTolerancePort = aFaultTolerancePort;
3026 return S_OK;
3027}
3028
3029HRESULT Machine::getFaultTolerancePassword(com::Utf8Str &aFaultTolerancePassword)
3030{
3031 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
3032
3033 aFaultTolerancePassword = mUserData->s.strFaultTolerancePassword;
3034
3035 return S_OK;
3036}
3037
3038HRESULT Machine::setFaultTolerancePassword(const com::Utf8Str &aFaultTolerancePassword)
3039{
3040 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
3041
3042 /* @todo deal with running state change. */
3043 HRESULT rc = i_checkStateDependency(MutableStateDep);
3044 if (FAILED(rc)) return rc;
3045
3046 i_setModified(IsModified_MachineData);
3047 mUserData.backup();
3048 mUserData->s.strFaultTolerancePassword = aFaultTolerancePassword;
3049
3050 return S_OK;
3051}
3052
3053HRESULT Machine::getFaultToleranceSyncInterval(ULONG *aFaultToleranceSyncInterval)
3054{
3055 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
3056
3057 *aFaultToleranceSyncInterval = mUserData->s.uFaultToleranceInterval;
3058 return S_OK;
3059}
3060
3061HRESULT Machine::setFaultToleranceSyncInterval(ULONG aFaultToleranceSyncInterval)
3062{
3063 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
3064
3065 /* @todo deal with running state change. */
3066 HRESULT rc = i_checkStateDependency(MutableStateDep);
3067 if (FAILED(rc)) return rc;
3068
3069 i_setModified(IsModified_MachineData);
3070 mUserData.backup();
3071 mUserData->s.uFaultToleranceInterval = aFaultToleranceSyncInterval;
3072 return S_OK;
3073}
3074
3075HRESULT Machine::getRTCUseUTC(BOOL *aRTCUseUTC)
3076{
3077 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
3078
3079 *aRTCUseUTC = mUserData->s.fRTCUseUTC;
3080
3081 return S_OK;
3082}
3083
3084HRESULT Machine::setRTCUseUTC(BOOL aRTCUseUTC)
3085{
3086 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
3087
3088 /* Only allow it to be set to true when PoweredOff or Aborted.
3089 (Clearing it is always permitted.) */
3090 if ( aRTCUseUTC
3091 && mData->mRegistered
3092 && ( !i_isSessionMachine()
3093 || ( mData->mMachineState != MachineState_PoweredOff
3094 && mData->mMachineState != MachineState_Teleported
3095 && mData->mMachineState != MachineState_Aborted
3096 )
3097 )
3098 )
3099 return setError(VBOX_E_INVALID_VM_STATE,
3100 tr("The machine is not powered off (state is %s)"),
3101 Global::stringifyMachineState(mData->mMachineState));
3102
3103 i_setModified(IsModified_MachineData);
3104 mUserData.backup();
3105 mUserData->s.fRTCUseUTC = !!aRTCUseUTC;
3106
3107 return S_OK;
3108}
3109
3110HRESULT Machine::getIOCacheEnabled(BOOL *aIOCacheEnabled)
3111{
3112 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
3113
3114 *aIOCacheEnabled = mHWData->mIOCacheEnabled;
3115
3116 return S_OK;
3117}
3118
3119HRESULT Machine::setIOCacheEnabled(BOOL aIOCacheEnabled)
3120{
3121 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
3122
3123 HRESULT rc = i_checkStateDependency(MutableStateDep);
3124 if (FAILED(rc)) return rc;
3125
3126 i_setModified(IsModified_MachineData);
3127 mHWData.backup();
3128 mHWData->mIOCacheEnabled = aIOCacheEnabled;
3129
3130 return S_OK;
3131}
3132
3133HRESULT Machine::getIOCacheSize(ULONG *aIOCacheSize)
3134{
3135 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
3136
3137 *aIOCacheSize = mHWData->mIOCacheSize;
3138
3139 return S_OK;
3140}
3141
3142HRESULT Machine::setIOCacheSize(ULONG aIOCacheSize)
3143{
3144 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
3145
3146 HRESULT rc = i_checkStateDependency(MutableStateDep);
3147 if (FAILED(rc)) return rc;
3148
3149 i_setModified(IsModified_MachineData);
3150 mHWData.backup();
3151 mHWData->mIOCacheSize = aIOCacheSize;
3152
3153 return S_OK;
3154}
3155
3156
3157/**
3158 * @note Locks objects!
3159 */
3160HRESULT Machine::lockMachine(const ComPtr<ISession> &aSession,
3161 LockType_T aLockType)
3162
3163{
3164 /* check the session state */
3165 SessionState_T state;
3166 HRESULT rc = aSession->COMGETTER(State)(&state);
3167 if (FAILED(rc)) return rc;
3168
3169 if (state != SessionState_Unlocked)
3170 return setError(VBOX_E_INVALID_OBJECT_STATE,
3171 tr("The given session is busy"));
3172
3173 // get the client's IInternalSessionControl interface
3174 ComPtr<IInternalSessionControl> pSessionControl = aSession;
3175 ComAssertMsgRet(!!pSessionControl, ("No IInternalSessionControl interface"),
3176 E_INVALIDARG);
3177
3178 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
3179
3180 if (!mData->mRegistered)
3181 return setError(E_UNEXPECTED,
3182 tr("The machine '%s' is not registered"),
3183 mUserData->s.strName.c_str());
3184
3185 LogFlowThisFunc(("mSession.mState=%s\n", Global::stringifySessionState(mData->mSession.mState)));
3186
3187 SessionState_T oldState = mData->mSession.mState;
3188 /* Hack: in case the session is closing and there is a progress object
3189 * which allows waiting for the session to be closed, take the opportunity
3190 * and do a limited wait (max. 1 second). This helps a lot when the system
3191 * is busy and thus session closing can take a little while. */
3192 if ( mData->mSession.mState == SessionState_Unlocking
3193 && mData->mSession.mProgress)
3194 {
3195 alock.release();
3196 mData->mSession.mProgress->WaitForCompletion(1000);
3197 alock.acquire();
3198 LogFlowThisFunc(("after waiting: mSession.mState=%s\n", Global::stringifySessionState(mData->mSession.mState)));
3199 }
3200
3201 // try again now
3202 if ( (mData->mSession.mState == SessionState_Locked) // machine is write-locked already
3203 // (i.e. session machine exists)
3204 && (aLockType == LockType_Shared) // caller wants a shared link to the
3205 // existing session that holds the write lock:
3206 )
3207 {
3208 // OK, share the session... we are now dealing with three processes:
3209 // 1) VBoxSVC (where this code runs);
3210 // 2) process C: the caller's client process (who wants a shared session);
3211 // 3) process W: the process which already holds the write lock on the machine (write-locking session)
3212
3213 // copy pointers to W (the write-locking session) before leaving lock (these must not be NULL)
3214 ComPtr<IInternalSessionControl> pSessionW = mData->mSession.mDirectControl;
3215 ComAssertRet(!pSessionW.isNull(), E_FAIL);
3216 ComObjPtr<SessionMachine> pSessionMachine = mData->mSession.mMachine;
3217 AssertReturn(!pSessionMachine.isNull(), E_FAIL);
3218
3219 /*
3220 * Release the lock before calling the client process. It's safe here
3221 * since the only thing to do after we get the lock again is to add
3222 * the remote control to the list (which doesn't directly influence
3223 * anything).
3224 */
3225 alock.release();
3226
3227 // get the console of the session holding the write lock (this is a remote call)
3228 ComPtr<IConsole> pConsoleW;
3229 LogFlowThisFunc(("Calling GetRemoteConsole()...\n"));
3230 rc = pSessionW->GetRemoteConsole(pConsoleW.asOutParam());
3231 LogFlowThisFunc(("GetRemoteConsole() returned %08X\n", rc));
3232 if (FAILED(rc))
3233 // the failure may occur w/o any error info (from RPC), so provide one
3234 return setError(VBOX_E_VM_ERROR,
3235 tr("Failed to get a console object from the direct session (%Rhrc)"), rc);
3236
3237 ComAssertRet(!pConsoleW.isNull(), E_FAIL);
3238
3239 // share the session machine and W's console with the caller's session
3240 LogFlowThisFunc(("Calling AssignRemoteMachine()...\n"));
3241 rc = pSessionControl->AssignRemoteMachine(pSessionMachine, pConsoleW);
3242 LogFlowThisFunc(("AssignRemoteMachine() returned %08X\n", rc));
3243
3244 if (FAILED(rc))
3245 // the failure may occur w/o any error info (from RPC), so provide one
3246 return setError(VBOX_E_VM_ERROR,
3247 tr("Failed to assign the machine to the session (%Rhrc)"), rc);
3248 alock.acquire();
3249
3250 // need to revalidate the state after acquiring the lock again
3251 if (mData->mSession.mState != SessionState_Locked)
3252 {
3253 pSessionControl->Uninitialize();
3254 return setError(VBOX_E_INVALID_SESSION_STATE,
3255 tr("The machine '%s' was unlocked unexpectedly while attempting to share its session"),
3256 mUserData->s.strName.c_str());
3257 }
3258
3259 // add the caller's session to the list
3260 mData->mSession.mRemoteControls.push_back(pSessionControl);
3261 }
3262 else if ( mData->mSession.mState == SessionState_Locked
3263 || mData->mSession.mState == SessionState_Unlocking
3264 )
3265 {
3266 // sharing not permitted, or machine still unlocking:
3267 return setError(VBOX_E_INVALID_OBJECT_STATE,
3268 tr("The machine '%s' is already locked for a session (or being unlocked)"),
3269 mUserData->s.strName.c_str());
3270 }
3271 else
3272 {
3273 // machine is not locked: then write-lock the machine (create the session machine)
3274
3275 // must not be busy
3276 AssertReturn(!Global::IsOnlineOrTransient(mData->mMachineState), E_FAIL);
3277
3278 // get the caller's session PID
3279 RTPROCESS pid = NIL_RTPROCESS;
3280 AssertCompile(sizeof(ULONG) == sizeof(RTPROCESS));
3281 pSessionControl->GetPID((ULONG*)&pid);
3282 Assert(pid != NIL_RTPROCESS);
3283
3284 bool fLaunchingVMProcess = (mData->mSession.mState == SessionState_Spawning);
3285
3286 if (fLaunchingVMProcess)
3287 {
3288 if (mData->mSession.mPID == NIL_RTPROCESS)
3289 {
3290 // two or more clients racing for a lock, the one which set the
3291 // session state to Spawning will win, the others will get an
3292 // error as we can't decide here if waiting a little would help
3293 // (only for shared locks this would avoid an error)
3294 return setError(VBOX_E_INVALID_OBJECT_STATE,
3295 tr("The machine '%s' already has a lock request pending"),
3296 mUserData->s.strName.c_str());
3297 }
3298
3299 // this machine is awaiting for a spawning session to be opened:
3300 // then the calling process must be the one that got started by
3301 // LaunchVMProcess()
3302
3303 LogFlowThisFunc(("mSession.mPID=%d(0x%x)\n", mData->mSession.mPID, mData->mSession.mPID));
3304 LogFlowThisFunc(("session.pid=%d(0x%x)\n", pid, pid));
3305
3306#if defined(VBOX_WITH_HARDENING) && defined(RT_OS_WINDOWS)
3307 /* Hardened windows builds spawns three processes when a VM is
3308 launched, the 3rd one is the one that will end up here. */
3309 RTPROCESS ppid;
3310 int rc = RTProcQueryParent(pid, &ppid);
3311 if (RT_SUCCESS(rc))
3312 rc = RTProcQueryParent(ppid, &ppid);
3313 if ( (RT_SUCCESS(rc) && mData->mSession.mPID == ppid)
3314 || rc == VERR_ACCESS_DENIED)
3315 {
3316 LogFlowThisFunc(("mSession.mPID => %d(%#x) - windows hardening stub\n", mData->mSession.mPID, pid));
3317 mData->mSession.mPID = pid;
3318 }
3319#endif
3320
3321 if (mData->mSession.mPID != pid)
3322 return setError(E_ACCESSDENIED,
3323 tr("An unexpected process (PID=0x%08X) has tried to lock the "
3324 "machine '%s', while only the process started by LaunchVMProcess (PID=0x%08X) is allowed"),
3325 pid, mUserData->s.strName.c_str(), mData->mSession.mPID);
3326 }
3327
3328 // create the mutable SessionMachine from the current machine
3329 ComObjPtr<SessionMachine> sessionMachine;
3330 sessionMachine.createObject();
3331 rc = sessionMachine->init(this);
3332 AssertComRC(rc);
3333
3334 /* NOTE: doing return from this function after this point but
3335 * before the end is forbidden since it may call SessionMachine::uninit()
3336 * (through the ComObjPtr's destructor) which requests the VirtualBox write
3337 * lock while still holding the Machine lock in alock so that a deadlock
3338 * is possible due to the wrong lock order. */
3339
3340 if (SUCCEEDED(rc))
3341 {
3342 /*
3343 * Set the session state to Spawning to protect against subsequent
3344 * attempts to open a session and to unregister the machine after
3345 * we release the lock.
3346 */
3347 SessionState_T origState = mData->mSession.mState;
3348 mData->mSession.mState = SessionState_Spawning;
3349
3350#ifndef VBOX_WITH_GENERIC_SESSION_WATCHER
3351 /* Get the client token ID to be passed to the client process */
3352 Utf8Str strTokenId;
3353 sessionMachine->i_getTokenId(strTokenId);
3354 Assert(!strTokenId.isEmpty());
3355#else /* VBOX_WITH_GENERIC_SESSION_WATCHER */
3356 /* Get the client token to be passed to the client process */
3357 ComPtr<IToken> pToken(sessionMachine->i_getToken());
3358 /* The token is now "owned" by pToken, fix refcount */
3359 if (!pToken.isNull())
3360 pToken->Release();
3361#endif /* VBOX_WITH_GENERIC_SESSION_WATCHER */
3362
3363 /*
3364 * Release the lock before calling the client process -- it will call
3365 * Machine/SessionMachine methods. Releasing the lock here is quite safe
3366 * because the state is Spawning, so that LaunchVMProcess() and
3367 * LockMachine() calls will fail. This method, called before we
3368 * acquire the lock again, will fail because of the wrong PID.
3369 *
3370 * Note that mData->mSession.mRemoteControls accessed outside
3371 * the lock may not be modified when state is Spawning, so it's safe.
3372 */
3373 alock.release();
3374
3375 LogFlowThisFunc(("Calling AssignMachine()...\n"));
3376#ifndef VBOX_WITH_GENERIC_SESSION_WATCHER
3377 rc = pSessionControl->AssignMachine(sessionMachine, aLockType, Bstr(strTokenId).raw());
3378#else /* VBOX_WITH_GENERIC_SESSION_WATCHER */
3379 rc = pSessionControl->AssignMachine(sessionMachine, aLockType, pToken);
3380 /* Now the token is owned by the client process. */
3381 pToken.setNull();
3382#endif /* VBOX_WITH_GENERIC_SESSION_WATCHER */
3383 LogFlowThisFunc(("AssignMachine() returned %08X\n", rc));
3384
3385 /* The failure may occur w/o any error info (from RPC), so provide one */
3386 if (FAILED(rc))
3387 setError(VBOX_E_VM_ERROR,
3388 tr("Failed to assign the machine to the session (%Rhrc)"), rc);
3389
3390 if ( SUCCEEDED(rc)
3391 && fLaunchingVMProcess
3392 )
3393 {
3394 /* complete the remote session initialization */
3395
3396 /* get the console from the direct session */
3397 ComPtr<IConsole> console;
3398 rc = pSessionControl->GetRemoteConsole(console.asOutParam());
3399 ComAssertComRC(rc);
3400
3401 if (SUCCEEDED(rc) && !console)
3402 {
3403 ComAssert(!!console);
3404 rc = E_FAIL;
3405 }
3406
3407 /* assign machine & console to the remote session */
3408 if (SUCCEEDED(rc))
3409 {
3410 /*
3411 * after LaunchVMProcess(), the first and the only
3412 * entry in remoteControls is that remote session
3413 */
3414 LogFlowThisFunc(("Calling AssignRemoteMachine()...\n"));
3415 rc = mData->mSession.mRemoteControls.front()->AssignRemoteMachine(sessionMachine, console);
3416 LogFlowThisFunc(("AssignRemoteMachine() returned %08X\n", rc));
3417
3418 /* The failure may occur w/o any error info (from RPC), so provide one */
3419 if (FAILED(rc))
3420 setError(VBOX_E_VM_ERROR,
3421 tr("Failed to assign the machine to the remote session (%Rhrc)"), rc);
3422 }
3423
3424 if (FAILED(rc))
3425 pSessionControl->Uninitialize();
3426 }
3427
3428 /* acquire the lock again */
3429 alock.acquire();
3430
3431 /* Restore the session state */
3432 mData->mSession.mState = origState;
3433 }
3434
3435 // finalize spawning anyway (this is why we don't return on errors above)
3436 if (fLaunchingVMProcess)
3437 {
3438 /* Note that the progress object is finalized later */
3439 /** @todo Consider checking mData->mSession.mProgress for cancellation
3440 * around here. */
3441
3442 /* We don't reset mSession.mPID here because it is necessary for
3443 * SessionMachine::uninit() to reap the child process later. */
3444
3445 if (FAILED(rc))
3446 {
3447 /* Close the remote session, remove the remote control from the list
3448 * and reset session state to Closed (@note keep the code in sync
3449 * with the relevant part in checkForSpawnFailure()). */
3450
3451 Assert(mData->mSession.mRemoteControls.size() == 1);
3452 if (mData->mSession.mRemoteControls.size() == 1)
3453 {
3454 ErrorInfoKeeper eik;
3455 mData->mSession.mRemoteControls.front()->Uninitialize();
3456 }
3457
3458 mData->mSession.mRemoteControls.clear();
3459 mData->mSession.mState = SessionState_Unlocked;
3460 }
3461 }
3462 else
3463 {
3464 /* memorize PID of the directly opened session */
3465 if (SUCCEEDED(rc))
3466 mData->mSession.mPID = pid;
3467 }
3468
3469 if (SUCCEEDED(rc))
3470 {
3471 /* memorize the direct session control and cache IUnknown for it */
3472 mData->mSession.mDirectControl = pSessionControl;
3473 mData->mSession.mState = SessionState_Locked;
3474 /* associate the SessionMachine with this Machine */
3475 mData->mSession.mMachine = sessionMachine;
3476
3477 /* request an IUnknown pointer early from the remote party for later
3478 * identity checks (it will be internally cached within mDirectControl
3479 * at least on XPCOM) */
3480 ComPtr<IUnknown> unk = mData->mSession.mDirectControl;
3481 NOREF(unk);
3482 }
3483
3484 /* Release the lock since SessionMachine::uninit() locks VirtualBox which
3485 * would break the lock order */
3486 alock.release();
3487
3488 /* uninitialize the created session machine on failure */
3489 if (FAILED(rc))
3490 sessionMachine->uninit();
3491
3492 }
3493
3494 if (SUCCEEDED(rc))
3495 {
3496 /*
3497 * tell the client watcher thread to update the set of
3498 * machines that have open sessions
3499 */
3500 mParent->i_updateClientWatcher();
3501
3502 if (oldState != SessionState_Locked)
3503 /* fire an event */
3504 mParent->i_onSessionStateChange(i_getId(), SessionState_Locked);
3505 }
3506
3507 return rc;
3508}
3509
3510/**
3511 * @note Locks objects!
3512 */
3513HRESULT Machine::launchVMProcess(const ComPtr<ISession> &aSession,
3514 const com::Utf8Str &aType,
3515 const com::Utf8Str &aEnvironment,
3516 ComPtr<IProgress> &aProgress)
3517{
3518 Utf8Str strFrontend(aType);
3519 Utf8Str strEnvironment(aEnvironment);
3520 /* "emergencystop" doesn't need the session, so skip the checks/interface
3521 * retrieval. This code doesn't quite fit in here, but introducing a
3522 * special API method would be even more effort, and would require explicit
3523 * support by every API client. It's better to hide the feature a bit. */
3524 if (strFrontend != "emergencystop")
3525 CheckComArgNotNull(aSession);
3526
3527 HRESULT rc = S_OK;
3528 if (strFrontend.isEmpty())
3529 {
3530 Bstr bstrFrontend;
3531 rc = COMGETTER(DefaultFrontend)(bstrFrontend.asOutParam());
3532 if (FAILED(rc))
3533 return rc;
3534 strFrontend = bstrFrontend;
3535 if (strFrontend.isEmpty())
3536 {
3537 ComPtr<ISystemProperties> systemProperties;
3538 rc = mParent->COMGETTER(SystemProperties)(systemProperties.asOutParam());
3539 if (FAILED(rc))
3540 return rc;
3541 rc = systemProperties->COMGETTER(DefaultFrontend)(bstrFrontend.asOutParam());
3542 if (FAILED(rc))
3543 return rc;
3544 strFrontend = bstrFrontend;
3545 }
3546 /* paranoia - emergencystop is not a valid default */
3547 if (strFrontend == "emergencystop")
3548 strFrontend = Utf8Str::Empty;
3549 }
3550 /* default frontend: Qt GUI */
3551 if (strFrontend.isEmpty())
3552 strFrontend = "GUI/Qt";
3553
3554 if (strFrontend != "emergencystop")
3555 {
3556 /* check the session state */
3557 SessionState_T state;
3558 rc = aSession->COMGETTER(State)(&state);
3559 if (FAILED(rc))
3560 return rc;
3561
3562 if (state != SessionState_Unlocked)
3563 return setError(VBOX_E_INVALID_OBJECT_STATE,
3564 tr("The given session is busy"));
3565
3566 /* get the IInternalSessionControl interface */
3567 ComPtr<IInternalSessionControl> control(aSession);
3568 ComAssertMsgRet(!control.isNull(),
3569 ("No IInternalSessionControl interface"),
3570 E_INVALIDARG);
3571
3572 /* get the teleporter enable state for the progress object init. */
3573 BOOL fTeleporterEnabled;
3574 rc = COMGETTER(TeleporterEnabled)(&fTeleporterEnabled);
3575 if (FAILED(rc))
3576 return rc;
3577
3578 /* create a progress object */
3579 ComObjPtr<ProgressProxy> progress;
3580 progress.createObject();
3581 rc = progress->init(mParent,
3582 static_cast<IMachine*>(this),
3583 Bstr(tr("Starting VM")).raw(),
3584 TRUE /* aCancelable */,
3585 fTeleporterEnabled ? 20 : 10 /* uTotalOperationsWeight */,
3586 BstrFmt(tr("Creating process for virtual machine \"%s\" (%s)"),
3587 mUserData->s.strName.c_str(), strFrontend.c_str()).raw(),
3588 2 /* uFirstOperationWeight */,
3589 fTeleporterEnabled ? 3 : 1 /* cOtherProgressObjectOperations */);
3590
3591 if (SUCCEEDED(rc))
3592 {
3593 rc = i_launchVMProcess(control, strFrontend, strEnvironment, progress);
3594 if (SUCCEEDED(rc))
3595 {
3596 progress.queryInterfaceTo(aProgress.asOutParam());
3597
3598 /* signal the client watcher thread */
3599 mParent->i_updateClientWatcher();
3600
3601 /* fire an event */
3602 mParent->i_onSessionStateChange(i_getId(), SessionState_Spawning);
3603 }
3604 }
3605 }
3606 else
3607 {
3608 /* no progress object - either instant success or failure */
3609 aProgress = NULL;
3610
3611 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
3612
3613 if (mData->mSession.mState != SessionState_Locked)
3614 return setError(VBOX_E_INVALID_OBJECT_STATE,
3615 tr("The machine '%s' is not locked by a session"),
3616 mUserData->s.strName.c_str());
3617
3618 /* must have a VM process associated - do not kill normal API clients
3619 * with an open session */
3620 if (!Global::IsOnline(mData->mMachineState))
3621 return setError(VBOX_E_INVALID_OBJECT_STATE,
3622 tr("The machine '%s' does not have a VM process"),
3623 mUserData->s.strName.c_str());
3624
3625 /* forcibly terminate the VM process */
3626 if (mData->mSession.mPID != NIL_RTPROCESS)
3627 RTProcTerminate(mData->mSession.mPID);
3628
3629 /* signal the client watcher thread, as most likely the client has
3630 * been terminated */
3631 mParent->i_updateClientWatcher();
3632 }
3633
3634 return rc;
3635}
3636
3637HRESULT Machine::setBootOrder(ULONG aPosition, DeviceType_T aDevice)
3638{
3639 if (aPosition < 1 || aPosition > SchemaDefs::MaxBootPosition)
3640 return setError(E_INVALIDARG,
3641 tr("Invalid boot position: %lu (must be in range [1, %lu])"),
3642 aPosition, SchemaDefs::MaxBootPosition);
3643
3644 if (aDevice == DeviceType_USB)
3645 return setError(E_NOTIMPL,
3646 tr("Booting from USB device is currently not supported"));
3647
3648 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
3649
3650 HRESULT rc = i_checkStateDependency(MutableStateDep);
3651 if (FAILED(rc)) return rc;
3652
3653 i_setModified(IsModified_MachineData);
3654 mHWData.backup();
3655 mHWData->mBootOrder[aPosition - 1] = aDevice;
3656
3657 return S_OK;
3658}
3659
3660HRESULT Machine::getBootOrder(ULONG aPosition, DeviceType_T *aDevice)
3661{
3662 if (aPosition < 1 || aPosition > SchemaDefs::MaxBootPosition)
3663 return setError(E_INVALIDARG,
3664 tr("Invalid boot position: %lu (must be in range [1, %lu])"),
3665 aPosition, SchemaDefs::MaxBootPosition);
3666
3667 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
3668
3669 *aDevice = mHWData->mBootOrder[aPosition - 1];
3670
3671 return S_OK;
3672}
3673
3674HRESULT Machine::attachDevice(const com::Utf8Str &aName,
3675 LONG aControllerPort,
3676 LONG aDevice,
3677 DeviceType_T aType,
3678 const ComPtr<IMedium> &aMedium)
3679{
3680 IMedium *aM = aMedium;
3681 LogFlowThisFunc(("aControllerName=\"%s\" aControllerPort=%d aDevice=%d aType=%d aMedium=%p\n",
3682 aName.c_str(), aControllerPort, aDevice, aType, aM));
3683
3684 // request the host lock first, since might be calling Host methods for getting host drives;
3685 // next, protect the media tree all the while we're in here, as well as our member variables
3686 AutoMultiWriteLock2 alock(mParent->i_host(), this COMMA_LOCKVAL_SRC_POS);
3687 AutoWriteLock treeLock(&mParent->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
3688
3689 HRESULT rc = i_checkStateDependency(MutableStateDep);
3690 if (FAILED(rc)) return rc;
3691
3692 /// @todo NEWMEDIA implicit machine registration
3693 if (!mData->mRegistered)
3694 return setError(VBOX_E_INVALID_OBJECT_STATE,
3695 tr("Cannot attach storage devices to an unregistered machine"));
3696
3697 AssertReturn(mData->mMachineState != MachineState_Saved, E_FAIL);
3698
3699 /* Check for an existing controller. */
3700 ComObjPtr<StorageController> ctl;
3701 rc = i_getStorageControllerByName(aName, ctl, true /* aSetError */);
3702 if (FAILED(rc)) return rc;
3703
3704 StorageControllerType_T ctrlType;
3705 rc = ctl->COMGETTER(ControllerType)(&ctrlType);
3706 if (FAILED(rc))
3707 return setError(E_FAIL,
3708 tr("Could not get type of controller '%s'"),
3709 aName.c_str());
3710
3711 bool fSilent = false;
3712 Utf8Str strReconfig;
3713
3714 /* Check whether the flag to allow silent storage attachment reconfiguration is set. */
3715 strReconfig = i_getExtraData(Utf8Str("VBoxInternal2/SilentReconfigureWhilePaused"));
3716 if ( mData->mMachineState == MachineState_Paused
3717 && strReconfig == "1")
3718 fSilent = true;
3719
3720 /* Check that the controller can do hotplugging if we detach the device while the VM is running. */
3721 bool fHotplug = false;
3722 if (!fSilent && Global::IsOnlineOrTransient(mData->mMachineState))
3723 fHotplug = true;
3724
3725 if (fHotplug && !i_isControllerHotplugCapable(ctrlType))
3726 return setError(VBOX_E_INVALID_VM_STATE,
3727 tr("Controller '%s' does not support hotplugging"),
3728 aName.c_str());
3729
3730 // check that the port and device are not out of range
3731 rc = ctl->i_checkPortAndDeviceValid(aControllerPort, aDevice);
3732 if (FAILED(rc)) return rc;
3733
3734 /* check if the device slot is already busy */
3735 MediumAttachment *pAttachTemp;
3736 if ((pAttachTemp = i_findAttachment(mMediaData->mAttachments,
3737 Bstr(aName).raw(),
3738 aControllerPort,
3739 aDevice)))
3740 {
3741 Medium *pMedium = pAttachTemp->i_getMedium();
3742 if (pMedium)
3743 {
3744 AutoReadLock mediumLock(pMedium COMMA_LOCKVAL_SRC_POS);
3745 return setError(VBOX_E_OBJECT_IN_USE,
3746 tr("Medium '%s' is already attached to port %d, device %d of controller '%s' of this virtual machine"),
3747 pMedium->i_getLocationFull().c_str(),
3748 aControllerPort,
3749 aDevice,
3750 aName.c_str());
3751 }
3752 else
3753 return setError(VBOX_E_OBJECT_IN_USE,
3754 tr("Device is already attached to port %d, device %d of controller '%s' of this virtual machine"),
3755 aControllerPort, aDevice, aName.c_str());
3756 }
3757
3758 ComObjPtr<Medium> medium = static_cast<Medium*>(aM);
3759 if (aMedium && medium.isNull())
3760 return setError(E_INVALIDARG, "The given medium pointer is invalid");
3761
3762 AutoCaller mediumCaller(medium);
3763 if (FAILED(mediumCaller.rc())) return mediumCaller.rc();
3764
3765 AutoWriteLock mediumLock(medium COMMA_LOCKVAL_SRC_POS);
3766
3767 if ( (pAttachTemp = i_findAttachment(mMediaData->mAttachments, medium))
3768 && !medium.isNull()
3769 )
3770 return setError(VBOX_E_OBJECT_IN_USE,
3771 tr("Medium '%s' is already attached to this virtual machine"),
3772 medium->i_getLocationFull().c_str());
3773
3774 if (!medium.isNull())
3775 {
3776 MediumType_T mtype = medium->i_getType();
3777 // MediumType_Readonly is also new, but only applies to DVDs and floppies.
3778 // For DVDs it's not written to the config file, so needs no global config
3779 // version bump. For floppies it's a new attribute "type", which is ignored
3780 // by older VirtualBox version, so needs no global config version bump either.
3781 // For hard disks this type is not accepted.
3782 if (mtype == MediumType_MultiAttach)
3783 {
3784 // This type is new with VirtualBox 4.0 and therefore requires settings
3785 // version 1.11 in the settings backend. Unfortunately it is not enough to do
3786 // the usual routine in MachineConfigFile::bumpSettingsVersionIfNeeded() for
3787 // two reasons: The medium type is a property of the media registry tree, which
3788 // can reside in the global config file (for pre-4.0 media); we would therefore
3789 // possibly need to bump the global config version. We don't want to do that though
3790 // because that might make downgrading to pre-4.0 impossible.
3791 // As a result, we can only use these two new types if the medium is NOT in the
3792 // global registry:
3793 const Guid &uuidGlobalRegistry = mParent->i_getGlobalRegistryId();
3794 if ( medium->i_isInRegistry(uuidGlobalRegistry)
3795 || !mData->pMachineConfigFile->canHaveOwnMediaRegistry()
3796 )
3797 return setError(VBOX_E_INVALID_OBJECT_STATE,
3798 tr("Cannot attach medium '%s': the media type 'MultiAttach' can only be attached "
3799 "to machines that were created with VirtualBox 4.0 or later"),
3800 medium->i_getLocationFull().c_str());
3801 }
3802 }
3803
3804 bool fIndirect = false;
3805 if (!medium.isNull())
3806 fIndirect = medium->i_isReadOnly();
3807 bool associate = true;
3808
3809 do
3810 {
3811 if ( aType == DeviceType_HardDisk
3812 && mMediaData.isBackedUp())
3813 {
3814 const MediaData::AttachmentList &oldAtts = mMediaData.backedUpData()->mAttachments;
3815
3816 /* check if the medium was attached to the VM before we started
3817 * changing attachments in which case the attachment just needs to
3818 * be restored */
3819 if ((pAttachTemp = i_findAttachment(oldAtts, medium)))
3820 {
3821 AssertReturn(!fIndirect, E_FAIL);
3822
3823 /* see if it's the same bus/channel/device */
3824 if (pAttachTemp->i_matches(Bstr(aName).raw(), aControllerPort, aDevice))
3825 {
3826 /* the simplest case: restore the whole attachment
3827 * and return, nothing else to do */
3828 mMediaData->mAttachments.push_back(pAttachTemp);
3829
3830 /* Reattach the medium to the VM. */
3831 if (fHotplug || fSilent)
3832 {
3833 mediumLock.release();
3834 treeLock.release();
3835 alock.release();
3836
3837 MediumLockList *pMediumLockList(new MediumLockList());
3838
3839 rc = medium->i_createMediumLockList(true /* fFailIfInaccessible */,
3840 true /* fMediumLockWrite */,
3841 NULL,
3842 *pMediumLockList);
3843 alock.acquire();
3844 if (FAILED(rc))
3845 delete pMediumLockList;
3846 else
3847 {
3848 mData->mSession.mLockedMedia.Unlock();
3849 alock.release();
3850 rc = mData->mSession.mLockedMedia.Insert(pAttachTemp, pMediumLockList);
3851 mData->mSession.mLockedMedia.Lock();
3852 alock.acquire();
3853 }
3854 alock.release();
3855
3856 if (SUCCEEDED(rc))
3857 {
3858 rc = i_onStorageDeviceChange(pAttachTemp, FALSE /* aRemove */, fSilent);
3859 /* Remove lock list in case of error. */
3860 if (FAILED(rc))
3861 {
3862 mData->mSession.mLockedMedia.Unlock();
3863 mData->mSession.mLockedMedia.Remove(pAttachTemp);
3864 mData->mSession.mLockedMedia.Lock();
3865 }
3866 }
3867 }
3868
3869 return S_OK;
3870 }
3871
3872 /* bus/channel/device differ; we need a new attachment object,
3873 * but don't try to associate it again */
3874 associate = false;
3875 break;
3876 }
3877 }
3878
3879 /* go further only if the attachment is to be indirect */
3880 if (!fIndirect)
3881 break;
3882
3883 /* perform the so called smart attachment logic for indirect
3884 * attachments. Note that smart attachment is only applicable to base
3885 * hard disks. */
3886
3887 if (medium->i_getParent().isNull())
3888 {
3889 /* first, investigate the backup copy of the current hard disk
3890 * attachments to make it possible to re-attach existing diffs to
3891 * another device slot w/o losing their contents */
3892 if (mMediaData.isBackedUp())
3893 {
3894 const MediaData::AttachmentList &oldAtts = mMediaData.backedUpData()->mAttachments;
3895
3896 MediaData::AttachmentList::const_iterator foundIt = oldAtts.end();
3897 uint32_t foundLevel = 0;
3898
3899 for (MediaData::AttachmentList::const_iterator it = oldAtts.begin(); it != oldAtts.end(); ++it)
3900 {
3901 uint32_t level = 0;
3902 MediumAttachment *pAttach = *it;
3903 ComObjPtr<Medium> pMedium = pAttach->i_getMedium();
3904 Assert(!pMedium.isNull() || pAttach->i_getType() != DeviceType_HardDisk);
3905 if (pMedium.isNull())
3906 continue;
3907
3908 if (pMedium->i_getBase(&level) == medium)
3909 {
3910 /* skip the hard disk if its currently attached (we
3911 * cannot attach the same hard disk twice) */
3912 if (i_findAttachment(mMediaData->mAttachments,
3913 pMedium))
3914 continue;
3915
3916 /* matched device, channel and bus (i.e. attached to the
3917 * same place) will win and immediately stop the search;
3918 * otherwise the attachment that has the youngest
3919 * descendant of medium will be used
3920 */
3921 if (pAttach->i_matches(Bstr(aName).raw(), aControllerPort, aDevice))
3922 {
3923 /* the simplest case: restore the whole attachment
3924 * and return, nothing else to do */
3925 mMediaData->mAttachments.push_back(*it);
3926
3927 /* Reattach the medium to the VM. */
3928 if (fHotplug || fSilent)
3929 {
3930 mediumLock.release();
3931 treeLock.release();
3932 alock.release();
3933
3934 MediumLockList *pMediumLockList(new MediumLockList());
3935
3936 rc = medium->i_createMediumLockList(true /* fFailIfInaccessible */,
3937 true /* fMediumLockWrite */,
3938 NULL,
3939 *pMediumLockList);
3940 alock.acquire();
3941 if (FAILED(rc))
3942 delete pMediumLockList;
3943 else
3944 {
3945 mData->mSession.mLockedMedia.Unlock();
3946 alock.release();
3947 rc = mData->mSession.mLockedMedia.Insert(pAttachTemp, pMediumLockList);
3948 mData->mSession.mLockedMedia.Lock();
3949 alock.acquire();
3950 }
3951 alock.release();
3952
3953 if (SUCCEEDED(rc))
3954 {
3955 rc = i_onStorageDeviceChange(pAttachTemp, FALSE /* aRemove */, fSilent);
3956 /* Remove lock list in case of error. */
3957 if (FAILED(rc))
3958 {
3959 mData->mSession.mLockedMedia.Unlock();
3960 mData->mSession.mLockedMedia.Remove(pAttachTemp);
3961 mData->mSession.mLockedMedia.Lock();
3962 }
3963 }
3964 }
3965
3966 return S_OK;
3967 }
3968 else if ( foundIt == oldAtts.end()
3969 || level > foundLevel /* prefer younger */
3970 )
3971 {
3972 foundIt = it;
3973 foundLevel = level;
3974 }
3975 }
3976 }
3977
3978 if (foundIt != oldAtts.end())
3979 {
3980 /* use the previously attached hard disk */
3981 medium = (*foundIt)->i_getMedium();
3982 mediumCaller.attach(medium);
3983 if (FAILED(mediumCaller.rc())) return mediumCaller.rc();
3984 mediumLock.attach(medium);
3985 /* not implicit, doesn't require association with this VM */
3986 fIndirect = false;
3987 associate = false;
3988 /* go right to the MediumAttachment creation */
3989 break;
3990 }
3991 }
3992
3993 /* must give up the medium lock and medium tree lock as below we
3994 * go over snapshots, which needs a lock with higher lock order. */
3995 mediumLock.release();
3996 treeLock.release();
3997
3998 /* then, search through snapshots for the best diff in the given
3999 * hard disk's chain to base the new diff on */
4000
4001 ComObjPtr<Medium> base;
4002 ComObjPtr<Snapshot> snap = mData->mCurrentSnapshot;
4003 while (snap)
4004 {
4005 AutoReadLock snapLock(snap COMMA_LOCKVAL_SRC_POS);
4006
4007 const MediaData::AttachmentList &snapAtts = snap->i_getSnapshotMachine()->mMediaData->mAttachments;
4008
4009 MediumAttachment *pAttachFound = NULL;
4010 uint32_t foundLevel = 0;
4011
4012 for (MediaData::AttachmentList::const_iterator it = snapAtts.begin(); it != snapAtts.end(); ++it)
4013 {
4014 MediumAttachment *pAttach = *it;
4015 ComObjPtr<Medium> pMedium = pAttach->i_getMedium();
4016 Assert(!pMedium.isNull() || pAttach->i_getType() != DeviceType_HardDisk);
4017 if (pMedium.isNull())
4018 continue;
4019
4020 uint32_t level = 0;
4021 if (pMedium->i_getBase(&level) == medium)
4022 {
4023 /* matched device, channel and bus (i.e. attached to the
4024 * same place) will win and immediately stop the search;
4025 * otherwise the attachment that has the youngest
4026 * descendant of medium will be used
4027 */
4028 if ( pAttach->i_getDevice() == aDevice
4029 && pAttach->i_getPort() == aControllerPort
4030 && pAttach->i_getControllerName() == aName
4031 )
4032 {
4033 pAttachFound = pAttach;
4034 break;
4035 }
4036 else if ( !pAttachFound
4037 || level > foundLevel /* prefer younger */
4038 )
4039 {
4040 pAttachFound = pAttach;
4041 foundLevel = level;
4042 }
4043 }
4044 }
4045
4046 if (pAttachFound)
4047 {
4048 base = pAttachFound->i_getMedium();
4049 break;
4050 }
4051
4052 snap = snap->i_getParent();
4053 }
4054
4055 /* re-lock medium tree and the medium, as we need it below */
4056 treeLock.acquire();
4057 mediumLock.acquire();
4058
4059 /* found a suitable diff, use it as a base */
4060 if (!base.isNull())
4061 {
4062 medium = base;
4063 mediumCaller.attach(medium);
4064 if (FAILED(mediumCaller.rc())) return mediumCaller.rc();
4065 mediumLock.attach(medium);
4066 }
4067 }
4068
4069 Utf8Str strFullSnapshotFolder;
4070 i_calculateFullPath(mUserData->s.strSnapshotFolder, strFullSnapshotFolder);
4071
4072 ComObjPtr<Medium> diff;
4073 diff.createObject();
4074 // store this diff in the same registry as the parent
4075 Guid uuidRegistryParent;
4076 if (!medium->i_getFirstRegistryMachineId(uuidRegistryParent))
4077 {
4078 // parent image has no registry: this can happen if we're attaching a new immutable
4079 // image that has not yet been attached (medium then points to the base and we're
4080 // creating the diff image for the immutable, and the parent is not yet registered);
4081 // put the parent in the machine registry then
4082 mediumLock.release();
4083 treeLock.release();
4084 alock.release();
4085 i_addMediumToRegistry(medium);
4086 alock.acquire();
4087 treeLock.acquire();
4088 mediumLock.acquire();
4089 medium->i_getFirstRegistryMachineId(uuidRegistryParent);
4090 }
4091 rc = diff->init(mParent,
4092 medium->i_getPreferredDiffFormat(),
4093 strFullSnapshotFolder.append(RTPATH_SLASH_STR),
4094 uuidRegistryParent);
4095 if (FAILED(rc)) return rc;
4096
4097 /* Apply the normal locking logic to the entire chain. */
4098 MediumLockList *pMediumLockList(new MediumLockList());
4099 mediumLock.release();
4100 treeLock.release();
4101 rc = diff->i_createMediumLockList(true /* fFailIfInaccessible */,
4102 true /* fMediumLockWrite */,
4103 medium,
4104 *pMediumLockList);
4105 treeLock.acquire();
4106 mediumLock.acquire();
4107 if (SUCCEEDED(rc))
4108 {
4109 mediumLock.release();
4110 treeLock.release();
4111 rc = pMediumLockList->Lock();
4112 treeLock.acquire();
4113 mediumLock.acquire();
4114 if (FAILED(rc))
4115 setError(rc,
4116 tr("Could not lock medium when creating diff '%s'"),
4117 diff->i_getLocationFull().c_str());
4118 else
4119 {
4120 /* will release the lock before the potentially lengthy
4121 * operation, so protect with the special state */
4122 MachineState_T oldState = mData->mMachineState;
4123 i_setMachineState(MachineState_SettingUp);
4124
4125 mediumLock.release();
4126 treeLock.release();
4127 alock.release();
4128
4129 rc = medium->i_createDiffStorage(diff,
4130 MediumVariant_Standard,
4131 pMediumLockList,
4132 NULL /* aProgress */,
4133 true /* aWait */);
4134
4135 alock.acquire();
4136 treeLock.acquire();
4137 mediumLock.acquire();
4138
4139 i_setMachineState(oldState);
4140 }
4141 }
4142
4143 /* Unlock the media and free the associated memory. */
4144 delete pMediumLockList;
4145
4146 if (FAILED(rc)) return rc;
4147
4148 /* use the created diff for the actual attachment */
4149 medium = diff;
4150 mediumCaller.attach(medium);
4151 if (FAILED(mediumCaller.rc())) return mediumCaller.rc();
4152 mediumLock.attach(medium);
4153 }
4154 while (0);
4155
4156 ComObjPtr<MediumAttachment> attachment;
4157 attachment.createObject();
4158 rc = attachment->init(this,
4159 medium,
4160 aName,
4161 aControllerPort,
4162 aDevice,
4163 aType,
4164 fIndirect,
4165 false /* fPassthrough */,
4166 false /* fTempEject */,
4167 false /* fNonRotational */,
4168 false /* fDiscard */,
4169 fHotplug /* fHotPluggable */,
4170 Utf8Str::Empty);
4171 if (FAILED(rc)) return rc;
4172
4173 if (associate && !medium.isNull())
4174 {
4175 // as the last step, associate the medium to the VM
4176 rc = medium->i_addBackReference(mData->mUuid);
4177 // here we can fail because of Deleting, or being in process of creating a Diff
4178 if (FAILED(rc)) return rc;
4179
4180 mediumLock.release();
4181 treeLock.release();
4182 alock.release();
4183 i_addMediumToRegistry(medium);
4184 alock.acquire();
4185 treeLock.acquire();
4186 mediumLock.acquire();
4187 }
4188
4189 /* success: finally remember the attachment */
4190 i_setModified(IsModified_Storage);
4191 mMediaData.backup();
4192 mMediaData->mAttachments.push_back(attachment);
4193
4194 mediumLock.release();
4195 treeLock.release();
4196 alock.release();
4197
4198 if (fHotplug || fSilent)
4199 {
4200 if (!medium.isNull())
4201 {
4202 MediumLockList *pMediumLockList(new MediumLockList());
4203
4204 rc = medium->i_createMediumLockList(true /* fFailIfInaccessible */,
4205 true /* fMediumLockWrite */,
4206 NULL,
4207 *pMediumLockList);
4208 alock.acquire();
4209 if (FAILED(rc))
4210 delete pMediumLockList;
4211 else
4212 {
4213 mData->mSession.mLockedMedia.Unlock();
4214 alock.release();
4215 rc = mData->mSession.mLockedMedia.Insert(attachment, pMediumLockList);
4216 mData->mSession.mLockedMedia.Lock();
4217 alock.acquire();
4218 }
4219 alock.release();
4220 }
4221
4222 if (SUCCEEDED(rc))
4223 {
4224 rc = i_onStorageDeviceChange(attachment, FALSE /* aRemove */, fSilent);
4225 /* Remove lock list in case of error. */
4226 if (FAILED(rc))
4227 {
4228 mData->mSession.mLockedMedia.Unlock();
4229 mData->mSession.mLockedMedia.Remove(attachment);
4230 mData->mSession.mLockedMedia.Lock();
4231 }
4232 }
4233 }
4234
4235 mParent->i_saveModifiedRegistries();
4236
4237 return rc;
4238}
4239
4240HRESULT Machine::detachDevice(const com::Utf8Str &aName, LONG aControllerPort,
4241 LONG aDevice)
4242{
4243 LogFlowThisFunc(("aControllerName=\"%s\" aControllerPort=%d aDevice=%d\n",
4244 aName.c_str(), aControllerPort, aDevice));
4245
4246 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
4247
4248 HRESULT rc = i_checkStateDependency(MutableStateDep);
4249 if (FAILED(rc)) return rc;
4250
4251 AssertReturn(mData->mMachineState != MachineState_Saved, E_FAIL);
4252
4253 /* Check for an existing controller. */
4254 ComObjPtr<StorageController> ctl;
4255 rc = i_getStorageControllerByName(aName, ctl, true /* aSetError */);
4256 if (FAILED(rc)) return rc;
4257
4258 StorageControllerType_T ctrlType;
4259 rc = ctl->COMGETTER(ControllerType)(&ctrlType);
4260 if (FAILED(rc))
4261 return setError(E_FAIL,
4262 tr("Could not get type of controller '%s'"),
4263 aName.c_str());
4264
4265 bool fSilent = false;
4266 Utf8Str strReconfig;
4267
4268 /* Check whether the flag to allow silent storage attachment reconfiguration is set. */
4269 strReconfig = i_getExtraData(Utf8Str("VBoxInternal2/SilentReconfigureWhilePaused"));
4270 if ( mData->mMachineState == MachineState_Paused
4271 && strReconfig == "1")
4272 fSilent = true;
4273
4274 /* Check that the controller can do hotplugging if we detach the device while the VM is running. */
4275 bool fHotplug = false;
4276 if (!fSilent && Global::IsOnlineOrTransient(mData->mMachineState))
4277 fHotplug = true;
4278
4279 if (fHotplug && !i_isControllerHotplugCapable(ctrlType))
4280 return setError(VBOX_E_INVALID_VM_STATE,
4281 tr("Controller '%s' does not support hotplugging"),
4282 aName.c_str());
4283
4284 MediumAttachment *pAttach = i_findAttachment(mMediaData->mAttachments,
4285 Bstr(aName).raw(),
4286 aControllerPort,
4287 aDevice);
4288 if (!pAttach)
4289 return setError(VBOX_E_OBJECT_NOT_FOUND,
4290 tr("No storage device attached to device slot %d on port %d of controller '%s'"),
4291 aDevice, aControllerPort, aName.c_str());
4292
4293 if (fHotplug && !pAttach->i_getHotPluggable())
4294 return setError(VBOX_E_NOT_SUPPORTED,
4295 tr("The device slot %d on port %d of controller '%s' does not support hotplugging"),
4296 aDevice, aControllerPort, aName.c_str());
4297
4298 /*
4299 * The VM has to detach the device before we delete any implicit diffs.
4300 * If this fails we can roll back without loosing data.
4301 */
4302 if (fHotplug || fSilent)
4303 {
4304 alock.release();
4305 rc = i_onStorageDeviceChange(pAttach, TRUE /* aRemove */, fSilent);
4306 alock.acquire();
4307 }
4308 if (FAILED(rc)) return rc;
4309
4310 /* If we are here everything went well and we can delete the implicit now. */
4311 rc = i_detachDevice(pAttach, alock, NULL /* pSnapshot */);
4312
4313 alock.release();
4314
4315 mParent->i_saveModifiedRegistries();
4316
4317 return rc;
4318}
4319
4320HRESULT Machine::passthroughDevice(const com::Utf8Str &aName, LONG aControllerPort,
4321 LONG aDevice, BOOL aPassthrough)
4322{
4323 LogFlowThisFunc(("aName=\"%s\" aControllerPort=%d aDevice=%d aPassthrough=%d\n",
4324 aName.c_str(), aControllerPort, aDevice, aPassthrough));
4325
4326 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
4327
4328 HRESULT rc = i_checkStateDependency(MutableStateDep);
4329 if (FAILED(rc)) return rc;
4330
4331 AssertReturn(mData->mMachineState != MachineState_Saved, E_FAIL);
4332
4333 if (Global::IsOnlineOrTransient(mData->mMachineState))
4334 return setError(VBOX_E_INVALID_VM_STATE,
4335 tr("Invalid machine state: %s"),
4336 Global::stringifyMachineState(mData->mMachineState));
4337
4338 MediumAttachment *pAttach = i_findAttachment(mMediaData->mAttachments,
4339 Bstr(aName).raw(),
4340 aControllerPort,
4341 aDevice);
4342 if (!pAttach)
4343 return setError(VBOX_E_OBJECT_NOT_FOUND,
4344 tr("No storage device attached to device slot %d on port %d of controller '%s'"),
4345 aDevice, aControllerPort, aName.c_str());
4346
4347
4348 i_setModified(IsModified_Storage);
4349 mMediaData.backup();
4350
4351 AutoWriteLock attLock(pAttach COMMA_LOCKVAL_SRC_POS);
4352
4353 if (pAttach->i_getType() != DeviceType_DVD)
4354 return setError(E_INVALIDARG,
4355 tr("Setting passthrough rejected as the device attached to device slot %d on port %d of controller '%s' is not a DVD"),
4356 aDevice, aControllerPort, aName.c_str());
4357 pAttach->i_updatePassthrough(!!aPassthrough);
4358
4359 return S_OK;
4360}
4361
4362HRESULT Machine::temporaryEjectDevice(const com::Utf8Str &aName, LONG aControllerPort,
4363 LONG aDevice, BOOL aTemporaryEject)
4364{
4365
4366 LogFlowThisFunc(("aName=\"%s\" aControllerPort=%d aDevice=%d aTemporaryEject=%d\n",
4367 aName.c_str(), aControllerPort, aDevice, aTemporaryEject));
4368
4369 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
4370
4371 HRESULT rc = i_checkStateDependency(MutableStateDep);
4372 if (FAILED(rc)) return rc;
4373
4374 MediumAttachment *pAttach = i_findAttachment(mMediaData->mAttachments,
4375 Bstr(aName).raw(),
4376 aControllerPort,
4377 aDevice);
4378 if (!pAttach)
4379 return setError(VBOX_E_OBJECT_NOT_FOUND,
4380 tr("No storage device attached to device slot %d on port %d of controller '%s'"),
4381 aDevice, aControllerPort, aName.c_str());
4382
4383
4384 i_setModified(IsModified_Storage);
4385 mMediaData.backup();
4386
4387 AutoWriteLock attLock(pAttach COMMA_LOCKVAL_SRC_POS);
4388
4389 if (pAttach->i_getType() != DeviceType_DVD)
4390 return setError(E_INVALIDARG,
4391 tr("Setting temporary eject flag rejected as the device attached to device slot %d on port %d of controller '%s' is not a DVD"),
4392 aDevice, aControllerPort, aName.c_str());
4393 pAttach->i_updateTempEject(!!aTemporaryEject);
4394
4395 return S_OK;
4396}
4397
4398HRESULT Machine::nonRotationalDevice(const com::Utf8Str &aName, LONG aControllerPort,
4399 LONG aDevice, BOOL aNonRotational)
4400{
4401
4402 LogFlowThisFunc(("aName=\"%s\" aControllerPort=%d aDevice=%d aNonRotational=%d\n",
4403 aName.c_str(), aControllerPort, aDevice, aNonRotational));
4404
4405 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
4406
4407 HRESULT rc = i_checkStateDependency(MutableStateDep);
4408 if (FAILED(rc)) return rc;
4409
4410 AssertReturn(mData->mMachineState != MachineState_Saved, E_FAIL);
4411
4412 if (Global::IsOnlineOrTransient(mData->mMachineState))
4413 return setError(VBOX_E_INVALID_VM_STATE,
4414 tr("Invalid machine state: %s"),
4415 Global::stringifyMachineState(mData->mMachineState));
4416
4417 MediumAttachment *pAttach = i_findAttachment(mMediaData->mAttachments,
4418 Bstr(aName).raw(),
4419 aControllerPort,
4420 aDevice);
4421 if (!pAttach)
4422 return setError(VBOX_E_OBJECT_NOT_FOUND,
4423 tr("No storage device attached to device slot %d on port %d of controller '%s'"),
4424 aDevice, aControllerPort, aName.c_str());
4425
4426
4427 i_setModified(IsModified_Storage);
4428 mMediaData.backup();
4429
4430 AutoWriteLock attLock(pAttach COMMA_LOCKVAL_SRC_POS);
4431
4432 if (pAttach->i_getType() != DeviceType_HardDisk)
4433 return setError(E_INVALIDARG,
4434 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"),
4435 aDevice, aControllerPort, aName.c_str());
4436 pAttach->i_updateNonRotational(!!aNonRotational);
4437
4438 return S_OK;
4439}
4440
4441HRESULT Machine::setAutoDiscardForDevice(const com::Utf8Str &aName, LONG aControllerPort,
4442 LONG aDevice, BOOL aDiscard)
4443{
4444
4445 LogFlowThisFunc(("aName=\"%s\" aControllerPort=%d aDevice=%d aDiscard=%d\n",
4446 aName.c_str(), aControllerPort, aDevice, aDiscard));
4447
4448 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
4449
4450 HRESULT rc = i_checkStateDependency(MutableStateDep);
4451 if (FAILED(rc)) return rc;
4452
4453 AssertReturn(mData->mMachineState != MachineState_Saved, E_FAIL);
4454
4455 if (Global::IsOnlineOrTransient(mData->mMachineState))
4456 return setError(VBOX_E_INVALID_VM_STATE,
4457 tr("Invalid machine state: %s"),
4458 Global::stringifyMachineState(mData->mMachineState));
4459
4460 MediumAttachment *pAttach = i_findAttachment(mMediaData->mAttachments,
4461 Bstr(aName).raw(),
4462 aControllerPort,
4463 aDevice);
4464 if (!pAttach)
4465 return setError(VBOX_E_OBJECT_NOT_FOUND,
4466 tr("No storage device attached to device slot %d on port %d of controller '%s'"),
4467 aDevice, aControllerPort, aName.c_str());
4468
4469
4470 i_setModified(IsModified_Storage);
4471 mMediaData.backup();
4472
4473 AutoWriteLock attLock(pAttach COMMA_LOCKVAL_SRC_POS);
4474
4475 if (pAttach->i_getType() != DeviceType_HardDisk)
4476 return setError(E_INVALIDARG,
4477 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"),
4478 aDevice, aControllerPort, aName.c_str());
4479 pAttach->i_updateDiscard(!!aDiscard);
4480
4481 return S_OK;
4482}
4483
4484HRESULT Machine::setHotPluggableForDevice(const com::Utf8Str &aName, LONG aControllerPort,
4485 LONG aDevice, BOOL aHotPluggable)
4486{
4487 LogFlowThisFunc(("aName=\"%s\" aControllerPort=%d aDevice=%d aHotPluggable=%d\n",
4488 aName.c_str(), aControllerPort, aDevice, aHotPluggable));
4489
4490 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
4491
4492 HRESULT rc = i_checkStateDependency(MutableStateDep);
4493 if (FAILED(rc)) return rc;
4494
4495 AssertReturn(mData->mMachineState != MachineState_Saved, E_FAIL);
4496
4497 if (Global::IsOnlineOrTransient(mData->mMachineState))
4498 return setError(VBOX_E_INVALID_VM_STATE,
4499 tr("Invalid machine state: %s"),
4500 Global::stringifyMachineState(mData->mMachineState));
4501
4502 MediumAttachment *pAttach = i_findAttachment(mMediaData->mAttachments,
4503 Bstr(aName).raw(),
4504 aControllerPort,
4505 aDevice);
4506 if (!pAttach)
4507 return setError(VBOX_E_OBJECT_NOT_FOUND,
4508 tr("No storage device attached to device slot %d on port %d of controller '%s'"),
4509 aDevice, aControllerPort, aName.c_str());
4510
4511 /* Check for an existing controller. */
4512 ComObjPtr<StorageController> ctl;
4513 rc = i_getStorageControllerByName(aName, ctl, true /* aSetError */);
4514 if (FAILED(rc)) return rc;
4515
4516 StorageControllerType_T ctrlType;
4517 rc = ctl->COMGETTER(ControllerType)(&ctrlType);
4518 if (FAILED(rc))
4519 return setError(E_FAIL,
4520 tr("Could not get type of controller '%s'"),
4521 aName.c_str());
4522
4523 if (!i_isControllerHotplugCapable(ctrlType))
4524 return setError(VBOX_E_NOT_SUPPORTED,
4525 tr("Controller '%s' does not support changing the hot-pluggable device flag"),
4526 aName.c_str());
4527
4528 i_setModified(IsModified_Storage);
4529 mMediaData.backup();
4530
4531 AutoWriteLock attLock(pAttach COMMA_LOCKVAL_SRC_POS);
4532
4533 if (pAttach->i_getType() == DeviceType_Floppy)
4534 return setError(E_INVALIDARG,
4535 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"),
4536 aDevice, aControllerPort, aName.c_str());
4537 pAttach->i_updateHotPluggable(!!aHotPluggable);
4538
4539 return S_OK;
4540}
4541
4542HRESULT Machine::setNoBandwidthGroupForDevice(const com::Utf8Str &aName, LONG aControllerPort,
4543 LONG aDevice)
4544{
4545 int rc = S_OK;
4546 LogFlowThisFunc(("aName=\"%s\" aControllerPort=%d aDevice=%d\n",
4547 aName.c_str(), aControllerPort, aDevice));
4548
4549 rc = setBandwidthGroupForDevice(aName, aControllerPort, aDevice, NULL);
4550
4551 return rc;
4552}
4553
4554HRESULT Machine::setBandwidthGroupForDevice(const com::Utf8Str &aName, LONG aControllerPort,
4555 LONG aDevice, const ComPtr<IBandwidthGroup> &aBandwidthGroup)
4556{
4557 LogFlowThisFunc(("aName=\"%s\" aControllerPort=%d aDevice=%d\n",
4558 aName.c_str(), aControllerPort, aDevice));
4559
4560 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
4561
4562 HRESULT rc = i_checkStateDependency(MutableStateDep);
4563 if (FAILED(rc)) return rc;
4564
4565 AssertReturn(mData->mMachineState != MachineState_Saved, E_FAIL);
4566
4567 if (Global::IsOnlineOrTransient(mData->mMachineState))
4568 return setError(VBOX_E_INVALID_VM_STATE,
4569 tr("Invalid machine state: %s"),
4570 Global::stringifyMachineState(mData->mMachineState));
4571
4572 MediumAttachment *pAttach = i_findAttachment(mMediaData->mAttachments,
4573 Bstr(aName).raw(),
4574 aControllerPort,
4575 aDevice);
4576 if (!pAttach)
4577 return setError(VBOX_E_OBJECT_NOT_FOUND,
4578 tr("No storage device attached to device slot %d on port %d of controller '%s'"),
4579 aDevice, aControllerPort, aName.c_str());
4580
4581
4582 i_setModified(IsModified_Storage);
4583 mMediaData.backup();
4584
4585 IBandwidthGroup *iB = aBandwidthGroup;
4586 ComObjPtr<BandwidthGroup> group = static_cast<BandwidthGroup*>(iB);
4587 if (aBandwidthGroup && group.isNull())
4588 return setError(E_INVALIDARG, "The given bandwidth group pointer is invalid");
4589
4590 AutoWriteLock attLock(pAttach COMMA_LOCKVAL_SRC_POS);
4591
4592 const Utf8Str strBandwidthGroupOld = pAttach->i_getBandwidthGroup();
4593 if (strBandwidthGroupOld.isNotEmpty())
4594 {
4595 /* Get the bandwidth group object and release it - this must not fail. */
4596 ComObjPtr<BandwidthGroup> pBandwidthGroupOld;
4597 rc = i_getBandwidthGroup(strBandwidthGroupOld, pBandwidthGroupOld, false);
4598 Assert(SUCCEEDED(rc));
4599
4600 pBandwidthGroupOld->i_release();
4601 pAttach->i_updateBandwidthGroup(Utf8Str::Empty);
4602 }
4603
4604 if (!group.isNull())
4605 {
4606 group->i_reference();
4607 pAttach->i_updateBandwidthGroup(group->i_getName());
4608 }
4609
4610 return S_OK;
4611}
4612
4613HRESULT Machine::attachDeviceWithoutMedium(const com::Utf8Str &aName,
4614 LONG aControllerPort,
4615 LONG aDevice,
4616 DeviceType_T aType)
4617{
4618 HRESULT rc = S_OK;
4619
4620 LogFlowThisFunc(("aName=\"%s\" aControllerPort=%d aDevice=%d aType=%d aMedium=%p\n",
4621 aName.c_str(), aControllerPort, aDevice, aType));
4622
4623 rc = AttachDevice(Bstr(aName).raw(), aControllerPort, aDevice, aType, NULL);
4624
4625 return rc;
4626}
4627
4628
4629HRESULT Machine::unmountMedium(const com::Utf8Str &aName,
4630 LONG aControllerPort,
4631 LONG aDevice,
4632 BOOL aForce)
4633{
4634 int rc = S_OK;
4635 LogFlowThisFunc(("aName=\"%s\" aControllerPort=%d aDevice=%d",
4636 aName.c_str(), aControllerPort, aForce));
4637
4638 rc = mountMedium(aName, aControllerPort, aDevice, NULL, aForce);
4639
4640 return rc;
4641}
4642
4643HRESULT Machine::mountMedium(const com::Utf8Str &aName,
4644 LONG aControllerPort,
4645 LONG aDevice,
4646 const ComPtr<IMedium> &aMedium,
4647 BOOL aForce)
4648{
4649 int rc = S_OK;
4650 LogFlowThisFunc(("aName=\"%s\" aControllerPort=%d aDevice=%d aForce=%d\n",
4651 aName.c_str(), aControllerPort, aDevice, aForce));
4652
4653 // request the host lock first, since might be calling Host methods for getting host drives;
4654 // next, protect the media tree all the while we're in here, as well as our member variables
4655 AutoMultiWriteLock3 multiLock(mParent->i_host()->lockHandle(),
4656 this->lockHandle(),
4657 &mParent->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
4658
4659 ComObjPtr<MediumAttachment> pAttach = i_findAttachment(mMediaData->mAttachments,
4660 Bstr(aName).raw(),
4661 aControllerPort,
4662 aDevice);
4663 if (pAttach.isNull())
4664 return setError(VBOX_E_OBJECT_NOT_FOUND,
4665 tr("No drive attached to device slot %d on port %d of controller '%s'"),
4666 aDevice, aControllerPort, aName.c_str());
4667
4668 /* Remember previously mounted medium. The medium before taking the
4669 * backup is not necessarily the same thing. */
4670 ComObjPtr<Medium> oldmedium;
4671 oldmedium = pAttach->i_getMedium();
4672
4673 IMedium *iM = aMedium;
4674 ComObjPtr<Medium> pMedium = static_cast<Medium*>(iM);
4675 if (aMedium && pMedium.isNull())
4676 return setError(E_INVALIDARG, "The given medium pointer is invalid");
4677
4678 AutoCaller mediumCaller(pMedium);
4679 if (FAILED(mediumCaller.rc())) return mediumCaller.rc();
4680
4681 AutoWriteLock mediumLock(pMedium COMMA_LOCKVAL_SRC_POS);
4682 if (pMedium)
4683 {
4684 DeviceType_T mediumType = pAttach->i_getType();
4685 switch (mediumType)
4686 {
4687 case DeviceType_DVD:
4688 case DeviceType_Floppy:
4689 break;
4690
4691 default:
4692 return setError(VBOX_E_INVALID_OBJECT_STATE,
4693 tr("The device at port %d, device %d of controller '%s' of this virtual machine is not removeable"),
4694 aControllerPort,
4695 aDevice,
4696 aName.c_str());
4697 }
4698 }
4699
4700 i_setModified(IsModified_Storage);
4701 mMediaData.backup();
4702
4703 {
4704 // The backup operation makes the pAttach reference point to the
4705 // old settings. Re-get the correct reference.
4706 pAttach = i_findAttachment(mMediaData->mAttachments,
4707 Bstr(aName).raw(),
4708 aControllerPort,
4709 aDevice);
4710 if (!oldmedium.isNull())
4711 oldmedium->i_removeBackReference(mData->mUuid);
4712 if (!pMedium.isNull())
4713 {
4714 pMedium->i_addBackReference(mData->mUuid);
4715
4716 mediumLock.release();
4717 multiLock.release();
4718 i_addMediumToRegistry(pMedium);
4719 multiLock.acquire();
4720 mediumLock.acquire();
4721 }
4722
4723 AutoWriteLock attLock(pAttach COMMA_LOCKVAL_SRC_POS);
4724 pAttach->i_updateMedium(pMedium);
4725 }
4726
4727 i_setModified(IsModified_Storage);
4728
4729 mediumLock.release();
4730 multiLock.release();
4731 rc = i_onMediumChange(pAttach, aForce);
4732 multiLock.acquire();
4733 mediumLock.acquire();
4734
4735 /* On error roll back this change only. */
4736 if (FAILED(rc))
4737 {
4738 if (!pMedium.isNull())
4739 pMedium->i_removeBackReference(mData->mUuid);
4740 pAttach = i_findAttachment(mMediaData->mAttachments,
4741 Bstr(aName).raw(),
4742 aControllerPort,
4743 aDevice);
4744 /* If the attachment is gone in the meantime, bail out. */
4745 if (pAttach.isNull())
4746 return rc;
4747 AutoWriteLock attLock(pAttach COMMA_LOCKVAL_SRC_POS);
4748 if (!oldmedium.isNull())
4749 oldmedium->i_addBackReference(mData->mUuid);
4750 pAttach->i_updateMedium(oldmedium);
4751 }
4752
4753 mediumLock.release();
4754 multiLock.release();
4755
4756 mParent->i_saveModifiedRegistries();
4757
4758 return rc;
4759}
4760HRESULT Machine::getMedium(const com::Utf8Str &aName,
4761 LONG aControllerPort,
4762 LONG aDevice,
4763 ComPtr<IMedium> &aMedium)
4764{
4765 LogFlowThisFunc(("aName=\"%s\" aControllerPort=%d aDevice=%d\n",
4766 aName.c_str(), aControllerPort, aDevice));
4767
4768 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
4769
4770 aMedium = NULL;
4771
4772 ComObjPtr<MediumAttachment> pAttach = i_findAttachment(mMediaData->mAttachments,
4773 Bstr(aName).raw(),
4774 aControllerPort,
4775 aDevice);
4776 if (pAttach.isNull())
4777 return setError(VBOX_E_OBJECT_NOT_FOUND,
4778 tr("No storage device attached to device slot %d on port %d of controller '%s'"),
4779 aDevice, aControllerPort, aName.c_str());
4780
4781 pAttach->i_getMedium().queryInterfaceTo(aMedium.asOutParam());
4782
4783 return S_OK;
4784}
4785
4786HRESULT Machine::getSerialPort(ULONG aSlot, ComPtr<ISerialPort> &aPort)
4787{
4788
4789 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
4790
4791 mSerialPorts[aSlot].queryInterfaceTo(aPort.asOutParam());
4792
4793 return S_OK;
4794}
4795
4796HRESULT Machine::getParallelPort(ULONG aSlot, ComPtr<IParallelPort> &aPort)
4797{
4798 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
4799
4800 mParallelPorts[aSlot].queryInterfaceTo(aPort.asOutParam());
4801
4802 return S_OK;
4803}
4804
4805HRESULT Machine::getNetworkAdapter(ULONG aSlot, ComPtr<INetworkAdapter> &aAdapter)
4806{
4807 /* Do not assert if slot is out of range, just return the advertised
4808 status. testdriver/vbox.py triggers this in logVmInfo. */
4809 if (aSlot >= mNetworkAdapters.size())
4810 return setError(E_INVALIDARG,
4811 tr("No network adapter in slot %RU32 (total %RU32 adapters)"),
4812 aSlot, mNetworkAdapters.size());
4813
4814 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
4815
4816 mNetworkAdapters[aSlot].queryInterfaceTo(aAdapter.asOutParam());
4817
4818 return S_OK;
4819}
4820
4821HRESULT Machine::getExtraDataKeys(std::vector<com::Utf8Str> &aKeys)
4822{
4823 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
4824
4825 com::SafeArray<BSTR> saKeys(mData->pMachineConfigFile->mapExtraDataItems.size());
4826 aKeys.resize(mData->pMachineConfigFile->mapExtraDataItems.size());
4827 size_t i = 0;
4828 for (settings::StringsMap::const_iterator it = mData->pMachineConfigFile->mapExtraDataItems.begin();
4829 it != mData->pMachineConfigFile->mapExtraDataItems.end();
4830 ++it, ++i)
4831 aKeys[i] = it->first;
4832
4833 return S_OK;
4834}
4835
4836 /**
4837 * @note Locks this object for reading.
4838 */
4839HRESULT Machine::getExtraData(const com::Utf8Str &aKey,
4840 com::Utf8Str &aValue)
4841{
4842 /* start with nothing found */
4843 aValue = "";
4844
4845 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
4846
4847 settings::StringsMap::const_iterator it = mData->pMachineConfigFile->mapExtraDataItems.find(aKey);
4848 if (it != mData->pMachineConfigFile->mapExtraDataItems.end())
4849 // found:
4850 aValue = it->second; // source is a Utf8Str
4851
4852 /* return the result to caller (may be empty) */
4853 return S_OK;
4854}
4855
4856 /**
4857 * @note Locks mParent for writing + this object for writing.
4858 */
4859HRESULT Machine::setExtraData(const com::Utf8Str &aKey, const com::Utf8Str &aValue)
4860{
4861 Utf8Str strOldValue; // empty
4862
4863 // locking note: we only hold the read lock briefly to look up the old value,
4864 // then release it and call the onExtraCanChange callbacks. There is a small
4865 // chance of a race insofar as the callback might be called twice if two callers
4866 // change the same key at the same time, but that's a much better solution
4867 // than the deadlock we had here before. The actual changing of the extradata
4868 // is then performed under the write lock and race-free.
4869
4870 // look up the old value first; if nothing has changed then we need not do anything
4871 {
4872 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS); // hold read lock only while looking up
4873 settings::StringsMap::const_iterator it = mData->pMachineConfigFile->mapExtraDataItems.find(aKey);
4874 if (it != mData->pMachineConfigFile->mapExtraDataItems.end())
4875 strOldValue = it->second;
4876 }
4877
4878 bool fChanged;
4879 if ((fChanged = (strOldValue != aValue)))
4880 {
4881 // ask for permission from all listeners outside the locks;
4882 // i_onExtraDataCanChange() only briefly requests the VirtualBox
4883 // lock to copy the list of callbacks to invoke
4884 Bstr error;
4885 Bstr bstrValue(aValue);
4886
4887 if (!mParent->i_onExtraDataCanChange(mData->mUuid, Bstr(aKey).raw(), bstrValue.raw(), error))
4888 {
4889 const char *sep = error.isEmpty() ? "" : ": ";
4890 CBSTR err = error.raw();
4891 LogWarningFunc(("Someone vetoed! Change refused%s%ls\n",
4892 sep, err));
4893 return setError(E_ACCESSDENIED,
4894 tr("Could not set extra data because someone refused the requested change of '%s' to '%s'%s%ls"),
4895 aKey.c_str(),
4896 aValue.c_str(),
4897 sep,
4898 err);
4899 }
4900
4901 // data is changing and change not vetoed: then write it out under the lock
4902 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
4903
4904 if (i_isSnapshotMachine())
4905 {
4906 HRESULT rc = i_checkStateDependency(MutableStateDep);
4907 if (FAILED(rc)) return rc;
4908 }
4909
4910 if (aValue.isEmpty())
4911 mData->pMachineConfigFile->mapExtraDataItems.erase(aKey);
4912 else
4913 mData->pMachineConfigFile->mapExtraDataItems[aKey] = aValue;
4914 // creates a new key if needed
4915
4916 bool fNeedsGlobalSaveSettings = false;
4917 i_saveSettings(&fNeedsGlobalSaveSettings);
4918
4919 if (fNeedsGlobalSaveSettings)
4920 {
4921 // save the global settings; for that we should hold only the VirtualBox lock
4922 alock.release();
4923 AutoWriteLock vboxlock(mParent COMMA_LOCKVAL_SRC_POS);
4924 mParent->i_saveSettings();
4925 }
4926 }
4927
4928 // fire notification outside the lock
4929 if (fChanged)
4930 mParent->i_onExtraDataChange(mData->mUuid, Bstr(aKey).raw(), Bstr(aValue).raw());
4931
4932 return S_OK;
4933}
4934
4935HRESULT Machine::setSettingsFilePath(const com::Utf8Str &aSettingsFilePath, ComPtr<IProgress> &aProgress)
4936{
4937 aProgress = NULL;
4938 NOREF(aSettingsFilePath);
4939 ReturnComNotImplemented();
4940}
4941
4942HRESULT Machine::saveSettings()
4943{
4944 AutoWriteLock mlock(this COMMA_LOCKVAL_SRC_POS);
4945
4946 /* when there was auto-conversion, we want to save the file even if
4947 * the VM is saved */
4948 HRESULT rc = i_checkStateDependency(MutableOrSavedStateDep);
4949 if (FAILED(rc)) return rc;
4950
4951 /* the settings file path may never be null */
4952 ComAssertRet(!mData->m_strConfigFileFull.isEmpty(), E_FAIL);
4953
4954 /* save all VM data excluding snapshots */
4955 bool fNeedsGlobalSaveSettings = false;
4956 rc = i_saveSettings(&fNeedsGlobalSaveSettings);
4957 mlock.release();
4958
4959 if (SUCCEEDED(rc) && fNeedsGlobalSaveSettings)
4960 {
4961 // save the global settings; for that we should hold only the VirtualBox lock
4962 AutoWriteLock vlock(mParent COMMA_LOCKVAL_SRC_POS);
4963 rc = mParent->i_saveSettings();
4964 }
4965
4966 return rc;
4967}
4968
4969
4970HRESULT Machine::discardSettings()
4971{
4972 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
4973
4974 HRESULT rc = i_checkStateDependency(MutableStateDep);
4975 if (FAILED(rc)) return rc;
4976
4977 /*
4978 * during this rollback, the session will be notified if data has
4979 * been actually changed
4980 */
4981 i_rollback(true /* aNotify */);
4982
4983 return S_OK;
4984}
4985
4986/** @note Locks objects! */
4987HRESULT Machine::unregister(CleanupMode_T aCleanupMode,
4988 std::vector<ComPtr<IMedium> > &aMedia)
4989{
4990 // use AutoLimitedCaller because this call is valid on inaccessible machines as well
4991 AutoLimitedCaller autoCaller(this);
4992 AssertComRCReturnRC(autoCaller.rc());
4993
4994 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
4995
4996 Guid id(i_getId());
4997
4998 if (mData->mSession.mState != SessionState_Unlocked)
4999 return setError(VBOX_E_INVALID_OBJECT_STATE,
5000 tr("Cannot unregister the machine '%s' while it is locked"),
5001 mUserData->s.strName.c_str());
5002
5003 // wait for state dependents to drop to zero
5004 i_ensureNoStateDependencies();
5005
5006 if (!mData->mAccessible)
5007 {
5008 // inaccessible maschines can only be unregistered; uninitialize ourselves
5009 // here because currently there may be no unregistered that are inaccessible
5010 // (this state combination is not supported). Note releasing the caller and
5011 // leaving the lock before calling uninit()
5012 alock.release();
5013 autoCaller.release();
5014
5015 uninit();
5016
5017 mParent->i_unregisterMachine(this, id);
5018 // calls VirtualBox::i_saveSettings()
5019
5020 return S_OK;
5021 }
5022
5023 HRESULT rc = S_OK;
5024
5025 // discard saved state
5026 if (mData->mMachineState == MachineState_Saved)
5027 {
5028 // add the saved state file to the list of files the caller should delete
5029 Assert(!mSSData->strStateFilePath.isEmpty());
5030 mData->llFilesToDelete.push_back(mSSData->strStateFilePath);
5031
5032 mSSData->strStateFilePath.setNull();
5033
5034 // unconditionally set the machine state to powered off, we now
5035 // know no session has locked the machine
5036 mData->mMachineState = MachineState_PoweredOff;
5037 }
5038
5039 size_t cSnapshots = 0;
5040 if (mData->mFirstSnapshot)
5041 cSnapshots = mData->mFirstSnapshot->i_getAllChildrenCount() + 1;
5042 if (cSnapshots && aCleanupMode == CleanupMode_UnregisterOnly)
5043 // fail now before we start detaching media
5044 return setError(VBOX_E_INVALID_OBJECT_STATE,
5045 tr("Cannot unregister the machine '%s' because it has %d snapshots"),
5046 mUserData->s.strName.c_str(), cSnapshots);
5047
5048 // This list collects the medium objects from all medium attachments
5049 // which we will detach from the machine and its snapshots, in a specific
5050 // order which allows for closing all media without getting "media in use"
5051 // errors, simply by going through the list from the front to the back:
5052 // 1) first media from machine attachments (these have the "leaf" attachments with snapshots
5053 // and must be closed before the parent media from the snapshots, or closing the parents
5054 // will fail because they still have children);
5055 // 2) media from the youngest snapshots followed by those from the parent snapshots until
5056 // the root ("first") snapshot of the machine.
5057 MediaList llMedia;
5058
5059 if ( !mMediaData.isNull() // can be NULL if machine is inaccessible
5060 && mMediaData->mAttachments.size()
5061 )
5062 {
5063 // we have media attachments: detach them all and add the Medium objects to our list
5064 if (aCleanupMode != CleanupMode_UnregisterOnly)
5065 i_detachAllMedia(alock, NULL /* pSnapshot */, aCleanupMode, llMedia);
5066 else
5067 return setError(VBOX_E_INVALID_OBJECT_STATE,
5068 tr("Cannot unregister the machine '%s' because it has %d media attachments"),
5069 mUserData->s.strName.c_str(), mMediaData->mAttachments.size());
5070 }
5071
5072 if (cSnapshots)
5073 {
5074 // autoCleanup must be true here, or we would have failed above
5075
5076 // add the media from the medium attachments of the snapshots to llMedia
5077 // as well, after the "main" machine media; Snapshot::uninitRecursively()
5078 // calls Machine::detachAllMedia() for the snapshot machine, recursing
5079 // into the children first
5080
5081 // Snapshot::beginDeletingSnapshot() asserts if the machine state is not this
5082 MachineState_T oldState = mData->mMachineState;
5083 mData->mMachineState = MachineState_DeletingSnapshot;
5084
5085 // make a copy of the first snapshot so the refcount does not drop to 0
5086 // in beginDeletingSnapshot, which sets pFirstSnapshot to 0 (that hangs
5087 // because of the AutoCaller voodoo)
5088 ComObjPtr<Snapshot> pFirstSnapshot = mData->mFirstSnapshot;
5089
5090 // GO!
5091 pFirstSnapshot->i_uninitRecursively(alock, aCleanupMode, llMedia, mData->llFilesToDelete);
5092
5093 mData->mMachineState = oldState;
5094 }
5095
5096 if (FAILED(rc))
5097 {
5098 i_rollbackMedia();
5099 return rc;
5100 }
5101
5102 // commit all the media changes made above
5103 i_commitMedia();
5104
5105 mData->mRegistered = false;
5106
5107 // machine lock no longer needed
5108 alock.release();
5109
5110 // return media to caller
5111 size_t i = 0;
5112 aMedia.resize(llMedia.size());
5113 for (MediaList::iterator it = llMedia.begin(); it != llMedia.end(); ++it, ++i)
5114 (*it).queryInterfaceTo(aMedia[i].asOutParam());
5115
5116 mParent->i_unregisterMachine(this, id);
5117 // calls VirtualBox::i_saveSettings() and VirtualBox::saveModifiedRegistries()
5118
5119 return S_OK;
5120}
5121
5122struct Machine::DeleteTask
5123{
5124 ComObjPtr<Machine> pMachine;
5125 RTCList<ComPtr<IMedium> > llMediums;
5126 StringsList llFilesToDelete;
5127 ComObjPtr<Progress> pProgress;
5128};
5129
5130HRESULT Machine::deleteConfig(const std::vector<ComPtr<IMedium> > &aMedia, ComPtr<IProgress> &aProgress)
5131{
5132 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
5133
5134 HRESULT rc = i_checkStateDependency(MutableStateDep);
5135 if (FAILED(rc)) return rc;
5136
5137 if (mData->mRegistered)
5138 return setError(VBOX_E_INVALID_VM_STATE,
5139 tr("Cannot delete settings of a registered machine"));
5140
5141 DeleteTask *pTask = new DeleteTask;
5142 pTask->pMachine = this;
5143
5144 // collect files to delete
5145 pTask->llFilesToDelete = mData->llFilesToDelete; // saved states pushed here by Unregister()
5146
5147 for (size_t i = 0; i < aMedia.size(); ++i)
5148 {
5149 IMedium *pIMedium(aMedia[i]);
5150 ComObjPtr<Medium> pMedium = static_cast<Medium*>(pIMedium);
5151 if (pMedium.isNull())
5152 return setError(E_INVALIDARG, "The given medium pointer with index %d is invalid", i);
5153 SafeArray<BSTR> ids;
5154 rc = pMedium->COMGETTER(MachineIds)(ComSafeArrayAsOutParam(ids));
5155 if (FAILED(rc)) return rc;
5156 /* At this point the medium should not have any back references
5157 * anymore. If it has it is attached to another VM and *must* not
5158 * deleted. */
5159 if (ids.size() < 1)
5160 pTask->llMediums.append(pMedium);
5161 }
5162 if (mData->pMachineConfigFile->fileExists())
5163 pTask->llFilesToDelete.push_back(mData->m_strConfigFileFull);
5164
5165 pTask->pProgress.createObject();
5166 pTask->pProgress->init(i_getVirtualBox(),
5167 static_cast<IMachine*>(this) /* aInitiator */,
5168 Bstr(tr("Deleting files")).raw(),
5169 true /* fCancellable */,
5170 (ULONG)(pTask->llFilesToDelete.size() + pTask->llMediums.size() + 1), // cOperations
5171 BstrFmt(tr("Deleting '%s'"), pTask->llFilesToDelete.front().c_str()).raw());
5172
5173 int vrc = RTThreadCreate(NULL,
5174 Machine::deleteThread,
5175 (void*)pTask,
5176 0,
5177 RTTHREADTYPE_MAIN_WORKER,
5178 0,
5179 "MachineDelete");
5180
5181 pTask->pProgress.queryInterfaceTo(aProgress.asOutParam());
5182
5183 if (RT_FAILURE(vrc))
5184 {
5185 delete pTask;
5186 return setError(E_FAIL, "Could not create MachineDelete thread (%Rrc)", vrc);
5187 }
5188
5189 LogFlowFuncLeave();
5190
5191 return S_OK;
5192}
5193
5194/**
5195 * Static task wrapper passed to RTThreadCreate() in Machine::Delete() which then
5196 * calls Machine::deleteTaskWorker() on the actual machine object.
5197 * @param Thread
5198 * @param pvUser
5199 * @return
5200 */
5201/*static*/
5202DECLCALLBACK(int) Machine::deleteThread(RTTHREAD Thread, void *pvUser)
5203{
5204 LogFlowFuncEnter();
5205
5206 DeleteTask *pTask = (DeleteTask*)pvUser;
5207 Assert(pTask);
5208 Assert(pTask->pMachine);
5209 Assert(pTask->pProgress);
5210
5211 HRESULT rc = pTask->pMachine->i_deleteTaskWorker(*pTask);
5212 pTask->pProgress->i_notifyComplete(rc);
5213
5214 delete pTask;
5215
5216 LogFlowFuncLeave();
5217
5218 NOREF(Thread);
5219
5220 return VINF_SUCCESS;
5221}
5222
5223/**
5224 * Task thread implementation for Machine::Delete(), called from Machine::deleteThread().
5225 * @param task
5226 * @return
5227 */
5228HRESULT Machine::i_deleteTaskWorker(DeleteTask &task)
5229{
5230 AutoCaller autoCaller(this);
5231 if (FAILED(autoCaller.rc())) return autoCaller.rc();
5232
5233 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
5234
5235 HRESULT rc = S_OK;
5236
5237 try
5238 {
5239 ULONG uLogHistoryCount = 3;
5240 ComPtr<ISystemProperties> systemProperties;
5241 rc = mParent->COMGETTER(SystemProperties)(systemProperties.asOutParam());
5242 if (FAILED(rc)) throw rc;
5243
5244 if (!systemProperties.isNull())
5245 {
5246 rc = systemProperties->COMGETTER(LogHistoryCount)(&uLogHistoryCount);
5247 if (FAILED(rc)) throw rc;
5248 }
5249
5250 MachineState_T oldState = mData->mMachineState;
5251 i_setMachineState(MachineState_SettingUp);
5252 alock.release();
5253 for (size_t i = 0; i < task.llMediums.size(); ++i)
5254 {
5255 ComObjPtr<Medium> pMedium = (Medium*)(IMedium*)task.llMediums.at(i);
5256 {
5257 AutoCaller mac(pMedium);
5258 if (FAILED(mac.rc())) throw mac.rc();
5259 Utf8Str strLocation = pMedium->i_getLocationFull();
5260 rc = task.pProgress->SetNextOperation(BstrFmt(tr("Deleting '%s'"), strLocation.c_str()).raw(), 1);
5261 if (FAILED(rc)) throw rc;
5262 LogFunc(("Deleting file %s\n", strLocation.c_str()));
5263 }
5264 ComPtr<IProgress> pProgress2;
5265 rc = pMedium->DeleteStorage(pProgress2.asOutParam());
5266 if (FAILED(rc)) throw rc;
5267 rc = task.pProgress->WaitForAsyncProgressCompletion(pProgress2);
5268 if (FAILED(rc)) throw rc;
5269 /* Check the result of the asynchronous process. */
5270 LONG iRc;
5271 rc = pProgress2->COMGETTER(ResultCode)(&iRc);
5272 if (FAILED(rc)) throw rc;
5273 /* If the thread of the progress object has an error, then
5274 * retrieve the error info from there, or it'll be lost. */
5275 if (FAILED(iRc))
5276 throw setError(ProgressErrorInfo(pProgress2));
5277
5278 /* Close the medium, deliberately without checking the return
5279- * code, and without leaving any trace in the error info, as
5280- * a failure here is a very minor issue, which shouldn't happen
5281- * as above we even managed to delete the medium. */
5282 {
5283 ErrorInfoKeeper eik;
5284 pMedium->Close();
5285 }
5286 }
5287 i_setMachineState(oldState);
5288 alock.acquire();
5289
5290 // delete the files pushed on the task list by Machine::Delete()
5291 // (this includes saved states of the machine and snapshots and
5292 // medium storage files from the IMedium list passed in, and the
5293 // machine XML file)
5294 StringsList::const_iterator it = task.llFilesToDelete.begin();
5295 while (it != task.llFilesToDelete.end())
5296 {
5297 const Utf8Str &strFile = *it;
5298 LogFunc(("Deleting file %s\n", strFile.c_str()));
5299 int vrc = RTFileDelete(strFile.c_str());
5300 if (RT_FAILURE(vrc))
5301 throw setError(VBOX_E_IPRT_ERROR,
5302 tr("Could not delete file '%s' (%Rrc)"), strFile.c_str(), vrc);
5303
5304 ++it;
5305 if (it == task.llFilesToDelete.end())
5306 {
5307 rc = task.pProgress->SetNextOperation(Bstr(tr("Cleaning up machine directory")).raw(), 1);
5308 if (FAILED(rc)) throw rc;
5309 break;
5310 }
5311
5312 rc = task.pProgress->SetNextOperation(BstrFmt(tr("Deleting '%s'"), it->c_str()).raw(), 1);
5313 if (FAILED(rc)) throw rc;
5314 }
5315
5316 /* delete the settings only when the file actually exists */
5317 if (mData->pMachineConfigFile->fileExists())
5318 {
5319 /* Delete any backup or uncommitted XML files. Ignore failures.
5320 See the fSafe parameter of xml::XmlFileWriter::write for details. */
5321 /** @todo Find a way to avoid referring directly to iprt/xml.h here. */
5322 Utf8Str otherXml = Utf8StrFmt("%s%s", mData->m_strConfigFileFull.c_str(), xml::XmlFileWriter::s_pszTmpSuff);
5323 RTFileDelete(otherXml.c_str());
5324 otherXml = Utf8StrFmt("%s%s", mData->m_strConfigFileFull.c_str(), xml::XmlFileWriter::s_pszPrevSuff);
5325 RTFileDelete(otherXml.c_str());
5326
5327 /* delete the Logs folder, nothing important should be left
5328 * there (we don't check for errors because the user might have
5329 * some private files there that we don't want to delete) */
5330 Utf8Str logFolder;
5331 getLogFolder(logFolder);
5332 Assert(logFolder.length());
5333 if (RTDirExists(logFolder.c_str()))
5334 {
5335 /* Delete all VBox.log[.N] files from the Logs folder
5336 * (this must be in sync with the rotation logic in
5337 * Console::powerUpThread()). Also, delete the VBox.png[.N]
5338 * files that may have been created by the GUI. */
5339 Utf8Str log = Utf8StrFmt("%s%cVBox.log",
5340 logFolder.c_str(), RTPATH_DELIMITER);
5341 RTFileDelete(log.c_str());
5342 log = Utf8StrFmt("%s%cVBox.png",
5343 logFolder.c_str(), RTPATH_DELIMITER);
5344 RTFileDelete(log.c_str());
5345 for (int i = uLogHistoryCount; i > 0; i--)
5346 {
5347 log = Utf8StrFmt("%s%cVBox.log.%d",
5348 logFolder.c_str(), RTPATH_DELIMITER, i);
5349 RTFileDelete(log.c_str());
5350 log = Utf8StrFmt("%s%cVBox.png.%d",
5351 logFolder.c_str(), RTPATH_DELIMITER, i);
5352 RTFileDelete(log.c_str());
5353 }
5354
5355 RTDirRemove(logFolder.c_str());
5356 }
5357
5358 /* delete the Snapshots folder, nothing important should be left
5359 * there (we don't check for errors because the user might have
5360 * some private files there that we don't want to delete) */
5361 Utf8Str strFullSnapshotFolder;
5362 i_calculateFullPath(mUserData->s.strSnapshotFolder, strFullSnapshotFolder);
5363 Assert(!strFullSnapshotFolder.isEmpty());
5364 if (RTDirExists(strFullSnapshotFolder.c_str()))
5365 RTDirRemove(strFullSnapshotFolder.c_str());
5366
5367 // delete the directory that contains the settings file, but only
5368 // if it matches the VM name
5369 Utf8Str settingsDir;
5370 if (i_isInOwnDir(&settingsDir))
5371 RTDirRemove(settingsDir.c_str());
5372 }
5373
5374 alock.release();
5375
5376 mParent->i_saveModifiedRegistries();
5377 }
5378 catch (HRESULT aRC) { rc = aRC; }
5379
5380 return rc;
5381}
5382
5383HRESULT Machine::findSnapshot(const com::Utf8Str &aNameOrId, ComPtr<ISnapshot> &aSnapshot)
5384{
5385 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
5386
5387 ComObjPtr<Snapshot> pSnapshot;
5388 HRESULT rc;
5389
5390 if (aNameOrId.isEmpty())
5391 // null case (caller wants root snapshot): i_findSnapshotById() handles this
5392 rc = i_findSnapshotById(Guid(), pSnapshot, true /* aSetError */);
5393 else
5394 {
5395 Guid uuid(aNameOrId);
5396 if (uuid.isValid())
5397 rc = i_findSnapshotById(uuid, pSnapshot, true /* aSetError */);
5398 else
5399 rc = i_findSnapshotByName(aNameOrId, pSnapshot, true /* aSetError */);
5400 }
5401 pSnapshot.queryInterfaceTo(aSnapshot.asOutParam());
5402
5403 return rc;
5404}
5405
5406HRESULT Machine::createSharedFolder(const com::Utf8Str &aName, const com::Utf8Str &aHostPath, BOOL aWritable, BOOL aAutomount)
5407{
5408 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
5409
5410 HRESULT rc = i_checkStateDependency(MutableStateDep);
5411 if (FAILED(rc)) return rc;
5412
5413 ComObjPtr<SharedFolder> sharedFolder;
5414 rc = i_findSharedFolder(aName, sharedFolder, false /* aSetError */);
5415 if (SUCCEEDED(rc))
5416 return setError(VBOX_E_OBJECT_IN_USE,
5417 tr("Shared folder named '%s' already exists"),
5418 aName.c_str());
5419
5420 sharedFolder.createObject();
5421 rc = sharedFolder->init(i_getMachine(),
5422 aName,
5423 aHostPath,
5424 !!aWritable,
5425 !!aAutomount,
5426 true /* fFailOnError */);
5427 if (FAILED(rc)) return rc;
5428
5429 i_setModified(IsModified_SharedFolders);
5430 mHWData.backup();
5431 mHWData->mSharedFolders.push_back(sharedFolder);
5432
5433 /* inform the direct session if any */
5434 alock.release();
5435 i_onSharedFolderChange();
5436
5437 return S_OK;
5438}
5439
5440HRESULT Machine::removeSharedFolder(const com::Utf8Str &aName)
5441{
5442 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
5443
5444 HRESULT rc = i_checkStateDependency(MutableStateDep);
5445 if (FAILED(rc)) return rc;
5446
5447 ComObjPtr<SharedFolder> sharedFolder;
5448 rc = i_findSharedFolder(aName, sharedFolder, true /* aSetError */);
5449 if (FAILED(rc)) return rc;
5450
5451 i_setModified(IsModified_SharedFolders);
5452 mHWData.backup();
5453 mHWData->mSharedFolders.remove(sharedFolder);
5454
5455 /* inform the direct session if any */
5456 alock.release();
5457 i_onSharedFolderChange();
5458
5459 return S_OK;
5460}
5461
5462HRESULT Machine::canShowConsoleWindow(BOOL *aCanShow)
5463{
5464 /* start with No */
5465 *aCanShow = FALSE;
5466
5467 ComPtr<IInternalSessionControl> directControl;
5468 {
5469 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
5470
5471 if (mData->mSession.mState != SessionState_Locked)
5472 return setError(VBOX_E_INVALID_VM_STATE,
5473 tr("Machine is not locked for session (session state: %s)"),
5474 Global::stringifySessionState(mData->mSession.mState));
5475
5476 directControl = mData->mSession.mDirectControl;
5477 }
5478
5479 /* ignore calls made after #OnSessionEnd() is called */
5480 if (!directControl)
5481 return S_OK;
5482
5483 LONG64 dummy;
5484 return directControl->OnShowWindow(TRUE /* aCheck */, aCanShow, &dummy);
5485}
5486
5487HRESULT Machine::showConsoleWindow(LONG64 *aWinId)
5488{
5489 ComPtr<IInternalSessionControl> directControl;
5490 {
5491 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
5492
5493 if (mData->mSession.mState != SessionState_Locked)
5494 return setError(E_FAIL,
5495 tr("Machine is not locked for session (session state: %s)"),
5496 Global::stringifySessionState(mData->mSession.mState));
5497
5498 directControl = mData->mSession.mDirectControl;
5499 }
5500
5501 /* ignore calls made after #OnSessionEnd() is called */
5502 if (!directControl)
5503 return S_OK;
5504
5505 BOOL dummy;
5506 return directControl->OnShowWindow(FALSE /* aCheck */, &dummy, aWinId);
5507}
5508
5509#ifdef VBOX_WITH_GUEST_PROPS
5510/**
5511 * Look up a guest property in VBoxSVC's internal structures.
5512 */
5513HRESULT Machine::i_getGuestPropertyFromService(const com::Utf8Str &aName,
5514 com::Utf8Str &aValue,
5515 LONG64 *aTimestamp,
5516 com::Utf8Str &aFlags) const
5517{
5518 using namespace guestProp;
5519
5520 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
5521 HWData::GuestPropertyMap::const_iterator it = mHWData->mGuestProperties.find(aName);
5522
5523 if (it != mHWData->mGuestProperties.end())
5524 {
5525 char szFlags[MAX_FLAGS_LEN + 1];
5526 aValue = it->second.strValue;
5527 *aTimestamp = it->second.mTimestamp;
5528 writeFlags(it->second.mFlags, szFlags);
5529 aFlags = Utf8Str(szFlags);
5530 }
5531
5532 return S_OK;
5533}
5534
5535/**
5536 * Query the VM that a guest property belongs to for the property.
5537 * @returns E_ACCESSDENIED if the VM process is not available or not
5538 * currently handling queries and the lookup should then be done in
5539 * VBoxSVC.
5540 */
5541HRESULT Machine::i_getGuestPropertyFromVM(const com::Utf8Str &aName,
5542 com::Utf8Str &aValue,
5543 LONG64 *aTimestamp,
5544 com::Utf8Str &aFlags) const
5545{
5546 HRESULT rc = S_OK;
5547 BSTR bValue = NULL;
5548 BSTR bFlags = NULL;
5549
5550 ComPtr<IInternalSessionControl> directControl;
5551 directControl = mData->mSession.mDirectControl;
5552
5553 /* fail if we were called after #OnSessionEnd() is called. This is a
5554 * silly race condition. */
5555
5556 /** @todo This code is bothering API clients (like python script clients) with
5557 * the AccessGuestProperty call, creating unncessary IPC. Need to
5558 * have a way of figuring out which kind of direct session it is... */
5559 if (!directControl)
5560 rc = E_ACCESSDENIED;
5561 else
5562 rc = directControl->AccessGuestProperty(Bstr(aName).raw(), Bstr("").raw(), Bstr("").raw(),
5563 0 /* accessMode */,
5564 &bValue, aTimestamp, &bFlags);
5565
5566 aValue = bValue;
5567 aFlags = bFlags;
5568
5569 return rc;
5570}
5571#endif // VBOX_WITH_GUEST_PROPS
5572
5573HRESULT Machine::getGuestProperty(const com::Utf8Str &aName,
5574 com::Utf8Str &aValue,
5575 LONG64 *aTimestamp,
5576 com::Utf8Str &aFlags)
5577{
5578#ifndef VBOX_WITH_GUEST_PROPS
5579 ReturnComNotImplemented();
5580#else // VBOX_WITH_GUEST_PROPS
5581
5582 HRESULT rc = i_getGuestPropertyFromVM(aName, aValue, aTimestamp, aFlags);
5583
5584 if (rc == E_ACCESSDENIED)
5585 /* The VM is not running or the service is not (yet) accessible */
5586 rc = i_getGuestPropertyFromService(aName, aValue, aTimestamp, aFlags);
5587 return rc;
5588#endif // VBOX_WITH_GUEST_PROPS
5589}
5590
5591HRESULT Machine::getGuestPropertyValue(const com::Utf8Str &aProperty, com::Utf8Str &aValue)
5592{
5593 LONG64 dummyTimestamp;
5594 com::Utf8Str dummyFlags;
5595 HRESULT rc = getGuestProperty(aProperty, aValue, &dummyTimestamp, dummyFlags);
5596 return rc;
5597
5598}
5599HRESULT Machine::getGuestPropertyTimestamp(const com::Utf8Str &aProperty, LONG64 *aValue)
5600{
5601 com::Utf8Str dummyFlags;
5602 com::Utf8Str dummyValue;
5603 HRESULT rc = getGuestProperty(aProperty, dummyValue, aValue, dummyFlags);
5604 return rc;
5605}
5606
5607#ifdef VBOX_WITH_GUEST_PROPS
5608/**
5609 * Set a guest property in VBoxSVC's internal structures.
5610 */
5611HRESULT Machine::i_setGuestPropertyToService(const com::Utf8Str &aName, const com::Utf8Str &aValue,
5612 const com::Utf8Str &aFlags, bool fDelete)
5613{
5614 using namespace guestProp;
5615
5616 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
5617 HRESULT rc = S_OK;
5618
5619 rc = i_checkStateDependency(MutableStateDep);
5620 if (FAILED(rc)) return rc;
5621
5622 try
5623 {
5624 uint32_t fFlags = NILFLAG;
5625 if (aFlags.length() && RT_FAILURE(validateFlags(aFlags.c_str(), &fFlags)))
5626 return setError(E_INVALIDARG, tr("Invalid guest property flag values: '%s'"), aFlags.c_str());
5627
5628 HWData::GuestPropertyMap::iterator it = mHWData->mGuestProperties.find(aName);
5629 if (it == mHWData->mGuestProperties.end())
5630 {
5631 if (!fDelete)
5632 {
5633 i_setModified(IsModified_MachineData);
5634 mHWData.backupEx();
5635
5636 RTTIMESPEC time;
5637 HWData::GuestProperty prop;
5638 prop.strValue = Bstr(aValue).raw();
5639 prop.mTimestamp = RTTimeSpecGetNano(RTTimeNow(&time));
5640 prop.mFlags = fFlags;
5641 mHWData->mGuestProperties[aName] = prop;
5642 }
5643 }
5644 else
5645 {
5646 if (it->second.mFlags & (RDONLYHOST))
5647 {
5648 rc = setError(E_ACCESSDENIED, tr("The property '%s' cannot be changed by the host"), aName.c_str());
5649 }
5650 else
5651 {
5652 i_setModified(IsModified_MachineData);
5653 mHWData.backupEx();
5654
5655 /* The backupEx() operation invalidates our iterator,
5656 * so get a new one. */
5657 it = mHWData->mGuestProperties.find(aName);
5658 Assert(it != mHWData->mGuestProperties.end());
5659
5660 if (!fDelete)
5661 {
5662 RTTIMESPEC time;
5663 it->second.strValue = aValue;
5664 it->second.mTimestamp = RTTimeSpecGetNano(RTTimeNow(&time));
5665 it->second.mFlags = fFlags;
5666 }
5667 else
5668 mHWData->mGuestProperties.erase(it);
5669 }
5670 }
5671
5672 if ( SUCCEEDED(rc)
5673 && ( mHWData->mGuestPropertyNotificationPatterns.isEmpty()
5674 || RTStrSimplePatternMultiMatch(mHWData->mGuestPropertyNotificationPatterns.c_str(),
5675 RTSTR_MAX,
5676 aName.c_str(),
5677 RTSTR_MAX,
5678 NULL)
5679 )
5680 )
5681 {
5682 alock.release();
5683
5684 mParent->i_onGuestPropertyChange(mData->mUuid,
5685 Bstr(aName).raw(),
5686 Bstr(aValue).raw(),
5687 Bstr(aFlags).raw());
5688 }
5689 }
5690 catch (std::bad_alloc &)
5691 {
5692 rc = E_OUTOFMEMORY;
5693 }
5694
5695 return rc;
5696}
5697
5698/**
5699 * Set a property on the VM that that property belongs to.
5700 * @returns E_ACCESSDENIED if the VM process is not available or not
5701 * currently handling queries and the setting should then be done in
5702 * VBoxSVC.
5703 */
5704HRESULT Machine::i_setGuestPropertyToVM(const com::Utf8Str &aName, const com::Utf8Str &aValue,
5705 const com::Utf8Str &aFlags, bool fDelete)
5706{
5707 HRESULT rc;
5708
5709 try
5710 {
5711 ComPtr<IInternalSessionControl> directControl = mData->mSession.mDirectControl;
5712
5713 BSTR dummy = NULL; /* will not be changed (setter) */
5714 LONG64 dummy64;
5715 if (!directControl)
5716 rc = E_ACCESSDENIED;
5717 else
5718 /** @todo Fix when adding DeleteGuestProperty(), see defect. */
5719 rc = directControl->AccessGuestProperty(Bstr(aName).raw(), Bstr(aValue).raw(), Bstr(aFlags).raw(),
5720 fDelete? 2: 1 /* accessMode */,
5721 &dummy, &dummy64, &dummy);
5722 }
5723 catch (std::bad_alloc &)
5724 {
5725 rc = E_OUTOFMEMORY;
5726 }
5727
5728 return rc;
5729}
5730#endif // VBOX_WITH_GUEST_PROPS
5731
5732HRESULT Machine::setGuestProperty(const com::Utf8Str &aProperty, const com::Utf8Str &aValue,
5733 const com::Utf8Str &aFlags)
5734{
5735#ifndef VBOX_WITH_GUEST_PROPS
5736 ReturnComNotImplemented();
5737#else // VBOX_WITH_GUEST_PROPS
5738 HRESULT rc = i_setGuestPropertyToVM(aProperty, aValue, aFlags, /* fDelete = */ false);
5739 if (rc == E_ACCESSDENIED)
5740 /* The VM is not running or the service is not (yet) accessible */
5741 rc = i_setGuestPropertyToService(aProperty, aValue, aFlags, /* fDelete = */ false);
5742 return rc;
5743#endif // VBOX_WITH_GUEST_PROPS
5744}
5745
5746HRESULT Machine::setGuestPropertyValue(const com::Utf8Str &aProperty, const com::Utf8Str &aValue)
5747{
5748 return setGuestProperty(aProperty, aValue, "");
5749}
5750
5751HRESULT Machine::deleteGuestProperty(const com::Utf8Str &aName)
5752{
5753#ifndef VBOX_WITH_GUEST_PROPS
5754 ReturnComNotImplemented();
5755#else // VBOX_WITH_GUEST_PROPS
5756 HRESULT rc = i_setGuestPropertyToVM(aName, "", "", /* fDelete = */ true);
5757 if (rc == E_ACCESSDENIED)
5758 /* The VM is not running or the service is not (yet) accessible */
5759 rc = i_setGuestPropertyToService(aName, "", "", /* fDelete = */ true);
5760 return rc;
5761#endif // VBOX_WITH_GUEST_PROPS
5762}
5763
5764#ifdef VBOX_WITH_GUEST_PROPS
5765/**
5766 * Enumerate the guest properties in VBoxSVC's internal structures.
5767 */
5768HRESULT Machine::i_enumerateGuestPropertiesInService(const com::Utf8Str &aPatterns,
5769 std::vector<com::Utf8Str> &aNames,
5770 std::vector<com::Utf8Str> &aValues,
5771 std::vector<LONG64> &aTimestamps,
5772 std::vector<com::Utf8Str> &aFlags)
5773{
5774 using namespace guestProp;
5775
5776 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
5777 Utf8Str strPatterns(aPatterns);
5778
5779 HWData::GuestPropertyMap propMap;
5780
5781 /*
5782 * Look for matching patterns and build up a list.
5783 */
5784 HWData::GuestPropertyMap::const_iterator it = mHWData->mGuestProperties.begin();
5785 while (it != mHWData->mGuestProperties.end())
5786 {
5787 if ( strPatterns.isEmpty()
5788 || RTStrSimplePatternMultiMatch(strPatterns.c_str(),
5789 RTSTR_MAX,
5790 it->first.c_str(),
5791 RTSTR_MAX,
5792 NULL)
5793 )
5794 propMap.insert(*it);
5795 it++;
5796 }
5797
5798 alock.release();
5799
5800 /*
5801 * And build up the arrays for returning the property information.
5802 */
5803 size_t cEntries = propMap.size();
5804
5805 aNames.resize(cEntries);
5806 aValues.resize(cEntries);
5807 aTimestamps.resize(cEntries);
5808 aFlags.resize(cEntries);
5809
5810 char szFlags[MAX_FLAGS_LEN + 1];
5811 size_t i= 0;
5812 for (it = propMap.begin(); it != propMap.end(); ++i, ++it)
5813 {
5814 aNames[i] = it->first;
5815 aValues[i] = it->second.strValue;
5816 aTimestamps[i] = it->second.mTimestamp;
5817 writeFlags(it->second.mFlags, szFlags);
5818 aFlags[i] = Utf8Str(szFlags);
5819 }
5820
5821 return S_OK;
5822}
5823
5824/**
5825 * Enumerate the properties managed by a VM.
5826 * @returns E_ACCESSDENIED if the VM process is not available or not
5827 * currently handling queries and the setting should then be done in
5828 * VBoxSVC.
5829 */
5830HRESULT Machine::i_enumerateGuestPropertiesOnVM(const com::Utf8Str &aPatterns,
5831 std::vector<com::Utf8Str> &aNames,
5832 std::vector<com::Utf8Str> &aValues,
5833 std::vector<LONG64> &aTimestamps,
5834 std::vector<com::Utf8Str> &aFlags)
5835{
5836 HRESULT rc;
5837 ComPtr<IInternalSessionControl> directControl;
5838 directControl = mData->mSession.mDirectControl;
5839
5840
5841 com::SafeArray<BSTR> bNames;
5842 com::SafeArray<BSTR> bValues;
5843 com::SafeArray<LONG64> bTimestamps;
5844 com::SafeArray<BSTR> bFlags;
5845
5846 if (!directControl)
5847 rc = E_ACCESSDENIED;
5848 else
5849 rc = directControl->EnumerateGuestProperties(Bstr(aPatterns).raw(),
5850 ComSafeArrayAsOutParam(bNames),
5851 ComSafeArrayAsOutParam(bValues),
5852 ComSafeArrayAsOutParam(bTimestamps),
5853 ComSafeArrayAsOutParam(bFlags));
5854 size_t i;
5855 aNames.resize(bNames.size());
5856 for (i = 0; i < bNames.size(); ++i)
5857 aNames[i] = Utf8Str(bNames[i]);
5858 aValues.resize(bValues.size());
5859 for (i = 0; i < bValues.size(); ++i)
5860 aValues[i] = Utf8Str(bValues[i]);
5861 aTimestamps.resize(bTimestamps.size());
5862 for (i = 0; i < bTimestamps.size(); ++i)
5863 aTimestamps[i] = bTimestamps[i];
5864 aFlags.resize(bFlags.size());
5865 for (i = 0; i < bFlags.size(); ++i)
5866 aFlags[i] = Utf8Str(bFlags[i]);
5867
5868 return rc;
5869}
5870#endif // VBOX_WITH_GUEST_PROPS
5871HRESULT Machine::enumerateGuestProperties(const com::Utf8Str &aPatterns,
5872 std::vector<com::Utf8Str> &aNames,
5873 std::vector<com::Utf8Str> &aValues,
5874 std::vector<LONG64> &aTimestamps,
5875 std::vector<com::Utf8Str> &aFlags)
5876{
5877#ifndef VBOX_WITH_GUEST_PROPS
5878 ReturnComNotImplemented();
5879#else // VBOX_WITH_GUEST_PROPS
5880
5881 HRESULT rc = i_enumerateGuestPropertiesOnVM(aPatterns, aNames, aValues, aTimestamps, aFlags);
5882
5883 if (rc == E_ACCESSDENIED)
5884 /* The VM is not running or the service is not (yet) accessible */
5885 rc = i_enumerateGuestPropertiesInService(aPatterns, aNames, aValues, aTimestamps, aFlags);
5886 return rc;
5887#endif // VBOX_WITH_GUEST_PROPS
5888}
5889
5890HRESULT Machine::getMediumAttachmentsOfController(const com::Utf8Str &aName,
5891 std::vector<ComPtr<IMediumAttachment> > &aMediumAttachments)
5892{
5893 MediaData::AttachmentList atts;
5894
5895 HRESULT rc = i_getMediumAttachmentsOfController(aName, atts);
5896 if (FAILED(rc)) return rc;
5897
5898 size_t i = 0;
5899 aMediumAttachments.resize(atts.size());
5900 for (MediaData::AttachmentList::iterator it = atts.begin(); it != atts.end(); ++it, ++i)
5901 (*it).queryInterfaceTo(aMediumAttachments[i].asOutParam());
5902
5903 return S_OK;
5904}
5905
5906HRESULT Machine::getMediumAttachment(const com::Utf8Str &aName,
5907 LONG aControllerPort,
5908 LONG aDevice,
5909 ComPtr<IMediumAttachment> &aAttachment)
5910{
5911 LogFlowThisFunc(("aControllerName=\"%s\" aControllerPort=%d aDevice=%d\n",
5912 aName.c_str(), aControllerPort, aDevice));
5913
5914 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
5915
5916 aAttachment = NULL;
5917
5918 ComObjPtr<MediumAttachment> pAttach = i_findAttachment(mMediaData->mAttachments,
5919 Bstr(aName).raw(),
5920 aControllerPort,
5921 aDevice);
5922 if (pAttach.isNull())
5923 return setError(VBOX_E_OBJECT_NOT_FOUND,
5924 tr("No storage device attached to device slot %d on port %d of controller '%s'"),
5925 aDevice, aControllerPort, aName.c_str());
5926
5927 pAttach.queryInterfaceTo(aAttachment.asOutParam());
5928
5929 return S_OK;
5930}
5931
5932
5933HRESULT Machine::addStorageController(const com::Utf8Str &aName,
5934 StorageBus_T aConnectionType,
5935 ComPtr<IStorageController> &aController)
5936{
5937 if ( (aConnectionType <= StorageBus_Null)
5938 || (aConnectionType > StorageBus_USB))
5939 return setError(E_INVALIDARG,
5940 tr("Invalid connection type: %d"),
5941 aConnectionType);
5942
5943 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
5944
5945 HRESULT rc = i_checkStateDependency(MutableStateDep);
5946 if (FAILED(rc)) return rc;
5947
5948 /* try to find one with the name first. */
5949 ComObjPtr<StorageController> ctrl;
5950
5951 rc = i_getStorageControllerByName(aName, ctrl, false /* aSetError */);
5952 if (SUCCEEDED(rc))
5953 return setError(VBOX_E_OBJECT_IN_USE,
5954 tr("Storage controller named '%s' already exists"),
5955 aName.c_str());
5956
5957 ctrl.createObject();
5958
5959 /* get a new instance number for the storage controller */
5960 ULONG ulInstance = 0;
5961 bool fBootable = true;
5962 for (StorageControllerList::const_iterator it = mStorageControllers->begin();
5963 it != mStorageControllers->end();
5964 ++it)
5965 {
5966 if ((*it)->i_getStorageBus() == aConnectionType)
5967 {
5968 ULONG ulCurInst = (*it)->i_getInstance();
5969
5970 if (ulCurInst >= ulInstance)
5971 ulInstance = ulCurInst + 1;
5972
5973 /* Only one controller of each type can be marked as bootable. */
5974 if ((*it)->i_getBootable())
5975 fBootable = false;
5976 }
5977 }
5978
5979 rc = ctrl->init(this, aName, aConnectionType, ulInstance, fBootable);
5980 if (FAILED(rc)) return rc;
5981
5982 i_setModified(IsModified_Storage);
5983 mStorageControllers.backup();
5984 mStorageControllers->push_back(ctrl);
5985
5986 ctrl.queryInterfaceTo(aController.asOutParam());
5987
5988 /* inform the direct session if any */
5989 alock.release();
5990 i_onStorageControllerChange();
5991
5992 return S_OK;
5993}
5994
5995HRESULT Machine::getStorageControllerByName(const com::Utf8Str &aName,
5996 ComPtr<IStorageController> &aStorageController)
5997{
5998 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
5999
6000 ComObjPtr<StorageController> ctrl;
6001
6002 HRESULT rc = i_getStorageControllerByName(aName, ctrl, true /* aSetError */);
6003 if (SUCCEEDED(rc))
6004 ctrl.queryInterfaceTo(aStorageController.asOutParam());
6005
6006 return rc;
6007}
6008
6009HRESULT Machine::getStorageControllerByInstance(ULONG aInstance,
6010 ComPtr<IStorageController> &aStorageController)
6011{
6012 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
6013
6014 for (StorageControllerList::const_iterator it = mStorageControllers->begin();
6015 it != mStorageControllers->end();
6016 ++it)
6017 {
6018 if ((*it)->i_getInstance() == aInstance)
6019 {
6020 (*it).queryInterfaceTo(aStorageController.asOutParam());
6021 return S_OK;
6022 }
6023 }
6024
6025 return setError(VBOX_E_OBJECT_NOT_FOUND,
6026 tr("Could not find a storage controller with instance number '%lu'"),
6027 aInstance);
6028}
6029
6030HRESULT Machine::setStorageControllerBootable(const com::Utf8Str &aName, BOOL aBootable)
6031{
6032 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
6033
6034 HRESULT rc = i_checkStateDependency(MutableStateDep);
6035 if (FAILED(rc)) return rc;
6036
6037 ComObjPtr<StorageController> ctrl;
6038
6039 rc = i_getStorageControllerByName(aName, ctrl, true /* aSetError */);
6040 if (SUCCEEDED(rc))
6041 {
6042 /* Ensure that only one controller of each type is marked as bootable. */
6043 if (aBootable == TRUE)
6044 {
6045 for (StorageControllerList::const_iterator it = mStorageControllers->begin();
6046 it != mStorageControllers->end();
6047 ++it)
6048 {
6049 ComObjPtr<StorageController> aCtrl = (*it);
6050
6051 if ( (aCtrl->i_getName() != aName)
6052 && aCtrl->i_getBootable() == TRUE
6053 && aCtrl->i_getStorageBus() == ctrl->i_getStorageBus()
6054 && aCtrl->i_getControllerType() == ctrl->i_getControllerType())
6055 {
6056 aCtrl->i_setBootable(FALSE);
6057 break;
6058 }
6059 }
6060 }
6061
6062 if (SUCCEEDED(rc))
6063 {
6064 ctrl->i_setBootable(aBootable);
6065 i_setModified(IsModified_Storage);
6066 }
6067 }
6068
6069 if (SUCCEEDED(rc))
6070 {
6071 /* inform the direct session if any */
6072 alock.release();
6073 i_onStorageControllerChange();
6074 }
6075
6076 return rc;
6077}
6078
6079HRESULT Machine::removeStorageController(const com::Utf8Str &aName)
6080{
6081 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
6082
6083 HRESULT rc = i_checkStateDependency(MutableStateDep);
6084 if (FAILED(rc)) return rc;
6085
6086 ComObjPtr<StorageController> ctrl;
6087 rc = i_getStorageControllerByName(aName, ctrl, true /* aSetError */);
6088 if (FAILED(rc)) return rc;
6089
6090 {
6091 /* find all attached devices to the appropriate storage controller and detach them all */
6092 // make a temporary list because detachDevice invalidates iterators into
6093 // mMediaData->mAttachments
6094 MediaData::AttachmentList llAttachments2 = mMediaData->mAttachments;
6095
6096 for (MediaData::AttachmentList::iterator it = llAttachments2.begin();
6097 it != llAttachments2.end();
6098 ++it)
6099 {
6100 MediumAttachment *pAttachTemp = *it;
6101
6102 AutoCaller localAutoCaller(pAttachTemp);
6103 if (FAILED(localAutoCaller.rc())) return localAutoCaller.rc();
6104
6105 AutoReadLock local_alock(pAttachTemp COMMA_LOCKVAL_SRC_POS);
6106
6107 if (pAttachTemp->i_getControllerName() == aName)
6108 {
6109 rc = i_detachDevice(pAttachTemp, alock, NULL);
6110 if (FAILED(rc)) return rc;
6111 }
6112 }
6113 }
6114
6115 /* We can remove it now. */
6116 i_setModified(IsModified_Storage);
6117 mStorageControllers.backup();
6118
6119 ctrl->i_unshare();
6120
6121 mStorageControllers->remove(ctrl);
6122
6123 /* inform the direct session if any */
6124 alock.release();
6125 i_onStorageControllerChange();
6126
6127 return S_OK;
6128}
6129
6130HRESULT Machine::addUSBController(const com::Utf8Str &aName, USBControllerType_T aType,
6131 ComPtr<IUSBController> &aController)
6132{
6133 if ( (aType <= USBControllerType_Null)
6134 || (aType >= USBControllerType_Last))
6135 return setError(E_INVALIDARG,
6136 tr("Invalid USB controller type: %d"),
6137 aType);
6138
6139 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
6140
6141 HRESULT rc = i_checkStateDependency(MutableStateDep);
6142 if (FAILED(rc)) return rc;
6143
6144 /* try to find one with the same type first. */
6145 ComObjPtr<USBController> ctrl;
6146
6147 rc = i_getUSBControllerByName(aName, ctrl, false /* aSetError */);
6148 if (SUCCEEDED(rc))
6149 return setError(VBOX_E_OBJECT_IN_USE,
6150 tr("USB controller named '%s' already exists"),
6151 aName.c_str());
6152
6153 /* Check that we don't exceed the maximum number of USB controllers for the given type. */
6154 ULONG maxInstances;
6155 rc = mParent->i_getSystemProperties()->GetMaxInstancesOfUSBControllerType(mHWData->mChipsetType, aType, &maxInstances);
6156 if (FAILED(rc))
6157 return rc;
6158
6159 ULONG cInstances = i_getUSBControllerCountByType(aType);
6160 if (cInstances >= maxInstances)
6161 return setError(E_INVALIDARG,
6162 tr("Too many USB controllers of this type"));
6163
6164 ctrl.createObject();
6165
6166 rc = ctrl->init(this, aName, aType);
6167 if (FAILED(rc)) return rc;
6168
6169 i_setModified(IsModified_USB);
6170 mUSBControllers.backup();
6171 mUSBControllers->push_back(ctrl);
6172
6173 ctrl.queryInterfaceTo(aController.asOutParam());
6174
6175 /* inform the direct session if any */
6176 alock.release();
6177 i_onUSBControllerChange();
6178
6179 return S_OK;
6180}
6181
6182HRESULT Machine::getUSBControllerByName(const com::Utf8Str &aName, ComPtr<IUSBController> &aController)
6183{
6184 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
6185
6186 ComObjPtr<USBController> ctrl;
6187
6188 HRESULT rc = i_getUSBControllerByName(aName, ctrl, true /* aSetError */);
6189 if (SUCCEEDED(rc))
6190 ctrl.queryInterfaceTo(aController.asOutParam());
6191
6192 return rc;
6193}
6194
6195HRESULT Machine::getUSBControllerCountByType(USBControllerType_T aType,
6196 ULONG *aControllers)
6197{
6198 if ( (aType <= USBControllerType_Null)
6199 || (aType >= USBControllerType_Last))
6200 return setError(E_INVALIDARG,
6201 tr("Invalid USB controller type: %d"),
6202 aType);
6203
6204 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
6205
6206 ComObjPtr<USBController> ctrl;
6207
6208 *aControllers = i_getUSBControllerCountByType(aType);
6209
6210 return S_OK;
6211}
6212
6213HRESULT Machine::removeUSBController(const com::Utf8Str &aName)
6214{
6215
6216 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
6217
6218 HRESULT rc = i_checkStateDependency(MutableStateDep);
6219 if (FAILED(rc)) return rc;
6220
6221 ComObjPtr<USBController> ctrl;
6222 rc = i_getUSBControllerByName(aName, ctrl, true /* aSetError */);
6223 if (FAILED(rc)) return rc;
6224
6225 i_setModified(IsModified_USB);
6226 mUSBControllers.backup();
6227
6228 ctrl->i_unshare();
6229
6230 mUSBControllers->remove(ctrl);
6231
6232 /* inform the direct session if any */
6233 alock.release();
6234 i_onUSBControllerChange();
6235
6236 return S_OK;
6237}
6238
6239HRESULT Machine::querySavedGuestScreenInfo(ULONG aScreenId,
6240 ULONG *aOriginX,
6241 ULONG *aOriginY,
6242 ULONG *aWidth,
6243 ULONG *aHeight,
6244 BOOL *aEnabled)
6245{
6246 uint32_t u32OriginX= 0;
6247 uint32_t u32OriginY= 0;
6248 uint32_t u32Width = 0;
6249 uint32_t u32Height = 0;
6250 uint16_t u16Flags = 0;
6251
6252 int vrc = readSavedGuestScreenInfo(mSSData->strStateFilePath, aScreenId,
6253 &u32OriginX, &u32OriginY, &u32Width, &u32Height, &u16Flags);
6254 if (RT_FAILURE(vrc))
6255 {
6256#ifdef RT_OS_WINDOWS
6257 /* HACK: GUI sets *pfEnabled to 'true' and expects it to stay so if the API fails.
6258 * This works with XPCOM. But Windows COM sets all output parameters to zero.
6259 * So just assign fEnable to TRUE again.
6260 * The right fix would be to change GUI API wrappers to make sure that parameters
6261 * are changed only if API succeeds.
6262 */
6263 *aEnabled = TRUE;
6264#endif
6265 return setError(VBOX_E_IPRT_ERROR,
6266 tr("Saved guest size is not available (%Rrc)"),
6267 vrc);
6268 }
6269
6270 *aOriginX = u32OriginX;
6271 *aOriginY = u32OriginY;
6272 *aWidth = u32Width;
6273 *aHeight = u32Height;
6274 *aEnabled = (u16Flags & VBVA_SCREEN_F_DISABLED) == 0;
6275
6276 return S_OK;
6277}
6278
6279HRESULT Machine::querySavedThumbnailSize(ULONG aScreenId, ULONG *aSize, ULONG *aWidth, ULONG *aHeight)
6280{
6281 if (aScreenId != 0)
6282 return E_NOTIMPL;
6283
6284 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
6285
6286 uint8_t *pu8Data = NULL;
6287 uint32_t cbData = 0;
6288 uint32_t u32Width = 0;
6289 uint32_t u32Height = 0;
6290
6291 int vrc = readSavedDisplayScreenshot(mSSData->strStateFilePath, 0 /* u32Type */, &pu8Data, &cbData, &u32Width, &u32Height);
6292
6293 if (RT_FAILURE(vrc))
6294 return setError(VBOX_E_IPRT_ERROR,
6295 tr("Saved screenshot data is not available (%Rrc)"),
6296 vrc);
6297
6298 *aSize = cbData;
6299 *aWidth = u32Width;
6300 *aHeight = u32Height;
6301
6302 freeSavedDisplayScreenshot(pu8Data);
6303
6304 return S_OK;
6305}
6306
6307
6308HRESULT Machine::readSavedThumbnailToArray(ULONG aScreenId, BOOL aBGR, ULONG *aWidth, ULONG *aHeight, std::vector<BYTE> &aData)
6309{
6310 if (aScreenId != 0)
6311 return E_NOTIMPL;
6312
6313 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
6314
6315 uint8_t *pu8Data = NULL;
6316 uint32_t cbData = 0;
6317 uint32_t u32Width = 0;
6318 uint32_t u32Height = 0;
6319
6320 int vrc = readSavedDisplayScreenshot(mSSData->strStateFilePath, 0 /* u32Type */, &pu8Data, &cbData, &u32Width, &u32Height);
6321
6322 if (RT_FAILURE(vrc))
6323 return setError(VBOX_E_IPRT_ERROR,
6324 tr("Saved screenshot data is not available (%Rrc)"),
6325 vrc);
6326
6327 *aWidth = u32Width;
6328 *aHeight = u32Height;
6329
6330 com::SafeArray<BYTE> bitmap(cbData);
6331 /* Convert pixels to format expected by the API caller. */
6332 if (aBGR)
6333 {
6334 /* [0] B, [1] G, [2] R, [3] A. */
6335 for (unsigned i = 0; i < cbData; i += 4)
6336 {
6337 bitmap[i] = pu8Data[i];
6338 bitmap[i + 1] = pu8Data[i + 1];
6339 bitmap[i + 2] = pu8Data[i + 2];
6340 bitmap[i + 3] = 0xff;
6341 }
6342 }
6343 else
6344 {
6345 /* [0] R, [1] G, [2] B, [3] A. */
6346 for (unsigned i = 0; i < cbData; i += 4)
6347 {
6348 bitmap[i] = pu8Data[i + 2];
6349 bitmap[i + 1] = pu8Data[i + 1];
6350 bitmap[i + 2] = pu8Data[i];
6351 bitmap[i + 3] = 0xff;
6352 }
6353 }
6354 aData.resize(bitmap.size());
6355 for (size_t i = 0; i < bitmap.size(); ++i)
6356 aData[i] = bitmap[i];
6357
6358 freeSavedDisplayScreenshot(pu8Data);
6359
6360 return S_OK;
6361}
6362
6363HRESULT Machine::readSavedThumbnailPNGToArray(ULONG aScreenId, ULONG *aWidth, ULONG *aHeight, std::vector<BYTE> &aData)
6364{
6365 if (aScreenId != 0)
6366 return E_NOTIMPL;
6367
6368 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
6369
6370 uint8_t *pu8Data = NULL;
6371 uint32_t cbData = 0;
6372 uint32_t u32Width = 0;
6373 uint32_t u32Height = 0;
6374
6375 int vrc = readSavedDisplayScreenshot(mSSData->strStateFilePath, 0 /* u32Type */, &pu8Data, &cbData, &u32Width, &u32Height);
6376
6377 if (RT_FAILURE(vrc))
6378 return setError(VBOX_E_IPRT_ERROR,
6379 tr("Saved screenshot data is not available (%Rrc)"),
6380 vrc);
6381
6382 *aWidth = u32Width;
6383 *aHeight = u32Height;
6384
6385 HRESULT rc = S_OK;
6386 uint8_t *pu8PNG = NULL;
6387 uint32_t cbPNG = 0;
6388 uint32_t cxPNG = 0;
6389 uint32_t cyPNG = 0;
6390
6391 vrc = DisplayMakePNG(pu8Data, u32Width, u32Height, &pu8PNG, &cbPNG, &cxPNG, &cyPNG, 0);
6392
6393 if (RT_SUCCESS(vrc))
6394 {
6395 com::SafeArray<BYTE> screenData(cbPNG);
6396 screenData.initFrom(pu8PNG, cbPNG);
6397 if (pu8PNG)
6398 RTMemFree(pu8PNG);
6399 aData.resize(screenData.size());
6400 for (size_t i = 0; i < screenData.size(); ++i)
6401 aData[i] = screenData[i];
6402 }
6403 else
6404 {
6405 if (pu8PNG)
6406 RTMemFree(pu8PNG);
6407 return setError(VBOX_E_IPRT_ERROR,
6408 tr("Could not convert screenshot to PNG (%Rrc)"),
6409 vrc);
6410 }
6411
6412 freeSavedDisplayScreenshot(pu8Data);
6413
6414 return rc;
6415}
6416
6417HRESULT Machine::querySavedScreenshotPNGSize(ULONG aScreenId, ULONG *aSize, ULONG *aWidth, ULONG *aHeight)
6418{
6419 if (aScreenId != 0)
6420 return E_NOTIMPL;
6421
6422 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
6423
6424 uint8_t *pu8Data = NULL;
6425 uint32_t cbData = 0;
6426 uint32_t u32Width = 0;
6427 uint32_t u32Height = 0;
6428
6429 int vrc = readSavedDisplayScreenshot(mSSData->strStateFilePath, 1 /* u32Type */, &pu8Data, &cbData, &u32Width, &u32Height);
6430
6431 if (RT_FAILURE(vrc))
6432 return setError(VBOX_E_IPRT_ERROR,
6433 tr("Saved screenshot data is not available (%Rrc)"),
6434 vrc);
6435
6436 *aSize = cbData;
6437 *aWidth = u32Width;
6438 *aHeight = u32Height;
6439
6440 freeSavedDisplayScreenshot(pu8Data);
6441
6442 return S_OK;
6443}
6444
6445HRESULT Machine::readSavedScreenshotPNGToArray(ULONG aScreenId, ULONG *aWidth, ULONG *aHeight, std::vector<BYTE> &aData)
6446{
6447 if (aScreenId != 0)
6448 return E_NOTIMPL;
6449
6450 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
6451
6452 uint8_t *pu8Data = NULL;
6453 uint32_t cbData = 0;
6454 uint32_t u32Width = 0;
6455 uint32_t u32Height = 0;
6456
6457 int vrc = readSavedDisplayScreenshot(mSSData->strStateFilePath, 1 /* u32Type */, &pu8Data, &cbData, &u32Width, &u32Height);
6458
6459 if (RT_FAILURE(vrc))
6460 return setError(VBOX_E_IPRT_ERROR,
6461 tr("Saved screenshot thumbnail data is not available (%Rrc)"),
6462 vrc);
6463
6464 *aWidth = u32Width;
6465 *aHeight = u32Height;
6466
6467 com::SafeArray<BYTE> png(cbData);
6468 png.initFrom(pu8Data, cbData);
6469 aData.resize(png.size());
6470 for (size_t i = 0; i < png.size(); ++i)
6471 aData[i] = png[i];
6472
6473 freeSavedDisplayScreenshot(pu8Data);
6474
6475 return S_OK;
6476}
6477
6478HRESULT Machine::hotPlugCPU(ULONG aCpu)
6479{
6480 HRESULT rc = S_OK;
6481 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
6482
6483 if (!mHWData->mCPUHotPlugEnabled)
6484 return setError(E_INVALIDARG, tr("CPU hotplug is not enabled"));
6485
6486 if (aCpu >= mHWData->mCPUCount)
6487 return setError(E_INVALIDARG, tr("CPU id exceeds number of possible CPUs [0:%lu]"), mHWData->mCPUCount-1);
6488
6489 if (mHWData->mCPUAttached[aCpu])
6490 return setError(VBOX_E_OBJECT_IN_USE, tr("CPU %lu is already attached"), aCpu);
6491
6492 alock.release();
6493 rc = i_onCPUChange(aCpu, false);
6494 alock.acquire();
6495 if (FAILED(rc)) return rc;
6496
6497 i_setModified(IsModified_MachineData);
6498 mHWData.backup();
6499 mHWData->mCPUAttached[aCpu] = true;
6500
6501 /** Save settings if online - @todo why is this required? -- @bugref{6818} */
6502 if (Global::IsOnline(mData->mMachineState))
6503 i_saveSettings(NULL);
6504
6505 return S_OK;
6506}
6507
6508HRESULT Machine::hotUnplugCPU(ULONG aCpu)
6509{
6510 HRESULT rc = S_OK;
6511
6512 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
6513
6514 if (!mHWData->mCPUHotPlugEnabled)
6515 return setError(E_INVALIDARG, tr("CPU hotplug is not enabled"));
6516
6517 if (aCpu >= SchemaDefs::MaxCPUCount)
6518 return setError(E_INVALIDARG,
6519 tr("CPU index exceeds maximum CPU count (must be in range [0:%lu])"),
6520 SchemaDefs::MaxCPUCount);
6521
6522 if (!mHWData->mCPUAttached[aCpu])
6523 return setError(VBOX_E_OBJECT_NOT_FOUND, tr("CPU %lu is not attached"), aCpu);
6524
6525 /* CPU 0 can't be detached */
6526 if (aCpu == 0)
6527 return setError(E_INVALIDARG, tr("It is not possible to detach CPU 0"));
6528
6529 alock.release();
6530 rc = i_onCPUChange(aCpu, true);
6531 alock.acquire();
6532 if (FAILED(rc)) return rc;
6533
6534 i_setModified(IsModified_MachineData);
6535 mHWData.backup();
6536 mHWData->mCPUAttached[aCpu] = false;
6537
6538 /** Save settings if online - @todo why is this required? -- @bugref{6818} */
6539 if (Global::IsOnline(mData->mMachineState))
6540 i_saveSettings(NULL);
6541
6542 return S_OK;
6543}
6544
6545HRESULT Machine::getCPUStatus(ULONG aCpu, BOOL *aAttached)
6546{
6547 *aAttached = false;
6548
6549 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
6550
6551 /* If hotplug is enabled the CPU is always enabled. */
6552 if (!mHWData->mCPUHotPlugEnabled)
6553 {
6554 if (aCpu < mHWData->mCPUCount)
6555 *aAttached = true;
6556 }
6557 else
6558 {
6559 if (aCpu < SchemaDefs::MaxCPUCount)
6560 *aAttached = mHWData->mCPUAttached[aCpu];
6561 }
6562
6563 return S_OK;
6564}
6565
6566HRESULT Machine::queryLogFilename(ULONG aIdx, com::Utf8Str &aFilename)
6567{
6568 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
6569
6570 Utf8Str log = i_queryLogFilename(aIdx);
6571 if (!RTFileExists(log.c_str()))
6572 log.setNull();
6573 aFilename = log;
6574
6575 return S_OK;
6576}
6577
6578HRESULT Machine::readLog(ULONG aIdx, LONG64 aOffset, LONG64 aSize, std::vector<BYTE> &aData)
6579{
6580 if (aSize < 0)
6581 return setError(E_INVALIDARG, tr("The size argument (%lld) is negative"), aSize);
6582
6583 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
6584
6585 HRESULT rc = S_OK;
6586 Utf8Str log = i_queryLogFilename(aIdx);
6587
6588 /* do not unnecessarily hold the lock while doing something which does
6589 * not need the lock and potentially takes a long time. */
6590 alock.release();
6591
6592 /* Limit the chunk size to 32K for now, as that gives better performance
6593 * over (XP)COM, and keeps the SOAP reply size under 1M for the webservice.
6594 * One byte expands to approx. 25 bytes of breathtaking XML. */
6595 size_t cbData = (size_t)RT_MIN(aSize, 32768);
6596 com::SafeArray<BYTE> logData(cbData);
6597
6598 RTFILE LogFile;
6599 int vrc = RTFileOpen(&LogFile, log.c_str(),
6600 RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_DENY_NONE);
6601 if (RT_SUCCESS(vrc))
6602 {
6603 vrc = RTFileReadAt(LogFile, aOffset, logData.raw(), cbData, &cbData);
6604 if (RT_SUCCESS(vrc))
6605 logData.resize(cbData);
6606 else
6607 rc = setError(VBOX_E_IPRT_ERROR,
6608 tr("Could not read log file '%s' (%Rrc)"),
6609 log.c_str(), vrc);
6610 RTFileClose(LogFile);
6611 }
6612 else
6613 rc = setError(VBOX_E_IPRT_ERROR,
6614 tr("Could not open log file '%s' (%Rrc)"),
6615 log.c_str(), vrc);
6616
6617 if (FAILED(rc))
6618 logData.resize(0);
6619
6620 aData.resize(logData.size());
6621 for (size_t i = 0; i < logData.size(); ++i)
6622 aData[i] = logData[i];
6623
6624 return rc;
6625}
6626
6627
6628/**
6629 * Currently this method doesn't attach device to the running VM,
6630 * just makes sure it's plugged on next VM start.
6631 */
6632HRESULT Machine::attachHostPCIDevice(LONG aHostAddress, LONG aDesiredGuestAddress, BOOL /* aTryToUnbind */)
6633{
6634 // lock scope
6635 {
6636 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
6637
6638 HRESULT rc = i_checkStateDependency(MutableStateDep);
6639 if (FAILED(rc)) return rc;
6640
6641 ChipsetType_T aChipset = ChipsetType_PIIX3;
6642 COMGETTER(ChipsetType)(&aChipset);
6643
6644 if (aChipset != ChipsetType_ICH9)
6645 {
6646 return setError(E_INVALIDARG,
6647 tr("Host PCI attachment only supported with ICH9 chipset"));
6648 }
6649
6650 // check if device with this host PCI address already attached
6651 for (HWData::PCIDeviceAssignmentList::iterator it = mHWData->mPCIDeviceAssignments.begin();
6652 it != mHWData->mPCIDeviceAssignments.end();
6653 ++it)
6654 {
6655 LONG iHostAddress = -1;
6656 ComPtr<PCIDeviceAttachment> pAttach;
6657 pAttach = *it;
6658 pAttach->COMGETTER(HostAddress)(&iHostAddress);
6659 if (iHostAddress == aHostAddress)
6660 return setError(E_INVALIDARG,
6661 tr("Device with host PCI address already attached to this VM"));
6662 }
6663
6664 ComObjPtr<PCIDeviceAttachment> pda;
6665 char name[32];
6666
6667 RTStrPrintf(name, sizeof(name), "host%02x:%02x.%x", (aHostAddress>>8) & 0xff,
6668 (aHostAddress & 0xf8) >> 3, aHostAddress & 7);
6669 Bstr bname(name);
6670 pda.createObject();
6671 pda->init(this, bname, aHostAddress, aDesiredGuestAddress, TRUE);
6672 i_setModified(IsModified_MachineData);
6673 mHWData.backup();
6674 mHWData->mPCIDeviceAssignments.push_back(pda);
6675 }
6676
6677 return S_OK;
6678}
6679
6680/**
6681 * Currently this method doesn't detach device from the running VM,
6682 * just makes sure it's not plugged on next VM start.
6683 */
6684HRESULT Machine::detachHostPCIDevice(LONG aHostAddress)
6685{
6686 ComObjPtr<PCIDeviceAttachment> pAttach;
6687 bool fRemoved = false;
6688 HRESULT rc;
6689
6690 // lock scope
6691 {
6692 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
6693
6694 rc = i_checkStateDependency(MutableStateDep);
6695 if (FAILED(rc)) return rc;
6696
6697 for (HWData::PCIDeviceAssignmentList::iterator it = mHWData->mPCIDeviceAssignments.begin();
6698 it != mHWData->mPCIDeviceAssignments.end();
6699 ++it)
6700 {
6701 LONG iHostAddress = -1;
6702 pAttach = *it;
6703 pAttach->COMGETTER(HostAddress)(&iHostAddress);
6704 if (iHostAddress != -1 && iHostAddress == aHostAddress)
6705 {
6706 i_setModified(IsModified_MachineData);
6707 mHWData.backup();
6708 mHWData->mPCIDeviceAssignments.remove(pAttach);
6709 fRemoved = true;
6710 break;
6711 }
6712 }
6713 }
6714
6715
6716 /* Fire event outside of the lock */
6717 if (fRemoved)
6718 {
6719 Assert(!pAttach.isNull());
6720 ComPtr<IEventSource> es;
6721 rc = mParent->COMGETTER(EventSource)(es.asOutParam());
6722 Assert(SUCCEEDED(rc));
6723 Bstr mid;
6724 rc = this->COMGETTER(Id)(mid.asOutParam());
6725 Assert(SUCCEEDED(rc));
6726 fireHostPCIDevicePlugEvent(es, mid.raw(), false /* unplugged */, true /* success */, pAttach, NULL);
6727 }
6728
6729 return fRemoved ? S_OK : setError(VBOX_E_OBJECT_NOT_FOUND,
6730 tr("No host PCI device %08x attached"),
6731 aHostAddress
6732 );
6733}
6734
6735HRESULT Machine::getPCIDeviceAssignments(std::vector<ComPtr<IPCIDeviceAttachment> > &aPCIDeviceAssignments)
6736{
6737 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
6738
6739 aPCIDeviceAssignments.resize(mHWData->mPCIDeviceAssignments.size());
6740
6741 size_t i = 0;
6742 for (std::list<ComObjPtr<PCIDeviceAttachment> >::const_iterator it = mHWData->mPCIDeviceAssignments.begin();
6743 it != mHWData->mPCIDeviceAssignments.end();
6744 ++i, ++it)
6745 (*it).queryInterfaceTo(aPCIDeviceAssignments[i].asOutParam());
6746
6747 return S_OK;
6748}
6749
6750HRESULT Machine::getBandwidthControl(ComPtr<IBandwidthControl> &aBandwidthControl)
6751{
6752 mBandwidthControl.queryInterfaceTo(aBandwidthControl.asOutParam());
6753
6754 return S_OK;
6755}
6756
6757HRESULT Machine::getTracingEnabled(BOOL *aTracingEnabled)
6758{
6759 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
6760
6761 *aTracingEnabled = mHWData->mDebugging.fTracingEnabled;
6762
6763 return S_OK;
6764}
6765
6766HRESULT Machine::setTracingEnabled(BOOL aTracingEnabled)
6767{
6768 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
6769 HRESULT hrc = i_checkStateDependency(MutableStateDep);
6770 if (SUCCEEDED(hrc))
6771 {
6772 hrc = mHWData.backupEx();
6773 if (SUCCEEDED(hrc))
6774 {
6775 i_setModified(IsModified_MachineData);
6776 mHWData->mDebugging.fTracingEnabled = aTracingEnabled != FALSE;
6777 }
6778 }
6779 return hrc;
6780}
6781
6782HRESULT Machine::getTracingConfig(com::Utf8Str &aTracingConfig)
6783{
6784 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
6785 aTracingConfig = mHWData->mDebugging.strTracingConfig;
6786 return S_OK;
6787}
6788
6789HRESULT Machine::setTracingConfig(const com::Utf8Str &aTracingConfig)
6790{
6791 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
6792 HRESULT hrc = i_checkStateDependency(MutableStateDep);
6793 if (SUCCEEDED(hrc))
6794 {
6795 hrc = mHWData.backupEx();
6796 if (SUCCEEDED(hrc))
6797 {
6798 mHWData->mDebugging.strTracingConfig = aTracingConfig;
6799 if (SUCCEEDED(hrc))
6800 i_setModified(IsModified_MachineData);
6801 }
6802 }
6803 return hrc;
6804}
6805
6806HRESULT Machine::getAllowTracingToAccessVM(BOOL *aAllowTracingToAccessVM)
6807{
6808 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
6809
6810 *aAllowTracingToAccessVM = mHWData->mDebugging.fAllowTracingToAccessVM;
6811
6812 return S_OK;
6813}
6814
6815HRESULT Machine::setAllowTracingToAccessVM(BOOL aAllowTracingToAccessVM)
6816{
6817 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
6818 HRESULT hrc = i_checkStateDependency(MutableStateDep);
6819 if (SUCCEEDED(hrc))
6820 {
6821 hrc = mHWData.backupEx();
6822 if (SUCCEEDED(hrc))
6823 {
6824 i_setModified(IsModified_MachineData);
6825 mHWData->mDebugging.fAllowTracingToAccessVM = aAllowTracingToAccessVM != FALSE;
6826 }
6827 }
6828 return hrc;
6829}
6830
6831HRESULT Machine::getAutostartEnabled(BOOL *aAutostartEnabled)
6832{
6833 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
6834
6835 *aAutostartEnabled = mHWData->mAutostart.fAutostartEnabled;
6836
6837 return S_OK;
6838}
6839
6840HRESULT Machine::setAutostartEnabled(BOOL aAutostartEnabled)
6841{
6842 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
6843
6844 HRESULT hrc = i_checkStateDependency(MutableStateDep);
6845 if ( SUCCEEDED(hrc)
6846 && mHWData->mAutostart.fAutostartEnabled != !!aAutostartEnabled)
6847 {
6848 AutostartDb *autostartDb = mParent->i_getAutostartDb();
6849 int vrc;
6850
6851 if (aAutostartEnabled)
6852 vrc = autostartDb->addAutostartVM(mUserData->s.strName.c_str());
6853 else
6854 vrc = autostartDb->removeAutostartVM(mUserData->s.strName.c_str());
6855
6856 if (RT_SUCCESS(vrc))
6857 {
6858 hrc = mHWData.backupEx();
6859 if (SUCCEEDED(hrc))
6860 {
6861 i_setModified(IsModified_MachineData);
6862 mHWData->mAutostart.fAutostartEnabled = aAutostartEnabled != FALSE;
6863 }
6864 }
6865 else if (vrc == VERR_NOT_SUPPORTED)
6866 hrc = setError(VBOX_E_NOT_SUPPORTED,
6867 tr("The VM autostart feature is not supported on this platform"));
6868 else if (vrc == VERR_PATH_NOT_FOUND)
6869 hrc = setError(E_FAIL,
6870 tr("The path to the autostart database is not set"));
6871 else
6872 hrc = setError(E_UNEXPECTED,
6873 tr("%s machine '%s' to the autostart database failed with %Rrc"),
6874 aAutostartEnabled ? "Adding" : "Removing",
6875 mUserData->s.strName.c_str(), vrc);
6876 }
6877 return hrc;
6878}
6879
6880HRESULT Machine::getAutostartDelay(ULONG *aAutostartDelay)
6881{
6882 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
6883
6884 *aAutostartDelay = mHWData->mAutostart.uAutostartDelay;
6885
6886 return S_OK;
6887}
6888
6889HRESULT Machine::setAutostartDelay(ULONG aAutostartDelay)
6890{
6891 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
6892 HRESULT hrc = i_checkStateDependency(MutableStateDep);
6893 if (SUCCEEDED(hrc))
6894 {
6895 hrc = mHWData.backupEx();
6896 if (SUCCEEDED(hrc))
6897 {
6898 i_setModified(IsModified_MachineData);
6899 mHWData->mAutostart.uAutostartDelay = aAutostartDelay;
6900 }
6901 }
6902 return hrc;
6903}
6904
6905HRESULT Machine::getAutostopType(AutostopType_T *aAutostopType)
6906{
6907 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
6908
6909 *aAutostopType = mHWData->mAutostart.enmAutostopType;
6910
6911 return S_OK;
6912}
6913
6914HRESULT Machine::setAutostopType(AutostopType_T aAutostopType)
6915{
6916 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
6917 HRESULT hrc = i_checkStateDependency(MutableStateDep);
6918 if ( SUCCEEDED(hrc)
6919 && mHWData->mAutostart.enmAutostopType != aAutostopType)
6920 {
6921 AutostartDb *autostartDb = mParent->i_getAutostartDb();
6922 int vrc;
6923
6924 if (aAutostopType != AutostopType_Disabled)
6925 vrc = autostartDb->addAutostopVM(mUserData->s.strName.c_str());
6926 else
6927 vrc = autostartDb->removeAutostopVM(mUserData->s.strName.c_str());
6928
6929 if (RT_SUCCESS(vrc))
6930 {
6931 hrc = mHWData.backupEx();
6932 if (SUCCEEDED(hrc))
6933 {
6934 i_setModified(IsModified_MachineData);
6935 mHWData->mAutostart.enmAutostopType = aAutostopType;
6936 }
6937 }
6938 else if (vrc == VERR_NOT_SUPPORTED)
6939 hrc = setError(VBOX_E_NOT_SUPPORTED,
6940 tr("The VM autostop feature is not supported on this platform"));
6941 else if (vrc == VERR_PATH_NOT_FOUND)
6942 hrc = setError(E_FAIL,
6943 tr("The path to the autostart database is not set"));
6944 else
6945 hrc = setError(E_UNEXPECTED,
6946 tr("%s machine '%s' to the autostop database failed with %Rrc"),
6947 aAutostopType != AutostopType_Disabled ? "Adding" : "Removing",
6948 mUserData->s.strName.c_str(), vrc);
6949 }
6950 return hrc;
6951}
6952
6953HRESULT Machine::getDefaultFrontend(com::Utf8Str &aDefaultFrontend)
6954{
6955 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
6956
6957 aDefaultFrontend = mHWData->mDefaultFrontend;
6958
6959 return S_OK;
6960}
6961
6962HRESULT Machine::setDefaultFrontend(const com::Utf8Str &aDefaultFrontend)
6963{
6964 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
6965 HRESULT hrc = i_checkStateDependency(MutableOrSavedStateDep);
6966 if (SUCCEEDED(hrc))
6967 {
6968 hrc = mHWData.backupEx();
6969 if (SUCCEEDED(hrc))
6970 {
6971 i_setModified(IsModified_MachineData);
6972 mHWData->mDefaultFrontend = aDefaultFrontend;
6973 }
6974 }
6975 return hrc;
6976}
6977
6978HRESULT Machine::getIcon(std::vector<BYTE> &aIcon)
6979{
6980 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
6981 com::SafeArray<BYTE> icon(mUserData->mIcon.size());
6982 aIcon.resize(mUserData->mIcon.size());
6983 memcpy(icon.raw(), &mUserData->mIcon[0], mUserData->mIcon.size());
6984 aIcon.resize(icon.size());
6985 for (size_t i = 0; i < icon.size(); ++i)
6986 aIcon[i] = icon[i];
6987 return S_OK;
6988}
6989
6990HRESULT Machine::setIcon(const std::vector<BYTE> &aIcon)
6991{
6992 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
6993 HRESULT hrc = i_checkStateDependency(MutableOrSavedStateDep);
6994 if (SUCCEEDED(hrc))
6995 {
6996 i_setModified(IsModified_MachineData);
6997 mUserData.backup();
6998 com::SafeArray<BYTE> icon(aIcon);
6999 mUserData->mIcon.resize(aIcon.size());
7000 memcpy(&mUserData->mIcon[0], icon.raw(), mUserData->mIcon.size());
7001 }
7002 return hrc;
7003}
7004
7005HRESULT Machine::getUSBProxyAvailable(BOOL *aUSBProxyAvailable)
7006{
7007
7008#ifdef VBOX_WITH_USB
7009 *aUSBProxyAvailable = true;
7010#else
7011 *aUSBProxyAvailable = false;
7012#endif
7013 return S_OK;
7014}
7015
7016HRESULT Machine::cloneTo(const ComPtr<IMachine> &aTarget, CloneMode_T aMode, const std::vector<CloneOptions_T> &aOptions,
7017 ComPtr<IProgress> &aProgress)
7018{
7019 ComObjPtr<Progress> pP;
7020 Progress *ppP = pP;
7021 IProgress *iP = static_cast<IProgress *>(ppP);
7022 IProgress **pProgress = &iP;
7023
7024 IMachine *pTarget = aTarget;
7025
7026 /* Convert the options. */
7027 RTCList<CloneOptions_T> optList;
7028 if (aOptions.size())
7029 for (size_t i = 0; i < aOptions.size(); ++i)
7030 optList.append(aOptions[i]);
7031
7032 if (optList.contains(CloneOptions_Link))
7033 {
7034 if (!i_isSnapshotMachine())
7035 return setError(E_INVALIDARG,
7036 tr("Linked clone can only be created from a snapshot"));
7037 if (aMode != CloneMode_MachineState)
7038 return setError(E_INVALIDARG,
7039 tr("Linked clone can only be created for a single machine state"));
7040 }
7041 AssertReturn(!(optList.contains(CloneOptions_KeepAllMACs) && optList.contains(CloneOptions_KeepNATMACs)), E_INVALIDARG);
7042
7043 MachineCloneVM *pWorker = new MachineCloneVM(this, static_cast<Machine*>(pTarget), aMode, optList);
7044
7045 HRESULT rc = pWorker->start(pProgress);
7046
7047 pP = static_cast<Progress *>(*pProgress);
7048 pP.queryInterfaceTo(aProgress.asOutParam());
7049
7050 return rc;
7051
7052}
7053
7054// public methods for internal purposes
7055/////////////////////////////////////////////////////////////////////////////
7056
7057/**
7058 * Adds the given IsModified_* flag to the dirty flags of the machine.
7059 * This must be called either during i_loadSettings or under the machine write lock.
7060 * @param fl
7061 */
7062void Machine::i_setModified(uint32_t fl, bool fAllowStateModification /* = true */)
7063{
7064 mData->flModifications |= fl;
7065 if (fAllowStateModification && i_isStateModificationAllowed())
7066 mData->mCurrentStateModified = true;
7067}
7068
7069/**
7070 * Adds the given IsModified_* flag to the dirty flags of the machine, taking
7071 * care of the write locking.
7072 *
7073 * @param fModifications The flag to add.
7074 */
7075void Machine::i_setModifiedLock(uint32_t fModification, bool fAllowStateModification /* = true */)
7076{
7077 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
7078 i_setModified(fModification, fAllowStateModification);
7079}
7080
7081/**
7082 * Saves the registry entry of this machine to the given configuration node.
7083 *
7084 * @param aEntryNode Node to save the registry entry to.
7085 *
7086 * @note locks this object for reading.
7087 */
7088HRESULT Machine::i_saveRegistryEntry(settings::MachineRegistryEntry &data)
7089{
7090 AutoLimitedCaller autoCaller(this);
7091 AssertComRCReturnRC(autoCaller.rc());
7092
7093 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
7094
7095 data.uuid = mData->mUuid;
7096 data.strSettingsFile = mData->m_strConfigFile;
7097
7098 return S_OK;
7099}
7100
7101/**
7102 * Calculates the absolute path of the given path taking the directory of the
7103 * machine settings file as the current directory.
7104 *
7105 * @param aPath Path to calculate the absolute path for.
7106 * @param aResult Where to put the result (used only on success, can be the
7107 * same Utf8Str instance as passed in @a aPath).
7108 * @return IPRT result.
7109 *
7110 * @note Locks this object for reading.
7111 */
7112int Machine::i_calculateFullPath(const Utf8Str &strPath, Utf8Str &aResult)
7113{
7114 AutoCaller autoCaller(this);
7115 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
7116
7117 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
7118
7119 AssertReturn(!mData->m_strConfigFileFull.isEmpty(), VERR_GENERAL_FAILURE);
7120
7121 Utf8Str strSettingsDir = mData->m_strConfigFileFull;
7122
7123 strSettingsDir.stripFilename();
7124 char folder[RTPATH_MAX];
7125 int vrc = RTPathAbsEx(strSettingsDir.c_str(), strPath.c_str(), folder, sizeof(folder));
7126 if (RT_SUCCESS(vrc))
7127 aResult = folder;
7128
7129 return vrc;
7130}
7131
7132/**
7133 * Copies strSource to strTarget, making it relative to the machine folder
7134 * if it is a subdirectory thereof, or simply copying it otherwise.
7135 *
7136 * @param strSource Path to evaluate and copy.
7137 * @param strTarget Buffer to receive target path.
7138 *
7139 * @note Locks this object for reading.
7140 */
7141void Machine::i_copyPathRelativeToMachine(const Utf8Str &strSource,
7142 Utf8Str &strTarget)
7143{
7144 AutoCaller autoCaller(this);
7145 AssertComRCReturn(autoCaller.rc(), (void)0);
7146
7147 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
7148
7149 AssertReturnVoid(!mData->m_strConfigFileFull.isEmpty());
7150 // use strTarget as a temporary buffer to hold the machine settings dir
7151 strTarget = mData->m_strConfigFileFull;
7152 strTarget.stripFilename();
7153 if (RTPathStartsWith(strSource.c_str(), strTarget.c_str()))
7154 {
7155 // is relative: then append what's left
7156 strTarget = strSource.substr(strTarget.length() + 1); // skip '/'
7157 // for empty paths (only possible for subdirs) use "." to avoid
7158 // triggering default settings for not present config attributes.
7159 if (strTarget.isEmpty())
7160 strTarget = ".";
7161 }
7162 else
7163 // is not relative: then overwrite
7164 strTarget = strSource;
7165}
7166
7167/**
7168 * Returns the full path to the machine's log folder in the
7169 * \a aLogFolder argument.
7170 */
7171void Machine::i_getLogFolder(Utf8Str &aLogFolder)
7172{
7173 AutoCaller autoCaller(this);
7174 AssertComRCReturnVoid(autoCaller.rc());
7175
7176 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
7177
7178 char szTmp[RTPATH_MAX];
7179 int vrc = RTEnvGetEx(RTENV_DEFAULT, "VBOX_USER_VMLOGDIR", szTmp, sizeof(szTmp), NULL);
7180 if (RT_SUCCESS(vrc))
7181 {
7182 if (szTmp[0] && !mUserData.isNull())
7183 {
7184 char szTmp2[RTPATH_MAX];
7185 vrc = RTPathAbs(szTmp, szTmp2, sizeof(szTmp2));
7186 if (RT_SUCCESS(vrc))
7187 aLogFolder = BstrFmt("%s%c%s",
7188 szTmp2,
7189 RTPATH_DELIMITER,
7190 mUserData->s.strName.c_str()); // path/to/logfolder/vmname
7191 }
7192 else
7193 vrc = VERR_PATH_IS_RELATIVE;
7194 }
7195
7196 if (RT_FAILURE(vrc))
7197 {
7198 // fallback if VBOX_USER_LOGHOME is not set or invalid
7199 aLogFolder = mData->m_strConfigFileFull; // path/to/machinesfolder/vmname/vmname.vbox
7200 aLogFolder.stripFilename(); // path/to/machinesfolder/vmname
7201 aLogFolder.append(RTPATH_DELIMITER);
7202 aLogFolder.append("Logs"); // path/to/machinesfolder/vmname/Logs
7203 }
7204}
7205
7206/**
7207 * Returns the full path to the machine's log file for an given index.
7208 */
7209Utf8Str Machine::i_queryLogFilename(ULONG idx) /** @todo r=bird: Misnamed. Should be i_getLogFilename as it cannot fail.
7210 See VBox-CodingGuidelines.cpp, Compulsory seciont, line 79. */
7211{
7212 Utf8Str logFolder;
7213 getLogFolder(logFolder);
7214 Assert(logFolder.length());
7215 Utf8Str log;
7216 if (idx == 0)
7217 log = Utf8StrFmt("%s%cVBox.log",
7218 logFolder.c_str(), RTPATH_DELIMITER);
7219 else
7220 log = Utf8StrFmt("%s%cVBox.log.%d",
7221 logFolder.c_str(), RTPATH_DELIMITER, idx);
7222 return log;
7223}
7224
7225/**
7226 * Returns the full path to the machine's (hardened) startup log file.
7227 */
7228Utf8Str Machine::i_getStartupLogFilename(void)
7229{
7230 Utf8Str strFilename;
7231 getLogFolder(strFilename);
7232 Assert(strFilename.length());
7233 strFilename.append(RTPATH_SLASH_STR "VBoxStartup.log");
7234 return strFilename;
7235}
7236
7237
7238/**
7239 * Composes a unique saved state filename based on the current system time. The filename is
7240 * granular to the second so this will work so long as no more than one snapshot is taken on
7241 * a machine per second.
7242 *
7243 * Before version 4.1, we used this formula for saved state files:
7244 * Utf8StrFmt("%s%c{%RTuuid}.sav", strFullSnapshotFolder.c_str(), RTPATH_DELIMITER, mData->mUuid.raw())
7245 * which no longer works because saved state files can now be shared between the saved state of the
7246 * "saved" machine and an online snapshot, and the following would cause problems:
7247 * 1) save machine
7248 * 2) create online snapshot from that machine state --> reusing saved state file
7249 * 3) save machine again --> filename would be reused, breaking the online snapshot
7250 *
7251 * So instead we now use a timestamp.
7252 *
7253 * @param str
7254 */
7255
7256void Machine::i_composeSavedStateFilename(Utf8Str &strStateFilePath)
7257{
7258 AutoCaller autoCaller(this);
7259 AssertComRCReturnVoid(autoCaller.rc());
7260
7261 {
7262 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
7263 i_calculateFullPath(mUserData->s.strSnapshotFolder, strStateFilePath);
7264 }
7265
7266 RTTIMESPEC ts;
7267 RTTimeNow(&ts);
7268 RTTIME time;
7269 RTTimeExplode(&time, &ts);
7270
7271 strStateFilePath += RTPATH_DELIMITER;
7272 strStateFilePath += Utf8StrFmt("%04d-%02u-%02uT%02u-%02u-%02u-%09uZ.sav",
7273 time.i32Year, time.u8Month, time.u8MonthDay,
7274 time.u8Hour, time.u8Minute, time.u8Second, time.u32Nanosecond);
7275}
7276
7277/**
7278 * Returns the full path to the default video capture file.
7279 */
7280void Machine::i_getDefaultVideoCaptureFile(Utf8Str &strFile)
7281{
7282 AutoCaller autoCaller(this);
7283 AssertComRCReturnVoid(autoCaller.rc());
7284
7285 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
7286
7287 strFile = mData->m_strConfigFileFull; // path/to/machinesfolder/vmname/vmname.vbox
7288 strFile.stripSuffix(); // path/to/machinesfolder/vmname/vmname
7289 strFile.append(".webm"); // path/to/machinesfolder/vmname/vmname.webm
7290}
7291
7292/**
7293 * Returns whether at least one USB controller is present for the VM.
7294 */
7295bool Machine::i_isUSBControllerPresent()
7296{
7297 AutoCaller autoCaller(this);
7298 AssertComRCReturn(autoCaller.rc(), false);
7299
7300 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
7301
7302 return (mUSBControllers->size() > 0);
7303}
7304
7305/**
7306 * @note Locks this object for writing, calls the client process
7307 * (inside the lock).
7308 */
7309HRESULT Machine::i_launchVMProcess(IInternalSessionControl *aControl,
7310 const Utf8Str &strFrontend,
7311 const Utf8Str &strEnvironment,
7312 ProgressProxy *aProgress)
7313{
7314 LogFlowThisFuncEnter();
7315
7316 AssertReturn(aControl, E_FAIL);
7317 AssertReturn(aProgress, E_FAIL);
7318 AssertReturn(!strFrontend.isEmpty(), E_FAIL);
7319
7320 AutoCaller autoCaller(this);
7321 if (FAILED(autoCaller.rc())) return autoCaller.rc();
7322
7323 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
7324
7325 if (!mData->mRegistered)
7326 return setError(E_UNEXPECTED,
7327 tr("The machine '%s' is not registered"),
7328 mUserData->s.strName.c_str());
7329
7330 LogFlowThisFunc(("mSession.mState=%s\n", Global::stringifySessionState(mData->mSession.mState)));
7331
7332 if ( mData->mSession.mState == SessionState_Locked
7333 || mData->mSession.mState == SessionState_Spawning
7334 || mData->mSession.mState == SessionState_Unlocking)
7335 return setError(VBOX_E_INVALID_OBJECT_STATE,
7336 tr("The machine '%s' is already locked by a session (or being locked or unlocked)"),
7337 mUserData->s.strName.c_str());
7338
7339 /* may not be busy */
7340 AssertReturn(!Global::IsOnlineOrTransient(mData->mMachineState), E_FAIL);
7341
7342 /* get the path to the executable */
7343 char szPath[RTPATH_MAX];
7344 RTPathAppPrivateArch(szPath, sizeof(szPath) - 1);
7345 size_t cchBufLeft = strlen(szPath);
7346 szPath[cchBufLeft++] = RTPATH_DELIMITER;
7347 szPath[cchBufLeft] = 0;
7348 char *pszNamePart = szPath + cchBufLeft;
7349 cchBufLeft = sizeof(szPath) - cchBufLeft;
7350
7351 int vrc = VINF_SUCCESS;
7352 RTPROCESS pid = NIL_RTPROCESS;
7353
7354 RTENV env = RTENV_DEFAULT;
7355
7356 if (!strEnvironment.isEmpty())
7357 {
7358 char *newEnvStr = NULL;
7359
7360 do
7361 {
7362 /* clone the current environment */
7363 int vrc2 = RTEnvClone(&env, RTENV_DEFAULT);
7364 AssertRCBreakStmt(vrc2, vrc = vrc2);
7365
7366 newEnvStr = RTStrDup(strEnvironment.c_str());
7367 AssertPtrBreakStmt(newEnvStr, vrc = vrc2);
7368
7369 /* put new variables to the environment
7370 * (ignore empty variable names here since RTEnv API
7371 * intentionally doesn't do that) */
7372 char *var = newEnvStr;
7373 for (char *p = newEnvStr; *p; ++p)
7374 {
7375 if (*p == '\n' && (p == newEnvStr || *(p - 1) != '\\'))
7376 {
7377 *p = '\0';
7378 if (*var)
7379 {
7380 char *val = strchr(var, '=');
7381 if (val)
7382 {
7383 *val++ = '\0';
7384 vrc2 = RTEnvSetEx(env, var, val);
7385 }
7386 else
7387 vrc2 = RTEnvUnsetEx(env, var);
7388 if (RT_FAILURE(vrc2))
7389 break;
7390 }
7391 var = p + 1;
7392 }
7393 }
7394 if (RT_SUCCESS(vrc2) && *var)
7395 vrc2 = RTEnvPutEx(env, var);
7396
7397 AssertRCBreakStmt(vrc2, vrc = vrc2);
7398 }
7399 while (0);
7400
7401 if (newEnvStr != NULL)
7402 RTStrFree(newEnvStr);
7403 }
7404
7405 /* Hardened startup logging */
7406#if defined(RT_OS_WINDOWS) && defined(VBOX_WITH_HARDENING)
7407 Utf8Str strSupStartLogArg("--sup-startup-log=");
7408 {
7409 Utf8Str strStartupLogFile = i_getStartupLogFilename();
7410 int vrc2 = RTFileDelete(strStartupLogFile.c_str());
7411 if (vrc2 == VERR_PATH_NOT_FOUND || vrc2 == VERR_FILE_NOT_FOUND)
7412 {
7413 Utf8Str strStartupLogDir = strStartupLogFile;
7414 strStartupLogDir.stripFilename();
7415 RTDirCreateFullPath(strStartupLogDir.c_str(), 0755); /** @todo add a variant for creating the path to a
7416 file without stripping the file. */
7417 }
7418 strSupStartLogArg.append(strStartupLogFile);
7419 }
7420 const char *pszSupStartupLogArg = strSupStartLogArg.c_str();
7421#else
7422 const char *pszSupStartupLogArg = NULL;
7423#endif
7424
7425
7426#ifdef VBOX_WITH_QTGUI
7427 if (strFrontend == "gui" || strFrontend == "GUI/Qt")
7428 {
7429# ifdef RT_OS_DARWIN /* Avoid Launch Services confusing this with the selector by using a helper app. */
7430 /* Modify the base path so that we don't need to use ".." below. */
7431 RTPathStripTrailingSlash(szPath);
7432 RTPathStripFilename(szPath);
7433 cchBufLeft = strlen(szPath);
7434 pszNamePart = szPath + cchBufLeft;
7435 cchBufLeft = sizeof(szPath) - cchBufLeft;
7436
7437# define OSX_APP_NAME "VirtualBoxVM"
7438# define OSX_APP_PATH_FMT "/Resources/%s.app/Contents/MacOS/VirtualBoxVM"
7439
7440 Utf8Str strAppOverride = i_getExtraData(Utf8Str("VBoxInternal2/VirtualBoxVMAppOverride"));
7441 if ( strAppOverride.contains(".")
7442 || strAppOverride.contains("/")
7443 || strAppOverride.contains("\\")
7444 || strAppOverride.contains(":"))
7445 strAppOverride.setNull();
7446 Utf8Str strAppPath;
7447 if (!strAppOverride.isEmpty())
7448 {
7449 strAppPath = Utf8StrFmt(OSX_APP_PATH_FMT, strAppOverride.c_str());
7450 Utf8Str strFullPath(szPath);
7451 strFullPath.append(strAppPath);
7452 /* there is a race, but people using this deserve the failure */
7453 if (!RTFileExists(strFullPath.c_str()))
7454 strAppOverride.setNull();
7455 }
7456 if (strAppOverride.isEmpty())
7457 strAppPath = Utf8StrFmt(OSX_APP_PATH_FMT, OSX_APP_NAME);
7458 AssertReturn(cchBufLeft > strAppPath.length(), E_UNEXPECTED);
7459 strcpy(pszNamePart, strAppPath.c_str());
7460# else
7461 static const char s_szVirtualBox_exe[] = "VirtualBox" HOSTSUFF_EXE;
7462 Assert(cchBufLeft >= sizeof(s_szVirtualBox_exe));
7463 strcpy(pszNamePart, s_szVirtualBox_exe);
7464# endif
7465
7466 Utf8Str idStr = mData->mUuid.toString();
7467 const char *apszArgs[] =
7468 {
7469 szPath,
7470 "--comment", mUserData->s.strName.c_str(),
7471 "--startvm", idStr.c_str(),
7472 "--no-startvm-errormsgbox",
7473 pszSupStartupLogArg,
7474 NULL
7475 };
7476 vrc = RTProcCreate(szPath, apszArgs, env, 0, &pid);
7477 }
7478#else /* !VBOX_WITH_QTGUI */
7479 if (0)
7480 ;
7481#endif /* VBOX_WITH_QTGUI */
7482
7483 else
7484
7485#ifdef VBOX_WITH_VBOXSDL
7486 if (strFrontend == "sdl" || strFrontend == "GUI/SDL")
7487 {
7488 static const char s_szVBoxSDL_exe[] = "VBoxSDL" HOSTSUFF_EXE;
7489 Assert(cchBufLeft >= sizeof(s_szVBoxSDL_exe));
7490 strcpy(pszNamePart, s_szVBoxSDL_exe);
7491
7492 Utf8Str idStr = mData->mUuid.toString();
7493 const char *apszArgs[] =
7494 {
7495 szPath,
7496 "--comment", mUserData->s.strName.c_str(),
7497 "--startvm", idStr.c_str(),
7498 pszSupStartupLogArg,
7499 NULL
7500 };
7501 vrc = RTProcCreate(szPath, apszArgs, env, 0, &pid);
7502 }
7503#else /* !VBOX_WITH_VBOXSDL */
7504 if (0)
7505 ;
7506#endif /* !VBOX_WITH_VBOXSDL */
7507
7508 else
7509
7510#ifdef VBOX_WITH_HEADLESS
7511 if ( strFrontend == "headless"
7512 || strFrontend == "capture"
7513 || strFrontend == "vrdp" /* Deprecated. Same as headless. */
7514 )
7515 {
7516 /* On pre-4.0 the "headless" type was used for passing "--vrdp off" to VBoxHeadless to let it work in OSE,
7517 * which did not contain VRDP server. In VBox 4.0 the remote desktop server (VRDE) is optional,
7518 * and a VM works even if the server has not been installed.
7519 * So in 4.0 the "headless" behavior remains the same for default VBox installations.
7520 * Only if a VRDE has been installed and the VM enables it, the "headless" will work
7521 * differently in 4.0 and 3.x.
7522 */
7523 static const char s_szVBoxHeadless_exe[] = "VBoxHeadless" HOSTSUFF_EXE;
7524 Assert(cchBufLeft >= sizeof(s_szVBoxHeadless_exe));
7525 strcpy(pszNamePart, s_szVBoxHeadless_exe);
7526
7527 Utf8Str idStr = mData->mUuid.toString();
7528 const char *apszArgs[] =
7529 {
7530 szPath,
7531 "--comment", mUserData->s.strName.c_str(),
7532 "--startvm", idStr.c_str(),
7533 "--vrde", "config",
7534 0, /* For "--capture". */
7535 0, /* For "--sup-startup-log". */
7536 0
7537 };
7538 unsigned iArg = 7;
7539 if (strFrontend == "capture")
7540 apszArgs[iArg++] = "--capture";
7541 apszArgs[iArg++] = pszSupStartupLogArg;
7542
7543# ifdef RT_OS_WINDOWS
7544 vrc = RTProcCreate(szPath, apszArgs, env, RTPROC_FLAGS_NO_WINDOW, &pid);
7545# else
7546 vrc = RTProcCreate(szPath, apszArgs, env, 0, &pid);
7547# endif
7548 }
7549#else /* !VBOX_WITH_HEADLESS */
7550 if (0)
7551 ;
7552#endif /* !VBOX_WITH_HEADLESS */
7553 else
7554 {
7555 RTEnvDestroy(env);
7556 return setError(E_INVALIDARG,
7557 tr("Invalid frontend name: '%s'"),
7558 strFrontend.c_str());
7559 }
7560
7561 RTEnvDestroy(env);
7562
7563 if (RT_FAILURE(vrc))
7564 return setError(VBOX_E_IPRT_ERROR,
7565 tr("Could not launch a process for the machine '%s' (%Rrc)"),
7566 mUserData->s.strName.c_str(), vrc);
7567
7568 LogFlowThisFunc(("launched.pid=%d(0x%x)\n", pid, pid));
7569
7570 /*
7571 * Note that we don't release the lock here before calling the client,
7572 * because it doesn't need to call us back if called with a NULL argument.
7573 * Releasing the lock here is dangerous because we didn't prepare the
7574 * launch data yet, but the client we've just started may happen to be
7575 * too fast and call LockMachine() that will fail (because of PID, etc.),
7576 * so that the Machine will never get out of the Spawning session state.
7577 */
7578
7579 /* inform the session that it will be a remote one */
7580 LogFlowThisFunc(("Calling AssignMachine (NULL)...\n"));
7581#ifndef VBOX_WITH_GENERIC_SESSION_WATCHER
7582 HRESULT rc = aControl->AssignMachine(NULL, LockType_Write, Bstr::Empty.raw());
7583#else /* VBOX_WITH_GENERIC_SESSION_WATCHER */
7584 HRESULT rc = aControl->AssignMachine(NULL, LockType_Write, NULL);
7585#endif /* VBOX_WITH_GENERIC_SESSION_WATCHER */
7586 LogFlowThisFunc(("AssignMachine (NULL) returned %08X\n", rc));
7587
7588 if (FAILED(rc))
7589 {
7590 /* restore the session state */
7591 mData->mSession.mState = SessionState_Unlocked;
7592 alock.release();
7593 mParent->i_addProcessToReap(pid);
7594 /* The failure may occur w/o any error info (from RPC), so provide one */
7595 return setError(VBOX_E_VM_ERROR,
7596 tr("Failed to assign the machine to the session (%Rhrc)"), rc);
7597 }
7598
7599 /* attach launch data to the machine */
7600 Assert(mData->mSession.mPID == NIL_RTPROCESS);
7601 mData->mSession.mRemoteControls.push_back(aControl);
7602 mData->mSession.mProgress = aProgress;
7603 mData->mSession.mPID = pid;
7604 mData->mSession.mState = SessionState_Spawning;
7605 mData->mSession.mType = strFrontend;
7606
7607 alock.release();
7608 mParent->i_addProcessToReap(pid);
7609
7610 LogFlowThisFuncLeave();
7611 return S_OK;
7612}
7613
7614/**
7615 * Returns @c true if the given session machine instance has an open direct
7616 * session (and optionally also for direct sessions which are closing) and
7617 * returns the session control machine instance if so.
7618 *
7619 * Note that when the method returns @c false, the arguments remain unchanged.
7620 *
7621 * @param aMachine Session machine object.
7622 * @param aControl Direct session control object (optional).
7623 * @param aAllowClosing If true then additionally a session which is currently
7624 * being closed will also be allowed.
7625 *
7626 * @note locks this object for reading.
7627 */
7628bool Machine::i_isSessionOpen(ComObjPtr<SessionMachine> &aMachine,
7629 ComPtr<IInternalSessionControl> *aControl /*= NULL*/,
7630 bool aAllowClosing /*= false*/)
7631{
7632 AutoLimitedCaller autoCaller(this);
7633 AssertComRCReturn(autoCaller.rc(), false);
7634
7635 /* just return false for inaccessible machines */
7636 if (getObjectState().getState() != ObjectState::Ready)
7637 return false;
7638
7639 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
7640
7641 if ( mData->mSession.mState == SessionState_Locked
7642 || (aAllowClosing && mData->mSession.mState == SessionState_Unlocking)
7643 )
7644 {
7645 AssertReturn(!mData->mSession.mMachine.isNull(), false);
7646
7647 aMachine = mData->mSession.mMachine;
7648
7649 if (aControl != NULL)
7650 *aControl = mData->mSession.mDirectControl;
7651
7652 return true;
7653 }
7654
7655 return false;
7656}
7657
7658/**
7659 * Returns @c true if the given machine has an spawning direct session.
7660 *
7661 * @note locks this object for reading.
7662 */
7663bool Machine::i_isSessionSpawning()
7664{
7665 AutoLimitedCaller autoCaller(this);
7666 AssertComRCReturn(autoCaller.rc(), false);
7667
7668 /* just return false for inaccessible machines */
7669 if (getObjectState().getState() != ObjectState::Ready)
7670 return false;
7671
7672 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
7673
7674 if (mData->mSession.mState == SessionState_Spawning)
7675 return true;
7676
7677 return false;
7678}
7679
7680/**
7681 * Called from the client watcher thread to check for unexpected client process
7682 * death during Session_Spawning state (e.g. before it successfully opened a
7683 * direct session).
7684 *
7685 * On Win32 and on OS/2, this method is called only when we've got the
7686 * direct client's process termination notification, so it always returns @c
7687 * true.
7688 *
7689 * On other platforms, this method returns @c true if the client process is
7690 * terminated and @c false if it's still alive.
7691 *
7692 * @note Locks this object for writing.
7693 */
7694bool Machine::i_checkForSpawnFailure()
7695{
7696 AutoCaller autoCaller(this);
7697 if (!autoCaller.isOk())
7698 {
7699 /* nothing to do */
7700 LogFlowThisFunc(("Already uninitialized!\n"));
7701 return true;
7702 }
7703
7704 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
7705
7706 if (mData->mSession.mState != SessionState_Spawning)
7707 {
7708 /* nothing to do */
7709 LogFlowThisFunc(("Not spawning any more!\n"));
7710 return true;
7711 }
7712
7713 HRESULT rc = S_OK;
7714
7715 /* PID not yet initialized, skip check. */
7716 if (mData->mSession.mPID == NIL_RTPROCESS)
7717 return false;
7718
7719 RTPROCSTATUS status;
7720 int vrc = RTProcWait(mData->mSession.mPID, RTPROCWAIT_FLAGS_NOBLOCK, &status);
7721
7722 if (vrc != VERR_PROCESS_RUNNING)
7723 {
7724 Utf8Str strExtraInfo;
7725
7726#if defined(RT_OS_WINDOWS) && defined(VBOX_WITH_HARDENING)
7727 /* If the startup logfile exists and is of non-zero length, tell the
7728 user to look there for more details to encourage them to attach it
7729 when reporting startup issues. */
7730 Utf8Str strStartupLogFile = i_getStartupLogFilename();
7731 uint64_t cbStartupLogFile = 0;
7732 int vrc2 = RTFileQuerySize(strStartupLogFile.c_str(), &cbStartupLogFile);
7733 if (RT_SUCCESS(vrc2) && cbStartupLogFile > 0)
7734 strExtraInfo.append(Utf8StrFmt(tr(". More details may be available in '%s'"), strStartupLogFile.c_str()));
7735#endif
7736
7737 if (RT_SUCCESS(vrc) && status.enmReason == RTPROCEXITREASON_NORMAL)
7738 rc = setError(E_FAIL,
7739 tr("The virtual machine '%s' has terminated unexpectedly during startup with exit code %d (%#x)%s"),
7740 i_getName().c_str(), status.iStatus, status.iStatus, strExtraInfo.c_str());
7741 else if (RT_SUCCESS(vrc) && status.enmReason == RTPROCEXITREASON_SIGNAL)
7742 rc = setError(E_FAIL,
7743 tr("The virtual machine '%s' has terminated unexpectedly during startup because of signal %d%s"),
7744 i_getName().c_str(), status.iStatus, strExtraInfo.c_str());
7745 else if (RT_SUCCESS(vrc) && status.enmReason == RTPROCEXITREASON_ABEND)
7746 rc = setError(E_FAIL,
7747 tr("The virtual machine '%s' has terminated abnormally (iStatus=%#x)%s"),
7748 i_getName().c_str(), status.iStatus, strExtraInfo.c_str());
7749 else
7750 rc = setError(E_FAIL,
7751 tr("The virtual machine '%s' has terminated unexpectedly during startup (%Rrc)%s"),
7752 i_getName().c_str(), vrc, strExtraInfo.c_str());
7753 }
7754
7755 if (FAILED(rc))
7756 {
7757 /* Close the remote session, remove the remote control from the list
7758 * and reset session state to Closed (@note keep the code in sync with
7759 * the relevant part in LockMachine()). */
7760
7761 Assert(mData->mSession.mRemoteControls.size() == 1);
7762 if (mData->mSession.mRemoteControls.size() == 1)
7763 {
7764 ErrorInfoKeeper eik;
7765 mData->mSession.mRemoteControls.front()->Uninitialize();
7766 }
7767
7768 mData->mSession.mRemoteControls.clear();
7769 mData->mSession.mState = SessionState_Unlocked;
7770
7771 /* finalize the progress after setting the state */
7772 if (!mData->mSession.mProgress.isNull())
7773 {
7774 mData->mSession.mProgress->notifyComplete(rc);
7775 mData->mSession.mProgress.setNull();
7776 }
7777
7778 mData->mSession.mPID = NIL_RTPROCESS;
7779
7780 mParent->i_onSessionStateChange(mData->mUuid, SessionState_Unlocked);
7781 return true;
7782 }
7783
7784 return false;
7785}
7786
7787/**
7788 * Checks whether the machine can be registered. If so, commits and saves
7789 * all settings.
7790 *
7791 * @note Must be called from mParent's write lock. Locks this object and
7792 * children for writing.
7793 */
7794HRESULT Machine::i_prepareRegister()
7795{
7796 AssertReturn(mParent->isWriteLockOnCurrentThread(), E_FAIL);
7797
7798 AutoLimitedCaller autoCaller(this);
7799 AssertComRCReturnRC(autoCaller.rc());
7800
7801 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
7802
7803 /* wait for state dependents to drop to zero */
7804 i_ensureNoStateDependencies();
7805
7806 if (!mData->mAccessible)
7807 return setError(VBOX_E_INVALID_OBJECT_STATE,
7808 tr("The machine '%s' with UUID {%s} is inaccessible and cannot be registered"),
7809 mUserData->s.strName.c_str(),
7810 mData->mUuid.toString().c_str());
7811
7812 AssertReturn(getObjectState().getState() == ObjectState::Ready, E_FAIL);
7813
7814 if (mData->mRegistered)
7815 return setError(VBOX_E_INVALID_OBJECT_STATE,
7816 tr("The machine '%s' with UUID {%s} is already registered"),
7817 mUserData->s.strName.c_str(),
7818 mData->mUuid.toString().c_str());
7819
7820 HRESULT rc = S_OK;
7821
7822 // Ensure the settings are saved. If we are going to be registered and
7823 // no config file exists yet, create it by calling i_saveSettings() too.
7824 if ( (mData->flModifications)
7825 || (!mData->pMachineConfigFile->fileExists())
7826 )
7827 {
7828 rc = i_saveSettings(NULL);
7829 // no need to check whether VirtualBox.xml needs saving too since
7830 // we can't have a machine XML file rename pending
7831 if (FAILED(rc)) return rc;
7832 }
7833
7834 /* more config checking goes here */
7835
7836 if (SUCCEEDED(rc))
7837 {
7838 /* we may have had implicit modifications we want to fix on success */
7839 i_commit();
7840
7841 mData->mRegistered = true;
7842 }
7843 else
7844 {
7845 /* we may have had implicit modifications we want to cancel on failure*/
7846 i_rollback(false /* aNotify */);
7847 }
7848
7849 return rc;
7850}
7851
7852/**
7853 * Increases the number of objects dependent on the machine state or on the
7854 * registered state. Guarantees that these two states will not change at least
7855 * until #releaseStateDependency() is called.
7856 *
7857 * Depending on the @a aDepType value, additional state checks may be made.
7858 * These checks will set extended error info on failure. See
7859 * #checkStateDependency() for more info.
7860 *
7861 * If this method returns a failure, the dependency is not added and the caller
7862 * is not allowed to rely on any particular machine state or registration state
7863 * value and may return the failed result code to the upper level.
7864 *
7865 * @param aDepType Dependency type to add.
7866 * @param aState Current machine state (NULL if not interested).
7867 * @param aRegistered Current registered state (NULL if not interested).
7868 *
7869 * @note Locks this object for writing.
7870 */
7871HRESULT Machine::i_addStateDependency(StateDependency aDepType /* = AnyStateDep */,
7872 MachineState_T *aState /* = NULL */,
7873 BOOL *aRegistered /* = NULL */)
7874{
7875 AutoCaller autoCaller(this);
7876 AssertComRCReturnRC(autoCaller.rc());
7877
7878 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
7879
7880 HRESULT rc = i_checkStateDependency(aDepType);
7881 if (FAILED(rc)) return rc;
7882
7883 {
7884 if (mData->mMachineStateChangePending != 0)
7885 {
7886 /* i_ensureNoStateDependencies() is waiting for state dependencies to
7887 * drop to zero so don't add more. It may make sense to wait a bit
7888 * and retry before reporting an error (since the pending state
7889 * transition should be really quick) but let's just assert for
7890 * now to see if it ever happens on practice. */
7891
7892 AssertFailed();
7893
7894 return setError(E_ACCESSDENIED,
7895 tr("Machine state change is in progress. Please retry the operation later."));
7896 }
7897
7898 ++mData->mMachineStateDeps;
7899 Assert(mData->mMachineStateDeps != 0 /* overflow */);
7900 }
7901
7902 if (aState)
7903 *aState = mData->mMachineState;
7904 if (aRegistered)
7905 *aRegistered = mData->mRegistered;
7906
7907 return S_OK;
7908}
7909
7910/**
7911 * Decreases the number of objects dependent on the machine state.
7912 * Must always complete the #addStateDependency() call after the state
7913 * dependency is no more necessary.
7914 */
7915void Machine::i_releaseStateDependency()
7916{
7917 AutoCaller autoCaller(this);
7918 AssertComRCReturnVoid(autoCaller.rc());
7919
7920 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
7921
7922 /* releaseStateDependency() w/o addStateDependency()? */
7923 AssertReturnVoid(mData->mMachineStateDeps != 0);
7924 -- mData->mMachineStateDeps;
7925
7926 if (mData->mMachineStateDeps == 0)
7927 {
7928 /* inform i_ensureNoStateDependencies() that there are no more deps */
7929 if (mData->mMachineStateChangePending != 0)
7930 {
7931 Assert(mData->mMachineStateDepsSem != NIL_RTSEMEVENTMULTI);
7932 RTSemEventMultiSignal (mData->mMachineStateDepsSem);
7933 }
7934 }
7935}
7936
7937Utf8Str Machine::i_getExtraData(const Utf8Str &strKey)
7938{
7939 /* start with nothing found */
7940 Utf8Str strResult("");
7941
7942 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
7943
7944 settings::StringsMap::const_iterator it = mData->pMachineConfigFile->mapExtraDataItems.find(strKey);
7945 if (it != mData->pMachineConfigFile->mapExtraDataItems.end())
7946 // found:
7947 strResult = it->second; // source is a Utf8Str
7948
7949 return strResult;
7950}
7951
7952// protected methods
7953/////////////////////////////////////////////////////////////////////////////
7954
7955/**
7956 * Performs machine state checks based on the @a aDepType value. If a check
7957 * fails, this method will set extended error info, otherwise it will return
7958 * S_OK. It is supposed, that on failure, the caller will immediately return
7959 * the return value of this method to the upper level.
7960 *
7961 * When @a aDepType is AnyStateDep, this method always returns S_OK.
7962 *
7963 * When @a aDepType is MutableStateDep, this method returns S_OK only if the
7964 * current state of this machine object allows to change settings of the
7965 * machine (i.e. the machine is not registered, or registered but not running
7966 * and not saved). It is useful to call this method from Machine setters
7967 * before performing any change.
7968 *
7969 * When @a aDepType is MutableOrSavedStateDep, this method behaves the same
7970 * as for MutableStateDep except that if the machine is saved, S_OK is also
7971 * returned. This is useful in setters which allow changing machine
7972 * properties when it is in the saved state.
7973 *
7974 * When @a aDepType is OfflineStateDep, this method returns S_OK if the
7975 * state is one of the 4 offline states (PoweredOff, Saved, Teleported,
7976 * Aborted).
7977 *
7978 * @param aDepType Dependency type to check.
7979 *
7980 * @note Non Machine based classes should use #addStateDependency() and
7981 * #releaseStateDependency() methods or the smart AutoStateDependency
7982 * template.
7983 *
7984 * @note This method must be called from under this object's read or write
7985 * lock.
7986 */
7987HRESULT Machine::i_checkStateDependency(StateDependency aDepType)
7988{
7989 switch (aDepType)
7990 {
7991 case AnyStateDep:
7992 {
7993 break;
7994 }
7995 case MutableStateDep:
7996 {
7997 if ( mData->mRegistered
7998 && ( !i_isSessionMachine() /** @todo This was just converted raw; Check if Running and
7999 Paused should actually be included here... (Live Migration) */
8000 || ( mData->mMachineState != MachineState_Paused
8001 && mData->mMachineState != MachineState_Running
8002 && mData->mMachineState != MachineState_Aborted
8003 && mData->mMachineState != MachineState_Teleported
8004 && mData->mMachineState != MachineState_PoweredOff
8005 )
8006 )
8007 )
8008 return setError(VBOX_E_INVALID_VM_STATE,
8009 tr("The machine is not mutable (state is %s)"),
8010 Global::stringifyMachineState(mData->mMachineState));
8011 break;
8012 }
8013 case MutableOrSavedStateDep:
8014 {
8015 if ( mData->mRegistered
8016 && ( !i_isSessionMachine() /** @todo This was just converted raw; Check if Running and
8017 Paused should actually be included here... (Live Migration) */
8018 || ( mData->mMachineState != MachineState_Paused
8019 && mData->mMachineState != MachineState_Running
8020 && mData->mMachineState != MachineState_Aborted
8021 && mData->mMachineState != MachineState_Teleported
8022 && mData->mMachineState != MachineState_Saved
8023 && mData->mMachineState != MachineState_PoweredOff
8024 )
8025 )
8026 )
8027 return setError(VBOX_E_INVALID_VM_STATE,
8028 tr("The machine is not mutable (state is %s)"),
8029 Global::stringifyMachineState(mData->mMachineState));
8030 break;
8031 }
8032 case OfflineStateDep:
8033 {
8034 if ( mData->mRegistered
8035 && ( !i_isSessionMachine()
8036 || ( mData->mMachineState != MachineState_PoweredOff
8037 && mData->mMachineState != MachineState_Saved
8038 && mData->mMachineState != MachineState_Aborted
8039 && mData->mMachineState != MachineState_Teleported
8040 )
8041 )
8042 )
8043 return setError(VBOX_E_INVALID_VM_STATE,
8044 tr("The machine is not offline (state is %s)"),
8045 Global::stringifyMachineState(mData->mMachineState));
8046 break;
8047 }
8048 }
8049
8050 return S_OK;
8051}
8052
8053/**
8054 * Helper to initialize all associated child objects and allocate data
8055 * structures.
8056 *
8057 * This method must be called as a part of the object's initialization procedure
8058 * (usually done in the #init() method).
8059 *
8060 * @note Must be called only from #init() or from #registeredInit().
8061 */
8062HRESULT Machine::initDataAndChildObjects()
8063{
8064 AutoCaller autoCaller(this);
8065 AssertComRCReturnRC(autoCaller.rc());
8066 AssertComRCReturn( getObjectState().getState() == ObjectState::InInit
8067 || getObjectState().getState() == ObjectState::Limited, E_FAIL);
8068
8069 AssertReturn(!mData->mAccessible, E_FAIL);
8070
8071 /* allocate data structures */
8072 mSSData.allocate();
8073 mUserData.allocate();
8074 mHWData.allocate();
8075 mMediaData.allocate();
8076 mStorageControllers.allocate();
8077 mUSBControllers.allocate();
8078
8079 /* initialize mOSTypeId */
8080 mUserData->s.strOsType = mParent->i_getUnknownOSType()->i_id();
8081
8082 /* create associated BIOS settings object */
8083 unconst(mBIOSSettings).createObject();
8084 mBIOSSettings->init(this);
8085
8086 /* create an associated VRDE object (default is disabled) */
8087 unconst(mVRDEServer).createObject();
8088 mVRDEServer->init(this);
8089
8090 /* create associated serial port objects */
8091 for (ULONG slot = 0; slot < RT_ELEMENTS(mSerialPorts); ++slot)
8092 {
8093 unconst(mSerialPorts[slot]).createObject();
8094 mSerialPorts[slot]->init(this, slot);
8095 }
8096
8097 /* create associated parallel port objects */
8098 for (ULONG slot = 0; slot < RT_ELEMENTS(mParallelPorts); ++slot)
8099 {
8100 unconst(mParallelPorts[slot]).createObject();
8101 mParallelPorts[slot]->init(this, slot);
8102 }
8103
8104 /* create the audio adapter object (always present, default is disabled) */
8105 unconst(mAudioAdapter).createObject();
8106 mAudioAdapter->init(this);
8107
8108 /* create the USB device filters object (always present) */
8109 unconst(mUSBDeviceFilters).createObject();
8110 mUSBDeviceFilters->init(this);
8111
8112 /* create associated network adapter objects */
8113 mNetworkAdapters.resize(Global::getMaxNetworkAdapters(mHWData->mChipsetType));
8114 for (ULONG slot = 0; slot < mNetworkAdapters.size(); ++slot)
8115 {
8116 unconst(mNetworkAdapters[slot]).createObject();
8117 mNetworkAdapters[slot]->init(this, slot);
8118 }
8119
8120 /* create the bandwidth control */
8121 unconst(mBandwidthControl).createObject();
8122 mBandwidthControl->init(this);
8123
8124 return S_OK;
8125}
8126
8127/**
8128 * Helper to uninitialize all associated child objects and to free all data
8129 * structures.
8130 *
8131 * This method must be called as a part of the object's uninitialization
8132 * procedure (usually done in the #uninit() method).
8133 *
8134 * @note Must be called only from #uninit() or from #registeredInit().
8135 */
8136void Machine::uninitDataAndChildObjects()
8137{
8138 AutoCaller autoCaller(this);
8139 AssertComRCReturnVoid(autoCaller.rc());
8140 AssertComRCReturnVoid( getObjectState().getState() == ObjectState::InUninit
8141 || getObjectState().getState() == ObjectState::Limited);
8142
8143 /* tell all our other child objects we've been uninitialized */
8144 if (mBandwidthControl)
8145 {
8146 mBandwidthControl->uninit();
8147 unconst(mBandwidthControl).setNull();
8148 }
8149
8150 for (ULONG slot = 0; slot < mNetworkAdapters.size(); ++slot)
8151 {
8152 if (mNetworkAdapters[slot])
8153 {
8154 mNetworkAdapters[slot]->uninit();
8155 unconst(mNetworkAdapters[slot]).setNull();
8156 }
8157 }
8158
8159 if (mUSBDeviceFilters)
8160 {
8161 mUSBDeviceFilters->uninit();
8162 unconst(mUSBDeviceFilters).setNull();
8163 }
8164
8165 if (mAudioAdapter)
8166 {
8167 mAudioAdapter->uninit();
8168 unconst(mAudioAdapter).setNull();
8169 }
8170
8171 for (ULONG slot = 0; slot < RT_ELEMENTS(mParallelPorts); ++slot)
8172 {
8173 if (mParallelPorts[slot])
8174 {
8175 mParallelPorts[slot]->uninit();
8176 unconst(mParallelPorts[slot]).setNull();
8177 }
8178 }
8179
8180 for (ULONG slot = 0; slot < RT_ELEMENTS(mSerialPorts); ++slot)
8181 {
8182 if (mSerialPorts[slot])
8183 {
8184 mSerialPorts[slot]->uninit();
8185 unconst(mSerialPorts[slot]).setNull();
8186 }
8187 }
8188
8189 if (mVRDEServer)
8190 {
8191 mVRDEServer->uninit();
8192 unconst(mVRDEServer).setNull();
8193 }
8194
8195 if (mBIOSSettings)
8196 {
8197 mBIOSSettings->uninit();
8198 unconst(mBIOSSettings).setNull();
8199 }
8200
8201 /* Deassociate media (only when a real Machine or a SnapshotMachine
8202 * instance is uninitialized; SessionMachine instances refer to real
8203 * Machine media). This is necessary for a clean re-initialization of
8204 * the VM after successfully re-checking the accessibility state. Note
8205 * that in case of normal Machine or SnapshotMachine uninitialization (as
8206 * a result of unregistering or deleting the snapshot), outdated media
8207 * attachments will already be uninitialized and deleted, so this
8208 * code will not affect them. */
8209 if ( !!mMediaData
8210 && (!i_isSessionMachine())
8211 )
8212 {
8213 for (MediaData::AttachmentList::const_iterator it = mMediaData->mAttachments.begin();
8214 it != mMediaData->mAttachments.end();
8215 ++it)
8216 {
8217 ComObjPtr<Medium> pMedium = (*it)->i_getMedium();
8218 if (pMedium.isNull())
8219 continue;
8220 HRESULT rc = pMedium->i_removeBackReference(mData->mUuid, i_getSnapshotId());
8221 AssertComRC(rc);
8222 }
8223 }
8224
8225 if (!i_isSessionMachine() && !i_isSnapshotMachine())
8226 {
8227 // clean up the snapshots list (Snapshot::uninit() will handle the snapshot's children recursively)
8228 if (mData->mFirstSnapshot)
8229 {
8230 // snapshots tree is protected by machine write lock; strictly
8231 // this isn't necessary here since we're deleting the entire
8232 // machine, but otherwise we assert in Snapshot::uninit()
8233 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
8234 mData->mFirstSnapshot->uninit();
8235 mData->mFirstSnapshot.setNull();
8236 }
8237
8238 mData->mCurrentSnapshot.setNull();
8239 }
8240
8241 /* free data structures (the essential mData structure is not freed here
8242 * since it may be still in use) */
8243 mMediaData.free();
8244 mStorageControllers.free();
8245 mUSBControllers.free();
8246 mHWData.free();
8247 mUserData.free();
8248 mSSData.free();
8249}
8250
8251/**
8252 * Returns a pointer to the Machine object for this machine that acts like a
8253 * parent for complex machine data objects such as shared folders, etc.
8254 *
8255 * For primary Machine objects and for SnapshotMachine objects, returns this
8256 * object's pointer itself. For SessionMachine objects, returns the peer
8257 * (primary) machine pointer.
8258 */
8259Machine* Machine::i_getMachine()
8260{
8261 if (i_isSessionMachine())
8262 return (Machine*)mPeer;
8263 return this;
8264}
8265
8266/**
8267 * Makes sure that there are no machine state dependents. If necessary, waits
8268 * for the number of dependents to drop to zero.
8269 *
8270 * Make sure this method is called from under this object's write lock to
8271 * guarantee that no new dependents may be added when this method returns
8272 * control to the caller.
8273 *
8274 * @note Locks this object for writing. The lock will be released while waiting
8275 * (if necessary).
8276 *
8277 * @warning To be used only in methods that change the machine state!
8278 */
8279void Machine::i_ensureNoStateDependencies()
8280{
8281 AssertReturnVoid(isWriteLockOnCurrentThread());
8282
8283 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
8284
8285 /* Wait for all state dependents if necessary */
8286 if (mData->mMachineStateDeps != 0)
8287 {
8288 /* lazy semaphore creation */
8289 if (mData->mMachineStateDepsSem == NIL_RTSEMEVENTMULTI)
8290 RTSemEventMultiCreate(&mData->mMachineStateDepsSem);
8291
8292 LogFlowThisFunc(("Waiting for state deps (%d) to drop to zero...\n",
8293 mData->mMachineStateDeps));
8294
8295 ++mData->mMachineStateChangePending;
8296
8297 /* reset the semaphore before waiting, the last dependent will signal
8298 * it */
8299 RTSemEventMultiReset(mData->mMachineStateDepsSem);
8300
8301 alock.release();
8302
8303 RTSemEventMultiWait(mData->mMachineStateDepsSem, RT_INDEFINITE_WAIT);
8304
8305 alock.acquire();
8306
8307 -- mData->mMachineStateChangePending;
8308 }
8309}
8310
8311/**
8312 * Changes the machine state and informs callbacks.
8313 *
8314 * This method is not intended to fail so it either returns S_OK or asserts (and
8315 * returns a failure).
8316 *
8317 * @note Locks this object for writing.
8318 */
8319HRESULT Machine::i_setMachineState(MachineState_T aMachineState)
8320{
8321 LogFlowThisFuncEnter();
8322 LogFlowThisFunc(("aMachineState=%s\n", Global::stringifyMachineState(aMachineState) ));
8323
8324 AutoCaller autoCaller(this);
8325 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
8326
8327 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
8328
8329 /* wait for state dependents to drop to zero */
8330 i_ensureNoStateDependencies();
8331
8332 if (mData->mMachineState != aMachineState)
8333 {
8334 mData->mMachineState = aMachineState;
8335
8336 RTTimeNow(&mData->mLastStateChange);
8337
8338 mParent->i_onMachineStateChange(mData->mUuid, aMachineState);
8339 }
8340
8341 LogFlowThisFuncLeave();
8342 return S_OK;
8343}
8344
8345/**
8346 * Searches for a shared folder with the given logical name
8347 * in the collection of shared folders.
8348 *
8349 * @param aName logical name of the shared folder
8350 * @param aSharedFolder where to return the found object
8351 * @param aSetError whether to set the error info if the folder is
8352 * not found
8353 * @return
8354 * S_OK when found or VBOX_E_OBJECT_NOT_FOUND when not found
8355 *
8356 * @note
8357 * must be called from under the object's lock!
8358 */
8359HRESULT Machine::i_findSharedFolder(const Utf8Str &aName,
8360 ComObjPtr<SharedFolder> &aSharedFolder,
8361 bool aSetError /* = false */)
8362{
8363 HRESULT rc = VBOX_E_OBJECT_NOT_FOUND;
8364 for (HWData::SharedFolderList::const_iterator it = mHWData->mSharedFolders.begin();
8365 it != mHWData->mSharedFolders.end();
8366 ++it)
8367 {
8368 SharedFolder *pSF = *it;
8369 AutoCaller autoCaller(pSF);
8370 if (pSF->i_getName() == aName)
8371 {
8372 aSharedFolder = pSF;
8373 rc = S_OK;
8374 break;
8375 }
8376 }
8377
8378 if (aSetError && FAILED(rc))
8379 setError(rc, tr("Could not find a shared folder named '%s'"), aName.c_str());
8380
8381 return rc;
8382}
8383
8384/**
8385 * Initializes all machine instance data from the given settings structures
8386 * from XML. The exception is the machine UUID which needs special handling
8387 * depending on the caller's use case, so the caller needs to set that herself.
8388 *
8389 * This gets called in several contexts during machine initialization:
8390 *
8391 * -- When machine XML exists on disk already and needs to be loaded into memory,
8392 * for example, from registeredInit() to load all registered machines on
8393 * VirtualBox startup. In this case, puuidRegistry is NULL because the media
8394 * attached to the machine should be part of some media registry already.
8395 *
8396 * -- During OVF import, when a machine config has been constructed from an
8397 * OVF file. In this case, puuidRegistry is set to the machine UUID to
8398 * ensure that the media listed as attachments in the config (which have
8399 * been imported from the OVF) receive the correct registry ID.
8400 *
8401 * -- During VM cloning.
8402 *
8403 * @param config Machine settings from XML.
8404 * @param puuidRegistry If != NULL, Medium::setRegistryIdIfFirst() gets called with this registry ID
8405 * for each attached medium in the config.
8406 * @return
8407 */
8408HRESULT Machine::i_loadMachineDataFromSettings(const settings::MachineConfigFile &config,
8409 const Guid *puuidRegistry)
8410{
8411 // copy name, description, OS type, teleporter, UTC etc.
8412 mUserData->s = config.machineUserData;
8413
8414 // Decode the Icon overide data from config userdata and set onto Machine.
8415 #define DECODE_STR_MAX _1M
8416 const char* pszStr = config.machineUserData.ovIcon.c_str();
8417 ssize_t cbOut = RTBase64DecodedSize(pszStr, NULL);
8418 if (cbOut > DECODE_STR_MAX)
8419 return setError(E_FAIL,
8420 tr("Icon Data too long.'%d' > '%d'"),
8421 cbOut,
8422 DECODE_STR_MAX);
8423 com::SafeArray<BYTE> iconByte(cbOut);
8424 HRESULT rc = RTBase64Decode(pszStr, iconByte.raw(), cbOut, NULL, NULL);
8425 if (FAILED(rc))
8426 return setError(E_FAIL,
8427 tr("Failure to Decode Icon Data. '%s' (%d)"),
8428 pszStr,
8429 rc);
8430 mUserData->mIcon.resize(iconByte.size());
8431 memcpy(&mUserData->mIcon[0], iconByte.raw(), mUserData->mIcon.size());
8432
8433 // look up the object by Id to check it is valid
8434 ComPtr<IGuestOSType> guestOSType;
8435 rc = mParent->GetGuestOSType(Bstr(mUserData->s.strOsType).raw(),
8436 guestOSType.asOutParam());
8437 if (FAILED(rc)) return rc;
8438
8439 // stateFile (optional)
8440 if (config.strStateFile.isEmpty())
8441 mSSData->strStateFilePath.setNull();
8442 else
8443 {
8444 Utf8Str stateFilePathFull(config.strStateFile);
8445 int vrc = i_calculateFullPath(stateFilePathFull, stateFilePathFull);
8446 if (RT_FAILURE(vrc))
8447 return setError(E_FAIL,
8448 tr("Invalid saved state file path '%s' (%Rrc)"),
8449 config.strStateFile.c_str(),
8450 vrc);
8451 mSSData->strStateFilePath = stateFilePathFull;
8452 }
8453
8454 // snapshot folder needs special processing so set it again
8455 rc = COMSETTER(SnapshotFolder)(Bstr(config.machineUserData.strSnapshotFolder).raw());
8456 if (FAILED(rc)) return rc;
8457
8458 /* Copy the extra data items (Not in any case config is already the same as
8459 * mData->pMachineConfigFile, like when the xml files are read from disk. So
8460 * make sure the extra data map is copied). */
8461 mData->pMachineConfigFile->mapExtraDataItems = config.mapExtraDataItems;
8462
8463 /* currentStateModified (optional, default is true) */
8464 mData->mCurrentStateModified = config.fCurrentStateModified;
8465
8466 mData->mLastStateChange = config.timeLastStateChange;
8467
8468 /*
8469 * note: all mUserData members must be assigned prior this point because
8470 * we need to commit changes in order to let mUserData be shared by all
8471 * snapshot machine instances.
8472 */
8473 mUserData.commitCopy();
8474
8475 // machine registry, if present (must be loaded before snapshots)
8476 if (config.canHaveOwnMediaRegistry())
8477 {
8478 // determine machine folder
8479 Utf8Str strMachineFolder = i_getSettingsFileFull();
8480 strMachineFolder.stripFilename();
8481 rc = mParent->initMedia(i_getId(), // media registry ID == machine UUID
8482 config.mediaRegistry,
8483 strMachineFolder);
8484 if (FAILED(rc)) return rc;
8485 }
8486
8487 /* Snapshot node (optional) */
8488 size_t cRootSnapshots;
8489 if ((cRootSnapshots = config.llFirstSnapshot.size()))
8490 {
8491 // there must be only one root snapshot
8492 Assert(cRootSnapshots == 1);
8493
8494 const settings::Snapshot &snap = config.llFirstSnapshot.front();
8495
8496 rc = i_loadSnapshot(snap,
8497 config.uuidCurrentSnapshot,
8498 NULL); // no parent == first snapshot
8499 if (FAILED(rc)) return rc;
8500 }
8501
8502 // hardware data
8503 rc = i_loadHardware(config.hardwareMachine, &config.debugging, &config.autostart);
8504 if (FAILED(rc)) return rc;
8505
8506 // load storage controllers
8507 rc = i_loadStorageControllers(config.storageMachine,
8508 puuidRegistry,
8509 NULL /* puuidSnapshot */);
8510 if (FAILED(rc)) return rc;
8511
8512 /*
8513 * NOTE: the assignment below must be the last thing to do,
8514 * otherwise it will be not possible to change the settings
8515 * somewhere in the code above because all setters will be
8516 * blocked by i_checkStateDependency(MutableStateDep).
8517 */
8518
8519 /* set the machine state to Aborted or Saved when appropriate */
8520 if (config.fAborted)
8521 {
8522 mSSData->strStateFilePath.setNull();
8523
8524 /* no need to use i_setMachineState() during init() */
8525 mData->mMachineState = MachineState_Aborted;
8526 }
8527 else if (!mSSData->strStateFilePath.isEmpty())
8528 {
8529 /* no need to use i_setMachineState() during init() */
8530 mData->mMachineState = MachineState_Saved;
8531 }
8532
8533 // after loading settings, we are no longer different from the XML on disk
8534 mData->flModifications = 0;
8535
8536 return S_OK;
8537}
8538
8539/**
8540 * Recursively loads all snapshots starting from the given.
8541 *
8542 * @param aNode <Snapshot> node.
8543 * @param aCurSnapshotId Current snapshot ID from the settings file.
8544 * @param aParentSnapshot Parent snapshot.
8545 */
8546HRESULT Machine::i_loadSnapshot(const settings::Snapshot &data,
8547 const Guid &aCurSnapshotId,
8548 Snapshot *aParentSnapshot)
8549{
8550 AssertReturn(!i_isSnapshotMachine(), E_FAIL);
8551 AssertReturn(!i_isSessionMachine(), E_FAIL);
8552
8553 HRESULT rc = S_OK;
8554
8555 Utf8Str strStateFile;
8556 if (!data.strStateFile.isEmpty())
8557 {
8558 /* optional */
8559 strStateFile = data.strStateFile;
8560 int vrc = i_calculateFullPath(strStateFile, strStateFile);
8561 if (RT_FAILURE(vrc))
8562 return setError(E_FAIL,
8563 tr("Invalid saved state file path '%s' (%Rrc)"),
8564 strStateFile.c_str(),
8565 vrc);
8566 }
8567
8568 /* create a snapshot machine object */
8569 ComObjPtr<SnapshotMachine> pSnapshotMachine;
8570 pSnapshotMachine.createObject();
8571 rc = pSnapshotMachine->initFromSettings(this,
8572 data.hardware,
8573 &data.debugging,
8574 &data.autostart,
8575 data.storage,
8576 data.uuid.ref(),
8577 strStateFile);
8578 if (FAILED(rc)) return rc;
8579
8580 /* create a snapshot object */
8581 ComObjPtr<Snapshot> pSnapshot;
8582 pSnapshot.createObject();
8583 /* initialize the snapshot */
8584 rc = pSnapshot->init(mParent, // VirtualBox object
8585 data.uuid,
8586 data.strName,
8587 data.strDescription,
8588 data.timestamp,
8589 pSnapshotMachine,
8590 aParentSnapshot);
8591 if (FAILED(rc)) return rc;
8592
8593 /* memorize the first snapshot if necessary */
8594 if (!mData->mFirstSnapshot)
8595 mData->mFirstSnapshot = pSnapshot;
8596
8597 /* memorize the current snapshot when appropriate */
8598 if ( !mData->mCurrentSnapshot
8599 && pSnapshot->i_getId() == aCurSnapshotId
8600 )
8601 mData->mCurrentSnapshot = pSnapshot;
8602
8603 // now create the children
8604 for (settings::SnapshotsList::const_iterator it = data.llChildSnapshots.begin();
8605 it != data.llChildSnapshots.end();
8606 ++it)
8607 {
8608 const settings::Snapshot &childData = *it;
8609 // recurse
8610 rc = i_loadSnapshot(childData,
8611 aCurSnapshotId,
8612 pSnapshot); // parent = the one we created above
8613 if (FAILED(rc)) return rc;
8614 }
8615
8616 return rc;
8617}
8618
8619/**
8620 * Loads settings into mHWData.
8621 *
8622 * @param data Reference to the hardware settings.
8623 * @param pDbg Pointer to the debugging settings.
8624 * @param pAutostart Pointer to the autostart settings.
8625 */
8626HRESULT Machine::i_loadHardware(const settings::Hardware &data, const settings::Debugging *pDbg,
8627 const settings::Autostart *pAutostart)
8628{
8629 AssertReturn(!i_isSessionMachine(), E_FAIL);
8630
8631 HRESULT rc = S_OK;
8632
8633 try
8634 {
8635 /* The hardware version attribute (optional). */
8636 mHWData->mHWVersion = data.strVersion;
8637 mHWData->mHardwareUUID = data.uuid;
8638
8639 mHWData->mHWVirtExEnabled = data.fHardwareVirt;
8640 mHWData->mHWVirtExNestedPagingEnabled = data.fNestedPaging;
8641 mHWData->mHWVirtExLargePagesEnabled = data.fLargePages;
8642 mHWData->mHWVirtExVPIDEnabled = data.fVPID;
8643 mHWData->mHWVirtExUXEnabled = data.fUnrestrictedExecution;
8644 mHWData->mHWVirtExForceEnabled = data.fHardwareVirtForce;
8645 mHWData->mPAEEnabled = data.fPAE;
8646 mHWData->mSyntheticCpu = data.fSyntheticCpu;
8647 mHWData->mLongMode = data.enmLongMode;
8648 mHWData->mTripleFaultReset = data.fTripleFaultReset;
8649 mHWData->mCPUCount = data.cCPUs;
8650 mHWData->mCPUHotPlugEnabled = data.fCpuHotPlug;
8651 mHWData->mCpuExecutionCap = data.ulCpuExecutionCap;
8652
8653 // cpu
8654 if (mHWData->mCPUHotPlugEnabled)
8655 {
8656 for (settings::CpuList::const_iterator it = data.llCpus.begin();
8657 it != data.llCpus.end();
8658 ++it)
8659 {
8660 const settings::Cpu &cpu = *it;
8661
8662 mHWData->mCPUAttached[cpu.ulId] = true;
8663 }
8664 }
8665
8666 // cpuid leafs
8667 for (settings::CpuIdLeafsList::const_iterator it = data.llCpuIdLeafs.begin();
8668 it != data.llCpuIdLeafs.end();
8669 ++it)
8670 {
8671 const settings::CpuIdLeaf &leaf = *it;
8672
8673 switch (leaf.ulId)
8674 {
8675 case 0x0:
8676 case 0x1:
8677 case 0x2:
8678 case 0x3:
8679 case 0x4:
8680 case 0x5:
8681 case 0x6:
8682 case 0x7:
8683 case 0x8:
8684 case 0x9:
8685 case 0xA:
8686 mHWData->mCpuIdStdLeafs[leaf.ulId] = leaf;
8687 break;
8688
8689 case 0x80000000:
8690 case 0x80000001:
8691 case 0x80000002:
8692 case 0x80000003:
8693 case 0x80000004:
8694 case 0x80000005:
8695 case 0x80000006:
8696 case 0x80000007:
8697 case 0x80000008:
8698 case 0x80000009:
8699 case 0x8000000A:
8700 mHWData->mCpuIdExtLeafs[leaf.ulId - 0x80000000] = leaf;
8701 break;
8702
8703 default:
8704 /* just ignore */
8705 break;
8706 }
8707 }
8708
8709 mHWData->mMemorySize = data.ulMemorySizeMB;
8710 mHWData->mPageFusionEnabled = data.fPageFusionEnabled;
8711
8712 // boot order
8713 for (unsigned i = 0; i < RT_ELEMENTS(mHWData->mBootOrder); ++i)
8714 {
8715 settings::BootOrderMap::const_iterator it = data.mapBootOrder.find(i);
8716 if (it == data.mapBootOrder.end())
8717 mHWData->mBootOrder[i] = DeviceType_Null;
8718 else
8719 mHWData->mBootOrder[i] = it->second;
8720 }
8721
8722 mHWData->mGraphicsControllerType = data.graphicsControllerType;
8723 mHWData->mVRAMSize = data.ulVRAMSizeMB;
8724 mHWData->mMonitorCount = data.cMonitors;
8725 mHWData->mAccelerate3DEnabled = data.fAccelerate3D;
8726 mHWData->mAccelerate2DVideoEnabled = data.fAccelerate2DVideo;
8727 mHWData->mVideoCaptureWidth = data.ulVideoCaptureHorzRes;
8728 mHWData->mVideoCaptureHeight = data.ulVideoCaptureVertRes;
8729 mHWData->mVideoCaptureEnabled = data.fVideoCaptureEnabled;
8730 for (unsigned i = 0; i < RT_ELEMENTS(mHWData->maVideoCaptureScreens); ++i)
8731 mHWData->maVideoCaptureScreens[i] = ASMBitTest(&data.u64VideoCaptureScreens, i);
8732 AssertCompile(RT_ELEMENTS(mHWData->maVideoCaptureScreens) == sizeof(data.u64VideoCaptureScreens) * 8);
8733 mHWData->mVideoCaptureRate = data.ulVideoCaptureRate;
8734 mHWData->mVideoCaptureFPS = data.ulVideoCaptureFPS;
8735 if (!data.strVideoCaptureFile.isEmpty())
8736 i_calculateFullPath(data.strVideoCaptureFile, mHWData->mVideoCaptureFile);
8737 else
8738 mHWData->mVideoCaptureFile.setNull();
8739 mHWData->mFirmwareType = data.firmwareType;
8740 mHWData->mPointingHIDType = data.pointingHIDType;
8741 mHWData->mKeyboardHIDType = data.keyboardHIDType;
8742 mHWData->mChipsetType = data.chipsetType;
8743 mHWData->mParavirtProvider = data.paravirtProvider;
8744 mHWData->mEmulatedUSBCardReaderEnabled = data.fEmulatedUSBCardReader;
8745 mHWData->mHPETEnabled = data.fHPETEnabled;
8746
8747 /* VRDEServer */
8748 rc = mVRDEServer->i_loadSettings(data.vrdeSettings);
8749 if (FAILED(rc)) return rc;
8750
8751 /* BIOS */
8752 rc = mBIOSSettings->i_loadSettings(data.biosSettings);
8753 if (FAILED(rc)) return rc;
8754
8755 // Bandwidth control (must come before network adapters)
8756 rc = mBandwidthControl->i_loadSettings(data.ioSettings);
8757 if (FAILED(rc)) return rc;
8758
8759 /* Shared folders */
8760 for (settings::USBControllerList::const_iterator it = data.usbSettings.llUSBControllers.begin();
8761 it != data.usbSettings.llUSBControllers.end();
8762 ++it)
8763 {
8764 const settings::USBController &settingsCtrl = *it;
8765 ComObjPtr<USBController> newCtrl;
8766
8767 newCtrl.createObject();
8768 newCtrl->init(this, settingsCtrl.strName, settingsCtrl.enmType);
8769 mUSBControllers->push_back(newCtrl);
8770 }
8771
8772 /* USB device filters */
8773 rc = mUSBDeviceFilters->i_loadSettings(data.usbSettings);
8774 if (FAILED(rc)) return rc;
8775
8776 // network adapters
8777 size_t newCount = Global::getMaxNetworkAdapters(mHWData->mChipsetType);
8778 size_t oldCount = mNetworkAdapters.size();
8779 if (newCount > oldCount)
8780 {
8781 mNetworkAdapters.resize(newCount);
8782 for (size_t slot = oldCount; slot < mNetworkAdapters.size(); ++slot)
8783 {
8784 unconst(mNetworkAdapters[slot]).createObject();
8785 mNetworkAdapters[slot]->init(this, (ULONG)slot);
8786 }
8787 }
8788 else if (newCount < oldCount)
8789 mNetworkAdapters.resize(newCount);
8790 for (settings::NetworkAdaptersList::const_iterator it = data.llNetworkAdapters.begin();
8791 it != data.llNetworkAdapters.end();
8792 ++it)
8793 {
8794 const settings::NetworkAdapter &nic = *it;
8795
8796 /* slot unicity is guaranteed by XML Schema */
8797 AssertBreak(nic.ulSlot < mNetworkAdapters.size());
8798 rc = mNetworkAdapters[nic.ulSlot]->i_loadSettings(mBandwidthControl, nic);
8799 if (FAILED(rc)) return rc;
8800 }
8801
8802 // serial ports
8803 for (settings::SerialPortsList::const_iterator it = data.llSerialPorts.begin();
8804 it != data.llSerialPorts.end();
8805 ++it)
8806 {
8807 const settings::SerialPort &s = *it;
8808
8809 AssertBreak(s.ulSlot < RT_ELEMENTS(mSerialPorts));
8810 rc = mSerialPorts[s.ulSlot]->i_loadSettings(s);
8811 if (FAILED(rc)) return rc;
8812 }
8813
8814 // parallel ports (optional)
8815 for (settings::ParallelPortsList::const_iterator it = data.llParallelPorts.begin();
8816 it != data.llParallelPorts.end();
8817 ++it)
8818 {
8819 const settings::ParallelPort &p = *it;
8820
8821 AssertBreak(p.ulSlot < RT_ELEMENTS(mParallelPorts));
8822 rc = mParallelPorts[p.ulSlot]->i_loadSettings(p);
8823 if (FAILED(rc)) return rc;
8824 }
8825
8826 /* AudioAdapter */
8827 rc = mAudioAdapter->i_loadSettings(data.audioAdapter);
8828 if (FAILED(rc)) return rc;
8829
8830 /* Shared folders */
8831 for (settings::SharedFoldersList::const_iterator it = data.llSharedFolders.begin();
8832 it != data.llSharedFolders.end();
8833 ++it)
8834 {
8835 const settings::SharedFolder &sf = *it;
8836
8837 ComObjPtr<SharedFolder> sharedFolder;
8838 /* Check for double entries. Not allowed! */
8839 rc = i_findSharedFolder(sf.strName, sharedFolder, false /* aSetError */);
8840 if (SUCCEEDED(rc))
8841 return setError(VBOX_E_OBJECT_IN_USE,
8842 tr("Shared folder named '%s' already exists"),
8843 sf.strName.c_str());
8844
8845 /* Create the new shared folder. Don't break on error. This will be
8846 * reported when the machine starts. */
8847 sharedFolder.createObject();
8848 rc = sharedFolder->init(i_getMachine(),
8849 sf.strName,
8850 sf.strHostPath,
8851 RT_BOOL(sf.fWritable),
8852 RT_BOOL(sf.fAutoMount),
8853 false /* fFailOnError */);
8854 if (FAILED(rc)) return rc;
8855 mHWData->mSharedFolders.push_back(sharedFolder);
8856 }
8857
8858 // Clipboard
8859 mHWData->mClipboardMode = data.clipboardMode;
8860
8861 // drag'n'drop
8862 mHWData->mDnDMode = data.dndMode;
8863
8864 // guest settings
8865 mHWData->mMemoryBalloonSize = data.ulMemoryBalloonSize;
8866
8867 // IO settings
8868 mHWData->mIOCacheEnabled = data.ioSettings.fIOCacheEnabled;
8869 mHWData->mIOCacheSize = data.ioSettings.ulIOCacheSize;
8870
8871 // Host PCI devices
8872 for (settings::HostPCIDeviceAttachmentList::const_iterator it = data.pciAttachments.begin();
8873 it != data.pciAttachments.end();
8874 ++it)
8875 {
8876 const settings::HostPCIDeviceAttachment &hpda = *it;
8877 ComObjPtr<PCIDeviceAttachment> pda;
8878
8879 pda.createObject();
8880 pda->i_loadSettings(this, hpda);
8881 mHWData->mPCIDeviceAssignments.push_back(pda);
8882 }
8883
8884 /*
8885 * (The following isn't really real hardware, but it lives in HWData
8886 * for reasons of convenience.)
8887 */
8888
8889#ifdef VBOX_WITH_GUEST_PROPS
8890 /* Guest properties (optional) */
8891 for (settings::GuestPropertiesList::const_iterator it = data.llGuestProperties.begin();
8892 it != data.llGuestProperties.end();
8893 ++it)
8894 {
8895 const settings::GuestProperty &prop = *it;
8896 uint32_t fFlags = guestProp::NILFLAG;
8897 guestProp::validateFlags(prop.strFlags.c_str(), &fFlags);
8898 HWData::GuestProperty property = { prop.strValue, (LONG64) prop.timestamp, fFlags };
8899 mHWData->mGuestProperties[prop.strName] = property;
8900 }
8901
8902 mHWData->mGuestPropertyNotificationPatterns = data.strNotificationPatterns;
8903#endif /* VBOX_WITH_GUEST_PROPS defined */
8904
8905 rc = i_loadDebugging(pDbg);
8906 if (FAILED(rc))
8907 return rc;
8908
8909 mHWData->mAutostart = *pAutostart;
8910
8911 /* default frontend */
8912 mHWData->mDefaultFrontend = data.strDefaultFrontend;
8913 }
8914 catch(std::bad_alloc &)
8915 {
8916 return E_OUTOFMEMORY;
8917 }
8918
8919 AssertComRC(rc);
8920 return rc;
8921}
8922
8923/**
8924 * Called from Machine::loadHardware() to load the debugging settings of the
8925 * machine.
8926 *
8927 * @param pDbg Pointer to the settings.
8928 */
8929HRESULT Machine::i_loadDebugging(const settings::Debugging *pDbg)
8930{
8931 mHWData->mDebugging = *pDbg;
8932 /* no more processing currently required, this will probably change. */
8933 return S_OK;
8934}
8935
8936/**
8937 * Called from i_loadMachineDataFromSettings() for the storage controller data, including media.
8938 *
8939 * @param data
8940 * @param puuidRegistry media registry ID to set media to or NULL; see Machine::i_loadMachineDataFromSettings()
8941 * @param puuidSnapshot
8942 * @return
8943 */
8944HRESULT Machine::i_loadStorageControllers(const settings::Storage &data,
8945 const Guid *puuidRegistry,
8946 const Guid *puuidSnapshot)
8947{
8948 AssertReturn(!i_isSessionMachine(), E_FAIL);
8949
8950 HRESULT rc = S_OK;
8951
8952 for (settings::StorageControllersList::const_iterator it = data.llStorageControllers.begin();
8953 it != data.llStorageControllers.end();
8954 ++it)
8955 {
8956 const settings::StorageController &ctlData = *it;
8957
8958 ComObjPtr<StorageController> pCtl;
8959 /* Try to find one with the name first. */
8960 rc = i_getStorageControllerByName(ctlData.strName, pCtl, false /* aSetError */);
8961 if (SUCCEEDED(rc))
8962 return setError(VBOX_E_OBJECT_IN_USE,
8963 tr("Storage controller named '%s' already exists"),
8964 ctlData.strName.c_str());
8965
8966 pCtl.createObject();
8967 rc = pCtl->init(this,
8968 ctlData.strName,
8969 ctlData.storageBus,
8970 ctlData.ulInstance,
8971 ctlData.fBootable);
8972 if (FAILED(rc)) return rc;
8973
8974 mStorageControllers->push_back(pCtl);
8975
8976 rc = pCtl->COMSETTER(ControllerType)(ctlData.controllerType);
8977 if (FAILED(rc)) return rc;
8978
8979 rc = pCtl->COMSETTER(PortCount)(ctlData.ulPortCount);
8980 if (FAILED(rc)) return rc;
8981
8982 rc = pCtl->COMSETTER(UseHostIOCache)(ctlData.fUseHostIOCache);
8983 if (FAILED(rc)) return rc;
8984
8985 /* Set IDE emulation settings (only for AHCI controller). */
8986 if (ctlData.controllerType == StorageControllerType_IntelAhci)
8987 {
8988 if ( (FAILED(rc = pCtl->i_setIDEEmulationPort(0, ctlData.lIDE0MasterEmulationPort)))
8989 || (FAILED(rc = pCtl->i_setIDEEmulationPort(1, ctlData.lIDE0SlaveEmulationPort)))
8990 || (FAILED(rc = pCtl->i_setIDEEmulationPort(2, ctlData.lIDE1MasterEmulationPort)))
8991 || (FAILED(rc = pCtl->i_setIDEEmulationPort(3, ctlData.lIDE1SlaveEmulationPort)))
8992 )
8993 return rc;
8994 }
8995
8996 /* Load the attached devices now. */
8997 rc = i_loadStorageDevices(pCtl,
8998 ctlData,
8999 puuidRegistry,
9000 puuidSnapshot);
9001 if (FAILED(rc)) return rc;
9002 }
9003
9004 return S_OK;
9005}
9006
9007/**
9008 * Called from i_loadStorageControllers for a controller's devices.
9009 *
9010 * @param aStorageController
9011 * @param data
9012 * @param puuidRegistry media registry ID to set media to or NULL; see Machine::i_loadMachineDataFromSettings()
9013 * @param aSnapshotId pointer to the snapshot ID if this is a snapshot machine
9014 * @return
9015 */
9016HRESULT Machine::i_loadStorageDevices(StorageController *aStorageController,
9017 const settings::StorageController &data,
9018 const Guid *puuidRegistry,
9019 const Guid *puuidSnapshot)
9020{
9021 HRESULT rc = S_OK;
9022
9023 /* paranoia: detect duplicate attachments */
9024 for (settings::AttachedDevicesList::const_iterator it = data.llAttachedDevices.begin();
9025 it != data.llAttachedDevices.end();
9026 ++it)
9027 {
9028 const settings::AttachedDevice &ad = *it;
9029
9030 for (settings::AttachedDevicesList::const_iterator it2 = it;
9031 it2 != data.llAttachedDevices.end();
9032 ++it2)
9033 {
9034 if (it == it2)
9035 continue;
9036
9037 const settings::AttachedDevice &ad2 = *it2;
9038
9039 if ( ad.lPort == ad2.lPort
9040 && ad.lDevice == ad2.lDevice)
9041 {
9042 return setError(E_FAIL,
9043 tr("Duplicate attachments for storage controller '%s', port %d, device %d of the virtual machine '%s'"),
9044 aStorageController->i_getName().c_str(),
9045 ad.lPort,
9046 ad.lDevice,
9047 mUserData->s.strName.c_str());
9048 }
9049 }
9050 }
9051
9052 for (settings::AttachedDevicesList::const_iterator it = data.llAttachedDevices.begin();
9053 it != data.llAttachedDevices.end();
9054 ++it)
9055 {
9056 const settings::AttachedDevice &dev = *it;
9057 ComObjPtr<Medium> medium;
9058
9059 switch (dev.deviceType)
9060 {
9061 case DeviceType_Floppy:
9062 case DeviceType_DVD:
9063 if (dev.strHostDriveSrc.isNotEmpty())
9064 rc = mParent->i_host()->i_findHostDriveByName(dev.deviceType, dev.strHostDriveSrc,
9065 false /* fRefresh */, medium);
9066 else
9067 rc = mParent->i_findRemoveableMedium(dev.deviceType,
9068 dev.uuid,
9069 false /* fRefresh */,
9070 false /* aSetError */,
9071 medium);
9072 if (rc == VBOX_E_OBJECT_NOT_FOUND)
9073 // This is not an error. The host drive or UUID might have vanished, so just go
9074 // ahead without this removeable medium attachment
9075 rc = S_OK;
9076 break;
9077
9078 case DeviceType_HardDisk:
9079 {
9080 /* find a hard disk by UUID */
9081 rc = mParent->i_findHardDiskById(dev.uuid, true /* aDoSetError */, &medium);
9082 if (FAILED(rc))
9083 {
9084 if (i_isSnapshotMachine())
9085 {
9086 // wrap another error message around the "cannot find hard disk" set by findHardDisk
9087 // so the user knows that the bad disk is in a snapshot somewhere
9088 com::ErrorInfo info;
9089 return setError(E_FAIL,
9090 tr("A differencing image of snapshot {%RTuuid} could not be found. %ls"),
9091 puuidSnapshot->raw(),
9092 info.getText().raw());
9093 }
9094 else
9095 return rc;
9096 }
9097
9098 AutoWriteLock hdLock(medium COMMA_LOCKVAL_SRC_POS);
9099
9100 if (medium->i_getType() == MediumType_Immutable)
9101 {
9102 if (i_isSnapshotMachine())
9103 return setError(E_FAIL,
9104 tr("Immutable hard disk '%s' with UUID {%RTuuid} cannot be directly attached to snapshot with UUID {%RTuuid} "
9105 "of the virtual machine '%s' ('%s')"),
9106 medium->i_getLocationFull().c_str(),
9107 dev.uuid.raw(),
9108 puuidSnapshot->raw(),
9109 mUserData->s.strName.c_str(),
9110 mData->m_strConfigFileFull.c_str());
9111
9112 return setError(E_FAIL,
9113 tr("Immutable hard disk '%s' with UUID {%RTuuid} cannot be directly attached to the virtual machine '%s' ('%s')"),
9114 medium->i_getLocationFull().c_str(),
9115 dev.uuid.raw(),
9116 mUserData->s.strName.c_str(),
9117 mData->m_strConfigFileFull.c_str());
9118 }
9119
9120 if (medium->i_getType() == MediumType_MultiAttach)
9121 {
9122 if (i_isSnapshotMachine())
9123 return setError(E_FAIL,
9124 tr("Multi-attach hard disk '%s' with UUID {%RTuuid} cannot be directly attached to snapshot with UUID {%RTuuid} "
9125 "of the virtual machine '%s' ('%s')"),
9126 medium->i_getLocationFull().c_str(),
9127 dev.uuid.raw(),
9128 puuidSnapshot->raw(),
9129 mUserData->s.strName.c_str(),
9130 mData->m_strConfigFileFull.c_str());
9131
9132 return setError(E_FAIL,
9133 tr("Multi-attach hard disk '%s' with UUID {%RTuuid} cannot be directly attached to the virtual machine '%s' ('%s')"),
9134 medium->i_getLocationFull().c_str(),
9135 dev.uuid.raw(),
9136 mUserData->s.strName.c_str(),
9137 mData->m_strConfigFileFull.c_str());
9138 }
9139
9140 if ( !i_isSnapshotMachine()
9141 && medium->i_getChildren().size() != 0
9142 )
9143 return setError(E_FAIL,
9144 tr("Hard disk '%s' with UUID {%RTuuid} cannot be directly attached to the virtual machine '%s' ('%s') "
9145 "because it has %d differencing child hard disks"),
9146 medium->i_getLocationFull().c_str(),
9147 dev.uuid.raw(),
9148 mUserData->s.strName.c_str(),
9149 mData->m_strConfigFileFull.c_str(),
9150 medium->i_getChildren().size());
9151
9152 if (i_findAttachment(mMediaData->mAttachments,
9153 medium))
9154 return setError(E_FAIL,
9155 tr("Hard disk '%s' with UUID {%RTuuid} is already attached to the virtual machine '%s' ('%s')"),
9156 medium->i_getLocationFull().c_str(),
9157 dev.uuid.raw(),
9158 mUserData->s.strName.c_str(),
9159 mData->m_strConfigFileFull.c_str());
9160
9161 break;
9162 }
9163
9164 default:
9165 return setError(E_FAIL,
9166 tr("Device '%s' with unknown type is attached to the virtual machine '%s' ('%s')"),
9167 medium->i_getLocationFull().c_str(),
9168 mUserData->s.strName.c_str(),
9169 mData->m_strConfigFileFull.c_str());
9170 }
9171
9172 if (FAILED(rc))
9173 break;
9174
9175 /* Bandwidth groups are loaded at this point. */
9176 ComObjPtr<BandwidthGroup> pBwGroup;
9177
9178 if (!dev.strBwGroup.isEmpty())
9179 {
9180 rc = mBandwidthControl->i_getBandwidthGroupByName(dev.strBwGroup, pBwGroup, false /* aSetError */);
9181 if (FAILED(rc))
9182 return setError(E_FAIL,
9183 tr("Device '%s' with unknown bandwidth group '%s' is attached to the virtual machine '%s' ('%s')"),
9184 medium->i_getLocationFull().c_str(),
9185 dev.strBwGroup.c_str(),
9186 mUserData->s.strName.c_str(),
9187 mData->m_strConfigFileFull.c_str());
9188 pBwGroup->i_reference();
9189 }
9190
9191 const Bstr controllerName = aStorageController->i_getName();
9192 ComObjPtr<MediumAttachment> pAttachment;
9193 pAttachment.createObject();
9194 rc = pAttachment->init(this,
9195 medium,
9196 controllerName,
9197 dev.lPort,
9198 dev.lDevice,
9199 dev.deviceType,
9200 false,
9201 dev.fPassThrough,
9202 dev.fTempEject,
9203 dev.fNonRotational,
9204 dev.fDiscard,
9205 dev.fHotPluggable,
9206 pBwGroup.isNull() ? Utf8Str::Empty : pBwGroup->i_getName());
9207 if (FAILED(rc)) break;
9208
9209 /* associate the medium with this machine and snapshot */
9210 if (!medium.isNull())
9211 {
9212 AutoCaller medCaller(medium);
9213 if (FAILED(medCaller.rc())) return medCaller.rc();
9214 AutoWriteLock mlock(medium COMMA_LOCKVAL_SRC_POS);
9215
9216 if (i_isSnapshotMachine())
9217 rc = medium->i_addBackReference(mData->mUuid, *puuidSnapshot);
9218 else
9219 rc = medium->i_addBackReference(mData->mUuid);
9220 /* If the medium->addBackReference fails it sets an appropriate
9221 * error message, so no need to do any guesswork here. */
9222
9223 if (puuidRegistry)
9224 // caller wants registry ID to be set on all attached media (OVF import case)
9225 medium->i_addRegistry(*puuidRegistry, false /* fRecurse */);
9226 }
9227
9228 if (FAILED(rc))
9229 break;
9230
9231 /* back up mMediaData to let registeredInit() properly rollback on failure
9232 * (= limited accessibility) */
9233 i_setModified(IsModified_Storage);
9234 mMediaData.backup();
9235 mMediaData->mAttachments.push_back(pAttachment);
9236 }
9237
9238 return rc;
9239}
9240
9241/**
9242 * Returns the snapshot with the given UUID or fails of no such snapshot exists.
9243 *
9244 * @param aId snapshot UUID to find (empty UUID refers the first snapshot)
9245 * @param aSnapshot where to return the found snapshot
9246 * @param aSetError true to set extended error info on failure
9247 */
9248HRESULT Machine::i_findSnapshotById(const Guid &aId,
9249 ComObjPtr<Snapshot> &aSnapshot,
9250 bool aSetError /* = false */)
9251{
9252 AutoReadLock chlock(this COMMA_LOCKVAL_SRC_POS);
9253
9254 if (!mData->mFirstSnapshot)
9255 {
9256 if (aSetError)
9257 return setError(E_FAIL, tr("This machine does not have any snapshots"));
9258 return E_FAIL;
9259 }
9260
9261 if (aId.isZero())
9262 aSnapshot = mData->mFirstSnapshot;
9263 else
9264 aSnapshot = mData->mFirstSnapshot->i_findChildOrSelf(aId.ref());
9265
9266 if (!aSnapshot)
9267 {
9268 if (aSetError)
9269 return setError(E_FAIL,
9270 tr("Could not find a snapshot with UUID {%s}"),
9271 aId.toString().c_str());
9272 return E_FAIL;
9273 }
9274
9275 return S_OK;
9276}
9277
9278/**
9279 * Returns the snapshot with the given name or fails of no such snapshot.
9280 *
9281 * @param aName snapshot name to find
9282 * @param aSnapshot where to return the found snapshot
9283 * @param aSetError true to set extended error info on failure
9284 */
9285HRESULT Machine::i_findSnapshotByName(const Utf8Str &strName,
9286 ComObjPtr<Snapshot> &aSnapshot,
9287 bool aSetError /* = false */)
9288{
9289 AssertReturn(!strName.isEmpty(), E_INVALIDARG);
9290
9291 AutoReadLock chlock(this COMMA_LOCKVAL_SRC_POS);
9292
9293 if (!mData->mFirstSnapshot)
9294 {
9295 if (aSetError)
9296 return setError(VBOX_E_OBJECT_NOT_FOUND,
9297 tr("This machine does not have any snapshots"));
9298 return VBOX_E_OBJECT_NOT_FOUND;
9299 }
9300
9301 aSnapshot = mData->mFirstSnapshot->i_findChildOrSelf(strName);
9302
9303 if (!aSnapshot)
9304 {
9305 if (aSetError)
9306 return setError(VBOX_E_OBJECT_NOT_FOUND,
9307 tr("Could not find a snapshot named '%s'"), strName.c_str());
9308 return VBOX_E_OBJECT_NOT_FOUND;
9309 }
9310
9311 return S_OK;
9312}
9313
9314/**
9315 * Returns a storage controller object with the given name.
9316 *
9317 * @param aName storage controller name to find
9318 * @param aStorageController where to return the found storage controller
9319 * @param aSetError true to set extended error info on failure
9320 */
9321HRESULT Machine::i_getStorageControllerByName(const Utf8Str &aName,
9322 ComObjPtr<StorageController> &aStorageController,
9323 bool aSetError /* = false */)
9324{
9325 AssertReturn(!aName.isEmpty(), E_INVALIDARG);
9326
9327 for (StorageControllerList::const_iterator it = mStorageControllers->begin();
9328 it != mStorageControllers->end();
9329 ++it)
9330 {
9331 if ((*it)->i_getName() == aName)
9332 {
9333 aStorageController = (*it);
9334 return S_OK;
9335 }
9336 }
9337
9338 if (aSetError)
9339 return setError(VBOX_E_OBJECT_NOT_FOUND,
9340 tr("Could not find a storage controller named '%s'"),
9341 aName.c_str());
9342 return VBOX_E_OBJECT_NOT_FOUND;
9343}
9344
9345/**
9346 * Returns a USB controller object with the given name.
9347 *
9348 * @param aName USB controller name to find
9349 * @param aUSBController where to return the found USB controller
9350 * @param aSetError true to set extended error info on failure
9351 */
9352HRESULT Machine::i_getUSBControllerByName(const Utf8Str &aName,
9353 ComObjPtr<USBController> &aUSBController,
9354 bool aSetError /* = false */)
9355{
9356 AssertReturn(!aName.isEmpty(), E_INVALIDARG);
9357
9358 for (USBControllerList::const_iterator it = mUSBControllers->begin();
9359 it != mUSBControllers->end();
9360 ++it)
9361 {
9362 if ((*it)->i_getName() == aName)
9363 {
9364 aUSBController = (*it);
9365 return S_OK;
9366 }
9367 }
9368
9369 if (aSetError)
9370 return setError(VBOX_E_OBJECT_NOT_FOUND,
9371 tr("Could not find a storage controller named '%s'"),
9372 aName.c_str());
9373 return VBOX_E_OBJECT_NOT_FOUND;
9374}
9375
9376/**
9377 * Returns the number of USB controller instance of the given type.
9378 *
9379 * @param enmType USB controller type.
9380 */
9381ULONG Machine::i_getUSBControllerCountByType(USBControllerType_T enmType)
9382{
9383 ULONG cCtrls = 0;
9384
9385 for (USBControllerList::const_iterator it = mUSBControllers->begin();
9386 it != mUSBControllers->end();
9387 ++it)
9388 {
9389 if ((*it)->i_getControllerType() == enmType)
9390 cCtrls++;
9391 }
9392
9393 return cCtrls;
9394}
9395
9396HRESULT Machine::i_getMediumAttachmentsOfController(const Utf8Str &aName,
9397 MediaData::AttachmentList &atts)
9398{
9399 AutoCaller autoCaller(this);
9400 if (FAILED(autoCaller.rc())) return autoCaller.rc();
9401
9402 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
9403
9404 for (MediaData::AttachmentList::iterator it = mMediaData->mAttachments.begin();
9405 it != mMediaData->mAttachments.end();
9406 ++it)
9407 {
9408 const ComObjPtr<MediumAttachment> &pAtt = *it;
9409 // should never happen, but deal with NULL pointers in the list.
9410 AssertStmt(!pAtt.isNull(), continue);
9411
9412 // getControllerName() needs caller+read lock
9413 AutoCaller autoAttCaller(pAtt);
9414 if (FAILED(autoAttCaller.rc()))
9415 {
9416 atts.clear();
9417 return autoAttCaller.rc();
9418 }
9419 AutoReadLock attLock(pAtt COMMA_LOCKVAL_SRC_POS);
9420
9421 if (pAtt->i_getControllerName() == Bstr(aName).raw())
9422 atts.push_back(pAtt);
9423 }
9424
9425 return S_OK;
9426}
9427
9428
9429/**
9430 * Helper for #i_saveSettings. Cares about renaming the settings directory and
9431 * file if the machine name was changed and about creating a new settings file
9432 * if this is a new machine.
9433 *
9434 * @note Must be never called directly but only from #saveSettings().
9435 */
9436HRESULT Machine::i_prepareSaveSettings(bool *pfNeedsGlobalSaveSettings)
9437{
9438 AssertReturn(isWriteLockOnCurrentThread(), E_FAIL);
9439
9440 HRESULT rc = S_OK;
9441
9442 bool fSettingsFileIsNew = !mData->pMachineConfigFile->fileExists();
9443
9444 /// @todo need to handle primary group change, too
9445
9446 /* attempt to rename the settings file if machine name is changed */
9447 if ( mUserData->s.fNameSync
9448 && mUserData.isBackedUp()
9449 && ( mUserData.backedUpData()->s.strName != mUserData->s.strName
9450 || mUserData.backedUpData()->s.llGroups.front() != mUserData->s.llGroups.front())
9451 )
9452 {
9453 bool dirRenamed = false;
9454 bool fileRenamed = false;
9455
9456 Utf8Str configFile, newConfigFile;
9457 Utf8Str configFilePrev, newConfigFilePrev;
9458 Utf8Str configDir, newConfigDir;
9459
9460 do
9461 {
9462 int vrc = VINF_SUCCESS;
9463
9464 Utf8Str name = mUserData.backedUpData()->s.strName;
9465 Utf8Str newName = mUserData->s.strName;
9466 Utf8Str group = mUserData.backedUpData()->s.llGroups.front();
9467 if (group == "/")
9468 group.setNull();
9469 Utf8Str newGroup = mUserData->s.llGroups.front();
9470 if (newGroup == "/")
9471 newGroup.setNull();
9472
9473 configFile = mData->m_strConfigFileFull;
9474
9475 /* first, rename the directory if it matches the group and machine name */
9476 Utf8Str groupPlusName = Utf8StrFmt("%s%c%s",
9477 group.c_str(), RTPATH_DELIMITER, name.c_str());
9478 /** @todo hack, make somehow use of ComposeMachineFilename */
9479 if (mUserData->s.fDirectoryIncludesUUID)
9480 groupPlusName += Utf8StrFmt(" (%RTuuid)", mData->mUuid.raw());
9481 Utf8Str newGroupPlusName = Utf8StrFmt("%s%c%s",
9482 newGroup.c_str(), RTPATH_DELIMITER, newName.c_str());
9483 /** @todo hack, make somehow use of ComposeMachineFilename */
9484 if (mUserData->s.fDirectoryIncludesUUID)
9485 newGroupPlusName += Utf8StrFmt(" (%RTuuid)", mData->mUuid.raw());
9486 configDir = configFile;
9487 configDir.stripFilename();
9488 newConfigDir = configDir;
9489 if ( configDir.length() >= groupPlusName.length()
9490 && !RTPathCompare(configDir.substr(configDir.length() - groupPlusName.length(), groupPlusName.length()).c_str(),
9491 groupPlusName.c_str()))
9492 {
9493 newConfigDir = newConfigDir.substr(0, configDir.length() - groupPlusName.length());
9494 Utf8Str newConfigBaseDir(newConfigDir);
9495 newConfigDir.append(newGroupPlusName);
9496 /* consistency: use \ if appropriate on the platform */
9497 RTPathChangeToDosSlashes(newConfigDir.mutableRaw(), false);
9498 /* new dir and old dir cannot be equal here because of 'if'
9499 * above and because name != newName */
9500 Assert(configDir != newConfigDir);
9501 if (!fSettingsFileIsNew)
9502 {
9503 /* perform real rename only if the machine is not new */
9504 vrc = RTPathRename(configDir.c_str(), newConfigDir.c_str(), 0);
9505 if ( vrc == VERR_FILE_NOT_FOUND
9506 || vrc == VERR_PATH_NOT_FOUND)
9507 {
9508 /* create the parent directory, then retry renaming */
9509 Utf8Str parent(newConfigDir);
9510 parent.stripFilename();
9511 (void)RTDirCreateFullPath(parent.c_str(), 0700);
9512 vrc = RTPathRename(configDir.c_str(), newConfigDir.c_str(), 0);
9513 }
9514 if (RT_FAILURE(vrc))
9515 {
9516 rc = setError(E_FAIL,
9517 tr("Could not rename the directory '%s' to '%s' to save the settings file (%Rrc)"),
9518 configDir.c_str(),
9519 newConfigDir.c_str(),
9520 vrc);
9521 break;
9522 }
9523 /* delete subdirectories which are no longer needed */
9524 Utf8Str dir(configDir);
9525 dir.stripFilename();
9526 while (dir != newConfigBaseDir && dir != ".")
9527 {
9528 vrc = RTDirRemove(dir.c_str());
9529 if (RT_FAILURE(vrc))
9530 break;
9531 dir.stripFilename();
9532 }
9533 dirRenamed = true;
9534 }
9535 }
9536
9537 newConfigFile = Utf8StrFmt("%s%c%s.vbox",
9538 newConfigDir.c_str(), RTPATH_DELIMITER, newName.c_str());
9539
9540 /* then try to rename the settings file itself */
9541 if (newConfigFile != configFile)
9542 {
9543 /* get the path to old settings file in renamed directory */
9544 configFile = Utf8StrFmt("%s%c%s",
9545 newConfigDir.c_str(),
9546 RTPATH_DELIMITER,
9547 RTPathFilename(configFile.c_str()));
9548 if (!fSettingsFileIsNew)
9549 {
9550 /* perform real rename only if the machine is not new */
9551 vrc = RTFileRename(configFile.c_str(), newConfigFile.c_str(), 0);
9552 if (RT_FAILURE(vrc))
9553 {
9554 rc = setError(E_FAIL,
9555 tr("Could not rename the settings file '%s' to '%s' (%Rrc)"),
9556 configFile.c_str(),
9557 newConfigFile.c_str(),
9558 vrc);
9559 break;
9560 }
9561 fileRenamed = true;
9562 configFilePrev = configFile;
9563 configFilePrev += "-prev";
9564 newConfigFilePrev = newConfigFile;
9565 newConfigFilePrev += "-prev";
9566 RTFileRename(configFilePrev.c_str(), newConfigFilePrev.c_str(), 0);
9567 }
9568 }
9569
9570 // update m_strConfigFileFull amd mConfigFile
9571 mData->m_strConfigFileFull = newConfigFile;
9572 // compute the relative path too
9573 mParent->i_copyPathRelativeToConfig(newConfigFile, mData->m_strConfigFile);
9574
9575 // store the old and new so that VirtualBox::i_saveSettings() can update
9576 // the media registry
9577 if ( mData->mRegistered
9578 && (configDir != newConfigDir || configFile != newConfigFile))
9579 {
9580 mParent->i_rememberMachineNameChangeForMedia(configDir, newConfigDir);
9581
9582 if (pfNeedsGlobalSaveSettings)
9583 *pfNeedsGlobalSaveSettings = true;
9584 }
9585
9586 // in the saved state file path, replace the old directory with the new directory
9587 if (RTPathStartsWith(mSSData->strStateFilePath.c_str(), configDir.c_str()))
9588 mSSData->strStateFilePath = newConfigDir.append(mSSData->strStateFilePath.c_str() + configDir.length());
9589
9590 // and do the same thing for the saved state file paths of all the online snapshots
9591 if (mData->mFirstSnapshot)
9592 mData->mFirstSnapshot->i_updateSavedStatePaths(configDir.c_str(),
9593 newConfigDir.c_str());
9594 }
9595 while (0);
9596
9597 if (FAILED(rc))
9598 {
9599 /* silently try to rename everything back */
9600 if (fileRenamed)
9601 {
9602 RTFileRename(newConfigFilePrev.c_str(), configFilePrev.c_str(), 0);
9603 RTFileRename(newConfigFile.c_str(), configFile.c_str(), 0);
9604 }
9605 if (dirRenamed)
9606 RTPathRename(newConfigDir.c_str(), configDir.c_str(), 0);
9607 }
9608
9609 if (FAILED(rc)) return rc;
9610 }
9611
9612 if (fSettingsFileIsNew)
9613 {
9614 /* create a virgin config file */
9615 int vrc = VINF_SUCCESS;
9616
9617 /* ensure the settings directory exists */
9618 Utf8Str path(mData->m_strConfigFileFull);
9619 path.stripFilename();
9620 if (!RTDirExists(path.c_str()))
9621 {
9622 vrc = RTDirCreateFullPath(path.c_str(), 0700);
9623 if (RT_FAILURE(vrc))
9624 {
9625 return setError(E_FAIL,
9626 tr("Could not create a directory '%s' to save the settings file (%Rrc)"),
9627 path.c_str(),
9628 vrc);
9629 }
9630 }
9631
9632 /* Note: open flags must correlate with RTFileOpen() in lockConfig() */
9633 path = Utf8Str(mData->m_strConfigFileFull);
9634 RTFILE f = NIL_RTFILE;
9635 vrc = RTFileOpen(&f, path.c_str(),
9636 RTFILE_O_READWRITE | RTFILE_O_CREATE | RTFILE_O_DENY_WRITE);
9637 if (RT_FAILURE(vrc))
9638 return setError(E_FAIL,
9639 tr("Could not create the settings file '%s' (%Rrc)"),
9640 path.c_str(),
9641 vrc);
9642 RTFileClose(f);
9643 }
9644
9645 return rc;
9646}
9647
9648/**
9649 * Saves and commits machine data, user data and hardware data.
9650 *
9651 * Note that on failure, the data remains uncommitted.
9652 *
9653 * @a aFlags may combine the following flags:
9654 *
9655 * - SaveS_ResetCurStateModified: Resets mData->mCurrentStateModified to FALSE.
9656 * Used when saving settings after an operation that makes them 100%
9657 * correspond to the settings from the current snapshot.
9658 * - SaveS_InformCallbacksAnyway: Callbacks will be informed even if
9659 * #isReallyModified() returns false. This is necessary for cases when we
9660 * change machine data directly, not through the backup()/commit() mechanism.
9661 * - SaveS_Force: settings will be saved without doing a deep compare of the
9662 * settings structures. This is used when this is called because snapshots
9663 * have changed to avoid the overhead of the deep compare.
9664 *
9665 * @note Must be called from under this object's write lock. Locks children for
9666 * writing.
9667 *
9668 * @param pfNeedsGlobalSaveSettings Optional pointer to a bool that must have been
9669 * initialized to false and that will be set to true by this function if
9670 * the caller must invoke VirtualBox::i_saveSettings() because the global
9671 * settings have changed. This will happen if a machine rename has been
9672 * saved and the global machine and media registries will therefore need
9673 * updating.
9674 */
9675HRESULT Machine::i_saveSettings(bool *pfNeedsGlobalSaveSettings,
9676 int aFlags /*= 0*/)
9677{
9678 LogFlowThisFuncEnter();
9679
9680 AssertReturn(isWriteLockOnCurrentThread(), E_FAIL);
9681
9682 /* make sure child objects are unable to modify the settings while we are
9683 * saving them */
9684 i_ensureNoStateDependencies();
9685
9686 AssertReturn(!i_isSnapshotMachine(),
9687 E_FAIL);
9688
9689 HRESULT rc = S_OK;
9690 bool fNeedsWrite = false;
9691
9692 /* First, prepare to save settings. It will care about renaming the
9693 * settings directory and file if the machine name was changed and about
9694 * creating a new settings file if this is a new machine. */
9695 rc = i_prepareSaveSettings(pfNeedsGlobalSaveSettings);
9696 if (FAILED(rc)) return rc;
9697
9698 // keep a pointer to the current settings structures
9699 settings::MachineConfigFile *pOldConfig = mData->pMachineConfigFile;
9700 settings::MachineConfigFile *pNewConfig = NULL;
9701
9702 try
9703 {
9704 // make a fresh one to have everyone write stuff into
9705 pNewConfig = new settings::MachineConfigFile(NULL);
9706 pNewConfig->copyBaseFrom(*mData->pMachineConfigFile);
9707
9708 // now go and copy all the settings data from COM to the settings structures
9709 // (this calles i_saveSettings() on all the COM objects in the machine)
9710 i_copyMachineDataToSettings(*pNewConfig);
9711
9712 if (aFlags & SaveS_ResetCurStateModified)
9713 {
9714 // this gets set by takeSnapshot() (if offline snapshot) and restoreSnapshot()
9715 mData->mCurrentStateModified = FALSE;
9716 fNeedsWrite = true; // always, no need to compare
9717 }
9718 else if (aFlags & SaveS_Force)
9719 {
9720 fNeedsWrite = true; // always, no need to compare
9721 }
9722 else
9723 {
9724 if (!mData->mCurrentStateModified)
9725 {
9726 // do a deep compare of the settings that we just saved with the settings
9727 // previously stored in the config file; this invokes MachineConfigFile::operator==
9728 // which does a deep compare of all the settings, which is expensive but less expensive
9729 // than writing out XML in vain
9730 bool fAnySettingsChanged = !(*pNewConfig == *pOldConfig);
9731
9732 // could still be modified if any settings changed
9733 mData->mCurrentStateModified = fAnySettingsChanged;
9734
9735 fNeedsWrite = fAnySettingsChanged;
9736 }
9737 else
9738 fNeedsWrite = true;
9739 }
9740
9741 pNewConfig->fCurrentStateModified = !!mData->mCurrentStateModified;
9742
9743 if (fNeedsWrite)
9744 // now spit it all out!
9745 pNewConfig->write(mData->m_strConfigFileFull);
9746
9747 mData->pMachineConfigFile = pNewConfig;
9748 delete pOldConfig;
9749 i_commit();
9750
9751 // after saving settings, we are no longer different from the XML on disk
9752 mData->flModifications = 0;
9753 }
9754 catch (HRESULT err)
9755 {
9756 // we assume that error info is set by the thrower
9757 rc = err;
9758
9759 // restore old config
9760 delete pNewConfig;
9761 mData->pMachineConfigFile = pOldConfig;
9762 }
9763 catch (...)
9764 {
9765 rc = VirtualBoxBase::handleUnexpectedExceptions(this, RT_SRC_POS);
9766 }
9767
9768 if (fNeedsWrite || (aFlags & SaveS_InformCallbacksAnyway))
9769 {
9770 /* Fire the data change event, even on failure (since we've already
9771 * committed all data). This is done only for SessionMachines because
9772 * mutable Machine instances are always not registered (i.e. private
9773 * to the client process that creates them) and thus don't need to
9774 * inform callbacks. */
9775 if (i_isSessionMachine())
9776 mParent->i_onMachineDataChange(mData->mUuid);
9777 }
9778
9779 LogFlowThisFunc(("rc=%08X\n", rc));
9780 LogFlowThisFuncLeave();
9781 return rc;
9782}
9783
9784/**
9785 * Implementation for saving the machine settings into the given
9786 * settings::MachineConfigFile instance. This copies machine extradata
9787 * from the previous machine config file in the instance data, if any.
9788 *
9789 * This gets called from two locations:
9790 *
9791 * -- Machine::i_saveSettings(), during the regular XML writing;
9792 *
9793 * -- Appliance::buildXMLForOneVirtualSystem(), when a machine gets
9794 * exported to OVF and we write the VirtualBox proprietary XML
9795 * into a <vbox:Machine> tag.
9796 *
9797 * This routine fills all the fields in there, including snapshots, *except*
9798 * for the following:
9799 *
9800 * -- fCurrentStateModified. There is some special logic associated with that.
9801 *
9802 * The caller can then call MachineConfigFile::write() or do something else
9803 * with it.
9804 *
9805 * Caller must hold the machine lock!
9806 *
9807 * This throws XML errors and HRESULT, so the caller must have a catch block!
9808 */
9809void Machine::i_copyMachineDataToSettings(settings::MachineConfigFile &config)
9810{
9811 // deep copy extradata
9812 config.mapExtraDataItems = mData->pMachineConfigFile->mapExtraDataItems;
9813
9814 config.uuid = mData->mUuid;
9815
9816 // copy name, description, OS type, teleport, UTC etc.
9817 config.machineUserData = mUserData->s;
9818
9819 // Encode the Icon Override data from Machine and store on config userdata.
9820 com::SafeArray<BYTE> iconByte;
9821 COMGETTER(Icon)(ComSafeArrayAsOutParam(iconByte));
9822 ssize_t cbData = iconByte.size();
9823 if (cbData > 0)
9824 {
9825 ssize_t cchOut = RTBase64EncodedLength(cbData);
9826 Utf8Str strIconData;
9827 strIconData.reserve(cchOut+1);
9828 int vrc = RTBase64Encode(iconByte.raw(), cbData,
9829 strIconData.mutableRaw(), strIconData.capacity(),
9830 NULL);
9831 if (RT_FAILURE(vrc))
9832 throw setError(E_FAIL, tr("Failure to Encode Icon Data. '%s' (%Rrc)"), strIconData.mutableRaw(), vrc);
9833 strIconData.jolt();
9834 config.machineUserData.ovIcon = strIconData;
9835 }
9836 else
9837 config.machineUserData.ovIcon.setNull();
9838
9839 if ( mData->mMachineState == MachineState_Saved
9840 || mData->mMachineState == MachineState_Restoring
9841 // when deleting a snapshot we may or may not have a saved state in the current state,
9842 // so let's not assert here please
9843 || ( ( mData->mMachineState == MachineState_DeletingSnapshot
9844 || mData->mMachineState == MachineState_DeletingSnapshotOnline
9845 || mData->mMachineState == MachineState_DeletingSnapshotPaused)
9846 && (!mSSData->strStateFilePath.isEmpty())
9847 )
9848 )
9849 {
9850 Assert(!mSSData->strStateFilePath.isEmpty());
9851 /* try to make the file name relative to the settings file dir */
9852 i_copyPathRelativeToMachine(mSSData->strStateFilePath, config.strStateFile);
9853 }
9854 else
9855 {
9856 Assert(mSSData->strStateFilePath.isEmpty() || mData->mMachineState == MachineState_Saving);
9857 config.strStateFile.setNull();
9858 }
9859
9860 if (mData->mCurrentSnapshot)
9861 config.uuidCurrentSnapshot = mData->mCurrentSnapshot->i_getId();
9862 else
9863 config.uuidCurrentSnapshot.clear();
9864
9865 config.timeLastStateChange = mData->mLastStateChange;
9866 config.fAborted = (mData->mMachineState == MachineState_Aborted);
9867 /// @todo Live Migration: config.fTeleported = (mData->mMachineState == MachineState_Teleported);
9868
9869 HRESULT rc = i_saveHardware(config.hardwareMachine, &config.debugging, &config.autostart);
9870 if (FAILED(rc)) throw rc;
9871
9872 rc = i_saveStorageControllers(config.storageMachine);
9873 if (FAILED(rc)) throw rc;
9874
9875 // save machine's media registry if this is VirtualBox 4.0 or later
9876 if (config.canHaveOwnMediaRegistry())
9877 {
9878 // determine machine folder
9879 Utf8Str strMachineFolder = i_getSettingsFileFull();
9880 strMachineFolder.stripFilename();
9881 mParent->i_saveMediaRegistry(config.mediaRegistry,
9882 i_getId(), // only media with registry ID == machine UUID
9883 strMachineFolder);
9884 // this throws HRESULT
9885 }
9886
9887 // save snapshots
9888 rc = i_saveAllSnapshots(config);
9889 if (FAILED(rc)) throw rc;
9890}
9891
9892/**
9893 * Saves all snapshots of the machine into the given machine config file. Called
9894 * from Machine::buildMachineXML() and SessionMachine::deleteSnapshotHandler().
9895 * @param config
9896 * @return
9897 */
9898HRESULT Machine::i_saveAllSnapshots(settings::MachineConfigFile &config)
9899{
9900 AssertReturn(isWriteLockOnCurrentThread(), E_FAIL);
9901
9902 HRESULT rc = S_OK;
9903
9904 try
9905 {
9906 config.llFirstSnapshot.clear();
9907
9908 if (mData->mFirstSnapshot)
9909 {
9910 settings::Snapshot snapNew;
9911 config.llFirstSnapshot.push_back(snapNew);
9912
9913 // get reference to the fresh copy of the snapshot on the list and
9914 // work on that copy directly to avoid excessive copying later
9915 settings::Snapshot &snap = config.llFirstSnapshot.front();
9916
9917 rc = mData->mFirstSnapshot->i_saveSnapshot(snap, false /*aAttrsOnly*/);
9918 if (FAILED(rc)) throw rc;
9919 }
9920
9921// if (mType == IsSessionMachine)
9922// mParent->onMachineDataChange(mData->mUuid); @todo is this necessary?
9923
9924 }
9925 catch (HRESULT err)
9926 {
9927 /* we assume that error info is set by the thrower */
9928 rc = err;
9929 }
9930 catch (...)
9931 {
9932 rc = VirtualBoxBase::handleUnexpectedExceptions(this, RT_SRC_POS);
9933 }
9934
9935 return rc;
9936}
9937
9938/**
9939 * Saves the VM hardware configuration. It is assumed that the
9940 * given node is empty.
9941 *
9942 * @param data Reference to the settings object for the hardware config.
9943 * @param pDbg Pointer to the settings object for the debugging config
9944 * which happens to live in mHWData.
9945 * @param pAutostart Pointer to the settings object for the autostart config
9946 * which happens to live in mHWData.
9947 */
9948HRESULT Machine::i_saveHardware(settings::Hardware &data, settings::Debugging *pDbg,
9949 settings::Autostart *pAutostart)
9950{
9951 HRESULT rc = S_OK;
9952
9953 try
9954 {
9955 /* The hardware version attribute (optional).
9956 Automatically upgrade from 1 to 2 when there is no saved state. (ugly!) */
9957 if ( mHWData->mHWVersion == "1"
9958 && mSSData->strStateFilePath.isEmpty()
9959 )
9960 mHWData->mHWVersion = "2"; /** @todo Is this safe, to update mHWVersion here? If not some
9961 other point needs to be found where this can be done. */
9962
9963 data.strVersion = mHWData->mHWVersion;
9964 data.uuid = mHWData->mHardwareUUID;
9965
9966 // CPU
9967 data.fHardwareVirt = !!mHWData->mHWVirtExEnabled;
9968 data.fNestedPaging = !!mHWData->mHWVirtExNestedPagingEnabled;
9969 data.fLargePages = !!mHWData->mHWVirtExLargePagesEnabled;
9970 data.fVPID = !!mHWData->mHWVirtExVPIDEnabled;
9971 data.fUnrestrictedExecution = !!mHWData->mHWVirtExUXEnabled;
9972 data.fHardwareVirtForce = !!mHWData->mHWVirtExForceEnabled;
9973 data.fPAE = !!mHWData->mPAEEnabled;
9974 data.enmLongMode = mHWData->mLongMode;
9975 data.fSyntheticCpu = !!mHWData->mSyntheticCpu;
9976 data.fTripleFaultReset = !!mHWData->mTripleFaultReset;
9977
9978 /* Standard and Extended CPUID leafs. */
9979 data.llCpuIdLeafs.clear();
9980 for (unsigned idx = 0; idx < RT_ELEMENTS(mHWData->mCpuIdStdLeafs); ++idx)
9981 {
9982 if (mHWData->mCpuIdStdLeafs[idx].ulId != UINT32_MAX)
9983 data.llCpuIdLeafs.push_back(mHWData->mCpuIdStdLeafs[idx]);
9984 }
9985 for (unsigned idx = 0; idx < RT_ELEMENTS(mHWData->mCpuIdExtLeafs); ++idx)
9986 {
9987 if (mHWData->mCpuIdExtLeafs[idx].ulId != UINT32_MAX)
9988 data.llCpuIdLeafs.push_back(mHWData->mCpuIdExtLeafs[idx]);
9989 }
9990
9991 data.cCPUs = mHWData->mCPUCount;
9992 data.fCpuHotPlug = !!mHWData->mCPUHotPlugEnabled;
9993 data.ulCpuExecutionCap = mHWData->mCpuExecutionCap;
9994
9995 data.llCpus.clear();
9996 if (data.fCpuHotPlug)
9997 {
9998 for (unsigned idx = 0; idx < data.cCPUs; ++idx)
9999 {
10000 if (mHWData->mCPUAttached[idx])
10001 {
10002 settings::Cpu cpu;
10003 cpu.ulId = idx;
10004 data.llCpus.push_back(cpu);
10005 }
10006 }
10007 }
10008
10009 // memory
10010 data.ulMemorySizeMB = mHWData->mMemorySize;
10011 data.fPageFusionEnabled = !!mHWData->mPageFusionEnabled;
10012
10013 // firmware
10014 data.firmwareType = mHWData->mFirmwareType;
10015
10016 // HID
10017 data.pointingHIDType = mHWData->mPointingHIDType;
10018 data.keyboardHIDType = mHWData->mKeyboardHIDType;
10019
10020 // chipset
10021 data.chipsetType = mHWData->mChipsetType;
10022
10023 // paravirt
10024 data.paravirtProvider = mHWData->mParavirtProvider;
10025
10026
10027 data.fEmulatedUSBCardReader = !!mHWData->mEmulatedUSBCardReaderEnabled;
10028
10029 // HPET
10030 data.fHPETEnabled = !!mHWData->mHPETEnabled;
10031
10032 // boot order
10033 data.mapBootOrder.clear();
10034 for (unsigned i = 0; i < RT_ELEMENTS(mHWData->mBootOrder); ++i)
10035 data.mapBootOrder[i] = mHWData->mBootOrder[i];
10036
10037 // display
10038 data.graphicsControllerType = mHWData->mGraphicsControllerType;
10039 data.ulVRAMSizeMB = mHWData->mVRAMSize;
10040 data.cMonitors = mHWData->mMonitorCount;
10041 data.fAccelerate3D = !!mHWData->mAccelerate3DEnabled;
10042 data.fAccelerate2DVideo = !!mHWData->mAccelerate2DVideoEnabled;
10043 data.ulVideoCaptureHorzRes = mHWData->mVideoCaptureWidth;
10044 data.ulVideoCaptureVertRes = mHWData->mVideoCaptureHeight;
10045 data.ulVideoCaptureRate = mHWData->mVideoCaptureRate;
10046 data.ulVideoCaptureFPS = mHWData->mVideoCaptureFPS;
10047 data.fVideoCaptureEnabled = !!mHWData->mVideoCaptureEnabled;
10048 for (unsigned i = 0; i < sizeof(data.u64VideoCaptureScreens) * 8; ++i)
10049 {
10050 if (mHWData->maVideoCaptureScreens[i])
10051 ASMBitSet(&data.u64VideoCaptureScreens, i);
10052 else
10053 ASMBitClear(&data.u64VideoCaptureScreens, i);
10054 }
10055 /* store relative video capture file if possible */
10056 i_copyPathRelativeToMachine(mHWData->mVideoCaptureFile, data.strVideoCaptureFile);
10057
10058 /* VRDEServer settings (optional) */
10059 rc = mVRDEServer->i_saveSettings(data.vrdeSettings);
10060 if (FAILED(rc)) throw rc;
10061
10062 /* BIOS (required) */
10063 rc = mBIOSSettings->i_saveSettings(data.biosSettings);
10064 if (FAILED(rc)) throw rc;
10065
10066 /* USB Controller (required) */
10067 for (USBControllerList::const_iterator it = mUSBControllers->begin(); it != mUSBControllers->end(); ++it)
10068 {
10069 ComObjPtr<USBController> ctrl = *it;
10070 settings::USBController settingsCtrl;
10071
10072 settingsCtrl.strName = ctrl->i_getName();
10073 settingsCtrl.enmType = ctrl->i_getControllerType();
10074
10075 data.usbSettings.llUSBControllers.push_back(settingsCtrl);
10076 }
10077
10078 /* USB device filters (required) */
10079 rc = mUSBDeviceFilters->i_saveSettings(data.usbSettings);
10080 if (FAILED(rc)) throw rc;
10081
10082 /* Network adapters (required) */
10083 size_t uMaxNICs = RT_MIN(Global::getMaxNetworkAdapters(mHWData->mChipsetType), mNetworkAdapters.size());
10084 data.llNetworkAdapters.clear();
10085 /* Write out only the nominal number of network adapters for this
10086 * chipset type. Since Machine::commit() hasn't been called there
10087 * may be extra NIC settings in the vector. */
10088 for (size_t slot = 0; slot < uMaxNICs; ++slot)
10089 {
10090 settings::NetworkAdapter nic;
10091 nic.ulSlot = (uint32_t)slot;
10092 /* paranoia check... must not be NULL, but must not crash either. */
10093 if (mNetworkAdapters[slot])
10094 {
10095 rc = mNetworkAdapters[slot]->i_saveSettings(nic);
10096 if (FAILED(rc)) throw rc;
10097
10098 data.llNetworkAdapters.push_back(nic);
10099 }
10100 }
10101
10102 /* Serial ports */
10103 data.llSerialPorts.clear();
10104 for (ULONG slot = 0; slot < RT_ELEMENTS(mSerialPorts); ++slot)
10105 {
10106 settings::SerialPort s;
10107 s.ulSlot = slot;
10108 rc = mSerialPorts[slot]->i_saveSettings(s);
10109 if (FAILED(rc)) return rc;
10110
10111 data.llSerialPorts.push_back(s);
10112 }
10113
10114 /* Parallel ports */
10115 data.llParallelPorts.clear();
10116 for (ULONG slot = 0; slot < RT_ELEMENTS(mParallelPorts); ++slot)
10117 {
10118 settings::ParallelPort p;
10119 p.ulSlot = slot;
10120 rc = mParallelPorts[slot]->i_saveSettings(p);
10121 if (FAILED(rc)) return rc;
10122
10123 data.llParallelPorts.push_back(p);
10124 }
10125
10126 /* Audio adapter */
10127 rc = mAudioAdapter->i_saveSettings(data.audioAdapter);
10128 if (FAILED(rc)) return rc;
10129
10130 /* Shared folders */
10131 data.llSharedFolders.clear();
10132 for (HWData::SharedFolderList::const_iterator it = mHWData->mSharedFolders.begin();
10133 it != mHWData->mSharedFolders.end();
10134 ++it)
10135 {
10136 SharedFolder *pSF = *it;
10137 AutoCaller sfCaller(pSF);
10138 AutoReadLock sfLock(pSF COMMA_LOCKVAL_SRC_POS);
10139 settings::SharedFolder sf;
10140 sf.strName = pSF->i_getName();
10141 sf.strHostPath = pSF->i_getHostPath();
10142 sf.fWritable = !!pSF->i_isWritable();
10143 sf.fAutoMount = !!pSF->i_isAutoMounted();
10144
10145 data.llSharedFolders.push_back(sf);
10146 }
10147
10148 // clipboard
10149 data.clipboardMode = mHWData->mClipboardMode;
10150
10151 // drag'n'drop
10152 data.dndMode = mHWData->mDnDMode;
10153
10154 /* Guest */
10155 data.ulMemoryBalloonSize = mHWData->mMemoryBalloonSize;
10156
10157 // IO settings
10158 data.ioSettings.fIOCacheEnabled = !!mHWData->mIOCacheEnabled;
10159 data.ioSettings.ulIOCacheSize = mHWData->mIOCacheSize;
10160
10161 /* BandwidthControl (required) */
10162 rc = mBandwidthControl->i_saveSettings(data.ioSettings);
10163 if (FAILED(rc)) throw rc;
10164
10165 /* Host PCI devices */
10166 for (HWData::PCIDeviceAssignmentList::const_iterator it = mHWData->mPCIDeviceAssignments.begin();
10167 it != mHWData->mPCIDeviceAssignments.end();
10168 ++it)
10169 {
10170 ComObjPtr<PCIDeviceAttachment> pda = *it;
10171 settings::HostPCIDeviceAttachment hpda;
10172
10173 rc = pda->i_saveSettings(hpda);
10174 if (FAILED(rc)) throw rc;
10175
10176 data.pciAttachments.push_back(hpda);
10177 }
10178
10179
10180 // guest properties
10181 data.llGuestProperties.clear();
10182#ifdef VBOX_WITH_GUEST_PROPS
10183 for (HWData::GuestPropertyMap::const_iterator it = mHWData->mGuestProperties.begin();
10184 it != mHWData->mGuestProperties.end();
10185 ++it)
10186 {
10187 HWData::GuestProperty property = it->second;
10188
10189 /* Remove transient guest properties at shutdown unless we
10190 * are saving state */
10191 if ( ( mData->mMachineState == MachineState_PoweredOff
10192 || mData->mMachineState == MachineState_Aborted
10193 || mData->mMachineState == MachineState_Teleported)
10194 && ( property.mFlags & guestProp::TRANSIENT
10195 || property.mFlags & guestProp::TRANSRESET))
10196 continue;
10197 settings::GuestProperty prop;
10198 prop.strName = it->first;
10199 prop.strValue = property.strValue;
10200 prop.timestamp = property.mTimestamp;
10201 char szFlags[guestProp::MAX_FLAGS_LEN + 1];
10202 guestProp::writeFlags(property.mFlags, szFlags);
10203 prop.strFlags = szFlags;
10204
10205 data.llGuestProperties.push_back(prop);
10206 }
10207
10208 data.strNotificationPatterns = mHWData->mGuestPropertyNotificationPatterns;
10209 /* I presume this doesn't require a backup(). */
10210 mData->mGuestPropertiesModified = FALSE;
10211#endif /* VBOX_WITH_GUEST_PROPS defined */
10212
10213 *pDbg = mHWData->mDebugging;
10214 *pAutostart = mHWData->mAutostart;
10215
10216 data.strDefaultFrontend = mHWData->mDefaultFrontend;
10217 }
10218 catch(std::bad_alloc &)
10219 {
10220 return E_OUTOFMEMORY;
10221 }
10222
10223 AssertComRC(rc);
10224 return rc;
10225}
10226
10227/**
10228 * Saves the storage controller configuration.
10229 *
10230 * @param aNode <StorageControllers> node to save the VM hardware configuration to.
10231 */
10232HRESULT Machine::i_saveStorageControllers(settings::Storage &data)
10233{
10234 data.llStorageControllers.clear();
10235
10236 for (StorageControllerList::const_iterator it = mStorageControllers->begin();
10237 it != mStorageControllers->end();
10238 ++it)
10239 {
10240 HRESULT rc;
10241 ComObjPtr<StorageController> pCtl = *it;
10242
10243 settings::StorageController ctl;
10244 ctl.strName = pCtl->i_getName();
10245 ctl.controllerType = pCtl->i_getControllerType();
10246 ctl.storageBus = pCtl->i_getStorageBus();
10247 ctl.ulInstance = pCtl->i_getInstance();
10248 ctl.fBootable = pCtl->i_getBootable();
10249
10250 /* Save the port count. */
10251 ULONG portCount;
10252 rc = pCtl->COMGETTER(PortCount)(&portCount);
10253 ComAssertComRCRet(rc, rc);
10254 ctl.ulPortCount = portCount;
10255
10256 /* Save fUseHostIOCache */
10257 BOOL fUseHostIOCache;
10258 rc = pCtl->COMGETTER(UseHostIOCache)(&fUseHostIOCache);
10259 ComAssertComRCRet(rc, rc);
10260 ctl.fUseHostIOCache = !!fUseHostIOCache;
10261
10262 /* Save IDE emulation settings. */
10263 if (ctl.controllerType == StorageControllerType_IntelAhci)
10264 {
10265 if ( (FAILED(rc = pCtl->i_getIDEEmulationPort(0, (LONG*)&ctl.lIDE0MasterEmulationPort)))
10266 || (FAILED(rc = pCtl->i_getIDEEmulationPort(1, (LONG*)&ctl.lIDE0SlaveEmulationPort)))
10267 || (FAILED(rc = pCtl->i_getIDEEmulationPort(2, (LONG*)&ctl.lIDE1MasterEmulationPort)))
10268 || (FAILED(rc = pCtl->i_getIDEEmulationPort(3, (LONG*)&ctl.lIDE1SlaveEmulationPort)))
10269 )
10270 ComAssertComRCRet(rc, rc);
10271 }
10272
10273 /* save the devices now. */
10274 rc = i_saveStorageDevices(pCtl, ctl);
10275 ComAssertComRCRet(rc, rc);
10276
10277 data.llStorageControllers.push_back(ctl);
10278 }
10279
10280 return S_OK;
10281}
10282
10283/**
10284 * Saves the hard disk configuration.
10285 */
10286HRESULT Machine::i_saveStorageDevices(ComObjPtr<StorageController> aStorageController,
10287 settings::StorageController &data)
10288{
10289 MediaData::AttachmentList atts;
10290
10291 HRESULT rc = i_getMediumAttachmentsOfController(aStorageController->i_getName(), atts);
10292 if (FAILED(rc)) return rc;
10293
10294 data.llAttachedDevices.clear();
10295 for (MediaData::AttachmentList::const_iterator it = atts.begin();
10296 it != atts.end();
10297 ++it)
10298 {
10299 settings::AttachedDevice dev;
10300 IMediumAttachment *iA = *it;
10301 MediumAttachment *pAttach = static_cast<MediumAttachment *>(iA);
10302 Medium *pMedium = pAttach->i_getMedium();
10303
10304 dev.deviceType = pAttach->i_getType();
10305 dev.lPort = pAttach->i_getPort();
10306 dev.lDevice = pAttach->i_getDevice();
10307 dev.fPassThrough = pAttach->i_getPassthrough();
10308 dev.fHotPluggable = pAttach->i_getHotPluggable();
10309 if (pMedium)
10310 {
10311 if (pMedium->i_isHostDrive())
10312 dev.strHostDriveSrc = pMedium->i_getLocationFull();
10313 else
10314 dev.uuid = pMedium->i_getId();
10315 dev.fTempEject = pAttach->i_getTempEject();
10316 dev.fNonRotational = pAttach->i_getNonRotational();
10317 dev.fDiscard = pAttach->i_getDiscard();
10318 }
10319
10320 dev.strBwGroup = pAttach->i_getBandwidthGroup();
10321
10322 data.llAttachedDevices.push_back(dev);
10323 }
10324
10325 return S_OK;
10326}
10327
10328/**
10329 * Saves machine state settings as defined by aFlags
10330 * (SaveSTS_* values).
10331 *
10332 * @param aFlags Combination of SaveSTS_* flags.
10333 *
10334 * @note Locks objects for writing.
10335 */
10336HRESULT Machine::i_saveStateSettings(int aFlags)
10337{
10338 if (aFlags == 0)
10339 return S_OK;
10340
10341 AutoCaller autoCaller(this);
10342 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
10343
10344 /* This object's write lock is also necessary to serialize file access
10345 * (prevent concurrent reads and writes) */
10346 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
10347
10348 HRESULT rc = S_OK;
10349
10350 Assert(mData->pMachineConfigFile);
10351
10352 try
10353 {
10354 if (aFlags & SaveSTS_CurStateModified)
10355 mData->pMachineConfigFile->fCurrentStateModified = true;
10356
10357 if (aFlags & SaveSTS_StateFilePath)
10358 {
10359 if (!mSSData->strStateFilePath.isEmpty())
10360 /* try to make the file name relative to the settings file dir */
10361 i_copyPathRelativeToMachine(mSSData->strStateFilePath, mData->pMachineConfigFile->strStateFile);
10362 else
10363 mData->pMachineConfigFile->strStateFile.setNull();
10364 }
10365
10366 if (aFlags & SaveSTS_StateTimeStamp)
10367 {
10368 Assert( mData->mMachineState != MachineState_Aborted
10369 || mSSData->strStateFilePath.isEmpty());
10370
10371 mData->pMachineConfigFile->timeLastStateChange = mData->mLastStateChange;
10372
10373 mData->pMachineConfigFile->fAborted = (mData->mMachineState == MachineState_Aborted);
10374//@todo live migration mData->pMachineConfigFile->fTeleported = (mData->mMachineState == MachineState_Teleported);
10375 }
10376
10377 mData->pMachineConfigFile->write(mData->m_strConfigFileFull);
10378 }
10379 catch (...)
10380 {
10381 rc = VirtualBoxBase::handleUnexpectedExceptions(this, RT_SRC_POS);
10382 }
10383
10384 return rc;
10385}
10386
10387/**
10388 * Ensures that the given medium is added to a media registry. If this machine
10389 * was created with 4.0 or later, then the machine registry is used. Otherwise
10390 * the global VirtualBox media registry is used.
10391 *
10392 * Caller must NOT hold machine lock, media tree or any medium locks!
10393 *
10394 * @param pMedium
10395 */
10396void Machine::i_addMediumToRegistry(ComObjPtr<Medium> &pMedium)
10397{
10398 /* Paranoia checks: do not hold machine or media tree locks. */
10399 AssertReturnVoid(!isWriteLockOnCurrentThread());
10400 AssertReturnVoid(!mParent->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
10401
10402 ComObjPtr<Medium> pBase;
10403 {
10404 AutoReadLock treeLock(&mParent->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
10405 pBase = pMedium->i_getBase();
10406 }
10407
10408 /* Paranoia checks: do not hold medium locks. */
10409 AssertReturnVoid(!pMedium->isWriteLockOnCurrentThread());
10410 AssertReturnVoid(!pBase->isWriteLockOnCurrentThread());
10411
10412 // decide which medium registry to use now that the medium is attached:
10413 Guid uuid;
10414 if (mData->pMachineConfigFile->canHaveOwnMediaRegistry())
10415 // machine XML is VirtualBox 4.0 or higher:
10416 uuid = i_getId(); // machine UUID
10417 else
10418 uuid = mParent->i_getGlobalRegistryId(); // VirtualBox global registry UUID
10419
10420 if (pMedium->i_addRegistry(uuid, false /* fRecurse */))
10421 mParent->i_markRegistryModified(uuid);
10422
10423 /* For more complex hard disk structures it can happen that the base
10424 * medium isn't yet associated with any medium registry. Do that now. */
10425 if (pMedium != pBase)
10426 {
10427 if (pBase->i_addRegistry(uuid, true /* fRecurse */))
10428 mParent->i_markRegistryModified(uuid);
10429 }
10430}
10431
10432/**
10433 * Creates differencing hard disks for all normal hard disks attached to this
10434 * machine and a new set of attachments to refer to created disks.
10435 *
10436 * Used when taking a snapshot or when deleting the current state. Gets called
10437 * from SessionMachine::BeginTakingSnapshot() and SessionMachine::restoreSnapshotHandler().
10438 *
10439 * This method assumes that mMediaData contains the original hard disk attachments
10440 * it needs to create diffs for. On success, these attachments will be replaced
10441 * with the created diffs. On failure, #deleteImplicitDiffs() is implicitly
10442 * called to delete created diffs which will also rollback mMediaData and restore
10443 * whatever was backed up before calling this method.
10444 *
10445 * Attachments with non-normal hard disks are left as is.
10446 *
10447 * If @a aOnline is @c false then the original hard disks that require implicit
10448 * diffs will be locked for reading. Otherwise it is assumed that they are
10449 * already locked for writing (when the VM was started). Note that in the latter
10450 * case it is responsibility of the caller to lock the newly created diffs for
10451 * writing if this method succeeds.
10452 *
10453 * @param aProgress Progress object to run (must contain at least as
10454 * many operations left as the number of hard disks
10455 * attached).
10456 * @param aOnline Whether the VM was online prior to this operation.
10457 *
10458 * @note The progress object is not marked as completed, neither on success nor
10459 * on failure. This is a responsibility of the caller.
10460 *
10461 * @note Locks this object and the media tree for writing.
10462 */
10463HRESULT Machine::i_createImplicitDiffs(IProgress *aProgress,
10464 ULONG aWeight,
10465 bool aOnline)
10466{
10467 LogFlowThisFunc(("aOnline=%d\n", aOnline));
10468
10469 AutoCaller autoCaller(this);
10470 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
10471
10472 AutoMultiWriteLock2 alock(this->lockHandle(),
10473 &mParent->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
10474
10475 /* must be in a protective state because we release the lock below */
10476 AssertReturn( mData->mMachineState == MachineState_Saving
10477 || mData->mMachineState == MachineState_LiveSnapshotting
10478 || mData->mMachineState == MachineState_RestoringSnapshot
10479 || mData->mMachineState == MachineState_DeletingSnapshot
10480 , E_FAIL);
10481
10482 HRESULT rc = S_OK;
10483
10484 // use appropriate locked media map (online or offline)
10485 MediumLockListMap lockedMediaOffline;
10486 MediumLockListMap *lockedMediaMap;
10487 if (aOnline)
10488 lockedMediaMap = &mData->mSession.mLockedMedia;
10489 else
10490 lockedMediaMap = &lockedMediaOffline;
10491
10492 try
10493 {
10494 if (!aOnline)
10495 {
10496 /* lock all attached hard disks early to detect "in use"
10497 * situations before creating actual diffs */
10498 for (MediaData::AttachmentList::const_iterator it = mMediaData->mAttachments.begin();
10499 it != mMediaData->mAttachments.end();
10500 ++it)
10501 {
10502 MediumAttachment* pAtt = *it;
10503 if (pAtt->i_getType() == DeviceType_HardDisk)
10504 {
10505 Medium* pMedium = pAtt->i_getMedium();
10506 Assert(pMedium);
10507
10508 MediumLockList *pMediumLockList(new MediumLockList());
10509 alock.release();
10510 rc = pMedium->i_createMediumLockList(true /* fFailIfInaccessible */,
10511 false /* fMediumLockWrite */,
10512 NULL,
10513 *pMediumLockList);
10514 alock.acquire();
10515 if (FAILED(rc))
10516 {
10517 delete pMediumLockList;
10518 throw rc;
10519 }
10520 rc = lockedMediaMap->Insert(pAtt, pMediumLockList);
10521 if (FAILED(rc))
10522 {
10523 throw setError(rc,
10524 tr("Collecting locking information for all attached media failed"));
10525 }
10526 }
10527 }
10528
10529 /* Now lock all media. If this fails, nothing is locked. */
10530 alock.release();
10531 rc = lockedMediaMap->Lock();
10532 alock.acquire();
10533 if (FAILED(rc))
10534 {
10535 throw setError(rc,
10536 tr("Locking of attached media failed"));
10537 }
10538 }
10539
10540 /* remember the current list (note that we don't use backup() since
10541 * mMediaData may be already backed up) */
10542 MediaData::AttachmentList atts = mMediaData->mAttachments;
10543
10544 /* start from scratch */
10545 mMediaData->mAttachments.clear();
10546
10547 /* go through remembered attachments and create diffs for normal hard
10548 * disks and attach them */
10549 for (MediaData::AttachmentList::const_iterator it = atts.begin();
10550 it != atts.end();
10551 ++it)
10552 {
10553 MediumAttachment* pAtt = *it;
10554
10555 DeviceType_T devType = pAtt->i_getType();
10556 Medium* pMedium = pAtt->i_getMedium();
10557
10558 if ( devType != DeviceType_HardDisk
10559 || pMedium == NULL
10560 || pMedium->i_getType() != MediumType_Normal)
10561 {
10562 /* copy the attachment as is */
10563
10564 /** @todo the progress object created in Console::TakeSnaphot
10565 * only expects operations for hard disks. Later other
10566 * device types need to show up in the progress as well. */
10567 if (devType == DeviceType_HardDisk)
10568 {
10569 if (pMedium == NULL)
10570 aProgress->SetNextOperation(Bstr(tr("Skipping attachment without medium")).raw(),
10571 aWeight); // weight
10572 else
10573 aProgress->SetNextOperation(BstrFmt(tr("Skipping medium '%s'"),
10574 pMedium->i_getBase()->i_getName().c_str()).raw(),
10575 aWeight); // weight
10576 }
10577
10578 mMediaData->mAttachments.push_back(pAtt);
10579 continue;
10580 }
10581
10582 /* need a diff */
10583 aProgress->SetNextOperation(BstrFmt(tr("Creating differencing hard disk for '%s'"),
10584 pMedium->i_getBase()->i_getName().c_str()).raw(),
10585 aWeight); // weight
10586
10587 Utf8Str strFullSnapshotFolder;
10588 i_calculateFullPath(mUserData->s.strSnapshotFolder, strFullSnapshotFolder);
10589
10590 ComObjPtr<Medium> diff;
10591 diff.createObject();
10592 // store the diff in the same registry as the parent
10593 // (this cannot fail here because we can't create implicit diffs for
10594 // unregistered images)
10595 Guid uuidRegistryParent;
10596 bool fInRegistry = pMedium->i_getFirstRegistryMachineId(uuidRegistryParent);
10597 Assert(fInRegistry); NOREF(fInRegistry);
10598 rc = diff->init(mParent,
10599 pMedium->i_getPreferredDiffFormat(),
10600 strFullSnapshotFolder.append(RTPATH_SLASH_STR),
10601 uuidRegistryParent);
10602 if (FAILED(rc)) throw rc;
10603
10604 /** @todo r=bird: How is the locking and diff image cleaned up if we fail before
10605 * the push_back? Looks like we're going to release medium with the
10606 * wrong kind of lock (general issue with if we fail anywhere at all)
10607 * and an orphaned VDI in the snapshots folder. */
10608
10609 /* update the appropriate lock list */
10610 MediumLockList *pMediumLockList;
10611 rc = lockedMediaMap->Get(pAtt, pMediumLockList);
10612 AssertComRCThrowRC(rc);
10613 if (aOnline)
10614 {
10615 alock.release();
10616 /* The currently attached medium will be read-only, change
10617 * the lock type to read. */
10618 rc = pMediumLockList->Update(pMedium, false);
10619 alock.acquire();
10620 AssertComRCThrowRC(rc);
10621 }
10622
10623 /* release the locks before the potentially lengthy operation */
10624 alock.release();
10625 rc = pMedium->i_createDiffStorage(diff, MediumVariant_Standard,
10626 pMediumLockList,
10627 NULL /* aProgress */,
10628 true /* aWait */);
10629 alock.acquire();
10630 if (FAILED(rc)) throw rc;
10631
10632 /* actual lock list update is done in Medium::commitMedia */
10633
10634 rc = diff->i_addBackReference(mData->mUuid);
10635 AssertComRCThrowRC(rc);
10636
10637 /* add a new attachment */
10638 ComObjPtr<MediumAttachment> attachment;
10639 attachment.createObject();
10640 rc = attachment->init(this,
10641 diff,
10642 pAtt->i_getControllerName(),
10643 pAtt->i_getPort(),
10644 pAtt->i_getDevice(),
10645 DeviceType_HardDisk,
10646 true /* aImplicit */,
10647 false /* aPassthrough */,
10648 false /* aTempEject */,
10649 pAtt->i_getNonRotational(),
10650 pAtt->i_getDiscard(),
10651 pAtt->i_getHotPluggable(),
10652 pAtt->i_getBandwidthGroup());
10653 if (FAILED(rc)) throw rc;
10654
10655 rc = lockedMediaMap->ReplaceKey(pAtt, attachment);
10656 AssertComRCThrowRC(rc);
10657 mMediaData->mAttachments.push_back(attachment);
10658 }
10659 }
10660 catch (HRESULT aRC) { rc = aRC; }
10661
10662 /* unlock all hard disks we locked when there is no VM */
10663 if (!aOnline)
10664 {
10665 ErrorInfoKeeper eik;
10666
10667 HRESULT rc1 = lockedMediaMap->Clear();
10668 AssertComRC(rc1);
10669 }
10670
10671 return rc;
10672}
10673
10674/**
10675 * Deletes implicit differencing hard disks created either by
10676 * #createImplicitDiffs() or by #AttachDevice() and rolls back mMediaData.
10677 *
10678 * Note that to delete hard disks created by #AttachDevice() this method is
10679 * called from #fixupMedia() when the changes are rolled back.
10680 *
10681 * @note Locks this object and the media tree for writing.
10682 */
10683HRESULT Machine::i_deleteImplicitDiffs(bool aOnline)
10684{
10685 LogFlowThisFunc(("aOnline=%d\n", aOnline));
10686
10687 AutoCaller autoCaller(this);
10688 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
10689
10690 AutoMultiWriteLock2 alock(this->lockHandle(),
10691 &mParent->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
10692
10693 /* We absolutely must have backed up state. */
10694 AssertReturn(mMediaData.isBackedUp(), E_FAIL);
10695
10696 /* Check if there are any implicitly created diff images. */
10697 bool fImplicitDiffs = false;
10698 for (MediaData::AttachmentList::const_iterator it = mMediaData->mAttachments.begin();
10699 it != mMediaData->mAttachments.end();
10700 ++it)
10701 {
10702 const ComObjPtr<MediumAttachment> &pAtt = *it;
10703 if (pAtt->i_isImplicit())
10704 {
10705 fImplicitDiffs = true;
10706 break;
10707 }
10708 }
10709 /* If there is nothing to do, leave early. This saves lots of image locking
10710 * effort. It also avoids a MachineStateChanged event without real reason.
10711 * This is important e.g. when loading a VM config, because there should be
10712 * no events. Otherwise API clients can become thoroughly confused for
10713 * inaccessible VMs (the code for loading VM configs uses this method for
10714 * cleanup if the config makes no sense), as they take such events as an
10715 * indication that the VM is alive, and they would force the VM config to
10716 * be reread, leading to an endless loop. */
10717 if (!fImplicitDiffs)
10718 return S_OK;
10719
10720 HRESULT rc = S_OK;
10721 MachineState_T oldState = mData->mMachineState;
10722
10723 /* will release the lock before the potentially lengthy operation,
10724 * so protect with the special state (unless already protected) */
10725 if ( oldState != MachineState_Saving
10726 && oldState != MachineState_LiveSnapshotting
10727 && oldState != MachineState_RestoringSnapshot
10728 && oldState != MachineState_DeletingSnapshot
10729 && oldState != MachineState_DeletingSnapshotOnline
10730 && oldState != MachineState_DeletingSnapshotPaused
10731 )
10732 i_setMachineState(MachineState_SettingUp);
10733
10734 // use appropriate locked media map (online or offline)
10735 MediumLockListMap lockedMediaOffline;
10736 MediumLockListMap *lockedMediaMap;
10737 if (aOnline)
10738 lockedMediaMap = &mData->mSession.mLockedMedia;
10739 else
10740 lockedMediaMap = &lockedMediaOffline;
10741
10742 try
10743 {
10744 if (!aOnline)
10745 {
10746 /* lock all attached hard disks early to detect "in use"
10747 * situations before deleting actual diffs */
10748 for (MediaData::AttachmentList::const_iterator it = mMediaData->mAttachments.begin();
10749 it != mMediaData->mAttachments.end();
10750 ++it)
10751 {
10752 MediumAttachment* pAtt = *it;
10753 if (pAtt->i_getType() == DeviceType_HardDisk)
10754 {
10755 Medium* pMedium = pAtt->i_getMedium();
10756 Assert(pMedium);
10757
10758 MediumLockList *pMediumLockList(new MediumLockList());
10759 alock.release();
10760 rc = pMedium->i_createMediumLockList(true /* fFailIfInaccessible */,
10761 false /* fMediumLockWrite */,
10762 NULL,
10763 *pMediumLockList);
10764 alock.acquire();
10765
10766 if (FAILED(rc))
10767 {
10768 delete pMediumLockList;
10769 throw rc;
10770 }
10771
10772 rc = lockedMediaMap->Insert(pAtt, pMediumLockList);
10773 if (FAILED(rc))
10774 throw rc;
10775 }
10776 }
10777
10778 if (FAILED(rc))
10779 throw rc;
10780 } // end of offline
10781
10782 /* Lock lists are now up to date and include implicitly created media */
10783
10784 /* Go through remembered attachments and delete all implicitly created
10785 * diffs and fix up the attachment information */
10786 const MediaData::AttachmentList &oldAtts = mMediaData.backedUpData()->mAttachments;
10787 MediaData::AttachmentList implicitAtts;
10788 for (MediaData::AttachmentList::const_iterator it = mMediaData->mAttachments.begin();
10789 it != mMediaData->mAttachments.end();
10790 ++it)
10791 {
10792 ComObjPtr<MediumAttachment> pAtt = *it;
10793 ComObjPtr<Medium> pMedium = pAtt->i_getMedium();
10794 if (pMedium.isNull())
10795 continue;
10796
10797 // Implicit attachments go on the list for deletion and back references are removed.
10798 if (pAtt->i_isImplicit())
10799 {
10800 /* Deassociate and mark for deletion */
10801 LogFlowThisFunc(("Detaching '%s', pending deletion\n", pAtt->i_getLogName()));
10802 rc = pMedium->i_removeBackReference(mData->mUuid);
10803 if (FAILED(rc))
10804 throw rc;
10805 implicitAtts.push_back(pAtt);
10806 continue;
10807 }
10808
10809 /* Was this medium attached before? */
10810 if (!i_findAttachment(oldAtts, pMedium))
10811 {
10812 /* no: de-associate */
10813 LogFlowThisFunc(("Detaching '%s', no deletion\n", pAtt->i_getLogName()));
10814 rc = pMedium->i_removeBackReference(mData->mUuid);
10815 if (FAILED(rc))
10816 throw rc;
10817 continue;
10818 }
10819 LogFlowThisFunc(("Not detaching '%s'\n", pAtt->i_getLogName()));
10820 }
10821
10822 /* If there are implicit attachments to delete, throw away the lock
10823 * map contents (which will unlock all media) since the medium
10824 * attachments will be rolled back. Below we need to completely
10825 * recreate the lock map anyway since it is infinitely complex to
10826 * do this incrementally (would need reconstructing each attachment
10827 * change, which would be extremely hairy). */
10828 if (implicitAtts.size() != 0)
10829 {
10830 ErrorInfoKeeper eik;
10831
10832 HRESULT rc1 = lockedMediaMap->Clear();
10833 AssertComRC(rc1);
10834 }
10835
10836 /* rollback hard disk changes */
10837 mMediaData.rollback();
10838
10839 MultiResult mrc(S_OK);
10840
10841 // Delete unused implicit diffs.
10842 if (implicitAtts.size() != 0)
10843 {
10844 alock.release();
10845
10846 for (MediaData::AttachmentList::const_iterator it = implicitAtts.begin(); it != implicitAtts.end(); ++it)
10847 {
10848 // Remove medium associated with this attachment.
10849 ComObjPtr<MediumAttachment> pAtt = *it;
10850 Assert(pAtt);
10851 LogFlowThisFunc(("Deleting '%s'\n", pAtt->i_getLogName()));
10852 ComObjPtr<Medium> pMedium = pAtt->i_getMedium();
10853 Assert(pMedium);
10854
10855 rc = pMedium->i_deleteStorage(NULL /*aProgress*/, true /*aWait*/);
10856 // continue on delete failure, just collect error messages
10857 AssertMsg(SUCCEEDED(rc), ("rc=%Rhrc it=%s hd=%s\n", rc, pAtt->i_getLogName(),
10858 pMedium->i_getLocationFull().c_str() ));
10859 mrc = rc;
10860 }
10861
10862 alock.acquire();
10863
10864 /* if there is a VM recreate media lock map as mentioned above,
10865 * otherwise it is a waste of time and we leave things unlocked */
10866 if (aOnline)
10867 {
10868 const ComObjPtr<SessionMachine> pMachine = mData->mSession.mMachine;
10869 /* must never be NULL, but better safe than sorry */
10870 if (!pMachine.isNull())
10871 {
10872 alock.release();
10873 rc = mData->mSession.mMachine->i_lockMedia();
10874 alock.acquire();
10875 if (FAILED(rc))
10876 throw rc;
10877 }
10878 }
10879 }
10880 }
10881 catch (HRESULT aRC) {rc = aRC;}
10882
10883 if (mData->mMachineState == MachineState_SettingUp)
10884 i_setMachineState(oldState);
10885
10886 /* unlock all hard disks we locked when there is no VM */
10887 if (!aOnline)
10888 {
10889 ErrorInfoKeeper eik;
10890
10891 HRESULT rc1 = lockedMediaMap->Clear();
10892 AssertComRC(rc1);
10893 }
10894
10895 return rc;
10896}
10897
10898
10899/**
10900 * Looks through the given list of media attachments for one with the given parameters
10901 * and returns it, or NULL if not found. The list is a parameter so that backup lists
10902 * can be searched as well if needed.
10903 *
10904 * @param list
10905 * @param aControllerName
10906 * @param aControllerPort
10907 * @param aDevice
10908 * @return
10909 */
10910MediumAttachment* Machine::i_findAttachment(const MediaData::AttachmentList &ll,
10911 IN_BSTR aControllerName,
10912 LONG aControllerPort,
10913 LONG aDevice)
10914{
10915 for (MediaData::AttachmentList::const_iterator it = ll.begin(); it != ll.end(); ++it)
10916 {
10917 MediumAttachment *pAttach = *it;
10918 if (pAttach->i_matches(aControllerName, aControllerPort, aDevice))
10919 return pAttach;
10920 }
10921
10922 return NULL;
10923}
10924
10925/**
10926 * Looks through the given list of media attachments for one with the given parameters
10927 * and returns it, or NULL if not found. The list is a parameter so that backup lists
10928 * can be searched as well if needed.
10929 *
10930 * @param list
10931 * @param aControllerName
10932 * @param aControllerPort
10933 * @param aDevice
10934 * @return
10935 */
10936MediumAttachment* Machine::i_findAttachment(const MediaData::AttachmentList &ll,
10937 ComObjPtr<Medium> pMedium)
10938{
10939 for (MediaData::AttachmentList::const_iterator it = ll.begin(); it != ll.end(); ++it)
10940 {
10941 MediumAttachment *pAttach = *it;
10942 ComObjPtr<Medium> pMediumThis = pAttach->i_getMedium();
10943 if (pMediumThis == pMedium)
10944 return pAttach;
10945 }
10946
10947 return NULL;
10948}
10949
10950/**
10951 * Looks through the given list of media attachments for one with the given parameters
10952 * and returns it, or NULL if not found. The list is a parameter so that backup lists
10953 * can be searched as well if needed.
10954 *
10955 * @param list
10956 * @param aControllerName
10957 * @param aControllerPort
10958 * @param aDevice
10959 * @return
10960 */
10961MediumAttachment* Machine::i_findAttachment(const MediaData::AttachmentList &ll,
10962 Guid &id)
10963{
10964 for (MediaData::AttachmentList::const_iterator it = ll.begin(); it != ll.end(); ++it)
10965 {
10966 MediumAttachment *pAttach = *it;
10967 ComObjPtr<Medium> pMediumThis = pAttach->i_getMedium();
10968 if (pMediumThis->i_getId() == id)
10969 return pAttach;
10970 }
10971
10972 return NULL;
10973}
10974
10975/**
10976 * Main implementation for Machine::DetachDevice. This also gets called
10977 * from Machine::prepareUnregister() so it has been taken out for simplicity.
10978 *
10979 * @param pAttach Medium attachment to detach.
10980 * @param writeLock Machine write lock which the caller must have locked once. This may be released temporarily in here.
10981 * @param pSnapshot If NULL, then the detachment is for the current machine. Otherwise this is for a
10982 * SnapshotMachine, and this must be its snapshot.
10983 * @return
10984 */
10985HRESULT Machine::i_detachDevice(MediumAttachment *pAttach,
10986 AutoWriteLock &writeLock,
10987 Snapshot *pSnapshot)
10988{
10989 ComObjPtr<Medium> oldmedium = pAttach->i_getMedium();
10990 DeviceType_T mediumType = pAttach->i_getType();
10991
10992 LogFlowThisFunc(("Entering, medium of attachment is %s\n", oldmedium ? oldmedium->i_getLocationFull().c_str() : "NULL"));
10993
10994 if (pAttach->i_isImplicit())
10995 {
10996 /* attempt to implicitly delete the implicitly created diff */
10997
10998 /// @todo move the implicit flag from MediumAttachment to Medium
10999 /// and forbid any hard disk operation when it is implicit. Or maybe
11000 /// a special media state for it to make it even more simple.
11001
11002 Assert(mMediaData.isBackedUp());
11003
11004 /* will release the lock before the potentially lengthy operation, so
11005 * protect with the special state */
11006 MachineState_T oldState = mData->mMachineState;
11007 i_setMachineState(MachineState_SettingUp);
11008
11009 writeLock.release();
11010
11011 HRESULT rc = oldmedium->i_deleteStorage(NULL /*aProgress*/,
11012 true /*aWait*/);
11013
11014 writeLock.acquire();
11015
11016 i_setMachineState(oldState);
11017
11018 if (FAILED(rc)) return rc;
11019 }
11020
11021 i_setModified(IsModified_Storage);
11022 mMediaData.backup();
11023 mMediaData->mAttachments.remove(pAttach);
11024
11025 if (!oldmedium.isNull())
11026 {
11027 // if this is from a snapshot, do not defer detachment to commitMedia()
11028 if (pSnapshot)
11029 oldmedium->i_removeBackReference(mData->mUuid, pSnapshot->i_getId());
11030 // else if non-hard disk media, do not defer detachment to commitMedia() either
11031 else if (mediumType != DeviceType_HardDisk)
11032 oldmedium->i_removeBackReference(mData->mUuid);
11033 }
11034
11035 return S_OK;
11036}
11037
11038/**
11039 * Goes thru all media of the given list and
11040 *
11041 * 1) calls i_detachDevice() on each of them for this machine and
11042 * 2) adds all Medium objects found in the process to the given list,
11043 * depending on cleanupMode.
11044 *
11045 * If cleanupMode is CleanupMode_DetachAllReturnHardDisksOnly, this only
11046 * adds hard disks to the list. If it is CleanupMode_Full, this adds all
11047 * media to the list.
11048 *
11049 * This gets called from Machine::Unregister, both for the actual Machine and
11050 * the SnapshotMachine objects that might be found in the snapshots.
11051 *
11052 * Requires caller and locking. The machine lock must be passed in because it
11053 * will be passed on to i_detachDevice which needs it for temporary unlocking.
11054 *
11055 * @param writeLock Machine lock from top-level caller; this gets passed to i_detachDevice.
11056 * @param pSnapshot Must be NULL when called for a "real" Machine or a snapshot object if called for a SnapshotMachine.
11057 * @param cleanupMode If DetachAllReturnHardDisksOnly, only hard disk media get added to llMedia; if
11058 * Full, then all media get added;
11059 * otherwise no media get added.
11060 * @param llMedia Caller's list to receive Medium objects which got detached so caller can close() them, depending on cleanupMode.
11061 * @return
11062 */
11063HRESULT Machine::i_detachAllMedia(AutoWriteLock &writeLock,
11064 Snapshot *pSnapshot,
11065 CleanupMode_T cleanupMode,
11066 MediaList &llMedia)
11067{
11068 Assert(isWriteLockOnCurrentThread());
11069
11070 HRESULT rc;
11071
11072 // make a temporary list because i_detachDevice invalidates iterators into
11073 // mMediaData->mAttachments
11074 MediaData::AttachmentList llAttachments2 = mMediaData->mAttachments;
11075
11076 for (MediaData::AttachmentList::iterator it = llAttachments2.begin(); it != llAttachments2.end(); ++it)
11077 {
11078 ComObjPtr<MediumAttachment> &pAttach = *it;
11079 ComObjPtr<Medium> pMedium = pAttach->i_getMedium();
11080
11081 if (!pMedium.isNull())
11082 {
11083 AutoCaller mac(pMedium);
11084 if (FAILED(mac.rc())) return mac.rc();
11085 AutoReadLock lock(pMedium COMMA_LOCKVAL_SRC_POS);
11086 DeviceType_T devType = pMedium->i_getDeviceType();
11087 if ( ( cleanupMode == CleanupMode_DetachAllReturnHardDisksOnly
11088 && devType == DeviceType_HardDisk)
11089 || (cleanupMode == CleanupMode_Full)
11090 )
11091 {
11092 llMedia.push_back(pMedium);
11093 ComObjPtr<Medium> pParent = pMedium->i_getParent();
11094 /*
11095 * Search for medias which are not attached to any machine, but
11096 * in the chain to an attached disk. Mediums are only consided
11097 * if they are:
11098 * - have only one child
11099 * - no references to any machines
11100 * - are of normal medium type
11101 */
11102 while (!pParent.isNull())
11103 {
11104 AutoCaller mac1(pParent);
11105 if (FAILED(mac1.rc())) return mac1.rc();
11106 AutoReadLock lock1(pParent COMMA_LOCKVAL_SRC_POS);
11107 if (pParent->i_getChildren().size() == 1)
11108 {
11109 if ( pParent->i_getMachineBackRefCount() == 0
11110 && pParent->i_getType() == MediumType_Normal
11111 && find(llMedia.begin(), llMedia.end(), pParent) == llMedia.end())
11112 llMedia.push_back(pParent);
11113 }
11114 else
11115 break;
11116 pParent = pParent->i_getParent();
11117 }
11118 }
11119 }
11120
11121 // real machine: then we need to use the proper method
11122 rc = i_detachDevice(pAttach, writeLock, pSnapshot);
11123
11124 if (FAILED(rc))
11125 return rc;
11126 }
11127
11128 return S_OK;
11129}
11130
11131/**
11132 * Perform deferred hard disk detachments.
11133 *
11134 * Does nothing if the hard disk attachment data (mMediaData) is not changed (not
11135 * backed up).
11136 *
11137 * If @a aOnline is @c true then this method will also unlock the old hard disks
11138 * for which the new implicit diffs were created and will lock these new diffs for
11139 * writing.
11140 *
11141 * @param aOnline Whether the VM was online prior to this operation.
11142 *
11143 * @note Locks this object for writing!
11144 */
11145void Machine::i_commitMedia(bool aOnline /*= false*/)
11146{
11147 AutoCaller autoCaller(this);
11148 AssertComRCReturnVoid(autoCaller.rc());
11149
11150 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
11151
11152 LogFlowThisFunc(("Entering, aOnline=%d\n", aOnline));
11153
11154 HRESULT rc = S_OK;
11155
11156 /* no attach/detach operations -- nothing to do */
11157 if (!mMediaData.isBackedUp())
11158 return;
11159
11160 MediaData::AttachmentList &oldAtts = mMediaData.backedUpData()->mAttachments;
11161 bool fMediaNeedsLocking = false;
11162
11163 /* enumerate new attachments */
11164 for (MediaData::AttachmentList::const_iterator it = mMediaData->mAttachments.begin();
11165 it != mMediaData->mAttachments.end();
11166 ++it)
11167 {
11168 MediumAttachment *pAttach = *it;
11169
11170 pAttach->i_commit();
11171
11172 Medium* pMedium = pAttach->i_getMedium();
11173 bool fImplicit = pAttach->i_isImplicit();
11174
11175 LogFlowThisFunc(("Examining current medium '%s' (implicit: %d)\n",
11176 (pMedium) ? pMedium->i_getName().c_str() : "NULL",
11177 fImplicit));
11178
11179 /** @todo convert all this Machine-based voodoo to MediumAttachment
11180 * based commit logic. */
11181 if (fImplicit)
11182 {
11183 /* convert implicit attachment to normal */
11184 pAttach->i_setImplicit(false);
11185
11186 if ( aOnline
11187 && pMedium
11188 && pAttach->i_getType() == DeviceType_HardDisk
11189 )
11190 {
11191 ComObjPtr<Medium> parent = pMedium->i_getParent();
11192 AutoWriteLock parentLock(parent COMMA_LOCKVAL_SRC_POS);
11193
11194 /* update the appropriate lock list */
11195 MediumLockList *pMediumLockList;
11196 rc = mData->mSession.mLockedMedia.Get(pAttach, pMediumLockList);
11197 AssertComRC(rc);
11198 if (pMediumLockList)
11199 {
11200 /* unlock if there's a need to change the locking */
11201 if (!fMediaNeedsLocking)
11202 {
11203 rc = mData->mSession.mLockedMedia.Unlock();
11204 AssertComRC(rc);
11205 fMediaNeedsLocking = true;
11206 }
11207 rc = pMediumLockList->Update(parent, false);
11208 AssertComRC(rc);
11209 rc = pMediumLockList->Append(pMedium, true);
11210 AssertComRC(rc);
11211 }
11212 }
11213
11214 continue;
11215 }
11216
11217 if (pMedium)
11218 {
11219 /* was this medium attached before? */
11220 for (MediaData::AttachmentList::iterator oldIt = oldAtts.begin(); oldIt != oldAtts.end(); ++oldIt)
11221 {
11222 MediumAttachment *pOldAttach = *oldIt;
11223 if (pOldAttach->i_getMedium() == pMedium)
11224 {
11225 LogFlowThisFunc(("--> medium '%s' was attached before, will not remove\n", pMedium->i_getName().c_str()));
11226
11227 /* yes: remove from old to avoid de-association */
11228 oldAtts.erase(oldIt);
11229 break;
11230 }
11231 }
11232 }
11233 }
11234
11235 /* enumerate remaining old attachments and de-associate from the
11236 * current machine state */
11237 for (MediaData::AttachmentList::const_iterator it = oldAtts.begin(); it != oldAtts.end(); ++it)
11238 {
11239 MediumAttachment *pAttach = *it;
11240 Medium* pMedium = pAttach->i_getMedium();
11241
11242 /* Detach only hard disks, since DVD/floppy media is detached
11243 * instantly in MountMedium. */
11244 if (pAttach->i_getType() == DeviceType_HardDisk && pMedium)
11245 {
11246 LogFlowThisFunc(("detaching medium '%s' from machine\n", pMedium->i_getName().c_str()));
11247
11248 /* now de-associate from the current machine state */
11249 rc = pMedium->i_removeBackReference(mData->mUuid);
11250 AssertComRC(rc);
11251
11252 if (aOnline)
11253 {
11254 /* unlock since medium is not used anymore */
11255 MediumLockList *pMediumLockList;
11256 rc = mData->mSession.mLockedMedia.Get(pAttach, pMediumLockList);
11257 if (RT_UNLIKELY(rc == VBOX_E_INVALID_OBJECT_STATE))
11258 {
11259 /* this happens for online snapshots, there the attachment
11260 * is changing, but only to a diff image created under
11261 * the old one, so there is no separate lock list */
11262 Assert(!pMediumLockList);
11263 }
11264 else
11265 {
11266 AssertComRC(rc);
11267 if (pMediumLockList)
11268 {
11269 rc = mData->mSession.mLockedMedia.Remove(pAttach);
11270 AssertComRC(rc);
11271 }
11272 }
11273 }
11274 }
11275 }
11276
11277 /* take media locks again so that the locking state is consistent */
11278 if (fMediaNeedsLocking)
11279 {
11280 Assert(aOnline);
11281 rc = mData->mSession.mLockedMedia.Lock();
11282 AssertComRC(rc);
11283 }
11284
11285 /* commit the hard disk changes */
11286 mMediaData.commit();
11287
11288 if (i_isSessionMachine())
11289 {
11290 /*
11291 * Update the parent machine to point to the new owner.
11292 * This is necessary because the stored parent will point to the
11293 * session machine otherwise and cause crashes or errors later
11294 * when the session machine gets invalid.
11295 */
11296 /** @todo Change the MediumAttachment class to behave like any other
11297 * class in this regard by creating peer MediumAttachment
11298 * objects for session machines and share the data with the peer
11299 * machine.
11300 */
11301 for (MediaData::AttachmentList::const_iterator it = mMediaData->mAttachments.begin();
11302 it != mMediaData->mAttachments.end();
11303 ++it)
11304 (*it)->i_updateParentMachine(mPeer);
11305
11306 /* attach new data to the primary machine and reshare it */
11307 mPeer->mMediaData.attach(mMediaData);
11308 }
11309
11310 return;
11311}
11312
11313/**
11314 * Perform deferred deletion of implicitly created diffs.
11315 *
11316 * Does nothing if the hard disk attachment data (mMediaData) is not changed (not
11317 * backed up).
11318 *
11319 * @note Locks this object for writing!
11320 */
11321void Machine::i_rollbackMedia()
11322{
11323 AutoCaller autoCaller(this);
11324 AssertComRCReturnVoid(autoCaller.rc());
11325
11326 // AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
11327 LogFlowThisFunc(("Entering rollbackMedia\n"));
11328
11329 HRESULT rc = S_OK;
11330
11331 /* no attach/detach operations -- nothing to do */
11332 if (!mMediaData.isBackedUp())
11333 return;
11334
11335 /* enumerate new attachments */
11336 for (MediaData::AttachmentList::const_iterator it = mMediaData->mAttachments.begin();
11337 it != mMediaData->mAttachments.end();
11338 ++it)
11339 {
11340 MediumAttachment *pAttach = *it;
11341 /* Fix up the backrefs for DVD/floppy media. */
11342 if (pAttach->i_getType() != DeviceType_HardDisk)
11343 {
11344 Medium* pMedium = pAttach->i_getMedium();
11345 if (pMedium)
11346 {
11347 rc = pMedium->i_removeBackReference(mData->mUuid);
11348 AssertComRC(rc);
11349 }
11350 }
11351
11352 (*it)->i_rollback();
11353
11354 pAttach = *it;
11355 /* Fix up the backrefs for DVD/floppy media. */
11356 if (pAttach->i_getType() != DeviceType_HardDisk)
11357 {
11358 Medium* pMedium = pAttach->i_getMedium();
11359 if (pMedium)
11360 {
11361 rc = pMedium->i_addBackReference(mData->mUuid);
11362 AssertComRC(rc);
11363 }
11364 }
11365 }
11366
11367 /** @todo convert all this Machine-based voodoo to MediumAttachment
11368 * based rollback logic. */
11369 i_deleteImplicitDiffs(Global::IsOnline(mData->mMachineState));
11370
11371 return;
11372}
11373
11374/**
11375 * Returns true if the settings file is located in the directory named exactly
11376 * as the machine; this means, among other things, that the machine directory
11377 * should be auto-renamed.
11378 *
11379 * @param aSettingsDir if not NULL, the full machine settings file directory
11380 * name will be assigned there.
11381 *
11382 * @note Doesn't lock anything.
11383 * @note Not thread safe (must be called from this object's lock).
11384 */
11385bool Machine::i_isInOwnDir(Utf8Str *aSettingsDir /* = NULL */) const
11386{
11387 Utf8Str strMachineDirName(mData->m_strConfigFileFull); // path/to/machinesfolder/vmname/vmname.vbox
11388 strMachineDirName.stripFilename(); // path/to/machinesfolder/vmname
11389 if (aSettingsDir)
11390 *aSettingsDir = strMachineDirName;
11391 strMachineDirName.stripPath(); // vmname
11392 Utf8Str strConfigFileOnly(mData->m_strConfigFileFull); // path/to/machinesfolder/vmname/vmname.vbox
11393 strConfigFileOnly.stripPath() // vmname.vbox
11394 .stripSuffix(); // vmname
11395 /** @todo hack, make somehow use of ComposeMachineFilename */
11396 if (mUserData->s.fDirectoryIncludesUUID)
11397 strConfigFileOnly += Utf8StrFmt(" (%RTuuid)", mData->mUuid.raw());
11398
11399 AssertReturn(!strMachineDirName.isEmpty(), false);
11400 AssertReturn(!strConfigFileOnly.isEmpty(), false);
11401
11402 return strMachineDirName == strConfigFileOnly;
11403}
11404
11405/**
11406 * Discards all changes to machine settings.
11407 *
11408 * @param aNotify Whether to notify the direct session about changes or not.
11409 *
11410 * @note Locks objects for writing!
11411 */
11412void Machine::i_rollback(bool aNotify)
11413{
11414 AutoCaller autoCaller(this);
11415 AssertComRCReturn(autoCaller.rc(), (void)0);
11416
11417 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
11418
11419 if (!mStorageControllers.isNull())
11420 {
11421 if (mStorageControllers.isBackedUp())
11422 {
11423 /* unitialize all new devices (absent in the backed up list). */
11424 StorageControllerList::const_iterator it = mStorageControllers->begin();
11425 StorageControllerList *backedList = mStorageControllers.backedUpData();
11426 while (it != mStorageControllers->end())
11427 {
11428 if ( std::find(backedList->begin(), backedList->end(), *it)
11429 == backedList->end()
11430 )
11431 {
11432 (*it)->uninit();
11433 }
11434 ++it;
11435 }
11436
11437 /* restore the list */
11438 mStorageControllers.rollback();
11439 }
11440
11441 /* rollback any changes to devices after restoring the list */
11442 if (mData->flModifications & IsModified_Storage)
11443 {
11444 StorageControllerList::const_iterator it = mStorageControllers->begin();
11445 while (it != mStorageControllers->end())
11446 {
11447 (*it)->i_rollback();
11448 ++it;
11449 }
11450 }
11451 }
11452
11453 if (!mUSBControllers.isNull())
11454 {
11455 if (mUSBControllers.isBackedUp())
11456 {
11457 /* unitialize all new devices (absent in the backed up list). */
11458 USBControllerList::const_iterator it = mUSBControllers->begin();
11459 USBControllerList *backedList = mUSBControllers.backedUpData();
11460 while (it != mUSBControllers->end())
11461 {
11462 if ( std::find(backedList->begin(), backedList->end(), *it)
11463 == backedList->end()
11464 )
11465 {
11466 (*it)->uninit();
11467 }
11468 ++it;
11469 }
11470
11471 /* restore the list */
11472 mUSBControllers.rollback();
11473 }
11474
11475 /* rollback any changes to devices after restoring the list */
11476 if (mData->flModifications & IsModified_USB)
11477 {
11478 USBControllerList::const_iterator it = mUSBControllers->begin();
11479 while (it != mUSBControllers->end())
11480 {
11481 (*it)->i_rollback();
11482 ++it;
11483 }
11484 }
11485 }
11486
11487 mUserData.rollback();
11488
11489 mHWData.rollback();
11490
11491 if (mData->flModifications & IsModified_Storage)
11492 i_rollbackMedia();
11493
11494 if (mBIOSSettings)
11495 mBIOSSettings->i_rollback();
11496
11497 if (mVRDEServer && (mData->flModifications & IsModified_VRDEServer))
11498 mVRDEServer->i_rollback();
11499
11500 if (mAudioAdapter)
11501 mAudioAdapter->i_rollback();
11502
11503 if (mUSBDeviceFilters && (mData->flModifications & IsModified_USB))
11504 mUSBDeviceFilters->i_rollback();
11505
11506 if (mBandwidthControl && (mData->flModifications & IsModified_BandwidthControl))
11507 mBandwidthControl->i_rollback();
11508
11509 if (!mHWData.isNull())
11510 mNetworkAdapters.resize(Global::getMaxNetworkAdapters(mHWData->mChipsetType));
11511 NetworkAdapterVector networkAdapters(mNetworkAdapters.size());
11512 ComPtr<ISerialPort> serialPorts[RT_ELEMENTS(mSerialPorts)];
11513 ComPtr<IParallelPort> parallelPorts[RT_ELEMENTS(mParallelPorts)];
11514
11515 if (mData->flModifications & IsModified_NetworkAdapters)
11516 for (ULONG slot = 0; slot < mNetworkAdapters.size(); ++slot)
11517 if ( mNetworkAdapters[slot]
11518 && mNetworkAdapters[slot]->i_isModified())
11519 {
11520 mNetworkAdapters[slot]->i_rollback();
11521 networkAdapters[slot] = mNetworkAdapters[slot];
11522 }
11523
11524 if (mData->flModifications & IsModified_SerialPorts)
11525 for (ULONG slot = 0; slot < RT_ELEMENTS(mSerialPorts); ++slot)
11526 if ( mSerialPorts[slot]
11527 && mSerialPorts[slot]->i_isModified())
11528 {
11529 mSerialPorts[slot]->i_rollback();
11530 serialPorts[slot] = mSerialPorts[slot];
11531 }
11532
11533 if (mData->flModifications & IsModified_ParallelPorts)
11534 for (ULONG slot = 0; slot < RT_ELEMENTS(mParallelPorts); ++slot)
11535 if ( mParallelPorts[slot]
11536 && mParallelPorts[slot]->i_isModified())
11537 {
11538 mParallelPorts[slot]->i_rollback();
11539 parallelPorts[slot] = mParallelPorts[slot];
11540 }
11541
11542 if (aNotify)
11543 {
11544 /* inform the direct session about changes */
11545
11546 ComObjPtr<Machine> that = this;
11547 uint32_t flModifications = mData->flModifications;
11548 alock.release();
11549
11550 if (flModifications & IsModified_SharedFolders)
11551 that->i_onSharedFolderChange();
11552
11553 if (flModifications & IsModified_VRDEServer)
11554 that->i_onVRDEServerChange(/* aRestart */ TRUE);
11555 if (flModifications & IsModified_USB)
11556 that->i_onUSBControllerChange();
11557
11558 for (ULONG slot = 0; slot < networkAdapters.size(); ++slot)
11559 if (networkAdapters[slot])
11560 that->i_onNetworkAdapterChange(networkAdapters[slot], FALSE);
11561 for (ULONG slot = 0; slot < RT_ELEMENTS(serialPorts); ++slot)
11562 if (serialPorts[slot])
11563 that->i_onSerialPortChange(serialPorts[slot]);
11564 for (ULONG slot = 0; slot < RT_ELEMENTS(parallelPorts); ++slot)
11565 if (parallelPorts[slot])
11566 that->i_onParallelPortChange(parallelPorts[slot]);
11567
11568 if (flModifications & IsModified_Storage)
11569 that->i_onStorageControllerChange();
11570
11571#if 0
11572 if (flModifications & IsModified_BandwidthControl)
11573 that->onBandwidthControlChange();
11574#endif
11575 }
11576}
11577
11578/**
11579 * Commits all the changes to machine settings.
11580 *
11581 * Note that this operation is supposed to never fail.
11582 *
11583 * @note Locks this object and children for writing.
11584 */
11585void Machine::i_commit()
11586{
11587 AutoCaller autoCaller(this);
11588 AssertComRCReturnVoid(autoCaller.rc());
11589
11590 AutoCaller peerCaller(mPeer);
11591 AssertComRCReturnVoid(peerCaller.rc());
11592
11593 AutoMultiWriteLock2 alock(mPeer, this COMMA_LOCKVAL_SRC_POS);
11594
11595 /*
11596 * use safe commit to ensure Snapshot machines (that share mUserData)
11597 * will still refer to a valid memory location
11598 */
11599 mUserData.commitCopy();
11600
11601 mHWData.commit();
11602
11603 if (mMediaData.isBackedUp())
11604 i_commitMedia(Global::IsOnline(mData->mMachineState));
11605
11606 mBIOSSettings->i_commit();
11607 mVRDEServer->i_commit();
11608 mAudioAdapter->i_commit();
11609 mUSBDeviceFilters->i_commit();
11610 mBandwidthControl->i_commit();
11611
11612 /* Since mNetworkAdapters is a list which might have been changed (resized)
11613 * without using the Backupable<> template we need to handle the copying
11614 * of the list entries manually, including the creation of peers for the
11615 * new objects. */
11616 bool commitNetworkAdapters = false;
11617 size_t newSize = Global::getMaxNetworkAdapters(mHWData->mChipsetType);
11618 if (mPeer)
11619 {
11620 /* commit everything, even the ones which will go away */
11621 for (size_t slot = 0; slot < mNetworkAdapters.size(); slot++)
11622 mNetworkAdapters[slot]->i_commit();
11623 /* copy over the new entries, creating a peer and uninit the original */
11624 mPeer->mNetworkAdapters.resize(RT_MAX(newSize, mPeer->mNetworkAdapters.size()));
11625 for (size_t slot = 0; slot < newSize; slot++)
11626 {
11627 /* look if this adapter has a peer device */
11628 ComObjPtr<NetworkAdapter> peer = mNetworkAdapters[slot]->i_getPeer();
11629 if (!peer)
11630 {
11631 /* no peer means the adapter is a newly created one;
11632 * create a peer owning data this data share it with */
11633 peer.createObject();
11634 peer->init(mPeer, mNetworkAdapters[slot], true /* aReshare */);
11635 }
11636 mPeer->mNetworkAdapters[slot] = peer;
11637 }
11638 /* uninit any no longer needed network adapters */
11639 for (size_t slot = newSize; slot < mNetworkAdapters.size(); ++slot)
11640 mNetworkAdapters[slot]->uninit();
11641 for (size_t slot = newSize; slot < mPeer->mNetworkAdapters.size(); ++slot)
11642 {
11643 if (mPeer->mNetworkAdapters[slot])
11644 mPeer->mNetworkAdapters[slot]->uninit();
11645 }
11646 /* Keep the original network adapter count until this point, so that
11647 * discarding a chipset type change will not lose settings. */
11648 mNetworkAdapters.resize(newSize);
11649 mPeer->mNetworkAdapters.resize(newSize);
11650 }
11651 else
11652 {
11653 /* we have no peer (our parent is the newly created machine);
11654 * just commit changes to the network adapters */
11655 commitNetworkAdapters = true;
11656 }
11657 if (commitNetworkAdapters)
11658 for (size_t slot = 0; slot < mNetworkAdapters.size(); ++slot)
11659 mNetworkAdapters[slot]->i_commit();
11660
11661 for (ULONG slot = 0; slot < RT_ELEMENTS(mSerialPorts); ++slot)
11662 mSerialPorts[slot]->i_commit();
11663 for (ULONG slot = 0; slot < RT_ELEMENTS(mParallelPorts); ++slot)
11664 mParallelPorts[slot]->i_commit();
11665
11666 bool commitStorageControllers = false;
11667
11668 if (mStorageControllers.isBackedUp())
11669 {
11670 mStorageControllers.commit();
11671
11672 if (mPeer)
11673 {
11674 /* Commit all changes to new controllers (this will reshare data with
11675 * peers for those who have peers) */
11676 StorageControllerList *newList = new StorageControllerList();
11677 StorageControllerList::const_iterator it = mStorageControllers->begin();
11678 while (it != mStorageControllers->end())
11679 {
11680 (*it)->i_commit();
11681
11682 /* look if this controller has a peer device */
11683 ComObjPtr<StorageController> peer = (*it)->i_getPeer();
11684 if (!peer)
11685 {
11686 /* no peer means the device is a newly created one;
11687 * create a peer owning data this device share it with */
11688 peer.createObject();
11689 peer->init(mPeer, *it, true /* aReshare */);
11690 }
11691 else
11692 {
11693 /* remove peer from the old list */
11694 mPeer->mStorageControllers->remove(peer);
11695 }
11696 /* and add it to the new list */
11697 newList->push_back(peer);
11698
11699 ++it;
11700 }
11701
11702 /* uninit old peer's controllers that are left */
11703 it = mPeer->mStorageControllers->begin();
11704 while (it != mPeer->mStorageControllers->end())
11705 {
11706 (*it)->uninit();
11707 ++it;
11708 }
11709
11710 /* attach new list of controllers to our peer */
11711 mPeer->mStorageControllers.attach(newList);
11712 }
11713 else
11714 {
11715 /* we have no peer (our parent is the newly created machine);
11716 * just commit changes to devices */
11717 commitStorageControllers = true;
11718 }
11719 }
11720 else
11721 {
11722 /* the list of controllers itself is not changed,
11723 * just commit changes to controllers themselves */
11724 commitStorageControllers = true;
11725 }
11726
11727 if (commitStorageControllers)
11728 {
11729 StorageControllerList::const_iterator it = mStorageControllers->begin();
11730 while (it != mStorageControllers->end())
11731 {
11732 (*it)->i_commit();
11733 ++it;
11734 }
11735 }
11736
11737 bool commitUSBControllers = false;
11738
11739 if (mUSBControllers.isBackedUp())
11740 {
11741 mUSBControllers.commit();
11742
11743 if (mPeer)
11744 {
11745 /* Commit all changes to new controllers (this will reshare data with
11746 * peers for those who have peers) */
11747 USBControllerList *newList = new USBControllerList();
11748 USBControllerList::const_iterator it = mUSBControllers->begin();
11749 while (it != mUSBControllers->end())
11750 {
11751 (*it)->i_commit();
11752
11753 /* look if this controller has a peer device */
11754 ComObjPtr<USBController> peer = (*it)->i_getPeer();
11755 if (!peer)
11756 {
11757 /* no peer means the device is a newly created one;
11758 * create a peer owning data this device share it with */
11759 peer.createObject();
11760 peer->init(mPeer, *it, true /* aReshare */);
11761 }
11762 else
11763 {
11764 /* remove peer from the old list */
11765 mPeer->mUSBControllers->remove(peer);
11766 }
11767 /* and add it to the new list */
11768 newList->push_back(peer);
11769
11770 ++it;
11771 }
11772
11773 /* uninit old peer's controllers that are left */
11774 it = mPeer->mUSBControllers->begin();
11775 while (it != mPeer->mUSBControllers->end())
11776 {
11777 (*it)->uninit();
11778 ++it;
11779 }
11780
11781 /* attach new list of controllers to our peer */
11782 mPeer->mUSBControllers.attach(newList);
11783 }
11784 else
11785 {
11786 /* we have no peer (our parent is the newly created machine);
11787 * just commit changes to devices */
11788 commitUSBControllers = true;
11789 }
11790 }
11791 else
11792 {
11793 /* the list of controllers itself is not changed,
11794 * just commit changes to controllers themselves */
11795 commitUSBControllers = true;
11796 }
11797
11798 if (commitUSBControllers)
11799 {
11800 USBControllerList::const_iterator it = mUSBControllers->begin();
11801 while (it != mUSBControllers->end())
11802 {
11803 (*it)->i_commit();
11804 ++it;
11805 }
11806 }
11807
11808 if (i_isSessionMachine())
11809 {
11810 /* attach new data to the primary machine and reshare it */
11811 mPeer->mUserData.attach(mUserData);
11812 mPeer->mHWData.attach(mHWData);
11813 /* mMediaData is reshared by fixupMedia */
11814 // mPeer->mMediaData.attach(mMediaData);
11815 Assert(mPeer->mMediaData.data() == mMediaData.data());
11816 }
11817}
11818
11819/**
11820 * Copies all the hardware data from the given machine.
11821 *
11822 * Currently, only called when the VM is being restored from a snapshot. In
11823 * particular, this implies that the VM is not running during this method's
11824 * call.
11825 *
11826 * @note This method must be called from under this object's lock.
11827 *
11828 * @note This method doesn't call #commit(), so all data remains backed up and
11829 * unsaved.
11830 */
11831void Machine::i_copyFrom(Machine *aThat)
11832{
11833 AssertReturnVoid(!i_isSnapshotMachine());
11834 AssertReturnVoid(aThat->i_isSnapshotMachine());
11835
11836 AssertReturnVoid(!Global::IsOnline(mData->mMachineState));
11837
11838 mHWData.assignCopy(aThat->mHWData);
11839
11840 // create copies of all shared folders (mHWData after attaching a copy
11841 // contains just references to original objects)
11842 for (HWData::SharedFolderList::iterator it = mHWData->mSharedFolders.begin();
11843 it != mHWData->mSharedFolders.end();
11844 ++it)
11845 {
11846 ComObjPtr<SharedFolder> folder;
11847 folder.createObject();
11848 HRESULT rc = folder->initCopy(i_getMachine(), *it);
11849 AssertComRC(rc);
11850 *it = folder;
11851 }
11852
11853 mBIOSSettings->i_copyFrom(aThat->mBIOSSettings);
11854 mVRDEServer->i_copyFrom(aThat->mVRDEServer);
11855 mAudioAdapter->i_copyFrom(aThat->mAudioAdapter);
11856 mUSBDeviceFilters->i_copyFrom(aThat->mUSBDeviceFilters);
11857 mBandwidthControl->i_copyFrom(aThat->mBandwidthControl);
11858
11859 /* create private copies of all controllers */
11860 mStorageControllers.backup();
11861 mStorageControllers->clear();
11862 for (StorageControllerList::iterator it = aThat->mStorageControllers->begin();
11863 it != aThat->mStorageControllers->end();
11864 ++it)
11865 {
11866 ComObjPtr<StorageController> ctrl;
11867 ctrl.createObject();
11868 ctrl->initCopy(this, *it);
11869 mStorageControllers->push_back(ctrl);
11870 }
11871
11872 /* create private copies of all USB controllers */
11873 mUSBControllers.backup();
11874 mUSBControllers->clear();
11875 for (USBControllerList::iterator it = aThat->mUSBControllers->begin();
11876 it != aThat->mUSBControllers->end();
11877 ++it)
11878 {
11879 ComObjPtr<USBController> ctrl;
11880 ctrl.createObject();
11881 ctrl->initCopy(this, *it);
11882 mUSBControllers->push_back(ctrl);
11883 }
11884
11885 mNetworkAdapters.resize(aThat->mNetworkAdapters.size());
11886 for (ULONG slot = 0; slot < mNetworkAdapters.size(); ++slot)
11887 mNetworkAdapters[slot]->i_copyFrom(aThat->mNetworkAdapters[slot]);
11888 for (ULONG slot = 0; slot < RT_ELEMENTS(mSerialPorts); ++slot)
11889 mSerialPorts[slot]->i_copyFrom(aThat->mSerialPorts[slot]);
11890 for (ULONG slot = 0; slot < RT_ELEMENTS(mParallelPorts); ++slot)
11891 mParallelPorts[slot]->i_copyFrom(aThat->mParallelPorts[slot]);
11892}
11893
11894/**
11895 * Returns whether the given storage controller is hotplug capable.
11896 *
11897 * @returns true if the controller supports hotplugging
11898 * false otherwise.
11899 * @param enmCtrlType The controller type to check for.
11900 */
11901bool Machine::i_isControllerHotplugCapable(StorageControllerType_T enmCtrlType)
11902{
11903 ComPtr<ISystemProperties> systemProperties;
11904 HRESULT rc = mParent->COMGETTER(SystemProperties)(systemProperties.asOutParam());
11905 if (FAILED(rc))
11906 return false;
11907
11908 BOOL aHotplugCapable = FALSE;
11909 systemProperties->GetStorageControllerHotplugCapable(enmCtrlType, &aHotplugCapable);
11910
11911 return RT_BOOL(aHotplugCapable);
11912}
11913
11914#ifdef VBOX_WITH_RESOURCE_USAGE_API
11915
11916void Machine::i_getDiskList(MediaList &list)
11917{
11918 for (MediaData::AttachmentList::const_iterator it = mMediaData->mAttachments.begin();
11919 it != mMediaData->mAttachments.end();
11920 ++it)
11921 {
11922 MediumAttachment* pAttach = *it;
11923 /* just in case */
11924 AssertStmt(pAttach, continue);
11925
11926 AutoCaller localAutoCallerA(pAttach);
11927 if (FAILED(localAutoCallerA.rc())) continue;
11928
11929 AutoReadLock local_alockA(pAttach COMMA_LOCKVAL_SRC_POS);
11930
11931 if (pAttach->i_getType() == DeviceType_HardDisk)
11932 list.push_back(pAttach->i_getMedium());
11933 }
11934}
11935
11936void Machine::i_registerMetrics(PerformanceCollector *aCollector, Machine *aMachine, RTPROCESS pid)
11937{
11938 AssertReturnVoid(isWriteLockOnCurrentThread());
11939 AssertPtrReturnVoid(aCollector);
11940
11941 pm::CollectorHAL *hal = aCollector->getHAL();
11942 /* Create sub metrics */
11943 pm::SubMetric *cpuLoadUser = new pm::SubMetric("CPU/Load/User",
11944 "Percentage of processor time spent in user mode by the VM process.");
11945 pm::SubMetric *cpuLoadKernel = new pm::SubMetric("CPU/Load/Kernel",
11946 "Percentage of processor time spent in kernel mode by the VM process.");
11947 pm::SubMetric *ramUsageUsed = new pm::SubMetric("RAM/Usage/Used",
11948 "Size of resident portion of VM process in memory.");
11949 pm::SubMetric *diskUsageUsed = new pm::SubMetric("Disk/Usage/Used",
11950 "Actual size of all VM disks combined.");
11951 pm::SubMetric *machineNetRx = new pm::SubMetric("Net/Rate/Rx",
11952 "Network receive rate.");
11953 pm::SubMetric *machineNetTx = new pm::SubMetric("Net/Rate/Tx",
11954 "Network transmit rate.");
11955 /* Create and register base metrics */
11956 pm::BaseMetric *cpuLoad = new pm::MachineCpuLoadRaw(hal, aMachine, pid,
11957 cpuLoadUser, cpuLoadKernel);
11958 aCollector->registerBaseMetric(cpuLoad);
11959 pm::BaseMetric *ramUsage = new pm::MachineRamUsage(hal, aMachine, pid,
11960 ramUsageUsed);
11961 aCollector->registerBaseMetric(ramUsage);
11962 MediaList disks;
11963 i_getDiskList(disks);
11964 pm::BaseMetric *diskUsage = new pm::MachineDiskUsage(hal, aMachine, disks,
11965 diskUsageUsed);
11966 aCollector->registerBaseMetric(diskUsage);
11967
11968 aCollector->registerMetric(new pm::Metric(cpuLoad, cpuLoadUser, 0));
11969 aCollector->registerMetric(new pm::Metric(cpuLoad, cpuLoadUser,
11970 new pm::AggregateAvg()));
11971 aCollector->registerMetric(new pm::Metric(cpuLoad, cpuLoadUser,
11972 new pm::AggregateMin()));
11973 aCollector->registerMetric(new pm::Metric(cpuLoad, cpuLoadUser,
11974 new pm::AggregateMax()));
11975 aCollector->registerMetric(new pm::Metric(cpuLoad, cpuLoadKernel, 0));
11976 aCollector->registerMetric(new pm::Metric(cpuLoad, cpuLoadKernel,
11977 new pm::AggregateAvg()));
11978 aCollector->registerMetric(new pm::Metric(cpuLoad, cpuLoadKernel,
11979 new pm::AggregateMin()));
11980 aCollector->registerMetric(new pm::Metric(cpuLoad, cpuLoadKernel,
11981 new pm::AggregateMax()));
11982
11983 aCollector->registerMetric(new pm::Metric(ramUsage, ramUsageUsed, 0));
11984 aCollector->registerMetric(new pm::Metric(ramUsage, ramUsageUsed,
11985 new pm::AggregateAvg()));
11986 aCollector->registerMetric(new pm::Metric(ramUsage, ramUsageUsed,
11987 new pm::AggregateMin()));
11988 aCollector->registerMetric(new pm::Metric(ramUsage, ramUsageUsed,
11989 new pm::AggregateMax()));
11990
11991 aCollector->registerMetric(new pm::Metric(diskUsage, diskUsageUsed, 0));
11992 aCollector->registerMetric(new pm::Metric(diskUsage, diskUsageUsed,
11993 new pm::AggregateAvg()));
11994 aCollector->registerMetric(new pm::Metric(diskUsage, diskUsageUsed,
11995 new pm::AggregateMin()));
11996 aCollector->registerMetric(new pm::Metric(diskUsage, diskUsageUsed,
11997 new pm::AggregateMax()));
11998
11999
12000 /* Guest metrics collector */
12001 mCollectorGuest = new pm::CollectorGuest(aMachine, pid);
12002 aCollector->registerGuest(mCollectorGuest);
12003 LogAleksey(("{%p} " LOG_FN_FMT ": mCollectorGuest=%p\n",
12004 this, __PRETTY_FUNCTION__, mCollectorGuest));
12005
12006 /* Create sub metrics */
12007 pm::SubMetric *guestLoadUser = new pm::SubMetric("Guest/CPU/Load/User",
12008 "Percentage of processor time spent in user mode as seen by the guest.");
12009 pm::SubMetric *guestLoadKernel = new pm::SubMetric("Guest/CPU/Load/Kernel",
12010 "Percentage of processor time spent in kernel mode as seen by the guest.");
12011 pm::SubMetric *guestLoadIdle = new pm::SubMetric("Guest/CPU/Load/Idle",
12012 "Percentage of processor time spent idling as seen by the guest.");
12013
12014 /* The total amount of physical ram is fixed now, but we'll support dynamic guest ram configurations in the future. */
12015 pm::SubMetric *guestMemTotal = new pm::SubMetric("Guest/RAM/Usage/Total", "Total amount of physical guest RAM.");
12016 pm::SubMetric *guestMemFree = new pm::SubMetric("Guest/RAM/Usage/Free", "Free amount of physical guest RAM.");
12017 pm::SubMetric *guestMemBalloon = new pm::SubMetric("Guest/RAM/Usage/Balloon", "Amount of ballooned physical guest RAM.");
12018 pm::SubMetric *guestMemShared = new pm::SubMetric("Guest/RAM/Usage/Shared", "Amount of shared physical guest RAM.");
12019 pm::SubMetric *guestMemCache = new pm::SubMetric(
12020 "Guest/RAM/Usage/Cache", "Total amount of guest (disk) cache memory.");
12021
12022 pm::SubMetric *guestPagedTotal = new pm::SubMetric(
12023 "Guest/Pagefile/Usage/Total", "Total amount of space in the page file.");
12024
12025 /* Create and register base metrics */
12026 pm::BaseMetric *machineNetRate = new pm::MachineNetRate(mCollectorGuest, aMachine,
12027 machineNetRx, machineNetTx);
12028 aCollector->registerBaseMetric(machineNetRate);
12029
12030 pm::BaseMetric *guestCpuLoad = new pm::GuestCpuLoad(mCollectorGuest, aMachine,
12031 guestLoadUser, guestLoadKernel, guestLoadIdle);
12032 aCollector->registerBaseMetric(guestCpuLoad);
12033
12034 pm::BaseMetric *guestCpuMem = new pm::GuestRamUsage(mCollectorGuest, aMachine,
12035 guestMemTotal, guestMemFree,
12036 guestMemBalloon, guestMemShared,
12037 guestMemCache, guestPagedTotal);
12038 aCollector->registerBaseMetric(guestCpuMem);
12039
12040 aCollector->registerMetric(new pm::Metric(machineNetRate, machineNetRx, 0));
12041 aCollector->registerMetric(new pm::Metric(machineNetRate, machineNetRx, new pm::AggregateAvg()));
12042 aCollector->registerMetric(new pm::Metric(machineNetRate, machineNetRx, new pm::AggregateMin()));
12043 aCollector->registerMetric(new pm::Metric(machineNetRate, machineNetRx, new pm::AggregateMax()));
12044
12045 aCollector->registerMetric(new pm::Metric(machineNetRate, machineNetTx, 0));
12046 aCollector->registerMetric(new pm::Metric(machineNetRate, machineNetTx, new pm::AggregateAvg()));
12047 aCollector->registerMetric(new pm::Metric(machineNetRate, machineNetTx, new pm::AggregateMin()));
12048 aCollector->registerMetric(new pm::Metric(machineNetRate, machineNetTx, new pm::AggregateMax()));
12049
12050 aCollector->registerMetric(new pm::Metric(guestCpuLoad, guestLoadUser, 0));
12051 aCollector->registerMetric(new pm::Metric(guestCpuLoad, guestLoadUser, new pm::AggregateAvg()));
12052 aCollector->registerMetric(new pm::Metric(guestCpuLoad, guestLoadUser, new pm::AggregateMin()));
12053 aCollector->registerMetric(new pm::Metric(guestCpuLoad, guestLoadUser, new pm::AggregateMax()));
12054
12055 aCollector->registerMetric(new pm::Metric(guestCpuLoad, guestLoadKernel, 0));
12056 aCollector->registerMetric(new pm::Metric(guestCpuLoad, guestLoadKernel, new pm::AggregateAvg()));
12057 aCollector->registerMetric(new pm::Metric(guestCpuLoad, guestLoadKernel, new pm::AggregateMin()));
12058 aCollector->registerMetric(new pm::Metric(guestCpuLoad, guestLoadKernel, new pm::AggregateMax()));
12059
12060 aCollector->registerMetric(new pm::Metric(guestCpuLoad, guestLoadIdle, 0));
12061 aCollector->registerMetric(new pm::Metric(guestCpuLoad, guestLoadIdle, new pm::AggregateAvg()));
12062 aCollector->registerMetric(new pm::Metric(guestCpuLoad, guestLoadIdle, new pm::AggregateMin()));
12063 aCollector->registerMetric(new pm::Metric(guestCpuLoad, guestLoadIdle, new pm::AggregateMax()));
12064
12065 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemTotal, 0));
12066 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemTotal, new pm::AggregateAvg()));
12067 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemTotal, new pm::AggregateMin()));
12068 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemTotal, new pm::AggregateMax()));
12069
12070 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemFree, 0));
12071 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemFree, new pm::AggregateAvg()));
12072 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemFree, new pm::AggregateMin()));
12073 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemFree, new pm::AggregateMax()));
12074
12075 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemBalloon, 0));
12076 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemBalloon, new pm::AggregateAvg()));
12077 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemBalloon, new pm::AggregateMin()));
12078 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemBalloon, new pm::AggregateMax()));
12079
12080 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemShared, 0));
12081 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemShared, new pm::AggregateAvg()));
12082 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemShared, new pm::AggregateMin()));
12083 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemShared, new pm::AggregateMax()));
12084
12085 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemCache, 0));
12086 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemCache, new pm::AggregateAvg()));
12087 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemCache, new pm::AggregateMin()));
12088 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemCache, new pm::AggregateMax()));
12089
12090 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestPagedTotal, 0));
12091 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestPagedTotal, new pm::AggregateAvg()));
12092 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestPagedTotal, new pm::AggregateMin()));
12093 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestPagedTotal, new pm::AggregateMax()));
12094}
12095
12096void Machine::i_unregisterMetrics(PerformanceCollector *aCollector, Machine *aMachine)
12097{
12098 AssertReturnVoid(isWriteLockOnCurrentThread());
12099
12100 if (aCollector)
12101 {
12102 aCollector->unregisterMetricsFor(aMachine);
12103 aCollector->unregisterBaseMetricsFor(aMachine);
12104 }
12105}
12106
12107#endif /* VBOX_WITH_RESOURCE_USAGE_API */
12108
12109
12110////////////////////////////////////////////////////////////////////////////////
12111
12112DEFINE_EMPTY_CTOR_DTOR(SessionMachine)
12113
12114HRESULT SessionMachine::FinalConstruct()
12115{
12116 LogFlowThisFunc(("\n"));
12117
12118 mClientToken = NULL;
12119
12120 return BaseFinalConstruct();
12121}
12122
12123void SessionMachine::FinalRelease()
12124{
12125 LogFlowThisFunc(("\n"));
12126
12127 Assert(!mClientToken);
12128 /* paranoia, should not hang around any more */
12129 if (mClientToken)
12130 {
12131 delete mClientToken;
12132 mClientToken = NULL;
12133 }
12134
12135 uninit(Uninit::Unexpected);
12136
12137 BaseFinalRelease();
12138}
12139
12140/**
12141 * @note Must be called only by Machine::LockMachine() from its own write lock.
12142 */
12143HRESULT SessionMachine::init(Machine *aMachine)
12144{
12145 LogFlowThisFuncEnter();
12146 LogFlowThisFunc(("mName={%s}\n", aMachine->mUserData->s.strName.c_str()));
12147
12148 AssertReturn(aMachine, E_INVALIDARG);
12149
12150 AssertReturn(aMachine->lockHandle()->isWriteLockOnCurrentThread(), E_FAIL);
12151
12152 /* Enclose the state transition NotReady->InInit->Ready */
12153 AutoInitSpan autoInitSpan(this);
12154 AssertReturn(autoInitSpan.isOk(), E_FAIL);
12155
12156 HRESULT rc = S_OK;
12157
12158 /* create the machine client token */
12159 try
12160 {
12161 mClientToken = new ClientToken(aMachine, this);
12162 if (!mClientToken->isReady())
12163 {
12164 delete mClientToken;
12165 mClientToken = NULL;
12166 rc = E_FAIL;
12167 }
12168 }
12169 catch (std::bad_alloc &)
12170 {
12171 rc = E_OUTOFMEMORY;
12172 }
12173 if (FAILED(rc))
12174 return rc;
12175
12176 /* memorize the peer Machine */
12177 unconst(mPeer) = aMachine;
12178 /* share the parent pointer */
12179 unconst(mParent) = aMachine->mParent;
12180
12181 /* take the pointers to data to share */
12182 mData.share(aMachine->mData);
12183 mSSData.share(aMachine->mSSData);
12184
12185 mUserData.share(aMachine->mUserData);
12186 mHWData.share(aMachine->mHWData);
12187 mMediaData.share(aMachine->mMediaData);
12188
12189 mStorageControllers.allocate();
12190 for (StorageControllerList::const_iterator it = aMachine->mStorageControllers->begin();
12191 it != aMachine->mStorageControllers->end();
12192 ++it)
12193 {
12194 ComObjPtr<StorageController> ctl;
12195 ctl.createObject();
12196 ctl->init(this, *it);
12197 mStorageControllers->push_back(ctl);
12198 }
12199
12200 mUSBControllers.allocate();
12201 for (USBControllerList::const_iterator it = aMachine->mUSBControllers->begin();
12202 it != aMachine->mUSBControllers->end();
12203 ++it)
12204 {
12205 ComObjPtr<USBController> ctl;
12206 ctl.createObject();
12207 ctl->init(this, *it);
12208 mUSBControllers->push_back(ctl);
12209 }
12210
12211 unconst(mBIOSSettings).createObject();
12212 mBIOSSettings->init(this, aMachine->mBIOSSettings);
12213 /* create another VRDEServer object that will be mutable */
12214 unconst(mVRDEServer).createObject();
12215 mVRDEServer->init(this, aMachine->mVRDEServer);
12216 /* create another audio adapter object that will be mutable */
12217 unconst(mAudioAdapter).createObject();
12218 mAudioAdapter->init(this, aMachine->mAudioAdapter);
12219 /* create a list of serial ports that will be mutable */
12220 for (ULONG slot = 0; slot < RT_ELEMENTS(mSerialPorts); ++slot)
12221 {
12222 unconst(mSerialPorts[slot]).createObject();
12223 mSerialPorts[slot]->init(this, aMachine->mSerialPorts[slot]);
12224 }
12225 /* create a list of parallel ports that will be mutable */
12226 for (ULONG slot = 0; slot < RT_ELEMENTS(mParallelPorts); ++slot)
12227 {
12228 unconst(mParallelPorts[slot]).createObject();
12229 mParallelPorts[slot]->init(this, aMachine->mParallelPorts[slot]);
12230 }
12231
12232 /* create another USB device filters object that will be mutable */
12233 unconst(mUSBDeviceFilters).createObject();
12234 mUSBDeviceFilters->init(this, aMachine->mUSBDeviceFilters);
12235
12236 /* create a list of network adapters that will be mutable */
12237 mNetworkAdapters.resize(aMachine->mNetworkAdapters.size());
12238 for (ULONG slot = 0; slot < mNetworkAdapters.size(); ++slot)
12239 {
12240 unconst(mNetworkAdapters[slot]).createObject();
12241 mNetworkAdapters[slot]->init(this, aMachine->mNetworkAdapters[slot]);
12242 }
12243
12244 /* create another bandwidth control object that will be mutable */
12245 unconst(mBandwidthControl).createObject();
12246 mBandwidthControl->init(this, aMachine->mBandwidthControl);
12247
12248 /* default is to delete saved state on Saved -> PoweredOff transition */
12249 mRemoveSavedState = true;
12250
12251 /* Confirm a successful initialization when it's the case */
12252 autoInitSpan.setSucceeded();
12253
12254 miNATNetworksStarted = 0;
12255
12256 LogFlowThisFuncLeave();
12257 return rc;
12258}
12259
12260/**
12261 * Uninitializes this session object. If the reason is other than
12262 * Uninit::Unexpected, then this method MUST be called from #checkForDeath()
12263 * or the client watcher code.
12264 *
12265 * @param aReason uninitialization reason
12266 *
12267 * @note Locks mParent + this object for writing.
12268 */
12269void SessionMachine::uninit(Uninit::Reason aReason)
12270{
12271 LogFlowThisFuncEnter();
12272 LogFlowThisFunc(("reason=%d\n", aReason));
12273
12274 /*
12275 * Strongly reference ourselves to prevent this object deletion after
12276 * mData->mSession.mMachine.setNull() below (which can release the last
12277 * reference and call the destructor). Important: this must be done before
12278 * accessing any members (and before AutoUninitSpan that does it as well).
12279 * This self reference will be released as the very last step on return.
12280 */
12281 ComObjPtr<SessionMachine> selfRef = this;
12282
12283 /* Enclose the state transition Ready->InUninit->NotReady */
12284 AutoUninitSpan autoUninitSpan(this);
12285 if (autoUninitSpan.uninitDone())
12286 {
12287 LogFlowThisFunc(("Already uninitialized\n"));
12288 LogFlowThisFuncLeave();
12289 return;
12290 }
12291
12292 if (autoUninitSpan.initFailed())
12293 {
12294 /* We've been called by init() because it's failed. It's not really
12295 * necessary (nor it's safe) to perform the regular uninit sequence
12296 * below, the following is enough.
12297 */
12298 LogFlowThisFunc(("Initialization failed.\n"));
12299 /* destroy the machine client token */
12300 if (mClientToken)
12301 {
12302 delete mClientToken;
12303 mClientToken = NULL;
12304 }
12305 uninitDataAndChildObjects();
12306 mData.free();
12307 unconst(mParent) = NULL;
12308 unconst(mPeer) = NULL;
12309 LogFlowThisFuncLeave();
12310 return;
12311 }
12312
12313 MachineState_T lastState;
12314 {
12315 AutoReadLock tempLock(this COMMA_LOCKVAL_SRC_POS);
12316 lastState = mData->mMachineState;
12317 }
12318 NOREF(lastState);
12319
12320#ifdef VBOX_WITH_USB
12321 // release all captured USB devices, but do this before requesting the locks below
12322 if (aReason == Uninit::Abnormal && Global::IsOnline(lastState))
12323 {
12324 /* Console::captureUSBDevices() is called in the VM process only after
12325 * setting the machine state to Starting or Restoring.
12326 * Console::detachAllUSBDevices() will be called upon successful
12327 * termination. So, we need to release USB devices only if there was
12328 * an abnormal termination of a running VM.
12329 *
12330 * This is identical to SessionMachine::DetachAllUSBDevices except
12331 * for the aAbnormal argument. */
12332 HRESULT rc = mUSBDeviceFilters->i_notifyProxy(false /* aInsertFilters */);
12333 AssertComRC(rc);
12334 NOREF(rc);
12335
12336 USBProxyService *service = mParent->i_host()->i_usbProxyService();
12337 if (service)
12338 service->detachAllDevicesFromVM(this, true /* aDone */, true /* aAbnormal */);
12339 }
12340#endif /* VBOX_WITH_USB */
12341
12342 // we need to lock this object in uninit() because the lock is shared
12343 // with mPeer (as well as data we modify below). mParent lock is needed
12344 // by several calls to it, and USB needs host lock.
12345 AutoMultiWriteLock3 multilock(mParent, mParent->i_host(), this COMMA_LOCKVAL_SRC_POS);
12346
12347#ifdef VBOX_WITH_RESOURCE_USAGE_API
12348 /*
12349 * It is safe to call Machine::i_unregisterMetrics() here because
12350 * PerformanceCollector::samplerCallback no longer accesses guest methods
12351 * holding the lock.
12352 */
12353 i_unregisterMetrics(mParent->i_performanceCollector(), mPeer);
12354 /* The guest must be unregistered after its metrics (@bugref{5949}). */
12355 LogAleksey(("{%p} " LOG_FN_FMT ": mCollectorGuest=%p\n",
12356 this, __PRETTY_FUNCTION__, mCollectorGuest));
12357 if (mCollectorGuest)
12358 {
12359 mParent->i_performanceCollector()->unregisterGuest(mCollectorGuest);
12360 // delete mCollectorGuest; => CollectorGuestManager::destroyUnregistered()
12361 mCollectorGuest = NULL;
12362 }
12363#endif
12364
12365 if (aReason == Uninit::Abnormal)
12366 {
12367 LogWarningThisFunc(("ABNORMAL client termination! (wasBusy=%d)\n",
12368 Global::IsOnlineOrTransient(lastState)));
12369
12370 /* reset the state to Aborted */
12371 if (mData->mMachineState != MachineState_Aborted)
12372 i_setMachineState(MachineState_Aborted);
12373 }
12374
12375 // any machine settings modified?
12376 if (mData->flModifications)
12377 {
12378 LogWarningThisFunc(("Discarding unsaved settings changes!\n"));
12379 i_rollback(false /* aNotify */);
12380 }
12381
12382 Assert( mConsoleTaskData.strStateFilePath.isEmpty()
12383 || !mConsoleTaskData.mSnapshot);
12384 if (!mConsoleTaskData.strStateFilePath.isEmpty())
12385 {
12386 LogWarningThisFunc(("canceling failed save state request!\n"));
12387 endSavingState(E_FAIL, tr("Machine terminated with pending save state!"));
12388 }
12389 else if (!mConsoleTaskData.mSnapshot.isNull())
12390 {
12391 LogWarningThisFunc(("canceling untaken snapshot!\n"));
12392
12393 /* delete all differencing hard disks created (this will also attach
12394 * their parents back by rolling back mMediaData) */
12395 i_rollbackMedia();
12396
12397 // delete the saved state file (it might have been already created)
12398 // AFTER killing the snapshot so that releaseSavedStateFile() won't
12399 // think it's still in use
12400 Utf8Str strStateFile = mConsoleTaskData.mSnapshot->i_getStateFilePath();
12401 mConsoleTaskData.mSnapshot->uninit();
12402 i_releaseSavedStateFile(strStateFile, NULL /* pSnapshotToIgnore */ );
12403 }
12404
12405 mData->mSession.mPID = NIL_RTPROCESS;
12406
12407 if (aReason == Uninit::Unexpected)
12408 {
12409 /* Uninitialization didn't come from #checkForDeath(), so tell the
12410 * client watcher thread to update the set of machines that have open
12411 * sessions. */
12412 mParent->i_updateClientWatcher();
12413 }
12414
12415 /* uninitialize all remote controls */
12416 if (mData->mSession.mRemoteControls.size())
12417 {
12418 LogFlowThisFunc(("Closing remote sessions (%d):\n",
12419 mData->mSession.mRemoteControls.size()));
12420
12421 Data::Session::RemoteControlList::iterator it =
12422 mData->mSession.mRemoteControls.begin();
12423 while (it != mData->mSession.mRemoteControls.end())
12424 {
12425 LogFlowThisFunc((" Calling remoteControl->Uninitialize()...\n"));
12426 HRESULT rc = (*it)->Uninitialize();
12427 LogFlowThisFunc((" remoteControl->Uninitialize() returned %08X\n", rc));
12428 if (FAILED(rc))
12429 LogWarningThisFunc(("Forgot to close the remote session?\n"));
12430 ++it;
12431 }
12432 mData->mSession.mRemoteControls.clear();
12433 }
12434
12435 /* Remove all references to the NAT network service. The service will stop
12436 * if all references (also from other VMs) are removed. */
12437 for (; miNATNetworksStarted > 0; miNATNetworksStarted--)
12438 {
12439 for (ULONG slot = 0; slot < mNetworkAdapters.size(); ++slot)
12440 {
12441 NetworkAttachmentType_T type;
12442 HRESULT hrc;
12443
12444 hrc = mNetworkAdapters[slot]->COMGETTER(AttachmentType)(&type);
12445 if ( SUCCEEDED(hrc)
12446 && type == NetworkAttachmentType_NATNetwork)
12447 {
12448 Bstr name;
12449 hrc = mNetworkAdapters[slot]->COMGETTER(NATNetwork)(name.asOutParam());
12450 if (SUCCEEDED(hrc))
12451 {
12452 multilock.release();
12453 LogRel(("VM '%s' stops using NAT network '%ls'\n",
12454 mUserData->s.strName.c_str(), name.raw()));
12455 mParent->i_natNetworkRefDec(name.raw());
12456 multilock.acquire();
12457 }
12458 }
12459 }
12460 }
12461
12462 /*
12463 * An expected uninitialization can come only from #checkForDeath().
12464 * Otherwise it means that something's gone really wrong (for example,
12465 * the Session implementation has released the VirtualBox reference
12466 * before it triggered #OnSessionEnd(), or before releasing IPC semaphore,
12467 * etc). However, it's also possible, that the client releases the IPC
12468 * semaphore correctly (i.e. before it releases the VirtualBox reference),
12469 * but the VirtualBox release event comes first to the server process.
12470 * This case is practically possible, so we should not assert on an
12471 * unexpected uninit, just log a warning.
12472 */
12473
12474 if ((aReason == Uninit::Unexpected))
12475 LogWarningThisFunc(("Unexpected SessionMachine uninitialization!\n"));
12476
12477 if (aReason != Uninit::Normal)
12478 {
12479 mData->mSession.mDirectControl.setNull();
12480 }
12481 else
12482 {
12483 /* this must be null here (see #OnSessionEnd()) */
12484 Assert(mData->mSession.mDirectControl.isNull());
12485 Assert(mData->mSession.mState == SessionState_Unlocking);
12486 Assert(!mData->mSession.mProgress.isNull());
12487 }
12488 if (mData->mSession.mProgress)
12489 {
12490 if (aReason == Uninit::Normal)
12491 mData->mSession.mProgress->i_notifyComplete(S_OK);
12492 else
12493 mData->mSession.mProgress->i_notifyComplete(E_FAIL,
12494 COM_IIDOF(ISession),
12495 getComponentName(),
12496 tr("The VM session was aborted"));
12497 mData->mSession.mProgress.setNull();
12498 }
12499
12500 /* remove the association between the peer machine and this session machine */
12501 Assert( (SessionMachine*)mData->mSession.mMachine == this
12502 || aReason == Uninit::Unexpected);
12503
12504 /* reset the rest of session data */
12505 mData->mSession.mMachine.setNull();
12506 mData->mSession.mState = SessionState_Unlocked;
12507 mData->mSession.mType.setNull();
12508
12509 /* destroy the machine client token before leaving the exclusive lock */
12510 if (mClientToken)
12511 {
12512 delete mClientToken;
12513 mClientToken = NULL;
12514 }
12515
12516 /* fire an event */
12517 mParent->i_onSessionStateChange(mData->mUuid, SessionState_Unlocked);
12518
12519 uninitDataAndChildObjects();
12520
12521 /* free the essential data structure last */
12522 mData.free();
12523
12524 /* release the exclusive lock before setting the below two to NULL */
12525 multilock.release();
12526
12527 unconst(mParent) = NULL;
12528 unconst(mPeer) = NULL;
12529
12530 LogFlowThisFuncLeave();
12531}
12532
12533// util::Lockable interface
12534////////////////////////////////////////////////////////////////////////////////
12535
12536/**
12537 * Overrides VirtualBoxBase::lockHandle() in order to share the lock handle
12538 * with the primary Machine instance (mPeer).
12539 */
12540RWLockHandle *SessionMachine::lockHandle() const
12541{
12542 AssertReturn(mPeer != NULL, NULL);
12543 return mPeer->lockHandle();
12544}
12545
12546// IInternalMachineControl methods
12547////////////////////////////////////////////////////////////////////////////////
12548
12549/**
12550 * Passes collected guest statistics to performance collector object
12551 */
12552HRESULT SessionMachine::reportVmStatistics(ULONG aValidStats, ULONG aCpuUser,
12553 ULONG aCpuKernel, ULONG aCpuIdle,
12554 ULONG aMemTotal, ULONG aMemFree,
12555 ULONG aMemBalloon, ULONG aMemShared,
12556 ULONG aMemCache, ULONG aPageTotal,
12557 ULONG aAllocVMM, ULONG aFreeVMM,
12558 ULONG aBalloonedVMM, ULONG aSharedVMM,
12559 ULONG aVmNetRx, ULONG aVmNetTx)
12560{
12561#ifdef VBOX_WITH_RESOURCE_USAGE_API
12562 if (mCollectorGuest)
12563 mCollectorGuest->updateStats(aValidStats, aCpuUser, aCpuKernel, aCpuIdle,
12564 aMemTotal, aMemFree, aMemBalloon, aMemShared,
12565 aMemCache, aPageTotal, aAllocVMM, aFreeVMM,
12566 aBalloonedVMM, aSharedVMM, aVmNetRx, aVmNetTx);
12567
12568 return S_OK;
12569#else
12570 NOREF(aValidStats);
12571 NOREF(aCpuUser);
12572 NOREF(aCpuKernel);
12573 NOREF(aCpuIdle);
12574 NOREF(aMemTotal);
12575 NOREF(aMemFree);
12576 NOREF(aMemBalloon);
12577 NOREF(aMemShared);
12578 NOREF(aMemCache);
12579 NOREF(aPageTotal);
12580 NOREF(aAllocVMM);
12581 NOREF(aFreeVMM);
12582 NOREF(aBalloonedVMM);
12583 NOREF(aSharedVMM);
12584 NOREF(aVmNetRx);
12585 NOREF(aVmNetTx);
12586 return E_NOTIMPL;
12587#endif
12588}
12589
12590/**
12591 * @note Locks this object for writing.
12592 */
12593HRESULT SessionMachine::setRemoveSavedStateFile(BOOL aRemove)
12594{
12595 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
12596
12597 mRemoveSavedState = aRemove;
12598
12599 return S_OK;
12600}
12601
12602/**
12603 * @note Locks the same as #i_setMachineState() does.
12604 */
12605HRESULT SessionMachine::updateState(MachineState_T aState)
12606{
12607 return i_setMachineState(aState);
12608}
12609
12610/**
12611 * @note Locks this object for writing.
12612 */
12613HRESULT SessionMachine::beginPowerUp(const ComPtr<IProgress> &aProgress)
12614{
12615 IProgress* pProgress(aProgress);
12616
12617 LogFlowThisFunc(("aProgress=%p\n", pProgress));
12618
12619 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
12620
12621 if (mData->mSession.mState != SessionState_Locked)
12622 return VBOX_E_INVALID_OBJECT_STATE;
12623
12624 if (!mData->mSession.mProgress.isNull())
12625 mData->mSession.mProgress->setOtherProgressObject(pProgress);
12626
12627 /* If we didn't reference the NAT network service yet, add a reference to
12628 * force a start */
12629 if (miNATNetworksStarted < 1)
12630 {
12631 for (ULONG slot = 0; slot < mNetworkAdapters.size(); ++slot)
12632 {
12633 NetworkAttachmentType_T type;
12634 HRESULT hrc;
12635 hrc = mNetworkAdapters[slot]->COMGETTER(AttachmentType)(&type);
12636 if ( SUCCEEDED(hrc)
12637 && type == NetworkAttachmentType_NATNetwork)
12638 {
12639 Bstr name;
12640 hrc = mNetworkAdapters[slot]->COMGETTER(NATNetwork)(name.asOutParam());
12641 if (SUCCEEDED(hrc))
12642 {
12643 LogRel(("VM '%s' starts using NAT network '%ls'\n",
12644 mUserData->s.strName.c_str(), name.raw()));
12645 mPeer->lockHandle()->unlockWrite();
12646 mParent->i_natNetworkRefInc(name.raw());
12647#ifdef RT_LOCK_STRICT
12648 mPeer->lockHandle()->lockWrite(RT_SRC_POS);
12649#else
12650 mPeer->lockHandle()->lockWrite();
12651#endif
12652 }
12653 }
12654 }
12655 miNATNetworksStarted++;
12656 }
12657
12658 LogFlowThisFunc(("returns S_OK.\n"));
12659 return S_OK;
12660}
12661
12662/**
12663 * @note Locks this object for writing.
12664 */
12665HRESULT SessionMachine::endPowerUp(LONG aResult)
12666{
12667 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
12668
12669 if (mData->mSession.mState != SessionState_Locked)
12670 return VBOX_E_INVALID_OBJECT_STATE;
12671
12672 /* Finalize the LaunchVMProcess progress object. */
12673 if (mData->mSession.mProgress)
12674 {
12675 mData->mSession.mProgress->notifyComplete((HRESULT)aResult);
12676 mData->mSession.mProgress.setNull();
12677 }
12678
12679 if (SUCCEEDED((HRESULT)aResult))
12680 {
12681#ifdef VBOX_WITH_RESOURCE_USAGE_API
12682 /* The VM has been powered up successfully, so it makes sense
12683 * now to offer the performance metrics for a running machine
12684 * object. Doing it earlier wouldn't be safe. */
12685 i_registerMetrics(mParent->i_performanceCollector(), mPeer,
12686 mData->mSession.mPID);
12687#endif /* VBOX_WITH_RESOURCE_USAGE_API */
12688 }
12689
12690 return S_OK;
12691}
12692
12693/**
12694 * @note Locks this object for writing.
12695 */
12696HRESULT SessionMachine::beginPoweringDown(ComPtr<IProgress> &aProgress)
12697{
12698 LogFlowThisFuncEnter();
12699
12700 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
12701
12702 AssertReturn(mConsoleTaskData.mLastState == MachineState_Null,
12703 E_FAIL);
12704
12705 /* create a progress object to track operation completion */
12706 ComObjPtr<Progress> pProgress;
12707 pProgress.createObject();
12708 pProgress->init(i_getVirtualBox(),
12709 static_cast<IMachine *>(this) /* aInitiator */,
12710 Bstr(tr("Stopping the virtual machine")).raw(),
12711 FALSE /* aCancelable */);
12712
12713 /* fill in the console task data */
12714 mConsoleTaskData.mLastState = mData->mMachineState;
12715 mConsoleTaskData.mProgress = pProgress;
12716
12717 /* set the state to Stopping (this is expected by Console::PowerDown()) */
12718 i_setMachineState(MachineState_Stopping);
12719
12720 pProgress.queryInterfaceTo(aProgress.asOutParam());
12721
12722 return S_OK;
12723}
12724
12725/**
12726 * @note Locks this object for writing.
12727 */
12728HRESULT SessionMachine::endPoweringDown(LONG aResult,
12729 const com::Utf8Str &aErrMsg)
12730{
12731 LogFlowThisFuncEnter();
12732
12733 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
12734
12735 AssertReturn( ( (SUCCEEDED(aResult) && mData->mMachineState == MachineState_PoweredOff)
12736 || (FAILED(aResult) && mData->mMachineState == MachineState_Stopping))
12737 && mConsoleTaskData.mLastState != MachineState_Null,
12738 E_FAIL);
12739
12740 /*
12741 * On failure, set the state to the state we had when BeginPoweringDown()
12742 * was called (this is expected by Console::PowerDown() and the associated
12743 * task). On success the VM process already changed the state to
12744 * MachineState_PoweredOff, so no need to do anything.
12745 */
12746 if (FAILED(aResult))
12747 i_setMachineState(mConsoleTaskData.mLastState);
12748
12749 /* notify the progress object about operation completion */
12750 Assert(mConsoleTaskData.mProgress);
12751 if (SUCCEEDED(aResult))
12752 mConsoleTaskData.mProgress->i_notifyComplete(S_OK);
12753 else
12754 {
12755 if (aErrMsg.length())
12756 mConsoleTaskData.mProgress->i_notifyComplete(aResult,
12757 COM_IIDOF(ISession),
12758 getComponentName(),
12759 aErrMsg.c_str());
12760 else
12761 mConsoleTaskData.mProgress->i_notifyComplete(aResult);
12762 }
12763
12764 /* clear out the temporary saved state data */
12765 mConsoleTaskData.mLastState = MachineState_Null;
12766 mConsoleTaskData.mProgress.setNull();
12767
12768 LogFlowThisFuncLeave();
12769 return S_OK;
12770}
12771
12772
12773/**
12774 * Goes through the USB filters of the given machine to see if the given
12775 * device matches any filter or not.
12776 *
12777 * @note Locks the same as USBController::hasMatchingFilter() does.
12778 */
12779HRESULT SessionMachine::runUSBDeviceFilters(const ComPtr<IUSBDevice> &aDevice,
12780 BOOL *aMatched,
12781 ULONG *aMaskedInterfaces)
12782{
12783 LogFlowThisFunc(("\n"));
12784
12785#ifdef VBOX_WITH_USB
12786 *aMatched = mUSBDeviceFilters->i_hasMatchingFilter(aDevice, aMaskedInterfaces);
12787#else
12788 NOREF(aDevice);
12789 NOREF(aMaskedInterfaces);
12790 *aMatched = FALSE;
12791#endif
12792
12793 return S_OK;
12794}
12795
12796/**
12797 * @note Locks the same as Host::captureUSBDevice() does.
12798 */
12799HRESULT SessionMachine::captureUSBDevice(const com::Guid &aId)
12800{
12801 LogFlowThisFunc(("\n"));
12802
12803#ifdef VBOX_WITH_USB
12804 /* if captureDeviceForVM() fails, it must have set extended error info */
12805 clearError();
12806 MultiResult rc = mParent->i_host()->i_checkUSBProxyService();
12807 if (FAILED(rc)) return rc;
12808
12809 USBProxyService *service = mParent->i_host()->i_usbProxyService();
12810 AssertReturn(service, E_FAIL);
12811 return service->captureDeviceForVM(this, aId.ref());
12812#else
12813 NOREF(aId);
12814 return E_NOTIMPL;
12815#endif
12816}
12817
12818/**
12819 * @note Locks the same as Host::detachUSBDevice() does.
12820 */
12821HRESULT SessionMachine::detachUSBDevice(const com::Guid &aId,
12822 BOOL aDone)
12823{
12824 LogFlowThisFunc(("\n"));
12825
12826#ifdef VBOX_WITH_USB
12827 USBProxyService *service = mParent->i_host()->i_usbProxyService();
12828 AssertReturn(service, E_FAIL);
12829 return service->detachDeviceFromVM(this, aId.ref(), !!aDone);
12830#else
12831 NOREF(aId);
12832 NOREF(aDone);
12833 return E_NOTIMPL;
12834#endif
12835}
12836
12837/**
12838 * Inserts all machine filters to the USB proxy service and then calls
12839 * Host::autoCaptureUSBDevices().
12840 *
12841 * Called by Console from the VM process upon VM startup.
12842 *
12843 * @note Locks what called methods lock.
12844 */
12845HRESULT SessionMachine::autoCaptureUSBDevices()
12846{
12847 LogFlowThisFunc(("\n"));
12848
12849#ifdef VBOX_WITH_USB
12850 HRESULT rc = mUSBDeviceFilters->i_notifyProxy(true /* aInsertFilters */);
12851 AssertComRC(rc);
12852 NOREF(rc);
12853
12854 USBProxyService *service = mParent->i_host()->i_usbProxyService();
12855 AssertReturn(service, E_FAIL);
12856 return service->autoCaptureDevicesForVM(this);
12857#else
12858 return S_OK;
12859#endif
12860}
12861
12862/**
12863 * Removes all machine filters from the USB proxy service and then calls
12864 * Host::detachAllUSBDevices().
12865 *
12866 * Called by Console from the VM process upon normal VM termination or by
12867 * SessionMachine::uninit() upon abnormal VM termination (from under the
12868 * Machine/SessionMachine lock).
12869 *
12870 * @note Locks what called methods lock.
12871 */
12872HRESULT SessionMachine::detachAllUSBDevices(BOOL aDone)
12873{
12874 LogFlowThisFunc(("\n"));
12875
12876#ifdef VBOX_WITH_USB
12877 HRESULT rc = mUSBDeviceFilters->i_notifyProxy(false /* aInsertFilters */);
12878 AssertComRC(rc);
12879 NOREF(rc);
12880
12881 USBProxyService *service = mParent->i_host()->i_usbProxyService();
12882 AssertReturn(service, E_FAIL);
12883 return service->detachAllDevicesFromVM(this, !!aDone, false /* aAbnormal */);
12884#else
12885 NOREF(aDone);
12886 return S_OK;
12887#endif
12888}
12889
12890/**
12891 * @note Locks this object for writing.
12892 */
12893HRESULT SessionMachine::onSessionEnd(const ComPtr<ISession> &aSession,
12894 ComPtr<IProgress> &aProgress)
12895{
12896 LogFlowThisFuncEnter();
12897
12898 LogFlowThisFunc(("callerstate=%d\n", getObjectState().getState()));
12899 /*
12900 * We don't assert below because it might happen that a non-direct session
12901 * informs us it is closed right after we've been uninitialized -- it's ok.
12902 */
12903
12904 /* get IInternalSessionControl interface */
12905 ComPtr<IInternalSessionControl> control(aSession);
12906
12907 ComAssertRet(!control.isNull(), E_INVALIDARG);
12908
12909 /* Creating a Progress object requires the VirtualBox lock, and
12910 * thus locking it here is required by the lock order rules. */
12911 AutoMultiWriteLock2 alock(mParent, this COMMA_LOCKVAL_SRC_POS);
12912
12913 if (control == mData->mSession.mDirectControl)
12914 {
12915 /* The direct session is being normally closed by the client process
12916 * ----------------------------------------------------------------- */
12917
12918 /* go to the closing state (essential for all open*Session() calls and
12919 * for #checkForDeath()) */
12920 Assert(mData->mSession.mState == SessionState_Locked);
12921 mData->mSession.mState = SessionState_Unlocking;
12922
12923 /* set direct control to NULL to release the remote instance */
12924 mData->mSession.mDirectControl.setNull();
12925 LogFlowThisFunc(("Direct control is set to NULL\n"));
12926
12927 if (mData->mSession.mProgress)
12928 {
12929 /* finalize the progress, someone might wait if a frontend
12930 * closes the session before powering on the VM. */
12931 mData->mSession.mProgress->notifyComplete(E_FAIL,
12932 COM_IIDOF(ISession),
12933 getComponentName(),
12934 tr("The VM session was closed before any attempt to power it on"));
12935 mData->mSession.mProgress.setNull();
12936 }
12937
12938 /* Create the progress object the client will use to wait until
12939 * #checkForDeath() is called to uninitialize this session object after
12940 * it releases the IPC semaphore.
12941 * Note! Because we're "reusing" mProgress here, this must be a proxy
12942 * object just like for LaunchVMProcess. */
12943 Assert(mData->mSession.mProgress.isNull());
12944 ComObjPtr<ProgressProxy> progress;
12945 progress.createObject();
12946 ComPtr<IUnknown> pPeer(mPeer);
12947 progress->init(mParent, pPeer,
12948 Bstr(tr("Closing session")).raw(),
12949 FALSE /* aCancelable */);
12950 progress.queryInterfaceTo(aProgress.asOutParam());
12951 mData->mSession.mProgress = progress;
12952 }
12953 else
12954 {
12955 /* the remote session is being normally closed */
12956 Data::Session::RemoteControlList::iterator it =
12957 mData->mSession.mRemoteControls.begin();
12958 while (it != mData->mSession.mRemoteControls.end())
12959 {
12960 if (control == *it)
12961 break;
12962 ++it;
12963 }
12964 BOOL found = it != mData->mSession.mRemoteControls.end();
12965 ComAssertMsgRet(found, ("The session is not found in the session list!"),
12966 E_INVALIDARG);
12967 // This MUST be erase(it), not remove(*it) as the latter triggers a
12968 // very nasty use after free due to the place where the value "lives".
12969 mData->mSession.mRemoteControls.erase(it);
12970 }
12971
12972 /* signal the client watcher thread, because the client is going away */
12973 mParent->i_updateClientWatcher();
12974
12975 LogFlowThisFuncLeave();
12976 return S_OK;
12977}
12978
12979/**
12980 * @note Locks this object for writing.
12981 */
12982HRESULT SessionMachine::beginSavingState(ComPtr<IProgress> &aProgress,
12983 com::Utf8Str &aStateFilePath)
12984{
12985 LogFlowThisFuncEnter();
12986
12987 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
12988
12989 AssertReturn( mData->mMachineState == MachineState_Paused
12990 && mConsoleTaskData.mLastState == MachineState_Null
12991 && mConsoleTaskData.strStateFilePath.isEmpty(),
12992 E_FAIL);
12993
12994 /* create a progress object to track operation completion */
12995 ComObjPtr<Progress> pProgress;
12996 pProgress.createObject();
12997 pProgress->init(i_getVirtualBox(),
12998 static_cast<IMachine *>(this) /* aInitiator */,
12999 Bstr(tr("Saving the execution state of the virtual machine")).raw(),
13000 FALSE /* aCancelable */);
13001
13002 /* stateFilePath is null when the machine is not running */
13003 if (mData->mMachineState == MachineState_Paused)
13004 i_composeSavedStateFilename(aStateFilePath);
13005
13006 /* fill in the console task data */
13007 mConsoleTaskData.mLastState = mData->mMachineState;
13008 mConsoleTaskData.strStateFilePath = aStateFilePath;
13009 mConsoleTaskData.mProgress = pProgress;
13010
13011 /* set the state to Saving (this is expected by Console::SaveState()) */
13012 i_setMachineState(MachineState_Saving);
13013
13014 pProgress.queryInterfaceTo(aProgress.asOutParam());
13015
13016 return S_OK;
13017}
13018
13019/**
13020 * @note Locks mParent + this object for writing.
13021 */
13022HRESULT SessionMachine::endSavingState(LONG aResult,
13023 const com::Utf8Str &aErrMsg)
13024{
13025 LogFlowThisFunc(("\n"));
13026
13027 /* endSavingState() need mParent lock */
13028 AutoMultiWriteLock2 alock(mParent, this COMMA_LOCKVAL_SRC_POS);
13029
13030 AssertReturn( ( (SUCCEEDED(aResult) && mData->mMachineState == MachineState_Saved)
13031 || (FAILED(aResult) && mData->mMachineState == MachineState_Saving))
13032 && mConsoleTaskData.mLastState != MachineState_Null
13033 && !mConsoleTaskData.strStateFilePath.isEmpty(),
13034 E_FAIL);
13035
13036 /*
13037 * On failure, set the state to the state we had when BeginSavingState()
13038 * was called (this is expected by Console::SaveState() and the associated
13039 * task). On success the VM process already changed the state to
13040 * MachineState_Saved, so no need to do anything.
13041 */
13042 if (FAILED(aResult))
13043 i_setMachineState(mConsoleTaskData.mLastState);
13044
13045 return i_endSavingState(aResult, aErrMsg);
13046}
13047
13048/**
13049 * @note Locks this object for writing.
13050 */
13051HRESULT SessionMachine::adoptSavedState(const com::Utf8Str &aSavedStateFile)
13052{
13053 LogFlowThisFunc(("\n"));
13054
13055 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
13056
13057 AssertReturn( mData->mMachineState == MachineState_PoweredOff
13058 || mData->mMachineState == MachineState_Teleported
13059 || mData->mMachineState == MachineState_Aborted
13060 , E_FAIL); /** @todo setError. */
13061
13062 com::Utf8Str stateFilePathFull;
13063 int vrc = i_calculateFullPath(aSavedStateFile, stateFilePathFull);
13064 if (RT_FAILURE(vrc))
13065 return setError(VBOX_E_FILE_ERROR,
13066 tr("Invalid saved state file path '%s' (%Rrc)"),
13067 aSavedStateFile.c_str(),
13068 vrc);
13069
13070 mSSData->strStateFilePath = stateFilePathFull;
13071
13072 /* The below i_setMachineState() will detect the state transition and will
13073 * update the settings file */
13074
13075 return i_setMachineState(MachineState_Saved);
13076}
13077
13078HRESULT SessionMachine::pullGuestProperties(std::vector<com::Utf8Str> &aNames,
13079 std::vector<com::Utf8Str> &aValues,
13080 std::vector<LONG64> &aTimestamps,
13081 std::vector<com::Utf8Str> &aFlags)
13082{
13083 LogFlowThisFunc(("\n"));
13084
13085#ifdef VBOX_WITH_GUEST_PROPS
13086 using namespace guestProp;
13087
13088 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
13089
13090 size_t cEntries = mHWData->mGuestProperties.size();
13091 aNames.resize(cEntries);
13092 aValues.resize(cEntries);
13093 aTimestamps.resize(cEntries);
13094 aFlags.resize(cEntries);
13095
13096 size_t i = 0;
13097 for (HWData::GuestPropertyMap::iterator it = mHWData->mGuestProperties.begin();
13098 it != mHWData->mGuestProperties.end();
13099 ++it, ++i)
13100 {
13101 char szFlags[MAX_FLAGS_LEN + 1];
13102 aNames[i] = it->first;
13103 aValues[i] = it->second.strValue;
13104 aTimestamps[i] = it->second.mTimestamp;
13105
13106 /* If it is NULL, keep it NULL. */
13107 if (it->second.mFlags)
13108 {
13109 writeFlags(it->second.mFlags, szFlags);
13110 aFlags[i] = szFlags;
13111 }
13112 else
13113 aFlags[i] = "";
13114 }
13115 return S_OK;
13116#else
13117 ReturnComNotImplemented();
13118#endif
13119}
13120
13121HRESULT SessionMachine::pushGuestProperty(const com::Utf8Str &aName,
13122 const com::Utf8Str &aValue,
13123 LONG64 aTimestamp,
13124 const com::Utf8Str &aFlags)
13125{
13126 LogFlowThisFunc(("\n"));
13127
13128#ifdef VBOX_WITH_GUEST_PROPS
13129 using namespace guestProp;
13130
13131 try
13132 {
13133 /*
13134 * Convert input up front.
13135 */
13136 uint32_t fFlags = NILFLAG;
13137 if (aFlags.length())
13138 {
13139 int vrc = validateFlags(aFlags.c_str(), &fFlags);
13140 AssertRCReturn(vrc, E_INVALIDARG);
13141 }
13142
13143 /*
13144 * Now grab the object lock, validate the state and do the update.
13145 */
13146
13147 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
13148
13149 switch (mData->mMachineState)
13150 {
13151 case MachineState_Paused:
13152 case MachineState_Running:
13153 case MachineState_Teleporting:
13154 case MachineState_TeleportingPausedVM:
13155 case MachineState_LiveSnapshotting:
13156 case MachineState_DeletingSnapshotOnline:
13157 case MachineState_DeletingSnapshotPaused:
13158 case MachineState_Saving:
13159 case MachineState_Stopping:
13160 break;
13161
13162 default:
13163 AssertMsgFailedReturn(("%s\n", Global::stringifyMachineState(mData->mMachineState)),
13164 VBOX_E_INVALID_VM_STATE);
13165 }
13166
13167 i_setModified(IsModified_MachineData);
13168 mHWData.backup();
13169
13170 bool fDelete = !aValue.length();
13171 HWData::GuestPropertyMap::iterator it = mHWData->mGuestProperties.find(aName);
13172 if (it != mHWData->mGuestProperties.end())
13173 {
13174 if (!fDelete)
13175 {
13176 it->second.strValue = aValue;
13177 it->second.mTimestamp = aTimestamp;
13178 it->second.mFlags = fFlags;
13179 }
13180 else
13181 mHWData->mGuestProperties.erase(it);
13182
13183 mData->mGuestPropertiesModified = TRUE;
13184 }
13185 else if (!fDelete)
13186 {
13187 HWData::GuestProperty prop;
13188 prop.strValue = aValue;
13189 prop.mTimestamp = aTimestamp;
13190 prop.mFlags = fFlags;
13191
13192 mHWData->mGuestProperties[aName] = prop;
13193 mData->mGuestPropertiesModified = TRUE;
13194 }
13195
13196 /*
13197 * Send a callback notification if appropriate
13198 */
13199 if ( mHWData->mGuestPropertyNotificationPatterns.isEmpty()
13200 || RTStrSimplePatternMultiMatch(mHWData->mGuestPropertyNotificationPatterns.c_str(),
13201 RTSTR_MAX,
13202 aName.c_str(),
13203 RTSTR_MAX, NULL)
13204 )
13205 {
13206 alock.release();
13207
13208 mParent->i_onGuestPropertyChange(mData->mUuid,
13209 Bstr(aName).raw(),
13210 Bstr(aValue).raw(),
13211 Bstr(aFlags).raw());
13212 }
13213 }
13214 catch (...)
13215 {
13216 return VirtualBoxBase::handleUnexpectedExceptions(this, RT_SRC_POS);
13217 }
13218 return S_OK;
13219#else
13220 ReturnComNotImplemented();
13221#endif
13222}
13223
13224
13225HRESULT SessionMachine::lockMedia()
13226{
13227 AutoMultiWriteLock2 alock(this->lockHandle(),
13228 &mParent->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
13229
13230 AssertReturn( mData->mMachineState == MachineState_Starting
13231 || mData->mMachineState == MachineState_Restoring
13232 || mData->mMachineState == MachineState_TeleportingIn, E_FAIL);
13233
13234 clearError();
13235 alock.release();
13236 return i_lockMedia();
13237}
13238
13239HRESULT SessionMachine::unlockMedia()
13240{
13241 HRESULT hrc = i_unlockMedia();
13242 return hrc;
13243}
13244
13245HRESULT SessionMachine::ejectMedium(const ComPtr<IMediumAttachment> &aAttachment,
13246 ComPtr<IMediumAttachment> &aNewAttachment)
13247{
13248 // request the host lock first, since might be calling Host methods for getting host drives;
13249 // next, protect the media tree all the while we're in here, as well as our member variables
13250 AutoMultiWriteLock3 multiLock(mParent->i_host()->lockHandle(),
13251 this->lockHandle(),
13252 &mParent->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
13253
13254 IMediumAttachment *iAttach = aAttachment;
13255 ComObjPtr<MediumAttachment> pAttach = static_cast<MediumAttachment *>(iAttach);
13256
13257 Bstr ctrlName;
13258 LONG lPort;
13259 LONG lDevice;
13260 bool fTempEject;
13261 {
13262 AutoReadLock attLock(pAttach COMMA_LOCKVAL_SRC_POS);
13263
13264 /* Need to query the details first, as the IMediumAttachment reference
13265 * might be to the original settings, which we are going to change. */
13266 ctrlName = pAttach->i_getControllerName();
13267 lPort = pAttach->i_getPort();
13268 lDevice = pAttach->i_getDevice();
13269 fTempEject = pAttach->i_getTempEject();
13270 }
13271
13272 if (!fTempEject)
13273 {
13274 /* Remember previously mounted medium. The medium before taking the
13275 * backup is not necessarily the same thing. */
13276 ComObjPtr<Medium> oldmedium;
13277 oldmedium = pAttach->i_getMedium();
13278
13279 i_setModified(IsModified_Storage);
13280 mMediaData.backup();
13281
13282 // The backup operation makes the pAttach reference point to the
13283 // old settings. Re-get the correct reference.
13284 pAttach = i_findAttachment(mMediaData->mAttachments,
13285 ctrlName.raw(),
13286 lPort,
13287 lDevice);
13288
13289 {
13290 AutoCaller autoAttachCaller(this);
13291 if (FAILED(autoAttachCaller.rc())) return autoAttachCaller.rc();
13292
13293 AutoWriteLock attLock(pAttach COMMA_LOCKVAL_SRC_POS);
13294 if (!oldmedium.isNull())
13295 oldmedium->i_removeBackReference(mData->mUuid);
13296
13297 pAttach->i_updateMedium(NULL);
13298 pAttach->i_updateEjected();
13299 }
13300
13301 i_setModified(IsModified_Storage);
13302 }
13303 else
13304 {
13305 {
13306 AutoWriteLock attLock(pAttach COMMA_LOCKVAL_SRC_POS);
13307 pAttach->i_updateEjected();
13308 }
13309 }
13310
13311 pAttach.queryInterfaceTo(aNewAttachment.asOutParam());
13312
13313 return S_OK;
13314}
13315
13316// public methods only for internal purposes
13317/////////////////////////////////////////////////////////////////////////////
13318
13319#ifndef VBOX_WITH_GENERIC_SESSION_WATCHER
13320/**
13321 * Called from the client watcher thread to check for expected or unexpected
13322 * death of the client process that has a direct session to this machine.
13323 *
13324 * On Win32 and on OS/2, this method is called only when we've got the
13325 * mutex (i.e. the client has either died or terminated normally) so it always
13326 * returns @c true (the client is terminated, the session machine is
13327 * uninitialized).
13328 *
13329 * On other platforms, the method returns @c true if the client process has
13330 * terminated normally or abnormally and the session machine was uninitialized,
13331 * and @c false if the client process is still alive.
13332 *
13333 * @note Locks this object for writing.
13334 */
13335bool SessionMachine::i_checkForDeath()
13336{
13337 Uninit::Reason reason;
13338 bool terminated = false;
13339
13340 /* Enclose autoCaller with a block because calling uninit() from under it
13341 * will deadlock. */
13342 {
13343 AutoCaller autoCaller(this);
13344 if (!autoCaller.isOk())
13345 {
13346 /* return true if not ready, to cause the client watcher to exclude
13347 * the corresponding session from watching */
13348 LogFlowThisFunc(("Already uninitialized!\n"));
13349 return true;
13350 }
13351
13352 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
13353
13354 /* Determine the reason of death: if the session state is Closing here,
13355 * everything is fine. Otherwise it means that the client did not call
13356 * OnSessionEnd() before it released the IPC semaphore. This may happen
13357 * either because the client process has abnormally terminated, or
13358 * because it simply forgot to call ISession::Close() before exiting. We
13359 * threat the latter also as an abnormal termination (see
13360 * Session::uninit() for details). */
13361 reason = mData->mSession.mState == SessionState_Unlocking ?
13362 Uninit::Normal :
13363 Uninit::Abnormal;
13364
13365 if (mClientToken)
13366 terminated = mClientToken->release();
13367 } /* AutoCaller block */
13368
13369 if (terminated)
13370 uninit(reason);
13371
13372 return terminated;
13373}
13374
13375void SessionMachine::i_getTokenId(Utf8Str &strTokenId)
13376{
13377 LogFlowThisFunc(("\n"));
13378
13379 strTokenId.setNull();
13380
13381 AutoCaller autoCaller(this);
13382 AssertComRCReturnVoid(autoCaller.rc());
13383
13384 Assert(mClientToken);
13385 if (mClientToken)
13386 mClientToken->getId(strTokenId);
13387}
13388#else /* VBOX_WITH_GENERIC_SESSION_WATCHER */
13389IToken *SessionMachine::i_getToken()
13390{
13391 LogFlowThisFunc(("\n"));
13392
13393 AutoCaller autoCaller(this);
13394 AssertComRCReturn(autoCaller.rc(), NULL);
13395
13396 Assert(mClientToken);
13397 if (mClientToken)
13398 return mClientToken->getToken();
13399 else
13400 return NULL;
13401}
13402#endif /* VBOX_WITH_GENERIC_SESSION_WATCHER */
13403
13404Machine::ClientToken *SessionMachine::i_getClientToken()
13405{
13406 LogFlowThisFunc(("\n"));
13407
13408 AutoCaller autoCaller(this);
13409 AssertComRCReturn(autoCaller.rc(), NULL);
13410
13411 return mClientToken;
13412}
13413
13414
13415/**
13416 * @note Locks this object for reading.
13417 */
13418HRESULT SessionMachine::i_onNetworkAdapterChange(INetworkAdapter *networkAdapter, BOOL changeAdapter)
13419{
13420 LogFlowThisFunc(("\n"));
13421
13422 AutoCaller autoCaller(this);
13423 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
13424
13425 ComPtr<IInternalSessionControl> directControl;
13426 {
13427 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
13428 directControl = mData->mSession.mDirectControl;
13429 }
13430
13431 /* ignore notifications sent after #OnSessionEnd() is called */
13432 if (!directControl)
13433 return S_OK;
13434
13435 return directControl->OnNetworkAdapterChange(networkAdapter, changeAdapter);
13436}
13437
13438/**
13439 * @note Locks this object for reading.
13440 */
13441HRESULT SessionMachine::i_onNATRedirectRuleChange(ULONG ulSlot, BOOL aNatRuleRemove, IN_BSTR aRuleName,
13442 NATProtocol_T aProto, IN_BSTR aHostIp, LONG aHostPort,
13443 IN_BSTR aGuestIp, LONG aGuestPort)
13444{
13445 LogFlowThisFunc(("\n"));
13446
13447 AutoCaller autoCaller(this);
13448 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
13449
13450 ComPtr<IInternalSessionControl> directControl;
13451 {
13452 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
13453 directControl = mData->mSession.mDirectControl;
13454 }
13455
13456 /* ignore notifications sent after #OnSessionEnd() is called */
13457 if (!directControl)
13458 return S_OK;
13459 /*
13460 * instead acting like callback we ask IVirtualBox deliver corresponding event
13461 */
13462
13463 mParent->i_onNatRedirectChange(i_getId(), ulSlot, RT_BOOL(aNatRuleRemove), aRuleName, aProto, aHostIp,
13464 (uint16_t)aHostPort, aGuestIp, (uint16_t)aGuestPort);
13465 return S_OK;
13466}
13467
13468/**
13469 * @note Locks this object for reading.
13470 */
13471HRESULT SessionMachine::i_onSerialPortChange(ISerialPort *serialPort)
13472{
13473 LogFlowThisFunc(("\n"));
13474
13475 AutoCaller autoCaller(this);
13476 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
13477
13478 ComPtr<IInternalSessionControl> directControl;
13479 {
13480 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
13481 directControl = mData->mSession.mDirectControl;
13482 }
13483
13484 /* ignore notifications sent after #OnSessionEnd() is called */
13485 if (!directControl)
13486 return S_OK;
13487
13488 return directControl->OnSerialPortChange(serialPort);
13489}
13490
13491/**
13492 * @note Locks this object for reading.
13493 */
13494HRESULT SessionMachine::i_onParallelPortChange(IParallelPort *parallelPort)
13495{
13496 LogFlowThisFunc(("\n"));
13497
13498 AutoCaller autoCaller(this);
13499 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
13500
13501 ComPtr<IInternalSessionControl> directControl;
13502 {
13503 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
13504 directControl = mData->mSession.mDirectControl;
13505 }
13506
13507 /* ignore notifications sent after #OnSessionEnd() is called */
13508 if (!directControl)
13509 return S_OK;
13510
13511 return directControl->OnParallelPortChange(parallelPort);
13512}
13513
13514/**
13515 * @note Locks this object for reading.
13516 */
13517HRESULT SessionMachine::i_onStorageControllerChange()
13518{
13519 LogFlowThisFunc(("\n"));
13520
13521 AutoCaller autoCaller(this);
13522 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
13523
13524 ComPtr<IInternalSessionControl> directControl;
13525 {
13526 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
13527 directControl = mData->mSession.mDirectControl;
13528 }
13529
13530 /* ignore notifications sent after #OnSessionEnd() is called */
13531 if (!directControl)
13532 return S_OK;
13533
13534 return directControl->OnStorageControllerChange();
13535}
13536
13537/**
13538 * @note Locks this object for reading.
13539 */
13540HRESULT SessionMachine::i_onMediumChange(IMediumAttachment *aAttachment, BOOL aForce)
13541{
13542 LogFlowThisFunc(("\n"));
13543
13544 AutoCaller autoCaller(this);
13545 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
13546
13547 ComPtr<IInternalSessionControl> directControl;
13548 {
13549 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
13550 directControl = mData->mSession.mDirectControl;
13551 }
13552
13553 /* ignore notifications sent after #OnSessionEnd() is called */
13554 if (!directControl)
13555 return S_OK;
13556
13557 return directControl->OnMediumChange(aAttachment, aForce);
13558}
13559
13560/**
13561 * @note Locks this object for reading.
13562 */
13563HRESULT SessionMachine::i_onCPUChange(ULONG aCPU, BOOL aRemove)
13564{
13565 LogFlowThisFunc(("\n"));
13566
13567 AutoCaller autoCaller(this);
13568 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
13569
13570 ComPtr<IInternalSessionControl> directControl;
13571 {
13572 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
13573 directControl = mData->mSession.mDirectControl;
13574 }
13575
13576 /* ignore notifications sent after #OnSessionEnd() is called */
13577 if (!directControl)
13578 return S_OK;
13579
13580 return directControl->OnCPUChange(aCPU, aRemove);
13581}
13582
13583HRESULT SessionMachine::i_onCPUExecutionCapChange(ULONG aExecutionCap)
13584{
13585 LogFlowThisFunc(("\n"));
13586
13587 AutoCaller autoCaller(this);
13588 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
13589
13590 ComPtr<IInternalSessionControl> directControl;
13591 {
13592 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
13593 directControl = mData->mSession.mDirectControl;
13594 }
13595
13596 /* ignore notifications sent after #OnSessionEnd() is called */
13597 if (!directControl)
13598 return S_OK;
13599
13600 return directControl->OnCPUExecutionCapChange(aExecutionCap);
13601}
13602
13603/**
13604 * @note Locks this object for reading.
13605 */
13606HRESULT SessionMachine::i_onVRDEServerChange(BOOL aRestart)
13607{
13608 LogFlowThisFunc(("\n"));
13609
13610 AutoCaller autoCaller(this);
13611 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
13612
13613 ComPtr<IInternalSessionControl> directControl;
13614 {
13615 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
13616 directControl = mData->mSession.mDirectControl;
13617 }
13618
13619 /* ignore notifications sent after #OnSessionEnd() is called */
13620 if (!directControl)
13621 return S_OK;
13622
13623 return directControl->OnVRDEServerChange(aRestart);
13624}
13625
13626/**
13627 * @note Locks this object for reading.
13628 */
13629HRESULT SessionMachine::i_onVideoCaptureChange()
13630{
13631 LogFlowThisFunc(("\n"));
13632
13633 AutoCaller autoCaller(this);
13634 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
13635
13636 ComPtr<IInternalSessionControl> directControl;
13637 {
13638 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
13639 directControl = mData->mSession.mDirectControl;
13640 }
13641
13642 /* ignore notifications sent after #OnSessionEnd() is called */
13643 if (!directControl)
13644 return S_OK;
13645
13646 return directControl->OnVideoCaptureChange();
13647}
13648
13649/**
13650 * @note Locks this object for reading.
13651 */
13652HRESULT SessionMachine::i_onUSBControllerChange()
13653{
13654 LogFlowThisFunc(("\n"));
13655
13656 AutoCaller autoCaller(this);
13657 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
13658
13659 ComPtr<IInternalSessionControl> directControl;
13660 {
13661 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
13662 directControl = mData->mSession.mDirectControl;
13663 }
13664
13665 /* ignore notifications sent after #OnSessionEnd() is called */
13666 if (!directControl)
13667 return S_OK;
13668
13669 return directControl->OnUSBControllerChange();
13670}
13671
13672/**
13673 * @note Locks this object for reading.
13674 */
13675HRESULT SessionMachine::i_onSharedFolderChange()
13676{
13677 LogFlowThisFunc(("\n"));
13678
13679 AutoCaller autoCaller(this);
13680 AssertComRCReturnRC(autoCaller.rc());
13681
13682 ComPtr<IInternalSessionControl> directControl;
13683 {
13684 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
13685 directControl = mData->mSession.mDirectControl;
13686 }
13687
13688 /* ignore notifications sent after #OnSessionEnd() is called */
13689 if (!directControl)
13690 return S_OK;
13691
13692 return directControl->OnSharedFolderChange(FALSE /* aGlobal */);
13693}
13694
13695/**
13696 * @note Locks this object for reading.
13697 */
13698HRESULT SessionMachine::i_onClipboardModeChange(ClipboardMode_T aClipboardMode)
13699{
13700 LogFlowThisFunc(("\n"));
13701
13702 AutoCaller autoCaller(this);
13703 AssertComRCReturnRC(autoCaller.rc());
13704
13705 ComPtr<IInternalSessionControl> directControl;
13706 {
13707 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
13708 directControl = mData->mSession.mDirectControl;
13709 }
13710
13711 /* ignore notifications sent after #OnSessionEnd() is called */
13712 if (!directControl)
13713 return S_OK;
13714
13715 return directControl->OnClipboardModeChange(aClipboardMode);
13716}
13717
13718/**
13719 * @note Locks this object for reading.
13720 */
13721HRESULT SessionMachine::i_onDnDModeChange(DnDMode_T aDnDMode)
13722{
13723 LogFlowThisFunc(("\n"));
13724
13725 AutoCaller autoCaller(this);
13726 AssertComRCReturnRC(autoCaller.rc());
13727
13728 ComPtr<IInternalSessionControl> directControl;
13729 {
13730 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
13731 directControl = mData->mSession.mDirectControl;
13732 }
13733
13734 /* ignore notifications sent after #OnSessionEnd() is called */
13735 if (!directControl)
13736 return S_OK;
13737
13738 return directControl->OnDnDModeChange(aDnDMode);
13739}
13740
13741/**
13742 * @note Locks this object for reading.
13743 */
13744HRESULT SessionMachine::i_onBandwidthGroupChange(IBandwidthGroup *aBandwidthGroup)
13745{
13746 LogFlowThisFunc(("\n"));
13747
13748 AutoCaller autoCaller(this);
13749 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
13750
13751 ComPtr<IInternalSessionControl> directControl;
13752 {
13753 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
13754 directControl = mData->mSession.mDirectControl;
13755 }
13756
13757 /* ignore notifications sent after #OnSessionEnd() is called */
13758 if (!directControl)
13759 return S_OK;
13760
13761 return directControl->OnBandwidthGroupChange(aBandwidthGroup);
13762}
13763
13764/**
13765 * @note Locks this object for reading.
13766 */
13767HRESULT SessionMachine::i_onStorageDeviceChange(IMediumAttachment *aAttachment, BOOL aRemove, BOOL aSilent)
13768{
13769 LogFlowThisFunc(("\n"));
13770
13771 AutoCaller autoCaller(this);
13772 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
13773
13774 ComPtr<IInternalSessionControl> directControl;
13775 {
13776 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
13777 directControl = mData->mSession.mDirectControl;
13778 }
13779
13780 /* ignore notifications sent after #OnSessionEnd() is called */
13781 if (!directControl)
13782 return S_OK;
13783
13784 return directControl->OnStorageDeviceChange(aAttachment, aRemove, aSilent);
13785}
13786
13787/**
13788 * Returns @c true if this machine's USB controller reports it has a matching
13789 * filter for the given USB device and @c false otherwise.
13790 *
13791 * @note locks this object for reading.
13792 */
13793bool SessionMachine::i_hasMatchingUSBFilter(const ComObjPtr<HostUSBDevice> &aDevice, ULONG *aMaskedIfs)
13794{
13795 AutoCaller autoCaller(this);
13796 /* silently return if not ready -- this method may be called after the
13797 * direct machine session has been called */
13798 if (!autoCaller.isOk())
13799 return false;
13800
13801#ifdef VBOX_WITH_USB
13802 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
13803
13804 switch (mData->mMachineState)
13805 {
13806 case MachineState_Starting:
13807 case MachineState_Restoring:
13808 case MachineState_TeleportingIn:
13809 case MachineState_Paused:
13810 case MachineState_Running:
13811 /** @todo Live Migration: snapshoting & teleporting. Need to fend things of
13812 * elsewhere... */
13813 alock.release();
13814 return mUSBDeviceFilters->i_hasMatchingFilter(aDevice, aMaskedIfs);
13815 default: break;
13816 }
13817#else
13818 NOREF(aDevice);
13819 NOREF(aMaskedIfs);
13820#endif
13821 return false;
13822}
13823
13824/**
13825 * @note The calls shall hold no locks. Will temporarily lock this object for reading.
13826 */
13827HRESULT SessionMachine::i_onUSBDeviceAttach(IUSBDevice *aDevice,
13828 IVirtualBoxErrorInfo *aError,
13829 ULONG aMaskedIfs)
13830{
13831 LogFlowThisFunc(("\n"));
13832
13833 AutoCaller autoCaller(this);
13834
13835 /* This notification may happen after the machine object has been
13836 * uninitialized (the session was closed), so don't assert. */
13837 if (FAILED(autoCaller.rc())) return autoCaller.rc();
13838
13839 ComPtr<IInternalSessionControl> directControl;
13840 {
13841 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
13842 directControl = mData->mSession.mDirectControl;
13843 }
13844
13845 /* fail on notifications sent after #OnSessionEnd() is called, it is
13846 * expected by the caller */
13847 if (!directControl)
13848 return E_FAIL;
13849
13850 /* No locks should be held at this point. */
13851 AssertMsg(RTLockValidatorWriteLockGetCount(RTThreadSelf()) == 0, ("%d\n", RTLockValidatorWriteLockGetCount(RTThreadSelf())));
13852 AssertMsg(RTLockValidatorReadLockGetCount(RTThreadSelf()) == 0, ("%d\n", RTLockValidatorReadLockGetCount(RTThreadSelf())));
13853
13854 return directControl->OnUSBDeviceAttach(aDevice, aError, aMaskedIfs);
13855}
13856
13857/**
13858 * @note The calls shall hold no locks. Will temporarily lock this object for reading.
13859 */
13860HRESULT SessionMachine::i_onUSBDeviceDetach(IN_BSTR aId,
13861 IVirtualBoxErrorInfo *aError)
13862{
13863 LogFlowThisFunc(("\n"));
13864
13865 AutoCaller autoCaller(this);
13866
13867 /* This notification may happen after the machine object has been
13868 * uninitialized (the session was closed), so don't assert. */
13869 if (FAILED(autoCaller.rc())) return autoCaller.rc();
13870
13871 ComPtr<IInternalSessionControl> directControl;
13872 {
13873 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
13874 directControl = mData->mSession.mDirectControl;
13875 }
13876
13877 /* fail on notifications sent after #OnSessionEnd() is called, it is
13878 * expected by the caller */
13879 if (!directControl)
13880 return E_FAIL;
13881
13882 /* No locks should be held at this point. */
13883 AssertMsg(RTLockValidatorWriteLockGetCount(RTThreadSelf()) == 0, ("%d\n", RTLockValidatorWriteLockGetCount(RTThreadSelf())));
13884 AssertMsg(RTLockValidatorReadLockGetCount(RTThreadSelf()) == 0, ("%d\n", RTLockValidatorReadLockGetCount(RTThreadSelf())));
13885
13886 return directControl->OnUSBDeviceDetach(aId, aError);
13887}
13888
13889// protected methods
13890/////////////////////////////////////////////////////////////////////////////
13891
13892/**
13893 * Helper method to finalize saving the state.
13894 *
13895 * @note Must be called from under this object's lock.
13896 *
13897 * @param aRc S_OK if the snapshot has been taken successfully
13898 * @param aErrMsg human readable error message for failure
13899 *
13900 * @note Locks mParent + this objects for writing.
13901 */
13902HRESULT SessionMachine::i_endSavingState(HRESULT aRc, const Utf8Str &aErrMsg)
13903{
13904 LogFlowThisFuncEnter();
13905
13906 AutoCaller autoCaller(this);
13907 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
13908
13909 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
13910
13911 HRESULT rc = S_OK;
13912
13913 if (SUCCEEDED(aRc))
13914 {
13915 mSSData->strStateFilePath = mConsoleTaskData.strStateFilePath;
13916
13917 /* save all VM settings */
13918 rc = i_saveSettings(NULL);
13919 // no need to check whether VirtualBox.xml needs saving also since
13920 // we can't have a name change pending at this point
13921 }
13922 else
13923 {
13924 // delete the saved state file (it might have been already created);
13925 // we need not check whether this is shared with a snapshot here because
13926 // we certainly created this saved state file here anew
13927 RTFileDelete(mConsoleTaskData.strStateFilePath.c_str());
13928 }
13929
13930 /* notify the progress object about operation completion */
13931 Assert(mConsoleTaskData.mProgress);
13932 if (SUCCEEDED(aRc))
13933 mConsoleTaskData.mProgress->i_notifyComplete(S_OK);
13934 else
13935 {
13936 if (aErrMsg.length())
13937 mConsoleTaskData.mProgress->i_notifyComplete(aRc,
13938 COM_IIDOF(ISession),
13939 getComponentName(),
13940 aErrMsg.c_str());
13941 else
13942 mConsoleTaskData.mProgress->i_notifyComplete(aRc);
13943 }
13944
13945 /* clear out the temporary saved state data */
13946 mConsoleTaskData.mLastState = MachineState_Null;
13947 mConsoleTaskData.strStateFilePath.setNull();
13948 mConsoleTaskData.mProgress.setNull();
13949
13950 LogFlowThisFuncLeave();
13951 return rc;
13952}
13953
13954/**
13955 * Deletes the given file if it is no longer in use by either the current machine state
13956 * (if the machine is "saved") or any of the machine's snapshots.
13957 *
13958 * Note: This checks mSSData->strStateFilePath, which is shared by the Machine and SessionMachine
13959 * but is different for each SnapshotMachine. When calling this, the order of calling this
13960 * function on the one hand and changing that variable OR the snapshots tree on the other hand
13961 * is therefore critical. I know, it's all rather messy.
13962 *
13963 * @param strStateFile
13964 * @param pSnapshotToIgnore Passed to Snapshot::sharesSavedStateFile(); this snapshot is ignored in
13965 * the test for whether the saved state file is in use.
13966 */
13967void SessionMachine::i_releaseSavedStateFile(const Utf8Str &strStateFile,
13968 Snapshot *pSnapshotToIgnore)
13969{
13970 // it is safe to delete this saved state file if it is not currently in use by the machine ...
13971 if ( (strStateFile.isNotEmpty())
13972 && (strStateFile != mSSData->strStateFilePath) // session machine's saved state
13973 )
13974 // ... and it must also not be shared with other snapshots
13975 if ( !mData->mFirstSnapshot
13976 || !mData->mFirstSnapshot->i_sharesSavedStateFile(strStateFile, pSnapshotToIgnore)
13977 // this checks the SnapshotMachine's state file paths
13978 )
13979 RTFileDelete(strStateFile.c_str());
13980}
13981
13982/**
13983 * Locks the attached media.
13984 *
13985 * All attached hard disks are locked for writing and DVD/floppy are locked for
13986 * reading. Parents of attached hard disks (if any) are locked for reading.
13987 *
13988 * This method also performs accessibility check of all media it locks: if some
13989 * media is inaccessible, the method will return a failure and a bunch of
13990 * extended error info objects per each inaccessible medium.
13991 *
13992 * Note that this method is atomic: if it returns a success, all media are
13993 * locked as described above; on failure no media is locked at all (all
13994 * succeeded individual locks will be undone).
13995 *
13996 * The caller is responsible for doing the necessary state sanity checks.
13997 *
13998 * The locks made by this method must be undone by calling #unlockMedia() when
13999 * no more needed.
14000 */
14001HRESULT SessionMachine::i_lockMedia()
14002{
14003 AutoCaller autoCaller(this);
14004 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
14005
14006 AutoMultiWriteLock2 alock(this->lockHandle(),
14007 &mParent->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
14008
14009 /* bail out if trying to lock things with already set up locking */
14010 AssertReturn(mData->mSession.mLockedMedia.IsEmpty(), E_FAIL);
14011
14012 MultiResult mrc(S_OK);
14013
14014 /* Collect locking information for all medium objects attached to the VM. */
14015 for (MediaData::AttachmentList::const_iterator it = mMediaData->mAttachments.begin();
14016 it != mMediaData->mAttachments.end();
14017 ++it)
14018 {
14019 MediumAttachment* pAtt = *it;
14020 DeviceType_T devType = pAtt->i_getType();
14021 Medium *pMedium = pAtt->i_getMedium();
14022
14023 MediumLockList *pMediumLockList(new MediumLockList());
14024 // There can be attachments without a medium (floppy/dvd), and thus
14025 // it's impossible to create a medium lock list. It still makes sense
14026 // to have the empty medium lock list in the map in case a medium is
14027 // attached later.
14028 if (pMedium != NULL)
14029 {
14030 MediumType_T mediumType = pMedium->i_getType();
14031 bool fIsReadOnlyLock = mediumType == MediumType_Readonly
14032 || mediumType == MediumType_Shareable;
14033 bool fIsVitalImage = (devType == DeviceType_HardDisk);
14034
14035 alock.release();
14036 mrc = pMedium->i_createMediumLockList(fIsVitalImage /* fFailIfInaccessible */,
14037 !fIsReadOnlyLock /* fMediumLockWrite */,
14038 NULL,
14039 *pMediumLockList);
14040 alock.acquire();
14041 if (FAILED(mrc))
14042 {
14043 delete pMediumLockList;
14044 mData->mSession.mLockedMedia.Clear();
14045 break;
14046 }
14047 }
14048
14049 HRESULT rc = mData->mSession.mLockedMedia.Insert(pAtt, pMediumLockList);
14050 if (FAILED(rc))
14051 {
14052 mData->mSession.mLockedMedia.Clear();
14053 mrc = setError(rc,
14054 tr("Collecting locking information for all attached media failed"));
14055 break;
14056 }
14057 }
14058
14059 if (SUCCEEDED(mrc))
14060 {
14061 /* Now lock all media. If this fails, nothing is locked. */
14062 alock.release();
14063 HRESULT rc = mData->mSession.mLockedMedia.Lock();
14064 alock.acquire();
14065 if (FAILED(rc))
14066 {
14067 mrc = setError(rc,
14068 tr("Locking of attached media failed"));
14069 }
14070 }
14071
14072 return mrc;
14073}
14074
14075/**
14076 * Undoes the locks made by by #lockMedia().
14077 */
14078HRESULT SessionMachine::i_unlockMedia()
14079{
14080 AutoCaller autoCaller(this);
14081 AssertComRCReturn(autoCaller.rc(),autoCaller.rc());
14082
14083 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
14084
14085 /* we may be holding important error info on the current thread;
14086 * preserve it */
14087 ErrorInfoKeeper eik;
14088
14089 HRESULT rc = mData->mSession.mLockedMedia.Clear();
14090 AssertComRC(rc);
14091 return rc;
14092}
14093
14094/**
14095 * Helper to change the machine state (reimplementation).
14096 *
14097 * @note Locks this object for writing.
14098 * @note This method must not call i_saveSettings or SaveSettings, otherwise
14099 * it can cause crashes in random places due to unexpectedly committing
14100 * the current settings. The caller is responsible for that. The call
14101 * to saveStateSettings is fine, because this method does not commit.
14102 */
14103HRESULT SessionMachine::i_setMachineState(MachineState_T aMachineState)
14104{
14105 LogFlowThisFuncEnter();
14106 LogFlowThisFunc(("aMachineState=%s\n", Global::stringifyMachineState(aMachineState) ));
14107
14108 AutoCaller autoCaller(this);
14109 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
14110
14111 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
14112
14113 MachineState_T oldMachineState = mData->mMachineState;
14114
14115 AssertMsgReturn(oldMachineState != aMachineState,
14116 ("oldMachineState=%s, aMachineState=%s\n",
14117 Global::stringifyMachineState(oldMachineState), Global::stringifyMachineState(aMachineState)),
14118 E_FAIL);
14119
14120 HRESULT rc = S_OK;
14121
14122 int stsFlags = 0;
14123 bool deleteSavedState = false;
14124
14125 /* detect some state transitions */
14126
14127 if ( ( oldMachineState == MachineState_Saved
14128 && aMachineState == MachineState_Restoring)
14129 || ( ( oldMachineState == MachineState_PoweredOff
14130 || oldMachineState == MachineState_Teleported
14131 || oldMachineState == MachineState_Aborted
14132 )
14133 && ( aMachineState == MachineState_TeleportingIn
14134 || aMachineState == MachineState_Starting
14135 )
14136 )
14137 )
14138 {
14139 /* The EMT thread is about to start */
14140
14141 /* Nothing to do here for now... */
14142
14143 /// @todo NEWMEDIA don't let mDVDDrive and other children
14144 /// change anything when in the Starting/Restoring state
14145 }
14146 else if ( ( oldMachineState == MachineState_Running
14147 || oldMachineState == MachineState_Paused
14148 || oldMachineState == MachineState_Teleporting
14149 || oldMachineState == MachineState_LiveSnapshotting
14150 || oldMachineState == MachineState_Stuck
14151 || oldMachineState == MachineState_Starting
14152 || oldMachineState == MachineState_Stopping
14153 || oldMachineState == MachineState_Saving
14154 || oldMachineState == MachineState_Restoring
14155 || oldMachineState == MachineState_TeleportingPausedVM
14156 || oldMachineState == MachineState_TeleportingIn
14157 )
14158 && ( aMachineState == MachineState_PoweredOff
14159 || aMachineState == MachineState_Saved
14160 || aMachineState == MachineState_Teleported
14161 || aMachineState == MachineState_Aborted
14162 )
14163 /* ignore PoweredOff->Saving->PoweredOff transition when taking a
14164 * snapshot */
14165 && ( mConsoleTaskData.mSnapshot.isNull()
14166 || mConsoleTaskData.mLastState >= MachineState_Running /** @todo Live Migration: clean up (lazy bird) */
14167 )
14168 )
14169 {
14170 /* The EMT thread has just stopped, unlock attached media. Note that as
14171 * opposed to locking that is done from Console, we do unlocking here
14172 * because the VM process may have aborted before having a chance to
14173 * properly unlock all media it locked. */
14174
14175 unlockMedia();
14176 }
14177
14178 if (oldMachineState == MachineState_Restoring)
14179 {
14180 if (aMachineState != MachineState_Saved)
14181 {
14182 /*
14183 * delete the saved state file once the machine has finished
14184 * restoring from it (note that Console sets the state from
14185 * Restoring to Saved if the VM couldn't restore successfully,
14186 * to give the user an ability to fix an error and retry --
14187 * we keep the saved state file in this case)
14188 */
14189 deleteSavedState = true;
14190 }
14191 }
14192 else if ( oldMachineState == MachineState_Saved
14193 && ( aMachineState == MachineState_PoweredOff
14194 || aMachineState == MachineState_Aborted
14195 || aMachineState == MachineState_Teleported
14196 )
14197 )
14198 {
14199 /*
14200 * delete the saved state after Console::ForgetSavedState() is called
14201 * or if the VM process (owning a direct VM session) crashed while the
14202 * VM was Saved
14203 */
14204
14205 /// @todo (dmik)
14206 // Not sure that deleting the saved state file just because of the
14207 // client death before it attempted to restore the VM is a good
14208 // thing. But when it crashes we need to go to the Aborted state
14209 // which cannot have the saved state file associated... The only
14210 // way to fix this is to make the Aborted condition not a VM state
14211 // but a bool flag: i.e., when a crash occurs, set it to true and
14212 // change the state to PoweredOff or Saved depending on the
14213 // saved state presence.
14214
14215 deleteSavedState = true;
14216 mData->mCurrentStateModified = TRUE;
14217 stsFlags |= SaveSTS_CurStateModified;
14218 }
14219
14220 if ( aMachineState == MachineState_Starting
14221 || aMachineState == MachineState_Restoring
14222 || aMachineState == MachineState_TeleportingIn
14223 )
14224 {
14225 /* set the current state modified flag to indicate that the current
14226 * state is no more identical to the state in the
14227 * current snapshot */
14228 if (!mData->mCurrentSnapshot.isNull())
14229 {
14230 mData->mCurrentStateModified = TRUE;
14231 stsFlags |= SaveSTS_CurStateModified;
14232 }
14233 }
14234
14235 if (deleteSavedState)
14236 {
14237 if (mRemoveSavedState)
14238 {
14239 Assert(!mSSData->strStateFilePath.isEmpty());
14240
14241 // it is safe to delete the saved state file if ...
14242 if ( !mData->mFirstSnapshot // ... we have no snapshots or
14243 || !mData->mFirstSnapshot->i_sharesSavedStateFile(mSSData->strStateFilePath, NULL /* pSnapshotToIgnore */)
14244 // ... none of the snapshots share the saved state file
14245 )
14246 RTFileDelete(mSSData->strStateFilePath.c_str());
14247 }
14248
14249 mSSData->strStateFilePath.setNull();
14250 stsFlags |= SaveSTS_StateFilePath;
14251 }
14252
14253 /* redirect to the underlying peer machine */
14254 mPeer->i_setMachineState(aMachineState);
14255
14256 if ( aMachineState == MachineState_PoweredOff
14257 || aMachineState == MachineState_Teleported
14258 || aMachineState == MachineState_Aborted
14259 || aMachineState == MachineState_Saved)
14260 {
14261 /* the machine has stopped execution
14262 * (or the saved state file was adopted) */
14263 stsFlags |= SaveSTS_StateTimeStamp;
14264 }
14265
14266 if ( ( oldMachineState == MachineState_PoweredOff
14267 || oldMachineState == MachineState_Aborted
14268 || oldMachineState == MachineState_Teleported
14269 )
14270 && aMachineState == MachineState_Saved)
14271 {
14272 /* the saved state file was adopted */
14273 Assert(!mSSData->strStateFilePath.isEmpty());
14274 stsFlags |= SaveSTS_StateFilePath;
14275 }
14276
14277#ifdef VBOX_WITH_GUEST_PROPS
14278 if ( aMachineState == MachineState_PoweredOff
14279 || aMachineState == MachineState_Aborted
14280 || aMachineState == MachineState_Teleported)
14281 {
14282 /* Make sure any transient guest properties get removed from the
14283 * property store on shutdown. */
14284
14285 HWData::GuestPropertyMap::const_iterator it;
14286 BOOL fNeedsSaving = mData->mGuestPropertiesModified;
14287 if (!fNeedsSaving)
14288 for (it = mHWData->mGuestProperties.begin();
14289 it != mHWData->mGuestProperties.end(); ++it)
14290 if ( (it->second.mFlags & guestProp::TRANSIENT)
14291 || (it->second.mFlags & guestProp::TRANSRESET))
14292 {
14293 fNeedsSaving = true;
14294 break;
14295 }
14296 if (fNeedsSaving)
14297 {
14298 mData->mCurrentStateModified = TRUE;
14299 stsFlags |= SaveSTS_CurStateModified;
14300 }
14301 }
14302#endif
14303
14304 rc = i_saveStateSettings(stsFlags);
14305
14306 if ( ( oldMachineState != MachineState_PoweredOff
14307 && oldMachineState != MachineState_Aborted
14308 && oldMachineState != MachineState_Teleported
14309 )
14310 && ( aMachineState == MachineState_PoweredOff
14311 || aMachineState == MachineState_Aborted
14312 || aMachineState == MachineState_Teleported
14313 )
14314 )
14315 {
14316 /* we've been shut down for any reason */
14317 /* no special action so far */
14318 }
14319
14320 LogFlowThisFunc(("rc=%Rhrc [%s]\n", rc, Global::stringifyMachineState(mData->mMachineState) ));
14321 LogFlowThisFuncLeave();
14322 return rc;
14323}
14324
14325/**
14326 * Sends the current machine state value to the VM process.
14327 *
14328 * @note Locks this object for reading, then calls a client process.
14329 */
14330HRESULT SessionMachine::i_updateMachineStateOnClient()
14331{
14332 AutoCaller autoCaller(this);
14333 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
14334
14335 ComPtr<IInternalSessionControl> directControl;
14336 {
14337 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
14338 AssertReturn(!!mData, E_FAIL);
14339 directControl = mData->mSession.mDirectControl;
14340
14341 /* directControl may be already set to NULL here in #OnSessionEnd()
14342 * called too early by the direct session process while there is still
14343 * some operation (like deleting the snapshot) in progress. The client
14344 * process in this case is waiting inside Session::close() for the
14345 * "end session" process object to complete, while #uninit() called by
14346 * #checkForDeath() on the Watcher thread is waiting for the pending
14347 * operation to complete. For now, we accept this inconsistent behavior
14348 * and simply do nothing here. */
14349
14350 if (mData->mSession.mState == SessionState_Unlocking)
14351 return S_OK;
14352
14353 AssertReturn(!directControl.isNull(), E_FAIL);
14354 }
14355
14356 return directControl->UpdateMachineState(mData->mMachineState);
14357}
14358
14359HRESULT Machine::setRemoveSavedStateFile(BOOL aRemove)
14360{
14361 NOREF(aRemove);
14362 ReturnComNotImplemented();
14363}
14364
14365HRESULT Machine::updateState(MachineState_T aState)
14366{
14367 NOREF(aState);
14368 ReturnComNotImplemented();
14369}
14370
14371HRESULT Machine::beginPowerUp(const ComPtr<IProgress> &aProgress)
14372{
14373 NOREF(aProgress);
14374 ReturnComNotImplemented();
14375}
14376
14377HRESULT Machine::endPowerUp(LONG aResult)
14378{
14379 NOREF(aResult);
14380 ReturnComNotImplemented();
14381}
14382
14383HRESULT Machine::beginPoweringDown(ComPtr<IProgress> &aProgress)
14384{
14385 NOREF(aProgress);
14386 ReturnComNotImplemented();
14387}
14388
14389HRESULT Machine::endPoweringDown(LONG aResult,
14390 const com::Utf8Str &aErrMsg)
14391{
14392 NOREF(aResult);
14393 NOREF(aErrMsg);
14394 ReturnComNotImplemented();
14395}
14396
14397HRESULT Machine::runUSBDeviceFilters(const ComPtr<IUSBDevice> &aDevice,
14398 BOOL *aMatched,
14399 ULONG *aMaskedInterfaces)
14400{
14401 NOREF(aDevice);
14402 NOREF(aMatched);
14403 NOREF(aMaskedInterfaces);
14404 ReturnComNotImplemented();
14405
14406}
14407
14408HRESULT Machine::captureUSBDevice(const com::Guid &aId)
14409{
14410 NOREF(aId);
14411 ReturnComNotImplemented();
14412}
14413
14414HRESULT Machine::detachUSBDevice(const com::Guid &aId,
14415 BOOL aDone)
14416{
14417 NOREF(aId);
14418 NOREF(aDone);
14419 ReturnComNotImplemented();
14420}
14421
14422HRESULT Machine::autoCaptureUSBDevices()
14423{
14424 ReturnComNotImplemented();
14425}
14426
14427HRESULT Machine::detachAllUSBDevices(BOOL aDone)
14428{
14429 NOREF(aDone);
14430 ReturnComNotImplemented();
14431}
14432
14433HRESULT Machine::onSessionEnd(const ComPtr<ISession> &aSession,
14434 ComPtr<IProgress> &aProgress)
14435{
14436 NOREF(aSession);
14437 NOREF(aProgress);
14438 ReturnComNotImplemented();
14439}
14440
14441HRESULT Machine::beginSavingState(ComPtr<IProgress> &aProgress,
14442 com::Utf8Str &aStateFilePath)
14443{
14444 NOREF(aProgress);
14445 NOREF(aStateFilePath);
14446 ReturnComNotImplemented();
14447}
14448
14449HRESULT Machine::endSavingState(LONG aResult,
14450 const com::Utf8Str &aErrMsg)
14451{
14452 NOREF(aResult);
14453 NOREF(aErrMsg);
14454 ReturnComNotImplemented();
14455}
14456
14457HRESULT Machine::adoptSavedState(const com::Utf8Str &aSavedStateFile)
14458{
14459 NOREF(aSavedStateFile);
14460 ReturnComNotImplemented();
14461}
14462
14463HRESULT Machine::beginTakingSnapshot(const ComPtr<IConsole> &aInitiator,
14464 const com::Utf8Str &aName,
14465 const com::Utf8Str &aDescription,
14466 const ComPtr<IProgress> &aConsoleProgress,
14467 BOOL aFTakingSnapshotOnline,
14468 com::Utf8Str &aStateFilePath)
14469{
14470 NOREF(aInitiator);
14471 NOREF(aName);
14472 NOREF(aDescription);
14473 NOREF(aConsoleProgress);
14474 NOREF(aFTakingSnapshotOnline);
14475 NOREF(aStateFilePath);
14476 ReturnComNotImplemented();
14477}
14478
14479HRESULT Machine::endTakingSnapshot(BOOL aSuccess)
14480{
14481 NOREF(aSuccess);
14482 ReturnComNotImplemented();
14483}
14484
14485HRESULT Machine::deleteSnapshot(const ComPtr<IConsole> &aInitiator,
14486 const com::Guid &aStartId,
14487 const com::Guid &aEndId,
14488 BOOL aDeleteAllChildren,
14489 MachineState_T *aMachineState,
14490 ComPtr<IProgress> &aProgress)
14491{
14492 NOREF(aInitiator);
14493 NOREF(aStartId);
14494 NOREF(aEndId);
14495 NOREF(aDeleteAllChildren);
14496 NOREF(aMachineState);
14497 NOREF(aProgress);
14498 ReturnComNotImplemented();
14499}
14500
14501HRESULT Machine::finishOnlineMergeMedium()
14502{
14503 ReturnComNotImplemented();
14504}
14505
14506HRESULT Machine::restoreSnapshot(const ComPtr<IConsole> &aInitiator,
14507 const ComPtr<ISnapshot> &aSnapshot,
14508 MachineState_T *aMachineState,
14509 ComPtr<IProgress> &aProgress)
14510{
14511 NOREF(aInitiator);
14512 NOREF(aSnapshot);
14513 NOREF(aMachineState);
14514 NOREF(aProgress);
14515 ReturnComNotImplemented();
14516}
14517
14518HRESULT Machine::pullGuestProperties(std::vector<com::Utf8Str> &aNames,
14519 std::vector<com::Utf8Str> &aValues,
14520 std::vector<LONG64> &aTimestamps,
14521 std::vector<com::Utf8Str> &aFlags)
14522{
14523 NOREF(aNames);
14524 NOREF(aValues);
14525 NOREF(aTimestamps);
14526 NOREF(aFlags);
14527 ReturnComNotImplemented();
14528}
14529
14530HRESULT Machine::pushGuestProperty(const com::Utf8Str &aName,
14531 const com::Utf8Str &aValue,
14532 LONG64 aTimestamp,
14533 const com::Utf8Str &aFlags)
14534{
14535 NOREF(aName);
14536 NOREF(aValue);
14537 NOREF(aTimestamp);
14538 NOREF(aFlags);
14539 ReturnComNotImplemented();
14540}
14541
14542HRESULT Machine::lockMedia()
14543{
14544 ReturnComNotImplemented();
14545}
14546
14547HRESULT Machine::unlockMedia()
14548{
14549 ReturnComNotImplemented();
14550}
14551
14552HRESULT Machine::ejectMedium(const ComPtr<IMediumAttachment> &aAttachment,
14553 ComPtr<IMediumAttachment> &aNewAttachment)
14554{
14555 NOREF(aAttachment);
14556 NOREF(aNewAttachment);
14557 ReturnComNotImplemented();
14558}
14559
14560HRESULT Machine::reportVmStatistics(ULONG aValidStats,
14561 ULONG aCpuUser,
14562 ULONG aCpuKernel,
14563 ULONG aCpuIdle,
14564 ULONG aMemTotal,
14565 ULONG aMemFree,
14566 ULONG aMemBalloon,
14567 ULONG aMemShared,
14568 ULONG aMemCache,
14569 ULONG aPagedTotal,
14570 ULONG aMemAllocTotal,
14571 ULONG aMemFreeTotal,
14572 ULONG aMemBalloonTotal,
14573 ULONG aMemSharedTotal,
14574 ULONG aVmNetRx,
14575 ULONG aVmNetTx)
14576{
14577 NOREF(aValidStats);
14578 NOREF(aCpuUser);
14579 NOREF(aCpuKernel);
14580 NOREF(aCpuIdle);
14581 NOREF(aMemTotal);
14582 NOREF(aMemFree);
14583 NOREF(aMemBalloon);
14584 NOREF(aMemShared);
14585 NOREF(aMemCache);
14586 NOREF(aPagedTotal);
14587 NOREF(aMemAllocTotal);
14588 NOREF(aMemFreeTotal);
14589 NOREF(aMemBalloonTotal);
14590 NOREF(aMemSharedTotal);
14591 NOREF(aVmNetRx);
14592 NOREF(aVmNetTx);
14593 ReturnComNotImplemented();
14594}
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette