VirtualBox

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

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

Guest Properties (Main, HostServices, VBoxGuestLib): MAX_*_LEN is the size in bytes, not the string length

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 36.7 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 <memory> /* for auto_ptr */
49
50#include <iprt/err.h>
51#include <iprt/assert.h>
52#include <iprt/string.h>
53#include <iprt/mem.h>
54#include <iprt/autores.h>
55#include <iprt/time.h>
56#include <iprt/cpputils.h>
57#include <iprt/req.h>
58#include <iprt/thread.h>
59#include <VBox/log.h>
60
61#include <VBox/cfgm.h>
62
63/*******************************************************************************
64* Internal functions *
65*******************************************************************************/
66/** Extract a pointer value from an HGCM parameter structure */
67static int VBoxHGCMParmPtrGet (VBOXHGCMSVCPARM *pParm, void **ppv, uint32_t *pcb)
68{
69 if (pParm->type == VBOX_HGCM_SVC_PARM_PTR)
70 {
71 *ppv = pParm->u.pointer.addr;
72 *pcb = pParm->u.pointer.size;
73 return VINF_SUCCESS;
74 }
75
76 return VERR_INVALID_PARAMETER;
77}
78
79/** Set a uint32_t value to an HGCM parameter structure */
80static void VBoxHGCMParmUInt32Set (VBOXHGCMSVCPARM *pParm, uint32_t u32)
81{
82 pParm->type = VBOX_HGCM_SVC_PARM_32BIT;
83 pParm->u.uint32 = u32;
84}
85
86
87/** Set a uint64_t value to an HGCM parameter structure */
88static void VBoxHGCMParmUInt64Set (VBOXHGCMSVCPARM *pParm, uint64_t u64)
89{
90 pParm->type = VBOX_HGCM_SVC_PARM_64BIT;
91 pParm->u.uint64 = u64;
92}
93
94namespace guestProp {
95
96/**
97 * Class containing the shared information service functionality.
98 */
99class Service : public stdx::non_copyable
100{
101private:
102 /** Type definition for use in callback functions */
103 typedef Service SELF;
104 /** HGCM helper functions. */
105 PVBOXHGCMSVCHELPERS mpHelpers;
106 /** Pointer to our configuration values node. */
107 PCFGMNODE mpValueNode;
108 /** Pointer to our configuration timestamps node. */
109 PCFGMNODE mpTimestampNode;
110 /** Pointer to our configuration flags node. */
111 PCFGMNODE mpFlagsNode;
112 /** @todo we should have classes for thread and request handler thread */
113 /** Queue of outstanding property change notifications */
114 RTREQQUEUE *mReqQueue;
115 /** Thread for processing the request queue */
116 RTTHREAD mReqThread;
117 /** Tell the thread that it should exit */
118 bool mfExitThread;
119 /** Callback function supplied by the host for notification of updates
120 * to properties */
121 PFNHGCMSVCEXT mpfnHostCallback;
122 /** User data pointer to be supplied to the host callback function */
123 void *mpvHostData;
124
125public:
126 explicit Service(PVBOXHGCMSVCHELPERS pHelpers)
127 : mpHelpers(pHelpers), mpValueNode(NULL), mpTimestampNode(NULL),
128 mpFlagsNode(NULL), mfExitThread(false), mpfnHostCallback(NULL),
129 mpvHostData(NULL)
130 {
131 int rc = RTReqCreateQueue(&mReqQueue);
132 rc = RTThreadCreate(&mReqThread, reqThreadFn, this, 0, RTTHREADTYPE_MSG_PUMP,
133 RTTHREADFLAGS_WAITABLE, "GuestPropReq");
134 if (RT_FAILURE(rc))
135 throw rc;
136 }
137
138 /**
139 * @copydoc VBOXHGCMSVCHELPERS::pfnUnload
140 * Simply deletes the service object
141 */
142 static DECLCALLBACK(int) svcUnload (void *pvService)
143 {
144 AssertLogRelReturn(VALID_PTR(pvService), VERR_INVALID_PARAMETER);
145 SELF *pSelf = reinterpret_cast<SELF *>(pvService);
146 int rc = pSelf->uninit();
147 AssertRC(rc);
148 if (RT_SUCCESS(rc))
149 delete pSelf;
150 return rc;
151 }
152
153 /**
154 * @copydoc VBOXHGCMSVCHELPERS::pfnConnect
155 * Stub implementation of pfnConnect and pfnDisconnect.
156 */
157 static DECLCALLBACK(int) svcConnectDisconnect (void * /* pvService */,
158 uint32_t /* u32ClientID */,
159 void * /* pvClient */)
160 {
161 return VINF_SUCCESS;
162 }
163
164 /**
165 * @copydoc VBOXHGCMSVCHELPERS::pfnCall
166 * Wraps to the call member function
167 */
168 static DECLCALLBACK(void) svcCall (void * pvService,
169 VBOXHGCMCALLHANDLE callHandle,
170 uint32_t u32ClientID,
171 void *pvClient,
172 uint32_t u32Function,
173 uint32_t cParms,
174 VBOXHGCMSVCPARM paParms[])
175 {
176 AssertLogRelReturnVoid(VALID_PTR(pvService));
177 SELF *pSelf = reinterpret_cast<SELF *>(pvService);
178 pSelf->call(callHandle, u32ClientID, pvClient, u32Function, cParms, paParms);
179 }
180
181 /**
182 * @copydoc VBOXHGCMSVCHELPERS::pfnHostCall
183 * Wraps to the hostCall member function
184 */
185 static DECLCALLBACK(int) svcHostCall (void *pvService,
186 uint32_t u32Function,
187 uint32_t cParms,
188 VBOXHGCMSVCPARM paParms[])
189 {
190 AssertLogRelReturn(VALID_PTR(pvService), VERR_INVALID_PARAMETER);
191 SELF *pSelf = reinterpret_cast<SELF *>(pvService);
192 return pSelf->hostCall(u32Function, cParms, paParms);
193 }
194
195 /**
196 * @copydoc VBOXHGCMSVCHELPERS::pfnRegisterExtension
197 * Installs a host callback for notifications of property changes.
198 */
199 static DECLCALLBACK(int) svcRegisterExtension (void *pvService,
200 PFNHGCMSVCEXT pfnExtension,
201 void *pvExtension)
202 {
203 AssertLogRelReturn(VALID_PTR(pvService), VERR_INVALID_PARAMETER);
204 SELF *pSelf = reinterpret_cast<SELF *>(pvService);
205 pSelf->mpfnHostCallback = pfnExtension;
206 pSelf->mpvHostData = pvExtension;
207 return VINF_SUCCESS;
208 }
209private:
210 static DECLCALLBACK(int) reqThreadFn(RTTHREAD ThreadSelf, void *pvUser);
211 int validateName(const char *pszName, uint32_t cbName);
212 int validateValue(char *pszValue, uint32_t cbValue);
213 int getPropValue(uint32_t cParms, VBOXHGCMSVCPARM paParms[]);
214 int getProperty(uint32_t cParms, VBOXHGCMSVCPARM paParms[]);
215 int setProperty(uint32_t cParms, VBOXHGCMSVCPARM paParms[], bool isGuest);
216 int delProperty(uint32_t cParms, VBOXHGCMSVCPARM paParms[], bool isGuest);
217 int enumProps(uint32_t cParms, VBOXHGCMSVCPARM paParms[]);
218 void notifyHost(const char *pszProperty);
219 static DECLCALLBACK(int) reqNotify(PFNHGCMSVCEXT pfnCallback,
220 void *pvData, char *pszName,
221 char *pszValue, uint32_t u32TimeHigh,
222 uint32_t u32TimeLow, char *pszFlags);
223 /**
224 * Empty request function for terminating the request thread.
225 * @returns VINF_EOF to cause the request processing function to return
226 * @todo return something more appropriate
227 */
228 static DECLCALLBACK(int) reqVoid() { return VINF_EOF; }
229
230 void call (VBOXHGCMCALLHANDLE callHandle, uint32_t u32ClientID,
231 void *pvClient, uint32_t eFunction, uint32_t cParms,
232 VBOXHGCMSVCPARM paParms[]);
233 int hostCall (uint32_t eFunction, uint32_t cParms, VBOXHGCMSVCPARM paParms[]);
234 int uninit ();
235};
236
237
238/**
239 * Thread function for processing the request queue
240 * @copydoc FNRTTHREAD
241 */
242DECLCALLBACK(int) Service::reqThreadFn(RTTHREAD ThreadSelf, void *pvUser)
243{
244 SELF *pSelf = reinterpret_cast<SELF *>(pvUser);
245 while (!pSelf->mfExitThread)
246 RTReqProcess(pSelf->mReqQueue, RT_INDEFINITE_WAIT);
247 return VINF_SUCCESS;
248}
249
250
251/**
252 * Checking that the name passed by the guest fits our criteria for a
253 * property name.
254 *
255 * @returns IPRT status code
256 * @param pszName the name passed by the guest
257 * @param cbName the number of bytes pszName points to, including the
258 * terminating '\0'
259 * @thread HGCM
260 */
261int Service::validateName(const char *pszName, uint32_t cbName)
262{
263 LogFlowFunc(("cbName=%d\n", cbName));
264
265 /*
266 * Validate the name, checking that it's proper UTF-8 and has
267 * a string terminator.
268 */
269 int rc = RTStrValidateEncodingEx(pszName, RT_MIN(cbName, (uint32_t) MAX_NAME_LEN),
270 RTSTR_VALIDATE_ENCODING_ZERO_TERMINATED);
271 if (RT_SUCCESS(rc) && (cbName < 2))
272 rc = VERR_INVALID_PARAMETER;
273
274 LogFlowFunc(("returning %Rrc\n", rc));
275 return rc;
276}
277
278
279/**
280 * Check that the data passed by the guest fits our criteria for the value of
281 * a guest property.
282 *
283 * @returns IPRT status code
284 * @param pszValue the value to store in the property
285 * @param cbValue the number of bytes in the buffer pszValue points to
286 * @thread HGCM
287 */
288int Service::validateValue(char *pszValue, uint32_t cbValue)
289{
290 LogFlowFunc(("cbValue=%d\n", cbValue));
291
292 /*
293 * Validate the value, checking that it's proper UTF-8 and has
294 * a string terminator. Don't pass a 0 length request to the
295 * validator since it won't find any '\0' then.
296 */
297 int rc = VINF_SUCCESS;
298 if (cbValue)
299 rc = RTStrValidateEncodingEx(pszValue, RT_MIN(cbValue, (uint32_t) MAX_VALUE_LEN),
300 RTSTR_VALIDATE_ENCODING_ZERO_TERMINATED);
301 if (RT_SUCCESS(rc))
302 LogFlow((" pszValue=%s\n", cbValue > 0 ? pszValue : NULL));
303 LogFlowFunc(("returning %Rrc\n", rc));
304 return rc;
305}
306
307
308/**
309 * Retrieve a value from the property registry by name, checking the validity
310 * of the arguments passed. If the guest has not allocated enough buffer
311 * space for the value then we return VERR_OVERFLOW and set the size of the
312 * buffer needed in the "size" HGCM parameter. If the name was not found at
313 * all, we return VERR_NOT_FOUND.
314 *
315 * @returns iprt status value
316 * @param cParms the number of HGCM parameters supplied
317 * @param paParms the array of HGCM parameters
318 * @thread HGCM
319 */
320int Service::getPropValue(uint32_t cParms, VBOXHGCMSVCPARM paParms[])
321{
322 int rc = VINF_SUCCESS;
323 char *pszName, *pszValue;
324 uint32_t cbName, cbValue;
325 size_t cbValueActual;
326
327 LogFlowThisFunc(("\n"));
328 AssertReturn(VALID_PTR(mpValueNode), VERR_WRONG_ORDER); /* a.k.a. VERR_NOT_INITIALIZED */
329 if ( (cParms != 3) /* Hardcoded value as the next lines depend on it. */
330 || (paParms[0].type != VBOX_HGCM_SVC_PARM_PTR) /* name */
331 || (paParms[1].type != VBOX_HGCM_SVC_PARM_PTR) /* value */
332 )
333 rc = VERR_INVALID_PARAMETER;
334 if (RT_SUCCESS(rc))
335 rc = VBoxHGCMParmPtrGet(&paParms[0], (void **) &pszName, &cbName);
336 if (RT_SUCCESS(rc))
337 rc = VBoxHGCMParmPtrGet(&paParms[1], (void **) &pszValue, &cbValue);
338 if (RT_SUCCESS(rc))
339 rc = validateName(pszName, cbName);
340 if (RT_SUCCESS(rc))
341 rc = CFGMR3QuerySize(mpValueNode, pszName, &cbValueActual);
342 if (RT_SUCCESS(rc))
343 VBoxHGCMParmUInt32Set(&paParms[2], cbValueActual);
344 if (RT_SUCCESS(rc) && (cbValueActual > cbValue))
345 rc = VERR_BUFFER_OVERFLOW;
346 if (RT_SUCCESS(rc))
347 rc = CFGMR3QueryString(mpValueNode, pszName, pszValue, cbValue);
348 if (RT_SUCCESS(rc))
349 Log2(("Queried string %s, rc=%Rrc, value=%.*s\n", pszName, rc, cbValue, pszValue));
350 else if (VERR_CFGM_VALUE_NOT_FOUND == rc)
351 rc = VERR_NOT_FOUND;
352 LogFlowThisFunc(("rc = %Rrc\n", rc));
353 return rc;
354}
355
356
357/**
358 * Retrieve a value from the property registry by name, checking the validity
359 * of the arguments passed. If the guest has not allocated enough buffer
360 * space for the value then we return VERR_OVERFLOW and set the size of the
361 * buffer needed in the "size" HGCM parameter. If the name was not found at
362 * all, we return VERR_NOT_FOUND.
363 *
364 * @returns iprt status value
365 * @param cParms the number of HGCM parameters supplied
366 * @param paParms the array of HGCM parameters
367 * @thread HGCM
368 */
369int Service::getProperty(uint32_t cParms, VBOXHGCMSVCPARM paParms[])
370{
371 int rc = VINF_SUCCESS;
372 char *pszName, *pchBuf;
373 uint32_t cchName, cchBuf;
374 size_t cchValue, cchFlags, cchBufActual;
375 char szFlags[MAX_FLAGS_LEN];
376 uint32_t fFlags;
377
378 /*
379 * Get and validate the parameters
380 */
381 LogFlowThisFunc(("\n"));
382 AssertReturn(VALID_PTR(mpValueNode), VERR_WRONG_ORDER); /* a.k.a. VERR_NOT_INITIALIZED */
383 if ( (cParms != 4) /* Hardcoded value as the next lines depend on it. */
384 || (paParms[0].type != VBOX_HGCM_SVC_PARM_PTR) /* name */
385 || (paParms[1].type != VBOX_HGCM_SVC_PARM_PTR) /* buffer */
386 )
387 rc = VERR_INVALID_PARAMETER;
388 if (RT_SUCCESS(rc))
389 rc = VBoxHGCMParmPtrGet(&paParms[0], (void **) &pszName, &cchName);
390 if (RT_SUCCESS(rc))
391 rc = VBoxHGCMParmPtrGet(&paParms[1], (void **) &pchBuf, &cchBuf);
392 if (RT_SUCCESS(rc))
393 rc = validateName(pszName, cchName);
394
395 /*
396 * Read and set the values we will return
397 */
398
399 /* Get the value size */
400 if (RT_SUCCESS(rc))
401 rc = CFGMR3QuerySize(mpValueNode, pszName, &cchValue);
402 /* Get the flags and their size */
403 if (RT_SUCCESS(rc))
404 CFGMR3QueryU32(mpFlagsNode, pszName, (uint32_t *)&fFlags);
405 if (RT_SUCCESS(rc))
406 rc = writeFlags(fFlags, szFlags);
407 if (RT_SUCCESS(rc))
408 cchFlags = strlen(szFlags);
409 /* Check that the buffer is big enough */
410 if (RT_SUCCESS(rc))
411 {
412 cchBufActual = cchValue + cchFlags;
413 VBoxHGCMParmUInt32Set(&paParms[3], cchBufActual);
414 }
415 if (RT_SUCCESS(rc) && (cchBufActual > cchBuf))
416 rc = VERR_BUFFER_OVERFLOW;
417 /* Write the value */
418 if (RT_SUCCESS(rc))
419 rc = CFGMR3QueryString(mpValueNode, pszName, pchBuf, cchBuf);
420 /* Write the flags */
421 if (RT_SUCCESS(rc))
422 strcpy(pchBuf + cchValue, szFlags);
423 /* Timestamp */
424 uint64_t u64Timestamp = 0;
425 if (RT_SUCCESS(rc))
426 CFGMR3QueryU64(mpTimestampNode, pszName, &u64Timestamp);
427 VBoxHGCMParmUInt64Set(&paParms[2], u64Timestamp);
428
429 /*
430 * Done! Do exit logging and return.
431 */
432 if (RT_SUCCESS(rc))
433 Log2(("Queried string %S, value=%.*S, timestamp=%lld, flags=%.*S\n",
434 pszName, cchValue, pchBuf, u64Timestamp, cchFlags,
435 pchBuf + cchValue));
436 else if (VERR_CFGM_VALUE_NOT_FOUND == rc)
437 rc = VERR_NOT_FOUND;
438 LogFlowThisFunc(("rc = %Rrc\n", rc));
439 return rc;
440}
441
442
443/**
444 * Set a value in the property registry by name, checking the validity
445 * of the arguments passed.
446 *
447 * @returns iprt status value
448 * @param cParms the number of HGCM parameters supplied
449 * @param paParms the array of HGCM parameters
450 * @thread HGCM
451 */
452int Service::setProperty(uint32_t cParms, VBOXHGCMSVCPARM paParms[], bool isGuest)
453{
454 int rc = VINF_SUCCESS;
455 char *pszName, *pszValue;
456 uint32_t cchName, cchValue;
457 uint32_t fFlags = NILFLAG;
458
459 LogFlowThisFunc(("\n"));
460 AssertReturn(VALID_PTR(mpValueNode), VERR_WRONG_ORDER); /* a.k.a. VERR_NOT_INITIALIZED */
461 /*
462 * First of all, make sure that we won't exceed the maximum number of properties.
463 */
464 {
465 unsigned cChildren = 0;
466 for (PCFGMNODE pChild = CFGMR3GetFirstChild(mpValueNode); pChild != 0; pChild = CFGMR3GetNextChild(pChild))
467 ++cChildren;
468 if (cChildren >= MAX_PROPS)
469 rc = VERR_TOO_MUCH_DATA;
470 }
471 /*
472 * General parameter correctness checking.
473 */
474 if ( RT_SUCCESS(rc)
475 && ( (cParms < 2) || (cParms > 4) /* Hardcoded value as the next lines depend on it. */
476 || (paParms[0].type != VBOX_HGCM_SVC_PARM_PTR) /* name */
477 || (paParms[1].type != VBOX_HGCM_SVC_PARM_PTR) /* value */
478 || ((3 == cParms) && (paParms[2].type != VBOX_HGCM_SVC_PARM_PTR)) /* flags */
479 )
480 )
481 rc = VERR_INVALID_PARAMETER;
482 /*
483 * Check the values passed in the parameters for correctness.
484 */
485 if (RT_SUCCESS(rc))
486 rc = VBoxHGCMParmPtrGet(&paParms[0], (void **) &pszName, &cchName);
487 if (RT_SUCCESS(rc))
488 rc = VBoxHGCMParmPtrGet(&paParms[1], (void **) &pszValue, &cchValue);
489 if (RT_SUCCESS(rc))
490 rc = validateName(pszName, cchName);
491 if (RT_SUCCESS(rc))
492 rc = validateValue(pszValue, cchValue);
493
494 /*
495 * If the property already exists, check its flags to see if we are allowed
496 * to change it.
497 */
498 if (RT_SUCCESS(rc))
499 {
500 CFGMR3QueryU32(mpFlagsNode, pszName, &fFlags); /* Failure is no problem here. */
501 if ( (fFlags & READONLY)
502 || (isGuest && (fFlags & HOSTWRITE))
503 || (!isGuest && (fFlags & GUESTWRITE))
504 )
505 rc = VERR_PERMISSION_DENIED;
506 }
507
508 /*
509 * Check whether the user supplied flags (if any) are valid.
510 */
511 if (RT_SUCCESS(rc) && (3 == cParms))
512 {
513 char *pszFlags;
514 uint32_t cchFlags;
515 rc = VBoxHGCMParmPtrGet(&paParms[2], (void **) &pszFlags, &cchFlags);
516 if (RT_SUCCESS(rc))
517 rc = validateFlags(pszFlags, &fFlags);
518 }
519 /*
520 * Set the actual value
521 */
522 if (RT_SUCCESS(rc))
523 {
524 RTTIMESPEC time;
525 CFGMR3RemoveValue(mpValueNode, pszName);
526 CFGMR3RemoveValue(mpTimestampNode, pszName);
527 CFGMR3RemoveValue(mpFlagsNode, pszName);
528 rc = CFGMR3InsertString(mpValueNode, pszName, pszValue);
529 if (RT_SUCCESS(rc))
530 rc = CFGMR3InsertInteger(mpTimestampNode, pszName,
531 RTTimeSpecGetNano(RTTimeNow(&time)));
532 if (RT_SUCCESS(rc))
533 rc = CFGMR3InsertInteger(mpFlagsNode, pszName, fFlags);
534 /* If anything goes wrong, make sure that we leave a clean state
535 * behind. */
536 if (RT_FAILURE(rc))
537 {
538 CFGMR3RemoveValue(mpValueNode, pszName);
539 CFGMR3RemoveValue(mpTimestampNode, pszName);
540 CFGMR3RemoveValue(mpFlagsNode, pszName);
541 }
542 }
543 /*
544 * Send a notification to the host and return.
545 */
546 if (RT_SUCCESS(rc))
547 {
548 if (isGuest)
549 notifyHost(pszName);
550 Log2(("Set string %s, rc=%Rrc, value=%s\n", pszName, rc, pszValue));
551 }
552 LogFlowThisFunc(("rc = %Rrc\n", rc));
553 return rc;
554}
555
556
557/**
558 * Remove a value in the property registry by name, checking the validity
559 * of the arguments passed.
560 *
561 * @returns iprt status value
562 * @param cParms the number of HGCM parameters supplied
563 * @param paParms the array of HGCM parameters
564 * @thread HGCM
565 */
566int Service::delProperty(uint32_t cParms, VBOXHGCMSVCPARM paParms[], bool isGuest)
567{
568 int rc = VINF_SUCCESS;
569 char *pszName;
570 uint32_t cbName;
571
572 LogFlowThisFunc(("\n"));
573 AssertReturn(VALID_PTR(mpValueNode), VERR_WRONG_ORDER); /* a.k.a. VERR_NOT_INITIALIZED */
574
575 /*
576 * Check the user-supplied parameters.
577 */
578 if ( (cParms != 1) /* Hardcoded value as the next lines depend on it. */
579 || (paParms[0].type != VBOX_HGCM_SVC_PARM_PTR) /* name */
580 )
581 rc = VERR_INVALID_PARAMETER;
582 if (RT_SUCCESS(rc))
583 rc = VBoxHGCMParmPtrGet(&paParms[0], (void **) &pszName, &cbName);
584 if (RT_SUCCESS(rc))
585 rc = validateName(pszName, cbName);
586
587 /*
588 * If the property already exists, check its flags to see if we are allowed
589 * to change it.
590 */
591 if (RT_SUCCESS(rc))
592 {
593 uint32_t fFlags = NILFLAG;
594 CFGMR3QueryU32(mpFlagsNode, pszName, &fFlags); /* Failure is no problem here. */
595 if ( (fFlags & READONLY)
596 || (isGuest && (fFlags & HOSTWRITE))
597 || (!isGuest && (fFlags & GUESTWRITE))
598 )
599 rc = VERR_PERMISSION_DENIED;
600 }
601
602 /*
603 * And delete the property if all is well.
604 */
605 if (RT_SUCCESS(rc))
606 {
607 CFGMR3RemoveValue(mpValueNode, pszName);
608 if (isGuest)
609 notifyHost(pszName);
610 }
611 LogFlowThisFunc(("rc = %Rrc\n", rc));
612 return rc;
613}
614
615/**
616 * Enumerate guest properties by mask, checking the validity
617 * of the arguments passed.
618 *
619 * @returns iprt status value
620 * @param cParms the number of HGCM parameters supplied
621 * @param paParms the array of HGCM parameters
622 * @thread HGCM
623 */
624int Service::enumProps(uint32_t cParms, VBOXHGCMSVCPARM paParms[])
625{
626 /* We reallocate the temporary buffer in which we build up our array in
627 * increments of size BLOCK: */
628 enum
629 {
630 /* Calculate the increment, not yet rounded down */
631 BLOCKINCRFULL = (MAX_NAME_LEN + MAX_VALUE_LEN + MAX_FLAGS_LEN + 2048),
632 /* And this is the increment after rounding */
633 BLOCKINCR = BLOCKINCRFULL - BLOCKINCRFULL % 1024
634 };
635 int rc = VINF_SUCCESS;
636
637/*
638 * Get the HGCM function arguments.
639 */
640 char *paszPatterns = NULL, *pchBuf = NULL;
641 uint32_t cchPatterns = 0, cchBuf = 0;
642 LogFlowThisFunc(("\n"));
643 AssertReturn(VALID_PTR(mpValueNode), VERR_WRONG_ORDER); /* a.k.a. VERR_NOT_INITIALIZED */
644 if ( (cParms != 3) /* Hardcoded value as the next lines depend on it. */
645 || (paParms[0].type != VBOX_HGCM_SVC_PARM_PTR) /* patterns */
646 || (paParms[1].type != VBOX_HGCM_SVC_PARM_PTR) /* return buffer */
647 )
648 rc = VERR_INVALID_PARAMETER;
649 if (RT_SUCCESS(rc))
650 rc = VBoxHGCMParmPtrGet(&paParms[0], (void **) &paszPatterns, &cchPatterns);
651 if (RT_SUCCESS(rc) && cchPatterns > MAX_PATTERN_LEN)
652 rc = VERR_TOO_MUCH_DATA;
653 if (RT_SUCCESS(rc))
654 rc = VBoxHGCMParmPtrGet(&paParms[1], (void **) &pchBuf, &cchBuf);
655
656/*
657 * First repack the patterns into the format expected by RTStrSimplePatternMatch()
658 */
659 bool matchAll = false;
660 char pszPatterns[MAX_PATTERN_LEN];
661 if ( (NULL == paszPatterns)
662 || (cchPatterns < 2) /* An empty pattern string means match all */
663 )
664 matchAll = true;
665 else
666 {
667 for (unsigned i = 0; i < cchPatterns - 1; ++i)
668 if (paszPatterns[i] != '\0')
669 pszPatterns[i] = paszPatterns[i];
670 else
671 pszPatterns[i] = '|';
672 pszPatterns[cchPatterns - 1] = '\0';
673 }
674
675/*
676 * Next enumerate all values in the current node into a temporary buffer.
677 */
678 RTMemAutoPtr<char> TmpBuf;
679 uint32_t cchTmpBuf = 0, iTmpBuf = 0;
680 PCFGMLEAF pLeaf = CFGMR3GetFirstValue(mpValueNode);
681 while ((pLeaf != NULL) && RT_SUCCESS(rc))
682 {
683 /* Reallocate the buffer if it has got too tight */
684 if (iTmpBuf + BLOCKINCR > cchTmpBuf)
685 {
686 cchTmpBuf += BLOCKINCR * 2;
687 if (!TmpBuf.realloc(cchTmpBuf))
688 rc = VERR_NO_MEMORY;
689 }
690 /* Fetch the name into the buffer and if it matches one of the
691 * patterns, add its value and an empty timestamp and flags. If it
692 * doesn't match, we simply overwrite it in the buffer. */
693 if (RT_SUCCESS(rc))
694 rc = CFGMR3GetValueName(pLeaf, &TmpBuf[iTmpBuf], cchTmpBuf - iTmpBuf);
695 /* Only increment the buffer offest if the name matches, otherwise we
696 * overwrite it next iteration. */
697 if ( RT_SUCCESS(rc)
698 && ( matchAll
699 || RTStrSimplePatternMultiMatch(pszPatterns, RTSTR_MAX,
700 &TmpBuf[iTmpBuf], RTSTR_MAX,
701 NULL)
702 )
703 )
704 {
705 const char *pszName = &TmpBuf[iTmpBuf];
706 /* Get value */
707 iTmpBuf += strlen(&TmpBuf[iTmpBuf]) + 1;
708 rc = CFGMR3QueryString(mpValueNode, pszName, &TmpBuf[iTmpBuf],
709 cchTmpBuf - iTmpBuf);
710 if (RT_SUCCESS(rc))
711 {
712 /* Get timestamp */
713 iTmpBuf += strlen(&TmpBuf[iTmpBuf]) + 1;
714 uint64_t u64Timestamp = 0;
715 CFGMR3QueryU64(mpTimestampNode, pszName, &u64Timestamp);
716 iTmpBuf += RTStrFormatNumber(&TmpBuf[iTmpBuf], u64Timestamp,
717 10, 0, 0, 0) + 1;
718 /* Get flags */
719 uint32_t fFlags = NILFLAG;
720 CFGMR3QueryU32(mpFlagsNode, pszName, &fFlags);
721 TmpBuf[iTmpBuf] = '\0'; /* Bad (== in)sanity, will be fixed. */
722 writeFlags(fFlags, &TmpBuf[iTmpBuf]);
723 iTmpBuf += strlen(&TmpBuf[iTmpBuf]) + 1;
724 }
725 }
726 if (RT_SUCCESS(rc))
727 pLeaf = CFGMR3GetNextValue(pLeaf);
728 }
729 if (RT_SUCCESS(rc))
730 {
731 /* The terminator. We *do* have space left for this. */
732 TmpBuf[iTmpBuf] = '\0';
733 TmpBuf[iTmpBuf + 1] = '\0';
734 TmpBuf[iTmpBuf + 2] = '\0';
735 TmpBuf[iTmpBuf + 3] = '\0';
736 iTmpBuf += 4;
737 VBoxHGCMParmUInt32Set(&paParms[2], iTmpBuf);
738 /* Copy the memory if it fits into the guest buffer */
739 if (iTmpBuf <= cchBuf)
740 memcpy(pchBuf, TmpBuf.get(), iTmpBuf);
741 else
742 rc = VERR_BUFFER_OVERFLOW;
743 }
744 return rc;
745}
746
747/**
748 * Notify the service owner that a property has been added/deleted/changed
749 * @param pszProperty the name of the property which has changed
750 * @note this call allocates memory which the reqNotify request is expected to
751 * free again, using RTStrFree().
752 *
753 * @thread HGCM service
754 */
755void Service::notifyHost(const char *pszProperty)
756{
757 char szValue[MAX_VALUE_LEN];
758 uint64_t u64Timestamp = 0;
759 uint32_t fFlags = NILFLAG;
760 char szFlags[MAX_FLAGS_LEN];
761 char *pszName = NULL, *pszValue = NULL, *pszFlags = NULL;
762
763 AssertPtr(mpValueNode);
764 if (NULL == mpfnHostCallback)
765 return; /* Nothing to do. */
766 int rc = CFGMR3QueryString(mpValueNode, pszProperty, szValue,
767 sizeof(szValue));
768 /*
769 * First case: if the property exists then send the host its current value
770 */
771 if (rc != VERR_CFGM_VALUE_NOT_FOUND)
772 {
773 if (RT_SUCCESS(rc))
774 rc = CFGMR3QueryU64(mpTimestampNode, pszProperty, &u64Timestamp);
775 if (RT_SUCCESS(rc))
776 rc = CFGMR3QueryU32(mpFlagsNode, pszProperty, &fFlags);
777 if (RT_SUCCESS(rc))
778 rc = writeFlags(fFlags, szFlags);
779 if (RT_SUCCESS(rc))
780 rc = RTStrDupEx(&pszName, pszProperty);
781 if (RT_SUCCESS(rc))
782 rc = RTStrDupEx(&pszValue, szValue);
783 if (RT_SUCCESS(rc))
784 rc = RTStrDupEx(&pszFlags, szFlags);
785 if (RT_SUCCESS(rc))
786 rc = RTReqCallEx(mReqQueue, NULL, 0, RTREQFLAGS_NO_WAIT,
787 (PFNRT)Service::reqNotify, 7, mpfnHostCallback,
788 mpvHostData, pszName, pszValue,
789 (uint32_t) RT_HIDWORD(u64Timestamp),
790 (uint32_t) RT_LODWORD(u64Timestamp), pszFlags);
791 if (RT_FAILURE(rc)) /* clean up */
792 {
793 RTStrFree(pszName);
794 RTStrFree(pszValue);
795 RTStrFree(pszFlags);
796 }
797 }
798 else
799 /*
800 * Second case: if the property does not exist then send the host an empty
801 * value
802 */
803 {
804 rc = RTStrDupEx(&pszName, pszProperty);
805 if (RT_SUCCESS(rc))
806 rc = RTReqCallEx(mReqQueue, NULL, 0, RTREQFLAGS_NO_WAIT,
807 (PFNRT)Service::reqNotify, 7, mpfnHostCallback,
808 mpvHostData, pszName, NULL, 0, 0, NULL);
809 }
810 if (RT_FAILURE(rc)) /* clean up if we failed somewhere */
811 {
812 RTStrFree(pszName);
813 RTStrFree(pszValue);
814 RTStrFree(pszFlags);
815 }
816}
817
818/**
819 * Notify the service owner that a property has been added/deleted/changed.
820 * asynchronous part.
821 * @param pszProperty the name of the property which has changed
822 * @note this call allocates memory which the reqNotify request is expected to
823 * free again, using RTStrFree().
824 *
825 * @thread request thread
826 */
827int Service::reqNotify(PFNHGCMSVCEXT pfnCallback, void *pvData,
828 char *pszName, char *pszValue, uint32_t u32TimeHigh,
829 uint32_t u32TimeLow, char *pszFlags)
830{
831 HOSTCALLBACKDATA HostCallbackData;
832 HostCallbackData.u32Magic = HOSTCALLBACKMAGIC;
833 HostCallbackData.pcszName = pszName;
834 HostCallbackData.pcszValue = pszValue;
835 HostCallbackData.u64Timestamp = RT_MAKE_U64(u32TimeLow, u32TimeHigh);
836 HostCallbackData.pcszFlags = pszFlags;
837 AssertRC(pfnCallback(pvData, 0, reinterpret_cast<void *>(&HostCallbackData),
838 sizeof(HostCallbackData)));
839 RTStrFree(pszName);
840 RTStrFree(pszValue);
841 RTStrFree(pszFlags);
842 return VINF_SUCCESS;
843}
844
845
846/**
847 * Handle an HGCM service call.
848 * @copydoc VBOXHGCMSVCFNTABLE::pfnCall
849 * @note All functions which do not involve an unreasonable delay will be
850 * handled synchronously. If needed, we will add a request handler
851 * thread in future for those which do.
852 *
853 * @thread HGCM
854 */
855void Service::call (VBOXHGCMCALLHANDLE callHandle, uint32_t u32ClientID,
856 void * /* pvClient */, uint32_t eFunction, uint32_t cParms,
857 VBOXHGCMSVCPARM paParms[])
858{
859 int rc = VINF_SUCCESS;
860 bool fCallSync = true;
861
862 LogFlowFunc(("u32ClientID = %d, fn = %d, cParms = %d, pparms = %d\n",
863 u32ClientID, eFunction, cParms, paParms));
864
865 switch (eFunction)
866 {
867 /* The guest wishes to read a property */
868 case GET_PROP:
869 LogFlowFunc(("GET_PROP\n"));
870 rc = getProperty(cParms, paParms);
871 break;
872
873 /* The guest wishes to set a property */
874 case SET_PROP:
875 LogFlowFunc(("SET_PROP\n"));
876 rc = setProperty(cParms, paParms, true);
877 break;
878
879 /* The guest wishes to set a property value */
880 case SET_PROP_VALUE:
881 LogFlowFunc(("SET_PROP_VALUE\n"));
882 rc = setProperty(cParms, paParms, true);
883 break;
884
885 /* The guest wishes to remove a configuration value */
886 case DEL_PROP:
887 LogFlowFunc(("DEL_PROP\n"));
888 rc = delProperty(cParms, paParms, true);
889 break;
890
891 /* The guest wishes to enumerate all properties */
892 case ENUM_PROPS:
893 LogFlowFunc(("ENUM_PROPS\n"));
894 rc = enumProps(cParms, paParms);
895 break;
896 default:
897 rc = VERR_NOT_IMPLEMENTED;
898 }
899 if (fCallSync)
900 {
901 LogFlowFunc(("rc = %Rrc\n", rc));
902 mpHelpers->pfnCallComplete (callHandle, rc);
903 }
904}
905
906
907/**
908 * Service call handler for the host.
909 * @copydoc VBOXHGCMSVCFNTABLE::pfnHostCall
910 * @thread hgcm
911 */
912int Service::hostCall (uint32_t eFunction, uint32_t cParms, VBOXHGCMSVCPARM paParms[])
913{
914 int rc = VINF_SUCCESS;
915
916 LogFlowFunc(("fn = %d, cParms = %d, pparms = %d\n",
917 eFunction, cParms, paParms));
918
919 switch (eFunction)
920 {
921 /* Set the root CFGM node used. This should be called when instantiating
922 * the service. */
923 /* This whole case is due to go away, so I will not clean it up. */
924 case SET_CFGM_NODE:
925 {
926 LogFlowFunc(("SET_CFGM_NODE\n"));
927
928 if ( (cParms != 3)
929 || (paParms[0].type != VBOX_HGCM_SVC_PARM_PTR) /* pValue */
930 || (paParms[1].type != VBOX_HGCM_SVC_PARM_PTR) /* pTimestamp */
931 || (paParms[2].type != VBOX_HGCM_SVC_PARM_PTR) /* pFlags */
932 )
933 rc = VERR_INVALID_PARAMETER;
934 else
935 {
936 PCFGMNODE pValue = NULL, pTimestamp = NULL, pFlags = NULL;
937 uint32_t cbDummy;
938
939 rc = VBoxHGCMParmPtrGet (&paParms[0], (void **) &pValue, &cbDummy);
940 if (RT_SUCCESS(rc))
941 rc = VBoxHGCMParmPtrGet (&paParms[1], (void **) &pTimestamp, &cbDummy);
942 if (RT_SUCCESS(rc))
943 rc = VBoxHGCMParmPtrGet (&paParms[2], (void **) &pFlags, &cbDummy);
944 if (RT_SUCCESS(rc))
945 {
946 mpValueNode = pValue;
947 mpTimestampNode = pTimestamp;
948 mpFlagsNode = pFlags;
949 }
950 }
951 } break;
952
953 /* The host wishes to read a configuration value */
954 case GET_PROP_HOST:
955 LogFlowFunc(("GET_PROP_HOST\n"));
956 rc = getProperty(cParms, paParms);
957 break;
958
959 /* The host wishes to set a configuration value */
960 case SET_PROP_HOST:
961 LogFlowFunc(("SET_PROP_HOST\n"));
962 rc = setProperty(cParms, paParms, false);
963 break;
964
965 /* The host wishes to set a configuration value */
966 case SET_PROP_VALUE_HOST:
967 LogFlowFunc(("SET_PROP_VALUE_HOST\n"));
968 rc = setProperty(cParms, paParms, false);
969 break;
970
971 /* The host wishes to remove a configuration value */
972 case DEL_PROP_HOST:
973 LogFlowFunc(("DEL_PROP_HOST\n"));
974 rc = delProperty(cParms, paParms, false);
975 break;
976
977 /* The host wishes to enumerate all properties */
978 case ENUM_PROPS_HOST:
979 LogFlowFunc(("ENUM_PROPS\n"));
980 rc = enumProps(cParms, paParms);
981 break;
982 default:
983 rc = VERR_NOT_SUPPORTED;
984 break;
985 }
986
987 LogFlowFunc(("rc = %Vrc\n", rc));
988 return rc;
989}
990
991int Service::uninit()
992{
993 int rc;
994 unsigned count = 0;
995
996 mfExitThread = true;
997 rc = RTReqCallEx(mReqQueue, NULL, 0, RTREQFLAGS_NO_WAIT, (PFNRT)reqVoid, 0);
998 if (RT_SUCCESS(rc))
999 do
1000 {
1001 rc = RTThreadWait(mReqThread, 1000, NULL);
1002 ++count;
1003 Assert(RT_SUCCESS(rc) || ((VERR_TIMEOUT == rc) && (count != 5)));
1004 } while ((VERR_TIMEOUT == rc) && (count < 300));
1005 if (RT_SUCCESS(rc))
1006 RTReqDestroyQueue(mReqQueue);
1007 return rc;
1008}
1009
1010} /* namespace guestProp */
1011
1012using guestProp::Service;
1013
1014/**
1015 * @copydoc VBOXHGCMSVCLOAD
1016 */
1017extern "C" DECLCALLBACK(DECLEXPORT(int)) VBoxHGCMSvcLoad (VBOXHGCMSVCFNTABLE *ptable)
1018{
1019 int rc = VINF_SUCCESS;
1020
1021 LogFlowFunc(("ptable = %p\n", ptable));
1022
1023 if (!VALID_PTR(ptable))
1024 {
1025 rc = VERR_INVALID_PARAMETER;
1026 }
1027 else
1028 {
1029 LogFlowFunc(("ptable->cbSize = %d, ptable->u32Version = 0x%08X\n", ptable->cbSize, ptable->u32Version));
1030
1031 if ( ptable->cbSize != sizeof (VBOXHGCMSVCFNTABLE)
1032 || ptable->u32Version != VBOX_HGCM_SVC_VERSION)
1033 {
1034 rc = VERR_VERSION_MISMATCH;
1035 }
1036 else
1037 {
1038 std::auto_ptr<Service> apService;
1039 /* No exceptions may propogate outside. */
1040 try {
1041 apService = std::auto_ptr<Service>(new Service(ptable->pHelpers));
1042 } catch (int rcThrown) {
1043 rc = rcThrown;
1044 } catch (...) {
1045 rc = VERR_UNRESOLVED_ERROR;
1046 }
1047
1048 if (RT_SUCCESS(rc))
1049 {
1050 /* We do not maintain connections, so no client data is needed. */
1051 ptable->cbClient = 0;
1052
1053 ptable->pfnUnload = Service::svcUnload;
1054 ptable->pfnConnect = Service::svcConnectDisconnect;
1055 ptable->pfnDisconnect = Service::svcConnectDisconnect;
1056 ptable->pfnCall = Service::svcCall;
1057 ptable->pfnHostCall = Service::svcHostCall;
1058 ptable->pfnSaveState = NULL; /* The service is stateless by definition, so the */
1059 ptable->pfnLoadState = NULL; /* normal construction done before restoring suffices */
1060 ptable->pfnRegisterExtension = Service::svcRegisterExtension;
1061
1062 /* Service specific initialization. */
1063 ptable->pvService = apService.release();
1064 }
1065 }
1066 }
1067
1068 LogFlowFunc(("returning %Rrc\n", rc));
1069 return rc;
1070}
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