VirtualBox

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

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

Guest properties: Added a length check for GuestPropWriteFlags() and got rid of strcpy(), as this now also is marked as being deprecated on macOS, take #2.

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