VirtualBox

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

Last change on this file since 87008 was 86908, checked in by vboxsync, 4 years ago

Shared Clipboard/Transfers: Removed clipboard area handling code. bugref:9437

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