VirtualBox

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

Last change on this file since 25414 was 25346, checked in by vboxsync, 14 years ago

iprt/cpputils.h -> iprt/cpp/utils.h

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

© 2023 Oracle
ContactPrivacy policyTerms of Use