VirtualBox

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

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

ConsoleImpl.cpp: no need to put parentheses around RT_SUCCESS.

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

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