VirtualBox

source: vbox/trunk/include/VBox/HostServices/GuestPropertySvc.h@ 70060

Last change on this file since 70060 was 70060, checked in by vboxsync, 7 years ago

GuestPropertySvc.h: Working on making it usable from C (VBoxGuest, ++)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 17.3 KB
Line 
1/** @file
2 * Guest property service:
3 * Common header for host service and guest clients.
4 */
5
6/*
7 * Copyright (C) 2006-2017 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27#ifndef ___VBox_HostService_GuestPropertyService_h
28#define ___VBox_HostService_GuestPropertyService_h
29
30#include <VBox/VMMDevCoreTypes.h>
31#include <VBox/VBoxGuestCoreTypes.h>
32#include <VBox/log.h>
33#include <VBox/hgcmsvc.h>
34#include <iprt/assertcompile.h>
35#include <iprt/string.h>
36
37
38/** Maximum length for property names */
39#define GUEST_PROP_MAX_NAME_LEN 64
40/** Maximum length for property values */
41#define GUEST_PROP_MAX_VALUE_LEN 128
42/** Maximum number of properties per guest */
43#define GUEST_PROP_MAX_PROPS 256
44/** Maximum size for enumeration patterns */
45#define GUEST_PROP_MAX_PATTERN_LEN 1024
46/** Maximum number of changes we remember for guest notifications */
47#define GUEST_PROP_MAX_GUEST_NOTIFICATIONS 256
48
49
50/** @name GUEST_PROP_F_XXX - The guest property flag values which are currently accepted.
51 * @{
52 */
53#define GUEST_PROP_F_NILFLAG UINT32_C(0)
54/** Transient until VM gets shut down. */
55#define GUEST_PROP_F_TRANSIENT RT_BIT_32(1)
56#define GUEST_PROP_F_RDONLYGUEST RT_BIT_32(2)
57#define GUEST_PROP_F_RDONLYHOST RT_BIT_32(3)
58/** Transient until VM gets a reset / restarts.
59 * Implies TRANSIENT. */
60#define GUEST_PROP_F_TRANSRESET RT_BIT_32(4)
61#define GUEST_PROP_F_READONLY (GUEST_PROP_F_RDONLYGUEST | GUEST_PROP_F_RDONLYHOST)
62#define GUEST_PROP_F_ALLFLAGS (GUEST_PROP_F_TRANSIENT | GUEST_PROP_F_READONLY | GUEST_PROP_F_TRANSRESET)
63/** @} */
64
65/**
66 * Get the name of a flag as a string.
67 * @returns the name, or NULL if fFlag is invalid.
68 * @param fFlag the flag. Must be a value from the ePropFlags enumeration
69 * list.
70 */
71DECLINLINE(const char *) GuestPropFlagName(uint32_t fFlag)
72{
73 switch (fFlag)
74 {
75 case GUEST_PROP_F_TRANSIENT:
76 return "TRANSIENT";
77 case GUEST_PROP_F_RDONLYGUEST:
78 return "RDONLYGUEST";
79 case GUEST_PROP_F_RDONLYHOST:
80 return "RDONLYHOST";
81 case GUEST_PROP_F_READONLY:
82 return "READONLY";
83 case GUEST_PROP_F_TRANSRESET:
84 return "TRANSRESET";
85 default:
86 break;
87 }
88 return NULL;
89}
90
91/**
92 * Get the length of a flag name as returned by flagName.
93 * @returns the length, or 0 if fFlag is invalid.
94 * @param fFlag the flag. Must be a value from the ePropFlags enumeration
95 * list.
96 */
97DECLINLINE(size_t) GuestPropFlagNameLen(uint32_t fFlag)
98{
99 const char *pcszName = GuestPropFlagName(fFlag);
100 return RT_LIKELY(pcszName != NULL) ? strlen(pcszName) : 0;
101}
102
103/**
104 * Maximum length for the property flags field. We only ever return one of
105 * RDONLYGUEST, RDONLYHOST and RDONLY
106 */
107#define GUEST_PROP_MAX_FLAGS_LEN sizeof("TRANSIENT, RDONLYGUEST, TRANSRESET")
108
109/**
110 * Parse a guest properties flags string for flag names and make sure that
111 * there is no junk text in the string.
112 *
113 * @returns IPRT status code
114 * @retval VERR_INVALID_PARAMETER if the flag string is not valid
115 * @param pcszFlags the flag string to parse
116 * @param pfFlags where to store the parse result. May not be NULL.
117 * @note This function is also inline because it must be accessible from
118 * several modules and it does not seem reasonable to put it into
119 * its own library.
120 */
121DECLINLINE(int) GuestPropValidateFlags(const char *pcszFlags, uint32_t *pfFlags)
122{
123 static const uint32_t s_aFlagList[] =
124 {
125 GUEST_PROP_F_TRANSIENT, GUEST_PROP_F_READONLY, GUEST_PROP_F_RDONLYGUEST, GUEST_PROP_F_RDONLYHOST, GUEST_PROP_F_TRANSRESET
126 };
127 const char *pcszNext = pcszFlags;
128 int rc = VINF_SUCCESS;
129 uint32_t fFlags = 0;
130 AssertLogRelReturn(VALID_PTR(pfFlags), VERR_INVALID_POINTER);
131
132 if (pcszFlags)
133 {
134 while (' ' == *pcszNext)
135 ++pcszNext;
136 while ((*pcszNext != '\0') && RT_SUCCESS(rc))
137 {
138 unsigned i = 0;
139 for (; i < RT_ELEMENTS(s_aFlagList); ++i)
140 if (RTStrNICmp(pcszNext, GuestPropFlagName(s_aFlagList[i]), GuestPropFlagNameLen(s_aFlagList[i])) == 0)
141 break;
142 if (RT_ELEMENTS(s_aFlagList) == i)
143 rc = VERR_PARSE_ERROR;
144 else
145 {
146 fFlags |= s_aFlagList[i];
147 pcszNext += GuestPropFlagNameLen(s_aFlagList[i]);
148 while (' ' == *pcszNext)
149 ++pcszNext;
150 if (',' == *pcszNext)
151 ++pcszNext;
152 else if (*pcszNext != '\0')
153 rc = VERR_PARSE_ERROR;
154 while (' ' == *pcszNext)
155 ++pcszNext;
156 }
157 }
158 }
159 if (RT_SUCCESS(rc))
160 *pfFlags = fFlags;
161 return rc;
162}
163
164
165/**
166 * Write out flags to a string.
167 * @returns IPRT status code
168 * @param fFlags the flags to write out
169 * @param pszFlags where to write the flags string. This must point to
170 * a buffer of size (at least) MAX_FLAGS_LEN.
171 */
172DECLINLINE(int) GuestPropWriteFlags(uint32_t fFlags, char *pszFlags)
173{
174 /* Putting READONLY before the other RDONLY flags keeps the result short. */
175 static const uint32_t s_aFlagList[] =
176 {
177 GUEST_PROP_F_TRANSIENT, GUEST_PROP_F_READONLY, GUEST_PROP_F_RDONLYGUEST, GUEST_PROP_F_RDONLYHOST, GUEST_PROP_F_TRANSRESET
178 };
179 int rc = VINF_SUCCESS;
180
181 AssertLogRelReturn(VALID_PTR(pszFlags), VERR_INVALID_POINTER);
182 if ((fFlags & ~GUEST_PROP_F_ALLFLAGS) == GUEST_PROP_F_NILFLAG)
183 {
184 char *pszNext;
185
186 /* TRANSRESET implies TRANSIENT. For compatability with old clients we
187 always set TRANSIENT when TRANSRESET appears. */
188 if (fFlags & GUEST_PROP_F_TRANSRESET)
189 fFlags |= GUEST_PROP_F_TRANSIENT;
190
191 pszNext = pszFlags;
192 for (unsigned i = 0; i < RT_ELEMENTS(s_aFlagList); ++i)
193 {
194 if (s_aFlagList[i] == (fFlags & s_aFlagList[i]))
195 {
196 strcpy(pszNext, GuestPropFlagName(s_aFlagList[i]));
197 pszNext += GuestPropFlagNameLen(s_aFlagList[i]);
198 fFlags &= ~s_aFlagList[i];
199 if (fFlags != GUEST_PROP_F_NILFLAG)
200 {
201 strcpy(pszNext, ", ");
202 pszNext += 2;
203 }
204 }
205 }
206 *pszNext = '\0';
207
208 Assert(fFlags == GUEST_PROP_F_NILFLAG); /* bad s_aFlagList */
209 }
210 else
211 rc = VERR_INVALID_PARAMETER;
212 return rc;
213}
214
215
216/** @name The service functions which are callable by host.
217 * @{
218 */
219/** Set properties in a block.
220 * The parameters are pointers to NULL-terminated arrays containing the
221 * parameters. These are, in order, name, value, timestamp, flags. Strings are
222 * stored as pointers to mutable utf8 data. All parameters must be supplied. */
223#define GUEST_PROP_FN_SET_PROPS_HOST 1
224/** Get the value attached to a guest property.
225 * The parameter format matches that of GET_PROP. */
226#define GUEST_PROP_FN_GET_PROP_HOST 2
227/** Set the value attached to a guest property.
228 * The parameter format matches that of SET_PROP. */
229#define GUEST_PROP_FN_SET_PROP_HOST 3
230/** Set the value attached to a guest property.
231 * The parameter format matches that of SET_PROP_VALUE. */
232#define GUEST_PROP_FN_SET_PROP_VALUE_HOST 4
233/** Remove a guest property.
234 * The parameter format matches that of DEL_PROP. */
235#define GUEST_PROP_FN_DEL_PROP_HOST 5
236/** Enumerate guest properties.
237 * The parameter format matches that of ENUM_PROPS. */
238#define GUEST_PROP_FN_ENUM_PROPS_HOST 6
239/** Set global flags for the service.
240 * Currently RDONLYGUEST is supported. Takes one 32-bit unsigned integer
241 * parameter for the flags. */
242#define GUEST_PROP_FN_SET_GLOBAL_FLAGS_HOST 7
243/** Return the pointer to a debug info function enumerating all guest
244 * properties. */
245#define GUEST_PROP_FN_GET_DBGF_INFO_FN 8
246/** @} */
247
248
249/** @name The service functions which are called by guest.
250 *
251 * @note The numbers may not change!
252 * @{
253 */
254/** Get a guest property */
255#define GUEST_PROP_FN_GET_PROP 1
256/** Set a guest property */
257#define GUEST_PROP_FN_SET_PROP 2
258/** Set just the value of a guest property */
259#define GUEST_PROP_FN_SET_PROP_VALUE 3
260/** Delete a guest property */
261#define GUEST_PROP_FN_DEL_PROP 4
262/** Enumerate guest properties */
263#define GUEST_PROP_FN_ENUM_PROPS 5
264/** Poll for guest notifications */
265#define GUEST_PROP_FN_GET_NOTIFICATION 6
266/** @} */
267
268
269/**
270 * Data structure to pass to the service extension callback.
271 * We use this to notify the host of changes to properties.
272 */
273typedef struct GUESTPROPHOSTCALLBACKDATA
274{
275 /** Magic number to identify the structure (GUESTPROPHOSTCALLBACKDATA_MAGIC). */
276 uint32_t u32Magic;
277 /** The name of the property that was changed */
278 const char *pcszName;
279 /** The new property value, or NULL if the property was deleted */
280 const char *pcszValue;
281 /** The timestamp of the modification */
282 uint64_t u64Timestamp;
283 /** The flags field of the modified property */
284 const char *pcszFlags;
285} GUESTPROPHOSTCALLBACKDATA;
286/** Poitner to a data structure to pass to the service extension callback. */
287typedef GUESTPROPHOSTCALLBACKDATA *PGUESTPROPHOSTCALLBACKDATA;
288
289/** Magic number for sanity checking the HOSTCALLBACKDATA structure */
290#define GUESTPROPHOSTCALLBACKDATA_MAGIC UINT32_C(0x69c87a78)
291
292/** Everything defined in this file lives in this namespace. */
293namespace guestProp {
294
295/**
296 * HGCM parameter structures. Packing is explicitly defined as this is a wire format.
297 */
298/** The guest is requesting the value of a property */
299typedef struct _GetProperty
300{
301 VBGLIOCHGCMCALL hdr;
302
303 /**
304 * The property name (IN pointer)
305 * This must fit to a number of criteria, namely
306 * - Only Utf8 strings are allowed
307 * - Less than or equal to MAX_NAME_LEN bytes in length
308 * - Zero terminated
309 */
310 HGCMFunctionParameter name;
311
312 /**
313 * The returned string data will be placed here. (OUT pointer)
314 * This call returns two null-terminated strings which will be placed one
315 * after another: value and flags.
316 */
317 HGCMFunctionParameter buffer;
318
319 /**
320 * The property timestamp. (OUT uint64_t)
321 */
322 HGCMFunctionParameter timestamp;
323
324 /**
325 * If the buffer provided was large enough this will contain the size of
326 * the returned data. Otherwise it will contain the size of the buffer
327 * needed to hold the data and VERR_BUFFER_OVERFLOW will be returned.
328 * (OUT uint32_t)
329 */
330 HGCMFunctionParameter size;
331} GetProperty;
332AssertCompileSize(GetProperty, 40 + 4 * (ARCH_BITS == 64 ? 16 : 12));
333
334/** The guest is requesting to change a property */
335typedef struct _SetProperty
336{
337 VBGLIOCHGCMCALL hdr;
338
339 /**
340 * The property name. (IN pointer)
341 * This must fit to a number of criteria, namely
342 * - Only Utf8 strings are allowed
343 * - Less than or equal to MAX_NAME_LEN bytes in length
344 * - Zero terminated
345 */
346 HGCMFunctionParameter name;
347
348 /**
349 * The value of the property (IN pointer)
350 * Criteria as for the name parameter, but with length less than or equal to
351 * MAX_VALUE_LEN.
352 */
353 HGCMFunctionParameter value;
354
355 /**
356 * The property flags (IN pointer)
357 * This is a comma-separated list of the format flag=value
358 * The length must be less than or equal to MAX_FLAGS_LEN and only
359 * known flag names and values will be accepted.
360 */
361 HGCMFunctionParameter flags;
362} SetProperty;
363AssertCompileSize(SetProperty, 40 + 3 * (ARCH_BITS == 64 ? 16 : 12));
364
365/** The guest is requesting to change the value of a property */
366typedef struct _SetPropertyValue
367{
368 VBGLIOCHGCMCALL hdr;
369
370 /**
371 * The property name. (IN pointer)
372 * This must fit to a number of criteria, namely
373 * - Only Utf8 strings are allowed
374 * - Less than or equal to MAX_NAME_LEN bytes in length
375 * - Zero terminated
376 */
377 HGCMFunctionParameter name;
378
379 /**
380 * The value of the property (IN pointer)
381 * Criteria as for the name parameter, but with length less than or equal to
382 * MAX_VALUE_LEN.
383 */
384 HGCMFunctionParameter value;
385} SetPropertyValue;
386AssertCompileSize(SetPropertyValue, 40 + 2 * (ARCH_BITS == 64 ? 16 : 12));
387
388/** The guest is requesting to remove a property */
389typedef struct _DelProperty
390{
391 VBGLIOCHGCMCALL hdr;
392
393 /**
394 * The property name. This must fit to a number of criteria, namely
395 * - Only Utf8 strings are allowed
396 * - Less than or equal to MAX_NAME_LEN bytes in length
397 * - Zero terminated
398 */
399 HGCMFunctionParameter name;
400} DelProperty;
401AssertCompileSize(DelProperty, 40 + 1 * (ARCH_BITS == 64 ? 16 : 12));
402
403/** The guest is requesting to enumerate properties */
404typedef struct _EnumProperties
405{
406 VBGLIOCHGCMCALL hdr;
407
408 /**
409 * Array of patterns to match the properties against, separated by '|'
410 * characters. For backwards compatibility, '\\0' is also accepted
411 * as a separater.
412 * (IN pointer)
413 * If only a single, empty pattern is given then match all.
414 */
415 HGCMFunctionParameter patterns;
416 /**
417 * On success, null-separated array of strings in which the properties are
418 * returned. (OUT pointer)
419 * The number of strings in the array is always a multiple of four,
420 * and in sequences of name, value, timestamp (hexadecimal string) and the
421 * flags as a comma-separated list in the format "name=value". The list
422 * is terminated by an empty string after a "flags" entry (or at the
423 * start).
424 */
425 HGCMFunctionParameter strings;
426 /**
427 * On success, the size of the returned data. If the buffer provided is
428 * too small, the size of buffer needed. (OUT uint32_t)
429 */
430 HGCMFunctionParameter size;
431} EnumProperties;
432AssertCompileSize(EnumProperties, 40 + 3 * (ARCH_BITS == 64 ? 16 : 12));
433
434/**
435 * The guest is polling for notifications on changes to properties, specifying
436 * a set of patterns to match the names of changed properties against and
437 * optionally the timestamp of the last notification seen.
438 * On success, VINF_SUCCESS will be returned and the buffer will contain
439 * details of a property notification. If no new notification is available
440 * which matches one of the specified patterns, the call will block until one
441 * is.
442 * If the last notification could not be found by timestamp, VWRN_NOT_FOUND
443 * will be returned and the oldest available notification will be returned.
444 * If a zero timestamp is specified, the call will always wait for a new
445 * notification to arrive.
446 * If the buffer supplied was not large enough to hold the notification,
447 * VERR_BUFFER_OVERFLOW will be returned and the size parameter will contain
448 * the size of the buffer needed.
449 *
450 * The protocol for a guest to obtain notifications is to call
451 * GET_NOTIFICATION in a loop. On the first call, the ingoing timestamp
452 * parameter should be set to zero. On subsequent calls, it should be set to
453 * the outgoing timestamp from the previous call.
454 */
455typedef struct _GetNotification
456{
457 VBGLIOCHGCMCALL hdr;
458
459 /**
460 * A list of patterns to match the guest event name against, separated by
461 * vertical bars (|) (IN pointer)
462 * An empty string means match all.
463 */
464 HGCMFunctionParameter patterns;
465 /**
466 * The timestamp of the last change seen (IN uint64_t)
467 * This may be zero, in which case the oldest available change will be
468 * sent. If the service does not remember an event matching the
469 * timestamp, then VWRN_NOT_FOUND will be returned, and the guest should
470 * assume that it has missed a certain number of notifications.
471 *
472 * The timestamp of the change being notified of (OUT uint64_t)
473 * Undefined on failure.
474 */
475 HGCMFunctionParameter timestamp;
476
477 /**
478 * The returned data, if any, will be placed here. (OUT pointer)
479 * This call returns three null-terminated strings which will be placed
480 * one after another: name, value and flags. For a delete notification,
481 * value and flags will be empty strings. Undefined on failure.
482 */
483 HGCMFunctionParameter buffer;
484
485 /**
486 * On success, the size of the returned data. (OUT uint32_t)
487 * On buffer overflow, the size of the buffer needed to hold the data.
488 * Undefined on failure.
489 */
490 HGCMFunctionParameter size;
491} GetNotification;
492AssertCompileSize(GetNotification, 40 + 4 * (ARCH_BITS == 64 ? 16 : 12));
493
494} /* namespace guestProp */
495
496#endif /* !___VBox_HostService_GuestPropertySvc_h */
497
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette