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