summaryrefslogtreecommitdiffstats
path: root/src/VBox/HostServices/GuestProperties
diff options
context:
space:
mode:
Diffstat (limited to 'src/VBox/HostServices/GuestProperties')
-rw-r--r--src/VBox/HostServices/GuestProperties/Makefile.kmk50
-rw-r--r--src/VBox/HostServices/GuestProperties/VBoxGuestPropSvc.cpp1894
-rw-r--r--src/VBox/HostServices/GuestProperties/VBoxGuestPropSvc.rc51
-rw-r--r--src/VBox/HostServices/GuestProperties/testcase/Makefile.kmk46
-rw-r--r--src/VBox/HostServices/GuestProperties/testcase/tstGuestPropSvc.cpp1171
5 files changed, 3212 insertions, 0 deletions
diff --git a/src/VBox/HostServices/GuestProperties/Makefile.kmk b/src/VBox/HostServices/GuestProperties/Makefile.kmk
new file mode 100644
index 00000000..d5c78d8e
--- /dev/null
+++ b/src/VBox/HostServices/GuestProperties/Makefile.kmk
@@ -0,0 +1,50 @@
+# $Id: Makefile.kmk $
+## @file
+# Sub-Makefile for the Shared Info Services Host Service.
+#
+
+#
+# Copyright (C) 2006-2019 Oracle Corporation
+#
+# This file is part of VirtualBox Open Source Edition (OSE), as
+# available from http://www.virtualbox.org. This file is free software;
+# you can redistribute it and/or modify it under the terms of the GNU
+# 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.
+#
+
+SUB_DEPTH = ../../../..
+include $(KBUILD_PATH)/subheader.kmk
+
+# Include sub-makefile(s).
+include $(PATH_SUB_CURRENT)/testcase/Makefile.kmk
+
+#
+# The shared folder service DLL.
+#
+DLLS += VBoxGuestPropSvc
+VBoxGuestPropSvc_TEMPLATE = VBOXR3
+VBoxGuestPropSvc_NAME.os2 = VBoxSIS
+VBoxGuestPropSvc_DEFS = VBOX_WITH_HGCM
+VBoxGuestPropSvc_INCS = $(PATH_ROOT)/src/VBox/Main/include
+VBoxGuestPropSvc_INCS.win = \
+ $(VBOX_PATH_SDK)
+
+VBoxGuestPropSvc_SOURCES = \
+ VBoxGuestPropSvc.cpp
+
+VBoxGuestPropSvc_SOURCES.win = \
+ VBoxGuestPropSvc.rc
+
+VBoxGuestPropSvc_LIBS = \
+ $(LIB_VMM) \
+ $(LIB_RUNTIME) \
+ $(LIB_REM)
+
+VBoxGuestPropSvc_LDFLAGS.darwin = \
+ -install_name $(VBOX_DYLD_EXECUTABLE_PATH)/VBoxGuestPropSvc.dylib
+
+include $(FILE_KBUILD_SUB_FOOTER)
+
diff --git a/src/VBox/HostServices/GuestProperties/VBoxGuestPropSvc.cpp b/src/VBox/HostServices/GuestProperties/VBoxGuestPropSvc.cpp
new file mode 100644
index 00000000..58f95269
--- /dev/null
+++ b/src/VBox/HostServices/GuestProperties/VBoxGuestPropSvc.cpp
@@ -0,0 +1,1894 @@
+/* $Id: VBoxGuestPropSvc.cpp $ */
+/** @file
+ * Guest Property Service: Host service entry points.
+ */
+
+/*
+ * Copyright (C) 2008-2019 Oracle Corporation
+ *
+ * This file is part of VirtualBox Open Source Edition (OSE), as
+ * available from http://www.virtualbox.org. This file is free software;
+ * you can redistribute it and/or modify it under the terms of the GNU
+ * 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 <VBox/HostServices/GuestPropertySvc.h>
+
+#include <VBox/log.h>
+#include <iprt/asm.h>
+#include <iprt/assert.h>
+#include <iprt/buildconfig.h>
+#include <iprt/cpp/autores.h>
+#include <iprt/cpp/utils.h>
+#include <iprt/cpp/ministring.h>
+#include <VBox/err.h>
+#include <VBox/hgcmsvc.h>
+#include <iprt/mem.h>
+#include <iprt/req.h>
+#include <iprt/string.h>
+#include <iprt/thread.h>
+#include <iprt/time.h>
+#include <VBox/vmm/dbgf.h>
+#include <VBox/version.h>
+
+#include <list>
+
+
+namespace guestProp {
+
+/**
+ * Structure for holding a property
+ */
+struct Property
+{
+ /** The string space core record. */
+ RTSTRSPACECORE mStrCore;
+ /** The name of the property */
+ RTCString mName;
+ /** The property value */
+ RTCString mValue;
+ /** The timestamp of the property */
+ uint64_t mTimestamp;
+ /** The property flags */
+ uint32_t mFlags;
+
+ /** Default constructor */
+ Property() : mTimestamp(0), mFlags(GUEST_PROP_F_NILFLAG)
+ {
+ RT_ZERO(mStrCore);
+ }
+ /** Constructor with const char * */
+ Property(const char *pcszName, const char *pcszValue, uint64_t nsTimestamp, uint32_t u32Flags)
+ : mName(pcszName)
+ , mValue(pcszValue)
+ , mTimestamp(nsTimestamp)
+ , mFlags(u32Flags)
+ {
+ RT_ZERO(mStrCore);
+ mStrCore.pszString = mName.c_str();
+ }
+ /** Constructor with std::string */
+ Property(RTCString const &rName, RTCString const &rValue, uint64_t nsTimestamp, uint32_t fFlags)
+ : mName(rName)
+ , mValue(rValue)
+ , mTimestamp(nsTimestamp)
+ , mFlags(fFlags)
+ {}
+
+ /** Does the property name match one of a set of patterns? */
+ bool Matches(const char *pszPatterns) const
+ {
+ return ( pszPatterns[0] == '\0' /* match all */
+ || RTStrSimplePatternMultiMatch(pszPatterns, RTSTR_MAX,
+ mName.c_str(), RTSTR_MAX,
+ NULL)
+ );
+ }
+
+ /** Are two properties equal? */
+ bool operator==(const Property &prop)
+ {
+ if (mTimestamp != prop.mTimestamp)
+ return false;
+ if (mFlags != prop.mFlags)
+ return false;
+ if (mName != prop.mName)
+ return false;
+ if (mValue != prop.mValue)
+ return false;
+ return true;
+ }
+
+ /* Is the property nil? */
+ bool isNull()
+ {
+ return mName.isEmpty();
+ }
+};
+/** The properties list type */
+typedef std::list <Property> PropertyList;
+
+/**
+ * Structure for holding an uncompleted guest call
+ */
+struct GuestCall
+{
+ uint32_t u32ClientId;
+ /** The call handle */
+ VBOXHGCMCALLHANDLE mHandle;
+ /** The function that was requested */
+ uint32_t mFunction;
+ /** Number of call parameters. */
+ uint32_t mParmsCnt;
+ /** The call parameters */
+ VBOXHGCMSVCPARM *mParms;
+ /** The default return value, used for passing warnings */
+ int mRc;
+
+ /** The standard constructor */
+ GuestCall(void) : u32ClientId(0), mFunction(0), mParmsCnt(0) {}
+ /** The normal constructor */
+ GuestCall(uint32_t aClientId, VBOXHGCMCALLHANDLE aHandle, uint32_t aFunction,
+ uint32_t aParmsCnt, VBOXHGCMSVCPARM aParms[], int aRc)
+ : u32ClientId(aClientId), mHandle(aHandle), mFunction(aFunction),
+ mParmsCnt(aParmsCnt), mParms(aParms), mRc(aRc) {}
+};
+/** The guest call list type */
+typedef std::list <GuestCall> CallList;
+
+/**
+ * Class containing the shared information service functionality.
+ */
+class Service : public RTCNonCopyable
+{
+private:
+ /** Type definition for use in callback functions */
+ typedef Service SELF;
+ /** HGCM helper functions. */
+ PVBOXHGCMSVCHELPERS mpHelpers;
+ /** Global flags for the service */
+ uint32_t mfGlobalFlags;
+ /** The property string space handle. */
+ RTSTRSPACE mhProperties;
+ /** The number of properties. */
+ unsigned mcProperties;
+ /** The list of property changes for guest notifications;
+ * only used for timestamp tracking in notifications at the moment */
+ PropertyList mGuestNotifications;
+ /** The list of outstanding guest notification calls */
+ CallList mGuestWaiters;
+ /** @todo we should have classes for thread and request handler thread */
+ /** Callback function supplied by the host for notification of updates
+ * to properties */
+ PFNHGCMSVCEXT mpfnHostCallback;
+ /** 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. */
+ uint64_t mPrevTimestamp;
+ /** The number of consecutive timestamp adjustments that we've made.
+ * Together with mPrevTimestamp, this defines a set of obsolete timestamp
+ * values: {(mPrevTimestamp - mcTimestampAdjustments), ..., mPrevTimestamp} */
+ uint64_t mcTimestampAdjustments;
+ /** For helping setting host version properties _after_ restoring VMs. */
+ bool m_fSetHostVersionProps;
+
+ /**
+ * 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 nsTimestamp the timestamp of the last notification
+ * @param pProp where to return the property found. If none is
+ * found this will be set to nil.
+ * @throws nothing
+ * @thread HGCM
+ */
+ int getOldNotification(const char *pszPatterns, uint64_t nsTimestamp, Property *pProp)
+ {
+ AssertPtrReturn(pszPatterns, VERR_INVALID_POINTER);
+ /* Zero means wait for a new notification. */
+ AssertReturn(nsTimestamp != 0, VERR_INVALID_PARAMETER);
+ AssertPtrReturn(pProp, VERR_INVALID_POINTER);
+ int rc = getOldNotificationInternal(pszPatterns, nsTimestamp, pProp);
+#ifdef VBOX_STRICT
+ /*
+ * ENSURE that pProp is the first event in the notification queue that:
+ * - Appears later than nsTimestamp
+ * - Matches the pszPatterns
+ */
+ /** @todo r=bird: This incorrectly ASSUMES that mTimestamp is unique.
+ * The timestamp resolution can be very coarse on windows for instance. */
+ PropertyList::const_iterator it = mGuestNotifications.begin();
+ for (; it != mGuestNotifications.end()
+ && it->mTimestamp != nsTimestamp; ++it)
+ { /*nothing*/ }
+ if (it == mGuestNotifications.end()) /* Not found */
+ it = mGuestNotifications.begin();
+ else
+ ++it; /* Next event */
+ for (; it != mGuestNotifications.end()
+ && it->mTimestamp != pProp->mTimestamp; ++it)
+ Assert(!it->Matches(pszPatterns));
+ if (pProp->mTimestamp != 0)
+ {
+ Assert(*pProp == *it);
+ Assert(pProp->Matches(pszPatterns));
+ }
+#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 fFlags the flags on the property in question
+ * @param isGuest is the guest or the host trying to make the change?
+ */
+ int checkPermission(uint32_t fFlags, bool isGuest)
+ {
+ if (fFlags & (isGuest ? GUEST_PROP_F_RDONLYGUEST : GUEST_PROP_F_RDONLYHOST))
+ return VERR_PERMISSION_DENIED;
+ if (isGuest && (mfGlobalFlags & GUEST_PROP_F_RDONLYGUEST))
+ return VINF_PERMISSION_DENIED;
+ return VINF_SUCCESS;
+ }
+
+ /**
+ * Check whether the property name is reserved for host changes only.
+ *
+ * @returns Boolean true (host reserved) or false (available to guest).
+ *
+ * @param pszName The property name to check.
+ */
+ bool checkHostReserved(const char *pszName)
+ {
+ if (RTStrStartsWith(pszName, "/VirtualBox/GuestAdd/VBoxService/"))
+ return true;
+ if (RTStrStartsWith(pszName, "/VirtualBox/GuestAdd/PAM/"))
+ return true;
+ if (RTStrStartsWith(pszName, "/VirtualBox/GuestAdd/Greeter/"))
+ return true;
+ if (RTStrStartsWith(pszName, "/VirtualBox/GuestAdd/SharedFolders/"))
+ return true;
+ if (RTStrStartsWith(pszName, "/VirtualBox/HostInfo/"))
+ return true;
+ if (RTStrStartsWith(pszName, "/VirtualBox/VMInfo/"))
+ return true;
+ return false;
+ }
+
+ /**
+ * Gets a property.
+ *
+ * @returns Pointer to the property if found, NULL if not.
+ *
+ * @param pszName The name of the property to get.
+ */
+ Property *getPropertyInternal(const char *pszName)
+ {
+ return (Property *)RTStrSpaceGet(&mhProperties, pszName);
+ }
+
+public:
+ explicit Service(PVBOXHGCMSVCHELPERS pHelpers)
+ : mpHelpers(pHelpers)
+ , mfGlobalFlags(GUEST_PROP_F_NILFLAG)
+ , mhProperties(NULL)
+ , mcProperties(0)
+ , mpfnHostCallback(NULL)
+ , mpvHostData(NULL)
+ , mPrevTimestamp(0)
+ , mcTimestampAdjustments(0)
+ , m_fSetHostVersionProps(false)
+ , mhThreadNotifyHost(NIL_RTTHREAD)
+ , mhReqQNotifyHost(NIL_RTREQQUEUE)
+ { }
+
+ /**
+ * @interface_method_impl{VBOXHGCMSVCFNTABLE,pfnUnload}
+ * Simply deletes the service object
+ */
+ static DECLCALLBACK(int) svcUnload(void *pvService)
+ {
+ AssertLogRelReturn(VALID_PTR(pvService), VERR_INVALID_PARAMETER);
+ SELF *pSelf = reinterpret_cast<SELF *>(pvService);
+ int rc = pSelf->uninit();
+ AssertRC(rc);
+ if (RT_SUCCESS(rc))
+ delete pSelf;
+ return rc;
+ }
+
+ /**
+ * @interface_method_impl{VBOXHGCMSVCFNTABLE,pfnConnect}
+ * Stub implementation of pfnConnect.
+ */
+ static DECLCALLBACK(int) svcConnect(void * /* pvService */,
+ uint32_t /* u32ClientID */,
+ void * /* pvClient */,
+ uint32_t /*fRequestor*/,
+ bool /*fRestoring*/)
+ {
+ return VINF_SUCCESS;
+ }
+
+ static DECLCALLBACK(int) svcDisconnect(void *pvService, uint32_t idClient, void *pvClient);
+
+ /**
+ * @interface_method_impl{VBOXHGCMSVCFNTABLE,pfnCall}
+ * Wraps to the call member function
+ */
+ static DECLCALLBACK(void) svcCall(void * pvService,
+ VBOXHGCMCALLHANDLE callHandle,
+ uint32_t u32ClientID,
+ void *pvClient,
+ uint32_t u32Function,
+ uint32_t cParms,
+ VBOXHGCMSVCPARM paParms[],
+ uint64_t tsArrival)
+ {
+ AssertLogRelReturnVoid(VALID_PTR(pvService));
+ LogFlowFunc(("pvService=%p, callHandle=%p, u32ClientID=%u, pvClient=%p, u32Function=%u, cParms=%u, paParms=%p\n", pvService, callHandle, u32ClientID, pvClient, u32Function, cParms, paParms));
+ SELF *pSelf = reinterpret_cast<SELF *>(pvService);
+ pSelf->call(callHandle, u32ClientID, pvClient, u32Function, cParms, paParms);
+ LogFlowFunc(("returning\n"));
+ RT_NOREF_PV(tsArrival);
+ }
+
+ /**
+ * @interface_method_impl{VBOXHGCMSVCFNTABLE,pfnHostCall}
+ * Wraps to the hostCall member function
+ */
+ static DECLCALLBACK(int) svcHostCall(void *pvService,
+ uint32_t u32Function,
+ uint32_t cParms,
+ VBOXHGCMSVCPARM paParms[])
+ {
+ AssertLogRelReturn(VALID_PTR(pvService), VERR_INVALID_PARAMETER);
+ LogFlowFunc(("pvService=%p, u32Function=%u, cParms=%u, paParms=%p\n", pvService, u32Function, cParms, paParms));
+ SELF *pSelf = reinterpret_cast<SELF *>(pvService);
+ int rc = pSelf->hostCall(u32Function, cParms, paParms);
+ LogFlowFunc(("rc=%Rrc\n", rc));
+ return rc;
+ }
+
+ /**
+ * @interface_method_impl{VBOXHGCMSVCFNTABLE,pfnRegisterExtension}
+ * Installs a host callback for notifications of property changes.
+ */
+ static DECLCALLBACK(int) svcRegisterExtension(void *pvService,
+ PFNHGCMSVCEXT pfnExtension,
+ void *pvExtension)
+ {
+ AssertLogRelReturn(VALID_PTR(pvService), VERR_INVALID_PARAMETER);
+ SELF *pSelf = reinterpret_cast<SELF *>(pvService);
+ pSelf->mpfnHostCallback = pfnExtension;
+ pSelf->mpvHostData = pvExtension;
+ return VINF_SUCCESS;
+ }
+
+ int setHostVersionProps();
+ void incrementCounterProp(const char *pszName);
+ static DECLCALLBACK(void) svcNotify(void *pvService, HGCMNOTIFYEVENT enmEvent);
+
+ int initialize();
+
+private:
+ static DECLCALLBACK(int) reqThreadFn(RTTHREAD ThreadSelf, void *pvUser);
+ uint64_t getCurrentTimestamp(void);
+ int validateName(const char *pszName, uint32_t cbName);
+ int validateValue(const char *pszValue, uint32_t cbValue);
+ int setPropertyBlock(uint32_t cParms, VBOXHGCMSVCPARM paParms[]);
+ int getProperty(uint32_t cParms, VBOXHGCMSVCPARM paParms[]);
+ int setProperty(uint32_t cParms, VBOXHGCMSVCPARM paParms[], bool isGuest);
+ int setPropertyInternal(const char *pcszName, const char *pcszValue, uint32_t fFlags, uint64_t nsTimestamp,
+ bool fIsGuest = false);
+ int delProperty(uint32_t cParms, VBOXHGCMSVCPARM paParms[], bool isGuest);
+ int enumProps(uint32_t cParms, VBOXHGCMSVCPARM paParms[]);
+ int getNotification(uint32_t u32ClientId, VBOXHGCMCALLHANDLE callHandle, uint32_t cParms, VBOXHGCMSVCPARM paParms[]);
+ int getOldNotificationInternal(const char *pszPattern, uint64_t nsTimestamp, Property *pProp);
+ int getNotificationWriteOut(uint32_t cParms, VBOXHGCMSVCPARM paParms[], Property const &prop);
+ int doNotifications(const char *pszProperty, uint64_t nsTimestamp);
+ int notifyHost(const char *pszName, const char *pszValue, uint64_t nsTimestamp, const char *pszFlags);
+
+ void call(VBOXHGCMCALLHANDLE callHandle, uint32_t u32ClientID,
+ void *pvClient, uint32_t eFunction, uint32_t cParms,
+ VBOXHGCMSVCPARM paParms[]);
+ int hostCall(uint32_t eFunction, uint32_t cParms, VBOXHGCMSVCPARM paParms[]);
+ int uninit();
+ static DECLCALLBACK(void) dbgInfo(void *pvUser, PCDBGFINFOHLP pHlp, const char *pszArgs);
+
+ /* Thread for handling host notifications. */
+ RTTHREAD mhThreadNotifyHost;
+ /* Queue for handling requests for notifications. */
+ RTREQQUEUE mhReqQNotifyHost;
+ static DECLCALLBACK(int) threadNotifyHost(RTTHREAD self, void *pvUser);
+
+ DECLARE_CLS_COPY_CTOR_ASSIGN_NOOP(Service);
+};
+
+
+/**
+ * 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.
+ */
+uint64_t Service::getCurrentTimestamp(void)
+{
+ RTTIMESPEC time;
+ uint64_t u64NanoTS = RTTimeSpecGetNano(RTTimeNow(&time));
+ if (mPrevTimestamp - u64NanoTS > mcTimestampAdjustments)
+ mcTimestampAdjustments = 0;
+ else
+ {
+ mcTimestampAdjustments++;
+ u64NanoTS = mPrevTimestamp + 1;
+ }
+ 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 Service::validateName(const char *pszName, uint32_t cbName)
+{
+ LogFlowFunc(("cbName=%d\n", cbName));
+ int rc = VINF_SUCCESS;
+ if (RT_SUCCESS(rc) && (cbName < 2))
+ rc = VERR_INVALID_PARAMETER;
+ for (unsigned i = 0; RT_SUCCESS(rc) && i < cbName; ++i)
+ if (pszName[i] == '*' || pszName[i] == '?' || pszName[i] == '|')
+ rc = VERR_INVALID_PARAMETER;
+ LogFlowFunc(("returning %Rrc\n", rc));
+ 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 Service::validateValue(const char *pszValue, uint32_t cbValue)
+{
+ LogFlowFunc(("cbValue=%d\n", cbValue)); RT_NOREF1(pszValue);
+
+ int rc = VINF_SUCCESS;
+ if (RT_SUCCESS(rc) && cbValue == 0)
+ rc = VERR_INVALID_PARAMETER;
+ if (RT_SUCCESS(rc))
+ LogFlow((" pszValue=%s\n", cbValue > 0 ? pszValue : NULL));
+ LogFlowFunc(("returning %Rrc\n", 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
+ */
+int Service::setPropertyBlock(uint32_t cParms, VBOXHGCMSVCPARM paParms[])
+{
+ const char **papszNames;
+ const char **papszValues;
+ const char **papszFlags;
+ uint64_t *paNsTimestamps;
+ uint32_t cbDummy;
+ int rc = VINF_SUCCESS;
+
+ /*
+ * Get and validate the parameters
+ */
+ if ( cParms != 4
+ || RT_FAILURE(HGCMSvcGetPv(&paParms[0], (void **)&papszNames, &cbDummy))
+ || RT_FAILURE(HGCMSvcGetPv(&paParms[1], (void **)&papszValues, &cbDummy))
+ || RT_FAILURE(HGCMSvcGetPv(&paParms[2], (void **)&paNsTimestamps, &cbDummy))
+ || RT_FAILURE(HGCMSvcGetPv(&paParms[3], (void **)&papszFlags, &cbDummy))
+ )
+ rc = VERR_INVALID_PARAMETER;
+ /** @todo validate the array sizes... */
+ else
+ {
+ for (unsigned i = 0; RT_SUCCESS(rc) && papszNames[i] != NULL; ++i)
+ {
+ if ( !RT_VALID_PTR(papszNames[i])
+ || !RT_VALID_PTR(papszValues[i])
+ || !RT_VALID_PTR(papszFlags[i])
+ )
+ rc = VERR_INVALID_POINTER;
+ else
+ {
+ uint32_t fFlagsIgn;
+ rc = GuestPropValidateFlags(papszFlags[i], &fFlagsIgn);
+ }
+ }
+ if (RT_SUCCESS(rc))
+ {
+ /*
+ * Add the properties. No way to roll back here.
+ */
+ for (unsigned i = 0; papszNames[i] != NULL; ++i)
+ {
+ uint32_t fFlags;
+ rc = GuestPropValidateFlags(papszFlags[i], &fFlags);
+ AssertRCBreak(rc);
+ /*
+ * Handle names which are read-only for the guest.
+ */
+ if (checkHostReserved(papszNames[i]))
+ fFlags |= GUEST_PROP_F_RDONLYGUEST;
+
+ Property *pProp = getPropertyInternal(papszNames[i]);
+ if (pProp)
+ {
+ /* Update existing property. */
+ rc = pProp->mValue.assignNoThrow(papszValues[i]);
+ AssertRCBreak(rc);
+ pProp->mTimestamp = paNsTimestamps[i];
+ pProp->mFlags = fFlags;
+ }
+ else
+ {
+ /* Create a new property */
+ try
+ {
+ pProp = new Property(papszNames[i], papszValues[i], paNsTimestamps[i], fFlags);
+ }
+ catch (std::bad_alloc &)
+ {
+ return VERR_NO_MEMORY;
+ }
+ if (RTStrSpaceInsert(&mhProperties, &pProp->mStrCore))
+ mcProperties++;
+ else
+ {
+ delete pProp;
+ rc = VERR_INTERNAL_ERROR_3;
+ AssertFailedBreak();
+ }
+ }
+ }
+ }
+ }
+
+ 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 Service::getProperty(uint32_t cParms, VBOXHGCMSVCPARM paParms[])
+{
+ int rc;
+ const char *pcszName = NULL; /* shut up gcc */
+ char *pchBuf = NULL; /* shut up MSC */
+ uint32_t cbName;
+ uint32_t cbBuf = 0; /* shut up MSC */
+
+ /*
+ * Get and validate the parameters
+ */
+ LogFlowThisFunc(("\n"));
+ if ( cParms != 4 /* Hardcoded value as the next lines depend on it. */
+ || RT_FAILURE(HGCMSvcGetCStr(&paParms[0], &pcszName, &cbName)) /* name */
+ || RT_FAILURE(HGCMSvcGetBuf(&paParms[1], (void **)&pchBuf, &cbBuf)) /* buffer */
+ )
+ rc = VERR_INVALID_PARAMETER;
+ else
+ rc = validateName(pcszName, cbName);
+ if (RT_FAILURE(rc))
+ {
+ LogFlowThisFunc(("rc = %Rrc\n", rc));
+ return rc;
+ }
+
+ /*
+ * Read and set the values we will return
+ */
+
+ /* Get the property. */
+ Property *pProp = getPropertyInternal(pcszName);
+ if (pProp)
+ {
+ char szFlags[GUEST_PROP_MAX_FLAGS_LEN];
+ rc = GuestPropWriteFlags(pProp->mFlags, szFlags);
+ if (RT_SUCCESS(rc))
+ {
+ /* Check that the buffer is big enough */
+ size_t const cbFlags = strlen(szFlags) + 1;
+ size_t const cbValue = pProp->mValue.length() + 1;
+ size_t const cbNeeded = cbValue + cbFlags;
+ HGCMSvcSetU32(&paParms[3], (uint32_t)cbNeeded);
+ if (cbBuf >= cbNeeded)
+ {
+ /* Write the value, flags and timestamp */
+ memcpy(pchBuf, pProp->mValue.c_str(), cbValue);
+ memcpy(pchBuf + cbValue, szFlags, cbFlags);
+
+ HGCMSvcSetU64(&paParms[2], pProp->mTimestamp);
+
+ /*
+ * Done! Do exit logging and return.
+ */
+ Log2(("Queried string %s, value=%s, timestamp=%lld, flags=%s\n",
+ pcszName, pProp->mValue.c_str(), pProp->mTimestamp, szFlags));
+ }
+ else
+ rc = VERR_BUFFER_OVERFLOW;
+ }
+ }
+ else
+ rc = VERR_NOT_FOUND;
+
+ LogFlowThisFunc(("rc = %Rrc (%s)\n", rc, pcszName));
+ 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 Service::setProperty(uint32_t cParms, VBOXHGCMSVCPARM paParms[], bool isGuest)
+{
+ int rc = VINF_SUCCESS;
+ const char *pcszName = NULL; /* shut up gcc */
+ const char *pcszValue = NULL; /* ditto */
+ const char *pcszFlags = NULL;
+ uint32_t cchName = 0; /* ditto */
+ uint32_t cchValue = 0; /* ditto */
+ uint32_t cchFlags = 0;
+ uint32_t fFlags = GUEST_PROP_F_NILFLAG;
+ uint64_t u64TimeNano = getCurrentTimestamp();
+
+ LogFlowThisFunc(("\n"));
+
+ /*
+ * General parameter correctness checking.
+ */
+ if ( RT_SUCCESS(rc)
+ && ( (cParms < 2) || (cParms > 3) /* Hardcoded value as the next lines depend on it. */
+ || RT_FAILURE(HGCMSvcGetCStr(&paParms[0], &pcszName, &cchName)) /* name */
+ || RT_FAILURE(HGCMSvcGetCStr(&paParms[1], &pcszValue, &cchValue)) /* value */
+ || ( (3 == cParms)
+ && RT_FAILURE(HGCMSvcGetCStr(&paParms[2], &pcszFlags, &cchFlags)) /* flags */
+ )
+ )
+ )
+ rc = VERR_INVALID_PARAMETER;
+
+ /*
+ * Check the values passed in the parameters for correctness.
+ */
+ if (RT_SUCCESS(rc))
+ rc = validateName(pcszName, cchName);
+ if (RT_SUCCESS(rc))
+ rc = validateValue(pcszValue, cchValue);
+ if ((3 == cParms) && RT_SUCCESS(rc))
+ rc = RTStrValidateEncodingEx(pcszFlags, cchFlags,
+ RTSTR_VALIDATE_ENCODING_ZERO_TERMINATED);
+ if ((3 == cParms) && RT_SUCCESS(rc))
+ rc = GuestPropValidateFlags(pcszFlags, &fFlags);
+ if (RT_FAILURE(rc))
+ {
+ LogFlowThisFunc(("rc = %Rrc\n", rc));
+ return rc;
+ }
+
+ /*
+ * Hand it over to the internal setter method.
+ */
+ rc = setPropertyInternal(pcszName, pcszValue, fFlags, u64TimeNano, isGuest);
+
+ LogFlowThisFunc(("%s=%s, rc=%Rrc\n", pcszName, pcszValue, rc));
+ return rc;
+}
+
+/**
+ * Internal property setter.
+ *
+ * @returns VBox status code.
+ * @param pcszName The property name.
+ * @param pcszValue The new value.
+ * @param fFlags The flags.
+ * @param nsTimestamp The timestamp.
+ * @param fIsGuest Is it the guest calling.
+ * @throws std::bad_alloc if an out of memory condition occurs
+ * @thread HGCM
+ */
+int Service::setPropertyInternal(const char *pcszName, const char *pcszValue, uint32_t fFlags, uint64_t nsTimestamp,
+ bool fIsGuest /*= false*/)
+{
+ /*
+ * If the property already exists, check its flags to see if we are allowed
+ * to change it.
+ */
+ Property *pProp = getPropertyInternal(pcszName);
+ int rc = checkPermission(pProp ? pProp->mFlags : GUEST_PROP_F_NILFLAG, fIsGuest);
+ /*
+ * Handle names which are read-only for the guest.
+ */
+ if (rc == VINF_SUCCESS && checkHostReserved(pcszName))
+ {
+ if (fIsGuest)
+ rc = VERR_PERMISSION_DENIED;
+ else
+ fFlags |= GUEST_PROP_F_RDONLYGUEST;
+ }
+ if (rc == VINF_SUCCESS)
+ {
+ /*
+ * Set the actual value
+ */
+ if (pProp)
+ {
+ rc = pProp->mValue.assignNoThrow(pcszValue);
+ if (RT_SUCCESS(rc))
+ {
+ pProp->mTimestamp = nsTimestamp;
+ pProp->mFlags = fFlags;
+ }
+ }
+ else if (mcProperties < GUEST_PROP_MAX_PROPS)
+ {
+ try
+ {
+ /* Create a new string space record. */
+ pProp = new Property(pcszName, pcszValue, nsTimestamp, fFlags);
+ AssertPtr(pProp);
+
+ if (RTStrSpaceInsert(&mhProperties, &pProp->mStrCore))
+ mcProperties++;
+ else
+ {
+ AssertFailed();
+ delete pProp;
+
+ rc = VERR_ALREADY_EXISTS;
+ }
+ }
+ catch (std::bad_alloc &)
+ {
+ rc = VERR_NO_MEMORY;
+ }
+ }
+ else
+ rc = VERR_TOO_MUCH_DATA;
+
+ /*
+ * Send a notification to the guest and host and return.
+ */
+ // if (fIsGuest) /* Notify the host even for properties that the host
+ // * changed. Less efficient, but ensures consistency. */
+ int rc2 = doNotifications(pcszName, nsTimestamp);
+ if (RT_SUCCESS(rc))
+ rc = rc2;
+ }
+
+ LogFlowThisFunc(("%s=%s, rc=%Rrc\n", pcszName, pcszValue, rc));
+ 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 Service::delProperty(uint32_t cParms, VBOXHGCMSVCPARM paParms[], bool isGuest)
+{
+ int rc;
+ const char *pcszName = NULL; /* shut up gcc */
+ uint32_t cbName;
+
+ LogFlowThisFunc(("\n"));
+
+ /*
+ * Check the user-supplied parameters.
+ */
+ if ( (cParms == 1) /* Hardcoded value as the next lines depend on it. */
+ && RT_SUCCESS(HGCMSvcGetCStr(&paParms[0], &pcszName, &cbName)) /* name */
+ )
+ rc = validateName(pcszName, cbName);
+ else
+ rc = VERR_INVALID_PARAMETER;
+ if (RT_FAILURE(rc))
+ {
+ LogFlowThisFunc(("rc=%Rrc\n", rc));
+ return rc;
+ }
+
+ /*
+ * If the property exists, check its flags to see if we are allowed
+ * to change it.
+ */
+ Property *pProp = getPropertyInternal(pcszName);
+ if (pProp)
+ rc = checkPermission(pProp->mFlags, isGuest);
+
+ /*
+ * And delete the property if all is well.
+ */
+ if (rc == VINF_SUCCESS && pProp)
+ {
+ uint64_t nsTimestamp = getCurrentTimestamp();
+ PRTSTRSPACECORE pStrCore = RTStrSpaceRemove(&mhProperties, pProp->mStrCore.pszString);
+ AssertPtr(pStrCore); NOREF(pStrCore);
+ mcProperties--;
+ delete pProp;
+ // if (isGuest) /* Notify the host even for properties that the host
+ // * changed. Less efficient, but ensures consistency. */
+ int rc2 = doNotifications(pcszName, nsTimestamp);
+ if (RT_SUCCESS(rc))
+ rc = rc2;
+ }
+
+ LogFlowThisFunc(("%s: rc=%Rrc\n", pcszName, rc));
+ 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. */
+ size_t cbLeft; /**< The amount of available buffer space. */
+ size_t cbNeeded; /**< The amount of needed buffer space. */
+} ENUMDATA;
+
+/**
+ * @callback_method_impl{FNRTSTRSPACECALLBACK}
+ */
+static DECLCALLBACK(int) enumPropsCallback(PRTSTRSPACECORE pStr, void *pvUser)
+{
+ Property *pProp = (Property *)pStr;
+ ENUMDATA *pEnum = (ENUMDATA *)pvUser;
+
+ /* Included in the enumeration? */
+ if (!pProp->Matches(pEnum->pszPattern))
+ return 0;
+
+ /* Convert the non-string members into strings. */
+ char szTimestamp[256];
+ size_t const cbTimestamp = RTStrFormatNumber(szTimestamp, pProp->mTimestamp, 10, 0, 0, 0) + 1;
+
+ char szFlags[GUEST_PROP_MAX_FLAGS_LEN];
+ int rc = GuestPropWriteFlags(pProp->mFlags, szFlags);
+ if (RT_FAILURE(rc))
+ return rc;
+ size_t const cbFlags = strlen(szFlags) + 1;
+
+ /* Calculate the buffer space requirements. */
+ size_t const cbName = pProp->mName.length() + 1;
+ size_t const cbValue = pProp->mValue.length() + 1;
+ size_t const cbRequired = cbName + cbValue + cbTimestamp + cbFlags;
+ pEnum->cbNeeded += cbRequired;
+
+ /* Sufficient buffer space? */
+ if (cbRequired > pEnum->cbLeft)
+ {
+ pEnum->cbLeft = 0;
+ return 0; /* don't quit */
+ }
+ pEnum->cbLeft -= cbRequired;
+
+ /* Append the property to the buffer. */
+ char *pchCur = pEnum->pchCur;
+ pEnum->pchCur += cbRequired;
+
+ memcpy(pchCur, pProp->mName.c_str(), cbName);
+ pchCur += cbName;
+
+ memcpy(pchCur, pProp->mValue.c_str(), cbValue);
+ pchCur += cbValue;
+
+ memcpy(pchCur, szTimestamp, cbTimestamp);
+ pchCur += cbTimestamp;
+
+ memcpy(pchCur, szFlags, cbFlags);
+ pchCur += cbFlags;
+
+ Assert(pchCur == pEnum->pchCur);
+ 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 Service::enumProps(uint32_t cParms, VBOXHGCMSVCPARM paParms[])
+{
+ int rc = VINF_SUCCESS;
+
+ /*
+ * Get the HGCM function arguments.
+ */
+ char const *pchPatterns = NULL;
+ char *pchBuf = NULL;
+ uint32_t cbPatterns = 0;
+ uint32_t cbBuf = 0;
+ LogFlowThisFunc(("\n"));
+ if ( (cParms != 3) /* Hardcoded value as the next lines depend on it. */
+ || RT_FAILURE(HGCMSvcGetCStr(&paParms[0], &pchPatterns, &cbPatterns)) /* patterns */
+ || RT_FAILURE(HGCMSvcGetBuf(&paParms[1], (void **)&pchBuf, &cbBuf)) /* return buffer */
+ )
+ rc = VERR_INVALID_PARAMETER;
+ if (RT_SUCCESS(rc) && cbPatterns > GUEST_PROP_MAX_PATTERN_LEN)
+ rc = VERR_TOO_MUCH_DATA;
+
+ /*
+ * First repack the patterns into the format expected by RTStrSimplePatternMatch()
+ */
+ char szPatterns[GUEST_PROP_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] = '|';
+ szPatterns[cbPatterns - 1] = '\0';
+ }
+
+ /*
+ * Next enumerate into the buffer.
+ */
+ if (RT_SUCCESS(rc))
+ {
+ ENUMDATA EnumData;
+ EnumData.pszPattern = szPatterns;
+ EnumData.pchCur = pchBuf;
+ EnumData.cbLeft = cbBuf;
+ EnumData.cbNeeded = 0;
+ rc = RTStrSpaceEnumerate(&mhProperties, enumPropsCallback, &EnumData);
+ AssertRCSuccess(rc);
+ if (RT_SUCCESS(rc))
+ {
+ HGCMSvcSetU32(&paParms[2], (uint32_t)(EnumData.cbNeeded + 4));
+ if (EnumData.cbLeft >= 4)
+ {
+ /* The final terminators. */
+ EnumData.pchCur[0] = '\0';
+ EnumData.pchCur[1] = '\0';
+ EnumData.pchCur[2] = '\0';
+ EnumData.pchCur[3] = '\0';
+ }
+ else
+ rc = VERR_BUFFER_OVERFLOW;
+ }
+ }
+
+ return rc;
+}
+
+
+/** Helper query used by getOldNotification
+ * @throws nothing
+ */
+int Service::getOldNotificationInternal(const char *pszPatterns, uint64_t nsTimestamp, Property *pProp)
+{
+ /* We count backwards, as the guest should normally be querying the
+ * most recent events. */
+ int rc = VWRN_NOT_FOUND;
+ PropertyList::reverse_iterator it = mGuestNotifications.rbegin();
+ for (; it != mGuestNotifications.rend(); ++it)
+ if (it->mTimestamp == nsTimestamp)
+ {
+ rc = VINF_SUCCESS;
+ break;
+ }
+
+ /* Now look for an event matching the patterns supplied. The base()
+ * member conveniently points to the following element. */
+ PropertyList::iterator base = it.base();
+ for (; base != mGuestNotifications.end(); ++base)
+ if (base->Matches(pszPatterns))
+ {
+ try
+ {
+ *pProp = *base;
+ }
+ catch (std::bad_alloc &)
+ {
+ rc = VERR_NO_MEMORY;
+ }
+ return rc;
+ }
+ *pProp = Property();
+ return rc;
+}
+
+
+/** Helper query used by getNotification */
+int Service::getNotificationWriteOut(uint32_t cParms, VBOXHGCMSVCPARM paParms[], Property const &rProp)
+{
+ AssertReturn(cParms == 4, VERR_INVALID_PARAMETER); /* Basic sanity checking. */
+
+ /* Format the data to write to the buffer. */
+ char *pchBuf;
+ uint32_t cbBuf;
+ int rc = HGCMSvcGetBuf(&paParms[2], (void **)&pchBuf, &cbBuf);
+ if (RT_SUCCESS(rc))
+ {
+ char szFlags[GUEST_PROP_MAX_FLAGS_LEN];
+ rc = GuestPropWriteFlags(rProp.mFlags, szFlags);
+ if (RT_SUCCESS(rc))
+ {
+ HGCMSvcSetU64(&paParms[1], rProp.mTimestamp);
+
+ size_t const cbFlags = strlen(szFlags) + 1;
+ size_t const cbName = rProp.mName.length() + 1;
+ size_t const cbValue = rProp.mValue.length() + 1;
+ size_t const cbNeeded = cbName + cbValue + cbFlags;
+ HGCMSvcSetU32(&paParms[3], (uint32_t)cbNeeded);
+ if (cbNeeded <= cbBuf)
+ {
+ memcpy(pchBuf, rProp.mName.c_str(), cbName);
+ pchBuf += cbName;
+ memcpy(pchBuf, rProp.mValue.c_str(), cbValue);
+ pchBuf += cbValue;
+ memcpy(pchBuf, szFlags, cbFlags);
+ }
+ else
+ rc = VERR_BUFFER_OVERFLOW;
+ }
+ }
+ return rc;
+}
+
+
+/**
+ * Get the next guest notification.
+ *
+ * @returns iprt status value
+ * @param u32ClientId the client ID
+ * @param callHandle handle
+ * @param cParms the number of HGCM parameters supplied
+ * @param paParms the array of HGCM parameters
+ * @thread HGCM
+ * @throws nothing
+ */
+int Service::getNotification(uint32_t u32ClientId, VBOXHGCMCALLHANDLE callHandle,
+ uint32_t cParms, VBOXHGCMSVCPARM paParms[])
+{
+ int rc = VINF_SUCCESS;
+ char *pszPatterns = NULL; /* shut up gcc */
+ char *pchBuf;
+ uint32_t cchPatterns = 0;
+ uint32_t cbBuf = 0;
+ uint64_t nsTimestamp;
+
+ /*
+ * Get the HGCM function arguments and perform basic verification.
+ */
+ LogFlowThisFunc(("\n"));
+ if ( cParms != 4 /* Hardcoded value as the next lines depend on it. */
+ || RT_FAILURE(HGCMSvcGetStr(&paParms[0], &pszPatterns, &cchPatterns)) /* patterns */
+ || RT_FAILURE(HGCMSvcGetU64(&paParms[1], &nsTimestamp)) /* timestamp */
+ || RT_FAILURE(HGCMSvcGetBuf(&paParms[2], (void **)&pchBuf, &cbBuf)) /* return buffer */
+ )
+ rc = VERR_INVALID_PARAMETER;
+ else
+ {
+ LogFlow(("pszPatterns=%s, nsTimestamp=%llu\n", pszPatterns, nsTimestamp));
+
+ /*
+ * If no timestamp was supplied or no notification was found in the queue
+ * of old notifications, enqueue the request in the waiting queue.
+ */
+ Property prop;
+ if (RT_SUCCESS(rc) && nsTimestamp != 0)
+ rc = getOldNotification(pszPatterns, nsTimestamp, &prop);
+ if (RT_SUCCESS(rc))
+ {
+ if (prop.isNull())
+ {
+ /*
+ * Check if the client already had the same request.
+ * Complete the old request with an error in this case.
+ * Protection against clients, which cancel and resubmits requests.
+ */
+ uint32_t cPendingWaits = 0;
+ CallList::iterator it = mGuestWaiters.begin();
+ while (it != mGuestWaiters.end())
+ {
+ if (u32ClientId == it->u32ClientId)
+ {
+ const char *pszPatternsExisting;
+ uint32_t cchPatternsExisting;
+ int rc3 = HGCMSvcGetCStr(&it->mParms[0], &pszPatternsExisting, &cchPatternsExisting);
+ if ( RT_SUCCESS(rc3)
+ && RTStrCmp(pszPatterns, pszPatternsExisting) == 0)
+ {
+ /* Complete the old request. */
+ mpHelpers->pfnCallComplete(it->mHandle, VERR_INTERRUPTED);
+ it = mGuestWaiters.erase(it);
+ }
+ else if (mpHelpers->pfnIsCallCancelled(it->mHandle))
+ {
+ /* Cleanup cancelled request. */
+ mpHelpers->pfnCallComplete(it->mHandle, VERR_INTERRUPTED);
+ it = mGuestWaiters.erase(it);
+ }
+ else
+ {
+ /** @todo check if cancelled. */
+ cPendingWaits++;
+ ++it;
+ }
+ }
+ else
+ ++it;
+ }
+
+ if (cPendingWaits < GUEST_PROP_MAX_GUEST_CONCURRENT_WAITS)
+ {
+ try
+ {
+ mGuestWaiters.push_back(GuestCall(u32ClientId, callHandle, GUEST_PROP_FN_GET_NOTIFICATION,
+ cParms, paParms, rc));
+ rc = VINF_HGCM_ASYNC_EXECUTE;
+ }
+ catch (std::bad_alloc &)
+ {
+ rc = VERR_NO_MEMORY;
+ }
+ }
+ else
+ {
+ LogFunc(("Too many pending waits already!\n"));
+ rc = VERR_OUT_OF_RESOURCES;
+ }
+ }
+ /*
+ * Otherwise reply at once with the enqueued notification we found.
+ */
+ else
+ {
+ int rc2 = getNotificationWriteOut(cParms, paParms, prop);
+ if (RT_FAILURE(rc2))
+ rc = rc2;
+ }
+ }
+ }
+
+ LogFlowThisFunc(("returning rc=%Rrc\n", rc));
+ return rc;
+}
+
+
+/**
+ * Notify the service owner and the guest that a property has been
+ * added/deleted/changed
+ *
+ * @param pszProperty The name of the property which has changed.
+ * @param nsTimestamp The time at which the change took place.
+ * @throws nothing.
+ * @thread HGCM service
+ */
+int Service::doNotifications(const char *pszProperty, uint64_t nsTimestamp)
+{
+ AssertPtrReturn(pszProperty, VERR_INVALID_POINTER);
+ LogFlowThisFunc(("pszProperty=%s, nsTimestamp=%llu\n", pszProperty, nsTimestamp));
+ /* Ensure that our timestamp is different to the last one. */
+ if ( !mGuestNotifications.empty()
+ && nsTimestamp == mGuestNotifications.back().mTimestamp)
+ ++nsTimestamp;
+
+ /*
+ * Don't keep too many changes around.
+ */
+ if (mGuestNotifications.size() >= GUEST_PROP_MAX_GUEST_NOTIFICATIONS)
+ mGuestNotifications.pop_front();
+
+ /*
+ * Try to find the property. Create a change event if we find it and a
+ * delete event if we do not.
+ */
+ Property prop;
+ int rc = prop.mName.assignNoThrow(pszProperty);
+ AssertRCReturn(rc, rc);
+ prop.mTimestamp = nsTimestamp;
+ /* prop is currently a delete event for pszProperty */
+ Property const * const pProp = getPropertyInternal(pszProperty);
+ if (pProp)
+ {
+ /* Make prop into a change event. */
+ rc = prop.mValue.assignNoThrow(pProp->mValue);
+ AssertRCReturn(rc, rc);
+ prop.mFlags = pProp->mFlags;
+ }
+
+ /* Release guest waiters if applicable and add the event
+ * to the queue for guest notifications */
+ CallList::iterator it = mGuestWaiters.begin();
+ if (it != mGuestWaiters.end())
+ {
+ const char *pszPatterns;
+ uint32_t cchPatterns;
+ HGCMSvcGetCStr(&it->mParms[0], &pszPatterns, &cchPatterns);
+
+ while (it != mGuestWaiters.end())
+ {
+ if (prop.Matches(pszPatterns))
+ {
+ int rc2 = getNotificationWriteOut(it->mParmsCnt, it->mParms, prop);
+ if (RT_SUCCESS(rc2))
+ rc2 = it->mRc;
+ mpHelpers->pfnCallComplete(it->mHandle, rc2);
+ it = mGuestWaiters.erase(it);
+ }
+ else
+ ++it;
+ }
+ }
+
+ try
+ {
+ mGuestNotifications.push_back(prop);
+ }
+ catch (std::bad_alloc &)
+ {
+ rc = VERR_NO_MEMORY;
+ }
+
+ if ( RT_SUCCESS(rc)
+ && mpfnHostCallback)
+ {
+ /*
+ * Host notifications - first case: if the property exists then send its
+ * current value
+ */
+ if (pProp)
+ {
+ char szFlags[GUEST_PROP_MAX_FLAGS_LEN];
+ /* Send out a host notification */
+ const char *pszValue = prop.mValue.c_str();
+ rc = GuestPropWriteFlags(prop.mFlags, szFlags);
+ if (RT_SUCCESS(rc))
+ rc = notifyHost(pszProperty, pszValue, nsTimestamp, szFlags);
+ }
+ /*
+ * Host notifications - second case: if the property does not exist then
+ * send the host an empty value
+ */
+ else
+ {
+ /* Send out a host notification */
+ rc = notifyHost(pszProperty, "", nsTimestamp, "");
+ }
+ }
+
+ LogFlowThisFunc(("returning rc=%Rrc\n", rc));
+ return rc;
+}
+
+static DECLCALLBACK(void)
+notifyHostAsyncWorker(PFNHGCMSVCEXT pfnHostCallback, void *pvHostData, PGUESTPROPHOSTCALLBACKDATA pHostCallbackData)
+{
+ pfnHostCallback(pvHostData, 0 /*u32Function*/, (void *)pHostCallbackData, sizeof(GUESTPROPHOSTCALLBACKDATA));
+ RTMemFree(pHostCallbackData);
+}
+
+/**
+ * Notify the service owner that a property has been added/deleted/changed.
+ * @returns IPRT status value
+ * @param pszName the property name
+ * @param pszValue the new value, or NULL if the property was deleted
+ * @param nsTimestamp the time of the change
+ * @param pszFlags the new flags string
+ */
+int Service::notifyHost(const char *pszName, const char *pszValue, uint64_t nsTimestamp, const char *pszFlags)
+{
+ LogFlowFunc(("pszName=%s, pszValue=%s, nsTimestamp=%llu, pszFlags=%s\n", pszName, pszValue, nsTimestamp, pszFlags));
+ int rc;
+
+ /* Allocate buffer for the callback data and strings. */
+ size_t cbName = pszName? strlen(pszName): 0;
+ size_t cbValue = pszValue? strlen(pszValue): 0;
+ size_t cbFlags = pszFlags? strlen(pszFlags): 0;
+ size_t cbAlloc = sizeof(GUESTPROPHOSTCALLBACKDATA) + cbName + cbValue + cbFlags + 3;
+ PGUESTPROPHOSTCALLBACKDATA pHostCallbackData = (PGUESTPROPHOSTCALLBACKDATA)RTMemAlloc(cbAlloc);
+ if (pHostCallbackData)
+ {
+ uint8_t *pu8 = (uint8_t *)pHostCallbackData;
+ pu8 += sizeof(GUESTPROPHOSTCALLBACKDATA);
+
+ pHostCallbackData->u32Magic = GUESTPROPHOSTCALLBACKDATA_MAGIC;
+
+ pHostCallbackData->pcszName = (const char *)pu8;
+ memcpy(pu8, pszName, cbName);
+ pu8 += cbName;
+ *pu8++ = 0;
+
+ pHostCallbackData->pcszValue = (const char *)pu8;
+ memcpy(pu8, pszValue, cbValue);
+ pu8 += cbValue;
+ *pu8++ = 0;
+
+ pHostCallbackData->u64Timestamp = nsTimestamp;
+
+ pHostCallbackData->pcszFlags = (const char *)pu8;
+ memcpy(pu8, pszFlags, cbFlags);
+ pu8 += cbFlags;
+ *pu8++ = 0;
+
+ rc = RTReqQueueCallEx(mhReqQNotifyHost, NULL, 0, RTREQFLAGS_VOID | RTREQFLAGS_NO_WAIT,
+ (PFNRT)notifyHostAsyncWorker, 3,
+ mpfnHostCallback, mpvHostData, pHostCallbackData);
+ if (RT_FAILURE(rc))
+ {
+ RTMemFree(pHostCallbackData);
+ }
+ }
+ else
+ {
+ rc = VERR_NO_MEMORY;
+ }
+ LogFlowFunc(("returning rc=%Rrc\n", rc));
+ return rc;
+}
+
+
+/**
+ * Handle an HGCM service call.
+ * @interface_method_impl{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
+ */
+void Service::call (VBOXHGCMCALLHANDLE callHandle, uint32_t u32ClientID,
+ void * /* pvClient */, uint32_t eFunction, uint32_t cParms,
+ VBOXHGCMSVCPARM paParms[])
+{
+ int rc;
+ LogFlowFunc(("u32ClientID = %d, fn = %d, cParms = %d, pparms = %p\n",
+ u32ClientID, eFunction, cParms, paParms));
+
+ switch (eFunction)
+ {
+ /* The guest wishes to read a property */
+ case GUEST_PROP_FN_GET_PROP:
+ LogFlowFunc(("GET_PROP\n"));
+ rc = getProperty(cParms, paParms);
+ break;
+
+ /* The guest wishes to set a property */
+ case GUEST_PROP_FN_SET_PROP:
+ LogFlowFunc(("SET_PROP\n"));
+ rc = setProperty(cParms, paParms, true);
+ break;
+
+ /* The guest wishes to set a property value */
+ case GUEST_PROP_FN_SET_PROP_VALUE:
+ LogFlowFunc(("SET_PROP_VALUE\n"));
+ rc = setProperty(cParms, paParms, true);
+ break;
+
+ /* The guest wishes to remove a configuration value */
+ case GUEST_PROP_FN_DEL_PROP:
+ LogFlowFunc(("DEL_PROP\n"));
+ rc = delProperty(cParms, paParms, true);
+ break;
+
+ /* The guest wishes to enumerate all properties */
+ case GUEST_PROP_FN_ENUM_PROPS:
+ LogFlowFunc(("ENUM_PROPS\n"));
+ rc = enumProps(cParms, paParms);
+ break;
+
+ /* The guest wishes to get the next property notification */
+ case GUEST_PROP_FN_GET_NOTIFICATION:
+ LogFlowFunc(("GET_NOTIFICATION\n"));
+ rc = getNotification(u32ClientID, callHandle, cParms, paParms);
+ break;
+
+ default:
+ rc = VERR_NOT_IMPLEMENTED;
+ }
+ LogFlowFunc(("rc = %Rrc\n", rc));
+ if (rc != VINF_HGCM_ASYNC_EXECUTE)
+ mpHelpers->pfnCallComplete(callHandle, rc);
+}
+
+/**
+ * Enumeration data shared between dbgInfoCallback and Service::dbgInfoShow.
+ */
+typedef struct ENUMDBGINFO
+{
+ PCDBGFINFOHLP pHlp;
+} ENUMDBGINFO;
+
+static DECLCALLBACK(int) dbgInfoCallback(PRTSTRSPACECORE pStr, void *pvUser)
+{
+ Property *pProp = (Property *)pStr;
+ PCDBGFINFOHLP pHlp = ((ENUMDBGINFO *)pvUser)->pHlp;
+
+ char szFlags[GUEST_PROP_MAX_FLAGS_LEN];
+ int rc = GuestPropWriteFlags(pProp->mFlags, szFlags);
+ if (RT_FAILURE(rc))
+ RTStrPrintf(szFlags, sizeof(szFlags), "???");
+
+ pHlp->pfnPrintf(pHlp, "%s: '%s', %RU64", pProp->mName.c_str(), pProp->mValue.c_str(), pProp->mTimestamp);
+ if (strlen(szFlags))
+ pHlp->pfnPrintf(pHlp, " (%s)", szFlags);
+ pHlp->pfnPrintf(pHlp, "\n");
+ return 0;
+}
+
+
+/**
+ * Handler for debug info.
+ *
+ * @param pvUser user pointer.
+ * @param pHlp The info helper functions.
+ * @param pszArgs Arguments, ignored.
+ */
+DECLCALLBACK(void) Service::dbgInfo(void *pvUser, PCDBGFINFOHLP pHlp, const char *pszArgs)
+{
+ RT_NOREF1(pszArgs);
+ SELF *pSelf = reinterpret_cast<SELF *>(pvUser);
+
+ ENUMDBGINFO EnumData = { pHlp };
+ RTStrSpaceEnumerate(&pSelf->mhProperties, dbgInfoCallback, &EnumData);
+}
+
+
+/**
+ * Service call handler for the host.
+ * @interface_method_impl{VBOXHGCMSVCFNTABLE,pfnHostCall}
+ * @thread hgcm
+ */
+int Service::hostCall (uint32_t eFunction, uint32_t cParms, VBOXHGCMSVCPARM paParms[])
+{
+ int rc;
+ LogFlowFunc(("fn = %d, cParms = %d, pparms = %p\n", eFunction, cParms, paParms));
+
+ switch (eFunction)
+ {
+ /* The host wishes to set a block of properties */
+ case GUEST_PROP_FN_HOST_SET_PROPS:
+ LogFlowFunc(("SET_PROPS_HOST\n"));
+ rc = setPropertyBlock(cParms, paParms);
+ break;
+
+ /* The host wishes to read a configuration value */
+ case GUEST_PROP_FN_HOST_GET_PROP:
+ LogFlowFunc(("GET_PROP_HOST\n"));
+ rc = getProperty(cParms, paParms);
+ break;
+
+ /* The host wishes to set a configuration value */
+ case GUEST_PROP_FN_HOST_SET_PROP:
+ LogFlowFunc(("SET_PROP_HOST\n"));
+ rc = setProperty(cParms, paParms, false);
+ break;
+
+ /* The host wishes to set a configuration value */
+ case GUEST_PROP_FN_HOST_SET_PROP_VALUE:
+ LogFlowFunc(("SET_PROP_VALUE_HOST\n"));
+ rc = setProperty(cParms, paParms, false);
+ break;
+
+ /* The host wishes to remove a configuration value */
+ case GUEST_PROP_FN_HOST_DEL_PROP:
+ LogFlowFunc(("DEL_PROP_HOST\n"));
+ rc = delProperty(cParms, paParms, false);
+ break;
+
+ /* The host wishes to enumerate all properties */
+ case GUEST_PROP_FN_HOST_ENUM_PROPS:
+ LogFlowFunc(("ENUM_PROPS\n"));
+ rc = enumProps(cParms, paParms);
+ break;
+
+ /* The host wishes to set global flags for the service */
+ case GUEST_PROP_FN_HOST_SET_GLOBAL_FLAGS:
+ LogFlowFunc(("SET_GLOBAL_FLAGS_HOST\n"));
+ if (cParms == 1)
+ {
+ uint32_t fFlags;
+ rc = HGCMSvcGetU32(&paParms[0], &fFlags);
+ if (RT_SUCCESS(rc))
+ mfGlobalFlags = fFlags;
+ }
+ else
+ rc = VERR_INVALID_PARAMETER;
+ break;
+
+ default:
+ rc = VERR_NOT_SUPPORTED;
+ break;
+ }
+
+ LogFlowFunc(("rc = %Rrc\n", rc));
+ return rc;
+}
+
+/**
+ * @interface_method_impl{VBOXHGCMSVCFNTABLE,pfnDisconnect}
+ */
+/*static*/ DECLCALLBACK(int) Service::svcDisconnect(void *pvService, uint32_t idClient, void *pvClient)
+{
+ RT_NOREF(pvClient);
+ LogFlowFunc(("idClient=%u\n", idClient));
+ SELF *pThis = reinterpret_cast<SELF *>(pvService);
+ AssertLogRelReturn(pThis, VERR_INVALID_POINTER);
+
+ /*
+ * Complete all pending requests for this client.
+ */
+ for (CallList::iterator It = pThis->mGuestWaiters.begin(); It != pThis->mGuestWaiters.end();)
+ {
+ GuestCall &rCurCall = *It;
+ if (rCurCall.u32ClientId != idClient)
+ ++It;
+ else
+ {
+ LogFlowFunc(("Completing call %u (%p)...\n", rCurCall.mFunction, rCurCall.mHandle));
+ pThis->mpHelpers->pfnCallComplete(rCurCall.mHandle, VERR_INTERRUPTED);
+ It = pThis->mGuestWaiters.erase(It);
+ }
+ }
+
+ return VINF_SUCCESS;
+}
+
+/**
+ * Increments a counter property.
+ *
+ * It is assumed that this a transient property that is read-only to the guest.
+ *
+ * @param pszName The property name.
+ * @throws std::bad_alloc if an out of memory condition occurs
+ */
+void Service::incrementCounterProp(const char *pszName)
+{
+ /* Format the incremented value. */
+ char szValue[64];
+ Property *pProp = getPropertyInternal(pszName);
+ if (pProp)
+ {
+ uint64_t uValue = RTStrToUInt64(pProp->mValue.c_str());
+ RTStrFormatU64(szValue, sizeof(szValue), uValue + 1, 10, 0, 0, 0);
+ }
+ else
+ {
+ szValue[0] = '1';
+ szValue[1] = '\0';
+ }
+
+ /* Set it. */
+ setPropertyInternal(pszName, szValue, GUEST_PROP_F_TRANSIENT | GUEST_PROP_F_RDONLYGUEST, getCurrentTimestamp());
+}
+
+/**
+ * Sets the VBoxVer, VBoxVerExt and VBoxRev properties.
+ */
+int Service::setHostVersionProps()
+{
+ uint64_t nsTimestamp = getCurrentTimestamp();
+
+ /* Set the raw VBox version string as a guest property. Used for host/guest
+ * version comparison. */
+ int rc = setPropertyInternal("/VirtualBox/HostInfo/VBoxVer", VBOX_VERSION_STRING_RAW,
+ GUEST_PROP_F_TRANSIENT | GUEST_PROP_F_RDONLYGUEST, nsTimestamp);
+ AssertRCReturn(rc, rc);
+
+ /* Set the full VBox version string as a guest property. Can contain vendor-specific
+ * information/branding and/or pre-release tags. */
+ rc = setPropertyInternal("/VirtualBox/HostInfo/VBoxVerExt", VBOX_VERSION_STRING,
+ GUEST_PROP_F_TRANSIENT | GUEST_PROP_F_RDONLYGUEST, nsTimestamp + 1);
+ AssertRCReturn(rc, rc);
+
+ /* Set the VBox SVN revision as a guest property */
+ rc = setPropertyInternal("/VirtualBox/HostInfo/VBoxRev", RTBldCfgRevisionStr(),
+ GUEST_PROP_F_TRANSIENT | GUEST_PROP_F_RDONLYGUEST, nsTimestamp + 2);
+ AssertRCReturn(rc, rc);
+ return VINF_SUCCESS;
+}
+
+
+/**
+ * @interface_method_impl{VBOXHGCMSVCFNTABLE,pfnNotify}
+ */
+/*static*/ DECLCALLBACK(void) Service::svcNotify(void *pvService, HGCMNOTIFYEVENT enmEvent)
+{
+ SELF *pThis = reinterpret_cast<SELF *>(pvService);
+ AssertPtrReturnVoid(pThis);
+
+ /* Make sure the host version properties have been touched and are
+ up-to-date after a restore: */
+ if ( !pThis->m_fSetHostVersionProps
+ && (enmEvent == HGCMNOTIFYEVENT_RESUME || enmEvent == HGCMNOTIFYEVENT_POWER_ON))
+ {
+ pThis->setHostVersionProps();
+ pThis->m_fSetHostVersionProps = true;
+ }
+
+ if (enmEvent == HGCMNOTIFYEVENT_RESUME)
+ pThis->incrementCounterProp("/VirtualBox/VMInfo/ResumeCounter");
+
+ if (enmEvent == HGCMNOTIFYEVENT_RESET)
+ pThis->incrementCounterProp("/VirtualBox/VMInfo/ResetCounter");
+}
+
+
+/* static */
+DECLCALLBACK(int) Service::threadNotifyHost(RTTHREAD hThreadSelf, void *pvUser)
+{
+ RT_NOREF1(hThreadSelf);
+ Service *pThis = (Service *)pvUser;
+ int rc = VINF_SUCCESS;
+
+ LogFlowFunc(("ENTER: %p\n", pThis));
+
+ for (;;)
+ {
+ rc = RTReqQueueProcess(pThis->mhReqQNotifyHost, RT_INDEFINITE_WAIT);
+
+ AssertMsg(rc == VWRN_STATE_CHANGED,
+ ("Left RTReqProcess and error code is not VWRN_STATE_CHANGED rc=%Rrc\n",
+ rc));
+ if (rc == VWRN_STATE_CHANGED)
+ {
+ break;
+ }
+ }
+
+ LogFlowFunc(("LEAVE: %Rrc\n", rc));
+ return rc;
+}
+
+static DECLCALLBACK(int) wakeupNotifyHost(void)
+{
+ /* Returning a VWRN_* will cause RTReqQueueProcess return. */
+ return VWRN_STATE_CHANGED;
+}
+
+
+int Service::initialize()
+{
+ /*
+ * Insert standard host properties.
+ */
+ /* The host version will but updated again on power on or resume
+ (after restore), however we need the properties now for restored
+ guest notification/wait calls. */
+ int rc = setHostVersionProps();
+ AssertRCReturn(rc, rc);
+
+ /* Sysprep execution by VBoxService (host is allowed to change these). */
+ uint64_t nsNow = getCurrentTimestamp();
+ rc = setPropertyInternal("/VirtualBox/HostGuest/SysprepExec", "", GUEST_PROP_F_TRANSIENT | GUEST_PROP_F_RDONLYGUEST, nsNow);
+ AssertRCReturn(rc, rc);
+ rc = setPropertyInternal("/VirtualBox/HostGuest/SysprepArgs", "", GUEST_PROP_F_TRANSIENT | GUEST_PROP_F_RDONLYGUEST, nsNow);
+ AssertRCReturn(rc, rc);
+
+ /* Resume and reset counters. */
+ rc = setPropertyInternal("/VirtualBox/VMInfo/ResumeCounter", "0", GUEST_PROP_F_TRANSIENT | GUEST_PROP_F_RDONLYGUEST, nsNow);
+ AssertRCReturn(rc, rc);
+ rc = setPropertyInternal("/VirtualBox/VMInfo/ResetCounter", "0", GUEST_PROP_F_TRANSIENT | GUEST_PROP_F_RDONLYGUEST, nsNow);
+ AssertRCReturn(rc, rc);
+
+ /* The host notification thread and queue. */
+ rc = RTReqQueueCreate(&mhReqQNotifyHost);
+ if (RT_SUCCESS(rc))
+ {
+ rc = RTThreadCreate(&mhThreadNotifyHost,
+ threadNotifyHost,
+ this,
+ 0 /* default stack size */,
+ RTTHREADTYPE_DEFAULT,
+ RTTHREADFLAGS_WAITABLE,
+ "GstPropNtfy");
+ if (RT_SUCCESS(rc))
+ {
+ /* Finally debug stuff (ignore failures): */
+ HGCMSvcHlpInfoRegister(mpHelpers, "guestprops", "Display the guest properties", Service::dbgInfo, this);
+ return rc;
+ }
+
+ RTReqQueueDestroy(mhReqQNotifyHost);
+ mhReqQNotifyHost = NIL_RTREQQUEUE;
+ }
+ return rc;
+}
+
+/**
+ * @callback_method_impl{FNRTSTRSPACECALLBACK, Destroys Property.}
+ */
+static DECLCALLBACK(int) destroyProperty(PRTSTRSPACECORE pStr, void *pvUser)
+{
+ RT_NOREF(pvUser);
+ Property *pProp = RT_FROM_CPP_MEMBER(pStr, struct Property, mStrCore); /* clang objects to offsetof on non-POD.*/
+ delete pProp;
+ return 0;
+}
+
+
+int Service::uninit()
+{
+ if (mpHelpers)
+ HGCMSvcHlpInfoDeregister(mpHelpers, "guestprops");
+
+ if (mhReqQNotifyHost != NIL_RTREQQUEUE)
+ {
+ /* Stop the thread */
+ PRTREQ pReq;
+ int rc = RTReqQueueCall(mhReqQNotifyHost, &pReq, 10000, (PFNRT)wakeupNotifyHost, 0);
+ if (RT_SUCCESS(rc))
+ RTReqRelease(pReq);
+ rc = RTThreadWait(mhThreadNotifyHost, 10000, NULL);
+ AssertRC(rc);
+ rc = RTReqQueueDestroy(mhReqQNotifyHost);
+ AssertRC(rc);
+ mhReqQNotifyHost = NIL_RTREQQUEUE;
+ mhThreadNotifyHost = NIL_RTTHREAD;
+ RTStrSpaceDestroy(&mhProperties, destroyProperty, NULL);
+ mhProperties = NULL;
+ }
+ return VINF_SUCCESS;
+}
+
+} /* namespace guestProp */
+
+using guestProp::Service;
+
+/**
+ * @copydoc VBOXHGCMSVCLOAD
+ */
+extern "C" DECLCALLBACK(DECLEXPORT(int)) VBoxHGCMSvcLoad(VBOXHGCMSVCFNTABLE *ptable)
+{
+ int rc = VERR_IPE_UNINITIALIZED_STATUS;
+
+ LogFlowFunc(("ptable = %p\n", ptable));
+
+ if (!RT_VALID_PTR(ptable))
+ rc = VERR_INVALID_PARAMETER;
+ else
+ {
+ LogFlowFunc(("ptable->cbSize = %d, ptable->u32Version = 0x%08X\n", ptable->cbSize, ptable->u32Version));
+
+ if ( ptable->cbSize != sizeof(VBOXHGCMSVCFNTABLE)
+ || ptable->u32Version != VBOX_HGCM_SVC_VERSION)
+ rc = VERR_VERSION_MISMATCH;
+ else
+ {
+ Service *pService = NULL;
+ /* No exceptions may propagate outside. */
+ try
+ {
+ pService = new Service(ptable->pHelpers);
+ rc = VINF_SUCCESS;
+ }
+ catch (int rcThrown)
+ {
+ rc = rcThrown;
+ }
+ catch (...)
+ {
+ rc = VERR_UNEXPECTED_EXCEPTION;
+ }
+
+ if (RT_SUCCESS(rc))
+ {
+ /* We do not maintain connections, so no client data is needed. */
+ ptable->cbClient = 0;
+
+ ptable->pfnUnload = Service::svcUnload;
+ ptable->pfnConnect = Service::svcConnect;
+ ptable->pfnDisconnect = Service::svcDisconnect;
+ ptable->pfnCall = Service::svcCall;
+ ptable->pfnHostCall = Service::svcHostCall;
+ ptable->pfnSaveState = NULL; /* The service is stateless, so the normal */
+ ptable->pfnLoadState = NULL; /* construction done before restoring suffices */
+ ptable->pfnRegisterExtension = Service::svcRegisterExtension;
+ ptable->pfnNotify = Service::svcNotify;
+ ptable->pvService = pService;
+
+ /* Service specific initialization. */
+ rc = pService->initialize();
+ if (RT_FAILURE(rc))
+ {
+ delete pService;
+ pService = NULL;
+ }
+ }
+ else
+ Assert(!pService);
+ }
+ }
+
+ LogFlowFunc(("returning %Rrc\n", rc));
+ return rc;
+}
+
diff --git a/src/VBox/HostServices/GuestProperties/VBoxGuestPropSvc.rc b/src/VBox/HostServices/GuestProperties/VBoxGuestPropSvc.rc
new file mode 100644
index 00000000..9ebafa35
--- /dev/null
+++ b/src/VBox/HostServices/GuestProperties/VBoxGuestPropSvc.rc
@@ -0,0 +1,51 @@
+/* $Id: VBoxGuestPropSvc.rc $ */
+/** @file
+ * VBoxGuestPropSvc - Resource file containing version info and icon.
+ */
+
+/*
+ * Copyright (C) 2015-2019 Oracle Corporation
+ *
+ * This file is part of VirtualBox Open Source Edition (OSE), as
+ * available from http://www.virtualbox.org. This file is free software;
+ * you can redistribute it and/or modify it under the terms of the GNU
+ * 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.
+ */
+
+#include <windows.h>
+#include <VBox/version.h>
+
+LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
+
+VS_VERSION_INFO VERSIONINFO
+ FILEVERSION VBOX_RC_FILE_VERSION
+ PRODUCTVERSION VBOX_RC_FILE_VERSION
+ FILEFLAGSMASK VS_FFI_FILEFLAGSMASK
+ FILEFLAGS VBOX_RC_FILE_FLAGS
+ FILEOS VBOX_RC_FILE_OS
+ FILETYPE VBOX_RC_TYPE_DLL
+ FILESUBTYPE VFT2_UNKNOWN
+BEGIN
+ BLOCK "StringFileInfo"
+ BEGIN
+ BLOCK "040904b0" // Lang=US English, CharSet=Unicode
+ BEGIN
+ VALUE "FileDescription", "VirtualBox Guest Properties Service\0"
+ VALUE "InternalName", "VBoxGuestPropSvc\0"
+ VALUE "OriginalFilename", "VBoxGuestPropSvc.dll\0"
+ VALUE "CompanyName", VBOX_RC_COMPANY_NAME
+ VALUE "FileVersion", VBOX_RC_FILE_VERSION_STR
+ VALUE "LegalCopyright", VBOX_RC_LEGAL_COPYRIGHT
+ VALUE "ProductName", VBOX_RC_PRODUCT_NAME_STR
+ VALUE "ProductVersion", VBOX_RC_PRODUCT_VERSION_STR
+ VBOX_RC_MORE_STRINGS
+ END
+ END
+ BLOCK "VarFileInfo"
+ BEGIN
+ VALUE "Translation", 0x409, 1200
+ END
+END
diff --git a/src/VBox/HostServices/GuestProperties/testcase/Makefile.kmk b/src/VBox/HostServices/GuestProperties/testcase/Makefile.kmk
new file mode 100644
index 00000000..5a05edeb
--- /dev/null
+++ b/src/VBox/HostServices/GuestProperties/testcase/Makefile.kmk
@@ -0,0 +1,46 @@
+# $Id: Makefile.kmk $
+## @file
+# Sub-Makefile for the Guest Properties Host Service testcases.
+#
+
+#
+# Copyright (C) 2006-2019 Oracle Corporation
+#
+# This file is part of VirtualBox Open Source Edition (OSE), as
+# available from http://www.virtualbox.org. This file is free software;
+# you can redistribute it and/or modify it under the terms of the GNU
+# 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.
+#
+
+SUB_DEPTH = ../../../../..
+include $(KBUILD_PATH)/subheader.kmk
+
+if defined(VBOX_WITH_TESTCASES) && !defined(VBOX_ONLY_ADDITIONS) && !defined(VBOX_ONLY_SDK)
+
+ # Set this in LocalConfig.kmk if you are working on the guest property
+ # service to automatically run the testcase at build time.
+ # OTHERS += $(tstGuestPropSvc_0_OUTDIR)/tstGuestPropSvc.run
+ #
+
+ PROGRAMS += tstGuestPropSvc
+ TESTING += $(tstGuestPropSvc_0_OUTDIR)/tstGuestPropSvc.run
+ tstGuestPropSvc_TEMPLATE = VBOXR3TSTEXE
+ # The second define here is to ensure that the testcase will run fast,
+ # without waiting for any thread synchronisation.
+ tstGuestPropSvc_DEFS = VBOX_WITH_HGCM VBOX_GUEST_PROP_TEST_NOTHREAD
+ tstGuestPropSvc_SOURCES = \
+ tstGuestPropSvc.cpp \
+ ../VBoxGuestPropSvc.cpp
+ tstGuestPropSvc_LIBS = $(LIB_RUNTIME)
+
+$$(tstGuestPropSvc_0_OUTDIR)/tstGuestPropSvc.run: $$(tstGuestPropSvc_1_STAGE_TARGET)
+ export VBOX_LOG_DEST=nofile; $(tstGuestPropSvc_1_STAGE_TARGET) quiet
+ $(QUIET)$(APPEND) -t "$@" "done"
+
+endif
+
+include $(FILE_KBUILD_SUB_FOOTER)
+
diff --git a/src/VBox/HostServices/GuestProperties/testcase/tstGuestPropSvc.cpp b/src/VBox/HostServices/GuestProperties/testcase/tstGuestPropSvc.cpp
new file mode 100644
index 00000000..fd5ca52e
--- /dev/null
+++ b/src/VBox/HostServices/GuestProperties/testcase/tstGuestPropSvc.cpp
@@ -0,0 +1,1171 @@
+/* $Id: tstGuestPropSvc.cpp $ */
+/** @file
+ *
+ * Testcase for the guest property service.
+ */
+
+/*
+ * Copyright (C) 2008-2019 Oracle Corporation
+ *
+ * This file is part of VirtualBox Open Source Edition (OSE), as
+ * available from http://www.virtualbox.org. This file is free software;
+ * you can redistribute it and/or modify it under the terms of the GNU
+ * 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 <VBox/HostServices/GuestPropertySvc.h>
+#include <VBox/err.h>
+#include <VBox/hgcmsvc.h>
+#include <iprt/test.h>
+#include <iprt/time.h>
+
+
+/*********************************************************************************************************************************
+* Global Variables *
+*********************************************************************************************************************************/
+static RTTEST g_hTest = NIL_RTTEST;
+
+
+/*********************************************************************************************************************************
+* Internal Functions *
+*********************************************************************************************************************************/
+extern "C" DECLCALLBACK(DECLEXPORT(int)) VBoxHGCMSvcLoad (VBOXHGCMSVCFNTABLE *ptable);
+
+
+/** Simple call handle structure for the guest call completion callback */
+struct VBOXHGCMCALLHANDLE_TYPEDEF
+{
+ /** Where to store the result code */
+ int32_t rc;
+};
+
+/** Call completion callback for guest calls. */
+static DECLCALLBACK(int) callComplete(VBOXHGCMCALLHANDLE callHandle, int32_t rc)
+{
+ callHandle->rc = rc;
+ return VINF_SUCCESS;
+}
+
+/**
+ * Initialise the HGCM service table as much as we need to start the
+ * service
+ * @param pTable the table to initialise
+ */
+void initTable(VBOXHGCMSVCFNTABLE *pTable, VBOXHGCMSVCHELPERS *pHelpers)
+{
+ pTable->cbSize = sizeof (VBOXHGCMSVCFNTABLE);
+ pTable->u32Version = VBOX_HGCM_SVC_VERSION;
+ pHelpers->pfnCallComplete = callComplete;
+ pTable->pHelpers = pHelpers;
+}
+
+/**
+ * A list of valid flag strings for testConvertFlags. The flag conversion
+ * functions should accept these and convert them from string to a flag type
+ * and back without errors.
+ */
+struct flagStrings
+{
+ /** Flag string in a format the functions should recognise */
+ const char *pcszIn;
+ /** How the functions should output the string again */
+ const char *pcszOut;
+}
+g_aValidFlagStrings[] =
+{
+ /* pcszIn, pcszOut */
+ { " ", "" },
+ { "transient, ", "TRANSIENT" },
+ { " rdOnLyHOST, transIENT , READONLY ", "TRANSIENT, READONLY" },
+ { " rdonlyguest", "RDONLYGUEST" },
+ { "rdonlyhost ", "RDONLYHOST" },
+ { "transient, transreset, rdonlyhost", "TRANSIENT, RDONLYHOST, TRANSRESET" },
+ { "transient, transreset, rdonlyguest", "TRANSIENT, RDONLYGUEST, TRANSRESET" }, /* max length */
+ { "rdonlyguest, rdonlyhost", "READONLY" },
+ { "transient, transreset, ", "TRANSIENT, TRANSRESET" }, /* Don't combine them ... */
+ { "transreset, ", "TRANSIENT, TRANSRESET" }, /* ... instead expand transreset for old adds. */
+};
+
+/**
+ * A list of invalid flag strings for testConvertFlags. The flag conversion
+ * functions should reject these.
+ */
+const char *g_apszInvalidFlagStrings[] =
+{
+ "RDONLYHOST,,",
+ " TRANSIENT READONLY"
+};
+
+/**
+ * Test the flag conversion functions.
+ * @returns iprt status value to indicate whether the test went as expected.
+ * @note prints its own diagnostic information to stdout.
+ */
+static void testConvertFlags(void)
+{
+ int rc = VINF_SUCCESS;
+ char *pszFlagBuffer = (char *)RTTestGuardedAllocTail(g_hTest, GUEST_PROP_MAX_FLAGS_LEN);
+
+ RTTestISub("Conversion of valid flags strings");
+ for (unsigned i = 0; i < RT_ELEMENTS(g_aValidFlagStrings) && RT_SUCCESS(rc); ++i)
+ {
+ uint32_t fFlags;
+ rc = GuestPropValidateFlags(g_aValidFlagStrings[i].pcszIn, &fFlags);
+ if (RT_FAILURE(rc))
+ RTTestIFailed("Failed to validate flag string '%s'", g_aValidFlagStrings[i].pcszIn);
+ if (RT_SUCCESS(rc))
+ {
+ rc = GuestPropWriteFlags(fFlags, pszFlagBuffer);
+ if (RT_FAILURE(rc))
+ RTTestIFailed("Failed to convert flag string '%s' back to a string.",
+ g_aValidFlagStrings[i].pcszIn);
+ }
+ if (RT_SUCCESS(rc) && (strlen(pszFlagBuffer) > GUEST_PROP_MAX_FLAGS_LEN - 1))
+ {
+ RTTestIFailed("String '%s' converts back to a flag string which is too long.\n",
+ g_aValidFlagStrings[i].pcszIn);
+ rc = VERR_TOO_MUCH_DATA;
+ }
+ if (RT_SUCCESS(rc) && (strcmp(pszFlagBuffer, g_aValidFlagStrings[i].pcszOut) != 0))
+ {
+ RTTestIFailed("String '%s' converts back to '%s' instead of to '%s'\n",
+ g_aValidFlagStrings[i].pcszIn, pszFlagBuffer,
+ g_aValidFlagStrings[i].pcszOut);
+ rc = VERR_PARSE_ERROR;
+ }
+ }
+ if (RT_SUCCESS(rc))
+ {
+ RTTestISub("Rejection of invalid flags strings");
+ for (unsigned i = 0; i < RT_ELEMENTS(g_apszInvalidFlagStrings) && RT_SUCCESS(rc); ++i)
+ {
+ uint32_t fFlags;
+ /* This is required to fail. */
+ if (RT_SUCCESS(GuestPropValidateFlags(g_apszInvalidFlagStrings[i], &fFlags)))
+ {
+ RTTestIFailed("String '%s' was incorrectly accepted as a valid flag string.\n",
+ g_apszInvalidFlagStrings[i]);
+ rc = VERR_PARSE_ERROR;
+ }
+ }
+ }
+ if (RT_SUCCESS(rc))
+ {
+ uint32_t u32BadFlags = GUEST_PROP_F_ALLFLAGS << 1;
+ RTTestISub("Rejection of an invalid flags field");
+ /* This is required to fail. */
+ if (RT_SUCCESS(GuestPropWriteFlags(u32BadFlags, pszFlagBuffer)))
+ {
+ RTTestIFailed("Flags 0x%x were incorrectly written out as '%.*s'\n",
+ u32BadFlags, GUEST_PROP_MAX_FLAGS_LEN, pszFlagBuffer);
+ rc = VERR_PARSE_ERROR;
+ }
+ }
+
+ RTTestGuardedFree(g_hTest, pszFlagBuffer);
+}
+
+/**
+ * List of property names for testSetPropsHost.
+ */
+const char *g_apcszNameBlock[] =
+{
+ "test/name/",
+ "test name",
+ "TEST NAME",
+ "/test/name",
+ NULL
+};
+
+/**
+ * List of property values for testSetPropsHost.
+ */
+const char *g_apcszValueBlock[] =
+{
+ "test/value/",
+ "test value",
+ "TEST VALUE",
+ "/test/value",
+ NULL
+};
+
+/**
+ * List of property timestamps for testSetPropsHost.
+ */
+uint64_t g_au64TimestampBlock[] =
+{
+ 0, 999, 999999, UINT64_C(999999999999), 0
+};
+
+/**
+ * List of property flags for testSetPropsHost.
+ */
+const char *g_apcszFlagsBlock[] =
+{
+ "",
+ "readonly, transient",
+ "RDONLYHOST",
+ "RdOnlyGuest",
+ NULL
+};
+
+/**
+ * Test the SET_PROPS_HOST function.
+ * @returns iprt status value to indicate whether the test went as expected.
+ * @note prints its own diagnostic information to stdout.
+ */
+static void testSetPropsHost(VBOXHGCMSVCFNTABLE *ptable)
+{
+ RTTestISub("SET_PROPS_HOST");
+ RTTESTI_CHECK_RETV(RT_VALID_PTR(ptable->pfnHostCall));
+
+ VBOXHGCMSVCPARM aParms[4];
+ HGCMSvcSetPv(&aParms[0], (void *)g_apcszNameBlock, 0);
+ HGCMSvcSetPv(&aParms[1], (void *)g_apcszValueBlock, 0);
+ HGCMSvcSetPv(&aParms[2], (void *)g_au64TimestampBlock, 0);
+ HGCMSvcSetPv(&aParms[3], (void *)g_apcszFlagsBlock, 0);
+ RTTESTI_CHECK_RC(ptable->pfnHostCall(ptable->pvService, GUEST_PROP_FN_HOST_SET_PROPS, 4, &aParms[0]), VINF_SUCCESS);
+}
+
+/** Result strings for zeroth enumeration test */
+static const char *g_apchEnumResult0[] =
+{
+ "test/name/\0test/value/\0""0\0",
+ "test name\0test value\0""999\0TRANSIENT, READONLY",
+ "TEST NAME\0TEST VALUE\0""999999\0RDONLYHOST",
+ "/test/name\0/test/value\0""999999999999\0RDONLYGUEST",
+ NULL
+};
+
+/** Result string sizes for zeroth enumeration test */
+static const uint32_t g_acbEnumResult0[] =
+{
+ sizeof("test/name/\0test/value/\0""0\0"),
+ sizeof("test name\0test value\0""999\0TRANSIENT, READONLY"),
+ sizeof("TEST NAME\0TEST VALUE\0""999999\0RDONLYHOST"),
+ sizeof("/test/name\0/test/value\0""999999999999\0RDONLYGUEST"),
+ 0
+};
+
+/**
+ * The size of the buffer returned by the zeroth enumeration test -
+ * the - 1 at the end is because of the hidden zero terminator
+ */
+static const uint32_t g_cbEnumBuffer0 =
+ sizeof("test/name/\0test/value/\0""0\0\0"
+ "test name\0test value\0""999\0TRANSIENT, READONLY\0"
+ "TEST NAME\0TEST VALUE\0""999999\0RDONLYHOST\0"
+ "/test/name\0/test/value\0""999999999999\0RDONLYGUEST\0\0\0\0\0") - 1;
+
+/** Result strings for first and second enumeration test */
+static const char *g_apchEnumResult1[] =
+{
+ "TEST NAME\0TEST VALUE\0""999999\0RDONLYHOST",
+ "/test/name\0/test/value\0""999999999999\0RDONLYGUEST",
+ NULL
+};
+
+/** Result string sizes for first and second enumeration test */
+static const uint32_t g_acbEnumResult1[] =
+{
+ sizeof("TEST NAME\0TEST VALUE\0""999999\0RDONLYHOST"),
+ sizeof("/test/name\0/test/value\0""999999999999\0RDONLYGUEST"),
+ 0
+};
+
+/**
+ * The size of the buffer returned by the first enumeration test -
+ * the - 1 at the end is because of the hidden zero terminator
+ */
+static const uint32_t g_cbEnumBuffer1 =
+ sizeof("TEST NAME\0TEST VALUE\0""999999\0RDONLYHOST\0"
+ "/test/name\0/test/value\0""999999999999\0RDONLYGUEST\0\0\0\0\0") - 1;
+
+static const struct enumStringStruct
+{
+ /** The enumeration pattern to test */
+ const char *pszPatterns;
+ /** The size of the pattern string */
+ const uint32_t cchPatterns;
+ /** The expected enumeration output strings */
+ const char **papchResult;
+ /** The size of the output strings */
+ const uint32_t *pacchResult;
+ /** The size of the buffer needed for the enumeration */
+ const uint32_t cbBuffer;
+} g_aEnumStrings[] =
+{
+ {
+ "", sizeof(""),
+ g_apchEnumResult0,
+ g_acbEnumResult0,
+ g_cbEnumBuffer0
+ },
+ {
+ "/*\0?E*", sizeof("/*\0?E*"),
+ g_apchEnumResult1,
+ g_acbEnumResult1,
+ g_cbEnumBuffer1
+ },
+ {
+ "/*|?E*", sizeof("/*|?E*"),
+ g_apchEnumResult1,
+ g_acbEnumResult1,
+ g_cbEnumBuffer1
+ }
+};
+
+/**
+ * Test the ENUM_PROPS_HOST function.
+ * @returns iprt status value to indicate whether the test went as expected.
+ * @note prints its own diagnostic information to stdout.
+ */
+static void testEnumPropsHost(VBOXHGCMSVCFNTABLE *ptable)
+{
+ RTTestISub("ENUM_PROPS_HOST");
+ RTTESTI_CHECK_RETV(RT_VALID_PTR(ptable->pfnHostCall));
+
+ for (unsigned i = 0; i < RT_ELEMENTS(g_aEnumStrings); ++i)
+ {
+ VBOXHGCMSVCPARM aParms[3];
+ char abBuffer[2048];
+ RTTESTI_CHECK_RETV(g_aEnumStrings[i].cbBuffer < sizeof(abBuffer));
+
+ /* Check that we get buffer overflow with a too small buffer. */
+ HGCMSvcSetPv(&aParms[0], (void *)g_aEnumStrings[i].pszPatterns, g_aEnumStrings[i].cchPatterns);
+ HGCMSvcSetPv(&aParms[1], (void *)abBuffer, g_aEnumStrings[i].cbBuffer - 1);
+ memset(abBuffer, 0x55, sizeof(abBuffer));
+ int rc2 = ptable->pfnHostCall(ptable->pvService, GUEST_PROP_FN_HOST_ENUM_PROPS, 3, aParms);
+ if (rc2 == VERR_BUFFER_OVERFLOW)
+ {
+ uint32_t cbNeeded;
+ RTTESTI_CHECK_RC(rc2 = HGCMSvcGetU32(&aParms[2], &cbNeeded), VINF_SUCCESS);
+ if (RT_SUCCESS(rc2))
+ RTTESTI_CHECK_MSG(cbNeeded == g_aEnumStrings[i].cbBuffer,
+ ("expected %u, got %u, pattern %d\n", g_aEnumStrings[i].cbBuffer, cbNeeded, i));
+ }
+ else
+ RTTestIFailed("ENUM_PROPS_HOST returned %Rrc instead of VERR_BUFFER_OVERFLOW on too small buffer, pattern number %d.", rc2, i);
+
+ /* Make a successfull call. */
+ HGCMSvcSetPv(&aParms[0], (void *)g_aEnumStrings[i].pszPatterns, g_aEnumStrings[i].cchPatterns);
+ HGCMSvcSetPv(&aParms[1], (void *)abBuffer, g_aEnumStrings[i].cbBuffer);
+ memset(abBuffer, 0x55, sizeof(abBuffer));
+ rc2 = ptable->pfnHostCall(ptable->pvService, GUEST_PROP_FN_HOST_ENUM_PROPS, 3, aParms);
+ if (rc2 == VINF_SUCCESS)
+ {
+ /* Look for each of the result strings in the buffer which was returned */
+ for (unsigned j = 0; g_aEnumStrings[i].papchResult[j] != NULL; ++j)
+ {
+ bool found = false;
+ for (unsigned k = 0; !found && k < g_aEnumStrings[i].cbBuffer
+ - g_aEnumStrings[i].pacchResult[j];
+ ++k)
+ if (memcmp(abBuffer + k, g_aEnumStrings[i].papchResult[j],
+ g_aEnumStrings[i].pacchResult[j]) == 0)
+ found = true;
+ if (!found)
+ RTTestIFailed("ENUM_PROPS_HOST did not produce the expected output for pattern %d.", i);
+ }
+ }
+ else
+ RTTestIFailed("ENUM_PROPS_HOST returned %Rrc instead of VINF_SUCCESS, pattern number %d.", rc2, i);
+ }
+}
+
+/**
+ * Set a property by calling the service
+ * @returns the status returned by the call to the service
+ *
+ * @param pTable the service instance handle
+ * @param pcszName the name of the property to set
+ * @param pcszValue the value to set the property to
+ * @param pcszFlags the flag string to set if one of the SET_PROP[_HOST]
+ * commands is used
+ * @param isHost whether the SET_PROP[_VALUE]_HOST commands should be
+ * used, rather than the guest ones
+ * @param useSetProp whether SET_PROP[_HOST] should be used rather than
+ * SET_PROP_VALUE[_HOST]
+ */
+int doSetProperty(VBOXHGCMSVCFNTABLE *pTable, const char *pcszName,
+ const char *pcszValue, const char *pcszFlags, bool isHost,
+ bool useSetProp)
+{
+ VBOXHGCMCALLHANDLE_TYPEDEF callHandle = { VINF_SUCCESS };
+ int command = GUEST_PROP_FN_SET_PROP_VALUE;
+ if (isHost)
+ {
+ if (useSetProp)
+ command = GUEST_PROP_FN_HOST_SET_PROP;
+ else
+ command = GUEST_PROP_FN_HOST_SET_PROP_VALUE;
+ }
+ else if (useSetProp)
+ command = GUEST_PROP_FN_SET_PROP;
+ VBOXHGCMSVCPARM aParms[3];
+ /* Work around silly constant issues - we ought to allow passing
+ * constant strings in the hgcm parameters. */
+ char szName[GUEST_PROP_MAX_NAME_LEN];
+ char szValue[GUEST_PROP_MAX_VALUE_LEN];
+ char szFlags[GUEST_PROP_MAX_FLAGS_LEN];
+ RTStrPrintf(szName, sizeof(szName), "%s", pcszName);
+ RTStrPrintf(szValue, sizeof(szValue), "%s", pcszValue);
+ RTStrPrintf(szFlags, sizeof(szFlags), "%s", pcszFlags);
+ HGCMSvcSetStr(&aParms[0], szName);
+ HGCMSvcSetStr(&aParms[1], szValue);
+ HGCMSvcSetStr(&aParms[2], szFlags);
+ if (isHost)
+ callHandle.rc = pTable->pfnHostCall(pTable->pvService, command,
+ useSetProp ? 3 : 2, aParms);
+ else
+ pTable->pfnCall(pTable->pvService, &callHandle, 0, NULL, command,
+ useSetProp ? 3 : 2, aParms, 0);
+ return callHandle.rc;
+}
+
+/**
+ * Test the SET_PROP, SET_PROP_VALUE, SET_PROP_HOST and SET_PROP_VALUE_HOST
+ * functions.
+ * @returns iprt status value to indicate whether the test went as expected.
+ * @note prints its own diagnostic information to stdout.
+ */
+static void testSetProp(VBOXHGCMSVCFNTABLE *pTable)
+{
+ RTTestISub("SET_PROP, _VALUE, _HOST, _VALUE_HOST");
+
+ /** Array of properties for testing SET_PROP_HOST and _GUEST. */
+ static const struct
+ {
+ /** Property name */
+ const char *pcszName;
+ /** Property value */
+ const char *pcszValue;
+ /** Property flags */
+ const char *pcszFlags;
+ /** Should this be set as the host or the guest? */
+ bool isHost;
+ /** Should we use SET_PROP or SET_PROP_VALUE? */
+ bool useSetProp;
+ /** Should this succeed or be rejected with VERR_PERMISSION_DENIED? */
+ bool isAllowed;
+ }
+ s_aSetProperties[] =
+ {
+ { "Red", "Stop!", "transient", false, true, true },
+ { "Amber", "Caution!", "", false, false, true },
+ { "Green", "Go!", "readonly", true, true, true },
+ { "Blue", "What on earth...?", "", true, false, true },
+ { "/test/name", "test", "", false, true, false },
+ { "TEST NAME", "test", "", true, true, false },
+ { "Green", "gone out...", "", false, false, false },
+ { "Green", "gone out...", "", true, false, false },
+ { "/VirtualBox/GuestAdd/SharedFolders/MountDir", "test", "", false, true, false },
+ { "/VirtualBox/GuestAdd/SomethingElse", "test", "", false, true, true },
+ { "/VirtualBox/HostInfo/VRDP/Client/1/Name", "test", "", false, false, false },
+ { "/VirtualBox/GuestAdd/SharedFolders/MountDir", "test", "", true, true, true },
+ { "/VirtualBox/HostInfo/VRDP/Client/1/Name", "test", "TRANSRESET", true, true, true },
+ };
+
+ for (unsigned i = 0; i < RT_ELEMENTS(s_aSetProperties); ++i)
+ {
+ int rc = doSetProperty(pTable,
+ s_aSetProperties[i].pcszName,
+ s_aSetProperties[i].pcszValue,
+ s_aSetProperties[i].pcszFlags,
+ s_aSetProperties[i].isHost,
+ s_aSetProperties[i].useSetProp);
+ if (s_aSetProperties[i].isAllowed && RT_FAILURE(rc))
+ RTTestIFailed("Setting property '%s' failed with rc=%Rrc.",
+ s_aSetProperties[i].pcszName, rc);
+ else if ( !s_aSetProperties[i].isAllowed
+ && rc != VERR_PERMISSION_DENIED)
+ RTTestIFailed("Setting property '%s' returned %Rrc instead of VERR_PERMISSION_DENIED.",
+ s_aSetProperties[i].pcszName, rc);
+ }
+}
+
+/**
+ * Delete a property by calling the service
+ * @returns the status returned by the call to the service
+ *
+ * @param pTable the service instance handle
+ * @param pcszName the name of the property to delete
+ * @param isHost whether the DEL_PROP_HOST command should be used, rather
+ * than the guest one
+ */
+static int doDelProp(VBOXHGCMSVCFNTABLE *pTable, const char *pcszName, bool isHost)
+{
+ VBOXHGCMCALLHANDLE_TYPEDEF callHandle = { VINF_SUCCESS };
+ int command = GUEST_PROP_FN_DEL_PROP;
+ if (isHost)
+ command = GUEST_PROP_FN_HOST_DEL_PROP;
+ VBOXHGCMSVCPARM aParms[1];
+ HGCMSvcSetStr(&aParms[0], pcszName);
+ if (isHost)
+ callHandle.rc = pTable->pfnHostCall(pTable->pvService, command, 1, aParms);
+ else
+ pTable->pfnCall(pTable->pvService, &callHandle, 0, NULL, command, 1, aParms, 0);
+ return callHandle.rc;
+}
+
+/**
+ * Test the DEL_PROP, and DEL_PROP_HOST functions.
+ * @returns iprt status value to indicate whether the test went as expected.
+ * @note prints its own diagnostic information to stdout.
+ */
+static void testDelProp(VBOXHGCMSVCFNTABLE *pTable)
+{
+ RTTestISub("DEL_PROP, DEL_PROP_HOST");
+
+ /** Array of properties for testing DEL_PROP_HOST and _GUEST. */
+ static const struct
+ {
+ /** Property name */
+ const char *pcszName;
+ /** Should this be set as the host or the guest? */
+ bool isHost;
+ /** Should this succeed or be rejected with VERR_PERMISSION_DENIED? */
+ bool isAllowed;
+ }
+ s_aDelProperties[] =
+ {
+ { "Red", false, true },
+ { "Amber", true, true },
+ { "Red2", false, true },
+ { "Amber2", true, true },
+ { "Green", false, false },
+ { "Green", true, false },
+ { "/test/name", false, false },
+ { "TEST NAME", true, false },
+ };
+
+ for (unsigned i = 0; i < RT_ELEMENTS(s_aDelProperties); ++i)
+ {
+ int rc = doDelProp(pTable, s_aDelProperties[i].pcszName,
+ s_aDelProperties[i].isHost);
+ if (s_aDelProperties[i].isAllowed && RT_FAILURE(rc))
+ RTTestIFailed("Deleting property '%s' failed with rc=%Rrc.",
+ s_aDelProperties[i].pcszName, rc);
+ else if ( !s_aDelProperties[i].isAllowed
+ && rc != VERR_PERMISSION_DENIED )
+ RTTestIFailed("Deleting property '%s' returned %Rrc instead of VERR_PERMISSION_DENIED.",
+ s_aDelProperties[i].pcszName, rc);
+ }
+}
+
+/**
+ * Test the GET_PROP_HOST function.
+ * @returns iprt status value to indicate whether the test went as expected.
+ * @note prints its own diagnostic information to stdout.
+ */
+static void testGetProp(VBOXHGCMSVCFNTABLE *pTable)
+{
+ RTTestISub("GET_PROP_HOST");
+
+ /** Array of properties for testing GET_PROP_HOST. */
+ static const struct
+ {
+ /** Property name */
+ const char *pcszName;
+ /** What value/flags pattern do we expect back? */
+ const char *pchValue;
+ /** What size should the value/flags array be? */
+ uint32_t cchValue;
+ /** Should this property exist? */
+ bool exists;
+ /** Do we expect a particular timestamp? */
+ bool hasTimestamp;
+ /** What timestamp if any do ex expect? */
+ uint64_t u64Timestamp;
+ }
+ s_aGetProperties[] =
+ {
+ { "test/name/", "test/value/\0", sizeof("test/value/\0"), true, true, 0 },
+ { "test name", "test value\0TRANSIENT, READONLY",
+ sizeof("test value\0TRANSIENT, READONLY"), true, true, 999 },
+ { "TEST NAME", "TEST VALUE\0RDONLYHOST", sizeof("TEST VALUE\0RDONLYHOST"),
+ true, true, 999999 },
+ { "/test/name", "/test/value\0RDONLYGUEST",
+ sizeof("/test/value\0RDONLYGUEST"), true, true, UINT64_C(999999999999) },
+ { "Green", "Go!\0READONLY", sizeof("Go!\0READONLY"), true, false, 0 },
+ { "Blue", "What on earth...?\0", sizeof("What on earth...?\0"), true,
+ false, 0 },
+ { "Red", "", 0, false, false, 0 },
+ };
+
+ for (unsigned i = 0; i < RT_ELEMENTS(s_aGetProperties); ++i)
+ {
+ VBOXHGCMSVCPARM aParms[4];
+ /* Work around silly constant issues - we ought to allow passing
+ * constant strings in the hgcm parameters. */
+ char szBuffer[GUEST_PROP_MAX_VALUE_LEN + GUEST_PROP_MAX_FLAGS_LEN];
+ RTTESTI_CHECK_RETV(s_aGetProperties[i].cchValue < sizeof(szBuffer));
+
+ HGCMSvcSetStr(&aParms[0], s_aGetProperties[i].pcszName);
+ memset(szBuffer, 0x55, sizeof(szBuffer));
+ HGCMSvcSetPv(&aParms[1], szBuffer, sizeof(szBuffer));
+ int rc2 = pTable->pfnHostCall(pTable->pvService, GUEST_PROP_FN_HOST_GET_PROP, 4, aParms);
+
+ if (s_aGetProperties[i].exists && RT_FAILURE(rc2))
+ {
+ RTTestIFailed("Getting property '%s' failed with rc=%Rrc.",
+ s_aGetProperties[i].pcszName, rc2);
+ continue;
+ }
+
+ if (!s_aGetProperties[i].exists && rc2 != VERR_NOT_FOUND)
+ {
+ RTTestIFailed("Getting property '%s' returned %Rrc instead of VERR_NOT_FOUND.",
+ s_aGetProperties[i].pcszName, rc2);
+ continue;
+ }
+
+ if (s_aGetProperties[i].exists)
+ {
+ AssertRC(rc2);
+
+ uint32_t u32ValueLen = UINT32_MAX;
+ RTTESTI_CHECK_RC(rc2 = HGCMSvcGetU32(&aParms[3], &u32ValueLen), VINF_SUCCESS);
+ if (RT_SUCCESS(rc2))
+ {
+ RTTESTI_CHECK_MSG(u32ValueLen <= sizeof(szBuffer), ("u32ValueLen=%d", u32ValueLen));
+ if (memcmp(szBuffer, s_aGetProperties[i].pchValue, s_aGetProperties[i].cchValue) != 0)
+ RTTestIFailed("Unexpected result '%.*s' for property '%s', expected '%.*s'.",
+ u32ValueLen, szBuffer, s_aGetProperties[i].pcszName,
+ s_aGetProperties[i].cchValue, s_aGetProperties[i].pchValue);
+ }
+
+ if (s_aGetProperties[i].hasTimestamp)
+ {
+ uint64_t u64Timestamp = UINT64_MAX;
+ RTTESTI_CHECK_RC(rc2 = HGCMSvcGetU64(&aParms[2], &u64Timestamp), VINF_SUCCESS);
+ if (u64Timestamp != s_aGetProperties[i].u64Timestamp)
+ RTTestIFailed("Bad timestamp %llu for property '%s', expected %llu.",
+ u64Timestamp, s_aGetProperties[i].pcszName,
+ s_aGetProperties[i].u64Timestamp);
+ }
+ }
+ }
+}
+
+/** Array of properties for testing GET_PROP_HOST. */
+static const struct
+{
+ /** Buffer returned */
+ const char *pchBuffer;
+ /** What size should the buffer be? */
+ uint32_t cbBuffer;
+}
+g_aGetNotifications[] =
+{
+ { "Red\0Stop!\0TRANSIENT", sizeof("Red\0Stop!\0TRANSIENT") },
+ { "Amber\0Caution!\0", sizeof("Amber\0Caution!\0") },
+ { "Green\0Go!\0READONLY", sizeof("Green\0Go!\0READONLY") },
+ { "Blue\0What on earth...?\0", sizeof("Blue\0What on earth...?\0") },
+ { "/VirtualBox/GuestAdd/SomethingElse\0test\0",
+ sizeof("/VirtualBox/GuestAdd/SomethingElse\0test\0") },
+ { "/VirtualBox/GuestAdd/SharedFolders/MountDir\0test\0RDONLYGUEST",
+ sizeof("/VirtualBox/GuestAdd/SharedFolders/MountDir\0test\0RDONLYGUEST") },
+ { "/VirtualBox/HostInfo/VRDP/Client/1/Name\0test\0TRANSIENT, RDONLYGUEST, TRANSRESET",
+ sizeof("/VirtualBox/HostInfo/VRDP/Client/1/Name\0test\0TRANSIENT, RDONLYGUEST, TRANSRESET") },
+ { "Red\0\0", sizeof("Red\0\0") },
+ { "Amber\0\0", sizeof("Amber\0\0") },
+};
+
+/**
+ * Test the GET_NOTIFICATION function.
+ * @returns iprt status value to indicate whether the test went as expected.
+ * @note prints its own diagnostic information to stdout.
+ */
+static void testGetNotification(VBOXHGCMSVCFNTABLE *pTable)
+{
+ RTTestISub("GET_NOTIFICATION");
+
+ /* Test "buffer too small" */
+ static char s_szPattern[] = "";
+ VBOXHGCMCALLHANDLE_TYPEDEF callHandle = { VINF_SUCCESS };
+ VBOXHGCMSVCPARM aParms[4];
+ uint32_t cbRetNeeded;
+
+ for (uint32_t cbBuf = 1;
+ cbBuf < g_aGetNotifications[0].cbBuffer - 1;
+ cbBuf++)
+ {
+ void *pvBuf = RTTestGuardedAllocTail(g_hTest, cbBuf);
+ RTTESTI_CHECK_BREAK(pvBuf);
+ memset(pvBuf, 0x55, cbBuf);
+
+ HGCMSvcSetPv(&aParms[0], (void *)s_szPattern, sizeof(s_szPattern));
+ HGCMSvcSetU64(&aParms[1], 1);
+ HGCMSvcSetPv(&aParms[2], pvBuf, cbBuf);
+ pTable->pfnCall(pTable->pvService, &callHandle, 0, NULL, GUEST_PROP_FN_GET_NOTIFICATION, 4, aParms, 0);
+
+ if ( callHandle.rc != VERR_BUFFER_OVERFLOW
+ || RT_FAILURE(HGCMSvcGetU32(&aParms[3], &cbRetNeeded))
+ || cbRetNeeded != g_aGetNotifications[0].cbBuffer
+ )
+ {
+ RTTestIFailed("Getting notification for property '%s' with a too small buffer did not fail correctly: %Rrc",
+ g_aGetNotifications[0].pchBuffer, callHandle.rc);
+ }
+ RTTestGuardedFree(g_hTest, pvBuf);
+ }
+
+ /* Test successful notification queries. Start with an unknown timestamp
+ * to get the oldest available notification. */
+ uint64_t u64Timestamp = 1;
+ for (unsigned i = 0; i < RT_ELEMENTS(g_aGetNotifications); ++i)
+ {
+ uint32_t cbBuf = g_aGetNotifications[i].cbBuffer + _1K;
+ void *pvBuf = RTTestGuardedAllocTail(g_hTest, cbBuf);
+ RTTESTI_CHECK_BREAK(pvBuf);
+ memset(pvBuf, 0x55, cbBuf);
+
+ HGCMSvcSetPv(&aParms[0], (void *)s_szPattern, sizeof(s_szPattern));
+ HGCMSvcSetU64(&aParms[1], u64Timestamp);
+ HGCMSvcSetPv(&aParms[2], pvBuf, cbBuf);
+ pTable->pfnCall(pTable->pvService, &callHandle, 0, NULL, GUEST_PROP_FN_GET_NOTIFICATION, 4, aParms, 0);
+ if ( RT_FAILURE(callHandle.rc)
+ || (i == 0 && callHandle.rc != VWRN_NOT_FOUND)
+ || RT_FAILURE(HGCMSvcGetU64(&aParms[1], &u64Timestamp))
+ || RT_FAILURE(HGCMSvcGetU32(&aParms[3], &cbRetNeeded))
+ || cbRetNeeded != g_aGetNotifications[i].cbBuffer
+ || memcmp(pvBuf, g_aGetNotifications[i].pchBuffer, cbRetNeeded) != 0
+ )
+ {
+ RTTestIFailed("Failed to get notification for property '%s' (rc=%Rrc).",
+ g_aGetNotifications[i].pchBuffer, callHandle.rc);
+ }
+ RTTestGuardedFree(g_hTest, pvBuf);
+ }
+}
+
+/** Parameters for the asynchronous guest notification call */
+struct asyncNotification_
+{
+ /** Call parameters */
+ VBOXHGCMSVCPARM aParms[4];
+ /** Result buffer */
+ char abBuffer[GUEST_PROP_MAX_NAME_LEN + GUEST_PROP_MAX_VALUE_LEN + GUEST_PROP_MAX_FLAGS_LEN];
+ /** Return value */
+ VBOXHGCMCALLHANDLE_TYPEDEF callHandle;
+} g_AsyncNotification;
+
+/**
+ * Set up the test for the asynchronous GET_NOTIFICATION function.
+ */
+static void setupAsyncNotification(VBOXHGCMSVCFNTABLE *pTable)
+{
+ RTTestISub("Async GET_NOTIFICATION without notifications");
+ static char s_szPattern[] = "";
+
+ HGCMSvcSetPv(&g_AsyncNotification.aParms[0], (void *)s_szPattern, sizeof(s_szPattern));
+ HGCMSvcSetU64(&g_AsyncNotification.aParms[1], 0);
+ HGCMSvcSetPv(&g_AsyncNotification.aParms[2], (void *)g_AsyncNotification.abBuffer,
+ sizeof(g_AsyncNotification.abBuffer));
+ g_AsyncNotification.callHandle.rc = VINF_HGCM_ASYNC_EXECUTE;
+ pTable->pfnCall(pTable->pvService, &g_AsyncNotification.callHandle, 0, NULL,
+ GUEST_PROP_FN_GET_NOTIFICATION, 4, g_AsyncNotification.aParms, 0);
+ if (RT_FAILURE(g_AsyncNotification.callHandle.rc))
+ RTTestIFailed("GET_NOTIFICATION call failed, rc=%Rrc.", g_AsyncNotification.callHandle.rc);
+ else if (g_AsyncNotification.callHandle.rc != VINF_HGCM_ASYNC_EXECUTE)
+ RTTestIFailed("GET_NOTIFICATION call completed when no new notifications should be available.");
+}
+
+/**
+ * Test the asynchronous GET_NOTIFICATION function.
+ */
+static void testAsyncNotification(VBOXHGCMSVCFNTABLE *pTable)
+{
+ RT_NOREF1(pTable);
+ uint64_t u64Timestamp;
+ uint32_t u32Size;
+ if ( g_AsyncNotification.callHandle.rc != VINF_SUCCESS
+ || RT_FAILURE(HGCMSvcGetU64(&g_AsyncNotification.aParms[1], &u64Timestamp))
+ || RT_FAILURE(HGCMSvcGetU32(&g_AsyncNotification.aParms[3], &u32Size))
+ || u32Size != g_aGetNotifications[0].cbBuffer
+ || memcmp(g_AsyncNotification.abBuffer, g_aGetNotifications[0].pchBuffer, u32Size) != 0
+ )
+ {
+ RTTestIFailed("Asynchronous GET_NOTIFICATION call did not complete as expected, rc=%Rrc.",
+ g_AsyncNotification.callHandle.rc);
+ }
+}
+
+
+static void test2(void)
+{
+ VBOXHGCMSVCFNTABLE svcTable;
+ VBOXHGCMSVCHELPERS svcHelpers;
+ initTable(&svcTable, &svcHelpers);
+
+ /* The function is inside the service, not HGCM. */
+ RTTESTI_CHECK_RC_OK_RETV(VBoxHGCMSvcLoad(&svcTable));
+
+ testSetPropsHost(&svcTable);
+ testEnumPropsHost(&svcTable);
+
+ /* Set up the asynchronous notification test */
+ setupAsyncNotification(&svcTable);
+ testSetProp(&svcTable);
+ RTTestISub("Async notification call data");
+ testAsyncNotification(&svcTable); /* Our previous notification call should have completed by now. */
+
+ testDelProp(&svcTable);
+ testGetProp(&svcTable);
+ testGetNotification(&svcTable);
+
+ /* Cleanup */
+ RTTESTI_CHECK_RC_OK(svcTable.pfnUnload(svcTable.pvService));
+}
+
+/**
+ * Set the global flags value by calling the service
+ * @returns the status returned by the call to the service
+ *
+ * @param pTable the service instance handle
+ * @param fFlags the flags to set
+ */
+static int doSetGlobalFlags(VBOXHGCMSVCFNTABLE *pTable, uint32_t fFlags)
+{
+ VBOXHGCMSVCPARM paParm;
+ HGCMSvcSetU32(&paParm, fFlags);
+ int rc = pTable->pfnHostCall(pTable->pvService, GUEST_PROP_FN_HOST_SET_GLOBAL_FLAGS, 1, &paParm);
+ if (RT_FAILURE(rc))
+ {
+ char szFlags[GUEST_PROP_MAX_FLAGS_LEN];
+ if (RT_FAILURE(GuestPropWriteFlags(fFlags, szFlags)))
+ RTTestIFailed("Failed to set the global flags.");
+ else
+ RTTestIFailed("Failed to set the global flags \"%s\".", szFlags);
+ }
+ return rc;
+}
+
+/**
+ * Test the SET_PROP, SET_PROP_VALUE, SET_PROP_HOST and SET_PROP_VALUE_HOST
+ * functions.
+ * @returns iprt status value to indicate whether the test went as expected.
+ * @note prints its own diagnostic information to stdout.
+ */
+static void testSetPropROGuest(VBOXHGCMSVCFNTABLE *pTable)
+{
+ RTTestISub("global READONLYGUEST and SET_PROP*");
+
+ /** Array of properties for testing SET_PROP_HOST and _GUEST with the
+ * READONLYGUEST global flag set. */
+ static const struct
+ {
+ /** Property name */
+ const char *pcszName;
+ /** Property value */
+ const char *pcszValue;
+ /** Property flags */
+ const char *pcszFlags;
+ /** Should this be set as the host or the guest? */
+ bool isHost;
+ /** Should we use SET_PROP or SET_PROP_VALUE? */
+ bool useSetProp;
+ /** Should this succeed or be rejected with VERR_ (NOT VINF_!)
+ * PERMISSION_DENIED? The global check is done after the property one. */
+ bool isAllowed;
+ }
+ s_aSetPropertiesROGuest[] =
+ {
+ { "Red", "Stop!", "transient", false, true, true },
+ { "Amber", "Caution!", "", false, false, true },
+ { "Green", "Go!", "readonly", true, true, true },
+ { "Blue", "What on earth...?", "", true, false, true },
+ { "/test/name", "test", "", false, true, true },
+ { "TEST NAME", "test", "", true, true, true },
+ { "Green", "gone out...", "", false, false, false },
+ { "Green", "gone out....", "", true, false, false },
+ };
+
+ RTTESTI_CHECK_RC_OK_RETV(VBoxHGCMSvcLoad(pTable));
+ int rc = doSetGlobalFlags(pTable, GUEST_PROP_F_RDONLYGUEST);
+ if (RT_SUCCESS(rc))
+ {
+ for (unsigned i = 0; i < RT_ELEMENTS(s_aSetPropertiesROGuest); ++i)
+ {
+ rc = doSetProperty(pTable, s_aSetPropertiesROGuest[i].pcszName,
+ s_aSetPropertiesROGuest[i].pcszValue,
+ s_aSetPropertiesROGuest[i].pcszFlags,
+ s_aSetPropertiesROGuest[i].isHost,
+ s_aSetPropertiesROGuest[i].useSetProp);
+ if (s_aSetPropertiesROGuest[i].isAllowed && RT_FAILURE(rc))
+ RTTestIFailed("Setting property '%s' to '%s' failed with rc=%Rrc.",
+ s_aSetPropertiesROGuest[i].pcszName,
+ s_aSetPropertiesROGuest[i].pcszValue, rc);
+ else if ( !s_aSetPropertiesROGuest[i].isAllowed
+ && rc != VERR_PERMISSION_DENIED)
+ RTTestIFailed("Setting property '%s' to '%s' returned %Rrc instead of VERR_PERMISSION_DENIED.\n",
+ s_aSetPropertiesROGuest[i].pcszName,
+ s_aSetPropertiesROGuest[i].pcszValue, rc);
+ else if ( !s_aSetPropertiesROGuest[i].isHost
+ && s_aSetPropertiesROGuest[i].isAllowed
+ && rc != VINF_PERMISSION_DENIED)
+ RTTestIFailed("Setting property '%s' to '%s' returned %Rrc instead of VINF_PERMISSION_DENIED.\n",
+ s_aSetPropertiesROGuest[i].pcszName,
+ s_aSetPropertiesROGuest[i].pcszValue, rc);
+ }
+ }
+ RTTESTI_CHECK_RC_OK(pTable->pfnUnload(pTable->pvService));
+}
+
+/**
+ * Test the DEL_PROP, and DEL_PROP_HOST functions.
+ * @returns iprt status value to indicate whether the test went as expected.
+ * @note prints its own diagnostic information to stdout.
+ */
+static void testDelPropROGuest(VBOXHGCMSVCFNTABLE *pTable)
+{
+ RTTestISub("global READONLYGUEST and DEL_PROP*");
+
+ /** Array of properties for testing DEL_PROP_HOST and _GUEST with
+ * READONLYGUEST set globally. */
+ static const struct
+ {
+ /** Property name */
+ const char *pcszName;
+ /** Should this be deleted as the host (or the guest)? */
+ bool isHost;
+ /** Should this property be created first? (As host, obviously) */
+ bool shouldCreate;
+ /** And with what flags? */
+ const char *pcszFlags;
+ /** Should this succeed or be rejected with VERR_ (NOT VINF_!)
+ * PERMISSION_DENIED? The global check is done after the property one. */
+ bool isAllowed;
+ }
+ s_aDelPropertiesROGuest[] =
+ {
+ { "Red", true, true, "", true },
+ { "Amber", false, true, "", true },
+ { "Red2", true, false, "", true },
+ { "Amber2", false, false, "", true },
+ { "Red3", true, true, "READONLY", false },
+ { "Amber3", false, true, "READONLY", false },
+ { "Red4", true, true, "RDONLYHOST", false },
+ { "Amber4", false, true, "RDONLYHOST", true },
+ };
+
+ RTTESTI_CHECK_RC_OK_RETV(VBoxHGCMSvcLoad(pTable));
+ int rc = doSetGlobalFlags(pTable, GUEST_PROP_F_RDONLYGUEST);
+ if (RT_SUCCESS(rc))
+ {
+ for (unsigned i = 0; i < RT_ELEMENTS(s_aDelPropertiesROGuest); ++i)
+ {
+ if (s_aDelPropertiesROGuest[i].shouldCreate)
+ rc = doSetProperty(pTable, s_aDelPropertiesROGuest[i].pcszName,
+ "none", s_aDelPropertiesROGuest[i].pcszFlags,
+ true, true);
+ rc = doDelProp(pTable, s_aDelPropertiesROGuest[i].pcszName,
+ s_aDelPropertiesROGuest[i].isHost);
+ if (s_aDelPropertiesROGuest[i].isAllowed && RT_FAILURE(rc))
+ RTTestIFailed("Deleting property '%s' failed with rc=%Rrc.",
+ s_aDelPropertiesROGuest[i].pcszName, rc);
+ else if ( !s_aDelPropertiesROGuest[i].isAllowed
+ && rc != VERR_PERMISSION_DENIED)
+ RTTestIFailed("Deleting property '%s' returned %Rrc instead of VERR_PERMISSION_DENIED.",
+ s_aDelPropertiesROGuest[i].pcszName, rc);
+ else if ( !s_aDelPropertiesROGuest[i].isHost
+ && s_aDelPropertiesROGuest[i].shouldCreate
+ && s_aDelPropertiesROGuest[i].isAllowed
+ && rc != VINF_PERMISSION_DENIED)
+ RTTestIFailed("Deleting property '%s' as guest returned %Rrc instead of VINF_PERMISSION_DENIED.",
+ s_aDelPropertiesROGuest[i].pcszName, rc);
+ }
+ }
+ RTTESTI_CHECK_RC_OK(pTable->pfnUnload(pTable->pvService));
+}
+
+static void test3(void)
+{
+ VBOXHGCMSVCFNTABLE svcTable;
+ VBOXHGCMSVCHELPERS svcHelpers;
+ initTable(&svcTable, &svcHelpers);
+ testSetPropROGuest(&svcTable);
+ testDelPropROGuest(&svcTable);
+}
+
+static void test4(void)
+{
+ RTTestISub("GET_PROP_HOST buffer handling");
+
+ VBOXHGCMSVCFNTABLE svcTable;
+ VBOXHGCMSVCHELPERS svcHelpers;
+ initTable(&svcTable, &svcHelpers);
+ RTTESTI_CHECK_RC_OK_RETV(VBoxHGCMSvcLoad(&svcTable));
+
+ /* Insert a property that we can mess around with. */
+ static char const s_szProp[] = "/MyProperties/Sub/Sub/Sub/Sub/Sub/Sub/Sub/Property";
+ static char const s_szValue[] = "Property Value";
+ RTTESTI_CHECK_RC_OK(doSetProperty(&svcTable, s_szProp, s_szValue, "", true, true));
+
+
+ /* Get the value with buffer sizes up to 1K. */
+ for (unsigned iVariation = 0; iVariation < 2; iVariation++)
+ {
+ for (uint32_t cbBuf = 0; cbBuf < _1K; cbBuf++)
+ {
+ void *pvBuf;
+ RTTESTI_CHECK_RC_BREAK(RTTestGuardedAlloc(g_hTest, cbBuf, 1, iVariation == 0, &pvBuf), VINF_SUCCESS);
+
+ VBOXHGCMSVCPARM aParms[4];
+ HGCMSvcSetStr(&aParms[0], s_szProp);
+ HGCMSvcSetPv(&aParms[1], pvBuf, cbBuf);
+ svcTable.pfnHostCall(svcTable.pvService, GUEST_PROP_FN_HOST_GET_PROP, RT_ELEMENTS(aParms), aParms);
+
+ RTTestGuardedFree(g_hTest, pvBuf);
+ }
+ }
+
+ /* Done. */
+ RTTESTI_CHECK_RC_OK(svcTable.pfnUnload(svcTable.pvService));
+}
+
+static void test5(void)
+{
+ RTTestISub("ENUM_PROPS_HOST buffer handling");
+
+ VBOXHGCMSVCFNTABLE svcTable;
+ VBOXHGCMSVCHELPERS svcHelpers;
+ initTable(&svcTable, &svcHelpers);
+ RTTESTI_CHECK_RC_OK_RETV(VBoxHGCMSvcLoad(&svcTable));
+
+ /* Insert a few property that we can mess around with. */
+ RTTESTI_CHECK_RC_OK(doSetProperty(&svcTable, "/MyProperties/Sub/Sub/Sub/Sub/Sub/Sub/Sub/Property", "Property Value", "", true, true));
+ RTTESTI_CHECK_RC_OK(doSetProperty(&svcTable, "/MyProperties/12357", "83848569", "", true, true));
+ RTTESTI_CHECK_RC_OK(doSetProperty(&svcTable, "/MyProperties/56678", "abcdefghijklm", "", true, true));
+ RTTESTI_CHECK_RC_OK(doSetProperty(&svcTable, "/MyProperties/932769", "n", "", true, true));
+
+ /* Get the value with buffer sizes up to 1K. */
+ for (unsigned iVariation = 0; iVariation < 2; iVariation++)
+ {
+ for (uint32_t cbBuf = 0; cbBuf < _1K; cbBuf++)
+ {
+ void *pvBuf;
+ RTTESTI_CHECK_RC_BREAK(RTTestGuardedAlloc(g_hTest, cbBuf, 1, iVariation == 0, &pvBuf), VINF_SUCCESS);
+
+ VBOXHGCMSVCPARM aParms[3];
+ HGCMSvcSetStr(&aParms[0], "*");
+ HGCMSvcSetPv(&aParms[1], pvBuf, cbBuf);
+ svcTable.pfnHostCall(svcTable.pvService, GUEST_PROP_FN_HOST_ENUM_PROPS, RT_ELEMENTS(aParms), aParms);
+
+ RTTestGuardedFree(g_hTest, pvBuf);
+ }
+ }
+
+ /* Done. */
+ RTTESTI_CHECK_RC_OK(svcTable.pfnUnload(svcTable.pvService));
+}
+
+static void test6(void)
+{
+ RTTestISub("Max properties");
+
+ VBOXHGCMSVCFNTABLE svcTable;
+ VBOXHGCMSVCHELPERS svcHelpers;
+ initTable(&svcTable, &svcHelpers);
+ RTTESTI_CHECK_RC_OK_RETV(VBoxHGCMSvcLoad(&svcTable));
+
+ /* Insert the max number of properties. */
+ static char const s_szPropFmt[] = "/MyProperties/Sub/Sub/Sub/Sub/Sub/Sub/Sub/PropertyNo#%u";
+ char szProp[80];
+ unsigned cProps = 0;
+ for (;;)
+ {
+ RTStrPrintf(szProp, sizeof(szProp), s_szPropFmt, cProps);
+ int rc = doSetProperty(&svcTable, szProp, "myvalue", "", true, true);
+ if (rc == VERR_TOO_MUCH_DATA)
+ break;
+ if (RT_FAILURE(rc))
+ {
+ RTTestIFailed("Unexpected error %Rrc setting property number %u", rc, cProps);
+ break;
+ }
+ cProps++;
+ }
+ RTTestIValue("Max Properties", cProps, RTTESTUNIT_OCCURRENCES);
+
+ /* Touch them all again. */
+ for (unsigned iProp = 0; iProp < cProps; iProp++)
+ {
+ RTStrPrintf(szProp, sizeof(szProp), s_szPropFmt, iProp);
+ int rc;
+ RTTESTI_CHECK_MSG((rc = doSetProperty(&svcTable, szProp, "myvalue", "", true, true)) == VINF_SUCCESS,
+ ("%Rrc - #%u\n", rc, iProp));
+ RTTESTI_CHECK_MSG((rc = doSetProperty(&svcTable, szProp, "myvalue", "", true, false)) == VINF_SUCCESS,
+ ("%Rrc - #%u\n", rc, iProp));
+ RTTESTI_CHECK_MSG((rc = doSetProperty(&svcTable, szProp, "myvalue", "", false, true)) == VINF_SUCCESS,
+ ("%Rrc - #%u\n", rc, iProp));
+ RTTESTI_CHECK_MSG((rc = doSetProperty(&svcTable, szProp, "myvalue", "", false, false)) == VINF_SUCCESS,
+ ("%Rrc - #%u\n", rc, iProp));
+ }
+
+ /* Benchmark. */
+ uint64_t cNsMax = 0;
+ uint64_t cNsMin = UINT64_MAX;
+ uint64_t cNsAvg = 0;
+ for (unsigned iProp = 0; iProp < cProps; iProp++)
+ {
+ size_t cchProp = RTStrPrintf(szProp, sizeof(szProp), s_szPropFmt, iProp);
+
+ uint64_t cNsElapsed = RTTimeNanoTS();
+ unsigned iCall;
+ for (iCall = 0; iCall < 1000; iCall++)
+ {
+ VBOXHGCMSVCPARM aParms[4];
+ char szBuffer[256];
+ HGCMSvcSetPv(&aParms[0], szProp, (uint32_t)cchProp + 1);
+ HGCMSvcSetPv(&aParms[1], szBuffer, sizeof(szBuffer));
+ RTTESTI_CHECK_RC_BREAK(svcTable.pfnHostCall(svcTable.pvService, GUEST_PROP_FN_HOST_GET_PROP, 4, aParms), VINF_SUCCESS);
+ }
+ cNsElapsed = RTTimeNanoTS() - cNsElapsed;
+ if (iCall)
+ {
+ uint64_t cNsPerCall = cNsElapsed / iCall;
+ cNsAvg += cNsPerCall;
+ if (cNsPerCall < cNsMin)
+ cNsMin = cNsPerCall;
+ if (cNsPerCall > cNsMax)
+ cNsMax = cNsPerCall;
+ }
+ }
+ if (cProps)
+ cNsAvg /= cProps;
+ RTTestIValue("GET_PROP_HOST Min", cNsMin, RTTESTUNIT_NS_PER_CALL);
+ RTTestIValue("GET_PROP_HOST Avg", cNsAvg, RTTESTUNIT_NS_PER_CALL);
+ RTTestIValue("GET_PROP_HOST Max", cNsMax, RTTESTUNIT_NS_PER_CALL);
+
+ /* Done. */
+ RTTESTI_CHECK_RC_OK(svcTable.pfnUnload(svcTable.pvService));
+}
+
+
+
+
+int main()
+{
+ RTEXITCODE rcExit = RTTestInitAndCreate("tstGuestPropSvc", &g_hTest);
+ if (rcExit != RTEXITCODE_SUCCESS)
+ return rcExit;
+ RTTestBanner(g_hTest);
+
+ testConvertFlags();
+ test2();
+ test3();
+ test4();
+ test5();
+ test6();
+
+ return RTTestSummaryAndDestroy(g_hTest);
+}