VirtualBox

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

Last change on this file since 23664 was 23664, checked in by vboxsync, 16 years ago

Main: stringify the machine state in error messages.

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

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