VirtualBox

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

Last change on this file since 86603 was 85121, checked in by vboxsync, 4 years ago

iprt/cdefs.h: Refactored the typedef use of DECLCALLBACK as well as DECLCALLBACKMEMBER to wrap the whole expression, similar to the DECLR?CALLBACKMEMBER macros. This allows adding a throw() at the end when compiling with the VC++ compiler to indicate that the callbacks won't throw anything, so we can stop supressing the C5039 warning about passing functions that can potential throw C++ exceptions to extern C code that can't necessarily cope with such (unwind,++). Introduced a few _EX variations that allows specifying different/no calling convention too, as that's handy when dynamically resolving host APIs. Fixed numerous places missing DECLCALLBACK and such. Left two angry @todos regarding use of CreateThread. bugref:9794

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