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