VirtualBox

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

Last change on this file since 17838 was 15277, checked in by vboxsync, 16 years ago

HostServices/GuestProperties: hopefully fix a crash on Solaris hosts

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 46.3 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 uint32_t cchFlags, cchBufActual;
531 char szFlags[MAX_FLAGS_LEN];
532
533 /*
534 * Get and validate the parameters
535 */
536 LogFlowThisFunc(("\n"));
537 if ( cParms != 4 /* Hardcoded value as the next lines depend on it. */
538 || RT_FAILURE (paParms[0].getPointer ((const void **) &pcszName, &cchName)) /* name */
539 || RT_FAILURE (paParms[1].getPointer ((void **) &pchBuf, &cchBuf)) /* buffer */
540 )
541 rc = VERR_INVALID_PARAMETER;
542 if (RT_SUCCESS(rc))
543 rc = validateName(pcszName, cchName);
544
545 /*
546 * Read and set the values we will return
547 */
548
549 /* Get the value size */
550 PropertyList::const_iterator it;
551 bool found = false;
552 if (RT_SUCCESS(rc))
553 for (it = mProperties.begin(); it != mProperties.end(); ++it)
554 if (it->mName.compare(pcszName) == 0)
555 {
556 found = true;
557 break;
558 }
559 if (RT_SUCCESS(rc) && !found)
560 rc = VERR_NOT_FOUND;
561 if (RT_SUCCESS(rc))
562 rc = writeFlags(it->mFlags, szFlags);
563 if (RT_SUCCESS(rc))
564 cchFlags = strlen(szFlags);
565 /* Check that the buffer is big enough */
566 if (RT_SUCCESS(rc))
567 {
568 cchBufActual = it->mValue.size() + 1 + cchFlags;
569 paParms[3].setUInt32 (cchBufActual);
570 }
571 if (RT_SUCCESS(rc) && (cchBufActual > cchBuf))
572 rc = VERR_BUFFER_OVERFLOW;
573 /* Write the value, flags and timestamp */
574 if (RT_SUCCESS(rc))
575 {
576 it->mValue.copy(pchBuf, cchBuf, 0);
577 pchBuf[it->mValue.size()] = '\0'; /* Terminate the value */
578 strcpy(pchBuf + it->mValue.size() + 1, szFlags);
579 paParms[2].setUInt64 (it->mTimestamp);
580 }
581
582 /*
583 * Done! Do exit logging and return.
584 */
585 if (RT_SUCCESS(rc))
586 Log2(("Queried string %s, value=%s, timestamp=%lld, flags=%s\n",
587 pcszName, it->mValue.c_str(), it->mTimestamp, szFlags));
588 LogFlowThisFunc(("rc = %Rrc\n", rc));
589 return rc;
590}
591
592/**
593 * Set a value in the property registry by name, checking the validity
594 * of the arguments passed.
595 *
596 * @returns iprt status value
597 * @param cParms the number of HGCM parameters supplied
598 * @param paParms the array of HGCM parameters
599 * @param isGuest is this call coming from the guest (or the host)?
600 * @throws std::bad_alloc if an out of memory condition occurs
601 * @thread HGCM
602 */
603int Service::setProperty(uint32_t cParms, VBOXHGCMSVCPARM paParms[], bool isGuest)
604{
605 int rc = VINF_SUCCESS;
606 const char *pcszName, *pcszValue, *pcszFlags = NULL;
607 uint32_t cchName, cchValue, cchFlags = 0;
608 uint32_t fFlags = NILFLAG;
609 RTTIMESPEC time;
610 uint64_t u64TimeNano = RTTimeSpecGetNano(RTTimeNow(&time));
611
612 LogFlowThisFunc(("\n"));
613 /*
614 * First of all, make sure that we won't exceed the maximum number of properties.
615 */
616 if (mProperties.size() >= MAX_PROPS)
617 rc = VERR_TOO_MUCH_DATA;
618
619 /*
620 * General parameter correctness checking.
621 */
622 if ( RT_SUCCESS(rc)
623 && ( (cParms < 2) || (cParms > 3) /* Hardcoded value as the next lines depend on it. */
624 || RT_FAILURE(paParms[0].getPointer ((const void **) &pcszName,
625 &cchName)) /* name */
626 || RT_FAILURE(paParms[1].getPointer ((const void **) &pcszValue,
627 &cchValue)) /* value */
628 || ( (3 == cParms)
629 && RT_FAILURE(paParms[2].getPointer ((const void **) &pcszFlags,
630 &cchFlags)) /* flags */
631 )
632 )
633 )
634 rc = VERR_INVALID_PARAMETER;
635
636 /*
637 * Check the values passed in the parameters for correctness.
638 */
639 if (RT_SUCCESS(rc))
640 rc = validateName(pcszName, cchName);
641 if (RT_SUCCESS(rc))
642 rc = validateValue(pcszValue, cchValue);
643 if ((3 == cParms) && RT_SUCCESS(rc))
644 rc = RTStrValidateEncodingEx(pcszFlags, cchFlags,
645 RTSTR_VALIDATE_ENCODING_ZERO_TERMINATED);
646 if ((3 == cParms) && RT_SUCCESS(rc))
647 rc = validateFlags(pcszFlags, &fFlags);
648
649 /*
650 * If the property already exists, check its flags to see if we are allowed
651 * to change it.
652 */
653 PropertyList::iterator it;
654 bool found = false;
655 if (RT_SUCCESS(rc))
656 for (it = mProperties.begin(); it != mProperties.end(); ++it)
657 if (it->mName.compare(pcszName) == 0)
658 {
659 found = true;
660 break;
661 }
662 if (RT_SUCCESS(rc) && found)
663 if ( (isGuest && (it->mFlags & RDONLYGUEST))
664 || (!isGuest && (it->mFlags & RDONLYHOST))
665 )
666 rc = VERR_PERMISSION_DENIED;
667
668 /*
669 * Set the actual value
670 */
671 if (RT_SUCCESS(rc))
672 {
673 if (found)
674 {
675 it->mValue = pcszValue;
676 it->mTimestamp = u64TimeNano;
677 it->mFlags = fFlags;
678 }
679 else /* This can throw. No problem as we have nothing to roll back. */
680 mProperties.push_back(Property(pcszName, pcszValue, u64TimeNano, fFlags));
681 }
682
683 /*
684 * Send a notification to the host and return.
685 */
686 if (RT_SUCCESS(rc))
687 {
688 // if (isGuest) /* Notify the host even for properties that the host
689 // * changed. Less efficient, but ensures consistency. */
690 doNotifications(pcszName, u64TimeNano);
691 Log2(("Set string %s, rc=%Rrc, value=%s\n", pcszName, rc, pcszValue));
692 }
693 LogFlowThisFunc(("rc = %Rrc\n", rc));
694 return rc;
695}
696
697
698/**
699 * Remove a value in the property registry by name, checking the validity
700 * of the arguments passed.
701 *
702 * @returns iprt status value
703 * @param cParms the number of HGCM parameters supplied
704 * @param paParms the array of HGCM parameters
705 * @param isGuest is this call coming from the guest (or the host)?
706 * @thread HGCM
707 */
708int Service::delProperty(uint32_t cParms, VBOXHGCMSVCPARM paParms[], bool isGuest)
709{
710 int rc = VINF_SUCCESS;
711 const char *pcszName;
712 uint32_t cbName;
713
714 LogFlowThisFunc(("\n"));
715
716 /*
717 * Check the user-supplied parameters.
718 */
719 if ( (cParms != 1) /* Hardcoded value as the next lines depend on it. */
720 || RT_FAILURE(paParms[0].getPointer ((const void **) &pcszName,
721 &cbName)) /* name */
722 )
723 rc = VERR_INVALID_PARAMETER;
724 if (RT_SUCCESS(rc))
725 rc = validateName(pcszName, cbName);
726
727 /*
728 * If the property exists, check its flags to see if we are allowed
729 * to change it.
730 */
731 PropertyList::iterator it;
732 bool found = false;
733 if (RT_SUCCESS(rc))
734 for (it = mProperties.begin(); it != mProperties.end(); ++it)
735 if (it->mName.compare(pcszName) == 0)
736 {
737 found = true;
738 break;
739 }
740 if (RT_SUCCESS(rc) && found)
741 if ( (isGuest && (it->mFlags & RDONLYGUEST))
742 || (!isGuest && (it->mFlags & RDONLYHOST))
743 )
744 rc = VERR_PERMISSION_DENIED;
745
746 /*
747 * And delete the property if all is well.
748 */
749 if (RT_SUCCESS(rc) && found)
750 {
751 RTTIMESPEC time;
752 uint64_t u64Timestamp = RTTimeSpecGetNano(RTTimeNow(&time));
753 mProperties.erase(it);
754 // if (isGuest) /* Notify the host even for properties that the host
755 // * changed. Less efficient, but ensures consistency. */
756 doNotifications(pcszName, u64Timestamp);
757 }
758 LogFlowThisFunc(("rc = %Rrc\n", rc));
759 return rc;
760}
761
762/**
763 * Enumerate guest properties by mask, checking the validity
764 * of the arguments passed.
765 *
766 * @returns iprt status value
767 * @param cParms the number of HGCM parameters supplied
768 * @param paParms the array of HGCM parameters
769 * @thread HGCM
770 */
771int Service::enumProps(uint32_t cParms, VBOXHGCMSVCPARM paParms[])
772{
773 int rc = VINF_SUCCESS;
774
775 /*
776 * Get the HGCM function arguments.
777 */
778 char *pcchPatterns = NULL, *pchBuf = NULL;
779 uint32_t cchPatterns = 0, cchBuf = 0;
780 LogFlowThisFunc(("\n"));
781 if ( (cParms != 3) /* Hardcoded value as the next lines depend on it. */
782 || RT_FAILURE(paParms[0].getPointer ((const void **) &pcchPatterns,
783 &cchPatterns)) /* patterns */
784 || RT_FAILURE(paParms[1].getPointer ((void **) &pchBuf, &cchBuf)) /* return buffer */
785 )
786 rc = VERR_INVALID_PARAMETER;
787 if (RT_SUCCESS(rc) && cchPatterns > MAX_PATTERN_LEN)
788 rc = VERR_TOO_MUCH_DATA;
789
790 /*
791 * First repack the patterns into the format expected by RTStrSimplePatternMatch()
792 */
793 char pszPatterns[MAX_PATTERN_LEN];
794 if (NULL == pcchPatterns)
795 pszPatterns[0] = '\0';
796 else
797 {
798 for (unsigned i = 0; i < cchPatterns - 1; ++i)
799 if (pcchPatterns[i] != '\0')
800 pszPatterns[i] = pcchPatterns[i];
801 else
802 pszPatterns[i] = '|';
803 pszPatterns[cchPatterns - 1] = '\0';
804 }
805
806 /*
807 * Next enumerate into a temporary buffer. This can throw, but this is
808 * not a problem as we have nothing to roll back.
809 */
810 std::string buffer;
811 for (PropertyList::const_iterator it = mProperties.begin();
812 RT_SUCCESS(rc) && (it != mProperties.end()); ++it)
813 {
814 if (it->Matches(pszPatterns))
815 {
816 char szFlags[MAX_FLAGS_LEN];
817 char szTimestamp[256];
818 uint32_t cchTimestamp;
819 buffer += it->mName;
820 buffer += '\0';
821 buffer += it->mValue;
822 buffer += '\0';
823 cchTimestamp = RTStrFormatNumber(szTimestamp, it->mTimestamp,
824 10, 0, 0, 0);
825 buffer.append(szTimestamp, cchTimestamp);
826 buffer += '\0';
827 rc = writeFlags(it->mFlags, szFlags);
828 if (RT_SUCCESS(rc))
829 buffer += szFlags;
830 buffer += '\0';
831 }
832 }
833 buffer.append(4, '\0'); /* The final terminators */
834
835 /*
836 * Finally write out the temporary buffer to the real one if it is not too
837 * small.
838 */
839 if (RT_SUCCESS(rc))
840 {
841 paParms[2].setUInt32 (buffer.size());
842 /* Copy the memory if it fits into the guest buffer */
843 if (buffer.size() <= cchBuf)
844 buffer.copy(pchBuf, cchBuf);
845 else
846 rc = VERR_BUFFER_OVERFLOW;
847 }
848 return rc;
849}
850
851/** Helper query used by getOldNotification */
852int Service::getOldNotificationInternal(const char *pszPatterns,
853 uint64_t u64Timestamp,
854 Property *pProp)
855{
856 int rc = VINF_SUCCESS;
857 bool warn = false;
858
859 /* We count backwards, as the guest should normally be querying the
860 * most recent events. */
861 PropertyList::reverse_iterator it = mGuestNotifications.rbegin();
862 for (; it->mTimestamp != u64Timestamp && it != mGuestNotifications.rend();
863 ++it) {}
864 /* Warn if the timestamp was not found. */
865 if (it->mTimestamp != u64Timestamp)
866 warn = true;
867 /* Now look for an event matching the patterns supplied. The base()
868 * member conveniently points to the following element. */
869 PropertyList::iterator base = it.base();
870 for (; !base->Matches(pszPatterns) && base != mGuestNotifications.end();
871 ++base) {}
872 if (RT_SUCCESS(rc) && base != mGuestNotifications.end())
873 *pProp = *base;
874 else if (RT_SUCCESS(rc))
875 *pProp = Property();
876 if (warn)
877 rc = VWRN_NOT_FOUND;
878 return rc;
879}
880
881/** Helper query used by getNotification */
882int Service::getNotificationWriteOut(VBOXHGCMSVCPARM paParms[], Property prop)
883{
884 int rc = VINF_SUCCESS;
885 /* Format the data to write to the buffer. */
886 std::string buffer;
887 uint64_t u64Timestamp;
888 char *pchBuf;
889 uint32_t cchBuf;
890 rc = paParms[2].getPointer((void **) &pchBuf, &cchBuf);
891 if (RT_SUCCESS(rc))
892 {
893 char szFlags[MAX_FLAGS_LEN];
894 rc = writeFlags(prop.mFlags, szFlags);
895 if (RT_SUCCESS(rc))
896 {
897 buffer += prop.mName;
898 buffer += '\0';
899 buffer += prop.mValue;
900 buffer += '\0';
901 buffer += szFlags;
902 buffer += '\0';
903 u64Timestamp = prop.mTimestamp;
904 }
905 }
906 /* Write out the data. */
907 if (RT_SUCCESS(rc))
908 {
909 paParms[1].setUInt64(u64Timestamp);
910 paParms[3].setUInt32(buffer.size());
911 if (buffer.size() <= cchBuf)
912 buffer.copy(pchBuf, cchBuf);
913 else
914 rc = VERR_BUFFER_OVERFLOW;
915 }
916 return rc;
917}
918
919/**
920 * Get the next guest notification.
921 *
922 * @returns iprt status value
923 * @param cParms the number of HGCM parameters supplied
924 * @param paParms the array of HGCM parameters
925 * @thread HGCM
926 * @throws can throw std::bad_alloc
927 */
928int Service::getNotification(VBOXHGCMCALLHANDLE callHandle, uint32_t cParms,
929 VBOXHGCMSVCPARM paParms[])
930{
931 int rc = VINF_SUCCESS;
932 char *pszPatterns, *pchBuf;
933 uint32_t cchPatterns = 0, cchBuf = 0;
934 uint64_t u64Timestamp;
935
936 /*
937 * Get the HGCM function arguments and perform basic verification.
938 */
939 LogFlowThisFunc(("\n"));
940 if ( (cParms != 4) /* Hardcoded value as the next lines depend on it. */
941 || RT_FAILURE(paParms[0].getPointer ((void **) &pszPatterns, &cchPatterns)) /* patterns */
942 || pszPatterns[cchPatterns - 1] != '\0' /* The patterns string must be zero-terminated */
943 || RT_FAILURE(paParms[1].getUInt64 (&u64Timestamp)) /* timestamp */
944 || RT_FAILURE(paParms[2].getPointer ((void **) &pchBuf, &cchBuf)) /* return buffer */
945 || cchBuf < 1
946 )
947 rc = VERR_INVALID_PARAMETER;
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 AssertRC(pfnCallback(pvData, 0, reinterpret_cast<void *>(&HostCallbackData),
1137 sizeof(HostCallbackData)));
1138 LogFlowFunc (("Freeing strings\n"));
1139 RTStrFree(pszName);
1140 RTStrFree(pszValue);
1141 RTStrFree(pszFlags);
1142 LogFlowFunc (("returning success\n"));
1143 return VINF_SUCCESS;
1144}
1145
1146
1147/**
1148 * Handle an HGCM service call.
1149 * @copydoc VBOXHGCMSVCFNTABLE::pfnCall
1150 * @note All functions which do not involve an unreasonable delay will be
1151 * handled synchronously. If needed, we will add a request handler
1152 * thread in future for those which do.
1153 *
1154 * @thread HGCM
1155 */
1156void Service::call (VBOXHGCMCALLHANDLE callHandle, uint32_t u32ClientID,
1157 void * /* pvClient */, uint32_t eFunction, uint32_t cParms,
1158 VBOXHGCMSVCPARM paParms[])
1159{
1160 int rc = VINF_SUCCESS;
1161 LogFlowFunc(("u32ClientID = %d, fn = %d, cParms = %d, pparms = %d\n",
1162 u32ClientID, eFunction, cParms, paParms));
1163
1164 try
1165 {
1166 switch (eFunction)
1167 {
1168 /* The guest wishes to read a property */
1169 case GET_PROP:
1170 LogFlowFunc(("GET_PROP\n"));
1171 rc = getProperty(cParms, paParms);
1172 break;
1173
1174 /* The guest wishes to set a property */
1175 case SET_PROP:
1176 LogFlowFunc(("SET_PROP\n"));
1177 rc = setProperty(cParms, paParms, true);
1178 break;
1179
1180 /* The guest wishes to set a property value */
1181 case SET_PROP_VALUE:
1182 LogFlowFunc(("SET_PROP_VALUE\n"));
1183 rc = setProperty(cParms, paParms, true);
1184 break;
1185
1186 /* The guest wishes to remove a configuration value */
1187 case DEL_PROP:
1188 LogFlowFunc(("DEL_PROP\n"));
1189 rc = delProperty(cParms, paParms, true);
1190 break;
1191
1192 /* The guest wishes to enumerate all properties */
1193 case ENUM_PROPS:
1194 LogFlowFunc(("ENUM_PROPS\n"));
1195 rc = enumProps(cParms, paParms);
1196 break;
1197
1198 /* The guest wishes to get the next property notification */
1199 case GET_NOTIFICATION:
1200 LogFlowFunc(("GET_NOTIFICATION\n"));
1201 rc = getNotification(callHandle, cParms, paParms);
1202 break;
1203
1204 default:
1205 rc = VERR_NOT_IMPLEMENTED;
1206 }
1207 }
1208 catch (std::bad_alloc)
1209 {
1210 rc = VERR_NO_MEMORY;
1211 }
1212 LogFlowFunc(("rc = %Rrc\n", rc));
1213 if (rc != VINF_HGCM_ASYNC_EXECUTE)
1214 {
1215 mpHelpers->pfnCallComplete (callHandle, rc);
1216 }
1217}
1218
1219
1220/**
1221 * Service call handler for the host.
1222 * @copydoc VBOXHGCMSVCFNTABLE::pfnHostCall
1223 * @thread hgcm
1224 */
1225int Service::hostCall (uint32_t eFunction, uint32_t cParms, VBOXHGCMSVCPARM paParms[])
1226{
1227 int rc = VINF_SUCCESS;
1228
1229 LogFlowFunc(("fn = %d, cParms = %d, pparms = %d\n",
1230 eFunction, cParms, paParms));
1231
1232 try
1233 {
1234 switch (eFunction)
1235 {
1236 /* The host wishes to set a block of properties */
1237 case SET_PROPS_HOST:
1238 LogFlowFunc(("SET_PROPS_HOST\n"));
1239 rc = setPropertyBlock(cParms, paParms);
1240 break;
1241
1242 /* The host wishes to read a configuration value */
1243 case GET_PROP_HOST:
1244 LogFlowFunc(("GET_PROP_HOST\n"));
1245 rc = getProperty(cParms, paParms);
1246 break;
1247
1248 /* The host wishes to set a configuration value */
1249 case SET_PROP_HOST:
1250 LogFlowFunc(("SET_PROP_HOST\n"));
1251 rc = setProperty(cParms, paParms, false);
1252 break;
1253
1254 /* The host wishes to set a configuration value */
1255 case SET_PROP_VALUE_HOST:
1256 LogFlowFunc(("SET_PROP_VALUE_HOST\n"));
1257 rc = setProperty(cParms, paParms, false);
1258 break;
1259
1260 /* The host wishes to remove a configuration value */
1261 case DEL_PROP_HOST:
1262 LogFlowFunc(("DEL_PROP_HOST\n"));
1263 rc = delProperty(cParms, paParms, false);
1264 break;
1265
1266 /* The host wishes to enumerate all properties */
1267 case ENUM_PROPS_HOST:
1268 LogFlowFunc(("ENUM_PROPS\n"));
1269 rc = enumProps(cParms, paParms);
1270 break;
1271
1272 default:
1273 rc = VERR_NOT_SUPPORTED;
1274 break;
1275 }
1276 }
1277 catch (std::bad_alloc)
1278 {
1279 rc = VERR_NO_MEMORY;
1280 }
1281
1282 LogFlowFunc(("rc = %Rrc\n", rc));
1283 return rc;
1284}
1285
1286int Service::uninit()
1287{
1288 int rc = VINF_SUCCESS;
1289 unsigned count = 0;
1290
1291 mfExitThread = true;
1292#ifndef VBOX_GUEST_PROP_TEST_NOTHREAD
1293 rc = RTReqCallEx(mReqQueue, NULL, 0, RTREQFLAGS_NO_WAIT, (PFNRT)reqVoid, 0);
1294 if (RT_SUCCESS(rc))
1295 do
1296 {
1297 rc = RTThreadWait(mReqThread, 1000, NULL);
1298 ++count;
1299 Assert(RT_SUCCESS(rc) || ((VERR_TIMEOUT == rc) && (count != 5)));
1300 } while ((VERR_TIMEOUT == rc) && (count < 300));
1301#endif /* VBOX_GUEST_PROP_TEST_NOTHREAD not defined */
1302 if (RT_SUCCESS(rc))
1303 RTReqDestroyQueue(mReqQueue);
1304 return rc;
1305}
1306
1307} /* namespace guestProp */
1308
1309using guestProp::Service;
1310
1311/**
1312 * @copydoc VBOXHGCMSVCLOAD
1313 */
1314extern "C" DECLCALLBACK(DECLEXPORT(int)) VBoxHGCMSvcLoad (VBOXHGCMSVCFNTABLE *ptable)
1315{
1316 int rc = VINF_SUCCESS;
1317
1318 LogFlowFunc(("ptable = %p\n", ptable));
1319
1320 if (!VALID_PTR(ptable))
1321 {
1322 rc = VERR_INVALID_PARAMETER;
1323 }
1324 else
1325 {
1326 LogFlowFunc(("ptable->cbSize = %d, ptable->u32Version = 0x%08X\n", ptable->cbSize, ptable->u32Version));
1327
1328 if ( ptable->cbSize != sizeof (VBOXHGCMSVCFNTABLE)
1329 || ptable->u32Version != VBOX_HGCM_SVC_VERSION)
1330 {
1331 rc = VERR_VERSION_MISMATCH;
1332 }
1333 else
1334 {
1335 std::auto_ptr<Service> apService;
1336 /* No exceptions may propogate outside. */
1337 try {
1338 apService = std::auto_ptr<Service>(new Service(ptable->pHelpers));
1339 } catch (int rcThrown) {
1340 rc = rcThrown;
1341 } catch (...) {
1342 rc = VERR_UNRESOLVED_ERROR;
1343 }
1344
1345 if (RT_SUCCESS(rc))
1346 {
1347 /* We do not maintain connections, so no client data is needed. */
1348 ptable->cbClient = 0;
1349
1350 ptable->pfnUnload = Service::svcUnload;
1351 ptable->pfnConnect = Service::svcConnectDisconnect;
1352 ptable->pfnDisconnect = Service::svcConnectDisconnect;
1353 ptable->pfnCall = Service::svcCall;
1354 ptable->pfnHostCall = Service::svcHostCall;
1355 ptable->pfnSaveState = NULL; /* The service is stateless, so the normal */
1356 ptable->pfnLoadState = NULL; /* construction done before restoring suffices */
1357 ptable->pfnRegisterExtension = Service::svcRegisterExtension;
1358
1359 /* Service specific initialization. */
1360 ptable->pvService = apService.release();
1361 }
1362 }
1363 }
1364
1365 LogFlowFunc(("returning %Rrc\n", rc));
1366 return rc;
1367}
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