VirtualBox

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

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

Main/Machine: avoid losing error info for OVF import failures, it replaced useful details by meaningless info what went wrong when closing the VM. Worth backporting!

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 507.5 KB
Line 
1/* $Id: MachineImpl.cpp 50435 2014-02-12 18:49:26Z vboxsync $ */
2/** @file
3 * Implementation of IMachine in VBoxSVC.
4 */
5
6/*
7 * Copyright (C) 2004-2013 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18/* Make sure all the stdint.h macros are included - must come first! */
19#ifndef __STDC_LIMIT_MACROS
20# define __STDC_LIMIT_MACROS
21#endif
22#ifndef __STDC_CONSTANT_MACROS
23# define __STDC_CONSTANT_MACROS
24#endif
25
26#include "Logging.h"
27#include "VirtualBoxImpl.h"
28#include "MachineImpl.h"
29#include "ClientToken.h"
30#include "ProgressImpl.h"
31#include "ProgressProxyImpl.h"
32#include "MediumAttachmentImpl.h"
33#include "MediumImpl.h"
34#include "MediumLock.h"
35#include "USBControllerImpl.h"
36#include "USBDeviceFiltersImpl.h"
37#include "HostImpl.h"
38#include "SharedFolderImpl.h"
39#include "GuestOSTypeImpl.h"
40#include "VirtualBoxErrorInfoImpl.h"
41#include "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 mVideoCaptureEnabled = false;
169 for (unsigned i = 0; i < RT_ELEMENTS(maVideoCaptureScreens); i++)
170 maVideoCaptureScreens[i] = true;
171
172 mHWVirtExEnabled = true;
173 mHWVirtExNestedPagingEnabled = true;
174#if HC_ARCH_BITS == 64 && !defined(RT_OS_LINUX)
175 mHWVirtExLargePagesEnabled = true;
176#else
177 /* Not supported on 32 bits hosts. */
178 mHWVirtExLargePagesEnabled = false;
179#endif
180 mHWVirtExVPIDEnabled = true;
181 mHWVirtExUXEnabled = true;
182 mHWVirtExForceEnabled = false;
183#if HC_ARCH_BITS == 64 || defined(RT_OS_WINDOWS) || defined(RT_OS_DARWIN)
184 mPAEEnabled = true;
185#else
186 mPAEEnabled = false;
187#endif
188 mLongMode = HC_ARCH_BITS == 64 ? settings::Hardware::LongMode_Enabled : settings::Hardware::LongMode_Disabled;
189 mSyntheticCpu = false;
190 mTripleFaultReset = false;
191 mHPETEnabled = false;
192
193 /* default boot order: floppy - DVD - HDD */
194 mBootOrder[0] = DeviceType_Floppy;
195 mBootOrder[1] = DeviceType_DVD;
196 mBootOrder[2] = DeviceType_HardDisk;
197 for (size_t i = 3; i < RT_ELEMENTS(mBootOrder); ++i)
198 mBootOrder[i] = DeviceType_Null;
199
200 mClipboardMode = ClipboardMode_Disabled;
201 mDragAndDropMode = DragAndDropMode_Disabled;
202 mGuestPropertyNotificationPatterns = "";
203
204 mFirmwareType = FirmwareType_BIOS;
205 mKeyboardHIDType = KeyboardHIDType_PS2Keyboard;
206 mPointingHIDType = PointingHIDType_PS2Mouse;
207 mChipsetType = ChipsetType_PIIX3;
208 mEmulatedUSBCardReaderEnabled = FALSE;
209
210 for (size_t i = 0; i < RT_ELEMENTS(mCPUAttached); i++)
211 mCPUAttached[i] = false;
212
213 mIOCacheEnabled = true;
214 mIOCacheSize = 5; /* 5MB */
215
216 /* Maximum CPU execution cap by default. */
217 mCpuExecutionCap = 100;
218}
219
220Machine::HWData::~HWData()
221{
222}
223
224/////////////////////////////////////////////////////////////////////////////
225// Machine::HDData structure
226/////////////////////////////////////////////////////////////////////////////
227
228Machine::MediaData::MediaData()
229{
230}
231
232Machine::MediaData::~MediaData()
233{
234}
235
236/////////////////////////////////////////////////////////////////////////////
237// Machine class
238/////////////////////////////////////////////////////////////////////////////
239
240// constructor / destructor
241/////////////////////////////////////////////////////////////////////////////
242
243Machine::Machine() :
244#ifdef VBOX_WITH_RESOURCE_USAGE_API
245 mCollectorGuest(NULL),
246#endif
247 mPeer(NULL),
248 mParent(NULL),
249 mSerialPorts(),
250 mParallelPorts(),
251 uRegistryNeedsSaving(0)
252{}
253
254Machine::~Machine()
255{}
256
257HRESULT Machine::FinalConstruct()
258{
259 LogFlowThisFunc(("\n"));
260 return BaseFinalConstruct();
261}
262
263void Machine::FinalRelease()
264{
265 LogFlowThisFunc(("\n"));
266 uninit();
267 BaseFinalRelease();
268}
269
270/**
271 * Initializes a new machine instance; this init() variant creates a new, empty machine.
272 * This gets called from VirtualBox::CreateMachine().
273 *
274 * @param aParent Associated parent object
275 * @param strConfigFile Local file system path to the VM settings file (can
276 * be relative to the VirtualBox config directory).
277 * @param strName name for the machine
278 * @param llGroups list of groups for the machine
279 * @param aOsType OS Type of this machine or NULL.
280 * @param aId UUID for the new machine.
281 * @param fForceOverwrite Whether to overwrite an existing machine settings file.
282 *
283 * @return Success indicator. if not S_OK, the machine object is invalid
284 */
285HRESULT Machine::init(VirtualBox *aParent,
286 const Utf8Str &strConfigFile,
287 const Utf8Str &strName,
288 const StringsList &llGroups,
289 GuestOSType *aOsType,
290 const Guid &aId,
291 bool fForceOverwrite,
292 bool fDirectoryIncludesUUID)
293{
294 LogFlowThisFuncEnter();
295 LogFlowThisFunc(("(Init_New) aConfigFile='%s'\n", strConfigFile.c_str()));
296
297 /* Enclose the state transition NotReady->InInit->Ready */
298 AutoInitSpan autoInitSpan(this);
299 AssertReturn(autoInitSpan.isOk(), E_FAIL);
300
301 HRESULT rc = initImpl(aParent, strConfigFile);
302 if (FAILED(rc)) return rc;
303
304 rc = tryCreateMachineConfigFile(fForceOverwrite);
305 if (FAILED(rc)) return rc;
306
307 if (SUCCEEDED(rc))
308 {
309 // create an empty machine config
310 mData->pMachineConfigFile = new settings::MachineConfigFile(NULL);
311
312 rc = initDataAndChildObjects();
313 }
314
315 if (SUCCEEDED(rc))
316 {
317 // set to true now to cause uninit() to call uninitDataAndChildObjects() on failure
318 mData->mAccessible = TRUE;
319
320 unconst(mData->mUuid) = aId;
321
322 mUserData->s.strName = strName;
323
324 mUserData->s.llGroups = llGroups;
325
326 mUserData->s.fDirectoryIncludesUUID = fDirectoryIncludesUUID;
327 // the "name sync" flag determines whether the machine directory gets renamed along
328 // with the machine file; say so if the settings file name is the same as the
329 // settings file parent directory (machine directory)
330 mUserData->s.fNameSync = isInOwnDir();
331
332 // initialize the default snapshots folder
333 rc = COMSETTER(SnapshotFolder)(NULL);
334 AssertComRC(rc);
335
336 if (aOsType)
337 {
338 /* Store OS type */
339 mUserData->s.strOsType = aOsType->i_id();
340
341 /* Apply BIOS defaults */
342 mBIOSSettings->i_applyDefaults(aOsType);
343
344 /* Apply network adapters defaults */
345 for (ULONG slot = 0; slot < mNetworkAdapters.size(); ++slot)
346 mNetworkAdapters[slot]->i_applyDefaults(aOsType);
347
348 /* Apply serial port defaults */
349 for (ULONG slot = 0; slot < RT_ELEMENTS(mSerialPorts); ++slot)
350 mSerialPorts[slot]->i_applyDefaults(aOsType);
351
352 /* Let the OS type select 64-bit ness. */
353 mHWData->mLongMode = aOsType->i_is64Bit()
354 ? settings::Hardware::LongMode_Enabled : settings::Hardware::LongMode_Disabled;
355 }
356
357 /* At this point the changing of the current state modification
358 * flag is allowed. */
359 allowStateModification();
360
361 /* commit all changes made during the initialization */
362 commit();
363 }
364
365 /* Confirm a successful initialization when it's the case */
366 if (SUCCEEDED(rc))
367 {
368 if (mData->mAccessible)
369 autoInitSpan.setSucceeded();
370 else
371 autoInitSpan.setLimited();
372 }
373
374 LogFlowThisFunc(("mName='%s', mRegistered=%RTbool, mAccessible=%RTbool, rc=%08X\n",
375 !!mUserData ? mUserData->s.strName.c_str() : "NULL",
376 mData->mRegistered,
377 mData->mAccessible,
378 rc));
379
380 LogFlowThisFuncLeave();
381
382 return rc;
383}
384
385/**
386 * Initializes a new instance with data from machine XML (formerly Init_Registered).
387 * Gets called in two modes:
388 *
389 * -- from VirtualBox::initMachines() during VirtualBox startup; in that case, the
390 * UUID is specified and we mark the machine as "registered";
391 *
392 * -- from the public VirtualBox::OpenMachine() API, in which case the UUID is NULL
393 * and the machine remains unregistered until RegisterMachine() is called.
394 *
395 * @param aParent Associated parent object
396 * @param aConfigFile Local file system path to the VM settings file (can
397 * be relative to the VirtualBox config directory).
398 * @param aId UUID of the machine or NULL (see above).
399 *
400 * @return Success indicator. if not S_OK, the machine object is invalid
401 */
402HRESULT Machine::initFromSettings(VirtualBox *aParent,
403 const Utf8Str &strConfigFile,
404 const Guid *aId)
405{
406 LogFlowThisFuncEnter();
407 LogFlowThisFunc(("(Init_Registered) aConfigFile='%s\n", strConfigFile.c_str()));
408
409 /* Enclose the state transition NotReady->InInit->Ready */
410 AutoInitSpan autoInitSpan(this);
411 AssertReturn(autoInitSpan.isOk(), E_FAIL);
412
413 HRESULT rc = initImpl(aParent, strConfigFile);
414 if (FAILED(rc)) return rc;
415
416 if (aId)
417 {
418 // loading a registered VM:
419 unconst(mData->mUuid) = *aId;
420 mData->mRegistered = TRUE;
421 // now load the settings from XML:
422 rc = registeredInit();
423 // this calls initDataAndChildObjects() and loadSettings()
424 }
425 else
426 {
427 // opening an unregistered VM (VirtualBox::OpenMachine()):
428 rc = initDataAndChildObjects();
429
430 if (SUCCEEDED(rc))
431 {
432 // set to true now to cause uninit() to call uninitDataAndChildObjects() on failure
433 mData->mAccessible = TRUE;
434
435 try
436 {
437 // load and parse machine XML; this will throw on XML or logic errors
438 mData->pMachineConfigFile = new settings::MachineConfigFile(&mData->m_strConfigFileFull);
439
440 // reject VM UUID duplicates, they can happen if someone
441 // tries to register an already known VM config again
442 if (aParent->i_findMachine(mData->pMachineConfigFile->uuid,
443 true /* fPermitInaccessible */,
444 false /* aDoSetError */,
445 NULL) != VBOX_E_OBJECT_NOT_FOUND)
446 {
447 throw setError(E_FAIL,
448 tr("Trying to open a VM config '%s' which has the same UUID as an existing virtual machine"),
449 mData->m_strConfigFile.c_str());
450 }
451
452 // use UUID from machine config
453 unconst(mData->mUuid) = mData->pMachineConfigFile->uuid;
454
455 rc = loadMachineDataFromSettings(*mData->pMachineConfigFile,
456 NULL /* puuidRegistry */);
457 if (FAILED(rc)) throw rc;
458
459 /* At this point the changing of the current state modification
460 * flag is allowed. */
461 allowStateModification();
462
463 commit();
464 }
465 catch (HRESULT err)
466 {
467 /* we assume that error info is set by the thrower */
468 rc = err;
469 }
470 catch (...)
471 {
472 rc = VirtualBoxBase::handleUnexpectedExceptions(this, RT_SRC_POS);
473 }
474 }
475 }
476
477 /* Confirm a successful initialization when it's the case */
478 if (SUCCEEDED(rc))
479 {
480 if (mData->mAccessible)
481 autoInitSpan.setSucceeded();
482 else
483 {
484 autoInitSpan.setLimited();
485
486 // uninit media from this machine's media registry, or else
487 // reloading the settings will fail
488 mParent->i_unregisterMachineMedia(getId());
489 }
490 }
491
492 LogFlowThisFunc(("mName='%s', mRegistered=%RTbool, mAccessible=%RTbool "
493 "rc=%08X\n",
494 !!mUserData ? mUserData->s.strName.c_str() : "NULL",
495 mData->mRegistered, mData->mAccessible, rc));
496
497 LogFlowThisFuncLeave();
498
499 return rc;
500}
501
502/**
503 * Initializes a new instance from a machine config that is already in memory
504 * (import OVF case). Since we are importing, the UUID in the machine
505 * config is ignored and we always generate a fresh one.
506 *
507 * @param strName Name for the new machine; this overrides what is specified in config and is used
508 * for the settings file as well.
509 * @param config Machine configuration loaded and parsed from XML.
510 *
511 * @return Success indicator. if not S_OK, the machine object is invalid
512 */
513HRESULT Machine::init(VirtualBox *aParent,
514 const Utf8Str &strName,
515 const settings::MachineConfigFile &config)
516{
517 LogFlowThisFuncEnter();
518
519 /* Enclose the state transition NotReady->InInit->Ready */
520 AutoInitSpan autoInitSpan(this);
521 AssertReturn(autoInitSpan.isOk(), E_FAIL);
522
523 Utf8Str strConfigFile;
524 aParent->i_getDefaultMachineFolder(strConfigFile);
525 strConfigFile.append(RTPATH_DELIMITER);
526 strConfigFile.append(strName);
527 strConfigFile.append(RTPATH_DELIMITER);
528 strConfigFile.append(strName);
529 strConfigFile.append(".vbox");
530
531 HRESULT rc = initImpl(aParent, strConfigFile);
532 if (FAILED(rc)) return rc;
533
534 rc = tryCreateMachineConfigFile(false /* fForceOverwrite */);
535 if (FAILED(rc)) return rc;
536
537 rc = initDataAndChildObjects();
538
539 if (SUCCEEDED(rc))
540 {
541 // set to true now to cause uninit() to call uninitDataAndChildObjects() on failure
542 mData->mAccessible = TRUE;
543
544 // create empty machine config for instance data
545 mData->pMachineConfigFile = new settings::MachineConfigFile(NULL);
546
547 // generate fresh UUID, ignore machine config
548 unconst(mData->mUuid).create();
549
550 rc = loadMachineDataFromSettings(config,
551 &mData->mUuid); // puuidRegistry: initialize media with this registry ID
552
553 // override VM name as well, it may be different
554 mUserData->s.strName = strName;
555
556 if (SUCCEEDED(rc))
557 {
558 /* At this point the changing of the current state modification
559 * flag is allowed. */
560 allowStateModification();
561
562 /* commit all changes made during the initialization */
563 commit();
564 }
565 }
566
567 /* Confirm a successful initialization when it's the case */
568 if (SUCCEEDED(rc))
569 {
570 if (mData->mAccessible)
571 autoInitSpan.setSucceeded();
572 else
573 {
574 /* Ignore all errors from unregistering, they would destroy
575 * the more interesting error information we already have,
576 * pinpointing the issue with the VM config. */
577 ErrorInfoKeeper eik;
578 autoInitSpan.setLimited();
579
580 // uninit media from this machine's media registry, or else
581 // reloading the settings will fail
582 mParent->i_unregisterMachineMedia(getId());
583 }
584 }
585
586 LogFlowThisFunc(("mName='%s', mRegistered=%RTbool, mAccessible=%RTbool "
587 "rc=%08X\n",
588 !!mUserData ? mUserData->s.strName.c_str() : "NULL",
589 mData->mRegistered, mData->mAccessible, rc));
590
591 LogFlowThisFuncLeave();
592
593 return rc;
594}
595
596/**
597 * Shared code between the various init() implementations.
598 * @param aParent
599 * @return
600 */
601HRESULT Machine::initImpl(VirtualBox *aParent,
602 const Utf8Str &strConfigFile)
603{
604 LogFlowThisFuncEnter();
605
606 AssertReturn(aParent, E_INVALIDARG);
607 AssertReturn(!strConfigFile.isEmpty(), E_INVALIDARG);
608
609 HRESULT rc = S_OK;
610
611 /* share the parent weakly */
612 unconst(mParent) = aParent;
613
614 /* allocate the essential machine data structure (the rest will be
615 * allocated later by initDataAndChildObjects() */
616 mData.allocate();
617
618 /* memorize the config file name (as provided) */
619 mData->m_strConfigFile = strConfigFile;
620
621 /* get the full file name */
622 int vrc1 = mParent->i_calculateFullPath(strConfigFile, mData->m_strConfigFileFull);
623 if (RT_FAILURE(vrc1))
624 return setError(VBOX_E_FILE_ERROR,
625 tr("Invalid machine settings file name '%s' (%Rrc)"),
626 strConfigFile.c_str(),
627 vrc1);
628
629 LogFlowThisFuncLeave();
630
631 return rc;
632}
633
634/**
635 * Tries to create a machine settings file in the path stored in the machine
636 * instance data. Used when a new machine is created to fail gracefully if
637 * the settings file could not be written (e.g. because machine dir is read-only).
638 * @return
639 */
640HRESULT Machine::tryCreateMachineConfigFile(bool fForceOverwrite)
641{
642 HRESULT rc = S_OK;
643
644 // when we create a new machine, we must be able to create the settings file
645 RTFILE f = NIL_RTFILE;
646 int vrc = RTFileOpen(&f, mData->m_strConfigFileFull.c_str(), RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
647 if ( RT_SUCCESS(vrc)
648 || vrc == VERR_SHARING_VIOLATION
649 )
650 {
651 if (RT_SUCCESS(vrc))
652 RTFileClose(f);
653 if (!fForceOverwrite)
654 rc = setError(VBOX_E_FILE_ERROR,
655 tr("Machine settings file '%s' already exists"),
656 mData->m_strConfigFileFull.c_str());
657 else
658 {
659 /* try to delete the config file, as otherwise the creation
660 * of a new settings file will fail. */
661 int vrc2 = RTFileDelete(mData->m_strConfigFileFull.c_str());
662 if (RT_FAILURE(vrc2))
663 rc = setError(VBOX_E_FILE_ERROR,
664 tr("Could not delete the existing settings file '%s' (%Rrc)"),
665 mData->m_strConfigFileFull.c_str(), vrc2);
666 }
667 }
668 else if ( vrc != VERR_FILE_NOT_FOUND
669 && vrc != VERR_PATH_NOT_FOUND
670 )
671 rc = setError(VBOX_E_FILE_ERROR,
672 tr("Invalid machine settings file name '%s' (%Rrc)"),
673 mData->m_strConfigFileFull.c_str(),
674 vrc);
675 return rc;
676}
677
678/**
679 * Initializes the registered machine by loading the settings file.
680 * This method is separated from #init() in order to make it possible to
681 * retry the operation after VirtualBox startup instead of refusing to
682 * startup the whole VirtualBox server in case if the settings file of some
683 * registered VM is invalid or inaccessible.
684 *
685 * @note Must be always called from this object's write lock
686 * (unless called from #init() that doesn't need any locking).
687 * @note Locks the mUSBController method for writing.
688 * @note Subclasses must not call this method.
689 */
690HRESULT Machine::registeredInit()
691{
692 AssertReturn(!isSessionMachine(), E_FAIL);
693 AssertReturn(!isSnapshotMachine(), E_FAIL);
694 AssertReturn(mData->mUuid.isValid(), E_FAIL);
695 AssertReturn(!mData->mAccessible, E_FAIL);
696
697 HRESULT rc = initDataAndChildObjects();
698
699 if (SUCCEEDED(rc))
700 {
701 /* Temporarily reset the registered flag in order to let setters
702 * potentially called from loadSettings() succeed (isMutable() used in
703 * all setters will return FALSE for a Machine instance if mRegistered
704 * is TRUE). */
705 mData->mRegistered = FALSE;
706
707 try
708 {
709 // load and parse machine XML; this will throw on XML or logic errors
710 mData->pMachineConfigFile = new settings::MachineConfigFile(&mData->m_strConfigFileFull);
711
712 if (mData->mUuid != mData->pMachineConfigFile->uuid)
713 throw setError(E_FAIL,
714 tr("Machine UUID {%RTuuid} in '%s' doesn't match its UUID {%s} in the registry file '%s'"),
715 mData->pMachineConfigFile->uuid.raw(),
716 mData->m_strConfigFileFull.c_str(),
717 mData->mUuid.toString().c_str(),
718 mParent->i_settingsFilePath().c_str());
719
720 rc = loadMachineDataFromSettings(*mData->pMachineConfigFile,
721 NULL /* const Guid *puuidRegistry */);
722 if (FAILED(rc)) throw rc;
723 }
724 catch (HRESULT err)
725 {
726 /* we assume that error info is set by the thrower */
727 rc = err;
728 }
729 catch (...)
730 {
731 rc = VirtualBoxBase::handleUnexpectedExceptions(this, RT_SRC_POS);
732 }
733
734 /* Restore the registered flag (even on failure) */
735 mData->mRegistered = TRUE;
736 }
737
738 if (SUCCEEDED(rc))
739 {
740 /* Set mAccessible to TRUE only if we successfully locked and loaded
741 * the settings file */
742 mData->mAccessible = TRUE;
743
744 /* commit all changes made during loading the settings file */
745 commit(); // @todo r=dj why do we need a commit during init?!? this is very expensive
746 /// @todo r=klaus for some reason the settings loading logic backs up
747 // the settings, and therefore a commit is needed. Should probably be changed.
748 }
749 else
750 {
751 /* If the machine is registered, then, instead of returning a
752 * failure, we mark it as inaccessible and set the result to
753 * success to give it a try later */
754
755 /* fetch the current error info */
756 mData->mAccessError = com::ErrorInfo();
757 LogWarning(("Machine {%RTuuid} is inaccessible! [%ls]\n",
758 mData->mUuid.raw(),
759 mData->mAccessError.getText().raw()));
760
761 /* rollback all changes */
762 rollback(false /* aNotify */);
763
764 // uninit media from this machine's media registry, or else
765 // reloading the settings will fail
766 mParent->i_unregisterMachineMedia(getId());
767
768 /* uninitialize the common part to make sure all data is reset to
769 * default (null) values */
770 uninitDataAndChildObjects();
771
772 rc = S_OK;
773 }
774
775 return rc;
776}
777
778/**
779 * Uninitializes the instance.
780 * Called either from FinalRelease() or by the parent when it gets destroyed.
781 *
782 * @note The caller of this method must make sure that this object
783 * a) doesn't have active callers on the current thread and b) is not locked
784 * by the current thread; otherwise uninit() will hang either a) due to
785 * AutoUninitSpan waiting for a number of calls to drop to zero or b) due to
786 * a dead-lock caused by this thread waiting for all callers on the other
787 * threads are done but preventing them from doing so by holding a lock.
788 */
789void Machine::uninit()
790{
791 LogFlowThisFuncEnter();
792
793 Assert(!isWriteLockOnCurrentThread());
794
795 Assert(!uRegistryNeedsSaving);
796 if (uRegistryNeedsSaving)
797 {
798 AutoCaller autoCaller(this);
799 if (SUCCEEDED(autoCaller.rc()))
800 {
801 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
802 saveSettings(NULL, Machine::SaveS_Force);
803 }
804 }
805
806 /* Enclose the state transition Ready->InUninit->NotReady */
807 AutoUninitSpan autoUninitSpan(this);
808 if (autoUninitSpan.uninitDone())
809 return;
810
811 Assert(!isSnapshotMachine());
812 Assert(!isSessionMachine());
813 Assert(!!mData);
814
815 LogFlowThisFunc(("initFailed()=%d\n", autoUninitSpan.initFailed()));
816 LogFlowThisFunc(("mRegistered=%d\n", mData->mRegistered));
817
818 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
819
820 if (!mData->mSession.mMachine.isNull())
821 {
822 /* Theoretically, this can only happen if the VirtualBox server has been
823 * terminated while there were clients running that owned open direct
824 * sessions. Since in this case we are definitely called by
825 * VirtualBox::uninit(), we may be sure that SessionMachine::uninit()
826 * won't happen on the client watcher thread (because it does
827 * VirtualBox::addCaller() for the duration of the
828 * SessionMachine::checkForDeath() call, so that VirtualBox::uninit()
829 * cannot happen until the VirtualBox caller is released). This is
830 * important, because SessionMachine::uninit() cannot correctly operate
831 * after we return from this method (it expects the Machine instance is
832 * still valid). We'll call it ourselves below.
833 */
834 LogWarningThisFunc(("Session machine is not NULL (%p), the direct session is still open!\n",
835 (SessionMachine*)mData->mSession.mMachine));
836
837 if (Global::IsOnlineOrTransient(mData->mMachineState))
838 {
839 LogWarningThisFunc(("Setting state to Aborted!\n"));
840 /* set machine state using SessionMachine reimplementation */
841 static_cast<Machine*>(mData->mSession.mMachine)->setMachineState(MachineState_Aborted);
842 }
843
844 /*
845 * Uninitialize SessionMachine using public uninit() to indicate
846 * an unexpected uninitialization.
847 */
848 mData->mSession.mMachine->uninit();
849 /* SessionMachine::uninit() must set mSession.mMachine to null */
850 Assert(mData->mSession.mMachine.isNull());
851 }
852
853 // uninit media from this machine's media registry, if they're still there
854 Guid uuidMachine(getId());
855
856 /* the lock is no more necessary (SessionMachine is uninitialized) */
857 alock.release();
858
859 /* XXX This will fail with
860 * "cannot be closed because it is still attached to 1 virtual machines"
861 * because at this point we did not call uninitDataAndChildObjects() yet
862 * and therefore also removeBackReference() for all these mediums was not called! */
863
864 if (uuidMachine.isValid() && !uuidMachine.isZero()) // can be empty if we're called from a failure of Machine::init
865 mParent->i_unregisterMachineMedia(uuidMachine);
866
867 // has machine been modified?
868 if (mData->flModifications)
869 {
870 LogWarningThisFunc(("Discarding unsaved settings changes!\n"));
871 rollback(false /* aNotify */);
872 }
873
874 if (mData->mAccessible)
875 uninitDataAndChildObjects();
876
877 /* free the essential data structure last */
878 mData.free();
879
880 LogFlowThisFuncLeave();
881}
882
883// IMachine properties
884/////////////////////////////////////////////////////////////////////////////
885
886STDMETHODIMP Machine::COMGETTER(Parent)(IVirtualBox **aParent)
887{
888 CheckComArgOutPointerValid(aParent);
889
890 AutoLimitedCaller autoCaller(this);
891 if (FAILED(autoCaller.rc())) return autoCaller.rc();
892
893 /* mParent is constant during life time, no need to lock */
894 ComObjPtr<VirtualBox> pVirtualBox(mParent);
895 pVirtualBox.queryInterfaceTo(aParent);
896
897 return S_OK;
898}
899
900STDMETHODIMP Machine::COMGETTER(Accessible)(BOOL *aAccessible)
901{
902 CheckComArgOutPointerValid(aAccessible);
903
904 AutoLimitedCaller autoCaller(this);
905 if (FAILED(autoCaller.rc())) return autoCaller.rc();
906
907 LogFlowThisFunc(("ENTER\n"));
908
909 /* In some cases (medium registry related), it is necessary to be able to
910 * go through the list of all machines. Happens when an inaccessible VM
911 * has a sensible medium registry. */
912 AutoReadLock mllock(mParent->i_getMachinesListLockHandle() COMMA_LOCKVAL_SRC_POS);
913 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
914
915 HRESULT rc = S_OK;
916
917 if (!mData->mAccessible)
918 {
919 /* try to initialize the VM once more if not accessible */
920
921 AutoReinitSpan autoReinitSpan(this);
922 AssertReturn(autoReinitSpan.isOk(), E_FAIL);
923
924#ifdef DEBUG
925 LogFlowThisFunc(("Dumping media backreferences\n"));
926 mParent->i_dumpAllBackRefs();
927#endif
928
929 if (mData->pMachineConfigFile)
930 {
931 // reset the XML file to force loadSettings() (called from registeredInit())
932 // to parse it again; the file might have changed
933 delete mData->pMachineConfigFile;
934 mData->pMachineConfigFile = NULL;
935 }
936
937 rc = registeredInit();
938
939 if (SUCCEEDED(rc) && mData->mAccessible)
940 {
941 autoReinitSpan.setSucceeded();
942
943 /* make sure interesting parties will notice the accessibility
944 * state change */
945 mParent->i_onMachineStateChange(mData->mUuid, mData->mMachineState);
946 mParent->i_onMachineDataChange(mData->mUuid);
947 }
948 }
949
950 if (SUCCEEDED(rc))
951 *aAccessible = mData->mAccessible;
952
953 LogFlowThisFuncLeave();
954
955 return rc;
956}
957
958STDMETHODIMP Machine::COMGETTER(AccessError)(IVirtualBoxErrorInfo **aAccessError)
959{
960 CheckComArgOutPointerValid(aAccessError);
961
962 AutoLimitedCaller autoCaller(this);
963 if (FAILED(autoCaller.rc())) return autoCaller.rc();
964
965 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
966
967 if (mData->mAccessible || !mData->mAccessError.isBasicAvailable())
968 {
969 /* return shortly */
970 aAccessError = NULL;
971 return S_OK;
972 }
973
974 HRESULT rc = S_OK;
975
976 ComObjPtr<VirtualBoxErrorInfo> errorInfo;
977 rc = errorInfo.createObject();
978 if (SUCCEEDED(rc))
979 {
980 errorInfo->init(mData->mAccessError.getResultCode(),
981 mData->mAccessError.getInterfaceID().ref(),
982 Utf8Str(mData->mAccessError.getComponent()).c_str(),
983 Utf8Str(mData->mAccessError.getText()));
984 rc = errorInfo.queryInterfaceTo(aAccessError);
985 }
986
987 return rc;
988}
989
990STDMETHODIMP Machine::COMGETTER(Name)(BSTR *aName)
991{
992 CheckComArgOutPointerValid(aName);
993
994 AutoCaller autoCaller(this);
995 if (FAILED(autoCaller.rc())) return autoCaller.rc();
996
997 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
998
999 mUserData->s.strName.cloneTo(aName);
1000
1001 return S_OK;
1002}
1003
1004STDMETHODIMP Machine::COMSETTER(Name)(IN_BSTR aName)
1005{
1006 CheckComArgStrNotEmptyOrNull(aName);
1007
1008 AutoCaller autoCaller(this);
1009 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1010
1011 // prohibit setting a UUID only as the machine name, or else it can
1012 // never be found by findMachine()
1013 Guid test(aName);
1014
1015 if (test.isValid())
1016 return setError(E_INVALIDARG, tr("A machine cannot have a UUID as its name"));
1017
1018 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1019
1020 HRESULT rc = checkStateDependency(MutableStateDep);
1021 if (FAILED(rc)) return rc;
1022
1023 setModified(IsModified_MachineData);
1024 mUserData.backup();
1025 mUserData->s.strName = aName;
1026
1027 return S_OK;
1028}
1029
1030STDMETHODIMP Machine::COMGETTER(Description)(BSTR *aDescription)
1031{
1032 CheckComArgOutPointerValid(aDescription);
1033
1034 AutoCaller autoCaller(this);
1035 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1036
1037 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1038
1039 mUserData->s.strDescription.cloneTo(aDescription);
1040
1041 return S_OK;
1042}
1043
1044STDMETHODIMP Machine::COMSETTER(Description)(IN_BSTR aDescription)
1045{
1046 AutoCaller autoCaller(this);
1047 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1048
1049 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1050
1051 // this can be done in principle in any state as it doesn't affect the VM
1052 // significantly, but play safe by not messing around while complex
1053 // activities are going on
1054 HRESULT rc = checkStateDependency(MutableOrSavedStateDep);
1055 if (FAILED(rc)) return rc;
1056
1057 setModified(IsModified_MachineData);
1058 mUserData.backup();
1059 mUserData->s.strDescription = aDescription;
1060
1061 return S_OK;
1062}
1063
1064STDMETHODIMP Machine::COMGETTER(Id)(BSTR *aId)
1065{
1066 CheckComArgOutPointerValid(aId);
1067
1068 AutoLimitedCaller autoCaller(this);
1069 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1070
1071 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1072
1073 mData->mUuid.toUtf16().cloneTo(aId);
1074
1075 return S_OK;
1076}
1077
1078STDMETHODIMP Machine::COMGETTER(Groups)(ComSafeArrayOut(BSTR, aGroups))
1079{
1080 CheckComArgOutSafeArrayPointerValid(aGroups);
1081
1082 AutoCaller autoCaller(this);
1083 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1084
1085 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1086 SafeArray<BSTR> groups(mUserData->s.llGroups.size());
1087 size_t i = 0;
1088 for (StringsList::const_iterator it = mUserData->s.llGroups.begin();
1089 it != mUserData->s.llGroups.end();
1090 ++it, i++)
1091 {
1092 Bstr tmp = *it;
1093 tmp.cloneTo(&groups[i]);
1094 }
1095 groups.detachTo(ComSafeArrayOutArg(aGroups));
1096
1097 return S_OK;
1098}
1099
1100STDMETHODIMP Machine::COMSETTER(Groups)(ComSafeArrayIn(IN_BSTR, aGroups))
1101{
1102 AutoCaller autoCaller(this);
1103 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1104 std::vector<com::Utf8Str> tmplist;
1105 SafeArray<IN_BSTR> tmp(ComSafeArrayInArg(aGroups));
1106 tmplist.resize(tmp.size());
1107 for (size_t i = 0; i < tmp.size(); ++i)
1108 tmplist[i] = Utf8Str(tmp[i]);
1109
1110 StringsList llGroups;
1111 HRESULT rc = mParent->i_convertMachineGroups(tmplist, &llGroups);
1112 if (FAILED(rc))
1113 return rc;
1114
1115 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1116
1117 // changing machine groups is possible while the VM is offline
1118 rc = checkStateDependency(OfflineStateDep);
1119 if (FAILED(rc)) return rc;
1120
1121 setModified(IsModified_MachineData);
1122 mUserData.backup();
1123 mUserData->s.llGroups = llGroups;
1124
1125 return S_OK;
1126}
1127
1128STDMETHODIMP Machine::COMGETTER(OSTypeId)(BSTR *aOSTypeId)
1129{
1130 CheckComArgOutPointerValid(aOSTypeId);
1131
1132 AutoCaller autoCaller(this);
1133 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1134
1135 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1136
1137 mUserData->s.strOsType.cloneTo(aOSTypeId);
1138
1139 return S_OK;
1140}
1141
1142STDMETHODIMP Machine::COMSETTER(OSTypeId)(IN_BSTR aOSTypeId)
1143{
1144 CheckComArgStrNotEmptyOrNull(aOSTypeId);
1145
1146 AutoCaller autoCaller(this);
1147 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1148
1149 /* look up the object by Id to check it is valid */
1150 ComPtr<IGuestOSType> guestOSType;
1151 HRESULT rc = mParent->GetGuestOSType(aOSTypeId, guestOSType.asOutParam());
1152 if (FAILED(rc)) return rc;
1153
1154 /* when setting, always use the "etalon" value for consistency -- lookup
1155 * by ID is case-insensitive and the input value may have different case */
1156 Bstr osTypeId;
1157 rc = guestOSType->COMGETTER(Id)(osTypeId.asOutParam());
1158 if (FAILED(rc)) return rc;
1159
1160 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1161
1162 rc = checkStateDependency(MutableStateDep);
1163 if (FAILED(rc)) return rc;
1164
1165 setModified(IsModified_MachineData);
1166 mUserData.backup();
1167 mUserData->s.strOsType = osTypeId;
1168
1169 return S_OK;
1170}
1171
1172
1173STDMETHODIMP Machine::COMGETTER(FirmwareType)(FirmwareType_T *aFirmwareType)
1174{
1175 CheckComArgOutPointerValid(aFirmwareType);
1176
1177 AutoCaller autoCaller(this);
1178 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1179
1180 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1181
1182 *aFirmwareType = mHWData->mFirmwareType;
1183
1184 return S_OK;
1185}
1186
1187STDMETHODIMP Machine::COMSETTER(FirmwareType)(FirmwareType_T aFirmwareType)
1188{
1189 AutoCaller autoCaller(this);
1190 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1191 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1192
1193 HRESULT rc = checkStateDependency(MutableStateDep);
1194 if (FAILED(rc)) return rc;
1195
1196 setModified(IsModified_MachineData);
1197 mHWData.backup();
1198 mHWData->mFirmwareType = aFirmwareType;
1199
1200 return S_OK;
1201}
1202
1203STDMETHODIMP Machine::COMGETTER(KeyboardHIDType)(KeyboardHIDType_T *aKeyboardHIDType)
1204{
1205 CheckComArgOutPointerValid(aKeyboardHIDType);
1206
1207 AutoCaller autoCaller(this);
1208 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1209
1210 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1211
1212 *aKeyboardHIDType = mHWData->mKeyboardHIDType;
1213
1214 return S_OK;
1215}
1216
1217STDMETHODIMP Machine::COMSETTER(KeyboardHIDType)(KeyboardHIDType_T aKeyboardHIDType)
1218{
1219 AutoCaller autoCaller(this);
1220 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1221 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1222
1223 HRESULT rc = checkStateDependency(MutableStateDep);
1224 if (FAILED(rc)) return rc;
1225
1226 setModified(IsModified_MachineData);
1227 mHWData.backup();
1228 mHWData->mKeyboardHIDType = aKeyboardHIDType;
1229
1230 return S_OK;
1231}
1232
1233STDMETHODIMP Machine::COMGETTER(PointingHIDType)(PointingHIDType_T *aPointingHIDType)
1234{
1235 CheckComArgOutPointerValid(aPointingHIDType);
1236
1237 AutoCaller autoCaller(this);
1238 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1239
1240 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1241
1242 *aPointingHIDType = mHWData->mPointingHIDType;
1243
1244 return S_OK;
1245}
1246
1247STDMETHODIMP Machine::COMSETTER(PointingHIDType)(PointingHIDType_T aPointingHIDType)
1248{
1249 AutoCaller autoCaller(this);
1250 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1251 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1252
1253 HRESULT rc = checkStateDependency(MutableStateDep);
1254 if (FAILED(rc)) return rc;
1255
1256 setModified(IsModified_MachineData);
1257 mHWData.backup();
1258 mHWData->mPointingHIDType = aPointingHIDType;
1259
1260 return S_OK;
1261}
1262
1263STDMETHODIMP Machine::COMGETTER(ChipsetType)(ChipsetType_T *aChipsetType)
1264{
1265 CheckComArgOutPointerValid(aChipsetType);
1266
1267 AutoCaller autoCaller(this);
1268 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1269
1270 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1271
1272 *aChipsetType = mHWData->mChipsetType;
1273
1274 return S_OK;
1275}
1276
1277STDMETHODIMP Machine::COMSETTER(ChipsetType)(ChipsetType_T aChipsetType)
1278{
1279 AutoCaller autoCaller(this);
1280 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1281 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1282
1283 HRESULT rc = checkStateDependency(MutableStateDep);
1284 if (FAILED(rc)) return rc;
1285
1286 if (aChipsetType != mHWData->mChipsetType)
1287 {
1288 setModified(IsModified_MachineData);
1289 mHWData.backup();
1290 mHWData->mChipsetType = aChipsetType;
1291
1292 // Resize network adapter array, to be finalized on commit/rollback.
1293 // We must not throw away entries yet, otherwise settings are lost
1294 // without a way to roll back.
1295 size_t newCount = Global::getMaxNetworkAdapters(aChipsetType);
1296 size_t oldCount = mNetworkAdapters.size();
1297 if (newCount > oldCount)
1298 {
1299 mNetworkAdapters.resize(newCount);
1300 for (ULONG slot = oldCount; slot < mNetworkAdapters.size(); slot++)
1301 {
1302 unconst(mNetworkAdapters[slot]).createObject();
1303 mNetworkAdapters[slot]->init(this, slot);
1304 }
1305 }
1306 }
1307
1308 return S_OK;
1309}
1310
1311STDMETHODIMP Machine::COMGETTER(HardwareVersion)(BSTR *aHWVersion)
1312{
1313 CheckComArgOutPointerValid(aHWVersion);
1314
1315 AutoCaller autoCaller(this);
1316 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1317
1318 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1319
1320 mHWData->mHWVersion.cloneTo(aHWVersion);
1321
1322 return S_OK;
1323}
1324
1325STDMETHODIMP Machine::COMSETTER(HardwareVersion)(IN_BSTR aHWVersion)
1326{
1327 /* check known version */
1328 Utf8Str hwVersion = aHWVersion;
1329 if ( hwVersion.compare("1") != 0
1330 && hwVersion.compare("2") != 0)
1331 return setError(E_INVALIDARG,
1332 tr("Invalid hardware version: %ls\n"), aHWVersion);
1333
1334 AutoCaller autoCaller(this);
1335 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1336
1337 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1338
1339 HRESULT rc = checkStateDependency(MutableStateDep);
1340 if (FAILED(rc)) return rc;
1341
1342 setModified(IsModified_MachineData);
1343 mHWData.backup();
1344 mHWData->mHWVersion = hwVersion;
1345
1346 return S_OK;
1347}
1348
1349STDMETHODIMP Machine::COMGETTER(HardwareUUID)(BSTR *aUUID)
1350{
1351 CheckComArgOutPointerValid(aUUID);
1352
1353 AutoCaller autoCaller(this);
1354 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1355
1356 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1357
1358 if (!mHWData->mHardwareUUID.isZero())
1359 mHWData->mHardwareUUID.toUtf16().cloneTo(aUUID);
1360 else
1361 mData->mUuid.toUtf16().cloneTo(aUUID);
1362
1363 return S_OK;
1364}
1365
1366STDMETHODIMP Machine::COMSETTER(HardwareUUID)(IN_BSTR aUUID)
1367{
1368 Guid hardwareUUID(aUUID);
1369 if (!hardwareUUID.isValid())
1370 return E_INVALIDARG;
1371
1372 AutoCaller autoCaller(this);
1373 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1374
1375 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1376
1377 HRESULT rc = checkStateDependency(MutableStateDep);
1378 if (FAILED(rc)) return rc;
1379
1380 setModified(IsModified_MachineData);
1381 mHWData.backup();
1382 if (hardwareUUID == mData->mUuid)
1383 mHWData->mHardwareUUID.clear();
1384 else
1385 mHWData->mHardwareUUID = hardwareUUID;
1386
1387 return S_OK;
1388}
1389
1390STDMETHODIMP Machine::COMGETTER(MemorySize)(ULONG *memorySize)
1391{
1392 CheckComArgOutPointerValid(memorySize);
1393
1394 AutoCaller autoCaller(this);
1395 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1396
1397 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1398
1399 *memorySize = mHWData->mMemorySize;
1400
1401 return S_OK;
1402}
1403
1404STDMETHODIMP Machine::COMSETTER(MemorySize)(ULONG memorySize)
1405{
1406 /* check RAM limits */
1407 if ( memorySize < MM_RAM_MIN_IN_MB
1408 || memorySize > MM_RAM_MAX_IN_MB
1409 )
1410 return setError(E_INVALIDARG,
1411 tr("Invalid RAM size: %lu MB (must be in range [%lu, %lu] MB)"),
1412 memorySize, MM_RAM_MIN_IN_MB, MM_RAM_MAX_IN_MB);
1413
1414 AutoCaller autoCaller(this);
1415 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1416
1417 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1418
1419 HRESULT rc = checkStateDependency(MutableStateDep);
1420 if (FAILED(rc)) return rc;
1421
1422 setModified(IsModified_MachineData);
1423 mHWData.backup();
1424 mHWData->mMemorySize = memorySize;
1425
1426 return S_OK;
1427}
1428
1429STDMETHODIMP Machine::COMGETTER(CPUCount)(ULONG *CPUCount)
1430{
1431 CheckComArgOutPointerValid(CPUCount);
1432
1433 AutoCaller autoCaller(this);
1434 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1435
1436 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1437
1438 *CPUCount = mHWData->mCPUCount;
1439
1440 return S_OK;
1441}
1442
1443STDMETHODIMP Machine::COMSETTER(CPUCount)(ULONG CPUCount)
1444{
1445 /* check CPU limits */
1446 if ( CPUCount < SchemaDefs::MinCPUCount
1447 || CPUCount > SchemaDefs::MaxCPUCount
1448 )
1449 return setError(E_INVALIDARG,
1450 tr("Invalid virtual CPU count: %lu (must be in range [%lu, %lu])"),
1451 CPUCount, SchemaDefs::MinCPUCount, SchemaDefs::MaxCPUCount);
1452
1453 AutoCaller autoCaller(this);
1454 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1455
1456 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1457
1458 /* We cant go below the current number of CPUs attached if hotplug is enabled*/
1459 if (mHWData->mCPUHotPlugEnabled)
1460 {
1461 for (unsigned idx = CPUCount; idx < SchemaDefs::MaxCPUCount; idx++)
1462 {
1463 if (mHWData->mCPUAttached[idx])
1464 return setError(E_INVALIDARG,
1465 tr("There is still a CPU attached to socket %lu."
1466 "Detach the CPU before removing the socket"),
1467 CPUCount, idx+1);
1468 }
1469 }
1470
1471 HRESULT rc = checkStateDependency(MutableStateDep);
1472 if (FAILED(rc)) return rc;
1473
1474 setModified(IsModified_MachineData);
1475 mHWData.backup();
1476 mHWData->mCPUCount = CPUCount;
1477
1478 return S_OK;
1479}
1480
1481STDMETHODIMP Machine::COMGETTER(CPUExecutionCap)(ULONG *aExecutionCap)
1482{
1483 CheckComArgOutPointerValid(aExecutionCap);
1484
1485 AutoCaller autoCaller(this);
1486 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1487
1488 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1489
1490 *aExecutionCap = mHWData->mCpuExecutionCap;
1491
1492 return S_OK;
1493}
1494
1495STDMETHODIMP Machine::COMSETTER(CPUExecutionCap)(ULONG aExecutionCap)
1496{
1497 HRESULT rc = S_OK;
1498
1499 /* check throttle limits */
1500 if ( aExecutionCap < 1
1501 || aExecutionCap > 100
1502 )
1503 return setError(E_INVALIDARG,
1504 tr("Invalid CPU execution cap value: %lu (must be in range [%lu, %lu])"),
1505 aExecutionCap, 1, 100);
1506
1507 AutoCaller autoCaller(this);
1508 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1509
1510 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1511
1512 alock.release();
1513 rc = onCPUExecutionCapChange(aExecutionCap);
1514 alock.acquire();
1515 if (FAILED(rc)) return rc;
1516
1517 setModified(IsModified_MachineData);
1518 mHWData.backup();
1519 mHWData->mCpuExecutionCap = aExecutionCap;
1520
1521 /** Save settings if online - @todo why is this required? -- @bugref{6818} */
1522 if (Global::IsOnline(mData->mMachineState))
1523 saveSettings(NULL);
1524
1525 return S_OK;
1526}
1527
1528
1529STDMETHODIMP Machine::COMGETTER(CPUHotPlugEnabled)(BOOL *aEnabled)
1530{
1531 CheckComArgOutPointerValid(aEnabled);
1532
1533 AutoCaller autoCaller(this);
1534 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1535
1536 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1537
1538 *aEnabled = mHWData->mCPUHotPlugEnabled;
1539
1540 return S_OK;
1541}
1542
1543STDMETHODIMP Machine::COMSETTER(CPUHotPlugEnabled)(BOOL aEnabled)
1544{
1545 HRESULT rc = S_OK;
1546
1547 AutoCaller autoCaller(this);
1548 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1549
1550 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1551
1552 rc = checkStateDependency(MutableStateDep);
1553 if (FAILED(rc)) return rc;
1554
1555 if (mHWData->mCPUHotPlugEnabled != aEnabled)
1556 {
1557 if (aEnabled)
1558 {
1559 setModified(IsModified_MachineData);
1560 mHWData.backup();
1561
1562 /* Add the amount of CPUs currently attached */
1563 for (unsigned i = 0; i < mHWData->mCPUCount; i++)
1564 {
1565 mHWData->mCPUAttached[i] = true;
1566 }
1567 }
1568 else
1569 {
1570 /*
1571 * We can disable hotplug only if the amount of maximum CPUs is equal
1572 * to the amount of attached CPUs
1573 */
1574 unsigned cCpusAttached = 0;
1575 unsigned iHighestId = 0;
1576
1577 for (unsigned i = 0; i < SchemaDefs::MaxCPUCount; i++)
1578 {
1579 if (mHWData->mCPUAttached[i])
1580 {
1581 cCpusAttached++;
1582 iHighestId = i;
1583 }
1584 }
1585
1586 if ( (cCpusAttached != mHWData->mCPUCount)
1587 || (iHighestId >= mHWData->mCPUCount))
1588 return setError(E_INVALIDARG,
1589 tr("CPU hotplugging can't be disabled because the maximum number of CPUs is not equal to the amount of CPUs attached"));
1590
1591 setModified(IsModified_MachineData);
1592 mHWData.backup();
1593 }
1594 }
1595
1596 mHWData->mCPUHotPlugEnabled = aEnabled;
1597
1598 return rc;
1599}
1600
1601STDMETHODIMP Machine::COMGETTER(EmulatedUSBCardReaderEnabled)(BOOL *aEnabled)
1602{
1603#ifdef VBOX_WITH_USB_CARDREADER
1604 CheckComArgOutPointerValid(aEnabled);
1605
1606 AutoCaller autoCaller(this);
1607 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1608
1609 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1610
1611 *aEnabled = mHWData->mEmulatedUSBCardReaderEnabled;
1612
1613 return S_OK;
1614#else
1615 NOREF(aEnabled);
1616 return E_NOTIMPL;
1617#endif
1618}
1619
1620STDMETHODIMP Machine::COMSETTER(EmulatedUSBCardReaderEnabled)(BOOL aEnabled)
1621{
1622#ifdef VBOX_WITH_USB_CARDREADER
1623 AutoCaller autoCaller(this);
1624 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1625 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1626
1627 HRESULT rc = checkStateDependency(MutableStateDep);
1628 if (FAILED(rc)) return rc;
1629
1630 setModified(IsModified_MachineData);
1631 mHWData.backup();
1632 mHWData->mEmulatedUSBCardReaderEnabled = aEnabled;
1633
1634 return S_OK;
1635#else
1636 NOREF(aEnabled);
1637 return E_NOTIMPL;
1638#endif
1639}
1640
1641STDMETHODIMP Machine::COMGETTER(HPETEnabled)(BOOL *aEnabled)
1642{
1643 CheckComArgOutPointerValid(aEnabled);
1644
1645 AutoCaller autoCaller(this);
1646 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1647 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1648
1649 *aEnabled = mHWData->mHPETEnabled;
1650
1651 return S_OK;
1652}
1653
1654STDMETHODIMP Machine::COMSETTER(HPETEnabled)(BOOL aEnabled)
1655{
1656 HRESULT rc = S_OK;
1657
1658 AutoCaller autoCaller(this);
1659 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1660 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1661
1662 rc = checkStateDependency(MutableStateDep);
1663 if (FAILED(rc)) return rc;
1664
1665 setModified(IsModified_MachineData);
1666 mHWData.backup();
1667
1668 mHWData->mHPETEnabled = aEnabled;
1669
1670 return rc;
1671}
1672
1673STDMETHODIMP Machine::COMGETTER(VideoCaptureEnabled)(BOOL *fEnabled)
1674{
1675 AutoCaller autoCaller(this);
1676 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1677
1678 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1679
1680 *fEnabled = mHWData->mVideoCaptureEnabled;
1681 return S_OK;
1682}
1683
1684STDMETHODIMP Machine::COMSETTER(VideoCaptureEnabled)(BOOL fEnabled)
1685{
1686 HRESULT rc = S_OK;
1687
1688 AutoCaller autoCaller(this);
1689 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1690 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1691
1692 setModified(IsModified_MachineData);
1693 mHWData.backup();
1694 mHWData->mVideoCaptureEnabled = fEnabled;
1695
1696 alock.release();
1697 rc = onVideoCaptureChange();
1698 alock.acquire();
1699 if (FAILED(rc))
1700 {
1701 /*
1702 * Normally we would do the actual change _after_ onVideoCaptureChange() succeeded.
1703 * We cannot do this because that function uses Machine::GetVideoCaptureEnabled to
1704 * determine if it should start or stop capturing. Therefore we need to manually
1705 * undo change.
1706 */
1707 mHWData->mVideoCaptureEnabled = mHWData.backedUpData()->mVideoCaptureEnabled;
1708 return rc;
1709 }
1710
1711 /** Save settings if online - @todo why is this required? -- @bugref{6818} */
1712 if (Global::IsOnline(mData->mMachineState))
1713 saveSettings(NULL);
1714
1715 return rc;
1716}
1717
1718STDMETHODIMP Machine::COMGETTER(VideoCaptureScreens)(ComSafeArrayOut(BOOL, aScreens))
1719{
1720 CheckComArgOutSafeArrayPointerValid(aScreens);
1721
1722 AutoCaller autoCaller(this);
1723 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1724
1725 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1726
1727 SafeArray<BOOL> screens(mHWData->mMonitorCount);
1728 for (unsigned i = 0; i < screens.size(); i++)
1729 screens[i] = mHWData->maVideoCaptureScreens[i];
1730 screens.detachTo(ComSafeArrayOutArg(aScreens));
1731 return S_OK;
1732}
1733
1734STDMETHODIMP Machine::COMSETTER(VideoCaptureScreens)(ComSafeArrayIn(BOOL, aScreens))
1735{
1736 SafeArray<BOOL> screens(ComSafeArrayInArg(aScreens));
1737 AssertReturn(screens.size() <= RT_ELEMENTS(mHWData->maVideoCaptureScreens), E_INVALIDARG);
1738 bool fChanged = false;
1739
1740 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1741
1742 for (unsigned i = 0; i < screens.size(); i++)
1743 {
1744 if (mHWData->maVideoCaptureScreens[i] != RT_BOOL(screens[i]))
1745 {
1746 mHWData->maVideoCaptureScreens[i] = RT_BOOL(screens[i]);
1747 fChanged = true;
1748 }
1749 }
1750 if (fChanged)
1751 {
1752 alock.release();
1753 HRESULT rc = onVideoCaptureChange();
1754 alock.acquire();
1755 if (FAILED(rc)) return rc;
1756 setModified(IsModified_MachineData);
1757
1758 /** Save settings if online - @todo why is this required? -- @bugref{6818} */
1759 if (Global::IsOnline(mData->mMachineState))
1760 saveSettings(NULL);
1761 }
1762
1763 return S_OK;
1764}
1765
1766STDMETHODIMP Machine::COMGETTER(VideoCaptureFile)(BSTR *apFile)
1767{
1768 AutoCaller autoCaller(this);
1769 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1770
1771 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1772 if (mHWData->mVideoCaptureFile.isEmpty())
1773 {
1774 Utf8Str defaultFile;
1775 getDefaultVideoCaptureFile(defaultFile);
1776 defaultFile.cloneTo(apFile);
1777 }
1778 else
1779 mHWData->mVideoCaptureFile.cloneTo(apFile);
1780 return S_OK;
1781}
1782
1783STDMETHODIMP Machine::COMSETTER(VideoCaptureFile)(IN_BSTR aFile)
1784{
1785 Utf8Str strFile(aFile);
1786 AutoCaller autoCaller(this);
1787 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1788
1789 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1790
1791 if ( Global::IsOnline(mData->mMachineState)
1792 && mHWData->mVideoCaptureEnabled)
1793 return setError(E_INVALIDARG, tr("Cannot change parameters while capturing is enabled"));
1794
1795 if (!RTPathStartsWithRoot(strFile.c_str()))
1796 return setError(E_INVALIDARG, tr("Video capture file name '%s' is not absolute"), strFile.c_str());
1797
1798 if (!strFile.isEmpty())
1799 {
1800 Utf8Str defaultFile;
1801 getDefaultVideoCaptureFile(defaultFile);
1802 if (!RTPathCompare(strFile.c_str(), defaultFile.c_str()))
1803 strFile.setNull();
1804 }
1805
1806 setModified(IsModified_MachineData);
1807 mHWData.backup();
1808 mHWData->mVideoCaptureFile = strFile;
1809
1810 return S_OK;
1811}
1812
1813STDMETHODIMP Machine::COMGETTER(VideoCaptureWidth)(ULONG *aHorzRes)
1814{
1815 AutoCaller autoCaller(this);
1816 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1817
1818 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1819 *aHorzRes = mHWData->mVideoCaptureWidth;
1820 return S_OK;
1821}
1822
1823STDMETHODIMP Machine::COMSETTER(VideoCaptureWidth)(ULONG aHorzRes)
1824{
1825 AutoCaller autoCaller(this);
1826 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1827
1828 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1829
1830 if ( Global::IsOnline(mData->mMachineState)
1831 && mHWData->mVideoCaptureEnabled)
1832 return setError(E_INVALIDARG, tr("Cannot change parameters while capturing is enabled"));
1833
1834 setModified(IsModified_MachineData);
1835 mHWData.backup();
1836 mHWData->mVideoCaptureWidth = aHorzRes;
1837
1838 return S_OK;
1839}
1840
1841STDMETHODIMP Machine::COMGETTER(VideoCaptureHeight)(ULONG *aVertRes)
1842{
1843 AutoCaller autoCaller(this);
1844 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1845
1846 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1847 *aVertRes = mHWData->mVideoCaptureHeight;
1848 return S_OK;
1849}
1850
1851STDMETHODIMP Machine::COMSETTER(VideoCaptureHeight)(ULONG aVertRes)
1852{
1853 AutoCaller autoCaller(this);
1854 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1855
1856 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1857
1858 if ( Global::IsOnline(mData->mMachineState)
1859 && mHWData->mVideoCaptureEnabled)
1860 return setError(E_INVALIDARG, tr("Cannot change parameters while capturing is enabled"));
1861
1862 setModified(IsModified_MachineData);
1863 mHWData.backup();
1864 mHWData->mVideoCaptureHeight = aVertRes;
1865
1866 return S_OK;
1867}
1868
1869STDMETHODIMP Machine::COMGETTER(VideoCaptureRate)(ULONG *aRate)
1870{
1871 AutoCaller autoCaller(this);
1872 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1873
1874 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1875 *aRate = mHWData->mVideoCaptureRate;
1876 return S_OK;
1877}
1878
1879STDMETHODIMP Machine::COMSETTER(VideoCaptureRate)(ULONG aRate)
1880{
1881 AutoCaller autoCaller(this);
1882 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1883
1884 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1885
1886 if ( Global::IsOnline(mData->mMachineState)
1887 && mHWData->mVideoCaptureEnabled)
1888 return setError(E_INVALIDARG, tr("Cannot change parameters while capturing is enabled"));
1889
1890 setModified(IsModified_MachineData);
1891 mHWData.backup();
1892 mHWData->mVideoCaptureRate = aRate;
1893
1894 return S_OK;
1895}
1896
1897STDMETHODIMP Machine::COMGETTER(VideoCaptureFPS)(ULONG *aFPS)
1898{
1899 AutoCaller autoCaller(this);
1900 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1901
1902 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1903 *aFPS = mHWData->mVideoCaptureFPS;
1904 return S_OK;
1905}
1906
1907STDMETHODIMP Machine::COMSETTER(VideoCaptureFPS)(ULONG aFPS)
1908{
1909 AutoCaller autoCaller(this);
1910 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1911
1912 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1913
1914 if ( Global::IsOnline(mData->mMachineState)
1915 && mHWData->mVideoCaptureEnabled)
1916 return setError(E_INVALIDARG, tr("Cannot change parameters while capturing is enabled"));
1917
1918 setModified(IsModified_MachineData);
1919 mHWData.backup();
1920 mHWData->mVideoCaptureFPS = aFPS;
1921
1922 return S_OK;
1923}
1924
1925STDMETHODIMP Machine::COMGETTER(GraphicsControllerType)(GraphicsControllerType_T *aGraphicsControllerType)
1926{
1927 CheckComArgOutPointerValid(aGraphicsControllerType);
1928
1929 AutoCaller autoCaller(this);
1930 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1931
1932 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1933
1934 *aGraphicsControllerType = mHWData->mGraphicsControllerType;
1935
1936 return S_OK;
1937}
1938
1939STDMETHODIMP Machine::COMSETTER(GraphicsControllerType)(GraphicsControllerType_T aGraphicsControllerType)
1940{
1941 switch (aGraphicsControllerType)
1942 {
1943 case GraphicsControllerType_Null:
1944 case GraphicsControllerType_VBoxVGA:
1945#ifdef VBOX_WITH_VMSVGA
1946 case GraphicsControllerType_VMSVGA:
1947#endif
1948 break;
1949 default:
1950 return setError(E_INVALIDARG, tr("The graphics controller type (%d) is invalid"), aGraphicsControllerType);
1951 }
1952
1953 AutoCaller autoCaller(this);
1954 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1955
1956 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1957
1958 HRESULT rc = checkStateDependency(MutableStateDep);
1959 if (FAILED(rc)) return rc;
1960
1961 setModified(IsModified_MachineData);
1962 mHWData.backup();
1963 mHWData->mGraphicsControllerType = aGraphicsControllerType;
1964
1965 return S_OK;
1966}
1967
1968STDMETHODIMP Machine::COMGETTER(VRAMSize)(ULONG *memorySize)
1969{
1970 CheckComArgOutPointerValid(memorySize);
1971
1972 AutoCaller autoCaller(this);
1973 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1974
1975 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1976
1977 *memorySize = mHWData->mVRAMSize;
1978
1979 return S_OK;
1980}
1981
1982STDMETHODIMP Machine::COMSETTER(VRAMSize)(ULONG memorySize)
1983{
1984 /* check VRAM limits */
1985 if (memorySize < SchemaDefs::MinGuestVRAM ||
1986 memorySize > SchemaDefs::MaxGuestVRAM)
1987 return setError(E_INVALIDARG,
1988 tr("Invalid VRAM size: %lu MB (must be in range [%lu, %lu] MB)"),
1989 memorySize, SchemaDefs::MinGuestVRAM, SchemaDefs::MaxGuestVRAM);
1990
1991 AutoCaller autoCaller(this);
1992 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1993
1994 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1995
1996 HRESULT rc = checkStateDependency(MutableStateDep);
1997 if (FAILED(rc)) return rc;
1998
1999 setModified(IsModified_MachineData);
2000 mHWData.backup();
2001 mHWData->mVRAMSize = memorySize;
2002
2003 return S_OK;
2004}
2005
2006/** @todo this method should not be public */
2007STDMETHODIMP Machine::COMGETTER(MemoryBalloonSize)(ULONG *memoryBalloonSize)
2008{
2009 CheckComArgOutPointerValid(memoryBalloonSize);
2010
2011 AutoCaller autoCaller(this);
2012 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2013
2014 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2015
2016 *memoryBalloonSize = mHWData->mMemoryBalloonSize;
2017
2018 return S_OK;
2019}
2020
2021/**
2022 * Set the memory balloon size.
2023 *
2024 * This method is also called from IGuest::COMSETTER(MemoryBalloonSize) so
2025 * we have to make sure that we never call IGuest from here.
2026 */
2027STDMETHODIMP Machine::COMSETTER(MemoryBalloonSize)(ULONG memoryBalloonSize)
2028{
2029 /* This must match GMMR0Init; currently we only support memory ballooning on all 64-bit hosts except Mac OS X */
2030#if HC_ARCH_BITS == 64 && (defined(RT_OS_WINDOWS) || defined(RT_OS_SOLARIS) || defined(RT_OS_LINUX) || defined(RT_OS_FREEBSD))
2031 /* check limits */
2032 if (memoryBalloonSize >= VMMDEV_MAX_MEMORY_BALLOON(mHWData->mMemorySize))
2033 return setError(E_INVALIDARG,
2034 tr("Invalid memory balloon size: %lu MB (must be in range [%lu, %lu] MB)"),
2035 memoryBalloonSize, 0, VMMDEV_MAX_MEMORY_BALLOON(mHWData->mMemorySize));
2036
2037 AutoCaller autoCaller(this);
2038 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2039
2040 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2041
2042 setModified(IsModified_MachineData);
2043 mHWData.backup();
2044 mHWData->mMemoryBalloonSize = memoryBalloonSize;
2045
2046 return S_OK;
2047#else
2048 NOREF(memoryBalloonSize);
2049 return setError(E_NOTIMPL, tr("Memory ballooning is only supported on 64-bit hosts"));
2050#endif
2051}
2052
2053STDMETHODIMP Machine::COMGETTER(PageFusionEnabled) (BOOL *aEnabled)
2054{
2055 CheckComArgOutPointerValid(aEnabled);
2056
2057 AutoCaller autoCaller(this);
2058 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2059
2060 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2061
2062 *aEnabled = mHWData->mPageFusionEnabled;
2063 return S_OK;
2064}
2065
2066STDMETHODIMP Machine::COMSETTER(PageFusionEnabled) (BOOL aEnabled)
2067{
2068#ifdef VBOX_WITH_PAGE_SHARING
2069 AutoCaller autoCaller(this);
2070 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2071
2072 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2073
2074 /** @todo must support changes for running vms and keep this in sync with IGuest. */
2075 setModified(IsModified_MachineData);
2076 mHWData.backup();
2077 mHWData->mPageFusionEnabled = aEnabled;
2078 return S_OK;
2079#else
2080 NOREF(aEnabled);
2081 return setError(E_NOTIMPL, tr("Page fusion is only supported on 64-bit hosts"));
2082#endif
2083}
2084
2085STDMETHODIMP Machine::COMGETTER(Accelerate3DEnabled)(BOOL *aEnabled)
2086{
2087 CheckComArgOutPointerValid(aEnabled);
2088
2089 AutoCaller autoCaller(this);
2090 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2091
2092 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2093
2094 *aEnabled = mHWData->mAccelerate3DEnabled;
2095
2096 return S_OK;
2097}
2098
2099STDMETHODIMP Machine::COMSETTER(Accelerate3DEnabled)(BOOL enable)
2100{
2101 AutoCaller autoCaller(this);
2102 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2103
2104 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2105
2106 HRESULT rc = checkStateDependency(MutableStateDep);
2107 if (FAILED(rc)) return rc;
2108
2109 /** @todo check validity! */
2110
2111 setModified(IsModified_MachineData);
2112 mHWData.backup();
2113 mHWData->mAccelerate3DEnabled = enable;
2114
2115 return S_OK;
2116}
2117
2118
2119STDMETHODIMP Machine::COMGETTER(Accelerate2DVideoEnabled)(BOOL *aEnabled)
2120{
2121 CheckComArgOutPointerValid(aEnabled);
2122
2123 AutoCaller autoCaller(this);
2124 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2125
2126 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2127
2128 *aEnabled = mHWData->mAccelerate2DVideoEnabled;
2129
2130 return S_OK;
2131}
2132
2133STDMETHODIMP Machine::COMSETTER(Accelerate2DVideoEnabled)(BOOL enable)
2134{
2135 AutoCaller autoCaller(this);
2136 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2137
2138 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2139
2140 HRESULT rc = checkStateDependency(MutableStateDep);
2141 if (FAILED(rc)) return rc;
2142
2143 /** @todo check validity! */
2144
2145 setModified(IsModified_MachineData);
2146 mHWData.backup();
2147 mHWData->mAccelerate2DVideoEnabled = enable;
2148
2149 return S_OK;
2150}
2151
2152STDMETHODIMP Machine::COMGETTER(MonitorCount)(ULONG *monitorCount)
2153{
2154 CheckComArgOutPointerValid(monitorCount);
2155
2156 AutoCaller autoCaller(this);
2157 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2158
2159 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2160
2161 *monitorCount = mHWData->mMonitorCount;
2162
2163 return S_OK;
2164}
2165
2166STDMETHODIMP Machine::COMSETTER(MonitorCount)(ULONG monitorCount)
2167{
2168 /* make sure monitor count is a sensible number */
2169 if (monitorCount < 1 || monitorCount > SchemaDefs::MaxGuestMonitors)
2170 return setError(E_INVALIDARG,
2171 tr("Invalid monitor count: %lu (must be in range [%lu, %lu])"),
2172 monitorCount, 1, SchemaDefs::MaxGuestMonitors);
2173
2174 AutoCaller autoCaller(this);
2175 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2176
2177 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2178
2179 HRESULT rc = checkStateDependency(MutableStateDep);
2180 if (FAILED(rc)) return rc;
2181
2182 setModified(IsModified_MachineData);
2183 mHWData.backup();
2184 mHWData->mMonitorCount = monitorCount;
2185
2186 return S_OK;
2187}
2188
2189STDMETHODIMP Machine::COMGETTER(BIOSSettings)(IBIOSSettings **biosSettings)
2190{
2191 CheckComArgOutPointerValid(biosSettings);
2192
2193 AutoCaller autoCaller(this);
2194 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2195
2196 /* mBIOSSettings is constant during life time, no need to lock */
2197 mBIOSSettings.queryInterfaceTo(biosSettings);
2198
2199 return S_OK;
2200}
2201
2202STDMETHODIMP Machine::GetCPUProperty(CPUPropertyType_T property, BOOL *aVal)
2203{
2204 CheckComArgOutPointerValid(aVal);
2205
2206 AutoCaller autoCaller(this);
2207 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2208
2209 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2210
2211 switch (property)
2212 {
2213 case CPUPropertyType_PAE:
2214 *aVal = mHWData->mPAEEnabled;
2215 break;
2216
2217 case CPUPropertyType_Synthetic:
2218 *aVal = mHWData->mSyntheticCpu;
2219 break;
2220
2221 case CPUPropertyType_LongMode:
2222 if (mHWData->mLongMode == settings::Hardware::LongMode_Enabled)
2223 *aVal = TRUE;
2224 else if (mHWData->mLongMode == settings::Hardware::LongMode_Disabled)
2225 *aVal = FALSE;
2226#if HC_ARCH_BITS == 64
2227 else
2228 *aVal = TRUE;
2229#else
2230 else
2231 {
2232 *aVal = FALSE;
2233
2234 ComPtr<IGuestOSType> ptrGuestOSType;
2235 HRESULT hrc2 = mParent->GetGuestOSType(Bstr(mUserData->s.strOsType).raw(), ptrGuestOSType.asOutParam());
2236 if (SUCCEEDED(hrc2))
2237 {
2238 BOOL fIs64Bit = FALSE;
2239 hrc2 = ptrGuestOSType->COMGETTER(Is64Bit)(&fIs64Bit); AssertComRC(hrc2);
2240 if (SUCCEEDED(hrc2) && fIs64Bit)
2241 {
2242 ComObjPtr<Host> ptrHost = mParent->i_host();
2243 alock.release();
2244
2245 hrc2 = ptrHost->GetProcessorFeature(ProcessorFeature_LongMode, aVal); AssertComRC(hrc2);
2246 if (FAILED(hrc2))
2247 *aVal = FALSE;
2248 }
2249 }
2250 }
2251#endif
2252 break;
2253
2254 case CPUPropertyType_TripleFaultReset:
2255 *aVal = mHWData->mTripleFaultReset;
2256 break;
2257
2258 default:
2259 return E_INVALIDARG;
2260 }
2261 return S_OK;
2262}
2263
2264STDMETHODIMP Machine::SetCPUProperty(CPUPropertyType_T property, BOOL aVal)
2265{
2266 AutoCaller autoCaller(this);
2267 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2268
2269 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2270
2271 HRESULT rc = checkStateDependency(MutableStateDep);
2272 if (FAILED(rc)) return rc;
2273
2274 switch (property)
2275 {
2276 case CPUPropertyType_PAE:
2277 setModified(IsModified_MachineData);
2278 mHWData.backup();
2279 mHWData->mPAEEnabled = !!aVal;
2280 break;
2281
2282 case CPUPropertyType_Synthetic:
2283 setModified(IsModified_MachineData);
2284 mHWData.backup();
2285 mHWData->mSyntheticCpu = !!aVal;
2286 break;
2287
2288 case CPUPropertyType_LongMode:
2289 setModified(IsModified_MachineData);
2290 mHWData.backup();
2291 mHWData->mLongMode = !aVal ? settings::Hardware::LongMode_Disabled : settings::Hardware::LongMode_Enabled;
2292 break;
2293
2294 case CPUPropertyType_TripleFaultReset:
2295 setModified(IsModified_MachineData);
2296 mHWData.backup();
2297 mHWData->mTripleFaultReset = !!aVal;
2298 break;
2299
2300 default:
2301 return E_INVALIDARG;
2302 }
2303 return S_OK;
2304}
2305
2306STDMETHODIMP Machine::GetCPUIDLeaf(ULONG aId, ULONG *aValEax, ULONG *aValEbx, ULONG *aValEcx, ULONG *aValEdx)
2307{
2308 CheckComArgOutPointerValid(aValEax);
2309 CheckComArgOutPointerValid(aValEbx);
2310 CheckComArgOutPointerValid(aValEcx);
2311 CheckComArgOutPointerValid(aValEdx);
2312
2313 AutoCaller autoCaller(this);
2314 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2315
2316 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2317
2318 switch(aId)
2319 {
2320 case 0x0:
2321 case 0x1:
2322 case 0x2:
2323 case 0x3:
2324 case 0x4:
2325 case 0x5:
2326 case 0x6:
2327 case 0x7:
2328 case 0x8:
2329 case 0x9:
2330 case 0xA:
2331 if (mHWData->mCpuIdStdLeafs[aId].ulId != aId)
2332 return E_INVALIDARG;
2333
2334 *aValEax = mHWData->mCpuIdStdLeafs[aId].ulEax;
2335 *aValEbx = mHWData->mCpuIdStdLeafs[aId].ulEbx;
2336 *aValEcx = mHWData->mCpuIdStdLeafs[aId].ulEcx;
2337 *aValEdx = mHWData->mCpuIdStdLeafs[aId].ulEdx;
2338 break;
2339
2340 case 0x80000000:
2341 case 0x80000001:
2342 case 0x80000002:
2343 case 0x80000003:
2344 case 0x80000004:
2345 case 0x80000005:
2346 case 0x80000006:
2347 case 0x80000007:
2348 case 0x80000008:
2349 case 0x80000009:
2350 case 0x8000000A:
2351 if (mHWData->mCpuIdExtLeafs[aId - 0x80000000].ulId != aId)
2352 return E_INVALIDARG;
2353
2354 *aValEax = mHWData->mCpuIdExtLeafs[aId - 0x80000000].ulEax;
2355 *aValEbx = mHWData->mCpuIdExtLeafs[aId - 0x80000000].ulEbx;
2356 *aValEcx = mHWData->mCpuIdExtLeafs[aId - 0x80000000].ulEcx;
2357 *aValEdx = mHWData->mCpuIdExtLeafs[aId - 0x80000000].ulEdx;
2358 break;
2359
2360 default:
2361 return setError(E_INVALIDARG, tr("CpuId override leaf %#x is out of range"), aId);
2362 }
2363 return S_OK;
2364}
2365
2366STDMETHODIMP Machine::SetCPUIDLeaf(ULONG aId, ULONG aValEax, ULONG aValEbx, ULONG aValEcx, ULONG aValEdx)
2367{
2368 AutoCaller autoCaller(this);
2369 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2370
2371 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2372
2373 HRESULT rc = checkStateDependency(MutableStateDep);
2374 if (FAILED(rc)) return rc;
2375
2376 switch(aId)
2377 {
2378 case 0x0:
2379 case 0x1:
2380 case 0x2:
2381 case 0x3:
2382 case 0x4:
2383 case 0x5:
2384 case 0x6:
2385 case 0x7:
2386 case 0x8:
2387 case 0x9:
2388 case 0xA:
2389 AssertCompile(RT_ELEMENTS(mHWData->mCpuIdStdLeafs) == 0xB);
2390 AssertRelease(aId < RT_ELEMENTS(mHWData->mCpuIdStdLeafs));
2391 setModified(IsModified_MachineData);
2392 mHWData.backup();
2393 mHWData->mCpuIdStdLeafs[aId].ulId = aId;
2394 mHWData->mCpuIdStdLeafs[aId].ulEax = aValEax;
2395 mHWData->mCpuIdStdLeafs[aId].ulEbx = aValEbx;
2396 mHWData->mCpuIdStdLeafs[aId].ulEcx = aValEcx;
2397 mHWData->mCpuIdStdLeafs[aId].ulEdx = aValEdx;
2398 break;
2399
2400 case 0x80000000:
2401 case 0x80000001:
2402 case 0x80000002:
2403 case 0x80000003:
2404 case 0x80000004:
2405 case 0x80000005:
2406 case 0x80000006:
2407 case 0x80000007:
2408 case 0x80000008:
2409 case 0x80000009:
2410 case 0x8000000A:
2411 AssertCompile(RT_ELEMENTS(mHWData->mCpuIdExtLeafs) == 0xB);
2412 AssertRelease(aId - 0x80000000 < RT_ELEMENTS(mHWData->mCpuIdExtLeafs));
2413 setModified(IsModified_MachineData);
2414 mHWData.backup();
2415 mHWData->mCpuIdExtLeafs[aId - 0x80000000].ulId = aId;
2416 mHWData->mCpuIdExtLeafs[aId - 0x80000000].ulEax = aValEax;
2417 mHWData->mCpuIdExtLeafs[aId - 0x80000000].ulEbx = aValEbx;
2418 mHWData->mCpuIdExtLeafs[aId - 0x80000000].ulEcx = aValEcx;
2419 mHWData->mCpuIdExtLeafs[aId - 0x80000000].ulEdx = aValEdx;
2420 break;
2421
2422 default:
2423 return setError(E_INVALIDARG, tr("CpuId override leaf %#x is out of range"), aId);
2424 }
2425 return S_OK;
2426}
2427
2428STDMETHODIMP Machine::RemoveCPUIDLeaf(ULONG aId)
2429{
2430 AutoCaller autoCaller(this);
2431 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2432
2433 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2434
2435 HRESULT rc = checkStateDependency(MutableStateDep);
2436 if (FAILED(rc)) return rc;
2437
2438 switch(aId)
2439 {
2440 case 0x0:
2441 case 0x1:
2442 case 0x2:
2443 case 0x3:
2444 case 0x4:
2445 case 0x5:
2446 case 0x6:
2447 case 0x7:
2448 case 0x8:
2449 case 0x9:
2450 case 0xA:
2451 AssertCompile(RT_ELEMENTS(mHWData->mCpuIdStdLeafs) == 0xB);
2452 AssertRelease(aId < RT_ELEMENTS(mHWData->mCpuIdStdLeafs));
2453 setModified(IsModified_MachineData);
2454 mHWData.backup();
2455 /* Invalidate leaf. */
2456 mHWData->mCpuIdStdLeafs[aId].ulId = UINT32_MAX;
2457 break;
2458
2459 case 0x80000000:
2460 case 0x80000001:
2461 case 0x80000002:
2462 case 0x80000003:
2463 case 0x80000004:
2464 case 0x80000005:
2465 case 0x80000006:
2466 case 0x80000007:
2467 case 0x80000008:
2468 case 0x80000009:
2469 case 0x8000000A:
2470 AssertCompile(RT_ELEMENTS(mHWData->mCpuIdExtLeafs) == 0xB);
2471 AssertRelease(aId - 0x80000000 < RT_ELEMENTS(mHWData->mCpuIdExtLeafs));
2472 setModified(IsModified_MachineData);
2473 mHWData.backup();
2474 /* Invalidate leaf. */
2475 mHWData->mCpuIdExtLeafs[aId - 0x80000000].ulId = UINT32_MAX;
2476 break;
2477
2478 default:
2479 return setError(E_INVALIDARG, tr("CpuId override leaf %#x is out of range"), aId);
2480 }
2481 return S_OK;
2482}
2483
2484STDMETHODIMP Machine::RemoveAllCPUIDLeaves()
2485{
2486 AutoCaller autoCaller(this);
2487 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2488
2489 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2490
2491 HRESULT rc = checkStateDependency(MutableStateDep);
2492 if (FAILED(rc)) return rc;
2493
2494 setModified(IsModified_MachineData);
2495 mHWData.backup();
2496
2497 /* Invalidate all standard leafs. */
2498 for (unsigned i = 0; i < RT_ELEMENTS(mHWData->mCpuIdStdLeafs); i++)
2499 mHWData->mCpuIdStdLeafs[i].ulId = UINT32_MAX;
2500
2501 /* Invalidate all extended leafs. */
2502 for (unsigned i = 0; i < RT_ELEMENTS(mHWData->mCpuIdExtLeafs); i++)
2503 mHWData->mCpuIdExtLeafs[i].ulId = UINT32_MAX;
2504
2505 return S_OK;
2506}
2507
2508STDMETHODIMP Machine::GetHWVirtExProperty(HWVirtExPropertyType_T property, BOOL *aVal)
2509{
2510 CheckComArgOutPointerValid(aVal);
2511
2512 AutoCaller autoCaller(this);
2513 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2514
2515 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2516
2517 switch(property)
2518 {
2519 case HWVirtExPropertyType_Enabled:
2520 *aVal = mHWData->mHWVirtExEnabled;
2521 break;
2522
2523 case HWVirtExPropertyType_VPID:
2524 *aVal = mHWData->mHWVirtExVPIDEnabled;
2525 break;
2526
2527 case HWVirtExPropertyType_NestedPaging:
2528 *aVal = mHWData->mHWVirtExNestedPagingEnabled;
2529 break;
2530
2531 case HWVirtExPropertyType_UnrestrictedExecution:
2532 *aVal = mHWData->mHWVirtExUXEnabled;
2533 break;
2534
2535 case HWVirtExPropertyType_LargePages:
2536 *aVal = mHWData->mHWVirtExLargePagesEnabled;
2537#if defined(DEBUG_bird) && defined(RT_OS_LINUX) /* This feature is deadly here */
2538 *aVal = FALSE;
2539#endif
2540 break;
2541
2542 case HWVirtExPropertyType_Force:
2543 *aVal = mHWData->mHWVirtExForceEnabled;
2544 break;
2545
2546 default:
2547 return E_INVALIDARG;
2548 }
2549 return S_OK;
2550}
2551
2552STDMETHODIMP Machine::SetHWVirtExProperty(HWVirtExPropertyType_T property, BOOL aVal)
2553{
2554 AutoCaller autoCaller(this);
2555 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2556
2557 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2558
2559 HRESULT rc = checkStateDependency(MutableStateDep);
2560 if (FAILED(rc)) return rc;
2561
2562 switch(property)
2563 {
2564 case HWVirtExPropertyType_Enabled:
2565 setModified(IsModified_MachineData);
2566 mHWData.backup();
2567 mHWData->mHWVirtExEnabled = !!aVal;
2568 break;
2569
2570 case HWVirtExPropertyType_VPID:
2571 setModified(IsModified_MachineData);
2572 mHWData.backup();
2573 mHWData->mHWVirtExVPIDEnabled = !!aVal;
2574 break;
2575
2576 case HWVirtExPropertyType_NestedPaging:
2577 setModified(IsModified_MachineData);
2578 mHWData.backup();
2579 mHWData->mHWVirtExNestedPagingEnabled = !!aVal;
2580 break;
2581
2582 case HWVirtExPropertyType_UnrestrictedExecution:
2583 setModified(IsModified_MachineData);
2584 mHWData.backup();
2585 mHWData->mHWVirtExUXEnabled = !!aVal;
2586 break;
2587
2588 case HWVirtExPropertyType_LargePages:
2589 setModified(IsModified_MachineData);
2590 mHWData.backup();
2591 mHWData->mHWVirtExLargePagesEnabled = !!aVal;
2592 break;
2593
2594 case HWVirtExPropertyType_Force:
2595 setModified(IsModified_MachineData);
2596 mHWData.backup();
2597 mHWData->mHWVirtExForceEnabled = !!aVal;
2598 break;
2599
2600 default:
2601 return E_INVALIDARG;
2602 }
2603
2604 return S_OK;
2605}
2606
2607STDMETHODIMP Machine::COMGETTER(SnapshotFolder)(BSTR *aSnapshotFolder)
2608{
2609 CheckComArgOutPointerValid(aSnapshotFolder);
2610
2611 AutoCaller autoCaller(this);
2612 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2613
2614 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2615
2616 Utf8Str strFullSnapshotFolder;
2617 calculateFullPath(mUserData->s.strSnapshotFolder, strFullSnapshotFolder);
2618 strFullSnapshotFolder.cloneTo(aSnapshotFolder);
2619
2620 return S_OK;
2621}
2622
2623STDMETHODIMP Machine::COMSETTER(SnapshotFolder)(IN_BSTR aSnapshotFolder)
2624{
2625 /* @todo (r=dmik):
2626 * 1. Allow to change the name of the snapshot folder containing snapshots
2627 * 2. Rename the folder on disk instead of just changing the property
2628 * value (to be smart and not to leave garbage). Note that it cannot be
2629 * done here because the change may be rolled back. Thus, the right
2630 * place is #saveSettings().
2631 */
2632
2633 AutoCaller autoCaller(this);
2634 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2635
2636 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2637
2638 HRESULT rc = checkStateDependency(MutableStateDep);
2639 if (FAILED(rc)) return rc;
2640
2641 if (!mData->mCurrentSnapshot.isNull())
2642 return setError(E_FAIL,
2643 tr("The snapshot folder of a machine with snapshots cannot be changed (please delete all snapshots first)"));
2644
2645 Utf8Str strSnapshotFolder0(aSnapshotFolder); // keep original
2646
2647 Utf8Str strSnapshotFolder(strSnapshotFolder0);
2648 if (strSnapshotFolder.isEmpty())
2649 strSnapshotFolder = "Snapshots";
2650 int vrc = calculateFullPath(strSnapshotFolder,
2651 strSnapshotFolder);
2652 if (RT_FAILURE(vrc))
2653 return setError(E_FAIL,
2654 tr("Invalid snapshot folder '%ls' (%Rrc)"),
2655 aSnapshotFolder, vrc);
2656
2657 setModified(IsModified_MachineData);
2658 mUserData.backup();
2659
2660 copyPathRelativeToMachine(strSnapshotFolder, mUserData->s.strSnapshotFolder);
2661
2662 return S_OK;
2663}
2664
2665STDMETHODIMP Machine::COMGETTER(MediumAttachments)(ComSafeArrayOut(IMediumAttachment*, aAttachments))
2666{
2667 CheckComArgOutSafeArrayPointerValid(aAttachments);
2668
2669 AutoCaller autoCaller(this);
2670 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2671
2672 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2673
2674 SafeIfaceArray<IMediumAttachment> attachments(mMediaData->mAttachments);
2675 attachments.detachTo(ComSafeArrayOutArg(aAttachments));
2676
2677 return S_OK;
2678}
2679
2680STDMETHODIMP Machine::COMGETTER(VRDEServer)(IVRDEServer **vrdeServer)
2681{
2682 CheckComArgOutPointerValid(vrdeServer);
2683
2684 AutoCaller autoCaller(this);
2685 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2686
2687 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2688
2689 Assert(!!mVRDEServer);
2690 mVRDEServer.queryInterfaceTo(vrdeServer);
2691
2692 return S_OK;
2693}
2694
2695STDMETHODIMP Machine::COMGETTER(AudioAdapter)(IAudioAdapter **audioAdapter)
2696{
2697 CheckComArgOutPointerValid(audioAdapter);
2698
2699 AutoCaller autoCaller(this);
2700 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2701
2702 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2703
2704 mAudioAdapter.queryInterfaceTo(audioAdapter);
2705 return S_OK;
2706}
2707
2708STDMETHODIMP Machine::COMGETTER(USBControllers)(ComSafeArrayOut(IUSBController *, aUSBControllers))
2709{
2710#ifdef VBOX_WITH_VUSB
2711 CheckComArgOutPointerValid(aUSBControllers);
2712
2713 AutoCaller autoCaller(this);
2714 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2715
2716 clearError();
2717 MultiResult rc(S_OK);
2718
2719# ifdef VBOX_WITH_USB
2720 rc = mParent->i_host()->i_checkUSBProxyService();
2721 if (FAILED(rc)) return rc;
2722# endif
2723
2724 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2725
2726 SafeIfaceArray<IUSBController> ctrls(*mUSBControllers.data());
2727 ctrls.detachTo(ComSafeArrayOutArg(aUSBControllers));
2728 return S_OK;
2729#else
2730 /* Note: The GUI depends on this method returning E_NOTIMPL with no
2731 * extended error info to indicate that USB is simply not available
2732 * (w/o treating it as a failure), for example, as in OSE */
2733 NOREF(aUSBControllers);
2734 ReturnComNotImplemented();
2735#endif /* VBOX_WITH_VUSB */
2736}
2737
2738STDMETHODIMP Machine::COMGETTER(USBDeviceFilters)(IUSBDeviceFilters **aUSBDeviceFilters)
2739{
2740#ifdef VBOX_WITH_VUSB
2741 CheckComArgOutPointerValid(aUSBDeviceFilters);
2742
2743 AutoCaller autoCaller(this);
2744 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2745
2746 clearError();
2747 MultiResult rc(S_OK);
2748
2749# ifdef VBOX_WITH_USB
2750 rc = mParent->i_host()->i_checkUSBProxyService();
2751 if (FAILED(rc)) return rc;
2752# endif
2753
2754 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2755
2756 return rc = mUSBDeviceFilters.queryInterfaceTo(aUSBDeviceFilters);
2757#else
2758 /* Note: The GUI depends on this method returning E_NOTIMPL with no
2759 * extended error info to indicate that USB is simply not available
2760 * (w/o treating it as a failure), for example, as in OSE */
2761 NOREF(aUSBDeviceFilters);
2762 ReturnComNotImplemented();
2763#endif /* VBOX_WITH_VUSB */
2764}
2765
2766STDMETHODIMP Machine::COMGETTER(SettingsFilePath)(BSTR *aFilePath)
2767{
2768 CheckComArgOutPointerValid(aFilePath);
2769
2770 AutoLimitedCaller autoCaller(this);
2771 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2772
2773 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2774
2775 mData->m_strConfigFileFull.cloneTo(aFilePath);
2776 return S_OK;
2777}
2778
2779STDMETHODIMP Machine::COMGETTER(SettingsModified)(BOOL *aModified)
2780{
2781 CheckComArgOutPointerValid(aModified);
2782
2783 AutoCaller autoCaller(this);
2784 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2785
2786 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2787
2788 HRESULT rc = checkStateDependency(MutableStateDep);
2789 if (FAILED(rc)) return rc;
2790
2791 if (!mData->pMachineConfigFile->fileExists())
2792 // this is a new machine, and no config file exists yet:
2793 *aModified = TRUE;
2794 else
2795 *aModified = (mData->flModifications != 0);
2796
2797 return S_OK;
2798}
2799
2800STDMETHODIMP Machine::COMGETTER(SessionState)(SessionState_T *aSessionState)
2801{
2802 CheckComArgOutPointerValid(aSessionState);
2803
2804 AutoCaller autoCaller(this);
2805 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2806
2807 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2808
2809 *aSessionState = mData->mSession.mState;
2810
2811 return S_OK;
2812}
2813
2814STDMETHODIMP Machine::COMGETTER(SessionType)(BSTR *aSessionType)
2815{
2816 CheckComArgOutPointerValid(aSessionType);
2817
2818 AutoCaller autoCaller(this);
2819 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2820
2821 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2822
2823 mData->mSession.mType.cloneTo(aSessionType);
2824
2825 return S_OK;
2826}
2827
2828STDMETHODIMP Machine::COMGETTER(SessionPID)(ULONG *aSessionPID)
2829{
2830 CheckComArgOutPointerValid(aSessionPID);
2831
2832 AutoCaller autoCaller(this);
2833 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2834
2835 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2836
2837 *aSessionPID = mData->mSession.mPID;
2838
2839 return S_OK;
2840}
2841
2842STDMETHODIMP Machine::COMGETTER(State)(MachineState_T *machineState)
2843{
2844 CheckComArgOutPointerValid(machineState);
2845
2846 AutoCaller autoCaller(this);
2847 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2848
2849 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2850
2851 *machineState = mData->mMachineState;
2852
2853 return S_OK;
2854}
2855
2856STDMETHODIMP Machine::COMGETTER(LastStateChange)(LONG64 *aLastStateChange)
2857{
2858 CheckComArgOutPointerValid(aLastStateChange);
2859
2860 AutoCaller autoCaller(this);
2861 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2862
2863 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2864
2865 *aLastStateChange = RTTimeSpecGetMilli(&mData->mLastStateChange);
2866
2867 return S_OK;
2868}
2869
2870STDMETHODIMP Machine::COMGETTER(StateFilePath)(BSTR *aStateFilePath)
2871{
2872 CheckComArgOutPointerValid(aStateFilePath);
2873
2874 AutoCaller autoCaller(this);
2875 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2876
2877 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2878
2879 mSSData->strStateFilePath.cloneTo(aStateFilePath);
2880
2881 return S_OK;
2882}
2883
2884STDMETHODIMP Machine::COMGETTER(LogFolder)(BSTR *aLogFolder)
2885{
2886 CheckComArgOutPointerValid(aLogFolder);
2887
2888 AutoCaller autoCaller(this);
2889 AssertComRCReturnRC(autoCaller.rc());
2890
2891 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2892
2893 Utf8Str logFolder;
2894 getLogFolder(logFolder);
2895 logFolder.cloneTo(aLogFolder);
2896
2897 return S_OK;
2898}
2899
2900STDMETHODIMP Machine::COMGETTER(CurrentSnapshot) (ISnapshot **aCurrentSnapshot)
2901{
2902 CheckComArgOutPointerValid(aCurrentSnapshot);
2903
2904 AutoCaller autoCaller(this);
2905 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2906
2907 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2908
2909 mData->mCurrentSnapshot.queryInterfaceTo(aCurrentSnapshot);
2910
2911 return S_OK;
2912}
2913
2914STDMETHODIMP Machine::COMGETTER(SnapshotCount)(ULONG *aSnapshotCount)
2915{
2916 CheckComArgOutPointerValid(aSnapshotCount);
2917
2918 AutoCaller autoCaller(this);
2919 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2920
2921 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2922
2923 *aSnapshotCount = mData->mFirstSnapshot.isNull()
2924 ? 0
2925 : mData->mFirstSnapshot->i_getAllChildrenCount() + 1;
2926
2927 return S_OK;
2928}
2929
2930STDMETHODIMP Machine::COMGETTER(CurrentStateModified)(BOOL *aCurrentStateModified)
2931{
2932 CheckComArgOutPointerValid(aCurrentStateModified);
2933
2934 AutoCaller autoCaller(this);
2935 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2936
2937 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2938
2939 /* Note: for machines with no snapshots, we always return FALSE
2940 * (mData->mCurrentStateModified will be TRUE in this case, for historical
2941 * reasons :) */
2942
2943 *aCurrentStateModified = mData->mFirstSnapshot.isNull()
2944 ? FALSE
2945 : mData->mCurrentStateModified;
2946
2947 return S_OK;
2948}
2949
2950STDMETHODIMP Machine::COMGETTER(SharedFolders)(ComSafeArrayOut(ISharedFolder *, aSharedFolders))
2951{
2952 CheckComArgOutSafeArrayPointerValid(aSharedFolders);
2953
2954 AutoCaller autoCaller(this);
2955 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2956
2957 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2958
2959 SafeIfaceArray<ISharedFolder> folders(mHWData->mSharedFolders);
2960 folders.detachTo(ComSafeArrayOutArg(aSharedFolders));
2961
2962 return S_OK;
2963}
2964
2965STDMETHODIMP Machine::COMGETTER(ClipboardMode)(ClipboardMode_T *aClipboardMode)
2966{
2967 CheckComArgOutPointerValid(aClipboardMode);
2968
2969 AutoCaller autoCaller(this);
2970 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2971
2972 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2973
2974 *aClipboardMode = mHWData->mClipboardMode;
2975
2976 return S_OK;
2977}
2978
2979STDMETHODIMP Machine::COMSETTER(ClipboardMode)(ClipboardMode_T aClipboardMode)
2980{
2981 HRESULT rc = S_OK;
2982
2983 AutoCaller autoCaller(this);
2984 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2985
2986 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2987
2988 alock.release();
2989 rc = onClipboardModeChange(aClipboardMode);
2990 alock.acquire();
2991 if (FAILED(rc)) return rc;
2992
2993 setModified(IsModified_MachineData);
2994 mHWData.backup();
2995 mHWData->mClipboardMode = aClipboardMode;
2996
2997 /** Save settings if online - @todo why is this required? -- @bugref{6818} */
2998 if (Global::IsOnline(mData->mMachineState))
2999 saveSettings(NULL);
3000
3001 return S_OK;
3002}
3003
3004STDMETHODIMP Machine::COMGETTER(DragAndDropMode)(DragAndDropMode_T *aDragAndDropMode)
3005{
3006 CheckComArgOutPointerValid(aDragAndDropMode);
3007
3008 AutoCaller autoCaller(this);
3009 if (FAILED(autoCaller.rc())) return autoCaller.rc();
3010
3011 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
3012
3013 *aDragAndDropMode = mHWData->mDragAndDropMode;
3014
3015 return S_OK;
3016}
3017
3018STDMETHODIMP Machine::COMSETTER(DragAndDropMode)(DragAndDropMode_T aDragAndDropMode)
3019{
3020 HRESULT rc = S_OK;
3021
3022 AutoCaller autoCaller(this);
3023 if (FAILED(autoCaller.rc())) return autoCaller.rc();
3024
3025 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
3026
3027 alock.release();
3028 rc = onDragAndDropModeChange(aDragAndDropMode);
3029 alock.acquire();
3030 if (FAILED(rc)) return rc;
3031
3032 setModified(IsModified_MachineData);
3033 mHWData.backup();
3034 mHWData->mDragAndDropMode = aDragAndDropMode;
3035
3036 /** Save settings if online - @todo why is this required? -- @bugref{6818} */
3037 if (Global::IsOnline(mData->mMachineState))
3038 saveSettings(NULL);
3039
3040 return S_OK;
3041}
3042
3043STDMETHODIMP Machine::COMGETTER(GuestPropertyNotificationPatterns)(BSTR *aPatterns)
3044{
3045 CheckComArgOutPointerValid(aPatterns);
3046
3047 AutoCaller autoCaller(this);
3048 if (FAILED(autoCaller.rc())) return autoCaller.rc();
3049
3050 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
3051
3052 try
3053 {
3054 mHWData->mGuestPropertyNotificationPatterns.cloneTo(aPatterns);
3055 }
3056 catch (...)
3057 {
3058 return VirtualBoxBase::handleUnexpectedExceptions(this, RT_SRC_POS);
3059 }
3060
3061 return S_OK;
3062}
3063
3064STDMETHODIMP Machine::COMSETTER(GuestPropertyNotificationPatterns)(IN_BSTR aPatterns)
3065{
3066 AutoCaller autoCaller(this);
3067 if (FAILED(autoCaller.rc())) return autoCaller.rc();
3068
3069 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
3070
3071 HRESULT rc = checkStateDependency(MutableStateDep);
3072 if (FAILED(rc)) return rc;
3073
3074 setModified(IsModified_MachineData);
3075 mHWData.backup();
3076 mHWData->mGuestPropertyNotificationPatterns = aPatterns;
3077 return rc;
3078}
3079
3080STDMETHODIMP Machine::COMGETTER(StorageControllers)(ComSafeArrayOut(IStorageController *, aStorageControllers))
3081{
3082 CheckComArgOutSafeArrayPointerValid(aStorageControllers);
3083
3084 AutoCaller autoCaller(this);
3085 if (FAILED(autoCaller.rc())) return autoCaller.rc();
3086
3087 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
3088
3089 SafeIfaceArray<IStorageController> ctrls(*mStorageControllers.data());
3090 ctrls.detachTo(ComSafeArrayOutArg(aStorageControllers));
3091
3092 return S_OK;
3093}
3094
3095STDMETHODIMP Machine::COMGETTER(TeleporterEnabled)(BOOL *aEnabled)
3096{
3097 CheckComArgOutPointerValid(aEnabled);
3098
3099 AutoCaller autoCaller(this);
3100 if (FAILED(autoCaller.rc())) return autoCaller.rc();
3101
3102 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
3103
3104 *aEnabled = mUserData->s.fTeleporterEnabled;
3105
3106 return S_OK;
3107}
3108
3109STDMETHODIMP Machine::COMSETTER(TeleporterEnabled)(BOOL aEnabled)
3110{
3111 AutoCaller autoCaller(this);
3112 if (FAILED(autoCaller.rc())) return autoCaller.rc();
3113
3114 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
3115
3116 /* Only allow it to be set to true when PoweredOff or Aborted.
3117 (Clearing it is always permitted.) */
3118 if ( aEnabled
3119 && mData->mRegistered
3120 && ( !isSessionMachine()
3121 || ( mData->mMachineState != MachineState_PoweredOff
3122 && mData->mMachineState != MachineState_Teleported
3123 && mData->mMachineState != MachineState_Aborted
3124 )
3125 )
3126 )
3127 return setError(VBOX_E_INVALID_VM_STATE,
3128 tr("The machine is not powered off (state is %s)"),
3129 Global::stringifyMachineState(mData->mMachineState));
3130
3131 setModified(IsModified_MachineData);
3132 mUserData.backup();
3133 mUserData->s.fTeleporterEnabled = !!aEnabled;
3134
3135 return S_OK;
3136}
3137
3138STDMETHODIMP Machine::COMGETTER(TeleporterPort)(ULONG *aPort)
3139{
3140 CheckComArgOutPointerValid(aPort);
3141
3142 AutoCaller autoCaller(this);
3143 if (FAILED(autoCaller.rc())) return autoCaller.rc();
3144
3145 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
3146
3147 *aPort = (ULONG)mUserData->s.uTeleporterPort;
3148
3149 return S_OK;
3150}
3151
3152STDMETHODIMP Machine::COMSETTER(TeleporterPort)(ULONG aPort)
3153{
3154 if (aPort >= _64K)
3155 return setError(E_INVALIDARG, tr("Invalid port number %d"), aPort);
3156
3157 AutoCaller autoCaller(this);
3158 if (FAILED(autoCaller.rc())) return autoCaller.rc();
3159
3160 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
3161
3162 HRESULT rc = checkStateDependency(MutableStateDep);
3163 if (FAILED(rc)) return rc;
3164
3165 setModified(IsModified_MachineData);
3166 mUserData.backup();
3167 mUserData->s.uTeleporterPort = (uint32_t)aPort;
3168
3169 return S_OK;
3170}
3171
3172STDMETHODIMP Machine::COMGETTER(TeleporterAddress)(BSTR *aAddress)
3173{
3174 CheckComArgOutPointerValid(aAddress);
3175
3176 AutoCaller autoCaller(this);
3177 if (FAILED(autoCaller.rc())) return autoCaller.rc();
3178
3179 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
3180
3181 mUserData->s.strTeleporterAddress.cloneTo(aAddress);
3182
3183 return S_OK;
3184}
3185
3186STDMETHODIMP Machine::COMSETTER(TeleporterAddress)(IN_BSTR aAddress)
3187{
3188 AutoCaller autoCaller(this);
3189 if (FAILED(autoCaller.rc())) return autoCaller.rc();
3190
3191 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
3192
3193 HRESULT rc = checkStateDependency(MutableStateDep);
3194 if (FAILED(rc)) return rc;
3195
3196 setModified(IsModified_MachineData);
3197 mUserData.backup();
3198 mUserData->s.strTeleporterAddress = aAddress;
3199
3200 return S_OK;
3201}
3202
3203STDMETHODIMP Machine::COMGETTER(TeleporterPassword)(BSTR *aPassword)
3204{
3205 CheckComArgOutPointerValid(aPassword);
3206
3207 AutoCaller autoCaller(this);
3208 HRESULT hrc = autoCaller.rc();
3209 if (SUCCEEDED(hrc))
3210 {
3211 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
3212 mUserData->s.strTeleporterPassword.cloneTo(aPassword);
3213 }
3214
3215 return hrc;
3216}
3217
3218STDMETHODIMP Machine::COMSETTER(TeleporterPassword)(IN_BSTR aPassword)
3219{
3220 /*
3221 * Hash the password first.
3222 */
3223 Utf8Str strPassword(aPassword);
3224 if (!strPassword.isEmpty())
3225 {
3226 if (VBoxIsPasswordHashed(&strPassword))
3227 return setError(E_INVALIDARG, tr("Cannot set an already hashed password, only plain text password please"));
3228 VBoxHashPassword(&strPassword);
3229 }
3230
3231 /*
3232 * Do the update.
3233 */
3234 AutoCaller autoCaller(this);
3235 HRESULT hrc = autoCaller.rc();
3236 if (SUCCEEDED(hrc))
3237 {
3238 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
3239 hrc = checkStateDependency(MutableStateDep);
3240 if (SUCCEEDED(hrc))
3241 {
3242 setModified(IsModified_MachineData);
3243 mUserData.backup();
3244 mUserData->s.strTeleporterPassword = strPassword;
3245 }
3246 }
3247
3248 return hrc;
3249}
3250
3251STDMETHODIMP Machine::COMGETTER(FaultToleranceState)(FaultToleranceState_T *aState)
3252{
3253 CheckComArgOutPointerValid(aState);
3254
3255 AutoCaller autoCaller(this);
3256 if (FAILED(autoCaller.rc())) return autoCaller.rc();
3257
3258 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
3259
3260 *aState = mUserData->s.enmFaultToleranceState;
3261 return S_OK;
3262}
3263
3264STDMETHODIMP Machine::COMSETTER(FaultToleranceState)(FaultToleranceState_T aState)
3265{
3266 AutoCaller autoCaller(this);
3267 if (FAILED(autoCaller.rc())) return autoCaller.rc();
3268
3269 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
3270
3271 /* @todo deal with running state change. */
3272 HRESULT rc = checkStateDependency(MutableStateDep);
3273 if (FAILED(rc)) return rc;
3274
3275 setModified(IsModified_MachineData);
3276 mUserData.backup();
3277 mUserData->s.enmFaultToleranceState = aState;
3278 return S_OK;
3279}
3280
3281STDMETHODIMP Machine::COMGETTER(FaultToleranceAddress)(BSTR *aAddress)
3282{
3283 CheckComArgOutPointerValid(aAddress);
3284
3285 AutoCaller autoCaller(this);
3286 if (FAILED(autoCaller.rc())) return autoCaller.rc();
3287
3288 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
3289
3290 mUserData->s.strFaultToleranceAddress.cloneTo(aAddress);
3291 return S_OK;
3292}
3293
3294STDMETHODIMP Machine::COMSETTER(FaultToleranceAddress)(IN_BSTR aAddress)
3295{
3296 AutoCaller autoCaller(this);
3297 if (FAILED(autoCaller.rc())) return autoCaller.rc();
3298
3299 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
3300
3301 /* @todo deal with running state change. */
3302 HRESULT rc = checkStateDependency(MutableStateDep);
3303 if (FAILED(rc)) return rc;
3304
3305 setModified(IsModified_MachineData);
3306 mUserData.backup();
3307 mUserData->s.strFaultToleranceAddress = aAddress;
3308 return S_OK;
3309}
3310
3311STDMETHODIMP Machine::COMGETTER(FaultTolerancePort)(ULONG *aPort)
3312{
3313 CheckComArgOutPointerValid(aPort);
3314
3315 AutoCaller autoCaller(this);
3316 if (FAILED(autoCaller.rc())) return autoCaller.rc();
3317
3318 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
3319
3320 *aPort = mUserData->s.uFaultTolerancePort;
3321 return S_OK;
3322}
3323
3324STDMETHODIMP Machine::COMSETTER(FaultTolerancePort)(ULONG aPort)
3325{
3326 AutoCaller autoCaller(this);
3327 if (FAILED(autoCaller.rc())) return autoCaller.rc();
3328
3329 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
3330
3331 /* @todo deal with running state change. */
3332 HRESULT rc = checkStateDependency(MutableStateDep);
3333 if (FAILED(rc)) return rc;
3334
3335 setModified(IsModified_MachineData);
3336 mUserData.backup();
3337 mUserData->s.uFaultTolerancePort = aPort;
3338 return S_OK;
3339}
3340
3341STDMETHODIMP Machine::COMGETTER(FaultTolerancePassword)(BSTR *aPassword)
3342{
3343 CheckComArgOutPointerValid(aPassword);
3344
3345 AutoCaller autoCaller(this);
3346 if (FAILED(autoCaller.rc())) return autoCaller.rc();
3347
3348 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
3349
3350 mUserData->s.strFaultTolerancePassword.cloneTo(aPassword);
3351
3352 return S_OK;
3353}
3354
3355STDMETHODIMP Machine::COMSETTER(FaultTolerancePassword)(IN_BSTR aPassword)
3356{
3357 AutoCaller autoCaller(this);
3358 if (FAILED(autoCaller.rc())) return autoCaller.rc();
3359
3360 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
3361
3362 /* @todo deal with running state change. */
3363 HRESULT rc = checkStateDependency(MutableStateDep);
3364 if (FAILED(rc)) return rc;
3365
3366 setModified(IsModified_MachineData);
3367 mUserData.backup();
3368 mUserData->s.strFaultTolerancePassword = aPassword;
3369
3370 return S_OK;
3371}
3372
3373STDMETHODIMP Machine::COMGETTER(FaultToleranceSyncInterval)(ULONG *aInterval)
3374{
3375 CheckComArgOutPointerValid(aInterval);
3376
3377 AutoCaller autoCaller(this);
3378 if (FAILED(autoCaller.rc())) return autoCaller.rc();
3379
3380 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
3381
3382 *aInterval = mUserData->s.uFaultToleranceInterval;
3383 return S_OK;
3384}
3385
3386STDMETHODIMP Machine::COMSETTER(FaultToleranceSyncInterval)(ULONG aInterval)
3387{
3388 AutoCaller autoCaller(this);
3389 if (FAILED(autoCaller.rc())) return autoCaller.rc();
3390
3391 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
3392
3393 /* @todo deal with running state change. */
3394 HRESULT rc = checkStateDependency(MutableStateDep);
3395 if (FAILED(rc)) return rc;
3396
3397 setModified(IsModified_MachineData);
3398 mUserData.backup();
3399 mUserData->s.uFaultToleranceInterval = aInterval;
3400 return S_OK;
3401}
3402
3403STDMETHODIMP Machine::COMGETTER(RTCUseUTC)(BOOL *aEnabled)
3404{
3405 CheckComArgOutPointerValid(aEnabled);
3406
3407 AutoCaller autoCaller(this);
3408 if (FAILED(autoCaller.rc())) return autoCaller.rc();
3409
3410 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
3411
3412 *aEnabled = mUserData->s.fRTCUseUTC;
3413
3414 return S_OK;
3415}
3416
3417STDMETHODIMP Machine::COMSETTER(RTCUseUTC)(BOOL aEnabled)
3418{
3419 AutoCaller autoCaller(this);
3420 if (FAILED(autoCaller.rc())) return autoCaller.rc();
3421
3422 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
3423
3424 /* Only allow it to be set to true when PoweredOff or Aborted.
3425 (Clearing it is always permitted.) */
3426 if ( aEnabled
3427 && mData->mRegistered
3428 && ( !isSessionMachine()
3429 || ( mData->mMachineState != MachineState_PoweredOff
3430 && mData->mMachineState != MachineState_Teleported
3431 && mData->mMachineState != MachineState_Aborted
3432 )
3433 )
3434 )
3435 return setError(VBOX_E_INVALID_VM_STATE,
3436 tr("The machine is not powered off (state is %s)"),
3437 Global::stringifyMachineState(mData->mMachineState));
3438
3439 setModified(IsModified_MachineData);
3440 mUserData.backup();
3441 mUserData->s.fRTCUseUTC = !!aEnabled;
3442
3443 return S_OK;
3444}
3445
3446STDMETHODIMP Machine::COMGETTER(IOCacheEnabled)(BOOL *aEnabled)
3447{
3448 CheckComArgOutPointerValid(aEnabled);
3449
3450 AutoCaller autoCaller(this);
3451 if (FAILED(autoCaller.rc())) return autoCaller.rc();
3452
3453 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
3454
3455 *aEnabled = mHWData->mIOCacheEnabled;
3456
3457 return S_OK;
3458}
3459
3460STDMETHODIMP Machine::COMSETTER(IOCacheEnabled)(BOOL aEnabled)
3461{
3462 AutoCaller autoCaller(this);
3463 if (FAILED(autoCaller.rc())) return autoCaller.rc();
3464
3465 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
3466
3467 HRESULT rc = checkStateDependency(MutableStateDep);
3468 if (FAILED(rc)) return rc;
3469
3470 setModified(IsModified_MachineData);
3471 mHWData.backup();
3472 mHWData->mIOCacheEnabled = aEnabled;
3473
3474 return S_OK;
3475}
3476
3477STDMETHODIMP Machine::COMGETTER(IOCacheSize)(ULONG *aIOCacheSize)
3478{
3479 CheckComArgOutPointerValid(aIOCacheSize);
3480
3481 AutoCaller autoCaller(this);
3482 if (FAILED(autoCaller.rc())) return autoCaller.rc();
3483
3484 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
3485
3486 *aIOCacheSize = mHWData->mIOCacheSize;
3487
3488 return S_OK;
3489}
3490
3491STDMETHODIMP Machine::COMSETTER(IOCacheSize)(ULONG aIOCacheSize)
3492{
3493 AutoCaller autoCaller(this);
3494 if (FAILED(autoCaller.rc())) return autoCaller.rc();
3495
3496 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
3497
3498 HRESULT rc = checkStateDependency(MutableStateDep);
3499 if (FAILED(rc)) return rc;
3500
3501 setModified(IsModified_MachineData);
3502 mHWData.backup();
3503 mHWData->mIOCacheSize = aIOCacheSize;
3504
3505 return S_OK;
3506}
3507
3508
3509/**
3510 * @note Locks objects!
3511 */
3512STDMETHODIMP Machine::LockMachine(ISession *aSession,
3513 LockType_T lockType)
3514{
3515 CheckComArgNotNull(aSession);
3516
3517 AutoCaller autoCaller(this);
3518 if (FAILED(autoCaller.rc())) return autoCaller.rc();
3519
3520 /* check the session state */
3521 SessionState_T state;
3522 HRESULT rc = aSession->COMGETTER(State)(&state);
3523 if (FAILED(rc)) return rc;
3524
3525 if (state != SessionState_Unlocked)
3526 return setError(VBOX_E_INVALID_OBJECT_STATE,
3527 tr("The given session is busy"));
3528
3529 // get the client's IInternalSessionControl interface
3530 ComPtr<IInternalSessionControl> pSessionControl = aSession;
3531 ComAssertMsgRet(!!pSessionControl, ("No IInternalSessionControl interface"),
3532 E_INVALIDARG);
3533
3534 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
3535
3536 if (!mData->mRegistered)
3537 return setError(E_UNEXPECTED,
3538 tr("The machine '%s' is not registered"),
3539 mUserData->s.strName.c_str());
3540
3541 LogFlowThisFunc(("mSession.mState=%s\n", Global::stringifySessionState(mData->mSession.mState)));
3542
3543 SessionState_T oldState = mData->mSession.mState;
3544 /* Hack: in case the session is closing and there is a progress object
3545 * which allows waiting for the session to be closed, take the opportunity
3546 * and do a limited wait (max. 1 second). This helps a lot when the system
3547 * is busy and thus session closing can take a little while. */
3548 if ( mData->mSession.mState == SessionState_Unlocking
3549 && mData->mSession.mProgress)
3550 {
3551 alock.release();
3552 mData->mSession.mProgress->WaitForCompletion(1000);
3553 alock.acquire();
3554 LogFlowThisFunc(("after waiting: mSession.mState=%s\n", Global::stringifySessionState(mData->mSession.mState)));
3555 }
3556
3557 // try again now
3558 if ( (mData->mSession.mState == SessionState_Locked) // machine is write-locked already (i.e. session machine exists)
3559 && (lockType == LockType_Shared) // caller wants a shared link to the existing session that holds the write lock:
3560 )
3561 {
3562 // OK, share the session... we are now dealing with three processes:
3563 // 1) VBoxSVC (where this code runs);
3564 // 2) process C: the caller's client process (who wants a shared session);
3565 // 3) process W: the process which already holds the write lock on the machine (write-locking session)
3566
3567 // copy pointers to W (the write-locking session) before leaving lock (these must not be NULL)
3568 ComPtr<IInternalSessionControl> pSessionW = mData->mSession.mDirectControl;
3569 ComAssertRet(!pSessionW.isNull(), E_FAIL);
3570 ComObjPtr<SessionMachine> pSessionMachine = mData->mSession.mMachine;
3571 AssertReturn(!pSessionMachine.isNull(), E_FAIL);
3572
3573 /*
3574 * Release the lock before calling the client process. It's safe here
3575 * since the only thing to do after we get the lock again is to add
3576 * the remote control to the list (which doesn't directly influence
3577 * anything).
3578 */
3579 alock.release();
3580
3581 // get the console of the session holding the write lock (this is a remote call)
3582 ComPtr<IConsole> pConsoleW;
3583 LogFlowThisFunc(("Calling GetRemoteConsole()...\n"));
3584 rc = pSessionW->GetRemoteConsole(pConsoleW.asOutParam());
3585 LogFlowThisFunc(("GetRemoteConsole() returned %08X\n", rc));
3586 if (FAILED(rc))
3587 // the failure may occur w/o any error info (from RPC), so provide one
3588 return setError(VBOX_E_VM_ERROR,
3589 tr("Failed to get a console object from the direct session (%Rhrc)"), rc);
3590
3591 ComAssertRet(!pConsoleW.isNull(), E_FAIL);
3592
3593 // share the session machine and W's console with the caller's session
3594 LogFlowThisFunc(("Calling AssignRemoteMachine()...\n"));
3595 rc = pSessionControl->AssignRemoteMachine(pSessionMachine, pConsoleW);
3596 LogFlowThisFunc(("AssignRemoteMachine() returned %08X\n", rc));
3597
3598 if (FAILED(rc))
3599 // the failure may occur w/o any error info (from RPC), so provide one
3600 return setError(VBOX_E_VM_ERROR,
3601 tr("Failed to assign the machine to the session (%Rhrc)"), rc);
3602 alock.acquire();
3603
3604 // need to revalidate the state after acquiring the lock again
3605 if (mData->mSession.mState != SessionState_Locked)
3606 {
3607 pSessionControl->Uninitialize();
3608 return setError(VBOX_E_INVALID_SESSION_STATE,
3609 tr("The machine '%s' was unlocked unexpectedly while attempting to share its session"),
3610 mUserData->s.strName.c_str());
3611 }
3612
3613 // add the caller's session to the list
3614 mData->mSession.mRemoteControls.push_back(pSessionControl);
3615 }
3616 else if ( mData->mSession.mState == SessionState_Locked
3617 || mData->mSession.mState == SessionState_Unlocking
3618 )
3619 {
3620 // sharing not permitted, or machine still unlocking:
3621 return setError(VBOX_E_INVALID_OBJECT_STATE,
3622 tr("The machine '%s' is already locked for a session (or being unlocked)"),
3623 mUserData->s.strName.c_str());
3624 }
3625 else
3626 {
3627 // machine is not locked: then write-lock the machine (create the session machine)
3628
3629 // must not be busy
3630 AssertReturn(!Global::IsOnlineOrTransient(mData->mMachineState), E_FAIL);
3631
3632 // get the caller's session PID
3633 RTPROCESS pid = NIL_RTPROCESS;
3634 AssertCompile(sizeof(ULONG) == sizeof(RTPROCESS));
3635 pSessionControl->GetPID((ULONG*)&pid);
3636 Assert(pid != NIL_RTPROCESS);
3637
3638 bool fLaunchingVMProcess = (mData->mSession.mState == SessionState_Spawning);
3639
3640 if (fLaunchingVMProcess)
3641 {
3642 if (mData->mSession.mPID == NIL_RTPROCESS)
3643 {
3644 // two or more clients racing for a lock, the one which set the
3645 // session state to Spawning will win, the others will get an
3646 // error as we can't decide here if waiting a little would help
3647 // (only for shared locks this would avoid an error)
3648 return setError(VBOX_E_INVALID_OBJECT_STATE,
3649 tr("The machine '%s' already has a lock request pending"),
3650 mUserData->s.strName.c_str());
3651 }
3652
3653 // this machine is awaiting for a spawning session to be opened:
3654 // then the calling process must be the one that got started by
3655 // LaunchVMProcess()
3656
3657 LogFlowThisFunc(("mSession.mPID=%d(0x%x)\n", mData->mSession.mPID, mData->mSession.mPID));
3658 LogFlowThisFunc(("session.pid=%d(0x%x)\n", pid, pid));
3659
3660 if (mData->mSession.mPID != pid)
3661 return setError(E_ACCESSDENIED,
3662 tr("An unexpected process (PID=0x%08X) has tried to lock the "
3663 "machine '%s', while only the process started by LaunchVMProcess (PID=0x%08X) is allowed"),
3664 pid, mUserData->s.strName.c_str(), mData->mSession.mPID);
3665 }
3666
3667 // create the mutable SessionMachine from the current machine
3668 ComObjPtr<SessionMachine> sessionMachine;
3669 sessionMachine.createObject();
3670 rc = sessionMachine->init(this);
3671 AssertComRC(rc);
3672
3673 /* NOTE: doing return from this function after this point but
3674 * before the end is forbidden since it may call SessionMachine::uninit()
3675 * (through the ComObjPtr's destructor) which requests the VirtualBox write
3676 * lock while still holding the Machine lock in alock so that a deadlock
3677 * is possible due to the wrong lock order. */
3678
3679 if (SUCCEEDED(rc))
3680 {
3681 /*
3682 * Set the session state to Spawning to protect against subsequent
3683 * attempts to open a session and to unregister the machine after
3684 * we release the lock.
3685 */
3686 SessionState_T origState = mData->mSession.mState;
3687 mData->mSession.mState = SessionState_Spawning;
3688
3689#ifndef VBOX_WITH_GENERIC_SESSION_WATCHER
3690 /* Get the client token ID to be passed to the client process */
3691 Utf8Str strTokenId;
3692 sessionMachine->getTokenId(strTokenId);
3693 Assert(!strTokenId.isEmpty());
3694#else /* VBOX_WITH_GENERIC_SESSION_WATCHER */
3695 /* Get the client token to be passed to the client process */
3696 ComPtr<IToken> pToken(sessionMachine->getToken());
3697 /* The token is now "owned" by pToken, fix refcount */
3698 if (!pToken.isNull())
3699 pToken->Release();
3700#endif /* VBOX_WITH_GENERIC_SESSION_WATCHER */
3701
3702 /*
3703 * Release the lock before calling the client process -- it will call
3704 * Machine/SessionMachine methods. Releasing the lock here is quite safe
3705 * because the state is Spawning, so that LaunchVMProcess() and
3706 * LockMachine() calls will fail. This method, called before we
3707 * acquire the lock again, will fail because of the wrong PID.
3708 *
3709 * Note that mData->mSession.mRemoteControls accessed outside
3710 * the lock may not be modified when state is Spawning, so it's safe.
3711 */
3712 alock.release();
3713
3714 LogFlowThisFunc(("Calling AssignMachine()...\n"));
3715#ifndef VBOX_WITH_GENERIC_SESSION_WATCHER
3716 rc = pSessionControl->AssignMachine(sessionMachine, lockType, Bstr(strTokenId).raw());
3717#else /* VBOX_WITH_GENERIC_SESSION_WATCHER */
3718 rc = pSessionControl->AssignMachine(sessionMachine, lockType, pToken);
3719 /* Now the token is owned by the client process. */
3720 pToken.setNull();
3721#endif /* VBOX_WITH_GENERIC_SESSION_WATCHER */
3722 LogFlowThisFunc(("AssignMachine() returned %08X\n", rc));
3723
3724 /* The failure may occur w/o any error info (from RPC), so provide one */
3725 if (FAILED(rc))
3726 setError(VBOX_E_VM_ERROR,
3727 tr("Failed to assign the machine to the session (%Rhrc)"), rc);
3728
3729 if ( SUCCEEDED(rc)
3730 && fLaunchingVMProcess
3731 )
3732 {
3733 /* complete the remote session initialization */
3734
3735 /* get the console from the direct session */
3736 ComPtr<IConsole> console;
3737 rc = pSessionControl->GetRemoteConsole(console.asOutParam());
3738 ComAssertComRC(rc);
3739
3740 if (SUCCEEDED(rc) && !console)
3741 {
3742 ComAssert(!!console);
3743 rc = E_FAIL;
3744 }
3745
3746 /* assign machine & console to the remote session */
3747 if (SUCCEEDED(rc))
3748 {
3749 /*
3750 * after LaunchVMProcess(), the first and the only
3751 * entry in remoteControls is that remote session
3752 */
3753 LogFlowThisFunc(("Calling AssignRemoteMachine()...\n"));
3754 rc = mData->mSession.mRemoteControls.front()->AssignRemoteMachine(sessionMachine, console);
3755 LogFlowThisFunc(("AssignRemoteMachine() returned %08X\n", rc));
3756
3757 /* The failure may occur w/o any error info (from RPC), so provide one */
3758 if (FAILED(rc))
3759 setError(VBOX_E_VM_ERROR,
3760 tr("Failed to assign the machine to the remote session (%Rhrc)"), rc);
3761 }
3762
3763 if (FAILED(rc))
3764 pSessionControl->Uninitialize();
3765 }
3766
3767 /* acquire the lock again */
3768 alock.acquire();
3769
3770 /* Restore the session state */
3771 mData->mSession.mState = origState;
3772 }
3773
3774 // finalize spawning anyway (this is why we don't return on errors above)
3775 if (fLaunchingVMProcess)
3776 {
3777 /* Note that the progress object is finalized later */
3778 /** @todo Consider checking mData->mSession.mProgress for cancellation
3779 * around here. */
3780
3781 /* We don't reset mSession.mPID here because it is necessary for
3782 * SessionMachine::uninit() to reap the child process later. */
3783
3784 if (FAILED(rc))
3785 {
3786 /* Close the remote session, remove the remote control from the list
3787 * and reset session state to Closed (@note keep the code in sync
3788 * with the relevant part in checkForSpawnFailure()). */
3789
3790 Assert(mData->mSession.mRemoteControls.size() == 1);
3791 if (mData->mSession.mRemoteControls.size() == 1)
3792 {
3793 ErrorInfoKeeper eik;
3794 mData->mSession.mRemoteControls.front()->Uninitialize();
3795 }
3796
3797 mData->mSession.mRemoteControls.clear();
3798 mData->mSession.mState = SessionState_Unlocked;
3799 }
3800 }
3801 else
3802 {
3803 /* memorize PID of the directly opened session */
3804 if (SUCCEEDED(rc))
3805 mData->mSession.mPID = pid;
3806 }
3807
3808 if (SUCCEEDED(rc))
3809 {
3810 /* memorize the direct session control and cache IUnknown for it */
3811 mData->mSession.mDirectControl = pSessionControl;
3812 mData->mSession.mState = SessionState_Locked;
3813 /* associate the SessionMachine with this Machine */
3814 mData->mSession.mMachine = sessionMachine;
3815
3816 /* request an IUnknown pointer early from the remote party for later
3817 * identity checks (it will be internally cached within mDirectControl
3818 * at least on XPCOM) */
3819 ComPtr<IUnknown> unk = mData->mSession.mDirectControl;
3820 NOREF(unk);
3821 }
3822
3823 /* Release the lock since SessionMachine::uninit() locks VirtualBox which
3824 * would break the lock order */
3825 alock.release();
3826
3827 /* uninitialize the created session machine on failure */
3828 if (FAILED(rc))
3829 sessionMachine->uninit();
3830
3831 }
3832
3833 if (SUCCEEDED(rc))
3834 {
3835 /*
3836 * tell the client watcher thread to update the set of
3837 * machines that have open sessions
3838 */
3839 mParent->i_updateClientWatcher();
3840
3841 if (oldState != SessionState_Locked)
3842 /* fire an event */
3843 mParent->i_onSessionStateChange(getId(), SessionState_Locked);
3844 }
3845
3846 return rc;
3847}
3848
3849/**
3850 * @note Locks objects!
3851 */
3852STDMETHODIMP Machine::LaunchVMProcess(ISession *aSession,
3853 IN_BSTR aFrontend,
3854 IN_BSTR aEnvironment,
3855 IProgress **aProgress)
3856{
3857 CheckComArgStr(aFrontend);
3858 Utf8Str strFrontend(aFrontend);
3859 Utf8Str strEnvironment(aEnvironment);
3860 /* "emergencystop" doesn't need the session, so skip the checks/interface
3861 * retrieval. This code doesn't quite fit in here, but introducing a
3862 * special API method would be even more effort, and would require explicit
3863 * support by every API client. It's better to hide the feature a bit. */
3864 if (strFrontend != "emergencystop")
3865 CheckComArgNotNull(aSession);
3866 CheckComArgOutPointerValid(aProgress);
3867
3868 AutoCaller autoCaller(this);
3869 if (FAILED(autoCaller.rc())) return autoCaller.rc();
3870
3871 HRESULT rc = S_OK;
3872 if (strFrontend.isEmpty())
3873 {
3874 Bstr bstrFrontend;
3875 rc = COMGETTER(DefaultFrontend)(bstrFrontend.asOutParam());
3876 if (FAILED(rc))
3877 return rc;
3878 strFrontend = bstrFrontend;
3879 if (strFrontend.isEmpty())
3880 {
3881 ComPtr<ISystemProperties> systemProperties;
3882 rc = mParent->COMGETTER(SystemProperties)(systemProperties.asOutParam());
3883 if (FAILED(rc))
3884 return rc;
3885 rc = systemProperties->COMGETTER(DefaultFrontend)(bstrFrontend.asOutParam());
3886 if (FAILED(rc))
3887 return rc;
3888 strFrontend = bstrFrontend;
3889 }
3890 /* paranoia - emergencystop is not a valid default */
3891 if (strFrontend == "emergencystop")
3892 strFrontend = Utf8Str::Empty;
3893 }
3894 /* default frontend: Qt GUI */
3895 if (strFrontend.isEmpty())
3896 strFrontend = "GUI/Qt";
3897
3898 if (strFrontend != "emergencystop")
3899 {
3900 /* check the session state */
3901 SessionState_T state;
3902 rc = aSession->COMGETTER(State)(&state);
3903 if (FAILED(rc))
3904 return rc;
3905
3906 if (state != SessionState_Unlocked)
3907 return setError(VBOX_E_INVALID_OBJECT_STATE,
3908 tr("The given session is busy"));
3909
3910 /* get the IInternalSessionControl interface */
3911 ComPtr<IInternalSessionControl> control(aSession);
3912 ComAssertMsgRet(!control.isNull(),
3913 ("No IInternalSessionControl interface"),
3914 E_INVALIDARG);
3915
3916 /* get the teleporter enable state for the progress object init. */
3917 BOOL fTeleporterEnabled;
3918 rc = COMGETTER(TeleporterEnabled)(&fTeleporterEnabled);
3919 if (FAILED(rc))
3920 return rc;
3921
3922 /* create a progress object */
3923 ComObjPtr<ProgressProxy> progress;
3924 progress.createObject();
3925 rc = progress->init(mParent,
3926 static_cast<IMachine*>(this),
3927 Bstr(tr("Starting VM")).raw(),
3928 TRUE /* aCancelable */,
3929 fTeleporterEnabled ? 20 : 10 /* uTotalOperationsWeight */,
3930 BstrFmt(tr("Creating process for virtual machine \"%s\" (%s)"), mUserData->s.strName.c_str(), strFrontend.c_str()).raw(),
3931 2 /* uFirstOperationWeight */,
3932 fTeleporterEnabled ? 3 : 1 /* cOtherProgressObjectOperations */);
3933
3934 if (SUCCEEDED(rc))
3935 {
3936 rc = launchVMProcess(control, strFrontend, strEnvironment, progress);
3937 if (SUCCEEDED(rc))
3938 {
3939 progress.queryInterfaceTo(aProgress);
3940
3941 /* signal the client watcher thread */
3942 mParent->i_updateClientWatcher();
3943
3944 /* fire an event */
3945 mParent->i_onSessionStateChange(getId(), SessionState_Spawning);
3946 }
3947 }
3948 }
3949 else
3950 {
3951 /* no progress object - either instant success or failure */
3952 *aProgress = NULL;
3953
3954 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
3955
3956 if (mData->mSession.mState != SessionState_Locked)
3957 return setError(VBOX_E_INVALID_OBJECT_STATE,
3958 tr("The machine '%s' is not locked by a session"),
3959 mUserData->s.strName.c_str());
3960
3961 /* must have a VM process associated - do not kill normal API clients
3962 * with an open session */
3963 if (!Global::IsOnline(mData->mMachineState))
3964 return setError(VBOX_E_INVALID_OBJECT_STATE,
3965 tr("The machine '%s' does not have a VM process"),
3966 mUserData->s.strName.c_str());
3967
3968 /* forcibly terminate the VM process */
3969 if (mData->mSession.mPID != NIL_RTPROCESS)
3970 RTProcTerminate(mData->mSession.mPID);
3971
3972 /* signal the client watcher thread, as most likely the client has
3973 * been terminated */
3974 mParent->i_updateClientWatcher();
3975 }
3976
3977 return rc;
3978}
3979
3980STDMETHODIMP Machine::SetBootOrder(ULONG aPosition, DeviceType_T aDevice)
3981{
3982 if (aPosition < 1 || aPosition > SchemaDefs::MaxBootPosition)
3983 return setError(E_INVALIDARG,
3984 tr("Invalid boot position: %lu (must be in range [1, %lu])"),
3985 aPosition, SchemaDefs::MaxBootPosition);
3986
3987 if (aDevice == DeviceType_USB)
3988 return setError(E_NOTIMPL,
3989 tr("Booting from USB device is currently not supported"));
3990
3991 AutoCaller autoCaller(this);
3992 if (FAILED(autoCaller.rc())) return autoCaller.rc();
3993
3994 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
3995
3996 HRESULT rc = checkStateDependency(MutableStateDep);
3997 if (FAILED(rc)) return rc;
3998
3999 setModified(IsModified_MachineData);
4000 mHWData.backup();
4001 mHWData->mBootOrder[aPosition - 1] = aDevice;
4002
4003 return S_OK;
4004}
4005
4006STDMETHODIMP Machine::GetBootOrder(ULONG aPosition, DeviceType_T *aDevice)
4007{
4008 if (aPosition < 1 || aPosition > SchemaDefs::MaxBootPosition)
4009 return setError(E_INVALIDARG,
4010 tr("Invalid boot position: %lu (must be in range [1, %lu])"),
4011 aPosition, SchemaDefs::MaxBootPosition);
4012
4013 AutoCaller autoCaller(this);
4014 if (FAILED(autoCaller.rc())) return autoCaller.rc();
4015
4016 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
4017
4018 *aDevice = mHWData->mBootOrder[aPosition - 1];
4019
4020 return S_OK;
4021}
4022
4023STDMETHODIMP Machine::AttachDevice(IN_BSTR aControllerName,
4024 LONG aControllerPort,
4025 LONG aDevice,
4026 DeviceType_T aType,
4027 IMedium *aMedium)
4028{
4029 LogFlowThisFunc(("aControllerName=\"%ls\" aControllerPort=%d aDevice=%d aType=%d aMedium=%p\n",
4030 aControllerName, aControllerPort, aDevice, aType, aMedium));
4031
4032 CheckComArgStrNotEmptyOrNull(aControllerName);
4033
4034 AutoCaller autoCaller(this);
4035 if (FAILED(autoCaller.rc())) return autoCaller.rc();
4036
4037 // request the host lock first, since might be calling Host methods for getting host drives;
4038 // next, protect the media tree all the while we're in here, as well as our member variables
4039 AutoMultiWriteLock2 alock(mParent->i_host(), this COMMA_LOCKVAL_SRC_POS);
4040 AutoWriteLock treeLock(&mParent->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
4041
4042 HRESULT rc = checkStateDependency(MutableStateDep);
4043 if (FAILED(rc)) return rc;
4044
4045 /// @todo NEWMEDIA implicit machine registration
4046 if (!mData->mRegistered)
4047 return setError(VBOX_E_INVALID_OBJECT_STATE,
4048 tr("Cannot attach storage devices to an unregistered machine"));
4049
4050 AssertReturn(mData->mMachineState != MachineState_Saved, E_FAIL);
4051
4052 /* Check for an existing controller. */
4053 ComObjPtr<StorageController> ctl;
4054 rc = getStorageControllerByName(aControllerName, ctl, true /* aSetError */);
4055 if (FAILED(rc)) return rc;
4056
4057 StorageControllerType_T ctrlType;
4058 rc = ctl->COMGETTER(ControllerType)(&ctrlType);
4059 if (FAILED(rc))
4060 return setError(E_FAIL,
4061 tr("Could not get type of controller '%ls'"),
4062 aControllerName);
4063
4064 bool fSilent = false;
4065 Utf8Str strReconfig;
4066
4067 /* Check whether the flag to allow silent storage attachment reconfiguration is set. */
4068 strReconfig = getExtraData(Utf8Str("VBoxInternal2/SilentReconfigureWhilePaused"));
4069 if ( mData->mMachineState == MachineState_Paused
4070 && strReconfig == "1")
4071 fSilent = true;
4072
4073 /* Check that the controller can do hotplugging if we detach the device while the VM is running. */
4074 bool fHotplug = false;
4075 if (!fSilent && Global::IsOnlineOrTransient(mData->mMachineState))
4076 fHotplug = true;
4077
4078 if (fHotplug && !isControllerHotplugCapable(ctrlType))
4079 return setError(VBOX_E_INVALID_VM_STATE,
4080 tr("Controller '%ls' does not support hotplugging"),
4081 aControllerName);
4082
4083 // check that the port and device are not out of range
4084 rc = ctl->i_checkPortAndDeviceValid(aControllerPort, aDevice);
4085 if (FAILED(rc)) return rc;
4086
4087 /* check if the device slot is already busy */
4088 MediumAttachment *pAttachTemp;
4089 if ((pAttachTemp = findAttachment(mMediaData->mAttachments,
4090 aControllerName,
4091 aControllerPort,
4092 aDevice)))
4093 {
4094 Medium *pMedium = pAttachTemp->i_getMedium();
4095 if (pMedium)
4096 {
4097 AutoReadLock mediumLock(pMedium COMMA_LOCKVAL_SRC_POS);
4098 return setError(VBOX_E_OBJECT_IN_USE,
4099 tr("Medium '%s' is already attached to port %d, device %d of controller '%ls' of this virtual machine"),
4100 pMedium->i_getLocationFull().c_str(),
4101 aControllerPort,
4102 aDevice,
4103 aControllerName);
4104 }
4105 else
4106 return setError(VBOX_E_OBJECT_IN_USE,
4107 tr("Device is already attached to port %d, device %d of controller '%ls' of this virtual machine"),
4108 aControllerPort, aDevice, aControllerName);
4109 }
4110
4111 ComObjPtr<Medium> medium = static_cast<Medium*>(aMedium);
4112 if (aMedium && medium.isNull())
4113 return setError(E_INVALIDARG, "The given medium pointer is invalid");
4114
4115 AutoCaller mediumCaller(medium);
4116 if (FAILED(mediumCaller.rc())) return mediumCaller.rc();
4117
4118 AutoWriteLock mediumLock(medium COMMA_LOCKVAL_SRC_POS);
4119
4120 if ( (pAttachTemp = findAttachment(mMediaData->mAttachments, medium))
4121 && !medium.isNull()
4122 )
4123 return setError(VBOX_E_OBJECT_IN_USE,
4124 tr("Medium '%s' is already attached to this virtual machine"),
4125 medium->i_getLocationFull().c_str());
4126
4127 if (!medium.isNull())
4128 {
4129 MediumType_T mtype = medium->i_getType();
4130 // MediumType_Readonly is also new, but only applies to DVDs and floppies.
4131 // For DVDs it's not written to the config file, so needs no global config
4132 // version bump. For floppies it's a new attribute "type", which is ignored
4133 // by older VirtualBox version, so needs no global config version bump either.
4134 // For hard disks this type is not accepted.
4135 if (mtype == MediumType_MultiAttach)
4136 {
4137 // This type is new with VirtualBox 4.0 and therefore requires settings
4138 // version 1.11 in the settings backend. Unfortunately it is not enough to do
4139 // the usual routine in MachineConfigFile::bumpSettingsVersionIfNeeded() for
4140 // two reasons: The medium type is a property of the media registry tree, which
4141 // can reside in the global config file (for pre-4.0 media); we would therefore
4142 // possibly need to bump the global config version. We don't want to do that though
4143 // because that might make downgrading to pre-4.0 impossible.
4144 // As a result, we can only use these two new types if the medium is NOT in the
4145 // global registry:
4146 const Guid &uuidGlobalRegistry = mParent->i_getGlobalRegistryId();
4147 if ( medium->i_isInRegistry(uuidGlobalRegistry)
4148 || !mData->pMachineConfigFile->canHaveOwnMediaRegistry()
4149 )
4150 return setError(VBOX_E_INVALID_OBJECT_STATE,
4151 tr("Cannot attach medium '%s': the media type 'MultiAttach' can only be attached "
4152 "to machines that were created with VirtualBox 4.0 or later"),
4153 medium->i_getLocationFull().c_str());
4154 }
4155 }
4156
4157 bool fIndirect = false;
4158 if (!medium.isNull())
4159 fIndirect = medium->i_isReadOnly();
4160 bool associate = true;
4161
4162 do
4163 {
4164 if ( aType == DeviceType_HardDisk
4165 && mMediaData.isBackedUp())
4166 {
4167 const MediaData::AttachmentList &oldAtts = mMediaData.backedUpData()->mAttachments;
4168
4169 /* check if the medium was attached to the VM before we started
4170 * changing attachments in which case the attachment just needs to
4171 * be restored */
4172 if ((pAttachTemp = findAttachment(oldAtts, medium)))
4173 {
4174 AssertReturn(!fIndirect, E_FAIL);
4175
4176 /* see if it's the same bus/channel/device */
4177 if (pAttachTemp->i_matches(aControllerName, aControllerPort, aDevice))
4178 {
4179 /* the simplest case: restore the whole attachment
4180 * and return, nothing else to do */
4181 mMediaData->mAttachments.push_back(pAttachTemp);
4182
4183 /* Reattach the medium to the VM. */
4184 if (fHotplug || fSilent)
4185 {
4186 mediumLock.release();
4187 treeLock.release();
4188 alock.release();
4189
4190 MediumLockList *pMediumLockList(new MediumLockList());
4191
4192 rc = medium->i_createMediumLockList(true /* fFailIfInaccessible */,
4193 true /* fMediumLockWrite */,
4194 NULL,
4195 *pMediumLockList);
4196 alock.acquire();
4197 if (FAILED(rc))
4198 delete pMediumLockList;
4199 else
4200 {
4201 mData->mSession.mLockedMedia.Unlock();
4202 alock.release();
4203 rc = mData->mSession.mLockedMedia.Insert(pAttachTemp, pMediumLockList);
4204 mData->mSession.mLockedMedia.Lock();
4205 alock.acquire();
4206 }
4207 alock.release();
4208
4209 if (SUCCEEDED(rc))
4210 {
4211 rc = onStorageDeviceChange(pAttachTemp, FALSE /* aRemove */, fSilent);
4212 /* Remove lock list in case of error. */
4213 if (FAILED(rc))
4214 {
4215 mData->mSession.mLockedMedia.Unlock();
4216 mData->mSession.mLockedMedia.Remove(pAttachTemp);
4217 mData->mSession.mLockedMedia.Lock();
4218 }
4219 }
4220 }
4221
4222 return S_OK;
4223 }
4224
4225 /* bus/channel/device differ; we need a new attachment object,
4226 * but don't try to associate it again */
4227 associate = false;
4228 break;
4229 }
4230 }
4231
4232 /* go further only if the attachment is to be indirect */
4233 if (!fIndirect)
4234 break;
4235
4236 /* perform the so called smart attachment logic for indirect
4237 * attachments. Note that smart attachment is only applicable to base
4238 * hard disks. */
4239
4240 if (medium->i_getParent().isNull())
4241 {
4242 /* first, investigate the backup copy of the current hard disk
4243 * attachments to make it possible to re-attach existing diffs to
4244 * another device slot w/o losing their contents */
4245 if (mMediaData.isBackedUp())
4246 {
4247 const MediaData::AttachmentList &oldAtts = mMediaData.backedUpData()->mAttachments;
4248
4249 MediaData::AttachmentList::const_iterator foundIt = oldAtts.end();
4250 uint32_t foundLevel = 0;
4251
4252 for (MediaData::AttachmentList::const_iterator it = oldAtts.begin();
4253 it != oldAtts.end();
4254 ++it)
4255 {
4256 uint32_t level = 0;
4257 MediumAttachment *pAttach = *it;
4258 ComObjPtr<Medium> pMedium = pAttach->i_getMedium();
4259 Assert(!pMedium.isNull() || pAttach->i_getType() != DeviceType_HardDisk);
4260 if (pMedium.isNull())
4261 continue;
4262
4263 if (pMedium->i_getBase(&level) == medium)
4264 {
4265 /* skip the hard disk if its currently attached (we
4266 * cannot attach the same hard disk twice) */
4267 if (findAttachment(mMediaData->mAttachments,
4268 pMedium))
4269 continue;
4270
4271 /* matched device, channel and bus (i.e. attached to the
4272 * same place) will win and immediately stop the search;
4273 * otherwise the attachment that has the youngest
4274 * descendant of medium will be used
4275 */
4276 if (pAttach->i_matches(aControllerName, aControllerPort, aDevice))
4277 {
4278 /* the simplest case: restore the whole attachment
4279 * and return, nothing else to do */
4280 mMediaData->mAttachments.push_back(*it);
4281
4282 /* Reattach the medium to the VM. */
4283 if (fHotplug || fSilent)
4284 {
4285 mediumLock.release();
4286 treeLock.release();
4287 alock.release();
4288
4289 MediumLockList *pMediumLockList(new MediumLockList());
4290
4291 rc = medium->i_createMediumLockList(true /* fFailIfInaccessible */,
4292 true /* fMediumLockWrite */,
4293 NULL,
4294 *pMediumLockList);
4295 alock.acquire();
4296 if (FAILED(rc))
4297 delete pMediumLockList;
4298 else
4299 {
4300 mData->mSession.mLockedMedia.Unlock();
4301 alock.release();
4302 rc = mData->mSession.mLockedMedia.Insert(pAttachTemp, pMediumLockList);
4303 mData->mSession.mLockedMedia.Lock();
4304 alock.acquire();
4305 }
4306 alock.release();
4307
4308 if (SUCCEEDED(rc))
4309 {
4310 rc = onStorageDeviceChange(pAttachTemp, FALSE /* aRemove */, fSilent);
4311 /* Remove lock list in case of error. */
4312 if (FAILED(rc))
4313 {
4314 mData->mSession.mLockedMedia.Unlock();
4315 mData->mSession.mLockedMedia.Remove(pAttachTemp);
4316 mData->mSession.mLockedMedia.Lock();
4317 }
4318 }
4319 }
4320
4321 return S_OK;
4322 }
4323 else if ( foundIt == oldAtts.end()
4324 || level > foundLevel /* prefer younger */
4325 )
4326 {
4327 foundIt = it;
4328 foundLevel = level;
4329 }
4330 }
4331 }
4332
4333 if (foundIt != oldAtts.end())
4334 {
4335 /* use the previously attached hard disk */
4336 medium = (*foundIt)->i_getMedium();
4337 mediumCaller.attach(medium);
4338 if (FAILED(mediumCaller.rc())) return mediumCaller.rc();
4339 mediumLock.attach(medium);
4340 /* not implicit, doesn't require association with this VM */
4341 fIndirect = false;
4342 associate = false;
4343 /* go right to the MediumAttachment creation */
4344 break;
4345 }
4346 }
4347
4348 /* must give up the medium lock and medium tree lock as below we
4349 * go over snapshots, which needs a lock with higher lock order. */
4350 mediumLock.release();
4351 treeLock.release();
4352
4353 /* then, search through snapshots for the best diff in the given
4354 * hard disk's chain to base the new diff on */
4355
4356 ComObjPtr<Medium> base;
4357 ComObjPtr<Snapshot> snap = mData->mCurrentSnapshot;
4358 while (snap)
4359 {
4360 AutoReadLock snapLock(snap COMMA_LOCKVAL_SRC_POS);
4361
4362 const MediaData::AttachmentList &snapAtts = snap->i_getSnapshotMachine()->mMediaData->mAttachments;
4363
4364 MediumAttachment *pAttachFound = NULL;
4365 uint32_t foundLevel = 0;
4366
4367 for (MediaData::AttachmentList::const_iterator it = snapAtts.begin();
4368 it != snapAtts.end();
4369 ++it)
4370 {
4371 MediumAttachment *pAttach = *it;
4372 ComObjPtr<Medium> pMedium = pAttach->i_getMedium();
4373 Assert(!pMedium.isNull() || pAttach->i_getType() != DeviceType_HardDisk);
4374 if (pMedium.isNull())
4375 continue;
4376
4377 uint32_t level = 0;
4378 if (pMedium->i_getBase(&level) == medium)
4379 {
4380 /* matched device, channel and bus (i.e. attached to the
4381 * same place) will win and immediately stop the search;
4382 * otherwise the attachment that has the youngest
4383 * descendant of medium will be used
4384 */
4385 if ( pAttach->i_getDevice() == aDevice
4386 && pAttach->i_getPort() == aControllerPort
4387 && pAttach->i_getControllerName() == aControllerName
4388 )
4389 {
4390 pAttachFound = pAttach;
4391 break;
4392 }
4393 else if ( !pAttachFound
4394 || level > foundLevel /* prefer younger */
4395 )
4396 {
4397 pAttachFound = pAttach;
4398 foundLevel = level;
4399 }
4400 }
4401 }
4402
4403 if (pAttachFound)
4404 {
4405 base = pAttachFound->i_getMedium();
4406 break;
4407 }
4408
4409 snap = snap->i_getParent();
4410 }
4411
4412 /* re-lock medium tree and the medium, as we need it below */
4413 treeLock.acquire();
4414 mediumLock.acquire();
4415
4416 /* found a suitable diff, use it as a base */
4417 if (!base.isNull())
4418 {
4419 medium = base;
4420 mediumCaller.attach(medium);
4421 if (FAILED(mediumCaller.rc())) return mediumCaller.rc();
4422 mediumLock.attach(medium);
4423 }
4424 }
4425
4426 Utf8Str strFullSnapshotFolder;
4427 calculateFullPath(mUserData->s.strSnapshotFolder, strFullSnapshotFolder);
4428
4429 ComObjPtr<Medium> diff;
4430 diff.createObject();
4431 // store this diff in the same registry as the parent
4432 Guid uuidRegistryParent;
4433 if (!medium->i_getFirstRegistryMachineId(uuidRegistryParent))
4434 {
4435 // parent image has no registry: this can happen if we're attaching a new immutable
4436 // image that has not yet been attached (medium then points to the base and we're
4437 // creating the diff image for the immutable, and the parent is not yet registered);
4438 // put the parent in the machine registry then
4439 mediumLock.release();
4440 treeLock.release();
4441 alock.release();
4442 addMediumToRegistry(medium);
4443 alock.acquire();
4444 treeLock.acquire();
4445 mediumLock.acquire();
4446 medium->i_getFirstRegistryMachineId(uuidRegistryParent);
4447 }
4448 rc = diff->init(mParent,
4449 medium->i_getPreferredDiffFormat(),
4450 strFullSnapshotFolder.append(RTPATH_SLASH_STR),
4451 uuidRegistryParent);
4452 if (FAILED(rc)) return rc;
4453
4454 /* Apply the normal locking logic to the entire chain. */
4455 MediumLockList *pMediumLockList(new MediumLockList());
4456 mediumLock.release();
4457 treeLock.release();
4458 rc = diff->i_createMediumLockList(true /* fFailIfInaccessible */,
4459 true /* fMediumLockWrite */,
4460 medium,
4461 *pMediumLockList);
4462 treeLock.acquire();
4463 mediumLock.acquire();
4464 if (SUCCEEDED(rc))
4465 {
4466 mediumLock.release();
4467 treeLock.release();
4468 rc = pMediumLockList->Lock();
4469 treeLock.acquire();
4470 mediumLock.acquire();
4471 if (FAILED(rc))
4472 setError(rc,
4473 tr("Could not lock medium when creating diff '%s'"),
4474 diff->i_getLocationFull().c_str());
4475 else
4476 {
4477 /* will release the lock before the potentially lengthy
4478 * operation, so protect with the special state */
4479 MachineState_T oldState = mData->mMachineState;
4480 setMachineState(MachineState_SettingUp);
4481
4482 mediumLock.release();
4483 treeLock.release();
4484 alock.release();
4485
4486 rc = medium->i_createDiffStorage(diff,
4487 MediumVariant_Standard,
4488 pMediumLockList,
4489 NULL /* aProgress */,
4490 true /* aWait */);
4491
4492 alock.acquire();
4493 treeLock.acquire();
4494 mediumLock.acquire();
4495
4496 setMachineState(oldState);
4497 }
4498 }
4499
4500 /* Unlock the media and free the associated memory. */
4501 delete pMediumLockList;
4502
4503 if (FAILED(rc)) return rc;
4504
4505 /* use the created diff for the actual attachment */
4506 medium = diff;
4507 mediumCaller.attach(medium);
4508 if (FAILED(mediumCaller.rc())) return mediumCaller.rc();
4509 mediumLock.attach(medium);
4510 }
4511 while (0);
4512
4513 ComObjPtr<MediumAttachment> attachment;
4514 attachment.createObject();
4515 rc = attachment->init(this,
4516 medium,
4517 aControllerName,
4518 aControllerPort,
4519 aDevice,
4520 aType,
4521 fIndirect,
4522 false /* fPassthrough */,
4523 false /* fTempEject */,
4524 false /* fNonRotational */,
4525 false /* fDiscard */,
4526 fHotplug /* fHotPluggable */,
4527 Utf8Str::Empty);
4528 if (FAILED(rc)) return rc;
4529
4530 if (associate && !medium.isNull())
4531 {
4532 // as the last step, associate the medium to the VM
4533 rc = medium->i_addBackReference(mData->mUuid);
4534 // here we can fail because of Deleting, or being in process of creating a Diff
4535 if (FAILED(rc)) return rc;
4536
4537 mediumLock.release();
4538 treeLock.release();
4539 alock.release();
4540 addMediumToRegistry(medium);
4541 alock.acquire();
4542 treeLock.acquire();
4543 mediumLock.acquire();
4544 }
4545
4546 /* success: finally remember the attachment */
4547 setModified(IsModified_Storage);
4548 mMediaData.backup();
4549 mMediaData->mAttachments.push_back(attachment);
4550
4551 mediumLock.release();
4552 treeLock.release();
4553 alock.release();
4554
4555 if (fHotplug || fSilent)
4556 {
4557 if (!medium.isNull())
4558 {
4559 MediumLockList *pMediumLockList(new MediumLockList());
4560
4561 rc = medium->i_createMediumLockList(true /* fFailIfInaccessible */,
4562 true /* fMediumLockWrite */,
4563 NULL,
4564 *pMediumLockList);
4565 alock.acquire();
4566 if (FAILED(rc))
4567 delete pMediumLockList;
4568 else
4569 {
4570 mData->mSession.mLockedMedia.Unlock();
4571 alock.release();
4572 rc = mData->mSession.mLockedMedia.Insert(attachment, pMediumLockList);
4573 mData->mSession.mLockedMedia.Lock();
4574 alock.acquire();
4575 }
4576 alock.release();
4577 }
4578
4579 if (SUCCEEDED(rc))
4580 {
4581 rc = onStorageDeviceChange(attachment, FALSE /* aRemove */, fSilent);
4582 /* Remove lock list in case of error. */
4583 if (FAILED(rc))
4584 {
4585 mData->mSession.mLockedMedia.Unlock();
4586 mData->mSession.mLockedMedia.Remove(attachment);
4587 mData->mSession.mLockedMedia.Lock();
4588 }
4589 }
4590 }
4591
4592 mParent->i_saveModifiedRegistries();
4593
4594 return rc;
4595}
4596
4597STDMETHODIMP Machine::DetachDevice(IN_BSTR aControllerName, LONG aControllerPort,
4598 LONG aDevice)
4599{
4600 CheckComArgStrNotEmptyOrNull(aControllerName);
4601
4602 LogFlowThisFunc(("aControllerName=\"%ls\" aControllerPort=%d aDevice=%d\n",
4603 aControllerName, aControllerPort, aDevice));
4604
4605 AutoCaller autoCaller(this);
4606 if (FAILED(autoCaller.rc())) return autoCaller.rc();
4607
4608 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
4609
4610 HRESULT rc = checkStateDependency(MutableStateDep);
4611 if (FAILED(rc)) return rc;
4612
4613 AssertReturn(mData->mMachineState != MachineState_Saved, E_FAIL);
4614
4615 /* Check for an existing controller. */
4616 ComObjPtr<StorageController> ctl;
4617 rc = getStorageControllerByName(aControllerName, ctl, true /* aSetError */);
4618 if (FAILED(rc)) return rc;
4619
4620 StorageControllerType_T ctrlType;
4621 rc = ctl->COMGETTER(ControllerType)(&ctrlType);
4622 if (FAILED(rc))
4623 return setError(E_FAIL,
4624 tr("Could not get type of controller '%ls'"),
4625 aControllerName);
4626
4627 bool fSilent = false;
4628 Utf8Str strReconfig;
4629
4630 /* Check whether the flag to allow silent storage attachment reconfiguration is set. */
4631 strReconfig = getExtraData(Utf8Str("VBoxInternal2/SilentReconfigureWhilePaused"));
4632 if ( mData->mMachineState == MachineState_Paused
4633 && strReconfig == "1")
4634 fSilent = true;
4635
4636 /* Check that the controller can do hotplugging if we detach the device while the VM is running. */
4637 bool fHotplug = false;
4638 if (!fSilent && Global::IsOnlineOrTransient(mData->mMachineState))
4639 fHotplug = true;
4640
4641 if (fHotplug && !isControllerHotplugCapable(ctrlType))
4642 return setError(VBOX_E_INVALID_VM_STATE,
4643 tr("Controller '%ls' does not support hotplugging"),
4644 aControllerName);
4645
4646 MediumAttachment *pAttach = findAttachment(mMediaData->mAttachments,
4647 aControllerName,
4648 aControllerPort,
4649 aDevice);
4650 if (!pAttach)
4651 return setError(VBOX_E_OBJECT_NOT_FOUND,
4652 tr("No storage device attached to device slot %d on port %d of controller '%ls'"),
4653 aDevice, aControllerPort, aControllerName);
4654
4655 if (fHotplug && !pAttach->i_getHotPluggable())
4656 return setError(VBOX_E_NOT_SUPPORTED,
4657 tr("The device slot %d on port %d of controller '%ls' does not support hotplugging"),
4658 aDevice, aControllerPort, aControllerName);
4659
4660 /*
4661 * The VM has to detach the device before we delete any implicit diffs.
4662 * If this fails we can roll back without loosing data.
4663 */
4664 if (fHotplug || fSilent)
4665 {
4666 alock.release();
4667 rc = onStorageDeviceChange(pAttach, TRUE /* aRemove */, fSilent);
4668 alock.acquire();
4669 }
4670 if (FAILED(rc)) return rc;
4671
4672 /* If we are here everything went well and we can delete the implicit now. */
4673 rc = detachDevice(pAttach, alock, NULL /* pSnapshot */);
4674
4675 alock.release();
4676
4677 mParent->i_saveModifiedRegistries();
4678
4679 return rc;
4680}
4681
4682STDMETHODIMP Machine::PassthroughDevice(IN_BSTR aControllerName, LONG aControllerPort,
4683 LONG aDevice, BOOL aPassthrough)
4684{
4685 CheckComArgStrNotEmptyOrNull(aControllerName);
4686
4687 LogFlowThisFunc(("aControllerName=\"%ls\" aControllerPort=%d aDevice=%d aPassthrough=%d\n",
4688 aControllerName, aControllerPort, aDevice, aPassthrough));
4689
4690 AutoCaller autoCaller(this);
4691 if (FAILED(autoCaller.rc())) return autoCaller.rc();
4692
4693 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
4694
4695 HRESULT rc = checkStateDependency(MutableStateDep);
4696 if (FAILED(rc)) return rc;
4697
4698 AssertReturn(mData->mMachineState != MachineState_Saved, E_FAIL);
4699
4700 if (Global::IsOnlineOrTransient(mData->mMachineState))
4701 return setError(VBOX_E_INVALID_VM_STATE,
4702 tr("Invalid machine state: %s"),
4703 Global::stringifyMachineState(mData->mMachineState));
4704
4705 MediumAttachment *pAttach = findAttachment(mMediaData->mAttachments,
4706 aControllerName,
4707 aControllerPort,
4708 aDevice);
4709 if (!pAttach)
4710 return setError(VBOX_E_OBJECT_NOT_FOUND,
4711 tr("No storage device attached to device slot %d on port %d of controller '%ls'"),
4712 aDevice, aControllerPort, aControllerName);
4713
4714
4715 setModified(IsModified_Storage);
4716 mMediaData.backup();
4717
4718 AutoWriteLock attLock(pAttach COMMA_LOCKVAL_SRC_POS);
4719
4720 if (pAttach->i_getType() != DeviceType_DVD)
4721 return setError(E_INVALIDARG,
4722 tr("Setting passthrough rejected as the device attached to device slot %d on port %d of controller '%ls' is not a DVD"),
4723 aDevice, aControllerPort, aControllerName);
4724 pAttach->i_updatePassthrough(!!aPassthrough);
4725
4726 return S_OK;
4727}
4728
4729STDMETHODIMP Machine::TemporaryEjectDevice(IN_BSTR aControllerName, LONG aControllerPort,
4730 LONG aDevice, BOOL aTemporaryEject)
4731{
4732 CheckComArgStrNotEmptyOrNull(aControllerName);
4733
4734 LogFlowThisFunc(("aControllerName=\"%ls\" aControllerPort=%d aDevice=%d aTemporaryEject=%d\n",
4735 aControllerName, aControllerPort, aDevice, aTemporaryEject));
4736
4737 AutoCaller autoCaller(this);
4738 if (FAILED(autoCaller.rc())) return autoCaller.rc();
4739
4740 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
4741
4742 HRESULT rc = checkStateDependency(MutableStateDep);
4743 if (FAILED(rc)) return rc;
4744
4745 MediumAttachment *pAttach = findAttachment(mMediaData->mAttachments,
4746 aControllerName,
4747 aControllerPort,
4748 aDevice);
4749 if (!pAttach)
4750 return setError(VBOX_E_OBJECT_NOT_FOUND,
4751 tr("No storage device attached to device slot %d on port %d of controller '%ls'"),
4752 aDevice, aControllerPort, aControllerName);
4753
4754
4755 setModified(IsModified_Storage);
4756 mMediaData.backup();
4757
4758 AutoWriteLock attLock(pAttach COMMA_LOCKVAL_SRC_POS);
4759
4760 if (pAttach->i_getType() != DeviceType_DVD)
4761 return setError(E_INVALIDARG,
4762 tr("Setting temporary eject flag rejected as the device attached to device slot %d on port %d of controller '%ls' is not a DVD"),
4763 aDevice, aControllerPort, aControllerName);
4764 pAttach->i_updateTempEject(!!aTemporaryEject);
4765
4766 return S_OK;
4767}
4768
4769STDMETHODIMP Machine::NonRotationalDevice(IN_BSTR aControllerName, LONG aControllerPort,
4770 LONG aDevice, BOOL aNonRotational)
4771{
4772 CheckComArgStrNotEmptyOrNull(aControllerName);
4773
4774 LogFlowThisFunc(("aControllerName=\"%ls\" aControllerPort=%d aDevice=%d aNonRotational=%d\n",
4775 aControllerName, aControllerPort, aDevice, aNonRotational));
4776
4777 AutoCaller autoCaller(this);
4778 if (FAILED(autoCaller.rc())) return autoCaller.rc();
4779
4780 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
4781
4782 HRESULT rc = checkStateDependency(MutableStateDep);
4783 if (FAILED(rc)) return rc;
4784
4785 AssertReturn(mData->mMachineState != MachineState_Saved, E_FAIL);
4786
4787 if (Global::IsOnlineOrTransient(mData->mMachineState))
4788 return setError(VBOX_E_INVALID_VM_STATE,
4789 tr("Invalid machine state: %s"),
4790 Global::stringifyMachineState(mData->mMachineState));
4791
4792 MediumAttachment *pAttach = findAttachment(mMediaData->mAttachments,
4793 aControllerName,
4794 aControllerPort,
4795 aDevice);
4796 if (!pAttach)
4797 return setError(VBOX_E_OBJECT_NOT_FOUND,
4798 tr("No storage device attached to device slot %d on port %d of controller '%ls'"),
4799 aDevice, aControllerPort, aControllerName);
4800
4801
4802 setModified(IsModified_Storage);
4803 mMediaData.backup();
4804
4805 AutoWriteLock attLock(pAttach COMMA_LOCKVAL_SRC_POS);
4806
4807 if (pAttach->i_getType() != DeviceType_HardDisk)
4808 return setError(E_INVALIDARG,
4809 tr("Setting the non-rotational medium flag rejected as the device attached to device slot %d on port %d of controller '%ls' is not a hard disk"),
4810 aDevice, aControllerPort, aControllerName);
4811 pAttach->i_updateNonRotational(!!aNonRotational);
4812
4813 return S_OK;
4814}
4815
4816STDMETHODIMP Machine::SetAutoDiscardForDevice(IN_BSTR aControllerName, LONG aControllerPort,
4817 LONG aDevice, BOOL aDiscard)
4818{
4819 CheckComArgStrNotEmptyOrNull(aControllerName);
4820
4821 LogFlowThisFunc(("aControllerName=\"%ls\" aControllerPort=%d aDevice=%d aDiscard=%d\n",
4822 aControllerName, aControllerPort, aDevice, aDiscard));
4823
4824 AutoCaller autoCaller(this);
4825 if (FAILED(autoCaller.rc())) return autoCaller.rc();
4826
4827 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
4828
4829 HRESULT rc = checkStateDependency(MutableStateDep);
4830 if (FAILED(rc)) return rc;
4831
4832 AssertReturn(mData->mMachineState != MachineState_Saved, E_FAIL);
4833
4834 if (Global::IsOnlineOrTransient(mData->mMachineState))
4835 return setError(VBOX_E_INVALID_VM_STATE,
4836 tr("Invalid machine state: %s"),
4837 Global::stringifyMachineState(mData->mMachineState));
4838
4839 MediumAttachment *pAttach = findAttachment(mMediaData->mAttachments,
4840 aControllerName,
4841 aControllerPort,
4842 aDevice);
4843 if (!pAttach)
4844 return setError(VBOX_E_OBJECT_NOT_FOUND,
4845 tr("No storage device attached to device slot %d on port %d of controller '%ls'"),
4846 aDevice, aControllerPort, aControllerName);
4847
4848
4849 setModified(IsModified_Storage);
4850 mMediaData.backup();
4851
4852 AutoWriteLock attLock(pAttach COMMA_LOCKVAL_SRC_POS);
4853
4854 if (pAttach->i_getType() != DeviceType_HardDisk)
4855 return setError(E_INVALIDARG,
4856 tr("Setting the discard medium flag rejected as the device attached to device slot %d on port %d of controller '%ls' is not a hard disk"),
4857 aDevice, aControllerPort, aControllerName);
4858 pAttach->i_updateDiscard(!!aDiscard);
4859
4860 return S_OK;
4861}
4862
4863STDMETHODIMP Machine::SetHotPluggableForDevice(IN_BSTR aControllerName, LONG aControllerPort,
4864 LONG aDevice, BOOL aHotPluggable)
4865{
4866 CheckComArgStrNotEmptyOrNull(aControllerName);
4867
4868 LogFlowThisFunc(("aControllerName=\"%ls\" aControllerPort=%d aDevice=%d aHotPluggable=%d\n",
4869 aControllerName, aControllerPort, aDevice, aHotPluggable));
4870
4871 AutoCaller autoCaller(this);
4872 if (FAILED(autoCaller.rc())) return autoCaller.rc();
4873
4874 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
4875
4876 HRESULT rc = checkStateDependency(MutableStateDep);
4877 if (FAILED(rc)) return rc;
4878
4879 AssertReturn(mData->mMachineState != MachineState_Saved, E_FAIL);
4880
4881 if (Global::IsOnlineOrTransient(mData->mMachineState))
4882 return setError(VBOX_E_INVALID_VM_STATE,
4883 tr("Invalid machine state: %s"),
4884 Global::stringifyMachineState(mData->mMachineState));
4885
4886 MediumAttachment *pAttach = findAttachment(mMediaData->mAttachments,
4887 aControllerName,
4888 aControllerPort,
4889 aDevice);
4890 if (!pAttach)
4891 return setError(VBOX_E_OBJECT_NOT_FOUND,
4892 tr("No storage device attached to device slot %d on port %d of controller '%ls'"),
4893 aDevice, aControllerPort, aControllerName);
4894
4895 /* Check for an existing controller. */
4896 ComObjPtr<StorageController> ctl;
4897 rc = getStorageControllerByName(aControllerName, ctl, true /* aSetError */);
4898 if (FAILED(rc)) return rc;
4899
4900 StorageControllerType_T ctrlType;
4901 rc = ctl->COMGETTER(ControllerType)(&ctrlType);
4902 if (FAILED(rc))
4903 return setError(E_FAIL,
4904 tr("Could not get type of controller '%ls'"),
4905 aControllerName);
4906
4907 if (!isControllerHotplugCapable(ctrlType))
4908 return setError(VBOX_E_NOT_SUPPORTED,
4909 tr("Controller '%ls' does not support changing the hot-pluggable device flag"),
4910 aControllerName);
4911
4912 setModified(IsModified_Storage);
4913 mMediaData.backup();
4914
4915 AutoWriteLock attLock(pAttach COMMA_LOCKVAL_SRC_POS);
4916
4917 if (pAttach->i_getType() == DeviceType_Floppy)
4918 return setError(E_INVALIDARG,
4919 tr("Setting the hot-pluggable device flag rejected as the device attached to device slot %d on port %d of controller '%ls' is a floppy drive"),
4920 aDevice, aControllerPort, aControllerName);
4921 pAttach->i_updateHotPluggable(!!aHotPluggable);
4922
4923 return S_OK;
4924}
4925
4926STDMETHODIMP Machine::SetNoBandwidthGroupForDevice(IN_BSTR aControllerName, LONG aControllerPort,
4927 LONG aDevice)
4928{
4929 int rc = S_OK;
4930 LogFlowThisFunc(("aControllerName=\"%ls\" aControllerPort=%d aDevice=%d\n",
4931 aControllerName, aControllerPort, aDevice));
4932
4933 rc = SetBandwidthGroupForDevice(aControllerName, aControllerPort, aDevice, NULL);
4934
4935 return rc;
4936}
4937
4938STDMETHODIMP Machine::SetBandwidthGroupForDevice(IN_BSTR aControllerName, LONG aControllerPort,
4939 LONG aDevice, IBandwidthGroup *aBandwidthGroup)
4940{
4941 CheckComArgStrNotEmptyOrNull(aControllerName);
4942
4943 LogFlowThisFunc(("aControllerName=\"%ls\" aControllerPort=%d aDevice=%d\n",
4944 aControllerName, aControllerPort, aDevice));
4945
4946 AutoCaller autoCaller(this);
4947 if (FAILED(autoCaller.rc())) return autoCaller.rc();
4948
4949 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
4950
4951 HRESULT rc = checkStateDependency(MutableStateDep);
4952 if (FAILED(rc)) return rc;
4953
4954 AssertReturn(mData->mMachineState != MachineState_Saved, E_FAIL);
4955
4956 if (Global::IsOnlineOrTransient(mData->mMachineState))
4957 return setError(VBOX_E_INVALID_VM_STATE,
4958 tr("Invalid machine state: %s"),
4959 Global::stringifyMachineState(mData->mMachineState));
4960
4961 MediumAttachment *pAttach = findAttachment(mMediaData->mAttachments,
4962 aControllerName,
4963 aControllerPort,
4964 aDevice);
4965 if (!pAttach)
4966 return setError(VBOX_E_OBJECT_NOT_FOUND,
4967 tr("No storage device attached to device slot %d on port %d of controller '%ls'"),
4968 aDevice, aControllerPort, aControllerName);
4969
4970
4971 setModified(IsModified_Storage);
4972 mMediaData.backup();
4973
4974 ComObjPtr<BandwidthGroup> group = static_cast<BandwidthGroup*>(aBandwidthGroup);
4975 if (aBandwidthGroup && group.isNull())
4976 return setError(E_INVALIDARG, "The given bandwidth group pointer is invalid");
4977
4978 AutoWriteLock attLock(pAttach COMMA_LOCKVAL_SRC_POS);
4979
4980 const Utf8Str strBandwidthGroupOld = pAttach->i_getBandwidthGroup();
4981 if (strBandwidthGroupOld.isNotEmpty())
4982 {
4983 /* Get the bandwidth group object and release it - this must not fail. */
4984 ComObjPtr<BandwidthGroup> pBandwidthGroupOld;
4985 rc = getBandwidthGroup(strBandwidthGroupOld, pBandwidthGroupOld, false);
4986 Assert(SUCCEEDED(rc));
4987
4988 pBandwidthGroupOld->i_release();
4989 pAttach->i_updateBandwidthGroup(Utf8Str::Empty);
4990 }
4991
4992 if (!group.isNull())
4993 {
4994 group->i_reference();
4995 pAttach->i_updateBandwidthGroup(group->i_getName());
4996 }
4997
4998 return S_OK;
4999}
5000
5001STDMETHODIMP Machine::AttachDeviceWithoutMedium(IN_BSTR aControllerName,
5002 LONG aControllerPort,
5003 LONG aDevice,
5004 DeviceType_T aType)
5005{
5006 HRESULT rc = S_OK;
5007
5008 LogFlowThisFunc(("aControllerName=\"%ls\" aControllerPort=%d aDevice=%d aType=%d aMedium=%p\n",
5009 aControllerName, aControllerPort, aDevice, aType));
5010
5011 rc = AttachDevice(aControllerName, aControllerPort, aDevice, aType, NULL);
5012
5013 return rc;
5014}
5015
5016
5017
5018STDMETHODIMP Machine::UnmountMedium(IN_BSTR aControllerName,
5019 LONG aControllerPort,
5020 LONG aDevice,
5021 BOOL aForce)
5022{
5023 int rc = S_OK;
5024 LogFlowThisFunc(("aControllerName=\"%ls\" aControllerPort=%d aDevice=%d",
5025 aControllerName, aControllerPort, aForce));
5026
5027 rc = MountMedium(aControllerName, aControllerPort, aDevice, NULL, aForce);
5028
5029 return rc;
5030}
5031
5032STDMETHODIMP Machine::MountMedium(IN_BSTR aControllerName,
5033 LONG aControllerPort,
5034 LONG aDevice,
5035 IMedium *aMedium,
5036 BOOL aForce)
5037{
5038 int rc = S_OK;
5039 LogFlowThisFunc(("aControllerName=\"%ls\" aControllerPort=%d aDevice=%d aForce=%d\n",
5040 aControllerName, aControllerPort, aDevice, aForce));
5041
5042 CheckComArgStrNotEmptyOrNull(aControllerName);
5043
5044 AutoCaller autoCaller(this);
5045 if (FAILED(autoCaller.rc())) return autoCaller.rc();
5046
5047 // request the host lock first, since might be calling Host methods for getting host drives;
5048 // next, protect the media tree all the while we're in here, as well as our member variables
5049 AutoMultiWriteLock3 multiLock(mParent->i_host()->lockHandle(),
5050 this->lockHandle(),
5051 &mParent->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
5052
5053 ComObjPtr<MediumAttachment> pAttach = findAttachment(mMediaData->mAttachments,
5054 aControllerName,
5055 aControllerPort,
5056 aDevice);
5057 if (pAttach.isNull())
5058 return setError(VBOX_E_OBJECT_NOT_FOUND,
5059 tr("No drive attached to device slot %d on port %d of controller '%ls'"),
5060 aDevice, aControllerPort, aControllerName);
5061
5062 /* Remember previously mounted medium. The medium before taking the
5063 * backup is not necessarily the same thing. */
5064 ComObjPtr<Medium> oldmedium;
5065 oldmedium = pAttach->i_getMedium();
5066
5067 ComObjPtr<Medium> pMedium = static_cast<Medium*>(aMedium);
5068 if (aMedium && pMedium.isNull())
5069 return setError(E_INVALIDARG, "The given medium pointer is invalid");
5070
5071 AutoCaller mediumCaller(pMedium);
5072 if (FAILED(mediumCaller.rc())) return mediumCaller.rc();
5073
5074 AutoWriteLock mediumLock(pMedium COMMA_LOCKVAL_SRC_POS);
5075 if (pMedium)
5076 {
5077 DeviceType_T mediumType = pAttach->i_getType();
5078 switch (mediumType)
5079 {
5080 case DeviceType_DVD:
5081 case DeviceType_Floppy:
5082 break;
5083
5084 default:
5085 return setError(VBOX_E_INVALID_OBJECT_STATE,
5086 tr("The device at port %d, device %d of controller '%ls' of this virtual machine is not removeable"),
5087 aControllerPort,
5088 aDevice,
5089 aControllerName);
5090 }
5091 }
5092
5093 setModified(IsModified_Storage);
5094 mMediaData.backup();
5095
5096 {
5097 // The backup operation makes the pAttach reference point to the
5098 // old settings. Re-get the correct reference.
5099 pAttach = findAttachment(mMediaData->mAttachments,
5100 aControllerName,
5101 aControllerPort,
5102 aDevice);
5103 if (!oldmedium.isNull())
5104 oldmedium->i_removeBackReference(mData->mUuid);
5105 if (!pMedium.isNull())
5106 {
5107 pMedium->i_addBackReference(mData->mUuid);
5108
5109 mediumLock.release();
5110 multiLock.release();
5111 addMediumToRegistry(pMedium);
5112 multiLock.acquire();
5113 mediumLock.acquire();
5114 }
5115
5116 AutoWriteLock attLock(pAttach COMMA_LOCKVAL_SRC_POS);
5117 pAttach->i_updateMedium(pMedium);
5118 }
5119
5120 setModified(IsModified_Storage);
5121
5122 mediumLock.release();
5123 multiLock.release();
5124 rc = onMediumChange(pAttach, aForce);
5125 multiLock.acquire();
5126 mediumLock.acquire();
5127
5128 /* On error roll back this change only. */
5129 if (FAILED(rc))
5130 {
5131 if (!pMedium.isNull())
5132 pMedium->i_removeBackReference(mData->mUuid);
5133 pAttach = findAttachment(mMediaData->mAttachments,
5134 aControllerName,
5135 aControllerPort,
5136 aDevice);
5137 /* If the attachment is gone in the meantime, bail out. */
5138 if (pAttach.isNull())
5139 return rc;
5140 AutoWriteLock attLock(pAttach COMMA_LOCKVAL_SRC_POS);
5141 if (!oldmedium.isNull())
5142 oldmedium->i_addBackReference(mData->mUuid);
5143 pAttach->i_updateMedium(oldmedium);
5144 }
5145
5146 mediumLock.release();
5147 multiLock.release();
5148
5149 mParent->i_saveModifiedRegistries();
5150
5151 return rc;
5152}
5153
5154STDMETHODIMP Machine::GetMedium(IN_BSTR aControllerName,
5155 LONG aControllerPort,
5156 LONG aDevice,
5157 IMedium **aMedium)
5158{
5159 LogFlowThisFunc(("aControllerName=\"%ls\" aControllerPort=%d aDevice=%d\n",
5160 aControllerName, aControllerPort, aDevice));
5161
5162 CheckComArgStrNotEmptyOrNull(aControllerName);
5163 CheckComArgOutPointerValid(aMedium);
5164
5165 AutoCaller autoCaller(this);
5166 if (FAILED(autoCaller.rc())) return autoCaller.rc();
5167
5168 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
5169
5170 *aMedium = NULL;
5171
5172 ComObjPtr<MediumAttachment> pAttach = findAttachment(mMediaData->mAttachments,
5173 aControllerName,
5174 aControllerPort,
5175 aDevice);
5176 if (pAttach.isNull())
5177 return setError(VBOX_E_OBJECT_NOT_FOUND,
5178 tr("No storage device attached to device slot %d on port %d of controller '%ls'"),
5179 aDevice, aControllerPort, aControllerName);
5180
5181 pAttach->i_getMedium().queryInterfaceTo(aMedium);
5182
5183 return S_OK;
5184}
5185
5186STDMETHODIMP Machine::GetSerialPort(ULONG slot, ISerialPort **port)
5187{
5188 CheckComArgOutPointerValid(port);
5189 CheckComArgExpr(slot, slot < RT_ELEMENTS(mSerialPorts));
5190
5191 AutoCaller autoCaller(this);
5192 if (FAILED(autoCaller.rc())) return autoCaller.rc();
5193
5194 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
5195
5196 mSerialPorts[slot].queryInterfaceTo(port);
5197
5198 return S_OK;
5199}
5200
5201STDMETHODIMP Machine::GetParallelPort(ULONG slot, IParallelPort **port)
5202{
5203 CheckComArgOutPointerValid(port);
5204 CheckComArgExpr(slot, slot < RT_ELEMENTS(mParallelPorts));
5205
5206 AutoCaller autoCaller(this);
5207 if (FAILED(autoCaller.rc())) return autoCaller.rc();
5208
5209 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
5210
5211 mParallelPorts[slot].queryInterfaceTo(port);
5212
5213 return S_OK;
5214}
5215
5216STDMETHODIMP Machine::GetNetworkAdapter(ULONG slot, INetworkAdapter **adapter)
5217{
5218 CheckComArgOutPointerValid(adapter);
5219 /* Do not assert if slot is out of range, just return the advertised
5220 status. testdriver/vbox.py triggers this in logVmInfo. */
5221 if (slot >= mNetworkAdapters.size())
5222 return setError(E_INVALIDARG,
5223 tr("No network adapter in slot %RU32 (total %RU32 adapters)"),
5224 slot, mNetworkAdapters.size());
5225
5226 AutoCaller autoCaller(this);
5227 if (FAILED(autoCaller.rc())) return autoCaller.rc();
5228
5229 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
5230
5231 mNetworkAdapters[slot].queryInterfaceTo(adapter);
5232
5233 return S_OK;
5234}
5235
5236STDMETHODIMP Machine::GetExtraDataKeys(ComSafeArrayOut(BSTR, aKeys))
5237{
5238 CheckComArgOutSafeArrayPointerValid(aKeys);
5239
5240 AutoCaller autoCaller(this);
5241 if (FAILED(autoCaller.rc())) return autoCaller.rc();
5242
5243 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
5244
5245 com::SafeArray<BSTR> saKeys(mData->pMachineConfigFile->mapExtraDataItems.size());
5246 int i = 0;
5247 for (settings::StringsMap::const_iterator it = mData->pMachineConfigFile->mapExtraDataItems.begin();
5248 it != mData->pMachineConfigFile->mapExtraDataItems.end();
5249 ++it, ++i)
5250 {
5251 const Utf8Str &strKey = it->first;
5252 strKey.cloneTo(&saKeys[i]);
5253 }
5254 saKeys.detachTo(ComSafeArrayOutArg(aKeys));
5255
5256 return S_OK;
5257 }
5258
5259 /**
5260 * @note Locks this object for reading.
5261 */
5262STDMETHODIMP Machine::GetExtraData(IN_BSTR aKey,
5263 BSTR *aValue)
5264{
5265 CheckComArgStrNotEmptyOrNull(aKey);
5266 CheckComArgOutPointerValid(aValue);
5267
5268 AutoCaller autoCaller(this);
5269 if (FAILED(autoCaller.rc())) return autoCaller.rc();
5270
5271 /* start with nothing found */
5272 Bstr bstrResult("");
5273
5274 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
5275
5276 settings::StringsMap::const_iterator it = mData->pMachineConfigFile->mapExtraDataItems.find(Utf8Str(aKey));
5277 if (it != mData->pMachineConfigFile->mapExtraDataItems.end())
5278 // found:
5279 bstrResult = it->second; // source is a Utf8Str
5280
5281 /* return the result to caller (may be empty) */
5282 bstrResult.cloneTo(aValue);
5283
5284 return S_OK;
5285}
5286
5287 /**
5288 * @note Locks mParent for writing + this object for writing.
5289 */
5290STDMETHODIMP Machine::SetExtraData(IN_BSTR aKey, IN_BSTR aValue)
5291{
5292 CheckComArgStrNotEmptyOrNull(aKey);
5293
5294 AutoCaller autoCaller(this);
5295 if (FAILED(autoCaller.rc())) return autoCaller.rc();
5296
5297 Utf8Str strKey(aKey);
5298 Utf8Str strValue(aValue);
5299 Utf8Str strOldValue; // empty
5300
5301 // locking note: we only hold the read lock briefly to look up the old value,
5302 // then release it and call the onExtraCanChange callbacks. There is a small
5303 // chance of a race insofar as the callback might be called twice if two callers
5304 // change the same key at the same time, but that's a much better solution
5305 // than the deadlock we had here before. The actual changing of the extradata
5306 // is then performed under the write lock and race-free.
5307
5308 // look up the old value first; if nothing has changed then we need not do anything
5309 {
5310 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS); // hold read lock only while looking up
5311 settings::StringsMap::const_iterator it = mData->pMachineConfigFile->mapExtraDataItems.find(strKey);
5312 if (it != mData->pMachineConfigFile->mapExtraDataItems.end())
5313 strOldValue = it->second;
5314 }
5315
5316 bool fChanged;
5317 if ((fChanged = (strOldValue != strValue)))
5318 {
5319 // ask for permission from all listeners outside the locks;
5320 // i_onExtraDataCanChange() only briefly requests the VirtualBox
5321 // lock to copy the list of callbacks to invoke
5322 Bstr error;
5323 Bstr bstrValue(aValue);
5324
5325 if (!mParent->i_onExtraDataCanChange(mData->mUuid, aKey, bstrValue.raw(), error))
5326 {
5327 const char *sep = error.isEmpty() ? "" : ": ";
5328 CBSTR err = error.raw();
5329 LogWarningFunc(("Someone vetoed! Change refused%s%ls\n",
5330 sep, err));
5331 return setError(E_ACCESSDENIED,
5332 tr("Could not set extra data because someone refused the requested change of '%ls' to '%ls'%s%ls"),
5333 aKey,
5334 bstrValue.raw(),
5335 sep,
5336 err);
5337 }
5338
5339 // data is changing and change not vetoed: then write it out under the lock
5340 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
5341
5342 if (isSnapshotMachine())
5343 {
5344 HRESULT rc = checkStateDependency(MutableStateDep);
5345 if (FAILED(rc)) return rc;
5346 }
5347
5348 if (strValue.isEmpty())
5349 mData->pMachineConfigFile->mapExtraDataItems.erase(strKey);
5350 else
5351 mData->pMachineConfigFile->mapExtraDataItems[strKey] = strValue;
5352 // creates a new key if needed
5353
5354 bool fNeedsGlobalSaveSettings = false;
5355 saveSettings(&fNeedsGlobalSaveSettings);
5356
5357 if (fNeedsGlobalSaveSettings)
5358 {
5359 // save the global settings; for that we should hold only the VirtualBox lock
5360 alock.release();
5361 AutoWriteLock vboxlock(mParent COMMA_LOCKVAL_SRC_POS);
5362 mParent->i_saveSettings();
5363 }
5364 }
5365
5366 // fire notification outside the lock
5367 if (fChanged)
5368 mParent->i_onExtraDataChange(mData->mUuid, aKey, aValue);
5369
5370 return S_OK;
5371}
5372
5373STDMETHODIMP Machine::SetSettingsFilePath(IN_BSTR aFilePath, IProgress **aProgress)
5374{
5375 CheckComArgStrNotEmptyOrNull(aFilePath);
5376 CheckComArgOutPointerValid(aProgress);
5377
5378 AutoCaller autoCaller(this);
5379 if (FAILED(autoCaller.rc())) return autoCaller.rc();
5380
5381 *aProgress = NULL;
5382 ReturnComNotImplemented();
5383}
5384
5385STDMETHODIMP Machine::SaveSettings()
5386{
5387 AutoCaller autoCaller(this);
5388 if (FAILED(autoCaller.rc())) return autoCaller.rc();
5389
5390 AutoWriteLock mlock(this COMMA_LOCKVAL_SRC_POS);
5391
5392 /* when there was auto-conversion, we want to save the file even if
5393 * the VM is saved */
5394 HRESULT rc = checkStateDependency(MutableOrSavedStateDep);
5395 if (FAILED(rc)) return rc;
5396
5397 /* the settings file path may never be null */
5398 ComAssertRet(!mData->m_strConfigFileFull.isEmpty(), E_FAIL);
5399
5400 /* save all VM data excluding snapshots */
5401 bool fNeedsGlobalSaveSettings = false;
5402 rc = saveSettings(&fNeedsGlobalSaveSettings);
5403 mlock.release();
5404
5405 if (SUCCEEDED(rc) && fNeedsGlobalSaveSettings)
5406 {
5407 // save the global settings; for that we should hold only the VirtualBox lock
5408 AutoWriteLock vlock(mParent COMMA_LOCKVAL_SRC_POS);
5409 rc = mParent->i_saveSettings();
5410 }
5411
5412 return rc;
5413}
5414
5415STDMETHODIMP Machine::DiscardSettings()
5416{
5417 AutoCaller autoCaller(this);
5418 if (FAILED(autoCaller.rc())) return autoCaller.rc();
5419
5420 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
5421
5422 HRESULT rc = checkStateDependency(MutableStateDep);
5423 if (FAILED(rc)) return rc;
5424
5425 /*
5426 * during this rollback, the session will be notified if data has
5427 * been actually changed
5428 */
5429 rollback(true /* aNotify */);
5430
5431 return S_OK;
5432}
5433
5434/** @note Locks objects! */
5435STDMETHODIMP Machine::Unregister(CleanupMode_T cleanupMode,
5436 ComSafeArrayOut(IMedium*, aMedia))
5437{
5438 // use AutoLimitedCaller because this call is valid on inaccessible machines as well
5439 AutoLimitedCaller autoCaller(this);
5440 AssertComRCReturnRC(autoCaller.rc());
5441
5442 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
5443
5444 Guid id(getId());
5445
5446 if (mData->mSession.mState != SessionState_Unlocked)
5447 return setError(VBOX_E_INVALID_OBJECT_STATE,
5448 tr("Cannot unregister the machine '%s' while it is locked"),
5449 mUserData->s.strName.c_str());
5450
5451 // wait for state dependents to drop to zero
5452 ensureNoStateDependencies();
5453
5454 if (!mData->mAccessible)
5455 {
5456 // inaccessible maschines can only be unregistered; uninitialize ourselves
5457 // here because currently there may be no unregistered that are inaccessible
5458 // (this state combination is not supported). Note releasing the caller and
5459 // leaving the lock before calling uninit()
5460 alock.release();
5461 autoCaller.release();
5462
5463 uninit();
5464
5465 mParent->i_unregisterMachine(this, id);
5466 // calls VirtualBox::saveSettings()
5467
5468 return S_OK;
5469 }
5470
5471 HRESULT rc = S_OK;
5472
5473 // discard saved state
5474 if (mData->mMachineState == MachineState_Saved)
5475 {
5476 // add the saved state file to the list of files the caller should delete
5477 Assert(!mSSData->strStateFilePath.isEmpty());
5478 mData->llFilesToDelete.push_back(mSSData->strStateFilePath);
5479
5480 mSSData->strStateFilePath.setNull();
5481
5482 // unconditionally set the machine state to powered off, we now
5483 // know no session has locked the machine
5484 mData->mMachineState = MachineState_PoweredOff;
5485 }
5486
5487 size_t cSnapshots = 0;
5488 if (mData->mFirstSnapshot)
5489 cSnapshots = mData->mFirstSnapshot->i_getAllChildrenCount() + 1;
5490 if (cSnapshots && cleanupMode == CleanupMode_UnregisterOnly)
5491 // fail now before we start detaching media
5492 return setError(VBOX_E_INVALID_OBJECT_STATE,
5493 tr("Cannot unregister the machine '%s' because it has %d snapshots"),
5494 mUserData->s.strName.c_str(), cSnapshots);
5495
5496 // This list collects the medium objects from all medium attachments
5497 // which we will detach from the machine and its snapshots, in a specific
5498 // order which allows for closing all media without getting "media in use"
5499 // errors, simply by going through the list from the front to the back:
5500 // 1) first media from machine attachments (these have the "leaf" attachments with snapshots
5501 // and must be closed before the parent media from the snapshots, or closing the parents
5502 // will fail because they still have children);
5503 // 2) media from the youngest snapshots followed by those from the parent snapshots until
5504 // the root ("first") snapshot of the machine.
5505 MediaList llMedia;
5506
5507 if ( !mMediaData.isNull() // can be NULL if machine is inaccessible
5508 && mMediaData->mAttachments.size()
5509 )
5510 {
5511 // we have media attachments: detach them all and add the Medium objects to our list
5512 if (cleanupMode != CleanupMode_UnregisterOnly)
5513 detachAllMedia(alock, NULL /* pSnapshot */, cleanupMode, llMedia);
5514 else
5515 return setError(VBOX_E_INVALID_OBJECT_STATE,
5516 tr("Cannot unregister the machine '%s' because it has %d media attachments"),
5517 mUserData->s.strName.c_str(), mMediaData->mAttachments.size());
5518 }
5519
5520 if (cSnapshots)
5521 {
5522 // autoCleanup must be true here, or we would have failed above
5523
5524 // add the media from the medium attachments of the snapshots to llMedia
5525 // as well, after the "main" machine media; Snapshot::uninitRecursively()
5526 // calls Machine::detachAllMedia() for the snapshot machine, recursing
5527 // into the children first
5528
5529 // Snapshot::beginDeletingSnapshot() asserts if the machine state is not this
5530 MachineState_T oldState = mData->mMachineState;
5531 mData->mMachineState = MachineState_DeletingSnapshot;
5532
5533 // make a copy of the first snapshot so the refcount does not drop to 0
5534 // in beginDeletingSnapshot, which sets pFirstSnapshot to 0 (that hangs
5535 // because of the AutoCaller voodoo)
5536 ComObjPtr<Snapshot> pFirstSnapshot = mData->mFirstSnapshot;
5537
5538 // GO!
5539 pFirstSnapshot->i_uninitRecursively(alock, cleanupMode, llMedia, mData->llFilesToDelete);
5540
5541 mData->mMachineState = oldState;
5542 }
5543
5544 if (FAILED(rc))
5545 {
5546 rollbackMedia();
5547 return rc;
5548 }
5549
5550 // commit all the media changes made above
5551 commitMedia();
5552
5553 mData->mRegistered = false;
5554
5555 // machine lock no longer needed
5556 alock.release();
5557
5558 // return media to caller
5559 SafeIfaceArray<IMedium> sfaMedia(llMedia);
5560 sfaMedia.detachTo(ComSafeArrayOutArg(aMedia));
5561
5562 mParent->i_unregisterMachine(this, id);
5563 // calls VirtualBox::saveSettings() and VirtualBox::i_saveModifiedRegistries()
5564
5565 return S_OK;
5566}
5567
5568struct Machine::DeleteTask
5569{
5570 ComObjPtr<Machine> pMachine;
5571 RTCList<ComPtr<IMedium> > llMediums;
5572 StringsList llFilesToDelete;
5573 ComObjPtr<Progress> pProgress;
5574};
5575
5576STDMETHODIMP Machine::DeleteConfig(ComSafeArrayIn(IMedium*, aMedia), IProgress **aProgress)
5577{
5578 LogFlowFuncEnter();
5579
5580 AutoCaller autoCaller(this);
5581 if (FAILED(autoCaller.rc())) return autoCaller.rc();
5582
5583 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
5584
5585 HRESULT rc = checkStateDependency(MutableStateDep);
5586 if (FAILED(rc)) return rc;
5587
5588 if (mData->mRegistered)
5589 return setError(VBOX_E_INVALID_VM_STATE,
5590 tr("Cannot delete settings of a registered machine"));
5591
5592 DeleteTask *pTask = new DeleteTask;
5593 pTask->pMachine = this;
5594 com::SafeIfaceArray<IMedium> sfaMedia(ComSafeArrayInArg(aMedia));
5595
5596 // collect files to delete
5597 pTask->llFilesToDelete = mData->llFilesToDelete; // saved states pushed here by Unregister()
5598
5599 for (size_t i = 0; i < sfaMedia.size(); ++i)
5600 {
5601 IMedium *pIMedium(sfaMedia[i]);
5602 ComObjPtr<Medium> pMedium = static_cast<Medium*>(pIMedium);
5603 if (pMedium.isNull())
5604 return setError(E_INVALIDARG, "The given medium pointer with index %d is invalid", i);
5605 SafeArray<BSTR> ids;
5606 rc = pMedium->COMGETTER(MachineIds)(ComSafeArrayAsOutParam(ids));
5607 if (FAILED(rc)) return rc;
5608 /* At this point the medium should not have any back references
5609 * anymore. If it has it is attached to another VM and *must* not
5610 * deleted. */
5611 if (ids.size() < 1)
5612 pTask->llMediums.append(pMedium);
5613 }
5614 if (mData->pMachineConfigFile->fileExists())
5615 pTask->llFilesToDelete.push_back(mData->m_strConfigFileFull);
5616
5617 pTask->pProgress.createObject();
5618 pTask->pProgress->init(getVirtualBox(),
5619 static_cast<IMachine*>(this) /* aInitiator */,
5620 Bstr(tr("Deleting files")).raw(),
5621 true /* fCancellable */,
5622 pTask->llFilesToDelete.size() + pTask->llMediums.size() + 1, // cOperations
5623 BstrFmt(tr("Deleting '%s'"), pTask->llFilesToDelete.front().c_str()).raw());
5624
5625 int vrc = RTThreadCreate(NULL,
5626 Machine::deleteThread,
5627 (void*)pTask,
5628 0,
5629 RTTHREADTYPE_MAIN_WORKER,
5630 0,
5631 "MachineDelete");
5632
5633 pTask->pProgress.queryInterfaceTo(aProgress);
5634
5635 if (RT_FAILURE(vrc))
5636 {
5637 delete pTask;
5638 return setError(E_FAIL, "Could not create MachineDelete thread (%Rrc)", vrc);
5639 }
5640
5641 LogFlowFuncLeave();
5642
5643 return S_OK;
5644}
5645
5646/**
5647 * Static task wrapper passed to RTThreadCreate() in Machine::Delete() which then
5648 * calls Machine::deleteTaskWorker() on the actual machine object.
5649 * @param Thread
5650 * @param pvUser
5651 * @return
5652 */
5653/*static*/
5654DECLCALLBACK(int) Machine::deleteThread(RTTHREAD Thread, void *pvUser)
5655{
5656 LogFlowFuncEnter();
5657
5658 DeleteTask *pTask = (DeleteTask*)pvUser;
5659 Assert(pTask);
5660 Assert(pTask->pMachine);
5661 Assert(pTask->pProgress);
5662
5663 HRESULT rc = pTask->pMachine->deleteTaskWorker(*pTask);
5664 pTask->pProgress->notifyComplete(rc);
5665
5666 delete pTask;
5667
5668 LogFlowFuncLeave();
5669
5670 NOREF(Thread);
5671
5672 return VINF_SUCCESS;
5673}
5674
5675/**
5676 * Task thread implementation for Machine::Delete(), called from Machine::deleteThread().
5677 * @param task
5678 * @return
5679 */
5680HRESULT Machine::deleteTaskWorker(DeleteTask &task)
5681{
5682 AutoCaller autoCaller(this);
5683 if (FAILED(autoCaller.rc())) return autoCaller.rc();
5684
5685 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
5686
5687 HRESULT rc = S_OK;
5688
5689 try
5690 {
5691 ULONG uLogHistoryCount = 3;
5692 ComPtr<ISystemProperties> systemProperties;
5693 rc = mParent->COMGETTER(SystemProperties)(systemProperties.asOutParam());
5694 if (FAILED(rc)) throw rc;
5695
5696 if (!systemProperties.isNull())
5697 {
5698 rc = systemProperties->COMGETTER(LogHistoryCount)(&uLogHistoryCount);
5699 if (FAILED(rc)) throw rc;
5700 }
5701
5702 MachineState_T oldState = mData->mMachineState;
5703 setMachineState(MachineState_SettingUp);
5704 alock.release();
5705 for (size_t i = 0; i < task.llMediums.size(); ++i)
5706 {
5707 ComObjPtr<Medium> pMedium = (Medium*)(IMedium*)task.llMediums.at(i);
5708 {
5709 AutoCaller mac(pMedium);
5710 if (FAILED(mac.rc())) throw mac.rc();
5711 Utf8Str strLocation = pMedium->i_getLocationFull();
5712 rc = task.pProgress->SetNextOperation(BstrFmt(tr("Deleting '%s'"), strLocation.c_str()).raw(), 1);
5713 if (FAILED(rc)) throw rc;
5714 LogFunc(("Deleting file %s\n", strLocation.c_str()));
5715 }
5716 ComPtr<IProgress> pProgress2;
5717 rc = pMedium->DeleteStorage(pProgress2.asOutParam());
5718 if (FAILED(rc)) throw rc;
5719 rc = task.pProgress->WaitForAsyncProgressCompletion(pProgress2);
5720 if (FAILED(rc)) throw rc;
5721 /* Check the result of the asynchronous process. */
5722 LONG iRc;
5723 rc = pProgress2->COMGETTER(ResultCode)(&iRc);
5724 if (FAILED(rc)) throw rc;
5725 /* If the thread of the progress object has an error, then
5726 * retrieve the error info from there, or it'll be lost. */
5727 if (FAILED(iRc))
5728 throw setError(ProgressErrorInfo(pProgress2));
5729
5730 /* Close the medium, deliberately without checking the return
5731 * code, and without leaving any trace in the error info, as
5732 * a failure here is a very minor issue, which shouldn't happen
5733 * as above we even managed to delete the medium. */
5734 {
5735 ErrorInfoKeeper eik;
5736 pMedium->Close();
5737 }
5738 }
5739 setMachineState(oldState);
5740 alock.acquire();
5741
5742 // delete the files pushed on the task list by Machine::Delete()
5743 // (this includes saved states of the machine and snapshots and
5744 // medium storage files from the IMedium list passed in, and the
5745 // machine XML file)
5746 StringsList::const_iterator it = task.llFilesToDelete.begin();
5747 while (it != task.llFilesToDelete.end())
5748 {
5749 const Utf8Str &strFile = *it;
5750 LogFunc(("Deleting file %s\n", strFile.c_str()));
5751 int vrc = RTFileDelete(strFile.c_str());
5752 if (RT_FAILURE(vrc))
5753 throw setError(VBOX_E_IPRT_ERROR,
5754 tr("Could not delete file '%s' (%Rrc)"), strFile.c_str(), vrc);
5755
5756 ++it;
5757 if (it == task.llFilesToDelete.end())
5758 {
5759 rc = task.pProgress->SetNextOperation(Bstr(tr("Cleaning up machine directory")).raw(), 1);
5760 if (FAILED(rc)) throw rc;
5761 break;
5762 }
5763
5764 rc = task.pProgress->SetNextOperation(BstrFmt(tr("Deleting '%s'"), it->c_str()).raw(), 1);
5765 if (FAILED(rc)) throw rc;
5766 }
5767
5768 /* delete the settings only when the file actually exists */
5769 if (mData->pMachineConfigFile->fileExists())
5770 {
5771 /* Delete any backup or uncommitted XML files. Ignore failures.
5772 See the fSafe parameter of xml::XmlFileWriter::write for details. */
5773 /** @todo Find a way to avoid referring directly to iprt/xml.h here. */
5774 Utf8Str otherXml = Utf8StrFmt("%s%s", mData->m_strConfigFileFull.c_str(), xml::XmlFileWriter::s_pszTmpSuff);
5775 RTFileDelete(otherXml.c_str());
5776 otherXml = Utf8StrFmt("%s%s", mData->m_strConfigFileFull.c_str(), xml::XmlFileWriter::s_pszPrevSuff);
5777 RTFileDelete(otherXml.c_str());
5778
5779 /* delete the Logs folder, nothing important should be left
5780 * there (we don't check for errors because the user might have
5781 * some private files there that we don't want to delete) */
5782 Utf8Str logFolder;
5783 getLogFolder(logFolder);
5784 Assert(logFolder.length());
5785 if (RTDirExists(logFolder.c_str()))
5786 {
5787 /* Delete all VBox.log[.N] files from the Logs folder
5788 * (this must be in sync with the rotation logic in
5789 * Console::powerUpThread()). Also, delete the VBox.png[.N]
5790 * files that may have been created by the GUI. */
5791 Utf8Str log = Utf8StrFmt("%s%cVBox.log",
5792 logFolder.c_str(), RTPATH_DELIMITER);
5793 RTFileDelete(log.c_str());
5794 log = Utf8StrFmt("%s%cVBox.png",
5795 logFolder.c_str(), RTPATH_DELIMITER);
5796 RTFileDelete(log.c_str());
5797 for (int i = uLogHistoryCount; i > 0; i--)
5798 {
5799 log = Utf8StrFmt("%s%cVBox.log.%d",
5800 logFolder.c_str(), RTPATH_DELIMITER, i);
5801 RTFileDelete(log.c_str());
5802 log = Utf8StrFmt("%s%cVBox.png.%d",
5803 logFolder.c_str(), RTPATH_DELIMITER, i);
5804 RTFileDelete(log.c_str());
5805 }
5806
5807 RTDirRemove(logFolder.c_str());
5808 }
5809
5810 /* delete the Snapshots folder, nothing important should be left
5811 * there (we don't check for errors because the user might have
5812 * some private files there that we don't want to delete) */
5813 Utf8Str strFullSnapshotFolder;
5814 calculateFullPath(mUserData->s.strSnapshotFolder, strFullSnapshotFolder);
5815 Assert(!strFullSnapshotFolder.isEmpty());
5816 if (RTDirExists(strFullSnapshotFolder.c_str()))
5817 RTDirRemove(strFullSnapshotFolder.c_str());
5818
5819 // delete the directory that contains the settings file, but only
5820 // if it matches the VM name
5821 Utf8Str settingsDir;
5822 if (isInOwnDir(&settingsDir))
5823 RTDirRemove(settingsDir.c_str());
5824 }
5825
5826 alock.release();
5827
5828 mParent->i_saveModifiedRegistries();
5829 }
5830 catch (HRESULT aRC) { rc = aRC; }
5831
5832 return rc;
5833}
5834
5835STDMETHODIMP Machine::FindSnapshot(IN_BSTR aNameOrId, ISnapshot **aSnapshot)
5836{
5837 CheckComArgOutPointerValid(aSnapshot);
5838
5839 AutoCaller autoCaller(this);
5840 if (FAILED(autoCaller.rc())) return autoCaller.rc();
5841
5842 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
5843
5844 ComObjPtr<Snapshot> pSnapshot;
5845 HRESULT rc;
5846
5847 if (!aNameOrId || !*aNameOrId)
5848 // null case (caller wants root snapshot): findSnapshotById() handles this
5849 rc = findSnapshotById(Guid(), pSnapshot, true /* aSetError */);
5850 else
5851 {
5852 Guid uuid(aNameOrId);
5853 if (uuid.isValid())
5854 rc = findSnapshotById(uuid, pSnapshot, true /* aSetError */);
5855 else
5856 rc = findSnapshotByName(Utf8Str(aNameOrId), pSnapshot, true /* aSetError */);
5857 }
5858 pSnapshot.queryInterfaceTo(aSnapshot);
5859
5860 return rc;
5861}
5862
5863STDMETHODIMP Machine::CreateSharedFolder(IN_BSTR aName, IN_BSTR aHostPath, BOOL aWritable, BOOL aAutoMount)
5864{
5865 CheckComArgStrNotEmptyOrNull(aName);
5866 CheckComArgStrNotEmptyOrNull(aHostPath);
5867
5868 AutoCaller autoCaller(this);
5869 if (FAILED(autoCaller.rc())) return autoCaller.rc();
5870
5871 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
5872
5873 HRESULT rc = checkStateDependency(MutableStateDep);
5874 if (FAILED(rc)) return rc;
5875
5876 Utf8Str strName(aName);
5877
5878 ComObjPtr<SharedFolder> sharedFolder;
5879 rc = findSharedFolder(strName, sharedFolder, false /* aSetError */);
5880 if (SUCCEEDED(rc))
5881 return setError(VBOX_E_OBJECT_IN_USE,
5882 tr("Shared folder named '%s' already exists"),
5883 strName.c_str());
5884
5885 sharedFolder.createObject();
5886 rc = sharedFolder->init(getMachine(),
5887 strName,
5888 aHostPath,
5889 !!aWritable,
5890 !!aAutoMount,
5891 true /* fFailOnError */);
5892 if (FAILED(rc)) return rc;
5893
5894 setModified(IsModified_SharedFolders);
5895 mHWData.backup();
5896 mHWData->mSharedFolders.push_back(sharedFolder);
5897
5898 /* inform the direct session if any */
5899 alock.release();
5900 onSharedFolderChange();
5901
5902 return S_OK;
5903}
5904
5905STDMETHODIMP Machine::RemoveSharedFolder(IN_BSTR aName)
5906{
5907 CheckComArgStrNotEmptyOrNull(aName);
5908
5909 AutoCaller autoCaller(this);
5910 if (FAILED(autoCaller.rc())) return autoCaller.rc();
5911
5912 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
5913
5914 HRESULT rc = checkStateDependency(MutableStateDep);
5915 if (FAILED(rc)) return rc;
5916
5917 ComObjPtr<SharedFolder> sharedFolder;
5918 rc = findSharedFolder(aName, sharedFolder, true /* aSetError */);
5919 if (FAILED(rc)) return rc;
5920
5921 setModified(IsModified_SharedFolders);
5922 mHWData.backup();
5923 mHWData->mSharedFolders.remove(sharedFolder);
5924
5925 /* inform the direct session if any */
5926 alock.release();
5927 onSharedFolderChange();
5928
5929 return S_OK;
5930}
5931
5932STDMETHODIMP Machine::CanShowConsoleWindow(BOOL *aCanShow)
5933{
5934 CheckComArgOutPointerValid(aCanShow);
5935
5936 /* start with No */
5937 *aCanShow = FALSE;
5938
5939 AutoCaller autoCaller(this);
5940 AssertComRCReturnRC(autoCaller.rc());
5941
5942 ComPtr<IInternalSessionControl> directControl;
5943 {
5944 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
5945
5946 if (mData->mSession.mState != SessionState_Locked)
5947 return setError(VBOX_E_INVALID_VM_STATE,
5948 tr("Machine is not locked for session (session state: %s)"),
5949 Global::stringifySessionState(mData->mSession.mState));
5950
5951 directControl = mData->mSession.mDirectControl;
5952 }
5953
5954 /* ignore calls made after #OnSessionEnd() is called */
5955 if (!directControl)
5956 return S_OK;
5957
5958 LONG64 dummy;
5959 return directControl->OnShowWindow(TRUE /* aCheck */, aCanShow, &dummy);
5960}
5961
5962STDMETHODIMP Machine::ShowConsoleWindow(LONG64 *aWinId)
5963{
5964 CheckComArgOutPointerValid(aWinId);
5965
5966 AutoCaller autoCaller(this);
5967 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
5968
5969 ComPtr<IInternalSessionControl> directControl;
5970 {
5971 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
5972
5973 if (mData->mSession.mState != SessionState_Locked)
5974 return setError(E_FAIL,
5975 tr("Machine is not locked for session (session state: %s)"),
5976 Global::stringifySessionState(mData->mSession.mState));
5977
5978 directControl = mData->mSession.mDirectControl;
5979 }
5980
5981 /* ignore calls made after #OnSessionEnd() is called */
5982 if (!directControl)
5983 return S_OK;
5984
5985 BOOL dummy;
5986 return directControl->OnShowWindow(FALSE /* aCheck */, &dummy, aWinId);
5987}
5988
5989#ifdef VBOX_WITH_GUEST_PROPS
5990/**
5991 * Look up a guest property in VBoxSVC's internal structures.
5992 */
5993HRESULT Machine::getGuestPropertyFromService(IN_BSTR aName,
5994 BSTR *aValue,
5995 LONG64 *aTimestamp,
5996 BSTR *aFlags) const
5997{
5998 using namespace guestProp;
5999
6000 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
6001 Utf8Str strName(aName);
6002 HWData::GuestPropertyMap::const_iterator it = mHWData->mGuestProperties.find(strName);
6003
6004 if (it != mHWData->mGuestProperties.end())
6005 {
6006 char szFlags[MAX_FLAGS_LEN + 1];
6007 it->second.strValue.cloneTo(aValue);
6008 *aTimestamp = it->second.mTimestamp;
6009 writeFlags(it->second.mFlags, szFlags);
6010 Bstr(szFlags).cloneTo(aFlags);
6011 }
6012
6013 return S_OK;
6014}
6015
6016/**
6017 * Query the VM that a guest property belongs to for the property.
6018 * @returns E_ACCESSDENIED if the VM process is not available or not
6019 * currently handling queries and the lookup should then be done in
6020 * VBoxSVC.
6021 */
6022HRESULT Machine::getGuestPropertyFromVM(IN_BSTR aName,
6023 BSTR *aValue,
6024 LONG64 *aTimestamp,
6025 BSTR *aFlags) const
6026{
6027 HRESULT rc;
6028 ComPtr<IInternalSessionControl> directControl;
6029 directControl = mData->mSession.mDirectControl;
6030
6031 /* fail if we were called after #OnSessionEnd() is called. This is a
6032 * silly race condition. */
6033
6034 /** @todo This code is bothering API clients (like python script clients) with
6035 * the AccessGuestProperty call, creating unncessary IPC. Need to
6036 * have a way of figuring out which kind of direct session it is... */
6037 if (!directControl)
6038 rc = E_ACCESSDENIED;
6039 else
6040 rc = directControl->AccessGuestProperty(aName, NULL, NULL,
6041 false /* isSetter */,
6042 aValue, aTimestamp, aFlags);
6043 return rc;
6044}
6045#endif // VBOX_WITH_GUEST_PROPS
6046
6047STDMETHODIMP Machine::GetGuestProperty(IN_BSTR aName,
6048 BSTR *aValue,
6049 LONG64 *aTimestamp,
6050 BSTR *aFlags)
6051{
6052#ifndef VBOX_WITH_GUEST_PROPS
6053 ReturnComNotImplemented();
6054#else // VBOX_WITH_GUEST_PROPS
6055 CheckComArgStrNotEmptyOrNull(aName);
6056 CheckComArgOutPointerValid(aValue);
6057 CheckComArgOutPointerValid(aTimestamp);
6058 CheckComArgOutPointerValid(aFlags);
6059
6060 AutoCaller autoCaller(this);
6061 if (FAILED(autoCaller.rc())) return autoCaller.rc();
6062
6063 HRESULT rc = getGuestPropertyFromVM(aName, aValue, aTimestamp, aFlags);
6064 if (rc == E_ACCESSDENIED)
6065 /* The VM is not running or the service is not (yet) accessible */
6066 rc = getGuestPropertyFromService(aName, aValue, aTimestamp, aFlags);
6067 return rc;
6068#endif // VBOX_WITH_GUEST_PROPS
6069}
6070
6071STDMETHODIMP Machine::GetGuestPropertyValue(IN_BSTR aName, BSTR *aValue)
6072{
6073 LONG64 dummyTimestamp;
6074 Bstr dummyFlags;
6075 return GetGuestProperty(aName, aValue, &dummyTimestamp, dummyFlags.asOutParam());
6076}
6077
6078STDMETHODIMP Machine::GetGuestPropertyTimestamp(IN_BSTR aName, LONG64 *aTimestamp)
6079{
6080 Bstr dummyValue;
6081 Bstr dummyFlags;
6082 return GetGuestProperty(aName, dummyValue.asOutParam(), aTimestamp, dummyFlags.asOutParam());
6083}
6084
6085#ifdef VBOX_WITH_GUEST_PROPS
6086/**
6087 * Set a guest property in VBoxSVC's internal structures.
6088 */
6089HRESULT Machine::setGuestPropertyToService(IN_BSTR aName, IN_BSTR aValue,
6090 IN_BSTR aFlags)
6091{
6092 using namespace guestProp;
6093
6094 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
6095 HRESULT rc = S_OK;
6096
6097 rc = checkStateDependency(MutableStateDep);
6098 if (FAILED(rc)) return rc;
6099
6100 try
6101 {
6102 Utf8Str utf8Name(aName);
6103 Utf8Str utf8Flags(aFlags);
6104 uint32_t fFlags = NILFLAG;
6105 if ( aFlags != NULL
6106 && RT_FAILURE(validateFlags(utf8Flags.c_str(), &fFlags)))
6107 return setError(E_INVALIDARG,
6108 tr("Invalid guest property flag values: '%ls'"),
6109 aFlags);
6110
6111 bool fDelete = !RT_VALID_PTR(aValue) || *(aValue) == '\0';
6112 HWData::GuestPropertyMap::iterator it = mHWData->mGuestProperties.find(utf8Name);
6113 if (it == mHWData->mGuestProperties.end())
6114 {
6115 if (!fDelete)
6116 {
6117 setModified(IsModified_MachineData);
6118 mHWData.backupEx();
6119
6120 RTTIMESPEC time;
6121 HWData::GuestProperty prop;
6122 prop.strValue = aValue;
6123 prop.mTimestamp = RTTimeSpecGetNano(RTTimeNow(&time));
6124 prop.mFlags = fFlags;
6125 mHWData->mGuestProperties[Utf8Str(aName)] = prop;
6126 }
6127 }
6128 else
6129 {
6130 if (it->second.mFlags & (RDONLYHOST))
6131 {
6132 rc = setError(E_ACCESSDENIED,
6133 tr("The property '%ls' cannot be changed by the host"),
6134 aName);
6135 }
6136 else
6137 {
6138 setModified(IsModified_MachineData);
6139 mHWData.backupEx();
6140
6141 /* The backupEx() operation invalidates our iterator,
6142 * so get a new one. */
6143 it = mHWData->mGuestProperties.find(utf8Name);
6144 Assert(it != mHWData->mGuestProperties.end());
6145
6146 if (!fDelete)
6147 {
6148 RTTIMESPEC time;
6149 it->second.strValue = aValue;
6150 it->second.mTimestamp = RTTimeSpecGetNano(RTTimeNow(&time));
6151 it->second.mFlags = fFlags;
6152 }
6153 else
6154 mHWData->mGuestProperties.erase(it);
6155 }
6156 }
6157
6158 if ( SUCCEEDED(rc)
6159 && ( mHWData->mGuestPropertyNotificationPatterns.isEmpty()
6160 || RTStrSimplePatternMultiMatch(mHWData->mGuestPropertyNotificationPatterns.c_str(),
6161 RTSTR_MAX,
6162 utf8Name.c_str(),
6163 RTSTR_MAX,
6164 NULL)
6165 )
6166 )
6167 {
6168 alock.release();
6169
6170 mParent->i_onGuestPropertyChange(mData->mUuid, aName,
6171 aValue ? aValue : Bstr("").raw(),
6172 aFlags ? aFlags : Bstr("").raw());
6173 }
6174 }
6175 catch (std::bad_alloc &)
6176 {
6177 rc = E_OUTOFMEMORY;
6178 }
6179
6180 return rc;
6181}
6182
6183/**
6184 * Set a property on the VM that that property belongs to.
6185 * @returns E_ACCESSDENIED if the VM process is not available or not
6186 * currently handling queries and the setting should then be done in
6187 * VBoxSVC.
6188 */
6189HRESULT Machine::setGuestPropertyToVM(IN_BSTR aName, IN_BSTR aValue,
6190 IN_BSTR aFlags)
6191{
6192 HRESULT rc;
6193
6194 try
6195 {
6196 ComPtr<IInternalSessionControl> directControl = mData->mSession.mDirectControl;
6197
6198 BSTR dummy = NULL; /* will not be changed (setter) */
6199 LONG64 dummy64;
6200 if (!directControl)
6201 rc = E_ACCESSDENIED;
6202 else
6203 /** @todo Fix when adding DeleteGuestProperty(), see defect. */
6204 rc = directControl->AccessGuestProperty(aName, aValue, aFlags,
6205 true /* isSetter */,
6206 &dummy, &dummy64, &dummy);
6207 }
6208 catch (std::bad_alloc &)
6209 {
6210 rc = E_OUTOFMEMORY;
6211 }
6212
6213 return rc;
6214}
6215#endif // VBOX_WITH_GUEST_PROPS
6216
6217STDMETHODIMP Machine::SetGuestProperty(IN_BSTR aName, IN_BSTR aValue,
6218 IN_BSTR aFlags)
6219{
6220#ifndef VBOX_WITH_GUEST_PROPS
6221 ReturnComNotImplemented();
6222#else // VBOX_WITH_GUEST_PROPS
6223 CheckComArgStrNotEmptyOrNull(aName);
6224 CheckComArgMaybeNull(aFlags);
6225 CheckComArgMaybeNull(aValue);
6226
6227 AutoCaller autoCaller(this);
6228 if (FAILED(autoCaller.rc()))
6229 return autoCaller.rc();
6230
6231 HRESULT rc = setGuestPropertyToVM(aName, aValue, aFlags);
6232 if (rc == E_ACCESSDENIED)
6233 /* The VM is not running or the service is not (yet) accessible */
6234 rc = setGuestPropertyToService(aName, aValue, aFlags);
6235 return rc;
6236#endif // VBOX_WITH_GUEST_PROPS
6237}
6238
6239STDMETHODIMP Machine::SetGuestPropertyValue(IN_BSTR aName, IN_BSTR aValue)
6240{
6241 return SetGuestProperty(aName, aValue, NULL);
6242}
6243
6244STDMETHODIMP Machine::DeleteGuestProperty(IN_BSTR aName)
6245{
6246 return SetGuestProperty(aName, NULL, NULL);
6247}
6248
6249#ifdef VBOX_WITH_GUEST_PROPS
6250/**
6251 * Enumerate the guest properties in VBoxSVC's internal structures.
6252 */
6253HRESULT Machine::enumerateGuestPropertiesInService
6254 (IN_BSTR aPatterns, ComSafeArrayOut(BSTR, aNames),
6255 ComSafeArrayOut(BSTR, aValues),
6256 ComSafeArrayOut(LONG64, aTimestamps),
6257 ComSafeArrayOut(BSTR, aFlags))
6258{
6259 using namespace guestProp;
6260
6261 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
6262 Utf8Str strPatterns(aPatterns);
6263
6264 HWData::GuestPropertyMap propMap;
6265
6266 /*
6267 * Look for matching patterns and build up a list.
6268 */
6269 HWData::GuestPropertyMap::const_iterator it = mHWData->mGuestProperties.begin();
6270 while (it != mHWData->mGuestProperties.end())
6271 {
6272 if ( strPatterns.isEmpty()
6273 || RTStrSimplePatternMultiMatch(strPatterns.c_str(),
6274 RTSTR_MAX,
6275 it->first.c_str(),
6276 RTSTR_MAX,
6277 NULL)
6278 )
6279 {
6280 propMap.insert(*it);
6281 }
6282
6283 it++;
6284 }
6285
6286 alock.release();
6287
6288 /*
6289 * And build up the arrays for returning the property information.
6290 */
6291 size_t cEntries = propMap.size();
6292 SafeArray<BSTR> names(cEntries);
6293 SafeArray<BSTR> values(cEntries);
6294 SafeArray<LONG64> timestamps(cEntries);
6295 SafeArray<BSTR> flags(cEntries);
6296 size_t iProp = 0;
6297
6298 it = propMap.begin();
6299 while (it != propMap.end())
6300 {
6301 char szFlags[MAX_FLAGS_LEN + 1];
6302 it->first.cloneTo(&names[iProp]);
6303 it->second.strValue.cloneTo(&values[iProp]);
6304 timestamps[iProp] = it->second.mTimestamp;
6305 writeFlags(it->second.mFlags, szFlags);
6306 Bstr(szFlags).cloneTo(&flags[iProp++]);
6307 it++;
6308 }
6309 names.detachTo(ComSafeArrayOutArg(aNames));
6310 values.detachTo(ComSafeArrayOutArg(aValues));
6311 timestamps.detachTo(ComSafeArrayOutArg(aTimestamps));
6312 flags.detachTo(ComSafeArrayOutArg(aFlags));
6313 return S_OK;
6314}
6315
6316/**
6317 * Enumerate the properties managed by a VM.
6318 * @returns E_ACCESSDENIED if the VM process is not available or not
6319 * currently handling queries and the setting should then be done in
6320 * VBoxSVC.
6321 */
6322HRESULT Machine::enumerateGuestPropertiesOnVM
6323 (IN_BSTR aPatterns, ComSafeArrayOut(BSTR, aNames),
6324 ComSafeArrayOut(BSTR, aValues),
6325 ComSafeArrayOut(LONG64, aTimestamps),
6326 ComSafeArrayOut(BSTR, aFlags))
6327{
6328 HRESULT rc;
6329 ComPtr<IInternalSessionControl> directControl;
6330 directControl = mData->mSession.mDirectControl;
6331
6332 if (!directControl)
6333 rc = E_ACCESSDENIED;
6334 else
6335 rc = directControl->EnumerateGuestProperties
6336 (aPatterns, ComSafeArrayOutArg(aNames),
6337 ComSafeArrayOutArg(aValues),
6338 ComSafeArrayOutArg(aTimestamps),
6339 ComSafeArrayOutArg(aFlags));
6340 return rc;
6341}
6342#endif // VBOX_WITH_GUEST_PROPS
6343
6344STDMETHODIMP Machine::EnumerateGuestProperties(IN_BSTR aPatterns,
6345 ComSafeArrayOut(BSTR, aNames),
6346 ComSafeArrayOut(BSTR, aValues),
6347 ComSafeArrayOut(LONG64, aTimestamps),
6348 ComSafeArrayOut(BSTR, aFlags))
6349{
6350#ifndef VBOX_WITH_GUEST_PROPS
6351 ReturnComNotImplemented();
6352#else // VBOX_WITH_GUEST_PROPS
6353 CheckComArgMaybeNull(aPatterns);
6354 CheckComArgOutSafeArrayPointerValid(aNames);
6355 CheckComArgOutSafeArrayPointerValid(aValues);
6356 CheckComArgOutSafeArrayPointerValid(aTimestamps);
6357 CheckComArgOutSafeArrayPointerValid(aFlags);
6358
6359 AutoCaller autoCaller(this);
6360 if (FAILED(autoCaller.rc())) return autoCaller.rc();
6361
6362 HRESULT rc = enumerateGuestPropertiesOnVM
6363 (aPatterns, ComSafeArrayOutArg(aNames),
6364 ComSafeArrayOutArg(aValues),
6365 ComSafeArrayOutArg(aTimestamps),
6366 ComSafeArrayOutArg(aFlags));
6367 if (rc == E_ACCESSDENIED)
6368 /* The VM is not running or the service is not (yet) accessible */
6369 rc = enumerateGuestPropertiesInService
6370 (aPatterns, ComSafeArrayOutArg(aNames),
6371 ComSafeArrayOutArg(aValues),
6372 ComSafeArrayOutArg(aTimestamps),
6373 ComSafeArrayOutArg(aFlags));
6374 return rc;
6375#endif // VBOX_WITH_GUEST_PROPS
6376}
6377
6378STDMETHODIMP Machine::GetMediumAttachmentsOfController(IN_BSTR aName,
6379 ComSafeArrayOut(IMediumAttachment*, aAttachments))
6380{
6381 MediaData::AttachmentList atts;
6382
6383 HRESULT rc = getMediumAttachmentsOfController(aName, atts);
6384 if (FAILED(rc)) return rc;
6385
6386 SafeIfaceArray<IMediumAttachment> attachments(atts);
6387 attachments.detachTo(ComSafeArrayOutArg(aAttachments));
6388
6389 return S_OK;
6390}
6391
6392STDMETHODIMP Machine::GetMediumAttachment(IN_BSTR aControllerName,
6393 LONG aControllerPort,
6394 LONG aDevice,
6395 IMediumAttachment **aAttachment)
6396{
6397 LogFlowThisFunc(("aControllerName=\"%ls\" aControllerPort=%d aDevice=%d\n",
6398 aControllerName, aControllerPort, aDevice));
6399
6400 CheckComArgStrNotEmptyOrNull(aControllerName);
6401 CheckComArgOutPointerValid(aAttachment);
6402
6403 AutoCaller autoCaller(this);
6404 if (FAILED(autoCaller.rc())) return autoCaller.rc();
6405
6406 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
6407
6408 *aAttachment = NULL;
6409
6410 ComObjPtr<MediumAttachment> pAttach = findAttachment(mMediaData->mAttachments,
6411 aControllerName,
6412 aControllerPort,
6413 aDevice);
6414 if (pAttach.isNull())
6415 return setError(VBOX_E_OBJECT_NOT_FOUND,
6416 tr("No storage device attached to device slot %d on port %d of controller '%ls'"),
6417 aDevice, aControllerPort, aControllerName);
6418
6419 pAttach.queryInterfaceTo(aAttachment);
6420
6421 return S_OK;
6422}
6423
6424STDMETHODIMP Machine::AddStorageController(IN_BSTR aName,
6425 StorageBus_T aConnectionType,
6426 IStorageController **controller)
6427{
6428 CheckComArgStrNotEmptyOrNull(aName);
6429
6430 if ( (aConnectionType <= StorageBus_Null)
6431 || (aConnectionType > StorageBus_USB))
6432 return setError(E_INVALIDARG,
6433 tr("Invalid connection type: %d"),
6434 aConnectionType);
6435
6436 AutoCaller autoCaller(this);
6437 if (FAILED(autoCaller.rc())) return autoCaller.rc();
6438
6439 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
6440
6441 HRESULT rc = checkStateDependency(MutableStateDep);
6442 if (FAILED(rc)) return rc;
6443
6444 /* try to find one with the name first. */
6445 ComObjPtr<StorageController> ctrl;
6446
6447 rc = getStorageControllerByName(aName, ctrl, false /* aSetError */);
6448 if (SUCCEEDED(rc))
6449 return setError(VBOX_E_OBJECT_IN_USE,
6450 tr("Storage controller named '%ls' already exists"),
6451 aName);
6452
6453 ctrl.createObject();
6454
6455 /* get a new instance number for the storage controller */
6456 ULONG ulInstance = 0;
6457 bool fBootable = true;
6458 for (StorageControllerList::const_iterator it = mStorageControllers->begin();
6459 it != mStorageControllers->end();
6460 ++it)
6461 {
6462 if ((*it)->i_getStorageBus() == aConnectionType)
6463 {
6464 ULONG ulCurInst = (*it)->i_getInstance();
6465
6466 if (ulCurInst >= ulInstance)
6467 ulInstance = ulCurInst + 1;
6468
6469 /* Only one controller of each type can be marked as bootable. */
6470 if ((*it)->i_getBootable())
6471 fBootable = false;
6472 }
6473 }
6474
6475 rc = ctrl->init(this, aName, aConnectionType, ulInstance, fBootable);
6476 if (FAILED(rc)) return rc;
6477
6478 setModified(IsModified_Storage);
6479 mStorageControllers.backup();
6480 mStorageControllers->push_back(ctrl);
6481
6482 ctrl.queryInterfaceTo(controller);
6483
6484 /* inform the direct session if any */
6485 alock.release();
6486 onStorageControllerChange();
6487
6488 return S_OK;
6489}
6490
6491STDMETHODIMP Machine::GetStorageControllerByName(IN_BSTR aName,
6492 IStorageController **aStorageController)
6493{
6494 CheckComArgStrNotEmptyOrNull(aName);
6495
6496 AutoCaller autoCaller(this);
6497 if (FAILED(autoCaller.rc())) return autoCaller.rc();
6498
6499 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
6500
6501 ComObjPtr<StorageController> ctrl;
6502
6503 HRESULT rc = getStorageControllerByName(aName, ctrl, true /* aSetError */);
6504 if (SUCCEEDED(rc))
6505 ctrl.queryInterfaceTo(aStorageController);
6506
6507 return rc;
6508}
6509
6510STDMETHODIMP Machine::GetStorageControllerByInstance(ULONG aInstance,
6511 IStorageController **aStorageController)
6512{
6513 AutoCaller autoCaller(this);
6514 if (FAILED(autoCaller.rc())) return autoCaller.rc();
6515
6516 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
6517
6518 for (StorageControllerList::const_iterator it = mStorageControllers->begin();
6519 it != mStorageControllers->end();
6520 ++it)
6521 {
6522 if ((*it)->i_getInstance() == aInstance)
6523 {
6524 (*it).queryInterfaceTo(aStorageController);
6525 return S_OK;
6526 }
6527 }
6528
6529 return setError(VBOX_E_OBJECT_NOT_FOUND,
6530 tr("Could not find a storage controller with instance number '%lu'"),
6531 aInstance);
6532}
6533
6534STDMETHODIMP Machine::SetStorageControllerBootable(IN_BSTR aName, BOOL fBootable)
6535{
6536 AutoCaller autoCaller(this);
6537 if (FAILED(autoCaller.rc())) return autoCaller.rc();
6538
6539 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
6540
6541 HRESULT rc = checkStateDependency(MutableStateDep);
6542 if (FAILED(rc)) return rc;
6543
6544 ComObjPtr<StorageController> ctrl;
6545
6546 rc = getStorageControllerByName(aName, ctrl, true /* aSetError */);
6547 if (SUCCEEDED(rc))
6548 {
6549 /* Ensure that only one controller of each type is marked as bootable. */
6550 if (fBootable == TRUE)
6551 {
6552 for (StorageControllerList::const_iterator it = mStorageControllers->begin();
6553 it != mStorageControllers->end();
6554 ++it)
6555 {
6556 ComObjPtr<StorageController> aCtrl = (*it);
6557
6558 if ( (aCtrl->i_getName() != Utf8Str(aName))
6559 && aCtrl->i_getBootable() == TRUE
6560 && aCtrl->i_getStorageBus() == ctrl->i_getStorageBus()
6561 && aCtrl->i_getControllerType() == ctrl->i_getControllerType())
6562 {
6563 aCtrl->i_setBootable(FALSE);
6564 break;
6565 }
6566 }
6567 }
6568
6569 if (SUCCEEDED(rc))
6570 {
6571 ctrl->i_setBootable(fBootable);
6572 setModified(IsModified_Storage);
6573 }
6574 }
6575
6576 if (SUCCEEDED(rc))
6577 {
6578 /* inform the direct session if any */
6579 alock.release();
6580 onStorageControllerChange();
6581 }
6582
6583 return rc;
6584}
6585
6586STDMETHODIMP Machine::RemoveStorageController(IN_BSTR aName)
6587{
6588 CheckComArgStrNotEmptyOrNull(aName);
6589
6590 AutoCaller autoCaller(this);
6591 if (FAILED(autoCaller.rc())) return autoCaller.rc();
6592
6593 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
6594
6595 HRESULT rc = checkStateDependency(MutableStateDep);
6596 if (FAILED(rc)) return rc;
6597
6598 ComObjPtr<StorageController> ctrl;
6599 rc = getStorageControllerByName(aName, ctrl, true /* aSetError */);
6600 if (FAILED(rc)) return rc;
6601
6602 {
6603 /* find all attached devices to the appropriate storage controller and detach them all */
6604 // make a temporary list because detachDevice invalidates iterators into
6605 // mMediaData->mAttachments
6606 MediaData::AttachmentList llAttachments2 = mMediaData->mAttachments;
6607
6608 for (MediaData::AttachmentList::iterator it = llAttachments2.begin();
6609 it != llAttachments2.end();
6610 ++it)
6611 {
6612 MediumAttachment *pAttachTemp = *it;
6613
6614 AutoCaller localAutoCaller(pAttachTemp);
6615 if (FAILED(localAutoCaller.rc())) return localAutoCaller.rc();
6616
6617 AutoReadLock local_alock(pAttachTemp COMMA_LOCKVAL_SRC_POS);
6618
6619 if (pAttachTemp->i_getControllerName() == aName)
6620 {
6621 rc = detachDevice(pAttachTemp, alock, NULL);
6622 if (FAILED(rc)) return rc;
6623 }
6624 }
6625 }
6626
6627 /* We can remove it now. */
6628 setModified(IsModified_Storage);
6629 mStorageControllers.backup();
6630
6631 ctrl->i_unshare();
6632
6633 mStorageControllers->remove(ctrl);
6634
6635 /* inform the direct session if any */
6636 alock.release();
6637 onStorageControllerChange();
6638
6639 return S_OK;
6640}
6641
6642STDMETHODIMP Machine::AddUSBController(IN_BSTR aName, USBControllerType_T aType,
6643 IUSBController **controller)
6644{
6645 if ( (aType <= USBControllerType_Null)
6646 || (aType >= USBControllerType_Last))
6647 return setError(E_INVALIDARG,
6648 tr("Invalid USB controller type: %d"),
6649 aType);
6650
6651 AutoCaller autoCaller(this);
6652 if (FAILED(autoCaller.rc())) return autoCaller.rc();
6653
6654 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
6655
6656 HRESULT rc = checkStateDependency(MutableStateDep);
6657 if (FAILED(rc)) return rc;
6658
6659 /* try to find one with the same type first. */
6660 ComObjPtr<USBController> ctrl;
6661
6662 rc = getUSBControllerByName(aName, ctrl, false /* aSetError */);
6663 if (SUCCEEDED(rc))
6664 return setError(VBOX_E_OBJECT_IN_USE,
6665 tr("USB controller named '%ls' already exists"),
6666 aName);
6667
6668 /* Check that we don't exceed the maximum number of USB controllers for the given type. */
6669 ULONG maxInstances;
6670 rc = mParent->i_getSystemProperties()->GetMaxInstancesOfUSBControllerType(mHWData->mChipsetType, aType, &maxInstances);
6671 if (FAILED(rc))
6672 return rc;
6673
6674 ULONG cInstances = getUSBControllerCountByType(aType);
6675 if (cInstances >= maxInstances)
6676 return setError(E_INVALIDARG,
6677 tr("Too many USB controllers of this type"));
6678
6679 ctrl.createObject();
6680
6681 rc = ctrl->init(this, aName, aType);
6682 if (FAILED(rc)) return rc;
6683
6684 setModified(IsModified_USB);
6685 mUSBControllers.backup();
6686 mUSBControllers->push_back(ctrl);
6687
6688 ctrl.queryInterfaceTo(controller);
6689
6690 /* inform the direct session if any */
6691 alock.release();
6692 onUSBControllerChange();
6693
6694 return S_OK;
6695}
6696
6697STDMETHODIMP Machine::GetUSBControllerByName(IN_BSTR aName, IUSBController **aUSBController)
6698{
6699 CheckComArgStrNotEmptyOrNull(aName);
6700
6701 AutoCaller autoCaller(this);
6702 if (FAILED(autoCaller.rc())) return autoCaller.rc();
6703
6704 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
6705
6706 ComObjPtr<USBController> ctrl;
6707
6708 HRESULT rc = getUSBControllerByName(aName, ctrl, true /* aSetError */);
6709 if (SUCCEEDED(rc))
6710 ctrl.queryInterfaceTo(aUSBController);
6711
6712 return rc;
6713}
6714
6715STDMETHODIMP Machine::GetUSBControllerCountByType(USBControllerType_T aType,
6716 ULONG *aControllers)
6717{
6718 CheckComArgOutPointerValid(aControllers);
6719
6720 if ( (aType <= USBControllerType_Null)
6721 || (aType >= USBControllerType_Last))
6722 return setError(E_INVALIDARG,
6723 tr("Invalid USB controller type: %d"),
6724 aType);
6725
6726 AutoCaller autoCaller(this);
6727 if (FAILED(autoCaller.rc())) return autoCaller.rc();
6728
6729 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
6730
6731 ComObjPtr<USBController> ctrl;
6732
6733 *aControllers = getUSBControllerCountByType(aType);
6734
6735 return S_OK;
6736}
6737
6738STDMETHODIMP Machine::RemoveUSBController(IN_BSTR aName)
6739{
6740 CheckComArgStrNotEmptyOrNull(aName);
6741
6742 AutoCaller autoCaller(this);
6743 if (FAILED(autoCaller.rc())) return autoCaller.rc();
6744
6745 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
6746
6747 HRESULT rc = checkStateDependency(MutableStateDep);
6748 if (FAILED(rc)) return rc;
6749
6750 ComObjPtr<USBController> ctrl;
6751 rc = getUSBControllerByName(aName, ctrl, true /* aSetError */);
6752 if (FAILED(rc)) return rc;
6753
6754 setModified(IsModified_USB);
6755 mUSBControllers.backup();
6756
6757 ctrl->i_unshare();
6758
6759 mUSBControllers->remove(ctrl);
6760
6761 /* inform the direct session if any */
6762 alock.release();
6763 onUSBControllerChange();
6764
6765 return S_OK;
6766}
6767
6768STDMETHODIMP Machine::QuerySavedGuestScreenInfo(ULONG uScreenId,
6769 ULONG *puOriginX,
6770 ULONG *puOriginY,
6771 ULONG *puWidth,
6772 ULONG *puHeight,
6773 BOOL *pfEnabled)
6774{
6775 LogFlowThisFunc(("\n"));
6776
6777 CheckComArgNotNull(puOriginX);
6778 CheckComArgNotNull(puOriginY);
6779 CheckComArgNotNull(puWidth);
6780 CheckComArgNotNull(puHeight);
6781 CheckComArgNotNull(pfEnabled);
6782
6783 uint32_t u32OriginX= 0;
6784 uint32_t u32OriginY= 0;
6785 uint32_t u32Width = 0;
6786 uint32_t u32Height = 0;
6787 uint16_t u16Flags = 0;
6788
6789 int vrc = readSavedGuestScreenInfo(mSSData->strStateFilePath, uScreenId,
6790 &u32OriginX, &u32OriginY, &u32Width, &u32Height, &u16Flags);
6791 if (RT_FAILURE(vrc))
6792 {
6793#ifdef RT_OS_WINDOWS
6794 /* HACK: GUI sets *pfEnabled to 'true' and expects it to stay so if the API fails.
6795 * This works with XPCOM. But Windows COM sets all output parameters to zero.
6796 * So just assign fEnable to TRUE again.
6797 * The right fix would be to change GUI API wrappers to make sure that parameters
6798 * are changed only if API succeeds.
6799 */
6800 *pfEnabled = TRUE;
6801#endif
6802 return setError(VBOX_E_IPRT_ERROR,
6803 tr("Saved guest size is not available (%Rrc)"),
6804 vrc);
6805 }
6806
6807 *puOriginX = u32OriginX;
6808 *puOriginY = u32OriginY;
6809 *puWidth = u32Width;
6810 *puHeight = u32Height;
6811 *pfEnabled = (u16Flags & VBVA_SCREEN_F_DISABLED) == 0;
6812
6813 return S_OK;
6814}
6815
6816STDMETHODIMP Machine::QuerySavedThumbnailSize(ULONG aScreenId, ULONG *aSize, ULONG *aWidth, ULONG *aHeight)
6817{
6818 LogFlowThisFunc(("\n"));
6819
6820 CheckComArgNotNull(aSize);
6821 CheckComArgNotNull(aWidth);
6822 CheckComArgNotNull(aHeight);
6823
6824 if (aScreenId != 0)
6825 return E_NOTIMPL;
6826
6827 AutoCaller autoCaller(this);
6828 if (FAILED(autoCaller.rc())) return autoCaller.rc();
6829
6830 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
6831
6832 uint8_t *pu8Data = NULL;
6833 uint32_t cbData = 0;
6834 uint32_t u32Width = 0;
6835 uint32_t u32Height = 0;
6836
6837 int vrc = readSavedDisplayScreenshot(mSSData->strStateFilePath, 0 /* u32Type */, &pu8Data, &cbData, &u32Width, &u32Height);
6838
6839 if (RT_FAILURE(vrc))
6840 return setError(VBOX_E_IPRT_ERROR,
6841 tr("Saved screenshot data is not available (%Rrc)"),
6842 vrc);
6843
6844 *aSize = cbData;
6845 *aWidth = u32Width;
6846 *aHeight = u32Height;
6847
6848 freeSavedDisplayScreenshot(pu8Data);
6849
6850 return S_OK;
6851}
6852
6853STDMETHODIMP Machine::ReadSavedThumbnailToArray(ULONG aScreenId, BOOL aBGR, ULONG *aWidth, ULONG *aHeight, ComSafeArrayOut(BYTE, aData))
6854{
6855 LogFlowThisFunc(("\n"));
6856
6857 CheckComArgNotNull(aWidth);
6858 CheckComArgNotNull(aHeight);
6859 CheckComArgOutSafeArrayPointerValid(aData);
6860
6861 if (aScreenId != 0)
6862 return E_NOTIMPL;
6863
6864 AutoCaller autoCaller(this);
6865 if (FAILED(autoCaller.rc())) return autoCaller.rc();
6866
6867 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
6868
6869 uint8_t *pu8Data = NULL;
6870 uint32_t cbData = 0;
6871 uint32_t u32Width = 0;
6872 uint32_t u32Height = 0;
6873
6874 int vrc = readSavedDisplayScreenshot(mSSData->strStateFilePath, 0 /* u32Type */, &pu8Data, &cbData, &u32Width, &u32Height);
6875
6876 if (RT_FAILURE(vrc))
6877 return setError(VBOX_E_IPRT_ERROR,
6878 tr("Saved screenshot data is not available (%Rrc)"),
6879 vrc);
6880
6881 *aWidth = u32Width;
6882 *aHeight = u32Height;
6883
6884 com::SafeArray<BYTE> bitmap(cbData);
6885 /* Convert pixels to format expected by the API caller. */
6886 if (aBGR)
6887 {
6888 /* [0] B, [1] G, [2] R, [3] A. */
6889 for (unsigned i = 0; i < cbData; i += 4)
6890 {
6891 bitmap[i] = pu8Data[i];
6892 bitmap[i + 1] = pu8Data[i + 1];
6893 bitmap[i + 2] = pu8Data[i + 2];
6894 bitmap[i + 3] = 0xff;
6895 }
6896 }
6897 else
6898 {
6899 /* [0] R, [1] G, [2] B, [3] A. */
6900 for (unsigned i = 0; i < cbData; i += 4)
6901 {
6902 bitmap[i] = pu8Data[i + 2];
6903 bitmap[i + 1] = pu8Data[i + 1];
6904 bitmap[i + 2] = pu8Data[i];
6905 bitmap[i + 3] = 0xff;
6906 }
6907 }
6908 bitmap.detachTo(ComSafeArrayOutArg(aData));
6909
6910 freeSavedDisplayScreenshot(pu8Data);
6911
6912 return S_OK;
6913}
6914
6915
6916STDMETHODIMP Machine::ReadSavedThumbnailPNGToArray(ULONG aScreenId, ULONG *aWidth, ULONG *aHeight, ComSafeArrayOut(BYTE, aData))
6917{
6918 LogFlowThisFunc(("\n"));
6919
6920 CheckComArgNotNull(aWidth);
6921 CheckComArgNotNull(aHeight);
6922 CheckComArgOutSafeArrayPointerValid(aData);
6923
6924 if (aScreenId != 0)
6925 return E_NOTIMPL;
6926
6927 AutoCaller autoCaller(this);
6928 if (FAILED(autoCaller.rc())) return autoCaller.rc();
6929
6930 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
6931
6932 uint8_t *pu8Data = NULL;
6933 uint32_t cbData = 0;
6934 uint32_t u32Width = 0;
6935 uint32_t u32Height = 0;
6936
6937 int vrc = readSavedDisplayScreenshot(mSSData->strStateFilePath, 0 /* u32Type */, &pu8Data, &cbData, &u32Width, &u32Height);
6938
6939 if (RT_FAILURE(vrc))
6940 return setError(VBOX_E_IPRT_ERROR,
6941 tr("Saved screenshot data is not available (%Rrc)"),
6942 vrc);
6943
6944 *aWidth = u32Width;
6945 *aHeight = u32Height;
6946
6947 HRESULT rc = S_OK;
6948 uint8_t *pu8PNG = NULL;
6949 uint32_t cbPNG = 0;
6950 uint32_t cxPNG = 0;
6951 uint32_t cyPNG = 0;
6952
6953 vrc = DisplayMakePNG(pu8Data, u32Width, u32Height, &pu8PNG, &cbPNG, &cxPNG, &cyPNG, 0);
6954
6955 if (RT_SUCCESS(vrc))
6956 {
6957 com::SafeArray<BYTE> screenData(cbPNG);
6958 screenData.initFrom(pu8PNG, cbPNG);
6959 if (pu8PNG)
6960 RTMemFree(pu8PNG);
6961 screenData.detachTo(ComSafeArrayOutArg(aData));
6962 }
6963 else
6964 {
6965 if (pu8PNG)
6966 RTMemFree(pu8PNG);
6967 return setError(VBOX_E_IPRT_ERROR,
6968 tr("Could not convert screenshot to PNG (%Rrc)"),
6969 vrc);
6970 }
6971
6972 freeSavedDisplayScreenshot(pu8Data);
6973
6974 return rc;
6975}
6976
6977STDMETHODIMP Machine::QuerySavedScreenshotPNGSize(ULONG aScreenId, ULONG *aSize, ULONG *aWidth, ULONG *aHeight)
6978{
6979 LogFlowThisFunc(("\n"));
6980
6981 CheckComArgNotNull(aSize);
6982 CheckComArgNotNull(aWidth);
6983 CheckComArgNotNull(aHeight);
6984
6985 if (aScreenId != 0)
6986 return E_NOTIMPL;
6987
6988 AutoCaller autoCaller(this);
6989 if (FAILED(autoCaller.rc())) return autoCaller.rc();
6990
6991 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
6992
6993 uint8_t *pu8Data = NULL;
6994 uint32_t cbData = 0;
6995 uint32_t u32Width = 0;
6996 uint32_t u32Height = 0;
6997
6998 int vrc = readSavedDisplayScreenshot(mSSData->strStateFilePath, 1 /* u32Type */, &pu8Data, &cbData, &u32Width, &u32Height);
6999
7000 if (RT_FAILURE(vrc))
7001 return setError(VBOX_E_IPRT_ERROR,
7002 tr("Saved screenshot data is not available (%Rrc)"),
7003 vrc);
7004
7005 *aSize = cbData;
7006 *aWidth = u32Width;
7007 *aHeight = u32Height;
7008
7009 freeSavedDisplayScreenshot(pu8Data);
7010
7011 return S_OK;
7012}
7013
7014STDMETHODIMP Machine::ReadSavedScreenshotPNGToArray(ULONG aScreenId, ULONG *aWidth, ULONG *aHeight, ComSafeArrayOut(BYTE, aData))
7015{
7016 LogFlowThisFunc(("\n"));
7017
7018 CheckComArgNotNull(aWidth);
7019 CheckComArgNotNull(aHeight);
7020 CheckComArgOutSafeArrayPointerValid(aData);
7021
7022 if (aScreenId != 0)
7023 return E_NOTIMPL;
7024
7025 AutoCaller autoCaller(this);
7026 if (FAILED(autoCaller.rc())) return autoCaller.rc();
7027
7028 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
7029
7030 uint8_t *pu8Data = NULL;
7031 uint32_t cbData = 0;
7032 uint32_t u32Width = 0;
7033 uint32_t u32Height = 0;
7034
7035 int vrc = readSavedDisplayScreenshot(mSSData->strStateFilePath, 1 /* u32Type */, &pu8Data, &cbData, &u32Width, &u32Height);
7036
7037 if (RT_FAILURE(vrc))
7038 return setError(VBOX_E_IPRT_ERROR,
7039 tr("Saved screenshot thumbnail data is not available (%Rrc)"),
7040 vrc);
7041
7042 *aWidth = u32Width;
7043 *aHeight = u32Height;
7044
7045 com::SafeArray<BYTE> png(cbData);
7046 png.initFrom(pu8Data, cbData);
7047 png.detachTo(ComSafeArrayOutArg(aData));
7048
7049 freeSavedDisplayScreenshot(pu8Data);
7050
7051 return S_OK;
7052}
7053
7054STDMETHODIMP Machine::HotPlugCPU(ULONG aCpu)
7055{
7056 HRESULT rc = S_OK;
7057 LogFlowThisFunc(("\n"));
7058
7059 AutoCaller autoCaller(this);
7060 if (FAILED(autoCaller.rc())) return autoCaller.rc();
7061
7062 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
7063
7064 if (!mHWData->mCPUHotPlugEnabled)
7065 return setError(E_INVALIDARG, tr("CPU hotplug is not enabled"));
7066
7067 if (aCpu >= mHWData->mCPUCount)
7068 return setError(E_INVALIDARG, tr("CPU id exceeds number of possible CPUs [0:%lu]"), mHWData->mCPUCount-1);
7069
7070 if (mHWData->mCPUAttached[aCpu])
7071 return setError(VBOX_E_OBJECT_IN_USE, tr("CPU %lu is already attached"), aCpu);
7072
7073 alock.release();
7074 rc = onCPUChange(aCpu, false);
7075 alock.acquire();
7076 if (FAILED(rc)) return rc;
7077
7078 setModified(IsModified_MachineData);
7079 mHWData.backup();
7080 mHWData->mCPUAttached[aCpu] = true;
7081
7082 /** Save settings if online - @todo why is this required? -- @bugref{6818} */
7083 if (Global::IsOnline(mData->mMachineState))
7084 saveSettings(NULL);
7085
7086 return S_OK;
7087}
7088
7089STDMETHODIMP Machine::HotUnplugCPU(ULONG aCpu)
7090{
7091 HRESULT rc = S_OK;
7092 LogFlowThisFunc(("\n"));
7093
7094 AutoCaller autoCaller(this);
7095 if (FAILED(autoCaller.rc())) return autoCaller.rc();
7096
7097 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
7098
7099 if (!mHWData->mCPUHotPlugEnabled)
7100 return setError(E_INVALIDARG, tr("CPU hotplug is not enabled"));
7101
7102 if (aCpu >= SchemaDefs::MaxCPUCount)
7103 return setError(E_INVALIDARG,
7104 tr("CPU index exceeds maximum CPU count (must be in range [0:%lu])"),
7105 SchemaDefs::MaxCPUCount);
7106
7107 if (!mHWData->mCPUAttached[aCpu])
7108 return setError(VBOX_E_OBJECT_NOT_FOUND, tr("CPU %lu is not attached"), aCpu);
7109
7110 /* CPU 0 can't be detached */
7111 if (aCpu == 0)
7112 return setError(E_INVALIDARG, tr("It is not possible to detach CPU 0"));
7113
7114 alock.release();
7115 rc = onCPUChange(aCpu, true);
7116 alock.acquire();
7117 if (FAILED(rc)) return rc;
7118
7119 setModified(IsModified_MachineData);
7120 mHWData.backup();
7121 mHWData->mCPUAttached[aCpu] = false;
7122
7123 /** Save settings if online - @todo why is this required? -- @bugref{6818} */
7124 if (Global::IsOnline(mData->mMachineState))
7125 saveSettings(NULL);
7126
7127 return S_OK;
7128}
7129
7130STDMETHODIMP Machine::GetCPUStatus(ULONG aCpu, BOOL *aCpuAttached)
7131{
7132 LogFlowThisFunc(("\n"));
7133
7134 CheckComArgNotNull(aCpuAttached);
7135
7136 *aCpuAttached = false;
7137
7138 AutoCaller autoCaller(this);
7139 if (FAILED(autoCaller.rc())) return autoCaller.rc();
7140
7141 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
7142
7143 /* If hotplug is enabled the CPU is always enabled. */
7144 if (!mHWData->mCPUHotPlugEnabled)
7145 {
7146 if (aCpu < mHWData->mCPUCount)
7147 *aCpuAttached = true;
7148 }
7149 else
7150 {
7151 if (aCpu < SchemaDefs::MaxCPUCount)
7152 *aCpuAttached = mHWData->mCPUAttached[aCpu];
7153 }
7154
7155 return S_OK;
7156}
7157
7158STDMETHODIMP Machine::QueryLogFilename(ULONG aIdx, BSTR *aName)
7159{
7160 CheckComArgOutPointerValid(aName);
7161
7162 AutoCaller autoCaller(this);
7163 if (FAILED(autoCaller.rc())) return autoCaller.rc();
7164
7165 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
7166
7167 Utf8Str log = queryLogFilename(aIdx);
7168 if (!RTFileExists(log.c_str()))
7169 log.setNull();
7170 log.cloneTo(aName);
7171
7172 return S_OK;
7173}
7174
7175STDMETHODIMP Machine::ReadLog(ULONG aIdx, LONG64 aOffset, LONG64 aSize, ComSafeArrayOut(BYTE, aData))
7176{
7177 LogFlowThisFunc(("\n"));
7178 CheckComArgOutSafeArrayPointerValid(aData);
7179 if (aSize < 0)
7180 return setError(E_INVALIDARG, tr("The size argument (%lld) is negative"), aSize);
7181
7182 AutoCaller autoCaller(this);
7183 if (FAILED(autoCaller.rc())) return autoCaller.rc();
7184
7185 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
7186
7187 HRESULT rc = S_OK;
7188 Utf8Str log = queryLogFilename(aIdx);
7189
7190 /* do not unnecessarily hold the lock while doing something which does
7191 * not need the lock and potentially takes a long time. */
7192 alock.release();
7193
7194 /* Limit the chunk size to 32K for now, as that gives better performance
7195 * over (XP)COM, and keeps the SOAP reply size under 1M for the webservice.
7196 * One byte expands to approx. 25 bytes of breathtaking XML. */
7197 size_t cbData = (size_t)RT_MIN(aSize, 32768);
7198 com::SafeArray<BYTE> logData(cbData);
7199
7200 RTFILE LogFile;
7201 int vrc = RTFileOpen(&LogFile, log.c_str(),
7202 RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_DENY_NONE);
7203 if (RT_SUCCESS(vrc))
7204 {
7205 vrc = RTFileReadAt(LogFile, aOffset, logData.raw(), cbData, &cbData);
7206 if (RT_SUCCESS(vrc))
7207 logData.resize(cbData);
7208 else
7209 rc = setError(VBOX_E_IPRT_ERROR,
7210 tr("Could not read log file '%s' (%Rrc)"),
7211 log.c_str(), vrc);
7212 RTFileClose(LogFile);
7213 }
7214 else
7215 rc = setError(VBOX_E_IPRT_ERROR,
7216 tr("Could not open log file '%s' (%Rrc)"),
7217 log.c_str(), vrc);
7218
7219 if (FAILED(rc))
7220 logData.resize(0);
7221 logData.detachTo(ComSafeArrayOutArg(aData));
7222
7223 return rc;
7224}
7225
7226
7227/**
7228 * Currently this method doesn't attach device to the running VM,
7229 * just makes sure it's plugged on next VM start.
7230 */
7231STDMETHODIMP Machine::AttachHostPCIDevice(LONG hostAddress, LONG desiredGuestAddress, BOOL /*tryToUnbind*/)
7232{
7233 AutoCaller autoCaller(this);
7234 if (FAILED(autoCaller.rc())) return autoCaller.rc();
7235
7236 // lock scope
7237 {
7238 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
7239
7240 HRESULT rc = checkStateDependency(MutableStateDep);
7241 if (FAILED(rc)) return rc;
7242
7243 ChipsetType_T aChipset = ChipsetType_PIIX3;
7244 COMGETTER(ChipsetType)(&aChipset);
7245
7246 if (aChipset != ChipsetType_ICH9)
7247 {
7248 return setError(E_INVALIDARG,
7249 tr("Host PCI attachment only supported with ICH9 chipset"));
7250 }
7251
7252 // check if device with this host PCI address already attached
7253 for (HWData::PCIDeviceAssignmentList::iterator it = mHWData->mPCIDeviceAssignments.begin();
7254 it != mHWData->mPCIDeviceAssignments.end();
7255 ++it)
7256 {
7257 LONG iHostAddress = -1;
7258 ComPtr<PCIDeviceAttachment> pAttach;
7259 pAttach = *it;
7260 pAttach->COMGETTER(HostAddress)(&iHostAddress);
7261 if (iHostAddress == hostAddress)
7262 return setError(E_INVALIDARG,
7263 tr("Device with host PCI address already attached to this VM"));
7264 }
7265
7266 ComObjPtr<PCIDeviceAttachment> pda;
7267 char name[32];
7268
7269 RTStrPrintf(name, sizeof(name), "host%02x:%02x.%x", (hostAddress>>8) & 0xff, (hostAddress & 0xf8) >> 3, hostAddress & 7);
7270 Bstr bname(name);
7271 pda.createObject();
7272 pda->init(this, bname, hostAddress, desiredGuestAddress, TRUE);
7273 setModified(IsModified_MachineData);
7274 mHWData.backup();
7275 mHWData->mPCIDeviceAssignments.push_back(pda);
7276 }
7277
7278 return S_OK;
7279}
7280
7281/**
7282 * Currently this method doesn't detach device from the running VM,
7283 * just makes sure it's not plugged on next VM start.
7284 */
7285STDMETHODIMP Machine::DetachHostPCIDevice(LONG hostAddress)
7286{
7287 AutoCaller autoCaller(this);
7288 if (FAILED(autoCaller.rc())) return autoCaller.rc();
7289
7290 ComObjPtr<PCIDeviceAttachment> pAttach;
7291 bool fRemoved = false;
7292 HRESULT rc;
7293
7294 // lock scope
7295 {
7296 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
7297
7298 rc = checkStateDependency(MutableStateDep);
7299 if (FAILED(rc)) return rc;
7300
7301 for (HWData::PCIDeviceAssignmentList::iterator it = mHWData->mPCIDeviceAssignments.begin();
7302 it != mHWData->mPCIDeviceAssignments.end();
7303 ++it)
7304 {
7305 LONG iHostAddress = -1;
7306 pAttach = *it;
7307 pAttach->COMGETTER(HostAddress)(&iHostAddress);
7308 if (iHostAddress != -1 && iHostAddress == hostAddress)
7309 {
7310 setModified(IsModified_MachineData);
7311 mHWData.backup();
7312 mHWData->mPCIDeviceAssignments.remove(pAttach);
7313 fRemoved = true;
7314 break;
7315 }
7316 }
7317 }
7318
7319
7320 /* Fire event outside of the lock */
7321 if (fRemoved)
7322 {
7323 Assert(!pAttach.isNull());
7324 ComPtr<IEventSource> es;
7325 rc = mParent->COMGETTER(EventSource)(es.asOutParam());
7326 Assert(SUCCEEDED(rc));
7327 Bstr mid;
7328 rc = this->COMGETTER(Id)(mid.asOutParam());
7329 Assert(SUCCEEDED(rc));
7330 fireHostPCIDevicePlugEvent(es, mid.raw(), false /* unplugged */, true /* success */, pAttach, NULL);
7331 }
7332
7333 return fRemoved ? S_OK : setError(VBOX_E_OBJECT_NOT_FOUND,
7334 tr("No host PCI device %08x attached"),
7335 hostAddress
7336 );
7337}
7338
7339STDMETHODIMP Machine::COMGETTER(PCIDeviceAssignments)(ComSafeArrayOut(IPCIDeviceAttachment *, aAssignments))
7340{
7341 CheckComArgOutSafeArrayPointerValid(aAssignments);
7342
7343 AutoCaller autoCaller(this);
7344 if (FAILED(autoCaller.rc())) return autoCaller.rc();
7345
7346 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
7347
7348 SafeIfaceArray<IPCIDeviceAttachment> assignments(mHWData->mPCIDeviceAssignments);
7349 assignments.detachTo(ComSafeArrayOutArg(aAssignments));
7350
7351 return S_OK;
7352}
7353
7354STDMETHODIMP Machine::COMGETTER(BandwidthControl)(IBandwidthControl **aBandwidthControl)
7355{
7356 CheckComArgOutPointerValid(aBandwidthControl);
7357
7358 AutoCaller autoCaller(this);
7359 if (FAILED(autoCaller.rc())) return autoCaller.rc();
7360
7361 mBandwidthControl.queryInterfaceTo(aBandwidthControl);
7362
7363 return S_OK;
7364}
7365
7366STDMETHODIMP Machine::COMGETTER(TracingEnabled)(BOOL *pfEnabled)
7367{
7368 CheckComArgOutPointerValid(pfEnabled);
7369 AutoCaller autoCaller(this);
7370 HRESULT hrc = autoCaller.rc();
7371 if (SUCCEEDED(hrc))
7372 {
7373 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
7374 *pfEnabled = mHWData->mDebugging.fTracingEnabled;
7375 }
7376 return hrc;
7377}
7378
7379STDMETHODIMP Machine::COMSETTER(TracingEnabled)(BOOL fEnabled)
7380{
7381 AutoCaller autoCaller(this);
7382 HRESULT hrc = autoCaller.rc();
7383 if (SUCCEEDED(hrc))
7384 {
7385 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
7386 hrc = checkStateDependency(MutableStateDep);
7387 if (SUCCEEDED(hrc))
7388 {
7389 hrc = mHWData.backupEx();
7390 if (SUCCEEDED(hrc))
7391 {
7392 setModified(IsModified_MachineData);
7393 mHWData->mDebugging.fTracingEnabled = fEnabled != FALSE;
7394 }
7395 }
7396 }
7397 return hrc;
7398}
7399
7400STDMETHODIMP Machine::COMGETTER(TracingConfig)(BSTR *pbstrConfig)
7401{
7402 CheckComArgOutPointerValid(pbstrConfig);
7403 AutoCaller autoCaller(this);
7404 HRESULT hrc = autoCaller.rc();
7405 if (SUCCEEDED(hrc))
7406 {
7407 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
7408 hrc = mHWData->mDebugging.strTracingConfig.cloneToEx(pbstrConfig);
7409 }
7410 return hrc;
7411}
7412
7413STDMETHODIMP Machine::COMSETTER(TracingConfig)(IN_BSTR bstrConfig)
7414{
7415 CheckComArgStr(bstrConfig);
7416 AutoCaller autoCaller(this);
7417 HRESULT hrc = autoCaller.rc();
7418 if (SUCCEEDED(hrc))
7419 {
7420 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
7421 hrc = checkStateDependency(MutableStateDep);
7422 if (SUCCEEDED(hrc))
7423 {
7424 hrc = mHWData.backupEx();
7425 if (SUCCEEDED(hrc))
7426 {
7427 hrc = mHWData->mDebugging.strTracingConfig.cloneEx(bstrConfig);
7428 if (SUCCEEDED(hrc))
7429 setModified(IsModified_MachineData);
7430 }
7431 }
7432 }
7433 return hrc;
7434
7435}
7436
7437STDMETHODIMP Machine::COMGETTER(AllowTracingToAccessVM)(BOOL *pfAllow)
7438{
7439 CheckComArgOutPointerValid(pfAllow);
7440 AutoCaller autoCaller(this);
7441 HRESULT hrc = autoCaller.rc();
7442 if (SUCCEEDED(hrc))
7443 {
7444 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
7445 *pfAllow = mHWData->mDebugging.fAllowTracingToAccessVM;
7446 }
7447 return hrc;
7448}
7449
7450STDMETHODIMP Machine::COMSETTER(AllowTracingToAccessVM)(BOOL fAllow)
7451{
7452 AutoCaller autoCaller(this);
7453 HRESULT hrc = autoCaller.rc();
7454 if (SUCCEEDED(hrc))
7455 {
7456 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
7457 hrc = checkStateDependency(MutableStateDep);
7458 if (SUCCEEDED(hrc))
7459 {
7460 hrc = mHWData.backupEx();
7461 if (SUCCEEDED(hrc))
7462 {
7463 setModified(IsModified_MachineData);
7464 mHWData->mDebugging.fAllowTracingToAccessVM = fAllow != FALSE;
7465 }
7466 }
7467 }
7468 return hrc;
7469}
7470
7471STDMETHODIMP Machine::COMGETTER(AutostartEnabled)(BOOL *pfEnabled)
7472{
7473 CheckComArgOutPointerValid(pfEnabled);
7474 AutoCaller autoCaller(this);
7475 HRESULT hrc = autoCaller.rc();
7476 if (SUCCEEDED(hrc))
7477 {
7478 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
7479 *pfEnabled = mHWData->mAutostart.fAutostartEnabled;
7480 }
7481 return hrc;
7482}
7483
7484STDMETHODIMP Machine::COMSETTER(AutostartEnabled)(BOOL fEnabled)
7485{
7486 AutoCaller autoCaller(this);
7487 HRESULT hrc = autoCaller.rc();
7488 if (SUCCEEDED(hrc))
7489 {
7490 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
7491 hrc = checkStateDependency(MutableStateDep);
7492 if ( SUCCEEDED(hrc)
7493 && mHWData->mAutostart.fAutostartEnabled != !!fEnabled)
7494 {
7495 AutostartDb *autostartDb = mParent->i_getAutostartDb();
7496 int vrc;
7497
7498 if (fEnabled)
7499 vrc = autostartDb->addAutostartVM(mUserData->s.strName.c_str());
7500 else
7501 vrc = autostartDb->removeAutostartVM(mUserData->s.strName.c_str());
7502
7503 if (RT_SUCCESS(vrc))
7504 {
7505 hrc = mHWData.backupEx();
7506 if (SUCCEEDED(hrc))
7507 {
7508 setModified(IsModified_MachineData);
7509 mHWData->mAutostart.fAutostartEnabled = fEnabled != FALSE;
7510 }
7511 }
7512 else if (vrc == VERR_NOT_SUPPORTED)
7513 hrc = setError(VBOX_E_NOT_SUPPORTED,
7514 tr("The VM autostart feature is not supported on this platform"));
7515 else if (vrc == VERR_PATH_NOT_FOUND)
7516 hrc = setError(E_FAIL,
7517 tr("The path to the autostart database is not set"));
7518 else
7519 hrc = setError(E_UNEXPECTED,
7520 tr("%s machine '%s' to the autostart database failed with %Rrc"),
7521 fEnabled ? "Adding" : "Removing",
7522 mUserData->s.strName.c_str(), vrc);
7523 }
7524 }
7525 return hrc;
7526}
7527
7528STDMETHODIMP Machine::COMGETTER(AutostartDelay)(ULONG *puDelay)
7529{
7530 CheckComArgOutPointerValid(puDelay);
7531 AutoCaller autoCaller(this);
7532 HRESULT hrc = autoCaller.rc();
7533 if (SUCCEEDED(hrc))
7534 {
7535 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
7536 *puDelay = mHWData->mAutostart.uAutostartDelay;
7537 }
7538 return hrc;
7539}
7540
7541STDMETHODIMP Machine::COMSETTER(AutostartDelay)(ULONG uDelay)
7542{
7543 AutoCaller autoCaller(this);
7544 HRESULT hrc = autoCaller.rc();
7545 if (SUCCEEDED(hrc))
7546 {
7547 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
7548 hrc = checkStateDependency(MutableStateDep);
7549 if (SUCCEEDED(hrc))
7550 {
7551 hrc = mHWData.backupEx();
7552 if (SUCCEEDED(hrc))
7553 {
7554 setModified(IsModified_MachineData);
7555 mHWData->mAutostart.uAutostartDelay = uDelay;
7556 }
7557 }
7558 }
7559 return hrc;
7560}
7561
7562STDMETHODIMP Machine::COMGETTER(AutostopType)(AutostopType_T *penmAutostopType)
7563{
7564 CheckComArgOutPointerValid(penmAutostopType);
7565 AutoCaller autoCaller(this);
7566 HRESULT hrc = autoCaller.rc();
7567 if (SUCCEEDED(hrc))
7568 {
7569 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
7570 *penmAutostopType = mHWData->mAutostart.enmAutostopType;
7571 }
7572 return hrc;
7573}
7574
7575STDMETHODIMP Machine::COMSETTER(AutostopType)(AutostopType_T enmAutostopType)
7576{
7577 AutoCaller autoCaller(this);
7578 HRESULT hrc = autoCaller.rc();
7579 if (SUCCEEDED(hrc))
7580 {
7581 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
7582 hrc = checkStateDependency(MutableStateDep);
7583 if ( SUCCEEDED(hrc)
7584 && mHWData->mAutostart.enmAutostopType != enmAutostopType)
7585 {
7586 AutostartDb *autostartDb = mParent->i_getAutostartDb();
7587 int vrc;
7588
7589 if (enmAutostopType != AutostopType_Disabled)
7590 vrc = autostartDb->addAutostopVM(mUserData->s.strName.c_str());
7591 else
7592 vrc = autostartDb->removeAutostopVM(mUserData->s.strName.c_str());
7593
7594 if (RT_SUCCESS(vrc))
7595 {
7596 hrc = mHWData.backupEx();
7597 if (SUCCEEDED(hrc))
7598 {
7599 setModified(IsModified_MachineData);
7600 mHWData->mAutostart.enmAutostopType = enmAutostopType;
7601 }
7602 }
7603 else if (vrc == VERR_NOT_SUPPORTED)
7604 hrc = setError(VBOX_E_NOT_SUPPORTED,
7605 tr("The VM autostop feature is not supported on this platform"));
7606 else if (vrc == VERR_PATH_NOT_FOUND)
7607 hrc = setError(E_FAIL,
7608 tr("The path to the autostart database is not set"));
7609 else
7610 hrc = setError(E_UNEXPECTED,
7611 tr("%s machine '%s' to the autostop database failed with %Rrc"),
7612 enmAutostopType != AutostopType_Disabled ? "Adding" : "Removing",
7613 mUserData->s.strName.c_str(), vrc);
7614 }
7615 }
7616 return hrc;
7617}
7618
7619STDMETHODIMP Machine::COMGETTER(DefaultFrontend)(BSTR *aDefaultFrontend)
7620{
7621 CheckComArgOutPointerValid(aDefaultFrontend);
7622 AutoCaller autoCaller(this);
7623 HRESULT hrc = autoCaller.rc();
7624 if (SUCCEEDED(hrc))
7625 {
7626 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
7627 mHWData->mDefaultFrontend.cloneTo(aDefaultFrontend);
7628 }
7629 return hrc;
7630}
7631
7632STDMETHODIMP Machine::COMSETTER(DefaultFrontend)(IN_BSTR aDefaultFrontend)
7633{
7634 CheckComArgStr(aDefaultFrontend);
7635 AutoCaller autoCaller(this);
7636 HRESULT hrc = autoCaller.rc();
7637 if (SUCCEEDED(hrc))
7638 {
7639 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
7640 hrc = checkStateDependency(MutableOrSavedStateDep);
7641 if (SUCCEEDED(hrc))
7642 {
7643 hrc = mHWData.backupEx();
7644 if (SUCCEEDED(hrc))
7645 {
7646 setModified(IsModified_MachineData);
7647 mHWData->mDefaultFrontend = aDefaultFrontend;
7648 }
7649 }
7650 }
7651 return hrc;
7652}
7653
7654STDMETHODIMP Machine::COMGETTER(Icon)(ComSafeArrayOut(BYTE, aIcon))
7655{
7656 CheckComArgSafeArrayNotNull(aIcon);
7657 CheckComArgOutSafeArrayPointerValid(aIcon);
7658 AutoCaller autoCaller(this);
7659 HRESULT hrc = autoCaller.rc();
7660 if (SUCCEEDED(hrc))
7661 {
7662 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
7663 com::SafeArray<BYTE> icon(mUserData->mIcon.size());
7664 memcpy(icon.raw(), &mUserData->mIcon[0], mUserData->mIcon.size());
7665 icon.detachTo(ComSafeArrayOutArg(aIcon));
7666 }
7667 return hrc;
7668}
7669
7670STDMETHODIMP Machine::COMSETTER(Icon)(ComSafeArrayIn(BYTE, aIcon))
7671{
7672 CheckComArgSafeArrayNotNull(aIcon);
7673 AutoCaller autoCaller(this);
7674 HRESULT hrc = autoCaller.rc();
7675 if (SUCCEEDED(hrc))
7676 {
7677 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
7678 hrc = checkStateDependency(MutableOrSavedStateDep);
7679 if (SUCCEEDED(hrc))
7680 {
7681 setModified(IsModified_MachineData);
7682 mUserData.backup();
7683 com::SafeArray<BYTE> icon(ComSafeArrayInArg(aIcon));
7684 mUserData->mIcon.resize(icon.size());
7685 memcpy(&mUserData->mIcon[0], icon.raw(), mUserData->mIcon.size());
7686 }
7687 }
7688 return hrc;
7689}
7690
7691STDMETHODIMP Machine::COMGETTER(USBProxyAvailable)(BOOL *aAvailable)
7692{
7693 CheckComArgOutPointerValid(aAvailable);
7694
7695 AutoCaller autoCaller(this);
7696 if (FAILED(autoCaller.rc())) return autoCaller.rc();
7697
7698#ifdef VBOX_WITH_USB
7699 *aAvailable = true;
7700#else
7701 *aAvailable = false;
7702#endif
7703 return S_OK;
7704}
7705
7706STDMETHODIMP Machine::CloneTo(IMachine *pTarget, CloneMode_T mode, ComSafeArrayIn(CloneOptions_T, options), IProgress **pProgress)
7707{
7708 LogFlowFuncEnter();
7709
7710 CheckComArgNotNull(pTarget);
7711 CheckComArgOutPointerValid(pProgress);
7712
7713 /* Convert the options. */
7714 RTCList<CloneOptions_T> optList;
7715 if (options != NULL)
7716 optList = com::SafeArray<CloneOptions_T>(ComSafeArrayInArg(options)).toList();
7717
7718 if (optList.contains(CloneOptions_Link))
7719 {
7720 if (!isSnapshotMachine())
7721 return setError(E_INVALIDARG,
7722 tr("Linked clone can only be created from a snapshot"));
7723 if (mode != CloneMode_MachineState)
7724 return setError(E_INVALIDARG,
7725 tr("Linked clone can only be created for a single machine state"));
7726 }
7727 AssertReturn(!(optList.contains(CloneOptions_KeepAllMACs) && optList.contains(CloneOptions_KeepNATMACs)), E_INVALIDARG);
7728
7729 AutoCaller autoCaller(this);
7730 if (FAILED(autoCaller.rc())) return autoCaller.rc();
7731
7732
7733 MachineCloneVM *pWorker = new MachineCloneVM(this, static_cast<Machine*>(pTarget), mode, optList);
7734
7735 HRESULT rc = pWorker->start(pProgress);
7736
7737 LogFlowFuncLeave();
7738
7739 return rc;
7740}
7741
7742// public methods for internal purposes
7743/////////////////////////////////////////////////////////////////////////////
7744
7745/**
7746 * Adds the given IsModified_* flag to the dirty flags of the machine.
7747 * This must be called either during loadSettings or under the machine write lock.
7748 * @param fl
7749 */
7750void Machine::setModified(uint32_t fl, bool fAllowStateModification /* = true */)
7751{
7752 mData->flModifications |= fl;
7753 if (fAllowStateModification && isStateModificationAllowed())
7754 mData->mCurrentStateModified = true;
7755}
7756
7757/**
7758 * Adds the given IsModified_* flag to the dirty flags of the machine, taking
7759 * care of the write locking.
7760 *
7761 * @param fModifications The flag to add.
7762 */
7763void Machine::setModifiedLock(uint32_t fModification, bool fAllowStateModification /* = true */)
7764{
7765 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
7766 setModified(fModification, fAllowStateModification);
7767}
7768
7769/**
7770 * Saves the registry entry of this machine to the given configuration node.
7771 *
7772 * @param aEntryNode Node to save the registry entry to.
7773 *
7774 * @note locks this object for reading.
7775 */
7776HRESULT Machine::saveRegistryEntry(settings::MachineRegistryEntry &data)
7777{
7778 AutoLimitedCaller autoCaller(this);
7779 AssertComRCReturnRC(autoCaller.rc());
7780
7781 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
7782
7783 data.uuid = mData->mUuid;
7784 data.strSettingsFile = mData->m_strConfigFile;
7785
7786 return S_OK;
7787}
7788
7789/**
7790 * Calculates the absolute path of the given path taking the directory of the
7791 * machine settings file as the current directory.
7792 *
7793 * @param aPath Path to calculate the absolute path for.
7794 * @param aResult Where to put the result (used only on success, can be the
7795 * same Utf8Str instance as passed in @a aPath).
7796 * @return IPRT result.
7797 *
7798 * @note Locks this object for reading.
7799 */
7800int Machine::calculateFullPath(const Utf8Str &strPath, Utf8Str &aResult)
7801{
7802 AutoCaller autoCaller(this);
7803 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
7804
7805 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
7806
7807 AssertReturn(!mData->m_strConfigFileFull.isEmpty(), VERR_GENERAL_FAILURE);
7808
7809 Utf8Str strSettingsDir = mData->m_strConfigFileFull;
7810
7811 strSettingsDir.stripFilename();
7812 char folder[RTPATH_MAX];
7813 int vrc = RTPathAbsEx(strSettingsDir.c_str(), strPath.c_str(), folder, sizeof(folder));
7814 if (RT_SUCCESS(vrc))
7815 aResult = folder;
7816
7817 return vrc;
7818}
7819
7820/**
7821 * Copies strSource to strTarget, making it relative to the machine folder
7822 * if it is a subdirectory thereof, or simply copying it otherwise.
7823 *
7824 * @param strSource Path to evaluate and copy.
7825 * @param strTarget Buffer to receive target path.
7826 *
7827 * @note Locks this object for reading.
7828 */
7829void Machine::copyPathRelativeToMachine(const Utf8Str &strSource,
7830 Utf8Str &strTarget)
7831{
7832 AutoCaller autoCaller(this);
7833 AssertComRCReturn(autoCaller.rc(), (void)0);
7834
7835 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
7836
7837 AssertReturnVoid(!mData->m_strConfigFileFull.isEmpty());
7838 // use strTarget as a temporary buffer to hold the machine settings dir
7839 strTarget = mData->m_strConfigFileFull;
7840 strTarget.stripFilename();
7841 if (RTPathStartsWith(strSource.c_str(), strTarget.c_str()))
7842 {
7843 // is relative: then append what's left
7844 strTarget = strSource.substr(strTarget.length() + 1); // skip '/'
7845 // for empty paths (only possible for subdirs) use "." to avoid
7846 // triggering default settings for not present config attributes.
7847 if (strTarget.isEmpty())
7848 strTarget = ".";
7849 }
7850 else
7851 // is not relative: then overwrite
7852 strTarget = strSource;
7853}
7854
7855/**
7856 * Returns the full path to the machine's log folder in the
7857 * \a aLogFolder argument.
7858 */
7859void Machine::getLogFolder(Utf8Str &aLogFolder)
7860{
7861 AutoCaller autoCaller(this);
7862 AssertComRCReturnVoid(autoCaller.rc());
7863
7864 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
7865
7866 char szTmp[RTPATH_MAX];
7867 int vrc = RTEnvGetEx(RTENV_DEFAULT, "VBOX_USER_VMLOGDIR", szTmp, sizeof(szTmp), NULL);
7868 if (RT_SUCCESS(vrc))
7869 {
7870 if (szTmp[0] && !mUserData.isNull())
7871 {
7872 char szTmp2[RTPATH_MAX];
7873 vrc = RTPathAbs(szTmp, szTmp2, sizeof(szTmp2));
7874 if (RT_SUCCESS(vrc))
7875 aLogFolder = BstrFmt("%s%c%s",
7876 szTmp2,
7877 RTPATH_DELIMITER,
7878 mUserData->s.strName.c_str()); // path/to/logfolder/vmname
7879 }
7880 else
7881 vrc = VERR_PATH_IS_RELATIVE;
7882 }
7883
7884 if (RT_FAILURE(vrc))
7885 {
7886 // fallback if VBOX_USER_LOGHOME is not set or invalid
7887 aLogFolder = mData->m_strConfigFileFull; // path/to/machinesfolder/vmname/vmname.vbox
7888 aLogFolder.stripFilename(); // path/to/machinesfolder/vmname
7889 aLogFolder.append(RTPATH_DELIMITER);
7890 aLogFolder.append("Logs"); // path/to/machinesfolder/vmname/Logs
7891 }
7892}
7893
7894/**
7895 * Returns the full path to the machine's log file for an given index.
7896 */
7897Utf8Str Machine::queryLogFilename(ULONG idx)
7898{
7899 Utf8Str logFolder;
7900 getLogFolder(logFolder);
7901 Assert(logFolder.length());
7902 Utf8Str log;
7903 if (idx == 0)
7904 log = Utf8StrFmt("%s%cVBox.log",
7905 logFolder.c_str(), RTPATH_DELIMITER);
7906 else
7907 log = Utf8StrFmt("%s%cVBox.log.%d",
7908 logFolder.c_str(), RTPATH_DELIMITER, idx);
7909 return log;
7910}
7911
7912/**
7913 * Composes a unique saved state filename based on the current system time. The filename is
7914 * granular to the second so this will work so long as no more than one snapshot is taken on
7915 * a machine per second.
7916 *
7917 * Before version 4.1, we used this formula for saved state files:
7918 * Utf8StrFmt("%s%c{%RTuuid}.sav", strFullSnapshotFolder.c_str(), RTPATH_DELIMITER, mData->mUuid.raw())
7919 * which no longer works because saved state files can now be shared between the saved state of the
7920 * "saved" machine and an online snapshot, and the following would cause problems:
7921 * 1) save machine
7922 * 2) create online snapshot from that machine state --> reusing saved state file
7923 * 3) save machine again --> filename would be reused, breaking the online snapshot
7924 *
7925 * So instead we now use a timestamp.
7926 *
7927 * @param str
7928 */
7929void Machine::composeSavedStateFilename(Utf8Str &strStateFilePath)
7930{
7931 AutoCaller autoCaller(this);
7932 AssertComRCReturnVoid(autoCaller.rc());
7933
7934 {
7935 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
7936 calculateFullPath(mUserData->s.strSnapshotFolder, strStateFilePath);
7937 }
7938
7939 RTTIMESPEC ts;
7940 RTTimeNow(&ts);
7941 RTTIME time;
7942 RTTimeExplode(&time, &ts);
7943
7944 strStateFilePath += RTPATH_DELIMITER;
7945 strStateFilePath += Utf8StrFmt("%04d-%02u-%02uT%02u-%02u-%02u-%09uZ.sav",
7946 time.i32Year, time.u8Month, time.u8MonthDay,
7947 time.u8Hour, time.u8Minute, time.u8Second, time.u32Nanosecond);
7948}
7949
7950/**
7951 * Returns the full path to the default video capture file.
7952 */
7953void Machine::getDefaultVideoCaptureFile(Utf8Str &strFile)
7954{
7955 AutoCaller autoCaller(this);
7956 AssertComRCReturnVoid(autoCaller.rc());
7957
7958 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
7959
7960 strFile = mData->m_strConfigFileFull; // path/to/machinesfolder/vmname/vmname.vbox
7961 strFile.stripSuffix(); // path/to/machinesfolder/vmname/vmname
7962 strFile.append(".webm"); // path/to/machinesfolder/vmname/vmname.webm
7963}
7964
7965/**
7966 * Returns whether at least one USB controller is present for the VM.
7967 */
7968bool Machine::isUSBControllerPresent()
7969{
7970 AutoCaller autoCaller(this);
7971 AssertComRCReturn(autoCaller.rc(), false);
7972
7973 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
7974
7975 return (mUSBControllers->size() > 0);
7976}
7977
7978/**
7979 * @note Locks this object for writing, calls the client process
7980 * (inside the lock).
7981 */
7982HRESULT Machine::launchVMProcess(IInternalSessionControl *aControl,
7983 const Utf8Str &strFrontend,
7984 const Utf8Str &strEnvironment,
7985 ProgressProxy *aProgress)
7986{
7987 LogFlowThisFuncEnter();
7988
7989 AssertReturn(aControl, E_FAIL);
7990 AssertReturn(aProgress, E_FAIL);
7991 AssertReturn(!strFrontend.isEmpty(), E_FAIL);
7992
7993 AutoCaller autoCaller(this);
7994 if (FAILED(autoCaller.rc())) return autoCaller.rc();
7995
7996 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
7997
7998 if (!mData->mRegistered)
7999 return setError(E_UNEXPECTED,
8000 tr("The machine '%s' is not registered"),
8001 mUserData->s.strName.c_str());
8002
8003 LogFlowThisFunc(("mSession.mState=%s\n", Global::stringifySessionState(mData->mSession.mState)));
8004
8005 if ( mData->mSession.mState == SessionState_Locked
8006 || mData->mSession.mState == SessionState_Spawning
8007 || mData->mSession.mState == SessionState_Unlocking)
8008 return setError(VBOX_E_INVALID_OBJECT_STATE,
8009 tr("The machine '%s' is already locked by a session (or being locked or unlocked)"),
8010 mUserData->s.strName.c_str());
8011
8012 /* may not be busy */
8013 AssertReturn(!Global::IsOnlineOrTransient(mData->mMachineState), E_FAIL);
8014
8015 /* get the path to the executable */
8016 char szPath[RTPATH_MAX];
8017 RTPathAppPrivateArch(szPath, sizeof(szPath) - 1);
8018 size_t sz = strlen(szPath);
8019 szPath[sz++] = RTPATH_DELIMITER;
8020 szPath[sz] = 0;
8021 char *cmd = szPath + sz;
8022 sz = sizeof(szPath) - sz;
8023
8024 int vrc = VINF_SUCCESS;
8025 RTPROCESS pid = NIL_RTPROCESS;
8026
8027 RTENV env = RTENV_DEFAULT;
8028
8029 if (!strEnvironment.isEmpty())
8030 {
8031 char *newEnvStr = NULL;
8032
8033 do
8034 {
8035 /* clone the current environment */
8036 int vrc2 = RTEnvClone(&env, RTENV_DEFAULT);
8037 AssertRCBreakStmt(vrc2, vrc = vrc2);
8038
8039 newEnvStr = RTStrDup(strEnvironment.c_str());
8040 AssertPtrBreakStmt(newEnvStr, vrc = vrc2);
8041
8042 /* put new variables to the environment
8043 * (ignore empty variable names here since RTEnv API
8044 * intentionally doesn't do that) */
8045 char *var = newEnvStr;
8046 for (char *p = newEnvStr; *p; ++p)
8047 {
8048 if (*p == '\n' && (p == newEnvStr || *(p - 1) != '\\'))
8049 {
8050 *p = '\0';
8051 if (*var)
8052 {
8053 char *val = strchr(var, '=');
8054 if (val)
8055 {
8056 *val++ = '\0';
8057 vrc2 = RTEnvSetEx(env, var, val);
8058 }
8059 else
8060 vrc2 = RTEnvUnsetEx(env, var);
8061 if (RT_FAILURE(vrc2))
8062 break;
8063 }
8064 var = p + 1;
8065 }
8066 }
8067 if (RT_SUCCESS(vrc2) && *var)
8068 vrc2 = RTEnvPutEx(env, var);
8069
8070 AssertRCBreakStmt(vrc2, vrc = vrc2);
8071 }
8072 while (0);
8073
8074 if (newEnvStr != NULL)
8075 RTStrFree(newEnvStr);
8076 }
8077
8078#ifdef VBOX_WITH_QTGUI
8079 if (strFrontend == "gui" || strFrontend == "GUI/Qt")
8080 {
8081# ifdef RT_OS_DARWIN /* Avoid Launch Services confusing this with the selector by using a helper app. */
8082 /* Modify the base path so that we don't need to use ".." below. */
8083 RTPathStripTrailingSlash(szPath);
8084 RTPathStripFilename(szPath);
8085 sz = strlen(szPath);
8086 cmd = szPath + sz;
8087 sz = sizeof(szPath) - sz;
8088
8089#define OSX_APP_NAME "VirtualBoxVM"
8090#define OSX_APP_PATH_FMT "/Resources/%s.app/Contents/MacOS/VirtualBoxVM"
8091
8092 Utf8Str strAppOverride = getExtraData(Utf8Str("VBoxInternal2/VirtualBoxVMAppOverride"));
8093 if ( strAppOverride.contains(".")
8094 || strAppOverride.contains("/")
8095 || strAppOverride.contains("\\")
8096 || strAppOverride.contains(":"))
8097 strAppOverride.setNull();
8098 Utf8Str strAppPath;
8099 if (!strAppOverride.isEmpty())
8100 {
8101 strAppPath = Utf8StrFmt(OSX_APP_PATH_FMT, strAppOverride.c_str());
8102 Utf8Str strFullPath(szPath);
8103 strFullPath.append(strAppPath);
8104 /* there is a race, but people using this deserve the failure */
8105 if (!RTFileExists(strFullPath.c_str()))
8106 strAppOverride.setNull();
8107 }
8108 if (strAppOverride.isEmpty())
8109 strAppPath = Utf8StrFmt(OSX_APP_PATH_FMT, OSX_APP_NAME);
8110 const char *VirtualBox_exe = strAppPath.c_str();
8111 AssertReturn(sz >= strlen(VirtualBox_exe), E_UNEXPECTED);
8112# else
8113 const char VirtualBox_exe[] = "VirtualBox" HOSTSUFF_EXE;
8114 Assert(sz >= sizeof(VirtualBox_exe));
8115# endif
8116 strcpy(cmd, VirtualBox_exe);
8117
8118 Utf8Str idStr = mData->mUuid.toString();
8119 const char * args[] = {szPath, "--comment", mUserData->s.strName.c_str(), "--startvm", idStr.c_str(), "--no-startvm-errormsgbox", 0 };
8120 vrc = RTProcCreate(szPath, args, env, 0, &pid);
8121 }
8122#else /* !VBOX_WITH_QTGUI */
8123 if (0)
8124 ;
8125#endif /* VBOX_WITH_QTGUI */
8126
8127 else
8128
8129#ifdef VBOX_WITH_VBOXSDL
8130 if (strFrontend == "sdl" || strFrontend == "GUI/SDL")
8131 {
8132 const char VBoxSDL_exe[] = "VBoxSDL" HOSTSUFF_EXE;
8133 Assert(sz >= sizeof(VBoxSDL_exe));
8134 strcpy(cmd, VBoxSDL_exe);
8135
8136 Utf8Str idStr = mData->mUuid.toString();
8137 const char * args[] = {szPath, "--comment", mUserData->s.strName.c_str(), "--startvm", idStr.c_str(), 0 };
8138 vrc = RTProcCreate(szPath, args, env, 0, &pid);
8139 }
8140#else /* !VBOX_WITH_VBOXSDL */
8141 if (0)
8142 ;
8143#endif /* !VBOX_WITH_VBOXSDL */
8144
8145 else
8146
8147#ifdef VBOX_WITH_HEADLESS
8148 if ( strFrontend == "headless"
8149 || strFrontend == "capture"
8150 || strFrontend == "vrdp" /* Deprecated. Same as headless. */
8151 )
8152 {
8153 /* On pre-4.0 the "headless" type was used for passing "--vrdp off" to VBoxHeadless to let it work in OSE,
8154 * which did not contain VRDP server. In VBox 4.0 the remote desktop server (VRDE) is optional,
8155 * and a VM works even if the server has not been installed.
8156 * So in 4.0 the "headless" behavior remains the same for default VBox installations.
8157 * Only if a VRDE has been installed and the VM enables it, the "headless" will work
8158 * differently in 4.0 and 3.x.
8159 */
8160 const char VBoxHeadless_exe[] = "VBoxHeadless" HOSTSUFF_EXE;
8161 Assert(sz >= sizeof(VBoxHeadless_exe));
8162 strcpy(cmd, VBoxHeadless_exe);
8163
8164 Utf8Str idStr = mData->mUuid.toString();
8165 /* Leave space for "--capture" arg. */
8166 const char * args[] = {szPath, "--comment", mUserData->s.strName.c_str(),
8167 "--startvm", idStr.c_str(),
8168 "--vrde", "config",
8169 0, /* For "--capture". */
8170 0 };
8171 if (strFrontend == "capture")
8172 {
8173 unsigned pos = RT_ELEMENTS(args) - 2;
8174 args[pos] = "--capture";
8175 }
8176 vrc = RTProcCreate(szPath, args, env,
8177#ifdef RT_OS_WINDOWS
8178 RTPROC_FLAGS_NO_WINDOW
8179#else
8180 0
8181#endif
8182 , &pid);
8183 }
8184#else /* !VBOX_WITH_HEADLESS */
8185 if (0)
8186 ;
8187#endif /* !VBOX_WITH_HEADLESS */
8188 else
8189 {
8190 RTEnvDestroy(env);
8191 return setError(E_INVALIDARG,
8192 tr("Invalid frontend name: '%s'"),
8193 strFrontend.c_str());
8194 }
8195
8196 RTEnvDestroy(env);
8197
8198 if (RT_FAILURE(vrc))
8199 return setError(VBOX_E_IPRT_ERROR,
8200 tr("Could not launch a process for the machine '%s' (%Rrc)"),
8201 mUserData->s.strName.c_str(), vrc);
8202
8203 LogFlowThisFunc(("launched.pid=%d(0x%x)\n", pid, pid));
8204
8205 /*
8206 * Note that we don't release the lock here before calling the client,
8207 * because it doesn't need to call us back if called with a NULL argument.
8208 * Releasing the lock here is dangerous because we didn't prepare the
8209 * launch data yet, but the client we've just started may happen to be
8210 * too fast and call LockMachine() that will fail (because of PID, etc.),
8211 * so that the Machine will never get out of the Spawning session state.
8212 */
8213
8214 /* inform the session that it will be a remote one */
8215 LogFlowThisFunc(("Calling AssignMachine (NULL)...\n"));
8216#ifndef VBOX_WITH_GENERIC_SESSION_WATCHER
8217 HRESULT rc = aControl->AssignMachine(NULL, LockType_Write, Bstr::Empty.raw());
8218#else /* VBOX_WITH_GENERIC_SESSION_WATCHER */
8219 HRESULT rc = aControl->AssignMachine(NULL, LockType_Write, NULL);
8220#endif /* VBOX_WITH_GENERIC_SESSION_WATCHER */
8221 LogFlowThisFunc(("AssignMachine (NULL) returned %08X\n", rc));
8222
8223 if (FAILED(rc))
8224 {
8225 /* restore the session state */
8226 mData->mSession.mState = SessionState_Unlocked;
8227 alock.release();
8228 mParent->i_addProcessToReap(pid);
8229 /* The failure may occur w/o any error info (from RPC), so provide one */
8230 return setError(VBOX_E_VM_ERROR,
8231 tr("Failed to assign the machine to the session (%Rhrc)"), rc);
8232 }
8233
8234 /* attach launch data to the machine */
8235 Assert(mData->mSession.mPID == NIL_RTPROCESS);
8236 mData->mSession.mRemoteControls.push_back(aControl);
8237 mData->mSession.mProgress = aProgress;
8238 mData->mSession.mPID = pid;
8239 mData->mSession.mState = SessionState_Spawning;
8240 mData->mSession.mType = strFrontend;
8241
8242 alock.release();
8243 mParent->i_addProcessToReap(pid);
8244
8245 LogFlowThisFuncLeave();
8246 return S_OK;
8247}
8248
8249/**
8250 * Returns @c true if the given session machine instance has an open direct
8251 * session (and optionally also for direct sessions which are closing) and
8252 * returns the session control machine instance if so.
8253 *
8254 * Note that when the method returns @c false, the arguments remain unchanged.
8255 *
8256 * @param aMachine Session machine object.
8257 * @param aControl Direct session control object (optional).
8258 * @param aAllowClosing If true then additionally a session which is currently
8259 * being closed will also be allowed.
8260 *
8261 * @note locks this object for reading.
8262 */
8263bool Machine::isSessionOpen(ComObjPtr<SessionMachine> &aMachine,
8264 ComPtr<IInternalSessionControl> *aControl /*= NULL*/,
8265 bool aAllowClosing /*= false*/)
8266{
8267 AutoLimitedCaller autoCaller(this);
8268 AssertComRCReturn(autoCaller.rc(), false);
8269
8270 /* just return false for inaccessible machines */
8271 if (autoCaller.state() != Ready)
8272 return false;
8273
8274 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
8275
8276 if ( mData->mSession.mState == SessionState_Locked
8277 || (aAllowClosing && mData->mSession.mState == SessionState_Unlocking)
8278 )
8279 {
8280 AssertReturn(!mData->mSession.mMachine.isNull(), false);
8281
8282 aMachine = mData->mSession.mMachine;
8283
8284 if (aControl != NULL)
8285 *aControl = mData->mSession.mDirectControl;
8286
8287 return true;
8288 }
8289
8290 return false;
8291}
8292
8293/**
8294 * Returns @c true if the given machine has an spawning direct session.
8295 *
8296 * @note locks this object for reading.
8297 */
8298bool Machine::isSessionSpawning()
8299{
8300 AutoLimitedCaller autoCaller(this);
8301 AssertComRCReturn(autoCaller.rc(), false);
8302
8303 /* just return false for inaccessible machines */
8304 if (autoCaller.state() != Ready)
8305 return false;
8306
8307 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
8308
8309 if (mData->mSession.mState == SessionState_Spawning)
8310 return true;
8311
8312 return false;
8313}
8314
8315/**
8316 * Called from the client watcher thread to check for unexpected client process
8317 * death during Session_Spawning state (e.g. before it successfully opened a
8318 * direct session).
8319 *
8320 * On Win32 and on OS/2, this method is called only when we've got the
8321 * direct client's process termination notification, so it always returns @c
8322 * true.
8323 *
8324 * On other platforms, this method returns @c true if the client process is
8325 * terminated and @c false if it's still alive.
8326 *
8327 * @note Locks this object for writing.
8328 */
8329bool Machine::checkForSpawnFailure()
8330{
8331 AutoCaller autoCaller(this);
8332 if (!autoCaller.isOk())
8333 {
8334 /* nothing to do */
8335 LogFlowThisFunc(("Already uninitialized!\n"));
8336 return true;
8337 }
8338
8339 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
8340
8341 if (mData->mSession.mState != SessionState_Spawning)
8342 {
8343 /* nothing to do */
8344 LogFlowThisFunc(("Not spawning any more!\n"));
8345 return true;
8346 }
8347
8348 HRESULT rc = S_OK;
8349
8350 /* PID not yet initialized, skip check. */
8351 if (mData->mSession.mPID == NIL_RTPROCESS)
8352 return false;
8353
8354 RTPROCSTATUS status;
8355 int vrc = RTProcWait(mData->mSession.mPID, RTPROCWAIT_FLAGS_NOBLOCK, &status);
8356
8357 if (vrc != VERR_PROCESS_RUNNING)
8358 {
8359 if (RT_SUCCESS(vrc) && status.enmReason == RTPROCEXITREASON_NORMAL)
8360 rc = setError(E_FAIL,
8361 tr("The virtual machine '%s' has terminated unexpectedly during startup with exit code %d"),
8362 getName().c_str(), status.iStatus);
8363 else if (RT_SUCCESS(vrc) && status.enmReason == RTPROCEXITREASON_SIGNAL)
8364 rc = setError(E_FAIL,
8365 tr("The virtual machine '%s' has terminated unexpectedly during startup because of signal %d"),
8366 getName().c_str(), status.iStatus);
8367 else if (RT_SUCCESS(vrc) && status.enmReason == RTPROCEXITREASON_ABEND)
8368 rc = setError(E_FAIL,
8369 tr("The virtual machine '%s' has terminated abnormally"),
8370 getName().c_str(), status.iStatus);
8371 else
8372 rc = setError(E_FAIL,
8373 tr("The virtual machine '%s' has terminated unexpectedly during startup (%Rrc)"),
8374 getName().c_str(), vrc);
8375 }
8376
8377 if (FAILED(rc))
8378 {
8379 /* Close the remote session, remove the remote control from the list
8380 * and reset session state to Closed (@note keep the code in sync with
8381 * the relevant part in LockMachine()). */
8382
8383 Assert(mData->mSession.mRemoteControls.size() == 1);
8384 if (mData->mSession.mRemoteControls.size() == 1)
8385 {
8386 ErrorInfoKeeper eik;
8387 mData->mSession.mRemoteControls.front()->Uninitialize();
8388 }
8389
8390 mData->mSession.mRemoteControls.clear();
8391 mData->mSession.mState = SessionState_Unlocked;
8392
8393 /* finalize the progress after setting the state */
8394 if (!mData->mSession.mProgress.isNull())
8395 {
8396 mData->mSession.mProgress->notifyComplete(rc);
8397 mData->mSession.mProgress.setNull();
8398 }
8399
8400 mData->mSession.mPID = NIL_RTPROCESS;
8401
8402 mParent->i_onSessionStateChange(mData->mUuid, SessionState_Unlocked);
8403 return true;
8404 }
8405
8406 return false;
8407}
8408
8409/**
8410 * Checks whether the machine can be registered. If so, commits and saves
8411 * all settings.
8412 *
8413 * @note Must be called from mParent's write lock. Locks this object and
8414 * children for writing.
8415 */
8416HRESULT Machine::prepareRegister()
8417{
8418 AssertReturn(mParent->isWriteLockOnCurrentThread(), E_FAIL);
8419
8420 AutoLimitedCaller autoCaller(this);
8421 AssertComRCReturnRC(autoCaller.rc());
8422
8423 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
8424
8425 /* wait for state dependents to drop to zero */
8426 ensureNoStateDependencies();
8427
8428 if (!mData->mAccessible)
8429 return setError(VBOX_E_INVALID_OBJECT_STATE,
8430 tr("The machine '%s' with UUID {%s} is inaccessible and cannot be registered"),
8431 mUserData->s.strName.c_str(),
8432 mData->mUuid.toString().c_str());
8433
8434 AssertReturn(autoCaller.state() == Ready, E_FAIL);
8435
8436 if (mData->mRegistered)
8437 return setError(VBOX_E_INVALID_OBJECT_STATE,
8438 tr("The machine '%s' with UUID {%s} is already registered"),
8439 mUserData->s.strName.c_str(),
8440 mData->mUuid.toString().c_str());
8441
8442 HRESULT rc = S_OK;
8443
8444 // Ensure the settings are saved. If we are going to be registered and
8445 // no config file exists yet, create it by calling saveSettings() too.
8446 if ( (mData->flModifications)
8447 || (!mData->pMachineConfigFile->fileExists())
8448 )
8449 {
8450 rc = saveSettings(NULL);
8451 // no need to check whether VirtualBox.xml needs saving too since
8452 // we can't have a machine XML file rename pending
8453 if (FAILED(rc)) return rc;
8454 }
8455
8456 /* more config checking goes here */
8457
8458 if (SUCCEEDED(rc))
8459 {
8460 /* we may have had implicit modifications we want to fix on success */
8461 commit();
8462
8463 mData->mRegistered = true;
8464 }
8465 else
8466 {
8467 /* we may have had implicit modifications we want to cancel on failure*/
8468 rollback(false /* aNotify */);
8469 }
8470
8471 return rc;
8472}
8473
8474/**
8475 * Increases the number of objects dependent on the machine state or on the
8476 * registered state. Guarantees that these two states will not change at least
8477 * until #releaseStateDependency() is called.
8478 *
8479 * Depending on the @a aDepType value, additional state checks may be made.
8480 * These checks will set extended error info on failure. See
8481 * #checkStateDependency() for more info.
8482 *
8483 * If this method returns a failure, the dependency is not added and the caller
8484 * is not allowed to rely on any particular machine state or registration state
8485 * value and may return the failed result code to the upper level.
8486 *
8487 * @param aDepType Dependency type to add.
8488 * @param aState Current machine state (NULL if not interested).
8489 * @param aRegistered Current registered state (NULL if not interested).
8490 *
8491 * @note Locks this object for writing.
8492 */
8493HRESULT Machine::addStateDependency(StateDependency aDepType /* = AnyStateDep */,
8494 MachineState_T *aState /* = NULL */,
8495 BOOL *aRegistered /* = NULL */)
8496{
8497 AutoCaller autoCaller(this);
8498 AssertComRCReturnRC(autoCaller.rc());
8499
8500 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
8501
8502 HRESULT rc = checkStateDependency(aDepType);
8503 if (FAILED(rc)) return rc;
8504
8505 {
8506 if (mData->mMachineStateChangePending != 0)
8507 {
8508 /* ensureNoStateDependencies() is waiting for state dependencies to
8509 * drop to zero so don't add more. It may make sense to wait a bit
8510 * and retry before reporting an error (since the pending state
8511 * transition should be really quick) but let's just assert for
8512 * now to see if it ever happens on practice. */
8513
8514 AssertFailed();
8515
8516 return setError(E_ACCESSDENIED,
8517 tr("Machine state change is in progress. Please retry the operation later."));
8518 }
8519
8520 ++mData->mMachineStateDeps;
8521 Assert(mData->mMachineStateDeps != 0 /* overflow */);
8522 }
8523
8524 if (aState)
8525 *aState = mData->mMachineState;
8526 if (aRegistered)
8527 *aRegistered = mData->mRegistered;
8528
8529 return S_OK;
8530}
8531
8532/**
8533 * Decreases the number of objects dependent on the machine state.
8534 * Must always complete the #addStateDependency() call after the state
8535 * dependency is no more necessary.
8536 */
8537void Machine::releaseStateDependency()
8538{
8539 AutoCaller autoCaller(this);
8540 AssertComRCReturnVoid(autoCaller.rc());
8541
8542 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
8543
8544 /* releaseStateDependency() w/o addStateDependency()? */
8545 AssertReturnVoid(mData->mMachineStateDeps != 0);
8546 -- mData->mMachineStateDeps;
8547
8548 if (mData->mMachineStateDeps == 0)
8549 {
8550 /* inform ensureNoStateDependencies() that there are no more deps */
8551 if (mData->mMachineStateChangePending != 0)
8552 {
8553 Assert(mData->mMachineStateDepsSem != NIL_RTSEMEVENTMULTI);
8554 RTSemEventMultiSignal (mData->mMachineStateDepsSem);
8555 }
8556 }
8557}
8558
8559Utf8Str Machine::getExtraData(const Utf8Str &strKey)
8560{
8561 /* start with nothing found */
8562 Utf8Str strResult("");
8563
8564 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
8565
8566 settings::StringsMap::const_iterator it = mData->pMachineConfigFile->mapExtraDataItems.find(strKey);
8567 if (it != mData->pMachineConfigFile->mapExtraDataItems.end())
8568 // found:
8569 strResult = it->second; // source is a Utf8Str
8570
8571 return strResult;
8572}
8573
8574// protected methods
8575/////////////////////////////////////////////////////////////////////////////
8576
8577/**
8578 * Performs machine state checks based on the @a aDepType value. If a check
8579 * fails, this method will set extended error info, otherwise it will return
8580 * S_OK. It is supposed, that on failure, the caller will immediately return
8581 * the return value of this method to the upper level.
8582 *
8583 * When @a aDepType is AnyStateDep, this method always returns S_OK.
8584 *
8585 * When @a aDepType is MutableStateDep, this method returns S_OK only if the
8586 * current state of this machine object allows to change settings of the
8587 * machine (i.e. the machine is not registered, or registered but not running
8588 * and not saved). It is useful to call this method from Machine setters
8589 * before performing any change.
8590 *
8591 * When @a aDepType is MutableOrSavedStateDep, this method behaves the same
8592 * as for MutableStateDep except that if the machine is saved, S_OK is also
8593 * returned. This is useful in setters which allow changing machine
8594 * properties when it is in the saved state.
8595 *
8596 * When @a aDepType is OfflineStateDep, this method returns S_OK if the
8597 * state is one of the 4 offline states (PoweredOff, Saved, Teleported,
8598 * Aborted).
8599 *
8600 * @param aDepType Dependency type to check.
8601 *
8602 * @note Non Machine based classes should use #addStateDependency() and
8603 * #releaseStateDependency() methods or the smart AutoStateDependency
8604 * template.
8605 *
8606 * @note This method must be called from under this object's read or write
8607 * lock.
8608 */
8609HRESULT Machine::checkStateDependency(StateDependency aDepType)
8610{
8611 switch (aDepType)
8612 {
8613 case AnyStateDep:
8614 {
8615 break;
8616 }
8617 case MutableStateDep:
8618 {
8619 if ( mData->mRegistered
8620 && ( !isSessionMachine() /** @todo This was just converted raw; Check if Running and Paused should actually be included here... (Live Migration) */
8621 || ( mData->mMachineState != MachineState_Paused
8622 && mData->mMachineState != MachineState_Running
8623 && mData->mMachineState != MachineState_Aborted
8624 && mData->mMachineState != MachineState_Teleported
8625 && mData->mMachineState != MachineState_PoweredOff
8626 )
8627 )
8628 )
8629 return setError(VBOX_E_INVALID_VM_STATE,
8630 tr("The machine is not mutable (state is %s)"),
8631 Global::stringifyMachineState(mData->mMachineState));
8632 break;
8633 }
8634 case MutableOrSavedStateDep:
8635 {
8636 if ( mData->mRegistered
8637 && ( !isSessionMachine() /** @todo This was just converted raw; Check if Running and Paused should actually be included here... (Live Migration) */
8638 || ( mData->mMachineState != MachineState_Paused
8639 && mData->mMachineState != MachineState_Running
8640 && mData->mMachineState != MachineState_Aborted
8641 && mData->mMachineState != MachineState_Teleported
8642 && mData->mMachineState != MachineState_Saved
8643 && mData->mMachineState != MachineState_PoweredOff
8644 )
8645 )
8646 )
8647 return setError(VBOX_E_INVALID_VM_STATE,
8648 tr("The machine is not mutable (state is %s)"),
8649 Global::stringifyMachineState(mData->mMachineState));
8650 break;
8651 }
8652 case OfflineStateDep:
8653 {
8654 if ( mData->mRegistered
8655 && ( !isSessionMachine()
8656 || ( mData->mMachineState != MachineState_PoweredOff
8657 && mData->mMachineState != MachineState_Saved
8658 && mData->mMachineState != MachineState_Aborted
8659 && mData->mMachineState != MachineState_Teleported
8660 )
8661 )
8662 )
8663 return setError(VBOX_E_INVALID_VM_STATE,
8664 tr("The machine is not offline (state is %s)"),
8665 Global::stringifyMachineState(mData->mMachineState));
8666 break;
8667 }
8668 }
8669
8670 return S_OK;
8671}
8672
8673/**
8674 * Helper to initialize all associated child objects and allocate data
8675 * structures.
8676 *
8677 * This method must be called as a part of the object's initialization procedure
8678 * (usually done in the #init() method).
8679 *
8680 * @note Must be called only from #init() or from #registeredInit().
8681 */
8682HRESULT Machine::initDataAndChildObjects()
8683{
8684 AutoCaller autoCaller(this);
8685 AssertComRCReturnRC(autoCaller.rc());
8686 AssertComRCReturn(autoCaller.state() == InInit ||
8687 autoCaller.state() == Limited, E_FAIL);
8688
8689 AssertReturn(!mData->mAccessible, E_FAIL);
8690
8691 /* allocate data structures */
8692 mSSData.allocate();
8693 mUserData.allocate();
8694 mHWData.allocate();
8695 mMediaData.allocate();
8696 mStorageControllers.allocate();
8697 mUSBControllers.allocate();
8698
8699 /* initialize mOSTypeId */
8700 mUserData->s.strOsType = mParent->i_getUnknownOSType()->i_id();
8701
8702 /* create associated BIOS settings object */
8703 unconst(mBIOSSettings).createObject();
8704 mBIOSSettings->init(this);
8705
8706 /* create an associated VRDE object (default is disabled) */
8707 unconst(mVRDEServer).createObject();
8708 mVRDEServer->init(this);
8709
8710 /* create associated serial port objects */
8711 for (ULONG slot = 0; slot < RT_ELEMENTS(mSerialPorts); slot++)
8712 {
8713 unconst(mSerialPorts[slot]).createObject();
8714 mSerialPorts[slot]->init(this, slot);
8715 }
8716
8717 /* create associated parallel port objects */
8718 for (ULONG slot = 0; slot < RT_ELEMENTS(mParallelPorts); slot++)
8719 {
8720 unconst(mParallelPorts[slot]).createObject();
8721 mParallelPorts[slot]->init(this, slot);
8722 }
8723
8724 /* create the audio adapter object (always present, default is disabled) */
8725 unconst(mAudioAdapter).createObject();
8726 mAudioAdapter->init(this);
8727
8728 /* create the USB device filters object (always present) */
8729 unconst(mUSBDeviceFilters).createObject();
8730 mUSBDeviceFilters->init(this);
8731
8732 /* create associated network adapter objects */
8733 mNetworkAdapters.resize(Global::getMaxNetworkAdapters(mHWData->mChipsetType));
8734 for (ULONG slot = 0; slot < mNetworkAdapters.size(); slot++)
8735 {
8736 unconst(mNetworkAdapters[slot]).createObject();
8737 mNetworkAdapters[slot]->init(this, slot);
8738 }
8739
8740 /* create the bandwidth control */
8741 unconst(mBandwidthControl).createObject();
8742 mBandwidthControl->init(this);
8743
8744 return S_OK;
8745}
8746
8747/**
8748 * Helper to uninitialize all associated child objects and to free all data
8749 * structures.
8750 *
8751 * This method must be called as a part of the object's uninitialization
8752 * procedure (usually done in the #uninit() method).
8753 *
8754 * @note Must be called only from #uninit() or from #registeredInit().
8755 */
8756void Machine::uninitDataAndChildObjects()
8757{
8758 AutoCaller autoCaller(this);
8759 AssertComRCReturnVoid(autoCaller.rc());
8760 AssertComRCReturnVoid( autoCaller.state() == InUninit
8761 || autoCaller.state() == Limited);
8762
8763 /* tell all our other child objects we've been uninitialized */
8764 if (mBandwidthControl)
8765 {
8766 mBandwidthControl->uninit();
8767 unconst(mBandwidthControl).setNull();
8768 }
8769
8770 for (ULONG slot = 0; slot < mNetworkAdapters.size(); slot++)
8771 {
8772 if (mNetworkAdapters[slot])
8773 {
8774 mNetworkAdapters[slot]->uninit();
8775 unconst(mNetworkAdapters[slot]).setNull();
8776 }
8777 }
8778
8779 if (mUSBDeviceFilters)
8780 {
8781 mUSBDeviceFilters->uninit();
8782 unconst(mUSBDeviceFilters).setNull();
8783 }
8784
8785 if (mAudioAdapter)
8786 {
8787 mAudioAdapter->uninit();
8788 unconst(mAudioAdapter).setNull();
8789 }
8790
8791 for (ULONG slot = 0; slot < RT_ELEMENTS(mParallelPorts); slot++)
8792 {
8793 if (mParallelPorts[slot])
8794 {
8795 mParallelPorts[slot]->uninit();
8796 unconst(mParallelPorts[slot]).setNull();
8797 }
8798 }
8799
8800 for (ULONG slot = 0; slot < RT_ELEMENTS(mSerialPorts); slot++)
8801 {
8802 if (mSerialPorts[slot])
8803 {
8804 mSerialPorts[slot]->uninit();
8805 unconst(mSerialPorts[slot]).setNull();
8806 }
8807 }
8808
8809 if (mVRDEServer)
8810 {
8811 mVRDEServer->uninit();
8812 unconst(mVRDEServer).setNull();
8813 }
8814
8815 if (mBIOSSettings)
8816 {
8817 mBIOSSettings->uninit();
8818 unconst(mBIOSSettings).setNull();
8819 }
8820
8821 /* Deassociate media (only when a real Machine or a SnapshotMachine
8822 * instance is uninitialized; SessionMachine instances refer to real
8823 * Machine media). This is necessary for a clean re-initialization of
8824 * the VM after successfully re-checking the accessibility state. Note
8825 * that in case of normal Machine or SnapshotMachine uninitialization (as
8826 * a result of unregistering or deleting the snapshot), outdated media
8827 * attachments will already be uninitialized and deleted, so this
8828 * code will not affect them. */
8829 if ( !!mMediaData
8830 && (!isSessionMachine())
8831 )
8832 {
8833 for (MediaData::AttachmentList::const_iterator it = mMediaData->mAttachments.begin();
8834 it != mMediaData->mAttachments.end();
8835 ++it)
8836 {
8837 ComObjPtr<Medium> pMedium = (*it)->i_getMedium();
8838 if (pMedium.isNull())
8839 continue;
8840 HRESULT rc = pMedium->i_removeBackReference(mData->mUuid, getSnapshotId());
8841 AssertComRC(rc);
8842 }
8843 }
8844
8845 if (!isSessionMachine() && !isSnapshotMachine())
8846 {
8847 // clean up the snapshots list (Snapshot::uninit() will handle the snapshot's children recursively)
8848 if (mData->mFirstSnapshot)
8849 {
8850 // snapshots tree is protected by machine write lock; strictly
8851 // this isn't necessary here since we're deleting the entire
8852 // machine, but otherwise we assert in Snapshot::uninit()
8853 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
8854 mData->mFirstSnapshot->uninit();
8855 mData->mFirstSnapshot.setNull();
8856 }
8857
8858 mData->mCurrentSnapshot.setNull();
8859 }
8860
8861 /* free data structures (the essential mData structure is not freed here
8862 * since it may be still in use) */
8863 mMediaData.free();
8864 mStorageControllers.free();
8865 mUSBControllers.free();
8866 mHWData.free();
8867 mUserData.free();
8868 mSSData.free();
8869}
8870
8871/**
8872 * Returns a pointer to the Machine object for this machine that acts like a
8873 * parent for complex machine data objects such as shared folders, etc.
8874 *
8875 * For primary Machine objects and for SnapshotMachine objects, returns this
8876 * object's pointer itself. For SessionMachine objects, returns the peer
8877 * (primary) machine pointer.
8878 */
8879Machine* Machine::getMachine()
8880{
8881 if (isSessionMachine())
8882 return (Machine*)mPeer;
8883 return this;
8884}
8885
8886/**
8887 * Makes sure that there are no machine state dependents. If necessary, waits
8888 * for the number of dependents to drop to zero.
8889 *
8890 * Make sure this method is called from under this object's write lock to
8891 * guarantee that no new dependents may be added when this method returns
8892 * control to the caller.
8893 *
8894 * @note Locks this object for writing. The lock will be released while waiting
8895 * (if necessary).
8896 *
8897 * @warning To be used only in methods that change the machine state!
8898 */
8899void Machine::ensureNoStateDependencies()
8900{
8901 AssertReturnVoid(isWriteLockOnCurrentThread());
8902
8903 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
8904
8905 /* Wait for all state dependents if necessary */
8906 if (mData->mMachineStateDeps != 0)
8907 {
8908 /* lazy semaphore creation */
8909 if (mData->mMachineStateDepsSem == NIL_RTSEMEVENTMULTI)
8910 RTSemEventMultiCreate(&mData->mMachineStateDepsSem);
8911
8912 LogFlowThisFunc(("Waiting for state deps (%d) to drop to zero...\n",
8913 mData->mMachineStateDeps));
8914
8915 ++mData->mMachineStateChangePending;
8916
8917 /* reset the semaphore before waiting, the last dependent will signal
8918 * it */
8919 RTSemEventMultiReset(mData->mMachineStateDepsSem);
8920
8921 alock.release();
8922
8923 RTSemEventMultiWait(mData->mMachineStateDepsSem, RT_INDEFINITE_WAIT);
8924
8925 alock.acquire();
8926
8927 -- mData->mMachineStateChangePending;
8928 }
8929}
8930
8931/**
8932 * Changes the machine state and informs callbacks.
8933 *
8934 * This method is not intended to fail so it either returns S_OK or asserts (and
8935 * returns a failure).
8936 *
8937 * @note Locks this object for writing.
8938 */
8939HRESULT Machine::setMachineState(MachineState_T aMachineState)
8940{
8941 LogFlowThisFuncEnter();
8942 LogFlowThisFunc(("aMachineState=%s\n", Global::stringifyMachineState(aMachineState) ));
8943
8944 AutoCaller autoCaller(this);
8945 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
8946
8947 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
8948
8949 /* wait for state dependents to drop to zero */
8950 ensureNoStateDependencies();
8951
8952 if (mData->mMachineState != aMachineState)
8953 {
8954 mData->mMachineState = aMachineState;
8955
8956 RTTimeNow(&mData->mLastStateChange);
8957
8958 mParent->i_onMachineStateChange(mData->mUuid, aMachineState);
8959 }
8960
8961 LogFlowThisFuncLeave();
8962 return S_OK;
8963}
8964
8965/**
8966 * Searches for a shared folder with the given logical name
8967 * in the collection of shared folders.
8968 *
8969 * @param aName logical name of the shared folder
8970 * @param aSharedFolder where to return the found object
8971 * @param aSetError whether to set the error info if the folder is
8972 * not found
8973 * @return
8974 * S_OK when found or VBOX_E_OBJECT_NOT_FOUND when not found
8975 *
8976 * @note
8977 * must be called from under the object's lock!
8978 */
8979HRESULT Machine::findSharedFolder(const Utf8Str &aName,
8980 ComObjPtr<SharedFolder> &aSharedFolder,
8981 bool aSetError /* = false */)
8982{
8983 HRESULT rc = VBOX_E_OBJECT_NOT_FOUND;
8984 for (HWData::SharedFolderList::const_iterator it = mHWData->mSharedFolders.begin();
8985 it != mHWData->mSharedFolders.end();
8986 ++it)
8987 {
8988 SharedFolder *pSF = *it;
8989 AutoCaller autoCaller(pSF);
8990 if (pSF->getName() == aName)
8991 {
8992 aSharedFolder = pSF;
8993 rc = S_OK;
8994 break;
8995 }
8996 }
8997
8998 if (aSetError && FAILED(rc))
8999 setError(rc, tr("Could not find a shared folder named '%s'"), aName.c_str());
9000
9001 return rc;
9002}
9003
9004/**
9005 * Initializes all machine instance data from the given settings structures
9006 * from XML. The exception is the machine UUID which needs special handling
9007 * depending on the caller's use case, so the caller needs to set that herself.
9008 *
9009 * This gets called in several contexts during machine initialization:
9010 *
9011 * -- When machine XML exists on disk already and needs to be loaded into memory,
9012 * for example, from registeredInit() to load all registered machines on
9013 * VirtualBox startup. In this case, puuidRegistry is NULL because the media
9014 * attached to the machine should be part of some media registry already.
9015 *
9016 * -- During OVF import, when a machine config has been constructed from an
9017 * OVF file. In this case, puuidRegistry is set to the machine UUID to
9018 * ensure that the media listed as attachments in the config (which have
9019 * been imported from the OVF) receive the correct registry ID.
9020 *
9021 * -- During VM cloning.
9022 *
9023 * @param config Machine settings from XML.
9024 * @param puuidRegistry If != NULL, Medium::setRegistryIdIfFirst() gets called with this registry ID for each attached medium in the config.
9025 * @return
9026 */
9027HRESULT Machine::loadMachineDataFromSettings(const settings::MachineConfigFile &config,
9028 const Guid *puuidRegistry)
9029{
9030 // copy name, description, OS type, teleporter, UTC etc.
9031 mUserData->s = config.machineUserData;
9032
9033 // Decode the Icon overide data from config userdata and set onto Machine.
9034 #define DECODE_STR_MAX _1M
9035 const char* pszStr = config.machineUserData.ovIcon.c_str();
9036 ssize_t cbOut = RTBase64DecodedSize(pszStr, NULL);
9037 if (cbOut > DECODE_STR_MAX)
9038 return setError(E_FAIL,
9039 tr("Icon Data too long.'%d' > '%d'"),
9040 cbOut,
9041 DECODE_STR_MAX);
9042 com::SafeArray<BYTE> iconByte(cbOut);
9043 HRESULT rc = RTBase64Decode(pszStr, iconByte.raw(), cbOut, NULL, NULL);
9044 if (FAILED(rc))
9045 return setError(E_FAIL,
9046 tr("Failure to Decode Icon Data. '%s' (%d)"),
9047 pszStr,
9048 rc);
9049 mUserData->mIcon.resize(iconByte.size());
9050 memcpy(&mUserData->mIcon[0], iconByte.raw(), mUserData->mIcon.size());
9051
9052 // look up the object by Id to check it is valid
9053 ComPtr<IGuestOSType> guestOSType;
9054 rc = mParent->GetGuestOSType(Bstr(mUserData->s.strOsType).raw(),
9055 guestOSType.asOutParam());
9056 if (FAILED(rc)) return rc;
9057
9058 // stateFile (optional)
9059 if (config.strStateFile.isEmpty())
9060 mSSData->strStateFilePath.setNull();
9061 else
9062 {
9063 Utf8Str stateFilePathFull(config.strStateFile);
9064 int vrc = calculateFullPath(stateFilePathFull, stateFilePathFull);
9065 if (RT_FAILURE(vrc))
9066 return setError(E_FAIL,
9067 tr("Invalid saved state file path '%s' (%Rrc)"),
9068 config.strStateFile.c_str(),
9069 vrc);
9070 mSSData->strStateFilePath = stateFilePathFull;
9071 }
9072
9073 // snapshot folder needs special processing so set it again
9074 rc = COMSETTER(SnapshotFolder)(Bstr(config.machineUserData.strSnapshotFolder).raw());
9075 if (FAILED(rc)) return rc;
9076
9077 /* Copy the extra data items (Not in any case config is already the same as
9078 * mData->pMachineConfigFile, like when the xml files are read from disk. So
9079 * make sure the extra data map is copied). */
9080 mData->pMachineConfigFile->mapExtraDataItems = config.mapExtraDataItems;
9081
9082 /* currentStateModified (optional, default is true) */
9083 mData->mCurrentStateModified = config.fCurrentStateModified;
9084
9085 mData->mLastStateChange = config.timeLastStateChange;
9086
9087 /*
9088 * note: all mUserData members must be assigned prior this point because
9089 * we need to commit changes in order to let mUserData be shared by all
9090 * snapshot machine instances.
9091 */
9092 mUserData.commitCopy();
9093
9094 // machine registry, if present (must be loaded before snapshots)
9095 if (config.canHaveOwnMediaRegistry())
9096 {
9097 // determine machine folder
9098 Utf8Str strMachineFolder = getSettingsFileFull();
9099 strMachineFolder.stripFilename();
9100 rc = mParent->initMedia(getId(), // media registry ID == machine UUID
9101 config.mediaRegistry,
9102 strMachineFolder);
9103 if (FAILED(rc)) return rc;
9104 }
9105
9106 /* Snapshot node (optional) */
9107 size_t cRootSnapshots;
9108 if ((cRootSnapshots = config.llFirstSnapshot.size()))
9109 {
9110 // there must be only one root snapshot
9111 Assert(cRootSnapshots == 1);
9112
9113 const settings::Snapshot &snap = config.llFirstSnapshot.front();
9114
9115 rc = loadSnapshot(snap,
9116 config.uuidCurrentSnapshot,
9117 NULL); // no parent == first snapshot
9118 if (FAILED(rc)) return rc;
9119 }
9120
9121 // hardware data
9122 rc = loadHardware(config.hardwareMachine, &config.debugging, &config.autostart);
9123 if (FAILED(rc)) return rc;
9124
9125 // load storage controllers
9126 rc = loadStorageControllers(config.storageMachine,
9127 puuidRegistry,
9128 NULL /* puuidSnapshot */);
9129 if (FAILED(rc)) return rc;
9130
9131 /*
9132 * NOTE: the assignment below must be the last thing to do,
9133 * otherwise it will be not possible to change the settings
9134 * somewhere in the code above because all setters will be
9135 * blocked by checkStateDependency(MutableStateDep).
9136 */
9137
9138 /* set the machine state to Aborted or Saved when appropriate */
9139 if (config.fAborted)
9140 {
9141 mSSData->strStateFilePath.setNull();
9142
9143 /* no need to use setMachineState() during init() */
9144 mData->mMachineState = MachineState_Aborted;
9145 }
9146 else if (!mSSData->strStateFilePath.isEmpty())
9147 {
9148 /* no need to use setMachineState() during init() */
9149 mData->mMachineState = MachineState_Saved;
9150 }
9151
9152 // after loading settings, we are no longer different from the XML on disk
9153 mData->flModifications = 0;
9154
9155 return S_OK;
9156}
9157
9158/**
9159 * Recursively loads all snapshots starting from the given.
9160 *
9161 * @param aNode <Snapshot> node.
9162 * @param aCurSnapshotId Current snapshot ID from the settings file.
9163 * @param aParentSnapshot Parent snapshot.
9164 */
9165HRESULT Machine::loadSnapshot(const settings::Snapshot &data,
9166 const Guid &aCurSnapshotId,
9167 Snapshot *aParentSnapshot)
9168{
9169 AssertReturn(!isSnapshotMachine(), E_FAIL);
9170 AssertReturn(!isSessionMachine(), E_FAIL);
9171
9172 HRESULT rc = S_OK;
9173
9174 Utf8Str strStateFile;
9175 if (!data.strStateFile.isEmpty())
9176 {
9177 /* optional */
9178 strStateFile = data.strStateFile;
9179 int vrc = calculateFullPath(strStateFile, strStateFile);
9180 if (RT_FAILURE(vrc))
9181 return setError(E_FAIL,
9182 tr("Invalid saved state file path '%s' (%Rrc)"),
9183 strStateFile.c_str(),
9184 vrc);
9185 }
9186
9187 /* create a snapshot machine object */
9188 ComObjPtr<SnapshotMachine> pSnapshotMachine;
9189 pSnapshotMachine.createObject();
9190 rc = pSnapshotMachine->initFromSettings(this,
9191 data.hardware,
9192 &data.debugging,
9193 &data.autostart,
9194 data.storage,
9195 data.uuid.ref(),
9196 strStateFile);
9197 if (FAILED(rc)) return rc;
9198
9199 /* create a snapshot object */
9200 ComObjPtr<Snapshot> pSnapshot;
9201 pSnapshot.createObject();
9202 /* initialize the snapshot */
9203 rc = pSnapshot->init(mParent, // VirtualBox object
9204 data.uuid,
9205 data.strName,
9206 data.strDescription,
9207 data.timestamp,
9208 pSnapshotMachine,
9209 aParentSnapshot);
9210 if (FAILED(rc)) return rc;
9211
9212 /* memorize the first snapshot if necessary */
9213 if (!mData->mFirstSnapshot)
9214 mData->mFirstSnapshot = pSnapshot;
9215
9216 /* memorize the current snapshot when appropriate */
9217 if ( !mData->mCurrentSnapshot
9218 && pSnapshot->i_getId() == aCurSnapshotId
9219 )
9220 mData->mCurrentSnapshot = pSnapshot;
9221
9222 // now create the children
9223 for (settings::SnapshotsList::const_iterator it = data.llChildSnapshots.begin();
9224 it != data.llChildSnapshots.end();
9225 ++it)
9226 {
9227 const settings::Snapshot &childData = *it;
9228 // recurse
9229 rc = loadSnapshot(childData,
9230 aCurSnapshotId,
9231 pSnapshot); // parent = the one we created above
9232 if (FAILED(rc)) return rc;
9233 }
9234
9235 return rc;
9236}
9237
9238/**
9239 * Loads settings into mHWData.
9240 *
9241 * @param data Reference to the hardware settings.
9242 * @param pDbg Pointer to the debugging settings.
9243 * @param pAutostart Pointer to the autostart settings.
9244 */
9245HRESULT Machine::loadHardware(const settings::Hardware &data, const settings::Debugging *pDbg,
9246 const settings::Autostart *pAutostart)
9247{
9248 AssertReturn(!isSessionMachine(), E_FAIL);
9249
9250 HRESULT rc = S_OK;
9251
9252 try
9253 {
9254 /* The hardware version attribute (optional). */
9255 mHWData->mHWVersion = data.strVersion;
9256 mHWData->mHardwareUUID = data.uuid;
9257
9258 mHWData->mHWVirtExEnabled = data.fHardwareVirt;
9259 mHWData->mHWVirtExNestedPagingEnabled = data.fNestedPaging;
9260 mHWData->mHWVirtExLargePagesEnabled = data.fLargePages;
9261 mHWData->mHWVirtExVPIDEnabled = data.fVPID;
9262 mHWData->mHWVirtExUXEnabled = data.fUnrestrictedExecution;
9263 mHWData->mHWVirtExForceEnabled = data.fHardwareVirtForce;
9264 mHWData->mPAEEnabled = data.fPAE;
9265 mHWData->mSyntheticCpu = data.fSyntheticCpu;
9266 mHWData->mLongMode = data.enmLongMode;
9267 mHWData->mTripleFaultReset = data.fTripleFaultReset;
9268 mHWData->mCPUCount = data.cCPUs;
9269 mHWData->mCPUHotPlugEnabled = data.fCpuHotPlug;
9270 mHWData->mCpuExecutionCap = data.ulCpuExecutionCap;
9271
9272 // cpu
9273 if (mHWData->mCPUHotPlugEnabled)
9274 {
9275 for (settings::CpuList::const_iterator it = data.llCpus.begin();
9276 it != data.llCpus.end();
9277 ++it)
9278 {
9279 const settings::Cpu &cpu = *it;
9280
9281 mHWData->mCPUAttached[cpu.ulId] = true;
9282 }
9283 }
9284
9285 // cpuid leafs
9286 for (settings::CpuIdLeafsList::const_iterator it = data.llCpuIdLeafs.begin();
9287 it != data.llCpuIdLeafs.end();
9288 ++it)
9289 {
9290 const settings::CpuIdLeaf &leaf = *it;
9291
9292 switch (leaf.ulId)
9293 {
9294 case 0x0:
9295 case 0x1:
9296 case 0x2:
9297 case 0x3:
9298 case 0x4:
9299 case 0x5:
9300 case 0x6:
9301 case 0x7:
9302 case 0x8:
9303 case 0x9:
9304 case 0xA:
9305 mHWData->mCpuIdStdLeafs[leaf.ulId] = leaf;
9306 break;
9307
9308 case 0x80000000:
9309 case 0x80000001:
9310 case 0x80000002:
9311 case 0x80000003:
9312 case 0x80000004:
9313 case 0x80000005:
9314 case 0x80000006:
9315 case 0x80000007:
9316 case 0x80000008:
9317 case 0x80000009:
9318 case 0x8000000A:
9319 mHWData->mCpuIdExtLeafs[leaf.ulId - 0x80000000] = leaf;
9320 break;
9321
9322 default:
9323 /* just ignore */
9324 break;
9325 }
9326 }
9327
9328 mHWData->mMemorySize = data.ulMemorySizeMB;
9329 mHWData->mPageFusionEnabled = data.fPageFusionEnabled;
9330
9331 // boot order
9332 for (size_t i = 0;
9333 i < RT_ELEMENTS(mHWData->mBootOrder);
9334 i++)
9335 {
9336 settings::BootOrderMap::const_iterator it = data.mapBootOrder.find(i);
9337 if (it == data.mapBootOrder.end())
9338 mHWData->mBootOrder[i] = DeviceType_Null;
9339 else
9340 mHWData->mBootOrder[i] = it->second;
9341 }
9342
9343 mHWData->mGraphicsControllerType = data.graphicsControllerType;
9344 mHWData->mVRAMSize = data.ulVRAMSizeMB;
9345 mHWData->mMonitorCount = data.cMonitors;
9346 mHWData->mAccelerate3DEnabled = data.fAccelerate3D;
9347 mHWData->mAccelerate2DVideoEnabled = data.fAccelerate2DVideo;
9348 mHWData->mVideoCaptureWidth = data.ulVideoCaptureHorzRes;
9349 mHWData->mVideoCaptureHeight = data.ulVideoCaptureVertRes;
9350 mHWData->mVideoCaptureEnabled = data.fVideoCaptureEnabled;
9351 for (unsigned i = 0; i < RT_ELEMENTS(mHWData->maVideoCaptureScreens); i++)
9352 mHWData->maVideoCaptureScreens[i] = ASMBitTest(&data.u64VideoCaptureScreens, i);
9353 AssertCompile(RT_ELEMENTS(mHWData->maVideoCaptureScreens) == sizeof(data.u64VideoCaptureScreens) * 8);
9354 mHWData->mVideoCaptureRate = data.ulVideoCaptureRate;
9355 mHWData->mVideoCaptureFPS = data.ulVideoCaptureFPS;
9356 if (!data.strVideoCaptureFile.isEmpty())
9357 calculateFullPath(data.strVideoCaptureFile, mHWData->mVideoCaptureFile);
9358 else
9359 mHWData->mVideoCaptureFile.setNull();
9360 mHWData->mFirmwareType = data.firmwareType;
9361 mHWData->mPointingHIDType = data.pointingHIDType;
9362 mHWData->mKeyboardHIDType = data.keyboardHIDType;
9363 mHWData->mChipsetType = data.chipsetType;
9364 mHWData->mEmulatedUSBCardReaderEnabled = data.fEmulatedUSBCardReader;
9365 mHWData->mHPETEnabled = data.fHPETEnabled;
9366
9367 /* VRDEServer */
9368 rc = mVRDEServer->i_loadSettings(data.vrdeSettings);
9369 if (FAILED(rc)) return rc;
9370
9371 /* BIOS */
9372 rc = mBIOSSettings->i_loadSettings(data.biosSettings);
9373 if (FAILED(rc)) return rc;
9374
9375 // Bandwidth control (must come before network adapters)
9376 rc = mBandwidthControl->i_loadSettings(data.ioSettings);
9377 if (FAILED(rc)) return rc;
9378
9379 /* Shared folders */
9380 for (settings::USBControllerList::const_iterator it = data.usbSettings.llUSBControllers.begin();
9381 it != data.usbSettings.llUSBControllers.end();
9382 ++it)
9383 {
9384 const settings::USBController &settingsCtrl = *it;
9385 ComObjPtr<USBController> newCtrl;
9386
9387 newCtrl.createObject();
9388 newCtrl->init(this, settingsCtrl.strName, settingsCtrl.enmType);
9389 mUSBControllers->push_back(newCtrl);
9390 }
9391
9392 /* USB device filters */
9393 rc = mUSBDeviceFilters->i_loadSettings(data.usbSettings);
9394 if (FAILED(rc)) return rc;
9395
9396 // network adapters
9397 uint32_t newCount = Global::getMaxNetworkAdapters(mHWData->mChipsetType);
9398 uint32_t oldCount = mNetworkAdapters.size();
9399 if (newCount > oldCount)
9400 {
9401 mNetworkAdapters.resize(newCount);
9402 for (ULONG slot = oldCount; slot < mNetworkAdapters.size(); slot++)
9403 {
9404 unconst(mNetworkAdapters[slot]).createObject();
9405 mNetworkAdapters[slot]->init(this, slot);
9406 }
9407 }
9408 else if (newCount < oldCount)
9409 mNetworkAdapters.resize(newCount);
9410 for (settings::NetworkAdaptersList::const_iterator it = data.llNetworkAdapters.begin();
9411 it != data.llNetworkAdapters.end();
9412 ++it)
9413 {
9414 const settings::NetworkAdapter &nic = *it;
9415
9416 /* slot unicity is guaranteed by XML Schema */
9417 AssertBreak(nic.ulSlot < mNetworkAdapters.size());
9418 rc = mNetworkAdapters[nic.ulSlot]->i_loadSettings(mBandwidthControl, nic);
9419 if (FAILED(rc)) return rc;
9420 }
9421
9422 // serial ports
9423 for (settings::SerialPortsList::const_iterator it = data.llSerialPorts.begin();
9424 it != data.llSerialPorts.end();
9425 ++it)
9426 {
9427 const settings::SerialPort &s = *it;
9428
9429 AssertBreak(s.ulSlot < RT_ELEMENTS(mSerialPorts));
9430 rc = mSerialPorts[s.ulSlot]->i_loadSettings(s);
9431 if (FAILED(rc)) return rc;
9432 }
9433
9434 // parallel ports (optional)
9435 for (settings::ParallelPortsList::const_iterator it = data.llParallelPorts.begin();
9436 it != data.llParallelPorts.end();
9437 ++it)
9438 {
9439 const settings::ParallelPort &p = *it;
9440
9441 AssertBreak(p.ulSlot < RT_ELEMENTS(mParallelPorts));
9442 rc = mParallelPorts[p.ulSlot]->i_loadSettings(p);
9443 if (FAILED(rc)) return rc;
9444 }
9445
9446 /* AudioAdapter */
9447 rc = mAudioAdapter->i_loadSettings(data.audioAdapter);
9448 if (FAILED(rc)) return rc;
9449
9450 /* Shared folders */
9451 for (settings::SharedFoldersList::const_iterator it = data.llSharedFolders.begin();
9452 it != data.llSharedFolders.end();
9453 ++it)
9454 {
9455 const settings::SharedFolder &sf = *it;
9456
9457 ComObjPtr<SharedFolder> sharedFolder;
9458 /* Check for double entries. Not allowed! */
9459 rc = findSharedFolder(sf.strName, sharedFolder, false /* aSetError */);
9460 if (SUCCEEDED(rc))
9461 return setError(VBOX_E_OBJECT_IN_USE,
9462 tr("Shared folder named '%s' already exists"),
9463 sf.strName.c_str());
9464
9465 /* Create the new shared folder. Don't break on error. This will be
9466 * reported when the machine starts. */
9467 sharedFolder.createObject();
9468 rc = sharedFolder->init(getMachine(),
9469 sf.strName,
9470 sf.strHostPath,
9471 RT_BOOL(sf.fWritable),
9472 RT_BOOL(sf.fAutoMount),
9473 false /* fFailOnError */);
9474 if (FAILED(rc)) return rc;
9475 mHWData->mSharedFolders.push_back(sharedFolder);
9476 }
9477
9478 // Clipboard
9479 mHWData->mClipboardMode = data.clipboardMode;
9480
9481 // drag'n'drop
9482 mHWData->mDragAndDropMode = data.dragAndDropMode;
9483
9484 // guest settings
9485 mHWData->mMemoryBalloonSize = data.ulMemoryBalloonSize;
9486
9487 // IO settings
9488 mHWData->mIOCacheEnabled = data.ioSettings.fIOCacheEnabled;
9489 mHWData->mIOCacheSize = data.ioSettings.ulIOCacheSize;
9490
9491 // Host PCI devices
9492 for (settings::HostPCIDeviceAttachmentList::const_iterator it = data.pciAttachments.begin();
9493 it != data.pciAttachments.end();
9494 ++it)
9495 {
9496 const settings::HostPCIDeviceAttachment &hpda = *it;
9497 ComObjPtr<PCIDeviceAttachment> pda;
9498
9499 pda.createObject();
9500 pda->loadSettings(this, hpda);
9501 mHWData->mPCIDeviceAssignments.push_back(pda);
9502 }
9503
9504 /*
9505 * (The following isn't really real hardware, but it lives in HWData
9506 * for reasons of convenience.)
9507 */
9508
9509#ifdef VBOX_WITH_GUEST_PROPS
9510 /* Guest properties (optional) */
9511 for (settings::GuestPropertiesList::const_iterator it = data.llGuestProperties.begin();
9512 it != data.llGuestProperties.end();
9513 ++it)
9514 {
9515 const settings::GuestProperty &prop = *it;
9516 uint32_t fFlags = guestProp::NILFLAG;
9517 guestProp::validateFlags(prop.strFlags.c_str(), &fFlags);
9518 HWData::GuestProperty property = { prop.strValue, (LONG64) prop.timestamp, fFlags };
9519 mHWData->mGuestProperties[prop.strName] = property;
9520 }
9521
9522 mHWData->mGuestPropertyNotificationPatterns = data.strNotificationPatterns;
9523#endif /* VBOX_WITH_GUEST_PROPS defined */
9524
9525 rc = loadDebugging(pDbg);
9526 if (FAILED(rc))
9527 return rc;
9528
9529 mHWData->mAutostart = *pAutostart;
9530
9531 /* default frontend */
9532 mHWData->mDefaultFrontend = data.strDefaultFrontend;
9533 }
9534 catch(std::bad_alloc &)
9535 {
9536 return E_OUTOFMEMORY;
9537 }
9538
9539 AssertComRC(rc);
9540 return rc;
9541}
9542
9543/**
9544 * Called from Machine::loadHardware() to load the debugging settings of the
9545 * machine.
9546 *
9547 * @param pDbg Pointer to the settings.
9548 */
9549HRESULT Machine::loadDebugging(const settings::Debugging *pDbg)
9550{
9551 mHWData->mDebugging = *pDbg;
9552 /* no more processing currently required, this will probably change. */
9553 return S_OK;
9554}
9555
9556/**
9557 * Called from loadMachineDataFromSettings() for the storage controller data, including media.
9558 *
9559 * @param data
9560 * @param puuidRegistry media registry ID to set media to or NULL; see Machine::loadMachineDataFromSettings()
9561 * @param puuidSnapshot
9562 * @return
9563 */
9564HRESULT Machine::loadStorageControllers(const settings::Storage &data,
9565 const Guid *puuidRegistry,
9566 const Guid *puuidSnapshot)
9567{
9568 AssertReturn(!isSessionMachine(), E_FAIL);
9569
9570 HRESULT rc = S_OK;
9571
9572 for (settings::StorageControllersList::const_iterator it = data.llStorageControllers.begin();
9573 it != data.llStorageControllers.end();
9574 ++it)
9575 {
9576 const settings::StorageController &ctlData = *it;
9577
9578 ComObjPtr<StorageController> pCtl;
9579 /* Try to find one with the name first. */
9580 rc = getStorageControllerByName(ctlData.strName, pCtl, false /* aSetError */);
9581 if (SUCCEEDED(rc))
9582 return setError(VBOX_E_OBJECT_IN_USE,
9583 tr("Storage controller named '%s' already exists"),
9584 ctlData.strName.c_str());
9585
9586 pCtl.createObject();
9587 rc = pCtl->init(this,
9588 ctlData.strName,
9589 ctlData.storageBus,
9590 ctlData.ulInstance,
9591 ctlData.fBootable);
9592 if (FAILED(rc)) return rc;
9593
9594 mStorageControllers->push_back(pCtl);
9595
9596 rc = pCtl->COMSETTER(ControllerType)(ctlData.controllerType);
9597 if (FAILED(rc)) return rc;
9598
9599 rc = pCtl->COMSETTER(PortCount)(ctlData.ulPortCount);
9600 if (FAILED(rc)) return rc;
9601
9602 rc = pCtl->COMSETTER(UseHostIOCache)(ctlData.fUseHostIOCache);
9603 if (FAILED(rc)) return rc;
9604
9605 /* Set IDE emulation settings (only for AHCI controller). */
9606 if (ctlData.controllerType == StorageControllerType_IntelAhci)
9607 {
9608 if ( (FAILED(rc = pCtl->i_setIDEEmulationPort(0, ctlData.lIDE0MasterEmulationPort)))
9609 || (FAILED(rc = pCtl->i_setIDEEmulationPort(1, ctlData.lIDE0SlaveEmulationPort)))
9610 || (FAILED(rc = pCtl->i_setIDEEmulationPort(2, ctlData.lIDE1MasterEmulationPort)))
9611 || (FAILED(rc = pCtl->i_setIDEEmulationPort(3, ctlData.lIDE1SlaveEmulationPort)))
9612 )
9613 return rc;
9614 }
9615
9616 /* Load the attached devices now. */
9617 rc = loadStorageDevices(pCtl,
9618 ctlData,
9619 puuidRegistry,
9620 puuidSnapshot);
9621 if (FAILED(rc)) return rc;
9622 }
9623
9624 return S_OK;
9625}
9626
9627/**
9628 * Called from loadStorageControllers for a controller's devices.
9629 *
9630 * @param aStorageController
9631 * @param data
9632 * @param puuidRegistry media registry ID to set media to or NULL; see Machine::loadMachineDataFromSettings()
9633 * @param aSnapshotId pointer to the snapshot ID if this is a snapshot machine
9634 * @return
9635 */
9636HRESULT Machine::loadStorageDevices(StorageController *aStorageController,
9637 const settings::StorageController &data,
9638 const Guid *puuidRegistry,
9639 const Guid *puuidSnapshot)
9640{
9641 HRESULT rc = S_OK;
9642
9643 /* paranoia: detect duplicate attachments */
9644 for (settings::AttachedDevicesList::const_iterator it = data.llAttachedDevices.begin();
9645 it != data.llAttachedDevices.end();
9646 ++it)
9647 {
9648 const settings::AttachedDevice &ad = *it;
9649
9650 for (settings::AttachedDevicesList::const_iterator it2 = it;
9651 it2 != data.llAttachedDevices.end();
9652 ++it2)
9653 {
9654 if (it == it2)
9655 continue;
9656
9657 const settings::AttachedDevice &ad2 = *it2;
9658
9659 if ( ad.lPort == ad2.lPort
9660 && ad.lDevice == ad2.lDevice)
9661 {
9662 return setError(E_FAIL,
9663 tr("Duplicate attachments for storage controller '%s', port %d, device %d of the virtual machine '%s'"),
9664 aStorageController->i_getName().c_str(),
9665 ad.lPort,
9666 ad.lDevice,
9667 mUserData->s.strName.c_str());
9668 }
9669 }
9670 }
9671
9672 for (settings::AttachedDevicesList::const_iterator it = data.llAttachedDevices.begin();
9673 it != data.llAttachedDevices.end();
9674 ++it)
9675 {
9676 const settings::AttachedDevice &dev = *it;
9677 ComObjPtr<Medium> medium;
9678
9679 switch (dev.deviceType)
9680 {
9681 case DeviceType_Floppy:
9682 case DeviceType_DVD:
9683 if (dev.strHostDriveSrc.isNotEmpty())
9684 rc = mParent->i_host()->i_findHostDriveByName(dev.deviceType, dev.strHostDriveSrc, false /* fRefresh */, medium);
9685 else
9686 rc = mParent->i_findRemoveableMedium(dev.deviceType,
9687 dev.uuid,
9688 false /* fRefresh */,
9689 false /* aSetError */,
9690 medium);
9691 if (rc == VBOX_E_OBJECT_NOT_FOUND)
9692 // This is not an error. The host drive or UUID might have vanished, so just go ahead without this removeable medium attachment
9693 rc = S_OK;
9694 break;
9695
9696 case DeviceType_HardDisk:
9697 {
9698 /* find a hard disk by UUID */
9699 rc = mParent->i_findHardDiskById(dev.uuid, true /* aDoSetError */, &medium);
9700 if (FAILED(rc))
9701 {
9702 if (isSnapshotMachine())
9703 {
9704 // wrap another error message around the "cannot find hard disk" set by findHardDisk
9705 // so the user knows that the bad disk is in a snapshot somewhere
9706 com::ErrorInfo info;
9707 return setError(E_FAIL,
9708 tr("A differencing image of snapshot {%RTuuid} could not be found. %ls"),
9709 puuidSnapshot->raw(),
9710 info.getText().raw());
9711 }
9712 else
9713 return rc;
9714 }
9715
9716 AutoWriteLock hdLock(medium COMMA_LOCKVAL_SRC_POS);
9717
9718 if (medium->i_getType() == MediumType_Immutable)
9719 {
9720 if (isSnapshotMachine())
9721 return setError(E_FAIL,
9722 tr("Immutable hard disk '%s' with UUID {%RTuuid} cannot be directly attached to snapshot with UUID {%RTuuid} "
9723 "of the virtual machine '%s' ('%s')"),
9724 medium->i_getLocationFull().c_str(),
9725 dev.uuid.raw(),
9726 puuidSnapshot->raw(),
9727 mUserData->s.strName.c_str(),
9728 mData->m_strConfigFileFull.c_str());
9729
9730 return setError(E_FAIL,
9731 tr("Immutable hard disk '%s' with UUID {%RTuuid} cannot be directly attached to the virtual machine '%s' ('%s')"),
9732 medium->i_getLocationFull().c_str(),
9733 dev.uuid.raw(),
9734 mUserData->s.strName.c_str(),
9735 mData->m_strConfigFileFull.c_str());
9736 }
9737
9738 if (medium->i_getType() == MediumType_MultiAttach)
9739 {
9740 if (isSnapshotMachine())
9741 return setError(E_FAIL,
9742 tr("Multi-attach hard disk '%s' with UUID {%RTuuid} cannot be directly attached to snapshot with UUID {%RTuuid} "
9743 "of the virtual machine '%s' ('%s')"),
9744 medium->i_getLocationFull().c_str(),
9745 dev.uuid.raw(),
9746 puuidSnapshot->raw(),
9747 mUserData->s.strName.c_str(),
9748 mData->m_strConfigFileFull.c_str());
9749
9750 return setError(E_FAIL,
9751 tr("Multi-attach hard disk '%s' with UUID {%RTuuid} cannot be directly attached to the virtual machine '%s' ('%s')"),
9752 medium->i_getLocationFull().c_str(),
9753 dev.uuid.raw(),
9754 mUserData->s.strName.c_str(),
9755 mData->m_strConfigFileFull.c_str());
9756 }
9757
9758 if ( !isSnapshotMachine()
9759 && medium->i_getChildren().size() != 0
9760 )
9761 return setError(E_FAIL,
9762 tr("Hard disk '%s' with UUID {%RTuuid} cannot be directly attached to the virtual machine '%s' ('%s') "
9763 "because it has %d differencing child hard disks"),
9764 medium->i_getLocationFull().c_str(),
9765 dev.uuid.raw(),
9766 mUserData->s.strName.c_str(),
9767 mData->m_strConfigFileFull.c_str(),
9768 medium->i_getChildren().size());
9769
9770 if (findAttachment(mMediaData->mAttachments,
9771 medium))
9772 return setError(E_FAIL,
9773 tr("Hard disk '%s' with UUID {%RTuuid} is already attached to the virtual machine '%s' ('%s')"),
9774 medium->i_getLocationFull().c_str(),
9775 dev.uuid.raw(),
9776 mUserData->s.strName.c_str(),
9777 mData->m_strConfigFileFull.c_str());
9778
9779 break;
9780 }
9781
9782 default:
9783 return setError(E_FAIL,
9784 tr("Device '%s' with unknown type is attached to the virtual machine '%s' ('%s')"),
9785 medium->i_getLocationFull().c_str(),
9786 mUserData->s.strName.c_str(),
9787 mData->m_strConfigFileFull.c_str());
9788 }
9789
9790 if (FAILED(rc))
9791 break;
9792
9793 /* Bandwidth groups are loaded at this point. */
9794 ComObjPtr<BandwidthGroup> pBwGroup;
9795
9796 if (!dev.strBwGroup.isEmpty())
9797 {
9798 rc = mBandwidthControl->i_getBandwidthGroupByName(dev.strBwGroup, pBwGroup, false /* aSetError */);
9799 if (FAILED(rc))
9800 return setError(E_FAIL,
9801 tr("Device '%s' with unknown bandwidth group '%s' is attached to the virtual machine '%s' ('%s')"),
9802 medium->i_getLocationFull().c_str(),
9803 dev.strBwGroup.c_str(),
9804 mUserData->s.strName.c_str(),
9805 mData->m_strConfigFileFull.c_str());
9806 pBwGroup->i_reference();
9807 }
9808
9809 const Bstr controllerName = aStorageController->i_getName();
9810 ComObjPtr<MediumAttachment> pAttachment;
9811 pAttachment.createObject();
9812 rc = pAttachment->init(this,
9813 medium,
9814 controllerName,
9815 dev.lPort,
9816 dev.lDevice,
9817 dev.deviceType,
9818 false,
9819 dev.fPassThrough,
9820 dev.fTempEject,
9821 dev.fNonRotational,
9822 dev.fDiscard,
9823 dev.fHotPluggable,
9824 pBwGroup.isNull() ? Utf8Str::Empty : pBwGroup->i_getName());
9825 if (FAILED(rc)) break;
9826
9827 /* associate the medium with this machine and snapshot */
9828 if (!medium.isNull())
9829 {
9830 AutoCaller medCaller(medium);
9831 if (FAILED(medCaller.rc())) return medCaller.rc();
9832 AutoWriteLock mlock(medium COMMA_LOCKVAL_SRC_POS);
9833
9834 if (isSnapshotMachine())
9835 rc = medium->i_addBackReference(mData->mUuid, *puuidSnapshot);
9836 else
9837 rc = medium->i_addBackReference(mData->mUuid);
9838 /* If the medium->addBackReference fails it sets an appropriate
9839 * error message, so no need to do any guesswork here. */
9840
9841 if (puuidRegistry)
9842 // caller wants registry ID to be set on all attached media (OVF import case)
9843 medium->i_addRegistry(*puuidRegistry, false /* fRecurse */);
9844 }
9845
9846 if (FAILED(rc))
9847 break;
9848
9849 /* back up mMediaData to let registeredInit() properly rollback on failure
9850 * (= limited accessibility) */
9851 setModified(IsModified_Storage);
9852 mMediaData.backup();
9853 mMediaData->mAttachments.push_back(pAttachment);
9854 }
9855
9856 return rc;
9857}
9858
9859/**
9860 * Returns the snapshot with the given UUID or fails of no such snapshot exists.
9861 *
9862 * @param aId snapshot UUID to find (empty UUID refers the first snapshot)
9863 * @param aSnapshot where to return the found snapshot
9864 * @param aSetError true to set extended error info on failure
9865 */
9866HRESULT Machine::findSnapshotById(const Guid &aId,
9867 ComObjPtr<Snapshot> &aSnapshot,
9868 bool aSetError /* = false */)
9869{
9870 AutoReadLock chlock(this COMMA_LOCKVAL_SRC_POS);
9871
9872 if (!mData->mFirstSnapshot)
9873 {
9874 if (aSetError)
9875 return setError(E_FAIL, tr("This machine does not have any snapshots"));
9876 return E_FAIL;
9877 }
9878
9879 if (aId.isZero())
9880 aSnapshot = mData->mFirstSnapshot;
9881 else
9882 aSnapshot = mData->mFirstSnapshot->i_findChildOrSelf(aId.ref());
9883
9884 if (!aSnapshot)
9885 {
9886 if (aSetError)
9887 return setError(E_FAIL,
9888 tr("Could not find a snapshot with UUID {%s}"),
9889 aId.toString().c_str());
9890 return E_FAIL;
9891 }
9892
9893 return S_OK;
9894}
9895
9896/**
9897 * Returns the snapshot with the given name or fails of no such snapshot.
9898 *
9899 * @param aName snapshot name to find
9900 * @param aSnapshot where to return the found snapshot
9901 * @param aSetError true to set extended error info on failure
9902 */
9903HRESULT Machine::findSnapshotByName(const Utf8Str &strName,
9904 ComObjPtr<Snapshot> &aSnapshot,
9905 bool aSetError /* = false */)
9906{
9907 AssertReturn(!strName.isEmpty(), E_INVALIDARG);
9908
9909 AutoReadLock chlock(this COMMA_LOCKVAL_SRC_POS);
9910
9911 if (!mData->mFirstSnapshot)
9912 {
9913 if (aSetError)
9914 return setError(VBOX_E_OBJECT_NOT_FOUND,
9915 tr("This machine does not have any snapshots"));
9916 return VBOX_E_OBJECT_NOT_FOUND;
9917 }
9918
9919 aSnapshot = mData->mFirstSnapshot->i_findChildOrSelf(strName);
9920
9921 if (!aSnapshot)
9922 {
9923 if (aSetError)
9924 return setError(VBOX_E_OBJECT_NOT_FOUND,
9925 tr("Could not find a snapshot named '%s'"), strName.c_str());
9926 return VBOX_E_OBJECT_NOT_FOUND;
9927 }
9928
9929 return S_OK;
9930}
9931
9932/**
9933 * Returns a storage controller object with the given name.
9934 *
9935 * @param aName storage controller name to find
9936 * @param aStorageController where to return the found storage controller
9937 * @param aSetError true to set extended error info on failure
9938 */
9939HRESULT Machine::getStorageControllerByName(const Utf8Str &aName,
9940 ComObjPtr<StorageController> &aStorageController,
9941 bool aSetError /* = false */)
9942{
9943 AssertReturn(!aName.isEmpty(), E_INVALIDARG);
9944
9945 for (StorageControllerList::const_iterator it = mStorageControllers->begin();
9946 it != mStorageControllers->end();
9947 ++it)
9948 {
9949 if ((*it)->i_getName() == aName)
9950 {
9951 aStorageController = (*it);
9952 return S_OK;
9953 }
9954 }
9955
9956 if (aSetError)
9957 return setError(VBOX_E_OBJECT_NOT_FOUND,
9958 tr("Could not find a storage controller named '%s'"),
9959 aName.c_str());
9960 return VBOX_E_OBJECT_NOT_FOUND;
9961}
9962
9963/**
9964 * Returns a USB controller object with the given name.
9965 *
9966 * @param aName USB controller name to find
9967 * @param aUSBController where to return the found USB controller
9968 * @param aSetError true to set extended error info on failure
9969 */
9970HRESULT Machine::getUSBControllerByName(const Utf8Str &aName,
9971 ComObjPtr<USBController> &aUSBController,
9972 bool aSetError /* = false */)
9973{
9974 AssertReturn(!aName.isEmpty(), E_INVALIDARG);
9975
9976 for (USBControllerList::const_iterator it = mUSBControllers->begin();
9977 it != mUSBControllers->end();
9978 ++it)
9979 {
9980 if ((*it)->i_getName() == aName)
9981 {
9982 aUSBController = (*it);
9983 return S_OK;
9984 }
9985 }
9986
9987 if (aSetError)
9988 return setError(VBOX_E_OBJECT_NOT_FOUND,
9989 tr("Could not find a storage controller named '%s'"),
9990 aName.c_str());
9991 return VBOX_E_OBJECT_NOT_FOUND;
9992}
9993
9994/**
9995 * Returns the number of USB controller instance of the given type.
9996 *
9997 * @param enmType USB controller type.
9998 */
9999ULONG Machine::getUSBControllerCountByType(USBControllerType_T enmType)
10000{
10001 ULONG cCtrls = 0;
10002
10003 for (USBControllerList::const_iterator it = mUSBControllers->begin();
10004 it != mUSBControllers->end();
10005 ++it)
10006 {
10007 if ((*it)->i_getControllerType() == enmType)
10008 cCtrls++;
10009 }
10010
10011 return cCtrls;
10012}
10013
10014HRESULT Machine::getMediumAttachmentsOfController(CBSTR aName,
10015 MediaData::AttachmentList &atts)
10016{
10017 AutoCaller autoCaller(this);
10018 if (FAILED(autoCaller.rc())) return autoCaller.rc();
10019
10020 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
10021
10022 for (MediaData::AttachmentList::iterator it = mMediaData->mAttachments.begin();
10023 it != mMediaData->mAttachments.end();
10024 ++it)
10025 {
10026 const ComObjPtr<MediumAttachment> &pAtt = *it;
10027
10028 // should never happen, but deal with NULL pointers in the list.
10029 AssertStmt(!pAtt.isNull(), continue);
10030
10031 // getControllerName() needs caller+read lock
10032 AutoCaller autoAttCaller(pAtt);
10033 if (FAILED(autoAttCaller.rc()))
10034 {
10035 atts.clear();
10036 return autoAttCaller.rc();
10037 }
10038 AutoReadLock attLock(pAtt COMMA_LOCKVAL_SRC_POS);
10039
10040 if (pAtt->i_getControllerName() == aName)
10041 atts.push_back(pAtt);
10042 }
10043
10044 return S_OK;
10045}
10046
10047/**
10048 * Helper for #saveSettings. Cares about renaming the settings directory and
10049 * file if the machine name was changed and about creating a new settings file
10050 * if this is a new machine.
10051 *
10052 * @note Must be never called directly but only from #saveSettings().
10053 */
10054HRESULT Machine::prepareSaveSettings(bool *pfNeedsGlobalSaveSettings)
10055{
10056 AssertReturn(isWriteLockOnCurrentThread(), E_FAIL);
10057
10058 HRESULT rc = S_OK;
10059
10060 bool fSettingsFileIsNew = !mData->pMachineConfigFile->fileExists();
10061
10062 /// @todo need to handle primary group change, too
10063
10064 /* attempt to rename the settings file if machine name is changed */
10065 if ( mUserData->s.fNameSync
10066 && mUserData.isBackedUp()
10067 && ( mUserData.backedUpData()->s.strName != mUserData->s.strName
10068 || mUserData.backedUpData()->s.llGroups.front() != mUserData->s.llGroups.front())
10069 )
10070 {
10071 bool dirRenamed = false;
10072 bool fileRenamed = false;
10073
10074 Utf8Str configFile, newConfigFile;
10075 Utf8Str configFilePrev, newConfigFilePrev;
10076 Utf8Str configDir, newConfigDir;
10077
10078 do
10079 {
10080 int vrc = VINF_SUCCESS;
10081
10082 Utf8Str name = mUserData.backedUpData()->s.strName;
10083 Utf8Str newName = mUserData->s.strName;
10084 Utf8Str group = mUserData.backedUpData()->s.llGroups.front();
10085 if (group == "/")
10086 group.setNull();
10087 Utf8Str newGroup = mUserData->s.llGroups.front();
10088 if (newGroup == "/")
10089 newGroup.setNull();
10090
10091 configFile = mData->m_strConfigFileFull;
10092
10093 /* first, rename the directory if it matches the group and machine name */
10094 Utf8Str groupPlusName = Utf8StrFmt("%s%c%s",
10095 group.c_str(), RTPATH_DELIMITER, name.c_str());
10096 /** @todo hack, make somehow use of ComposeMachineFilename */
10097 if (mUserData->s.fDirectoryIncludesUUID)
10098 groupPlusName += Utf8StrFmt(" (%RTuuid)", mData->mUuid.raw());
10099 Utf8Str newGroupPlusName = Utf8StrFmt("%s%c%s",
10100 newGroup.c_str(), RTPATH_DELIMITER, newName.c_str());
10101 /** @todo hack, make somehow use of ComposeMachineFilename */
10102 if (mUserData->s.fDirectoryIncludesUUID)
10103 newGroupPlusName += Utf8StrFmt(" (%RTuuid)", mData->mUuid.raw());
10104 configDir = configFile;
10105 configDir.stripFilename();
10106 newConfigDir = configDir;
10107 if ( configDir.length() >= groupPlusName.length()
10108 && !RTPathCompare(configDir.substr(configDir.length() - groupPlusName.length(), groupPlusName.length()).c_str(), groupPlusName.c_str()))
10109 {
10110 newConfigDir = newConfigDir.substr(0, configDir.length() - groupPlusName.length());
10111 Utf8Str newConfigBaseDir(newConfigDir);
10112 newConfigDir.append(newGroupPlusName);
10113 /* consistency: use \ if appropriate on the platform */
10114 RTPathChangeToDosSlashes(newConfigDir.mutableRaw(), false);
10115 /* new dir and old dir cannot be equal here because of 'if'
10116 * above and because name != newName */
10117 Assert(configDir != newConfigDir);
10118 if (!fSettingsFileIsNew)
10119 {
10120 /* perform real rename only if the machine is not new */
10121 vrc = RTPathRename(configDir.c_str(), newConfigDir.c_str(), 0);
10122 if ( vrc == VERR_FILE_NOT_FOUND
10123 || vrc == VERR_PATH_NOT_FOUND)
10124 {
10125 /* create the parent directory, then retry renaming */
10126 Utf8Str parent(newConfigDir);
10127 parent.stripFilename();
10128 (void)RTDirCreateFullPath(parent.c_str(), 0700);
10129 vrc = RTPathRename(configDir.c_str(), newConfigDir.c_str(), 0);
10130 }
10131 if (RT_FAILURE(vrc))
10132 {
10133 rc = setError(E_FAIL,
10134 tr("Could not rename the directory '%s' to '%s' to save the settings file (%Rrc)"),
10135 configDir.c_str(),
10136 newConfigDir.c_str(),
10137 vrc);
10138 break;
10139 }
10140 /* delete subdirectories which are no longer needed */
10141 Utf8Str dir(configDir);
10142 dir.stripFilename();
10143 while (dir != newConfigBaseDir && dir != ".")
10144 {
10145 vrc = RTDirRemove(dir.c_str());
10146 if (RT_FAILURE(vrc))
10147 break;
10148 dir.stripFilename();
10149 }
10150 dirRenamed = true;
10151 }
10152 }
10153
10154 newConfigFile = Utf8StrFmt("%s%c%s.vbox",
10155 newConfigDir.c_str(), RTPATH_DELIMITER, newName.c_str());
10156
10157 /* then try to rename the settings file itself */
10158 if (newConfigFile != configFile)
10159 {
10160 /* get the path to old settings file in renamed directory */
10161 configFile = Utf8StrFmt("%s%c%s",
10162 newConfigDir.c_str(),
10163 RTPATH_DELIMITER,
10164 RTPathFilename(configFile.c_str()));
10165 if (!fSettingsFileIsNew)
10166 {
10167 /* perform real rename only if the machine is not new */
10168 vrc = RTFileRename(configFile.c_str(), newConfigFile.c_str(), 0);
10169 if (RT_FAILURE(vrc))
10170 {
10171 rc = setError(E_FAIL,
10172 tr("Could not rename the settings file '%s' to '%s' (%Rrc)"),
10173 configFile.c_str(),
10174 newConfigFile.c_str(),
10175 vrc);
10176 break;
10177 }
10178 fileRenamed = true;
10179 configFilePrev = configFile;
10180 configFilePrev += "-prev";
10181 newConfigFilePrev = newConfigFile;
10182 newConfigFilePrev += "-prev";
10183 RTFileRename(configFilePrev.c_str(), newConfigFilePrev.c_str(), 0);
10184 }
10185 }
10186
10187 // update m_strConfigFileFull amd mConfigFile
10188 mData->m_strConfigFileFull = newConfigFile;
10189 // compute the relative path too
10190 mParent->i_copyPathRelativeToConfig(newConfigFile, mData->m_strConfigFile);
10191
10192 // store the old and new so that VirtualBox::saveSettings() can update
10193 // the media registry
10194 if ( mData->mRegistered
10195 && configDir != newConfigDir)
10196 {
10197 mParent->i_rememberMachineNameChangeForMedia(configDir, newConfigDir);
10198
10199 if (pfNeedsGlobalSaveSettings)
10200 *pfNeedsGlobalSaveSettings = true;
10201 }
10202
10203 // in the saved state file path, replace the old directory with the new directory
10204 if (RTPathStartsWith(mSSData->strStateFilePath.c_str(), configDir.c_str()))
10205 mSSData->strStateFilePath = newConfigDir.append(mSSData->strStateFilePath.c_str() + configDir.length());
10206
10207 // and do the same thing for the saved state file paths of all the online snapshots
10208 if (mData->mFirstSnapshot)
10209 mData->mFirstSnapshot->i_updateSavedStatePaths(configDir.c_str(),
10210 newConfigDir.c_str());
10211 }
10212 while (0);
10213
10214 if (FAILED(rc))
10215 {
10216 /* silently try to rename everything back */
10217 if (fileRenamed)
10218 {
10219 RTFileRename(newConfigFilePrev.c_str(), configFilePrev.c_str(), 0);
10220 RTFileRename(newConfigFile.c_str(), configFile.c_str(), 0);
10221 }
10222 if (dirRenamed)
10223 RTPathRename(newConfigDir.c_str(), configDir.c_str(), 0);
10224 }
10225
10226 if (FAILED(rc)) return rc;
10227 }
10228
10229 if (fSettingsFileIsNew)
10230 {
10231 /* create a virgin config file */
10232 int vrc = VINF_SUCCESS;
10233
10234 /* ensure the settings directory exists */
10235 Utf8Str path(mData->m_strConfigFileFull);
10236 path.stripFilename();
10237 if (!RTDirExists(path.c_str()))
10238 {
10239 vrc = RTDirCreateFullPath(path.c_str(), 0700);
10240 if (RT_FAILURE(vrc))
10241 {
10242 return setError(E_FAIL,
10243 tr("Could not create a directory '%s' to save the settings file (%Rrc)"),
10244 path.c_str(),
10245 vrc);
10246 }
10247 }
10248
10249 /* Note: open flags must correlate with RTFileOpen() in lockConfig() */
10250 path = Utf8Str(mData->m_strConfigFileFull);
10251 RTFILE f = NIL_RTFILE;
10252 vrc = RTFileOpen(&f, path.c_str(),
10253 RTFILE_O_READWRITE | RTFILE_O_CREATE | RTFILE_O_DENY_WRITE);
10254 if (RT_FAILURE(vrc))
10255 return setError(E_FAIL,
10256 tr("Could not create the settings file '%s' (%Rrc)"),
10257 path.c_str(),
10258 vrc);
10259 RTFileClose(f);
10260 }
10261
10262 return rc;
10263}
10264
10265/**
10266 * Saves and commits machine data, user data and hardware data.
10267 *
10268 * Note that on failure, the data remains uncommitted.
10269 *
10270 * @a aFlags may combine the following flags:
10271 *
10272 * - SaveS_ResetCurStateModified: Resets mData->mCurrentStateModified to FALSE.
10273 * Used when saving settings after an operation that makes them 100%
10274 * correspond to the settings from the current snapshot.
10275 * - SaveS_InformCallbacksAnyway: Callbacks will be informed even if
10276 * #isReallyModified() returns false. This is necessary for cases when we
10277 * change machine data directly, not through the backup()/commit() mechanism.
10278 * - SaveS_Force: settings will be saved without doing a deep compare of the
10279 * settings structures. This is used when this is called because snapshots
10280 * have changed to avoid the overhead of the deep compare.
10281 *
10282 * @note Must be called from under this object's write lock. Locks children for
10283 * writing.
10284 *
10285 * @param pfNeedsGlobalSaveSettings Optional pointer to a bool that must have been
10286 * initialized to false and that will be set to true by this function if
10287 * the caller must invoke VirtualBox::saveSettings() because the global
10288 * settings have changed. This will happen if a machine rename has been
10289 * saved and the global machine and media registries will therefore need
10290 * updating.
10291 */
10292HRESULT Machine::saveSettings(bool *pfNeedsGlobalSaveSettings,
10293 int aFlags /*= 0*/)
10294{
10295 LogFlowThisFuncEnter();
10296
10297 AssertReturn(isWriteLockOnCurrentThread(), E_FAIL);
10298
10299 /* make sure child objects are unable to modify the settings while we are
10300 * saving them */
10301 ensureNoStateDependencies();
10302
10303 AssertReturn(!isSnapshotMachine(),
10304 E_FAIL);
10305
10306 HRESULT rc = S_OK;
10307 bool fNeedsWrite = false;
10308
10309 /* First, prepare to save settings. It will care about renaming the
10310 * settings directory and file if the machine name was changed and about
10311 * creating a new settings file if this is a new machine. */
10312 rc = prepareSaveSettings(pfNeedsGlobalSaveSettings);
10313 if (FAILED(rc)) return rc;
10314
10315 // keep a pointer to the current settings structures
10316 settings::MachineConfigFile *pOldConfig = mData->pMachineConfigFile;
10317 settings::MachineConfigFile *pNewConfig = NULL;
10318
10319 try
10320 {
10321 // make a fresh one to have everyone write stuff into
10322 pNewConfig = new settings::MachineConfigFile(NULL);
10323 pNewConfig->copyBaseFrom(*mData->pMachineConfigFile);
10324
10325 // now go and copy all the settings data from COM to the settings structures
10326 // (this calles saveSettings() on all the COM objects in the machine)
10327 copyMachineDataToSettings(*pNewConfig);
10328
10329 if (aFlags & SaveS_ResetCurStateModified)
10330 {
10331 // this gets set by takeSnapshot() (if offline snapshot) and restoreSnapshot()
10332 mData->mCurrentStateModified = FALSE;
10333 fNeedsWrite = true; // always, no need to compare
10334 }
10335 else if (aFlags & SaveS_Force)
10336 {
10337 fNeedsWrite = true; // always, no need to compare
10338 }
10339 else
10340 {
10341 if (!mData->mCurrentStateModified)
10342 {
10343 // do a deep compare of the settings that we just saved with the settings
10344 // previously stored in the config file; this invokes MachineConfigFile::operator==
10345 // which does a deep compare of all the settings, which is expensive but less expensive
10346 // than writing out XML in vain
10347 bool fAnySettingsChanged = !(*pNewConfig == *pOldConfig);
10348
10349 // could still be modified if any settings changed
10350 mData->mCurrentStateModified = fAnySettingsChanged;
10351
10352 fNeedsWrite = fAnySettingsChanged;
10353 }
10354 else
10355 fNeedsWrite = true;
10356 }
10357
10358 pNewConfig->fCurrentStateModified = !!mData->mCurrentStateModified;
10359
10360 if (fNeedsWrite)
10361 // now spit it all out!
10362 pNewConfig->write(mData->m_strConfigFileFull);
10363
10364 mData->pMachineConfigFile = pNewConfig;
10365 delete pOldConfig;
10366 commit();
10367
10368 // after saving settings, we are no longer different from the XML on disk
10369 mData->flModifications = 0;
10370 }
10371 catch (HRESULT err)
10372 {
10373 // we assume that error info is set by the thrower
10374 rc = err;
10375
10376 // restore old config
10377 delete pNewConfig;
10378 mData->pMachineConfigFile = pOldConfig;
10379 }
10380 catch (...)
10381 {
10382 rc = VirtualBoxBase::handleUnexpectedExceptions(this, RT_SRC_POS);
10383 }
10384
10385 if (fNeedsWrite || (aFlags & SaveS_InformCallbacksAnyway))
10386 {
10387 /* Fire the data change event, even on failure (since we've already
10388 * committed all data). This is done only for SessionMachines because
10389 * mutable Machine instances are always not registered (i.e. private
10390 * to the client process that creates them) and thus don't need to
10391 * inform callbacks. */
10392 if (isSessionMachine())
10393 mParent->i_onMachineDataChange(mData->mUuid);
10394 }
10395
10396 LogFlowThisFunc(("rc=%08X\n", rc));
10397 LogFlowThisFuncLeave();
10398 return rc;
10399}
10400
10401/**
10402 * Implementation for saving the machine settings into the given
10403 * settings::MachineConfigFile instance. This copies machine extradata
10404 * from the previous machine config file in the instance data, if any.
10405 *
10406 * This gets called from two locations:
10407 *
10408 * -- Machine::saveSettings(), during the regular XML writing;
10409 *
10410 * -- Appliance::buildXMLForOneVirtualSystem(), when a machine gets
10411 * exported to OVF and we write the VirtualBox proprietary XML
10412 * into a <vbox:Machine> tag.
10413 *
10414 * This routine fills all the fields in there, including snapshots, *except*
10415 * for the following:
10416 *
10417 * -- fCurrentStateModified. There is some special logic associated with that.
10418 *
10419 * The caller can then call MachineConfigFile::write() or do something else
10420 * with it.
10421 *
10422 * Caller must hold the machine lock!
10423 *
10424 * This throws XML errors and HRESULT, so the caller must have a catch block!
10425 */
10426void Machine::copyMachineDataToSettings(settings::MachineConfigFile &config)
10427{
10428 // deep copy extradata
10429 config.mapExtraDataItems = mData->pMachineConfigFile->mapExtraDataItems;
10430
10431 config.uuid = mData->mUuid;
10432
10433 // copy name, description, OS type, teleport, UTC etc.
10434 config.machineUserData = mUserData->s;
10435
10436 // Encode the Icon Override data from Machine and store on config userdata.
10437 com::SafeArray<BYTE> iconByte;
10438 COMGETTER(Icon)(ComSafeArrayAsOutParam(iconByte));
10439 ssize_t cbData = iconByte.size();
10440 if (cbData > 0)
10441 {
10442 ssize_t cchOut = RTBase64EncodedLength(cbData);
10443 Utf8Str strIconData;
10444 strIconData.reserve(cchOut+1);
10445 int vrc = RTBase64Encode(iconByte.raw(), cbData,
10446 strIconData.mutableRaw(), strIconData.capacity(),
10447 NULL);
10448 if (RT_FAILURE(vrc))
10449 throw setError(E_FAIL, tr("Failure to Encode Icon Data. '%s' (%Rrc)"), strIconData.mutableRaw(), vrc);
10450 strIconData.jolt();
10451 config.machineUserData.ovIcon = strIconData;
10452 }
10453 else
10454 config.machineUserData.ovIcon.setNull();
10455
10456 if ( mData->mMachineState == MachineState_Saved
10457 || mData->mMachineState == MachineState_Restoring
10458 // when deleting a snapshot we may or may not have a saved state in the current state,
10459 // so let's not assert here please
10460 || ( ( mData->mMachineState == MachineState_DeletingSnapshot
10461 || mData->mMachineState == MachineState_DeletingSnapshotOnline
10462 || mData->mMachineState == MachineState_DeletingSnapshotPaused)
10463 && (!mSSData->strStateFilePath.isEmpty())
10464 )
10465 )
10466 {
10467 Assert(!mSSData->strStateFilePath.isEmpty());
10468 /* try to make the file name relative to the settings file dir */
10469 copyPathRelativeToMachine(mSSData->strStateFilePath, config.strStateFile);
10470 }
10471 else
10472 {
10473 Assert(mSSData->strStateFilePath.isEmpty() || mData->mMachineState == MachineState_Saving);
10474 config.strStateFile.setNull();
10475 }
10476
10477 if (mData->mCurrentSnapshot)
10478 config.uuidCurrentSnapshot = mData->mCurrentSnapshot->i_getId();
10479 else
10480 config.uuidCurrentSnapshot.clear();
10481
10482 config.timeLastStateChange = mData->mLastStateChange;
10483 config.fAborted = (mData->mMachineState == MachineState_Aborted);
10484 /// @todo Live Migration: config.fTeleported = (mData->mMachineState == MachineState_Teleported);
10485
10486 HRESULT rc = saveHardware(config.hardwareMachine, &config.debugging, &config.autostart);
10487 if (FAILED(rc)) throw rc;
10488
10489 rc = saveStorageControllers(config.storageMachine);
10490 if (FAILED(rc)) throw rc;
10491
10492 // save machine's media registry if this is VirtualBox 4.0 or later
10493 if (config.canHaveOwnMediaRegistry())
10494 {
10495 // determine machine folder
10496 Utf8Str strMachineFolder = getSettingsFileFull();
10497 strMachineFolder.stripFilename();
10498 mParent->i_saveMediaRegistry(config.mediaRegistry,
10499 getId(), // only media with registry ID == machine UUID
10500 strMachineFolder);
10501 // this throws HRESULT
10502 }
10503
10504 // save snapshots
10505 rc = saveAllSnapshots(config);
10506 if (FAILED(rc)) throw rc;
10507}
10508
10509/**
10510 * Saves all snapshots of the machine into the given machine config file. Called
10511 * from Machine::buildMachineXML() and SessionMachine::deleteSnapshotHandler().
10512 * @param config
10513 * @return
10514 */
10515HRESULT Machine::saveAllSnapshots(settings::MachineConfigFile &config)
10516{
10517 AssertReturn(isWriteLockOnCurrentThread(), E_FAIL);
10518
10519 HRESULT rc = S_OK;
10520
10521 try
10522 {
10523 config.llFirstSnapshot.clear();
10524
10525 if (mData->mFirstSnapshot)
10526 {
10527 settings::Snapshot snapNew;
10528 config.llFirstSnapshot.push_back(snapNew);
10529
10530 // get reference to the fresh copy of the snapshot on the list and
10531 // work on that copy directly to avoid excessive copying later
10532 settings::Snapshot &snap = config.llFirstSnapshot.front();
10533
10534 rc = mData->mFirstSnapshot->i_saveSnapshot(snap, false /*aAttrsOnly*/);
10535 if (FAILED(rc)) throw rc;
10536 }
10537
10538// if (mType == IsSessionMachine)
10539// mParent->onMachineDataChange(mData->mUuid); @todo is this necessary?
10540
10541 }
10542 catch (HRESULT err)
10543 {
10544 /* we assume that error info is set by the thrower */
10545 rc = err;
10546 }
10547 catch (...)
10548 {
10549 rc = VirtualBoxBase::handleUnexpectedExceptions(this, RT_SRC_POS);
10550 }
10551
10552 return rc;
10553}
10554
10555/**
10556 * Saves the VM hardware configuration. It is assumed that the
10557 * given node is empty.
10558 *
10559 * @param data Reference to the settings object for the hardware config.
10560 * @param pDbg Pointer to the settings object for the debugging config
10561 * which happens to live in mHWData.
10562 * @param pAutostart Pointer to the settings object for the autostart config
10563 * which happens to live in mHWData.
10564 */
10565HRESULT Machine::saveHardware(settings::Hardware &data, settings::Debugging *pDbg,
10566 settings::Autostart *pAutostart)
10567{
10568 HRESULT rc = S_OK;
10569
10570 try
10571 {
10572 /* The hardware version attribute (optional).
10573 Automatically upgrade from 1 to 2 when there is no saved state. (ugly!) */
10574 if ( mHWData->mHWVersion == "1"
10575 && mSSData->strStateFilePath.isEmpty()
10576 )
10577 mHWData->mHWVersion = "2"; /** @todo Is this safe, to update mHWVersion here? If not some other point needs to be found where this can be done. */
10578
10579 data.strVersion = mHWData->mHWVersion;
10580 data.uuid = mHWData->mHardwareUUID;
10581
10582 // CPU
10583 data.fHardwareVirt = !!mHWData->mHWVirtExEnabled;
10584 data.fNestedPaging = !!mHWData->mHWVirtExNestedPagingEnabled;
10585 data.fLargePages = !!mHWData->mHWVirtExLargePagesEnabled;
10586 data.fVPID = !!mHWData->mHWVirtExVPIDEnabled;
10587 data.fUnrestrictedExecution = !!mHWData->mHWVirtExUXEnabled;
10588 data.fHardwareVirtForce = !!mHWData->mHWVirtExForceEnabled;
10589 data.fPAE = !!mHWData->mPAEEnabled;
10590 data.enmLongMode = mHWData->mLongMode;
10591 data.fSyntheticCpu = !!mHWData->mSyntheticCpu;
10592 data.fTripleFaultReset = !!mHWData->mTripleFaultReset;
10593
10594 /* Standard and Extended CPUID leafs. */
10595 data.llCpuIdLeafs.clear();
10596 for (unsigned idx = 0; idx < RT_ELEMENTS(mHWData->mCpuIdStdLeafs); idx++)
10597 {
10598 if (mHWData->mCpuIdStdLeafs[idx].ulId != UINT32_MAX)
10599 data.llCpuIdLeafs.push_back(mHWData->mCpuIdStdLeafs[idx]);
10600 }
10601 for (unsigned idx = 0; idx < RT_ELEMENTS(mHWData->mCpuIdExtLeafs); idx++)
10602 {
10603 if (mHWData->mCpuIdExtLeafs[idx].ulId != UINT32_MAX)
10604 data.llCpuIdLeafs.push_back(mHWData->mCpuIdExtLeafs[idx]);
10605 }
10606
10607 data.cCPUs = mHWData->mCPUCount;
10608 data.fCpuHotPlug = !!mHWData->mCPUHotPlugEnabled;
10609 data.ulCpuExecutionCap = mHWData->mCpuExecutionCap;
10610
10611 data.llCpus.clear();
10612 if (data.fCpuHotPlug)
10613 {
10614 for (unsigned idx = 0; idx < data.cCPUs; idx++)
10615 {
10616 if (mHWData->mCPUAttached[idx])
10617 {
10618 settings::Cpu cpu;
10619 cpu.ulId = idx;
10620 data.llCpus.push_back(cpu);
10621 }
10622 }
10623 }
10624
10625 // memory
10626 data.ulMemorySizeMB = mHWData->mMemorySize;
10627 data.fPageFusionEnabled = !!mHWData->mPageFusionEnabled;
10628
10629 // firmware
10630 data.firmwareType = mHWData->mFirmwareType;
10631
10632 // HID
10633 data.pointingHIDType = mHWData->mPointingHIDType;
10634 data.keyboardHIDType = mHWData->mKeyboardHIDType;
10635
10636 // chipset
10637 data.chipsetType = mHWData->mChipsetType;
10638
10639 data.fEmulatedUSBCardReader = !!mHWData->mEmulatedUSBCardReaderEnabled;
10640
10641 // HPET
10642 data.fHPETEnabled = !!mHWData->mHPETEnabled;
10643
10644 // boot order
10645 data.mapBootOrder.clear();
10646 for (size_t i = 0;
10647 i < RT_ELEMENTS(mHWData->mBootOrder);
10648 ++i)
10649 data.mapBootOrder[i] = mHWData->mBootOrder[i];
10650
10651 // display
10652 data.graphicsControllerType = mHWData->mGraphicsControllerType;
10653 data.ulVRAMSizeMB = mHWData->mVRAMSize;
10654 data.cMonitors = mHWData->mMonitorCount;
10655 data.fAccelerate3D = !!mHWData->mAccelerate3DEnabled;
10656 data.fAccelerate2DVideo = !!mHWData->mAccelerate2DVideoEnabled;
10657 data.ulVideoCaptureHorzRes = mHWData->mVideoCaptureWidth;
10658 data.ulVideoCaptureVertRes = mHWData->mVideoCaptureHeight;
10659 data.ulVideoCaptureRate = mHWData->mVideoCaptureRate;
10660 data.ulVideoCaptureFPS = mHWData->mVideoCaptureFPS;
10661 data.fVideoCaptureEnabled = !!mHWData->mVideoCaptureEnabled;
10662 for (unsigned i = 0; i < sizeof(data.u64VideoCaptureScreens) * 8; i++)
10663 {
10664 if (mHWData->maVideoCaptureScreens[i])
10665 ASMBitSet(&data.u64VideoCaptureScreens, i);
10666 else
10667 ASMBitClear(&data.u64VideoCaptureScreens, i);
10668 }
10669 /* store relative video capture file if possible */
10670 copyPathRelativeToMachine(mHWData->mVideoCaptureFile, data.strVideoCaptureFile);
10671
10672 /* VRDEServer settings (optional) */
10673 rc = mVRDEServer->i_saveSettings(data.vrdeSettings);
10674 if (FAILED(rc)) throw rc;
10675
10676 /* BIOS (required) */
10677 rc = mBIOSSettings->i_saveSettings(data.biosSettings);
10678 if (FAILED(rc)) throw rc;
10679
10680 /* USB Controller (required) */
10681 for (USBControllerList::const_iterator it = mUSBControllers->begin();
10682 it != mUSBControllers->end();
10683 ++it)
10684 {
10685 ComObjPtr<USBController> ctrl = *it;
10686 settings::USBController settingsCtrl;
10687
10688 settingsCtrl.strName = ctrl->i_getName();
10689 settingsCtrl.enmType = ctrl->i_getControllerType();
10690
10691 data.usbSettings.llUSBControllers.push_back(settingsCtrl);
10692 }
10693
10694 /* USB device filters (required) */
10695 rc = mUSBDeviceFilters->i_saveSettings(data.usbSettings);
10696 if (FAILED(rc)) throw rc;
10697
10698 /* Network adapters (required) */
10699 uint32_t uMaxNICs = RT_MIN(Global::getMaxNetworkAdapters(mHWData->mChipsetType), mNetworkAdapters.size());
10700 data.llNetworkAdapters.clear();
10701 /* Write out only the nominal number of network adapters for this
10702 * chipset type. Since Machine::commit() hasn't been called there
10703 * may be extra NIC settings in the vector. */
10704 for (ULONG slot = 0; slot < uMaxNICs; ++slot)
10705 {
10706 settings::NetworkAdapter nic;
10707 nic.ulSlot = slot;
10708 /* paranoia check... must not be NULL, but must not crash either. */
10709 if (mNetworkAdapters[slot])
10710 {
10711 rc = mNetworkAdapters[slot]->i_saveSettings(nic);
10712 if (FAILED(rc)) throw rc;
10713
10714 data.llNetworkAdapters.push_back(nic);
10715 }
10716 }
10717
10718 /* Serial ports */
10719 data.llSerialPorts.clear();
10720 for (ULONG slot = 0;
10721 slot < RT_ELEMENTS(mSerialPorts);
10722 ++slot)
10723 {
10724 settings::SerialPort s;
10725 s.ulSlot = slot;
10726 rc = mSerialPorts[slot]->i_saveSettings(s);
10727 if (FAILED(rc)) return rc;
10728
10729 data.llSerialPorts.push_back(s);
10730 }
10731
10732 /* Parallel ports */
10733 data.llParallelPorts.clear();
10734 for (ULONG slot = 0;
10735 slot < RT_ELEMENTS(mParallelPorts);
10736 ++slot)
10737 {
10738 settings::ParallelPort p;
10739 p.ulSlot = slot;
10740 rc = mParallelPorts[slot]->i_saveSettings(p);
10741 if (FAILED(rc)) return rc;
10742
10743 data.llParallelPorts.push_back(p);
10744 }
10745
10746 /* Audio adapter */
10747 rc = mAudioAdapter->i_saveSettings(data.audioAdapter);
10748 if (FAILED(rc)) return rc;
10749
10750 /* Shared folders */
10751 data.llSharedFolders.clear();
10752 for (HWData::SharedFolderList::const_iterator it = mHWData->mSharedFolders.begin();
10753 it != mHWData->mSharedFolders.end();
10754 ++it)
10755 {
10756 SharedFolder *pSF = *it;
10757 AutoCaller sfCaller(pSF);
10758 AutoReadLock sfLock(pSF COMMA_LOCKVAL_SRC_POS);
10759 settings::SharedFolder sf;
10760 sf.strName = pSF->getName();
10761 sf.strHostPath = pSF->getHostPath();
10762 sf.fWritable = !!pSF->isWritable();
10763 sf.fAutoMount = !!pSF->isAutoMounted();
10764
10765 data.llSharedFolders.push_back(sf);
10766 }
10767
10768 // clipboard
10769 data.clipboardMode = mHWData->mClipboardMode;
10770
10771 // drag'n'drop
10772 data.dragAndDropMode = mHWData->mDragAndDropMode;
10773
10774 /* Guest */
10775 data.ulMemoryBalloonSize = mHWData->mMemoryBalloonSize;
10776
10777 // IO settings
10778 data.ioSettings.fIOCacheEnabled = !!mHWData->mIOCacheEnabled;
10779 data.ioSettings.ulIOCacheSize = mHWData->mIOCacheSize;
10780
10781 /* BandwidthControl (required) */
10782 rc = mBandwidthControl->i_saveSettings(data.ioSettings);
10783 if (FAILED(rc)) throw rc;
10784
10785 /* Host PCI devices */
10786 for (HWData::PCIDeviceAssignmentList::const_iterator it = mHWData->mPCIDeviceAssignments.begin();
10787 it != mHWData->mPCIDeviceAssignments.end();
10788 ++it)
10789 {
10790 ComObjPtr<PCIDeviceAttachment> pda = *it;
10791 settings::HostPCIDeviceAttachment hpda;
10792
10793 rc = pda->saveSettings(hpda);
10794 if (FAILED(rc)) throw rc;
10795
10796 data.pciAttachments.push_back(hpda);
10797 }
10798
10799
10800 // guest properties
10801 data.llGuestProperties.clear();
10802#ifdef VBOX_WITH_GUEST_PROPS
10803 for (HWData::GuestPropertyMap::const_iterator it = mHWData->mGuestProperties.begin();
10804 it != mHWData->mGuestProperties.end();
10805 ++it)
10806 {
10807 HWData::GuestProperty property = it->second;
10808
10809 /* Remove transient guest properties at shutdown unless we
10810 * are saving state */
10811 if ( ( mData->mMachineState == MachineState_PoweredOff
10812 || mData->mMachineState == MachineState_Aborted
10813 || mData->mMachineState == MachineState_Teleported)
10814 && ( property.mFlags & guestProp::TRANSIENT
10815 || property.mFlags & guestProp::TRANSRESET))
10816 continue;
10817 settings::GuestProperty prop;
10818 prop.strName = it->first;
10819 prop.strValue = property.strValue;
10820 prop.timestamp = property.mTimestamp;
10821 char szFlags[guestProp::MAX_FLAGS_LEN + 1];
10822 guestProp::writeFlags(property.mFlags, szFlags);
10823 prop.strFlags = szFlags;
10824
10825 data.llGuestProperties.push_back(prop);
10826 }
10827
10828 data.strNotificationPatterns = mHWData->mGuestPropertyNotificationPatterns;
10829 /* I presume this doesn't require a backup(). */
10830 mData->mGuestPropertiesModified = FALSE;
10831#endif /* VBOX_WITH_GUEST_PROPS defined */
10832
10833 *pDbg = mHWData->mDebugging;
10834 *pAutostart = mHWData->mAutostart;
10835
10836 data.strDefaultFrontend = mHWData->mDefaultFrontend;
10837 }
10838 catch(std::bad_alloc &)
10839 {
10840 return E_OUTOFMEMORY;
10841 }
10842
10843 AssertComRC(rc);
10844 return rc;
10845}
10846
10847/**
10848 * Saves the storage controller configuration.
10849 *
10850 * @param aNode <StorageControllers> node to save the VM hardware configuration to.
10851 */
10852HRESULT Machine::saveStorageControllers(settings::Storage &data)
10853{
10854 data.llStorageControllers.clear();
10855
10856 for (StorageControllerList::const_iterator it = mStorageControllers->begin();
10857 it != mStorageControllers->end();
10858 ++it)
10859 {
10860 HRESULT rc;
10861 ComObjPtr<StorageController> pCtl = *it;
10862
10863 settings::StorageController ctl;
10864 ctl.strName = pCtl->i_getName();
10865 ctl.controllerType = pCtl->i_getControllerType();
10866 ctl.storageBus = pCtl->i_getStorageBus();
10867 ctl.ulInstance = pCtl->i_getInstance();
10868 ctl.fBootable = pCtl->i_getBootable();
10869
10870 /* Save the port count. */
10871 ULONG portCount;
10872 rc = pCtl->COMGETTER(PortCount)(&portCount);
10873 ComAssertComRCRet(rc, rc);
10874 ctl.ulPortCount = portCount;
10875
10876 /* Save fUseHostIOCache */
10877 BOOL fUseHostIOCache;
10878 rc = pCtl->COMGETTER(UseHostIOCache)(&fUseHostIOCache);
10879 ComAssertComRCRet(rc, rc);
10880 ctl.fUseHostIOCache = !!fUseHostIOCache;
10881
10882 /* Save IDE emulation settings. */
10883 if (ctl.controllerType == StorageControllerType_IntelAhci)
10884 {
10885 if ( (FAILED(rc = pCtl->i_getIDEEmulationPort(0, (LONG*)&ctl.lIDE0MasterEmulationPort)))
10886 || (FAILED(rc = pCtl->i_getIDEEmulationPort(1, (LONG*)&ctl.lIDE0SlaveEmulationPort)))
10887 || (FAILED(rc = pCtl->i_getIDEEmulationPort(2, (LONG*)&ctl.lIDE1MasterEmulationPort)))
10888 || (FAILED(rc = pCtl->i_getIDEEmulationPort(3, (LONG*)&ctl.lIDE1SlaveEmulationPort)))
10889 )
10890 ComAssertComRCRet(rc, rc);
10891 }
10892
10893 /* save the devices now. */
10894 rc = saveStorageDevices(pCtl, ctl);
10895 ComAssertComRCRet(rc, rc);
10896
10897 data.llStorageControllers.push_back(ctl);
10898 }
10899
10900 return S_OK;
10901}
10902
10903/**
10904 * Saves the hard disk configuration.
10905 */
10906HRESULT Machine::saveStorageDevices(ComObjPtr<StorageController> aStorageController,
10907 settings::StorageController &data)
10908{
10909 MediaData::AttachmentList atts;
10910
10911 HRESULT rc = getMediumAttachmentsOfController(Bstr(aStorageController->i_getName()).raw(), atts);
10912 if (FAILED(rc)) return rc;
10913
10914 data.llAttachedDevices.clear();
10915 for (MediaData::AttachmentList::const_iterator it = atts.begin();
10916 it != atts.end();
10917 ++it)
10918 {
10919 settings::AttachedDevice dev;
10920
10921 MediumAttachment *pAttach = *it;
10922 Medium *pMedium = pAttach->i_getMedium();
10923
10924 dev.deviceType = pAttach->i_getType();
10925 dev.lPort = pAttach->i_getPort();
10926 dev.lDevice = pAttach->i_getDevice();
10927 dev.fPassThrough = pAttach->i_getPassthrough();
10928 dev.fHotPluggable = pAttach->i_getHotPluggable();
10929 if (pMedium)
10930 {
10931 if (pMedium->i_isHostDrive())
10932 dev.strHostDriveSrc = pMedium->i_getLocationFull();
10933 else
10934 dev.uuid = pMedium->i_getId();
10935 dev.fTempEject = pAttach->i_getTempEject();
10936 dev.fNonRotational = pAttach->i_getNonRotational();
10937 dev.fDiscard = pAttach->i_getDiscard();
10938 }
10939
10940 dev.strBwGroup = pAttach->i_getBandwidthGroup();
10941
10942 data.llAttachedDevices.push_back(dev);
10943 }
10944
10945 return S_OK;
10946}
10947
10948/**
10949 * Saves machine state settings as defined by aFlags
10950 * (SaveSTS_* values).
10951 *
10952 * @param aFlags Combination of SaveSTS_* flags.
10953 *
10954 * @note Locks objects for writing.
10955 */
10956HRESULT Machine::saveStateSettings(int aFlags)
10957{
10958 if (aFlags == 0)
10959 return S_OK;
10960
10961 AutoCaller autoCaller(this);
10962 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
10963
10964 /* This object's write lock is also necessary to serialize file access
10965 * (prevent concurrent reads and writes) */
10966 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
10967
10968 HRESULT rc = S_OK;
10969
10970 Assert(mData->pMachineConfigFile);
10971
10972 try
10973 {
10974 if (aFlags & SaveSTS_CurStateModified)
10975 mData->pMachineConfigFile->fCurrentStateModified = true;
10976
10977 if (aFlags & SaveSTS_StateFilePath)
10978 {
10979 if (!mSSData->strStateFilePath.isEmpty())
10980 /* try to make the file name relative to the settings file dir */
10981 copyPathRelativeToMachine(mSSData->strStateFilePath, mData->pMachineConfigFile->strStateFile);
10982 else
10983 mData->pMachineConfigFile->strStateFile.setNull();
10984 }
10985
10986 if (aFlags & SaveSTS_StateTimeStamp)
10987 {
10988 Assert( mData->mMachineState != MachineState_Aborted
10989 || mSSData->strStateFilePath.isEmpty());
10990
10991 mData->pMachineConfigFile->timeLastStateChange = mData->mLastStateChange;
10992
10993 mData->pMachineConfigFile->fAborted = (mData->mMachineState == MachineState_Aborted);
10994//@todo live migration mData->pMachineConfigFile->fTeleported = (mData->mMachineState == MachineState_Teleported);
10995 }
10996
10997 mData->pMachineConfigFile->write(mData->m_strConfigFileFull);
10998 }
10999 catch (...)
11000 {
11001 rc = VirtualBoxBase::handleUnexpectedExceptions(this, RT_SRC_POS);
11002 }
11003
11004 return rc;
11005}
11006
11007/**
11008 * Ensures that the given medium is added to a media registry. If this machine
11009 * was created with 4.0 or later, then the machine registry is used. Otherwise
11010 * the global VirtualBox media registry is used.
11011 *
11012 * Caller must NOT hold machine lock, media tree or any medium locks!
11013 *
11014 * @param pMedium
11015 */
11016void Machine::addMediumToRegistry(ComObjPtr<Medium> &pMedium)
11017{
11018 /* Paranoia checks: do not hold machine or media tree locks. */
11019 AssertReturnVoid(!isWriteLockOnCurrentThread());
11020 AssertReturnVoid(!mParent->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
11021
11022 ComObjPtr<Medium> pBase;
11023 {
11024 AutoReadLock treeLock(&mParent->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
11025 pBase = pMedium->i_getBase();
11026 }
11027
11028 /* Paranoia checks: do not hold medium locks. */
11029 AssertReturnVoid(!pMedium->isWriteLockOnCurrentThread());
11030 AssertReturnVoid(!pBase->isWriteLockOnCurrentThread());
11031
11032 // decide which medium registry to use now that the medium is attached:
11033 Guid uuid;
11034 if (mData->pMachineConfigFile->canHaveOwnMediaRegistry())
11035 // machine XML is VirtualBox 4.0 or higher:
11036 uuid = getId(); // machine UUID
11037 else
11038 uuid = mParent->i_getGlobalRegistryId(); // VirtualBox global registry UUID
11039
11040 if (pMedium->i_addRegistry(uuid, false /* fRecurse */))
11041 mParent->i_markRegistryModified(uuid);
11042
11043 /* For more complex hard disk structures it can happen that the base
11044 * medium isn't yet associated with any medium registry. Do that now. */
11045 if (pMedium != pBase)
11046 {
11047 if (pBase->i_addRegistry(uuid, true /* fRecurse */))
11048 mParent->i_markRegistryModified(uuid);
11049 }
11050}
11051
11052/**
11053 * Creates differencing hard disks for all normal hard disks attached to this
11054 * machine and a new set of attachments to refer to created disks.
11055 *
11056 * Used when taking a snapshot or when deleting the current state. Gets called
11057 * from SessionMachine::BeginTakingSnapshot() and SessionMachine::restoreSnapshotHandler().
11058 *
11059 * This method assumes that mMediaData contains the original hard disk attachments
11060 * it needs to create diffs for. On success, these attachments will be replaced
11061 * with the created diffs. On failure, #deleteImplicitDiffs() is implicitly
11062 * called to delete created diffs which will also rollback mMediaData and restore
11063 * whatever was backed up before calling this method.
11064 *
11065 * Attachments with non-normal hard disks are left as is.
11066 *
11067 * If @a aOnline is @c false then the original hard disks that require implicit
11068 * diffs will be locked for reading. Otherwise it is assumed that they are
11069 * already locked for writing (when the VM was started). Note that in the latter
11070 * case it is responsibility of the caller to lock the newly created diffs for
11071 * writing if this method succeeds.
11072 *
11073 * @param aProgress Progress object to run (must contain at least as
11074 * many operations left as the number of hard disks
11075 * attached).
11076 * @param aOnline Whether the VM was online prior to this operation.
11077 *
11078 * @note The progress object is not marked as completed, neither on success nor
11079 * on failure. This is a responsibility of the caller.
11080 *
11081 * @note Locks this object and the media tree for writing.
11082 */
11083HRESULT Machine::createImplicitDiffs(IProgress *aProgress,
11084 ULONG aWeight,
11085 bool aOnline)
11086{
11087 LogFlowThisFunc(("aOnline=%d\n", aOnline));
11088
11089 AutoCaller autoCaller(this);
11090 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
11091
11092 AutoMultiWriteLock2 alock(this->lockHandle(),
11093 &mParent->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
11094
11095 /* must be in a protective state because we release the lock below */
11096 AssertReturn( mData->mMachineState == MachineState_Saving
11097 || mData->mMachineState == MachineState_LiveSnapshotting
11098 || mData->mMachineState == MachineState_RestoringSnapshot
11099 || mData->mMachineState == MachineState_DeletingSnapshot
11100 , E_FAIL);
11101
11102 HRESULT rc = S_OK;
11103
11104 // use appropriate locked media map (online or offline)
11105 MediumLockListMap lockedMediaOffline;
11106 MediumLockListMap *lockedMediaMap;
11107 if (aOnline)
11108 lockedMediaMap = &mData->mSession.mLockedMedia;
11109 else
11110 lockedMediaMap = &lockedMediaOffline;
11111
11112 try
11113 {
11114 if (!aOnline)
11115 {
11116 /* lock all attached hard disks early to detect "in use"
11117 * situations before creating actual diffs */
11118 for (MediaData::AttachmentList::const_iterator it = mMediaData->mAttachments.begin();
11119 it != mMediaData->mAttachments.end();
11120 ++it)
11121 {
11122 MediumAttachment* pAtt = *it;
11123 if (pAtt->i_getType() == DeviceType_HardDisk)
11124 {
11125 Medium* pMedium = pAtt->i_getMedium();
11126 Assert(pMedium);
11127
11128 MediumLockList *pMediumLockList(new MediumLockList());
11129 alock.release();
11130 rc = pMedium->i_createMediumLockList(true /* fFailIfInaccessible */,
11131 false /* fMediumLockWrite */,
11132 NULL,
11133 *pMediumLockList);
11134 alock.acquire();
11135 if (FAILED(rc))
11136 {
11137 delete pMediumLockList;
11138 throw rc;
11139 }
11140 rc = lockedMediaMap->Insert(pAtt, pMediumLockList);
11141 if (FAILED(rc))
11142 {
11143 throw setError(rc,
11144 tr("Collecting locking information for all attached media failed"));
11145 }
11146 }
11147 }
11148
11149 /* Now lock all media. If this fails, nothing is locked. */
11150 alock.release();
11151 rc = lockedMediaMap->Lock();
11152 alock.acquire();
11153 if (FAILED(rc))
11154 {
11155 throw setError(rc,
11156 tr("Locking of attached media failed"));
11157 }
11158 }
11159
11160 /* remember the current list (note that we don't use backup() since
11161 * mMediaData may be already backed up) */
11162 MediaData::AttachmentList atts = mMediaData->mAttachments;
11163
11164 /* start from scratch */
11165 mMediaData->mAttachments.clear();
11166
11167 /* go through remembered attachments and create diffs for normal hard
11168 * disks and attach them */
11169 for (MediaData::AttachmentList::const_iterator it = atts.begin();
11170 it != atts.end();
11171 ++it)
11172 {
11173 MediumAttachment* pAtt = *it;
11174
11175 DeviceType_T devType = pAtt->i_getType();
11176 Medium* pMedium = pAtt->i_getMedium();
11177
11178 if ( devType != DeviceType_HardDisk
11179 || pMedium == NULL
11180 || pMedium->i_getType() != MediumType_Normal)
11181 {
11182 /* copy the attachment as is */
11183
11184 /** @todo the progress object created in Console::TakeSnaphot
11185 * only expects operations for hard disks. Later other
11186 * device types need to show up in the progress as well. */
11187 if (devType == DeviceType_HardDisk)
11188 {
11189 if (pMedium == NULL)
11190 aProgress->SetNextOperation(Bstr(tr("Skipping attachment without medium")).raw(),
11191 aWeight); // weight
11192 else
11193 aProgress->SetNextOperation(BstrFmt(tr("Skipping medium '%s'"),
11194 pMedium->i_getBase()->i_getName().c_str()).raw(),
11195 aWeight); // weight
11196 }
11197
11198 mMediaData->mAttachments.push_back(pAtt);
11199 continue;
11200 }
11201
11202 /* need a diff */
11203 aProgress->SetNextOperation(BstrFmt(tr("Creating differencing hard disk for '%s'"),
11204 pMedium->i_getBase()->i_getName().c_str()).raw(),
11205 aWeight); // weight
11206
11207 Utf8Str strFullSnapshotFolder;
11208 calculateFullPath(mUserData->s.strSnapshotFolder, strFullSnapshotFolder);
11209
11210 ComObjPtr<Medium> diff;
11211 diff.createObject();
11212 // store the diff in the same registry as the parent
11213 // (this cannot fail here because we can't create implicit diffs for
11214 // unregistered images)
11215 Guid uuidRegistryParent;
11216 bool fInRegistry = pMedium->i_getFirstRegistryMachineId(uuidRegistryParent);
11217 Assert(fInRegistry); NOREF(fInRegistry);
11218 rc = diff->init(mParent,
11219 pMedium->i_getPreferredDiffFormat(),
11220 strFullSnapshotFolder.append(RTPATH_SLASH_STR),
11221 uuidRegistryParent);
11222 if (FAILED(rc)) throw rc;
11223
11224 /** @todo r=bird: How is the locking and diff image cleaned up if we fail before
11225 * the push_back? Looks like we're going to release medium with the
11226 * wrong kind of lock (general issue with if we fail anywhere at all)
11227 * and an orphaned VDI in the snapshots folder. */
11228
11229 /* update the appropriate lock list */
11230 MediumLockList *pMediumLockList;
11231 rc = lockedMediaMap->Get(pAtt, pMediumLockList);
11232 AssertComRCThrowRC(rc);
11233 if (aOnline)
11234 {
11235 alock.release();
11236 /* The currently attached medium will be read-only, change
11237 * the lock type to read. */
11238 rc = pMediumLockList->Update(pMedium, false);
11239 alock.acquire();
11240 AssertComRCThrowRC(rc);
11241 }
11242
11243 /* release the locks before the potentially lengthy operation */
11244 alock.release();
11245 rc = pMedium->i_createDiffStorage(diff, MediumVariant_Standard,
11246 pMediumLockList,
11247 NULL /* aProgress */,
11248 true /* aWait */);
11249 alock.acquire();
11250 if (FAILED(rc)) throw rc;
11251
11252 /* actual lock list update is done in Medium::commitMedia */
11253
11254 rc = diff->i_addBackReference(mData->mUuid);
11255 AssertComRCThrowRC(rc);
11256
11257 /* add a new attachment */
11258 ComObjPtr<MediumAttachment> attachment;
11259 attachment.createObject();
11260 rc = attachment->init(this,
11261 diff,
11262 pAtt->i_getControllerName(),
11263 pAtt->i_getPort(),
11264 pAtt->i_getDevice(),
11265 DeviceType_HardDisk,
11266 true /* aImplicit */,
11267 false /* aPassthrough */,
11268 false /* aTempEject */,
11269 pAtt->i_getNonRotational(),
11270 pAtt->i_getDiscard(),
11271 pAtt->i_getHotPluggable(),
11272 pAtt->i_getBandwidthGroup());
11273 if (FAILED(rc)) throw rc;
11274
11275 rc = lockedMediaMap->ReplaceKey(pAtt, attachment);
11276 AssertComRCThrowRC(rc);
11277 mMediaData->mAttachments.push_back(attachment);
11278 }
11279 }
11280 catch (HRESULT aRC) { rc = aRC; }
11281
11282 /* unlock all hard disks we locked when there is no VM */
11283 if (!aOnline)
11284 {
11285 ErrorInfoKeeper eik;
11286
11287 HRESULT rc1 = lockedMediaMap->Clear();
11288 AssertComRC(rc1);
11289 }
11290
11291 return rc;
11292}
11293
11294/**
11295 * Deletes implicit differencing hard disks created either by
11296 * #createImplicitDiffs() or by #AttachDevice() and rolls back mMediaData.
11297 *
11298 * Note that to delete hard disks created by #AttachDevice() this method is
11299 * called from #fixupMedia() when the changes are rolled back.
11300 *
11301 * @note Locks this object and the media tree for writing.
11302 */
11303HRESULT Machine::deleteImplicitDiffs(bool aOnline)
11304{
11305 LogFlowThisFunc(("aOnline=%d\n", aOnline));
11306
11307 AutoCaller autoCaller(this);
11308 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
11309
11310 AutoMultiWriteLock2 alock(this->lockHandle(),
11311 &mParent->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
11312
11313 /* We absolutely must have backed up state. */
11314 AssertReturn(mMediaData.isBackedUp(), E_FAIL);
11315
11316 /* Check if there are any implicitly created diff images. */
11317 bool fImplicitDiffs = false;
11318 for (MediaData::AttachmentList::const_iterator it = mMediaData->mAttachments.begin();
11319 it != mMediaData->mAttachments.end();
11320 ++it)
11321 {
11322 const ComObjPtr<MediumAttachment> &pAtt = *it;
11323 if (pAtt->i_isImplicit())
11324 {
11325 fImplicitDiffs = true;
11326 break;
11327 }
11328 }
11329 /* If there is nothing to do, leave early. This saves lots of image locking
11330 * effort. It also avoids a MachineStateChanged event without real reason.
11331 * This is important e.g. when loading a VM config, because there should be
11332 * no events. Otherwise API clients can become thoroughly confused for
11333 * inaccessible VMs (the code for loading VM configs uses this method for
11334 * cleanup if the config makes no sense), as they take such events as an
11335 * indication that the VM is alive, and they would force the VM config to
11336 * be reread, leading to an endless loop. */
11337 if (!fImplicitDiffs)
11338 return S_OK;
11339
11340 HRESULT rc = S_OK;
11341 MachineState_T oldState = mData->mMachineState;
11342
11343 /* will release the lock before the potentially lengthy operation,
11344 * so protect with the special state (unless already protected) */
11345 if ( oldState != MachineState_Saving
11346 && oldState != MachineState_LiveSnapshotting
11347 && oldState != MachineState_RestoringSnapshot
11348 && oldState != MachineState_DeletingSnapshot
11349 && oldState != MachineState_DeletingSnapshotOnline
11350 && oldState != MachineState_DeletingSnapshotPaused
11351 )
11352 setMachineState(MachineState_SettingUp);
11353
11354 // use appropriate locked media map (online or offline)
11355 MediumLockListMap lockedMediaOffline;
11356 MediumLockListMap *lockedMediaMap;
11357 if (aOnline)
11358 lockedMediaMap = &mData->mSession.mLockedMedia;
11359 else
11360 lockedMediaMap = &lockedMediaOffline;
11361
11362 try
11363 {
11364 if (!aOnline)
11365 {
11366 /* lock all attached hard disks early to detect "in use"
11367 * situations before deleting actual diffs */
11368 for (MediaData::AttachmentList::const_iterator it = mMediaData->mAttachments.begin();
11369 it != mMediaData->mAttachments.end();
11370 ++it)
11371 {
11372 MediumAttachment* pAtt = *it;
11373 if (pAtt->i_getType() == DeviceType_HardDisk)
11374 {
11375 Medium* pMedium = pAtt->i_getMedium();
11376 Assert(pMedium);
11377
11378 MediumLockList *pMediumLockList(new MediumLockList());
11379 alock.release();
11380 rc = pMedium->i_createMediumLockList(true /* fFailIfInaccessible */,
11381 false /* fMediumLockWrite */,
11382 NULL,
11383 *pMediumLockList);
11384 alock.acquire();
11385
11386 if (FAILED(rc))
11387 {
11388 delete pMediumLockList;
11389 throw rc;
11390 }
11391
11392 rc = lockedMediaMap->Insert(pAtt, pMediumLockList);
11393 if (FAILED(rc))
11394 throw rc;
11395 }
11396 }
11397
11398 if (FAILED(rc))
11399 throw rc;
11400 } // end of offline
11401
11402 /* Lock lists are now up to date and include implicitly created media */
11403
11404 /* Go through remembered attachments and delete all implicitly created
11405 * diffs and fix up the attachment information */
11406 const MediaData::AttachmentList &oldAtts = mMediaData.backedUpData()->mAttachments;
11407 MediaData::AttachmentList implicitAtts;
11408 for (MediaData::AttachmentList::const_iterator it = mMediaData->mAttachments.begin();
11409 it != mMediaData->mAttachments.end();
11410 ++it)
11411 {
11412 ComObjPtr<MediumAttachment> pAtt = *it;
11413 ComObjPtr<Medium> pMedium = pAtt->i_getMedium();
11414 if (pMedium.isNull())
11415 continue;
11416
11417 // Implicit attachments go on the list for deletion and back references are removed.
11418 if (pAtt->i_isImplicit())
11419 {
11420 /* Deassociate and mark for deletion */
11421 LogFlowThisFunc(("Detaching '%s', pending deletion\n", pAtt->i_getLogName()));
11422 rc = pMedium->i_removeBackReference(mData->mUuid);
11423 if (FAILED(rc))
11424 throw rc;
11425 implicitAtts.push_back(pAtt);
11426 continue;
11427 }
11428
11429 /* Was this medium attached before? */
11430 if (!findAttachment(oldAtts, pMedium))
11431 {
11432 /* no: de-associate */
11433 LogFlowThisFunc(("Detaching '%s', no deletion\n", pAtt->i_getLogName()));
11434 rc = pMedium->i_removeBackReference(mData->mUuid);
11435 if (FAILED(rc))
11436 throw rc;
11437 continue;
11438 }
11439 LogFlowThisFunc(("Not detaching '%s'\n", pAtt->i_getLogName()));
11440 }
11441
11442 /* If there are implicit attachments to delete, throw away the lock
11443 * map contents (which will unlock all media) since the medium
11444 * attachments will be rolled back. Below we need to completely
11445 * recreate the lock map anyway since it is infinitely complex to
11446 * do this incrementally (would need reconstructing each attachment
11447 * change, which would be extremely hairy). */
11448 if (implicitAtts.size() != 0)
11449 {
11450 ErrorInfoKeeper eik;
11451
11452 HRESULT rc1 = lockedMediaMap->Clear();
11453 AssertComRC(rc1);
11454 }
11455
11456 /* rollback hard disk changes */
11457 mMediaData.rollback();
11458
11459 MultiResult mrc(S_OK);
11460
11461 // Delete unused implicit diffs.
11462 if (implicitAtts.size() != 0)
11463 {
11464 alock.release();
11465
11466 for (MediaData::AttachmentList::const_iterator it = implicitAtts.begin();
11467 it != implicitAtts.end();
11468 ++it)
11469 {
11470 // Remove medium associated with this attachment.
11471 ComObjPtr<MediumAttachment> pAtt = *it;
11472 Assert(pAtt);
11473 LogFlowThisFunc(("Deleting '%s'\n", pAtt->i_getLogName()));
11474 ComObjPtr<Medium> pMedium = pAtt->i_getMedium();
11475 Assert(pMedium);
11476
11477 rc = pMedium->i_deleteStorage(NULL /*aProgress*/, true /*aWait*/);
11478 // continue on delete failure, just collect error messages
11479 AssertMsg(SUCCEEDED(rc), ("rc=%Rhrc it=%s hd=%s\n", rc, pAtt->i_getLogName(), pMedium->i_getLocationFull().c_str() ));
11480 mrc = rc;
11481 }
11482
11483 alock.acquire();
11484
11485 /* if there is a VM recreate media lock map as mentioned above,
11486 * otherwise it is a waste of time and we leave things unlocked */
11487 if (aOnline)
11488 {
11489 const ComObjPtr<SessionMachine> pMachine = mData->mSession.mMachine;
11490 /* must never be NULL, but better safe than sorry */
11491 if (!pMachine.isNull())
11492 {
11493 alock.release();
11494 rc = mData->mSession.mMachine->lockMedia();
11495 alock.acquire();
11496 if (FAILED(rc))
11497 throw rc;
11498 }
11499 }
11500 }
11501 }
11502 catch (HRESULT aRC) {rc = aRC;}
11503
11504 if (mData->mMachineState == MachineState_SettingUp)
11505 setMachineState(oldState);
11506
11507 /* unlock all hard disks we locked when there is no VM */
11508 if (!aOnline)
11509 {
11510 ErrorInfoKeeper eik;
11511
11512 HRESULT rc1 = lockedMediaMap->Clear();
11513 AssertComRC(rc1);
11514 }
11515
11516 return rc;
11517}
11518
11519
11520/**
11521 * Looks through the given list of media attachments for one with the given parameters
11522 * and returns it, or NULL if not found. The list is a parameter so that backup lists
11523 * can be searched as well if needed.
11524 *
11525 * @param list
11526 * @param aControllerName
11527 * @param aControllerPort
11528 * @param aDevice
11529 * @return
11530 */
11531MediumAttachment* Machine::findAttachment(const MediaData::AttachmentList &ll,
11532 IN_BSTR aControllerName,
11533 LONG aControllerPort,
11534 LONG aDevice)
11535{
11536 for (MediaData::AttachmentList::const_iterator it = ll.begin();
11537 it != ll.end();
11538 ++it)
11539 {
11540 MediumAttachment *pAttach = *it;
11541 if (pAttach->i_matches(aControllerName, aControllerPort, aDevice))
11542 return pAttach;
11543 }
11544
11545 return NULL;
11546}
11547
11548/**
11549 * Looks through the given list of media attachments for one with the given parameters
11550 * and returns it, or NULL if not found. The list is a parameter so that backup lists
11551 * can be searched as well if needed.
11552 *
11553 * @param list
11554 * @param aControllerName
11555 * @param aControllerPort
11556 * @param aDevice
11557 * @return
11558 */
11559MediumAttachment* Machine::findAttachment(const MediaData::AttachmentList &ll,
11560 ComObjPtr<Medium> pMedium)
11561{
11562 for (MediaData::AttachmentList::const_iterator it = ll.begin();
11563 it != ll.end();
11564 ++it)
11565 {
11566 MediumAttachment *pAttach = *it;
11567 ComObjPtr<Medium> pMediumThis = pAttach->i_getMedium();
11568 if (pMediumThis == pMedium)
11569 return pAttach;
11570 }
11571
11572 return NULL;
11573}
11574
11575/**
11576 * Looks through the given list of media attachments for one with the given parameters
11577 * and returns it, or NULL if not found. The list is a parameter so that backup lists
11578 * can be searched as well if needed.
11579 *
11580 * @param list
11581 * @param aControllerName
11582 * @param aControllerPort
11583 * @param aDevice
11584 * @return
11585 */
11586MediumAttachment* Machine::findAttachment(const MediaData::AttachmentList &ll,
11587 Guid &id)
11588{
11589 for (MediaData::AttachmentList::const_iterator it = ll.begin();
11590 it != ll.end();
11591 ++it)
11592 {
11593 MediumAttachment *pAttach = *it;
11594 ComObjPtr<Medium> pMediumThis = pAttach->i_getMedium();
11595 if (pMediumThis->i_getId() == id)
11596 return pAttach;
11597 }
11598
11599 return NULL;
11600}
11601
11602/**
11603 * Main implementation for Machine::DetachDevice. This also gets called
11604 * from Machine::prepareUnregister() so it has been taken out for simplicity.
11605 *
11606 * @param pAttach Medium attachment to detach.
11607 * @param writeLock Machine write lock which the caller must have locked once. This may be released temporarily in here.
11608 * @param pSnapshot If NULL, then the detachment is for the current machine. Otherwise this is for a SnapshotMachine, and this must be its snapshot.
11609 * @return
11610 */
11611HRESULT Machine::detachDevice(MediumAttachment *pAttach,
11612 AutoWriteLock &writeLock,
11613 Snapshot *pSnapshot)
11614{
11615 ComObjPtr<Medium> oldmedium = pAttach->i_getMedium();
11616 DeviceType_T mediumType = pAttach->i_getType();
11617
11618 LogFlowThisFunc(("Entering, medium of attachment is %s\n", oldmedium ? oldmedium->i_getLocationFull().c_str() : "NULL"));
11619
11620 if (pAttach->i_isImplicit())
11621 {
11622 /* attempt to implicitly delete the implicitly created diff */
11623
11624 /// @todo move the implicit flag from MediumAttachment to Medium
11625 /// and forbid any hard disk operation when it is implicit. Or maybe
11626 /// a special media state for it to make it even more simple.
11627
11628 Assert(mMediaData.isBackedUp());
11629
11630 /* will release the lock before the potentially lengthy operation, so
11631 * protect with the special state */
11632 MachineState_T oldState = mData->mMachineState;
11633 setMachineState(MachineState_SettingUp);
11634
11635 writeLock.release();
11636
11637 HRESULT rc = oldmedium->i_deleteStorage(NULL /*aProgress*/,
11638 true /*aWait*/);
11639
11640 writeLock.acquire();
11641
11642 setMachineState(oldState);
11643
11644 if (FAILED(rc)) return rc;
11645 }
11646
11647 setModified(IsModified_Storage);
11648 mMediaData.backup();
11649 mMediaData->mAttachments.remove(pAttach);
11650
11651 if (!oldmedium.isNull())
11652 {
11653 // if this is from a snapshot, do not defer detachment to commitMedia()
11654 if (pSnapshot)
11655 oldmedium->i_removeBackReference(mData->mUuid, pSnapshot->i_getId());
11656 // else if non-hard disk media, do not defer detachment to commitMedia() either
11657 else if (mediumType != DeviceType_HardDisk)
11658 oldmedium->i_removeBackReference(mData->mUuid);
11659 }
11660
11661 return S_OK;
11662}
11663
11664/**
11665 * Goes thru all media of the given list and
11666 *
11667 * 1) calls detachDevice() on each of them for this machine and
11668 * 2) adds all Medium objects found in the process to the given list,
11669 * depending on cleanupMode.
11670 *
11671 * If cleanupMode is CleanupMode_DetachAllReturnHardDisksOnly, this only
11672 * adds hard disks to the list. If it is CleanupMode_Full, this adds all
11673 * media to the list.
11674 *
11675 * This gets called from Machine::Unregister, both for the actual Machine and
11676 * the SnapshotMachine objects that might be found in the snapshots.
11677 *
11678 * Requires caller and locking. The machine lock must be passed in because it
11679 * will be passed on to detachDevice which needs it for temporary unlocking.
11680 *
11681 * @param writeLock Machine lock from top-level caller; this gets passed to detachDevice.
11682 * @param pSnapshot Must be NULL when called for a "real" Machine or a snapshot object if called for a SnapshotMachine.
11683 * @param cleanupMode If DetachAllReturnHardDisksOnly, only hard disk media get added to llMedia; if Full, then all media get added;
11684 * otherwise no media get added.
11685 * @param llMedia Caller's list to receive Medium objects which got detached so caller can close() them, depending on cleanupMode.
11686 * @return
11687 */
11688HRESULT Machine::detachAllMedia(AutoWriteLock &writeLock,
11689 Snapshot *pSnapshot,
11690 CleanupMode_T cleanupMode,
11691 MediaList &llMedia)
11692{
11693 Assert(isWriteLockOnCurrentThread());
11694
11695 HRESULT rc;
11696
11697 // make a temporary list because detachDevice invalidates iterators into
11698 // mMediaData->mAttachments
11699 MediaData::AttachmentList llAttachments2 = mMediaData->mAttachments;
11700
11701 for (MediaData::AttachmentList::iterator it = llAttachments2.begin();
11702 it != llAttachments2.end();
11703 ++it)
11704 {
11705 ComObjPtr<MediumAttachment> &pAttach = *it;
11706 ComObjPtr<Medium> pMedium = pAttach->i_getMedium();
11707
11708 if (!pMedium.isNull())
11709 {
11710 AutoCaller mac(pMedium);
11711 if (FAILED(mac.rc())) return mac.rc();
11712 AutoReadLock lock(pMedium COMMA_LOCKVAL_SRC_POS);
11713 DeviceType_T devType = pMedium->i_getDeviceType();
11714 if ( ( cleanupMode == CleanupMode_DetachAllReturnHardDisksOnly
11715 && devType == DeviceType_HardDisk)
11716 || (cleanupMode == CleanupMode_Full)
11717 )
11718 {
11719 llMedia.push_back(pMedium);
11720 ComObjPtr<Medium> pParent = pMedium->i_getParent();
11721 /*
11722 * Search for medias which are not attached to any machine, but
11723 * in the chain to an attached disk. Mediums are only consided
11724 * if they are:
11725 * - have only one child
11726 * - no references to any machines
11727 * - are of normal medium type
11728 */
11729 while (!pParent.isNull())
11730 {
11731 AutoCaller mac1(pParent);
11732 if (FAILED(mac1.rc())) return mac1.rc();
11733 AutoReadLock lock1(pParent COMMA_LOCKVAL_SRC_POS);
11734 if (pParent->i_getChildren().size() == 1)
11735 {
11736 if ( pParent->i_getMachineBackRefCount() == 0
11737 && pParent->i_getType() == MediumType_Normal
11738 && find(llMedia.begin(), llMedia.end(), pParent) == llMedia.end())
11739 llMedia.push_back(pParent);
11740 }
11741 else
11742 break;
11743 pParent = pParent->i_getParent();
11744 }
11745 }
11746 }
11747
11748 // real machine: then we need to use the proper method
11749 rc = detachDevice(pAttach, writeLock, pSnapshot);
11750
11751 if (FAILED(rc))
11752 return rc;
11753 }
11754
11755 return S_OK;
11756}
11757
11758/**
11759 * Perform deferred hard disk detachments.
11760 *
11761 * Does nothing if the hard disk attachment data (mMediaData) is not changed (not
11762 * backed up).
11763 *
11764 * If @a aOnline is @c true then this method will also unlock the old hard disks
11765 * for which the new implicit diffs were created and will lock these new diffs for
11766 * writing.
11767 *
11768 * @param aOnline Whether the VM was online prior to this operation.
11769 *
11770 * @note Locks this object for writing!
11771 */
11772void Machine::commitMedia(bool aOnline /*= false*/)
11773{
11774 AutoCaller autoCaller(this);
11775 AssertComRCReturnVoid(autoCaller.rc());
11776
11777 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
11778
11779 LogFlowThisFunc(("Entering, aOnline=%d\n", aOnline));
11780
11781 HRESULT rc = S_OK;
11782
11783 /* no attach/detach operations -- nothing to do */
11784 if (!mMediaData.isBackedUp())
11785 return;
11786
11787 MediaData::AttachmentList &oldAtts = mMediaData.backedUpData()->mAttachments;
11788 bool fMediaNeedsLocking = false;
11789
11790 /* enumerate new attachments */
11791 for (MediaData::AttachmentList::const_iterator it = mMediaData->mAttachments.begin();
11792 it != mMediaData->mAttachments.end();
11793 ++it)
11794 {
11795 MediumAttachment *pAttach = *it;
11796
11797 pAttach->i_commit();
11798
11799 Medium* pMedium = pAttach->i_getMedium();
11800 bool fImplicit = pAttach->i_isImplicit();
11801
11802 LogFlowThisFunc(("Examining current medium '%s' (implicit: %d)\n",
11803 (pMedium) ? pMedium->i_getName().c_str() : "NULL",
11804 fImplicit));
11805
11806 /** @todo convert all this Machine-based voodoo to MediumAttachment
11807 * based commit logic. */
11808 if (fImplicit)
11809 {
11810 /* convert implicit attachment to normal */
11811 pAttach->i_setImplicit(false);
11812
11813 if ( aOnline
11814 && pMedium
11815 && pAttach->i_getType() == DeviceType_HardDisk
11816 )
11817 {
11818 ComObjPtr<Medium> parent = pMedium->i_getParent();
11819 AutoWriteLock parentLock(parent COMMA_LOCKVAL_SRC_POS);
11820
11821 /* update the appropriate lock list */
11822 MediumLockList *pMediumLockList;
11823 rc = mData->mSession.mLockedMedia.Get(pAttach, pMediumLockList);
11824 AssertComRC(rc);
11825 if (pMediumLockList)
11826 {
11827 /* unlock if there's a need to change the locking */
11828 if (!fMediaNeedsLocking)
11829 {
11830 rc = mData->mSession.mLockedMedia.Unlock();
11831 AssertComRC(rc);
11832 fMediaNeedsLocking = true;
11833 }
11834 rc = pMediumLockList->Update(parent, false);
11835 AssertComRC(rc);
11836 rc = pMediumLockList->Append(pMedium, true);
11837 AssertComRC(rc);
11838 }
11839 }
11840
11841 continue;
11842 }
11843
11844 if (pMedium)
11845 {
11846 /* was this medium attached before? */
11847 for (MediaData::AttachmentList::iterator oldIt = oldAtts.begin();
11848 oldIt != oldAtts.end();
11849 ++oldIt)
11850 {
11851 MediumAttachment *pOldAttach = *oldIt;
11852 if (pOldAttach->i_getMedium() == pMedium)
11853 {
11854 LogFlowThisFunc(("--> medium '%s' was attached before, will not remove\n", pMedium->i_getName().c_str()));
11855
11856 /* yes: remove from old to avoid de-association */
11857 oldAtts.erase(oldIt);
11858 break;
11859 }
11860 }
11861 }
11862 }
11863
11864 /* enumerate remaining old attachments and de-associate from the
11865 * current machine state */
11866 for (MediaData::AttachmentList::const_iterator it = oldAtts.begin();
11867 it != oldAtts.end();
11868 ++it)
11869 {
11870 MediumAttachment *pAttach = *it;
11871 Medium* pMedium = pAttach->i_getMedium();
11872
11873 /* Detach only hard disks, since DVD/floppy media is detached
11874 * instantly in MountMedium. */
11875 if (pAttach->i_getType() == DeviceType_HardDisk && pMedium)
11876 {
11877 LogFlowThisFunc(("detaching medium '%s' from machine\n", pMedium->i_getName().c_str()));
11878
11879 /* now de-associate from the current machine state */
11880 rc = pMedium->i_removeBackReference(mData->mUuid);
11881 AssertComRC(rc);
11882
11883 if (aOnline)
11884 {
11885 /* unlock since medium is not used anymore */
11886 MediumLockList *pMediumLockList;
11887 rc = mData->mSession.mLockedMedia.Get(pAttach, pMediumLockList);
11888 if (RT_UNLIKELY(rc == VBOX_E_INVALID_OBJECT_STATE))
11889 {
11890 /* this happens for online snapshots, there the attachment
11891 * is changing, but only to a diff image created under
11892 * the old one, so there is no separate lock list */
11893 Assert(!pMediumLockList);
11894 }
11895 else
11896 {
11897 AssertComRC(rc);
11898 if (pMediumLockList)
11899 {
11900 rc = mData->mSession.mLockedMedia.Remove(pAttach);
11901 AssertComRC(rc);
11902 }
11903 }
11904 }
11905 }
11906 }
11907
11908 /* take media locks again so that the locking state is consistent */
11909 if (fMediaNeedsLocking)
11910 {
11911 Assert(aOnline);
11912 rc = mData->mSession.mLockedMedia.Lock();
11913 AssertComRC(rc);
11914 }
11915
11916 /* commit the hard disk changes */
11917 mMediaData.commit();
11918
11919 if (isSessionMachine())
11920 {
11921 /*
11922 * Update the parent machine to point to the new owner.
11923 * This is necessary because the stored parent will point to the
11924 * session machine otherwise and cause crashes or errors later
11925 * when the session machine gets invalid.
11926 */
11927 /** @todo Change the MediumAttachment class to behave like any other
11928 * class in this regard by creating peer MediumAttachment
11929 * objects for session machines and share the data with the peer
11930 * machine.
11931 */
11932 for (MediaData::AttachmentList::const_iterator it = mMediaData->mAttachments.begin();
11933 it != mMediaData->mAttachments.end();
11934 ++it)
11935 {
11936 (*it)->i_updateParentMachine(mPeer);
11937 }
11938
11939 /* attach new data to the primary machine and reshare it */
11940 mPeer->mMediaData.attach(mMediaData);
11941 }
11942
11943 return;
11944}
11945
11946/**
11947 * Perform deferred deletion of implicitly created diffs.
11948 *
11949 * Does nothing if the hard disk attachment data (mMediaData) is not changed (not
11950 * backed up).
11951 *
11952 * @note Locks this object for writing!
11953 */
11954void Machine::rollbackMedia()
11955{
11956 AutoCaller autoCaller(this);
11957 AssertComRCReturnVoid(autoCaller.rc());
11958
11959 // AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
11960 LogFlowThisFunc(("Entering rollbackMedia\n"));
11961
11962 HRESULT rc = S_OK;
11963
11964 /* no attach/detach operations -- nothing to do */
11965 if (!mMediaData.isBackedUp())
11966 return;
11967
11968 /* enumerate new attachments */
11969 for (MediaData::AttachmentList::const_iterator it = mMediaData->mAttachments.begin();
11970 it != mMediaData->mAttachments.end();
11971 ++it)
11972 {
11973 MediumAttachment *pAttach = *it;
11974 /* Fix up the backrefs for DVD/floppy media. */
11975 if (pAttach->i_getType() != DeviceType_HardDisk)
11976 {
11977 Medium* pMedium = pAttach->i_getMedium();
11978 if (pMedium)
11979 {
11980 rc = pMedium->i_removeBackReference(mData->mUuid);
11981 AssertComRC(rc);
11982 }
11983 }
11984
11985 (*it)->i_rollback();
11986
11987 pAttach = *it;
11988 /* Fix up the backrefs for DVD/floppy media. */
11989 if (pAttach->i_getType() != DeviceType_HardDisk)
11990 {
11991 Medium* pMedium = pAttach->i_getMedium();
11992 if (pMedium)
11993 {
11994 rc = pMedium->i_addBackReference(mData->mUuid);
11995 AssertComRC(rc);
11996 }
11997 }
11998 }
11999
12000 /** @todo convert all this Machine-based voodoo to MediumAttachment
12001 * based rollback logic. */
12002 deleteImplicitDiffs(Global::IsOnline(mData->mMachineState));
12003
12004 return;
12005}
12006
12007/**
12008 * Returns true if the settings file is located in the directory named exactly
12009 * as the machine; this means, among other things, that the machine directory
12010 * should be auto-renamed.
12011 *
12012 * @param aSettingsDir if not NULL, the full machine settings file directory
12013 * name will be assigned there.
12014 *
12015 * @note Doesn't lock anything.
12016 * @note Not thread safe (must be called from this object's lock).
12017 */
12018bool Machine::isInOwnDir(Utf8Str *aSettingsDir /* = NULL */) const
12019{
12020 Utf8Str strMachineDirName(mData->m_strConfigFileFull); // path/to/machinesfolder/vmname/vmname.vbox
12021 strMachineDirName.stripFilename(); // path/to/machinesfolder/vmname
12022 if (aSettingsDir)
12023 *aSettingsDir = strMachineDirName;
12024 strMachineDirName.stripPath(); // vmname
12025 Utf8Str strConfigFileOnly(mData->m_strConfigFileFull); // path/to/machinesfolder/vmname/vmname.vbox
12026 strConfigFileOnly.stripPath() // vmname.vbox
12027 .stripSuffix(); // vmname
12028 /** @todo hack, make somehow use of ComposeMachineFilename */
12029 if (mUserData->s.fDirectoryIncludesUUID)
12030 strConfigFileOnly += Utf8StrFmt(" (%RTuuid)", mData->mUuid.raw());
12031
12032 AssertReturn(!strMachineDirName.isEmpty(), false);
12033 AssertReturn(!strConfigFileOnly.isEmpty(), false);
12034
12035 return strMachineDirName == strConfigFileOnly;
12036}
12037
12038/**
12039 * Discards all changes to machine settings.
12040 *
12041 * @param aNotify Whether to notify the direct session about changes or not.
12042 *
12043 * @note Locks objects for writing!
12044 */
12045void Machine::rollback(bool aNotify)
12046{
12047 AutoCaller autoCaller(this);
12048 AssertComRCReturn(autoCaller.rc(), (void)0);
12049
12050 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
12051
12052 if (!mStorageControllers.isNull())
12053 {
12054 if (mStorageControllers.isBackedUp())
12055 {
12056 /* unitialize all new devices (absent in the backed up list). */
12057 StorageControllerList::const_iterator it = mStorageControllers->begin();
12058 StorageControllerList *backedList = mStorageControllers.backedUpData();
12059 while (it != mStorageControllers->end())
12060 {
12061 if ( std::find(backedList->begin(), backedList->end(), *it)
12062 == backedList->end()
12063 )
12064 {
12065 (*it)->uninit();
12066 }
12067 ++it;
12068 }
12069
12070 /* restore the list */
12071 mStorageControllers.rollback();
12072 }
12073
12074 /* rollback any changes to devices after restoring the list */
12075 if (mData->flModifications & IsModified_Storage)
12076 {
12077 StorageControllerList::const_iterator it = mStorageControllers->begin();
12078 while (it != mStorageControllers->end())
12079 {
12080 (*it)->i_rollback();
12081 ++it;
12082 }
12083 }
12084 }
12085
12086 if (!mUSBControllers.isNull())
12087 {
12088 if (mUSBControllers.isBackedUp())
12089 {
12090 /* unitialize all new devices (absent in the backed up list). */
12091 USBControllerList::const_iterator it = mUSBControllers->begin();
12092 USBControllerList *backedList = mUSBControllers.backedUpData();
12093 while (it != mUSBControllers->end())
12094 {
12095 if ( std::find(backedList->begin(), backedList->end(), *it)
12096 == backedList->end()
12097 )
12098 {
12099 (*it)->uninit();
12100 }
12101 ++it;
12102 }
12103
12104 /* restore the list */
12105 mUSBControllers.rollback();
12106 }
12107
12108 /* rollback any changes to devices after restoring the list */
12109 if (mData->flModifications & IsModified_USB)
12110 {
12111 USBControllerList::const_iterator it = mUSBControllers->begin();
12112 while (it != mUSBControllers->end())
12113 {
12114 (*it)->i_rollback();
12115 ++it;
12116 }
12117 }
12118 }
12119
12120 mUserData.rollback();
12121
12122 mHWData.rollback();
12123
12124 if (mData->flModifications & IsModified_Storage)
12125 rollbackMedia();
12126
12127 if (mBIOSSettings)
12128 mBIOSSettings->i_rollback();
12129
12130 if (mVRDEServer && (mData->flModifications & IsModified_VRDEServer))
12131 mVRDEServer->i_rollback();
12132
12133 if (mAudioAdapter)
12134 mAudioAdapter->i_rollback();
12135
12136 if (mUSBDeviceFilters && (mData->flModifications & IsModified_USB))
12137 mUSBDeviceFilters->i_rollback();
12138
12139 if (mBandwidthControl && (mData->flModifications & IsModified_BandwidthControl))
12140 mBandwidthControl->i_rollback();
12141
12142 if (!mHWData.isNull())
12143 mNetworkAdapters.resize(Global::getMaxNetworkAdapters(mHWData->mChipsetType));
12144 NetworkAdapterVector networkAdapters(mNetworkAdapters.size());
12145 ComPtr<ISerialPort> serialPorts[RT_ELEMENTS(mSerialPorts)];
12146 ComPtr<IParallelPort> parallelPorts[RT_ELEMENTS(mParallelPorts)];
12147
12148 if (mData->flModifications & IsModified_NetworkAdapters)
12149 for (ULONG slot = 0; slot < mNetworkAdapters.size(); slot++)
12150 if ( mNetworkAdapters[slot]
12151 && mNetworkAdapters[slot]->i_isModified())
12152 {
12153 mNetworkAdapters[slot]->i_rollback();
12154 networkAdapters[slot] = mNetworkAdapters[slot];
12155 }
12156
12157 if (mData->flModifications & IsModified_SerialPorts)
12158 for (ULONG slot = 0; slot < RT_ELEMENTS(mSerialPorts); slot++)
12159 if ( mSerialPorts[slot]
12160 && mSerialPorts[slot]->i_isModified())
12161 {
12162 mSerialPorts[slot]->i_rollback();
12163 serialPorts[slot] = mSerialPorts[slot];
12164 }
12165
12166 if (mData->flModifications & IsModified_ParallelPorts)
12167 for (ULONG slot = 0; slot < RT_ELEMENTS(mParallelPorts); slot++)
12168 if ( mParallelPorts[slot]
12169 && mParallelPorts[slot]->i_isModified())
12170 {
12171 mParallelPorts[slot]->i_rollback();
12172 parallelPorts[slot] = mParallelPorts[slot];
12173 }
12174
12175 if (aNotify)
12176 {
12177 /* inform the direct session about changes */
12178
12179 ComObjPtr<Machine> that = this;
12180 uint32_t flModifications = mData->flModifications;
12181 alock.release();
12182
12183 if (flModifications & IsModified_SharedFolders)
12184 that->onSharedFolderChange();
12185
12186 if (flModifications & IsModified_VRDEServer)
12187 that->onVRDEServerChange(/* aRestart */ TRUE);
12188 if (flModifications & IsModified_USB)
12189 that->onUSBControllerChange();
12190
12191 for (ULONG slot = 0; slot < networkAdapters.size(); slot++)
12192 if (networkAdapters[slot])
12193 that->onNetworkAdapterChange(networkAdapters[slot], FALSE);
12194 for (ULONG slot = 0; slot < RT_ELEMENTS(serialPorts); slot++)
12195 if (serialPorts[slot])
12196 that->onSerialPortChange(serialPorts[slot]);
12197 for (ULONG slot = 0; slot < RT_ELEMENTS(parallelPorts); slot++)
12198 if (parallelPorts[slot])
12199 that->onParallelPortChange(parallelPorts[slot]);
12200
12201 if (flModifications & IsModified_Storage)
12202 that->onStorageControllerChange();
12203
12204#if 0
12205 if (flModifications & IsModified_BandwidthControl)
12206 that->onBandwidthControlChange();
12207#endif
12208 }
12209}
12210
12211/**
12212 * Commits all the changes to machine settings.
12213 *
12214 * Note that this operation is supposed to never fail.
12215 *
12216 * @note Locks this object and children for writing.
12217 */
12218void Machine::commit()
12219{
12220 AutoCaller autoCaller(this);
12221 AssertComRCReturnVoid(autoCaller.rc());
12222
12223 AutoCaller peerCaller(mPeer);
12224 AssertComRCReturnVoid(peerCaller.rc());
12225
12226 AutoMultiWriteLock2 alock(mPeer, this COMMA_LOCKVAL_SRC_POS);
12227
12228 /*
12229 * use safe commit to ensure Snapshot machines (that share mUserData)
12230 * will still refer to a valid memory location
12231 */
12232 mUserData.commitCopy();
12233
12234 mHWData.commit();
12235
12236 if (mMediaData.isBackedUp())
12237 commitMedia(Global::IsOnline(mData->mMachineState));
12238
12239 mBIOSSettings->i_commit();
12240 mVRDEServer->i_commit();
12241 mAudioAdapter->i_commit();
12242 mUSBDeviceFilters->i_commit();
12243 mBandwidthControl->i_commit();
12244
12245 /* Since mNetworkAdapters is a list which might have been changed (resized)
12246 * without using the Backupable<> template we need to handle the copying
12247 * of the list entries manually, including the creation of peers for the
12248 * new objects. */
12249 bool commitNetworkAdapters = false;
12250 size_t newSize = Global::getMaxNetworkAdapters(mHWData->mChipsetType);
12251 if (mPeer)
12252 {
12253 /* commit everything, even the ones which will go away */
12254 for (size_t slot = 0; slot < mNetworkAdapters.size(); slot++)
12255 mNetworkAdapters[slot]->i_commit();
12256 /* copy over the new entries, creating a peer and uninit the original */
12257 mPeer->mNetworkAdapters.resize(RT_MAX(newSize, mPeer->mNetworkAdapters.size()));
12258 for (size_t slot = 0; slot < newSize; slot++)
12259 {
12260 /* look if this adapter has a peer device */
12261 ComObjPtr<NetworkAdapter> peer = mNetworkAdapters[slot]->i_getPeer();
12262 if (!peer)
12263 {
12264 /* no peer means the adapter is a newly created one;
12265 * create a peer owning data this data share it with */
12266 peer.createObject();
12267 peer->init(mPeer, mNetworkAdapters[slot], true /* aReshare */);
12268 }
12269 mPeer->mNetworkAdapters[slot] = peer;
12270 }
12271 /* uninit any no longer needed network adapters */
12272 for (size_t slot = newSize; slot < mNetworkAdapters.size(); slot++)
12273 mNetworkAdapters[slot]->uninit();
12274 for (size_t slot = newSize; slot < mPeer->mNetworkAdapters.size(); slot++)
12275 {
12276 if (mPeer->mNetworkAdapters[slot])
12277 mPeer->mNetworkAdapters[slot]->uninit();
12278 }
12279 /* Keep the original network adapter count until this point, so that
12280 * discarding a chipset type change will not lose settings. */
12281 mNetworkAdapters.resize(newSize);
12282 mPeer->mNetworkAdapters.resize(newSize);
12283 }
12284 else
12285 {
12286 /* we have no peer (our parent is the newly created machine);
12287 * just commit changes to the network adapters */
12288 commitNetworkAdapters = true;
12289 }
12290 if (commitNetworkAdapters)
12291 {
12292 for (size_t slot = 0; slot < mNetworkAdapters.size(); slot++)
12293 mNetworkAdapters[slot]->i_commit();
12294 }
12295
12296 for (ULONG slot = 0; slot < RT_ELEMENTS(mSerialPorts); slot++)
12297 mSerialPorts[slot]->i_commit();
12298 for (ULONG slot = 0; slot < RT_ELEMENTS(mParallelPorts); slot++)
12299 mParallelPorts[slot]->i_commit();
12300
12301 bool commitStorageControllers = false;
12302
12303 if (mStorageControllers.isBackedUp())
12304 {
12305 mStorageControllers.commit();
12306
12307 if (mPeer)
12308 {
12309 /* Commit all changes to new controllers (this will reshare data with
12310 * peers for those who have peers) */
12311 StorageControllerList *newList = new StorageControllerList();
12312 StorageControllerList::const_iterator it = mStorageControllers->begin();
12313 while (it != mStorageControllers->end())
12314 {
12315 (*it)->i_commit();
12316
12317 /* look if this controller has a peer device */
12318 ComObjPtr<StorageController> peer = (*it)->i_getPeer();
12319 if (!peer)
12320 {
12321 /* no peer means the device is a newly created one;
12322 * create a peer owning data this device share it with */
12323 peer.createObject();
12324 peer->init(mPeer, *it, true /* aReshare */);
12325 }
12326 else
12327 {
12328 /* remove peer from the old list */
12329 mPeer->mStorageControllers->remove(peer);
12330 }
12331 /* and add it to the new list */
12332 newList->push_back(peer);
12333
12334 ++it;
12335 }
12336
12337 /* uninit old peer's controllers that are left */
12338 it = mPeer->mStorageControllers->begin();
12339 while (it != mPeer->mStorageControllers->end())
12340 {
12341 (*it)->uninit();
12342 ++it;
12343 }
12344
12345 /* attach new list of controllers to our peer */
12346 mPeer->mStorageControllers.attach(newList);
12347 }
12348 else
12349 {
12350 /* we have no peer (our parent is the newly created machine);
12351 * just commit changes to devices */
12352 commitStorageControllers = true;
12353 }
12354 }
12355 else
12356 {
12357 /* the list of controllers itself is not changed,
12358 * just commit changes to controllers themselves */
12359 commitStorageControllers = true;
12360 }
12361
12362 if (commitStorageControllers)
12363 {
12364 StorageControllerList::const_iterator it = mStorageControllers->begin();
12365 while (it != mStorageControllers->end())
12366 {
12367 (*it)->i_commit();
12368 ++it;
12369 }
12370 }
12371
12372 bool commitUSBControllers = false;
12373
12374 if (mUSBControllers.isBackedUp())
12375 {
12376 mUSBControllers.commit();
12377
12378 if (mPeer)
12379 {
12380 /* Commit all changes to new controllers (this will reshare data with
12381 * peers for those who have peers) */
12382 USBControllerList *newList = new USBControllerList();
12383 USBControllerList::const_iterator it = mUSBControllers->begin();
12384 while (it != mUSBControllers->end())
12385 {
12386 (*it)->i_commit();
12387
12388 /* look if this controller has a peer device */
12389 ComObjPtr<USBController> peer = (*it)->i_getPeer();
12390 if (!peer)
12391 {
12392 /* no peer means the device is a newly created one;
12393 * create a peer owning data this device share it with */
12394 peer.createObject();
12395 peer->init(mPeer, *it, true /* aReshare */);
12396 }
12397 else
12398 {
12399 /* remove peer from the old list */
12400 mPeer->mUSBControllers->remove(peer);
12401 }
12402 /* and add it to the new list */
12403 newList->push_back(peer);
12404
12405 ++it;
12406 }
12407
12408 /* uninit old peer's controllers that are left */
12409 it = mPeer->mUSBControllers->begin();
12410 while (it != mPeer->mUSBControllers->end())
12411 {
12412 (*it)->uninit();
12413 ++it;
12414 }
12415
12416 /* attach new list of controllers to our peer */
12417 mPeer->mUSBControllers.attach(newList);
12418 }
12419 else
12420 {
12421 /* we have no peer (our parent is the newly created machine);
12422 * just commit changes to devices */
12423 commitUSBControllers = true;
12424 }
12425 }
12426 else
12427 {
12428 /* the list of controllers itself is not changed,
12429 * just commit changes to controllers themselves */
12430 commitUSBControllers = true;
12431 }
12432
12433 if (commitUSBControllers)
12434 {
12435 USBControllerList::const_iterator it = mUSBControllers->begin();
12436 while (it != mUSBControllers->end())
12437 {
12438 (*it)->i_commit();
12439 ++it;
12440 }
12441 }
12442
12443 if (isSessionMachine())
12444 {
12445 /* attach new data to the primary machine and reshare it */
12446 mPeer->mUserData.attach(mUserData);
12447 mPeer->mHWData.attach(mHWData);
12448 /* mMediaData is reshared by fixupMedia */
12449 // mPeer->mMediaData.attach(mMediaData);
12450 Assert(mPeer->mMediaData.data() == mMediaData.data());
12451 }
12452}
12453
12454/**
12455 * Copies all the hardware data from the given machine.
12456 *
12457 * Currently, only called when the VM is being restored from a snapshot. In
12458 * particular, this implies that the VM is not running during this method's
12459 * call.
12460 *
12461 * @note This method must be called from under this object's lock.
12462 *
12463 * @note This method doesn't call #commit(), so all data remains backed up and
12464 * unsaved.
12465 */
12466void Machine::copyFrom(Machine *aThat)
12467{
12468 AssertReturnVoid(!isSnapshotMachine());
12469 AssertReturnVoid(aThat->isSnapshotMachine());
12470
12471 AssertReturnVoid(!Global::IsOnline(mData->mMachineState));
12472
12473 mHWData.assignCopy(aThat->mHWData);
12474
12475 // create copies of all shared folders (mHWData after attaching a copy
12476 // contains just references to original objects)
12477 for (HWData::SharedFolderList::iterator it = mHWData->mSharedFolders.begin();
12478 it != mHWData->mSharedFolders.end();
12479 ++it)
12480 {
12481 ComObjPtr<SharedFolder> folder;
12482 folder.createObject();
12483 HRESULT rc = folder->initCopy(getMachine(), *it);
12484 AssertComRC(rc);
12485 *it = folder;
12486 }
12487
12488 mBIOSSettings->i_copyFrom(aThat->mBIOSSettings);
12489 mVRDEServer->i_copyFrom(aThat->mVRDEServer);
12490 mAudioAdapter->i_copyFrom(aThat->mAudioAdapter);
12491 mUSBDeviceFilters->i_copyFrom(aThat->mUSBDeviceFilters);
12492 mBandwidthControl->i_copyFrom(aThat->mBandwidthControl);
12493
12494 /* create private copies of all controllers */
12495 mStorageControllers.backup();
12496 mStorageControllers->clear();
12497 for (StorageControllerList::iterator it = aThat->mStorageControllers->begin();
12498 it != aThat->mStorageControllers->end();
12499 ++it)
12500 {
12501 ComObjPtr<StorageController> ctrl;
12502 ctrl.createObject();
12503 ctrl->initCopy(this, *it);
12504 mStorageControllers->push_back(ctrl);
12505 }
12506
12507 /* create private copies of all USB controllers */
12508 mUSBControllers.backup();
12509 mUSBControllers->clear();
12510 for (USBControllerList::iterator it = aThat->mUSBControllers->begin();
12511 it != aThat->mUSBControllers->end();
12512 ++it)
12513 {
12514 ComObjPtr<USBController> ctrl;
12515 ctrl.createObject();
12516 ctrl->initCopy(this, *it);
12517 mUSBControllers->push_back(ctrl);
12518 }
12519
12520 mNetworkAdapters.resize(aThat->mNetworkAdapters.size());
12521 for (ULONG slot = 0; slot < mNetworkAdapters.size(); slot++)
12522 mNetworkAdapters[slot]->i_copyFrom(aThat->mNetworkAdapters[slot]);
12523 for (ULONG slot = 0; slot < RT_ELEMENTS(mSerialPorts); slot++)
12524 mSerialPorts[slot]->i_copyFrom(aThat->mSerialPorts[slot]);
12525 for (ULONG slot = 0; slot < RT_ELEMENTS(mParallelPorts); slot++)
12526 mParallelPorts[slot]->i_copyFrom(aThat->mParallelPorts[slot]);
12527}
12528
12529/**
12530 * Returns whether the given storage controller is hotplug capable.
12531 *
12532 * @returns true if the controller supports hotplugging
12533 * false otherwise.
12534 * @param enmCtrlType The controller type to check for.
12535 */
12536bool Machine::isControllerHotplugCapable(StorageControllerType_T enmCtrlType)
12537{
12538 ComPtr<ISystemProperties> systemProperties;
12539 HRESULT rc = mParent->COMGETTER(SystemProperties)(systemProperties.asOutParam());
12540 if (FAILED(rc))
12541 return false;
12542
12543 BOOL aHotplugCapable = FALSE;
12544 systemProperties->GetStorageControllerHotplugCapable(enmCtrlType, &aHotplugCapable);
12545
12546 return RT_BOOL(aHotplugCapable);
12547}
12548
12549#ifdef VBOX_WITH_RESOURCE_USAGE_API
12550
12551void Machine::getDiskList(MediaList &list)
12552{
12553 for (MediaData::AttachmentList::const_iterator it = mMediaData->mAttachments.begin();
12554 it != mMediaData->mAttachments.end();
12555 ++it)
12556 {
12557 MediumAttachment* pAttach = *it;
12558 /* just in case */
12559 AssertStmt(pAttach, continue);
12560
12561 AutoCaller localAutoCallerA(pAttach);
12562 if (FAILED(localAutoCallerA.rc())) continue;
12563
12564 AutoReadLock local_alockA(pAttach COMMA_LOCKVAL_SRC_POS);
12565
12566 if (pAttach->i_getType() == DeviceType_HardDisk)
12567 list.push_back(pAttach->i_getMedium());
12568 }
12569}
12570
12571void Machine::registerMetrics(PerformanceCollector *aCollector, Machine *aMachine, RTPROCESS pid)
12572{
12573 AssertReturnVoid(isWriteLockOnCurrentThread());
12574 AssertPtrReturnVoid(aCollector);
12575
12576 pm::CollectorHAL *hal = aCollector->getHAL();
12577 /* Create sub metrics */
12578 pm::SubMetric *cpuLoadUser = new pm::SubMetric("CPU/Load/User",
12579 "Percentage of processor time spent in user mode by the VM process.");
12580 pm::SubMetric *cpuLoadKernel = new pm::SubMetric("CPU/Load/Kernel",
12581 "Percentage of processor time spent in kernel mode by the VM process.");
12582 pm::SubMetric *ramUsageUsed = new pm::SubMetric("RAM/Usage/Used",
12583 "Size of resident portion of VM process in memory.");
12584 pm::SubMetric *diskUsageUsed = new pm::SubMetric("Disk/Usage/Used",
12585 "Actual size of all VM disks combined.");
12586 pm::SubMetric *machineNetRx = new pm::SubMetric("Net/Rate/Rx",
12587 "Network receive rate.");
12588 pm::SubMetric *machineNetTx = new pm::SubMetric("Net/Rate/Tx",
12589 "Network transmit rate.");
12590 /* Create and register base metrics */
12591 pm::BaseMetric *cpuLoad = new pm::MachineCpuLoadRaw(hal, aMachine, pid,
12592 cpuLoadUser, cpuLoadKernel);
12593 aCollector->registerBaseMetric(cpuLoad);
12594 pm::BaseMetric *ramUsage = new pm::MachineRamUsage(hal, aMachine, pid,
12595 ramUsageUsed);
12596 aCollector->registerBaseMetric(ramUsage);
12597 MediaList disks;
12598 getDiskList(disks);
12599 pm::BaseMetric *diskUsage = new pm::MachineDiskUsage(hal, aMachine, disks,
12600 diskUsageUsed);
12601 aCollector->registerBaseMetric(diskUsage);
12602
12603 aCollector->registerMetric(new pm::Metric(cpuLoad, cpuLoadUser, 0));
12604 aCollector->registerMetric(new pm::Metric(cpuLoad, cpuLoadUser,
12605 new pm::AggregateAvg()));
12606 aCollector->registerMetric(new pm::Metric(cpuLoad, cpuLoadUser,
12607 new pm::AggregateMin()));
12608 aCollector->registerMetric(new pm::Metric(cpuLoad, cpuLoadUser,
12609 new pm::AggregateMax()));
12610 aCollector->registerMetric(new pm::Metric(cpuLoad, cpuLoadKernel, 0));
12611 aCollector->registerMetric(new pm::Metric(cpuLoad, cpuLoadKernel,
12612 new pm::AggregateAvg()));
12613 aCollector->registerMetric(new pm::Metric(cpuLoad, cpuLoadKernel,
12614 new pm::AggregateMin()));
12615 aCollector->registerMetric(new pm::Metric(cpuLoad, cpuLoadKernel,
12616 new pm::AggregateMax()));
12617
12618 aCollector->registerMetric(new pm::Metric(ramUsage, ramUsageUsed, 0));
12619 aCollector->registerMetric(new pm::Metric(ramUsage, ramUsageUsed,
12620 new pm::AggregateAvg()));
12621 aCollector->registerMetric(new pm::Metric(ramUsage, ramUsageUsed,
12622 new pm::AggregateMin()));
12623 aCollector->registerMetric(new pm::Metric(ramUsage, ramUsageUsed,
12624 new pm::AggregateMax()));
12625
12626 aCollector->registerMetric(new pm::Metric(diskUsage, diskUsageUsed, 0));
12627 aCollector->registerMetric(new pm::Metric(diskUsage, diskUsageUsed,
12628 new pm::AggregateAvg()));
12629 aCollector->registerMetric(new pm::Metric(diskUsage, diskUsageUsed,
12630 new pm::AggregateMin()));
12631 aCollector->registerMetric(new pm::Metric(diskUsage, diskUsageUsed,
12632 new pm::AggregateMax()));
12633
12634
12635 /* Guest metrics collector */
12636 mCollectorGuest = new pm::CollectorGuest(aMachine, pid);
12637 aCollector->registerGuest(mCollectorGuest);
12638 LogAleksey(("{%p} " LOG_FN_FMT ": mCollectorGuest=%p\n",
12639 this, __PRETTY_FUNCTION__, mCollectorGuest));
12640
12641 /* Create sub metrics */
12642 pm::SubMetric *guestLoadUser = new pm::SubMetric("Guest/CPU/Load/User",
12643 "Percentage of processor time spent in user mode as seen by the guest.");
12644 pm::SubMetric *guestLoadKernel = new pm::SubMetric("Guest/CPU/Load/Kernel",
12645 "Percentage of processor time spent in kernel mode as seen by the guest.");
12646 pm::SubMetric *guestLoadIdle = new pm::SubMetric("Guest/CPU/Load/Idle",
12647 "Percentage of processor time spent idling as seen by the guest.");
12648
12649 /* The total amount of physical ram is fixed now, but we'll support dynamic guest ram configurations in the future. */
12650 pm::SubMetric *guestMemTotal = new pm::SubMetric("Guest/RAM/Usage/Total", "Total amount of physical guest RAM.");
12651 pm::SubMetric *guestMemFree = new pm::SubMetric("Guest/RAM/Usage/Free", "Free amount of physical guest RAM.");
12652 pm::SubMetric *guestMemBalloon = new pm::SubMetric("Guest/RAM/Usage/Balloon", "Amount of ballooned physical guest RAM.");
12653 pm::SubMetric *guestMemShared = new pm::SubMetric("Guest/RAM/Usage/Shared", "Amount of shared physical guest RAM.");
12654 pm::SubMetric *guestMemCache = new pm::SubMetric("Guest/RAM/Usage/Cache", "Total amount of guest (disk) cache memory.");
12655
12656 pm::SubMetric *guestPagedTotal = new pm::SubMetric("Guest/Pagefile/Usage/Total", "Total amount of space in the page file.");
12657
12658 /* Create and register base metrics */
12659 pm::BaseMetric *machineNetRate = new pm::MachineNetRate(mCollectorGuest, aMachine,
12660 machineNetRx, machineNetTx);
12661 aCollector->registerBaseMetric(machineNetRate);
12662
12663 pm::BaseMetric *guestCpuLoad = new pm::GuestCpuLoad(mCollectorGuest, aMachine,
12664 guestLoadUser, guestLoadKernel, guestLoadIdle);
12665 aCollector->registerBaseMetric(guestCpuLoad);
12666
12667 pm::BaseMetric *guestCpuMem = new pm::GuestRamUsage(mCollectorGuest, aMachine,
12668 guestMemTotal, guestMemFree,
12669 guestMemBalloon, guestMemShared,
12670 guestMemCache, guestPagedTotal);
12671 aCollector->registerBaseMetric(guestCpuMem);
12672
12673 aCollector->registerMetric(new pm::Metric(machineNetRate, machineNetRx, 0));
12674 aCollector->registerMetric(new pm::Metric(machineNetRate, machineNetRx, new pm::AggregateAvg()));
12675 aCollector->registerMetric(new pm::Metric(machineNetRate, machineNetRx, new pm::AggregateMin()));
12676 aCollector->registerMetric(new pm::Metric(machineNetRate, machineNetRx, new pm::AggregateMax()));
12677
12678 aCollector->registerMetric(new pm::Metric(machineNetRate, machineNetTx, 0));
12679 aCollector->registerMetric(new pm::Metric(machineNetRate, machineNetTx, new pm::AggregateAvg()));
12680 aCollector->registerMetric(new pm::Metric(machineNetRate, machineNetTx, new pm::AggregateMin()));
12681 aCollector->registerMetric(new pm::Metric(machineNetRate, machineNetTx, new pm::AggregateMax()));
12682
12683 aCollector->registerMetric(new pm::Metric(guestCpuLoad, guestLoadUser, 0));
12684 aCollector->registerMetric(new pm::Metric(guestCpuLoad, guestLoadUser, new pm::AggregateAvg()));
12685 aCollector->registerMetric(new pm::Metric(guestCpuLoad, guestLoadUser, new pm::AggregateMin()));
12686 aCollector->registerMetric(new pm::Metric(guestCpuLoad, guestLoadUser, new pm::AggregateMax()));
12687
12688 aCollector->registerMetric(new pm::Metric(guestCpuLoad, guestLoadKernel, 0));
12689 aCollector->registerMetric(new pm::Metric(guestCpuLoad, guestLoadKernel, new pm::AggregateAvg()));
12690 aCollector->registerMetric(new pm::Metric(guestCpuLoad, guestLoadKernel, new pm::AggregateMin()));
12691 aCollector->registerMetric(new pm::Metric(guestCpuLoad, guestLoadKernel, new pm::AggregateMax()));
12692
12693 aCollector->registerMetric(new pm::Metric(guestCpuLoad, guestLoadIdle, 0));
12694 aCollector->registerMetric(new pm::Metric(guestCpuLoad, guestLoadIdle, new pm::AggregateAvg()));
12695 aCollector->registerMetric(new pm::Metric(guestCpuLoad, guestLoadIdle, new pm::AggregateMin()));
12696 aCollector->registerMetric(new pm::Metric(guestCpuLoad, guestLoadIdle, new pm::AggregateMax()));
12697
12698 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemTotal, 0));
12699 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemTotal, new pm::AggregateAvg()));
12700 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemTotal, new pm::AggregateMin()));
12701 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemTotal, new pm::AggregateMax()));
12702
12703 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemFree, 0));
12704 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemFree, new pm::AggregateAvg()));
12705 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemFree, new pm::AggregateMin()));
12706 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemFree, new pm::AggregateMax()));
12707
12708 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemBalloon, 0));
12709 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemBalloon, new pm::AggregateAvg()));
12710 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemBalloon, new pm::AggregateMin()));
12711 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemBalloon, new pm::AggregateMax()));
12712
12713 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemShared, 0));
12714 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemShared, new pm::AggregateAvg()));
12715 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemShared, new pm::AggregateMin()));
12716 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemShared, new pm::AggregateMax()));
12717
12718 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemCache, 0));
12719 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemCache, new pm::AggregateAvg()));
12720 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemCache, new pm::AggregateMin()));
12721 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestMemCache, new pm::AggregateMax()));
12722
12723 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestPagedTotal, 0));
12724 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestPagedTotal, new pm::AggregateAvg()));
12725 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestPagedTotal, new pm::AggregateMin()));
12726 aCollector->registerMetric(new pm::Metric(guestCpuMem, guestPagedTotal, new pm::AggregateMax()));
12727}
12728
12729void Machine::unregisterMetrics(PerformanceCollector *aCollector, Machine *aMachine)
12730{
12731 AssertReturnVoid(isWriteLockOnCurrentThread());
12732
12733 if (aCollector)
12734 {
12735 aCollector->unregisterMetricsFor(aMachine);
12736 aCollector->unregisterBaseMetricsFor(aMachine);
12737 }
12738}
12739
12740#endif /* VBOX_WITH_RESOURCE_USAGE_API */
12741
12742
12743////////////////////////////////////////////////////////////////////////////////
12744
12745DEFINE_EMPTY_CTOR_DTOR(SessionMachine)
12746
12747HRESULT SessionMachine::FinalConstruct()
12748{
12749 LogFlowThisFunc(("\n"));
12750
12751 mClientToken = NULL;
12752
12753 return BaseFinalConstruct();
12754}
12755
12756void SessionMachine::FinalRelease()
12757{
12758 LogFlowThisFunc(("\n"));
12759
12760 Assert(!mClientToken);
12761 /* paranoia, should not hang around any more */
12762 if (mClientToken)
12763 {
12764 delete mClientToken;
12765 mClientToken = NULL;
12766 }
12767
12768 uninit(Uninit::Unexpected);
12769
12770 BaseFinalRelease();
12771}
12772
12773/**
12774 * @note Must be called only by Machine::LockMachine() from its own write lock.
12775 */
12776HRESULT SessionMachine::init(Machine *aMachine)
12777{
12778 LogFlowThisFuncEnter();
12779 LogFlowThisFunc(("mName={%s}\n", aMachine->mUserData->s.strName.c_str()));
12780
12781 AssertReturn(aMachine, E_INVALIDARG);
12782
12783 AssertReturn(aMachine->lockHandle()->isWriteLockOnCurrentThread(), E_FAIL);
12784
12785 /* Enclose the state transition NotReady->InInit->Ready */
12786 AutoInitSpan autoInitSpan(this);
12787 AssertReturn(autoInitSpan.isOk(), E_FAIL);
12788
12789 HRESULT rc = S_OK;
12790
12791 /* create the machine client token */
12792 try
12793 {
12794 mClientToken = new ClientToken(aMachine, this);
12795 if (!mClientToken->isReady())
12796 {
12797 delete mClientToken;
12798 mClientToken = NULL;
12799 rc = E_FAIL;
12800 }
12801 }
12802 catch (std::bad_alloc &)
12803 {
12804 rc = E_OUTOFMEMORY;
12805 }
12806 if (FAILED(rc))
12807 return rc;
12808
12809 /* memorize the peer Machine */
12810 unconst(mPeer) = aMachine;
12811 /* share the parent pointer */
12812 unconst(mParent) = aMachine->mParent;
12813
12814 /* take the pointers to data to share */
12815 mData.share(aMachine->mData);
12816 mSSData.share(aMachine->mSSData);
12817
12818 mUserData.share(aMachine->mUserData);
12819 mHWData.share(aMachine->mHWData);
12820 mMediaData.share(aMachine->mMediaData);
12821
12822 mStorageControllers.allocate();
12823 for (StorageControllerList::const_iterator it = aMachine->mStorageControllers->begin();
12824 it != aMachine->mStorageControllers->end();
12825 ++it)
12826 {
12827 ComObjPtr<StorageController> ctl;
12828 ctl.createObject();
12829 ctl->init(this, *it);
12830 mStorageControllers->push_back(ctl);
12831 }
12832
12833 mUSBControllers.allocate();
12834 for (USBControllerList::const_iterator it = aMachine->mUSBControllers->begin();
12835 it != aMachine->mUSBControllers->end();
12836 ++it)
12837 {
12838 ComObjPtr<USBController> ctl;
12839 ctl.createObject();
12840 ctl->init(this, *it);
12841 mUSBControllers->push_back(ctl);
12842 }
12843
12844 unconst(mBIOSSettings).createObject();
12845 mBIOSSettings->init(this, aMachine->mBIOSSettings);
12846 /* create another VRDEServer object that will be mutable */
12847 unconst(mVRDEServer).createObject();
12848 mVRDEServer->init(this, aMachine->mVRDEServer);
12849 /* create another audio adapter object that will be mutable */
12850 unconst(mAudioAdapter).createObject();
12851 mAudioAdapter->init(this, aMachine->mAudioAdapter);
12852 /* create a list of serial ports that will be mutable */
12853 for (ULONG slot = 0; slot < RT_ELEMENTS(mSerialPorts); slot++)
12854 {
12855 unconst(mSerialPorts[slot]).createObject();
12856 mSerialPorts[slot]->init(this, aMachine->mSerialPorts[slot]);
12857 }
12858 /* create a list of parallel ports that will be mutable */
12859 for (ULONG slot = 0; slot < RT_ELEMENTS(mParallelPorts); slot++)
12860 {
12861 unconst(mParallelPorts[slot]).createObject();
12862 mParallelPorts[slot]->init(this, aMachine->mParallelPorts[slot]);
12863 }
12864
12865 /* create another USB device filters object that will be mutable */
12866 unconst(mUSBDeviceFilters).createObject();
12867 mUSBDeviceFilters->init(this, aMachine->mUSBDeviceFilters);
12868
12869 /* create a list of network adapters that will be mutable */
12870 mNetworkAdapters.resize(aMachine->mNetworkAdapters.size());
12871 for (ULONG slot = 0; slot < mNetworkAdapters.size(); slot++)
12872 {
12873 unconst(mNetworkAdapters[slot]).createObject();
12874 mNetworkAdapters[slot]->init(this, aMachine->mNetworkAdapters[slot]);
12875
12876 NetworkAttachmentType_T type;
12877 HRESULT hrc;
12878 hrc = mNetworkAdapters[slot]->COMGETTER(AttachmentType)(&type);
12879 if ( SUCCEEDED(hrc)
12880 && type == NetworkAttachmentType_NATNetwork)
12881 {
12882 Bstr name;
12883 hrc = mNetworkAdapters[slot]->COMGETTER(NATNetwork)(name.asOutParam());
12884 if (SUCCEEDED(hrc))
12885 {
12886 LogRel(("VM '%s' starts using NAT network '%ls'\n",
12887 mUserData->s.strName.c_str(), name.raw()));
12888 aMachine->lockHandle()->unlockWrite();
12889 mParent->i_natNetworkRefInc(name.raw());
12890#ifdef RT_LOCK_STRICT
12891 aMachine->lockHandle()->lockWrite(RT_SRC_POS);
12892#else
12893 aMachine->lockHandle()->lockWrite();
12894#endif
12895 }
12896 }
12897 }
12898
12899 /* create another bandwidth control object that will be mutable */
12900 unconst(mBandwidthControl).createObject();
12901 mBandwidthControl->init(this, aMachine->mBandwidthControl);
12902
12903 /* default is to delete saved state on Saved -> PoweredOff transition */
12904 mRemoveSavedState = true;
12905
12906 /* Confirm a successful initialization when it's the case */
12907 autoInitSpan.setSucceeded();
12908
12909 LogFlowThisFuncLeave();
12910 return rc;
12911}
12912
12913/**
12914 * Uninitializes this session object. If the reason is other than
12915 * Uninit::Unexpected, then this method MUST be called from #checkForDeath()
12916 * or the client watcher code.
12917 *
12918 * @param aReason uninitialization reason
12919 *
12920 * @note Locks mParent + this object for writing.
12921 */
12922void SessionMachine::uninit(Uninit::Reason aReason)
12923{
12924 LogFlowThisFuncEnter();
12925 LogFlowThisFunc(("reason=%d\n", aReason));
12926
12927 /*
12928 * Strongly reference ourselves to prevent this object deletion after
12929 * mData->mSession.mMachine.setNull() below (which can release the last
12930 * reference and call the destructor). Important: this must be done before
12931 * accessing any members (and before AutoUninitSpan that does it as well).
12932 * This self reference will be released as the very last step on return.
12933 */
12934 ComObjPtr<SessionMachine> selfRef = this;
12935
12936 /* Enclose the state transition Ready->InUninit->NotReady */
12937 AutoUninitSpan autoUninitSpan(this);
12938 if (autoUninitSpan.uninitDone())
12939 {
12940 LogFlowThisFunc(("Already uninitialized\n"));
12941 LogFlowThisFuncLeave();
12942 return;
12943 }
12944
12945 if (autoUninitSpan.initFailed())
12946 {
12947 /* We've been called by init() because it's failed. It's not really
12948 * necessary (nor it's safe) to perform the regular uninit sequence
12949 * below, the following is enough.
12950 */
12951 LogFlowThisFunc(("Initialization failed.\n"));
12952 /* destroy the machine client token */
12953 if (mClientToken)
12954 {
12955 delete mClientToken;
12956 mClientToken = NULL;
12957 }
12958 uninitDataAndChildObjects();
12959 mData.free();
12960 unconst(mParent) = NULL;
12961 unconst(mPeer) = NULL;
12962 LogFlowThisFuncLeave();
12963 return;
12964 }
12965
12966 MachineState_T lastState;
12967 {
12968 AutoReadLock tempLock(this COMMA_LOCKVAL_SRC_POS);
12969 lastState = mData->mMachineState;
12970 }
12971 NOREF(lastState);
12972
12973#ifdef VBOX_WITH_USB
12974 // release all captured USB devices, but do this before requesting the locks below
12975 if (aReason == Uninit::Abnormal && Global::IsOnline(lastState))
12976 {
12977 /* Console::captureUSBDevices() is called in the VM process only after
12978 * setting the machine state to Starting or Restoring.
12979 * Console::detachAllUSBDevices() will be called upon successful
12980 * termination. So, we need to release USB devices only if there was
12981 * an abnormal termination of a running VM.
12982 *
12983 * This is identical to SessionMachine::DetachAllUSBDevices except
12984 * for the aAbnormal argument. */
12985 HRESULT rc = mUSBDeviceFilters->i_notifyProxy(false /* aInsertFilters */);
12986 AssertComRC(rc);
12987 NOREF(rc);
12988
12989 USBProxyService *service = mParent->i_host()->i_usbProxyService();
12990 if (service)
12991 service->detachAllDevicesFromVM(this, true /* aDone */, true /* aAbnormal */);
12992 }
12993#endif /* VBOX_WITH_USB */
12994
12995 // we need to lock this object in uninit() because the lock is shared
12996 // with mPeer (as well as data we modify below). mParent lock is needed
12997 // by several calls to it, and USB needs host lock.
12998 AutoMultiWriteLock3 multilock(mParent, mParent->i_host(), this COMMA_LOCKVAL_SRC_POS);
12999
13000#ifdef VBOX_WITH_RESOURCE_USAGE_API
13001 /*
13002 * It is safe to call Machine::unregisterMetrics() here because
13003 * PerformanceCollector::samplerCallback no longer accesses guest methods
13004 * holding the lock.
13005 */
13006 unregisterMetrics(mParent->i_performanceCollector(), mPeer);
13007 /* The guest must be unregistered after its metrics (@bugref{5949}). */
13008 LogAleksey(("{%p} " LOG_FN_FMT ": mCollectorGuest=%p\n",
13009 this, __PRETTY_FUNCTION__, mCollectorGuest));
13010 if (mCollectorGuest)
13011 {
13012 mParent->i_performanceCollector()->unregisterGuest(mCollectorGuest);
13013 // delete mCollectorGuest; => CollectorGuestManager::destroyUnregistered()
13014 mCollectorGuest = NULL;
13015 }
13016#endif
13017
13018 if (aReason == Uninit::Abnormal)
13019 {
13020 LogWarningThisFunc(("ABNORMAL client termination! (wasBusy=%d)\n",
13021 Global::IsOnlineOrTransient(lastState)));
13022
13023 /* reset the state to Aborted */
13024 if (mData->mMachineState != MachineState_Aborted)
13025 setMachineState(MachineState_Aborted);
13026 }
13027
13028 // any machine settings modified?
13029 if (mData->flModifications)
13030 {
13031 LogWarningThisFunc(("Discarding unsaved settings changes!\n"));
13032 rollback(false /* aNotify */);
13033 }
13034
13035 Assert( mConsoleTaskData.strStateFilePath.isEmpty()
13036 || !mConsoleTaskData.mSnapshot);
13037 if (!mConsoleTaskData.strStateFilePath.isEmpty())
13038 {
13039 LogWarningThisFunc(("canceling failed save state request!\n"));
13040 endSavingState(E_FAIL, tr("Machine terminated with pending save state!"));
13041 }
13042 else if (!mConsoleTaskData.mSnapshot.isNull())
13043 {
13044 LogWarningThisFunc(("canceling untaken snapshot!\n"));
13045
13046 /* delete all differencing hard disks created (this will also attach
13047 * their parents back by rolling back mMediaData) */
13048 rollbackMedia();
13049
13050 // delete the saved state file (it might have been already created)
13051 // AFTER killing the snapshot so that releaseSavedStateFile() won't
13052 // think it's still in use
13053 Utf8Str strStateFile = mConsoleTaskData.mSnapshot->i_getStateFilePath();
13054 mConsoleTaskData.mSnapshot->uninit();
13055 releaseSavedStateFile(strStateFile, NULL /* pSnapshotToIgnore */ );
13056 }
13057
13058 mData->mSession.mPID = NIL_RTPROCESS;
13059
13060 if (aReason == Uninit::Unexpected)
13061 {
13062 /* Uninitialization didn't come from #checkForDeath(), so tell the
13063 * client watcher thread to update the set of machines that have open
13064 * sessions. */
13065 mParent->i_updateClientWatcher();
13066 }
13067
13068 /* uninitialize all remote controls */
13069 if (mData->mSession.mRemoteControls.size())
13070 {
13071 LogFlowThisFunc(("Closing remote sessions (%d):\n",
13072 mData->mSession.mRemoteControls.size()));
13073
13074 Data::Session::RemoteControlList::iterator it =
13075 mData->mSession.mRemoteControls.begin();
13076 while (it != mData->mSession.mRemoteControls.end())
13077 {
13078 LogFlowThisFunc((" Calling remoteControl->Uninitialize()...\n"));
13079 HRESULT rc = (*it)->Uninitialize();
13080 LogFlowThisFunc((" remoteControl->Uninitialize() returned %08X\n", rc));
13081 if (FAILED(rc))
13082 LogWarningThisFunc(("Forgot to close the remote session?\n"));
13083 ++it;
13084 }
13085 mData->mSession.mRemoteControls.clear();
13086 }
13087
13088 for (ULONG slot = 0; slot < mNetworkAdapters.size(); slot++)
13089 {
13090 NetworkAttachmentType_T type;
13091 HRESULT hrc;
13092
13093 hrc = mNetworkAdapters[slot]->COMGETTER(AttachmentType)(&type);
13094 if ( SUCCEEDED(hrc)
13095 && type == NetworkAttachmentType_NATNetwork)
13096 {
13097 Bstr name;
13098 hrc = mNetworkAdapters[slot]->COMGETTER(NATNetwork)(name.asOutParam());
13099 if (SUCCEEDED(hrc))
13100 {
13101 multilock.release();
13102 LogRel(("VM '%s' stops using NAT network '%ls'\n",
13103 mUserData->s.strName.c_str(), name.raw()));
13104 mParent->i_natNetworkRefDec(name.raw());
13105 multilock.acquire();
13106 }
13107 }
13108 }
13109
13110 /*
13111 * An expected uninitialization can come only from #checkForDeath().
13112 * Otherwise it means that something's gone really wrong (for example,
13113 * the Session implementation has released the VirtualBox reference
13114 * before it triggered #OnSessionEnd(), or before releasing IPC semaphore,
13115 * etc). However, it's also possible, that the client releases the IPC
13116 * semaphore correctly (i.e. before it releases the VirtualBox reference),
13117 * but the VirtualBox release event comes first to the server process.
13118 * This case is practically possible, so we should not assert on an
13119 * unexpected uninit, just log a warning.
13120 */
13121
13122 if ((aReason == Uninit::Unexpected))
13123 LogWarningThisFunc(("Unexpected SessionMachine uninitialization!\n"));
13124
13125 if (aReason != Uninit::Normal)
13126 {
13127 mData->mSession.mDirectControl.setNull();
13128 }
13129 else
13130 {
13131 /* this must be null here (see #OnSessionEnd()) */
13132 Assert(mData->mSession.mDirectControl.isNull());
13133 Assert(mData->mSession.mState == SessionState_Unlocking);
13134 Assert(!mData->mSession.mProgress.isNull());
13135 }
13136 if (mData->mSession.mProgress)
13137 {
13138 if (aReason == Uninit::Normal)
13139 mData->mSession.mProgress->notifyComplete(S_OK);
13140 else
13141 mData->mSession.mProgress->notifyComplete(E_FAIL,
13142 COM_IIDOF(ISession),
13143 getComponentName(),
13144 tr("The VM session was aborted"));
13145 mData->mSession.mProgress.setNull();
13146 }
13147
13148 /* remove the association between the peer machine and this session machine */
13149 Assert( (SessionMachine*)mData->mSession.mMachine == this
13150 || aReason == Uninit::Unexpected);
13151
13152 /* reset the rest of session data */
13153 mData->mSession.mMachine.setNull();
13154 mData->mSession.mState = SessionState_Unlocked;
13155 mData->mSession.mType.setNull();
13156
13157 /* destroy the machine client token before leaving the exclusive lock */
13158 if (mClientToken)
13159 {
13160 delete mClientToken;
13161 mClientToken = NULL;
13162 }
13163
13164 /* fire an event */
13165 mParent->i_onSessionStateChange(mData->mUuid, SessionState_Unlocked);
13166
13167 uninitDataAndChildObjects();
13168
13169 /* free the essential data structure last */
13170 mData.free();
13171
13172 /* release the exclusive lock before setting the below two to NULL */
13173 multilock.release();
13174
13175 unconst(mParent) = NULL;
13176 unconst(mPeer) = NULL;
13177
13178 LogFlowThisFuncLeave();
13179}
13180
13181// util::Lockable interface
13182////////////////////////////////////////////////////////////////////////////////
13183
13184/**
13185 * Overrides VirtualBoxBase::lockHandle() in order to share the lock handle
13186 * with the primary Machine instance (mPeer).
13187 */
13188RWLockHandle *SessionMachine::lockHandle() const
13189{
13190 AssertReturn(mPeer != NULL, NULL);
13191 return mPeer->lockHandle();
13192}
13193
13194// IInternalMachineControl methods
13195////////////////////////////////////////////////////////////////////////////////
13196
13197/**
13198 * Passes collected guest statistics to performance collector object
13199 */
13200STDMETHODIMP SessionMachine::ReportVmStatistics(ULONG aValidStats, ULONG aCpuUser,
13201 ULONG aCpuKernel, ULONG aCpuIdle,
13202 ULONG aMemTotal, ULONG aMemFree,
13203 ULONG aMemBalloon, ULONG aMemShared,
13204 ULONG aMemCache, ULONG aPageTotal,
13205 ULONG aAllocVMM, ULONG aFreeVMM,
13206 ULONG aBalloonedVMM, ULONG aSharedVMM,
13207 ULONG aVmNetRx, ULONG aVmNetTx)
13208{
13209#ifdef VBOX_WITH_RESOURCE_USAGE_API
13210 if (mCollectorGuest)
13211 mCollectorGuest->updateStats(aValidStats, aCpuUser, aCpuKernel, aCpuIdle,
13212 aMemTotal, aMemFree, aMemBalloon, aMemShared,
13213 aMemCache, aPageTotal, aAllocVMM, aFreeVMM,
13214 aBalloonedVMM, aSharedVMM, aVmNetRx, aVmNetTx);
13215
13216 return S_OK;
13217#else
13218 NOREF(aValidStats);
13219 NOREF(aCpuUser);
13220 NOREF(aCpuKernel);
13221 NOREF(aCpuIdle);
13222 NOREF(aMemTotal);
13223 NOREF(aMemFree);
13224 NOREF(aMemBalloon);
13225 NOREF(aMemShared);
13226 NOREF(aMemCache);
13227 NOREF(aPageTotal);
13228 NOREF(aAllocVMM);
13229 NOREF(aFreeVMM);
13230 NOREF(aBalloonedVMM);
13231 NOREF(aSharedVMM);
13232 NOREF(aVmNetRx);
13233 NOREF(aVmNetTx);
13234 return E_NOTIMPL;
13235#endif
13236}
13237
13238/**
13239 * @note Locks this object for writing.
13240 */
13241STDMETHODIMP SessionMachine::SetRemoveSavedStateFile(BOOL aRemove)
13242{
13243 AutoCaller autoCaller(this);
13244 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
13245
13246 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
13247
13248 mRemoveSavedState = aRemove;
13249
13250 return S_OK;
13251}
13252
13253/**
13254 * @note Locks the same as #setMachineState() does.
13255 */
13256STDMETHODIMP SessionMachine::UpdateState(MachineState_T aMachineState)
13257{
13258 return setMachineState(aMachineState);
13259}
13260
13261/**
13262 * @note Locks this object for writing.
13263 */
13264STDMETHODIMP SessionMachine::BeginPowerUp(IProgress *aProgress)
13265{
13266 LogFlowThisFunc(("aProgress=%p\n", aProgress));
13267 AutoCaller autoCaller(this);
13268 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
13269
13270 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
13271
13272 if (mData->mSession.mState != SessionState_Locked)
13273 return VBOX_E_INVALID_OBJECT_STATE;
13274
13275 if (!mData->mSession.mProgress.isNull())
13276 mData->mSession.mProgress->setOtherProgressObject(aProgress);
13277
13278 LogFlowThisFunc(("returns S_OK.\n"));
13279 return S_OK;
13280}
13281
13282/**
13283 * @note Locks this object for writing.
13284 */
13285STDMETHODIMP SessionMachine::EndPowerUp(LONG iResult)
13286{
13287 AutoCaller autoCaller(this);
13288 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
13289
13290 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
13291
13292 if (mData->mSession.mState != SessionState_Locked)
13293 return VBOX_E_INVALID_OBJECT_STATE;
13294
13295 /* Finalize the LaunchVMProcess progress object. */
13296 if (mData->mSession.mProgress)
13297 {
13298 mData->mSession.mProgress->notifyComplete((HRESULT)iResult);
13299 mData->mSession.mProgress.setNull();
13300 }
13301
13302 if (SUCCEEDED((HRESULT)iResult))
13303 {
13304#ifdef VBOX_WITH_RESOURCE_USAGE_API
13305 /* The VM has been powered up successfully, so it makes sense
13306 * now to offer the performance metrics for a running machine
13307 * object. Doing it earlier wouldn't be safe. */
13308 registerMetrics(mParent->i_performanceCollector(), mPeer,
13309 mData->mSession.mPID);
13310#endif /* VBOX_WITH_RESOURCE_USAGE_API */
13311 }
13312
13313 return S_OK;
13314}
13315
13316/**
13317 * @note Locks this object for writing.
13318 */
13319STDMETHODIMP SessionMachine::BeginPoweringDown(IProgress **aProgress)
13320{
13321 LogFlowThisFuncEnter();
13322
13323 CheckComArgOutPointerValid(aProgress);
13324
13325 AutoCaller autoCaller(this);
13326 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
13327
13328 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
13329
13330 AssertReturn(mConsoleTaskData.mLastState == MachineState_Null,
13331 E_FAIL);
13332
13333 /* create a progress object to track operation completion */
13334 ComObjPtr<Progress> pProgress;
13335 pProgress.createObject();
13336 pProgress->init(getVirtualBox(),
13337 static_cast<IMachine *>(this) /* aInitiator */,
13338 Bstr(tr("Stopping the virtual machine")).raw(),
13339 FALSE /* aCancelable */);
13340
13341 /* fill in the console task data */
13342 mConsoleTaskData.mLastState = mData->mMachineState;
13343 mConsoleTaskData.mProgress = pProgress;
13344
13345 /* set the state to Stopping (this is expected by Console::PowerDown()) */
13346 setMachineState(MachineState_Stopping);
13347
13348 pProgress.queryInterfaceTo(aProgress);
13349
13350 return S_OK;
13351}
13352
13353/**
13354 * @note Locks this object for writing.
13355 */
13356STDMETHODIMP SessionMachine::EndPoweringDown(LONG iResult, IN_BSTR aErrMsg)
13357{
13358 LogFlowThisFuncEnter();
13359
13360 AutoCaller autoCaller(this);
13361 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
13362
13363 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
13364
13365 AssertReturn( ( (SUCCEEDED(iResult) && mData->mMachineState == MachineState_PoweredOff)
13366 || (FAILED(iResult) && mData->mMachineState == MachineState_Stopping))
13367 && mConsoleTaskData.mLastState != MachineState_Null,
13368 E_FAIL);
13369
13370 /*
13371 * On failure, set the state to the state we had when BeginPoweringDown()
13372 * was called (this is expected by Console::PowerDown() and the associated
13373 * task). On success the VM process already changed the state to
13374 * MachineState_PoweredOff, so no need to do anything.
13375 */
13376 if (FAILED(iResult))
13377 setMachineState(mConsoleTaskData.mLastState);
13378
13379 /* notify the progress object about operation completion */
13380 Assert(mConsoleTaskData.mProgress);
13381 if (SUCCEEDED(iResult))
13382 mConsoleTaskData.mProgress->notifyComplete(S_OK);
13383 else
13384 {
13385 Utf8Str strErrMsg(aErrMsg);
13386 if (strErrMsg.length())
13387 mConsoleTaskData.mProgress->notifyComplete(iResult,
13388 COM_IIDOF(ISession),
13389 getComponentName(),
13390 strErrMsg.c_str());
13391 else
13392 mConsoleTaskData.mProgress->notifyComplete(iResult);
13393 }
13394
13395 /* clear out the temporary saved state data */
13396 mConsoleTaskData.mLastState = MachineState_Null;
13397 mConsoleTaskData.mProgress.setNull();
13398
13399 LogFlowThisFuncLeave();
13400 return S_OK;
13401}
13402
13403
13404/**
13405 * Goes through the USB filters of the given machine to see if the given
13406 * device matches any filter or not.
13407 *
13408 * @note Locks the same as USBController::hasMatchingFilter() does.
13409 */
13410STDMETHODIMP SessionMachine::RunUSBDeviceFilters(IUSBDevice *aUSBDevice,
13411 BOOL *aMatched,
13412 ULONG *aMaskedIfs)
13413{
13414 LogFlowThisFunc(("\n"));
13415
13416 CheckComArgNotNull(aUSBDevice);
13417 CheckComArgOutPointerValid(aMatched);
13418
13419 AutoCaller autoCaller(this);
13420 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
13421
13422#ifdef VBOX_WITH_USB
13423 *aMatched = mUSBDeviceFilters->i_hasMatchingFilter(aUSBDevice, aMaskedIfs);
13424#else
13425 NOREF(aUSBDevice);
13426 NOREF(aMaskedIfs);
13427 *aMatched = FALSE;
13428#endif
13429
13430 return S_OK;
13431}
13432
13433/**
13434 * @note Locks the same as Host::captureUSBDevice() does.
13435 */
13436STDMETHODIMP SessionMachine::CaptureUSBDevice(IN_BSTR aId)
13437{
13438 LogFlowThisFunc(("\n"));
13439
13440 AutoCaller autoCaller(this);
13441 AssertComRCReturnRC(autoCaller.rc());
13442
13443#ifdef VBOX_WITH_USB
13444 /* if captureDeviceForVM() fails, it must have set extended error info */
13445 clearError();
13446 MultiResult rc = mParent->i_host()->i_checkUSBProxyService();
13447 if (FAILED(rc)) return rc;
13448
13449 USBProxyService *service = mParent->i_host()->i_usbProxyService();
13450 AssertReturn(service, E_FAIL);
13451 return service->captureDeviceForVM(this, Guid(aId).ref());
13452#else
13453 NOREF(aId);
13454 return E_NOTIMPL;
13455#endif
13456}
13457
13458/**
13459 * @note Locks the same as Host::detachUSBDevice() does.
13460 */
13461STDMETHODIMP SessionMachine::DetachUSBDevice(IN_BSTR aId, BOOL aDone)
13462{
13463 LogFlowThisFunc(("\n"));
13464
13465 AutoCaller autoCaller(this);
13466 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
13467
13468#ifdef VBOX_WITH_USB
13469 USBProxyService *service = mParent->i_host()->i_usbProxyService();
13470 AssertReturn(service, E_FAIL);
13471 return service->detachDeviceFromVM(this, Guid(aId).ref(), !!aDone);
13472#else
13473 NOREF(aId);
13474 NOREF(aDone);
13475 return E_NOTIMPL;
13476#endif
13477}
13478
13479/**
13480 * Inserts all machine filters to the USB proxy service and then calls
13481 * Host::autoCaptureUSBDevices().
13482 *
13483 * Called by Console from the VM process upon VM startup.
13484 *
13485 * @note Locks what called methods lock.
13486 */
13487STDMETHODIMP SessionMachine::AutoCaptureUSBDevices()
13488{
13489 LogFlowThisFunc(("\n"));
13490
13491 AutoCaller autoCaller(this);
13492 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
13493
13494#ifdef VBOX_WITH_USB
13495 HRESULT rc = mUSBDeviceFilters->i_notifyProxy(true /* aInsertFilters */);
13496 AssertComRC(rc);
13497 NOREF(rc);
13498
13499 USBProxyService *service = mParent->i_host()->i_usbProxyService();
13500 AssertReturn(service, E_FAIL);
13501 return service->autoCaptureDevicesForVM(this);
13502#else
13503 return S_OK;
13504#endif
13505}
13506
13507/**
13508 * Removes all machine filters from the USB proxy service and then calls
13509 * Host::detachAllUSBDevices().
13510 *
13511 * Called by Console from the VM process upon normal VM termination or by
13512 * SessionMachine::uninit() upon abnormal VM termination (from under the
13513 * Machine/SessionMachine lock).
13514 *
13515 * @note Locks what called methods lock.
13516 */
13517STDMETHODIMP SessionMachine::DetachAllUSBDevices(BOOL aDone)
13518{
13519 LogFlowThisFunc(("\n"));
13520
13521 AutoCaller autoCaller(this);
13522 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
13523
13524#ifdef VBOX_WITH_USB
13525 HRESULT rc = mUSBDeviceFilters->i_notifyProxy(false /* aInsertFilters */);
13526 AssertComRC(rc);
13527 NOREF(rc);
13528
13529 USBProxyService *service = mParent->i_host()->i_usbProxyService();
13530 AssertReturn(service, E_FAIL);
13531 return service->detachAllDevicesFromVM(this, !!aDone, false /* aAbnormal */);
13532#else
13533 NOREF(aDone);
13534 return S_OK;
13535#endif
13536}
13537
13538/**
13539 * @note Locks this object for writing.
13540 */
13541STDMETHODIMP SessionMachine::OnSessionEnd(ISession *aSession,
13542 IProgress **aProgress)
13543{
13544 LogFlowThisFuncEnter();
13545
13546 AssertReturn(aSession, E_INVALIDARG);
13547 AssertReturn(aProgress, E_INVALIDARG);
13548
13549 AutoCaller autoCaller(this);
13550
13551 LogFlowThisFunc(("callerstate=%d\n", autoCaller.state()));
13552 /*
13553 * We don't assert below because it might happen that a non-direct session
13554 * informs us it is closed right after we've been uninitialized -- it's ok.
13555 */
13556 if (FAILED(autoCaller.rc())) return autoCaller.rc();
13557
13558 /* get IInternalSessionControl interface */
13559 ComPtr<IInternalSessionControl> control(aSession);
13560
13561 ComAssertRet(!control.isNull(), E_INVALIDARG);
13562
13563 /* Creating a Progress object requires the VirtualBox lock, and
13564 * thus locking it here is required by the lock order rules. */
13565 AutoMultiWriteLock2 alock(mParent, this COMMA_LOCKVAL_SRC_POS);
13566
13567 if (control == mData->mSession.mDirectControl)
13568 {
13569 ComAssertRet(aProgress, E_POINTER);
13570
13571 /* The direct session is being normally closed by the client process
13572 * ----------------------------------------------------------------- */
13573
13574 /* go to the closing state (essential for all open*Session() calls and
13575 * for #checkForDeath()) */
13576 Assert(mData->mSession.mState == SessionState_Locked);
13577 mData->mSession.mState = SessionState_Unlocking;
13578
13579 /* set direct control to NULL to release the remote instance */
13580 mData->mSession.mDirectControl.setNull();
13581 LogFlowThisFunc(("Direct control is set to NULL\n"));
13582
13583 if (mData->mSession.mProgress)
13584 {
13585 /* finalize the progress, someone might wait if a frontend
13586 * closes the session before powering on the VM. */
13587 mData->mSession.mProgress->notifyComplete(E_FAIL,
13588 COM_IIDOF(ISession),
13589 getComponentName(),
13590 tr("The VM session was closed before any attempt to power it on"));
13591 mData->mSession.mProgress.setNull();
13592 }
13593
13594 /* Create the progress object the client will use to wait until
13595 * #checkForDeath() is called to uninitialize this session object after
13596 * it releases the IPC semaphore.
13597 * Note! Because we're "reusing" mProgress here, this must be a proxy
13598 * object just like for LaunchVMProcess. */
13599 Assert(mData->mSession.mProgress.isNull());
13600 ComObjPtr<ProgressProxy> progress;
13601 progress.createObject();
13602 ComPtr<IUnknown> pPeer(mPeer);
13603 progress->init(mParent, pPeer,
13604 Bstr(tr("Closing session")).raw(),
13605 FALSE /* aCancelable */);
13606 progress.queryInterfaceTo(aProgress);
13607 mData->mSession.mProgress = progress;
13608 }
13609 else
13610 {
13611 /* the remote session is being normally closed */
13612 Data::Session::RemoteControlList::iterator it =
13613 mData->mSession.mRemoteControls.begin();
13614 while (it != mData->mSession.mRemoteControls.end())
13615 {
13616 if (control == *it)
13617 break;
13618 ++it;
13619 }
13620 BOOL found = it != mData->mSession.mRemoteControls.end();
13621 ComAssertMsgRet(found, ("The session is not found in the session list!"),
13622 E_INVALIDARG);
13623 // This MUST be erase(it), not remove(*it) as the latter triggers a
13624 // very nasty use after free due to the place where the value "lives".
13625 mData->mSession.mRemoteControls.erase(it);
13626 }
13627
13628 /* signal the client watcher thread, because the client is going away */
13629 mParent->i_updateClientWatcher();
13630
13631 LogFlowThisFuncLeave();
13632 return S_OK;
13633}
13634
13635/**
13636 * @note Locks this object for writing.
13637 */
13638STDMETHODIMP SessionMachine::BeginSavingState(IProgress **aProgress, BSTR *aStateFilePath)
13639{
13640 LogFlowThisFuncEnter();
13641
13642 CheckComArgOutPointerValid(aProgress);
13643 CheckComArgOutPointerValid(aStateFilePath);
13644
13645 AutoCaller autoCaller(this);
13646 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
13647
13648 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
13649
13650 AssertReturn( mData->mMachineState == MachineState_Paused
13651 && mConsoleTaskData.mLastState == MachineState_Null
13652 && mConsoleTaskData.strStateFilePath.isEmpty(),
13653 E_FAIL);
13654
13655 /* create a progress object to track operation completion */
13656 ComObjPtr<Progress> pProgress;
13657 pProgress.createObject();
13658 pProgress->init(getVirtualBox(),
13659 static_cast<IMachine *>(this) /* aInitiator */,
13660 Bstr(tr("Saving the execution state of the virtual machine")).raw(),
13661 FALSE /* aCancelable */);
13662
13663 Utf8Str strStateFilePath;
13664 /* stateFilePath is null when the machine is not running */
13665 if (mData->mMachineState == MachineState_Paused)
13666 composeSavedStateFilename(strStateFilePath);
13667
13668 /* fill in the console task data */
13669 mConsoleTaskData.mLastState = mData->mMachineState;
13670 mConsoleTaskData.strStateFilePath = strStateFilePath;
13671 mConsoleTaskData.mProgress = pProgress;
13672
13673 /* set the state to Saving (this is expected by Console::SaveState()) */
13674 setMachineState(MachineState_Saving);
13675
13676 strStateFilePath.cloneTo(aStateFilePath);
13677 pProgress.queryInterfaceTo(aProgress);
13678
13679 return S_OK;
13680}
13681
13682/**
13683 * @note Locks mParent + this object for writing.
13684 */
13685STDMETHODIMP SessionMachine::EndSavingState(LONG iResult, IN_BSTR aErrMsg)
13686{
13687 LogFlowThisFunc(("\n"));
13688
13689 AutoCaller autoCaller(this);
13690 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
13691
13692 /* endSavingState() need mParent lock */
13693 AutoMultiWriteLock2 alock(mParent, this COMMA_LOCKVAL_SRC_POS);
13694
13695 AssertReturn( ( (SUCCEEDED(iResult) && mData->mMachineState == MachineState_Saved)
13696 || (FAILED(iResult) && mData->mMachineState == MachineState_Saving))
13697 && mConsoleTaskData.mLastState != MachineState_Null
13698 && !mConsoleTaskData.strStateFilePath.isEmpty(),
13699 E_FAIL);
13700
13701 /*
13702 * On failure, set the state to the state we had when BeginSavingState()
13703 * was called (this is expected by Console::SaveState() and the associated
13704 * task). On success the VM process already changed the state to
13705 * MachineState_Saved, so no need to do anything.
13706 */
13707 if (FAILED(iResult))
13708 setMachineState(mConsoleTaskData.mLastState);
13709
13710 return endSavingState(iResult, aErrMsg);
13711}
13712
13713/**
13714 * @note Locks this object for writing.
13715 */
13716STDMETHODIMP SessionMachine::AdoptSavedState(IN_BSTR aSavedStateFile)
13717{
13718 LogFlowThisFunc(("\n"));
13719
13720 CheckComArgStrNotEmptyOrNull(aSavedStateFile);
13721
13722 AutoCaller autoCaller(this);
13723 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
13724
13725 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
13726
13727 AssertReturn( mData->mMachineState == MachineState_PoweredOff
13728 || mData->mMachineState == MachineState_Teleported
13729 || mData->mMachineState == MachineState_Aborted
13730 , E_FAIL); /** @todo setError. */
13731
13732 Utf8Str stateFilePathFull = aSavedStateFile;
13733 int vrc = calculateFullPath(stateFilePathFull, stateFilePathFull);
13734 if (RT_FAILURE(vrc))
13735 return setError(VBOX_E_FILE_ERROR,
13736 tr("Invalid saved state file path '%ls' (%Rrc)"),
13737 aSavedStateFile,
13738 vrc);
13739
13740 mSSData->strStateFilePath = stateFilePathFull;
13741
13742 /* The below setMachineState() will detect the state transition and will
13743 * update the settings file */
13744
13745 return setMachineState(MachineState_Saved);
13746}
13747
13748STDMETHODIMP SessionMachine::PullGuestProperties(ComSafeArrayOut(BSTR, aNames),
13749 ComSafeArrayOut(BSTR, aValues),
13750 ComSafeArrayOut(LONG64, aTimestamps),
13751 ComSafeArrayOut(BSTR, aFlags))
13752{
13753 LogFlowThisFunc(("\n"));
13754
13755#ifdef VBOX_WITH_GUEST_PROPS
13756 using namespace guestProp;
13757
13758 AutoCaller autoCaller(this);
13759 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
13760
13761 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
13762
13763 CheckComArgOutSafeArrayPointerValid(aNames);
13764 CheckComArgOutSafeArrayPointerValid(aValues);
13765 CheckComArgOutSafeArrayPointerValid(aTimestamps);
13766 CheckComArgOutSafeArrayPointerValid(aFlags);
13767
13768 size_t cEntries = mHWData->mGuestProperties.size();
13769 com::SafeArray<BSTR> names(cEntries);
13770 com::SafeArray<BSTR> values(cEntries);
13771 com::SafeArray<LONG64> timestamps(cEntries);
13772 com::SafeArray<BSTR> flags(cEntries);
13773 unsigned i = 0;
13774 for (HWData::GuestPropertyMap::iterator it = mHWData->mGuestProperties.begin();
13775 it != mHWData->mGuestProperties.end();
13776 ++it)
13777 {
13778 char szFlags[MAX_FLAGS_LEN + 1];
13779 it->first.cloneTo(&names[i]);
13780 it->second.strValue.cloneTo(&values[i]);
13781 timestamps[i] = it->second.mTimestamp;
13782 /* If it is NULL, keep it NULL. */
13783 if (it->second.mFlags)
13784 {
13785 writeFlags(it->second.mFlags, szFlags);
13786 Bstr(szFlags).cloneTo(&flags[i]);
13787 }
13788 else
13789 flags[i] = NULL;
13790 ++i;
13791 }
13792 names.detachTo(ComSafeArrayOutArg(aNames));
13793 values.detachTo(ComSafeArrayOutArg(aValues));
13794 timestamps.detachTo(ComSafeArrayOutArg(aTimestamps));
13795 flags.detachTo(ComSafeArrayOutArg(aFlags));
13796 return S_OK;
13797#else
13798 ReturnComNotImplemented();
13799#endif
13800}
13801
13802STDMETHODIMP SessionMachine::PushGuestProperty(IN_BSTR aName,
13803 IN_BSTR aValue,
13804 LONG64 aTimestamp,
13805 IN_BSTR aFlags)
13806{
13807 LogFlowThisFunc(("\n"));
13808
13809#ifdef VBOX_WITH_GUEST_PROPS
13810 using namespace guestProp;
13811
13812 CheckComArgStrNotEmptyOrNull(aName);
13813 CheckComArgNotNull(aValue);
13814 CheckComArgNotNull(aFlags);
13815
13816 try
13817 {
13818 /*
13819 * Convert input up front.
13820 */
13821 Utf8Str utf8Name(aName);
13822 uint32_t fFlags = NILFLAG;
13823 if (aFlags)
13824 {
13825 Utf8Str utf8Flags(aFlags);
13826 int vrc = validateFlags(utf8Flags.c_str(), &fFlags);
13827 AssertRCReturn(vrc, E_INVALIDARG);
13828 }
13829
13830 /*
13831 * Now grab the object lock, validate the state and do the update.
13832 */
13833 AutoCaller autoCaller(this);
13834 if (FAILED(autoCaller.rc())) return autoCaller.rc();
13835
13836 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
13837
13838 switch (mData->mMachineState)
13839 {
13840 case MachineState_Paused:
13841 case MachineState_Running:
13842 case MachineState_Teleporting:
13843 case MachineState_TeleportingPausedVM:
13844 case MachineState_LiveSnapshotting:
13845 case MachineState_DeletingSnapshotOnline:
13846 case MachineState_DeletingSnapshotPaused:
13847 case MachineState_Saving:
13848 case MachineState_Stopping:
13849 break;
13850
13851 default:
13852 AssertMsgFailedReturn(("%s\n", Global::stringifyMachineState(mData->mMachineState)),
13853 VBOX_E_INVALID_VM_STATE);
13854 }
13855
13856 setModified(IsModified_MachineData);
13857 mHWData.backup();
13858
13859 bool fDelete = !RT_VALID_PTR(aValue) || *(aValue) == '\0';
13860 HWData::GuestPropertyMap::iterator it = mHWData->mGuestProperties.find(utf8Name);
13861 if (it != mHWData->mGuestProperties.end())
13862 {
13863 if (!fDelete)
13864 {
13865 it->second.strValue = aValue;
13866 it->second.mTimestamp = aTimestamp;
13867 it->second.mFlags = fFlags;
13868 }
13869 else
13870 mHWData->mGuestProperties.erase(it);
13871
13872 mData->mGuestPropertiesModified = TRUE;
13873 }
13874 else if (!fDelete)
13875 {
13876 HWData::GuestProperty prop;
13877 prop.strValue = aValue;
13878 prop.mTimestamp = aTimestamp;
13879 prop.mFlags = fFlags;
13880
13881 mHWData->mGuestProperties[utf8Name] = prop;
13882 mData->mGuestPropertiesModified = TRUE;
13883 }
13884
13885 /*
13886 * Send a callback notification if appropriate
13887 */
13888 if ( mHWData->mGuestPropertyNotificationPatterns.isEmpty()
13889 || RTStrSimplePatternMultiMatch(mHWData->mGuestPropertyNotificationPatterns.c_str(),
13890 RTSTR_MAX,
13891 utf8Name.c_str(),
13892 RTSTR_MAX, NULL)
13893 )
13894 {
13895 alock.release();
13896
13897 mParent->i_onGuestPropertyChange(mData->mUuid,
13898 aName,
13899 aValue,
13900 aFlags);
13901 }
13902 }
13903 catch (...)
13904 {
13905 return VirtualBoxBase::handleUnexpectedExceptions(this, RT_SRC_POS);
13906 }
13907 return S_OK;
13908#else
13909 ReturnComNotImplemented();
13910#endif
13911}
13912
13913STDMETHODIMP SessionMachine::LockMedia()
13914{
13915 AutoCaller autoCaller(this);
13916 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
13917
13918 AutoMultiWriteLock2 alock(this->lockHandle(),
13919 &mParent->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
13920
13921 AssertReturn( mData->mMachineState == MachineState_Starting
13922 || mData->mMachineState == MachineState_Restoring
13923 || mData->mMachineState == MachineState_TeleportingIn, E_FAIL);
13924
13925 clearError();
13926 alock.release();
13927 return lockMedia();
13928}
13929
13930STDMETHODIMP SessionMachine::UnlockMedia()
13931{
13932 unlockMedia();
13933 return S_OK;
13934}
13935
13936STDMETHODIMP SessionMachine::EjectMedium(IMediumAttachment *aAttachment,
13937 IMediumAttachment **aNewAttachment)
13938{
13939 CheckComArgNotNull(aAttachment);
13940 CheckComArgOutPointerValid(aNewAttachment);
13941
13942 AutoCaller autoCaller(this);
13943 if (FAILED(autoCaller.rc())) return autoCaller.rc();
13944
13945 // request the host lock first, since might be calling Host methods for getting host drives;
13946 // next, protect the media tree all the while we're in here, as well as our member variables
13947 AutoMultiWriteLock3 multiLock(mParent->i_host()->lockHandle(),
13948 this->lockHandle(),
13949 &mParent->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
13950
13951 ComObjPtr<MediumAttachment> pAttach = static_cast<MediumAttachment *>(aAttachment);
13952
13953 Bstr ctrlName;
13954 LONG lPort;
13955 LONG lDevice;
13956 bool fTempEject;
13957 {
13958 AutoCaller autoAttachCaller(this);
13959 if (FAILED(autoAttachCaller.rc())) return autoAttachCaller.rc();
13960
13961 AutoReadLock attLock(pAttach COMMA_LOCKVAL_SRC_POS);
13962
13963 /* Need to query the details first, as the IMediumAttachment reference
13964 * might be to the original settings, which we are going to change. */
13965 ctrlName = pAttach->i_getControllerName();
13966 lPort = pAttach->i_getPort();
13967 lDevice = pAttach->i_getDevice();
13968 fTempEject = pAttach->i_getTempEject();
13969 }
13970
13971 if (!fTempEject)
13972 {
13973 /* Remember previously mounted medium. The medium before taking the
13974 * backup is not necessarily the same thing. */
13975 ComObjPtr<Medium> oldmedium;
13976 oldmedium = pAttach->i_getMedium();
13977
13978 setModified(IsModified_Storage);
13979 mMediaData.backup();
13980
13981 // The backup operation makes the pAttach reference point to the
13982 // old settings. Re-get the correct reference.
13983 pAttach = findAttachment(mMediaData->mAttachments,
13984 ctrlName.raw(),
13985 lPort,
13986 lDevice);
13987
13988 {
13989 AutoCaller autoAttachCaller(this);
13990 if (FAILED(autoAttachCaller.rc())) return autoAttachCaller.rc();
13991
13992 AutoWriteLock attLock(pAttach COMMA_LOCKVAL_SRC_POS);
13993 if (!oldmedium.isNull())
13994 oldmedium->i_removeBackReference(mData->mUuid);
13995
13996 pAttach->i_updateMedium(NULL);
13997 pAttach->i_updateEjected();
13998 }
13999
14000 setModified(IsModified_Storage);
14001 }
14002 else
14003 {
14004 {
14005 AutoWriteLock attLock(pAttach COMMA_LOCKVAL_SRC_POS);
14006 pAttach->i_updateEjected();
14007 }
14008 }
14009
14010 pAttach.queryInterfaceTo(aNewAttachment);
14011
14012 return S_OK;
14013}
14014
14015// public methods only for internal purposes
14016/////////////////////////////////////////////////////////////////////////////
14017
14018#ifndef VBOX_WITH_GENERIC_SESSION_WATCHER
14019/**
14020 * Called from the client watcher thread to check for expected or unexpected
14021 * death of the client process that has a direct session to this machine.
14022 *
14023 * On Win32 and on OS/2, this method is called only when we've got the
14024 * mutex (i.e. the client has either died or terminated normally) so it always
14025 * returns @c true (the client is terminated, the session machine is
14026 * uninitialized).
14027 *
14028 * On other platforms, the method returns @c true if the client process has
14029 * terminated normally or abnormally and the session machine was uninitialized,
14030 * and @c false if the client process is still alive.
14031 *
14032 * @note Locks this object for writing.
14033 */
14034bool SessionMachine::checkForDeath()
14035{
14036 Uninit::Reason reason;
14037 bool terminated = false;
14038
14039 /* Enclose autoCaller with a block because calling uninit() from under it
14040 * will deadlock. */
14041 {
14042 AutoCaller autoCaller(this);
14043 if (!autoCaller.isOk())
14044 {
14045 /* return true if not ready, to cause the client watcher to exclude
14046 * the corresponding session from watching */
14047 LogFlowThisFunc(("Already uninitialized!\n"));
14048 return true;
14049 }
14050
14051 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
14052
14053 /* Determine the reason of death: if the session state is Closing here,
14054 * everything is fine. Otherwise it means that the client did not call
14055 * OnSessionEnd() before it released the IPC semaphore. This may happen
14056 * either because the client process has abnormally terminated, or
14057 * because it simply forgot to call ISession::Close() before exiting. We
14058 * threat the latter also as an abnormal termination (see
14059 * Session::uninit() for details). */
14060 reason = mData->mSession.mState == SessionState_Unlocking ?
14061 Uninit::Normal :
14062 Uninit::Abnormal;
14063
14064 if (mClientToken)
14065 terminated = mClientToken->release();
14066 } /* AutoCaller block */
14067
14068 if (terminated)
14069 uninit(reason);
14070
14071 return terminated;
14072}
14073
14074void SessionMachine::getTokenId(Utf8Str &strTokenId)
14075{
14076 LogFlowThisFunc(("\n"));
14077
14078 strTokenId.setNull();
14079
14080 AutoCaller autoCaller(this);
14081 AssertComRCReturnVoid(autoCaller.rc());
14082
14083 Assert(mClientToken);
14084 if (mClientToken)
14085 mClientToken->getId(strTokenId);
14086}
14087#else /* VBOX_WITH_GENERIC_SESSION_WATCHER */
14088IToken *SessionMachine::getToken()
14089{
14090 LogFlowThisFunc(("\n"));
14091
14092 AutoCaller autoCaller(this);
14093 AssertComRCReturn(autoCaller.rc(), NULL);
14094
14095 Assert(mClientToken);
14096 if (mClientToken)
14097 return mClientToken->getToken();
14098 else
14099 return NULL;
14100}
14101#endif /* VBOX_WITH_GENERIC_SESSION_WATCHER */
14102
14103Machine::ClientToken *SessionMachine::getClientToken()
14104{
14105 LogFlowThisFunc(("\n"));
14106
14107 AutoCaller autoCaller(this);
14108 AssertComRCReturn(autoCaller.rc(), NULL);
14109
14110 return mClientToken;
14111}
14112
14113
14114/**
14115 * @note Locks this object for reading.
14116 */
14117HRESULT SessionMachine::onNetworkAdapterChange(INetworkAdapter *networkAdapter, BOOL changeAdapter)
14118{
14119 LogFlowThisFunc(("\n"));
14120
14121 AutoCaller autoCaller(this);
14122 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
14123
14124 ComPtr<IInternalSessionControl> directControl;
14125 {
14126 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
14127 directControl = mData->mSession.mDirectControl;
14128 }
14129
14130 /* ignore notifications sent after #OnSessionEnd() is called */
14131 if (!directControl)
14132 return S_OK;
14133
14134 return directControl->OnNetworkAdapterChange(networkAdapter, changeAdapter);
14135}
14136
14137/**
14138 * @note Locks this object for reading.
14139 */
14140HRESULT SessionMachine::onNATRedirectRuleChange(ULONG ulSlot, BOOL aNatRuleRemove, IN_BSTR aRuleName,
14141 NATProtocol_T aProto, IN_BSTR aHostIp, LONG aHostPort, IN_BSTR aGuestIp, LONG aGuestPort)
14142{
14143 LogFlowThisFunc(("\n"));
14144
14145 AutoCaller autoCaller(this);
14146 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
14147
14148 ComPtr<IInternalSessionControl> directControl;
14149 {
14150 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
14151 directControl = mData->mSession.mDirectControl;
14152 }
14153
14154 /* ignore notifications sent after #OnSessionEnd() is called */
14155 if (!directControl)
14156 return S_OK;
14157 /*
14158 * instead acting like callback we ask IVirtualBox deliver corresponding event
14159 */
14160
14161 mParent->i_onNatRedirectChange(getId(), ulSlot, RT_BOOL(aNatRuleRemove), aRuleName, aProto, aHostIp, (uint16_t)aHostPort, aGuestIp, (uint16_t)aGuestPort);
14162 return S_OK;
14163}
14164
14165/**
14166 * @note Locks this object for reading.
14167 */
14168HRESULT SessionMachine::onSerialPortChange(ISerialPort *serialPort)
14169{
14170 LogFlowThisFunc(("\n"));
14171
14172 AutoCaller autoCaller(this);
14173 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
14174
14175 ComPtr<IInternalSessionControl> directControl;
14176 {
14177 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
14178 directControl = mData->mSession.mDirectControl;
14179 }
14180
14181 /* ignore notifications sent after #OnSessionEnd() is called */
14182 if (!directControl)
14183 return S_OK;
14184
14185 return directControl->OnSerialPortChange(serialPort);
14186}
14187
14188/**
14189 * @note Locks this object for reading.
14190 */
14191HRESULT SessionMachine::onParallelPortChange(IParallelPort *parallelPort)
14192{
14193 LogFlowThisFunc(("\n"));
14194
14195 AutoCaller autoCaller(this);
14196 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
14197
14198 ComPtr<IInternalSessionControl> directControl;
14199 {
14200 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
14201 directControl = mData->mSession.mDirectControl;
14202 }
14203
14204 /* ignore notifications sent after #OnSessionEnd() is called */
14205 if (!directControl)
14206 return S_OK;
14207
14208 return directControl->OnParallelPortChange(parallelPort);
14209}
14210
14211/**
14212 * @note Locks this object for reading.
14213 */
14214HRESULT SessionMachine::onStorageControllerChange()
14215{
14216 LogFlowThisFunc(("\n"));
14217
14218 AutoCaller autoCaller(this);
14219 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
14220
14221 ComPtr<IInternalSessionControl> directControl;
14222 {
14223 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
14224 directControl = mData->mSession.mDirectControl;
14225 }
14226
14227 /* ignore notifications sent after #OnSessionEnd() is called */
14228 if (!directControl)
14229 return S_OK;
14230
14231 return directControl->OnStorageControllerChange();
14232}
14233
14234/**
14235 * @note Locks this object for reading.
14236 */
14237HRESULT SessionMachine::onMediumChange(IMediumAttachment *aAttachment, BOOL aForce)
14238{
14239 LogFlowThisFunc(("\n"));
14240
14241 AutoCaller autoCaller(this);
14242 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
14243
14244 ComPtr<IInternalSessionControl> directControl;
14245 {
14246 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
14247 directControl = mData->mSession.mDirectControl;
14248 }
14249
14250 /* ignore notifications sent after #OnSessionEnd() is called */
14251 if (!directControl)
14252 return S_OK;
14253
14254 return directControl->OnMediumChange(aAttachment, aForce);
14255}
14256
14257/**
14258 * @note Locks this object for reading.
14259 */
14260HRESULT SessionMachine::onCPUChange(ULONG aCPU, BOOL aRemove)
14261{
14262 LogFlowThisFunc(("\n"));
14263
14264 AutoCaller autoCaller(this);
14265 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
14266
14267 ComPtr<IInternalSessionControl> directControl;
14268 {
14269 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
14270 directControl = mData->mSession.mDirectControl;
14271 }
14272
14273 /* ignore notifications sent after #OnSessionEnd() is called */
14274 if (!directControl)
14275 return S_OK;
14276
14277 return directControl->OnCPUChange(aCPU, aRemove);
14278}
14279
14280HRESULT SessionMachine::onCPUExecutionCapChange(ULONG aExecutionCap)
14281{
14282 LogFlowThisFunc(("\n"));
14283
14284 AutoCaller autoCaller(this);
14285 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
14286
14287 ComPtr<IInternalSessionControl> directControl;
14288 {
14289 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
14290 directControl = mData->mSession.mDirectControl;
14291 }
14292
14293 /* ignore notifications sent after #OnSessionEnd() is called */
14294 if (!directControl)
14295 return S_OK;
14296
14297 return directControl->OnCPUExecutionCapChange(aExecutionCap);
14298}
14299
14300/**
14301 * @note Locks this object for reading.
14302 */
14303HRESULT SessionMachine::onVRDEServerChange(BOOL aRestart)
14304{
14305 LogFlowThisFunc(("\n"));
14306
14307 AutoCaller autoCaller(this);
14308 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
14309
14310 ComPtr<IInternalSessionControl> directControl;
14311 {
14312 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
14313 directControl = mData->mSession.mDirectControl;
14314 }
14315
14316 /* ignore notifications sent after #OnSessionEnd() is called */
14317 if (!directControl)
14318 return S_OK;
14319
14320 return directControl->OnVRDEServerChange(aRestart);
14321}
14322
14323/**
14324 * @note Locks this object for reading.
14325 */
14326HRESULT SessionMachine::onVideoCaptureChange()
14327{
14328 LogFlowThisFunc(("\n"));
14329
14330 AutoCaller autoCaller(this);
14331 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
14332
14333 ComPtr<IInternalSessionControl> directControl;
14334 {
14335 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
14336 directControl = mData->mSession.mDirectControl;
14337 }
14338
14339 /* ignore notifications sent after #OnSessionEnd() is called */
14340 if (!directControl)
14341 return S_OK;
14342
14343 return directControl->OnVideoCaptureChange();
14344}
14345
14346/**
14347 * @note Locks this object for reading.
14348 */
14349HRESULT SessionMachine::onUSBControllerChange()
14350{
14351 LogFlowThisFunc(("\n"));
14352
14353 AutoCaller autoCaller(this);
14354 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
14355
14356 ComPtr<IInternalSessionControl> directControl;
14357 {
14358 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
14359 directControl = mData->mSession.mDirectControl;
14360 }
14361
14362 /* ignore notifications sent after #OnSessionEnd() is called */
14363 if (!directControl)
14364 return S_OK;
14365
14366 return directControl->OnUSBControllerChange();
14367}
14368
14369/**
14370 * @note Locks this object for reading.
14371 */
14372HRESULT SessionMachine::onSharedFolderChange()
14373{
14374 LogFlowThisFunc(("\n"));
14375
14376 AutoCaller autoCaller(this);
14377 AssertComRCReturnRC(autoCaller.rc());
14378
14379 ComPtr<IInternalSessionControl> directControl;
14380 {
14381 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
14382 directControl = mData->mSession.mDirectControl;
14383 }
14384
14385 /* ignore notifications sent after #OnSessionEnd() is called */
14386 if (!directControl)
14387 return S_OK;
14388
14389 return directControl->OnSharedFolderChange(FALSE /* aGlobal */);
14390}
14391
14392/**
14393 * @note Locks this object for reading.
14394 */
14395HRESULT SessionMachine::onClipboardModeChange(ClipboardMode_T aClipboardMode)
14396{
14397 LogFlowThisFunc(("\n"));
14398
14399 AutoCaller autoCaller(this);
14400 AssertComRCReturnRC(autoCaller.rc());
14401
14402 ComPtr<IInternalSessionControl> directControl;
14403 {
14404 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
14405 directControl = mData->mSession.mDirectControl;
14406 }
14407
14408 /* ignore notifications sent after #OnSessionEnd() is called */
14409 if (!directControl)
14410 return S_OK;
14411
14412 return directControl->OnClipboardModeChange(aClipboardMode);
14413}
14414
14415/**
14416 * @note Locks this object for reading.
14417 */
14418HRESULT SessionMachine::onDragAndDropModeChange(DragAndDropMode_T aDragAndDropMode)
14419{
14420 LogFlowThisFunc(("\n"));
14421
14422 AutoCaller autoCaller(this);
14423 AssertComRCReturnRC(autoCaller.rc());
14424
14425 ComPtr<IInternalSessionControl> directControl;
14426 {
14427 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
14428 directControl = mData->mSession.mDirectControl;
14429 }
14430
14431 /* ignore notifications sent after #OnSessionEnd() is called */
14432 if (!directControl)
14433 return S_OK;
14434
14435 return directControl->OnDragAndDropModeChange(aDragAndDropMode);
14436}
14437
14438/**
14439 * @note Locks this object for reading.
14440 */
14441HRESULT SessionMachine::onBandwidthGroupChange(IBandwidthGroup *aBandwidthGroup)
14442{
14443 LogFlowThisFunc(("\n"));
14444
14445 AutoCaller autoCaller(this);
14446 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
14447
14448 ComPtr<IInternalSessionControl> directControl;
14449 {
14450 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
14451 directControl = mData->mSession.mDirectControl;
14452 }
14453
14454 /* ignore notifications sent after #OnSessionEnd() is called */
14455 if (!directControl)
14456 return S_OK;
14457
14458 return directControl->OnBandwidthGroupChange(aBandwidthGroup);
14459}
14460
14461/**
14462 * @note Locks this object for reading.
14463 */
14464HRESULT SessionMachine::onStorageDeviceChange(IMediumAttachment *aAttachment, BOOL aRemove, BOOL aSilent)
14465{
14466 LogFlowThisFunc(("\n"));
14467
14468 AutoCaller autoCaller(this);
14469 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
14470
14471 ComPtr<IInternalSessionControl> directControl;
14472 {
14473 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
14474 directControl = mData->mSession.mDirectControl;
14475 }
14476
14477 /* ignore notifications sent after #OnSessionEnd() is called */
14478 if (!directControl)
14479 return S_OK;
14480
14481 return directControl->OnStorageDeviceChange(aAttachment, aRemove, aSilent);
14482}
14483
14484/**
14485 * Returns @c true if this machine's USB controller reports it has a matching
14486 * filter for the given USB device and @c false otherwise.
14487 *
14488 * @note locks this object for reading.
14489 */
14490bool SessionMachine::hasMatchingUSBFilter(const ComObjPtr<HostUSBDevice> &aDevice, ULONG *aMaskedIfs)
14491{
14492 AutoCaller autoCaller(this);
14493 /* silently return if not ready -- this method may be called after the
14494 * direct machine session has been called */
14495 if (!autoCaller.isOk())
14496 return false;
14497
14498#ifdef VBOX_WITH_USB
14499 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
14500
14501 switch (mData->mMachineState)
14502 {
14503 case MachineState_Starting:
14504 case MachineState_Restoring:
14505 case MachineState_TeleportingIn:
14506 case MachineState_Paused:
14507 case MachineState_Running:
14508 /** @todo Live Migration: snapshoting & teleporting. Need to fend things of
14509 * elsewhere... */
14510 alock.release();
14511 return mUSBDeviceFilters->i_hasMatchingFilter(aDevice, aMaskedIfs);
14512 default: break;
14513 }
14514#else
14515 NOREF(aDevice);
14516 NOREF(aMaskedIfs);
14517#endif
14518 return false;
14519}
14520
14521/**
14522 * @note The calls shall hold no locks. Will temporarily lock this object for reading.
14523 */
14524HRESULT SessionMachine::onUSBDeviceAttach(IUSBDevice *aDevice,
14525 IVirtualBoxErrorInfo *aError,
14526 ULONG aMaskedIfs)
14527{
14528 LogFlowThisFunc(("\n"));
14529
14530 AutoCaller autoCaller(this);
14531
14532 /* This notification may happen after the machine object has been
14533 * uninitialized (the session was closed), so don't assert. */
14534 if (FAILED(autoCaller.rc())) return autoCaller.rc();
14535
14536 ComPtr<IInternalSessionControl> directControl;
14537 {
14538 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
14539 directControl = mData->mSession.mDirectControl;
14540 }
14541
14542 /* fail on notifications sent after #OnSessionEnd() is called, it is
14543 * expected by the caller */
14544 if (!directControl)
14545 return E_FAIL;
14546
14547 /* No locks should be held at this point. */
14548 AssertMsg(RTLockValidatorWriteLockGetCount(RTThreadSelf()) == 0, ("%d\n", RTLockValidatorWriteLockGetCount(RTThreadSelf())));
14549 AssertMsg(RTLockValidatorReadLockGetCount(RTThreadSelf()) == 0, ("%d\n", RTLockValidatorReadLockGetCount(RTThreadSelf())));
14550
14551 return directControl->OnUSBDeviceAttach(aDevice, aError, aMaskedIfs);
14552}
14553
14554/**
14555 * @note The calls shall hold no locks. Will temporarily lock this object for reading.
14556 */
14557HRESULT SessionMachine::onUSBDeviceDetach(IN_BSTR aId,
14558 IVirtualBoxErrorInfo *aError)
14559{
14560 LogFlowThisFunc(("\n"));
14561
14562 AutoCaller autoCaller(this);
14563
14564 /* This notification may happen after the machine object has been
14565 * uninitialized (the session was closed), so don't assert. */
14566 if (FAILED(autoCaller.rc())) return autoCaller.rc();
14567
14568 ComPtr<IInternalSessionControl> directControl;
14569 {
14570 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
14571 directControl = mData->mSession.mDirectControl;
14572 }
14573
14574 /* fail on notifications sent after #OnSessionEnd() is called, it is
14575 * expected by the caller */
14576 if (!directControl)
14577 return E_FAIL;
14578
14579 /* No locks should be held at this point. */
14580 AssertMsg(RTLockValidatorWriteLockGetCount(RTThreadSelf()) == 0, ("%d\n", RTLockValidatorWriteLockGetCount(RTThreadSelf())));
14581 AssertMsg(RTLockValidatorReadLockGetCount(RTThreadSelf()) == 0, ("%d\n", RTLockValidatorReadLockGetCount(RTThreadSelf())));
14582
14583 return directControl->OnUSBDeviceDetach(aId, aError);
14584}
14585
14586// protected methods
14587/////////////////////////////////////////////////////////////////////////////
14588
14589/**
14590 * Helper method to finalize saving the state.
14591 *
14592 * @note Must be called from under this object's lock.
14593 *
14594 * @param aRc S_OK if the snapshot has been taken successfully
14595 * @param aErrMsg human readable error message for failure
14596 *
14597 * @note Locks mParent + this objects for writing.
14598 */
14599HRESULT SessionMachine::endSavingState(HRESULT aRc, const Utf8Str &aErrMsg)
14600{
14601 LogFlowThisFuncEnter();
14602
14603 AutoCaller autoCaller(this);
14604 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
14605
14606 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
14607
14608 HRESULT rc = S_OK;
14609
14610 if (SUCCEEDED(aRc))
14611 {
14612 mSSData->strStateFilePath = mConsoleTaskData.strStateFilePath;
14613
14614 /* save all VM settings */
14615 rc = saveSettings(NULL);
14616 // no need to check whether VirtualBox.xml needs saving also since
14617 // we can't have a name change pending at this point
14618 }
14619 else
14620 {
14621 // delete the saved state file (it might have been already created);
14622 // we need not check whether this is shared with a snapshot here because
14623 // we certainly created this saved state file here anew
14624 RTFileDelete(mConsoleTaskData.strStateFilePath.c_str());
14625 }
14626
14627 /* notify the progress object about operation completion */
14628 Assert(mConsoleTaskData.mProgress);
14629 if (SUCCEEDED(aRc))
14630 mConsoleTaskData.mProgress->notifyComplete(S_OK);
14631 else
14632 {
14633 if (aErrMsg.length())
14634 mConsoleTaskData.mProgress->notifyComplete(aRc,
14635 COM_IIDOF(ISession),
14636 getComponentName(),
14637 aErrMsg.c_str());
14638 else
14639 mConsoleTaskData.mProgress->notifyComplete(aRc);
14640 }
14641
14642 /* clear out the temporary saved state data */
14643 mConsoleTaskData.mLastState = MachineState_Null;
14644 mConsoleTaskData.strStateFilePath.setNull();
14645 mConsoleTaskData.mProgress.setNull();
14646
14647 LogFlowThisFuncLeave();
14648 return rc;
14649}
14650
14651/**
14652 * Deletes the given file if it is no longer in use by either the current machine state
14653 * (if the machine is "saved") or any of the machine's snapshots.
14654 *
14655 * Note: This checks mSSData->strStateFilePath, which is shared by the Machine and SessionMachine
14656 * but is different for each SnapshotMachine. When calling this, the order of calling this
14657 * function on the one hand and changing that variable OR the snapshots tree on the other hand
14658 * is therefore critical. I know, it's all rather messy.
14659 *
14660 * @param strStateFile
14661 * @param pSnapshotToIgnore Passed to Snapshot::sharesSavedStateFile(); this snapshot is ignored in the test for whether the saved state file is in use.
14662 */
14663void SessionMachine::releaseSavedStateFile(const Utf8Str &strStateFile,
14664 Snapshot *pSnapshotToIgnore)
14665{
14666 // it is safe to delete this saved state file if it is not currently in use by the machine ...
14667 if ( (strStateFile.isNotEmpty())
14668 && (strStateFile != mSSData->strStateFilePath) // session machine's saved state
14669 )
14670 // ... and it must also not be shared with other snapshots
14671 if ( !mData->mFirstSnapshot
14672 || !mData->mFirstSnapshot->i_sharesSavedStateFile(strStateFile, pSnapshotToIgnore)
14673 // this checks the SnapshotMachine's state file paths
14674 )
14675 RTFileDelete(strStateFile.c_str());
14676}
14677
14678/**
14679 * Locks the attached media.
14680 *
14681 * All attached hard disks are locked for writing and DVD/floppy are locked for
14682 * reading. Parents of attached hard disks (if any) are locked for reading.
14683 *
14684 * This method also performs accessibility check of all media it locks: if some
14685 * media is inaccessible, the method will return a failure and a bunch of
14686 * extended error info objects per each inaccessible medium.
14687 *
14688 * Note that this method is atomic: if it returns a success, all media are
14689 * locked as described above; on failure no media is locked at all (all
14690 * succeeded individual locks will be undone).
14691 *
14692 * The caller is responsible for doing the necessary state sanity checks.
14693 *
14694 * The locks made by this method must be undone by calling #unlockMedia() when
14695 * no more needed.
14696 */
14697HRESULT SessionMachine::lockMedia()
14698{
14699 AutoCaller autoCaller(this);
14700 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
14701
14702 AutoMultiWriteLock2 alock(this->lockHandle(),
14703 &mParent->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
14704
14705 /* bail out if trying to lock things with already set up locking */
14706 AssertReturn(mData->mSession.mLockedMedia.IsEmpty(), E_FAIL);
14707
14708 MultiResult mrc(S_OK);
14709
14710 /* Collect locking information for all medium objects attached to the VM. */
14711 for (MediaData::AttachmentList::const_iterator it = mMediaData->mAttachments.begin();
14712 it != mMediaData->mAttachments.end();
14713 ++it)
14714 {
14715 MediumAttachment* pAtt = *it;
14716 DeviceType_T devType = pAtt->i_getType();
14717 Medium *pMedium = pAtt->i_getMedium();
14718
14719 MediumLockList *pMediumLockList(new MediumLockList());
14720 // There can be attachments without a medium (floppy/dvd), and thus
14721 // it's impossible to create a medium lock list. It still makes sense
14722 // to have the empty medium lock list in the map in case a medium is
14723 // attached later.
14724 if (pMedium != NULL)
14725 {
14726 MediumType_T mediumType = pMedium->i_getType();
14727 bool fIsReadOnlyLock = mediumType == MediumType_Readonly
14728 || mediumType == MediumType_Shareable;
14729 bool fIsVitalImage = (devType == DeviceType_HardDisk);
14730
14731 alock.release();
14732 mrc = pMedium->i_createMediumLockList(fIsVitalImage /* fFailIfInaccessible */,
14733 !fIsReadOnlyLock /* fMediumLockWrite */,
14734 NULL,
14735 *pMediumLockList);
14736 alock.acquire();
14737 if (FAILED(mrc))
14738 {
14739 delete pMediumLockList;
14740 mData->mSession.mLockedMedia.Clear();
14741 break;
14742 }
14743 }
14744
14745 HRESULT rc = mData->mSession.mLockedMedia.Insert(pAtt, pMediumLockList);
14746 if (FAILED(rc))
14747 {
14748 mData->mSession.mLockedMedia.Clear();
14749 mrc = setError(rc,
14750 tr("Collecting locking information for all attached media failed"));
14751 break;
14752 }
14753 }
14754
14755 if (SUCCEEDED(mrc))
14756 {
14757 /* Now lock all media. If this fails, nothing is locked. */
14758 alock.release();
14759 HRESULT rc = mData->mSession.mLockedMedia.Lock();
14760 alock.acquire();
14761 if (FAILED(rc))
14762 {
14763 mrc = setError(rc,
14764 tr("Locking of attached media failed"));
14765 }
14766 }
14767
14768 return mrc;
14769}
14770
14771/**
14772 * Undoes the locks made by by #lockMedia().
14773 */
14774void SessionMachine::unlockMedia()
14775{
14776 AutoCaller autoCaller(this);
14777 AssertComRCReturnVoid(autoCaller.rc());
14778
14779 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
14780
14781 /* we may be holding important error info on the current thread;
14782 * preserve it */
14783 ErrorInfoKeeper eik;
14784
14785 HRESULT rc = mData->mSession.mLockedMedia.Clear();
14786 AssertComRC(rc);
14787}
14788
14789/**
14790 * Helper to change the machine state (reimplementation).
14791 *
14792 * @note Locks this object for writing.
14793 * @note This method must not call saveSettings or SaveSettings, otherwise
14794 * it can cause crashes in random places due to unexpectedly committing
14795 * the current settings. The caller is responsible for that. The call
14796 * to saveStateSettings is fine, because this method does not commit.
14797 */
14798HRESULT SessionMachine::setMachineState(MachineState_T aMachineState)
14799{
14800 LogFlowThisFuncEnter();
14801 LogFlowThisFunc(("aMachineState=%s\n", Global::stringifyMachineState(aMachineState) ));
14802
14803 AutoCaller autoCaller(this);
14804 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
14805
14806 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
14807
14808 MachineState_T oldMachineState = mData->mMachineState;
14809
14810 AssertMsgReturn(oldMachineState != aMachineState,
14811 ("oldMachineState=%s, aMachineState=%s\n",
14812 Global::stringifyMachineState(oldMachineState), Global::stringifyMachineState(aMachineState)),
14813 E_FAIL);
14814
14815 HRESULT rc = S_OK;
14816
14817 int stsFlags = 0;
14818 bool deleteSavedState = false;
14819
14820 /* detect some state transitions */
14821
14822 if ( ( oldMachineState == MachineState_Saved
14823 && aMachineState == MachineState_Restoring)
14824 || ( ( oldMachineState == MachineState_PoweredOff
14825 || oldMachineState == MachineState_Teleported
14826 || oldMachineState == MachineState_Aborted
14827 )
14828 && ( aMachineState == MachineState_TeleportingIn
14829 || aMachineState == MachineState_Starting
14830 )
14831 )
14832 )
14833 {
14834 /* The EMT thread is about to start */
14835
14836 /* Nothing to do here for now... */
14837
14838 /// @todo NEWMEDIA don't let mDVDDrive and other children
14839 /// change anything when in the Starting/Restoring state
14840 }
14841 else if ( ( oldMachineState == MachineState_Running
14842 || oldMachineState == MachineState_Paused
14843 || oldMachineState == MachineState_Teleporting
14844 || oldMachineState == MachineState_LiveSnapshotting
14845 || oldMachineState == MachineState_Stuck
14846 || oldMachineState == MachineState_Starting
14847 || oldMachineState == MachineState_Stopping
14848 || oldMachineState == MachineState_Saving
14849 || oldMachineState == MachineState_Restoring
14850 || oldMachineState == MachineState_TeleportingPausedVM
14851 || oldMachineState == MachineState_TeleportingIn
14852 )
14853 && ( aMachineState == MachineState_PoweredOff
14854 || aMachineState == MachineState_Saved
14855 || aMachineState == MachineState_Teleported
14856 || aMachineState == MachineState_Aborted
14857 )
14858 /* ignore PoweredOff->Saving->PoweredOff transition when taking a
14859 * snapshot */
14860 && ( mConsoleTaskData.mSnapshot.isNull()
14861 || mConsoleTaskData.mLastState >= MachineState_Running /** @todo Live Migration: clean up (lazy bird) */
14862 )
14863 )
14864 {
14865 /* The EMT thread has just stopped, unlock attached media. Note that as
14866 * opposed to locking that is done from Console, we do unlocking here
14867 * because the VM process may have aborted before having a chance to
14868 * properly unlock all media it locked. */
14869
14870 unlockMedia();
14871 }
14872
14873 if (oldMachineState == MachineState_Restoring)
14874 {
14875 if (aMachineState != MachineState_Saved)
14876 {
14877 /*
14878 * delete the saved state file once the machine has finished
14879 * restoring from it (note that Console sets the state from
14880 * Restoring to Saved if the VM couldn't restore successfully,
14881 * to give the user an ability to fix an error and retry --
14882 * we keep the saved state file in this case)
14883 */
14884 deleteSavedState = true;
14885 }
14886 }
14887 else if ( oldMachineState == MachineState_Saved
14888 && ( aMachineState == MachineState_PoweredOff
14889 || aMachineState == MachineState_Aborted
14890 || aMachineState == MachineState_Teleported
14891 )
14892 )
14893 {
14894 /*
14895 * delete the saved state after Console::ForgetSavedState() is called
14896 * or if the VM process (owning a direct VM session) crashed while the
14897 * VM was Saved
14898 */
14899
14900 /// @todo (dmik)
14901 // Not sure that deleting the saved state file just because of the
14902 // client death before it attempted to restore the VM is a good
14903 // thing. But when it crashes we need to go to the Aborted state
14904 // which cannot have the saved state file associated... The only
14905 // way to fix this is to make the Aborted condition not a VM state
14906 // but a bool flag: i.e., when a crash occurs, set it to true and
14907 // change the state to PoweredOff or Saved depending on the
14908 // saved state presence.
14909
14910 deleteSavedState = true;
14911 mData->mCurrentStateModified = TRUE;
14912 stsFlags |= SaveSTS_CurStateModified;
14913 }
14914
14915 if ( aMachineState == MachineState_Starting
14916 || aMachineState == MachineState_Restoring
14917 || aMachineState == MachineState_TeleportingIn
14918 )
14919 {
14920 /* set the current state modified flag to indicate that the current
14921 * state is no more identical to the state in the
14922 * current snapshot */
14923 if (!mData->mCurrentSnapshot.isNull())
14924 {
14925 mData->mCurrentStateModified = TRUE;
14926 stsFlags |= SaveSTS_CurStateModified;
14927 }
14928 }
14929
14930 if (deleteSavedState)
14931 {
14932 if (mRemoveSavedState)
14933 {
14934 Assert(!mSSData->strStateFilePath.isEmpty());
14935
14936 // it is safe to delete the saved state file if ...
14937 if ( !mData->mFirstSnapshot // ... we have no snapshots or
14938 || !mData->mFirstSnapshot->i_sharesSavedStateFile(mSSData->strStateFilePath, NULL /* pSnapshotToIgnore */)
14939 // ... none of the snapshots share the saved state file
14940 )
14941 RTFileDelete(mSSData->strStateFilePath.c_str());
14942 }
14943
14944 mSSData->strStateFilePath.setNull();
14945 stsFlags |= SaveSTS_StateFilePath;
14946 }
14947
14948 /* redirect to the underlying peer machine */
14949 mPeer->setMachineState(aMachineState);
14950
14951 if ( aMachineState == MachineState_PoweredOff
14952 || aMachineState == MachineState_Teleported
14953 || aMachineState == MachineState_Aborted
14954 || aMachineState == MachineState_Saved)
14955 {
14956 /* the machine has stopped execution
14957 * (or the saved state file was adopted) */
14958 stsFlags |= SaveSTS_StateTimeStamp;
14959 }
14960
14961 if ( ( oldMachineState == MachineState_PoweredOff
14962 || oldMachineState == MachineState_Aborted
14963 || oldMachineState == MachineState_Teleported
14964 )
14965 && aMachineState == MachineState_Saved)
14966 {
14967 /* the saved state file was adopted */
14968 Assert(!mSSData->strStateFilePath.isEmpty());
14969 stsFlags |= SaveSTS_StateFilePath;
14970 }
14971
14972#ifdef VBOX_WITH_GUEST_PROPS
14973 if ( aMachineState == MachineState_PoweredOff
14974 || aMachineState == MachineState_Aborted
14975 || aMachineState == MachineState_Teleported)
14976 {
14977 /* Make sure any transient guest properties get removed from the
14978 * property store on shutdown. */
14979
14980 HWData::GuestPropertyMap::const_iterator it;
14981 BOOL fNeedsSaving = mData->mGuestPropertiesModified;
14982 if (!fNeedsSaving)
14983 for (it = mHWData->mGuestProperties.begin();
14984 it != mHWData->mGuestProperties.end(); ++it)
14985 if ( (it->second.mFlags & guestProp::TRANSIENT)
14986 || (it->second.mFlags & guestProp::TRANSRESET))
14987 {
14988 fNeedsSaving = true;
14989 break;
14990 }
14991 if (fNeedsSaving)
14992 {
14993 mData->mCurrentStateModified = TRUE;
14994 stsFlags |= SaveSTS_CurStateModified;
14995 }
14996 }
14997#endif
14998
14999 rc = saveStateSettings(stsFlags);
15000
15001 if ( ( oldMachineState != MachineState_PoweredOff
15002 && oldMachineState != MachineState_Aborted
15003 && oldMachineState != MachineState_Teleported
15004 )
15005 && ( aMachineState == MachineState_PoweredOff
15006 || aMachineState == MachineState_Aborted
15007 || aMachineState == MachineState_Teleported
15008 )
15009 )
15010 {
15011 /* we've been shut down for any reason */
15012 /* no special action so far */
15013 }
15014
15015 LogFlowThisFunc(("rc=%Rhrc [%s]\n", rc, Global::stringifyMachineState(mData->mMachineState) ));
15016 LogFlowThisFuncLeave();
15017 return rc;
15018}
15019
15020/**
15021 * Sends the current machine state value to the VM process.
15022 *
15023 * @note Locks this object for reading, then calls a client process.
15024 */
15025HRESULT SessionMachine::updateMachineStateOnClient()
15026{
15027 AutoCaller autoCaller(this);
15028 AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
15029
15030 ComPtr<IInternalSessionControl> directControl;
15031 {
15032 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
15033 AssertReturn(!!mData, E_FAIL);
15034 directControl = mData->mSession.mDirectControl;
15035
15036 /* directControl may be already set to NULL here in #OnSessionEnd()
15037 * called too early by the direct session process while there is still
15038 * some operation (like deleting the snapshot) in progress. The client
15039 * process in this case is waiting inside Session::close() for the
15040 * "end session" process object to complete, while #uninit() called by
15041 * #checkForDeath() on the Watcher thread is waiting for the pending
15042 * operation to complete. For now, we accept this inconsistent behavior
15043 * and simply do nothing here. */
15044
15045 if (mData->mSession.mState == SessionState_Unlocking)
15046 return S_OK;
15047
15048 AssertReturn(!directControl.isNull(), E_FAIL);
15049 }
15050
15051 return directControl->UpdateMachineState(mData->mMachineState);
15052}
Note: See TracBrowser for help on using the repository browser.

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