VirtualBox

source: vbox/trunk/src/VBox/Main/ConsoleImpl.cpp@ 33000

Last change on this file since 33000 was 32961, checked in by vboxsync, 14 years ago

whitespace

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

© 2023 Oracle
ContactPrivacy policyTerms of Use