service.cpp revision 279f89eae4e37af8e041cc0f8f01af7ca05113d8
/* $Id$ */
/** @file
* Guest Control Service: Controlling the guest.
*/
/*
* Copyright (C) 2011 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 this service. A client is represented by its unique HGCM client ID.
* - Context ID: An (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 happened, 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
* maintaining 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 constructor. */
};
/** 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 constructor. */
/** The normal constructor. */
};
/** 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. */
/** How many times the host service has tried to deliver this
* command to the guest. */
/** Dynamic structure for holding the HGCM parms */
/** The standard constructor. */
};
/** The host cmd list + iterator type */
/**
* Class containing the shared information service functionality.
*/
class Service : public RTCNonCopyable
{
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. */
/** Number of connected clients. */
public:
, mpvHostData(NULL)
, mNumClients(0)
{
}
/**
* @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 paramBufferAssign(VBOXHGCMSVCPARM paDstParms[], uint32_t cDstParms, PVBOXGUESTCTRPARAMBUFFER pSrcBuf);
int assignHostCmdToGuest(HostCmd *pCmd, VBOXHGCMCALLHANDLE callHandle, uint32_t cParms, VBOXHGCMSVCPARM paParms[]);
int retrieveNextHostCmd(uint32_t u32ClientID, VBOXHGCMCALLHANDLE callHandle, uint32_t cParms, VBOXHGCMSVCPARM paParms[]);
int uninit();
};
/**
* Stores a HGCM request in an internal buffer. Needs to be free'd using paramBufferFree().
*
* @return IPRT status code.
* @param pBuf Buffer to store the HGCM request into.
* @param uMsg Message type.
* @param cParms Number of parameters of HGCM request.
* @param paParms Array of parameters of HGCM request.
*/
int Service::paramBufferAllocate(PVBOXGUESTCTRPARAMBUFFER pBuf, uint32_t uMsg, uint32_t cParms, VBOXHGCMSVCPARM paParms[])
{
if (cParms)
/* Paranoia. */
if (cParms > 256)
cParms = 256;
int rc = VINF_SUCCESS;
/*
* 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 (pBuf->uParmCount)
{
rc = VERR_NO_MEMORY;
}
if (RT_SUCCESS(rc))
{
{
{
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
}
else
{
/* Size is 0 -- make sure we don't have any pointer. */
}
break;
default:
break;
}
if (RT_FAILURE(rc))
break;
}
}
return rc;
}
/**
* Frees a buffered HGCM request.
*
* @return IPRT status code.
* @param pBuf Parameter buffer to free.
*/
{
{
{
case VBOX_HGCM_SVC_PARM_PTR:
break;
}
}
if (pBuf->uParmCount)
{
pBuf->uParmCount = 0;
}
}
/**
* Copies data from a buffered HGCM request to the current HGCM request.
*
* @return IPRT status code.
* @param paDstParms Array of parameters of HGCM request to fill the data into.
* @param cPDstarms Number of parameters the HGCM request can handle.
* @param pSrcBuf Parameter buffer to assign.
*/
int Service::paramBufferAssign(VBOXHGCMSVCPARM paDstParms[], uint32_t cDstParms, PVBOXGUESTCTRPARAMBUFFER pSrcBuf)
{
int rc = VINF_SUCCESS;
{
LogFlowFunc(("Parameter count does not match (got %u, expected %u)\n",
}
else
{
{
{
LogFlowFunc(("Parameter %u type mismatch (got %u, expected %u)\n",
}
else
{
{
case VBOX_HGCM_SVC_PARM_32BIT:
break;
case VBOX_HGCM_SVC_PARM_PTR:
{
continue; /* Only copy buffer if there actually is something to copy. */
if (RT_SUCCESS(rc))
{
}
break;
}
case VBOX_HGCM_SVC_PARM_64BIT:
/* Fall through is intentional. */
default:
LogFlowFunc(("Parameter %u of type %u is not supported yet\n",
break;
}
}
if (RT_FAILURE(rc))
{
LogFlowFunc(("Parameter %u invalid (rc=%Rrc), refusing\n",
i, rc));
break;
}
}
}
return rc;
}
/**
* Handles a client which just connected.
*
* @return IPRT status code.
* @param u32ClientID
* @param pvClient
*/
{
if (mNumClients < UINT32_MAX)
mNumClients++;
else
AssertMsgFailed(("Max. number of clients reached\n"));
return VINF_SUCCESS;
}
/**
* Handles a client which disconnected. This functiond does some
* internal cleanup as well as sends notifications to the host so
* that the host can do the same (if required).
*
* @return IPRT status code.
* @param u32ClientID The client's ID of which disconnected.
* @param pvClient User data, not used at the moment.
*/
{
LogFlowFunc(("Client (ID=%u, %u clients total) disconnected\n",
Assert(mNumClients > 0);
mNumClients--;
/* If this was the last connected (guest) client we need to
* unblock all eventually queued up (waiting) host calls. */
bool fAllClientsDisconnected = mNumClients == 0;
LogFlowFunc(("No connected clients left, notifying all queued up callbacks\n"));
/*
* Throw out all stale clients.
*/
int rc = VINF_SUCCESS;
{
{
}
else
itCall++;
}
&& RT_SUCCESS(rc))
{
/*
* or for all items in case there is no waiting client around
* anymore.
*/
{
{
/*
* Notify the host that clients with u32ClientID are no longer
* around and need to be cleaned up (canceling waits etc).
*/
if (RT_FAILURE(rc))
{
LogFlowFunc(("Cancelling of CID=%u failed with rc=%Rrc\n",
uContextID, rc));
/* Keep going. */
}
itContext++;
}
}
else
}
{
/*
* If all clients disconnected we also need to make sure that all buffered
* host commands need to be notified, because Main is waiting a notification
* via a (multi stage) progress object.
*/
{
if (RT_FAILURE(rc))
{
LogFlowFunc(("Cancelling of buffered CID=%u failed with rc=%Rrc\n",
/* Keep going. */
}
}
}
return rc;
}
/**
* Assigns a specified host command to a client.
*
* @return IPRT status code.
* @param pCmd Host command to send.
* @param callHandle Call handle of the client to send the command to.
* @param cParms Number of parameters.
* @param paParms Array of parameters.
*/
int Service::assignHostCmdToGuest(HostCmd *pCmd, VBOXHGCMCALLHANDLE callHandle, uint32_t cParms, VBOXHGCMSVCPARM paParms[])
{
int rc;
/* Does the current host command need more parameter space which
* the client does not provide yet? */
{
/*
* 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.
*
* @return IPRT status code.
* @param u32ClientID The client's ID.
* @param callHandle The client's call handle.
* @param cParms Number of parameters.
* @param paParms Array of parameters.
*/
{
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. */
}
else if (rc == VERR_BUFFER_OVERFLOW)
{
/* If the client understood the message but supplied too little buffer space
* don't send this message again and drop it after 3 unsuccessful attempts.
* The host then should take care of next actions (maybe retry it with a smaller buffer). */
{
}
}
else
{
/* Client did not understand the message or something else weird happened. Try again one
* more time and drop it if it didn't get handled then. */
{
}
}
}
return rc;
}
/**
* Cancels a buffered host command to unblock waits on Main side
* (via (multi stage) progress objects.
*
* @return IPRT status code.
* @param u32ContextID Context ID of host command to cancel.
*/
{
}
/**
* Client asks itself (in another thread) to cancel all pending waits which are blocking the client
* from shutting down / doing something else.
*
* @return IPRT status code.
* @param u32ClientID The client's ID.
*/
{
int rc = VINF_SUCCESS;
{
{
{
}
if (mpHelpers)
}
else
it++;
}
return rc;
}
/**
* Notifies the host (using low-level HGCM callbacks) about an event
* which was sent from the client.
*
* @return IPRT status code.
* @param eFunction Function (event) that occured.
* @param cParms Number of parameters.
* @param paParms Array of parameters.
*/
{
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 if ( eFunction == GUEST_EXEC_SEND_INPUT_STATUS
&& cParms == 5)
{
if (mpfnHostCallback)
}
else
return rc;
}
/**
* Processes a command receiveed from the host side and re-routes it to
* a connect client on the guest.
*
* @return IPRT status code.
* @param eFunction Function code to process.
* @param cParms Number of parameters.
* @param paParms Array of parameters.
*/
{
/*
* If no client is connected at all we don't buffer any host commands
* and immediately return an error to the host. This avoids the host
* waiting for a response from the guest side in case VBoxService on
*/
if (mNumClients == 0)
return VERR_NOT_FOUND;
if ( RT_SUCCESS(rc)
&& cParms) /* Make sure we at least get one parameter (that is, the context ID). */
{
/*
* Assume that the context ID *always* is the first parameter,
* assign the context ID to the command.
*/
}
else if (!cParms)
if (RT_SUCCESS(rc))
{
LogFlowFunc(("Handling host command CID = %u\n",
newCmd.mContextID));
bool fProcessed = false;
/* 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 back an error (like VERR_TOO_MUCH_DATA or VERR_BUFFER_OVERFLOW)
* we buffer the host command in the next block and return success to the host.
*/
if (RT_FAILURE(rc))
{
rc = VINF_SUCCESS;
}
else /* If command was understood by the client, free and remove from host commands list. */
{
LogFlowFunc(("Host command CID = %u processed with rc=%Rrc\n",
}
}
if (!fProcessed)
{
LogFlowFunc(("Buffering host command CID = %u (rc=%Rrc)\n",
}
}
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 = %u, fn = %u, cParms = %u, paParms = 0x%p\n",
try
{
switch (eFunction)
{
/*
* The guest asks the host for the next message to process.
*/
case GUEST_GET_HOST_MSG:
LogFlowFunc(("GUEST_GET_HOST_MSG\n"));
break;
/*
* The guest wants to shut down and asks us (this service) to cancel
* all blocking pending waits (VINF_HGCM_ASYNC_EXECUTE) so that the
* guest can gracefully shut down.
*/
LogFlowFunc(("GUEST_CANCEL_PENDING_WAITS\n"));
break;
/*
* For all other regular commands we call our notifyHost
* function. If the current command does not support notifications
* notifyHost will return VERR_NOT_SUPPORTED.
*/
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 = VERR_NOT_SUPPORTED;
LogFlowFunc(("fn = %u, cParms = %u, paParms = 0x%p\n",
try
{
}
{
rc = VERR_NO_MEMORY;
}
return rc;
}
{
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 propagate 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;
}