service.cpp revision 7d243573ddd18562ffc1546360a99a762b2b0ca3
/* $Id$ */
/** @file
* Guest Property Service: Host service entry points.
*/
/*
* Copyright (C) 2008 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_properties Guest Property HGCM Service
*
* This HGCM service allows the guest to set and query values in a property
* store on the host. The service proxies the guest requests to the service
* owner on the host using a request callback provided by the owner, and is
* notified of changes to properties made by the host. It forwards these
* notifications to clients in the guest which have expressed interest and
* are waiting for notification.
*
* The service currently consists of two threads. One of these is the main
* HGCM service thread which deals with requests from the guest and from the
* host. The second thread sends the host asynchronous notifications of
* changes made by the guest and deals with notification timeouts.
*
* Guest requests to wait for notification are added to a list of open
* notification requests and completed when a corresponding guest property
* is changed or when the request times out.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP LOG_GROUP_HGCM
#include <memory> /* for auto_ptr */
#include <string>
#include <list>
namespace guestProp {
/**
* Structure for holding a property
*/
struct Property
{
/** The string space core record. */
/** The name of the property */
/** The property value */
/** The timestamp of the property */
/** The property flags */
/** Default constructor */
{
}
/** Constructor with const char * */
{
}
/** Constructor with std::string */
/** Does the property name match one of a set of patterns? */
bool Matches(const char *pszPatterns) const
{
NULL)
);
}
/** Are two properties equal? */
{
return false;
return false;
return false;
return false;
return true;
}
/* Is the property nil? */
bool isNull()
{
}
};
/** The properties list type */
/**
* Structure for holding an uncompleted guest call
*/
struct GuestCall
{
/** The call handle */
/** The function that was requested */
/** The call parameters */
/** The default return value, used for passing warnings */
int mRc;
/** The standard constructor */
/** The normal constructor */
};
/** The guest call list type */
/**
* Class containing the shared information service functionality.
*/
class Service : public RTCNonCopyable
{
private:
/** Type definition for use in callback functions */
/** HGCM helper functions. */
/** Global flags for the service */
/** The property string space handle. */
/** The number of properties. */
unsigned mcProperties;
/** The list of property changes for guest notifications */
/** The list of outstanding guest notification calls */
/** @todo we should have classes for thread and request handler thread */
/** 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 previous timestamp.
* This is used by getCurrentTimestamp() to decrease the chance of
* generating duplicate timestamps. */
/** The number of consecutive timestamp adjustments that we've made.
* Together with mPrevTimestamp, this defines a set of obsolete timestamp
* values: {(mPrevTimestamp - mcTimestampAdjustments), ..., mPrevTimestamp} */
/**
* Get the next property change notification from the queue of saved
* notification based on the timestamp of the last notification seen.
* Notifications will only be reported if the property name matches the
* pattern given.
*
* @returns iprt status value
* @returns VWRN_NOT_FOUND if the last notification was not found in the queue
* @param pszPatterns the patterns to match the property name against
* @param u64Timestamp the timestamp of the last notification
* @param pProp where to return the property found. If none is
* found this will be set to nil.
* @thread HGCM
*/
{
/* Zero means wait for a new notification. */
#ifdef VBOX_STRICT
/*
* ENSURE that pProp is the first event in the notification queue that:
* - Appears later than u64Timestamp
* - Matches the pszPatterns
*/
/** @todo r=bird: This incorrectly ASSUMES that mTimestamp is unique.
* The timestamp resolution can be very coarse on windows for instance. */
{}
else
++it; /* Next event */
if (pProp->mTimestamp != 0)
{
}
#endif /* VBOX_STRICT */
return rc;
}
/**
* Check whether we have permission to change a property.
*
* @returns Strict VBox status code.
* @retval VINF_SUCCESS if we do.
* @retval VERR_PERMISSION_DENIED if the value is read-only for the requesting
* side.
* @retval VINF_PERMISSION_DENIED if the side is globally marked read-only.
*
* @param eFlags the flags on the property in question
* @param isGuest is the guest or the host trying to make the change?
*/
{
return VERR_PERMISSION_DENIED;
return VINF_PERMISSION_DENIED;
return VINF_SUCCESS;
}
/**
* Gets a property.
*
* @returns Pointer to the property if found, NULL if not.
*
* @param pszName The name of the property to get.
*/
{
}
public:
, mhProperties(NULL)
, mcProperties(0)
, mpvHostData(NULL)
, mPrevTimestamp(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.
*/
uint32_t /* u32ClientID */,
void * /* pvClient */)
{
return VINF_SUCCESS;
}
/**
* @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:
uint64_t getCurrentTimestamp(void);
int getOldNotificationInternal(const char *pszPattern,
int uninit();
};
/**
* Gets the current timestamp.
*
* Since the RTTimeNow resolution can be very coarse, this method takes some
* simple steps to try avoid returning the same timestamp for two consecutive
* calls. Code like getOldNotification() more or less assumes unique
* timestamps.
*
* @returns Nanosecond timestamp.
*/
{
else
{
}
this->mPrevTimestamp = u64NanoTS;
return u64NanoTS;
}
/**
* Check that a string fits our criteria for a property name.
*
* @returns IPRT status code
* @param pszName the string to check, must be valid Utf8
* @param cbName the number of bytes @a pszName points to, including the
* terminating '\0'
* @thread HGCM
*/
{
int rc = VINF_SUCCESS;
return rc;
}
/**
* Check a string fits our criteria for the value of a guest property.
*
* @returns IPRT status code
* @param pszValue the string to check, must be valid Utf8
* @param cbValue the length in bytes of @a pszValue, including the
* terminator
* @thread HGCM
*/
{
int rc = VINF_SUCCESS;
if (RT_SUCCESS(rc))
return rc;
}
/**
* Set a block of properties in the property registry, checking the validity
* of the arguments passed.
*
* @returns iprt status value
* @param cParms the number of HGCM parameters supplied
* @param paParms the array of HGCM parameters
* @thread HGCM
*/
{
const char **papszNames;
const char **papszValues;
const char **papszFlags;
int rc = VINF_SUCCESS;
/*
* Get and validate the parameters
*/
if ( cParms != 4
)
/** @todo validate the array sizes... */
{
if ( !RT_VALID_PTR(papszNames[i])
|| !RT_VALID_PTR(papszValues[i])
|| !RT_VALID_PTR(papszFlags[i])
)
else
{
}
}
if (RT_SUCCESS(rc))
{
/*
* Add the properties. No way to roll back here.
*/
for (unsigned i = 0; papszNames[i] != NULL; ++i)
{
if (pProp)
{
/* Update existing property. */
}
else
{
/* Create a new property */
if (!pProp)
{
rc = VERR_NO_MEMORY;
break;
}
mcProperties++;
else
{
delete pProp;
}
}
}
}
return rc;
}
/**
* Retrieve a value from the property registry by name, checking the validity
* of the arguments passed. If the guest has not allocated enough buffer
* space for the value then we return VERR_OVERFLOW and set the size of the
* buffer needed in the "size" HGCM parameter. If the name was not found at
* all, we return VERR_NOT_FOUND.
*
* @returns iprt status value
* @param cParms the number of HGCM parameters supplied
* @param paParms the array of HGCM parameters
* @thread HGCM
*/
{
int rc;
char *pchBuf;
/*
* Get and validate the parameters
*/
LogFlowThisFunc(("\n"));
)
else
if (RT_FAILURE(rc))
{
return rc;
}
/*
* Read and set the values we will return
*/
/* Get the property. */
if (pProp)
{
char szFlags[MAX_FLAGS_LEN];
if (RT_SUCCESS(rc))
{
/* Check that the buffer is big enough */
{
/* Write the value, flags and timestamp */
/*
* Done! Do exit logging and return.
*/
Log2(("Queried string %s, value=%s, timestamp=%lld, flags=%s\n",
}
else
}
}
else
rc = VERR_NOT_FOUND;
return rc;
}
/**
* Set a value in the property registry by name, checking the validity
* of the arguments passed.
*
* @returns iprt status value
* @param cParms the number of HGCM parameters supplied
* @param paParms the array of HGCM parameters
* @param isGuest is this call coming from the guest (or the host)?
* @throws std::bad_alloc if an out of memory condition occurs
* @thread HGCM
*/
{
int rc = VINF_SUCCESS;
LogFlowThisFunc(("\n"));
/*
* General parameter correctness checking.
*/
if ( RT_SUCCESS(rc)
|| ( (3 == cParms)
)
)
)
/*
* Check the values passed in the parameters for correctness.
*/
if (RT_SUCCESS(rc))
if (RT_SUCCESS(rc))
if (RT_FAILURE(rc))
{
return rc;
}
/*
* If the property already exists, check its flags to see if we are allowed
* to change it.
*/
if (rc == VINF_SUCCESS)
{
/*
* Set the actual value
*/
if (pProp)
{
}
else if (mcProperties < MAX_PROPS)
{
/* Create a new string space record. */
if (pProp)
{
mcProperties++;
else
{
AssertFailed();
delete pProp;
}
}
else
rc = VERR_NO_MEMORY;
}
else
/*
* Send a notification to the host and return.
*/
// if (isGuest) /* Notify the host even for properties that the host
// * changed. Less efficient, but ensures consistency. */
}
return rc;
}
/**
* Remove a value in the property registry by name, checking the validity
* of the arguments passed.
*
* @returns iprt status value
* @param cParms the number of HGCM parameters supplied
* @param paParms the array of HGCM parameters
* @param isGuest is this call coming from the guest (or the host)?
* @thread HGCM
*/
{
int rc;
LogFlowThisFunc(("\n"));
/*
* Check the user-supplied parameters.
*/
)
else
if (RT_FAILURE(rc))
{
return rc;
}
/*
* If the property exists, check its flags to see if we are allowed
* to change it.
*/
if (pProp)
/*
* And delete the property if all is well.
*/
{
mcProperties--;
delete pProp;
// if (isGuest) /* Notify the host even for properties that the host
// * changed. Less efficient, but ensures consistency. */
}
return rc;
}
/**
* Enumeration data shared between enumPropsCallback and Service::enumProps.
*/
typedef struct ENUMDATA
{
const char *pszPattern; /**< The pattern to match properties against. */
char *pchCur; /**< The current buffer postion. */
} ENUMDATA;
/**
* @callback_method_impl{FNRTSTRSPACECALLBACK}
*/
{
/* Included in the enumeration? */
return 0;
/* Convert the non-string members into strings. */
char szTimestamp[256];
char szFlags[MAX_FLAGS_LEN];
if (RT_FAILURE(rc))
return rc;
/* Calculate the buffer space requirements. */
/* Sufficient buffer space? */
{
return 0; /* don't quit */
}
/* Append the property to the buffer. */
pchCur += cbTimestamp;
return 0;
}
/**
* Enumerate guest properties by mask, checking the validity
* of the arguments passed.
*
* @returns iprt status value
* @param cParms the number of HGCM parameters supplied
* @param paParms the array of HGCM parameters
* @thread HGCM
*/
{
int rc = VINF_SUCCESS;
/*
* Get the HGCM function arguments.
*/
char const *pchPatterns = NULL;
uint32_t cbPatterns = 0;
LogFlowThisFunc(("\n"));
)
/*
* First repack the patterns into the format expected by RTStrSimplePatternMatch()
*/
char szPatterns[MAX_PATTERN_LEN];
if (RT_SUCCESS(rc))
{
for (unsigned i = 0; i < cbPatterns - 1; ++i)
if (pchPatterns[i] != '\0')
szPatterns[i] = pchPatterns[i];
else
szPatterns[i] = '|';
}
/*
* Next enumerate into the buffer.
*/
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
{
{
/* The final terminators. */
}
else
}
}
return rc;
}
/** Helper query used by getOldNotification */
{
/* We count backwards, as the guest should normally be querying the
* most recent events. */
int rc = VWRN_NOT_FOUND;
{
rc = VINF_SUCCESS;
break;
}
/* Now look for an event matching the patterns supplied. The base()
* member conveniently points to the following element. */
{
return rc;
}
return rc;
}
/** Helper query used by getNotification */
{
int rc = VINF_SUCCESS;
/* Format the data to write to the buffer. */
char *pchBuf;
if (RT_SUCCESS(rc))
{
char szFlags[MAX_FLAGS_LEN];
if (RT_SUCCESS(rc))
{
buffer += '\0';
buffer += '\0';
buffer += '\0';
}
}
/* Write out the data. */
if (RT_SUCCESS(rc))
{
else
}
return rc;
}
/**
* Get the next guest notification.
*
* @returns iprt status value
* @param cParms the number of HGCM parameters supplied
* @param paParms the array of HGCM parameters
* @thread HGCM
* @throws can throw std::bad_alloc
*/
{
int rc = VINF_SUCCESS;
char *pchBuf;
uint32_t cchPatterns = 0;
/*
* Get the HGCM function arguments and perform basic verification.
*/
LogFlowThisFunc(("\n"));
)
if (RT_SUCCESS(rc))
u64Timestamp));
/*
* If no timestamp was supplied or no notification was found in the queue
* of old notifications, enqueue the request in the waiting queue.
*/
{
}
/*
* Otherwise reply at once with the enqueued notification we found.
*/
else
{
if (RT_FAILURE(rc2))
}
return rc;
}
/**
* Notify the service owner and the guest that a property has been
* @param pszProperty the name of the property which has changed
* @param u64Timestamp the time at which the change took place
*
* @thread HGCM service
*/
{
/* Ensure that our timestamp is different to the last one. */
if ( !mGuestNotifications.empty()
++u64Timestamp;
/*
* Try to find the property. Create a change event if we find it and a
* delete event if we do not.
*/
/* prop is currently a delete event for pszProperty */
if (pProp)
{
/* Make prop into a change event. */
}
/* Release waiters if applicable and add the event to the queue for
* guest notifications */
int rc = VINF_SUCCESS;
try
{
{
const char *pszPatterns;
{
if (RT_SUCCESS(rc2))
}
else
++it;
}
}
{
rc = VERR_NO_MEMORY;
}
/*
* Host notifications - first case: if the property exists then send its
* current value
*/
{
char szFlags[MAX_FLAGS_LEN];
/* Send out a host notification */
if (RT_SUCCESS(rc))
if (RT_SUCCESS(rc))
}
/*
* Host notifications - second case: if the property does not exist then
* send the host an empty value
*/
{
/* Send out a host notification */
if (RT_SUCCESS(rc))
}
LogFlowThisFunc(("returning\n"));
}
/**
* @returns IPRT status value
* @param pszName the property name
* @param pszValue the new value, or NULL if the property was deleted
* @param u64Timestamp the time of the change
* @param pszFlags the new flags string
*/
{
LogFlowFunc(("pszName=%s, pszValue=%s, u64Timestamp=%llu, pszFlags=%s\n",
(void *)(&HostCallbackData),
sizeof(HostCallbackData));
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 = %p\n",
try
{
switch (eFunction)
{
/* The guest wishes to read a property */
case GET_PROP:
LogFlowFunc(("GET_PROP\n"));
break;
/* The guest wishes to set a property */
case SET_PROP:
LogFlowFunc(("SET_PROP\n"));
break;
/* The guest wishes to set a property value */
case SET_PROP_VALUE:
LogFlowFunc(("SET_PROP_VALUE\n"));
break;
/* The guest wishes to remove a configuration value */
case DEL_PROP:
LogFlowFunc(("DEL_PROP\n"));
break;
/* The guest wishes to enumerate all properties */
case ENUM_PROPS:
LogFlowFunc(("ENUM_PROPS\n"));
break;
/* The guest wishes to get the next property notification */
case GET_NOTIFICATION:
LogFlowFunc(("GET_NOTIFICATION\n"));
break;
default:
}
}
{
rc = VERR_NO_MEMORY;
}
if (rc != VINF_HGCM_ASYNC_EXECUTE)
{
}
}
/**
* Enumeration data shared between dbgInfoCallback and Service::dbgInfoShow.
*/
typedef struct ENUMDBGINFO
{
} ENUMDBGINFO;
{
char szFlags[MAX_FLAGS_LEN];
if (RT_FAILURE(rc))
return 0;
}
{
}
/**
* Handler for debug info.
*
* @param pvUser user pointer.
* @param pHlp The info helper functions.
* @param pszArgs Arguments, ignored.
*/
{
}
/**
* Service call handler for the host.
* @copydoc VBOXHGCMSVCFNTABLE::pfnHostCall
* @thread hgcm
*/
{
int rc = VINF_SUCCESS;
LogFlowFunc(("fn = %d, cParms = %d, pparms = %p\n",
try
{
switch (eFunction)
{
/* The host wishes to set a block of properties */
case SET_PROPS_HOST:
LogFlowFunc(("SET_PROPS_HOST\n"));
break;
/* The host wishes to read a configuration value */
case GET_PROP_HOST:
LogFlowFunc(("GET_PROP_HOST\n"));
break;
/* The host wishes to set a configuration value */
case SET_PROP_HOST:
LogFlowFunc(("SET_PROP_HOST\n"));
break;
/* The host wishes to set a configuration value */
case SET_PROP_VALUE_HOST:
LogFlowFunc(("SET_PROP_VALUE_HOST\n"));
break;
/* The host wishes to remove a configuration value */
case DEL_PROP_HOST:
LogFlowFunc(("DEL_PROP_HOST\n"));
break;
/* The host wishes to enumerate all properties */
case ENUM_PROPS_HOST:
LogFlowFunc(("ENUM_PROPS\n"));
break;
/* The host wishes to set global flags for the service */
case SET_GLOBAL_FLAGS_HOST:
LogFlowFunc(("SET_GLOBAL_FLAGS_HOST\n"));
if (cParms == 1)
{
if (RT_SUCCESS(rc))
}
else
break;
case GET_DBGF_INFO_FN:
if (cParms != 2)
return VERR_INVALID_PARAMETER;
break;
default:
break;
}
}
{
rc = VERR_NO_MEMORY;
}
return rc;
}
{
return VINF_SUCCESS;
}
} /* namespace guestProp */
/**
* @copydoc VBOXHGCMSVCLOAD
*/
{
int rc = VERR_IPE_UNINITIALIZED_STATUS;
if (!RT_VALID_PTR(ptable))
else
{
LogFlowFunc(("ptable->cbSize = %d, ptable->u32Version = 0x%08X\n", ptable->cbSize, ptable->u32Version));
else
{
/* No exceptions may propagate outside. */
try
{
rc = VINF_SUCCESS;
}
catch (int rcThrown)
{
}
catch (...)
{
}
if (RT_SUCCESS(rc))
{
/* We do not maintain connections, so no client data is needed. */
/* Service specific initialization. */
}
else
}
}
return rc;
}