VBoxHeadless.cpp revision 808fd59c426121c9864f0856c0ec500a55acd9fc
/* $Id$ */
/** @file
* VBoxHeadless - The VirtualBox Headless frontend for running VMs on servers.
*/
/*
* Copyright (C) 2006-2010 Oracle Corporation
*
* 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.
*/
using namespace com;
#define LOG_GROUP LOG_GROUP_GUI
#ifdef VBOX_WITH_VRDP
#endif
#include <iprt/buildconfig.h>
#include <iprt/initterm.h>
#include <VBox/VBoxVideo.h>
#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
#ifdef VBOX_WITH_VNC
# include "FramebufferVNC.h"
#endif
////////////////////////////////////////////////////////////////////////////////
do { \
RTPrintf("%s\n", 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 VirtualBox events
*/
class VirtualBoxCallback :
{
public:
{
#ifndef VBOX_WITH_XPCOM
refcnt = 0;
#endif
mfNoLoggedInUsers = true;
}
virtual ~VirtualBoxCallback()
{
}
#ifndef VBOX_WITH_XPCOM
{
return ::InterlockedIncrement(&refcnt);
}
{
if (cnt == 0)
delete this;
return cnt;
}
#endif
{
return VBOX_E_DONT_CALL_AGAIN;
}
{
return VBOX_E_DONT_CALL_AGAIN;
}
{
/* we never disagree */
if (!changeAllowed)
return E_INVALIDARG;
*changeAllowed = TRUE;
return VBOX_E_DONT_CALL_AGAIN;
}
{
return VBOX_E_DONT_CALL_AGAIN;
}
{
return VBOX_E_DONT_CALL_AGAIN;
}
{
return VBOX_E_DONT_CALL_AGAIN;
}
{
return VBOX_E_DONT_CALL_AGAIN;
}
{
return VBOX_E_DONT_CALL_AGAIN;
}
{
return VBOX_E_DONT_CALL_AGAIN;
}
{
return VBOX_E_DONT_CALL_AGAIN;
}
{
#ifdef VBOX_WITH_GUEST_PROPS
if (utf8Key == "/VirtualBox/GuestInfo/OS/NoLoggedInUsers")
{
/* Check if the "disconnect on logout feature" is enabled. */
if (gConsole)
{
{
{
}
}
}
{
if (utf8Value == "true")
{
if (!mfNoLoggedInUsers) /* Only if the property really changes. */
{
mfNoLoggedInUsers = true;
/* If there is a VRDP connection, drop it. */
{
{
{
}
}
}
}
}
else
{
mfNoLoggedInUsers = false;
}
}
}
return S_OK;
#else /* !VBOX_WITH_GUEST_PROPS */
return VBOX_E_DONT_CALL_AGAIN;
#endif /* !VBOX_WITH_GUEST_PROPS */
}
private:
#ifndef VBOX_WITH_XPCOM
long refcnt;
#endif
bool mfNoLoggedInUsers;
};
/**
* Callback handler for machine events.
*/
{
public:
{
#ifndef VBOX_WITH_XPCOM
refcnt = 0;
#endif
mLastVRDPPort = -1;
}
virtual ~ConsoleCallback() {}
#ifndef VBOX_WITH_XPCOM
{
return ::InterlockedIncrement(&refcnt);
}
{
if (cnt == 0)
delete this;
return cnt;
}
#endif
{
return VBOX_E_DONT_CALL_AGAIN;
}
STDMETHOD(OnMouseCapabilityChange)(BOOL supportsAbsolute, BOOL supportsRelative, BOOL needsHostCursor)
{
/* Emit absolute mouse event to actually enable the host mouse cursor. */
if (supportsAbsolute && gConsole)
{
if (mouse)
{
}
}
return S_OK;
}
{
return VBOX_E_DONT_CALL_AGAIN;
}
{
return S_OK;
}
{
return VBOX_E_DONT_CALL_AGAIN;
}
{
return VBOX_E_DONT_CALL_AGAIN;
}
{
return VBOX_E_DONT_CALL_AGAIN;
}
{
return VBOX_E_DONT_CALL_AGAIN;
}
{
return VBOX_E_DONT_CALL_AGAIN;
}
{
return VBOX_E_DONT_CALL_AGAIN;
}
{
#ifdef VBOX_WITH_VRDP
if (gConsole)
{
if (info)
{
if (port != mLastVRDPPort)
{
if (port == -1)
RTPrintf("VRDP server is inactive.\n");
else if (port == 0)
RTPrintf("VRDP server failed to start.\n");
else
}
}
}
#endif
return S_OK;
}
{
return VBOX_E_DONT_CALL_AGAIN;
}
{
return VBOX_E_DONT_CALL_AGAIN;
}
{
return VBOX_E_DONT_CALL_AGAIN;
}
{
return VBOX_E_DONT_CALL_AGAIN;
}
{
return VBOX_E_DONT_CALL_AGAIN;
}
{
return VBOX_E_DONT_CALL_AGAIN;
}
{
return VBOX_E_DONT_CALL_AGAIN;
}
{
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
long mLastVRDPPort;
};
#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_VNC
" -n, --vnc Enable the built in VNC server\n"
" -m, --vncport <port> TCP port number to use for the VNC server\n"
" -o, --vncpass <pw> Set the VNC server password\n"
#endif
#ifdef VBOX_WITH_VRDP
" -v, -vrdp, --vrdp on|off|config Enable (default) or disable the VRDP\n"
" server or don't change the setting\n"
" -p, -vrdpport, --vrdpport <ports> Comma-separated list of ports the VRDP\n"
" server can bind to. Use a dash between\n"
" two port numbers to specify a range\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;
const char *vrdpEnabled = NULL;
#endif
#ifdef VBOX_WITH_VNC
bool fVNCEnable = false;
unsigned uVNCPort = 0; /* default port */
#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 */
/* Make sure that DISPLAY is unset, so that X11 bits do not get initialised
* on X11-using OSes. */
/** @todo this should really be taken care of in Main. */
RTEnvUnset("DISPLAY");
LogFlow (("VBoxHeadless STARTED.\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 RTGETOPTDEF s_aOptions[] =
{
#ifdef VBOX_WITH_VRDP
#endif /* VBOX_WITH_VRDP defined */
#ifdef VBOX_WITH_VNC
{ "--vnc", 'n', 0 },
#endif /* VBOX_WITH_VNC */
{ "-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;
{
switch(ch)
{
case 's':
/* If the argument was not a UUID, then it must be a name. */
break;
#ifdef VBOX_WITH_VRDP
case 'p':
break;
case 'a':
break;
case 'v':
break;
#endif /* VBOX_WITH_VRDP defined */
#ifdef VBOX_WITH_VNC
case 'n':
fVNCEnable = true;
break;
case 'm':
break;
case 'o':
break;
#endif /* VBOX_WITH_VNC */
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 'r':
break;
case 'f':
break;
#endif /* VBOX_FFMPEG defined */
case 'h':
#ifdef VBOX_FFMPEG
{
break;
}
#endif
show_usage();
return 0;
case OPT_COMMENT:
/* nothing to do */
break;
case 'V':
return 0;
default:
show_usage();
return ch;
}
}
#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;
}
{
RTPrintf("VBoxHeadless: ERROR: failed to initialize COM!\n");
return 1;
}
bool fSessionOpened = false;
do
{
RTPrintf("VBoxHeadless: ERROR: failed to create the VirtualBox object!\n");
else
{
RTPrintf("VBoxHeadless: ERROR: failed to create a session object!\n");
}
{
{
RTPrintf("Most likely, the VirtualBox COM server is not running or failed to start.\n");
}
else
break;
}
/* find ID by name */
{
{
break;
}
break;
}
Log(("VBoxHeadless: Opening a session with machine (id={%s})...\n",
// open a session
fSessionOpened = true;
/* 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();
}
}
{
break;
}
#endif /* defined(VBOX_FFMPEG) */
#ifdef VBOX_WITH_VNC
if (fVNCEnable)
{
{
delete pFramebufferVNC;
break;
}
Log2(("VBoxHeadless: Registering VNC framebuffer\n"));
}
break;
#endif
#ifdef VBOX_WITH_VRDP
unsigned uScreenId;
{
#ifdef VBOX_FFMPEG
{
/* Already registered. */
continue;
}
#endif /* defined(VBOX_FFMPEG) */
if (!pVRDPFramebuffer)
{
break;
}
}
{
break;
}
#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;
}
}
/* initialize global references */
/* register a callback for machine events */
{
break;
}
#ifdef VBOX_WITH_VRDP
/* default is to enable the RDP server (backward compatibility) */
BOOL fVRDPEnable = true;
if (vrdpEnabled != NULL)
{
/* -vrdp on|off|config */
fVRDPEnable = false;
{
if (!fVRDPEnabled)
fVRDPEnable = false;
}
{
RTPrintf("-vrdp requires an argument (on|off|config)\n");
break;
}
}
if (fVRDPEnable)
{
Log(("VBoxHeadless: Enabling VRDP server...\n"));
/* set VRDP port if requested by the user */
{
}
/* set VRDP address if requested by the user */
if (vrdpAddress != NULL)
{
}
/* enable VRDP server (only if currently disabled) */
if (!fVRDPEnabled)
{
}
}
else
{
/* disable VRDP server (only if currently enabled */
if (fVRDPEnabled)
{
}
}
#endif
Log(("VBoxHeadless: Powering up the machine...\n"));
/* wait for result because there can be errors */
{
rc = progressRc;
if (FAILED(progressRc))
{
if (info.isBasicAvailable())
{
}
else
{
RTPrintf("Error: failed to start machine. No error message available!\n");
}
}
}
/* VirtualBox callback registration. */
vboxCallback = new VirtualBoxCallback();
vboxCallback->AddRef();
vboxCallback->Release();
break;
#endif
Log(("VBoxHeadless: Waiting for PowerDown...\n"));
Event *e;
while (gEventQ->waitForEvent(&e) && e)
gEventQ->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 */
}
while (0);
/* VirtualBox callback unregistration. */
if (vboxCallback)
{
vboxCallback->AddRef();
vboxCallback->Release();
}
/* No more access to the 'console' object, which will be uninitialized by the next session->Close call. */
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"));
}
/* Must be before com::Shutdown */
LogFlow(("VBoxHeadless FINISHED.\n"));
}
#ifndef VBOX_WITH_HARDENING
/**
* Main entry point.
*/
{
// initialize VBox Runtime
int rc = RTR3InitAndSUPLib();
if (RT_FAILURE(rc))
{
RTPrintf("VBoxHeadless: Runtime Error:\n"
switch (rc)
{
RTPrintf("Cannot access the kernel driver. Make sure the kernel module has been \n"
"loaded successfully. Aborting ...\n");
break;
default:
break;
}
return 1;
}
}
#endif /* !VBOX_WITH_HARDENING */