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