VirtualBox

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

Last change on this file since 2469 was 2463, checked in by vboxsync, 18 years ago

Main & All Frontends: Prototyped a bunch of Main API changes (IVirtualBoxErrorInfo extension for cascading errors; IMachine/IConsoleCallback extension to properly activate the console window; IVirtualBoxCallback::onExtraDataCanChange() support for error messages; minor IHost::createUSBDeviceFilter/removeUSBDeviceFilter corrections).

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