VirtualBox

source: vbox/trunk/src/VBox/Main/src-client/ConsoleImpl.cpp@ 48112

Last change on this file since 48112 was 48054, checked in by vboxsync, 11 years ago

Main: Add new extradata setting VBoxInternal2/TurnResetIntoPowerOff to power off the VM when it is reset. Add guest property to reflect the last power off reason

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 339.8 KB
Line 
1/* $Id: ConsoleImpl.cpp 48054 2013-08-26 10:30:25Z vboxsync $ */
2/** @file
3 * VBox Console COM Class implementation
4 */
5
6/*
7 * Copyright (C) 2005-2013 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/** @todo Move the TAP mess back into the driver! */
19#if defined(RT_OS_WINDOWS)
20#elif defined(RT_OS_LINUX)
21# include <errno.h>
22# include <sys/ioctl.h>
23# include <sys/poll.h>
24# include <sys/fcntl.h>
25# include <sys/types.h>
26# include <sys/wait.h>
27# include <net/if.h>
28# include <linux/if_tun.h>
29# include <stdio.h>
30# include <stdlib.h>
31# include <string.h>
32#elif defined(RT_OS_FREEBSD)
33# include <errno.h>
34# include <sys/ioctl.h>
35# include <sys/poll.h>
36# include <sys/fcntl.h>
37# include <sys/types.h>
38# include <sys/wait.h>
39# include <stdio.h>
40# include <stdlib.h>
41# include <string.h>
42#elif defined(RT_OS_SOLARIS)
43# include <iprt/coredumper.h>
44#endif
45
46#include "ConsoleImpl.h"
47
48#include "Global.h"
49#include "VirtualBoxErrorInfoImpl.h"
50#include "GuestImpl.h"
51#include "KeyboardImpl.h"
52#include "MouseImpl.h"
53#include "DisplayImpl.h"
54#include "MachineDebuggerImpl.h"
55#include "USBDeviceImpl.h"
56#include "RemoteUSBDeviceImpl.h"
57#include "SharedFolderImpl.h"
58#include "AudioSnifferInterface.h"
59#include "Nvram.h"
60#ifdef VBOX_WITH_USB_VIDEO
61# include "UsbWebcamInterface.h"
62#endif
63#ifdef VBOX_WITH_USB_CARDREADER
64# include "UsbCardReader.h"
65#endif
66#include "ProgressImpl.h"
67#include "ConsoleVRDPServer.h"
68#include "VMMDev.h"
69#ifdef VBOX_WITH_EXTPACK
70# include "ExtPackManagerImpl.h"
71#endif
72#include "BusAssignmentManager.h"
73
74#include "VBoxEvents.h"
75#include "AutoCaller.h"
76#include "Logging.h"
77
78#include <VBox/com/array.h>
79#include "VBox/com/ErrorInfo.h"
80#include <VBox/com/listeners.h>
81
82#include <iprt/asm.h>
83#include <iprt/buildconfig.h>
84#include <iprt/cpp/utils.h>
85#include <iprt/dir.h>
86#include <iprt/file.h>
87#include <iprt/ldr.h>
88#include <iprt/path.h>
89#include <iprt/process.h>
90#include <iprt/string.h>
91#include <iprt/system.h>
92
93#include <VBox/vmm/vmapi.h>
94#include <VBox/vmm/vmm.h>
95#include <VBox/vmm/pdmapi.h>
96#include <VBox/vmm/pdmasynccompletion.h>
97#include <VBox/vmm/pdmnetifs.h>
98#ifdef VBOX_WITH_USB
99# include <VBox/vmm/pdmusb.h>
100#endif
101#ifdef VBOX_WITH_NETSHAPER
102# include <VBox/vmm/pdmnetshaper.h>
103#endif /* VBOX_WITH_NETSHAPER */
104#include <VBox/vmm/mm.h>
105#include <VBox/vmm/ftm.h>
106#include <VBox/vmm/ssm.h>
107#include <VBox/err.h>
108#include <VBox/param.h>
109#include <VBox/vusb.h>
110
111#include <VBox/VMMDev.h>
112
113#include <VBox/HostServices/VBoxClipboardSvc.h>
114#include <VBox/HostServices/DragAndDropSvc.h>
115#ifdef VBOX_WITH_GUEST_PROPS
116# include <VBox/HostServices/GuestPropertySvc.h>
117# include <VBox/com/array.h>
118#endif
119
120#include <set>
121#include <algorithm>
122#include <memory> // for auto_ptr
123#include <vector>
124
125
126// VMTask and friends
127////////////////////////////////////////////////////////////////////////////////
128
129/**
130 * Task structure for asynchronous VM operations.
131 *
132 * Once created, the task structure adds itself as a Console caller. This means:
133 *
134 * 1. The user must check for #rc() before using the created structure
135 * (e.g. passing it as a thread function argument). If #rc() returns a
136 * failure, the Console object may not be used by the task (see
137 * Console::addCaller() for more details).
138 * 2. On successful initialization, the structure keeps the Console caller
139 * until destruction (to ensure Console remains in the Ready state and won't
140 * be accidentally uninitialized). Forgetting to delete the created task
141 * will lead to Console::uninit() stuck waiting for releasing all added
142 * callers.
143 *
144 * If \a aUsesVMPtr parameter is true, the task structure will also add itself
145 * as a Console::mpUVM caller with the same meaning as above. See
146 * Console::addVMCaller() for more info.
147 */
148struct VMTask
149{
150 VMTask(Console *aConsole,
151 Progress *aProgress,
152 const ComPtr<IProgress> &aServerProgress,
153 bool aUsesVMPtr)
154 : mConsole(aConsole),
155 mConsoleCaller(aConsole),
156 mProgress(aProgress),
157 mServerProgress(aServerProgress),
158 mpUVM(NULL),
159 mRC(E_FAIL),
160 mpSafeVMPtr(NULL)
161 {
162 AssertReturnVoid(aConsole);
163 mRC = mConsoleCaller.rc();
164 if (FAILED(mRC))
165 return;
166 if (aUsesVMPtr)
167 {
168 mpSafeVMPtr = new Console::SafeVMPtr(aConsole);
169 if (mpSafeVMPtr->isOk())
170 mpUVM = mpSafeVMPtr->rawUVM();
171 else
172 mRC = mpSafeVMPtr->rc();
173 }
174 }
175
176 ~VMTask()
177 {
178 releaseVMCaller();
179 }
180
181 HRESULT rc() const { return mRC; }
182 bool isOk() const { return SUCCEEDED(rc()); }
183
184 /** Releases the VM caller before destruction. Not normally necessary. */
185 void releaseVMCaller()
186 {
187 if (mpSafeVMPtr)
188 {
189 delete mpSafeVMPtr;
190 mpSafeVMPtr = NULL;
191 }
192 }
193
194 const ComObjPtr<Console> mConsole;
195 AutoCaller mConsoleCaller;
196 const ComObjPtr<Progress> mProgress;
197 Utf8Str mErrorMsg;
198 const ComPtr<IProgress> mServerProgress;
199 PUVM mpUVM;
200
201private:
202 HRESULT mRC;
203 Console::SafeVMPtr *mpSafeVMPtr;
204};
205
206struct VMTakeSnapshotTask : public VMTask
207{
208 VMTakeSnapshotTask(Console *aConsole,
209 Progress *aProgress,
210 IN_BSTR aName,
211 IN_BSTR aDescription)
212 : VMTask(aConsole, aProgress, NULL /* aServerProgress */,
213 false /* aUsesVMPtr */),
214 bstrName(aName),
215 bstrDescription(aDescription),
216 lastMachineState(MachineState_Null)
217 {}
218
219 Bstr bstrName,
220 bstrDescription;
221 Bstr bstrSavedStateFile; // received from BeginTakeSnapshot()
222 MachineState_T lastMachineState;
223 bool fTakingSnapshotOnline;
224 ULONG ulMemSize;
225};
226
227struct VMPowerUpTask : public VMTask
228{
229 VMPowerUpTask(Console *aConsole,
230 Progress *aProgress)
231 : VMTask(aConsole, aProgress, NULL /* aServerProgress */,
232 false /* aUsesVMPtr */),
233 mConfigConstructor(NULL),
234 mStartPaused(false),
235 mTeleporterEnabled(FALSE),
236 mEnmFaultToleranceState(FaultToleranceState_Inactive)
237 {}
238
239 PFNCFGMCONSTRUCTOR mConfigConstructor;
240 Utf8Str mSavedStateFile;
241 Console::SharedFolderDataMap mSharedFolders;
242 bool mStartPaused;
243 BOOL mTeleporterEnabled;
244 FaultToleranceState_T mEnmFaultToleranceState;
245
246 /* array of progress objects for hard disk reset operations */
247 typedef std::list<ComPtr<IProgress> > ProgressList;
248 ProgressList hardDiskProgresses;
249};
250
251struct VMPowerDownTask : public VMTask
252{
253 VMPowerDownTask(Console *aConsole,
254 const ComPtr<IProgress> &aServerProgress)
255 : VMTask(aConsole, NULL /* aProgress */, aServerProgress,
256 true /* aUsesVMPtr */)
257 {}
258};
259
260struct VMSaveTask : public VMTask
261{
262 VMSaveTask(Console *aConsole,
263 const ComPtr<IProgress> &aServerProgress,
264 const Utf8Str &aSavedStateFile,
265 MachineState_T aMachineStateBefore,
266 Reason_T aReason)
267 : VMTask(aConsole, NULL /* aProgress */, aServerProgress,
268 true /* aUsesVMPtr */),
269 mSavedStateFile(aSavedStateFile),
270 mMachineStateBefore(aMachineStateBefore),
271 mReason(aReason)
272 {}
273
274 Utf8Str mSavedStateFile;
275 /* The local machine state we had before. Required if something fails */
276 MachineState_T mMachineStateBefore;
277 /* The reason for saving state */
278 Reason_T mReason;
279};
280
281// Handler for global events
282////////////////////////////////////////////////////////////////////////////////
283inline static const char *networkAdapterTypeToName(NetworkAdapterType_T adapterType);
284
285class VmEventListener {
286public:
287 VmEventListener()
288 {}
289
290
291 HRESULT init(Console *aConsole)
292 {
293 mConsole = aConsole;
294 return S_OK;
295 }
296
297 void uninit()
298 {
299 }
300
301 virtual ~VmEventListener()
302 {
303 }
304
305 STDMETHOD(HandleEvent)(VBoxEventType_T aType, IEvent * aEvent)
306 {
307 switch(aType)
308 {
309 case VBoxEventType_OnNATRedirect:
310 {
311 Bstr id;
312 ComPtr<IMachine> pMachine = mConsole->machine();
313 ComPtr<INATRedirectEvent> pNREv = aEvent;
314 HRESULT rc = E_FAIL;
315 Assert(pNREv);
316
317 Bstr interestedId;
318 rc = pMachine->COMGETTER(Id)(interestedId.asOutParam());
319 AssertComRC(rc);
320 rc = pNREv->COMGETTER(MachineId)(id.asOutParam());
321 AssertComRC(rc);
322 if (id != interestedId)
323 break;
324 /* now we can operate with redirects */
325 NATProtocol_T proto;
326 pNREv->COMGETTER(Proto)(&proto);
327 BOOL fRemove;
328 pNREv->COMGETTER(Remove)(&fRemove);
329 bool fUdp = (proto == NATProtocol_UDP);
330 Bstr hostIp, guestIp;
331 LONG hostPort, guestPort;
332 pNREv->COMGETTER(HostIP)(hostIp.asOutParam());
333 pNREv->COMGETTER(HostPort)(&hostPort);
334 pNREv->COMGETTER(GuestIP)(guestIp.asOutParam());
335 pNREv->COMGETTER(GuestPort)(&guestPort);
336 ULONG ulSlot;
337 rc = pNREv->COMGETTER(Slot)(&ulSlot);
338 AssertComRC(rc);
339 if (FAILED(rc))
340 break;
341 mConsole->onNATRedirectRuleChange(ulSlot, fRemove, proto, hostIp.raw(), hostPort, guestIp.raw(), guestPort);
342 }
343 break;
344
345 case VBoxEventType_OnHostPCIDevicePlug:
346 {
347 // handle if needed
348 break;
349 }
350
351 default:
352 AssertFailed();
353 }
354 return S_OK;
355 }
356private:
357 Console *mConsole;
358};
359
360typedef ListenerImpl<VmEventListener, Console*> VmEventListenerImpl;
361
362
363VBOX_LISTENER_DECLARE(VmEventListenerImpl)
364
365
366// constructor / destructor
367/////////////////////////////////////////////////////////////////////////////
368
369Console::Console()
370 : mSavedStateDataLoaded(false)
371 , mConsoleVRDPServer(NULL)
372 , mfVRDEChangeInProcess(false)
373 , mfVRDEChangePending(false)
374 , mpUVM(NULL)
375 , mVMCallers(0)
376 , mVMZeroCallersSem(NIL_RTSEMEVENT)
377 , mVMDestroying(false)
378 , mVMPoweredOff(false)
379 , mVMIsAlreadyPoweringOff(false)
380 , mfSnapshotFolderSizeWarningShown(false)
381 , mfSnapshotFolderExt4WarningShown(false)
382 , mfSnapshotFolderDiskTypeShown(false)
383 , mfVMHasUsbController(false)
384 , mpVmm2UserMethods(NULL)
385 , m_pVMMDev(NULL)
386 , mAudioSniffer(NULL)
387 , mNvram(NULL)
388#ifdef VBOX_WITH_USB_VIDEO
389 , mEmWebcam(NULL)
390#endif
391#ifdef VBOX_WITH_USB_CARDREADER
392 , mUsbCardReader(NULL)
393#endif
394 , mBusMgr(NULL)
395 , mVMStateChangeCallbackDisabled(false)
396 , mfUseHostClipboard(true)
397 , mMachineState(MachineState_PoweredOff)
398{
399}
400
401Console::~Console()
402{}
403
404HRESULT Console::FinalConstruct()
405{
406 LogFlowThisFunc(("\n"));
407
408 RT_ZERO(mapStorageLeds);
409 RT_ZERO(mapNetworkLeds);
410 RT_ZERO(mapUSBLed);
411 RT_ZERO(mapSharedFolderLed);
412
413 for (unsigned i = 0; i < RT_ELEMENTS(maStorageDevType); ++i)
414 maStorageDevType[i] = DeviceType_Null;
415
416 MYVMM2USERMETHODS *pVmm2UserMethods = (MYVMM2USERMETHODS *)RTMemAllocZ(sizeof(*mpVmm2UserMethods) + sizeof(Console *));
417 if (!pVmm2UserMethods)
418 return E_OUTOFMEMORY;
419 pVmm2UserMethods->u32Magic = VMM2USERMETHODS_MAGIC;
420 pVmm2UserMethods->u32Version = VMM2USERMETHODS_VERSION;
421 pVmm2UserMethods->pfnSaveState = Console::vmm2User_SaveState;
422 pVmm2UserMethods->pfnNotifyEmtInit = Console::vmm2User_NotifyEmtInit;
423 pVmm2UserMethods->pfnNotifyEmtTerm = Console::vmm2User_NotifyEmtTerm;
424 pVmm2UserMethods->pfnNotifyPdmtInit = Console::vmm2User_NotifyPdmtInit;
425 pVmm2UserMethods->pfnNotifyPdmtTerm = Console::vmm2User_NotifyPdmtTerm;
426 pVmm2UserMethods->u32EndMagic = VMM2USERMETHODS_MAGIC;
427 pVmm2UserMethods->pConsole = this;
428 mpVmm2UserMethods = pVmm2UserMethods;
429
430 return BaseFinalConstruct();
431}
432
433void Console::FinalRelease()
434{
435 LogFlowThisFunc(("\n"));
436
437 uninit();
438
439 BaseFinalRelease();
440}
441
442// public initializer/uninitializer for internal purposes only
443/////////////////////////////////////////////////////////////////////////////
444
445HRESULT Console::init(IMachine *aMachine, IInternalMachineControl *aControl, LockType_T aLockType)
446{
447 AssertReturn(aMachine && aControl, E_INVALIDARG);
448
449 /* Enclose the state transition NotReady->InInit->Ready */
450 AutoInitSpan autoInitSpan(this);
451 AssertReturn(autoInitSpan.isOk(), E_FAIL);
452
453 LogFlowThisFuncEnter();
454 LogFlowThisFunc(("aMachine=%p, aControl=%p\n", aMachine, aControl));
455
456 HRESULT rc = E_FAIL;
457
458 unconst(mMachine) = aMachine;
459 unconst(mControl) = aControl;
460
461 /* Cache essential properties and objects, and create child objects */
462
463 rc = mMachine->COMGETTER(State)(&mMachineState);
464 AssertComRCReturnRC(rc);
465
466#ifdef VBOX_WITH_EXTPACK
467 unconst(mptrExtPackManager).createObject();
468 rc = mptrExtPackManager->initExtPackManager(NULL, VBOXEXTPACKCTX_VM_PROCESS);
469 AssertComRCReturnRC(rc);
470#endif
471
472 // Event source may be needed by other children
473 unconst(mEventSource).createObject();
474 rc = mEventSource->init(static_cast<IConsole*>(this));
475 AssertComRCReturnRC(rc);
476
477 mcAudioRefs = 0;
478 mcVRDPClients = 0;
479 mu32SingleRDPClientId = 0;
480 mcGuestCredentialsProvided = false;
481
482 /* Now the VM specific parts */
483 if (aLockType == LockType_VM)
484 {
485 rc = mMachine->COMGETTER(VRDEServer)(unconst(mVRDEServer).asOutParam());
486 AssertComRCReturnRC(rc);
487
488 unconst(mGuest).createObject();
489 rc = mGuest->init(this);
490 AssertComRCReturnRC(rc);
491
492 unconst(mKeyboard).createObject();
493 rc = mKeyboard->init(this);
494 AssertComRCReturnRC(rc);
495
496 unconst(mMouse).createObject();
497 rc = mMouse->init(this);
498 AssertComRCReturnRC(rc);
499
500 unconst(mDisplay).createObject();
501 rc = mDisplay->init(this);
502 AssertComRCReturnRC(rc);
503
504 unconst(mVRDEServerInfo).createObject();
505 rc = mVRDEServerInfo->init(this);
506 AssertComRCReturnRC(rc);
507
508 /* Grab global and machine shared folder lists */
509
510 rc = fetchSharedFolders(true /* aGlobal */);
511 AssertComRCReturnRC(rc);
512 rc = fetchSharedFolders(false /* aGlobal */);
513 AssertComRCReturnRC(rc);
514
515 /* Create other child objects */
516
517 unconst(mConsoleVRDPServer) = new ConsoleVRDPServer(this);
518 AssertReturn(mConsoleVRDPServer, E_FAIL);
519
520 /* Figure out size of meAttachmentType vector */
521 ComPtr<IVirtualBox> pVirtualBox;
522 rc = aMachine->COMGETTER(Parent)(pVirtualBox.asOutParam());
523 AssertComRC(rc);
524 ComPtr<ISystemProperties> pSystemProperties;
525 if (pVirtualBox)
526 pVirtualBox->COMGETTER(SystemProperties)(pSystemProperties.asOutParam());
527 ChipsetType_T chipsetType = ChipsetType_PIIX3;
528 aMachine->COMGETTER(ChipsetType)(&chipsetType);
529 ULONG maxNetworkAdapters = 0;
530 if (pSystemProperties)
531 pSystemProperties->GetMaxNetworkAdapters(chipsetType, &maxNetworkAdapters);
532 meAttachmentType.resize(maxNetworkAdapters);
533 for (ULONG slot = 0; slot < maxNetworkAdapters; ++slot)
534 meAttachmentType[slot] = NetworkAttachmentType_Null;
535
536 // VirtualBox 4.0: We no longer initialize the VMMDev instance here,
537 // which starts the HGCM thread. Instead, this is now done in the
538 // power-up thread when a VM is actually being powered up to avoid
539 // having HGCM threads all over the place every time a session is
540 // opened, even if that session will not run a VM.
541 // unconst(m_pVMMDev) = new VMMDev(this);
542 // AssertReturn(mVMMDev, E_FAIL);
543
544 unconst(mAudioSniffer) = new AudioSniffer(this);
545 AssertReturn(mAudioSniffer, E_FAIL);
546
547 FirmwareType_T enmFirmwareType;
548 mMachine->COMGETTER(FirmwareType)(&enmFirmwareType);
549 if ( enmFirmwareType == FirmwareType_EFI
550 || enmFirmwareType == FirmwareType_EFI32
551 || enmFirmwareType == FirmwareType_EFI64
552 || enmFirmwareType == FirmwareType_EFIDUAL)
553 {
554 unconst(mNvram) = new Nvram(this);
555 AssertReturn(mNvram, E_FAIL);
556 }
557
558#ifdef VBOX_WITH_USB_VIDEO
559 unconst(mEmWebcam) = new EmWebcam(this);
560 AssertReturn(mEmWebcam, E_FAIL);
561#endif
562#ifdef VBOX_WITH_USB_CARDREADER
563 unconst(mUsbCardReader) = new UsbCardReader(this);
564 AssertReturn(mUsbCardReader, E_FAIL);
565#endif
566
567 /* VirtualBox events registration. */
568 {
569 ComPtr<IEventSource> pES;
570 rc = pVirtualBox->COMGETTER(EventSource)(pES.asOutParam());
571 AssertComRC(rc);
572 ComObjPtr<VmEventListenerImpl> aVmListener;
573 aVmListener.createObject();
574 aVmListener->init(new VmEventListener(), this);
575 mVmListener = aVmListener;
576 com::SafeArray<VBoxEventType_T> eventTypes;
577 eventTypes.push_back(VBoxEventType_OnNATRedirect);
578 eventTypes.push_back(VBoxEventType_OnHostPCIDevicePlug);
579 rc = pES->RegisterListener(aVmListener, ComSafeArrayAsInParam(eventTypes), true);
580 AssertComRC(rc);
581 }
582 }
583
584 /* Confirm a successful initialization when it's the case */
585 autoInitSpan.setSucceeded();
586
587#ifdef VBOX_WITH_EXTPACK
588 /* Let the extension packs have a go at things (hold no locks). */
589 if (SUCCEEDED(rc))
590 mptrExtPackManager->callAllConsoleReadyHooks(this);
591#endif
592
593 LogFlowThisFuncLeave();
594
595 return S_OK;
596}
597
598/**
599 * Uninitializes the Console object.
600 */
601void Console::uninit()
602{
603 LogFlowThisFuncEnter();
604
605 /* Enclose the state transition Ready->InUninit->NotReady */
606 AutoUninitSpan autoUninitSpan(this);
607 if (autoUninitSpan.uninitDone())
608 {
609 LogFlowThisFunc(("Already uninitialized.\n"));
610 LogFlowThisFuncLeave();
611 return;
612 }
613
614 LogFlowThisFunc(("initFailed()=%d\n", autoUninitSpan.initFailed()));
615 if (mVmListener)
616 {
617 ComPtr<IEventSource> pES;
618 ComPtr<IVirtualBox> pVirtualBox;
619 HRESULT rc = mMachine->COMGETTER(Parent)(pVirtualBox.asOutParam());
620 AssertComRC(rc);
621 if (SUCCEEDED(rc) && !pVirtualBox.isNull())
622 {
623 rc = pVirtualBox->COMGETTER(EventSource)(pES.asOutParam());
624 AssertComRC(rc);
625 if (!pES.isNull())
626 {
627 rc = pES->UnregisterListener(mVmListener);
628 AssertComRC(rc);
629 }
630 }
631 mVmListener.setNull();
632 }
633
634 /* power down the VM if necessary */
635 if (mpUVM)
636 {
637 powerDown();
638 Assert(mpUVM == NULL);
639 }
640
641 if (mVMZeroCallersSem != NIL_RTSEMEVENT)
642 {
643 RTSemEventDestroy(mVMZeroCallersSem);
644 mVMZeroCallersSem = NIL_RTSEMEVENT;
645 }
646
647 if (mpVmm2UserMethods)
648 {
649 RTMemFree((void *)mpVmm2UserMethods);
650 mpVmm2UserMethods = NULL;
651 }
652
653 if (mNvram)
654 {
655 delete mNvram;
656 unconst(mNvram) = NULL;
657 }
658
659#ifdef VBOX_WITH_USB_VIDEO
660 if (mEmWebcam)
661 {
662 delete mEmWebcam;
663 unconst(mEmWebcam) = NULL;
664 }
665#endif
666
667#ifdef VBOX_WITH_USB_CARDREADER
668 if (mUsbCardReader)
669 {
670 delete mUsbCardReader;
671 unconst(mUsbCardReader) = NULL;
672 }
673#endif
674
675 if (mAudioSniffer)
676 {
677 delete mAudioSniffer;
678 unconst(mAudioSniffer) = NULL;
679 }
680
681 // if the VM had a VMMDev with an HGCM thread, then remove that here
682 if (m_pVMMDev)
683 {
684 delete m_pVMMDev;
685 unconst(m_pVMMDev) = NULL;
686 }
687
688 if (mBusMgr)
689 {
690 mBusMgr->Release();
691 mBusMgr = NULL;
692 }
693
694 m_mapGlobalSharedFolders.clear();
695 m_mapMachineSharedFolders.clear();
696 m_mapSharedFolders.clear(); // console instances
697
698 mRemoteUSBDevices.clear();
699 mUSBDevices.clear();
700
701 if (mVRDEServerInfo)
702 {
703 mVRDEServerInfo->uninit();
704 unconst(mVRDEServerInfo).setNull();
705 }
706
707 if (mDebugger)
708 {
709 mDebugger->uninit();
710 unconst(mDebugger).setNull();
711 }
712
713 if (mDisplay)
714 {
715 mDisplay->uninit();
716 unconst(mDisplay).setNull();
717 }
718
719 if (mMouse)
720 {
721 mMouse->uninit();
722 unconst(mMouse).setNull();
723 }
724
725 if (mKeyboard)
726 {
727 mKeyboard->uninit();
728 unconst(mKeyboard).setNull();
729 }
730
731 if (mGuest)
732 {
733 mGuest->uninit();
734 unconst(mGuest).setNull();
735 }
736
737 if (mConsoleVRDPServer)
738 {
739 delete mConsoleVRDPServer;
740 unconst(mConsoleVRDPServer) = NULL;
741 }
742
743 unconst(mVRDEServer).setNull();
744
745 unconst(mControl).setNull();
746 unconst(mMachine).setNull();
747
748 // we don't perform uninit() as it's possible that some pending event refers to this source
749 unconst(mEventSource).setNull();
750
751#ifdef CONSOLE_WITH_EVENT_CACHE
752 mCallbackData.clear();
753#endif
754
755 LogFlowThisFuncLeave();
756}
757
758#ifdef VBOX_WITH_GUEST_PROPS
759
760/**
761 * Handles guest properties on a VM reset.
762 *
763 * We must delete properties that are flagged TRANSRESET.
764 *
765 * @todo r=bird: Would be more efficient if we added a request to the HGCM
766 * service to do this instead of detouring thru VBoxSVC.
767 * (IMachine::SetGuestProperty ends up in VBoxSVC, which in turns calls
768 * back into the VM process and the HGCM service.)
769 */
770void Console::guestPropertiesHandleVMReset(void)
771{
772 com::SafeArray<BSTR> arrNames;
773 com::SafeArray<BSTR> arrValues;
774 com::SafeArray<LONG64> arrTimestamps;
775 com::SafeArray<BSTR> arrFlags;
776 HRESULT hrc = enumerateGuestProperties(Bstr("*").raw(),
777 ComSafeArrayAsOutParam(arrNames),
778 ComSafeArrayAsOutParam(arrValues),
779 ComSafeArrayAsOutParam(arrTimestamps),
780 ComSafeArrayAsOutParam(arrFlags));
781 if (SUCCEEDED(hrc))
782 {
783 for (size_t i = 0; i < arrFlags.size(); i++)
784 {
785 /* Delete all properties which have the flag "TRANSRESET". */
786 if (Utf8Str(arrFlags[i]).contains("TRANSRESET", Utf8Str::CaseInsensitive))
787 {
788 hrc = mMachine->DeleteGuestProperty(arrNames[i]);
789 if (FAILED(hrc))
790 LogRel(("RESET: Could not delete transient property \"%ls\", rc=%Rhrc\n",
791 arrNames[i], hrc));
792 }
793 }
794 }
795 else
796 LogRel(("RESET: Unable to enumerate guest properties, rc=%Rhrc\n", hrc));
797}
798
799bool Console::guestPropertiesVRDPEnabled(void)
800{
801 Bstr value;
802 HRESULT hrc = mMachine->GetExtraData(Bstr("VBoxInternal2/EnableGuestPropertiesVRDP").raw(),
803 value.asOutParam());
804 if ( hrc == S_OK
805 && value == "1")
806 return true;
807 return false;
808}
809
810void Console::guestPropertiesVRDPUpdateLogon(uint32_t u32ClientId, const char *pszUser, const char *pszDomain)
811{
812 if (!guestPropertiesVRDPEnabled())
813 return;
814
815 LogFlowFunc(("\n"));
816
817 char szPropNm[256];
818 Bstr bstrReadOnlyGuest(L"RDONLYGUEST");
819
820 RTStrPrintf(szPropNm, sizeof(szPropNm), "/VirtualBox/HostInfo/VRDP/Client/%u/Name", u32ClientId);
821 Bstr clientName;
822 mVRDEServerInfo->COMGETTER(ClientName)(clientName.asOutParam());
823
824 mMachine->SetGuestProperty(Bstr(szPropNm).raw(),
825 clientName.raw(),
826 bstrReadOnlyGuest.raw());
827
828 RTStrPrintf(szPropNm, sizeof(szPropNm), "/VirtualBox/HostInfo/VRDP/Client/%u/User", u32ClientId);
829 mMachine->SetGuestProperty(Bstr(szPropNm).raw(),
830 Bstr(pszUser).raw(),
831 bstrReadOnlyGuest.raw());
832
833 RTStrPrintf(szPropNm, sizeof(szPropNm), "/VirtualBox/HostInfo/VRDP/Client/%u/Domain", u32ClientId);
834 mMachine->SetGuestProperty(Bstr(szPropNm).raw(),
835 Bstr(pszDomain).raw(),
836 bstrReadOnlyGuest.raw());
837
838 char szClientId[64];
839 RTStrPrintf(szClientId, sizeof(szClientId), "%u", u32ClientId);
840 mMachine->SetGuestProperty(Bstr("/VirtualBox/HostInfo/VRDP/LastConnectedClient").raw(),
841 Bstr(szClientId).raw(),
842 bstrReadOnlyGuest.raw());
843
844 return;
845}
846
847void Console::guestPropertiesVRDPUpdateActiveClient(uint32_t u32ClientId)
848{
849 if (!guestPropertiesVRDPEnabled())
850 return;
851
852 LogFlowFunc(("%d\n", u32ClientId));
853
854 Bstr bstrFlags(L"RDONLYGUEST,TRANSIENT");
855
856 char szClientId[64];
857 RTStrPrintf(szClientId, sizeof(szClientId), "%u", u32ClientId);
858
859 mMachine->SetGuestProperty(Bstr("/VirtualBox/HostInfo/VRDP/ActiveClient").raw(),
860 Bstr(szClientId).raw(),
861 bstrFlags.raw());
862
863 return;
864}
865
866void Console::guestPropertiesVRDPUpdateNameChange(uint32_t u32ClientId, const char *pszName)
867{
868 if (!guestPropertiesVRDPEnabled())
869 return;
870
871 LogFlowFunc(("\n"));
872
873 char szPropNm[256];
874 Bstr bstrReadOnlyGuest(L"RDONLYGUEST");
875
876 RTStrPrintf(szPropNm, sizeof(szPropNm), "/VirtualBox/HostInfo/VRDP/Client/%u/Name", u32ClientId);
877 Bstr clientName(pszName);
878
879 mMachine->SetGuestProperty(Bstr(szPropNm).raw(),
880 clientName.raw(),
881 bstrReadOnlyGuest.raw());
882
883}
884
885void Console::guestPropertiesVRDPUpdateIPAddrChange(uint32_t u32ClientId, const char *pszIPAddr)
886{
887 if (!guestPropertiesVRDPEnabled())
888 return;
889
890 LogFlowFunc(("\n"));
891
892 char szPropNm[256];
893 Bstr bstrReadOnlyGuest(L"RDONLYGUEST");
894
895 RTStrPrintf(szPropNm, sizeof(szPropNm), "/VirtualBox/HostInfo/VRDP/Client/%u/IPAddr", u32ClientId);
896 Bstr clientIPAddr(pszIPAddr);
897
898 mMachine->SetGuestProperty(Bstr(szPropNm).raw(),
899 clientIPAddr.raw(),
900 bstrReadOnlyGuest.raw());
901
902}
903
904void Console::guestPropertiesVRDPUpdateLocationChange(uint32_t u32ClientId, const char *pszLocation)
905{
906 if (!guestPropertiesVRDPEnabled())
907 return;
908
909 LogFlowFunc(("\n"));
910
911 char szPropNm[256];
912 Bstr bstrReadOnlyGuest(L"RDONLYGUEST");
913
914 RTStrPrintf(szPropNm, sizeof(szPropNm), "/VirtualBox/HostInfo/VRDP/Client/%u/Location", u32ClientId);
915 Bstr clientLocation(pszLocation);
916
917 mMachine->SetGuestProperty(Bstr(szPropNm).raw(),
918 clientLocation.raw(),
919 bstrReadOnlyGuest.raw());
920
921}
922
923void Console::guestPropertiesVRDPUpdateOtherInfoChange(uint32_t u32ClientId, const char *pszOtherInfo)
924{
925 if (!guestPropertiesVRDPEnabled())
926 return;
927
928 LogFlowFunc(("\n"));
929
930 char szPropNm[256];
931 Bstr bstrReadOnlyGuest(L"RDONLYGUEST");
932
933 RTStrPrintf(szPropNm, sizeof(szPropNm), "/VirtualBox/HostInfo/VRDP/Client/%u/OtherInfo", u32ClientId);
934 Bstr clientOtherInfo(pszOtherInfo);
935
936 mMachine->SetGuestProperty(Bstr(szPropNm).raw(),
937 clientOtherInfo.raw(),
938 bstrReadOnlyGuest.raw());
939
940}
941
942void Console::guestPropertiesVRDPUpdateClientAttach(uint32_t u32ClientId, bool fAttached)
943{
944 if (!guestPropertiesVRDPEnabled())
945 return;
946
947 LogFlowFunc(("\n"));
948
949 Bstr bstrReadOnlyGuest(L"RDONLYGUEST");
950
951 char szPropNm[256];
952 RTStrPrintf(szPropNm, sizeof(szPropNm), "/VirtualBox/HostInfo/VRDP/Client/%u/Attach", u32ClientId);
953
954 Bstr bstrValue = fAttached? "1": "0";
955
956 mMachine->SetGuestProperty(Bstr(szPropNm).raw(),
957 bstrValue.raw(),
958 bstrReadOnlyGuest.raw());
959}
960
961void Console::guestPropertiesVRDPUpdateDisconnect(uint32_t u32ClientId)
962{
963 if (!guestPropertiesVRDPEnabled())
964 return;
965
966 LogFlowFunc(("\n"));
967
968 Bstr bstrReadOnlyGuest(L"RDONLYGUEST");
969
970 char szPropNm[256];
971 RTStrPrintf(szPropNm, sizeof(szPropNm), "/VirtualBox/HostInfo/VRDP/Client/%u/Name", u32ClientId);
972 mMachine->SetGuestProperty(Bstr(szPropNm).raw(), NULL,
973 bstrReadOnlyGuest.raw());
974
975 RTStrPrintf(szPropNm, sizeof(szPropNm), "/VirtualBox/HostInfo/VRDP/Client/%u/User", u32ClientId);
976 mMachine->SetGuestProperty(Bstr(szPropNm).raw(), NULL,
977 bstrReadOnlyGuest.raw());
978
979 RTStrPrintf(szPropNm, sizeof(szPropNm), "/VirtualBox/HostInfo/VRDP/Client/%u/Domain", u32ClientId);
980 mMachine->SetGuestProperty(Bstr(szPropNm).raw(), NULL,
981 bstrReadOnlyGuest.raw());
982
983 RTStrPrintf(szPropNm, sizeof(szPropNm), "/VirtualBox/HostInfo/VRDP/Client/%u/Attach", u32ClientId);
984 mMachine->SetGuestProperty(Bstr(szPropNm).raw(), NULL,
985 bstrReadOnlyGuest.raw());
986
987 char szClientId[64];
988 RTStrPrintf(szClientId, sizeof(szClientId), "%d", u32ClientId);
989 mMachine->SetGuestProperty(Bstr("/VirtualBox/HostInfo/VRDP/LastDisconnectedClient").raw(),
990 Bstr(szClientId).raw(),
991 bstrReadOnlyGuest.raw());
992
993 return;
994}
995
996#endif /* VBOX_WITH_GUEST_PROPS */
997
998bool Console::isResetTurnedIntoPowerOff(void)
999{
1000 Bstr value;
1001 HRESULT hrc = mMachine->GetExtraData(Bstr("VBoxInternal2/TurnResetIntoPowerOff").raw(),
1002 value.asOutParam());
1003 if ( hrc == S_OK
1004 && value == "1")
1005 return true;
1006 return false;
1007}
1008
1009#ifdef VBOX_WITH_EXTPACK
1010/**
1011 * Used by VRDEServer and others to talke to the extension pack manager.
1012 *
1013 * @returns The extension pack manager.
1014 */
1015ExtPackManager *Console::getExtPackManager()
1016{
1017 return mptrExtPackManager;
1018}
1019#endif
1020
1021
1022int Console::VRDPClientLogon(uint32_t u32ClientId, const char *pszUser, const char *pszPassword, const char *pszDomain)
1023{
1024 LogFlowFuncEnter();
1025 LogFlowFunc(("%d, %s, %s, %s\n", u32ClientId, pszUser, pszPassword, pszDomain));
1026
1027 AutoCaller autoCaller(this);
1028 if (!autoCaller.isOk())
1029 {
1030 /* Console has been already uninitialized, deny request */
1031 LogRel(("AUTH: Access denied (Console uninitialized).\n"));
1032 LogFlowFuncLeave();
1033 return VERR_ACCESS_DENIED;
1034 }
1035
1036 Bstr id;
1037 HRESULT hrc = mMachine->COMGETTER(Id)(id.asOutParam());
1038 Guid uuid = Guid(id);
1039
1040 AssertComRCReturn(hrc, VERR_ACCESS_DENIED);
1041
1042 AuthType_T authType = AuthType_Null;
1043 hrc = mVRDEServer->COMGETTER(AuthType)(&authType);
1044 AssertComRCReturn(hrc, VERR_ACCESS_DENIED);
1045
1046 ULONG authTimeout = 0;
1047 hrc = mVRDEServer->COMGETTER(AuthTimeout)(&authTimeout);
1048 AssertComRCReturn(hrc, VERR_ACCESS_DENIED);
1049
1050 AuthResult result = AuthResultAccessDenied;
1051 AuthGuestJudgement guestJudgement = AuthGuestNotAsked;
1052
1053 LogFlowFunc(("Auth type %d\n", authType));
1054
1055 LogRel(("AUTH: User: [%s]. Domain: [%s]. Authentication type: [%s]\n",
1056 pszUser, pszDomain,
1057 authType == AuthType_Null?
1058 "Null":
1059 (authType == AuthType_External?
1060 "External":
1061 (authType == AuthType_Guest?
1062 "Guest":
1063 "INVALID"
1064 )
1065 )
1066 ));
1067
1068 switch (authType)
1069 {
1070 case AuthType_Null:
1071 {
1072 result = AuthResultAccessGranted;
1073 break;
1074 }
1075
1076 case AuthType_External:
1077 {
1078 /* Call the external library. */
1079 result = mConsoleVRDPServer->Authenticate(uuid, guestJudgement, pszUser, pszPassword, pszDomain, u32ClientId);
1080
1081 if (result != AuthResultDelegateToGuest)
1082 {
1083 break;
1084 }
1085
1086 LogRel(("AUTH: Delegated to guest.\n"));
1087
1088 LogFlowFunc(("External auth asked for guest judgement\n"));
1089 } /* pass through */
1090
1091 case AuthType_Guest:
1092 {
1093 guestJudgement = AuthGuestNotReacted;
1094
1095 // @todo r=dj locking required here for m_pVMMDev?
1096 PPDMIVMMDEVPORT pDevPort;
1097 if ( (m_pVMMDev)
1098 && ((pDevPort = m_pVMMDev->getVMMDevPort()))
1099 )
1100 {
1101 /* Issue the request to guest. Assume that the call does not require EMT. It should not. */
1102
1103 /* Ask the guest to judge these credentials. */
1104 uint32_t u32GuestFlags = VMMDEV_SETCREDENTIALS_JUDGE;
1105
1106 int rc = pDevPort->pfnSetCredentials(pDevPort, pszUser, pszPassword, pszDomain, u32GuestFlags);
1107
1108 if (RT_SUCCESS(rc))
1109 {
1110 /* Wait for guest. */
1111 rc = m_pVMMDev->WaitCredentialsJudgement(authTimeout, &u32GuestFlags);
1112
1113 if (RT_SUCCESS(rc))
1114 {
1115 switch (u32GuestFlags & (VMMDEV_CREDENTIALS_JUDGE_OK | VMMDEV_CREDENTIALS_JUDGE_DENY | VMMDEV_CREDENTIALS_JUDGE_NOJUDGEMENT))
1116 {
1117 case VMMDEV_CREDENTIALS_JUDGE_DENY: guestJudgement = AuthGuestAccessDenied; break;
1118 case VMMDEV_CREDENTIALS_JUDGE_NOJUDGEMENT: guestJudgement = AuthGuestNoJudgement; break;
1119 case VMMDEV_CREDENTIALS_JUDGE_OK: guestJudgement = AuthGuestAccessGranted; break;
1120 default:
1121 LogFlowFunc(("Invalid guest flags %08X!!!\n", u32GuestFlags)); break;
1122 }
1123 }
1124 else
1125 {
1126 LogFlowFunc(("Wait for credentials judgement rc = %Rrc!!!\n", rc));
1127 }
1128
1129 LogFlowFunc(("Guest judgement %d\n", guestJudgement));
1130 }
1131 else
1132 {
1133 LogFlowFunc(("Could not set credentials rc = %Rrc!!!\n", rc));
1134 }
1135 }
1136
1137 if (authType == AuthType_External)
1138 {
1139 LogRel(("AUTH: Guest judgement %d.\n", guestJudgement));
1140 LogFlowFunc(("External auth called again with guest judgement = %d\n", guestJudgement));
1141 result = mConsoleVRDPServer->Authenticate(uuid, guestJudgement, pszUser, pszPassword, pszDomain, u32ClientId);
1142 }
1143 else
1144 {
1145 switch (guestJudgement)
1146 {
1147 case AuthGuestAccessGranted:
1148 result = AuthResultAccessGranted;
1149 break;
1150 default:
1151 result = AuthResultAccessDenied;
1152 break;
1153 }
1154 }
1155 } break;
1156
1157 default:
1158 AssertFailed();
1159 }
1160
1161 LogFlowFunc(("Result = %d\n", result));
1162 LogFlowFuncLeave();
1163
1164 if (result != AuthResultAccessGranted)
1165 {
1166 /* Reject. */
1167 LogRel(("AUTH: Access denied.\n"));
1168 return VERR_ACCESS_DENIED;
1169 }
1170
1171 LogRel(("AUTH: Access granted.\n"));
1172
1173 /* Multiconnection check must be made after authentication, so bad clients would not interfere with a good one. */
1174 BOOL allowMultiConnection = FALSE;
1175 hrc = mVRDEServer->COMGETTER(AllowMultiConnection)(&allowMultiConnection);
1176 AssertComRCReturn(hrc, VERR_ACCESS_DENIED);
1177
1178 BOOL reuseSingleConnection = FALSE;
1179 hrc = mVRDEServer->COMGETTER(ReuseSingleConnection)(&reuseSingleConnection);
1180 AssertComRCReturn(hrc, VERR_ACCESS_DENIED);
1181
1182 LogFlowFunc(("allowMultiConnection %d, reuseSingleConnection = %d, mcVRDPClients = %d, mu32SingleRDPClientId = %d\n", allowMultiConnection, reuseSingleConnection, mcVRDPClients, mu32SingleRDPClientId));
1183
1184 if (allowMultiConnection == FALSE)
1185 {
1186 /* Note: the 'mcVRDPClients' variable is incremented in ClientConnect callback, which is called when the client
1187 * is successfully connected, that is after the ClientLogon callback. Therefore the mcVRDPClients
1188 * value is 0 for first client.
1189 */
1190 if (mcVRDPClients != 0)
1191 {
1192 Assert(mcVRDPClients == 1);
1193 /* There is a client already.
1194 * If required drop the existing client connection and let the connecting one in.
1195 */
1196 if (reuseSingleConnection)
1197 {
1198 LogRel(("AUTH: Multiple connections are not enabled. Disconnecting existing client.\n"));
1199 mConsoleVRDPServer->DisconnectClient(mu32SingleRDPClientId, false);
1200 }
1201 else
1202 {
1203 /* Reject. */
1204 LogRel(("AUTH: Multiple connections are not enabled. Access denied.\n"));
1205 return VERR_ACCESS_DENIED;
1206 }
1207 }
1208
1209 /* Save the connected client id. From now on it will be necessary to disconnect this one. */
1210 mu32SingleRDPClientId = u32ClientId;
1211 }
1212
1213#ifdef VBOX_WITH_GUEST_PROPS
1214 guestPropertiesVRDPUpdateLogon(u32ClientId, pszUser, pszDomain);
1215#endif /* VBOX_WITH_GUEST_PROPS */
1216
1217 /* Check if the successfully verified credentials are to be sent to the guest. */
1218 BOOL fProvideGuestCredentials = FALSE;
1219
1220 Bstr value;
1221 hrc = mMachine->GetExtraData(Bstr("VRDP/ProvideGuestCredentials").raw(),
1222 value.asOutParam());
1223 if (SUCCEEDED(hrc) && value == "1")
1224 {
1225 /* Provide credentials only if there are no logged in users. */
1226 Bstr noLoggedInUsersValue;
1227 LONG64 ul64Timestamp = 0;
1228 Bstr flags;
1229
1230 hrc = getGuestProperty(Bstr("/VirtualBox/GuestInfo/OS/NoLoggedInUsers").raw(),
1231 noLoggedInUsersValue.asOutParam(), &ul64Timestamp, flags.asOutParam());
1232
1233 if (SUCCEEDED(hrc) && noLoggedInUsersValue != Bstr("false"))
1234 {
1235 /* And only if there are no connected clients. */
1236 if (ASMAtomicCmpXchgBool(&mcGuestCredentialsProvided, true, false))
1237 {
1238 fProvideGuestCredentials = TRUE;
1239 }
1240 }
1241 }
1242
1243 // @todo r=dj locking required here for m_pVMMDev?
1244 if ( fProvideGuestCredentials
1245 && m_pVMMDev)
1246 {
1247 uint32_t u32GuestFlags = VMMDEV_SETCREDENTIALS_GUESTLOGON;
1248
1249 PPDMIVMMDEVPORT pDevPort = m_pVMMDev->getVMMDevPort();
1250 if (pDevPort)
1251 {
1252 int rc = pDevPort->pfnSetCredentials(m_pVMMDev->getVMMDevPort(),
1253 pszUser, pszPassword, pszDomain, u32GuestFlags);
1254 AssertRC(rc);
1255 }
1256 }
1257
1258 return VINF_SUCCESS;
1259}
1260
1261void Console::VRDPClientStatusChange(uint32_t u32ClientId, const char *pszStatus)
1262{
1263 LogFlowFuncEnter();
1264
1265 AutoCaller autoCaller(this);
1266 AssertComRCReturnVoid(autoCaller.rc());
1267
1268 LogFlowFunc(("%s\n", pszStatus));
1269
1270#ifdef VBOX_WITH_GUEST_PROPS
1271 /* Parse the status string. */
1272 if (RTStrICmp(pszStatus, "ATTACH") == 0)
1273 {
1274 guestPropertiesVRDPUpdateClientAttach(u32ClientId, true);
1275 }
1276 else if (RTStrICmp(pszStatus, "DETACH") == 0)
1277 {
1278 guestPropertiesVRDPUpdateClientAttach(u32ClientId, false);
1279 }
1280 else if (RTStrNICmp(pszStatus, "NAME=", strlen("NAME=")) == 0)
1281 {
1282 guestPropertiesVRDPUpdateNameChange(u32ClientId, pszStatus + strlen("NAME="));
1283 }
1284 else if (RTStrNICmp(pszStatus, "CIPA=", strlen("CIPA=")) == 0)
1285 {
1286 guestPropertiesVRDPUpdateIPAddrChange(u32ClientId, pszStatus + strlen("CIPA="));
1287 }
1288 else if (RTStrNICmp(pszStatus, "CLOCATION=", strlen("CLOCATION=")) == 0)
1289 {
1290 guestPropertiesVRDPUpdateLocationChange(u32ClientId, pszStatus + strlen("CLOCATION="));
1291 }
1292 else if (RTStrNICmp(pszStatus, "COINFO=", strlen("COINFO=")) == 0)
1293 {
1294 guestPropertiesVRDPUpdateOtherInfoChange(u32ClientId, pszStatus + strlen("COINFO="));
1295 }
1296#endif
1297
1298 LogFlowFuncLeave();
1299}
1300
1301void Console::VRDPClientConnect(uint32_t u32ClientId)
1302{
1303 LogFlowFuncEnter();
1304
1305 AutoCaller autoCaller(this);
1306 AssertComRCReturnVoid(autoCaller.rc());
1307
1308 uint32_t u32Clients = ASMAtomicIncU32(&mcVRDPClients);
1309 VMMDev *pDev;
1310 PPDMIVMMDEVPORT pPort;
1311 if ( (u32Clients == 1)
1312 && ((pDev = getVMMDev()))
1313 && ((pPort = pDev->getVMMDevPort()))
1314 )
1315 {
1316 pPort->pfnVRDPChange(pPort,
1317 true,
1318 VRDP_EXPERIENCE_LEVEL_FULL); // @todo configurable
1319 }
1320
1321 NOREF(u32ClientId);
1322 mDisplay->VideoAccelVRDP(true);
1323
1324#ifdef VBOX_WITH_GUEST_PROPS
1325 guestPropertiesVRDPUpdateActiveClient(u32ClientId);
1326#endif /* VBOX_WITH_GUEST_PROPS */
1327
1328 LogFlowFuncLeave();
1329 return;
1330}
1331
1332void Console::VRDPClientDisconnect(uint32_t u32ClientId,
1333 uint32_t fu32Intercepted)
1334{
1335 LogFlowFuncEnter();
1336
1337 AutoCaller autoCaller(this);
1338 AssertComRCReturnVoid(autoCaller.rc());
1339
1340 AssertReturnVoid(mConsoleVRDPServer);
1341
1342 uint32_t u32Clients = ASMAtomicDecU32(&mcVRDPClients);
1343 VMMDev *pDev;
1344 PPDMIVMMDEVPORT pPort;
1345
1346 if ( (u32Clients == 0)
1347 && ((pDev = getVMMDev()))
1348 && ((pPort = pDev->getVMMDevPort()))
1349 )
1350 {
1351 pPort->pfnVRDPChange(pPort,
1352 false,
1353 0);
1354 }
1355
1356 mDisplay->VideoAccelVRDP(false);
1357
1358 if (fu32Intercepted & VRDE_CLIENT_INTERCEPT_USB)
1359 {
1360 mConsoleVRDPServer->USBBackendDelete(u32ClientId);
1361 }
1362
1363 if (fu32Intercepted & VRDE_CLIENT_INTERCEPT_CLIPBOARD)
1364 {
1365 mConsoleVRDPServer->ClipboardDelete(u32ClientId);
1366 }
1367
1368 if (fu32Intercepted & VRDE_CLIENT_INTERCEPT_AUDIO)
1369 {
1370 mcAudioRefs--;
1371
1372 if (mcAudioRefs <= 0)
1373 {
1374 if (mAudioSniffer)
1375 {
1376 PPDMIAUDIOSNIFFERPORT port = mAudioSniffer->getAudioSnifferPort();
1377 if (port)
1378 {
1379 port->pfnSetup(port, false, false);
1380 }
1381 }
1382 }
1383 }
1384
1385 Bstr uuid;
1386 HRESULT hrc = mMachine->COMGETTER(Id)(uuid.asOutParam());
1387 AssertComRC(hrc);
1388
1389 AuthType_T authType = AuthType_Null;
1390 hrc = mVRDEServer->COMGETTER(AuthType)(&authType);
1391 AssertComRC(hrc);
1392
1393 if (authType == AuthType_External)
1394 mConsoleVRDPServer->AuthDisconnect(uuid, u32ClientId);
1395
1396#ifdef VBOX_WITH_GUEST_PROPS
1397 guestPropertiesVRDPUpdateDisconnect(u32ClientId);
1398 if (u32Clients == 0)
1399 guestPropertiesVRDPUpdateActiveClient(0);
1400#endif /* VBOX_WITH_GUEST_PROPS */
1401
1402 if (u32Clients == 0)
1403 mcGuestCredentialsProvided = false;
1404
1405 LogFlowFuncLeave();
1406 return;
1407}
1408
1409void Console::VRDPInterceptAudio(uint32_t u32ClientId)
1410{
1411 LogFlowFuncEnter();
1412
1413 AutoCaller autoCaller(this);
1414 AssertComRCReturnVoid(autoCaller.rc());
1415
1416 LogFlowFunc(("mAudioSniffer %p, u32ClientId %d.\n",
1417 mAudioSniffer, u32ClientId));
1418 NOREF(u32ClientId);
1419
1420 ++mcAudioRefs;
1421
1422 if (mcAudioRefs == 1)
1423 {
1424 if (mAudioSniffer)
1425 {
1426 PPDMIAUDIOSNIFFERPORT port = mAudioSniffer->getAudioSnifferPort();
1427 if (port)
1428 {
1429 port->pfnSetup(port, true, true);
1430 }
1431 }
1432 }
1433
1434 LogFlowFuncLeave();
1435 return;
1436}
1437
1438void Console::VRDPInterceptUSB(uint32_t u32ClientId, void **ppvIntercept)
1439{
1440 LogFlowFuncEnter();
1441
1442 AutoCaller autoCaller(this);
1443 AssertComRCReturnVoid(autoCaller.rc());
1444
1445 AssertReturnVoid(mConsoleVRDPServer);
1446
1447 mConsoleVRDPServer->USBBackendCreate(u32ClientId, ppvIntercept);
1448
1449 LogFlowFuncLeave();
1450 return;
1451}
1452
1453void Console::VRDPInterceptClipboard(uint32_t u32ClientId)
1454{
1455 LogFlowFuncEnter();
1456
1457 AutoCaller autoCaller(this);
1458 AssertComRCReturnVoid(autoCaller.rc());
1459
1460 AssertReturnVoid(mConsoleVRDPServer);
1461
1462 mConsoleVRDPServer->ClipboardCreate(u32ClientId);
1463
1464 LogFlowFuncLeave();
1465 return;
1466}
1467
1468
1469//static
1470const char *Console::sSSMConsoleUnit = "ConsoleData";
1471//static
1472uint32_t Console::sSSMConsoleVer = 0x00010001;
1473
1474inline static const char *networkAdapterTypeToName(NetworkAdapterType_T adapterType)
1475{
1476 switch (adapterType)
1477 {
1478 case NetworkAdapterType_Am79C970A:
1479 case NetworkAdapterType_Am79C973:
1480 return "pcnet";
1481#ifdef VBOX_WITH_E1000
1482 case NetworkAdapterType_I82540EM:
1483 case NetworkAdapterType_I82543GC:
1484 case NetworkAdapterType_I82545EM:
1485 return "e1000";
1486#endif
1487#ifdef VBOX_WITH_VIRTIO
1488 case NetworkAdapterType_Virtio:
1489 return "virtio-net";
1490#endif
1491 default:
1492 AssertFailed();
1493 return "unknown";
1494 }
1495 return NULL;
1496}
1497
1498/**
1499 * Loads various console data stored in the saved state file.
1500 * This method does validation of the state file and returns an error info
1501 * when appropriate.
1502 *
1503 * The method does nothing if the machine is not in the Saved file or if
1504 * console data from it has already been loaded.
1505 *
1506 * @note The caller must lock this object for writing.
1507 */
1508HRESULT Console::loadDataFromSavedState()
1509{
1510 if (mMachineState != MachineState_Saved || mSavedStateDataLoaded)
1511 return S_OK;
1512
1513 Bstr savedStateFile;
1514 HRESULT rc = mMachine->COMGETTER(StateFilePath)(savedStateFile.asOutParam());
1515 if (FAILED(rc))
1516 return rc;
1517
1518 PSSMHANDLE ssm;
1519 int vrc = SSMR3Open(Utf8Str(savedStateFile).c_str(), 0, &ssm);
1520 if (RT_SUCCESS(vrc))
1521 {
1522 uint32_t version = 0;
1523 vrc = SSMR3Seek(ssm, sSSMConsoleUnit, 0 /* iInstance */, &version);
1524 if (SSM_VERSION_MAJOR(version) == SSM_VERSION_MAJOR(sSSMConsoleVer))
1525 {
1526 if (RT_SUCCESS(vrc))
1527 vrc = loadStateFileExecInternal(ssm, version);
1528 else if (vrc == VERR_SSM_UNIT_NOT_FOUND)
1529 vrc = VINF_SUCCESS;
1530 }
1531 else
1532 vrc = VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
1533
1534 SSMR3Close(ssm);
1535 }
1536
1537 if (RT_FAILURE(vrc))
1538 rc = setError(VBOX_E_FILE_ERROR,
1539 tr("The saved state file '%ls' is invalid (%Rrc). Delete the saved state and try again"),
1540 savedStateFile.raw(), vrc);
1541
1542 mSavedStateDataLoaded = true;
1543
1544 return rc;
1545}
1546
1547/**
1548 * Callback handler to save various console data to the state file,
1549 * called when the user saves the VM state.
1550 *
1551 * @param pvUser pointer to Console
1552 *
1553 * @note Locks the Console object for reading.
1554 */
1555//static
1556DECLCALLBACK(void)
1557Console::saveStateFileExec(PSSMHANDLE pSSM, void *pvUser)
1558{
1559 LogFlowFunc(("\n"));
1560
1561 Console *that = static_cast<Console *>(pvUser);
1562 AssertReturnVoid(that);
1563
1564 AutoCaller autoCaller(that);
1565 AssertComRCReturnVoid(autoCaller.rc());
1566
1567 AutoReadLock alock(that COMMA_LOCKVAL_SRC_POS);
1568
1569 int vrc = SSMR3PutU32(pSSM, (uint32_t)that->m_mapSharedFolders.size());
1570 AssertRC(vrc);
1571
1572 for (SharedFolderMap::const_iterator it = that->m_mapSharedFolders.begin();
1573 it != that->m_mapSharedFolders.end();
1574 ++it)
1575 {
1576 SharedFolder *pSF = (*it).second;
1577 AutoCaller sfCaller(pSF);
1578 AutoReadLock sfLock(pSF COMMA_LOCKVAL_SRC_POS);
1579
1580 Utf8Str name = pSF->getName();
1581 vrc = SSMR3PutU32(pSSM, (uint32_t)name.length() + 1 /* term. 0 */);
1582 AssertRC(vrc);
1583 vrc = SSMR3PutStrZ(pSSM, name.c_str());
1584 AssertRC(vrc);
1585
1586 Utf8Str hostPath = pSF->getHostPath();
1587 vrc = SSMR3PutU32(pSSM, (uint32_t)hostPath.length() + 1 /* term. 0 */);
1588 AssertRC(vrc);
1589 vrc = SSMR3PutStrZ(pSSM, hostPath.c_str());
1590 AssertRC(vrc);
1591
1592 vrc = SSMR3PutBool(pSSM, !!pSF->isWritable());
1593 AssertRC(vrc);
1594
1595 vrc = SSMR3PutBool(pSSM, !!pSF->isAutoMounted());
1596 AssertRC(vrc);
1597 }
1598
1599 return;
1600}
1601
1602/**
1603 * Callback handler to load various console data from the state file.
1604 * Called when the VM is being restored from the saved state.
1605 *
1606 * @param pvUser pointer to Console
1607 * @param uVersion Console unit version.
1608 * Should match sSSMConsoleVer.
1609 * @param uPass The data pass.
1610 *
1611 * @note Should locks the Console object for writing, if necessary.
1612 */
1613//static
1614DECLCALLBACK(int)
1615Console::loadStateFileExec(PSSMHANDLE pSSM, void *pvUser, uint32_t uVersion, uint32_t uPass)
1616{
1617 LogFlowFunc(("\n"));
1618
1619 if (SSM_VERSION_MAJOR_CHANGED(uVersion, sSSMConsoleVer))
1620 return VERR_VERSION_MISMATCH;
1621 Assert(uPass == SSM_PASS_FINAL); NOREF(uPass);
1622
1623 Console *that = static_cast<Console *>(pvUser);
1624 AssertReturn(that, VERR_INVALID_PARAMETER);
1625
1626 /* Currently, nothing to do when we've been called from VMR3Load*. */
1627 return SSMR3SkipToEndOfUnit(pSSM);
1628}
1629
1630/**
1631 * Method to load various console data from the state file.
1632 * Called from #loadDataFromSavedState.
1633 *
1634 * @param pvUser pointer to Console
1635 * @param u32Version Console unit version.
1636 * Should match sSSMConsoleVer.
1637 *
1638 * @note Locks the Console object for writing.
1639 */
1640int
1641Console::loadStateFileExecInternal(PSSMHANDLE pSSM, uint32_t u32Version)
1642{
1643 AutoCaller autoCaller(this);
1644 AssertComRCReturn(autoCaller.rc(), VERR_ACCESS_DENIED);
1645
1646 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1647
1648 AssertReturn(m_mapSharedFolders.size() == 0, VERR_INTERNAL_ERROR);
1649
1650 uint32_t size = 0;
1651 int vrc = SSMR3GetU32(pSSM, &size);
1652 AssertRCReturn(vrc, vrc);
1653
1654 for (uint32_t i = 0; i < size; ++i)
1655 {
1656 Utf8Str strName;
1657 Utf8Str strHostPath;
1658 bool writable = true;
1659 bool autoMount = false;
1660
1661 uint32_t szBuf = 0;
1662 char *buf = NULL;
1663
1664 vrc = SSMR3GetU32(pSSM, &szBuf);
1665 AssertRCReturn(vrc, vrc);
1666 buf = new char[szBuf];
1667 vrc = SSMR3GetStrZ(pSSM, buf, szBuf);
1668 AssertRC(vrc);
1669 strName = buf;
1670 delete[] buf;
1671
1672 vrc = SSMR3GetU32(pSSM, &szBuf);
1673 AssertRCReturn(vrc, vrc);
1674 buf = new char[szBuf];
1675 vrc = SSMR3GetStrZ(pSSM, buf, szBuf);
1676 AssertRC(vrc);
1677 strHostPath = buf;
1678 delete[] buf;
1679
1680 if (u32Version > 0x00010000)
1681 SSMR3GetBool(pSSM, &writable);
1682
1683 if (u32Version > 0x00010000) // ???
1684 SSMR3GetBool(pSSM, &autoMount);
1685
1686 ComObjPtr<SharedFolder> pSharedFolder;
1687 pSharedFolder.createObject();
1688 HRESULT rc = pSharedFolder->init(this,
1689 strName,
1690 strHostPath,
1691 writable,
1692 autoMount,
1693 false /* fFailOnError */);
1694 AssertComRCReturn(rc, VERR_INTERNAL_ERROR);
1695
1696 m_mapSharedFolders.insert(std::make_pair(strName, pSharedFolder));
1697 }
1698
1699 return VINF_SUCCESS;
1700}
1701
1702#ifdef VBOX_WITH_GUEST_PROPS
1703
1704// static
1705DECLCALLBACK(int) Console::doGuestPropNotification(void *pvExtension,
1706 uint32_t u32Function,
1707 void *pvParms,
1708 uint32_t cbParms)
1709{
1710 using namespace guestProp;
1711
1712 Assert(u32Function == 0); NOREF(u32Function);
1713
1714 /*
1715 * No locking, as this is purely a notification which does not make any
1716 * changes to the object state.
1717 */
1718 PHOSTCALLBACKDATA pCBData = reinterpret_cast<PHOSTCALLBACKDATA>(pvParms);
1719 AssertReturn(sizeof(HOSTCALLBACKDATA) == cbParms, VERR_INVALID_PARAMETER);
1720 AssertReturn(HOSTCALLBACKMAGIC == pCBData->u32Magic, VERR_INVALID_PARAMETER);
1721 LogFlow(("Console::doGuestPropNotification: pCBData={.pcszName=%s, .pcszValue=%s, .pcszFlags=%s}\n",
1722 pCBData->pcszName, pCBData->pcszValue, pCBData->pcszFlags));
1723
1724 int rc;
1725 Bstr name(pCBData->pcszName);
1726 Bstr value(pCBData->pcszValue);
1727 Bstr flags(pCBData->pcszFlags);
1728 ComObjPtr<Console> pConsole = reinterpret_cast<Console *>(pvExtension);
1729 HRESULT hrc = pConsole->mControl->PushGuestProperty(name.raw(),
1730 value.raw(),
1731 pCBData->u64Timestamp,
1732 flags.raw());
1733 if (SUCCEEDED(hrc))
1734 rc = VINF_SUCCESS;
1735 else
1736 {
1737 LogFlow(("Console::doGuestPropNotification: hrc=%Rhrc pCBData={.pcszName=%s, .pcszValue=%s, .pcszFlags=%s}\n",
1738 hrc, pCBData->pcszName, pCBData->pcszValue, pCBData->pcszFlags));
1739 rc = Global::vboxStatusCodeFromCOM(hrc);
1740 }
1741 return rc;
1742}
1743
1744HRESULT Console::doEnumerateGuestProperties(CBSTR aPatterns,
1745 ComSafeArrayOut(BSTR, aNames),
1746 ComSafeArrayOut(BSTR, aValues),
1747 ComSafeArrayOut(LONG64, aTimestamps),
1748 ComSafeArrayOut(BSTR, aFlags))
1749{
1750 AssertReturn(m_pVMMDev, E_FAIL);
1751
1752 using namespace guestProp;
1753
1754 VBOXHGCMSVCPARM parm[3];
1755
1756 Utf8Str utf8Patterns(aPatterns);
1757 parm[0].type = VBOX_HGCM_SVC_PARM_PTR;
1758 parm[0].u.pointer.addr = (void*)utf8Patterns.c_str();
1759 parm[0].u.pointer.size = (uint32_t)utf8Patterns.length() + 1;
1760
1761 /*
1762 * Now things get slightly complicated. Due to a race with the guest adding
1763 * properties, there is no good way to know how much to enlarge a buffer for
1764 * the service to enumerate into. We choose a decent starting size and loop a
1765 * few times, each time retrying with the size suggested by the service plus
1766 * one Kb.
1767 */
1768 size_t cchBuf = 4096;
1769 Utf8Str Utf8Buf;
1770 int vrc = VERR_BUFFER_OVERFLOW;
1771 for (unsigned i = 0; i < 10 && (VERR_BUFFER_OVERFLOW == vrc); ++i)
1772 {
1773 try
1774 {
1775 Utf8Buf.reserve(cchBuf + 1024);
1776 }
1777 catch(...)
1778 {
1779 return E_OUTOFMEMORY;
1780 }
1781 parm[1].type = VBOX_HGCM_SVC_PARM_PTR;
1782 parm[1].u.pointer.addr = Utf8Buf.mutableRaw();
1783 parm[1].u.pointer.size = (uint32_t)cchBuf + 1024;
1784 vrc = m_pVMMDev->hgcmHostCall("VBoxGuestPropSvc", ENUM_PROPS_HOST, 3,
1785 &parm[0]);
1786 Utf8Buf.jolt();
1787 if (parm[2].type != VBOX_HGCM_SVC_PARM_32BIT)
1788 return setError(E_FAIL, tr("Internal application error"));
1789 cchBuf = parm[2].u.uint32;
1790 }
1791 if (VERR_BUFFER_OVERFLOW == vrc)
1792 return setError(E_UNEXPECTED,
1793 tr("Temporary failure due to guest activity, please retry"));
1794
1795 /*
1796 * Finally we have to unpack the data returned by the service into the safe
1797 * arrays supplied by the caller. We start by counting the number of entries.
1798 */
1799 const char *pszBuf
1800 = reinterpret_cast<const char *>(parm[1].u.pointer.addr);
1801 unsigned cEntries = 0;
1802 /* The list is terminated by a zero-length string at the end of a set
1803 * of four strings. */
1804 for (size_t i = 0; strlen(pszBuf + i) != 0; )
1805 {
1806 /* We are counting sets of four strings. */
1807 for (unsigned j = 0; j < 4; ++j)
1808 i += strlen(pszBuf + i) + 1;
1809 ++cEntries;
1810 }
1811
1812 /*
1813 * And now we create the COM safe arrays and fill them in.
1814 */
1815 com::SafeArray<BSTR> names(cEntries);
1816 com::SafeArray<BSTR> values(cEntries);
1817 com::SafeArray<LONG64> timestamps(cEntries);
1818 com::SafeArray<BSTR> flags(cEntries);
1819 size_t iBuf = 0;
1820 /* Rely on the service to have formated the data correctly. */
1821 for (unsigned i = 0; i < cEntries; ++i)
1822 {
1823 size_t cchName = strlen(pszBuf + iBuf);
1824 Bstr(pszBuf + iBuf).detachTo(&names[i]);
1825 iBuf += cchName + 1;
1826 size_t cchValue = strlen(pszBuf + iBuf);
1827 Bstr(pszBuf + iBuf).detachTo(&values[i]);
1828 iBuf += cchValue + 1;
1829 size_t cchTimestamp = strlen(pszBuf + iBuf);
1830 timestamps[i] = RTStrToUInt64(pszBuf + iBuf);
1831 iBuf += cchTimestamp + 1;
1832 size_t cchFlags = strlen(pszBuf + iBuf);
1833 Bstr(pszBuf + iBuf).detachTo(&flags[i]);
1834 iBuf += cchFlags + 1;
1835 }
1836 names.detachTo(ComSafeArrayOutArg(aNames));
1837 values.detachTo(ComSafeArrayOutArg(aValues));
1838 timestamps.detachTo(ComSafeArrayOutArg(aTimestamps));
1839 flags.detachTo(ComSafeArrayOutArg(aFlags));
1840 return S_OK;
1841}
1842
1843#endif /* VBOX_WITH_GUEST_PROPS */
1844
1845
1846// IConsole properties
1847/////////////////////////////////////////////////////////////////////////////
1848
1849STDMETHODIMP Console::COMGETTER(Machine)(IMachine **aMachine)
1850{
1851 CheckComArgOutPointerValid(aMachine);
1852
1853 AutoCaller autoCaller(this);
1854 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1855
1856 /* mMachine is constant during life time, no need to lock */
1857 mMachine.queryInterfaceTo(aMachine);
1858
1859 /* callers expect to get a valid reference, better fail than crash them */
1860 if (mMachine.isNull())
1861 return E_FAIL;
1862
1863 return S_OK;
1864}
1865
1866STDMETHODIMP Console::COMGETTER(State)(MachineState_T *aMachineState)
1867{
1868 CheckComArgOutPointerValid(aMachineState);
1869
1870 AutoCaller autoCaller(this);
1871 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1872
1873 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1874
1875 /* we return our local state (since it's always the same as on the server) */
1876 *aMachineState = mMachineState;
1877
1878 return S_OK;
1879}
1880
1881STDMETHODIMP Console::COMGETTER(Guest)(IGuest **aGuest)
1882{
1883 CheckComArgOutPointerValid(aGuest);
1884
1885 AutoCaller autoCaller(this);
1886 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1887
1888 /* mGuest is constant during life time, no need to lock */
1889 mGuest.queryInterfaceTo(aGuest);
1890
1891 return S_OK;
1892}
1893
1894STDMETHODIMP Console::COMGETTER(Keyboard)(IKeyboard **aKeyboard)
1895{
1896 CheckComArgOutPointerValid(aKeyboard);
1897
1898 AutoCaller autoCaller(this);
1899 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1900
1901 /* mKeyboard is constant during life time, no need to lock */
1902 mKeyboard.queryInterfaceTo(aKeyboard);
1903
1904 return S_OK;
1905}
1906
1907STDMETHODIMP Console::COMGETTER(Mouse)(IMouse **aMouse)
1908{
1909 CheckComArgOutPointerValid(aMouse);
1910
1911 AutoCaller autoCaller(this);
1912 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1913
1914 /* mMouse is constant during life time, no need to lock */
1915 mMouse.queryInterfaceTo(aMouse);
1916
1917 return S_OK;
1918}
1919
1920STDMETHODIMP Console::COMGETTER(Display)(IDisplay **aDisplay)
1921{
1922 CheckComArgOutPointerValid(aDisplay);
1923
1924 AutoCaller autoCaller(this);
1925 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1926
1927 /* mDisplay is constant during life time, no need to lock */
1928 mDisplay.queryInterfaceTo(aDisplay);
1929
1930 return S_OK;
1931}
1932
1933STDMETHODIMP Console::COMGETTER(Debugger)(IMachineDebugger **aDebugger)
1934{
1935 CheckComArgOutPointerValid(aDebugger);
1936
1937 AutoCaller autoCaller(this);
1938 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1939
1940 /* we need a write lock because of the lazy mDebugger initialization*/
1941 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1942
1943 /* check if we have to create the debugger object */
1944 if (!mDebugger)
1945 {
1946 unconst(mDebugger).createObject();
1947 mDebugger->init(this);
1948 }
1949
1950 mDebugger.queryInterfaceTo(aDebugger);
1951
1952 return S_OK;
1953}
1954
1955STDMETHODIMP Console::COMGETTER(USBDevices)(ComSafeArrayOut(IUSBDevice *, aUSBDevices))
1956{
1957 CheckComArgOutSafeArrayPointerValid(aUSBDevices);
1958
1959 AutoCaller autoCaller(this);
1960 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1961
1962 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1963
1964 SafeIfaceArray<IUSBDevice> collection(mUSBDevices);
1965 collection.detachTo(ComSafeArrayOutArg(aUSBDevices));
1966
1967 return S_OK;
1968}
1969
1970STDMETHODIMP Console::COMGETTER(RemoteUSBDevices)(ComSafeArrayOut(IHostUSBDevice *, aRemoteUSBDevices))
1971{
1972 CheckComArgOutSafeArrayPointerValid(aRemoteUSBDevices);
1973
1974 AutoCaller autoCaller(this);
1975 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1976
1977 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1978
1979 SafeIfaceArray<IHostUSBDevice> collection(mRemoteUSBDevices);
1980 collection.detachTo(ComSafeArrayOutArg(aRemoteUSBDevices));
1981
1982 return S_OK;
1983}
1984
1985STDMETHODIMP Console::COMGETTER(VRDEServerInfo)(IVRDEServerInfo **aVRDEServerInfo)
1986{
1987 CheckComArgOutPointerValid(aVRDEServerInfo);
1988
1989 AutoCaller autoCaller(this);
1990 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1991
1992 /* mVRDEServerInfo is constant during life time, no need to lock */
1993 mVRDEServerInfo.queryInterfaceTo(aVRDEServerInfo);
1994
1995 return S_OK;
1996}
1997
1998STDMETHODIMP
1999Console::COMGETTER(SharedFolders)(ComSafeArrayOut(ISharedFolder *, aSharedFolders))
2000{
2001 CheckComArgOutSafeArrayPointerValid(aSharedFolders);
2002
2003 AutoCaller autoCaller(this);
2004 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2005
2006 /* loadDataFromSavedState() needs a write lock */
2007 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2008
2009 /* Read console data stored in the saved state file (if not yet done) */
2010 HRESULT rc = loadDataFromSavedState();
2011 if (FAILED(rc)) return rc;
2012
2013 SafeIfaceArray<ISharedFolder> sf(m_mapSharedFolders);
2014 sf.detachTo(ComSafeArrayOutArg(aSharedFolders));
2015
2016 return S_OK;
2017}
2018
2019
2020STDMETHODIMP Console::COMGETTER(EventSource)(IEventSource ** aEventSource)
2021{
2022 CheckComArgOutPointerValid(aEventSource);
2023
2024 AutoCaller autoCaller(this);
2025 HRESULT hrc = autoCaller.rc();
2026 if (SUCCEEDED(hrc))
2027 {
2028 // no need to lock - lifetime constant
2029 mEventSource.queryInterfaceTo(aEventSource);
2030 }
2031
2032 return hrc;
2033}
2034
2035STDMETHODIMP Console::COMGETTER(AttachedPCIDevices)(ComSafeArrayOut(IPCIDeviceAttachment *, aAttachments))
2036{
2037 CheckComArgOutSafeArrayPointerValid(aAttachments);
2038
2039 AutoCaller autoCaller(this);
2040 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2041
2042 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2043
2044 if (mBusMgr)
2045 mBusMgr->listAttachedPCIDevices(ComSafeArrayOutArg(aAttachments));
2046 else
2047 {
2048 com::SafeIfaceArray<IPCIDeviceAttachment> result((size_t)0);
2049 result.detachTo(ComSafeArrayOutArg(aAttachments));
2050 }
2051
2052 return S_OK;
2053}
2054
2055STDMETHODIMP Console::COMGETTER(UseHostClipboard)(BOOL *aUseHostClipboard)
2056{
2057 CheckComArgOutPointerValid(aUseHostClipboard);
2058
2059 AutoCaller autoCaller(this);
2060 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2061
2062 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2063
2064 *aUseHostClipboard = mfUseHostClipboard;
2065
2066 return S_OK;
2067}
2068
2069STDMETHODIMP Console::COMSETTER(UseHostClipboard)(BOOL aUseHostClipboard)
2070{
2071 AutoCaller autoCaller(this);
2072 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2073
2074 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2075
2076 mfUseHostClipboard = !!aUseHostClipboard;
2077
2078 return S_OK;
2079}
2080
2081// IConsole methods
2082/////////////////////////////////////////////////////////////////////////////
2083
2084
2085STDMETHODIMP Console::PowerUp(IProgress **aProgress)
2086{
2087 return powerUp(aProgress, false /* aPaused */);
2088}
2089
2090STDMETHODIMP Console::PowerUpPaused(IProgress **aProgress)
2091{
2092 return powerUp(aProgress, true /* aPaused */);
2093}
2094
2095STDMETHODIMP Console::PowerDown(IProgress **aProgress)
2096{
2097 LogFlowThisFuncEnter();
2098
2099 CheckComArgOutPointerValid(aProgress);
2100
2101 AutoCaller autoCaller(this);
2102 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2103
2104 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2105
2106 LogFlowThisFunc(("mMachineState=%d\n", mMachineState));
2107 switch (mMachineState)
2108 {
2109 case MachineState_Running:
2110 case MachineState_Paused:
2111 case MachineState_Stuck:
2112 break;
2113
2114 /* Try cancel the teleportation. */
2115 case MachineState_Teleporting:
2116 case MachineState_TeleportingPausedVM:
2117 if (!mptrCancelableProgress.isNull())
2118 {
2119 HRESULT hrc = mptrCancelableProgress->Cancel();
2120 if (SUCCEEDED(hrc))
2121 break;
2122 }
2123 return setError(VBOX_E_INVALID_VM_STATE, tr("Cannot power down at this point in a teleportation"));
2124
2125 /* Try cancel the live snapshot. */
2126 case MachineState_LiveSnapshotting:
2127 if (!mptrCancelableProgress.isNull())
2128 {
2129 HRESULT hrc = mptrCancelableProgress->Cancel();
2130 if (SUCCEEDED(hrc))
2131 break;
2132 }
2133 return setError(VBOX_E_INVALID_VM_STATE, tr("Cannot power down at this point in a live snapshot"));
2134
2135 /* Try cancel the FT sync. */
2136 case MachineState_FaultTolerantSyncing:
2137 if (!mptrCancelableProgress.isNull())
2138 {
2139 HRESULT hrc = mptrCancelableProgress->Cancel();
2140 if (SUCCEEDED(hrc))
2141 break;
2142 }
2143 return setError(VBOX_E_INVALID_VM_STATE, tr("Cannot power down at this point in a fault tolerant sync"));
2144
2145 /* extra nice error message for a common case */
2146 case MachineState_Saved:
2147 return setError(VBOX_E_INVALID_VM_STATE, tr("Cannot power down a saved virtual machine"));
2148 case MachineState_Stopping:
2149 return setError(VBOX_E_INVALID_VM_STATE, tr("The virtual machine is being powered down"));
2150 default:
2151 return setError(VBOX_E_INVALID_VM_STATE,
2152 tr("Invalid machine state: %s (must be Running, Paused or Stuck)"),
2153 Global::stringifyMachineState(mMachineState));
2154 }
2155
2156 LogFlowThisFunc(("Initiating SHUTDOWN request...\n"));
2157
2158 /* memorize the current machine state */
2159 MachineState_T lastMachineState = mMachineState;
2160
2161 HRESULT rc = S_OK;
2162 bool fBeganPowerDown = false;
2163
2164 do
2165 {
2166 ComPtr<IProgress> pProgress;
2167
2168 alock.release();
2169
2170#ifdef VBOX_WITH_GUEST_PROPS
2171 if (isResetTurnedIntoPowerOff())
2172 {
2173 mMachine->DeleteGuestProperty(Bstr("/VirtualBox/HostInfo/VMPowerOffReason").raw());
2174 mMachine->SetGuestProperty(Bstr("/VirtualBox/HostInfo/VMPowerOffReason").raw(),
2175 Bstr("PowerOff").raw(), Bstr("RDONLYGUEST").raw());
2176 mMachine->SaveSettings();
2177 }
2178#endif
2179
2180 alock.acquire();
2181
2182 /*
2183 * request a progress object from the server
2184 * (this will set the machine state to Stopping on the server to block
2185 * others from accessing this machine)
2186 */
2187 rc = mControl->BeginPoweringDown(pProgress.asOutParam());
2188 if (FAILED(rc))
2189 break;
2190
2191 fBeganPowerDown = true;
2192
2193 /* sync the state with the server */
2194 setMachineStateLocally(MachineState_Stopping);
2195
2196 /* setup task object and thread to carry out the operation asynchronously */
2197 std::auto_ptr<VMPowerDownTask> task(new VMPowerDownTask(this, pProgress));
2198 AssertBreakStmt(task->isOk(), rc = E_FAIL);
2199
2200 int vrc = RTThreadCreate(NULL, Console::powerDownThread,
2201 (void *) task.get(), 0,
2202 RTTHREADTYPE_MAIN_WORKER, 0,
2203 "VMPwrDwn");
2204 if (RT_FAILURE(vrc))
2205 {
2206 rc = setError(E_FAIL, "Could not create VMPowerDown thread (%Rrc)", vrc);
2207 break;
2208 }
2209
2210 /* task is now owned by powerDownThread(), so release it */
2211 task.release();
2212
2213 /* pass the progress to the caller */
2214 pProgress.queryInterfaceTo(aProgress);
2215 }
2216 while (0);
2217
2218 if (FAILED(rc))
2219 {
2220 /* preserve existing error info */
2221 ErrorInfoKeeper eik;
2222
2223 if (fBeganPowerDown)
2224 {
2225 /*
2226 * cancel the requested power down procedure.
2227 * This will reset the machine state to the state it had right
2228 * before calling mControl->BeginPoweringDown().
2229 */
2230 mControl->EndPoweringDown(eik.getResultCode(), eik.getText().raw()); }
2231
2232 setMachineStateLocally(lastMachineState);
2233 }
2234
2235 LogFlowThisFunc(("rc=%Rhrc\n", rc));
2236 LogFlowThisFuncLeave();
2237
2238 return rc;
2239}
2240
2241STDMETHODIMP Console::Reset()
2242{
2243 LogFlowThisFuncEnter();
2244
2245 AutoCaller autoCaller(this);
2246 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2247
2248 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2249
2250 LogFlowThisFunc(("mMachineState=%d\n", mMachineState));
2251 if ( mMachineState != MachineState_Running
2252 && mMachineState != MachineState_Teleporting
2253 && mMachineState != MachineState_LiveSnapshotting
2254 /** @todo r=bird: This should be allowed on paused VMs as well. Later. */
2255 )
2256 return setInvalidMachineStateError();
2257
2258 /* protect mpUVM */
2259 SafeVMPtr ptrVM(this);
2260 if (!ptrVM.isOk())
2261 return ptrVM.rc();
2262
2263 /* release the lock before a VMR3* call (EMT will call us back)! */
2264 alock.release();
2265
2266 int vrc = VMR3Reset(ptrVM.rawUVM());
2267
2268 HRESULT rc = RT_SUCCESS(vrc) ? S_OK :
2269 setError(VBOX_E_VM_ERROR,
2270 tr("Could not reset the machine (%Rrc)"),
2271 vrc);
2272
2273 LogFlowThisFunc(("mMachineState=%d, rc=%Rhrc\n", mMachineState, rc));
2274 LogFlowThisFuncLeave();
2275 return rc;
2276}
2277
2278/*static*/ DECLCALLBACK(int) Console::unplugCpu(Console *pThis, PUVM pUVM, VMCPUID idCpu)
2279{
2280 LogFlowFunc(("pThis=%p pVM=%p idCpu=%u\n", pThis, pUVM, idCpu));
2281
2282 AssertReturn(pThis, VERR_INVALID_PARAMETER);
2283
2284 int vrc = PDMR3DeviceDetach(pUVM, "acpi", 0, idCpu, 0);
2285 Log(("UnplugCpu: rc=%Rrc\n", vrc));
2286
2287 return vrc;
2288}
2289
2290HRESULT Console::doCPURemove(ULONG aCpu, PUVM pUVM)
2291{
2292 HRESULT rc = S_OK;
2293
2294 LogFlowThisFuncEnter();
2295
2296 AutoCaller autoCaller(this);
2297 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2298
2299 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2300
2301 LogFlowThisFunc(("mMachineState=%d\n", mMachineState));
2302 AssertReturn(m_pVMMDev, E_FAIL);
2303 PPDMIVMMDEVPORT pVmmDevPort = m_pVMMDev->getVMMDevPort();
2304 AssertReturn(pVmmDevPort, E_FAIL);
2305
2306 if ( mMachineState != MachineState_Running
2307 && mMachineState != MachineState_Teleporting
2308 && mMachineState != MachineState_LiveSnapshotting
2309 )
2310 return setInvalidMachineStateError();
2311
2312 /* Check if the CPU is present */
2313 BOOL fCpuAttached;
2314 rc = mMachine->GetCPUStatus(aCpu, &fCpuAttached);
2315 if (FAILED(rc))
2316 return rc;
2317 if (!fCpuAttached)
2318 return setError(E_FAIL, tr("CPU %d is not attached"), aCpu);
2319
2320 /* Leave the lock before any EMT/VMMDev call. */
2321 alock.release();
2322 bool fLocked = true;
2323
2324 /* Check if the CPU is unlocked */
2325 PPDMIBASE pBase;
2326 int vrc = PDMR3QueryDeviceLun(pUVM, "acpi", 0, aCpu, &pBase);
2327 if (RT_SUCCESS(vrc))
2328 {
2329 Assert(pBase);
2330 PPDMIACPIPORT pApicPort = PDMIBASE_QUERY_INTERFACE(pBase, PDMIACPIPORT);
2331
2332 /* Notify the guest if possible. */
2333 uint32_t idCpuCore, idCpuPackage;
2334 vrc = VMR3GetCpuCoreAndPackageIdFromCpuId(pUVM, aCpu, &idCpuCore, &idCpuPackage); AssertRC(vrc);
2335 if (RT_SUCCESS(vrc))
2336 vrc = pVmmDevPort->pfnCpuHotUnplug(pVmmDevPort, idCpuCore, idCpuPackage);
2337 if (RT_SUCCESS(vrc))
2338 {
2339 unsigned cTries = 100;
2340 do
2341 {
2342 /* It will take some time until the event is processed in the guest. Wait... */
2343 vrc = pApicPort ? pApicPort->pfnGetCpuStatus(pApicPort, aCpu, &fLocked) : VERR_INVALID_POINTER;
2344 if (RT_SUCCESS(vrc) && !fLocked)
2345 break;
2346
2347 /* Sleep a bit */
2348 RTThreadSleep(100);
2349 } while (cTries-- > 0);
2350 }
2351 else if (vrc == VERR_CPU_HOTPLUG_NOT_MONITORED_BY_GUEST)
2352 {
2353 /* Query one time. It is possible that the user ejected the CPU. */
2354 vrc = pApicPort ? pApicPort->pfnGetCpuStatus(pApicPort, aCpu, &fLocked) : VERR_INVALID_POINTER;
2355 }
2356 }
2357
2358 /* If the CPU was unlocked we can detach it now. */
2359 if (RT_SUCCESS(vrc) && !fLocked)
2360 {
2361 /*
2362 * Call worker in EMT, that's faster and safer than doing everything
2363 * using VMR3ReqCall.
2364 */
2365 PVMREQ pReq;
2366 vrc = VMR3ReqCallU(pUVM, 0, &pReq, 0 /* no wait! */, VMREQFLAGS_VBOX_STATUS,
2367 (PFNRT)Console::unplugCpu, 3,
2368 this, pUVM, (VMCPUID)aCpu);
2369 if (vrc == VERR_TIMEOUT || RT_SUCCESS(vrc))
2370 {
2371 vrc = VMR3ReqWait(pReq, RT_INDEFINITE_WAIT);
2372 AssertRC(vrc);
2373 if (RT_SUCCESS(vrc))
2374 vrc = pReq->iStatus;
2375 }
2376 VMR3ReqFree(pReq);
2377
2378 if (RT_SUCCESS(vrc))
2379 {
2380 /* Detach it from the VM */
2381 vrc = VMR3HotUnplugCpu(pUVM, aCpu);
2382 AssertRC(vrc);
2383 }
2384 else
2385 rc = setError(VBOX_E_VM_ERROR,
2386 tr("Hot-Remove failed (rc=%Rrc)"), vrc);
2387 }
2388 else
2389 rc = setError(VBOX_E_VM_ERROR,
2390 tr("Hot-Remove was aborted because the CPU may still be used by the guest"), VERR_RESOURCE_BUSY);
2391
2392 LogFlowThisFunc(("mMachineState=%d, rc=%Rhrc\n", mMachineState, rc));
2393 LogFlowThisFuncLeave();
2394 return rc;
2395}
2396
2397/*static*/ DECLCALLBACK(int) Console::plugCpu(Console *pThis, PUVM pUVM, VMCPUID idCpu)
2398{
2399 LogFlowFunc(("pThis=%p uCpu=%u\n", pThis, idCpu));
2400
2401 AssertReturn(pThis, VERR_INVALID_PARAMETER);
2402
2403 int rc = VMR3HotPlugCpu(pUVM, idCpu);
2404 AssertRC(rc);
2405
2406 PCFGMNODE pInst = CFGMR3GetChild(CFGMR3GetRootU(pUVM), "Devices/acpi/0/");
2407 AssertRelease(pInst);
2408 /* nuke anything which might have been left behind. */
2409 CFGMR3RemoveNode(CFGMR3GetChildF(pInst, "LUN#%u", idCpu));
2410
2411#define RC_CHECK() do { if (RT_FAILURE(rc)) { AssertReleaseRC(rc); break; } } while (0)
2412
2413 PCFGMNODE pLunL0;
2414 PCFGMNODE pCfg;
2415 rc = CFGMR3InsertNodeF(pInst, &pLunL0, "LUN#%u", idCpu); RC_CHECK();
2416 rc = CFGMR3InsertString(pLunL0, "Driver", "ACPICpu"); RC_CHECK();
2417 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
2418
2419 /*
2420 * Attach the driver.
2421 */
2422 PPDMIBASE pBase;
2423 rc = PDMR3DeviceAttach(pUVM, "acpi", 0, idCpu, 0, &pBase); RC_CHECK();
2424
2425 Log(("PlugCpu: rc=%Rrc\n", rc));
2426
2427 CFGMR3Dump(pInst);
2428
2429#undef RC_CHECK
2430
2431 return VINF_SUCCESS;
2432}
2433
2434HRESULT Console::doCPUAdd(ULONG aCpu, PUVM pUVM)
2435{
2436 HRESULT rc = S_OK;
2437
2438 LogFlowThisFuncEnter();
2439
2440 AutoCaller autoCaller(this);
2441 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2442
2443 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2444
2445 LogFlowThisFunc(("mMachineState=%d\n", mMachineState));
2446 if ( mMachineState != MachineState_Running
2447 && mMachineState != MachineState_Teleporting
2448 && mMachineState != MachineState_LiveSnapshotting
2449 /** @todo r=bird: This should be allowed on paused VMs as well. Later. */
2450 )
2451 return setInvalidMachineStateError();
2452
2453 AssertReturn(m_pVMMDev, E_FAIL);
2454 PPDMIVMMDEVPORT pDevPort = m_pVMMDev->getVMMDevPort();
2455 AssertReturn(pDevPort, E_FAIL);
2456
2457 /* Check if the CPU is present */
2458 BOOL fCpuAttached;
2459 rc = mMachine->GetCPUStatus(aCpu, &fCpuAttached);
2460 if (FAILED(rc)) return rc;
2461
2462 if (fCpuAttached)
2463 return setError(E_FAIL,
2464 tr("CPU %d is already attached"), aCpu);
2465
2466 /*
2467 * Call worker in EMT, that's faster and safer than doing everything
2468 * using VMR3ReqCall. Note that we separate VMR3ReqCall from VMR3ReqWait
2469 * here to make requests from under the lock in order to serialize them.
2470 */
2471 PVMREQ pReq;
2472 int vrc = VMR3ReqCallU(pUVM, 0, &pReq, 0 /* no wait! */, VMREQFLAGS_VBOX_STATUS,
2473 (PFNRT)Console::plugCpu, 3,
2474 this, pUVM, aCpu);
2475
2476 /* release the lock before a VMR3* call (EMT will call us back)! */
2477 alock.release();
2478
2479 if (vrc == VERR_TIMEOUT || RT_SUCCESS(vrc))
2480 {
2481 vrc = VMR3ReqWait(pReq, RT_INDEFINITE_WAIT);
2482 AssertRC(vrc);
2483 if (RT_SUCCESS(vrc))
2484 vrc = pReq->iStatus;
2485 }
2486 VMR3ReqFree(pReq);
2487
2488 rc = RT_SUCCESS(vrc) ? S_OK :
2489 setError(VBOX_E_VM_ERROR,
2490 tr("Could not add CPU to the machine (%Rrc)"),
2491 vrc);
2492
2493 if (RT_SUCCESS(vrc))
2494 {
2495 /* Notify the guest if possible. */
2496 uint32_t idCpuCore, idCpuPackage;
2497 vrc = VMR3GetCpuCoreAndPackageIdFromCpuId(pUVM, aCpu, &idCpuCore, &idCpuPackage); AssertRC(vrc);
2498 if (RT_SUCCESS(vrc))
2499 vrc = pDevPort->pfnCpuHotPlug(pDevPort, idCpuCore, idCpuPackage);
2500 /** @todo warning if the guest doesn't support it */
2501 }
2502
2503 LogFlowThisFunc(("mMachineState=%d, rc=%Rhrc\n", mMachineState, rc));
2504 LogFlowThisFuncLeave();
2505 return rc;
2506}
2507
2508STDMETHODIMP Console::Pause()
2509{
2510 LogFlowThisFuncEnter();
2511
2512 HRESULT rc = pause(Reason_Unspecified);
2513
2514 LogFlowThisFunc(("rc=%Rhrc\n", rc));
2515 LogFlowThisFuncLeave();
2516 return rc;
2517}
2518
2519STDMETHODIMP Console::Resume()
2520{
2521 LogFlowThisFuncEnter();
2522
2523 HRESULT rc = resume(Reason_Unspecified);
2524
2525 LogFlowThisFunc(("rc=%Rhrc\n", rc));
2526 LogFlowThisFuncLeave();
2527 return rc;
2528}
2529
2530STDMETHODIMP Console::PowerButton()
2531{
2532 LogFlowThisFuncEnter();
2533
2534 AutoCaller autoCaller(this);
2535 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2536
2537 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2538
2539 if ( mMachineState != MachineState_Running
2540 && mMachineState != MachineState_Teleporting
2541 && mMachineState != MachineState_LiveSnapshotting
2542 )
2543 return setInvalidMachineStateError();
2544
2545 /* get the VM handle. */
2546 SafeVMPtr ptrVM(this);
2547 if (!ptrVM.isOk())
2548 return ptrVM.rc();
2549
2550 // no need to release lock, as there are no cross-thread callbacks
2551
2552 /* get the acpi device interface and press the button. */
2553 PPDMIBASE pBase;
2554 int vrc = PDMR3QueryDeviceLun(ptrVM.rawUVM(), "acpi", 0, 0, &pBase);
2555 if (RT_SUCCESS(vrc))
2556 {
2557 Assert(pBase);
2558 PPDMIACPIPORT pPort = PDMIBASE_QUERY_INTERFACE(pBase, PDMIACPIPORT);
2559 if (pPort)
2560 vrc = pPort->pfnPowerButtonPress(pPort);
2561 else
2562 vrc = VERR_PDM_MISSING_INTERFACE;
2563 }
2564
2565 HRESULT rc = RT_SUCCESS(vrc) ? S_OK :
2566 setError(VBOX_E_PDM_ERROR,
2567 tr("Controlled power off failed (%Rrc)"),
2568 vrc);
2569
2570 LogFlowThisFunc(("rc=%Rhrc\n", rc));
2571 LogFlowThisFuncLeave();
2572 return rc;
2573}
2574
2575STDMETHODIMP Console::GetPowerButtonHandled(BOOL *aHandled)
2576{
2577 LogFlowThisFuncEnter();
2578
2579 CheckComArgOutPointerValid(aHandled);
2580
2581 *aHandled = FALSE;
2582
2583 AutoCaller autoCaller(this);
2584
2585 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2586
2587 if ( mMachineState != MachineState_Running
2588 && mMachineState != MachineState_Teleporting
2589 && mMachineState != MachineState_LiveSnapshotting
2590 )
2591 return setInvalidMachineStateError();
2592
2593 /* get the VM handle. */
2594 SafeVMPtr ptrVM(this);
2595 if (!ptrVM.isOk())
2596 return ptrVM.rc();
2597
2598 // no need to release lock, as there are no cross-thread callbacks
2599
2600 /* get the acpi device interface and check if the button press was handled. */
2601 PPDMIBASE pBase;
2602 int vrc = PDMR3QueryDeviceLun(ptrVM.rawUVM(), "acpi", 0, 0, &pBase);
2603 if (RT_SUCCESS(vrc))
2604 {
2605 Assert(pBase);
2606 PPDMIACPIPORT pPort = PDMIBASE_QUERY_INTERFACE(pBase, PDMIACPIPORT);
2607 if (pPort)
2608 {
2609 bool fHandled = false;
2610 vrc = pPort->pfnGetPowerButtonHandled(pPort, &fHandled);
2611 if (RT_SUCCESS(vrc))
2612 *aHandled = fHandled;
2613 }
2614 else
2615 vrc = VERR_PDM_MISSING_INTERFACE;
2616 }
2617
2618 HRESULT rc = RT_SUCCESS(vrc) ? S_OK :
2619 setError(VBOX_E_PDM_ERROR,
2620 tr("Checking if the ACPI Power Button event was handled by the guest OS failed (%Rrc)"),
2621 vrc);
2622
2623 LogFlowThisFunc(("rc=%Rhrc\n", rc));
2624 LogFlowThisFuncLeave();
2625 return rc;
2626}
2627
2628STDMETHODIMP Console::GetGuestEnteredACPIMode(BOOL *aEntered)
2629{
2630 LogFlowThisFuncEnter();
2631
2632 CheckComArgOutPointerValid(aEntered);
2633
2634 *aEntered = FALSE;
2635
2636 AutoCaller autoCaller(this);
2637
2638 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2639
2640 if ( mMachineState != MachineState_Running
2641 && mMachineState != MachineState_Teleporting
2642 && mMachineState != MachineState_LiveSnapshotting
2643 )
2644 return setError(VBOX_E_INVALID_VM_STATE,
2645 tr("Invalid machine state %s when checking if the guest entered the ACPI mode)"),
2646 Global::stringifyMachineState(mMachineState));
2647
2648 /* get the VM handle. */
2649 SafeVMPtr ptrVM(this);
2650 if (!ptrVM.isOk())
2651 return ptrVM.rc();
2652
2653 // no need to release lock, as there are no cross-thread callbacks
2654
2655 /* get the acpi device interface and query the information. */
2656 PPDMIBASE pBase;
2657 int vrc = PDMR3QueryDeviceLun(ptrVM.rawUVM(), "acpi", 0, 0, &pBase);
2658 if (RT_SUCCESS(vrc))
2659 {
2660 Assert(pBase);
2661 PPDMIACPIPORT pPort = PDMIBASE_QUERY_INTERFACE(pBase, PDMIACPIPORT);
2662 if (pPort)
2663 {
2664 bool fEntered = false;
2665 vrc = pPort->pfnGetGuestEnteredACPIMode(pPort, &fEntered);
2666 if (RT_SUCCESS(vrc))
2667 *aEntered = fEntered;
2668 }
2669 else
2670 vrc = VERR_PDM_MISSING_INTERFACE;
2671 }
2672
2673 LogFlowThisFuncLeave();
2674 return S_OK;
2675}
2676
2677STDMETHODIMP Console::SleepButton()
2678{
2679 LogFlowThisFuncEnter();
2680
2681 AutoCaller autoCaller(this);
2682 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2683
2684 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2685
2686 if ( mMachineState != MachineState_Running
2687 && mMachineState != MachineState_Teleporting
2688 && mMachineState != MachineState_LiveSnapshotting)
2689 return setInvalidMachineStateError();
2690
2691 /* get the VM handle. */
2692 SafeVMPtr ptrVM(this);
2693 if (!ptrVM.isOk())
2694 return ptrVM.rc();
2695
2696 // no need to release lock, as there are no cross-thread callbacks
2697
2698 /* get the acpi device interface and press the sleep button. */
2699 PPDMIBASE pBase;
2700 int vrc = PDMR3QueryDeviceLun(ptrVM.rawUVM(), "acpi", 0, 0, &pBase);
2701 if (RT_SUCCESS(vrc))
2702 {
2703 Assert(pBase);
2704 PPDMIACPIPORT pPort = PDMIBASE_QUERY_INTERFACE(pBase, PDMIACPIPORT);
2705 if (pPort)
2706 vrc = pPort->pfnSleepButtonPress(pPort);
2707 else
2708 vrc = VERR_PDM_MISSING_INTERFACE;
2709 }
2710
2711 HRESULT rc = RT_SUCCESS(vrc) ? S_OK :
2712 setError(VBOX_E_PDM_ERROR,
2713 tr("Sending sleep button event failed (%Rrc)"),
2714 vrc);
2715
2716 LogFlowThisFunc(("rc=%Rhrc\n", rc));
2717 LogFlowThisFuncLeave();
2718 return rc;
2719}
2720
2721STDMETHODIMP Console::SaveState(IProgress **aProgress)
2722{
2723 LogFlowThisFuncEnter();
2724
2725 HRESULT rc = saveState(Reason_Unspecified, aProgress);
2726
2727 LogFlowThisFunc(("rc=%Rhrc\n", rc));
2728 LogFlowThisFuncLeave();
2729 return rc;
2730}
2731
2732STDMETHODIMP Console::AdoptSavedState(IN_BSTR aSavedStateFile)
2733{
2734 CheckComArgStrNotEmptyOrNull(aSavedStateFile);
2735
2736 AutoCaller autoCaller(this);
2737 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2738
2739 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2740
2741 if ( mMachineState != MachineState_PoweredOff
2742 && mMachineState != MachineState_Teleported
2743 && mMachineState != MachineState_Aborted
2744 )
2745 return setError(VBOX_E_INVALID_VM_STATE,
2746 tr("Cannot adopt the saved machine state as the machine is not in Powered Off, Teleported or Aborted state (machine state: %s)"),
2747 Global::stringifyMachineState(mMachineState));
2748
2749 return mControl->AdoptSavedState(aSavedStateFile);
2750}
2751
2752STDMETHODIMP Console::DiscardSavedState(BOOL aRemoveFile)
2753{
2754 AutoCaller autoCaller(this);
2755 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2756
2757 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2758
2759 if (mMachineState != MachineState_Saved)
2760 return setError(VBOX_E_INVALID_VM_STATE,
2761 tr("Cannot delete the machine state as the machine is not in the saved state (machine state: %s)"),
2762 Global::stringifyMachineState(mMachineState));
2763
2764 HRESULT rc = mControl->SetRemoveSavedStateFile(aRemoveFile);
2765 if (FAILED(rc)) return rc;
2766
2767 /*
2768 * Saved -> PoweredOff transition will be detected in the SessionMachine
2769 * and properly handled.
2770 */
2771 rc = setMachineState(MachineState_PoweredOff);
2772
2773 return rc;
2774}
2775
2776/** read the value of a LED. */
2777inline uint32_t readAndClearLed(PPDMLED pLed)
2778{
2779 if (!pLed)
2780 return 0;
2781 uint32_t u32 = pLed->Actual.u32 | pLed->Asserted.u32;
2782 pLed->Asserted.u32 = 0;
2783 return u32;
2784}
2785
2786STDMETHODIMP Console::GetDeviceActivity(DeviceType_T aDeviceType,
2787 DeviceActivity_T *aDeviceActivity)
2788{
2789 CheckComArgNotNull(aDeviceActivity);
2790
2791 AutoCaller autoCaller(this);
2792 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2793
2794 /*
2795 * Note: we don't lock the console object here because
2796 * readAndClearLed() should be thread safe.
2797 */
2798
2799 /* Get LED array to read */
2800 PDMLEDCORE SumLed = {0};
2801 switch (aDeviceType)
2802 {
2803 case DeviceType_Floppy:
2804 case DeviceType_DVD:
2805 case DeviceType_HardDisk:
2806 {
2807 for (unsigned i = 0; i < RT_ELEMENTS(mapStorageLeds); ++i)
2808 if (maStorageDevType[i] == aDeviceType)
2809 SumLed.u32 |= readAndClearLed(mapStorageLeds[i]);
2810 break;
2811 }
2812
2813 case DeviceType_Network:
2814 {
2815 for (unsigned i = 0; i < RT_ELEMENTS(mapNetworkLeds); ++i)
2816 SumLed.u32 |= readAndClearLed(mapNetworkLeds[i]);
2817 break;
2818 }
2819
2820 case DeviceType_USB:
2821 {
2822 for (unsigned i = 0; i < RT_ELEMENTS(mapUSBLed); ++i)
2823 SumLed.u32 |= readAndClearLed(mapUSBLed[i]);
2824 break;
2825 }
2826
2827 case DeviceType_SharedFolder:
2828 {
2829 SumLed.u32 |= readAndClearLed(mapSharedFolderLed);
2830 break;
2831 }
2832
2833 default:
2834 return setError(E_INVALIDARG,
2835 tr("Invalid device type: %d"),
2836 aDeviceType);
2837 }
2838
2839 /* Compose the result */
2840 switch (SumLed.u32 & (PDMLED_READING | PDMLED_WRITING))
2841 {
2842 case 0:
2843 *aDeviceActivity = DeviceActivity_Idle;
2844 break;
2845 case PDMLED_READING:
2846 *aDeviceActivity = DeviceActivity_Reading;
2847 break;
2848 case PDMLED_WRITING:
2849 case PDMLED_READING | PDMLED_WRITING:
2850 *aDeviceActivity = DeviceActivity_Writing;
2851 break;
2852 }
2853
2854 return S_OK;
2855}
2856
2857STDMETHODIMP Console::AttachUSBDevice(IN_BSTR aId)
2858{
2859#ifdef VBOX_WITH_USB
2860 AutoCaller autoCaller(this);
2861 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2862
2863 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2864
2865 if ( mMachineState != MachineState_Running
2866 && mMachineState != MachineState_Paused)
2867 return setError(VBOX_E_INVALID_VM_STATE,
2868 tr("Cannot attach a USB device to the machine which is not running or paused (machine state: %s)"),
2869 Global::stringifyMachineState(mMachineState));
2870
2871 /* Get the VM handle. */
2872 SafeVMPtr ptrVM(this);
2873 if (!ptrVM.isOk())
2874 return ptrVM.rc();
2875
2876 /* Don't proceed unless we have a USB controller. */
2877 if (!mfVMHasUsbController)
2878 return setError(VBOX_E_PDM_ERROR,
2879 tr("The virtual machine does not have a USB controller"));
2880
2881 /* release the lock because the USB Proxy service may call us back
2882 * (via onUSBDeviceAttach()) */
2883 alock.release();
2884
2885 /* Request the device capture */
2886 return mControl->CaptureUSBDevice(aId);
2887
2888#else /* !VBOX_WITH_USB */
2889 return setError(VBOX_E_PDM_ERROR,
2890 tr("The virtual machine does not have a USB controller"));
2891#endif /* !VBOX_WITH_USB */
2892}
2893
2894STDMETHODIMP Console::DetachUSBDevice(IN_BSTR aId, IUSBDevice **aDevice)
2895{
2896#ifdef VBOX_WITH_USB
2897 CheckComArgOutPointerValid(aDevice);
2898
2899 AutoCaller autoCaller(this);
2900 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2901
2902 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2903
2904 /* Find it. */
2905 ComObjPtr<OUSBDevice> pUSBDevice;
2906 USBDeviceList::iterator it = mUSBDevices.begin();
2907 Guid uuid(aId);
2908 while (it != mUSBDevices.end())
2909 {
2910 if ((*it)->id() == uuid)
2911 {
2912 pUSBDevice = *it;
2913 break;
2914 }
2915 ++it;
2916 }
2917
2918 if (!pUSBDevice)
2919 return setError(E_INVALIDARG,
2920 tr("USB device with UUID {%RTuuid} is not attached to this machine"),
2921 Guid(aId).raw());
2922
2923 /* Remove the device from the collection, it is re-added below for failures */
2924 mUSBDevices.erase(it);
2925
2926 /*
2927 * Inform the USB device and USB proxy about what's cooking.
2928 */
2929 alock.release();
2930 HRESULT rc = mControl->DetachUSBDevice(aId, false /* aDone */);
2931 if (FAILED(rc))
2932 {
2933 /* Re-add the device to the collection */
2934 alock.acquire();
2935 mUSBDevices.push_back(pUSBDevice);
2936 return rc;
2937 }
2938
2939 /* Request the PDM to detach the USB device. */
2940 rc = detachUSBDevice(pUSBDevice);
2941 if (SUCCEEDED(rc))
2942 {
2943 /* Request the device release. Even if it fails, the device will
2944 * remain as held by proxy, which is OK for us (the VM process). */
2945 rc = mControl->DetachUSBDevice(aId, true /* aDone */);
2946 }
2947 else
2948 {
2949 /* Re-add the device to the collection */
2950 alock.acquire();
2951 mUSBDevices.push_back(pUSBDevice);
2952 }
2953
2954 return rc;
2955
2956
2957#else /* !VBOX_WITH_USB */
2958 return setError(VBOX_E_PDM_ERROR,
2959 tr("The virtual machine does not have a USB controller"));
2960#endif /* !VBOX_WITH_USB */
2961}
2962
2963STDMETHODIMP Console::FindUSBDeviceByAddress(IN_BSTR aAddress, IUSBDevice **aDevice)
2964{
2965#ifdef VBOX_WITH_USB
2966 CheckComArgStrNotEmptyOrNull(aAddress);
2967 CheckComArgOutPointerValid(aDevice);
2968
2969 *aDevice = NULL;
2970
2971 SafeIfaceArray<IUSBDevice> devsvec;
2972 HRESULT rc = COMGETTER(USBDevices)(ComSafeArrayAsOutParam(devsvec));
2973 if (FAILED(rc)) return rc;
2974
2975 for (size_t i = 0; i < devsvec.size(); ++i)
2976 {
2977 Bstr address;
2978 rc = devsvec[i]->COMGETTER(Address)(address.asOutParam());
2979 if (FAILED(rc)) return rc;
2980 if (address == aAddress)
2981 {
2982 ComObjPtr<OUSBDevice> pUSBDevice;
2983 pUSBDevice.createObject();
2984 pUSBDevice->init(devsvec[i]);
2985 return pUSBDevice.queryInterfaceTo(aDevice);
2986 }
2987 }
2988
2989 return setErrorNoLog(VBOX_E_OBJECT_NOT_FOUND,
2990 tr("Could not find a USB device with address '%ls'"),
2991 aAddress);
2992
2993#else /* !VBOX_WITH_USB */
2994 return E_NOTIMPL;
2995#endif /* !VBOX_WITH_USB */
2996}
2997
2998STDMETHODIMP Console::FindUSBDeviceById(IN_BSTR aId, IUSBDevice **aDevice)
2999{
3000#ifdef VBOX_WITH_USB
3001 CheckComArgExpr(aId, Guid(aId).isValid());
3002 CheckComArgOutPointerValid(aDevice);
3003
3004 *aDevice = NULL;
3005
3006 SafeIfaceArray<IUSBDevice> devsvec;
3007 HRESULT rc = COMGETTER(USBDevices)(ComSafeArrayAsOutParam(devsvec));
3008 if (FAILED(rc)) return rc;
3009
3010 for (size_t i = 0; i < devsvec.size(); ++i)
3011 {
3012 Bstr id;
3013 rc = devsvec[i]->COMGETTER(Id)(id.asOutParam());
3014 if (FAILED(rc)) return rc;
3015 if (id == aId)
3016 {
3017 ComObjPtr<OUSBDevice> pUSBDevice;
3018 pUSBDevice.createObject();
3019 pUSBDevice->init(devsvec[i]);
3020 return pUSBDevice.queryInterfaceTo(aDevice);
3021 }
3022 }
3023
3024 return setErrorNoLog(VBOX_E_OBJECT_NOT_FOUND,
3025 tr("Could not find a USB device with uuid {%RTuuid}"),
3026 Guid(aId).raw());
3027
3028#else /* !VBOX_WITH_USB */
3029 return E_NOTIMPL;
3030#endif /* !VBOX_WITH_USB */
3031}
3032
3033STDMETHODIMP
3034Console::CreateSharedFolder(IN_BSTR aName, IN_BSTR aHostPath, BOOL aWritable, BOOL aAutoMount)
3035{
3036 CheckComArgStrNotEmptyOrNull(aName);
3037 CheckComArgStrNotEmptyOrNull(aHostPath);
3038
3039 LogFlowThisFunc(("Entering for '%ls' -> '%ls'\n", aName, aHostPath));
3040
3041 AutoCaller autoCaller(this);
3042 if (FAILED(autoCaller.rc())) return autoCaller.rc();
3043
3044 Utf8Str strName(aName);
3045 Utf8Str strHostPath(aHostPath);
3046
3047 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
3048
3049 /// @todo see @todo in AttachUSBDevice() about the Paused state
3050 if (mMachineState == MachineState_Saved)
3051 return setError(VBOX_E_INVALID_VM_STATE,
3052 tr("Cannot create a transient shared folder on the machine in the saved state"));
3053 if ( mMachineState != MachineState_PoweredOff
3054 && mMachineState != MachineState_Teleported
3055 && mMachineState != MachineState_Aborted
3056 && mMachineState != MachineState_Running
3057 && mMachineState != MachineState_Paused
3058 )
3059 return setError(VBOX_E_INVALID_VM_STATE,
3060 tr("Cannot create a transient shared folder on the machine while it is changing the state (machine state: %s)"),
3061 Global::stringifyMachineState(mMachineState));
3062
3063 ComObjPtr<SharedFolder> pSharedFolder;
3064 HRESULT rc = findSharedFolder(strName, pSharedFolder, false /* aSetError */);
3065 if (SUCCEEDED(rc))
3066 return setError(VBOX_E_FILE_ERROR,
3067 tr("Shared folder named '%s' already exists"),
3068 strName.c_str());
3069
3070 pSharedFolder.createObject();
3071 rc = pSharedFolder->init(this,
3072 strName,
3073 strHostPath,
3074 !!aWritable,
3075 !!aAutoMount,
3076 true /* fFailOnError */);
3077 if (FAILED(rc)) return rc;
3078
3079 /* If the VM is online and supports shared folders, share this folder
3080 * under the specified name. (Ignore any failure to obtain the VM handle.) */
3081 SafeVMPtrQuiet ptrVM(this);
3082 if ( ptrVM.isOk()
3083 && m_pVMMDev
3084 && m_pVMMDev->isShFlActive()
3085 )
3086 {
3087 /* first, remove the machine or the global folder if there is any */
3088 SharedFolderDataMap::const_iterator it;
3089 if (findOtherSharedFolder(aName, it))
3090 {
3091 rc = removeSharedFolder(aName);
3092 if (FAILED(rc))
3093 return rc;
3094 }
3095
3096 /* second, create the given folder */
3097 rc = createSharedFolder(aName, SharedFolderData(aHostPath, !!aWritable, !!aAutoMount));
3098 if (FAILED(rc))
3099 return rc;
3100 }
3101
3102 m_mapSharedFolders.insert(std::make_pair(aName, pSharedFolder));
3103
3104 /* Notify console callbacks after the folder is added to the list. */
3105 alock.release();
3106 fireSharedFolderChangedEvent(mEventSource, Scope_Session);
3107
3108 LogFlowThisFunc(("Leaving for '%ls' -> '%ls'\n", aName, aHostPath));
3109
3110 return rc;
3111}
3112
3113STDMETHODIMP Console::RemoveSharedFolder(IN_BSTR aName)
3114{
3115 CheckComArgStrNotEmptyOrNull(aName);
3116
3117 AutoCaller autoCaller(this);
3118 if (FAILED(autoCaller.rc())) return autoCaller.rc();
3119
3120 LogFlowThisFunc(("Entering for '%ls'\n", aName));
3121
3122 Utf8Str strName(aName);
3123
3124 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
3125
3126 /// @todo see @todo in AttachUSBDevice() about the Paused state
3127 if (mMachineState == MachineState_Saved)
3128 return setError(VBOX_E_INVALID_VM_STATE,
3129 tr("Cannot remove a transient shared folder from the machine in the saved state"));
3130 if ( mMachineState != MachineState_PoweredOff
3131 && mMachineState != MachineState_Teleported
3132 && mMachineState != MachineState_Aborted
3133 && mMachineState != MachineState_Running
3134 && mMachineState != MachineState_Paused
3135 )
3136 return setError(VBOX_E_INVALID_VM_STATE,
3137 tr("Cannot remove a transient shared folder from the machine while it is changing the state (machine state: %s)"),
3138 Global::stringifyMachineState(mMachineState));
3139
3140 ComObjPtr<SharedFolder> pSharedFolder;
3141 HRESULT rc = findSharedFolder(aName, pSharedFolder, true /* aSetError */);
3142 if (FAILED(rc)) return rc;
3143
3144 /* protect the VM handle (if not NULL) */
3145 SafeVMPtrQuiet ptrVM(this);
3146 if ( ptrVM.isOk()
3147 && m_pVMMDev
3148 && m_pVMMDev->isShFlActive()
3149 )
3150 {
3151 /* if the VM is online and supports shared folders, UNshare this
3152 * folder. */
3153
3154 /* first, remove the given folder */
3155 rc = removeSharedFolder(strName);
3156 if (FAILED(rc)) return rc;
3157
3158 /* first, remove the machine or the global folder if there is any */
3159 SharedFolderDataMap::const_iterator it;
3160 if (findOtherSharedFolder(strName, it))
3161 {
3162 rc = createSharedFolder(strName, it->second);
3163 /* don't check rc here because we need to remove the console
3164 * folder from the collection even on failure */
3165 }
3166 }
3167
3168 m_mapSharedFolders.erase(strName);
3169
3170 /* Notify console callbacks after the folder is removed from the list. */
3171 alock.release();
3172 fireSharedFolderChangedEvent(mEventSource, Scope_Session);
3173
3174 LogFlowThisFunc(("Leaving for '%ls'\n", aName));
3175
3176 return rc;
3177}
3178
3179STDMETHODIMP Console::TakeSnapshot(IN_BSTR aName,
3180 IN_BSTR aDescription,
3181 IProgress **aProgress)
3182{
3183 LogFlowThisFuncEnter();
3184
3185 CheckComArgStrNotEmptyOrNull(aName);
3186 CheckComArgOutPointerValid(aProgress);
3187
3188 AutoCaller autoCaller(this);
3189 if (FAILED(autoCaller.rc())) return autoCaller.rc();
3190
3191 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
3192 LogFlowThisFunc(("aName='%ls' mMachineState=%d\n", aName, mMachineState));
3193
3194 if (Global::IsTransient(mMachineState))
3195 return setError(VBOX_E_INVALID_VM_STATE,
3196 tr("Cannot take a snapshot of the machine while it is changing the state (machine state: %s)"),
3197 Global::stringifyMachineState(mMachineState));
3198
3199 HRESULT rc = S_OK;
3200
3201 /* prepare the progress object:
3202 a) count the no. of hard disk attachments to get a matching no. of progress sub-operations */
3203 ULONG cOperations = 2; // always at least setting up + finishing up
3204 ULONG ulTotalOperationsWeight = 2; // one each for setting up + finishing up
3205 SafeIfaceArray<IMediumAttachment> aMediumAttachments;
3206 rc = mMachine->COMGETTER(MediumAttachments)(ComSafeArrayAsOutParam(aMediumAttachments));
3207 if (FAILED(rc))
3208 return setError(rc, tr("Cannot get medium attachments of the machine"));
3209
3210 ULONG ulMemSize;
3211 rc = mMachine->COMGETTER(MemorySize)(&ulMemSize);
3212 if (FAILED(rc))
3213 return rc;
3214
3215 for (size_t i = 0;
3216 i < aMediumAttachments.size();
3217 ++i)
3218 {
3219 DeviceType_T type;
3220 rc = aMediumAttachments[i]->COMGETTER(Type)(&type);
3221 if (FAILED(rc))
3222 return rc;
3223
3224 if (type == DeviceType_HardDisk)
3225 {
3226 ++cOperations;
3227
3228 // assume that creating a diff image takes as long as saving a 1MB state
3229 // (note, the same value must be used in SessionMachine::BeginTakingSnapshot() on the server!)
3230 ulTotalOperationsWeight += 1;
3231 }
3232 }
3233
3234 // b) one extra sub-operations for online snapshots OR offline snapshots that have a saved state (needs to be copied)
3235 bool const fTakingSnapshotOnline = Global::IsOnline(mMachineState);
3236
3237 LogFlowFunc(("fTakingSnapshotOnline = %d, mMachineState = %d\n", fTakingSnapshotOnline, mMachineState));
3238
3239 if (fTakingSnapshotOnline)
3240 {
3241 ++cOperations;
3242 ulTotalOperationsWeight += ulMemSize;
3243 }
3244
3245 // finally, create the progress object
3246 ComObjPtr<Progress> pProgress;
3247 pProgress.createObject();
3248 rc = pProgress->init(static_cast<IConsole *>(this),
3249 Bstr(tr("Taking a snapshot of the virtual machine")).raw(),
3250 (mMachineState >= MachineState_FirstOnline)
3251 && (mMachineState <= MachineState_LastOnline) /* aCancelable */,
3252 cOperations,
3253 ulTotalOperationsWeight,
3254 Bstr(tr("Setting up snapshot operation")).raw(), // first sub-op description
3255 1); // ulFirstOperationWeight
3256
3257 if (FAILED(rc))
3258 return rc;
3259
3260 VMTakeSnapshotTask *pTask;
3261 if (!(pTask = new VMTakeSnapshotTask(this, pProgress, aName, aDescription)))
3262 return E_OUTOFMEMORY;
3263
3264 Assert(pTask->mProgress);
3265
3266 try
3267 {
3268 mptrCancelableProgress = pProgress;
3269
3270 /*
3271 * If we fail here it means a PowerDown() call happened on another
3272 * thread while we were doing Pause() (which releases the Console lock).
3273 * We assign PowerDown() a higher precedence than TakeSnapshot(),
3274 * therefore just return the error to the caller.
3275 */
3276 rc = pTask->rc();
3277 if (FAILED(rc)) throw rc;
3278
3279 pTask->ulMemSize = ulMemSize;
3280
3281 /* memorize the current machine state */
3282 pTask->lastMachineState = mMachineState;
3283 pTask->fTakingSnapshotOnline = fTakingSnapshotOnline;
3284
3285 int vrc = RTThreadCreate(NULL,
3286 Console::fntTakeSnapshotWorker,
3287 (void *)pTask,
3288 0,
3289 RTTHREADTYPE_MAIN_WORKER,
3290 0,
3291 "TakeSnap");
3292 if (FAILED(vrc))
3293 throw setError(E_FAIL,
3294 tr("Could not create VMTakeSnap thread (%Rrc)"),
3295 vrc);
3296
3297 pTask->mProgress.queryInterfaceTo(aProgress);
3298 }
3299 catch (HRESULT erc)
3300 {
3301 delete pTask;
3302 rc = erc;
3303 mptrCancelableProgress.setNull();
3304 }
3305
3306 LogFlowThisFunc(("rc=%Rhrc\n", rc));
3307 LogFlowThisFuncLeave();
3308 return rc;
3309}
3310
3311STDMETHODIMP Console::DeleteSnapshot(IN_BSTR aId, IProgress **aProgress)
3312{
3313 CheckComArgExpr(aId, Guid(aId).isValid());
3314 CheckComArgOutPointerValid(aProgress);
3315
3316 AutoCaller autoCaller(this);
3317 if (FAILED(autoCaller.rc())) return autoCaller.rc();
3318
3319 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
3320
3321 if (Global::IsTransient(mMachineState))
3322 return setError(VBOX_E_INVALID_VM_STATE,
3323 tr("Cannot delete a snapshot of the machine while it is changing the state (machine state: %s)"),
3324 Global::stringifyMachineState(mMachineState));
3325
3326 MachineState_T machineState = MachineState_Null;
3327 HRESULT rc = mControl->DeleteSnapshot(this, aId, aId, FALSE /* fDeleteAllChildren */, &machineState, aProgress);
3328 if (FAILED(rc)) return rc;
3329
3330 setMachineStateLocally(machineState);
3331 return S_OK;
3332}
3333
3334STDMETHODIMP Console::DeleteSnapshotAndAllChildren(IN_BSTR aId, IProgress **aProgress)
3335{
3336 CheckComArgExpr(aId, Guid(aId).isValid());
3337 CheckComArgOutPointerValid(aProgress);
3338
3339 AutoCaller autoCaller(this);
3340 if (FAILED(autoCaller.rc())) return autoCaller.rc();
3341
3342 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
3343
3344 if (Global::IsTransient(mMachineState))
3345 return setError(VBOX_E_INVALID_VM_STATE,
3346 tr("Cannot delete a snapshot of the machine while it is changing the state (machine state: %s)"),
3347 Global::stringifyMachineState(mMachineState));
3348
3349 MachineState_T machineState = MachineState_Null;
3350 HRESULT rc = mControl->DeleteSnapshot(this, aId, aId, TRUE /* fDeleteAllChildren */, &machineState, aProgress);
3351 if (FAILED(rc)) return rc;
3352
3353 setMachineStateLocally(machineState);
3354 return S_OK;
3355}
3356
3357STDMETHODIMP Console::DeleteSnapshotRange(IN_BSTR aStartId, IN_BSTR aEndId, IProgress **aProgress)
3358{
3359 CheckComArgExpr(aStartId, Guid(aStartId).isValid());
3360 CheckComArgExpr(aEndId, Guid(aEndId).isValid());
3361 CheckComArgOutPointerValid(aProgress);
3362
3363 AutoCaller autoCaller(this);
3364 if (FAILED(autoCaller.rc())) return autoCaller.rc();
3365
3366 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
3367
3368 if (Global::IsTransient(mMachineState))
3369 return setError(VBOX_E_INVALID_VM_STATE,
3370 tr("Cannot delete a snapshot of the machine while it is changing the state (machine state: %s)"),
3371 Global::stringifyMachineState(mMachineState));
3372
3373 MachineState_T machineState = MachineState_Null;
3374 HRESULT rc = mControl->DeleteSnapshot(this, aStartId, aEndId, FALSE /* fDeleteAllChildren */, &machineState, aProgress);
3375 if (FAILED(rc)) return rc;
3376
3377 setMachineStateLocally(machineState);
3378 return S_OK;
3379}
3380
3381STDMETHODIMP Console::RestoreSnapshot(ISnapshot *aSnapshot, IProgress **aProgress)
3382{
3383 AutoCaller autoCaller(this);
3384 if (FAILED(autoCaller.rc())) return autoCaller.rc();
3385
3386 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
3387
3388 if (Global::IsOnlineOrTransient(mMachineState))
3389 return setError(VBOX_E_INVALID_VM_STATE,
3390 tr("Cannot delete the current state of the running machine (machine state: %s)"),
3391 Global::stringifyMachineState(mMachineState));
3392
3393 MachineState_T machineState = MachineState_Null;
3394 HRESULT rc = mControl->RestoreSnapshot(this, aSnapshot, &machineState, aProgress);
3395 if (FAILED(rc)) return rc;
3396
3397 setMachineStateLocally(machineState);
3398 return S_OK;
3399}
3400
3401// Non-interface public methods
3402/////////////////////////////////////////////////////////////////////////////
3403
3404/*static*/
3405HRESULT Console::setErrorStatic(HRESULT aResultCode, const char *pcsz, ...)
3406{
3407 va_list args;
3408 va_start(args, pcsz);
3409 HRESULT rc = setErrorInternal(aResultCode,
3410 getStaticClassIID(),
3411 getStaticComponentName(),
3412 Utf8Str(pcsz, args),
3413 false /* aWarning */,
3414 true /* aLogIt */);
3415 va_end(args);
3416 return rc;
3417}
3418
3419HRESULT Console::setInvalidMachineStateError()
3420{
3421 return setError(VBOX_E_INVALID_VM_STATE,
3422 tr("Invalid machine state: %s"),
3423 Global::stringifyMachineState(mMachineState));
3424}
3425
3426
3427/* static */
3428const char *Console::convertControllerTypeToDev(StorageControllerType_T enmCtrlType)
3429{
3430 switch (enmCtrlType)
3431 {
3432 case StorageControllerType_LsiLogic:
3433 return "lsilogicscsi";
3434 case StorageControllerType_BusLogic:
3435 return "buslogic";
3436 case StorageControllerType_LsiLogicSas:
3437 return "lsilogicsas";
3438 case StorageControllerType_IntelAhci:
3439 return "ahci";
3440 case StorageControllerType_PIIX3:
3441 case StorageControllerType_PIIX4:
3442 case StorageControllerType_ICH6:
3443 return "piix3ide";
3444 case StorageControllerType_I82078:
3445 return "i82078";
3446 default:
3447 return NULL;
3448 }
3449}
3450
3451HRESULT Console::convertBusPortDeviceToLun(StorageBus_T enmBus, LONG port, LONG device, unsigned &uLun)
3452{
3453 switch (enmBus)
3454 {
3455 case StorageBus_IDE:
3456 case StorageBus_Floppy:
3457 {
3458 AssertMsgReturn(port < 2 && port >= 0, ("%d\n", port), E_INVALIDARG);
3459 AssertMsgReturn(device < 2 && device >= 0, ("%d\n", device), E_INVALIDARG);
3460 uLun = 2 * port + device;
3461 return S_OK;
3462 }
3463 case StorageBus_SATA:
3464 case StorageBus_SCSI:
3465 case StorageBus_SAS:
3466 {
3467 uLun = port;
3468 return S_OK;
3469 }
3470 default:
3471 uLun = 0;
3472 AssertMsgFailedReturn(("%d\n", enmBus), E_INVALIDARG);
3473 }
3474}
3475
3476// private methods
3477/////////////////////////////////////////////////////////////////////////////
3478
3479/**
3480 * Process a medium change.
3481 *
3482 * @param aMediumAttachment The medium attachment with the new medium state.
3483 * @param fForce Force medium chance, if it is locked or not.
3484 * @param pUVM Safe VM handle.
3485 *
3486 * @note Locks this object for writing.
3487 */
3488HRESULT Console::doMediumChange(IMediumAttachment *aMediumAttachment, bool fForce, PUVM pUVM)
3489{
3490 AutoCaller autoCaller(this);
3491 AssertComRCReturnRC(autoCaller.rc());
3492
3493 /* We will need to release the write lock before calling EMT */
3494 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
3495
3496 HRESULT rc = S_OK;
3497 const char *pszDevice = NULL;
3498
3499 SafeIfaceArray<IStorageController> ctrls;
3500 rc = mMachine->COMGETTER(StorageControllers)(ComSafeArrayAsOutParam(ctrls));
3501 AssertComRC(rc);
3502 IMedium *pMedium;
3503 rc = aMediumAttachment->COMGETTER(Medium)(&pMedium);
3504 AssertComRC(rc);
3505 Bstr mediumLocation;
3506 if (pMedium)
3507 {
3508 rc = pMedium->COMGETTER(Location)(mediumLocation.asOutParam());
3509 AssertComRC(rc);
3510 }
3511
3512 Bstr attCtrlName;
3513 rc = aMediumAttachment->COMGETTER(Controller)(attCtrlName.asOutParam());
3514 AssertComRC(rc);
3515 ComPtr<IStorageController> pStorageController;
3516 for (size_t i = 0; i < ctrls.size(); ++i)
3517 {
3518 Bstr ctrlName;
3519 rc = ctrls[i]->COMGETTER(Name)(ctrlName.asOutParam());
3520 AssertComRC(rc);
3521 if (attCtrlName == ctrlName)
3522 {
3523 pStorageController = ctrls[i];
3524 break;
3525 }
3526 }
3527 if (pStorageController.isNull())
3528 return setError(E_FAIL,
3529 tr("Could not find storage controller '%ls'"), attCtrlName.raw());
3530
3531 StorageControllerType_T enmCtrlType;
3532 rc = pStorageController->COMGETTER(ControllerType)(&enmCtrlType);
3533 AssertComRC(rc);
3534 pszDevice = convertControllerTypeToDev(enmCtrlType);
3535
3536 StorageBus_T enmBus;
3537 rc = pStorageController->COMGETTER(Bus)(&enmBus);
3538 AssertComRC(rc);
3539 ULONG uInstance;
3540 rc = pStorageController->COMGETTER(Instance)(&uInstance);
3541 AssertComRC(rc);
3542 BOOL fUseHostIOCache;
3543 rc = pStorageController->COMGETTER(UseHostIOCache)(&fUseHostIOCache);
3544 AssertComRC(rc);
3545
3546 /*
3547 * Call worker in EMT, that's faster and safer than doing everything
3548 * using VMR3ReqCall. Note that we separate VMR3ReqCall from VMR3ReqWait
3549 * here to make requests from under the lock in order to serialize them.
3550 */
3551 PVMREQ pReq;
3552 int vrc = VMR3ReqCallU(pUVM,
3553 VMCPUID_ANY,
3554 &pReq,
3555 0 /* no wait! */,
3556 VMREQFLAGS_VBOX_STATUS,
3557 (PFNRT)Console::changeRemovableMedium,
3558 8,
3559 this,
3560 pUVM,
3561 pszDevice,
3562 uInstance,
3563 enmBus,
3564 fUseHostIOCache,
3565 aMediumAttachment,
3566 fForce);
3567
3568 /* release the lock before waiting for a result (EMT will call us back!) */
3569 alock.release();
3570
3571 if (vrc == VERR_TIMEOUT || RT_SUCCESS(vrc))
3572 {
3573 vrc = VMR3ReqWait(pReq, RT_INDEFINITE_WAIT);
3574 AssertRC(vrc);
3575 if (RT_SUCCESS(vrc))
3576 vrc = pReq->iStatus;
3577 }
3578 VMR3ReqFree(pReq);
3579
3580 if (RT_SUCCESS(vrc))
3581 {
3582 LogFlowThisFunc(("Returns S_OK\n"));
3583 return S_OK;
3584 }
3585
3586 if (pMedium)
3587 return setError(E_FAIL,
3588 tr("Could not mount the media/drive '%ls' (%Rrc)"),
3589 mediumLocation.raw(), vrc);
3590
3591 return setError(E_FAIL,
3592 tr("Could not unmount the currently mounted media/drive (%Rrc)"),
3593 vrc);
3594}
3595
3596/**
3597 * Performs the medium change in EMT.
3598 *
3599 * @returns VBox status code.
3600 *
3601 * @param pThis Pointer to the Console object.
3602 * @param pUVM The VM handle.
3603 * @param pcszDevice The PDM device name.
3604 * @param uInstance The PDM device instance.
3605 * @param uLun The PDM LUN number of the drive.
3606 * @param fHostDrive True if this is a host drive attachment.
3607 * @param pszPath The path to the media / drive which is now being mounted / captured.
3608 * If NULL no media or drive is attached and the LUN will be configured with
3609 * the default block driver with no media. This will also be the state if
3610 * mounting / capturing the specified media / drive fails.
3611 * @param pszFormat Medium format string, usually "RAW".
3612 * @param fPassthrough Enables using passthrough mode of the host DVD drive if applicable.
3613 *
3614 * @thread EMT
3615 */
3616DECLCALLBACK(int) Console::changeRemovableMedium(Console *pConsole,
3617 PUVM pUVM,
3618 const char *pcszDevice,
3619 unsigned uInstance,
3620 StorageBus_T enmBus,
3621 bool fUseHostIOCache,
3622 IMediumAttachment *aMediumAtt,
3623 bool fForce)
3624{
3625 LogFlowFunc(("pConsole=%p uInstance=%u pszDevice=%p:{%s} enmBus=%u, aMediumAtt=%p, fForce=%d\n",
3626 pConsole, uInstance, pcszDevice, pcszDevice, enmBus, aMediumAtt, fForce));
3627
3628 AssertReturn(pConsole, VERR_INVALID_PARAMETER);
3629
3630 AutoCaller autoCaller(pConsole);
3631 AssertComRCReturn(autoCaller.rc(), VERR_ACCESS_DENIED);
3632
3633 /*
3634 * Suspend the VM first.
3635 *
3636 * The VM must not be running since it might have pending I/O to
3637 * the drive which is being changed.
3638 */
3639 bool fResume;
3640 VMSTATE enmVMState = VMR3GetStateU(pUVM);
3641 switch (enmVMState)
3642 {
3643 case VMSTATE_RESETTING:
3644 case VMSTATE_RUNNING:
3645 {
3646 LogFlowFunc(("Suspending the VM...\n"));
3647 /* disable the callback to prevent Console-level state change */
3648 pConsole->mVMStateChangeCallbackDisabled = true;
3649 int rc = VMR3Suspend(pUVM, VMSUSPENDREASON_RECONFIG);
3650 pConsole->mVMStateChangeCallbackDisabled = false;
3651 AssertRCReturn(rc, rc);
3652 fResume = true;
3653 break;
3654 }
3655
3656 case VMSTATE_SUSPENDED:
3657 case VMSTATE_CREATED:
3658 case VMSTATE_OFF:
3659 fResume = false;
3660 break;
3661
3662 case VMSTATE_RUNNING_LS:
3663 case VMSTATE_RUNNING_FT:
3664 return setErrorInternal(VBOX_E_INVALID_VM_STATE,
3665 COM_IIDOF(IConsole),
3666 getStaticComponentName(),
3667 (enmVMState == VMSTATE_RUNNING_LS) ? Utf8Str(tr("Cannot change drive during live migration")) : Utf8Str(tr("Cannot change drive during fault tolerant syncing")),
3668 false /*aWarning*/,
3669 true /*aLogIt*/);
3670
3671 default:
3672 AssertMsgFailedReturn(("enmVMState=%d\n", enmVMState), VERR_ACCESS_DENIED);
3673 }
3674
3675 /* Determine the base path for the device instance. */
3676 PCFGMNODE pCtlInst;
3677 pCtlInst = CFGMR3GetChildF(CFGMR3GetRootU(pUVM), "Devices/%s/%u/", pcszDevice, uInstance);
3678 AssertReturn(pCtlInst, VERR_INTERNAL_ERROR);
3679
3680 int rc = VINF_SUCCESS;
3681 int rcRet = VINF_SUCCESS;
3682
3683 rcRet = pConsole->configMediumAttachment(pCtlInst,
3684 pcszDevice,
3685 uInstance,
3686 enmBus,
3687 fUseHostIOCache,
3688 false /* fSetupMerge */,
3689 false /* fBuiltinIOCache */,
3690 0 /* uMergeSource */,
3691 0 /* uMergeTarget */,
3692 aMediumAtt,
3693 pConsole->mMachineState,
3694 NULL /* phrc */,
3695 true /* fAttachDetach */,
3696 fForce /* fForceUnmount */,
3697 false /* fHotplug */,
3698 pUVM,
3699 NULL /* paLedDevType */);
3700 /** @todo this dumps everything attached to this device instance, which
3701 * is more than necessary. Dumping the changed LUN would be enough. */
3702 CFGMR3Dump(pCtlInst);
3703
3704 /*
3705 * Resume the VM if necessary.
3706 */
3707 if (fResume)
3708 {
3709 LogFlowFunc(("Resuming the VM...\n"));
3710 /* disable the callback to prevent Console-level state change */
3711 pConsole->mVMStateChangeCallbackDisabled = true;
3712 rc = VMR3Resume(pUVM, VMRESUMEREASON_RECONFIG);
3713 pConsole->mVMStateChangeCallbackDisabled = false;
3714 AssertRC(rc);
3715 if (RT_FAILURE(rc))
3716 {
3717 /* too bad, we failed. try to sync the console state with the VMM state */
3718 vmstateChangeCallback(pUVM, VMSTATE_SUSPENDED, enmVMState, pConsole);
3719 }
3720 /// @todo (r=dmik) if we failed with drive mount, then the VMR3Resume
3721 // error (if any) will be hidden from the caller. For proper reporting
3722 // of such multiple errors to the caller we need to enhance the
3723 // IVirtualBoxError interface. For now, give the first error the higher
3724 // priority.
3725 if (RT_SUCCESS(rcRet))
3726 rcRet = rc;
3727 }
3728
3729 LogFlowFunc(("Returning %Rrc\n", rcRet));
3730 return rcRet;
3731}
3732
3733
3734/**
3735 * Attach a new storage device to the VM.
3736 *
3737 * @param aMediumAttachment The medium attachment which is added.
3738 * @param pUVM Safe VM handle.
3739 * @param fSilent Flag whether to notify the guest about the attached device.
3740 *
3741 * @note Locks this object for writing.
3742 */
3743HRESULT Console::doStorageDeviceAttach(IMediumAttachment *aMediumAttachment, PUVM pUVM, bool fSilent)
3744{
3745 AutoCaller autoCaller(this);
3746 AssertComRCReturnRC(autoCaller.rc());
3747
3748 /* We will need to release the write lock before calling EMT */
3749 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
3750
3751 HRESULT rc = S_OK;
3752 const char *pszDevice = NULL;
3753
3754 SafeIfaceArray<IStorageController> ctrls;
3755 rc = mMachine->COMGETTER(StorageControllers)(ComSafeArrayAsOutParam(ctrls));
3756 AssertComRC(rc);
3757 IMedium *pMedium;
3758 rc = aMediumAttachment->COMGETTER(Medium)(&pMedium);
3759 AssertComRC(rc);
3760 Bstr mediumLocation;
3761 if (pMedium)
3762 {
3763 rc = pMedium->COMGETTER(Location)(mediumLocation.asOutParam());
3764 AssertComRC(rc);
3765 }
3766
3767 Bstr attCtrlName;
3768 rc = aMediumAttachment->COMGETTER(Controller)(attCtrlName.asOutParam());
3769 AssertComRC(rc);
3770 ComPtr<IStorageController> pStorageController;
3771 for (size_t i = 0; i < ctrls.size(); ++i)
3772 {
3773 Bstr ctrlName;
3774 rc = ctrls[i]->COMGETTER(Name)(ctrlName.asOutParam());
3775 AssertComRC(rc);
3776 if (attCtrlName == ctrlName)
3777 {
3778 pStorageController = ctrls[i];
3779 break;
3780 }
3781 }
3782 if (pStorageController.isNull())
3783 return setError(E_FAIL,
3784 tr("Could not find storage controller '%ls'"), attCtrlName.raw());
3785
3786 StorageControllerType_T enmCtrlType;
3787 rc = pStorageController->COMGETTER(ControllerType)(&enmCtrlType);
3788 AssertComRC(rc);
3789 pszDevice = convertControllerTypeToDev(enmCtrlType);
3790
3791 StorageBus_T enmBus;
3792 rc = pStorageController->COMGETTER(Bus)(&enmBus);
3793 AssertComRC(rc);
3794 ULONG uInstance;
3795 rc = pStorageController->COMGETTER(Instance)(&uInstance);
3796 AssertComRC(rc);
3797 BOOL fUseHostIOCache;
3798 rc = pStorageController->COMGETTER(UseHostIOCache)(&fUseHostIOCache);
3799 AssertComRC(rc);
3800
3801 /*
3802 * Call worker in EMT, that's faster and safer than doing everything
3803 * using VMR3ReqCall. Note that we separate VMR3ReqCall from VMR3ReqWait
3804 * here to make requests from under the lock in order to serialize them.
3805 */
3806 PVMREQ pReq;
3807 int vrc = VMR3ReqCallU(pUVM,
3808 VMCPUID_ANY,
3809 &pReq,
3810 0 /* no wait! */,
3811 VMREQFLAGS_VBOX_STATUS,
3812 (PFNRT)Console::attachStorageDevice,
3813 8,
3814 this,
3815 pUVM,
3816 pszDevice,
3817 uInstance,
3818 enmBus,
3819 fUseHostIOCache,
3820 aMediumAttachment,
3821 fSilent);
3822
3823 /* release the lock before waiting for a result (EMT will call us back!) */
3824 alock.release();
3825
3826 if (vrc == VERR_TIMEOUT || RT_SUCCESS(vrc))
3827 {
3828 vrc = VMR3ReqWait(pReq, RT_INDEFINITE_WAIT);
3829 AssertRC(vrc);
3830 if (RT_SUCCESS(vrc))
3831 vrc = pReq->iStatus;
3832 }
3833 VMR3ReqFree(pReq);
3834
3835 if (RT_SUCCESS(vrc))
3836 {
3837 LogFlowThisFunc(("Returns S_OK\n"));
3838 return S_OK;
3839 }
3840
3841 if (!pMedium)
3842 return setError(E_FAIL,
3843 tr("Could not mount the media/drive '%ls' (%Rrc)"),
3844 mediumLocation.raw(), vrc);
3845
3846 return setError(E_FAIL,
3847 tr("Could not unmount the currently mounted media/drive (%Rrc)"),
3848 vrc);
3849}
3850
3851
3852/**
3853 * Performs the storage attach operation in EMT.
3854 *
3855 * @returns VBox status code.
3856 *
3857 * @param pThis Pointer to the Console object.
3858 * @param pUVM The VM handle.
3859 * @param pcszDevice The PDM device name.
3860 * @param uInstance The PDM device instance.
3861 * @param fSilent Flag whether to inform the guest about the attached device.
3862 *
3863 * @thread EMT
3864 */
3865DECLCALLBACK(int) Console::attachStorageDevice(Console *pConsole,
3866 PUVM pUVM,
3867 const char *pcszDevice,
3868 unsigned uInstance,
3869 StorageBus_T enmBus,
3870 bool fUseHostIOCache,
3871 IMediumAttachment *aMediumAtt,
3872 bool fSilent)
3873{
3874 LogFlowFunc(("pConsole=%p uInstance=%u pszDevice=%p:{%s} enmBus=%u, aMediumAtt=%p\n",
3875 pConsole, uInstance, pcszDevice, pcszDevice, enmBus, aMediumAtt));
3876
3877 AssertReturn(pConsole, VERR_INVALID_PARAMETER);
3878
3879 AutoCaller autoCaller(pConsole);
3880 AssertComRCReturn(autoCaller.rc(), VERR_ACCESS_DENIED);
3881
3882 /*
3883 * Suspend the VM first.
3884 *
3885 * The VM must not be running since it might have pending I/O to
3886 * the drive which is being changed.
3887 */
3888 bool fResume;
3889 VMSTATE enmVMState = VMR3GetStateU(pUVM);
3890 switch (enmVMState)
3891 {
3892 case VMSTATE_RESETTING:
3893 case VMSTATE_RUNNING:
3894 {
3895 LogFlowFunc(("Suspending the VM...\n"));
3896 /* disable the callback to prevent Console-level state change */
3897 pConsole->mVMStateChangeCallbackDisabled = true;
3898 int rc = VMR3Suspend(pUVM, VMSUSPENDREASON_RECONFIG);
3899 pConsole->mVMStateChangeCallbackDisabled = false;
3900 AssertRCReturn(rc, rc);
3901 fResume = true;
3902 break;
3903 }
3904
3905 case VMSTATE_SUSPENDED:
3906 case VMSTATE_CREATED:
3907 case VMSTATE_OFF:
3908 fResume = false;
3909 break;
3910
3911 case VMSTATE_RUNNING_LS:
3912 case VMSTATE_RUNNING_FT:
3913 return setErrorInternal(VBOX_E_INVALID_VM_STATE,
3914 COM_IIDOF(IConsole),
3915 getStaticComponentName(),
3916 (enmVMState == VMSTATE_RUNNING_LS) ? Utf8Str(tr("Cannot change drive during live migration")) : Utf8Str(tr("Cannot change drive during fault tolerant syncing")),
3917 false /*aWarning*/,
3918 true /*aLogIt*/);
3919
3920 default:
3921 AssertMsgFailedReturn(("enmVMState=%d\n", enmVMState), VERR_ACCESS_DENIED);
3922 }
3923
3924 /* Determine the base path for the device instance. */
3925 PCFGMNODE pCtlInst;
3926 pCtlInst = CFGMR3GetChildF(CFGMR3GetRootU(pUVM), "Devices/%s/%u/", pcszDevice, uInstance);
3927 AssertReturn(pCtlInst, VERR_INTERNAL_ERROR);
3928
3929 int rc = VINF_SUCCESS;
3930 int rcRet = VINF_SUCCESS;
3931
3932 rcRet = pConsole->configMediumAttachment(pCtlInst,
3933 pcszDevice,
3934 uInstance,
3935 enmBus,
3936 fUseHostIOCache,
3937 false /* fSetupMerge */,
3938 false /* fBuiltinIOCache */,
3939 0 /* uMergeSource */,
3940 0 /* uMergeTarget */,
3941 aMediumAtt,
3942 pConsole->mMachineState,
3943 NULL /* phrc */,
3944 true /* fAttachDetach */,
3945 false /* fForceUnmount */,
3946 !fSilent /* fHotplug */,
3947 pUVM,
3948 NULL /* paLedDevType */);
3949 /** @todo this dumps everything attached to this device instance, which
3950 * is more than necessary. Dumping the changed LUN would be enough. */
3951 CFGMR3Dump(pCtlInst);
3952
3953 /*
3954 * Resume the VM if necessary.
3955 */
3956 if (fResume)
3957 {
3958 LogFlowFunc(("Resuming the VM...\n"));
3959 /* disable the callback to prevent Console-level state change */
3960 pConsole->mVMStateChangeCallbackDisabled = true;
3961 rc = VMR3Resume(pUVM, VMRESUMEREASON_RECONFIG);
3962 pConsole->mVMStateChangeCallbackDisabled = false;
3963 AssertRC(rc);
3964 if (RT_FAILURE(rc))
3965 {
3966 /* too bad, we failed. try to sync the console state with the VMM state */
3967 vmstateChangeCallback(pUVM, VMSTATE_SUSPENDED, enmVMState, pConsole);
3968 }
3969 /** @todo if we failed with drive mount, then the VMR3Resume
3970 * error (if any) will be hidden from the caller. For proper reporting
3971 * of such multiple errors to the caller we need to enhance the
3972 * IVirtualBoxError interface. For now, give the first error the higher
3973 * priority.
3974 */
3975 if (RT_SUCCESS(rcRet))
3976 rcRet = rc;
3977 }
3978
3979 LogFlowFunc(("Returning %Rrc\n", rcRet));
3980 return rcRet;
3981}
3982
3983/**
3984 * Attach a new storage device to the VM.
3985 *
3986 * @param aMediumAttachment The medium attachment which is added.
3987 * @param pUVM Safe VM handle.
3988 * @param fSilent Flag whether to notify the guest about the detached device.
3989 *
3990 * @note Locks this object for writing.
3991 */
3992HRESULT Console::doStorageDeviceDetach(IMediumAttachment *aMediumAttachment, PUVM pUVM, bool fSilent)
3993{
3994 AutoCaller autoCaller(this);
3995 AssertComRCReturnRC(autoCaller.rc());
3996
3997 /* We will need to release the write lock before calling EMT */
3998 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
3999
4000 HRESULT rc = S_OK;
4001 const char *pszDevice = NULL;
4002
4003 SafeIfaceArray<IStorageController> ctrls;
4004 rc = mMachine->COMGETTER(StorageControllers)(ComSafeArrayAsOutParam(ctrls));
4005 AssertComRC(rc);
4006 IMedium *pMedium;
4007 rc = aMediumAttachment->COMGETTER(Medium)(&pMedium);
4008 AssertComRC(rc);
4009 Bstr mediumLocation;
4010 if (pMedium)
4011 {
4012 rc = pMedium->COMGETTER(Location)(mediumLocation.asOutParam());
4013 AssertComRC(rc);
4014 }
4015
4016 Bstr attCtrlName;
4017 rc = aMediumAttachment->COMGETTER(Controller)(attCtrlName.asOutParam());
4018 AssertComRC(rc);
4019 ComPtr<IStorageController> pStorageController;
4020 for (size_t i = 0; i < ctrls.size(); ++i)
4021 {
4022 Bstr ctrlName;
4023 rc = ctrls[i]->COMGETTER(Name)(ctrlName.asOutParam());
4024 AssertComRC(rc);
4025 if (attCtrlName == ctrlName)
4026 {
4027 pStorageController = ctrls[i];
4028 break;
4029 }
4030 }
4031 if (pStorageController.isNull())
4032 return setError(E_FAIL,
4033 tr("Could not find storage controller '%ls'"), attCtrlName.raw());
4034
4035 StorageControllerType_T enmCtrlType;
4036 rc = pStorageController->COMGETTER(ControllerType)(&enmCtrlType);
4037 AssertComRC(rc);
4038 pszDevice = convertControllerTypeToDev(enmCtrlType);
4039
4040 StorageBus_T enmBus;
4041 rc = pStorageController->COMGETTER(Bus)(&enmBus);
4042 AssertComRC(rc);
4043 ULONG uInstance;
4044 rc = pStorageController->COMGETTER(Instance)(&uInstance);
4045 AssertComRC(rc);
4046
4047 /*
4048 * Call worker in EMT, that's faster and safer than doing everything
4049 * using VMR3ReqCall. Note that we separate VMR3ReqCall from VMR3ReqWait
4050 * here to make requests from under the lock in order to serialize them.
4051 */
4052 PVMREQ pReq;
4053 int vrc = VMR3ReqCallU(pUVM,
4054 VMCPUID_ANY,
4055 &pReq,
4056 0 /* no wait! */,
4057 VMREQFLAGS_VBOX_STATUS,
4058 (PFNRT)Console::detachStorageDevice,
4059 7,
4060 this,
4061 pUVM,
4062 pszDevice,
4063 uInstance,
4064 enmBus,
4065 aMediumAttachment,
4066 fSilent);
4067
4068 /* release the lock before waiting for a result (EMT will call us back!) */
4069 alock.release();
4070
4071 if (vrc == VERR_TIMEOUT || RT_SUCCESS(vrc))
4072 {
4073 vrc = VMR3ReqWait(pReq, RT_INDEFINITE_WAIT);
4074 AssertRC(vrc);
4075 if (RT_SUCCESS(vrc))
4076 vrc = pReq->iStatus;
4077 }
4078 VMR3ReqFree(pReq);
4079
4080 if (RT_SUCCESS(vrc))
4081 {
4082 LogFlowThisFunc(("Returns S_OK\n"));
4083 return S_OK;
4084 }
4085
4086 if (!pMedium)
4087 return setError(E_FAIL,
4088 tr("Could not mount the media/drive '%ls' (%Rrc)"),
4089 mediumLocation.raw(), vrc);
4090
4091 return setError(E_FAIL,
4092 tr("Could not unmount the currently mounted media/drive (%Rrc)"),
4093 vrc);
4094}
4095
4096/**
4097 * Performs the storage detach operation in EMT.
4098 *
4099 * @returns VBox status code.
4100 *
4101 * @param pThis Pointer to the Console object.
4102 * @param pUVM The VM handle.
4103 * @param pcszDevice The PDM device name.
4104 * @param uInstance The PDM device instance.
4105 * @param fSilent Flag whether to notify the guest about the detached device.
4106 *
4107 * @thread EMT
4108 */
4109DECLCALLBACK(int) Console::detachStorageDevice(Console *pConsole,
4110 PUVM pUVM,
4111 const char *pcszDevice,
4112 unsigned uInstance,
4113 StorageBus_T enmBus,
4114 IMediumAttachment *pMediumAtt,
4115 bool fSilent)
4116{
4117 LogFlowFunc(("pConsole=%p uInstance=%u pszDevice=%p:{%s} enmBus=%u, pMediumAtt=%p\n",
4118 pConsole, uInstance, pcszDevice, pcszDevice, enmBus, pMediumAtt));
4119
4120 AssertReturn(pConsole, VERR_INVALID_PARAMETER);
4121
4122 AutoCaller autoCaller(pConsole);
4123 AssertComRCReturn(autoCaller.rc(), VERR_ACCESS_DENIED);
4124
4125 /*
4126 * Suspend the VM first.
4127 *
4128 * The VM must not be running since it might have pending I/O to
4129 * the drive which is being changed.
4130 */
4131 bool fResume;
4132 VMSTATE enmVMState = VMR3GetStateU(pUVM);
4133 switch (enmVMState)
4134 {
4135 case VMSTATE_RESETTING:
4136 case VMSTATE_RUNNING:
4137 {
4138 LogFlowFunc(("Suspending the VM...\n"));
4139 /* disable the callback to prevent Console-level state change */
4140 pConsole->mVMStateChangeCallbackDisabled = true;
4141 int rc = VMR3Suspend(pUVM, VMSUSPENDREASON_RECONFIG);
4142 pConsole->mVMStateChangeCallbackDisabled = false;
4143 AssertRCReturn(rc, rc);
4144 fResume = true;
4145 break;
4146 }
4147
4148 case VMSTATE_SUSPENDED:
4149 case VMSTATE_CREATED:
4150 case VMSTATE_OFF:
4151 fResume = false;
4152 break;
4153
4154 case VMSTATE_RUNNING_LS:
4155 case VMSTATE_RUNNING_FT:
4156 return setErrorInternal(VBOX_E_INVALID_VM_STATE,
4157 COM_IIDOF(IConsole),
4158 getStaticComponentName(),
4159 (enmVMState == VMSTATE_RUNNING_LS) ? Utf8Str(tr("Cannot change drive during live migration")) : Utf8Str(tr("Cannot change drive during fault tolerant syncing")),
4160 false /*aWarning*/,
4161 true /*aLogIt*/);
4162
4163 default:
4164 AssertMsgFailedReturn(("enmVMState=%d\n", enmVMState), VERR_ACCESS_DENIED);
4165 }
4166
4167 /* Determine the base path for the device instance. */
4168 PCFGMNODE pCtlInst;
4169 pCtlInst = CFGMR3GetChildF(CFGMR3GetRootU(pUVM), "Devices/%s/%u/", pcszDevice, uInstance);
4170 AssertReturn(pCtlInst, VERR_INTERNAL_ERROR);
4171
4172#define H() AssertMsgReturn(!FAILED(hrc), ("hrc=%Rhrc\n", hrc), VERR_GENERAL_FAILURE)
4173
4174 HRESULT hrc;
4175 int rc = VINF_SUCCESS;
4176 int rcRet = VINF_SUCCESS;
4177 unsigned uLUN;
4178 LONG lDev;
4179 LONG lPort;
4180 DeviceType_T lType;
4181 PCFGMNODE pLunL0 = NULL;
4182 PCFGMNODE pCfg = NULL;
4183
4184 hrc = pMediumAtt->COMGETTER(Device)(&lDev); H();
4185 hrc = pMediumAtt->COMGETTER(Port)(&lPort); H();
4186 hrc = pMediumAtt->COMGETTER(Type)(&lType); H();
4187 hrc = Console::convertBusPortDeviceToLun(enmBus, lPort, lDev, uLUN); H();
4188
4189#undef H
4190
4191 /* First check if the LUN really exists. */
4192 pLunL0 = CFGMR3GetChildF(pCtlInst, "LUN#%u", uLUN);
4193 if (pLunL0)
4194 {
4195 uint32_t fFlags = 0;
4196
4197 if (fSilent)
4198 fFlags |= PDM_TACH_FLAGS_NOT_HOT_PLUG;
4199
4200 rc = PDMR3DeviceDetach(pUVM, pcszDevice, uInstance, uLUN, fFlags);
4201 if (rc == VERR_PDM_NO_DRIVER_ATTACHED_TO_LUN)
4202 rc = VINF_SUCCESS;
4203 AssertRCReturn(rc, rc);
4204 CFGMR3RemoveNode(pLunL0);
4205
4206 Utf8Str devicePath = Utf8StrFmt("%s/%u/LUN#%u", pcszDevice, uInstance, uLUN);
4207 pConsole->mapMediumAttachments.erase(devicePath);
4208
4209 }
4210 else
4211 AssertFailedReturn(VERR_INTERNAL_ERROR);
4212
4213 CFGMR3Dump(pCtlInst);
4214
4215 /*
4216 * Resume the VM if necessary.
4217 */
4218 if (fResume)
4219 {
4220 LogFlowFunc(("Resuming the VM...\n"));
4221 /* disable the callback to prevent Console-level state change */
4222 pConsole->mVMStateChangeCallbackDisabled = true;
4223 rc = VMR3Resume(pUVM, VMRESUMEREASON_RECONFIG);
4224 pConsole->mVMStateChangeCallbackDisabled = false;
4225 AssertRC(rc);
4226 if (RT_FAILURE(rc))
4227 {
4228 /* too bad, we failed. try to sync the console state with the VMM state */
4229 vmstateChangeCallback(pUVM, VMSTATE_SUSPENDED, enmVMState, pConsole);
4230 }
4231 /** @todo: if we failed with drive mount, then the VMR3Resume
4232 * error (if any) will be hidden from the caller. For proper reporting
4233 * of such multiple errors to the caller we need to enhance the
4234 * IVirtualBoxError interface. For now, give the first error the higher
4235 * priority.
4236 */
4237 if (RT_SUCCESS(rcRet))
4238 rcRet = rc;
4239 }
4240
4241 LogFlowFunc(("Returning %Rrc\n", rcRet));
4242 return rcRet;
4243}
4244
4245/**
4246 * Called by IInternalSessionControl::OnNetworkAdapterChange().
4247 *
4248 * @note Locks this object for writing.
4249 */
4250HRESULT Console::onNetworkAdapterChange(INetworkAdapter *aNetworkAdapter, BOOL changeAdapter)
4251{
4252 LogFlowThisFunc(("\n"));
4253
4254 AutoCaller autoCaller(this);
4255 AssertComRCReturnRC(autoCaller.rc());
4256
4257 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
4258
4259 HRESULT rc = S_OK;
4260
4261 /* don't trigger network changes if the VM isn't running */
4262 SafeVMPtrQuiet ptrVM(this);
4263 if (ptrVM.isOk())
4264 {
4265 /* Get the properties we need from the adapter */
4266 BOOL fCableConnected, fTraceEnabled;
4267 rc = aNetworkAdapter->COMGETTER(CableConnected)(&fCableConnected);
4268 AssertComRC(rc);
4269 if (SUCCEEDED(rc))
4270 {
4271 rc = aNetworkAdapter->COMGETTER(TraceEnabled)(&fTraceEnabled);
4272 AssertComRC(rc);
4273 }
4274 if (SUCCEEDED(rc))
4275 {
4276 ULONG ulInstance;
4277 rc = aNetworkAdapter->COMGETTER(Slot)(&ulInstance);
4278 AssertComRC(rc);
4279 if (SUCCEEDED(rc))
4280 {
4281 /*
4282 * Find the adapter instance, get the config interface and update
4283 * the link state.
4284 */
4285 NetworkAdapterType_T adapterType;
4286 rc = aNetworkAdapter->COMGETTER(AdapterType)(&adapterType);
4287 AssertComRC(rc);
4288 const char *pszAdapterName = networkAdapterTypeToName(adapterType);
4289
4290 // prevent cross-thread deadlocks, don't need the lock any more
4291 alock.release();
4292
4293 PPDMIBASE pBase;
4294 int vrc = PDMR3QueryDeviceLun(ptrVM.rawUVM(), pszAdapterName, ulInstance, 0, &pBase);
4295 if (RT_SUCCESS(vrc))
4296 {
4297 Assert(pBase);
4298 PPDMINETWORKCONFIG pINetCfg;
4299 pINetCfg = PDMIBASE_QUERY_INTERFACE(pBase, PDMINETWORKCONFIG);
4300 if (pINetCfg)
4301 {
4302 Log(("Console::onNetworkAdapterChange: setting link state to %d\n",
4303 fCableConnected));
4304 vrc = pINetCfg->pfnSetLinkState(pINetCfg,
4305 fCableConnected ? PDMNETWORKLINKSTATE_UP
4306 : PDMNETWORKLINKSTATE_DOWN);
4307 ComAssertRC(vrc);
4308 }
4309 if (RT_SUCCESS(vrc) && changeAdapter)
4310 {
4311 VMSTATE enmVMState = VMR3GetStateU(ptrVM.rawUVM());
4312 if ( enmVMState == VMSTATE_RUNNING /** @todo LiveMigration: Forbid or deal correctly with the _LS variants */
4313 || enmVMState == VMSTATE_SUSPENDED)
4314 {
4315 if (fTraceEnabled && fCableConnected && pINetCfg)
4316 {
4317 vrc = pINetCfg->pfnSetLinkState(pINetCfg, PDMNETWORKLINKSTATE_DOWN);
4318 ComAssertRC(vrc);
4319 }
4320
4321 rc = doNetworkAdapterChange(ptrVM.rawUVM(), pszAdapterName, ulInstance, 0, aNetworkAdapter);
4322
4323 if (fTraceEnabled && fCableConnected && pINetCfg)
4324 {
4325 vrc = pINetCfg->pfnSetLinkState(pINetCfg, PDMNETWORKLINKSTATE_UP);
4326 ComAssertRC(vrc);
4327 }
4328 }
4329 }
4330 }
4331 else if (vrc == VERR_PDM_DEVICE_INSTANCE_NOT_FOUND)
4332 return setError(E_FAIL,
4333 tr("The network adapter #%u is not enabled"), ulInstance);
4334 else
4335 ComAssertRC(vrc);
4336
4337 if (RT_FAILURE(vrc))
4338 rc = E_FAIL;
4339
4340 alock.acquire();
4341 }
4342 }
4343 ptrVM.release();
4344 }
4345
4346 // definitely don't need the lock any more
4347 alock.release();
4348
4349 /* notify console callbacks on success */
4350 if (SUCCEEDED(rc))
4351 fireNetworkAdapterChangedEvent(mEventSource, aNetworkAdapter);
4352
4353 LogFlowThisFunc(("Leaving rc=%#x\n", rc));
4354 return rc;
4355}
4356
4357/**
4358 * Called by IInternalSessionControl::OnNATEngineChange().
4359 *
4360 * @note Locks this object for writing.
4361 */
4362HRESULT Console::onNATRedirectRuleChange(ULONG ulInstance, BOOL aNatRuleRemove,
4363 NATProtocol_T aProto, IN_BSTR aHostIP, LONG aHostPort, IN_BSTR aGuestIP, LONG aGuestPort)
4364{
4365 LogFlowThisFunc(("\n"));
4366
4367 AutoCaller autoCaller(this);
4368 AssertComRCReturnRC(autoCaller.rc());
4369
4370 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
4371
4372 HRESULT rc = S_OK;
4373
4374 /* don't trigger NAT engine changes if the VM isn't running */
4375 SafeVMPtrQuiet ptrVM(this);
4376 if (ptrVM.isOk())
4377 {
4378 do
4379 {
4380 ComPtr<INetworkAdapter> pNetworkAdapter;
4381 rc = machine()->GetNetworkAdapter(ulInstance, pNetworkAdapter.asOutParam());
4382 if ( FAILED(rc)
4383 || pNetworkAdapter.isNull())
4384 break;
4385
4386 /*
4387 * Find the adapter instance, get the config interface and update
4388 * the link state.
4389 */
4390 NetworkAdapterType_T adapterType;
4391 rc = pNetworkAdapter->COMGETTER(AdapterType)(&adapterType);
4392 if (FAILED(rc))
4393 {
4394 AssertComRC(rc);
4395 rc = E_FAIL;
4396 break;
4397 }
4398
4399 const char *pszAdapterName = networkAdapterTypeToName(adapterType);
4400 PPDMIBASE pBase;
4401 int vrc = PDMR3QueryLun(ptrVM.rawUVM(), pszAdapterName, ulInstance, 0, &pBase);
4402 if (RT_FAILURE(vrc))
4403 {
4404 ComAssertRC(vrc);
4405 rc = E_FAIL;
4406 break;
4407 }
4408
4409 NetworkAttachmentType_T attachmentType;
4410 rc = pNetworkAdapter->COMGETTER(AttachmentType)(&attachmentType);
4411 if ( FAILED(rc)
4412 || attachmentType != NetworkAttachmentType_NAT)
4413 {
4414 rc = E_FAIL;
4415 break;
4416 }
4417
4418 /* look down for PDMINETWORKNATCONFIG interface */
4419 PPDMINETWORKNATCONFIG pNetNatCfg = NULL;
4420 while (pBase)
4421 {
4422 pNetNatCfg = (PPDMINETWORKNATCONFIG)pBase->pfnQueryInterface(pBase, PDMINETWORKNATCONFIG_IID);
4423 if (pNetNatCfg)
4424 break;
4425 /** @todo r=bird: This stinks! */
4426 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pBase);
4427 pBase = pDrvIns->pDownBase;
4428 }
4429 if (!pNetNatCfg)
4430 break;
4431
4432 bool fUdp = aProto == NATProtocol_UDP;
4433 vrc = pNetNatCfg->pfnRedirectRuleCommand(pNetNatCfg, !!aNatRuleRemove, fUdp,
4434 Utf8Str(aHostIP).c_str(), (uint16_t)aHostPort, Utf8Str(aGuestIP).c_str(),
4435 (uint16_t)aGuestPort);
4436 if (RT_FAILURE(vrc))
4437 rc = E_FAIL;
4438 } while (0); /* break loop */
4439 ptrVM.release();
4440 }
4441
4442 LogFlowThisFunc(("Leaving rc=%#x\n", rc));
4443 return rc;
4444}
4445
4446VMMDevMouseInterface *Console::getVMMDevMouseInterface()
4447{
4448 return m_pVMMDev;
4449}
4450
4451DisplayMouseInterface *Console::getDisplayMouseInterface()
4452{
4453 return mDisplay;
4454}
4455
4456/**
4457 * Process a network adaptor change.
4458 *
4459 * @returns COM status code.
4460 *
4461 * @parma pUVM The VM handle (caller hold this safely).
4462 * @param pszDevice The PDM device name.
4463 * @param uInstance The PDM device instance.
4464 * @param uLun The PDM LUN number of the drive.
4465 * @param aNetworkAdapter The network adapter whose attachment needs to be changed
4466 */
4467HRESULT Console::doNetworkAdapterChange(PUVM pUVM,
4468 const char *pszDevice,
4469 unsigned uInstance,
4470 unsigned uLun,
4471 INetworkAdapter *aNetworkAdapter)
4472{
4473 LogFlowThisFunc(("pszDevice=%p:{%s} uInstance=%u uLun=%u aNetworkAdapter=%p\n",
4474 pszDevice, pszDevice, uInstance, uLun, aNetworkAdapter));
4475
4476 AutoCaller autoCaller(this);
4477 AssertComRCReturnRC(autoCaller.rc());
4478
4479 /*
4480 * Call worker in EMT, that's faster and safer than doing everything
4481 * using VM3ReqCall. Note that we separate VMR3ReqCall from VMR3ReqWait
4482 * here to make requests from under the lock in order to serialize them.
4483 */
4484 PVMREQ pReq;
4485 int vrc = VMR3ReqCallU(pUVM, 0 /*idDstCpu*/, &pReq, 0 /* no wait! */, VMREQFLAGS_VBOX_STATUS,
4486 (PFNRT)Console::changeNetworkAttachment, 6,
4487 this, pUVM, pszDevice, uInstance, uLun, aNetworkAdapter);
4488
4489 if (vrc == VERR_TIMEOUT || RT_SUCCESS(vrc))
4490 {
4491 vrc = VMR3ReqWait(pReq, RT_INDEFINITE_WAIT);
4492 AssertRC(vrc);
4493 if (RT_SUCCESS(vrc))
4494 vrc = pReq->iStatus;
4495 }
4496 VMR3ReqFree(pReq);
4497
4498 if (RT_SUCCESS(vrc))
4499 {
4500 LogFlowThisFunc(("Returns S_OK\n"));
4501 return S_OK;
4502 }
4503
4504 return setError(E_FAIL,
4505 tr("Could not change the network adaptor attachement type (%Rrc)"),
4506 vrc);
4507}
4508
4509
4510/**
4511 * Performs the Network Adaptor change in EMT.
4512 *
4513 * @returns VBox status code.
4514 *
4515 * @param pThis Pointer to the Console object.
4516 * @param pUVM The VM handle.
4517 * @param pszDevice The PDM device name.
4518 * @param uInstance The PDM device instance.
4519 * @param uLun The PDM LUN number of the drive.
4520 * @param aNetworkAdapter The network adapter whose attachment needs to be changed
4521 *
4522 * @thread EMT
4523 * @note Locks the Console object for writing.
4524 */
4525DECLCALLBACK(int) Console::changeNetworkAttachment(Console *pThis,
4526 PUVM pUVM,
4527 const char *pszDevice,
4528 unsigned uInstance,
4529 unsigned uLun,
4530 INetworkAdapter *aNetworkAdapter)
4531{
4532 LogFlowFunc(("pThis=%p pszDevice=%p:{%s} uInstance=%u uLun=%u aNetworkAdapter=%p\n",
4533 pThis, pszDevice, pszDevice, uInstance, uLun, aNetworkAdapter));
4534
4535 AssertReturn(pThis, VERR_INVALID_PARAMETER);
4536
4537 AutoCaller autoCaller(pThis);
4538 AssertComRCReturn(autoCaller.rc(), VERR_ACCESS_DENIED);
4539
4540 ComPtr<IVirtualBox> pVirtualBox;
4541 pThis->mMachine->COMGETTER(Parent)(pVirtualBox.asOutParam());
4542 ComPtr<ISystemProperties> pSystemProperties;
4543 if (pVirtualBox)
4544 pVirtualBox->COMGETTER(SystemProperties)(pSystemProperties.asOutParam());
4545 ChipsetType_T chipsetType = ChipsetType_PIIX3;
4546 pThis->mMachine->COMGETTER(ChipsetType)(&chipsetType);
4547 ULONG maxNetworkAdapters = 0;
4548 if (pSystemProperties)
4549 pSystemProperties->GetMaxNetworkAdapters(chipsetType, &maxNetworkAdapters);
4550 AssertMsg( ( !strcmp(pszDevice, "pcnet")
4551 || !strcmp(pszDevice, "e1000")
4552 || !strcmp(pszDevice, "virtio-net"))
4553 && uLun == 0
4554 && uInstance < maxNetworkAdapters,
4555 ("pszDevice=%s uLun=%d uInstance=%d\n", pszDevice, uLun, uInstance));
4556 Log(("pszDevice=%s uLun=%d uInstance=%d\n", pszDevice, uLun, uInstance));
4557
4558 /*
4559 * Suspend the VM first.
4560 *
4561 * The VM must not be running since it might have pending I/O to
4562 * the drive which is being changed.
4563 */
4564 bool fResume;
4565 VMSTATE enmVMState = VMR3GetStateU(pUVM);
4566 switch (enmVMState)
4567 {
4568 case VMSTATE_RESETTING:
4569 case VMSTATE_RUNNING:
4570 {
4571 LogFlowFunc(("Suspending the VM...\n"));
4572 /* disable the callback to prevent Console-level state change */
4573 pThis->mVMStateChangeCallbackDisabled = true;
4574 int rc = VMR3Suspend(pUVM, VMSUSPENDREASON_RECONFIG);
4575 pThis->mVMStateChangeCallbackDisabled = false;
4576 AssertRCReturn(rc, rc);
4577 fResume = true;
4578 break;
4579 }
4580
4581 case VMSTATE_SUSPENDED:
4582 case VMSTATE_CREATED:
4583 case VMSTATE_OFF:
4584 fResume = false;
4585 break;
4586
4587 default:
4588 AssertLogRelMsgFailedReturn(("enmVMState=%d\n", enmVMState), VERR_ACCESS_DENIED);
4589 }
4590
4591 int rc = VINF_SUCCESS;
4592 int rcRet = VINF_SUCCESS;
4593
4594 PCFGMNODE pCfg = NULL; /* /Devices/Dev/.../Config/ */
4595 PCFGMNODE pLunL0 = NULL; /* /Devices/Dev/0/LUN#0/ */
4596 PCFGMNODE pInst = CFGMR3GetChildF(CFGMR3GetRootU(pUVM), "Devices/%s/%d/", pszDevice, uInstance);
4597 AssertRelease(pInst);
4598
4599 rcRet = pThis->configNetwork(pszDevice, uInstance, uLun, aNetworkAdapter, pCfg, pLunL0, pInst,
4600 true /*fAttachDetach*/, false /*fIgnoreConnectFailure*/);
4601
4602 /*
4603 * Resume the VM if necessary.
4604 */
4605 if (fResume)
4606 {
4607 LogFlowFunc(("Resuming the VM...\n"));
4608 /* disable the callback to prevent Console-level state change */
4609 pThis->mVMStateChangeCallbackDisabled = true;
4610 rc = VMR3Resume(pUVM, VMRESUMEREASON_RECONFIG);
4611 pThis->mVMStateChangeCallbackDisabled = false;
4612 AssertRC(rc);
4613 if (RT_FAILURE(rc))
4614 {
4615 /* too bad, we failed. try to sync the console state with the VMM state */
4616 vmstateChangeCallback(pUVM, VMSTATE_SUSPENDED, enmVMState, pThis);
4617 }
4618 /// @todo (r=dmik) if we failed with drive mount, then the VMR3Resume
4619 // error (if any) will be hidden from the caller. For proper reporting
4620 // of such multiple errors to the caller we need to enhance the
4621 // IVirtualBoxError interface. For now, give the first error the higher
4622 // priority.
4623 if (RT_SUCCESS(rcRet))
4624 rcRet = rc;
4625 }
4626
4627 LogFlowFunc(("Returning %Rrc\n", rcRet));
4628 return rcRet;
4629}
4630
4631
4632/**
4633 * Called by IInternalSessionControl::OnSerialPortChange().
4634 */
4635HRESULT Console::onSerialPortChange(ISerialPort *aSerialPort)
4636{
4637 LogFlowThisFunc(("\n"));
4638
4639 AutoCaller autoCaller(this);
4640 AssertComRCReturnRC(autoCaller.rc());
4641
4642 fireSerialPortChangedEvent(mEventSource, aSerialPort);
4643
4644 LogFlowThisFunc(("Leaving rc=%#x\n", S_OK));
4645 return S_OK;
4646}
4647
4648/**
4649 * Called by IInternalSessionControl::OnParallelPortChange().
4650 */
4651HRESULT Console::onParallelPortChange(IParallelPort *aParallelPort)
4652{
4653 LogFlowThisFunc(("\n"));
4654
4655 AutoCaller autoCaller(this);
4656 AssertComRCReturnRC(autoCaller.rc());
4657
4658 fireParallelPortChangedEvent(mEventSource, aParallelPort);
4659
4660 LogFlowThisFunc(("Leaving rc=%#x\n", S_OK));
4661 return S_OK;
4662}
4663
4664/**
4665 * Called by IInternalSessionControl::OnStorageControllerChange().
4666 */
4667HRESULT Console::onStorageControllerChange()
4668{
4669 LogFlowThisFunc(("\n"));
4670
4671 AutoCaller autoCaller(this);
4672 AssertComRCReturnRC(autoCaller.rc());
4673
4674 fireStorageControllerChangedEvent(mEventSource);
4675
4676 LogFlowThisFunc(("Leaving rc=%#x\n", S_OK));
4677 return S_OK;
4678}
4679
4680/**
4681 * Called by IInternalSessionControl::OnMediumChange().
4682 */
4683HRESULT Console::onMediumChange(IMediumAttachment *aMediumAttachment, BOOL aForce)
4684{
4685 LogFlowThisFunc(("\n"));
4686
4687 AutoCaller autoCaller(this);
4688 AssertComRCReturnRC(autoCaller.rc());
4689
4690 HRESULT rc = S_OK;
4691
4692 /* don't trigger medium changes if the VM isn't running */
4693 SafeVMPtrQuiet ptrVM(this);
4694 if (ptrVM.isOk())
4695 {
4696 rc = doMediumChange(aMediumAttachment, !!aForce, ptrVM.rawUVM());
4697 ptrVM.release();
4698 }
4699
4700 /* notify console callbacks on success */
4701 if (SUCCEEDED(rc))
4702 fireMediumChangedEvent(mEventSource, aMediumAttachment);
4703
4704 LogFlowThisFunc(("Leaving rc=%#x\n", rc));
4705 return rc;
4706}
4707
4708/**
4709 * Called by IInternalSessionControl::OnCPUChange().
4710 *
4711 * @note Locks this object for writing.
4712 */
4713HRESULT Console::onCPUChange(ULONG aCPU, BOOL aRemove)
4714{
4715 LogFlowThisFunc(("\n"));
4716
4717 AutoCaller autoCaller(this);
4718 AssertComRCReturnRC(autoCaller.rc());
4719
4720 HRESULT rc = S_OK;
4721
4722 /* don't trigger CPU changes if the VM isn't running */
4723 SafeVMPtrQuiet ptrVM(this);
4724 if (ptrVM.isOk())
4725 {
4726 if (aRemove)
4727 rc = doCPURemove(aCPU, ptrVM.rawUVM());
4728 else
4729 rc = doCPUAdd(aCPU, ptrVM.rawUVM());
4730 ptrVM.release();
4731 }
4732
4733 /* notify console callbacks on success */
4734 if (SUCCEEDED(rc))
4735 fireCPUChangedEvent(mEventSource, aCPU, aRemove);
4736
4737 LogFlowThisFunc(("Leaving rc=%#x\n", rc));
4738 return rc;
4739}
4740
4741/**
4742 * Called by IInternalSessionControl::OnCpuExecutionCapChange().
4743 *
4744 * @note Locks this object for writing.
4745 */
4746HRESULT Console::onCPUExecutionCapChange(ULONG aExecutionCap)
4747{
4748 LogFlowThisFunc(("\n"));
4749
4750 AutoCaller autoCaller(this);
4751 AssertComRCReturnRC(autoCaller.rc());
4752
4753 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
4754
4755 HRESULT rc = S_OK;
4756
4757 /* don't trigger the CPU priority change if the VM isn't running */
4758 SafeVMPtrQuiet ptrVM(this);
4759 if (ptrVM.isOk())
4760 {
4761 if ( mMachineState == MachineState_Running
4762 || mMachineState == MachineState_Teleporting
4763 || mMachineState == MachineState_LiveSnapshotting
4764 )
4765 {
4766 /* No need to call in the EMT thread. */
4767 rc = VMR3SetCpuExecutionCap(ptrVM.rawUVM(), aExecutionCap);
4768 }
4769 else
4770 rc = setInvalidMachineStateError();
4771 ptrVM.release();
4772 }
4773
4774 /* notify console callbacks on success */
4775 if (SUCCEEDED(rc))
4776 {
4777 alock.release();
4778 fireCPUExecutionCapChangedEvent(mEventSource, aExecutionCap);
4779 }
4780
4781 LogFlowThisFunc(("Leaving rc=%#x\n", rc));
4782 return rc;
4783}
4784
4785/**
4786 * Called by IInternalSessionControl::OnClipboardModeChange().
4787 *
4788 * @note Locks this object for writing.
4789 */
4790HRESULT Console::onClipboardModeChange(ClipboardMode_T aClipboardMode)
4791{
4792 LogFlowThisFunc(("\n"));
4793
4794 AutoCaller autoCaller(this);
4795 AssertComRCReturnRC(autoCaller.rc());
4796
4797 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
4798
4799 HRESULT rc = S_OK;
4800
4801 /* don't trigger the clipboard mode change if the VM isn't running */
4802 SafeVMPtrQuiet ptrVM(this);
4803 if (ptrVM.isOk())
4804 {
4805 if ( mMachineState == MachineState_Running
4806 || mMachineState == MachineState_Teleporting
4807 || mMachineState == MachineState_LiveSnapshotting)
4808 changeClipboardMode(aClipboardMode);
4809 else
4810 rc = setInvalidMachineStateError();
4811 ptrVM.release();
4812 }
4813
4814 /* notify console callbacks on success */
4815 if (SUCCEEDED(rc))
4816 {
4817 alock.release();
4818 fireClipboardModeChangedEvent(mEventSource, aClipboardMode);
4819 }
4820
4821 LogFlowThisFunc(("Leaving rc=%#x\n", rc));
4822 return rc;
4823}
4824
4825/**
4826 * Called by IInternalSessionControl::OnDragAndDropModeChange().
4827 *
4828 * @note Locks this object for writing.
4829 */
4830HRESULT Console::onDragAndDropModeChange(DragAndDropMode_T aDragAndDropMode)
4831{
4832 LogFlowThisFunc(("\n"));
4833
4834 AutoCaller autoCaller(this);
4835 AssertComRCReturnRC(autoCaller.rc());
4836
4837 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
4838
4839 HRESULT rc = S_OK;
4840
4841 /* don't trigger the drag'n'drop mode change if the VM isn't running */
4842 SafeVMPtrQuiet ptrVM(this);
4843 if (ptrVM.isOk())
4844 {
4845 if ( mMachineState == MachineState_Running
4846 || mMachineState == MachineState_Teleporting
4847 || mMachineState == MachineState_LiveSnapshotting)
4848 changeDragAndDropMode(aDragAndDropMode);
4849 else
4850 rc = setInvalidMachineStateError();
4851 ptrVM.release();
4852 }
4853
4854 /* notify console callbacks on success */
4855 if (SUCCEEDED(rc))
4856 {
4857 alock.release();
4858 fireDragAndDropModeChangedEvent(mEventSource, aDragAndDropMode);
4859 }
4860
4861 LogFlowThisFunc(("Leaving rc=%#x\n", rc));
4862 return rc;
4863}
4864
4865/**
4866 * Called by IInternalSessionControl::OnVRDEServerChange().
4867 *
4868 * @note Locks this object for writing.
4869 */
4870HRESULT Console::onVRDEServerChange(BOOL aRestart)
4871{
4872 AutoCaller autoCaller(this);
4873 AssertComRCReturnRC(autoCaller.rc());
4874
4875 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
4876
4877 HRESULT rc = S_OK;
4878
4879 /* don't trigger VRDE server changes if the VM isn't running */
4880 SafeVMPtrQuiet ptrVM(this);
4881 if (ptrVM.isOk())
4882 {
4883 /* Serialize. */
4884 if (mfVRDEChangeInProcess)
4885 mfVRDEChangePending = true;
4886 else
4887 {
4888 do {
4889 mfVRDEChangeInProcess = true;
4890 mfVRDEChangePending = false;
4891
4892 if ( mVRDEServer
4893 && ( mMachineState == MachineState_Running
4894 || mMachineState == MachineState_Teleporting
4895 || mMachineState == MachineState_LiveSnapshotting
4896 || mMachineState == MachineState_Paused
4897 )
4898 )
4899 {
4900 BOOL vrdpEnabled = FALSE;
4901
4902 rc = mVRDEServer->COMGETTER(Enabled)(&vrdpEnabled);
4903 ComAssertComRCRetRC(rc);
4904
4905 if (aRestart)
4906 {
4907 /* VRDP server may call this Console object back from other threads (VRDP INPUT or OUTPUT). */
4908 alock.release();
4909
4910 if (vrdpEnabled)
4911 {
4912 // If there was no VRDP server started the 'stop' will do nothing.
4913 // However if a server was started and this notification was called,
4914 // we have to restart the server.
4915 mConsoleVRDPServer->Stop();
4916
4917 if (RT_FAILURE(mConsoleVRDPServer->Launch()))
4918 rc = E_FAIL;
4919 else
4920 mConsoleVRDPServer->EnableConnections();
4921 }
4922 else
4923 mConsoleVRDPServer->Stop();
4924
4925 alock.acquire();
4926 }
4927 }
4928 else
4929 rc = setInvalidMachineStateError();
4930
4931 mfVRDEChangeInProcess = false;
4932 } while (mfVRDEChangePending && SUCCEEDED(rc));
4933 }
4934
4935 ptrVM.release();
4936 }
4937
4938 /* notify console callbacks on success */
4939 if (SUCCEEDED(rc))
4940 {
4941 alock.release();
4942 fireVRDEServerChangedEvent(mEventSource);
4943 }
4944
4945 return rc;
4946}
4947
4948void Console::onVRDEServerInfoChange()
4949{
4950 AutoCaller autoCaller(this);
4951 AssertComRCReturnVoid(autoCaller.rc());
4952
4953 fireVRDEServerInfoChangedEvent(mEventSource);
4954}
4955
4956HRESULT Console::onVideoCaptureChange()
4957{
4958 AutoCaller autoCaller(this);
4959 AssertComRCReturnRC(autoCaller.rc());
4960
4961 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
4962
4963 HRESULT rc = S_OK;
4964
4965 /* don't trigger video capture changes if the VM isn't running */
4966 SafeVMPtrQuiet ptrVM(this);
4967 if (ptrVM.isOk())
4968 {
4969 BOOL fEnabled;
4970 rc = mMachine->COMGETTER(VideoCaptureEnabled)(&fEnabled);
4971 SafeArray<BOOL> screens;
4972 if (SUCCEEDED(rc))
4973 rc = mMachine->COMGETTER(VideoCaptureScreens)(ComSafeArrayAsOutParam(screens));
4974 if (mDisplay)
4975 {
4976 int vrc = VINF_SUCCESS;
4977 if (SUCCEEDED(rc))
4978 vrc = mDisplay->VideoCaptureEnableScreens(ComSafeArrayAsInParam(screens));
4979 if (RT_SUCCESS(vrc))
4980 {
4981 if (fEnabled)
4982 {
4983 vrc = mDisplay->VideoCaptureStart();
4984 if (RT_FAILURE(vrc))
4985 rc = setError(E_FAIL, tr("Unable to start video capturing (%Rrc)"), vrc);
4986 }
4987 else
4988 mDisplay->VideoCaptureStop();
4989 }
4990 else
4991 rc = setError(E_FAIL, tr("Unable to set screens for capturing (%Rrc)"), vrc);
4992 }
4993 ptrVM.release();
4994 }
4995
4996 /* notify console callbacks on success */
4997 if (SUCCEEDED(rc))
4998 {
4999 alock.release();
5000 fireVideoCaptureChangedEvent(mEventSource);
5001 }
5002
5003 return rc;
5004}
5005
5006/**
5007 * Called by IInternalSessionControl::OnUSBControllerChange().
5008 */
5009HRESULT Console::onUSBControllerChange()
5010{
5011 LogFlowThisFunc(("\n"));
5012
5013 AutoCaller autoCaller(this);
5014 AssertComRCReturnRC(autoCaller.rc());
5015
5016 fireUSBControllerChangedEvent(mEventSource);
5017
5018 return S_OK;
5019}
5020
5021/**
5022 * Called by IInternalSessionControl::OnSharedFolderChange().
5023 *
5024 * @note Locks this object for writing.
5025 */
5026HRESULT Console::onSharedFolderChange(BOOL aGlobal)
5027{
5028 LogFlowThisFunc(("aGlobal=%RTbool\n", aGlobal));
5029
5030 AutoCaller autoCaller(this);
5031 AssertComRCReturnRC(autoCaller.rc());
5032
5033 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
5034
5035 HRESULT rc = fetchSharedFolders(aGlobal);
5036
5037 /* notify console callbacks on success */
5038 if (SUCCEEDED(rc))
5039 {
5040 alock.release();
5041 fireSharedFolderChangedEvent(mEventSource, aGlobal ? (Scope_T)Scope_Global : (Scope_T)Scope_Machine);
5042 }
5043
5044 return rc;
5045}
5046
5047/**
5048 * Called by IInternalSessionControl::OnUSBDeviceAttach() or locally by
5049 * processRemoteUSBDevices() after IInternalMachineControl::RunUSBDeviceFilters()
5050 * returns TRUE for a given remote USB device.
5051 *
5052 * @return S_OK if the device was attached to the VM.
5053 * @return failure if not attached.
5054 *
5055 * @param aDevice
5056 * The device in question.
5057 * @param aMaskedIfs
5058 * The interfaces to hide from the guest.
5059 *
5060 * @note Locks this object for writing.
5061 */
5062HRESULT Console::onUSBDeviceAttach(IUSBDevice *aDevice, IVirtualBoxErrorInfo *aError, ULONG aMaskedIfs)
5063{
5064#ifdef VBOX_WITH_USB
5065 LogFlowThisFunc(("aDevice=%p aError=%p\n", aDevice, aError));
5066
5067 AutoCaller autoCaller(this);
5068 ComAssertComRCRetRC(autoCaller.rc());
5069
5070 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
5071
5072 /* Get the VM pointer (we don't need error info, since it's a callback). */
5073 SafeVMPtrQuiet ptrVM(this);
5074 if (!ptrVM.isOk())
5075 {
5076 /* The VM may be no more operational when this message arrives
5077 * (e.g. it may be Saving or Stopping or just PoweredOff) --
5078 * autoVMCaller.rc() will return a failure in this case. */
5079 LogFlowThisFunc(("Attach request ignored (mMachineState=%d).\n",
5080 mMachineState));
5081 return ptrVM.rc();
5082 }
5083
5084 if (aError != NULL)
5085 {
5086 /* notify callbacks about the error */
5087 alock.release();
5088 onUSBDeviceStateChange(aDevice, true /* aAttached */, aError);
5089 return S_OK;
5090 }
5091
5092 /* Don't proceed unless there's at least one USB hub. */
5093 if (!PDMR3UsbHasHub(ptrVM.rawUVM()))
5094 {
5095 LogFlowThisFunc(("Attach request ignored (no USB controller).\n"));
5096 return E_FAIL;
5097 }
5098
5099 alock.release();
5100 HRESULT rc = attachUSBDevice(aDevice, aMaskedIfs);
5101 if (FAILED(rc))
5102 {
5103 /* take the current error info */
5104 com::ErrorInfoKeeper eik;
5105 /* the error must be a VirtualBoxErrorInfo instance */
5106 ComPtr<IVirtualBoxErrorInfo> pError = eik.takeError();
5107 Assert(!pError.isNull());
5108 if (!pError.isNull())
5109 {
5110 /* notify callbacks about the error */
5111 onUSBDeviceStateChange(aDevice, true /* aAttached */, pError);
5112 }
5113 }
5114
5115 return rc;
5116
5117#else /* !VBOX_WITH_USB */
5118 return E_FAIL;
5119#endif /* !VBOX_WITH_USB */
5120}
5121
5122/**
5123 * Called by IInternalSessionControl::OnUSBDeviceDetach() and locally by
5124 * processRemoteUSBDevices().
5125 *
5126 * @note Locks this object for writing.
5127 */
5128HRESULT Console::onUSBDeviceDetach(IN_BSTR aId,
5129 IVirtualBoxErrorInfo *aError)
5130{
5131#ifdef VBOX_WITH_USB
5132 Guid Uuid(aId);
5133 LogFlowThisFunc(("aId={%RTuuid} aError=%p\n", Uuid.raw(), aError));
5134
5135 AutoCaller autoCaller(this);
5136 AssertComRCReturnRC(autoCaller.rc());
5137
5138 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
5139
5140 /* Find the device. */
5141 ComObjPtr<OUSBDevice> pUSBDevice;
5142 USBDeviceList::iterator it = mUSBDevices.begin();
5143 while (it != mUSBDevices.end())
5144 {
5145 LogFlowThisFunc(("it={%RTuuid}\n", (*it)->id().raw()));
5146 if ((*it)->id() == Uuid)
5147 {
5148 pUSBDevice = *it;
5149 break;
5150 }
5151 ++it;
5152 }
5153
5154
5155 if (pUSBDevice.isNull())
5156 {
5157 LogFlowThisFunc(("USB device not found.\n"));
5158
5159 /* The VM may be no more operational when this message arrives
5160 * (e.g. it may be Saving or Stopping or just PoweredOff). Use
5161 * AutoVMCaller to detect it -- AutoVMCaller::rc() will return a
5162 * failure in this case. */
5163
5164 AutoVMCallerQuiet autoVMCaller(this);
5165 if (FAILED(autoVMCaller.rc()))
5166 {
5167 LogFlowThisFunc(("Detach request ignored (mMachineState=%d).\n",
5168 mMachineState));
5169 return autoVMCaller.rc();
5170 }
5171
5172 /* the device must be in the list otherwise */
5173 AssertFailedReturn(E_FAIL);
5174 }
5175
5176 if (aError != NULL)
5177 {
5178 /* notify callback about an error */
5179 alock.release();
5180 onUSBDeviceStateChange(pUSBDevice, false /* aAttached */, aError);
5181 return S_OK;
5182 }
5183
5184 /* Remove the device from the collection, it is re-added below for failures */
5185 mUSBDevices.erase(it);
5186
5187 alock.release();
5188 HRESULT rc = detachUSBDevice(pUSBDevice);
5189 if (FAILED(rc))
5190 {
5191 /* Re-add the device to the collection */
5192 alock.acquire();
5193 mUSBDevices.push_back(pUSBDevice);
5194 alock.release();
5195 /* take the current error info */
5196 com::ErrorInfoKeeper eik;
5197 /* the error must be a VirtualBoxErrorInfo instance */
5198 ComPtr<IVirtualBoxErrorInfo> pError = eik.takeError();
5199 Assert(!pError.isNull());
5200 if (!pError.isNull())
5201 {
5202 /* notify callbacks about the error */
5203 onUSBDeviceStateChange(pUSBDevice, false /* aAttached */, pError);
5204 }
5205 }
5206
5207 return rc;
5208
5209#else /* !VBOX_WITH_USB */
5210 return E_FAIL;
5211#endif /* !VBOX_WITH_USB */
5212}
5213
5214/**
5215 * Called by IInternalSessionControl::OnBandwidthGroupChange().
5216 *
5217 * @note Locks this object for writing.
5218 */
5219HRESULT Console::onBandwidthGroupChange(IBandwidthGroup *aBandwidthGroup)
5220{
5221 LogFlowThisFunc(("\n"));
5222
5223 AutoCaller autoCaller(this);
5224 AssertComRCReturnRC(autoCaller.rc());
5225
5226 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
5227
5228 HRESULT rc = S_OK;
5229
5230 /* don't trigger bandwidth group changes if the VM isn't running */
5231 SafeVMPtrQuiet ptrVM(this);
5232 if (ptrVM.isOk())
5233 {
5234 if ( mMachineState == MachineState_Running
5235 || mMachineState == MachineState_Teleporting
5236 || mMachineState == MachineState_LiveSnapshotting
5237 )
5238 {
5239 /* No need to call in the EMT thread. */
5240 LONG64 cMax;
5241 Bstr strName;
5242 BandwidthGroupType_T enmType;
5243 rc = aBandwidthGroup->COMGETTER(Name)(strName.asOutParam());
5244 if (SUCCEEDED(rc))
5245 rc = aBandwidthGroup->COMGETTER(MaxBytesPerSec)(&cMax);
5246 if (SUCCEEDED(rc))
5247 rc = aBandwidthGroup->COMGETTER(Type)(&enmType);
5248
5249 if (SUCCEEDED(rc))
5250 {
5251 int vrc = VINF_SUCCESS;
5252 if (enmType == BandwidthGroupType_Disk)
5253 vrc = PDMR3AsyncCompletionBwMgrSetMaxForFile(ptrVM.rawUVM(), Utf8Str(strName).c_str(), (uint32_t)cMax);
5254#ifdef VBOX_WITH_NETSHAPER
5255 else if (enmType == BandwidthGroupType_Network)
5256 vrc = PDMR3NsBwGroupSetLimit(ptrVM.rawUVM(), Utf8Str(strName).c_str(), cMax);
5257 else
5258 rc = E_NOTIMPL;
5259#endif /* VBOX_WITH_NETSHAPER */
5260 AssertRC(vrc);
5261 }
5262 }
5263 else
5264 rc = setInvalidMachineStateError();
5265 ptrVM.release();
5266 }
5267
5268 /* notify console callbacks on success */
5269 if (SUCCEEDED(rc))
5270 {
5271 alock.release();
5272 fireBandwidthGroupChangedEvent(mEventSource, aBandwidthGroup);
5273 }
5274
5275 LogFlowThisFunc(("Leaving rc=%#x\n", rc));
5276 return rc;
5277}
5278
5279/**
5280 * Called by IInternalSessionControl::OnStorageDeviceChange().
5281 *
5282 * @note Locks this object for writing.
5283 */
5284HRESULT Console::onStorageDeviceChange(IMediumAttachment *aMediumAttachment, BOOL aRemove, BOOL aSilent)
5285{
5286 LogFlowThisFunc(("\n"));
5287
5288 AutoCaller autoCaller(this);
5289 AssertComRCReturnRC(autoCaller.rc());
5290
5291 HRESULT rc = S_OK;
5292
5293 /* don't trigger medium changes if the VM isn't running */
5294 SafeVMPtrQuiet ptrVM(this);
5295 if (ptrVM.isOk())
5296 {
5297 if (aRemove)
5298 rc = doStorageDeviceDetach(aMediumAttachment, ptrVM.rawUVM(), RT_BOOL(aSilent));
5299 else
5300 rc = doStorageDeviceAttach(aMediumAttachment, ptrVM.rawUVM(), RT_BOOL(aSilent));
5301 ptrVM.release();
5302 }
5303
5304 /* notify console callbacks on success */
5305 if (SUCCEEDED(rc))
5306 fireStorageDeviceChangedEvent(mEventSource, aMediumAttachment, aRemove, aSilent);
5307
5308 LogFlowThisFunc(("Leaving rc=%#x\n", rc));
5309 return rc;
5310}
5311
5312/**
5313 * @note Temporarily locks this object for writing.
5314 */
5315HRESULT Console::getGuestProperty(IN_BSTR aName, BSTR *aValue,
5316 LONG64 *aTimestamp, BSTR *aFlags)
5317{
5318#ifndef VBOX_WITH_GUEST_PROPS
5319 ReturnComNotImplemented();
5320#else /* VBOX_WITH_GUEST_PROPS */
5321 if (!VALID_PTR(aName))
5322 return E_INVALIDARG;
5323 if (!VALID_PTR(aValue))
5324 return E_POINTER;
5325 if ((aTimestamp != NULL) && !VALID_PTR(aTimestamp))
5326 return E_POINTER;
5327 if ((aFlags != NULL) && !VALID_PTR(aFlags))
5328 return E_POINTER;
5329
5330 AutoCaller autoCaller(this);
5331 AssertComRCReturnRC(autoCaller.rc());
5332
5333 /* protect mpUVM (if not NULL) */
5334 SafeVMPtrQuiet ptrVM(this);
5335 if (FAILED(ptrVM.rc()))
5336 return ptrVM.rc();
5337
5338 /* Note: validity of mVMMDev which is bound to uninit() is guaranteed by
5339 * ptrVM, so there is no need to hold a lock of this */
5340
5341 HRESULT rc = E_UNEXPECTED;
5342 using namespace guestProp;
5343
5344 try
5345 {
5346 VBOXHGCMSVCPARM parm[4];
5347 Utf8Str Utf8Name = aName;
5348 char szBuffer[MAX_VALUE_LEN + MAX_FLAGS_LEN];
5349
5350 parm[0].type = VBOX_HGCM_SVC_PARM_PTR;
5351 parm[0].u.pointer.addr = (void*)Utf8Name.c_str();
5352 /* The + 1 is the null terminator */
5353 parm[0].u.pointer.size = (uint32_t)Utf8Name.length() + 1;
5354 parm[1].type = VBOX_HGCM_SVC_PARM_PTR;
5355 parm[1].u.pointer.addr = szBuffer;
5356 parm[1].u.pointer.size = sizeof(szBuffer);
5357 int vrc = m_pVMMDev->hgcmHostCall("VBoxGuestPropSvc", GET_PROP_HOST,
5358 4, &parm[0]);
5359 /* The returned string should never be able to be greater than our buffer */
5360 AssertLogRel(vrc != VERR_BUFFER_OVERFLOW);
5361 AssertLogRel(RT_FAILURE(vrc) || VBOX_HGCM_SVC_PARM_64BIT == parm[2].type);
5362 if (RT_SUCCESS(vrc) || (VERR_NOT_FOUND == vrc))
5363 {
5364 rc = S_OK;
5365 if (vrc != VERR_NOT_FOUND)
5366 {
5367 Utf8Str strBuffer(szBuffer);
5368 strBuffer.cloneTo(aValue);
5369
5370 if (aTimestamp)
5371 *aTimestamp = parm[2].u.uint64;
5372
5373 if (aFlags)
5374 {
5375 size_t iFlags = strBuffer.length() + 1;
5376 Utf8Str(szBuffer + iFlags).cloneTo(aFlags);
5377 }
5378 }
5379 else
5380 aValue = NULL;
5381 }
5382 else
5383 rc = setError(E_UNEXPECTED,
5384 tr("The service call failed with the error %Rrc"),
5385 vrc);
5386 }
5387 catch(std::bad_alloc & /*e*/)
5388 {
5389 rc = E_OUTOFMEMORY;
5390 }
5391 return rc;
5392#endif /* VBOX_WITH_GUEST_PROPS */
5393}
5394
5395/**
5396 * @note Temporarily locks this object for writing.
5397 */
5398HRESULT Console::setGuestProperty(IN_BSTR aName, IN_BSTR aValue, IN_BSTR aFlags)
5399{
5400#ifndef VBOX_WITH_GUEST_PROPS
5401 ReturnComNotImplemented();
5402#else /* VBOX_WITH_GUEST_PROPS */
5403 if (!RT_VALID_PTR(aName))
5404 return setError(E_INVALIDARG, tr("Name cannot be NULL or an invalid pointer"));
5405 if (aValue != NULL && !RT_VALID_PTR(aValue))
5406 return setError(E_INVALIDARG, tr("Invalid value pointer"));
5407 if (aFlags != NULL && !RT_VALID_PTR(aFlags))
5408 return setError(E_INVALIDARG, tr("Invalid flags pointer"));
5409
5410 AutoCaller autoCaller(this);
5411 AssertComRCReturnRC(autoCaller.rc());
5412
5413 /* protect mpUVM (if not NULL) */
5414 SafeVMPtrQuiet ptrVM(this);
5415 if (FAILED(ptrVM.rc()))
5416 return ptrVM.rc();
5417
5418 /* Note: validity of mVMMDev which is bound to uninit() is guaranteed by
5419 * ptrVM, so there is no need to hold a lock of this */
5420
5421 using namespace guestProp;
5422
5423 VBOXHGCMSVCPARM parm[3];
5424
5425 Utf8Str Utf8Name = aName;
5426 parm[0].type = VBOX_HGCM_SVC_PARM_PTR;
5427 parm[0].u.pointer.addr = (void*)Utf8Name.c_str();
5428 /* The + 1 is the null terminator */
5429 parm[0].u.pointer.size = (uint32_t)Utf8Name.length() + 1;
5430
5431 Utf8Str Utf8Value;
5432 if (aValue != NULL)
5433 {
5434 Utf8Value = aValue;
5435 parm[1].type = VBOX_HGCM_SVC_PARM_PTR;
5436 parm[1].u.pointer.addr = (void *)Utf8Value.c_str();
5437 /* The + 1 is the null terminator */
5438 parm[1].u.pointer.size = (uint32_t)Utf8Value.length() + 1;
5439 }
5440
5441 Utf8Str Utf8Flags;
5442 if (aFlags != NULL)
5443 {
5444 Utf8Flags = aFlags;
5445 parm[2].type = VBOX_HGCM_SVC_PARM_PTR;
5446 parm[2].u.pointer.addr = (void*)Utf8Flags.c_str();
5447 /* The + 1 is the null terminator */
5448 parm[2].u.pointer.size = (uint32_t)Utf8Flags.length() + 1;
5449 }
5450
5451 int vrc;
5452 if (aValue != NULL && aFlags != NULL)
5453 vrc = m_pVMMDev->hgcmHostCall("VBoxGuestPropSvc", SET_PROP_HOST,
5454 3, &parm[0]);
5455 else if (aValue != NULL)
5456 vrc = m_pVMMDev->hgcmHostCall("VBoxGuestPropSvc", SET_PROP_VALUE_HOST,
5457 2, &parm[0]);
5458 else
5459 vrc = m_pVMMDev->hgcmHostCall("VBoxGuestPropSvc", DEL_PROP_HOST,
5460 1, &parm[0]);
5461 HRESULT hrc;
5462 if (RT_SUCCESS(vrc))
5463 hrc = S_OK;
5464 else
5465 hrc = setError(E_UNEXPECTED, tr("The service call failed with the error %Rrc"), vrc);
5466 return hrc;
5467#endif /* VBOX_WITH_GUEST_PROPS */
5468}
5469
5470
5471/**
5472 * @note Temporarily locks this object for writing.
5473 */
5474HRESULT Console::enumerateGuestProperties(IN_BSTR aPatterns,
5475 ComSafeArrayOut(BSTR, aNames),
5476 ComSafeArrayOut(BSTR, aValues),
5477 ComSafeArrayOut(LONG64, aTimestamps),
5478 ComSafeArrayOut(BSTR, aFlags))
5479{
5480#ifndef VBOX_WITH_GUEST_PROPS
5481 ReturnComNotImplemented();
5482#else /* VBOX_WITH_GUEST_PROPS */
5483 if (!VALID_PTR(aPatterns) && (aPatterns != NULL))
5484 return E_POINTER;
5485 if (ComSafeArrayOutIsNull(aNames))
5486 return E_POINTER;
5487 if (ComSafeArrayOutIsNull(aValues))
5488 return E_POINTER;
5489 if (ComSafeArrayOutIsNull(aTimestamps))
5490 return E_POINTER;
5491 if (ComSafeArrayOutIsNull(aFlags))
5492 return E_POINTER;
5493
5494 AutoCaller autoCaller(this);
5495 AssertComRCReturnRC(autoCaller.rc());
5496
5497 /* protect mpUVM (if not NULL) */
5498 AutoVMCallerWeak autoVMCaller(this);
5499 if (FAILED(autoVMCaller.rc()))
5500 return autoVMCaller.rc();
5501
5502 /* Note: validity of mVMMDev which is bound to uninit() is guaranteed by
5503 * autoVMCaller, so there is no need to hold a lock of this */
5504
5505 return doEnumerateGuestProperties(aPatterns, ComSafeArrayOutArg(aNames),
5506 ComSafeArrayOutArg(aValues),
5507 ComSafeArrayOutArg(aTimestamps),
5508 ComSafeArrayOutArg(aFlags));
5509#endif /* VBOX_WITH_GUEST_PROPS */
5510}
5511
5512
5513/*
5514 * Internal: helper function for connecting progress reporting
5515 */
5516static int onlineMergeMediumProgress(void *pvUser, unsigned uPercentage)
5517{
5518 HRESULT rc = S_OK;
5519 IProgress *pProgress = static_cast<IProgress *>(pvUser);
5520 if (pProgress)
5521 rc = pProgress->SetCurrentOperationProgress(uPercentage);
5522 return SUCCEEDED(rc) ? VINF_SUCCESS : VERR_GENERAL_FAILURE;
5523}
5524
5525/**
5526 * @note Temporarily locks this object for writing. bird: And/or reading?
5527 */
5528HRESULT Console::onlineMergeMedium(IMediumAttachment *aMediumAttachment,
5529 ULONG aSourceIdx, ULONG aTargetIdx,
5530 IMedium *aSource, IMedium *aTarget,
5531 BOOL aMergeForward,
5532 IMedium *aParentForTarget,
5533 ComSafeArrayIn(IMedium *, aChildrenToReparent),
5534 IProgress *aProgress)
5535{
5536 AutoCaller autoCaller(this);
5537 AssertComRCReturnRC(autoCaller.rc());
5538
5539 HRESULT rc = S_OK;
5540 int vrc = VINF_SUCCESS;
5541
5542 /* Get the VM - must be done before the read-locking. */
5543 SafeVMPtr ptrVM(this);
5544 if (!ptrVM.isOk())
5545 return ptrVM.rc();
5546
5547 /* We will need to release the lock before doing the actual merge */
5548 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
5549
5550 /* paranoia - we don't want merges to happen while teleporting etc. */
5551 switch (mMachineState)
5552 {
5553 case MachineState_DeletingSnapshotOnline:
5554 case MachineState_DeletingSnapshotPaused:
5555 break;
5556
5557 default:
5558 return setInvalidMachineStateError();
5559 }
5560
5561 /** @todo AssertComRC -> AssertComRCReturn! Could potentially end up
5562 * using uninitialized variables here. */
5563 BOOL fBuiltinIOCache;
5564 rc = mMachine->COMGETTER(IOCacheEnabled)(&fBuiltinIOCache);
5565 AssertComRC(rc);
5566 SafeIfaceArray<IStorageController> ctrls;
5567 rc = mMachine->COMGETTER(StorageControllers)(ComSafeArrayAsOutParam(ctrls));
5568 AssertComRC(rc);
5569 LONG lDev;
5570 rc = aMediumAttachment->COMGETTER(Device)(&lDev);
5571 AssertComRC(rc);
5572 LONG lPort;
5573 rc = aMediumAttachment->COMGETTER(Port)(&lPort);
5574 AssertComRC(rc);
5575 IMedium *pMedium;
5576 rc = aMediumAttachment->COMGETTER(Medium)(&pMedium);
5577 AssertComRC(rc);
5578 Bstr mediumLocation;
5579 if (pMedium)
5580 {
5581 rc = pMedium->COMGETTER(Location)(mediumLocation.asOutParam());
5582 AssertComRC(rc);
5583 }
5584
5585 Bstr attCtrlName;
5586 rc = aMediumAttachment->COMGETTER(Controller)(attCtrlName.asOutParam());
5587 AssertComRC(rc);
5588 ComPtr<IStorageController> pStorageController;
5589 for (size_t i = 0; i < ctrls.size(); ++i)
5590 {
5591 Bstr ctrlName;
5592 rc = ctrls[i]->COMGETTER(Name)(ctrlName.asOutParam());
5593 AssertComRC(rc);
5594 if (attCtrlName == ctrlName)
5595 {
5596 pStorageController = ctrls[i];
5597 break;
5598 }
5599 }
5600 if (pStorageController.isNull())
5601 return setError(E_FAIL,
5602 tr("Could not find storage controller '%ls'"),
5603 attCtrlName.raw());
5604
5605 StorageControllerType_T enmCtrlType;
5606 rc = pStorageController->COMGETTER(ControllerType)(&enmCtrlType);
5607 AssertComRC(rc);
5608 const char *pcszDevice = convertControllerTypeToDev(enmCtrlType);
5609
5610 StorageBus_T enmBus;
5611 rc = pStorageController->COMGETTER(Bus)(&enmBus);
5612 AssertComRC(rc);
5613 ULONG uInstance;
5614 rc = pStorageController->COMGETTER(Instance)(&uInstance);
5615 AssertComRC(rc);
5616 BOOL fUseHostIOCache;
5617 rc = pStorageController->COMGETTER(UseHostIOCache)(&fUseHostIOCache);
5618 AssertComRC(rc);
5619
5620 unsigned uLUN;
5621 rc = Console::convertBusPortDeviceToLun(enmBus, lPort, lDev, uLUN);
5622 AssertComRCReturnRC(rc);
5623
5624 alock.release();
5625
5626 /* Pause the VM, as it might have pending IO on this drive */
5627 VMSTATE enmVMState = VMR3GetStateU(ptrVM.rawUVM());
5628 if (mMachineState == MachineState_DeletingSnapshotOnline)
5629 {
5630 LogFlowFunc(("Suspending the VM...\n"));
5631 /* disable the callback to prevent Console-level state change */
5632 mVMStateChangeCallbackDisabled = true;
5633 int vrc2 = VMR3Suspend(ptrVM.rawUVM(), VMSUSPENDREASON_RECONFIG);
5634 mVMStateChangeCallbackDisabled = false;
5635 AssertRCReturn(vrc2, E_FAIL);
5636 }
5637
5638 vrc = VMR3ReqCallWaitU(ptrVM.rawUVM(),
5639 VMCPUID_ANY,
5640 (PFNRT)reconfigureMediumAttachment,
5641 13,
5642 this,
5643 ptrVM.rawUVM(),
5644 pcszDevice,
5645 uInstance,
5646 enmBus,
5647 fUseHostIOCache,
5648 fBuiltinIOCache,
5649 true /* fSetupMerge */,
5650 aSourceIdx,
5651 aTargetIdx,
5652 aMediumAttachment,
5653 mMachineState,
5654 &rc);
5655 /* error handling is after resuming the VM */
5656
5657 if (mMachineState == MachineState_DeletingSnapshotOnline)
5658 {
5659 LogFlowFunc(("Resuming the VM...\n"));
5660 /* disable the callback to prevent Console-level state change */
5661 mVMStateChangeCallbackDisabled = true;
5662 int vrc2 = VMR3Resume(ptrVM.rawUVM(), VMRESUMEREASON_RECONFIG);
5663 mVMStateChangeCallbackDisabled = false;
5664 if (RT_FAILURE(vrc2))
5665 {
5666 /* too bad, we failed. try to sync the console state with the VMM state */
5667 AssertLogRelRC(vrc2);
5668 vmstateChangeCallback(ptrVM.rawUVM(), VMSTATE_SUSPENDED, enmVMState, this);
5669 }
5670 }
5671
5672 if (RT_FAILURE(vrc))
5673 return setError(E_FAIL, tr("%Rrc"), vrc);
5674 if (FAILED(rc))
5675 return rc;
5676
5677 PPDMIBASE pIBase = NULL;
5678 PPDMIMEDIA pIMedium = NULL;
5679 vrc = PDMR3QueryDriverOnLun(ptrVM.rawUVM(), pcszDevice, uInstance, uLUN, "VD", &pIBase);
5680 if (RT_SUCCESS(vrc))
5681 {
5682 if (pIBase)
5683 {
5684 pIMedium = (PPDMIMEDIA)pIBase->pfnQueryInterface(pIBase, PDMIMEDIA_IID);
5685 if (!pIMedium)
5686 return setError(E_FAIL, tr("could not query medium interface of controller"));
5687 }
5688 else
5689 return setError(E_FAIL, tr("could not query base interface of controller"));
5690 }
5691
5692 /* Finally trigger the merge. */
5693 vrc = pIMedium->pfnMerge(pIMedium, onlineMergeMediumProgress, aProgress);
5694 if (RT_FAILURE(vrc))
5695 return setError(E_FAIL, tr("Failed to perform an online medium merge (%Rrc)"), vrc);
5696
5697 /* Pause the VM, as it might have pending IO on this drive */
5698 enmVMState = VMR3GetStateU(ptrVM.rawUVM());
5699 if (mMachineState == MachineState_DeletingSnapshotOnline)
5700 {
5701 LogFlowFunc(("Suspending the VM...\n"));
5702 /* disable the callback to prevent Console-level state change */
5703 mVMStateChangeCallbackDisabled = true;
5704 int vrc2 = VMR3Suspend(ptrVM.rawUVM(), VMSUSPENDREASON_RECONFIG);
5705 mVMStateChangeCallbackDisabled = false;
5706 AssertRCReturn(vrc2, E_FAIL);
5707 }
5708
5709 /* Update medium chain and state now, so that the VM can continue. */
5710 rc = mControl->FinishOnlineMergeMedium(aMediumAttachment, aSource, aTarget,
5711 aMergeForward, aParentForTarget,
5712 ComSafeArrayInArg(aChildrenToReparent));
5713
5714 vrc = VMR3ReqCallWaitU(ptrVM.rawUVM(),
5715 VMCPUID_ANY,
5716 (PFNRT)reconfigureMediumAttachment,
5717 13,
5718 this,
5719 ptrVM.rawUVM(),
5720 pcszDevice,
5721 uInstance,
5722 enmBus,
5723 fUseHostIOCache,
5724 fBuiltinIOCache,
5725 false /* fSetupMerge */,
5726 0 /* uMergeSource */,
5727 0 /* uMergeTarget */,
5728 aMediumAttachment,
5729 mMachineState,
5730 &rc);
5731 /* error handling is after resuming the VM */
5732
5733 if (mMachineState == MachineState_DeletingSnapshotOnline)
5734 {
5735 LogFlowFunc(("Resuming the VM...\n"));
5736 /* disable the callback to prevent Console-level state change */
5737 mVMStateChangeCallbackDisabled = true;
5738 int vrc2 = VMR3Resume(ptrVM.rawUVM(), VMRESUMEREASON_RECONFIG);
5739 mVMStateChangeCallbackDisabled = false;
5740 AssertRC(vrc2);
5741 if (RT_FAILURE(vrc2))
5742 {
5743 /* too bad, we failed. try to sync the console state with the VMM state */
5744 vmstateChangeCallback(ptrVM.rawUVM(), VMSTATE_SUSPENDED, enmVMState, this);
5745 }
5746 }
5747
5748 if (RT_FAILURE(vrc))
5749 return setError(E_FAIL, tr("%Rrc"), vrc);
5750 if (FAILED(rc))
5751 return rc;
5752
5753 return rc;
5754}
5755
5756
5757/**
5758 * Merely passes the call to Guest::enableVMMStatistics().
5759 */
5760void Console::enableVMMStatistics(BOOL aEnable)
5761{
5762 if (mGuest)
5763 mGuest->enableVMMStatistics(aEnable);
5764}
5765
5766/**
5767 * Worker for Console::Pause and internal entry point for pausing a VM for
5768 * a specific reason.
5769 */
5770HRESULT Console::pause(Reason_T aReason)
5771{
5772 LogFlowThisFuncEnter();
5773
5774 AutoCaller autoCaller(this);
5775 if (FAILED(autoCaller.rc())) return autoCaller.rc();
5776
5777 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
5778
5779 switch (mMachineState)
5780 {
5781 case MachineState_Running:
5782 case MachineState_Teleporting:
5783 case MachineState_LiveSnapshotting:
5784 break;
5785
5786 case MachineState_Paused:
5787 case MachineState_TeleportingPausedVM:
5788 case MachineState_Saving:
5789 return setError(VBOX_E_INVALID_VM_STATE, tr("Already paused"));
5790
5791 default:
5792 return setInvalidMachineStateError();
5793 }
5794
5795 /* get the VM handle. */
5796 SafeVMPtr ptrVM(this);
5797 if (!ptrVM.isOk())
5798 return ptrVM.rc();
5799
5800 /* release the lock before a VMR3* call (EMT will call us back)! */
5801 alock.release();
5802
5803 LogFlowThisFunc(("Sending PAUSE request...\n"));
5804 if (aReason != Reason_Unspecified)
5805 LogRel(("Pausing VM execution, reason \"%s\"\n", Global::stringifyReason(aReason)));
5806
5807 /** @todo r=klaus make use of aReason */
5808 VMSUSPENDREASON enmReason = VMSUSPENDREASON_USER;
5809 if (aReason == Reason_HostSuspend)
5810 enmReason = VMSUSPENDREASON_HOST_SUSPEND;
5811 else if (aReason == Reason_HostBatteryLow)
5812 enmReason = VMSUSPENDREASON_HOST_BATTERY_LOW;
5813 int vrc = VMR3Suspend(ptrVM.rawUVM(), enmReason);
5814
5815 HRESULT hrc = S_OK;
5816 if (RT_FAILURE(vrc))
5817 hrc = setError(VBOX_E_VM_ERROR, tr("Could not suspend the machine execution (%Rrc)"), vrc);
5818
5819 LogFlowThisFunc(("hrc=%Rhrc\n", hrc));
5820 LogFlowThisFuncLeave();
5821 return hrc;
5822}
5823
5824/**
5825 * Worker for Console::Resume and internal entry point for resuming a VM for
5826 * a specific reason.
5827 */
5828HRESULT Console::resume(Reason_T aReason)
5829{
5830 LogFlowThisFuncEnter();
5831
5832 AutoCaller autoCaller(this);
5833 if (FAILED(autoCaller.rc())) return autoCaller.rc();
5834
5835 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
5836
5837 if (mMachineState != MachineState_Paused)
5838 return setError(VBOX_E_INVALID_VM_STATE,
5839 tr("Cannot resume the machine as it is not paused (machine state: %s)"),
5840 Global::stringifyMachineState(mMachineState));
5841
5842 /* get the VM handle. */
5843 SafeVMPtr ptrVM(this);
5844 if (!ptrVM.isOk())
5845 return ptrVM.rc();
5846
5847 /* release the lock before a VMR3* call (EMT will call us back)! */
5848 alock.release();
5849
5850 LogFlowThisFunc(("Sending RESUME request...\n"));
5851 if (aReason != Reason_Unspecified)
5852 LogRel(("Resuming VM execution, reason \"%s\"\n", Global::stringifyReason(aReason)));
5853
5854 int vrc;
5855 if (VMR3GetStateU(ptrVM.rawUVM()) == VMSTATE_CREATED)
5856 {
5857#ifdef VBOX_WITH_EXTPACK
5858 vrc = mptrExtPackManager->callAllVmPowerOnHooks(this, VMR3GetVM(ptrVM.rawUVM()));
5859#else
5860 vrc = VINF_SUCCESS;
5861#endif
5862 if (RT_SUCCESS(vrc))
5863 vrc = VMR3PowerOn(ptrVM.rawUVM()); /* (PowerUpPaused) */
5864 }
5865 else
5866 {
5867 VMRESUMEREASON enmReason = VMRESUMEREASON_USER;
5868 if (aReason == Reason_HostResume)
5869 enmReason = VMRESUMEREASON_HOST_RESUME;
5870 vrc = VMR3Resume(ptrVM.rawUVM(), enmReason);
5871 }
5872
5873 HRESULT rc = RT_SUCCESS(vrc) ? S_OK :
5874 setError(VBOX_E_VM_ERROR,
5875 tr("Could not resume the machine execution (%Rrc)"),
5876 vrc);
5877
5878 LogFlowThisFunc(("rc=%Rhrc\n", rc));
5879 LogFlowThisFuncLeave();
5880 return rc;
5881}
5882
5883/**
5884 * Worker for Console::SaveState and internal entry point for saving state of
5885 * a VM for a specific reason.
5886 */
5887HRESULT Console::saveState(Reason_T aReason, IProgress **aProgress)
5888{
5889 LogFlowThisFuncEnter();
5890
5891 CheckComArgOutPointerValid(aProgress);
5892
5893 AutoCaller autoCaller(this);
5894 if (FAILED(autoCaller.rc())) return autoCaller.rc();
5895
5896 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
5897
5898 LogFlowThisFunc(("mMachineState=%d\n", mMachineState));
5899 if ( mMachineState != MachineState_Running
5900 && mMachineState != MachineState_Paused)
5901 {
5902 return setError(VBOX_E_INVALID_VM_STATE,
5903 tr("Cannot save the execution state as the machine is not running or paused (machine state: %s)"),
5904 Global::stringifyMachineState(mMachineState));
5905 }
5906
5907 if (aReason != Reason_Unspecified)
5908 LogRel(("Saving state of VM, reason \"%s\"\n", Global::stringifyReason(aReason)));
5909
5910 /* memorize the current machine state */
5911 MachineState_T lastMachineState = mMachineState;
5912
5913 if (mMachineState == MachineState_Running)
5914 {
5915 /* get the VM handle. */
5916 SafeVMPtr ptrVM(this);
5917 if (!ptrVM.isOk())
5918 return ptrVM.rc();
5919
5920 /* release the lock before a VMR3* call (EMT will call us back)! */
5921 alock.release();
5922 VMSUSPENDREASON enmReason = VMSUSPENDREASON_USER;
5923 if (aReason == Reason_HostSuspend)
5924 enmReason = VMSUSPENDREASON_HOST_SUSPEND;
5925 else if (aReason == Reason_HostBatteryLow)
5926 enmReason = VMSUSPENDREASON_HOST_BATTERY_LOW;
5927 int vrc = VMR3Suspend(ptrVM.rawUVM(), enmReason);
5928 alock.acquire();
5929
5930 HRESULT hrc = S_OK;
5931 if (RT_FAILURE(vrc))
5932 hrc = setError(VBOX_E_VM_ERROR, tr("Could not suspend the machine execution (%Rrc)"), vrc);
5933 if (FAILED(hrc))
5934 return hrc;
5935 }
5936
5937 HRESULT rc = S_OK;
5938 bool fBeganSavingState = false;
5939 bool fTaskCreationFailed = false;
5940
5941 do
5942 {
5943 ComPtr<IProgress> pProgress;
5944 Bstr stateFilePath;
5945
5946 /*
5947 * request a saved state file path from the server
5948 * (this will set the machine state to Saving on the server to block
5949 * others from accessing this machine)
5950 */
5951 rc = mControl->BeginSavingState(pProgress.asOutParam(),
5952 stateFilePath.asOutParam());
5953 if (FAILED(rc))
5954 break;
5955
5956 fBeganSavingState = true;
5957
5958 /* sync the state with the server */
5959 setMachineStateLocally(MachineState_Saving);
5960
5961 /* ensure the directory for the saved state file exists */
5962 {
5963 Utf8Str dir = stateFilePath;
5964 dir.stripFilename();
5965 if (!RTDirExists(dir.c_str()))
5966 {
5967 int vrc = RTDirCreateFullPath(dir.c_str(), 0700);
5968 if (RT_FAILURE(vrc))
5969 {
5970 rc = setError(VBOX_E_FILE_ERROR,
5971 tr("Could not create a directory '%s' to save the state to (%Rrc)"),
5972 dir.c_str(), vrc);
5973 break;
5974 }
5975 }
5976 }
5977
5978 /* Create a task object early to ensure mpUVM protection is successful. */
5979 std::auto_ptr<VMSaveTask> task(new VMSaveTask(this, pProgress,
5980 stateFilePath,
5981 lastMachineState,
5982 aReason));
5983 rc = task->rc();
5984 /*
5985 * If we fail here it means a PowerDown() call happened on another
5986 * thread while we were doing Pause() (which releases the Console lock).
5987 * We assign PowerDown() a higher precedence than SaveState(),
5988 * therefore just return the error to the caller.
5989 */
5990 if (FAILED(rc))
5991 {
5992 fTaskCreationFailed = true;
5993 break;
5994 }
5995
5996 /* create a thread to wait until the VM state is saved */
5997 int vrc = RTThreadCreate(NULL, Console::saveStateThread, (void *)task.get(),
5998 0, RTTHREADTYPE_MAIN_WORKER, 0, "VMSave");
5999 if (RT_FAILURE(vrc))
6000 {
6001 rc = setError(E_FAIL, "Could not create VMSave thread (%Rrc)", vrc);
6002 break;
6003 }
6004
6005 /* task is now owned by saveStateThread(), so release it */
6006 task.release();
6007
6008 /* return the progress to the caller */
6009 pProgress.queryInterfaceTo(aProgress);
6010 } while (0);
6011
6012 if (FAILED(rc) && !fTaskCreationFailed)
6013 {
6014 /* preserve existing error info */
6015 ErrorInfoKeeper eik;
6016
6017 if (fBeganSavingState)
6018 {
6019 /*
6020 * cancel the requested save state procedure.
6021 * This will reset the machine state to the state it had right
6022 * before calling mControl->BeginSavingState().
6023 */
6024 mControl->EndSavingState(eik.getResultCode(), eik.getText().raw());
6025 }
6026
6027 if (lastMachineState == MachineState_Running)
6028 {
6029 /* restore the paused state if appropriate */
6030 setMachineStateLocally(MachineState_Paused);
6031 /* restore the running state if appropriate */
6032 SafeVMPtr ptrVM(this);
6033 if (ptrVM.isOk())
6034 {
6035 alock.release();
6036 VMR3Resume(ptrVM.rawUVM(), VMRESUMEREASON_STATE_RESTORED);
6037 alock.acquire();
6038 }
6039 }
6040 else
6041 setMachineStateLocally(lastMachineState);
6042 }
6043
6044 LogFlowThisFunc(("rc=%Rhrc\n", rc));
6045 LogFlowThisFuncLeave();
6046 return rc;
6047}
6048
6049/**
6050 * Gets called by Session::UpdateMachineState()
6051 * (IInternalSessionControl::updateMachineState()).
6052 *
6053 * Must be called only in certain cases (see the implementation).
6054 *
6055 * @note Locks this object for writing.
6056 */
6057HRESULT Console::updateMachineState(MachineState_T aMachineState)
6058{
6059 AutoCaller autoCaller(this);
6060 AssertComRCReturnRC(autoCaller.rc());
6061
6062 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
6063
6064 AssertReturn( mMachineState == MachineState_Saving
6065 || mMachineState == MachineState_LiveSnapshotting
6066 || mMachineState == MachineState_RestoringSnapshot
6067 || mMachineState == MachineState_DeletingSnapshot
6068 || mMachineState == MachineState_DeletingSnapshotOnline
6069 || mMachineState == MachineState_DeletingSnapshotPaused
6070 , E_FAIL);
6071
6072 return setMachineStateLocally(aMachineState);
6073}
6074
6075#ifdef CONSOLE_WITH_EVENT_CACHE
6076/**
6077 * @note Locks this object for writing.
6078 */
6079#endif
6080void Console::onMousePointerShapeChange(bool fVisible, bool fAlpha,
6081 uint32_t xHot, uint32_t yHot,
6082 uint32_t width, uint32_t height,
6083 ComSafeArrayIn(BYTE,pShape))
6084{
6085#if 0
6086 LogFlowThisFuncEnter();
6087 LogFlowThisFunc(("fVisible=%d, fAlpha=%d, xHot = %d, yHot = %d, width=%d, height=%d, shape=%p\n",
6088 fVisible, fAlpha, xHot, yHot, width, height, pShape));
6089#endif
6090
6091 AutoCaller autoCaller(this);
6092 AssertComRCReturnVoid(autoCaller.rc());
6093
6094#ifdef CONSOLE_WITH_EVENT_CACHE
6095 {
6096 /* We need a write lock because we alter the cached callback data */
6097 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
6098
6099 /* Save the callback arguments */
6100 mCallbackData.mpsc.visible = fVisible;
6101 mCallbackData.mpsc.alpha = fAlpha;
6102 mCallbackData.mpsc.xHot = xHot;
6103 mCallbackData.mpsc.yHot = yHot;
6104 mCallbackData.mpsc.width = width;
6105 mCallbackData.mpsc.height = height;
6106
6107 /* start with not valid */
6108 bool wasValid = mCallbackData.mpsc.valid;
6109 mCallbackData.mpsc.valid = false;
6110
6111 com::SafeArray<BYTE> aShape(ComSafeArrayInArg(pShape));
6112 if (aShape.size() != 0)
6113 mCallbackData.mpsc.shape.initFrom(aShape);
6114 else
6115 mCallbackData.mpsc.shape.resize(0);
6116 mCallbackData.mpsc.valid = true;
6117 }
6118#endif
6119
6120 fireMousePointerShapeChangedEvent(mEventSource, fVisible, fAlpha, xHot, yHot, width, height, ComSafeArrayInArg(pShape));
6121
6122#if 0
6123 LogFlowThisFuncLeave();
6124#endif
6125}
6126
6127#ifdef CONSOLE_WITH_EVENT_CACHE
6128/**
6129 * @note Locks this object for writing.
6130 */
6131#endif
6132void Console::onMouseCapabilityChange(BOOL supportsAbsolute, BOOL supportsRelative,
6133 BOOL supportsMT, BOOL needsHostCursor)
6134{
6135 LogFlowThisFunc(("supportsAbsolute=%d supportsRelative=%d needsHostCursor=%d\n",
6136 supportsAbsolute, supportsRelative, needsHostCursor));
6137
6138 AutoCaller autoCaller(this);
6139 AssertComRCReturnVoid(autoCaller.rc());
6140
6141#ifdef CONSOLE_WITH_EVENT_CACHE
6142 {
6143 /* We need a write lock because we alter the cached callback data */
6144 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
6145
6146 /* save the callback arguments */
6147 mCallbackData.mcc.supportsAbsolute = supportsAbsolute;
6148 mCallbackData.mcc.supportsRelative = supportsRelative;
6149 mCallbackData.mcc.needsHostCursor = needsHostCursor;
6150 mCallbackData.mcc.valid = true;
6151 }
6152#endif
6153
6154 fireMouseCapabilityChangedEvent(mEventSource, supportsAbsolute, supportsRelative, supportsMT, needsHostCursor);
6155}
6156
6157void Console::onStateChange(MachineState_T machineState)
6158{
6159 AutoCaller autoCaller(this);
6160 AssertComRCReturnVoid(autoCaller.rc());
6161 fireStateChangedEvent(mEventSource, machineState);
6162}
6163
6164void Console::onAdditionsStateChange()
6165{
6166 AutoCaller autoCaller(this);
6167 AssertComRCReturnVoid(autoCaller.rc());
6168
6169 fireAdditionsStateChangedEvent(mEventSource);
6170}
6171
6172/**
6173 * @remarks This notification only is for reporting an incompatible
6174 * Guest Additions interface, *not* the Guest Additions version!
6175 *
6176 * The user will be notified inside the guest if new Guest
6177 * Additions are available (via VBoxTray/VBoxClient).
6178 */
6179void Console::onAdditionsOutdated()
6180{
6181 AutoCaller autoCaller(this);
6182 AssertComRCReturnVoid(autoCaller.rc());
6183
6184 /** @todo implement this */
6185}
6186
6187#ifdef CONSOLE_WITH_EVENT_CACHE
6188/**
6189 * @note Locks this object for writing.
6190 */
6191#endif
6192void Console::onKeyboardLedsChange(bool fNumLock, bool fCapsLock, bool fScrollLock)
6193{
6194 AutoCaller autoCaller(this);
6195 AssertComRCReturnVoid(autoCaller.rc());
6196
6197#ifdef CONSOLE_WITH_EVENT_CACHE
6198 {
6199 /* We need a write lock because we alter the cached callback data */
6200 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
6201
6202 /* save the callback arguments */
6203 mCallbackData.klc.numLock = fNumLock;
6204 mCallbackData.klc.capsLock = fCapsLock;
6205 mCallbackData.klc.scrollLock = fScrollLock;
6206 mCallbackData.klc.valid = true;
6207 }
6208#endif
6209
6210 fireKeyboardLedsChangedEvent(mEventSource, fNumLock, fCapsLock, fScrollLock);
6211}
6212
6213void Console::onUSBDeviceStateChange(IUSBDevice *aDevice, bool aAttached,
6214 IVirtualBoxErrorInfo *aError)
6215{
6216 AutoCaller autoCaller(this);
6217 AssertComRCReturnVoid(autoCaller.rc());
6218
6219 fireUSBDeviceStateChangedEvent(mEventSource, aDevice, aAttached, aError);
6220}
6221
6222void Console::onRuntimeError(BOOL aFatal, IN_BSTR aErrorID, IN_BSTR aMessage)
6223{
6224 AutoCaller autoCaller(this);
6225 AssertComRCReturnVoid(autoCaller.rc());
6226
6227 fireRuntimeErrorEvent(mEventSource, aFatal, aErrorID, aMessage);
6228}
6229
6230HRESULT Console::onShowWindow(BOOL aCheck, BOOL *aCanShow, LONG64 *aWinId)
6231{
6232 AssertReturn(aCanShow, E_POINTER);
6233 AssertReturn(aWinId, E_POINTER);
6234
6235 *aCanShow = FALSE;
6236 *aWinId = 0;
6237
6238 AutoCaller autoCaller(this);
6239 AssertComRCReturnRC(autoCaller.rc());
6240
6241 VBoxEventDesc evDesc;
6242 if (aCheck)
6243 {
6244 evDesc.init(mEventSource, VBoxEventType_OnCanShowWindow);
6245 BOOL fDelivered = evDesc.fire(5000); /* Wait up to 5 secs for delivery */
6246 //Assert(fDelivered);
6247 if (fDelivered)
6248 {
6249 ComPtr<IEvent> pEvent;
6250 evDesc.getEvent(pEvent.asOutParam());
6251 // bit clumsy
6252 ComPtr<ICanShowWindowEvent> pCanShowEvent = pEvent;
6253 if (pCanShowEvent)
6254 {
6255 BOOL fVetoed = FALSE;
6256 pCanShowEvent->IsVetoed(&fVetoed);
6257 *aCanShow = !fVetoed;
6258 }
6259 else
6260 {
6261 AssertFailed();
6262 *aCanShow = TRUE;
6263 }
6264 }
6265 else
6266 *aCanShow = TRUE;
6267 }
6268 else
6269 {
6270 evDesc.init(mEventSource, VBoxEventType_OnShowWindow, INT64_C(0));
6271 BOOL fDelivered = evDesc.fire(5000); /* Wait up to 5 secs for delivery */
6272 //Assert(fDelivered);
6273 if (fDelivered)
6274 {
6275 ComPtr<IEvent> pEvent;
6276 evDesc.getEvent(pEvent.asOutParam());
6277 ComPtr<IShowWindowEvent> pShowEvent = pEvent;
6278 if (pShowEvent)
6279 {
6280 LONG64 iEvWinId = 0;
6281 pShowEvent->COMGETTER(WinId)(&iEvWinId);
6282 if (iEvWinId != 0 && *aWinId == 0)
6283 *aWinId = iEvWinId;
6284 }
6285 else
6286 AssertFailed();
6287 }
6288 }
6289
6290 return S_OK;
6291}
6292
6293// private methods
6294////////////////////////////////////////////////////////////////////////////////
6295
6296/**
6297 * Increases the usage counter of the mpUVM pointer.
6298 *
6299 * Guarantees that VMR3Destroy() will not be called on it at least until
6300 * releaseVMCaller() is called.
6301 *
6302 * If this method returns a failure, the caller is not allowed to use mpUVM and
6303 * may return the failed result code to the upper level. This method sets the
6304 * extended error info on failure if \a aQuiet is false.
6305 *
6306 * Setting \a aQuiet to true is useful for methods that don't want to return
6307 * the failed result code to the caller when this method fails (e.g. need to
6308 * silently check for the mpUVM availability).
6309 *
6310 * When mpUVM is NULL but \a aAllowNullVM is true, a corresponding error will be
6311 * returned instead of asserting. Having it false is intended as a sanity check
6312 * for methods that have checked mMachineState and expect mpUVM *NOT* to be
6313 * NULL.
6314 *
6315 * @param aQuiet true to suppress setting error info
6316 * @param aAllowNullVM true to accept mpUVM being NULL and return a failure
6317 * (otherwise this method will assert if mpUVM is NULL)
6318 *
6319 * @note Locks this object for writing.
6320 */
6321HRESULT Console::addVMCaller(bool aQuiet /* = false */,
6322 bool aAllowNullVM /* = false */)
6323{
6324 AutoCaller autoCaller(this);
6325 /** @todo Fix race during console/VM reference destruction, refer @bugref{6318}
6326 * comment 25. */
6327 if (FAILED(autoCaller.rc()))
6328 return autoCaller.rc();
6329
6330 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
6331
6332 if (mVMDestroying)
6333 {
6334 /* powerDown() is waiting for all callers to finish */
6335 return aQuiet ? E_ACCESSDENIED : setError(E_ACCESSDENIED,
6336 tr("The virtual machine is being powered down"));
6337 }
6338
6339 if (mpUVM == NULL)
6340 {
6341 Assert(aAllowNullVM == true);
6342
6343 /* The machine is not powered up */
6344 return aQuiet ? E_ACCESSDENIED : setError(E_ACCESSDENIED,
6345 tr("The virtual machine is not powered up"));
6346 }
6347
6348 ++mVMCallers;
6349
6350 return S_OK;
6351}
6352
6353/**
6354 * Decreases the usage counter of the mpUVM pointer.
6355 *
6356 * Must always complete the addVMCaller() call after the mpUVM pointer is no
6357 * more necessary.
6358 *
6359 * @note Locks this object for writing.
6360 */
6361void Console::releaseVMCaller()
6362{
6363 AutoCaller autoCaller(this);
6364 AssertComRCReturnVoid(autoCaller.rc());
6365
6366 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
6367
6368 AssertReturnVoid(mpUVM != NULL);
6369
6370 Assert(mVMCallers > 0);
6371 --mVMCallers;
6372
6373 if (mVMCallers == 0 && mVMDestroying)
6374 {
6375 /* inform powerDown() there are no more callers */
6376 RTSemEventSignal(mVMZeroCallersSem);
6377 }
6378}
6379
6380
6381HRESULT Console::safeVMPtrRetainer(PUVM *a_ppUVM, bool a_Quiet)
6382{
6383 *a_ppUVM = NULL;
6384
6385 AutoCaller autoCaller(this);
6386 AssertComRCReturnRC(autoCaller.rc());
6387 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
6388
6389 /*
6390 * Repeat the checks done by addVMCaller.
6391 */
6392 if (mVMDestroying) /* powerDown() is waiting for all callers to finish */
6393 return a_Quiet
6394 ? E_ACCESSDENIED
6395 : setError(E_ACCESSDENIED, tr("The virtual machine is being powered down"));
6396 PUVM pUVM = mpUVM;
6397 if (!pUVM)
6398 return a_Quiet
6399 ? E_ACCESSDENIED
6400 : setError(E_ACCESSDENIED, tr("The virtual machine is powered off"));
6401
6402 /*
6403 * Retain a reference to the user mode VM handle and get the global handle.
6404 */
6405 uint32_t cRefs = VMR3RetainUVM(pUVM);
6406 if (cRefs == UINT32_MAX)
6407 return a_Quiet
6408 ? E_ACCESSDENIED
6409 : setError(E_ACCESSDENIED, tr("The virtual machine is powered off"));
6410
6411 /* done */
6412 *a_ppUVM = pUVM;
6413 return S_OK;
6414}
6415
6416void Console::safeVMPtrReleaser(PUVM *a_ppUVM)
6417{
6418 if (*a_ppUVM)
6419 VMR3ReleaseUVM(*a_ppUVM);
6420 *a_ppUVM = NULL;
6421}
6422
6423
6424/**
6425 * Initialize the release logging facility. In case something
6426 * goes wrong, there will be no release logging. Maybe in the future
6427 * we can add some logic to use different file names in this case.
6428 * Note that the logic must be in sync with Machine::DeleteSettings().
6429 */
6430HRESULT Console::consoleInitReleaseLog(const ComPtr<IMachine> aMachine)
6431{
6432 HRESULT hrc = S_OK;
6433
6434 Bstr logFolder;
6435 hrc = aMachine->COMGETTER(LogFolder)(logFolder.asOutParam());
6436 if (FAILED(hrc))
6437 return hrc;
6438
6439 Utf8Str logDir = logFolder;
6440
6441 /* make sure the Logs folder exists */
6442 Assert(logDir.length());
6443 if (!RTDirExists(logDir.c_str()))
6444 RTDirCreateFullPath(logDir.c_str(), 0700);
6445
6446 Utf8Str logFile = Utf8StrFmt("%s%cVBox.log",
6447 logDir.c_str(), RTPATH_DELIMITER);
6448 Utf8Str pngFile = Utf8StrFmt("%s%cVBox.png",
6449 logDir.c_str(), RTPATH_DELIMITER);
6450
6451 /*
6452 * Age the old log files
6453 * Rename .(n-1) to .(n), .(n-2) to .(n-1), ..., and the last log file to .1
6454 * Overwrite target files in case they exist.
6455 */
6456 ComPtr<IVirtualBox> pVirtualBox;
6457 aMachine->COMGETTER(Parent)(pVirtualBox.asOutParam());
6458 ComPtr<ISystemProperties> pSystemProperties;
6459 pVirtualBox->COMGETTER(SystemProperties)(pSystemProperties.asOutParam());
6460 ULONG cHistoryFiles = 3;
6461 pSystemProperties->COMGETTER(LogHistoryCount)(&cHistoryFiles);
6462 if (cHistoryFiles)
6463 {
6464 for (int i = cHistoryFiles-1; i >= 0; i--)
6465 {
6466 Utf8Str *files[] = { &logFile, &pngFile };
6467 Utf8Str oldName, newName;
6468
6469 for (unsigned int j = 0; j < RT_ELEMENTS(files); ++j)
6470 {
6471 if (i > 0)
6472 oldName = Utf8StrFmt("%s.%d", files[j]->c_str(), i);
6473 else
6474 oldName = *files[j];
6475 newName = Utf8StrFmt("%s.%d", files[j]->c_str(), i + 1);
6476 /* If the old file doesn't exist, delete the new file (if it
6477 * exists) to provide correct rotation even if the sequence is
6478 * broken */
6479 if ( RTFileRename(oldName.c_str(), newName.c_str(), RTFILEMOVE_FLAGS_REPLACE)
6480 == VERR_FILE_NOT_FOUND)
6481 RTFileDelete(newName.c_str());
6482 }
6483 }
6484 }
6485
6486 char szError[RTPATH_MAX + 128];
6487 int vrc = com::VBoxLogRelCreate("VM", logFile.c_str(),
6488 RTLOGFLAGS_PREFIX_TIME_PROG | RTLOGFLAGS_RESTRICT_GROUPS,
6489 "all all.restrict -default.restrict",
6490 "VBOX_RELEASE_LOG", RTLOGDEST_FILE,
6491 32768 /* cMaxEntriesPerGroup */,
6492 0 /* cHistory */, 0 /* uHistoryFileTime */,
6493 0 /* uHistoryFileSize */, szError, sizeof(szError));
6494 if (RT_FAILURE(vrc))
6495 hrc = setError(E_FAIL, tr("Failed to open release log (%s, %Rrc)"),
6496 szError, vrc);
6497
6498 /* If we've made any directory changes, flush the directory to increase
6499 the likelihood that the log file will be usable after a system panic.
6500
6501 Tip: Try 'export VBOX_RELEASE_LOG_FLAGS=flush' if the last bits of the log
6502 is missing. Just don't have too high hopes for this to help. */
6503 if (SUCCEEDED(hrc) || cHistoryFiles)
6504 RTDirFlush(logDir.c_str());
6505
6506 return hrc;
6507}
6508
6509/**
6510 * Common worker for PowerUp and PowerUpPaused.
6511 *
6512 * @returns COM status code.
6513 *
6514 * @param aProgress Where to return the progress object.
6515 * @param aPaused true if PowerUpPaused called.
6516 */
6517HRESULT Console::powerUp(IProgress **aProgress, bool aPaused)
6518{
6519
6520 LogFlowThisFuncEnter();
6521
6522 CheckComArgOutPointerValid(aProgress);
6523
6524 AutoCaller autoCaller(this);
6525 if (FAILED(autoCaller.rc())) return autoCaller.rc();
6526
6527 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
6528
6529 LogFlowThisFunc(("mMachineState=%d\n", mMachineState));
6530 HRESULT rc = S_OK;
6531 ComObjPtr<Progress> pPowerupProgress;
6532 bool fBeganPoweringUp = false;
6533
6534 LONG cOperations = 1;
6535 LONG ulTotalOperationsWeight = 1;
6536
6537 try
6538 {
6539
6540 if (Global::IsOnlineOrTransient(mMachineState))
6541 throw setError(VBOX_E_INVALID_VM_STATE,
6542 tr("The virtual machine is already running or busy (machine state: %s)"),
6543 Global::stringifyMachineState(mMachineState));
6544
6545 /* Set up release logging as early as possible after the check if
6546 * there is already a running VM which we shouldn't disturb. */
6547 rc = consoleInitReleaseLog(mMachine);
6548 if (FAILED(rc))
6549 throw rc;
6550
6551 /* test and clear the TeleporterEnabled property */
6552 BOOL fTeleporterEnabled;
6553 rc = mMachine->COMGETTER(TeleporterEnabled)(&fTeleporterEnabled);
6554 if (FAILED(rc))
6555 throw rc;
6556
6557#if 0 /** @todo we should save it afterwards, but that isn't necessarily a good idea. Find a better place for this (VBoxSVC). */
6558 if (fTeleporterEnabled)
6559 {
6560 rc = mMachine->COMSETTER(TeleporterEnabled)(FALSE);
6561 if (FAILED(rc))
6562 throw rc;
6563 }
6564#endif
6565
6566 /* test the FaultToleranceState property */
6567 FaultToleranceState_T enmFaultToleranceState;
6568 rc = mMachine->COMGETTER(FaultToleranceState)(&enmFaultToleranceState);
6569 if (FAILED(rc))
6570 throw rc;
6571 BOOL fFaultToleranceSyncEnabled = (enmFaultToleranceState == FaultToleranceState_Standby);
6572
6573 /* Create a progress object to track progress of this operation. Must
6574 * be done as early as possible (together with BeginPowerUp()) as this
6575 * is vital for communicating as much as possible early powerup
6576 * failure information to the API caller */
6577 pPowerupProgress.createObject();
6578 Bstr progressDesc;
6579 if (mMachineState == MachineState_Saved)
6580 progressDesc = tr("Restoring virtual machine");
6581 else if (fTeleporterEnabled)
6582 progressDesc = tr("Teleporting virtual machine");
6583 else if (fFaultToleranceSyncEnabled)
6584 progressDesc = tr("Fault Tolerance syncing of remote virtual machine");
6585 else
6586 progressDesc = tr("Starting virtual machine");
6587
6588 /* Check all types of shared folders and compose a single list */
6589 SharedFolderDataMap sharedFolders;
6590 {
6591 /* first, insert global folders */
6592 for (SharedFolderDataMap::const_iterator it = m_mapGlobalSharedFolders.begin();
6593 it != m_mapGlobalSharedFolders.end();
6594 ++it)
6595 {
6596 const SharedFolderData &d = it->second;
6597 sharedFolders[it->first] = d;
6598 }
6599
6600 /* second, insert machine folders */
6601 for (SharedFolderDataMap::const_iterator it = m_mapMachineSharedFolders.begin();
6602 it != m_mapMachineSharedFolders.end();
6603 ++it)
6604 {
6605 const SharedFolderData &d = it->second;
6606 sharedFolders[it->first] = d;
6607 }
6608
6609 /* third, insert console folders */
6610 for (SharedFolderMap::const_iterator it = m_mapSharedFolders.begin();
6611 it != m_mapSharedFolders.end();
6612 ++it)
6613 {
6614 SharedFolder *pSF = it->second;
6615 AutoCaller sfCaller(pSF);
6616 AutoReadLock sfLock(pSF COMMA_LOCKVAL_SRC_POS);
6617 sharedFolders[it->first] = SharedFolderData(pSF->getHostPath(),
6618 pSF->isWritable(),
6619 pSF->isAutoMounted());
6620 }
6621 }
6622
6623 Bstr savedStateFile;
6624
6625 /*
6626 * Saved VMs will have to prove that their saved states seem kosher.
6627 */
6628 if (mMachineState == MachineState_Saved)
6629 {
6630 rc = mMachine->COMGETTER(StateFilePath)(savedStateFile.asOutParam());
6631 if (FAILED(rc))
6632 throw rc;
6633 ComAssertRet(!savedStateFile.isEmpty(), E_FAIL);
6634 int vrc = SSMR3ValidateFile(Utf8Str(savedStateFile).c_str(), false /* fChecksumIt */);
6635 if (RT_FAILURE(vrc))
6636 throw setError(VBOX_E_FILE_ERROR,
6637 tr("VM cannot start because the saved state file '%ls' is invalid (%Rrc). Delete the saved state prior to starting the VM"),
6638 savedStateFile.raw(), vrc);
6639 }
6640
6641 /* Setup task object and thread to carry out the operaton
6642 * Asycnhronously */
6643 std::auto_ptr<VMPowerUpTask> task(new VMPowerUpTask(this, pPowerupProgress));
6644 ComAssertComRCRetRC(task->rc());
6645
6646 task->mConfigConstructor = configConstructor;
6647 task->mSharedFolders = sharedFolders;
6648 task->mStartPaused = aPaused;
6649 if (mMachineState == MachineState_Saved)
6650 task->mSavedStateFile = savedStateFile;
6651 task->mTeleporterEnabled = fTeleporterEnabled;
6652 task->mEnmFaultToleranceState = enmFaultToleranceState;
6653
6654 /* Reset differencing hard disks for which autoReset is true,
6655 * but only if the machine has no snapshots OR the current snapshot
6656 * is an OFFLINE snapshot; otherwise we would reset the current
6657 * differencing image of an ONLINE snapshot which contains the disk
6658 * state of the machine while it was previously running, but without
6659 * the corresponding machine state, which is equivalent to powering
6660 * off a running machine and not good idea
6661 */
6662 ComPtr<ISnapshot> pCurrentSnapshot;
6663 rc = mMachine->COMGETTER(CurrentSnapshot)(pCurrentSnapshot.asOutParam());
6664 if (FAILED(rc))
6665 throw rc;
6666
6667 BOOL fCurrentSnapshotIsOnline = false;
6668 if (pCurrentSnapshot)
6669 {
6670 rc = pCurrentSnapshot->COMGETTER(Online)(&fCurrentSnapshotIsOnline);
6671 if (FAILED(rc))
6672 throw rc;
6673 }
6674
6675 if (!fCurrentSnapshotIsOnline)
6676 {
6677 LogFlowThisFunc(("Looking for immutable images to reset\n"));
6678
6679 com::SafeIfaceArray<IMediumAttachment> atts;
6680 rc = mMachine->COMGETTER(MediumAttachments)(ComSafeArrayAsOutParam(atts));
6681 if (FAILED(rc))
6682 throw rc;
6683
6684 for (size_t i = 0;
6685 i < atts.size();
6686 ++i)
6687 {
6688 DeviceType_T devType;
6689 rc = atts[i]->COMGETTER(Type)(&devType);
6690 /** @todo later applies to floppies as well */
6691 if (devType == DeviceType_HardDisk)
6692 {
6693 ComPtr<IMedium> pMedium;
6694 rc = atts[i]->COMGETTER(Medium)(pMedium.asOutParam());
6695 if (FAILED(rc))
6696 throw rc;
6697
6698 /* needs autoreset? */
6699 BOOL autoReset = FALSE;
6700 rc = pMedium->COMGETTER(AutoReset)(&autoReset);
6701 if (FAILED(rc))
6702 throw rc;
6703
6704 if (autoReset)
6705 {
6706 ComPtr<IProgress> pResetProgress;
6707 rc = pMedium->Reset(pResetProgress.asOutParam());
6708 if (FAILED(rc))
6709 throw rc;
6710
6711 /* save for later use on the powerup thread */
6712 task->hardDiskProgresses.push_back(pResetProgress);
6713 }
6714 }
6715 }
6716 }
6717 else
6718 LogFlowThisFunc(("Machine has a current snapshot which is online, skipping immutable images reset\n"));
6719
6720 /* setup task object and thread to carry out the operation
6721 * asynchronously */
6722
6723#ifdef VBOX_WITH_EXTPACK
6724 mptrExtPackManager->dumpAllToReleaseLog();
6725#endif
6726
6727#ifdef RT_OS_SOLARIS
6728 /* setup host core dumper for the VM */
6729 Bstr value;
6730 HRESULT hrc = mMachine->GetExtraData(Bstr("VBoxInternal2/CoreDumpEnabled").raw(), value.asOutParam());
6731 if (SUCCEEDED(hrc) && value == "1")
6732 {
6733 Bstr coreDumpDir, coreDumpReplaceSys, coreDumpLive;
6734 mMachine->GetExtraData(Bstr("VBoxInternal2/CoreDumpDir").raw(), coreDumpDir.asOutParam());
6735 mMachine->GetExtraData(Bstr("VBoxInternal2/CoreDumpReplaceSystemDump").raw(), coreDumpReplaceSys.asOutParam());
6736 mMachine->GetExtraData(Bstr("VBoxInternal2/CoreDumpLive").raw(), coreDumpLive.asOutParam());
6737
6738 uint32_t fCoreFlags = 0;
6739 if ( coreDumpReplaceSys.isEmpty() == false
6740 && Utf8Str(coreDumpReplaceSys).toUInt32() == 1)
6741 fCoreFlags |= RTCOREDUMPER_FLAGS_REPLACE_SYSTEM_DUMP;
6742
6743 if ( coreDumpLive.isEmpty() == false
6744 && Utf8Str(coreDumpLive).toUInt32() == 1)
6745 fCoreFlags |= RTCOREDUMPER_FLAGS_LIVE_CORE;
6746
6747 Utf8Str strDumpDir(coreDumpDir);
6748 const char *pszDumpDir = strDumpDir.c_str();
6749 if ( pszDumpDir
6750 && *pszDumpDir == '\0')
6751 pszDumpDir = NULL;
6752
6753 int vrc;
6754 if ( pszDumpDir
6755 && !RTDirExists(pszDumpDir))
6756 {
6757 /*
6758 * Try create the directory.
6759 */
6760 vrc = RTDirCreateFullPath(pszDumpDir, 0700);
6761 if (RT_FAILURE(vrc))
6762 throw setError(E_FAIL, "Failed to setup CoreDumper. Couldn't create dump directory '%s' (%Rrc)\n", pszDumpDir, vrc);
6763 }
6764
6765 vrc = RTCoreDumperSetup(pszDumpDir, fCoreFlags);
6766 if (RT_FAILURE(vrc))
6767 throw setError(E_FAIL, "Failed to setup CoreDumper (%Rrc)", vrc);
6768 else
6769 LogRel(("CoreDumper setup successful. pszDumpDir=%s fFlags=%#x\n", pszDumpDir ? pszDumpDir : ".", fCoreFlags));
6770 }
6771#endif
6772
6773
6774 // If there is immutable drive the process that.
6775 VMPowerUpTask::ProgressList progresses(task->hardDiskProgresses);
6776 if (aProgress && progresses.size() > 0){
6777
6778 for (VMPowerUpTask::ProgressList::const_iterator it = progresses.begin(); it != progresses.end(); ++it)
6779 {
6780 ++cOperations;
6781 ulTotalOperationsWeight += 1;
6782 }
6783 rc = pPowerupProgress->init(static_cast<IConsole *>(this),
6784 progressDesc.raw(),
6785 TRUE, // Cancelable
6786 cOperations,
6787 ulTotalOperationsWeight,
6788 Bstr(tr("Starting Hard Disk operations")).raw(),
6789 1,
6790 NULL);
6791 AssertComRCReturnRC(rc);
6792 }
6793 else if ( mMachineState == MachineState_Saved
6794 || (!fTeleporterEnabled && !fFaultToleranceSyncEnabled))
6795 {
6796 rc = pPowerupProgress->init(static_cast<IConsole *>(this),
6797 progressDesc.raw(),
6798 FALSE /* aCancelable */);
6799 }
6800 else if (fTeleporterEnabled)
6801 {
6802 rc = pPowerupProgress->init(static_cast<IConsole *>(this),
6803 progressDesc.raw(),
6804 TRUE /* aCancelable */,
6805 3 /* cOperations */,
6806 10 /* ulTotalOperationsWeight */,
6807 Bstr(tr("Teleporting virtual machine")).raw(),
6808 1 /* ulFirstOperationWeight */,
6809 NULL);
6810 }
6811 else if (fFaultToleranceSyncEnabled)
6812 {
6813 rc = pPowerupProgress->init(static_cast<IConsole *>(this),
6814 progressDesc.raw(),
6815 TRUE /* aCancelable */,
6816 3 /* cOperations */,
6817 10 /* ulTotalOperationsWeight */,
6818 Bstr(tr("Fault Tolerance syncing of remote virtual machine")).raw(),
6819 1 /* ulFirstOperationWeight */,
6820 NULL);
6821 }
6822
6823 if (FAILED(rc))
6824 throw rc;
6825
6826 /* Tell VBoxSVC and Machine about the progress object so they can
6827 combine/proxy it to any openRemoteSession caller. */
6828 LogFlowThisFunc(("Calling BeginPowerUp...\n"));
6829 rc = mControl->BeginPowerUp(pPowerupProgress);
6830 if (FAILED(rc))
6831 {
6832 LogFlowThisFunc(("BeginPowerUp failed\n"));
6833 throw rc;
6834 }
6835 fBeganPoweringUp = true;
6836
6837 LogFlowThisFunc(("Checking if canceled...\n"));
6838 BOOL fCanceled;
6839 rc = pPowerupProgress->COMGETTER(Canceled)(&fCanceled);
6840 if (FAILED(rc))
6841 throw rc;
6842
6843 if (fCanceled)
6844 {
6845 LogFlowThisFunc(("Canceled in BeginPowerUp\n"));
6846 throw setError(E_FAIL, tr("Powerup was canceled"));
6847 }
6848 LogFlowThisFunc(("Not canceled yet.\n"));
6849
6850 /** @todo this code prevents starting a VM with unavailable bridged
6851 * networking interface. The only benefit is a slightly better error
6852 * message, which should be moved to the driver code. This is the
6853 * only reason why I left the code in for now. The driver allows
6854 * unavailable bridged networking interfaces in certain circumstances,
6855 * and this is sabotaged by this check. The VM will initially have no
6856 * network connectivity, but the user can fix this at runtime. */
6857#if 0
6858 /* the network cards will undergo a quick consistency check */
6859 for (ULONG slot = 0;
6860 slot < maxNetworkAdapters;
6861 ++slot)
6862 {
6863 ComPtr<INetworkAdapter> pNetworkAdapter;
6864 mMachine->GetNetworkAdapter(slot, pNetworkAdapter.asOutParam());
6865 BOOL enabled = FALSE;
6866 pNetworkAdapter->COMGETTER(Enabled)(&enabled);
6867 if (!enabled)
6868 continue;
6869
6870 NetworkAttachmentType_T netattach;
6871 pNetworkAdapter->COMGETTER(AttachmentType)(&netattach);
6872 switch (netattach)
6873 {
6874 case NetworkAttachmentType_Bridged:
6875 {
6876 /* a valid host interface must have been set */
6877 Bstr hostif;
6878 pNetworkAdapter->COMGETTER(HostInterface)(hostif.asOutParam());
6879 if (hostif.isEmpty())
6880 {
6881 throw setError(VBOX_E_HOST_ERROR,
6882 tr("VM cannot start because host interface networking requires a host interface name to be set"));
6883 }
6884 ComPtr<IVirtualBox> pVirtualBox;
6885 mMachine->COMGETTER(Parent)(pVirtualBox.asOutParam());
6886 ComPtr<IHost> pHost;
6887 pVirtualBox->COMGETTER(Host)(pHost.asOutParam());
6888 ComPtr<IHostNetworkInterface> pHostInterface;
6889 if (!SUCCEEDED(pHost->FindHostNetworkInterfaceByName(hostif.raw(),
6890 pHostInterface.asOutParam())))
6891 {
6892 throw setError(VBOX_E_HOST_ERROR,
6893 tr("VM cannot start because the host interface '%ls' does not exist"),
6894 hostif.raw());
6895 }
6896 break;
6897 }
6898 default:
6899 break;
6900 }
6901 }
6902#endif // 0
6903
6904 /* Read console data stored in the saved state file (if not yet done) */
6905 rc = loadDataFromSavedState();
6906 if (FAILED(rc))
6907 throw rc;
6908
6909 /* setup task object and thread to carry out the operation
6910 * asynchronously */
6911 if (aProgress){
6912 rc = pPowerupProgress.queryInterfaceTo(aProgress);
6913 AssertComRCReturnRC(rc);
6914 }
6915
6916 int vrc = RTThreadCreate(NULL, Console::powerUpThread,
6917 (void *)task.get(), 0,
6918 RTTHREADTYPE_MAIN_WORKER, 0, "VMPwrUp");
6919 if (RT_FAILURE(vrc))
6920 throw setError(E_FAIL, "Could not create VMPowerUp thread (%Rrc)", vrc);
6921
6922 /* task is now owned by powerUpThread(), so release it */
6923 task.release();
6924
6925 /* finally, set the state: no right to fail in this method afterwards
6926 * since we've already started the thread and it is now responsible for
6927 * any error reporting and appropriate state change! */
6928 if (mMachineState == MachineState_Saved)
6929 setMachineState(MachineState_Restoring);
6930 else if (fTeleporterEnabled)
6931 setMachineState(MachineState_TeleportingIn);
6932 else if (enmFaultToleranceState == FaultToleranceState_Standby)
6933 setMachineState(MachineState_FaultTolerantSyncing);
6934 else
6935 setMachineState(MachineState_Starting);
6936 }
6937 catch (HRESULT aRC) { rc = aRC; }
6938
6939 if (FAILED(rc) && fBeganPoweringUp)
6940 {
6941
6942 /* The progress object will fetch the current error info */
6943 if (!pPowerupProgress.isNull())
6944 pPowerupProgress->notifyComplete(rc);
6945
6946 /* Save the error info across the IPC below. Can't be done before the
6947 * progress notification above, as saving the error info deletes it
6948 * from the current context, and thus the progress object wouldn't be
6949 * updated correctly. */
6950 ErrorInfoKeeper eik;
6951
6952 /* signal end of operation */
6953 mControl->EndPowerUp(rc);
6954 }
6955
6956 LogFlowThisFunc(("mMachineState=%d, rc=%Rhrc\n", mMachineState, rc));
6957 LogFlowThisFuncLeave();
6958 return rc;
6959}
6960
6961/**
6962 * Internal power off worker routine.
6963 *
6964 * This method may be called only at certain places with the following meaning
6965 * as shown below:
6966 *
6967 * - if the machine state is either Running or Paused, a normal
6968 * Console-initiated powerdown takes place (e.g. PowerDown());
6969 * - if the machine state is Saving, saveStateThread() has successfully done its
6970 * job;
6971 * - if the machine state is Starting or Restoring, powerUpThread() has failed
6972 * to start/load the VM;
6973 * - if the machine state is Stopping, the VM has powered itself off (i.e. not
6974 * as a result of the powerDown() call).
6975 *
6976 * Calling it in situations other than the above will cause unexpected behavior.
6977 *
6978 * Note that this method should be the only one that destroys mpUVM and sets it
6979 * to NULL.
6980 *
6981 * @param aProgress Progress object to run (may be NULL).
6982 *
6983 * @note Locks this object for writing.
6984 *
6985 * @note Never call this method from a thread that called addVMCaller() or
6986 * instantiated an AutoVMCaller object; first call releaseVMCaller() or
6987 * release(). Otherwise it will deadlock.
6988 */
6989HRESULT Console::powerDown(IProgress *aProgress /*= NULL*/)
6990{
6991 LogFlowThisFuncEnter();
6992
6993 AutoCaller autoCaller(this);
6994 AssertComRCReturnRC(autoCaller.rc());
6995
6996 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
6997
6998 /* Total # of steps for the progress object. Must correspond to the
6999 * number of "advance percent count" comments in this method! */
7000 enum { StepCount = 7 };
7001 /* current step */
7002 ULONG step = 0;
7003
7004 HRESULT rc = S_OK;
7005 int vrc = VINF_SUCCESS;
7006
7007 /* sanity */
7008 Assert(mVMDestroying == false);
7009
7010 PUVM pUVM = mpUVM; Assert(pUVM != NULL);
7011 uint32_t cRefs = VMR3RetainUVM(pUVM); Assert(cRefs != UINT32_MAX);
7012
7013 AssertMsg( mMachineState == MachineState_Running
7014 || mMachineState == MachineState_Paused
7015 || mMachineState == MachineState_Stuck
7016 || mMachineState == MachineState_Starting
7017 || mMachineState == MachineState_Stopping
7018 || mMachineState == MachineState_Saving
7019 || mMachineState == MachineState_Restoring
7020 || mMachineState == MachineState_TeleportingPausedVM
7021 || mMachineState == MachineState_FaultTolerantSyncing
7022 || mMachineState == MachineState_TeleportingIn
7023 , ("Invalid machine state: %s\n", Global::stringifyMachineState(mMachineState)));
7024
7025 LogRel(("Console::powerDown(): A request to power off the VM has been issued (mMachineState=%s, InUninit=%d)\n",
7026 Global::stringifyMachineState(mMachineState), autoCaller.state() == InUninit));
7027
7028 /* Check if we need to power off the VM. In case of mVMPoweredOff=true, the
7029 * VM has already powered itself off in vmstateChangeCallback() and is just
7030 * notifying Console about that. In case of Starting or Restoring,
7031 * powerUpThread() is calling us on failure, so the VM is already off at
7032 * that point. */
7033 if ( !mVMPoweredOff
7034 && ( mMachineState == MachineState_Starting
7035 || mMachineState == MachineState_Restoring
7036 || mMachineState == MachineState_FaultTolerantSyncing
7037 || mMachineState == MachineState_TeleportingIn)
7038 )
7039 mVMPoweredOff = true;
7040
7041 /*
7042 * Go to Stopping state if not already there.
7043 *
7044 * Note that we don't go from Saving/Restoring to Stopping because
7045 * vmstateChangeCallback() needs it to set the state to Saved on
7046 * VMSTATE_TERMINATED. In terms of protecting from inappropriate operations
7047 * while leaving the lock below, Saving or Restoring should be fine too.
7048 * Ditto for TeleportingPausedVM -> Teleported.
7049 */
7050 if ( mMachineState != MachineState_Saving
7051 && mMachineState != MachineState_Restoring
7052 && mMachineState != MachineState_Stopping
7053 && mMachineState != MachineState_TeleportingIn
7054 && mMachineState != MachineState_TeleportingPausedVM
7055 && mMachineState != MachineState_FaultTolerantSyncing
7056 )
7057 setMachineState(MachineState_Stopping);
7058
7059 /* ----------------------------------------------------------------------
7060 * DONE with necessary state changes, perform the power down actions (it's
7061 * safe to release the object lock now if needed)
7062 * ---------------------------------------------------------------------- */
7063
7064 /* Stop the VRDP server to prevent new clients connection while VM is being
7065 * powered off. */
7066 if (mConsoleVRDPServer)
7067 {
7068 LogFlowThisFunc(("Stopping VRDP server...\n"));
7069
7070 /* Leave the lock since EMT will call us back as addVMCaller()
7071 * in updateDisplayData(). */
7072 alock.release();
7073
7074 mConsoleVRDPServer->Stop();
7075
7076 alock.acquire();
7077 }
7078
7079 /* advance percent count */
7080 if (aProgress)
7081 aProgress->SetCurrentOperationProgress(99 * (++step) / StepCount );
7082
7083
7084 /* ----------------------------------------------------------------------
7085 * Now, wait for all mpUVM callers to finish their work if there are still
7086 * some on other threads. NO methods that need mpUVM (or initiate other calls
7087 * that need it) may be called after this point
7088 * ---------------------------------------------------------------------- */
7089
7090 /* go to the destroying state to prevent from adding new callers */
7091 mVMDestroying = true;
7092
7093 if (mVMCallers > 0)
7094 {
7095 /* lazy creation */
7096 if (mVMZeroCallersSem == NIL_RTSEMEVENT)
7097 RTSemEventCreate(&mVMZeroCallersSem);
7098
7099 LogFlowThisFunc(("Waiting for mpUVM callers (%d) to drop to zero...\n", mVMCallers));
7100
7101 alock.release();
7102
7103 RTSemEventWait(mVMZeroCallersSem, RT_INDEFINITE_WAIT);
7104
7105 alock.acquire();
7106 }
7107
7108 /* advance percent count */
7109 if (aProgress)
7110 aProgress->SetCurrentOperationProgress(99 * (++step) / StepCount );
7111
7112 vrc = VINF_SUCCESS;
7113
7114 /*
7115 * Power off the VM if not already done that.
7116 * Leave the lock since EMT will call vmstateChangeCallback.
7117 *
7118 * Note that VMR3PowerOff() may fail here (invalid VMSTATE) if the
7119 * VM-(guest-)initiated power off happened in parallel a ms before this
7120 * call. So far, we let this error pop up on the user's side.
7121 */
7122 if (!mVMPoweredOff)
7123 {
7124 LogFlowThisFunc(("Powering off the VM...\n"));
7125 alock.release();
7126 vrc = VMR3PowerOff(pUVM);
7127#ifdef VBOX_WITH_EXTPACK
7128 mptrExtPackManager->callAllVmPowerOffHooks(this, VMR3GetVM(pUVM));
7129#endif
7130 alock.acquire();
7131 }
7132
7133 /* advance percent count */
7134 if (aProgress)
7135 aProgress->SetCurrentOperationProgress(99 * (++step) / StepCount );
7136
7137#ifdef VBOX_WITH_HGCM
7138 /* Shutdown HGCM services before destroying the VM. */
7139 if (m_pVMMDev)
7140 {
7141 LogFlowThisFunc(("Shutdown HGCM...\n"));
7142
7143 /* Leave the lock since EMT will call us back as addVMCaller() */
7144 alock.release();
7145
7146 m_pVMMDev->hgcmShutdown();
7147
7148 alock.acquire();
7149 }
7150
7151 /* advance percent count */
7152 if (aProgress)
7153 aProgress->SetCurrentOperationProgress(99 * (++step) / StepCount);
7154
7155#endif /* VBOX_WITH_HGCM */
7156
7157 LogFlowThisFunc(("Ready for VM destruction.\n"));
7158
7159 /* If we are called from Console::uninit(), then try to destroy the VM even
7160 * on failure (this will most likely fail too, but what to do?..) */
7161 if (RT_SUCCESS(vrc) || autoCaller.state() == InUninit)
7162 {
7163 /* If the machine has a USB controller, release all USB devices
7164 * (symmetric to the code in captureUSBDevices()) */
7165 if (mfVMHasUsbController)
7166 {
7167 alock.release();
7168 detachAllUSBDevices(false /* aDone */);
7169 alock.acquire();
7170 }
7171
7172 /* Now we've got to destroy the VM as well. (mpUVM is not valid beyond
7173 * this point). We release the lock before calling VMR3Destroy() because
7174 * it will result into calling destructors of drivers associated with
7175 * Console children which may in turn try to lock Console (e.g. by
7176 * instantiating SafeVMPtr to access mpUVM). It's safe here because
7177 * mVMDestroying is set which should prevent any activity. */
7178
7179 /* Set mpUVM to NULL early just in case if some old code is not using
7180 * addVMCaller()/releaseVMCaller(). (We have our own ref on pUVM.) */
7181 VMR3ReleaseUVM(mpUVM);
7182 mpUVM = NULL;
7183
7184 LogFlowThisFunc(("Destroying the VM...\n"));
7185
7186 alock.release();
7187
7188 vrc = VMR3Destroy(pUVM);
7189
7190 /* take the lock again */
7191 alock.acquire();
7192
7193 /* advance percent count */
7194 if (aProgress)
7195 aProgress->SetCurrentOperationProgress(99 * (++step) / StepCount);
7196
7197 if (RT_SUCCESS(vrc))
7198 {
7199 LogFlowThisFunc(("Machine has been destroyed (mMachineState=%d)\n",
7200 mMachineState));
7201 /* Note: the Console-level machine state change happens on the
7202 * VMSTATE_TERMINATE state change in vmstateChangeCallback(). If
7203 * powerDown() is called from EMT (i.e. from vmstateChangeCallback()
7204 * on receiving VM-initiated VMSTATE_OFF), VMSTATE_TERMINATE hasn't
7205 * occurred yet. This is okay, because mMachineState is already
7206 * Stopping in this case, so any other attempt to call PowerDown()
7207 * will be rejected. */
7208 }
7209 else
7210 {
7211 /* bad bad bad, but what to do? (Give Console our UVM ref.) */
7212 mpUVM = pUVM;
7213 pUVM = NULL;
7214 rc = setError(VBOX_E_VM_ERROR,
7215 tr("Could not destroy the machine. (Error: %Rrc)"),
7216 vrc);
7217 }
7218
7219 /* Complete the detaching of the USB devices. */
7220 if (mfVMHasUsbController)
7221 {
7222 alock.release();
7223 detachAllUSBDevices(true /* aDone */);
7224 alock.acquire();
7225 }
7226
7227 /* advance percent count */
7228 if (aProgress)
7229 aProgress->SetCurrentOperationProgress(99 * (++step) / StepCount);
7230 }
7231 else
7232 {
7233 rc = setError(VBOX_E_VM_ERROR,
7234 tr("Could not power off the machine. (Error: %Rrc)"),
7235 vrc);
7236 }
7237
7238 /*
7239 * Finished with the destruction.
7240 *
7241 * Note that if something impossible happened and we've failed to destroy
7242 * the VM, mVMDestroying will remain true and mMachineState will be
7243 * something like Stopping, so most Console methods will return an error
7244 * to the caller.
7245 */
7246 if (pUVM != NULL)
7247 VMR3ReleaseUVM(pUVM);
7248 else
7249 mVMDestroying = false;
7250
7251#ifdef CONSOLE_WITH_EVENT_CACHE
7252 if (SUCCEEDED(rc))
7253 mCallbackData.clear();
7254#endif
7255
7256 LogFlowThisFuncLeave();
7257 return rc;
7258}
7259
7260/**
7261 * @note Locks this object for writing.
7262 */
7263HRESULT Console::setMachineState(MachineState_T aMachineState,
7264 bool aUpdateServer /* = true */)
7265{
7266 AutoCaller autoCaller(this);
7267 AssertComRCReturnRC(autoCaller.rc());
7268
7269 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
7270
7271 HRESULT rc = S_OK;
7272
7273 if (mMachineState != aMachineState)
7274 {
7275 LogThisFunc(("machineState=%s -> %s aUpdateServer=%RTbool\n",
7276 Global::stringifyMachineState(mMachineState), Global::stringifyMachineState(aMachineState), aUpdateServer));
7277 mMachineState = aMachineState;
7278
7279 /// @todo (dmik)
7280 // possibly, we need to redo onStateChange() using the dedicated
7281 // Event thread, like it is done in VirtualBox. This will make it
7282 // much safer (no deadlocks possible if someone tries to use the
7283 // console from the callback), however, listeners will lose the
7284 // ability to synchronously react to state changes (is it really
7285 // necessary??)
7286 LogFlowThisFunc(("Doing onStateChange()...\n"));
7287 onStateChange(aMachineState);
7288 LogFlowThisFunc(("Done onStateChange()\n"));
7289
7290 if (aUpdateServer)
7291 {
7292 /* Server notification MUST be done from under the lock; otherwise
7293 * the machine state here and on the server might go out of sync
7294 * which can lead to various unexpected results (like the machine
7295 * state being >= MachineState_Running on the server, while the
7296 * session state is already SessionState_Unlocked at the same time
7297 * there).
7298 *
7299 * Cross-lock conditions should be carefully watched out: calling
7300 * UpdateState we will require Machine and SessionMachine locks
7301 * (remember that here we're holding the Console lock here, and also
7302 * all locks that have been acquire by the thread before calling
7303 * this method).
7304 */
7305 LogFlowThisFunc(("Doing mControl->UpdateState()...\n"));
7306 rc = mControl->UpdateState(aMachineState);
7307 LogFlowThisFunc(("mControl->UpdateState()=%Rhrc\n", rc));
7308 }
7309 }
7310
7311 return rc;
7312}
7313
7314/**
7315 * Searches for a shared folder with the given logical name
7316 * in the collection of shared folders.
7317 *
7318 * @param aName logical name of the shared folder
7319 * @param aSharedFolder where to return the found object
7320 * @param aSetError whether to set the error info if the folder is
7321 * not found
7322 * @return
7323 * S_OK when found or E_INVALIDARG when not found
7324 *
7325 * @note The caller must lock this object for writing.
7326 */
7327HRESULT Console::findSharedFolder(const Utf8Str &strName,
7328 ComObjPtr<SharedFolder> &aSharedFolder,
7329 bool aSetError /* = false */)
7330{
7331 /* sanity check */
7332 AssertReturn(isWriteLockOnCurrentThread(), E_FAIL);
7333
7334 SharedFolderMap::const_iterator it = m_mapSharedFolders.find(strName);
7335 if (it != m_mapSharedFolders.end())
7336 {
7337 aSharedFolder = it->second;
7338 return S_OK;
7339 }
7340
7341 if (aSetError)
7342 setError(VBOX_E_FILE_ERROR,
7343 tr("Could not find a shared folder named '%s'."),
7344 strName.c_str());
7345
7346 return VBOX_E_FILE_ERROR;
7347}
7348
7349/**
7350 * Fetches the list of global or machine shared folders from the server.
7351 *
7352 * @param aGlobal true to fetch global folders.
7353 *
7354 * @note The caller must lock this object for writing.
7355 */
7356HRESULT Console::fetchSharedFolders(BOOL aGlobal)
7357{
7358 /* sanity check */
7359 AssertReturn(AutoCaller(this).state() == InInit ||
7360 isWriteLockOnCurrentThread(), E_FAIL);
7361
7362 LogFlowThisFunc(("Entering\n"));
7363
7364 /* Check if we're online and keep it that way. */
7365 SafeVMPtrQuiet ptrVM(this);
7366 AutoVMCallerQuietWeak autoVMCaller(this);
7367 bool const online = ptrVM.isOk()
7368 && m_pVMMDev
7369 && m_pVMMDev->isShFlActive();
7370
7371 HRESULT rc = S_OK;
7372
7373 try
7374 {
7375 if (aGlobal)
7376 {
7377 /// @todo grab & process global folders when they are done
7378 }
7379 else
7380 {
7381 SharedFolderDataMap oldFolders;
7382 if (online)
7383 oldFolders = m_mapMachineSharedFolders;
7384
7385 m_mapMachineSharedFolders.clear();
7386
7387 SafeIfaceArray<ISharedFolder> folders;
7388 rc = mMachine->COMGETTER(SharedFolders)(ComSafeArrayAsOutParam(folders));
7389 if (FAILED(rc)) throw rc;
7390
7391 for (size_t i = 0; i < folders.size(); ++i)
7392 {
7393 ComPtr<ISharedFolder> pSharedFolder = folders[i];
7394
7395 Bstr bstrName;
7396 Bstr bstrHostPath;
7397 BOOL writable;
7398 BOOL autoMount;
7399
7400 rc = pSharedFolder->COMGETTER(Name)(bstrName.asOutParam());
7401 if (FAILED(rc)) throw rc;
7402 Utf8Str strName(bstrName);
7403
7404 rc = pSharedFolder->COMGETTER(HostPath)(bstrHostPath.asOutParam());
7405 if (FAILED(rc)) throw rc;
7406 Utf8Str strHostPath(bstrHostPath);
7407
7408 rc = pSharedFolder->COMGETTER(Writable)(&writable);
7409 if (FAILED(rc)) throw rc;
7410
7411 rc = pSharedFolder->COMGETTER(AutoMount)(&autoMount);
7412 if (FAILED(rc)) throw rc;
7413
7414 m_mapMachineSharedFolders.insert(std::make_pair(strName,
7415 SharedFolderData(strHostPath, !!writable, !!autoMount)));
7416
7417 /* send changes to HGCM if the VM is running */
7418 if (online)
7419 {
7420 SharedFolderDataMap::iterator it = oldFolders.find(strName);
7421 if ( it == oldFolders.end()
7422 || it->second.m_strHostPath != strHostPath)
7423 {
7424 /* a new machine folder is added or
7425 * the existing machine folder is changed */
7426 if (m_mapSharedFolders.find(strName) != m_mapSharedFolders.end())
7427 ; /* the console folder exists, nothing to do */
7428 else
7429 {
7430 /* remove the old machine folder (when changed)
7431 * or the global folder if any (when new) */
7432 if ( it != oldFolders.end()
7433 || m_mapGlobalSharedFolders.find(strName) != m_mapGlobalSharedFolders.end()
7434 )
7435 {
7436 rc = removeSharedFolder(strName);
7437 if (FAILED(rc)) throw rc;
7438 }
7439
7440 /* create the new machine folder */
7441 rc = createSharedFolder(strName,
7442 SharedFolderData(strHostPath, !!writable, !!autoMount));
7443 if (FAILED(rc)) throw rc;
7444 }
7445 }
7446 /* forget the processed (or identical) folder */
7447 if (it != oldFolders.end())
7448 oldFolders.erase(it);
7449 }
7450 }
7451
7452 /* process outdated (removed) folders */
7453 if (online)
7454 {
7455 for (SharedFolderDataMap::const_iterator it = oldFolders.begin();
7456 it != oldFolders.end(); ++it)
7457 {
7458 if (m_mapSharedFolders.find(it->first) != m_mapSharedFolders.end())
7459 ; /* the console folder exists, nothing to do */
7460 else
7461 {
7462 /* remove the outdated machine folder */
7463 rc = removeSharedFolder(it->first);
7464 if (FAILED(rc)) throw rc;
7465
7466 /* create the global folder if there is any */
7467 SharedFolderDataMap::const_iterator git =
7468 m_mapGlobalSharedFolders.find(it->first);
7469 if (git != m_mapGlobalSharedFolders.end())
7470 {
7471 rc = createSharedFolder(git->first, git->second);
7472 if (FAILED(rc)) throw rc;
7473 }
7474 }
7475 }
7476 }
7477 }
7478 }
7479 catch (HRESULT rc2)
7480 {
7481 rc = rc2;
7482 if (online)
7483 setVMRuntimeErrorCallbackF(0, "BrokenSharedFolder",
7484 N_("Broken shared folder!"));
7485 }
7486
7487 LogFlowThisFunc(("Leaving\n"));
7488
7489 return rc;
7490}
7491
7492/**
7493 * Searches for a shared folder with the given name in the list of machine
7494 * shared folders and then in the list of the global shared folders.
7495 *
7496 * @param aName Name of the folder to search for.
7497 * @param aIt Where to store the pointer to the found folder.
7498 * @return @c true if the folder was found and @c false otherwise.
7499 *
7500 * @note The caller must lock this object for reading.
7501 */
7502bool Console::findOtherSharedFolder(const Utf8Str &strName,
7503 SharedFolderDataMap::const_iterator &aIt)
7504{
7505 /* sanity check */
7506 AssertReturn(isWriteLockOnCurrentThread(), false);
7507
7508 /* first, search machine folders */
7509 aIt = m_mapMachineSharedFolders.find(strName);
7510 if (aIt != m_mapMachineSharedFolders.end())
7511 return true;
7512
7513 /* second, search machine folders */
7514 aIt = m_mapGlobalSharedFolders.find(strName);
7515 if (aIt != m_mapGlobalSharedFolders.end())
7516 return true;
7517
7518 return false;
7519}
7520
7521/**
7522 * Calls the HGCM service to add a shared folder definition.
7523 *
7524 * @param aName Shared folder name.
7525 * @param aHostPath Shared folder path.
7526 *
7527 * @note Must be called from under AutoVMCaller and when mpUVM != NULL!
7528 * @note Doesn't lock anything.
7529 */
7530HRESULT Console::createSharedFolder(const Utf8Str &strName, const SharedFolderData &aData)
7531{
7532 ComAssertRet(strName.isNotEmpty(), E_FAIL);
7533 ComAssertRet(aData.m_strHostPath.isNotEmpty(), E_FAIL);
7534
7535 /* sanity checks */
7536 AssertReturn(mpUVM, E_FAIL);
7537 AssertReturn(m_pVMMDev && m_pVMMDev->isShFlActive(), E_FAIL);
7538
7539 VBOXHGCMSVCPARM parms[SHFL_CPARMS_ADD_MAPPING];
7540 SHFLSTRING *pFolderName, *pMapName;
7541 size_t cbString;
7542
7543 Bstr value;
7544 HRESULT hrc = mMachine->GetExtraData(BstrFmt("VBoxInternal2/SharedFoldersEnableSymlinksCreate/%s",
7545 strName.c_str()).raw(),
7546 value.asOutParam());
7547 bool fSymlinksCreate = hrc == S_OK && value == "1";
7548
7549 Log(("Adding shared folder '%s' -> '%s'\n", strName.c_str(), aData.m_strHostPath.c_str()));
7550
7551 // check whether the path is valid and exists
7552 char hostPathFull[RTPATH_MAX];
7553 int vrc = RTPathAbsEx(NULL,
7554 aData.m_strHostPath.c_str(),
7555 hostPathFull,
7556 sizeof(hostPathFull));
7557
7558 bool fMissing = false;
7559 if (RT_FAILURE(vrc))
7560 return setError(E_INVALIDARG,
7561 tr("Invalid shared folder path: '%s' (%Rrc)"),
7562 aData.m_strHostPath.c_str(), vrc);
7563 if (!RTPathExists(hostPathFull))
7564 fMissing = true;
7565
7566 /* Check whether the path is full (absolute) */
7567 if (RTPathCompare(aData.m_strHostPath.c_str(), hostPathFull) != 0)
7568 return setError(E_INVALIDARG,
7569 tr("Shared folder path '%s' is not absolute"),
7570 aData.m_strHostPath.c_str());
7571
7572 // now that we know the path is good, give it to HGCM
7573
7574 Bstr bstrName(strName);
7575 Bstr bstrHostPath(aData.m_strHostPath);
7576
7577 cbString = (bstrHostPath.length() + 1) * sizeof(RTUTF16);
7578 if (cbString >= UINT16_MAX)
7579 return setError(E_INVALIDARG, tr("The name is too long"));
7580 pFolderName = (SHFLSTRING*)RTMemAllocZ(sizeof(SHFLSTRING) + cbString);
7581 Assert(pFolderName);
7582 memcpy(pFolderName->String.ucs2, bstrHostPath.raw(), cbString);
7583
7584 pFolderName->u16Size = (uint16_t)cbString;
7585 pFolderName->u16Length = (uint16_t)cbString - sizeof(RTUTF16);
7586
7587 parms[0].type = VBOX_HGCM_SVC_PARM_PTR;
7588 parms[0].u.pointer.addr = pFolderName;
7589 parms[0].u.pointer.size = sizeof(SHFLSTRING) + (uint16_t)cbString;
7590
7591 cbString = (bstrName.length() + 1) * sizeof(RTUTF16);
7592 if (cbString >= UINT16_MAX)
7593 {
7594 RTMemFree(pFolderName);
7595 return setError(E_INVALIDARG, tr("The host path is too long"));
7596 }
7597 pMapName = (SHFLSTRING*)RTMemAllocZ(sizeof(SHFLSTRING) + cbString);
7598 Assert(pMapName);
7599 memcpy(pMapName->String.ucs2, bstrName.raw(), cbString);
7600
7601 pMapName->u16Size = (uint16_t)cbString;
7602 pMapName->u16Length = (uint16_t)cbString - sizeof(RTUTF16);
7603
7604 parms[1].type = VBOX_HGCM_SVC_PARM_PTR;
7605 parms[1].u.pointer.addr = pMapName;
7606 parms[1].u.pointer.size = sizeof(SHFLSTRING) + (uint16_t)cbString;
7607
7608 parms[2].type = VBOX_HGCM_SVC_PARM_32BIT;
7609 parms[2].u.uint32 = (aData.m_fWritable ? SHFL_ADD_MAPPING_F_WRITABLE : 0)
7610 | (aData.m_fAutoMount ? SHFL_ADD_MAPPING_F_AUTOMOUNT : 0)
7611 | (fSymlinksCreate ? SHFL_ADD_MAPPING_F_CREATE_SYMLINKS : 0)
7612 | (fMissing ? SHFL_ADD_MAPPING_F_MISSING : 0)
7613 ;
7614
7615 vrc = m_pVMMDev->hgcmHostCall("VBoxSharedFolders",
7616 SHFL_FN_ADD_MAPPING,
7617 SHFL_CPARMS_ADD_MAPPING, &parms[0]);
7618 RTMemFree(pFolderName);
7619 RTMemFree(pMapName);
7620
7621 if (RT_FAILURE(vrc))
7622 return setError(E_FAIL,
7623 tr("Could not create a shared folder '%s' mapped to '%s' (%Rrc)"),
7624 strName.c_str(), aData.m_strHostPath.c_str(), vrc);
7625
7626 if (fMissing)
7627 return setError(E_INVALIDARG,
7628 tr("Shared folder path '%s' does not exist on the host"),
7629 aData.m_strHostPath.c_str());
7630
7631 return S_OK;
7632}
7633
7634/**
7635 * Calls the HGCM service to remove the shared folder definition.
7636 *
7637 * @param aName Shared folder name.
7638 *
7639 * @note Must be called from under AutoVMCaller and when mpUVM != NULL!
7640 * @note Doesn't lock anything.
7641 */
7642HRESULT Console::removeSharedFolder(const Utf8Str &strName)
7643{
7644 ComAssertRet(strName.isNotEmpty(), E_FAIL);
7645
7646 /* sanity checks */
7647 AssertReturn(mpUVM, E_FAIL);
7648 AssertReturn(m_pVMMDev && m_pVMMDev->isShFlActive(), E_FAIL);
7649
7650 VBOXHGCMSVCPARM parms;
7651 SHFLSTRING *pMapName;
7652 size_t cbString;
7653
7654 Log(("Removing shared folder '%s'\n", strName.c_str()));
7655
7656 Bstr bstrName(strName);
7657 cbString = (bstrName.length() + 1) * sizeof(RTUTF16);
7658 if (cbString >= UINT16_MAX)
7659 return setError(E_INVALIDARG, tr("The name is too long"));
7660 pMapName = (SHFLSTRING *) RTMemAllocZ(sizeof(SHFLSTRING) + cbString);
7661 Assert(pMapName);
7662 memcpy(pMapName->String.ucs2, bstrName.raw(), cbString);
7663
7664 pMapName->u16Size = (uint16_t)cbString;
7665 pMapName->u16Length = (uint16_t)cbString - sizeof(RTUTF16);
7666
7667 parms.type = VBOX_HGCM_SVC_PARM_PTR;
7668 parms.u.pointer.addr = pMapName;
7669 parms.u.pointer.size = sizeof(SHFLSTRING) + (uint16_t)cbString;
7670
7671 int vrc = m_pVMMDev->hgcmHostCall("VBoxSharedFolders",
7672 SHFL_FN_REMOVE_MAPPING,
7673 1, &parms);
7674 RTMemFree(pMapName);
7675 if (RT_FAILURE(vrc))
7676 return setError(E_FAIL,
7677 tr("Could not remove the shared folder '%s' (%Rrc)"),
7678 strName.c_str(), vrc);
7679
7680 return S_OK;
7681}
7682
7683/**
7684 * Internal VM power off worker.
7685 *
7686 * @return nothing.
7687 * @param that Console object.
7688 * @param fCalledFromReset Flag whether the worker was called from the reset state change.
7689 */
7690void Console::vmstateChangePowerOff(bool fCalledFromReset = false)
7691{
7692#ifdef VBOX_WITH_GUEST_PROPS
7693 if (isResetTurnedIntoPowerOff())
7694 {
7695 Bstr strPowerOffReason;
7696
7697 if (fCalledFromReset)
7698 strPowerOffReason = Bstr("Reset");
7699 else
7700 strPowerOffReason = Bstr("PowerOff");
7701
7702 mMachine->DeleteGuestProperty(Bstr("/VirtualBox/HostInfo/VMPowerOffReason").raw());
7703 mMachine->SetGuestProperty(Bstr("/VirtualBox/HostInfo/VMPowerOffReason").raw(),
7704 strPowerOffReason.raw(), Bstr("RDONLYGUEST").raw());
7705 mMachine->SaveSettings();
7706 }
7707#endif
7708
7709 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
7710
7711 if (mVMStateChangeCallbackDisabled)
7712 return;
7713
7714 /* Do we still think that it is running? It may happen if this is a
7715 * VM-(guest-)initiated shutdown/poweroff.
7716 */
7717 if ( mMachineState != MachineState_Stopping
7718 && mMachineState != MachineState_Saving
7719 && mMachineState != MachineState_Restoring
7720 && mMachineState != MachineState_TeleportingIn
7721 && mMachineState != MachineState_FaultTolerantSyncing
7722 && mMachineState != MachineState_TeleportingPausedVM
7723 && !mVMIsAlreadyPoweringOff
7724 )
7725 {
7726 LogFlowFunc(("VM has powered itself off but Console still thinks it is running. Notifying.\n"));
7727
7728 /*
7729 * Prevent powerDown() from calling VMR3PowerOff() again if this was called from
7730 * the power off state change.
7731 * When called from the Reset state make sure to call VMR3PowerOff() first.
7732 */
7733 Assert(mVMPoweredOff == false);
7734 mVMPoweredOff = !fCalledFromReset;
7735
7736 /*
7737 * request a progress object from the server
7738 * (this will set the machine state to Stopping on the server
7739 * to block others from accessing this machine)
7740 */
7741 ComPtr<IProgress> pProgress;
7742 HRESULT rc = mControl->BeginPoweringDown(pProgress.asOutParam());
7743 AssertComRC(rc);
7744
7745 /* sync the state with the server */
7746 setMachineStateLocally(MachineState_Stopping);
7747
7748 /* Setup task object and thread to carry out the operation
7749 * asynchronously (if we call powerDown() right here but there
7750 * is one or more mpUVM callers (added with addVMCaller()) we'll
7751 * deadlock).
7752 */
7753 std::auto_ptr<VMPowerDownTask> task(new VMPowerDownTask(this, pProgress));
7754
7755 /* If creating a task failed, this can currently mean one of
7756 * two: either Console::uninit() has been called just a ms
7757 * before (so a powerDown() call is already on the way), or
7758 * powerDown() itself is being already executed. Just do
7759 * nothing.
7760 */
7761 if (!task->isOk())
7762 {
7763 LogFlowFunc(("Console is already being uninitialized.\n"));
7764 return;
7765 }
7766
7767 int vrc = RTThreadCreate(NULL, Console::powerDownThread,
7768 (void *)task.get(), 0,
7769 RTTHREADTYPE_MAIN_WORKER, 0,
7770 "VMPwrDwn");
7771 AssertMsgRCReturnVoid(vrc, ("Could not create VMPowerDown thread (%Rrc)\n", vrc));
7772
7773 /* task is now owned by powerDownThread(), so release it */
7774 task.release();
7775 }
7776}
7777
7778/** @callback_method_impl{FNVMATSTATE}
7779 *
7780 * @note Locks the Console object for writing.
7781 * @remarks The @a pUVM parameter can be NULL in one case where powerUpThread()
7782 * calls after the VM was destroyed.
7783 */
7784DECLCALLBACK(void) Console::vmstateChangeCallback(PUVM pUVM, VMSTATE enmState, VMSTATE enmOldState, void *pvUser)
7785{
7786 LogFlowFunc(("Changing state from %s to %s (pUVM=%p)\n",
7787 VMR3GetStateName(enmOldState), VMR3GetStateName(enmState), pUVM));
7788
7789 Console *that = static_cast<Console *>(pvUser);
7790 AssertReturnVoid(that);
7791
7792 AutoCaller autoCaller(that);
7793
7794 /* Note that we must let this method proceed even if Console::uninit() has
7795 * been already called. In such case this VMSTATE change is a result of:
7796 * 1) powerDown() called from uninit() itself, or
7797 * 2) VM-(guest-)initiated power off. */
7798 AssertReturnVoid( autoCaller.isOk()
7799 || autoCaller.state() == InUninit);
7800
7801 switch (enmState)
7802 {
7803 /*
7804 * The VM has terminated
7805 */
7806 case VMSTATE_OFF:
7807 {
7808 that->vmstateChangePowerOff();
7809 break;
7810 }
7811
7812 /* The VM has been completely destroyed.
7813 *
7814 * Note: This state change can happen at two points:
7815 * 1) At the end of VMR3Destroy() if it was not called from EMT.
7816 * 2) At the end of vmR3EmulationThread if VMR3Destroy() was
7817 * called by EMT.
7818 */
7819 case VMSTATE_TERMINATED:
7820 {
7821 AutoWriteLock alock(that COMMA_LOCKVAL_SRC_POS);
7822
7823 if (that->mVMStateChangeCallbackDisabled)
7824 break;
7825
7826 /* Terminate host interface networking. If pUVM is NULL, we've been
7827 * manually called from powerUpThread() either before calling
7828 * VMR3Create() or after VMR3Create() failed, so no need to touch
7829 * networking.
7830 */
7831 if (pUVM)
7832 that->powerDownHostInterfaces();
7833
7834 /* From now on the machine is officially powered down or remains in
7835 * the Saved state.
7836 */
7837 switch (that->mMachineState)
7838 {
7839 default:
7840 AssertFailed();
7841 /* fall through */
7842 case MachineState_Stopping:
7843 /* successfully powered down */
7844 that->setMachineState(MachineState_PoweredOff);
7845 break;
7846 case MachineState_Saving:
7847 /* successfully saved */
7848 that->setMachineState(MachineState_Saved);
7849 break;
7850 case MachineState_Starting:
7851 /* failed to start, but be patient: set back to PoweredOff
7852 * (for similarity with the below) */
7853 that->setMachineState(MachineState_PoweredOff);
7854 break;
7855 case MachineState_Restoring:
7856 /* failed to load the saved state file, but be patient: set
7857 * back to Saved (to preserve the saved state file) */
7858 that->setMachineState(MachineState_Saved);
7859 break;
7860 case MachineState_TeleportingIn:
7861 /* Teleportation failed or was canceled. Back to powered off. */
7862 that->setMachineState(MachineState_PoweredOff);
7863 break;
7864 case MachineState_TeleportingPausedVM:
7865 /* Successfully teleported the VM. */
7866 that->setMachineState(MachineState_Teleported);
7867 break;
7868 case MachineState_FaultTolerantSyncing:
7869 /* Fault tolerant sync failed or was canceled. Back to powered off. */
7870 that->setMachineState(MachineState_PoweredOff);
7871 break;
7872 }
7873 break;
7874 }
7875
7876 case VMSTATE_RESETTING:
7877 {
7878 if (!that->isResetTurnedIntoPowerOff())
7879 {
7880#ifdef VBOX_WITH_GUEST_PROPS
7881 /* Do not take any read/write locks here! */
7882 that->guestPropertiesHandleVMReset();
7883#endif
7884 }
7885 else
7886 that->vmstateChangePowerOff(true /* fCalledFromReset*/);
7887 break;
7888 }
7889
7890 case VMSTATE_SUSPENDED:
7891 {
7892 AutoWriteLock alock(that COMMA_LOCKVAL_SRC_POS);
7893
7894 if (that->mVMStateChangeCallbackDisabled)
7895 break;
7896
7897 switch (that->mMachineState)
7898 {
7899 case MachineState_Teleporting:
7900 that->setMachineState(MachineState_TeleportingPausedVM);
7901 break;
7902
7903 case MachineState_LiveSnapshotting:
7904 that->setMachineState(MachineState_Saving);
7905 break;
7906
7907 case MachineState_TeleportingPausedVM:
7908 case MachineState_Saving:
7909 case MachineState_Restoring:
7910 case MachineState_Stopping:
7911 case MachineState_TeleportingIn:
7912 case MachineState_FaultTolerantSyncing:
7913 /* The worker thread handles the transition. */
7914 break;
7915
7916 default:
7917 AssertMsgFailed(("%s\n", Global::stringifyMachineState(that->mMachineState)));
7918 case MachineState_Running:
7919 that->setMachineState(MachineState_Paused);
7920 break;
7921
7922 case MachineState_Paused:
7923 /* Nothing to do. */
7924 break;
7925 }
7926 break;
7927 }
7928
7929 case VMSTATE_SUSPENDED_LS:
7930 case VMSTATE_SUSPENDED_EXT_LS:
7931 {
7932 AutoWriteLock alock(that COMMA_LOCKVAL_SRC_POS);
7933 if (that->mVMStateChangeCallbackDisabled)
7934 break;
7935 switch (that->mMachineState)
7936 {
7937 case MachineState_Teleporting:
7938 that->setMachineState(MachineState_TeleportingPausedVM);
7939 break;
7940
7941 case MachineState_LiveSnapshotting:
7942 that->setMachineState(MachineState_Saving);
7943 break;
7944
7945 case MachineState_TeleportingPausedVM:
7946 case MachineState_Saving:
7947 /* ignore */
7948 break;
7949
7950 default:
7951 AssertMsgFailed(("%s/%s -> %s\n", Global::stringifyMachineState(that->mMachineState), VMR3GetStateName(enmOldState), VMR3GetStateName(enmState) ));
7952 that->setMachineState(MachineState_Paused);
7953 break;
7954 }
7955 break;
7956 }
7957
7958 case VMSTATE_RUNNING:
7959 {
7960 if ( enmOldState == VMSTATE_POWERING_ON
7961 || enmOldState == VMSTATE_RESUMING
7962 || enmOldState == VMSTATE_RUNNING_FT)
7963 {
7964 AutoWriteLock alock(that COMMA_LOCKVAL_SRC_POS);
7965
7966 if (that->mVMStateChangeCallbackDisabled)
7967 break;
7968
7969 Assert( ( ( that->mMachineState == MachineState_Starting
7970 || that->mMachineState == MachineState_Paused)
7971 && enmOldState == VMSTATE_POWERING_ON)
7972 || ( ( that->mMachineState == MachineState_Restoring
7973 || that->mMachineState == MachineState_TeleportingIn
7974 || that->mMachineState == MachineState_Paused
7975 || that->mMachineState == MachineState_Saving
7976 )
7977 && enmOldState == VMSTATE_RESUMING)
7978 || ( that->mMachineState == MachineState_FaultTolerantSyncing
7979 && enmOldState == VMSTATE_RUNNING_FT));
7980
7981 that->setMachineState(MachineState_Running);
7982 }
7983
7984 break;
7985 }
7986
7987 case VMSTATE_RUNNING_LS:
7988 AssertMsg( that->mMachineState == MachineState_LiveSnapshotting
7989 || that->mMachineState == MachineState_Teleporting,
7990 ("%s/%s -> %s\n", Global::stringifyMachineState(that->mMachineState), VMR3GetStateName(enmOldState), VMR3GetStateName(enmState) ));
7991 break;
7992
7993 case VMSTATE_RUNNING_FT:
7994 AssertMsg(that->mMachineState == MachineState_FaultTolerantSyncing,
7995 ("%s/%s -> %s\n", Global::stringifyMachineState(that->mMachineState), VMR3GetStateName(enmOldState), VMR3GetStateName(enmState) ));
7996 break;
7997
7998 case VMSTATE_FATAL_ERROR:
7999 {
8000 AutoWriteLock alock(that COMMA_LOCKVAL_SRC_POS);
8001
8002 if (that->mVMStateChangeCallbackDisabled)
8003 break;
8004
8005 /* Fatal errors are only for running VMs. */
8006 Assert(Global::IsOnline(that->mMachineState));
8007
8008 /* Note! 'Pause' is used here in want of something better. There
8009 * are currently only two places where fatal errors might be
8010 * raised, so it is not worth adding a new externally
8011 * visible state for this yet. */
8012 that->setMachineState(MachineState_Paused);
8013 break;
8014 }
8015
8016 case VMSTATE_GURU_MEDITATION:
8017 {
8018 AutoWriteLock alock(that COMMA_LOCKVAL_SRC_POS);
8019
8020 if (that->mVMStateChangeCallbackDisabled)
8021 break;
8022
8023 /* Guru are only for running VMs */
8024 Assert(Global::IsOnline(that->mMachineState));
8025
8026 that->setMachineState(MachineState_Stuck);
8027 break;
8028 }
8029
8030 default: /* shut up gcc */
8031 break;
8032 }
8033}
8034
8035/**
8036 * Changes the clipboard mode.
8037 *
8038 * @param aClipboardMode new clipboard mode.
8039 */
8040void Console::changeClipboardMode(ClipboardMode_T aClipboardMode)
8041{
8042 VMMDev *pVMMDev = m_pVMMDev;
8043 Assert(pVMMDev);
8044
8045 VBOXHGCMSVCPARM parm;
8046 parm.type = VBOX_HGCM_SVC_PARM_32BIT;
8047
8048 switch (aClipboardMode)
8049 {
8050 default:
8051 case ClipboardMode_Disabled:
8052 LogRel(("Shared clipboard mode: Off\n"));
8053 parm.u.uint32 = VBOX_SHARED_CLIPBOARD_MODE_OFF;
8054 break;
8055 case ClipboardMode_GuestToHost:
8056 LogRel(("Shared clipboard mode: Guest to Host\n"));
8057 parm.u.uint32 = VBOX_SHARED_CLIPBOARD_MODE_GUEST_TO_HOST;
8058 break;
8059 case ClipboardMode_HostToGuest:
8060 LogRel(("Shared clipboard mode: Host to Guest\n"));
8061 parm.u.uint32 = VBOX_SHARED_CLIPBOARD_MODE_HOST_TO_GUEST;
8062 break;
8063 case ClipboardMode_Bidirectional:
8064 LogRel(("Shared clipboard mode: Bidirectional\n"));
8065 parm.u.uint32 = VBOX_SHARED_CLIPBOARD_MODE_BIDIRECTIONAL;
8066 break;
8067 }
8068
8069 pVMMDev->hgcmHostCall("VBoxSharedClipboard", VBOX_SHARED_CLIPBOARD_HOST_FN_SET_MODE, 1, &parm);
8070}
8071
8072/**
8073 * Changes the drag'n_drop mode.
8074 *
8075 * @param aDragAndDropMode new drag'n'drop mode.
8076 */
8077void Console::changeDragAndDropMode(DragAndDropMode_T aDragAndDropMode)
8078{
8079 VMMDev *pVMMDev = m_pVMMDev;
8080 Assert(pVMMDev);
8081
8082 VBOXHGCMSVCPARM parm;
8083 parm.type = VBOX_HGCM_SVC_PARM_32BIT;
8084
8085 switch (aDragAndDropMode)
8086 {
8087 default:
8088 case DragAndDropMode_Disabled:
8089 LogRel(("Drag'n'drop mode: Off\n"));
8090 parm.u.uint32 = VBOX_DRAG_AND_DROP_MODE_OFF;
8091 break;
8092 case DragAndDropMode_GuestToHost:
8093 LogRel(("Drag'n'drop mode: Guest to Host\n"));
8094 parm.u.uint32 = VBOX_DRAG_AND_DROP_MODE_GUEST_TO_HOST;
8095 break;
8096 case DragAndDropMode_HostToGuest:
8097 LogRel(("Drag'n'drop mode: Host to Guest\n"));
8098 parm.u.uint32 = VBOX_DRAG_AND_DROP_MODE_HOST_TO_GUEST;
8099 break;
8100 case DragAndDropMode_Bidirectional:
8101 LogRel(("Drag'n'drop mode: Bidirectional\n"));
8102 parm.u.uint32 = VBOX_DRAG_AND_DROP_MODE_BIDIRECTIONAL;
8103 break;
8104 }
8105
8106 pVMMDev->hgcmHostCall("VBoxDragAndDropSvc", DragAndDropSvc::HOST_DND_SET_MODE, 1, &parm);
8107}
8108
8109#ifdef VBOX_WITH_USB
8110/**
8111 * Sends a request to VMM to attach the given host device.
8112 * After this method succeeds, the attached device will appear in the
8113 * mUSBDevices collection.
8114 *
8115 * @param aHostDevice device to attach
8116 *
8117 * @note Synchronously calls EMT.
8118 */
8119HRESULT Console::attachUSBDevice(IUSBDevice *aHostDevice, ULONG aMaskedIfs)
8120{
8121 AssertReturn(aHostDevice, E_FAIL);
8122 AssertReturn(!isWriteLockOnCurrentThread(), E_FAIL);
8123
8124 HRESULT hrc;
8125
8126 /*
8127 * Get the address and the Uuid, and call the pfnCreateProxyDevice roothub
8128 * method in EMT (using usbAttachCallback()).
8129 */
8130 Bstr BstrAddress;
8131 hrc = aHostDevice->COMGETTER(Address)(BstrAddress.asOutParam());
8132 ComAssertComRCRetRC(hrc);
8133
8134 Utf8Str Address(BstrAddress);
8135
8136 Bstr id;
8137 hrc = aHostDevice->COMGETTER(Id)(id.asOutParam());
8138 ComAssertComRCRetRC(hrc);
8139 Guid uuid(id);
8140
8141 BOOL fRemote = FALSE;
8142 hrc = aHostDevice->COMGETTER(Remote)(&fRemote);
8143 ComAssertComRCRetRC(hrc);
8144
8145 /* Get the VM handle. */
8146 SafeVMPtr ptrVM(this);
8147 if (!ptrVM.isOk())
8148 return ptrVM.rc();
8149
8150 LogFlowThisFunc(("Proxying USB device '%s' {%RTuuid}...\n",
8151 Address.c_str(), uuid.raw()));
8152
8153 void *pvRemoteBackend = NULL;
8154 if (fRemote)
8155 {
8156 RemoteUSBDevice *pRemoteUSBDevice = static_cast<RemoteUSBDevice *>(aHostDevice);
8157 pvRemoteBackend = consoleVRDPServer()->USBBackendRequestPointer(pRemoteUSBDevice->clientId(), &uuid);
8158 if (!pvRemoteBackend)
8159 return E_INVALIDARG; /* The clientId is invalid then. */
8160 }
8161
8162 USHORT portVersion = 1;
8163 hrc = aHostDevice->COMGETTER(PortVersion)(&portVersion);
8164 AssertComRCReturnRC(hrc);
8165 Assert(portVersion == 1 || portVersion == 2);
8166
8167 int vrc = VMR3ReqCallWaitU(ptrVM.rawUVM(), 0 /* idDstCpu (saved state, see #6232) */,
8168 (PFNRT)usbAttachCallback, 9,
8169 this, ptrVM.rawUVM(), aHostDevice, uuid.raw(), fRemote,
8170 Address.c_str(), pvRemoteBackend, portVersion, aMaskedIfs);
8171
8172 if (RT_SUCCESS(vrc))
8173 {
8174 /* Create a OUSBDevice and add it to the device list */
8175 ComObjPtr<OUSBDevice> pUSBDevice;
8176 pUSBDevice.createObject();
8177 hrc = pUSBDevice->init(aHostDevice);
8178 AssertComRC(hrc);
8179
8180 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
8181 mUSBDevices.push_back(pUSBDevice);
8182 LogFlowFunc(("Attached device {%RTuuid}\n", pUSBDevice->id().raw()));
8183
8184 /* notify callbacks */
8185 alock.release();
8186 onUSBDeviceStateChange(pUSBDevice, true /* aAttached */, NULL);
8187 }
8188 else
8189 {
8190 LogWarningThisFunc(("Failed to create proxy device for '%s' {%RTuuid} (%Rrc)\n",
8191 Address.c_str(), uuid.raw(), vrc));
8192
8193 switch (vrc)
8194 {
8195 case VERR_VUSB_NO_PORTS:
8196 hrc = setError(E_FAIL, tr("Failed to attach the USB device. (No available ports on the USB controller)."));
8197 break;
8198 case VERR_VUSB_USBFS_PERMISSION:
8199 hrc = setError(E_FAIL, tr("Not permitted to open the USB device, check usbfs options"));
8200 break;
8201 default:
8202 hrc = setError(E_FAIL, tr("Failed to create a proxy device for the USB device. (Error: %Rrc)"), vrc);
8203 break;
8204 }
8205 }
8206
8207 return hrc;
8208}
8209
8210/**
8211 * USB device attach callback used by AttachUSBDevice().
8212 * Note that AttachUSBDevice() doesn't return until this callback is executed,
8213 * so we don't use AutoCaller and don't care about reference counters of
8214 * interface pointers passed in.
8215 *
8216 * @thread EMT
8217 * @note Locks the console object for writing.
8218 */
8219//static
8220DECLCALLBACK(int)
8221Console::usbAttachCallback(Console *that, PUVM pUVM, IUSBDevice *aHostDevice, PCRTUUID aUuid, bool aRemote,
8222 const char *aAddress, void *pvRemoteBackend, USHORT aPortVersion, ULONG aMaskedIfs)
8223{
8224 LogFlowFuncEnter();
8225 LogFlowFunc(("that={%p} aUuid={%RTuuid}\n", that, aUuid));
8226
8227 AssertReturn(that && aUuid, VERR_INVALID_PARAMETER);
8228 AssertReturn(!that->isWriteLockOnCurrentThread(), VERR_GENERAL_FAILURE);
8229
8230 int vrc = PDMR3UsbCreateProxyDevice(pUVM, aUuid, aRemote, aAddress, pvRemoteBackend,
8231 aPortVersion == 1 ? VUSB_STDVER_11 : VUSB_STDVER_20, aMaskedIfs);
8232 LogFlowFunc(("vrc=%Rrc\n", vrc));
8233 LogFlowFuncLeave();
8234 return vrc;
8235}
8236
8237/**
8238 * Sends a request to VMM to detach the given host device. After this method
8239 * succeeds, the detached device will disappear from the mUSBDevices
8240 * collection.
8241 *
8242 * @param aHostDevice device to attach
8243 *
8244 * @note Synchronously calls EMT.
8245 */
8246HRESULT Console::detachUSBDevice(const ComObjPtr<OUSBDevice> &aHostDevice)
8247{
8248 AssertReturn(!isWriteLockOnCurrentThread(), E_FAIL);
8249
8250 /* Get the VM handle. */
8251 SafeVMPtr ptrVM(this);
8252 if (!ptrVM.isOk())
8253 return ptrVM.rc();
8254
8255 /* if the device is attached, then there must at least one USB hub. */
8256 AssertReturn(PDMR3UsbHasHub(ptrVM.rawUVM()), E_FAIL);
8257
8258 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
8259 LogFlowThisFunc(("Detaching USB proxy device {%RTuuid}...\n",
8260 aHostDevice->id().raw()));
8261
8262 /*
8263 * If this was a remote device, release the backend pointer.
8264 * The pointer was requested in usbAttachCallback.
8265 */
8266 BOOL fRemote = FALSE;
8267
8268 HRESULT hrc2 = aHostDevice->COMGETTER(Remote)(&fRemote);
8269 if (FAILED(hrc2))
8270 setErrorStatic(hrc2, "GetRemote() failed");
8271
8272 PCRTUUID pUuid = aHostDevice->id().raw();
8273 if (fRemote)
8274 {
8275 Guid guid(*pUuid);
8276 consoleVRDPServer()->USBBackendReleasePointer(&guid);
8277 }
8278
8279 alock.release();
8280 int vrc = VMR3ReqCallWaitU(ptrVM.rawUVM(), 0 /* idDstCpu (saved state, see #6232) */,
8281 (PFNRT)usbDetachCallback, 5,
8282 this, ptrVM.rawUVM(), pUuid);
8283 if (RT_SUCCESS(vrc))
8284 {
8285 LogFlowFunc(("Detached device {%RTuuid}\n", pUuid));
8286
8287 /* notify callbacks */
8288 onUSBDeviceStateChange(aHostDevice, false /* aAttached */, NULL);
8289 }
8290
8291 ComAssertRCRet(vrc, E_FAIL);
8292
8293 return S_OK;
8294}
8295
8296/**
8297 * USB device detach callback used by DetachUSBDevice().
8298 *
8299 * Note that DetachUSBDevice() doesn't return until this callback is executed,
8300 * so we don't use AutoCaller and don't care about reference counters of
8301 * interface pointers passed in.
8302 *
8303 * @thread EMT
8304 */
8305//static
8306DECLCALLBACK(int)
8307Console::usbDetachCallback(Console *that, PUVM pUVM, PCRTUUID aUuid)
8308{
8309 LogFlowFuncEnter();
8310 LogFlowFunc(("that={%p} aUuid={%RTuuid}\n", that, aUuid));
8311
8312 AssertReturn(that && aUuid, VERR_INVALID_PARAMETER);
8313 AssertReturn(!that->isWriteLockOnCurrentThread(), VERR_GENERAL_FAILURE);
8314
8315 int vrc = PDMR3UsbDetachDevice(pUVM, aUuid);
8316
8317 LogFlowFunc(("vrc=%Rrc\n", vrc));
8318 LogFlowFuncLeave();
8319 return vrc;
8320}
8321#endif /* VBOX_WITH_USB */
8322
8323/* Note: FreeBSD needs this whether netflt is used or not. */
8324#if ((defined(RT_OS_LINUX) && !defined(VBOX_WITH_NETFLT)) || defined(RT_OS_FREEBSD))
8325/**
8326 * Helper function to handle host interface device creation and attachment.
8327 *
8328 * @param networkAdapter the network adapter which attachment should be reset
8329 * @return COM status code
8330 *
8331 * @note The caller must lock this object for writing.
8332 *
8333 * @todo Move this back into the driver!
8334 */
8335HRESULT Console::attachToTapInterface(INetworkAdapter *networkAdapter)
8336{
8337 LogFlowThisFunc(("\n"));
8338 /* sanity check */
8339 AssertReturn(isWriteLockOnCurrentThread(), E_FAIL);
8340
8341# ifdef VBOX_STRICT
8342 /* paranoia */
8343 NetworkAttachmentType_T attachment;
8344 networkAdapter->COMGETTER(AttachmentType)(&attachment);
8345 Assert(attachment == NetworkAttachmentType_Bridged);
8346# endif /* VBOX_STRICT */
8347
8348 HRESULT rc = S_OK;
8349
8350 ULONG slot = 0;
8351 rc = networkAdapter->COMGETTER(Slot)(&slot);
8352 AssertComRC(rc);
8353
8354# ifdef RT_OS_LINUX
8355 /*
8356 * Allocate a host interface device
8357 */
8358 int rcVBox = RTFileOpen(&maTapFD[slot], "/dev/net/tun",
8359 RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_INHERIT);
8360 if (RT_SUCCESS(rcVBox))
8361 {
8362 /*
8363 * Set/obtain the tap interface.
8364 */
8365 struct ifreq IfReq;
8366 RT_ZERO(IfReq);
8367 /* The name of the TAP interface we are using */
8368 Bstr tapDeviceName;
8369 rc = networkAdapter->COMGETTER(BridgedInterface)(tapDeviceName.asOutParam());
8370 if (FAILED(rc))
8371 tapDeviceName.setNull(); /* Is this necessary? */
8372 if (tapDeviceName.isEmpty())
8373 {
8374 LogRel(("No TAP device name was supplied.\n"));
8375 rc = setError(E_FAIL, tr("No TAP device name was supplied for the host networking interface"));
8376 }
8377
8378 if (SUCCEEDED(rc))
8379 {
8380 /* If we are using a static TAP device then try to open it. */
8381 Utf8Str str(tapDeviceName);
8382 RTStrCopy(IfReq.ifr_name, sizeof(IfReq.ifr_name), str.c_str()); /** @todo bitch about names which are too long... */
8383 IfReq.ifr_flags = IFF_TAP | IFF_NO_PI;
8384 rcVBox = ioctl(maTapFD[slot], TUNSETIFF, &IfReq);
8385 if (rcVBox != 0)
8386 {
8387 LogRel(("Failed to open the host network interface %ls\n", tapDeviceName.raw()));
8388 rc = setError(E_FAIL,
8389 tr("Failed to open the host network interface %ls"),
8390 tapDeviceName.raw());
8391 }
8392 }
8393 if (SUCCEEDED(rc))
8394 {
8395 /*
8396 * Make it pollable.
8397 */
8398 if (fcntl(maTapFD[slot], F_SETFL, O_NONBLOCK) != -1)
8399 {
8400 Log(("attachToTapInterface: %RTfile %ls\n", maTapFD[slot], tapDeviceName.raw()));
8401 /*
8402 * Here is the right place to communicate the TAP file descriptor and
8403 * the host interface name to the server if/when it becomes really
8404 * necessary.
8405 */
8406 maTAPDeviceName[slot] = tapDeviceName;
8407 rcVBox = VINF_SUCCESS;
8408 }
8409 else
8410 {
8411 int iErr = errno;
8412
8413 LogRel(("Configuration error: Failed to configure /dev/net/tun non blocking. Error: %s\n", strerror(iErr)));
8414 rcVBox = VERR_HOSTIF_BLOCKING;
8415 rc = setError(E_FAIL,
8416 tr("could not set up the host networking device for non blocking access: %s"),
8417 strerror(errno));
8418 }
8419 }
8420 }
8421 else
8422 {
8423 LogRel(("Configuration error: Failed to open /dev/net/tun rc=%Rrc\n", rcVBox));
8424 switch (rcVBox)
8425 {
8426 case VERR_ACCESS_DENIED:
8427 /* will be handled by our caller */
8428 rc = rcVBox;
8429 break;
8430 default:
8431 rc = setError(E_FAIL,
8432 tr("Could not set up the host networking device: %Rrc"),
8433 rcVBox);
8434 break;
8435 }
8436 }
8437
8438# elif defined(RT_OS_FREEBSD)
8439 /*
8440 * Set/obtain the tap interface.
8441 */
8442 /* The name of the TAP interface we are using */
8443 Bstr tapDeviceName;
8444 rc = networkAdapter->COMGETTER(BridgedInterface)(tapDeviceName.asOutParam());
8445 if (FAILED(rc))
8446 tapDeviceName.setNull(); /* Is this necessary? */
8447 if (tapDeviceName.isEmpty())
8448 {
8449 LogRel(("No TAP device name was supplied.\n"));
8450 rc = setError(E_FAIL, tr("No TAP device name was supplied for the host networking interface"));
8451 }
8452 char szTapdev[1024] = "/dev/";
8453 /* If we are using a static TAP device then try to open it. */
8454 Utf8Str str(tapDeviceName);
8455 if (str.length() + strlen(szTapdev) <= sizeof(szTapdev))
8456 strcat(szTapdev, str.c_str());
8457 else
8458 memcpy(szTapdev + strlen(szTapdev), str.c_str(),
8459 sizeof(szTapdev) - strlen(szTapdev) - 1); /** @todo bitch about names which are too long... */
8460 int rcVBox = RTFileOpen(&maTapFD[slot], szTapdev,
8461 RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_INHERIT | RTFILE_O_NON_BLOCK);
8462
8463 if (RT_SUCCESS(rcVBox))
8464 maTAPDeviceName[slot] = tapDeviceName;
8465 else
8466 {
8467 switch (rcVBox)
8468 {
8469 case VERR_ACCESS_DENIED:
8470 /* will be handled by our caller */
8471 rc = rcVBox;
8472 break;
8473 default:
8474 rc = setError(E_FAIL,
8475 tr("Failed to open the host network interface %ls"),
8476 tapDeviceName.raw());
8477 break;
8478 }
8479 }
8480# else
8481# error "huh?"
8482# endif
8483 /* in case of failure, cleanup. */
8484 if (RT_FAILURE(rcVBox) && SUCCEEDED(rc))
8485 {
8486 LogRel(("General failure attaching to host interface\n"));
8487 rc = setError(E_FAIL,
8488 tr("General failure attaching to host interface"));
8489 }
8490 LogFlowThisFunc(("rc=%d\n", rc));
8491 return rc;
8492}
8493
8494
8495/**
8496 * Helper function to handle detachment from a host interface
8497 *
8498 * @param networkAdapter the network adapter which attachment should be reset
8499 * @return COM status code
8500 *
8501 * @note The caller must lock this object for writing.
8502 *
8503 * @todo Move this back into the driver!
8504 */
8505HRESULT Console::detachFromTapInterface(INetworkAdapter *networkAdapter)
8506{
8507 /* sanity check */
8508 LogFlowThisFunc(("\n"));
8509 AssertReturn(isWriteLockOnCurrentThread(), E_FAIL);
8510
8511 HRESULT rc = S_OK;
8512# ifdef VBOX_STRICT
8513 /* paranoia */
8514 NetworkAttachmentType_T attachment;
8515 networkAdapter->COMGETTER(AttachmentType)(&attachment);
8516 Assert(attachment == NetworkAttachmentType_Bridged);
8517# endif /* VBOX_STRICT */
8518
8519 ULONG slot = 0;
8520 rc = networkAdapter->COMGETTER(Slot)(&slot);
8521 AssertComRC(rc);
8522
8523 /* is there an open TAP device? */
8524 if (maTapFD[slot] != NIL_RTFILE)
8525 {
8526 /*
8527 * Close the file handle.
8528 */
8529 Bstr tapDeviceName, tapTerminateApplication;
8530 bool isStatic = true;
8531 rc = networkAdapter->COMGETTER(BridgedInterface)(tapDeviceName.asOutParam());
8532 if (FAILED(rc) || tapDeviceName.isEmpty())
8533 {
8534 /* If the name is empty, this is a dynamic TAP device, so close it now,
8535 so that the termination script can remove the interface. Otherwise we still
8536 need the FD to pass to the termination script. */
8537 isStatic = false;
8538 int rcVBox = RTFileClose(maTapFD[slot]);
8539 AssertRC(rcVBox);
8540 maTapFD[slot] = NIL_RTFILE;
8541 }
8542 if (isStatic)
8543 {
8544 /* If we are using a static TAP device, we close it now, after having called the
8545 termination script. */
8546 int rcVBox = RTFileClose(maTapFD[slot]);
8547 AssertRC(rcVBox);
8548 }
8549 /* the TAP device name and handle are no longer valid */
8550 maTapFD[slot] = NIL_RTFILE;
8551 maTAPDeviceName[slot] = "";
8552 }
8553 LogFlowThisFunc(("returning %d\n", rc));
8554 return rc;
8555}
8556#endif /* (RT_OS_LINUX || RT_OS_FREEBSD) && !VBOX_WITH_NETFLT */
8557
8558/**
8559 * Called at power down to terminate host interface networking.
8560 *
8561 * @note The caller must lock this object for writing.
8562 */
8563HRESULT Console::powerDownHostInterfaces()
8564{
8565 LogFlowThisFunc(("\n"));
8566
8567 /* sanity check */
8568 AssertReturn(isWriteLockOnCurrentThread(), E_FAIL);
8569
8570 /*
8571 * host interface termination handling
8572 */
8573 HRESULT rc = S_OK;
8574 ComPtr<IVirtualBox> pVirtualBox;
8575 mMachine->COMGETTER(Parent)(pVirtualBox.asOutParam());
8576 ComPtr<ISystemProperties> pSystemProperties;
8577 if (pVirtualBox)
8578 pVirtualBox->COMGETTER(SystemProperties)(pSystemProperties.asOutParam());
8579 ChipsetType_T chipsetType = ChipsetType_PIIX3;
8580 mMachine->COMGETTER(ChipsetType)(&chipsetType);
8581 ULONG maxNetworkAdapters = 0;
8582 if (pSystemProperties)
8583 pSystemProperties->GetMaxNetworkAdapters(chipsetType, &maxNetworkAdapters);
8584
8585 for (ULONG slot = 0; slot < maxNetworkAdapters; slot++)
8586 {
8587 ComPtr<INetworkAdapter> pNetworkAdapter;
8588 rc = mMachine->GetNetworkAdapter(slot, pNetworkAdapter.asOutParam());
8589 if (FAILED(rc)) break;
8590
8591 BOOL enabled = FALSE;
8592 pNetworkAdapter->COMGETTER(Enabled)(&enabled);
8593 if (!enabled)
8594 continue;
8595
8596 NetworkAttachmentType_T attachment;
8597 pNetworkAdapter->COMGETTER(AttachmentType)(&attachment);
8598 if (attachment == NetworkAttachmentType_Bridged)
8599 {
8600#if ((defined(RT_OS_LINUX) || defined(RT_OS_FREEBSD)) && !defined(VBOX_WITH_NETFLT))
8601 HRESULT rc2 = detachFromTapInterface(pNetworkAdapter);
8602 if (FAILED(rc2) && SUCCEEDED(rc))
8603 rc = rc2;
8604#endif /* (RT_OS_LINUX || RT_OS_FREEBSD) && !VBOX_WITH_NETFLT */
8605 }
8606 }
8607
8608 return rc;
8609}
8610
8611
8612/**
8613 * Process callback handler for VMR3LoadFromFile, VMR3LoadFromStream, VMR3Save
8614 * and VMR3Teleport.
8615 *
8616 * @param pUVM The user mode VM handle.
8617 * @param uPercent Completion percentage (0-100).
8618 * @param pvUser Pointer to an IProgress instance.
8619 * @return VINF_SUCCESS.
8620 */
8621/*static*/
8622DECLCALLBACK(int) Console::stateProgressCallback(PUVM pUVM, unsigned uPercent, void *pvUser)
8623{
8624 IProgress *pProgress = static_cast<IProgress *>(pvUser);
8625
8626 /* update the progress object */
8627 if (pProgress)
8628 pProgress->SetCurrentOperationProgress(uPercent);
8629
8630 NOREF(pUVM);
8631 return VINF_SUCCESS;
8632}
8633
8634/**
8635 * @copydoc FNVMATERROR
8636 *
8637 * @remarks Might be some tiny serialization concerns with access to the string
8638 * object here...
8639 */
8640/*static*/ DECLCALLBACK(void)
8641Console::genericVMSetErrorCallback(PUVM pUVM, void *pvUser, int rc, RT_SRC_POS_DECL,
8642 const char *pszErrorFmt, va_list va)
8643{
8644 Utf8Str *pErrorText = (Utf8Str *)pvUser;
8645 AssertPtr(pErrorText);
8646
8647 /* We ignore RT_SRC_POS_DECL arguments to avoid confusion of end-users. */
8648 va_list va2;
8649 va_copy(va2, va);
8650
8651 /* Append to any the existing error message. */
8652 if (pErrorText->length())
8653 *pErrorText = Utf8StrFmt("%s.\n%N (%Rrc)", pErrorText->c_str(),
8654 pszErrorFmt, &va2, rc, rc);
8655 else
8656 *pErrorText = Utf8StrFmt("%N (%Rrc)", pszErrorFmt, &va2, rc, rc);
8657
8658 va_end(va2);
8659
8660 NOREF(pUVM);
8661}
8662
8663/**
8664 * VM runtime error callback function.
8665 * See VMSetRuntimeError for the detailed description of parameters.
8666 *
8667 * @param pUVM The user mode VM handle. Ignored, so passing NULL
8668 * is fine.
8669 * @param pvUser The user argument, pointer to the Console instance.
8670 * @param fFlags The action flags. See VMSETRTERR_FLAGS_*.
8671 * @param pszErrorId Error ID string.
8672 * @param pszFormat Error message format string.
8673 * @param va Error message arguments.
8674 * @thread EMT.
8675 */
8676/* static */ DECLCALLBACK(void)
8677Console::setVMRuntimeErrorCallback(PUVM pUVM, void *pvUser, uint32_t fFlags,
8678 const char *pszErrorId,
8679 const char *pszFormat, va_list va)
8680{
8681 bool const fFatal = !!(fFlags & VMSETRTERR_FLAGS_FATAL);
8682 LogFlowFuncEnter();
8683
8684 Console *that = static_cast<Console *>(pvUser);
8685 AssertReturnVoid(that);
8686
8687 Utf8Str message(pszFormat, va);
8688
8689 LogRel(("Console: VM runtime error: fatal=%RTbool, errorID=%s message=\"%s\"\n",
8690 fFatal, pszErrorId, message.c_str()));
8691
8692 that->onRuntimeError(BOOL(fFatal), Bstr(pszErrorId).raw(), Bstr(message).raw());
8693
8694 LogFlowFuncLeave(); NOREF(pUVM);
8695}
8696
8697/**
8698 * Captures USB devices that match filters of the VM.
8699 * Called at VM startup.
8700 *
8701 * @param pUVM The VM handle.
8702 */
8703HRESULT Console::captureUSBDevices(PUVM pUVM)
8704{
8705 LogFlowThisFunc(("\n"));
8706
8707 /* sanity check */
8708 AssertReturn(!isWriteLockOnCurrentThread(), E_FAIL);
8709 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
8710
8711 /* If the machine has a USB controller, ask the USB proxy service to
8712 * capture devices */
8713 if (mfVMHasUsbController)
8714 {
8715 /* release the lock before calling Host in VBoxSVC since Host may call
8716 * us back from under its lock (e.g. onUSBDeviceAttach()) which would
8717 * produce an inter-process dead-lock otherwise. */
8718 alock.release();
8719
8720 HRESULT hrc = mControl->AutoCaptureUSBDevices();
8721 ComAssertComRCRetRC(hrc);
8722 }
8723
8724 return S_OK;
8725}
8726
8727
8728/**
8729 * Detach all USB device which are attached to the VM for the
8730 * purpose of clean up and such like.
8731 */
8732void Console::detachAllUSBDevices(bool aDone)
8733{
8734 LogFlowThisFunc(("aDone=%RTbool\n", aDone));
8735
8736 /* sanity check */
8737 AssertReturnVoid(!isWriteLockOnCurrentThread());
8738 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
8739
8740 mUSBDevices.clear();
8741
8742 /* release the lock before calling Host in VBoxSVC since Host may call
8743 * us back from under its lock (e.g. onUSBDeviceAttach()) which would
8744 * produce an inter-process dead-lock otherwise. */
8745 alock.release();
8746
8747 mControl->DetachAllUSBDevices(aDone);
8748}
8749
8750/**
8751 * @note Locks this object for writing.
8752 */
8753void Console::processRemoteUSBDevices(uint32_t u32ClientId, VRDEUSBDEVICEDESC *pDevList, uint32_t cbDevList, bool fDescExt)
8754{
8755 LogFlowThisFuncEnter();
8756 LogFlowThisFunc(("u32ClientId = %d, pDevList=%p, cbDevList = %d, fDescExt = %d\n", u32ClientId, pDevList, cbDevList, fDescExt));
8757
8758 AutoCaller autoCaller(this);
8759 if (!autoCaller.isOk())
8760 {
8761 /* Console has been already uninitialized, deny request */
8762 AssertMsgFailed(("Console is already uninitialized\n"));
8763 LogFlowThisFunc(("Console is already uninitialized\n"));
8764 LogFlowThisFuncLeave();
8765 return;
8766 }
8767
8768 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
8769
8770 /*
8771 * Mark all existing remote USB devices as dirty.
8772 */
8773 for (RemoteUSBDeviceList::iterator it = mRemoteUSBDevices.begin();
8774 it != mRemoteUSBDevices.end();
8775 ++it)
8776 {
8777 (*it)->dirty(true);
8778 }
8779
8780 /*
8781 * Process the pDevList and add devices those are not already in the mRemoteUSBDevices list.
8782 */
8783 /** @todo (sunlover) REMOTE_USB Strict validation of the pDevList. */
8784 VRDEUSBDEVICEDESC *e = pDevList;
8785
8786 /* The cbDevList condition must be checked first, because the function can
8787 * receive pDevList = NULL and cbDevList = 0 on client disconnect.
8788 */
8789 while (cbDevList >= 2 && e->oNext)
8790 {
8791 /* Sanitize incoming strings in case they aren't valid UTF-8. */
8792 if (e->oManufacturer)
8793 RTStrPurgeEncoding((char *)e + e->oManufacturer);
8794 if (e->oProduct)
8795 RTStrPurgeEncoding((char *)e + e->oProduct);
8796 if (e->oSerialNumber)
8797 RTStrPurgeEncoding((char *)e + e->oSerialNumber);
8798
8799 LogFlowThisFunc(("vendor %04X, product %04X, name = %s\n",
8800 e->idVendor, e->idProduct,
8801 e->oProduct? (char *)e + e->oProduct: ""));
8802
8803 bool fNewDevice = true;
8804
8805 for (RemoteUSBDeviceList::iterator it = mRemoteUSBDevices.begin();
8806 it != mRemoteUSBDevices.end();
8807 ++it)
8808 {
8809 if ((*it)->devId() == e->id
8810 && (*it)->clientId() == u32ClientId)
8811 {
8812 /* The device is already in the list. */
8813 (*it)->dirty(false);
8814 fNewDevice = false;
8815 break;
8816 }
8817 }
8818
8819 if (fNewDevice)
8820 {
8821 LogRel(("Remote USB: ++++ Vendor %04X. Product %04X. Name = [%s].\n",
8822 e->idVendor, e->idProduct, e->oProduct? (char *)e + e->oProduct: ""));
8823
8824 /* Create the device object and add the new device to list. */
8825 ComObjPtr<RemoteUSBDevice> pUSBDevice;
8826 pUSBDevice.createObject();
8827 pUSBDevice->init(u32ClientId, e, fDescExt);
8828
8829 mRemoteUSBDevices.push_back(pUSBDevice);
8830
8831 /* Check if the device is ok for current USB filters. */
8832 BOOL fMatched = FALSE;
8833 ULONG fMaskedIfs = 0;
8834
8835 HRESULT hrc = mControl->RunUSBDeviceFilters(pUSBDevice, &fMatched, &fMaskedIfs);
8836
8837 AssertComRC(hrc);
8838
8839 LogFlowThisFunc(("USB filters return %d %#x\n", fMatched, fMaskedIfs));
8840
8841 if (fMatched)
8842 {
8843 alock.release();
8844 hrc = onUSBDeviceAttach(pUSBDevice, NULL, fMaskedIfs);
8845 alock.acquire();
8846
8847 /// @todo (r=dmik) warning reporting subsystem
8848
8849 if (hrc == S_OK)
8850 {
8851 LogFlowThisFunc(("Device attached\n"));
8852 pUSBDevice->captured(true);
8853 }
8854 }
8855 }
8856
8857 if (cbDevList < e->oNext)
8858 {
8859 LogWarningThisFunc(("cbDevList %d > oNext %d\n",
8860 cbDevList, e->oNext));
8861 break;
8862 }
8863
8864 cbDevList -= e->oNext;
8865
8866 e = (VRDEUSBDEVICEDESC *)((uint8_t *)e + e->oNext);
8867 }
8868
8869 /*
8870 * Remove dirty devices, that is those which are not reported by the server anymore.
8871 */
8872 for (;;)
8873 {
8874 ComObjPtr<RemoteUSBDevice> pUSBDevice;
8875
8876 RemoteUSBDeviceList::iterator it = mRemoteUSBDevices.begin();
8877 while (it != mRemoteUSBDevices.end())
8878 {
8879 if ((*it)->dirty())
8880 {
8881 pUSBDevice = *it;
8882 break;
8883 }
8884
8885 ++it;
8886 }
8887
8888 if (!pUSBDevice)
8889 {
8890 break;
8891 }
8892
8893 USHORT vendorId = 0;
8894 pUSBDevice->COMGETTER(VendorId)(&vendorId);
8895
8896 USHORT productId = 0;
8897 pUSBDevice->COMGETTER(ProductId)(&productId);
8898
8899 Bstr product;
8900 pUSBDevice->COMGETTER(Product)(product.asOutParam());
8901
8902 LogRel(("Remote USB: ---- Vendor %04X. Product %04X. Name = [%ls].\n",
8903 vendorId, productId, product.raw()));
8904
8905 /* Detach the device from VM. */
8906 if (pUSBDevice->captured())
8907 {
8908 Bstr uuid;
8909 pUSBDevice->COMGETTER(Id)(uuid.asOutParam());
8910 alock.release();
8911 onUSBDeviceDetach(uuid.raw(), NULL);
8912 alock.acquire();
8913 }
8914
8915 /* And remove it from the list. */
8916 mRemoteUSBDevices.erase(it);
8917 }
8918
8919 LogFlowThisFuncLeave();
8920}
8921
8922/**
8923 * Progress cancelation callback for fault tolerance VM poweron
8924 */
8925static void faultToleranceProgressCancelCallback(void *pvUser)
8926{
8927 PUVM pUVM = (PUVM)pvUser;
8928
8929 if (pUVM)
8930 FTMR3CancelStandby(pUVM);
8931}
8932
8933/**
8934 * Thread function which starts the VM (also from saved state) and
8935 * track progress.
8936 *
8937 * @param Thread The thread id.
8938 * @param pvUser Pointer to a VMPowerUpTask structure.
8939 * @return VINF_SUCCESS (ignored).
8940 *
8941 * @note Locks the Console object for writing.
8942 */
8943/*static*/
8944DECLCALLBACK(int) Console::powerUpThread(RTTHREAD Thread, void *pvUser)
8945{
8946 LogFlowFuncEnter();
8947
8948 std::auto_ptr<VMPowerUpTask> task(static_cast<VMPowerUpTask *>(pvUser));
8949 AssertReturn(task.get(), VERR_INVALID_PARAMETER);
8950
8951 AssertReturn(!task->mConsole.isNull(), VERR_INVALID_PARAMETER);
8952 AssertReturn(!task->mProgress.isNull(), VERR_INVALID_PARAMETER);
8953
8954 VirtualBoxBase::initializeComForThread();
8955
8956 HRESULT rc = S_OK;
8957 int vrc = VINF_SUCCESS;
8958
8959 /* Set up a build identifier so that it can be seen from core dumps what
8960 * exact build was used to produce the core. */
8961 static char saBuildID[40];
8962 RTStrPrintf(saBuildID, sizeof(saBuildID), "%s%s%s%s VirtualBox %s r%u %s%s%s%s",
8963 "BU", "IL", "DI", "D", RTBldCfgVersion(), RTBldCfgRevision(), "BU", "IL", "DI", "D");
8964
8965 ComObjPtr<Console> pConsole = task->mConsole;
8966
8967 /* Note: no need to use addCaller() because VMPowerUpTask does that */
8968
8969 /* The lock is also used as a signal from the task initiator (which
8970 * releases it only after RTThreadCreate()) that we can start the job */
8971 AutoWriteLock alock(pConsole COMMA_LOCKVAL_SRC_POS);
8972
8973 /* sanity */
8974 Assert(pConsole->mpUVM == NULL);
8975
8976 try
8977 {
8978 // Create the VMM device object, which starts the HGCM thread; do this only
8979 // once for the console, for the pathological case that the same console
8980 // object is used to power up a VM twice. VirtualBox 4.0: we now do that
8981 // here instead of the Console constructor (see Console::init())
8982 if (!pConsole->m_pVMMDev)
8983 {
8984 pConsole->m_pVMMDev = new VMMDev(pConsole);
8985 AssertReturn(pConsole->m_pVMMDev, E_FAIL);
8986 }
8987
8988 /* wait for auto reset ops to complete so that we can successfully lock
8989 * the attached hard disks by calling LockMedia() below */
8990 for (VMPowerUpTask::ProgressList::const_iterator
8991 it = task->hardDiskProgresses.begin();
8992 it != task->hardDiskProgresses.end(); ++it)
8993 {
8994 HRESULT rc2 = (*it)->WaitForCompletion(-1);
8995 AssertComRC(rc2);
8996
8997 rc = task->mProgress->SetNextOperation(BstrFmt(tr("Disk Image Reset Operation - Immutable Image")).raw(), 1);
8998 AssertComRCReturnRC(rc);
8999 }
9000
9001 /*
9002 * Lock attached media. This method will also check their accessibility.
9003 * If we're a teleporter, we'll have to postpone this action so we can
9004 * migrate between local processes.
9005 *
9006 * Note! The media will be unlocked automatically by
9007 * SessionMachine::setMachineState() when the VM is powered down.
9008 */
9009 if ( !task->mTeleporterEnabled
9010 && task->mEnmFaultToleranceState != FaultToleranceState_Standby)
9011 {
9012 rc = pConsole->mControl->LockMedia();
9013 if (FAILED(rc)) throw rc;
9014 }
9015
9016 /* Create the VRDP server. In case of headless operation, this will
9017 * also create the framebuffer, required at VM creation.
9018 */
9019 ConsoleVRDPServer *server = pConsole->consoleVRDPServer();
9020 Assert(server);
9021
9022 /* Does VRDP server call Console from the other thread?
9023 * Not sure (and can change), so release the lock just in case.
9024 */
9025 alock.release();
9026 vrc = server->Launch();
9027 alock.acquire();
9028
9029 if (vrc == VERR_NET_ADDRESS_IN_USE)
9030 {
9031 Utf8Str errMsg;
9032 Bstr bstr;
9033 pConsole->mVRDEServer->GetVRDEProperty(Bstr("TCP/Ports").raw(), bstr.asOutParam());
9034 Utf8Str ports = bstr;
9035 errMsg = Utf8StrFmt(tr("VirtualBox Remote Desktop Extension server can't bind to the port: %s"),
9036 ports.c_str());
9037 LogRel(("VRDE: Warning: failed to launch VRDE server (%Rrc): '%s'\n",
9038 vrc, errMsg.c_str()));
9039 }
9040 else if (vrc == VINF_NOT_SUPPORTED)
9041 {
9042 /* This means that the VRDE is not installed. */
9043 LogRel(("VRDE: VirtualBox Remote Desktop Extension is not available.\n"));
9044 }
9045 else if (RT_FAILURE(vrc))
9046 {
9047 /* Fail, if the server is installed but can't start. */
9048 Utf8Str errMsg;
9049 switch (vrc)
9050 {
9051 case VERR_FILE_NOT_FOUND:
9052 {
9053 /* VRDE library file is missing. */
9054 errMsg = Utf8StrFmt(tr("Could not find the VirtualBox Remote Desktop Extension library."));
9055 break;
9056 }
9057 default:
9058 errMsg = Utf8StrFmt(tr("Failed to launch Remote Desktop Extension server (%Rrc)"),
9059 vrc);
9060 }
9061 LogRel(("VRDE: Failed: (%Rrc), error message: '%s'\n",
9062 vrc, errMsg.c_str()));
9063 throw setErrorStatic(E_FAIL, errMsg.c_str());
9064 }
9065
9066 ComPtr<IMachine> pMachine = pConsole->machine();
9067 ULONG cCpus = 1;
9068 pMachine->COMGETTER(CPUCount)(&cCpus);
9069
9070 /*
9071 * Create the VM
9072 *
9073 * Note! Release the lock since EMT will call Console. It's safe because
9074 * mMachineState is either Starting or Restoring state here.
9075 */
9076 alock.release();
9077
9078 PVM pVM;
9079 vrc = VMR3Create(cCpus,
9080 pConsole->mpVmm2UserMethods,
9081 Console::genericVMSetErrorCallback,
9082 &task->mErrorMsg,
9083 task->mConfigConstructor,
9084 static_cast<Console *>(pConsole),
9085 &pVM, NULL);
9086
9087 alock.acquire();
9088
9089 /* Enable client connections to the server. */
9090 pConsole->consoleVRDPServer()->EnableConnections();
9091
9092 if (RT_SUCCESS(vrc))
9093 {
9094 do
9095 {
9096 /*
9097 * Register our load/save state file handlers
9098 */
9099 vrc = SSMR3RegisterExternal(pConsole->mpUVM, sSSMConsoleUnit, 0 /*iInstance*/, sSSMConsoleVer, 0 /* cbGuess */,
9100 NULL, NULL, NULL,
9101 NULL, saveStateFileExec, NULL,
9102 NULL, loadStateFileExec, NULL,
9103 static_cast<Console *>(pConsole));
9104 AssertRCBreak(vrc);
9105
9106 vrc = static_cast<Console *>(pConsole)->getDisplay()->registerSSM(pConsole->mpUVM);
9107 AssertRC(vrc);
9108 if (RT_FAILURE(vrc))
9109 break;
9110
9111 /*
9112 * Synchronize debugger settings
9113 */
9114 MachineDebugger *machineDebugger = pConsole->getMachineDebugger();
9115 if (machineDebugger)
9116 machineDebugger->flushQueuedSettings();
9117
9118 /*
9119 * Shared Folders
9120 */
9121 if (pConsole->m_pVMMDev->isShFlActive())
9122 {
9123 /* Does the code below call Console from the other thread?
9124 * Not sure, so release the lock just in case. */
9125 alock.release();
9126
9127 for (SharedFolderDataMap::const_iterator it = task->mSharedFolders.begin();
9128 it != task->mSharedFolders.end();
9129 ++it)
9130 {
9131 const SharedFolderData &d = it->second;
9132 rc = pConsole->createSharedFolder(it->first, d);
9133 if (FAILED(rc))
9134 {
9135 ErrorInfoKeeper eik;
9136 pConsole->setVMRuntimeErrorCallbackF(0, "BrokenSharedFolder",
9137 N_("The shared folder '%s' could not be set up: %ls.\n"
9138 "The shared folder setup will not be complete. It is recommended to power down the virtual "
9139 "machine and fix the shared folder settings while the machine is not running"),
9140 it->first.c_str(), eik.getText().raw());
9141 }
9142 }
9143 if (FAILED(rc))
9144 rc = S_OK; // do not fail with broken shared folders
9145
9146 /* acquire the lock again */
9147 alock.acquire();
9148 }
9149
9150 /* release the lock before a lengthy operation */
9151 alock.release();
9152
9153 /*
9154 * Capture USB devices.
9155 */
9156 rc = pConsole->captureUSBDevices(pConsole->mpUVM);
9157 if (FAILED(rc))
9158 break;
9159
9160 /* Load saved state? */
9161 if (task->mSavedStateFile.length())
9162 {
9163 LogFlowFunc(("Restoring saved state from '%s'...\n",
9164 task->mSavedStateFile.c_str()));
9165
9166 vrc = VMR3LoadFromFile(pConsole->mpUVM,
9167 task->mSavedStateFile.c_str(),
9168 Console::stateProgressCallback,
9169 static_cast<IProgress *>(task->mProgress));
9170
9171 if (RT_SUCCESS(vrc))
9172 {
9173 if (task->mStartPaused)
9174 /* done */
9175 pConsole->setMachineState(MachineState_Paused);
9176 else
9177 {
9178 /* Start/Resume the VM execution */
9179#ifdef VBOX_WITH_EXTPACK
9180 vrc = pConsole->mptrExtPackManager->callAllVmPowerOnHooks(pConsole, pVM);
9181#endif
9182 if (RT_SUCCESS(vrc))
9183 vrc = VMR3Resume(pConsole->mpUVM, VMRESUMEREASON_STATE_RESTORED);
9184 AssertLogRelRC(vrc);
9185 }
9186 }
9187
9188 /* Power off in case we failed loading or resuming the VM */
9189 if (RT_FAILURE(vrc))
9190 {
9191 int vrc2 = VMR3PowerOff(pConsole->mpUVM); AssertLogRelRC(vrc2);
9192#ifdef VBOX_WITH_EXTPACK
9193 pConsole->mptrExtPackManager->callAllVmPowerOffHooks(pConsole, pVM);
9194#endif
9195 }
9196 }
9197 else if (task->mTeleporterEnabled)
9198 {
9199 /* -> ConsoleImplTeleporter.cpp */
9200 bool fPowerOffOnFailure;
9201 rc = pConsole->teleporterTrg(pConsole->mpUVM, pMachine, &task->mErrorMsg, task->mStartPaused,
9202 task->mProgress, &fPowerOffOnFailure);
9203 if (FAILED(rc) && fPowerOffOnFailure)
9204 {
9205 ErrorInfoKeeper eik;
9206 int vrc2 = VMR3PowerOff(pConsole->mpUVM); AssertLogRelRC(vrc2);
9207#ifdef VBOX_WITH_EXTPACK
9208 pConsole->mptrExtPackManager->callAllVmPowerOffHooks(pConsole, pVM);
9209#endif
9210 }
9211 }
9212 else if (task->mEnmFaultToleranceState != FaultToleranceState_Inactive)
9213 {
9214 /*
9215 * Get the config.
9216 */
9217 ULONG uPort;
9218 ULONG uInterval;
9219 Bstr bstrAddress, bstrPassword;
9220
9221 rc = pMachine->COMGETTER(FaultTolerancePort)(&uPort);
9222 if (SUCCEEDED(rc))
9223 {
9224 rc = pMachine->COMGETTER(FaultToleranceSyncInterval)(&uInterval);
9225 if (SUCCEEDED(rc))
9226 rc = pMachine->COMGETTER(FaultToleranceAddress)(bstrAddress.asOutParam());
9227 if (SUCCEEDED(rc))
9228 rc = pMachine->COMGETTER(FaultTolerancePassword)(bstrPassword.asOutParam());
9229 }
9230 if (task->mProgress->setCancelCallback(faultToleranceProgressCancelCallback, pConsole->mpUVM))
9231 {
9232 if (SUCCEEDED(rc))
9233 {
9234 Utf8Str strAddress(bstrAddress);
9235 const char *pszAddress = strAddress.isEmpty() ? NULL : strAddress.c_str();
9236 Utf8Str strPassword(bstrPassword);
9237 const char *pszPassword = strPassword.isEmpty() ? NULL : strPassword.c_str();
9238
9239 /* Power on the FT enabled VM. */
9240#ifdef VBOX_WITH_EXTPACK
9241 vrc = pConsole->mptrExtPackManager->callAllVmPowerOnHooks(pConsole, pVM);
9242#endif
9243 if (RT_SUCCESS(vrc))
9244 vrc = FTMR3PowerOn(pConsole->mpUVM,
9245 task->mEnmFaultToleranceState == FaultToleranceState_Master /* fMaster */,
9246 uInterval,
9247 pszAddress,
9248 uPort,
9249 pszPassword);
9250 AssertLogRelRC(vrc);
9251 }
9252 task->mProgress->setCancelCallback(NULL, NULL);
9253 }
9254 else
9255 rc = E_FAIL;
9256 }
9257 else if (task->mStartPaused)
9258 /* done */
9259 pConsole->setMachineState(MachineState_Paused);
9260 else
9261 {
9262 /* Power on the VM (i.e. start executing) */
9263#ifdef VBOX_WITH_EXTPACK
9264 vrc = pConsole->mptrExtPackManager->callAllVmPowerOnHooks(pConsole, pVM);
9265#endif
9266 if (RT_SUCCESS(vrc))
9267 vrc = VMR3PowerOn(pConsole->mpUVM);
9268 AssertLogRelRC(vrc);
9269 }
9270
9271 /* acquire the lock again */
9272 alock.acquire();
9273 }
9274 while (0);
9275
9276 /* On failure, destroy the VM */
9277 if (FAILED(rc) || RT_FAILURE(vrc))
9278 {
9279 /* preserve existing error info */
9280 ErrorInfoKeeper eik;
9281
9282 /* powerDown() will call VMR3Destroy() and do all necessary
9283 * cleanup (VRDP, USB devices) */
9284 alock.release();
9285 HRESULT rc2 = pConsole->powerDown();
9286 alock.acquire();
9287 AssertComRC(rc2);
9288 }
9289 else
9290 {
9291 /*
9292 * Deregister the VMSetError callback. This is necessary as the
9293 * pfnVMAtError() function passed to VMR3Create() is supposed to
9294 * be sticky but our error callback isn't.
9295 */
9296 alock.release();
9297 VMR3AtErrorDeregister(pConsole->mpUVM, Console::genericVMSetErrorCallback, &task->mErrorMsg);
9298 /** @todo register another VMSetError callback? */
9299 alock.acquire();
9300 }
9301 }
9302 else
9303 {
9304 /*
9305 * If VMR3Create() failed it has released the VM memory.
9306 */
9307 VMR3ReleaseUVM(pConsole->mpUVM);
9308 pConsole->mpUVM = NULL;
9309 }
9310
9311 if (SUCCEEDED(rc) && RT_FAILURE(vrc))
9312 {
9313 /* If VMR3Create() or one of the other calls in this function fail,
9314 * an appropriate error message has been set in task->mErrorMsg.
9315 * However since that happens via a callback, the rc status code in
9316 * this function is not updated.
9317 */
9318 if (!task->mErrorMsg.length())
9319 {
9320 /* If the error message is not set but we've got a failure,
9321 * convert the VBox status code into a meaningful error message.
9322 * This becomes unused once all the sources of errors set the
9323 * appropriate error message themselves.
9324 */
9325 AssertMsgFailed(("Missing error message during powerup for status code %Rrc\n", vrc));
9326 task->mErrorMsg = Utf8StrFmt(tr("Failed to start VM execution (%Rrc)"),
9327 vrc);
9328 }
9329
9330 /* Set the error message as the COM error.
9331 * Progress::notifyComplete() will pick it up later. */
9332 throw setErrorStatic(E_FAIL, task->mErrorMsg.c_str());
9333 }
9334 }
9335 catch (HRESULT aRC) { rc = aRC; }
9336
9337 if ( pConsole->mMachineState == MachineState_Starting
9338 || pConsole->mMachineState == MachineState_Restoring
9339 || pConsole->mMachineState == MachineState_TeleportingIn
9340 )
9341 {
9342 /* We are still in the Starting/Restoring state. This means one of:
9343 *
9344 * 1) we failed before VMR3Create() was called;
9345 * 2) VMR3Create() failed.
9346 *
9347 * In both cases, there is no need to call powerDown(), but we still
9348 * need to go back to the PoweredOff/Saved state. Reuse
9349 * vmstateChangeCallback() for that purpose.
9350 */
9351
9352 /* preserve existing error info */
9353 ErrorInfoKeeper eik;
9354
9355 Assert(pConsole->mpUVM == NULL);
9356 vmstateChangeCallback(NULL, VMSTATE_TERMINATED, VMSTATE_CREATING, pConsole);
9357 }
9358
9359 /*
9360 * Evaluate the final result. Note that the appropriate mMachineState value
9361 * is already set by vmstateChangeCallback() in all cases.
9362 */
9363
9364 /* release the lock, don't need it any more */
9365 alock.release();
9366
9367 if (SUCCEEDED(rc))
9368 {
9369 /* Notify the progress object of the success */
9370 task->mProgress->notifyComplete(S_OK);
9371 }
9372 else
9373 {
9374 /* The progress object will fetch the current error info */
9375 task->mProgress->notifyComplete(rc);
9376 LogRel(("Power up failed (vrc=%Rrc, rc=%Rhrc (%#08X))\n", vrc, rc, rc));
9377 }
9378
9379 /* Notify VBoxSVC and any waiting openRemoteSession progress object. */
9380 pConsole->mControl->EndPowerUp(rc);
9381
9382#if defined(RT_OS_WINDOWS)
9383 /* uninitialize COM */
9384 CoUninitialize();
9385#endif
9386
9387 LogFlowFuncLeave();
9388
9389 return VINF_SUCCESS;
9390}
9391
9392
9393/**
9394 * Reconfigures a medium attachment (part of taking or deleting an online snapshot).
9395 *
9396 * @param pConsole Reference to the console object.
9397 * @param pUVM The VM handle.
9398 * @param lInstance The instance of the controller.
9399 * @param pcszDevice The name of the controller type.
9400 * @param enmBus The storage bus type of the controller.
9401 * @param fSetupMerge Whether to set up a medium merge
9402 * @param uMergeSource Merge source image index
9403 * @param uMergeTarget Merge target image index
9404 * @param aMediumAtt The medium attachment.
9405 * @param aMachineState The current machine state.
9406 * @param phrc Where to store com error - only valid if we return VERR_GENERAL_FAILURE.
9407 * @return VBox status code.
9408 */
9409/* static */
9410DECLCALLBACK(int) Console::reconfigureMediumAttachment(Console *pConsole,
9411 PUVM pUVM,
9412 const char *pcszDevice,
9413 unsigned uInstance,
9414 StorageBus_T enmBus,
9415 bool fUseHostIOCache,
9416 bool fBuiltinIOCache,
9417 bool fSetupMerge,
9418 unsigned uMergeSource,
9419 unsigned uMergeTarget,
9420 IMediumAttachment *aMediumAtt,
9421 MachineState_T aMachineState,
9422 HRESULT *phrc)
9423{
9424 LogFlowFunc(("pUVM=%p aMediumAtt=%p phrc=%p\n", pUVM, aMediumAtt, phrc));
9425
9426 int rc;
9427 HRESULT hrc;
9428 Bstr bstr;
9429 *phrc = S_OK;
9430#define RC_CHECK() do { if (RT_FAILURE(rc)) { AssertMsgFailed(("rc=%Rrc\n", rc)); return rc; } } while (0)
9431#define H() do { if (FAILED(hrc)) { AssertMsgFailed(("hrc=%Rhrc (%#x)\n", hrc, hrc)); *phrc = hrc; return VERR_GENERAL_FAILURE; } } while (0)
9432
9433 /* Ignore attachments other than hard disks, since at the moment they are
9434 * not subject to snapshotting in general. */
9435 DeviceType_T lType;
9436 hrc = aMediumAtt->COMGETTER(Type)(&lType); H();
9437 if (lType != DeviceType_HardDisk)
9438 return VINF_SUCCESS;
9439
9440 /* Determine the base path for the device instance. */
9441 PCFGMNODE pCtlInst;
9442 pCtlInst = CFGMR3GetChildF(CFGMR3GetRootU(pUVM), "Devices/%s/%u/", pcszDevice, uInstance);
9443 AssertReturn(pCtlInst, VERR_INTERNAL_ERROR);
9444
9445 /* Update the device instance configuration. */
9446 rc = pConsole->configMediumAttachment(pCtlInst,
9447 pcszDevice,
9448 uInstance,
9449 enmBus,
9450 fUseHostIOCache,
9451 fBuiltinIOCache,
9452 fSetupMerge,
9453 uMergeSource,
9454 uMergeTarget,
9455 aMediumAtt,
9456 aMachineState,
9457 phrc,
9458 true /* fAttachDetach */,
9459 false /* fForceUnmount */,
9460 false /* fHotplug */,
9461 pUVM,
9462 NULL /* paLedDevType */);
9463 /** @todo this dumps everything attached to this device instance, which
9464 * is more than necessary. Dumping the changed LUN would be enough. */
9465 CFGMR3Dump(pCtlInst);
9466 RC_CHECK();
9467
9468#undef RC_CHECK
9469#undef H
9470
9471 LogFlowFunc(("Returns success\n"));
9472 return VINF_SUCCESS;
9473}
9474
9475/**
9476 * Progress cancelation callback employed by Console::fntTakeSnapshotWorker.
9477 */
9478static void takesnapshotProgressCancelCallback(void *pvUser)
9479{
9480 PUVM pUVM = (PUVM)pvUser;
9481 SSMR3Cancel(pUVM);
9482}
9483
9484/**
9485 * Worker thread created by Console::TakeSnapshot.
9486 * @param Thread The current thread (ignored).
9487 * @param pvUser The task.
9488 * @return VINF_SUCCESS (ignored).
9489 */
9490/*static*/
9491DECLCALLBACK(int) Console::fntTakeSnapshotWorker(RTTHREAD Thread, void *pvUser)
9492{
9493 VMTakeSnapshotTask *pTask = (VMTakeSnapshotTask*)pvUser;
9494
9495 // taking a snapshot consists of the following:
9496
9497 // 1) creating a diff image for each virtual hard disk, into which write operations go after
9498 // the snapshot has been created (done in VBoxSVC, in SessionMachine::BeginTakingSnapshot)
9499 // 2) creating a Snapshot object with the state of the machine (hardware + storage,
9500 // done in VBoxSVC, also in SessionMachine::BeginTakingSnapshot)
9501 // 3) saving the state of the virtual machine (here, in the VM process, if the machine is online)
9502
9503 Console *that = pTask->mConsole;
9504 bool fBeganTakingSnapshot = false;
9505 bool fSuspenededBySave = false;
9506
9507 AutoCaller autoCaller(that);
9508 if (FAILED(autoCaller.rc()))
9509 {
9510 that->mptrCancelableProgress.setNull();
9511 return autoCaller.rc();
9512 }
9513
9514 AutoWriteLock alock(that COMMA_LOCKVAL_SRC_POS);
9515
9516 HRESULT rc = S_OK;
9517
9518 try
9519 {
9520 /* STEP 1 + 2:
9521 * request creating the diff images on the server and create the snapshot object
9522 * (this will set the machine state to Saving on the server to block
9523 * others from accessing this machine)
9524 */
9525 rc = that->mControl->BeginTakingSnapshot(that,
9526 pTask->bstrName.raw(),
9527 pTask->bstrDescription.raw(),
9528 pTask->mProgress,
9529 pTask->fTakingSnapshotOnline,
9530 pTask->bstrSavedStateFile.asOutParam());
9531 if (FAILED(rc))
9532 throw rc;
9533
9534 fBeganTakingSnapshot = true;
9535
9536 /* Check sanity: for offline snapshots there must not be a saved state
9537 * file name. All other combinations are valid (even though online
9538 * snapshots without saved state file seems inconsistent - there are
9539 * some exotic use cases, which need to be explicitly enabled, see the
9540 * code of SessionMachine::BeginTakingSnapshot. */
9541 if ( !pTask->fTakingSnapshotOnline
9542 && !pTask->bstrSavedStateFile.isEmpty())
9543 throw setErrorStatic(E_FAIL, "Invalid state of saved state file");
9544
9545 /* sync the state with the server */
9546 if (pTask->lastMachineState == MachineState_Running)
9547 that->setMachineStateLocally(MachineState_LiveSnapshotting);
9548 else
9549 that->setMachineStateLocally(MachineState_Saving);
9550
9551 // STEP 3: save the VM state (if online)
9552 if (pTask->fTakingSnapshotOnline)
9553 {
9554 int vrc;
9555 SafeVMPtr ptrVM(that);
9556 if (!ptrVM.isOk())
9557 throw ptrVM.rc();
9558
9559 pTask->mProgress->SetNextOperation(Bstr(tr("Saving the machine state")).raw(),
9560 pTask->ulMemSize); // operation weight, same as computed when setting up progress object
9561 if (!pTask->bstrSavedStateFile.isEmpty())
9562 {
9563 Utf8Str strSavedStateFile(pTask->bstrSavedStateFile);
9564
9565 pTask->mProgress->setCancelCallback(takesnapshotProgressCancelCallback, ptrVM.rawUVM());
9566
9567 alock.release();
9568 LogFlowFunc(("VMR3Save...\n"));
9569 vrc = VMR3Save(ptrVM.rawUVM(),
9570 strSavedStateFile.c_str(),
9571 true /*fContinueAfterwards*/,
9572 Console::stateProgressCallback,
9573 static_cast<IProgress *>(pTask->mProgress),
9574 &fSuspenededBySave);
9575 alock.acquire();
9576 if (RT_FAILURE(vrc))
9577 throw setErrorStatic(E_FAIL,
9578 tr("Failed to save the machine state to '%s' (%Rrc)"),
9579 strSavedStateFile.c_str(), vrc);
9580
9581 pTask->mProgress->setCancelCallback(NULL, NULL);
9582 }
9583 else
9584 LogRel(("Console: skipped saving state as part of online snapshot\n"));
9585
9586 if (!pTask->mProgress->notifyPointOfNoReturn())
9587 throw setErrorStatic(E_FAIL, tr("Canceled"));
9588 that->mptrCancelableProgress.setNull();
9589
9590 // STEP 4: reattach hard disks
9591 LogFlowFunc(("Reattaching new differencing hard disks...\n"));
9592
9593 pTask->mProgress->SetNextOperation(Bstr(tr("Reconfiguring medium attachments")).raw(),
9594 1); // operation weight, same as computed when setting up progress object
9595
9596 com::SafeIfaceArray<IMediumAttachment> atts;
9597 rc = that->mMachine->COMGETTER(MediumAttachments)(ComSafeArrayAsOutParam(atts));
9598 if (FAILED(rc))
9599 throw rc;
9600
9601 for (size_t i = 0;
9602 i < atts.size();
9603 ++i)
9604 {
9605 ComPtr<IStorageController> pStorageController;
9606 Bstr controllerName;
9607 ULONG lInstance;
9608 StorageControllerType_T enmController;
9609 StorageBus_T enmBus;
9610 BOOL fUseHostIOCache;
9611
9612 /*
9613 * We can't pass a storage controller object directly
9614 * (g++ complains about not being able to pass non POD types through '...')
9615 * so we have to query needed values here and pass them.
9616 */
9617 rc = atts[i]->COMGETTER(Controller)(controllerName.asOutParam());
9618 if (FAILED(rc))
9619 throw rc;
9620
9621 rc = that->mMachine->GetStorageControllerByName(controllerName.raw(),
9622 pStorageController.asOutParam());
9623 if (FAILED(rc))
9624 throw rc;
9625
9626 rc = pStorageController->COMGETTER(ControllerType)(&enmController);
9627 if (FAILED(rc))
9628 throw rc;
9629 rc = pStorageController->COMGETTER(Instance)(&lInstance);
9630 if (FAILED(rc))
9631 throw rc;
9632 rc = pStorageController->COMGETTER(Bus)(&enmBus);
9633 if (FAILED(rc))
9634 throw rc;
9635 rc = pStorageController->COMGETTER(UseHostIOCache)(&fUseHostIOCache);
9636 if (FAILED(rc))
9637 throw rc;
9638
9639 const char *pcszDevice = Console::convertControllerTypeToDev(enmController);
9640
9641 BOOL fBuiltinIOCache;
9642 rc = that->mMachine->COMGETTER(IOCacheEnabled)(&fBuiltinIOCache);
9643 if (FAILED(rc))
9644 throw rc;
9645
9646 /*
9647 * don't release the lock since reconfigureMediumAttachment
9648 * isn't going to need the Console lock.
9649 */
9650 vrc = VMR3ReqCallWaitU(ptrVM.rawUVM(),
9651 VMCPUID_ANY,
9652 (PFNRT)reconfigureMediumAttachment,
9653 13,
9654 that,
9655 ptrVM.rawUVM(),
9656 pcszDevice,
9657 lInstance,
9658 enmBus,
9659 fUseHostIOCache,
9660 fBuiltinIOCache,
9661 false /* fSetupMerge */,
9662 0 /* uMergeSource */,
9663 0 /* uMergeTarget */,
9664 atts[i],
9665 that->mMachineState,
9666 &rc);
9667 if (RT_FAILURE(vrc))
9668 throw setErrorStatic(E_FAIL, Console::tr("%Rrc"), vrc);
9669 if (FAILED(rc))
9670 throw rc;
9671 }
9672 }
9673
9674 /*
9675 * finalize the requested snapshot object.
9676 * This will reset the machine state to the state it had right
9677 * before calling mControl->BeginTakingSnapshot().
9678 */
9679 rc = that->mControl->EndTakingSnapshot(TRUE /*aSuccess*/);
9680 // do not throw rc here because we can't call EndTakingSnapshot() twice
9681 LogFlowFunc(("EndTakingSnapshot -> %Rhrc [mMachineState=%s]\n", rc, Global::stringifyMachineState(that->mMachineState)));
9682 }
9683 catch (HRESULT rcThrown)
9684 {
9685 /* preserve existing error info */
9686 ErrorInfoKeeper eik;
9687
9688 if (fBeganTakingSnapshot)
9689 that->mControl->EndTakingSnapshot(FALSE /*aSuccess*/);
9690
9691 rc = rcThrown;
9692 LogFunc(("Caught %Rhrc [mMachineState=%s]\n", rc, Global::stringifyMachineState(that->mMachineState)));
9693 }
9694 Assert(alock.isWriteLockOnCurrentThread());
9695
9696 if (FAILED(rc)) /* Must come before calling setMachineState. */
9697 pTask->mProgress->notifyComplete(rc);
9698
9699 /*
9700 * Fix up the machine state.
9701 *
9702 * For live snapshots we do all the work, for the two other variations we
9703 * just update the local copy.
9704 */
9705 MachineState_T enmMachineState;
9706 that->mMachine->COMGETTER(State)(&enmMachineState);
9707 if ( that->mMachineState == MachineState_LiveSnapshotting
9708 || that->mMachineState == MachineState_Saving)
9709 {
9710
9711 if (!pTask->fTakingSnapshotOnline)
9712 that->setMachineStateLocally(pTask->lastMachineState);
9713 else if (SUCCEEDED(rc))
9714 {
9715 Assert( pTask->lastMachineState == MachineState_Running
9716 || pTask->lastMachineState == MachineState_Paused);
9717 Assert(that->mMachineState == MachineState_Saving);
9718 if (pTask->lastMachineState == MachineState_Running)
9719 {
9720 LogFlowFunc(("VMR3Resume...\n"));
9721 SafeVMPtr ptrVM(that);
9722 alock.release();
9723 int vrc = VMR3Resume(ptrVM.rawUVM(), VMRESUMEREASON_STATE_SAVED);
9724 alock.acquire();
9725 if (RT_FAILURE(vrc))
9726 {
9727 rc = setErrorStatic(VBOX_E_VM_ERROR, tr("Could not resume the machine execution (%Rrc)"), vrc);
9728 pTask->mProgress->notifyComplete(rc);
9729 if (that->mMachineState == MachineState_Saving)
9730 that->setMachineStateLocally(MachineState_Paused);
9731 }
9732 }
9733 else
9734 that->setMachineStateLocally(MachineState_Paused);
9735 }
9736 else
9737 {
9738 /** @todo this could probably be made more generic and reused elsewhere. */
9739 /* paranoid cleanup on for a failed online snapshot. */
9740 VMSTATE enmVMState = VMR3GetStateU(that->mpUVM);
9741 switch (enmVMState)
9742 {
9743 case VMSTATE_RUNNING:
9744 case VMSTATE_RUNNING_LS:
9745 case VMSTATE_DEBUGGING:
9746 case VMSTATE_DEBUGGING_LS:
9747 case VMSTATE_POWERING_OFF:
9748 case VMSTATE_POWERING_OFF_LS:
9749 case VMSTATE_RESETTING:
9750 case VMSTATE_RESETTING_LS:
9751 Assert(!fSuspenededBySave);
9752 that->setMachineState(MachineState_Running);
9753 break;
9754
9755 case VMSTATE_GURU_MEDITATION:
9756 case VMSTATE_GURU_MEDITATION_LS:
9757 that->setMachineState(MachineState_Stuck);
9758 break;
9759
9760 case VMSTATE_FATAL_ERROR:
9761 case VMSTATE_FATAL_ERROR_LS:
9762 if (pTask->lastMachineState == MachineState_Paused)
9763 that->setMachineStateLocally(pTask->lastMachineState);
9764 else
9765 that->setMachineState(MachineState_Paused);
9766 break;
9767
9768 default:
9769 AssertMsgFailed(("%s\n", VMR3GetStateName(enmVMState)));
9770 case VMSTATE_SUSPENDED:
9771 case VMSTATE_SUSPENDED_LS:
9772 case VMSTATE_SUSPENDING:
9773 case VMSTATE_SUSPENDING_LS:
9774 case VMSTATE_SUSPENDING_EXT_LS:
9775 if (fSuspenededBySave)
9776 {
9777 Assert(pTask->lastMachineState == MachineState_Running);
9778 LogFlowFunc(("VMR3Resume (on failure)...\n"));
9779 SafeVMPtr ptrVM(that);
9780 alock.release();
9781 int vrc = VMR3Resume(ptrVM.rawUVM(), VMRESUMEREASON_STATE_SAVED); AssertLogRelRC(vrc);
9782 alock.acquire();
9783 if (RT_FAILURE(vrc))
9784 that->setMachineState(MachineState_Paused);
9785 }
9786 else if (pTask->lastMachineState == MachineState_Paused)
9787 that->setMachineStateLocally(pTask->lastMachineState);
9788 else
9789 that->setMachineState(MachineState_Paused);
9790 break;
9791 }
9792
9793 }
9794 }
9795 /*else: somebody else has change the state... Leave it. */
9796
9797 /* check the remote state to see that we got it right. */
9798 that->mMachine->COMGETTER(State)(&enmMachineState);
9799 AssertLogRelMsg(that->mMachineState == enmMachineState,
9800 ("mMachineState=%s enmMachineState=%s\n", Global::stringifyMachineState(that->mMachineState),
9801 Global::stringifyMachineState(enmMachineState) ));
9802
9803
9804 if (SUCCEEDED(rc)) /* The failure cases are handled above. */
9805 pTask->mProgress->notifyComplete(rc);
9806
9807 delete pTask;
9808
9809 LogFlowFuncLeave();
9810 return VINF_SUCCESS;
9811}
9812
9813/**
9814 * Thread for executing the saved state operation.
9815 *
9816 * @param Thread The thread handle.
9817 * @param pvUser Pointer to a VMSaveTask structure.
9818 * @return VINF_SUCCESS (ignored).
9819 *
9820 * @note Locks the Console object for writing.
9821 */
9822/*static*/
9823DECLCALLBACK(int) Console::saveStateThread(RTTHREAD Thread, void *pvUser)
9824{
9825 LogFlowFuncEnter();
9826
9827 std::auto_ptr<VMSaveTask> task(static_cast<VMSaveTask*>(pvUser));
9828 AssertReturn(task.get(), VERR_INVALID_PARAMETER);
9829
9830 Assert(task->mSavedStateFile.length());
9831 Assert(task->mProgress.isNull());
9832 Assert(!task->mServerProgress.isNull());
9833
9834 const ComObjPtr<Console> &that = task->mConsole;
9835 Utf8Str errMsg;
9836 HRESULT rc = S_OK;
9837
9838 LogFlowFunc(("Saving the state to '%s'...\n", task->mSavedStateFile.c_str()));
9839
9840 bool fSuspenededBySave;
9841 int vrc = VMR3Save(task->mpUVM,
9842 task->mSavedStateFile.c_str(),
9843 false, /*fContinueAfterwards*/
9844 Console::stateProgressCallback,
9845 static_cast<IProgress *>(task->mServerProgress),
9846 &fSuspenededBySave);
9847 if (RT_FAILURE(vrc))
9848 {
9849 errMsg = Utf8StrFmt(Console::tr("Failed to save the machine state to '%s' (%Rrc)"),
9850 task->mSavedStateFile.c_str(), vrc);
9851 rc = E_FAIL;
9852 }
9853 Assert(!fSuspenededBySave);
9854
9855 /* lock the console once we're going to access it */
9856 AutoWriteLock thatLock(that COMMA_LOCKVAL_SRC_POS);
9857
9858 /* synchronize the state with the server */
9859 if (SUCCEEDED(rc))
9860 {
9861 /*
9862 * The machine has been successfully saved, so power it down
9863 * (vmstateChangeCallback() will set state to Saved on success).
9864 * Note: we release the task's VM caller, otherwise it will
9865 * deadlock.
9866 */
9867 task->releaseVMCaller();
9868 thatLock.release();
9869 rc = that->powerDown();
9870 thatLock.acquire();
9871 }
9872
9873 /*
9874 * If we failed, reset the local machine state.
9875 */
9876 if (FAILED(rc))
9877 that->setMachineStateLocally(task->mMachineStateBefore);
9878
9879 /*
9880 * Finalize the requested save state procedure. In case of failure it will
9881 * reset the machine state to the state it had right before calling
9882 * mControl->BeginSavingState(). This must be the last thing because it
9883 * will set the progress to completed, and that means that the frontend
9884 * can immediately uninit the associated console object.
9885 */
9886 that->mControl->EndSavingState(rc, Bstr(errMsg).raw());
9887
9888 LogFlowFuncLeave();
9889 return VINF_SUCCESS;
9890}
9891
9892/**
9893 * Thread for powering down the Console.
9894 *
9895 * @param Thread The thread handle.
9896 * @param pvUser Pointer to the VMTask structure.
9897 * @return VINF_SUCCESS (ignored).
9898 *
9899 * @note Locks the Console object for writing.
9900 */
9901/*static*/
9902DECLCALLBACK(int) Console::powerDownThread(RTTHREAD Thread, void *pvUser)
9903{
9904 LogFlowFuncEnter();
9905
9906 std::auto_ptr<VMPowerDownTask> task(static_cast<VMPowerDownTask *>(pvUser));
9907 AssertReturn(task.get(), VERR_INVALID_PARAMETER);
9908
9909 AssertReturn(task->isOk(), VERR_GENERAL_FAILURE);
9910
9911 Assert(task->mProgress.isNull());
9912
9913 const ComObjPtr<Console> &that = task->mConsole;
9914
9915 /* Note: no need to use addCaller() to protect Console because VMTask does
9916 * that */
9917
9918 /* wait until the method tat started us returns */
9919 AutoWriteLock thatLock(that COMMA_LOCKVAL_SRC_POS);
9920
9921 /* release VM caller to avoid the powerDown() deadlock */
9922 task->releaseVMCaller();
9923
9924 thatLock.release();
9925
9926 that->powerDown(task->mServerProgress);
9927
9928 /* complete the operation */
9929 that->mControl->EndPoweringDown(S_OK, Bstr().raw());
9930
9931 LogFlowFuncLeave();
9932 return VINF_SUCCESS;
9933}
9934
9935
9936/**
9937 * @interface_method_impl{VMM2USERMETHODS,pfnSaveState}
9938 */
9939/*static*/ DECLCALLBACK(int)
9940Console::vmm2User_SaveState(PCVMM2USERMETHODS pThis, PUVM pUVM)
9941{
9942 Console *pConsole = ((MYVMM2USERMETHODS *)pThis)->pConsole;
9943 NOREF(pUVM);
9944
9945 /*
9946 * For now, just call SaveState. We should probably try notify the GUI so
9947 * it can pop up a progress object and stuff.
9948 */
9949 HRESULT hrc = pConsole->SaveState(NULL);
9950 return SUCCEEDED(hrc) ? VINF_SUCCESS : Global::vboxStatusCodeFromCOM(hrc);
9951}
9952
9953/**
9954 * @interface_method_impl{VMM2USERMETHODS,pfnNotifyEmtInit}
9955 */
9956/*static*/ DECLCALLBACK(void)
9957Console::vmm2User_NotifyEmtInit(PCVMM2USERMETHODS pThis, PUVM pUVM, PUVMCPU pUVCpu)
9958{
9959 NOREF(pThis); NOREF(pUVM); NOREF(pUVCpu);
9960 VirtualBoxBase::initializeComForThread();
9961}
9962
9963/**
9964 * @interface_method_impl{VMM2USERMETHODS,pfnNotifyEmtTerm}
9965 */
9966/*static*/ DECLCALLBACK(void)
9967Console::vmm2User_NotifyEmtTerm(PCVMM2USERMETHODS pThis, PUVM pUVM, PUVMCPU pUVCpu)
9968{
9969 NOREF(pThis); NOREF(pUVM); NOREF(pUVCpu);
9970 VirtualBoxBase::uninitializeComForThread();
9971}
9972
9973/**
9974 * @interface_method_impl{VMM2USERMETHODS,pfnNotifyPdmtInit}
9975 */
9976/*static*/ DECLCALLBACK(void)
9977Console::vmm2User_NotifyPdmtInit(PCVMM2USERMETHODS pThis, PUVM pUVM)
9978{
9979 NOREF(pThis); NOREF(pUVM);
9980 VirtualBoxBase::initializeComForThread();
9981}
9982
9983/**
9984 * @interface_method_impl{VMM2USERMETHODS,pfnNotifyPdmtTerm}
9985 */
9986/*static*/ DECLCALLBACK(void)
9987Console::vmm2User_NotifyPdmtTerm(PCVMM2USERMETHODS pThis, PUVM pUVM)
9988{
9989 NOREF(pThis); NOREF(pUVM);
9990 VirtualBoxBase::uninitializeComForThread();
9991}
9992
9993
9994
9995
9996/**
9997 * The Main status driver instance data.
9998 */
9999typedef struct DRVMAINSTATUS
10000{
10001 /** The LED connectors. */
10002 PDMILEDCONNECTORS ILedConnectors;
10003 /** Pointer to the LED ports interface above us. */
10004 PPDMILEDPORTS pLedPorts;
10005 /** Pointer to the array of LED pointers. */
10006 PPDMLED *papLeds;
10007 /** The unit number corresponding to the first entry in the LED array. */
10008 RTUINT iFirstLUN;
10009 /** The unit number corresponding to the last entry in the LED array.
10010 * (The size of the LED array is iLastLUN - iFirstLUN + 1.) */
10011 RTUINT iLastLUN;
10012 /** Pointer to the driver instance. */
10013 PPDMDRVINS pDrvIns;
10014 /** The Media Notify interface. */
10015 PDMIMEDIANOTIFY IMediaNotify;
10016 /** Map for translating PDM storage controller/LUN information to
10017 * IMediumAttachment references. */
10018 Console::MediumAttachmentMap *pmapMediumAttachments;
10019 /** Device name+instance for mapping */
10020 char *pszDeviceInstance;
10021 /** Pointer to the Console object, for driver triggered activities. */
10022 Console *pConsole;
10023} DRVMAINSTATUS, *PDRVMAINSTATUS;
10024
10025
10026/**
10027 * Notification about a unit which have been changed.
10028 *
10029 * The driver must discard any pointers to data owned by
10030 * the unit and requery it.
10031 *
10032 * @param pInterface Pointer to the interface structure containing the called function pointer.
10033 * @param iLUN The unit number.
10034 */
10035DECLCALLBACK(void) Console::drvStatus_UnitChanged(PPDMILEDCONNECTORS pInterface, unsigned iLUN)
10036{
10037 PDRVMAINSTATUS pThis = (PDRVMAINSTATUS)((uintptr_t)pInterface - RT_OFFSETOF(DRVMAINSTATUS, ILedConnectors));
10038 if (iLUN >= pThis->iFirstLUN && iLUN <= pThis->iLastLUN)
10039 {
10040 PPDMLED pLed;
10041 int rc = pThis->pLedPorts->pfnQueryStatusLed(pThis->pLedPorts, iLUN, &pLed);
10042 if (RT_FAILURE(rc))
10043 pLed = NULL;
10044 ASMAtomicWritePtr(&pThis->papLeds[iLUN - pThis->iFirstLUN], pLed);
10045 Log(("drvStatus_UnitChanged: iLUN=%d pLed=%p\n", iLUN, pLed));
10046 }
10047}
10048
10049
10050/**
10051 * Notification about a medium eject.
10052 *
10053 * @returns VBox status.
10054 * @param pInterface Pointer to the interface structure containing the called function pointer.
10055 * @param uLUN The unit number.
10056 */
10057DECLCALLBACK(int) Console::drvStatus_MediumEjected(PPDMIMEDIANOTIFY pInterface, unsigned uLUN)
10058{
10059 PDRVMAINSTATUS pThis = (PDRVMAINSTATUS)((uintptr_t)pInterface - RT_OFFSETOF(DRVMAINSTATUS, IMediaNotify));
10060 PPDMDRVINS pDrvIns = pThis->pDrvIns;
10061 LogFunc(("uLUN=%d\n", uLUN));
10062 if (pThis->pmapMediumAttachments)
10063 {
10064 AutoWriteLock alock(pThis->pConsole COMMA_LOCKVAL_SRC_POS);
10065
10066 ComPtr<IMediumAttachment> pMediumAtt;
10067 Utf8Str devicePath = Utf8StrFmt("%s/LUN#%u", pThis->pszDeviceInstance, uLUN);
10068 Console::MediumAttachmentMap::const_iterator end = pThis->pmapMediumAttachments->end();
10069 Console::MediumAttachmentMap::const_iterator it = pThis->pmapMediumAttachments->find(devicePath);
10070 if (it != end)
10071 pMediumAtt = it->second;
10072 Assert(!pMediumAtt.isNull());
10073 if (!pMediumAtt.isNull())
10074 {
10075 IMedium *pMedium = NULL;
10076 HRESULT rc = pMediumAtt->COMGETTER(Medium)(&pMedium);
10077 AssertComRC(rc);
10078 if (SUCCEEDED(rc) && pMedium)
10079 {
10080 BOOL fHostDrive = FALSE;
10081 rc = pMedium->COMGETTER(HostDrive)(&fHostDrive);
10082 AssertComRC(rc);
10083 if (!fHostDrive)
10084 {
10085 alock.release();
10086
10087 ComPtr<IMediumAttachment> pNewMediumAtt;
10088 rc = pThis->pConsole->mControl->EjectMedium(pMediumAtt, pNewMediumAtt.asOutParam());
10089 if (SUCCEEDED(rc))
10090 fireMediumChangedEvent(pThis->pConsole->mEventSource, pNewMediumAtt);
10091
10092 alock.acquire();
10093 if (pNewMediumAtt != pMediumAtt)
10094 {
10095 pThis->pmapMediumAttachments->erase(devicePath);
10096 pThis->pmapMediumAttachments->insert(std::make_pair(devicePath, pNewMediumAtt));
10097 }
10098 }
10099 }
10100 }
10101 }
10102 return VINF_SUCCESS;
10103}
10104
10105
10106/**
10107 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
10108 */
10109DECLCALLBACK(void *) Console::drvStatus_QueryInterface(PPDMIBASE pInterface, const char *pszIID)
10110{
10111 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
10112 PDRVMAINSTATUS pThis = PDMINS_2_DATA(pDrvIns, PDRVMAINSTATUS);
10113 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
10114 PDMIBASE_RETURN_INTERFACE(pszIID, PDMILEDCONNECTORS, &pThis->ILedConnectors);
10115 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIMEDIANOTIFY, &pThis->IMediaNotify);
10116 return NULL;
10117}
10118
10119
10120/**
10121 * Destruct a status driver instance.
10122 *
10123 * @returns VBox status.
10124 * @param pDrvIns The driver instance data.
10125 */
10126DECLCALLBACK(void) Console::drvStatus_Destruct(PPDMDRVINS pDrvIns)
10127{
10128 PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
10129 PDRVMAINSTATUS pThis = PDMINS_2_DATA(pDrvIns, PDRVMAINSTATUS);
10130 LogFlowFunc(("iInstance=%d\n", pDrvIns->iInstance));
10131
10132 if (pThis->papLeds)
10133 {
10134 unsigned iLed = pThis->iLastLUN - pThis->iFirstLUN + 1;
10135 while (iLed-- > 0)
10136 ASMAtomicWriteNullPtr(&pThis->papLeds[iLed]);
10137 }
10138}
10139
10140
10141/**
10142 * Construct a status driver instance.
10143 *
10144 * @copydoc FNPDMDRVCONSTRUCT
10145 */
10146DECLCALLBACK(int) Console::drvStatus_Construct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
10147{
10148 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
10149 PDRVMAINSTATUS pThis = PDMINS_2_DATA(pDrvIns, PDRVMAINSTATUS);
10150 LogFlowFunc(("iInstance=%d\n", pDrvIns->iInstance));
10151
10152 /*
10153 * Validate configuration.
10154 */
10155 if (!CFGMR3AreValuesValid(pCfg, "papLeds\0pmapMediumAttachments\0DeviceInstance\0pConsole\0First\0Last\0"))
10156 return VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES;
10157 AssertMsgReturn(PDMDrvHlpNoAttach(pDrvIns) == VERR_PDM_NO_ATTACHED_DRIVER,
10158 ("Configuration error: Not possible to attach anything to this driver!\n"),
10159 VERR_PDM_DRVINS_NO_ATTACH);
10160
10161 /*
10162 * Data.
10163 */
10164 pDrvIns->IBase.pfnQueryInterface = Console::drvStatus_QueryInterface;
10165 pThis->ILedConnectors.pfnUnitChanged = Console::drvStatus_UnitChanged;
10166 pThis->IMediaNotify.pfnEjected = Console::drvStatus_MediumEjected;
10167 pThis->pDrvIns = pDrvIns;
10168 pThis->pszDeviceInstance = NULL;
10169
10170 /*
10171 * Read config.
10172 */
10173 int rc = CFGMR3QueryPtr(pCfg, "papLeds", (void **)&pThis->papLeds);
10174 if (RT_FAILURE(rc))
10175 {
10176 AssertMsgFailed(("Configuration error: Failed to query the \"papLeds\" value! rc=%Rrc\n", rc));
10177 return rc;
10178 }
10179
10180 rc = CFGMR3QueryPtrDef(pCfg, "pmapMediumAttachments", (void **)&pThis->pmapMediumAttachments, NULL);
10181 if (RT_FAILURE(rc))
10182 {
10183 AssertMsgFailed(("Configuration error: Failed to query the \"pmapMediumAttachments\" value! rc=%Rrc\n", rc));
10184 return rc;
10185 }
10186 if (pThis->pmapMediumAttachments)
10187 {
10188 rc = CFGMR3QueryStringAlloc(pCfg, "DeviceInstance", &pThis->pszDeviceInstance);
10189 if (RT_FAILURE(rc))
10190 {
10191 AssertMsgFailed(("Configuration error: Failed to query the \"DeviceInstance\" value! rc=%Rrc\n", rc));
10192 return rc;
10193 }
10194 rc = CFGMR3QueryPtr(pCfg, "pConsole", (void **)&pThis->pConsole);
10195 if (RT_FAILURE(rc))
10196 {
10197 AssertMsgFailed(("Configuration error: Failed to query the \"pConsole\" value! rc=%Rrc\n", rc));
10198 return rc;
10199 }
10200 }
10201
10202 rc = CFGMR3QueryU32(pCfg, "First", &pThis->iFirstLUN);
10203 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
10204 pThis->iFirstLUN = 0;
10205 else if (RT_FAILURE(rc))
10206 {
10207 AssertMsgFailed(("Configuration error: Failed to query the \"First\" value! rc=%Rrc\n", rc));
10208 return rc;
10209 }
10210
10211 rc = CFGMR3QueryU32(pCfg, "Last", &pThis->iLastLUN);
10212 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
10213 pThis->iLastLUN = 0;
10214 else if (RT_FAILURE(rc))
10215 {
10216 AssertMsgFailed(("Configuration error: Failed to query the \"Last\" value! rc=%Rrc\n", rc));
10217 return rc;
10218 }
10219 if (pThis->iFirstLUN > pThis->iLastLUN)
10220 {
10221 AssertMsgFailed(("Configuration error: Invalid unit range %u-%u\n", pThis->iFirstLUN, pThis->iLastLUN));
10222 return VERR_GENERAL_FAILURE;
10223 }
10224
10225 /*
10226 * Get the ILedPorts interface of the above driver/device and
10227 * query the LEDs we want.
10228 */
10229 pThis->pLedPorts = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMILEDPORTS);
10230 AssertMsgReturn(pThis->pLedPorts, ("Configuration error: No led ports interface above!\n"),
10231 VERR_PDM_MISSING_INTERFACE_ABOVE);
10232
10233 for (unsigned i = pThis->iFirstLUN; i <= pThis->iLastLUN; ++i)
10234 Console::drvStatus_UnitChanged(&pThis->ILedConnectors, i);
10235
10236 return VINF_SUCCESS;
10237}
10238
10239
10240/**
10241 * Console status driver (LED) registration record.
10242 */
10243const PDMDRVREG Console::DrvStatusReg =
10244{
10245 /* u32Version */
10246 PDM_DRVREG_VERSION,
10247 /* szName */
10248 "MainStatus",
10249 /* szRCMod */
10250 "",
10251 /* szR0Mod */
10252 "",
10253 /* pszDescription */
10254 "Main status driver (Main as in the API).",
10255 /* fFlags */
10256 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
10257 /* fClass. */
10258 PDM_DRVREG_CLASS_STATUS,
10259 /* cMaxInstances */
10260 ~0U,
10261 /* cbInstance */
10262 sizeof(DRVMAINSTATUS),
10263 /* pfnConstruct */
10264 Console::drvStatus_Construct,
10265 /* pfnDestruct */
10266 Console::drvStatus_Destruct,
10267 /* pfnRelocate */
10268 NULL,
10269 /* pfnIOCtl */
10270 NULL,
10271 /* pfnPowerOn */
10272 NULL,
10273 /* pfnReset */
10274 NULL,
10275 /* pfnSuspend */
10276 NULL,
10277 /* pfnResume */
10278 NULL,
10279 /* pfnAttach */
10280 NULL,
10281 /* pfnDetach */
10282 NULL,
10283 /* pfnPowerOff */
10284 NULL,
10285 /* pfnSoftReset */
10286 NULL,
10287 /* u32EndVersion */
10288 PDM_DRVREG_VERSION
10289};
10290
10291/* vi: set tabstop=4 shiftwidth=4 expandtab: */
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette