VirtualBox

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

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

Backed out r119634 + r119637 again.

  • 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 - Common header for host service and guest clients.
3 */
4
5/*
6 * Copyright (C) 2006-2017 Oracle Corporation
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.virtualbox.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License (GPL) as published by the Free Software
12 * Foundation, in version 2 as it comes in the "COPYING" file of the
13 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15 *
16 * The contents of this file may alternatively be used under the terms
17 * of the Common Development and Distribution License Version 1.0
18 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
19 * VirtualBox OSE distribution, in which case the provisions of the
20 * CDDL are applicable instead of those of the GPL.
21 *
22 * You may elect to license modified versions of this file under the
23 * terms and conditions of either the GPL or the CDDL or both.
24 */
25
26#ifndef ___VBox_HostService_GuestPropertyService_h
27#define ___VBox_HostService_GuestPropertyService_h
28
29#include <VBox/VMMDevCoreTypes.h>
30#include <VBox/VBoxGuestCoreTypes.h>
31#include <VBox/log.h>
32#include <VBox/hgcmsvc.h>
33#include <iprt/assertcompile.h>
34#include <iprt/string.h>
35
36
37/** Maximum length for property names */
38#define GUEST_PROP_MAX_NAME_LEN 64
39/** Maximum length for property values */
40#define GUEST_PROP_MAX_VALUE_LEN 128
41/** Maximum number of properties per guest */
42#define GUEST_PROP_MAX_PROPS 256
43/** Maximum size for enumeration patterns */
44#define GUEST_PROP_MAX_PATTERN_LEN 1024
45/** Maximum number of changes we remember for guest notifications */
46#define GUEST_PROP_MAX_GUEST_NOTIFICATIONS 256
47
48
49/** @name GUEST_PROP_F_XXX - The guest property flag values which are currently accepted.
50 * @{
51 */
52#define GUEST_PROP_F_NILFLAG UINT32_C(0)
53/** Transient until VM gets shut down. */
54#define GUEST_PROP_F_TRANSIENT RT_BIT_32(1)
55#define GUEST_PROP_F_RDONLYGUEST RT_BIT_32(2)
56#define GUEST_PROP_F_RDONLYHOST RT_BIT_32(3)
57/** Transient until VM gets a reset / restarts.
58 * Implies TRANSIENT. */
59#define GUEST_PROP_F_TRANSRESET RT_BIT_32(4)
60#define GUEST_PROP_F_READONLY (GUEST_PROP_F_RDONLYGUEST | GUEST_PROP_F_RDONLYHOST)
61#define GUEST_PROP_F_ALLFLAGS (GUEST_PROP_F_TRANSIENT | GUEST_PROP_F_READONLY | GUEST_PROP_F_TRANSRESET)
62/** @} */
63
64/**
65 * Get the name of a flag as a string.
66 * @returns the name, or NULL if fFlag is invalid.
67 * @param fFlag the flag. Must be a value from the ePropFlags enumeration
68 * list.
69 */
70DECLINLINE(const char *) GuestPropFlagName(uint32_t fFlag)
71{
72 switch (fFlag)
73 {
74 case GUEST_PROP_F_TRANSIENT:
75 return "TRANSIENT";
76 case GUEST_PROP_F_RDONLYGUEST:
77 return "RDONLYGUEST";
78 case GUEST_PROP_F_RDONLYHOST:
79 return "RDONLYHOST";
80 case GUEST_PROP_F_READONLY:
81 return "READONLY";
82 case GUEST_PROP_F_TRANSRESET:
83 return "TRANSRESET";
84 default:
85 break;
86 }
87 return NULL;
88}
89
90/**
91 * Get the length of a flag name as returned by flagName.
92 * @returns the length, or 0 if fFlag is invalid.
93 * @param fFlag the flag. Must be a value from the ePropFlags enumeration
94 * list.
95 */
96DECLINLINE(size_t) GuestPropFlagNameLen(uint32_t fFlag)
97{
98 const char *pcszName = GuestPropFlagName(fFlag);
99 return RT_LIKELY(pcszName != NULL) ? strlen(pcszName) : 0;
100}
101
102/**
103 * Maximum length for the property flags field. We only ever return one of
104 * RDONLYGUEST, RDONLYHOST and RDONLY
105 */
106#define GUEST_PROP_MAX_FLAGS_LEN sizeof("TRANSIENT, RDONLYGUEST, TRANSRESET")
107
108/**
109 * Parse a guest properties flags string for flag names and make sure that
110 * there is no junk text in the string.
111 *
112 * @returns IPRT status code
113 * @retval VERR_INVALID_PARAMETER if the flag string is not valid
114 * @param pcszFlags the flag string to parse
115 * @param pfFlags where to store the parse result. May not be NULL.
116 * @note This function is also inline because it must be accessible from
117 * several modules and it does not seem reasonable to put it into
118 * its own library.
119 */
120DECLINLINE(int) GuestPropValidateFlags(const char *pcszFlags, uint32_t *pfFlags)
121{
122 static const uint32_t s_aFlagList[] =
123 {
124 GUEST_PROP_F_TRANSIENT, GUEST_PROP_F_READONLY, GUEST_PROP_F_RDONLYGUEST, GUEST_PROP_F_RDONLYHOST, GUEST_PROP_F_TRANSRESET
125 };
126 const char *pcszNext = pcszFlags;
127 int rc = VINF_SUCCESS;
128 uint32_t fFlags = 0;
129 AssertLogRelReturn(VALID_PTR(pfFlags), VERR_INVALID_POINTER);
130
131 if (pcszFlags)
132 {
133 while (' ' == *pcszNext)
134 ++pcszNext;
135 while ((*pcszNext != '\0') && RT_SUCCESS(rc))
136 {
137 unsigned i = 0;
138 for (; i < RT_ELEMENTS(s_aFlagList); ++i)
139 if (RTStrNICmp(pcszNext, GuestPropFlagName(s_aFlagList[i]), GuestPropFlagNameLen(s_aFlagList[i])) == 0)
140 break;
141 if (RT_ELEMENTS(s_aFlagList) == i)
142 rc = VERR_PARSE_ERROR;
143 else
144 {
145 fFlags |= s_aFlagList[i];
146 pcszNext += GuestPropFlagNameLen(s_aFlagList[i]);
147 while (' ' == *pcszNext)
148 ++pcszNext;
149 if (',' == *pcszNext)
150 ++pcszNext;
151 else if (*pcszNext != '\0')
152 rc = VERR_PARSE_ERROR;
153 while (' ' == *pcszNext)
154 ++pcszNext;
155 }
156 }
157 }
158 if (RT_SUCCESS(rc))
159 *pfFlags = fFlags;
160 return rc;
161}
162
163
164/**
165 * Write out flags to a string.
166 * @returns IPRT status code
167 * @param fFlags the flags to write out
168 * @param pszFlags where to write the flags string. This must point to
169 * a buffer of size (at least) MAX_FLAGS_LEN.
170 */
171DECLINLINE(int) GuestPropWriteFlags(uint32_t fFlags, char *pszFlags)
172{
173 /* Putting READONLY before the other RDONLY flags keeps the result short. */
174 static const uint32_t s_aFlagList[] =
175 {
176 GUEST_PROP_F_TRANSIENT, GUEST_PROP_F_READONLY, GUEST_PROP_F_RDONLYGUEST, GUEST_PROP_F_RDONLYHOST, GUEST_PROP_F_TRANSRESET
177 };
178 int rc = VINF_SUCCESS;
179
180 AssertLogRelReturn(VALID_PTR(pszFlags), VERR_INVALID_POINTER);
181 if ((fFlags & ~GUEST_PROP_F_ALLFLAGS) == GUEST_PROP_F_NILFLAG)
182 {
183 char *pszNext;
184 unsigned i;
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 (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_HOST_SET_PROPS 1
224/** Get the value attached to a guest property.
225 * The parameter format matches that of GET_PROP. */
226#define GUEST_PROP_FN_HOST_GET_PROP 2
227/** Set the value attached to a guest property.
228 * The parameter format matches that of SET_PROP. */
229#define GUEST_PROP_FN_HOST_SET_PROP 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_HOST_SET_PROP_VALUE 4
233/** Remove a guest property.
234 * The parameter format matches that of DEL_PROP. */
235#define GUEST_PROP_FN_HOST_DEL_PROP 5
236/** Enumerate guest properties.
237 * The parameter format matches that of ENUM_PROPS. */
238#define GUEST_PROP_FN_HOST_ENUM_PROPS 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_HOST_SET_GLOBAL_FLAGS 7
243/** Return the pointer to a debug info function enumerating all guest
244 * properties. */
245#define GUEST_PROP_FN_HOST_GET_DBGF_INFO 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/**
293 * HGCM parameter structures. Packing is explicitly defined as this is a wire format.
294 */
295/** The guest is requesting the value of a property */
296typedef struct GuestPropMsgGetProperty
297{
298 VBGLIOCHGCMCALL hdr;
299
300 /**
301 * The property name (IN pointer)
302 * This must fit to a number of criteria, namely
303 * - Only Utf8 strings are allowed
304 * - Less than or equal to MAX_NAME_LEN bytes in length
305 * - Zero terminated
306 */
307 HGCMFunctionParameter name;
308
309 /**
310 * The returned string data will be placed here. (OUT pointer)
311 * This call returns two null-terminated strings which will be placed one
312 * after another: value and flags.
313 */
314 HGCMFunctionParameter buffer;
315
316 /**
317 * The property timestamp. (OUT uint64_t)
318 */
319 HGCMFunctionParameter timestamp;
320
321 /**
322 * If the buffer provided was large enough this will contain the size of
323 * the returned data. Otherwise it will contain the size of the buffer
324 * needed to hold the data and VERR_BUFFER_OVERFLOW will be returned.
325 * (OUT uint32_t)
326 */
327 HGCMFunctionParameter size;
328} GuestPropMsgGetProperty;
329AssertCompileSize(GuestPropMsgGetProperty, 40 + 4 * (ARCH_BITS == 64 ? 16 : 12));
330
331/** The guest is requesting to change a property */
332typedef struct GuestPropMsgSetProperty
333{
334 VBGLIOCHGCMCALL hdr;
335
336 /**
337 * The property name. (IN pointer)
338 * This must fit to a number of criteria, namely
339 * - Only Utf8 strings are allowed
340 * - Less than or equal to MAX_NAME_LEN bytes in length
341 * - Zero terminated
342 */
343 HGCMFunctionParameter name;
344
345 /**
346 * The value of the property (IN pointer)
347 * Criteria as for the name parameter, but with length less than or equal to
348 * MAX_VALUE_LEN.
349 */
350 HGCMFunctionParameter value;
351
352 /**
353 * The property flags (IN pointer)
354 * This is a comma-separated list of the format flag=value
355 * The length must be less than or equal to MAX_FLAGS_LEN and only
356 * known flag names and values will be accepted.
357 */
358 HGCMFunctionParameter flags;
359} GuestPropMsgSetProperty;
360AssertCompileSize(GuestPropMsgSetProperty, 40 + 3 * (ARCH_BITS == 64 ? 16 : 12));
361
362/** The guest is requesting to change the value of a property */
363typedef struct GuestPropMsgSetPropertyValue
364{
365 VBGLIOCHGCMCALL hdr;
366
367 /**
368 * The property name. (IN pointer)
369 * This must fit to a number of criteria, namely
370 * - Only Utf8 strings are allowed
371 * - Less than or equal to MAX_NAME_LEN bytes in length
372 * - Zero terminated
373 */
374 HGCMFunctionParameter name;
375
376 /**
377 * The value of the property (IN pointer)
378 * Criteria as for the name parameter, but with length less than or equal to
379 * MAX_VALUE_LEN.
380 */
381 HGCMFunctionParameter value;
382} GuestPropMsgSetPropertyValue;
383AssertCompileSize(GuestPropMsgSetPropertyValue, 40 + 2 * (ARCH_BITS == 64 ? 16 : 12));
384
385/** The guest is requesting to remove a property */
386typedef struct GuestPropMsgDelProperty
387{
388 VBGLIOCHGCMCALL hdr;
389
390 /**
391 * The property name. This must fit to a number of criteria, namely
392 * - Only Utf8 strings are allowed
393 * - Less than or equal to MAX_NAME_LEN bytes in length
394 * - Zero terminated
395 */
396 HGCMFunctionParameter name;
397} GuestPropMsgDelProperty;
398AssertCompileSize(GuestPropMsgDelProperty, 40 + 1 * (ARCH_BITS == 64 ? 16 : 12));
399
400/** The guest is requesting to enumerate properties */
401typedef struct GuestPropMsgEnumProperties
402{
403 VBGLIOCHGCMCALL hdr;
404
405 /**
406 * Array of patterns to match the properties against, separated by '|'
407 * characters. For backwards compatibility, '\\0' is also accepted
408 * as a separater.
409 * (IN pointer)
410 * If only a single, empty pattern is given then match all.
411 */
412 HGCMFunctionParameter patterns;
413 /**
414 * On success, null-separated array of strings in which the properties are
415 * returned. (OUT pointer)
416 * The number of strings in the array is always a multiple of four,
417 * and in sequences of name, value, timestamp (hexadecimal string) and the
418 * flags as a comma-separated list in the format "name=value". The list
419 * is terminated by an empty string after a "flags" entry (or at the
420 * start).
421 */
422 HGCMFunctionParameter strings;
423 /**
424 * On success, the size of the returned data. If the buffer provided is
425 * too small, the size of buffer needed. (OUT uint32_t)
426 */
427 HGCMFunctionParameter size;
428} GuestPropMsgEnumProperties;
429AssertCompileSize(GuestPropMsgEnumProperties, 40 + 3 * (ARCH_BITS == 64 ? 16 : 12));
430
431/**
432 * The guest is polling for notifications on changes to properties, specifying
433 * a set of patterns to match the names of changed properties against and
434 * optionally the timestamp of the last notification seen.
435 * On success, VINF_SUCCESS will be returned and the buffer will contain
436 * details of a property notification. If no new notification is available
437 * which matches one of the specified patterns, the call will block until one
438 * is.
439 * If the last notification could not be found by timestamp, VWRN_NOT_FOUND
440 * will be returned and the oldest available notification will be returned.
441 * If a zero timestamp is specified, the call will always wait for a new
442 * notification to arrive.
443 * If the buffer supplied was not large enough to hold the notification,
444 * VERR_BUFFER_OVERFLOW will be returned and the size parameter will contain
445 * the size of the buffer needed.
446 *
447 * The protocol for a guest to obtain notifications is to call
448 * GET_NOTIFICATION in a loop. On the first call, the ingoing timestamp
449 * parameter should be set to zero. On subsequent calls, it should be set to
450 * the outgoing timestamp from the previous call.
451 */
452typedef struct GuestPropMsgGetNotification
453{
454 VBGLIOCHGCMCALL hdr;
455
456 /**
457 * A list of patterns to match the guest event name against, separated by
458 * vertical bars (|) (IN pointer)
459 * An empty string means match all.
460 */
461 HGCMFunctionParameter patterns;
462 /**
463 * The timestamp of the last change seen (IN uint64_t)
464 * This may be zero, in which case the oldest available change will be
465 * sent. If the service does not remember an event matching the
466 * timestamp, then VWRN_NOT_FOUND will be returned, and the guest should
467 * assume that it has missed a certain number of notifications.
468 *
469 * The timestamp of the change being notified of (OUT uint64_t)
470 * Undefined on failure.
471 */
472 HGCMFunctionParameter timestamp;
473
474 /**
475 * The returned data, if any, will be placed here. (OUT pointer)
476 * This call returns three null-terminated strings which will be placed
477 * one after another: name, value and flags. For a delete notification,
478 * value and flags will be empty strings. Undefined on failure.
479 */
480 HGCMFunctionParameter buffer;
481
482 /**
483 * On success, the size of the returned data. (OUT uint32_t)
484 * On buffer overflow, the size of the buffer needed to hold the data.
485 * Undefined on failure.
486 */
487 HGCMFunctionParameter size;
488} GuestPropMsgGetNotification;
489AssertCompileSize(GuestPropMsgGetNotification, 40 + 4 * (ARCH_BITS == 64 ? 16 : 12));
490
491
492#endif /* !___VBox_HostService_GuestPropertySvc_h */
493
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