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