VirtualBox

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

Last change on this file since 18719 was 18520, checked in by vboxsync, 16 years ago

Main: #3548 VBoxSVC holds vm xml definition file open after VM is unregistered
Make Machine::unlockConfig public and call it from VirtualBox::UnregisterMachine.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 41.4 KB
Line 
1/* $Id: MachineImpl.h 18520 2009-03-30 08:45:50Z vboxsync $ */
2
3/** @file
4 *
5 * VirtualBox COM class declaration
6 */
7
8/*
9 * Copyright (C) 2006-2009 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 "ProgressImpl.h"
29#include "SnapshotImpl.h"
30#include "VRDPServerImpl.h"
31#include "DVDDriveImpl.h"
32#include "FloppyDriveImpl.h"
33#include "HardDiskAttachmentImpl.h"
34#include "NetworkAdapterImpl.h"
35#include "AudioAdapterImpl.h"
36#include "SerialPortImpl.h"
37#include "ParallelPortImpl.h"
38#include "BIOSSettingsImpl.h"
39#include "StorageControllerImpl.h"
40#ifdef VBOX_WITH_RESOURCE_USAGE_API
41#include "PerformanceImpl.h"
42#endif /* VBOX_WITH_RESOURCE_USAGE_API */
43
44// generated header
45#include "SchemaDefs.h"
46
47#include <VBox/types.h>
48
49#include <iprt/file.h>
50#include <iprt/thread.h>
51#include <iprt/time.h>
52
53#include <list>
54
55// defines
56////////////////////////////////////////////////////////////////////////////////
57
58// helper declarations
59////////////////////////////////////////////////////////////////////////////////
60
61class VirtualBox;
62class Progress;
63class CombinedProgress;
64class Keyboard;
65class Mouse;
66class Display;
67class MachineDebugger;
68class USBController;
69class Snapshot;
70class SharedFolder;
71class HostUSBDevice;
72
73class SessionMachine;
74
75// Machine class
76////////////////////////////////////////////////////////////////////////////////
77
78class ATL_NO_VTABLE Machine :
79 public VirtualBoxBaseWithChildrenNEXT,
80 public VirtualBoxSupportErrorInfoImpl <Machine, IMachine>,
81 public VirtualBoxSupportTranslation <Machine>,
82 public IMachine
83{
84 Q_OBJECT
85
86public:
87
88 enum InstanceType { IsMachine, IsSessionMachine, IsSnapshotMachine };
89
90 /**
91 * Internal machine data.
92 *
93 * Only one instance of this data exists per every machine -- it is shared
94 * by the Machine, SessionMachine and all SnapshotMachine instances
95 * associated with the given machine using the util::Shareable template
96 * through the mData variable.
97 *
98 * @note |const| members are persistent during lifetime so can be
99 * accessed without locking.
100 *
101 * @note There is no need to lock anything inside init() or uninit()
102 * methods, because they are always serialized (see AutoCaller).
103 */
104 struct Data
105 {
106 /**
107 * Data structure to hold information about sessions opened for the
108 * given machine.
109 */
110 struct Session
111 {
112 /** Control of the direct session opened by openSession() */
113 ComPtr <IInternalSessionControl> mDirectControl;
114
115 typedef std::list <ComPtr <IInternalSessionControl> > RemoteControlList;
116
117 /** list of controls of all opened remote sessions */
118 RemoteControlList mRemoteControls;
119
120 /** openRemoteSession() and OnSessionEnd() progress indicator */
121 ComObjPtr <Progress> mProgress;
122
123 /**
124 * PID of the session object that must be passed to openSession() to
125 * finalize the openRemoteSession() request (i.e., PID of the
126 * process created by openRemoteSession())
127 */
128 RTPROCESS mPid;
129
130 /** Current session state */
131 SessionState_T mState;
132
133 /** Session type string (for indirect sessions) */
134 Bstr mType;
135
136 /** Session machine object */
137 ComObjPtr <SessionMachine> mMachine;
138
139 /**
140 * Successfully locked media list. The 2nd value in the pair is true
141 * if the medium is locked for writing and false if locked for
142 * reading.
143 */
144 typedef std::list <std::pair <ComPtr <IMedium>, bool > > LockedMedia;
145 LockedMedia mLockedMedia;
146 };
147
148 Data();
149 ~Data();
150
151 const Guid mUuid;
152 BOOL mRegistered;
153
154 Bstr mConfigFile;
155 Bstr mConfigFileFull;
156
157 Utf8Str mSettingsFileVersion;
158
159 BOOL mAccessible;
160 com::ErrorInfo mAccessError;
161
162 MachineState_T mMachineState;
163 RTTIMESPEC mLastStateChange;
164
165 /* Note: These are guarded by VirtualBoxBase::stateLockHandle() */
166 uint32_t mMachineStateDeps;
167 RTSEMEVENTMULTI mMachineStateDepsSem;
168 uint32_t mMachineStateChangePending;
169
170 BOOL mCurrentStateModified;
171
172 RTFILE mHandleCfgFile;
173
174 Session mSession;
175
176 ComObjPtr <Snapshot> mFirstSnapshot;
177 ComObjPtr <Snapshot> mCurrentSnapshot;
178
179 };
180
181 /**
182 * Saved state data.
183 *
184 * It's actually only the state file path string, but it needs to be
185 * separate from Data, because Machine and SessionMachine instances
186 * share it, while SnapshotMachine does not.
187 *
188 * The data variable is |mSSData|.
189 */
190 struct SSData
191 {
192 Bstr mStateFilePath;
193 };
194
195 /**
196 * User changeable machine data.
197 *
198 * This data is common for all machine snapshots, i.e. it is shared
199 * by all SnapshotMachine instances associated with the given machine
200 * using the util::Backupable template through the |mUserData| variable.
201 *
202 * SessionMachine instances can alter this data and discard changes.
203 *
204 * @note There is no need to lock anything inside init() or uninit()
205 * methods, because they are always serialized (see AutoCaller).
206 */
207 struct UserData
208 {
209 UserData();
210 ~UserData();
211
212 bool operator== (const UserData &that) const
213 {
214 return this == &that ||
215 (mName == that.mName &&
216 mNameSync == that.mNameSync &&
217 mDescription == that.mDescription &&
218 mOSTypeId == that.mOSTypeId &&
219 mSnapshotFolderFull == that.mSnapshotFolderFull);
220 }
221
222 Bstr mName;
223 BOOL mNameSync;
224 Bstr mDescription;
225 Bstr mOSTypeId;
226 Bstr mSnapshotFolder;
227 Bstr mSnapshotFolderFull;
228 };
229
230 /**
231 * Hardware data.
232 *
233 * This data is unique for a machine and for every machine snapshot.
234 * Stored using the util::Backupable template in the |mHWData| variable.
235 *
236 * SessionMachine instances can alter this data and discard changes.
237 */
238 struct HWData
239 {
240 /**
241 * Data structure to hold information about a guest property.
242 */
243 struct GuestProperty {
244 /** Property name */
245 Bstr mName;
246 /** Property value */
247 Bstr mValue;
248 /** Property timestamp */
249 ULONG64 mTimestamp;
250 /** Property flags */
251 ULONG mFlags;
252 };
253
254 HWData();
255 ~HWData();
256
257 bool operator== (const HWData &that) const;
258
259 Bstr mHWVersion;
260 ULONG mMemorySize;
261 ULONG mMemoryBalloonSize;
262 ULONG mStatisticsUpdateInterval;
263 ULONG mVRAMSize;
264 ULONG mMonitorCount;
265 TSBool_T mHWVirtExEnabled;
266 BOOL mHWVirtExNestedPagingEnabled;
267 BOOL mHWVirtExVPIDEnabled;
268 BOOL mAccelerate3DEnabled;
269 BOOL mPAEEnabled;
270 ULONG mCPUCount;
271
272 DeviceType_T mBootOrder [SchemaDefs::MaxBootPosition];
273
274 typedef std::list <ComObjPtr <SharedFolder> > SharedFolderList;
275 SharedFolderList mSharedFolders;
276 ClipboardMode_T mClipboardMode;
277 typedef std::list <GuestProperty> GuestPropertyList;
278 GuestPropertyList mGuestProperties;
279 BOOL mPropertyServiceActive;
280 Bstr mGuestPropertyNotificationPatterns;
281 };
282
283 /**
284 * Hard disk data.
285 *
286 * The usage policy is the same as for HWData, but a separate structure
287 * is necessary because hard disk data requires different procedures when
288 * taking or discarding snapshots, etc.
289 *
290 * The data variable is |mHDData|.
291 */
292 struct HDData
293 {
294 HDData();
295 ~HDData();
296
297 bool operator== (const HDData &that) const;
298
299 typedef std::list< ComObjPtr<HardDiskAttachment> > AttachmentList;
300 AttachmentList mAttachments;
301 };
302
303 enum StateDependency
304 {
305 AnyStateDep = 0, MutableStateDep, MutableOrSavedStateDep
306 };
307
308 /**
309 * Helper class that safely manages the machine state dependency by
310 * calling Machine::addStateDependency() on construction and
311 * Machine::releaseStateDependency() on destruction. Intended for Machine
312 * children. The usage pattern is:
313 *
314 * @code
315 * AutoCaller autoCaller (this);
316 * CheckComRCReturnRC (autoCaller.rc());
317 *
318 * Machine::AutoStateDependency <MutableStateDep> adep (mParent);
319 * CheckComRCReturnRC (stateDep.rc());
320 * ...
321 * // code that depends on the particular machine state
322 * ...
323 * @endcode
324 *
325 * Note that it is more convenient to use the following individual
326 * shortcut classes instead of using this template directly:
327 * AutoAnyStateDependency, AutoMutableStateDependency and
328 * AutoMutableOrSavedStateDependency. The usage pattern is exactly the
329 * same as above except that there is no need to specify the template
330 * argument because it is already done by the shortcut class.
331 *
332 * @param taDepType Dependecy type to manage.
333 */
334 template <StateDependency taDepType = AnyStateDep>
335 class AutoStateDependency
336 {
337 public:
338
339 AutoStateDependency (Machine *aThat)
340 : mThat (aThat), mRC (S_OK)
341 , mMachineState (MachineState_Null)
342 , mRegistered (FALSE)
343 {
344 Assert (aThat);
345 mRC = aThat->addStateDependency (taDepType, &mMachineState,
346 &mRegistered);
347 }
348 ~AutoStateDependency()
349 {
350 if (SUCCEEDED (mRC))
351 mThat->releaseStateDependency();
352 }
353
354 /** Decreases the number of dependencies before the instance is
355 * destroyed. Note that will reset #rc() to E_FAIL. */
356 void release()
357 {
358 AssertReturnVoid (SUCCEEDED (mRC));
359 mThat->releaseStateDependency();
360 mRC = E_FAIL;
361 }
362
363 /** Restores the number of callers after by #release(). #rc() will be
364 * reset to the result of calling addStateDependency() and must be
365 * rechecked to ensure the operation succeeded. */
366 void add()
367 {
368 AssertReturnVoid (!SUCCEEDED (mRC));
369 mRC = mThat->addStateDependency (taDepType, &mMachineState,
370 &mRegistered);
371 }
372
373 /** Returns the result of Machine::addStateDependency(). */
374 HRESULT rc() const { return mRC; }
375
376 /** Shortcut to SUCCEEDED (rc()). */
377 bool isOk() const { return SUCCEEDED (mRC); }
378
379 /** Returns the machine state value as returned by
380 * Machine::addStateDependency(). */
381 MachineState_T machineState() const { return mMachineState; }
382
383 /** Returns the machine state value as returned by
384 * Machine::addStateDependency(). */
385 BOOL machineRegistered() const { return mRegistered; }
386
387 protected:
388
389 Machine *mThat;
390 HRESULT mRC;
391 MachineState_T mMachineState;
392 BOOL mRegistered;
393
394 private:
395
396 DECLARE_CLS_COPY_CTOR_ASSIGN_NOOP (AutoStateDependency)
397 DECLARE_CLS_NEW_DELETE_NOOP (AutoStateDependency)
398 };
399
400 /**
401 * Shortcut to AutoStateDependency <AnyStateDep>.
402 * See AutoStateDependency to get the usage pattern.
403 *
404 * Accepts any machine state and guarantees the state won't change before
405 * this object is destroyed. If the machine state cannot be protected (as
406 * a result of the state change currently in progress), this instance's
407 * #rc() method will indicate a failure, and the caller is not allowed to
408 * rely on any particular machine state and should return the failed
409 * result code to the upper level.
410 */
411 typedef AutoStateDependency <AnyStateDep> AutoAnyStateDependency;
412
413 /**
414 * Shortcut to AutoStateDependency <MutableStateDep>.
415 * See AutoStateDependency to get the usage pattern.
416 *
417 * Succeeds only if the machine state is in one of the mutable states, and
418 * guarantees the given mutable state won't change before this object is
419 * destroyed. If the machine is not mutable, this instance's #rc() method
420 * will indicate a failure, and the caller is not allowed to rely on any
421 * particular machine state and should return the failed result code to
422 * the upper level.
423 *
424 * Intended to be used within all setter methods of IMachine
425 * children objects (DVDDrive, NetworkAdapter, AudioAdapter, etc.) to
426 * provide data protection and consistency.
427 */
428 typedef AutoStateDependency <MutableStateDep> AutoMutableStateDependency;
429
430 /**
431 * Shortcut to AutoStateDependency <MutableOrSavedStateDep>.
432 * See AutoStateDependency to get the usage pattern.
433 *
434 * Succeeds only if the machine state is in one of the mutable states, or
435 * if the machine is in the Saved state, and guarantees the given mutable
436 * state won't change before this object is destroyed. If the machine is
437 * not mutable, this instance's #rc() method will indicate a failure, and
438 * the caller is not allowed to rely on any particular machine state and
439 * should return the failed result code to the upper level.
440 *
441 * Intended to be used within setter methods of IMachine
442 * children objects that may also operate on Saved machines.
443 */
444 typedef AutoStateDependency <MutableOrSavedStateDep> AutoMutableOrSavedStateDependency;
445
446
447 VIRTUALBOXBASE_ADD_ERRORINFO_SUPPORT (Machine)
448
449 DECLARE_NOT_AGGREGATABLE(Machine)
450
451 DECLARE_PROTECT_FINAL_CONSTRUCT()
452
453 BEGIN_COM_MAP(Machine)
454 COM_INTERFACE_ENTRY(ISupportErrorInfo)
455 COM_INTERFACE_ENTRY(IMachine)
456 END_COM_MAP()
457
458 NS_DECL_ISUPPORTS
459
460 DECLARE_EMPTY_CTOR_DTOR (Machine)
461
462 HRESULT FinalConstruct();
463 void FinalRelease();
464
465 enum InitMode { Init_New, Init_Existing, Init_Registered };
466
467 // public initializer/uninitializer for internal purposes only
468 HRESULT init (VirtualBox *aParent, CBSTR aConfigFile,
469 InitMode aMode, CBSTR aName = NULL,
470 GuestOSType *aOsType = NULL,
471 BOOL aNameSync = TRUE, const Guid *aId = NULL);
472 void uninit();
473
474 // IMachine properties
475 STDMETHOD(COMGETTER(Parent))(IVirtualBox **aParent);
476 STDMETHOD(COMGETTER(Accessible)) (BOOL *aAccessible);
477 STDMETHOD(COMGETTER(AccessError)) (IVirtualBoxErrorInfo **aAccessError);
478 STDMETHOD(COMGETTER(Name))(BSTR *aName);
479 STDMETHOD(COMSETTER(Name))(IN_BSTR aName);
480 STDMETHOD(COMGETTER(Description))(BSTR *aDescription);
481 STDMETHOD(COMSETTER(Description))(IN_BSTR aDescription);
482 STDMETHOD(COMGETTER(Id))(OUT_GUID aId);
483 STDMETHOD(COMGETTER(OSTypeId)) (BSTR *aOSTypeId);
484 STDMETHOD(COMSETTER(OSTypeId)) (IN_BSTR aOSTypeId);
485 STDMETHOD(COMGETTER(HardwareVersion))(BSTR *aVersion);
486 STDMETHOD(COMSETTER(HardwareVersion))(IN_BSTR aVersion);
487 STDMETHOD(COMGETTER(MemorySize))(ULONG *memorySize);
488 STDMETHOD(COMSETTER(MemorySize))(ULONG memorySize);
489 STDMETHOD(COMGETTER(CPUCount))(ULONG *cpuCount);
490 STDMETHOD(COMSETTER(CPUCount))(ULONG cpuCount);
491 STDMETHOD(COMGETTER(MemoryBalloonSize))(ULONG *memoryBalloonSize);
492 STDMETHOD(COMSETTER(MemoryBalloonSize))(ULONG memoryBalloonSize);
493 STDMETHOD(COMGETTER(StatisticsUpdateInterval))(ULONG *statisticsUpdateInterval);
494 STDMETHOD(COMSETTER(StatisticsUpdateInterval))(ULONG statisticsUpdateInterval);
495 STDMETHOD(COMGETTER(VRAMSize))(ULONG *memorySize);
496 STDMETHOD(COMSETTER(VRAMSize))(ULONG memorySize);
497 STDMETHOD(COMGETTER(MonitorCount))(ULONG *monitorCount);
498 STDMETHOD(COMSETTER(MonitorCount))(ULONG monitorCount);
499 STDMETHOD(COMGETTER(Accelerate3DEnabled))(BOOL *enabled);
500 STDMETHOD(COMSETTER(Accelerate3DEnabled))(BOOL enabled);
501 STDMETHOD(COMGETTER(BIOSSettings))(IBIOSSettings **biosSettings);
502 STDMETHOD(COMGETTER(HWVirtExEnabled))(TSBool_T *enabled);
503 STDMETHOD(COMSETTER(HWVirtExEnabled))(TSBool_T enabled);
504 STDMETHOD(COMGETTER(HWVirtExNestedPagingEnabled))(BOOL *enabled);
505 STDMETHOD(COMSETTER(HWVirtExNestedPagingEnabled))(BOOL enabled);
506 STDMETHOD(COMGETTER(HWVirtExVPIDEnabled))(BOOL *enabled);
507 STDMETHOD(COMSETTER(HWVirtExVPIDEnabled))(BOOL enabled);
508 STDMETHOD(COMGETTER(PAEEnabled))(BOOL *enabled);
509 STDMETHOD(COMSETTER(PAEEnabled))(BOOL enabled);
510 STDMETHOD(COMGETTER(SnapshotFolder))(BSTR *aSavedStateFolder);
511 STDMETHOD(COMSETTER(SnapshotFolder))(IN_BSTR aSavedStateFolder);
512 STDMETHOD(COMGETTER(HardDiskAttachments))(ComSafeArrayOut (IHardDiskAttachment *, aAttachments));
513 STDMETHOD(COMGETTER(VRDPServer))(IVRDPServer **vrdpServer);
514 STDMETHOD(COMGETTER(DVDDrive))(IDVDDrive **dvdDrive);
515 STDMETHOD(COMGETTER(FloppyDrive))(IFloppyDrive **floppyDrive);
516 STDMETHOD(COMGETTER(AudioAdapter))(IAudioAdapter **audioAdapter);
517 STDMETHOD(COMGETTER(USBController)) (IUSBController * *aUSBController);
518 STDMETHOD(COMGETTER(SettingsFilePath)) (BSTR *aFilePath);
519 STDMETHOD(COMGETTER(SettingsFileVersion)) (BSTR *aSettingsFileVersion);
520 STDMETHOD(COMGETTER(SettingsModified)) (BOOL *aModified);
521 STDMETHOD(COMGETTER(SessionState)) (SessionState_T *aSessionState);
522 STDMETHOD(COMGETTER(SessionType)) (BSTR *aSessionType);
523 STDMETHOD(COMGETTER(SessionPid)) (ULONG *aSessionPid);
524 STDMETHOD(COMGETTER(State)) (MachineState_T *machineState);
525 STDMETHOD(COMGETTER(LastStateChange)) (LONG64 *aLastStateChange);
526 STDMETHOD(COMGETTER(StateFilePath)) (BSTR *aStateFilePath);
527 STDMETHOD(COMGETTER(LogFolder)) (BSTR *aLogFolder);
528 STDMETHOD(COMGETTER(CurrentSnapshot)) (ISnapshot **aCurrentSnapshot);
529 STDMETHOD(COMGETTER(SnapshotCount)) (ULONG *aSnapshotCount);
530 STDMETHOD(COMGETTER(CurrentStateModified))(BOOL *aCurrentStateModified);
531 STDMETHOD(COMGETTER(SharedFolders)) (ComSafeArrayOut (ISharedFolder *, aSharedFolders));
532 STDMETHOD(COMGETTER(ClipboardMode)) (ClipboardMode_T *aClipboardMode);
533 STDMETHOD(COMSETTER(ClipboardMode)) (ClipboardMode_T aClipboardMode);
534 STDMETHOD(COMGETTER(GuestPropertyNotificationPatterns)) (BSTR *aPattern);
535 STDMETHOD(COMSETTER(GuestPropertyNotificationPatterns)) (IN_BSTR aPattern);
536 STDMETHOD(COMGETTER(StorageControllers)) (ComSafeArrayOut(IStorageController *, aStorageControllers));
537
538 // IMachine methods
539 STDMETHOD(SetBootOrder)(ULONG aPosition, DeviceType_T aDevice);
540 STDMETHOD(GetBootOrder)(ULONG aPosition, DeviceType_T *aDevice);
541 STDMETHOD(AttachHardDisk)(IN_GUID aId, IN_BSTR aControllerName,
542 LONG aControllerPort, LONG aDevice);
543 STDMETHOD(GetHardDisk)(IN_BSTR aControllerName, LONG aControllerPort, LONG aDevice,
544 IHardDisk **aHardDisk);
545 STDMETHOD(DetachHardDisk)(IN_BSTR aControllerName, LONG aControllerPort, LONG aDevice);
546 STDMETHOD(GetSerialPort) (ULONG slot, ISerialPort **port);
547 STDMETHOD(GetParallelPort) (ULONG slot, IParallelPort **port);
548 STDMETHOD(GetNetworkAdapter) (ULONG slot, INetworkAdapter **adapter);
549 STDMETHOD(GetNextExtraDataKey)(IN_BSTR aKey, BSTR *aNextKey, BSTR *aNextValue);
550 STDMETHOD(GetExtraData)(IN_BSTR aKey, BSTR *aValue);
551 STDMETHOD(SetExtraData)(IN_BSTR aKey, IN_BSTR aValue);
552 STDMETHOD(SaveSettings)();
553 STDMETHOD(SaveSettingsWithBackup) (BSTR *aBakFileName);
554 STDMETHOD(DiscardSettings)();
555 STDMETHOD(DeleteSettings)();
556 STDMETHOD(Export)(IAppliance *aAppliance, IVirtualSystemDescription **aDescription);
557 STDMETHOD(GetSnapshot) (IN_GUID aId, ISnapshot **aSnapshot);
558 STDMETHOD(FindSnapshot) (IN_BSTR aName, ISnapshot **aSnapshot);
559 STDMETHOD(SetCurrentSnapshot) (IN_GUID aId);
560 STDMETHOD(CreateSharedFolder) (IN_BSTR aName, IN_BSTR aHostPath, BOOL aWritable);
561 STDMETHOD(RemoveSharedFolder) (IN_BSTR aName);
562 STDMETHOD(CanShowConsoleWindow) (BOOL *aCanShow);
563 STDMETHOD(ShowConsoleWindow) (ULONG64 *aWinId);
564 STDMETHOD(GetGuestProperty) (IN_BSTR aName, BSTR *aValue, ULONG64 *aTimestamp, BSTR *aFlags);
565 STDMETHOD(GetGuestPropertyValue) (IN_BSTR aName, BSTR *aValue);
566 STDMETHOD(GetGuestPropertyTimestamp) (IN_BSTR aName, ULONG64 *aTimestamp);
567 STDMETHOD(SetGuestProperty) (IN_BSTR aName, IN_BSTR aValue, IN_BSTR aFlags);
568 STDMETHOD(SetGuestPropertyValue) (IN_BSTR aName, IN_BSTR aValue);
569 STDMETHOD(EnumerateGuestProperties) (IN_BSTR aPattern, ComSafeArrayOut(BSTR, aNames), ComSafeArrayOut(BSTR, aValues), ComSafeArrayOut(ULONG64, aTimestamps), ComSafeArrayOut(BSTR, aFlags));
570 STDMETHOD(GetHardDiskAttachmentsOfController)(IN_BSTR aName, ComSafeArrayOut (IHardDiskAttachment *, aAttachments));
571 STDMETHOD(AddStorageController) (IN_BSTR aName, StorageBus_T aConnectionType, IStorageController **controller);
572 STDMETHOD(RemoveStorageController (IN_BSTR aName));
573 STDMETHOD(GetStorageControllerByName (IN_BSTR aName, IStorageController **storageController));
574
575 // public methods only for internal purposes
576
577 InstanceType type() const { return mType; }
578
579 /// @todo (dmik) add lock and make non-inlined after revising classes
580 // that use it. Note: they should enter Machine lock to keep the returned
581 // information valid!
582 bool isRegistered() { return !!mData->mRegistered; }
583
584 // unsafe inline public methods for internal purposes only (ensure there is
585 // a caller and a read lock before calling them!)
586
587 /**
588 * Returns the VirtualBox object this machine belongs to.
589 *
590 * @note This method doesn't check this object's readiness. Intended to be
591 * used by ready Machine children (whose readiness is bound to the parent's
592 * one) or after doing addCaller() manually.
593 */
594 const ComObjPtr <VirtualBox, ComWeakRef> &virtualBox() const { return mParent; }
595
596 /**
597 * Returns this machine ID.
598 *
599 * @note This method doesn't check this object's readiness. Intended to be
600 * used by ready Machine children (whose readiness is bound to the parent's
601 * one) or after adding a caller manually.
602 */
603 const Guid &id() const { return mData->mUuid; }
604
605 /**
606 * Returns the snapshot ID this machine represents or an empty UUID if this
607 * instance is not SnapshotMachine.
608 *
609 * @note This method doesn't check this object's readiness. Intended to be
610 * used by ready Machine children (whose readiness is bound to the parent's
611 * one) or after adding a caller manually.
612 */
613 inline const Guid &snapshotId() const;
614
615 /**
616 * Returns this machine's full settings file path.
617 *
618 * @note This method doesn't lock this object or check its readiness.
619 * Intended to be used only after doing addCaller() manually and locking it
620 * for reading.
621 */
622 const Bstr &settingsFileFull() const { return mData->mConfigFileFull; }
623
624 /**
625 * Returns this machine name.
626 *
627 * @note This method doesn't lock this object or check its readiness.
628 * Intended to be used only after doing addCaller() manually and locking it
629 * for reading.
630 */
631 const Bstr &name() const { return mUserData->mName; }
632
633 // callback handlers
634 virtual HRESULT onDVDDriveChange() { return S_OK; }
635 virtual HRESULT onFloppyDriveChange() { return S_OK; }
636 virtual HRESULT onNetworkAdapterChange(INetworkAdapter * /* networkAdapter */) { return S_OK; }
637 virtual HRESULT onSerialPortChange(ISerialPort * /* serialPort */) { return S_OK; }
638 virtual HRESULT onParallelPortChange(IParallelPort * /* parallelPort */) { return S_OK; }
639 virtual HRESULT onVRDPServerChange() { return S_OK; }
640 virtual HRESULT onUSBControllerChange() { return S_OK; }
641 virtual HRESULT onStorageControllerChange() { return S_OK; }
642 virtual HRESULT onSharedFolderChange() { return S_OK; }
643
644 HRESULT saveRegistryEntry (settings::Key &aEntryNode);
645
646 int calculateFullPath (const char *aPath, Utf8Str &aResult);
647 void calculateRelativePath (const char *aPath, Utf8Str &aResult);
648
649 void getLogFolder (Utf8Str &aLogFolder);
650
651 HRESULT openSession (IInternalSessionControl *aControl);
652 HRESULT openRemoteSession (IInternalSessionControl *aControl,
653 IN_BSTR aType, IN_BSTR aEnvironment,
654 Progress *aProgress);
655 HRESULT openExistingSession (IInternalSessionControl *aControl);
656
657#if defined (RT_OS_WINDOWS)
658
659 bool isSessionOpen (ComObjPtr <SessionMachine> &aMachine,
660 ComPtr <IInternalSessionControl> *aControl = NULL,
661 HANDLE *aIPCSem = NULL, bool aAllowClosing = false);
662 bool isSessionSpawning (RTPROCESS *aPID = NULL);
663
664 bool isSessionOpenOrClosing (ComObjPtr <SessionMachine> &aMachine,
665 ComPtr <IInternalSessionControl> *aControl = NULL,
666 HANDLE *aIPCSem = NULL)
667 { return isSessionOpen (aMachine, aControl, aIPCSem, true /* aAllowClosing */); }
668
669#elif defined (RT_OS_OS2)
670
671 bool isSessionOpen (ComObjPtr <SessionMachine> &aMachine,
672 ComPtr <IInternalSessionControl> *aControl = NULL,
673 HMTX *aIPCSem = NULL, bool aAllowClosing = false);
674
675 bool isSessionSpawning (RTPROCESS *aPID = NULL);
676
677 bool isSessionOpenOrClosing (ComObjPtr <SessionMachine> &aMachine,
678 ComPtr <IInternalSessionControl> *aControl = NULL,
679 HMTX *aIPCSem = NULL)
680 { return isSessionOpen (aMachine, aControl, aIPCSem, true /* aAllowClosing */); }
681
682#else
683
684 bool isSessionOpen (ComObjPtr <SessionMachine> &aMachine,
685 ComPtr <IInternalSessionControl> *aControl = NULL,
686 bool aAllowClosing = false);
687 bool isSessionSpawning();
688
689 bool isSessionOpenOrClosing (ComObjPtr <SessionMachine> &aMachine,
690 ComPtr <IInternalSessionControl> *aControl = NULL)
691 { return isSessionOpen (aMachine, aControl, true /* aAllowClosing */); }
692
693#endif
694
695 bool checkForSpawnFailure();
696
697 HRESULT trySetRegistered (BOOL aRegistered);
698
699 HRESULT getSharedFolder (CBSTR aName,
700 ComObjPtr <SharedFolder> &aSharedFolder,
701 bool aSetError = false)
702 {
703 AutoWriteLock alock (this);
704 return findSharedFolder (aName, aSharedFolder, aSetError);
705 }
706
707 HRESULT addStateDependency (StateDependency aDepType = AnyStateDep,
708 MachineState_T *aState = NULL,
709 BOOL *aRegistered = NULL);
710 void releaseStateDependency();
711
712 // for VirtualBoxSupportErrorInfoImpl
713 static const wchar_t *getComponentName() { return L"Machine"; }
714
715protected:
716
717 HRESULT registeredInit();
718
719 HRESULT checkStateDependency (StateDependency aDepType);
720
721 inline Machine *machine();
722
723 HRESULT initDataAndChildObjects();
724 void uninitDataAndChildObjects();
725
726 void ensureNoStateDependencies();
727
728 virtual HRESULT setMachineState (MachineState_T aMachineState);
729
730 HRESULT findSharedFolder (CBSTR aName,
731 ComObjPtr <SharedFolder> &aSharedFolder,
732 bool aSetError = false);
733
734 HRESULT loadSettings (bool aRegistered);
735 HRESULT loadSnapshot (const settings::Key &aNode, const Guid &aCurSnapshotId,
736 Snapshot *aParentSnapshot);
737 HRESULT loadHardware (const settings::Key &aNode);
738 HRESULT loadStorageControllers (const settings::Key &aNode, bool aRegistered,
739 const Guid *aSnapshotId = NULL);
740 HRESULT loadStorageDevices (ComObjPtr<StorageController> aStorageController,
741 const settings::Key &aNode, bool aRegistered,
742 const Guid *aSnapshotId = NULL);
743
744 HRESULT findSnapshotNode (Snapshot *aSnapshot, settings::Key &aMachineNode,
745 settings::Key *aSnapshotsNode,
746 settings::Key *aSnapshotNode);
747
748 HRESULT findSnapshot (const Guid &aId, ComObjPtr <Snapshot> &aSnapshot,
749 bool aSetError = false);
750 HRESULT findSnapshot (IN_BSTR aName, ComObjPtr <Snapshot> &aSnapshot,
751 bool aSetError = false);
752
753 HRESULT getStorageControllerByName(CBSTR aName,
754 ComObjPtr <StorageController> &aStorageController,
755 bool aSetError = false);
756
757 HRESULT getHardDiskAttachmentsOfController(CBSTR aName,
758 HDData::AttachmentList &aAttachments);
759
760 enum
761 {
762 /* flags for #saveSettings() */
763 SaveS_ResetCurStateModified = 0x01,
764 SaveS_InformCallbacksAnyway = 0x02,
765 /* ops for #saveSnapshotSettings() */
766 SaveSS_NoOp = 0x00, SaveSS_AddOp = 0x01,
767 SaveSS_UpdateAttrsOp = 0x02, SaveSS_UpdateAllOp = 0x03,
768 SaveSS_OpMask = 0xF,
769 /* flags for #saveSnapshotSettings() */
770 SaveSS_CurStateModified = 0x40,
771 SaveSS_CurrentId = 0x80,
772 /* flags for #saveStateSettings() */
773 SaveSTS_CurStateModified = 0x20,
774 SaveSTS_StateFilePath = 0x40,
775 SaveSTS_StateTimeStamp = 0x80,
776 };
777
778 HRESULT prepareSaveSettings (bool &aRenamed, bool &aNew);
779 HRESULT saveSettings (int aFlags = 0);
780
781 HRESULT saveSnapshotSettings (Snapshot *aSnapshot, int aOpFlags);
782 HRESULT saveSnapshotSettingsWorker (settings::Key &aMachineNode,
783 Snapshot *aSnapshot, int aOpFlags);
784
785 HRESULT saveSnapshot (settings::Key &aNode, Snapshot *aSnapshot, bool aAttrsOnly);
786 HRESULT saveHardware (settings::Key &aNode);
787 HRESULT saveStorageControllers (settings::Key &aNode);
788 HRESULT saveStorageDevices (ComObjPtr<StorageController> aStorageController,
789 settings::Key &aNode);
790
791 HRESULT saveStateSettings (int aFlags);
792
793 HRESULT createImplicitDiffs (const Bstr &aFolder,
794 ComObjPtr <Progress> &aProgress,
795 bool aOnline);
796 HRESULT deleteImplicitDiffs();
797
798 void fixupHardDisks(bool aCommit, bool aOnline = false);
799
800 HRESULT lockConfig();
801public: /* To have it available in VirtualBox::UnregisterMachine. */
802 HRESULT unlockConfig();
803
804protected:
805 /** @note This method is not thread safe */
806 BOOL isConfigLocked()
807 {
808 return !!mData && mData->mHandleCfgFile != NIL_RTFILE;
809 }
810
811 bool isInOwnDir (Utf8Str *aSettingsDir = NULL);
812
813 bool isModified();
814 bool isReallyModified (bool aIgnoreUserData = false);
815 void rollback (bool aNotify);
816 void commit();
817 void copyFrom (Machine *aThat);
818
819#ifdef VBOX_WITH_RESOURCE_USAGE_API
820 void registerMetrics (PerformanceCollector *aCollector, Machine *aMachine, RTPROCESS pid);
821 void unregisterMetrics (PerformanceCollector *aCollector, Machine *aMachine);
822#endif /* VBOX_WITH_RESOURCE_USAGE_API */
823
824 const InstanceType mType;
825
826 const ComObjPtr <Machine, ComWeakRef> mPeer;
827
828 const ComObjPtr <VirtualBox, ComWeakRef> mParent;
829
830 Shareable <Data> mData;
831 Shareable <SSData> mSSData;
832
833 Backupable <UserData> mUserData;
834 Backupable <HWData> mHWData;
835 Backupable <HDData> mHDData;
836
837 // the following fields need special backup/rollback/commit handling,
838 // so they cannot be a part of HWData
839
840 const ComObjPtr <VRDPServer> mVRDPServer;
841 const ComObjPtr <DVDDrive> mDVDDrive;
842 const ComObjPtr <FloppyDrive> mFloppyDrive;
843 const ComObjPtr <SerialPort>
844 mSerialPorts [SchemaDefs::SerialPortCount];
845 const ComObjPtr <ParallelPort>
846 mParallelPorts [SchemaDefs::ParallelPortCount];
847 const ComObjPtr <AudioAdapter> mAudioAdapter;
848 const ComObjPtr <USBController> mUSBController;
849 const ComObjPtr <BIOSSettings> mBIOSSettings;
850 const ComObjPtr <NetworkAdapter>
851 mNetworkAdapters [SchemaDefs::NetworkAdapterCount];
852
853 typedef std::list< ComObjPtr<StorageController> > StorageControllerList;
854 Backupable<StorageControllerList> mStorageControllers;
855
856 friend class SessionMachine;
857 friend class SnapshotMachine;
858};
859
860// SessionMachine class
861////////////////////////////////////////////////////////////////////////////////
862
863/**
864 * @note Notes on locking objects of this class:
865 * SessionMachine shares some data with the primary Machine instance (pointed
866 * to by the |mPeer| member). In order to provide data consistency it also
867 * shares its lock handle. This means that whenever you lock a SessionMachine
868 * instance using Auto[Reader]Lock or AutoMultiLock, the corresponding Machine
869 * instance is also locked in the same lock mode. Keep it in mind.
870 */
871class ATL_NO_VTABLE SessionMachine :
872 public VirtualBoxSupportTranslation <SessionMachine>,
873 public Machine,
874 public IInternalMachineControl
875{
876public:
877
878 VIRTUALBOXSUPPORTTRANSLATION_OVERRIDE(SessionMachine)
879
880 DECLARE_NOT_AGGREGATABLE(SessionMachine)
881
882 DECLARE_PROTECT_FINAL_CONSTRUCT()
883
884 BEGIN_COM_MAP(SessionMachine)
885 COM_INTERFACE_ENTRY(ISupportErrorInfo)
886 COM_INTERFACE_ENTRY(IMachine)
887 COM_INTERFACE_ENTRY(IInternalMachineControl)
888 END_COM_MAP()
889
890 NS_DECL_ISUPPORTS
891
892 DECLARE_EMPTY_CTOR_DTOR (SessionMachine)
893
894 HRESULT FinalConstruct();
895 void FinalRelease();
896
897 // public initializer/uninitializer for internal purposes only
898 HRESULT init (Machine *aMachine);
899 void uninit() { uninit (Uninit::Unexpected); }
900
901 // util::Lockable interface
902 RWLockHandle *lockHandle() const;
903
904 // IInternalMachineControl methods
905 STDMETHOD(UpdateState)(MachineState_T machineState);
906 STDMETHOD(GetIPCId)(BSTR *id);
907 STDMETHOD(RunUSBDeviceFilters) (IUSBDevice *aUSBDevice, BOOL *aMatched, ULONG *aMaskedIfs);
908 STDMETHOD(CaptureUSBDevice) (IN_GUID aId);
909 STDMETHOD(DetachUSBDevice) (IN_GUID aId, BOOL aDone);
910 STDMETHOD(AutoCaptureUSBDevices)();
911 STDMETHOD(DetachAllUSBDevices)(BOOL aDone);
912 STDMETHOD(OnSessionEnd)(ISession *aSession, IProgress **aProgress);
913 STDMETHOD(BeginSavingState) (IProgress *aProgress, BSTR *aStateFilePath);
914 STDMETHOD(EndSavingState) (BOOL aSuccess);
915 STDMETHOD(AdoptSavedState) (IN_BSTR aSavedStateFile);
916 STDMETHOD(BeginTakingSnapshot) (IConsole *aInitiator,
917 IN_BSTR aName, IN_BSTR aDescription,
918 IProgress *aProgress, BSTR *aStateFilePath,
919 IProgress **aServerProgress);
920 STDMETHOD(EndTakingSnapshot) (BOOL aSuccess);
921 STDMETHOD(DiscardSnapshot) (IConsole *aInitiator, IN_GUID aId,
922 MachineState_T *aMachineState, IProgress **aProgress);
923 STDMETHOD(DiscardCurrentState) (
924 IConsole *aInitiator, MachineState_T *aMachineState, IProgress **aProgress);
925 STDMETHOD(DiscardCurrentSnapshotAndState) (
926 IConsole *aInitiator, MachineState_T *aMachineState, IProgress **aProgress);
927 STDMETHOD(PullGuestProperties) (ComSafeArrayOut(BSTR, aNames), ComSafeArrayOut(BSTR, aValues),
928 ComSafeArrayOut(ULONG64, aTimestamps), ComSafeArrayOut(BSTR, aFlags));
929 STDMETHOD(PushGuestProperties) (ComSafeArrayIn(IN_BSTR, aNames), ComSafeArrayIn(IN_BSTR, aValues),
930 ComSafeArrayIn(ULONG64, aTimestamps), ComSafeArrayIn(IN_BSTR, aFlags));
931 STDMETHOD(PushGuestProperty) (IN_BSTR aName, IN_BSTR aValue,
932 ULONG64 aTimestamp, IN_BSTR aFlags);
933 STDMETHOD(LockMedia)() { return lockMedia(); }
934
935 // public methods only for internal purposes
936
937 bool checkForDeath();
938
939 HRESULT onDVDDriveChange();
940 HRESULT onFloppyDriveChange();
941 HRESULT onNetworkAdapterChange(INetworkAdapter *networkAdapter);
942 HRESULT onStorageControllerChange();
943 HRESULT onSerialPortChange(ISerialPort *serialPort);
944 HRESULT onParallelPortChange(IParallelPort *parallelPort);
945 HRESULT onVRDPServerChange();
946 HRESULT onUSBControllerChange();
947 HRESULT onUSBDeviceAttach (IUSBDevice *aDevice,
948 IVirtualBoxErrorInfo *aError,
949 ULONG aMaskedIfs);
950 HRESULT onUSBDeviceDetach (IN_GUID aId,
951 IVirtualBoxErrorInfo *aError);
952 HRESULT onSharedFolderChange();
953
954 bool hasMatchingUSBFilter (const ComObjPtr <HostUSBDevice> &aDevice, ULONG *aMaskedIfs);
955
956private:
957
958 struct SnapshotData
959 {
960 SnapshotData() : mLastState (MachineState_Null) {}
961
962 MachineState_T mLastState;
963
964 // used when taking snapshot
965 ComObjPtr <Snapshot> mSnapshot;
966 ComObjPtr <Progress> mServerProgress;
967 ComObjPtr <CombinedProgress> mCombinedProgress;
968
969 // used when saving state
970 Guid mProgressId;
971 Bstr mStateFilePath;
972 };
973
974 struct Uninit
975 {
976 enum Reason { Unexpected, Abnormal, Normal };
977 };
978
979 struct Task;
980 struct TakeSnapshotTask;
981 struct DiscardSnapshotTask;
982 struct DiscardCurrentStateTask;
983
984 friend struct TakeSnapshotTask;
985 friend struct DiscardSnapshotTask;
986 friend struct DiscardCurrentStateTask;
987
988 void uninit (Uninit::Reason aReason);
989
990 HRESULT endSavingState (BOOL aSuccess);
991 HRESULT endTakingSnapshot (BOOL aSuccess);
992
993 typedef std::map <ComObjPtr <Machine>, MachineState_T> AffectedMachines;
994
995 void takeSnapshotHandler (TakeSnapshotTask &aTask);
996 void discardSnapshotHandler (DiscardSnapshotTask &aTask);
997 void discardCurrentStateHandler (DiscardCurrentStateTask &aTask);
998
999 HRESULT lockMedia();
1000 void unlockMedia();
1001
1002 HRESULT setMachineState (MachineState_T aMachineState);
1003 HRESULT updateMachineStateOnClient();
1004
1005 SnapshotData mSnapshotData;
1006
1007 /** interprocess semaphore handle for this machine */
1008#if defined (RT_OS_WINDOWS)
1009 HANDLE mIPCSem;
1010 Bstr mIPCSemName;
1011 friend bool Machine::isSessionOpen (ComObjPtr <SessionMachine> &aMachine,
1012 ComPtr <IInternalSessionControl> *aControl,
1013 HANDLE *aIPCSem, bool aAllowClosing);
1014#elif defined (RT_OS_OS2)
1015 HMTX mIPCSem;
1016 Bstr mIPCSemName;
1017 friend bool Machine::isSessionOpen (ComObjPtr <SessionMachine> &aMachine,
1018 ComPtr <IInternalSessionControl> *aControl,
1019 HMTX *aIPCSem, bool aAllowClosing);
1020#elif defined (VBOX_WITH_SYS_V_IPC_SESSION_WATCHER)
1021 int mIPCSem;
1022# ifdef VBOX_WITH_NEW_SYS_V_KEYGEN
1023 Bstr mIPCKey;
1024# endif /*VBOX_WITH_NEW_SYS_V_KEYGEN */
1025#else
1026# error "Port me!"
1027#endif
1028
1029 static DECLCALLBACK(int) taskHandler (RTTHREAD thread, void *pvUser);
1030};
1031
1032// SnapshotMachine class
1033////////////////////////////////////////////////////////////////////////////////
1034
1035/**
1036 * @note Notes on locking objects of this class:
1037 * SnapshotMachine shares some data with the primary Machine instance (pointed
1038 * to by the |mPeer| member). In order to provide data consistency it also
1039 * shares its lock handle. This means that whenever you lock a SessionMachine
1040 * instance using Auto[Reader]Lock or AutoMultiLock, the corresponding Machine
1041 * instance is also locked in the same lock mode. Keep it in mind.
1042 */
1043class ATL_NO_VTABLE SnapshotMachine :
1044 public VirtualBoxSupportTranslation <SnapshotMachine>,
1045 public Machine
1046{
1047public:
1048
1049 VIRTUALBOXSUPPORTTRANSLATION_OVERRIDE(SnapshotMachine)
1050
1051 DECLARE_NOT_AGGREGATABLE(SnapshotMachine)
1052
1053 DECLARE_PROTECT_FINAL_CONSTRUCT()
1054
1055 BEGIN_COM_MAP(SnapshotMachine)
1056 COM_INTERFACE_ENTRY(ISupportErrorInfo)
1057 COM_INTERFACE_ENTRY(IMachine)
1058 END_COM_MAP()
1059
1060 NS_DECL_ISUPPORTS
1061
1062 DECLARE_EMPTY_CTOR_DTOR (SnapshotMachine)
1063
1064 HRESULT FinalConstruct();
1065 void FinalRelease();
1066
1067 // public initializer/uninitializer for internal purposes only
1068 HRESULT init (SessionMachine *aSessionMachine,
1069 IN_GUID aSnapshotId, IN_BSTR aStateFilePath);
1070 HRESULT init (Machine *aMachine,
1071 const settings::Key &aHWNode, const settings::Key &aHDAsNode,
1072 IN_GUID aSnapshotId, IN_BSTR aStateFilePath);
1073 void uninit();
1074
1075 // util::Lockable interface
1076 RWLockHandle *lockHandle() const;
1077
1078 // public methods only for internal purposes
1079
1080 HRESULT onSnapshotChange (Snapshot *aSnapshot);
1081
1082 // unsafe inline public methods for internal purposes only (ensure there is
1083 // a caller and a read lock before calling them!)
1084
1085 const Guid &snapshotId() const { return mSnapshotId; }
1086
1087private:
1088
1089 Guid mSnapshotId;
1090
1091 friend class Snapshot;
1092};
1093
1094// third party methods that depend on SnapshotMachine definiton
1095
1096inline const Guid &Machine::snapshotId() const
1097{
1098 return mType != IsSnapshotMachine ? Guid::Empty :
1099 static_cast <const SnapshotMachine *> (this)->snapshotId();
1100}
1101
1102////////////////////////////////////////////////////////////////////////////////
1103
1104/**
1105 * Returns a pointer to the Machine object for this machine that acts like a
1106 * parent for complex machine data objects such as shared folders, etc.
1107 *
1108 * For primary Machine objects and for SnapshotMachine objects, returns this
1109 * object's pointer itself. For SessoinMachine objects, returns the peer
1110 * (primary) machine pointer.
1111 */
1112inline Machine *Machine::machine()
1113{
1114 if (mType == IsSessionMachine)
1115 return mPeer;
1116 return this;
1117}
1118
1119#endif // ____H_MACHINEIMPL
1120/* vi: set tabstop=4 shiftwidth=4 expandtab: */
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