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