VirtualBox

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

Last change on this file since 105635 was 105353, checked in by vboxsync, 4 months ago

iprt/req.h,*: Adjustments of the RTReqQueue API to fit darwin/arm64 restrictions. bugref:10725

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 63.4 KB
Line 
1/* $Id: VBoxGuestPropSvc.cpp 105353 2024-07-16 11:47:19Z 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 while (it != mGuestWaiters.end())
1268 {
1269 const char *pszPatterns = NULL;
1270 uint32_t cchPatterns;
1271 int rc2;
1272
1273 rc2 = HGCMSvcGetCStr(&it->mParms[0], &pszPatterns, &cchPatterns);
1274 if (RT_FAILURE(rc2))
1275 {
1276 LogRel(("doNotifications: failed to get match pattern for guest property notification request, rc=%Rrc\n", rc2));
1277 mpHelpers->pfnCallComplete(it->mHandle, VERR_INVALID_PARAMETER);
1278 it = mGuestWaiters.erase(it);
1279 }
1280 else if (prop.Matches(pszPatterns))
1281 {
1282 rc2 = getNotificationWriteOut(it->mParmsCnt, it->mParms, prop, !pProp);
1283 if (RT_SUCCESS(rc2))
1284 rc2 = it->mRc;
1285 mpHelpers->pfnCallComplete(it->mHandle, rc2);
1286 it = mGuestWaiters.erase(it);
1287 }
1288 else
1289 ++it;
1290 }
1291
1292 try
1293 {
1294 mGuestNotifications.push_back(prop);
1295 }
1296 catch (std::bad_alloc &)
1297 {
1298 rc = VERR_NO_MEMORY;
1299 }
1300
1301 if ( RT_SUCCESS(rc)
1302 && mpfnHostCallback)
1303 {
1304 /*
1305 * Host notifications - first case: if the property exists then send its
1306 * current value
1307 */
1308 if (pProp)
1309 {
1310 char szFlags[GUEST_PROP_MAX_FLAGS_LEN];
1311 /* Send out a host notification */
1312 const char *pszValue = prop.mValue.c_str();
1313 rc = GuestPropWriteFlags(prop.mFlags, szFlags);
1314 if (RT_SUCCESS(rc))
1315 rc = notifyHost(pszProperty, pszValue, nsTimestamp, szFlags);
1316 }
1317 /*
1318 * Host notifications - second case: if the property does not exist then
1319 * send the host an empty value
1320 */
1321 else
1322 {
1323 /* Send out a host notification */
1324 rc = notifyHost(pszProperty, NULL, nsTimestamp, "");
1325 }
1326 }
1327
1328 LogFlowThisFunc(("returning rc=%Rrc\n", rc));
1329 return rc;
1330}
1331
1332static DECLCALLBACK(void)
1333notifyHostAsyncWorker(PFNHGCMSVCEXT pfnHostCallback, void *pvHostData, PGUESTPROPHOSTCALLBACKDATA pHostCallbackData)
1334{
1335 pfnHostCallback(pvHostData, 0 /*u32Function*/, (void *)pHostCallbackData, sizeof(GUESTPROPHOSTCALLBACKDATA));
1336 RTMemFree(pHostCallbackData);
1337}
1338
1339/**
1340 * Notify the service owner that a property has been added/deleted/changed.
1341 * @returns IPRT status value
1342 * @param pszName the property name
1343 * @param pszValue the new value, or NULL if the property was deleted
1344 * @param nsTimestamp the time of the change
1345 * @param pszFlags the new flags string
1346 */
1347int Service::notifyHost(const char *pszName, const char *pszValue, uint64_t nsTimestamp, const char *pszFlags)
1348{
1349 LogFlowFunc(("pszName=%s, pszValue=%s, nsTimestamp=%llu, pszFlags=%s\n", pszName, pszValue, nsTimestamp, pszFlags));
1350 int rc;
1351
1352 /* Allocate buffer for the callback data and strings. */
1353 size_t cbName = pszName? strlen(pszName): 0;
1354 size_t cbValue = pszValue? strlen(pszValue): 0;
1355 size_t cbFlags = pszFlags? strlen(pszFlags): 0;
1356 size_t cbAlloc = sizeof(GUESTPROPHOSTCALLBACKDATA) + cbName + cbValue + cbFlags + 3;
1357 PGUESTPROPHOSTCALLBACKDATA pHostCallbackData = (PGUESTPROPHOSTCALLBACKDATA)RTMemAlloc(cbAlloc);
1358 if (pHostCallbackData)
1359 {
1360 uint8_t *pu8 = (uint8_t *)pHostCallbackData;
1361 pu8 += sizeof(GUESTPROPHOSTCALLBACKDATA);
1362
1363 pHostCallbackData->u32Magic = GUESTPROPHOSTCALLBACKDATA_MAGIC;
1364
1365 pHostCallbackData->pcszName = (const char *)pu8;
1366 memcpy(pu8, pszName, cbName);
1367 pu8 += cbName;
1368 *pu8++ = 0;
1369
1370 /* NULL value means property was deleted. */
1371 pHostCallbackData->pcszValue = pszValue ? (const char *)pu8 : NULL;
1372 memcpy(pu8, pszValue, cbValue);
1373 pu8 += cbValue;
1374 *pu8++ = 0;
1375
1376 pHostCallbackData->u64Timestamp = nsTimestamp;
1377
1378 pHostCallbackData->pcszFlags = (const char *)pu8;
1379 memcpy(pu8, pszFlags, cbFlags);
1380 pu8 += cbFlags;
1381 *pu8++ = 0;
1382
1383 rc = RTReqQueueCallEx(mhReqQNotifyHost, NULL, 0, RTREQFLAGS_VOID | RTREQFLAGS_NO_WAIT,
1384 (PFNRT)notifyHostAsyncWorker, 3, mpfnHostCallback, mpvHostData, pHostCallbackData);
1385 if (RT_FAILURE(rc))
1386 {
1387 RTMemFree(pHostCallbackData);
1388 }
1389 }
1390 else
1391 {
1392 rc = VERR_NO_MEMORY;
1393 }
1394 LogFlowFunc(("returning rc=%Rrc\n", rc));
1395 return rc;
1396}
1397
1398
1399/**
1400 * Handle an HGCM service call.
1401 * @interface_method_impl{VBOXHGCMSVCFNTABLE,pfnCall}
1402 * @note All functions which do not involve an unreasonable delay will be
1403 * handled synchronously. If needed, we will add a request handler
1404 * thread in future for those which do.
1405 *
1406 * @thread HGCM
1407 */
1408void Service::call (VBOXHGCMCALLHANDLE callHandle, uint32_t u32ClientID,
1409 void * /* pvClient */, uint32_t eFunction, uint32_t cParms,
1410 VBOXHGCMSVCPARM paParms[])
1411{
1412 int rc;
1413 LogFlowFunc(("u32ClientID = %d, fn = %d, cParms = %d, pparms = %p\n",
1414 u32ClientID, eFunction, cParms, paParms));
1415
1416 switch (eFunction)
1417 {
1418 /* The guest wishes to read a property */
1419 case GUEST_PROP_FN_GET_PROP:
1420 LogFlowFunc(("GET_PROP\n"));
1421 rc = getProperty(cParms, paParms);
1422 break;
1423
1424 /* The guest wishes to set a property */
1425 case GUEST_PROP_FN_SET_PROP:
1426 LogFlowFunc(("SET_PROP\n"));
1427 rc = setProperty(cParms, paParms, true);
1428 break;
1429
1430 /* The guest wishes to set a property value */
1431 case GUEST_PROP_FN_SET_PROP_VALUE:
1432 LogFlowFunc(("SET_PROP_VALUE\n"));
1433 rc = setProperty(cParms, paParms, true);
1434 break;
1435
1436 /* The guest wishes to remove a configuration value */
1437 case GUEST_PROP_FN_DEL_PROP:
1438 LogFlowFunc(("DEL_PROP\n"));
1439 rc = delProperty(cParms, paParms, true);
1440 break;
1441
1442 /* The guest wishes to enumerate all properties */
1443 case GUEST_PROP_FN_ENUM_PROPS:
1444 LogFlowFunc(("ENUM_PROPS\n"));
1445 rc = enumProps(cParms, paParms);
1446 break;
1447
1448 /* The guest wishes to get the next property notification */
1449 case GUEST_PROP_FN_GET_NOTIFICATION:
1450 LogFlowFunc(("GET_NOTIFICATION\n"));
1451 rc = getNotification(u32ClientID, callHandle, cParms, paParms);
1452 break;
1453
1454 default:
1455 rc = VERR_NOT_IMPLEMENTED;
1456 }
1457 LogFlowFunc(("rc = %Rrc\n", rc));
1458 if (rc != VINF_HGCM_ASYNC_EXECUTE)
1459 mpHelpers->pfnCallComplete(callHandle, rc);
1460}
1461
1462/**
1463 * Enumeration data shared between dbgInfoCallback and Service::dbgInfoShow.
1464 */
1465typedef struct ENUMDBGINFO
1466{
1467 PCDBGFINFOHLP pHlp;
1468} ENUMDBGINFO;
1469
1470static DECLCALLBACK(int) dbgInfoCallback(PRTSTRSPACECORE pStr, void *pvUser)
1471{
1472 Property *pProp = (Property *)pStr;
1473 PCDBGFINFOHLP pHlp = ((ENUMDBGINFO *)pvUser)->pHlp;
1474
1475 char szFlags[GUEST_PROP_MAX_FLAGS_LEN];
1476 int rc = GuestPropWriteFlags(pProp->mFlags, szFlags);
1477 if (RT_FAILURE(rc))
1478 RTStrPrintf(szFlags, sizeof(szFlags), "???");
1479
1480 pHlp->pfnPrintf(pHlp, "%s: '%s', %RU64", pProp->mName.c_str(), pProp->mValue.c_str(), pProp->mTimestamp);
1481 if (strlen(szFlags))
1482 pHlp->pfnPrintf(pHlp, " (%s)", szFlags);
1483 pHlp->pfnPrintf(pHlp, "\n");
1484 return 0;
1485}
1486
1487
1488/**
1489 * Handler for debug info.
1490 *
1491 * @param pvUser user pointer.
1492 * @param pHlp The info helper functions.
1493 * @param pszArgs Arguments, ignored.
1494 */
1495DECLCALLBACK(void) Service::dbgInfo(void *pvUser, PCDBGFINFOHLP pHlp, const char *pszArgs)
1496{
1497 RT_NOREF1(pszArgs);
1498 SELF *pSelf = reinterpret_cast<SELF *>(pvUser);
1499
1500 ENUMDBGINFO EnumData = { pHlp };
1501 RTStrSpaceEnumerate(&pSelf->mhProperties, dbgInfoCallback, &EnumData);
1502}
1503
1504
1505/**
1506 * Service call handler for the host.
1507 * @interface_method_impl{VBOXHGCMSVCFNTABLE,pfnHostCall}
1508 * @thread hgcm
1509 */
1510int Service::hostCall (uint32_t eFunction, uint32_t cParms, VBOXHGCMSVCPARM paParms[])
1511{
1512 int rc;
1513 LogFlowFunc(("fn = %d, cParms = %d, pparms = %p\n", eFunction, cParms, paParms));
1514
1515 switch (eFunction)
1516 {
1517 /* The host wishes to set a block of properties */
1518 case GUEST_PROP_FN_HOST_SET_PROPS:
1519 LogFlowFunc(("SET_PROPS_HOST\n"));
1520 rc = setPropertyBlock(cParms, paParms);
1521 break;
1522
1523 /* The host wishes to read a configuration value */
1524 case GUEST_PROP_FN_HOST_GET_PROP:
1525 LogFlowFunc(("GET_PROP_HOST\n"));
1526 rc = getProperty(cParms, paParms);
1527 break;
1528
1529 /* The host wishes to set a configuration value */
1530 case GUEST_PROP_FN_HOST_SET_PROP:
1531 LogFlowFunc(("SET_PROP_HOST\n"));
1532 rc = setProperty(cParms, paParms, false);
1533 break;
1534
1535 /* The host wishes to set a configuration value */
1536 case GUEST_PROP_FN_HOST_SET_PROP_VALUE:
1537 LogFlowFunc(("SET_PROP_VALUE_HOST\n"));
1538 rc = setProperty(cParms, paParms, false);
1539 break;
1540
1541 /* The host wishes to remove a configuration value */
1542 case GUEST_PROP_FN_HOST_DEL_PROP:
1543 LogFlowFunc(("DEL_PROP_HOST\n"));
1544 rc = delProperty(cParms, paParms, false);
1545 break;
1546
1547 /* The host wishes to enumerate all properties */
1548 case GUEST_PROP_FN_HOST_ENUM_PROPS:
1549 LogFlowFunc(("ENUM_PROPS\n"));
1550 rc = enumProps(cParms, paParms);
1551 break;
1552
1553 /* The host wishes to set global flags for the service */
1554 case GUEST_PROP_FN_HOST_SET_GLOBAL_FLAGS:
1555 LogFlowFunc(("SET_GLOBAL_FLAGS_HOST\n"));
1556 if (cParms == 1)
1557 {
1558 uint32_t fFlags;
1559 rc = HGCMSvcGetU32(&paParms[0], &fFlags);
1560 if (RT_SUCCESS(rc))
1561 mfGlobalFlags = fFlags;
1562 }
1563 else
1564 rc = VERR_INVALID_PARAMETER;
1565 break;
1566
1567 default:
1568 rc = VERR_NOT_SUPPORTED;
1569 break;
1570 }
1571
1572 LogFlowFunc(("rc = %Rrc\n", rc));
1573 return rc;
1574}
1575
1576/**
1577 * @interface_method_impl{VBOXHGCMSVCFNTABLE,pfnDisconnect}
1578 */
1579/*static*/ DECLCALLBACK(int) Service::svcDisconnect(void *pvService, uint32_t idClient, void *pvClient)
1580{
1581 RT_NOREF(pvClient);
1582 LogFlowFunc(("idClient=%u\n", idClient));
1583 SELF *pThis = reinterpret_cast<SELF *>(pvService);
1584 AssertLogRelReturn(pThis, VERR_INVALID_POINTER);
1585
1586 /*
1587 * Complete all pending requests for this client.
1588 */
1589 for (CallList::iterator It = pThis->mGuestWaiters.begin(); It != pThis->mGuestWaiters.end();)
1590 {
1591 GuestCall &rCurCall = *It;
1592 if (rCurCall.u32ClientId != idClient)
1593 ++It;
1594 else
1595 {
1596 LogFlowFunc(("Completing call %u (%p)...\n", rCurCall.mFunction, rCurCall.mHandle));
1597 pThis->mpHelpers->pfnCallComplete(rCurCall.mHandle, VERR_INTERRUPTED);
1598 It = pThis->mGuestWaiters.erase(It);
1599 }
1600 }
1601
1602 return VINF_SUCCESS;
1603}
1604
1605/**
1606 * Increments a counter property.
1607 *
1608 * It is assumed that this a transient property that is read-only to the guest.
1609 *
1610 * @param pszName The property name.
1611 * @throws std::bad_alloc if an out of memory condition occurs
1612 */
1613void Service::incrementCounterProp(const char *pszName)
1614{
1615 /* Format the incremented value. */
1616 char szValue[64];
1617 Property *pProp = getPropertyInternal(pszName);
1618 if (pProp)
1619 {
1620 uint64_t uValue = RTStrToUInt64(pProp->mValue.c_str());
1621 RTStrFormatU64(szValue, sizeof(szValue), uValue + 1, 10, 0, 0, 0);
1622 }
1623 else
1624 {
1625 szValue[0] = '1';
1626 szValue[1] = '\0';
1627 }
1628
1629 /* Set it. */
1630 setPropertyInternal(pszName, szValue, GUEST_PROP_F_TRANSIENT | GUEST_PROP_F_RDONLYGUEST, getCurrentTimestamp());
1631}
1632
1633/**
1634 * Sets the VBoxVer, VBoxVerExt and VBoxRev properties.
1635 */
1636int Service::setHostVersionProps()
1637{
1638 uint64_t nsTimestamp = getCurrentTimestamp();
1639
1640 /* Set the raw VBox version string as a guest property. Used for host/guest
1641 * version comparison. */
1642 int rc = setPropertyInternal("/VirtualBox/HostInfo/VBoxVer", VBOX_VERSION_STRING_RAW,
1643 GUEST_PROP_F_TRANSIENT | GUEST_PROP_F_RDONLYGUEST, nsTimestamp);
1644 AssertRCReturn(rc, rc);
1645
1646 /* Set the full VBox version string as a guest property. Can contain vendor-specific
1647 * information/branding and/or pre-release tags. */
1648 rc = setPropertyInternal("/VirtualBox/HostInfo/VBoxVerExt", VBOX_VERSION_STRING,
1649 GUEST_PROP_F_TRANSIENT | GUEST_PROP_F_RDONLYGUEST, nsTimestamp + 1);
1650 AssertRCReturn(rc, rc);
1651
1652 /* Set the VBox SVN revision as a guest property */
1653 rc = setPropertyInternal("/VirtualBox/HostInfo/VBoxRev", RTBldCfgRevisionStr(),
1654 GUEST_PROP_F_TRANSIENT | GUEST_PROP_F_RDONLYGUEST, nsTimestamp + 2);
1655 AssertRCReturn(rc, rc);
1656 return VINF_SUCCESS;
1657}
1658
1659
1660/**
1661 * @interface_method_impl{VBOXHGCMSVCFNTABLE,pfnNotify}
1662 */
1663/*static*/ DECLCALLBACK(void) Service::svcNotify(void *pvService, HGCMNOTIFYEVENT enmEvent)
1664{
1665 SELF *pThis = reinterpret_cast<SELF *>(pvService);
1666 AssertPtrReturnVoid(pThis);
1667
1668 /* Make sure the host version properties have been touched and are
1669 up-to-date after a restore: */
1670 if ( !pThis->m_fSetHostVersionProps
1671 && (enmEvent == HGCMNOTIFYEVENT_RESUME || enmEvent == HGCMNOTIFYEVENT_POWER_ON))
1672 {
1673 pThis->setHostVersionProps();
1674 pThis->m_fSetHostVersionProps = true;
1675 }
1676
1677 if (enmEvent == HGCMNOTIFYEVENT_RESUME)
1678 pThis->incrementCounterProp("/VirtualBox/VMInfo/ResumeCounter");
1679
1680 if (enmEvent == HGCMNOTIFYEVENT_RESET)
1681 pThis->incrementCounterProp("/VirtualBox/VMInfo/ResetCounter");
1682}
1683
1684
1685/* static */
1686DECLCALLBACK(int) Service::threadNotifyHost(RTTHREAD hThreadSelf, void *pvUser)
1687{
1688 RT_NOREF1(hThreadSelf);
1689 Service *pThis = (Service *)pvUser;
1690 int rc = VINF_SUCCESS;
1691
1692 LogFlowFunc(("ENTER: %p\n", pThis));
1693
1694 for (;;)
1695 {
1696 rc = RTReqQueueProcess(pThis->mhReqQNotifyHost, RT_INDEFINITE_WAIT);
1697
1698 AssertMsg(rc == VWRN_STATE_CHANGED,
1699 ("Left RTReqProcess and error code is not VWRN_STATE_CHANGED rc=%Rrc\n",
1700 rc));
1701 if (rc == VWRN_STATE_CHANGED)
1702 {
1703 break;
1704 }
1705 }
1706
1707 LogFlowFunc(("LEAVE: %Rrc\n", rc));
1708 return rc;
1709}
1710
1711static DECLCALLBACK(int) wakeupNotifyHost(void)
1712{
1713 /* Returning a VWRN_* will cause RTReqQueueProcess return. */
1714 return VWRN_STATE_CHANGED;
1715}
1716
1717
1718int Service::initialize()
1719{
1720 /*
1721 * Insert standard host properties.
1722 */
1723 /* The host version will but updated again on power on or resume
1724 (after restore), however we need the properties now for restored
1725 guest notification/wait calls. */
1726 int rc = setHostVersionProps();
1727 AssertRCReturn(rc, rc);
1728
1729 uint64_t nsNow = getCurrentTimestamp(); /* Must increment this for each property to avoid asserting in getOldNotification. */
1730
1731 /* Resume and reset counters. */
1732 rc = setPropertyInternal("/VirtualBox/VMInfo/ResetCounter", "0", GUEST_PROP_F_TRANSIENT | GUEST_PROP_F_RDONLYGUEST, nsNow);
1733 AssertRCReturn(rc, rc);
1734 rc = setPropertyInternal("/VirtualBox/VMInfo/ResumeCounter", "0", GUEST_PROP_F_TRANSIENT | GUEST_PROP_F_RDONLYGUEST, ++nsNow);
1735 AssertRCReturn(rc, rc);
1736
1737 /* Sysprep execution by VBoxService (host is allowed to change these). */
1738 rc = setPropertyInternal("/VirtualBox/HostGuest/SysprepExec", "", GUEST_PROP_F_TRANSIENT | GUEST_PROP_F_RDONLYGUEST, ++nsNow);
1739 AssertRCReturn(rc, rc);
1740 rc = setPropertyInternal("/VirtualBox/HostGuest/SysprepArgs", "", GUEST_PROP_F_TRANSIENT | GUEST_PROP_F_RDONLYGUEST, ++nsNow);
1741 AssertRCReturn(rc, rc);
1742
1743
1744 /* The host notification thread and queue. */
1745 rc = RTReqQueueCreate(&mhReqQNotifyHost);
1746 if (RT_SUCCESS(rc))
1747 {
1748 rc = RTThreadCreate(&mhThreadNotifyHost,
1749 threadNotifyHost,
1750 this,
1751 0 /* default stack size */,
1752 RTTHREADTYPE_DEFAULT,
1753 RTTHREADFLAGS_WAITABLE,
1754 "GstPropNtfy");
1755 if (RT_SUCCESS(rc))
1756 {
1757 /* Finally debug stuff (ignore failures): */
1758 HGCMSvcHlpInfoRegister(mpHelpers, "guestprops", "Display the guest properties", Service::dbgInfo, this);
1759 return rc;
1760 }
1761
1762 RTReqQueueDestroy(mhReqQNotifyHost);
1763 mhReqQNotifyHost = NIL_RTREQQUEUE;
1764 }
1765 return rc;
1766}
1767
1768/**
1769 * @callback_method_impl{FNRTSTRSPACECALLBACK, Destroys Property.}
1770 */
1771static DECLCALLBACK(int) destroyProperty(PRTSTRSPACECORE pStr, void *pvUser)
1772{
1773 RT_NOREF(pvUser);
1774 Property *pProp = RT_FROM_CPP_MEMBER(pStr, struct Property, mStrCore); /* clang objects to offsetof on non-POD.*/
1775 delete pProp;
1776 return 0;
1777}
1778
1779
1780int Service::uninit()
1781{
1782 if (mpHelpers)
1783 HGCMSvcHlpInfoDeregister(mpHelpers, "guestprops");
1784
1785 if (mhReqQNotifyHost != NIL_RTREQQUEUE)
1786 {
1787 /* Stop the thread */
1788 PRTREQ pReq;
1789 int rc = RTReqQueueCall(mhReqQNotifyHost, &pReq, 10000, (PFNRT)wakeupNotifyHost, 0);
1790 if (RT_SUCCESS(rc))
1791 RTReqRelease(pReq);
1792 rc = RTThreadWait(mhThreadNotifyHost, 10000, NULL);
1793 AssertRC(rc);
1794 rc = RTReqQueueDestroy(mhReqQNotifyHost);
1795 AssertRC(rc);
1796 mhReqQNotifyHost = NIL_RTREQQUEUE;
1797 mhThreadNotifyHost = NIL_RTTHREAD;
1798 RTStrSpaceDestroy(&mhProperties, destroyProperty, NULL);
1799 mhProperties = NULL;
1800 }
1801 return VINF_SUCCESS;
1802}
1803
1804} /* namespace guestProp */
1805
1806using guestProp::Service;
1807
1808/**
1809 * @copydoc FNVBOXHGCMSVCLOAD
1810 */
1811extern "C" DECLCALLBACK(DECLEXPORT(int)) VBoxHGCMSvcLoad(VBOXHGCMSVCFNTABLE *ptable)
1812{
1813 int rc = VERR_IPE_UNINITIALIZED_STATUS;
1814
1815 LogFlowFunc(("ptable = %p\n", ptable));
1816
1817 if (!RT_VALID_PTR(ptable))
1818 rc = VERR_INVALID_PARAMETER;
1819 else
1820 {
1821 LogFlowFunc(("ptable->cbSize = %d, ptable->u32Version = 0x%08X\n", ptable->cbSize, ptable->u32Version));
1822
1823 if ( ptable->cbSize != sizeof(VBOXHGCMSVCFNTABLE)
1824 || ptable->u32Version != VBOX_HGCM_SVC_VERSION)
1825 rc = VERR_VERSION_MISMATCH;
1826 else
1827 {
1828 Service *pService = NULL;
1829 /* No exceptions may propagate outside. */
1830 try
1831 {
1832 pService = new Service(ptable->pHelpers);
1833 rc = VINF_SUCCESS;
1834 }
1835 catch (int rcThrown)
1836 {
1837 rc = rcThrown;
1838 }
1839 catch (...)
1840 {
1841 rc = VERR_UNEXPECTED_EXCEPTION;
1842 }
1843
1844 if (RT_SUCCESS(rc))
1845 {
1846 /* We do not maintain connections, so no client data is needed. */
1847 ptable->cbClient = 0;
1848
1849 /* Legacy clients map to the kernel category. */
1850 ptable->idxLegacyClientCategory = HGCM_CLIENT_CATEGORY_KERNEL;
1851
1852 /* Go with default client limits, but we won't ever need more than
1853 16 pending calls per client I would think (1 should be enough). */
1854 for (uintptr_t i = 0; i < RT_ELEMENTS(ptable->acMaxClients); i++)
1855 ptable->acMaxCallsPerClient[i] = 16;
1856
1857 ptable->pfnUnload = Service::svcUnload;
1858 ptable->pfnConnect = Service::svcConnect;
1859 ptable->pfnDisconnect = Service::svcDisconnect;
1860 ptable->pfnCall = Service::svcCall;
1861 ptable->pfnHostCall = Service::svcHostCall;
1862 ptable->pfnSaveState = NULL; /* The service is stateless, so the normal */
1863 ptable->pfnLoadState = NULL; /* construction done before restoring suffices */
1864 ptable->pfnRegisterExtension = Service::svcRegisterExtension;
1865 ptable->pfnNotify = Service::svcNotify;
1866 ptable->pvService = pService;
1867
1868 /* Service specific initialization. */
1869 rc = pService->initialize();
1870 if (RT_FAILURE(rc))
1871 {
1872 delete pService;
1873 pService = NULL;
1874 }
1875 }
1876 else
1877 Assert(!pService);
1878 }
1879 }
1880
1881 LogFlowFunc(("returning %Rrc\n", rc));
1882 return rc;
1883}
1884
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