service.cpp revision 1060aa8ee07bcf6ba69496ea2b97531b2cd15d8e
/* $Id$ */
/** @file
* Guest Control Service: Controlling the guest.
*/
/*
* Copyright (C) 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.
*/
/** @page pg_svc_guest_control Guest Control HGCM Service
*
* This service acts as a proxy for handling and buffering host command requests
* and clients on the guest. It tries to be as transparent as possible to let
* the guest (client) and host side do their protocol handling as desired.
*
* The following terms are used:
* - Host: A host process (e.g. VBoxManage or another tool utilizing the Main API)
* which wants to control something on the guest.
* - Client: A client (e.g. VBoxService) running inside the guest OS waiting for
* new host commands to perform. There can be multiple clients connected
* to a service. A client is represented by its HGCM client ID.
* - Context ID: A (almost) unique ID automatically generated on the host (Main API)
* to not only distinguish clients but individual requests. Because
* the host does not know anything about connected clients it needs
* an indicator which it can refer to later. This context ID gets
* internally bound by the service to a client which actually processes
* the command in order to have a relationship between client<->context ID(s).
*
* The host can trigger commands which get buffered by the service (with full HGCM
* parameter info). As soon as a client connects (or is ready to do some new work)
* it gets a buffered host command to process it. This command then will be immediately
* removed from the command list. If there are ready clients but no new commands to be
* processed, these clients will be set into a deferred state (that is being blocked
* to return until a new command is available).
*
* If a client needs to inform the host that something happend, it can send a
* message to a low level HGCM callback registered in Main. This callback contains
* the actual data as well as the context ID to let the host do the next necessary
* steps for this context. This context ID makes it possible to wait for an event
* inside the host's Main API function (like starting a process on the guest and
* wait for getting its PID returned by the client) as well as cancelling blocking
* host calls in order the client terminated/crashed (HGCM detects disconnected
* clients and reports it to this service's callback).
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP LOG_GROUP_HGCM
#include <memory> /* for auto_ptr */
#include <string>
#include <list>
#include "gctrl.h"
namespace guestControl {
/**
* Structure for holding all clients with their
* generated host contexts. This is necessary for
* mainting the relationship between a client and its context IDs.
*/
struct ClientContexts
{
/** This client ID. */
/** The list of contexts a client is assigned to. */
/** The normal contructor. */
};
/** The client list + iterator type */
/**
* Structure for holding an uncompleted guest call.
*/
struct ClientWaiter
{
/** Client ID; a client can have multiple handles! */
/** The call handle */
/** The call parameters */
/** Number of parameters */
/** The standard contructor. */
/** The normal contructor. */
};
/** The guest call list type */
/**
* Structure for holding a buffered host command.
*/
struct HostCmd
{
/** The context ID this command belongs to. Will be extracted
* from the HGCM parameters. */
/** Dynamic structure for holding the HGCM parms */
/** The standard contructor. */
HostCmd() : mContextID(0) {}
};
/** The host cmd list + iterator type */
/**
* Class containing the shared information service functionality.
*/
{
private:
/** Type definition for use in callback functions. */
/** HGCM helper functions. */
/*
* Callback function supplied by the host for notification of updates
* to properties.
*/
/** User data pointer to be supplied to the host callback function. */
void *mpvHostData;
/** The deferred calls list. */
/** The host command list. */
/** Client contexts list. */
public:
, mpvHostData(NULL)
{
}
/**
* @copydoc VBOXHGCMSVCHELPERS::pfnUnload
* Simply deletes the service object
*/
{
if (RT_SUCCESS(rc))
delete pSelf;
return rc;
}
/**
* @copydoc VBOXHGCMSVCHELPERS::pfnConnect
* Stub implementation of pfnConnect and pfnDisconnect.
*/
void *pvClient)
{
return rc;
}
/**
* @copydoc VBOXHGCMSVCHELPERS::pfnConnect
* Stub implementation of pfnConnect and pfnDisconnect.
*/
void *pvClient)
{
return rc;
}
/**
* @copydoc VBOXHGCMSVCHELPERS::pfnCall
* Wraps to the call member function
*/
void *pvClient,
{
LogFlowFunc (("pvService=%p, callHandle=%p, u32ClientID=%u, pvClient=%p, u32Function=%u, cParms=%u, paParms=%p\n", pvService, callHandle, u32ClientID, pvClient, u32Function, cParms, paParms));
LogFlowFunc (("returning\n"));
}
/**
* @copydoc VBOXHGCMSVCHELPERS::pfnHostCall
* Wraps to the hostCall member function
*/
{
LogFlowFunc (("pvService=%p, u32Function=%u, cParms=%u, paParms=%p\n", pvService, u32Function, cParms, paParms));
return rc;
}
/**
* @copydoc VBOXHGCMSVCHELPERS::pfnRegisterExtension
* Installs a host callback for notifications of property changes.
*/
void *pvExtension)
{
return VINF_SUCCESS;
}
private:
int paramBufferAllocate(PVBOXGUESTCTRPARAMBUFFER pBuf, uint32_t uMsg, uint32_t cParms, VBOXHGCMSVCPARM paParms[]);
int sendHostCmdToGuest(HostCmd *pCmd, VBOXHGCMCALLHANDLE callHandle, uint32_t cParms, VBOXHGCMSVCPARM paParms[]);
int retrieveNextHostCmd(uint32_t u32ClientID, VBOXHGCMCALLHANDLE callHandle, uint32_t cParms, VBOXHGCMSVCPARM paParms[]);
int uninit();
};
/** @todo Write some nice doc headers! */
/* Stores a HGCM request in an internal buffer (pEx). Needs to be freed later using execBufferFree(). */
int Service::paramBufferAllocate(PVBOXGUESTCTRPARAMBUFFER pBuf, uint32_t uMsg, uint32_t cParms, VBOXHGCMSVCPARM paParms[])
{
int rc = VINF_SUCCESS;
/* Paranoia. */
if (cParms > 256)
cParms = 256;
/*
* Don't verify anything here (yet), because this function only buffers
* the HGCM data into an internal structure and reaches it back to the guest (client)
* in an unmodified state.
*/
if (RT_SUCCESS(rc))
{
{
rc = VERR_NO_MEMORY;
}
else
{
{
{
case VBOX_HGCM_SVC_PARM_32BIT:
break;
case VBOX_HGCM_SVC_PARM_64BIT:
/* Not supported yet. */
break;
case VBOX_HGCM_SVC_PARM_PTR:
{
{
rc = VERR_NO_MEMORY;
break;
}
else
}
break;
default:
break;
}
if (RT_FAILURE(rc))
break;
}
}
}
return rc;
}
/* Frees a buffered HGCM request. */
{
{
{
case VBOX_HGCM_SVC_PARM_PTR:
break;
}
}
if (pBuf->uParmCount)
{
pBuf->uParmCount = 0;
}
}
/* Assigns data from a buffered HGCM request to the current HGCM request. */
int Service::paramBufferAssign(PVBOXGUESTCTRPARAMBUFFER pBuf, uint32_t cParms, VBOXHGCMSVCPARM paParms[])
{
int rc = VINF_SUCCESS;
{
}
else
{
/** @todo Add check to verify if the HGCM request is the same *type* as the buffered one! */
{
{
case VBOX_HGCM_SVC_PARM_32BIT:
break;
case VBOX_HGCM_SVC_PARM_64BIT:
/* Not supported yet. */
break;
case VBOX_HGCM_SVC_PARM_PTR:
break;
default:
break;
}
}
}
return rc;
}
{
return VINF_SUCCESS;
}
{
/*
* Throw out all stale clients.
*/
int rc = VINF_SUCCESS;
{
{
}
else
itCall++;
}
&& RT_SUCCESS(rc))
{
{
&& RT_SUCCESS(rc))
{
/*
* Notify the host that clients with u32ClientID are no longer
* around and need to be cleaned up (canceling waits etc).
*/
if (mpfnHostCallback)
{
if (RT_FAILURE(rc))
}
itContext++;
}
}
else
it++;
}
return rc;
}
int Service::sendHostCmdToGuest(HostCmd *pCmd, VBOXHGCMCALLHANDLE callHandle, uint32_t cParms, VBOXHGCMSVCPARM paParms[])
{
int rc;
/* Sufficient parameter space? */
{
/*
* So this call apparently failed because the guest wanted to peek
* how much parameters it has to supply in order to successfully retrieve
* this command. Let's tell him so!
*/
}
else
{
}
return rc;
}
/*
* Either fills in parameters from a pending host command into our guest context or
* defer the guest call until we have something from the host.
*/
{
int rc = VINF_SUCCESS;
/*
* Lookup client in our list so that we can assign the context ID of
* a command to that client.
*/
{
break;
it++;
}
/* Not found? Add client to list. */
{
}
/*
* If host command list is empty (nothing to do right now) just
* defer the call until we got something to do (makes the client
* wait, depending on the flags set).
*/
{
}
else
{
/*
* Get the next unassigned host command in the list.
*/
if (RT_SUCCESS(rc))
{
/* Remember which client processes which context (for
* later reference & cleanup). */
/// @todo r=bird: check if already in the list.
/* Only if the guest really got and understood the message remove it from the list. */
}
}
return rc;
}
/*
* Client asks itself (in another thread) to cancel all pending waits which are blocking the client
* from shutting down / doing something else.
*/
{
int rc = VINF_SUCCESS;
{
{
{
}
if (mpHelpers)
}
else
it++;
}
return rc;
}
{
LogFlowFunc(("eFunction=%ld, cParms=%ld, paParms=%p\n",
int rc = VINF_SUCCESS;
if ( eFunction == GUEST_EXEC_SEND_STATUS
&& cParms == 5)
{
if (mpfnHostCallback)
}
else if ( eFunction == GUEST_EXEC_SEND_OUTPUT
&& cParms == 5)
{
if (mpfnHostCallback)
}
else
return rc;
}
{
int rc = VINF_SUCCESS;
if ( RT_SUCCESS(rc)
{
/*
* Assume that the context ID *always* is the first parameter,
* assign the context ID to the command.
*/
}
bool fProcessed = false;
if (RT_SUCCESS(rc))
{
/* Can we wake up a waiting client on guest? */
if (!mClientWaiterList.empty())
{
/* In any case the client did something, so wake up and remove from list. */
/* If we got VERR_TOO_MUCH_DATA we buffer the host command in the next block
* and return success to the host. */
if (rc == VERR_TOO_MUCH_DATA)
{
rc = VINF_SUCCESS;
}
else /* If command was understood by the client, free and remove from host commands list. */
{
fProcessed = true;
}
}
/* If not processed, buffer it ... */
if (!fProcessed)
{
#if 0
/* Limit list size by deleting oldest element. */
#endif
}
}
return rc;
}
/**
* Handle an HGCM service call.
* @copydoc VBOXHGCMSVCFNTABLE::pfnCall
* @note All functions which do not involve an unreasonable delay will be
* handled synchronously. If needed, we will add a request handler
* thread in future for those which do.
*
* @thread HGCM
*/
{
int rc = VINF_SUCCESS;
LogFlowFunc(("u32ClientID = %d, fn = %d, cParms = %d, pparms = %d\n",
try
{
switch (eFunction)
{
/* The guest asks the host for the next messsage to process. */
case GUEST_GET_HOST_MSG:
LogFlowFunc(("GUEST_GET_HOST_MSG\n"));
break;
LogFlowFunc(("GUEST_CANCEL_PENDING_WAITS\n"));
break;
case GUEST_EXEC_SEND_OUTPUT:
LogFlowFunc(("GUEST_EXEC_SEND_OUTPUT\n"));
break;
/* The guest notifies the host of the current client status. */
case GUEST_EXEC_SEND_STATUS:
LogFlowFunc(("SEND_STATUS\n"));
break;
default:
break;
}
if (rc != VINF_HGCM_ASYNC_EXECUTE)
{
/* Tell the client that the call is complete (unblocks waiting). */
}
}
{
rc = VERR_NO_MEMORY;
}
}
/**
* Service call handler for the host.
* @copydoc VBOXHGCMSVCFNTABLE::pfnHostCall
* @thread hgcm
*/
{
int rc = VINF_SUCCESS;
LogFlowFunc(("fn = %d, cParms = %d, pparms = %d\n",
try
{
switch (eFunction)
{
/* The host wants to execute something. */
case HOST_EXEC_CMD:
LogFlowFunc(("HOST_EXEC_CMD\n"));
break;
/* The host wants to send something to the guest's stdin pipe. */
case HOST_EXEC_SET_INPUT:
LogFlowFunc(("HOST_EXEC_SET_INPUT\n"));
break;
case HOST_EXEC_GET_OUTPUT:
LogFlowFunc(("HOST_EXEC_GET_OUTPUT\n"));
break;
default:
break;
}
}
{
rc = VERR_NO_MEMORY;
}
return rc;
}
{
/* Free allocated buffered host commands. */
return VINF_SUCCESS;
}
} /* namespace guestControl */
using guestControl::Service;
/**
* @copydoc VBOXHGCMSVCLOAD
*/
{
int rc = VINF_SUCCESS;
{
}
else
{
LogFlowFunc(("ptable->cbSize = %d, ptable->u32Version = 0x%08X\n", ptable->cbSize, ptable->u32Version));
{
}
else
{
/* No exceptions may propogate outside. */
try {
} catch (int rcThrown) {
} catch (...) {
}
if (RT_SUCCESS(rc))
{
/*
* We don't need an additional client data area on the host,
* because we're a class which can have members for that :-).
*/
/* Register functions. */
/* Service specific initialization. */
}
}
}
return rc;
}