VirtualBox

source: vbox/trunk/src/VBox/Main/src-server/VirtualBoxImpl.cpp@ 78985

Last change on this file since 78985 was 78950, checked in by vboxsync, 6 years ago

Shared Clipboard/URI: Update; more work on clipboard area code, documentation.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 201.5 KB
Line 
1/* $Id: VirtualBoxImpl.cpp 78950 2019-06-04 07:26:56Z vboxsync $ */
2/** @file
3 * Implementation of IVirtualBox in VBoxSVC.
4 */
5
6/*
7 * Copyright (C) 2006-2019 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18#define LOG_GROUP LOG_GROUP_MAIN_VIRTUALBOX
19#include <iprt/asm.h>
20#include <iprt/base64.h>
21#include <iprt/buildconfig.h>
22#include <iprt/cpp/utils.h>
23#include <iprt/dir.h>
24#include <iprt/env.h>
25#include <iprt/file.h>
26#include <iprt/path.h>
27#include <iprt/process.h>
28#include <iprt/rand.h>
29#include <iprt/sha.h>
30#include <iprt/string.h>
31#include <iprt/stream.h>
32#include <iprt/system.h>
33#include <iprt/thread.h>
34#include <iprt/uuid.h>
35#include <iprt/cpp/xml.h>
36#include <iprt/ctype.h>
37
38#include <VBox/com/com.h>
39#include <VBox/com/array.h>
40#include "VBox/com/EventQueue.h"
41#include "VBox/com/MultiResult.h"
42
43#include <VBox/err.h>
44#include <VBox/param.h>
45#include <VBox/settings.h>
46#include <VBox/version.h>
47
48#ifdef VBOX_WITH_SHARED_CLIPBOARD_URI_LIST
49# include <VBox/GuestHost/SharedClipboard-uri.h>
50#endif
51
52#include <package-generated.h>
53
54#include <algorithm>
55#include <set>
56#include <vector>
57#include <memory> // for auto_ptr
58
59#include "VirtualBoxImpl.h"
60
61#include "Global.h"
62#include "MachineImpl.h"
63#include "MediumImpl.h"
64#include "SharedFolderImpl.h"
65#include "ProgressImpl.h"
66#include "HostImpl.h"
67#include "USBControllerImpl.h"
68#include "SystemPropertiesImpl.h"
69#include "GuestOSTypeImpl.h"
70#include "NetworkServiceRunner.h"
71#include "DHCPServerImpl.h"
72#include "NATNetworkImpl.h"
73#ifdef VBOX_WITH_RESOURCE_USAGE_API
74# include "PerformanceImpl.h"
75#endif /* VBOX_WITH_RESOURCE_USAGE_API */
76#include "EventImpl.h"
77#ifdef VBOX_WITH_EXTPACK
78# include "ExtPackManagerImpl.h"
79#endif
80#ifdef VBOX_WITH_UNATTENDED
81# include "UnattendedImpl.h"
82#endif
83#include "AutostartDb.h"
84#include "ClientWatcher.h"
85#include "AutoCaller.h"
86#include "LoggingNew.h"
87#include "CloudProviderManagerImpl.h"
88#include "ThreadTask.h"
89
90#include <QMTranslator.h>
91
92#ifdef RT_OS_WINDOWS
93# include "win/svchlp.h"
94# include "tchar.h"
95#endif
96
97
98////////////////////////////////////////////////////////////////////////////////
99//
100// Definitions
101//
102////////////////////////////////////////////////////////////////////////////////
103
104#define VBOX_GLOBAL_SETTINGS_FILE "VirtualBox.xml"
105
106////////////////////////////////////////////////////////////////////////////////
107//
108// Global variables
109//
110////////////////////////////////////////////////////////////////////////////////
111
112// static
113com::Utf8Str VirtualBox::sVersion;
114
115// static
116com::Utf8Str VirtualBox::sVersionNormalized;
117
118// static
119ULONG VirtualBox::sRevision;
120
121// static
122com::Utf8Str VirtualBox::sPackageType;
123
124// static
125com::Utf8Str VirtualBox::sAPIVersion;
126
127// static
128std::map<com::Utf8Str, int> VirtualBox::sNatNetworkNameToRefCount;
129
130// static leaked (todo: find better place to free it.)
131RWLockHandle *VirtualBox::spMtxNatNetworkNameToRefCountLock;
132
133
134////////////////////////////////////////////////////////////////////////////////
135//
136// CallbackEvent class
137//
138////////////////////////////////////////////////////////////////////////////////
139
140/**
141 * Abstract callback event class to asynchronously call VirtualBox callbacks
142 * on a dedicated event thread. Subclasses reimplement #prepareEventDesc()
143 * to initialize the event depending on the event to be dispatched.
144 *
145 * @note The VirtualBox instance passed to the constructor is strongly
146 * referenced, so that the VirtualBox singleton won't be released until the
147 * event gets handled by the event thread.
148 */
149class VirtualBox::CallbackEvent : public Event
150{
151public:
152
153 CallbackEvent(VirtualBox *aVirtualBox, VBoxEventType_T aWhat)
154 : mVirtualBox(aVirtualBox), mWhat(aWhat)
155 {
156 Assert(aVirtualBox);
157 }
158
159 void *handler();
160
161 virtual HRESULT prepareEventDesc(IEventSource* aSource, VBoxEventDesc& aEvDesc) = 0;
162
163private:
164
165 /**
166 * Note that this is a weak ref -- the CallbackEvent handler thread
167 * is bound to the lifetime of the VirtualBox instance, so it's safe.
168 */
169 VirtualBox *mVirtualBox;
170protected:
171 VBoxEventType_T mWhat;
172};
173
174////////////////////////////////////////////////////////////////////////////////
175//
176// VirtualBox private member data definition
177//
178////////////////////////////////////////////////////////////////////////////////
179
180#if defined(RT_OS_WINDOWS) && defined(VBOXSVC_WITH_CLIENT_WATCHER)
181/**
182 * Client process watcher data.
183 */
184class WatchedClientProcess
185{
186public:
187 WatchedClientProcess(RTPROCESS a_pid, HANDLE a_hProcess) RT_NOEXCEPT
188 : m_pid(a_pid)
189 , m_cRefs(1)
190 , m_hProcess(a_hProcess)
191 {
192 }
193
194 ~WatchedClientProcess()
195 {
196 if (m_hProcess != NULL)
197 {
198 ::CloseHandle(m_hProcess);
199 m_hProcess = NULL;
200 }
201 m_pid = NIL_RTPROCESS;
202 }
203
204 /** The client PID. */
205 RTPROCESS m_pid;
206 /** Number of references to this structure. */
207 uint32_t volatile m_cRefs;
208 /** Handle of the client process.
209 * Ideally, we've got full query privileges, but we'll settle for waiting. */
210 HANDLE m_hProcess;
211};
212typedef std::map<RTPROCESS, WatchedClientProcess *> WatchedClientProcessMap;
213#endif
214
215
216typedef ObjectsList<Medium> MediaOList;
217typedef ObjectsList<GuestOSType> GuestOSTypesOList;
218typedef ObjectsList<SharedFolder> SharedFoldersOList;
219typedef ObjectsList<DHCPServer> DHCPServersOList;
220typedef ObjectsList<NATNetwork> NATNetworksOList;
221
222typedef std::map<Guid, ComPtr<IProgress> > ProgressMap;
223typedef std::map<Guid, ComObjPtr<Medium> > HardDiskMap;
224
225#ifdef VBOX_WITH_SHARED_CLIPBOARD_URI_LIST
226/**
227 * Structure for keeping Shared Clipboard area data within the VirtualBox object.
228 */
229struct SharedClipboardAreaData
230{
231 SharedClipboardAreaData()
232 : uID(NIL_SHAREDCLIPBOARDAREAID) { }
233
234 /** The area's (unique) ID.
235 * Set to NIL_SHAREDCLIPBOARDAREAID if not initialized yet. */
236 ULONG uID;
237 /** The actual Shared Clipboard area assigned to this ID. */
238 SharedClipboardArea Area;
239};
240
241/** Map of Shared Clipboard areas. The key defines the area ID. */
242typedef std::map<ULONG, SharedClipboardAreaData> SharedClipboardAreaMap;
243
244/**
245 * Structure for keeping global Shared Clipboard data within the VirtualBox object.
246 */
247struct SharedClipboardData
248{
249 SharedClipboardData()
250 : uMostRecentClipboardAreaID(NIL_SHAREDCLIPBOARDAREAID)
251 , uMaxClipboardAreas(32) /** @todo Make this configurable. */
252 {
253 int rc2 = RTCritSectInit(&CritSect);
254 AssertRC(rc2);
255 }
256
257 virtual ~SharedClipboardData()
258 {
259 RTCritSectDelete(&CritSect);
260 }
261
262 /**
263 * Generates a new clipboard area ID.
264 * Currently does *not* check for collisions and stuff.
265 *
266 * @returns New clipboard area ID.
267 */
268 ULONG GenerateAreaID(void)
269 {
270 ULONG uID = NIL_SHAREDCLIPBOARDAREAID;
271
272 int rc = RTCritSectEnter(&CritSect);
273 if (RT_SUCCESS(rc))
274 {
275 uID = RTRandU32Ex(1, UINT32_MAX - 1); /** @todo Make this a bit more sophisticated. Later. */
276
277 int rc2 = RTCritSectLeave(&CritSect);
278 AssertRC(rc2);
279 }
280
281 LogFlowFunc(("uID=%RU32\n", uID));
282 return uID;
283 }
284
285 /** Critical section to serialize access. */
286 RTCRITSECT CritSect;
287 /** The most recent (last created) clipboard area ID.
288 * NIL_SHAREDCLIPBOARDAREAID if not initialized yet. */
289 ULONG uMostRecentClipboardAreaID;
290 /** Maximum of concurrent clipboard areas.
291 * @todo Make this configurable. */
292 ULONG uMaxClipboardAreas;
293 /** Map of clipboard areas. The key is the area ID. */
294 SharedClipboardAreaMap mapClipboardAreas;
295};
296#endif /* VBOX_WITH_SHARED_CLIPBOARD_URI_LIST */
297
298/**
299 * Main VirtualBox data structure.
300 * @note |const| members are persistent during lifetime so can be accessed
301 * without locking.
302 */
303struct VirtualBox::Data
304{
305 Data()
306 : pMainConfigFile(NULL)
307 , uuidMediaRegistry("48024e5c-fdd9-470f-93af-ec29f7ea518c")
308 , uRegistryNeedsSaving(0)
309 , lockMachines(LOCKCLASS_LISTOFMACHINES)
310 , allMachines(lockMachines)
311 , lockGuestOSTypes(LOCKCLASS_LISTOFOTHEROBJECTS)
312 , allGuestOSTypes(lockGuestOSTypes)
313 , lockMedia(LOCKCLASS_LISTOFMEDIA)
314 , allHardDisks(lockMedia)
315 , allDVDImages(lockMedia)
316 , allFloppyImages(lockMedia)
317 , lockSharedFolders(LOCKCLASS_LISTOFOTHEROBJECTS)
318 , allSharedFolders(lockSharedFolders)
319 , lockDHCPServers(LOCKCLASS_LISTOFOTHEROBJECTS)
320 , allDHCPServers(lockDHCPServers)
321 , lockNATNetworks(LOCKCLASS_LISTOFOTHEROBJECTS)
322 , allNATNetworks(lockNATNetworks)
323 , mtxProgressOperations(LOCKCLASS_PROGRESSLIST)
324 , pClientWatcher(NULL)
325 , threadAsyncEvent(NIL_RTTHREAD)
326 , pAsyncEventQ(NULL)
327 , pAutostartDb(NULL)
328 , fSettingsCipherKeySet(false)
329#if defined(RT_OS_WINDOWS) && defined(VBOXSVC_WITH_CLIENT_WATCHER)
330 , fWatcherIsReliable(RTSystemGetNtVersion() >= RTSYSTEM_MAKE_NT_VERSION(6, 0, 0))
331#endif
332 {
333#if defined(RT_OS_WINDOWS) && defined(VBOXSVC_WITH_CLIENT_WATCHER)
334 RTCritSectRwInit(&WatcherCritSect);
335#endif
336 }
337
338 ~Data()
339 {
340 if (pMainConfigFile)
341 {
342 delete pMainConfigFile;
343 pMainConfigFile = NULL;
344 }
345 };
346
347 // const data members not requiring locking
348 const Utf8Str strHomeDir;
349
350 // VirtualBox main settings file
351 const Utf8Str strSettingsFilePath;
352 settings::MainConfigFile *pMainConfigFile;
353
354 // constant pseudo-machine ID for global media registry
355 const Guid uuidMediaRegistry;
356
357 // counter if global media registry needs saving, updated using atomic
358 // operations, without requiring any locks
359 uint64_t uRegistryNeedsSaving;
360
361 // const objects not requiring locking
362 const ComObjPtr<Host> pHost;
363 const ComObjPtr<SystemProperties> pSystemProperties;
364#ifdef VBOX_WITH_RESOURCE_USAGE_API
365 const ComObjPtr<PerformanceCollector> pPerformanceCollector;
366#endif /* VBOX_WITH_RESOURCE_USAGE_API */
367
368 // Each of the following lists use a particular lock handle that protects the
369 // list as a whole. As opposed to version 3.1 and earlier, these lists no
370 // longer need the main VirtualBox object lock, but only the respective list
371 // lock. In each case, the locking order is defined that the list must be
372 // requested before object locks of members of the lists (see the order definitions
373 // in AutoLock.h; e.g. LOCKCLASS_LISTOFMACHINES before LOCKCLASS_MACHINEOBJECT).
374 RWLockHandle lockMachines;
375 MachinesOList allMachines;
376
377 RWLockHandle lockGuestOSTypes;
378 GuestOSTypesOList allGuestOSTypes;
379
380 // All the media lists are protected by the following locking handle:
381 RWLockHandle lockMedia;
382 MediaOList allHardDisks, // base images only!
383 allDVDImages,
384 allFloppyImages;
385 // the hard disks map is an additional map sorted by UUID for quick lookup
386 // and contains ALL hard disks (base and differencing); it is protected by
387 // the same lock as the other media lists above
388 HardDiskMap mapHardDisks;
389
390 // list of pending machine renames (also protected by media tree lock;
391 // see VirtualBox::rememberMachineNameChangeForMedia())
392 struct PendingMachineRename
393 {
394 Utf8Str strConfigDirOld;
395 Utf8Str strConfigDirNew;
396 };
397 typedef std::list<PendingMachineRename> PendingMachineRenamesList;
398 PendingMachineRenamesList llPendingMachineRenames;
399
400 RWLockHandle lockSharedFolders;
401 SharedFoldersOList allSharedFolders;
402
403 RWLockHandle lockDHCPServers;
404 DHCPServersOList allDHCPServers;
405
406 RWLockHandle lockNATNetworks;
407 NATNetworksOList allNATNetworks;
408
409 RWLockHandle mtxProgressOperations;
410 ProgressMap mapProgressOperations;
411
412 ClientWatcher * const pClientWatcher;
413
414 // the following are data for the async event thread
415 const RTTHREAD threadAsyncEvent;
416 EventQueue * const pAsyncEventQ;
417 const ComObjPtr<EventSource> pEventSource;
418
419#ifdef VBOX_WITH_EXTPACK
420 /** The extension pack manager object lives here. */
421 const ComObjPtr<ExtPackManager> ptrExtPackManager;
422#endif
423
424 /** The reference to the cloud provider manager singleton. */
425 const ComObjPtr<CloudProviderManager> pCloudProviderManager;
426
427 /** The global autostart database for the user. */
428 AutostartDb * const pAutostartDb;
429
430 /** Settings secret */
431 bool fSettingsCipherKeySet;
432 uint8_t SettingsCipherKey[RTSHA512_HASH_SIZE];
433
434#if defined(RT_OS_WINDOWS) && defined(VBOXSVC_WITH_CLIENT_WATCHER)
435 /** Critical section protecting WatchedProcesses. */
436 RTCRITSECTRW WatcherCritSect;
437 /** Map of processes being watched, key is the PID. */
438 WatchedClientProcessMap WatchedProcesses;
439 /** Set if the watcher is reliable, otherwise cleared.
440 * The watcher goes unreliable when we run out of memory, fail open a client
441 * process, or if the watcher thread gets messed up. */
442 bool fWatcherIsReliable;
443#endif
444
445#ifdef VBOX_WITH_SHARED_CLIPBOARD_URI_LIST
446 /** Data related to Shared Clipboard handling. */
447 SharedClipboardData SharedClipboard;
448#endif
449};
450
451// constructor / destructor
452/////////////////////////////////////////////////////////////////////////////
453
454DEFINE_EMPTY_CTOR_DTOR(VirtualBox)
455
456HRESULT VirtualBox::FinalConstruct()
457{
458 LogRelFlowThisFuncEnter();
459 LogRel(("VirtualBox: object creation starts\n"));
460
461 BaseFinalConstruct();
462
463 HRESULT rc = init();
464
465 LogRelFlowThisFuncLeave();
466 LogRel(("VirtualBox: object created\n"));
467
468 return rc;
469}
470
471void VirtualBox::FinalRelease()
472{
473 LogRelFlowThisFuncEnter();
474 LogRel(("VirtualBox: object deletion starts\n"));
475
476 uninit();
477
478 BaseFinalRelease();
479
480 LogRel(("VirtualBox: object deleted\n"));
481 LogRelFlowThisFuncLeave();
482}
483
484// public initializer/uninitializer for internal purposes only
485/////////////////////////////////////////////////////////////////////////////
486
487/**
488 * Initializes the VirtualBox object.
489 *
490 * @return COM result code
491 */
492HRESULT VirtualBox::init()
493{
494 LogRelFlowThisFuncEnter();
495 /* Enclose the state transition NotReady->InInit->Ready */
496 AutoInitSpan autoInitSpan(this);
497 AssertReturn(autoInitSpan.isOk(), E_FAIL);
498
499 /* Locking this object for writing during init sounds a bit paradoxical,
500 * but in the current locking mess this avoids that some code gets a
501 * read lock and later calls code which wants the same write lock. */
502 AutoWriteLock lock(this COMMA_LOCKVAL_SRC_POS);
503
504 // allocate our instance data
505 m = new Data;
506
507 LogFlow(("===========================================================\n"));
508 LogFlowThisFuncEnter();
509
510 if (sVersion.isEmpty())
511 sVersion = RTBldCfgVersion();
512 if (sVersionNormalized.isEmpty())
513 {
514 Utf8Str tmp(RTBldCfgVersion());
515 if (tmp.endsWith(VBOX_BUILD_PUBLISHER))
516 tmp = tmp.substr(0, tmp.length() - strlen(VBOX_BUILD_PUBLISHER));
517 sVersionNormalized = tmp;
518 }
519 sRevision = RTBldCfgRevision();
520 if (sPackageType.isEmpty())
521 sPackageType = VBOX_PACKAGE_STRING;
522 if (sAPIVersion.isEmpty())
523 sAPIVersion = VBOX_API_VERSION_STRING;
524 if (!spMtxNatNetworkNameToRefCountLock)
525 spMtxNatNetworkNameToRefCountLock = new RWLockHandle(LOCKCLASS_VIRTUALBOXOBJECT);
526
527 LogFlowThisFunc(("Version: %s, Package: %s, API Version: %s\n", sVersion.c_str(), sPackageType.c_str(), sAPIVersion.c_str()));
528
529 /* Important: DO NOT USE any kind of "early return" (except the single
530 * one above, checking the init span success) in this method. It is vital
531 * for correct error handling that it has only one point of return, which
532 * does all the magic on COM to signal object creation success and
533 * reporting the error later for every API method. COM translates any
534 * unsuccessful object creation to REGDB_E_CLASSNOTREG errors or similar
535 * unhelpful ones which cause us a lot of grief with troubleshooting. */
536
537 HRESULT rc = S_OK;
538 bool fCreate = false;
539 try
540 {
541 /* Get the VirtualBox home directory. */
542 {
543 char szHomeDir[RTPATH_MAX];
544 int vrc = com::GetVBoxUserHomeDirectory(szHomeDir, sizeof(szHomeDir));
545 if (RT_FAILURE(vrc))
546 throw setErrorBoth(E_FAIL, vrc,
547 tr("Could not create the VirtualBox home directory '%s' (%Rrc)"),
548 szHomeDir, vrc);
549
550 unconst(m->strHomeDir) = szHomeDir;
551 }
552
553 LogRel(("Home directory: '%s'\n", m->strHomeDir.c_str()));
554
555 i_reportDriverVersions();
556
557 /* compose the VirtualBox.xml file name */
558 unconst(m->strSettingsFilePath) = Utf8StrFmt("%s%c%s",
559 m->strHomeDir.c_str(),
560 RTPATH_DELIMITER,
561 VBOX_GLOBAL_SETTINGS_FILE);
562 // load and parse VirtualBox.xml; this will throw on XML or logic errors
563 try
564 {
565 m->pMainConfigFile = new settings::MainConfigFile(&m->strSettingsFilePath);
566 }
567 catch (xml::EIPRTFailure &e)
568 {
569 // this is thrown by the XML backend if the RTOpen() call fails;
570 // only if the main settings file does not exist, create it,
571 // if there's something more serious, then do fail!
572 if (e.rc() == VERR_FILE_NOT_FOUND)
573 fCreate = true;
574 else
575 throw;
576 }
577
578 if (fCreate)
579 m->pMainConfigFile = new settings::MainConfigFile(NULL);
580
581#ifdef VBOX_WITH_RESOURCE_USAGE_API
582 /* create the performance collector object BEFORE host */
583 unconst(m->pPerformanceCollector).createObject();
584 rc = m->pPerformanceCollector->init();
585 ComAssertComRCThrowRC(rc);
586#endif /* VBOX_WITH_RESOURCE_USAGE_API */
587
588 /* create the host object early, machines will need it */
589 unconst(m->pHost).createObject();
590 rc = m->pHost->init(this);
591 ComAssertComRCThrowRC(rc);
592
593 rc = m->pHost->i_loadSettings(m->pMainConfigFile->host);
594 if (FAILED(rc)) throw rc;
595
596 /*
597 * Create autostart database object early, because the system properties
598 * might need it.
599 */
600 unconst(m->pAutostartDb) = new AutostartDb;
601
602#ifdef VBOX_WITH_EXTPACK
603 /*
604 * Initialize extension pack manager before system properties because
605 * it is required for the VD plugins.
606 */
607 rc = unconst(m->ptrExtPackManager).createObject();
608 if (SUCCEEDED(rc))
609 rc = m->ptrExtPackManager->initExtPackManager(this, VBOXEXTPACKCTX_PER_USER_DAEMON);
610 if (FAILED(rc))
611 throw rc;
612#endif
613
614 /* create the system properties object, someone may need it too */
615 rc = unconst(m->pSystemProperties).createObject();
616 if (SUCCEEDED(rc))
617 rc = m->pSystemProperties->init(this);
618 ComAssertComRCThrowRC(rc);
619
620 rc = m->pSystemProperties->i_loadSettings(m->pMainConfigFile->systemProperties);
621 if (FAILED(rc)) throw rc;
622
623 /* guest OS type objects, needed by machines */
624 for (size_t i = 0; i < Global::cOSTypes; ++i)
625 {
626 ComObjPtr<GuestOSType> guestOSTypeObj;
627 rc = guestOSTypeObj.createObject();
628 if (SUCCEEDED(rc))
629 {
630 rc = guestOSTypeObj->init(Global::sOSTypes[i]);
631 if (SUCCEEDED(rc))
632 m->allGuestOSTypes.addChild(guestOSTypeObj);
633 }
634 ComAssertComRCThrowRC(rc);
635 }
636
637 /* all registered media, needed by machines */
638 if (FAILED(rc = initMedia(m->uuidMediaRegistry,
639 m->pMainConfigFile->mediaRegistry,
640 Utf8Str::Empty))) // const Utf8Str &machineFolder
641 throw rc;
642
643 /* machines */
644 if (FAILED(rc = initMachines()))
645 throw rc;
646
647#ifdef DEBUG
648 LogFlowThisFunc(("Dumping media backreferences\n"));
649 i_dumpAllBackRefs();
650#endif
651
652 /* net services - dhcp services */
653 for (settings::DHCPServersList::const_iterator it = m->pMainConfigFile->llDhcpServers.begin();
654 it != m->pMainConfigFile->llDhcpServers.end();
655 ++it)
656 {
657 const settings::DHCPServer &data = *it;
658
659 ComObjPtr<DHCPServer> pDhcpServer;
660 if (SUCCEEDED(rc = pDhcpServer.createObject()))
661 rc = pDhcpServer->init(this, data);
662 if (FAILED(rc)) throw rc;
663
664 rc = i_registerDHCPServer(pDhcpServer, false /* aSaveRegistry */);
665 if (FAILED(rc)) throw rc;
666 }
667
668 /* net services - nat networks */
669 for (settings::NATNetworksList::const_iterator it = m->pMainConfigFile->llNATNetworks.begin();
670 it != m->pMainConfigFile->llNATNetworks.end();
671 ++it)
672 {
673 const settings::NATNetwork &net = *it;
674
675 ComObjPtr<NATNetwork> pNATNetwork;
676 rc = pNATNetwork.createObject();
677 AssertComRCThrowRC(rc);
678 rc = pNATNetwork->init(this, "");
679 AssertComRCThrowRC(rc);
680 rc = pNATNetwork->i_loadSettings(net);
681 AssertComRCThrowRC(rc);
682 rc = i_registerNATNetwork(pNATNetwork, false /* aSaveRegistry */);
683 AssertComRCThrowRC(rc);
684 }
685
686 /* events */
687 if (SUCCEEDED(rc = unconst(m->pEventSource).createObject()))
688 rc = m->pEventSource->init();
689 if (FAILED(rc)) throw rc;
690
691 /* cloud provider manager */
692 rc = unconst(m->pCloudProviderManager).createObject();
693 if (SUCCEEDED(rc))
694 rc = m->pCloudProviderManager->init();
695 ComAssertComRCThrowRC(rc);
696 if (FAILED(rc)) throw rc;
697 }
698 catch (HRESULT err)
699 {
700 /* we assume that error info is set by the thrower */
701 rc = err;
702 }
703 catch (...)
704 {
705 rc = VirtualBoxBase::handleUnexpectedExceptions(this, RT_SRC_POS);
706 }
707
708 if (SUCCEEDED(rc))
709 {
710 /* set up client monitoring */
711 try
712 {
713 unconst(m->pClientWatcher) = new ClientWatcher(this);
714 if (!m->pClientWatcher->isReady())
715 {
716 delete m->pClientWatcher;
717 unconst(m->pClientWatcher) = NULL;
718 rc = E_FAIL;
719 }
720 }
721 catch (std::bad_alloc &)
722 {
723 rc = E_OUTOFMEMORY;
724 }
725 }
726
727 if (SUCCEEDED(rc))
728 {
729 try
730 {
731 /* start the async event handler thread */
732 int vrc = RTThreadCreate(&unconst(m->threadAsyncEvent),
733 AsyncEventHandler,
734 &unconst(m->pAsyncEventQ),
735 0,
736 RTTHREADTYPE_MAIN_WORKER,
737 RTTHREADFLAGS_WAITABLE,
738 "EventHandler");
739 ComAssertRCThrow(vrc, E_FAIL);
740
741 /* wait until the thread sets m->pAsyncEventQ */
742 RTThreadUserWait(m->threadAsyncEvent, RT_INDEFINITE_WAIT);
743 ComAssertThrow(m->pAsyncEventQ, E_FAIL);
744 }
745 catch (HRESULT aRC)
746 {
747 rc = aRC;
748 }
749 }
750
751#ifdef VBOX_WITH_EXTPACK
752 /* Let the extension packs have a go at things. */
753 if (SUCCEEDED(rc))
754 {
755 lock.release();
756 m->ptrExtPackManager->i_callAllVirtualBoxReadyHooks();
757 }
758#endif
759
760 /* Confirm a successful initialization when it's the case. Must be last,
761 * as on failure it will uninitialize the object. */
762 if (SUCCEEDED(rc))
763 autoInitSpan.setSucceeded();
764 else
765 autoInitSpan.setFailed(rc);
766
767 LogFlowThisFunc(("rc=%Rhrc\n", rc));
768 LogFlowThisFuncLeave();
769 LogFlow(("===========================================================\n"));
770 /* Unconditionally return success, because the error return is delayed to
771 * the attribute/method calls through the InitFailed object state. */
772 return S_OK;
773}
774
775HRESULT VirtualBox::initMachines()
776{
777 for (settings::MachinesRegistry::const_iterator it = m->pMainConfigFile->llMachines.begin();
778 it != m->pMainConfigFile->llMachines.end();
779 ++it)
780 {
781 HRESULT rc = S_OK;
782 const settings::MachineRegistryEntry &xmlMachine = *it;
783 Guid uuid = xmlMachine.uuid;
784
785 /* Check if machine record has valid parameters. */
786 if (xmlMachine.strSettingsFile.isEmpty() || uuid.isZero())
787 {
788 LogRel(("Skipped invalid machine record.\n"));
789 continue;
790 }
791
792 ComObjPtr<Machine> pMachine;
793 if (SUCCEEDED(rc = pMachine.createObject()))
794 {
795 rc = pMachine->initFromSettings(this,
796 xmlMachine.strSettingsFile,
797 &uuid);
798 if (SUCCEEDED(rc))
799 rc = i_registerMachine(pMachine);
800 if (FAILED(rc))
801 return rc;
802 }
803 }
804
805 return S_OK;
806}
807
808/**
809 * Loads a media registry from XML and adds the media contained therein to
810 * the global lists of known media.
811 *
812 * This now (4.0) gets called from two locations:
813 *
814 * -- VirtualBox::init(), to load the global media registry from VirtualBox.xml;
815 *
816 * -- Machine::loadMachineDataFromSettings(), to load the per-machine registry
817 * from machine XML, for machines created with VirtualBox 4.0 or later.
818 *
819 * In both cases, the media found are added to the global lists so the
820 * global arrays of media (including the GUI's virtual media manager)
821 * continue to work as before.
822 *
823 * @param uuidRegistry The UUID of the media registry. This is either the
824 * transient UUID created at VirtualBox startup for the global registry or
825 * a machine ID.
826 * @param mediaRegistry The XML settings structure to load, either from VirtualBox.xml
827 * or a machine XML.
828 * @param strMachineFolder The folder of the machine.
829 * @return
830 */
831HRESULT VirtualBox::initMedia(const Guid &uuidRegistry,
832 const settings::MediaRegistry &mediaRegistry,
833 const Utf8Str &strMachineFolder)
834{
835 LogFlow(("VirtualBox::initMedia ENTERING, uuidRegistry=%s, strMachineFolder=%s\n",
836 uuidRegistry.toString().c_str(),
837 strMachineFolder.c_str()));
838
839 AutoWriteLock treeLock(i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
840
841 std::map<Guid, DeviceType_T> uIdsForNotify;
842
843 HRESULT rc = S_OK;
844 settings::MediaList::const_iterator it;
845 for (it = mediaRegistry.llHardDisks.begin();
846 it != mediaRegistry.llHardDisks.end();
847 ++it)
848 {
849 const settings::Medium &xmlHD = *it;
850
851 ComObjPtr<Medium> pHardDisk;
852 if (SUCCEEDED(rc = pHardDisk.createObject()))
853 rc = pHardDisk->init(this,
854 NULL, // parent
855 DeviceType_HardDisk,
856 uuidRegistry,
857 xmlHD, // XML data; this recurses to processes the children
858 strMachineFolder,
859 treeLock);
860 if (FAILED(rc)) return rc;
861
862 rc = i_registerMedium(pHardDisk, &pHardDisk, treeLock);
863 if (SUCCEEDED(rc))
864 uIdsForNotify[pHardDisk->i_getId()] = DeviceType_HardDisk;
865 // Avoid trouble with lock/refcount, before returning or not.
866 treeLock.release();
867 pHardDisk.setNull();
868 treeLock.acquire();
869 if (FAILED(rc)) return rc;
870 }
871
872 for (it = mediaRegistry.llDvdImages.begin();
873 it != mediaRegistry.llDvdImages.end();
874 ++it)
875 {
876 const settings::Medium &xmlDvd = *it;
877
878 ComObjPtr<Medium> pImage;
879 if (SUCCEEDED(pImage.createObject()))
880 rc = pImage->init(this,
881 NULL,
882 DeviceType_DVD,
883 uuidRegistry,
884 xmlDvd,
885 strMachineFolder,
886 treeLock);
887 if (FAILED(rc)) return rc;
888
889 rc = i_registerMedium(pImage, &pImage, treeLock);
890 if (SUCCEEDED(rc))
891 uIdsForNotify[pImage->i_getId()] = DeviceType_DVD;
892 // Avoid trouble with lock/refcount, before returning or not.
893 treeLock.release();
894 pImage.setNull();
895 treeLock.acquire();
896 if (FAILED(rc)) return rc;
897 }
898
899 for (it = mediaRegistry.llFloppyImages.begin();
900 it != mediaRegistry.llFloppyImages.end();
901 ++it)
902 {
903 const settings::Medium &xmlFloppy = *it;
904
905 ComObjPtr<Medium> pImage;
906 if (SUCCEEDED(pImage.createObject()))
907 rc = pImage->init(this,
908 NULL,
909 DeviceType_Floppy,
910 uuidRegistry,
911 xmlFloppy,
912 strMachineFolder,
913 treeLock);
914 if (FAILED(rc)) return rc;
915
916 rc = i_registerMedium(pImage, &pImage, treeLock);
917 if (SUCCEEDED(rc))
918 uIdsForNotify[pImage->i_getId()] = DeviceType_Floppy;
919 // Avoid trouble with lock/refcount, before returning or not.
920 treeLock.release();
921 pImage.setNull();
922 treeLock.acquire();
923 if (FAILED(rc)) return rc;
924 }
925
926 if (SUCCEEDED(rc))
927 {
928 for (std::map<com::Guid, DeviceType_T>::const_iterator itItem = uIdsForNotify.begin();
929 itItem != uIdsForNotify.end();
930 ++itItem)
931 {
932 i_onMediumRegistered(itItem->first, itItem->second, TRUE);
933 }
934 }
935
936 LogFlow(("VirtualBox::initMedia LEAVING\n"));
937
938 return S_OK;
939}
940
941void VirtualBox::uninit()
942{
943 /* Must be done outside the AutoUninitSpan, as it expects AutoCaller to
944 * be successful. This needs additional checks to protect against double
945 * uninit, as then the pointer is NULL. */
946 if (RT_VALID_PTR(m))
947 {
948 Assert(!m->uRegistryNeedsSaving);
949 if (m->uRegistryNeedsSaving)
950 i_saveSettings();
951 }
952
953 /* Enclose the state transition Ready->InUninit->NotReady */
954 AutoUninitSpan autoUninitSpan(this);
955 if (autoUninitSpan.uninitDone())
956 return;
957
958 LogFlow(("===========================================================\n"));
959 LogFlowThisFuncEnter();
960 LogFlowThisFunc(("initFailed()=%d\n", autoUninitSpan.initFailed()));
961
962 /* tell all our child objects we've been uninitialized */
963
964 LogFlowThisFunc(("Uninitializing machines (%d)...\n", m->allMachines.size()));
965 if (m->pHost)
966 {
967 /* It is necessary to hold the VirtualBox and Host locks here because
968 we may have to uninitialize SessionMachines. */
969 AutoMultiWriteLock2 multilock(this, m->pHost COMMA_LOCKVAL_SRC_POS);
970 m->allMachines.uninitAll();
971 }
972 else
973 m->allMachines.uninitAll();
974 m->allFloppyImages.uninitAll();
975 m->allDVDImages.uninitAll();
976 m->allHardDisks.uninitAll();
977 m->allDHCPServers.uninitAll();
978
979 m->mapProgressOperations.clear();
980
981 m->allGuestOSTypes.uninitAll();
982
983 /* Note that we release singleton children after we've all other children.
984 * In some cases this is important because these other children may use
985 * some resources of the singletons which would prevent them from
986 * uninitializing (as for example, mSystemProperties which owns
987 * MediumFormat objects which Medium objects refer to) */
988 if (m->pCloudProviderManager)
989 {
990 m->pCloudProviderManager->uninit();
991 unconst(m->pCloudProviderManager).setNull();
992 }
993
994 if (m->pSystemProperties)
995 {
996 m->pSystemProperties->uninit();
997 unconst(m->pSystemProperties).setNull();
998 }
999
1000 if (m->pHost)
1001 {
1002 m->pHost->uninit();
1003 unconst(m->pHost).setNull();
1004 }
1005
1006#ifdef VBOX_WITH_RESOURCE_USAGE_API
1007 if (m->pPerformanceCollector)
1008 {
1009 m->pPerformanceCollector->uninit();
1010 unconst(m->pPerformanceCollector).setNull();
1011 }
1012#endif /* VBOX_WITH_RESOURCE_USAGE_API */
1013
1014#ifdef VBOX_WITH_EXTPACK
1015 if (m->ptrExtPackManager)
1016 {
1017 m->ptrExtPackManager->uninit();
1018 unconst(m->ptrExtPackManager).setNull();
1019 }
1020#endif
1021
1022#ifdef VBOX_WITH_SHARED_CLIPBOARD_URI_LIST
1023 LogFlowThisFunc(("Destroying Shared Clipboard areas...\n"));
1024 SharedClipboardAreaMap::iterator itArea = m->SharedClipboard.mapClipboardAreas.begin();
1025 while (itArea != m->SharedClipboard.mapClipboardAreas.end())
1026 {
1027 i_clipboardAreaDestroy(itArea->second);
1028 ++itArea;
1029 }
1030 m->SharedClipboard.mapClipboardAreas.clear();
1031#endif
1032
1033 LogFlowThisFunc(("Terminating the async event handler...\n"));
1034 if (m->threadAsyncEvent != NIL_RTTHREAD)
1035 {
1036 /* signal to exit the event loop */
1037 if (RT_SUCCESS(m->pAsyncEventQ->interruptEventQueueProcessing()))
1038 {
1039 /*
1040 * Wait for thread termination (only after we've successfully
1041 * interrupted the event queue processing!)
1042 */
1043 int vrc = RTThreadWait(m->threadAsyncEvent, 60000, NULL);
1044 if (RT_FAILURE(vrc))
1045 Log1WarningFunc(("RTThreadWait(%RTthrd) -> %Rrc\n", m->threadAsyncEvent, vrc));
1046 }
1047 else
1048 {
1049 AssertMsgFailed(("interruptEventQueueProcessing() failed\n"));
1050 RTThreadWait(m->threadAsyncEvent, 0, NULL);
1051 }
1052
1053 unconst(m->threadAsyncEvent) = NIL_RTTHREAD;
1054 unconst(m->pAsyncEventQ) = NULL;
1055 }
1056
1057 LogFlowThisFunc(("Releasing event source...\n"));
1058 if (m->pEventSource)
1059 {
1060 // Must uninit the event source here, because it makes no sense that
1061 // it survives longer than the base object. If someone gets an event
1062 // with such an event source then that's life and it has to be dealt
1063 // with appropriately on the API client side.
1064 m->pEventSource->uninit();
1065 unconst(m->pEventSource).setNull();
1066 }
1067
1068 LogFlowThisFunc(("Terminating the client watcher...\n"));
1069 if (m->pClientWatcher)
1070 {
1071 delete m->pClientWatcher;
1072 unconst(m->pClientWatcher) = NULL;
1073 }
1074
1075 delete m->pAutostartDb;
1076
1077 // clean up our instance data
1078 delete m;
1079 m = NULL;
1080
1081 /* Unload hard disk plugin backends. */
1082 VDShutdown();
1083
1084 LogFlowThisFuncLeave();
1085 LogFlow(("===========================================================\n"));
1086}
1087
1088// Wrapped IVirtualBox properties
1089/////////////////////////////////////////////////////////////////////////////
1090HRESULT VirtualBox::getVersion(com::Utf8Str &aVersion)
1091{
1092 aVersion = sVersion;
1093 return S_OK;
1094}
1095
1096HRESULT VirtualBox::getVersionNormalized(com::Utf8Str &aVersionNormalized)
1097{
1098 aVersionNormalized = sVersionNormalized;
1099 return S_OK;
1100}
1101
1102HRESULT VirtualBox::getRevision(ULONG *aRevision)
1103{
1104 *aRevision = sRevision;
1105 return S_OK;
1106}
1107
1108HRESULT VirtualBox::getPackageType(com::Utf8Str &aPackageType)
1109{
1110 aPackageType = sPackageType;
1111 return S_OK;
1112}
1113
1114HRESULT VirtualBox::getAPIVersion(com::Utf8Str &aAPIVersion)
1115{
1116 aAPIVersion = sAPIVersion;
1117 return S_OK;
1118}
1119
1120HRESULT VirtualBox::getAPIRevision(LONG64 *aAPIRevision)
1121{
1122 AssertCompile(VBOX_VERSION_MAJOR < 128 && VBOX_VERSION_MAJOR > 0);
1123 AssertCompile((uint64_t)VBOX_VERSION_MINOR < 256);
1124 uint64_t uRevision = ((uint64_t)VBOX_VERSION_MAJOR << 56)
1125 | ((uint64_t)VBOX_VERSION_MINOR << 48);
1126
1127 if (VBOX_VERSION_BUILD >= 51 && (VBOX_VERSION_BUILD & 1)) /* pre-release trunk */
1128 uRevision |= (uint64_t)VBOX_VERSION_BUILD << 40;
1129
1130 /** @todo This needs to be the same in OSE and non-OSE, preferrably
1131 * only changing when actual API changes happens. */
1132 uRevision |= 0;
1133
1134 *aAPIRevision = uRevision;
1135
1136 return S_OK;
1137}
1138
1139HRESULT VirtualBox::getHomeFolder(com::Utf8Str &aHomeFolder)
1140{
1141 /* mHomeDir is const and doesn't need a lock */
1142 aHomeFolder = m->strHomeDir;
1143 return S_OK;
1144}
1145
1146HRESULT VirtualBox::getSettingsFilePath(com::Utf8Str &aSettingsFilePath)
1147{
1148 /* mCfgFile.mName is const and doesn't need a lock */
1149 aSettingsFilePath = m->strSettingsFilePath;
1150 return S_OK;
1151}
1152
1153HRESULT VirtualBox::getHost(ComPtr<IHost> &aHost)
1154{
1155 /* mHost is const, no need to lock */
1156 m->pHost.queryInterfaceTo(aHost.asOutParam());
1157 return S_OK;
1158}
1159
1160HRESULT VirtualBox::getSystemProperties(ComPtr<ISystemProperties> &aSystemProperties)
1161{
1162 /* mSystemProperties is const, no need to lock */
1163 m->pSystemProperties.queryInterfaceTo(aSystemProperties.asOutParam());
1164 return S_OK;
1165}
1166
1167HRESULT VirtualBox::getMachines(std::vector<ComPtr<IMachine> > &aMachines)
1168{
1169 AutoReadLock al(m->allMachines.getLockHandle() COMMA_LOCKVAL_SRC_POS);
1170 aMachines.resize(m->allMachines.size());
1171 size_t i = 0;
1172 for (MachinesOList::const_iterator it= m->allMachines.begin();
1173 it!= m->allMachines.end(); ++it, ++i)
1174 (*it).queryInterfaceTo(aMachines[i].asOutParam());
1175 return S_OK;
1176}
1177
1178HRESULT VirtualBox::getMachineGroups(std::vector<com::Utf8Str> &aMachineGroups)
1179{
1180 std::list<com::Utf8Str> allGroups;
1181
1182 /* get copy of all machine references, to avoid holding the list lock */
1183 MachinesOList::MyList allMachines;
1184 {
1185 AutoReadLock al(m->allMachines.getLockHandle() COMMA_LOCKVAL_SRC_POS);
1186 allMachines = m->allMachines.getList();
1187 }
1188 for (MachinesOList::MyList::const_iterator it = allMachines.begin();
1189 it != allMachines.end();
1190 ++it)
1191 {
1192 const ComObjPtr<Machine> &pMachine = *it;
1193 AutoCaller autoMachineCaller(pMachine);
1194 if (FAILED(autoMachineCaller.rc()))
1195 continue;
1196 AutoReadLock mlock(pMachine COMMA_LOCKVAL_SRC_POS);
1197
1198 if (pMachine->i_isAccessible())
1199 {
1200 const StringsList &thisGroups = pMachine->i_getGroups();
1201 for (StringsList::const_iterator it2 = thisGroups.begin();
1202 it2 != thisGroups.end(); ++it2)
1203 allGroups.push_back(*it2);
1204 }
1205 }
1206
1207 /* throw out any duplicates */
1208 allGroups.sort();
1209 allGroups.unique();
1210 aMachineGroups.resize(allGroups.size());
1211 size_t i = 0;
1212 for (std::list<com::Utf8Str>::const_iterator it = allGroups.begin();
1213 it != allGroups.end(); ++it, ++i)
1214 aMachineGroups[i] = (*it);
1215 return S_OK;
1216}
1217
1218HRESULT VirtualBox::getHardDisks(std::vector<ComPtr<IMedium> > &aHardDisks)
1219{
1220 AutoReadLock al(m->allHardDisks.getLockHandle() COMMA_LOCKVAL_SRC_POS);
1221 aHardDisks.resize(m->allHardDisks.size());
1222 size_t i = 0;
1223 for (MediaOList::const_iterator it = m->allHardDisks.begin();
1224 it != m->allHardDisks.end(); ++it, ++i)
1225 (*it).queryInterfaceTo(aHardDisks[i].asOutParam());
1226 return S_OK;
1227}
1228
1229HRESULT VirtualBox::getDVDImages(std::vector<ComPtr<IMedium> > &aDVDImages)
1230{
1231 AutoReadLock al(m->allDVDImages.getLockHandle() COMMA_LOCKVAL_SRC_POS);
1232 aDVDImages.resize(m->allDVDImages.size());
1233 size_t i = 0;
1234 for (MediaOList::const_iterator it = m->allDVDImages.begin();
1235 it!= m->allDVDImages.end(); ++it, ++i)
1236 (*it).queryInterfaceTo(aDVDImages[i].asOutParam());
1237 return S_OK;
1238}
1239
1240HRESULT VirtualBox::getFloppyImages(std::vector<ComPtr<IMedium> > &aFloppyImages)
1241{
1242 AutoReadLock al(m->allFloppyImages.getLockHandle() COMMA_LOCKVAL_SRC_POS);
1243 aFloppyImages.resize(m->allFloppyImages.size());
1244 size_t i = 0;
1245 for (MediaOList::const_iterator it = m->allFloppyImages.begin();
1246 it != m->allFloppyImages.end(); ++it, ++i)
1247 (*it).queryInterfaceTo(aFloppyImages[i].asOutParam());
1248 return S_OK;
1249}
1250
1251HRESULT VirtualBox::getProgressOperations(std::vector<ComPtr<IProgress> > &aProgressOperations)
1252{
1253 /* protect mProgressOperations */
1254 AutoReadLock safeLock(m->mtxProgressOperations COMMA_LOCKVAL_SRC_POS);
1255 ProgressMap pmap(m->mapProgressOperations);
1256 aProgressOperations.resize(pmap.size());
1257 size_t i = 0;
1258 for (ProgressMap::iterator it = pmap.begin(); it != pmap.end(); ++it, ++i)
1259 it->second.queryInterfaceTo(aProgressOperations[i].asOutParam());
1260 return S_OK;
1261}
1262
1263HRESULT VirtualBox::getGuestOSTypes(std::vector<ComPtr<IGuestOSType> > &aGuestOSTypes)
1264{
1265 AutoReadLock al(m->allGuestOSTypes.getLockHandle() COMMA_LOCKVAL_SRC_POS);
1266 aGuestOSTypes.resize(m->allGuestOSTypes.size());
1267 size_t i = 0;
1268 for (GuestOSTypesOList::const_iterator it = m->allGuestOSTypes.begin();
1269 it != m->allGuestOSTypes.end(); ++it, ++i)
1270 (*it).queryInterfaceTo(aGuestOSTypes[i].asOutParam());
1271 return S_OK;
1272}
1273
1274HRESULT VirtualBox::getSharedFolders(std::vector<ComPtr<ISharedFolder> > &aSharedFolders)
1275{
1276 NOREF(aSharedFolders);
1277
1278 return setError(E_NOTIMPL, "Not yet implemented");
1279}
1280
1281HRESULT VirtualBox::getPerformanceCollector(ComPtr<IPerformanceCollector> &aPerformanceCollector)
1282{
1283#ifdef VBOX_WITH_RESOURCE_USAGE_API
1284 /* mPerformanceCollector is const, no need to lock */
1285 m->pPerformanceCollector.queryInterfaceTo(aPerformanceCollector.asOutParam());
1286
1287 return S_OK;
1288#else /* !VBOX_WITH_RESOURCE_USAGE_API */
1289 NOREF(aPerformanceCollector);
1290 ReturnComNotImplemented();
1291#endif /* !VBOX_WITH_RESOURCE_USAGE_API */
1292}
1293
1294HRESULT VirtualBox::getDHCPServers(std::vector<ComPtr<IDHCPServer> > &aDHCPServers)
1295{
1296 AutoReadLock al(m->allDHCPServers.getLockHandle() COMMA_LOCKVAL_SRC_POS);
1297 aDHCPServers.resize(m->allDHCPServers.size());
1298 size_t i = 0;
1299 for (DHCPServersOList::const_iterator it= m->allDHCPServers.begin();
1300 it!= m->allDHCPServers.end(); ++it, ++i)
1301 (*it).queryInterfaceTo(aDHCPServers[i].asOutParam());
1302 return S_OK;
1303}
1304
1305
1306HRESULT VirtualBox::getNATNetworks(std::vector<ComPtr<INATNetwork> > &aNATNetworks)
1307{
1308#ifdef VBOX_WITH_NAT_SERVICE
1309 AutoReadLock al(m->allNATNetworks.getLockHandle() COMMA_LOCKVAL_SRC_POS);
1310 aNATNetworks.resize(m->allNATNetworks.size());
1311 size_t i = 0;
1312 for (NATNetworksOList::const_iterator it= m->allNATNetworks.begin();
1313 it!= m->allNATNetworks.end(); ++it, ++i)
1314 (*it).queryInterfaceTo(aNATNetworks[i].asOutParam());
1315 return S_OK;
1316#else
1317 NOREF(aNATNetworks);
1318 return E_NOTIMPL;
1319#endif
1320}
1321
1322HRESULT VirtualBox::getEventSource(ComPtr<IEventSource> &aEventSource)
1323{
1324 /* event source is const, no need to lock */
1325 m->pEventSource.queryInterfaceTo(aEventSource.asOutParam());
1326 return S_OK;
1327}
1328
1329HRESULT VirtualBox::getExtensionPackManager(ComPtr<IExtPackManager> &aExtensionPackManager)
1330{
1331 HRESULT hrc = S_OK;
1332#ifdef VBOX_WITH_EXTPACK
1333 /* The extension pack manager is const, no need to lock. */
1334 hrc = m->ptrExtPackManager.queryInterfaceTo(aExtensionPackManager.asOutParam());
1335#else
1336 hrc = E_NOTIMPL;
1337 NOREF(aExtensionPackManager);
1338#endif
1339 return hrc;
1340}
1341
1342HRESULT VirtualBox::getInternalNetworks(std::vector<com::Utf8Str> &aInternalNetworks)
1343{
1344 std::list<com::Utf8Str> allInternalNetworks;
1345
1346 /* get copy of all machine references, to avoid holding the list lock */
1347 MachinesOList::MyList allMachines;
1348 {
1349 AutoReadLock al(m->allMachines.getLockHandle() COMMA_LOCKVAL_SRC_POS);
1350 allMachines = m->allMachines.getList();
1351 }
1352 for (MachinesOList::MyList::const_iterator it = allMachines.begin();
1353 it != allMachines.end(); ++it)
1354 {
1355 const ComObjPtr<Machine> &pMachine = *it;
1356 AutoCaller autoMachineCaller(pMachine);
1357 if (FAILED(autoMachineCaller.rc()))
1358 continue;
1359 AutoReadLock mlock(pMachine COMMA_LOCKVAL_SRC_POS);
1360
1361 if (pMachine->i_isAccessible())
1362 {
1363 uint32_t cNetworkAdapters = Global::getMaxNetworkAdapters(pMachine->i_getChipsetType());
1364 for (ULONG i = 0; i < cNetworkAdapters; i++)
1365 {
1366 ComPtr<INetworkAdapter> pNet;
1367 HRESULT rc = pMachine->GetNetworkAdapter(i, pNet.asOutParam());
1368 if (FAILED(rc) || pNet.isNull())
1369 continue;
1370 Bstr strInternalNetwork;
1371 rc = pNet->COMGETTER(InternalNetwork)(strInternalNetwork.asOutParam());
1372 if (FAILED(rc) || strInternalNetwork.isEmpty())
1373 continue;
1374
1375 allInternalNetworks.push_back(Utf8Str(strInternalNetwork));
1376 }
1377 }
1378 }
1379
1380 /* throw out any duplicates */
1381 allInternalNetworks.sort();
1382 allInternalNetworks.unique();
1383 size_t i = 0;
1384 aInternalNetworks.resize(allInternalNetworks.size());
1385 for (std::list<com::Utf8Str>::const_iterator it = allInternalNetworks.begin();
1386 it != allInternalNetworks.end();
1387 ++it, ++i)
1388 aInternalNetworks[i] = *it;
1389 return S_OK;
1390}
1391
1392HRESULT VirtualBox::getGenericNetworkDrivers(std::vector<com::Utf8Str> &aGenericNetworkDrivers)
1393{
1394 std::list<com::Utf8Str> allGenericNetworkDrivers;
1395
1396 /* get copy of all machine references, to avoid holding the list lock */
1397 MachinesOList::MyList allMachines;
1398 {
1399 AutoReadLock al(m->allMachines.getLockHandle() COMMA_LOCKVAL_SRC_POS);
1400 allMachines = m->allMachines.getList();
1401 }
1402 for (MachinesOList::MyList::const_iterator it = allMachines.begin();
1403 it != allMachines.end();
1404 ++it)
1405 {
1406 const ComObjPtr<Machine> &pMachine = *it;
1407 AutoCaller autoMachineCaller(pMachine);
1408 if (FAILED(autoMachineCaller.rc()))
1409 continue;
1410 AutoReadLock mlock(pMachine COMMA_LOCKVAL_SRC_POS);
1411
1412 if (pMachine->i_isAccessible())
1413 {
1414 uint32_t cNetworkAdapters = Global::getMaxNetworkAdapters(pMachine->i_getChipsetType());
1415 for (ULONG i = 0; i < cNetworkAdapters; i++)
1416 {
1417 ComPtr<INetworkAdapter> pNet;
1418 HRESULT rc = pMachine->GetNetworkAdapter(i, pNet.asOutParam());
1419 if (FAILED(rc) || pNet.isNull())
1420 continue;
1421 Bstr strGenericNetworkDriver;
1422 rc = pNet->COMGETTER(GenericDriver)(strGenericNetworkDriver.asOutParam());
1423 if (FAILED(rc) || strGenericNetworkDriver.isEmpty())
1424 continue;
1425
1426 allGenericNetworkDrivers.push_back(Utf8Str(strGenericNetworkDriver).c_str());
1427 }
1428 }
1429 }
1430
1431 /* throw out any duplicates */
1432 allGenericNetworkDrivers.sort();
1433 allGenericNetworkDrivers.unique();
1434 aGenericNetworkDrivers.resize(allGenericNetworkDrivers.size());
1435 size_t i = 0;
1436 for (std::list<com::Utf8Str>::const_iterator it = allGenericNetworkDrivers.begin();
1437 it != allGenericNetworkDrivers.end(); ++it, ++i)
1438 aGenericNetworkDrivers[i] = *it;
1439
1440 return S_OK;
1441}
1442
1443HRESULT VirtualBox::getCloudProviderManager(ComPtr<ICloudProviderManager> &aCloudProviderManager)
1444{
1445 HRESULT hrc = m->pCloudProviderManager.queryInterfaceTo(aCloudProviderManager.asOutParam());
1446 return hrc;
1447}
1448
1449HRESULT VirtualBox::checkFirmwarePresent(FirmwareType_T aFirmwareType,
1450 const com::Utf8Str &aVersion,
1451 com::Utf8Str &aUrl,
1452 com::Utf8Str &aFile,
1453 BOOL *aResult)
1454{
1455 NOREF(aVersion);
1456
1457 static const struct
1458 {
1459 FirmwareType_T type;
1460 const char* fileName;
1461 const char* url;
1462 }
1463 firmwareDesc[] =
1464 {
1465 {
1466 /* compiled-in firmware */
1467 FirmwareType_BIOS, NULL, NULL
1468 },
1469 {
1470 FirmwareType_EFI32, "VBoxEFI32.fd", "http://virtualbox.org/firmware/VBoxEFI32.fd"
1471 },
1472 {
1473 FirmwareType_EFI64, "VBoxEFI64.fd", "http://virtualbox.org/firmware/VBoxEFI64.fd"
1474 },
1475 {
1476 FirmwareType_EFIDUAL, "VBoxEFIDual.fd", "http://virtualbox.org/firmware/VBoxEFIDual.fd"
1477 }
1478 };
1479
1480 for (size_t i = 0; i < sizeof(firmwareDesc) / sizeof(firmwareDesc[0]); i++)
1481 {
1482 if (aFirmwareType != firmwareDesc[i].type)
1483 continue;
1484
1485 /* compiled-in firmware */
1486 if (firmwareDesc[i].fileName == NULL)
1487 {
1488 *aResult = TRUE;
1489 break;
1490 }
1491
1492 Utf8Str shortName, fullName;
1493
1494 shortName = Utf8StrFmt("Firmware%c%s",
1495 RTPATH_DELIMITER,
1496 firmwareDesc[i].fileName);
1497 int rc = i_calculateFullPath(shortName, fullName);
1498 AssertRCReturn(rc, VBOX_E_IPRT_ERROR);
1499 if (RTFileExists(fullName.c_str()))
1500 {
1501 *aResult = TRUE;
1502 aFile = fullName;
1503 break;
1504 }
1505
1506 char pszVBoxPath[RTPATH_MAX];
1507 rc = RTPathExecDir(pszVBoxPath, RTPATH_MAX);
1508 AssertRCReturn(rc, VBOX_E_IPRT_ERROR);
1509 fullName = Utf8StrFmt("%s%c%s",
1510 pszVBoxPath,
1511 RTPATH_DELIMITER,
1512 firmwareDesc[i].fileName);
1513 if (RTFileExists(fullName.c_str()))
1514 {
1515 *aResult = TRUE;
1516 aFile = fullName;
1517 break;
1518 }
1519
1520 /** @todo account for version in the URL */
1521 aUrl = firmwareDesc[i].url;
1522 *aResult = FALSE;
1523
1524 /* Assume single record per firmware type */
1525 break;
1526 }
1527
1528 return S_OK;
1529}
1530// Wrapped IVirtualBox methods
1531/////////////////////////////////////////////////////////////////////////////
1532
1533/* Helper for VirtualBox::ComposeMachineFilename */
1534static void sanitiseMachineFilename(Utf8Str &aName);
1535
1536HRESULT VirtualBox::composeMachineFilename(const com::Utf8Str &aName,
1537 const com::Utf8Str &aGroup,
1538 const com::Utf8Str &aCreateFlags,
1539 const com::Utf8Str &aBaseFolder,
1540 com::Utf8Str &aFile)
1541{
1542 if (RT_UNLIKELY(aName.isEmpty()))
1543 return setError(E_INVALIDARG, tr("Machine name is invalid, must not be empty"));
1544
1545 Utf8Str strBase = aBaseFolder;
1546 Utf8Str strName = aName;
1547
1548 LogFlowThisFunc(("aName=\"%s\",aBaseFolder=\"%s\"\n", strName.c_str(), strBase.c_str()));
1549
1550 com::Guid id;
1551 bool fDirectoryIncludesUUID = false;
1552 if (!aCreateFlags.isEmpty())
1553 {
1554 size_t uPos = 0;
1555 com::Utf8Str strKey;
1556 com::Utf8Str strValue;
1557 while ((uPos = aCreateFlags.parseKeyValue(strKey, strValue, uPos)) != com::Utf8Str::npos)
1558 {
1559 if (strKey == "UUID")
1560 id = strValue.c_str();
1561 else if (strKey == "directoryIncludesUUID")
1562 fDirectoryIncludesUUID = (strValue == "1");
1563 }
1564 }
1565
1566 if (id.isZero())
1567 fDirectoryIncludesUUID = false;
1568 else if (!id.isValid())
1569 {
1570 /* do something else */
1571 return setError(E_INVALIDARG,
1572 tr("'%s' is not a valid Guid"),
1573 id.toStringCurly().c_str());
1574 }
1575
1576 Utf8Str strGroup(aGroup);
1577 if (strGroup.isEmpty())
1578 strGroup = "/";
1579 HRESULT rc = i_validateMachineGroup(strGroup, true);
1580 if (FAILED(rc))
1581 return rc;
1582
1583 /* Compose the settings file name using the following scheme:
1584 *
1585 * <base_folder><group>/<machine_name>/<machine_name>.xml
1586 *
1587 * If a non-null and non-empty base folder is specified, the default
1588 * machine folder will be used as a base folder.
1589 * We sanitise the machine name to a safe white list of characters before
1590 * using it.
1591 */
1592 Utf8Str strDirName(strName);
1593 if (fDirectoryIncludesUUID)
1594 strDirName += Utf8StrFmt(" (%RTuuid)", id.raw());
1595 sanitiseMachineFilename(strName);
1596 sanitiseMachineFilename(strDirName);
1597
1598 if (strBase.isEmpty())
1599 /* we use the non-full folder value below to keep the path relative */
1600 i_getDefaultMachineFolder(strBase);
1601
1602 i_calculateFullPath(strBase, strBase);
1603
1604 /* eliminate toplevel group to avoid // in the result */
1605 if (strGroup == "/")
1606 strGroup.setNull();
1607 aFile = com::Utf8StrFmt("%s%s%c%s%c%s.vbox",
1608 strBase.c_str(),
1609 strGroup.c_str(),
1610 RTPATH_DELIMITER,
1611 strDirName.c_str(),
1612 RTPATH_DELIMITER,
1613 strName.c_str());
1614 return S_OK;
1615}
1616
1617/**
1618 * Remove characters from a machine file name which can be problematic on
1619 * particular systems.
1620 * @param strName The file name to sanitise.
1621 */
1622void sanitiseMachineFilename(Utf8Str &strName)
1623{
1624 if (strName.isEmpty())
1625 return;
1626
1627 /* Set of characters which should be safe for use in filenames: some basic
1628 * ASCII, Unicode from Latin-1 alphabetic to the end of Hangul. We try to
1629 * skip anything that could count as a control character in Windows or
1630 * *nix, or be otherwise difficult for shells to handle (I would have
1631 * preferred to remove the space and brackets too). We also remove all
1632 * characters which need UTF-16 surrogate pairs for Windows's benefit.
1633 */
1634 static RTUNICP const s_uszValidRangePairs[] =
1635 {
1636 ' ', ' ',
1637 '(', ')',
1638 '-', '.',
1639 '0', '9',
1640 'A', 'Z',
1641 'a', 'z',
1642 '_', '_',
1643 0xa0, 0xd7af,
1644 '\0'
1645 };
1646
1647 char *pszName = strName.mutableRaw();
1648 ssize_t cReplacements = RTStrPurgeComplementSet(pszName, s_uszValidRangePairs, '_');
1649 Assert(cReplacements >= 0);
1650 NOREF(cReplacements);
1651
1652 /* No leading dot or dash. */
1653 if (pszName[0] == '.' || pszName[0] == '-')
1654 pszName[0] = '_';
1655
1656 /* No trailing dot. */
1657 if (pszName[strName.length() - 1] == '.')
1658 pszName[strName.length() - 1] = '_';
1659
1660 /* Mangle leading and trailing spaces. */
1661 for (size_t i = 0; pszName[i] == ' '; ++i)
1662 pszName[i] = '_';
1663 for (size_t i = strName.length() - 1; i && pszName[i] == ' '; --i)
1664 pszName[i] = '_';
1665}
1666
1667#ifdef DEBUG
1668/** Simple unit test/operation examples for sanitiseMachineFilename(). */
1669static unsigned testSanitiseMachineFilename(DECLCALLBACKMEMBER(void, pfnPrintf)(const char *, ...))
1670{
1671 unsigned cErrors = 0;
1672
1673 /** Expected results of sanitising given file names. */
1674 static struct
1675 {
1676 /** The test file name to be sanitised (Utf-8). */
1677 const char *pcszIn;
1678 /** The expected sanitised output (Utf-8). */
1679 const char *pcszOutExpected;
1680 } aTest[] =
1681 {
1682 { "OS/2 2.1", "OS_2 2.1" },
1683 { "-!My VM!-", "__My VM_-" },
1684 { "\xF0\x90\x8C\xB0", "____" },
1685 { " My VM ", "__My VM__" },
1686 { ".My VM.", "_My VM_" },
1687 { "My VM", "My VM" }
1688 };
1689 for (unsigned i = 0; i < RT_ELEMENTS(aTest); ++i)
1690 {
1691 Utf8Str str(aTest[i].pcszIn);
1692 sanitiseMachineFilename(str);
1693 if (str.compare(aTest[i].pcszOutExpected))
1694 {
1695 ++cErrors;
1696 pfnPrintf("%s: line %d, expected %s, actual %s\n",
1697 __PRETTY_FUNCTION__, i, aTest[i].pcszOutExpected,
1698 str.c_str());
1699 }
1700 }
1701 return cErrors;
1702}
1703
1704/** @todo Proper testcase. */
1705/** @todo Do we have a better method of doing init functions? */
1706namespace
1707{
1708 class TestSanitiseMachineFilename
1709 {
1710 public:
1711 TestSanitiseMachineFilename(void)
1712 {
1713 Assert(!testSanitiseMachineFilename(RTAssertMsg2));
1714 }
1715 };
1716 TestSanitiseMachineFilename s_TestSanitiseMachineFilename;
1717}
1718#endif
1719
1720/** @note Locks mSystemProperties object for reading. */
1721HRESULT VirtualBox::createMachine(const com::Utf8Str &aSettingsFile,
1722 const com::Utf8Str &aName,
1723 const std::vector<com::Utf8Str> &aGroups,
1724 const com::Utf8Str &aOsTypeId,
1725 const com::Utf8Str &aFlags,
1726 ComPtr<IMachine> &aMachine)
1727{
1728 LogFlowThisFuncEnter();
1729 LogFlowThisFunc(("aSettingsFile=\"%s\", aName=\"%s\", aOsTypeId =\"%s\", aCreateFlags=\"%s\"\n",
1730 aSettingsFile.c_str(), aName.c_str(), aOsTypeId.c_str(), aFlags.c_str()));
1731
1732 StringsList llGroups;
1733 HRESULT rc = i_convertMachineGroups(aGroups, &llGroups);
1734 if (FAILED(rc))
1735 return rc;
1736
1737 Utf8Str strCreateFlags(aFlags);
1738 Guid id;
1739 bool fForceOverwrite = false;
1740 bool fDirectoryIncludesUUID = false;
1741 if (!strCreateFlags.isEmpty())
1742 {
1743 const char *pcszNext = strCreateFlags.c_str();
1744 while (*pcszNext != '\0')
1745 {
1746 Utf8Str strFlag;
1747 const char *pcszComma = RTStrStr(pcszNext, ",");
1748 if (!pcszComma)
1749 strFlag = pcszNext;
1750 else
1751 strFlag = Utf8Str(pcszNext, pcszComma - pcszNext);
1752
1753 const char *pcszEqual = RTStrStr(strFlag.c_str(), "=");
1754 /* skip over everything which doesn't contain '=' */
1755 if (pcszEqual && pcszEqual != strFlag.c_str())
1756 {
1757 Utf8Str strKey(strFlag.c_str(), pcszEqual - strFlag.c_str());
1758 Utf8Str strValue(strFlag.c_str() + (pcszEqual - strFlag.c_str() + 1));
1759
1760 if (strKey == "UUID")
1761 id = strValue.c_str();
1762 else if (strKey == "forceOverwrite")
1763 fForceOverwrite = (strValue == "1");
1764 else if (strKey == "directoryIncludesUUID")
1765 fDirectoryIncludesUUID = (strValue == "1");
1766 }
1767
1768 if (!pcszComma)
1769 pcszNext += strFlag.length();
1770 else
1771 pcszNext += strFlag.length() + 1;
1772 }
1773 }
1774 /* Create UUID if none was specified. */
1775 if (id.isZero())
1776 id.create();
1777 else if (!id.isValid())
1778 {
1779 /* do something else */
1780 return setError(E_INVALIDARG,
1781 tr("'%s' is not a valid Guid"),
1782 id.toStringCurly().c_str());
1783 }
1784
1785 /* NULL settings file means compose automatically */
1786 Utf8Str strSettingsFile(aSettingsFile);
1787 if (strSettingsFile.isEmpty())
1788 {
1789 Utf8Str strNewCreateFlags(Utf8StrFmt("UUID=%RTuuid", id.raw()));
1790 if (fDirectoryIncludesUUID)
1791 strNewCreateFlags += ",directoryIncludesUUID=1";
1792
1793 com::Utf8Str blstr = "";
1794 rc = composeMachineFilename(aName,
1795 llGroups.front(),
1796 strNewCreateFlags,
1797 blstr /* aBaseFolder */,
1798 strSettingsFile);
1799 if (FAILED(rc)) return rc;
1800 }
1801
1802 /* create a new object */
1803 ComObjPtr<Machine> machine;
1804 rc = machine.createObject();
1805 if (FAILED(rc)) return rc;
1806
1807 ComObjPtr<GuestOSType> osType;
1808 if (!aOsTypeId.isEmpty())
1809 i_findGuestOSType(aOsTypeId, osType);
1810
1811 /* initialize the machine object */
1812 rc = machine->init(this,
1813 strSettingsFile,
1814 aName,
1815 llGroups,
1816 aOsTypeId,
1817 osType,
1818 id,
1819 fForceOverwrite,
1820 fDirectoryIncludesUUID);
1821 if (SUCCEEDED(rc))
1822 {
1823 /* set the return value */
1824 machine.queryInterfaceTo(aMachine.asOutParam());
1825 AssertComRC(rc);
1826
1827#ifdef VBOX_WITH_EXTPACK
1828 /* call the extension pack hooks */
1829 m->ptrExtPackManager->i_callAllVmCreatedHooks(machine);
1830#endif
1831 }
1832
1833 LogFlowThisFuncLeave();
1834
1835 return rc;
1836}
1837
1838HRESULT VirtualBox::openMachine(const com::Utf8Str &aSettingsFile,
1839 ComPtr<IMachine> &aMachine)
1840{
1841 HRESULT rc = E_FAIL;
1842
1843 /* create a new object */
1844 ComObjPtr<Machine> machine;
1845 rc = machine.createObject();
1846 if (SUCCEEDED(rc))
1847 {
1848 /* initialize the machine object */
1849 rc = machine->initFromSettings(this,
1850 aSettingsFile,
1851 NULL); /* const Guid *aId */
1852 if (SUCCEEDED(rc))
1853 {
1854 /* set the return value */
1855 machine.queryInterfaceTo(aMachine.asOutParam());
1856 ComAssertComRC(rc);
1857 }
1858 }
1859
1860 return rc;
1861}
1862
1863/** @note Locks objects! */
1864HRESULT VirtualBox::registerMachine(const ComPtr<IMachine> &aMachine)
1865{
1866 HRESULT rc;
1867
1868 Bstr name;
1869 rc = aMachine->COMGETTER(Name)(name.asOutParam());
1870 if (FAILED(rc)) return rc;
1871
1872 /* We can safely cast child to Machine * here because only Machine
1873 * implementations of IMachine can be among our children. */
1874 IMachine *aM = aMachine;
1875 Machine *pMachine = static_cast<Machine*>(aM);
1876
1877 AutoCaller machCaller(pMachine);
1878 ComAssertComRCRetRC(machCaller.rc());
1879
1880 rc = i_registerMachine(pMachine);
1881 /* fire an event */
1882 if (SUCCEEDED(rc))
1883 i_onMachineRegistered(pMachine->i_getId(), TRUE);
1884
1885 return rc;
1886}
1887
1888/** @note Locks this object for reading, then some machine objects for reading. */
1889HRESULT VirtualBox::findMachine(const com::Utf8Str &aSettingsFile,
1890 ComPtr<IMachine> &aMachine)
1891{
1892 LogFlowThisFuncEnter();
1893 LogFlowThisFunc(("aSettingsFile=\"%s\", aMachine={%p}\n", aSettingsFile.c_str(), &aMachine));
1894
1895 /* start with not found */
1896 HRESULT rc = S_OK;
1897 ComObjPtr<Machine> pMachineFound;
1898
1899 Guid id(aSettingsFile);
1900 Utf8Str strFile(aSettingsFile);
1901 if (id.isValid() && !id.isZero())
1902
1903 rc = i_findMachine(id,
1904 true /* fPermitInaccessible */,
1905 true /* setError */,
1906 &pMachineFound);
1907 // returns VBOX_E_OBJECT_NOT_FOUND if not found and sets error
1908 else
1909 {
1910 rc = i_findMachineByName(strFile,
1911 true /* setError */,
1912 &pMachineFound);
1913 // returns VBOX_E_OBJECT_NOT_FOUND if not found and sets error
1914 }
1915
1916 /* this will set (*machine) to NULL if machineObj is null */
1917 pMachineFound.queryInterfaceTo(aMachine.asOutParam());
1918
1919 LogFlowThisFunc(("aName=\"%s\", aMachine=%p, rc=%08X\n", aSettingsFile.c_str(), &aMachine, rc));
1920 LogFlowThisFuncLeave();
1921
1922 return rc;
1923}
1924
1925HRESULT VirtualBox::getMachinesByGroups(const std::vector<com::Utf8Str> &aGroups,
1926 std::vector<ComPtr<IMachine> > &aMachines)
1927{
1928 StringsList llGroups;
1929 HRESULT rc = i_convertMachineGroups(aGroups, &llGroups);
1930 if (FAILED(rc))
1931 return rc;
1932
1933 /* we want to rely on sorted groups during compare, to save time */
1934 llGroups.sort();
1935
1936 /* get copy of all machine references, to avoid holding the list lock */
1937 MachinesOList::MyList allMachines;
1938 AutoReadLock al(m->allMachines.getLockHandle() COMMA_LOCKVAL_SRC_POS);
1939 allMachines = m->allMachines.getList();
1940
1941 std::vector<ComObjPtr<IMachine> > saMachines;
1942 saMachines.resize(0);
1943 for (MachinesOList::MyList::const_iterator it = allMachines.begin();
1944 it != allMachines.end();
1945 ++it)
1946 {
1947 const ComObjPtr<Machine> &pMachine = *it;
1948 AutoCaller autoMachineCaller(pMachine);
1949 if (FAILED(autoMachineCaller.rc()))
1950 continue;
1951 AutoReadLock mlock(pMachine COMMA_LOCKVAL_SRC_POS);
1952
1953 if (pMachine->i_isAccessible())
1954 {
1955 const StringsList &thisGroups = pMachine->i_getGroups();
1956 for (StringsList::const_iterator it2 = thisGroups.begin();
1957 it2 != thisGroups.end();
1958 ++it2)
1959 {
1960 const Utf8Str &group = *it2;
1961 bool fAppended = false;
1962 for (StringsList::const_iterator it3 = llGroups.begin();
1963 it3 != llGroups.end();
1964 ++it3)
1965 {
1966 int order = it3->compare(group);
1967 if (order == 0)
1968 {
1969 saMachines.push_back(static_cast<IMachine *>(pMachine));
1970 fAppended = true;
1971 break;
1972 }
1973 else if (order > 0)
1974 break;
1975 else
1976 continue;
1977 }
1978 /* avoid duplicates and save time */
1979 if (fAppended)
1980 break;
1981 }
1982 }
1983 }
1984 aMachines.resize(saMachines.size());
1985 size_t i = 0;
1986 for(i = 0; i < saMachines.size(); ++i)
1987 saMachines[i].queryInterfaceTo(aMachines[i].asOutParam());
1988
1989 return S_OK;
1990}
1991
1992HRESULT VirtualBox::getMachineStates(const std::vector<ComPtr<IMachine> > &aMachines,
1993 std::vector<MachineState_T> &aStates)
1994{
1995 com::SafeIfaceArray<IMachine> saMachines(aMachines);
1996 aStates.resize(aMachines.size());
1997 for (size_t i = 0; i < saMachines.size(); i++)
1998 {
1999 ComPtr<IMachine> pMachine = saMachines[i];
2000 MachineState_T state = MachineState_Null;
2001 if (!pMachine.isNull())
2002 {
2003 HRESULT rc = pMachine->COMGETTER(State)(&state);
2004 if (rc == E_ACCESSDENIED)
2005 rc = S_OK;
2006 AssertComRC(rc);
2007 }
2008 aStates[i] = state;
2009 }
2010 return S_OK;
2011}
2012
2013HRESULT VirtualBox::createUnattendedInstaller(ComPtr<IUnattended> &aUnattended)
2014{
2015#ifdef VBOX_WITH_UNATTENDED
2016 ComObjPtr<Unattended> ptrUnattended;
2017 HRESULT hrc = ptrUnattended.createObject();
2018 if (SUCCEEDED(hrc))
2019 {
2020 AutoReadLock wlock(this COMMA_LOCKVAL_SRC_POS);
2021 hrc = ptrUnattended->initUnattended(this);
2022 if (SUCCEEDED(hrc))
2023 hrc = ptrUnattended.queryInterfaceTo(aUnattended.asOutParam());
2024 }
2025 return hrc;
2026#else
2027 NOREF(aUnattended);
2028 return E_NOTIMPL;
2029#endif
2030}
2031
2032HRESULT VirtualBox::createMedium(const com::Utf8Str &aFormat,
2033 const com::Utf8Str &aLocation,
2034 AccessMode_T aAccessMode,
2035 DeviceType_T aDeviceType,
2036 ComPtr<IMedium> &aMedium)
2037{
2038 NOREF(aAccessMode); /**< @todo r=klaus make use of access mode */
2039
2040 HRESULT rc = S_OK;
2041
2042 ComObjPtr<Medium> medium;
2043 medium.createObject();
2044 com::Utf8Str format = aFormat;
2045
2046 switch (aDeviceType)
2047 {
2048 case DeviceType_HardDisk:
2049 {
2050
2051 /* we don't access non-const data members so no need to lock */
2052 if (format.isEmpty())
2053 i_getDefaultHardDiskFormat(format);
2054
2055 rc = medium->init(this,
2056 format,
2057 aLocation,
2058 Guid::Empty /* media registry: none yet */,
2059 aDeviceType);
2060 }
2061 break;
2062
2063 case DeviceType_DVD:
2064 case DeviceType_Floppy:
2065 {
2066
2067 if (format.isEmpty())
2068 return setError(E_INVALIDARG, "Format must be Valid Type%s", format.c_str());
2069
2070 // enforce read-only for DVDs even if caller specified ReadWrite
2071 if (aDeviceType == DeviceType_DVD)
2072 aAccessMode = AccessMode_ReadOnly;
2073
2074 rc = medium->init(this,
2075 format,
2076 aLocation,
2077 Guid::Empty /* media registry: none yet */,
2078 aDeviceType);
2079
2080 }
2081 break;
2082
2083 default:
2084 return setError(E_INVALIDARG, "Device type must be HardDisk, DVD or Floppy %d", aDeviceType);
2085 }
2086
2087 if (SUCCEEDED(rc))
2088 {
2089 medium.queryInterfaceTo(aMedium.asOutParam());
2090 com::Guid uMediumId = medium->i_getId();
2091 if (uMediumId.isValid() && !uMediumId.isZero())
2092 i_onMediumRegistered(uMediumId, medium->i_getDeviceType(), TRUE);
2093 }
2094
2095 return rc;
2096}
2097
2098HRESULT VirtualBox::openMedium(const com::Utf8Str &aLocation,
2099 DeviceType_T aDeviceType,
2100 AccessMode_T aAccessMode,
2101 BOOL aForceNewUuid,
2102 ComPtr<IMedium> &aMedium)
2103{
2104 HRESULT rc = S_OK;
2105 Guid id(aLocation);
2106 ComObjPtr<Medium> pMedium;
2107
2108 // have to get write lock as the whole find/update sequence must be done
2109 // in one critical section, otherwise there are races which can lead to
2110 // multiple Medium objects with the same content
2111 AutoWriteLock treeLock(i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
2112
2113 // check if the device type is correct, and see if a medium for the
2114 // given path has already initialized; if so, return that
2115 switch (aDeviceType)
2116 {
2117 case DeviceType_HardDisk:
2118 if (id.isValid() && !id.isZero())
2119 rc = i_findHardDiskById(id, false /* setError */, &pMedium);
2120 else
2121 rc = i_findHardDiskByLocation(aLocation,
2122 false, /* aSetError */
2123 &pMedium);
2124 break;
2125
2126 case DeviceType_Floppy:
2127 case DeviceType_DVD:
2128 if (id.isValid() && !id.isZero())
2129 rc = i_findDVDOrFloppyImage(aDeviceType, &id, Utf8Str::Empty,
2130 false /* setError */, &pMedium);
2131 else
2132 rc = i_findDVDOrFloppyImage(aDeviceType, NULL, aLocation,
2133 false /* setError */, &pMedium);
2134
2135 // enforce read-only for DVDs even if caller specified ReadWrite
2136 if (aDeviceType == DeviceType_DVD)
2137 aAccessMode = AccessMode_ReadOnly;
2138 break;
2139
2140 default:
2141 return setError(E_INVALIDARG, "Device type must be HardDisk, DVD or Floppy %d", aDeviceType);
2142 }
2143
2144 bool fMediumRegistered = false;
2145 if (pMedium.isNull())
2146 {
2147 pMedium.createObject();
2148 treeLock.release();
2149 rc = pMedium->init(this,
2150 aLocation,
2151 (aAccessMode == AccessMode_ReadWrite) ? Medium::OpenReadWrite : Medium::OpenReadOnly,
2152 !!aForceNewUuid,
2153 aDeviceType);
2154 treeLock.acquire();
2155
2156 if (SUCCEEDED(rc))
2157 {
2158 rc = i_registerMedium(pMedium, &pMedium, treeLock);
2159
2160 treeLock.release();
2161
2162 /* Note that it's important to call uninit() on failure to register
2163 * because the differencing hard disk would have been already associated
2164 * with the parent and this association needs to be broken. */
2165
2166 if (FAILED(rc))
2167 {
2168 pMedium->uninit();
2169 rc = VBOX_E_OBJECT_NOT_FOUND;
2170 }
2171 else
2172 {
2173 fMediumRegistered = true;
2174 }
2175 }
2176 else
2177 {
2178 if (rc != VBOX_E_INVALID_OBJECT_STATE)
2179 rc = VBOX_E_OBJECT_NOT_FOUND;
2180 }
2181 }
2182
2183 if (SUCCEEDED(rc))
2184 {
2185 pMedium.queryInterfaceTo(aMedium.asOutParam());
2186 if (fMediumRegistered)
2187 i_onMediumRegistered(pMedium->i_getId(), pMedium->i_getDeviceType() ,TRUE);
2188 }
2189
2190 return rc;
2191}
2192
2193
2194/** @note Locks this object for reading. */
2195HRESULT VirtualBox::getGuestOSType(const com::Utf8Str &aId,
2196 ComPtr<IGuestOSType> &aType)
2197{
2198 ComObjPtr<GuestOSType> pType;
2199 HRESULT rc = i_findGuestOSType(aId, pType);
2200 pType.queryInterfaceTo(aType.asOutParam());
2201 return rc;
2202}
2203
2204HRESULT VirtualBox::createSharedFolder(const com::Utf8Str &aName,
2205 const com::Utf8Str &aHostPath,
2206 BOOL aWritable,
2207 BOOL aAutomount,
2208 const com::Utf8Str &aAutoMountPoint)
2209{
2210 NOREF(aName);
2211 NOREF(aHostPath);
2212 NOREF(aWritable);
2213 NOREF(aAutomount);
2214 NOREF(aAutoMountPoint);
2215
2216 return setError(E_NOTIMPL, "Not yet implemented");
2217}
2218
2219HRESULT VirtualBox::removeSharedFolder(const com::Utf8Str &aName)
2220{
2221 NOREF(aName);
2222 return setError(E_NOTIMPL, "Not yet implemented");
2223}
2224
2225/**
2226 * @note Locks this object for reading.
2227 */
2228HRESULT VirtualBox::getExtraDataKeys(std::vector<com::Utf8Str> &aKeys)
2229{
2230 using namespace settings;
2231
2232 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2233
2234 aKeys.resize(m->pMainConfigFile->mapExtraDataItems.size());
2235 size_t i = 0;
2236 for (StringsMap::const_iterator it = m->pMainConfigFile->mapExtraDataItems.begin();
2237 it != m->pMainConfigFile->mapExtraDataItems.end(); ++it, ++i)
2238 aKeys[i] = it->first;
2239
2240 return S_OK;
2241}
2242
2243/**
2244 * @note Locks this object for reading.
2245 */
2246HRESULT VirtualBox::getExtraData(const com::Utf8Str &aKey,
2247 com::Utf8Str &aValue)
2248{
2249 settings::StringsMap::const_iterator it = m->pMainConfigFile->mapExtraDataItems.find(aKey);
2250 if (it != m->pMainConfigFile->mapExtraDataItems.end())
2251 // found:
2252 aValue = it->second; // source is a Utf8Str
2253
2254 /* return the result to caller (may be empty) */
2255
2256 return S_OK;
2257}
2258
2259/**
2260 * @note Locks this object for writing.
2261 */
2262HRESULT VirtualBox::setExtraData(const com::Utf8Str &aKey,
2263 const com::Utf8Str &aValue)
2264{
2265 Utf8Str strKey(aKey);
2266 Utf8Str strValue(aValue);
2267 Utf8Str strOldValue; // empty
2268 HRESULT rc = S_OK;
2269
2270 /* Because control characters in aKey have caused problems in the settings
2271 * they are rejected unless the key should be deleted. */
2272 if (!strValue.isEmpty())
2273 {
2274 for (size_t i = 0; i < strKey.length(); ++i)
2275 {
2276 char ch = strKey[i];
2277 if (RTLocCIsCntrl(ch))
2278 return E_INVALIDARG;
2279 }
2280 }
2281
2282 // locking note: we only hold the read lock briefly to look up the old value,
2283 // then release it and call the onExtraCanChange callbacks. There is a small
2284 // chance of a race insofar as the callback might be called twice if two callers
2285 // change the same key at the same time, but that's a much better solution
2286 // than the deadlock we had here before. The actual changing of the extradata
2287 // is then performed under the write lock and race-free.
2288
2289 // look up the old value first; if nothing has changed then we need not do anything
2290 {
2291 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS); // hold read lock only while looking up
2292 settings::StringsMap::const_iterator it = m->pMainConfigFile->mapExtraDataItems.find(strKey);
2293 if (it != m->pMainConfigFile->mapExtraDataItems.end())
2294 strOldValue = it->second;
2295 }
2296
2297 bool fChanged;
2298 if ((fChanged = (strOldValue != strValue)))
2299 {
2300 // ask for permission from all listeners outside the locks;
2301 // onExtraDataCanChange() only briefly requests the VirtualBox
2302 // lock to copy the list of callbacks to invoke
2303 Bstr error;
2304
2305 if (!i_onExtraDataCanChange(Guid::Empty, Bstr(aKey).raw(), Bstr(aValue).raw(), error))
2306 {
2307 const char *sep = error.isEmpty() ? "" : ": ";
2308 Log1WarningFunc(("Someone vetoed! Change refused%s%ls\n", sep, error.raw()));
2309 return setError(E_ACCESSDENIED,
2310 tr("Could not set extra data because someone refused the requested change of '%s' to '%s'%s%ls"),
2311 strKey.c_str(),
2312 strValue.c_str(),
2313 sep,
2314 error.raw());
2315 }
2316
2317 // data is changing and change not vetoed: then write it out under the lock
2318
2319 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2320
2321 if (strValue.isEmpty())
2322 m->pMainConfigFile->mapExtraDataItems.erase(strKey);
2323 else
2324 m->pMainConfigFile->mapExtraDataItems[strKey] = strValue;
2325 // creates a new key if needed
2326
2327 /* save settings on success */
2328 rc = i_saveSettings();
2329 if (FAILED(rc)) return rc;
2330 }
2331
2332 // fire notification outside the lock
2333 if (fChanged)
2334 i_onExtraDataChange(Guid::Empty, Bstr(aKey).raw(), Bstr(aValue).raw());
2335
2336 return rc;
2337}
2338
2339/**
2340 *
2341 */
2342HRESULT VirtualBox::setSettingsSecret(const com::Utf8Str &aPassword)
2343{
2344 i_storeSettingsKey(aPassword);
2345 i_decryptSettings();
2346 return S_OK;
2347}
2348
2349int VirtualBox::i_decryptMediumSettings(Medium *pMedium)
2350{
2351 Bstr bstrCipher;
2352 HRESULT hrc = pMedium->GetProperty(Bstr("InitiatorSecretEncrypted").raw(),
2353 bstrCipher.asOutParam());
2354 if (SUCCEEDED(hrc))
2355 {
2356 Utf8Str strPlaintext;
2357 int rc = i_decryptSetting(&strPlaintext, bstrCipher);
2358 if (RT_SUCCESS(rc))
2359 pMedium->i_setPropertyDirect("InitiatorSecret", strPlaintext);
2360 else
2361 return rc;
2362 }
2363 return VINF_SUCCESS;
2364}
2365
2366/**
2367 * Decrypt all encrypted settings.
2368 *
2369 * So far we only have encrypted iSCSI initiator secrets so we just go through
2370 * all hard disk mediums and determine the plain 'InitiatorSecret' from
2371 * 'InitiatorSecretEncrypted. The latter is stored as Base64 because medium
2372 * properties need to be null-terminated strings.
2373 */
2374int VirtualBox::i_decryptSettings()
2375{
2376 bool fFailure = false;
2377 AutoReadLock al(m->allHardDisks.getLockHandle() COMMA_LOCKVAL_SRC_POS);
2378 for (MediaList::const_iterator mt = m->allHardDisks.begin();
2379 mt != m->allHardDisks.end();
2380 ++mt)
2381 {
2382 ComObjPtr<Medium> pMedium = *mt;
2383 AutoCaller medCaller(pMedium);
2384 if (FAILED(medCaller.rc()))
2385 continue;
2386 AutoWriteLock mlock(pMedium COMMA_LOCKVAL_SRC_POS);
2387 int vrc = i_decryptMediumSettings(pMedium);
2388 if (RT_FAILURE(vrc))
2389 fFailure = true;
2390 }
2391 if (!fFailure)
2392 {
2393 for (MediaList::const_iterator mt = m->allHardDisks.begin();
2394 mt != m->allHardDisks.end();
2395 ++mt)
2396 {
2397 i_onMediumConfigChanged(*mt);
2398 }
2399 }
2400 return fFailure ? VERR_INVALID_PARAMETER : VINF_SUCCESS;
2401}
2402
2403/**
2404 * Encode.
2405 *
2406 * @param aPlaintext plaintext to be encrypted
2407 * @param aCiphertext resulting ciphertext (base64-encoded)
2408 */
2409int VirtualBox::i_encryptSetting(const Utf8Str &aPlaintext, Utf8Str *aCiphertext)
2410{
2411 uint8_t abCiphertext[32];
2412 char szCipherBase64[128];
2413 size_t cchCipherBase64;
2414 int rc = i_encryptSettingBytes((uint8_t*)aPlaintext.c_str(), abCiphertext,
2415 aPlaintext.length()+1, sizeof(abCiphertext));
2416 if (RT_SUCCESS(rc))
2417 {
2418 rc = RTBase64Encode(abCiphertext, sizeof(abCiphertext),
2419 szCipherBase64, sizeof(szCipherBase64),
2420 &cchCipherBase64);
2421 if (RT_SUCCESS(rc))
2422 *aCiphertext = szCipherBase64;
2423 }
2424 return rc;
2425}
2426
2427/**
2428 * Decode.
2429 *
2430 * @param aPlaintext resulting plaintext
2431 * @param aCiphertext ciphertext (base64-encoded) to decrypt
2432 */
2433int VirtualBox::i_decryptSetting(Utf8Str *aPlaintext, const Utf8Str &aCiphertext)
2434{
2435 uint8_t abPlaintext[64];
2436 uint8_t abCiphertext[64];
2437 size_t cbCiphertext;
2438 int rc = RTBase64Decode(aCiphertext.c_str(),
2439 abCiphertext, sizeof(abCiphertext),
2440 &cbCiphertext, NULL);
2441 if (RT_SUCCESS(rc))
2442 {
2443 rc = i_decryptSettingBytes(abPlaintext, abCiphertext, cbCiphertext);
2444 if (RT_SUCCESS(rc))
2445 {
2446 for (unsigned i = 0; i < cbCiphertext; i++)
2447 {
2448 /* sanity check: null-terminated string? */
2449 if (abPlaintext[i] == '\0')
2450 {
2451 /* sanity check: valid UTF8 string? */
2452 if (RTStrIsValidEncoding((const char*)abPlaintext))
2453 {
2454 *aPlaintext = Utf8Str((const char*)abPlaintext);
2455 return VINF_SUCCESS;
2456 }
2457 }
2458 }
2459 rc = VERR_INVALID_MAGIC;
2460 }
2461 }
2462 return rc;
2463}
2464
2465/**
2466 * Encrypt secret bytes. Use the m->SettingsCipherKey as key.
2467 *
2468 * @param aPlaintext clear text to be encrypted
2469 * @param aCiphertext resulting encrypted text
2470 * @param aPlaintextSize size of the plaintext
2471 * @param aCiphertextSize size of the ciphertext
2472 */
2473int VirtualBox::i_encryptSettingBytes(const uint8_t *aPlaintext, uint8_t *aCiphertext,
2474 size_t aPlaintextSize, size_t aCiphertextSize) const
2475{
2476 unsigned i, j;
2477 uint8_t aBytes[64];
2478
2479 if (!m->fSettingsCipherKeySet)
2480 return VERR_INVALID_STATE;
2481
2482 if (aCiphertextSize > sizeof(aBytes))
2483 return VERR_BUFFER_OVERFLOW;
2484
2485 if (aCiphertextSize < 32)
2486 return VERR_INVALID_PARAMETER;
2487
2488 AssertCompile(sizeof(m->SettingsCipherKey) >= 32);
2489
2490 /* store the first 8 bytes of the cipherkey for verification */
2491 for (i = 0, j = 0; i < 8; i++, j++)
2492 aCiphertext[i] = m->SettingsCipherKey[j];
2493
2494 for (unsigned k = 0; k < aPlaintextSize && i < aCiphertextSize; i++, k++)
2495 {
2496 aCiphertext[i] = (aPlaintext[k] ^ m->SettingsCipherKey[j]);
2497 if (++j >= sizeof(m->SettingsCipherKey))
2498 j = 0;
2499 }
2500
2501 /* fill with random data to have a minimal length (salt) */
2502 if (i < aCiphertextSize)
2503 {
2504 RTRandBytes(aBytes, aCiphertextSize - i);
2505 for (int k = 0; i < aCiphertextSize; i++, k++)
2506 {
2507 aCiphertext[i] = aBytes[k] ^ m->SettingsCipherKey[j];
2508 if (++j >= sizeof(m->SettingsCipherKey))
2509 j = 0;
2510 }
2511 }
2512
2513 return VINF_SUCCESS;
2514}
2515
2516/**
2517 * Decrypt secret bytes. Use the m->SettingsCipherKey as key.
2518 *
2519 * @param aPlaintext resulting plaintext
2520 * @param aCiphertext ciphertext to be decrypted
2521 * @param aCiphertextSize size of the ciphertext == size of the plaintext
2522 */
2523int VirtualBox::i_decryptSettingBytes(uint8_t *aPlaintext,
2524 const uint8_t *aCiphertext, size_t aCiphertextSize) const
2525{
2526 unsigned i, j;
2527
2528 if (!m->fSettingsCipherKeySet)
2529 return VERR_INVALID_STATE;
2530
2531 if (aCiphertextSize < 32)
2532 return VERR_INVALID_PARAMETER;
2533
2534 /* key verification */
2535 for (i = 0, j = 0; i < 8; i++, j++)
2536 if (aCiphertext[i] != m->SettingsCipherKey[j])
2537 return VERR_INVALID_MAGIC;
2538
2539 /* poison */
2540 memset(aPlaintext, 0xff, aCiphertextSize);
2541 for (int k = 0; i < aCiphertextSize; i++, k++)
2542 {
2543 aPlaintext[k] = aCiphertext[i] ^ m->SettingsCipherKey[j];
2544 if (++j >= sizeof(m->SettingsCipherKey))
2545 j = 0;
2546 }
2547
2548 return VINF_SUCCESS;
2549}
2550
2551/**
2552 * Store a settings key.
2553 *
2554 * @param aKey the key to store
2555 */
2556void VirtualBox::i_storeSettingsKey(const Utf8Str &aKey)
2557{
2558 RTSha512(aKey.c_str(), aKey.length(), m->SettingsCipherKey);
2559 m->fSettingsCipherKeySet = true;
2560}
2561
2562// public methods only for internal purposes
2563/////////////////////////////////////////////////////////////////////////////
2564
2565#ifdef DEBUG
2566void VirtualBox::i_dumpAllBackRefs()
2567{
2568 {
2569 AutoReadLock al(m->allHardDisks.getLockHandle() COMMA_LOCKVAL_SRC_POS);
2570 for (MediaList::const_iterator mt = m->allHardDisks.begin();
2571 mt != m->allHardDisks.end();
2572 ++mt)
2573 {
2574 ComObjPtr<Medium> pMedium = *mt;
2575 pMedium->i_dumpBackRefs();
2576 }
2577 }
2578 {
2579 AutoReadLock al(m->allDVDImages.getLockHandle() COMMA_LOCKVAL_SRC_POS);
2580 for (MediaList::const_iterator mt = m->allDVDImages.begin();
2581 mt != m->allDVDImages.end();
2582 ++mt)
2583 {
2584 ComObjPtr<Medium> pMedium = *mt;
2585 pMedium->i_dumpBackRefs();
2586 }
2587 }
2588}
2589#endif
2590
2591/**
2592 * Posts an event to the event queue that is processed asynchronously
2593 * on a dedicated thread.
2594 *
2595 * Posting events to the dedicated event queue is useful to perform secondary
2596 * actions outside any object locks -- for example, to iterate over a list
2597 * of callbacks and inform them about some change caused by some object's
2598 * method call.
2599 *
2600 * @param event event to post; must have been allocated using |new|, will
2601 * be deleted automatically by the event thread after processing
2602 *
2603 * @note Doesn't lock any object.
2604 */
2605HRESULT VirtualBox::i_postEvent(Event *event)
2606{
2607 AssertReturn(event, E_FAIL);
2608
2609 HRESULT rc;
2610 AutoCaller autoCaller(this);
2611 if (SUCCEEDED((rc = autoCaller.rc())))
2612 {
2613 if (getObjectState().getState() != ObjectState::Ready)
2614 Log1WarningFunc(("VirtualBox has been uninitialized (state=%d), the event is discarded!\n",
2615 getObjectState().getState()));
2616 // return S_OK
2617 else if ( (m->pAsyncEventQ)
2618 && (m->pAsyncEventQ->postEvent(event))
2619 )
2620 return S_OK;
2621 else
2622 rc = E_FAIL;
2623 }
2624
2625 // in any event of failure, we must clean up here, or we'll leak;
2626 // the caller has allocated the object using new()
2627 delete event;
2628 return rc;
2629}
2630
2631/**
2632 * Adds a progress to the global collection of pending operations.
2633 * Usually gets called upon progress object initialization.
2634 *
2635 * @param aProgress Operation to add to the collection.
2636 *
2637 * @note Doesn't lock objects.
2638 */
2639HRESULT VirtualBox::i_addProgress(IProgress *aProgress)
2640{
2641 CheckComArgNotNull(aProgress);
2642
2643 AutoCaller autoCaller(this);
2644 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2645
2646 Bstr id;
2647 HRESULT rc = aProgress->COMGETTER(Id)(id.asOutParam());
2648 AssertComRCReturnRC(rc);
2649
2650 /* protect mProgressOperations */
2651 AutoWriteLock safeLock(m->mtxProgressOperations COMMA_LOCKVAL_SRC_POS);
2652
2653 m->mapProgressOperations.insert(ProgressMap::value_type(Guid(id), aProgress));
2654 return S_OK;
2655}
2656
2657/**
2658 * Removes the progress from the global collection of pending operations.
2659 * Usually gets called upon progress completion.
2660 *
2661 * @param aId UUID of the progress operation to remove
2662 *
2663 * @note Doesn't lock objects.
2664 */
2665HRESULT VirtualBox::i_removeProgress(IN_GUID aId)
2666{
2667 AutoCaller autoCaller(this);
2668 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2669
2670 ComPtr<IProgress> progress;
2671
2672 /* protect mProgressOperations */
2673 AutoWriteLock safeLock(m->mtxProgressOperations COMMA_LOCKVAL_SRC_POS);
2674
2675 size_t cnt = m->mapProgressOperations.erase(aId);
2676 Assert(cnt == 1);
2677 NOREF(cnt);
2678
2679 return S_OK;
2680}
2681
2682#ifdef RT_OS_WINDOWS
2683
2684class StartSVCHelperClientData : public ThreadTask
2685{
2686public:
2687 StartSVCHelperClientData()
2688 {
2689 LogFlowFuncEnter();
2690 m_strTaskName = "SVCHelper";
2691 threadVoidData = NULL;
2692 initialized = false;
2693 }
2694
2695 virtual ~StartSVCHelperClientData()
2696 {
2697 LogFlowFuncEnter();
2698 if (threadVoidData!=NULL)
2699 {
2700 delete threadVoidData;
2701 threadVoidData=NULL;
2702 }
2703 };
2704
2705 void handler()
2706 {
2707 VirtualBox::i_SVCHelperClientThreadTask(this);
2708 }
2709
2710 const ComPtr<Progress>& GetProgressObject() const {return progress;}
2711
2712 bool init(VirtualBox* aVbox,
2713 Progress* aProgress,
2714 bool aPrivileged,
2715 VirtualBox::SVCHelperClientFunc aFunc,
2716 void *aUser)
2717 {
2718 LogFlowFuncEnter();
2719 that = aVbox;
2720 progress = aProgress;
2721 privileged = aPrivileged;
2722 func = aFunc;
2723 user = aUser;
2724
2725 initThreadVoidData();
2726
2727 initialized = true;
2728
2729 return initialized;
2730 }
2731
2732 bool isOk() const{ return initialized;}
2733
2734 bool initialized;
2735 ComObjPtr<VirtualBox> that;
2736 ComObjPtr<Progress> progress;
2737 bool privileged;
2738 VirtualBox::SVCHelperClientFunc func;
2739 void *user;
2740 ThreadVoidData *threadVoidData;
2741
2742private:
2743 bool initThreadVoidData()
2744 {
2745 LogFlowFuncEnter();
2746 threadVoidData = static_cast<ThreadVoidData*>(user);
2747 return true;
2748 }
2749};
2750
2751/**
2752 * Helper method that starts a worker thread that:
2753 * - creates a pipe communication channel using SVCHlpClient;
2754 * - starts an SVC Helper process that will inherit this channel;
2755 * - executes the supplied function by passing it the created SVCHlpClient
2756 * and opened instance to communicate to the Helper process and the given
2757 * Progress object.
2758 *
2759 * The user function is supposed to communicate to the helper process
2760 * using the \a aClient argument to do the requested job and optionally expose
2761 * the progress through the \a aProgress object. The user function should never
2762 * call notifyComplete() on it: this will be done automatically using the
2763 * result code returned by the function.
2764 *
2765 * Before the user function is started, the communication channel passed to
2766 * the \a aClient argument is fully set up, the function should start using
2767 * its write() and read() methods directly.
2768 *
2769 * The \a aVrc parameter of the user function may be used to return an error
2770 * code if it is related to communication errors (for example, returned by
2771 * the SVCHlpClient members when they fail). In this case, the correct error
2772 * message using this value will be reported to the caller. Note that the
2773 * value of \a aVrc is inspected only if the user function itself returns
2774 * success.
2775 *
2776 * If a failure happens anywhere before the user function would be normally
2777 * called, it will be called anyway in special "cleanup only" mode indicated
2778 * by \a aClient, \a aProgress and \aVrc arguments set to NULL. In this mode,
2779 * all the function is supposed to do is to cleanup its aUser argument if
2780 * necessary (it's assumed that the ownership of this argument is passed to
2781 * the user function once #startSVCHelperClient() returns a success, thus
2782 * making it responsible for the cleanup).
2783 *
2784 * After the user function returns, the thread will send the SVCHlpMsg::Null
2785 * message to indicate a process termination.
2786 *
2787 * @param aPrivileged |true| to start the SVC Helper process as a privileged
2788 * user that can perform administrative tasks
2789 * @param aFunc user function to run
2790 * @param aUser argument to the user function
2791 * @param aProgress progress object that will track operation completion
2792 *
2793 * @note aPrivileged is currently ignored (due to some unsolved problems in
2794 * Vista) and the process will be started as a normal (unprivileged)
2795 * process.
2796 *
2797 * @note Doesn't lock anything.
2798 */
2799HRESULT VirtualBox::i_startSVCHelperClient(bool aPrivileged,
2800 SVCHelperClientFunc aFunc,
2801 void *aUser, Progress *aProgress)
2802{
2803 LogFlowFuncEnter();
2804 AssertReturn(aFunc, E_POINTER);
2805 AssertReturn(aProgress, E_POINTER);
2806
2807 AutoCaller autoCaller(this);
2808 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2809
2810 /* create the i_SVCHelperClientThreadTask() argument */
2811
2812 HRESULT hr = S_OK;
2813 StartSVCHelperClientData *pTask = NULL;
2814 try
2815 {
2816 pTask = new StartSVCHelperClientData();
2817
2818 pTask->init(this, aProgress, aPrivileged, aFunc, aUser);
2819
2820 if (!pTask->isOk())
2821 {
2822 delete pTask;
2823 LogRel(("Could not init StartSVCHelperClientData object \n"));
2824 throw E_FAIL;
2825 }
2826
2827 //this function delete pTask in case of exceptions, so there is no need in the call of delete operator
2828 hr = pTask->createThreadWithType(RTTHREADTYPE_MAIN_WORKER);
2829
2830 }
2831 catch(std::bad_alloc &)
2832 {
2833 hr = setError(E_OUTOFMEMORY);
2834 }
2835 catch(...)
2836 {
2837 LogRel(("Could not create thread for StartSVCHelperClientData \n"));
2838 hr = E_FAIL;
2839 }
2840
2841 return hr;
2842}
2843
2844/**
2845 * Worker thread for startSVCHelperClient().
2846 */
2847/* static */
2848void VirtualBox::i_SVCHelperClientThreadTask(StartSVCHelperClientData *pTask)
2849{
2850 LogFlowFuncEnter();
2851 HRESULT rc = S_OK;
2852 bool userFuncCalled = false;
2853
2854 do
2855 {
2856 AssertBreakStmt(pTask, rc = E_POINTER);
2857 AssertReturnVoid(!pTask->progress.isNull());
2858
2859 /* protect VirtualBox from uninitialization */
2860 AutoCaller autoCaller(pTask->that);
2861 if (!autoCaller.isOk())
2862 {
2863 /* it's too late */
2864 rc = autoCaller.rc();
2865 break;
2866 }
2867
2868 int vrc = VINF_SUCCESS;
2869
2870 Guid id;
2871 id.create();
2872 SVCHlpClient client;
2873 vrc = client.create(Utf8StrFmt("VirtualBox\\SVCHelper\\{%RTuuid}",
2874 id.raw()).c_str());
2875 if (RT_FAILURE(vrc))
2876 {
2877 rc = pTask->that->setErrorBoth(E_FAIL, vrc, tr("Could not create the communication channel (%Rrc)"), vrc);
2878 break;
2879 }
2880
2881 /* get the path to the executable */
2882 char exePathBuf[RTPATH_MAX];
2883 char *exePath = RTProcGetExecutablePath(exePathBuf, RTPATH_MAX);
2884 if (!exePath)
2885 {
2886 rc = pTask->that->setError(E_FAIL, tr("Cannot get executable name"));
2887 break;
2888 }
2889
2890 Utf8Str argsStr = Utf8StrFmt("/Helper %s", client.name().c_str());
2891
2892 LogFlowFunc(("Starting '\"%s\" %s'...\n", exePath, argsStr.c_str()));
2893
2894 RTPROCESS pid = NIL_RTPROCESS;
2895
2896 if (pTask->privileged)
2897 {
2898 /* Attempt to start a privileged process using the Run As dialog */
2899
2900 Bstr file = exePath;
2901 Bstr parameters = argsStr;
2902
2903 SHELLEXECUTEINFO shExecInfo;
2904
2905 shExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
2906
2907 shExecInfo.fMask = NULL;
2908 shExecInfo.hwnd = NULL;
2909 shExecInfo.lpVerb = L"runas";
2910 shExecInfo.lpFile = file.raw();
2911 shExecInfo.lpParameters = parameters.raw();
2912 shExecInfo.lpDirectory = NULL;
2913 shExecInfo.nShow = SW_NORMAL;
2914 shExecInfo.hInstApp = NULL;
2915
2916 if (!ShellExecuteEx(&shExecInfo))
2917 {
2918 int vrc2 = RTErrConvertFromWin32(GetLastError());
2919 /* hide excessive details in case of a frequent error
2920 * (pressing the Cancel button to close the Run As dialog) */
2921 if (vrc2 == VERR_CANCELLED)
2922 rc = pTask->that->setErrorBoth(E_FAIL, vrc, tr("Operation canceled by the user"));
2923 else
2924 rc = pTask->that->setErrorBoth(E_FAIL, vrc, tr("Could not launch a privileged process '%s' (%Rrc)"), exePath, vrc2);
2925 break;
2926 }
2927 }
2928 else
2929 {
2930 const char *args[] = { exePath, "/Helper", client.name().c_str(), 0 };
2931 vrc = RTProcCreate(exePath, args, RTENV_DEFAULT, 0, &pid);
2932 if (RT_FAILURE(vrc))
2933 {
2934 rc = pTask->that->setErrorBoth(E_FAIL, vrc, tr("Could not launch a process '%s' (%Rrc)"), exePath, vrc);
2935 break;
2936 }
2937 }
2938
2939 /* wait for the client to connect */
2940 vrc = client.connect();
2941 if (RT_SUCCESS(vrc))
2942 {
2943 /* start the user supplied function */
2944 rc = pTask->func(&client, pTask->progress, pTask->user, &vrc);
2945 userFuncCalled = true;
2946 }
2947
2948 /* send the termination signal to the process anyway */
2949 {
2950 int vrc2 = client.write(SVCHlpMsg::Null);
2951 if (RT_SUCCESS(vrc))
2952 vrc = vrc2;
2953 }
2954
2955 if (SUCCEEDED(rc) && RT_FAILURE(vrc))
2956 {
2957 rc = pTask->that->setErrorBoth(E_FAIL, vrc, tr("Could not operate the communication channel (%Rrc)"), vrc);
2958 break;
2959 }
2960 }
2961 while (0);
2962
2963 if (FAILED(rc) && !userFuncCalled)
2964 {
2965 /* call the user function in the "cleanup only" mode
2966 * to let it free resources passed to in aUser */
2967 pTask->func(NULL, NULL, pTask->user, NULL);
2968 }
2969
2970 pTask->progress->i_notifyComplete(rc);
2971
2972 LogFlowFuncLeave();
2973}
2974
2975#endif /* RT_OS_WINDOWS */
2976
2977/**
2978 * Sends a signal to the client watcher to rescan the set of machines
2979 * that have open sessions.
2980 *
2981 * @note Doesn't lock anything.
2982 */
2983void VirtualBox::i_updateClientWatcher()
2984{
2985 AutoCaller autoCaller(this);
2986 AssertComRCReturnVoid(autoCaller.rc());
2987
2988 AssertPtrReturnVoid(m->pClientWatcher);
2989 m->pClientWatcher->update();
2990}
2991
2992/**
2993 * Adds the given child process ID to the list of processes to be reaped.
2994 * This call should be followed by #i_updateClientWatcher() to take the effect.
2995 *
2996 * @note Doesn't lock anything.
2997 */
2998void VirtualBox::i_addProcessToReap(RTPROCESS pid)
2999{
3000 AutoCaller autoCaller(this);
3001 AssertComRCReturnVoid(autoCaller.rc());
3002
3003 AssertPtrReturnVoid(m->pClientWatcher);
3004 m->pClientWatcher->addProcess(pid);
3005}
3006
3007/** Event for onMachineStateChange(), onMachineDataChange(), onMachineRegistered() */
3008struct MachineEvent : public VirtualBox::CallbackEvent
3009{
3010 MachineEvent(VirtualBox *aVB, VBoxEventType_T aWhat, const Guid &aId, BOOL aBool)
3011 : CallbackEvent(aVB, aWhat), id(aId.toUtf16())
3012 , mBool(aBool)
3013 { }
3014
3015 MachineEvent(VirtualBox *aVB, VBoxEventType_T aWhat, const Guid &aId, MachineState_T aState)
3016 : CallbackEvent(aVB, aWhat), id(aId.toUtf16())
3017 , mState(aState)
3018 {}
3019
3020 virtual HRESULT prepareEventDesc(IEventSource* aSource, VBoxEventDesc& aEvDesc)
3021 {
3022 switch (mWhat)
3023 {
3024 case VBoxEventType_OnMachineDataChanged:
3025 aEvDesc.init(aSource, mWhat, id.raw(), mBool);
3026 break;
3027
3028 case VBoxEventType_OnMachineStateChanged:
3029 aEvDesc.init(aSource, mWhat, id.raw(), mState);
3030 break;
3031
3032 case VBoxEventType_OnMachineRegistered:
3033 aEvDesc.init(aSource, mWhat, id.raw(), mBool);
3034 break;
3035
3036 default:
3037 AssertFailedReturn(S_OK);
3038 }
3039 return S_OK;
3040 }
3041
3042 Bstr id;
3043 MachineState_T mState;
3044 BOOL mBool;
3045};
3046
3047
3048/**
3049 * VD plugin load
3050 */
3051int VirtualBox::i_loadVDPlugin(const char *pszPluginLibrary)
3052{
3053 return m->pSystemProperties->i_loadVDPlugin(pszPluginLibrary);
3054}
3055
3056/**
3057 * VD plugin unload
3058 */
3059int VirtualBox::i_unloadVDPlugin(const char *pszPluginLibrary)
3060{
3061 return m->pSystemProperties->i_unloadVDPlugin(pszPluginLibrary);
3062}
3063
3064
3065/** Event for onMediumRegistered() */
3066struct MediumRegisteredEventStruct : public VirtualBox::CallbackEvent
3067{
3068 MediumRegisteredEventStruct(VirtualBox *aVB, const Guid &aMediumId,
3069 const DeviceType_T aDevType, const BOOL aRegistered)
3070 : CallbackEvent(aVB, VBoxEventType_OnMediumRegistered)
3071 , mMediumId(aMediumId.toUtf16()), mDevType(aDevType), mRegistered(aRegistered)
3072 {}
3073
3074 virtual HRESULT prepareEventDesc(IEventSource *aSource, VBoxEventDesc &aEvDesc)
3075 {
3076 return aEvDesc.init(aSource, VBoxEventType_OnMediumRegistered, mMediumId.raw(), mDevType, mRegistered);
3077 }
3078
3079 Bstr mMediumId;
3080 DeviceType_T mDevType;
3081 BOOL mRegistered;
3082};
3083
3084/**
3085 * @note Doesn't lock any object.
3086 */
3087void VirtualBox::i_onMediumRegistered(const Guid &aMediumId, const DeviceType_T aDevType, const BOOL aRegistered)
3088{
3089 i_postEvent(new MediumRegisteredEventStruct(this, aMediumId, aDevType, aRegistered));
3090}
3091
3092/** Event for onMediumConfigChanged() */
3093struct MediumConfigChangedEventStruct : public VirtualBox::CallbackEvent
3094{
3095 MediumConfigChangedEventStruct(VirtualBox *aVB, IMedium *aMedium)
3096 : CallbackEvent(aVB, VBoxEventType_OnMediumConfigChanged)
3097 , mMedium(aMedium)
3098 {}
3099
3100 virtual HRESULT prepareEventDesc(IEventSource *aSource, VBoxEventDesc &aEvDesc)
3101 {
3102 return aEvDesc.init(aSource, VBoxEventType_OnMediumConfigChanged, mMedium);
3103 }
3104
3105 IMedium* mMedium;
3106};
3107
3108void VirtualBox::i_onMediumConfigChanged(IMedium *aMedium)
3109{
3110 i_postEvent(new MediumConfigChangedEventStruct(this, aMedium));
3111}
3112
3113/** Event for onMediumChanged() */
3114struct MediumChangedEventStruct : public VirtualBox::CallbackEvent
3115{
3116 MediumChangedEventStruct(VirtualBox *aVB, IMediumAttachment *aMediumAttachment)
3117 : CallbackEvent(aVB, VBoxEventType_OnMediumChanged)
3118 , mMediumAttachment(aMediumAttachment)
3119 {}
3120
3121 virtual HRESULT prepareEventDesc(IEventSource *aSource, VBoxEventDesc &aEvDesc)
3122 {
3123 return aEvDesc.init(aSource, VBoxEventType_OnMediumChanged, mMediumAttachment);
3124 }
3125
3126 IMediumAttachment* mMediumAttachment;
3127};
3128
3129void VirtualBox::i_onMediumChanged(IMediumAttachment *aMediumAttachment)
3130{
3131 i_postEvent(new MediumChangedEventStruct(this, aMediumAttachment));
3132}
3133
3134/** Event for onStorageControllerChanged() */
3135struct StorageControllerChangedEventStruct : public VirtualBox::CallbackEvent
3136{
3137 StorageControllerChangedEventStruct(VirtualBox *aVB, const Guid &aMachineId,
3138 const com::Utf8Str &aControllerName)
3139 : CallbackEvent(aVB, VBoxEventType_OnStorageControllerChanged)
3140 , mMachineId(aMachineId.toUtf16()), mControllerName(aControllerName)
3141 {}
3142
3143 virtual HRESULT prepareEventDesc(IEventSource *aSource, VBoxEventDesc &aEvDesc)
3144 {
3145 return aEvDesc.init(aSource, VBoxEventType_OnStorageControllerChanged, mMachineId.raw(), mControllerName.raw());
3146 }
3147
3148 Bstr mMachineId;
3149 Bstr mControllerName;
3150};
3151
3152/**
3153 * @note Doesn't lock any object.
3154 */
3155void VirtualBox::i_onStorageControllerChanged(const Guid &aMachineId, const com::Utf8Str &aControllerName)
3156{
3157 i_postEvent(new StorageControllerChangedEventStruct(this, aMachineId, aControllerName));
3158}
3159
3160/** Event for onStorageDeviceChanged() */
3161struct StorageDeviceChangedEventStruct : public VirtualBox::CallbackEvent
3162{
3163 StorageDeviceChangedEventStruct(VirtualBox *aVB, IMediumAttachment *aStorageDevice, BOOL fRemoved, BOOL fSilent)
3164 : CallbackEvent(aVB, VBoxEventType_OnStorageDeviceChanged)
3165 , mStorageDevice(aStorageDevice)
3166 , mRemoved(fRemoved)
3167 , mSilent(fSilent)
3168 {}
3169
3170 virtual HRESULT prepareEventDesc(IEventSource *aSource, VBoxEventDesc &aEvDesc)
3171 {
3172 return aEvDesc.init(aSource, VBoxEventType_OnStorageDeviceChanged, mStorageDevice, mRemoved, mSilent);
3173 }
3174
3175 IMediumAttachment* mStorageDevice;
3176 BOOL mRemoved;
3177 BOOL mSilent;
3178};
3179
3180void VirtualBox::i_onStorageDeviceChanged(IMediumAttachment *aStorageDevice, const BOOL fRemoved, const BOOL fSilent)
3181{
3182 i_postEvent(new StorageDeviceChangedEventStruct(this, aStorageDevice, fRemoved, fSilent));
3183}
3184
3185/**
3186 * @note Doesn't lock any object.
3187 */
3188void VirtualBox::i_onMachineStateChange(const Guid &aId, MachineState_T aState)
3189{
3190 i_postEvent(new MachineEvent(this, VBoxEventType_OnMachineStateChanged, aId, aState));
3191}
3192
3193/**
3194 * @note Doesn't lock any object.
3195 */
3196void VirtualBox::i_onMachineDataChange(const Guid &aId, BOOL aTemporary)
3197{
3198 i_postEvent(new MachineEvent(this, VBoxEventType_OnMachineDataChanged, aId, aTemporary));
3199}
3200
3201/**
3202 * @note Locks this object for reading.
3203 */
3204BOOL VirtualBox::i_onExtraDataCanChange(const Guid &aId, IN_BSTR aKey, IN_BSTR aValue,
3205 Bstr &aError)
3206{
3207 LogFlowThisFunc(("machine={%s} aKey={%ls} aValue={%ls}\n",
3208 aId.toString().c_str(), aKey, aValue));
3209
3210 AutoCaller autoCaller(this);
3211 AssertComRCReturn(autoCaller.rc(), FALSE);
3212
3213 BOOL allowChange = TRUE;
3214 Bstr id = aId.toUtf16();
3215
3216 VBoxEventDesc evDesc;
3217 evDesc.init(m->pEventSource, VBoxEventType_OnExtraDataCanChange, id.raw(), aKey, aValue);
3218 BOOL fDelivered = evDesc.fire(3000); /* Wait up to 3 secs for delivery */
3219 //Assert(fDelivered);
3220 if (fDelivered)
3221 {
3222 ComPtr<IEvent> aEvent;
3223 evDesc.getEvent(aEvent.asOutParam());
3224 ComPtr<IExtraDataCanChangeEvent> aCanChangeEvent = aEvent;
3225 Assert(aCanChangeEvent);
3226 BOOL fVetoed = FALSE;
3227 aCanChangeEvent->IsVetoed(&fVetoed);
3228 allowChange = !fVetoed;
3229
3230 if (!allowChange)
3231 {
3232 SafeArray<BSTR> aVetos;
3233 aCanChangeEvent->GetVetos(ComSafeArrayAsOutParam(aVetos));
3234 if (aVetos.size() > 0)
3235 aError = aVetos[0];
3236 }
3237 }
3238 else
3239 allowChange = TRUE;
3240
3241 LogFlowThisFunc(("allowChange=%RTbool\n", allowChange));
3242 return allowChange;
3243}
3244
3245/** Event for onExtraDataChange() */
3246struct ExtraDataEvent : public VirtualBox::CallbackEvent
3247{
3248 ExtraDataEvent(VirtualBox *aVB, const Guid &aMachineId,
3249 IN_BSTR aKey, IN_BSTR aVal)
3250 : CallbackEvent(aVB, VBoxEventType_OnExtraDataChanged)
3251 , machineId(aMachineId.toUtf16()), key(aKey), val(aVal)
3252 {}
3253
3254 virtual HRESULT prepareEventDesc(IEventSource* aSource, VBoxEventDesc& aEvDesc)
3255 {
3256 return aEvDesc.init(aSource, VBoxEventType_OnExtraDataChanged, machineId.raw(), key.raw(), val.raw());
3257 }
3258
3259 Bstr machineId, key, val;
3260};
3261
3262/**
3263 * @note Doesn't lock any object.
3264 */
3265void VirtualBox::i_onExtraDataChange(const Guid &aId, IN_BSTR aKey, IN_BSTR aValue)
3266{
3267 i_postEvent(new ExtraDataEvent(this, aId, aKey, aValue));
3268}
3269
3270/**
3271 * @note Doesn't lock any object.
3272 */
3273void VirtualBox::i_onMachineRegistered(const Guid &aId, BOOL aRegistered)
3274{
3275 i_postEvent(new MachineEvent(this, VBoxEventType_OnMachineRegistered, aId, aRegistered));
3276}
3277
3278/** Event for onSessionStateChange() */
3279struct SessionEvent : public VirtualBox::CallbackEvent
3280{
3281 SessionEvent(VirtualBox *aVB, const Guid &aMachineId, SessionState_T aState)
3282 : CallbackEvent(aVB, VBoxEventType_OnSessionStateChanged)
3283 , machineId(aMachineId.toUtf16()), sessionState(aState)
3284 {}
3285
3286 virtual HRESULT prepareEventDesc(IEventSource* aSource, VBoxEventDesc& aEvDesc)
3287 {
3288 return aEvDesc.init(aSource, VBoxEventType_OnSessionStateChanged, machineId.raw(), sessionState);
3289 }
3290 Bstr machineId;
3291 SessionState_T sessionState;
3292};
3293
3294/**
3295 * @note Doesn't lock any object.
3296 */
3297void VirtualBox::i_onSessionStateChange(const Guid &aId, SessionState_T aState)
3298{
3299 i_postEvent(new SessionEvent(this, aId, aState));
3300}
3301
3302/** Event for i_onSnapshotTaken(), i_onSnapshotDeleted(), i_onSnapshotRestored() and i_onSnapshotChange() */
3303struct SnapshotEvent : public VirtualBox::CallbackEvent
3304{
3305 SnapshotEvent(VirtualBox *aVB, const Guid &aMachineId, const Guid &aSnapshotId,
3306 VBoxEventType_T aWhat)
3307 : CallbackEvent(aVB, aWhat)
3308 , machineId(aMachineId), snapshotId(aSnapshotId)
3309 {}
3310
3311 virtual HRESULT prepareEventDesc(IEventSource* aSource, VBoxEventDesc& aEvDesc)
3312 {
3313 return aEvDesc.init(aSource, mWhat, machineId.toUtf16().raw(),
3314 snapshotId.toUtf16().raw());
3315 }
3316
3317 Guid machineId;
3318 Guid snapshotId;
3319};
3320
3321/**
3322 * @note Doesn't lock any object.
3323 */
3324void VirtualBox::i_onSnapshotTaken(const Guid &aMachineId, const Guid &aSnapshotId)
3325{
3326 i_postEvent(new SnapshotEvent(this, aMachineId, aSnapshotId,
3327 VBoxEventType_OnSnapshotTaken));
3328}
3329
3330/**
3331 * @note Doesn't lock any object.
3332 */
3333void VirtualBox::i_onSnapshotDeleted(const Guid &aMachineId, const Guid &aSnapshotId)
3334{
3335 i_postEvent(new SnapshotEvent(this, aMachineId, aSnapshotId,
3336 VBoxEventType_OnSnapshotDeleted));
3337}
3338
3339/**
3340 * @note Doesn't lock any object.
3341 */
3342void VirtualBox::i_onSnapshotRestored(const Guid &aMachineId, const Guid &aSnapshotId)
3343{
3344 i_postEvent(new SnapshotEvent(this, aMachineId, aSnapshotId,
3345 VBoxEventType_OnSnapshotRestored));
3346}
3347
3348/**
3349 * @note Doesn't lock any object.
3350 */
3351void VirtualBox::i_onSnapshotChange(const Guid &aMachineId, const Guid &aSnapshotId)
3352{
3353 i_postEvent(new SnapshotEvent(this, aMachineId, aSnapshotId,
3354 VBoxEventType_OnSnapshotChanged));
3355}
3356
3357/** Event for onGuestPropertyChange() */
3358struct GuestPropertyEvent : public VirtualBox::CallbackEvent
3359{
3360 GuestPropertyEvent(VirtualBox *aVBox, const Guid &aMachineId,
3361 IN_BSTR aName, IN_BSTR aValue, IN_BSTR aFlags)
3362 : CallbackEvent(aVBox, VBoxEventType_OnGuestPropertyChanged),
3363 machineId(aMachineId),
3364 name(aName),
3365 value(aValue),
3366 flags(aFlags)
3367 {}
3368
3369 virtual HRESULT prepareEventDesc(IEventSource* aSource, VBoxEventDesc& aEvDesc)
3370 {
3371 return aEvDesc.init(aSource, VBoxEventType_OnGuestPropertyChanged,
3372 machineId.toUtf16().raw(), name.raw(), value.raw(), flags.raw());
3373 }
3374
3375 Guid machineId;
3376 Bstr name, value, flags;
3377};
3378
3379#ifdef VBOX_WITH_SHARED_CLIPBOARD_URI_LIST
3380/**
3381 * Generates a new clipboard area on the host by opening (and locking) a new, temporary directory.
3382 *
3383 * @returns HRESULT
3384 * @param AreaData Area data to use for clipboard area creation.
3385 * @param fFlags Additional creation flags; currently unused and ignored.
3386 */
3387int VirtualBox::i_clipboardAreaCreate(SharedClipboardAreaData &AreaData, uint32_t fFlags)
3388{
3389 RT_NOREF(fFlags);
3390 int vrc = AreaData.Area.OpenTemp(AreaData.uID, SHAREDCLIPBOARDAREA_OPEN_FLAGS_MUST_NOT_EXIST);
3391 LogFlowFunc(("uID=%RU32, rc=%Rrc\n", AreaData.uID, vrc));
3392 return vrc;
3393}
3394
3395/**
3396 * Destroys a formerly created clipboard area.
3397 *
3398 * @returns HRESULT
3399 * @param AreaData Area data to use for clipboard area destruction.
3400 */
3401int VirtualBox::i_clipboardAreaDestroy(SharedClipboardAreaData &AreaData)
3402{
3403 /** @todo Do we need a worker for this to not block here for too long?
3404 * This could take a while to clean up huge areas ... */
3405 int vrc = AreaData.Area.Close();
3406 LogFlowFunc(("uID=%RU32, rc=%Rrc\n", AreaData.uID, vrc));
3407 return vrc;
3408}
3409
3410/**
3411 * Registers and creates a new clipboard area on the host (for all VMs), returned the clipboard area ID for it.
3412 *
3413 * @returns HRESULT
3414 * @param aParms Creation parameters. Currently unused.
3415 * @param aID Where to return the clipboard area ID on success.
3416 */
3417HRESULT VirtualBox::i_onClipboardAreaRegister(const std::vector<com::Utf8Str> &aParms, ULONG *aID)
3418{
3419 RT_NOREF(aParms);
3420
3421 HRESULT rc = S_OK;
3422
3423 int vrc = RTCritSectEnter(&m->SharedClipboard.CritSect);
3424 if (RT_SUCCESS(vrc))
3425 {
3426 try
3427 {
3428 if (m->SharedClipboard.mapClipboardAreas.size() >= m->SharedClipboard.uMaxClipboardAreas)
3429 {
3430 for (unsigned uTries = 0; uTries < 32; uTries++) /* Don't try too hard. */
3431 {
3432 const ULONG uAreaID = m->SharedClipboard.GenerateAreaID();
3433
3434 /* Area ID already taken? */
3435 if (m->SharedClipboard.mapClipboardAreas.find(uAreaID) != m->SharedClipboard.mapClipboardAreas.end())
3436 continue;
3437
3438 SharedClipboardAreaData AreaData;
3439 AreaData.uID = uAreaID;
3440
3441 vrc = i_clipboardAreaCreate(AreaData, 0 /* fFlags */);
3442 if (RT_SUCCESS(vrc))
3443 {
3444 m->SharedClipboard.mapClipboardAreas[uAreaID] = AreaData;
3445 m->SharedClipboard.uMostRecentClipboardAreaID = uAreaID;
3446
3447 /** @todo Implement collision detection / wrap-around. */
3448
3449 if (aID)
3450 *aID = uAreaID;
3451
3452 LogThisFunc(("Registered new clipboard area %RU32: '%s'\n",
3453 uAreaID, AreaData.Area.GetDirAbs()));
3454 break;
3455 }
3456 }
3457
3458 if (RT_FAILURE(vrc))
3459 rc = setError(E_FAIL, /** @todo Find a better rc. */
3460 tr("Failed to create new clipboard area (%Rrc)"), vrc);
3461 }
3462 else
3463 {
3464 rc = setError(E_FAIL, /** @todo Find a better rc. */
3465 tr("Maximum number of concurrent clipboard areas reached (%RU32)"),
3466 m->SharedClipboard.uMaxClipboardAreas);
3467 }
3468 }
3469 catch (std::bad_alloc &ba)
3470 {
3471 vrc = VERR_NO_MEMORY;
3472 RT_NOREF(ba);
3473 }
3474
3475 RTCritSectLeave(&m->SharedClipboard.CritSect);
3476 }
3477 LogFlowThisFunc(("rc=%Rhrc\n", rc));
3478 return rc;
3479}
3480
3481/**
3482 * Unregisters (destroys) a formerly created clipboard area.
3483 *
3484 * @returns HRESULT
3485 * @param aID ID of clipboard area to destroy.
3486 */
3487HRESULT VirtualBox::i_onClipboardAreaUnregister(ULONG aID)
3488{
3489 HRESULT rc = S_OK;
3490
3491 int vrc = RTCritSectEnter(&m->SharedClipboard.CritSect);
3492 if (RT_SUCCESS(vrc))
3493 {
3494 SharedClipboardAreaMap::iterator itArea = m->SharedClipboard.mapClipboardAreas.find(aID);
3495 if (itArea != m->SharedClipboard.mapClipboardAreas.end())
3496 {
3497 if (itArea->second.Area.GetRefCount() == 0)
3498 {
3499 vrc = i_clipboardAreaDestroy(itArea->second);
3500 if (RT_SUCCESS(vrc))
3501 {
3502 m->SharedClipboard.mapClipboardAreas.erase(itArea);
3503 }
3504 }
3505 else
3506 rc = setError(E_ACCESSDENIED, /** @todo Find a better rc. */
3507 tr("Area with ID %RU32 still in used, cannot unregister"), aID);
3508 }
3509 else
3510 rc = setError(VBOX_E_OBJECT_NOT_FOUND, /** @todo Find a better rc. */
3511 tr("Could not find a registered clipboard area with ID %RU32"), aID);
3512
3513 int vrc2 = RTCritSectLeave(&m->SharedClipboard.CritSect);
3514 AssertRC(vrc2);
3515 }
3516 LogFlowThisFunc(("aID=%RU32, rc=%Rhrc\n", aID, rc));
3517 return rc;
3518}
3519
3520/**
3521 * Attaches to an existing clipboard area.
3522 *
3523 * @returns HRESULT
3524 * @param aID ID of clipboard area to attach.
3525 */
3526HRESULT VirtualBox::i_onClipboardAreaAttach(ULONG aID)
3527{
3528 HRESULT rc = S_OK;
3529
3530 int vrc = RTCritSectEnter(&m->SharedClipboard.CritSect);
3531 if (RT_SUCCESS(vrc))
3532 {
3533 SharedClipboardAreaMap::iterator itArea = m->SharedClipboard.mapClipboardAreas.find(aID);
3534 if (itArea != m->SharedClipboard.mapClipboardAreas.end())
3535 {
3536 uint32_t cRefs = itArea->second.Area.AddRef();
3537 LogFlowThisFunc(("aID=%RU32 -> cRefs=%RU32\n", aID, cRefs));
3538 vrc = VINF_SUCCESS;
3539 }
3540 else
3541 rc = setError(VBOX_E_OBJECT_NOT_FOUND, /** @todo Find a better rc. */
3542 tr("Could not find a registered clipboard area with ID %RU32"), aID);
3543
3544 int vrc2 = RTCritSectLeave(&m->SharedClipboard.CritSect);
3545 AssertRC(vrc2);
3546 }
3547 LogFlowThisFunc(("aID=%RU32, rc=%Rhrc\n", aID, rc));
3548 return rc;
3549}
3550
3551/**
3552 * Detaches from an existing clipboard area.
3553 *
3554 * @returns HRESULT
3555 * @param aID ID of clipboard area to detach from.
3556 */
3557HRESULT VirtualBox::i_onClipboardAreaDetach(ULONG aID)
3558{
3559 HRESULT rc = S_OK;
3560
3561 int vrc = RTCritSectEnter(&m->SharedClipboard.CritSect);
3562 if (RT_SUCCESS(vrc))
3563 {
3564 SharedClipboardAreaMap::iterator itArea = m->SharedClipboard.mapClipboardAreas.find(aID);
3565 if (itArea != m->SharedClipboard.mapClipboardAreas.end())
3566 {
3567 uint32_t cRefs = itArea->second.Area.Release();
3568 LogFlowThisFunc(("aID=%RU32 -> cRefs=%RU32\n", aID, cRefs));
3569 vrc = VINF_SUCCESS;
3570 }
3571 else
3572 rc = setError(VBOX_E_OBJECT_NOT_FOUND, /** @todo Find a better rc. */
3573 tr("Could not find a registered clipboard area with ID %RU32"), aID);
3574
3575 int rc2 = RTCritSectLeave(&m->SharedClipboard.CritSect);
3576 AssertRC(rc2);
3577 }
3578 LogFlowThisFunc(("aID=%RU32, rc=%Rhrc\n", aID, rc));
3579 return rc;
3580}
3581
3582/**
3583 * Returns the ID of the most recent (last created) clipboard area,
3584 * or NIL_SHAREDCLIPBOARDAREAID if no clipboard area has been created yet.
3585 *
3586 * @returns Most recent clipboard area ID.
3587 */
3588ULONG VirtualBox::i_onClipboardAreaGetMostRecent(void)
3589{
3590 ULONG aID = 0;
3591 int vrc2 = RTCritSectEnter(&m->SharedClipboard.CritSect);
3592 if (RT_SUCCESS(vrc2))
3593 {
3594 aID = m->SharedClipboard.uMostRecentClipboardAreaID;
3595
3596 vrc2 = RTCritSectLeave(&m->SharedClipboard.CritSect);
3597 AssertRC(vrc2);
3598 }
3599 LogFlowThisFunc(("aID=%RU32\n", aID));
3600 return aID;
3601}
3602
3603/**
3604 * Returns the current reference count of a clipboard area.
3605 *
3606 * @returns Reference count of given clipboard area ID.
3607 */
3608ULONG VirtualBox::i_onClipboardAreaGetRefCount(ULONG aID)
3609{
3610 ULONG cRefCount = 0;
3611 int rc2 = RTCritSectEnter(&m->SharedClipboard.CritSect);
3612 if (RT_SUCCESS(rc2))
3613 {
3614 SharedClipboardAreaMap::iterator itArea = m->SharedClipboard.mapClipboardAreas.find(aID);
3615 if (itArea != m->SharedClipboard.mapClipboardAreas.end())
3616 {
3617 cRefCount = itArea->second.Area.GetRefCount();
3618 }
3619
3620 rc2 = RTCritSectLeave(&m->SharedClipboard.CritSect);
3621 AssertRC(rc2);
3622 }
3623 LogFlowThisFunc(("aID=%RU32, cRefCount=%RU32\n", aID, cRefCount));
3624 return cRefCount;
3625}
3626#endif /* VBOX_WITH_SHARED_CLIPBOARD_URI_LIST */
3627
3628/**
3629 * @note Doesn't lock any object.
3630 */
3631void VirtualBox::i_onGuestPropertyChange(const Guid &aMachineId, IN_BSTR aName,
3632 IN_BSTR aValue, IN_BSTR aFlags)
3633{
3634 i_postEvent(new GuestPropertyEvent(this, aMachineId, aName, aValue, aFlags));
3635}
3636
3637/**
3638 * @note Doesn't lock any object.
3639 */
3640void VirtualBox::i_onNatRedirectChange(const Guid &aMachineId, ULONG ulSlot, bool fRemove, IN_BSTR aName,
3641 NATProtocol_T aProto, IN_BSTR aHostIp, uint16_t aHostPort,
3642 IN_BSTR aGuestIp, uint16_t aGuestPort)
3643{
3644 fireNATRedirectEvent(m->pEventSource, aMachineId.toUtf16().raw(), ulSlot, fRemove, aName, aProto, aHostIp,
3645 aHostPort, aGuestIp, aGuestPort);
3646}
3647
3648void VirtualBox::i_onNATNetworkChange(IN_BSTR aName)
3649{
3650 fireNATNetworkChangedEvent(m->pEventSource, aName);
3651}
3652
3653void VirtualBox::i_onNATNetworkStartStop(IN_BSTR aName, BOOL fStart)
3654{
3655 fireNATNetworkStartStopEvent(m->pEventSource, aName, fStart);
3656}
3657
3658void VirtualBox::i_onNATNetworkSetting(IN_BSTR aNetworkName, BOOL aEnabled,
3659 IN_BSTR aNetwork, IN_BSTR aGateway,
3660 BOOL aAdvertiseDefaultIpv6RouteEnabled,
3661 BOOL fNeedDhcpServer)
3662{
3663 fireNATNetworkSettingEvent(m->pEventSource, aNetworkName, aEnabled,
3664 aNetwork, aGateway,
3665 aAdvertiseDefaultIpv6RouteEnabled, fNeedDhcpServer);
3666}
3667
3668void VirtualBox::i_onNATNetworkPortForward(IN_BSTR aNetworkName, BOOL create, BOOL fIpv6,
3669 IN_BSTR aRuleName, NATProtocol_T proto,
3670 IN_BSTR aHostIp, LONG aHostPort,
3671 IN_BSTR aGuestIp, LONG aGuestPort)
3672{
3673 fireNATNetworkPortForwardEvent(m->pEventSource, aNetworkName, create,
3674 fIpv6, aRuleName, proto,
3675 aHostIp, aHostPort,
3676 aGuestIp, aGuestPort);
3677}
3678
3679
3680void VirtualBox::i_onHostNameResolutionConfigurationChange()
3681{
3682 if (m->pEventSource)
3683 fireHostNameResolutionConfigurationChangeEvent(m->pEventSource);
3684}
3685
3686
3687int VirtualBox::i_natNetworkRefInc(const Utf8Str &aNetworkName)
3688{
3689 AutoWriteLock safeLock(*spMtxNatNetworkNameToRefCountLock COMMA_LOCKVAL_SRC_POS);
3690
3691 if (!sNatNetworkNameToRefCount[aNetworkName])
3692 {
3693 ComPtr<INATNetwork> nat;
3694 HRESULT rc = findNATNetworkByName(aNetworkName, nat);
3695 if (FAILED(rc)) return -1;
3696
3697 rc = nat->Start(Bstr("whatever").raw());
3698 if (SUCCEEDED(rc))
3699 LogRel(("Started NAT network '%s'\n", aNetworkName.c_str()));
3700 else
3701 LogRel(("Error %Rhrc starting NAT network '%s'\n", rc, aNetworkName.c_str()));
3702 AssertComRCReturn(rc, -1);
3703 }
3704
3705 sNatNetworkNameToRefCount[aNetworkName]++;
3706
3707 return sNatNetworkNameToRefCount[aNetworkName];
3708}
3709
3710
3711int VirtualBox::i_natNetworkRefDec(const Utf8Str &aNetworkName)
3712{
3713 AutoWriteLock safeLock(*spMtxNatNetworkNameToRefCountLock COMMA_LOCKVAL_SRC_POS);
3714
3715 if (!sNatNetworkNameToRefCount[aNetworkName])
3716 return 0;
3717
3718 sNatNetworkNameToRefCount[aNetworkName]--;
3719
3720 if (!sNatNetworkNameToRefCount[aNetworkName])
3721 {
3722 ComPtr<INATNetwork> nat;
3723 HRESULT rc = findNATNetworkByName(aNetworkName, nat);
3724 if (FAILED(rc)) return -1;
3725
3726 rc = nat->Stop();
3727 if (SUCCEEDED(rc))
3728 LogRel(("Stopped NAT network '%s'\n", aNetworkName.c_str()));
3729 else
3730 LogRel(("Error %Rhrc stopping NAT network '%s'\n", rc, aNetworkName.c_str()));
3731 AssertComRCReturn(rc, -1);
3732 }
3733
3734 return sNatNetworkNameToRefCount[aNetworkName];
3735}
3736
3737
3738/**
3739 * @note Locks the list of other objects for reading.
3740 */
3741ComObjPtr<GuestOSType> VirtualBox::i_getUnknownOSType()
3742{
3743 ComObjPtr<GuestOSType> type;
3744
3745 /* unknown type must always be the first */
3746 ComAssertRet(m->allGuestOSTypes.size() > 0, type);
3747
3748 return m->allGuestOSTypes.front();
3749}
3750
3751/**
3752 * Returns the list of opened machines (machines having VM sessions opened,
3753 * ignoring other sessions) and optionally the list of direct session controls.
3754 *
3755 * @param aMachines Where to put opened machines (will be empty if none).
3756 * @param aControls Where to put direct session controls (optional).
3757 *
3758 * @note The returned lists contain smart pointers. So, clear it as soon as
3759 * it becomes no more necessary to release instances.
3760 *
3761 * @note It can be possible that a session machine from the list has been
3762 * already uninitialized, so do a usual AutoCaller/AutoReadLock sequence
3763 * when accessing unprotected data directly.
3764 *
3765 * @note Locks objects for reading.
3766 */
3767void VirtualBox::i_getOpenedMachines(SessionMachinesList &aMachines,
3768 InternalControlList *aControls /*= NULL*/)
3769{
3770 AutoCaller autoCaller(this);
3771 AssertComRCReturnVoid(autoCaller.rc());
3772
3773 aMachines.clear();
3774 if (aControls)
3775 aControls->clear();
3776
3777 AutoReadLock alock(m->allMachines.getLockHandle() COMMA_LOCKVAL_SRC_POS);
3778
3779 for (MachinesOList::iterator it = m->allMachines.begin();
3780 it != m->allMachines.end();
3781 ++it)
3782 {
3783 ComObjPtr<SessionMachine> sm;
3784 ComPtr<IInternalSessionControl> ctl;
3785 if ((*it)->i_isSessionOpenVM(sm, &ctl))
3786 {
3787 aMachines.push_back(sm);
3788 if (aControls)
3789 aControls->push_back(ctl);
3790 }
3791 }
3792}
3793
3794/**
3795 * Gets a reference to the machine list. This is the real thing, not a copy,
3796 * so bad things will happen if the caller doesn't hold the necessary lock.
3797 *
3798 * @returns reference to machine list
3799 *
3800 * @note Caller must hold the VirtualBox object lock at least for reading.
3801 */
3802VirtualBox::MachinesOList &VirtualBox::i_getMachinesList(void)
3803{
3804 return m->allMachines;
3805}
3806
3807/**
3808 * Searches for a machine object with the given ID in the collection
3809 * of registered machines.
3810 *
3811 * @param aId Machine UUID to look for.
3812 * @param fPermitInaccessible If true, inaccessible machines will be found;
3813 * if false, this will fail if the given machine is inaccessible.
3814 * @param aSetError If true, set errorinfo if the machine is not found.
3815 * @param aMachine Returned machine, if found.
3816 * @return
3817 */
3818HRESULT VirtualBox::i_findMachine(const Guid &aId,
3819 bool fPermitInaccessible,
3820 bool aSetError,
3821 ComObjPtr<Machine> *aMachine /* = NULL */)
3822{
3823 HRESULT rc = VBOX_E_OBJECT_NOT_FOUND;
3824
3825 AutoCaller autoCaller(this);
3826 AssertComRCReturnRC(autoCaller.rc());
3827
3828 {
3829 AutoReadLock al(m->allMachines.getLockHandle() COMMA_LOCKVAL_SRC_POS);
3830
3831 for (MachinesOList::iterator it = m->allMachines.begin();
3832 it != m->allMachines.end();
3833 ++it)
3834 {
3835 ComObjPtr<Machine> pMachine = *it;
3836
3837 if (!fPermitInaccessible)
3838 {
3839 // skip inaccessible machines
3840 AutoCaller machCaller(pMachine);
3841 if (FAILED(machCaller.rc()))
3842 continue;
3843 }
3844
3845 if (pMachine->i_getId() == aId)
3846 {
3847 rc = S_OK;
3848 if (aMachine)
3849 *aMachine = pMachine;
3850 break;
3851 }
3852 }
3853 }
3854
3855 if (aSetError && FAILED(rc))
3856 rc = setError(rc,
3857 tr("Could not find a registered machine with UUID {%RTuuid}"),
3858 aId.raw());
3859
3860 return rc;
3861}
3862
3863/**
3864 * Searches for a machine object with the given name or location in the
3865 * collection of registered machines.
3866 *
3867 * @param aName Machine name or location to look for.
3868 * @param aSetError If true, set errorinfo if the machine is not found.
3869 * @param aMachine Returned machine, if found.
3870 * @return
3871 */
3872HRESULT VirtualBox::i_findMachineByName(const Utf8Str &aName,
3873 bool aSetError,
3874 ComObjPtr<Machine> *aMachine /* = NULL */)
3875{
3876 HRESULT rc = VBOX_E_OBJECT_NOT_FOUND;
3877
3878 AutoReadLock al(m->allMachines.getLockHandle() COMMA_LOCKVAL_SRC_POS);
3879 for (MachinesOList::iterator it = m->allMachines.begin();
3880 it != m->allMachines.end();
3881 ++it)
3882 {
3883 ComObjPtr<Machine> &pMachine = *it;
3884 AutoCaller machCaller(pMachine);
3885 if (machCaller.rc())
3886 continue; // we can't ask inaccessible machines for their names
3887
3888 AutoReadLock machLock(pMachine COMMA_LOCKVAL_SRC_POS);
3889 if (pMachine->i_getName() == aName)
3890 {
3891 rc = S_OK;
3892 if (aMachine)
3893 *aMachine = pMachine;
3894 break;
3895 }
3896 if (!RTPathCompare(pMachine->i_getSettingsFileFull().c_str(), aName.c_str()))
3897 {
3898 rc = S_OK;
3899 if (aMachine)
3900 *aMachine = pMachine;
3901 break;
3902 }
3903 }
3904
3905 if (aSetError && FAILED(rc))
3906 rc = setError(rc,
3907 tr("Could not find a registered machine named '%s'"), aName.c_str());
3908
3909 return rc;
3910}
3911
3912static HRESULT i_validateMachineGroupHelper(const Utf8Str &aGroup, bool fPrimary, VirtualBox *pVirtualBox)
3913{
3914 /* empty strings are invalid */
3915 if (aGroup.isEmpty())
3916 return E_INVALIDARG;
3917 /* the toplevel group is valid */
3918 if (aGroup == "/")
3919 return S_OK;
3920 /* any other strings of length 1 are invalid */
3921 if (aGroup.length() == 1)
3922 return E_INVALIDARG;
3923 /* must start with a slash */
3924 if (aGroup.c_str()[0] != '/')
3925 return E_INVALIDARG;
3926 /* must not end with a slash */
3927 if (aGroup.c_str()[aGroup.length() - 1] == '/')
3928 return E_INVALIDARG;
3929 /* check the group components */
3930 const char *pStr = aGroup.c_str() + 1; /* first char is /, skip it */
3931 while (pStr)
3932 {
3933 char *pSlash = RTStrStr(pStr, "/");
3934 if (pSlash)
3935 {
3936 /* no empty components (or // sequences in other words) */
3937 if (pSlash == pStr)
3938 return E_INVALIDARG;
3939 /* check if the machine name rules are violated, because that means
3940 * the group components are too close to the limits. */
3941 Utf8Str tmp((const char *)pStr, (size_t)(pSlash - pStr));
3942 Utf8Str tmp2(tmp);
3943 sanitiseMachineFilename(tmp);
3944 if (tmp != tmp2)
3945 return E_INVALIDARG;
3946 if (fPrimary)
3947 {
3948 HRESULT rc = pVirtualBox->i_findMachineByName(tmp,
3949 false /* aSetError */);
3950 if (SUCCEEDED(rc))
3951 return VBOX_E_VM_ERROR;
3952 }
3953 pStr = pSlash + 1;
3954 }
3955 else
3956 {
3957 /* check if the machine name rules are violated, because that means
3958 * the group components is too close to the limits. */
3959 Utf8Str tmp(pStr);
3960 Utf8Str tmp2(tmp);
3961 sanitiseMachineFilename(tmp);
3962 if (tmp != tmp2)
3963 return E_INVALIDARG;
3964 pStr = NULL;
3965 }
3966 }
3967 return S_OK;
3968}
3969
3970/**
3971 * Validates a machine group.
3972 *
3973 * @param aGroup Machine group.
3974 * @param fPrimary Set if this is the primary group.
3975 *
3976 * @return S_OK or E_INVALIDARG
3977 */
3978HRESULT VirtualBox::i_validateMachineGroup(const Utf8Str &aGroup, bool fPrimary)
3979{
3980 HRESULT rc = i_validateMachineGroupHelper(aGroup, fPrimary, this);
3981 if (FAILED(rc))
3982 {
3983 if (rc == VBOX_E_VM_ERROR)
3984 rc = setError(E_INVALIDARG,
3985 tr("Machine group '%s' conflicts with a virtual machine name"),
3986 aGroup.c_str());
3987 else
3988 rc = setError(rc,
3989 tr("Invalid machine group '%s'"),
3990 aGroup.c_str());
3991 }
3992 return rc;
3993}
3994
3995/**
3996 * Takes a list of machine groups, and sanitizes/validates it.
3997 *
3998 * @param aMachineGroups Array with the machine groups.
3999 * @param pllMachineGroups Pointer to list of strings for the result.
4000 *
4001 * @return S_OK or E_INVALIDARG
4002 */
4003HRESULT VirtualBox::i_convertMachineGroups(const std::vector<com::Utf8Str> aMachineGroups, StringsList *pllMachineGroups)
4004{
4005 pllMachineGroups->clear();
4006 if (aMachineGroups.size())
4007 {
4008 for (size_t i = 0; i < aMachineGroups.size(); i++)
4009 {
4010 Utf8Str group(aMachineGroups[i]);
4011 if (group.length() == 0)
4012 group = "/";
4013
4014 HRESULT rc = i_validateMachineGroup(group, i == 0);
4015 if (FAILED(rc))
4016 return rc;
4017
4018 /* no duplicates please */
4019 if ( find(pllMachineGroups->begin(), pllMachineGroups->end(), group)
4020 == pllMachineGroups->end())
4021 pllMachineGroups->push_back(group);
4022 }
4023 if (pllMachineGroups->size() == 0)
4024 pllMachineGroups->push_back("/");
4025 }
4026 else
4027 pllMachineGroups->push_back("/");
4028
4029 return S_OK;
4030}
4031
4032/**
4033 * Searches for a Medium object with the given ID in the list of registered
4034 * hard disks.
4035 *
4036 * @param aId ID of the hard disk. Must not be empty.
4037 * @param aSetError If @c true , the appropriate error info is set in case
4038 * when the hard disk is not found.
4039 * @param aHardDisk Where to store the found hard disk object (can be NULL).
4040 *
4041 * @return S_OK, E_INVALIDARG or VBOX_E_OBJECT_NOT_FOUND when not found.
4042 *
4043 * @note Locks the media tree for reading.
4044 */
4045HRESULT VirtualBox::i_findHardDiskById(const Guid &aId,
4046 bool aSetError,
4047 ComObjPtr<Medium> *aHardDisk /*= NULL*/)
4048{
4049 AssertReturn(!aId.isZero(), E_INVALIDARG);
4050
4051 // we use the hard disks map, but it is protected by the
4052 // hard disk _list_ lock handle
4053 AutoReadLock alock(m->allHardDisks.getLockHandle() COMMA_LOCKVAL_SRC_POS);
4054
4055 HardDiskMap::const_iterator it = m->mapHardDisks.find(aId);
4056 if (it != m->mapHardDisks.end())
4057 {
4058 if (aHardDisk)
4059 *aHardDisk = (*it).second;
4060 return S_OK;
4061 }
4062
4063 if (aSetError)
4064 return setError(VBOX_E_OBJECT_NOT_FOUND,
4065 tr("Could not find an open hard disk with UUID {%RTuuid}"),
4066 aId.raw());
4067
4068 return VBOX_E_OBJECT_NOT_FOUND;
4069}
4070
4071/**
4072 * Searches for a Medium object with the given ID or location in the list of
4073 * registered hard disks. If both ID and location are specified, the first
4074 * object that matches either of them (not necessarily both) is returned.
4075 *
4076 * @param strLocation Full location specification. Must not be empty.
4077 * @param aSetError If @c true , the appropriate error info is set in case
4078 * when the hard disk is not found.
4079 * @param aHardDisk Where to store the found hard disk object (can be NULL).
4080 *
4081 * @return S_OK, E_INVALIDARG or VBOX_E_OBJECT_NOT_FOUND when not found.
4082 *
4083 * @note Locks the media tree for reading.
4084 */
4085HRESULT VirtualBox::i_findHardDiskByLocation(const Utf8Str &strLocation,
4086 bool aSetError,
4087 ComObjPtr<Medium> *aHardDisk /*= NULL*/)
4088{
4089 AssertReturn(!strLocation.isEmpty(), E_INVALIDARG);
4090
4091 // we use the hard disks map, but it is protected by the
4092 // hard disk _list_ lock handle
4093 AutoReadLock alock(m->allHardDisks.getLockHandle() COMMA_LOCKVAL_SRC_POS);
4094
4095 for (HardDiskMap::const_iterator it = m->mapHardDisks.begin();
4096 it != m->mapHardDisks.end();
4097 ++it)
4098 {
4099 const ComObjPtr<Medium> &pHD = (*it).second;
4100
4101 AutoCaller autoCaller(pHD);
4102 if (FAILED(autoCaller.rc())) return autoCaller.rc();
4103 AutoWriteLock mlock(pHD COMMA_LOCKVAL_SRC_POS);
4104
4105 Utf8Str strLocationFull = pHD->i_getLocationFull();
4106
4107 if (0 == RTPathCompare(strLocationFull.c_str(), strLocation.c_str()))
4108 {
4109 if (aHardDisk)
4110 *aHardDisk = pHD;
4111 return S_OK;
4112 }
4113 }
4114
4115 if (aSetError)
4116 return setError(VBOX_E_OBJECT_NOT_FOUND,
4117 tr("Could not find an open hard disk with location '%s'"),
4118 strLocation.c_str());
4119
4120 return VBOX_E_OBJECT_NOT_FOUND;
4121}
4122
4123/**
4124 * Searches for a Medium object with the given ID or location in the list of
4125 * registered DVD or floppy images, depending on the @a mediumType argument.
4126 * If both ID and file path are specified, the first object that matches either
4127 * of them (not necessarily both) is returned.
4128 *
4129 * @param mediumType Must be either DeviceType_DVD or DeviceType_Floppy.
4130 * @param aId ID of the image file (unused when NULL).
4131 * @param aLocation Full path to the image file (unused when NULL).
4132 * @param aSetError If @c true, the appropriate error info is set in case when
4133 * the image is not found.
4134 * @param aImage Where to store the found image object (can be NULL).
4135 *
4136 * @return S_OK when found or E_INVALIDARG or VBOX_E_OBJECT_NOT_FOUND when not found.
4137 *
4138 * @note Locks the media tree for reading.
4139 */
4140HRESULT VirtualBox::i_findDVDOrFloppyImage(DeviceType_T mediumType,
4141 const Guid *aId,
4142 const Utf8Str &aLocation,
4143 bool aSetError,
4144 ComObjPtr<Medium> *aImage /* = NULL */)
4145{
4146 AssertReturn(aId || !aLocation.isEmpty(), E_INVALIDARG);
4147
4148 Utf8Str location;
4149 if (!aLocation.isEmpty())
4150 {
4151 int vrc = i_calculateFullPath(aLocation, location);
4152 if (RT_FAILURE(vrc))
4153 return setError(VBOX_E_FILE_ERROR,
4154 tr("Invalid image file location '%s' (%Rrc)"),
4155 aLocation.c_str(),
4156 vrc);
4157 }
4158
4159 MediaOList *pMediaList;
4160
4161 switch (mediumType)
4162 {
4163 case DeviceType_DVD:
4164 pMediaList = &m->allDVDImages;
4165 break;
4166
4167 case DeviceType_Floppy:
4168 pMediaList = &m->allFloppyImages;
4169 break;
4170
4171 default:
4172 return E_INVALIDARG;
4173 }
4174
4175 AutoReadLock alock(pMediaList->getLockHandle() COMMA_LOCKVAL_SRC_POS);
4176
4177 bool found = false;
4178
4179 for (MediaList::const_iterator it = pMediaList->begin();
4180 it != pMediaList->end();
4181 ++it)
4182 {
4183 // no AutoCaller, registered image life time is bound to this
4184 Medium *pMedium = *it;
4185 AutoReadLock imageLock(pMedium COMMA_LOCKVAL_SRC_POS);
4186 const Utf8Str &strLocationFull = pMedium->i_getLocationFull();
4187
4188 found = ( aId
4189 && pMedium->i_getId() == *aId)
4190 || ( !aLocation.isEmpty()
4191 && RTPathCompare(location.c_str(),
4192 strLocationFull.c_str()) == 0);
4193 if (found)
4194 {
4195 if (pMedium->i_getDeviceType() != mediumType)
4196 {
4197 if (mediumType == DeviceType_DVD)
4198 return setError(E_INVALIDARG,
4199 "Cannot mount DVD medium '%s' as floppy", strLocationFull.c_str());
4200 else
4201 return setError(E_INVALIDARG,
4202 "Cannot mount floppy medium '%s' as DVD", strLocationFull.c_str());
4203 }
4204
4205 if (aImage)
4206 *aImage = pMedium;
4207 break;
4208 }
4209 }
4210
4211 HRESULT rc = found ? S_OK : VBOX_E_OBJECT_NOT_FOUND;
4212
4213 if (aSetError && !found)
4214 {
4215 if (aId)
4216 setError(rc,
4217 tr("Could not find an image file with UUID {%RTuuid} in the media registry ('%s')"),
4218 aId->raw(),
4219 m->strSettingsFilePath.c_str());
4220 else
4221 setError(rc,
4222 tr("Could not find an image file with location '%s' in the media registry ('%s')"),
4223 aLocation.c_str(),
4224 m->strSettingsFilePath.c_str());
4225 }
4226
4227 return rc;
4228}
4229
4230/**
4231 * Searches for an IMedium object that represents the given UUID.
4232 *
4233 * If the UUID is empty (indicating an empty drive), this sets pMedium
4234 * to NULL and returns S_OK.
4235 *
4236 * If the UUID refers to a host drive of the given device type, this
4237 * sets pMedium to the object from the list in IHost and returns S_OK.
4238 *
4239 * If the UUID is an image file, this sets pMedium to the object that
4240 * findDVDOrFloppyImage() returned.
4241 *
4242 * If none of the above apply, this returns VBOX_E_OBJECT_NOT_FOUND.
4243 *
4244 * @param mediumType Must be DeviceType_DVD or DeviceType_Floppy.
4245 * @param uuid UUID to search for; must refer to a host drive or an image file or be null.
4246 * @param fRefresh Whether to refresh the list of host drives in IHost (see Host::getDrives())
4247 * @param aSetError
4248 * @param pMedium out: IMedium object found.
4249 * @return
4250 */
4251HRESULT VirtualBox::i_findRemoveableMedium(DeviceType_T mediumType,
4252 const Guid &uuid,
4253 bool fRefresh,
4254 bool aSetError,
4255 ComObjPtr<Medium> &pMedium)
4256{
4257 if (uuid.isZero())
4258 {
4259 // that's easy
4260 pMedium.setNull();
4261 return S_OK;
4262 }
4263 else if (!uuid.isValid())
4264 {
4265 /* handling of case invalid GUID */
4266 return setError(VBOX_E_OBJECT_NOT_FOUND,
4267 tr("Guid '%s' is invalid"),
4268 uuid.toString().c_str());
4269 }
4270
4271 // first search for host drive with that UUID
4272 HRESULT rc = m->pHost->i_findHostDriveById(mediumType,
4273 uuid,
4274 fRefresh,
4275 pMedium);
4276 if (rc == VBOX_E_OBJECT_NOT_FOUND)
4277 // then search for an image with that UUID
4278 rc = i_findDVDOrFloppyImage(mediumType, &uuid, Utf8Str::Empty, aSetError, &pMedium);
4279
4280 return rc;
4281}
4282
4283/* Look for a GuestOSType object */
4284HRESULT VirtualBox::i_findGuestOSType(const Utf8Str &strOSType,
4285 ComObjPtr<GuestOSType> &guestOSType)
4286{
4287 guestOSType.setNull();
4288
4289 AssertMsg(m->allGuestOSTypes.size() != 0,
4290 ("Guest OS types array must be filled"));
4291
4292 AutoReadLock alock(m->allGuestOSTypes.getLockHandle() COMMA_LOCKVAL_SRC_POS);
4293 for (GuestOSTypesOList::const_iterator it = m->allGuestOSTypes.begin();
4294 it != m->allGuestOSTypes.end();
4295 ++it)
4296 {
4297 const Utf8Str &typeId = (*it)->i_id();
4298 AssertMsg(!typeId.isEmpty(), ("ID must not be NULL"));
4299 if (strOSType.compare(typeId, Utf8Str::CaseInsensitive) == 0)
4300 {
4301 guestOSType = *it;
4302 return S_OK;
4303 }
4304 }
4305
4306 return setError(VBOX_E_OBJECT_NOT_FOUND,
4307 tr("'%s' is not a valid Guest OS type"),
4308 strOSType.c_str());
4309}
4310
4311/**
4312 * Returns the constant pseudo-machine UUID that is used to identify the
4313 * global media registry.
4314 *
4315 * Starting with VirtualBox 4.0 each medium remembers in its instance data
4316 * in which media registry it is saved (if any): this can either be a machine
4317 * UUID, if it's in a per-machine media registry, or this global ID.
4318 *
4319 * This UUID is only used to identify the VirtualBox object while VirtualBox
4320 * is running. It is a compile-time constant and not saved anywhere.
4321 *
4322 * @return
4323 */
4324const Guid& VirtualBox::i_getGlobalRegistryId() const
4325{
4326 return m->uuidMediaRegistry;
4327}
4328
4329const ComObjPtr<Host>& VirtualBox::i_host() const
4330{
4331 return m->pHost;
4332}
4333
4334SystemProperties* VirtualBox::i_getSystemProperties() const
4335{
4336 return m->pSystemProperties;
4337}
4338
4339CloudProviderManager *VirtualBox::i_getCloudProviderManager() const
4340{
4341 return m->pCloudProviderManager;
4342}
4343
4344#ifdef VBOX_WITH_EXTPACK
4345/**
4346 * Getter that SystemProperties and others can use to talk to the extension
4347 * pack manager.
4348 */
4349ExtPackManager* VirtualBox::i_getExtPackManager() const
4350{
4351 return m->ptrExtPackManager;
4352}
4353#endif
4354
4355/**
4356 * Getter that machines can talk to the autostart database.
4357 */
4358AutostartDb* VirtualBox::i_getAutostartDb() const
4359{
4360 return m->pAutostartDb;
4361}
4362
4363#ifdef VBOX_WITH_RESOURCE_USAGE_API
4364const ComObjPtr<PerformanceCollector>& VirtualBox::i_performanceCollector() const
4365{
4366 return m->pPerformanceCollector;
4367}
4368#endif /* VBOX_WITH_RESOURCE_USAGE_API */
4369
4370/**
4371 * Returns the default machine folder from the system properties
4372 * with proper locking.
4373 * @return
4374 */
4375void VirtualBox::i_getDefaultMachineFolder(Utf8Str &str) const
4376{
4377 AutoReadLock propsLock(m->pSystemProperties COMMA_LOCKVAL_SRC_POS);
4378 str = m->pSystemProperties->m->strDefaultMachineFolder;
4379}
4380
4381/**
4382 * Returns the default hard disk format from the system properties
4383 * with proper locking.
4384 * @return
4385 */
4386void VirtualBox::i_getDefaultHardDiskFormat(Utf8Str &str) const
4387{
4388 AutoReadLock propsLock(m->pSystemProperties COMMA_LOCKVAL_SRC_POS);
4389 str = m->pSystemProperties->m->strDefaultHardDiskFormat;
4390}
4391
4392const Utf8Str& VirtualBox::i_homeDir() const
4393{
4394 return m->strHomeDir;
4395}
4396
4397/**
4398 * Calculates the absolute path of the given path taking the VirtualBox home
4399 * directory as the current directory.
4400 *
4401 * @param strPath Path to calculate the absolute path for.
4402 * @param aResult Where to put the result (used only on success, can be the
4403 * same Utf8Str instance as passed in @a aPath).
4404 * @return IPRT result.
4405 *
4406 * @note Doesn't lock any object.
4407 */
4408int VirtualBox::i_calculateFullPath(const Utf8Str &strPath, Utf8Str &aResult)
4409{
4410 AutoCaller autoCaller(this);
4411 AssertComRCReturn(autoCaller.rc(), VERR_GENERAL_FAILURE);
4412
4413 /* no need to lock since strHomeDir is const */
4414
4415 char szFolder[RTPATH_MAX];
4416 size_t cbFolder = sizeof(szFolder);
4417 int vrc = RTPathAbsEx(m->strHomeDir.c_str(),
4418 strPath.c_str(),
4419 RTPATH_STR_F_STYLE_HOST,
4420 szFolder,
4421 &cbFolder);
4422 if (RT_SUCCESS(vrc))
4423 aResult = szFolder;
4424
4425 return vrc;
4426}
4427
4428/**
4429 * Copies strSource to strTarget, making it relative to the VirtualBox config folder
4430 * if it is a subdirectory thereof, or simply copying it otherwise.
4431 *
4432 * @param strSource Path to evalue and copy.
4433 * @param strTarget Buffer to receive target path.
4434 */
4435void VirtualBox::i_copyPathRelativeToConfig(const Utf8Str &strSource,
4436 Utf8Str &strTarget)
4437{
4438 AutoCaller autoCaller(this);
4439 AssertComRCReturnVoid(autoCaller.rc());
4440
4441 // no need to lock since mHomeDir is const
4442
4443 // use strTarget as a temporary buffer to hold the machine settings dir
4444 strTarget = m->strHomeDir;
4445 if (RTPathStartsWith(strSource.c_str(), strTarget.c_str()))
4446 // is relative: then append what's left
4447 strTarget.append(strSource.c_str() + strTarget.length()); // include '/'
4448 else
4449 // is not relative: then overwrite
4450 strTarget = strSource;
4451}
4452
4453// private methods
4454/////////////////////////////////////////////////////////////////////////////
4455
4456/**
4457 * Checks if there is a hard disk, DVD or floppy image with the given ID or
4458 * location already registered.
4459 *
4460 * On return, sets @a aConflict to the string describing the conflicting medium,
4461 * or sets it to @c Null if no conflicting media is found. Returns S_OK in
4462 * either case. A failure is unexpected.
4463 *
4464 * @param aId UUID to check.
4465 * @param aLocation Location to check.
4466 * @param aConflict Where to return parameters of the conflicting medium.
4467 * @param ppMedium Medium reference in case this is simply a duplicate.
4468 *
4469 * @note Locks the media tree and media objects for reading.
4470 */
4471HRESULT VirtualBox::i_checkMediaForConflicts(const Guid &aId,
4472 const Utf8Str &aLocation,
4473 Utf8Str &aConflict,
4474 ComObjPtr<Medium> *ppMedium)
4475{
4476 AssertReturn(!aId.isZero() && !aLocation.isEmpty(), E_FAIL);
4477 AssertReturn(ppMedium, E_INVALIDARG);
4478
4479 aConflict.setNull();
4480 ppMedium->setNull();
4481
4482 AutoReadLock alock(i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
4483
4484 HRESULT rc = S_OK;
4485
4486 ComObjPtr<Medium> pMediumFound;
4487 const char *pcszType = NULL;
4488
4489 if (aId.isValid() && !aId.isZero())
4490 rc = i_findHardDiskById(aId, false /* aSetError */, &pMediumFound);
4491 if (FAILED(rc) && !aLocation.isEmpty())
4492 rc = i_findHardDiskByLocation(aLocation, false /* aSetError */, &pMediumFound);
4493 if (SUCCEEDED(rc))
4494 pcszType = tr("hard disk");
4495
4496 if (!pcszType)
4497 {
4498 rc = i_findDVDOrFloppyImage(DeviceType_DVD, &aId, aLocation, false /* aSetError */, &pMediumFound);
4499 if (SUCCEEDED(rc))
4500 pcszType = tr("CD/DVD image");
4501 }
4502
4503 if (!pcszType)
4504 {
4505 rc = i_findDVDOrFloppyImage(DeviceType_Floppy, &aId, aLocation, false /* aSetError */, &pMediumFound);
4506 if (SUCCEEDED(rc))
4507 pcszType = tr("floppy image");
4508 }
4509
4510 if (pcszType && pMediumFound)
4511 {
4512 /* Note: no AutoCaller since bound to this */
4513 AutoReadLock mlock(pMediumFound COMMA_LOCKVAL_SRC_POS);
4514
4515 Utf8Str strLocFound = pMediumFound->i_getLocationFull();
4516 Guid idFound = pMediumFound->i_getId();
4517
4518 if ( (RTPathCompare(strLocFound.c_str(), aLocation.c_str()) == 0)
4519 && (idFound == aId)
4520 )
4521 *ppMedium = pMediumFound;
4522
4523 aConflict = Utf8StrFmt(tr("%s '%s' with UUID {%RTuuid}"),
4524 pcszType,
4525 strLocFound.c_str(),
4526 idFound.raw());
4527 }
4528
4529 return S_OK;
4530}
4531
4532/**
4533 * Checks whether the given UUID is already in use by one medium for the
4534 * given device type.
4535 *
4536 * @returns true if the UUID is already in use
4537 * fale otherwise
4538 * @param aId The UUID to check.
4539 * @param deviceType The device type the UUID is going to be checked for
4540 * conflicts.
4541 */
4542bool VirtualBox::i_isMediaUuidInUse(const Guid &aId, DeviceType_T deviceType)
4543{
4544 /* A zero UUID is invalid here, always claim that it is already used. */
4545 AssertReturn(!aId.isZero(), true);
4546
4547 AutoReadLock alock(i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
4548
4549 HRESULT rc = S_OK;
4550 bool fInUse = false;
4551
4552 ComObjPtr<Medium> pMediumFound;
4553
4554 switch (deviceType)
4555 {
4556 case DeviceType_HardDisk:
4557 rc = i_findHardDiskById(aId, false /* aSetError */, &pMediumFound);
4558 break;
4559 case DeviceType_DVD:
4560 rc = i_findDVDOrFloppyImage(DeviceType_DVD, &aId, Utf8Str::Empty, false /* aSetError */, &pMediumFound);
4561 break;
4562 case DeviceType_Floppy:
4563 rc = i_findDVDOrFloppyImage(DeviceType_Floppy, &aId, Utf8Str::Empty, false /* aSetError */, &pMediumFound);
4564 break;
4565 default:
4566 AssertMsgFailed(("Invalid device type %d\n", deviceType));
4567 }
4568
4569 if (SUCCEEDED(rc) && pMediumFound)
4570 fInUse = true;
4571
4572 return fInUse;
4573}
4574
4575/**
4576 * Called from Machine::prepareSaveSettings() when it has detected
4577 * that a machine has been renamed. Such renames will require
4578 * updating the global media registry during the
4579 * VirtualBox::saveSettings() that follows later.
4580*
4581 * When a machine is renamed, there may well be media (in particular,
4582 * diff images for snapshots) in the global registry that will need
4583 * to have their paths updated. Before 3.2, Machine::saveSettings
4584 * used to call VirtualBox::saveSettings implicitly, which was both
4585 * unintuitive and caused locking order problems. Now, we remember
4586 * such pending name changes with this method so that
4587 * VirtualBox::saveSettings() can process them properly.
4588 */
4589void VirtualBox::i_rememberMachineNameChangeForMedia(const Utf8Str &strOldConfigDir,
4590 const Utf8Str &strNewConfigDir)
4591{
4592 AutoWriteLock mediaLock(i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
4593
4594 Data::PendingMachineRename pmr;
4595 pmr.strConfigDirOld = strOldConfigDir;
4596 pmr.strConfigDirNew = strNewConfigDir;
4597 m->llPendingMachineRenames.push_back(pmr);
4598}
4599
4600static DECLCALLBACK(int) fntSaveMediaRegistries(void *pvUser);
4601
4602class SaveMediaRegistriesDesc : public ThreadTask
4603{
4604
4605public:
4606 SaveMediaRegistriesDesc()
4607 {
4608 m_strTaskName = "SaveMediaReg";
4609 }
4610 virtual ~SaveMediaRegistriesDesc(void) { }
4611
4612private:
4613 void handler()
4614 {
4615 try
4616 {
4617 fntSaveMediaRegistries(this);
4618 }
4619 catch(...)
4620 {
4621 LogRel(("Exception in the function fntSaveMediaRegistries()\n"));
4622 }
4623 }
4624
4625 MediaList llMedia;
4626 ComObjPtr<VirtualBox> pVirtualBox;
4627
4628 friend DECLCALLBACK(int) fntSaveMediaRegistries(void *pvUser);
4629 friend void VirtualBox::i_saveMediaRegistry(settings::MediaRegistry &mediaRegistry,
4630 const Guid &uuidRegistry,
4631 const Utf8Str &strMachineFolder);
4632};
4633
4634DECLCALLBACK(int) fntSaveMediaRegistries(void *pvUser)
4635{
4636 SaveMediaRegistriesDesc *pDesc = (SaveMediaRegistriesDesc *)pvUser;
4637 if (!pDesc)
4638 {
4639 LogRelFunc(("Thread for saving media registries lacks parameters\n"));
4640 return VERR_INVALID_PARAMETER;
4641 }
4642
4643 for (MediaList::const_iterator it = pDesc->llMedia.begin();
4644 it != pDesc->llMedia.end();
4645 ++it)
4646 {
4647 Medium *pMedium = *it;
4648 pMedium->i_markRegistriesModified();
4649 }
4650
4651 pDesc->pVirtualBox->i_saveModifiedRegistries();
4652
4653 pDesc->llMedia.clear();
4654 pDesc->pVirtualBox.setNull();
4655
4656 return VINF_SUCCESS;
4657}
4658
4659/**
4660 * Goes through all known media (hard disks, floppies and DVDs) and saves
4661 * those into the given settings::MediaRegistry structures whose registry
4662 * ID match the given UUID.
4663 *
4664 * Before actually writing to the structures, all media paths (not just the
4665 * ones for the given registry) are updated if machines have been renamed
4666 * since the last call.
4667 *
4668 * This gets called from two contexts:
4669 *
4670 * -- VirtualBox::saveSettings() with the UUID of the global registry
4671 * (VirtualBox::Data.uuidRegistry); this will save those media
4672 * which had been loaded from the global registry or have been
4673 * attached to a "legacy" machine which can't save its own registry;
4674 *
4675 * -- Machine::saveSettings() with the UUID of a machine, if a medium
4676 * has been attached to a machine created with VirtualBox 4.0 or later.
4677 *
4678 * Media which have only been temporarily opened without having been
4679 * attached to a machine have a NULL registry UUID and therefore don't
4680 * get saved.
4681 *
4682 * This locks the media tree. Throws HRESULT on errors!
4683 *
4684 * @param mediaRegistry Settings structure to fill.
4685 * @param uuidRegistry The UUID of the media registry; either a machine UUID
4686 * (if machine registry) or the UUID of the global registry.
4687 * @param strMachineFolder The machine folder for relative paths, if machine registry, or an empty string otherwise.
4688 */
4689void VirtualBox::i_saveMediaRegistry(settings::MediaRegistry &mediaRegistry,
4690 const Guid &uuidRegistry,
4691 const Utf8Str &strMachineFolder)
4692{
4693 // lock all media for the following; use a write lock because we're
4694 // modifying the PendingMachineRenamesList, which is protected by this
4695 AutoWriteLock mediaLock(i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
4696
4697 // if a machine was renamed, then we'll need to refresh media paths
4698 if (m->llPendingMachineRenames.size())
4699 {
4700 // make a single list from the three media lists so we don't need three loops
4701 MediaList llAllMedia;
4702 // with hard disks, we must use the map, not the list, because the list only has base images
4703 for (HardDiskMap::iterator it = m->mapHardDisks.begin(); it != m->mapHardDisks.end(); ++it)
4704 llAllMedia.push_back(it->second);
4705 for (MediaList::iterator it = m->allDVDImages.begin(); it != m->allDVDImages.end(); ++it)
4706 llAllMedia.push_back(*it);
4707 for (MediaList::iterator it = m->allFloppyImages.begin(); it != m->allFloppyImages.end(); ++it)
4708 llAllMedia.push_back(*it);
4709
4710 SaveMediaRegistriesDesc *pDesc = new SaveMediaRegistriesDesc();
4711 for (MediaList::iterator it = llAllMedia.begin();
4712 it != llAllMedia.end();
4713 ++it)
4714 {
4715 Medium *pMedium = *it;
4716 for (Data::PendingMachineRenamesList::iterator it2 = m->llPendingMachineRenames.begin();
4717 it2 != m->llPendingMachineRenames.end();
4718 ++it2)
4719 {
4720 const Data::PendingMachineRename &pmr = *it2;
4721 HRESULT rc = pMedium->i_updatePath(pmr.strConfigDirOld,
4722 pmr.strConfigDirNew);
4723 if (SUCCEEDED(rc))
4724 {
4725 // Remember which medium objects has been changed,
4726 // to trigger saving their registries later.
4727 pDesc->llMedia.push_back(pMedium);
4728 } else if (rc == VBOX_E_FILE_ERROR)
4729 /* nothing */;
4730 else
4731 AssertComRC(rc);
4732 }
4733 }
4734 // done, don't do it again until we have more machine renames
4735 m->llPendingMachineRenames.clear();
4736
4737 if (pDesc->llMedia.size())
4738 {
4739 // Handle the media registry saving in a separate thread, to
4740 // avoid giant locking problems and passing up the list many
4741 // levels up to whoever triggered saveSettings, as there are
4742 // lots of places which would need to handle saving more settings.
4743 pDesc->pVirtualBox = this;
4744
4745 //the function createThread() takes ownership of pDesc
4746 //so there is no need to use delete operator for pDesc
4747 //after calling this function
4748 HRESULT hr = pDesc->createThread();
4749 pDesc = NULL;
4750
4751 if (FAILED(hr))
4752 {
4753 // failure means that settings aren't saved, but there isn't
4754 // much we can do besides avoiding memory leaks
4755 LogRelFunc(("Failed to create thread for saving media registries (%Rhr)\n", hr));
4756 }
4757 }
4758 else
4759 delete pDesc;
4760 }
4761
4762 struct {
4763 MediaOList &llSource;
4764 settings::MediaList &llTarget;
4765 } s[] =
4766 {
4767 // hard disks
4768 { m->allHardDisks, mediaRegistry.llHardDisks },
4769 // CD/DVD images
4770 { m->allDVDImages, mediaRegistry.llDvdImages },
4771 // floppy images
4772 { m->allFloppyImages, mediaRegistry.llFloppyImages }
4773 };
4774
4775 HRESULT rc;
4776
4777 for (size_t i = 0; i < RT_ELEMENTS(s); ++i)
4778 {
4779 MediaOList &llSource = s[i].llSource;
4780 settings::MediaList &llTarget = s[i].llTarget;
4781 llTarget.clear();
4782 for (MediaList::const_iterator it = llSource.begin();
4783 it != llSource.end();
4784 ++it)
4785 {
4786 Medium *pMedium = *it;
4787 AutoCaller autoCaller(pMedium);
4788 if (FAILED(autoCaller.rc())) throw autoCaller.rc();
4789 AutoReadLock mlock(pMedium COMMA_LOCKVAL_SRC_POS);
4790
4791 if (pMedium->i_isInRegistry(uuidRegistry))
4792 {
4793 llTarget.push_back(settings::Medium::Empty);
4794 rc = pMedium->i_saveSettings(llTarget.back(), strMachineFolder); // this recurses into child hard disks
4795 if (FAILED(rc))
4796 {
4797 llTarget.pop_back();
4798 throw rc;
4799 }
4800 }
4801 }
4802 }
4803}
4804
4805/**
4806 * Helper function which actually writes out VirtualBox.xml, the main configuration file.
4807 * Gets called from the public VirtualBox::SaveSettings() as well as from various other
4808 * places internally when settings need saving.
4809 *
4810 * @note Caller must have locked the VirtualBox object for writing and must not hold any
4811 * other locks since this locks all kinds of member objects and trees temporarily,
4812 * which could cause conflicts.
4813 */
4814HRESULT VirtualBox::i_saveSettings()
4815{
4816 AutoCaller autoCaller(this);
4817 AssertComRCReturnRC(autoCaller.rc());
4818
4819 AssertReturn(isWriteLockOnCurrentThread(), E_FAIL);
4820 AssertReturn(!m->strSettingsFilePath.isEmpty(), E_FAIL);
4821
4822 i_unmarkRegistryModified(i_getGlobalRegistryId());
4823
4824 HRESULT rc = S_OK;
4825
4826 try
4827 {
4828 // machines
4829 m->pMainConfigFile->llMachines.clear();
4830 {
4831 AutoReadLock machinesLock(m->allMachines.getLockHandle() COMMA_LOCKVAL_SRC_POS);
4832 for (MachinesOList::iterator it = m->allMachines.begin();
4833 it != m->allMachines.end();
4834 ++it)
4835 {
4836 Machine *pMachine = *it;
4837 // save actual machine registry entry
4838 settings::MachineRegistryEntry mre;
4839 rc = pMachine->i_saveRegistryEntry(mre);
4840 m->pMainConfigFile->llMachines.push_back(mre);
4841 }
4842 }
4843
4844 i_saveMediaRegistry(m->pMainConfigFile->mediaRegistry,
4845 m->uuidMediaRegistry, // global media registry ID
4846 Utf8Str::Empty); // strMachineFolder
4847
4848 m->pMainConfigFile->llDhcpServers.clear();
4849 {
4850 AutoReadLock dhcpLock(m->allDHCPServers.getLockHandle() COMMA_LOCKVAL_SRC_POS);
4851 for (DHCPServersOList::const_iterator it = m->allDHCPServers.begin();
4852 it != m->allDHCPServers.end();
4853 ++it)
4854 {
4855 settings::DHCPServer d;
4856 rc = (*it)->i_saveSettings(d);
4857 if (FAILED(rc)) throw rc;
4858 m->pMainConfigFile->llDhcpServers.push_back(d);
4859 }
4860 }
4861
4862#ifdef VBOX_WITH_NAT_SERVICE
4863 /* Saving NAT Network configuration */
4864 m->pMainConfigFile->llNATNetworks.clear();
4865 {
4866 AutoReadLock natNetworkLock(m->allNATNetworks.getLockHandle() COMMA_LOCKVAL_SRC_POS);
4867 for (NATNetworksOList::const_iterator it = m->allNATNetworks.begin();
4868 it != m->allNATNetworks.end();
4869 ++it)
4870 {
4871 settings::NATNetwork n;
4872 rc = (*it)->i_saveSettings(n);
4873 if (FAILED(rc)) throw rc;
4874 m->pMainConfigFile->llNATNetworks.push_back(n);
4875 }
4876 }
4877#endif
4878
4879 // leave extra data alone, it's still in the config file
4880
4881 // host data (USB filters)
4882 rc = m->pHost->i_saveSettings(m->pMainConfigFile->host);
4883 if (FAILED(rc)) throw rc;
4884
4885 rc = m->pSystemProperties->i_saveSettings(m->pMainConfigFile->systemProperties);
4886 if (FAILED(rc)) throw rc;
4887
4888 // and write out the XML, still under the lock
4889 m->pMainConfigFile->write(m->strSettingsFilePath);
4890 }
4891 catch (HRESULT err)
4892 {
4893 /* we assume that error info is set by the thrower */
4894 rc = err;
4895 }
4896 catch (...)
4897 {
4898 rc = VirtualBoxBase::handleUnexpectedExceptions(this, RT_SRC_POS);
4899 }
4900
4901 return rc;
4902}
4903
4904/**
4905 * Helper to register the machine.
4906 *
4907 * When called during VirtualBox startup, adds the given machine to the
4908 * collection of registered machines. Otherwise tries to mark the machine
4909 * as registered, and, if succeeded, adds it to the collection and
4910 * saves global settings.
4911 *
4912 * @note The caller must have added itself as a caller of the @a aMachine
4913 * object if calls this method not on VirtualBox startup.
4914 *
4915 * @param aMachine machine to register
4916 *
4917 * @note Locks objects!
4918 */
4919HRESULT VirtualBox::i_registerMachine(Machine *aMachine)
4920{
4921 ComAssertRet(aMachine, E_INVALIDARG);
4922
4923 AutoCaller autoCaller(this);
4924 if (FAILED(autoCaller.rc())) return autoCaller.rc();
4925
4926 HRESULT rc = S_OK;
4927
4928 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
4929
4930 {
4931 ComObjPtr<Machine> pMachine;
4932 rc = i_findMachine(aMachine->i_getId(),
4933 true /* fPermitInaccessible */,
4934 false /* aDoSetError */,
4935 &pMachine);
4936 if (SUCCEEDED(rc))
4937 {
4938 /* sanity */
4939 AutoLimitedCaller machCaller(pMachine);
4940 AssertComRC(machCaller.rc());
4941
4942 return setError(E_INVALIDARG,
4943 tr("Registered machine with UUID {%RTuuid} ('%s') already exists"),
4944 aMachine->i_getId().raw(),
4945 pMachine->i_getSettingsFileFull().c_str());
4946 }
4947
4948 ComAssertRet(rc == VBOX_E_OBJECT_NOT_FOUND, rc);
4949 rc = S_OK;
4950 }
4951
4952 if (getObjectState().getState() != ObjectState::InInit)
4953 {
4954 rc = aMachine->i_prepareRegister();
4955 if (FAILED(rc)) return rc;
4956 }
4957
4958 /* add to the collection of registered machines */
4959 m->allMachines.addChild(aMachine);
4960
4961 if (getObjectState().getState() != ObjectState::InInit)
4962 rc = i_saveSettings();
4963
4964 return rc;
4965}
4966
4967/**
4968 * Remembers the given medium object by storing it in either the global
4969 * medium registry or a machine one.
4970 *
4971 * @note Caller must hold the media tree lock for writing; in addition, this
4972 * locks @a pMedium for reading
4973 *
4974 * @param pMedium Medium object to remember.
4975 * @param ppMedium Actually stored medium object. Can be different if due
4976 * to an unavoidable race there was a duplicate Medium object
4977 * created.
4978 * @param mediaTreeLock Reference to the AutoWriteLock holding the media tree
4979 * lock, necessary to release it in the right spot.
4980 * @return
4981 */
4982HRESULT VirtualBox::i_registerMedium(const ComObjPtr<Medium> &pMedium,
4983 ComObjPtr<Medium> *ppMedium,
4984 AutoWriteLock &mediaTreeLock)
4985{
4986 AssertReturn(pMedium != NULL, E_INVALIDARG);
4987 AssertReturn(ppMedium != NULL, E_INVALIDARG);
4988
4989 // caller must hold the media tree write lock
4990 Assert(i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
4991
4992 AutoCaller autoCaller(this);
4993 AssertComRCReturnRC(autoCaller.rc());
4994
4995 AutoCaller mediumCaller(pMedium);
4996 AssertComRCReturnRC(mediumCaller.rc());
4997
4998 bool fAddToGlobalRegistry = false;
4999 const char *pszDevType = NULL;
5000 Guid regId;
5001 ObjectsList<Medium> *pall = NULL;
5002 DeviceType_T devType;
5003 {
5004 AutoReadLock mediumLock(pMedium COMMA_LOCKVAL_SRC_POS);
5005 devType = pMedium->i_getDeviceType();
5006
5007 if (!pMedium->i_getFirstRegistryMachineId(regId))
5008 fAddToGlobalRegistry = true;
5009 }
5010 switch (devType)
5011 {
5012 case DeviceType_HardDisk:
5013 pall = &m->allHardDisks;
5014 pszDevType = tr("hard disk");
5015 break;
5016 case DeviceType_DVD:
5017 pszDevType = tr("DVD image");
5018 pall = &m->allDVDImages;
5019 break;
5020 case DeviceType_Floppy:
5021 pszDevType = tr("floppy image");
5022 pall = &m->allFloppyImages;
5023 break;
5024 default:
5025 AssertMsgFailedReturn(("invalid device type %d", devType), E_INVALIDARG);
5026 }
5027
5028 Guid id;
5029 Utf8Str strLocationFull;
5030 ComObjPtr<Medium> pParent;
5031 {
5032 AutoReadLock mediumLock(pMedium COMMA_LOCKVAL_SRC_POS);
5033 id = pMedium->i_getId();
5034 strLocationFull = pMedium->i_getLocationFull();
5035 pParent = pMedium->i_getParent();
5036 }
5037
5038 HRESULT rc;
5039
5040 Utf8Str strConflict;
5041 ComObjPtr<Medium> pDupMedium;
5042 rc = i_checkMediaForConflicts(id,
5043 strLocationFull,
5044 strConflict,
5045 &pDupMedium);
5046 if (FAILED(rc)) return rc;
5047
5048 if (pDupMedium.isNull())
5049 {
5050 if (strConflict.length())
5051 return setError(E_INVALIDARG,
5052 tr("Cannot register the %s '%s' {%RTuuid} because a %s already exists"),
5053 pszDevType,
5054 strLocationFull.c_str(),
5055 id.raw(),
5056 strConflict.c_str(),
5057 m->strSettingsFilePath.c_str());
5058
5059 // add to the collection if it is a base medium
5060 if (pParent.isNull())
5061 pall->getList().push_back(pMedium);
5062
5063 // store all hard disks (even differencing images) in the map
5064 if (devType == DeviceType_HardDisk)
5065 m->mapHardDisks[id] = pMedium;
5066
5067 mediumCaller.release();
5068 mediaTreeLock.release();
5069 *ppMedium = pMedium;
5070 }
5071 else
5072 {
5073 // pMedium may be the last reference to the Medium object, and the
5074 // caller may have specified the same ComObjPtr as the output parameter.
5075 // In this case the assignment will uninit the object, and we must not
5076 // have a caller pending.
5077 mediumCaller.release();
5078 // release media tree lock, must not be held at uninit time.
5079 mediaTreeLock.release();
5080 // must not hold the media tree write lock any more
5081 Assert(!i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
5082 *ppMedium = pDupMedium;
5083 }
5084
5085 if (fAddToGlobalRegistry)
5086 {
5087 AutoWriteLock mediumLock(pMedium COMMA_LOCKVAL_SRC_POS);
5088 if (pMedium->i_addRegistry(m->uuidMediaRegistry))
5089 i_markRegistryModified(m->uuidMediaRegistry);
5090 }
5091
5092 // Restore the initial lock state, so that no unexpected lock changes are
5093 // done by this method, which would need adjustments everywhere.
5094 mediaTreeLock.acquire();
5095
5096 return rc;
5097}
5098
5099/**
5100 * Removes the given medium from the respective registry.
5101 *
5102 * @param pMedium Hard disk object to remove.
5103 *
5104 * @note Caller must hold the media tree lock for writing; in addition, this locks @a pMedium for reading
5105 */
5106HRESULT VirtualBox::i_unregisterMedium(Medium *pMedium)
5107{
5108 AssertReturn(pMedium != NULL, E_INVALIDARG);
5109
5110 AutoCaller autoCaller(this);
5111 AssertComRCReturnRC(autoCaller.rc());
5112
5113 AutoCaller mediumCaller(pMedium);
5114 AssertComRCReturnRC(mediumCaller.rc());
5115
5116 // caller must hold the media tree write lock
5117 Assert(i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
5118
5119 Guid id;
5120 ComObjPtr<Medium> pParent;
5121 DeviceType_T devType;
5122 {
5123 AutoReadLock mediumLock(pMedium COMMA_LOCKVAL_SRC_POS);
5124 id = pMedium->i_getId();
5125 pParent = pMedium->i_getParent();
5126 devType = pMedium->i_getDeviceType();
5127 }
5128
5129 ObjectsList<Medium> *pall = NULL;
5130 switch (devType)
5131 {
5132 case DeviceType_HardDisk:
5133 pall = &m->allHardDisks;
5134 break;
5135 case DeviceType_DVD:
5136 pall = &m->allDVDImages;
5137 break;
5138 case DeviceType_Floppy:
5139 pall = &m->allFloppyImages;
5140 break;
5141 default:
5142 AssertMsgFailedReturn(("invalid device type %d", devType), E_INVALIDARG);
5143 }
5144
5145 // remove from the collection if it is a base medium
5146 if (pParent.isNull())
5147 pall->getList().remove(pMedium);
5148
5149 // remove all hard disks (even differencing images) from map
5150 if (devType == DeviceType_HardDisk)
5151 {
5152 size_t cnt = m->mapHardDisks.erase(id);
5153 Assert(cnt == 1);
5154 NOREF(cnt);
5155 }
5156
5157 return S_OK;
5158}
5159
5160/**
5161 * Little helper called from unregisterMachineMedia() to recursively add media to the given list,
5162 * with children appearing before their parents.
5163 * @param llMedia
5164 * @param pMedium
5165 */
5166void VirtualBox::i_pushMediumToListWithChildren(MediaList &llMedia, Medium *pMedium)
5167{
5168 // recurse first, then add ourselves; this way children end up on the
5169 // list before their parents
5170
5171 const MediaList &llChildren = pMedium->i_getChildren();
5172 for (MediaList::const_iterator it = llChildren.begin();
5173 it != llChildren.end();
5174 ++it)
5175 {
5176 Medium *pChild = *it;
5177 i_pushMediumToListWithChildren(llMedia, pChild);
5178 }
5179
5180 Log(("Pushing medium %RTuuid\n", pMedium->i_getId().raw()));
5181 llMedia.push_back(pMedium);
5182}
5183
5184/**
5185 * Unregisters all Medium objects which belong to the given machine registry.
5186 * Gets called from Machine::uninit() just before the machine object dies
5187 * and must only be called with a machine UUID as the registry ID.
5188 *
5189 * Locks the media tree.
5190 *
5191 * @param uuidMachine Medium registry ID (always a machine UUID)
5192 * @return
5193 */
5194HRESULT VirtualBox::i_unregisterMachineMedia(const Guid &uuidMachine)
5195{
5196 Assert(!uuidMachine.isZero() && uuidMachine.isValid());
5197
5198 LogFlowFuncEnter();
5199
5200 AutoCaller autoCaller(this);
5201 AssertComRCReturnRC(autoCaller.rc());
5202
5203 MediaList llMedia2Close;
5204
5205 {
5206 AutoWriteLock tlock(i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
5207
5208 for (MediaOList::iterator it = m->allHardDisks.getList().begin();
5209 it != m->allHardDisks.getList().end();
5210 ++it)
5211 {
5212 ComObjPtr<Medium> pMedium = *it;
5213 AutoCaller medCaller(pMedium);
5214 if (FAILED(medCaller.rc())) return medCaller.rc();
5215 AutoReadLock medlock(pMedium COMMA_LOCKVAL_SRC_POS);
5216
5217 if (pMedium->i_isInRegistry(uuidMachine))
5218 // recursively with children first
5219 i_pushMediumToListWithChildren(llMedia2Close, pMedium);
5220 }
5221 }
5222
5223 for (MediaList::iterator it = llMedia2Close.begin();
5224 it != llMedia2Close.end();
5225 ++it)
5226 {
5227 ComObjPtr<Medium> pMedium = *it;
5228 Log(("Closing medium %RTuuid\n", pMedium->i_getId().raw()));
5229 AutoCaller mac(pMedium);
5230 pMedium->i_close(mac);
5231 }
5232
5233 LogFlowFuncLeave();
5234
5235 return S_OK;
5236}
5237
5238/**
5239 * Removes the given machine object from the internal list of registered machines.
5240 * Called from Machine::Unregister().
5241 * @param pMachine
5242 * @param id UUID of the machine. Must be passed by caller because machine may be dead by this time.
5243 * @return
5244 */
5245HRESULT VirtualBox::i_unregisterMachine(Machine *pMachine,
5246 const Guid &id)
5247{
5248 // remove from the collection of registered machines
5249 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
5250 m->allMachines.removeChild(pMachine);
5251 // save the global registry
5252 HRESULT rc = i_saveSettings();
5253 alock.release();
5254
5255 /*
5256 * Now go over all known media and checks if they were registered in the
5257 * media registry of the given machine. Each such medium is then moved to
5258 * a different media registry to make sure it doesn't get lost since its
5259 * media registry is about to go away.
5260 *
5261 * This fixes the following use case: Image A.vdi of machine A is also used
5262 * by machine B, but registered in the media registry of machine A. If machine
5263 * A is deleted, A.vdi must be moved to the registry of B, or else B will
5264 * become inaccessible.
5265 */
5266 {
5267 AutoReadLock tlock(i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
5268 // iterate over the list of *base* images
5269 for (MediaOList::iterator it = m->allHardDisks.getList().begin();
5270 it != m->allHardDisks.getList().end();
5271 ++it)
5272 {
5273 ComObjPtr<Medium> &pMedium = *it;
5274 AutoCaller medCaller(pMedium);
5275 if (FAILED(medCaller.rc())) return medCaller.rc();
5276 AutoWriteLock mlock(pMedium COMMA_LOCKVAL_SRC_POS);
5277
5278 if (pMedium->i_removeRegistryRecursive(id))
5279 {
5280 // machine ID was found in base medium's registry list:
5281 // move this base image and all its children to another registry then
5282 // 1) first, find a better registry to add things to
5283 const Guid *puuidBetter = pMedium->i_getAnyMachineBackref();
5284 if (puuidBetter)
5285 {
5286 // 2) better registry found: then use that
5287 pMedium->i_addRegistryRecursive(*puuidBetter);
5288 // 3) and make sure the registry is saved below
5289 mlock.release();
5290 tlock.release();
5291 i_markRegistryModified(*puuidBetter);
5292 tlock.acquire();
5293 mlock.acquire();
5294 }
5295 }
5296 }
5297 }
5298
5299 i_saveModifiedRegistries();
5300
5301 /* fire an event */
5302 i_onMachineRegistered(id, FALSE);
5303
5304 return rc;
5305}
5306
5307/**
5308 * Marks the registry for @a uuid as modified, so that it's saved in a later
5309 * call to saveModifiedRegistries().
5310 *
5311 * @param uuid
5312 */
5313void VirtualBox::i_markRegistryModified(const Guid &uuid)
5314{
5315 if (uuid == i_getGlobalRegistryId())
5316 ASMAtomicIncU64(&m->uRegistryNeedsSaving);
5317 else
5318 {
5319 ComObjPtr<Machine> pMachine;
5320 HRESULT rc = i_findMachine(uuid,
5321 false /* fPermitInaccessible */,
5322 false /* aSetError */,
5323 &pMachine);
5324 if (SUCCEEDED(rc))
5325 {
5326 AutoCaller machineCaller(pMachine);
5327 if (SUCCEEDED(machineCaller.rc()) && pMachine->i_isAccessible())
5328 ASMAtomicIncU64(&pMachine->uRegistryNeedsSaving);
5329 }
5330 }
5331}
5332
5333/**
5334 * Marks the registry for @a uuid as unmodified, so that it's not saved in
5335 * a later call to saveModifiedRegistries().
5336 *
5337 * @param uuid
5338 */
5339void VirtualBox::i_unmarkRegistryModified(const Guid &uuid)
5340{
5341 uint64_t uOld;
5342 if (uuid == i_getGlobalRegistryId())
5343 {
5344 for (;;)
5345 {
5346 uOld = ASMAtomicReadU64(&m->uRegistryNeedsSaving);
5347 if (!uOld)
5348 break;
5349 if (ASMAtomicCmpXchgU64(&m->uRegistryNeedsSaving, 0, uOld))
5350 break;
5351 ASMNopPause();
5352 }
5353 }
5354 else
5355 {
5356 ComObjPtr<Machine> pMachine;
5357 HRESULT rc = i_findMachine(uuid,
5358 false /* fPermitInaccessible */,
5359 false /* aSetError */,
5360 &pMachine);
5361 if (SUCCEEDED(rc))
5362 {
5363 AutoCaller machineCaller(pMachine);
5364 if (SUCCEEDED(machineCaller.rc()))
5365 {
5366 for (;;)
5367 {
5368 uOld = ASMAtomicReadU64(&pMachine->uRegistryNeedsSaving);
5369 if (!uOld)
5370 break;
5371 if (ASMAtomicCmpXchgU64(&pMachine->uRegistryNeedsSaving, 0, uOld))
5372 break;
5373 ASMNopPause();
5374 }
5375 }
5376 }
5377 }
5378}
5379
5380/**
5381 * Saves all settings files according to the modified flags in the Machine
5382 * objects and in the VirtualBox object.
5383 *
5384 * This locks machines and the VirtualBox object as necessary, so better not
5385 * hold any locks before calling this.
5386 *
5387 * @return
5388 */
5389void VirtualBox::i_saveModifiedRegistries()
5390{
5391 HRESULT rc = S_OK;
5392 bool fNeedsGlobalSettings = false;
5393 uint64_t uOld;
5394
5395 {
5396 AutoReadLock alock(m->allMachines.getLockHandle() COMMA_LOCKVAL_SRC_POS);
5397 for (MachinesOList::iterator it = m->allMachines.begin();
5398 it != m->allMachines.end();
5399 ++it)
5400 {
5401 const ComObjPtr<Machine> &pMachine = *it;
5402
5403 for (;;)
5404 {
5405 uOld = ASMAtomicReadU64(&pMachine->uRegistryNeedsSaving);
5406 if (!uOld)
5407 break;
5408 if (ASMAtomicCmpXchgU64(&pMachine->uRegistryNeedsSaving, 0, uOld))
5409 break;
5410 ASMNopPause();
5411 }
5412 if (uOld)
5413 {
5414 AutoCaller autoCaller(pMachine);
5415 if (FAILED(autoCaller.rc()))
5416 continue;
5417 /* object is already dead, no point in saving settings */
5418 if (getObjectState().getState() != ObjectState::Ready)
5419 continue;
5420 AutoWriteLock mlock(pMachine COMMA_LOCKVAL_SRC_POS);
5421 rc = pMachine->i_saveSettings(&fNeedsGlobalSettings,
5422 Machine::SaveS_Force); // caller said save, so stop arguing
5423 }
5424 }
5425 }
5426
5427 for (;;)
5428 {
5429 uOld = ASMAtomicReadU64(&m->uRegistryNeedsSaving);
5430 if (!uOld)
5431 break;
5432 if (ASMAtomicCmpXchgU64(&m->uRegistryNeedsSaving, 0, uOld))
5433 break;
5434 ASMNopPause();
5435 }
5436 if (uOld || fNeedsGlobalSettings)
5437 {
5438 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
5439 rc = i_saveSettings();
5440 }
5441 NOREF(rc); /* XXX */
5442}
5443
5444
5445/* static */
5446const com::Utf8Str &VirtualBox::i_getVersionNormalized()
5447{
5448 return sVersionNormalized;
5449}
5450
5451/**
5452 * Checks if the path to the specified file exists, according to the path
5453 * information present in the file name. Optionally the path is created.
5454 *
5455 * Note that the given file name must contain the full path otherwise the
5456 * extracted relative path will be created based on the current working
5457 * directory which is normally unknown.
5458 *
5459 * @param strFileName Full file name which path is checked/created.
5460 * @param fCreate Flag if the path should be created if it doesn't exist.
5461 *
5462 * @return Extended error information on failure to check/create the path.
5463 */
5464/* static */
5465HRESULT VirtualBox::i_ensureFilePathExists(const Utf8Str &strFileName, bool fCreate)
5466{
5467 Utf8Str strDir(strFileName);
5468 strDir.stripFilename();
5469 if (!RTDirExists(strDir.c_str()))
5470 {
5471 if (fCreate)
5472 {
5473 int vrc = RTDirCreateFullPath(strDir.c_str(), 0700);
5474 if (RT_FAILURE(vrc))
5475 return i_setErrorStaticBoth(VBOX_E_IPRT_ERROR, vrc,
5476 Utf8StrFmt(tr("Could not create the directory '%s' (%Rrc)"),
5477 strDir.c_str(),
5478 vrc));
5479 }
5480 else
5481 return i_setErrorStaticBoth(VBOX_E_IPRT_ERROR, VERR_FILE_NOT_FOUND,
5482 Utf8StrFmt(tr("Directory '%s' does not exist"), strDir.c_str()));
5483 }
5484
5485 return S_OK;
5486}
5487
5488const Utf8Str& VirtualBox::i_settingsFilePath()
5489{
5490 return m->strSettingsFilePath;
5491}
5492
5493/**
5494 * Returns the lock handle which protects the machines list. As opposed
5495 * to version 3.1 and earlier, these lists are no longer protected by the
5496 * VirtualBox lock, but by this more specialized lock. Mind the locking
5497 * order: always request this lock after the VirtualBox object lock but
5498 * before the locks of any machine object. See AutoLock.h.
5499 */
5500RWLockHandle& VirtualBox::i_getMachinesListLockHandle()
5501{
5502 return m->lockMachines;
5503}
5504
5505/**
5506 * Returns the lock handle which protects the media trees (hard disks,
5507 * DVDs, floppies). As opposed to version 3.1 and earlier, these lists
5508 * are no longer protected by the VirtualBox lock, but by this more
5509 * specialized lock. Mind the locking order: always request this lock
5510 * after the VirtualBox object lock but before the locks of the media
5511 * objects contained in these lists. See AutoLock.h.
5512 */
5513RWLockHandle& VirtualBox::i_getMediaTreeLockHandle()
5514{
5515 return m->lockMedia;
5516}
5517
5518/**
5519 * Thread function that handles custom events posted using #i_postEvent().
5520 */
5521// static
5522DECLCALLBACK(int) VirtualBox::AsyncEventHandler(RTTHREAD thread, void *pvUser)
5523{
5524 LogFlowFuncEnter();
5525
5526 AssertReturn(pvUser, VERR_INVALID_POINTER);
5527
5528 HRESULT hr = com::Initialize();
5529 if (FAILED(hr))
5530 return VERR_COM_UNEXPECTED;
5531
5532 int rc = VINF_SUCCESS;
5533
5534 try
5535 {
5536 /* Create an event queue for the current thread. */
5537 EventQueue *pEventQueue = new EventQueue();
5538 AssertPtr(pEventQueue);
5539
5540 /* Return the queue to the one who created this thread. */
5541 *(static_cast <EventQueue **>(pvUser)) = pEventQueue;
5542
5543 /* signal that we're ready. */
5544 RTThreadUserSignal(thread);
5545
5546 /*
5547 * In case of spurious wakeups causing VERR_TIMEOUTs and/or other return codes
5548 * we must not stop processing events and delete the pEventQueue object. This must
5549 * be done ONLY when we stop this loop via interruptEventQueueProcessing().
5550 * See @bugref{5724}.
5551 */
5552 for (;;)
5553 {
5554 rc = pEventQueue->processEventQueue(RT_INDEFINITE_WAIT);
5555 if (rc == VERR_INTERRUPTED)
5556 {
5557 LogFlow(("Event queue processing ended with rc=%Rrc\n", rc));
5558 rc = VINF_SUCCESS; /* Set success when exiting. */
5559 break;
5560 }
5561 }
5562
5563 delete pEventQueue;
5564 }
5565 catch (std::bad_alloc &ba)
5566 {
5567 rc = VERR_NO_MEMORY;
5568 NOREF(ba);
5569 }
5570
5571 com::Shutdown();
5572
5573 LogFlowFuncLeaveRC(rc);
5574 return rc;
5575}
5576
5577
5578////////////////////////////////////////////////////////////////////////////////
5579
5580/**
5581 * Prepare the event using the overwritten #prepareEventDesc method and fire.
5582 *
5583 * @note Locks the managed VirtualBox object for reading but leaves the lock
5584 * before iterating over callbacks and calling their methods.
5585 */
5586void *VirtualBox::CallbackEvent::handler()
5587{
5588 if (!mVirtualBox)
5589 return NULL;
5590
5591 AutoCaller autoCaller(mVirtualBox);
5592 if (!autoCaller.isOk())
5593 {
5594 Log1WarningFunc(("VirtualBox has been uninitialized (state=%d), the callback event is discarded!\n",
5595 mVirtualBox->getObjectState().getState()));
5596 /* We don't need mVirtualBox any more, so release it */
5597 mVirtualBox = NULL;
5598 return NULL;
5599 }
5600
5601 {
5602 VBoxEventDesc evDesc;
5603 prepareEventDesc(mVirtualBox->m->pEventSource, evDesc);
5604
5605 evDesc.fire(/* don't wait for delivery */0);
5606 }
5607
5608 mVirtualBox = NULL; /* Not needed any longer. Still make sense to do this? */
5609 return NULL;
5610}
5611
5612//STDMETHODIMP VirtualBox::CreateDHCPServerForInterface(/*IHostNetworkInterface * aIinterface,*/ IDHCPServer ** aServer)
5613//{
5614// return E_NOTIMPL;
5615//}
5616
5617HRESULT VirtualBox::createDHCPServer(const com::Utf8Str &aName,
5618 ComPtr<IDHCPServer> &aServer)
5619{
5620 ComObjPtr<DHCPServer> dhcpServer;
5621 dhcpServer.createObject();
5622 HRESULT rc = dhcpServer->init(this, aName);
5623 if (FAILED(rc)) return rc;
5624
5625 rc = i_registerDHCPServer(dhcpServer, true);
5626 if (FAILED(rc)) return rc;
5627
5628 dhcpServer.queryInterfaceTo(aServer.asOutParam());
5629
5630 return rc;
5631}
5632
5633HRESULT VirtualBox::findDHCPServerByNetworkName(const com::Utf8Str &aName,
5634 ComPtr<IDHCPServer> &aServer)
5635{
5636 HRESULT rc = S_OK;
5637 ComPtr<DHCPServer> found;
5638
5639 AutoReadLock alock(m->allDHCPServers.getLockHandle() COMMA_LOCKVAL_SRC_POS);
5640
5641 for (DHCPServersOList::const_iterator it = m->allDHCPServers.begin();
5642 it != m->allDHCPServers.end();
5643 ++it)
5644 {
5645 Bstr bstrNetworkName;
5646 rc = (*it)->COMGETTER(NetworkName)(bstrNetworkName.asOutParam());
5647 if (FAILED(rc)) return rc;
5648
5649 if (Utf8Str(bstrNetworkName) == aName)
5650 {
5651 found = *it;
5652 break;
5653 }
5654 }
5655
5656 if (!found)
5657 return E_INVALIDARG;
5658
5659 rc = found.queryInterfaceTo(aServer.asOutParam());
5660
5661 return rc;
5662}
5663
5664HRESULT VirtualBox::removeDHCPServer(const ComPtr<IDHCPServer> &aServer)
5665{
5666 IDHCPServer *aP = aServer;
5667
5668 HRESULT rc = i_unregisterDHCPServer(static_cast<DHCPServer *>(aP));
5669
5670 return rc;
5671}
5672
5673/**
5674 * Remembers the given DHCP server in the settings.
5675 *
5676 * @param aDHCPServer DHCP server object to remember.
5677 * @param aSaveSettings @c true to save settings to disk (default).
5678 *
5679 * When @a aSaveSettings is @c true, this operation may fail because of the
5680 * failed #i_saveSettings() method it calls. In this case, the dhcp server object
5681 * will not be remembered. It is therefore the responsibility of the caller to
5682 * call this method as the last step of some action that requires registration
5683 * in order to make sure that only fully functional dhcp server objects get
5684 * registered.
5685 *
5686 * @note Locks this object for writing and @a aDHCPServer for reading.
5687 */
5688HRESULT VirtualBox::i_registerDHCPServer(DHCPServer *aDHCPServer,
5689 bool aSaveSettings /*= true*/)
5690{
5691 AssertReturn(aDHCPServer != NULL, E_INVALIDARG);
5692
5693 AutoCaller autoCaller(this);
5694 AssertComRCReturnRC(autoCaller.rc());
5695
5696 // Acquire a lock on the VirtualBox object early to avoid lock order issues
5697 // when we call i_saveSettings() later on.
5698 AutoWriteLock vboxLock(this COMMA_LOCKVAL_SRC_POS);
5699 // need it below, in findDHCPServerByNetworkName (reading) and in
5700 // m->allDHCPServers.addChild, so need to get it here to avoid lock
5701 // order trouble with dhcpServerCaller
5702 AutoWriteLock alock(m->allDHCPServers.getLockHandle() COMMA_LOCKVAL_SRC_POS);
5703
5704 AutoCaller dhcpServerCaller(aDHCPServer);
5705 AssertComRCReturnRC(dhcpServerCaller.rc());
5706
5707 Bstr bstrNetworkName;
5708 HRESULT rc = S_OK;
5709 rc = aDHCPServer->COMGETTER(NetworkName)(bstrNetworkName.asOutParam());
5710 if (FAILED(rc)) return rc;
5711
5712 ComPtr<IDHCPServer> existing;
5713 rc = findDHCPServerByNetworkName(Utf8Str(bstrNetworkName), existing);
5714 if (SUCCEEDED(rc))
5715 return E_INVALIDARG;
5716 rc = S_OK;
5717
5718 m->allDHCPServers.addChild(aDHCPServer);
5719 // we need to release the list lock before we attempt to acquire locks
5720 // on other objects in i_saveSettings (see @bugref{7500})
5721 alock.release();
5722
5723 if (aSaveSettings)
5724 {
5725 // we acquired the lock on 'this' earlier to avoid lock order issues
5726 rc = i_saveSettings();
5727
5728 if (FAILED(rc))
5729 {
5730 alock.acquire();
5731 m->allDHCPServers.removeChild(aDHCPServer);
5732 }
5733 }
5734
5735 return rc;
5736}
5737
5738/**
5739 * Removes the given DHCP server from the settings.
5740 *
5741 * @param aDHCPServer DHCP server object to remove.
5742 *
5743 * This operation may fail because of the failed #i_saveSettings() method it
5744 * calls. In this case, the DHCP server will NOT be removed from the settings
5745 * when this method returns.
5746 *
5747 * @note Locks this object for writing.
5748 */
5749HRESULT VirtualBox::i_unregisterDHCPServer(DHCPServer *aDHCPServer)
5750{
5751 AssertReturn(aDHCPServer != NULL, E_INVALIDARG);
5752
5753 AutoCaller autoCaller(this);
5754 AssertComRCReturnRC(autoCaller.rc());
5755
5756 AutoCaller dhcpServerCaller(aDHCPServer);
5757 AssertComRCReturnRC(dhcpServerCaller.rc());
5758
5759 AutoWriteLock vboxLock(this COMMA_LOCKVAL_SRC_POS);
5760 AutoWriteLock alock(m->allDHCPServers.getLockHandle() COMMA_LOCKVAL_SRC_POS);
5761 m->allDHCPServers.removeChild(aDHCPServer);
5762 // we need to release the list lock before we attempt to acquire locks
5763 // on other objects in i_saveSettings (see @bugref{7500})
5764 alock.release();
5765
5766 HRESULT rc = i_saveSettings();
5767
5768 // undo the changes if we failed to save them
5769 if (FAILED(rc))
5770 {
5771 alock.acquire();
5772 m->allDHCPServers.addChild(aDHCPServer);
5773 }
5774
5775 return rc;
5776}
5777
5778
5779/**
5780 * NAT Network
5781 */
5782HRESULT VirtualBox::createNATNetwork(const com::Utf8Str &aNetworkName,
5783 ComPtr<INATNetwork> &aNetwork)
5784{
5785#ifdef VBOX_WITH_NAT_SERVICE
5786 ComObjPtr<NATNetwork> natNetwork;
5787 natNetwork.createObject();
5788 HRESULT rc = natNetwork->init(this, aNetworkName);
5789 if (FAILED(rc)) return rc;
5790
5791 rc = i_registerNATNetwork(natNetwork, true);
5792 if (FAILED(rc)) return rc;
5793
5794 natNetwork.queryInterfaceTo(aNetwork.asOutParam());
5795
5796 fireNATNetworkCreationDeletionEvent(m->pEventSource, Bstr(aNetworkName).raw(), TRUE);
5797
5798 return rc;
5799#else
5800 NOREF(aNetworkName);
5801 NOREF(aNetwork);
5802 return E_NOTIMPL;
5803#endif
5804}
5805
5806HRESULT VirtualBox::findNATNetworkByName(const com::Utf8Str &aNetworkName,
5807 ComPtr<INATNetwork> &aNetwork)
5808{
5809#ifdef VBOX_WITH_NAT_SERVICE
5810
5811 HRESULT rc = S_OK;
5812 ComPtr<NATNetwork> found;
5813
5814 AutoReadLock alock(m->allNATNetworks.getLockHandle() COMMA_LOCKVAL_SRC_POS);
5815
5816 for (NATNetworksOList::const_iterator it = m->allNATNetworks.begin();
5817 it != m->allNATNetworks.end();
5818 ++it)
5819 {
5820 Bstr bstrNATNetworkName;
5821 rc = (*it)->COMGETTER(NetworkName)(bstrNATNetworkName.asOutParam());
5822 if (FAILED(rc)) return rc;
5823
5824 if (Utf8Str(bstrNATNetworkName) == aNetworkName)
5825 {
5826 found = *it;
5827 break;
5828 }
5829 }
5830
5831 if (!found)
5832 return E_INVALIDARG;
5833 found.queryInterfaceTo(aNetwork.asOutParam());
5834 return rc;
5835#else
5836 NOREF(aNetworkName);
5837 NOREF(aNetwork);
5838 return E_NOTIMPL;
5839#endif
5840}
5841
5842HRESULT VirtualBox::removeNATNetwork(const ComPtr<INATNetwork> &aNetwork)
5843{
5844#ifdef VBOX_WITH_NAT_SERVICE
5845 Bstr name;
5846 HRESULT rc = aNetwork->COMGETTER(NetworkName)(name.asOutParam());
5847 if (FAILED(rc))
5848 return rc;
5849 INATNetwork *p = aNetwork;
5850 NATNetwork *network = static_cast<NATNetwork *>(p);
5851 rc = i_unregisterNATNetwork(network, true);
5852 fireNATNetworkCreationDeletionEvent(m->pEventSource, name.raw(), FALSE);
5853 return rc;
5854#else
5855 NOREF(aNetwork);
5856 return E_NOTIMPL;
5857#endif
5858
5859}
5860/**
5861 * Remembers the given NAT network in the settings.
5862 *
5863 * @param aNATNetwork NAT Network object to remember.
5864 * @param aSaveSettings @c true to save settings to disk (default).
5865 *
5866 *
5867 * @note Locks this object for writing and @a aNATNetwork for reading.
5868 */
5869HRESULT VirtualBox::i_registerNATNetwork(NATNetwork *aNATNetwork,
5870 bool aSaveSettings /*= true*/)
5871{
5872#ifdef VBOX_WITH_NAT_SERVICE
5873 AssertReturn(aNATNetwork != NULL, E_INVALIDARG);
5874
5875 AutoCaller autoCaller(this);
5876 AssertComRCReturnRC(autoCaller.rc());
5877
5878 AutoCaller natNetworkCaller(aNATNetwork);
5879 AssertComRCReturnRC(natNetworkCaller.rc());
5880
5881 Bstr name;
5882 HRESULT rc;
5883 rc = aNATNetwork->COMGETTER(NetworkName)(name.asOutParam());
5884 AssertComRCReturnRC(rc);
5885
5886 /* returned value isn't 0 and aSaveSettings is true
5887 * means that we create duplicate, otherwise we just load settings.
5888 */
5889 if ( sNatNetworkNameToRefCount[name]
5890 && aSaveSettings)
5891 AssertComRCReturnRC(E_INVALIDARG);
5892
5893 rc = S_OK;
5894
5895 sNatNetworkNameToRefCount[name] = 0;
5896
5897 m->allNATNetworks.addChild(aNATNetwork);
5898
5899 if (aSaveSettings)
5900 {
5901 AutoWriteLock vboxLock(this COMMA_LOCKVAL_SRC_POS);
5902 rc = i_saveSettings();
5903 vboxLock.release();
5904
5905 if (FAILED(rc))
5906 i_unregisterNATNetwork(aNATNetwork, false /* aSaveSettings */);
5907 }
5908
5909 return rc;
5910#else
5911 NOREF(aNATNetwork);
5912 NOREF(aSaveSettings);
5913 /* No panic please (silently ignore) */
5914 return S_OK;
5915#endif
5916}
5917
5918/**
5919 * Removes the given NAT network from the settings.
5920 *
5921 * @param aNATNetwork NAT network object to remove.
5922 * @param aSaveSettings @c true to save settings to disk (default).
5923 *
5924 * When @a aSaveSettings is @c true, this operation may fail because of the
5925 * failed #i_saveSettings() method it calls. In this case, the DHCP server
5926 * will NOT be removed from the settingsi when this method returns.
5927 *
5928 * @note Locks this object for writing.
5929 */
5930HRESULT VirtualBox::i_unregisterNATNetwork(NATNetwork *aNATNetwork,
5931 bool aSaveSettings /*= true*/)
5932{
5933#ifdef VBOX_WITH_NAT_SERVICE
5934 AssertReturn(aNATNetwork != NULL, E_INVALIDARG);
5935
5936 AutoCaller autoCaller(this);
5937 AssertComRCReturnRC(autoCaller.rc());
5938
5939 AutoCaller natNetworkCaller(aNATNetwork);
5940 AssertComRCReturnRC(natNetworkCaller.rc());
5941
5942 Bstr name;
5943 HRESULT rc = aNATNetwork->COMGETTER(NetworkName)(name.asOutParam());
5944 /* Hm, there're still running clients. */
5945 if (FAILED(rc) || sNatNetworkNameToRefCount[name])
5946 AssertComRCReturnRC(E_INVALIDARG);
5947
5948 m->allNATNetworks.removeChild(aNATNetwork);
5949
5950 if (aSaveSettings)
5951 {
5952 AutoWriteLock vboxLock(this COMMA_LOCKVAL_SRC_POS);
5953 rc = i_saveSettings();
5954 vboxLock.release();
5955
5956 if (FAILED(rc))
5957 i_registerNATNetwork(aNATNetwork, false /* aSaveSettings */);
5958 }
5959
5960 return rc;
5961#else
5962 NOREF(aNATNetwork);
5963 NOREF(aSaveSettings);
5964 return E_NOTIMPL;
5965#endif
5966}
5967
5968
5969#ifdef RT_OS_WINDOWS
5970#include <psapi.h>
5971
5972/**
5973 * Report versions of installed drivers to release log.
5974 */
5975void VirtualBox::i_reportDriverVersions()
5976{
5977 /** @todo r=klaus this code is very confusing, as it uses TCHAR (and
5978 * randomly also _TCHAR, which sounds to me like asking for trouble),
5979 * the "sz" variable prefix but "%ls" for the format string - so the whole
5980 * thing is better compiled with UNICODE and _UNICODE defined. Would be
5981 * far easier to read if it would be coded explicitly for the unicode
5982 * case, as it won't work otherwise. */
5983 DWORD err;
5984 HRESULT hrc;
5985 LPVOID aDrivers[1024];
5986 LPVOID *pDrivers = aDrivers;
5987 UINT cNeeded = 0;
5988 TCHAR szSystemRoot[MAX_PATH];
5989 TCHAR *pszSystemRoot = szSystemRoot;
5990 LPVOID pVerInfo = NULL;
5991 DWORD cbVerInfo = 0;
5992
5993 do
5994 {
5995 cNeeded = GetWindowsDirectory(szSystemRoot, RT_ELEMENTS(szSystemRoot));
5996 if (cNeeded == 0)
5997 {
5998 err = GetLastError();
5999 hrc = HRESULT_FROM_WIN32(err);
6000 AssertLogRelMsgFailed(("GetWindowsDirectory failed, hr=%Rhrc (0x%x) err=%u\n",
6001 hrc, hrc, err));
6002 break;
6003 }
6004 else if (cNeeded > RT_ELEMENTS(szSystemRoot))
6005 {
6006 /* The buffer is too small, allocate big one. */
6007 pszSystemRoot = (TCHAR *)RTMemTmpAlloc(cNeeded * sizeof(_TCHAR));
6008 if (!pszSystemRoot)
6009 {
6010 AssertLogRelMsgFailed(("RTMemTmpAlloc failed to allocate %d bytes\n", cNeeded));
6011 break;
6012 }
6013 if (GetWindowsDirectory(pszSystemRoot, cNeeded) == 0)
6014 {
6015 err = GetLastError();
6016 hrc = HRESULT_FROM_WIN32(err);
6017 AssertLogRelMsgFailed(("GetWindowsDirectory failed, hr=%Rhrc (0x%x) err=%u\n",
6018 hrc, hrc, err));
6019 break;
6020 }
6021 }
6022
6023 DWORD cbNeeded = 0;
6024 if (!EnumDeviceDrivers(aDrivers, sizeof(aDrivers), &cbNeeded) || cbNeeded > sizeof(aDrivers))
6025 {
6026 pDrivers = (LPVOID *)RTMemTmpAlloc(cbNeeded);
6027 if (!EnumDeviceDrivers(pDrivers, cbNeeded, &cbNeeded))
6028 {
6029 err = GetLastError();
6030 hrc = HRESULT_FROM_WIN32(err);
6031 AssertLogRelMsgFailed(("EnumDeviceDrivers failed, hr=%Rhrc (0x%x) err=%u\n",
6032 hrc, hrc, err));
6033 break;
6034 }
6035 }
6036
6037 LogRel(("Installed Drivers:\n"));
6038
6039 TCHAR szDriver[1024];
6040 int cDrivers = cbNeeded / sizeof(pDrivers[0]);
6041 for (int i = 0; i < cDrivers; i++)
6042 {
6043 if (GetDeviceDriverBaseName(pDrivers[i], szDriver, sizeof(szDriver) / sizeof(szDriver[0])))
6044 {
6045 if (_tcsnicmp(TEXT("vbox"), szDriver, 4))
6046 continue;
6047 }
6048 else
6049 continue;
6050 if (GetDeviceDriverFileName(pDrivers[i], szDriver, sizeof(szDriver) / sizeof(szDriver[0])))
6051 {
6052 _TCHAR szTmpDrv[1024];
6053 _TCHAR *pszDrv = szDriver;
6054 if (!_tcsncmp(TEXT("\\SystemRoot"), szDriver, 11))
6055 {
6056 _tcscpy_s(szTmpDrv, pszSystemRoot);
6057 _tcsncat_s(szTmpDrv, szDriver + 11, sizeof(szTmpDrv) / sizeof(szTmpDrv[0]) - _tclen(pszSystemRoot));
6058 pszDrv = szTmpDrv;
6059 }
6060 else if (!_tcsncmp(TEXT("\\??\\"), szDriver, 4))
6061 pszDrv = szDriver + 4;
6062
6063 /* Allocate a buffer for version info. Reuse if large enough. */
6064 DWORD cbNewVerInfo = GetFileVersionInfoSize(pszDrv, NULL);
6065 if (cbNewVerInfo > cbVerInfo)
6066 {
6067 if (pVerInfo)
6068 RTMemTmpFree(pVerInfo);
6069 cbVerInfo = cbNewVerInfo;
6070 pVerInfo = RTMemTmpAlloc(cbVerInfo);
6071 if (!pVerInfo)
6072 {
6073 AssertLogRelMsgFailed(("RTMemTmpAlloc failed to allocate %d bytes\n", cbVerInfo));
6074 break;
6075 }
6076 }
6077
6078 if (GetFileVersionInfo(pszDrv, NULL, cbVerInfo, pVerInfo))
6079 {
6080 UINT cbSize = 0;
6081 LPBYTE lpBuffer = NULL;
6082 if (VerQueryValue(pVerInfo, TEXT("\\"), (VOID FAR* FAR*)&lpBuffer, &cbSize))
6083 {
6084 if (cbSize)
6085 {
6086 VS_FIXEDFILEINFO *pFileInfo = (VS_FIXEDFILEINFO *)lpBuffer;
6087 if (pFileInfo->dwSignature == 0xfeef04bd)
6088 {
6089 LogRel((" %ls (Version: %d.%d.%d.%d)\n", pszDrv,
6090 (pFileInfo->dwFileVersionMS >> 16) & 0xffff,
6091 (pFileInfo->dwFileVersionMS >> 0) & 0xffff,
6092 (pFileInfo->dwFileVersionLS >> 16) & 0xffff,
6093 (pFileInfo->dwFileVersionLS >> 0) & 0xffff));
6094 }
6095 }
6096 }
6097 }
6098 }
6099 }
6100
6101 }
6102 while (0);
6103
6104 if (pVerInfo)
6105 RTMemTmpFree(pVerInfo);
6106
6107 if (pDrivers != aDrivers)
6108 RTMemTmpFree(pDrivers);
6109
6110 if (pszSystemRoot != szSystemRoot)
6111 RTMemTmpFree(pszSystemRoot);
6112}
6113#else /* !RT_OS_WINDOWS */
6114void VirtualBox::i_reportDriverVersions(void)
6115{
6116}
6117#endif /* !RT_OS_WINDOWS */
6118
6119#if defined(RT_OS_WINDOWS) && defined(VBOXSVC_WITH_CLIENT_WATCHER)
6120
6121# include <psapi.h> /* for GetProcessImageFileNameW */
6122
6123/**
6124 * Callout from the wrapper.
6125 */
6126void VirtualBox::i_callHook(const char *a_pszFunction)
6127{
6128 RT_NOREF(a_pszFunction);
6129
6130 /*
6131 * Let'see figure out who is calling.
6132 * Note! Requires Vista+, so skip this entirely on older systems.
6133 */
6134 if (RTSystemGetNtVersion() >= RTSYSTEM_MAKE_NT_VERSION(6, 0, 0))
6135 {
6136 RPC_CALL_ATTRIBUTES_V2_W CallAttribs = { RPC_CALL_ATTRIBUTES_VERSION, RPC_QUERY_CLIENT_PID | RPC_QUERY_IS_CLIENT_LOCAL };
6137 RPC_STATUS rcRpc = RpcServerInqCallAttributesW(NULL, &CallAttribs);
6138 if ( rcRpc == RPC_S_OK
6139 && CallAttribs.ClientPID != 0)
6140 {
6141 RTPROCESS const pidClient = (RTPROCESS)(uintptr_t)CallAttribs.ClientPID;
6142 if (pidClient != RTProcSelf())
6143 {
6144 /** @todo LogRel2 later: */
6145 LogRel(("i_callHook: %Rfn [ClientPID=%#zx/%zu IsClientLocal=%d ProtocolSequence=%#x CallStatus=%#x CallType=%#x OpNum=%#x InterfaceUuid=%RTuuid]\n",
6146 a_pszFunction, CallAttribs.ClientPID, CallAttribs.ClientPID, CallAttribs.IsClientLocal,
6147 CallAttribs.ProtocolSequence, CallAttribs.CallStatus, CallAttribs.CallType, CallAttribs.OpNum,
6148 &CallAttribs.InterfaceUuid));
6149
6150 /*
6151 * Do we know this client PID already?
6152 */
6153 RTCritSectRwEnterShared(&m->WatcherCritSect);
6154 WatchedClientProcessMap::iterator It = m->WatchedProcesses.find(pidClient);
6155 if (It != m->WatchedProcesses.end())
6156 RTCritSectRwLeaveShared(&m->WatcherCritSect); /* Known process, nothing to do. */
6157 else
6158 {
6159 /* This is a new client process, start watching it. */
6160 RTCritSectRwLeaveShared(&m->WatcherCritSect);
6161 i_watchClientProcess(pidClient, a_pszFunction);
6162 }
6163 }
6164 }
6165 else
6166 LogRel(("i_callHook: %Rfn - rcRpc=%#x ClientPID=%#zx/%zu !! [IsClientLocal=%d ProtocolSequence=%#x CallStatus=%#x CallType=%#x OpNum=%#x InterfaceUuid=%RTuuid]\n",
6167 a_pszFunction, rcRpc, CallAttribs.ClientPID, CallAttribs.ClientPID, CallAttribs.IsClientLocal,
6168 CallAttribs.ProtocolSequence, CallAttribs.CallStatus, CallAttribs.CallType, CallAttribs.OpNum,
6169 &CallAttribs.InterfaceUuid));
6170 }
6171}
6172
6173
6174/**
6175 * Wathces @a a_pidClient for termination.
6176 *
6177 * @returns true if successfully enabled watching of it, false if not.
6178 * @param a_pidClient The PID to watch.
6179 * @param a_pszFunction The function we which we detected the client in.
6180 */
6181bool VirtualBox::i_watchClientProcess(RTPROCESS a_pidClient, const char *a_pszFunction)
6182{
6183 RT_NOREF_PV(a_pszFunction);
6184
6185 /*
6186 * Open the client process.
6187 */
6188 HANDLE hClient = OpenProcess(SYNCHRONIZE | PROCESS_QUERY_INFORMATION, FALSE /*fInherit*/, a_pidClient);
6189 if (hClient == NULL)
6190 hClient = OpenProcess(SYNCHRONIZE | PROCESS_QUERY_LIMITED_INFORMATION, FALSE , a_pidClient);
6191 if (hClient == NULL)
6192 hClient = OpenProcess(SYNCHRONIZE, FALSE , a_pidClient);
6193 AssertLogRelMsgReturn(hClient != NULL, ("pidClient=%d (%#x) err=%d\n", a_pidClient, a_pidClient, GetLastError()),
6194 m->fWatcherIsReliable = false);
6195
6196 /*
6197 * Create a new watcher structure and try add it to the map.
6198 */
6199 bool fRet = true;
6200 WatchedClientProcess *pWatched = new (std::nothrow) WatchedClientProcess(a_pidClient, hClient);
6201 if (pWatched)
6202 {
6203 RTCritSectRwEnterExcl(&m->WatcherCritSect);
6204
6205 WatchedClientProcessMap::iterator It = m->WatchedProcesses.find(a_pidClient);
6206 if (It == m->WatchedProcesses.end())
6207 {
6208 try
6209 {
6210 m->WatchedProcesses.insert(WatchedClientProcessMap::value_type(a_pidClient, pWatched));
6211 }
6212 catch (std::bad_alloc &)
6213 {
6214 fRet = false;
6215 }
6216 if (fRet)
6217 {
6218 /*
6219 * Schedule it on a watcher thread.
6220 */
6221 /** @todo later. */
6222 RTCritSectRwLeaveExcl(&m->WatcherCritSect);
6223 }
6224 else
6225 {
6226 RTCritSectRwLeaveExcl(&m->WatcherCritSect);
6227 delete pWatched;
6228 LogRel(("VirtualBox::i_watchClientProcess: out of memory inserting into client map!\n"));
6229 }
6230 }
6231 else
6232 {
6233 /*
6234 * Someone raced us here, we lost.
6235 */
6236 RTCritSectRwLeaveExcl(&m->WatcherCritSect);
6237 delete pWatched;
6238 }
6239 }
6240 else
6241 {
6242 LogRel(("VirtualBox::i_watchClientProcess: out of memory!\n"));
6243 CloseHandle(hClient);
6244 m->fWatcherIsReliable = fRet = false;
6245 }
6246 return fRet;
6247}
6248
6249
6250/** Logs the RPC caller info to the release log. */
6251/*static*/ void VirtualBox::i_logCaller(const char *a_pszFormat, ...)
6252{
6253 if (RTSystemGetNtVersion() >= RTSYSTEM_MAKE_NT_VERSION(6, 0, 0))
6254 {
6255 char szTmp[80];
6256 va_list va;
6257 va_start(va, a_pszFormat);
6258 RTStrPrintfV(szTmp, sizeof(szTmp), a_pszFormat, va);
6259 va_end(va);
6260
6261 RPC_CALL_ATTRIBUTES_V2_W CallAttribs = { RPC_CALL_ATTRIBUTES_VERSION, RPC_QUERY_CLIENT_PID | RPC_QUERY_IS_CLIENT_LOCAL };
6262 RPC_STATUS rcRpc = RpcServerInqCallAttributesW(NULL, &CallAttribs);
6263
6264 RTUTF16 wszProcName[256];
6265 wszProcName[0] = '\0';
6266 if (rcRpc == 0 && CallAttribs.ClientPID != 0)
6267 {
6268 HANDLE hProcess = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, (DWORD)(uintptr_t)CallAttribs.ClientPID);
6269 if (hProcess)
6270 {
6271 RT_ZERO(wszProcName);
6272 GetProcessImageFileNameW(hProcess, wszProcName, RT_ELEMENTS(wszProcName) - 1);
6273 CloseHandle(hProcess);
6274 }
6275 }
6276 LogRel(("%s [rcRpc=%#x ClientPID=%#zx/%zu (%ls) IsClientLocal=%d ProtocolSequence=%#x CallStatus=%#x CallType=%#x OpNum=%#x InterfaceUuid=%RTuuid]\n",
6277 szTmp, rcRpc, CallAttribs.ClientPID, CallAttribs.ClientPID, wszProcName, CallAttribs.IsClientLocal,
6278 CallAttribs.ProtocolSequence, CallAttribs.CallStatus, CallAttribs.CallType, CallAttribs.OpNum,
6279 &CallAttribs.InterfaceUuid));
6280 }
6281}
6282
6283#endif /* RT_OS_WINDOWS && VBOXSVC_WITH_CLIENT_WATCHER */
6284
6285
6286/* vi: set tabstop=4 shiftwidth=4 expandtab: */
Note: See TracBrowser for help on using the repository browser.

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