DECLCALLBACK(int) vmmdevVideoModeSupported(PPDMIVMMDEVCONNECTOR pInterface, uint32_t display, uint32_t width, uint32_t height, uint32_t bpp, bool *fSupported) { PDRVMAINVMMDEV pDrv = PDMIVMMDEVCONNECTOR_2_MAINVMMDEV(pInterface); Console *pConsole = pDrv->pVMMDev->getParent(); if (!fSupported) return VERR_INVALID_PARAMETER; #ifdef DEBUG_sunlover Log(("vmmdevVideoModeSupported: [%d]: %dx%dx%d\n", display, width, height, bpp)); #endif IFramebuffer *framebuffer = NULL; LONG xOrigin = 0; LONG yOrigin = 0; HRESULT hrc = pConsole->getDisplay()->GetFramebuffer(display, &framebuffer, &xOrigin, &yOrigin); if (SUCCEEDED(hrc) && framebuffer) { framebuffer->VideoModeSupported(width, height, bpp, (BOOL*)fSupported); framebuffer->Release(); } else { #ifdef DEBUG_sunlover Log(("vmmdevVideoModeSupported: hrc %x, framebuffer %p!!!\n", hrc, framebuffer)); #endif *fSupported = true; } return VINF_SUCCESS; }
DECLCALLBACK(int) vmmdevGetHeightReduction(PPDMIVMMDEVCONNECTOR pInterface, uint32_t *heightReduction) { PDRVMAINVMMDEV pDrv = PDMIVMMDEVCONNECTOR_2_MAINVMMDEV(pInterface); Console *pConsole = pDrv->pVMMDev->getParent(); if (!heightReduction) return VERR_INVALID_PARAMETER; IFramebuffer *framebuffer = pConsole->getDisplay()->getFramebuffer(); if (framebuffer) framebuffer->COMGETTER(HeightReduction)((ULONG*)heightReduction); else *heightReduction = 0; return VINF_SUCCESS; }
DECLCALLBACK(int) vmmdevGetHeightReduction(PPDMIVMMDEVCONNECTOR pInterface, uint32_t *heightReduction) { PDRVMAINVMMDEV pDrv = RT_FROM_MEMBER(pInterface, DRVMAINVMMDEV, Connector); Console *pConsole = pDrv->pVMMDev->getParent(); if (!heightReduction) return VERR_INVALID_PARAMETER; IFramebuffer *framebuffer = NULL; HRESULT hrc = pConsole->i_getDisplay()->QueryFramebuffer(0, &framebuffer); if (SUCCEEDED(hrc) && framebuffer) { framebuffer->COMGETTER(HeightReduction)((ULONG*)heightReduction); framebuffer->Release(); } else *heightReduction = 0; return VINF_SUCCESS; }
/** * Entry point. */ extern "C" DECLEXPORT(int) TrustedMain(int argc, char **argv, char **envp) { const char *vrdePort = NULL; const char *vrdeAddress = NULL; const char *vrdeEnabled = NULL; unsigned cVRDEProperties = 0; const char *aVRDEProperties[16]; unsigned fRawR0 = ~0U; unsigned fRawR3 = ~0U; unsigned fPATM = ~0U; unsigned fCSAM = ~0U; #ifdef VBOX_WITH_VIDEO_REC unsigned fVIDEOREC = 0; unsigned long ulFrameWidth = 800; unsigned long ulFrameHeight = 600; unsigned long ulBitRate = 300000; char pszMPEGFile[RTPATH_MAX]; const char *pszFileNameParam = "VBox-%d.vob"; #endif /* VBOX_WITH_VIDEO_REC */ LogFlow (("VBoxHeadless STARTED.\n")); RTPrintf (VBOX_PRODUCT " Headless Interface " VBOX_VERSION_STRING "\n" "(C) 2008-" VBOX_C_YEAR " " VBOX_VENDOR "\n" "All rights reserved.\n\n"); #ifdef VBOX_WITH_VIDEO_REC /* Parse the environment */ parse_environ(&ulFrameWidth, &ulFrameHeight, &ulBitRate, &pszFileNameParam); #endif enum eHeadlessOptions { OPT_RAW_R0 = 0x100, OPT_NO_RAW_R0, OPT_RAW_R3, OPT_NO_RAW_R3, OPT_PATM, OPT_NO_PATM, OPT_CSAM, OPT_NO_CSAM, OPT_SETTINGSPW, OPT_SETTINGSPW_FILE, OPT_COMMENT }; static const RTGETOPTDEF s_aOptions[] = { { "-startvm", 's', RTGETOPT_REQ_STRING }, { "--startvm", 's', RTGETOPT_REQ_STRING }, { "-vrdpport", 'p', RTGETOPT_REQ_STRING }, /* VRDE: deprecated. */ { "--vrdpport", 'p', RTGETOPT_REQ_STRING }, /* VRDE: deprecated. */ { "-vrdpaddress", 'a', RTGETOPT_REQ_STRING }, /* VRDE: deprecated. */ { "--vrdpaddress", 'a', RTGETOPT_REQ_STRING }, /* VRDE: deprecated. */ { "-vrdp", 'v', RTGETOPT_REQ_STRING }, /* VRDE: deprecated. */ { "--vrdp", 'v', RTGETOPT_REQ_STRING }, /* VRDE: deprecated. */ { "-vrde", 'v', RTGETOPT_REQ_STRING }, { "--vrde", 'v', RTGETOPT_REQ_STRING }, { "-vrdeproperty", 'e', RTGETOPT_REQ_STRING }, { "--vrdeproperty", 'e', RTGETOPT_REQ_STRING }, { "-rawr0", OPT_RAW_R0, 0 }, { "--rawr0", OPT_RAW_R0, 0 }, { "-norawr0", OPT_NO_RAW_R0, 0 }, { "--norawr0", OPT_NO_RAW_R0, 0 }, { "-rawr3", OPT_RAW_R3, 0 }, { "--rawr3", OPT_RAW_R3, 0 }, { "-norawr3", OPT_NO_RAW_R3, 0 }, { "--norawr3", OPT_NO_RAW_R3, 0 }, { "-patm", OPT_PATM, 0 }, { "--patm", OPT_PATM, 0 }, { "-nopatm", OPT_NO_PATM, 0 }, { "--nopatm", OPT_NO_PATM, 0 }, { "-csam", OPT_CSAM, 0 }, { "--csam", OPT_CSAM, 0 }, { "-nocsam", OPT_NO_CSAM, 0 }, { "--nocsam", OPT_NO_CSAM, 0 }, { "--settingspw", OPT_SETTINGSPW, RTGETOPT_REQ_STRING }, { "--settingspwfile", OPT_SETTINGSPW_FILE, RTGETOPT_REQ_STRING }, #ifdef VBOX_WITH_VIDEO_REC { "-capture", 'c', 0 }, { "--capture", 'c', 0 }, { "--width", 'w', RTGETOPT_REQ_UINT32 }, { "--height", 'h', RTGETOPT_REQ_UINT32 }, /* great choice of short option! */ { "--bitrate", 'r', RTGETOPT_REQ_UINT32 }, { "--filename", 'f', RTGETOPT_REQ_STRING }, #endif /* VBOX_WITH_VIDEO_REC defined */ { "-comment", OPT_COMMENT, RTGETOPT_REQ_STRING }, { "--comment", OPT_COMMENT, RTGETOPT_REQ_STRING } }; const char *pcszNameOrUUID = NULL; // parse the command line int ch; const char *pcszSettingsPw = NULL; const char *pcszSettingsPwFile = NULL; RTGETOPTUNION ValueUnion; RTGETOPTSTATE GetState; RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1, 0 /* fFlags */); while ((ch = RTGetOpt(&GetState, &ValueUnion))) { switch(ch) { case 's': pcszNameOrUUID = ValueUnion.psz; break; case 'p': RTPrintf("Warning: '-p' or '-vrdpport' are deprecated. Use '-e \"TCP/Ports=%s\"'\n", ValueUnion.psz); vrdePort = ValueUnion.psz; break; case 'a': RTPrintf("Warning: '-a' or '-vrdpaddress' are deprecated. Use '-e \"TCP/Address=%s\"'\n", ValueUnion.psz); vrdeAddress = ValueUnion.psz; break; case 'v': vrdeEnabled = ValueUnion.psz; break; case 'e': if (cVRDEProperties < RT_ELEMENTS(aVRDEProperties)) aVRDEProperties[cVRDEProperties++] = ValueUnion.psz; else RTPrintf("Warning: too many VRDE properties. Ignored: '%s'\n", ValueUnion.psz); break; case OPT_RAW_R0: fRawR0 = true; break; case OPT_NO_RAW_R0: fRawR0 = false; break; case OPT_RAW_R3: fRawR3 = true; break; case OPT_NO_RAW_R3: fRawR3 = false; break; case OPT_PATM: fPATM = true; break; case OPT_NO_PATM: fPATM = false; break; case OPT_CSAM: fCSAM = true; break; case OPT_NO_CSAM: fCSAM = false; break; case OPT_SETTINGSPW: pcszSettingsPw = ValueUnion.psz; break; case OPT_SETTINGSPW_FILE: pcszSettingsPwFile = ValueUnion.psz; break; #ifdef VBOX_WITH_VIDEO_REC case 'c': fVIDEOREC = true; break; case 'w': ulFrameWidth = ValueUnion.u32; break; case 'r': ulBitRate = ValueUnion.u32; break; case 'f': pszFileNameParam = ValueUnion.psz; break; #endif /* VBOX_WITH_VIDEO_REC defined */ case 'h': #ifdef VBOX_WITH_VIDEO_REC if ((GetState.pDef->fFlags & RTGETOPT_REQ_MASK) != RTGETOPT_REQ_NOTHING) { ulFrameHeight = ValueUnion.u32; break; } #endif show_usage(); return 0; case OPT_COMMENT: /* nothing to do */ break; case 'V': RTPrintf("%sr%s\n", RTBldCfgVersion(), RTBldCfgRevisionStr()); return 0; default: ch = RTGetOptPrintError(ch, &ValueUnion); show_usage(); return ch; } } #ifdef VBOX_WITH_VIDEO_REC if (ulFrameWidth < 512 || ulFrameWidth > 2048 || ulFrameWidth % 2) { LogError("VBoxHeadless: ERROR: please specify an even frame width between 512 and 2048", 0); return 1; } if (ulFrameHeight < 384 || ulFrameHeight > 1536 || ulFrameHeight % 2) { LogError("VBoxHeadless: ERROR: please specify an even frame height between 384 and 1536", 0); return 1; } if (ulBitRate < 300000 || ulBitRate > 1000000) { LogError("VBoxHeadless: ERROR: please specify an even bitrate between 300000 and 1000000", 0); return 1; } /* Make sure we only have %d or %u (or none) in the file name specified */ char *pcPercent = (char*)strchr(pszFileNameParam, '%'); if (pcPercent != 0 && *(pcPercent + 1) != 'd' && *(pcPercent + 1) != 'u') { LogError("VBoxHeadless: ERROR: Only %%d and %%u are allowed in the capture file name.", -1); return 1; } /* And no more than one % in the name */ if (pcPercent != 0 && strchr(pcPercent + 1, '%') != 0) { LogError("VBoxHeadless: ERROR: Only one format modifier is allowed in the capture file name.", -1); return 1; } RTStrPrintf(&pszMPEGFile[0], RTPATH_MAX, pszFileNameParam, RTProcSelf()); #endif /* defined VBOX_WITH_VIDEO_REC */ if (!pcszNameOrUUID) { show_usage(); return 1; } HRESULT rc; rc = com::Initialize(); #ifdef VBOX_WITH_XPCOM if (rc == NS_ERROR_FILE_ACCESS_DENIED) { char szHome[RTPATH_MAX] = ""; com::GetVBoxUserHomeDirectory(szHome, sizeof(szHome)); RTPrintf("Failed to initialize COM because the global settings directory '%s' is not accessible!", szHome); return 1; } #endif if (FAILED(rc)) { RTPrintf("VBoxHeadless: ERROR: failed to initialize COM!\n"); return 1; } ComPtr<IVirtualBoxClient> pVirtualBoxClient; ComPtr<IVirtualBox> virtualBox; ComPtr<ISession> session; ComPtr<IMachine> machine; bool fSessionOpened = false; ComPtr<IEventListener> vboxClientListener; ComPtr<IEventListener> vboxListener; ComObjPtr<ConsoleEventListenerImpl> consoleListener; do { rc = pVirtualBoxClient.createInprocObject(CLSID_VirtualBoxClient); if (FAILED(rc)) { RTPrintf("VBoxHeadless: ERROR: failed to create the VirtualBoxClient object!\n"); com::ErrorInfo info; if (!info.isFullAvailable() && !info.isBasicAvailable()) { com::GluePrintRCMessage(rc); RTPrintf("Most likely, the VirtualBox COM server is not running or failed to start.\n"); } else GluePrintErrorInfo(info); break; } rc = pVirtualBoxClient->COMGETTER(VirtualBox)(virtualBox.asOutParam()); if (FAILED(rc)) { RTPrintf("Failed to get VirtualBox object (rc=%Rhrc)!\n", rc); break; } rc = pVirtualBoxClient->COMGETTER(Session)(session.asOutParam()); if (FAILED(rc)) { RTPrintf("Failed to get session object (rc=%Rhrc)!\n", rc); break; } if (pcszSettingsPw) { CHECK_ERROR(virtualBox, SetSettingsSecret(Bstr(pcszSettingsPw).raw())); if (FAILED(rc)) break; } else if (pcszSettingsPwFile) { int rcExit = settingsPasswordFile(virtualBox, pcszSettingsPwFile); if (rcExit != RTEXITCODE_SUCCESS) break; } ComPtr<IMachine> m; rc = virtualBox->FindMachine(Bstr(pcszNameOrUUID).raw(), m.asOutParam()); if (FAILED(rc)) { LogError("Invalid machine name or UUID!\n", rc); break; } Bstr id; m->COMGETTER(Id)(id.asOutParam()); AssertComRC(rc); if (FAILED(rc)) break; Log(("VBoxHeadless: Opening a session with machine (id={%s})...\n", Utf8Str(id).c_str())); // open a session CHECK_ERROR_BREAK(m, LockMachine(session, LockType_VM)); fSessionOpened = true; /* get the console */ ComPtr<IConsole> console; CHECK_ERROR_BREAK(session, COMGETTER(Console)(console.asOutParam())); /* get the mutable machine */ CHECK_ERROR_BREAK(console, COMGETTER(Machine)(machine.asOutParam())); ComPtr<IDisplay> display; CHECK_ERROR_BREAK(console, COMGETTER(Display)(display.asOutParam())); #ifdef VBOX_WITH_VIDEO_REC IFramebuffer *pFramebuffer = 0; RTLDRMOD hLdrVideoRecFB; PFNREGISTERVIDEORECFB pfnRegisterVideoRecFB; if (fVIDEOREC) { HRESULT rcc = S_OK; int rrc = VINF_SUCCESS; RTERRINFOSTATIC ErrInfo; Log2(("VBoxHeadless: loading VBoxVideoRecFB and libvpx shared library\n")); RTErrInfoInitStatic(&ErrInfo); rrc = SUPR3HardenedLdrLoadAppPriv("VBoxVideoRecFB", &hLdrVideoRecFB, RTLDRLOAD_FLAGS_LOCAL, &ErrInfo.Core); if (RT_SUCCESS(rrc)) { Log2(("VBoxHeadless: looking up symbol VBoxRegisterVideoRecFB\n")); rrc = RTLdrGetSymbol(hLdrVideoRecFB, "VBoxRegisterVideoRecFB", reinterpret_cast<void **>(&pfnRegisterVideoRecFB)); if (RT_FAILURE(rrc)) LogError("Failed to load the video capture extension, possibly due to a damaged file\n", rrc); } else LogError("Failed to load the video capture extension\n", rrc); /** @todo stupid function, no formatting options. */ if (RT_SUCCESS(rrc)) { Log2(("VBoxHeadless: calling pfnRegisterVideoRecFB\n")); rcc = pfnRegisterVideoRecFB(ulFrameWidth, ulFrameHeight, ulBitRate, pszMPEGFile, &pFramebuffer); if (rcc != S_OK) LogError("Failed to initialise video capturing - make sure that the file format\n" "you wish to use is supported on your system\n", rcc); } if (RT_SUCCESS(rrc) && rcc == S_OK) { Log2(("VBoxHeadless: Registering framebuffer\n")); pFramebuffer->AddRef(); display->SetFramebuffer(VBOX_VIDEO_PRIMARY_SCREEN, pFramebuffer); } if (!RT_SUCCESS(rrc) || rcc != S_OK) rc = E_FAIL; } if (rc != S_OK) { break; } #endif /* defined(VBOX_WITH_VIDEO_REC) */ ULONG cMonitors = 1; machine->COMGETTER(MonitorCount)(&cMonitors); unsigned uScreenId; for (uScreenId = 0; uScreenId < cMonitors; uScreenId++) { # ifdef VBOX_WITH_VIDEO_REC if (fVIDEOREC && uScreenId == 0) { /* Already registered. */ continue; } # endif VRDPFramebuffer *pVRDPFramebuffer = new VRDPFramebuffer(); if (!pVRDPFramebuffer) { RTPrintf("Error: could not create framebuffer object %d\n", uScreenId); break; } pVRDPFramebuffer->AddRef(); display->SetFramebuffer(uScreenId, pVRDPFramebuffer); } if (uScreenId < cMonitors) { break; } // fill in remaining slots with null framebuffers for (uScreenId = 0; uScreenId < cMonitors; uScreenId++) { ComPtr<IFramebuffer> fb; LONG xOrigin, yOrigin; HRESULT hrc2 = display->GetFramebuffer(uScreenId, fb.asOutParam(), &xOrigin, &yOrigin); if (hrc2 == S_OK && fb.isNull()) { NullFB *pNullFB = new NullFB(); pNullFB->AddRef(); pNullFB->init(); display->SetFramebuffer(uScreenId, pNullFB); } } /* get the machine debugger (isn't necessarily available) */ ComPtr <IMachineDebugger> machineDebugger; console->COMGETTER(Debugger)(machineDebugger.asOutParam()); if (machineDebugger) { Log(("Machine debugger available!\n")); } if (fRawR0 != ~0U) { if (!machineDebugger) { RTPrintf("Error: No debugger object; -%srawr0 cannot be executed!\n", fRawR0 ? "" : "no"); break; } machineDebugger->COMSETTER(RecompileSupervisor)(!fRawR0); } if (fRawR3 != ~0U) { if (!machineDebugger) { RTPrintf("Error: No debugger object; -%srawr3 cannot be executed!\n", fRawR3 ? "" : "no"); break; } machineDebugger->COMSETTER(RecompileUser)(!fRawR3); } if (fPATM != ~0U) { if (!machineDebugger) { RTPrintf("Error: No debugger object; -%spatm cannot be executed!\n", fPATM ? "" : "no"); break; } machineDebugger->COMSETTER(PATMEnabled)(fPATM); } if (fCSAM != ~0U) { if (!machineDebugger) { RTPrintf("Error: No debugger object; -%scsam cannot be executed!\n", fCSAM ? "" : "no"); break; } machineDebugger->COMSETTER(CSAMEnabled)(fCSAM); } /* initialize global references */ gConsole = console; gEventQ = com::EventQueue::getMainEventQueue(); /* VirtualBoxClient events registration. */ { ComPtr<IEventSource> pES; CHECK_ERROR(pVirtualBoxClient, COMGETTER(EventSource)(pES.asOutParam())); ComObjPtr<VirtualBoxClientEventListenerImpl> listener; listener.createObject(); listener->init(new VirtualBoxClientEventListener()); vboxClientListener = listener; com::SafeArray<VBoxEventType_T> eventTypes; eventTypes.push_back(VBoxEventType_OnVBoxSVCAvailabilityChanged); CHECK_ERROR(pES, RegisterListener(vboxClientListener, ComSafeArrayAsInParam(eventTypes), true)); } /* Console events registration. */ { ComPtr<IEventSource> es; CHECK_ERROR(console, COMGETTER(EventSource)(es.asOutParam())); consoleListener.createObject(); consoleListener->init(new ConsoleEventListener()); com::SafeArray<VBoxEventType_T> eventTypes; eventTypes.push_back(VBoxEventType_OnMouseCapabilityChanged); eventTypes.push_back(VBoxEventType_OnStateChanged); eventTypes.push_back(VBoxEventType_OnVRDEServerInfoChanged); eventTypes.push_back(VBoxEventType_OnCanShowWindow); eventTypes.push_back(VBoxEventType_OnShowWindow); CHECK_ERROR(es, RegisterListener(consoleListener, ComSafeArrayAsInParam(eventTypes), true)); } /* default is to enable the remote desktop server (backward compatibility) */ BOOL fVRDEEnable = true; BOOL fVRDEEnabled; ComPtr <IVRDEServer> vrdeServer; CHECK_ERROR_BREAK(machine, COMGETTER(VRDEServer)(vrdeServer.asOutParam())); CHECK_ERROR_BREAK(vrdeServer, COMGETTER(Enabled)(&fVRDEEnabled)); if (vrdeEnabled != NULL) { /* -vrdeServer on|off|config */ if (!strcmp(vrdeEnabled, "off") || !strcmp(vrdeEnabled, "disable")) fVRDEEnable = false; else if (!strcmp(vrdeEnabled, "config")) { if (!fVRDEEnabled) fVRDEEnable = false; } else if (strcmp(vrdeEnabled, "on") && strcmp(vrdeEnabled, "enable")) { RTPrintf("-vrdeServer requires an argument (on|off|config)\n"); break; } } if (fVRDEEnable) { Log(("VBoxHeadless: Enabling VRDE server...\n")); /* set VRDE port if requested by the user */ if (vrdePort != NULL) { Bstr bstr = vrdePort; CHECK_ERROR_BREAK(vrdeServer, SetVRDEProperty(Bstr("TCP/Ports").raw(), bstr.raw())); } /* set VRDE address if requested by the user */ if (vrdeAddress != NULL) { CHECK_ERROR_BREAK(vrdeServer, SetVRDEProperty(Bstr("TCP/Address").raw(), Bstr(vrdeAddress).raw())); } /* Set VRDE properties. */ if (cVRDEProperties > 0) { for (unsigned i = 0; i < cVRDEProperties; i++) { /* Parse 'name=value' */ char *pszProperty = RTStrDup(aVRDEProperties[i]); if (pszProperty) { char *pDelimiter = strchr(pszProperty, '='); if (pDelimiter) { *pDelimiter = '\0'; Bstr bstrName = pszProperty; Bstr bstrValue = &pDelimiter[1]; CHECK_ERROR_BREAK(vrdeServer, SetVRDEProperty(bstrName.raw(), bstrValue.raw())); } else { RTPrintf("Error: Invalid VRDE property '%s'\n", aVRDEProperties[i]); RTStrFree(pszProperty); rc = E_INVALIDARG; break; } RTStrFree(pszProperty); } else { RTPrintf("Error: Failed to allocate memory for VRDE property '%s'\n", aVRDEProperties[i]); rc = E_OUTOFMEMORY; break; } } if (FAILED(rc)) break; } /* enable VRDE server (only if currently disabled) */ if (!fVRDEEnabled) { CHECK_ERROR_BREAK(vrdeServer, COMSETTER(Enabled)(TRUE)); } } else { /* disable VRDE server (only if currently enabled */ if (fVRDEEnabled) { CHECK_ERROR_BREAK(vrdeServer, COMSETTER(Enabled)(FALSE)); } } /* Disable the host clipboard before powering up */ console->COMSETTER(UseHostClipboard)(false); Log(("VBoxHeadless: Powering up the machine...\n")); ComPtr <IProgress> progress; CHECK_ERROR_BREAK(console, PowerUp(progress.asOutParam())); /* * Wait for the result because there can be errors. * * It's vital to process events while waiting (teleportation deadlocks), * so we'll poll for the completion instead of waiting on it. */ for (;;) { BOOL fCompleted; rc = progress->COMGETTER(Completed)(&fCompleted); if (FAILED(rc) || fCompleted) break; /* Process pending events, then wait for new ones. Note, this * processes NULL events signalling event loop termination. */ gEventQ->processEventQueue(0); if (!g_fTerminateFE) gEventQ->processEventQueue(500); } if (SUCCEEDED(progress->WaitForCompletion(-1))) { /* Figure out if the operation completed with a failed status * and print the error message. Terminate immediately, and let * the cleanup code take care of potentially pending events. */ LONG progressRc; progress->COMGETTER(ResultCode)(&progressRc); rc = progressRc; if (FAILED(rc)) { com::ProgressErrorInfo info(progress); if (info.isBasicAvailable()) { RTPrintf("Error: failed to start machine. Error message: %ls\n", info.getText().raw()); } else { RTPrintf("Error: failed to start machine. No error message available!\n"); } break; } } /* VirtualBox events registration. */ { ComPtr<IEventSource> es; CHECK_ERROR(virtualBox, COMGETTER(EventSource)(es.asOutParam())); ComObjPtr<VirtualBoxEventListenerImpl> listener; listener.createObject(); listener->init(new VirtualBoxEventListener()); vboxListener = listener; com::SafeArray<VBoxEventType_T> eventTypes; eventTypes.push_back(VBoxEventType_OnGuestPropertyChanged); /** * @todo Set the notification pattern to "/VirtualBox/GuestInfo/OS/ *Logged*" * to not cause too much load. The current API is broken as * IMachine::GuestPropertyNotificationPatterns() would change the * filter for _all_ clients. This is not what we want! */ CHECK_ERROR(es, RegisterListener(vboxListener, ComSafeArrayAsInParam(eventTypes), true)); } #ifdef VBOX_WITH_SAVESTATE_ON_SIGNAL signal(SIGINT, SaveState); signal(SIGTERM, SaveState); #endif Log(("VBoxHeadless: Waiting for PowerDown...\n")); while ( !g_fTerminateFE && RT_SUCCESS(gEventQ->processEventQueue(RT_INDEFINITE_WAIT))) /* nothing */ ; Log(("VBoxHeadless: event loop has terminated...\n")); #ifdef VBOX_WITH_VIDEO_REC if (pFramebuffer) { pFramebuffer->Release(); Log(("Released framebuffer\n")); pFramebuffer = NULL; } #endif /* defined(VBOX_WITH_VIDEO_REC) */ /* we don't have to disable VRDE here because we don't save the settings of the VM */ } while (0); /* * Get the machine state. */ MachineState_T machineState = MachineState_Aborted; if (!machine.isNull()) machine->COMGETTER(State)(&machineState); /* * Turn off the VM if it's running */ if ( gConsole && ( machineState == MachineState_Running || machineState == MachineState_Teleporting || machineState == MachineState_LiveSnapshotting /** @todo power off paused VMs too? */ ) ) do { consoleListener->getWrapped()->ignorePowerOffEvents(true); ComPtr<IProgress> pProgress; CHECK_ERROR_BREAK(gConsole, PowerDown(pProgress.asOutParam())); CHECK_ERROR_BREAK(pProgress, WaitForCompletion(-1)); BOOL completed; CHECK_ERROR_BREAK(pProgress, COMGETTER(Completed)(&completed)); ASSERT(completed); LONG hrc; CHECK_ERROR_BREAK(pProgress, COMGETTER(ResultCode)(&hrc)); if (FAILED(hrc)) { RTPrintf("VBoxHeadless: ERROR: Failed to power down VM!"); com::ErrorInfo info; if (!info.isFullAvailable() && !info.isBasicAvailable()) com::GluePrintRCMessage(hrc); else GluePrintErrorInfo(info); break; } } while (0); /* VirtualBox callback unregistration. */ if (vboxListener) { ComPtr<IEventSource> es; CHECK_ERROR(virtualBox, COMGETTER(EventSource)(es.asOutParam())); if (!es.isNull()) CHECK_ERROR(es, UnregisterListener(vboxListener)); vboxListener.setNull(); } /* Console callback unregistration. */ if (consoleListener) { ComPtr<IEventSource> es; CHECK_ERROR(gConsole, COMGETTER(EventSource)(es.asOutParam())); if (!es.isNull()) CHECK_ERROR(es, UnregisterListener(consoleListener)); consoleListener.setNull(); } /* VirtualBoxClient callback unregistration. */ if (vboxClientListener) { ComPtr<IEventSource> pES; CHECK_ERROR(pVirtualBoxClient, COMGETTER(EventSource)(pES.asOutParam())); if (!pES.isNull()) CHECK_ERROR(pES, UnregisterListener(vboxClientListener)); vboxClientListener.setNull(); } /* No more access to the 'console' object, which will be uninitialized by the next session->Close call. */ gConsole = NULL; if (fSessionOpened) { /* * Close the session. This will also uninitialize the console and * unregister the callback we've registered before. */ Log(("VBoxHeadless: Closing the session...\n")); session->UnlockMachine(); } /* Must be before com::Shutdown */ session.setNull(); virtualBox.setNull(); pVirtualBoxClient.setNull(); machine.setNull(); com::Shutdown(); LogFlow(("VBoxHeadless FINISHED.\n")); return FAILED(rc) ? 1 : 0; }