VBoxServiceControl.cpp revision c55bf74b54ecdfb5ebc4e5d90b620d0fee31737e
/* $Id$ */
/** @file
* VBoxServiceControl - Host-driven Guest Control.
*/
/*
* Copyright (C) 2012-2013 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.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include <iprt/semaphore.h>
#include <VBox/VBoxGuestLib.h>
#include "VBoxServiceInternal.h"
#include "VBoxServiceControl.h"
#include "VBoxServiceUtils.h"
using namespace guestControl;
/*******************************************************************************
* Global Variables *
*******************************************************************************/
/** The control interval (milliseconds). */
static uint32_t g_uControlIntervalMS = 0;
/** The semaphore we're blocking our main control thread on. */
/** The VM session ID. Changes whenever the VM is restored or reset. */
static uint64_t g_idControlSession;
/** The guest control service client ID. */
static uint32_t g_uControlSvcClientID = 0;
/** How many started guest processes are kept into memory for supplying
* information to the host. Default is 256 processes. If 0 is specified,
* the maximum number of processes is unlimited. */
/** List of guest control session threads (VBOXSERVICECTRLSESSIONTHREAD).
* A guest session thread represents a forked guest session process
* of VBoxService. */
/** The local session object used for handling all session-related stuff.
* When using the legacy guest control protocol (< 2), this session runs
* under behalf of the VBoxService main process. On newer protocol versions
* each session is a forked version of VBoxService using the appropriate
* user credentials for opening a guest session. These forked sessions then
* are kept in VBOXSERVICECTRLSESSIONTHREAD structures. */
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
static void VBoxServiceControlShutdown(void);
/** @copydoc VBOXSERVICE::pfnPreInit */
static DECLCALLBACK(int) VBoxServiceControlPreInit(void)
{
#ifdef VBOX_WITH_GUEST_PROPS
/*
* Read the service options from the VM's guest properties.
* Note that these options can be overridden by the command line options later.
*/
if (RT_FAILURE(rc))
{
{
VBoxServiceVerbose(0, "Guest property service is not available, skipping\n");
rc = VINF_SUCCESS;
}
else
}
else
{
}
rc = VINF_SUCCESS;
return rc;
#else
/* Nothing to do here yet. */
return VINF_SUCCESS;
#endif
}
/** @copydoc VBOXSERVICE::pfnOption */
static DECLCALLBACK(int) VBoxServiceControlOption(const char **ppszShort, int argc, char **argv, int *pi)
{
int rc = -1;
if (ppszShort)
/* no short options */;
#ifdef DEBUG
{
rc = 0; /* Flag this command as parsed. */
}
{
rc = 0; /* Flag this command as parsed. */
}
#endif
return rc;
}
/** @copydoc VBOXSERVICE::pfnInit */
static DECLCALLBACK(int) VBoxServiceControlInit(void)
{
/*
* If not specified, find the right interval default.
* Then create the event sem to block on.
*/
if (!g_uControlIntervalMS)
g_uControlIntervalMS = 1000;
/* The status code is ignored as this information is not available with VBox < 3.2.10. */
/* Init session object. */
if (RT_SUCCESS(rc))
if (RT_SUCCESS(rc))
{
/* Init session thread list. */
}
else
{
/* If the service was not found, we disable this service without
causing VBoxService to fail. */
{
VBoxServiceVerbose(0, "Guest control service is not available\n");
}
else
}
return rc;
}
/** @copydoc VBOXSERVICE::pfnWorker */
{
/*
* Tell the control thread that it can continue
* spawning services.
*/
Assert(g_uControlSvcClientID > 0);
int rc = VINF_SUCCESS;
/* Allocate a scratch buffer for commands which also send
* payload data with them. */
/* Set default protocol version to 1. */
for (;;)
{
if (rc == VERR_TOO_MUCH_DATA)
{
VBoxServiceVerbose(4, "Message requires %ld parameters, but only 2 supplied -- retrying request (no error!)...\n", cParms);
}
else if (RT_FAILURE(rc))
VBoxServiceVerbose(3, "Getting host message failed with %Rrc\n", rc); /* VERR_GEN_IO_FAILURE seems to be normal if ran into timeout. */
if (RT_SUCCESS(rc))
{
/* Set number of parameters for current host context. */
/* Check for VM session change. */
if ( RT_SUCCESS(rc2)
&& (idNewSession != g_idControlSession))
{
/* Close all opened guest sessions -- all context IDs, sessions etc.
* are now invalid. */
}
switch (uMsg)
{
break;
case HOST_SESSION_CREATE:
break;
case HOST_SESSION_CLOSE:
break;
default:
{
/*
* Protocol v1 did not have support for (dedicated)
* guest sessions, so all actions need to be performed
* under behalf of VBoxService's main executable.
*
* The global session object then acts as a host for all
* started guest processes which bring all their
* credentials with them with the actual execution call.
*/
{
}
else
{
/*
* ... on newer protocols handling all other commands is
* up to the guest session fork of VBoxService, so just
* skip all not wanted messages here.
*/
}
break;
}
}
}
/* Do we need to shutdown? */
if ( *pfShutdown
{
break;
}
/* Let's sleep for a bit and let others run ... */
}
VBoxServiceVerbose(0, "Guest control service stopped\n");
/* Delete scratch buffer. */
if (pvScratchBuf)
return rc;
}
{
VBOXSERVICECTRLSESSIONSTARTUPINFO ssInfo = { 0 };
if (RT_SUCCESS(rc))
{
/* The session open call has the protocol version the host
* wants to use. So update the current protocol version with the one the
* host wants to use in subsequent calls. */
}
if (RT_FAILURE(rc))
{
/* Report back on failure. On success this will be done
* by the forked session thread. */
if (RT_FAILURE(rc2))
{
}
}
return rc;
}
{
if (RT_SUCCESS(rc))
{
rc = VERR_NOT_FOUND;
{
{
break;
}
}
#if 0
if (RT_FAILURE(rc))
{
/* Report back on failure. On success this will be done
* by the forked session thread. */
if (RT_FAILURE(rc2))
{
if (RT_SUCCESS(rc))
}
}
#endif
uSessionID, rc);
}
else
return rc;
}
/** @copydoc VBOXSERVICE::pfnStop */
static DECLCALLBACK(void) VBoxServiceControlStop(void)
{
/** @todo Later, figure what to do if we're in RTProcWait(). It's a very
* annoying call since doesn't support timeouts in the posix world. */
if (g_hControlEvent != NIL_RTSEMEVENTMULTI)
/*
* Ask the host service to cancel all pending requests so that we can
* shutdown properly here.
*/
{
if (RT_FAILURE(rc))
}
}
/**
* Destroys all guest process threads which are still active.
*/
static void VBoxServiceControlShutdown(void)
{
0 /* Flags */);
if (RT_FAILURE(rc2))
if (RT_FAILURE(rc2))
}
/** @copydoc VBOXSERVICE::pfnTerm */
static DECLCALLBACK(void) VBoxServiceControlTerm(void)
{
if (g_hControlEvent != NIL_RTSEMEVENTMULTI)
{
}
}
/**
* The 'vminfo' service description.
*/
{
/* pszName. */
"control",
/* pszDescription. */
"Host-driven Guest Control",
/* pszUsage. */
#ifdef DEBUG
" [--control-dump-stderr] [--control-dump-stdout]\n"
#endif
" [--control-interval <ms>]\n"
" [--control-procs-mem-std[in|out|err] <KB>]"
,
/* pszOptions. */
#ifdef DEBUG
" --control-dump-stderr Dumps all guest proccesses stderr data to the\n"
" temporary directory.\n"
" --control-dump-stdout Dumps all guest proccesses stdout data to the\n"
" temporary directory.\n"
#endif
" --control-interval Specifies the interval at which to check for\n"
" new control commands. The default is 1000 ms.\n"
,
/* methods */
};