VirtualBox

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

Last change on this file since 24255 was 24255, checked in by vboxsync, 15 years ago

ConsoleImpl: don't need controllerDevToBool() anymore

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

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