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