VirtualBox

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

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

Main: Cancellation fixes (almost perfect).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 241.2 KB
Line 
1/* $Id: ConsoleImpl.cpp 23827 2009-10-16 15:50: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 mTeleporterEnabled(FALSE)
231 {}
232
233 PFNVMATERROR mSetVMErrorCallback;
234 PFNCFGMCONSTRUCTOR mConfigConstructor;
235 Utf8Str mSavedStateFile;
236 Console::SharedFolderDataMap mSharedFolders;
237 bool mStartPaused;
238 BOOL mTeleporterEnabled;
239
240 typedef std::list< ComPtr<IMedium> > HardDiskList;
241 HardDiskList hardDisks;
242
243 /* array of progress objects for hard disk reset operations */
244 typedef std::list< ComPtr<IProgress> > ProgressList;
245 ProgressList hardDiskProgresses;
246};
247
248struct VMSaveTask : public VMProgressTask
249{
250 VMSaveTask(Console *aConsole, Progress *aProgress)
251 : VMProgressTask(aConsole, aProgress, true /* aUsesVMPtr */),
252 mLastMachineState(MachineState_Null)
253 {}
254
255 Utf8Str mSavedStateFile;
256 MachineState_T mLastMachineState;
257 ComPtr<IProgress> mServerProgress;
258};
259
260// constructor / destructor
261/////////////////////////////////////////////////////////////////////////////
262
263Console::Console()
264 : mSavedStateDataLoaded(false)
265 , mConsoleVRDPServer(NULL)
266 , mpVM(NULL)
267 , mVMCallers(0)
268 , mVMZeroCallersSem(NIL_RTSEMEVENT)
269 , mVMDestroying(false)
270 , mVMPoweredOff(false)
271 , mVMMDev(NULL)
272 , mAudioSniffer(NULL)
273 , mVMStateChangeCallbackDisabled(false)
274 , mMachineState(MachineState_PoweredOff)
275{
276 for (ULONG slot = 0; slot < SchemaDefs::NetworkAdapterCount; ++slot)
277 meAttachmentType[slot] = NetworkAttachmentType_Null;
278}
279
280Console::~Console()
281{}
282
283HRESULT Console::FinalConstruct()
284{
285 LogFlowThisFunc(("\n"));
286
287 memset(mapFDLeds, 0, sizeof(mapFDLeds));
288 memset(mapIDELeds, 0, sizeof(mapIDELeds));
289 memset(mapSATALeds, 0, sizeof(mapSATALeds));
290 memset(mapSCSILeds, 0, sizeof(mapSCSILeds));
291 memset(mapNetworkLeds, 0, sizeof(mapNetworkLeds));
292 memset(&mapUSBLed, 0, sizeof(mapUSBLed));
293 memset(&mapSharedFolderLed, 0, sizeof(mapSharedFolderLed));
294
295 return S_OK;
296}
297
298void Console::FinalRelease()
299{
300 LogFlowThisFunc(("\n"));
301
302 uninit();
303}
304
305// public initializer/uninitializer for internal purposes only
306/////////////////////////////////////////////////////////////////////////////
307
308HRESULT Console::init(IMachine *aMachine, IInternalMachineControl *aControl)
309{
310 AssertReturn(aMachine && aControl, E_INVALIDARG);
311
312 /* Enclose the state transition NotReady->InInit->Ready */
313 AutoInitSpan autoInitSpan(this);
314 AssertReturn(autoInitSpan.isOk(), E_FAIL);
315
316 LogFlowThisFuncEnter();
317 LogFlowThisFunc(("aMachine=%p, aControl=%p\n", aMachine, aControl));
318
319 HRESULT rc = E_FAIL;
320
321 unconst(mMachine) = aMachine;
322 unconst(mControl) = aControl;
323
324 memset(&mCallbackData, 0, sizeof(mCallbackData));
325
326 /* Cache essential properties and objects */
327
328 rc = mMachine->COMGETTER(State)(&mMachineState);
329 AssertComRCReturnRC(rc);
330
331#ifdef VBOX_WITH_VRDP
332 rc = mMachine->COMGETTER(VRDPServer)(unconst(mVRDPServer).asOutParam());
333 AssertComRCReturnRC(rc);
334#endif
335
336 /* Create associated child COM objects */
337
338 unconst(mGuest).createObject();
339 rc = mGuest->init(this);
340 AssertComRCReturnRC(rc);
341
342 unconst(mKeyboard).createObject();
343 rc = mKeyboard->init(this);
344 AssertComRCReturnRC(rc);
345
346 unconst(mMouse).createObject();
347 rc = mMouse->init(this);
348 AssertComRCReturnRC(rc);
349
350 unconst(mDisplay).createObject();
351 rc = mDisplay->init(this);
352 AssertComRCReturnRC(rc);
353
354 unconst(mRemoteDisplayInfo).createObject();
355 rc = mRemoteDisplayInfo->init(this);
356 AssertComRCReturnRC(rc);
357
358 /* Grab global and machine shared folder lists */
359
360 rc = fetchSharedFolders(true /* aGlobal */);
361 AssertComRCReturnRC(rc);
362 rc = fetchSharedFolders(false /* aGlobal */);
363 AssertComRCReturnRC(rc);
364
365 /* Create other child objects */
366
367 unconst(mConsoleVRDPServer) = new ConsoleVRDPServer(this);
368 AssertReturn(mConsoleVRDPServer, E_FAIL);
369
370 mcAudioRefs = 0;
371 mcVRDPClients = 0;
372 mu32SingleRDPClientId = 0;
373
374 unconst(mVMMDev) = new VMMDev(this);
375 AssertReturn(mVMMDev, E_FAIL);
376
377 unconst(mAudioSniffer) = new AudioSniffer(this);
378 AssertReturn(mAudioSniffer, E_FAIL);
379
380 /* Confirm a successful initialization when it's the case */
381 autoInitSpan.setSucceeded();
382
383 LogFlowThisFuncLeave();
384
385 return S_OK;
386}
387
388/**
389 * Uninitializes the Console object.
390 */
391void Console::uninit()
392{
393 LogFlowThisFuncEnter();
394
395 /* Enclose the state transition Ready->InUninit->NotReady */
396 AutoUninitSpan autoUninitSpan(this);
397 if (autoUninitSpan.uninitDone())
398 {
399 LogFlowThisFunc(("Already uninitialized.\n"));
400 LogFlowThisFuncLeave();
401 return;
402 }
403
404 LogFlowThisFunc(("initFailed()=%d\n", autoUninitSpan.initFailed()));
405
406 /*
407 * Uninit all children that use addDependentChild()/removeDependentChild()
408 * in their init()/uninit() methods.
409 */
410 uninitDependentChildren();
411
412 /* power down the VM if necessary */
413 if (mpVM)
414 {
415 powerDown();
416 Assert(mpVM == NULL);
417 }
418
419 if (mVMZeroCallersSem != NIL_RTSEMEVENT)
420 {
421 RTSemEventDestroy(mVMZeroCallersSem);
422 mVMZeroCallersSem = NIL_RTSEMEVENT;
423 }
424
425 if (mAudioSniffer)
426 {
427 delete mAudioSniffer;
428 unconst(mAudioSniffer) = NULL;
429 }
430
431 if (mVMMDev)
432 {
433 delete mVMMDev;
434 unconst(mVMMDev) = NULL;
435 }
436
437 mGlobalSharedFolders.clear();
438 mMachineSharedFolders.clear();
439
440 mSharedFolders.clear();
441 mRemoteUSBDevices.clear();
442 mUSBDevices.clear();
443
444 if (mRemoteDisplayInfo)
445 {
446 mRemoteDisplayInfo->uninit();
447 unconst(mRemoteDisplayInfo).setNull();;
448 }
449
450 if (mDebugger)
451 {
452 mDebugger->uninit();
453 unconst(mDebugger).setNull();
454 }
455
456 if (mDisplay)
457 {
458 mDisplay->uninit();
459 unconst(mDisplay).setNull();
460 }
461
462 if (mMouse)
463 {
464 mMouse->uninit();
465 unconst(mMouse).setNull();
466 }
467
468 if (mKeyboard)
469 {
470 mKeyboard->uninit();
471 unconst(mKeyboard).setNull();;
472 }
473
474 if (mGuest)
475 {
476 mGuest->uninit();
477 unconst(mGuest).setNull();;
478 }
479
480 if (mConsoleVRDPServer)
481 {
482 delete mConsoleVRDPServer;
483 unconst(mConsoleVRDPServer) = NULL;
484 }
485
486#ifdef VBOX_WITH_VRDP
487 unconst(mVRDPServer).setNull();
488#endif
489
490 unconst(mControl).setNull();
491 unconst(mMachine).setNull();
492
493 /* Release all callbacks. Do this after uninitializing the components,
494 * as some of them are well-behaved and unregister their callbacks.
495 * These would trigger error messages complaining about trying to
496 * unregister a non-registered callback. */
497 mCallbacks.clear();
498
499 /* dynamically allocated members of mCallbackData are uninitialized
500 * at the end of powerDown() */
501 Assert(!mCallbackData.mpsc.valid && mCallbackData.mpsc.shape == NULL);
502 Assert(!mCallbackData.mcc.valid);
503 Assert(!mCallbackData.klc.valid);
504
505 LogFlowThisFuncLeave();
506}
507
508#ifdef VBOX_WITH_GUEST_PROPS
509bool Console::enabledGuestPropertiesVRDP(void)
510{
511 Bstr value;
512 HRESULT hrc = mMachine->GetExtraData(Bstr("VBoxInternal2/EnableGuestPropertiesVRDP"), value.asOutParam());
513 if (hrc == S_OK)
514 {
515 if (value == "1")
516 {
517 return true;
518 }
519 }
520 return false;
521}
522
523void Console::updateGuestPropertiesVRDPLogon(uint32_t u32ClientId, const char *pszUser, const char *pszDomain)
524{
525 if (!enabledGuestPropertiesVRDP())
526 {
527 return;
528 }
529
530 int rc;
531 char *pszPropertyName;
532
533 rc = RTStrAPrintf(&pszPropertyName, "/VirtualBox/HostInfo/VRDP/Client/%u/Name", u32ClientId);
534 if (RT_SUCCESS(rc))
535 {
536 Bstr clientName;
537 mRemoteDisplayInfo->COMGETTER(ClientName)(clientName.asOutParam());
538
539 mMachine->SetGuestProperty(Bstr(pszPropertyName), clientName, Bstr("RDONLYGUEST"));
540 RTStrFree(pszPropertyName);
541 }
542
543 rc = RTStrAPrintf(&pszPropertyName, "/VirtualBox/HostInfo/VRDP/Client/%u/User", u32ClientId);
544 if (RT_SUCCESS(rc))
545 {
546 mMachine->SetGuestProperty(Bstr(pszPropertyName), Bstr(pszUser), Bstr("RDONLYGUEST"));
547 RTStrFree(pszPropertyName);
548 }
549
550 rc = RTStrAPrintf(&pszPropertyName, "/VirtualBox/HostInfo/VRDP/Client/%u/Domain", u32ClientId);
551 if (RT_SUCCESS(rc))
552 {
553 mMachine->SetGuestProperty(Bstr(pszPropertyName), Bstr(pszDomain), Bstr("RDONLYGUEST"));
554 RTStrFree(pszPropertyName);
555 }
556
557 char *pszClientId;
558 rc = RTStrAPrintf(&pszClientId, "%d", u32ClientId);
559 if (RT_SUCCESS(rc))
560 {
561 mMachine->SetGuestProperty(Bstr("/VirtualBox/HostInfo/VRDP/LastConnectedClient"), Bstr(pszClientId), Bstr("RDONLYGUEST"));
562 RTStrFree(pszClientId);
563 }
564
565 return;
566}
567
568void Console::updateGuestPropertiesVRDPDisconnect(uint32_t u32ClientId)
569{
570 if (!enabledGuestPropertiesVRDP())
571 {
572 return;
573 }
574
575 int rc;
576 char *pszPropertyName;
577
578 rc = RTStrAPrintf(&pszPropertyName, "/VirtualBox/HostInfo/VRDP/Client/%u/Name", u32ClientId);
579 if (RT_SUCCESS(rc))
580 {
581 mMachine->SetGuestProperty(Bstr(pszPropertyName), Bstr(""), Bstr("RDONLYGUEST"));
582 RTStrFree(pszPropertyName);
583 }
584
585 rc = RTStrAPrintf(&pszPropertyName, "/VirtualBox/HostInfo/VRDP/Client/%u/User", u32ClientId);
586 if (RT_SUCCESS(rc))
587 {
588 mMachine->SetGuestProperty(Bstr(pszPropertyName), Bstr(""), Bstr("RDONLYGUEST"));
589 RTStrFree(pszPropertyName);
590 }
591
592 rc = RTStrAPrintf(&pszPropertyName, "/VirtualBox/HostInfo/VRDP/Client/%u/Domain", u32ClientId);
593 if (RT_SUCCESS(rc))
594 {
595 mMachine->SetGuestProperty(Bstr(pszPropertyName), Bstr(""), Bstr("RDONLYGUEST"));
596 RTStrFree(pszPropertyName);
597 }
598
599 char *pszClientId;
600 rc = RTStrAPrintf(&pszClientId, "%d", u32ClientId);
601 if (RT_SUCCESS(rc))
602 {
603 mMachine->SetGuestProperty(Bstr("/VirtualBox/HostInfo/VRDP/LastDisconnectedClient"), Bstr(pszClientId), Bstr("RDONLYGUEST"));
604 RTStrFree(pszClientId);
605 }
606
607 return;
608}
609#endif /* VBOX_WITH_GUEST_PROPS */
610
611
612int Console::VRDPClientLogon(uint32_t u32ClientId, const char *pszUser, const char *pszPassword, const char *pszDomain)
613{
614 LogFlowFuncEnter();
615 LogFlowFunc(("%d, %s, %s, %s\n", u32ClientId, pszUser, pszPassword, pszDomain));
616
617 AutoCaller autoCaller(this);
618 if (!autoCaller.isOk())
619 {
620 /* Console has been already uninitialized, deny request */
621 LogRel(("VRDPAUTH: Access denied (Console uninitialized).\n"));
622 LogFlowFuncLeave();
623 return VERR_ACCESS_DENIED;
624 }
625
626 Bstr id;
627 HRESULT hrc = mMachine->COMGETTER(Id)(id.asOutParam());
628 Guid uuid = Guid(id);
629
630 AssertComRCReturn(hrc, VERR_ACCESS_DENIED);
631
632 VRDPAuthType_T authType = VRDPAuthType_Null;
633 hrc = mVRDPServer->COMGETTER(AuthType)(&authType);
634 AssertComRCReturn(hrc, VERR_ACCESS_DENIED);
635
636 ULONG authTimeout = 0;
637 hrc = mVRDPServer->COMGETTER(AuthTimeout)(&authTimeout);
638 AssertComRCReturn(hrc, VERR_ACCESS_DENIED);
639
640 VRDPAuthResult result = VRDPAuthAccessDenied;
641 VRDPAuthGuestJudgement guestJudgement = VRDPAuthGuestNotAsked;
642
643 LogFlowFunc(("Auth type %d\n", authType));
644
645 LogRel(("VRDPAUTH: User: [%s]. Domain: [%s]. Authentication type: [%s]\n",
646 pszUser, pszDomain,
647 authType == VRDPAuthType_Null?
648 "Null":
649 (authType == VRDPAuthType_External?
650 "External":
651 (authType == VRDPAuthType_Guest?
652 "Guest":
653 "INVALID"
654 )
655 )
656 ));
657
658 switch (authType)
659 {
660 case VRDPAuthType_Null:
661 {
662 result = VRDPAuthAccessGranted;
663 break;
664 }
665
666 case VRDPAuthType_External:
667 {
668 /* Call the external library. */
669 result = mConsoleVRDPServer->Authenticate(uuid, guestJudgement, pszUser, pszPassword, pszDomain, u32ClientId);
670
671 if (result != VRDPAuthDelegateToGuest)
672 {
673 break;
674 }
675
676 LogRel(("VRDPAUTH: Delegated to guest.\n"));
677
678 LogFlowFunc(("External auth asked for guest judgement\n"));
679 } /* pass through */
680
681 case VRDPAuthType_Guest:
682 {
683 guestJudgement = VRDPAuthGuestNotReacted;
684
685 if (mVMMDev)
686 {
687 /* Issue the request to guest. Assume that the call does not require EMT. It should not. */
688
689 /* Ask the guest to judge these credentials. */
690 uint32_t u32GuestFlags = VMMDEV_SETCREDENTIALS_JUDGE;
691
692 int rc = mVMMDev->getVMMDevPort()->pfnSetCredentials(mVMMDev->getVMMDevPort(),
693 pszUser, pszPassword, pszDomain, u32GuestFlags);
694
695 if (RT_SUCCESS(rc))
696 {
697 /* Wait for guest. */
698 rc = mVMMDev->WaitCredentialsJudgement(authTimeout, &u32GuestFlags);
699
700 if (RT_SUCCESS(rc))
701 {
702 switch (u32GuestFlags & (VMMDEV_CREDENTIALS_JUDGE_OK | VMMDEV_CREDENTIALS_JUDGE_DENY | VMMDEV_CREDENTIALS_JUDGE_NOJUDGEMENT))
703 {
704 case VMMDEV_CREDENTIALS_JUDGE_DENY: guestJudgement = VRDPAuthGuestAccessDenied; break;
705 case VMMDEV_CREDENTIALS_JUDGE_NOJUDGEMENT: guestJudgement = VRDPAuthGuestNoJudgement; break;
706 case VMMDEV_CREDENTIALS_JUDGE_OK: guestJudgement = VRDPAuthGuestAccessGranted; break;
707 default:
708 LogFlowFunc(("Invalid guest flags %08X!!!\n", u32GuestFlags)); break;
709 }
710 }
711 else
712 {
713 LogFlowFunc(("Wait for credentials judgement rc = %Rrc!!!\n", rc));
714 }
715
716 LogFlowFunc(("Guest judgement %d\n", guestJudgement));
717 }
718 else
719 {
720 LogFlowFunc(("Could not set credentials rc = %Rrc!!!\n", rc));
721 }
722 }
723
724 if (authType == VRDPAuthType_External)
725 {
726 LogRel(("VRDPAUTH: Guest judgement %d.\n", guestJudgement));
727 LogFlowFunc(("External auth called again with guest judgement = %d\n", guestJudgement));
728 result = mConsoleVRDPServer->Authenticate(uuid, guestJudgement, pszUser, pszPassword, pszDomain, u32ClientId);
729 }
730 else
731 {
732 switch (guestJudgement)
733 {
734 case VRDPAuthGuestAccessGranted:
735 result = VRDPAuthAccessGranted;
736 break;
737 default:
738 result = VRDPAuthAccessDenied;
739 break;
740 }
741 }
742 } break;
743
744 default:
745 AssertFailed();
746 }
747
748 LogFlowFunc(("Result = %d\n", result));
749 LogFlowFuncLeave();
750
751 if (result != VRDPAuthAccessGranted)
752 {
753 /* Reject. */
754 LogRel(("VRDPAUTH: Access denied.\n"));
755 return VERR_ACCESS_DENIED;
756 }
757
758 LogRel(("VRDPAUTH: Access granted.\n"));
759
760 /* Multiconnection check must be made after authentication, so bad clients would not interfere with a good one. */
761 BOOL allowMultiConnection = FALSE;
762 hrc = mVRDPServer->COMGETTER(AllowMultiConnection)(&allowMultiConnection);
763 AssertComRCReturn(hrc, VERR_ACCESS_DENIED);
764
765 BOOL reuseSingleConnection = FALSE;
766 hrc = mVRDPServer->COMGETTER(ReuseSingleConnection)(&reuseSingleConnection);
767 AssertComRCReturn(hrc, VERR_ACCESS_DENIED);
768
769 LogFlowFunc(("allowMultiConnection %d, reuseSingleConnection = %d, mcVRDPClients = %d, mu32SingleRDPClientId = %d\n", allowMultiConnection, reuseSingleConnection, mcVRDPClients, mu32SingleRDPClientId));
770
771 if (allowMultiConnection == FALSE)
772 {
773 /* Note: the 'mcVRDPClients' variable is incremented in ClientConnect callback, which is called when the client
774 * is successfully connected, that is after the ClientLogon callback. Therefore the mcVRDPClients
775 * value is 0 for first client.
776 */
777 if (mcVRDPClients != 0)
778 {
779 Assert(mcVRDPClients == 1);
780 /* There is a client already.
781 * If required drop the existing client connection and let the connecting one in.
782 */
783 if (reuseSingleConnection)
784 {
785 LogRel(("VRDPAUTH: Multiple connections are not enabled. Disconnecting existing client.\n"));
786 mConsoleVRDPServer->DisconnectClient(mu32SingleRDPClientId, false);
787 }
788 else
789 {
790 /* Reject. */
791 LogRel(("VRDPAUTH: Multiple connections are not enabled. Access denied.\n"));
792 return VERR_ACCESS_DENIED;
793 }
794 }
795
796 /* Save the connected client id. From now on it will be necessary to disconnect this one. */
797 mu32SingleRDPClientId = u32ClientId;
798 }
799
800#ifdef VBOX_WITH_GUEST_PROPS
801 updateGuestPropertiesVRDPLogon(u32ClientId, pszUser, pszDomain);
802#endif /* VBOX_WITH_GUEST_PROPS */
803
804 return VINF_SUCCESS;
805}
806
807void Console::VRDPClientConnect(uint32_t u32ClientId)
808{
809 LogFlowFuncEnter();
810
811 AutoCaller autoCaller(this);
812 AssertComRCReturnVoid(autoCaller.rc());
813
814#ifdef VBOX_WITH_VRDP
815 uint32_t u32Clients = ASMAtomicIncU32(&mcVRDPClients);
816
817 if (u32Clients == 1)
818 {
819 getVMMDev()->getVMMDevPort()->
820 pfnVRDPChange(getVMMDev()->getVMMDevPort(),
821 true, VRDP_EXPERIENCE_LEVEL_FULL); // @todo configurable
822 }
823
824 NOREF(u32ClientId);
825 mDisplay->VideoAccelVRDP(true);
826#endif /* VBOX_WITH_VRDP */
827
828 LogFlowFuncLeave();
829 return;
830}
831
832void Console::VRDPClientDisconnect(uint32_t u32ClientId,
833 uint32_t fu32Intercepted)
834{
835 LogFlowFuncEnter();
836
837 AutoCaller autoCaller(this);
838 AssertComRCReturnVoid(autoCaller.rc());
839
840 AssertReturnVoid(mConsoleVRDPServer);
841
842#ifdef VBOX_WITH_VRDP
843 uint32_t u32Clients = ASMAtomicDecU32(&mcVRDPClients);
844
845 if (u32Clients == 0)
846 {
847 getVMMDev()->getVMMDevPort()->
848 pfnVRDPChange(getVMMDev()->getVMMDevPort(),
849 false, 0);
850 }
851
852 mDisplay->VideoAccelVRDP(false);
853#endif /* VBOX_WITH_VRDP */
854
855 if (fu32Intercepted & VRDP_CLIENT_INTERCEPT_USB)
856 {
857 mConsoleVRDPServer->USBBackendDelete(u32ClientId);
858 }
859
860#ifdef VBOX_WITH_VRDP
861 if (fu32Intercepted & VRDP_CLIENT_INTERCEPT_CLIPBOARD)
862 {
863 mConsoleVRDPServer->ClipboardDelete(u32ClientId);
864 }
865
866 if (fu32Intercepted & VRDP_CLIENT_INTERCEPT_AUDIO)
867 {
868 mcAudioRefs--;
869
870 if (mcAudioRefs <= 0)
871 {
872 if (mAudioSniffer)
873 {
874 PPDMIAUDIOSNIFFERPORT port = mAudioSniffer->getAudioSnifferPort();
875 if (port)
876 {
877 port->pfnSetup(port, false, false);
878 }
879 }
880 }
881 }
882#endif /* VBOX_WITH_VRDP */
883
884 Bstr uuid;
885 HRESULT hrc = mMachine->COMGETTER(Id)(uuid.asOutParam());
886 AssertComRC(hrc);
887
888 VRDPAuthType_T authType = VRDPAuthType_Null;
889 hrc = mVRDPServer->COMGETTER(AuthType)(&authType);
890 AssertComRC(hrc);
891
892 if (authType == VRDPAuthType_External)
893 mConsoleVRDPServer->AuthDisconnect(uuid, u32ClientId);
894
895#ifdef VBOX_WITH_GUEST_PROPS
896 updateGuestPropertiesVRDPDisconnect(u32ClientId);
897#endif /* VBOX_WITH_GUEST_PROPS */
898
899 LogFlowFuncLeave();
900 return;
901}
902
903void Console::VRDPInterceptAudio(uint32_t u32ClientId)
904{
905 LogFlowFuncEnter();
906
907 AutoCaller autoCaller(this);
908 AssertComRCReturnVoid(autoCaller.rc());
909
910 LogFlowFunc(("mAudioSniffer %p, u32ClientId %d.\n",
911 mAudioSniffer, u32ClientId));
912 NOREF(u32ClientId);
913
914#ifdef VBOX_WITH_VRDP
915 ++mcAudioRefs;
916
917 if (mcAudioRefs == 1)
918 {
919 if (mAudioSniffer)
920 {
921 PPDMIAUDIOSNIFFERPORT port = mAudioSniffer->getAudioSnifferPort();
922 if (port)
923 {
924 port->pfnSetup(port, true, true);
925 }
926 }
927 }
928#endif
929
930 LogFlowFuncLeave();
931 return;
932}
933
934void Console::VRDPInterceptUSB(uint32_t u32ClientId, void **ppvIntercept)
935{
936 LogFlowFuncEnter();
937
938 AutoCaller autoCaller(this);
939 AssertComRCReturnVoid(autoCaller.rc());
940
941 AssertReturnVoid(mConsoleVRDPServer);
942
943 mConsoleVRDPServer->USBBackendCreate(u32ClientId, ppvIntercept);
944
945 LogFlowFuncLeave();
946 return;
947}
948
949void Console::VRDPInterceptClipboard(uint32_t u32ClientId)
950{
951 LogFlowFuncEnter();
952
953 AutoCaller autoCaller(this);
954 AssertComRCReturnVoid(autoCaller.rc());
955
956 AssertReturnVoid(mConsoleVRDPServer);
957
958#ifdef VBOX_WITH_VRDP
959 mConsoleVRDPServer->ClipboardCreate(u32ClientId);
960#endif /* VBOX_WITH_VRDP */
961
962 LogFlowFuncLeave();
963 return;
964}
965
966
967//static
968const char *Console::sSSMConsoleUnit = "ConsoleData";
969//static
970uint32_t Console::sSSMConsoleVer = 0x00010001;
971
972/**
973 * Loads various console data stored in the saved state file.
974 * This method does validation of the state file and returns an error info
975 * when appropriate.
976 *
977 * The method does nothing if the machine is not in the Saved file or if
978 * console data from it has already been loaded.
979 *
980 * @note The caller must lock this object for writing.
981 */
982HRESULT Console::loadDataFromSavedState()
983{
984 if (mMachineState != MachineState_Saved || mSavedStateDataLoaded)
985 return S_OK;
986
987 Bstr savedStateFile;
988 HRESULT rc = mMachine->COMGETTER(StateFilePath)(savedStateFile.asOutParam());
989 if (FAILED(rc))
990 return rc;
991
992 PSSMHANDLE ssm;
993 int vrc = SSMR3Open(Utf8Str(savedStateFile).c_str(), 0, &ssm);
994 if (RT_SUCCESS(vrc))
995 {
996 uint32_t version = 0;
997 vrc = SSMR3Seek(ssm, sSSMConsoleUnit, 0 /* iInstance */, &version);
998 if (SSM_VERSION_MAJOR(version) == SSM_VERSION_MAJOR(sSSMConsoleVer))
999 {
1000 if (RT_SUCCESS(vrc))
1001 vrc = loadStateFileExecInternal(ssm, version);
1002 else if (vrc == VERR_SSM_UNIT_NOT_FOUND)
1003 vrc = VINF_SUCCESS;
1004 }
1005 else
1006 vrc = VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
1007
1008 SSMR3Close(ssm);
1009 }
1010
1011 if (RT_FAILURE(vrc))
1012 rc = setError(VBOX_E_FILE_ERROR,
1013 tr("The saved state file '%ls' is invalid (%Rrc). Discard the saved state and try again"),
1014 savedStateFile.raw(), vrc);
1015
1016 mSavedStateDataLoaded = true;
1017
1018 return rc;
1019}
1020
1021/**
1022 * Callback handler to save various console data to the state file,
1023 * called when the user saves the VM state.
1024 *
1025 * @param pvUser pointer to Console
1026 *
1027 * @note Locks the Console object for reading.
1028 */
1029//static
1030DECLCALLBACK(void)
1031Console::saveStateFileExec(PSSMHANDLE pSSM, void *pvUser)
1032{
1033 LogFlowFunc(("\n"));
1034
1035 Console *that = static_cast<Console *>(pvUser);
1036 AssertReturnVoid(that);
1037
1038 AutoCaller autoCaller(that);
1039 AssertComRCReturnVoid(autoCaller.rc());
1040
1041 AutoReadLock alock(that);
1042
1043 int vrc = SSMR3PutU32(pSSM, (uint32_t)that->mSharedFolders.size());
1044 AssertRC(vrc);
1045
1046 for (SharedFolderMap::const_iterator it = that->mSharedFolders.begin();
1047 it != that->mSharedFolders.end();
1048 ++ it)
1049 {
1050 ComObjPtr<SharedFolder> folder = (*it).second;
1051 // don't lock the folder because methods we access are const
1052
1053 Utf8Str name = folder->name();
1054 vrc = SSMR3PutU32(pSSM, (uint32_t)name.length() + 1 /* term. 0 */);
1055 AssertRC(vrc);
1056 vrc = SSMR3PutStrZ(pSSM, name.c_str());
1057 AssertRC(vrc);
1058
1059 Utf8Str hostPath = folder->hostPath();
1060 vrc = SSMR3PutU32(pSSM, (uint32_t)hostPath.length() + 1 /* term. 0 */);
1061 AssertRC(vrc);
1062 vrc = SSMR3PutStrZ(pSSM, hostPath.c_str());
1063 AssertRC(vrc);
1064
1065 vrc = SSMR3PutBool(pSSM, !!folder->writable());
1066 AssertRC(vrc);
1067 }
1068
1069 return;
1070}
1071
1072/**
1073 * Callback handler to load various console data from the state file.
1074 * Called when the VM is being restored from the saved state.
1075 *
1076 * @param pvUser pointer to Console
1077 * @param uVersion Console unit version.
1078 * Should match sSSMConsoleVer.
1079 * @param uPass The data pass.
1080 *
1081 * @note Should locks the Console object for writing, if necessary.
1082 */
1083//static
1084DECLCALLBACK(int)
1085Console::loadStateFileExec(PSSMHANDLE pSSM, void *pvUser, uint32_t uVersion, uint32_t uPass)
1086{
1087 LogFlowFunc(("\n"));
1088
1089 if (SSM_VERSION_MAJOR_CHANGED(uVersion, sSSMConsoleVer))
1090 return VERR_VERSION_MISMATCH;
1091 Assert(uPass == SSM_PASS_FINAL); NOREF(uPass);
1092
1093 Console *that = static_cast<Console *>(pvUser);
1094 AssertReturn(that, VERR_INVALID_PARAMETER);
1095
1096 /* Currently, nothing to do when we've been called from VMR3Load*. */
1097 return SSMR3SkipToEndOfUnit(pSSM);
1098}
1099
1100/**
1101 * Method to load various console data from the state file.
1102 * Called from #loadDataFromSavedState.
1103 *
1104 * @param pvUser pointer to Console
1105 * @param u32Version Console unit version.
1106 * Should match sSSMConsoleVer.
1107 *
1108 * @note Locks the Console object for writing.
1109 */
1110int
1111Console::loadStateFileExecInternal(PSSMHANDLE pSSM, uint32_t u32Version)
1112{
1113 AutoCaller autoCaller(this);
1114 AssertComRCReturn(autoCaller.rc(), VERR_ACCESS_DENIED);
1115
1116 AutoWriteLock alock(this);
1117
1118 AssertReturn(mSharedFolders.size() == 0, VERR_INTERNAL_ERROR);
1119
1120 uint32_t size = 0;
1121 int vrc = SSMR3GetU32(pSSM, &size);
1122 AssertRCReturn(vrc, vrc);
1123
1124 for (uint32_t i = 0; i < size; ++ i)
1125 {
1126 Bstr name;
1127 Bstr hostPath;
1128 bool writable = true;
1129
1130 uint32_t szBuf = 0;
1131 char *buf = NULL;
1132
1133 vrc = SSMR3GetU32(pSSM, &szBuf);
1134 AssertRCReturn(vrc, vrc);
1135 buf = new char[szBuf];
1136 vrc = SSMR3GetStrZ(pSSM, buf, szBuf);
1137 AssertRC(vrc);
1138 name = buf;
1139 delete[] buf;
1140
1141 vrc = SSMR3GetU32(pSSM, &szBuf);
1142 AssertRCReturn(vrc, vrc);
1143 buf = new char[szBuf];
1144 vrc = SSMR3GetStrZ(pSSM, buf, szBuf);
1145 AssertRC(vrc);
1146 hostPath = buf;
1147 delete[] buf;
1148
1149 if (u32Version > 0x00010000)
1150 SSMR3GetBool(pSSM, &writable);
1151
1152 ComObjPtr<SharedFolder> sharedFolder;
1153 sharedFolder.createObject();
1154 HRESULT rc = sharedFolder->init(this, name, hostPath, writable);
1155 AssertComRCReturn(rc, VERR_INTERNAL_ERROR);
1156
1157 mSharedFolders.insert(std::make_pair(name, sharedFolder));
1158 }
1159
1160 return VINF_SUCCESS;
1161}
1162
1163#ifdef VBOX_WITH_GUEST_PROPS
1164// static
1165DECLCALLBACK(int)
1166Console::doGuestPropNotification(void *pvExtension, uint32_t,
1167 void *pvParms, uint32_t cbParms)
1168{
1169 using namespace guestProp;
1170
1171// LogFlowFunc(("pvExtension=%p, pvParms=%p, cbParms=%u\n", pvExtension, pvParms, cbParms));
1172 int rc = VINF_SUCCESS;
1173 /* No locking, as this is purely a notification which does not make any
1174 * changes to the object state. */
1175 PHOSTCALLBACKDATA pCBData = reinterpret_cast<PHOSTCALLBACKDATA>(pvParms);
1176 AssertReturn(sizeof(HOSTCALLBACKDATA) == cbParms, VERR_INVALID_PARAMETER);
1177 AssertReturn(HOSTCALLBACKMAGIC == pCBData->u32Magic, VERR_INVALID_PARAMETER);
1178 ComObjPtr<Console> pConsole = reinterpret_cast<Console *>(pvExtension);
1179// LogFlowFunc(("pCBData->pcszName=%s, pCBData->pcszValue=%s, pCBData->pcszFlags=%s\n", pCBData->pcszName, pCBData->pcszValue, pCBData->pcszFlags));
1180 Bstr name(pCBData->pcszName);
1181 Bstr value(pCBData->pcszValue);
1182 Bstr flags(pCBData->pcszFlags);
1183 if ( name.isNull()
1184 || (value.isNull() && (pCBData->pcszValue != NULL))
1185 || (flags.isNull() && (pCBData->pcszFlags != NULL))
1186 )
1187 rc = VERR_NO_MEMORY;
1188 else
1189 {
1190 HRESULT hrc = pConsole->mControl->PushGuestProperty(name, value,
1191 pCBData->u64Timestamp,
1192 flags);
1193 if (FAILED(hrc))
1194 {
1195// LogFunc(("pConsole->mControl->PushGuestProperty failed, hrc=0x%x\n", hrc));
1196// LogFunc(("pCBData->pcszName=%s\n", pCBData->pcszName));
1197// LogFunc(("pCBData->pcszValue=%s\n", pCBData->pcszValue));
1198// LogFunc(("pCBData->pcszFlags=%s\n", pCBData->pcszFlags));
1199 rc = VERR_UNRESOLVED_ERROR; /** @todo translate error code */
1200 }
1201 }
1202// LogFlowFunc(("rc=%Rrc\n", rc));
1203 return rc;
1204}
1205
1206HRESULT Console::doEnumerateGuestProperties(CBSTR aPatterns,
1207 ComSafeArrayOut(BSTR, aNames),
1208 ComSafeArrayOut(BSTR, aValues),
1209 ComSafeArrayOut(ULONG64, aTimestamps),
1210 ComSafeArrayOut(BSTR, aFlags))
1211{
1212 using namespace guestProp;
1213
1214 VBOXHGCMSVCPARM parm[3];
1215
1216 Utf8Str utf8Patterns(aPatterns);
1217 parm[0].type = VBOX_HGCM_SVC_PARM_PTR;
1218 parm[0].u.pointer.addr = utf8Patterns.mutableRaw();
1219 parm[0].u.pointer.size = (uint32_t)utf8Patterns.length() + 1;
1220
1221 /*
1222 * Now things get slightly complicated. Due to a race with the guest adding
1223 * properties, there is no good way to know how much to enlarge a buffer for
1224 * the service to enumerate into. We choose a decent starting size and loop a
1225 * few times, each time retrying with the size suggested by the service plus
1226 * one Kb.
1227 */
1228 size_t cchBuf = 4096;
1229 Utf8Str Utf8Buf;
1230 int vrc = VERR_BUFFER_OVERFLOW;
1231 for (unsigned i = 0; i < 10 && (VERR_BUFFER_OVERFLOW == vrc); ++i)
1232 {
1233 try
1234 {
1235 Utf8Buf.reserve(cchBuf + 1024);
1236 }
1237 catch(...)
1238 {
1239 return E_OUTOFMEMORY;
1240 }
1241 parm[1].type = VBOX_HGCM_SVC_PARM_PTR;
1242 parm[1].u.pointer.addr = Utf8Buf.mutableRaw();
1243 parm[1].u.pointer.size = (uint32_t)cchBuf + 1024;
1244 vrc = mVMMDev->hgcmHostCall("VBoxGuestPropSvc", ENUM_PROPS_HOST, 3,
1245 &parm[0]);
1246 Utf8Buf.jolt();
1247 if (parm[2].type != VBOX_HGCM_SVC_PARM_32BIT)
1248 return setError(E_FAIL, tr("Internal application error"));
1249 cchBuf = parm[2].u.uint32;
1250 }
1251 if (VERR_BUFFER_OVERFLOW == vrc)
1252 return setError(E_UNEXPECTED,
1253 tr("Temporary failure due to guest activity, please retry"));
1254
1255 /*
1256 * Finally we have to unpack the data returned by the service into the safe
1257 * arrays supplied by the caller. We start by counting the number of entries.
1258 */
1259 const char *pszBuf
1260 = reinterpret_cast<const char *>(parm[1].u.pointer.addr);
1261 unsigned cEntries = 0;
1262 /* The list is terminated by a zero-length string at the end of a set
1263 * of four strings. */
1264 for (size_t i = 0; strlen(pszBuf + i) != 0; )
1265 {
1266 /* We are counting sets of four strings. */
1267 for (unsigned j = 0; j < 4; ++j)
1268 i += strlen(pszBuf + i) + 1;
1269 ++cEntries;
1270 }
1271
1272 /*
1273 * And now we create the COM safe arrays and fill them in.
1274 */
1275 com::SafeArray<BSTR> names(cEntries);
1276 com::SafeArray<BSTR> values(cEntries);
1277 com::SafeArray<ULONG64> timestamps(cEntries);
1278 com::SafeArray<BSTR> flags(cEntries);
1279 size_t iBuf = 0;
1280 /* Rely on the service to have formated the data correctly. */
1281 for (unsigned i = 0; i < cEntries; ++i)
1282 {
1283 size_t cchName = strlen(pszBuf + iBuf);
1284 Bstr(pszBuf + iBuf).detachTo(&names[i]);
1285 iBuf += cchName + 1;
1286 size_t cchValue = strlen(pszBuf + iBuf);
1287 Bstr(pszBuf + iBuf).detachTo(&values[i]);
1288 iBuf += cchValue + 1;
1289 size_t cchTimestamp = strlen(pszBuf + iBuf);
1290 timestamps[i] = RTStrToUInt64(pszBuf + iBuf);
1291 iBuf += cchTimestamp + 1;
1292 size_t cchFlags = strlen(pszBuf + iBuf);
1293 Bstr(pszBuf + iBuf).detachTo(&flags[i]);
1294 iBuf += cchFlags + 1;
1295 }
1296 names.detachTo(ComSafeArrayOutArg(aNames));
1297 values.detachTo(ComSafeArrayOutArg(aValues));
1298 timestamps.detachTo(ComSafeArrayOutArg(aTimestamps));
1299 flags.detachTo(ComSafeArrayOutArg(aFlags));
1300 return S_OK;
1301}
1302#endif
1303
1304
1305// IConsole properties
1306/////////////////////////////////////////////////////////////////////////////
1307
1308STDMETHODIMP Console::COMGETTER(Machine)(IMachine **aMachine)
1309{
1310 CheckComArgOutPointerValid(aMachine);
1311
1312 AutoCaller autoCaller(this);
1313 CheckComRCReturnRC(autoCaller.rc());
1314
1315 /* mMachine is constant during life time, no need to lock */
1316 mMachine.queryInterfaceTo(aMachine);
1317
1318 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 /* test and clear the TeleporterEnabled property */
4617 BOOL fTeleporterEnabled;
4618 rc = mMachine->COMGETTER(TeleporterEnabled)(&fTeleporterEnabled);
4619 CheckComRCReturnRC(rc);
4620 if (fTeleporterEnabled)
4621 {
4622 rc = mMachine->COMSETTER(TeleporterEnabled)(FALSE);
4623 CheckComRCReturnRC(rc);
4624 }
4625
4626 /* create a progress object to track progress of this operation */
4627 ComObjPtr<Progress> powerupProgress;
4628 powerupProgress.createObject();
4629 Bstr progressDesc;
4630 if (mMachineState == MachineState_Saved)
4631 progressDesc = tr("Restoring virtual machine");
4632 else if (fTeleporterEnabled)
4633 progressDesc = tr("Teleporting virtual machine");
4634 else
4635 progressDesc = tr("Starting virtual machine");
4636 rc = powerupProgress->init(static_cast<IConsole *>(this),
4637 progressDesc,
4638 fTeleporterEnabled /* aCancelable */);
4639 CheckComRCReturnRC(rc);
4640
4641 /* setup task object and thread to carry out the operation
4642 * asynchronously */
4643
4644 std::auto_ptr<VMPowerUpTask> task(new VMPowerUpTask(this, powerupProgress));
4645 ComAssertComRCRetRC(task->rc());
4646
4647 task->mSetVMErrorCallback = setVMErrorCallback;
4648 task->mConfigConstructor = configConstructor;
4649 task->mSharedFolders = sharedFolders;
4650 task->mStartPaused = aPaused;
4651 if (mMachineState == MachineState_Saved)
4652 task->mSavedStateFile = savedStateFile;
4653 task->mTeleporterEnabled = fTeleporterEnabled;
4654
4655 /* Reset differencing hard disks for which autoReset is true */
4656 {
4657 com::SafeIfaceArray<IMediumAttachment> atts;
4658 rc = mMachine->COMGETTER(MediumAttachments)(ComSafeArrayAsOutParam(atts));
4659 CheckComRCReturnRC(rc);
4660
4661 for (size_t i = 0; i < atts.size(); ++ i)
4662 {
4663 DeviceType_T devType;
4664 rc = atts[i]->COMGETTER(Type)(&devType);
4665 /** @todo later applies to floppies as well */
4666 if (devType == DeviceType_HardDisk)
4667 {
4668 ComPtr<IMedium> medium;
4669 rc = atts[i]->COMGETTER(Medium)(medium.asOutParam());
4670 CheckComRCReturnRC(rc);
4671
4672 /* save for later use on the powerup thread */
4673 task->hardDisks.push_back(medium);
4674
4675 /* needs autoreset? */
4676 BOOL autoReset = FALSE;
4677 rc = medium->COMGETTER(AutoReset)(&autoReset);
4678 CheckComRCReturnRC(rc);
4679
4680 if (autoReset)
4681 {
4682 ComPtr<IProgress> resetProgress;
4683 rc = medium->Reset(resetProgress.asOutParam());
4684 CheckComRCReturnRC(rc);
4685
4686 /* save for later use on the powerup thread */
4687 task->hardDiskProgresses.push_back(resetProgress);
4688 }
4689 }
4690 }
4691 }
4692
4693 rc = consoleInitReleaseLog(mMachine);
4694 CheckComRCReturnRC(rc);
4695
4696 /* pass the progress object to the caller if requested */
4697 if (aProgress)
4698 {
4699 if (task->hardDiskProgresses.size() == 0)
4700 {
4701 /* there are no other operations to track, return the powerup
4702 * progress only */
4703 powerupProgress.queryInterfaceTo(aProgress);
4704 }
4705 else
4706 {
4707 /* create a combined progress object */
4708 ComObjPtr<CombinedProgress> progress;
4709 progress.createObject();
4710 VMPowerUpTask::ProgressList progresses(task->hardDiskProgresses);
4711 progresses.push_back(ComPtr<IProgress> (powerupProgress));
4712 rc = progress->init(static_cast<IConsole *>(this),
4713 progressDesc, progresses.begin(),
4714 progresses.end());
4715 AssertComRCReturnRC(rc);
4716 progress.queryInterfaceTo(aProgress);
4717 }
4718 }
4719
4720 int vrc = RTThreadCreate(NULL, Console::powerUpThread, (void *) task.get(),
4721 0, RTTHREADTYPE_MAIN_WORKER, 0, "VMPowerUp");
4722
4723 ComAssertMsgRCRet(vrc, ("Could not create VMPowerUp thread (%Rrc)", vrc),
4724 E_FAIL);
4725
4726 /* task is now owned by powerUpThread(), so release it */
4727 task.release();
4728
4729 /* finally, set the state: no right to fail in this method afterwards
4730 * since we've already started the thread and it is now responsible for
4731 * any error reporting and appropriate state change! */
4732
4733 if (mMachineState == MachineState_Saved)
4734 setMachineState(MachineState_Restoring);
4735 else if (fTeleporterEnabled)
4736 setMachineState(MachineState_TeleportingFrom);
4737 else
4738 setMachineState(MachineState_Starting);
4739
4740 LogFlowThisFunc(("mMachineState=%d\n", mMachineState));
4741 LogFlowThisFuncLeave();
4742 return S_OK;
4743}
4744
4745/**
4746 * Internal power off worker routine.
4747 *
4748 * This method may be called only at certain places with the following meaning
4749 * as shown below:
4750 *
4751 * - if the machine state is either Running or Paused, a normal
4752 * Console-initiated powerdown takes place (e.g. PowerDown());
4753 * - if the machine state is Saving, saveStateThread() has successfully done its
4754 * job;
4755 * - if the machine state is Starting or Restoring, powerUpThread() has failed
4756 * to start/load the VM;
4757 * - if the machine state is Stopping, the VM has powered itself off (i.e. not
4758 * as a result of the powerDown() call).
4759 *
4760 * Calling it in situations other than the above will cause unexpected behavior.
4761 *
4762 * Note that this method should be the only one that destroys mpVM and sets it
4763 * to NULL.
4764 *
4765 * @param aProgress Progress object to run (may be NULL).
4766 *
4767 * @note Locks this object for writing.
4768 *
4769 * @note Never call this method from a thread that called addVMCaller() or
4770 * instantiated an AutoVMCaller object; first call releaseVMCaller() or
4771 * release(). Otherwise it will deadlock.
4772 */
4773HRESULT Console::powerDown(Progress *aProgress /*= NULL*/)
4774{
4775 LogFlowThisFuncEnter();
4776
4777 AutoCaller autoCaller(this);
4778 AssertComRCReturnRC(autoCaller.rc());
4779
4780 AutoWriteLock alock(this);
4781
4782 /* Total # of steps for the progress object. Must correspond to the
4783 * number of "advance percent count" comments in this method! */
4784 enum { StepCount = 7 };
4785 /* current step */
4786 ULONG step = 0;
4787
4788 HRESULT rc = S_OK;
4789 int vrc = VINF_SUCCESS;
4790
4791 /* sanity */
4792 Assert(mVMDestroying == false);
4793
4794 Assert(mpVM != NULL);
4795
4796 AssertMsg(mMachineState == MachineState_Running ||
4797 mMachineState == MachineState_Paused ||
4798 mMachineState == MachineState_Stuck ||
4799 mMachineState == MachineState_Saving ||
4800 mMachineState == MachineState_Starting ||
4801 mMachineState == MachineState_Restoring ||
4802 mMachineState == MachineState_TeleportingFrom || /** @todo Teleportation ???*/
4803 mMachineState == MachineState_Stopping,
4804 ("Invalid machine state: %s\n", Global::stringifyMachineState(mMachineState)));
4805
4806 LogRel(("Console::powerDown(): A request to power off the VM has been issued (mMachineState=%d, InUninit=%d)\n",
4807 mMachineState, autoCaller.state() == InUninit));
4808
4809 /* Check if we need to power off the VM. In case of mVMPoweredOff=true, the
4810 * VM has already powered itself off in vmstateChangeCallback() and is just
4811 * notifying Console about that. In case of Starting or Restoring,
4812 * powerUpThread() is calling us on failure, so the VM is already off at
4813 * that point. */
4814 if ( !mVMPoweredOff
4815 && ( mMachineState == MachineState_Starting
4816 || mMachineState == MachineState_Restoring
4817 || mMachineState == MachineState_TeleportingFrom)
4818 )
4819 mVMPoweredOff = true;
4820
4821 /* go to Stopping state if not already there. Note that we don't go from
4822 * Saving/Restoring to Stopping because vmstateChangeCallback() needs it to
4823 * set the state to Saved on VMSTATE_TERMINATED. In terms of protecting from
4824 * inappropriate operations while leaving the lock below, Saving or
4825 * Restoring should be fine too */
4826 if ( mMachineState != MachineState_Saving
4827 && mMachineState != MachineState_Restoring
4828 && mMachineState != MachineState_TeleportingFrom
4829 && mMachineState != MachineState_Stopping
4830 )
4831 setMachineState(MachineState_Stopping);
4832
4833 /* ----------------------------------------------------------------------
4834 * DONE with necessary state changes, perform the power down actions (it's
4835 * safe to leave the object lock now if needed)
4836 * ---------------------------------------------------------------------- */
4837
4838 /* Stop the VRDP server to prevent new clients connection while VM is being
4839 * powered off. */
4840 if (mConsoleVRDPServer)
4841 {
4842 LogFlowThisFunc(("Stopping VRDP server...\n"));
4843
4844 /* Leave the lock since EMT will call us back as addVMCaller()
4845 * in updateDisplayData(). */
4846 alock.leave();
4847
4848 mConsoleVRDPServer->Stop();
4849
4850 alock.enter();
4851 }
4852
4853 /* advance percent count */
4854 if (aProgress)
4855 aProgress->SetCurrentOperationProgress(99 * (++ step) / StepCount );
4856
4857#ifdef VBOX_WITH_HGCM
4858
4859# ifdef VBOX_WITH_GUEST_PROPS
4860
4861 /* Save all guest property store entries to the machine XML file */
4862 com::SafeArray<BSTR> namesOut;
4863 com::SafeArray<BSTR> valuesOut;
4864 com::SafeArray<ULONG64> timestampsOut;
4865 com::SafeArray<BSTR> flagsOut;
4866 Bstr pattern("");
4867 if (pattern.isNull())
4868 rc = E_OUTOFMEMORY;
4869 else
4870 rc = doEnumerateGuestProperties(Bstr(""), ComSafeArrayAsOutParam(namesOut),
4871 ComSafeArrayAsOutParam(valuesOut),
4872 ComSafeArrayAsOutParam(timestampsOut),
4873 ComSafeArrayAsOutParam(flagsOut));
4874 if (SUCCEEDED(rc))
4875 {
4876 try
4877 {
4878 std::vector <BSTR> names;
4879 std::vector <BSTR> values;
4880 std::vector <ULONG64> timestamps;
4881 std::vector <BSTR> flags;
4882 for (unsigned i = 0; i < namesOut.size(); ++i)
4883 {
4884 uint32_t fFlags;
4885 guestProp::validateFlags(Utf8Str(flagsOut[i]).raw(), &fFlags);
4886 if ( !( fFlags & guestProp::TRANSIENT)
4887 || (mMachineState == MachineState_Saving)
4888 )
4889 {
4890 names.push_back(namesOut[i]);
4891 values.push_back(valuesOut[i]);
4892 timestamps.push_back(timestampsOut[i]);
4893 flags.push_back(flagsOut[i]);
4894 }
4895 }
4896 com::SafeArray<BSTR> namesIn(names);
4897 com::SafeArray<BSTR> valuesIn(values);
4898 com::SafeArray<ULONG64> timestampsIn(timestamps);
4899 com::SafeArray<BSTR> flagsIn(flags);
4900 if ( namesIn.isNull()
4901 || valuesIn.isNull()
4902 || timestampsIn.isNull()
4903 || flagsIn.isNull()
4904 )
4905 throw std::bad_alloc();
4906 /* PushGuestProperties() calls DiscardSettings(), which calls us back */
4907 alock.leave();
4908 mControl->PushGuestProperties(ComSafeArrayAsInParam(namesIn),
4909 ComSafeArrayAsInParam(valuesIn),
4910 ComSafeArrayAsInParam(timestampsIn),
4911 ComSafeArrayAsInParam(flagsIn));
4912 alock.enter();
4913 }
4914 catch (std::bad_alloc)
4915 {
4916 rc = E_OUTOFMEMORY;
4917 }
4918 }
4919
4920 /* advance percent count */
4921 if (aProgress)
4922 aProgress->SetCurrentOperationProgress(99 * (++ step) / StepCount );
4923
4924# endif /* VBOX_WITH_GUEST_PROPS defined */
4925
4926 /* Shutdown HGCM services before stopping the guest, because they might
4927 * need a cleanup. */
4928 if (mVMMDev)
4929 {
4930 LogFlowThisFunc(("Shutdown HGCM...\n"));
4931
4932 /* Leave the lock since EMT will call us back as addVMCaller() */
4933 alock.leave();
4934
4935 mVMMDev->hgcmShutdown();
4936
4937 alock.enter();
4938 }
4939
4940 /* advance percent count */
4941 if (aProgress)
4942 aProgress->SetCurrentOperationProgress(99 * (++ step) / StepCount );
4943
4944#endif /* VBOX_WITH_HGCM */
4945
4946 /* ----------------------------------------------------------------------
4947 * Now, wait for all mpVM callers to finish their work if there are still
4948 * some on other threads. NO methods that need mpVM (or initiate other calls
4949 * that need it) may be called after this point
4950 * ---------------------------------------------------------------------- */
4951
4952 if (mVMCallers > 0)
4953 {
4954 /* go to the destroying state to prevent from adding new callers */
4955 mVMDestroying = true;
4956
4957 /* lazy creation */
4958 if (mVMZeroCallersSem == NIL_RTSEMEVENT)
4959 RTSemEventCreate(&mVMZeroCallersSem);
4960
4961 LogFlowThisFunc(("Waiting for mpVM callers (%d) to drop to zero...\n",
4962 mVMCallers));
4963
4964 alock.leave();
4965
4966 RTSemEventWait(mVMZeroCallersSem, RT_INDEFINITE_WAIT);
4967
4968 alock.enter();
4969 }
4970
4971 /* advance percent count */
4972 if (aProgress)
4973 aProgress->SetCurrentOperationProgress(99 * (++ step) / StepCount );
4974
4975 vrc = VINF_SUCCESS;
4976
4977 /* Power off the VM if not already done that */
4978 if (!mVMPoweredOff)
4979 {
4980 LogFlowThisFunc(("Powering off the VM...\n"));
4981
4982 /* Leave the lock since EMT will call us back on VMR3PowerOff() */
4983 alock.leave();
4984
4985 vrc = VMR3PowerOff(mpVM);
4986
4987 /* Note that VMR3PowerOff() may fail here (invalid VMSTATE) if the
4988 * VM-(guest-)initiated power off happened in parallel a ms before this
4989 * call. So far, we let this error pop up on the user's side. */
4990
4991 alock.enter();
4992
4993 }
4994 else
4995 {
4996 /* reset the flag for further re-use */
4997 mVMPoweredOff = false;
4998 }
4999
5000 /* advance percent count */
5001 if (aProgress)
5002 aProgress->SetCurrentOperationProgress(99 * (++ step) / StepCount );
5003
5004 LogFlowThisFunc(("Ready for VM destruction.\n"));
5005
5006 /* If we are called from Console::uninit(), then try to destroy the VM even
5007 * on failure (this will most likely fail too, but what to do?..) */
5008 if (RT_SUCCESS(vrc) || autoCaller.state() == InUninit)
5009 {
5010 /* If the machine has an USB comtroller, release all USB devices
5011 * (symmetric to the code in captureUSBDevices()) */
5012 bool fHasUSBController = false;
5013 {
5014 PPDMIBASE pBase;
5015 int vrc = PDMR3QueryLun(mpVM, "usb-ohci", 0, 0, &pBase);
5016 if (RT_SUCCESS(vrc))
5017 {
5018 fHasUSBController = true;
5019 detachAllUSBDevices(false /* aDone */);
5020 }
5021 }
5022
5023 /* Now we've got to destroy the VM as well. (mpVM is not valid beyond
5024 * this point). We leave the lock before calling VMR3Destroy() because
5025 * it will result into calling destructors of drivers associated with
5026 * Console children which may in turn try to lock Console (e.g. by
5027 * instantiating SafeVMPtr to access mpVM). It's safe here because
5028 * mVMDestroying is set which should prevent any activity. */
5029
5030 /* Set mpVM to NULL early just in case if some old code is not using
5031 * addVMCaller()/releaseVMCaller(). */
5032 PVM pVM = mpVM;
5033 mpVM = NULL;
5034
5035 LogFlowThisFunc(("Destroying the VM...\n"));
5036
5037 alock.leave();
5038
5039 vrc = VMR3Destroy(pVM);
5040
5041 /* take the lock again */
5042 alock.enter();
5043
5044 /* advance percent count */
5045 if (aProgress)
5046 aProgress->SetCurrentOperationProgress(99 * (++ step) / StepCount );
5047
5048 if (RT_SUCCESS(vrc))
5049 {
5050 LogFlowThisFunc(("Machine has been destroyed (mMachineState=%d)\n",
5051 mMachineState));
5052 /* Note: the Console-level machine state change happens on the
5053 * VMSTATE_TERMINATE state change in vmstateChangeCallback(). If
5054 * powerDown() is called from EMT (i.e. from vmstateChangeCallback()
5055 * on receiving VM-initiated VMSTATE_OFF), VMSTATE_TERMINATE hasn't
5056 * occurred yet. This is okay, because mMachineState is already
5057 * Stopping in this case, so any other attempt to call PowerDown()
5058 * will be rejected. */
5059 }
5060 else
5061 {
5062 /* bad bad bad, but what to do? */
5063 mpVM = pVM;
5064 rc = setError(VBOX_E_VM_ERROR,
5065 tr("Could not destroy the machine. (Error: %Rrc)"),
5066 vrc);
5067 }
5068
5069 /* Complete the detaching of the USB devices. */
5070 if (fHasUSBController)
5071 detachAllUSBDevices(true /* aDone */);
5072
5073 /* advance percent count */
5074 if (aProgress)
5075 aProgress->SetCurrentOperationProgress(99 * (++ step) / StepCount );
5076 }
5077 else
5078 {
5079 rc = setError(VBOX_E_VM_ERROR,
5080 tr("Could not power off the machine. (Error: %Rrc)"),
5081 vrc);
5082 }
5083
5084 /* Finished with destruction. Note that if something impossible happened and
5085 * we've failed to destroy the VM, mVMDestroying will remain true and
5086 * mMachineState will be something like Stopping, so most Console methods
5087 * will return an error to the caller. */
5088 if (mpVM == NULL)
5089 mVMDestroying = false;
5090
5091 if (SUCCEEDED(rc))
5092 {
5093 /* uninit dynamically allocated members of mCallbackData */
5094 if (mCallbackData.mpsc.valid)
5095 {
5096 if (mCallbackData.mpsc.shape != NULL)
5097 RTMemFree(mCallbackData.mpsc.shape);
5098 }
5099 memset(&mCallbackData, 0, sizeof(mCallbackData));
5100 }
5101
5102 /* complete the progress */
5103 if (aProgress)
5104 aProgress->notifyComplete(rc);
5105
5106 LogFlowThisFuncLeave();
5107 return rc;
5108}
5109
5110/**
5111 * @note Locks this object for writing.
5112 */
5113HRESULT Console::setMachineState(MachineState_T aMachineState,
5114 bool aUpdateServer /* = true */)
5115{
5116 AutoCaller autoCaller(this);
5117 AssertComRCReturnRC(autoCaller.rc());
5118
5119 AutoWriteLock alock(this);
5120
5121 HRESULT rc = S_OK;
5122
5123 if (mMachineState != aMachineState)
5124 {
5125 LogFlowThisFunc(("machineState=%d\n", aMachineState));
5126 mMachineState = aMachineState;
5127
5128 /// @todo (dmik)
5129 // possibly, we need to redo onStateChange() using the dedicated
5130 // Event thread, like it is done in VirtualBox. This will make it
5131 // much safer (no deadlocks possible if someone tries to use the
5132 // console from the callback), however, listeners will lose the
5133 // ability to synchronously react to state changes (is it really
5134 // necessary??)
5135 LogFlowThisFunc(("Doing onStateChange()...\n"));
5136 onStateChange(aMachineState);
5137 LogFlowThisFunc(("Done onStateChange()\n"));
5138
5139 if (aUpdateServer)
5140 {
5141 /* Server notification MUST be done from under the lock; otherwise
5142 * the machine state here and on the server might go out of sync
5143 * which can lead to various unexpected results (like the machine
5144 * state being >= MachineState_Running on the server, while the
5145 * session state is already SessionState_Closed at the same time
5146 * there).
5147 *
5148 * Cross-lock conditions should be carefully watched out: calling
5149 * UpdateState we will require Machine and SessionMachine locks
5150 * (remember that here we're holding the Console lock here, and also
5151 * all locks that have been entered by the thread before calling
5152 * this method).
5153 */
5154 LogFlowThisFunc(("Doing mControl->UpdateState()...\n"));
5155 rc = mControl->UpdateState(aMachineState);
5156 LogFlowThisFunc(("mControl->UpdateState()=%08X\n", rc));
5157 }
5158 }
5159
5160 return rc;
5161}
5162
5163/**
5164 * Searches for a shared folder with the given logical name
5165 * in the collection of shared folders.
5166 *
5167 * @param aName logical name of the shared folder
5168 * @param aSharedFolder where to return the found object
5169 * @param aSetError whether to set the error info if the folder is
5170 * not found
5171 * @return
5172 * S_OK when found or E_INVALIDARG when not found
5173 *
5174 * @note The caller must lock this object for writing.
5175 */
5176HRESULT Console::findSharedFolder(CBSTR aName,
5177 ComObjPtr<SharedFolder> &aSharedFolder,
5178 bool aSetError /* = false */)
5179{
5180 /* sanity check */
5181 AssertReturn(isWriteLockOnCurrentThread(), E_FAIL);
5182
5183 SharedFolderMap::const_iterator it = mSharedFolders.find(aName);
5184 if (it != mSharedFolders.end())
5185 {
5186 aSharedFolder = it->second;
5187 return S_OK;
5188 }
5189
5190 if (aSetError)
5191 setError(VBOX_E_FILE_ERROR,
5192 tr("Could not find a shared folder named '%ls'."),
5193 aName);
5194
5195 return VBOX_E_FILE_ERROR;
5196}
5197
5198/**
5199 * Fetches the list of global or machine shared folders from the server.
5200 *
5201 * @param aGlobal true to fetch global folders.
5202 *
5203 * @note The caller must lock this object for writing.
5204 */
5205HRESULT Console::fetchSharedFolders(BOOL aGlobal)
5206{
5207 /* sanity check */
5208 AssertReturn(AutoCaller(this).state() == InInit ||
5209 isWriteLockOnCurrentThread(), E_FAIL);
5210
5211 /* protect mpVM (if not NULL) */
5212 AutoVMCallerQuietWeak autoVMCaller(this);
5213
5214 HRESULT rc = S_OK;
5215
5216 bool online = mpVM && autoVMCaller.isOk() && mVMMDev->isShFlActive();
5217
5218 if (aGlobal)
5219 {
5220 /// @todo grab & process global folders when they are done
5221 }
5222 else
5223 {
5224 SharedFolderDataMap oldFolders;
5225 if (online)
5226 oldFolders = mMachineSharedFolders;
5227
5228 mMachineSharedFolders.clear();
5229
5230 SafeIfaceArray<ISharedFolder> folders;
5231 rc = mMachine->COMGETTER(SharedFolders)(ComSafeArrayAsOutParam(folders));
5232 AssertComRCReturnRC(rc);
5233
5234 for (size_t i = 0; i < folders.size(); ++i)
5235 {
5236 ComPtr<ISharedFolder> folder = folders[i];
5237
5238 Bstr name;
5239 Bstr hostPath;
5240 BOOL writable;
5241
5242 rc = folder->COMGETTER(Name)(name.asOutParam());
5243 CheckComRCBreakRC(rc);
5244 rc = folder->COMGETTER(HostPath)(hostPath.asOutParam());
5245 CheckComRCBreakRC(rc);
5246 rc = folder->COMGETTER(Writable)(&writable);
5247
5248 mMachineSharedFolders.insert(std::make_pair(name, SharedFolderData(hostPath, writable)));
5249
5250 /* send changes to HGCM if the VM is running */
5251 /// @todo report errors as runtime warnings through VMSetError
5252 if (online)
5253 {
5254 SharedFolderDataMap::iterator it = oldFolders.find(name);
5255 if (it == oldFolders.end() || it->second.mHostPath != hostPath)
5256 {
5257 /* a new machine folder is added or
5258 * the existing machine folder is changed */
5259 if (mSharedFolders.find(name) != mSharedFolders.end())
5260 ; /* the console folder exists, nothing to do */
5261 else
5262 {
5263 /* remove the old machine folder (when changed)
5264 * or the global folder if any (when new) */
5265 if (it != oldFolders.end() ||
5266 mGlobalSharedFolders.find(name) !=
5267 mGlobalSharedFolders.end())
5268 rc = removeSharedFolder(name);
5269 /* create the new machine folder */
5270 rc = createSharedFolder(name, SharedFolderData(hostPath, writable));
5271 }
5272 }
5273 /* forget the processed (or identical) folder */
5274 if (it != oldFolders.end())
5275 oldFolders.erase(it);
5276
5277 rc = S_OK;
5278 }
5279 }
5280
5281 AssertComRCReturnRC(rc);
5282
5283 /* process outdated (removed) folders */
5284 /// @todo report errors as runtime warnings through VMSetError
5285 if (online)
5286 {
5287 for (SharedFolderDataMap::const_iterator it = oldFolders.begin();
5288 it != oldFolders.end(); ++ it)
5289 {
5290 if (mSharedFolders.find(it->first) != mSharedFolders.end())
5291 ; /* the console folder exists, nothing to do */
5292 else
5293 {
5294 /* remove the outdated machine folder */
5295 rc = removeSharedFolder(it->first);
5296 /* create the global folder if there is any */
5297 SharedFolderDataMap::const_iterator git =
5298 mGlobalSharedFolders.find(it->first);
5299 if (git != mGlobalSharedFolders.end())
5300 rc = createSharedFolder(git->first, git->second);
5301 }
5302 }
5303
5304 rc = S_OK;
5305 }
5306 }
5307
5308 return rc;
5309}
5310
5311/**
5312 * Searches for a shared folder with the given name in the list of machine
5313 * shared folders and then in the list of the global shared folders.
5314 *
5315 * @param aName Name of the folder to search for.
5316 * @param aIt Where to store the pointer to the found folder.
5317 * @return @c true if the folder was found and @c false otherwise.
5318 *
5319 * @note The caller must lock this object for reading.
5320 */
5321bool Console::findOtherSharedFolder(IN_BSTR aName,
5322 SharedFolderDataMap::const_iterator &aIt)
5323{
5324 /* sanity check */
5325 AssertReturn(isWriteLockOnCurrentThread(), false);
5326
5327 /* first, search machine folders */
5328 aIt = mMachineSharedFolders.find(aName);
5329 if (aIt != mMachineSharedFolders.end())
5330 return true;
5331
5332 /* second, search machine folders */
5333 aIt = mGlobalSharedFolders.find(aName);
5334 if (aIt != mGlobalSharedFolders.end())
5335 return true;
5336
5337 return false;
5338}
5339
5340/**
5341 * Calls the HGCM service to add a shared folder definition.
5342 *
5343 * @param aName Shared folder name.
5344 * @param aHostPath Shared folder path.
5345 *
5346 * @note Must be called from under AutoVMCaller and when mpVM != NULL!
5347 * @note Doesn't lock anything.
5348 */
5349HRESULT Console::createSharedFolder(CBSTR aName, SharedFolderData aData)
5350{
5351 ComAssertRet(aName && *aName, E_FAIL);
5352 ComAssertRet(aData.mHostPath, E_FAIL);
5353
5354 /* sanity checks */
5355 AssertReturn(mpVM, E_FAIL);
5356 AssertReturn(mVMMDev->isShFlActive(), E_FAIL);
5357
5358 VBOXHGCMSVCPARM parms[SHFL_CPARMS_ADD_MAPPING];
5359 SHFLSTRING *pFolderName, *pMapName;
5360 size_t cbString;
5361
5362 Log(("Adding shared folder '%ls' -> '%ls'\n", aName, aData.mHostPath.raw()));
5363
5364 cbString = (RTUtf16Len(aData.mHostPath) + 1) * sizeof(RTUTF16);
5365 if (cbString >= UINT16_MAX)
5366 return setError(E_INVALIDARG, tr("The name is too long"));
5367 pFolderName = (SHFLSTRING *) RTMemAllocZ(sizeof(SHFLSTRING) + cbString);
5368 Assert(pFolderName);
5369 memcpy(pFolderName->String.ucs2, aData.mHostPath, cbString);
5370
5371 pFolderName->u16Size = (uint16_t)cbString;
5372 pFolderName->u16Length = (uint16_t)cbString - sizeof(RTUTF16);
5373
5374 parms[0].type = VBOX_HGCM_SVC_PARM_PTR;
5375 parms[0].u.pointer.addr = pFolderName;
5376 parms[0].u.pointer.size = sizeof(SHFLSTRING) + (uint16_t)cbString;
5377
5378 cbString = (RTUtf16Len(aName) + 1) * sizeof(RTUTF16);
5379 if (cbString >= UINT16_MAX)
5380 {
5381 RTMemFree(pFolderName);
5382 return setError(E_INVALIDARG, tr("The host path is too long"));
5383 }
5384 pMapName = (SHFLSTRING *) RTMemAllocZ(sizeof(SHFLSTRING) + cbString);
5385 Assert(pMapName);
5386 memcpy(pMapName->String.ucs2, aName, cbString);
5387
5388 pMapName->u16Size = (uint16_t)cbString;
5389 pMapName->u16Length = (uint16_t)cbString - sizeof(RTUTF16);
5390
5391 parms[1].type = VBOX_HGCM_SVC_PARM_PTR;
5392 parms[1].u.pointer.addr = pMapName;
5393 parms[1].u.pointer.size = sizeof(SHFLSTRING) + (uint16_t)cbString;
5394
5395 parms[2].type = VBOX_HGCM_SVC_PARM_32BIT;
5396 parms[2].u.uint32 = aData.mWritable;
5397
5398 int vrc = mVMMDev->hgcmHostCall("VBoxSharedFolders",
5399 SHFL_FN_ADD_MAPPING,
5400 SHFL_CPARMS_ADD_MAPPING, &parms[0]);
5401 RTMemFree(pFolderName);
5402 RTMemFree(pMapName);
5403
5404 if (RT_FAILURE(vrc))
5405 return setError(E_FAIL,
5406 tr("Could not create a shared folder '%ls' mapped to '%ls' (%Rrc)"),
5407 aName, aData.mHostPath.raw(), vrc);
5408
5409 return S_OK;
5410}
5411
5412/**
5413 * Calls the HGCM service to remove the shared folder definition.
5414 *
5415 * @param aName Shared folder name.
5416 *
5417 * @note Must be called from under AutoVMCaller and when mpVM != NULL!
5418 * @note Doesn't lock anything.
5419 */
5420HRESULT Console::removeSharedFolder(CBSTR aName)
5421{
5422 ComAssertRet(aName && *aName, E_FAIL);
5423
5424 /* sanity checks */
5425 AssertReturn(mpVM, E_FAIL);
5426 AssertReturn(mVMMDev->isShFlActive(), E_FAIL);
5427
5428 VBOXHGCMSVCPARM parms;
5429 SHFLSTRING *pMapName;
5430 size_t cbString;
5431
5432 Log(("Removing shared folder '%ls'\n", aName));
5433
5434 cbString = (RTUtf16Len(aName) + 1) * sizeof(RTUTF16);
5435 if (cbString >= UINT16_MAX)
5436 return setError(E_INVALIDARG, tr("The name is too long"));
5437 pMapName = (SHFLSTRING *) RTMemAllocZ(sizeof(SHFLSTRING) + cbString);
5438 Assert(pMapName);
5439 memcpy(pMapName->String.ucs2, aName, cbString);
5440
5441 pMapName->u16Size = (uint16_t)cbString;
5442 pMapName->u16Length = (uint16_t)cbString - sizeof(RTUTF16);
5443
5444 parms.type = VBOX_HGCM_SVC_PARM_PTR;
5445 parms.u.pointer.addr = pMapName;
5446 parms.u.pointer.size = sizeof(SHFLSTRING) + (uint16_t)cbString;
5447
5448 int vrc = mVMMDev->hgcmHostCall("VBoxSharedFolders",
5449 SHFL_FN_REMOVE_MAPPING,
5450 1, &parms);
5451 RTMemFree(pMapName);
5452 if (RT_FAILURE(vrc))
5453 return setError(E_FAIL,
5454 tr("Could not remove the shared folder '%ls' (%Rrc)"),
5455 aName, vrc);
5456
5457 return S_OK;
5458}
5459
5460/**
5461 * VM state callback function. Called by the VMM
5462 * using its state machine states.
5463 *
5464 * Primarily used to handle VM initiated power off, suspend and state saving,
5465 * but also for doing termination completed work (VMSTATE_TERMINATE).
5466 *
5467 * In general this function is called in the context of the EMT.
5468 *
5469 * @param aVM The VM handle.
5470 * @param aState The new state.
5471 * @param aOldState The old state.
5472 * @param aUser The user argument (pointer to the Console object).
5473 *
5474 * @note Locks the Console object for writing.
5475 */
5476DECLCALLBACK(void) Console::vmstateChangeCallback(PVM aVM,
5477 VMSTATE aState,
5478 VMSTATE aOldState,
5479 void *aUser)
5480{
5481 LogFlowFunc(("Changing state from %d to %d (aVM=%p)\n",
5482 aOldState, aState, aVM));
5483
5484 Console *that = static_cast<Console *>(aUser);
5485 AssertReturnVoid(that);
5486
5487 AutoCaller autoCaller(that);
5488
5489 /* Note that we must let this method proceed even if Console::uninit() has
5490 * been already called. In such case this VMSTATE change is a result of:
5491 * 1) powerDown() called from uninit() itself, or
5492 * 2) VM-(guest-)initiated power off. */
5493 AssertReturnVoid( autoCaller.isOk()
5494 || autoCaller.state() == InUninit);
5495
5496 switch (aState)
5497 {
5498 /*
5499 * The VM has terminated
5500 */
5501 case VMSTATE_OFF:
5502 {
5503 AutoWriteLock alock(that);
5504
5505 if (that->mVMStateChangeCallbackDisabled)
5506 break;
5507
5508 /* Do we still think that it is running? It may happen if this is a
5509 * VM-(guest-)initiated shutdown/poweroff.
5510 */
5511 if ( that->mMachineState != MachineState_Stopping
5512 && that->mMachineState != MachineState_Saving
5513 && that->mMachineState != MachineState_Restoring
5514 && that->mMachineState != MachineState_TeleportingFrom
5515 )
5516 {
5517 LogFlowFunc(("VM has powered itself off but Console still thinks it is running. Notifying.\n"));
5518
5519 /* prevent powerDown() from calling VMR3PowerOff() again */
5520 Assert(that->mVMPoweredOff == false);
5521 that->mVMPoweredOff = true;
5522
5523 /* we are stopping now */
5524 that->setMachineState(MachineState_Stopping);
5525
5526 /* Setup task object and thread to carry out the operation
5527 * asynchronously (if we call powerDown() right here but there
5528 * is one or more mpVM callers (added with addVMCaller()) we'll
5529 * deadlock).
5530 */
5531 std::auto_ptr<VMProgressTask> task(new VMProgressTask(that, NULL /* aProgress */,
5532 true /* aUsesVMPtr */));
5533
5534 /* If creating a task is falied, this can currently mean one of
5535 * two: either Console::uninit() has been called just a ms
5536 * before (so a powerDown() call is already on the way), or
5537 * powerDown() itself is being already executed. Just do
5538 * nothing.
5539 */
5540 if (!task->isOk())
5541 {
5542 LogFlowFunc(("Console is already being uninitialized.\n"));
5543 break;
5544 }
5545
5546 int vrc = RTThreadCreate(NULL, Console::powerDownThread,
5547 (void *) task.get(), 0,
5548 RTTHREADTYPE_MAIN_WORKER, 0,
5549 "VMPowerDown");
5550 AssertMsgRCBreak(vrc, ("Could not create VMPowerDown thread (%Rrc)\n", vrc));
5551
5552 /* task is now owned by powerDownThread(), so release it */
5553 task.release();
5554 }
5555 break;
5556 }
5557
5558 /* The VM has been completely destroyed.
5559 *
5560 * Note: This state change can happen at two points:
5561 * 1) At the end of VMR3Destroy() if it was not called from EMT.
5562 * 2) At the end of vmR3EmulationThread if VMR3Destroy() was
5563 * called by EMT.
5564 */
5565 case VMSTATE_TERMINATED:
5566 {
5567 AutoWriteLock alock(that);
5568
5569 if (that->mVMStateChangeCallbackDisabled)
5570 break;
5571
5572 /* Terminate host interface networking. If aVM is NULL, we've been
5573 * manually called from powerUpThread() either before calling
5574 * VMR3Create() or after VMR3Create() failed, so no need to touch
5575 * networking.
5576 */
5577 if (aVM)
5578 that->powerDownHostInterfaces();
5579
5580 /* From now on the machine is officially powered down or remains in
5581 * the Saved state.
5582 */
5583 switch (that->mMachineState)
5584 {
5585 default:
5586 AssertFailed();
5587 /* fall through */
5588 case MachineState_Stopping:
5589 /* successfully powered down */
5590 that->setMachineState(MachineState_PoweredOff);
5591 break;
5592 case MachineState_Saving:
5593 /* successfully saved (note that the machine is already in
5594 * the Saved state on the server due to EndSavingState()
5595 * called from saveStateThread(), so only change the local
5596 * state) */
5597 that->setMachineStateLocally(MachineState_Saved);
5598 break;
5599 case MachineState_Starting:
5600 /* failed to start, but be patient: set back to PoweredOff
5601 * (for similarity with the below) */
5602 that->setMachineState(MachineState_PoweredOff);
5603 break;
5604 case MachineState_Restoring:
5605 /* failed to load the saved state file, but be patient: set
5606 * back to Saved (to preserve the saved state file) */
5607 that->setMachineState(MachineState_Saved);
5608 break;
5609 case MachineState_TeleportingFrom:
5610 /* Teleportation failed or was cancelled. Back to powered off. */
5611 that->setMachineState(MachineState_PoweredOff);
5612 break;
5613 }
5614 break;
5615 }
5616
5617 case VMSTATE_SUSPENDED:
5618 {
5619 /** @todo state/live VMSTATE_SUSPENDING_LS. */
5620 if (aOldState == VMSTATE_SUSPENDING)
5621 {
5622 AutoWriteLock alock(that);
5623
5624 if (that->mVMStateChangeCallbackDisabled)
5625 break;
5626
5627 /* Change the machine state from Running to Paused. */
5628 AssertBreak(that->mMachineState == MachineState_Running);
5629 that->setMachineState(MachineState_Paused);
5630 }
5631 break;
5632 }
5633
5634 case VMSTATE_RUNNING:
5635 {
5636 if ( aOldState == VMSTATE_POWERING_ON
5637 || aOldState == VMSTATE_RESUMING)
5638 {
5639 AutoWriteLock alock(that);
5640
5641 if (that->mVMStateChangeCallbackDisabled)
5642 break;
5643
5644 /* Change the machine state from Starting, Restoring or Paused
5645 * to Running */
5646 Assert( ( ( that->mMachineState == MachineState_Starting
5647 || that->mMachineState == MachineState_Paused)
5648 && aOldState == VMSTATE_POWERING_ON)
5649 || ( ( that->mMachineState == MachineState_Restoring
5650 || that->mMachineState == MachineState_TeleportingFrom
5651 || that->mMachineState == MachineState_Paused)
5652 && aOldState == VMSTATE_RESUMING));
5653
5654 that->setMachineState(MachineState_Running);
5655 }
5656
5657 break;
5658 }
5659
5660 case VMSTATE_FATAL_ERROR:
5661 {
5662 AutoWriteLock alock(that);
5663
5664 if (that->mVMStateChangeCallbackDisabled)
5665 break;
5666
5667 /* Fatal errors are only for running VMs. */
5668 Assert(Global::IsOnline(that->mMachineState));
5669
5670 /* Note! 'Pause' is used here in want of something better. There
5671 * are currently only two places where fatal errors might be
5672 * raised, so it is not worth adding a new externally
5673 * visible state for this yet. */
5674 that->setMachineState(MachineState_Paused);
5675 break;
5676 }
5677
5678 case VMSTATE_GURU_MEDITATION:
5679 {
5680 AutoWriteLock alock(that);
5681
5682 if (that->mVMStateChangeCallbackDisabled)
5683 break;
5684
5685 /* Guru are only for running VMs */
5686 Assert(Global::IsOnline(that->mMachineState));
5687
5688 that->setMachineState(MachineState_Stuck);
5689 break;
5690 }
5691
5692 default: /* shut up gcc */
5693 break;
5694 }
5695}
5696
5697#ifdef VBOX_WITH_USB
5698
5699/**
5700 * Sends a request to VMM to attach the given host device.
5701 * After this method succeeds, the attached device will appear in the
5702 * mUSBDevices collection.
5703 *
5704 * @param aHostDevice device to attach
5705 *
5706 * @note Synchronously calls EMT.
5707 * @note Must be called from under this object's lock.
5708 */
5709HRESULT Console::attachUSBDevice(IUSBDevice *aHostDevice, ULONG aMaskedIfs)
5710{
5711 AssertReturn(aHostDevice, E_FAIL);
5712 AssertReturn(isWriteLockOnCurrentThread(), E_FAIL);
5713
5714 /* still want a lock object because we need to leave it */
5715 AutoWriteLock alock(this);
5716
5717 HRESULT hrc;
5718
5719 /*
5720 * Get the address and the Uuid, and call the pfnCreateProxyDevice roothub
5721 * method in EMT (using usbAttachCallback()).
5722 */
5723 Bstr BstrAddress;
5724 hrc = aHostDevice->COMGETTER(Address)(BstrAddress.asOutParam());
5725 ComAssertComRCRetRC(hrc);
5726
5727 Utf8Str Address(BstrAddress);
5728
5729 Bstr id;
5730 hrc = aHostDevice->COMGETTER(Id)(id.asOutParam());
5731 ComAssertComRCRetRC(hrc);
5732 Guid uuid(id);
5733
5734 BOOL fRemote = FALSE;
5735 hrc = aHostDevice->COMGETTER(Remote)(&fRemote);
5736 ComAssertComRCRetRC(hrc);
5737
5738 /* protect mpVM */
5739 AutoVMCaller autoVMCaller(this);
5740 CheckComRCReturnRC(autoVMCaller.rc());
5741
5742 LogFlowThisFunc(("Proxying USB device '%s' {%RTuuid}...\n",
5743 Address.raw(), uuid.ptr()));
5744
5745 /* leave the lock before a VMR3* call (EMT will call us back)! */
5746 alock.leave();
5747
5748/** @todo just do everything here and only wrap the PDMR3Usb call. That'll offload some notification stuff from the EMT thread. */
5749 int vrc = VMR3ReqCallWait(mpVM, VMCPUID_ANY,
5750 (PFNRT) usbAttachCallback, 6, this, aHostDevice, uuid.ptr(), fRemote, Address.raw(), aMaskedIfs);
5751
5752 /* restore the lock */
5753 alock.enter();
5754
5755 /* hrc is S_OK here */
5756
5757 if (RT_FAILURE(vrc))
5758 {
5759 LogWarningThisFunc(("Failed to create proxy device for '%s' {%RTuuid} (%Rrc)\n",
5760 Address.raw(), uuid.ptr(), vrc));
5761
5762 switch (vrc)
5763 {
5764 case VERR_VUSB_NO_PORTS:
5765 hrc = setError(E_FAIL,
5766 tr("Failed to attach the USB device. (No available ports on the USB controller)."));
5767 break;
5768 case VERR_VUSB_USBFS_PERMISSION:
5769 hrc = setError(E_FAIL,
5770 tr("Not permitted to open the USB device, check usbfs options"));
5771 break;
5772 default:
5773 hrc = setError(E_FAIL,
5774 tr("Failed to create a proxy device for the USB device. (Error: %Rrc)"),
5775 vrc);
5776 break;
5777 }
5778 }
5779
5780 return hrc;
5781}
5782
5783/**
5784 * USB device attach callback used by AttachUSBDevice().
5785 * Note that AttachUSBDevice() doesn't return until this callback is executed,
5786 * so we don't use AutoCaller and don't care about reference counters of
5787 * interface pointers passed in.
5788 *
5789 * @thread EMT
5790 * @note Locks the console object for writing.
5791 */
5792//static
5793DECLCALLBACK(int)
5794Console::usbAttachCallback(Console *that, IUSBDevice *aHostDevice, PCRTUUID aUuid, bool aRemote, const char *aAddress, ULONG aMaskedIfs)
5795{
5796 LogFlowFuncEnter();
5797 LogFlowFunc(("that={%p}\n", that));
5798
5799 AssertReturn(that && aUuid, VERR_INVALID_PARAMETER);
5800
5801 void *pvRemoteBackend = NULL;
5802 if (aRemote)
5803 {
5804 RemoteUSBDevice *pRemoteUSBDevice = static_cast<RemoteUSBDevice *>(aHostDevice);
5805 Guid guid(*aUuid);
5806
5807 pvRemoteBackend = that->consoleVRDPServer()->USBBackendRequestPointer(pRemoteUSBDevice->clientId(), &guid);
5808 if (!pvRemoteBackend)
5809 return VERR_INVALID_PARAMETER; /* The clientId is invalid then. */
5810 }
5811
5812 USHORT portVersion = 1;
5813 HRESULT hrc = aHostDevice->COMGETTER(PortVersion)(&portVersion);
5814 AssertComRCReturn(hrc, VERR_GENERAL_FAILURE);
5815 Assert(portVersion == 1 || portVersion == 2);
5816
5817 int vrc = PDMR3USBCreateProxyDevice(that->mpVM, aUuid, aRemote, aAddress, pvRemoteBackend,
5818 portVersion == 1 ? VUSB_STDVER_11 : VUSB_STDVER_20, aMaskedIfs);
5819 if (RT_SUCCESS(vrc))
5820 {
5821 /* Create a OUSBDevice and add it to the device list */
5822 ComObjPtr<OUSBDevice> device;
5823 device.createObject();
5824 HRESULT hrc = device->init(aHostDevice);
5825 AssertComRC(hrc);
5826
5827 AutoWriteLock alock(that);
5828 that->mUSBDevices.push_back(device);
5829 LogFlowFunc(("Attached device {%RTuuid}\n", device->id().raw()));
5830
5831 /* notify callbacks */
5832 that->onUSBDeviceStateChange(device, true /* aAttached */, NULL);
5833 }
5834
5835 LogFlowFunc(("vrc=%Rrc\n", vrc));
5836 LogFlowFuncLeave();
5837 return vrc;
5838}
5839
5840/**
5841 * Sends a request to VMM to detach the given host device. After this method
5842 * succeeds, the detached device will disappear from the mUSBDevices
5843 * collection.
5844 *
5845 * @param aIt Iterator pointing to the device to detach.
5846 *
5847 * @note Synchronously calls EMT.
5848 * @note Must be called from under this object's lock.
5849 */
5850HRESULT Console::detachUSBDevice(USBDeviceList::iterator &aIt)
5851{
5852 AssertReturn(isWriteLockOnCurrentThread(), E_FAIL);
5853
5854 /* still want a lock object because we need to leave it */
5855 AutoWriteLock alock(this);
5856
5857 /* protect mpVM */
5858 AutoVMCaller autoVMCaller(this);
5859 CheckComRCReturnRC(autoVMCaller.rc());
5860
5861 /* if the device is attached, then there must at least one USB hub. */
5862 AssertReturn(PDMR3USBHasHub(mpVM), E_FAIL);
5863
5864 LogFlowThisFunc(("Detaching USB proxy device {%RTuuid}...\n",
5865 (*aIt)->id().raw()));
5866
5867 /* leave the lock before a VMR3* call (EMT will call us back)! */
5868 alock.leave();
5869
5870/** @todo just do everything here and only wrap the PDMR3Usb call. That'll offload some notification stuff from the EMT thread. */
5871 int vrc = VMR3ReqCallWait(mpVM, VMCPUID_ANY,
5872 (PFNRT) usbDetachCallback, 4, this, &aIt, (*aIt)->id().raw());
5873 ComAssertRCRet(vrc, E_FAIL);
5874
5875 return S_OK;
5876}
5877
5878/**
5879 * USB device detach callback used by DetachUSBDevice().
5880 * Note that DetachUSBDevice() doesn't return until this callback is executed,
5881 * so we don't use AutoCaller and don't care about reference counters of
5882 * interface pointers passed in.
5883 *
5884 * @thread EMT
5885 * @note Locks the console object for writing.
5886 */
5887//static
5888DECLCALLBACK(int)
5889Console::usbDetachCallback(Console *that, USBDeviceList::iterator *aIt, PCRTUUID aUuid)
5890{
5891 LogFlowFuncEnter();
5892 LogFlowFunc(("that={%p}\n", that));
5893
5894 AssertReturn(that && aUuid, VERR_INVALID_PARAMETER);
5895 ComObjPtr<OUSBDevice> device = **aIt;
5896
5897 /*
5898 * If that was a remote device, release the backend pointer.
5899 * The pointer was requested in usbAttachCallback.
5900 */
5901 BOOL fRemote = FALSE;
5902
5903 HRESULT hrc2 = (**aIt)->COMGETTER(Remote)(&fRemote);
5904 ComAssertComRC(hrc2);
5905
5906 if (fRemote)
5907 {
5908 Guid guid(*aUuid);
5909 that->consoleVRDPServer()->USBBackendReleasePointer(&guid);
5910 }
5911
5912 int vrc = PDMR3USBDetachDevice(that->mpVM, aUuid);
5913
5914 if (RT_SUCCESS(vrc))
5915 {
5916 AutoWriteLock alock(that);
5917
5918 /* Remove the device from the collection */
5919 that->mUSBDevices.erase(*aIt);
5920 LogFlowFunc(("Detached device {%RTuuid}\n", device->id().raw()));
5921
5922 /* notify callbacks */
5923 that->onUSBDeviceStateChange(device, false /* aAttached */, NULL);
5924 }
5925
5926 LogFlowFunc(("vrc=%Rrc\n", vrc));
5927 LogFlowFuncLeave();
5928 return vrc;
5929}
5930
5931#endif /* VBOX_WITH_USB */
5932#if (defined(RT_OS_LINUX) || defined(RT_OS_FREEBSD)) && !defined(VBOX_WITH_NETFLT)
5933
5934/**
5935 * Helper function to handle host interface device creation and attachment.
5936 *
5937 * @param networkAdapter the network adapter which attachment should be reset
5938 * @return COM status code
5939 *
5940 * @note The caller must lock this object for writing.
5941 *
5942 * @todo Move this back into the driver!
5943 */
5944HRESULT Console::attachToTapInterface(INetworkAdapter *networkAdapter)
5945{
5946 LogFlowThisFunc(("\n"));
5947 /* sanity check */
5948 AssertReturn(isWriteLockOnCurrentThread(), E_FAIL);
5949
5950# ifdef VBOX_STRICT
5951 /* paranoia */
5952 NetworkAttachmentType_T attachment;
5953 networkAdapter->COMGETTER(AttachmentType)(&attachment);
5954 Assert(attachment == NetworkAttachmentType_Bridged);
5955# endif /* VBOX_STRICT */
5956
5957 HRESULT rc = S_OK;
5958
5959 ULONG slot = 0;
5960 rc = networkAdapter->COMGETTER(Slot)(&slot);
5961 AssertComRC(rc);
5962
5963# ifdef RT_OS_LINUX
5964 /*
5965 * Allocate a host interface device
5966 */
5967 int rcVBox = RTFileOpen(&maTapFD[slot], "/dev/net/tun",
5968 RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_INHERIT);
5969 if (RT_SUCCESS(rcVBox))
5970 {
5971 /*
5972 * Set/obtain the tap interface.
5973 */
5974 struct ifreq IfReq;
5975 memset(&IfReq, 0, sizeof(IfReq));
5976 /* The name of the TAP interface we are using */
5977 Bstr tapDeviceName;
5978 rc = networkAdapter->COMGETTER(HostInterface)(tapDeviceName.asOutParam());
5979 if (FAILED(rc))
5980 tapDeviceName.setNull(); /* Is this necessary? */
5981 if (tapDeviceName.isEmpty())
5982 {
5983 LogRel(("No TAP device name was supplied.\n"));
5984 rc = setError(E_FAIL, tr("No TAP device name was supplied for the host networking interface"));
5985 }
5986
5987 if (SUCCEEDED(rc))
5988 {
5989 /* If we are using a static TAP device then try to open it. */
5990 Utf8Str str(tapDeviceName);
5991 if (str.length() <= sizeof(IfReq.ifr_name))
5992 strcpy(IfReq.ifr_name, str.raw());
5993 else
5994 memcpy(IfReq.ifr_name, str.raw(), sizeof(IfReq.ifr_name) - 1); /** @todo bitch about names which are too long... */
5995 IfReq.ifr_flags = IFF_TAP | IFF_NO_PI;
5996 rcVBox = ioctl(maTapFD[slot], TUNSETIFF, &IfReq);
5997 if (rcVBox != 0)
5998 {
5999 LogRel(("Failed to open the host network interface %ls\n", tapDeviceName.raw()));
6000 rc = setError(E_FAIL,
6001 tr("Failed to open the host network interface %ls"),
6002 tapDeviceName.raw());
6003 }
6004 }
6005 if (SUCCEEDED(rc))
6006 {
6007 /*
6008 * Make it pollable.
6009 */
6010 if (fcntl(maTapFD[slot], F_SETFL, O_NONBLOCK) != -1)
6011 {
6012 Log(("attachToTapInterface: %RTfile %ls\n", maTapFD[slot], tapDeviceName.raw()));
6013 /*
6014 * Here is the right place to communicate the TAP file descriptor and
6015 * the host interface name to the server if/when it becomes really
6016 * necessary.
6017 */
6018 maTAPDeviceName[slot] = tapDeviceName;
6019 rcVBox = VINF_SUCCESS;
6020 }
6021 else
6022 {
6023 int iErr = errno;
6024
6025 LogRel(("Configuration error: Failed to configure /dev/net/tun non blocking. Error: %s\n", strerror(iErr)));
6026 rcVBox = VERR_HOSTIF_BLOCKING;
6027 rc = setError(E_FAIL,
6028 tr("could not set up the host networking device for non blocking access: %s"),
6029 strerror(errno));
6030 }
6031 }
6032 }
6033 else
6034 {
6035 LogRel(("Configuration error: Failed to open /dev/net/tun rc=%Rrc\n", rcVBox));
6036 switch (rcVBox)
6037 {
6038 case VERR_ACCESS_DENIED:
6039 /* will be handled by our caller */
6040 rc = rcVBox;
6041 break;
6042 default:
6043 rc = setError(E_FAIL,
6044 tr("Could not set up the host networking device: %Rrc"),
6045 rcVBox);
6046 break;
6047 }
6048 }
6049
6050# elif defined(RT_OS_FREEBSD)
6051 /*
6052 * Set/obtain the tap interface.
6053 */
6054 /* The name of the TAP interface we are using */
6055 Bstr tapDeviceName;
6056 rc = networkAdapter->COMGETTER(HostInterface)(tapDeviceName.asOutParam());
6057 if (FAILED(rc))
6058 tapDeviceName.setNull(); /* Is this necessary? */
6059 if (tapDeviceName.isEmpty())
6060 {
6061 LogRel(("No TAP device name was supplied.\n"));
6062 rc = setError(E_FAIL, tr("No TAP device name was supplied for the host networking interface"));
6063 }
6064 char szTapdev[1024] = "/dev/";
6065 /* If we are using a static TAP device then try to open it. */
6066 Utf8Str str(tapDeviceName);
6067 if (str.length() + strlen(szTapdev) <= sizeof(szTapdev))
6068 strcat(szTapdev, str.raw());
6069 else
6070 memcpy(szTapdev + strlen(szTapdev), str.raw(), sizeof(szTapdev) - strlen(szTapdev) - 1); /** @todo bitch about names which are too long... */
6071 int rcVBox = RTFileOpen(&maTapFD[slot], szTapdev,
6072 RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_INHERIT | RTFILE_O_NON_BLOCK);
6073
6074 if (RT_SUCCESS(rcVBox))
6075 maTAPDeviceName[slot] = tapDeviceName;
6076 else
6077 {
6078 switch (rcVBox)
6079 {
6080 case VERR_ACCESS_DENIED:
6081 /* will be handled by our caller */
6082 rc = rcVBox;
6083 break;
6084 default:
6085 rc = setError(E_FAIL,
6086 tr("Failed to open the host network interface %ls"),
6087 tapDeviceName.raw());
6088 break;
6089 }
6090 }
6091# else
6092# error "huh?"
6093# endif
6094 /* in case of failure, cleanup. */
6095 if (RT_FAILURE(rcVBox) && SUCCEEDED(rc))
6096 {
6097 LogRel(("General failure attaching to host interface\n"));
6098 rc = setError(E_FAIL,
6099 tr("General failure attaching to host interface"));
6100 }
6101 LogFlowThisFunc(("rc=%d\n", rc));
6102 return rc;
6103}
6104
6105
6106/**
6107 * Helper function to handle detachment from a host interface
6108 *
6109 * @param networkAdapter the network adapter which attachment should be reset
6110 * @return COM status code
6111 *
6112 * @note The caller must lock this object for writing.
6113 *
6114 * @todo Move this back into the driver!
6115 */
6116HRESULT Console::detachFromTapInterface(INetworkAdapter *networkAdapter)
6117{
6118 /* sanity check */
6119 LogFlowThisFunc(("\n"));
6120 AssertReturn(isWriteLockOnCurrentThread(), E_FAIL);
6121
6122 HRESULT rc = S_OK;
6123# ifdef VBOX_STRICT
6124 /* paranoia */
6125 NetworkAttachmentType_T attachment;
6126 networkAdapter->COMGETTER(AttachmentType)(&attachment);
6127 Assert(attachment == NetworkAttachmentType_Bridged);
6128# endif /* VBOX_STRICT */
6129
6130 ULONG slot = 0;
6131 rc = networkAdapter->COMGETTER(Slot)(&slot);
6132 AssertComRC(rc);
6133
6134 /* is there an open TAP device? */
6135 if (maTapFD[slot] != NIL_RTFILE)
6136 {
6137 /*
6138 * Close the file handle.
6139 */
6140 Bstr tapDeviceName, tapTerminateApplication;
6141 bool isStatic = true;
6142 rc = networkAdapter->COMGETTER(HostInterface)(tapDeviceName.asOutParam());
6143 if (FAILED(rc) || tapDeviceName.isEmpty())
6144 {
6145 /* If the name is empty, this is a dynamic TAP device, so close it now,
6146 so that the termination script can remove the interface. Otherwise we still
6147 need the FD to pass to the termination script. */
6148 isStatic = false;
6149 int rcVBox = RTFileClose(maTapFD[slot]);
6150 AssertRC(rcVBox);
6151 maTapFD[slot] = NIL_RTFILE;
6152 }
6153 if (isStatic)
6154 {
6155 /* If we are using a static TAP device, we close it now, after having called the
6156 termination script. */
6157 int rcVBox = RTFileClose(maTapFD[slot]);
6158 AssertRC(rcVBox);
6159 }
6160 /* the TAP device name and handle are no longer valid */
6161 maTapFD[slot] = NIL_RTFILE;
6162 maTAPDeviceName[slot] = "";
6163 }
6164 LogFlowThisFunc(("returning %d\n", rc));
6165 return rc;
6166}
6167
6168#endif /* (RT_OS_LINUX || RT_OS_FREEBSD) && !VBOX_WITH_NETFLT */
6169
6170/**
6171 * Called at power down to terminate host interface networking.
6172 *
6173 * @note The caller must lock this object for writing.
6174 */
6175HRESULT Console::powerDownHostInterfaces()
6176{
6177 LogFlowThisFunc(("\n"));
6178
6179 /* sanity check */
6180 AssertReturn(isWriteLockOnCurrentThread(), E_FAIL);
6181
6182 /*
6183 * host interface termination handling
6184 */
6185 HRESULT rc;
6186 for (ULONG slot = 0; slot < SchemaDefs::NetworkAdapterCount; slot ++)
6187 {
6188 ComPtr<INetworkAdapter> networkAdapter;
6189 rc = mMachine->GetNetworkAdapter(slot, networkAdapter.asOutParam());
6190 CheckComRCBreakRC(rc);
6191
6192 BOOL enabled = FALSE;
6193 networkAdapter->COMGETTER(Enabled)(&enabled);
6194 if (!enabled)
6195 continue;
6196
6197 NetworkAttachmentType_T attachment;
6198 networkAdapter->COMGETTER(AttachmentType)(&attachment);
6199 if (attachment == NetworkAttachmentType_Bridged)
6200 {
6201#if defined(RT_OS_LINUX) && !defined(VBOX_WITH_NETFLT)
6202 HRESULT rc2 = detachFromTapInterface(networkAdapter);
6203 if (FAILED(rc2) && SUCCEEDED(rc))
6204 rc = rc2;
6205#endif
6206 }
6207 }
6208
6209 return rc;
6210}
6211
6212
6213/**
6214 * Process callback handler for VMR3LoadFromFile, VMR3LoadFromStream, VMR3Save
6215 * and VMR3Teleport.
6216 *
6217 * @param pVM The VM handle.
6218 * @param uPercent Completetion precentage (0-100).
6219 * @param pvUser Pointer to the VMProgressTask structure.
6220 * @return VINF_SUCCESS.
6221 */
6222/*static*/
6223DECLCALLBACK(int) Console::stateProgressCallback(PVM pVM, unsigned uPercent, void *pvUser)
6224{
6225 VMProgressTask *task = static_cast<VMProgressTask *>(pvUser);
6226 AssertReturn(task, VERR_INVALID_PARAMETER);
6227
6228 /* update the progress object */
6229 if (task->mProgress)
6230 task->mProgress->SetCurrentOperationProgress(uPercent);
6231
6232 return VINF_SUCCESS;
6233}
6234
6235/**
6236 * VM error callback function. Called by the various VM components.
6237 *
6238 * @param pVM VM handle. Can be NULL if an error occurred before
6239 * successfully creating a VM.
6240 * @param pvUser Pointer to the VMProgressTask structure.
6241 * @param rc VBox status code.
6242 * @param pszFormat Printf-like error message.
6243 * @param args Various number of arguments for the error message.
6244 *
6245 * @thread EMT, VMPowerUp...
6246 *
6247 * @note The VMProgressTask structure modified by this callback is not thread
6248 * safe.
6249 */
6250/* static */ DECLCALLBACK(void)
6251Console::setVMErrorCallback(PVM pVM, void *pvUser, int rc, RT_SRC_POS_DECL,
6252 const char *pszFormat, va_list args)
6253{
6254 VMProgressTask *task = static_cast<VMProgressTask *>(pvUser);
6255 AssertReturnVoid(task);
6256
6257 /* we ignore RT_SRC_POS_DECL arguments to avoid confusion of end-users */
6258 va_list va2;
6259 va_copy(va2, args); /* Have to make a copy here or GCC will break. */
6260
6261 /* append to the existing error message if any */
6262 if (task->mErrorMsg.length())
6263 task->mErrorMsg = Utf8StrFmt("%s.\n%N (%Rrc)", task->mErrorMsg.raw(),
6264 pszFormat, &va2, rc, rc);
6265 else
6266 task->mErrorMsg = Utf8StrFmt("%N (%Rrc)",
6267 pszFormat, &va2, rc, rc);
6268
6269 va_end (va2);
6270}
6271
6272/**
6273 * VM runtime error callback function.
6274 * See VMSetRuntimeError for the detailed description of parameters.
6275 *
6276 * @param pVM The VM handle.
6277 * @param pvUser The user argument.
6278 * @param fFlags The action flags. See VMSETRTERR_FLAGS_*.
6279 * @param pszErrorId Error ID string.
6280 * @param pszFormat Error message format string.
6281 * @param va Error message arguments.
6282 * @thread EMT.
6283 */
6284/* static */ DECLCALLBACK(void)
6285Console::setVMRuntimeErrorCallback(PVM pVM, void *pvUser, uint32_t fFlags,
6286 const char *pszErrorId,
6287 const char *pszFormat, va_list va)
6288{
6289 bool const fFatal = !!(fFlags & VMSETRTERR_FLAGS_FATAL);
6290 LogFlowFuncEnter();
6291
6292 Console *that = static_cast<Console *>(pvUser);
6293 AssertReturnVoid(that);
6294
6295 Utf8Str message = Utf8StrFmtVA(pszFormat, va);
6296
6297 LogRel(("Console: VM runtime error: fatal=%RTbool, errorID=%s message=\"%s\"\n",
6298 fFatal, pszErrorId, message.raw()));
6299
6300 that->onRuntimeError(BOOL(fFatal), Bstr(pszErrorId), Bstr(message));
6301
6302 LogFlowFuncLeave();
6303}
6304
6305/**
6306 * Captures USB devices that match filters of the VM.
6307 * Called at VM startup.
6308 *
6309 * @param pVM The VM handle.
6310 *
6311 * @note The caller must lock this object for writing.
6312 */
6313HRESULT Console::captureUSBDevices(PVM pVM)
6314{
6315 LogFlowThisFunc(("\n"));
6316
6317 /* sanity check */
6318 ComAssertRet(isWriteLockOnCurrentThread(), E_FAIL);
6319
6320 /* If the machine has an USB controller, ask the USB proxy service to
6321 * capture devices */
6322 PPDMIBASE pBase;
6323 int vrc = PDMR3QueryLun(pVM, "usb-ohci", 0, 0, &pBase);
6324 if (RT_SUCCESS(vrc))
6325 {
6326 /* leave the lock before calling Host in VBoxSVC since Host may call
6327 * us back from under its lock (e.g. onUSBDeviceAttach()) which would
6328 * produce an inter-process dead-lock otherwise. */
6329 AutoWriteLock alock(this);
6330 alock.leave();
6331
6332 HRESULT hrc = mControl->AutoCaptureUSBDevices();
6333 ComAssertComRCRetRC(hrc);
6334 }
6335 else if ( vrc == VERR_PDM_DEVICE_NOT_FOUND
6336 || vrc == VERR_PDM_DEVICE_INSTANCE_NOT_FOUND)
6337 vrc = VINF_SUCCESS;
6338 else
6339 AssertRC(vrc);
6340
6341 return RT_SUCCESS(vrc) ? S_OK : E_FAIL;
6342}
6343
6344
6345/**
6346 * Detach all USB device which are attached to the VM for the
6347 * purpose of clean up and such like.
6348 *
6349 * @note The caller must lock this object for writing.
6350 */
6351void Console::detachAllUSBDevices(bool aDone)
6352{
6353 LogFlowThisFunc(("aDone=%RTbool\n", aDone));
6354
6355 /* sanity check */
6356 AssertReturnVoid(isWriteLockOnCurrentThread());
6357
6358 mUSBDevices.clear();
6359
6360 /* leave the lock before calling Host in VBoxSVC since Host may call
6361 * us back from under its lock (e.g. onUSBDeviceAttach()) which would
6362 * produce an inter-process dead-lock otherwise. */
6363 AutoWriteLock alock(this);
6364 alock.leave();
6365
6366 mControl->DetachAllUSBDevices(aDone);
6367}
6368
6369/**
6370 * @note Locks this object for writing.
6371 */
6372void Console::processRemoteUSBDevices(uint32_t u32ClientId, VRDPUSBDEVICEDESC *pDevList, uint32_t cbDevList)
6373{
6374 LogFlowThisFuncEnter();
6375 LogFlowThisFunc(("u32ClientId = %d, pDevList=%p, cbDevList = %d\n", u32ClientId, pDevList, cbDevList));
6376
6377 AutoCaller autoCaller(this);
6378 if (!autoCaller.isOk())
6379 {
6380 /* Console has been already uninitialized, deny request */
6381 AssertMsgFailed(("Console is already uninitialized\n"));
6382 LogFlowThisFunc(("Console is already uninitialized\n"));
6383 LogFlowThisFuncLeave();
6384 return;
6385 }
6386
6387 AutoWriteLock alock(this);
6388
6389 /*
6390 * Mark all existing remote USB devices as dirty.
6391 */
6392 RemoteUSBDeviceList::iterator it = mRemoteUSBDevices.begin();
6393 while (it != mRemoteUSBDevices.end())
6394 {
6395 (*it)->dirty(true);
6396 ++ it;
6397 }
6398
6399 /*
6400 * Process the pDevList and add devices those are not already in the mRemoteUSBDevices list.
6401 */
6402 /** @todo (sunlover) REMOTE_USB Strict validation of the pDevList. */
6403 VRDPUSBDEVICEDESC *e = pDevList;
6404
6405 /* The cbDevList condition must be checked first, because the function can
6406 * receive pDevList = NULL and cbDevList = 0 on client disconnect.
6407 */
6408 while (cbDevList >= 2 && e->oNext)
6409 {
6410 LogFlowThisFunc(("vendor %04X, product %04X, name = %s\n",
6411 e->idVendor, e->idProduct,
6412 e->oProduct? (char *)e + e->oProduct: ""));
6413
6414 bool fNewDevice = true;
6415
6416 it = mRemoteUSBDevices.begin();
6417 while (it != mRemoteUSBDevices.end())
6418 {
6419 if ((*it)->devId() == e->id
6420 && (*it)->clientId() == u32ClientId)
6421 {
6422 /* The device is already in the list. */
6423 (*it)->dirty(false);
6424 fNewDevice = false;
6425 break;
6426 }
6427
6428 ++ it;
6429 }
6430
6431 if (fNewDevice)
6432 {
6433 LogRel(("Remote USB: ++++ Vendor %04X. Product %04X. Name = [%s].\n",
6434 e->idVendor, e->idProduct, e->oProduct? (char *)e + e->oProduct: ""));
6435
6436 /* Create the device object and add the new device to list. */
6437 ComObjPtr<RemoteUSBDevice> device;
6438 device.createObject();
6439 device->init(u32ClientId, e);
6440
6441 mRemoteUSBDevices.push_back(device);
6442
6443 /* Check if the device is ok for current USB filters. */
6444 BOOL fMatched = FALSE;
6445 ULONG fMaskedIfs = 0;
6446
6447 HRESULT hrc = mControl->RunUSBDeviceFilters(device, &fMatched, &fMaskedIfs);
6448
6449 AssertComRC(hrc);
6450
6451 LogFlowThisFunc(("USB filters return %d %#x\n", fMatched, fMaskedIfs));
6452
6453 if (fMatched)
6454 {
6455 hrc = onUSBDeviceAttach(device, NULL, fMaskedIfs);
6456
6457 /// @todo (r=dmik) warning reporting subsystem
6458
6459 if (hrc == S_OK)
6460 {
6461 LogFlowThisFunc(("Device attached\n"));
6462 device->captured(true);
6463 }
6464 }
6465 }
6466
6467 if (cbDevList < e->oNext)
6468 {
6469 LogWarningThisFunc(("cbDevList %d > oNext %d\n",
6470 cbDevList, e->oNext));
6471 break;
6472 }
6473
6474 cbDevList -= e->oNext;
6475
6476 e = (VRDPUSBDEVICEDESC *)((uint8_t *)e + e->oNext);
6477 }
6478
6479 /*
6480 * Remove dirty devices, that is those which are not reported by the server anymore.
6481 */
6482 for (;;)
6483 {
6484 ComObjPtr<RemoteUSBDevice> device;
6485
6486 RemoteUSBDeviceList::iterator it = mRemoteUSBDevices.begin();
6487 while (it != mRemoteUSBDevices.end())
6488 {
6489 if ((*it)->dirty())
6490 {
6491 device = *it;
6492 break;
6493 }
6494
6495 ++ it;
6496 }
6497
6498 if (!device)
6499 {
6500 break;
6501 }
6502
6503 USHORT vendorId = 0;
6504 device->COMGETTER(VendorId)(&vendorId);
6505
6506 USHORT productId = 0;
6507 device->COMGETTER(ProductId)(&productId);
6508
6509 Bstr product;
6510 device->COMGETTER(Product)(product.asOutParam());
6511
6512 LogRel(("Remote USB: ---- Vendor %04X. Product %04X. Name = [%ls].\n",
6513 vendorId, productId, product.raw()));
6514
6515 /* Detach the device from VM. */
6516 if (device->captured())
6517 {
6518 Bstr uuid;
6519 device->COMGETTER(Id)(uuid.asOutParam());
6520 onUSBDeviceDetach(uuid, NULL);
6521 }
6522
6523 /* And remove it from the list. */
6524 mRemoteUSBDevices.erase(it);
6525 }
6526
6527 LogFlowThisFuncLeave();
6528}
6529
6530/**
6531 * Thread function which starts the VM (also from saved state) and
6532 * track progress.
6533 *
6534 * @param Thread The thread id.
6535 * @param pvUser Pointer to a VMPowerUpTask structure.
6536 * @return VINF_SUCCESS (ignored).
6537 *
6538 * @note Locks the Console object for writing.
6539 */
6540/*static*/
6541DECLCALLBACK(int) Console::powerUpThread(RTTHREAD Thread, void *pvUser)
6542{
6543 LogFlowFuncEnter();
6544
6545 std::auto_ptr<VMPowerUpTask> task(static_cast<VMPowerUpTask *>(pvUser));
6546 AssertReturn(task.get(), VERR_INVALID_PARAMETER);
6547
6548 AssertReturn(!task->mConsole.isNull(), VERR_INVALID_PARAMETER);
6549 AssertReturn(!task->mProgress.isNull(), VERR_INVALID_PARAMETER);
6550
6551#if defined(RT_OS_WINDOWS)
6552 {
6553 /* initialize COM */
6554 HRESULT hrc = CoInitializeEx(NULL,
6555 COINIT_MULTITHREADED | COINIT_DISABLE_OLE1DDE |
6556 COINIT_SPEED_OVER_MEMORY);
6557 LogFlowFunc(("CoInitializeEx()=%08X\n", hrc));
6558 }
6559#endif
6560
6561 HRESULT rc = S_OK;
6562 int vrc = VINF_SUCCESS;
6563
6564 /* Set up a build identifier so that it can be seen from core dumps what
6565 * exact build was used to produce the core. */
6566 static char saBuildID[40];
6567 RTStrPrintf(saBuildID, sizeof(saBuildID), "%s%s%s%s VirtualBox %s r%u %s%s%s%s",
6568 "BU", "IL", "DI", "D", VBOX_VERSION_STRING, RTBldCfgRevision(), "BU", "IL", "DI", "D");
6569
6570 ComObjPtr<Console> console = task->mConsole;
6571
6572 /* Note: no need to use addCaller() because VMPowerUpTask does that */
6573
6574 /* The lock is also used as a signal from the task initiator (which
6575 * releases it only after RTThreadCreate()) that we can start the job */
6576 AutoWriteLock alock(console);
6577
6578 /* sanity */
6579 Assert(console->mpVM == NULL);
6580
6581 try
6582 {
6583 /* wait for auto reset ops to complete so that we can successfully lock
6584 * the attached hard disks by calling LockMedia() below */
6585 for (VMPowerUpTask::ProgressList::const_iterator
6586 it = task->hardDiskProgresses.begin();
6587 it != task->hardDiskProgresses.end(); ++ it)
6588 {
6589 HRESULT rc2 = (*it)->WaitForCompletion(-1);
6590 AssertComRC(rc2);
6591 }
6592
6593 /* lock attached media. This method will also check their
6594 * accessibility. Note that the media will be unlocked automatically
6595 * by SessionMachine::setMachineState() when the VM is powered down. */
6596 rc = console->mControl->LockMedia();
6597 CheckComRCThrowRC(rc);
6598
6599#ifdef VBOX_WITH_VRDP
6600
6601 /* Create the VRDP server. In case of headless operation, this will
6602 * also create the framebuffer, required at VM creation.
6603 */
6604 ConsoleVRDPServer *server = console->consoleVRDPServer();
6605 Assert(server);
6606
6607 /* Does VRDP server call Console from the other thread?
6608 * Not sure (and can change), so leave the lock just in case.
6609 */
6610 alock.leave();
6611 vrc = server->Launch();
6612 alock.enter();
6613
6614 if (vrc == VERR_NET_ADDRESS_IN_USE)
6615 {
6616 Utf8Str errMsg;
6617 Bstr bstr;
6618 console->mVRDPServer->COMGETTER(Ports)(bstr.asOutParam());
6619 Utf8Str ports = bstr;
6620 errMsg = Utf8StrFmt(tr("VRDP server can't bind to a port: %s"),
6621 ports.raw());
6622 LogRel(("Warning: failed to launch VRDP server (%Rrc): '%s'\n",
6623 vrc, errMsg.raw()));
6624 }
6625 else if (RT_FAILURE(vrc))
6626 {
6627 Utf8Str errMsg;
6628 switch (vrc)
6629 {
6630 case VERR_FILE_NOT_FOUND:
6631 {
6632 errMsg = Utf8StrFmt(tr("Could not load the VRDP library"));
6633 break;
6634 }
6635 default:
6636 errMsg = Utf8StrFmt(tr("Failed to launch VRDP server (%Rrc)"),
6637 vrc);
6638 }
6639 LogRel(("Failed to launch VRDP server (%Rrc), error message: '%s'\n",
6640 vrc, errMsg.raw()));
6641 throw setError(E_FAIL, errMsg.c_str());
6642 }
6643
6644#endif /* VBOX_WITH_VRDP */
6645
6646 ComPtr<IMachine> pMachine = console->machine();
6647 ULONG cCpus = 1;
6648 pMachine->COMGETTER(CPUCount)(&cCpus);
6649
6650 /*
6651 * Create the VM
6652 */
6653 PVM pVM;
6654 /*
6655 * leave the lock since EMT will call Console. It's safe because
6656 * mMachineState is either Starting or Restoring state here.
6657 */
6658 alock.leave();
6659
6660 vrc = VMR3Create(cCpus, task->mSetVMErrorCallback, task.get(),
6661 task->mConfigConstructor, static_cast<Console *>(console),
6662 &pVM);
6663
6664 alock.enter();
6665
6666#ifdef VBOX_WITH_VRDP
6667 /* Enable client connections to the server. */
6668 console->consoleVRDPServer()->EnableConnections();
6669#endif /* VBOX_WITH_VRDP */
6670
6671 if (RT_SUCCESS(vrc))
6672 {
6673 do
6674 {
6675 /*
6676 * Register our load/save state file handlers
6677 */
6678 vrc = SSMR3RegisterExternal(pVM, sSSMConsoleUnit, 0 /*iInstance*/, sSSMConsoleVer, 0 /* cbGuess */,
6679 NULL, NULL, NULL,
6680 NULL, saveStateFileExec, NULL,
6681 NULL, loadStateFileExec, NULL,
6682 static_cast<Console *>(console));
6683 AssertRCBreak(vrc);
6684
6685 vrc = static_cast<Console *>(console)->getDisplay()->registerSSM(pVM);
6686 AssertRC(vrc);
6687 if (RT_FAILURE(vrc))
6688 break;
6689
6690 /*
6691 * Synchronize debugger settings
6692 */
6693 MachineDebugger *machineDebugger = console->getMachineDebugger();
6694 if (machineDebugger)
6695 {
6696 machineDebugger->flushQueuedSettings();
6697 }
6698
6699 /*
6700 * Shared Folders
6701 */
6702 if (console->getVMMDev()->isShFlActive())
6703 {
6704 /* Does the code below call Console from the other thread?
6705 * Not sure, so leave the lock just in case. */
6706 alock.leave();
6707
6708 for (SharedFolderDataMap::const_iterator
6709 it = task->mSharedFolders.begin();
6710 it != task->mSharedFolders.end();
6711 ++ it)
6712 {
6713 rc = console->createSharedFolder((*it).first, (*it).second);
6714 CheckComRCBreakRC(rc);
6715 }
6716
6717 /* enter the lock again */
6718 alock.enter();
6719
6720 CheckComRCBreakRC(rc);
6721 }
6722
6723 /*
6724 * Capture USB devices.
6725 */
6726 rc = console->captureUSBDevices(pVM);
6727 CheckComRCBreakRC(rc);
6728
6729 /* leave the lock before a lengthy operation */
6730 alock.leave();
6731
6732 /* Load saved state? */
6733 if (task->mSavedStateFile.length())
6734 {
6735 LogFlowFunc(("Restoring saved state from '%s'...\n",
6736 task->mSavedStateFile.raw()));
6737
6738 vrc = VMR3LoadFromFile(pVM,
6739 task->mSavedStateFile.c_str(),
6740 Console::stateProgressCallback,
6741 static_cast<VMProgressTask*>(task.get()));
6742
6743 if (RT_SUCCESS(vrc))
6744 {
6745 if (task->mStartPaused)
6746 /* done */
6747 console->setMachineState(MachineState_Paused);
6748 else
6749 {
6750 /* Start/Resume the VM execution */
6751 vrc = VMR3Resume(pVM);
6752 AssertRC(vrc);
6753 }
6754 }
6755
6756 /* Power off in case we failed loading or resuming the VM */
6757 if (RT_FAILURE(vrc))
6758 {
6759 int vrc2 = VMR3PowerOff(pVM);
6760 AssertRC(vrc2);
6761 }
6762 }
6763 else if (task->mTeleporterEnabled)
6764 /* -> ConsoleImplTeleporter.cpp */
6765 vrc = console->teleporterTrg(pVM, pMachine, task->mStartPaused, task->mProgress);
6766 else if (task->mStartPaused)
6767 /* done */
6768 console->setMachineState(MachineState_Paused);
6769 else
6770 {
6771 /* Power on the VM (i.e. start executing) */
6772 vrc = VMR3PowerOn(pVM);
6773 AssertRC(vrc);
6774 }
6775
6776 /* enter the lock again */
6777 alock.enter();
6778 }
6779 while (0);
6780
6781 /* On failure, destroy the VM */
6782 if (FAILED(rc) || RT_FAILURE(vrc))
6783 {
6784 /* preserve existing error info */
6785 ErrorInfoKeeper eik;
6786
6787 /* powerDown() will call VMR3Destroy() and do all necessary
6788 * cleanup (VRDP, USB devices) */
6789 HRESULT rc2 = console->powerDown();
6790 AssertComRC(rc2);
6791 }
6792 else
6793 {
6794 /*
6795 * Deregister the VMSetError callback. This is necessary as the
6796 * pfnVMAtError() function passed to VMR3Create() is supposed to
6797 * be sticky but our error callback isn't.
6798 */
6799 alock.leave();
6800 VMR3AtErrorDeregister(pVM, task->mSetVMErrorCallback, task.get());
6801 /** @todo register another VMSetError callback? */
6802 alock.enter();
6803 }
6804 }
6805 else
6806 {
6807 /*
6808 * If VMR3Create() failed it has released the VM memory.
6809 */
6810 console->mpVM = NULL;
6811 }
6812
6813 if (SUCCEEDED(rc) && RT_FAILURE(vrc))
6814 {
6815 /* If VMR3Create() or one of the other calls in this function fail,
6816 * an appropriate error message has been set in task->mErrorMsg.
6817 * However since that happens via a callback, the rc status code in
6818 * this function is not updated.
6819 */
6820 if (!task->mErrorMsg.length())
6821 {
6822 /* If the error message is not set but we've got a failure,
6823 * convert the VBox status code into a meaningfulerror message.
6824 * This becomes unused once all the sources of errors set the
6825 * appropriate error message themselves.
6826 */
6827 AssertMsgFailed(("Missing error message during powerup for status code %Rrc\n", vrc));
6828 task->mErrorMsg = Utf8StrFmt(tr("Failed to start VM execution (%Rrc)"),
6829 vrc);
6830 }
6831
6832 /* Set the error message as the COM error.
6833 * Progress::notifyComplete() will pick it up later. */
6834 throw setError(E_FAIL, task->mErrorMsg.c_str());
6835 }
6836 }
6837 catch (HRESULT aRC) { rc = aRC; }
6838
6839 if ( console->mMachineState == MachineState_Starting
6840 || console->mMachineState == MachineState_Restoring
6841 || console->mMachineState == MachineState_TeleportingFrom
6842 )
6843 {
6844 /* We are still in the Starting/Restoring state. This means one of:
6845 *
6846 * 1) we failed before VMR3Create() was called;
6847 * 2) VMR3Create() failed.
6848 *
6849 * In both cases, there is no need to call powerDown(), but we still
6850 * need to go back to the PoweredOff/Saved state. Reuse
6851 * vmstateChangeCallback() for that purpose.
6852 */
6853
6854 /* preserve existing error info */
6855 ErrorInfoKeeper eik;
6856
6857 Assert(console->mpVM == NULL);
6858 vmstateChangeCallback(NULL, VMSTATE_TERMINATED, VMSTATE_CREATING,
6859 console);
6860 }
6861
6862 /*
6863 * Evaluate the final result. Note that the appropriate mMachineState value
6864 * is already set by vmstateChangeCallback() in all cases.
6865 */
6866
6867 /* leave the lock, don't need it any more */
6868 alock.leave();
6869
6870 if (SUCCEEDED(rc))
6871 {
6872 /* Notify the progress object of the success */
6873 task->mProgress->notifyComplete(S_OK);
6874 }
6875 else
6876 {
6877 /* The progress object will fetch the current error info */
6878 task->mProgress->notifyComplete(rc);
6879
6880 LogRel(("Power up failed (vrc=%Rrc, rc=%Rhrc (%#08X))\n", vrc, rc, rc));
6881 }
6882
6883#if defined(RT_OS_WINDOWS)
6884 /* uninitialize COM */
6885 CoUninitialize();
6886#endif
6887
6888 LogFlowFuncLeave();
6889
6890 return VINF_SUCCESS;
6891}
6892
6893
6894/**
6895 * Reconfigures a VDI.
6896 *
6897 * @param pVM The VM handle.
6898 * @param lInstance The instance of the controller.
6899 * @param enmController The type of the controller.
6900 * @param hda The harddisk attachment.
6901 * @param phrc Where to store com error - only valid if we return VERR_GENERAL_FAILURE.
6902 * @return VBox status code.
6903 */
6904static DECLCALLBACK(int) reconfigureHardDisks(PVM pVM, ULONG lInstance,
6905 StorageControllerType_T enmController,
6906 IMediumAttachment *hda,
6907 HRESULT *phrc)
6908{
6909 LogFlowFunc(("pVM=%p hda=%p phrc=%p\n", pVM, hda, phrc));
6910
6911 int rc;
6912 HRESULT hrc;
6913 Bstr bstr;
6914 *phrc = S_OK;
6915#define RC_CHECK() do { if (RT_FAILURE(rc)) { AssertMsgFailed(("rc=%Rrc\n", rc)); return rc; } } while (0)
6916#define H() do { if (FAILED(hrc)) { AssertMsgFailed(("hrc=%Rhrc (%#x)\n", hrc, hrc)); *phrc = hrc; return VERR_GENERAL_FAILURE; } } while (0)
6917
6918 /*
6919 * Figure out which IDE device this is.
6920 */
6921 ComPtr<IMedium> hardDisk;
6922 hrc = hda->COMGETTER(Medium)(hardDisk.asOutParam()); H();
6923 LONG lDev;
6924 hrc = hda->COMGETTER(Device)(&lDev); H();
6925 LONG lPort;
6926 hrc = hda->COMGETTER(Port)(&lPort); H();
6927
6928 int iLUN;
6929 const char *pcszDevice = NULL;
6930 bool fSCSI = false;
6931
6932 switch (enmController)
6933 {
6934 case StorageControllerType_PIIX3:
6935 case StorageControllerType_PIIX4:
6936 case StorageControllerType_ICH6:
6937 {
6938 if (lPort >= 2 || lPort < 0)
6939 {
6940 AssertMsgFailed(("invalid controller channel number: %d\n", lPort));
6941 return VERR_GENERAL_FAILURE;
6942 }
6943
6944 if (lDev >= 2 || lDev < 0)
6945 {
6946 AssertMsgFailed(("invalid controller device number: %d\n", lDev));
6947 return VERR_GENERAL_FAILURE;
6948 }
6949
6950 iLUN = 2*lPort + lDev;
6951 pcszDevice = "piix3ide";
6952 break;
6953 }
6954 case StorageControllerType_IntelAhci:
6955 {
6956 iLUN = lPort;
6957 pcszDevice = "ahci";
6958 break;
6959 }
6960 case StorageControllerType_BusLogic:
6961 {
6962 iLUN = lPort;
6963 pcszDevice = "buslogic";
6964 fSCSI = true;
6965 break;
6966 }
6967 case StorageControllerType_LsiLogic:
6968 {
6969 iLUN = lPort;
6970 pcszDevice = "lsilogicscsi";
6971 fSCSI = true;
6972 break;
6973 }
6974 default:
6975 {
6976 AssertMsgFailed(("invalid disk controller type: %d\n", enmController));
6977 return VERR_GENERAL_FAILURE;
6978 }
6979 }
6980
6981 /** @todo this should be unified with the relevant part of
6982 * Console::configConstructor to avoid inconsistencies. */
6983
6984 /*
6985 * Is there an existing LUN? If not create it.
6986 * We ASSUME that this will NEVER collide with the DVD.
6987 */
6988 PCFGMNODE pCfg;
6989 PCFGMNODE pLunL1;
6990
6991 /* SCSI has an extra driver between the device and the block driver. */
6992 if (fSCSI)
6993 pLunL1 = CFGMR3GetChildF(CFGMR3GetRoot(pVM), "Devices/%s/%u/LUN#%d/AttachedDriver/AttachedDriver/", pcszDevice, lInstance, iLUN);
6994 else
6995 pLunL1 = CFGMR3GetChildF(CFGMR3GetRoot(pVM), "Devices/%s/%u/LUN#%d/AttachedDriver/", pcszDevice, lInstance, iLUN);
6996
6997 if (!pLunL1)
6998 {
6999 PCFGMNODE pInst = CFGMR3GetChildF(CFGMR3GetRoot(pVM), "Devices/%s/%u/", pcszDevice, lInstance);
7000 AssertReturn(pInst, VERR_INTERNAL_ERROR);
7001
7002 PCFGMNODE pLunL0;
7003 rc = CFGMR3InsertNodeF(pInst, &pLunL0, "LUN#%d", iLUN); RC_CHECK();
7004
7005 if (fSCSI)
7006 {
7007 rc = CFGMR3InsertString(pLunL0, "Driver", "SCSI"); RC_CHECK();
7008 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
7009
7010 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL0); RC_CHECK();
7011 }
7012
7013 rc = CFGMR3InsertString(pLunL0, "Driver", "Block"); RC_CHECK();
7014 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
7015 rc = CFGMR3InsertString(pCfg, "Type", "HardDisk"); RC_CHECK();
7016 rc = CFGMR3InsertInteger(pCfg, "Mountable", 0); RC_CHECK();
7017
7018 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL1); RC_CHECK();
7019 rc = CFGMR3InsertString(pLunL1, "Driver", "VD"); RC_CHECK();
7020 rc = CFGMR3InsertNode(pLunL1, "Config", &pCfg); RC_CHECK();
7021 }
7022 else
7023 {
7024#ifdef VBOX_STRICT
7025 char *pszDriver;
7026 rc = CFGMR3QueryStringAlloc(pLunL1, "Driver", &pszDriver); RC_CHECK();
7027 Assert(!strcmp(pszDriver, "VD"));
7028 MMR3HeapFree(pszDriver);
7029#endif
7030
7031 pCfg = CFGMR3GetChild(pLunL1, "Config");
7032 AssertReturn(pCfg, VERR_INTERNAL_ERROR);
7033
7034 /* Here used to be a lot of code checking if things have changed,
7035 * but that's not really worth it, as with snapshots there is always
7036 * some change, so the code was just logging useless information in
7037 * a hard to analyze form. */
7038
7039 /*
7040 * Detach the driver and replace the config node.
7041 */
7042 rc = PDMR3DeviceDetach(pVM, pcszDevice, 0, iLUN, PDM_TACH_FLAGS_NOT_HOT_PLUG); RC_CHECK();
7043 CFGMR3RemoveNode(pCfg);
7044 rc = CFGMR3InsertNode(pLunL1, "Config", &pCfg); RC_CHECK();
7045 }
7046
7047 /*
7048 * Create the driver configuration.
7049 */
7050 hrc = hardDisk->COMGETTER(Location)(bstr.asOutParam()); H();
7051 LogFlowFunc(("LUN#%d: leaf location '%ls'\n", iLUN, bstr.raw()));
7052 rc = CFGMR3InsertString(pCfg, "Path", Utf8Str(bstr).c_str()); RC_CHECK();
7053 hrc = hardDisk->COMGETTER(Format)(bstr.asOutParam()); H();
7054 LogFlowFunc(("LUN#%d: leaf format '%ls'\n", iLUN, bstr.raw()));
7055 rc = CFGMR3InsertString(pCfg, "Format", Utf8Str(bstr).c_str()); RC_CHECK();
7056
7057 /* Pass all custom parameters. */
7058 bool fHostIP = true;
7059 SafeArray<BSTR> names;
7060 SafeArray<BSTR> values;
7061 hrc = hardDisk->GetProperties(NULL,
7062 ComSafeArrayAsOutParam(names),
7063 ComSafeArrayAsOutParam(values)); H();
7064
7065 if (names.size() != 0)
7066 {
7067 PCFGMNODE pVDC;
7068 rc = CFGMR3InsertNode(pCfg, "VDConfig", &pVDC); RC_CHECK();
7069 for (size_t i = 0; i < names.size(); ++ i)
7070 {
7071 if (values[i])
7072 {
7073 Utf8Str name = names[i];
7074 Utf8Str value = values[i];
7075 rc = CFGMR3InsertString(pVDC, name.c_str(), value.c_str());
7076 if ( !(name.compare("HostIPStack"))
7077 && !(value.compare("0")))
7078 fHostIP = false;
7079 }
7080 }
7081 }
7082
7083 /* Create an inversed tree of parents. */
7084 ComPtr<IMedium> parentHardDisk = hardDisk;
7085 for (PCFGMNODE pParent = pCfg;;)
7086 {
7087 hrc = parentHardDisk->COMGETTER(Parent)(hardDisk.asOutParam()); H();
7088 if (hardDisk.isNull())
7089 break;
7090
7091 PCFGMNODE pCur;
7092 rc = CFGMR3InsertNode(pParent, "Parent", &pCur); RC_CHECK();
7093 hrc = hardDisk->COMGETTER(Location)(bstr.asOutParam()); H();
7094 rc = CFGMR3InsertString(pCur, "Path", Utf8Str(bstr).c_str()); RC_CHECK();
7095
7096 hrc = hardDisk->COMGETTER(Format)(bstr.asOutParam()); H();
7097 rc = CFGMR3InsertString(pCur, "Format", Utf8Str(bstr).c_str()); RC_CHECK();
7098
7099 /* Pass all custom parameters. */
7100 SafeArray<BSTR> names;
7101 SafeArray<BSTR> values;
7102 hrc = hardDisk->GetProperties(NULL,
7103 ComSafeArrayAsOutParam(names),
7104 ComSafeArrayAsOutParam(values)); H();
7105
7106 if (names.size() != 0)
7107 {
7108 PCFGMNODE pVDC;
7109 rc = CFGMR3InsertNode(pCur, "VDConfig", &pVDC); RC_CHECK();
7110 for (size_t i = 0; i < names.size(); ++ i)
7111 {
7112 if (values[i])
7113 {
7114 Utf8Str name = names[i];
7115 Utf8Str value = values[i];
7116 rc = CFGMR3InsertString(pVDC, name.c_str(), value.c_str());
7117 if ( !(name.compare("HostIPStack"))
7118 && !(value.compare("0")))
7119 fHostIP = false;
7120 }
7121 }
7122 }
7123
7124
7125 /* Custom code: put marker to not use host IP stack to driver
7126 * configuration node. Simplifies life of DrvVD a bit. */
7127 if (!fHostIP)
7128 {
7129 rc = CFGMR3InsertInteger(pCfg, "HostIPStack", 0); RC_CHECK();
7130 }
7131
7132
7133 /* next */
7134 pParent = pCur;
7135 parentHardDisk = hardDisk;
7136 }
7137
7138 CFGMR3Dump(CFGMR3GetRoot(pVM));
7139
7140 /*
7141 * Attach the new driver.
7142 */
7143 rc = PDMR3DeviceAttach(pVM, pcszDevice, 0, iLUN, PDM_TACH_FLAGS_NOT_HOT_PLUG, NULL /*ppBase*/); RC_CHECK();
7144
7145 LogFlowFunc(("Returns success\n"));
7146 return rc;
7147}
7148
7149/**
7150 * Worker thread created by Console::TakeSnapshot.
7151 * @param Thread The current thread (ignored).
7152 * @param pvUser The task.
7153 * @return VINF_SUCCESS (ignored).
7154 */
7155/*static*/
7156DECLCALLBACK(int) Console::fntTakeSnapshotWorker(RTTHREAD Thread, void *pvUser)
7157{
7158 VMTakeSnapshotTask *pTask = (VMTakeSnapshotTask*)pvUser;
7159
7160 // taking a snapshot consists of the following:
7161
7162 // 1) creating a diff image for each virtual hard disk, into which write operations go after
7163 // the snapshot has been created (done in VBoxSVC, in SessionMachine::BeginTakingSnapshot)
7164 // 2) creating a Snapshot object with the state of the machine (hardware + storage,
7165 // done in VBoxSVC, also in SessionMachine::BeginTakingSnapshot)
7166 // 3) saving the state of the virtual machine (here, in the VM process, if the machine is online)
7167
7168 bool fBeganTakingSnapshot = false;
7169
7170 AutoCaller autoCaller(pTask->mConsole);
7171 CheckComRCReturnRC(autoCaller.rc());
7172
7173 AutoWriteLock alock(pTask->mConsole);
7174
7175 HRESULT rc = S_OK;
7176
7177 Console *that = pTask->mConsole;
7178
7179 try
7180 {
7181 /* STEP 1 + 2:
7182 * request creating the diff images on the server and create the snapshot object
7183 * (this will set the machine state to Saving on the server to block
7184 * others from accessing this machine)
7185 */
7186 rc = pTask->mConsole->mControl->BeginTakingSnapshot(that,
7187 pTask->bstrName,
7188 pTask->bstrDescription,
7189 pTask->mProgress,
7190 pTask->fTakingSnapshotOnline,
7191 pTask->bstrSavedStateFile.asOutParam());
7192 if (FAILED(rc)) throw rc;
7193
7194 fBeganTakingSnapshot = true;
7195
7196 /*
7197 * state file is non-null only when the VM is paused
7198 * (i.e. creating a snapshot online)
7199 */
7200 ComAssertThrow( (!pTask->bstrSavedStateFile.isNull() && pTask->fTakingSnapshotOnline)
7201 || (pTask->bstrSavedStateFile.isNull() && !pTask->fTakingSnapshotOnline),
7202 rc = E_FAIL);
7203
7204 /* sync the state with the server */
7205 that->setMachineStateLocally(MachineState_Saving);
7206
7207 // STEP 3: save the VM state (if online)
7208 if (pTask->fTakingSnapshotOnline)
7209 {
7210 Utf8Str strSavedStateFile(pTask->bstrSavedStateFile);
7211
7212 pTask->mProgress->SetNextOperation(Bstr(tr("Saving the machine state")),
7213 pTask->ulMemSize); // operation weight, same as computed when setting up progress object
7214
7215 alock.leave();
7216
7217 int vrc = VMR3Save(that->mpVM,
7218 strSavedStateFile.c_str(),
7219 true /*fContinueAfterwards*/,
7220 Console::stateProgressCallback,
7221 (void*)pTask);
7222 if (RT_FAILURE(vrc))
7223 throw setError(E_FAIL,
7224 tr("Failed to save the machine state to '%s' (%Rrc)"),
7225 strSavedStateFile.c_str(), vrc);
7226
7227 alock.enter();
7228
7229 // STEP 4: reattach hard disks
7230 LogFlowFunc(("Reattaching new differencing hard disks...\n"));
7231
7232 pTask->mProgress->SetNextOperation(Bstr(tr("Reconfiguring hard disks")),
7233 1); // operation weight, same as computed when setting up progress object
7234
7235 com::SafeIfaceArray<IMediumAttachment> atts;
7236 rc = that->mMachine->COMGETTER(MediumAttachments)(ComSafeArrayAsOutParam(atts));
7237 if (FAILED(rc)) throw rc;
7238
7239 for (size_t i = 0;
7240 i < atts.size();
7241 ++i)
7242 {
7243 ComPtr<IStorageController> controller;
7244 BSTR controllerName;
7245 ULONG lInstance;
7246 StorageControllerType_T enmController;
7247
7248 /*
7249 * We can't pass a storage controller object directly
7250 * (g++ complains about not being able to pass non POD types through '...')
7251 * so we have to query needed values here and pass them.
7252 */
7253 rc = atts[i]->COMGETTER(Controller)(&controllerName);
7254 if (FAILED(rc))
7255 break;
7256
7257 rc = that->mMachine->GetStorageControllerByName(controllerName, controller.asOutParam());
7258 if (FAILED(rc)) throw rc;
7259
7260 rc = controller->COMGETTER(ControllerType)(&enmController);
7261 rc = controller->COMGETTER(Instance)(&lInstance);
7262
7263 /*
7264 * don't leave the lock since reconfigureHardDisks isn't going
7265 * to access Console.
7266 */
7267 int vrc = VMR3ReqCallWait(that->mpVM,
7268 VMCPUID_ANY,
7269 (PFNRT)reconfigureHardDisks,
7270 5,
7271 that->mpVM,
7272 lInstance,
7273 enmController,
7274 atts[i],
7275 &rc);
7276 if (RT_FAILURE(vrc))
7277 throw setError(E_FAIL, Console::tr("%Rrc"), vrc);
7278 if (FAILED(rc)) throw rc;
7279 break;
7280 if (FAILED(rc))
7281 break;
7282 }
7283 }
7284
7285 /*
7286 * finalize the requested snapshot object.
7287 * This will reset the machine state to the state it had right
7288 * before calling mControl->BeginTakingSnapshot().
7289 */
7290 rc = that->mControl->EndTakingSnapshot(TRUE); // success
7291 // do not throw rc here because we can't call EndTakingSnapshot() twice
7292 }
7293 catch (HRESULT rc)
7294 {
7295 /* preserve existing error info */
7296 ErrorInfoKeeper eik;
7297
7298 if (fBeganTakingSnapshot)
7299 that->mControl->EndTakingSnapshot(FALSE); // failure
7300 }
7301
7302 pTask->mProgress->notifyComplete(rc);
7303
7304 delete pTask;
7305
7306 if (pTask->lastMachineState == MachineState_Running)
7307 {
7308 /* restore the paused state if appropriate */
7309 that->setMachineStateLocally(MachineState_Paused);
7310 /* restore the running state if appropriate */
7311 that->Resume();
7312 }
7313 else
7314 that->setMachineStateLocally(pTask->lastMachineState);
7315
7316 LogFlowFuncLeave();
7317 return VINF_SUCCESS;
7318}
7319
7320/**
7321 * Thread for executing the saved state operation.
7322 *
7323 * @param Thread The thread handle.
7324 * @param pvUser Pointer to a VMSaveTask structure.
7325 * @return VINF_SUCCESS (ignored).
7326 *
7327 * @note Locks the Console object for writing.
7328 */
7329/*static*/
7330DECLCALLBACK(int) Console::saveStateThread(RTTHREAD Thread, void *pvUser)
7331{
7332 LogFlowFuncEnter();
7333
7334 std::auto_ptr<VMSaveTask> task(static_cast<VMSaveTask*>(pvUser));
7335 AssertReturn(task.get(), VERR_INVALID_PARAMETER);
7336
7337 Assert(task->mSavedStateFile.length());
7338 Assert(!task->mProgress.isNull());
7339
7340 const ComObjPtr<Console> &that = task->mConsole;
7341 Utf8Str errMsg;
7342 HRESULT rc = S_OK;
7343
7344 LogFlowFunc(("Saving the state to '%s'...\n", task->mSavedStateFile.raw()));
7345
7346 int vrc = VMR3Save(that->mpVM,
7347 task->mSavedStateFile.c_str(),
7348 false, /*fContinueAfterwards*/
7349 Console::stateProgressCallback,
7350 static_cast<VMProgressTask*>(task.get()));
7351 if (RT_FAILURE(vrc))
7352 {
7353 errMsg = Utf8StrFmt(Console::tr("Failed to save the machine state to '%s' (%Rrc)"),
7354 task->mSavedStateFile.raw(), vrc);
7355 rc = E_FAIL;
7356 }
7357
7358 /* lock the console once we're going to access it */
7359 AutoWriteLock thatLock(that);
7360
7361 /*
7362 * finalize the requested save state procedure.
7363 * In case of success, the server will set the machine state to Saved;
7364 * in case of failure it will reset the it to the state it had right
7365 * before calling mControl->BeginSavingState().
7366 */
7367 that->mControl->EndSavingState(SUCCEEDED(rc));
7368
7369 /* synchronize the state with the server */
7370 if (!FAILED(rc))
7371 {
7372 /*
7373 * The machine has been successfully saved, so power it down
7374 * (vmstateChangeCallback() will set state to Saved on success).
7375 * Note: we release the task's VM caller, otherwise it will
7376 * deadlock.
7377 */
7378 task->releaseVMCaller();
7379
7380 rc = that->powerDown();
7381 }
7382
7383 /* notify the progress object about operation completion */
7384 if (SUCCEEDED(rc))
7385 task->mProgress->notifyComplete(S_OK);
7386 else
7387 {
7388 if (errMsg.length())
7389 task->mProgress->notifyComplete(rc,
7390 COM_IIDOF(IConsole),
7391 Console::getComponentName(),
7392 errMsg.c_str());
7393 else
7394 task->mProgress->notifyComplete(rc);
7395 }
7396
7397 LogFlowFuncLeave();
7398 return VINF_SUCCESS;
7399}
7400
7401/**
7402 * Thread for powering down the Console.
7403 *
7404 * @param Thread The thread handle.
7405 * @param pvUser Pointer to the VMTask structure.
7406 * @return VINF_SUCCESS (ignored).
7407 *
7408 * @note Locks the Console object for writing.
7409 */
7410/*static*/
7411DECLCALLBACK(int) Console::powerDownThread(RTTHREAD Thread, void *pvUser)
7412{
7413 LogFlowFuncEnter();
7414
7415 std::auto_ptr<VMProgressTask> task(static_cast<VMProgressTask *>(pvUser));
7416 AssertReturn(task.get(), VERR_INVALID_PARAMETER);
7417
7418 AssertReturn(task->isOk(), VERR_GENERAL_FAILURE);
7419
7420 const ComObjPtr<Console> &that = task->mConsole;
7421
7422 /* Note: no need to use addCaller() to protect Console because VMTask does
7423 * that */
7424
7425 /* wait until the method tat started us returns */
7426 AutoWriteLock thatLock(that);
7427
7428 /* release VM caller to avoid the powerDown() deadlock */
7429 task->releaseVMCaller();
7430
7431 that->powerDown(task->mProgress);
7432
7433 LogFlowFuncLeave();
7434 return VINF_SUCCESS;
7435}
7436
7437/**
7438 * The Main status driver instance data.
7439 */
7440typedef struct DRVMAINSTATUS
7441{
7442 /** The LED connectors. */
7443 PDMILEDCONNECTORS ILedConnectors;
7444 /** Pointer to the LED ports interface above us. */
7445 PPDMILEDPORTS pLedPorts;
7446 /** Pointer to the array of LED pointers. */
7447 PPDMLED *papLeds;
7448 /** The unit number corresponding to the first entry in the LED array. */
7449 RTUINT iFirstLUN;
7450 /** The unit number corresponding to the last entry in the LED array.
7451 * (The size of the LED array is iLastLUN - iFirstLUN + 1.) */
7452 RTUINT iLastLUN;
7453} DRVMAINSTATUS, *PDRVMAINSTATUS;
7454
7455
7456/**
7457 * Notification about a unit which have been changed.
7458 *
7459 * The driver must discard any pointers to data owned by
7460 * the unit and requery it.
7461 *
7462 * @param pInterface Pointer to the interface structure containing the called function pointer.
7463 * @param iLUN The unit number.
7464 */
7465DECLCALLBACK(void) Console::drvStatus_UnitChanged(PPDMILEDCONNECTORS pInterface, unsigned iLUN)
7466{
7467 PDRVMAINSTATUS pData = (PDRVMAINSTATUS)(void *)pInterface;
7468 if (iLUN >= pData->iFirstLUN && iLUN <= pData->iLastLUN)
7469 {
7470 PPDMLED pLed;
7471 int rc = pData->pLedPorts->pfnQueryStatusLed(pData->pLedPorts, iLUN, &pLed);
7472 if (RT_FAILURE(rc))
7473 pLed = NULL;
7474 ASMAtomicXchgPtr((void * volatile *)&pData->papLeds[iLUN - pData->iFirstLUN], pLed);
7475 Log(("drvStatus_UnitChanged: iLUN=%d pLed=%p\n", iLUN, pLed));
7476 }
7477}
7478
7479
7480/**
7481 * Queries an interface to the driver.
7482 *
7483 * @returns Pointer to interface.
7484 * @returns NULL if the interface was not supported by the driver.
7485 * @param pInterface Pointer to this interface structure.
7486 * @param enmInterface The requested interface identification.
7487 */
7488DECLCALLBACK(void *) Console::drvStatus_QueryInterface(PPDMIBASE pInterface, PDMINTERFACE enmInterface)
7489{
7490 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
7491 PDRVMAINSTATUS pDrv = PDMINS_2_DATA(pDrvIns, PDRVMAINSTATUS);
7492 switch (enmInterface)
7493 {
7494 case PDMINTERFACE_BASE:
7495 return &pDrvIns->IBase;
7496 case PDMINTERFACE_LED_CONNECTORS:
7497 return &pDrv->ILedConnectors;
7498 default:
7499 return NULL;
7500 }
7501}
7502
7503
7504/**
7505 * Destruct a status driver instance.
7506 *
7507 * @returns VBox status.
7508 * @param pDrvIns The driver instance data.
7509 */
7510DECLCALLBACK(void) Console::drvStatus_Destruct(PPDMDRVINS pDrvIns)
7511{
7512 PDRVMAINSTATUS pData = PDMINS_2_DATA(pDrvIns, PDRVMAINSTATUS);
7513 LogFlowFunc(("iInstance=%d\n", pDrvIns->iInstance));
7514 if (pData->papLeds)
7515 {
7516 unsigned iLed = pData->iLastLUN - pData->iFirstLUN + 1;
7517 while (iLed-- > 0)
7518 ASMAtomicXchgPtr((void * volatile *)&pData->papLeds[iLed], NULL);
7519 }
7520}
7521
7522
7523/**
7524 * Construct a status driver instance.
7525 *
7526 * @copydoc FNPDMDRVCONSTRUCT
7527 */
7528DECLCALLBACK(int) Console::drvStatus_Construct(PPDMDRVINS pDrvIns, PCFGMNODE pCfgHandle, uint32_t fFlags)
7529{
7530 PDRVMAINSTATUS pData = PDMINS_2_DATA(pDrvIns, PDRVMAINSTATUS);
7531 LogFlowFunc(("iInstance=%d\n", pDrvIns->iInstance));
7532
7533 /*
7534 * Validate configuration.
7535 */
7536 if (!CFGMR3AreValuesValid(pCfgHandle, "papLeds\0First\0Last\0"))
7537 return VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES;
7538 AssertMsgReturn(PDMDrvHlpNoAttach(pDrvIns) == VERR_PDM_NO_ATTACHED_DRIVER,
7539 ("Configuration error: Not possible to attach anything to this driver!\n"),
7540 VERR_PDM_DRVINS_NO_ATTACH);
7541
7542 /*
7543 * Data.
7544 */
7545 pDrvIns->IBase.pfnQueryInterface = Console::drvStatus_QueryInterface;
7546 pData->ILedConnectors.pfnUnitChanged = Console::drvStatus_UnitChanged;
7547
7548 /*
7549 * Read config.
7550 */
7551 int rc = CFGMR3QueryPtr(pCfgHandle, "papLeds", (void **)&pData->papLeds);
7552 if (RT_FAILURE(rc))
7553 {
7554 AssertMsgFailed(("Configuration error: Failed to query the \"papLeds\" value! rc=%Rrc\n", rc));
7555 return rc;
7556 }
7557
7558 rc = CFGMR3QueryU32(pCfgHandle, "First", &pData->iFirstLUN);
7559 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
7560 pData->iFirstLUN = 0;
7561 else if (RT_FAILURE(rc))
7562 {
7563 AssertMsgFailed(("Configuration error: Failed to query the \"First\" value! rc=%Rrc\n", rc));
7564 return rc;
7565 }
7566
7567 rc = CFGMR3QueryU32(pCfgHandle, "Last", &pData->iLastLUN);
7568 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
7569 pData->iLastLUN = 0;
7570 else if (RT_FAILURE(rc))
7571 {
7572 AssertMsgFailed(("Configuration error: Failed to query the \"Last\" value! rc=%Rrc\n", rc));
7573 return rc;
7574 }
7575 if (pData->iFirstLUN > pData->iLastLUN)
7576 {
7577 AssertMsgFailed(("Configuration error: Invalid unit range %u-%u\n", pData->iFirstLUN, pData->iLastLUN));
7578 return VERR_GENERAL_FAILURE;
7579 }
7580
7581 /*
7582 * Get the ILedPorts interface of the above driver/device and
7583 * query the LEDs we want.
7584 */
7585 pData->pLedPorts = (PPDMILEDPORTS)pDrvIns->pUpBase->pfnQueryInterface(pDrvIns->pUpBase, PDMINTERFACE_LED_PORTS);
7586 if (!pData->pLedPorts)
7587 {
7588 AssertMsgFailed(("Configuration error: No led ports interface above!\n"));
7589 return VERR_PDM_MISSING_INTERFACE_ABOVE;
7590 }
7591
7592 for (unsigned i = pData->iFirstLUN; i <= pData->iLastLUN; ++i)
7593 Console::drvStatus_UnitChanged(&pData->ILedConnectors, i);
7594
7595 return VINF_SUCCESS;
7596}
7597
7598
7599/**
7600 * Keyboard driver registration record.
7601 */
7602const PDMDRVREG Console::DrvStatusReg =
7603{
7604 /* u32Version */
7605 PDM_DRVREG_VERSION,
7606 /* szDriverName */
7607 "MainStatus",
7608 /* pszDescription */
7609 "Main status driver (Main as in the API).",
7610 /* fFlags */
7611 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
7612 /* fClass. */
7613 PDM_DRVREG_CLASS_STATUS,
7614 /* cMaxInstances */
7615 ~0,
7616 /* cbInstance */
7617 sizeof(DRVMAINSTATUS),
7618 /* pfnConstruct */
7619 Console::drvStatus_Construct,
7620 /* pfnDestruct */
7621 Console::drvStatus_Destruct,
7622 /* pfnIOCtl */
7623 NULL,
7624 /* pfnPowerOn */
7625 NULL,
7626 /* pfnReset */
7627 NULL,
7628 /* pfnSuspend */
7629 NULL,
7630 /* pfnResume */
7631 NULL,
7632 /* pfnAttach */
7633 NULL,
7634 /* pfnDetach */
7635 NULL,
7636 /* pfnPowerOff */
7637 NULL,
7638 /* pfnSoftReset */
7639 NULL,
7640 /* u32EndVersion */
7641 PDM_DRVREG_VERSION
7642};
7643
7644/* vi: set tabstop=4 shiftwidth=4 expandtab: */
Note: See TracBrowser for help on using the repository browser.

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