VirtualBox

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

Last change on this file since 70063 was 70063, 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.4 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 unsigned i;
186
187 /* TRANSRESET implies TRANSIENT. For compatability with old clients we
188 always set TRANSIENT when TRANSRESET appears. */
189 if (fFlags & GUEST_PROP_F_TRANSRESET)
190 fFlags |= GUEST_PROP_F_TRANSIENT;
191
192 pszNext = pszFlags;
193 for (i = 0; i < RT_ELEMENTS(s_aFlagList); ++i)
194 {
195 if (s_aFlagList[i] == (fFlags & s_aFlagList[i]))
196 {
197 strcpy(pszNext, GuestPropFlagName(s_aFlagList[i]));
198 pszNext += GuestPropFlagNameLen(s_aFlagList[i]);
199 fFlags &= ~s_aFlagList[i];
200 if (fFlags != GUEST_PROP_F_NILFLAG)
201 {
202 strcpy(pszNext, ", ");
203 pszNext += 2;
204 }
205 }
206 }
207 *pszNext = '\0';
208
209 Assert(fFlags == GUEST_PROP_F_NILFLAG); /* bad s_aFlagList */
210 }
211 else
212 rc = VERR_INVALID_PARAMETER;
213 return rc;
214}
215
216
217/** @name The service functions which are callable by host.
218 * @{
219 */
220/** Set properties in a block.
221 * The parameters are pointers to NULL-terminated arrays containing the
222 * parameters. These are, in order, name, value, timestamp, flags. Strings are
223 * stored as pointers to mutable utf8 data. All parameters must be supplied. */
224#define GUEST_PROP_FN_HOST_SET_PROPS 1
225/** Get the value attached to a guest property.
226 * The parameter format matches that of GET_PROP. */
227#define GUEST_PROP_FN_HOST_GET_PROP 2
228/** Set the value attached to a guest property.
229 * The parameter format matches that of SET_PROP. */
230#define GUEST_PROP_FN_HOST_SET_PROP 3
231/** Set the value attached to a guest property.
232 * The parameter format matches that of SET_PROP_VALUE. */
233#define GUEST_PROP_FN_HOST_SET_PROP_VALUE 4
234/** Remove a guest property.
235 * The parameter format matches that of DEL_PROP. */
236#define GUEST_PROP_FN_HOST_DEL_PROP 5
237/** Enumerate guest properties.
238 * The parameter format matches that of ENUM_PROPS. */
239#define GUEST_PROP_FN_HOST_ENUM_PROPS 6
240/** Set global flags for the service.
241 * Currently RDONLYGUEST is supported. Takes one 32-bit unsigned integer
242 * parameter for the flags. */
243#define GUEST_PROP_FN_HOST_SET_GLOBAL_FLAGS 7
244/** Return the pointer to a debug info function enumerating all guest
245 * properties. */
246#define GUEST_PROP_FN_HOST_GET_DBGF_INFO 8
247/** @} */
248
249
250/** @name The service functions which are called by guest.
251 *
252 * @note The numbers may not change!
253 * @{
254 */
255/** Get a guest property */
256#define GUEST_PROP_FN_GET_PROP 1
257/** Set a guest property */
258#define GUEST_PROP_FN_SET_PROP 2
259/** Set just the value of a guest property */
260#define GUEST_PROP_FN_SET_PROP_VALUE 3
261/** Delete a guest property */
262#define GUEST_PROP_FN_DEL_PROP 4
263/** Enumerate guest properties */
264#define GUEST_PROP_FN_ENUM_PROPS 5
265/** Poll for guest notifications */
266#define GUEST_PROP_FN_GET_NOTIFICATION 6
267/** @} */
268
269
270/**
271 * Data structure to pass to the service extension callback.
272 * We use this to notify the host of changes to properties.
273 */
274typedef struct GUESTPROPHOSTCALLBACKDATA
275{
276 /** Magic number to identify the structure (GUESTPROPHOSTCALLBACKDATA_MAGIC). */
277 uint32_t u32Magic;
278 /** The name of the property that was changed */
279 const char *pcszName;
280 /** The new property value, or NULL if the property was deleted */
281 const char *pcszValue;
282 /** The timestamp of the modification */
283 uint64_t u64Timestamp;
284 /** The flags field of the modified property */
285 const char *pcszFlags;
286} GUESTPROPHOSTCALLBACKDATA;
287/** Poitner to a data structure to pass to the service extension callback. */
288typedef GUESTPROPHOSTCALLBACKDATA *PGUESTPROPHOSTCALLBACKDATA;
289
290/** Magic number for sanity checking the HOSTCALLBACKDATA structure */
291#define GUESTPROPHOSTCALLBACKDATA_MAGIC UINT32_C(0x69c87a78)
292
293/**
294 * HGCM parameter structures. Packing is explicitly defined as this is a wire format.
295 */
296/** The guest is requesting the value of a property */
297typedef struct GuestPropMsgGetProperty
298{
299 VBGLIOCHGCMCALL hdr;
300
301 /**
302 * The property name (IN pointer)
303 * This must fit to a number of criteria, namely
304 * - Only Utf8 strings are allowed
305 * - Less than or equal to MAX_NAME_LEN bytes in length
306 * - Zero terminated
307 */
308 HGCMFunctionParameter name;
309
310 /**
311 * The returned string data will be placed here. (OUT pointer)
312 * This call returns two null-terminated strings which will be placed one
313 * after another: value and flags.
314 */
315 HGCMFunctionParameter buffer;
316
317 /**
318 * The property timestamp. (OUT uint64_t)
319 */
320 HGCMFunctionParameter timestamp;
321
322 /**
323 * If the buffer provided was large enough this will contain the size of
324 * the returned data. Otherwise it will contain the size of the buffer
325 * needed to hold the data and VERR_BUFFER_OVERFLOW will be returned.
326 * (OUT uint32_t)
327 */
328 HGCMFunctionParameter size;
329} GuestPropMsgGetProperty;
330AssertCompileSize(GuestPropMsgGetProperty, 40 + 4 * (ARCH_BITS == 64 ? 16 : 12));
331
332/** The guest is requesting to change a property */
333typedef struct GuestPropMsgSetProperty
334{
335 VBGLIOCHGCMCALL hdr;
336
337 /**
338 * The property name. (IN pointer)
339 * This must fit to a number of criteria, namely
340 * - Only Utf8 strings are allowed
341 * - Less than or equal to MAX_NAME_LEN bytes in length
342 * - Zero terminated
343 */
344 HGCMFunctionParameter name;
345
346 /**
347 * The value of the property (IN pointer)
348 * Criteria as for the name parameter, but with length less than or equal to
349 * MAX_VALUE_LEN.
350 */
351 HGCMFunctionParameter value;
352
353 /**
354 * The property flags (IN pointer)
355 * This is a comma-separated list of the format flag=value
356 * The length must be less than or equal to MAX_FLAGS_LEN and only
357 * known flag names and values will be accepted.
358 */
359 HGCMFunctionParameter flags;
360} GuestPropMsgSetProperty;
361AssertCompileSize(GuestPropMsgSetProperty, 40 + 3 * (ARCH_BITS == 64 ? 16 : 12));
362
363/** The guest is requesting to change the value of a property */
364typedef struct GuestPropMsgSetPropertyValue
365{
366 VBGLIOCHGCMCALL hdr;
367
368 /**
369 * The property name. (IN pointer)
370 * This must fit to a number of criteria, namely
371 * - Only Utf8 strings are allowed
372 * - Less than or equal to MAX_NAME_LEN bytes in length
373 * - Zero terminated
374 */
375 HGCMFunctionParameter name;
376
377 /**
378 * The value of the property (IN pointer)
379 * Criteria as for the name parameter, but with length less than or equal to
380 * MAX_VALUE_LEN.
381 */
382 HGCMFunctionParameter value;
383} GuestPropMsgSetPropertyValue;
384AssertCompileSize(GuestPropMsgSetPropertyValue, 40 + 2 * (ARCH_BITS == 64 ? 16 : 12));
385
386/** The guest is requesting to remove a property */
387typedef struct GuestPropMsgDelProperty
388{
389 VBGLIOCHGCMCALL hdr;
390
391 /**
392 * The property name. This must fit to a number of criteria, namely
393 * - Only Utf8 strings are allowed
394 * - Less than or equal to MAX_NAME_LEN bytes in length
395 * - Zero terminated
396 */
397 HGCMFunctionParameter name;
398} GuestPropMsgDelProperty;
399AssertCompileSize(GuestPropMsgDelProperty, 40 + 1 * (ARCH_BITS == 64 ? 16 : 12));
400
401/** The guest is requesting to enumerate properties */
402typedef struct GuestPropMsgEnumProperties
403{
404 VBGLIOCHGCMCALL hdr;
405
406 /**
407 * Array of patterns to match the properties against, separated by '|'
408 * characters. For backwards compatibility, '\\0' is also accepted
409 * as a separater.
410 * (IN pointer)
411 * If only a single, empty pattern is given then match all.
412 */
413 HGCMFunctionParameter patterns;
414 /**
415 * On success, null-separated array of strings in which the properties are
416 * returned. (OUT pointer)
417 * The number of strings in the array is always a multiple of four,
418 * and in sequences of name, value, timestamp (hexadecimal string) and the
419 * flags as a comma-separated list in the format "name=value". The list
420 * is terminated by an empty string after a "flags" entry (or at the
421 * start).
422 */
423 HGCMFunctionParameter strings;
424 /**
425 * On success, the size of the returned data. If the buffer provided is
426 * too small, the size of buffer needed. (OUT uint32_t)
427 */
428 HGCMFunctionParameter size;
429} GuestPropMsgEnumProperties;
430AssertCompileSize(GuestPropMsgEnumProperties, 40 + 3 * (ARCH_BITS == 64 ? 16 : 12));
431
432/**
433 * The guest is polling for notifications on changes to properties, specifying
434 * a set of patterns to match the names of changed properties against and
435 * optionally the timestamp of the last notification seen.
436 * On success, VINF_SUCCESS will be returned and the buffer will contain
437 * details of a property notification. If no new notification is available
438 * which matches one of the specified patterns, the call will block until one
439 * is.
440 * If the last notification could not be found by timestamp, VWRN_NOT_FOUND
441 * will be returned and the oldest available notification will be returned.
442 * If a zero timestamp is specified, the call will always wait for a new
443 * notification to arrive.
444 * If the buffer supplied was not large enough to hold the notification,
445 * VERR_BUFFER_OVERFLOW will be returned and the size parameter will contain
446 * the size of the buffer needed.
447 *
448 * The protocol for a guest to obtain notifications is to call
449 * GET_NOTIFICATION in a loop. On the first call, the ingoing timestamp
450 * parameter should be set to zero. On subsequent calls, it should be set to
451 * the outgoing timestamp from the previous call.
452 */
453typedef struct GuestPropMsgGetNotification
454{
455 VBGLIOCHGCMCALL hdr;
456
457 /**
458 * A list of patterns to match the guest event name against, separated by
459 * vertical bars (|) (IN pointer)
460 * An empty string means match all.
461 */
462 HGCMFunctionParameter patterns;
463 /**
464 * The timestamp of the last change seen (IN uint64_t)
465 * This may be zero, in which case the oldest available change will be
466 * sent. If the service does not remember an event matching the
467 * timestamp, then VWRN_NOT_FOUND will be returned, and the guest should
468 * assume that it has missed a certain number of notifications.
469 *
470 * The timestamp of the change being notified of (OUT uint64_t)
471 * Undefined on failure.
472 */
473 HGCMFunctionParameter timestamp;
474
475 /**
476 * The returned data, if any, will be placed here. (OUT pointer)
477 * This call returns three null-terminated strings which will be placed
478 * one after another: name, value and flags. For a delete notification,
479 * value and flags will be empty strings. Undefined on failure.
480 */
481 HGCMFunctionParameter buffer;
482
483 /**
484 * On success, the size of the returned data. (OUT uint32_t)
485 * On buffer overflow, the size of the buffer needed to hold the data.
486 * Undefined on failure.
487 */
488 HGCMFunctionParameter size;
489} GuestPropMsgGetNotification;
490AssertCompileSize(GuestPropMsgGetNotification, 40 + 4 * (ARCH_BITS == 64 ? 16 : 12));
491
492
493#endif /* !___VBox_HostService_GuestPropertySvc_h */
494
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