VirtualBox

source: vbox/trunk/src/VBox/Main/VirtualBoxImpl.cpp@ 12768

Last change on this file since 12768 was 12284, checked in by vboxsync, 16 years ago

added subversion revison to the IDL

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 148.9 KB
Line 
1/* $Id: VirtualBoxImpl.cpp 12284 2008-09-09 10:26:27Z vboxsync $ */
2
3/** @file
4 * Implmentation of IVirtualBox in VBoxSVC.
5 */
6
7/*
8 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
9 *
10 * This file is part of VirtualBox Open Source Edition (OSE), as
11 * available from http://www.virtualbox.org. This file is free software;
12 * you can redistribute it and/or modify it under the terms of the GNU
13 * General Public License (GPL) as published by the Free Software
14 * Foundation, in version 2 as it comes in the "COPYING" file of the
15 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
16 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
17 *
18 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
19 * Clara, CA 95054 USA or visit http://www.sun.com if you need
20 * additional information or have any questions.
21 */
22
23#include "VirtualBoxImpl.h"
24
25#include "Global.h"
26#include "MachineImpl.h"
27#include "HardDiskImpl.h"
28#include "DVDImageImpl.h"
29#include "FloppyImageImpl.h"
30#include "SharedFolderImpl.h"
31#include "ProgressImpl.h"
32#include "HostImpl.h"
33#include "USBControllerImpl.h"
34#include "SystemPropertiesImpl.h"
35#include "GuestOSTypeImpl.h"
36#include "Version.h"
37
38#include "VirtualBoxXMLUtil.h"
39
40#include "Logging.h"
41
42#ifdef RT_OS_WINDOWS
43# include "win/svchlp.h"
44#endif
45
46#include <stdio.h>
47#include <stdlib.h>
48
49#include <iprt/path.h>
50#include <iprt/dir.h>
51#include <iprt/file.h>
52#include <iprt/string.h>
53#include <iprt/uuid.h>
54#include <iprt/thread.h>
55#include <iprt/process.h>
56#include <iprt/env.h>
57#include <iprt/cpputils.h>
58
59#include <VBox/err.h>
60#include <VBox/param.h>
61#include <VBox/VBoxHDD.h>
62#include <VBox/VBoxHDD-new.h>
63#include <VBox/version.h>
64#include <package-generated.h>
65
66#include <VBox/com/com.h>
67#include <VBox/com/array.h>
68
69#include <algorithm>
70#include <set>
71#include <memory> // for auto_ptr
72
73#include <typeinfo>
74
75// defines
76/////////////////////////////////////////////////////////////////////////////
77
78#define VBOX_GLOBAL_SETTINGS_FILE "VirtualBox.xml"
79
80// globals
81/////////////////////////////////////////////////////////////////////////////
82
83static const char gDefaultGlobalConfig [] =
84{
85 "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" RTFILE_LINEFEED
86 "<!-- Sun xVM VirtualBox Global Configuration -->" RTFILE_LINEFEED
87 "<VirtualBox xmlns=\"" VBOX_XML_NAMESPACE "\" "
88 "version=\"" VBOX_XML_VERSION_FULL "\">" RTFILE_LINEFEED
89 " <Global>"RTFILE_LINEFEED
90 " <MachineRegistry/>"RTFILE_LINEFEED
91 " <DiskRegistry/>"RTFILE_LINEFEED
92 " <USBDeviceFilters/>"RTFILE_LINEFEED
93 " <SystemProperties/>"RTFILE_LINEFEED
94 " </Global>"RTFILE_LINEFEED
95 "</VirtualBox>"RTFILE_LINEFEED
96};
97
98// static
99Bstr VirtualBox::sVersion;
100
101// static
102ULONG VirtualBox::sRevision;
103
104// static
105Bstr VirtualBox::sPackageType;
106
107// static
108Bstr VirtualBox::sSettingsFormatVersion;
109
110// constructor / destructor
111/////////////////////////////////////////////////////////////////////////////
112
113VirtualBox::VirtualBox()
114 : mAsyncEventThread (NIL_RTTHREAD)
115 , mAsyncEventQ (NULL)
116{}
117
118VirtualBox::~VirtualBox() {}
119
120HRESULT VirtualBox::FinalConstruct()
121{
122 LogFlowThisFunc (("\n"));
123
124 return init();
125}
126
127void VirtualBox::FinalRelease()
128{
129 LogFlowThisFunc (("\n"));
130
131 uninit();
132}
133
134VirtualBox::Data::Data()
135{
136}
137
138// public initializer/uninitializer for internal purposes only
139/////////////////////////////////////////////////////////////////////////////
140
141/**
142 * Initializes the VirtualBox object.
143 *
144 * @return COM result code
145 */
146HRESULT VirtualBox::init()
147{
148 /* Enclose the state transition NotReady->InInit->Ready */
149 AutoInitSpan autoInitSpan (this);
150 AssertReturn (autoInitSpan.isOk(), E_UNEXPECTED);
151
152 LogFlow (("===========================================================\n"));
153 LogFlowThisFuncEnter();
154
155 if (sVersion.isNull())
156 sVersion = VBOX_VERSION_STRING;
157 sRevision = VBoxSVNRev();
158 if (sPackageType.isNull())
159 sPackageType = VBOX_PACKAGE_STRING;
160 LogFlowThisFunc (("Version: %ls, Package: %ls\n", sVersion.raw(), sPackageType.raw()));
161
162 if (sSettingsFormatVersion.isNull())
163 sSettingsFormatVersion = VBOX_XML_VERSION_FULL;
164 LogFlowThisFunc (("Settings Format Version: %ls\n",
165 sSettingsFormatVersion.raw()));
166
167 /* Get the VirtualBox home directory. */
168 {
169 char homeDir [RTPATH_MAX];
170 int vrc = com::GetVBoxUserHomeDirectory (homeDir, sizeof (homeDir));
171 if (VBOX_FAILURE (vrc))
172 return setError (E_FAIL,
173 tr ("Could not create the VirtualBox home directory '%s'"
174 "(%Vrc)"),
175 homeDir, vrc);
176
177 unconst (mData.mHomeDir) = homeDir;
178 }
179
180 /* compose the global config file name (always full path) */
181 Utf8StrFmt vboxConfigFile ("%s%c%s", mData.mHomeDir.raw(),
182 RTPATH_DELIMITER, VBOX_GLOBAL_SETTINGS_FILE);
183
184 /* store the config file name */
185 unconst (mData.mCfgFile.mName) = vboxConfigFile;
186
187 /* lock the config file */
188 HRESULT rc = lockConfig();
189 if (SUCCEEDED (rc))
190 {
191 if (!isConfigLocked())
192 {
193 /*
194 * This means the config file not found. This is not fatal --
195 * we just create an empty one.
196 */
197 RTFILE handle = NIL_RTFILE;
198 int vrc = RTFileOpen (&handle, vboxConfigFile,
199 RTFILE_O_READWRITE | RTFILE_O_CREATE |
200 RTFILE_O_DENY_WRITE);
201 if (VBOX_SUCCESS (vrc))
202 vrc = RTFileWrite (handle,
203 (void *) gDefaultGlobalConfig,
204 sizeof (gDefaultGlobalConfig), NULL);
205 if (VBOX_FAILURE (vrc))
206 {
207 rc = setError (E_FAIL, tr ("Could not create the default settings file "
208 "'%s' (%Vrc)"),
209 vboxConfigFile.raw(), vrc);
210 }
211 else
212 {
213 mData.mCfgFile.mHandle = handle;
214 /* we do not close the file to simulate lockConfig() */
215 }
216 }
217 }
218
219 if (SUCCEEDED (rc))
220 {
221 try
222 {
223 using namespace settings;
224
225 /* no concurrent file access is possible in init() so open by handle */
226 File file (mData.mCfgFile.mHandle, vboxConfigFile);
227 XmlTreeBackend tree;
228
229 rc = VirtualBox::loadSettingsTree_FirstTime (tree, file,
230 mData.mSettingsFileVersion);
231 CheckComRCThrowRC (rc);
232
233 Key global = tree.rootKey().key ("Global");
234
235#ifdef VBOX_WITH_RESOURCE_USAGE_API
236 /* create the performance collector object BEFORE host */
237 unconst (mData.mPerformanceCollector).createObject();
238 rc = mData.mPerformanceCollector->init();
239 ComAssertComRCThrowRC (rc);
240#endif /* VBOX_WITH_RESOURCE_USAGE_API */
241
242 /* create the host object early, machines will need it */
243 unconst (mData.mHost).createObject();
244 rc = mData.mHost->init (this);
245 ComAssertComRCThrowRC (rc);
246
247 rc = mData.mHost->loadSettings (global);
248 CheckComRCThrowRC (rc);
249
250 /* create the system properties object */
251 unconst (mData.mSystemProperties).createObject();
252 rc = mData.mSystemProperties->init (this);
253 ComAssertComRCThrowRC (rc);
254
255 rc = mData.mSystemProperties->loadSettings (global);
256 CheckComRCThrowRC (rc);
257
258 /* guest OS type objects, needed by machines */
259 for (size_t i = 0; i < RT_ELEMENTS (Global::sOSTypes); ++ i)
260 {
261 ComObjPtr <GuestOSType> guestOSTypeObj;
262 rc = guestOSTypeObj.createObject();
263 if (SUCCEEDED (rc))
264 {
265 rc = guestOSTypeObj->init (Global::sOSTypes [i].id,
266 Global::sOSTypes [i].description,
267 Global::sOSTypes [i].osType,
268 Global::sOSTypes [i].recommendedRAM,
269 Global::sOSTypes [i].recommendedVRAM,
270 Global::sOSTypes [i].recommendedHDD);
271 if (SUCCEEDED (rc))
272 mData.mGuestOSTypes.push_back (guestOSTypeObj);
273 }
274 ComAssertComRCThrowRC (rc);
275 }
276
277 /* hard disks, needed by machines */
278 rc = loadDisks (global);
279 CheckComRCThrowRC (rc);
280
281 /* machines */
282 rc = loadMachines (global);
283 CheckComRCThrowRC (rc);
284
285 /* check hard disk consistency */
286/// @todo (r=dmik) add IVirtualBox::cleanupHardDisks() instead or similar
287// for (HardDiskList::const_iterator it = mData.mHardDisks.begin();
288// it != mData.mHardDisks.end() && SUCCEEDED (rc);
289// ++ it)
290// {
291// rc = (*it)->checkConsistency();
292// }
293// CheckComRCBreakRC ((rc));
294
295 /// @todo (dmik) if successful, check for orphan (unused) diffs
296 // that might be left because of the server crash, and remove
297 // Hmm, is it the same remark as above?..
298 }
299 catch (HRESULT err)
300 {
301 /* we assume that error info is set by the thrower */
302 rc = err;
303 }
304 catch (...)
305 {
306 rc = VirtualBox::handleUnexpectedExceptions (RT_SRC_POS);
307 }
308 }
309
310 if (SUCCEEDED (rc))
311 {
312 /* start the client watcher thread */
313#if defined(RT_OS_WINDOWS)
314 unconst (mWatcherData.mUpdateReq) = ::CreateEvent (NULL, FALSE, FALSE, NULL);
315#elif defined(RT_OS_OS2)
316 RTSemEventCreate (&unconst (mWatcherData.mUpdateReq));
317#elif defined(VBOX_WITH_SYS_V_IPC_SESSION_WATCHER)
318 RTSemEventCreate (&unconst (mWatcherData.mUpdateReq));
319#else
320# error "Port me!"
321#endif
322 int vrc = RTThreadCreate (&unconst (mWatcherData.mThread),
323 ClientWatcher, (void *) this,
324 0, RTTHREADTYPE_MAIN_WORKER,
325 RTTHREADFLAGS_WAITABLE, "Watcher");
326 ComAssertRC (vrc);
327 if (VBOX_FAILURE (vrc))
328 rc = E_FAIL;
329 }
330
331 if (SUCCEEDED (rc)) do
332 {
333 /* start the async event handler thread */
334 int vrc = RTThreadCreate (&unconst (mAsyncEventThread), AsyncEventHandler,
335 &unconst (mAsyncEventQ),
336 0, RTTHREADTYPE_MAIN_WORKER,
337 RTTHREADFLAGS_WAITABLE, "EventHandler");
338 ComAssertRCBreak (vrc, rc = E_FAIL);
339
340 /* wait until the thread sets mAsyncEventQ */
341 RTThreadUserWait (mAsyncEventThread, RT_INDEFINITE_WAIT);
342 ComAssertBreak (mAsyncEventQ, rc = E_FAIL);
343 }
344 while (0);
345
346 /* Confirm a successful initialization when it's the case */
347 if (SUCCEEDED (rc))
348 autoInitSpan.setSucceeded();
349
350 LogFlowThisFunc (("rc=%08X\n", rc));
351 LogFlowThisFuncLeave();
352 LogFlow (("===========================================================\n"));
353 return rc;
354}
355
356void VirtualBox::uninit()
357{
358 /* Enclose the state transition Ready->InUninit->NotReady */
359 AutoUninitSpan autoUninitSpan (this);
360 if (autoUninitSpan.uninitDone())
361 return;
362
363 LogFlow (("===========================================================\n"));
364 LogFlowThisFuncEnter();
365 LogFlowThisFunc (("initFailed()=%d\n", autoUninitSpan.initFailed()));
366
367 /* tell all our child objects we've been uninitialized */
368
369 LogFlowThisFunc (("Uninitializing machines (%d)...\n", mData.mMachines.size()));
370 if (mData.mMachines.size())
371 {
372 MachineList::iterator it = mData.mMachines.begin();
373 while (it != mData.mMachines.end())
374 (*it++)->uninit();
375 mData.mMachines.clear();
376 }
377
378 if (mData.mSystemProperties)
379 {
380 mData.mSystemProperties->uninit();
381 unconst (mData.mSystemProperties).setNull();
382 }
383
384 if (mData.mHost)
385 {
386 mData.mHost->uninit();
387 unconst (mData.mHost).setNull();
388 }
389
390#ifdef VBOX_WITH_RESOURCE_USAGE_API
391 if (mData.mPerformanceCollector)
392 {
393 mData.mPerformanceCollector->uninit();
394 unconst (mData.mPerformanceCollector).setNull();
395 }
396#endif /* VBOX_WITH_RESOURCE_USAGE_API */
397
398 /*
399 * Uninit all other children still referenced by clients
400 * (unregistered machines, hard disks, DVD/floppy images,
401 * server-side progress operations).
402 */
403 uninitDependentChildren();
404
405 mData.mFloppyImages.clear();
406 mData.mDVDImages.clear();
407 mData.mHardDisks.clear();
408
409 mData.mHardDiskMap.clear();
410
411 mData.mProgressOperations.clear();
412
413 mData.mGuestOSTypes.clear();
414
415 /* unlock the config file */
416 unlockConfig();
417
418 LogFlowThisFunc (("Releasing callbacks...\n"));
419 if (mData.mCallbacks.size())
420 {
421 /* release all callbacks */
422 LogWarningFunc (("%d unregistered callbacks!\n",
423 mData.mCallbacks.size()));
424 mData.mCallbacks.clear();
425 }
426
427 LogFlowThisFunc (("Terminating the async event handler...\n"));
428 if (mAsyncEventThread != NIL_RTTHREAD)
429 {
430 /* signal to exit the event loop */
431 if (mAsyncEventQ->postEvent (NULL))
432 {
433 /*
434 * Wait for thread termination (only if we've successfully posted
435 * a NULL event!)
436 */
437 int vrc = RTThreadWait (mAsyncEventThread, 60000, NULL);
438 if (VBOX_FAILURE (vrc))
439 LogWarningFunc (("RTThreadWait(%RTthrd) -> %Vrc\n",
440 mAsyncEventThread, vrc));
441 }
442 else
443 {
444 AssertMsgFailed (("postEvent(NULL) failed\n"));
445 RTThreadWait (mAsyncEventThread, 0, NULL);
446 }
447
448 unconst (mAsyncEventThread) = NIL_RTTHREAD;
449 unconst (mAsyncEventQ) = NULL;
450 }
451
452 LogFlowThisFunc (("Terminating the client watcher...\n"));
453 if (mWatcherData.mThread != NIL_RTTHREAD)
454 {
455 /* signal the client watcher thread */
456 updateClientWatcher();
457 /* wait for the termination */
458 RTThreadWait (mWatcherData.mThread, RT_INDEFINITE_WAIT, NULL);
459 unconst (mWatcherData.mThread) = NIL_RTTHREAD;
460 }
461 mWatcherData.mProcesses.clear();
462#if defined(RT_OS_WINDOWS)
463 if (mWatcherData.mUpdateReq != NULL)
464 {
465 ::CloseHandle (mWatcherData.mUpdateReq);
466 unconst (mWatcherData.mUpdateReq) = NULL;
467 }
468#elif defined(RT_OS_OS2)
469 if (mWatcherData.mUpdateReq != NIL_RTSEMEVENT)
470 {
471 RTSemEventDestroy (mWatcherData.mUpdateReq);
472 unconst (mWatcherData.mUpdateReq) = NIL_RTSEMEVENT;
473 }
474#elif defined(VBOX_WITH_SYS_V_IPC_SESSION_WATCHER)
475 if (mWatcherData.mUpdateReq != NIL_RTSEMEVENT)
476 {
477 RTSemEventDestroy (mWatcherData.mUpdateReq);
478 unconst (mWatcherData.mUpdateReq) = NIL_RTSEMEVENT;
479 }
480#else
481# error "Port me!"
482#endif
483
484 LogFlowThisFuncLeave();
485 LogFlow (("===========================================================\n"));
486}
487
488// IVirtualBox properties
489/////////////////////////////////////////////////////////////////////////////
490
491STDMETHODIMP VirtualBox::COMGETTER(Version) (BSTR *aVersion)
492{
493 if (!aVersion)
494 return E_INVALIDARG;
495
496 AutoCaller autoCaller (this);
497 CheckComRCReturnRC (autoCaller.rc());
498
499 sVersion.cloneTo (aVersion);
500 return S_OK;
501}
502
503STDMETHODIMP VirtualBox::COMGETTER(Revision) (ULONG *aRevision)
504{
505 if (!aRevision)
506 return E_INVALIDARG;
507
508 AutoCaller autoCaller (this);
509 CheckComRCReturnRC (autoCaller.rc());
510
511 *aRevision = sRevision;
512 return S_OK;
513}
514
515STDMETHODIMP VirtualBox::COMGETTER(PackageType) (BSTR *aPackageType)
516{
517 if (!aPackageType)
518 return E_INVALIDARG;
519
520 AutoCaller autoCaller (this);
521 CheckComRCReturnRC (autoCaller.rc());
522
523 sPackageType.cloneTo (aPackageType);
524 return S_OK;
525}
526
527STDMETHODIMP VirtualBox::COMGETTER(HomeFolder) (BSTR *aHomeFolder)
528{
529 if (!aHomeFolder)
530 return E_POINTER;
531
532 AutoCaller autoCaller (this);
533 CheckComRCReturnRC (autoCaller.rc());
534
535 /* mHomeDir is const and doesn't need a lock */
536 mData.mHomeDir.cloneTo (aHomeFolder);
537 return S_OK;
538}
539
540STDMETHODIMP VirtualBox::COMGETTER(SettingsFilePath) (BSTR *aSettingsFilePath)
541{
542 if (!aSettingsFilePath)
543 return E_INVALIDARG;
544
545 AutoCaller autoCaller (this);
546 CheckComRCReturnRC (autoCaller.rc());
547
548 /* mCfgFile.mName is const and doesn't need a lock */
549 mData.mCfgFile.mName.cloneTo (aSettingsFilePath);
550 return S_OK;
551}
552
553STDMETHODIMP VirtualBox::
554COMGETTER(SettingsFileVersion) (BSTR *aSettingsFileVersion)
555{
556 if (!aSettingsFileVersion)
557 return E_INVALIDARG;
558
559 AutoCaller autoCaller (this);
560 CheckComRCReturnRC (autoCaller.rc());
561
562 AutoReadLock alock (this);
563
564 mData.mSettingsFileVersion.cloneTo (aSettingsFileVersion);
565 return S_OK;
566}
567
568STDMETHODIMP VirtualBox::
569COMGETTER(SettingsFormatVersion) (BSTR *aSettingsFormatVersion)
570{
571 if (!aSettingsFormatVersion)
572 return E_INVALIDARG;
573
574 AutoCaller autoCaller (this);
575 CheckComRCReturnRC (autoCaller.rc());
576
577 sSettingsFormatVersion.cloneTo (aSettingsFormatVersion);
578 return S_OK;
579}
580
581STDMETHODIMP VirtualBox::COMGETTER(Host) (IHost **aHost)
582{
583 if (!aHost)
584 return E_POINTER;
585
586 AutoCaller autoCaller (this);
587 CheckComRCReturnRC (autoCaller.rc());
588
589 /* mHost is const, no need to lock */
590 mData.mHost.queryInterfaceTo (aHost);
591 return S_OK;
592}
593
594STDMETHODIMP
595VirtualBox::COMGETTER(SystemProperties) (ISystemProperties **aSystemProperties)
596{
597 if (!aSystemProperties)
598 return E_POINTER;
599
600 AutoCaller autoCaller (this);
601 CheckComRCReturnRC (autoCaller.rc());
602
603 /* mSystemProperties is const, no need to lock */
604 mData.mSystemProperties.queryInterfaceTo (aSystemProperties);
605 return S_OK;
606}
607
608/**
609 * @note Locks this object for reading.
610 */
611STDMETHODIMP VirtualBox::COMGETTER(Machines) (IMachineCollection **aMachines)
612{
613 if (!aMachines)
614 return E_POINTER;
615
616 AutoCaller autoCaller (this);
617 CheckComRCReturnRC (autoCaller.rc());
618
619 ComObjPtr <MachineCollection> collection;
620 collection.createObject();
621
622 AutoReadLock alock (this);
623 collection->init (mData.mMachines);
624 collection.queryInterfaceTo (aMachines);
625
626 return S_OK;
627}
628
629/**
630 * @note Locks this object for reading.
631 */
632STDMETHODIMP
633VirtualBox::COMGETTER(Machines2) (ComSafeArrayOut (IMachine *, aMachines))
634{
635 if (ComSafeArrayOutIsNull (aMachines))
636 return E_POINTER;
637
638 AutoCaller autoCaller (this);
639 CheckComRCReturnRC (autoCaller.rc());
640
641 AutoReadLock alock (this);
642
643 SafeIfaceArray <IMachine> machines (mData.mMachines);
644 machines.detachTo (ComSafeArrayOutArg (aMachines));
645
646 return S_OK;
647}
648
649/**
650 * @note Locks this object for reading.
651 */
652STDMETHODIMP VirtualBox::COMGETTER(HardDisks) (IHardDiskCollection **aHardDisks)
653{
654 if (!aHardDisks)
655 return E_POINTER;
656
657 AutoCaller autoCaller (this);
658 CheckComRCReturnRC (autoCaller.rc());
659
660 ComObjPtr <HardDiskCollection> collection;
661 collection.createObject();
662
663 AutoReadLock alock (this);
664 collection->init (mData.mHardDisks);
665 collection.queryInterfaceTo (aHardDisks);
666
667 return S_OK;
668}
669
670/**
671 * @note Locks this object for reading.
672 */
673STDMETHODIMP VirtualBox::COMGETTER(DVDImages) (IDVDImageCollection **aDVDImages)
674{
675 if (!aDVDImages)
676 return E_POINTER;
677
678 AutoCaller autoCaller (this);
679 CheckComRCReturnRC (autoCaller.rc());
680
681 ComObjPtr <DVDImageCollection> collection;
682 collection.createObject();
683
684 AutoReadLock alock (this);
685 collection->init (mData.mDVDImages);
686 collection.queryInterfaceTo (aDVDImages);
687
688 return S_OK;
689}
690
691/**
692 * @note Locks this object for reading.
693 */
694STDMETHODIMP VirtualBox::COMGETTER(FloppyImages) (IFloppyImageCollection **aFloppyImages)
695{
696 if (!aFloppyImages)
697 return E_POINTER;
698
699 AutoCaller autoCaller (this);
700 CheckComRCReturnRC (autoCaller.rc());
701
702 ComObjPtr <FloppyImageCollection> collection;
703 collection.createObject();
704
705 AutoReadLock alock (this);
706 collection->init (mData.mFloppyImages);
707 collection.queryInterfaceTo (aFloppyImages);
708
709 return S_OK;
710}
711
712/**
713 * @note Locks this object for reading.
714 */
715STDMETHODIMP VirtualBox::COMGETTER(ProgressOperations) (IProgressCollection **aOperations)
716{
717 if (!aOperations)
718 return E_POINTER;
719
720 AutoCaller autoCaller (this);
721 CheckComRCReturnRC (autoCaller.rc());
722
723 ComObjPtr <ProgressCollection> collection;
724 collection.createObject();
725
726 AutoReadLock alock (this);
727 collection->init (mData.mProgressOperations);
728 collection.queryInterfaceTo (aOperations);
729
730 return S_OK;
731}
732
733/**
734 * @note Locks this object for reading.
735 */
736STDMETHODIMP VirtualBox::COMGETTER(GuestOSTypes) (IGuestOSTypeCollection **aGuestOSTypes)
737{
738 if (!aGuestOSTypes)
739 return E_POINTER;
740
741 AutoCaller autoCaller (this);
742 CheckComRCReturnRC (autoCaller.rc());
743
744 ComObjPtr <GuestOSTypeCollection> collection;
745 collection.createObject();
746
747 AutoReadLock alock (this);
748 collection->init (mData.mGuestOSTypes);
749 collection.queryInterfaceTo (aGuestOSTypes);
750
751 return S_OK;
752}
753
754STDMETHODIMP
755VirtualBox::COMGETTER(SharedFolders) (ISharedFolderCollection **aSharedFolders)
756{
757 if (!aSharedFolders)
758 return E_POINTER;
759
760 AutoCaller autoCaller (this);
761 CheckComRCReturnRC (autoCaller.rc());
762
763 return setError (E_NOTIMPL, "Not yet implemented");
764}
765
766STDMETHODIMP
767VirtualBox::COMGETTER(PerformanceCollector) (IPerformanceCollector **aPerformanceCollector)
768{
769#ifdef VBOX_WITH_RESOURCE_USAGE_API
770 if (!aPerformanceCollector)
771 return E_POINTER;
772
773 AutoCaller autoCaller (this);
774 CheckComRCReturnRC (autoCaller.rc());
775
776 /* mPerformanceCollector is const, no need to lock */
777 mData.mPerformanceCollector.queryInterfaceTo (aPerformanceCollector);
778
779 return S_OK;
780#else /* !VBOX_WITH_RESOURCE_USAGE_API */
781 return E_NOTIMPL;
782#endif /* !VBOX_WITH_RESOURCE_USAGE_API */
783}
784
785// IVirtualBox methods
786/////////////////////////////////////////////////////////////////////////////
787
788/** @note Locks mSystemProperties object for reading. */
789STDMETHODIMP VirtualBox::CreateMachine (INPTR BSTR aBaseFolder,
790 INPTR BSTR aName,
791 INPTR GUIDPARAM aId,
792 IMachine **aMachine)
793{
794 LogFlowThisFuncEnter();
795 LogFlowThisFunc (("aBaseFolder='%ls', aName='%ls' aMachine={%p}\n",
796 aBaseFolder, aName, aMachine));
797
798 if (!aName)
799 return E_INVALIDARG;
800 if (!aMachine)
801 return E_POINTER;
802
803 if (!*aName)
804 return setError (E_INVALIDARG,
805 tr ("Machine name cannot be empty"));
806
807 AutoCaller autoCaller (this);
808 CheckComRCReturnRC (autoCaller.rc());
809
810 /* Compose the settings file name using the following scheme:
811 *
812 * <base_folder>/<machine_name>/<machine_name>.xml
813 *
814 * If a non-null and non-empty base folder is specified, the default
815 * machine folder will be used as a base folder.
816 */
817 Bstr settingsFile = aBaseFolder;
818 if (settingsFile.isEmpty())
819 {
820 AutoReadLock propsLock (systemProperties());
821 /* we use the non-full folder value below to keep the path relative */
822 settingsFile = systemProperties()->defaultMachineFolder();
823 }
824 settingsFile = Utf8StrFmt ("%ls%c%ls%c%ls.xml",
825 settingsFile.raw(), RTPATH_DELIMITER,
826 aName, RTPATH_DELIMITER, aName);
827
828 HRESULT rc = E_FAIL;
829
830 /* create a new object */
831 ComObjPtr <Machine> machine;
832 rc = machine.createObject();
833 if (SUCCEEDED (rc))
834 {
835 /* Create UUID if an empty one was specified. */
836 Guid id = aId;
837 if (id.isEmpty())
838 id.create();
839
840 /* initialize the machine object */
841 rc = machine->init (this, settingsFile, Machine::Init_New, aName, TRUE, &id);
842 if (SUCCEEDED (rc))
843 {
844 /* set the return value */
845 rc = machine.queryInterfaceTo (aMachine);
846 ComAssertComRC (rc);
847 }
848 }
849
850 LogFlowThisFunc (("rc=%08X\n", rc));
851 LogFlowThisFuncLeave();
852
853 return rc;
854}
855
856STDMETHODIMP VirtualBox::CreateLegacyMachine (INPTR BSTR aSettingsFile,
857 INPTR BSTR aName,
858 INPTR GUIDPARAM aId,
859 IMachine **aMachine)
860{
861 /* null and empty strings are not allowed as path names */
862 if (!aSettingsFile || !(*aSettingsFile))
863 return E_INVALIDARG;
864
865 if (!aName)
866 return E_INVALIDARG;
867 if (!aMachine)
868 return E_POINTER;
869
870 if (!*aName)
871 return setError (E_INVALIDARG,
872 tr ("Machine name cannot be empty"));
873
874 AutoCaller autoCaller (this);
875 CheckComRCReturnRC (autoCaller.rc());
876
877 HRESULT rc = E_FAIL;
878
879 Utf8Str settingsFile = aSettingsFile;
880 /* append the default extension if none */
881 if (!RTPathHaveExt (settingsFile))
882 settingsFile = Utf8StrFmt ("%s.xml", settingsFile.raw());
883
884 /* create a new object */
885 ComObjPtr<Machine> machine;
886 rc = machine.createObject();
887 if (SUCCEEDED (rc))
888 {
889 /* Create UUID if an empty one was specified. */
890 Guid id = aId;
891 if (id.isEmpty())
892 id.create();
893
894 /* initialize the machine object */
895 rc = machine->init (this, Bstr (settingsFile), Machine::Init_New,
896 aName, FALSE /* aNameSync */, &id);
897 if (SUCCEEDED (rc))
898 {
899 /* set the return value */
900 rc = machine.queryInterfaceTo (aMachine);
901 ComAssertComRC (rc);
902 }
903 }
904 return rc;
905}
906
907STDMETHODIMP VirtualBox::OpenMachine (INPTR BSTR aSettingsFile,
908 IMachine **aMachine)
909{
910 /* null and empty strings are not allowed as path names */
911 if (!aSettingsFile || !(*aSettingsFile))
912 return E_INVALIDARG;
913
914 if (!aMachine)
915 return E_POINTER;
916
917 AutoCaller autoCaller (this);
918 CheckComRCReturnRC (autoCaller.rc());
919
920 HRESULT rc = E_FAIL;
921
922 /* create a new object */
923 ComObjPtr<Machine> machine;
924 rc = machine.createObject();
925 if (SUCCEEDED (rc))
926 {
927 /* initialize the machine object */
928 rc = machine->init (this, aSettingsFile, Machine::Init_Existing);
929 if (SUCCEEDED (rc))
930 {
931 /* set the return value */
932 rc = machine.queryInterfaceTo (aMachine);
933 ComAssertComRC (rc);
934 }
935 }
936
937 return rc;
938}
939
940/** @note Locks objects! */
941STDMETHODIMP VirtualBox::RegisterMachine (IMachine *aMachine)
942{
943 if (!aMachine)
944 return E_INVALIDARG;
945
946 AutoCaller autoCaller (this);
947 CheckComRCReturnRC (autoCaller.rc());
948
949 HRESULT rc;
950
951 Bstr name;
952 rc = aMachine->COMGETTER(Name) (name.asOutParam());
953 CheckComRCReturnRC (rc);
954
955 /*
956 * we can safely cast child to Machine * here because only Machine
957 * implementations of IMachine can be among our children
958 */
959 Machine *machine = static_cast <Machine *> (getDependentChild (aMachine));
960 if (!machine)
961 {
962 /*
963 * this machine was not created by CreateMachine()
964 * or opened by OpenMachine() or loaded during startup
965 */
966 return setError (E_FAIL,
967 tr ("The machine named '%ls' is not created within this "
968 "VirtualBox instance"), name.raw());
969 }
970
971 AutoCaller machCaller (machine);
972 ComAssertComRCRetRC (machCaller.rc());
973
974 rc = registerMachine (machine);
975
976 /* fire an event */
977 if (SUCCEEDED (rc))
978 onMachineRegistered (machine->uuid(), TRUE);
979
980 return rc;
981}
982
983/** @note Locks objects! */
984STDMETHODIMP VirtualBox::GetMachine (INPTR GUIDPARAM aId, IMachine **aMachine)
985{
986 if (!aMachine)
987 return E_POINTER;
988
989 AutoCaller autoCaller (this);
990 CheckComRCReturnRC (autoCaller.rc());
991
992 ComObjPtr <Machine> machine;
993 HRESULT rc = findMachine (Guid (aId), true /* setError */, &machine);
994
995 /* the below will set *aMachine to NULL if machine is null */
996 machine.queryInterfaceTo (aMachine);
997
998 return rc;
999}
1000
1001/** @note Locks this object for reading, then some machine objects for reading. */
1002STDMETHODIMP VirtualBox::FindMachine (INPTR BSTR aName, IMachine **aMachine)
1003{
1004 LogFlowThisFuncEnter();
1005 LogFlowThisFunc (("aName=\"%ls\", aMachine={%p}\n", aName, aMachine));
1006
1007 if (!aName)
1008 return E_INVALIDARG;
1009 if (!aMachine)
1010 return E_POINTER;
1011
1012 AutoCaller autoCaller (this);
1013 CheckComRCReturnRC (autoCaller.rc());
1014
1015 /* start with not found */
1016 ComObjPtr <Machine> machine;
1017 MachineList machines;
1018 {
1019 /* take a copy for safe iteration outside the lock */
1020 AutoReadLock alock (this);
1021 machines = mData.mMachines;
1022 }
1023
1024 for (MachineList::iterator it = machines.begin();
1025 !machine && it != machines.end();
1026 ++ it)
1027 {
1028 AutoLimitedCaller machCaller (*it);
1029 AssertComRC (machCaller.rc());
1030
1031 /* skip inaccessible machines */
1032 if (machCaller.state() == Machine::Ready)
1033 {
1034 AutoReadLock machLock (*it);
1035 if ((*it)->name() == aName)
1036 machine = *it;
1037 }
1038 }
1039
1040 /* this will set (*machine) to NULL if machineObj is null */
1041 machine.queryInterfaceTo (aMachine);
1042
1043 HRESULT rc = machine
1044 ? S_OK
1045 : setError (E_INVALIDARG,
1046 tr ("Could not find a registered machine named '%ls'"), aName);
1047
1048 LogFlowThisFunc (("rc=%08X\n", rc));
1049 LogFlowThisFuncLeave();
1050
1051 return rc;
1052}
1053
1054/** @note Locks objects! */
1055STDMETHODIMP VirtualBox::UnregisterMachine (INPTR GUIDPARAM aId,
1056 IMachine **aMachine)
1057{
1058 Guid id = aId;
1059 if (id.isEmpty())
1060 return E_INVALIDARG;
1061
1062 AutoCaller autoCaller (this);
1063 CheckComRCReturnRC (autoCaller.rc());
1064
1065 AutoWriteLock alock (this);
1066
1067 ComObjPtr <Machine> machine;
1068
1069 HRESULT rc = findMachine (id, true /* setError */, &machine);
1070 CheckComRCReturnRC (rc);
1071
1072 rc = machine->trySetRegistered (FALSE);
1073 CheckComRCReturnRC (rc);
1074
1075 /* remove from the collection of registered machines */
1076 mData.mMachines.remove (machine);
1077
1078 /* save the global registry */
1079 rc = saveSettings();
1080
1081 /* return the unregistered machine to the caller */
1082 machine.queryInterfaceTo (aMachine);
1083
1084 /* fire an event */
1085 onMachineRegistered (id, FALSE);
1086
1087 return rc;
1088}
1089
1090STDMETHODIMP VirtualBox::CreateHardDisk (HardDiskStorageType_T aStorageType,
1091 IHardDisk **aHardDisk)
1092{
1093 if (!aHardDisk)
1094 return E_POINTER;
1095
1096 AutoCaller autoCaller (this);
1097 CheckComRCReturnRC (autoCaller.rc());
1098
1099 HRESULT rc = E_FAIL;
1100
1101 ComObjPtr <HardDisk> hardDisk;
1102
1103 switch (aStorageType)
1104 {
1105 case HardDiskStorageType_VirtualDiskImage:
1106 {
1107 ComObjPtr <HVirtualDiskImage> vdi;
1108 vdi.createObject();
1109 rc = vdi->init (this, NULL, NULL);
1110 hardDisk = vdi;
1111 break;
1112 }
1113
1114 case HardDiskStorageType_ISCSIHardDisk:
1115 {
1116 ComObjPtr <HISCSIHardDisk> iscsi;
1117 iscsi.createObject();
1118 rc = iscsi->init (this);
1119 hardDisk = iscsi;
1120 break;
1121 }
1122
1123 case HardDiskStorageType_VMDKImage:
1124 {
1125 ComObjPtr <HVMDKImage> vmdk;
1126 vmdk.createObject();
1127 rc = vmdk->init (this, NULL, NULL);
1128 hardDisk = vmdk;
1129 break;
1130 }
1131 case HardDiskStorageType_CustomHardDisk:
1132 {
1133 ComObjPtr <HCustomHardDisk> custom;
1134 custom.createObject();
1135 rc = custom->init (this, NULL, NULL);
1136 hardDisk = custom;
1137 break;
1138 }
1139 case HardDiskStorageType_VHDImage:
1140 {
1141 ComObjPtr <HVHDImage> vhd;
1142 vhd.createObject();
1143 rc = vhd->init (this, NULL, NULL);
1144 hardDisk = vhd;
1145 break;
1146 }
1147 default:
1148 AssertFailed();
1149 };
1150
1151 if (SUCCEEDED (rc))
1152 hardDisk.queryInterfaceTo (aHardDisk);
1153
1154 return rc;
1155}
1156
1157/** @note Locks mSystemProperties object for reading. */
1158STDMETHODIMP VirtualBox::OpenHardDisk (INPTR BSTR aLocation, IHardDisk **aHardDisk)
1159{
1160 /* null and empty strings are not allowed locations */
1161 if (!aLocation || !(*aLocation))
1162 return E_INVALIDARG;
1163
1164 if (!aHardDisk)
1165 return E_POINTER;
1166
1167 AutoCaller autoCaller (this);
1168 CheckComRCReturnRC (autoCaller.rc());
1169
1170 /* Currently, the location is always a path. So, append the
1171 * default path if only a name is given. */
1172 Bstr location = aLocation;
1173 {
1174 Utf8Str loc = aLocation;
1175 if (!RTPathHavePath (loc))
1176 {
1177 AutoWriteLock propsLock (mData.mSystemProperties);
1178 location = Utf8StrFmt ("%ls%c%s",
1179 mData.mSystemProperties->defaultVDIFolder().raw(),
1180 RTPATH_DELIMITER,
1181 loc.raw());
1182 }
1183 }
1184
1185 ComObjPtr <HardDisk> hardDisk;
1186 HRESULT rc = HardDisk::openHardDisk (this, location, hardDisk);
1187 if (SUCCEEDED (rc))
1188 hardDisk.queryInterfaceTo (aHardDisk);
1189
1190 return rc;
1191}
1192
1193/** @note Locks mSystemProperties object for reading. */
1194STDMETHODIMP VirtualBox::OpenVirtualDiskImage (INPTR BSTR aFilePath,
1195 IVirtualDiskImage **aImage)
1196{
1197 /* null and empty strings are not allowed as path names here */
1198 if (!aFilePath || !(*aFilePath))
1199 return E_INVALIDARG;
1200
1201 if (!aImage)
1202 return E_POINTER;
1203
1204 AutoCaller autoCaller (this);
1205 CheckComRCReturnRC (autoCaller.rc());
1206
1207 /* append the default path if only a name is given */
1208 Bstr path = aFilePath;
1209 {
1210 Utf8Str fp = aFilePath;
1211 if (!RTPathHavePath (fp))
1212 {
1213 AutoWriteLock propsLock (mData.mSystemProperties);
1214 path = Utf8StrFmt ("%ls%c%s",
1215 mData.mSystemProperties->defaultVDIFolder().raw(),
1216 RTPATH_DELIMITER,
1217 fp.raw());
1218 }
1219 }
1220
1221 ComObjPtr <HVirtualDiskImage> vdi;
1222 vdi.createObject();
1223 HRESULT rc = vdi->init (this, NULL, path);
1224
1225 if (SUCCEEDED (rc))
1226 vdi.queryInterfaceTo (aImage);
1227
1228 return rc;
1229}
1230
1231/** @note Locks objects! */
1232STDMETHODIMP VirtualBox::RegisterHardDisk (IHardDisk *aHardDisk)
1233{
1234 if (!aHardDisk)
1235 return E_POINTER;
1236
1237 AutoCaller autoCaller (this);
1238 CheckComRCReturnRC (autoCaller.rc());
1239
1240 VirtualBoxBase *child = getDependentChild (aHardDisk);
1241 if (!child)
1242 return setError (E_FAIL, tr ("The given hard disk is not created within "
1243 "this VirtualBox instance"));
1244
1245 /*
1246 * we can safely cast child to HardDisk * here because only HardDisk
1247 * implementations of IHardDisk can be among our children
1248 */
1249
1250 return registerHardDisk (static_cast <HardDisk *> (child), RHD_External);
1251}
1252
1253/** @note Locks objects! */
1254STDMETHODIMP VirtualBox::GetHardDisk (INPTR GUIDPARAM aId, IHardDisk **aHardDisk)
1255{
1256 if (!aHardDisk)
1257 return E_POINTER;
1258
1259 AutoCaller autoCaller (this);
1260 CheckComRCReturnRC (autoCaller.rc());
1261
1262 Guid id = aId;
1263 ComObjPtr <HardDisk> hd;
1264 HRESULT rc = findHardDisk (&id, NULL, true /* setError */, &hd);
1265
1266 /* the below will set *aHardDisk to NULL if hd is null */
1267 hd.queryInterfaceTo (aHardDisk);
1268
1269 return rc;
1270}
1271
1272/** @note Locks objects! */
1273STDMETHODIMP VirtualBox::FindHardDisk (INPTR BSTR aLocation,
1274 IHardDisk **aHardDisk)
1275{
1276 if (!aLocation)
1277 return E_INVALIDARG;
1278 if (!aHardDisk)
1279 return E_POINTER;
1280
1281 AutoCaller autoCaller (this);
1282 CheckComRCReturnRC (autoCaller.rc());
1283
1284 Utf8Str location = aLocation;
1285 if (strncmp (location, "iscsi:", 6) == 0)
1286 {
1287 /* nothing special */
1288 }
1289 else
1290 {
1291 /* For locations represented by file paths, append the default path if
1292 * only a name is given, and then get the full path. */
1293 if (!RTPathHavePath (location))
1294 {
1295 AutoWriteLock propsLock (mData.mSystemProperties);
1296 location = Utf8StrFmt ("%ls%c%s",
1297 mData.mSystemProperties->defaultVDIFolder().raw(),
1298 RTPATH_DELIMITER,
1299 location.raw());
1300 }
1301
1302 /* get the full file name */
1303 char buf [RTPATH_MAX];
1304 int vrc = RTPathAbsEx (mData.mHomeDir, location, buf, sizeof (buf));
1305 if (VBOX_FAILURE (vrc))
1306 return setError (E_FAIL, tr ("Invalid hard disk location '%ls' (%Vrc)"),
1307 aLocation, vrc);
1308 location = buf;
1309 }
1310
1311 ComObjPtr <HardDisk> hardDisk;
1312 HRESULT rc = findHardDisk (NULL, Bstr (location), true /* setError */,
1313 &hardDisk);
1314
1315 /* the below will set *aHardDisk to NULL if hardDisk is null */
1316 hardDisk.queryInterfaceTo (aHardDisk);
1317
1318 return rc;
1319}
1320
1321/** @note Locks objects! */
1322STDMETHODIMP VirtualBox::FindVirtualDiskImage (INPTR BSTR aFilePath,
1323 IVirtualDiskImage **aImage)
1324{
1325 if (!aFilePath)
1326 return E_INVALIDARG;
1327 if (!aImage)
1328 return E_POINTER;
1329
1330 AutoCaller autoCaller (this);
1331 CheckComRCReturnRC (autoCaller.rc());
1332
1333 /* append the default path if only a name is given */
1334 Utf8Str path = aFilePath;
1335 {
1336 Utf8Str fp = path;
1337 if (!RTPathHavePath (fp))
1338 {
1339 AutoWriteLock propsLock (mData.mSystemProperties);
1340 path = Utf8StrFmt ("%ls%c%s",
1341 mData.mSystemProperties->defaultVDIFolder().raw(),
1342 RTPATH_DELIMITER,
1343 fp.raw());
1344 }
1345 }
1346
1347 /* get the full file name */
1348 char buf [RTPATH_MAX];
1349 int vrc = RTPathAbsEx (mData.mHomeDir, path, buf, sizeof (buf));
1350 if (VBOX_FAILURE (vrc))
1351 return setError (E_FAIL, tr ("Invalid image file path '%ls' (%Vrc)"),
1352 aFilePath, vrc);
1353
1354 ComObjPtr <HVirtualDiskImage> vdi;
1355 HRESULT rc = findVirtualDiskImage (NULL, Bstr (buf), true /* setError */,
1356 &vdi);
1357
1358 /* the below will set *aImage to NULL if vdi is null */
1359 vdi.queryInterfaceTo (aImage);
1360
1361 return rc;
1362}
1363
1364/** @note Locks objects! */
1365STDMETHODIMP VirtualBox::UnregisterHardDisk (INPTR GUIDPARAM aId, IHardDisk **aHardDisk)
1366{
1367 if (!aHardDisk)
1368 return E_POINTER;
1369
1370 AutoCaller autoCaller (this);
1371 CheckComRCReturnRC (autoCaller.rc());
1372
1373 *aHardDisk = NULL;
1374
1375 Guid id = aId;
1376 ComObjPtr <HardDisk> hd;
1377 HRESULT rc = findHardDisk (&id, NULL, true /* setError */, &hd);
1378 CheckComRCReturnRC (rc);
1379
1380 rc = unregisterHardDisk (hd);
1381 if (SUCCEEDED (rc))
1382 hd.queryInterfaceTo (aHardDisk);
1383
1384 return rc;
1385}
1386
1387/** @note Doesn't lock anything. */
1388STDMETHODIMP VirtualBox::OpenDVDImage (INPTR BSTR aFilePath, INPTR GUIDPARAM aId,
1389 IDVDImage **aDVDImage)
1390{
1391 /* null and empty strings are not allowed as path names */
1392 if (!aFilePath || !(*aFilePath))
1393 return E_INVALIDARG;
1394
1395 if (!aDVDImage)
1396 return E_POINTER;
1397
1398 AutoCaller autoCaller (this);
1399 CheckComRCReturnRC (autoCaller.rc());
1400
1401 HRESULT rc = E_FAIL;
1402
1403 Guid uuid = aId;
1404 /* generate an UUID if not specified */
1405 if (uuid.isEmpty())
1406 uuid.create();
1407
1408 ComObjPtr <DVDImage> dvdImage;
1409 dvdImage.createObject();
1410 rc = dvdImage->init (this, aFilePath, FALSE /* !isRegistered */, uuid);
1411 if (SUCCEEDED (rc))
1412 dvdImage.queryInterfaceTo (aDVDImage);
1413
1414 return rc;
1415}
1416
1417/** @note Locks objects! */
1418STDMETHODIMP VirtualBox::RegisterDVDImage (IDVDImage *aDVDImage)
1419{
1420 if (!aDVDImage)
1421 return E_POINTER;
1422
1423 AutoCaller autoCaller (this);
1424 CheckComRCReturnRC (autoCaller.rc());
1425
1426 VirtualBoxBase *child = getDependentChild (aDVDImage);
1427 if (!child)
1428 return setError (E_FAIL, tr ("The given CD/DVD image is not created within "
1429 "this VirtualBox instance"));
1430
1431 /*
1432 * we can safely cast child to DVDImage * here because only DVDImage
1433 * implementations of IDVDImage can be among our children
1434 */
1435
1436 return registerDVDImage (static_cast <DVDImage *> (child),
1437 FALSE /* aOnStartUp */);
1438}
1439
1440/** @note Locks objects! */
1441STDMETHODIMP VirtualBox::GetDVDImage (INPTR GUIDPARAM aId, IDVDImage **aDVDImage)
1442{
1443 if (!aDVDImage)
1444 return E_POINTER;
1445
1446 AutoCaller autoCaller (this);
1447 CheckComRCReturnRC (autoCaller.rc());
1448
1449 Guid uuid = aId;
1450 ComObjPtr <DVDImage> dvd;
1451 HRESULT rc = findDVDImage (&uuid, NULL, true /* setError */, &dvd);
1452
1453 /* the below will set *aDVDImage to NULL if dvd is null */
1454 dvd.queryInterfaceTo (aDVDImage);
1455
1456 return rc;
1457}
1458
1459/** @note Locks objects! */
1460STDMETHODIMP VirtualBox::FindDVDImage (INPTR BSTR aFilePath, IDVDImage **aDVDImage)
1461{
1462 if (!aFilePath)
1463 return E_INVALIDARG;
1464 if (!aDVDImage)
1465 return E_POINTER;
1466
1467 AutoCaller autoCaller (this);
1468 CheckComRCReturnRC (autoCaller.rc());
1469
1470 /* get the full file name */
1471 char buf [RTPATH_MAX];
1472 int vrc = RTPathAbsEx (mData.mHomeDir, Utf8Str (aFilePath), buf, sizeof (buf));
1473 if (VBOX_FAILURE (vrc))
1474 return setError (E_FAIL, tr ("Invalid image file path: '%ls' (%Vrc)"),
1475 aFilePath, vrc);
1476
1477 ComObjPtr <DVDImage> dvd;
1478 HRESULT rc = findDVDImage (NULL, Bstr (buf), true /* setError */, &dvd);
1479
1480 /* the below will set *dvdImage to NULL if dvd is null */
1481 dvd.queryInterfaceTo (aDVDImage);
1482
1483 return rc;
1484}
1485
1486/** @note Locks objects! */
1487STDMETHODIMP VirtualBox::GetDVDImageUsage (INPTR GUIDPARAM aId,
1488 ResourceUsage_T aUsage,
1489 BSTR *aMachineIDs)
1490{
1491 if (!aMachineIDs)
1492 return E_POINTER;
1493 if (aUsage == ResourceUsage_Null)
1494 return E_INVALIDARG;
1495
1496 AutoCaller autoCaller (this);
1497 CheckComRCReturnRC (autoCaller.rc());
1498
1499 AutoReadLock alock (this);
1500
1501 Guid uuid = Guid (aId);
1502 HRESULT rc = findDVDImage (&uuid, NULL, true /* setError */, NULL);
1503 if (FAILED (rc))
1504 return rc;
1505
1506 Bstr ids;
1507 getDVDImageUsage (uuid, aUsage, &ids);
1508 ids.cloneTo (aMachineIDs);
1509
1510 return S_OK;
1511}
1512
1513/** @note Locks objects! */
1514STDMETHODIMP VirtualBox::UnregisterDVDImage (INPTR GUIDPARAM aId,
1515 IDVDImage **aDVDImage)
1516{
1517 if (!aDVDImage)
1518 return E_POINTER;
1519
1520 AutoCaller autoCaller (this);
1521 CheckComRCReturnRC (autoCaller.rc());
1522
1523 AutoWriteLock alock (this);
1524
1525 *aDVDImage = NULL;
1526
1527 Guid uuid = aId;
1528 ComObjPtr <DVDImage> dvd;
1529 HRESULT rc = findDVDImage (&uuid, NULL, true /* setError */, &dvd);
1530 CheckComRCReturnRC (rc);
1531
1532 if (!getDVDImageUsage (aId, ResourceUsage_All))
1533 {
1534 /* remove from the collection */
1535 mData.mDVDImages.remove (dvd);
1536
1537 /* save the global config file */
1538 rc = saveSettings();
1539
1540 if (SUCCEEDED (rc))
1541 {
1542 rc = dvd.queryInterfaceTo (aDVDImage);
1543 ComAssertComRC (rc);
1544 }
1545 }
1546 else
1547 rc = setError(E_FAIL,
1548 tr ("The CD/DVD image with the UUID {%s} is currently in use"),
1549 uuid.toString().raw());
1550
1551 return rc;
1552}
1553
1554/** @note Doesn't lock anything. */
1555STDMETHODIMP VirtualBox::OpenFloppyImage (INPTR BSTR aFilePath, INPTR GUIDPARAM aId,
1556 IFloppyImage **aFloppyImage)
1557{
1558 /* null and empty strings are not allowed as path names */
1559 if (!aFilePath || !(*aFilePath))
1560 return E_INVALIDARG;
1561
1562 if (!aFloppyImage)
1563 return E_POINTER;
1564
1565 AutoCaller autoCaller (this);
1566 CheckComRCReturnRC (autoCaller.rc());
1567
1568 HRESULT rc = E_FAIL;
1569
1570 Guid uuid = aId;
1571 /* generate an UUID if not specified */
1572 if (Guid::isEmpty (aId))
1573 uuid.create();
1574
1575 ComObjPtr <FloppyImage> floppyImage;
1576 floppyImage.createObject();
1577 rc = floppyImage->init (this, aFilePath, FALSE /* !isRegistered */, uuid);
1578 if (SUCCEEDED (rc))
1579 floppyImage.queryInterfaceTo (aFloppyImage);
1580
1581 return rc;
1582}
1583
1584/** @note Locks objects! */
1585STDMETHODIMP VirtualBox::RegisterFloppyImage (IFloppyImage *aFloppyImage)
1586{
1587 if (!aFloppyImage)
1588 return E_POINTER;
1589
1590 AutoCaller autoCaller (this);
1591 CheckComRCReturnRC (autoCaller.rc());
1592
1593 VirtualBoxBase *child = getDependentChild (aFloppyImage);
1594 if (!child)
1595 return setError (E_FAIL, tr ("The given floppy image is not created within "
1596 "this VirtualBox instance"));
1597
1598 /*
1599 * we can safely cast child to FloppyImage * here because only FloppyImage
1600 * implementations of IFloppyImage can be among our children
1601 */
1602
1603 return registerFloppyImage (static_cast <FloppyImage *> (child),
1604 FALSE /* aOnStartUp */);
1605}
1606
1607/** @note Locks objects! */
1608STDMETHODIMP VirtualBox::GetFloppyImage (INPTR GUIDPARAM aId,
1609 IFloppyImage **aFloppyImage)
1610{
1611 if (!aFloppyImage)
1612 return E_POINTER;
1613
1614 AutoCaller autoCaller (this);
1615 CheckComRCReturnRC (autoCaller.rc());
1616
1617 Guid uuid = aId;
1618 ComObjPtr <FloppyImage> floppy;
1619 HRESULT rc = findFloppyImage (&uuid, NULL, true /* setError */, &floppy);
1620
1621 /* the below will set *aFloppyImage to NULL if dvd is null */
1622 floppy.queryInterfaceTo (aFloppyImage);
1623
1624 return rc;
1625}
1626
1627/** @note Locks objects! */
1628STDMETHODIMP VirtualBox::FindFloppyImage (INPTR BSTR aFilePath,
1629 IFloppyImage **aFloppyImage)
1630{
1631 if (!aFilePath)
1632 return E_INVALIDARG;
1633 if (!aFloppyImage)
1634 return E_POINTER;
1635
1636 AutoCaller autoCaller (this);
1637 CheckComRCReturnRC (autoCaller.rc());
1638
1639 /* get the full file name */
1640 char buf [RTPATH_MAX];
1641 int vrc = RTPathAbsEx (mData.mHomeDir, Utf8Str (aFilePath), buf, sizeof (buf));
1642 if (VBOX_FAILURE (vrc))
1643 return setError (E_FAIL, tr ("Invalid image file path: '%ls' (%Vrc)"),
1644 aFilePath, vrc);
1645
1646 ComObjPtr <FloppyImage> floppy;
1647 HRESULT rc = findFloppyImage (NULL, Bstr (buf), true /* setError */, &floppy);
1648
1649 /* the below will set *image to NULL if img is null */
1650 floppy.queryInterfaceTo (aFloppyImage);
1651
1652 return rc;
1653}
1654
1655/** @note Locks objects! */
1656STDMETHODIMP VirtualBox::GetFloppyImageUsage (INPTR GUIDPARAM aId,
1657 ResourceUsage_T aUsage,
1658 BSTR *aMachineIDs)
1659{
1660 if (!aMachineIDs)
1661 return E_POINTER;
1662 if (aUsage == ResourceUsage_Null)
1663 return E_INVALIDARG;
1664
1665 AutoCaller autoCaller (this);
1666 CheckComRCReturnRC (autoCaller.rc());
1667
1668 AutoReadLock alock (this);
1669
1670 Guid uuid = Guid (aId);
1671 HRESULT rc = findFloppyImage (&uuid, NULL, true /* setError */, NULL);
1672 if (FAILED (rc))
1673 return rc;
1674
1675 Bstr ids;
1676 getFloppyImageUsage (uuid, aUsage, &ids);
1677 ids.cloneTo (aMachineIDs);
1678
1679 return S_OK;
1680}
1681
1682/** @note Locks objects! */
1683STDMETHODIMP VirtualBox::UnregisterFloppyImage (INPTR GUIDPARAM aId,
1684 IFloppyImage **aFloppyImage)
1685{
1686 if (!aFloppyImage)
1687 return E_INVALIDARG;
1688
1689 AutoCaller autoCaller (this);
1690 CheckComRCReturnRC (autoCaller.rc());
1691
1692 AutoWriteLock alock (this);
1693
1694 *aFloppyImage = NULL;
1695
1696 Guid uuid = aId;
1697 ComObjPtr <FloppyImage> floppy;
1698 HRESULT rc = findFloppyImage (&uuid, NULL, true /* setError */, &floppy);
1699 CheckComRCReturnRC (rc);
1700
1701 if (!getFloppyImageUsage (aId, ResourceUsage_All))
1702 {
1703 /* remove from the collection */
1704 mData.mFloppyImages.remove (floppy);
1705
1706 /* save the global config file */
1707 rc = saveSettings();
1708 if (SUCCEEDED (rc))
1709 {
1710 rc = floppy.queryInterfaceTo (aFloppyImage);
1711 ComAssertComRC (rc);
1712 }
1713 }
1714 else
1715 rc = setError(E_FAIL,
1716 tr ("A floppy image with UUID {%s} is currently in use"),
1717 uuid.toString().raw());
1718
1719 return rc;
1720}
1721
1722/** @note Locks this object for reading. */
1723STDMETHODIMP VirtualBox::GetGuestOSType (INPTR BSTR aId, IGuestOSType **aType)
1724{
1725 if (!aType)
1726 return E_INVALIDARG;
1727
1728 AutoCaller autoCaller (this);
1729 CheckComRCReturnRC (autoCaller.rc());
1730
1731 *aType = NULL;
1732
1733 AutoReadLock alock (this);
1734
1735 for (GuestOSTypeList::iterator it = mData.mGuestOSTypes.begin();
1736 it != mData.mGuestOSTypes.end();
1737 ++ it)
1738 {
1739 const Bstr &typeId = (*it)->id();
1740 AssertMsg (!!typeId, ("ID must not be NULL"));
1741 if (typeId == aId)
1742 {
1743 (*it).queryInterfaceTo (aType);
1744 break;
1745 }
1746 }
1747
1748 return (*aType) ? S_OK :
1749 setError (E_INVALIDARG,
1750 tr ("'%ls' is not a valid Guest OS type"),
1751 aId);
1752}
1753
1754STDMETHODIMP
1755VirtualBox::CreateSharedFolder (INPTR BSTR aName, INPTR BSTR aHostPath, BOOL aWritable)
1756{
1757 if (!aName || !aHostPath)
1758 return E_INVALIDARG;
1759
1760 AutoCaller autoCaller (this);
1761 CheckComRCReturnRC (autoCaller.rc());
1762
1763 return setError (E_NOTIMPL, "Not yet implemented");
1764}
1765
1766STDMETHODIMP VirtualBox::RemoveSharedFolder (INPTR BSTR aName)
1767{
1768 if (!aName)
1769 return E_INVALIDARG;
1770
1771 AutoCaller autoCaller (this);
1772 CheckComRCReturnRC (autoCaller.rc());
1773
1774 return setError (E_NOTIMPL, "Not yet implemented");
1775}
1776
1777/**
1778 * @note Locks this object for reading.
1779 */
1780STDMETHODIMP VirtualBox::
1781GetNextExtraDataKey (INPTR BSTR aKey, BSTR *aNextKey, BSTR *aNextValue)
1782{
1783 if (!aNextKey)
1784 return E_POINTER;
1785
1786 AutoCaller autoCaller (this);
1787 CheckComRCReturnRC (autoCaller.rc());
1788
1789 /* start with nothing found */
1790 *aNextKey = NULL;
1791 if (aNextValue)
1792 *aNextValue = NULL;
1793
1794 HRESULT rc = S_OK;
1795
1796 /* serialize file access (prevent writes) */
1797 AutoReadLock alock (this);
1798
1799 try
1800 {
1801 using namespace settings;
1802
1803 /* load the settings file (we don't reuse the existing handle but
1804 * request a new one to allow for concurrent multithreaded reads) */
1805 File file (File::Mode_Read, Utf8Str (mData.mCfgFile.mName));
1806 XmlTreeBackend tree;
1807
1808 rc = VirtualBox::loadSettingsTree_Again (tree, file);
1809 CheckComRCReturnRC (rc);
1810
1811 Key globalNode = tree.rootKey().key ("Global");
1812 Key extraDataNode = globalNode.findKey ("ExtraData");
1813
1814 if (!extraDataNode.isNull())
1815 {
1816 Key::List items = extraDataNode.keys ("ExtraDataItem");
1817 if (items.size())
1818 {
1819 for (Key::List::const_iterator it = items.begin();
1820 it != items.end(); ++ it)
1821 {
1822 Bstr key = (*it).stringValue ("name");
1823
1824 /* if we're supposed to return the first one */
1825 if (aKey == NULL)
1826 {
1827 key.cloneTo (aNextKey);
1828 if (aNextValue)
1829 {
1830 Bstr val = (*it).stringValue ("value");
1831 val.cloneTo (aNextValue);
1832 }
1833 return S_OK;
1834 }
1835
1836 /* did we find the key we're looking for? */
1837 if (key == aKey)
1838 {
1839 ++ it;
1840 /* is there another item? */
1841 if (it != items.end())
1842 {
1843 Bstr key = (*it).stringValue ("name");
1844 key.cloneTo (aNextKey);
1845 if (aNextValue)
1846 {
1847 Bstr val = (*it).stringValue ("value");
1848 val.cloneTo (aNextValue);
1849 }
1850 }
1851 /* else it's the last one, arguments are already NULL */
1852 return S_OK;
1853 }
1854 }
1855 }
1856 }
1857
1858 /* Here we are when a) there are no items at all or b) there are items
1859 * but none of them equals to the requested non-NULL key. b) is an
1860 * error as well as a) if the key is non-NULL. When the key is NULL
1861 * (which is the case only when there are no items), we just fall
1862 * through to return NULLs and S_OK. */
1863
1864 if (aKey != NULL)
1865 return setError (E_FAIL,
1866 tr ("Could not find the extra data key '%ls'"), aKey);
1867 }
1868 catch (...)
1869 {
1870 rc = VirtualBox::handleUnexpectedExceptions (RT_SRC_POS);
1871 }
1872
1873 return rc;
1874}
1875
1876/**
1877 * @note Locks this object for reading.
1878 */
1879STDMETHODIMP VirtualBox::GetExtraData (INPTR BSTR aKey, BSTR *aValue)
1880{
1881 if (!aKey)
1882 return E_INVALIDARG;
1883 if (!aValue)
1884 return E_POINTER;
1885
1886 AutoCaller autoCaller (this);
1887 CheckComRCReturnRC (autoCaller.rc());
1888
1889 /* start with nothing found */
1890 *aValue = NULL;
1891
1892 HRESULT rc = S_OK;
1893
1894 /* serialize file access (prevent writes) */
1895 AutoReadLock alock (this);
1896
1897 try
1898 {
1899 using namespace settings;
1900
1901 /* load the settings file (we don't reuse the existing handle but
1902 * request a new one to allow for concurrent multithreaded reads) */
1903 File file (File::Mode_Read, Utf8Str (mData.mCfgFile.mName));
1904 XmlTreeBackend tree;
1905
1906 rc = VirtualBox::loadSettingsTree_Again (tree, file);
1907 CheckComRCReturnRC (rc);
1908
1909 const Utf8Str key = aKey;
1910
1911 Key globalNode = tree.rootKey().key ("Global");
1912 Key extraDataNode = globalNode.findKey ("ExtraData");
1913
1914 if (!extraDataNode.isNull())
1915 {
1916 /* check if the key exists */
1917 Key::List items = extraDataNode.keys ("ExtraDataItem");
1918 for (Key::List::const_iterator it = items.begin();
1919 it != items.end(); ++ it)
1920 {
1921 if (key == (*it).stringValue ("name"))
1922 {
1923 Bstr val = (*it).stringValue ("value");
1924 val.cloneTo (aValue);
1925 break;
1926 }
1927 }
1928 }
1929 }
1930 catch (...)
1931 {
1932 rc = VirtualBox::handleUnexpectedExceptions (RT_SRC_POS);
1933 }
1934
1935 return rc;
1936}
1937
1938/**
1939 * @note Locks this object for writing.
1940 */
1941STDMETHODIMP VirtualBox::SetExtraData (INPTR BSTR aKey, INPTR BSTR aValue)
1942{
1943 if (!aKey)
1944 return E_INVALIDARG;
1945
1946 AutoCaller autoCaller (this);
1947 CheckComRCReturnRC (autoCaller.rc());
1948
1949 Guid emptyGuid;
1950
1951 bool changed = false;
1952 HRESULT rc = S_OK;
1953
1954 /* serialize file access (prevent concurrent reads and writes) */
1955 AutoWriteLock alock (this);
1956
1957 try
1958 {
1959 using namespace settings;
1960
1961 /* load the settings file */
1962 File file (mData.mCfgFile.mHandle, Utf8Str (mData.mCfgFile.mName));
1963 XmlTreeBackend tree;
1964
1965 rc = VirtualBox::loadSettingsTree_ForUpdate (tree, file);
1966 CheckComRCReturnRC (rc);
1967
1968 const Utf8Str key = aKey;
1969 Bstr oldVal;
1970
1971 Key globalNode = tree.rootKey().key ("Global");
1972 Key extraDataNode = globalNode.createKey ("ExtraData");
1973 Key extraDataItemNode;
1974
1975 Key::List items = extraDataNode.keys ("ExtraDataItem");
1976 for (Key::List::const_iterator it = items.begin();
1977 it != items.end(); ++ it)
1978 {
1979 if (key == (*it).stringValue ("name"))
1980 {
1981 extraDataItemNode = *it;
1982 oldVal = (*it).stringValue ("value");
1983 break;
1984 }
1985 }
1986
1987 /* When no key is found, oldVal is null */
1988 changed = oldVal != aValue;
1989
1990 if (changed)
1991 {
1992 /* ask for permission from all listeners */
1993 Bstr error;
1994 if (!onExtraDataCanChange (Guid::Empty, aKey, aValue, error))
1995 {
1996 const char *sep = error.isEmpty() ? "" : ": ";
1997 const BSTR err = error.isNull() ? (const BSTR) L"" : error.raw();
1998 LogWarningFunc (("Someone vetoed! Change refused%s%ls\n",
1999 sep, err));
2000 return setError (E_ACCESSDENIED,
2001 tr ("Could not set extra data because someone refused "
2002 "the requested change of '%ls' to '%ls'%s%ls"),
2003 aKey, aValue, sep, err);
2004 }
2005
2006 if (aValue != NULL)
2007 {
2008 if (extraDataItemNode.isNull())
2009 {
2010 extraDataItemNode = extraDataNode.appendKey ("ExtraDataItem");
2011 extraDataItemNode.setStringValue ("name", key);
2012 }
2013 extraDataItemNode.setStringValue ("value", Utf8Str (aValue));
2014 }
2015 else
2016 {
2017 /* an old value does for sure exist here (XML schema
2018 * guarantees that "value" may not absent in the
2019 * <ExtraDataItem> element) */
2020 Assert (!extraDataItemNode.isNull());
2021 extraDataItemNode.zap();
2022 }
2023
2024 /* save settings on success */
2025 rc = VirtualBox::saveSettingsTree (tree, file,
2026 mData.mSettingsFileVersion);
2027 CheckComRCReturnRC (rc);
2028 }
2029 }
2030 catch (...)
2031 {
2032 rc = VirtualBox::handleUnexpectedExceptions (RT_SRC_POS);
2033 }
2034
2035 /* fire a notification */
2036 if (SUCCEEDED (rc) && changed)
2037 onExtraDataChange (Guid::Empty, aKey, aValue);
2038
2039 return rc;
2040}
2041
2042/**
2043 * @note Locks objects!
2044 */
2045STDMETHODIMP VirtualBox::OpenSession (ISession *aSession, INPTR GUIDPARAM aMachineId)
2046{
2047 if (!aSession)
2048 return E_INVALIDARG;
2049
2050 AutoCaller autoCaller (this);
2051 CheckComRCReturnRC (autoCaller.rc());
2052
2053 Guid id = aMachineId;
2054 ComObjPtr <Machine> machine;
2055
2056 HRESULT rc = findMachine (id, true /* setError */, &machine);
2057 CheckComRCReturnRC (rc);
2058
2059 /* check the session state */
2060 SessionState_T state;
2061 rc = aSession->COMGETTER(State) (&state);
2062 CheckComRCReturnRC (rc);
2063
2064 if (state != SessionState_Closed)
2065 return setError (E_INVALIDARG,
2066 tr ("The given session is already open or being opened"));
2067
2068 /* get the IInternalSessionControl interface */
2069 ComPtr <IInternalSessionControl> control = aSession;
2070 ComAssertMsgRet (!!control, ("No IInternalSessionControl interface"),
2071 E_INVALIDARG);
2072
2073 rc = machine->openSession (control);
2074
2075 if (SUCCEEDED (rc))
2076 {
2077 /*
2078 * tell the client watcher thread to update the set of
2079 * machines that have open sessions
2080 */
2081 updateClientWatcher();
2082
2083 /* fire an event */
2084 onSessionStateChange (aMachineId, SessionState_Open);
2085 }
2086
2087 return rc;
2088}
2089
2090/**
2091 * @note Locks objects!
2092 */
2093STDMETHODIMP VirtualBox::OpenRemoteSession (ISession *aSession,
2094 INPTR GUIDPARAM aMachineId,
2095 INPTR BSTR aType,
2096 INPTR BSTR aEnvironment,
2097 IProgress **aProgress)
2098{
2099 if (!aSession || !aType)
2100 return E_INVALIDARG;
2101 if (!aProgress)
2102 return E_POINTER;
2103
2104 AutoCaller autoCaller (this);
2105 CheckComRCReturnRC (autoCaller.rc());
2106
2107 Guid id = aMachineId;
2108 ComObjPtr <Machine> machine;
2109
2110 HRESULT rc = findMachine (id, true /* setError */, &machine);
2111 CheckComRCReturnRC (rc);
2112
2113 /* check the session state */
2114 SessionState_T state;
2115 rc = aSession->COMGETTER(State) (&state);
2116 CheckComRCReturnRC (rc);
2117
2118 if (state != SessionState_Closed)
2119 return setError (E_INVALIDARG,
2120 tr ("The given session is already open or being opened"));
2121
2122 /* get the IInternalSessionControl interface */
2123 ComPtr <IInternalSessionControl> control = aSession;
2124 ComAssertMsgRet (!!control, ("No IInternalSessionControl interface"),
2125 E_INVALIDARG);
2126
2127 /* create a progress object */
2128 ComObjPtr <Progress> progress;
2129 progress.createObject();
2130 progress->init (this, static_cast <IMachine *> (machine),
2131 Bstr (tr ("Spawning session")),
2132 FALSE /* aCancelable */);
2133
2134 rc = machine->openRemoteSession (control, aType, aEnvironment, progress);
2135
2136 if (SUCCEEDED (rc))
2137 {
2138 progress.queryInterfaceTo (aProgress);
2139
2140 /* fire an event */
2141 onSessionStateChange (aMachineId, SessionState_Spawning);
2142 }
2143
2144 return rc;
2145}
2146
2147/**
2148 * @note Locks objects!
2149 */
2150STDMETHODIMP VirtualBox::OpenExistingSession (ISession *aSession,
2151 INPTR GUIDPARAM aMachineId)
2152{
2153 if (!aSession)
2154 return E_POINTER;
2155
2156 AutoCaller autoCaller (this);
2157 CheckComRCReturnRC (autoCaller.rc());
2158
2159 Guid id = aMachineId;
2160 ComObjPtr <Machine> machine;
2161
2162 HRESULT rc = findMachine (id, true /* setError */, &machine);
2163 CheckComRCReturnRC (rc);
2164
2165 /* check the session state */
2166 SessionState_T state;
2167 rc = aSession->COMGETTER(State) (&state);
2168 CheckComRCReturnRC (rc);
2169
2170 if (state != SessionState_Closed)
2171 return setError (E_INVALIDARG,
2172 tr ("The given session is already open or being opened"));
2173
2174 /* get the IInternalSessionControl interface */
2175 ComPtr <IInternalSessionControl> control = aSession;
2176 ComAssertMsgRet (!!control, ("No IInternalSessionControl interface"),
2177 E_INVALIDARG);
2178
2179 rc = machine->openExistingSession (control);
2180
2181 return rc;
2182}
2183
2184/**
2185 * @note Locks this object for writing.
2186 */
2187STDMETHODIMP VirtualBox::RegisterCallback (IVirtualBoxCallback *aCallback)
2188{
2189 LogFlowThisFunc (("aCallback=%p\n", aCallback));
2190
2191 if (!aCallback)
2192 return E_INVALIDARG;
2193
2194 AutoCaller autoCaller (this);
2195 CheckComRCReturnRC (autoCaller.rc());
2196
2197 AutoWriteLock alock (this);
2198 mData.mCallbacks.push_back (CallbackList::value_type (aCallback));
2199
2200 return S_OK;
2201}
2202
2203/**
2204 * @note Locks this object for writing.
2205 */
2206STDMETHODIMP VirtualBox::UnregisterCallback (IVirtualBoxCallback *aCallback)
2207{
2208 if (!aCallback)
2209 return E_INVALIDARG;
2210
2211 AutoCaller autoCaller (this);
2212 CheckComRCReturnRC (autoCaller.rc());
2213
2214 HRESULT rc = S_OK;
2215
2216 AutoWriteLock alock (this);
2217
2218 CallbackList::iterator it;
2219 it = std::find (mData.mCallbacks.begin(),
2220 mData.mCallbacks.end(),
2221 CallbackList::value_type (aCallback));
2222 if (it == mData.mCallbacks.end())
2223 rc = E_INVALIDARG;
2224 else
2225 mData.mCallbacks.erase (it);
2226
2227 LogFlowThisFunc (("aCallback=%p, rc=%08X\n", aCallback, rc));
2228 return rc;
2229}
2230
2231STDMETHODIMP VirtualBox::WaitForPropertyChange (INPTR BSTR aWhat, ULONG aTimeout,
2232 BSTR *aChanged, BSTR *aValues)
2233{
2234 return E_NOTIMPL;
2235}
2236
2237STDMETHODIMP VirtualBox::SaveSettings()
2238{
2239 AutoCaller autoCaller (this);
2240 CheckComRCReturnRC (autoCaller.rc());
2241
2242 return saveSettings();
2243}
2244
2245STDMETHODIMP VirtualBox::SaveSettingsWithBackup (BSTR *aBakFileName)
2246{
2247 if (!aBakFileName)
2248 return E_POINTER;
2249
2250 AutoCaller autoCaller (this);
2251 CheckComRCReturnRC (autoCaller.rc());
2252
2253 /* saveSettings() needs write lock */
2254 AutoWriteLock alock (this);
2255
2256 /* perform backup only when there was auto-conversion */
2257 if (mData.mSettingsFileVersion != VBOX_XML_VERSION_FULL)
2258 {
2259 Bstr bakFileName;
2260
2261 HRESULT rc = backupSettingsFile (mData.mCfgFile.mName,
2262 mData.mSettingsFileVersion,
2263 bakFileName);
2264 CheckComRCReturnRC (rc);
2265
2266 bakFileName.cloneTo (aBakFileName);
2267 }
2268
2269 return saveSettings();
2270}
2271
2272// public methods only for internal purposes
2273/////////////////////////////////////////////////////////////////////////////
2274
2275/**
2276 * Posts an event to the event queue that is processed asynchronously
2277 * on a dedicated thread.
2278 *
2279 * Posting events to the dedicated event queue is useful to perform secondary
2280 * actions outside any object locks -- for example, to iterate over a list
2281 * of callbacks and inform them about some change caused by some object's
2282 * method call.
2283 *
2284 * @param event event to post
2285 * (must be allocated using |new|, will be deleted automatically
2286 * by the event thread after processing)
2287 *
2288 * @note Doesn't lock any object.
2289 */
2290HRESULT VirtualBox::postEvent (Event *event)
2291{
2292 AutoCaller autoCaller (this);
2293 AssertComRCReturn (autoCaller.rc(), autoCaller.rc());
2294
2295 if (autoCaller.state() != Ready)
2296 {
2297 LogWarningFunc (("VirtualBox has been uninitialized (state=%d), "
2298 "the event is discarded!\n",
2299 autoCaller.state()));
2300 return S_OK;
2301 }
2302
2303 AssertReturn (event, E_FAIL);
2304 AssertReturn (mAsyncEventQ, E_FAIL);
2305
2306 AutoWriteLock alock (mAsyncEventQLock);
2307 if (mAsyncEventQ->postEvent (event))
2308 return S_OK;
2309
2310 return E_FAIL;
2311}
2312
2313/**
2314 * Helper method to add a progress to the global collection of pending
2315 * operations.
2316 *
2317 * @param aProgress operation to add to the collection
2318 * @return COM status code
2319 *
2320 * @note Locks this object for writing.
2321 */
2322HRESULT VirtualBox::addProgress (IProgress *aProgress)
2323{
2324 if (!aProgress)
2325 return E_INVALIDARG;
2326
2327 AutoCaller autoCaller (this);
2328 CheckComRCReturnRC (autoCaller.rc());
2329
2330 AutoWriteLock alock (this);
2331 mData.mProgressOperations.push_back (aProgress);
2332 return S_OK;
2333}
2334
2335/**
2336 * Helper method to remove the progress from the global collection of pending
2337 * operations. Usualy gets called upon progress completion.
2338 *
2339 * @param aId UUID of the progress operation to remove
2340 * @return COM status code
2341 *
2342 * @note Locks this object for writing.
2343 */
2344HRESULT VirtualBox::removeProgress (INPTR GUIDPARAM aId)
2345{
2346 AutoCaller autoCaller (this);
2347 CheckComRCReturnRC (autoCaller.rc());
2348
2349 ComPtr <IProgress> progress;
2350
2351 AutoWriteLock alock (this);
2352
2353 for (ProgressList::iterator it = mData.mProgressOperations.begin();
2354 it != mData.mProgressOperations.end();
2355 ++ it)
2356 {
2357 Guid id;
2358 (*it)->COMGETTER(Id) (id.asOutParam());
2359 if (id == aId)
2360 {
2361 mData.mProgressOperations.erase (it);
2362 return S_OK;
2363 }
2364 }
2365
2366 AssertFailed(); /* should never happen */
2367
2368 return E_FAIL;
2369}
2370
2371#ifdef RT_OS_WINDOWS
2372
2373struct StartSVCHelperClientData
2374{
2375 ComObjPtr <VirtualBox> that;
2376 ComObjPtr <Progress> progress;
2377 bool privileged;
2378 VirtualBox::SVCHelperClientFunc func;
2379 void *user;
2380};
2381
2382/**
2383 * Helper method to that starts a worker thread that:
2384 * - creates a pipe communication channel using SVCHlpClient;
2385 * - starts a SVC Helper process that will inherit this channel;
2386 * - executes the supplied function by passing it the created SVCHlpClient
2387 * and opened instance to communicate to the Helper process and the given
2388 * Progress object.
2389 *
2390 * The user function is supposed to communicate to the helper process
2391 * using the \a aClient argument to do the requested job and optionally expose
2392 * the prgress through the \a aProgress object. The user function should never
2393 * call notifyComplete() on it: this will be done automatically using the
2394 * result code returned by the function.
2395 *
2396 * Before the user function is stared, the communication channel passed to in
2397 * the \a aClient argument, is fully set up, the function should start using
2398 * it's write() and read() methods directly.
2399 *
2400 * The \a aVrc parameter of the user function may be used to return an error
2401 * code if it is related to communication errors (for example, returned by
2402 * the SVCHlpClient members when they fail). In this case, the correct error
2403 * message using this value will be reported to the caller. Note that the
2404 * value of \a aVrc is inspected only if the user function itself returns
2405 * a success.
2406 *
2407 * If a failure happens anywhere before the user function would be normally
2408 * called, it will be called anyway in special "cleanup only" mode indicated
2409 * by \a aClient, \a aProgress and \aVrc arguments set to NULL. In this mode,
2410 * all the function is supposed to do is to cleanup its aUser argument if
2411 * necessary (it's assumed that the ownership of this argument is passed to
2412 * the user function once #startSVCHelperClient() returns a success, thus
2413 * making it responsible for the cleanup).
2414 *
2415 * After the user function returns, the thread will send the SVCHlpMsg::Null
2416 * message to indicate a process termination.
2417 *
2418 * @param aPrivileged |true| to start the SVC Hepler process as a privlieged
2419 * user that can perform administrative tasks
2420 * @param aFunc user function to run
2421 * @param aUser argument to the user function
2422 * @param aProgress progress object that will track operation completion
2423 *
2424 * @note aPrivileged is currently ignored (due to some unsolved problems in
2425 * Vista) and the process will be started as a normal (unprivileged)
2426 * process.
2427 *
2428 * @note Doesn't lock anything.
2429 */
2430HRESULT VirtualBox::startSVCHelperClient (bool aPrivileged,
2431 SVCHelperClientFunc aFunc,
2432 void *aUser, Progress *aProgress)
2433{
2434 AssertReturn (aFunc, E_POINTER);
2435 AssertReturn (aProgress, E_POINTER);
2436
2437 AutoCaller autoCaller (this);
2438 CheckComRCReturnRC (autoCaller.rc());
2439
2440 /* create the SVCHelperClientThread() argument */
2441 std::auto_ptr <StartSVCHelperClientData>
2442 d (new StartSVCHelperClientData());
2443 AssertReturn (d.get(), E_OUTOFMEMORY);
2444
2445 d->that = this;
2446 d->progress = aProgress;
2447 d->privileged = aPrivileged;
2448 d->func = aFunc;
2449 d->user = aUser;
2450
2451 RTTHREAD tid = NIL_RTTHREAD;
2452 int vrc = RTThreadCreate (&tid, SVCHelperClientThread,
2453 static_cast <void *> (d.get()),
2454 0, RTTHREADTYPE_MAIN_WORKER,
2455 RTTHREADFLAGS_WAITABLE, "SVCHelper");
2456
2457 ComAssertMsgRCRet (vrc, ("Could not create SVCHelper thread (%Vrc)\n", vrc),
2458 E_FAIL);
2459
2460 /* d is now owned by SVCHelperClientThread(), so release it */
2461 d.release();
2462
2463 return S_OK;
2464}
2465
2466/**
2467 * Worker thread for startSVCHelperClient().
2468 */
2469/* static */
2470DECLCALLBACK(int)
2471VirtualBox::SVCHelperClientThread (RTTHREAD aThread, void *aUser)
2472{
2473 LogFlowFuncEnter();
2474
2475 std::auto_ptr <StartSVCHelperClientData>
2476 d (static_cast <StartSVCHelperClientData *> (aUser));
2477
2478 HRESULT rc = S_OK;
2479 bool userFuncCalled = false;
2480
2481 do
2482 {
2483 AssertBreakStmt (d.get(), rc = E_POINTER);
2484 AssertReturn (!d->progress.isNull(), E_POINTER);
2485
2486 /* protect VirtualBox from uninitialization */
2487 AutoCaller autoCaller (d->that);
2488 if (!autoCaller.isOk())
2489 {
2490 /* it's too late */
2491 rc = autoCaller.rc();
2492 break;
2493 }
2494
2495 int vrc = VINF_SUCCESS;
2496
2497 Guid id;
2498 id.create();
2499 SVCHlpClient client;
2500 vrc = client.create (Utf8StrFmt ("VirtualBox\\SVCHelper\\{%Vuuid}",
2501 id.raw()));
2502 if (VBOX_FAILURE (vrc))
2503 {
2504 rc = setError (E_FAIL,
2505 tr ("Could not create the communication channel (%Vrc)"), vrc);
2506 break;
2507 }
2508
2509 /* get the path to the executable */
2510 char exePathBuf [RTPATH_MAX];
2511 char *exePath = RTProcGetExecutableName (exePathBuf, RTPATH_MAX);
2512 ComAssertBreak (exePath, E_FAIL);
2513
2514 Utf8Str argsStr = Utf8StrFmt ("/Helper %s", client.name().raw());
2515
2516 LogFlowFunc (("Starting '\"%s\" %s'...\n", exePath, argsStr.raw()));
2517
2518 RTPROCESS pid = NIL_RTPROCESS;
2519
2520 if (d->privileged)
2521 {
2522 /* Attempt to start a privileged process using the Run As dialog */
2523
2524 Bstr file = exePath;
2525 Bstr parameters = argsStr;
2526
2527 SHELLEXECUTEINFO shExecInfo;
2528
2529 shExecInfo.cbSize = sizeof (SHELLEXECUTEINFO);
2530
2531 shExecInfo.fMask = NULL;
2532 shExecInfo.hwnd = NULL;
2533 shExecInfo.lpVerb = L"runas";
2534 shExecInfo.lpFile = file;
2535 shExecInfo.lpParameters = parameters;
2536 shExecInfo.lpDirectory = NULL;
2537 shExecInfo.nShow = SW_NORMAL;
2538 shExecInfo.hInstApp = NULL;
2539
2540 if (!ShellExecuteEx (&shExecInfo))
2541 {
2542 int vrc2 = RTErrConvertFromWin32 (GetLastError());
2543 /* hide excessive details in case of a frequent error
2544 * (pressing the Cancel button to close the Run As dialog) */
2545 if (vrc2 == VERR_CANCELLED)
2546 rc = setError (E_FAIL,
2547 tr ("Operatiion cancelled by the user"));
2548 else
2549 rc = setError (E_FAIL,
2550 tr ("Could not launch a privileged process '%s' (%Vrc)"),
2551 exePath, vrc2);
2552 break;
2553 }
2554 }
2555 else
2556 {
2557 const char *args[] = { exePath, "/Helper", client.name(), 0 };
2558 vrc = RTProcCreate (exePath, args, RTENV_DEFAULT, 0, &pid);
2559 if (VBOX_FAILURE (vrc))
2560 {
2561 rc = setError (E_FAIL,
2562 tr ("Could not launch a process '%s' (%Vrc)"), exePath, vrc);
2563 break;
2564 }
2565 }
2566
2567 /* wait for the client to connect */
2568 vrc = client.connect();
2569 if (VBOX_SUCCESS (vrc))
2570 {
2571 /* start the user supplied function */
2572 rc = d->func (&client, d->progress, d->user, &vrc);
2573 userFuncCalled = true;
2574 }
2575
2576 /* send the termination signal to the process anyway */
2577 {
2578 int vrc2 = client.write (SVCHlpMsg::Null);
2579 if (VBOX_SUCCESS (vrc))
2580 vrc = vrc2;
2581 }
2582
2583 if (SUCCEEDED (rc) && VBOX_FAILURE (vrc))
2584 {
2585 rc = setError (E_FAIL,
2586 tr ("Could not operate the communication channel (%Vrc)"), vrc);
2587 break;
2588 }
2589 }
2590 while (0);
2591
2592 if (FAILED (rc) && !userFuncCalled)
2593 {
2594 /* call the user function in the "cleanup only" mode
2595 * to let it free resources passed to in aUser */
2596 d->func (NULL, NULL, d->user, NULL);
2597 }
2598
2599 d->progress->notifyComplete (rc);
2600
2601 LogFlowFuncLeave();
2602 return 0;
2603}
2604
2605#endif /* RT_OS_WINDOWS */
2606
2607/**
2608 * Sends a signal to the client watcher thread to rescan the set of machines
2609 * that have open sessions.
2610 *
2611 * @note Doesn't lock anything.
2612 */
2613void VirtualBox::updateClientWatcher()
2614{
2615 AutoCaller autoCaller (this);
2616 AssertComRCReturn (autoCaller.rc(), (void) 0);
2617
2618 AssertReturn (mWatcherData.mThread != NIL_RTTHREAD, (void) 0);
2619
2620 /* sent an update request */
2621#if defined(RT_OS_WINDOWS)
2622 ::SetEvent (mWatcherData.mUpdateReq);
2623#elif defined(RT_OS_OS2)
2624 RTSemEventSignal (mWatcherData.mUpdateReq);
2625#elif defined(VBOX_WITH_SYS_V_IPC_SESSION_WATCHER)
2626 RTSemEventSignal (mWatcherData.mUpdateReq);
2627#else
2628# error "Port me!"
2629#endif
2630}
2631
2632/**
2633 * Adds the given child process ID to the list of processes to be reaped.
2634 * This call should be followed by #updateClientWatcher() to take the effect.
2635 */
2636void VirtualBox::addProcessToReap (RTPROCESS pid)
2637{
2638 AutoCaller autoCaller (this);
2639 AssertComRCReturn (autoCaller.rc(), (void) 0);
2640
2641 /// @todo (dmik) Win32?
2642#ifndef RT_OS_WINDOWS
2643 AutoWriteLock alock (this);
2644 mWatcherData.mProcesses.push_back (pid);
2645#endif
2646}
2647
2648/** Event for onMachineStateChange(), onMachineDataChange(), onMachineRegistered() */
2649struct MachineEvent : public VirtualBox::CallbackEvent
2650{
2651 enum What { DataChanged, StateChanged, Registered };
2652
2653 MachineEvent (VirtualBox *aVB, const Guid &aId)
2654 : CallbackEvent (aVB), what (DataChanged), id (aId)
2655 {}
2656
2657 MachineEvent (VirtualBox *aVB, const Guid &aId, MachineState_T aState)
2658 : CallbackEvent (aVB), what (StateChanged), id (aId)
2659 , state (aState)
2660 {}
2661
2662 MachineEvent (VirtualBox *aVB, const Guid &aId, BOOL aRegistered)
2663 : CallbackEvent (aVB), what (Registered), id (aId)
2664 , registered (aRegistered)
2665 {}
2666
2667 void handleCallback (const ComPtr <IVirtualBoxCallback> &aCallback)
2668 {
2669 switch (what)
2670 {
2671 case DataChanged:
2672 LogFlow (("OnMachineDataChange: id={%Vuuid}\n", id.ptr()));
2673 aCallback->OnMachineDataChange (id);
2674 break;
2675
2676 case StateChanged:
2677 LogFlow (("OnMachineStateChange: id={%Vuuid}, state=%d\n",
2678 id.ptr(), state));
2679 aCallback->OnMachineStateChange (id, state);
2680 break;
2681
2682 case Registered:
2683 LogFlow (("OnMachineRegistered: id={%Vuuid}, registered=%d\n",
2684 id.ptr(), registered));
2685 aCallback->OnMachineRegistered (id, registered);
2686 break;
2687 }
2688 }
2689
2690 const What what;
2691
2692 Guid id;
2693 MachineState_T state;
2694 BOOL registered;
2695};
2696
2697/**
2698 * @note Doesn't lock any object.
2699 */
2700void VirtualBox::onMachineStateChange (const Guid &aId, MachineState_T aState)
2701{
2702 postEvent (new MachineEvent (this, aId, aState));
2703}
2704
2705/**
2706 * @note Doesn't lock any object.
2707 */
2708void VirtualBox::onMachineDataChange (const Guid &aId)
2709{
2710 postEvent (new MachineEvent (this, aId));
2711}
2712
2713/**
2714 * @note Locks this object for reading.
2715 */
2716BOOL VirtualBox::onExtraDataCanChange (const Guid &aId, INPTR BSTR aKey, INPTR BSTR aValue,
2717 Bstr &aError)
2718{
2719 LogFlowThisFunc (("machine={%s} aKey={%ls} aValue={%ls}\n",
2720 aId.toString().raw(), aKey, aValue));
2721
2722 AutoCaller autoCaller (this);
2723 AssertComRCReturn (autoCaller.rc(), autoCaller.rc());
2724
2725 CallbackList list;
2726 {
2727 AutoReadLock alock (this);
2728 list = mData.mCallbacks;
2729 }
2730
2731 BOOL allowChange = TRUE;
2732 CallbackList::iterator it = list.begin();
2733 while ((it != list.end()) && allowChange)
2734 {
2735 HRESULT rc = (*it++)->OnExtraDataCanChange (aId, aKey, aValue,
2736 aError.asOutParam(), &allowChange);
2737 if (FAILED (rc))
2738 {
2739 /* if a call to this method fails for some reason (for ex., because
2740 * the other side is dead), we ensure allowChange stays true
2741 * (MS COM RPC implementation seems to zero all output vars before
2742 * issuing an IPC call or after a failure, so it's essential
2743 * there) */
2744 allowChange = TRUE;
2745 }
2746 }
2747
2748 LogFlowThisFunc (("allowChange=%RTbool\n", allowChange));
2749 return allowChange;
2750}
2751
2752/** Event for onExtraDataChange() */
2753struct ExtraDataEvent : public VirtualBox::CallbackEvent
2754{
2755 ExtraDataEvent (VirtualBox *aVB, const Guid &aMachineId,
2756 INPTR BSTR aKey, INPTR BSTR aVal)
2757 : CallbackEvent (aVB), machineId (aMachineId)
2758 , key (aKey), val (aVal)
2759 {}
2760
2761 void handleCallback (const ComPtr <IVirtualBoxCallback> &aCallback)
2762 {
2763 LogFlow (("OnExtraDataChange: machineId={%Vuuid}, key='%ls', val='%ls'\n",
2764 machineId.ptr(), key.raw(), val.raw()));
2765 aCallback->OnExtraDataChange (machineId, key, val);
2766 }
2767
2768 Guid machineId;
2769 Bstr key, val;
2770};
2771
2772/**
2773 * @note Doesn't lock any object.
2774 */
2775void VirtualBox::onExtraDataChange (const Guid &aId, INPTR BSTR aKey, INPTR BSTR aValue)
2776{
2777 postEvent (new ExtraDataEvent (this, aId, aKey, aValue));
2778}
2779
2780/**
2781 * @note Doesn't lock any object.
2782 */
2783void VirtualBox::onMachineRegistered (const Guid &aId, BOOL aRegistered)
2784{
2785 postEvent (new MachineEvent (this, aId, aRegistered));
2786}
2787
2788/** Event for onSessionStateChange() */
2789struct SessionEvent : public VirtualBox::CallbackEvent
2790{
2791 SessionEvent (VirtualBox *aVB, const Guid &aMachineId, SessionState_T aState)
2792 : CallbackEvent (aVB), machineId (aMachineId), sessionState (aState)
2793 {}
2794
2795 void handleCallback (const ComPtr <IVirtualBoxCallback> &aCallback)
2796 {
2797 LogFlow (("OnSessionStateChange: machineId={%Vuuid}, sessionState=%d\n",
2798 machineId.ptr(), sessionState));
2799 aCallback->OnSessionStateChange (machineId, sessionState);
2800 }
2801
2802 Guid machineId;
2803 SessionState_T sessionState;
2804};
2805
2806/**
2807 * @note Doesn't lock any object.
2808 */
2809void VirtualBox::onSessionStateChange (const Guid &aId, SessionState_T aState)
2810{
2811 postEvent (new SessionEvent (this, aId, aState));
2812}
2813
2814/** Event for onSnapshotTaken(), onSnapshotRemoved() and onSnapshotChange() */
2815struct SnapshotEvent : public VirtualBox::CallbackEvent
2816{
2817 enum What { Taken, Discarded, Changed };
2818
2819 SnapshotEvent (VirtualBox *aVB, const Guid &aMachineId, const Guid &aSnapshotId,
2820 What aWhat)
2821 : CallbackEvent (aVB)
2822 , what (aWhat)
2823 , machineId (aMachineId), snapshotId (aSnapshotId)
2824 {}
2825
2826 void handleCallback (const ComPtr <IVirtualBoxCallback> &aCallback)
2827 {
2828 switch (what)
2829 {
2830 case Taken:
2831 LogFlow (("OnSnapshotTaken: machineId={%Vuuid}, snapshotId={%Vuuid}\n",
2832 machineId.ptr(), snapshotId.ptr()));
2833 aCallback->OnSnapshotTaken (machineId, snapshotId);
2834 break;
2835
2836 case Discarded:
2837 LogFlow (("OnSnapshotDiscarded: machineId={%Vuuid}, snapshotId={%Vuuid}\n",
2838 machineId.ptr(), snapshotId.ptr()));
2839 aCallback->OnSnapshotDiscarded (machineId, snapshotId);
2840 break;
2841
2842 case Changed:
2843 LogFlow (("OnSnapshotChange: machineId={%Vuuid}, snapshotId={%Vuuid}\n",
2844 machineId.ptr(), snapshotId.ptr()));
2845 aCallback->OnSnapshotChange (machineId, snapshotId);
2846 break;
2847 }
2848 }
2849
2850 const What what;
2851
2852 Guid machineId;
2853 Guid snapshotId;
2854};
2855
2856/**
2857 * @note Doesn't lock any object.
2858 */
2859void VirtualBox::onSnapshotTaken (const Guid &aMachineId, const Guid &aSnapshotId)
2860{
2861 postEvent (new SnapshotEvent (this, aMachineId, aSnapshotId, SnapshotEvent::Taken));
2862}
2863
2864/**
2865 * @note Doesn't lock any object.
2866 */
2867void VirtualBox::onSnapshotDiscarded (const Guid &aMachineId, const Guid &aSnapshotId)
2868{
2869 postEvent (new SnapshotEvent (this, aMachineId, aSnapshotId, SnapshotEvent::Discarded));
2870}
2871
2872/**
2873 * @note Doesn't lock any object.
2874 */
2875void VirtualBox::onSnapshotChange (const Guid &aMachineId, const Guid &aSnapshotId)
2876{
2877 postEvent (new SnapshotEvent (this, aMachineId, aSnapshotId, SnapshotEvent::Changed));
2878}
2879
2880/**
2881 * @note Locks this object for reading.
2882 */
2883ComObjPtr <GuestOSType> VirtualBox::getUnknownOSType()
2884{
2885 ComObjPtr <GuestOSType> type;
2886
2887 AutoCaller autoCaller (this);
2888 AssertComRCReturn (autoCaller.rc(), type);
2889
2890 AutoReadLock alock (this);
2891
2892 /* unknown type must always be the first */
2893 ComAssertRet (mData.mGuestOSTypes.size() > 0, type);
2894
2895 type = mData.mGuestOSTypes.front();
2896 return type;
2897}
2898
2899/**
2900 * Returns the list of opened machines (i.e. machines having direct sessions
2901 * opened by client processes).
2902 *
2903 * @note the returned list contains smart pointers. So, clear it as soon as
2904 * it becomes no more necessary to release instances.
2905 * @note it can be possible that a session machine from the list has been
2906 * already uninitialized, so a) lock the instance and b) chheck for
2907 * instance->isReady() return value before manipulating the object directly
2908 * (i.e. not through COM methods).
2909 *
2910 * @note Locks objects for reading.
2911 */
2912void VirtualBox::getOpenedMachines (SessionMachineVector &aVector)
2913{
2914 AutoCaller autoCaller (this);
2915 AssertComRCReturn (autoCaller.rc(), (void) 0);
2916
2917 std::list <ComObjPtr <SessionMachine> > list;
2918
2919 {
2920 AutoReadLock alock (this);
2921
2922 for (MachineList::iterator it = mData.mMachines.begin();
2923 it != mData.mMachines.end();
2924 ++ it)
2925 {
2926 ComObjPtr <SessionMachine> sm = (*it)->sessionMachine();
2927 /* SessionMachine is null when there are no open sessions */
2928 if (!sm.isNull())
2929 list.push_back (sm);
2930 }
2931 }
2932
2933 aVector = SessionMachineVector (list.begin(), list.end());
2934 return;
2935}
2936
2937/**
2938 * Helper to find machines that use the given DVD image.
2939 *
2940 * This method also checks whether an existing snapshot refers to the given
2941 * image. However, only the machine ID is returned in this case, not IDs of
2942 * individual snapshots.
2943 *
2944 * @param aId Image ID to get usage for.
2945 * @param aUsage Type of the check.
2946 * @param aMachineIDs Where to store the list of machine IDs (can be NULL)
2947 *
2948 * @return @c true if at least one machine or its snapshot found and @c false
2949 * otherwise.
2950 *
2951 * @note For now, we just scan all the machines. We can optimize this later
2952 * if required by adding the corresponding field to DVDImage and requiring all
2953 * IDVDImage instances to be DVDImage objects.
2954 *
2955 * @note Locks objects for reading.
2956 */
2957bool VirtualBox::getDVDImageUsage (const Guid &aId,
2958 ResourceUsage_T aUsage,
2959 Bstr *aMachineIDs)
2960{
2961 AutoCaller autoCaller (this);
2962 AssertComRCReturn (autoCaller.rc(), FALSE);
2963
2964 typedef std::set <Guid> Set;
2965 Set idSet;
2966
2967 {
2968 AutoReadLock alock (this);
2969
2970 for (MachineList::const_iterator mit = mData.mMachines.begin();
2971 mit != mData.mMachines.end();
2972 ++ mit)
2973 {
2974 ComObjPtr <Machine> m = *mit;
2975
2976 AutoLimitedCaller machCaller (m);
2977 AssertComRC (machCaller.rc());
2978
2979 /* ignore inaccessible machines */
2980 if (machCaller.state() == Machine::Ready)
2981 {
2982 if (m->isDVDImageUsed (aId, aUsage))
2983 {
2984 /* if not interested in the list, return shortly */
2985 if (aMachineIDs == NULL)
2986 return true;
2987
2988 idSet.insert (m->uuid());
2989 }
2990 }
2991 }
2992 }
2993
2994 if (aMachineIDs)
2995 {
2996 if (!idSet.empty())
2997 {
2998 /* convert to a string of UUIDs */
2999 char *idList = (char *) RTMemTmpAllocZ (RTUUID_STR_LENGTH * idSet.size());
3000 char *idListPtr = idList;
3001 for (Set::iterator it = idSet.begin(); it != idSet.end(); ++ it)
3002 {
3003 RTUuidToStr (*it, idListPtr, RTUUID_STR_LENGTH);
3004 idListPtr += RTUUID_STR_LENGTH - 1;
3005 /* replace EOS with a space char */
3006 *(idListPtr ++) = ' ';
3007 }
3008 Assert (int (idListPtr - idList) == int (RTUUID_STR_LENGTH * idSet.size()));
3009 /* remove the trailing space */
3010 *(-- idListPtr) = 0;
3011 /* copy the string */
3012 *aMachineIDs = idList;
3013 RTMemTmpFree (idList);
3014 }
3015 else
3016 {
3017 (*aMachineIDs).setNull();
3018 }
3019 }
3020
3021 return !idSet.empty();
3022}
3023
3024/**
3025 * Helper to find machines that use the given Floppy image.
3026 *
3027 * This method also checks whether an existing snapshot refers to the given
3028 * image. However, only the machine ID is returned in this case, not IDs of
3029 * individual snapshots.
3030 *
3031 * @param aId Image ID to get usage for.
3032 * @param aUsage Type of the check.
3033 * @param aMachineIDs Where to store the list of machine IDs (can be NULL)
3034 *
3035 * @return @c true if at least one machine or its snapshot found and @c false
3036 * otherwise.
3037 *
3038 * @note For now, we just scan all the machines. We can optimize this later
3039 * if required by adding the corresponding field to FloppyImage and requiring all
3040 * FloppyImage instances to be FloppyImage objects.
3041 *
3042 * @note Locks objects for reading.
3043 */
3044bool VirtualBox::getFloppyImageUsage (const Guid &aId,
3045 ResourceUsage_T aUsage,
3046 Bstr *aMachineIDs)
3047{
3048 AutoCaller autoCaller (this);
3049 AssertComRCReturn (autoCaller.rc(), FALSE);
3050
3051 typedef std::set <Guid> Set;
3052 Set idSet;
3053
3054 {
3055 AutoReadLock alock (this);
3056
3057 for (MachineList::const_iterator mit = mData.mMachines.begin();
3058 mit != mData.mMachines.end();
3059 ++ mit)
3060 {
3061 ComObjPtr <Machine> m = *mit;
3062
3063 AutoLimitedCaller machCaller (m);
3064 AssertComRC (machCaller.rc());
3065
3066 /* ignore inaccessible machines */
3067 if (machCaller.state() == Machine::Ready)
3068 {
3069 if (m->isFloppyImageUsed (aId, aUsage))
3070 {
3071 /* if not interested in the list, return shortly */
3072 if (aMachineIDs == NULL)
3073 return true;
3074
3075 idSet.insert (m->uuid());
3076 }
3077 }
3078 }
3079 }
3080
3081 if (aMachineIDs)
3082 {
3083 if (!idSet.empty())
3084 {
3085 /* convert to a string of UUIDs */
3086 char *idList = (char *) RTMemTmpAllocZ (RTUUID_STR_LENGTH * idSet.size());
3087 char *idListPtr = idList;
3088 for (Set::iterator it = idSet.begin(); it != idSet.end(); ++ it)
3089 {
3090 RTUuidToStr (*it, idListPtr, RTUUID_STR_LENGTH);
3091 idListPtr += RTUUID_STR_LENGTH - 1;
3092 /* replace EOS with a space char */
3093 *(idListPtr ++) = ' ';
3094 }
3095 Assert (int (idListPtr - idList) == int (RTUUID_STR_LENGTH * idSet.size()));
3096 /* remove the trailing space */
3097 *(-- idListPtr) = 0;
3098 /* copy the string */
3099 *aMachineIDs = idList;
3100 RTMemTmpFree (idList);
3101 }
3102 else
3103 {
3104 (*aMachineIDs).setNull();
3105 }
3106 }
3107
3108 return !idSet.empty();
3109}
3110
3111/**
3112 * Tries to calculate the relative path of the given absolute path using the
3113 * directory of the VirtualBox settings file as the base directory.
3114 *
3115 * @param aPath absolute path to calculate the relative path for
3116 * @param aResult where to put the result (used only when it's possible to
3117 * make a relative path from the given absolute path;
3118 * otherwise left untouched)
3119 *
3120 * @note Doesn't lock any object.
3121 */
3122void VirtualBox::calculateRelativePath (const char *aPath, Utf8Str &aResult)
3123{
3124 AutoCaller autoCaller (this);
3125 AssertComRCReturn (autoCaller.rc(), (void) 0);
3126
3127 /* no need to lock since mHomeDir is const */
3128
3129 Utf8Str settingsDir = mData.mHomeDir;
3130
3131 if (RTPathStartsWith (aPath, settingsDir))
3132 {
3133 /* when assigning, we create a separate Utf8Str instance because both
3134 * aPath and aResult can point to the same memory location when this
3135 * func is called (if we just do aResult = aPath, aResult will be freed
3136 * first, and since its the same as aPath, an attempt to copy garbage
3137 * will be made. */
3138 aResult = Utf8Str (aPath + settingsDir.length() + 1);
3139 }
3140}
3141
3142// private methods
3143/////////////////////////////////////////////////////////////////////////////
3144
3145/**
3146 * Searches for a Machine object with the given ID in the collection
3147 * of registered machines.
3148 *
3149 * @param id
3150 * ID of the machine
3151 * @param doSetError
3152 * if TRUE, the appropriate error info is set in case when the machine
3153 * is not found
3154 * @param machine
3155 * where to store the found machine object (can be NULL)
3156 *
3157 * @return
3158 * S_OK when found or E_INVALIDARG when not found
3159 *
3160 * @note Locks this object for reading.
3161 */
3162HRESULT VirtualBox::findMachine (const Guid &aId, bool aSetError,
3163 ComObjPtr <Machine> *aMachine /* = NULL */)
3164{
3165 AutoCaller autoCaller (this);
3166 AssertComRCReturn (autoCaller.rc(), autoCaller.rc());
3167
3168 bool found = false;
3169
3170 {
3171 AutoReadLock alock (this);
3172
3173 for (MachineList::iterator it = mData.mMachines.begin();
3174 !found && it != mData.mMachines.end();
3175 ++ it)
3176 {
3177 /* sanity */
3178 AutoLimitedCaller machCaller (*it);
3179 AssertComRC (machCaller.rc());
3180
3181 found = (*it)->uuid() == aId;
3182 if (found && aMachine)
3183 *aMachine = *it;
3184 }
3185 }
3186
3187 HRESULT rc = found ? S_OK : E_INVALIDARG;
3188
3189 if (aSetError && !found)
3190 {
3191 setError (E_INVALIDARG,
3192 tr ("Could not find a registered machine with UUID {%Vuuid}"),
3193 aId.raw());
3194 }
3195
3196 return rc;
3197}
3198
3199/**
3200 * Searches for a HardDisk object with the given ID or location specification
3201 * in the collection of registered hard disks. If both ID and location are
3202 * specified, the first object that matches either of them (not necessarily
3203 * both) is returned.
3204 *
3205 * @param aId ID of the hard disk (NULL when unused)
3206 * @param aLocation full location specification (NULL when unused)
3207 * @param aSetError if TRUE, the appropriate error info is set in case when
3208 * the disk is not found and only one search criteria (ID
3209 * or file name) is specified.
3210 * @param aHardDisk where to store the found hard disk object (can be NULL)
3211 *
3212 * @return
3213 * S_OK when found or E_INVALIDARG when not found
3214 *
3215 * @note Locks objects for reading!
3216 */
3217HRESULT VirtualBox::
3218findHardDisk (const Guid *aId, const BSTR aLocation,
3219 bool aSetError, ComObjPtr <HardDisk> *aHardDisk /* = NULL */)
3220{
3221 ComAssertRet (aId || aLocation, E_INVALIDARG);
3222
3223 AutoReadLock alock (this);
3224
3225 /* first lookup the map by UUID if UUID is provided */
3226 if (aId)
3227 {
3228 HardDiskMap::const_iterator it = mData.mHardDiskMap.find (*aId);
3229 if (it != mData.mHardDiskMap.end())
3230 {
3231 if (aHardDisk)
3232 *aHardDisk = (*it).second;
3233 return S_OK;
3234 }
3235 }
3236
3237 /* then iterate and find by location */
3238 bool found = false;
3239 if (aLocation)
3240 {
3241 Utf8Str location = aLocation;
3242
3243 for (HardDiskMap::const_iterator it = mData.mHardDiskMap.begin();
3244 !found && it != mData.mHardDiskMap.end();
3245 ++ it)
3246 {
3247 const ComObjPtr <HardDisk> &hd = (*it).second;
3248 AutoReadLock hdLock (hd);
3249
3250 if (hd->storageType() == HardDiskStorageType_VirtualDiskImage ||
3251 hd->storageType() == HardDiskStorageType_VMDKImage ||
3252 hd->storageType() == HardDiskStorageType_VHDImage)
3253 {
3254 /* locations of VDI and VMDK hard disks for now are just
3255 * file paths */
3256 found = RTPathCompare (location,
3257 Utf8Str (hd->toString
3258 (false /* aShort */))) == 0;
3259 }
3260 else
3261 {
3262 found = aLocation == hd->toString (false /* aShort */);
3263 }
3264
3265 if (found && aHardDisk)
3266 *aHardDisk = hd;
3267 }
3268 }
3269
3270 HRESULT rc = found ? S_OK : E_INVALIDARG;
3271
3272 if (aSetError && !found)
3273 {
3274 if (aId && !aLocation)
3275 setError (rc, tr ("Could not find a registered hard disk "
3276 "with UUID {%Vuuid}"), aId->raw());
3277 else if (aLocation && !aId)
3278 setError (rc, tr ("Could not find a registered hard disk "
3279 "with location '%ls'"), aLocation);
3280 }
3281
3282 return rc;
3283}
3284
3285/**
3286 * @deprecated Use #findHardDisk() instead.
3287 *
3288 * Searches for a HVirtualDiskImage object with the given ID or file path in the
3289 * collection of registered hard disks. If both ID and file path are specified,
3290 * the first object that matches either of them (not necessarily both)
3291 * is returned.
3292 *
3293 * @param aId ID of the hard disk (NULL when unused)
3294 * @param filePathFull full path to the image file (NULL when unused)
3295 * @param aSetError if TRUE, the appropriate error info is set in case when
3296 * the disk is not found and only one search criteria (ID
3297 * or file name) is specified.
3298 * @param aHardDisk where to store the found hard disk object (can be NULL)
3299 *
3300 * @return
3301 * S_OK when found or E_INVALIDARG when not found
3302 *
3303 * @note Locks objects for reading!
3304 */
3305HRESULT VirtualBox::
3306findVirtualDiskImage (const Guid *aId, const BSTR aFilePathFull,
3307 bool aSetError, ComObjPtr <HVirtualDiskImage> *aImage /* = NULL */)
3308{
3309 ComAssertRet (aId || aFilePathFull, E_INVALIDARG);
3310
3311 AutoReadLock alock (this);
3312
3313 /* first lookup the map by UUID if UUID is provided */
3314 if (aId)
3315 {
3316 HardDiskMap::const_iterator it = mData.mHardDiskMap.find (*aId);
3317 if (it != mData.mHardDiskMap.end())
3318 {
3319 AutoReadLock hdLock ((*it).second);
3320 if ((*it).second->storageType() == HardDiskStorageType_VirtualDiskImage)
3321 {
3322 if (aImage)
3323 *aImage = (*it).second->asVDI();
3324 return S_OK;
3325 }
3326 }
3327 }
3328
3329 /* then iterate and find by name */
3330 bool found = false;
3331 if (aFilePathFull)
3332 {
3333 for (HardDiskMap::const_iterator it = mData.mHardDiskMap.begin();
3334 !found && it != mData.mHardDiskMap.end();
3335 ++ it)
3336 {
3337 const ComObjPtr <HardDisk> &hd = (*it).second;
3338 AutoReadLock hdLock (hd);
3339 if (hd->storageType() != HardDiskStorageType_VirtualDiskImage)
3340 continue;
3341
3342 found = RTPathCompare (Utf8Str (aFilePathFull),
3343 Utf8Str (hd->asVDI()->filePathFull())) == 0;
3344 if (found && aImage)
3345 *aImage = hd->asVDI();
3346 }
3347 }
3348
3349 HRESULT rc = found ? S_OK : E_INVALIDARG;
3350
3351 if (aSetError && !found)
3352 {
3353 if (aId && !aFilePathFull)
3354 setError (rc, tr ("Could not find a registered VDI hard disk "
3355 "with UUID {%Vuuid}"), aId->raw());
3356 else if (aFilePathFull && !aId)
3357 setError (rc, tr ("Could not find a registered VDI hard disk "
3358 "with the file path '%ls'"), aFilePathFull);
3359 }
3360
3361 return rc;
3362}
3363
3364/**
3365 * Searches for a DVDImage object with the given ID or file path in the
3366 * collection of registered DVD images. If both ID and file path are specified,
3367 * the first object that matches either of them (not necessarily both)
3368 * is returned.
3369 *
3370 * @param aId
3371 * ID of the DVD image (unused when NULL)
3372 * @param aFilePathFull
3373 * full path to the image file (unused when NULL)
3374 * @param aSetError
3375 * if TRUE, the appropriate error info is set in case when the image is not
3376 * found and only one search criteria (ID or file name) is specified.
3377 * @param aImage
3378 * where to store the found DVD image object (can be NULL)
3379 *
3380 * @return
3381 * S_OK when found or E_INVALIDARG when not found
3382 *
3383 * @note Locks this object for reading.
3384 */
3385HRESULT VirtualBox::findDVDImage (const Guid *aId, const BSTR aFilePathFull,
3386 bool aSetError,
3387 ComObjPtr <DVDImage> *aImage /* = NULL */)
3388{
3389 ComAssertRet (aId || aFilePathFull, E_INVALIDARG);
3390
3391 bool found = false;
3392
3393 {
3394 AutoReadLock alock (this);
3395
3396 for (DVDImageList::const_iterator it = mData.mDVDImages.begin();
3397 !found && it != mData.mDVDImages.end();
3398 ++ it)
3399 {
3400 /* DVDImage fields are constant, so no need to lock */
3401 found = (aId && (*it)->id() == *aId) ||
3402 (aFilePathFull &&
3403 RTPathCompare (Utf8Str (aFilePathFull),
3404 Utf8Str ((*it)->filePathFull())) == 0);
3405 if (found && aImage)
3406 *aImage = *it;
3407 }
3408 }
3409
3410 HRESULT rc = found ? S_OK : E_INVALIDARG;
3411
3412 if (aSetError && !found)
3413 {
3414 if (aId && !aFilePathFull)
3415 setError (rc, tr ("Could not find a registered CD/DVD image "
3416 "with UUID {%s}"), aId->toString().raw());
3417 else if (aFilePathFull && !aId)
3418 setError (rc, tr ("Could not find a registered CD/DVD image "
3419 "with the file path '%ls'"), aFilePathFull);
3420 }
3421
3422 return rc;
3423}
3424
3425/**
3426 * Searches for a FloppyImage object with the given ID or file path in the
3427 * collection of registered floppy images. If both ID and file path are specified,
3428 * the first object that matches either of them (not necessarily both)
3429 * is returned.
3430 *
3431 * @param aId
3432 * ID of the floppy image (unused when NULL)
3433 * @param aFilePathFull
3434 * full path to the image file (unused when NULL)
3435 * @param aSetError
3436 * if TRUE, the appropriate error info is set in case when the image is not
3437 * found and only one search criteria (ID or file name) is specified.
3438 * @param aImage
3439 * where to store the found floppy image object (can be NULL)
3440 *
3441 * @return
3442 * S_OK when found or E_INVALIDARG when not found
3443 *
3444 * @note Locks this object for reading.
3445 */
3446HRESULT VirtualBox::findFloppyImage (const Guid *aId, const BSTR aFilePathFull,
3447 bool aSetError,
3448 ComObjPtr <FloppyImage> *aImage /* = NULL */)
3449{
3450 ComAssertRet (aId || aFilePathFull, E_INVALIDARG);
3451
3452 bool found = false;
3453
3454 {
3455 AutoReadLock alock (this);
3456
3457 for (FloppyImageList::iterator it = mData.mFloppyImages.begin();
3458 !found && it != mData.mFloppyImages.end();
3459 ++ it)
3460 {
3461 /* FloppyImage fields are constant, so no need to lock */
3462 found = (aId && (*it)->id() == *aId) ||
3463 (aFilePathFull &&
3464 RTPathCompare (Utf8Str (aFilePathFull),
3465 Utf8Str ((*it)->filePathFull())) == 0);
3466 if (found && aImage)
3467 *aImage = *it;
3468 }
3469 }
3470
3471 HRESULT rc = found ? S_OK : E_INVALIDARG;
3472
3473 if (aSetError && !found)
3474 {
3475 if (aId && !aFilePathFull)
3476 setError (rc, tr ("Could not find a registered floppy image "
3477 "with UUID {%s}"), aId->toString().raw());
3478 else if (aFilePathFull && !aId)
3479 setError (rc, tr ("Could not find a registered floppy image "
3480 "with the file path '%ls'"), aFilePathFull);
3481 }
3482
3483 return rc;
3484}
3485
3486/**
3487 * When \a aHardDisk is not NULL, searches for an object equal to the given
3488 * hard disk in the collection of registered hard disks, or, if the given hard
3489 * disk is HVirtualDiskImage, for an object with the given file path in the
3490 * collection of all registered non-hard disk images (DVDs and floppies).
3491 * Other parameters are unused.
3492 *
3493 * When \a aHardDisk is NULL, searches for an object with the given ID or file
3494 * path in the collection of all registered images (VDIs, DVDs and floppies).
3495 * If both ID and file path are specified, matching either of them will satisfy
3496 * the search.
3497 *
3498 * If a matching object is found, this method returns E_INVALIDARG and sets the
3499 * appropriate error info. Otherwise, S_OK is returned.
3500 *
3501 * @param aHardDisk hard disk object to check against registered media
3502 * (NULL when unused)
3503 * @param aId UUID of the media to check (NULL when unused)
3504 * @param aFilePathFull full path to the image file (NULL when unused)
3505 *
3506 * @note Locks objects!
3507 */
3508HRESULT VirtualBox::checkMediaForConflicts (HardDisk *aHardDisk,
3509 const Guid *aId,
3510 const BSTR aFilePathFull)
3511{
3512 AssertReturn (aHardDisk || aId || aFilePathFull, E_FAIL);
3513
3514 HRESULT rc = S_OK;
3515
3516 AutoReadLock alock (this);
3517
3518 if (aHardDisk)
3519 {
3520 for (HardDiskMap::const_iterator it = mData.mHardDiskMap.begin();
3521 it != mData.mHardDiskMap.end();
3522 ++ it)
3523 {
3524 const ComObjPtr <HardDisk> &hd = (*it).second;
3525 if (hd->sameAs (aHardDisk))
3526 return setError (E_INVALIDARG,
3527 tr ("A hard disk with UUID {%Vuuid} or with the same properties "
3528 "('%ls') is already registered"),
3529 aHardDisk->id().raw(), aHardDisk->toString().raw());
3530 }
3531
3532 aId = &aHardDisk->id();
3533 if (aHardDisk->storageType() == HardDiskStorageType_VirtualDiskImage)
3534#if !defined (VBOX_WITH_XPCOM)
3535 /// @todo (dmik) stupid BSTR declaration lacks the BCSTR counterpart
3536 const_cast <BSTR> (aFilePathFull) = aHardDisk->asVDI()->filePathFull();
3537#else
3538 aFilePathFull = aHardDisk->asVDI()->filePathFull();
3539#endif
3540 }
3541
3542 bool found = false;
3543
3544 if (aId || aFilePathFull) do
3545 {
3546 if (!aHardDisk)
3547 {
3548 rc = findHardDisk (aId, aFilePathFull, false /* aSetError */);
3549 found = SUCCEEDED (rc);
3550 if (found)
3551 break;
3552 }
3553
3554 rc = findDVDImage (aId, aFilePathFull, false /* aSetError */);
3555 found = SUCCEEDED (rc);
3556 if (found)
3557 break;
3558
3559 rc = findFloppyImage (aId, aFilePathFull, false /* aSetError */);
3560 found = SUCCEEDED (rc);
3561 if (found)
3562 break;
3563 }
3564 while (0);
3565
3566 if (found)
3567 {
3568 if (aId && !aFilePathFull)
3569 rc = setError (E_INVALIDARG,
3570 tr ("A disk image with UUID {%Vuuid} is already registered"),
3571 aId->raw());
3572 else if (aFilePathFull && !aId)
3573 rc = setError (E_INVALIDARG,
3574 tr ("A disk image with file path '%ls' is already registered"),
3575 aFilePathFull);
3576 else
3577 rc = setError (E_INVALIDARG,
3578 tr ("A disk image with UUID {%Vuuid} or file path '%ls' "
3579 "is already registered"), aId->raw(), aFilePathFull);
3580 }
3581 else
3582 rc = S_OK;
3583
3584 return rc;
3585}
3586
3587/**
3588 * Reads in the machine definitions from the configuration loader
3589 * and creates the relevant objects.
3590 *
3591 * @param aGlobal <Global> node.
3592 *
3593 * @note Can be called only from #init().
3594 * @note Doesn't lock anything.
3595 */
3596HRESULT VirtualBox::loadMachines (const settings::Key &aGlobal)
3597{
3598 using namespace settings;
3599
3600 AutoCaller autoCaller (this);
3601 AssertReturn (autoCaller.state() == InInit, E_FAIL);
3602
3603 HRESULT rc = S_OK;
3604
3605 Key::List machines = aGlobal.key ("MachineRegistry").keys ("MachineEntry");
3606 for (Key::List::const_iterator it = machines.begin();
3607 it != machines.end(); ++ it)
3608 {
3609 /* required */
3610 Guid uuid = (*it).value <Guid> ("uuid");
3611 /* required */
3612 Bstr src = (*it).stringValue ("src");
3613
3614 /* create a new machine object */
3615 ComObjPtr <Machine> machine;
3616 rc = machine.createObject();
3617 if (SUCCEEDED (rc))
3618 {
3619 /* initialize the machine object and register it */
3620 rc = machine->init (this, src, Machine::Init_Registered,
3621 NULL, FALSE, &uuid);
3622 if (SUCCEEDED (rc))
3623 rc = registerMachine (machine);
3624 }
3625 }
3626
3627 return rc;
3628}
3629
3630/**
3631 * Reads in the disk registration entries from the global settings file
3632 * and creates the relevant objects
3633 *
3634 * @param aGlobal <Global> node
3635 *
3636 * @note Can be called only from #init().
3637 * @note Doesn't lock anything.
3638 */
3639HRESULT VirtualBox::loadDisks (const settings::Key &aGlobal)
3640{
3641 using namespace settings;
3642
3643 AutoCaller autoCaller (this);
3644 AssertReturn (autoCaller.state() == InInit, E_FAIL);
3645
3646 HRESULT rc = S_OK;
3647
3648 Key registry = aGlobal.key ("DiskRegistry");
3649
3650 const char *kMediaNodes[] = { "HardDisks", "DVDImages", "FloppyImages" };
3651
3652 for (size_t n = 0; n < ELEMENTS (kMediaNodes); ++ n)
3653 {
3654 /* All three media nodes are optional */
3655 Key node = registry.findKey (kMediaNodes [n]);
3656 if (node.isNull())
3657 continue;
3658
3659 if (n == 0)
3660 {
3661 /* HardDisks node */
3662 rc = loadHardDisks (node);
3663 continue;
3664 }
3665
3666 Key::List images = node.keys ("Image");
3667 for (Key::List::const_iterator it = images.begin();
3668 it != images.end(); ++ it)
3669 {
3670 /* required */
3671 Guid uuid = (*it).value <Guid> ("uuid");
3672 /* required */
3673 Bstr src = (*it).stringValue ("src");
3674
3675 switch (n)
3676 {
3677 case 1: /* DVDImages */
3678 {
3679 ComObjPtr <DVDImage> image;
3680 image.createObject();
3681 rc = image->init (this, src, TRUE /* isRegistered */, uuid);
3682 if (SUCCEEDED (rc))
3683 rc = registerDVDImage (image, TRUE /* aOnStartUp */);
3684
3685 break;
3686 }
3687 case 2: /* FloppyImages */
3688 {
3689 ComObjPtr <FloppyImage> image;
3690 image.createObject();
3691 rc = image->init (this, src, TRUE /* isRegistered */, uuid);
3692 if (SUCCEEDED (rc))
3693 rc = registerFloppyImage (image, TRUE /* aOnStartUp */);
3694
3695 break;
3696 }
3697 default:
3698 AssertFailed();
3699 }
3700
3701 CheckComRCBreakRC (rc);
3702 }
3703
3704 CheckComRCBreakRC (rc);
3705 }
3706
3707 return rc;
3708}
3709
3710/**
3711 * Loads all hard disks from the given <HardDisks> node.
3712 * Note that all loaded hard disks register themselves within this VirtualBox.
3713 *
3714 * @param aNode <HardDisks> node.
3715 *
3716 * @note Can be called only from #init().
3717 * @note Doesn't lock anything.
3718 */
3719HRESULT VirtualBox::loadHardDisks (const settings::Key &aNode)
3720{
3721 using namespace settings;
3722
3723 AutoCaller autoCaller (this);
3724 AssertReturn (autoCaller.state() == InInit, E_FAIL);
3725
3726 AssertReturn (!aNode.isNull(), E_INVALIDARG);
3727
3728 HRESULT rc = S_OK;
3729
3730 Key::List disks = aNode.keys ("HardDisk");
3731 for (Key::List::const_iterator it = disks.begin();
3732 it != disks.end(); ++ it)
3733 {
3734 Key storageNode;
3735
3736 /* detect the type of the hard disk (either one of VirtualDiskImage,
3737 * ISCSIHardDisk, VMDKImage or HCustomHardDisk) */
3738
3739 do
3740 {
3741 storageNode = (*it).findKey ("VirtualDiskImage");
3742 if (!storageNode.isNull())
3743 {
3744 ComObjPtr <HVirtualDiskImage> vdi;
3745 vdi.createObject();
3746 rc = vdi->init (this, NULL, (*it), storageNode);
3747 break;
3748 }
3749
3750 storageNode = (*it).findKey ("ISCSIHardDisk");
3751 if (!storageNode.isNull())
3752 {
3753 ComObjPtr <HISCSIHardDisk> iscsi;
3754 iscsi.createObject();
3755 rc = iscsi->init (this, (*it), storageNode);
3756 break;
3757 }
3758
3759 storageNode = (*it).findKey ("VMDKImage");
3760 if (!storageNode.isNull())
3761 {
3762 ComObjPtr <HVMDKImage> vmdk;
3763 vmdk.createObject();
3764 rc = vmdk->init (this, NULL, (*it), storageNode);
3765 break;
3766 }
3767
3768 storageNode = (*it).findKey ("CustomHardDisk");
3769 if (!storageNode.isNull())
3770 {
3771 ComObjPtr <HCustomHardDisk> custom;
3772 custom.createObject();
3773 rc = custom->init (this, NULL, (*it), storageNode);
3774 break;
3775 }
3776
3777 storageNode = (*it).findKey ("VHDImage");
3778 if (!storageNode.isNull())
3779 {
3780 ComObjPtr <HVHDImage> vhd;
3781 vhd.createObject();
3782 rc = vhd->init (this, NULL, (*it), storageNode);
3783 break;
3784 }
3785
3786 ComAssertMsgFailedBreak (("No valid hard disk storage node!\n"),
3787 rc = E_FAIL);
3788 }
3789 while (0);
3790 }
3791
3792 return rc;
3793}
3794
3795/**
3796 * Helper function to write out the configuration tree.
3797 *
3798 * @note Locks this object for writing and child objects for reading/writing!
3799 */
3800HRESULT VirtualBox::saveSettings()
3801{
3802 AutoCaller autoCaller (this);
3803 AssertComRCReturn (autoCaller.rc(), autoCaller.rc());
3804
3805 AssertReturn (!!mData.mCfgFile.mName, E_FAIL);
3806
3807 HRESULT rc = S_OK;
3808
3809 /* serialize file access (prevent concurrent reads and writes) */
3810 AutoWriteLock alock (this);
3811
3812 try
3813 {
3814 using namespace settings;
3815
3816 /* load the settings file */
3817 File file (mData.mCfgFile.mHandle, Utf8Str (mData.mCfgFile.mName));
3818 XmlTreeBackend tree;
3819
3820 rc = VirtualBox::loadSettingsTree_ForUpdate (tree, file);
3821 CheckComRCThrowRC (rc);
3822
3823 Key global = tree.rootKey().createKey ("Global");
3824
3825 /* machines */
3826 {
3827 /* first, delete the entire machine registry */
3828 Key registryNode = global.findKey ("MachineRegistry");
3829 if (!registryNode.isNull())
3830 registryNode.zap();
3831 /* then, recreate it */
3832 registryNode = global.createKey ("MachineRegistry");
3833
3834 /* write out the machines */
3835 for (MachineList::iterator it = mData.mMachines.begin();
3836 it != mData.mMachines.end();
3837 ++ it)
3838 {
3839 Key entryNode = registryNode.appendKey ("MachineEntry");
3840 rc = (*it)->saveRegistryEntry (entryNode);
3841 CheckComRCThrowRC (rc);
3842 }
3843 }
3844
3845 /* disk images */
3846 {
3847 /* first, delete the entire disk image registr */
3848 Key registryNode = global.findKey ("DiskRegistry");
3849 if (!registryNode.isNull())
3850 registryNode.zap();
3851 /* then, recreate it */
3852 registryNode = global.createKey ("DiskRegistry");
3853
3854 /* write out the hard disks */
3855 {
3856 Key imagesNode = registryNode.createKey ("HardDisks");
3857 rc = saveHardDisks (imagesNode);
3858 CheckComRCThrowRC (rc);
3859 }
3860
3861 /* write out the CD/DVD images */
3862 {
3863 Key imagesNode = registryNode.createKey ("DVDImages");
3864
3865 for (DVDImageList::iterator it = mData.mDVDImages.begin();
3866 it != mData.mDVDImages.end();
3867 ++ it)
3868 {
3869 ComObjPtr <DVDImage> dvd = *it;
3870 /* no need to lock: fields are constant */
3871 Key imageNode = imagesNode.appendKey ("Image");
3872 imageNode.setValue <Guid> ("uuid", dvd->id());
3873 imageNode.setValue <Bstr> ("src", dvd->filePath());
3874 }
3875 }
3876
3877 /* write out the floppy images */
3878 {
3879 Key imagesNode = registryNode.createKey ("FloppyImages");
3880
3881 for (FloppyImageList::iterator it = mData.mFloppyImages.begin();
3882 it != mData.mFloppyImages.end();
3883 ++ it)
3884 {
3885 ComObjPtr <FloppyImage> fd = *it;
3886 /* no need to lock: fields are constant */
3887 Key imageNode = imagesNode.appendKey ("Image");
3888 imageNode.setValue <Guid> ("uuid", fd->id());
3889 imageNode.setValue <Bstr> ("src", fd->filePath());
3890 }
3891 }
3892 }
3893
3894 /* host data (USB filters) */
3895 rc = mData.mHost->saveSettings (global);
3896 CheckComRCThrowRC (rc);
3897
3898 rc = mData.mSystemProperties->saveSettings (global);
3899 CheckComRCThrowRC (rc);
3900
3901 /* save the settings on success */
3902 rc = VirtualBox::saveSettingsTree (tree, file,
3903 mData.mSettingsFileVersion);
3904 CheckComRCThrowRC (rc);
3905 }
3906 catch (HRESULT err)
3907 {
3908 /* we assume that error info is set by the thrower */
3909 rc = err;
3910 }
3911 catch (...)
3912 {
3913 rc = VirtualBox::handleUnexpectedExceptions (RT_SRC_POS);
3914 }
3915
3916 return rc;
3917}
3918
3919/**
3920 * Saves all hard disks to the given <HardDisks> node.
3921 *
3922 * @param aNode <HardDisks> node.
3923 *
3924 * @note Locks this object for reding.
3925 */
3926HRESULT VirtualBox::saveHardDisks (settings::Key &aNode)
3927{
3928 using namespace settings;
3929
3930 AssertReturn (!aNode.isNull(), E_INVALIDARG);
3931
3932 HRESULT rc = S_OK;
3933
3934 AutoReadLock alock (this);
3935
3936 for (HardDiskList::const_iterator it = mData.mHardDisks.begin();
3937 it != mData.mHardDisks.end();
3938 ++ it)
3939 {
3940 ComObjPtr <HardDisk> hd = *it;
3941 AutoReadLock hdLock (hd);
3942
3943 Key hdNode = aNode.appendKey ("HardDisk");
3944
3945 switch (hd->storageType())
3946 {
3947 case HardDiskStorageType_VirtualDiskImage:
3948 {
3949 Key storageNode = hdNode.createKey ("VirtualDiskImage");
3950 rc = hd->saveSettings (hdNode, storageNode);
3951 break;
3952 }
3953
3954 case HardDiskStorageType_ISCSIHardDisk:
3955 {
3956 Key storageNode = hdNode.createKey ("ISCSIHardDisk");
3957 rc = hd->saveSettings (hdNode, storageNode);
3958 break;
3959 }
3960
3961 case HardDiskStorageType_VMDKImage:
3962 {
3963 Key storageNode = hdNode.createKey ("VMDKImage");
3964 rc = hd->saveSettings (hdNode, storageNode);
3965 break;
3966 }
3967 case HardDiskStorageType_CustomHardDisk:
3968 {
3969 Key storageNode = hdNode.createKey ("CustomHardDisk");
3970 rc = hd->saveSettings (hdNode, storageNode);
3971 break;
3972 }
3973
3974 case HardDiskStorageType_VHDImage:
3975 {
3976 Key storageNode = hdNode.createKey ("VHDImage");
3977 rc = hd->saveSettings (hdNode, storageNode);
3978 break;
3979 }
3980 }
3981
3982 CheckComRCBreakRC (rc);
3983 }
3984
3985 return rc;
3986}
3987
3988/**
3989 * Helper to register the machine.
3990 *
3991 * When called during VirtualBox startup, adds the given machine to the
3992 * collection of registered machines. Otherwise tries to mark the machine
3993 * as registered, and, if succeeded, adds it to the collection and
3994 * saves global settings.
3995 *
3996 * @note The caller must have added itself as a caller of the @a aMachine
3997 * object if calls this method not on VirtualBox startup.
3998 *
3999 * @param aMachine machine to register
4000 *
4001 * @note Locks objects!
4002 */
4003HRESULT VirtualBox::registerMachine (Machine *aMachine)
4004{
4005 ComAssertRet (aMachine, E_INVALIDARG);
4006
4007 AutoCaller autoCaller (this);
4008 CheckComRCReturnRC (autoCaller.rc());
4009
4010 AutoWriteLock alock (this);
4011
4012 HRESULT rc = S_OK;
4013
4014 {
4015 ComObjPtr <Machine> m;
4016 rc = findMachine (aMachine->uuid(), false /* aDoSetError */, &m);
4017 if (SUCCEEDED (rc))
4018 {
4019 /* sanity */
4020 AutoLimitedCaller machCaller (m);
4021 AssertComRC (machCaller.rc());
4022
4023 return setError (E_INVALIDARG,
4024 tr ("Registered machine with UUID {%Vuuid} ('%ls') already exists"),
4025 aMachine->uuid().raw(), m->settingsFileFull().raw());
4026 }
4027
4028 ComAssertRet (rc == E_INVALIDARG, rc);
4029 rc = S_OK;
4030 }
4031
4032 if (autoCaller.state() != InInit)
4033 {
4034 /* Machine::trySetRegistered() will commit and save machine settings */
4035 rc = aMachine->trySetRegistered (TRUE);
4036 CheckComRCReturnRC (rc);
4037 }
4038
4039 /* add to the collection of registered machines */
4040 mData.mMachines.push_back (aMachine);
4041
4042 if (autoCaller.state() != InInit)
4043 rc = saveSettings();
4044
4045 return rc;
4046}
4047
4048/**
4049 * Helper to register the hard disk.
4050 *
4051 * @param aHardDisk object to register
4052 * @param aFlags one of RHD_* values
4053 *
4054 * @note Locks objects!
4055 */
4056HRESULT VirtualBox::registerHardDisk (HardDisk *aHardDisk, RHD_Flags aFlags)
4057{
4058 ComAssertRet (aHardDisk, E_INVALIDARG);
4059
4060 AutoCaller autoCaller (this);
4061 AssertComRCReturn (autoCaller.rc(), autoCaller.rc());
4062
4063 AutoWriteLock alock (this);
4064
4065 HRESULT rc = checkMediaForConflicts (aHardDisk, NULL, NULL);
4066 CheckComRCReturnRC (rc);
4067
4068 /* mark the hard disk as registered only when registration is external */
4069 if (aFlags == RHD_External)
4070 {
4071 rc = aHardDisk->trySetRegistered (TRUE);
4072 CheckComRCReturnRC (rc);
4073 }
4074
4075 if (!aHardDisk->parent())
4076 {
4077 /* add to the collection of top-level images */
4078 mData.mHardDisks.push_back (aHardDisk);
4079 }
4080
4081 /* insert to the map of hard disks */
4082 mData.mHardDiskMap
4083 .insert (HardDiskMap::value_type (aHardDisk->id(), aHardDisk));
4084
4085 /* save global config file if not on startup */
4086 /// @todo (dmik) optimize later to save only the <HardDisks> node
4087 if (aFlags != RHD_OnStartUp)
4088 rc = saveSettings();
4089
4090 return rc;
4091}
4092
4093/**
4094 * Helper to unregister the hard disk.
4095 *
4096 * If the hard disk is a differencing hard disk and if the unregistration
4097 * succeeds, the hard disk image is deleted and the object is uninitialized.
4098 *
4099 * @param aHardDisk hard disk to unregister
4100 *
4101 * @note Locks objects!
4102 */
4103HRESULT VirtualBox::unregisterHardDisk (HardDisk *aHardDisk)
4104{
4105 AssertReturn (aHardDisk, E_INVALIDARG);
4106
4107 AutoCaller autoCaller (this);
4108 AssertComRCReturn (autoCaller.rc(), autoCaller.rc());
4109
4110 LogFlowThisFunc (("image='%ls'\n", aHardDisk->toString().raw()));
4111
4112 AutoWriteLock alock (this);
4113
4114 /* Lock the hard disk to ensure nobody registers it again before we delete
4115 * the differencing image (sanity check actually -- should never happen). */
4116 AutoWriteLock hdLock (aHardDisk);
4117
4118 /* try to unregister */
4119 HRESULT rc = aHardDisk->trySetRegistered (FALSE);
4120 CheckComRCReturnRC (rc);
4121
4122 /* remove from the map of hard disks */
4123 mData.mHardDiskMap.erase (aHardDisk->id());
4124
4125 if (!aHardDisk->parent())
4126 {
4127 /* non-differencing hard disk:
4128 * remove from the collection of top-level hard disks */
4129 mData.mHardDisks.remove (aHardDisk);
4130 }
4131 else
4132 {
4133 Assert (aHardDisk->isDifferencing());
4134
4135 /* differencing hard disk: delete and uninitialize
4136 *
4137 * Note that we ignore errors because this operation may be a result
4138 * of unregistering a missing (inaccessible) differencing hard disk
4139 * in which case a failure to implicitly delete the image will not
4140 * prevent it from being unregistered and therefore should not pop up
4141 * on the caller's side. */
4142 rc = aHardDisk->asVDI()->deleteImage (true /* aIgnoreErrors*/);
4143 aHardDisk->uninit();
4144 }
4145
4146 /* save the global config file anyway (already unregistered) */
4147 /// @todo (dmik) optimize later to save only the <HardDisks> node
4148 HRESULT rc2 = saveSettings();
4149 if (SUCCEEDED (rc))
4150 rc = rc2;
4151
4152 return rc;
4153}
4154
4155/**
4156 * Helper to unregister the differencing hard disk image.
4157 * Resets machine ID of the hard disk (to let the unregistration succeed)
4158 * and then calls #unregisterHardDisk().
4159 *
4160 * @param aHardDisk differencing hard disk image to unregister
4161 *
4162 * @note Locks objects!
4163 */
4164HRESULT VirtualBox::unregisterDiffHardDisk (HardDisk *aHardDisk)
4165{
4166 AssertReturn (aHardDisk, E_INVALIDARG);
4167
4168 AutoCaller autoCaller (this);
4169 AssertComRCReturn (autoCaller.rc(), autoCaller.rc());
4170
4171 AutoWriteLock alock (this);
4172
4173 /*
4174 * Note: it's safe to lock aHardDisk here because the same object
4175 * will be locked by #unregisterHardDisk().
4176 */
4177 AutoWriteLock hdLock (aHardDisk);
4178
4179 AssertReturn (aHardDisk->isDifferencing(), E_INVALIDARG);
4180
4181 /*
4182 * deassociate the machine from the hard disk
4183 * (otherwise trySetRegistered() will definitely fail)
4184 */
4185 aHardDisk->setMachineId (Guid());
4186
4187 return unregisterHardDisk (aHardDisk);
4188}
4189
4190
4191/**
4192 * Helper to update the global settings file when the name of some machine
4193 * changes so that file and directory renaming occurs. This method ensures
4194 * that all affected paths in the disk registry are properly updated.
4195 *
4196 * @param aOldPath old path (full)
4197 * @param aNewPath new path (full)
4198 *
4199 * @note Locks this object + DVD, Floppy and HardDisk children for writing.
4200 */
4201HRESULT VirtualBox::updateSettings (const char *aOldPath, const char *aNewPath)
4202{
4203 LogFlowThisFunc (("aOldPath={%s} aNewPath={%s}\n", aOldPath, aNewPath));
4204
4205 AssertReturn (aOldPath, E_INVALIDARG);
4206 AssertReturn (aNewPath, E_INVALIDARG);
4207
4208 AutoCaller autoCaller (this);
4209 AssertComRCReturn (autoCaller.rc(), autoCaller.rc());
4210
4211 AutoWriteLock alock (this);
4212
4213 size_t oldPathLen = strlen (aOldPath);
4214
4215 /* check DVD paths */
4216 for (DVDImageList::iterator it = mData.mDVDImages.begin();
4217 it != mData.mDVDImages.end();
4218 ++ it)
4219 {
4220 ComObjPtr <DVDImage> image = *it;
4221
4222 /* no need to lock: fields are constant */
4223 Utf8Str path = image->filePathFull();
4224 LogFlowThisFunc (("DVD.fullPath={%s}\n", path.raw()));
4225
4226 if (RTPathStartsWith (path, aOldPath))
4227 {
4228 Utf8Str newPath = Utf8StrFmt ("%s%s", aNewPath,
4229 path.raw() + oldPathLen);
4230 path = newPath;
4231 calculateRelativePath (path, path);
4232 image->updatePath (newPath, path);
4233
4234 LogFlowThisFunc (("-> updated: full={%s} rel={%s}\n",
4235 newPath.raw(), path.raw()));
4236 }
4237 }
4238
4239 /* check Floppy paths */
4240 for (FloppyImageList::iterator it = mData.mFloppyImages.begin();
4241 it != mData.mFloppyImages.end();
4242 ++ it)
4243 {
4244 ComObjPtr <FloppyImage> image = *it;
4245
4246 /* no need to lock: fields are constant */
4247 Utf8Str path = image->filePathFull();
4248 LogFlowThisFunc (("Floppy.fullPath={%s}\n", path.raw()));
4249
4250 if (RTPathStartsWith (path, aOldPath))
4251 {
4252 Utf8Str newPath = Utf8StrFmt ("%s%s", aNewPath,
4253 path.raw() + oldPathLen);
4254 path = newPath;
4255 calculateRelativePath (path, path);
4256 image->updatePath (newPath, path);
4257
4258 LogFlowThisFunc (("-> updated: full={%s} rel={%s}\n",
4259 newPath.raw(), path.raw()));
4260 }
4261 }
4262
4263 /* check HardDisk paths */
4264 for (HardDiskList::const_iterator it = mData.mHardDisks.begin();
4265 it != mData.mHardDisks.end();
4266 ++ it)
4267 {
4268 (*it)->updatePaths (aOldPath, aNewPath);
4269 }
4270
4271 HRESULT rc = saveSettings();
4272
4273 return rc;
4274}
4275
4276/**
4277 * Helper method to load the setting tree and turn expected exceptions into
4278 * COM errors, according to arguments.
4279 *
4280 * Note that this method will not catch unexpected errors so it may still
4281 * throw something.
4282 *
4283 * @param aTree Tree to load into settings.
4284 * @param aFile File to load settings from.
4285 * @param aValidate @c @true to enable tree validation.
4286 * @param aCatchLoadErrors @c true to catch exceptions caused by file
4287 * access or validation errors.
4288 * @param aAddDefaults @c true to cause the substitution of default
4289 * values for for missing attributes that have
4290 * defaults in the XML schema.
4291 * @param aFormatVersion Where to store the current format version of the
4292 * loaded settings tree (optional, may be NULL).
4293 */
4294/* static */
4295HRESULT VirtualBox::loadSettingsTree (settings::XmlTreeBackend &aTree,
4296 settings::File &aFile,
4297 bool aValidate,
4298 bool aCatchLoadErrors,
4299 bool aAddDefaults,
4300 Utf8Str *aFormatVersion /* = NULL */)
4301{
4302 using namespace settings;
4303
4304 try
4305 {
4306 SettingsTreeHelper helper = SettingsTreeHelper();
4307
4308 aTree.setInputResolver (helper);
4309 aTree.setAutoConverter (helper);
4310
4311 aTree.read (aFile, aValidate ? VBOX_XML_SCHEMA : NULL,
4312 aAddDefaults ? XmlTreeBackend::Read_AddDefaults : 0);
4313
4314 aTree.resetAutoConverter();
4315 aTree.resetInputResolver();
4316
4317 /* on success, memorize the current settings file version or set it to
4318 * the most recent version if no settings conversion took place. Note
4319 * that it's not necessary to do it every time we load the settings file
4320 * (i.e. only loadSettingsTree_FirstTime() passes a non-NULL
4321 * aFormatVersion value) because currently we keep the settings
4322 * files locked so that the only legal way to change the format version
4323 * while VirtualBox is running is saveSettingsTree(). */
4324 if (aFormatVersion != NULL)
4325 {
4326 *aFormatVersion = aTree.oldVersion();
4327 if (aFormatVersion->isNull())
4328 *aFormatVersion = VBOX_XML_VERSION_FULL;
4329 }
4330 }
4331 catch (const EIPRTFailure &err)
4332 {
4333 if (!aCatchLoadErrors)
4334 throw;
4335
4336 return setError (E_FAIL,
4337 tr ("Could not load the settings file '%s' (%Vrc)"),
4338 aFile.uri(), err.rc());
4339 }
4340 catch (const XmlTreeBackend::Error &err)
4341 {
4342 Assert (err.what() != NULL);
4343
4344 if (!aCatchLoadErrors)
4345 throw;
4346
4347 return setError (E_FAIL,
4348 tr ("Could not load the settings file '%s'.\n%s"),
4349 aFile.uri(),
4350 err.what() ? err.what() : "Unknown error");
4351 }
4352
4353 return S_OK;
4354}
4355
4356/**
4357 * Helper method to save the settings tree and turn expected exceptions to COM
4358 * errors.
4359 *
4360 * Note that this method will not catch unexpected errors so it may still
4361 * throw something.
4362 *
4363 * @param aTree Tree to save.
4364 * @param aFile File to save the tree to.
4365 * @param aFormatVersion Where to store the (recent) format version of the
4366 * saved settings tree on success.
4367 */
4368/* static */
4369HRESULT VirtualBox::saveSettingsTree (settings::TreeBackend &aTree,
4370 settings::File &aFile,
4371 Utf8Str &aFormatVersion)
4372{
4373 using namespace settings;
4374
4375 try
4376 {
4377 aTree.write (aFile);
4378
4379 /* set the current settings file version to the most recent version on
4380 * success. See also VirtualBox::loadSettingsTree(). */
4381 if (aFormatVersion != VBOX_XML_VERSION_FULL)
4382 aFormatVersion = VBOX_XML_VERSION_FULL;
4383 }
4384 catch (const EIPRTFailure &err)
4385 {
4386 /* this is the only expected exception for now */
4387 return setError (E_FAIL,
4388 tr ("Could not save the settings file '%s' (%Vrc)"),
4389 aFile.uri(), err.rc());
4390 }
4391
4392 return S_OK;
4393}
4394
4395/**
4396 * Creates a backup copy of the given settings file by suffixing it with the
4397 * supplied version format string and optionally with numbers from .0 to .9
4398 * if the backup file already exists.
4399 *
4400 * @param aFileName Orignal settings file name.
4401 * @param aOldFormat Version of the original format.
4402 * @param aBakFileName File name of the created backup copy (only on success).
4403 */
4404/* static */
4405HRESULT VirtualBox::backupSettingsFile (const Bstr &aFileName,
4406 const Utf8Str &aOldFormat,
4407 Bstr &aBakFileName)
4408{
4409 Utf8Str of = aFileName;
4410 Utf8Str nf = Utf8StrFmt ("%s.%s.bak", of.raw(), aOldFormat.raw());
4411
4412 int vrc = RTFileCopyEx (of, nf, RTFILECOPY_FLAGS_NO_SRC_DENY_WRITE,
4413 NULL, NULL);
4414
4415 /* try progressive suffix from .0 to .9 on failure */
4416 if (vrc == VERR_ALREADY_EXISTS)
4417 {
4418 Utf8Str tmp = nf;
4419 for (int i = 0; i <= 9 && RT_FAILURE (vrc); ++ i)
4420 {
4421 nf = Utf8StrFmt ("%s.%d", tmp.raw(), i);
4422 vrc = RTFileCopyEx (of, nf, RTFILECOPY_FLAGS_NO_SRC_DENY_WRITE,
4423 NULL, NULL);
4424 }
4425 }
4426
4427 if (RT_FAILURE (vrc))
4428 return setError (E_FAIL,
4429 tr ("Could not copy the settings file '%s' to '%s' (%Vrc)"),
4430 of.raw(), nf.raw(), vrc);
4431
4432 aBakFileName = nf;
4433
4434 return S_OK;
4435}
4436
4437/**
4438 * Handles unexpected exceptions by turning them into COM errors in release
4439 * builds or by hitting a breakpoint in the release builds.
4440 *
4441 * Usage pattern:
4442 * @code
4443 try
4444 {
4445 // ...
4446 }
4447 catch (LaLalA)
4448 {
4449 // ...
4450 }
4451 catch (...)
4452 {
4453 rc = VirtualBox::handleUnexpectedExceptions (RT_SRC_POS);
4454 }
4455 * @endcode
4456 *
4457 * @param RT_SRC_POS_DECL "RT_SRC_POS" macro instantiation.
4458 */
4459/* static */
4460HRESULT VirtualBox::handleUnexpectedExceptions (RT_SRC_POS_DECL)
4461{
4462 try
4463 {
4464 /* rethrow the current exception */
4465 throw;
4466 }
4467 catch (const std::exception &err)
4468 {
4469 ComAssertMsgFailedPos (("Unexpected exception '%s' (%s)\n",
4470 typeid (err).name(), err.what()),
4471 pszFile, iLine, pszFunction);
4472 return E_FAIL;
4473 }
4474 catch (...)
4475 {
4476 ComAssertMsgFailedPos (("Unknown exception\n"),
4477 pszFile, iLine, pszFunction);
4478 return E_FAIL;
4479 }
4480
4481 /* should not get here */
4482 AssertFailed();
4483 return E_FAIL;
4484}
4485
4486/**
4487 * Helper to register the DVD image.
4488 *
4489 * @param aImage object to register
4490 * @param aOnStartUp whether this method called during VirtualBox init or not
4491 *
4492 * @return COM status code
4493 *
4494 * @note Locks objects!
4495 */
4496HRESULT VirtualBox::registerDVDImage (DVDImage *aImage, bool aOnStartUp)
4497{
4498 AssertReturn (aImage, E_INVALIDARG);
4499
4500 AutoCaller autoCaller (this);
4501 AssertComRCReturn (autoCaller.rc(), autoCaller.rc());
4502
4503 AutoWriteLock alock (this);
4504
4505 HRESULT rc = checkMediaForConflicts (NULL, &aImage->id(),
4506 aImage->filePathFull());
4507 CheckComRCReturnRC (rc);
4508
4509 /* add to the collection */
4510 mData.mDVDImages.push_back (aImage);
4511
4512 /* save global config file if we're supposed to */
4513 if (!aOnStartUp)
4514 rc = saveSettings();
4515
4516 return rc;
4517}
4518
4519/**
4520 * Helper to register the floppy image.
4521 *
4522 * @param aImage object to register
4523 * @param aOnStartUp whether this method called during VirtualBox init or not
4524 *
4525 * @return COM status code
4526 *
4527 * @note Locks objects!
4528 */
4529HRESULT VirtualBox::registerFloppyImage (FloppyImage *aImage, bool aOnStartUp)
4530{
4531 AssertReturn (aImage, E_INVALIDARG);
4532
4533 AutoCaller autoCaller (this);
4534 AssertComRCReturn (autoCaller.rc(), autoCaller.rc());
4535
4536 AutoWriteLock alock (this);
4537
4538 HRESULT rc = checkMediaForConflicts (NULL, &aImage->id(),
4539 aImage->filePathFull());
4540 CheckComRCReturnRC (rc);
4541
4542 /* add to the collection */
4543 mData.mFloppyImages.push_back (aImage);
4544
4545 /* save global config file if we're supposed to */
4546 if (!aOnStartUp)
4547 rc = saveSettings();
4548
4549 return rc;
4550}
4551
4552/**
4553 * Helper to lock the VirtualBox configuration for write access.
4554 *
4555 * @note This method is not thread safe (must be called only from #init()
4556 * or #uninit()).
4557 *
4558 * @note If the configuration file is not found, the method returns
4559 * S_OK, but subsequent #isConfigLocked() will return FALSE. This is used
4560 * in some places to determine the (valid) situation when no config file
4561 * exists yet, and therefore a new one should be created from scatch.
4562 */
4563HRESULT VirtualBox::lockConfig()
4564{
4565 AutoCaller autoCaller (this);
4566 AssertComRCReturn (autoCaller.rc(), autoCaller.rc());
4567 AssertReturn (autoCaller.state() == InInit, E_FAIL);
4568
4569 HRESULT rc = S_OK;
4570
4571 Assert (!isConfigLocked());
4572 if (!isConfigLocked())
4573 {
4574 /* open the associated config file */
4575 int vrc = RTFileOpen (&mData.mCfgFile.mHandle,
4576 Utf8Str (mData.mCfgFile.mName),
4577 RTFILE_O_READWRITE | RTFILE_O_OPEN |
4578 RTFILE_O_DENY_WRITE);
4579 if (VBOX_FAILURE (vrc))
4580 {
4581 mData.mCfgFile.mHandle = NIL_RTFILE;
4582
4583 /*
4584 * It is ok if the file is not found, it will be created by
4585 * init(). Otherwise return an error.
4586 */
4587 if (vrc != VERR_FILE_NOT_FOUND)
4588 rc = setError (E_FAIL,
4589 tr ("Could not lock the settings file '%ls' (%Vrc)"),
4590 mData.mCfgFile.mName.raw(), vrc);
4591 }
4592
4593 LogFlowThisFunc (("mCfgFile.mName='%ls', mCfgFile.mHandle=%d, rc=%08X\n",
4594 mData.mCfgFile.mName.raw(), mData.mCfgFile.mHandle, rc));
4595 }
4596
4597 return rc;
4598}
4599
4600/**
4601 * Helper to unlock the VirtualBox configuration from write access.
4602 *
4603 * @note This method is not thread safe (must be called only from #init()
4604 * or #uninit()).
4605 */
4606HRESULT VirtualBox::unlockConfig()
4607{
4608 AutoCaller autoCaller (this);
4609 AssertComRCReturn (autoCaller.rc(), E_FAIL);
4610 AssertReturn (autoCaller.state() == InUninit, E_FAIL);
4611
4612 HRESULT rc = S_OK;
4613
4614 if (isConfigLocked())
4615 {
4616 RTFileFlush (mData.mCfgFile.mHandle);
4617 RTFileClose (mData.mCfgFile.mHandle);
4618 /** @todo flush the directory too. */
4619 mData.mCfgFile.mHandle = NIL_RTFILE;
4620 LogFlowThisFunc (("\n"));
4621 }
4622
4623 return rc;
4624}
4625
4626/**
4627 * Thread function that watches the termination of all client processes
4628 * that have opened sessions using IVirtualBox::OpenSession()
4629 */
4630// static
4631DECLCALLBACK(int) VirtualBox::ClientWatcher (RTTHREAD thread, void *pvUser)
4632{
4633 LogFlowFuncEnter();
4634
4635 VirtualBox *that = (VirtualBox *) pvUser;
4636 Assert (that);
4637
4638 SessionMachineVector machines;
4639 size_t cnt = 0;
4640
4641#if defined(RT_OS_WINDOWS)
4642
4643 HRESULT hrc = CoInitializeEx (NULL,
4644 COINIT_MULTITHREADED | COINIT_DISABLE_OLE1DDE |
4645 COINIT_SPEED_OVER_MEMORY);
4646 AssertComRC (hrc);
4647
4648 /// @todo (dmik) processes reaping!
4649
4650 HANDLE *handles = new HANDLE [1];
4651 handles [0] = that->mWatcherData.mUpdateReq;
4652
4653 do
4654 {
4655 AutoCaller autoCaller (that);
4656 /* VirtualBox has been early uninitialized, terminate */
4657 if (!autoCaller.isOk())
4658 break;
4659
4660 do
4661 {
4662 /* release the caller to let uninit() ever proceed */
4663 autoCaller.release();
4664
4665 DWORD rc = ::WaitForMultipleObjects (cnt + 1, handles, FALSE, INFINITE);
4666
4667 /*
4668 * Restore the caller before using VirtualBox. If it fails, this
4669 * means VirtualBox is being uninitialized and we must terminate.
4670 */
4671 autoCaller.add();
4672 if (!autoCaller.isOk())
4673 break;
4674
4675 bool update = false;
4676 if (rc == WAIT_OBJECT_0)
4677 {
4678 /* update event is signaled */
4679 update = true;
4680 }
4681 else if (rc > WAIT_OBJECT_0 && rc <= (WAIT_OBJECT_0 + cnt))
4682 {
4683 /* machine mutex is released */
4684 (machines [rc - WAIT_OBJECT_0 - 1])->checkForDeath();
4685 update = true;
4686 }
4687 else if (rc > WAIT_ABANDONED_0 && rc <= (WAIT_ABANDONED_0 + cnt))
4688 {
4689 /* machine mutex is abandoned due to client process termination */
4690 (machines [rc - WAIT_ABANDONED_0 - 1])->checkForDeath();
4691 update = true;
4692 }
4693 if (update)
4694 {
4695 /* obtain a new set of opened machines */
4696 that->getOpenedMachines (machines);
4697 cnt = machines.size();
4698 LogFlowFunc (("UPDATE: direct session count = %d\n", cnt));
4699 AssertMsg ((cnt + 1) <= MAXIMUM_WAIT_OBJECTS,
4700 ("MAXIMUM_WAIT_OBJECTS reached"));
4701 /* renew the set of event handles */
4702 delete [] handles;
4703 handles = new HANDLE [cnt + 1];
4704 handles [0] = that->mWatcherData.mUpdateReq;
4705 for (size_t i = 0; i < cnt; ++ i)
4706 handles [i + 1] = (machines [i])->ipcSem();
4707 }
4708 }
4709 while (true);
4710 }
4711 while (0);
4712
4713 /* delete the set of event handles */
4714 delete [] handles;
4715
4716 /* delete the set of opened machines if any */
4717 machines.clear();
4718
4719 ::CoUninitialize();
4720
4721#elif defined (RT_OS_OS2)
4722
4723 /// @todo (dmik) processes reaping!
4724
4725 /* according to PMREF, 64 is the maximum for the muxwait list */
4726 SEMRECORD handles [64];
4727
4728 HMUX muxSem = NULLHANDLE;
4729
4730 do
4731 {
4732 AutoCaller autoCaller (that);
4733 /* VirtualBox has been early uninitialized, terminate */
4734 if (!autoCaller.isOk())
4735 break;
4736
4737 do
4738 {
4739 /* release the caller to let uninit() ever proceed */
4740 autoCaller.release();
4741
4742 int vrc = RTSemEventWait (that->mWatcherData.mUpdateReq, 500);
4743
4744 /*
4745 * Restore the caller before using VirtualBox. If it fails, this
4746 * means VirtualBox is being uninitialized and we must terminate.
4747 */
4748 autoCaller.add();
4749 if (!autoCaller.isOk())
4750 break;
4751
4752 bool update = false;
4753
4754 if (VBOX_SUCCESS (vrc))
4755 {
4756 /* update event is signaled */
4757 update = true;
4758 }
4759 else
4760 {
4761 AssertMsg (vrc == VERR_TIMEOUT || vrc == VERR_INTERRUPTED,
4762 ("RTSemEventWait returned %Vrc\n", vrc));
4763
4764 /* are there any mutexes? */
4765 if (cnt > 0)
4766 {
4767 /* figure out what's going on with machines */
4768
4769 unsigned long semId = 0;
4770 APIRET arc = ::DosWaitMuxWaitSem (muxSem,
4771 SEM_IMMEDIATE_RETURN, &semId);
4772
4773 if (arc == NO_ERROR)
4774 {
4775 /* machine mutex is normally released */
4776 Assert (semId >= 0 && semId < cnt);
4777 if (semId >= 0 && semId < cnt)
4778 {
4779#ifdef DEBUG
4780 {
4781 AutoReadLock machieLock (machines [semId]);
4782 LogFlowFunc (("released mutex: machine='%ls'\n",
4783 machines [semId]->name().raw()));
4784 }
4785#endif
4786 machines [semId]->checkForDeath();
4787 }
4788 update = true;
4789 }
4790 else if (arc == ERROR_SEM_OWNER_DIED)
4791 {
4792 /* machine mutex is abandoned due to client process
4793 * termination; find which mutex is in the Owner Died
4794 * state */
4795 for (size_t i = 0; i < cnt; ++ i)
4796 {
4797 PID pid; TID tid;
4798 unsigned long reqCnt;
4799 arc = DosQueryMutexSem ((HMTX) handles [i].hsemCur, &pid,
4800 &tid, &reqCnt);
4801 if (arc == ERROR_SEM_OWNER_DIED)
4802 {
4803 /* close the dead mutex as asked by PMREF */
4804 ::DosCloseMutexSem ((HMTX) handles [i].hsemCur);
4805
4806 Assert (i >= 0 && i < cnt);
4807 if (i >= 0 && i < cnt)
4808 {
4809#ifdef DEBUG
4810 {
4811 AutoReadLock machieLock (machines [semId]);
4812 LogFlowFunc (("mutex owner dead: machine='%ls'\n",
4813 machines [i]->name().raw()));
4814 }
4815#endif
4816 machines [i]->checkForDeath();
4817 }
4818 }
4819 }
4820 update = true;
4821 }
4822 else
4823 AssertMsg (arc == ERROR_INTERRUPT || arc == ERROR_TIMEOUT,
4824 ("DosWaitMuxWaitSem returned %d\n", arc));
4825 }
4826 }
4827
4828 if (update)
4829 {
4830 /* close the old muxsem */
4831 if (muxSem != NULLHANDLE)
4832 ::DosCloseMuxWaitSem (muxSem);
4833 /* obtain a new set of opened machines */
4834 that->getOpenedMachines (machines);
4835 cnt = machines.size();
4836 LogFlowFunc (("UPDATE: direct session count = %d\n", cnt));
4837 /// @todo use several muxwait sems if cnt is > 64
4838 AssertMsg (cnt <= 64 /* according to PMREF */,
4839 ("maximum of 64 mutex semaphores reached (%d)", cnt));
4840 if (cnt > 64)
4841 cnt = 64;
4842 if (cnt > 0)
4843 {
4844 /* renew the set of event handles */
4845 for (size_t i = 0; i < cnt; ++ i)
4846 {
4847 handles [i].hsemCur = (HSEM) machines [i]->ipcSem();
4848 handles [i].ulUser = i;
4849 }
4850 /* create a new muxsem */
4851 APIRET arc = ::DosCreateMuxWaitSem (NULL, &muxSem, cnt, handles,
4852 DCMW_WAIT_ANY);
4853 AssertMsg (arc == NO_ERROR,
4854 ("DosCreateMuxWaitSem returned %d\n", arc)); NOREF(arc);
4855 }
4856 }
4857 }
4858 while (true);
4859 }
4860 while (0);
4861
4862 /* close the muxsem */
4863 if (muxSem != NULLHANDLE)
4864 ::DosCloseMuxWaitSem (muxSem);
4865
4866 /* delete the set of opened machines if any */
4867 machines.clear();
4868
4869#elif defined(VBOX_WITH_SYS_V_IPC_SESSION_WATCHER)
4870
4871 bool need_update = false;
4872
4873 do
4874 {
4875 AutoCaller autoCaller (that);
4876 if (!autoCaller.isOk())
4877 break;
4878
4879 do
4880 {
4881 /* release the caller to let uninit() ever proceed */
4882 autoCaller.release();
4883
4884 int rc = RTSemEventWait (that->mWatcherData.mUpdateReq, 500);
4885
4886 /*
4887 * Restore the caller before using VirtualBox. If it fails, this
4888 * means VirtualBox is being uninitialized and we must terminate.
4889 */
4890 autoCaller.add();
4891 if (!autoCaller.isOk())
4892 break;
4893
4894 if (VBOX_SUCCESS (rc) || need_update)
4895 {
4896 /* VBOX_SUCCESS (rc) means an update event is signaled */
4897
4898 /* obtain a new set of opened machines */
4899 that->getOpenedMachines (machines);
4900 cnt = machines.size();
4901 LogFlowFunc (("UPDATE: direct session count = %d\n", cnt));
4902 }
4903
4904 need_update = false;
4905 for (size_t i = 0; i < cnt; ++ i)
4906 need_update |= (machines [i])->checkForDeath();
4907
4908 /* reap child processes */
4909 {
4910 AutoWriteLock alock (that);
4911 if (that->mWatcherData.mProcesses.size())
4912 {
4913 LogFlowFunc (("UPDATE: child process count = %d\n",
4914 that->mWatcherData.mProcesses.size()));
4915 ClientWatcherData::ProcessList::iterator it =
4916 that->mWatcherData.mProcesses.begin();
4917 while (it != that->mWatcherData.mProcesses.end())
4918 {
4919 RTPROCESS pid = *it;
4920 RTPROCSTATUS status;
4921 int vrc = ::RTProcWait (pid, RTPROCWAIT_FLAGS_NOBLOCK,
4922 &status);
4923 if (vrc == VINF_SUCCESS)
4924 {
4925 LogFlowFunc (("pid %d (%x) was reaped, "
4926 "status=%d, reason=%d\n",
4927 pid, pid, status.iStatus,
4928 status.enmReason));
4929 it = that->mWatcherData.mProcesses.erase (it);
4930 }
4931 else
4932 {
4933 LogFlowFunc (("pid %d (%x) was NOT reaped, vrc=%Vrc\n",
4934 pid, pid, vrc));
4935 if (vrc != VERR_PROCESS_RUNNING)
4936 {
4937 /* remove the process if it is not already running */
4938 it = that->mWatcherData.mProcesses.erase (it);
4939 }
4940 else
4941 ++ it;
4942 }
4943 }
4944 }
4945 }
4946 }
4947 while (true);
4948 }
4949 while (0);
4950
4951 /* delete the set of opened machines if any */
4952 machines.clear();
4953
4954#else
4955# error "Port me!"
4956#endif
4957
4958 LogFlowFuncLeave();
4959 return 0;
4960}
4961
4962/**
4963 * Thread function that handles custom events posted using #postEvent().
4964 */
4965// static
4966DECLCALLBACK(int) VirtualBox::AsyncEventHandler (RTTHREAD thread, void *pvUser)
4967{
4968 LogFlowFuncEnter();
4969
4970 AssertReturn (pvUser, VERR_INVALID_POINTER);
4971
4972 // create an event queue for the current thread
4973 EventQueue *eventQ = new EventQueue();
4974 AssertReturn (eventQ, VERR_NO_MEMORY);
4975
4976 // return the queue to the one who created this thread
4977 *(static_cast <EventQueue **> (pvUser)) = eventQ;
4978 // signal that we're ready
4979 RTThreadUserSignal (thread);
4980
4981 BOOL ok = TRUE;
4982 Event *event = NULL;
4983
4984 while ((ok = eventQ->waitForEvent (&event)) && event)
4985 eventQ->handleEvent (event);
4986
4987 AssertReturn (ok, VERR_GENERAL_FAILURE);
4988
4989 delete eventQ;
4990
4991 LogFlowFuncLeave();
4992
4993 return 0;
4994}
4995
4996////////////////////////////////////////////////////////////////////////////////
4997
4998/**
4999 * Takes the current list of registered callbacks of the managed VirtualBox
5000 * instance, and calls #handleCallback() for every callback item from the
5001 * list, passing the item as an argument.
5002 *
5003 * @note Locks the managed VirtualBox object for reading but leaves the lock
5004 * before iterating over callbacks and calling their methods.
5005 */
5006void *VirtualBox::CallbackEvent::handler()
5007{
5008 if (mVirtualBox.isNull())
5009 return NULL;
5010
5011 AutoCaller autoCaller (mVirtualBox);
5012 if (!autoCaller.isOk())
5013 {
5014 LogWarningFunc (("VirtualBox has been uninitialized (state=%d), "
5015 "the callback event is discarded!\n",
5016 autoCaller.state()));
5017 /* We don't need mVirtualBox any more, so release it */
5018 mVirtualBox.setNull();
5019 return NULL;
5020 }
5021
5022 CallbackVector callbacks;
5023 {
5024 /* Make a copy to release the lock before iterating */
5025 AutoReadLock alock (mVirtualBox);
5026 callbacks = CallbackVector (mVirtualBox->mData.mCallbacks.begin(),
5027 mVirtualBox->mData.mCallbacks.end());
5028 /* We don't need mVirtualBox any more, so release it */
5029 mVirtualBox.setNull();
5030 }
5031
5032 for (VirtualBox::CallbackVector::const_iterator it = callbacks.begin();
5033 it != callbacks.end(); ++ it)
5034 handleCallback (*it);
5035
5036 return NULL;
5037}
5038
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