VirtualBox

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

Last change on this file since 24665 was 24665, checked in by vboxsync, 15 years ago

GuestProperties/service.cpp: comment

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 45.4 KB
Line 
1/** @file
2 *
3 * Guest Property Service:
4 * Host service entry points.
5 */
6
7/*
8 * Copyright (C) 2008 Sun Microsystems, Inc.
9 *
10 * This file is part of VirtualBox Open Source Edition (OSE), as
11 * available from http://www.virtualbox.org. This file is free software;
12 * you can redistribute it and/or modify it under the terms of the GNU
13 * General Public License (GPL) as published by the Free Software
14 * Foundation, in version 2 as it comes in the "COPYING" file of the
15 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
16 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
17 *
18 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
19 * Clara, CA 95054 USA or visit http://www.sun.com if you need
20 * additional information or have any questions.
21 */
22
23/**
24 * This HGCM service allows the guest to set and query values in a property
25 * store on the host. The service proxies the guest requests to the service
26 * owner on the host using a request callback provided by the owner, and is
27 * notified of changes to properties made by the host. It forwards these
28 * notifications to clients in the guest which have expressed interest and
29 * are waiting for notification.
30 *
31 * The service currently consists of two threads. One of these is the main
32 * HGCM service thread which deals with requests from the guest and from the
33 * host. The second thread sends the host asynchronous notifications of
34 * changes made by the guest and deals with notification timeouts.
35 *
36 * Guest requests to wait for notification are added to a list of open
37 * notification requests and completed when a corresponding guest property
38 * is changed or when the request times out.
39 */
40
41#define LOG_GROUP LOG_GROUP_HGCM
42
43/*******************************************************************************
44* Header Files *
45*******************************************************************************/
46#include <VBox/HostServices/GuestPropertySvc.h>
47
48#include <VBox/log.h>
49#include <iprt/err.h>
50#include <iprt/assert.h>
51#include <iprt/string.h>
52#include <iprt/mem.h>
53#include <iprt/autores.h>
54#include <iprt/time.h>
55#include <iprt/cpputils.h>
56#include <iprt/req.h>
57#include <iprt/thread.h>
58
59#include <memory> /* for auto_ptr */
60#include <string>
61#include <list>
62
63namespace guestProp {
64
65/**
66 * Structure for holding a property
67 */
68struct Property
69{
70 /** The name of the property */
71 std::string mName;
72 /** The property value */
73 std::string mValue;
74 /** The timestamp of the property */
75 uint64_t mTimestamp;
76 /** The property flags */
77 uint32_t mFlags;
78
79 /** Default constructor */
80 Property() : mTimestamp(0), mFlags(NILFLAG) {}
81 /** Constructor with const char * */
82 Property(const char *pcszName, const char *pcszValue,
83 uint64_t u64Timestamp, uint32_t u32Flags)
84 : mName(pcszName), mValue(pcszValue), mTimestamp(u64Timestamp),
85 mFlags(u32Flags) {}
86 /** Constructor with std::string */
87 Property(std::string name, std::string value, uint64_t u64Timestamp,
88 uint32_t u32Flags)
89 : mName(name), mValue(value), mTimestamp(u64Timestamp),
90 mFlags(u32Flags) {}
91
92 /** Does the property name match one of a set of patterns? */
93 bool Matches(const char *pszPatterns) const
94 {
95 return ( pszPatterns[0] == '\0' /* match all */
96 || RTStrSimplePatternMultiMatch(pszPatterns, RTSTR_MAX,
97 mName.c_str(), RTSTR_MAX,
98 NULL)
99 );
100 }
101
102 /** Are two properties equal? */
103 bool operator== (const Property &prop)
104 {
105 return ( mName == prop.mName
106 && mValue == prop.mValue
107 && mTimestamp == prop.mTimestamp
108 && mFlags == prop.mFlags
109 );
110 }
111
112 /* Is the property nil? */
113 bool isNull()
114 {
115 return mName.empty();
116 }
117};
118/** The properties list type */
119typedef std::list <Property> PropertyList;
120
121/**
122 * Structure for holding an uncompleted guest call
123 */
124struct GuestCall
125{
126 /** The call handle */
127 VBOXHGCMCALLHANDLE mHandle;
128 /** The function that was requested */
129 uint32_t mFunction;
130 /** The call parameters */
131 VBOXHGCMSVCPARM *mParms;
132 /** The default return value, used for passing warnings */
133 int mRc;
134
135 /** The standard constructor */
136 GuestCall() : mFunction(0) {}
137 /** The normal contructor */
138 GuestCall(VBOXHGCMCALLHANDLE aHandle, uint32_t aFunction,
139 VBOXHGCMSVCPARM aParms[], int aRc)
140 : mHandle(aHandle), mFunction(aFunction), mParms(aParms),
141 mRc(aRc) {}
142};
143/** The guest call list type */
144typedef std::list <GuestCall> CallList;
145
146/**
147 * Class containing the shared information service functionality.
148 */
149class Service : public stdx::non_copyable
150{
151private:
152 /** Type definition for use in callback functions */
153 typedef Service SELF;
154 /** HGCM helper functions. */
155 PVBOXHGCMSVCHELPERS mpHelpers;
156 /** The property list */
157 PropertyList mProperties;
158 /** The list of property changes for guest notifications */
159 PropertyList mGuestNotifications;
160 /** The list of outstanding guest notification calls */
161 CallList mGuestWaiters;
162 /** @todo we should have classes for thread and request handler thread */
163 /** Queue of outstanding property change notifications */
164 RTREQQUEUE *mReqQueue;
165 /** Thread for processing the request queue */
166 RTTHREAD mReqThread;
167 /** Tell the thread that it should exit */
168 bool mfExitThread;
169 /** Callback function supplied by the host for notification of updates
170 * to properties */
171 PFNHGCMSVCEXT mpfnHostCallback;
172 /** User data pointer to be supplied to the host callback function */
173 void *mpvHostData;
174
175 /**
176 * Get the next property change notification from the queue of saved
177 * notification based on the timestamp of the last notification seen.
178 * Notifications will only be reported if the property name matches the
179 * pattern given.
180 *
181 * @returns iprt status value
182 * @returns VWRN_NOT_FOUND if the last notification was not found in the queue
183 * @param pszPatterns the patterns to match the property name against
184 * @param u64Timestamp the timestamp of the last notification
185 * @param pProp where to return the property found. If none is
186 * found this will be set to nil.
187 * @thread HGCM
188 */
189 int getOldNotification(const char *pszPatterns, uint64_t u64Timestamp,
190 Property *pProp)
191 {
192 AssertPtrReturn(pszPatterns, VERR_INVALID_POINTER);
193 /* Zero means wait for a new notification. */
194 AssertReturn(u64Timestamp != 0, VERR_INVALID_PARAMETER);
195 AssertPtrReturn(pProp, VERR_INVALID_POINTER);
196 int rc = getOldNotificationInternal(pszPatterns, u64Timestamp, pProp);
197#ifdef VBOX_STRICT
198 /*
199 * ENSURE that pProp is the first event in the notification queue that:
200 * - Appears later than u64Timestamp
201 * - Matches the pszPatterns
202 */
203 PropertyList::const_iterator it = mGuestNotifications.begin();
204 for (; it != mGuestNotifications.end()
205 && it->mTimestamp != u64Timestamp; ++it) {}
206 if (it == mGuestNotifications.end()) /* Not found */
207 it = mGuestNotifications.begin();
208 else
209 ++it; /* Next event */
210 for (; it != mGuestNotifications.end()
211 && it->mTimestamp != pProp->mTimestamp; ++it)
212 Assert(!it->Matches(pszPatterns));
213 if (pProp->mTimestamp != 0)
214 {
215 Assert(*pProp == *it);
216 Assert(pProp->Matches(pszPatterns));
217 }
218#endif /* VBOX_STRICT */
219 return rc;
220 }
221
222public:
223 explicit Service(PVBOXHGCMSVCHELPERS pHelpers)
224 : mpHelpers(pHelpers), mfExitThread(false), mpfnHostCallback(NULL),
225 mpvHostData(NULL)
226 {
227 int rc = RTReqCreateQueue(&mReqQueue);
228#ifndef VBOX_GUEST_PROP_TEST_NOTHREAD
229 if (RT_SUCCESS(rc))
230 rc = RTThreadCreate(&mReqThread, reqThreadFn, this, 0,
231 RTTHREADTYPE_MSG_PUMP, RTTHREADFLAGS_WAITABLE,
232 "GuestPropReq");
233#endif
234 if (RT_FAILURE(rc))
235 throw rc;
236 }
237
238 /**
239 * @copydoc VBOXHGCMSVCHELPERS::pfnUnload
240 * Simply deletes the service object
241 */
242 static DECLCALLBACK(int) svcUnload (void *pvService)
243 {
244 AssertLogRelReturn(VALID_PTR(pvService), VERR_INVALID_PARAMETER);
245 SELF *pSelf = reinterpret_cast<SELF *>(pvService);
246 int rc = pSelf->uninit();
247 AssertRC(rc);
248 if (RT_SUCCESS(rc))
249 delete pSelf;
250 return rc;
251 }
252
253 /**
254 * @copydoc VBOXHGCMSVCHELPERS::pfnConnect
255 * Stub implementation of pfnConnect and pfnDisconnect.
256 */
257 static DECLCALLBACK(int) svcConnectDisconnect (void * /* pvService */,
258 uint32_t /* u32ClientID */,
259 void * /* pvClient */)
260 {
261 return VINF_SUCCESS;
262 }
263
264 /**
265 * @copydoc VBOXHGCMSVCHELPERS::pfnCall
266 * Wraps to the call member function
267 */
268 static DECLCALLBACK(void) svcCall (void * pvService,
269 VBOXHGCMCALLHANDLE callHandle,
270 uint32_t u32ClientID,
271 void *pvClient,
272 uint32_t u32Function,
273 uint32_t cParms,
274 VBOXHGCMSVCPARM paParms[])
275 {
276 AssertLogRelReturnVoid(VALID_PTR(pvService));
277 LogFlowFunc (("pvService=%p, callHandle=%p, u32ClientID=%u, pvClient=%p, u32Function=%u, cParms=%u, paParms=%p\n", pvService, callHandle, u32ClientID, pvClient, u32Function, cParms, paParms));
278 SELF *pSelf = reinterpret_cast<SELF *>(pvService);
279 pSelf->call(callHandle, u32ClientID, pvClient, u32Function, cParms, paParms);
280 LogFlowFunc (("returning\n"));
281 }
282
283 /**
284 * @copydoc VBOXHGCMSVCHELPERS::pfnHostCall
285 * Wraps to the hostCall member function
286 */
287 static DECLCALLBACK(int) svcHostCall (void *pvService,
288 uint32_t u32Function,
289 uint32_t cParms,
290 VBOXHGCMSVCPARM paParms[])
291 {
292 AssertLogRelReturn(VALID_PTR(pvService), VERR_INVALID_PARAMETER);
293 LogFlowFunc (("pvService=%p, u32Function=%u, cParms=%u, paParms=%p\n", pvService, u32Function, cParms, paParms));
294 SELF *pSelf = reinterpret_cast<SELF *>(pvService);
295 int rc = pSelf->hostCall(u32Function, cParms, paParms);
296 LogFlowFunc (("rc=%Rrc\n", rc));
297 return rc;
298 }
299
300 /**
301 * @copydoc VBOXHGCMSVCHELPERS::pfnRegisterExtension
302 * Installs a host callback for notifications of property changes.
303 */
304 static DECLCALLBACK(int) svcRegisterExtension (void *pvService,
305 PFNHGCMSVCEXT pfnExtension,
306 void *pvExtension)
307 {
308 AssertLogRelReturn(VALID_PTR(pvService), VERR_INVALID_PARAMETER);
309 SELF *pSelf = reinterpret_cast<SELF *>(pvService);
310 pSelf->mpfnHostCallback = pfnExtension;
311 pSelf->mpvHostData = pvExtension;
312 return VINF_SUCCESS;
313 }
314private:
315 static DECLCALLBACK(int) reqThreadFn(RTTHREAD ThreadSelf, void *pvUser);
316 int validateName(const char *pszName, uint32_t cbName);
317 int validateValue(const char *pszValue, uint32_t cbValue);
318 int setPropertyBlock(uint32_t cParms, VBOXHGCMSVCPARM paParms[]);
319 int getProperty(uint32_t cParms, VBOXHGCMSVCPARM paParms[]);
320 int setProperty(uint32_t cParms, VBOXHGCMSVCPARM paParms[], bool isGuest);
321 int delProperty(uint32_t cParms, VBOXHGCMSVCPARM paParms[], bool isGuest);
322 int enumProps(uint32_t cParms, VBOXHGCMSVCPARM paParms[]);
323 int getNotification(VBOXHGCMCALLHANDLE callHandle, uint32_t cParms,
324 VBOXHGCMSVCPARM paParms[]);
325 int getOldNotificationInternal(const char *pszPattern,
326 uint64_t u64Timestamp, Property *pProp);
327 int getNotificationWriteOut(VBOXHGCMSVCPARM paParms[], Property prop);
328 void doNotifications(const char *pszProperty, uint64_t u64Timestamp);
329 static DECLCALLBACK(int) reqNotify(PFNHGCMSVCEXT pfnCallback,
330 void *pvData, char *pszName,
331 char *pszValue, uint32_t u32TimeHigh,
332 uint32_t u32TimeLow, char *pszFlags);
333 /**
334 * Empty request function for terminating the request thread.
335 * @returns VINF_EOF to cause the request processing function to return
336 * @todo return something more appropriate
337 */
338 static DECLCALLBACK(int) reqVoid() { return VINF_EOF; }
339
340 void call (VBOXHGCMCALLHANDLE callHandle, uint32_t u32ClientID,
341 void *pvClient, uint32_t eFunction, uint32_t cParms,
342 VBOXHGCMSVCPARM paParms[]);
343 int hostCall (uint32_t eFunction, uint32_t cParms, VBOXHGCMSVCPARM paParms[]);
344 int uninit ();
345};
346
347
348/**
349 * Thread function for processing the request queue
350 * @copydoc FNRTTHREAD
351 */
352DECLCALLBACK(int) Service::reqThreadFn(RTTHREAD ThreadSelf, void *pvUser)
353{
354 SELF *pSelf = reinterpret_cast<SELF *>(pvUser);
355 while (!pSelf->mfExitThread)
356 RTReqProcess(pSelf->mReqQueue, RT_INDEFINITE_WAIT);
357 return VINF_SUCCESS;
358}
359
360
361/**
362 * Check that a string fits our criteria for a property name.
363 *
364 * @returns IPRT status code
365 * @param pszName the string to check, must be valid Utf8
366 * @param cbName the number of bytes @a pszName points to, including the
367 * terminating '\0'
368 * @thread HGCM
369 */
370int Service::validateName(const char *pszName, uint32_t cbName)
371{
372 LogFlowFunc(("cbName=%d\n", cbName));
373 int rc = VINF_SUCCESS;
374 if (RT_SUCCESS(rc) && (cbName < 2))
375 rc = VERR_INVALID_PARAMETER;
376 for (unsigned i = 0; RT_SUCCESS(rc) && i < cbName; ++i)
377 if (pszName[i] == '*' || pszName[i] == '?' || pszName[i] == '|')
378 rc = VERR_INVALID_PARAMETER;
379 LogFlowFunc(("returning %Rrc\n", rc));
380 return rc;
381}
382
383
384/**
385 * Check a string fits our criteria for the value of a guest property.
386 *
387 * @returns IPRT status code
388 * @param pszValue the string to check, must be valid Utf8
389 * @param cbValue the length in bytes of @a pszValue, including the
390 * terminator
391 * @thread HGCM
392 */
393int Service::validateValue(const char *pszValue, uint32_t cbValue)
394{
395 LogFlowFunc(("cbValue=%d\n", cbValue));
396
397 int rc = VINF_SUCCESS;
398 if (RT_SUCCESS(rc) && cbValue == 0)
399 rc = VERR_INVALID_PARAMETER;
400 if (RT_SUCCESS(rc))
401 LogFlow((" pszValue=%s\n", cbValue > 0 ? pszValue : NULL));
402 LogFlowFunc(("returning %Rrc\n", rc));
403 return rc;
404}
405
406/**
407 * Set a block of properties in the property registry, checking the validity
408 * of the arguments passed.
409 *
410 * @returns iprt status value
411 * @param cParms the number of HGCM parameters supplied
412 * @param paParms the array of HGCM parameters
413 * @thread HGCM
414 */
415int Service::setPropertyBlock(uint32_t cParms, VBOXHGCMSVCPARM paParms[])
416{
417 char **ppNames, **ppValues, **ppFlags;
418 uint64_t *pTimestamps;
419 uint32_t cbDummy;
420 int rc = VINF_SUCCESS;
421
422 /*
423 * Get and validate the parameters
424 */
425 if ( (cParms != 4)
426 || RT_FAILURE(paParms[0].getPointer ((void **) &ppNames, &cbDummy))
427 || RT_FAILURE(paParms[1].getPointer ((void **) &ppValues, &cbDummy))
428 || RT_FAILURE(paParms[2].getPointer ((void **) &pTimestamps, &cbDummy))
429 || RT_FAILURE(paParms[3].getPointer ((void **) &ppFlags, &cbDummy))
430 )
431 rc = VERR_INVALID_PARAMETER;
432
433 /*
434 * Add the properties to the end of the list. If we succeed then we
435 * will remove duplicates afterwards.
436 */
437 /* Remember the last property before we started adding, for rollback or
438 * cleanup. */
439 PropertyList::iterator itEnd = mProperties.end();
440 if (!mProperties.empty())
441 --itEnd;
442 try
443 {
444 for (unsigned i = 0; RT_SUCCESS(rc) && ppNames[i] != NULL; ++i)
445 {
446 uint32_t fFlags;
447 if ( !VALID_PTR(ppNames[i])
448 || !VALID_PTR(ppValues[i])
449 || !VALID_PTR(ppFlags[i])
450 )
451 rc = VERR_INVALID_POINTER;
452 if (RT_SUCCESS(rc))
453 rc = validateFlags(ppFlags[i], &fFlags);
454 if (RT_SUCCESS(rc))
455 mProperties.push_back(Property(ppNames[i], ppValues[i],
456 pTimestamps[i], fFlags));
457 }
458 }
459 catch (std::bad_alloc)
460 {
461 rc = VERR_NO_MEMORY;
462 }
463
464 /*
465 * If all went well then remove the duplicate elements.
466 */
467 if (RT_SUCCESS(rc) && itEnd != mProperties.end())
468 {
469 ++itEnd;
470 for (unsigned i = 0; ppNames[i] != NULL; ++i)
471 {
472 bool found = false;
473 for (PropertyList::iterator it = mProperties.begin();
474 !found && it != itEnd; ++it)
475 if (it->mName.compare(ppNames[i]) == 0)
476 {
477 found = true;
478 mProperties.erase(it);
479 }
480 }
481 }
482
483 /*
484 * If something went wrong then rollback. This is possible because we
485 * haven't deleted anything yet.
486 */
487 if (RT_FAILURE(rc))
488 {
489 if (itEnd != mProperties.end())
490 ++itEnd;
491 mProperties.erase(itEnd, mProperties.end());
492 }
493 return rc;
494}
495
496/**
497 * Retrieve a value from the property registry by name, checking the validity
498 * of the arguments passed. If the guest has not allocated enough buffer
499 * space for the value then we return VERR_OVERFLOW and set the size of the
500 * buffer needed in the "size" HGCM parameter. If the name was not found at
501 * all, we return VERR_NOT_FOUND.
502 *
503 * @returns iprt status value
504 * @param cParms the number of HGCM parameters supplied
505 * @param paParms the array of HGCM parameters
506 * @thread HGCM
507 */
508int Service::getProperty(uint32_t cParms, VBOXHGCMSVCPARM paParms[])
509{
510 int rc = VINF_SUCCESS;
511 const char *pcszName = NULL; /* shut up gcc */
512 char *pchBuf;
513 uint32_t cchName, cchBuf;
514 char szFlags[MAX_FLAGS_LEN];
515
516 /*
517 * Get and validate the parameters
518 */
519 LogFlowThisFunc(("\n"));
520 if ( cParms != 4 /* Hardcoded value as the next lines depend on it. */
521 || RT_FAILURE (paParms[0].getString(&pcszName, &cchName)) /* name */
522 || RT_FAILURE (paParms[1].getBuffer((void **) &pchBuf, &cchBuf)) /* buffer */
523 )
524 rc = VERR_INVALID_PARAMETER;
525 else
526 rc = validateName(pcszName, cchName);
527
528 /*
529 * Read and set the values we will return
530 */
531
532 /* Get the value size */
533 PropertyList::const_iterator it;
534 if (RT_SUCCESS(rc))
535 {
536 rc = VERR_NOT_FOUND;
537 for (it = mProperties.begin(); it != mProperties.end(); ++it)
538 if (it->mName.compare(pcszName) == 0)
539 {
540 rc = VINF_SUCCESS;
541 break;
542 }
543 }
544 if (RT_SUCCESS(rc))
545 rc = writeFlags(it->mFlags, szFlags);
546 if (RT_SUCCESS(rc))
547 {
548 /* Check that the buffer is big enough */
549 size_t cchBufActual = it->mValue.size() + 1 + strlen(szFlags);
550 paParms[3].setUInt32 ((uint32_t)cchBufActual);
551 if (cchBufActual <= cchBuf)
552 {
553 /* Write the value, flags and timestamp */
554 it->mValue.copy(pchBuf, cchBuf, 0);
555 pchBuf[it->mValue.size()] = '\0'; /* Terminate the value */
556 strcpy(pchBuf + it->mValue.size() + 1, szFlags);
557 paParms[2].setUInt64 (it->mTimestamp);
558
559 /*
560 * Done! Do exit logging and return.
561 */
562 Log2(("Queried string %s, value=%s, timestamp=%lld, flags=%s\n",
563 pcszName, it->mValue.c_str(), it->mTimestamp, szFlags));
564 }
565 else
566 rc = VERR_BUFFER_OVERFLOW;
567 }
568
569 LogFlowThisFunc(("rc = %Rrc\n", rc));
570 return rc;
571}
572
573/**
574 * Set a value in the property registry by name, checking the validity
575 * of the arguments passed.
576 *
577 * @returns iprt status value
578 * @param cParms the number of HGCM parameters supplied
579 * @param paParms the array of HGCM parameters
580 * @param isGuest is this call coming from the guest (or the host)?
581 * @throws std::bad_alloc if an out of memory condition occurs
582 * @thread HGCM
583 */
584int Service::setProperty(uint32_t cParms, VBOXHGCMSVCPARM paParms[], bool isGuest)
585{
586 int rc = VINF_SUCCESS;
587 const char *pcszName = NULL; /* shut up gcc */
588 const char *pcszValue = NULL; /* ditto */
589 const char *pcszFlags = NULL;
590 uint32_t cchName, cchValue, cchFlags = 0;
591 uint32_t fFlags = NILFLAG;
592 RTTIMESPEC time;
593 uint64_t u64TimeNano = RTTimeSpecGetNano(RTTimeNow(&time));
594
595 LogFlowThisFunc(("\n"));
596 /*
597 * First of all, make sure that we won't exceed the maximum number of properties.
598 */
599 if (mProperties.size() >= MAX_PROPS)
600 rc = VERR_TOO_MUCH_DATA;
601
602 /*
603 * General parameter correctness checking.
604 */
605 if ( RT_SUCCESS(rc)
606 && ( (cParms < 2) || (cParms > 3) /* Hardcoded value as the next lines depend on it. */
607 || RT_FAILURE(paParms[0].getString(&pcszName, &cchName)) /* name */
608 || RT_FAILURE(paParms[1].getString(&pcszValue, &cchValue)) /* value */
609 || ( (3 == cParms)
610 && RT_FAILURE(paParms[2].getString(&pcszFlags, &cchFlags)) /* flags */
611 )
612 )
613 )
614 rc = VERR_INVALID_PARAMETER;
615
616 /*
617 * Check the values passed in the parameters for correctness.
618 */
619 if (RT_SUCCESS(rc))
620 rc = validateName(pcszName, cchName);
621 if (RT_SUCCESS(rc))
622 rc = validateValue(pcszValue, cchValue);
623 if ((3 == cParms) && RT_SUCCESS(rc))
624 rc = RTStrValidateEncodingEx(pcszFlags, cchFlags,
625 RTSTR_VALIDATE_ENCODING_ZERO_TERMINATED);
626 if ((3 == cParms) && RT_SUCCESS(rc))
627 rc = validateFlags(pcszFlags, &fFlags);
628
629 /*
630 * If the property already exists, check its flags to see if we are allowed
631 * to change it.
632 */
633 PropertyList::iterator it;
634 bool found = false;
635 if (RT_SUCCESS(rc))
636 for (it = mProperties.begin(); it != mProperties.end(); ++it)
637 if (it->mName.compare(pcszName) == 0)
638 {
639 found = true;
640 break;
641 }
642 if (RT_SUCCESS(rc) && found)
643 if ( (isGuest && (it->mFlags & RDONLYGUEST))
644 || (!isGuest && (it->mFlags & RDONLYHOST))
645 )
646 rc = VERR_PERMISSION_DENIED;
647
648 /*
649 * Set the actual value
650 */
651 if (RT_SUCCESS(rc))
652 {
653 if (found)
654 {
655 it->mValue = pcszValue;
656 it->mTimestamp = u64TimeNano;
657 it->mFlags = fFlags;
658 }
659 else /* This can throw. No problem as we have nothing to roll back. */
660 mProperties.push_back(Property(pcszName, pcszValue, u64TimeNano, fFlags));
661 }
662
663 /*
664 * Send a notification to the host and return.
665 */
666 if (RT_SUCCESS(rc))
667 {
668 // if (isGuest) /* Notify the host even for properties that the host
669 // * changed. Less efficient, but ensures consistency. */
670 doNotifications(pcszName, u64TimeNano);
671 Log2(("Set string %s, rc=%Rrc, value=%s\n", pcszName, rc, pcszValue));
672 }
673 LogFlowThisFunc(("rc = %Rrc\n", rc));
674 return rc;
675}
676
677
678/**
679 * Remove a value in the property registry by name, checking the validity
680 * of the arguments passed.
681 *
682 * @returns iprt status value
683 * @param cParms the number of HGCM parameters supplied
684 * @param paParms the array of HGCM parameters
685 * @param isGuest is this call coming from the guest (or the host)?
686 * @thread HGCM
687 */
688int Service::delProperty(uint32_t cParms, VBOXHGCMSVCPARM paParms[], bool isGuest)
689{
690 int rc = VINF_SUCCESS;
691 const char *pcszName = NULL; /* shut up gcc */
692 uint32_t cbName;
693
694 LogFlowThisFunc(("\n"));
695
696 /*
697 * Check the user-supplied parameters.
698 */
699 if ( (cParms == 1) /* Hardcoded value as the next lines depend on it. */
700 && RT_SUCCESS(paParms[0].getString(&pcszName, &cbName)) /* name */
701 )
702 rc = validateName(pcszName, cbName);
703 else
704 rc = VERR_INVALID_PARAMETER;
705
706 /*
707 * If the property exists, check its flags to see if we are allowed
708 * to change it.
709 */
710 PropertyList::iterator it;
711 bool found = false;
712 if (RT_SUCCESS(rc))
713 for (it = mProperties.begin(); it != mProperties.end(); ++it)
714 if (it->mName.compare(pcszName) == 0)
715 {
716 found = true;
717 break;
718 }
719 if (RT_SUCCESS(rc) && found)
720 if ( (isGuest && (it->mFlags & RDONLYGUEST))
721 || (!isGuest && (it->mFlags & RDONLYHOST))
722 )
723 rc = VERR_PERMISSION_DENIED;
724
725 /*
726 * And delete the property if all is well.
727 */
728 if (RT_SUCCESS(rc) && found)
729 {
730 RTTIMESPEC time;
731 uint64_t u64Timestamp = RTTimeSpecGetNano(RTTimeNow(&time));
732 mProperties.erase(it);
733 // if (isGuest) /* Notify the host even for properties that the host
734 // * changed. Less efficient, but ensures consistency. */
735 doNotifications(pcszName, u64Timestamp);
736 }
737 LogFlowThisFunc(("rc = %Rrc\n", rc));
738 return rc;
739}
740
741/**
742 * Enumerate guest properties by mask, checking the validity
743 * of the arguments passed.
744 *
745 * @returns iprt status value
746 * @param cParms the number of HGCM parameters supplied
747 * @param paParms the array of HGCM parameters
748 * @thread HGCM
749 */
750int Service::enumProps(uint32_t cParms, VBOXHGCMSVCPARM paParms[])
751{
752 int rc = VINF_SUCCESS;
753
754 /*
755 * Get the HGCM function arguments.
756 */
757 char *pcchPatterns = NULL, *pchBuf = NULL;
758 uint32_t cchPatterns = 0, cchBuf = 0;
759 LogFlowThisFunc(("\n"));
760 if ( (cParms != 3) /* Hardcoded value as the next lines depend on it. */
761 || RT_FAILURE(paParms[0].getString(&pcchPatterns, &cchPatterns)) /* patterns */
762 || RT_FAILURE(paParms[1].getBuffer((void **) &pchBuf, &cchBuf)) /* return buffer */
763 )
764 rc = VERR_INVALID_PARAMETER;
765 if (RT_SUCCESS(rc) && cchPatterns > MAX_PATTERN_LEN)
766 rc = VERR_TOO_MUCH_DATA;
767
768 /*
769 * First repack the patterns into the format expected by RTStrSimplePatternMatch()
770 */
771 char pszPatterns[MAX_PATTERN_LEN];
772 for (unsigned i = 0; i < cchPatterns - 1; ++i)
773 if (pcchPatterns[i] != '\0')
774 pszPatterns[i] = pcchPatterns[i];
775 else
776 pszPatterns[i] = '|';
777 pszPatterns[cchPatterns - 1] = '\0';
778
779 /*
780 * Next enumerate into a temporary buffer. This can throw, but this is
781 * not a problem as we have nothing to roll back.
782 */
783 std::string buffer;
784 for (PropertyList::const_iterator it = mProperties.begin();
785 RT_SUCCESS(rc) && (it != mProperties.end()); ++it)
786 {
787 if (it->Matches(pszPatterns))
788 {
789 char szFlags[MAX_FLAGS_LEN];
790 char szTimestamp[256];
791 uint32_t cchTimestamp;
792 buffer += it->mName;
793 buffer += '\0';
794 buffer += it->mValue;
795 buffer += '\0';
796 cchTimestamp = RTStrFormatNumber(szTimestamp, it->mTimestamp,
797 10, 0, 0, 0);
798 buffer.append(szTimestamp, cchTimestamp);
799 buffer += '\0';
800 rc = writeFlags(it->mFlags, szFlags);
801 if (RT_SUCCESS(rc))
802 buffer += szFlags;
803 buffer += '\0';
804 }
805 }
806 buffer.append(4, '\0'); /* The final terminators */
807
808 /*
809 * Finally write out the temporary buffer to the real one if it is not too
810 * small.
811 */
812 if (RT_SUCCESS(rc))
813 {
814 paParms[2].setUInt32 ((uint32_t)buffer.size());
815 /* Copy the memory if it fits into the guest buffer */
816 if (buffer.size() <= cchBuf)
817 buffer.copy(pchBuf, cchBuf);
818 else
819 rc = VERR_BUFFER_OVERFLOW;
820 }
821 return rc;
822}
823
824/** Helper query used by getOldNotification */
825int Service::getOldNotificationInternal(const char *pszPatterns,
826 uint64_t u64Timestamp,
827 Property *pProp)
828{
829 int rc = VINF_SUCCESS;
830 bool warn = false;
831
832 /* We count backwards, as the guest should normally be querying the
833 * most recent events. */
834 PropertyList::reverse_iterator it = mGuestNotifications.rbegin();
835 for (; it->mTimestamp != u64Timestamp && it != mGuestNotifications.rend();
836 ++it) {}
837 /* Warn if the timestamp was not found. */
838 if (it->mTimestamp != u64Timestamp)
839 warn = true;
840 /* Now look for an event matching the patterns supplied. The base()
841 * member conveniently points to the following element. */
842 PropertyList::iterator base = it.base();
843 for (; !base->Matches(pszPatterns) && base != mGuestNotifications.end();
844 ++base) {}
845 if (RT_SUCCESS(rc) && base != mGuestNotifications.end())
846 *pProp = *base;
847 else if (RT_SUCCESS(rc))
848 *pProp = Property();
849 if (warn)
850 rc = VWRN_NOT_FOUND;
851 return rc;
852}
853
854/** Helper query used by getNotification */
855int Service::getNotificationWriteOut(VBOXHGCMSVCPARM paParms[], Property prop)
856{
857 int rc = VINF_SUCCESS;
858 /* Format the data to write to the buffer. */
859 std::string buffer;
860 uint64_t u64Timestamp;
861 char *pchBuf;
862 uint32_t cchBuf;
863 rc = paParms[2].getBuffer((void **) &pchBuf, &cchBuf);
864 if (RT_SUCCESS(rc))
865 {
866 char szFlags[MAX_FLAGS_LEN];
867 rc = writeFlags(prop.mFlags, szFlags);
868 if (RT_SUCCESS(rc))
869 {
870 buffer += prop.mName;
871 buffer += '\0';
872 buffer += prop.mValue;
873 buffer += '\0';
874 buffer += szFlags;
875 buffer += '\0';
876 u64Timestamp = prop.mTimestamp;
877 }
878 }
879 /* Write out the data. */
880 if (RT_SUCCESS(rc))
881 {
882 paParms[1].setUInt64(u64Timestamp);
883 paParms[3].setUInt32((uint32_t)buffer.size());
884 if (buffer.size() <= cchBuf)
885 buffer.copy(pchBuf, cchBuf);
886 else
887 rc = VERR_BUFFER_OVERFLOW;
888 }
889 return rc;
890}
891
892/**
893 * Get the next guest notification.
894 *
895 * @returns iprt status value
896 * @param cParms the number of HGCM parameters supplied
897 * @param paParms the array of HGCM parameters
898 * @thread HGCM
899 * @throws can throw std::bad_alloc
900 */
901int Service::getNotification(VBOXHGCMCALLHANDLE callHandle, uint32_t cParms,
902 VBOXHGCMSVCPARM paParms[])
903{
904 int rc = VINF_SUCCESS;
905 char *pszPatterns = NULL; /* shut up gcc */
906 char *pchBuf;
907 uint32_t cchPatterns = 0;
908 uint32_t cchBuf = 0;
909 uint64_t u64Timestamp;
910
911 /*
912 * Get the HGCM function arguments and perform basic verification.
913 */
914 LogFlowThisFunc(("\n"));
915 if ( (cParms != 4) /* Hardcoded value as the next lines depend on it. */
916 || RT_FAILURE(paParms[0].getString(&pszPatterns, &cchPatterns)) /* patterns */
917 || RT_FAILURE(paParms[1].getUInt64(&u64Timestamp)) /* timestamp */
918 || RT_FAILURE(paParms[2].getBuffer((void **) &pchBuf, &cchBuf)) /* return buffer */
919 )
920 rc = VERR_INVALID_PARAMETER;
921 if (RT_SUCCESS(rc))
922 LogFlow((" pszPatterns=%s, u64Timestamp=%llu\n", pszPatterns,
923 u64Timestamp));
924
925 /*
926 * If no timestamp was supplied or no notification was found in the queue
927 * of old notifications, enqueue the request in the waiting queue.
928 */
929 Property prop;
930 if (RT_SUCCESS(rc) && u64Timestamp != 0)
931 rc = getOldNotification(pszPatterns, u64Timestamp, &prop);
932 if (RT_SUCCESS(rc) && prop.isNull())
933 {
934 mGuestWaiters.push_back(GuestCall(callHandle, GET_NOTIFICATION,
935 paParms, rc));
936 rc = VINF_HGCM_ASYNC_EXECUTE;
937 }
938 /*
939 * Otherwise reply at once with the enqueued notification we found.
940 */
941 else
942 {
943 int rc2 = getNotificationWriteOut(paParms, prop);
944 if (RT_FAILURE(rc2))
945 rc = rc2;
946 }
947 return rc;
948}
949
950/**
951 * Notify the service owner and the guest that a property has been
952 * added/deleted/changed
953 * @param pszProperty the name of the property which has changed
954 * @param u64Timestamp the time at which the change took place
955 * @note this call allocates memory which the reqNotify request is expected to
956 * free again, using RTStrFree().
957 *
958 * @thread HGCM service
959 */
960void Service::doNotifications(const char *pszProperty, uint64_t u64Timestamp)
961{
962 int rc = VINF_SUCCESS;
963
964 AssertPtrReturnVoid(pszProperty);
965 LogFlowThisFunc (("pszProperty=%s, u64Timestamp=%llu\n", pszProperty, u64Timestamp));
966 /* Ensure that our timestamp is different to the last one. */
967 if ( !mGuestNotifications.empty()
968 && u64Timestamp == mGuestNotifications.back().mTimestamp)
969 ++u64Timestamp;
970
971 /*
972 * Try to find the property. Create a change event if we find it and a
973 * delete event if we do not.
974 */
975 Property prop;
976 prop.mName = pszProperty;
977 prop.mTimestamp = u64Timestamp;
978 /* prop is currently a delete event for pszProperty */
979 bool found = false;
980 if (RT_SUCCESS(rc))
981 for (PropertyList::const_iterator it = mProperties.begin();
982 !found && it != mProperties.end(); ++it)
983 if (it->mName.compare(pszProperty) == 0)
984 {
985 found = true;
986 /* Make prop into a change event. */
987 prop.mValue = it->mValue;
988 prop.mFlags = it->mFlags;
989 }
990
991
992 /* Release waiters if applicable and add the event to the queue for
993 * guest notifications */
994 if (RT_SUCCESS(rc))
995 {
996 try
997 {
998 CallList::iterator it = mGuestWaiters.begin();
999 while (it != mGuestWaiters.end())
1000 {
1001 const char *pszPatterns;
1002 uint32_t cchPatterns;
1003 it->mParms[0].getString(&pszPatterns, &cchPatterns);
1004 if (prop.Matches(pszPatterns))
1005 {
1006 GuestCall call = *it;
1007 int rc2 = getNotificationWriteOut(call.mParms, prop);
1008 if (RT_SUCCESS(rc2))
1009 rc2 = call.mRc;
1010 mpHelpers->pfnCallComplete (call.mHandle, rc2);
1011 it = mGuestWaiters.erase(it);
1012 }
1013 else
1014 ++it;
1015 }
1016 mGuestNotifications.push_back(prop);
1017 }
1018 catch (std::bad_alloc)
1019 {
1020 rc = VERR_NO_MEMORY;
1021 }
1022 }
1023 if (mGuestNotifications.size() > MAX_GUEST_NOTIFICATIONS)
1024 mGuestNotifications.pop_front();
1025
1026#ifndef VBOX_GUEST_PROP_TEST_NOTHREAD
1027 /*
1028 * Host notifications - first case: if the property exists then send its
1029 * current value
1030 */
1031 char *pszName = NULL, *pszValue = NULL, *pszFlags = NULL;
1032
1033 if (found && mpfnHostCallback != NULL)
1034 {
1035 char szFlags[MAX_FLAGS_LEN];
1036 /* Send out a host notification */
1037 rc = writeFlags(prop.mFlags, szFlags);
1038 if (RT_SUCCESS(rc))
1039 rc = RTStrDupEx(&pszName, pszProperty);
1040 if (RT_SUCCESS(rc))
1041 rc = RTStrDupEx(&pszValue, prop.mValue.c_str());
1042 if (RT_SUCCESS(rc))
1043 rc = RTStrDupEx(&pszFlags, szFlags);
1044 if (RT_SUCCESS(rc))
1045 {
1046 LogFlowThisFunc (("pszName=%p (%s)\n", pszName, pszName));
1047 LogFlowThisFunc (("pszValue=%p (%s)\n", pszValue, pszValue));
1048 LogFlowThisFunc (("pszFlags=%p (%s)\n", pszFlags, pszFlags));
1049 rc = RTReqCallEx(mReqQueue, NULL, 0, RTREQFLAGS_NO_WAIT,
1050 (PFNRT)Service::reqNotify, 7, mpfnHostCallback,
1051 mpvHostData, pszName, pszValue,
1052 (uint32_t) RT_HIDWORD(u64Timestamp),
1053 (uint32_t) RT_LODWORD(u64Timestamp), pszFlags);
1054 }
1055 }
1056
1057 /*
1058 * Host notifications - second case: if the property does not exist then
1059 * send the host an empty value
1060 */
1061 if (!found && mpfnHostCallback != NULL)
1062 {
1063 /* Send out a host notification */
1064 rc = RTStrDupEx(&pszName, pszProperty);
1065 if (RT_SUCCESS(rc))
1066 {
1067 LogFlowThisFunc (("pszName=%p (%s)\n", pszName, pszName));
1068 rc = RTReqCallEx(mReqQueue, NULL, 0, RTREQFLAGS_NO_WAIT,
1069 (PFNRT)Service::reqNotify, 7, mpfnHostCallback,
1070 mpvHostData, pszName, (uintptr_t) NULL,
1071 (uint32_t) RT_HIDWORD(u64Timestamp),
1072 (uint32_t) RT_LODWORD(u64Timestamp),
1073 (uintptr_t) NULL);
1074 }
1075 }
1076 if (RT_FAILURE(rc)) /* clean up if we failed somewhere */
1077 {
1078 LogThisFunc (("Failed, freeing allocated strings.\n"));
1079 RTStrFree(pszName);
1080 RTStrFree(pszValue);
1081 RTStrFree(pszFlags);
1082 }
1083 LogFlowThisFunc (("returning\n"));
1084#endif /* VBOX_GUEST_PROP_TEST_NOTHREAD not defined */
1085}
1086
1087/**
1088 * Notify the service owner that a property has been added/deleted/changed.
1089 * asynchronous part.
1090 * @param pszProperty the name of the property which has changed
1091 * @note this call allocates memory which the reqNotify request is expected to
1092 * free again, using RTStrFree().
1093 *
1094 * @thread request thread
1095 */
1096/* static */
1097int Service::reqNotify(PFNHGCMSVCEXT pfnCallback, void *pvData,
1098 char *pszName, char *pszValue, uint32_t u32TimeHigh,
1099 uint32_t u32TimeLow, char *pszFlags)
1100{
1101 LogFlowFunc (("pfnCallback=%p, pvData=%p, pszName=%p, pszValue=%p, u32TimeHigh=%u, u32TimeLow=%u, pszFlags=%p\n", pfnCallback, pvData, pszName, pszValue, u32TimeHigh, u32TimeLow, pszFlags));
1102 LogFlowFunc (("pszName=%s\n", pszName));
1103 LogFlowFunc (("pszValue=%s\n", pszValue));
1104 LogFlowFunc (("pszFlags=%s\n", pszFlags));
1105 /* LogFlowFunc (("pfnCallback=%p, pvData=%p, pszName=%s, pszValue=%s, u32TimeHigh=%u, u32TimeLow=%u, pszFlags=%s\n", pfnCallback, pvData, pszName, pszValue, u32TimeHigh, u32TimeLow, pszFlags)); */
1106 HOSTCALLBACKDATA HostCallbackData;
1107 HostCallbackData.u32Magic = HOSTCALLBACKMAGIC;
1108 HostCallbackData.pcszName = pszName;
1109 HostCallbackData.pcszValue = pszValue;
1110 HostCallbackData.u64Timestamp = RT_MAKE_U64(u32TimeLow, u32TimeHigh);
1111 HostCallbackData.pcszFlags = pszFlags;
1112 int rc = pfnCallback(pvData, 0 /*u32Function*/, (void *)(&HostCallbackData),
1113 sizeof(HostCallbackData));
1114 AssertRC(rc);
1115 LogFlowFunc (("Freeing strings\n"));
1116 RTStrFree(pszName);
1117 RTStrFree(pszValue);
1118 RTStrFree(pszFlags);
1119 LogFlowFunc (("returning success\n"));
1120 return VINF_SUCCESS;
1121}
1122
1123
1124/**
1125 * Handle an HGCM service call.
1126 * @copydoc VBOXHGCMSVCFNTABLE::pfnCall
1127 * @note All functions which do not involve an unreasonable delay will be
1128 * handled synchronously. If needed, we will add a request handler
1129 * thread in future for those which do.
1130 *
1131 * @thread HGCM
1132 */
1133void Service::call (VBOXHGCMCALLHANDLE callHandle, uint32_t u32ClientID,
1134 void * /* pvClient */, uint32_t eFunction, uint32_t cParms,
1135 VBOXHGCMSVCPARM paParms[])
1136{
1137 int rc = VINF_SUCCESS;
1138 LogFlowFunc(("u32ClientID = %d, fn = %d, cParms = %d, pparms = %d\n",
1139 u32ClientID, eFunction, cParms, paParms));
1140
1141 try
1142 {
1143 switch (eFunction)
1144 {
1145 /* The guest wishes to read a property */
1146 case GET_PROP:
1147 LogFlowFunc(("GET_PROP\n"));
1148 rc = getProperty(cParms, paParms);
1149 break;
1150
1151 /* The guest wishes to set a property */
1152 case SET_PROP:
1153 LogFlowFunc(("SET_PROP\n"));
1154 rc = setProperty(cParms, paParms, true);
1155 break;
1156
1157 /* The guest wishes to set a property value */
1158 case SET_PROP_VALUE:
1159 LogFlowFunc(("SET_PROP_VALUE\n"));
1160 rc = setProperty(cParms, paParms, true);
1161 break;
1162
1163 /* The guest wishes to remove a configuration value */
1164 case DEL_PROP:
1165 LogFlowFunc(("DEL_PROP\n"));
1166 rc = delProperty(cParms, paParms, true);
1167 break;
1168
1169 /* The guest wishes to enumerate all properties */
1170 case ENUM_PROPS:
1171 LogFlowFunc(("ENUM_PROPS\n"));
1172 rc = enumProps(cParms, paParms);
1173 break;
1174
1175 /* The guest wishes to get the next property notification */
1176 case GET_NOTIFICATION:
1177 LogFlowFunc(("GET_NOTIFICATION\n"));
1178 rc = getNotification(callHandle, cParms, paParms);
1179 break;
1180
1181 default:
1182 rc = VERR_NOT_IMPLEMENTED;
1183 }
1184 }
1185 catch (std::bad_alloc)
1186 {
1187 rc = VERR_NO_MEMORY;
1188 }
1189 LogFlowFunc(("rc = %Rrc\n", rc));
1190 if (rc != VINF_HGCM_ASYNC_EXECUTE)
1191 {
1192 mpHelpers->pfnCallComplete (callHandle, rc);
1193 }
1194}
1195
1196
1197/**
1198 * Service call handler for the host.
1199 * @copydoc VBOXHGCMSVCFNTABLE::pfnHostCall
1200 * @thread hgcm
1201 */
1202int Service::hostCall (uint32_t eFunction, uint32_t cParms, VBOXHGCMSVCPARM paParms[])
1203{
1204 int rc = VINF_SUCCESS;
1205
1206 LogFlowFunc(("fn = %d, cParms = %d, pparms = %d\n",
1207 eFunction, cParms, paParms));
1208
1209 try
1210 {
1211 switch (eFunction)
1212 {
1213 /* The host wishes to set a block of properties */
1214 case SET_PROPS_HOST:
1215 LogFlowFunc(("SET_PROPS_HOST\n"));
1216 rc = setPropertyBlock(cParms, paParms);
1217 break;
1218
1219 /* The host wishes to read a configuration value */
1220 case GET_PROP_HOST:
1221 LogFlowFunc(("GET_PROP_HOST\n"));
1222 rc = getProperty(cParms, paParms);
1223 break;
1224
1225 /* The host wishes to set a configuration value */
1226 case SET_PROP_HOST:
1227 LogFlowFunc(("SET_PROP_HOST\n"));
1228 rc = setProperty(cParms, paParms, false);
1229 break;
1230
1231 /* The host wishes to set a configuration value */
1232 case SET_PROP_VALUE_HOST:
1233 LogFlowFunc(("SET_PROP_VALUE_HOST\n"));
1234 rc = setProperty(cParms, paParms, false);
1235 break;
1236
1237 /* The host wishes to remove a configuration value */
1238 case DEL_PROP_HOST:
1239 LogFlowFunc(("DEL_PROP_HOST\n"));
1240 rc = delProperty(cParms, paParms, false);
1241 break;
1242
1243 /* The host wishes to enumerate all properties */
1244 case ENUM_PROPS_HOST:
1245 LogFlowFunc(("ENUM_PROPS\n"));
1246 rc = enumProps(cParms, paParms);
1247 break;
1248
1249 default:
1250 rc = VERR_NOT_SUPPORTED;
1251 break;
1252 }
1253 }
1254 catch (std::bad_alloc)
1255 {
1256 rc = VERR_NO_MEMORY;
1257 }
1258
1259 LogFlowFunc(("rc = %Rrc\n", rc));
1260 return rc;
1261}
1262
1263int Service::uninit()
1264{
1265 int rc = VINF_SUCCESS;
1266 unsigned count = 0;
1267
1268 mfExitThread = true;
1269#ifndef VBOX_GUEST_PROP_TEST_NOTHREAD
1270 rc = RTReqCallEx(mReqQueue, NULL, 0, RTREQFLAGS_NO_WAIT, (PFNRT)reqVoid, 0);
1271 if (RT_SUCCESS(rc))
1272 do
1273 {
1274 rc = RTThreadWait(mReqThread, 1000, NULL);
1275 ++count;
1276 Assert(RT_SUCCESS(rc) || ((VERR_TIMEOUT == rc) && (count != 5)));
1277 } while ((VERR_TIMEOUT == rc) && (count < 300));
1278#endif /* VBOX_GUEST_PROP_TEST_NOTHREAD not defined */
1279 if (RT_SUCCESS(rc))
1280 RTReqDestroyQueue(mReqQueue);
1281 return rc;
1282}
1283
1284} /* namespace guestProp */
1285
1286using guestProp::Service;
1287
1288/**
1289 * @copydoc VBOXHGCMSVCLOAD
1290 */
1291extern "C" DECLCALLBACK(DECLEXPORT(int)) VBoxHGCMSvcLoad (VBOXHGCMSVCFNTABLE *ptable)
1292{
1293 int rc = VINF_SUCCESS;
1294
1295 LogFlowFunc(("ptable = %p\n", ptable));
1296
1297 if (!VALID_PTR(ptable))
1298 {
1299 rc = VERR_INVALID_PARAMETER;
1300 }
1301 else
1302 {
1303 LogFlowFunc(("ptable->cbSize = %d, ptable->u32Version = 0x%08X\n", ptable->cbSize, ptable->u32Version));
1304
1305 if ( ptable->cbSize != sizeof (VBOXHGCMSVCFNTABLE)
1306 || ptable->u32Version != VBOX_HGCM_SVC_VERSION)
1307 {
1308 rc = VERR_VERSION_MISMATCH;
1309 }
1310 else
1311 {
1312 std::auto_ptr<Service> apService;
1313 /* No exceptions may propogate outside. */
1314 try {
1315 apService = std::auto_ptr<Service>(new Service(ptable->pHelpers));
1316 } catch (int rcThrown) {
1317 rc = rcThrown;
1318 } catch (...) {
1319 rc = VERR_UNRESOLVED_ERROR;
1320 }
1321
1322 if (RT_SUCCESS(rc))
1323 {
1324 /* We do not maintain connections, so no client data is needed. */
1325 ptable->cbClient = 0;
1326
1327 ptable->pfnUnload = Service::svcUnload;
1328 ptable->pfnConnect = Service::svcConnectDisconnect;
1329 ptable->pfnDisconnect = Service::svcConnectDisconnect;
1330 ptable->pfnCall = Service::svcCall;
1331 ptable->pfnHostCall = Service::svcHostCall;
1332 ptable->pfnSaveState = NULL; /* The service is stateless, so the normal */
1333 ptable->pfnLoadState = NULL; /* construction done before restoring suffices */
1334 ptable->pfnRegisterExtension = Service::svcRegisterExtension;
1335
1336 /* Service specific initialization. */
1337 ptable->pvService = apService.release();
1338 }
1339 }
1340 }
1341
1342 LogFlowFunc(("returning %Rrc\n", rc));
1343 return rc;
1344}
1345
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