VirtualBox

source: vbox/trunk/src/VBox/HostServices/GuestProperties/VBoxGuestPropSvc.cpp@ 98110

Last change on this file since 98110 was 98103, checked in by vboxsync, 23 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 63.3 KB
Line 
1/* $Id: VBoxGuestPropSvc.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * Guest Property Service: Host service entry points.
4 */
5
6/*
7 * Copyright (C) 2008-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28/** @page pg_svc_guest_properties Guest Property HGCM Service
29 *
30 * This HGCM service allows the guest to set and query values in a property
31 * store on the host. The service proxies the guest requests to the service
32 * owner on the host using a request callback provided by the owner, and is
33 * notified of changes to properties made by the host. It forwards these
34 * notifications to clients in the guest which have expressed interest and
35 * are waiting for notification.
36 *
37 * The service currently consists of two threads. One of these is the main
38 * HGCM service thread which deals with requests from the guest and from the
39 * host. The second thread sends the host asynchronous notifications of
40 * changes made by the guest and deals with notification timeouts.
41 *
42 * Guest requests to wait for notification are added to a list of open
43 * notification requests and completed when a corresponding guest property
44 * is changed or when the request times out.
45 */
46
47
48/*********************************************************************************************************************************
49* Header Files *
50*********************************************************************************************************************************/
51#define LOG_GROUP LOG_GROUP_HGCM
52#include <VBox/HostServices/GuestPropertySvc.h>
53
54#include <VBox/log.h>
55#include <iprt/asm.h>
56#include <iprt/assert.h>
57#include <iprt/buildconfig.h>
58#include <iprt/cpp/autores.h>
59#include <iprt/cpp/utils.h>
60#include <iprt/cpp/ministring.h>
61#include <VBox/err.h>
62#include <VBox/hgcmsvc.h>
63#include <iprt/mem.h>
64#include <iprt/req.h>
65#include <iprt/string.h>
66#include <iprt/thread.h>
67#include <iprt/time.h>
68#include <VBox/vmm/dbgf.h>
69#include <VBox/version.h>
70#include <VBox/AssertGuest.h>
71
72#include <list>
73
74
75namespace guestProp {
76
77/**
78 * Structure for holding a property
79 */
80struct Property
81{
82 /** The string space core record. */
83 RTSTRSPACECORE mStrCore;
84 /** The name of the property */
85 RTCString mName;
86 /** The property value */
87 RTCString mValue;
88 /** The timestamp of the property */
89 uint64_t mTimestamp;
90 /** The property flags */
91 uint32_t mFlags;
92
93 /** Default constructor */
94 Property() : mTimestamp(0), mFlags(GUEST_PROP_F_NILFLAG)
95 {
96 RT_ZERO(mStrCore);
97 }
98 /** Constructor with const char * */
99 Property(const char *pcszName, const char *pcszValue, uint64_t nsTimestamp, uint32_t u32Flags)
100 : mName(pcszName)
101 , mValue(pcszValue)
102 , mTimestamp(nsTimestamp)
103 , mFlags(u32Flags)
104 {
105 RT_ZERO(mStrCore);
106 mStrCore.pszString = mName.c_str();
107 }
108 /** Constructor with std::string */
109 Property(RTCString const &rName, RTCString const &rValue, uint64_t nsTimestamp, uint32_t fFlags)
110 : mName(rName)
111 , mValue(rValue)
112 , mTimestamp(nsTimestamp)
113 , mFlags(fFlags)
114 {}
115
116 /** Does the property name match one of a set of patterns? */
117 bool Matches(const char *pszPatterns) const
118 {
119 return ( pszPatterns[0] == '\0' /* match all */
120 || RTStrSimplePatternMultiMatch(pszPatterns, RTSTR_MAX,
121 mName.c_str(), RTSTR_MAX,
122 NULL)
123 );
124 }
125
126 /** Are two properties equal? */
127 bool operator==(const Property &prop)
128 {
129 if (mTimestamp != prop.mTimestamp)
130 return false;
131 if (mFlags != prop.mFlags)
132 return false;
133 if (mName != prop.mName)
134 return false;
135 if (mValue != prop.mValue)
136 return false;
137 return true;
138 }
139
140 /* Is the property nil? */
141 bool isNull()
142 {
143 return mName.isEmpty();
144 }
145};
146/** The properties list type */
147typedef std::list <Property> PropertyList;
148
149/**
150 * Structure for holding an uncompleted guest call
151 */
152struct GuestCall
153{
154 uint32_t u32ClientId;
155 /** The call handle */
156 VBOXHGCMCALLHANDLE mHandle;
157 /** The function that was requested */
158 uint32_t mFunction;
159 /** Number of call parameters. */
160 uint32_t mParmsCnt;
161 /** The call parameters */
162 VBOXHGCMSVCPARM *mParms;
163 /** The default return value, used for passing warnings */
164 int mRc;
165
166 /** The standard constructor */
167 GuestCall(void) : u32ClientId(0), mFunction(0), mParmsCnt(0) {}
168 /** The normal constructor */
169 GuestCall(uint32_t aClientId, VBOXHGCMCALLHANDLE aHandle, uint32_t aFunction,
170 uint32_t aParmsCnt, VBOXHGCMSVCPARM aParms[], int aRc)
171 : u32ClientId(aClientId), mHandle(aHandle), mFunction(aFunction),
172 mParmsCnt(aParmsCnt), mParms(aParms), mRc(aRc) {}
173};
174/** The guest call list type */
175typedef std::list <GuestCall> CallList;
176
177/**
178 * Class containing the shared information service functionality.
179 */
180class Service : public RTCNonCopyable
181{
182private:
183 /** Type definition for use in callback functions */
184 typedef Service SELF;
185 /** HGCM helper functions. */
186 PVBOXHGCMSVCHELPERS mpHelpers;
187 /** Global flags for the service */
188 uint32_t mfGlobalFlags;
189 /** The property string space handle. */
190 RTSTRSPACE mhProperties;
191 /** The number of properties. */
192 unsigned mcProperties;
193 /** The list of property changes for guest notifications;
194 * only used for timestamp tracking in notifications at the moment */
195 PropertyList mGuestNotifications;
196 /** The list of outstanding guest notification calls */
197 CallList mGuestWaiters;
198 /** @todo we should have classes for thread and request handler thread */
199 /** Callback function supplied by the host for notification of updates
200 * to properties */
201 PFNHGCMSVCEXT mpfnHostCallback;
202 /** User data pointer to be supplied to the host callback function */
203 void *mpvHostData;
204 /** The previous timestamp.
205 * This is used by getCurrentTimestamp() to decrease the chance of
206 * generating duplicate timestamps. */
207 uint64_t mPrevTimestamp;
208 /** The number of consecutive timestamp adjustments that we've made.
209 * Together with mPrevTimestamp, this defines a set of obsolete timestamp
210 * values: {(mPrevTimestamp - mcTimestampAdjustments), ..., mPrevTimestamp} */
211 uint64_t mcTimestampAdjustments;
212 /** For helping setting host version properties _after_ restoring VMs. */
213 bool m_fSetHostVersionProps;
214
215 /**
216 * Get the next property change notification from the queue of saved
217 * notification based on the timestamp of the last notification seen.
218 * Notifications will only be reported if the property name matches the
219 * pattern given.
220 *
221 * @returns iprt status value
222 * @returns VWRN_NOT_FOUND if the last notification was not found in the queue
223 * @param pszPatterns the patterns to match the property name against
224 * @param nsTimestamp the timestamp of the last notification
225 * @param pProp where to return the property found. If none is
226 * found this will be set to nil.
227 * @throws nothing
228 * @thread HGCM
229 */
230 int getOldNotification(const char *pszPatterns, uint64_t nsTimestamp, Property *pProp)
231 {
232 AssertPtrReturn(pszPatterns, VERR_INVALID_POINTER);
233 /* Zero means wait for a new notification. */
234 AssertReturn(nsTimestamp != 0, VERR_INVALID_PARAMETER);
235 AssertPtrReturn(pProp, VERR_INVALID_POINTER);
236 int rc = getOldNotificationInternal(pszPatterns, nsTimestamp, pProp);
237
238#ifdef VBOX_STRICT
239 /*
240 * ENSURE that pProp is the first event in the notification queue that:
241 * - Appears later than nsTimestamp
242 * - Matches the pszPatterns
243 */
244 /** @todo r=bird: This incorrectly ASSUMES that mTimestamp is unique.
245 * The timestamp resolution can be very coarse on windows for instance. */
246 PropertyList::const_iterator it = mGuestNotifications.begin();
247 for (; it != mGuestNotifications.end()
248 && it->mTimestamp != nsTimestamp; ++it)
249 { /*nothing*/ }
250 if (it == mGuestNotifications.end()) /* Not found */
251 it = mGuestNotifications.begin();
252 else
253 ++it; /* Next event */
254 for (; it != mGuestNotifications.end()
255 && it->mTimestamp != pProp->mTimestamp; ++it)
256 Assert(!it->Matches(pszPatterns));
257 if (pProp->mTimestamp != 0)
258 {
259 Assert(*pProp == *it);
260 Assert(pProp->Matches(pszPatterns));
261 }
262#endif /* VBOX_STRICT */
263 return rc;
264 }
265
266 /**
267 * Check whether we have permission to change a property.
268 *
269 * @returns Strict VBox status code.
270 * @retval VINF_SUCCESS if we do.
271 * @retval VERR_PERMISSION_DENIED if the value is read-only for the requesting
272 * side.
273 * @retval VINF_PERMISSION_DENIED if the side is globally marked read-only.
274 *
275 * @param fFlags the flags on the property in question
276 * @param isGuest is the guest or the host trying to make the change?
277 */
278 int checkPermission(uint32_t fFlags, bool isGuest)
279 {
280 if (fFlags & (isGuest ? GUEST_PROP_F_RDONLYGUEST : GUEST_PROP_F_RDONLYHOST))
281 return VERR_PERMISSION_DENIED;
282 if (isGuest && (mfGlobalFlags & GUEST_PROP_F_RDONLYGUEST))
283 return VINF_PERMISSION_DENIED;
284 return VINF_SUCCESS;
285 }
286
287 /**
288 * Check whether the property name is reserved for host changes only.
289 *
290 * @returns Boolean true (host reserved) or false (available to guest).
291 *
292 * @param pszName The property name to check.
293 */
294 bool checkHostReserved(const char *pszName)
295 {
296 if (RTStrStartsWith(pszName, "/VirtualBox/GuestAdd/VBoxService/"))
297 return true;
298 if (RTStrStartsWith(pszName, "/VirtualBox/GuestAdd/PAM/"))
299 return true;
300 if (RTStrStartsWith(pszName, "/VirtualBox/GuestAdd/Greeter/"))
301 return true;
302 if (RTStrStartsWith(pszName, "/VirtualBox/GuestAdd/SharedFolders/"))
303 return true;
304 if (RTStrStartsWith(pszName, "/VirtualBox/HostInfo/"))
305 return true;
306 if (RTStrStartsWith(pszName, "/VirtualBox/VMInfo/"))
307 return true;
308 return false;
309 }
310
311 /**
312 * Gets a property.
313 *
314 * @returns Pointer to the property if found, NULL if not.
315 *
316 * @param pszName The name of the property to get.
317 */
318 Property *getPropertyInternal(const char *pszName)
319 {
320 return (Property *)RTStrSpaceGet(&mhProperties, pszName);
321 }
322
323public:
324 explicit Service(PVBOXHGCMSVCHELPERS pHelpers)
325 : mpHelpers(pHelpers)
326 , mfGlobalFlags(GUEST_PROP_F_NILFLAG)
327 , mhProperties(NULL)
328 , mcProperties(0)
329 , mpfnHostCallback(NULL)
330 , mpvHostData(NULL)
331 , mPrevTimestamp(0)
332 , mcTimestampAdjustments(0)
333 , m_fSetHostVersionProps(false)
334 , mhThreadNotifyHost(NIL_RTTHREAD)
335 , mhReqQNotifyHost(NIL_RTREQQUEUE)
336 { }
337
338 /**
339 * @interface_method_impl{VBOXHGCMSVCFNTABLE,pfnUnload}
340 * Simply deletes the service object
341 */
342 static DECLCALLBACK(int) svcUnload(void *pvService)
343 {
344 AssertLogRelReturn(RT_VALID_PTR(pvService), VERR_INVALID_PARAMETER);
345 SELF *pSelf = reinterpret_cast<SELF *>(pvService);
346 int rc = pSelf->uninit();
347 AssertRC(rc);
348 if (RT_SUCCESS(rc))
349 delete pSelf;
350 return rc;
351 }
352
353 /**
354 * @interface_method_impl{VBOXHGCMSVCFNTABLE,pfnConnect}
355 * Stub implementation of pfnConnect.
356 */
357 static DECLCALLBACK(int) svcConnect(void * /* pvService */,
358 uint32_t /* u32ClientID */,
359 void * /* pvClient */,
360 uint32_t /*fRequestor*/,
361 bool /*fRestoring*/)
362 {
363 return VINF_SUCCESS;
364 }
365
366 static DECLCALLBACK(int) svcDisconnect(void *pvService, uint32_t idClient, void *pvClient);
367
368 /**
369 * @interface_method_impl{VBOXHGCMSVCFNTABLE,pfnCall}
370 * Wraps to the call member function
371 */
372 static DECLCALLBACK(void) svcCall(void * pvService,
373 VBOXHGCMCALLHANDLE callHandle,
374 uint32_t u32ClientID,
375 void *pvClient,
376 uint32_t u32Function,
377 uint32_t cParms,
378 VBOXHGCMSVCPARM paParms[],
379 uint64_t tsArrival)
380 {
381 AssertLogRelReturnVoid(RT_VALID_PTR(pvService));
382 LogFlowFunc(("pvService=%p, callHandle=%p, u32ClientID=%u, pvClient=%p, u32Function=%u, cParms=%u, paParms=%p\n", pvService, callHandle, u32ClientID, pvClient, u32Function, cParms, paParms));
383 SELF *pSelf = reinterpret_cast<SELF *>(pvService);
384 pSelf->call(callHandle, u32ClientID, pvClient, u32Function, cParms, paParms);
385 LogFlowFunc(("returning\n"));
386 RT_NOREF_PV(tsArrival);
387 }
388
389 /**
390 * @interface_method_impl{VBOXHGCMSVCFNTABLE,pfnHostCall}
391 * Wraps to the hostCall member function
392 */
393 static DECLCALLBACK(int) svcHostCall(void *pvService,
394 uint32_t u32Function,
395 uint32_t cParms,
396 VBOXHGCMSVCPARM paParms[])
397 {
398 AssertLogRelReturn(RT_VALID_PTR(pvService), VERR_INVALID_PARAMETER);
399 LogFlowFunc(("pvService=%p, u32Function=%u, cParms=%u, paParms=%p\n", pvService, u32Function, cParms, paParms));
400 SELF *pSelf = reinterpret_cast<SELF *>(pvService);
401 int rc = pSelf->hostCall(u32Function, cParms, paParms);
402 LogFlowFunc(("rc=%Rrc\n", rc));
403 return rc;
404 }
405
406 /**
407 * @interface_method_impl{VBOXHGCMSVCFNTABLE,pfnRegisterExtension}
408 * Installs a host callback for notifications of property changes.
409 */
410 static DECLCALLBACK(int) svcRegisterExtension(void *pvService,
411 PFNHGCMSVCEXT pfnExtension,
412 void *pvExtension)
413 {
414 AssertLogRelReturn(RT_VALID_PTR(pvService), VERR_INVALID_PARAMETER);
415 SELF *pSelf = reinterpret_cast<SELF *>(pvService);
416 pSelf->mpfnHostCallback = pfnExtension;
417 pSelf->mpvHostData = pvExtension;
418 return VINF_SUCCESS;
419 }
420
421 int setHostVersionProps();
422 void incrementCounterProp(const char *pszName);
423 static DECLCALLBACK(void) svcNotify(void *pvService, HGCMNOTIFYEVENT enmEvent);
424
425 int initialize();
426
427private:
428 static DECLCALLBACK(int) reqThreadFn(RTTHREAD ThreadSelf, void *pvUser);
429 uint64_t getCurrentTimestamp(void);
430 int setPropertyBlock(uint32_t cParms, VBOXHGCMSVCPARM paParms[]);
431 int getProperty(uint32_t cParms, VBOXHGCMSVCPARM paParms[]);
432 int setProperty(uint32_t cParms, VBOXHGCMSVCPARM paParms[], bool isGuest);
433 int setPropertyInternal(const char *pcszName, const char *pcszValue, uint32_t fFlags, uint64_t nsTimestamp,
434 bool fIsGuest = false);
435 int delProperty(uint32_t cParms, VBOXHGCMSVCPARM paParms[], bool isGuest);
436 int enumProps(uint32_t cParms, VBOXHGCMSVCPARM paParms[]);
437 int getNotification(uint32_t u32ClientId, VBOXHGCMCALLHANDLE callHandle, uint32_t cParms, VBOXHGCMSVCPARM paParms[]);
438 int getOldNotificationInternal(const char *pszPattern, uint64_t nsTimestamp, Property *pProp);
439 int getNotificationWriteOut(uint32_t cParms, VBOXHGCMSVCPARM paParms[], Property const &prop, bool fWasDeleted);
440 int doNotifications(const char *pszProperty, uint64_t nsTimestamp);
441 int notifyHost(const char *pszName, const char *pszValue, uint64_t nsTimestamp, const char *pszFlags);
442
443 void call(VBOXHGCMCALLHANDLE callHandle, uint32_t u32ClientID,
444 void *pvClient, uint32_t eFunction, uint32_t cParms,
445 VBOXHGCMSVCPARM paParms[]);
446 int hostCall(uint32_t eFunction, uint32_t cParms, VBOXHGCMSVCPARM paParms[]);
447 int uninit();
448 static DECLCALLBACK(void) dbgInfo(void *pvUser, PCDBGFINFOHLP pHlp, const char *pszArgs);
449
450 /* Thread for handling host notifications. */
451 RTTHREAD mhThreadNotifyHost;
452 /* Queue for handling requests for notifications. */
453 RTREQQUEUE mhReqQNotifyHost;
454 static DECLCALLBACK(int) threadNotifyHost(RTTHREAD self, void *pvUser);
455
456 DECLARE_CLS_COPY_CTOR_ASSIGN_NOOP(Service);
457};
458
459
460/**
461 * Gets the current timestamp.
462 *
463 * Since the RTTimeNow resolution can be very coarse, this method takes some
464 * simple steps to try avoid returning the same timestamp for two consecutive
465 * calls. Code like getOldNotification() more or less assumes unique
466 * timestamps.
467 *
468 * @returns Nanosecond timestamp.
469 */
470uint64_t Service::getCurrentTimestamp(void)
471{
472 RTTIMESPEC time;
473 uint64_t u64NanoTS = RTTimeSpecGetNano(RTTimeNow(&time));
474 if (mPrevTimestamp - u64NanoTS > mcTimestampAdjustments)
475 mcTimestampAdjustments = 0;
476 else
477 {
478 mcTimestampAdjustments++;
479 u64NanoTS = mPrevTimestamp + 1;
480 }
481 this->mPrevTimestamp = u64NanoTS;
482 return u64NanoTS;
483}
484
485/**
486 * Set a block of properties in the property registry, checking the validity
487 * of the arguments passed.
488 *
489 * @returns iprt status value
490 * @param cParms the number of HGCM parameters supplied
491 * @param paParms the array of HGCM parameters
492 * @thread HGCM
493 */
494int Service::setPropertyBlock(uint32_t cParms, VBOXHGCMSVCPARM paParms[])
495{
496 const char **papszNames;
497 const char **papszValues;
498 const char **papszFlags;
499 uint64_t *paNsTimestamps;
500 uint32_t cbDummy;
501 int rc = VINF_SUCCESS;
502
503 /*
504 * Get and validate the parameters
505 */
506 if ( cParms != 4
507 || RT_FAILURE(HGCMSvcGetPv(&paParms[0], (void **)&papszNames, &cbDummy))
508 || RT_FAILURE(HGCMSvcGetPv(&paParms[1], (void **)&papszValues, &cbDummy))
509 || RT_FAILURE(HGCMSvcGetPv(&paParms[2], (void **)&paNsTimestamps, &cbDummy))
510 || RT_FAILURE(HGCMSvcGetPv(&paParms[3], (void **)&papszFlags, &cbDummy))
511 )
512 rc = VERR_INVALID_PARAMETER;
513 /** @todo validate the array sizes... */
514 else
515 {
516 for (unsigned i = 0; RT_SUCCESS(rc) && papszNames[i] != NULL; ++i)
517 {
518 if ( !RT_VALID_PTR(papszNames[i])
519 || !RT_VALID_PTR(papszValues[i])
520 || !RT_VALID_PTR(papszFlags[i])
521 )
522 rc = VERR_INVALID_POINTER;
523 else
524 {
525 uint32_t fFlagsIgn;
526 rc = GuestPropValidateFlags(papszFlags[i], &fFlagsIgn);
527 }
528 }
529 if (RT_SUCCESS(rc))
530 {
531 /*
532 * Add the properties. No way to roll back here.
533 */
534 for (unsigned i = 0; papszNames[i] != NULL; ++i)
535 {
536 uint32_t fFlags;
537 rc = GuestPropValidateFlags(papszFlags[i], &fFlags);
538 AssertRCBreak(rc);
539 /*
540 * Handle names which are read-only for the guest.
541 */
542 if (checkHostReserved(papszNames[i]))
543 fFlags |= GUEST_PROP_F_RDONLYGUEST;
544
545 Property *pProp = getPropertyInternal(papszNames[i]);
546 if (pProp)
547 {
548 /* Update existing property. */
549 rc = pProp->mValue.assignNoThrow(papszValues[i]);
550 AssertRCBreak(rc);
551 pProp->mTimestamp = paNsTimestamps[i];
552 pProp->mFlags = fFlags;
553 }
554 else
555 {
556 /* Create a new property */
557 try
558 {
559 pProp = new Property(papszNames[i], papszValues[i], paNsTimestamps[i], fFlags);
560 }
561 catch (std::bad_alloc &)
562 {
563 return VERR_NO_MEMORY;
564 }
565 if (RTStrSpaceInsert(&mhProperties, &pProp->mStrCore))
566 mcProperties++;
567 else
568 {
569 delete pProp;
570 rc = VERR_INTERNAL_ERROR_3;
571 AssertFailedBreak();
572 }
573 }
574 }
575 }
576 }
577
578 return rc;
579}
580
581/**
582 * Retrieve a value from the property registry by name, checking the validity
583 * of the arguments passed. If the guest has not allocated enough buffer
584 * space for the value then we return VERR_OVERFLOW and set the size of the
585 * buffer needed in the "size" HGCM parameter. If the name was not found at
586 * all, we return VERR_NOT_FOUND.
587 *
588 * @returns iprt status value
589 * @param cParms the number of HGCM parameters supplied
590 * @param paParms the array of HGCM parameters
591 * @thread HGCM
592 */
593int Service::getProperty(uint32_t cParms, VBOXHGCMSVCPARM paParms[])
594{
595 int rc;
596 const char *pcszName = NULL; /* shut up gcc */
597 char *pchBuf = NULL; /* shut up MSC */
598 uint32_t cbName;
599 uint32_t cbBuf = 0; /* shut up MSC */
600
601 /*
602 * Get and validate the parameters
603 */
604 LogFlowThisFunc(("\n"));
605 if ( cParms != 4 /* Hardcoded value as the next lines depend on it. */
606 || RT_FAILURE(HGCMSvcGetCStr(&paParms[0], &pcszName, &cbName)) /* name */
607 || RT_FAILURE(HGCMSvcGetBuf(&paParms[1], (void **)&pchBuf, &cbBuf)) /* buffer */
608 )
609 rc = VERR_INVALID_PARAMETER;
610 else
611 rc = GuestPropValidateName(pcszName, cbName);
612 if (RT_FAILURE(rc))
613 {
614 LogFlowThisFunc(("rc = %Rrc\n", rc));
615 return rc;
616 }
617
618 /*
619 * Read and set the values we will return
620 */
621
622 /* Get the property. */
623 Property *pProp = getPropertyInternal(pcszName);
624 if (pProp)
625 {
626 char szFlags[GUEST_PROP_MAX_FLAGS_LEN];
627 rc = GuestPropWriteFlags(pProp->mFlags, szFlags);
628 if (RT_SUCCESS(rc))
629 {
630 /* Check that the buffer is big enough */
631 size_t const cbFlags = strlen(szFlags) + 1;
632 size_t const cbValue = pProp->mValue.length() + 1;
633 size_t const cbNeeded = cbValue + cbFlags;
634 HGCMSvcSetU32(&paParms[3], (uint32_t)cbNeeded);
635 if (cbBuf >= cbNeeded)
636 {
637 /* Write the value, flags and timestamp */
638 memcpy(pchBuf, pProp->mValue.c_str(), cbValue);
639 memcpy(pchBuf + cbValue, szFlags, cbFlags);
640
641 HGCMSvcSetU64(&paParms[2], pProp->mTimestamp);
642
643 /*
644 * Done! Do exit logging and return.
645 */
646 Log2(("Queried string %s, value=%s, timestamp=%lld, flags=%s\n",
647 pcszName, pProp->mValue.c_str(), pProp->mTimestamp, szFlags));
648 }
649 else
650 rc = VERR_BUFFER_OVERFLOW;
651 }
652 }
653 else
654 rc = VERR_NOT_FOUND;
655
656 LogFlowThisFunc(("rc = %Rrc (%s)\n", rc, pcszName));
657 return rc;
658}
659
660/**
661 * Set a value in the property registry by name, checking the validity
662 * of the arguments passed.
663 *
664 * @returns iprt status value
665 * @param cParms the number of HGCM parameters supplied
666 * @param paParms the array of HGCM parameters
667 * @param isGuest is this call coming from the guest (or the host)?
668 * @throws std::bad_alloc if an out of memory condition occurs
669 * @thread HGCM
670 */
671int Service::setProperty(uint32_t cParms, VBOXHGCMSVCPARM paParms[], bool isGuest)
672{
673 const char *pcszName = NULL; /* shut up gcc */
674 const char *pcszValue = NULL; /* ditto */
675 const char *pcszFlags = NULL;
676 uint32_t cbName = 0; /* ditto */
677 uint32_t cbValue = 0; /* ditto */
678 uint32_t cbFlags = 0;
679 uint32_t fFlags = GUEST_PROP_F_NILFLAG;
680 uint64_t u64TimeNano = getCurrentTimestamp();
681
682 LogFlowThisFunc(("\n"));
683
684 /*
685 * General parameter correctness checking.
686 */
687 int rc = VINF_SUCCESS;
688 if ( cParms < 2 /* Hardcoded value as the next lines depend on it these range checks. */
689 || cParms > 3
690 || RT_FAILURE(HGCMSvcGetCStr(&paParms[0], &pcszName, &cbName)) /* name */
691 || RT_FAILURE(HGCMSvcGetCStr(&paParms[1], &pcszValue, &cbValue)) /* value */
692 || ( cParms == 3
693 && RT_FAILURE(HGCMSvcGetCStr(&paParms[2], &pcszFlags, &cbFlags)) /* flags */
694 )
695 )
696 rc = VERR_INVALID_PARAMETER;
697
698 /*
699 * Check the values passed in the parameters for correctness.
700 */
701 if (RT_SUCCESS(rc))
702 rc = GuestPropValidateName(pcszName, cbName);
703 if (RT_SUCCESS(rc))
704 rc = GuestPropValidateValue(pcszValue, cbValue);
705 if (cParms == 3 && RT_SUCCESS(rc))
706 rc = GuestPropValidateFlags(pcszFlags, &fFlags);
707 if (RT_FAILURE(rc))
708 {
709 LogFlowThisFunc(("rc = %Rrc\n", rc));
710 return rc;
711 }
712
713 /*
714 * Hand it over to the internal setter method.
715 */
716 rc = setPropertyInternal(pcszName, pcszValue, fFlags, u64TimeNano, isGuest);
717
718 LogFlowThisFunc(("%s=%s, rc=%Rrc\n", pcszName, pcszValue, rc));
719 return rc;
720}
721
722/**
723 * Internal property setter.
724 *
725 * @returns VBox status code.
726 * @param pcszName The property name.
727 * @param pcszValue The new value.
728 * @param fFlags The flags.
729 * @param nsTimestamp The timestamp.
730 * @param fIsGuest Is it the guest calling.
731 * @throws std::bad_alloc if an out of memory condition occurs
732 * @thread HGCM
733 */
734int Service::setPropertyInternal(const char *pcszName, const char *pcszValue, uint32_t fFlags, uint64_t nsTimestamp,
735 bool fIsGuest /*= false*/)
736{
737 /*
738 * If the property already exists, check its flags to see if we are allowed
739 * to change it.
740 */
741 Property *pProp = getPropertyInternal(pcszName);
742 int rc = checkPermission(pProp ? pProp->mFlags : GUEST_PROP_F_NILFLAG, fIsGuest);
743 /*
744 * Handle names which are read-only for the guest.
745 */
746 if (rc == VINF_SUCCESS && checkHostReserved(pcszName))
747 {
748 if (fIsGuest)
749 rc = VERR_PERMISSION_DENIED;
750 else
751 fFlags |= GUEST_PROP_F_RDONLYGUEST;
752 }
753 if (rc == VINF_SUCCESS)
754 {
755 /*
756 * Set the actual value
757 */
758 if (pProp)
759 {
760 rc = pProp->mValue.assignNoThrow(pcszValue);
761 if (RT_SUCCESS(rc))
762 {
763 pProp->mTimestamp = nsTimestamp;
764 pProp->mFlags = fFlags;
765 }
766 }
767 else if (mcProperties < GUEST_PROP_MAX_PROPS)
768 {
769 try
770 {
771 /* Create a new string space record. */
772 pProp = new Property(pcszName, pcszValue, nsTimestamp, fFlags);
773 AssertPtr(pProp);
774
775 if (RTStrSpaceInsert(&mhProperties, &pProp->mStrCore))
776 mcProperties++;
777 else
778 {
779 AssertFailed();
780 delete pProp;
781
782 rc = VERR_ALREADY_EXISTS;
783 }
784 }
785 catch (std::bad_alloc &)
786 {
787 rc = VERR_NO_MEMORY;
788 }
789 }
790 else
791 rc = VERR_TOO_MUCH_DATA;
792
793 /*
794 * Send a notification to the guest and host and return.
795 */
796 // if (fIsGuest) /* Notify the host even for properties that the host
797 // * changed. Less efficient, but ensures consistency. */
798 int rc2 = doNotifications(pcszName, nsTimestamp);
799 if (RT_SUCCESS(rc))
800 rc = rc2;
801 }
802
803 LogFlowThisFunc(("%s=%s, rc=%Rrc\n", pcszName, pcszValue, rc));
804 return rc;
805}
806
807
808/**
809 * Remove a value in the property registry by name, checking the validity
810 * of the arguments passed.
811 *
812 * @returns iprt status value
813 * @param cParms the number of HGCM parameters supplied
814 * @param paParms the array of HGCM parameters
815 * @param isGuest is this call coming from the guest (or the host)?
816 * @thread HGCM
817 */
818int Service::delProperty(uint32_t cParms, VBOXHGCMSVCPARM paParms[], bool isGuest)
819{
820 int rc;
821 const char *pcszName = NULL; /* shut up gcc */
822 uint32_t cbName;
823
824 LogFlowThisFunc(("\n"));
825
826 /*
827 * Check the user-supplied parameters.
828 */
829 if ( (cParms == 1) /* Hardcoded value as the next lines depend on it. */
830 && RT_SUCCESS(HGCMSvcGetCStr(&paParms[0], &pcszName, &cbName)) /* name */
831 )
832 rc = GuestPropValidateName(pcszName, cbName);
833 else
834 rc = VERR_INVALID_PARAMETER;
835 if (RT_FAILURE(rc))
836 {
837 LogFlowThisFunc(("rc=%Rrc\n", rc));
838 return rc;
839 }
840
841 /*
842 * If the property exists, check its flags to see if we are allowed
843 * to change it.
844 */
845 Property *pProp = getPropertyInternal(pcszName);
846 if (pProp)
847 rc = checkPermission(pProp->mFlags, isGuest);
848
849 /*
850 * And delete the property if all is well.
851 */
852 if (rc == VINF_SUCCESS && pProp)
853 {
854 uint64_t nsTimestamp = getCurrentTimestamp();
855 PRTSTRSPACECORE pStrCore = RTStrSpaceRemove(&mhProperties, pProp->mStrCore.pszString);
856 AssertPtr(pStrCore); NOREF(pStrCore);
857 mcProperties--;
858 delete pProp;
859 // if (isGuest) /* Notify the host even for properties that the host
860 // * changed. Less efficient, but ensures consistency. */
861 int rc2 = doNotifications(pcszName, nsTimestamp);
862 if (RT_SUCCESS(rc))
863 rc = rc2;
864 }
865
866 LogFlowThisFunc(("%s: rc=%Rrc\n", pcszName, rc));
867 return rc;
868}
869
870/**
871 * Enumeration data shared between enumPropsCallback and Service::enumProps.
872 */
873typedef struct ENUMDATA
874{
875 const char *pszPattern; /**< The pattern to match properties against. */
876 char *pchCur; /**< The current buffer postion. */
877 size_t cbLeft; /**< The amount of available buffer space. */
878 size_t cbNeeded; /**< The amount of needed buffer space. */
879} ENUMDATA;
880
881/**
882 * @callback_method_impl{FNRTSTRSPACECALLBACK}
883 */
884static DECLCALLBACK(int) enumPropsCallback(PRTSTRSPACECORE pStr, void *pvUser)
885{
886 Property *pProp = (Property *)pStr;
887 ENUMDATA *pEnum = (ENUMDATA *)pvUser;
888
889 /* Included in the enumeration? */
890 if (!pProp->Matches(pEnum->pszPattern))
891 return 0;
892
893 /* Convert the non-string members into strings. */
894 char szTimestamp[256];
895 size_t const cbTimestamp = RTStrFormatNumber(szTimestamp, pProp->mTimestamp, 10, 0, 0, 0) + 1;
896
897 char szFlags[GUEST_PROP_MAX_FLAGS_LEN];
898 int rc = GuestPropWriteFlags(pProp->mFlags, szFlags);
899 if (RT_FAILURE(rc))
900 return rc;
901 size_t const cbFlags = strlen(szFlags) + 1;
902
903 /* Calculate the buffer space requirements. */
904 size_t const cbName = pProp->mName.length() + 1;
905 size_t const cbValue = pProp->mValue.length() + 1;
906 size_t const cbRequired = cbName + cbValue + cbTimestamp + cbFlags;
907 pEnum->cbNeeded += cbRequired;
908
909 /* Sufficient buffer space? */
910 if (cbRequired > pEnum->cbLeft)
911 {
912 pEnum->cbLeft = 0;
913 return 0; /* don't quit */
914 }
915 pEnum->cbLeft -= cbRequired;
916
917 /* Append the property to the buffer. */
918 char *pchCur = pEnum->pchCur;
919 pEnum->pchCur += cbRequired;
920
921 memcpy(pchCur, pProp->mName.c_str(), cbName);
922 pchCur += cbName;
923
924 memcpy(pchCur, pProp->mValue.c_str(), cbValue);
925 pchCur += cbValue;
926
927 memcpy(pchCur, szTimestamp, cbTimestamp);
928 pchCur += cbTimestamp;
929
930 memcpy(pchCur, szFlags, cbFlags);
931 pchCur += cbFlags;
932
933 Assert(pchCur == pEnum->pchCur);
934 return 0;
935}
936
937/**
938 * Enumerate guest properties by mask, checking the validity
939 * of the arguments passed.
940 *
941 * @returns iprt status value
942 * @param cParms the number of HGCM parameters supplied
943 * @param paParms the array of HGCM parameters
944 * @thread HGCM
945 */
946int Service::enumProps(uint32_t cParms, VBOXHGCMSVCPARM paParms[])
947{
948 int rc = VINF_SUCCESS;
949
950 /*
951 * Get the HGCM function arguments.
952 */
953 char const *pchPatterns = NULL;
954 char *pchBuf = NULL;
955 uint32_t cbPatterns = 0;
956 uint32_t cbBuf = 0;
957 LogFlowThisFunc(("\n"));
958 if ( (cParms != 3) /* Hardcoded value as the next lines depend on it. */
959 || RT_FAILURE(HGCMSvcGetCStr(&paParms[0], &pchPatterns, &cbPatterns)) /* patterns */
960 || RT_FAILURE(HGCMSvcGetBuf(&paParms[1], (void **)&pchBuf, &cbBuf)) /* return buffer */
961 )
962 rc = VERR_INVALID_PARAMETER;
963 if (RT_SUCCESS(rc) && cbPatterns > GUEST_PROP_MAX_PATTERN_LEN)
964 rc = VERR_TOO_MUCH_DATA;
965
966 /*
967 * First repack the patterns into the format expected by RTStrSimplePatternMatch()
968 */
969 char szPatterns[GUEST_PROP_MAX_PATTERN_LEN];
970 if (RT_SUCCESS(rc))
971 {
972 for (unsigned i = 0; i < cbPatterns - 1; ++i)
973 {
974 char ch = pchPatterns[i];
975 if (pchPatterns[i] != '\0')
976 { /* likely*/ }
977 else
978 {
979 /* Since the RTStrValidateEncodingEx call in HGCMSvcGetCStr stops at the
980 first terminator, we have to validate all subsequent pattern strings. */
981 rc = RTStrValidateEncodingEx(&pchPatterns[i + 1], cbPatterns - i -1, RTSTR_VALIDATE_ENCODING_ZERO_TERMINATED);
982 ASSERT_GUEST_RC_BREAK(rc);
983 ch = '|';
984 }
985 szPatterns[i] = ch;
986 }
987 szPatterns[cbPatterns - 1] = '\0';
988 }
989
990 /*
991 * Next enumerate into the buffer.
992 */
993 if (RT_SUCCESS(rc))
994 {
995 ENUMDATA EnumData;
996 EnumData.pszPattern = szPatterns;
997 EnumData.pchCur = pchBuf;
998 EnumData.cbLeft = cbBuf;
999 EnumData.cbNeeded = 0;
1000 rc = RTStrSpaceEnumerate(&mhProperties, enumPropsCallback, &EnumData);
1001 AssertRCSuccess(rc);
1002 if (RT_SUCCESS(rc))
1003 {
1004 HGCMSvcSetU32(&paParms[2], (uint32_t)(EnumData.cbNeeded + 4));
1005 if (EnumData.cbLeft >= 4)
1006 {
1007 /* The final terminators. */
1008 EnumData.pchCur[0] = '\0';
1009 EnumData.pchCur[1] = '\0';
1010 EnumData.pchCur[2] = '\0';
1011 EnumData.pchCur[3] = '\0';
1012 }
1013 else
1014 rc = VERR_BUFFER_OVERFLOW;
1015 }
1016 }
1017
1018 return rc;
1019}
1020
1021
1022/** Helper query used by getOldNotification
1023 * @throws nothing
1024 */
1025int Service::getOldNotificationInternal(const char *pszPatterns, uint64_t nsTimestamp, Property *pProp)
1026{
1027 /* We count backwards, as the guest should normally be querying the
1028 * most recent events. */
1029 int rc = VWRN_NOT_FOUND;
1030 PropertyList::reverse_iterator it = mGuestNotifications.rbegin();
1031 for (; it != mGuestNotifications.rend(); ++it)
1032 if (it->mTimestamp == nsTimestamp)
1033 {
1034 rc = VINF_SUCCESS;
1035 break;
1036 }
1037
1038 /* Now look for an event matching the patterns supplied. The base()
1039 * member conveniently points to the following element. */
1040 PropertyList::iterator base = it.base();
1041 for (; base != mGuestNotifications.end(); ++base)
1042 if (base->Matches(pszPatterns))
1043 {
1044 try
1045 {
1046 *pProp = *base;
1047 }
1048 catch (std::bad_alloc &)
1049 {
1050 rc = VERR_NO_MEMORY;
1051 }
1052 return rc;
1053 }
1054 *pProp = Property();
1055 return rc;
1056}
1057
1058
1059/** Helper query used by getNotification */
1060int Service::getNotificationWriteOut(uint32_t cParms, VBOXHGCMSVCPARM paParms[], Property const &rProp, bool fWasDeleted)
1061{
1062 AssertReturn(cParms == 4, VERR_INVALID_PARAMETER); /* Basic sanity checking. */
1063
1064 /* Format the data to write to the buffer. */
1065 char *pchBuf;
1066 uint32_t cbBuf;
1067 int rc = HGCMSvcGetBuf(&paParms[2], (void **)&pchBuf, &cbBuf);
1068 if (RT_SUCCESS(rc))
1069 {
1070 char szFlags[GUEST_PROP_MAX_FLAGS_LEN];
1071 rc = GuestPropWriteFlags(rProp.mFlags, szFlags);
1072 if (RT_SUCCESS(rc))
1073 {
1074 HGCMSvcSetU64(&paParms[1], rProp.mTimestamp);
1075
1076 size_t const cbFlags = strlen(szFlags) + 1;
1077 size_t const cbName = rProp.mName.length() + 1;
1078 size_t const cbValue = rProp.mValue.length() + 1;
1079 size_t const cbWasDeleted = 2;
1080 size_t const cbNeeded = cbName + cbValue + cbFlags + cbWasDeleted;
1081 HGCMSvcSetU32(&paParms[3], (uint32_t)cbNeeded);
1082 if (cbNeeded <= cbBuf)
1083 {
1084 /* Buffer layout: Name\0Value\0Flags\0fWasDeleted\0. */
1085 memcpy(pchBuf, rProp.mName.c_str(), cbName);
1086 pchBuf += cbName;
1087 memcpy(pchBuf, rProp.mValue.c_str(), cbValue);
1088 pchBuf += cbValue;
1089 memcpy(pchBuf, szFlags, cbFlags);
1090 pchBuf += cbFlags;
1091 *pchBuf++ = fWasDeleted ? '1' : '0';
1092 *pchBuf++ = '\0';
1093 }
1094 else
1095 rc = VERR_BUFFER_OVERFLOW;
1096 }
1097 }
1098 return rc;
1099}
1100
1101
1102/**
1103 * Get the next guest notification.
1104 *
1105 * @returns iprt status value
1106 * @param u32ClientId the client ID
1107 * @param callHandle handle
1108 * @param cParms the number of HGCM parameters supplied
1109 * @param paParms the array of HGCM parameters
1110 * @thread HGCM
1111 * @throws nothing
1112 */
1113int Service::getNotification(uint32_t u32ClientId, VBOXHGCMCALLHANDLE callHandle,
1114 uint32_t cParms, VBOXHGCMSVCPARM paParms[])
1115{
1116 int rc = VINF_SUCCESS;
1117 char *pszPatterns = NULL; /* shut up gcc */
1118 char *pchBuf;
1119 uint32_t cchPatterns = 0;
1120 uint32_t cbBuf = 0;
1121 uint64_t nsTimestamp;
1122
1123 /*
1124 * Get the HGCM function arguments and perform basic verification.
1125 */
1126 LogFlowThisFunc(("\n"));
1127 if ( cParms != 4 /* Hardcoded value as the next lines depend on it. */
1128 || RT_FAILURE(HGCMSvcGetStr(&paParms[0], &pszPatterns, &cchPatterns)) /* patterns */
1129 || RT_FAILURE(HGCMSvcGetU64(&paParms[1], &nsTimestamp)) /* timestamp */
1130 || RT_FAILURE(HGCMSvcGetBuf(&paParms[2], (void **)&pchBuf, &cbBuf)) /* return buffer */
1131 )
1132 rc = VERR_INVALID_PARAMETER;
1133 else
1134 {
1135 LogFlow(("pszPatterns=%s, nsTimestamp=%llu\n", pszPatterns, nsTimestamp));
1136
1137 /*
1138 * If no timestamp was supplied or no notification was found in the queue
1139 * of old notifications, enqueue the request in the waiting queue.
1140 */
1141 Property prop;
1142 if (RT_SUCCESS(rc) && nsTimestamp != 0)
1143 rc = getOldNotification(pszPatterns, nsTimestamp, &prop);
1144 if (RT_SUCCESS(rc))
1145 {
1146 if (prop.isNull())
1147 {
1148 /*
1149 * Check if the client already had the same request.
1150 * Complete the old request with an error in this case.
1151 * Protection against clients, which cancel and resubmits requests.
1152 */
1153 uint32_t cPendingWaits = 0;
1154 CallList::iterator it = mGuestWaiters.begin();
1155 while (it != mGuestWaiters.end())
1156 {
1157 if (u32ClientId == it->u32ClientId)
1158 {
1159 const char *pszPatternsExisting;
1160 uint32_t cchPatternsExisting;
1161 int rc3 = HGCMSvcGetCStr(&it->mParms[0], &pszPatternsExisting, &cchPatternsExisting);
1162 if ( RT_SUCCESS(rc3)
1163 && RTStrCmp(pszPatterns, pszPatternsExisting) == 0)
1164 {
1165 /* Complete the old request. */
1166 mpHelpers->pfnCallComplete(it->mHandle, VERR_INTERRUPTED);
1167 it = mGuestWaiters.erase(it);
1168 }
1169 else if (mpHelpers->pfnIsCallCancelled(it->mHandle))
1170 {
1171 /* Cleanup cancelled request. */
1172 mpHelpers->pfnCallComplete(it->mHandle, VERR_INTERRUPTED);
1173 it = mGuestWaiters.erase(it);
1174 }
1175 else
1176 {
1177 /** @todo check if cancelled. */
1178 cPendingWaits++;
1179 ++it;
1180 }
1181 }
1182 else
1183 ++it;
1184 }
1185
1186 if (cPendingWaits < GUEST_PROP_MAX_GUEST_CONCURRENT_WAITS)
1187 {
1188 try
1189 {
1190 mGuestWaiters.push_back(GuestCall(u32ClientId, callHandle, GUEST_PROP_FN_GET_NOTIFICATION,
1191 cParms, paParms, rc));
1192 rc = VINF_HGCM_ASYNC_EXECUTE;
1193 }
1194 catch (std::bad_alloc &)
1195 {
1196 rc = VERR_NO_MEMORY;
1197 }
1198 }
1199 else
1200 {
1201 LogFunc(("Too many pending waits already!\n"));
1202 rc = VERR_OUT_OF_RESOURCES;
1203 }
1204 }
1205 /*
1206 * Otherwise reply at once with the enqueued notification we found.
1207 */
1208 else
1209 {
1210 int rc2 = getNotificationWriteOut(cParms, paParms, prop, !getPropertyInternal(prop.mName.c_str()));
1211 if (RT_FAILURE(rc2))
1212 rc = rc2;
1213 }
1214 }
1215 }
1216
1217 LogFlowThisFunc(("returning rc=%Rrc\n", rc));
1218 return rc;
1219}
1220
1221
1222/**
1223 * Notify the service owner and the guest that a property has been
1224 * added/deleted/changed
1225 *
1226 * @param pszProperty The name of the property which has changed.
1227 * @param nsTimestamp The time at which the change took place.
1228 * @throws nothing.
1229 * @thread HGCM service
1230 */
1231int Service::doNotifications(const char *pszProperty, uint64_t nsTimestamp)
1232{
1233 AssertPtrReturn(pszProperty, VERR_INVALID_POINTER);
1234 LogFlowThisFunc(("pszProperty=%s, nsTimestamp=%llu\n", pszProperty, nsTimestamp));
1235 /* Ensure that our timestamp is different to the last one. */
1236 if ( !mGuestNotifications.empty()
1237 && nsTimestamp == mGuestNotifications.back().mTimestamp)
1238 ++nsTimestamp;
1239
1240 /*
1241 * Don't keep too many changes around.
1242 */
1243 if (mGuestNotifications.size() >= GUEST_PROP_MAX_GUEST_NOTIFICATIONS)
1244 mGuestNotifications.pop_front();
1245
1246 /*
1247 * Try to find the property. Create a change event if we find it and a
1248 * delete event if we do not.
1249 */
1250 Property prop;
1251 int rc = prop.mName.assignNoThrow(pszProperty);
1252 AssertRCReturn(rc, rc);
1253 prop.mTimestamp = nsTimestamp;
1254 /* prop is currently a delete event for pszProperty */
1255 Property const * const pProp = getPropertyInternal(pszProperty);
1256 if (pProp)
1257 {
1258 /* Make prop into a change event. */
1259 rc = prop.mValue.assignNoThrow(pProp->mValue);
1260 AssertRCReturn(rc, rc);
1261 prop.mFlags = pProp->mFlags;
1262 }
1263
1264 /* Release guest waiters if applicable and add the event
1265 * to the queue for guest notifications */
1266 CallList::iterator it = mGuestWaiters.begin();
1267 if (it != mGuestWaiters.end())
1268 {
1269 const char *pszPatterns = NULL;
1270 uint32_t cchPatterns;
1271 HGCMSvcGetCStr(&it->mParms[0], &pszPatterns, &cchPatterns);
1272
1273 while (it != mGuestWaiters.end())
1274 {
1275 if (prop.Matches(pszPatterns))
1276 {
1277 int rc2 = getNotificationWriteOut(it->mParmsCnt, it->mParms, prop, !pProp);
1278 if (RT_SUCCESS(rc2))
1279 rc2 = it->mRc;
1280 mpHelpers->pfnCallComplete(it->mHandle, rc2);
1281 it = mGuestWaiters.erase(it);
1282 }
1283 else
1284 ++it;
1285 }
1286 }
1287
1288 try
1289 {
1290 mGuestNotifications.push_back(prop);
1291 }
1292 catch (std::bad_alloc &)
1293 {
1294 rc = VERR_NO_MEMORY;
1295 }
1296
1297 if ( RT_SUCCESS(rc)
1298 && mpfnHostCallback)
1299 {
1300 /*
1301 * Host notifications - first case: if the property exists then send its
1302 * current value
1303 */
1304 if (pProp)
1305 {
1306 char szFlags[GUEST_PROP_MAX_FLAGS_LEN];
1307 /* Send out a host notification */
1308 const char *pszValue = prop.mValue.c_str();
1309 rc = GuestPropWriteFlags(prop.mFlags, szFlags);
1310 if (RT_SUCCESS(rc))
1311 rc = notifyHost(pszProperty, pszValue, nsTimestamp, szFlags);
1312 }
1313 /*
1314 * Host notifications - second case: if the property does not exist then
1315 * send the host an empty value
1316 */
1317 else
1318 {
1319 /* Send out a host notification */
1320 rc = notifyHost(pszProperty, NULL, nsTimestamp, "");
1321 }
1322 }
1323
1324 LogFlowThisFunc(("returning rc=%Rrc\n", rc));
1325 return rc;
1326}
1327
1328static DECLCALLBACK(void)
1329notifyHostAsyncWorker(PFNHGCMSVCEXT pfnHostCallback, void *pvHostData, PGUESTPROPHOSTCALLBACKDATA pHostCallbackData)
1330{
1331 pfnHostCallback(pvHostData, 0 /*u32Function*/, (void *)pHostCallbackData, sizeof(GUESTPROPHOSTCALLBACKDATA));
1332 RTMemFree(pHostCallbackData);
1333}
1334
1335/**
1336 * Notify the service owner that a property has been added/deleted/changed.
1337 * @returns IPRT status value
1338 * @param pszName the property name
1339 * @param pszValue the new value, or NULL if the property was deleted
1340 * @param nsTimestamp the time of the change
1341 * @param pszFlags the new flags string
1342 */
1343int Service::notifyHost(const char *pszName, const char *pszValue, uint64_t nsTimestamp, const char *pszFlags)
1344{
1345 LogFlowFunc(("pszName=%s, pszValue=%s, nsTimestamp=%llu, pszFlags=%s\n", pszName, pszValue, nsTimestamp, pszFlags));
1346 int rc;
1347
1348 /* Allocate buffer for the callback data and strings. */
1349 size_t cbName = pszName? strlen(pszName): 0;
1350 size_t cbValue = pszValue? strlen(pszValue): 0;
1351 size_t cbFlags = pszFlags? strlen(pszFlags): 0;
1352 size_t cbAlloc = sizeof(GUESTPROPHOSTCALLBACKDATA) + cbName + cbValue + cbFlags + 3;
1353 PGUESTPROPHOSTCALLBACKDATA pHostCallbackData = (PGUESTPROPHOSTCALLBACKDATA)RTMemAlloc(cbAlloc);
1354 if (pHostCallbackData)
1355 {
1356 uint8_t *pu8 = (uint8_t *)pHostCallbackData;
1357 pu8 += sizeof(GUESTPROPHOSTCALLBACKDATA);
1358
1359 pHostCallbackData->u32Magic = GUESTPROPHOSTCALLBACKDATA_MAGIC;
1360
1361 pHostCallbackData->pcszName = (const char *)pu8;
1362 memcpy(pu8, pszName, cbName);
1363 pu8 += cbName;
1364 *pu8++ = 0;
1365
1366 /* NULL value means property was deleted. */
1367 pHostCallbackData->pcszValue = pszValue ? (const char *)pu8 : NULL;
1368 memcpy(pu8, pszValue, cbValue);
1369 pu8 += cbValue;
1370 *pu8++ = 0;
1371
1372 pHostCallbackData->u64Timestamp = nsTimestamp;
1373
1374 pHostCallbackData->pcszFlags = (const char *)pu8;
1375 memcpy(pu8, pszFlags, cbFlags);
1376 pu8 += cbFlags;
1377 *pu8++ = 0;
1378
1379 rc = RTReqQueueCallEx(mhReqQNotifyHost, NULL, 0, RTREQFLAGS_VOID | RTREQFLAGS_NO_WAIT,
1380 (PFNRT)notifyHostAsyncWorker, 3,
1381 mpfnHostCallback, mpvHostData, pHostCallbackData);
1382 if (RT_FAILURE(rc))
1383 {
1384 RTMemFree(pHostCallbackData);
1385 }
1386 }
1387 else
1388 {
1389 rc = VERR_NO_MEMORY;
1390 }
1391 LogFlowFunc(("returning rc=%Rrc\n", rc));
1392 return rc;
1393}
1394
1395
1396/**
1397 * Handle an HGCM service call.
1398 * @interface_method_impl{VBOXHGCMSVCFNTABLE,pfnCall}
1399 * @note All functions which do not involve an unreasonable delay will be
1400 * handled synchronously. If needed, we will add a request handler
1401 * thread in future for those which do.
1402 *
1403 * @thread HGCM
1404 */
1405void Service::call (VBOXHGCMCALLHANDLE callHandle, uint32_t u32ClientID,
1406 void * /* pvClient */, uint32_t eFunction, uint32_t cParms,
1407 VBOXHGCMSVCPARM paParms[])
1408{
1409 int rc;
1410 LogFlowFunc(("u32ClientID = %d, fn = %d, cParms = %d, pparms = %p\n",
1411 u32ClientID, eFunction, cParms, paParms));
1412
1413 switch (eFunction)
1414 {
1415 /* The guest wishes to read a property */
1416 case GUEST_PROP_FN_GET_PROP:
1417 LogFlowFunc(("GET_PROP\n"));
1418 rc = getProperty(cParms, paParms);
1419 break;
1420
1421 /* The guest wishes to set a property */
1422 case GUEST_PROP_FN_SET_PROP:
1423 LogFlowFunc(("SET_PROP\n"));
1424 rc = setProperty(cParms, paParms, true);
1425 break;
1426
1427 /* The guest wishes to set a property value */
1428 case GUEST_PROP_FN_SET_PROP_VALUE:
1429 LogFlowFunc(("SET_PROP_VALUE\n"));
1430 rc = setProperty(cParms, paParms, true);
1431 break;
1432
1433 /* The guest wishes to remove a configuration value */
1434 case GUEST_PROP_FN_DEL_PROP:
1435 LogFlowFunc(("DEL_PROP\n"));
1436 rc = delProperty(cParms, paParms, true);
1437 break;
1438
1439 /* The guest wishes to enumerate all properties */
1440 case GUEST_PROP_FN_ENUM_PROPS:
1441 LogFlowFunc(("ENUM_PROPS\n"));
1442 rc = enumProps(cParms, paParms);
1443 break;
1444
1445 /* The guest wishes to get the next property notification */
1446 case GUEST_PROP_FN_GET_NOTIFICATION:
1447 LogFlowFunc(("GET_NOTIFICATION\n"));
1448 rc = getNotification(u32ClientID, callHandle, cParms, paParms);
1449 break;
1450
1451 default:
1452 rc = VERR_NOT_IMPLEMENTED;
1453 }
1454 LogFlowFunc(("rc = %Rrc\n", rc));
1455 if (rc != VINF_HGCM_ASYNC_EXECUTE)
1456 mpHelpers->pfnCallComplete(callHandle, rc);
1457}
1458
1459/**
1460 * Enumeration data shared between dbgInfoCallback and Service::dbgInfoShow.
1461 */
1462typedef struct ENUMDBGINFO
1463{
1464 PCDBGFINFOHLP pHlp;
1465} ENUMDBGINFO;
1466
1467static DECLCALLBACK(int) dbgInfoCallback(PRTSTRSPACECORE pStr, void *pvUser)
1468{
1469 Property *pProp = (Property *)pStr;
1470 PCDBGFINFOHLP pHlp = ((ENUMDBGINFO *)pvUser)->pHlp;
1471
1472 char szFlags[GUEST_PROP_MAX_FLAGS_LEN];
1473 int rc = GuestPropWriteFlags(pProp->mFlags, szFlags);
1474 if (RT_FAILURE(rc))
1475 RTStrPrintf(szFlags, sizeof(szFlags), "???");
1476
1477 pHlp->pfnPrintf(pHlp, "%s: '%s', %RU64", pProp->mName.c_str(), pProp->mValue.c_str(), pProp->mTimestamp);
1478 if (strlen(szFlags))
1479 pHlp->pfnPrintf(pHlp, " (%s)", szFlags);
1480 pHlp->pfnPrintf(pHlp, "\n");
1481 return 0;
1482}
1483
1484
1485/**
1486 * Handler for debug info.
1487 *
1488 * @param pvUser user pointer.
1489 * @param pHlp The info helper functions.
1490 * @param pszArgs Arguments, ignored.
1491 */
1492DECLCALLBACK(void) Service::dbgInfo(void *pvUser, PCDBGFINFOHLP pHlp, const char *pszArgs)
1493{
1494 RT_NOREF1(pszArgs);
1495 SELF *pSelf = reinterpret_cast<SELF *>(pvUser);
1496
1497 ENUMDBGINFO EnumData = { pHlp };
1498 RTStrSpaceEnumerate(&pSelf->mhProperties, dbgInfoCallback, &EnumData);
1499}
1500
1501
1502/**
1503 * Service call handler for the host.
1504 * @interface_method_impl{VBOXHGCMSVCFNTABLE,pfnHostCall}
1505 * @thread hgcm
1506 */
1507int Service::hostCall (uint32_t eFunction, uint32_t cParms, VBOXHGCMSVCPARM paParms[])
1508{
1509 int rc;
1510 LogFlowFunc(("fn = %d, cParms = %d, pparms = %p\n", eFunction, cParms, paParms));
1511
1512 switch (eFunction)
1513 {
1514 /* The host wishes to set a block of properties */
1515 case GUEST_PROP_FN_HOST_SET_PROPS:
1516 LogFlowFunc(("SET_PROPS_HOST\n"));
1517 rc = setPropertyBlock(cParms, paParms);
1518 break;
1519
1520 /* The host wishes to read a configuration value */
1521 case GUEST_PROP_FN_HOST_GET_PROP:
1522 LogFlowFunc(("GET_PROP_HOST\n"));
1523 rc = getProperty(cParms, paParms);
1524 break;
1525
1526 /* The host wishes to set a configuration value */
1527 case GUEST_PROP_FN_HOST_SET_PROP:
1528 LogFlowFunc(("SET_PROP_HOST\n"));
1529 rc = setProperty(cParms, paParms, false);
1530 break;
1531
1532 /* The host wishes to set a configuration value */
1533 case GUEST_PROP_FN_HOST_SET_PROP_VALUE:
1534 LogFlowFunc(("SET_PROP_VALUE_HOST\n"));
1535 rc = setProperty(cParms, paParms, false);
1536 break;
1537
1538 /* The host wishes to remove a configuration value */
1539 case GUEST_PROP_FN_HOST_DEL_PROP:
1540 LogFlowFunc(("DEL_PROP_HOST\n"));
1541 rc = delProperty(cParms, paParms, false);
1542 break;
1543
1544 /* The host wishes to enumerate all properties */
1545 case GUEST_PROP_FN_HOST_ENUM_PROPS:
1546 LogFlowFunc(("ENUM_PROPS\n"));
1547 rc = enumProps(cParms, paParms);
1548 break;
1549
1550 /* The host wishes to set global flags for the service */
1551 case GUEST_PROP_FN_HOST_SET_GLOBAL_FLAGS:
1552 LogFlowFunc(("SET_GLOBAL_FLAGS_HOST\n"));
1553 if (cParms == 1)
1554 {
1555 uint32_t fFlags;
1556 rc = HGCMSvcGetU32(&paParms[0], &fFlags);
1557 if (RT_SUCCESS(rc))
1558 mfGlobalFlags = fFlags;
1559 }
1560 else
1561 rc = VERR_INVALID_PARAMETER;
1562 break;
1563
1564 default:
1565 rc = VERR_NOT_SUPPORTED;
1566 break;
1567 }
1568
1569 LogFlowFunc(("rc = %Rrc\n", rc));
1570 return rc;
1571}
1572
1573/**
1574 * @interface_method_impl{VBOXHGCMSVCFNTABLE,pfnDisconnect}
1575 */
1576/*static*/ DECLCALLBACK(int) Service::svcDisconnect(void *pvService, uint32_t idClient, void *pvClient)
1577{
1578 RT_NOREF(pvClient);
1579 LogFlowFunc(("idClient=%u\n", idClient));
1580 SELF *pThis = reinterpret_cast<SELF *>(pvService);
1581 AssertLogRelReturn(pThis, VERR_INVALID_POINTER);
1582
1583 /*
1584 * Complete all pending requests for this client.
1585 */
1586 for (CallList::iterator It = pThis->mGuestWaiters.begin(); It != pThis->mGuestWaiters.end();)
1587 {
1588 GuestCall &rCurCall = *It;
1589 if (rCurCall.u32ClientId != idClient)
1590 ++It;
1591 else
1592 {
1593 LogFlowFunc(("Completing call %u (%p)...\n", rCurCall.mFunction, rCurCall.mHandle));
1594 pThis->mpHelpers->pfnCallComplete(rCurCall.mHandle, VERR_INTERRUPTED);
1595 It = pThis->mGuestWaiters.erase(It);
1596 }
1597 }
1598
1599 return VINF_SUCCESS;
1600}
1601
1602/**
1603 * Increments a counter property.
1604 *
1605 * It is assumed that this a transient property that is read-only to the guest.
1606 *
1607 * @param pszName The property name.
1608 * @throws std::bad_alloc if an out of memory condition occurs
1609 */
1610void Service::incrementCounterProp(const char *pszName)
1611{
1612 /* Format the incremented value. */
1613 char szValue[64];
1614 Property *pProp = getPropertyInternal(pszName);
1615 if (pProp)
1616 {
1617 uint64_t uValue = RTStrToUInt64(pProp->mValue.c_str());
1618 RTStrFormatU64(szValue, sizeof(szValue), uValue + 1, 10, 0, 0, 0);
1619 }
1620 else
1621 {
1622 szValue[0] = '1';
1623 szValue[1] = '\0';
1624 }
1625
1626 /* Set it. */
1627 setPropertyInternal(pszName, szValue, GUEST_PROP_F_TRANSIENT | GUEST_PROP_F_RDONLYGUEST, getCurrentTimestamp());
1628}
1629
1630/**
1631 * Sets the VBoxVer, VBoxVerExt and VBoxRev properties.
1632 */
1633int Service::setHostVersionProps()
1634{
1635 uint64_t nsTimestamp = getCurrentTimestamp();
1636
1637 /* Set the raw VBox version string as a guest property. Used for host/guest
1638 * version comparison. */
1639 int rc = setPropertyInternal("/VirtualBox/HostInfo/VBoxVer", VBOX_VERSION_STRING_RAW,
1640 GUEST_PROP_F_TRANSIENT | GUEST_PROP_F_RDONLYGUEST, nsTimestamp);
1641 AssertRCReturn(rc, rc);
1642
1643 /* Set the full VBox version string as a guest property. Can contain vendor-specific
1644 * information/branding and/or pre-release tags. */
1645 rc = setPropertyInternal("/VirtualBox/HostInfo/VBoxVerExt", VBOX_VERSION_STRING,
1646 GUEST_PROP_F_TRANSIENT | GUEST_PROP_F_RDONLYGUEST, nsTimestamp + 1);
1647 AssertRCReturn(rc, rc);
1648
1649 /* Set the VBox SVN revision as a guest property */
1650 rc = setPropertyInternal("/VirtualBox/HostInfo/VBoxRev", RTBldCfgRevisionStr(),
1651 GUEST_PROP_F_TRANSIENT | GUEST_PROP_F_RDONLYGUEST, nsTimestamp + 2);
1652 AssertRCReturn(rc, rc);
1653 return VINF_SUCCESS;
1654}
1655
1656
1657/**
1658 * @interface_method_impl{VBOXHGCMSVCFNTABLE,pfnNotify}
1659 */
1660/*static*/ DECLCALLBACK(void) Service::svcNotify(void *pvService, HGCMNOTIFYEVENT enmEvent)
1661{
1662 SELF *pThis = reinterpret_cast<SELF *>(pvService);
1663 AssertPtrReturnVoid(pThis);
1664
1665 /* Make sure the host version properties have been touched and are
1666 up-to-date after a restore: */
1667 if ( !pThis->m_fSetHostVersionProps
1668 && (enmEvent == HGCMNOTIFYEVENT_RESUME || enmEvent == HGCMNOTIFYEVENT_POWER_ON))
1669 {
1670 pThis->setHostVersionProps();
1671 pThis->m_fSetHostVersionProps = true;
1672 }
1673
1674 if (enmEvent == HGCMNOTIFYEVENT_RESUME)
1675 pThis->incrementCounterProp("/VirtualBox/VMInfo/ResumeCounter");
1676
1677 if (enmEvent == HGCMNOTIFYEVENT_RESET)
1678 pThis->incrementCounterProp("/VirtualBox/VMInfo/ResetCounter");
1679}
1680
1681
1682/* static */
1683DECLCALLBACK(int) Service::threadNotifyHost(RTTHREAD hThreadSelf, void *pvUser)
1684{
1685 RT_NOREF1(hThreadSelf);
1686 Service *pThis = (Service *)pvUser;
1687 int rc = VINF_SUCCESS;
1688
1689 LogFlowFunc(("ENTER: %p\n", pThis));
1690
1691 for (;;)
1692 {
1693 rc = RTReqQueueProcess(pThis->mhReqQNotifyHost, RT_INDEFINITE_WAIT);
1694
1695 AssertMsg(rc == VWRN_STATE_CHANGED,
1696 ("Left RTReqProcess and error code is not VWRN_STATE_CHANGED rc=%Rrc\n",
1697 rc));
1698 if (rc == VWRN_STATE_CHANGED)
1699 {
1700 break;
1701 }
1702 }
1703
1704 LogFlowFunc(("LEAVE: %Rrc\n", rc));
1705 return rc;
1706}
1707
1708static DECLCALLBACK(int) wakeupNotifyHost(void)
1709{
1710 /* Returning a VWRN_* will cause RTReqQueueProcess return. */
1711 return VWRN_STATE_CHANGED;
1712}
1713
1714
1715int Service::initialize()
1716{
1717 /*
1718 * Insert standard host properties.
1719 */
1720 /* The host version will but updated again on power on or resume
1721 (after restore), however we need the properties now for restored
1722 guest notification/wait calls. */
1723 int rc = setHostVersionProps();
1724 AssertRCReturn(rc, rc);
1725
1726 uint64_t nsNow = getCurrentTimestamp(); /* Must increment this for each property to avoid asserting in getOldNotification. */
1727
1728 /* Resume and reset counters. */
1729 rc = setPropertyInternal("/VirtualBox/VMInfo/ResetCounter", "0", GUEST_PROP_F_TRANSIENT | GUEST_PROP_F_RDONLYGUEST, nsNow);
1730 AssertRCReturn(rc, rc);
1731 rc = setPropertyInternal("/VirtualBox/VMInfo/ResumeCounter", "0", GUEST_PROP_F_TRANSIENT | GUEST_PROP_F_RDONLYGUEST, ++nsNow);
1732 AssertRCReturn(rc, rc);
1733
1734 /* Sysprep execution by VBoxService (host is allowed to change these). */
1735 rc = setPropertyInternal("/VirtualBox/HostGuest/SysprepExec", "", GUEST_PROP_F_TRANSIENT | GUEST_PROP_F_RDONLYGUEST, ++nsNow);
1736 AssertRCReturn(rc, rc);
1737 rc = setPropertyInternal("/VirtualBox/HostGuest/SysprepArgs", "", GUEST_PROP_F_TRANSIENT | GUEST_PROP_F_RDONLYGUEST, ++nsNow);
1738 AssertRCReturn(rc, rc);
1739
1740
1741 /* The host notification thread and queue. */
1742 rc = RTReqQueueCreate(&mhReqQNotifyHost);
1743 if (RT_SUCCESS(rc))
1744 {
1745 rc = RTThreadCreate(&mhThreadNotifyHost,
1746 threadNotifyHost,
1747 this,
1748 0 /* default stack size */,
1749 RTTHREADTYPE_DEFAULT,
1750 RTTHREADFLAGS_WAITABLE,
1751 "GstPropNtfy");
1752 if (RT_SUCCESS(rc))
1753 {
1754 /* Finally debug stuff (ignore failures): */
1755 HGCMSvcHlpInfoRegister(mpHelpers, "guestprops", "Display the guest properties", Service::dbgInfo, this);
1756 return rc;
1757 }
1758
1759 RTReqQueueDestroy(mhReqQNotifyHost);
1760 mhReqQNotifyHost = NIL_RTREQQUEUE;
1761 }
1762 return rc;
1763}
1764
1765/**
1766 * @callback_method_impl{FNRTSTRSPACECALLBACK, Destroys Property.}
1767 */
1768static DECLCALLBACK(int) destroyProperty(PRTSTRSPACECORE pStr, void *pvUser)
1769{
1770 RT_NOREF(pvUser);
1771 Property *pProp = RT_FROM_CPP_MEMBER(pStr, struct Property, mStrCore); /* clang objects to offsetof on non-POD.*/
1772 delete pProp;
1773 return 0;
1774}
1775
1776
1777int Service::uninit()
1778{
1779 if (mpHelpers)
1780 HGCMSvcHlpInfoDeregister(mpHelpers, "guestprops");
1781
1782 if (mhReqQNotifyHost != NIL_RTREQQUEUE)
1783 {
1784 /* Stop the thread */
1785 PRTREQ pReq;
1786 int rc = RTReqQueueCall(mhReqQNotifyHost, &pReq, 10000, (PFNRT)wakeupNotifyHost, 0);
1787 if (RT_SUCCESS(rc))
1788 RTReqRelease(pReq);
1789 rc = RTThreadWait(mhThreadNotifyHost, 10000, NULL);
1790 AssertRC(rc);
1791 rc = RTReqQueueDestroy(mhReqQNotifyHost);
1792 AssertRC(rc);
1793 mhReqQNotifyHost = NIL_RTREQQUEUE;
1794 mhThreadNotifyHost = NIL_RTTHREAD;
1795 RTStrSpaceDestroy(&mhProperties, destroyProperty, NULL);
1796 mhProperties = NULL;
1797 }
1798 return VINF_SUCCESS;
1799}
1800
1801} /* namespace guestProp */
1802
1803using guestProp::Service;
1804
1805/**
1806 * @copydoc FNVBOXHGCMSVCLOAD
1807 */
1808extern "C" DECLCALLBACK(DECLEXPORT(int)) VBoxHGCMSvcLoad(VBOXHGCMSVCFNTABLE *ptable)
1809{
1810 int rc = VERR_IPE_UNINITIALIZED_STATUS;
1811
1812 LogFlowFunc(("ptable = %p\n", ptable));
1813
1814 if (!RT_VALID_PTR(ptable))
1815 rc = VERR_INVALID_PARAMETER;
1816 else
1817 {
1818 LogFlowFunc(("ptable->cbSize = %d, ptable->u32Version = 0x%08X\n", ptable->cbSize, ptable->u32Version));
1819
1820 if ( ptable->cbSize != sizeof(VBOXHGCMSVCFNTABLE)
1821 || ptable->u32Version != VBOX_HGCM_SVC_VERSION)
1822 rc = VERR_VERSION_MISMATCH;
1823 else
1824 {
1825 Service *pService = NULL;
1826 /* No exceptions may propagate outside. */
1827 try
1828 {
1829 pService = new Service(ptable->pHelpers);
1830 rc = VINF_SUCCESS;
1831 }
1832 catch (int rcThrown)
1833 {
1834 rc = rcThrown;
1835 }
1836 catch (...)
1837 {
1838 rc = VERR_UNEXPECTED_EXCEPTION;
1839 }
1840
1841 if (RT_SUCCESS(rc))
1842 {
1843 /* We do not maintain connections, so no client data is needed. */
1844 ptable->cbClient = 0;
1845
1846 /* Legacy clients map to the kernel category. */
1847 ptable->idxLegacyClientCategory = HGCM_CLIENT_CATEGORY_KERNEL;
1848
1849 /* Go with default client limits, but we won't ever need more than
1850 16 pending calls per client I would think (1 should be enough). */
1851 for (uintptr_t i = 0; i < RT_ELEMENTS(ptable->acMaxClients); i++)
1852 ptable->acMaxCallsPerClient[i] = 16;
1853
1854 ptable->pfnUnload = Service::svcUnload;
1855 ptable->pfnConnect = Service::svcConnect;
1856 ptable->pfnDisconnect = Service::svcDisconnect;
1857 ptable->pfnCall = Service::svcCall;
1858 ptable->pfnHostCall = Service::svcHostCall;
1859 ptable->pfnSaveState = NULL; /* The service is stateless, so the normal */
1860 ptable->pfnLoadState = NULL; /* construction done before restoring suffices */
1861 ptable->pfnRegisterExtension = Service::svcRegisterExtension;
1862 ptable->pfnNotify = Service::svcNotify;
1863 ptable->pvService = pService;
1864
1865 /* Service specific initialization. */
1866 rc = pService->initialize();
1867 if (RT_FAILURE(rc))
1868 {
1869 delete pService;
1870 pService = NULL;
1871 }
1872 }
1873 else
1874 Assert(!pService);
1875 }
1876 }
1877
1878 LogFlowFunc(("returning %Rrc\n", rc));
1879 return rc;
1880}
1881
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette