VBoxHeadless.cpp revision bbb95a0ab03c6fcb2ccdcfe8e89b04ca13bea7ab
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * VBoxHeadless - The VirtualBox Headless frontend for running VMs on servers.
e64031e20c39650a7bc902a3e1aba613b9415deevboxsync * Copyright (C) 2006-2012 Oracle Corporation
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * available from http://www.virtualbox.org. This file is free software;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * you can redistribute it and/or modify it under the terms of the GNU
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * General Public License (GPL) as published by the Free Software
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsyncusing namespace com;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync//#define VBOX_WITH_SAVESTATE_ON_SIGNAL
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync////////////////////////////////////////////////////////////////////////////////
2f0d866e126dd288169fed591c259c1c6b4016e5vboxsync Log(("VBoxHeadless: ERROR: " m " [rc=0x%08X]\n", rc)); \
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync } while (0)
ae5379e3e7573369566d4628ef6c597da693cc55vboxsync////////////////////////////////////////////////////////////////////////////////
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/* global weak references (for event handlers) */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync/* flag whether frontend should terminate */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsyncstatic volatile bool g_fTerminateFE = false;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync////////////////////////////////////////////////////////////////////////////////
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Handler for VirtualBoxClient events.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync STDMETHOD(HandleEvent)(VBoxEventType_T aType, IEvent *aEvent)
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync ComPtr<IVBoxSVCAvailabilityChangedEvent> pVSACEv = aEvent;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync LogRel(("VBoxHeadless: VBoxSVC became unavailable, exiting.\n"));
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync RTPrintf("VBoxSVC became unavailable, exiting.\n");
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync /* Terminate the VM as cleanly as possible given that VBoxSVC
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * is no longer present. */
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync * Handler for global events.
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync STDMETHOD(HandleEvent)(VBoxEventType_T aType, IEvent *aEvent)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if (aKey == Bstr("/VirtualBox/GuestInfo/OS/NoLoggedInUsers"))
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync /* Check if this is our machine and the "disconnect on logout feature" is enabled. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync hrc = gConsole->COMGETTER(Machine)(machine.asOutParam());
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync gpcev->COMGETTER(MachineId)(machineId.asOutParam());
39592d8ff3243f6116c4e99be391bcf30a4ad187vboxsync hrc = machine->GetExtraData(Bstr("VRDP/DisconnectOnGuestLogout").raw(),
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if (!mfNoLoggedInUsers) /* Only if the property really changes. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /* Guest property got deleted due to reset,
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * so it has no value anymore. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /* Guest property got deleted due to reset,
c77e7bff89c7639353778366984d51ff165ea0e3vboxsync * take the shortcut without touching the mfNoLoggedInUsers
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * state. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /* If there is a connection, drop it. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync hrc = gConsole->COMGETTER(VRDEServerInfo)(info.asOutParam());
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync hrc = machine->COMGETTER(VRDEServer)(vrdeServer.asOutParam());
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync LogRel(("VRDE: the guest user has logged out, disconnecting remote clients.\n"));
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Handler for machine events.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync STDMETHOD(HandleEvent)(VBoxEventType_T aType, IEvent *aEvent)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync ComPtr<IMouseCapabilityChangedEvent> mccev = aEvent;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync mccev->COMGETTER(SupportsAbsolute)(&fSupportsAbsolute);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /* Emit absolute mouse event to actually enable the host mouse cursor. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync mouse->PutMouseEventAbsolute(-1, -1, 0, 0 /* Horizontal wheel */, 0);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /* Terminate any event wait operation if the machine has been
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if (machineState < MachineState_Running && !m_fIgnorePowerOffEvents)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync ComPtr<IVRDEServerInfoChangedEvent> rdicev = aEvent;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync gConsole->COMGETTER(VRDEServerInfo)(info.asOutParam());
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync else if (port == 0)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync RTPrintf("VRDE server is listening on port %d.\n", port);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsynctypedef ListenerImpl<VirtualBoxClientEventListener> VirtualBoxClientEventListenerImpl;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsynctypedef ListenerImpl<VirtualBoxEventListener> VirtualBoxEventListenerImpl;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsynctypedef ListenerImpl<ConsoleEventListener> ConsoleEventListenerImpl;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsyncVBOX_LISTENER_DECLARE(VirtualBoxClientEventListenerImpl)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/** @todo Deal with nested signals, multithreaded signal dispatching (esp. on windows),
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * and multiple signals (both SIGINT and SIGTERM in some order).
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Consider processing the signal request asynchronously since there are lots of things
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * which aren't safe (like RTPrintf and printf IIRC) in a signal context. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync HRESULT rc = gConsole->SaveState(progress.asOutParam());
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync RTPrintf("-- Error saving state, lrc=%d (%#x)\n", lrc, lrc);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#endif /* VBOX_WITH_SAVESTATE_ON_SIGNAL */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync////////////////////////////////////////////////////////////////////////////////
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync " -s, -startvm, --startvm <name|uuid> Start given VM (required argument)\n"
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync " -v, -vrde, --vrde on|off|config Enable (default) or disable the VRDE\n"
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync " server or don't change the setting\n"
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync " -e, -vrdeproperty, --vrdeproperty <name=[value]> Set a VRDE property:\n"
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync " \"TCP/Ports\" - comma-separated list of ports\n"
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync " the VRDE server can bind to. Use a dash between\n"
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync " two port numbers to specify a range\n"
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync " \"TCP/Address\" - interface IP the VRDE server\n"
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync " will bind to\n"
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync " -c, -capture, --capture Record the VM screen output to a file\n"
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync " -w, --width Frame width when recording\n"
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync " -h, --height Frame height when recording\n"
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync " -r, --bitrate Recording bit rate when recording\n"
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync " -f, --filename File name when recording. The codec\n"
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync " used will be chosen based on the\n"
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync " file extension\n"
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Parse the environment for variables which can influence the FFMPEG settings.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * purely for backwards compatibility.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param pulFrameWidth may be updated with a desired frame width
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param pulFrameHeight may be updated with a desired frame height
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param pulBitRate may be updated with a desired bit rate
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param ppszFileName may be updated with a desired file name
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsyncstatic void parse_environ(unsigned long *pulFrameWidth, unsigned long *pulFrameHeight,
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync unsigned long *pulBitRate, const char **ppszFileName)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if ((pszEnvTemp = RTEnvGet("VBOX_CAPTUREWIDTH")) != 0)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync unsigned long ulFrameWidth = strtoul(pszEnvTemp, 0, 10);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync LogError("VBoxHeadless: ERROR: invalid VBOX_CAPTUREWIDTH environment variable", 0);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if ((pszEnvTemp = RTEnvGet("VBOX_CAPTUREHEIGHT")) != 0)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync unsigned long ulFrameHeight = strtoul(pszEnvTemp, 0, 10);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync LogError("VBoxHeadless: ERROR: invalid VBOX_CAPTUREHEIGHT environment variable", 0);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if ((pszEnvTemp = RTEnvGet("VBOX_CAPTUREBITRATE")) != 0)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync unsigned long ulBitRate = strtoul(pszEnvTemp, 0, 10);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync LogError("VBoxHeadless: ERROR: invalid VBOX_CAPTUREBITRATE environment variable", 0);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if ((pszEnvTemp = RTEnvGet("VBOX_CAPTUREFILE")) != 0)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#endif /* VBOX_WITH_VIDEO_REC defined */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync// Required for ATL
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Entry point.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsyncextern "C" DECLEXPORT(int) TrustedMain(int argc, char **argv, char **envp)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync unsigned fRawR0 = ~0U;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync unsigned fRawR3 = ~0U;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync unsigned fPATM = ~0U;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync unsigned fCSAM = ~0U;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync unsigned fFFMPEG = 0;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#endif /* VBOX_WITH_VIDEO_REC */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync RTPrintf (VBOX_PRODUCT " Headless Interface " VBOX_VERSION_STRING "\n"
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync "All rights reserved.\n\n");
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /* Parse the environment */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync parse_environ(&ulFrameWidth, &ulFrameHeight, &ulBitRate, &pszFileNameParam);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync { "-vrdpport", 'p', RTGETOPT_REQ_STRING }, /* VRDE: deprecated. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync { "--vrdpport", 'p', RTGETOPT_REQ_STRING }, /* VRDE: deprecated. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync { "-vrdpaddress", 'a', RTGETOPT_REQ_STRING }, /* VRDE: deprecated. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync { "--vrdpaddress", 'a', RTGETOPT_REQ_STRING }, /* VRDE: deprecated. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync { "-vrdp", 'v', RTGETOPT_REQ_STRING }, /* VRDE: deprecated. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync { "--vrdp", 'v', RTGETOPT_REQ_STRING }, /* VRDE: deprecated. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync { "--height", 'h', RTGETOPT_REQ_UINT32 }, /* great choice of short option! */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#endif /* VBOX_WITH_VIDEO_REC defined */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync // parse the command line
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1, 0 /* fFlags */);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync RTPrintf("Warning: '-p' or '-vrdpport' are deprecated. Use '-e \"TCP/Ports=%s\"'\n", ValueUnion.psz);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync RTPrintf("Warning: '-a' or '-vrdpaddress' are deprecated. Use '-e \"TCP/Address=%s\"'\n", ValueUnion.psz);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if (cVRDEProperties < RT_ELEMENTS(aVRDEProperties))
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync aVRDEProperties[cVRDEProperties++] = ValueUnion.psz;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync RTPrintf("Warning: too many VRDE properties. Ignored: '%s'\n", ValueUnion.psz);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#endif /* VBOX_WITH_VIDEO_REC defined */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if ((GetState.pDef->fFlags & RTGETOPT_REQ_MASK) != RTGETOPT_REQ_NOTHING)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /* nothing to do */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync RTPrintf("%sr%s\n", RTBldCfgVersion(), RTBldCfgRevisionStr());
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if (ulFrameWidth < 512 || ulFrameWidth > 2048 || ulFrameWidth % 2)
ad27e1d5e48ca41245120c331cc88b50464813cevboxsync LogError("VBoxHeadless: ERROR: please specify an even frame width between 512 and 2048", 0);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if (ulFrameHeight < 384 || ulFrameHeight > 1536 || ulFrameHeight % 2)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync LogError("VBoxHeadless: ERROR: please specify an even frame height between 384 and 1536", 0);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync LogError("VBoxHeadless: ERROR: please specify an even bitrate between 300000 and 1000000", 0);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /* Make sure we only have %d or %u (or none) in the file name specified */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync char *pcPercent = (char*)strchr(pszFileNameParam, '%');
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if (pcPercent != 0 && *(pcPercent + 1) != 'd' && *(pcPercent + 1) != 'u')
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync LogError("VBoxHeadless: ERROR: Only %%d and %%u are allowed in the capture file name.", -1);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /* And no more than one % in the name */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if (pcPercent != 0 && strchr(pcPercent + 1, '%') != 0)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync LogError("VBoxHeadless: ERROR: Only one format modifier is allowed in the capture file name.", -1);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync RTStrPrintf(&pszMPEGFile[0], RTPATH_MAX, pszFileNameParam, RTProcSelf());
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#endif /* defined VBOX_WITH_VIDEO_REC */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync com::GetVBoxUserHomeDirectory(szHome, sizeof(szHome));
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync RTPrintf("Failed to initialize COM because the global settings directory '%s' is not accessible!", szHome);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync RTPrintf("VBoxHeadless: ERROR: failed to initialize COM!\n");
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync ComObjPtr<ConsoleEventListenerImpl> consoleListener;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync rc = pVirtualBoxClient.createInprocObject(CLSID_VirtualBoxClient);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync RTPrintf("VBoxHeadless: ERROR: failed to create the VirtualBoxClient object!\n");
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync if (!info.isFullAvailable() && !info.isBasicAvailable())
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync RTPrintf("Most likely, the VirtualBox COM server is not running or failed to start.\n");
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync rc = pVirtualBoxClient->COMGETTER(VirtualBox)(virtualBox.asOutParam());
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync RTPrintf("Failed to get VirtualBox object (rc=%Rhrc)!\n", rc);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync rc = pVirtualBoxClient->COMGETTER(Session)(session.asOutParam());
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync RTPrintf("Failed to get session object (rc=%Rhrc)!\n", rc);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync rc = virtualBox->FindMachine(Bstr(pcszNameOrUUID).raw(), m.asOutParam());
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync Log(("VBoxHeadless: Opening a session with machine (id={%s})...\n",
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync // open a session
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync CHECK_ERROR_BREAK(m, LockMachine(session, LockType_Write));
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync /* get the console */
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync CHECK_ERROR_BREAK(session, COMGETTER(Console)(console.asOutParam()));
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync /* get the mutable machine */
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync CHECK_ERROR_BREAK(console, COMGETTER(Machine)(machine.asOutParam()));
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync CHECK_ERROR_BREAK(console, COMGETTER(Display)(display.asOutParam()));
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync Log2(("VBoxHeadless: loading VBoxFFmpegFB and libvpx shared library\n"));
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync rrc = SUPR3HardenedLdrLoadAppPriv("VBoxFFmpegFB", &hLdrFFmpegFB, RTLDRLOAD_FLAGS_LOCAL, &ErrInfo.Core);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync Log2(("VBoxHeadless: looking up symbol VBoxRegisterFFmpegFB\n"));
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync rrc = RTLdrGetSymbol(hLdrFFmpegFB, "VBoxRegisterFFmpegFB",
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync reinterpret_cast<void **>(&pfnRegisterFFmpegFB));
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync LogError("Failed to load the video capture extension, possibly due to a damaged file\n", rrc);
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync LogError("Failed to load the video capture extension\n", rrc); /** @todo stupid function, no formatting options. */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync Log2(("VBoxHeadless: calling pfnRegisterFFmpegFB\n"));
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync rcc = pfnRegisterFFmpegFB(ulFrameWidth, ulFrameHeight, ulBitRate,
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync LogError("Failed to initialise video capturing - make sure that the file format\n"
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync "you wish to use is supported on your system\n", rcc);
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync display->SetFramebuffer(VBOX_VIDEO_PRIMARY_SCREEN, pFramebuffer);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync#endif /* defined(VBOX_WITH_VIDEO_REC) */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync for (uScreenId = 0; uScreenId < cMonitors; uScreenId++)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /* Already registered. */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync VRDPFramebuffer *pVRDPFramebuffer = new VRDPFramebuffer();
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync RTPrintf("Error: could not create framebuffer object %d\n", uScreenId);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync display->SetFramebuffer(uScreenId, pVRDPFramebuffer);
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync // fill in remaining slots with null framebuffers
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync for (uScreenId = 0; uScreenId < cMonitors; uScreenId++)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /* get the machine debugger (isn't necessarily available) */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync console->COMGETTER(Debugger)(machineDebugger.asOutParam());
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync RTPrintf("Error: No debugger object; -%srawr0 cannot be executed!\n", fRawR0 ? "" : "no");
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync machineDebugger->COMSETTER(RecompileSupervisor)(!fRawR0);
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync RTPrintf("Error: No debugger object; -%srawr3 cannot be executed!\n", fRawR3 ? "" : "no");
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync machineDebugger->COMSETTER(RecompileUser)(!fRawR3);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync RTPrintf("Error: No debugger object; -%spatm cannot be executed!\n", fPATM ? "" : "no");
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync RTPrintf("Error: No debugger object; -%scsam cannot be executed!\n", fCSAM ? "" : "no");
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /* initialize global references */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /* VirtualBoxClient events registration. */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync CHECK_ERROR(pVirtualBoxClient, COMGETTER(EventSource)(pES.asOutParam()));
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync ComObjPtr<VirtualBoxClientEventListenerImpl> listener;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync listener->init(new VirtualBoxClientEventListener());
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync eventTypes.push_back(VBoxEventType_OnVBoxSVCAvailabilityChanged);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync CHECK_ERROR(pES, RegisterListener(vboxClientListener, ComSafeArrayAsInParam(eventTypes), true));
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /* Console events registration. */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync CHECK_ERROR(console, COMGETTER(EventSource)(es.asOutParam()));
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync eventTypes.push_back(VBoxEventType_OnMouseCapabilityChanged);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync eventTypes.push_back(VBoxEventType_OnStateChanged);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync eventTypes.push_back(VBoxEventType_OnVRDEServerInfoChanged);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync eventTypes.push_back(VBoxEventType_OnCanShowWindow);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync CHECK_ERROR(es, RegisterListener(consoleListener, ComSafeArrayAsInParam(eventTypes), true));
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /* default is to enable the remote desktop server (backward compatibility) */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync CHECK_ERROR_BREAK(machine, COMGETTER(VRDEServer)(vrdeServer.asOutParam()));
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync CHECK_ERROR_BREAK(vrdeServer, COMGETTER(Enabled)(&fVRDEEnabled));
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /* -vrdeServer on|off|config */
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync if (!strcmp(vrdeEnabled, "off") || !strcmp(vrdeEnabled, "disable"))
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync else if (strcmp(vrdeEnabled, "on") && strcmp(vrdeEnabled, "enable"))
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync RTPrintf("-vrdeServer requires an argument (on|off|config)\n");
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /* set VRDE port if requested by the user */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync CHECK_ERROR_BREAK(vrdeServer, SetVRDEProperty(Bstr("TCP/Ports").raw(), bstr.raw()));
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /* set VRDE address if requested by the user */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync CHECK_ERROR_BREAK(vrdeServer, SetVRDEProperty(Bstr("TCP/Address").raw(), Bstr(vrdeAddress).raw()));
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /* Set VRDE properties. */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync for (unsigned i = 0; i < cVRDEProperties; i++)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /* Parse 'name=value' */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync CHECK_ERROR_BREAK(vrdeServer, SetVRDEProperty(bstrName.raw(), bstrValue.raw()));
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync RTPrintf("Error: Invalid VRDE property '%s'\n", aVRDEProperties[i]);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync RTPrintf("Error: Failed to allocate memory for VRDE property '%s'\n", aVRDEProperties[i]);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /* enable VRDE server (only if currently disabled) */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync CHECK_ERROR_BREAK(vrdeServer, COMSETTER(Enabled)(TRUE));
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /* disable VRDE server (only if currently enabled */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync CHECK_ERROR_BREAK(vrdeServer, COMSETTER(Enabled)(FALSE));
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /* Disable the host clipboard before powering up */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync Log(("VBoxHeadless: Powering up the machine...\n"));
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync CHECK_ERROR_BREAK(console, PowerUp(progress.asOutParam()));
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Wait for the result because there can be errors.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * It's vital to process events while waiting (teleportation deadlocks),
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * so we'll poll for the completion instead of waiting on it.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync /* Process pending events, then wait for new ones. Note, this
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * processes NULL events signalling event loop termination. */
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync /* Figure out if the operation completed with a failed status
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * and print the error message. Terminate immediately, and let
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * the cleanup code take care of potentially pending events. */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync RTPrintf("Error: failed to start machine. Error message: %ls\n", info.getText().raw());
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync RTPrintf("Error: failed to start machine. No error message available!\n");
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /* VirtualBox events registration. */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync CHECK_ERROR(virtualBox, COMGETTER(EventSource)(es.asOutParam()));
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync eventTypes.push_back(VBoxEventType_OnGuestPropertyChanged);
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync CHECK_ERROR(es, RegisterListener(vboxListener, ComSafeArrayAsInParam(eventTypes), true));
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync && RT_SUCCESS(gEventQ->processEventQueue(RT_INDEFINITE_WAIT)))
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /* nothing */ ;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync Log(("VBoxHeadless: event loop has terminated...\n"));
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync#endif /* defined(VBOX_WITH_VIDEO_REC) */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /* we don't have to disable VRDE here because we don't save the settings of the VM */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Get the machine state.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync MachineState_T machineState = MachineState_Aborted;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Turn off the VM if it's running
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /** @todo power off paused VMs too? */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync consoleListener->getWrapped()->ignorePowerOffEvents(true);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync CHECK_ERROR_BREAK(gConsole, PowerDown(pProgress.asOutParam()));
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync CHECK_ERROR_BREAK(pProgress, WaitForCompletion(-1));
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync CHECK_ERROR_BREAK(pProgress, COMGETTER(Completed)(&completed));
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync CHECK_ERROR_BREAK(pProgress, COMGETTER(ResultCode)(&hrc));
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync RTPrintf("VBoxHeadless: ERROR: Failed to power down VM!");
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync if (!info.isFullAvailable() && !info.isBasicAvailable())
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync } while (0);
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync /* VirtualBox callback unregistration. */
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync CHECK_ERROR(virtualBox, COMGETTER(EventSource)(es.asOutParam()));
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /* Console callback unregistration. */
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync CHECK_ERROR(gConsole, COMGETTER(EventSource)(es.asOutParam()));
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync CHECK_ERROR(es, UnregisterListener(consoleListener));
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync /* VirtualBoxClient callback unregistration. */
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync CHECK_ERROR(pVirtualBoxClient, COMGETTER(EventSource)(pES.asOutParam()));
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync CHECK_ERROR(pES, UnregisterListener(vboxClientListener));
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /* No more access to the 'console' object, which will be uninitialized by the next session->Close call. */
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * Close the session. This will also uninitialize the console and
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * unregister the callback we've registered before.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /* Must be before com::Shutdown */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Main entry point.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync // initialize VBox Runtime
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync int rc = RTR3InitExe(argc, &argv, RTR3INIT_FLAGS_SUPLIB);
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync RTPrintf("Cannot access the kernel driver. Make sure the kernel module has been \n"
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync "loaded successfully. Aborting ...\n");
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync#endif /* !VBOX_WITH_HARDENING */