VirtualBox

source: vbox/trunk/src/VBox/HostServices/GuestProperties/service.cpp@ 75989

Last change on this file since 75989 was 75989, checked in by vboxsync, 6 years ago

GuestProperty: Handle std::bad_alloc as close as possible to the source.

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