VirtualBox

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

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

HostServices/GuestProperties: fix a todo

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 45.2 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;
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, *pcszValue, *pcszFlags = NULL;
588 uint32_t cchName, cchValue, cchFlags = 0;
589 uint32_t fFlags = NILFLAG;
590 RTTIMESPEC time;
591 uint64_t u64TimeNano = RTTimeSpecGetNano(RTTimeNow(&time));
592
593 LogFlowThisFunc(("\n"));
594 /*
595 * First of all, make sure that we won't exceed the maximum number of properties.
596 */
597 if (mProperties.size() >= MAX_PROPS)
598 rc = VERR_TOO_MUCH_DATA;
599
600 /*
601 * General parameter correctness checking.
602 */
603 if ( RT_SUCCESS(rc)
604 && ( (cParms < 2) || (cParms > 3) /* Hardcoded value as the next lines depend on it. */
605 || RT_FAILURE(paParms[0].getString(&pcszName, &cchName)) /* name */
606 || RT_FAILURE(paParms[1].getString(&pcszValue, &cchValue)) /* value */
607 || ( (3 == cParms)
608 && RT_FAILURE(paParms[2].getString(&pcszFlags, &cchFlags)) /* flags */
609 )
610 )
611 )
612 rc = VERR_INVALID_PARAMETER;
613
614 /*
615 * Check the values passed in the parameters for correctness.
616 */
617 if (RT_SUCCESS(rc))
618 rc = validateName(pcszName, cchName);
619 if (RT_SUCCESS(rc))
620 rc = validateValue(pcszValue, cchValue);
621 if ((3 == cParms) && RT_SUCCESS(rc))
622 rc = RTStrValidateEncodingEx(pcszFlags, cchFlags,
623 RTSTR_VALIDATE_ENCODING_ZERO_TERMINATED);
624 if ((3 == cParms) && RT_SUCCESS(rc))
625 rc = validateFlags(pcszFlags, &fFlags);
626
627 /*
628 * If the property already exists, check its flags to see if we are allowed
629 * to change it.
630 */
631 PropertyList::iterator it;
632 bool found = false;
633 if (RT_SUCCESS(rc))
634 for (it = mProperties.begin(); it != mProperties.end(); ++it)
635 if (it->mName.compare(pcszName) == 0)
636 {
637 found = true;
638 break;
639 }
640 if (RT_SUCCESS(rc) && found)
641 if ( (isGuest && (it->mFlags & RDONLYGUEST))
642 || (!isGuest && (it->mFlags & RDONLYHOST))
643 )
644 rc = VERR_PERMISSION_DENIED;
645
646 /*
647 * Set the actual value
648 */
649 if (RT_SUCCESS(rc))
650 {
651 if (found)
652 {
653 it->mValue = pcszValue;
654 it->mTimestamp = u64TimeNano;
655 it->mFlags = fFlags;
656 }
657 else /* This can throw. No problem as we have nothing to roll back. */
658 mProperties.push_back(Property(pcszName, pcszValue, u64TimeNano, fFlags));
659 }
660
661 /*
662 * Send a notification to the host and return.
663 */
664 if (RT_SUCCESS(rc))
665 {
666 // if (isGuest) /* Notify the host even for properties that the host
667 // * changed. Less efficient, but ensures consistency. */
668 doNotifications(pcszName, u64TimeNano);
669 Log2(("Set string %s, rc=%Rrc, value=%s\n", pcszName, rc, pcszValue));
670 }
671 LogFlowThisFunc(("rc = %Rrc\n", rc));
672 return rc;
673}
674
675
676/**
677 * Remove a value in the property registry by name, checking the validity
678 * of the arguments passed.
679 *
680 * @returns iprt status value
681 * @param cParms the number of HGCM parameters supplied
682 * @param paParms the array of HGCM parameters
683 * @param isGuest is this call coming from the guest (or the host)?
684 * @thread HGCM
685 */
686int Service::delProperty(uint32_t cParms, VBOXHGCMSVCPARM paParms[], bool isGuest)
687{
688 int rc = VINF_SUCCESS;
689 const char *pcszName;
690 uint32_t cbName;
691
692 LogFlowThisFunc(("\n"));
693
694 /*
695 * Check the user-supplied parameters.
696 */
697 if ( (cParms != 1) /* Hardcoded value as the next lines depend on it. */
698 || RT_FAILURE(paParms[0].getString(&pcszName, &cbName)) /* name */
699 )
700 rc = VERR_INVALID_PARAMETER;
701 if (RT_SUCCESS(rc))
702 rc = validateName(pcszName, cbName);
703
704 /*
705 * If the property exists, check its flags to see if we are allowed
706 * to change it.
707 */
708 PropertyList::iterator it;
709 bool found = false;
710 if (RT_SUCCESS(rc))
711 for (it = mProperties.begin(); it != mProperties.end(); ++it)
712 if (it->mName.compare(pcszName) == 0)
713 {
714 found = true;
715 break;
716 }
717 if (RT_SUCCESS(rc) && found)
718 if ( (isGuest && (it->mFlags & RDONLYGUEST))
719 || (!isGuest && (it->mFlags & RDONLYHOST))
720 )
721 rc = VERR_PERMISSION_DENIED;
722
723 /*
724 * And delete the property if all is well.
725 */
726 if (RT_SUCCESS(rc) && found)
727 {
728 RTTIMESPEC time;
729 uint64_t u64Timestamp = RTTimeSpecGetNano(RTTimeNow(&time));
730 mProperties.erase(it);
731 // if (isGuest) /* Notify the host even for properties that the host
732 // * changed. Less efficient, but ensures consistency. */
733 doNotifications(pcszName, u64Timestamp);
734 }
735 LogFlowThisFunc(("rc = %Rrc\n", rc));
736 return rc;
737}
738
739/**
740 * Enumerate guest properties by mask, checking the validity
741 * of the arguments passed.
742 *
743 * @returns iprt status value
744 * @param cParms the number of HGCM parameters supplied
745 * @param paParms the array of HGCM parameters
746 * @thread HGCM
747 */
748int Service::enumProps(uint32_t cParms, VBOXHGCMSVCPARM paParms[])
749{
750 int rc = VINF_SUCCESS;
751
752 /*
753 * Get the HGCM function arguments.
754 */
755 char *pcchPatterns = NULL, *pchBuf = NULL;
756 uint32_t cchPatterns = 0, cchBuf = 0;
757 LogFlowThisFunc(("\n"));
758 if ( (cParms != 3) /* Hardcoded value as the next lines depend on it. */
759 || RT_FAILURE(paParms[0].getString(&pcchPatterns, &cchPatterns)) /* patterns */
760 || RT_FAILURE(paParms[1].getBuffer((void **) &pchBuf, &cchBuf)) /* return buffer */
761 )
762 rc = VERR_INVALID_PARAMETER;
763 if (RT_SUCCESS(rc) && cchPatterns > MAX_PATTERN_LEN)
764 rc = VERR_TOO_MUCH_DATA;
765
766 /*
767 * First repack the patterns into the format expected by RTStrSimplePatternMatch()
768 */
769 char pszPatterns[MAX_PATTERN_LEN];
770 for (unsigned i = 0; i < cchPatterns - 1; ++i)
771 if (pcchPatterns[i] != '\0')
772 pszPatterns[i] = pcchPatterns[i];
773 else
774 pszPatterns[i] = '|';
775 pszPatterns[cchPatterns - 1] = '\0';
776
777 /*
778 * Next enumerate into a temporary buffer. This can throw, but this is
779 * not a problem as we have nothing to roll back.
780 */
781 std::string buffer;
782 for (PropertyList::const_iterator it = mProperties.begin();
783 RT_SUCCESS(rc) && (it != mProperties.end()); ++it)
784 {
785 if (it->Matches(pszPatterns))
786 {
787 char szFlags[MAX_FLAGS_LEN];
788 char szTimestamp[256];
789 uint32_t cchTimestamp;
790 buffer += it->mName;
791 buffer += '\0';
792 buffer += it->mValue;
793 buffer += '\0';
794 cchTimestamp = RTStrFormatNumber(szTimestamp, it->mTimestamp,
795 10, 0, 0, 0);
796 buffer.append(szTimestamp, cchTimestamp);
797 buffer += '\0';
798 rc = writeFlags(it->mFlags, szFlags);
799 if (RT_SUCCESS(rc))
800 buffer += szFlags;
801 buffer += '\0';
802 }
803 }
804 buffer.append(4, '\0'); /* The final terminators */
805
806 /*
807 * Finally write out the temporary buffer to the real one if it is not too
808 * small.
809 */
810 if (RT_SUCCESS(rc))
811 {
812 paParms[2].setUInt32 ((uint32_t)buffer.size());
813 /* Copy the memory if it fits into the guest buffer */
814 if (buffer.size() <= cchBuf)
815 buffer.copy(pchBuf, cchBuf);
816 else
817 rc = VERR_BUFFER_OVERFLOW;
818 }
819 return rc;
820}
821
822/** Helper query used by getOldNotification */
823int Service::getOldNotificationInternal(const char *pszPatterns,
824 uint64_t u64Timestamp,
825 Property *pProp)
826{
827 int rc = VINF_SUCCESS;
828 bool warn = false;
829
830 /* We count backwards, as the guest should normally be querying the
831 * most recent events. */
832 PropertyList::reverse_iterator it = mGuestNotifications.rbegin();
833 for (; it->mTimestamp != u64Timestamp && it != mGuestNotifications.rend();
834 ++it) {}
835 /* Warn if the timestamp was not found. */
836 if (it->mTimestamp != u64Timestamp)
837 warn = true;
838 /* Now look for an event matching the patterns supplied. The base()
839 * member conveniently points to the following element. */
840 PropertyList::iterator base = it.base();
841 for (; !base->Matches(pszPatterns) && base != mGuestNotifications.end();
842 ++base) {}
843 if (RT_SUCCESS(rc) && base != mGuestNotifications.end())
844 *pProp = *base;
845 else if (RT_SUCCESS(rc))
846 *pProp = Property();
847 if (warn)
848 rc = VWRN_NOT_FOUND;
849 return rc;
850}
851
852/** Helper query used by getNotification */
853int Service::getNotificationWriteOut(VBOXHGCMSVCPARM paParms[], Property prop)
854{
855 int rc = VINF_SUCCESS;
856 /* Format the data to write to the buffer. */
857 std::string buffer;
858 uint64_t u64Timestamp;
859 char *pchBuf;
860 uint32_t cchBuf;
861 rc = paParms[2].getBuffer((void **) &pchBuf, &cchBuf);
862 if (RT_SUCCESS(rc))
863 {
864 char szFlags[MAX_FLAGS_LEN];
865 rc = writeFlags(prop.mFlags, szFlags);
866 if (RT_SUCCESS(rc))
867 {
868 buffer += prop.mName;
869 buffer += '\0';
870 buffer += prop.mValue;
871 buffer += '\0';
872 buffer += szFlags;
873 buffer += '\0';
874 u64Timestamp = prop.mTimestamp;
875 }
876 }
877 /* Write out the data. */
878 if (RT_SUCCESS(rc))
879 {
880 paParms[1].setUInt64(u64Timestamp);
881 paParms[3].setUInt32((uint32_t)buffer.size());
882 if (buffer.size() <= cchBuf)
883 buffer.copy(pchBuf, cchBuf);
884 else
885 rc = VERR_BUFFER_OVERFLOW;
886 }
887 return rc;
888}
889
890/**
891 * Get the next guest notification.
892 *
893 * @returns iprt status value
894 * @param cParms the number of HGCM parameters supplied
895 * @param paParms the array of HGCM parameters
896 * @thread HGCM
897 * @throws can throw std::bad_alloc
898 */
899int Service::getNotification(VBOXHGCMCALLHANDLE callHandle, uint32_t cParms,
900 VBOXHGCMSVCPARM paParms[])
901{
902 int rc = VINF_SUCCESS;
903 char *pszPatterns, *pchBuf;
904 uint32_t cchPatterns = 0, cchBuf = 0;
905 uint64_t u64Timestamp;
906
907 /*
908 * Get the HGCM function arguments and perform basic verification.
909 */
910 LogFlowThisFunc(("\n"));
911 if ( (cParms != 4) /* Hardcoded value as the next lines depend on it. */
912 || RT_FAILURE(paParms[0].getString(&pszPatterns, &cchPatterns)) /* patterns */
913 || RT_FAILURE(paParms[1].getUInt64(&u64Timestamp)) /* timestamp */
914 || RT_FAILURE(paParms[2].getBuffer((void **) &pchBuf, &cchBuf)) /* return buffer */
915 )
916 rc = VERR_INVALID_PARAMETER;
917 if (RT_SUCCESS(rc))
918 LogFlow((" pszPatterns=%s, u64Timestamp=%llu\n", pszPatterns,
919 u64Timestamp));
920
921 /*
922 * If no timestamp was supplied or no notification was found in the queue
923 * of old notifications, enqueue the request in the waiting queue.
924 */
925 Property prop;
926 if (RT_SUCCESS(rc) && u64Timestamp != 0)
927 rc = getOldNotification(pszPatterns, u64Timestamp, &prop);
928 if (RT_SUCCESS(rc) && prop.isNull())
929 {
930 mGuestWaiters.push_back(GuestCall(callHandle, GET_NOTIFICATION,
931 paParms, rc));
932 rc = VINF_HGCM_ASYNC_EXECUTE;
933 }
934 /*
935 * Otherwise reply at once with the enqueued notification we found.
936 */
937 else
938 {
939 int rc2 = getNotificationWriteOut(paParms, prop);
940 if (RT_FAILURE(rc2))
941 rc = rc2;
942 }
943 return rc;
944}
945
946/**
947 * Notify the service owner and the guest that a property has been
948 * added/deleted/changed
949 * @param pszProperty the name of the property which has changed
950 * @param u64Timestamp the time at which the change took place
951 * @note this call allocates memory which the reqNotify request is expected to
952 * free again, using RTStrFree().
953 *
954 * @thread HGCM service
955 */
956void Service::doNotifications(const char *pszProperty, uint64_t u64Timestamp)
957{
958 int rc = VINF_SUCCESS;
959
960 AssertPtrReturnVoid(pszProperty);
961 LogFlowThisFunc (("pszProperty=%s, u64Timestamp=%llu\n", pszProperty, u64Timestamp));
962 /* Ensure that our timestamp is different to the last one. */
963 if ( !mGuestNotifications.empty()
964 && u64Timestamp == mGuestNotifications.back().mTimestamp)
965 ++u64Timestamp;
966
967 /*
968 * Try to find the property. Create a change event if we find it and a
969 * delete event if we do not.
970 */
971 Property prop;
972 prop.mName = pszProperty;
973 prop.mTimestamp = u64Timestamp;
974 /* prop is currently a delete event for pszProperty */
975 bool found = false;
976 if (RT_SUCCESS(rc))
977 for (PropertyList::const_iterator it = mProperties.begin();
978 !found && it != mProperties.end(); ++it)
979 if (it->mName.compare(pszProperty) == 0)
980 {
981 found = true;
982 /* Make prop into a change event. */
983 prop.mValue = it->mValue;
984 prop.mFlags = it->mFlags;
985 }
986
987
988 /* Release waiters if applicable and add the event to the queue for
989 * guest notifications */
990 if (RT_SUCCESS(rc))
991 {
992 try
993 {
994 CallList::iterator it = mGuestWaiters.begin();
995 while (it != mGuestWaiters.end())
996 {
997 const char *pszPatterns;
998 uint32_t cchPatterns;
999 it->mParms[0].getString(&pszPatterns, &cchPatterns);
1000 if (prop.Matches(pszPatterns))
1001 {
1002 GuestCall call = *it;
1003 int rc2 = getNotificationWriteOut(call.mParms, prop);
1004 if (RT_SUCCESS(rc2))
1005 rc2 = call.mRc;
1006 mpHelpers->pfnCallComplete (call.mHandle, rc2);
1007 it = mGuestWaiters.erase(it);
1008 }
1009 else
1010 ++it;
1011 }
1012 mGuestNotifications.push_back(prop);
1013 }
1014 catch (std::bad_alloc)
1015 {
1016 rc = VERR_NO_MEMORY;
1017 }
1018 }
1019 if (mGuestNotifications.size() > MAX_GUEST_NOTIFICATIONS)
1020 mGuestNotifications.pop_front();
1021
1022#ifndef VBOX_GUEST_PROP_TEST_NOTHREAD
1023 /*
1024 * Host notifications - first case: if the property exists then send its
1025 * current value
1026 */
1027 char *pszName = NULL, *pszValue = NULL, *pszFlags = NULL;
1028
1029 if (found && mpfnHostCallback != NULL)
1030 {
1031 char szFlags[MAX_FLAGS_LEN];
1032 /* Send out a host notification */
1033 rc = writeFlags(prop.mFlags, szFlags);
1034 if (RT_SUCCESS(rc))
1035 rc = RTStrDupEx(&pszName, pszProperty);
1036 if (RT_SUCCESS(rc))
1037 rc = RTStrDupEx(&pszValue, prop.mValue.c_str());
1038 if (RT_SUCCESS(rc))
1039 rc = RTStrDupEx(&pszFlags, szFlags);
1040 if (RT_SUCCESS(rc))
1041 {
1042 LogFlowThisFunc (("pszName=%p (%s)\n", pszName, pszName));
1043 LogFlowThisFunc (("pszValue=%p (%s)\n", pszValue, pszValue));
1044 LogFlowThisFunc (("pszFlags=%p (%s)\n", pszFlags, pszFlags));
1045 rc = RTReqCallEx(mReqQueue, NULL, 0, RTREQFLAGS_NO_WAIT,
1046 (PFNRT)Service::reqNotify, 7, mpfnHostCallback,
1047 mpvHostData, pszName, pszValue,
1048 (uint32_t) RT_HIDWORD(u64Timestamp),
1049 (uint32_t) RT_LODWORD(u64Timestamp), pszFlags);
1050 }
1051 }
1052
1053 /*
1054 * Host notifications - second case: if the property does not exist then
1055 * send the host an empty value
1056 */
1057 if (!found && mpfnHostCallback != NULL)
1058 {
1059 /* Send out a host notification */
1060 rc = RTStrDupEx(&pszName, pszProperty);
1061 if (RT_SUCCESS(rc))
1062 {
1063 LogFlowThisFunc (("pszName=%p (%s)\n", pszName, pszName));
1064 rc = RTReqCallEx(mReqQueue, NULL, 0, RTREQFLAGS_NO_WAIT,
1065 (PFNRT)Service::reqNotify, 7, mpfnHostCallback,
1066 mpvHostData, pszName, (uintptr_t) NULL,
1067 (uint32_t) RT_HIDWORD(u64Timestamp),
1068 (uint32_t) RT_LODWORD(u64Timestamp),
1069 (uintptr_t) NULL);
1070 }
1071 }
1072 if (RT_FAILURE(rc)) /* clean up if we failed somewhere */
1073 {
1074 LogThisFunc (("Failed, freeing allocated strings.\n"));
1075 RTStrFree(pszName);
1076 RTStrFree(pszValue);
1077 RTStrFree(pszFlags);
1078 }
1079 LogFlowThisFunc (("returning\n"));
1080#endif /* VBOX_GUEST_PROP_TEST_NOTHREAD not defined */
1081}
1082
1083/**
1084 * Notify the service owner that a property has been added/deleted/changed.
1085 * asynchronous part.
1086 * @param pszProperty the name of the property which has changed
1087 * @note this call allocates memory which the reqNotify request is expected to
1088 * free again, using RTStrFree().
1089 *
1090 * @thread request thread
1091 */
1092/* static */
1093int Service::reqNotify(PFNHGCMSVCEXT pfnCallback, void *pvData,
1094 char *pszName, char *pszValue, uint32_t u32TimeHigh,
1095 uint32_t u32TimeLow, char *pszFlags)
1096{
1097 LogFlowFunc (("pfnCallback=%p, pvData=%p, pszName=%p, pszValue=%p, u32TimeHigh=%u, u32TimeLow=%u, pszFlags=%p\n", pfnCallback, pvData, pszName, pszValue, u32TimeHigh, u32TimeLow, pszFlags));
1098 LogFlowFunc (("pszName=%s\n", pszName));
1099 LogFlowFunc (("pszValue=%s\n", pszValue));
1100 LogFlowFunc (("pszFlags=%s\n", pszFlags));
1101 /* LogFlowFunc (("pfnCallback=%p, pvData=%p, pszName=%s, pszValue=%s, u32TimeHigh=%u, u32TimeLow=%u, pszFlags=%s\n", pfnCallback, pvData, pszName, pszValue, u32TimeHigh, u32TimeLow, pszFlags)); */
1102 HOSTCALLBACKDATA HostCallbackData;
1103 HostCallbackData.u32Magic = HOSTCALLBACKMAGIC;
1104 HostCallbackData.pcszName = pszName;
1105 HostCallbackData.pcszValue = pszValue;
1106 HostCallbackData.u64Timestamp = RT_MAKE_U64(u32TimeLow, u32TimeHigh);
1107 HostCallbackData.pcszFlags = pszFlags;
1108 int rc = pfnCallback(pvData, 0, (void *)(&HostCallbackData),
1109 sizeof(HostCallbackData));
1110 AssertRC(rc);
1111 LogFlowFunc (("Freeing strings\n"));
1112 RTStrFree(pszName);
1113 RTStrFree(pszValue);
1114 RTStrFree(pszFlags);
1115 LogFlowFunc (("returning success\n"));
1116 return VINF_SUCCESS;
1117}
1118
1119
1120/**
1121 * Handle an HGCM service call.
1122 * @copydoc VBOXHGCMSVCFNTABLE::pfnCall
1123 * @note All functions which do not involve an unreasonable delay will be
1124 * handled synchronously. If needed, we will add a request handler
1125 * thread in future for those which do.
1126 *
1127 * @thread HGCM
1128 */
1129void Service::call (VBOXHGCMCALLHANDLE callHandle, uint32_t u32ClientID,
1130 void * /* pvClient */, uint32_t eFunction, uint32_t cParms,
1131 VBOXHGCMSVCPARM paParms[])
1132{
1133 int rc = VINF_SUCCESS;
1134 LogFlowFunc(("u32ClientID = %d, fn = %d, cParms = %d, pparms = %d\n",
1135 u32ClientID, eFunction, cParms, paParms));
1136
1137 try
1138 {
1139 switch (eFunction)
1140 {
1141 /* The guest wishes to read a property */
1142 case GET_PROP:
1143 LogFlowFunc(("GET_PROP\n"));
1144 rc = getProperty(cParms, paParms);
1145 break;
1146
1147 /* The guest wishes to set a property */
1148 case SET_PROP:
1149 LogFlowFunc(("SET_PROP\n"));
1150 rc = setProperty(cParms, paParms, true);
1151 break;
1152
1153 /* The guest wishes to set a property value */
1154 case SET_PROP_VALUE:
1155 LogFlowFunc(("SET_PROP_VALUE\n"));
1156 rc = setProperty(cParms, paParms, true);
1157 break;
1158
1159 /* The guest wishes to remove a configuration value */
1160 case DEL_PROP:
1161 LogFlowFunc(("DEL_PROP\n"));
1162 rc = delProperty(cParms, paParms, true);
1163 break;
1164
1165 /* The guest wishes to enumerate all properties */
1166 case ENUM_PROPS:
1167 LogFlowFunc(("ENUM_PROPS\n"));
1168 rc = enumProps(cParms, paParms);
1169 break;
1170
1171 /* The guest wishes to get the next property notification */
1172 case GET_NOTIFICATION:
1173 LogFlowFunc(("GET_NOTIFICATION\n"));
1174 rc = getNotification(callHandle, cParms, paParms);
1175 break;
1176
1177 default:
1178 rc = VERR_NOT_IMPLEMENTED;
1179 }
1180 }
1181 catch (std::bad_alloc)
1182 {
1183 rc = VERR_NO_MEMORY;
1184 }
1185 LogFlowFunc(("rc = %Rrc\n", rc));
1186 if (rc != VINF_HGCM_ASYNC_EXECUTE)
1187 {
1188 mpHelpers->pfnCallComplete (callHandle, rc);
1189 }
1190}
1191
1192
1193/**
1194 * Service call handler for the host.
1195 * @copydoc VBOXHGCMSVCFNTABLE::pfnHostCall
1196 * @thread hgcm
1197 */
1198int Service::hostCall (uint32_t eFunction, uint32_t cParms, VBOXHGCMSVCPARM paParms[])
1199{
1200 int rc = VINF_SUCCESS;
1201
1202 LogFlowFunc(("fn = %d, cParms = %d, pparms = %d\n",
1203 eFunction, cParms, paParms));
1204
1205 try
1206 {
1207 switch (eFunction)
1208 {
1209 /* The host wishes to set a block of properties */
1210 case SET_PROPS_HOST:
1211 LogFlowFunc(("SET_PROPS_HOST\n"));
1212 rc = setPropertyBlock(cParms, paParms);
1213 break;
1214
1215 /* The host wishes to read a configuration value */
1216 case GET_PROP_HOST:
1217 LogFlowFunc(("GET_PROP_HOST\n"));
1218 rc = getProperty(cParms, paParms);
1219 break;
1220
1221 /* The host wishes to set a configuration value */
1222 case SET_PROP_HOST:
1223 LogFlowFunc(("SET_PROP_HOST\n"));
1224 rc = setProperty(cParms, paParms, false);
1225 break;
1226
1227 /* The host wishes to set a configuration value */
1228 case SET_PROP_VALUE_HOST:
1229 LogFlowFunc(("SET_PROP_VALUE_HOST\n"));
1230 rc = setProperty(cParms, paParms, false);
1231 break;
1232
1233 /* The host wishes to remove a configuration value */
1234 case DEL_PROP_HOST:
1235 LogFlowFunc(("DEL_PROP_HOST\n"));
1236 rc = delProperty(cParms, paParms, false);
1237 break;
1238
1239 /* The host wishes to enumerate all properties */
1240 case ENUM_PROPS_HOST:
1241 LogFlowFunc(("ENUM_PROPS\n"));
1242 rc = enumProps(cParms, paParms);
1243 break;
1244
1245 default:
1246 rc = VERR_NOT_SUPPORTED;
1247 break;
1248 }
1249 }
1250 catch (std::bad_alloc)
1251 {
1252 rc = VERR_NO_MEMORY;
1253 }
1254
1255 LogFlowFunc(("rc = %Rrc\n", rc));
1256 return rc;
1257}
1258
1259int Service::uninit()
1260{
1261 int rc = VINF_SUCCESS;
1262 unsigned count = 0;
1263
1264 mfExitThread = true;
1265#ifndef VBOX_GUEST_PROP_TEST_NOTHREAD
1266 rc = RTReqCallEx(mReqQueue, NULL, 0, RTREQFLAGS_NO_WAIT, (PFNRT)reqVoid, 0);
1267 if (RT_SUCCESS(rc))
1268 do
1269 {
1270 rc = RTThreadWait(mReqThread, 1000, NULL);
1271 ++count;
1272 Assert(RT_SUCCESS(rc) || ((VERR_TIMEOUT == rc) && (count != 5)));
1273 } while ((VERR_TIMEOUT == rc) && (count < 300));
1274#endif /* VBOX_GUEST_PROP_TEST_NOTHREAD not defined */
1275 if (RT_SUCCESS(rc))
1276 RTReqDestroyQueue(mReqQueue);
1277 return rc;
1278}
1279
1280} /* namespace guestProp */
1281
1282using guestProp::Service;
1283
1284/**
1285 * @copydoc VBOXHGCMSVCLOAD
1286 */
1287extern "C" DECLCALLBACK(DECLEXPORT(int)) VBoxHGCMSvcLoad (VBOXHGCMSVCFNTABLE *ptable)
1288{
1289 int rc = VINF_SUCCESS;
1290
1291 LogFlowFunc(("ptable = %p\n", ptable));
1292
1293 if (!VALID_PTR(ptable))
1294 {
1295 rc = VERR_INVALID_PARAMETER;
1296 }
1297 else
1298 {
1299 LogFlowFunc(("ptable->cbSize = %d, ptable->u32Version = 0x%08X\n", ptable->cbSize, ptable->u32Version));
1300
1301 if ( ptable->cbSize != sizeof (VBOXHGCMSVCFNTABLE)
1302 || ptable->u32Version != VBOX_HGCM_SVC_VERSION)
1303 {
1304 rc = VERR_VERSION_MISMATCH;
1305 }
1306 else
1307 {
1308 std::auto_ptr<Service> apService;
1309 /* No exceptions may propogate outside. */
1310 try {
1311 apService = std::auto_ptr<Service>(new Service(ptable->pHelpers));
1312 } catch (int rcThrown) {
1313 rc = rcThrown;
1314 } catch (...) {
1315 rc = VERR_UNRESOLVED_ERROR;
1316 }
1317
1318 if (RT_SUCCESS(rc))
1319 {
1320 /* We do not maintain connections, so no client data is needed. */
1321 ptable->cbClient = 0;
1322
1323 ptable->pfnUnload = Service::svcUnload;
1324 ptable->pfnConnect = Service::svcConnectDisconnect;
1325 ptable->pfnDisconnect = Service::svcConnectDisconnect;
1326 ptable->pfnCall = Service::svcCall;
1327 ptable->pfnHostCall = Service::svcHostCall;
1328 ptable->pfnSaveState = NULL; /* The service is stateless, so the normal */
1329 ptable->pfnLoadState = NULL; /* construction done before restoring suffices */
1330 ptable->pfnRegisterExtension = Service::svcRegisterExtension;
1331
1332 /* Service specific initialization. */
1333 ptable->pvService = apService.release();
1334 }
1335 }
1336 }
1337
1338 LogFlowFunc(("returning %Rrc\n", rc));
1339 return rc;
1340}
1341
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