VirtualBox

source: vbox/trunk/src/VBox/Additions/common/VBoxGuestLib/VBoxGuestR3LibGuestProp.cpp@ 14214

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

Additions/Guest Library: add R3 property wait API

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 34.1 KB
Line 
1/* $Id: VBoxGuestR3LibGuestProp.cpp 14214 2008-11-14 14:47:38Z vboxsync $ */
2/** @file
3 * VBoxGuestR3Lib - Ring-3 Support Library for VirtualBox guest additions,
4 * guest properties.
5 */
6
7/*
8 * Copyright (C) 2007 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/*******************************************************************************
25* Header Files *
26*******************************************************************************/
27#include <iprt/string.h>
28#include <iprt/mem.h>
29#include <iprt/assert.h>
30#include <iprt/autores.h>
31#include <iprt/stdarg.h>
32#include <VBox/log.h>
33#include <VBox/HostServices/GuestPropertySvc.h>
34
35#include "VBGLR3Internal.h"
36
37
38/*******************************************************************************
39* Structures and Typedefs *
40*******************************************************************************/
41/**
42 * Structure containing information needed to enumerate through guest
43 * properties.
44 */
45struct VBGLR3GUESTPROPENUM
46{
47 /** @todo add a magic and validate the handle. */
48 /** The buffer containing the raw enumeration data */
49 char *pchBuf;
50 /** The end of the buffer */
51 char *pchBufEnd;
52 /** Pointer to the next entry to enumerate inside the buffer */
53 char *pchNext;
54};
55
56using namespace guestProp;
57
58/**
59 * Connects to the guest property service.
60 *
61 * @returns VBox status code
62 * @param pu32ClientId Where to put the client id on success. The client id
63 * must be passed to all the other calls to the service.
64 */
65VBGLR3DECL(int) VbglR3GuestPropConnect(uint32_t *pu32ClientId)
66{
67 VBoxGuestHGCMConnectInfo Info;
68 Info.result = (uint32_t)VERR_WRONG_ORDER; /** @todo drop the cast when the result type has been fixed! */
69 Info.Loc.type = VMMDevHGCMLoc_LocalHost_Existing;
70 memset(&Info.Loc.u, 0, sizeof(Info.Loc.u));
71 strcpy(Info.Loc.u.host.achName, "VBoxGuestPropSvc");
72 Info.u32ClientID = UINT32_MAX; /* try make valgrid shut up. */
73
74 int rc = vbglR3DoIOCtl(VBOXGUEST_IOCTL_HGCM_CONNECT, &Info, sizeof(Info));
75 if (RT_SUCCESS(rc))
76 {
77 rc = Info.result;
78 if (RT_SUCCESS(rc))
79 *pu32ClientId = Info.u32ClientID;
80 }
81 return rc;
82}
83
84
85/**
86 * Disconnect from the guest property service.
87 *
88 * @returns VBox status code.
89 * @param u32ClientId The client id returned by VbglR3InfoSvcConnect().
90 */
91VBGLR3DECL(int) VbglR3GuestPropDisconnect(uint32_t u32ClientId)
92{
93 VBoxGuestHGCMDisconnectInfo Info;
94 Info.result = (uint32_t)VERR_WRONG_ORDER; /** @todo drop the cast when the result type has been fixed! */
95 Info.u32ClientID = u32ClientId;
96
97 int rc = vbglR3DoIOCtl(VBOXGUEST_IOCTL_HGCM_DISCONNECT, &Info, sizeof(Info));
98 if (RT_SUCCESS(rc))
99 rc = Info.result;
100 return rc;
101}
102
103
104/**
105 * Write a property value.
106 *
107 * @returns VBox status code.
108 * @param u32ClientId The client id returned by VbglR3InvsSvcConnect().
109 * @param pszName The property to save to. Utf8
110 * @param pszValue The value to store. Utf8. If this is NULL then
111 * the property will be removed.
112 * @param pszFlags The flags for the property
113 */
114VBGLR3DECL(int) VbglR3GuestPropWrite(uint32_t u32ClientId, const char *pszName, const char *pszValue, const char *pszFlags)
115{
116 int rc;
117
118 if (pszValue != NULL)
119 {
120 SetProperty Msg;
121
122 Msg.hdr.result = (uint32_t)VERR_WRONG_ORDER; /** @todo drop the cast when the result type has been fixed! */
123 Msg.hdr.u32ClientID = u32ClientId;
124 Msg.hdr.u32Function = SET_PROP_VALUE;
125 Msg.hdr.cParms = 3;
126 VbglHGCMParmPtrSet(&Msg.name, const_cast<char *>(pszName), strlen(pszName) + 1);
127 VbglHGCMParmPtrSet(&Msg.value, const_cast<char *>(pszValue), strlen(pszValue) + 1);
128 VbglHGCMParmPtrSet(&Msg.flags, const_cast<char *>(pszFlags), strlen(pszFlags) + 1);
129 rc = vbglR3DoIOCtl(VBOXGUEST_IOCTL_HGCM_CALL(sizeof(Msg)), &Msg, sizeof(Msg));
130 if (RT_SUCCESS(rc))
131 rc = Msg.hdr.result;
132 }
133 else
134 {
135 DelProperty Msg;
136
137 Msg.hdr.result = (uint32_t)VERR_WRONG_ORDER; /** @todo drop the cast when the result type has been fixed! */
138 Msg.hdr.u32ClientID = u32ClientId;
139 Msg.hdr.u32Function = DEL_PROP;
140 Msg.hdr.cParms = 1;
141 VbglHGCMParmPtrSet(&Msg.name, const_cast<char *>(pszName), strlen(pszName) + 1);
142 rc = vbglR3DoIOCtl(VBOXGUEST_IOCTL_HGCM_CALL(sizeof(Msg)), &Msg, sizeof(Msg));
143 if (RT_SUCCESS(rc))
144 rc = Msg.hdr.result;
145 }
146 return rc;
147}
148
149
150/**
151 * Write a property value.
152 *
153 * @returns VBox status code.
154 *
155 * @param u32ClientId The client id returned by VbglR3InvsSvcConnect().
156 * @param pszName The property to save to. Must be valid UTF-8.
157 * @param pszValue The value to store. Must be valid UTF-8.
158 * If this is NULL then the property will be removed.
159 *
160 * @note if the property already exists and pszValue is not NULL then the
161 * property's flags field will be left unchanged
162 */
163VBGLR3DECL(int) VbglR3GuestPropWriteValue(uint32_t u32ClientId, const char *pszName, const char *pszValue)
164{
165 int rc;
166
167 if (pszValue != NULL)
168 {
169 SetPropertyValue Msg;
170
171 Msg.hdr.result = (uint32_t)VERR_WRONG_ORDER; /** @todo drop the cast when the result type has been fixed! */
172 Msg.hdr.u32ClientID = u32ClientId;
173 Msg.hdr.u32Function = SET_PROP_VALUE;
174 Msg.hdr.cParms = 2;
175 VbglHGCMParmPtrSet(&Msg.name, const_cast<char *>(pszName), strlen(pszName) + 1);
176 VbglHGCMParmPtrSet(&Msg.value, const_cast<char *>(pszValue), strlen(pszValue) + 1);
177 rc = vbglR3DoIOCtl(VBOXGUEST_IOCTL_HGCM_CALL(sizeof(Msg)), &Msg, sizeof(Msg));
178 if (RT_SUCCESS(rc))
179 rc = Msg.hdr.result;
180 }
181 else
182 {
183 DelProperty Msg;
184
185 Msg.hdr.result = (uint32_t)VERR_WRONG_ORDER; /** @todo drop the cast when the result type has been fixed! */
186 Msg.hdr.u32ClientID = u32ClientId;
187 Msg.hdr.u32Function = DEL_PROP;
188 Msg.hdr.cParms = 1;
189 VbglHGCMParmPtrSet(&Msg.name, const_cast<char *>(pszName), strlen(pszName) + 1);
190 rc = vbglR3DoIOCtl(VBOXGUEST_IOCTL_HGCM_CALL(sizeof(Msg)), &Msg, sizeof(Msg));
191 if (RT_SUCCESS(rc))
192 rc = Msg.hdr.result;
193 }
194 return rc;
195}
196
197
198/**
199 * Write a property value where the value is formatted in RTStrPrintfV fashion.
200 *
201 * @returns The same as VbglR3GuestPropWriteValue with the addition of VERR_NO_STR_MEMORY.
202 *
203 * @param u32ClientId The client ID returned by VbglR3InvsSvcConnect().
204 * @param pszName The property to save to. Must be valid UTF-8.
205 * @param pszValueFormat The value format. This must be valid UTF-8 when fully formatted.
206 * @param va The format arguments.
207 */
208VBGLR3DECL(int) VbglR3GuestPropWriteValueV(uint32_t u32ClientId, const char *pszName, const char *pszValueFormat, va_list va)
209{
210 /*
211 * Format the value and pass it on to the setter.
212 */
213 int rc = VERR_NO_STR_MEMORY;
214 char *pszValue;
215 if (RTStrAPrintfV(&pszValue, pszValueFormat, va) < 0)
216 {
217 rc = VbglR3GuestPropWriteValue(u32ClientId, pszName, pszValue);
218 RTStrFree(pszValue);
219 }
220 return rc;
221}
222
223
224/**
225 * Write a property value where the value is formatted in RTStrPrintf fashion.
226 *
227 * @returns The same as VbglR3GuestPropWriteValue with the addition of VERR_NO_STR_MEMORY.
228 *
229 * @param u32ClientId The client ID returned by VbglR3InvsSvcConnect().
230 * @param pszName The property to save to. Must be valid UTF-8.
231 * @param pszValueFormat The value format. This must be valid UTF-8 when fully formatted.
232 * @param ... The format arguments.
233 */
234VBGLR3DECL(int) VbglR3GuestPropWriteValueF(uint32_t u32ClientId, const char *pszName, const char *pszValueFormat, ...)
235{
236 va_list va;
237 va_start(va, pszValueFormat);
238 int rc = VbglR3GuestPropWriteValueV(u32ClientId, pszName, pszValueFormat, va);
239 va_end(va);
240 return rc;
241}
242
243
244/**
245 * Retrieve a property.
246 *
247 * @returns VBox status code.
248 * @retval VINF_SUCCESS on success, pszValue, pu64Timestamp and pszFlags
249 * containing valid data.
250 * @retval VERR_BUFFER_OVERFLOW if the scratch buffer @a pcBuf is not large
251 * enough. In this case the size needed will be placed in
252 * @a pcbBufActual if it is not NULL.
253 * @retval VERR_NOT_FOUND if the key wasn't found.
254 *
255 * @param u32ClientId The client id returned by VbglR3GuestPropConnect().
256 * @param pszName The value to read. Utf8
257 * @param pvBuf A scratch buffer to store the data retrieved into.
258 * The returned data is only valid for it's lifetime.
259 * @a ppszValue will point to the start of this buffer.
260 * @param cbBuf The size of @a pcBuf
261 * @param pszValue Where to store the pointer to the value retrieved.
262 * Optional.
263 * @param pu64Timestamp Where to store the timestamp. Optional.
264 * @param pszFlags Where to store the pointer to the flags. Optional.
265 * @param pcbBufActual If @a pcBuf is not large enough, the size needed.
266 * Optional.
267 */
268VBGLR3DECL(int) VbglR3GuestPropRead(uint32_t u32ClientId, const char *pszName,
269 void *pvBuf, uint32_t cbBuf,
270 char **ppszValue, uint64_t *pu64Timestamp,
271 char **ppszFlags,
272 uint32_t *pcbBufActual)
273{
274 /*
275 * Create the GET_PROP message and call the host.
276 */
277 GetProperty Msg;
278
279 Msg.hdr.result = (uint32_t)VERR_WRONG_ORDER; /** @todo drop the cast when the result type has been fixed! */
280 Msg.hdr.u32ClientID = u32ClientId;
281 Msg.hdr.u32Function = GET_PROP;
282 Msg.hdr.cParms = 4;
283 VbglHGCMParmPtrSet(&Msg.name, const_cast<char *>(pszName), strlen(pszName) + 1);
284 VbglHGCMParmPtrSet(&Msg.buffer, pvBuf, cbBuf);
285 VbglHGCMParmUInt64Set(&Msg.timestamp, 0);
286 VbglHGCMParmUInt32Set(&Msg.size, 0);
287
288 int rc = vbglR3DoIOCtl(VBOXGUEST_IOCTL_HGCM_CALL(sizeof(Msg)), &Msg, sizeof(Msg));
289 if (RT_SUCCESS(rc))
290 rc = Msg.hdr.result;
291
292 /*
293 * The cbBufActual parameter is also returned on overflow so the call can
294 * adjust his/her buffer.
295 */
296 if ( rc == VERR_BUFFER_OVERFLOW
297 || pcbBufActual != NULL)
298 {
299 int rc2 = VbglHGCMParmUInt32Get(&Msg.size, pcbBufActual);
300 AssertRCReturn(rc2, RT_FAILURE(rc) ? rc : rc2);
301 }
302 if (RT_FAILURE(rc))
303 return rc;
304
305 /*
306 * Buffer layout: Value\0Flags\0.
307 *
308 * If the caller cares about any of these strings, make sure things are
309 * propertly terminated (paranoia).
310 */
311 if ( RT_SUCCESS(rc)
312 && (ppszValue != NULL || ppszFlags != NULL))
313 {
314 /* Validate / skip 'Name'. */
315 char *pszFlags = (char *)memchr(pvBuf, '\0', cbBuf) + 1;
316 AssertPtrReturn(pszFlags, VERR_TOO_MUCH_DATA);
317 if (ppszValue)
318 *ppszValue = (char *)pvBuf;
319
320 if (ppszFlags)
321 {
322 /* Validate 'Flags'. */
323 void *pvEos = memchr(pszFlags, '\0', cbBuf - (pszFlags - (char *)pvBuf));
324 AssertPtrReturn(pvEos, VERR_TOO_MUCH_DATA);
325 *ppszFlags = pszFlags;
326 }
327 }
328
329 /* And the timestamp, if requested. */
330 if (pu64Timestamp != NULL)
331 {
332 rc = VbglHGCMParmUInt64Get(&Msg.timestamp, pu64Timestamp);
333 AssertRCReturn(rc, rc);
334 }
335
336 return VINF_SUCCESS;
337}
338
339
340/**
341 * Retrieve a property value, allocating space for it.
342 *
343 * @returns VBox status code.
344 * @retval VINF_SUCCESS on success, *ppszValue containing valid data.
345 * @retval VERR_NOT_FOUND if the key wasn't found.
346 * @retval VERR_TOO_MUCH_DATA if we were unable to determine the right size
347 * to allocate for the buffer. This can happen as the result of a
348 * race between our allocating space and the host changing the
349 * property value.
350 *
351 * @param u32ClientId The client id returned by VbglR3GuestPropConnect().
352 * @param pszName The value to read. Must be valid UTF-8.
353 * @param ppszValue Where to store the pointer to the value returned.
354 * This is always set to NULL or to the result, even
355 * on failure.
356 */
357VBGLR3DECL(int) VbglR3GuestPropReadValueAlloc(uint32_t u32ClientId,
358 const char *pszName,
359 char **ppszValue)
360{
361 /*
362 * Quick input validation.
363 */
364 AssertPtr(ppszValue);
365 *ppszValue = NULL;
366 AssertPtrReturn(pszName, VERR_INVALID_PARAMETER);
367
368 /*
369 * There is a race here between our reading the property size and the
370 * host changing the value before we read it. Try up to ten times and
371 * report the problem if that fails.
372 */
373 char *pszValue = NULL;
374 void *pvBuf = NULL;
375 uint32_t cchBuf = MAX_VALUE_LEN;
376 int rc = VERR_BUFFER_OVERFLOW;
377 for (unsigned i = 0; i < 10 && rc == VERR_BUFFER_OVERFLOW; ++i)
378 {
379 /* We leave a bit of space here in case the maximum value is raised. */
380 cchBuf += 1024;
381 void *pvTmpBuf = RTMemRealloc(pvBuf, cchBuf);
382 if (pvTmpBuf)
383 {
384 pvBuf = pvTmpBuf;
385 rc = VbglR3GuestPropRead(u32ClientId, pszName, pvBuf, cchBuf,
386 &pszValue, NULL, NULL, &cchBuf);
387 }
388 else
389 rc = VERR_NO_MEMORY;
390 }
391 if (RT_SUCCESS(rc))
392 {
393 Assert(pszValue == (char *)pvBuf);
394 *ppszValue = pszValue;
395 }
396 else
397 {
398 RTMemFree(pvBuf);
399 if (rc == VERR_BUFFER_OVERFLOW)
400 /* VERR_BUFFER_OVERFLOW has a different meaning here as a
401 * return code, but we need to report the race. */
402 rc = VERR_TOO_MUCH_DATA;
403 }
404
405 return rc;
406}
407
408
409/**
410 * Free the memory used by VbglR3GuestPropReadValueAlloc for returning a
411 * value.
412 *
413 * @param pszValue the memory to be freed. NULL pointers will be ignored.
414 */
415VBGLR3DECL(void) VbglR3GuestPropReadValueFree(char *pszValue)
416{
417 RTMemFree(pszValue);
418}
419
420
421/**
422 * Retrieve a property value, using a user-provided buffer to store it.
423 *
424 * @returns VBox status code.
425 * @retval VINF_SUCCESS on success, pszValue containing valid data.
426 * @retval VERR_BUFFER_OVERFLOW and the size needed in pcchValueActual if the
427 * buffer provided was too small
428 * @retval VERR_NOT_FOUND if the key wasn't found.
429 *
430 * @note There is a race here between obtaining the size of the buffer
431 * needed to hold the value and the value being updated.
432 *
433 * @param u32ClientId The client id returned by VbglR3GuestPropConnect().
434 * @param pszName The value to read. Utf8
435 * @param pszValue Where to store the value retrieved.
436 * @param cchValue The size of the buffer pointed to by @a pszValue
437 * @param pcchValueActual Where to store the size of the buffer needed if
438 * the buffer supplied is too small. Optional.
439 */
440VBGLR3DECL(int) VbglR3GuestPropReadValue(uint32_t u32ClientId, const char *pszName,
441 char *pszValue, uint32_t cchValue,
442 uint32_t *pcchValueActual)
443{
444 char *pcBuf = NULL;
445 int rc = VbglR3GuestPropReadValueAlloc(u32ClientId, pszName, &pcBuf);
446 if (RT_SUCCESS(rc))
447 {
448 uint32_t cchValueActual = strlen(pcBuf) + 1;
449 if (cchValueActual > cchValue)
450 {
451 if (pcchValueActual != NULL)
452 *pcchValueActual = cchValueActual;
453 rc = VERR_BUFFER_OVERFLOW;
454 }
455 if (RT_SUCCESS(rc))
456 strcpy(pszValue, pcBuf);
457 }
458 VbglR3GuestPropReadValueFree(pcBuf);
459 return rc;
460}
461
462
463/**
464 * Raw API for enumerating guest properties which match a given pattern.
465 *
466 * @returns VBox status code.
467 * @retval VINF_SUCCESS on success and pcBuf points to a packed array
468 * of the form <name>, <value>, <timestamp string>, <flags>,
469 * terminated by four empty strings. pcbBufActual will contain the
470 * total size of the array.
471 * @retval VERR_BUFFER_OVERFLOW if the buffer provided was too small. In
472 * this case pcbBufActual will contain the size of the buffer needed.
473 * @returns IPRT error code in other cases, and pchBufActual is undefined.
474 *
475 * @param u32ClientId The client ID returned by VbglR3GuestPropConnect
476 * @param paszPatterns A packed array of zero terminated strings, terminated
477 * by an empty string.
478 * @param pcBuf The buffer to store the results to.
479 * @param cbBuf The size of the buffer
480 * @param pcbBufActual Where to store the size of the returned data on
481 * success or the buffer size needed if @a pcBuf is too
482 * small.
483 */
484VBGLR3DECL(int) VbglR3GuestPropEnumRaw(uint32_t u32ClientId,
485 const char *pszzPatterns,
486 char *pcBuf,
487 uint32_t cbBuf,
488 uint32_t *pcbBufActual)
489{
490 EnumProperties Msg;
491
492 Msg.hdr.result = (uint32_t)VERR_WRONG_ORDER; /** @todo drop the cast when the result type has been fixed! */
493 Msg.hdr.u32ClientID = u32ClientId;
494 Msg.hdr.u32Function = ENUM_PROPS;
495 Msg.hdr.cParms = 3;
496 /* Get the length of the patterns array... */
497 uint32_t cchPatterns = 0;
498 for (uint32_t cchCurrent = strlen(pszzPatterns); cchCurrent != 0;
499 cchCurrent = strlen(pszzPatterns + cchPatterns))
500 cchPatterns += cchCurrent + 1;
501 /* ...including the terminator. */
502 ++cchPatterns;
503 VbglHGCMParmPtrSet(&Msg.patterns, const_cast<char *>(pszzPatterns),
504 cchPatterns);
505 VbglHGCMParmPtrSet(&Msg.strings, pcBuf, cbBuf);
506 VbglHGCMParmUInt32Set(&Msg.size, 0);
507
508 int rc = vbglR3DoIOCtl(VBOXGUEST_IOCTL_HGCM_CALL(sizeof(Msg)), &Msg, sizeof(Msg));
509 if (RT_SUCCESS(rc))
510 rc = Msg.hdr.result;
511 if ( pcbBufActual
512 && ( RT_SUCCESS(rc)
513 || rc == VERR_BUFFER_OVERFLOW))
514 {
515 int rc2 = VbglHGCMParmUInt32Get(&Msg.size, pcbBufActual);
516 if (!RT_SUCCESS(rc2))
517 rc = rc2;
518 }
519 return rc;
520}
521
522
523/**
524 * Start enumerating guest properties which match a given pattern.
525 * This function creates a handle which can be used to continue enumerating.
526 *
527 * @returns VBox status code.
528 * @retval VINF_SUCCESS on success, *ppHandle points to a handle for continuing
529 * the enumeration and *ppszName, *ppszValue, *pu64Timestamp and
530 * *ppszFlags are set.
531 * @retval VERR_NOT_FOUND if no matching properties were found. In this case
532 * the return parameters are not initialised.
533 * @retval VERR_TOO_MUCH_DATA if it was not possible to determine the amount
534 * of local space needed to store all the enumeration data. This is
535 * due to a race between allocating space and the host adding new
536 * data, so retrying may help here. Other parameters are left
537 * uninitialised
538 *
539 * @param u32ClientId The client id returned by VbglR3InfoSvcConnect().
540 * @param papszPatterns The patterns against which the properties are
541 * matched. Pass NULL if everything should be matched.
542 * @param cPatterns The number of patterns in @a papszPatterns. 0 means
543 * match everything.
544 * @param ppHandle where the handle for continued enumeration is stored
545 * on success. This must be freed with
546 * VbglR3GuestPropEnumFree when it is no longer needed.
547 * @param ppszName Where to store the first property name on success.
548 * Should not be freed. Optional.
549 * @param ppszValue Where to store the first property value on success.
550 * Should not be freed. Optional.
551 * @param ppszValue Where to store the first timestamp value on success.
552 * Optional.
553 * @param ppszFlags Where to store the first flags value on success.
554 * Should not be freed. Optional.
555 */
556VBGLR3DECL(int) VbglR3GuestPropEnum(uint32_t u32ClientId,
557 char const * const *papszPatterns,
558 uint32_t cPatterns,
559 PVBGLR3GUESTPROPENUM *ppHandle,
560 char const **ppszName,
561 char const **ppszValue,
562 uint64_t *pu64Timestamp,
563 char const **ppszFlags)
564{
565 /* Create the handle. */
566 RTMemAutoPtr<VBGLR3GUESTPROPENUM, VbglR3GuestPropEnumFree> Handle;
567 Handle = (PVBGLR3GUESTPROPENUM)RTMemAllocZ(sizeof(VBGLR3GUESTPROPENUM));
568 if (!Handle)
569 return VERR_NO_MEMORY;
570
571 /* Get the length of the pattern string, including the final terminator. */
572 uint32_t cchPatterns = 1;
573 for (unsigned i = 0; i < cPatterns; ++i)
574 cchPatterns += strlen(papszPatterns[i]) + 1;
575
576 /* Pack the pattern array */
577 RTMemAutoPtr<char> Patterns;
578 Patterns = (char *)RTMemAlloc(cchPatterns);
579 uint32_t iOffs = 0;
580 for (uint32_t i = 0; i < cPatterns; ++i)
581 {
582 uint32_t cb = strlen(papszPatterns[i]) + 1;
583 memcpy(&Patterns[iOffs], papszPatterns[i], cb);
584 iOffs += cb;
585 }
586 Patterns[iOffs] = '\0';
587
588 /* Randomly chosen initial size for the buffer to hold the enumeration
589 * information. */
590 uint32_t cchBuf = 4096;
591 RTMemAutoPtr<char> Buf;
592
593 /* In reading the guest property data we are racing against the host
594 * adding more of it, so loop a few times and retry on overflow. */
595 int rc = VINF_SUCCESS;
596 for (int i = 0; i < 10; ++i)
597 {
598 if (!Buf.realloc(cchBuf))
599 {
600 rc = VERR_NO_MEMORY;
601 break;
602 }
603 rc = VbglR3GuestPropEnumRaw(u32ClientId, Patterns.get(),
604 Buf.get(), cchBuf, &cchBuf);
605 if (rc != VERR_BUFFER_OVERFLOW)
606 break;
607 cchBuf += 4096; /* Just to increase our chances */
608 }
609 if (RT_SUCCESS(rc))
610 {
611 /*
612 * Transfer ownership of the buffer to the handle structure and
613 * call VbglR3GuestPropEnumNext to retriev the first entry.
614 */
615 Handle->pchNext = Handle->pchBuf = Buf.release();
616 Handle->pchBufEnd = Handle->pchBuf + cchBuf;
617
618 const char *pszNameTmp;
619 if (!ppszName)
620 ppszName = &pszNameTmp;
621 rc = VbglR3GuestPropEnumNext(Handle.get(), ppszName, ppszValue,
622 pu64Timestamp, ppszFlags);
623 if (RT_SUCCESS(rc) && *ppszName != NULL)
624 *ppHandle = Handle.release();
625 else if (RT_SUCCESS(rc))
626 rc = VERR_NOT_FOUND; /* No matching properties found. */
627 }
628 else if (rc == VERR_BUFFER_OVERFLOW)
629 rc = VERR_TOO_MUCH_DATA;
630 return rc;
631}
632
633
634/**
635 * Get the next guest property.
636 *
637 * See @a VbglR3GuestPropEnum.
638 *
639 * @returns VBox status code.
640 *
641 * @param pHandle Handle obtained from @a VbglR3GuestPropEnum.
642 * @param ppszName Where to store the next property name. This will be
643 * set to NULL if there are no more properties to
644 * enumerate. This pointer should not be freed. Optional.
645 * @param ppszValue Where to store the next property value. This will be
646 * set to NULL if there are no more properties to
647 * enumerate. This pointer should not be freed. Optional.
648 * @param pu64Timestamp Where to store the next property timestamp. This
649 * will be set to zero if there are no more properties
650 * to enumerate. Optional.
651 * @param ppszFlags Where to store the next property flags. This will be
652 * set to NULL if there are no more properties to
653 * enumerate. This pointer should not be freed. Optional.
654 *
655 * @remarks While all output parameters are optional, you need at least one to
656 * figure out when to stop.
657 */
658VBGLR3DECL(int) VbglR3GuestPropEnumNext(PVBGLR3GUESTPROPENUM pHandle,
659 char const **ppszName,
660 char const **ppszValue,
661 uint64_t *pu64Timestamp,
662 char const **ppszFlags)
663{
664 /*
665 * The VBGLR3GUESTPROPENUM structure contains a buffer containing the raw
666 * properties data and a pointer into the buffer which tracks how far we
667 * have parsed so far. The buffer contains packed strings in groups of
668 * four - name, value, timestamp (as a decimal string) and flags. It is
669 * terminated by four empty strings. We can rely on this layout unless
670 * the caller has been poking about in the structure internals, in which
671 * case they must take responsibility for the results.
672 *
673 * Layout:
674 * Name\0Value\0Timestamp\0Flags\0
675 */
676 char *pchNext = pHandle->pchNext; /* The cursor. */
677 char *pchEnd = pHandle->pchBufEnd; /* End of buffer, for size calculations. */
678
679 char *pszName = pchNext;
680 char *pszValue = pchNext = (char *)memchr(pchNext, '\0', pchEnd - pchNext) + 1;
681 AssertPtrReturn(pchNext, VERR_PARSE_ERROR); /* 0x1 is also an invalid pointer :) */
682
683 char *pszTimestamp = pchNext = (char *)memchr(pchNext, '\0', pchEnd - pchNext) + 1;
684 AssertPtrReturn(pchNext, VERR_PARSE_ERROR);
685
686 char *pszFlags = pchNext = (char *)memchr(pchNext, '\0', pchEnd - pchNext) + 1;
687 AssertPtrReturn(pchNext, VERR_PARSE_ERROR);
688
689 /*
690 * Don't move the index pointer if we found the terminating "\0\0\0\0" entry.
691 * Don't try convert the timestamp either.
692 */
693 uint64_t u64Timestamp;
694 if (*pszName != '\0')
695 {
696 pchNext = (char *)memchr(pchNext, '\0', pchEnd - pchNext) + 1;
697 AssertPtrReturn(pchNext, VERR_PARSE_ERROR);
698
699 /* Convert the timestamp string into a number. */
700 int rc = RTStrToUInt64Full(pszTimestamp, 0, &u64Timestamp);
701 AssertRCSuccessReturn(rc, VERR_PARSE_ERROR);
702
703 pHandle->pchNext = pchNext;
704 AssertPtr(pchNext);
705 }
706 else
707 {
708 u64Timestamp = 0;
709 AssertMsgReturn(!*pszValue && !*pszTimestamp && !*pszFlags,
710 ("'%s' '%s' '%s'\n", pszValue, pszTimestamp, pszFlags),
711 VERR_PARSE_ERROR);
712 }
713
714 /*
715 * Everything is fine, set the return values.
716 */
717 if (ppszName)
718 *ppszName = *pszName != '\0' ? pszName : NULL;
719 if (ppszValue)
720 *ppszValue = *pszValue != '\0' ? pszValue : NULL;
721 if (pu64Timestamp)
722 *pu64Timestamp = u64Timestamp;
723 if (ppszFlags)
724 *ppszFlags = *pszFlags != '\0' ? pszFlags : NULL;
725 return VINF_SUCCESS;
726}
727
728
729/**
730 * Free an enumeration handle returned by @a VbglR3GuestPropEnum.
731 * @param pHandle the handle to free
732 */
733VBGLR3DECL(void) VbglR3GuestPropEnumFree(PVBGLR3GUESTPROPENUM pHandle)
734{
735 RTMemFree(pHandle->pchBuf);
736 RTMemFree(pHandle);
737}
738
739
740/**
741 * Deletes a set of keys.
742 *
743 * The set is specified in the same way as for VbglR3GuestPropEnum.
744 *
745 * @returns VBox status code. Stops on first failure.
746 * See also VbglR3GuestPropEnum.
747 *
748 * @param u32ClientId The client id returned by VbglR3InfoSvcConnect().
749 * @param papszPatterns The patterns against which the properties are
750 * matched. Pass NULL if everything should be matched.
751 * @param cPatterns The number of patterns in @a papszPatterns. 0 means
752 * match everything.
753 */
754VBGLR3DECL(int) VbglR3GuestPropDelSet(uint32_t u32ClientId,
755 const char * const *papszPatterns,
756 uint32_t cPatterns)
757{
758 PVBGLR3GUESTPROPENUM pHandle;
759 char const *pszName, *pszValue, *pszFlags;
760 uint64_t pu64Timestamp;
761 int rc = VbglR3GuestPropEnum(u32ClientId,
762 (char **)papszPatterns, /** @todo fix this cast. */
763 cPatterns,
764 &pHandle,
765 &pszName,
766 &pszValue,
767 &pu64Timestamp,
768 &pszFlags);
769
770 while (RT_SUCCESS(rc) && pszName)
771 {
772 rc = VbglR3GuestPropWriteValue(u32ClientId, pszName, NULL);
773 if (!RT_SUCCESS(rc))
774 break;
775
776 rc = VbglR3GuestPropEnumNext(pHandle,
777 &pszName,
778 &pszValue,
779 &pu64Timestamp,
780 &pszFlags);
781 }
782
783 VbglR3GuestPropEnumFree(pHandle);
784 return rc;
785}
786
787
788/**
789 * Wait for notification of changes to a guest property. If this is called in
790 * a loop, the timestamp of the last notification seen can be passed as a
791 * parameter to be sure that no notifications are missed.
792 *
793 * @returns VBox status code.
794 * @retval VINF_SUCCESS on success, @a ppszName, @a ppszValue,
795 * @a pu64Timestamp and @a ppszFlags containing valid data.
796 * @retval VINF_NOT_FOUND if no previous notification could be found with the
797 * timestamp supplied. This will normally mean that a large number
798 * of notifications occurred in between.
799 * @retval VERR_BUFFER_OVERFLOW if the scratch buffer @a pvBuf is not large
800 * enough. In this case the size needed will be placed in
801 * @a pcbBufActual if it is not NULL.
802 * @retval VERR_TIMEOUT if a timeout occurred before a notification was seen.
803 *
804 * @param u32ClientId The client id returned by VbglR3GuestPropConnect().
805 * @param pszPatterns The patterns that the property names must matchfor
806 * the change to be reported.
807 * @param pvBuf A scratch buffer to store the data retrieved into.
808 * The returned data is only valid for it's lifetime.
809 * @a ppszValue will point to the start of this buffer.
810 * @param cbBuf The size of @a pvBuf
811 * @param u64Timestamp The timestamp of the last event seen. Pass zero
812 * to wait for the next event.
813 * @param u32Timeout Timeout in milliseconds. Use RT_INDEFINITE_WAIT
814 * to wait indefinitely.
815 * @param ppszName Where to store the pointer to the name retrieved.
816 * Optional.
817 * @param ppszValue Where to store the pointer to the value retrieved.
818 * Optional.
819 * @param pu64Timestamp Where to store the timestamp. Optional.
820 * @param ppszFlags Where to store the pointer to the flags. Optional.
821 * @param pcbBufActual If @a pcBuf is not large enough, the size needed.
822 * Optional.
823 */
824VBGLR3DECL(int) VbglR3GuestPropWait(uint32_t u32ClientId,
825 const char *pszPatterns,
826 void *pvBuf, uint32_t cbBuf,
827 uint64_t u64Timestamp, uint32_t u32Timeout,
828 char ** ppszName, char **ppszValue,
829 uint64_t *pu64Timestamp, char **ppszFlags,
830 uint32_t *pcbBufActual)
831{
832 /*
833 * Create the GET_NOTIFICATION message and call the host.
834 */
835 GetNotification Msg;
836
837 Msg.hdr.u32Timeout = u32Timeout;
838 Msg.hdr.info.result = (uint32_t)VERR_WRONG_ORDER; /** @todo drop the cast when the result type has been fixed! */
839 Msg.hdr.info.u32ClientID = u32ClientId;
840 Msg.hdr.info.u32Function = GET_NOTIFICATION;
841 Msg.hdr.info.cParms = 4;
842 Msg.patterns.SetPtr(const_cast<char *>(pszPatterns),
843 strlen(pszPatterns) + 1);
844 Msg.buffer.SetPtr(pvBuf, cbBuf);
845 Msg.timestamp.SetUInt64(u64Timestamp);
846 Msg.size.SetUInt32(0);
847
848 int rc = vbglR3DoIOCtl(VBOXGUEST_IOCTL_HGCM_CALL_TIMEOUT(sizeof(Msg)), &Msg, sizeof(Msg));
849 if (RT_SUCCESS(rc))
850 rc = Msg.hdr.info.result;
851
852 /*
853 * The cbBufActual parameter is also returned on overflow so the caller can
854 * adjust their buffer.
855 */
856 if ( rc == VERR_BUFFER_OVERFLOW
857 || pcbBufActual != NULL)
858 {
859 int rc2 = Msg.size.GetUInt32(pcbBufActual);
860 AssertRCReturn(rc2, RT_FAILURE(rc) ? rc : rc2);
861 }
862 if (RT_FAILURE(rc))
863 return rc;
864
865 /*
866 * Buffer layout: Name\0Value\0Flags\0.
867 *
868 * If the caller cares about any of these strings, make sure things are
869 * propertly terminated (paranoia).
870 */
871 if ( RT_SUCCESS(rc)
872 && (ppszName != NULL || ppszValue != NULL || ppszFlags != NULL))
873 {
874 /* Validate / skip 'Name'. */
875 char *pszValue = (char *)memchr(pvBuf, '\0', cbBuf) + 1;
876 AssertPtrReturn(pszValue, VERR_TOO_MUCH_DATA);
877 if (ppszName)
878 *ppszName = (char *)pvBuf;
879
880 /* Validate / skip 'Value'. */
881 char *pszFlags = (char *)memchr(pszValue, '\0',
882 cbBuf - (pszValue - (char *)pvBuf)) + 1;
883 AssertPtrReturn(pszFlags, VERR_TOO_MUCH_DATA);
884 if (ppszValue)
885 *ppszValue = pszValue;
886
887 if (ppszFlags)
888 {
889 /* Validate 'Flags'. */
890 void *pvEos = memchr(pszFlags, '\0', cbBuf - (pszFlags - (char *)pvBuf));
891 AssertPtrReturn(pvEos, VERR_TOO_MUCH_DATA);
892 *ppszFlags = pszFlags;
893 }
894 }
895
896 /* And the timestamp, if requested. */
897 if (pu64Timestamp != NULL)
898 {
899 rc = Msg.timestamp.GetUInt64(pu64Timestamp);
900 AssertRCReturn(rc, rc);
901 }
902
903 return VINF_SUCCESS;
904}
905
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