VBoxHeadless.cpp revision 353a4d1d78e3be140fa5ac56fbde62a413cb34c5
/** @file
*
* VBox frontends: VBoxHeadless (headless frontend):
* Headless server executable
*/
/*
* Copyright (C) 2006-2007 innotek GmbH
*
* innotek GmbH confidential
* All rights reserved
*/
using namespace com;
#define LOG_GROUP LOG_GROUP_GUI
#ifdef VBOX_WITH_VRDP
#endif
#ifdef VBOX_FFMPEG
#include <cstdlib>
#include <cerrno>
#include "VBoxHeadless.h"
#endif
#ifdef VBOX_WITH_VRDP
# include "Framebuffer.h"
#endif
////////////////////////////////////////////////////////////////////////////////
if (1) { \
}
////////////////////////////////////////////////////////////////////////////////
/* global weak references (for event handlers) */
////////////////////////////////////////////////////////////////////////////////
/**
* State change event.
*/
class StateChangeEvent : public Event
{
public:
protected:
void *handler()
{
/* post the termination event if the machine has been PoweredDown/Saved/Aborted */
if (mState < MachineState_Running)
return 0;
}
private:
};
/**
* Callback handler for machine events.
*/
class ConsoleCallback : public IConsoleCallback
{
public:
{
#ifndef VBOX_WITH_XPCOM
refcnt = 0;
#endif
}
virtual ~ConsoleCallback() {}
#ifndef VBOX_WITH_XPCOM
{
return ::InterlockedIncrement(&refcnt);
}
{
if (cnt == 0)
delete this;
return cnt;
}
{
if (riid == IID_IUnknown)
{
*ppObj = this;
AddRef();
return S_OK;
}
if (riid == IID_IConsoleCallback)
{
*ppObj = this;
AddRef();
return S_OK;
}
return E_NOINTERFACE;
}
#endif
{
return S_OK;
}
{
/* Emit absolute mouse event to actually enable the host mouse cursor. */
if (supportsAbsolute && gConsole)
{
if (mouse)
{
}
}
return S_OK;
}
{
return S_OK;
}
{
return S_OK;
}
{
return S_OK;
}
{
return S_OK;
}
{
return S_OK;
}
{
return S_OK;
}
{
return S_OK;
}
{
return S_OK;
}
{
return S_OK;
}
{
return S_OK;
}
{
return S_OK;
}
{
return S_OK;
}
{
return S_OK;
}
{
return S_OK;
}
{
if (!canShow)
return E_POINTER;
/* Headless windows should not be shown */
return S_OK;
}
{
/* OnCanShowWindow() always returns FALSE, so this call should never
* happen. */
AssertFailed();
if (!winId)
return E_POINTER;
*winId = 0;
return E_NOTIMPL;
}
private:
#ifndef VBOX_WITH_XPCOM
long refcnt;
#endif
};
#ifdef VBOX_WITH_XPCOM
#endif
////////////////////////////////////////////////////////////////////////////////
static void show_usage()
{
RTPrintf("Usage:\n"
" -startvm <name|uuid> Start given VM (required argument)\n"
#ifdef VBOX_WITH_VRDP
" -vrdpport <port> Port number the VRDP server will bind to\n"
" -vrdpaddress <ip> Interface IP the VRDP will bind to \n"
#endif
#ifdef VBOX_FFMPEG
" -capture Record the VM screen output to a file\n"
"\n"
"When recording, the following optional environment variables are also\n"
"recognized:\n"
"\n"
" VBOX_CAPTUREWIDTH Frame width\n"
" VBOX_CAPTUREHEIGHT Frame height\n"
" VBOX_CAPTUREBITRATE Recording bit rate\n"
" VBOX_CAPTUREBITRATE Recording bit rate\n"
" VBOX_CAPTUREFILE Specify a file name\n"
#endif
"\n");
}
/**
* Entry point.
*/
{
#ifdef VBOX_WITH_VRDP
const char *vrdpAddress = NULL;
#endif
unsigned fRawR0 = ~0U;
unsigned fRawR3 = ~0U;
unsigned fPATM = ~0U;
unsigned fCSAM = ~0U;
#ifdef VBOX_FFMPEG
unsigned fFFMPEG = 0;
unsigned ulFrameWidth = 800;
unsigned ulFrameHeight = 600;
unsigned ulBitRate = 300000;
char pszMPEGFile[RTPATH_MAX];
#endif /* VBOX_FFMPEG */
// initialize VBox Runtime
LogFlow (("VBoxHeadless STARTED.\n"));
RTPrintf ("VirtualBox Headless Interface %s\n"
"(C) 2005-2007 innotek GmbH\n"
"All rights reserved\n\n",
/* the below cannot be Bstr because on Linux Bstr doesn't work until XPCOM (nsMemory) is initialized */
// parse the command line
{
{
{
LogError("VBoxHeadless: ERROR: missing VM identifier!", 0);
return -1;
}
/* If the argument was not a UUID, then it must be a name. */
if (!id)
{
}
}
#ifdef VBOX_WITH_VRDP
{
{
LogError("VBoxHeadless: ERROR: missing VRDP port value!", 0);
return -1;
}
}
{
{
LogError("VBoxHeadless: ERROR: missing VRDP address value!", 0);
return -1;
}
}
#endif
{
fRawR0 = true;
}
{
fRawR0 = false;
}
{
fRawR3 = true;
}
{
fRawR3 = false;
}
{
fPATM = true;
}
{
fPATM = false;
}
{
fCSAM = true;
}
{
fCSAM = false;
}
#ifdef VBOX_FFMPEG
{
fFFMPEG = true;
}
#endif
{
/* We could just ignore this, but check the syntax for consistency... */
{
LogError("VBoxHeadless: ERROR: missing comment!", 0);
return -1;
}
}
else
{
show_usage();
return -1;
}
}
#ifdef VBOX_FFMPEG
const char *pszEnvTemp;
{
errno = 0;
if (errno != 0)
{
LogError("VBoxHeadless: ERROR: invalid VBOX_CAPTUREWIDTH environment variable", 0);
return -1;
}
{
LogError("VBoxHeadless: ERROR: please specify an even VBOX_CAPTUREWIDTH variable between 512 and 2048", 0);
return -1;
}
}
{
errno = 0;
if (errno != 0)
{
LogError("VBoxHeadless: ERROR: invalid VBOX_CAPTUREHEIGHT environment variable", 0);
return -1;
}
{
LogError("VBoxHeadless: ERROR: please specify an even VBOX_CAPTUREHEIGHT variable between 384 and 1536", 0);
return -1;
}
}
{
errno = 0;
if (errno != 0)
{
LogError("VBoxHeadless: ERROR: invalid VBOX_CAPTUREBITRATE environment variable", 0);
return -1;
}
{
LogError("VBoxHeadless: ERROR: please specify an even VBOX_CAPTUREHEIGHT variable between 300000 and 1000000", 0);
return -1;
}
}
/* Standard base name */
pszEnvTemp = "VBox-%d.vob";
/* Make sure we only have %d or %u (or none) */
{
LogError("VBoxHeadless: ERROR: Only %%d and %%u are allowed in the VBOX_CAPTUREFILE parameter.", -1);
return -1;
}
/* And no more than one % in the name */
{
LogError("VBoxHeadless: ERROR: Only one format modifier is allowed in the VBOX_CAPTUREFILE parameter.", -1);
return -1;
}
#endif /* defined VBOX_FFMPEG */
{
show_usage();
return -1;
}
return rc;
do
{
/* create VirtualBox object */
{
if (info.isFullAvailable())
{
RTPrintf("Failed to create VirtualBox object! Error info: '%lS' (component %lS).\n",
}
else
break;
}
/* create Session object */
{
break;
}
/* find ID by name */
if (!id)
{
{
break;
}
AssertComRC (rc);
break;
}
Log (("VBoxHeadless: Opening a session with machine (id={%s})...\n",
// open a session
/* get the console */
/* get the machine */
#ifdef VBOX_FFMPEG
IFramebuffer *pFramebuffer = 0;
if (fFFMPEG)
{
Log2(("VBoxHeadless: loading VBoxFFmpegFB shared library\n"));
if (rc == VINF_SUCCESS)
{
Log2(("VBoxHeadless: looking up symbol VBoxRegisterFFmpegFB\n"));
reinterpret_cast<void **>(&pfnRegisterFFmpegFB));
if (rc == VINF_SUCCESS)
{
Log2(("VBoxHeadless: calling pfnRegisterFFmpegFB\n"));
{
Log2(("VBoxHeadless: Registering framebuffer\n"));
pFramebuffer->AddRef();
}
}
}
}
{
LogError ("Failed to load VBoxFFmpegFB shared library\n", 0);
return -1;
}
#endif /* defined(VBOX_FFMPEG) */
#ifdef VBOX_WITH_VRDP
unsigned uScreenId;
{
#ifdef VBOX_FFMPEG
{
/* Already registered. */
continue;
}
#endif /* defined(VBOX_FFMPEG) */
if (!pFramebuffer)
{
return -1;
}
pFramebuffer->AddRef();
}
#endif
/* get the machine debugger (isn't necessarily available) */
if (machineDebugger)
{
Log(("Machine debugger available!\n"));
}
if (fRawR0 != ~0U)
{
if (!machineDebugger)
{
break;
}
}
if (fRawR3 != ~0U)
{
if (!machineDebugger)
{
break;
}
}
if (fPATM != ~0U)
{
if (!machineDebugger)
{
break;
}
}
if (fCSAM != ~0U)
{
if (!machineDebugger)
{
break;
}
}
/* create an event queue */
/* initialize global references */
/* register a callback for machine events */
{
break;
}
#ifdef VBOX_WITH_VRDP
Log (("VBoxHeadless: Enabling VRDP server...\n"));
/* set VRDP port if requested by the user */
if (vrdpPort != ~0U)
else
/* set VRDP address if requested by the user */
if (vrdpAddress != NULL)
/* enable VRDP server (only if currently disabled) */
if (!fVRDPEnabled)
{
}
#endif
Log (("VBoxHeadless: Powering up the machine...\n"));
#ifdef VBOX_WITH_VRDP
#endif
/* wait for result because there can be errors */
{
{
if (info.isBasicAvailable())
{
}
else
{
RTPrintf("Error: failed to start machine. No error message available!\n");
}
}
}
Log (("VBoxHeadless: Waiting for PowerDown...\n"));
Event *e;
while (eventQ.waitForEvent (&e) && e)
eventQ.handleEvent (e);
Log (("VBoxHeadless: event loop has terminated...\n"));
#ifdef VBOX_FFMPEG
if (pFramebuffer)
{
pFramebuffer->Release ();
Log(("Released framebuffer\n"));
pFramebuffer = NULL;
}
#endif /* defined(VBOX_FFMPEG) */
/* we don't have to disable VRDP here because we don't save the settings of the VM */
/*
* Close the session. This will also uninitialize the console and
* unregister the callback we've registered before.
*/
Log (("VBoxHeadless: Closing the session...\n"));
}
while (0);
LogFlow (("VBoxHeadless FINISHED.\n"));
return rc;
}