1 | /* $Id: VirtualBoxImpl.cpp 75663 2018-11-22 14:00:59Z 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();
|
---|
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 | const com::Utf8Str &aAutoMountPoint)
|
---|
2017 | {
|
---|
2018 | NOREF(aName);
|
---|
2019 | NOREF(aHostPath);
|
---|
2020 | NOREF(aWritable);
|
---|
2021 | NOREF(aAutomount);
|
---|
2022 | NOREF(aAutoMountPoint);
|
---|
2023 |
|
---|
2024 | return setError(E_NOTIMPL, "Not yet implemented");
|
---|
2025 | }
|
---|
2026 |
|
---|
2027 | HRESULT VirtualBox::removeSharedFolder(const com::Utf8Str &aName)
|
---|
2028 | {
|
---|
2029 | NOREF(aName);
|
---|
2030 | return setError(E_NOTIMPL, "Not yet implemented");
|
---|
2031 | }
|
---|
2032 |
|
---|
2033 | /**
|
---|
2034 | * @note Locks this object for reading.
|
---|
2035 | */
|
---|
2036 | HRESULT VirtualBox::getExtraDataKeys(std::vector<com::Utf8Str> &aKeys)
|
---|
2037 | {
|
---|
2038 | using namespace settings;
|
---|
2039 |
|
---|
2040 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2041 |
|
---|
2042 | aKeys.resize(m->pMainConfigFile->mapExtraDataItems.size());
|
---|
2043 | size_t i = 0;
|
---|
2044 | for (StringsMap::const_iterator it = m->pMainConfigFile->mapExtraDataItems.begin();
|
---|
2045 | it != m->pMainConfigFile->mapExtraDataItems.end(); ++it, ++i)
|
---|
2046 | aKeys[i] = it->first;
|
---|
2047 |
|
---|
2048 | return S_OK;
|
---|
2049 | }
|
---|
2050 |
|
---|
2051 | /**
|
---|
2052 | * @note Locks this object for reading.
|
---|
2053 | */
|
---|
2054 | HRESULT VirtualBox::getExtraData(const com::Utf8Str &aKey,
|
---|
2055 | com::Utf8Str &aValue)
|
---|
2056 | {
|
---|
2057 | settings::StringsMap::const_iterator it = m->pMainConfigFile->mapExtraDataItems.find(aKey);
|
---|
2058 | if (it != m->pMainConfigFile->mapExtraDataItems.end())
|
---|
2059 | // found:
|
---|
2060 | aValue = it->second; // source is a Utf8Str
|
---|
2061 |
|
---|
2062 | /* return the result to caller (may be empty) */
|
---|
2063 |
|
---|
2064 | return S_OK;
|
---|
2065 | }
|
---|
2066 |
|
---|
2067 | /**
|
---|
2068 | * @note Locks this object for writing.
|
---|
2069 | */
|
---|
2070 | HRESULT VirtualBox::setExtraData(const com::Utf8Str &aKey,
|
---|
2071 | const com::Utf8Str &aValue)
|
---|
2072 | {
|
---|
2073 | Utf8Str strKey(aKey);
|
---|
2074 | Utf8Str strValue(aValue);
|
---|
2075 | Utf8Str strOldValue; // empty
|
---|
2076 | HRESULT rc = S_OK;
|
---|
2077 |
|
---|
2078 | /* Because non-ASCII characters in aKey have caused problems in the settings
|
---|
2079 | * they are rejected unless the key should be deleted. */
|
---|
2080 | if (!strValue.isEmpty())
|
---|
2081 | {
|
---|
2082 | for (size_t i = 0; i < strKey.length(); ++i)
|
---|
2083 | {
|
---|
2084 | char ch = strKey[i];
|
---|
2085 | if (!RTLocCIsPrint(ch))
|
---|
2086 | return E_INVALIDARG;
|
---|
2087 | }
|
---|
2088 | }
|
---|
2089 |
|
---|
2090 | // locking note: we only hold the read lock briefly to look up the old value,
|
---|
2091 | // then release it and call the onExtraCanChange callbacks. There is a small
|
---|
2092 | // chance of a race insofar as the callback might be called twice if two callers
|
---|
2093 | // change the same key at the same time, but that's a much better solution
|
---|
2094 | // than the deadlock we had here before. The actual changing of the extradata
|
---|
2095 | // is then performed under the write lock and race-free.
|
---|
2096 |
|
---|
2097 | // look up the old value first; if nothing has changed then we need not do anything
|
---|
2098 | {
|
---|
2099 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS); // hold read lock only while looking up
|
---|
2100 | settings::StringsMap::const_iterator it = m->pMainConfigFile->mapExtraDataItems.find(strKey);
|
---|
2101 | if (it != m->pMainConfigFile->mapExtraDataItems.end())
|
---|
2102 | strOldValue = it->second;
|
---|
2103 | }
|
---|
2104 |
|
---|
2105 | bool fChanged;
|
---|
2106 | if ((fChanged = (strOldValue != strValue)))
|
---|
2107 | {
|
---|
2108 | // ask for permission from all listeners outside the locks;
|
---|
2109 | // onExtraDataCanChange() only briefly requests the VirtualBox
|
---|
2110 | // lock to copy the list of callbacks to invoke
|
---|
2111 | Bstr error;
|
---|
2112 |
|
---|
2113 | if (!i_onExtraDataCanChange(Guid::Empty, Bstr(aKey).raw(), Bstr(aValue).raw(), error))
|
---|
2114 | {
|
---|
2115 | const char *sep = error.isEmpty() ? "" : ": ";
|
---|
2116 | CBSTR err = error.raw();
|
---|
2117 | Log1WarningFunc(("Someone vetoed! Change refused%s%ls\n", sep, err));
|
---|
2118 | return setError(E_ACCESSDENIED,
|
---|
2119 | tr("Could not set extra data because someone refused the requested change of '%s' to '%s'%s%ls"),
|
---|
2120 | strKey.c_str(),
|
---|
2121 | strValue.c_str(),
|
---|
2122 | sep,
|
---|
2123 | err);
|
---|
2124 | }
|
---|
2125 |
|
---|
2126 | // data is changing and change not vetoed: then write it out under the lock
|
---|
2127 |
|
---|
2128 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2129 |
|
---|
2130 | if (strValue.isEmpty())
|
---|
2131 | m->pMainConfigFile->mapExtraDataItems.erase(strKey);
|
---|
2132 | else
|
---|
2133 | m->pMainConfigFile->mapExtraDataItems[strKey] = strValue;
|
---|
2134 | // creates a new key if needed
|
---|
2135 |
|
---|
2136 | /* save settings on success */
|
---|
2137 | rc = i_saveSettings();
|
---|
2138 | if (FAILED(rc)) return rc;
|
---|
2139 | }
|
---|
2140 |
|
---|
2141 | // fire notification outside the lock
|
---|
2142 | if (fChanged)
|
---|
2143 | i_onExtraDataChange(Guid::Empty, Bstr(aKey).raw(), Bstr(aValue).raw());
|
---|
2144 |
|
---|
2145 | return rc;
|
---|
2146 | }
|
---|
2147 |
|
---|
2148 | /**
|
---|
2149 | *
|
---|
2150 | */
|
---|
2151 | HRESULT VirtualBox::setSettingsSecret(const com::Utf8Str &aPassword)
|
---|
2152 | {
|
---|
2153 | i_storeSettingsKey(aPassword);
|
---|
2154 | i_decryptSettings();
|
---|
2155 | return S_OK;
|
---|
2156 | }
|
---|
2157 |
|
---|
2158 | int VirtualBox::i_decryptMediumSettings(Medium *pMedium)
|
---|
2159 | {
|
---|
2160 | Bstr bstrCipher;
|
---|
2161 | HRESULT hrc = pMedium->GetProperty(Bstr("InitiatorSecretEncrypted").raw(),
|
---|
2162 | bstrCipher.asOutParam());
|
---|
2163 | if (SUCCEEDED(hrc))
|
---|
2164 | {
|
---|
2165 | Utf8Str strPlaintext;
|
---|
2166 | int rc = i_decryptSetting(&strPlaintext, bstrCipher);
|
---|
2167 | if (RT_SUCCESS(rc))
|
---|
2168 | pMedium->i_setPropertyDirect("InitiatorSecret", strPlaintext);
|
---|
2169 | else
|
---|
2170 | return rc;
|
---|
2171 | }
|
---|
2172 | return VINF_SUCCESS;
|
---|
2173 | }
|
---|
2174 |
|
---|
2175 | /**
|
---|
2176 | * Decrypt all encrypted settings.
|
---|
2177 | *
|
---|
2178 | * So far we only have encrypted iSCSI initiator secrets so we just go through
|
---|
2179 | * all hard disk mediums and determine the plain 'InitiatorSecret' from
|
---|
2180 | * 'InitiatorSecretEncrypted. The latter is stored as Base64 because medium
|
---|
2181 | * properties need to be null-terminated strings.
|
---|
2182 | */
|
---|
2183 | int VirtualBox::i_decryptSettings()
|
---|
2184 | {
|
---|
2185 | bool fFailure = false;
|
---|
2186 | AutoReadLock al(m->allHardDisks.getLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
2187 | for (MediaList::const_iterator mt = m->allHardDisks.begin();
|
---|
2188 | mt != m->allHardDisks.end();
|
---|
2189 | ++mt)
|
---|
2190 | {
|
---|
2191 | ComObjPtr<Medium> pMedium = *mt;
|
---|
2192 | AutoCaller medCaller(pMedium);
|
---|
2193 | if (FAILED(medCaller.rc()))
|
---|
2194 | continue;
|
---|
2195 | AutoWriteLock mlock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
2196 | int vrc = i_decryptMediumSettings(pMedium);
|
---|
2197 | if (RT_FAILURE(vrc))
|
---|
2198 | fFailure = true;
|
---|
2199 | }
|
---|
2200 | return fFailure ? VERR_INVALID_PARAMETER : VINF_SUCCESS;
|
---|
2201 | }
|
---|
2202 |
|
---|
2203 | /**
|
---|
2204 | * Encode.
|
---|
2205 | *
|
---|
2206 | * @param aPlaintext plaintext to be encrypted
|
---|
2207 | * @param aCiphertext resulting ciphertext (base64-encoded)
|
---|
2208 | */
|
---|
2209 | int VirtualBox::i_encryptSetting(const Utf8Str &aPlaintext, Utf8Str *aCiphertext)
|
---|
2210 | {
|
---|
2211 | uint8_t abCiphertext[32];
|
---|
2212 | char szCipherBase64[128];
|
---|
2213 | size_t cchCipherBase64;
|
---|
2214 | int rc = i_encryptSettingBytes((uint8_t*)aPlaintext.c_str(), abCiphertext,
|
---|
2215 | aPlaintext.length()+1, sizeof(abCiphertext));
|
---|
2216 | if (RT_SUCCESS(rc))
|
---|
2217 | {
|
---|
2218 | rc = RTBase64Encode(abCiphertext, sizeof(abCiphertext),
|
---|
2219 | szCipherBase64, sizeof(szCipherBase64),
|
---|
2220 | &cchCipherBase64);
|
---|
2221 | if (RT_SUCCESS(rc))
|
---|
2222 | *aCiphertext = szCipherBase64;
|
---|
2223 | }
|
---|
2224 | return rc;
|
---|
2225 | }
|
---|
2226 |
|
---|
2227 | /**
|
---|
2228 | * Decode.
|
---|
2229 | *
|
---|
2230 | * @param aPlaintext resulting plaintext
|
---|
2231 | * @param aCiphertext ciphertext (base64-encoded) to decrypt
|
---|
2232 | */
|
---|
2233 | int VirtualBox::i_decryptSetting(Utf8Str *aPlaintext, const Utf8Str &aCiphertext)
|
---|
2234 | {
|
---|
2235 | uint8_t abPlaintext[64];
|
---|
2236 | uint8_t abCiphertext[64];
|
---|
2237 | size_t cbCiphertext;
|
---|
2238 | int rc = RTBase64Decode(aCiphertext.c_str(),
|
---|
2239 | abCiphertext, sizeof(abCiphertext),
|
---|
2240 | &cbCiphertext, NULL);
|
---|
2241 | if (RT_SUCCESS(rc))
|
---|
2242 | {
|
---|
2243 | rc = i_decryptSettingBytes(abPlaintext, abCiphertext, cbCiphertext);
|
---|
2244 | if (RT_SUCCESS(rc))
|
---|
2245 | {
|
---|
2246 | for (unsigned i = 0; i < cbCiphertext; i++)
|
---|
2247 | {
|
---|
2248 | /* sanity check: null-terminated string? */
|
---|
2249 | if (abPlaintext[i] == '\0')
|
---|
2250 | {
|
---|
2251 | /* sanity check: valid UTF8 string? */
|
---|
2252 | if (RTStrIsValidEncoding((const char*)abPlaintext))
|
---|
2253 | {
|
---|
2254 | *aPlaintext = Utf8Str((const char*)abPlaintext);
|
---|
2255 | return VINF_SUCCESS;
|
---|
2256 | }
|
---|
2257 | }
|
---|
2258 | }
|
---|
2259 | rc = VERR_INVALID_MAGIC;
|
---|
2260 | }
|
---|
2261 | }
|
---|
2262 | return rc;
|
---|
2263 | }
|
---|
2264 |
|
---|
2265 | /**
|
---|
2266 | * Encrypt secret bytes. Use the m->SettingsCipherKey as key.
|
---|
2267 | *
|
---|
2268 | * @param aPlaintext clear text to be encrypted
|
---|
2269 | * @param aCiphertext resulting encrypted text
|
---|
2270 | * @param aPlaintextSize size of the plaintext
|
---|
2271 | * @param aCiphertextSize size of the ciphertext
|
---|
2272 | */
|
---|
2273 | int VirtualBox::i_encryptSettingBytes(const uint8_t *aPlaintext, uint8_t *aCiphertext,
|
---|
2274 | size_t aPlaintextSize, size_t aCiphertextSize) const
|
---|
2275 | {
|
---|
2276 | unsigned i, j;
|
---|
2277 | uint8_t aBytes[64];
|
---|
2278 |
|
---|
2279 | if (!m->fSettingsCipherKeySet)
|
---|
2280 | return VERR_INVALID_STATE;
|
---|
2281 |
|
---|
2282 | if (aCiphertextSize > sizeof(aBytes))
|
---|
2283 | return VERR_BUFFER_OVERFLOW;
|
---|
2284 |
|
---|
2285 | if (aCiphertextSize < 32)
|
---|
2286 | return VERR_INVALID_PARAMETER;
|
---|
2287 |
|
---|
2288 | AssertCompile(sizeof(m->SettingsCipherKey) >= 32);
|
---|
2289 |
|
---|
2290 | /* store the first 8 bytes of the cipherkey for verification */
|
---|
2291 | for (i = 0, j = 0; i < 8; i++, j++)
|
---|
2292 | aCiphertext[i] = m->SettingsCipherKey[j];
|
---|
2293 |
|
---|
2294 | for (unsigned k = 0; k < aPlaintextSize && i < aCiphertextSize; i++, k++)
|
---|
2295 | {
|
---|
2296 | aCiphertext[i] = (aPlaintext[k] ^ m->SettingsCipherKey[j]);
|
---|
2297 | if (++j >= sizeof(m->SettingsCipherKey))
|
---|
2298 | j = 0;
|
---|
2299 | }
|
---|
2300 |
|
---|
2301 | /* fill with random data to have a minimal length (salt) */
|
---|
2302 | if (i < aCiphertextSize)
|
---|
2303 | {
|
---|
2304 | RTRandBytes(aBytes, aCiphertextSize - i);
|
---|
2305 | for (int k = 0; i < aCiphertextSize; i++, k++)
|
---|
2306 | {
|
---|
2307 | aCiphertext[i] = aBytes[k] ^ m->SettingsCipherKey[j];
|
---|
2308 | if (++j >= sizeof(m->SettingsCipherKey))
|
---|
2309 | j = 0;
|
---|
2310 | }
|
---|
2311 | }
|
---|
2312 |
|
---|
2313 | return VINF_SUCCESS;
|
---|
2314 | }
|
---|
2315 |
|
---|
2316 | /**
|
---|
2317 | * Decrypt secret bytes. Use the m->SettingsCipherKey as key.
|
---|
2318 | *
|
---|
2319 | * @param aPlaintext resulting plaintext
|
---|
2320 | * @param aCiphertext ciphertext to be decrypted
|
---|
2321 | * @param aCiphertextSize size of the ciphertext == size of the plaintext
|
---|
2322 | */
|
---|
2323 | int VirtualBox::i_decryptSettingBytes(uint8_t *aPlaintext,
|
---|
2324 | const uint8_t *aCiphertext, size_t aCiphertextSize) const
|
---|
2325 | {
|
---|
2326 | unsigned i, j;
|
---|
2327 |
|
---|
2328 | if (!m->fSettingsCipherKeySet)
|
---|
2329 | return VERR_INVALID_STATE;
|
---|
2330 |
|
---|
2331 | if (aCiphertextSize < 32)
|
---|
2332 | return VERR_INVALID_PARAMETER;
|
---|
2333 |
|
---|
2334 | /* key verification */
|
---|
2335 | for (i = 0, j = 0; i < 8; i++, j++)
|
---|
2336 | if (aCiphertext[i] != m->SettingsCipherKey[j])
|
---|
2337 | return VERR_INVALID_MAGIC;
|
---|
2338 |
|
---|
2339 | /* poison */
|
---|
2340 | memset(aPlaintext, 0xff, aCiphertextSize);
|
---|
2341 | for (int k = 0; i < aCiphertextSize; i++, k++)
|
---|
2342 | {
|
---|
2343 | aPlaintext[k] = aCiphertext[i] ^ m->SettingsCipherKey[j];
|
---|
2344 | if (++j >= sizeof(m->SettingsCipherKey))
|
---|
2345 | j = 0;
|
---|
2346 | }
|
---|
2347 |
|
---|
2348 | return VINF_SUCCESS;
|
---|
2349 | }
|
---|
2350 |
|
---|
2351 | /**
|
---|
2352 | * Store a settings key.
|
---|
2353 | *
|
---|
2354 | * @param aKey the key to store
|
---|
2355 | */
|
---|
2356 | void VirtualBox::i_storeSettingsKey(const Utf8Str &aKey)
|
---|
2357 | {
|
---|
2358 | RTSha512(aKey.c_str(), aKey.length(), m->SettingsCipherKey);
|
---|
2359 | m->fSettingsCipherKeySet = true;
|
---|
2360 | }
|
---|
2361 |
|
---|
2362 | // public methods only for internal purposes
|
---|
2363 | /////////////////////////////////////////////////////////////////////////////
|
---|
2364 |
|
---|
2365 | #ifdef DEBUG
|
---|
2366 | void VirtualBox::i_dumpAllBackRefs()
|
---|
2367 | {
|
---|
2368 | {
|
---|
2369 | AutoReadLock al(m->allHardDisks.getLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
2370 | for (MediaList::const_iterator mt = m->allHardDisks.begin();
|
---|
2371 | mt != m->allHardDisks.end();
|
---|
2372 | ++mt)
|
---|
2373 | {
|
---|
2374 | ComObjPtr<Medium> pMedium = *mt;
|
---|
2375 | pMedium->i_dumpBackRefs();
|
---|
2376 | }
|
---|
2377 | }
|
---|
2378 | {
|
---|
2379 | AutoReadLock al(m->allDVDImages.getLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
2380 | for (MediaList::const_iterator mt = m->allDVDImages.begin();
|
---|
2381 | mt != m->allDVDImages.end();
|
---|
2382 | ++mt)
|
---|
2383 | {
|
---|
2384 | ComObjPtr<Medium> pMedium = *mt;
|
---|
2385 | pMedium->i_dumpBackRefs();
|
---|
2386 | }
|
---|
2387 | }
|
---|
2388 | }
|
---|
2389 | #endif
|
---|
2390 |
|
---|
2391 | /**
|
---|
2392 | * Posts an event to the event queue that is processed asynchronously
|
---|
2393 | * on a dedicated thread.
|
---|
2394 | *
|
---|
2395 | * Posting events to the dedicated event queue is useful to perform secondary
|
---|
2396 | * actions outside any object locks -- for example, to iterate over a list
|
---|
2397 | * of callbacks and inform them about some change caused by some object's
|
---|
2398 | * method call.
|
---|
2399 | *
|
---|
2400 | * @param event event to post; must have been allocated using |new|, will
|
---|
2401 | * be deleted automatically by the event thread after processing
|
---|
2402 | *
|
---|
2403 | * @note Doesn't lock any object.
|
---|
2404 | */
|
---|
2405 | HRESULT VirtualBox::i_postEvent(Event *event)
|
---|
2406 | {
|
---|
2407 | AssertReturn(event, E_FAIL);
|
---|
2408 |
|
---|
2409 | HRESULT rc;
|
---|
2410 | AutoCaller autoCaller(this);
|
---|
2411 | if (SUCCEEDED((rc = autoCaller.rc())))
|
---|
2412 | {
|
---|
2413 | if (getObjectState().getState() != ObjectState::Ready)
|
---|
2414 | Log1WarningFunc(("VirtualBox has been uninitialized (state=%d), the event is discarded!\n",
|
---|
2415 | getObjectState().getState()));
|
---|
2416 | // return S_OK
|
---|
2417 | else if ( (m->pAsyncEventQ)
|
---|
2418 | && (m->pAsyncEventQ->postEvent(event))
|
---|
2419 | )
|
---|
2420 | return S_OK;
|
---|
2421 | else
|
---|
2422 | rc = E_FAIL;
|
---|
2423 | }
|
---|
2424 |
|
---|
2425 | // in any event of failure, we must clean up here, or we'll leak;
|
---|
2426 | // the caller has allocated the object using new()
|
---|
2427 | delete event;
|
---|
2428 | return rc;
|
---|
2429 | }
|
---|
2430 |
|
---|
2431 | /**
|
---|
2432 | * Adds a progress to the global collection of pending operations.
|
---|
2433 | * Usually gets called upon progress object initialization.
|
---|
2434 | *
|
---|
2435 | * @param aProgress Operation to add to the collection.
|
---|
2436 | *
|
---|
2437 | * @note Doesn't lock objects.
|
---|
2438 | */
|
---|
2439 | HRESULT VirtualBox::i_addProgress(IProgress *aProgress)
|
---|
2440 | {
|
---|
2441 | CheckComArgNotNull(aProgress);
|
---|
2442 |
|
---|
2443 | AutoCaller autoCaller(this);
|
---|
2444 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2445 |
|
---|
2446 | Bstr id;
|
---|
2447 | HRESULT rc = aProgress->COMGETTER(Id)(id.asOutParam());
|
---|
2448 | AssertComRCReturnRC(rc);
|
---|
2449 |
|
---|
2450 | /* protect mProgressOperations */
|
---|
2451 | AutoWriteLock safeLock(m->mtxProgressOperations COMMA_LOCKVAL_SRC_POS);
|
---|
2452 |
|
---|
2453 | m->mapProgressOperations.insert(ProgressMap::value_type(Guid(id), aProgress));
|
---|
2454 | return S_OK;
|
---|
2455 | }
|
---|
2456 |
|
---|
2457 | /**
|
---|
2458 | * Removes the progress from the global collection of pending operations.
|
---|
2459 | * Usually gets called upon progress completion.
|
---|
2460 | *
|
---|
2461 | * @param aId UUID of the progress operation to remove
|
---|
2462 | *
|
---|
2463 | * @note Doesn't lock objects.
|
---|
2464 | */
|
---|
2465 | HRESULT VirtualBox::i_removeProgress(IN_GUID aId)
|
---|
2466 | {
|
---|
2467 | AutoCaller autoCaller(this);
|
---|
2468 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2469 |
|
---|
2470 | ComPtr<IProgress> progress;
|
---|
2471 |
|
---|
2472 | /* protect mProgressOperations */
|
---|
2473 | AutoWriteLock safeLock(m->mtxProgressOperations COMMA_LOCKVAL_SRC_POS);
|
---|
2474 |
|
---|
2475 | size_t cnt = m->mapProgressOperations.erase(aId);
|
---|
2476 | Assert(cnt == 1);
|
---|
2477 | NOREF(cnt);
|
---|
2478 |
|
---|
2479 | return S_OK;
|
---|
2480 | }
|
---|
2481 |
|
---|
2482 | #ifdef RT_OS_WINDOWS
|
---|
2483 |
|
---|
2484 | class StartSVCHelperClientData : public ThreadTask
|
---|
2485 | {
|
---|
2486 | public:
|
---|
2487 | StartSVCHelperClientData()
|
---|
2488 | {
|
---|
2489 | LogFlowFuncEnter();
|
---|
2490 | m_strTaskName = "SVCHelper";
|
---|
2491 | threadVoidData = NULL;
|
---|
2492 | initialized = false;
|
---|
2493 | }
|
---|
2494 |
|
---|
2495 | virtual ~StartSVCHelperClientData()
|
---|
2496 | {
|
---|
2497 | LogFlowFuncEnter();
|
---|
2498 | if (threadVoidData!=NULL)
|
---|
2499 | {
|
---|
2500 | delete threadVoidData;
|
---|
2501 | threadVoidData=NULL;
|
---|
2502 | }
|
---|
2503 | };
|
---|
2504 |
|
---|
2505 | void handler()
|
---|
2506 | {
|
---|
2507 | VirtualBox::i_SVCHelperClientThreadTask(this);
|
---|
2508 | }
|
---|
2509 |
|
---|
2510 | const ComPtr<Progress>& GetProgressObject() const {return progress;}
|
---|
2511 |
|
---|
2512 | bool init(VirtualBox* aVbox,
|
---|
2513 | Progress* aProgress,
|
---|
2514 | bool aPrivileged,
|
---|
2515 | VirtualBox::SVCHelperClientFunc aFunc,
|
---|
2516 | void *aUser)
|
---|
2517 | {
|
---|
2518 | LogFlowFuncEnter();
|
---|
2519 | that = aVbox;
|
---|
2520 | progress = aProgress;
|
---|
2521 | privileged = aPrivileged;
|
---|
2522 | func = aFunc;
|
---|
2523 | user = aUser;
|
---|
2524 |
|
---|
2525 | initThreadVoidData();
|
---|
2526 |
|
---|
2527 | initialized = true;
|
---|
2528 |
|
---|
2529 | return initialized;
|
---|
2530 | }
|
---|
2531 |
|
---|
2532 | bool isOk() const{ return initialized;}
|
---|
2533 |
|
---|
2534 | bool initialized;
|
---|
2535 | ComObjPtr<VirtualBox> that;
|
---|
2536 | ComObjPtr<Progress> progress;
|
---|
2537 | bool privileged;
|
---|
2538 | VirtualBox::SVCHelperClientFunc func;
|
---|
2539 | void *user;
|
---|
2540 | ThreadVoidData *threadVoidData;
|
---|
2541 |
|
---|
2542 | private:
|
---|
2543 | bool initThreadVoidData()
|
---|
2544 | {
|
---|
2545 | LogFlowFuncEnter();
|
---|
2546 | threadVoidData = static_cast<ThreadVoidData*>(user);
|
---|
2547 | return true;
|
---|
2548 | }
|
---|
2549 | };
|
---|
2550 |
|
---|
2551 | /**
|
---|
2552 | * Helper method that starts a worker thread that:
|
---|
2553 | * - creates a pipe communication channel using SVCHlpClient;
|
---|
2554 | * - starts an SVC Helper process that will inherit this channel;
|
---|
2555 | * - executes the supplied function by passing it the created SVCHlpClient
|
---|
2556 | * and opened instance to communicate to the Helper process and the given
|
---|
2557 | * Progress object.
|
---|
2558 | *
|
---|
2559 | * The user function is supposed to communicate to the helper process
|
---|
2560 | * using the \a aClient argument to do the requested job and optionally expose
|
---|
2561 | * the progress through the \a aProgress object. The user function should never
|
---|
2562 | * call notifyComplete() on it: this will be done automatically using the
|
---|
2563 | * result code returned by the function.
|
---|
2564 | *
|
---|
2565 | * Before the user function is started, the communication channel passed to
|
---|
2566 | * the \a aClient argument is fully set up, the function should start using
|
---|
2567 | * its write() and read() methods directly.
|
---|
2568 | *
|
---|
2569 | * The \a aVrc parameter of the user function may be used to return an error
|
---|
2570 | * code if it is related to communication errors (for example, returned by
|
---|
2571 | * the SVCHlpClient members when they fail). In this case, the correct error
|
---|
2572 | * message using this value will be reported to the caller. Note that the
|
---|
2573 | * value of \a aVrc is inspected only if the user function itself returns
|
---|
2574 | * success.
|
---|
2575 | *
|
---|
2576 | * If a failure happens anywhere before the user function would be normally
|
---|
2577 | * called, it will be called anyway in special "cleanup only" mode indicated
|
---|
2578 | * by \a aClient, \a aProgress and \aVrc arguments set to NULL. In this mode,
|
---|
2579 | * all the function is supposed to do is to cleanup its aUser argument if
|
---|
2580 | * necessary (it's assumed that the ownership of this argument is passed to
|
---|
2581 | * the user function once #startSVCHelperClient() returns a success, thus
|
---|
2582 | * making it responsible for the cleanup).
|
---|
2583 | *
|
---|
2584 | * After the user function returns, the thread will send the SVCHlpMsg::Null
|
---|
2585 | * message to indicate a process termination.
|
---|
2586 | *
|
---|
2587 | * @param aPrivileged |true| to start the SVC Helper process as a privileged
|
---|
2588 | * user that can perform administrative tasks
|
---|
2589 | * @param aFunc user function to run
|
---|
2590 | * @param aUser argument to the user function
|
---|
2591 | * @param aProgress progress object that will track operation completion
|
---|
2592 | *
|
---|
2593 | * @note aPrivileged is currently ignored (due to some unsolved problems in
|
---|
2594 | * Vista) and the process will be started as a normal (unprivileged)
|
---|
2595 | * process.
|
---|
2596 | *
|
---|
2597 | * @note Doesn't lock anything.
|
---|
2598 | */
|
---|
2599 | HRESULT VirtualBox::i_startSVCHelperClient(bool aPrivileged,
|
---|
2600 | SVCHelperClientFunc aFunc,
|
---|
2601 | void *aUser, Progress *aProgress)
|
---|
2602 | {
|
---|
2603 | LogFlowFuncEnter();
|
---|
2604 | AssertReturn(aFunc, E_POINTER);
|
---|
2605 | AssertReturn(aProgress, E_POINTER);
|
---|
2606 |
|
---|
2607 | AutoCaller autoCaller(this);
|
---|
2608 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2609 |
|
---|
2610 | /* create the i_SVCHelperClientThreadTask() argument */
|
---|
2611 |
|
---|
2612 | HRESULT hr = S_OK;
|
---|
2613 | StartSVCHelperClientData *pTask = NULL;
|
---|
2614 | try
|
---|
2615 | {
|
---|
2616 | pTask = new StartSVCHelperClientData();
|
---|
2617 |
|
---|
2618 | pTask->init(this, aProgress, aPrivileged, aFunc, aUser);
|
---|
2619 |
|
---|
2620 | if (!pTask->isOk())
|
---|
2621 | {
|
---|
2622 | delete pTask;
|
---|
2623 | LogRel(("Could not init StartSVCHelperClientData object \n"));
|
---|
2624 | throw E_FAIL;
|
---|
2625 | }
|
---|
2626 |
|
---|
2627 | //this function delete pTask in case of exceptions, so there is no need in the call of delete operator
|
---|
2628 | hr = pTask->createThreadWithType(RTTHREADTYPE_MAIN_WORKER);
|
---|
2629 |
|
---|
2630 | }
|
---|
2631 | catch(std::bad_alloc &)
|
---|
2632 | {
|
---|
2633 | hr = setError(E_OUTOFMEMORY);
|
---|
2634 | }
|
---|
2635 | catch(...)
|
---|
2636 | {
|
---|
2637 | LogRel(("Could not create thread for StartSVCHelperClientData \n"));
|
---|
2638 | hr = E_FAIL;
|
---|
2639 | }
|
---|
2640 |
|
---|
2641 | return hr;
|
---|
2642 | }
|
---|
2643 |
|
---|
2644 | /**
|
---|
2645 | * Worker thread for startSVCHelperClient().
|
---|
2646 | */
|
---|
2647 | /* static */
|
---|
2648 | void VirtualBox::i_SVCHelperClientThreadTask(StartSVCHelperClientData *pTask)
|
---|
2649 | {
|
---|
2650 | LogFlowFuncEnter();
|
---|
2651 | HRESULT rc = S_OK;
|
---|
2652 | bool userFuncCalled = false;
|
---|
2653 |
|
---|
2654 | do
|
---|
2655 | {
|
---|
2656 | AssertBreakStmt(pTask, rc = E_POINTER);
|
---|
2657 | AssertReturnVoid(!pTask->progress.isNull());
|
---|
2658 |
|
---|
2659 | /* protect VirtualBox from uninitialization */
|
---|
2660 | AutoCaller autoCaller(pTask->that);
|
---|
2661 | if (!autoCaller.isOk())
|
---|
2662 | {
|
---|
2663 | /* it's too late */
|
---|
2664 | rc = autoCaller.rc();
|
---|
2665 | break;
|
---|
2666 | }
|
---|
2667 |
|
---|
2668 | int vrc = VINF_SUCCESS;
|
---|
2669 |
|
---|
2670 | Guid id;
|
---|
2671 | id.create();
|
---|
2672 | SVCHlpClient client;
|
---|
2673 | vrc = client.create(Utf8StrFmt("VirtualBox\\SVCHelper\\{%RTuuid}",
|
---|
2674 | id.raw()).c_str());
|
---|
2675 | if (RT_FAILURE(vrc))
|
---|
2676 | {
|
---|
2677 | rc = pTask->that->setErrorBoth(E_FAIL, vrc, tr("Could not create the communication channel (%Rrc)"), vrc);
|
---|
2678 | break;
|
---|
2679 | }
|
---|
2680 |
|
---|
2681 | /* get the path to the executable */
|
---|
2682 | char exePathBuf[RTPATH_MAX];
|
---|
2683 | char *exePath = RTProcGetExecutablePath(exePathBuf, RTPATH_MAX);
|
---|
2684 | if (!exePath)
|
---|
2685 | {
|
---|
2686 | rc = pTask->that->setError(E_FAIL, tr("Cannot get executable name"));
|
---|
2687 | break;
|
---|
2688 | }
|
---|
2689 |
|
---|
2690 | Utf8Str argsStr = Utf8StrFmt("/Helper %s", client.name().c_str());
|
---|
2691 |
|
---|
2692 | LogFlowFunc(("Starting '\"%s\" %s'...\n", exePath, argsStr.c_str()));
|
---|
2693 |
|
---|
2694 | RTPROCESS pid = NIL_RTPROCESS;
|
---|
2695 |
|
---|
2696 | if (pTask->privileged)
|
---|
2697 | {
|
---|
2698 | /* Attempt to start a privileged process using the Run As dialog */
|
---|
2699 |
|
---|
2700 | Bstr file = exePath;
|
---|
2701 | Bstr parameters = argsStr;
|
---|
2702 |
|
---|
2703 | SHELLEXECUTEINFO shExecInfo;
|
---|
2704 |
|
---|
2705 | shExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
|
---|
2706 |
|
---|
2707 | shExecInfo.fMask = NULL;
|
---|
2708 | shExecInfo.hwnd = NULL;
|
---|
2709 | shExecInfo.lpVerb = L"runas";
|
---|
2710 | shExecInfo.lpFile = file.raw();
|
---|
2711 | shExecInfo.lpParameters = parameters.raw();
|
---|
2712 | shExecInfo.lpDirectory = NULL;
|
---|
2713 | shExecInfo.nShow = SW_NORMAL;
|
---|
2714 | shExecInfo.hInstApp = NULL;
|
---|
2715 |
|
---|
2716 | if (!ShellExecuteEx(&shExecInfo))
|
---|
2717 | {
|
---|
2718 | int vrc2 = RTErrConvertFromWin32(GetLastError());
|
---|
2719 | /* hide excessive details in case of a frequent error
|
---|
2720 | * (pressing the Cancel button to close the Run As dialog) */
|
---|
2721 | if (vrc2 == VERR_CANCELLED)
|
---|
2722 | rc = pTask->that->setErrorBoth(E_FAIL, vrc, tr("Operation canceled by the user"));
|
---|
2723 | else
|
---|
2724 | rc = pTask->that->setErrorBoth(E_FAIL, vrc, tr("Could not launch a privileged process '%s' (%Rrc)"), exePath, vrc2);
|
---|
2725 | break;
|
---|
2726 | }
|
---|
2727 | }
|
---|
2728 | else
|
---|
2729 | {
|
---|
2730 | const char *args[] = { exePath, "/Helper", client.name().c_str(), 0 };
|
---|
2731 | vrc = RTProcCreate(exePath, args, RTENV_DEFAULT, 0, &pid);
|
---|
2732 | if (RT_FAILURE(vrc))
|
---|
2733 | {
|
---|
2734 | rc = pTask->that->setErrorBoth(E_FAIL, vrc, tr("Could not launch a process '%s' (%Rrc)"), exePath, vrc);
|
---|
2735 | break;
|
---|
2736 | }
|
---|
2737 | }
|
---|
2738 |
|
---|
2739 | /* wait for the client to connect */
|
---|
2740 | vrc = client.connect();
|
---|
2741 | if (RT_SUCCESS(vrc))
|
---|
2742 | {
|
---|
2743 | /* start the user supplied function */
|
---|
2744 | rc = pTask->func(&client, pTask->progress, pTask->user, &vrc);
|
---|
2745 | userFuncCalled = true;
|
---|
2746 | }
|
---|
2747 |
|
---|
2748 | /* send the termination signal to the process anyway */
|
---|
2749 | {
|
---|
2750 | int vrc2 = client.write(SVCHlpMsg::Null);
|
---|
2751 | if (RT_SUCCESS(vrc))
|
---|
2752 | vrc = vrc2;
|
---|
2753 | }
|
---|
2754 |
|
---|
2755 | if (SUCCEEDED(rc) && RT_FAILURE(vrc))
|
---|
2756 | {
|
---|
2757 | rc = pTask->that->setErrorBoth(E_FAIL, vrc, tr("Could not operate the communication channel (%Rrc)"), vrc);
|
---|
2758 | break;
|
---|
2759 | }
|
---|
2760 | }
|
---|
2761 | while (0);
|
---|
2762 |
|
---|
2763 | if (FAILED(rc) && !userFuncCalled)
|
---|
2764 | {
|
---|
2765 | /* call the user function in the "cleanup only" mode
|
---|
2766 | * to let it free resources passed to in aUser */
|
---|
2767 | pTask->func(NULL, NULL, pTask->user, NULL);
|
---|
2768 | }
|
---|
2769 |
|
---|
2770 | pTask->progress->i_notifyComplete(rc);
|
---|
2771 |
|
---|
2772 | LogFlowFuncLeave();
|
---|
2773 | }
|
---|
2774 |
|
---|
2775 | #endif /* RT_OS_WINDOWS */
|
---|
2776 |
|
---|
2777 | /**
|
---|
2778 | * Sends a signal to the client watcher to rescan the set of machines
|
---|
2779 | * that have open sessions.
|
---|
2780 | *
|
---|
2781 | * @note Doesn't lock anything.
|
---|
2782 | */
|
---|
2783 | void VirtualBox::i_updateClientWatcher()
|
---|
2784 | {
|
---|
2785 | AutoCaller autoCaller(this);
|
---|
2786 | AssertComRCReturnVoid(autoCaller.rc());
|
---|
2787 |
|
---|
2788 | AssertPtrReturnVoid(m->pClientWatcher);
|
---|
2789 | m->pClientWatcher->update();
|
---|
2790 | }
|
---|
2791 |
|
---|
2792 | /**
|
---|
2793 | * Adds the given child process ID to the list of processes to be reaped.
|
---|
2794 | * This call should be followed by #i_updateClientWatcher() to take the effect.
|
---|
2795 | *
|
---|
2796 | * @note Doesn't lock anything.
|
---|
2797 | */
|
---|
2798 | void VirtualBox::i_addProcessToReap(RTPROCESS pid)
|
---|
2799 | {
|
---|
2800 | AutoCaller autoCaller(this);
|
---|
2801 | AssertComRCReturnVoid(autoCaller.rc());
|
---|
2802 |
|
---|
2803 | AssertPtrReturnVoid(m->pClientWatcher);
|
---|
2804 | m->pClientWatcher->addProcess(pid);
|
---|
2805 | }
|
---|
2806 |
|
---|
2807 | /** Event for onMachineStateChange(), onMachineDataChange(), onMachineRegistered() */
|
---|
2808 | struct MachineEvent : public VirtualBox::CallbackEvent
|
---|
2809 | {
|
---|
2810 | MachineEvent(VirtualBox *aVB, VBoxEventType_T aWhat, const Guid &aId, BOOL aBool)
|
---|
2811 | : CallbackEvent(aVB, aWhat), id(aId.toUtf16())
|
---|
2812 | , mBool(aBool)
|
---|
2813 | { }
|
---|
2814 |
|
---|
2815 | MachineEvent(VirtualBox *aVB, VBoxEventType_T aWhat, const Guid &aId, MachineState_T aState)
|
---|
2816 | : CallbackEvent(aVB, aWhat), id(aId.toUtf16())
|
---|
2817 | , mState(aState)
|
---|
2818 | {}
|
---|
2819 |
|
---|
2820 | virtual HRESULT prepareEventDesc(IEventSource* aSource, VBoxEventDesc& aEvDesc)
|
---|
2821 | {
|
---|
2822 | switch (mWhat)
|
---|
2823 | {
|
---|
2824 | case VBoxEventType_OnMachineDataChanged:
|
---|
2825 | aEvDesc.init(aSource, mWhat, id.raw(), mBool);
|
---|
2826 | break;
|
---|
2827 |
|
---|
2828 | case VBoxEventType_OnMachineStateChanged:
|
---|
2829 | aEvDesc.init(aSource, mWhat, id.raw(), mState);
|
---|
2830 | break;
|
---|
2831 |
|
---|
2832 | case VBoxEventType_OnMachineRegistered:
|
---|
2833 | aEvDesc.init(aSource, mWhat, id.raw(), mBool);
|
---|
2834 | break;
|
---|
2835 |
|
---|
2836 | default:
|
---|
2837 | AssertFailedReturn(S_OK);
|
---|
2838 | }
|
---|
2839 | return S_OK;
|
---|
2840 | }
|
---|
2841 |
|
---|
2842 | Bstr id;
|
---|
2843 | MachineState_T mState;
|
---|
2844 | BOOL mBool;
|
---|
2845 | };
|
---|
2846 |
|
---|
2847 |
|
---|
2848 | /**
|
---|
2849 | * VD plugin load
|
---|
2850 | */
|
---|
2851 | int VirtualBox::i_loadVDPlugin(const char *pszPluginLibrary)
|
---|
2852 | {
|
---|
2853 | return m->pSystemProperties->i_loadVDPlugin(pszPluginLibrary);
|
---|
2854 | }
|
---|
2855 |
|
---|
2856 | /**
|
---|
2857 | * VD plugin unload
|
---|
2858 | */
|
---|
2859 | int VirtualBox::i_unloadVDPlugin(const char *pszPluginLibrary)
|
---|
2860 | {
|
---|
2861 | return m->pSystemProperties->i_unloadVDPlugin(pszPluginLibrary);
|
---|
2862 | }
|
---|
2863 |
|
---|
2864 |
|
---|
2865 | /**
|
---|
2866 | * @note Doesn't lock any object.
|
---|
2867 | */
|
---|
2868 | void VirtualBox::i_onMachineStateChange(const Guid &aId, MachineState_T aState)
|
---|
2869 | {
|
---|
2870 | i_postEvent(new MachineEvent(this, VBoxEventType_OnMachineStateChanged, aId, aState));
|
---|
2871 | }
|
---|
2872 |
|
---|
2873 | /**
|
---|
2874 | * @note Doesn't lock any object.
|
---|
2875 | */
|
---|
2876 | void VirtualBox::i_onMachineDataChange(const Guid &aId, BOOL aTemporary)
|
---|
2877 | {
|
---|
2878 | i_postEvent(new MachineEvent(this, VBoxEventType_OnMachineDataChanged, aId, aTemporary));
|
---|
2879 | }
|
---|
2880 |
|
---|
2881 | /**
|
---|
2882 | * @note Locks this object for reading.
|
---|
2883 | */
|
---|
2884 | BOOL VirtualBox::i_onExtraDataCanChange(const Guid &aId, IN_BSTR aKey, IN_BSTR aValue,
|
---|
2885 | Bstr &aError)
|
---|
2886 | {
|
---|
2887 | LogFlowThisFunc(("machine={%s} aKey={%ls} aValue={%ls}\n",
|
---|
2888 | aId.toString().c_str(), aKey, aValue));
|
---|
2889 |
|
---|
2890 | AutoCaller autoCaller(this);
|
---|
2891 | AssertComRCReturn(autoCaller.rc(), FALSE);
|
---|
2892 |
|
---|
2893 | BOOL allowChange = TRUE;
|
---|
2894 | Bstr id = aId.toUtf16();
|
---|
2895 |
|
---|
2896 | VBoxEventDesc evDesc;
|
---|
2897 | evDesc.init(m->pEventSource, VBoxEventType_OnExtraDataCanChange, id.raw(), aKey, aValue);
|
---|
2898 | BOOL fDelivered = evDesc.fire(3000); /* Wait up to 3 secs for delivery */
|
---|
2899 | //Assert(fDelivered);
|
---|
2900 | if (fDelivered)
|
---|
2901 | {
|
---|
2902 | ComPtr<IEvent> aEvent;
|
---|
2903 | evDesc.getEvent(aEvent.asOutParam());
|
---|
2904 | ComPtr<IExtraDataCanChangeEvent> aCanChangeEvent = aEvent;
|
---|
2905 | Assert(aCanChangeEvent);
|
---|
2906 | BOOL fVetoed = FALSE;
|
---|
2907 | aCanChangeEvent->IsVetoed(&fVetoed);
|
---|
2908 | allowChange = !fVetoed;
|
---|
2909 |
|
---|
2910 | if (!allowChange)
|
---|
2911 | {
|
---|
2912 | SafeArray<BSTR> aVetos;
|
---|
2913 | aCanChangeEvent->GetVetos(ComSafeArrayAsOutParam(aVetos));
|
---|
2914 | if (aVetos.size() > 0)
|
---|
2915 | aError = aVetos[0];
|
---|
2916 | }
|
---|
2917 | }
|
---|
2918 | else
|
---|
2919 | allowChange = TRUE;
|
---|
2920 |
|
---|
2921 | LogFlowThisFunc(("allowChange=%RTbool\n", allowChange));
|
---|
2922 | return allowChange;
|
---|
2923 | }
|
---|
2924 |
|
---|
2925 | /** Event for onExtraDataChange() */
|
---|
2926 | struct ExtraDataEvent : public VirtualBox::CallbackEvent
|
---|
2927 | {
|
---|
2928 | ExtraDataEvent(VirtualBox *aVB, const Guid &aMachineId,
|
---|
2929 | IN_BSTR aKey, IN_BSTR aVal)
|
---|
2930 | : CallbackEvent(aVB, VBoxEventType_OnExtraDataChanged)
|
---|
2931 | , machineId(aMachineId.toUtf16()), key(aKey), val(aVal)
|
---|
2932 | {}
|
---|
2933 |
|
---|
2934 | virtual HRESULT prepareEventDesc(IEventSource* aSource, VBoxEventDesc& aEvDesc)
|
---|
2935 | {
|
---|
2936 | return aEvDesc.init(aSource, VBoxEventType_OnExtraDataChanged, machineId.raw(), key.raw(), val.raw());
|
---|
2937 | }
|
---|
2938 |
|
---|
2939 | Bstr machineId, key, val;
|
---|
2940 | };
|
---|
2941 |
|
---|
2942 | /**
|
---|
2943 | * @note Doesn't lock any object.
|
---|
2944 | */
|
---|
2945 | void VirtualBox::i_onExtraDataChange(const Guid &aId, IN_BSTR aKey, IN_BSTR aValue)
|
---|
2946 | {
|
---|
2947 | i_postEvent(new ExtraDataEvent(this, aId, aKey, aValue));
|
---|
2948 | }
|
---|
2949 |
|
---|
2950 | /**
|
---|
2951 | * @note Doesn't lock any object.
|
---|
2952 | */
|
---|
2953 | void VirtualBox::i_onMachineRegistered(const Guid &aId, BOOL aRegistered)
|
---|
2954 | {
|
---|
2955 | i_postEvent(new MachineEvent(this, VBoxEventType_OnMachineRegistered, aId, aRegistered));
|
---|
2956 | }
|
---|
2957 |
|
---|
2958 | /** Event for onSessionStateChange() */
|
---|
2959 | struct SessionEvent : public VirtualBox::CallbackEvent
|
---|
2960 | {
|
---|
2961 | SessionEvent(VirtualBox *aVB, const Guid &aMachineId, SessionState_T aState)
|
---|
2962 | : CallbackEvent(aVB, VBoxEventType_OnSessionStateChanged)
|
---|
2963 | , machineId(aMachineId.toUtf16()), sessionState(aState)
|
---|
2964 | {}
|
---|
2965 |
|
---|
2966 | virtual HRESULT prepareEventDesc(IEventSource* aSource, VBoxEventDesc& aEvDesc)
|
---|
2967 | {
|
---|
2968 | return aEvDesc.init(aSource, VBoxEventType_OnSessionStateChanged, machineId.raw(), sessionState);
|
---|
2969 | }
|
---|
2970 | Bstr machineId;
|
---|
2971 | SessionState_T sessionState;
|
---|
2972 | };
|
---|
2973 |
|
---|
2974 | /**
|
---|
2975 | * @note Doesn't lock any object.
|
---|
2976 | */
|
---|
2977 | void VirtualBox::i_onSessionStateChange(const Guid &aId, SessionState_T aState)
|
---|
2978 | {
|
---|
2979 | i_postEvent(new SessionEvent(this, aId, aState));
|
---|
2980 | }
|
---|
2981 |
|
---|
2982 | /** Event for i_onSnapshotTaken(), i_onSnapshotDeleted(), i_onSnapshotRestored() and i_onSnapshotChange() */
|
---|
2983 | struct SnapshotEvent : public VirtualBox::CallbackEvent
|
---|
2984 | {
|
---|
2985 | SnapshotEvent(VirtualBox *aVB, const Guid &aMachineId, const Guid &aSnapshotId,
|
---|
2986 | VBoxEventType_T aWhat)
|
---|
2987 | : CallbackEvent(aVB, aWhat)
|
---|
2988 | , machineId(aMachineId), snapshotId(aSnapshotId)
|
---|
2989 | {}
|
---|
2990 |
|
---|
2991 | virtual HRESULT prepareEventDesc(IEventSource* aSource, VBoxEventDesc& aEvDesc)
|
---|
2992 | {
|
---|
2993 | return aEvDesc.init(aSource, mWhat, machineId.toUtf16().raw(),
|
---|
2994 | snapshotId.toUtf16().raw());
|
---|
2995 | }
|
---|
2996 |
|
---|
2997 | Guid machineId;
|
---|
2998 | Guid snapshotId;
|
---|
2999 | };
|
---|
3000 |
|
---|
3001 | /**
|
---|
3002 | * @note Doesn't lock any object.
|
---|
3003 | */
|
---|
3004 | void VirtualBox::i_onSnapshotTaken(const Guid &aMachineId, const Guid &aSnapshotId)
|
---|
3005 | {
|
---|
3006 | i_postEvent(new SnapshotEvent(this, aMachineId, aSnapshotId,
|
---|
3007 | VBoxEventType_OnSnapshotTaken));
|
---|
3008 | }
|
---|
3009 |
|
---|
3010 | /**
|
---|
3011 | * @note Doesn't lock any object.
|
---|
3012 | */
|
---|
3013 | void VirtualBox::i_onSnapshotDeleted(const Guid &aMachineId, const Guid &aSnapshotId)
|
---|
3014 | {
|
---|
3015 | i_postEvent(new SnapshotEvent(this, aMachineId, aSnapshotId,
|
---|
3016 | VBoxEventType_OnSnapshotDeleted));
|
---|
3017 | }
|
---|
3018 |
|
---|
3019 | /**
|
---|
3020 | * @note Doesn't lock any object.
|
---|
3021 | */
|
---|
3022 | void VirtualBox::i_onSnapshotRestored(const Guid &aMachineId, const Guid &aSnapshotId)
|
---|
3023 | {
|
---|
3024 | i_postEvent(new SnapshotEvent(this, aMachineId, aSnapshotId,
|
---|
3025 | VBoxEventType_OnSnapshotRestored));
|
---|
3026 | }
|
---|
3027 |
|
---|
3028 | /**
|
---|
3029 | * @note Doesn't lock any object.
|
---|
3030 | */
|
---|
3031 | void VirtualBox::i_onSnapshotChange(const Guid &aMachineId, const Guid &aSnapshotId)
|
---|
3032 | {
|
---|
3033 | i_postEvent(new SnapshotEvent(this, aMachineId, aSnapshotId,
|
---|
3034 | VBoxEventType_OnSnapshotChanged));
|
---|
3035 | }
|
---|
3036 |
|
---|
3037 | /** Event for onGuestPropertyChange() */
|
---|
3038 | struct GuestPropertyEvent : public VirtualBox::CallbackEvent
|
---|
3039 | {
|
---|
3040 | GuestPropertyEvent(VirtualBox *aVBox, const Guid &aMachineId,
|
---|
3041 | IN_BSTR aName, IN_BSTR aValue, IN_BSTR aFlags)
|
---|
3042 | : CallbackEvent(aVBox, VBoxEventType_OnGuestPropertyChanged),
|
---|
3043 | machineId(aMachineId),
|
---|
3044 | name(aName),
|
---|
3045 | value(aValue),
|
---|
3046 | flags(aFlags)
|
---|
3047 | {}
|
---|
3048 |
|
---|
3049 | virtual HRESULT prepareEventDesc(IEventSource* aSource, VBoxEventDesc& aEvDesc)
|
---|
3050 | {
|
---|
3051 | return aEvDesc.init(aSource, VBoxEventType_OnGuestPropertyChanged,
|
---|
3052 | machineId.toUtf16().raw(), name.raw(), value.raw(), flags.raw());
|
---|
3053 | }
|
---|
3054 |
|
---|
3055 | Guid machineId;
|
---|
3056 | Bstr name, value, flags;
|
---|
3057 | };
|
---|
3058 |
|
---|
3059 | /**
|
---|
3060 | * @note Doesn't lock any object.
|
---|
3061 | */
|
---|
3062 | void VirtualBox::i_onGuestPropertyChange(const Guid &aMachineId, IN_BSTR aName,
|
---|
3063 | IN_BSTR aValue, IN_BSTR aFlags)
|
---|
3064 | {
|
---|
3065 | i_postEvent(new GuestPropertyEvent(this, aMachineId, aName, aValue, aFlags));
|
---|
3066 | }
|
---|
3067 |
|
---|
3068 | /**
|
---|
3069 | * @note Doesn't lock any object.
|
---|
3070 | */
|
---|
3071 | void VirtualBox::i_onNatRedirectChange(const Guid &aMachineId, ULONG ulSlot, bool fRemove, IN_BSTR aName,
|
---|
3072 | NATProtocol_T aProto, IN_BSTR aHostIp, uint16_t aHostPort,
|
---|
3073 | IN_BSTR aGuestIp, uint16_t aGuestPort)
|
---|
3074 | {
|
---|
3075 | fireNATRedirectEvent(m->pEventSource, aMachineId.toUtf16().raw(), ulSlot, fRemove, aName, aProto, aHostIp,
|
---|
3076 | aHostPort, aGuestIp, aGuestPort);
|
---|
3077 | }
|
---|
3078 |
|
---|
3079 | void VirtualBox::i_onNATNetworkChange(IN_BSTR aName)
|
---|
3080 | {
|
---|
3081 | fireNATNetworkChangedEvent(m->pEventSource, aName);
|
---|
3082 | }
|
---|
3083 |
|
---|
3084 | void VirtualBox::i_onNATNetworkStartStop(IN_BSTR aName, BOOL fStart)
|
---|
3085 | {
|
---|
3086 | fireNATNetworkStartStopEvent(m->pEventSource, aName, fStart);
|
---|
3087 | }
|
---|
3088 |
|
---|
3089 | void VirtualBox::i_onNATNetworkSetting(IN_BSTR aNetworkName, BOOL aEnabled,
|
---|
3090 | IN_BSTR aNetwork, IN_BSTR aGateway,
|
---|
3091 | BOOL aAdvertiseDefaultIpv6RouteEnabled,
|
---|
3092 | BOOL fNeedDhcpServer)
|
---|
3093 | {
|
---|
3094 | fireNATNetworkSettingEvent(m->pEventSource, aNetworkName, aEnabled,
|
---|
3095 | aNetwork, aGateway,
|
---|
3096 | aAdvertiseDefaultIpv6RouteEnabled, fNeedDhcpServer);
|
---|
3097 | }
|
---|
3098 |
|
---|
3099 | void VirtualBox::i_onNATNetworkPortForward(IN_BSTR aNetworkName, BOOL create, BOOL fIpv6,
|
---|
3100 | IN_BSTR aRuleName, NATProtocol_T proto,
|
---|
3101 | IN_BSTR aHostIp, LONG aHostPort,
|
---|
3102 | IN_BSTR aGuestIp, LONG aGuestPort)
|
---|
3103 | {
|
---|
3104 | fireNATNetworkPortForwardEvent(m->pEventSource, aNetworkName, create,
|
---|
3105 | fIpv6, aRuleName, proto,
|
---|
3106 | aHostIp, aHostPort,
|
---|
3107 | aGuestIp, aGuestPort);
|
---|
3108 | }
|
---|
3109 |
|
---|
3110 |
|
---|
3111 | void VirtualBox::i_onHostNameResolutionConfigurationChange()
|
---|
3112 | {
|
---|
3113 | if (m->pEventSource)
|
---|
3114 | fireHostNameResolutionConfigurationChangeEvent(m->pEventSource);
|
---|
3115 | }
|
---|
3116 |
|
---|
3117 |
|
---|
3118 | int VirtualBox::i_natNetworkRefInc(const Utf8Str &aNetworkName)
|
---|
3119 | {
|
---|
3120 | AutoWriteLock safeLock(*spMtxNatNetworkNameToRefCountLock COMMA_LOCKVAL_SRC_POS);
|
---|
3121 |
|
---|
3122 | if (!sNatNetworkNameToRefCount[aNetworkName])
|
---|
3123 | {
|
---|
3124 | ComPtr<INATNetwork> nat;
|
---|
3125 | HRESULT rc = findNATNetworkByName(aNetworkName, nat);
|
---|
3126 | if (FAILED(rc)) return -1;
|
---|
3127 |
|
---|
3128 | rc = nat->Start(Bstr("whatever").raw());
|
---|
3129 | if (SUCCEEDED(rc))
|
---|
3130 | LogRel(("Started NAT network '%s'\n", aNetworkName.c_str()));
|
---|
3131 | else
|
---|
3132 | LogRel(("Error %Rhrc starting NAT network '%s'\n", rc, aNetworkName.c_str()));
|
---|
3133 | AssertComRCReturn(rc, -1);
|
---|
3134 | }
|
---|
3135 |
|
---|
3136 | sNatNetworkNameToRefCount[aNetworkName]++;
|
---|
3137 |
|
---|
3138 | return sNatNetworkNameToRefCount[aNetworkName];
|
---|
3139 | }
|
---|
3140 |
|
---|
3141 |
|
---|
3142 | int VirtualBox::i_natNetworkRefDec(const Utf8Str &aNetworkName)
|
---|
3143 | {
|
---|
3144 | AutoWriteLock safeLock(*spMtxNatNetworkNameToRefCountLock COMMA_LOCKVAL_SRC_POS);
|
---|
3145 |
|
---|
3146 | if (!sNatNetworkNameToRefCount[aNetworkName])
|
---|
3147 | return 0;
|
---|
3148 |
|
---|
3149 | sNatNetworkNameToRefCount[aNetworkName]--;
|
---|
3150 |
|
---|
3151 | if (!sNatNetworkNameToRefCount[aNetworkName])
|
---|
3152 | {
|
---|
3153 | ComPtr<INATNetwork> nat;
|
---|
3154 | HRESULT rc = findNATNetworkByName(aNetworkName, nat);
|
---|
3155 | if (FAILED(rc)) return -1;
|
---|
3156 |
|
---|
3157 | rc = nat->Stop();
|
---|
3158 | if (SUCCEEDED(rc))
|
---|
3159 | LogRel(("Stopped NAT network '%s'\n", aNetworkName.c_str()));
|
---|
3160 | else
|
---|
3161 | LogRel(("Error %Rhrc stopping NAT network '%s'\n", rc, aNetworkName.c_str()));
|
---|
3162 | AssertComRCReturn(rc, -1);
|
---|
3163 | }
|
---|
3164 |
|
---|
3165 | return sNatNetworkNameToRefCount[aNetworkName];
|
---|
3166 | }
|
---|
3167 |
|
---|
3168 |
|
---|
3169 | /**
|
---|
3170 | * @note Locks the list of other objects for reading.
|
---|
3171 | */
|
---|
3172 | ComObjPtr<GuestOSType> VirtualBox::i_getUnknownOSType()
|
---|
3173 | {
|
---|
3174 | ComObjPtr<GuestOSType> type;
|
---|
3175 |
|
---|
3176 | /* unknown type must always be the first */
|
---|
3177 | ComAssertRet(m->allGuestOSTypes.size() > 0, type);
|
---|
3178 |
|
---|
3179 | return m->allGuestOSTypes.front();
|
---|
3180 | }
|
---|
3181 |
|
---|
3182 | /**
|
---|
3183 | * Returns the list of opened machines (machines having VM sessions opened,
|
---|
3184 | * ignoring other sessions) and optionally the list of direct session controls.
|
---|
3185 | *
|
---|
3186 | * @param aMachines Where to put opened machines (will be empty if none).
|
---|
3187 | * @param aControls Where to put direct session controls (optional).
|
---|
3188 | *
|
---|
3189 | * @note The returned lists contain smart pointers. So, clear it as soon as
|
---|
3190 | * it becomes no more necessary to release instances.
|
---|
3191 | *
|
---|
3192 | * @note It can be possible that a session machine from the list has been
|
---|
3193 | * already uninitialized, so do a usual AutoCaller/AutoReadLock sequence
|
---|
3194 | * when accessing unprotected data directly.
|
---|
3195 | *
|
---|
3196 | * @note Locks objects for reading.
|
---|
3197 | */
|
---|
3198 | void VirtualBox::i_getOpenedMachines(SessionMachinesList &aMachines,
|
---|
3199 | InternalControlList *aControls /*= NULL*/)
|
---|
3200 | {
|
---|
3201 | AutoCaller autoCaller(this);
|
---|
3202 | AssertComRCReturnVoid(autoCaller.rc());
|
---|
3203 |
|
---|
3204 | aMachines.clear();
|
---|
3205 | if (aControls)
|
---|
3206 | aControls->clear();
|
---|
3207 |
|
---|
3208 | AutoReadLock alock(m->allMachines.getLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
3209 |
|
---|
3210 | for (MachinesOList::iterator it = m->allMachines.begin();
|
---|
3211 | it != m->allMachines.end();
|
---|
3212 | ++it)
|
---|
3213 | {
|
---|
3214 | ComObjPtr<SessionMachine> sm;
|
---|
3215 | ComPtr<IInternalSessionControl> ctl;
|
---|
3216 | if ((*it)->i_isSessionOpenVM(sm, &ctl))
|
---|
3217 | {
|
---|
3218 | aMachines.push_back(sm);
|
---|
3219 | if (aControls)
|
---|
3220 | aControls->push_back(ctl);
|
---|
3221 | }
|
---|
3222 | }
|
---|
3223 | }
|
---|
3224 |
|
---|
3225 | /**
|
---|
3226 | * Gets a reference to the machine list. This is the real thing, not a copy,
|
---|
3227 | * so bad things will happen if the caller doesn't hold the necessary lock.
|
---|
3228 | *
|
---|
3229 | * @returns reference to machine list
|
---|
3230 | *
|
---|
3231 | * @note Caller must hold the VirtualBox object lock at least for reading.
|
---|
3232 | */
|
---|
3233 | VirtualBox::MachinesOList &VirtualBox::i_getMachinesList(void)
|
---|
3234 | {
|
---|
3235 | return m->allMachines;
|
---|
3236 | }
|
---|
3237 |
|
---|
3238 | /**
|
---|
3239 | * Searches for a machine object with the given ID in the collection
|
---|
3240 | * of registered machines.
|
---|
3241 | *
|
---|
3242 | * @param aId Machine UUID to look for.
|
---|
3243 | * @param fPermitInaccessible If true, inaccessible machines will be found;
|
---|
3244 | * if false, this will fail if the given machine is inaccessible.
|
---|
3245 | * @param aSetError If true, set errorinfo if the machine is not found.
|
---|
3246 | * @param aMachine Returned machine, if found.
|
---|
3247 | * @return
|
---|
3248 | */
|
---|
3249 | HRESULT VirtualBox::i_findMachine(const Guid &aId,
|
---|
3250 | bool fPermitInaccessible,
|
---|
3251 | bool aSetError,
|
---|
3252 | ComObjPtr<Machine> *aMachine /* = NULL */)
|
---|
3253 | {
|
---|
3254 | HRESULT rc = VBOX_E_OBJECT_NOT_FOUND;
|
---|
3255 |
|
---|
3256 | AutoCaller autoCaller(this);
|
---|
3257 | AssertComRCReturnRC(autoCaller.rc());
|
---|
3258 |
|
---|
3259 | {
|
---|
3260 | AutoReadLock al(m->allMachines.getLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
3261 |
|
---|
3262 | for (MachinesOList::iterator it = m->allMachines.begin();
|
---|
3263 | it != m->allMachines.end();
|
---|
3264 | ++it)
|
---|
3265 | {
|
---|
3266 | ComObjPtr<Machine> pMachine = *it;
|
---|
3267 |
|
---|
3268 | if (!fPermitInaccessible)
|
---|
3269 | {
|
---|
3270 | // skip inaccessible machines
|
---|
3271 | AutoCaller machCaller(pMachine);
|
---|
3272 | if (FAILED(machCaller.rc()))
|
---|
3273 | continue;
|
---|
3274 | }
|
---|
3275 |
|
---|
3276 | if (pMachine->i_getId() == aId)
|
---|
3277 | {
|
---|
3278 | rc = S_OK;
|
---|
3279 | if (aMachine)
|
---|
3280 | *aMachine = pMachine;
|
---|
3281 | break;
|
---|
3282 | }
|
---|
3283 | }
|
---|
3284 | }
|
---|
3285 |
|
---|
3286 | if (aSetError && FAILED(rc))
|
---|
3287 | rc = setError(rc,
|
---|
3288 | tr("Could not find a registered machine with UUID {%RTuuid}"),
|
---|
3289 | aId.raw());
|
---|
3290 |
|
---|
3291 | return rc;
|
---|
3292 | }
|
---|
3293 |
|
---|
3294 | /**
|
---|
3295 | * Searches for a machine object with the given name or location in the
|
---|
3296 | * collection of registered machines.
|
---|
3297 | *
|
---|
3298 | * @param aName Machine name or location to look for.
|
---|
3299 | * @param aSetError If true, set errorinfo if the machine is not found.
|
---|
3300 | * @param aMachine Returned machine, if found.
|
---|
3301 | * @return
|
---|
3302 | */
|
---|
3303 | HRESULT VirtualBox::i_findMachineByName(const Utf8Str &aName,
|
---|
3304 | bool aSetError,
|
---|
3305 | ComObjPtr<Machine> *aMachine /* = NULL */)
|
---|
3306 | {
|
---|
3307 | HRESULT rc = VBOX_E_OBJECT_NOT_FOUND;
|
---|
3308 |
|
---|
3309 | AutoReadLock al(m->allMachines.getLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
3310 | for (MachinesOList::iterator it = m->allMachines.begin();
|
---|
3311 | it != m->allMachines.end();
|
---|
3312 | ++it)
|
---|
3313 | {
|
---|
3314 | ComObjPtr<Machine> &pMachine = *it;
|
---|
3315 | AutoCaller machCaller(pMachine);
|
---|
3316 | if (machCaller.rc())
|
---|
3317 | continue; // we can't ask inaccessible machines for their names
|
---|
3318 |
|
---|
3319 | AutoReadLock machLock(pMachine COMMA_LOCKVAL_SRC_POS);
|
---|
3320 | if (pMachine->i_getName() == aName)
|
---|
3321 | {
|
---|
3322 | rc = S_OK;
|
---|
3323 | if (aMachine)
|
---|
3324 | *aMachine = pMachine;
|
---|
3325 | break;
|
---|
3326 | }
|
---|
3327 | if (!RTPathCompare(pMachine->i_getSettingsFileFull().c_str(), aName.c_str()))
|
---|
3328 | {
|
---|
3329 | rc = S_OK;
|
---|
3330 | if (aMachine)
|
---|
3331 | *aMachine = pMachine;
|
---|
3332 | break;
|
---|
3333 | }
|
---|
3334 | }
|
---|
3335 |
|
---|
3336 | if (aSetError && FAILED(rc))
|
---|
3337 | rc = setError(rc,
|
---|
3338 | tr("Could not find a registered machine named '%s'"), aName.c_str());
|
---|
3339 |
|
---|
3340 | return rc;
|
---|
3341 | }
|
---|
3342 |
|
---|
3343 | static HRESULT i_validateMachineGroupHelper(const Utf8Str &aGroup, bool fPrimary, VirtualBox *pVirtualBox)
|
---|
3344 | {
|
---|
3345 | /* empty strings are invalid */
|
---|
3346 | if (aGroup.isEmpty())
|
---|
3347 | return E_INVALIDARG;
|
---|
3348 | /* the toplevel group is valid */
|
---|
3349 | if (aGroup == "/")
|
---|
3350 | return S_OK;
|
---|
3351 | /* any other strings of length 1 are invalid */
|
---|
3352 | if (aGroup.length() == 1)
|
---|
3353 | return E_INVALIDARG;
|
---|
3354 | /* must start with a slash */
|
---|
3355 | if (aGroup.c_str()[0] != '/')
|
---|
3356 | return E_INVALIDARG;
|
---|
3357 | /* must not end with a slash */
|
---|
3358 | if (aGroup.c_str()[aGroup.length() - 1] == '/')
|
---|
3359 | return E_INVALIDARG;
|
---|
3360 | /* check the group components */
|
---|
3361 | const char *pStr = aGroup.c_str() + 1; /* first char is /, skip it */
|
---|
3362 | while (pStr)
|
---|
3363 | {
|
---|
3364 | char *pSlash = RTStrStr(pStr, "/");
|
---|
3365 | if (pSlash)
|
---|
3366 | {
|
---|
3367 | /* no empty components (or // sequences in other words) */
|
---|
3368 | if (pSlash == pStr)
|
---|
3369 | return E_INVALIDARG;
|
---|
3370 | /* check if the machine name rules are violated, because that means
|
---|
3371 | * the group components are too close to the limits. */
|
---|
3372 | Utf8Str tmp((const char *)pStr, (size_t)(pSlash - pStr));
|
---|
3373 | Utf8Str tmp2(tmp);
|
---|
3374 | sanitiseMachineFilename(tmp);
|
---|
3375 | if (tmp != tmp2)
|
---|
3376 | return E_INVALIDARG;
|
---|
3377 | if (fPrimary)
|
---|
3378 | {
|
---|
3379 | HRESULT rc = pVirtualBox->i_findMachineByName(tmp,
|
---|
3380 | false /* aSetError */);
|
---|
3381 | if (SUCCEEDED(rc))
|
---|
3382 | return VBOX_E_VM_ERROR;
|
---|
3383 | }
|
---|
3384 | pStr = pSlash + 1;
|
---|
3385 | }
|
---|
3386 | else
|
---|
3387 | {
|
---|
3388 | /* check if the machine name rules are violated, because that means
|
---|
3389 | * the group components is too close to the limits. */
|
---|
3390 | Utf8Str tmp(pStr);
|
---|
3391 | Utf8Str tmp2(tmp);
|
---|
3392 | sanitiseMachineFilename(tmp);
|
---|
3393 | if (tmp != tmp2)
|
---|
3394 | return E_INVALIDARG;
|
---|
3395 | pStr = NULL;
|
---|
3396 | }
|
---|
3397 | }
|
---|
3398 | return S_OK;
|
---|
3399 | }
|
---|
3400 |
|
---|
3401 | /**
|
---|
3402 | * Validates a machine group.
|
---|
3403 | *
|
---|
3404 | * @param aGroup Machine group.
|
---|
3405 | * @param fPrimary Set if this is the primary group.
|
---|
3406 | *
|
---|
3407 | * @return S_OK or E_INVALIDARG
|
---|
3408 | */
|
---|
3409 | HRESULT VirtualBox::i_validateMachineGroup(const Utf8Str &aGroup, bool fPrimary)
|
---|
3410 | {
|
---|
3411 | HRESULT rc = i_validateMachineGroupHelper(aGroup, fPrimary, this);
|
---|
3412 | if (FAILED(rc))
|
---|
3413 | {
|
---|
3414 | if (rc == VBOX_E_VM_ERROR)
|
---|
3415 | rc = setError(E_INVALIDARG,
|
---|
3416 | tr("Machine group '%s' conflicts with a virtual machine name"),
|
---|
3417 | aGroup.c_str());
|
---|
3418 | else
|
---|
3419 | rc = setError(rc,
|
---|
3420 | tr("Invalid machine group '%s'"),
|
---|
3421 | aGroup.c_str());
|
---|
3422 | }
|
---|
3423 | return rc;
|
---|
3424 | }
|
---|
3425 |
|
---|
3426 | /**
|
---|
3427 | * Takes a list of machine groups, and sanitizes/validates it.
|
---|
3428 | *
|
---|
3429 | * @param aMachineGroups Array with the machine groups.
|
---|
3430 | * @param pllMachineGroups Pointer to list of strings for the result.
|
---|
3431 | *
|
---|
3432 | * @return S_OK or E_INVALIDARG
|
---|
3433 | */
|
---|
3434 | HRESULT VirtualBox::i_convertMachineGroups(const std::vector<com::Utf8Str> aMachineGroups, StringsList *pllMachineGroups)
|
---|
3435 | {
|
---|
3436 | pllMachineGroups->clear();
|
---|
3437 | if (aMachineGroups.size())
|
---|
3438 | {
|
---|
3439 | for (size_t i = 0; i < aMachineGroups.size(); i++)
|
---|
3440 | {
|
---|
3441 | Utf8Str group(aMachineGroups[i]);
|
---|
3442 | if (group.length() == 0)
|
---|
3443 | group = "/";
|
---|
3444 |
|
---|
3445 | HRESULT rc = i_validateMachineGroup(group, i == 0);
|
---|
3446 | if (FAILED(rc))
|
---|
3447 | return rc;
|
---|
3448 |
|
---|
3449 | /* no duplicates please */
|
---|
3450 | if ( find(pllMachineGroups->begin(), pllMachineGroups->end(), group)
|
---|
3451 | == pllMachineGroups->end())
|
---|
3452 | pllMachineGroups->push_back(group);
|
---|
3453 | }
|
---|
3454 | if (pllMachineGroups->size() == 0)
|
---|
3455 | pllMachineGroups->push_back("/");
|
---|
3456 | }
|
---|
3457 | else
|
---|
3458 | pllMachineGroups->push_back("/");
|
---|
3459 |
|
---|
3460 | return S_OK;
|
---|
3461 | }
|
---|
3462 |
|
---|
3463 | /**
|
---|
3464 | * Searches for a Medium object with the given ID in the list of registered
|
---|
3465 | * hard disks.
|
---|
3466 | *
|
---|
3467 | * @param aId ID of the hard disk. Must not be empty.
|
---|
3468 | * @param aSetError If @c true , the appropriate error info is set in case
|
---|
3469 | * when the hard disk is not found.
|
---|
3470 | * @param aHardDisk Where to store the found hard disk object (can be NULL).
|
---|
3471 | *
|
---|
3472 | * @return S_OK, E_INVALIDARG or VBOX_E_OBJECT_NOT_FOUND when not found.
|
---|
3473 | *
|
---|
3474 | * @note Locks the media tree for reading.
|
---|
3475 | */
|
---|
3476 | HRESULT VirtualBox::i_findHardDiskById(const Guid &aId,
|
---|
3477 | bool aSetError,
|
---|
3478 | ComObjPtr<Medium> *aHardDisk /*= NULL*/)
|
---|
3479 | {
|
---|
3480 | AssertReturn(!aId.isZero(), E_INVALIDARG);
|
---|
3481 |
|
---|
3482 | // we use the hard disks map, but it is protected by the
|
---|
3483 | // hard disk _list_ lock handle
|
---|
3484 | AutoReadLock alock(m->allHardDisks.getLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
3485 |
|
---|
3486 | HardDiskMap::const_iterator it = m->mapHardDisks.find(aId);
|
---|
3487 | if (it != m->mapHardDisks.end())
|
---|
3488 | {
|
---|
3489 | if (aHardDisk)
|
---|
3490 | *aHardDisk = (*it).second;
|
---|
3491 | return S_OK;
|
---|
3492 | }
|
---|
3493 |
|
---|
3494 | if (aSetError)
|
---|
3495 | return setError(VBOX_E_OBJECT_NOT_FOUND,
|
---|
3496 | tr("Could not find an open hard disk with UUID {%RTuuid}"),
|
---|
3497 | aId.raw());
|
---|
3498 |
|
---|
3499 | return VBOX_E_OBJECT_NOT_FOUND;
|
---|
3500 | }
|
---|
3501 |
|
---|
3502 | /**
|
---|
3503 | * Searches for a Medium object with the given ID or location in the list of
|
---|
3504 | * registered hard disks. If both ID and location are specified, the first
|
---|
3505 | * object that matches either of them (not necessarily both) is returned.
|
---|
3506 | *
|
---|
3507 | * @param strLocation Full location specification. Must not be empty.
|
---|
3508 | * @param aSetError If @c true , the appropriate error info is set in case
|
---|
3509 | * when the hard disk is not found.
|
---|
3510 | * @param aHardDisk Where to store the found hard disk object (can be NULL).
|
---|
3511 | *
|
---|
3512 | * @return S_OK, E_INVALIDARG or VBOX_E_OBJECT_NOT_FOUND when not found.
|
---|
3513 | *
|
---|
3514 | * @note Locks the media tree for reading.
|
---|
3515 | */
|
---|
3516 | HRESULT VirtualBox::i_findHardDiskByLocation(const Utf8Str &strLocation,
|
---|
3517 | bool aSetError,
|
---|
3518 | ComObjPtr<Medium> *aHardDisk /*= NULL*/)
|
---|
3519 | {
|
---|
3520 | AssertReturn(!strLocation.isEmpty(), E_INVALIDARG);
|
---|
3521 |
|
---|
3522 | // we use the hard disks map, but it is protected by the
|
---|
3523 | // hard disk _list_ lock handle
|
---|
3524 | AutoReadLock alock(m->allHardDisks.getLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
3525 |
|
---|
3526 | for (HardDiskMap::const_iterator it = m->mapHardDisks.begin();
|
---|
3527 | it != m->mapHardDisks.end();
|
---|
3528 | ++it)
|
---|
3529 | {
|
---|
3530 | const ComObjPtr<Medium> &pHD = (*it).second;
|
---|
3531 |
|
---|
3532 | AutoCaller autoCaller(pHD);
|
---|
3533 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
3534 | AutoWriteLock mlock(pHD COMMA_LOCKVAL_SRC_POS);
|
---|
3535 |
|
---|
3536 | Utf8Str strLocationFull = pHD->i_getLocationFull();
|
---|
3537 |
|
---|
3538 | if (0 == RTPathCompare(strLocationFull.c_str(), strLocation.c_str()))
|
---|
3539 | {
|
---|
3540 | if (aHardDisk)
|
---|
3541 | *aHardDisk = pHD;
|
---|
3542 | return S_OK;
|
---|
3543 | }
|
---|
3544 | }
|
---|
3545 |
|
---|
3546 | if (aSetError)
|
---|
3547 | return setError(VBOX_E_OBJECT_NOT_FOUND,
|
---|
3548 | tr("Could not find an open hard disk with location '%s'"),
|
---|
3549 | strLocation.c_str());
|
---|
3550 |
|
---|
3551 | return VBOX_E_OBJECT_NOT_FOUND;
|
---|
3552 | }
|
---|
3553 |
|
---|
3554 | /**
|
---|
3555 | * Searches for a Medium object with the given ID or location in the list of
|
---|
3556 | * registered DVD or floppy images, depending on the @a mediumType argument.
|
---|
3557 | * If both ID and file path are specified, the first object that matches either
|
---|
3558 | * of them (not necessarily both) is returned.
|
---|
3559 | *
|
---|
3560 | * @param mediumType Must be either DeviceType_DVD or DeviceType_Floppy.
|
---|
3561 | * @param aId ID of the image file (unused when NULL).
|
---|
3562 | * @param aLocation Full path to the image file (unused when NULL).
|
---|
3563 | * @param aSetError If @c true, the appropriate error info is set in case when
|
---|
3564 | * the image is not found.
|
---|
3565 | * @param aImage Where to store the found image object (can be NULL).
|
---|
3566 | *
|
---|
3567 | * @return S_OK when found or E_INVALIDARG or VBOX_E_OBJECT_NOT_FOUND when not found.
|
---|
3568 | *
|
---|
3569 | * @note Locks the media tree for reading.
|
---|
3570 | */
|
---|
3571 | HRESULT VirtualBox::i_findDVDOrFloppyImage(DeviceType_T mediumType,
|
---|
3572 | const Guid *aId,
|
---|
3573 | const Utf8Str &aLocation,
|
---|
3574 | bool aSetError,
|
---|
3575 | ComObjPtr<Medium> *aImage /* = NULL */)
|
---|
3576 | {
|
---|
3577 | AssertReturn(aId || !aLocation.isEmpty(), E_INVALIDARG);
|
---|
3578 |
|
---|
3579 | Utf8Str location;
|
---|
3580 | if (!aLocation.isEmpty())
|
---|
3581 | {
|
---|
3582 | int vrc = i_calculateFullPath(aLocation, location);
|
---|
3583 | if (RT_FAILURE(vrc))
|
---|
3584 | return setError(VBOX_E_FILE_ERROR,
|
---|
3585 | tr("Invalid image file location '%s' (%Rrc)"),
|
---|
3586 | aLocation.c_str(),
|
---|
3587 | vrc);
|
---|
3588 | }
|
---|
3589 |
|
---|
3590 | MediaOList *pMediaList;
|
---|
3591 |
|
---|
3592 | switch (mediumType)
|
---|
3593 | {
|
---|
3594 | case DeviceType_DVD:
|
---|
3595 | pMediaList = &m->allDVDImages;
|
---|
3596 | break;
|
---|
3597 |
|
---|
3598 | case DeviceType_Floppy:
|
---|
3599 | pMediaList = &m->allFloppyImages;
|
---|
3600 | break;
|
---|
3601 |
|
---|
3602 | default:
|
---|
3603 | return E_INVALIDARG;
|
---|
3604 | }
|
---|
3605 |
|
---|
3606 | AutoReadLock alock(pMediaList->getLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
3607 |
|
---|
3608 | bool found = false;
|
---|
3609 |
|
---|
3610 | for (MediaList::const_iterator it = pMediaList->begin();
|
---|
3611 | it != pMediaList->end();
|
---|
3612 | ++it)
|
---|
3613 | {
|
---|
3614 | // no AutoCaller, registered image life time is bound to this
|
---|
3615 | Medium *pMedium = *it;
|
---|
3616 | AutoReadLock imageLock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
3617 | const Utf8Str &strLocationFull = pMedium->i_getLocationFull();
|
---|
3618 |
|
---|
3619 | found = ( aId
|
---|
3620 | && pMedium->i_getId() == *aId)
|
---|
3621 | || ( !aLocation.isEmpty()
|
---|
3622 | && RTPathCompare(location.c_str(),
|
---|
3623 | strLocationFull.c_str()) == 0);
|
---|
3624 | if (found)
|
---|
3625 | {
|
---|
3626 | if (pMedium->i_getDeviceType() != mediumType)
|
---|
3627 | {
|
---|
3628 | if (mediumType == DeviceType_DVD)
|
---|
3629 | return setError(E_INVALIDARG,
|
---|
3630 | "Cannot mount DVD medium '%s' as floppy", strLocationFull.c_str());
|
---|
3631 | else
|
---|
3632 | return setError(E_INVALIDARG,
|
---|
3633 | "Cannot mount floppy medium '%s' as DVD", strLocationFull.c_str());
|
---|
3634 | }
|
---|
3635 |
|
---|
3636 | if (aImage)
|
---|
3637 | *aImage = pMedium;
|
---|
3638 | break;
|
---|
3639 | }
|
---|
3640 | }
|
---|
3641 |
|
---|
3642 | HRESULT rc = found ? S_OK : VBOX_E_OBJECT_NOT_FOUND;
|
---|
3643 |
|
---|
3644 | if (aSetError && !found)
|
---|
3645 | {
|
---|
3646 | if (aId)
|
---|
3647 | setError(rc,
|
---|
3648 | tr("Could not find an image file with UUID {%RTuuid} in the media registry ('%s')"),
|
---|
3649 | aId->raw(),
|
---|
3650 | m->strSettingsFilePath.c_str());
|
---|
3651 | else
|
---|
3652 | setError(rc,
|
---|
3653 | tr("Could not find an image file with location '%s' in the media registry ('%s')"),
|
---|
3654 | aLocation.c_str(),
|
---|
3655 | m->strSettingsFilePath.c_str());
|
---|
3656 | }
|
---|
3657 |
|
---|
3658 | return rc;
|
---|
3659 | }
|
---|
3660 |
|
---|
3661 | /**
|
---|
3662 | * Searches for an IMedium object that represents the given UUID.
|
---|
3663 | *
|
---|
3664 | * If the UUID is empty (indicating an empty drive), this sets pMedium
|
---|
3665 | * to NULL and returns S_OK.
|
---|
3666 | *
|
---|
3667 | * If the UUID refers to a host drive of the given device type, this
|
---|
3668 | * sets pMedium to the object from the list in IHost and returns S_OK.
|
---|
3669 | *
|
---|
3670 | * If the UUID is an image file, this sets pMedium to the object that
|
---|
3671 | * findDVDOrFloppyImage() returned.
|
---|
3672 | *
|
---|
3673 | * If none of the above apply, this returns VBOX_E_OBJECT_NOT_FOUND.
|
---|
3674 | *
|
---|
3675 | * @param mediumType Must be DeviceType_DVD or DeviceType_Floppy.
|
---|
3676 | * @param uuid UUID to search for; must refer to a host drive or an image file or be null.
|
---|
3677 | * @param fRefresh Whether to refresh the list of host drives in IHost (see Host::getDrives())
|
---|
3678 | * @param aSetError
|
---|
3679 | * @param pMedium out: IMedium object found.
|
---|
3680 | * @return
|
---|
3681 | */
|
---|
3682 | HRESULT VirtualBox::i_findRemoveableMedium(DeviceType_T mediumType,
|
---|
3683 | const Guid &uuid,
|
---|
3684 | bool fRefresh,
|
---|
3685 | bool aSetError,
|
---|
3686 | ComObjPtr<Medium> &pMedium)
|
---|
3687 | {
|
---|
3688 | if (uuid.isZero())
|
---|
3689 | {
|
---|
3690 | // that's easy
|
---|
3691 | pMedium.setNull();
|
---|
3692 | return S_OK;
|
---|
3693 | }
|
---|
3694 | else if (!uuid.isValid())
|
---|
3695 | {
|
---|
3696 | /* handling of case invalid GUID */
|
---|
3697 | return setError(VBOX_E_OBJECT_NOT_FOUND,
|
---|
3698 | tr("Guid '%s' is invalid"),
|
---|
3699 | uuid.toString().c_str());
|
---|
3700 | }
|
---|
3701 |
|
---|
3702 | // first search for host drive with that UUID
|
---|
3703 | HRESULT rc = m->pHost->i_findHostDriveById(mediumType,
|
---|
3704 | uuid,
|
---|
3705 | fRefresh,
|
---|
3706 | pMedium);
|
---|
3707 | if (rc == VBOX_E_OBJECT_NOT_FOUND)
|
---|
3708 | // then search for an image with that UUID
|
---|
3709 | rc = i_findDVDOrFloppyImage(mediumType, &uuid, Utf8Str::Empty, aSetError, &pMedium);
|
---|
3710 |
|
---|
3711 | return rc;
|
---|
3712 | }
|
---|
3713 |
|
---|
3714 | /* Look for a GuestOSType object */
|
---|
3715 | HRESULT VirtualBox::i_findGuestOSType(const Utf8Str &strOSType,
|
---|
3716 | ComObjPtr<GuestOSType> &guestOSType)
|
---|
3717 | {
|
---|
3718 | guestOSType.setNull();
|
---|
3719 |
|
---|
3720 | AssertMsg(m->allGuestOSTypes.size() != 0,
|
---|
3721 | ("Guest OS types array must be filled"));
|
---|
3722 |
|
---|
3723 | AutoReadLock alock(m->allGuestOSTypes.getLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
3724 | for (GuestOSTypesOList::const_iterator it = m->allGuestOSTypes.begin();
|
---|
3725 | it != m->allGuestOSTypes.end();
|
---|
3726 | ++it)
|
---|
3727 | {
|
---|
3728 | const Utf8Str &typeId = (*it)->i_id();
|
---|
3729 | AssertMsg(!typeId.isEmpty(), ("ID must not be NULL"));
|
---|
3730 | if (strOSType.compare(typeId, Utf8Str::CaseInsensitive) == 0)
|
---|
3731 | {
|
---|
3732 | guestOSType = *it;
|
---|
3733 | return S_OK;
|
---|
3734 | }
|
---|
3735 | }
|
---|
3736 |
|
---|
3737 | return setError(VBOX_E_OBJECT_NOT_FOUND,
|
---|
3738 | tr("'%s' is not a valid Guest OS type"),
|
---|
3739 | strOSType.c_str());
|
---|
3740 | }
|
---|
3741 |
|
---|
3742 | /**
|
---|
3743 | * Returns the constant pseudo-machine UUID that is used to identify the
|
---|
3744 | * global media registry.
|
---|
3745 | *
|
---|
3746 | * Starting with VirtualBox 4.0 each medium remembers in its instance data
|
---|
3747 | * in which media registry it is saved (if any): this can either be a machine
|
---|
3748 | * UUID, if it's in a per-machine media registry, or this global ID.
|
---|
3749 | *
|
---|
3750 | * This UUID is only used to identify the VirtualBox object while VirtualBox
|
---|
3751 | * is running. It is a compile-time constant and not saved anywhere.
|
---|
3752 | *
|
---|
3753 | * @return
|
---|
3754 | */
|
---|
3755 | const Guid& VirtualBox::i_getGlobalRegistryId() const
|
---|
3756 | {
|
---|
3757 | return m->uuidMediaRegistry;
|
---|
3758 | }
|
---|
3759 |
|
---|
3760 | const ComObjPtr<Host>& VirtualBox::i_host() const
|
---|
3761 | {
|
---|
3762 | return m->pHost;
|
---|
3763 | }
|
---|
3764 |
|
---|
3765 | SystemProperties* VirtualBox::i_getSystemProperties() const
|
---|
3766 | {
|
---|
3767 | return m->pSystemProperties;
|
---|
3768 | }
|
---|
3769 |
|
---|
3770 | CloudProviderManager *VirtualBox::i_getCloudProviderManager() const
|
---|
3771 | {
|
---|
3772 | return m->pCloudProviderManager;
|
---|
3773 | }
|
---|
3774 |
|
---|
3775 | #ifdef VBOX_WITH_EXTPACK
|
---|
3776 | /**
|
---|
3777 | * Getter that SystemProperties and others can use to talk to the extension
|
---|
3778 | * pack manager.
|
---|
3779 | */
|
---|
3780 | ExtPackManager* VirtualBox::i_getExtPackManager() const
|
---|
3781 | {
|
---|
3782 | return m->ptrExtPackManager;
|
---|
3783 | }
|
---|
3784 | #endif
|
---|
3785 |
|
---|
3786 | /**
|
---|
3787 | * Getter that machines can talk to the autostart database.
|
---|
3788 | */
|
---|
3789 | AutostartDb* VirtualBox::i_getAutostartDb() const
|
---|
3790 | {
|
---|
3791 | return m->pAutostartDb;
|
---|
3792 | }
|
---|
3793 |
|
---|
3794 | #ifdef VBOX_WITH_RESOURCE_USAGE_API
|
---|
3795 | const ComObjPtr<PerformanceCollector>& VirtualBox::i_performanceCollector() const
|
---|
3796 | {
|
---|
3797 | return m->pPerformanceCollector;
|
---|
3798 | }
|
---|
3799 | #endif /* VBOX_WITH_RESOURCE_USAGE_API */
|
---|
3800 |
|
---|
3801 | /**
|
---|
3802 | * Returns the default machine folder from the system properties
|
---|
3803 | * with proper locking.
|
---|
3804 | * @return
|
---|
3805 | */
|
---|
3806 | void VirtualBox::i_getDefaultMachineFolder(Utf8Str &str) const
|
---|
3807 | {
|
---|
3808 | AutoReadLock propsLock(m->pSystemProperties COMMA_LOCKVAL_SRC_POS);
|
---|
3809 | str = m->pSystemProperties->m->strDefaultMachineFolder;
|
---|
3810 | }
|
---|
3811 |
|
---|
3812 | /**
|
---|
3813 | * Returns the default hard disk format from the system properties
|
---|
3814 | * with proper locking.
|
---|
3815 | * @return
|
---|
3816 | */
|
---|
3817 | void VirtualBox::i_getDefaultHardDiskFormat(Utf8Str &str) const
|
---|
3818 | {
|
---|
3819 | AutoReadLock propsLock(m->pSystemProperties COMMA_LOCKVAL_SRC_POS);
|
---|
3820 | str = m->pSystemProperties->m->strDefaultHardDiskFormat;
|
---|
3821 | }
|
---|
3822 |
|
---|
3823 | const Utf8Str& VirtualBox::i_homeDir() const
|
---|
3824 | {
|
---|
3825 | return m->strHomeDir;
|
---|
3826 | }
|
---|
3827 |
|
---|
3828 | /**
|
---|
3829 | * Calculates the absolute path of the given path taking the VirtualBox home
|
---|
3830 | * directory as the current directory.
|
---|
3831 | *
|
---|
3832 | * @param strPath Path to calculate the absolute path for.
|
---|
3833 | * @param aResult Where to put the result (used only on success, can be the
|
---|
3834 | * same Utf8Str instance as passed in @a aPath).
|
---|
3835 | * @return IPRT result.
|
---|
3836 | *
|
---|
3837 | * @note Doesn't lock any object.
|
---|
3838 | */
|
---|
3839 | int VirtualBox::i_calculateFullPath(const Utf8Str &strPath, Utf8Str &aResult)
|
---|
3840 | {
|
---|
3841 | AutoCaller autoCaller(this);
|
---|
3842 | AssertComRCReturn(autoCaller.rc(), VERR_GENERAL_FAILURE);
|
---|
3843 |
|
---|
3844 | /* no need to lock since mHomeDir is const */
|
---|
3845 |
|
---|
3846 | char folder[RTPATH_MAX];
|
---|
3847 | int vrc = RTPathAbsEx(m->strHomeDir.c_str(),
|
---|
3848 | strPath.c_str(),
|
---|
3849 | folder,
|
---|
3850 | sizeof(folder));
|
---|
3851 | if (RT_SUCCESS(vrc))
|
---|
3852 | aResult = folder;
|
---|
3853 |
|
---|
3854 | return vrc;
|
---|
3855 | }
|
---|
3856 |
|
---|
3857 | /**
|
---|
3858 | * Copies strSource to strTarget, making it relative to the VirtualBox config folder
|
---|
3859 | * if it is a subdirectory thereof, or simply copying it otherwise.
|
---|
3860 | *
|
---|
3861 | * @param strSource Path to evalue and copy.
|
---|
3862 | * @param strTarget Buffer to receive target path.
|
---|
3863 | */
|
---|
3864 | void VirtualBox::i_copyPathRelativeToConfig(const Utf8Str &strSource,
|
---|
3865 | Utf8Str &strTarget)
|
---|
3866 | {
|
---|
3867 | AutoCaller autoCaller(this);
|
---|
3868 | AssertComRCReturnVoid(autoCaller.rc());
|
---|
3869 |
|
---|
3870 | // no need to lock since mHomeDir is const
|
---|
3871 |
|
---|
3872 | // use strTarget as a temporary buffer to hold the machine settings dir
|
---|
3873 | strTarget = m->strHomeDir;
|
---|
3874 | if (RTPathStartsWith(strSource.c_str(), strTarget.c_str()))
|
---|
3875 | // is relative: then append what's left
|
---|
3876 | strTarget.append(strSource.c_str() + strTarget.length()); // include '/'
|
---|
3877 | else
|
---|
3878 | // is not relative: then overwrite
|
---|
3879 | strTarget = strSource;
|
---|
3880 | }
|
---|
3881 |
|
---|
3882 | // private methods
|
---|
3883 | /////////////////////////////////////////////////////////////////////////////
|
---|
3884 |
|
---|
3885 | /**
|
---|
3886 | * Checks if there is a hard disk, DVD or floppy image with the given ID or
|
---|
3887 | * location already registered.
|
---|
3888 | *
|
---|
3889 | * On return, sets @a aConflict to the string describing the conflicting medium,
|
---|
3890 | * or sets it to @c Null if no conflicting media is found. Returns S_OK in
|
---|
3891 | * either case. A failure is unexpected.
|
---|
3892 | *
|
---|
3893 | * @param aId UUID to check.
|
---|
3894 | * @param aLocation Location to check.
|
---|
3895 | * @param aConflict Where to return parameters of the conflicting medium.
|
---|
3896 | * @param ppMedium Medium reference in case this is simply a duplicate.
|
---|
3897 | *
|
---|
3898 | * @note Locks the media tree and media objects for reading.
|
---|
3899 | */
|
---|
3900 | HRESULT VirtualBox::i_checkMediaForConflicts(const Guid &aId,
|
---|
3901 | const Utf8Str &aLocation,
|
---|
3902 | Utf8Str &aConflict,
|
---|
3903 | ComObjPtr<Medium> *ppMedium)
|
---|
3904 | {
|
---|
3905 | AssertReturn(!aId.isZero() && !aLocation.isEmpty(), E_FAIL);
|
---|
3906 | AssertReturn(ppMedium, E_INVALIDARG);
|
---|
3907 |
|
---|
3908 | aConflict.setNull();
|
---|
3909 | ppMedium->setNull();
|
---|
3910 |
|
---|
3911 | AutoReadLock alock(i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
3912 |
|
---|
3913 | HRESULT rc = S_OK;
|
---|
3914 |
|
---|
3915 | ComObjPtr<Medium> pMediumFound;
|
---|
3916 | const char *pcszType = NULL;
|
---|
3917 |
|
---|
3918 | if (aId.isValid() && !aId.isZero())
|
---|
3919 | rc = i_findHardDiskById(aId, false /* aSetError */, &pMediumFound);
|
---|
3920 | if (FAILED(rc) && !aLocation.isEmpty())
|
---|
3921 | rc = i_findHardDiskByLocation(aLocation, false /* aSetError */, &pMediumFound);
|
---|
3922 | if (SUCCEEDED(rc))
|
---|
3923 | pcszType = tr("hard disk");
|
---|
3924 |
|
---|
3925 | if (!pcszType)
|
---|
3926 | {
|
---|
3927 | rc = i_findDVDOrFloppyImage(DeviceType_DVD, &aId, aLocation, false /* aSetError */, &pMediumFound);
|
---|
3928 | if (SUCCEEDED(rc))
|
---|
3929 | pcszType = tr("CD/DVD image");
|
---|
3930 | }
|
---|
3931 |
|
---|
3932 | if (!pcszType)
|
---|
3933 | {
|
---|
3934 | rc = i_findDVDOrFloppyImage(DeviceType_Floppy, &aId, aLocation, false /* aSetError */, &pMediumFound);
|
---|
3935 | if (SUCCEEDED(rc))
|
---|
3936 | pcszType = tr("floppy image");
|
---|
3937 | }
|
---|
3938 |
|
---|
3939 | if (pcszType && pMediumFound)
|
---|
3940 | {
|
---|
3941 | /* Note: no AutoCaller since bound to this */
|
---|
3942 | AutoReadLock mlock(pMediumFound COMMA_LOCKVAL_SRC_POS);
|
---|
3943 |
|
---|
3944 | Utf8Str strLocFound = pMediumFound->i_getLocationFull();
|
---|
3945 | Guid idFound = pMediumFound->i_getId();
|
---|
3946 |
|
---|
3947 | if ( (RTPathCompare(strLocFound.c_str(), aLocation.c_str()) == 0)
|
---|
3948 | && (idFound == aId)
|
---|
3949 | )
|
---|
3950 | *ppMedium = pMediumFound;
|
---|
3951 |
|
---|
3952 | aConflict = Utf8StrFmt(tr("%s '%s' with UUID {%RTuuid}"),
|
---|
3953 | pcszType,
|
---|
3954 | strLocFound.c_str(),
|
---|
3955 | idFound.raw());
|
---|
3956 | }
|
---|
3957 |
|
---|
3958 | return S_OK;
|
---|
3959 | }
|
---|
3960 |
|
---|
3961 | /**
|
---|
3962 | * Checks whether the given UUID is already in use by one medium for the
|
---|
3963 | * given device type.
|
---|
3964 | *
|
---|
3965 | * @returns true if the UUID is already in use
|
---|
3966 | * fale otherwise
|
---|
3967 | * @param aId The UUID to check.
|
---|
3968 | * @param deviceType The device type the UUID is going to be checked for
|
---|
3969 | * conflicts.
|
---|
3970 | */
|
---|
3971 | bool VirtualBox::i_isMediaUuidInUse(const Guid &aId, DeviceType_T deviceType)
|
---|
3972 | {
|
---|
3973 | /* A zero UUID is invalid here, always claim that it is already used. */
|
---|
3974 | AssertReturn(!aId.isZero(), true);
|
---|
3975 |
|
---|
3976 | AutoReadLock alock(i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
3977 |
|
---|
3978 | HRESULT rc = S_OK;
|
---|
3979 | bool fInUse = false;
|
---|
3980 |
|
---|
3981 | ComObjPtr<Medium> pMediumFound;
|
---|
3982 |
|
---|
3983 | switch (deviceType)
|
---|
3984 | {
|
---|
3985 | case DeviceType_HardDisk:
|
---|
3986 | rc = i_findHardDiskById(aId, false /* aSetError */, &pMediumFound);
|
---|
3987 | break;
|
---|
3988 | case DeviceType_DVD:
|
---|
3989 | rc = i_findDVDOrFloppyImage(DeviceType_DVD, &aId, Utf8Str::Empty, false /* aSetError */, &pMediumFound);
|
---|
3990 | break;
|
---|
3991 | case DeviceType_Floppy:
|
---|
3992 | rc = i_findDVDOrFloppyImage(DeviceType_Floppy, &aId, Utf8Str::Empty, false /* aSetError */, &pMediumFound);
|
---|
3993 | break;
|
---|
3994 | default:
|
---|
3995 | AssertMsgFailed(("Invalid device type %d\n", deviceType));
|
---|
3996 | }
|
---|
3997 |
|
---|
3998 | if (SUCCEEDED(rc) && pMediumFound)
|
---|
3999 | fInUse = true;
|
---|
4000 |
|
---|
4001 | return fInUse;
|
---|
4002 | }
|
---|
4003 |
|
---|
4004 | /**
|
---|
4005 | * Called from Machine::prepareSaveSettings() when it has detected
|
---|
4006 | * that a machine has been renamed. Such renames will require
|
---|
4007 | * updating the global media registry during the
|
---|
4008 | * VirtualBox::saveSettings() that follows later.
|
---|
4009 | *
|
---|
4010 | * When a machine is renamed, there may well be media (in particular,
|
---|
4011 | * diff images for snapshots) in the global registry that will need
|
---|
4012 | * to have their paths updated. Before 3.2, Machine::saveSettings
|
---|
4013 | * used to call VirtualBox::saveSettings implicitly, which was both
|
---|
4014 | * unintuitive and caused locking order problems. Now, we remember
|
---|
4015 | * such pending name changes with this method so that
|
---|
4016 | * VirtualBox::saveSettings() can process them properly.
|
---|
4017 | */
|
---|
4018 | void VirtualBox::i_rememberMachineNameChangeForMedia(const Utf8Str &strOldConfigDir,
|
---|
4019 | const Utf8Str &strNewConfigDir)
|
---|
4020 | {
|
---|
4021 | AutoWriteLock mediaLock(i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
4022 |
|
---|
4023 | Data::PendingMachineRename pmr;
|
---|
4024 | pmr.strConfigDirOld = strOldConfigDir;
|
---|
4025 | pmr.strConfigDirNew = strNewConfigDir;
|
---|
4026 | m->llPendingMachineRenames.push_back(pmr);
|
---|
4027 | }
|
---|
4028 |
|
---|
4029 | static DECLCALLBACK(int) fntSaveMediaRegistries(void *pvUser);
|
---|
4030 |
|
---|
4031 | class SaveMediaRegistriesDesc : public ThreadTask
|
---|
4032 | {
|
---|
4033 |
|
---|
4034 | public:
|
---|
4035 | SaveMediaRegistriesDesc()
|
---|
4036 | {
|
---|
4037 | m_strTaskName = "SaveMediaReg";
|
---|
4038 | }
|
---|
4039 | virtual ~SaveMediaRegistriesDesc(void) { }
|
---|
4040 |
|
---|
4041 | private:
|
---|
4042 | void handler()
|
---|
4043 | {
|
---|
4044 | try
|
---|
4045 | {
|
---|
4046 | fntSaveMediaRegistries(this);
|
---|
4047 | }
|
---|
4048 | catch(...)
|
---|
4049 | {
|
---|
4050 | LogRel(("Exception in the function fntSaveMediaRegistries()\n"));
|
---|
4051 | }
|
---|
4052 | }
|
---|
4053 |
|
---|
4054 | MediaList llMedia;
|
---|
4055 | ComObjPtr<VirtualBox> pVirtualBox;
|
---|
4056 |
|
---|
4057 | friend DECLCALLBACK(int) fntSaveMediaRegistries(void *pvUser);
|
---|
4058 | friend void VirtualBox::i_saveMediaRegistry(settings::MediaRegistry &mediaRegistry,
|
---|
4059 | const Guid &uuidRegistry,
|
---|
4060 | const Utf8Str &strMachineFolder);
|
---|
4061 | };
|
---|
4062 |
|
---|
4063 | DECLCALLBACK(int) fntSaveMediaRegistries(void *pvUser)
|
---|
4064 | {
|
---|
4065 | SaveMediaRegistriesDesc *pDesc = (SaveMediaRegistriesDesc *)pvUser;
|
---|
4066 | if (!pDesc)
|
---|
4067 | {
|
---|
4068 | LogRelFunc(("Thread for saving media registries lacks parameters\n"));
|
---|
4069 | return VERR_INVALID_PARAMETER;
|
---|
4070 | }
|
---|
4071 |
|
---|
4072 | for (MediaList::const_iterator it = pDesc->llMedia.begin();
|
---|
4073 | it != pDesc->llMedia.end();
|
---|
4074 | ++it)
|
---|
4075 | {
|
---|
4076 | Medium *pMedium = *it;
|
---|
4077 | pMedium->i_markRegistriesModified();
|
---|
4078 | }
|
---|
4079 |
|
---|
4080 | pDesc->pVirtualBox->i_saveModifiedRegistries();
|
---|
4081 |
|
---|
4082 | pDesc->llMedia.clear();
|
---|
4083 | pDesc->pVirtualBox.setNull();
|
---|
4084 |
|
---|
4085 | return VINF_SUCCESS;
|
---|
4086 | }
|
---|
4087 |
|
---|
4088 | /**
|
---|
4089 | * Goes through all known media (hard disks, floppies and DVDs) and saves
|
---|
4090 | * those into the given settings::MediaRegistry structures whose registry
|
---|
4091 | * ID match the given UUID.
|
---|
4092 | *
|
---|
4093 | * Before actually writing to the structures, all media paths (not just the
|
---|
4094 | * ones for the given registry) are updated if machines have been renamed
|
---|
4095 | * since the last call.
|
---|
4096 | *
|
---|
4097 | * This gets called from two contexts:
|
---|
4098 | *
|
---|
4099 | * -- VirtualBox::saveSettings() with the UUID of the global registry
|
---|
4100 | * (VirtualBox::Data.uuidRegistry); this will save those media
|
---|
4101 | * which had been loaded from the global registry or have been
|
---|
4102 | * attached to a "legacy" machine which can't save its own registry;
|
---|
4103 | *
|
---|
4104 | * -- Machine::saveSettings() with the UUID of a machine, if a medium
|
---|
4105 | * has been attached to a machine created with VirtualBox 4.0 or later.
|
---|
4106 | *
|
---|
4107 | * Media which have only been temporarily opened without having been
|
---|
4108 | * attached to a machine have a NULL registry UUID and therefore don't
|
---|
4109 | * get saved.
|
---|
4110 | *
|
---|
4111 | * This locks the media tree. Throws HRESULT on errors!
|
---|
4112 | *
|
---|
4113 | * @param mediaRegistry Settings structure to fill.
|
---|
4114 | * @param uuidRegistry The UUID of the media registry; either a machine UUID
|
---|
4115 | * (if machine registry) or the UUID of the global registry.
|
---|
4116 | * @param strMachineFolder The machine folder for relative paths, if machine registry, or an empty string otherwise.
|
---|
4117 | */
|
---|
4118 | void VirtualBox::i_saveMediaRegistry(settings::MediaRegistry &mediaRegistry,
|
---|
4119 | const Guid &uuidRegistry,
|
---|
4120 | const Utf8Str &strMachineFolder)
|
---|
4121 | {
|
---|
4122 | // lock all media for the following; use a write lock because we're
|
---|
4123 | // modifying the PendingMachineRenamesList, which is protected by this
|
---|
4124 | AutoWriteLock mediaLock(i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
4125 |
|
---|
4126 | // if a machine was renamed, then we'll need to refresh media paths
|
---|
4127 | if (m->llPendingMachineRenames.size())
|
---|
4128 | {
|
---|
4129 | // make a single list from the three media lists so we don't need three loops
|
---|
4130 | MediaList llAllMedia;
|
---|
4131 | // with hard disks, we must use the map, not the list, because the list only has base images
|
---|
4132 | for (HardDiskMap::iterator it = m->mapHardDisks.begin(); it != m->mapHardDisks.end(); ++it)
|
---|
4133 | llAllMedia.push_back(it->second);
|
---|
4134 | for (MediaList::iterator it = m->allDVDImages.begin(); it != m->allDVDImages.end(); ++it)
|
---|
4135 | llAllMedia.push_back(*it);
|
---|
4136 | for (MediaList::iterator it = m->allFloppyImages.begin(); it != m->allFloppyImages.end(); ++it)
|
---|
4137 | llAllMedia.push_back(*it);
|
---|
4138 |
|
---|
4139 | SaveMediaRegistriesDesc *pDesc = new SaveMediaRegistriesDesc();
|
---|
4140 | for (MediaList::iterator it = llAllMedia.begin();
|
---|
4141 | it != llAllMedia.end();
|
---|
4142 | ++it)
|
---|
4143 | {
|
---|
4144 | Medium *pMedium = *it;
|
---|
4145 | for (Data::PendingMachineRenamesList::iterator it2 = m->llPendingMachineRenames.begin();
|
---|
4146 | it2 != m->llPendingMachineRenames.end();
|
---|
4147 | ++it2)
|
---|
4148 | {
|
---|
4149 | const Data::PendingMachineRename &pmr = *it2;
|
---|
4150 | HRESULT rc = pMedium->i_updatePath(pmr.strConfigDirOld,
|
---|
4151 | pmr.strConfigDirNew);
|
---|
4152 | if (SUCCEEDED(rc))
|
---|
4153 | {
|
---|
4154 | // Remember which medium objects has been changed,
|
---|
4155 | // to trigger saving their registries later.
|
---|
4156 | pDesc->llMedia.push_back(pMedium);
|
---|
4157 | } else if (rc == VBOX_E_FILE_ERROR)
|
---|
4158 | /* nothing */;
|
---|
4159 | else
|
---|
4160 | AssertComRC(rc);
|
---|
4161 | }
|
---|
4162 | }
|
---|
4163 | // done, don't do it again until we have more machine renames
|
---|
4164 | m->llPendingMachineRenames.clear();
|
---|
4165 |
|
---|
4166 | if (pDesc->llMedia.size())
|
---|
4167 | {
|
---|
4168 | // Handle the media registry saving in a separate thread, to
|
---|
4169 | // avoid giant locking problems and passing up the list many
|
---|
4170 | // levels up to whoever triggered saveSettings, as there are
|
---|
4171 | // lots of places which would need to handle saving more settings.
|
---|
4172 | pDesc->pVirtualBox = this;
|
---|
4173 | HRESULT hr = S_OK;
|
---|
4174 | try
|
---|
4175 | {
|
---|
4176 | //the function createThread() takes ownership of pDesc
|
---|
4177 | //so there is no need to use delete operator for pDesc
|
---|
4178 | //after calling this function
|
---|
4179 | hr = pDesc->createThread();
|
---|
4180 | }
|
---|
4181 | catch(...)
|
---|
4182 | {
|
---|
4183 | hr = E_FAIL;
|
---|
4184 | }
|
---|
4185 |
|
---|
4186 | if (FAILED(hr))
|
---|
4187 | {
|
---|
4188 | // failure means that settings aren't saved, but there isn't
|
---|
4189 | // much we can do besides avoiding memory leaks
|
---|
4190 | LogRelFunc(("Failed to create thread for saving media registries (%Rhr)\n", hr));
|
---|
4191 | }
|
---|
4192 | }
|
---|
4193 | else
|
---|
4194 | delete pDesc;
|
---|
4195 | }
|
---|
4196 |
|
---|
4197 | struct {
|
---|
4198 | MediaOList &llSource;
|
---|
4199 | settings::MediaList &llTarget;
|
---|
4200 | } s[] =
|
---|
4201 | {
|
---|
4202 | // hard disks
|
---|
4203 | { m->allHardDisks, mediaRegistry.llHardDisks },
|
---|
4204 | // CD/DVD images
|
---|
4205 | { m->allDVDImages, mediaRegistry.llDvdImages },
|
---|
4206 | // floppy images
|
---|
4207 | { m->allFloppyImages, mediaRegistry.llFloppyImages }
|
---|
4208 | };
|
---|
4209 |
|
---|
4210 | HRESULT rc;
|
---|
4211 |
|
---|
4212 | for (size_t i = 0; i < RT_ELEMENTS(s); ++i)
|
---|
4213 | {
|
---|
4214 | MediaOList &llSource = s[i].llSource;
|
---|
4215 | settings::MediaList &llTarget = s[i].llTarget;
|
---|
4216 | llTarget.clear();
|
---|
4217 | for (MediaList::const_iterator it = llSource.begin();
|
---|
4218 | it != llSource.end();
|
---|
4219 | ++it)
|
---|
4220 | {
|
---|
4221 | Medium *pMedium = *it;
|
---|
4222 | AutoCaller autoCaller(pMedium);
|
---|
4223 | if (FAILED(autoCaller.rc())) throw autoCaller.rc();
|
---|
4224 | AutoReadLock mlock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
4225 |
|
---|
4226 | if (pMedium->i_isInRegistry(uuidRegistry))
|
---|
4227 | {
|
---|
4228 | llTarget.push_back(settings::Medium::Empty);
|
---|
4229 | rc = pMedium->i_saveSettings(llTarget.back(), strMachineFolder); // this recurses into child hard disks
|
---|
4230 | if (FAILED(rc))
|
---|
4231 | {
|
---|
4232 | llTarget.pop_back();
|
---|
4233 | throw rc;
|
---|
4234 | }
|
---|
4235 | }
|
---|
4236 | }
|
---|
4237 | }
|
---|
4238 | }
|
---|
4239 |
|
---|
4240 | /**
|
---|
4241 | * Helper function which actually writes out VirtualBox.xml, the main configuration file.
|
---|
4242 | * Gets called from the public VirtualBox::SaveSettings() as well as from various other
|
---|
4243 | * places internally when settings need saving.
|
---|
4244 | *
|
---|
4245 | * @note Caller must have locked the VirtualBox object for writing and must not hold any
|
---|
4246 | * other locks since this locks all kinds of member objects and trees temporarily,
|
---|
4247 | * which could cause conflicts.
|
---|
4248 | */
|
---|
4249 | HRESULT VirtualBox::i_saveSettings()
|
---|
4250 | {
|
---|
4251 | AutoCaller autoCaller(this);
|
---|
4252 | AssertComRCReturnRC(autoCaller.rc());
|
---|
4253 |
|
---|
4254 | AssertReturn(isWriteLockOnCurrentThread(), E_FAIL);
|
---|
4255 | AssertReturn(!m->strSettingsFilePath.isEmpty(), E_FAIL);
|
---|
4256 |
|
---|
4257 | i_unmarkRegistryModified(i_getGlobalRegistryId());
|
---|
4258 |
|
---|
4259 | HRESULT rc = S_OK;
|
---|
4260 |
|
---|
4261 | try
|
---|
4262 | {
|
---|
4263 | // machines
|
---|
4264 | m->pMainConfigFile->llMachines.clear();
|
---|
4265 | {
|
---|
4266 | AutoReadLock machinesLock(m->allMachines.getLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
4267 | for (MachinesOList::iterator it = m->allMachines.begin();
|
---|
4268 | it != m->allMachines.end();
|
---|
4269 | ++it)
|
---|
4270 | {
|
---|
4271 | Machine *pMachine = *it;
|
---|
4272 | // save actual machine registry entry
|
---|
4273 | settings::MachineRegistryEntry mre;
|
---|
4274 | rc = pMachine->i_saveRegistryEntry(mre);
|
---|
4275 | m->pMainConfigFile->llMachines.push_back(mre);
|
---|
4276 | }
|
---|
4277 | }
|
---|
4278 |
|
---|
4279 | i_saveMediaRegistry(m->pMainConfigFile->mediaRegistry,
|
---|
4280 | m->uuidMediaRegistry, // global media registry ID
|
---|
4281 | Utf8Str::Empty); // strMachineFolder
|
---|
4282 |
|
---|
4283 | m->pMainConfigFile->llDhcpServers.clear();
|
---|
4284 | {
|
---|
4285 | AutoReadLock dhcpLock(m->allDHCPServers.getLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
4286 | for (DHCPServersOList::const_iterator it = m->allDHCPServers.begin();
|
---|
4287 | it != m->allDHCPServers.end();
|
---|
4288 | ++it)
|
---|
4289 | {
|
---|
4290 | settings::DHCPServer d;
|
---|
4291 | rc = (*it)->i_saveSettings(d);
|
---|
4292 | if (FAILED(rc)) throw rc;
|
---|
4293 | m->pMainConfigFile->llDhcpServers.push_back(d);
|
---|
4294 | }
|
---|
4295 | }
|
---|
4296 |
|
---|
4297 | #ifdef VBOX_WITH_NAT_SERVICE
|
---|
4298 | /* Saving NAT Network configuration */
|
---|
4299 | m->pMainConfigFile->llNATNetworks.clear();
|
---|
4300 | {
|
---|
4301 | AutoReadLock natNetworkLock(m->allNATNetworks.getLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
4302 | for (NATNetworksOList::const_iterator it = m->allNATNetworks.begin();
|
---|
4303 | it != m->allNATNetworks.end();
|
---|
4304 | ++it)
|
---|
4305 | {
|
---|
4306 | settings::NATNetwork n;
|
---|
4307 | rc = (*it)->i_saveSettings(n);
|
---|
4308 | if (FAILED(rc)) throw rc;
|
---|
4309 | m->pMainConfigFile->llNATNetworks.push_back(n);
|
---|
4310 | }
|
---|
4311 | }
|
---|
4312 | #endif
|
---|
4313 |
|
---|
4314 | // leave extra data alone, it's still in the config file
|
---|
4315 |
|
---|
4316 | // host data (USB filters)
|
---|
4317 | rc = m->pHost->i_saveSettings(m->pMainConfigFile->host);
|
---|
4318 | if (FAILED(rc)) throw rc;
|
---|
4319 |
|
---|
4320 | rc = m->pSystemProperties->i_saveSettings(m->pMainConfigFile->systemProperties);
|
---|
4321 | if (FAILED(rc)) throw rc;
|
---|
4322 |
|
---|
4323 | // and write out the XML, still under the lock
|
---|
4324 | m->pMainConfigFile->write(m->strSettingsFilePath);
|
---|
4325 | }
|
---|
4326 | catch (HRESULT err)
|
---|
4327 | {
|
---|
4328 | /* we assume that error info is set by the thrower */
|
---|
4329 | rc = err;
|
---|
4330 | }
|
---|
4331 | catch (...)
|
---|
4332 | {
|
---|
4333 | rc = VirtualBoxBase::handleUnexpectedExceptions(this, RT_SRC_POS);
|
---|
4334 | }
|
---|
4335 |
|
---|
4336 | return rc;
|
---|
4337 | }
|
---|
4338 |
|
---|
4339 | /**
|
---|
4340 | * Helper to register the machine.
|
---|
4341 | *
|
---|
4342 | * When called during VirtualBox startup, adds the given machine to the
|
---|
4343 | * collection of registered machines. Otherwise tries to mark the machine
|
---|
4344 | * as registered, and, if succeeded, adds it to the collection and
|
---|
4345 | * saves global settings.
|
---|
4346 | *
|
---|
4347 | * @note The caller must have added itself as a caller of the @a aMachine
|
---|
4348 | * object if calls this method not on VirtualBox startup.
|
---|
4349 | *
|
---|
4350 | * @param aMachine machine to register
|
---|
4351 | *
|
---|
4352 | * @note Locks objects!
|
---|
4353 | */
|
---|
4354 | HRESULT VirtualBox::i_registerMachine(Machine *aMachine)
|
---|
4355 | {
|
---|
4356 | ComAssertRet(aMachine, E_INVALIDARG);
|
---|
4357 |
|
---|
4358 | AutoCaller autoCaller(this);
|
---|
4359 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
4360 |
|
---|
4361 | HRESULT rc = S_OK;
|
---|
4362 |
|
---|
4363 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4364 |
|
---|
4365 | {
|
---|
4366 | ComObjPtr<Machine> pMachine;
|
---|
4367 | rc = i_findMachine(aMachine->i_getId(),
|
---|
4368 | true /* fPermitInaccessible */,
|
---|
4369 | false /* aDoSetError */,
|
---|
4370 | &pMachine);
|
---|
4371 | if (SUCCEEDED(rc))
|
---|
4372 | {
|
---|
4373 | /* sanity */
|
---|
4374 | AutoLimitedCaller machCaller(pMachine);
|
---|
4375 | AssertComRC(machCaller.rc());
|
---|
4376 |
|
---|
4377 | return setError(E_INVALIDARG,
|
---|
4378 | tr("Registered machine with UUID {%RTuuid} ('%s') already exists"),
|
---|
4379 | aMachine->i_getId().raw(),
|
---|
4380 | pMachine->i_getSettingsFileFull().c_str());
|
---|
4381 | }
|
---|
4382 |
|
---|
4383 | ComAssertRet(rc == VBOX_E_OBJECT_NOT_FOUND, rc);
|
---|
4384 | rc = S_OK;
|
---|
4385 | }
|
---|
4386 |
|
---|
4387 | if (getObjectState().getState() != ObjectState::InInit)
|
---|
4388 | {
|
---|
4389 | rc = aMachine->i_prepareRegister();
|
---|
4390 | if (FAILED(rc)) return rc;
|
---|
4391 | }
|
---|
4392 |
|
---|
4393 | /* add to the collection of registered machines */
|
---|
4394 | m->allMachines.addChild(aMachine);
|
---|
4395 |
|
---|
4396 | if (getObjectState().getState() != ObjectState::InInit)
|
---|
4397 | rc = i_saveSettings();
|
---|
4398 |
|
---|
4399 | return rc;
|
---|
4400 | }
|
---|
4401 |
|
---|
4402 | /**
|
---|
4403 | * Remembers the given medium object by storing it in either the global
|
---|
4404 | * medium registry or a machine one.
|
---|
4405 | *
|
---|
4406 | * @note Caller must hold the media tree lock for writing; in addition, this
|
---|
4407 | * locks @a pMedium for reading
|
---|
4408 | *
|
---|
4409 | * @param pMedium Medium object to remember.
|
---|
4410 | * @param ppMedium Actually stored medium object. Can be different if due
|
---|
4411 | * to an unavoidable race there was a duplicate Medium object
|
---|
4412 | * created.
|
---|
4413 | * @param mediaTreeLock Reference to the AutoWriteLock holding the media tree
|
---|
4414 | * lock, necessary to release it in the right spot.
|
---|
4415 | * @return
|
---|
4416 | */
|
---|
4417 | HRESULT VirtualBox::i_registerMedium(const ComObjPtr<Medium> &pMedium,
|
---|
4418 | ComObjPtr<Medium> *ppMedium,
|
---|
4419 | AutoWriteLock &mediaTreeLock)
|
---|
4420 | {
|
---|
4421 | AssertReturn(pMedium != NULL, E_INVALIDARG);
|
---|
4422 | AssertReturn(ppMedium != NULL, E_INVALIDARG);
|
---|
4423 |
|
---|
4424 | // caller must hold the media tree write lock
|
---|
4425 | Assert(i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
4426 |
|
---|
4427 | AutoCaller autoCaller(this);
|
---|
4428 | AssertComRCReturnRC(autoCaller.rc());
|
---|
4429 |
|
---|
4430 | AutoCaller mediumCaller(pMedium);
|
---|
4431 | AssertComRCReturnRC(mediumCaller.rc());
|
---|
4432 |
|
---|
4433 | bool fAddToGlobalRegistry = false;
|
---|
4434 | const char *pszDevType = NULL;
|
---|
4435 | Guid regId;
|
---|
4436 | ObjectsList<Medium> *pall = NULL;
|
---|
4437 | DeviceType_T devType;
|
---|
4438 | {
|
---|
4439 | AutoReadLock mediumLock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
4440 | devType = pMedium->i_getDeviceType();
|
---|
4441 |
|
---|
4442 | if (!pMedium->i_getFirstRegistryMachineId(regId))
|
---|
4443 | fAddToGlobalRegistry = true;
|
---|
4444 | }
|
---|
4445 | switch (devType)
|
---|
4446 | {
|
---|
4447 | case DeviceType_HardDisk:
|
---|
4448 | pall = &m->allHardDisks;
|
---|
4449 | pszDevType = tr("hard disk");
|
---|
4450 | break;
|
---|
4451 | case DeviceType_DVD:
|
---|
4452 | pszDevType = tr("DVD image");
|
---|
4453 | pall = &m->allDVDImages;
|
---|
4454 | break;
|
---|
4455 | case DeviceType_Floppy:
|
---|
4456 | pszDevType = tr("floppy image");
|
---|
4457 | pall = &m->allFloppyImages;
|
---|
4458 | break;
|
---|
4459 | default:
|
---|
4460 | AssertMsgFailedReturn(("invalid device type %d", devType), E_INVALIDARG);
|
---|
4461 | }
|
---|
4462 |
|
---|
4463 | Guid id;
|
---|
4464 | Utf8Str strLocationFull;
|
---|
4465 | ComObjPtr<Medium> pParent;
|
---|
4466 | {
|
---|
4467 | AutoReadLock mediumLock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
4468 | id = pMedium->i_getId();
|
---|
4469 | strLocationFull = pMedium->i_getLocationFull();
|
---|
4470 | pParent = pMedium->i_getParent();
|
---|
4471 | }
|
---|
4472 |
|
---|
4473 | HRESULT rc;
|
---|
4474 |
|
---|
4475 | Utf8Str strConflict;
|
---|
4476 | ComObjPtr<Medium> pDupMedium;
|
---|
4477 | rc = i_checkMediaForConflicts(id,
|
---|
4478 | strLocationFull,
|
---|
4479 | strConflict,
|
---|
4480 | &pDupMedium);
|
---|
4481 | if (FAILED(rc)) return rc;
|
---|
4482 |
|
---|
4483 | if (pDupMedium.isNull())
|
---|
4484 | {
|
---|
4485 | if (strConflict.length())
|
---|
4486 | return setError(E_INVALIDARG,
|
---|
4487 | tr("Cannot register the %s '%s' {%RTuuid} because a %s already exists"),
|
---|
4488 | pszDevType,
|
---|
4489 | strLocationFull.c_str(),
|
---|
4490 | id.raw(),
|
---|
4491 | strConflict.c_str(),
|
---|
4492 | m->strSettingsFilePath.c_str());
|
---|
4493 |
|
---|
4494 | // add to the collection if it is a base medium
|
---|
4495 | if (pParent.isNull())
|
---|
4496 | pall->getList().push_back(pMedium);
|
---|
4497 |
|
---|
4498 | // store all hard disks (even differencing images) in the map
|
---|
4499 | if (devType == DeviceType_HardDisk)
|
---|
4500 | m->mapHardDisks[id] = pMedium;
|
---|
4501 |
|
---|
4502 | mediumCaller.release();
|
---|
4503 | mediaTreeLock.release();
|
---|
4504 | *ppMedium = pMedium;
|
---|
4505 | }
|
---|
4506 | else
|
---|
4507 | {
|
---|
4508 | // pMedium may be the last reference to the Medium object, and the
|
---|
4509 | // caller may have specified the same ComObjPtr as the output parameter.
|
---|
4510 | // In this case the assignment will uninit the object, and we must not
|
---|
4511 | // have a caller pending.
|
---|
4512 | mediumCaller.release();
|
---|
4513 | // release media tree lock, must not be held at uninit time.
|
---|
4514 | mediaTreeLock.release();
|
---|
4515 | // must not hold the media tree write lock any more
|
---|
4516 | Assert(!i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
4517 | *ppMedium = pDupMedium;
|
---|
4518 | }
|
---|
4519 |
|
---|
4520 | if (fAddToGlobalRegistry)
|
---|
4521 | {
|
---|
4522 | AutoWriteLock mediumLock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
4523 | if (pMedium->i_addRegistry(m->uuidMediaRegistry))
|
---|
4524 | i_markRegistryModified(m->uuidMediaRegistry);
|
---|
4525 | }
|
---|
4526 |
|
---|
4527 | // Restore the initial lock state, so that no unexpected lock changes are
|
---|
4528 | // done by this method, which would need adjustments everywhere.
|
---|
4529 | mediaTreeLock.acquire();
|
---|
4530 |
|
---|
4531 | return rc;
|
---|
4532 | }
|
---|
4533 |
|
---|
4534 | /**
|
---|
4535 | * Removes the given medium from the respective registry.
|
---|
4536 | *
|
---|
4537 | * @param pMedium Hard disk object to remove.
|
---|
4538 | *
|
---|
4539 | * @note Caller must hold the media tree lock for writing; in addition, this locks @a pMedium for reading
|
---|
4540 | */
|
---|
4541 | HRESULT VirtualBox::i_unregisterMedium(Medium *pMedium)
|
---|
4542 | {
|
---|
4543 | AssertReturn(pMedium != NULL, E_INVALIDARG);
|
---|
4544 |
|
---|
4545 | AutoCaller autoCaller(this);
|
---|
4546 | AssertComRCReturnRC(autoCaller.rc());
|
---|
4547 |
|
---|
4548 | AutoCaller mediumCaller(pMedium);
|
---|
4549 | AssertComRCReturnRC(mediumCaller.rc());
|
---|
4550 |
|
---|
4551 | // caller must hold the media tree write lock
|
---|
4552 | Assert(i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
4553 |
|
---|
4554 | Guid id;
|
---|
4555 | ComObjPtr<Medium> pParent;
|
---|
4556 | DeviceType_T devType;
|
---|
4557 | {
|
---|
4558 | AutoReadLock mediumLock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
4559 | id = pMedium->i_getId();
|
---|
4560 | pParent = pMedium->i_getParent();
|
---|
4561 | devType = pMedium->i_getDeviceType();
|
---|
4562 | }
|
---|
4563 |
|
---|
4564 | ObjectsList<Medium> *pall = NULL;
|
---|
4565 | switch (devType)
|
---|
4566 | {
|
---|
4567 | case DeviceType_HardDisk:
|
---|
4568 | pall = &m->allHardDisks;
|
---|
4569 | break;
|
---|
4570 | case DeviceType_DVD:
|
---|
4571 | pall = &m->allDVDImages;
|
---|
4572 | break;
|
---|
4573 | case DeviceType_Floppy:
|
---|
4574 | pall = &m->allFloppyImages;
|
---|
4575 | break;
|
---|
4576 | default:
|
---|
4577 | AssertMsgFailedReturn(("invalid device type %d", devType), E_INVALIDARG);
|
---|
4578 | }
|
---|
4579 |
|
---|
4580 | // remove from the collection if it is a base medium
|
---|
4581 | if (pParent.isNull())
|
---|
4582 | pall->getList().remove(pMedium);
|
---|
4583 |
|
---|
4584 | // remove all hard disks (even differencing images) from map
|
---|
4585 | if (devType == DeviceType_HardDisk)
|
---|
4586 | {
|
---|
4587 | size_t cnt = m->mapHardDisks.erase(id);
|
---|
4588 | Assert(cnt == 1);
|
---|
4589 | NOREF(cnt);
|
---|
4590 | }
|
---|
4591 |
|
---|
4592 | return S_OK;
|
---|
4593 | }
|
---|
4594 |
|
---|
4595 | /**
|
---|
4596 | * Little helper called from unregisterMachineMedia() to recursively add media to the given list,
|
---|
4597 | * with children appearing before their parents.
|
---|
4598 | * @param llMedia
|
---|
4599 | * @param pMedium
|
---|
4600 | */
|
---|
4601 | void VirtualBox::i_pushMediumToListWithChildren(MediaList &llMedia, Medium *pMedium)
|
---|
4602 | {
|
---|
4603 | // recurse first, then add ourselves; this way children end up on the
|
---|
4604 | // list before their parents
|
---|
4605 |
|
---|
4606 | const MediaList &llChildren = pMedium->i_getChildren();
|
---|
4607 | for (MediaList::const_iterator it = llChildren.begin();
|
---|
4608 | it != llChildren.end();
|
---|
4609 | ++it)
|
---|
4610 | {
|
---|
4611 | Medium *pChild = *it;
|
---|
4612 | i_pushMediumToListWithChildren(llMedia, pChild);
|
---|
4613 | }
|
---|
4614 |
|
---|
4615 | Log(("Pushing medium %RTuuid\n", pMedium->i_getId().raw()));
|
---|
4616 | llMedia.push_back(pMedium);
|
---|
4617 | }
|
---|
4618 |
|
---|
4619 | /**
|
---|
4620 | * Unregisters all Medium objects which belong to the given machine registry.
|
---|
4621 | * Gets called from Machine::uninit() just before the machine object dies
|
---|
4622 | * and must only be called with a machine UUID as the registry ID.
|
---|
4623 | *
|
---|
4624 | * Locks the media tree.
|
---|
4625 | *
|
---|
4626 | * @param uuidMachine Medium registry ID (always a machine UUID)
|
---|
4627 | * @return
|
---|
4628 | */
|
---|
4629 | HRESULT VirtualBox::i_unregisterMachineMedia(const Guid &uuidMachine)
|
---|
4630 | {
|
---|
4631 | Assert(!uuidMachine.isZero() && uuidMachine.isValid());
|
---|
4632 |
|
---|
4633 | LogFlowFuncEnter();
|
---|
4634 |
|
---|
4635 | AutoCaller autoCaller(this);
|
---|
4636 | AssertComRCReturnRC(autoCaller.rc());
|
---|
4637 |
|
---|
4638 | MediaList llMedia2Close;
|
---|
4639 |
|
---|
4640 | {
|
---|
4641 | AutoWriteLock tlock(i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
4642 |
|
---|
4643 | for (MediaOList::iterator it = m->allHardDisks.getList().begin();
|
---|
4644 | it != m->allHardDisks.getList().end();
|
---|
4645 | ++it)
|
---|
4646 | {
|
---|
4647 | ComObjPtr<Medium> pMedium = *it;
|
---|
4648 | AutoCaller medCaller(pMedium);
|
---|
4649 | if (FAILED(medCaller.rc())) return medCaller.rc();
|
---|
4650 | AutoReadLock medlock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
4651 |
|
---|
4652 | if (pMedium->i_isInRegistry(uuidMachine))
|
---|
4653 | // recursively with children first
|
---|
4654 | i_pushMediumToListWithChildren(llMedia2Close, pMedium);
|
---|
4655 | }
|
---|
4656 | }
|
---|
4657 |
|
---|
4658 | for (MediaList::iterator it = llMedia2Close.begin();
|
---|
4659 | it != llMedia2Close.end();
|
---|
4660 | ++it)
|
---|
4661 | {
|
---|
4662 | ComObjPtr<Medium> pMedium = *it;
|
---|
4663 | Log(("Closing medium %RTuuid\n", pMedium->i_getId().raw()));
|
---|
4664 | AutoCaller mac(pMedium);
|
---|
4665 | pMedium->i_close(mac);
|
---|
4666 | }
|
---|
4667 |
|
---|
4668 | LogFlowFuncLeave();
|
---|
4669 |
|
---|
4670 | return S_OK;
|
---|
4671 | }
|
---|
4672 |
|
---|
4673 | /**
|
---|
4674 | * Removes the given machine object from the internal list of registered machines.
|
---|
4675 | * Called from Machine::Unregister().
|
---|
4676 | * @param pMachine
|
---|
4677 | * @param id UUID of the machine. Must be passed by caller because machine may be dead by this time.
|
---|
4678 | * @return
|
---|
4679 | */
|
---|
4680 | HRESULT VirtualBox::i_unregisterMachine(Machine *pMachine,
|
---|
4681 | const Guid &id)
|
---|
4682 | {
|
---|
4683 | // remove from the collection of registered machines
|
---|
4684 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4685 | m->allMachines.removeChild(pMachine);
|
---|
4686 | // save the global registry
|
---|
4687 | HRESULT rc = i_saveSettings();
|
---|
4688 | alock.release();
|
---|
4689 |
|
---|
4690 | /*
|
---|
4691 | * Now go over all known media and checks if they were registered in the
|
---|
4692 | * media registry of the given machine. Each such medium is then moved to
|
---|
4693 | * a different media registry to make sure it doesn't get lost since its
|
---|
4694 | * media registry is about to go away.
|
---|
4695 | *
|
---|
4696 | * This fixes the following use case: Image A.vdi of machine A is also used
|
---|
4697 | * by machine B, but registered in the media registry of machine A. If machine
|
---|
4698 | * A is deleted, A.vdi must be moved to the registry of B, or else B will
|
---|
4699 | * become inaccessible.
|
---|
4700 | */
|
---|
4701 | {
|
---|
4702 | AutoReadLock tlock(i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
4703 | // iterate over the list of *base* images
|
---|
4704 | for (MediaOList::iterator it = m->allHardDisks.getList().begin();
|
---|
4705 | it != m->allHardDisks.getList().end();
|
---|
4706 | ++it)
|
---|
4707 | {
|
---|
4708 | ComObjPtr<Medium> &pMedium = *it;
|
---|
4709 | AutoCaller medCaller(pMedium);
|
---|
4710 | if (FAILED(medCaller.rc())) return medCaller.rc();
|
---|
4711 | AutoWriteLock mlock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
4712 |
|
---|
4713 | if (pMedium->i_removeRegistryRecursive(id))
|
---|
4714 | {
|
---|
4715 | // machine ID was found in base medium's registry list:
|
---|
4716 | // move this base image and all its children to another registry then
|
---|
4717 | // 1) first, find a better registry to add things to
|
---|
4718 | const Guid *puuidBetter = pMedium->i_getAnyMachineBackref();
|
---|
4719 | if (puuidBetter)
|
---|
4720 | {
|
---|
4721 | // 2) better registry found: then use that
|
---|
4722 | pMedium->i_addRegistryRecursive(*puuidBetter);
|
---|
4723 | // 3) and make sure the registry is saved below
|
---|
4724 | mlock.release();
|
---|
4725 | tlock.release();
|
---|
4726 | i_markRegistryModified(*puuidBetter);
|
---|
4727 | tlock.acquire();
|
---|
4728 | mlock.acquire();
|
---|
4729 | }
|
---|
4730 | }
|
---|
4731 | }
|
---|
4732 | }
|
---|
4733 |
|
---|
4734 | i_saveModifiedRegistries();
|
---|
4735 |
|
---|
4736 | /* fire an event */
|
---|
4737 | i_onMachineRegistered(id, FALSE);
|
---|
4738 |
|
---|
4739 | return rc;
|
---|
4740 | }
|
---|
4741 |
|
---|
4742 | /**
|
---|
4743 | * Marks the registry for @a uuid as modified, so that it's saved in a later
|
---|
4744 | * call to saveModifiedRegistries().
|
---|
4745 | *
|
---|
4746 | * @param uuid
|
---|
4747 | */
|
---|
4748 | void VirtualBox::i_markRegistryModified(const Guid &uuid)
|
---|
4749 | {
|
---|
4750 | if (uuid == i_getGlobalRegistryId())
|
---|
4751 | ASMAtomicIncU64(&m->uRegistryNeedsSaving);
|
---|
4752 | else
|
---|
4753 | {
|
---|
4754 | ComObjPtr<Machine> pMachine;
|
---|
4755 | HRESULT rc = i_findMachine(uuid,
|
---|
4756 | false /* fPermitInaccessible */,
|
---|
4757 | false /* aSetError */,
|
---|
4758 | &pMachine);
|
---|
4759 | if (SUCCEEDED(rc))
|
---|
4760 | {
|
---|
4761 | AutoCaller machineCaller(pMachine);
|
---|
4762 | if (SUCCEEDED(machineCaller.rc()))
|
---|
4763 | ASMAtomicIncU64(&pMachine->uRegistryNeedsSaving);
|
---|
4764 | }
|
---|
4765 | }
|
---|
4766 | }
|
---|
4767 |
|
---|
4768 | /**
|
---|
4769 | * Marks the registry for @a uuid as unmodified, so that it's not saved in
|
---|
4770 | * a later call to saveModifiedRegistries().
|
---|
4771 | *
|
---|
4772 | * @param uuid
|
---|
4773 | */
|
---|
4774 | void VirtualBox::i_unmarkRegistryModified(const Guid &uuid)
|
---|
4775 | {
|
---|
4776 | uint64_t uOld;
|
---|
4777 | if (uuid == i_getGlobalRegistryId())
|
---|
4778 | {
|
---|
4779 | for (;;)
|
---|
4780 | {
|
---|
4781 | uOld = ASMAtomicReadU64(&m->uRegistryNeedsSaving);
|
---|
4782 | if (!uOld)
|
---|
4783 | break;
|
---|
4784 | if (ASMAtomicCmpXchgU64(&m->uRegistryNeedsSaving, 0, uOld))
|
---|
4785 | break;
|
---|
4786 | ASMNopPause();
|
---|
4787 | }
|
---|
4788 | }
|
---|
4789 | else
|
---|
4790 | {
|
---|
4791 | ComObjPtr<Machine> pMachine;
|
---|
4792 | HRESULT rc = i_findMachine(uuid,
|
---|
4793 | false /* fPermitInaccessible */,
|
---|
4794 | false /* aSetError */,
|
---|
4795 | &pMachine);
|
---|
4796 | if (SUCCEEDED(rc))
|
---|
4797 | {
|
---|
4798 | AutoCaller machineCaller(pMachine);
|
---|
4799 | if (SUCCEEDED(machineCaller.rc()))
|
---|
4800 | {
|
---|
4801 | for (;;)
|
---|
4802 | {
|
---|
4803 | uOld = ASMAtomicReadU64(&pMachine->uRegistryNeedsSaving);
|
---|
4804 | if (!uOld)
|
---|
4805 | break;
|
---|
4806 | if (ASMAtomicCmpXchgU64(&pMachine->uRegistryNeedsSaving, 0, uOld))
|
---|
4807 | break;
|
---|
4808 | ASMNopPause();
|
---|
4809 | }
|
---|
4810 | }
|
---|
4811 | }
|
---|
4812 | }
|
---|
4813 | }
|
---|
4814 |
|
---|
4815 | /**
|
---|
4816 | * Saves all settings files according to the modified flags in the Machine
|
---|
4817 | * objects and in the VirtualBox object.
|
---|
4818 | *
|
---|
4819 | * This locks machines and the VirtualBox object as necessary, so better not
|
---|
4820 | * hold any locks before calling this.
|
---|
4821 | *
|
---|
4822 | * @return
|
---|
4823 | */
|
---|
4824 | void VirtualBox::i_saveModifiedRegistries()
|
---|
4825 | {
|
---|
4826 | HRESULT rc = S_OK;
|
---|
4827 | bool fNeedsGlobalSettings = false;
|
---|
4828 | uint64_t uOld;
|
---|
4829 |
|
---|
4830 | {
|
---|
4831 | AutoReadLock alock(m->allMachines.getLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
4832 | for (MachinesOList::iterator it = m->allMachines.begin();
|
---|
4833 | it != m->allMachines.end();
|
---|
4834 | ++it)
|
---|
4835 | {
|
---|
4836 | const ComObjPtr<Machine> &pMachine = *it;
|
---|
4837 |
|
---|
4838 | for (;;)
|
---|
4839 | {
|
---|
4840 | uOld = ASMAtomicReadU64(&pMachine->uRegistryNeedsSaving);
|
---|
4841 | if (!uOld)
|
---|
4842 | break;
|
---|
4843 | if (ASMAtomicCmpXchgU64(&pMachine->uRegistryNeedsSaving, 0, uOld))
|
---|
4844 | break;
|
---|
4845 | ASMNopPause();
|
---|
4846 | }
|
---|
4847 | if (uOld)
|
---|
4848 | {
|
---|
4849 | AutoCaller autoCaller(pMachine);
|
---|
4850 | if (FAILED(autoCaller.rc()))
|
---|
4851 | continue;
|
---|
4852 | /* object is already dead, no point in saving settings */
|
---|
4853 | if (getObjectState().getState() != ObjectState::Ready)
|
---|
4854 | continue;
|
---|
4855 | AutoWriteLock mlock(pMachine COMMA_LOCKVAL_SRC_POS);
|
---|
4856 | rc = pMachine->i_saveSettings(&fNeedsGlobalSettings,
|
---|
4857 | Machine::SaveS_Force); // caller said save, so stop arguing
|
---|
4858 | }
|
---|
4859 | }
|
---|
4860 | }
|
---|
4861 |
|
---|
4862 | for (;;)
|
---|
4863 | {
|
---|
4864 | uOld = ASMAtomicReadU64(&m->uRegistryNeedsSaving);
|
---|
4865 | if (!uOld)
|
---|
4866 | break;
|
---|
4867 | if (ASMAtomicCmpXchgU64(&m->uRegistryNeedsSaving, 0, uOld))
|
---|
4868 | break;
|
---|
4869 | ASMNopPause();
|
---|
4870 | }
|
---|
4871 | if (uOld || fNeedsGlobalSettings)
|
---|
4872 | {
|
---|
4873 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4874 | rc = i_saveSettings();
|
---|
4875 | }
|
---|
4876 | NOREF(rc); /* XXX */
|
---|
4877 | }
|
---|
4878 |
|
---|
4879 |
|
---|
4880 | /* static */
|
---|
4881 | const com::Utf8Str &VirtualBox::i_getVersionNormalized()
|
---|
4882 | {
|
---|
4883 | return sVersionNormalized;
|
---|
4884 | }
|
---|
4885 |
|
---|
4886 | /**
|
---|
4887 | * Checks if the path to the specified file exists, according to the path
|
---|
4888 | * information present in the file name. Optionally the path is created.
|
---|
4889 | *
|
---|
4890 | * Note that the given file name must contain the full path otherwise the
|
---|
4891 | * extracted relative path will be created based on the current working
|
---|
4892 | * directory which is normally unknown.
|
---|
4893 | *
|
---|
4894 | * @param strFileName Full file name which path is checked/created.
|
---|
4895 | * @param fCreate Flag if the path should be created if it doesn't exist.
|
---|
4896 | *
|
---|
4897 | * @return Extended error information on failure to check/create the path.
|
---|
4898 | */
|
---|
4899 | /* static */
|
---|
4900 | HRESULT VirtualBox::i_ensureFilePathExists(const Utf8Str &strFileName, bool fCreate)
|
---|
4901 | {
|
---|
4902 | Utf8Str strDir(strFileName);
|
---|
4903 | strDir.stripFilename();
|
---|
4904 | if (!RTDirExists(strDir.c_str()))
|
---|
4905 | {
|
---|
4906 | if (fCreate)
|
---|
4907 | {
|
---|
4908 | int vrc = RTDirCreateFullPath(strDir.c_str(), 0700);
|
---|
4909 | if (RT_FAILURE(vrc))
|
---|
4910 | return i_setErrorStaticBoth(VBOX_E_IPRT_ERROR, vrc,
|
---|
4911 | Utf8StrFmt(tr("Could not create the directory '%s' (%Rrc)"),
|
---|
4912 | strDir.c_str(),
|
---|
4913 | vrc));
|
---|
4914 | }
|
---|
4915 | else
|
---|
4916 | return i_setErrorStaticBoth(VBOX_E_IPRT_ERROR, VERR_FILE_NOT_FOUND,
|
---|
4917 | Utf8StrFmt(tr("Directory '%s' does not exist"), strDir.c_str()));
|
---|
4918 | }
|
---|
4919 |
|
---|
4920 | return S_OK;
|
---|
4921 | }
|
---|
4922 |
|
---|
4923 | const Utf8Str& VirtualBox::i_settingsFilePath()
|
---|
4924 | {
|
---|
4925 | return m->strSettingsFilePath;
|
---|
4926 | }
|
---|
4927 |
|
---|
4928 | /**
|
---|
4929 | * Returns the lock handle which protects the machines list. As opposed
|
---|
4930 | * to version 3.1 and earlier, these lists are no longer protected by the
|
---|
4931 | * VirtualBox lock, but by this more specialized lock. Mind the locking
|
---|
4932 | * order: always request this lock after the VirtualBox object lock but
|
---|
4933 | * before the locks of any machine object. See AutoLock.h.
|
---|
4934 | */
|
---|
4935 | RWLockHandle& VirtualBox::i_getMachinesListLockHandle()
|
---|
4936 | {
|
---|
4937 | return m->lockMachines;
|
---|
4938 | }
|
---|
4939 |
|
---|
4940 | /**
|
---|
4941 | * Returns the lock handle which protects the media trees (hard disks,
|
---|
4942 | * DVDs, floppies). As opposed to version 3.1 and earlier, these lists
|
---|
4943 | * are no longer protected by the VirtualBox lock, but by this more
|
---|
4944 | * specialized lock. Mind the locking order: always request this lock
|
---|
4945 | * after the VirtualBox object lock but before the locks of the media
|
---|
4946 | * objects contained in these lists. See AutoLock.h.
|
---|
4947 | */
|
---|
4948 | RWLockHandle& VirtualBox::i_getMediaTreeLockHandle()
|
---|
4949 | {
|
---|
4950 | return m->lockMedia;
|
---|
4951 | }
|
---|
4952 |
|
---|
4953 | /**
|
---|
4954 | * Thread function that handles custom events posted using #i_postEvent().
|
---|
4955 | */
|
---|
4956 | // static
|
---|
4957 | DECLCALLBACK(int) VirtualBox::AsyncEventHandler(RTTHREAD thread, void *pvUser)
|
---|
4958 | {
|
---|
4959 | LogFlowFuncEnter();
|
---|
4960 |
|
---|
4961 | AssertReturn(pvUser, VERR_INVALID_POINTER);
|
---|
4962 |
|
---|
4963 | HRESULT hr = com::Initialize();
|
---|
4964 | if (FAILED(hr))
|
---|
4965 | return VERR_COM_UNEXPECTED;
|
---|
4966 |
|
---|
4967 | int rc = VINF_SUCCESS;
|
---|
4968 |
|
---|
4969 | try
|
---|
4970 | {
|
---|
4971 | /* Create an event queue for the current thread. */
|
---|
4972 | EventQueue *pEventQueue = new EventQueue();
|
---|
4973 | AssertPtr(pEventQueue);
|
---|
4974 |
|
---|
4975 | /* Return the queue to the one who created this thread. */
|
---|
4976 | *(static_cast <EventQueue **>(pvUser)) = pEventQueue;
|
---|
4977 |
|
---|
4978 | /* signal that we're ready. */
|
---|
4979 | RTThreadUserSignal(thread);
|
---|
4980 |
|
---|
4981 | /*
|
---|
4982 | * In case of spurious wakeups causing VERR_TIMEOUTs and/or other return codes
|
---|
4983 | * we must not stop processing events and delete the pEventQueue object. This must
|
---|
4984 | * be done ONLY when we stop this loop via interruptEventQueueProcessing().
|
---|
4985 | * See @bugref{5724}.
|
---|
4986 | */
|
---|
4987 | for (;;)
|
---|
4988 | {
|
---|
4989 | rc = pEventQueue->processEventQueue(RT_INDEFINITE_WAIT);
|
---|
4990 | if (rc == VERR_INTERRUPTED)
|
---|
4991 | {
|
---|
4992 | LogFlow(("Event queue processing ended with rc=%Rrc\n", rc));
|
---|
4993 | rc = VINF_SUCCESS; /* Set success when exiting. */
|
---|
4994 | break;
|
---|
4995 | }
|
---|
4996 | }
|
---|
4997 |
|
---|
4998 | delete pEventQueue;
|
---|
4999 | }
|
---|
5000 | catch (std::bad_alloc &ba)
|
---|
5001 | {
|
---|
5002 | rc = VERR_NO_MEMORY;
|
---|
5003 | NOREF(ba);
|
---|
5004 | }
|
---|
5005 |
|
---|
5006 | com::Shutdown();
|
---|
5007 |
|
---|
5008 | LogFlowFuncLeaveRC(rc);
|
---|
5009 | return rc;
|
---|
5010 | }
|
---|
5011 |
|
---|
5012 |
|
---|
5013 | ////////////////////////////////////////////////////////////////////////////////
|
---|
5014 |
|
---|
5015 | /**
|
---|
5016 | * Prepare the event using the overwritten #prepareEventDesc method and fire.
|
---|
5017 | *
|
---|
5018 | * @note Locks the managed VirtualBox object for reading but leaves the lock
|
---|
5019 | * before iterating over callbacks and calling their methods.
|
---|
5020 | */
|
---|
5021 | void *VirtualBox::CallbackEvent::handler()
|
---|
5022 | {
|
---|
5023 | if (!mVirtualBox)
|
---|
5024 | return NULL;
|
---|
5025 |
|
---|
5026 | AutoCaller autoCaller(mVirtualBox);
|
---|
5027 | if (!autoCaller.isOk())
|
---|
5028 | {
|
---|
5029 | Log1WarningFunc(("VirtualBox has been uninitialized (state=%d), the callback event is discarded!\n",
|
---|
5030 | mVirtualBox->getObjectState().getState()));
|
---|
5031 | /* We don't need mVirtualBox any more, so release it */
|
---|
5032 | mVirtualBox = NULL;
|
---|
5033 | return NULL;
|
---|
5034 | }
|
---|
5035 |
|
---|
5036 | {
|
---|
5037 | VBoxEventDesc evDesc;
|
---|
5038 | prepareEventDesc(mVirtualBox->m->pEventSource, evDesc);
|
---|
5039 |
|
---|
5040 | evDesc.fire(/* don't wait for delivery */0);
|
---|
5041 | }
|
---|
5042 |
|
---|
5043 | mVirtualBox = NULL; /* Not needed any longer. Still make sense to do this? */
|
---|
5044 | return NULL;
|
---|
5045 | }
|
---|
5046 |
|
---|
5047 | //STDMETHODIMP VirtualBox::CreateDHCPServerForInterface(/*IHostNetworkInterface * aIinterface,*/ IDHCPServer ** aServer)
|
---|
5048 | //{
|
---|
5049 | // return E_NOTIMPL;
|
---|
5050 | //}
|
---|
5051 |
|
---|
5052 | HRESULT VirtualBox::createDHCPServer(const com::Utf8Str &aName,
|
---|
5053 | ComPtr<IDHCPServer> &aServer)
|
---|
5054 | {
|
---|
5055 | ComObjPtr<DHCPServer> dhcpServer;
|
---|
5056 | dhcpServer.createObject();
|
---|
5057 | HRESULT rc = dhcpServer->init(this, aName);
|
---|
5058 | if (FAILED(rc)) return rc;
|
---|
5059 |
|
---|
5060 | rc = i_registerDHCPServer(dhcpServer, true);
|
---|
5061 | if (FAILED(rc)) return rc;
|
---|
5062 |
|
---|
5063 | dhcpServer.queryInterfaceTo(aServer.asOutParam());
|
---|
5064 |
|
---|
5065 | return rc;
|
---|
5066 | }
|
---|
5067 |
|
---|
5068 | HRESULT VirtualBox::findDHCPServerByNetworkName(const com::Utf8Str &aName,
|
---|
5069 | ComPtr<IDHCPServer> &aServer)
|
---|
5070 | {
|
---|
5071 | HRESULT rc = S_OK;
|
---|
5072 | ComPtr<DHCPServer> found;
|
---|
5073 |
|
---|
5074 | AutoReadLock alock(m->allDHCPServers.getLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
5075 |
|
---|
5076 | for (DHCPServersOList::const_iterator it = m->allDHCPServers.begin();
|
---|
5077 | it != m->allDHCPServers.end();
|
---|
5078 | ++it)
|
---|
5079 | {
|
---|
5080 | Bstr bstrNetworkName;
|
---|
5081 | rc = (*it)->COMGETTER(NetworkName)(bstrNetworkName.asOutParam());
|
---|
5082 | if (FAILED(rc)) return rc;
|
---|
5083 |
|
---|
5084 | if (Utf8Str(bstrNetworkName) == aName)
|
---|
5085 | {
|
---|
5086 | found = *it;
|
---|
5087 | break;
|
---|
5088 | }
|
---|
5089 | }
|
---|
5090 |
|
---|
5091 | if (!found)
|
---|
5092 | return E_INVALIDARG;
|
---|
5093 |
|
---|
5094 | rc = found.queryInterfaceTo(aServer.asOutParam());
|
---|
5095 |
|
---|
5096 | return rc;
|
---|
5097 | }
|
---|
5098 |
|
---|
5099 | HRESULT VirtualBox::removeDHCPServer(const ComPtr<IDHCPServer> &aServer)
|
---|
5100 | {
|
---|
5101 | IDHCPServer *aP = aServer;
|
---|
5102 |
|
---|
5103 | HRESULT rc = i_unregisterDHCPServer(static_cast<DHCPServer *>(aP));
|
---|
5104 |
|
---|
5105 | return rc;
|
---|
5106 | }
|
---|
5107 |
|
---|
5108 | /**
|
---|
5109 | * Remembers the given DHCP server in the settings.
|
---|
5110 | *
|
---|
5111 | * @param aDHCPServer DHCP server object to remember.
|
---|
5112 | * @param aSaveSettings @c true to save settings to disk (default).
|
---|
5113 | *
|
---|
5114 | * When @a aSaveSettings is @c true, this operation may fail because of the
|
---|
5115 | * failed #i_saveSettings() method it calls. In this case, the dhcp server object
|
---|
5116 | * will not be remembered. It is therefore the responsibility of the caller to
|
---|
5117 | * call this method as the last step of some action that requires registration
|
---|
5118 | * in order to make sure that only fully functional dhcp server objects get
|
---|
5119 | * registered.
|
---|
5120 | *
|
---|
5121 | * @note Locks this object for writing and @a aDHCPServer for reading.
|
---|
5122 | */
|
---|
5123 | HRESULT VirtualBox::i_registerDHCPServer(DHCPServer *aDHCPServer,
|
---|
5124 | bool aSaveSettings /*= true*/)
|
---|
5125 | {
|
---|
5126 | AssertReturn(aDHCPServer != NULL, E_INVALIDARG);
|
---|
5127 |
|
---|
5128 | AutoCaller autoCaller(this);
|
---|
5129 | AssertComRCReturnRC(autoCaller.rc());
|
---|
5130 |
|
---|
5131 | // Acquire a lock on the VirtualBox object early to avoid lock order issues
|
---|
5132 | // when we call i_saveSettings() later on.
|
---|
5133 | AutoWriteLock vboxLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5134 | // need it below, in findDHCPServerByNetworkName (reading) and in
|
---|
5135 | // m->allDHCPServers.addChild, so need to get it here to avoid lock
|
---|
5136 | // order trouble with dhcpServerCaller
|
---|
5137 | AutoWriteLock alock(m->allDHCPServers.getLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
5138 |
|
---|
5139 | AutoCaller dhcpServerCaller(aDHCPServer);
|
---|
5140 | AssertComRCReturnRC(dhcpServerCaller.rc());
|
---|
5141 |
|
---|
5142 | Bstr bstrNetworkName;
|
---|
5143 | HRESULT rc = S_OK;
|
---|
5144 | rc = aDHCPServer->COMGETTER(NetworkName)(bstrNetworkName.asOutParam());
|
---|
5145 | if (FAILED(rc)) return rc;
|
---|
5146 |
|
---|
5147 | ComPtr<IDHCPServer> existing;
|
---|
5148 | rc = findDHCPServerByNetworkName(Utf8Str(bstrNetworkName), existing);
|
---|
5149 | if (SUCCEEDED(rc))
|
---|
5150 | return E_INVALIDARG;
|
---|
5151 | rc = S_OK;
|
---|
5152 |
|
---|
5153 | m->allDHCPServers.addChild(aDHCPServer);
|
---|
5154 | // we need to release the list lock before we attempt to acquire locks
|
---|
5155 | // on other objects in i_saveSettings (see @bugref{7500})
|
---|
5156 | alock.release();
|
---|
5157 |
|
---|
5158 | if (aSaveSettings)
|
---|
5159 | {
|
---|
5160 | // we acquired the lock on 'this' earlier to avoid lock order issues
|
---|
5161 | rc = i_saveSettings();
|
---|
5162 |
|
---|
5163 | if (FAILED(rc))
|
---|
5164 | {
|
---|
5165 | alock.acquire();
|
---|
5166 | m->allDHCPServers.removeChild(aDHCPServer);
|
---|
5167 | }
|
---|
5168 | }
|
---|
5169 |
|
---|
5170 | return rc;
|
---|
5171 | }
|
---|
5172 |
|
---|
5173 | /**
|
---|
5174 | * Removes the given DHCP server from the settings.
|
---|
5175 | *
|
---|
5176 | * @param aDHCPServer DHCP server object to remove.
|
---|
5177 | *
|
---|
5178 | * This operation may fail because of the failed #i_saveSettings() method it
|
---|
5179 | * calls. In this case, the DHCP server will NOT be removed from the settings
|
---|
5180 | * when this method returns.
|
---|
5181 | *
|
---|
5182 | * @note Locks this object for writing.
|
---|
5183 | */
|
---|
5184 | HRESULT VirtualBox::i_unregisterDHCPServer(DHCPServer *aDHCPServer)
|
---|
5185 | {
|
---|
5186 | AssertReturn(aDHCPServer != NULL, E_INVALIDARG);
|
---|
5187 |
|
---|
5188 | AutoCaller autoCaller(this);
|
---|
5189 | AssertComRCReturnRC(autoCaller.rc());
|
---|
5190 |
|
---|
5191 | AutoCaller dhcpServerCaller(aDHCPServer);
|
---|
5192 | AssertComRCReturnRC(dhcpServerCaller.rc());
|
---|
5193 |
|
---|
5194 | AutoWriteLock vboxLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5195 | AutoWriteLock alock(m->allDHCPServers.getLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
5196 | m->allDHCPServers.removeChild(aDHCPServer);
|
---|
5197 | // we need to release the list lock before we attempt to acquire locks
|
---|
5198 | // on other objects in i_saveSettings (see @bugref{7500})
|
---|
5199 | alock.release();
|
---|
5200 |
|
---|
5201 | HRESULT rc = i_saveSettings();
|
---|
5202 |
|
---|
5203 | // undo the changes if we failed to save them
|
---|
5204 | if (FAILED(rc))
|
---|
5205 | {
|
---|
5206 | alock.acquire();
|
---|
5207 | m->allDHCPServers.addChild(aDHCPServer);
|
---|
5208 | }
|
---|
5209 |
|
---|
5210 | return rc;
|
---|
5211 | }
|
---|
5212 |
|
---|
5213 |
|
---|
5214 | /**
|
---|
5215 | * NAT Network
|
---|
5216 | */
|
---|
5217 | HRESULT VirtualBox::createNATNetwork(const com::Utf8Str &aNetworkName,
|
---|
5218 | ComPtr<INATNetwork> &aNetwork)
|
---|
5219 | {
|
---|
5220 | #ifdef VBOX_WITH_NAT_SERVICE
|
---|
5221 | ComObjPtr<NATNetwork> natNetwork;
|
---|
5222 | natNetwork.createObject();
|
---|
5223 | HRESULT rc = natNetwork->init(this, aNetworkName);
|
---|
5224 | if (FAILED(rc)) return rc;
|
---|
5225 |
|
---|
5226 | rc = i_registerNATNetwork(natNetwork, true);
|
---|
5227 | if (FAILED(rc)) return rc;
|
---|
5228 |
|
---|
5229 | natNetwork.queryInterfaceTo(aNetwork.asOutParam());
|
---|
5230 |
|
---|
5231 | fireNATNetworkCreationDeletionEvent(m->pEventSource, Bstr(aNetworkName).raw(), TRUE);
|
---|
5232 |
|
---|
5233 | return rc;
|
---|
5234 | #else
|
---|
5235 | NOREF(aNetworkName);
|
---|
5236 | NOREF(aNetwork);
|
---|
5237 | return E_NOTIMPL;
|
---|
5238 | #endif
|
---|
5239 | }
|
---|
5240 |
|
---|
5241 | HRESULT VirtualBox::findNATNetworkByName(const com::Utf8Str &aNetworkName,
|
---|
5242 | ComPtr<INATNetwork> &aNetwork)
|
---|
5243 | {
|
---|
5244 | #ifdef VBOX_WITH_NAT_SERVICE
|
---|
5245 |
|
---|
5246 | HRESULT rc = S_OK;
|
---|
5247 | ComPtr<NATNetwork> found;
|
---|
5248 |
|
---|
5249 | AutoReadLock alock(m->allNATNetworks.getLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
5250 |
|
---|
5251 | for (NATNetworksOList::const_iterator it = m->allNATNetworks.begin();
|
---|
5252 | it != m->allNATNetworks.end();
|
---|
5253 | ++it)
|
---|
5254 | {
|
---|
5255 | Bstr bstrNATNetworkName;
|
---|
5256 | rc = (*it)->COMGETTER(NetworkName)(bstrNATNetworkName.asOutParam());
|
---|
5257 | if (FAILED(rc)) return rc;
|
---|
5258 |
|
---|
5259 | if (Utf8Str(bstrNATNetworkName) == aNetworkName)
|
---|
5260 | {
|
---|
5261 | found = *it;
|
---|
5262 | break;
|
---|
5263 | }
|
---|
5264 | }
|
---|
5265 |
|
---|
5266 | if (!found)
|
---|
5267 | return E_INVALIDARG;
|
---|
5268 | found.queryInterfaceTo(aNetwork.asOutParam());
|
---|
5269 | return rc;
|
---|
5270 | #else
|
---|
5271 | NOREF(aNetworkName);
|
---|
5272 | NOREF(aNetwork);
|
---|
5273 | return E_NOTIMPL;
|
---|
5274 | #endif
|
---|
5275 | }
|
---|
5276 |
|
---|
5277 | HRESULT VirtualBox::removeNATNetwork(const ComPtr<INATNetwork> &aNetwork)
|
---|
5278 | {
|
---|
5279 | #ifdef VBOX_WITH_NAT_SERVICE
|
---|
5280 | Bstr name;
|
---|
5281 | HRESULT rc = aNetwork->COMGETTER(NetworkName)(name.asOutParam());
|
---|
5282 | if (FAILED(rc))
|
---|
5283 | return rc;
|
---|
5284 | INATNetwork *p = aNetwork;
|
---|
5285 | NATNetwork *network = static_cast<NATNetwork *>(p);
|
---|
5286 | rc = i_unregisterNATNetwork(network, true);
|
---|
5287 | fireNATNetworkCreationDeletionEvent(m->pEventSource, name.raw(), FALSE);
|
---|
5288 | return rc;
|
---|
5289 | #else
|
---|
5290 | NOREF(aNetwork);
|
---|
5291 | return E_NOTIMPL;
|
---|
5292 | #endif
|
---|
5293 |
|
---|
5294 | }
|
---|
5295 | /**
|
---|
5296 | * Remembers the given NAT network in the settings.
|
---|
5297 | *
|
---|
5298 | * @param aNATNetwork NAT Network object to remember.
|
---|
5299 | * @param aSaveSettings @c true to save settings to disk (default).
|
---|
5300 | *
|
---|
5301 | *
|
---|
5302 | * @note Locks this object for writing and @a aNATNetwork for reading.
|
---|
5303 | */
|
---|
5304 | HRESULT VirtualBox::i_registerNATNetwork(NATNetwork *aNATNetwork,
|
---|
5305 | bool aSaveSettings /*= true*/)
|
---|
5306 | {
|
---|
5307 | #ifdef VBOX_WITH_NAT_SERVICE
|
---|
5308 | AssertReturn(aNATNetwork != NULL, E_INVALIDARG);
|
---|
5309 |
|
---|
5310 | AutoCaller autoCaller(this);
|
---|
5311 | AssertComRCReturnRC(autoCaller.rc());
|
---|
5312 |
|
---|
5313 | AutoCaller natNetworkCaller(aNATNetwork);
|
---|
5314 | AssertComRCReturnRC(natNetworkCaller.rc());
|
---|
5315 |
|
---|
5316 | Bstr name;
|
---|
5317 | HRESULT rc;
|
---|
5318 | rc = aNATNetwork->COMGETTER(NetworkName)(name.asOutParam());
|
---|
5319 | AssertComRCReturnRC(rc);
|
---|
5320 |
|
---|
5321 | /* returned value isn't 0 and aSaveSettings is true
|
---|
5322 | * means that we create duplicate, otherwise we just load settings.
|
---|
5323 | */
|
---|
5324 | if ( sNatNetworkNameToRefCount[name]
|
---|
5325 | && aSaveSettings)
|
---|
5326 | AssertComRCReturnRC(E_INVALIDARG);
|
---|
5327 |
|
---|
5328 | rc = S_OK;
|
---|
5329 |
|
---|
5330 | sNatNetworkNameToRefCount[name] = 0;
|
---|
5331 |
|
---|
5332 | m->allNATNetworks.addChild(aNATNetwork);
|
---|
5333 |
|
---|
5334 | if (aSaveSettings)
|
---|
5335 | {
|
---|
5336 | AutoWriteLock vboxLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5337 | rc = i_saveSettings();
|
---|
5338 | vboxLock.release();
|
---|
5339 |
|
---|
5340 | if (FAILED(rc))
|
---|
5341 | i_unregisterNATNetwork(aNATNetwork, false /* aSaveSettings */);
|
---|
5342 | }
|
---|
5343 |
|
---|
5344 | return rc;
|
---|
5345 | #else
|
---|
5346 | NOREF(aNATNetwork);
|
---|
5347 | NOREF(aSaveSettings);
|
---|
5348 | /* No panic please (silently ignore) */
|
---|
5349 | return S_OK;
|
---|
5350 | #endif
|
---|
5351 | }
|
---|
5352 |
|
---|
5353 | /**
|
---|
5354 | * Removes the given NAT network from the settings.
|
---|
5355 | *
|
---|
5356 | * @param aNATNetwork NAT network object to remove.
|
---|
5357 | * @param aSaveSettings @c true to save settings to disk (default).
|
---|
5358 | *
|
---|
5359 | * When @a aSaveSettings is @c true, this operation may fail because of the
|
---|
5360 | * failed #i_saveSettings() method it calls. In this case, the DHCP server
|
---|
5361 | * will NOT be removed from the settingsi when this method returns.
|
---|
5362 | *
|
---|
5363 | * @note Locks this object for writing.
|
---|
5364 | */
|
---|
5365 | HRESULT VirtualBox::i_unregisterNATNetwork(NATNetwork *aNATNetwork,
|
---|
5366 | bool aSaveSettings /*= true*/)
|
---|
5367 | {
|
---|
5368 | #ifdef VBOX_WITH_NAT_SERVICE
|
---|
5369 | AssertReturn(aNATNetwork != NULL, E_INVALIDARG);
|
---|
5370 |
|
---|
5371 | AutoCaller autoCaller(this);
|
---|
5372 | AssertComRCReturnRC(autoCaller.rc());
|
---|
5373 |
|
---|
5374 | AutoCaller natNetworkCaller(aNATNetwork);
|
---|
5375 | AssertComRCReturnRC(natNetworkCaller.rc());
|
---|
5376 |
|
---|
5377 | Bstr name;
|
---|
5378 | HRESULT rc = aNATNetwork->COMGETTER(NetworkName)(name.asOutParam());
|
---|
5379 | /* Hm, there're still running clients. */
|
---|
5380 | if (FAILED(rc) || sNatNetworkNameToRefCount[name])
|
---|
5381 | AssertComRCReturnRC(E_INVALIDARG);
|
---|
5382 |
|
---|
5383 | m->allNATNetworks.removeChild(aNATNetwork);
|
---|
5384 |
|
---|
5385 | if (aSaveSettings)
|
---|
5386 | {
|
---|
5387 | AutoWriteLock vboxLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5388 | rc = i_saveSettings();
|
---|
5389 | vboxLock.release();
|
---|
5390 |
|
---|
5391 | if (FAILED(rc))
|
---|
5392 | i_registerNATNetwork(aNATNetwork, false /* aSaveSettings */);
|
---|
5393 | }
|
---|
5394 |
|
---|
5395 | return rc;
|
---|
5396 | #else
|
---|
5397 | NOREF(aNATNetwork);
|
---|
5398 | NOREF(aSaveSettings);
|
---|
5399 | return E_NOTIMPL;
|
---|
5400 | #endif
|
---|
5401 | }
|
---|
5402 |
|
---|
5403 |
|
---|
5404 | #ifdef RT_OS_WINDOWS
|
---|
5405 | #include <psapi.h>
|
---|
5406 |
|
---|
5407 | /**
|
---|
5408 | * Report versions of installed drivers to release log.
|
---|
5409 | */
|
---|
5410 | void VirtualBox::i_reportDriverVersions()
|
---|
5411 | {
|
---|
5412 | /** @todo r=klaus this code is very confusing, as it uses TCHAR (and
|
---|
5413 | * randomly also _TCHAR, which sounds to me like asking for trouble),
|
---|
5414 | * the "sz" variable prefix but "%ls" for the format string - so the whole
|
---|
5415 | * thing is better compiled with UNICODE and _UNICODE defined. Would be
|
---|
5416 | * far easier to read if it would be coded explicitly for the unicode
|
---|
5417 | * case, as it won't work otherwise. */
|
---|
5418 | DWORD err;
|
---|
5419 | HRESULT hrc;
|
---|
5420 | LPVOID aDrivers[1024];
|
---|
5421 | LPVOID *pDrivers = aDrivers;
|
---|
5422 | UINT cNeeded = 0;
|
---|
5423 | TCHAR szSystemRoot[MAX_PATH];
|
---|
5424 | TCHAR *pszSystemRoot = szSystemRoot;
|
---|
5425 | LPVOID pVerInfo = NULL;
|
---|
5426 | DWORD cbVerInfo = 0;
|
---|
5427 |
|
---|
5428 | do
|
---|
5429 | {
|
---|
5430 | cNeeded = GetWindowsDirectory(szSystemRoot, RT_ELEMENTS(szSystemRoot));
|
---|
5431 | if (cNeeded == 0)
|
---|
5432 | {
|
---|
5433 | err = GetLastError();
|
---|
5434 | hrc = HRESULT_FROM_WIN32(err);
|
---|
5435 | AssertLogRelMsgFailed(("GetWindowsDirectory failed, hr=%Rhrc (0x%x) err=%u\n",
|
---|
5436 | hrc, hrc, err));
|
---|
5437 | break;
|
---|
5438 | }
|
---|
5439 | else if (cNeeded > RT_ELEMENTS(szSystemRoot))
|
---|
5440 | {
|
---|
5441 | /* The buffer is too small, allocate big one. */
|
---|
5442 | pszSystemRoot = (TCHAR *)RTMemTmpAlloc(cNeeded * sizeof(_TCHAR));
|
---|
5443 | if (!pszSystemRoot)
|
---|
5444 | {
|
---|
5445 | AssertLogRelMsgFailed(("RTMemTmpAlloc failed to allocate %d bytes\n", cNeeded));
|
---|
5446 | break;
|
---|
5447 | }
|
---|
5448 | if (GetWindowsDirectory(pszSystemRoot, cNeeded) == 0)
|
---|
5449 | {
|
---|
5450 | err = GetLastError();
|
---|
5451 | hrc = HRESULT_FROM_WIN32(err);
|
---|
5452 | AssertLogRelMsgFailed(("GetWindowsDirectory failed, hr=%Rhrc (0x%x) err=%u\n",
|
---|
5453 | hrc, hrc, err));
|
---|
5454 | break;
|
---|
5455 | }
|
---|
5456 | }
|
---|
5457 |
|
---|
5458 | DWORD cbNeeded = 0;
|
---|
5459 | if (!EnumDeviceDrivers(aDrivers, sizeof(aDrivers), &cbNeeded) || cbNeeded > sizeof(aDrivers))
|
---|
5460 | {
|
---|
5461 | pDrivers = (LPVOID *)RTMemTmpAlloc(cbNeeded);
|
---|
5462 | if (!EnumDeviceDrivers(pDrivers, cbNeeded, &cbNeeded))
|
---|
5463 | {
|
---|
5464 | err = GetLastError();
|
---|
5465 | hrc = HRESULT_FROM_WIN32(err);
|
---|
5466 | AssertLogRelMsgFailed(("EnumDeviceDrivers failed, hr=%Rhrc (0x%x) err=%u\n",
|
---|
5467 | hrc, hrc, err));
|
---|
5468 | break;
|
---|
5469 | }
|
---|
5470 | }
|
---|
5471 |
|
---|
5472 | LogRel(("Installed Drivers:\n"));
|
---|
5473 |
|
---|
5474 | TCHAR szDriver[1024];
|
---|
5475 | int cDrivers = cbNeeded / sizeof(pDrivers[0]);
|
---|
5476 | for (int i = 0; i < cDrivers; i++)
|
---|
5477 | {
|
---|
5478 | if (GetDeviceDriverBaseName(pDrivers[i], szDriver, sizeof(szDriver) / sizeof(szDriver[0])))
|
---|
5479 | {
|
---|
5480 | if (_tcsnicmp(TEXT("vbox"), szDriver, 4))
|
---|
5481 | continue;
|
---|
5482 | }
|
---|
5483 | else
|
---|
5484 | continue;
|
---|
5485 | if (GetDeviceDriverFileName(pDrivers[i], szDriver, sizeof(szDriver) / sizeof(szDriver[0])))
|
---|
5486 | {
|
---|
5487 | _TCHAR szTmpDrv[1024];
|
---|
5488 | _TCHAR *pszDrv = szDriver;
|
---|
5489 | if (!_tcsncmp(TEXT("\\SystemRoot"), szDriver, 11))
|
---|
5490 | {
|
---|
5491 | _tcscpy_s(szTmpDrv, pszSystemRoot);
|
---|
5492 | _tcsncat_s(szTmpDrv, szDriver + 11, sizeof(szTmpDrv) / sizeof(szTmpDrv[0]) - _tclen(pszSystemRoot));
|
---|
5493 | pszDrv = szTmpDrv;
|
---|
5494 | }
|
---|
5495 | else if (!_tcsncmp(TEXT("\\??\\"), szDriver, 4))
|
---|
5496 | pszDrv = szDriver + 4;
|
---|
5497 |
|
---|
5498 | /* Allocate a buffer for version info. Reuse if large enough. */
|
---|
5499 | DWORD cbNewVerInfo = GetFileVersionInfoSize(pszDrv, NULL);
|
---|
5500 | if (cbNewVerInfo > cbVerInfo)
|
---|
5501 | {
|
---|
5502 | if (pVerInfo)
|
---|
5503 | RTMemTmpFree(pVerInfo);
|
---|
5504 | cbVerInfo = cbNewVerInfo;
|
---|
5505 | pVerInfo = RTMemTmpAlloc(cbVerInfo);
|
---|
5506 | if (!pVerInfo)
|
---|
5507 | {
|
---|
5508 | AssertLogRelMsgFailed(("RTMemTmpAlloc failed to allocate %d bytes\n", cbVerInfo));
|
---|
5509 | break;
|
---|
5510 | }
|
---|
5511 | }
|
---|
5512 |
|
---|
5513 | if (GetFileVersionInfo(pszDrv, NULL, cbVerInfo, pVerInfo))
|
---|
5514 | {
|
---|
5515 | UINT cbSize = 0;
|
---|
5516 | LPBYTE lpBuffer = NULL;
|
---|
5517 | if (VerQueryValue(pVerInfo, TEXT("\\"), (VOID FAR* FAR*)&lpBuffer, &cbSize))
|
---|
5518 | {
|
---|
5519 | if (cbSize)
|
---|
5520 | {
|
---|
5521 | VS_FIXEDFILEINFO *pFileInfo = (VS_FIXEDFILEINFO *)lpBuffer;
|
---|
5522 | if (pFileInfo->dwSignature == 0xfeef04bd)
|
---|
5523 | {
|
---|
5524 | LogRel((" %ls (Version: %d.%d.%d.%d)\n", pszDrv,
|
---|
5525 | (pFileInfo->dwFileVersionMS >> 16) & 0xffff,
|
---|
5526 | (pFileInfo->dwFileVersionMS >> 0) & 0xffff,
|
---|
5527 | (pFileInfo->dwFileVersionLS >> 16) & 0xffff,
|
---|
5528 | (pFileInfo->dwFileVersionLS >> 0) & 0xffff));
|
---|
5529 | }
|
---|
5530 | }
|
---|
5531 | }
|
---|
5532 | }
|
---|
5533 | }
|
---|
5534 | }
|
---|
5535 |
|
---|
5536 | }
|
---|
5537 | while (0);
|
---|
5538 |
|
---|
5539 | if (pVerInfo)
|
---|
5540 | RTMemTmpFree(pVerInfo);
|
---|
5541 |
|
---|
5542 | if (pDrivers != aDrivers)
|
---|
5543 | RTMemTmpFree(pDrivers);
|
---|
5544 |
|
---|
5545 | if (pszSystemRoot != szSystemRoot)
|
---|
5546 | RTMemTmpFree(pszSystemRoot);
|
---|
5547 | }
|
---|
5548 | #else /* !RT_OS_WINDOWS */
|
---|
5549 | void VirtualBox::i_reportDriverVersions(void)
|
---|
5550 | {
|
---|
5551 | }
|
---|
5552 | #endif /* !RT_OS_WINDOWS */
|
---|
5553 |
|
---|
5554 | /* vi: set tabstop=4 shiftwidth=4 expandtab: */
|
---|