VirtualBox

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

Last change on this file since 21019 was 20894, checked in by vboxsync, 16 years ago

HostServices/GuestProperties: fixed a bad debug assertion

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 46.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 * Checking that the name passed by the guest fits our criteria for a
363 * property name.
364 *
365 * @returns IPRT status code
366 * @param pszName the name passed by the guest
367 * @param cbName the number of bytes pszName points to, including the
368 * terminating '\0'
369 * @thread HGCM
370 */
371int Service::validateName(const char *pszName, uint32_t cbName)
372{
373 LogFlowFunc(("cbName=%d\n", cbName));
374
375 /*
376 * Validate the name, checking that it's proper UTF-8 and has
377 * a string terminator.
378 */
379 int rc = VINF_SUCCESS;
380 if (RT_SUCCESS(rc) && (cbName < 2))
381 rc = VERR_INVALID_PARAMETER;
382 if (RT_SUCCESS(rc))
383 rc = RTStrValidateEncodingEx(pszName, RT_MIN(cbName, (uint32_t) MAX_NAME_LEN),
384 RTSTR_VALIDATE_ENCODING_ZERO_TERMINATED);
385 for (unsigned i = 0; RT_SUCCESS(rc) && i < cbName; ++i)
386 if (pszName[i] == '*' || pszName[i] == '?' || pszName[i] == '|')
387 rc = VERR_INVALID_PARAMETER;
388 LogFlowFunc(("returning %Rrc\n", rc));
389 return rc;
390}
391
392
393/**
394 * Check that the data passed by the guest fits our criteria for the value of
395 * a guest property.
396 *
397 * @returns IPRT status code
398 * @param pszValue the value to store in the property
399 * @param cbValue the number of bytes in the buffer pszValue points to
400 * @thread HGCM
401 */
402int Service::validateValue(const char *pszValue, uint32_t cbValue)
403{
404 LogFlowFunc(("cbValue=%d\n", cbValue));
405
406 /*
407 * Validate the value, checking that it's proper UTF-8 and has
408 * a string terminator.
409 */
410 int rc = VINF_SUCCESS;
411 if (RT_SUCCESS(rc) && cbValue == 0)
412 rc = VERR_INVALID_PARAMETER;
413 if (RT_SUCCESS(rc))
414 rc = RTStrValidateEncodingEx(pszValue, RT_MIN(cbValue, (uint32_t) MAX_VALUE_LEN),
415 RTSTR_VALIDATE_ENCODING_ZERO_TERMINATED);
416 if (RT_SUCCESS(rc))
417 LogFlow((" pszValue=%s\n", cbValue > 0 ? pszValue : NULL));
418 LogFlowFunc(("returning %Rrc\n", rc));
419 return rc;
420}
421
422/**
423 * Set a block of properties in the property registry, checking the validity
424 * of the arguments passed.
425 *
426 * @returns iprt status value
427 * @param cParms the number of HGCM parameters supplied
428 * @param paParms the array of HGCM parameters
429 * @thread HGCM
430 */
431int Service::setPropertyBlock(uint32_t cParms, VBOXHGCMSVCPARM paParms[])
432{
433 char **ppNames, **ppValues, **ppFlags;
434 uint64_t *pTimestamps;
435 uint32_t cbDummy;
436 int rc = VINF_SUCCESS;
437
438 /*
439 * Get and validate the parameters
440 */
441 if ( (cParms != 4)
442 || RT_FAILURE(paParms[0].getPointer ((void **) &ppNames, &cbDummy))
443 || RT_FAILURE(paParms[1].getPointer ((void **) &ppValues, &cbDummy))
444 || RT_FAILURE(paParms[2].getPointer ((void **) &pTimestamps, &cbDummy))
445 || RT_FAILURE(paParms[3].getPointer ((void **) &ppFlags, &cbDummy))
446 )
447 rc = VERR_INVALID_PARAMETER;
448
449 /*
450 * Add the properties to the end of the list. If we succeed then we
451 * will remove duplicates afterwards.
452 */
453 /* Remember the last property before we started adding, for rollback or
454 * cleanup. */
455 PropertyList::iterator itEnd = mProperties.end();
456 if (!mProperties.empty())
457 --itEnd;
458 try
459 {
460 for (unsigned i = 0; RT_SUCCESS(rc) && ppNames[i] != NULL; ++i)
461 {
462 uint32_t fFlags;
463 if ( !VALID_PTR(ppNames[i])
464 || !VALID_PTR(ppValues[i])
465 || !VALID_PTR(ppFlags[i])
466 )
467 rc = VERR_INVALID_POINTER;
468 if (RT_SUCCESS(rc))
469 rc = validateFlags(ppFlags[i], &fFlags);
470 if (RT_SUCCESS(rc))
471 mProperties.push_back(Property(ppNames[i], ppValues[i],
472 pTimestamps[i], fFlags));
473 }
474 }
475 catch (std::bad_alloc)
476 {
477 rc = VERR_NO_MEMORY;
478 }
479
480 /*
481 * If all went well then remove the duplicate elements.
482 */
483 if (RT_SUCCESS(rc) && itEnd != mProperties.end())
484 {
485 ++itEnd;
486 for (unsigned i = 0; ppNames[i] != NULL; ++i)
487 {
488 bool found = false;
489 for (PropertyList::iterator it = mProperties.begin();
490 !found && it != itEnd; ++it)
491 if (it->mName.compare(ppNames[i]) == 0)
492 {
493 found = true;
494 mProperties.erase(it);
495 }
496 }
497 }
498
499 /*
500 * If something went wrong then rollback. This is possible because we
501 * haven't deleted anything yet.
502 */
503 if (RT_FAILURE(rc))
504 {
505 if (itEnd != mProperties.end())
506 ++itEnd;
507 mProperties.erase(itEnd, mProperties.end());
508 }
509 return rc;
510}
511
512/**
513 * Retrieve a value from the property registry by name, checking the validity
514 * of the arguments passed. If the guest has not allocated enough buffer
515 * space for the value then we return VERR_OVERFLOW and set the size of the
516 * buffer needed in the "size" HGCM parameter. If the name was not found at
517 * all, we return VERR_NOT_FOUND.
518 *
519 * @returns iprt status value
520 * @param cParms the number of HGCM parameters supplied
521 * @param paParms the array of HGCM parameters
522 * @thread HGCM
523 */
524int Service::getProperty(uint32_t cParms, VBOXHGCMSVCPARM paParms[])
525{
526 int rc = VINF_SUCCESS;
527 const char *pcszName;
528 char *pchBuf;
529 uint32_t cchName, cchBuf;
530 char szFlags[MAX_FLAGS_LEN];
531
532 /*
533 * Get and validate the parameters
534 */
535 LogFlowThisFunc(("\n"));
536 if ( cParms != 4 /* Hardcoded value as the next lines depend on it. */
537 || RT_FAILURE (paParms[0].getPointer ((const void **) &pcszName, &cchName)) /* name */
538 || RT_FAILURE (paParms[1].getPointer ((void **) &pchBuf, &cchBuf)) /* buffer */
539 )
540 rc = VERR_INVALID_PARAMETER;
541 else
542 rc = validateName(pcszName, cchName);
543
544 /*
545 * Read and set the values we will return
546 */
547
548 /* Get the value size */
549 PropertyList::const_iterator it;
550 if (RT_SUCCESS(rc))
551 {
552 rc = VERR_NOT_FOUND;
553 for (it = mProperties.begin(); it != mProperties.end(); ++it)
554 if (it->mName.compare(pcszName) == 0)
555 {
556 rc = VINF_SUCCESS;
557 break;
558 }
559 }
560 if (RT_SUCCESS(rc))
561 rc = writeFlags(it->mFlags, szFlags);
562 if (RT_SUCCESS(rc))
563 {
564 /* Check that the buffer is big enough */
565 size_t cchBufActual = it->mValue.size() + 1 + strlen(szFlags);
566 paParms[3].setUInt32 ((uint32_t)cchBufActual);
567 if (cchBufActual <= cchBuf)
568 {
569 /* Write the value, flags and timestamp */
570 it->mValue.copy(pchBuf, cchBuf, 0);
571 pchBuf[it->mValue.size()] = '\0'; /* Terminate the value */
572 strcpy(pchBuf + it->mValue.size() + 1, szFlags);
573 paParms[2].setUInt64 (it->mTimestamp);
574
575 /*
576 * Done! Do exit logging and return.
577 */
578 Log2(("Queried string %s, value=%s, timestamp=%lld, flags=%s\n",
579 pcszName, it->mValue.c_str(), it->mTimestamp, szFlags));
580 }
581 else
582 rc = VERR_BUFFER_OVERFLOW;
583 }
584
585 LogFlowThisFunc(("rc = %Rrc\n", rc));
586 return rc;
587}
588
589/**
590 * Set a value in the property registry by name, checking the validity
591 * of the arguments passed.
592 *
593 * @returns iprt status value
594 * @param cParms the number of HGCM parameters supplied
595 * @param paParms the array of HGCM parameters
596 * @param isGuest is this call coming from the guest (or the host)?
597 * @throws std::bad_alloc if an out of memory condition occurs
598 * @thread HGCM
599 */
600int Service::setProperty(uint32_t cParms, VBOXHGCMSVCPARM paParms[], bool isGuest)
601{
602 int rc = VINF_SUCCESS;
603 const char *pcszName, *pcszValue, *pcszFlags = NULL;
604 uint32_t cchName, cchValue, cchFlags = 0;
605 uint32_t fFlags = NILFLAG;
606 RTTIMESPEC time;
607 uint64_t u64TimeNano = RTTimeSpecGetNano(RTTimeNow(&time));
608
609 LogFlowThisFunc(("\n"));
610 /*
611 * First of all, make sure that we won't exceed the maximum number of properties.
612 */
613 if (mProperties.size() >= MAX_PROPS)
614 rc = VERR_TOO_MUCH_DATA;
615
616 /*
617 * General parameter correctness checking.
618 */
619 if ( RT_SUCCESS(rc)
620 && ( (cParms < 2) || (cParms > 3) /* Hardcoded value as the next lines depend on it. */
621 || RT_FAILURE(paParms[0].getPointer ((const void **) &pcszName,
622 &cchName)) /* name */
623 || RT_FAILURE(paParms[1].getPointer ((const void **) &pcszValue,
624 &cchValue)) /* value */
625 || ( (3 == cParms)
626 && RT_FAILURE(paParms[2].getPointer ((const void **) &pcszFlags,
627 &cchFlags)) /* flags */
628 )
629 )
630 )
631 rc = VERR_INVALID_PARAMETER;
632
633 /*
634 * Check the values passed in the parameters for correctness.
635 */
636 if (RT_SUCCESS(rc))
637 rc = validateName(pcszName, cchName);
638 if (RT_SUCCESS(rc))
639 rc = validateValue(pcszValue, cchValue);
640 if ((3 == cParms) && RT_SUCCESS(rc))
641 rc = RTStrValidateEncodingEx(pcszFlags, cchFlags,
642 RTSTR_VALIDATE_ENCODING_ZERO_TERMINATED);
643 if ((3 == cParms) && RT_SUCCESS(rc))
644 rc = validateFlags(pcszFlags, &fFlags);
645
646 /*
647 * If the property already exists, check its flags to see if we are allowed
648 * to change it.
649 */
650 PropertyList::iterator it;
651 bool found = false;
652 if (RT_SUCCESS(rc))
653 for (it = mProperties.begin(); it != mProperties.end(); ++it)
654 if (it->mName.compare(pcszName) == 0)
655 {
656 found = true;
657 break;
658 }
659 if (RT_SUCCESS(rc) && found)
660 if ( (isGuest && (it->mFlags & RDONLYGUEST))
661 || (!isGuest && (it->mFlags & RDONLYHOST))
662 )
663 rc = VERR_PERMISSION_DENIED;
664
665 /*
666 * Set the actual value
667 */
668 if (RT_SUCCESS(rc))
669 {
670 if (found)
671 {
672 it->mValue = pcszValue;
673 it->mTimestamp = u64TimeNano;
674 it->mFlags = fFlags;
675 }
676 else /* This can throw. No problem as we have nothing to roll back. */
677 mProperties.push_back(Property(pcszName, pcszValue, u64TimeNano, fFlags));
678 }
679
680 /*
681 * Send a notification to the host and return.
682 */
683 if (RT_SUCCESS(rc))
684 {
685 // if (isGuest) /* Notify the host even for properties that the host
686 // * changed. Less efficient, but ensures consistency. */
687 doNotifications(pcszName, u64TimeNano);
688 Log2(("Set string %s, rc=%Rrc, value=%s\n", pcszName, rc, pcszValue));
689 }
690 LogFlowThisFunc(("rc = %Rrc\n", rc));
691 return rc;
692}
693
694
695/**
696 * Remove 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 * @thread HGCM
704 */
705int Service::delProperty(uint32_t cParms, VBOXHGCMSVCPARM paParms[], bool isGuest)
706{
707 int rc = VINF_SUCCESS;
708 const char *pcszName;
709 uint32_t cbName;
710
711 LogFlowThisFunc(("\n"));
712
713 /*
714 * Check the user-supplied parameters.
715 */
716 if ( (cParms != 1) /* Hardcoded value as the next lines depend on it. */
717 || RT_FAILURE(paParms[0].getPointer ((const void **) &pcszName,
718 &cbName)) /* name */
719 )
720 rc = VERR_INVALID_PARAMETER;
721 if (RT_SUCCESS(rc))
722 rc = validateName(pcszName, cbName);
723
724 /*
725 * If the property exists, check its flags to see if we are allowed
726 * to change it.
727 */
728 PropertyList::iterator it;
729 bool found = false;
730 if (RT_SUCCESS(rc))
731 for (it = mProperties.begin(); it != mProperties.end(); ++it)
732 if (it->mName.compare(pcszName) == 0)
733 {
734 found = true;
735 break;
736 }
737 if (RT_SUCCESS(rc) && found)
738 if ( (isGuest && (it->mFlags & RDONLYGUEST))
739 || (!isGuest && (it->mFlags & RDONLYHOST))
740 )
741 rc = VERR_PERMISSION_DENIED;
742
743 /*
744 * And delete the property if all is well.
745 */
746 if (RT_SUCCESS(rc) && found)
747 {
748 RTTIMESPEC time;
749 uint64_t u64Timestamp = RTTimeSpecGetNano(RTTimeNow(&time));
750 mProperties.erase(it);
751 // if (isGuest) /* Notify the host even for properties that the host
752 // * changed. Less efficient, but ensures consistency. */
753 doNotifications(pcszName, u64Timestamp);
754 }
755 LogFlowThisFunc(("rc = %Rrc\n", rc));
756 return rc;
757}
758
759/**
760 * Enumerate guest properties by mask, checking the validity
761 * of the arguments passed.
762 *
763 * @returns iprt status value
764 * @param cParms the number of HGCM parameters supplied
765 * @param paParms the array of HGCM parameters
766 * @thread HGCM
767 */
768int Service::enumProps(uint32_t cParms, VBOXHGCMSVCPARM paParms[])
769{
770 int rc = VINF_SUCCESS;
771
772 /*
773 * Get the HGCM function arguments.
774 */
775 char *pcchPatterns = NULL, *pchBuf = NULL;
776 uint32_t cchPatterns = 0, cchBuf = 0;
777 LogFlowThisFunc(("\n"));
778 if ( (cParms != 3) /* Hardcoded value as the next lines depend on it. */
779 || RT_FAILURE(paParms[0].getPointer ((const void **) &pcchPatterns,
780 &cchPatterns)) /* patterns */
781 || RT_FAILURE(paParms[1].getPointer ((void **) &pchBuf, &cchBuf)) /* return buffer */
782 )
783 rc = VERR_INVALID_PARAMETER;
784 if (RT_SUCCESS(rc) && cchPatterns > MAX_PATTERN_LEN)
785 rc = VERR_TOO_MUCH_DATA;
786
787 /*
788 * First repack the patterns into the format expected by RTStrSimplePatternMatch()
789 */
790 char pszPatterns[MAX_PATTERN_LEN];
791 if (NULL == pcchPatterns)
792 pszPatterns[0] = '\0';
793 else
794 {
795 for (unsigned i = 0; i < cchPatterns - 1; ++i)
796 if (pcchPatterns[i] != '\0')
797 pszPatterns[i] = pcchPatterns[i];
798 else
799 pszPatterns[i] = '|';
800 pszPatterns[cchPatterns - 1] = '\0';
801 }
802
803 /*
804 * Next enumerate into a temporary buffer. This can throw, but this is
805 * not a problem as we have nothing to roll back.
806 */
807 std::string buffer;
808 for (PropertyList::const_iterator it = mProperties.begin();
809 RT_SUCCESS(rc) && (it != mProperties.end()); ++it)
810 {
811 if (it->Matches(pszPatterns))
812 {
813 char szFlags[MAX_FLAGS_LEN];
814 char szTimestamp[256];
815 uint32_t cchTimestamp;
816 buffer += it->mName;
817 buffer += '\0';
818 buffer += it->mValue;
819 buffer += '\0';
820 cchTimestamp = RTStrFormatNumber(szTimestamp, it->mTimestamp,
821 10, 0, 0, 0);
822 buffer.append(szTimestamp, cchTimestamp);
823 buffer += '\0';
824 rc = writeFlags(it->mFlags, szFlags);
825 if (RT_SUCCESS(rc))
826 buffer += szFlags;
827 buffer += '\0';
828 }
829 }
830 buffer.append(4, '\0'); /* The final terminators */
831
832 /*
833 * Finally write out the temporary buffer to the real one if it is not too
834 * small.
835 */
836 if (RT_SUCCESS(rc))
837 {
838 paParms[2].setUInt32 ((uint32_t)buffer.size());
839 /* Copy the memory if it fits into the guest buffer */
840 if (buffer.size() <= cchBuf)
841 buffer.copy(pchBuf, cchBuf);
842 else
843 rc = VERR_BUFFER_OVERFLOW;
844 }
845 return rc;
846}
847
848/** Helper query used by getOldNotification */
849int Service::getOldNotificationInternal(const char *pszPatterns,
850 uint64_t u64Timestamp,
851 Property *pProp)
852{
853 int rc = VINF_SUCCESS;
854 bool warn = false;
855
856 /* We count backwards, as the guest should normally be querying the
857 * most recent events. */
858 PropertyList::reverse_iterator it = mGuestNotifications.rbegin();
859 for (; it->mTimestamp != u64Timestamp && it != mGuestNotifications.rend();
860 ++it) {}
861 /* Warn if the timestamp was not found. */
862 if (it->mTimestamp != u64Timestamp)
863 warn = true;
864 /* Now look for an event matching the patterns supplied. The base()
865 * member conveniently points to the following element. */
866 PropertyList::iterator base = it.base();
867 for (; !base->Matches(pszPatterns) && base != mGuestNotifications.end();
868 ++base) {}
869 if (RT_SUCCESS(rc) && base != mGuestNotifications.end())
870 *pProp = *base;
871 else if (RT_SUCCESS(rc))
872 *pProp = Property();
873 if (warn)
874 rc = VWRN_NOT_FOUND;
875 return rc;
876}
877
878/** Helper query used by getNotification */
879int Service::getNotificationWriteOut(VBOXHGCMSVCPARM paParms[], Property prop)
880{
881 int rc = VINF_SUCCESS;
882 /* Format the data to write to the buffer. */
883 std::string buffer;
884 uint64_t u64Timestamp;
885 char *pchBuf;
886 uint32_t cchBuf;
887 rc = paParms[2].getPointer((void **) &pchBuf, &cchBuf);
888 if (RT_SUCCESS(rc))
889 {
890 char szFlags[MAX_FLAGS_LEN];
891 rc = writeFlags(prop.mFlags, szFlags);
892 if (RT_SUCCESS(rc))
893 {
894 buffer += prop.mName;
895 buffer += '\0';
896 buffer += prop.mValue;
897 buffer += '\0';
898 buffer += szFlags;
899 buffer += '\0';
900 u64Timestamp = prop.mTimestamp;
901 }
902 }
903 /* Write out the data. */
904 if (RT_SUCCESS(rc))
905 {
906 paParms[1].setUInt64(u64Timestamp);
907 paParms[3].setUInt32((uint32_t)buffer.size());
908 if (buffer.size() <= cchBuf)
909 buffer.copy(pchBuf, cchBuf);
910 else
911 rc = VERR_BUFFER_OVERFLOW;
912 }
913 return rc;
914}
915
916/**
917 * Get the next guest notification.
918 *
919 * @returns iprt status value
920 * @param cParms the number of HGCM parameters supplied
921 * @param paParms the array of HGCM parameters
922 * @thread HGCM
923 * @throws can throw std::bad_alloc
924 */
925int Service::getNotification(VBOXHGCMCALLHANDLE callHandle, uint32_t cParms,
926 VBOXHGCMSVCPARM paParms[])
927{
928 int rc = VINF_SUCCESS;
929 char *pszPatterns, *pchBuf;
930 uint32_t cchPatterns = 0, cchBuf = 0;
931 uint64_t u64Timestamp;
932
933 /*
934 * Get the HGCM function arguments and perform basic verification.
935 */
936 LogFlowThisFunc(("\n"));
937 if ( (cParms != 4) /* Hardcoded value as the next lines depend on it. */
938 || RT_FAILURE(paParms[0].getPointer ((void **) &pszPatterns, &cchPatterns)) /* patterns */
939 || pszPatterns[cchPatterns - 1] != '\0' /* The patterns string must be zero-terminated */
940 || RT_FAILURE(paParms[1].getUInt64 (&u64Timestamp)) /* timestamp */
941 || RT_FAILURE(paParms[2].getPointer ((void **) &pchBuf, &cchBuf)) /* return buffer */
942 || cchBuf < 1
943 )
944 rc = VERR_INVALID_PARAMETER;
945 if (RT_SUCCESS(rc))
946 LogFlow((" pszPatterns=%s, u64Timestamp=%llu\n", pszPatterns,
947 u64Timestamp));
948
949 /*
950 * If no timestamp was supplied or no notification was found in the queue
951 * of old notifications, enqueue the request in the waiting queue.
952 */
953 Property prop;
954 if (RT_SUCCESS(rc) && u64Timestamp != 0)
955 rc = getOldNotification(pszPatterns, u64Timestamp, &prop);
956 if (RT_SUCCESS(rc) && prop.isNull())
957 {
958 mGuestWaiters.push_back(GuestCall(callHandle, GET_NOTIFICATION,
959 paParms, rc));
960 rc = VINF_HGCM_ASYNC_EXECUTE;
961 }
962 /*
963 * Otherwise reply at once with the enqueued notification we found.
964 */
965 else
966 {
967 int rc2 = getNotificationWriteOut(paParms, prop);
968 if (RT_FAILURE(rc2))
969 rc = rc2;
970 }
971 return rc;
972}
973
974/**
975 * Notify the service owner and the guest that a property has been
976 * added/deleted/changed
977 * @param pszProperty the name of the property which has changed
978 * @param u64Timestamp the time at which the change took place
979 * @note this call allocates memory which the reqNotify request is expected to
980 * free again, using RTStrFree().
981 *
982 * @thread HGCM service
983 */
984void Service::doNotifications(const char *pszProperty, uint64_t u64Timestamp)
985{
986 int rc = VINF_SUCCESS;
987
988 AssertPtrReturnVoid(pszProperty);
989 LogFlowThisFunc (("pszProperty=%s, u64Timestamp=%llu\n", pszProperty, u64Timestamp));
990 /* Ensure that our timestamp is different to the last one. */
991 if ( !mGuestNotifications.empty()
992 && u64Timestamp == mGuestNotifications.back().mTimestamp)
993 ++u64Timestamp;
994
995 /*
996 * Try to find the property. Create a change event if we find it and a
997 * delete event if we do not.
998 */
999 Property prop;
1000 prop.mName = pszProperty;
1001 prop.mTimestamp = u64Timestamp;
1002 /* prop is currently a delete event for pszProperty */
1003 bool found = false;
1004 if (RT_SUCCESS(rc))
1005 for (PropertyList::const_iterator it = mProperties.begin();
1006 !found && it != mProperties.end(); ++it)
1007 if (it->mName.compare(pszProperty) == 0)
1008 {
1009 found = true;
1010 /* Make prop into a change event. */
1011 prop.mValue = it->mValue;
1012 prop.mFlags = it->mFlags;
1013 }
1014
1015
1016 /* Release waiters if applicable and add the event to the queue for
1017 * guest notifications */
1018 if (RT_SUCCESS(rc))
1019 {
1020 try
1021 {
1022 CallList::iterator it = mGuestWaiters.begin();
1023 while (it != mGuestWaiters.end())
1024 {
1025 const char *pszPatterns;
1026 uint32_t cchPatterns;
1027 it->mParms[0].getPointer((void **) &pszPatterns, &cchPatterns);
1028 if (prop.Matches(pszPatterns))
1029 {
1030 GuestCall call = *it;
1031 int rc2 = getNotificationWriteOut(call.mParms, prop);
1032 if (RT_SUCCESS(rc2))
1033 rc2 = call.mRc;
1034 mpHelpers->pfnCallComplete (call.mHandle, rc2);
1035 it = mGuestWaiters.erase(it);
1036 }
1037 else
1038 ++it;
1039 }
1040 mGuestNotifications.push_back(prop);
1041 }
1042 catch (std::bad_alloc)
1043 {
1044 rc = VERR_NO_MEMORY;
1045 }
1046 }
1047 if (mGuestNotifications.size() > MAX_GUEST_NOTIFICATIONS)
1048 mGuestNotifications.pop_front();
1049
1050#ifndef VBOX_GUEST_PROP_TEST_NOTHREAD
1051 /*
1052 * Host notifications - first case: if the property exists then send its
1053 * current value
1054 */
1055 char *pszName = NULL, *pszValue = NULL, *pszFlags = NULL;
1056
1057 if (found && mpfnHostCallback != NULL)
1058 {
1059 char szFlags[MAX_FLAGS_LEN];
1060 /* Send out a host notification */
1061 rc = writeFlags(prop.mFlags, szFlags);
1062 if (RT_SUCCESS(rc))
1063 rc = RTStrDupEx(&pszName, pszProperty);
1064 if (RT_SUCCESS(rc))
1065 rc = RTStrDupEx(&pszValue, prop.mValue.c_str());
1066 if (RT_SUCCESS(rc))
1067 rc = RTStrDupEx(&pszFlags, szFlags);
1068 if (RT_SUCCESS(rc))
1069 {
1070 LogFlowThisFunc (("pszName=%p (%s)\n", pszName, pszName));
1071 LogFlowThisFunc (("pszValue=%p (%s)\n", pszValue, pszValue));
1072 LogFlowThisFunc (("pszFlags=%p (%s)\n", pszFlags, pszFlags));
1073 rc = RTReqCallEx(mReqQueue, NULL, 0, RTREQFLAGS_NO_WAIT,
1074 (PFNRT)Service::reqNotify, 7, mpfnHostCallback,
1075 mpvHostData, pszName, pszValue,
1076 (uint32_t) RT_HIDWORD(u64Timestamp),
1077 (uint32_t) RT_LODWORD(u64Timestamp), pszFlags);
1078 }
1079 }
1080
1081 /*
1082 * Host notifications - second case: if the property does not exist then
1083 * send the host an empty value
1084 */
1085 if (!found && mpfnHostCallback != NULL)
1086 {
1087 /* Send out a host notification */
1088 rc = RTStrDupEx(&pszName, pszProperty);
1089 if (RT_SUCCESS(rc))
1090 {
1091 LogFlowThisFunc (("pszName=%p (%s)\n", pszName, pszName));
1092 rc = RTReqCallEx(mReqQueue, NULL, 0, RTREQFLAGS_NO_WAIT,
1093 (PFNRT)Service::reqNotify, 7, mpfnHostCallback,
1094 mpvHostData, pszName, (uintptr_t) NULL,
1095 (uint32_t) RT_HIDWORD(u64Timestamp),
1096 (uint32_t) RT_LODWORD(u64Timestamp),
1097 (uintptr_t) NULL);
1098 }
1099 }
1100 if (RT_FAILURE(rc)) /* clean up if we failed somewhere */
1101 {
1102 LogThisFunc (("Failed, freeing allocated strings.\n"));
1103 RTStrFree(pszName);
1104 RTStrFree(pszValue);
1105 RTStrFree(pszFlags);
1106 }
1107 LogFlowThisFunc (("returning\n"));
1108#endif /* VBOX_GUEST_PROP_TEST_NOTHREAD not defined */
1109}
1110
1111/**
1112 * Notify the service owner that a property has been added/deleted/changed.
1113 * asynchronous part.
1114 * @param pszProperty the name of the property which has changed
1115 * @note this call allocates memory which the reqNotify request is expected to
1116 * free again, using RTStrFree().
1117 *
1118 * @thread request thread
1119 */
1120/* static */
1121int Service::reqNotify(PFNHGCMSVCEXT pfnCallback, void *pvData,
1122 char *pszName, char *pszValue, uint32_t u32TimeHigh,
1123 uint32_t u32TimeLow, char *pszFlags)
1124{
1125 LogFlowFunc (("pfnCallback=%p, pvData=%p, pszName=%p, pszValue=%p, u32TimeHigh=%u, u32TimeLow=%u, pszFlags=%p\n", pfnCallback, pvData, pszName, pszValue, u32TimeHigh, u32TimeLow, pszFlags));
1126 LogFlowFunc (("pszName=%s\n", pszName));
1127 LogFlowFunc (("pszValue=%s\n", pszValue));
1128 LogFlowFunc (("pszFlags=%s\n", pszFlags));
1129 /* LogFlowFunc (("pfnCallback=%p, pvData=%p, pszName=%s, pszValue=%s, u32TimeHigh=%u, u32TimeLow=%u, pszFlags=%s\n", pfnCallback, pvData, pszName, pszValue, u32TimeHigh, u32TimeLow, pszFlags)); */
1130 HOSTCALLBACKDATA HostCallbackData;
1131 HostCallbackData.u32Magic = HOSTCALLBACKMAGIC;
1132 HostCallbackData.pcszName = pszName;
1133 HostCallbackData.pcszValue = pszValue;
1134 HostCallbackData.u64Timestamp = RT_MAKE_U64(u32TimeLow, u32TimeHigh);
1135 HostCallbackData.pcszFlags = pszFlags;
1136 int rc = pfnCallback(pvData, 0, (void *)(&HostCallbackData),
1137 sizeof(HostCallbackData));
1138 AssertRC(rc);
1139 LogFlowFunc (("Freeing strings\n"));
1140 RTStrFree(pszName);
1141 RTStrFree(pszValue);
1142 RTStrFree(pszFlags);
1143 LogFlowFunc (("returning success\n"));
1144 return VINF_SUCCESS;
1145}
1146
1147
1148/**
1149 * Handle an HGCM service call.
1150 * @copydoc VBOXHGCMSVCFNTABLE::pfnCall
1151 * @note All functions which do not involve an unreasonable delay will be
1152 * handled synchronously. If needed, we will add a request handler
1153 * thread in future for those which do.
1154 *
1155 * @thread HGCM
1156 */
1157void Service::call (VBOXHGCMCALLHANDLE callHandle, uint32_t u32ClientID,
1158 void * /* pvClient */, uint32_t eFunction, uint32_t cParms,
1159 VBOXHGCMSVCPARM paParms[])
1160{
1161 int rc = VINF_SUCCESS;
1162 LogFlowFunc(("u32ClientID = %d, fn = %d, cParms = %d, pparms = %d\n",
1163 u32ClientID, eFunction, cParms, paParms));
1164
1165 try
1166 {
1167 switch (eFunction)
1168 {
1169 /* The guest wishes to read a property */
1170 case GET_PROP:
1171 LogFlowFunc(("GET_PROP\n"));
1172 rc = getProperty(cParms, paParms);
1173 break;
1174
1175 /* The guest wishes to set a property */
1176 case SET_PROP:
1177 LogFlowFunc(("SET_PROP\n"));
1178 rc = setProperty(cParms, paParms, true);
1179 break;
1180
1181 /* The guest wishes to set a property value */
1182 case SET_PROP_VALUE:
1183 LogFlowFunc(("SET_PROP_VALUE\n"));
1184 rc = setProperty(cParms, paParms, true);
1185 break;
1186
1187 /* The guest wishes to remove a configuration value */
1188 case DEL_PROP:
1189 LogFlowFunc(("DEL_PROP\n"));
1190 rc = delProperty(cParms, paParms, true);
1191 break;
1192
1193 /* The guest wishes to enumerate all properties */
1194 case ENUM_PROPS:
1195 LogFlowFunc(("ENUM_PROPS\n"));
1196 rc = enumProps(cParms, paParms);
1197 break;
1198
1199 /* The guest wishes to get the next property notification */
1200 case GET_NOTIFICATION:
1201 LogFlowFunc(("GET_NOTIFICATION\n"));
1202 rc = getNotification(callHandle, cParms, paParms);
1203 break;
1204
1205 default:
1206 rc = VERR_NOT_IMPLEMENTED;
1207 }
1208 }
1209 catch (std::bad_alloc)
1210 {
1211 rc = VERR_NO_MEMORY;
1212 }
1213 LogFlowFunc(("rc = %Rrc\n", rc));
1214 if (rc != VINF_HGCM_ASYNC_EXECUTE)
1215 {
1216 mpHelpers->pfnCallComplete (callHandle, rc);
1217 }
1218}
1219
1220
1221/**
1222 * Service call handler for the host.
1223 * @copydoc VBOXHGCMSVCFNTABLE::pfnHostCall
1224 * @thread hgcm
1225 */
1226int Service::hostCall (uint32_t eFunction, uint32_t cParms, VBOXHGCMSVCPARM paParms[])
1227{
1228 int rc = VINF_SUCCESS;
1229
1230 LogFlowFunc(("fn = %d, cParms = %d, pparms = %d\n",
1231 eFunction, cParms, paParms));
1232
1233 try
1234 {
1235 switch (eFunction)
1236 {
1237 /* The host wishes to set a block of properties */
1238 case SET_PROPS_HOST:
1239 LogFlowFunc(("SET_PROPS_HOST\n"));
1240 rc = setPropertyBlock(cParms, paParms);
1241 break;
1242
1243 /* The host wishes to read a configuration value */
1244 case GET_PROP_HOST:
1245 LogFlowFunc(("GET_PROP_HOST\n"));
1246 rc = getProperty(cParms, paParms);
1247 break;
1248
1249 /* The host wishes to set a configuration value */
1250 case SET_PROP_HOST:
1251 LogFlowFunc(("SET_PROP_HOST\n"));
1252 rc = setProperty(cParms, paParms, false);
1253 break;
1254
1255 /* The host wishes to set a configuration value */
1256 case SET_PROP_VALUE_HOST:
1257 LogFlowFunc(("SET_PROP_VALUE_HOST\n"));
1258 rc = setProperty(cParms, paParms, false);
1259 break;
1260
1261 /* The host wishes to remove a configuration value */
1262 case DEL_PROP_HOST:
1263 LogFlowFunc(("DEL_PROP_HOST\n"));
1264 rc = delProperty(cParms, paParms, false);
1265 break;
1266
1267 /* The host wishes to enumerate all properties */
1268 case ENUM_PROPS_HOST:
1269 LogFlowFunc(("ENUM_PROPS\n"));
1270 rc = enumProps(cParms, paParms);
1271 break;
1272
1273 default:
1274 rc = VERR_NOT_SUPPORTED;
1275 break;
1276 }
1277 }
1278 catch (std::bad_alloc)
1279 {
1280 rc = VERR_NO_MEMORY;
1281 }
1282
1283 LogFlowFunc(("rc = %Rrc\n", rc));
1284 return rc;
1285}
1286
1287int Service::uninit()
1288{
1289 int rc = VINF_SUCCESS;
1290 unsigned count = 0;
1291
1292 mfExitThread = true;
1293#ifndef VBOX_GUEST_PROP_TEST_NOTHREAD
1294 rc = RTReqCallEx(mReqQueue, NULL, 0, RTREQFLAGS_NO_WAIT, (PFNRT)reqVoid, 0);
1295 if (RT_SUCCESS(rc))
1296 do
1297 {
1298 rc = RTThreadWait(mReqThread, 1000, NULL);
1299 ++count;
1300 Assert(RT_SUCCESS(rc) || ((VERR_TIMEOUT == rc) && (count != 5)));
1301 } while ((VERR_TIMEOUT == rc) && (count < 300));
1302#endif /* VBOX_GUEST_PROP_TEST_NOTHREAD not defined */
1303 if (RT_SUCCESS(rc))
1304 RTReqDestroyQueue(mReqQueue);
1305 return rc;
1306}
1307
1308} /* namespace guestProp */
1309
1310using guestProp::Service;
1311
1312/**
1313 * @copydoc VBOXHGCMSVCLOAD
1314 */
1315extern "C" DECLCALLBACK(DECLEXPORT(int)) VBoxHGCMSvcLoad (VBOXHGCMSVCFNTABLE *ptable)
1316{
1317 int rc = VINF_SUCCESS;
1318
1319 LogFlowFunc(("ptable = %p\n", ptable));
1320
1321 if (!VALID_PTR(ptable))
1322 {
1323 rc = VERR_INVALID_PARAMETER;
1324 }
1325 else
1326 {
1327 LogFlowFunc(("ptable->cbSize = %d, ptable->u32Version = 0x%08X\n", ptable->cbSize, ptable->u32Version));
1328
1329 if ( ptable->cbSize != sizeof (VBOXHGCMSVCFNTABLE)
1330 || ptable->u32Version != VBOX_HGCM_SVC_VERSION)
1331 {
1332 rc = VERR_VERSION_MISMATCH;
1333 }
1334 else
1335 {
1336 std::auto_ptr<Service> apService;
1337 /* No exceptions may propogate outside. */
1338 try {
1339 apService = std::auto_ptr<Service>(new Service(ptable->pHelpers));
1340 } catch (int rcThrown) {
1341 rc = rcThrown;
1342 } catch (...) {
1343 rc = VERR_UNRESOLVED_ERROR;
1344 }
1345
1346 if (RT_SUCCESS(rc))
1347 {
1348 /* We do not maintain connections, so no client data is needed. */
1349 ptable->cbClient = 0;
1350
1351 ptable->pfnUnload = Service::svcUnload;
1352 ptable->pfnConnect = Service::svcConnectDisconnect;
1353 ptable->pfnDisconnect = Service::svcConnectDisconnect;
1354 ptable->pfnCall = Service::svcCall;
1355 ptable->pfnHostCall = Service::svcHostCall;
1356 ptable->pfnSaveState = NULL; /* The service is stateless, so the normal */
1357 ptable->pfnLoadState = NULL; /* construction done before restoring suffices */
1358 ptable->pfnRegisterExtension = Service::svcRegisterExtension;
1359
1360 /* Service specific initialization. */
1361 ptable->pvService = apService.release();
1362 }
1363 }
1364 }
1365
1366 LogFlowFunc(("returning %Rrc\n", rc));
1367 return rc;
1368}
1369
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