1 | /* $Id: VirtualBoxImpl.cpp 80862 2019-09-17 14:45:21Z 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_TRANSFERS
|
---|
49 | # include <VBox/GuestHost/SharedClipboard-transfers.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_TRANSFERS
|
---|
226 | /**
|
---|
227 | * Structure for keeping Shared Clipboard area data within the VirtualBox object.
|
---|
228 | */
|
---|
229 | struct SharedClipboardAreaData
|
---|
230 | {
|
---|
231 | SharedClipboardAreaData()
|
---|
232 | : uID(NIL_SHCLAREAID) { }
|
---|
233 |
|
---|
234 | /** The area's (unique) ID.
|
---|
235 | * Set to NIL_SHCLAREAID 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_SHCLAREAID)
|
---|
251 | , uMaxClipboardAreas(32) /** @todo Make this configurable. */
|
---|
252 | {
|
---|
253 | #ifdef DEBUG_andy
|
---|
254 | uMaxClipboardAreas = 9999;
|
---|
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_SHCLAREAID;
|
---|
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_SHCLAREAID 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_TRANSFERS */
|
---|
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_TRANSFERS
|
---|
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_TRANSFERS
|
---|
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 | | ((uint64_t)VBOX_VERSION_BUILD << 40);
|
---|
1156 |
|
---|
1157 | /** @todo This needs to be the same in OSE and non-OSE, preferrably
|
---|
1158 | * only changing when actual API changes happens. */
|
---|
1159 | uRevision |= 1;
|
---|
1160 |
|
---|
1161 | *aAPIRevision = uRevision;
|
---|
1162 |
|
---|
1163 | return S_OK;
|
---|
1164 | }
|
---|
1165 |
|
---|
1166 | HRESULT VirtualBox::getHomeFolder(com::Utf8Str &aHomeFolder)
|
---|
1167 | {
|
---|
1168 | /* mHomeDir is const and doesn't need a lock */
|
---|
1169 | aHomeFolder = m->strHomeDir;
|
---|
1170 | return S_OK;
|
---|
1171 | }
|
---|
1172 |
|
---|
1173 | HRESULT VirtualBox::getSettingsFilePath(com::Utf8Str &aSettingsFilePath)
|
---|
1174 | {
|
---|
1175 | /* mCfgFile.mName is const and doesn't need a lock */
|
---|
1176 | aSettingsFilePath = m->strSettingsFilePath;
|
---|
1177 | return S_OK;
|
---|
1178 | }
|
---|
1179 |
|
---|
1180 | HRESULT VirtualBox::getHost(ComPtr<IHost> &aHost)
|
---|
1181 | {
|
---|
1182 | /* mHost is const, no need to lock */
|
---|
1183 | m->pHost.queryInterfaceTo(aHost.asOutParam());
|
---|
1184 | return S_OK;
|
---|
1185 | }
|
---|
1186 |
|
---|
1187 | HRESULT VirtualBox::getSystemProperties(ComPtr<ISystemProperties> &aSystemProperties)
|
---|
1188 | {
|
---|
1189 | /* mSystemProperties is const, no need to lock */
|
---|
1190 | m->pSystemProperties.queryInterfaceTo(aSystemProperties.asOutParam());
|
---|
1191 | return S_OK;
|
---|
1192 | }
|
---|
1193 |
|
---|
1194 | HRESULT VirtualBox::getMachines(std::vector<ComPtr<IMachine> > &aMachines)
|
---|
1195 | {
|
---|
1196 | AutoReadLock al(m->allMachines.getLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
1197 | aMachines.resize(m->allMachines.size());
|
---|
1198 | size_t i = 0;
|
---|
1199 | for (MachinesOList::const_iterator it= m->allMachines.begin();
|
---|
1200 | it!= m->allMachines.end(); ++it, ++i)
|
---|
1201 | (*it).queryInterfaceTo(aMachines[i].asOutParam());
|
---|
1202 | return S_OK;
|
---|
1203 | }
|
---|
1204 |
|
---|
1205 | HRESULT VirtualBox::getMachineGroups(std::vector<com::Utf8Str> &aMachineGroups)
|
---|
1206 | {
|
---|
1207 | std::list<com::Utf8Str> allGroups;
|
---|
1208 |
|
---|
1209 | /* get copy of all machine references, to avoid holding the list lock */
|
---|
1210 | MachinesOList::MyList allMachines;
|
---|
1211 | {
|
---|
1212 | AutoReadLock al(m->allMachines.getLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
1213 | allMachines = m->allMachines.getList();
|
---|
1214 | }
|
---|
1215 | for (MachinesOList::MyList::const_iterator it = allMachines.begin();
|
---|
1216 | it != allMachines.end();
|
---|
1217 | ++it)
|
---|
1218 | {
|
---|
1219 | const ComObjPtr<Machine> &pMachine = *it;
|
---|
1220 | AutoCaller autoMachineCaller(pMachine);
|
---|
1221 | if (FAILED(autoMachineCaller.rc()))
|
---|
1222 | continue;
|
---|
1223 | AutoReadLock mlock(pMachine COMMA_LOCKVAL_SRC_POS);
|
---|
1224 |
|
---|
1225 | if (pMachine->i_isAccessible())
|
---|
1226 | {
|
---|
1227 | const StringsList &thisGroups = pMachine->i_getGroups();
|
---|
1228 | for (StringsList::const_iterator it2 = thisGroups.begin();
|
---|
1229 | it2 != thisGroups.end(); ++it2)
|
---|
1230 | allGroups.push_back(*it2);
|
---|
1231 | }
|
---|
1232 | }
|
---|
1233 |
|
---|
1234 | /* throw out any duplicates */
|
---|
1235 | allGroups.sort();
|
---|
1236 | allGroups.unique();
|
---|
1237 | aMachineGroups.resize(allGroups.size());
|
---|
1238 | size_t i = 0;
|
---|
1239 | for (std::list<com::Utf8Str>::const_iterator it = allGroups.begin();
|
---|
1240 | it != allGroups.end(); ++it, ++i)
|
---|
1241 | aMachineGroups[i] = (*it);
|
---|
1242 | return S_OK;
|
---|
1243 | }
|
---|
1244 |
|
---|
1245 | HRESULT VirtualBox::getHardDisks(std::vector<ComPtr<IMedium> > &aHardDisks)
|
---|
1246 | {
|
---|
1247 | AutoReadLock al(m->allHardDisks.getLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
1248 | aHardDisks.resize(m->allHardDisks.size());
|
---|
1249 | size_t i = 0;
|
---|
1250 | for (MediaOList::const_iterator it = m->allHardDisks.begin();
|
---|
1251 | it != m->allHardDisks.end(); ++it, ++i)
|
---|
1252 | (*it).queryInterfaceTo(aHardDisks[i].asOutParam());
|
---|
1253 | return S_OK;
|
---|
1254 | }
|
---|
1255 |
|
---|
1256 | HRESULT VirtualBox::getDVDImages(std::vector<ComPtr<IMedium> > &aDVDImages)
|
---|
1257 | {
|
---|
1258 | AutoReadLock al(m->allDVDImages.getLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
1259 | aDVDImages.resize(m->allDVDImages.size());
|
---|
1260 | size_t i = 0;
|
---|
1261 | for (MediaOList::const_iterator it = m->allDVDImages.begin();
|
---|
1262 | it!= m->allDVDImages.end(); ++it, ++i)
|
---|
1263 | (*it).queryInterfaceTo(aDVDImages[i].asOutParam());
|
---|
1264 | return S_OK;
|
---|
1265 | }
|
---|
1266 |
|
---|
1267 | HRESULT VirtualBox::getFloppyImages(std::vector<ComPtr<IMedium> > &aFloppyImages)
|
---|
1268 | {
|
---|
1269 | AutoReadLock al(m->allFloppyImages.getLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
1270 | aFloppyImages.resize(m->allFloppyImages.size());
|
---|
1271 | size_t i = 0;
|
---|
1272 | for (MediaOList::const_iterator it = m->allFloppyImages.begin();
|
---|
1273 | it != m->allFloppyImages.end(); ++it, ++i)
|
---|
1274 | (*it).queryInterfaceTo(aFloppyImages[i].asOutParam());
|
---|
1275 | return S_OK;
|
---|
1276 | }
|
---|
1277 |
|
---|
1278 | HRESULT VirtualBox::getProgressOperations(std::vector<ComPtr<IProgress> > &aProgressOperations)
|
---|
1279 | {
|
---|
1280 | /* protect mProgressOperations */
|
---|
1281 | AutoReadLock safeLock(m->mtxProgressOperations COMMA_LOCKVAL_SRC_POS);
|
---|
1282 | ProgressMap pmap(m->mapProgressOperations);
|
---|
1283 | aProgressOperations.resize(pmap.size());
|
---|
1284 | size_t i = 0;
|
---|
1285 | for (ProgressMap::iterator it = pmap.begin(); it != pmap.end(); ++it, ++i)
|
---|
1286 | it->second.queryInterfaceTo(aProgressOperations[i].asOutParam());
|
---|
1287 | return S_OK;
|
---|
1288 | }
|
---|
1289 |
|
---|
1290 | HRESULT VirtualBox::getGuestOSTypes(std::vector<ComPtr<IGuestOSType> > &aGuestOSTypes)
|
---|
1291 | {
|
---|
1292 | AutoReadLock al(m->allGuestOSTypes.getLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
1293 | aGuestOSTypes.resize(m->allGuestOSTypes.size());
|
---|
1294 | size_t i = 0;
|
---|
1295 | for (GuestOSTypesOList::const_iterator it = m->allGuestOSTypes.begin();
|
---|
1296 | it != m->allGuestOSTypes.end(); ++it, ++i)
|
---|
1297 | (*it).queryInterfaceTo(aGuestOSTypes[i].asOutParam());
|
---|
1298 | return S_OK;
|
---|
1299 | }
|
---|
1300 |
|
---|
1301 | HRESULT VirtualBox::getSharedFolders(std::vector<ComPtr<ISharedFolder> > &aSharedFolders)
|
---|
1302 | {
|
---|
1303 | NOREF(aSharedFolders);
|
---|
1304 |
|
---|
1305 | return setError(E_NOTIMPL, "Not yet implemented");
|
---|
1306 | }
|
---|
1307 |
|
---|
1308 | HRESULT VirtualBox::getPerformanceCollector(ComPtr<IPerformanceCollector> &aPerformanceCollector)
|
---|
1309 | {
|
---|
1310 | #ifdef VBOX_WITH_RESOURCE_USAGE_API
|
---|
1311 | /* mPerformanceCollector is const, no need to lock */
|
---|
1312 | m->pPerformanceCollector.queryInterfaceTo(aPerformanceCollector.asOutParam());
|
---|
1313 |
|
---|
1314 | return S_OK;
|
---|
1315 | #else /* !VBOX_WITH_RESOURCE_USAGE_API */
|
---|
1316 | NOREF(aPerformanceCollector);
|
---|
1317 | ReturnComNotImplemented();
|
---|
1318 | #endif /* !VBOX_WITH_RESOURCE_USAGE_API */
|
---|
1319 | }
|
---|
1320 |
|
---|
1321 | HRESULT VirtualBox::getDHCPServers(std::vector<ComPtr<IDHCPServer> > &aDHCPServers)
|
---|
1322 | {
|
---|
1323 | AutoReadLock al(m->allDHCPServers.getLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
1324 | aDHCPServers.resize(m->allDHCPServers.size());
|
---|
1325 | size_t i = 0;
|
---|
1326 | for (DHCPServersOList::const_iterator it= m->allDHCPServers.begin();
|
---|
1327 | it!= m->allDHCPServers.end(); ++it, ++i)
|
---|
1328 | (*it).queryInterfaceTo(aDHCPServers[i].asOutParam());
|
---|
1329 | return S_OK;
|
---|
1330 | }
|
---|
1331 |
|
---|
1332 |
|
---|
1333 | HRESULT VirtualBox::getNATNetworks(std::vector<ComPtr<INATNetwork> > &aNATNetworks)
|
---|
1334 | {
|
---|
1335 | #ifdef VBOX_WITH_NAT_SERVICE
|
---|
1336 | AutoReadLock al(m->allNATNetworks.getLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
1337 | aNATNetworks.resize(m->allNATNetworks.size());
|
---|
1338 | size_t i = 0;
|
---|
1339 | for (NATNetworksOList::const_iterator it= m->allNATNetworks.begin();
|
---|
1340 | it!= m->allNATNetworks.end(); ++it, ++i)
|
---|
1341 | (*it).queryInterfaceTo(aNATNetworks[i].asOutParam());
|
---|
1342 | return S_OK;
|
---|
1343 | #else
|
---|
1344 | NOREF(aNATNetworks);
|
---|
1345 | return E_NOTIMPL;
|
---|
1346 | #endif
|
---|
1347 | }
|
---|
1348 |
|
---|
1349 | HRESULT VirtualBox::getEventSource(ComPtr<IEventSource> &aEventSource)
|
---|
1350 | {
|
---|
1351 | /* event source is const, no need to lock */
|
---|
1352 | m->pEventSource.queryInterfaceTo(aEventSource.asOutParam());
|
---|
1353 | return S_OK;
|
---|
1354 | }
|
---|
1355 |
|
---|
1356 | HRESULT VirtualBox::getExtensionPackManager(ComPtr<IExtPackManager> &aExtensionPackManager)
|
---|
1357 | {
|
---|
1358 | HRESULT hrc = S_OK;
|
---|
1359 | #ifdef VBOX_WITH_EXTPACK
|
---|
1360 | /* The extension pack manager is const, no need to lock. */
|
---|
1361 | hrc = m->ptrExtPackManager.queryInterfaceTo(aExtensionPackManager.asOutParam());
|
---|
1362 | #else
|
---|
1363 | hrc = E_NOTIMPL;
|
---|
1364 | NOREF(aExtensionPackManager);
|
---|
1365 | #endif
|
---|
1366 | return hrc;
|
---|
1367 | }
|
---|
1368 |
|
---|
1369 | HRESULT VirtualBox::getInternalNetworks(std::vector<com::Utf8Str> &aInternalNetworks)
|
---|
1370 | {
|
---|
1371 | std::list<com::Utf8Str> allInternalNetworks;
|
---|
1372 |
|
---|
1373 | /* get copy of all machine references, to avoid holding the list lock */
|
---|
1374 | MachinesOList::MyList allMachines;
|
---|
1375 | {
|
---|
1376 | AutoReadLock al(m->allMachines.getLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
1377 | allMachines = m->allMachines.getList();
|
---|
1378 | }
|
---|
1379 | for (MachinesOList::MyList::const_iterator it = allMachines.begin();
|
---|
1380 | it != allMachines.end(); ++it)
|
---|
1381 | {
|
---|
1382 | const ComObjPtr<Machine> &pMachine = *it;
|
---|
1383 | AutoCaller autoMachineCaller(pMachine);
|
---|
1384 | if (FAILED(autoMachineCaller.rc()))
|
---|
1385 | continue;
|
---|
1386 | AutoReadLock mlock(pMachine COMMA_LOCKVAL_SRC_POS);
|
---|
1387 |
|
---|
1388 | if (pMachine->i_isAccessible())
|
---|
1389 | {
|
---|
1390 | uint32_t cNetworkAdapters = Global::getMaxNetworkAdapters(pMachine->i_getChipsetType());
|
---|
1391 | for (ULONG i = 0; i < cNetworkAdapters; i++)
|
---|
1392 | {
|
---|
1393 | ComPtr<INetworkAdapter> pNet;
|
---|
1394 | HRESULT rc = pMachine->GetNetworkAdapter(i, pNet.asOutParam());
|
---|
1395 | if (FAILED(rc) || pNet.isNull())
|
---|
1396 | continue;
|
---|
1397 | Bstr strInternalNetwork;
|
---|
1398 | rc = pNet->COMGETTER(InternalNetwork)(strInternalNetwork.asOutParam());
|
---|
1399 | if (FAILED(rc) || strInternalNetwork.isEmpty())
|
---|
1400 | continue;
|
---|
1401 |
|
---|
1402 | allInternalNetworks.push_back(Utf8Str(strInternalNetwork));
|
---|
1403 | }
|
---|
1404 | }
|
---|
1405 | }
|
---|
1406 |
|
---|
1407 | /* throw out any duplicates */
|
---|
1408 | allInternalNetworks.sort();
|
---|
1409 | allInternalNetworks.unique();
|
---|
1410 | size_t i = 0;
|
---|
1411 | aInternalNetworks.resize(allInternalNetworks.size());
|
---|
1412 | for (std::list<com::Utf8Str>::const_iterator it = allInternalNetworks.begin();
|
---|
1413 | it != allInternalNetworks.end();
|
---|
1414 | ++it, ++i)
|
---|
1415 | aInternalNetworks[i] = *it;
|
---|
1416 | return S_OK;
|
---|
1417 | }
|
---|
1418 |
|
---|
1419 | HRESULT VirtualBox::getGenericNetworkDrivers(std::vector<com::Utf8Str> &aGenericNetworkDrivers)
|
---|
1420 | {
|
---|
1421 | std::list<com::Utf8Str> allGenericNetworkDrivers;
|
---|
1422 |
|
---|
1423 | /* get copy of all machine references, to avoid holding the list lock */
|
---|
1424 | MachinesOList::MyList allMachines;
|
---|
1425 | {
|
---|
1426 | AutoReadLock al(m->allMachines.getLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
1427 | allMachines = m->allMachines.getList();
|
---|
1428 | }
|
---|
1429 | for (MachinesOList::MyList::const_iterator it = allMachines.begin();
|
---|
1430 | it != allMachines.end();
|
---|
1431 | ++it)
|
---|
1432 | {
|
---|
1433 | const ComObjPtr<Machine> &pMachine = *it;
|
---|
1434 | AutoCaller autoMachineCaller(pMachine);
|
---|
1435 | if (FAILED(autoMachineCaller.rc()))
|
---|
1436 | continue;
|
---|
1437 | AutoReadLock mlock(pMachine COMMA_LOCKVAL_SRC_POS);
|
---|
1438 |
|
---|
1439 | if (pMachine->i_isAccessible())
|
---|
1440 | {
|
---|
1441 | uint32_t cNetworkAdapters = Global::getMaxNetworkAdapters(pMachine->i_getChipsetType());
|
---|
1442 | for (ULONG i = 0; i < cNetworkAdapters; i++)
|
---|
1443 | {
|
---|
1444 | ComPtr<INetworkAdapter> pNet;
|
---|
1445 | HRESULT rc = pMachine->GetNetworkAdapter(i, pNet.asOutParam());
|
---|
1446 | if (FAILED(rc) || pNet.isNull())
|
---|
1447 | continue;
|
---|
1448 | Bstr strGenericNetworkDriver;
|
---|
1449 | rc = pNet->COMGETTER(GenericDriver)(strGenericNetworkDriver.asOutParam());
|
---|
1450 | if (FAILED(rc) || strGenericNetworkDriver.isEmpty())
|
---|
1451 | continue;
|
---|
1452 |
|
---|
1453 | allGenericNetworkDrivers.push_back(Utf8Str(strGenericNetworkDriver).c_str());
|
---|
1454 | }
|
---|
1455 | }
|
---|
1456 | }
|
---|
1457 |
|
---|
1458 | /* throw out any duplicates */
|
---|
1459 | allGenericNetworkDrivers.sort();
|
---|
1460 | allGenericNetworkDrivers.unique();
|
---|
1461 | aGenericNetworkDrivers.resize(allGenericNetworkDrivers.size());
|
---|
1462 | size_t i = 0;
|
---|
1463 | for (std::list<com::Utf8Str>::const_iterator it = allGenericNetworkDrivers.begin();
|
---|
1464 | it != allGenericNetworkDrivers.end(); ++it, ++i)
|
---|
1465 | aGenericNetworkDrivers[i] = *it;
|
---|
1466 |
|
---|
1467 | return S_OK;
|
---|
1468 | }
|
---|
1469 |
|
---|
1470 | HRESULT VirtualBox::getCloudProviderManager(ComPtr<ICloudProviderManager> &aCloudProviderManager)
|
---|
1471 | {
|
---|
1472 | HRESULT hrc = m->pCloudProviderManager.queryInterfaceTo(aCloudProviderManager.asOutParam());
|
---|
1473 | return hrc;
|
---|
1474 | }
|
---|
1475 |
|
---|
1476 | HRESULT VirtualBox::checkFirmwarePresent(FirmwareType_T aFirmwareType,
|
---|
1477 | const com::Utf8Str &aVersion,
|
---|
1478 | com::Utf8Str &aUrl,
|
---|
1479 | com::Utf8Str &aFile,
|
---|
1480 | BOOL *aResult)
|
---|
1481 | {
|
---|
1482 | NOREF(aVersion);
|
---|
1483 |
|
---|
1484 | static const struct
|
---|
1485 | {
|
---|
1486 | FirmwareType_T type;
|
---|
1487 | const char* fileName;
|
---|
1488 | const char* url;
|
---|
1489 | }
|
---|
1490 | firmwareDesc[] =
|
---|
1491 | {
|
---|
1492 | {
|
---|
1493 | /* compiled-in firmware */
|
---|
1494 | FirmwareType_BIOS, NULL, NULL
|
---|
1495 | },
|
---|
1496 | {
|
---|
1497 | FirmwareType_EFI32, "VBoxEFI32.fd", "http://virtualbox.org/firmware/VBoxEFI32.fd"
|
---|
1498 | },
|
---|
1499 | {
|
---|
1500 | FirmwareType_EFI64, "VBoxEFI64.fd", "http://virtualbox.org/firmware/VBoxEFI64.fd"
|
---|
1501 | },
|
---|
1502 | {
|
---|
1503 | FirmwareType_EFIDUAL, "VBoxEFIDual.fd", "http://virtualbox.org/firmware/VBoxEFIDual.fd"
|
---|
1504 | }
|
---|
1505 | };
|
---|
1506 |
|
---|
1507 | for (size_t i = 0; i < sizeof(firmwareDesc) / sizeof(firmwareDesc[0]); i++)
|
---|
1508 | {
|
---|
1509 | if (aFirmwareType != firmwareDesc[i].type)
|
---|
1510 | continue;
|
---|
1511 |
|
---|
1512 | /* compiled-in firmware */
|
---|
1513 | if (firmwareDesc[i].fileName == NULL)
|
---|
1514 | {
|
---|
1515 | *aResult = TRUE;
|
---|
1516 | break;
|
---|
1517 | }
|
---|
1518 |
|
---|
1519 | Utf8Str shortName, fullName;
|
---|
1520 |
|
---|
1521 | shortName = Utf8StrFmt("Firmware%c%s",
|
---|
1522 | RTPATH_DELIMITER,
|
---|
1523 | firmwareDesc[i].fileName);
|
---|
1524 | int rc = i_calculateFullPath(shortName, fullName);
|
---|
1525 | AssertRCReturn(rc, VBOX_E_IPRT_ERROR);
|
---|
1526 | if (RTFileExists(fullName.c_str()))
|
---|
1527 | {
|
---|
1528 | *aResult = TRUE;
|
---|
1529 | aFile = fullName;
|
---|
1530 | break;
|
---|
1531 | }
|
---|
1532 |
|
---|
1533 | char pszVBoxPath[RTPATH_MAX];
|
---|
1534 | rc = RTPathExecDir(pszVBoxPath, RTPATH_MAX);
|
---|
1535 | AssertRCReturn(rc, VBOX_E_IPRT_ERROR);
|
---|
1536 | fullName = Utf8StrFmt("%s%c%s",
|
---|
1537 | pszVBoxPath,
|
---|
1538 | RTPATH_DELIMITER,
|
---|
1539 | firmwareDesc[i].fileName);
|
---|
1540 | if (RTFileExists(fullName.c_str()))
|
---|
1541 | {
|
---|
1542 | *aResult = TRUE;
|
---|
1543 | aFile = fullName;
|
---|
1544 | break;
|
---|
1545 | }
|
---|
1546 |
|
---|
1547 | /** @todo account for version in the URL */
|
---|
1548 | aUrl = firmwareDesc[i].url;
|
---|
1549 | *aResult = FALSE;
|
---|
1550 |
|
---|
1551 | /* Assume single record per firmware type */
|
---|
1552 | break;
|
---|
1553 | }
|
---|
1554 |
|
---|
1555 | return S_OK;
|
---|
1556 | }
|
---|
1557 | // Wrapped IVirtualBox methods
|
---|
1558 | /////////////////////////////////////////////////////////////////////////////
|
---|
1559 |
|
---|
1560 | /* Helper for VirtualBox::ComposeMachineFilename */
|
---|
1561 | static void sanitiseMachineFilename(Utf8Str &aName);
|
---|
1562 |
|
---|
1563 | HRESULT VirtualBox::composeMachineFilename(const com::Utf8Str &aName,
|
---|
1564 | const com::Utf8Str &aGroup,
|
---|
1565 | const com::Utf8Str &aCreateFlags,
|
---|
1566 | const com::Utf8Str &aBaseFolder,
|
---|
1567 | com::Utf8Str &aFile)
|
---|
1568 | {
|
---|
1569 | if (RT_UNLIKELY(aName.isEmpty()))
|
---|
1570 | return setError(E_INVALIDARG, tr("Machine name is invalid, must not be empty"));
|
---|
1571 |
|
---|
1572 | Utf8Str strBase = aBaseFolder;
|
---|
1573 | Utf8Str strName = aName;
|
---|
1574 |
|
---|
1575 | LogFlowThisFunc(("aName=\"%s\",aBaseFolder=\"%s\"\n", strName.c_str(), strBase.c_str()));
|
---|
1576 |
|
---|
1577 | com::Guid id;
|
---|
1578 | bool fDirectoryIncludesUUID = false;
|
---|
1579 | if (!aCreateFlags.isEmpty())
|
---|
1580 | {
|
---|
1581 | size_t uPos = 0;
|
---|
1582 | com::Utf8Str strKey;
|
---|
1583 | com::Utf8Str strValue;
|
---|
1584 | while ((uPos = aCreateFlags.parseKeyValue(strKey, strValue, uPos)) != com::Utf8Str::npos)
|
---|
1585 | {
|
---|
1586 | if (strKey == "UUID")
|
---|
1587 | id = strValue.c_str();
|
---|
1588 | else if (strKey == "directoryIncludesUUID")
|
---|
1589 | fDirectoryIncludesUUID = (strValue == "1");
|
---|
1590 | }
|
---|
1591 | }
|
---|
1592 |
|
---|
1593 | if (id.isZero())
|
---|
1594 | fDirectoryIncludesUUID = false;
|
---|
1595 | else if (!id.isValid())
|
---|
1596 | {
|
---|
1597 | /* do something else */
|
---|
1598 | return setError(E_INVALIDARG,
|
---|
1599 | tr("'%s' is not a valid Guid"),
|
---|
1600 | id.toStringCurly().c_str());
|
---|
1601 | }
|
---|
1602 |
|
---|
1603 | Utf8Str strGroup(aGroup);
|
---|
1604 | if (strGroup.isEmpty())
|
---|
1605 | strGroup = "/";
|
---|
1606 | HRESULT rc = i_validateMachineGroup(strGroup, true);
|
---|
1607 | if (FAILED(rc))
|
---|
1608 | return rc;
|
---|
1609 |
|
---|
1610 | /* Compose the settings file name using the following scheme:
|
---|
1611 | *
|
---|
1612 | * <base_folder><group>/<machine_name>/<machine_name>.xml
|
---|
1613 | *
|
---|
1614 | * If a non-null and non-empty base folder is specified, the default
|
---|
1615 | * machine folder will be used as a base folder.
|
---|
1616 | * We sanitise the machine name to a safe white list of characters before
|
---|
1617 | * using it.
|
---|
1618 | */
|
---|
1619 | Utf8Str strDirName(strName);
|
---|
1620 | if (fDirectoryIncludesUUID)
|
---|
1621 | strDirName += Utf8StrFmt(" (%RTuuid)", id.raw());
|
---|
1622 | sanitiseMachineFilename(strName);
|
---|
1623 | sanitiseMachineFilename(strDirName);
|
---|
1624 |
|
---|
1625 | if (strBase.isEmpty())
|
---|
1626 | /* we use the non-full folder value below to keep the path relative */
|
---|
1627 | i_getDefaultMachineFolder(strBase);
|
---|
1628 |
|
---|
1629 | i_calculateFullPath(strBase, strBase);
|
---|
1630 |
|
---|
1631 | /* eliminate toplevel group to avoid // in the result */
|
---|
1632 | if (strGroup == "/")
|
---|
1633 | strGroup.setNull();
|
---|
1634 | aFile = com::Utf8StrFmt("%s%s%c%s%c%s.vbox",
|
---|
1635 | strBase.c_str(),
|
---|
1636 | strGroup.c_str(),
|
---|
1637 | RTPATH_DELIMITER,
|
---|
1638 | strDirName.c_str(),
|
---|
1639 | RTPATH_DELIMITER,
|
---|
1640 | strName.c_str());
|
---|
1641 | return S_OK;
|
---|
1642 | }
|
---|
1643 |
|
---|
1644 | /**
|
---|
1645 | * Remove characters from a machine file name which can be problematic on
|
---|
1646 | * particular systems.
|
---|
1647 | * @param strName The file name to sanitise.
|
---|
1648 | */
|
---|
1649 | void sanitiseMachineFilename(Utf8Str &strName)
|
---|
1650 | {
|
---|
1651 | if (strName.isEmpty())
|
---|
1652 | return;
|
---|
1653 |
|
---|
1654 | /* Set of characters which should be safe for use in filenames: some basic
|
---|
1655 | * ASCII, Unicode from Latin-1 alphabetic to the end of Hangul. We try to
|
---|
1656 | * skip anything that could count as a control character in Windows or
|
---|
1657 | * *nix, or be otherwise difficult for shells to handle (I would have
|
---|
1658 | * preferred to remove the space and brackets too). We also remove all
|
---|
1659 | * characters which need UTF-16 surrogate pairs for Windows's benefit.
|
---|
1660 | */
|
---|
1661 | static RTUNICP const s_uszValidRangePairs[] =
|
---|
1662 | {
|
---|
1663 | ' ', ' ',
|
---|
1664 | '(', ')',
|
---|
1665 | '-', '.',
|
---|
1666 | '0', '9',
|
---|
1667 | 'A', 'Z',
|
---|
1668 | 'a', 'z',
|
---|
1669 | '_', '_',
|
---|
1670 | 0xa0, 0xd7af,
|
---|
1671 | '\0'
|
---|
1672 | };
|
---|
1673 |
|
---|
1674 | char *pszName = strName.mutableRaw();
|
---|
1675 | ssize_t cReplacements = RTStrPurgeComplementSet(pszName, s_uszValidRangePairs, '_');
|
---|
1676 | Assert(cReplacements >= 0);
|
---|
1677 | NOREF(cReplacements);
|
---|
1678 |
|
---|
1679 | /* No leading dot or dash. */
|
---|
1680 | if (pszName[0] == '.' || pszName[0] == '-')
|
---|
1681 | pszName[0] = '_';
|
---|
1682 |
|
---|
1683 | /* No trailing dot. */
|
---|
1684 | if (pszName[strName.length() - 1] == '.')
|
---|
1685 | pszName[strName.length() - 1] = '_';
|
---|
1686 |
|
---|
1687 | /* Mangle leading and trailing spaces. */
|
---|
1688 | for (size_t i = 0; pszName[i] == ' '; ++i)
|
---|
1689 | pszName[i] = '_';
|
---|
1690 | for (size_t i = strName.length() - 1; i && pszName[i] == ' '; --i)
|
---|
1691 | pszName[i] = '_';
|
---|
1692 | }
|
---|
1693 |
|
---|
1694 | #ifdef DEBUG
|
---|
1695 | /** Simple unit test/operation examples for sanitiseMachineFilename(). */
|
---|
1696 | static unsigned testSanitiseMachineFilename(DECLCALLBACKMEMBER(void, pfnPrintf)(const char *, ...))
|
---|
1697 | {
|
---|
1698 | unsigned cErrors = 0;
|
---|
1699 |
|
---|
1700 | /** Expected results of sanitising given file names. */
|
---|
1701 | static struct
|
---|
1702 | {
|
---|
1703 | /** The test file name to be sanitised (Utf-8). */
|
---|
1704 | const char *pcszIn;
|
---|
1705 | /** The expected sanitised output (Utf-8). */
|
---|
1706 | const char *pcszOutExpected;
|
---|
1707 | } aTest[] =
|
---|
1708 | {
|
---|
1709 | { "OS/2 2.1", "OS_2 2.1" },
|
---|
1710 | { "-!My VM!-", "__My VM_-" },
|
---|
1711 | { "\xF0\x90\x8C\xB0", "____" },
|
---|
1712 | { " My VM ", "__My VM__" },
|
---|
1713 | { ".My VM.", "_My VM_" },
|
---|
1714 | { "My VM", "My VM" }
|
---|
1715 | };
|
---|
1716 | for (unsigned i = 0; i < RT_ELEMENTS(aTest); ++i)
|
---|
1717 | {
|
---|
1718 | Utf8Str str(aTest[i].pcszIn);
|
---|
1719 | sanitiseMachineFilename(str);
|
---|
1720 | if (str.compare(aTest[i].pcszOutExpected))
|
---|
1721 | {
|
---|
1722 | ++cErrors;
|
---|
1723 | pfnPrintf("%s: line %d, expected %s, actual %s\n",
|
---|
1724 | __PRETTY_FUNCTION__, i, aTest[i].pcszOutExpected,
|
---|
1725 | str.c_str());
|
---|
1726 | }
|
---|
1727 | }
|
---|
1728 | return cErrors;
|
---|
1729 | }
|
---|
1730 |
|
---|
1731 | /** @todo Proper testcase. */
|
---|
1732 | /** @todo Do we have a better method of doing init functions? */
|
---|
1733 | namespace
|
---|
1734 | {
|
---|
1735 | class TestSanitiseMachineFilename
|
---|
1736 | {
|
---|
1737 | public:
|
---|
1738 | TestSanitiseMachineFilename(void)
|
---|
1739 | {
|
---|
1740 | Assert(!testSanitiseMachineFilename(RTAssertMsg2));
|
---|
1741 | }
|
---|
1742 | };
|
---|
1743 | TestSanitiseMachineFilename s_TestSanitiseMachineFilename;
|
---|
1744 | }
|
---|
1745 | #endif
|
---|
1746 |
|
---|
1747 | /** @note Locks mSystemProperties object for reading. */
|
---|
1748 | HRESULT VirtualBox::createMachine(const com::Utf8Str &aSettingsFile,
|
---|
1749 | const com::Utf8Str &aName,
|
---|
1750 | const std::vector<com::Utf8Str> &aGroups,
|
---|
1751 | const com::Utf8Str &aOsTypeId,
|
---|
1752 | const com::Utf8Str &aFlags,
|
---|
1753 | ComPtr<IMachine> &aMachine)
|
---|
1754 | {
|
---|
1755 | LogFlowThisFuncEnter();
|
---|
1756 | LogFlowThisFunc(("aSettingsFile=\"%s\", aName=\"%s\", aOsTypeId =\"%s\", aCreateFlags=\"%s\"\n",
|
---|
1757 | aSettingsFile.c_str(), aName.c_str(), aOsTypeId.c_str(), aFlags.c_str()));
|
---|
1758 |
|
---|
1759 | StringsList llGroups;
|
---|
1760 | HRESULT rc = i_convertMachineGroups(aGroups, &llGroups);
|
---|
1761 | if (FAILED(rc))
|
---|
1762 | return rc;
|
---|
1763 |
|
---|
1764 | Utf8Str strCreateFlags(aFlags);
|
---|
1765 | Guid id;
|
---|
1766 | bool fForceOverwrite = false;
|
---|
1767 | bool fDirectoryIncludesUUID = false;
|
---|
1768 | if (!strCreateFlags.isEmpty())
|
---|
1769 | {
|
---|
1770 | const char *pcszNext = strCreateFlags.c_str();
|
---|
1771 | while (*pcszNext != '\0')
|
---|
1772 | {
|
---|
1773 | Utf8Str strFlag;
|
---|
1774 | const char *pcszComma = RTStrStr(pcszNext, ",");
|
---|
1775 | if (!pcszComma)
|
---|
1776 | strFlag = pcszNext;
|
---|
1777 | else
|
---|
1778 | strFlag = Utf8Str(pcszNext, pcszComma - pcszNext);
|
---|
1779 |
|
---|
1780 | const char *pcszEqual = RTStrStr(strFlag.c_str(), "=");
|
---|
1781 | /* skip over everything which doesn't contain '=' */
|
---|
1782 | if (pcszEqual && pcszEqual != strFlag.c_str())
|
---|
1783 | {
|
---|
1784 | Utf8Str strKey(strFlag.c_str(), pcszEqual - strFlag.c_str());
|
---|
1785 | Utf8Str strValue(strFlag.c_str() + (pcszEqual - strFlag.c_str() + 1));
|
---|
1786 |
|
---|
1787 | if (strKey == "UUID")
|
---|
1788 | id = strValue.c_str();
|
---|
1789 | else if (strKey == "forceOverwrite")
|
---|
1790 | fForceOverwrite = (strValue == "1");
|
---|
1791 | else if (strKey == "directoryIncludesUUID")
|
---|
1792 | fDirectoryIncludesUUID = (strValue == "1");
|
---|
1793 | }
|
---|
1794 |
|
---|
1795 | if (!pcszComma)
|
---|
1796 | pcszNext += strFlag.length();
|
---|
1797 | else
|
---|
1798 | pcszNext += strFlag.length() + 1;
|
---|
1799 | }
|
---|
1800 | }
|
---|
1801 | /* Create UUID if none was specified. */
|
---|
1802 | if (id.isZero())
|
---|
1803 | id.create();
|
---|
1804 | else if (!id.isValid())
|
---|
1805 | {
|
---|
1806 | /* do something else */
|
---|
1807 | return setError(E_INVALIDARG,
|
---|
1808 | tr("'%s' is not a valid Guid"),
|
---|
1809 | id.toStringCurly().c_str());
|
---|
1810 | }
|
---|
1811 |
|
---|
1812 | /* NULL settings file means compose automatically */
|
---|
1813 | Utf8Str strSettingsFile(aSettingsFile);
|
---|
1814 | if (strSettingsFile.isEmpty())
|
---|
1815 | {
|
---|
1816 | Utf8Str strNewCreateFlags(Utf8StrFmt("UUID=%RTuuid", id.raw()));
|
---|
1817 | if (fDirectoryIncludesUUID)
|
---|
1818 | strNewCreateFlags += ",directoryIncludesUUID=1";
|
---|
1819 |
|
---|
1820 | com::Utf8Str blstr = "";
|
---|
1821 | rc = composeMachineFilename(aName,
|
---|
1822 | llGroups.front(),
|
---|
1823 | strNewCreateFlags,
|
---|
1824 | blstr /* aBaseFolder */,
|
---|
1825 | strSettingsFile);
|
---|
1826 | if (FAILED(rc)) return rc;
|
---|
1827 | }
|
---|
1828 |
|
---|
1829 | /* create a new object */
|
---|
1830 | ComObjPtr<Machine> machine;
|
---|
1831 | rc = machine.createObject();
|
---|
1832 | if (FAILED(rc)) return rc;
|
---|
1833 |
|
---|
1834 | ComObjPtr<GuestOSType> osType;
|
---|
1835 | if (!aOsTypeId.isEmpty())
|
---|
1836 | i_findGuestOSType(aOsTypeId, osType);
|
---|
1837 |
|
---|
1838 | /* initialize the machine object */
|
---|
1839 | rc = machine->init(this,
|
---|
1840 | strSettingsFile,
|
---|
1841 | aName,
|
---|
1842 | llGroups,
|
---|
1843 | aOsTypeId,
|
---|
1844 | osType,
|
---|
1845 | id,
|
---|
1846 | fForceOverwrite,
|
---|
1847 | fDirectoryIncludesUUID);
|
---|
1848 | if (SUCCEEDED(rc))
|
---|
1849 | {
|
---|
1850 | /* set the return value */
|
---|
1851 | machine.queryInterfaceTo(aMachine.asOutParam());
|
---|
1852 | AssertComRC(rc);
|
---|
1853 |
|
---|
1854 | #ifdef VBOX_WITH_EXTPACK
|
---|
1855 | /* call the extension pack hooks */
|
---|
1856 | m->ptrExtPackManager->i_callAllVmCreatedHooks(machine);
|
---|
1857 | #endif
|
---|
1858 | }
|
---|
1859 |
|
---|
1860 | LogFlowThisFuncLeave();
|
---|
1861 |
|
---|
1862 | return rc;
|
---|
1863 | }
|
---|
1864 |
|
---|
1865 | HRESULT VirtualBox::openMachine(const com::Utf8Str &aSettingsFile,
|
---|
1866 | ComPtr<IMachine> &aMachine)
|
---|
1867 | {
|
---|
1868 | HRESULT rc = E_FAIL;
|
---|
1869 |
|
---|
1870 | /* create a new object */
|
---|
1871 | ComObjPtr<Machine> machine;
|
---|
1872 | rc = machine.createObject();
|
---|
1873 | if (SUCCEEDED(rc))
|
---|
1874 | {
|
---|
1875 | /* initialize the machine object */
|
---|
1876 | rc = machine->initFromSettings(this,
|
---|
1877 | aSettingsFile,
|
---|
1878 | NULL); /* const Guid *aId */
|
---|
1879 | if (SUCCEEDED(rc))
|
---|
1880 | {
|
---|
1881 | /* set the return value */
|
---|
1882 | machine.queryInterfaceTo(aMachine.asOutParam());
|
---|
1883 | ComAssertComRC(rc);
|
---|
1884 | }
|
---|
1885 | }
|
---|
1886 |
|
---|
1887 | return rc;
|
---|
1888 | }
|
---|
1889 |
|
---|
1890 | /** @note Locks objects! */
|
---|
1891 | HRESULT VirtualBox::registerMachine(const ComPtr<IMachine> &aMachine)
|
---|
1892 | {
|
---|
1893 | HRESULT rc;
|
---|
1894 |
|
---|
1895 | Bstr name;
|
---|
1896 | rc = aMachine->COMGETTER(Name)(name.asOutParam());
|
---|
1897 | if (FAILED(rc)) return rc;
|
---|
1898 |
|
---|
1899 | /* We can safely cast child to Machine * here because only Machine
|
---|
1900 | * implementations of IMachine can be among our children. */
|
---|
1901 | IMachine *aM = aMachine;
|
---|
1902 | Machine *pMachine = static_cast<Machine*>(aM);
|
---|
1903 |
|
---|
1904 | AutoCaller machCaller(pMachine);
|
---|
1905 | ComAssertComRCRetRC(machCaller.rc());
|
---|
1906 |
|
---|
1907 | rc = i_registerMachine(pMachine);
|
---|
1908 | /* fire an event */
|
---|
1909 | if (SUCCEEDED(rc))
|
---|
1910 | i_onMachineRegistered(pMachine->i_getId(), TRUE);
|
---|
1911 |
|
---|
1912 | return rc;
|
---|
1913 | }
|
---|
1914 |
|
---|
1915 | /** @note Locks this object for reading, then some machine objects for reading. */
|
---|
1916 | HRESULT VirtualBox::findMachine(const com::Utf8Str &aSettingsFile,
|
---|
1917 | ComPtr<IMachine> &aMachine)
|
---|
1918 | {
|
---|
1919 | LogFlowThisFuncEnter();
|
---|
1920 | LogFlowThisFunc(("aSettingsFile=\"%s\", aMachine={%p}\n", aSettingsFile.c_str(), &aMachine));
|
---|
1921 |
|
---|
1922 | /* start with not found */
|
---|
1923 | HRESULT rc = S_OK;
|
---|
1924 | ComObjPtr<Machine> pMachineFound;
|
---|
1925 |
|
---|
1926 | Guid id(aSettingsFile);
|
---|
1927 | Utf8Str strFile(aSettingsFile);
|
---|
1928 | if (id.isValid() && !id.isZero())
|
---|
1929 |
|
---|
1930 | rc = i_findMachine(id,
|
---|
1931 | true /* fPermitInaccessible */,
|
---|
1932 | true /* setError */,
|
---|
1933 | &pMachineFound);
|
---|
1934 | // returns VBOX_E_OBJECT_NOT_FOUND if not found and sets error
|
---|
1935 | else
|
---|
1936 | {
|
---|
1937 | rc = i_findMachineByName(strFile,
|
---|
1938 | true /* setError */,
|
---|
1939 | &pMachineFound);
|
---|
1940 | // returns VBOX_E_OBJECT_NOT_FOUND if not found and sets error
|
---|
1941 | }
|
---|
1942 |
|
---|
1943 | /* this will set (*machine) to NULL if machineObj is null */
|
---|
1944 | pMachineFound.queryInterfaceTo(aMachine.asOutParam());
|
---|
1945 |
|
---|
1946 | LogFlowThisFunc(("aName=\"%s\", aMachine=%p, rc=%08X\n", aSettingsFile.c_str(), &aMachine, rc));
|
---|
1947 | LogFlowThisFuncLeave();
|
---|
1948 |
|
---|
1949 | return rc;
|
---|
1950 | }
|
---|
1951 |
|
---|
1952 | HRESULT VirtualBox::getMachinesByGroups(const std::vector<com::Utf8Str> &aGroups,
|
---|
1953 | std::vector<ComPtr<IMachine> > &aMachines)
|
---|
1954 | {
|
---|
1955 | StringsList llGroups;
|
---|
1956 | HRESULT rc = i_convertMachineGroups(aGroups, &llGroups);
|
---|
1957 | if (FAILED(rc))
|
---|
1958 | return rc;
|
---|
1959 |
|
---|
1960 | /* we want to rely on sorted groups during compare, to save time */
|
---|
1961 | llGroups.sort();
|
---|
1962 |
|
---|
1963 | /* get copy of all machine references, to avoid holding the list lock */
|
---|
1964 | MachinesOList::MyList allMachines;
|
---|
1965 | AutoReadLock al(m->allMachines.getLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
1966 | allMachines = m->allMachines.getList();
|
---|
1967 |
|
---|
1968 | std::vector<ComObjPtr<IMachine> > saMachines;
|
---|
1969 | saMachines.resize(0);
|
---|
1970 | for (MachinesOList::MyList::const_iterator it = allMachines.begin();
|
---|
1971 | it != allMachines.end();
|
---|
1972 | ++it)
|
---|
1973 | {
|
---|
1974 | const ComObjPtr<Machine> &pMachine = *it;
|
---|
1975 | AutoCaller autoMachineCaller(pMachine);
|
---|
1976 | if (FAILED(autoMachineCaller.rc()))
|
---|
1977 | continue;
|
---|
1978 | AutoReadLock mlock(pMachine COMMA_LOCKVAL_SRC_POS);
|
---|
1979 |
|
---|
1980 | if (pMachine->i_isAccessible())
|
---|
1981 | {
|
---|
1982 | const StringsList &thisGroups = pMachine->i_getGroups();
|
---|
1983 | for (StringsList::const_iterator it2 = thisGroups.begin();
|
---|
1984 | it2 != thisGroups.end();
|
---|
1985 | ++it2)
|
---|
1986 | {
|
---|
1987 | const Utf8Str &group = *it2;
|
---|
1988 | bool fAppended = false;
|
---|
1989 | for (StringsList::const_iterator it3 = llGroups.begin();
|
---|
1990 | it3 != llGroups.end();
|
---|
1991 | ++it3)
|
---|
1992 | {
|
---|
1993 | int order = it3->compare(group);
|
---|
1994 | if (order == 0)
|
---|
1995 | {
|
---|
1996 | saMachines.push_back(static_cast<IMachine *>(pMachine));
|
---|
1997 | fAppended = true;
|
---|
1998 | break;
|
---|
1999 | }
|
---|
2000 | else if (order > 0)
|
---|
2001 | break;
|
---|
2002 | else
|
---|
2003 | continue;
|
---|
2004 | }
|
---|
2005 | /* avoid duplicates and save time */
|
---|
2006 | if (fAppended)
|
---|
2007 | break;
|
---|
2008 | }
|
---|
2009 | }
|
---|
2010 | }
|
---|
2011 | aMachines.resize(saMachines.size());
|
---|
2012 | size_t i = 0;
|
---|
2013 | for(i = 0; i < saMachines.size(); ++i)
|
---|
2014 | saMachines[i].queryInterfaceTo(aMachines[i].asOutParam());
|
---|
2015 |
|
---|
2016 | return S_OK;
|
---|
2017 | }
|
---|
2018 |
|
---|
2019 | HRESULT VirtualBox::getMachineStates(const std::vector<ComPtr<IMachine> > &aMachines,
|
---|
2020 | std::vector<MachineState_T> &aStates)
|
---|
2021 | {
|
---|
2022 | com::SafeIfaceArray<IMachine> saMachines(aMachines);
|
---|
2023 | aStates.resize(aMachines.size());
|
---|
2024 | for (size_t i = 0; i < saMachines.size(); i++)
|
---|
2025 | {
|
---|
2026 | ComPtr<IMachine> pMachine = saMachines[i];
|
---|
2027 | MachineState_T state = MachineState_Null;
|
---|
2028 | if (!pMachine.isNull())
|
---|
2029 | {
|
---|
2030 | HRESULT rc = pMachine->COMGETTER(State)(&state);
|
---|
2031 | if (rc == E_ACCESSDENIED)
|
---|
2032 | rc = S_OK;
|
---|
2033 | AssertComRC(rc);
|
---|
2034 | }
|
---|
2035 | aStates[i] = state;
|
---|
2036 | }
|
---|
2037 | return S_OK;
|
---|
2038 | }
|
---|
2039 |
|
---|
2040 | HRESULT VirtualBox::createUnattendedInstaller(ComPtr<IUnattended> &aUnattended)
|
---|
2041 | {
|
---|
2042 | #ifdef VBOX_WITH_UNATTENDED
|
---|
2043 | ComObjPtr<Unattended> ptrUnattended;
|
---|
2044 | HRESULT hrc = ptrUnattended.createObject();
|
---|
2045 | if (SUCCEEDED(hrc))
|
---|
2046 | {
|
---|
2047 | AutoReadLock wlock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2048 | hrc = ptrUnattended->initUnattended(this);
|
---|
2049 | if (SUCCEEDED(hrc))
|
---|
2050 | hrc = ptrUnattended.queryInterfaceTo(aUnattended.asOutParam());
|
---|
2051 | }
|
---|
2052 | return hrc;
|
---|
2053 | #else
|
---|
2054 | NOREF(aUnattended);
|
---|
2055 | return E_NOTIMPL;
|
---|
2056 | #endif
|
---|
2057 | }
|
---|
2058 |
|
---|
2059 | HRESULT VirtualBox::createMedium(const com::Utf8Str &aFormat,
|
---|
2060 | const com::Utf8Str &aLocation,
|
---|
2061 | AccessMode_T aAccessMode,
|
---|
2062 | DeviceType_T aDeviceType,
|
---|
2063 | ComPtr<IMedium> &aMedium)
|
---|
2064 | {
|
---|
2065 | NOREF(aAccessMode); /**< @todo r=klaus make use of access mode */
|
---|
2066 |
|
---|
2067 | HRESULT rc = S_OK;
|
---|
2068 |
|
---|
2069 | ComObjPtr<Medium> medium;
|
---|
2070 | medium.createObject();
|
---|
2071 | com::Utf8Str format = aFormat;
|
---|
2072 |
|
---|
2073 | switch (aDeviceType)
|
---|
2074 | {
|
---|
2075 | case DeviceType_HardDisk:
|
---|
2076 | {
|
---|
2077 |
|
---|
2078 | /* we don't access non-const data members so no need to lock */
|
---|
2079 | if (format.isEmpty())
|
---|
2080 | i_getDefaultHardDiskFormat(format);
|
---|
2081 |
|
---|
2082 | rc = medium->init(this,
|
---|
2083 | format,
|
---|
2084 | aLocation,
|
---|
2085 | Guid::Empty /* media registry: none yet */,
|
---|
2086 | aDeviceType);
|
---|
2087 | }
|
---|
2088 | break;
|
---|
2089 |
|
---|
2090 | case DeviceType_DVD:
|
---|
2091 | case DeviceType_Floppy:
|
---|
2092 | {
|
---|
2093 |
|
---|
2094 | if (format.isEmpty())
|
---|
2095 | return setError(E_INVALIDARG, "Format must be Valid Type%s", format.c_str());
|
---|
2096 |
|
---|
2097 | // enforce read-only for DVDs even if caller specified ReadWrite
|
---|
2098 | if (aDeviceType == DeviceType_DVD)
|
---|
2099 | aAccessMode = AccessMode_ReadOnly;
|
---|
2100 |
|
---|
2101 | rc = medium->init(this,
|
---|
2102 | format,
|
---|
2103 | aLocation,
|
---|
2104 | Guid::Empty /* media registry: none yet */,
|
---|
2105 | aDeviceType);
|
---|
2106 |
|
---|
2107 | }
|
---|
2108 | break;
|
---|
2109 |
|
---|
2110 | default:
|
---|
2111 | return setError(E_INVALIDARG, "Device type must be HardDisk, DVD or Floppy %d", aDeviceType);
|
---|
2112 | }
|
---|
2113 |
|
---|
2114 | if (SUCCEEDED(rc))
|
---|
2115 | {
|
---|
2116 | medium.queryInterfaceTo(aMedium.asOutParam());
|
---|
2117 | com::Guid uMediumId = medium->i_getId();
|
---|
2118 | if (uMediumId.isValid() && !uMediumId.isZero())
|
---|
2119 | i_onMediumRegistered(uMediumId, medium->i_getDeviceType(), TRUE);
|
---|
2120 | }
|
---|
2121 |
|
---|
2122 | return rc;
|
---|
2123 | }
|
---|
2124 |
|
---|
2125 | HRESULT VirtualBox::openMedium(const com::Utf8Str &aLocation,
|
---|
2126 | DeviceType_T aDeviceType,
|
---|
2127 | AccessMode_T aAccessMode,
|
---|
2128 | BOOL aForceNewUuid,
|
---|
2129 | ComPtr<IMedium> &aMedium)
|
---|
2130 | {
|
---|
2131 | HRESULT rc = S_OK;
|
---|
2132 | Guid id(aLocation);
|
---|
2133 | ComObjPtr<Medium> pMedium;
|
---|
2134 |
|
---|
2135 | // have to get write lock as the whole find/update sequence must be done
|
---|
2136 | // in one critical section, otherwise there are races which can lead to
|
---|
2137 | // multiple Medium objects with the same content
|
---|
2138 | AutoWriteLock treeLock(i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
2139 |
|
---|
2140 | // check if the device type is correct, and see if a medium for the
|
---|
2141 | // given path has already initialized; if so, return that
|
---|
2142 | switch (aDeviceType)
|
---|
2143 | {
|
---|
2144 | case DeviceType_HardDisk:
|
---|
2145 | if (id.isValid() && !id.isZero())
|
---|
2146 | rc = i_findHardDiskById(id, false /* setError */, &pMedium);
|
---|
2147 | else
|
---|
2148 | rc = i_findHardDiskByLocation(aLocation,
|
---|
2149 | false, /* aSetError */
|
---|
2150 | &pMedium);
|
---|
2151 | break;
|
---|
2152 |
|
---|
2153 | case DeviceType_Floppy:
|
---|
2154 | case DeviceType_DVD:
|
---|
2155 | if (id.isValid() && !id.isZero())
|
---|
2156 | rc = i_findDVDOrFloppyImage(aDeviceType, &id, Utf8Str::Empty,
|
---|
2157 | false /* setError */, &pMedium);
|
---|
2158 | else
|
---|
2159 | rc = i_findDVDOrFloppyImage(aDeviceType, NULL, aLocation,
|
---|
2160 | false /* setError */, &pMedium);
|
---|
2161 |
|
---|
2162 | // enforce read-only for DVDs even if caller specified ReadWrite
|
---|
2163 | if (aDeviceType == DeviceType_DVD)
|
---|
2164 | aAccessMode = AccessMode_ReadOnly;
|
---|
2165 | break;
|
---|
2166 |
|
---|
2167 | default:
|
---|
2168 | return setError(E_INVALIDARG, "Device type must be HardDisk, DVD or Floppy %d", aDeviceType);
|
---|
2169 | }
|
---|
2170 |
|
---|
2171 | bool fMediumRegistered = false;
|
---|
2172 | if (pMedium.isNull())
|
---|
2173 | {
|
---|
2174 | pMedium.createObject();
|
---|
2175 | treeLock.release();
|
---|
2176 | rc = pMedium->init(this,
|
---|
2177 | aLocation,
|
---|
2178 | (aAccessMode == AccessMode_ReadWrite) ? Medium::OpenReadWrite : Medium::OpenReadOnly,
|
---|
2179 | !!aForceNewUuid,
|
---|
2180 | aDeviceType);
|
---|
2181 | treeLock.acquire();
|
---|
2182 |
|
---|
2183 | if (SUCCEEDED(rc))
|
---|
2184 | {
|
---|
2185 | rc = i_registerMedium(pMedium, &pMedium, treeLock);
|
---|
2186 |
|
---|
2187 | treeLock.release();
|
---|
2188 |
|
---|
2189 | /* Note that it's important to call uninit() on failure to register
|
---|
2190 | * because the differencing hard disk would have been already associated
|
---|
2191 | * with the parent and this association needs to be broken. */
|
---|
2192 |
|
---|
2193 | if (FAILED(rc))
|
---|
2194 | {
|
---|
2195 | pMedium->uninit();
|
---|
2196 | rc = VBOX_E_OBJECT_NOT_FOUND;
|
---|
2197 | }
|
---|
2198 | else
|
---|
2199 | {
|
---|
2200 | fMediumRegistered = true;
|
---|
2201 | }
|
---|
2202 | }
|
---|
2203 | else
|
---|
2204 | {
|
---|
2205 | if (rc != VBOX_E_INVALID_OBJECT_STATE)
|
---|
2206 | rc = VBOX_E_OBJECT_NOT_FOUND;
|
---|
2207 | }
|
---|
2208 | }
|
---|
2209 |
|
---|
2210 | if (SUCCEEDED(rc))
|
---|
2211 | {
|
---|
2212 | pMedium.queryInterfaceTo(aMedium.asOutParam());
|
---|
2213 | if (fMediumRegistered)
|
---|
2214 | i_onMediumRegistered(pMedium->i_getId(), pMedium->i_getDeviceType() ,TRUE);
|
---|
2215 | }
|
---|
2216 |
|
---|
2217 | return rc;
|
---|
2218 | }
|
---|
2219 |
|
---|
2220 |
|
---|
2221 | /** @note Locks this object for reading. */
|
---|
2222 | HRESULT VirtualBox::getGuestOSType(const com::Utf8Str &aId,
|
---|
2223 | ComPtr<IGuestOSType> &aType)
|
---|
2224 | {
|
---|
2225 | ComObjPtr<GuestOSType> pType;
|
---|
2226 | HRESULT rc = i_findGuestOSType(aId, pType);
|
---|
2227 | pType.queryInterfaceTo(aType.asOutParam());
|
---|
2228 | return rc;
|
---|
2229 | }
|
---|
2230 |
|
---|
2231 | HRESULT VirtualBox::createSharedFolder(const com::Utf8Str &aName,
|
---|
2232 | const com::Utf8Str &aHostPath,
|
---|
2233 | BOOL aWritable,
|
---|
2234 | BOOL aAutomount,
|
---|
2235 | const com::Utf8Str &aAutoMountPoint)
|
---|
2236 | {
|
---|
2237 | NOREF(aName);
|
---|
2238 | NOREF(aHostPath);
|
---|
2239 | NOREF(aWritable);
|
---|
2240 | NOREF(aAutomount);
|
---|
2241 | NOREF(aAutoMountPoint);
|
---|
2242 |
|
---|
2243 | return setError(E_NOTIMPL, "Not yet implemented");
|
---|
2244 | }
|
---|
2245 |
|
---|
2246 | HRESULT VirtualBox::removeSharedFolder(const com::Utf8Str &aName)
|
---|
2247 | {
|
---|
2248 | NOREF(aName);
|
---|
2249 | return setError(E_NOTIMPL, "Not yet implemented");
|
---|
2250 | }
|
---|
2251 |
|
---|
2252 | /**
|
---|
2253 | * @note Locks this object for reading.
|
---|
2254 | */
|
---|
2255 | HRESULT VirtualBox::getExtraDataKeys(std::vector<com::Utf8Str> &aKeys)
|
---|
2256 | {
|
---|
2257 | using namespace settings;
|
---|
2258 |
|
---|
2259 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2260 |
|
---|
2261 | aKeys.resize(m->pMainConfigFile->mapExtraDataItems.size());
|
---|
2262 | size_t i = 0;
|
---|
2263 | for (StringsMap::const_iterator it = m->pMainConfigFile->mapExtraDataItems.begin();
|
---|
2264 | it != m->pMainConfigFile->mapExtraDataItems.end(); ++it, ++i)
|
---|
2265 | aKeys[i] = it->first;
|
---|
2266 |
|
---|
2267 | return S_OK;
|
---|
2268 | }
|
---|
2269 |
|
---|
2270 | /**
|
---|
2271 | * @note Locks this object for reading.
|
---|
2272 | */
|
---|
2273 | HRESULT VirtualBox::getExtraData(const com::Utf8Str &aKey,
|
---|
2274 | com::Utf8Str &aValue)
|
---|
2275 | {
|
---|
2276 | settings::StringsMap::const_iterator it = m->pMainConfigFile->mapExtraDataItems.find(aKey);
|
---|
2277 | if (it != m->pMainConfigFile->mapExtraDataItems.end())
|
---|
2278 | // found:
|
---|
2279 | aValue = it->second; // source is a Utf8Str
|
---|
2280 |
|
---|
2281 | /* return the result to caller (may be empty) */
|
---|
2282 |
|
---|
2283 | return S_OK;
|
---|
2284 | }
|
---|
2285 |
|
---|
2286 | /**
|
---|
2287 | * @note Locks this object for writing.
|
---|
2288 | */
|
---|
2289 | HRESULT VirtualBox::setExtraData(const com::Utf8Str &aKey,
|
---|
2290 | const com::Utf8Str &aValue)
|
---|
2291 | {
|
---|
2292 | Utf8Str strKey(aKey);
|
---|
2293 | Utf8Str strValue(aValue);
|
---|
2294 | Utf8Str strOldValue; // empty
|
---|
2295 | HRESULT rc = S_OK;
|
---|
2296 |
|
---|
2297 | /* Because control characters in aKey have caused problems in the settings
|
---|
2298 | * they are rejected unless the key should be deleted. */
|
---|
2299 | if (!strValue.isEmpty())
|
---|
2300 | {
|
---|
2301 | for (size_t i = 0; i < strKey.length(); ++i)
|
---|
2302 | {
|
---|
2303 | char ch = strKey[i];
|
---|
2304 | if (RTLocCIsCntrl(ch))
|
---|
2305 | return E_INVALIDARG;
|
---|
2306 | }
|
---|
2307 | }
|
---|
2308 |
|
---|
2309 | // locking note: we only hold the read lock briefly to look up the old value,
|
---|
2310 | // then release it and call the onExtraCanChange callbacks. There is a small
|
---|
2311 | // chance of a race insofar as the callback might be called twice if two callers
|
---|
2312 | // change the same key at the same time, but that's a much better solution
|
---|
2313 | // than the deadlock we had here before. The actual changing of the extradata
|
---|
2314 | // is then performed under the write lock and race-free.
|
---|
2315 |
|
---|
2316 | // look up the old value first; if nothing has changed then we need not do anything
|
---|
2317 | {
|
---|
2318 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS); // hold read lock only while looking up
|
---|
2319 | settings::StringsMap::const_iterator it = m->pMainConfigFile->mapExtraDataItems.find(strKey);
|
---|
2320 | if (it != m->pMainConfigFile->mapExtraDataItems.end())
|
---|
2321 | strOldValue = it->second;
|
---|
2322 | }
|
---|
2323 |
|
---|
2324 | bool fChanged;
|
---|
2325 | if ((fChanged = (strOldValue != strValue)))
|
---|
2326 | {
|
---|
2327 | // ask for permission from all listeners outside the locks;
|
---|
2328 | // onExtraDataCanChange() only briefly requests the VirtualBox
|
---|
2329 | // lock to copy the list of callbacks to invoke
|
---|
2330 | Bstr error;
|
---|
2331 |
|
---|
2332 | if (!i_onExtraDataCanChange(Guid::Empty, Bstr(aKey).raw(), Bstr(aValue).raw(), error))
|
---|
2333 | {
|
---|
2334 | const char *sep = error.isEmpty() ? "" : ": ";
|
---|
2335 | Log1WarningFunc(("Someone vetoed! Change refused%s%ls\n", sep, error.raw()));
|
---|
2336 | return setError(E_ACCESSDENIED,
|
---|
2337 | tr("Could not set extra data because someone refused the requested change of '%s' to '%s'%s%ls"),
|
---|
2338 | strKey.c_str(),
|
---|
2339 | strValue.c_str(),
|
---|
2340 | sep,
|
---|
2341 | error.raw());
|
---|
2342 | }
|
---|
2343 |
|
---|
2344 | // data is changing and change not vetoed: then write it out under the lock
|
---|
2345 |
|
---|
2346 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2347 |
|
---|
2348 | if (strValue.isEmpty())
|
---|
2349 | m->pMainConfigFile->mapExtraDataItems.erase(strKey);
|
---|
2350 | else
|
---|
2351 | m->pMainConfigFile->mapExtraDataItems[strKey] = strValue;
|
---|
2352 | // creates a new key if needed
|
---|
2353 |
|
---|
2354 | /* save settings on success */
|
---|
2355 | rc = i_saveSettings();
|
---|
2356 | if (FAILED(rc)) return rc;
|
---|
2357 | }
|
---|
2358 |
|
---|
2359 | // fire notification outside the lock
|
---|
2360 | if (fChanged)
|
---|
2361 | i_onExtraDataChange(Guid::Empty, Bstr(aKey).raw(), Bstr(aValue).raw());
|
---|
2362 |
|
---|
2363 | return rc;
|
---|
2364 | }
|
---|
2365 |
|
---|
2366 | /**
|
---|
2367 | *
|
---|
2368 | */
|
---|
2369 | HRESULT VirtualBox::setSettingsSecret(const com::Utf8Str &aPassword)
|
---|
2370 | {
|
---|
2371 | i_storeSettingsKey(aPassword);
|
---|
2372 | i_decryptSettings();
|
---|
2373 | return S_OK;
|
---|
2374 | }
|
---|
2375 |
|
---|
2376 | int VirtualBox::i_decryptMediumSettings(Medium *pMedium)
|
---|
2377 | {
|
---|
2378 | Bstr bstrCipher;
|
---|
2379 | HRESULT hrc = pMedium->GetProperty(Bstr("InitiatorSecretEncrypted").raw(),
|
---|
2380 | bstrCipher.asOutParam());
|
---|
2381 | if (SUCCEEDED(hrc))
|
---|
2382 | {
|
---|
2383 | Utf8Str strPlaintext;
|
---|
2384 | int rc = i_decryptSetting(&strPlaintext, bstrCipher);
|
---|
2385 | if (RT_SUCCESS(rc))
|
---|
2386 | pMedium->i_setPropertyDirect("InitiatorSecret", strPlaintext);
|
---|
2387 | else
|
---|
2388 | return rc;
|
---|
2389 | }
|
---|
2390 | return VINF_SUCCESS;
|
---|
2391 | }
|
---|
2392 |
|
---|
2393 | /**
|
---|
2394 | * Decrypt all encrypted settings.
|
---|
2395 | *
|
---|
2396 | * So far we only have encrypted iSCSI initiator secrets so we just go through
|
---|
2397 | * all hard disk mediums and determine the plain 'InitiatorSecret' from
|
---|
2398 | * 'InitiatorSecretEncrypted. The latter is stored as Base64 because medium
|
---|
2399 | * properties need to be null-terminated strings.
|
---|
2400 | */
|
---|
2401 | int VirtualBox::i_decryptSettings()
|
---|
2402 | {
|
---|
2403 | bool fFailure = false;
|
---|
2404 | AutoReadLock al(m->allHardDisks.getLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
2405 | for (MediaList::const_iterator mt = m->allHardDisks.begin();
|
---|
2406 | mt != m->allHardDisks.end();
|
---|
2407 | ++mt)
|
---|
2408 | {
|
---|
2409 | ComObjPtr<Medium> pMedium = *mt;
|
---|
2410 | AutoCaller medCaller(pMedium);
|
---|
2411 | if (FAILED(medCaller.rc()))
|
---|
2412 | continue;
|
---|
2413 | AutoWriteLock mlock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
2414 | int vrc = i_decryptMediumSettings(pMedium);
|
---|
2415 | if (RT_FAILURE(vrc))
|
---|
2416 | fFailure = true;
|
---|
2417 | }
|
---|
2418 | if (!fFailure)
|
---|
2419 | {
|
---|
2420 | for (MediaList::const_iterator mt = m->allHardDisks.begin();
|
---|
2421 | mt != m->allHardDisks.end();
|
---|
2422 | ++mt)
|
---|
2423 | {
|
---|
2424 | i_onMediumConfigChanged(*mt);
|
---|
2425 | }
|
---|
2426 | }
|
---|
2427 | return fFailure ? VERR_INVALID_PARAMETER : VINF_SUCCESS;
|
---|
2428 | }
|
---|
2429 |
|
---|
2430 | /**
|
---|
2431 | * Encode.
|
---|
2432 | *
|
---|
2433 | * @param aPlaintext plaintext to be encrypted
|
---|
2434 | * @param aCiphertext resulting ciphertext (base64-encoded)
|
---|
2435 | */
|
---|
2436 | int VirtualBox::i_encryptSetting(const Utf8Str &aPlaintext, Utf8Str *aCiphertext)
|
---|
2437 | {
|
---|
2438 | uint8_t abCiphertext[32];
|
---|
2439 | char szCipherBase64[128];
|
---|
2440 | size_t cchCipherBase64;
|
---|
2441 | int rc = i_encryptSettingBytes((uint8_t*)aPlaintext.c_str(), abCiphertext,
|
---|
2442 | aPlaintext.length()+1, sizeof(abCiphertext));
|
---|
2443 | if (RT_SUCCESS(rc))
|
---|
2444 | {
|
---|
2445 | rc = RTBase64Encode(abCiphertext, sizeof(abCiphertext),
|
---|
2446 | szCipherBase64, sizeof(szCipherBase64),
|
---|
2447 | &cchCipherBase64);
|
---|
2448 | if (RT_SUCCESS(rc))
|
---|
2449 | *aCiphertext = szCipherBase64;
|
---|
2450 | }
|
---|
2451 | return rc;
|
---|
2452 | }
|
---|
2453 |
|
---|
2454 | /**
|
---|
2455 | * Decode.
|
---|
2456 | *
|
---|
2457 | * @param aPlaintext resulting plaintext
|
---|
2458 | * @param aCiphertext ciphertext (base64-encoded) to decrypt
|
---|
2459 | */
|
---|
2460 | int VirtualBox::i_decryptSetting(Utf8Str *aPlaintext, const Utf8Str &aCiphertext)
|
---|
2461 | {
|
---|
2462 | uint8_t abPlaintext[64];
|
---|
2463 | uint8_t abCiphertext[64];
|
---|
2464 | size_t cbCiphertext;
|
---|
2465 | int rc = RTBase64Decode(aCiphertext.c_str(),
|
---|
2466 | abCiphertext, sizeof(abCiphertext),
|
---|
2467 | &cbCiphertext, NULL);
|
---|
2468 | if (RT_SUCCESS(rc))
|
---|
2469 | {
|
---|
2470 | rc = i_decryptSettingBytes(abPlaintext, abCiphertext, cbCiphertext);
|
---|
2471 | if (RT_SUCCESS(rc))
|
---|
2472 | {
|
---|
2473 | for (unsigned i = 0; i < cbCiphertext; i++)
|
---|
2474 | {
|
---|
2475 | /* sanity check: null-terminated string? */
|
---|
2476 | if (abPlaintext[i] == '\0')
|
---|
2477 | {
|
---|
2478 | /* sanity check: valid UTF8 string? */
|
---|
2479 | if (RTStrIsValidEncoding((const char*)abPlaintext))
|
---|
2480 | {
|
---|
2481 | *aPlaintext = Utf8Str((const char*)abPlaintext);
|
---|
2482 | return VINF_SUCCESS;
|
---|
2483 | }
|
---|
2484 | }
|
---|
2485 | }
|
---|
2486 | rc = VERR_INVALID_MAGIC;
|
---|
2487 | }
|
---|
2488 | }
|
---|
2489 | return rc;
|
---|
2490 | }
|
---|
2491 |
|
---|
2492 | /**
|
---|
2493 | * Encrypt secret bytes. Use the m->SettingsCipherKey as key.
|
---|
2494 | *
|
---|
2495 | * @param aPlaintext clear text to be encrypted
|
---|
2496 | * @param aCiphertext resulting encrypted text
|
---|
2497 | * @param aPlaintextSize size of the plaintext
|
---|
2498 | * @param aCiphertextSize size of the ciphertext
|
---|
2499 | */
|
---|
2500 | int VirtualBox::i_encryptSettingBytes(const uint8_t *aPlaintext, uint8_t *aCiphertext,
|
---|
2501 | size_t aPlaintextSize, size_t aCiphertextSize) const
|
---|
2502 | {
|
---|
2503 | unsigned i, j;
|
---|
2504 | uint8_t aBytes[64];
|
---|
2505 |
|
---|
2506 | if (!m->fSettingsCipherKeySet)
|
---|
2507 | return VERR_INVALID_STATE;
|
---|
2508 |
|
---|
2509 | if (aCiphertextSize > sizeof(aBytes))
|
---|
2510 | return VERR_BUFFER_OVERFLOW;
|
---|
2511 |
|
---|
2512 | if (aCiphertextSize < 32)
|
---|
2513 | return VERR_INVALID_PARAMETER;
|
---|
2514 |
|
---|
2515 | AssertCompile(sizeof(m->SettingsCipherKey) >= 32);
|
---|
2516 |
|
---|
2517 | /* store the first 8 bytes of the cipherkey for verification */
|
---|
2518 | for (i = 0, j = 0; i < 8; i++, j++)
|
---|
2519 | aCiphertext[i] = m->SettingsCipherKey[j];
|
---|
2520 |
|
---|
2521 | for (unsigned k = 0; k < aPlaintextSize && i < aCiphertextSize; i++, k++)
|
---|
2522 | {
|
---|
2523 | aCiphertext[i] = (aPlaintext[k] ^ m->SettingsCipherKey[j]);
|
---|
2524 | if (++j >= sizeof(m->SettingsCipherKey))
|
---|
2525 | j = 0;
|
---|
2526 | }
|
---|
2527 |
|
---|
2528 | /* fill with random data to have a minimal length (salt) */
|
---|
2529 | if (i < aCiphertextSize)
|
---|
2530 | {
|
---|
2531 | RTRandBytes(aBytes, aCiphertextSize - i);
|
---|
2532 | for (int k = 0; i < aCiphertextSize; i++, k++)
|
---|
2533 | {
|
---|
2534 | aCiphertext[i] = aBytes[k] ^ m->SettingsCipherKey[j];
|
---|
2535 | if (++j >= sizeof(m->SettingsCipherKey))
|
---|
2536 | j = 0;
|
---|
2537 | }
|
---|
2538 | }
|
---|
2539 |
|
---|
2540 | return VINF_SUCCESS;
|
---|
2541 | }
|
---|
2542 |
|
---|
2543 | /**
|
---|
2544 | * Decrypt secret bytes. Use the m->SettingsCipherKey as key.
|
---|
2545 | *
|
---|
2546 | * @param aPlaintext resulting plaintext
|
---|
2547 | * @param aCiphertext ciphertext to be decrypted
|
---|
2548 | * @param aCiphertextSize size of the ciphertext == size of the plaintext
|
---|
2549 | */
|
---|
2550 | int VirtualBox::i_decryptSettingBytes(uint8_t *aPlaintext,
|
---|
2551 | const uint8_t *aCiphertext, size_t aCiphertextSize) const
|
---|
2552 | {
|
---|
2553 | unsigned i, j;
|
---|
2554 |
|
---|
2555 | if (!m->fSettingsCipherKeySet)
|
---|
2556 | return VERR_INVALID_STATE;
|
---|
2557 |
|
---|
2558 | if (aCiphertextSize < 32)
|
---|
2559 | return VERR_INVALID_PARAMETER;
|
---|
2560 |
|
---|
2561 | /* key verification */
|
---|
2562 | for (i = 0, j = 0; i < 8; i++, j++)
|
---|
2563 | if (aCiphertext[i] != m->SettingsCipherKey[j])
|
---|
2564 | return VERR_INVALID_MAGIC;
|
---|
2565 |
|
---|
2566 | /* poison */
|
---|
2567 | memset(aPlaintext, 0xff, aCiphertextSize);
|
---|
2568 | for (int k = 0; i < aCiphertextSize; i++, k++)
|
---|
2569 | {
|
---|
2570 | aPlaintext[k] = aCiphertext[i] ^ m->SettingsCipherKey[j];
|
---|
2571 | if (++j >= sizeof(m->SettingsCipherKey))
|
---|
2572 | j = 0;
|
---|
2573 | }
|
---|
2574 |
|
---|
2575 | return VINF_SUCCESS;
|
---|
2576 | }
|
---|
2577 |
|
---|
2578 | /**
|
---|
2579 | * Store a settings key.
|
---|
2580 | *
|
---|
2581 | * @param aKey the key to store
|
---|
2582 | */
|
---|
2583 | void VirtualBox::i_storeSettingsKey(const Utf8Str &aKey)
|
---|
2584 | {
|
---|
2585 | RTSha512(aKey.c_str(), aKey.length(), m->SettingsCipherKey);
|
---|
2586 | m->fSettingsCipherKeySet = true;
|
---|
2587 | }
|
---|
2588 |
|
---|
2589 | // public methods only for internal purposes
|
---|
2590 | /////////////////////////////////////////////////////////////////////////////
|
---|
2591 |
|
---|
2592 | #ifdef DEBUG
|
---|
2593 | void VirtualBox::i_dumpAllBackRefs()
|
---|
2594 | {
|
---|
2595 | {
|
---|
2596 | AutoReadLock al(m->allHardDisks.getLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
2597 | for (MediaList::const_iterator mt = m->allHardDisks.begin();
|
---|
2598 | mt != m->allHardDisks.end();
|
---|
2599 | ++mt)
|
---|
2600 | {
|
---|
2601 | ComObjPtr<Medium> pMedium = *mt;
|
---|
2602 | pMedium->i_dumpBackRefs();
|
---|
2603 | }
|
---|
2604 | }
|
---|
2605 | {
|
---|
2606 | AutoReadLock al(m->allDVDImages.getLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
2607 | for (MediaList::const_iterator mt = m->allDVDImages.begin();
|
---|
2608 | mt != m->allDVDImages.end();
|
---|
2609 | ++mt)
|
---|
2610 | {
|
---|
2611 | ComObjPtr<Medium> pMedium = *mt;
|
---|
2612 | pMedium->i_dumpBackRefs();
|
---|
2613 | }
|
---|
2614 | }
|
---|
2615 | }
|
---|
2616 | #endif
|
---|
2617 |
|
---|
2618 | /**
|
---|
2619 | * Posts an event to the event queue that is processed asynchronously
|
---|
2620 | * on a dedicated thread.
|
---|
2621 | *
|
---|
2622 | * Posting events to the dedicated event queue is useful to perform secondary
|
---|
2623 | * actions outside any object locks -- for example, to iterate over a list
|
---|
2624 | * of callbacks and inform them about some change caused by some object's
|
---|
2625 | * method call.
|
---|
2626 | *
|
---|
2627 | * @param event event to post; must have been allocated using |new|, will
|
---|
2628 | * be deleted automatically by the event thread after processing
|
---|
2629 | *
|
---|
2630 | * @note Doesn't lock any object.
|
---|
2631 | */
|
---|
2632 | HRESULT VirtualBox::i_postEvent(Event *event)
|
---|
2633 | {
|
---|
2634 | AssertReturn(event, E_FAIL);
|
---|
2635 |
|
---|
2636 | HRESULT rc;
|
---|
2637 | AutoCaller autoCaller(this);
|
---|
2638 | if (SUCCEEDED((rc = autoCaller.rc())))
|
---|
2639 | {
|
---|
2640 | if (getObjectState().getState() != ObjectState::Ready)
|
---|
2641 | Log1WarningFunc(("VirtualBox has been uninitialized (state=%d), the event is discarded!\n",
|
---|
2642 | getObjectState().getState()));
|
---|
2643 | // return S_OK
|
---|
2644 | else if ( (m->pAsyncEventQ)
|
---|
2645 | && (m->pAsyncEventQ->postEvent(event))
|
---|
2646 | )
|
---|
2647 | return S_OK;
|
---|
2648 | else
|
---|
2649 | rc = E_FAIL;
|
---|
2650 | }
|
---|
2651 |
|
---|
2652 | // in any event of failure, we must clean up here, or we'll leak;
|
---|
2653 | // the caller has allocated the object using new()
|
---|
2654 | delete event;
|
---|
2655 | return rc;
|
---|
2656 | }
|
---|
2657 |
|
---|
2658 | /**
|
---|
2659 | * Adds a progress to the global collection of pending operations.
|
---|
2660 | * Usually gets called upon progress object initialization.
|
---|
2661 | *
|
---|
2662 | * @param aProgress Operation to add to the collection.
|
---|
2663 | *
|
---|
2664 | * @note Doesn't lock objects.
|
---|
2665 | */
|
---|
2666 | HRESULT VirtualBox::i_addProgress(IProgress *aProgress)
|
---|
2667 | {
|
---|
2668 | CheckComArgNotNull(aProgress);
|
---|
2669 |
|
---|
2670 | AutoCaller autoCaller(this);
|
---|
2671 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2672 |
|
---|
2673 | Bstr id;
|
---|
2674 | HRESULT rc = aProgress->COMGETTER(Id)(id.asOutParam());
|
---|
2675 | AssertComRCReturnRC(rc);
|
---|
2676 |
|
---|
2677 | /* protect mProgressOperations */
|
---|
2678 | AutoWriteLock safeLock(m->mtxProgressOperations COMMA_LOCKVAL_SRC_POS);
|
---|
2679 |
|
---|
2680 | m->mapProgressOperations.insert(ProgressMap::value_type(Guid(id), aProgress));
|
---|
2681 | return S_OK;
|
---|
2682 | }
|
---|
2683 |
|
---|
2684 | /**
|
---|
2685 | * Removes the progress from the global collection of pending operations.
|
---|
2686 | * Usually gets called upon progress completion.
|
---|
2687 | *
|
---|
2688 | * @param aId UUID of the progress operation to remove
|
---|
2689 | *
|
---|
2690 | * @note Doesn't lock objects.
|
---|
2691 | */
|
---|
2692 | HRESULT VirtualBox::i_removeProgress(IN_GUID aId)
|
---|
2693 | {
|
---|
2694 | AutoCaller autoCaller(this);
|
---|
2695 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2696 |
|
---|
2697 | ComPtr<IProgress> progress;
|
---|
2698 |
|
---|
2699 | /* protect mProgressOperations */
|
---|
2700 | AutoWriteLock safeLock(m->mtxProgressOperations COMMA_LOCKVAL_SRC_POS);
|
---|
2701 |
|
---|
2702 | size_t cnt = m->mapProgressOperations.erase(aId);
|
---|
2703 | Assert(cnt == 1);
|
---|
2704 | NOREF(cnt);
|
---|
2705 |
|
---|
2706 | return S_OK;
|
---|
2707 | }
|
---|
2708 |
|
---|
2709 | #ifdef RT_OS_WINDOWS
|
---|
2710 |
|
---|
2711 | class StartSVCHelperClientData : public ThreadTask
|
---|
2712 | {
|
---|
2713 | public:
|
---|
2714 | StartSVCHelperClientData()
|
---|
2715 | {
|
---|
2716 | LogFlowFuncEnter();
|
---|
2717 | m_strTaskName = "SVCHelper";
|
---|
2718 | threadVoidData = NULL;
|
---|
2719 | initialized = false;
|
---|
2720 | }
|
---|
2721 |
|
---|
2722 | virtual ~StartSVCHelperClientData()
|
---|
2723 | {
|
---|
2724 | LogFlowFuncEnter();
|
---|
2725 | if (threadVoidData!=NULL)
|
---|
2726 | {
|
---|
2727 | delete threadVoidData;
|
---|
2728 | threadVoidData=NULL;
|
---|
2729 | }
|
---|
2730 | };
|
---|
2731 |
|
---|
2732 | void handler()
|
---|
2733 | {
|
---|
2734 | VirtualBox::i_SVCHelperClientThreadTask(this);
|
---|
2735 | }
|
---|
2736 |
|
---|
2737 | const ComPtr<Progress>& GetProgressObject() const {return progress;}
|
---|
2738 |
|
---|
2739 | bool init(VirtualBox* aVbox,
|
---|
2740 | Progress* aProgress,
|
---|
2741 | bool aPrivileged,
|
---|
2742 | VirtualBox::SVCHelperClientFunc aFunc,
|
---|
2743 | void *aUser)
|
---|
2744 | {
|
---|
2745 | LogFlowFuncEnter();
|
---|
2746 | that = aVbox;
|
---|
2747 | progress = aProgress;
|
---|
2748 | privileged = aPrivileged;
|
---|
2749 | func = aFunc;
|
---|
2750 | user = aUser;
|
---|
2751 |
|
---|
2752 | initThreadVoidData();
|
---|
2753 |
|
---|
2754 | initialized = true;
|
---|
2755 |
|
---|
2756 | return initialized;
|
---|
2757 | }
|
---|
2758 |
|
---|
2759 | bool isOk() const{ return initialized;}
|
---|
2760 |
|
---|
2761 | bool initialized;
|
---|
2762 | ComObjPtr<VirtualBox> that;
|
---|
2763 | ComObjPtr<Progress> progress;
|
---|
2764 | bool privileged;
|
---|
2765 | VirtualBox::SVCHelperClientFunc func;
|
---|
2766 | void *user;
|
---|
2767 | ThreadVoidData *threadVoidData;
|
---|
2768 |
|
---|
2769 | private:
|
---|
2770 | bool initThreadVoidData()
|
---|
2771 | {
|
---|
2772 | LogFlowFuncEnter();
|
---|
2773 | threadVoidData = static_cast<ThreadVoidData*>(user);
|
---|
2774 | return true;
|
---|
2775 | }
|
---|
2776 | };
|
---|
2777 |
|
---|
2778 | /**
|
---|
2779 | * Helper method that starts a worker thread that:
|
---|
2780 | * - creates a pipe communication channel using SVCHlpClient;
|
---|
2781 | * - starts an SVC Helper process that will inherit this channel;
|
---|
2782 | * - executes the supplied function by passing it the created SVCHlpClient
|
---|
2783 | * and opened instance to communicate to the Helper process and the given
|
---|
2784 | * Progress object.
|
---|
2785 | *
|
---|
2786 | * The user function is supposed to communicate to the helper process
|
---|
2787 | * using the \a aClient argument to do the requested job and optionally expose
|
---|
2788 | * the progress through the \a aProgress object. The user function should never
|
---|
2789 | * call notifyComplete() on it: this will be done automatically using the
|
---|
2790 | * result code returned by the function.
|
---|
2791 | *
|
---|
2792 | * Before the user function is started, the communication channel passed to
|
---|
2793 | * the \a aClient argument is fully set up, the function should start using
|
---|
2794 | * its write() and read() methods directly.
|
---|
2795 | *
|
---|
2796 | * The \a aVrc parameter of the user function may be used to return an error
|
---|
2797 | * code if it is related to communication errors (for example, returned by
|
---|
2798 | * the SVCHlpClient members when they fail). In this case, the correct error
|
---|
2799 | * message using this value will be reported to the caller. Note that the
|
---|
2800 | * value of \a aVrc is inspected only if the user function itself returns
|
---|
2801 | * success.
|
---|
2802 | *
|
---|
2803 | * If a failure happens anywhere before the user function would be normally
|
---|
2804 | * called, it will be called anyway in special "cleanup only" mode indicated
|
---|
2805 | * by \a aClient, \a aProgress and \aVrc arguments set to NULL. In this mode,
|
---|
2806 | * all the function is supposed to do is to cleanup its aUser argument if
|
---|
2807 | * necessary (it's assumed that the ownership of this argument is passed to
|
---|
2808 | * the user function once #startSVCHelperClient() returns a success, thus
|
---|
2809 | * making it responsible for the cleanup).
|
---|
2810 | *
|
---|
2811 | * After the user function returns, the thread will send the SVCHlpMsg::Null
|
---|
2812 | * message to indicate a process termination.
|
---|
2813 | *
|
---|
2814 | * @param aPrivileged |true| to start the SVC Helper process as a privileged
|
---|
2815 | * user that can perform administrative tasks
|
---|
2816 | * @param aFunc user function to run
|
---|
2817 | * @param aUser argument to the user function
|
---|
2818 | * @param aProgress progress object that will track operation completion
|
---|
2819 | *
|
---|
2820 | * @note aPrivileged is currently ignored (due to some unsolved problems in
|
---|
2821 | * Vista) and the process will be started as a normal (unprivileged)
|
---|
2822 | * process.
|
---|
2823 | *
|
---|
2824 | * @note Doesn't lock anything.
|
---|
2825 | */
|
---|
2826 | HRESULT VirtualBox::i_startSVCHelperClient(bool aPrivileged,
|
---|
2827 | SVCHelperClientFunc aFunc,
|
---|
2828 | void *aUser, Progress *aProgress)
|
---|
2829 | {
|
---|
2830 | LogFlowFuncEnter();
|
---|
2831 | AssertReturn(aFunc, E_POINTER);
|
---|
2832 | AssertReturn(aProgress, E_POINTER);
|
---|
2833 |
|
---|
2834 | AutoCaller autoCaller(this);
|
---|
2835 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2836 |
|
---|
2837 | /* create the i_SVCHelperClientThreadTask() argument */
|
---|
2838 |
|
---|
2839 | HRESULT hr = S_OK;
|
---|
2840 | StartSVCHelperClientData *pTask = NULL;
|
---|
2841 | try
|
---|
2842 | {
|
---|
2843 | pTask = new StartSVCHelperClientData();
|
---|
2844 |
|
---|
2845 | pTask->init(this, aProgress, aPrivileged, aFunc, aUser);
|
---|
2846 |
|
---|
2847 | if (!pTask->isOk())
|
---|
2848 | {
|
---|
2849 | delete pTask;
|
---|
2850 | LogRel(("Could not init StartSVCHelperClientData object \n"));
|
---|
2851 | throw E_FAIL;
|
---|
2852 | }
|
---|
2853 |
|
---|
2854 | //this function delete pTask in case of exceptions, so there is no need in the call of delete operator
|
---|
2855 | hr = pTask->createThreadWithType(RTTHREADTYPE_MAIN_WORKER);
|
---|
2856 |
|
---|
2857 | }
|
---|
2858 | catch(std::bad_alloc &)
|
---|
2859 | {
|
---|
2860 | hr = setError(E_OUTOFMEMORY);
|
---|
2861 | }
|
---|
2862 | catch(...)
|
---|
2863 | {
|
---|
2864 | LogRel(("Could not create thread for StartSVCHelperClientData \n"));
|
---|
2865 | hr = E_FAIL;
|
---|
2866 | }
|
---|
2867 |
|
---|
2868 | return hr;
|
---|
2869 | }
|
---|
2870 |
|
---|
2871 | /**
|
---|
2872 | * Worker thread for startSVCHelperClient().
|
---|
2873 | */
|
---|
2874 | /* static */
|
---|
2875 | void VirtualBox::i_SVCHelperClientThreadTask(StartSVCHelperClientData *pTask)
|
---|
2876 | {
|
---|
2877 | LogFlowFuncEnter();
|
---|
2878 | HRESULT rc = S_OK;
|
---|
2879 | bool userFuncCalled = false;
|
---|
2880 |
|
---|
2881 | do
|
---|
2882 | {
|
---|
2883 | AssertBreakStmt(pTask, rc = E_POINTER);
|
---|
2884 | AssertReturnVoid(!pTask->progress.isNull());
|
---|
2885 |
|
---|
2886 | /* protect VirtualBox from uninitialization */
|
---|
2887 | AutoCaller autoCaller(pTask->that);
|
---|
2888 | if (!autoCaller.isOk())
|
---|
2889 | {
|
---|
2890 | /* it's too late */
|
---|
2891 | rc = autoCaller.rc();
|
---|
2892 | break;
|
---|
2893 | }
|
---|
2894 |
|
---|
2895 | int vrc = VINF_SUCCESS;
|
---|
2896 |
|
---|
2897 | Guid id;
|
---|
2898 | id.create();
|
---|
2899 | SVCHlpClient client;
|
---|
2900 | vrc = client.create(Utf8StrFmt("VirtualBox\\SVCHelper\\{%RTuuid}",
|
---|
2901 | id.raw()).c_str());
|
---|
2902 | if (RT_FAILURE(vrc))
|
---|
2903 | {
|
---|
2904 | rc = pTask->that->setErrorBoth(E_FAIL, vrc, tr("Could not create the communication channel (%Rrc)"), vrc);
|
---|
2905 | break;
|
---|
2906 | }
|
---|
2907 |
|
---|
2908 | /* get the path to the executable */
|
---|
2909 | char exePathBuf[RTPATH_MAX];
|
---|
2910 | char *exePath = RTProcGetExecutablePath(exePathBuf, RTPATH_MAX);
|
---|
2911 | if (!exePath)
|
---|
2912 | {
|
---|
2913 | rc = pTask->that->setError(E_FAIL, tr("Cannot get executable name"));
|
---|
2914 | break;
|
---|
2915 | }
|
---|
2916 |
|
---|
2917 | Utf8Str argsStr = Utf8StrFmt("/Helper %s", client.name().c_str());
|
---|
2918 |
|
---|
2919 | LogFlowFunc(("Starting '\"%s\" %s'...\n", exePath, argsStr.c_str()));
|
---|
2920 |
|
---|
2921 | RTPROCESS pid = NIL_RTPROCESS;
|
---|
2922 |
|
---|
2923 | if (pTask->privileged)
|
---|
2924 | {
|
---|
2925 | /* Attempt to start a privileged process using the Run As dialog */
|
---|
2926 |
|
---|
2927 | Bstr file = exePath;
|
---|
2928 | Bstr parameters = argsStr;
|
---|
2929 |
|
---|
2930 | SHELLEXECUTEINFO shExecInfo;
|
---|
2931 |
|
---|
2932 | shExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
|
---|
2933 |
|
---|
2934 | shExecInfo.fMask = NULL;
|
---|
2935 | shExecInfo.hwnd = NULL;
|
---|
2936 | shExecInfo.lpVerb = L"runas";
|
---|
2937 | shExecInfo.lpFile = file.raw();
|
---|
2938 | shExecInfo.lpParameters = parameters.raw();
|
---|
2939 | shExecInfo.lpDirectory = NULL;
|
---|
2940 | shExecInfo.nShow = SW_NORMAL;
|
---|
2941 | shExecInfo.hInstApp = NULL;
|
---|
2942 |
|
---|
2943 | if (!ShellExecuteEx(&shExecInfo))
|
---|
2944 | {
|
---|
2945 | int vrc2 = RTErrConvertFromWin32(GetLastError());
|
---|
2946 | /* hide excessive details in case of a frequent error
|
---|
2947 | * (pressing the Cancel button to close the Run As dialog) */
|
---|
2948 | if (vrc2 == VERR_CANCELLED)
|
---|
2949 | rc = pTask->that->setErrorBoth(E_FAIL, vrc, tr("Operation canceled by the user"));
|
---|
2950 | else
|
---|
2951 | rc = pTask->that->setErrorBoth(E_FAIL, vrc, tr("Could not launch a privileged process '%s' (%Rrc)"), exePath, vrc2);
|
---|
2952 | break;
|
---|
2953 | }
|
---|
2954 | }
|
---|
2955 | else
|
---|
2956 | {
|
---|
2957 | const char *args[] = { exePath, "/Helper", client.name().c_str(), 0 };
|
---|
2958 | vrc = RTProcCreate(exePath, args, RTENV_DEFAULT, 0, &pid);
|
---|
2959 | if (RT_FAILURE(vrc))
|
---|
2960 | {
|
---|
2961 | rc = pTask->that->setErrorBoth(E_FAIL, vrc, tr("Could not launch a process '%s' (%Rrc)"), exePath, vrc);
|
---|
2962 | break;
|
---|
2963 | }
|
---|
2964 | }
|
---|
2965 |
|
---|
2966 | /* wait for the client to connect */
|
---|
2967 | vrc = client.connect();
|
---|
2968 | if (RT_SUCCESS(vrc))
|
---|
2969 | {
|
---|
2970 | /* start the user supplied function */
|
---|
2971 | rc = pTask->func(&client, pTask->progress, pTask->user, &vrc);
|
---|
2972 | userFuncCalled = true;
|
---|
2973 | }
|
---|
2974 |
|
---|
2975 | /* send the termination signal to the process anyway */
|
---|
2976 | {
|
---|
2977 | int vrc2 = client.write(SVCHlpMsg::Null);
|
---|
2978 | if (RT_SUCCESS(vrc))
|
---|
2979 | vrc = vrc2;
|
---|
2980 | }
|
---|
2981 |
|
---|
2982 | if (SUCCEEDED(rc) && RT_FAILURE(vrc))
|
---|
2983 | {
|
---|
2984 | rc = pTask->that->setErrorBoth(E_FAIL, vrc, tr("Could not operate the communication channel (%Rrc)"), vrc);
|
---|
2985 | break;
|
---|
2986 | }
|
---|
2987 | }
|
---|
2988 | while (0);
|
---|
2989 |
|
---|
2990 | if (FAILED(rc) && !userFuncCalled)
|
---|
2991 | {
|
---|
2992 | /* call the user function in the "cleanup only" mode
|
---|
2993 | * to let it free resources passed to in aUser */
|
---|
2994 | pTask->func(NULL, NULL, pTask->user, NULL);
|
---|
2995 | }
|
---|
2996 |
|
---|
2997 | pTask->progress->i_notifyComplete(rc);
|
---|
2998 |
|
---|
2999 | LogFlowFuncLeave();
|
---|
3000 | }
|
---|
3001 |
|
---|
3002 | #endif /* RT_OS_WINDOWS */
|
---|
3003 |
|
---|
3004 | /**
|
---|
3005 | * Sends a signal to the client watcher to rescan the set of machines
|
---|
3006 | * that have open sessions.
|
---|
3007 | *
|
---|
3008 | * @note Doesn't lock anything.
|
---|
3009 | */
|
---|
3010 | void VirtualBox::i_updateClientWatcher()
|
---|
3011 | {
|
---|
3012 | AutoCaller autoCaller(this);
|
---|
3013 | AssertComRCReturnVoid(autoCaller.rc());
|
---|
3014 |
|
---|
3015 | AssertPtrReturnVoid(m->pClientWatcher);
|
---|
3016 | m->pClientWatcher->update();
|
---|
3017 | }
|
---|
3018 |
|
---|
3019 | /**
|
---|
3020 | * Adds the given child process ID to the list of processes to be reaped.
|
---|
3021 | * This call should be followed by #i_updateClientWatcher() to take the effect.
|
---|
3022 | *
|
---|
3023 | * @note Doesn't lock anything.
|
---|
3024 | */
|
---|
3025 | void VirtualBox::i_addProcessToReap(RTPROCESS pid)
|
---|
3026 | {
|
---|
3027 | AutoCaller autoCaller(this);
|
---|
3028 | AssertComRCReturnVoid(autoCaller.rc());
|
---|
3029 |
|
---|
3030 | AssertPtrReturnVoid(m->pClientWatcher);
|
---|
3031 | m->pClientWatcher->addProcess(pid);
|
---|
3032 | }
|
---|
3033 |
|
---|
3034 | /** Event for onMachineStateChange(), onMachineDataChange(), onMachineRegistered() */
|
---|
3035 | struct MachineEvent : public VirtualBox::CallbackEvent
|
---|
3036 | {
|
---|
3037 | MachineEvent(VirtualBox *aVB, VBoxEventType_T aWhat, const Guid &aId, BOOL aBool)
|
---|
3038 | : CallbackEvent(aVB, aWhat), id(aId.toUtf16())
|
---|
3039 | , mBool(aBool)
|
---|
3040 | { }
|
---|
3041 |
|
---|
3042 | MachineEvent(VirtualBox *aVB, VBoxEventType_T aWhat, const Guid &aId, MachineState_T aState)
|
---|
3043 | : CallbackEvent(aVB, aWhat), id(aId.toUtf16())
|
---|
3044 | , mState(aState)
|
---|
3045 | {}
|
---|
3046 |
|
---|
3047 | virtual HRESULT prepareEventDesc(IEventSource* aSource, VBoxEventDesc& aEvDesc)
|
---|
3048 | {
|
---|
3049 | switch (mWhat)
|
---|
3050 | {
|
---|
3051 | case VBoxEventType_OnMachineDataChanged:
|
---|
3052 | aEvDesc.init(aSource, mWhat, id.raw(), mBool);
|
---|
3053 | break;
|
---|
3054 |
|
---|
3055 | case VBoxEventType_OnMachineStateChanged:
|
---|
3056 | aEvDesc.init(aSource, mWhat, id.raw(), mState);
|
---|
3057 | break;
|
---|
3058 |
|
---|
3059 | case VBoxEventType_OnMachineRegistered:
|
---|
3060 | aEvDesc.init(aSource, mWhat, id.raw(), mBool);
|
---|
3061 | break;
|
---|
3062 |
|
---|
3063 | default:
|
---|
3064 | AssertFailedReturn(S_OK);
|
---|
3065 | }
|
---|
3066 | return S_OK;
|
---|
3067 | }
|
---|
3068 |
|
---|
3069 | Bstr id;
|
---|
3070 | MachineState_T mState;
|
---|
3071 | BOOL mBool;
|
---|
3072 | };
|
---|
3073 |
|
---|
3074 |
|
---|
3075 | /**
|
---|
3076 | * VD plugin load
|
---|
3077 | */
|
---|
3078 | int VirtualBox::i_loadVDPlugin(const char *pszPluginLibrary)
|
---|
3079 | {
|
---|
3080 | return m->pSystemProperties->i_loadVDPlugin(pszPluginLibrary);
|
---|
3081 | }
|
---|
3082 |
|
---|
3083 | /**
|
---|
3084 | * VD plugin unload
|
---|
3085 | */
|
---|
3086 | int VirtualBox::i_unloadVDPlugin(const char *pszPluginLibrary)
|
---|
3087 | {
|
---|
3088 | return m->pSystemProperties->i_unloadVDPlugin(pszPluginLibrary);
|
---|
3089 | }
|
---|
3090 |
|
---|
3091 |
|
---|
3092 | /** Event for onMediumRegistered() */
|
---|
3093 | struct MediumRegisteredEventStruct : public VirtualBox::CallbackEvent
|
---|
3094 | {
|
---|
3095 | MediumRegisteredEventStruct(VirtualBox *aVB, const Guid &aMediumId,
|
---|
3096 | const DeviceType_T aDevType, const BOOL aRegistered)
|
---|
3097 | : CallbackEvent(aVB, VBoxEventType_OnMediumRegistered)
|
---|
3098 | , mMediumId(aMediumId.toUtf16()), mDevType(aDevType), mRegistered(aRegistered)
|
---|
3099 | {}
|
---|
3100 |
|
---|
3101 | virtual HRESULT prepareEventDesc(IEventSource *aSource, VBoxEventDesc &aEvDesc)
|
---|
3102 | {
|
---|
3103 | return aEvDesc.init(aSource, VBoxEventType_OnMediumRegistered, mMediumId.raw(), mDevType, mRegistered);
|
---|
3104 | }
|
---|
3105 |
|
---|
3106 | Bstr mMediumId;
|
---|
3107 | DeviceType_T mDevType;
|
---|
3108 | BOOL mRegistered;
|
---|
3109 | };
|
---|
3110 |
|
---|
3111 | /**
|
---|
3112 | * @note Doesn't lock any object.
|
---|
3113 | */
|
---|
3114 | void VirtualBox::i_onMediumRegistered(const Guid &aMediumId, const DeviceType_T aDevType, const BOOL aRegistered)
|
---|
3115 | {
|
---|
3116 | i_postEvent(new MediumRegisteredEventStruct(this, aMediumId, aDevType, aRegistered));
|
---|
3117 | }
|
---|
3118 |
|
---|
3119 | /** Event for onMediumConfigChanged() */
|
---|
3120 | struct MediumConfigChangedEventStruct : public VirtualBox::CallbackEvent
|
---|
3121 | {
|
---|
3122 | MediumConfigChangedEventStruct(VirtualBox *aVB, IMedium *aMedium)
|
---|
3123 | : CallbackEvent(aVB, VBoxEventType_OnMediumConfigChanged)
|
---|
3124 | , mMedium(aMedium)
|
---|
3125 | {}
|
---|
3126 |
|
---|
3127 | virtual HRESULT prepareEventDesc(IEventSource *aSource, VBoxEventDesc &aEvDesc)
|
---|
3128 | {
|
---|
3129 | return aEvDesc.init(aSource, VBoxEventType_OnMediumConfigChanged, mMedium);
|
---|
3130 | }
|
---|
3131 |
|
---|
3132 | IMedium* mMedium;
|
---|
3133 | };
|
---|
3134 |
|
---|
3135 | void VirtualBox::i_onMediumConfigChanged(IMedium *aMedium)
|
---|
3136 | {
|
---|
3137 | i_postEvent(new MediumConfigChangedEventStruct(this, aMedium));
|
---|
3138 | }
|
---|
3139 |
|
---|
3140 | /** Event for onMediumChanged() */
|
---|
3141 | struct MediumChangedEventStruct : public VirtualBox::CallbackEvent
|
---|
3142 | {
|
---|
3143 | MediumChangedEventStruct(VirtualBox *aVB, IMediumAttachment *aMediumAttachment)
|
---|
3144 | : CallbackEvent(aVB, VBoxEventType_OnMediumChanged)
|
---|
3145 | , mMediumAttachment(aMediumAttachment)
|
---|
3146 | {}
|
---|
3147 |
|
---|
3148 | virtual HRESULT prepareEventDesc(IEventSource *aSource, VBoxEventDesc &aEvDesc)
|
---|
3149 | {
|
---|
3150 | return aEvDesc.init(aSource, VBoxEventType_OnMediumChanged, mMediumAttachment);
|
---|
3151 | }
|
---|
3152 |
|
---|
3153 | IMediumAttachment* mMediumAttachment;
|
---|
3154 | };
|
---|
3155 |
|
---|
3156 | void VirtualBox::i_onMediumChanged(IMediumAttachment *aMediumAttachment)
|
---|
3157 | {
|
---|
3158 | i_postEvent(new MediumChangedEventStruct(this, aMediumAttachment));
|
---|
3159 | }
|
---|
3160 |
|
---|
3161 | /** Event for onStorageControllerChanged() */
|
---|
3162 | struct StorageControllerChangedEventStruct : public VirtualBox::CallbackEvent
|
---|
3163 | {
|
---|
3164 | StorageControllerChangedEventStruct(VirtualBox *aVB, const Guid &aMachineId,
|
---|
3165 | const com::Utf8Str &aControllerName)
|
---|
3166 | : CallbackEvent(aVB, VBoxEventType_OnStorageControllerChanged)
|
---|
3167 | , mMachineId(aMachineId.toUtf16()), mControllerName(aControllerName)
|
---|
3168 | {}
|
---|
3169 |
|
---|
3170 | virtual HRESULT prepareEventDesc(IEventSource *aSource, VBoxEventDesc &aEvDesc)
|
---|
3171 | {
|
---|
3172 | return aEvDesc.init(aSource, VBoxEventType_OnStorageControllerChanged, mMachineId.raw(), mControllerName.raw());
|
---|
3173 | }
|
---|
3174 |
|
---|
3175 | Bstr mMachineId;
|
---|
3176 | Bstr mControllerName;
|
---|
3177 | };
|
---|
3178 |
|
---|
3179 | /**
|
---|
3180 | * @note Doesn't lock any object.
|
---|
3181 | */
|
---|
3182 | void VirtualBox::i_onStorageControllerChanged(const Guid &aMachineId, const com::Utf8Str &aControllerName)
|
---|
3183 | {
|
---|
3184 | i_postEvent(new StorageControllerChangedEventStruct(this, aMachineId, aControllerName));
|
---|
3185 | }
|
---|
3186 |
|
---|
3187 | /** Event for onStorageDeviceChanged() */
|
---|
3188 | struct StorageDeviceChangedEventStruct : public VirtualBox::CallbackEvent
|
---|
3189 | {
|
---|
3190 | StorageDeviceChangedEventStruct(VirtualBox *aVB, IMediumAttachment *aStorageDevice, BOOL fRemoved, BOOL fSilent)
|
---|
3191 | : CallbackEvent(aVB, VBoxEventType_OnStorageDeviceChanged)
|
---|
3192 | , mStorageDevice(aStorageDevice)
|
---|
3193 | , mRemoved(fRemoved)
|
---|
3194 | , mSilent(fSilent)
|
---|
3195 | {}
|
---|
3196 |
|
---|
3197 | virtual HRESULT prepareEventDesc(IEventSource *aSource, VBoxEventDesc &aEvDesc)
|
---|
3198 | {
|
---|
3199 | return aEvDesc.init(aSource, VBoxEventType_OnStorageDeviceChanged, mStorageDevice, mRemoved, mSilent);
|
---|
3200 | }
|
---|
3201 |
|
---|
3202 | IMediumAttachment* mStorageDevice;
|
---|
3203 | BOOL mRemoved;
|
---|
3204 | BOOL mSilent;
|
---|
3205 | };
|
---|
3206 |
|
---|
3207 | void VirtualBox::i_onStorageDeviceChanged(IMediumAttachment *aStorageDevice, const BOOL fRemoved, const BOOL fSilent)
|
---|
3208 | {
|
---|
3209 | i_postEvent(new StorageDeviceChangedEventStruct(this, aStorageDevice, fRemoved, fSilent));
|
---|
3210 | }
|
---|
3211 |
|
---|
3212 | /**
|
---|
3213 | * @note Doesn't lock any object.
|
---|
3214 | */
|
---|
3215 | void VirtualBox::i_onMachineStateChange(const Guid &aId, MachineState_T aState)
|
---|
3216 | {
|
---|
3217 | i_postEvent(new MachineEvent(this, VBoxEventType_OnMachineStateChanged, aId, aState));
|
---|
3218 | }
|
---|
3219 |
|
---|
3220 | /**
|
---|
3221 | * @note Doesn't lock any object.
|
---|
3222 | */
|
---|
3223 | void VirtualBox::i_onMachineDataChange(const Guid &aId, BOOL aTemporary)
|
---|
3224 | {
|
---|
3225 | i_postEvent(new MachineEvent(this, VBoxEventType_OnMachineDataChanged, aId, aTemporary));
|
---|
3226 | }
|
---|
3227 |
|
---|
3228 | /**
|
---|
3229 | * @note Locks this object for reading.
|
---|
3230 | */
|
---|
3231 | BOOL VirtualBox::i_onExtraDataCanChange(const Guid &aId, IN_BSTR aKey, IN_BSTR aValue,
|
---|
3232 | Bstr &aError)
|
---|
3233 | {
|
---|
3234 | LogFlowThisFunc(("machine={%s} aKey={%ls} aValue={%ls}\n",
|
---|
3235 | aId.toString().c_str(), aKey, aValue));
|
---|
3236 |
|
---|
3237 | AutoCaller autoCaller(this);
|
---|
3238 | AssertComRCReturn(autoCaller.rc(), FALSE);
|
---|
3239 |
|
---|
3240 | BOOL allowChange = TRUE;
|
---|
3241 | Bstr id = aId.toUtf16();
|
---|
3242 |
|
---|
3243 | VBoxEventDesc evDesc;
|
---|
3244 | evDesc.init(m->pEventSource, VBoxEventType_OnExtraDataCanChange, id.raw(), aKey, aValue);
|
---|
3245 | BOOL fDelivered = evDesc.fire(3000); /* Wait up to 3 secs for delivery */
|
---|
3246 | //Assert(fDelivered);
|
---|
3247 | if (fDelivered)
|
---|
3248 | {
|
---|
3249 | ComPtr<IEvent> aEvent;
|
---|
3250 | evDesc.getEvent(aEvent.asOutParam());
|
---|
3251 | ComPtr<IExtraDataCanChangeEvent> aCanChangeEvent = aEvent;
|
---|
3252 | Assert(aCanChangeEvent);
|
---|
3253 | BOOL fVetoed = FALSE;
|
---|
3254 | aCanChangeEvent->IsVetoed(&fVetoed);
|
---|
3255 | allowChange = !fVetoed;
|
---|
3256 |
|
---|
3257 | if (!allowChange)
|
---|
3258 | {
|
---|
3259 | SafeArray<BSTR> aVetos;
|
---|
3260 | aCanChangeEvent->GetVetos(ComSafeArrayAsOutParam(aVetos));
|
---|
3261 | if (aVetos.size() > 0)
|
---|
3262 | aError = aVetos[0];
|
---|
3263 | }
|
---|
3264 | }
|
---|
3265 | else
|
---|
3266 | allowChange = TRUE;
|
---|
3267 |
|
---|
3268 | LogFlowThisFunc(("allowChange=%RTbool\n", allowChange));
|
---|
3269 | return allowChange;
|
---|
3270 | }
|
---|
3271 |
|
---|
3272 | /** Event for onExtraDataChange() */
|
---|
3273 | struct ExtraDataEvent : public VirtualBox::CallbackEvent
|
---|
3274 | {
|
---|
3275 | ExtraDataEvent(VirtualBox *aVB, const Guid &aMachineId,
|
---|
3276 | IN_BSTR aKey, IN_BSTR aVal)
|
---|
3277 | : CallbackEvent(aVB, VBoxEventType_OnExtraDataChanged)
|
---|
3278 | , machineId(aMachineId.toUtf16()), key(aKey), val(aVal)
|
---|
3279 | {}
|
---|
3280 |
|
---|
3281 | virtual HRESULT prepareEventDesc(IEventSource* aSource, VBoxEventDesc& aEvDesc)
|
---|
3282 | {
|
---|
3283 | return aEvDesc.init(aSource, VBoxEventType_OnExtraDataChanged, machineId.raw(), key.raw(), val.raw());
|
---|
3284 | }
|
---|
3285 |
|
---|
3286 | Bstr machineId, key, val;
|
---|
3287 | };
|
---|
3288 |
|
---|
3289 | /**
|
---|
3290 | * @note Doesn't lock any object.
|
---|
3291 | */
|
---|
3292 | void VirtualBox::i_onExtraDataChange(const Guid &aId, IN_BSTR aKey, IN_BSTR aValue)
|
---|
3293 | {
|
---|
3294 | i_postEvent(new ExtraDataEvent(this, aId, aKey, aValue));
|
---|
3295 | }
|
---|
3296 |
|
---|
3297 | /**
|
---|
3298 | * @note Doesn't lock any object.
|
---|
3299 | */
|
---|
3300 | void VirtualBox::i_onMachineRegistered(const Guid &aId, BOOL aRegistered)
|
---|
3301 | {
|
---|
3302 | i_postEvent(new MachineEvent(this, VBoxEventType_OnMachineRegistered, aId, aRegistered));
|
---|
3303 | }
|
---|
3304 |
|
---|
3305 | /** Event for onSessionStateChange() */
|
---|
3306 | struct SessionEvent : public VirtualBox::CallbackEvent
|
---|
3307 | {
|
---|
3308 | SessionEvent(VirtualBox *aVB, const Guid &aMachineId, SessionState_T aState)
|
---|
3309 | : CallbackEvent(aVB, VBoxEventType_OnSessionStateChanged)
|
---|
3310 | , machineId(aMachineId.toUtf16()), sessionState(aState)
|
---|
3311 | {}
|
---|
3312 |
|
---|
3313 | virtual HRESULT prepareEventDesc(IEventSource* aSource, VBoxEventDesc& aEvDesc)
|
---|
3314 | {
|
---|
3315 | return aEvDesc.init(aSource, VBoxEventType_OnSessionStateChanged, machineId.raw(), sessionState);
|
---|
3316 | }
|
---|
3317 | Bstr machineId;
|
---|
3318 | SessionState_T sessionState;
|
---|
3319 | };
|
---|
3320 |
|
---|
3321 | /**
|
---|
3322 | * @note Doesn't lock any object.
|
---|
3323 | */
|
---|
3324 | void VirtualBox::i_onSessionStateChange(const Guid &aId, SessionState_T aState)
|
---|
3325 | {
|
---|
3326 | i_postEvent(new SessionEvent(this, aId, aState));
|
---|
3327 | }
|
---|
3328 |
|
---|
3329 | /** Event for i_onSnapshotTaken(), i_onSnapshotDeleted(), i_onSnapshotRestored() and i_onSnapshotChange() */
|
---|
3330 | struct SnapshotEvent : public VirtualBox::CallbackEvent
|
---|
3331 | {
|
---|
3332 | SnapshotEvent(VirtualBox *aVB, const Guid &aMachineId, const Guid &aSnapshotId,
|
---|
3333 | VBoxEventType_T aWhat)
|
---|
3334 | : CallbackEvent(aVB, aWhat)
|
---|
3335 | , machineId(aMachineId), snapshotId(aSnapshotId)
|
---|
3336 | {}
|
---|
3337 |
|
---|
3338 | virtual HRESULT prepareEventDesc(IEventSource* aSource, VBoxEventDesc& aEvDesc)
|
---|
3339 | {
|
---|
3340 | return aEvDesc.init(aSource, mWhat, machineId.toUtf16().raw(),
|
---|
3341 | snapshotId.toUtf16().raw());
|
---|
3342 | }
|
---|
3343 |
|
---|
3344 | Guid machineId;
|
---|
3345 | Guid snapshotId;
|
---|
3346 | };
|
---|
3347 |
|
---|
3348 | /**
|
---|
3349 | * @note Doesn't lock any object.
|
---|
3350 | */
|
---|
3351 | void VirtualBox::i_onSnapshotTaken(const Guid &aMachineId, const Guid &aSnapshotId)
|
---|
3352 | {
|
---|
3353 | i_postEvent(new SnapshotEvent(this, aMachineId, aSnapshotId,
|
---|
3354 | VBoxEventType_OnSnapshotTaken));
|
---|
3355 | }
|
---|
3356 |
|
---|
3357 | /**
|
---|
3358 | * @note Doesn't lock any object.
|
---|
3359 | */
|
---|
3360 | void VirtualBox::i_onSnapshotDeleted(const Guid &aMachineId, const Guid &aSnapshotId)
|
---|
3361 | {
|
---|
3362 | i_postEvent(new SnapshotEvent(this, aMachineId, aSnapshotId,
|
---|
3363 | VBoxEventType_OnSnapshotDeleted));
|
---|
3364 | }
|
---|
3365 |
|
---|
3366 | /**
|
---|
3367 | * @note Doesn't lock any object.
|
---|
3368 | */
|
---|
3369 | void VirtualBox::i_onSnapshotRestored(const Guid &aMachineId, const Guid &aSnapshotId)
|
---|
3370 | {
|
---|
3371 | i_postEvent(new SnapshotEvent(this, aMachineId, aSnapshotId,
|
---|
3372 | VBoxEventType_OnSnapshotRestored));
|
---|
3373 | }
|
---|
3374 |
|
---|
3375 | /**
|
---|
3376 | * @note Doesn't lock any object.
|
---|
3377 | */
|
---|
3378 | void VirtualBox::i_onSnapshotChange(const Guid &aMachineId, const Guid &aSnapshotId)
|
---|
3379 | {
|
---|
3380 | i_postEvent(new SnapshotEvent(this, aMachineId, aSnapshotId,
|
---|
3381 | VBoxEventType_OnSnapshotChanged));
|
---|
3382 | }
|
---|
3383 |
|
---|
3384 | /** Event for onGuestPropertyChange() */
|
---|
3385 | struct GuestPropertyEvent : public VirtualBox::CallbackEvent
|
---|
3386 | {
|
---|
3387 | GuestPropertyEvent(VirtualBox *aVBox, const Guid &aMachineId,
|
---|
3388 | IN_BSTR aName, IN_BSTR aValue, IN_BSTR aFlags)
|
---|
3389 | : CallbackEvent(aVBox, VBoxEventType_OnGuestPropertyChanged),
|
---|
3390 | machineId(aMachineId),
|
---|
3391 | name(aName),
|
---|
3392 | value(aValue),
|
---|
3393 | flags(aFlags)
|
---|
3394 | {}
|
---|
3395 |
|
---|
3396 | virtual HRESULT prepareEventDesc(IEventSource* aSource, VBoxEventDesc& aEvDesc)
|
---|
3397 | {
|
---|
3398 | return aEvDesc.init(aSource, VBoxEventType_OnGuestPropertyChanged,
|
---|
3399 | machineId.toUtf16().raw(), name.raw(), value.raw(), flags.raw());
|
---|
3400 | }
|
---|
3401 |
|
---|
3402 | Guid machineId;
|
---|
3403 | Bstr name, value, flags;
|
---|
3404 | };
|
---|
3405 |
|
---|
3406 | #ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
|
---|
3407 | /**
|
---|
3408 | * Generates a new clipboard area on the host by opening (and locking) a new, temporary directory.
|
---|
3409 | *
|
---|
3410 | * @returns VBox status code.
|
---|
3411 | * @param uAreaID Clipboard area ID to use for creation.
|
---|
3412 | * @param fFlags Additional creation flags; currently unused and ignored.
|
---|
3413 | * @param ppAreaData Where to return the created clipboard area on success.
|
---|
3414 | */
|
---|
3415 | int VirtualBox::i_clipboardAreaCreate(ULONG uAreaID, uint32_t fFlags, SharedClipboardAreaData **ppAreaData)
|
---|
3416 | {
|
---|
3417 | RT_NOREF(fFlags);
|
---|
3418 |
|
---|
3419 | int vrc;
|
---|
3420 |
|
---|
3421 | SharedClipboardAreaData *pAreaData = new SharedClipboardAreaData();
|
---|
3422 | if (pAreaData)
|
---|
3423 | {
|
---|
3424 | vrc = pAreaData->Area.OpenTemp(uAreaID, SHCLAREA_OPEN_FLAGS_MUST_NOT_EXIST);
|
---|
3425 | if (RT_SUCCESS(vrc))
|
---|
3426 | {
|
---|
3427 | pAreaData->uID = uAreaID;
|
---|
3428 |
|
---|
3429 | *ppAreaData = pAreaData;
|
---|
3430 | }
|
---|
3431 | }
|
---|
3432 | else
|
---|
3433 | vrc = VERR_NO_MEMORY;
|
---|
3434 |
|
---|
3435 | LogFlowFunc(("uID=%RU32, rc=%Rrc\n", uAreaID, vrc));
|
---|
3436 | return vrc;
|
---|
3437 | }
|
---|
3438 |
|
---|
3439 | /**
|
---|
3440 | * Destroys a formerly created clipboard area.
|
---|
3441 | *
|
---|
3442 | * @returns VBox status code.
|
---|
3443 | * @param pAreaData Area data to destroy. The pointer will be invalid on successful return.
|
---|
3444 | */
|
---|
3445 | int VirtualBox::i_clipboardAreaDestroy(SharedClipboardAreaData *pAreaData)
|
---|
3446 | {
|
---|
3447 | if (!pAreaData)
|
---|
3448 | return VINF_SUCCESS;
|
---|
3449 |
|
---|
3450 | /** @todo Do we need a worker for this to not block here for too long?
|
---|
3451 | * This could take a while to clean up huge areas ... */
|
---|
3452 | int vrc = pAreaData->Area.Close();
|
---|
3453 | if (RT_SUCCESS(vrc))
|
---|
3454 | {
|
---|
3455 | delete pAreaData;
|
---|
3456 | pAreaData = NULL;
|
---|
3457 | }
|
---|
3458 |
|
---|
3459 | LogFlowFunc(("uID=%RU32, rc=%Rrc\n", pAreaData->uID, vrc));
|
---|
3460 | return vrc;
|
---|
3461 | }
|
---|
3462 |
|
---|
3463 | /**
|
---|
3464 | * Registers and creates a new clipboard area on the host (for all VMs), returned the clipboard area ID for it.
|
---|
3465 | *
|
---|
3466 | * @returns HRESULT
|
---|
3467 | * @param aParms Creation parameters. Currently unused.
|
---|
3468 | * @param aID Where to return the clipboard area ID on success.
|
---|
3469 | */
|
---|
3470 | HRESULT VirtualBox::i_onClipboardAreaRegister(const std::vector<com::Utf8Str> &aParms, ULONG *aID)
|
---|
3471 | {
|
---|
3472 | RT_NOREF(aParms);
|
---|
3473 |
|
---|
3474 | HRESULT rc = S_OK;
|
---|
3475 |
|
---|
3476 | int vrc = RTCritSectEnter(&m->SharedClipboard.CritSect);
|
---|
3477 | if (RT_SUCCESS(vrc))
|
---|
3478 | {
|
---|
3479 | try
|
---|
3480 | {
|
---|
3481 | if (m->SharedClipboard.mapClipboardAreas.size() < m->SharedClipboard.uMaxClipboardAreas)
|
---|
3482 | {
|
---|
3483 | for (unsigned uTries = 0; uTries < 32; uTries++) /* Don't try too hard. */
|
---|
3484 | {
|
---|
3485 | const ULONG uAreaID = m->SharedClipboard.GenerateAreaID();
|
---|
3486 |
|
---|
3487 | /* Area ID already taken? */
|
---|
3488 | if (m->SharedClipboard.mapClipboardAreas.find(uAreaID) != m->SharedClipboard.mapClipboardAreas.end())
|
---|
3489 | continue;
|
---|
3490 |
|
---|
3491 | SharedClipboardAreaData *pAreaData;
|
---|
3492 | vrc = i_clipboardAreaCreate(uAreaID, 0 /* fFlags */, &pAreaData);
|
---|
3493 | if (RT_SUCCESS(vrc))
|
---|
3494 | {
|
---|
3495 | m->SharedClipboard.mapClipboardAreas[uAreaID] = pAreaData;
|
---|
3496 | m->SharedClipboard.uMostRecentClipboardAreaID = uAreaID;
|
---|
3497 |
|
---|
3498 | /** @todo Implement collision detection / wrap-around. */
|
---|
3499 |
|
---|
3500 | if (aID)
|
---|
3501 | *aID = uAreaID;
|
---|
3502 |
|
---|
3503 | LogThisFunc(("Registered new clipboard area %RU32: '%s'\n",
|
---|
3504 | uAreaID, pAreaData->Area.GetDirAbs()));
|
---|
3505 | break;
|
---|
3506 | }
|
---|
3507 | }
|
---|
3508 |
|
---|
3509 | if (RT_FAILURE(vrc))
|
---|
3510 | rc = setError(E_FAIL, /** @todo Find a better rc. */
|
---|
3511 | tr("Failed to create new clipboard area (%Rrc)"), vrc);
|
---|
3512 | }
|
---|
3513 | else
|
---|
3514 | {
|
---|
3515 | rc = setError(E_FAIL, /** @todo Find a better rc. */
|
---|
3516 | tr("Maximum number of concurrent clipboard areas reached (%RU32)"),
|
---|
3517 | m->SharedClipboard.uMaxClipboardAreas);
|
---|
3518 | }
|
---|
3519 | }
|
---|
3520 | catch (std::bad_alloc &ba)
|
---|
3521 | {
|
---|
3522 | vrc = VERR_NO_MEMORY;
|
---|
3523 | RT_NOREF(ba);
|
---|
3524 | }
|
---|
3525 |
|
---|
3526 | RTCritSectLeave(&m->SharedClipboard.CritSect);
|
---|
3527 | }
|
---|
3528 | LogFlowThisFunc(("rc=%Rhrc\n", rc));
|
---|
3529 | return rc;
|
---|
3530 | }
|
---|
3531 |
|
---|
3532 | /**
|
---|
3533 | * Unregisters (destroys) a formerly created clipboard area.
|
---|
3534 | *
|
---|
3535 | * @returns HRESULT
|
---|
3536 | * @param aID ID of clipboard area to destroy.
|
---|
3537 | */
|
---|
3538 | HRESULT VirtualBox::i_onClipboardAreaUnregister(ULONG aID)
|
---|
3539 | {
|
---|
3540 | HRESULT rc = S_OK;
|
---|
3541 |
|
---|
3542 | int vrc = RTCritSectEnter(&m->SharedClipboard.CritSect);
|
---|
3543 | if (RT_SUCCESS(vrc))
|
---|
3544 | {
|
---|
3545 | SharedClipboardAreaMap::iterator itArea = m->SharedClipboard.mapClipboardAreas.find(aID);
|
---|
3546 | if (itArea != m->SharedClipboard.mapClipboardAreas.end())
|
---|
3547 | {
|
---|
3548 | if (itArea->second->Area.GetRefCount() == 0)
|
---|
3549 | {
|
---|
3550 | vrc = i_clipboardAreaDestroy(itArea->second);
|
---|
3551 | if (RT_SUCCESS(vrc))
|
---|
3552 | {
|
---|
3553 | m->SharedClipboard.mapClipboardAreas.erase(itArea);
|
---|
3554 | }
|
---|
3555 | }
|
---|
3556 | else
|
---|
3557 | rc = setError(E_ACCESSDENIED, /** @todo Find a better rc. */
|
---|
3558 | tr("Area with ID %RU32 still in used, cannot unregister"), aID);
|
---|
3559 | }
|
---|
3560 | else
|
---|
3561 | rc = setError(VBOX_E_OBJECT_NOT_FOUND, /** @todo Find a better rc. */
|
---|
3562 | tr("Could not find a registered clipboard area with ID %RU32"), aID);
|
---|
3563 |
|
---|
3564 | int vrc2 = RTCritSectLeave(&m->SharedClipboard.CritSect);
|
---|
3565 | AssertRC(vrc2);
|
---|
3566 | }
|
---|
3567 | LogFlowThisFunc(("aID=%RU32, rc=%Rhrc\n", aID, rc));
|
---|
3568 | return rc;
|
---|
3569 | }
|
---|
3570 |
|
---|
3571 | /**
|
---|
3572 | * Attaches to an existing clipboard area.
|
---|
3573 | *
|
---|
3574 | * @returns HRESULT
|
---|
3575 | * @param aID ID of clipboard area to attach.
|
---|
3576 | */
|
---|
3577 | HRESULT VirtualBox::i_onClipboardAreaAttach(ULONG aID)
|
---|
3578 | {
|
---|
3579 | HRESULT rc = S_OK;
|
---|
3580 |
|
---|
3581 | int vrc = RTCritSectEnter(&m->SharedClipboard.CritSect);
|
---|
3582 | if (RT_SUCCESS(vrc))
|
---|
3583 | {
|
---|
3584 | SharedClipboardAreaMap::iterator itArea = m->SharedClipboard.mapClipboardAreas.find(aID);
|
---|
3585 | if (itArea != m->SharedClipboard.mapClipboardAreas.end())
|
---|
3586 | {
|
---|
3587 | const uint32_t cRefs = itArea->second->Area.AddRef();
|
---|
3588 | RT_NOREF(cRefs);
|
---|
3589 | LogFlowThisFunc(("aID=%RU32 -> cRefs=%RU32\n", aID, cRefs));
|
---|
3590 | vrc = VINF_SUCCESS;
|
---|
3591 | }
|
---|
3592 | else
|
---|
3593 | rc = setError(VBOX_E_OBJECT_NOT_FOUND, /** @todo Find a better rc. */
|
---|
3594 | tr("Could not find a registered clipboard area with ID %RU32"), aID);
|
---|
3595 |
|
---|
3596 | int vrc2 = RTCritSectLeave(&m->SharedClipboard.CritSect);
|
---|
3597 | AssertRC(vrc2);
|
---|
3598 | }
|
---|
3599 | LogFlowThisFunc(("aID=%RU32, rc=%Rhrc\n", aID, rc));
|
---|
3600 | return rc;
|
---|
3601 | }
|
---|
3602 |
|
---|
3603 | /**
|
---|
3604 | * Detaches from an existing clipboard area.
|
---|
3605 | *
|
---|
3606 | * @returns HRESULT
|
---|
3607 | * @param aID ID of clipboard area to detach from.
|
---|
3608 | */
|
---|
3609 | HRESULT VirtualBox::i_onClipboardAreaDetach(ULONG aID)
|
---|
3610 | {
|
---|
3611 | HRESULT rc = S_OK;
|
---|
3612 |
|
---|
3613 | int vrc = RTCritSectEnter(&m->SharedClipboard.CritSect);
|
---|
3614 | if (RT_SUCCESS(vrc))
|
---|
3615 | {
|
---|
3616 | SharedClipboardAreaMap::iterator itArea = m->SharedClipboard.mapClipboardAreas.find(aID);
|
---|
3617 | if (itArea != m->SharedClipboard.mapClipboardAreas.end())
|
---|
3618 | {
|
---|
3619 | const uint32_t cRefs = itArea->second->Area.Release();
|
---|
3620 | RT_NOREF(cRefs);
|
---|
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_SHCLAREAID 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_TRANSFERS */
|
---|
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: */
|
---|