VirtualBox

source: vbox/trunk/src/VBox/Main/include/MachineImpl.h@ 26323

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

Main/Machine+Console: Extend the scope of the power up progress object, and collect error information from the frontend. Much better error signalling when the VM is started via openRemoteSession. Adjusted documentation accordingly, and finally fixed the incomprehensible messages emitted by VBoxManage startvm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 39.8 KB
Line 
1/* $Id: MachineImpl.h 26323 2010-02-08 11:13:28Z vboxsync $ */
2
3/** @file
4 *
5 * VirtualBox COM class declaration
6 */
7
8/*
9 * Copyright (C) 2006-2010 Sun Microsystems, Inc.
10 *
11 * This file is part of VirtualBox Open Source Edition (OSE), as
12 * available from http://www.virtualbox.org. This file is free software;
13 * you can redistribute it and/or modify it under the terms of the GNU
14 * General Public License (GPL) as published by the Free Software
15 * Foundation, in version 2 as it comes in the "COPYING" file of the
16 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
17 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
20 * Clara, CA 95054 USA or visit http://www.sun.com if you need
21 * additional information or have any questions.
22 */
23
24#ifndef ____H_MACHINEIMPL
25#define ____H_MACHINEIMPL
26
27#include "VirtualBoxBase.h"
28#include "SnapshotImpl.h"
29#include "VRDPServerImpl.h"
30#include "MediumAttachmentImpl.h"
31#include "NetworkAdapterImpl.h"
32#include "AudioAdapterImpl.h"
33#include "SerialPortImpl.h"
34#include "ParallelPortImpl.h"
35#include "BIOSSettingsImpl.h"
36#include "StorageControllerImpl.h" // required for MachineImpl.h to compile on Windows
37#include "VBox/settings.h"
38#ifdef VBOX_WITH_RESOURCE_USAGE_API
39#include "PerformanceImpl.h"
40#endif /* VBOX_WITH_RESOURCE_USAGE_API */
41
42// generated header
43#include "SchemaDefs.h"
44
45#include <VBox/types.h>
46
47#include <iprt/file.h>
48#include <iprt/thread.h>
49#include <iprt/time.h>
50
51#include <list>
52
53// defines
54////////////////////////////////////////////////////////////////////////////////
55
56// helper declarations
57////////////////////////////////////////////////////////////////////////////////
58
59class Progress;
60class Keyboard;
61class Mouse;
62class Display;
63class MachineDebugger;
64class USBController;
65class Snapshot;
66class SharedFolder;
67class HostUSBDevice;
68class StorageController;
69
70class SessionMachine;
71
72namespace settings
73{
74 class MachineConfigFile;
75 struct Snapshot;
76 struct Hardware;
77 struct Storage;
78 struct StorageController;
79 struct MachineRegistryEntry;
80}
81
82// Machine class
83////////////////////////////////////////////////////////////////////////////////
84
85class ATL_NO_VTABLE Machine :
86 public VirtualBoxBaseWithChildrenNEXT,
87 public VirtualBoxSupportErrorInfoImpl<Machine, IMachine>,
88 public VirtualBoxSupportTranslation<Machine>,
89 VBOX_SCRIPTABLE_IMPL(IMachine)
90{
91 Q_OBJECT
92
93public:
94
95 enum InitMode { Init_New, Init_Import, Init_Registered };
96
97 enum StateDependency
98 {
99 AnyStateDep = 0, MutableStateDep, MutableOrSavedStateDep
100 };
101
102 /**
103 * Internal machine data.
104 *
105 * Only one instance of this data exists per every machine -- it is shared
106 * by the Machine, SessionMachine and all SnapshotMachine instances
107 * associated with the given machine using the util::Shareable template
108 * through the mData variable.
109 *
110 * @note |const| members are persistent during lifetime so can be
111 * accessed without locking.
112 *
113 * @note There is no need to lock anything inside init() or uninit()
114 * methods, because they are always serialized (see AutoCaller).
115 */
116 struct Data
117 {
118 /**
119 * Data structure to hold information about sessions opened for the
120 * given machine.
121 */
122 struct Session
123 {
124 /** Control of the direct session opened by openSession() */
125 ComPtr<IInternalSessionControl> mDirectControl;
126
127 typedef std::list<ComPtr<IInternalSessionControl> > RemoteControlList;
128
129 /** list of controls of all opened remote sessions */
130 RemoteControlList mRemoteControls;
131
132 /** openRemoteSession() and OnSessionEnd() progress indicator */
133 ComObjPtr<Progress> mProgress;
134
135 /**
136 * PID of the session object that must be passed to openSession() to
137 * finalize the openRemoteSession() request (i.e., PID of the
138 * process created by openRemoteSession())
139 */
140 RTPROCESS mPid;
141
142 /** Current session state */
143 SessionState_T mState;
144
145 /** Session type string (for indirect sessions) */
146 Bstr mType;
147
148 /** Session machine object */
149 ComObjPtr<SessionMachine> mMachine;
150
151 /**
152 * Successfully locked media list. The 2nd value in the pair is true
153 * if the medium is locked for writing and false if locked for
154 * reading.
155 */
156 typedef std::list<std::pair<ComPtr<IMedium>, bool > > LockedMedia;
157 LockedMedia mLockedMedia;
158 };
159
160 Data();
161 ~Data();
162
163 const Guid mUuid;
164 BOOL mRegistered;
165 InitMode mInitMode;
166
167 /** Flag indicating that the config file is read-only. */
168 BOOL mConfigFileReadonly;
169 Utf8Str m_strConfigFile;
170 Utf8Str m_strConfigFileFull;
171
172 // machine settings XML file
173 settings::MachineConfigFile *m_pMachineConfigFile;
174
175 BOOL mAccessible;
176 com::ErrorInfo mAccessError;
177
178 MachineState_T mMachineState;
179 RTTIMESPEC mLastStateChange;
180
181 /* Note: These are guarded by VirtualBoxBase::stateLockHandle() */
182 uint32_t mMachineStateDeps;
183 RTSEMEVENTMULTI mMachineStateDepsSem;
184 uint32_t mMachineStateChangePending;
185
186 BOOL mCurrentStateModified;
187
188 RTFILE mHandleCfgFile;
189
190 Session mSession;
191
192 ComObjPtr<Snapshot> mFirstSnapshot;
193 ComObjPtr<Snapshot> mCurrentSnapshot;
194 };
195
196 /**
197 * Saved state data.
198 *
199 * It's actually only the state file path string, but it needs to be
200 * separate from Data, because Machine and SessionMachine instances
201 * share it, while SnapshotMachine does not.
202 *
203 * The data variable is |mSSData|.
204 */
205 struct SSData
206 {
207 Utf8Str mStateFilePath;
208 };
209
210 /**
211 * User changeable machine data.
212 *
213 * This data is common for all machine snapshots, i.e. it is shared
214 * by all SnapshotMachine instances associated with the given machine
215 * using the util::Backupable template through the |mUserData| variable.
216 *
217 * SessionMachine instances can alter this data and discard changes.
218 *
219 * @note There is no need to lock anything inside init() or uninit()
220 * methods, because they are always serialized (see AutoCaller).
221 */
222 struct UserData
223 {
224 UserData();
225 ~UserData();
226
227 Bstr mName;
228 BOOL mNameSync;
229 Bstr mDescription;
230 Bstr mOSTypeId;
231 Bstr mSnapshotFolder;
232 Bstr mSnapshotFolderFull;
233 BOOL mTeleporterEnabled;
234 ULONG mTeleporterPort;
235 Bstr mTeleporterAddress;
236 Bstr mTeleporterPassword;
237 BOOL mRTCUseUTC;
238 };
239
240 /**
241 * Hardware data.
242 *
243 * This data is unique for a machine and for every machine snapshot.
244 * Stored using the util::Backupable template in the |mHWData| variable.
245 *
246 * SessionMachine instances can alter this data and discard changes.
247 */
248 struct HWData
249 {
250 /**
251 * Data structure to hold information about a guest property.
252 */
253 struct GuestProperty {
254 /** Property name */
255 Utf8Str strName;
256 /** Property value */
257 Utf8Str strValue;
258 /** Property timestamp */
259 ULONG64 mTimestamp;
260 /** Property flags */
261 ULONG mFlags;
262 };
263
264 HWData();
265 ~HWData();
266
267 Bstr mHWVersion;
268 Guid mHardwareUUID; /**< If Null, use mData.mUuid. */
269 ULONG mMemorySize;
270 ULONG mMemoryBalloonSize;
271 ULONG mStatisticsUpdateInterval;
272 ULONG mVRAMSize;
273 ULONG mMonitorCount;
274 BOOL mHWVirtExEnabled;
275 BOOL mHWVirtExExclusive;
276 BOOL mHWVirtExNestedPagingEnabled;
277 BOOL mHWVirtExVPIDEnabled;
278 BOOL mAccelerate2DVideoEnabled;
279 BOOL mPAEEnabled;
280 BOOL mSyntheticCpu;
281 ULONG mCPUCount;
282 BOOL mCPUHotPlugEnabled;
283 BOOL mAccelerate3DEnabled;
284
285 BOOL mCPUAttached[SchemaDefs::MaxCPUCount];
286
287 settings::CpuIdLeaf mCpuIdStdLeafs[10];
288 settings::CpuIdLeaf mCpuIdExtLeafs[10];
289
290 DeviceType_T mBootOrder[SchemaDefs::MaxBootPosition];
291
292 typedef std::list< ComObjPtr<SharedFolder> > SharedFolderList;
293 SharedFolderList mSharedFolders;
294
295 ClipboardMode_T mClipboardMode;
296
297 typedef std::list<GuestProperty> GuestPropertyList;
298 GuestPropertyList mGuestProperties;
299 BOOL mPropertyServiceActive;
300 Utf8Str mGuestPropertyNotificationPatterns;
301
302 FirmwareType_T mFirmwareType;
303 };
304
305 /**
306 * Hard disk and other media data.
307 *
308 * The usage policy is the same as for HWData, but a separate structure
309 * is necessary because hard disk data requires different procedures when
310 * taking or discarding snapshots, etc.
311 *
312 * The data variable is |mMediaData|.
313 */
314 struct MediaData
315 {
316 MediaData();
317 ~MediaData();
318
319 typedef std::list< ComObjPtr<MediumAttachment> > AttachmentList;
320 AttachmentList mAttachments;
321 };
322
323 VIRTUALBOXBASE_ADD_ERRORINFO_SUPPORT(Machine)
324
325 DECLARE_NOT_AGGREGATABLE(Machine)
326
327 DECLARE_PROTECT_FINAL_CONSTRUCT()
328
329 BEGIN_COM_MAP(Machine)
330 COM_INTERFACE_ENTRY(ISupportErrorInfo)
331 COM_INTERFACE_ENTRY(IMachine)
332 COM_INTERFACE_ENTRY(IDispatch)
333 END_COM_MAP()
334
335 DECLARE_EMPTY_CTOR_DTOR(Machine)
336
337 HRESULT FinalConstruct();
338 void FinalRelease();
339
340 // public initializer/uninitializer for internal purposes only
341 HRESULT init(VirtualBox *aParent,
342 const Utf8Str &strConfigFile,
343 InitMode aMode,
344 CBSTR aName = NULL,
345 GuestOSType *aOsType = NULL,
346 BOOL aNameSync = TRUE,
347 const Guid *aId = NULL);
348 void uninit();
349
350protected:
351 HRESULT initDataAndChildObjects();
352 void uninitDataAndChildObjects();
353
354public:
355 // IMachine properties
356 STDMETHOD(COMGETTER(Parent))(IVirtualBox **aParent);
357 STDMETHOD(COMGETTER(Accessible))(BOOL *aAccessible);
358 STDMETHOD(COMGETTER(AccessError))(IVirtualBoxErrorInfo **aAccessError);
359 STDMETHOD(COMGETTER(Name))(BSTR *aName);
360 STDMETHOD(COMSETTER(Name))(IN_BSTR aName);
361 STDMETHOD(COMGETTER(Description))(BSTR *aDescription);
362 STDMETHOD(COMSETTER(Description))(IN_BSTR aDescription);
363 STDMETHOD(COMGETTER(Id))(BSTR *aId);
364 STDMETHOD(COMGETTER(OSTypeId))(BSTR *aOSTypeId);
365 STDMETHOD(COMSETTER(OSTypeId))(IN_BSTR aOSTypeId);
366 STDMETHOD(COMGETTER(HardwareVersion))(BSTR *aVersion);
367 STDMETHOD(COMSETTER(HardwareVersion))(IN_BSTR aVersion);
368 STDMETHOD(COMGETTER(HardwareUUID))(BSTR *aUUID);
369 STDMETHOD(COMSETTER(HardwareUUID))(IN_BSTR aUUID);
370 STDMETHOD(COMGETTER(MemorySize))(ULONG *memorySize);
371 STDMETHOD(COMSETTER(MemorySize))(ULONG memorySize);
372 STDMETHOD(COMGETTER(CPUCount))(ULONG *cpuCount);
373 STDMETHOD(COMSETTER(CPUCount))(ULONG cpuCount);
374 STDMETHOD(COMGETTER(CPUHotPlugEnabled))(BOOL *enabled);
375 STDMETHOD(COMSETTER(CPUHotPlugEnabled))(BOOL enabled);
376 STDMETHOD(COMGETTER(MemoryBalloonSize))(ULONG *memoryBalloonSize);
377 STDMETHOD(COMSETTER(MemoryBalloonSize))(ULONG memoryBalloonSize);
378 STDMETHOD(COMGETTER(StatisticsUpdateInterval))(ULONG *statisticsUpdateInterval);
379 STDMETHOD(COMSETTER(StatisticsUpdateInterval))(ULONG statisticsUpdateInterval);
380 STDMETHOD(COMGETTER(VRAMSize))(ULONG *memorySize);
381 STDMETHOD(COMSETTER(VRAMSize))(ULONG memorySize);
382 STDMETHOD(COMGETTER(MonitorCount))(ULONG *monitorCount);
383 STDMETHOD(COMSETTER(MonitorCount))(ULONG monitorCount);
384 STDMETHOD(COMGETTER(Accelerate3DEnabled))(BOOL *enabled);
385 STDMETHOD(COMSETTER(Accelerate3DEnabled))(BOOL enabled);
386 STDMETHOD(COMGETTER(Accelerate2DVideoEnabled))(BOOL *enabled);
387 STDMETHOD(COMSETTER(Accelerate2DVideoEnabled))(BOOL enabled);
388 STDMETHOD(COMGETTER(BIOSSettings))(IBIOSSettings **biosSettings);
389 STDMETHOD(COMGETTER(SnapshotFolder))(BSTR *aSavedStateFolder);
390 STDMETHOD(COMSETTER(SnapshotFolder))(IN_BSTR aSavedStateFolder);
391 STDMETHOD(COMGETTER(MediumAttachments))(ComSafeArrayOut(IMediumAttachment *, aAttachments));
392 STDMETHOD(COMGETTER(VRDPServer))(IVRDPServer **vrdpServer);
393 STDMETHOD(COMGETTER(AudioAdapter))(IAudioAdapter **audioAdapter);
394 STDMETHOD(COMGETTER(USBController))(IUSBController * *aUSBController);
395 STDMETHOD(COMGETTER(SettingsFilePath))(BSTR *aFilePath);
396 STDMETHOD(COMGETTER(SettingsModified))(BOOL *aModified);
397 STDMETHOD(COMGETTER(SessionState))(SessionState_T *aSessionState);
398 STDMETHOD(COMGETTER(SessionType))(BSTR *aSessionType);
399 STDMETHOD(COMGETTER(SessionPid))(ULONG *aSessionPid);
400 STDMETHOD(COMGETTER(State))(MachineState_T *machineState);
401 STDMETHOD(COMGETTER(LastStateChange))(LONG64 *aLastStateChange);
402 STDMETHOD(COMGETTER(StateFilePath))(BSTR *aStateFilePath);
403 STDMETHOD(COMGETTER(LogFolder))(BSTR *aLogFolder);
404 STDMETHOD(COMGETTER(CurrentSnapshot))(ISnapshot **aCurrentSnapshot);
405 STDMETHOD(COMGETTER(SnapshotCount))(ULONG *aSnapshotCount);
406 STDMETHOD(COMGETTER(CurrentStateModified))(BOOL *aCurrentStateModified);
407 STDMETHOD(COMGETTER(SharedFolders))(ComSafeArrayOut(ISharedFolder *, aSharedFolders));
408 STDMETHOD(COMGETTER(ClipboardMode))(ClipboardMode_T *aClipboardMode);
409 STDMETHOD(COMSETTER(ClipboardMode))(ClipboardMode_T aClipboardMode);
410 STDMETHOD(COMGETTER(GuestPropertyNotificationPatterns))(BSTR *aPattern);
411 STDMETHOD(COMSETTER(GuestPropertyNotificationPatterns))(IN_BSTR aPattern);
412 STDMETHOD(COMGETTER(StorageControllers))(ComSafeArrayOut(IStorageController *, aStorageControllers));
413 STDMETHOD(COMGETTER(TeleporterEnabled))(BOOL *aEnabled);
414 STDMETHOD(COMSETTER(TeleporterEnabled))(BOOL aEnabled);
415 STDMETHOD(COMGETTER(TeleporterPort))(ULONG *aPort);
416 STDMETHOD(COMSETTER(TeleporterPort))(ULONG aPort);
417 STDMETHOD(COMGETTER(TeleporterAddress))(BSTR *aAddress);
418 STDMETHOD(COMSETTER(TeleporterAddress))(IN_BSTR aAddress);
419 STDMETHOD(COMGETTER(TeleporterPassword))(BSTR *aPassword);
420 STDMETHOD(COMSETTER(TeleporterPassword))(IN_BSTR aPassword);
421 STDMETHOD(COMGETTER(RTCUseUTC))(BOOL *aEnabled);
422 STDMETHOD(COMSETTER(RTCUseUTC))(BOOL aEnabled);
423
424 // IMachine methods
425 STDMETHOD(SetBootOrder)(ULONG aPosition, DeviceType_T aDevice);
426 STDMETHOD(GetBootOrder)(ULONG aPosition, DeviceType_T *aDevice);
427 STDMETHOD(AttachDevice)(IN_BSTR aControllerName, LONG aControllerPort,
428 LONG aDevice, DeviceType_T aType, IN_BSTR aId);
429 STDMETHOD(DetachDevice)(IN_BSTR aControllerName, LONG aControllerPort, LONG aDevice);
430 STDMETHOD(PassthroughDevice)(IN_BSTR aControllerName, LONG aControllerPort, LONG aDevice, BOOL aPassthrough);
431 STDMETHOD(MountMedium)(IN_BSTR aControllerName, LONG aControllerPort,
432 LONG aDevice, IN_BSTR aId, BOOL aForce);
433 STDMETHOD(GetMedium)(IN_BSTR aControllerName, LONG aControllerPort, LONG aDevice,
434 IMedium **aMedium);
435 STDMETHOD(GetSerialPort)(ULONG slot, ISerialPort **port);
436 STDMETHOD(GetParallelPort)(ULONG slot, IParallelPort **port);
437 STDMETHOD(GetNetworkAdapter)(ULONG slot, INetworkAdapter **adapter);
438 STDMETHOD(GetExtraDataKeys)(ComSafeArrayOut(BSTR, aKeys));
439 STDMETHOD(GetExtraData)(IN_BSTR aKey, BSTR *aValue);
440 STDMETHOD(SetExtraData)(IN_BSTR aKey, IN_BSTR aValue);
441 STDMETHOD(GetCpuProperty)(CpuPropertyType_T property, BOOL *aVal);
442 STDMETHOD(SetCpuProperty)(CpuPropertyType_T property, BOOL aVal);
443 STDMETHOD(GetCpuIdLeaf)(ULONG id, ULONG *aValEax, ULONG *aValEbx, ULONG *aValEcx, ULONG *aValEdx);
444 STDMETHOD(SetCpuIdLeaf)(ULONG id, ULONG aValEax, ULONG aValEbx, ULONG aValEcx, ULONG aValEdx);
445 STDMETHOD(RemoveCpuIdLeaf)(ULONG id);
446 STDMETHOD(RemoveAllCpuIdLeafs)();
447 STDMETHOD(GetHWVirtExProperty)(HWVirtExPropertyType_T property, BOOL *aVal);
448 STDMETHOD(SetHWVirtExProperty)(HWVirtExPropertyType_T property, BOOL aVal);
449 STDMETHOD(SaveSettings)();
450 STDMETHOD(DiscardSettings)();
451 STDMETHOD(DeleteSettings)();
452 STDMETHOD(Export)(IAppliance *aAppliance, IVirtualSystemDescription **aDescription);
453 STDMETHOD(GetSnapshot)(IN_BSTR aId, ISnapshot **aSnapshot);
454 STDMETHOD(FindSnapshot)(IN_BSTR aName, ISnapshot **aSnapshot);
455 STDMETHOD(SetCurrentSnapshot)(IN_BSTR aId);
456 STDMETHOD(CreateSharedFolder)(IN_BSTR aName, IN_BSTR aHostPath, BOOL aWritable);
457 STDMETHOD(RemoveSharedFolder)(IN_BSTR aName);
458 STDMETHOD(CanShowConsoleWindow)(BOOL *aCanShow);
459 STDMETHOD(ShowConsoleWindow)(ULONG64 *aWinId);
460 STDMETHOD(GetGuestProperty)(IN_BSTR aName, BSTR *aValue, ULONG64 *aTimestamp, BSTR *aFlags);
461 STDMETHOD(GetGuestPropertyValue)(IN_BSTR aName, BSTR *aValue);
462 STDMETHOD(GetGuestPropertyTimestamp)(IN_BSTR aName, ULONG64 *aTimestamp);
463 STDMETHOD(SetGuestProperty)(IN_BSTR aName, IN_BSTR aValue, IN_BSTR aFlags);
464 STDMETHOD(SetGuestPropertyValue)(IN_BSTR aName, IN_BSTR aValue);
465 STDMETHOD(EnumerateGuestProperties)(IN_BSTR aPattern, ComSafeArrayOut(BSTR, aNames), ComSafeArrayOut(BSTR, aValues), ComSafeArrayOut(ULONG64, aTimestamps), ComSafeArrayOut(BSTR, aFlags));
466 STDMETHOD(GetMediumAttachmentsOfController)(IN_BSTR aName, ComSafeArrayOut(IMediumAttachment *, aAttachments));
467 STDMETHOD(GetMediumAttachment)(IN_BSTR aConstrollerName, LONG aControllerPort, LONG aDevice, IMediumAttachment **aAttachment);
468 STDMETHOD(AddStorageController)(IN_BSTR aName, StorageBus_T aConnectionType, IStorageController **controller);
469 STDMETHOD(RemoveStorageController(IN_BSTR aName));
470 STDMETHOD(GetStorageControllerByName(IN_BSTR aName, IStorageController **storageController));
471 STDMETHOD(GetStorageControllerByInstance(ULONG aInstance, IStorageController **storageController));
472 STDMETHOD(COMGETTER(FirmwareType)) (FirmwareType_T *aFirmware);
473 STDMETHOD(COMSETTER(FirmwareType)) (FirmwareType_T aFirmware);
474
475 STDMETHOD(QuerySavedThumbnailSize)(ULONG *aSize, ULONG *aWidth, ULONG *aHeight);
476 STDMETHOD(ReadSavedThumbnailToArray)(BOOL aBGR, ULONG *aWidth, ULONG *aHeight, ComSafeArrayOut(BYTE, aData));
477 STDMETHOD(QuerySavedScreenshotPNGSize)(ULONG *aSize, ULONG *aWidth, ULONG *aHeight);
478 STDMETHOD(ReadSavedScreenshotPNGToArray)(ULONG *aWidth, ULONG *aHeight, ComSafeArrayOut(BYTE, aData));
479 STDMETHOD(HotPlugCPU(ULONG aCpu));
480 STDMETHOD(HotUnplugCPU(ULONG aCpu));
481 STDMETHOD(GetCPUStatus(ULONG aCpu, BOOL *aCpuAttached));
482
483 // public methods only for internal purposes
484
485 /**
486 * Simple run-time type identification without having to enable C++ RTTI.
487 * The class IDs are defined in VirtualBoxBase.h.
488 * @return
489 */
490 virtual VBoxClsID getClassID() const
491 {
492 return clsidMachine;
493 }
494
495 /**
496 * Override of the default locking class to be used for validating lock
497 * order with the standard member lock handle.
498 */
499 virtual VBoxLockingClass getLockingClass() const
500 {
501 return LOCKCLASS_MACHINEOBJECT;
502 }
503
504 /// @todo (dmik) add lock and make non-inlined after revising classes
505 // that use it. Note: they should enter Machine lock to keep the returned
506 // information valid!
507 bool isRegistered() { return !!mData->mRegistered; }
508
509 // unsafe inline public methods for internal purposes only (ensure there is
510 // a caller and a read lock before calling them!)
511
512 /**
513 * Returns the VirtualBox object this machine belongs to.
514 *
515 * @note This method doesn't check this object's readiness. Intended to be
516 * used by ready Machine children (whose readiness is bound to the parent's
517 * one) or after doing addCaller() manually.
518 */
519 const ComObjPtr<VirtualBox, ComWeakRef>& getVirtualBox() const { return mParent; }
520
521 /**
522 * Returns this machine ID.
523 *
524 * @note This method doesn't check this object's readiness. Intended to be
525 * used by ready Machine children (whose readiness is bound to the parent's
526 * one) or after adding a caller manually.
527 */
528 const Guid& getId() const { return mData->mUuid; }
529
530 /**
531 * Returns the snapshot ID this machine represents or an empty UUID if this
532 * instance is not SnapshotMachine.
533 *
534 * @note This method doesn't check this object's readiness. Intended to be
535 * used by ready Machine children (whose readiness is bound to the parent's
536 * one) or after adding a caller manually.
537 */
538 inline const Guid& getSnapshotId() const;
539
540 /**
541 * Returns this machine's full settings file path.
542 *
543 * @note This method doesn't lock this object or check its readiness.
544 * Intended to be used only after doing addCaller() manually and locking it
545 * for reading.
546 */
547 const Utf8Str& getSettingsFileFull() const { return mData->m_strConfigFileFull; }
548
549 /**
550 * Returns this machine name.
551 *
552 * @note This method doesn't lock this object or check its readiness.
553 * Intended to be used only after doing addCaller() manually and locking it
554 * for reading.
555 */
556 const Bstr& getName() const { return mUserData->mName; }
557
558 enum
559 {
560 IsModified_MachineData = 0x0001,
561 IsModified_Storage = 0x0002,
562 IsModified_NetworkAdapters = 0x0008,
563 IsModified_SerialPorts = 0x0010,
564 IsModified_ParallelPorts = 0x0020,
565 IsModified_VRDPServer = 0x0040,
566 IsModified_AudioAdapter = 0x0080,
567 IsModified_USB = 0x0100,
568 IsModified_BIOS = 0x0200,
569 IsModified_SharedFolders = 0x0400
570 };
571
572 void setModified(uint32_t fl);
573
574 // callback handlers
575 virtual HRESULT onNetworkAdapterChange(INetworkAdapter * /* networkAdapter */, BOOL /* changeAdapter */) { return S_OK; }
576 virtual HRESULT onSerialPortChange(ISerialPort * /* serialPort */) { return S_OK; }
577 virtual HRESULT onParallelPortChange(IParallelPort * /* parallelPort */) { return S_OK; }
578 virtual HRESULT onVRDPServerChange() { return S_OK; }
579 virtual HRESULT onUSBControllerChange() { return S_OK; }
580 virtual HRESULT onStorageControllerChange() { return S_OK; }
581 virtual HRESULT onCPUChange(ULONG /* aCPU */, BOOL /* aRemove */) { return S_OK; }
582 virtual HRESULT onMediumChange(IMediumAttachment * /* mediumAttachment */, BOOL /* force */) { return S_OK; }
583 virtual HRESULT onSharedFolderChange() { return S_OK; }
584
585 HRESULT saveRegistryEntry(settings::MachineRegistryEntry &data);
586
587 int calculateFullPath(const Utf8Str &strPath, Utf8Str &aResult);
588 void calculateRelativePath(const Utf8Str &strPath, Utf8Str &aResult);
589
590 void getLogFolder(Utf8Str &aLogFolder);
591
592 HRESULT openSession(IInternalSessionControl *aControl);
593 HRESULT openRemoteSession(IInternalSessionControl *aControl,
594 IN_BSTR aType, IN_BSTR aEnvironment,
595 Progress *aProgress);
596 HRESULT openExistingSession(IInternalSessionControl *aControl);
597
598#if defined(RT_OS_WINDOWS)
599
600 bool isSessionOpen(ComObjPtr<SessionMachine> &aMachine,
601 ComPtr<IInternalSessionControl> *aControl = NULL,
602 HANDLE *aIPCSem = NULL, bool aAllowClosing = false);
603 bool isSessionSpawning(RTPROCESS *aPID = NULL);
604
605 bool isSessionOpenOrClosing(ComObjPtr<SessionMachine> &aMachine,
606 ComPtr<IInternalSessionControl> *aControl = NULL,
607 HANDLE *aIPCSem = NULL)
608 { return isSessionOpen(aMachine, aControl, aIPCSem, true /* aAllowClosing */); }
609
610#elif defined(RT_OS_OS2)
611
612 bool isSessionOpen(ComObjPtr<SessionMachine> &aMachine,
613 ComPtr<IInternalSessionControl> *aControl = NULL,
614 HMTX *aIPCSem = NULL, bool aAllowClosing = false);
615
616 bool isSessionSpawning(RTPROCESS *aPID = NULL);
617
618 bool isSessionOpenOrClosing(ComObjPtr<SessionMachine> &aMachine,
619 ComPtr<IInternalSessionControl> *aControl = NULL,
620 HMTX *aIPCSem = NULL)
621 { return isSessionOpen(aMachine, aControl, aIPCSem, true /* aAllowClosing */); }
622
623#else
624
625 bool isSessionOpen(ComObjPtr<SessionMachine> &aMachine,
626 ComPtr<IInternalSessionControl> *aControl = NULL,
627 bool aAllowClosing = false);
628 bool isSessionSpawning();
629
630 bool isSessionOpenOrClosing(ComObjPtr<SessionMachine> &aMachine,
631 ComPtr<IInternalSessionControl> *aControl = NULL)
632 { return isSessionOpen(aMachine, aControl, true /* aAllowClosing */); }
633
634#endif
635
636 bool checkForSpawnFailure();
637
638 HRESULT trySetRegistered(BOOL aRegistered);
639
640 HRESULT getSharedFolder(CBSTR aName,
641 ComObjPtr<SharedFolder> &aSharedFolder,
642 bool aSetError = false)
643 {
644 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
645 return findSharedFolder(aName, aSharedFolder, aSetError);
646 }
647
648 HRESULT addStateDependency(StateDependency aDepType = AnyStateDep,
649 MachineState_T *aState = NULL,
650 BOOL *aRegistered = NULL);
651 void releaseStateDependency();
652
653 // for VirtualBoxSupportErrorInfoImpl
654 static const wchar_t *getComponentName() { return L"Machine"; }
655
656protected:
657
658 HRESULT registeredInit();
659
660 HRESULT checkStateDependency(StateDependency aDepType);
661
662 inline Machine *getMachine();
663
664 void ensureNoStateDependencies();
665
666 virtual HRESULT setMachineState(MachineState_T aMachineState);
667
668 HRESULT findSharedFolder(CBSTR aName,
669 ComObjPtr<SharedFolder> &aSharedFolder,
670 bool aSetError = false);
671
672 HRESULT loadSettings(bool aRegistered);
673 HRESULT loadSnapshot(const settings::Snapshot &data,
674 const Guid &aCurSnapshotId,
675 Snapshot *aParentSnapshot);
676 HRESULT loadHardware(const settings::Hardware &data);
677 HRESULT loadStorageControllers(const settings::Storage &data,
678 bool aRegistered,
679 const Guid *aSnapshotId = NULL);
680 HRESULT loadStorageDevices(StorageController *aStorageController,
681 const settings::StorageController &data,
682 bool aRegistered,
683 const Guid *aSnapshotId = NULL);
684
685 HRESULT findSnapshot(const Guid &aId, ComObjPtr<Snapshot> &aSnapshot,
686 bool aSetError = false);
687 HRESULT findSnapshot(IN_BSTR aName, ComObjPtr<Snapshot> &aSnapshot,
688 bool aSetError = false);
689
690 HRESULT getStorageControllerByName(const Utf8Str &aName,
691 ComObjPtr<StorageController> &aStorageController,
692 bool aSetError = false);
693
694 HRESULT getMediumAttachmentsOfController(CBSTR aName,
695 MediaData::AttachmentList &aAttachments);
696
697 enum
698 {
699 /* flags for #saveSettings() */
700 SaveS_ResetCurStateModified = 0x01,
701 SaveS_InformCallbacksAnyway = 0x02,
702 /* flags for #saveSnapshotSettings() */
703 SaveSS_CurStateModified = 0x40,
704 SaveSS_CurrentId = 0x80,
705 /* flags for #saveStateSettings() */
706 SaveSTS_CurStateModified = 0x20,
707 SaveSTS_StateFilePath = 0x40,
708 SaveSTS_StateTimeStamp = 0x80,
709 };
710
711 HRESULT prepareSaveSettings(bool &aRenamed, bool &aNew);
712 HRESULT saveSettings(int aFlags = 0);
713
714 HRESULT saveAllSnapshots();
715
716 HRESULT saveHardware(settings::Hardware &data);
717 HRESULT saveStorageControllers(settings::Storage &data);
718 HRESULT saveStorageDevices(ComObjPtr<StorageController> aStorageController,
719 settings::StorageController &data);
720
721 HRESULT saveStateSettings(int aFlags);
722
723 HRESULT createImplicitDiffs(const Bstr &aFolder,
724 IProgress *aProgress,
725 ULONG aWeight,
726 bool aOnline,
727 bool *pfNeedsSaveSettings);
728 HRESULT deleteImplicitDiffs(bool *pfNeedsSaveSettings);
729
730 MediumAttachment* findAttachment(const MediaData::AttachmentList &ll,
731 IN_BSTR aControllerName,
732 LONG aControllerPort,
733 LONG aDevice);
734 MediumAttachment* findAttachment(const MediaData::AttachmentList &ll,
735 ComObjPtr<Medium> pMedium);
736 MediumAttachment* findAttachment(const MediaData::AttachmentList &ll,
737 Guid &id);
738
739 void commitMedia(bool aOnline = false);
740 void rollbackMedia();
741
742 bool isInOwnDir(Utf8Str *aSettingsDir = NULL);
743
744 void rollback(bool aNotify);
745 void commit();
746 void copyFrom(Machine *aThat);
747
748#ifdef VBOX_WITH_RESOURCE_USAGE_API
749 void registerMetrics(PerformanceCollector *aCollector, Machine *aMachine, RTPROCESS pid);
750 void unregisterMetrics(PerformanceCollector *aCollector, Machine *aMachine);
751#endif /* VBOX_WITH_RESOURCE_USAGE_API */
752
753 const ComObjPtr<Machine, ComWeakRef> mPeer;
754
755 const ComObjPtr<VirtualBox, ComWeakRef> mParent;
756
757 uint32_t m_flModifications;
758
759 Shareable<Data> mData;
760 Shareable<SSData> mSSData;
761
762 Backupable<UserData> mUserData;
763 Backupable<HWData> mHWData;
764 Backupable<MediaData> mMediaData;
765
766 // the following fields need special backup/rollback/commit handling,
767 // so they cannot be a part of HWData
768
769 const ComObjPtr<VRDPServer> mVRDPServer;
770 const ComObjPtr<SerialPort> mSerialPorts[SchemaDefs::SerialPortCount];
771 const ComObjPtr<ParallelPort> mParallelPorts[SchemaDefs::ParallelPortCount];
772 const ComObjPtr<AudioAdapter> mAudioAdapter;
773 const ComObjPtr<USBController> mUSBController;
774 const ComObjPtr<BIOSSettings> mBIOSSettings;
775 const ComObjPtr<NetworkAdapter> mNetworkAdapters[SchemaDefs::NetworkAdapterCount];
776
777 typedef std::list< ComObjPtr<StorageController> > StorageControllerList;
778 Backupable<StorageControllerList> mStorageControllers;
779
780 friend class SessionMachine;
781 friend class SnapshotMachine;
782};
783
784// SessionMachine class
785////////////////////////////////////////////////////////////////////////////////
786
787/**
788 * @note Notes on locking objects of this class:
789 * SessionMachine shares some data with the primary Machine instance (pointed
790 * to by the |mPeer| member). In order to provide data consistency it also
791 * shares its lock handle. This means that whenever you lock a SessionMachine
792 * instance using Auto[Reader]Lock or AutoMultiLock, the corresponding Machine
793 * instance is also locked in the same lock mode. Keep it in mind.
794 */
795class ATL_NO_VTABLE SessionMachine :
796 public VirtualBoxSupportTranslation<SessionMachine>,
797 public Machine,
798 VBOX_SCRIPTABLE_IMPL(IInternalMachineControl)
799{
800public:
801
802 VIRTUALBOXSUPPORTTRANSLATION_OVERRIDE(SessionMachine)
803
804 DECLARE_NOT_AGGREGATABLE(SessionMachine)
805
806 DECLARE_PROTECT_FINAL_CONSTRUCT()
807
808 BEGIN_COM_MAP(SessionMachine)
809 COM_INTERFACE_ENTRY2(IDispatch, IMachine)
810 COM_INTERFACE_ENTRY(ISupportErrorInfo)
811 COM_INTERFACE_ENTRY(IMachine)
812 COM_INTERFACE_ENTRY(IInternalMachineControl)
813 END_COM_MAP()
814
815 DECLARE_EMPTY_CTOR_DTOR(SessionMachine)
816
817 HRESULT FinalConstruct();
818 void FinalRelease();
819
820 // public initializer/uninitializer for internal purposes only
821 HRESULT init(Machine *aMachine);
822 void uninit() { uninit(Uninit::Unexpected); }
823
824 // util::Lockable interface
825 RWLockHandle *lockHandle() const;
826
827 // IInternalMachineControl methods
828 STDMETHOD(SetRemoveSavedState)(BOOL aRemove);
829 STDMETHOD(UpdateState)(MachineState_T machineState);
830 STDMETHOD(GetIPCId)(BSTR *id);
831 STDMETHOD(SetPowerUpInfo)(IVirtualBoxErrorInfo *aError);
832 STDMETHOD(RunUSBDeviceFilters)(IUSBDevice *aUSBDevice, BOOL *aMatched, ULONG *aMaskedIfs);
833 STDMETHOD(CaptureUSBDevice)(IN_BSTR aId);
834 STDMETHOD(DetachUSBDevice)(IN_BSTR aId, BOOL aDone);
835 STDMETHOD(AutoCaptureUSBDevices)();
836 STDMETHOD(DetachAllUSBDevices)(BOOL aDone);
837 STDMETHOD(OnSessionEnd)(ISession *aSession, IProgress **aProgress);
838 STDMETHOD(BeginSavingState)(IProgress *aProgress, BSTR *aStateFilePath);
839 STDMETHOD(EndSavingState)(BOOL aSuccess);
840 STDMETHOD(AdoptSavedState)(IN_BSTR aSavedStateFile);
841 STDMETHOD(BeginTakingSnapshot)(IConsole *aInitiator,
842 IN_BSTR aName,
843 IN_BSTR aDescription,
844 IProgress *aConsoleProgress,
845 BOOL fTakingSnapshotOnline,
846 BSTR *aStateFilePath);
847 STDMETHOD(EndTakingSnapshot)(BOOL aSuccess);
848 STDMETHOD(DeleteSnapshot)(IConsole *aInitiator, IN_BSTR aId,
849 MachineState_T *aMachineState, IProgress **aProgress);
850 STDMETHOD(RestoreSnapshot)(IConsole *aInitiator,
851 ISnapshot *aSnapshot,
852 MachineState_T *aMachineState,
853 IProgress **aProgress);
854 STDMETHOD(PullGuestProperties)(ComSafeArrayOut(BSTR, aNames), ComSafeArrayOut(BSTR, aValues),
855 ComSafeArrayOut(ULONG64, aTimestamps), ComSafeArrayOut(BSTR, aFlags));
856 STDMETHOD(PushGuestProperties)(ComSafeArrayIn(IN_BSTR, aNames), ComSafeArrayIn(IN_BSTR, aValues),
857 ComSafeArrayIn(ULONG64, aTimestamps), ComSafeArrayIn(IN_BSTR, aFlags));
858 STDMETHOD(PushGuestProperty)(IN_BSTR aName, IN_BSTR aValue,
859 ULONG64 aTimestamp, IN_BSTR aFlags);
860 STDMETHOD(LockMedia)() { return lockMedia(); }
861 STDMETHOD(UnlockMedia)() { unlockMedia(); return S_OK; }
862
863 // public methods only for internal purposes
864
865 /**
866 * Simple run-time type identification without having to enable C++ RTTI.
867 * The class IDs are defined in VirtualBoxBase.h.
868 * @return
869 */
870 virtual VBoxClsID getClassID() const
871 {
872 return clsidSessionMachine;
873 }
874
875 bool checkForDeath();
876
877 HRESULT onNetworkAdapterChange(INetworkAdapter *networkAdapter, BOOL changeAdapter);
878 HRESULT onStorageControllerChange();
879 HRESULT onMediumChange(IMediumAttachment *aMediumAttachment, BOOL aForce);
880 HRESULT onSerialPortChange(ISerialPort *serialPort);
881 HRESULT onParallelPortChange(IParallelPort *parallelPort);
882 HRESULT onCPUChange(ULONG aCPU, BOOL aRemove);
883 HRESULT onVRDPServerChange();
884 HRESULT onUSBControllerChange();
885 HRESULT onUSBDeviceAttach(IUSBDevice *aDevice,
886 IVirtualBoxErrorInfo *aError,
887 ULONG aMaskedIfs);
888 HRESULT onUSBDeviceDetach(IN_BSTR aId,
889 IVirtualBoxErrorInfo *aError);
890 HRESULT onSharedFolderChange();
891
892 bool hasMatchingUSBFilter(const ComObjPtr<HostUSBDevice> &aDevice, ULONG *aMaskedIfs);
893
894private:
895
896 struct SnapshotData
897 {
898 SnapshotData() : mLastState(MachineState_Null) {}
899
900 MachineState_T mLastState;
901
902 // used when taking snapshot
903 ComObjPtr<Snapshot> mSnapshot;
904
905 // used when saving state
906 Guid mProgressId;
907 Utf8Str mStateFilePath;
908 };
909
910 struct Uninit
911 {
912 enum Reason { Unexpected, Abnormal, Normal };
913 };
914
915 struct SnapshotTask;
916 struct DeleteSnapshotTask;
917 struct RestoreSnapshotTask;
918
919 friend struct DeleteSnapshotTask;
920 friend struct RestoreSnapshotTask;
921
922 void uninit(Uninit::Reason aReason);
923
924 HRESULT endSavingState(BOOL aSuccess);
925
926 typedef std::map<ComObjPtr<Machine>, MachineState_T> AffectedMachines;
927
928 void deleteSnapshotHandler(DeleteSnapshotTask &aTask);
929 void restoreSnapshotHandler(RestoreSnapshotTask &aTask);
930
931 HRESULT lockMedia();
932 void unlockMedia();
933
934 HRESULT setMachineState(MachineState_T aMachineState);
935 HRESULT updateMachineStateOnClient();
936
937 HRESULT mRemoveSavedState;
938
939 SnapshotData mSnapshotData;
940
941 /** interprocess semaphore handle for this machine */
942#if defined(RT_OS_WINDOWS)
943 HANDLE mIPCSem;
944 Bstr mIPCSemName;
945 friend bool Machine::isSessionOpen(ComObjPtr<SessionMachine> &aMachine,
946 ComPtr<IInternalSessionControl> *aControl,
947 HANDLE *aIPCSem, bool aAllowClosing);
948#elif defined(RT_OS_OS2)
949 HMTX mIPCSem;
950 Bstr mIPCSemName;
951 friend bool Machine::isSessionOpen(ComObjPtr<SessionMachine> &aMachine,
952 ComPtr<IInternalSessionControl> *aControl,
953 HMTX *aIPCSem, bool aAllowClosing);
954#elif defined(VBOX_WITH_SYS_V_IPC_SESSION_WATCHER)
955 int mIPCSem;
956# ifdef VBOX_WITH_NEW_SYS_V_KEYGEN
957 Bstr mIPCKey;
958# endif /*VBOX_WITH_NEW_SYS_V_KEYGEN */
959#else
960# error "Port me!"
961#endif
962
963 static DECLCALLBACK(int) taskHandler(RTTHREAD thread, void *pvUser);
964};
965
966// SnapshotMachine class
967////////////////////////////////////////////////////////////////////////////////
968
969/**
970 * @note Notes on locking objects of this class:
971 * SnapshotMachine shares some data with the primary Machine instance (pointed
972 * to by the |mPeer| member). In order to provide data consistency it also
973 * shares its lock handle. This means that whenever you lock a SessionMachine
974 * instance using Auto[Reader]Lock or AutoMultiLock, the corresponding Machine
975 * instance is also locked in the same lock mode. Keep it in mind.
976 */
977class ATL_NO_VTABLE SnapshotMachine :
978 public VirtualBoxSupportTranslation<SnapshotMachine>,
979 public Machine
980{
981public:
982
983 VIRTUALBOXSUPPORTTRANSLATION_OVERRIDE(SnapshotMachine)
984
985 DECLARE_NOT_AGGREGATABLE(SnapshotMachine)
986
987 DECLARE_PROTECT_FINAL_CONSTRUCT()
988
989 BEGIN_COM_MAP(SnapshotMachine)
990 COM_INTERFACE_ENTRY2(IDispatch, IMachine)
991 COM_INTERFACE_ENTRY(ISupportErrorInfo)
992 COM_INTERFACE_ENTRY(IMachine)
993 END_COM_MAP()
994
995 DECLARE_EMPTY_CTOR_DTOR(SnapshotMachine)
996
997 HRESULT FinalConstruct();
998 void FinalRelease();
999
1000 // public initializer/uninitializer for internal purposes only
1001 HRESULT init(SessionMachine *aSessionMachine,
1002 IN_GUID aSnapshotId,
1003 const Utf8Str &aStateFilePath);
1004 HRESULT init(Machine *aMachine,
1005 const settings::Hardware &hardware,
1006 const settings::Storage &storage,
1007 IN_GUID aSnapshotId,
1008 const Utf8Str &aStateFilePath);
1009 void uninit();
1010
1011 // util::Lockable interface
1012 RWLockHandle *lockHandle() const;
1013
1014 // public methods only for internal purposes
1015
1016 /**
1017 * Simple run-time type identification without having to enable C++ RTTI.
1018 * The class IDs are defined in VirtualBoxBase.h.
1019 * @return
1020 */
1021 virtual VBoxClsID getClassID() const
1022 {
1023 return clsidSnapshotMachine;
1024 }
1025
1026 HRESULT onSnapshotChange(Snapshot *aSnapshot);
1027
1028 // unsafe inline public methods for internal purposes only (ensure there is
1029 // a caller and a read lock before calling them!)
1030
1031 const Guid& getSnapshotId() const { return mSnapshotId; }
1032
1033private:
1034
1035 Guid mSnapshotId;
1036
1037 friend class Snapshot;
1038};
1039
1040// third party methods that depend on SnapshotMachine definiton
1041
1042inline const Guid &Machine::getSnapshotId() const
1043{
1044 return getClassID() != clsidSnapshotMachine
1045 ? Guid::Empty
1046 : static_cast<const SnapshotMachine*>(this)->getSnapshotId();
1047}
1048
1049////////////////////////////////////////////////////////////////////////////////
1050
1051/**
1052 * Returns a pointer to the Machine object for this machine that acts like a
1053 * parent for complex machine data objects such as shared folders, etc.
1054 *
1055 * For primary Machine objects and for SnapshotMachine objects, returns this
1056 * object's pointer itself. For SessoinMachine objects, returns the peer
1057 * (primary) machine pointer.
1058 */
1059inline Machine *Machine::getMachine()
1060{
1061 if (getClassID() == clsidSessionMachine)
1062 return mPeer;
1063 return this;
1064}
1065
1066
1067#endif // ____H_MACHINEIMPL
1068/* vi: set tabstop=4 shiftwidth=4 expandtab: */
Note: See TracBrowser for help on using the repository browser.

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