VBoxHeadless.cpp revision 27ff3359094d10df126ef7c1009920d3a94d5bb7
/** @file
*
* VBox frontends: VBoxHeadless (headless frontend):
* Headless server executable
*/
/*
* Copyright (C) 2006-2007 Sun Microsystems, Inc.
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
* Clara, CA 95054 USA or visit http://www.sun.com if you need
* additional information or have any questions.
*/
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
//#define VBOX_WITH_SAVESTATE_ON_SIGNAL
#include <signal.h>
#endif
#ifdef VBOX_WITH_VRDP
# include "Framebuffer.h"
#endif
////////////////////////////////////////////////////////////////////////////////
do { \
RTPrintf ("%s", m); \
} while (0)
////////////////////////////////////////////////////////////////////////////////
/* 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
{
/** @todo Deal with nested signals, multithreaded signal dispatching (esp. on windows),
* and multiple signals (both SIGINT and SIGTERM in some order).
* Consider processing the signal request asynchronously since there are lots of things
* which aren't safe (like RTPrintf and printf IIRC) in a signal context. */
RTPrintf("Signal received, saving state.\n");
{
return;
}
RTPrintf("0%%");
for (;;)
{
BOOL fCompleted = false;
break;
break;
{
}
/* wait */
}
lrc = ~0;
if (!lrc)
{
RTPrintf(" -- Saved the state successfully.\n");
}
else
}
#endif /* VBOX_WITH_SAVESTATE_ON_SIGNAL */
////////////////////////////////////////////////////////////////////////////////
static void show_usage()
{
RTPrintf("Usage:\n"
" -s, -startvm, --startvm <name|uuid> Start given VM (required argument)\n"
#ifdef VBOX_WITH_VRDP
" -p, -vrdpport, --vrdpport <port> Port number the VRDP server will bind\n"
" to\n"
" -a, -vrdpaddress, --vrdpaddress <ip> Interface IP the VRDP will bind to \n"
#endif
#ifdef VBOX_FFMPEG
" -c, -capture, --capture Record the VM screen output to a file\n"
" -w, --width Frame width when recording\n"
" -h, --height Frame height when recording\n"
" -r, --bitrate Recording bit rate when recording\n"
" -f, --filename File name when recording. The codec\n"
" used will be chosen based on the\n"
" file extension\n"
#endif
"\n");
}
#ifdef VBOX_FFMPEG
/**
* Parse the environment for variables which can influence the FFMPEG settings.
* purely for backwards compatibility.
* @param pulFrameWidth may be updated with a desired frame width
* @param pulFrameHeight may be updated with a desired frame height
* @param pulBitRate may be updated with a desired bit rate
* @param ppszFileName may be updated with a desired file name
*/
unsigned long *pulBitRate, const char **ppszFileName)
{
const char *pszEnvTemp;
{
errno = 0;
if (errno != 0)
LogError("VBoxHeadless: ERROR: invalid VBOX_CAPTUREWIDTH environment variable", 0);
else
}
{
errno = 0;
if (errno != 0)
LogError("VBoxHeadless: ERROR: invalid VBOX_CAPTUREHEIGHT environment variable", 0);
else
}
{
errno = 0;
if (errno != 0)
LogError("VBoxHeadless: ERROR: invalid VBOX_CAPTUREBITRATE environment variable", 0);
else
*pulBitRate = ulBitRate;
}
}
#endif /* VBOX_FFMPEG defined */
/**
* 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 long ulFrameWidth = 800;
unsigned long ulFrameHeight = 600;
unsigned long ulBitRate = 300000;
char pszMPEGFile[RTPATH_MAX];
const char *pszFileNameParam = "VBox-%d.vob";
#endif /* VBOX_FFMPEG */
// initialize VBox Runtime
LogFlow (("VBoxHeadless STARTED.\n"));
RTPrintf ("VirtualBox Headless Interface %s\n"
"(C) 2008 Sun Microsystems, Inc.\n"
"All rights reserved\n\n",
/* the below cannot be Bstr because on Linux Bstr doesn't work until XPCOM (nsMemory) is initialized */
#ifdef VBOX_FFMPEG
/* Parse the environment */
#endif
enum eHeadlessOptions
{
OPT_RAW_R0 = 0x100,
};
static const RTOPTIONDEF g_aOptions[] =
{
#ifdef VBOX_WITH_VRDP
#endif /* VBOX_WITH_VRDP defined */
{ "-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 },
#ifdef VBOX_FFMPEG
{ "-capture", 'c', 0 },
{ "--capture", 'c', 0 },
#endif /* VBOX_FFMPEG defined */
};
// parse the command line
int ch;
int i = 1;
{
if (ch < 0)
{
show_usage();
exit(-1);
}
switch(ch)
{
case 's':
/* If the argument was not a UUID, then it must be a name. */
if (!id)
break;
#ifdef VBOX_WITH_VRDP
case 'p':
break;
case 'a':
break;
#endif /* VBOX_WITH_VRDP defined */
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;
#ifdef VBOX_FFMPEG
case 'c':
fFFMPEG = true;
break;
case 'w':
break;
case 'h':
break;
case 'r':
break;
case 'f':
break;
#endif /* VBOX_FFMPEG defined */
default: /* comment */
break;
}
}
#ifdef VBOX_FFMPEG
{
LogError("VBoxHeadless: ERROR: please specify an even frame width between 512 and 2048", 0);
return -1;
}
{
LogError("VBoxHeadless: ERROR: please specify an even frame height between 384 and 1536", 0);
return -1;
}
{
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 */
{
return -1;
}
/* And no more than one % in the name */
{
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 (RT_SUCCESS(rrc))
{
Log2(("VBoxHeadless: looking up symbol VBoxRegisterFFmpegFB\n"));
reinterpret_cast<void **>(&pfnRegisterFFmpegFB));
if (RT_FAILURE(rrc))
}
else
if (RT_SUCCESS(rrc))
{
Log2(("VBoxHeadless: calling pfnRegisterFFmpegFB\n"));
LogError("Failed to initialise video capturing - make sure that the file format\n"
"you wish to use is supported on your system\n", rcc);
}
{
Log2(("VBoxHeadless: Registering framebuffer\n"));
pFramebuffer->AddRef();
}
}
{
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");
}
}
}
#endif
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;
}