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