VirtualBox

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

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

GuestPropertySvc.h: Use memcpy instead of strcpy as the latter is frowned upon by recent darwin and others.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 17.7 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, GUEST_PROP_F_XXX.
68 * @param pcchName Where to return the name length.
69 */
70DECLINLINE(const char *) GuestPropFlagNameAndLen(uint32_t fFlag, size_t *pcchName)
71{
72 switch (fFlag)
73 {
74 case GUEST_PROP_F_TRANSIENT:
75 *pcchName = sizeof("TRANSIENT") - 1;
76 return "TRANSIENT";
77 case GUEST_PROP_F_RDONLYGUEST:
78 *pcchName = sizeof("RDONLYGUEST") - 1;
79 return "RDONLYGUEST";
80 case GUEST_PROP_F_RDONLYHOST:
81 *pcchName = sizeof("RDONLYHOST") - 1;
82 return "RDONLYHOST";
83 case GUEST_PROP_F_READONLY:
84 *pcchName = sizeof("READONLY") - 1;
85 return "READONLY";
86 case GUEST_PROP_F_TRANSRESET:
87 *pcchName = sizeof("TRANSRESET") - 1;
88 return "TRANSRESET";
89 default:
90 *pcchName = 0;
91 return NULL;
92 }
93}
94
95/**
96 * Maximum length for the property flags field. We only ever return one of
97 * RDONLYGUEST, RDONLYHOST and RDONLY
98 */
99#define GUEST_PROP_MAX_FLAGS_LEN sizeof("TRANSIENT, RDONLYGUEST, TRANSRESET")
100
101/**
102 * Parse a guest properties flags string for flag names and make sure that
103 * there is no junk text in the string.
104 *
105 * @returns IPRT status code
106 * @retval VERR_INVALID_PARAMETER if the flag string is not valid
107 * @param pcszFlags the flag string to parse
108 * @param pfFlags where to store the parse result. May not be NULL.
109 * @note This function is also inline because it must be accessible from
110 * several modules and it does not seem reasonable to put it into
111 * its own library.
112 */
113DECLINLINE(int) GuestPropValidateFlags(const char *pcszFlags, uint32_t *pfFlags)
114{
115 static const uint32_t s_aFlagList[] =
116 {
117 GUEST_PROP_F_TRANSIENT, GUEST_PROP_F_READONLY, GUEST_PROP_F_RDONLYGUEST, GUEST_PROP_F_RDONLYHOST, GUEST_PROP_F_TRANSRESET
118 };
119 const char *pcszNext = pcszFlags;
120 int rc = VINF_SUCCESS;
121 uint32_t fFlags = 0;
122 AssertLogRelReturn(VALID_PTR(pfFlags), VERR_INVALID_POINTER);
123
124 if (pcszFlags)
125 {
126 while (*pcszNext == ' ')
127 ++pcszNext;
128 while ((*pcszNext != '\0') && RT_SUCCESS(rc))
129 {
130 unsigned i;
131 rc = VERR_PARSE_ERROR;
132 for (i = 0; i < RT_ELEMENTS(s_aFlagList); ++i)
133 {
134 size_t cchFlagName;
135 const char *pszFlagName = GuestPropFlagNameAndLen(s_aFlagList[i], &cchFlagName);
136 if (RTStrNICmp(pcszNext, pszFlagName, cchFlagName) == 0)
137 {
138 char ch;
139 fFlags |= s_aFlagList[i];
140 pcszNext += cchFlagName;
141 while ((ch = *pcszNext) == ' ')
142 ++pcszNext;
143 rc = VINF_SUCCESS;
144 if (ch == ',')
145 {
146 ++pcszNext;
147 while (*pcszNext == ' ')
148 ++pcszNext;
149 }
150 else if (ch != '\0')
151 rc = VERR_PARSE_ERROR;
152 break;
153 }
154 }
155 }
156 }
157 if (RT_SUCCESS(rc))
158 *pfFlags = fFlags;
159 return rc;
160}
161
162
163/**
164 * Write out flags to a string.
165 * @returns IPRT status code
166 * @param fFlags the flags to write out
167 * @param pszFlags where to write the flags string. This must point to
168 * a buffer of size (at least) GUEST_PROP_MAX_FLAGS_LEN.
169 */
170DECLINLINE(int) GuestPropWriteFlags(uint32_t fFlags, char *pszFlags)
171{
172 /* Putting READONLY before the other RDONLY flags keeps the result short. */
173 static const uint32_t s_aFlagList[] =
174 {
175 GUEST_PROP_F_TRANSIENT, GUEST_PROP_F_READONLY, GUEST_PROP_F_RDONLYGUEST, GUEST_PROP_F_RDONLYHOST, GUEST_PROP_F_TRANSRESET
176 };
177 int rc = VINF_SUCCESS;
178
179 AssertLogRelReturn(VALID_PTR(pszFlags), VERR_INVALID_POINTER);
180 if ((fFlags & ~GUEST_PROP_F_ALLFLAGS) == GUEST_PROP_F_NILFLAG)
181 {
182 char *pszNext;
183 unsigned i;
184
185 /* TRANSRESET implies TRANSIENT. For compatability with old clients we
186 always set TRANSIENT when TRANSRESET appears. */
187 if (fFlags & GUEST_PROP_F_TRANSRESET)
188 fFlags |= GUEST_PROP_F_TRANSIENT;
189
190 pszNext = pszFlags;
191 for (i = 0; i < RT_ELEMENTS(s_aFlagList); ++i)
192 {
193 if (s_aFlagList[i] == (fFlags & s_aFlagList[i]))
194 {
195 size_t cchFlagName;
196 const char *pszFlagName = GuestPropFlagNameAndLen(s_aFlagList[i], &cchFlagName);
197 memcpy(pszNext, pszFlagName, cchFlagName);
198 pszNext += cchFlagName;
199 fFlags &= ~s_aFlagList[i];
200 if (fFlags != GUEST_PROP_F_NILFLAG)
201 {
202 *pszNext++ = ',';
203 *pszNext++ = ' ';
204 }
205 }
206 }
207 *pszNext = '\0';
208
209 Assert((uintptr_t)(pszNext - pszFlags) < GUEST_PROP_MAX_FLAGS_LEN);
210 Assert(fFlags == GUEST_PROP_F_NILFLAG); /* bad s_aFlagList */
211 }
212 else
213 rc = VERR_INVALID_PARAMETER;
214 return rc;
215}
216
217
218/** @name The service functions which are callable by host.
219 * @{
220 */
221/** Set properties in a block.
222 * The parameters are pointers to NULL-terminated arrays containing the
223 * parameters. These are, in order, name, value, timestamp, flags. Strings are
224 * stored as pointers to mutable utf8 data. All parameters must be supplied. */
225#define GUEST_PROP_FN_HOST_SET_PROPS 1
226/** Get the value attached to a guest property.
227 * The parameter format matches that of GET_PROP. */
228#define GUEST_PROP_FN_HOST_GET_PROP 2
229/** Set the value attached to a guest property.
230 * The parameter format matches that of SET_PROP. */
231#define GUEST_PROP_FN_HOST_SET_PROP 3
232/** Set the value attached to a guest property.
233 * The parameter format matches that of SET_PROP_VALUE. */
234#define GUEST_PROP_FN_HOST_SET_PROP_VALUE 4
235/** Remove a guest property.
236 * The parameter format matches that of DEL_PROP. */
237#define GUEST_PROP_FN_HOST_DEL_PROP 5
238/** Enumerate guest properties.
239 * The parameter format matches that of ENUM_PROPS. */
240#define GUEST_PROP_FN_HOST_ENUM_PROPS 6
241/** Set global flags for the service.
242 * Currently RDONLYGUEST is supported. Takes one 32-bit unsigned integer
243 * parameter for the flags. */
244#define GUEST_PROP_FN_HOST_SET_GLOBAL_FLAGS 7
245/** Return the pointer to a debug info function enumerating all guest
246 * properties. */
247#define GUEST_PROP_FN_HOST_GET_DBGF_INFO 8
248/** @} */
249
250
251/** @name The service functions which are called by guest.
252 *
253 * @note The numbers may not change!
254 * @{
255 */
256/** Get a guest property */
257#define GUEST_PROP_FN_GET_PROP 1
258/** Set a guest property */
259#define GUEST_PROP_FN_SET_PROP 2
260/** Set just the value of a guest property */
261#define GUEST_PROP_FN_SET_PROP_VALUE 3
262/** Delete a guest property */
263#define GUEST_PROP_FN_DEL_PROP 4
264/** Enumerate guest properties */
265#define GUEST_PROP_FN_ENUM_PROPS 5
266/** Poll for guest notifications */
267#define GUEST_PROP_FN_GET_NOTIFICATION 6
268/** @} */
269
270
271/**
272 * Data structure to pass to the service extension callback.
273 * We use this to notify the host of changes to properties.
274 */
275typedef struct GUESTPROPHOSTCALLBACKDATA
276{
277 /** Magic number to identify the structure (GUESTPROPHOSTCALLBACKDATA_MAGIC). */
278 uint32_t u32Magic;
279 /** The name of the property that was changed */
280 const char *pcszName;
281 /** The new property value, or NULL if the property was deleted */
282 const char *pcszValue;
283 /** The timestamp of the modification */
284 uint64_t u64Timestamp;
285 /** The flags field of the modified property */
286 const char *pcszFlags;
287} GUESTPROPHOSTCALLBACKDATA;
288/** Poitner to a data structure to pass to the service extension callback. */
289typedef GUESTPROPHOSTCALLBACKDATA *PGUESTPROPHOSTCALLBACKDATA;
290
291/** Magic number for sanity checking the HOSTCALLBACKDATA structure */
292#define GUESTPROPHOSTCALLBACKDATA_MAGIC UINT32_C(0x69c87a78)
293
294/**
295 * HGCM parameter structures. Packing is explicitly defined as this is a wire format.
296 */
297/** The guest is requesting the value of a property */
298typedef struct GuestPropMsgGetProperty
299{
300 VBGLIOCHGCMCALL hdr;
301
302 /**
303 * The property name (IN pointer)
304 * This must fit to a number of criteria, namely
305 * - Only Utf8 strings are allowed
306 * - Less than or equal to MAX_NAME_LEN bytes in length
307 * - Zero terminated
308 */
309 HGCMFunctionParameter name;
310
311 /**
312 * The returned string data will be placed here. (OUT pointer)
313 * This call returns two null-terminated strings which will be placed one
314 * after another: value and flags.
315 */
316 HGCMFunctionParameter buffer;
317
318 /**
319 * The property timestamp. (OUT uint64_t)
320 */
321 HGCMFunctionParameter timestamp;
322
323 /**
324 * If the buffer provided was large enough this will contain the size of
325 * the returned data. Otherwise it will contain the size of the buffer
326 * needed to hold the data and VERR_BUFFER_OVERFLOW will be returned.
327 * (OUT uint32_t)
328 */
329 HGCMFunctionParameter size;
330} GuestPropMsgGetProperty;
331AssertCompileSize(GuestPropMsgGetProperty, 40 + 4 * (ARCH_BITS == 64 ? 16 : 12));
332
333/** The guest is requesting to change a property */
334typedef struct GuestPropMsgSetProperty
335{
336 VBGLIOCHGCMCALL hdr;
337
338 /**
339 * The property name. (IN pointer)
340 * This must fit to a number of criteria, namely
341 * - Only Utf8 strings are allowed
342 * - Less than or equal to MAX_NAME_LEN bytes in length
343 * - Zero terminated
344 */
345 HGCMFunctionParameter name;
346
347 /**
348 * The value of the property (IN pointer)
349 * Criteria as for the name parameter, but with length less than or equal to
350 * MAX_VALUE_LEN.
351 */
352 HGCMFunctionParameter value;
353
354 /**
355 * The property flags (IN pointer)
356 * This is a comma-separated list of the format flag=value
357 * The length must be less than or equal to GUEST_PROP_MAX_FLAGS_LEN and only
358 * known flag names and values will be accepted.
359 */
360 HGCMFunctionParameter flags;
361} GuestPropMsgSetProperty;
362AssertCompileSize(GuestPropMsgSetProperty, 40 + 3 * (ARCH_BITS == 64 ? 16 : 12));
363
364/** The guest is requesting to change the value of a property */
365typedef struct GuestPropMsgSetPropertyValue
366{
367 VBGLIOCHGCMCALL hdr;
368
369 /**
370 * The property name. (IN pointer)
371 * This must fit to a number of criteria, namely
372 * - Only Utf8 strings are allowed
373 * - Less than or equal to MAX_NAME_LEN bytes in length
374 * - Zero terminated
375 */
376 HGCMFunctionParameter name;
377
378 /**
379 * The value of the property (IN pointer)
380 * Criteria as for the name parameter, but with length less than or equal to
381 * MAX_VALUE_LEN.
382 */
383 HGCMFunctionParameter value;
384} GuestPropMsgSetPropertyValue;
385AssertCompileSize(GuestPropMsgSetPropertyValue, 40 + 2 * (ARCH_BITS == 64 ? 16 : 12));
386
387/** The guest is requesting to remove a property */
388typedef struct GuestPropMsgDelProperty
389{
390 VBGLIOCHGCMCALL hdr;
391
392 /**
393 * The property name. This must fit to a number of criteria, namely
394 * - Only Utf8 strings are allowed
395 * - Less than or equal to MAX_NAME_LEN bytes in length
396 * - Zero terminated
397 */
398 HGCMFunctionParameter name;
399} GuestPropMsgDelProperty;
400AssertCompileSize(GuestPropMsgDelProperty, 40 + 1 * (ARCH_BITS == 64 ? 16 : 12));
401
402/** The guest is requesting to enumerate properties */
403typedef struct GuestPropMsgEnumProperties
404{
405 VBGLIOCHGCMCALL hdr;
406
407 /**
408 * Array of patterns to match the properties against, separated by '|'
409 * characters. For backwards compatibility, '\\0' is also accepted
410 * as a separater.
411 * (IN pointer)
412 * If only a single, empty pattern is given then match all.
413 */
414 HGCMFunctionParameter patterns;
415 /**
416 * On success, null-separated array of strings in which the properties are
417 * returned. (OUT pointer)
418 * The number of strings in the array is always a multiple of four,
419 * and in sequences of name, value, timestamp (hexadecimal string) and the
420 * flags as a comma-separated list in the format "name=value". The list
421 * is terminated by an empty string after a "flags" entry (or at the
422 * start).
423 */
424 HGCMFunctionParameter strings;
425 /**
426 * On success, the size of the returned data. If the buffer provided is
427 * too small, the size of buffer needed. (OUT uint32_t)
428 */
429 HGCMFunctionParameter size;
430} GuestPropMsgEnumProperties;
431AssertCompileSize(GuestPropMsgEnumProperties, 40 + 3 * (ARCH_BITS == 64 ? 16 : 12));
432
433/**
434 * The guest is polling for notifications on changes to properties, specifying
435 * a set of patterns to match the names of changed properties against and
436 * optionally the timestamp of the last notification seen.
437 * On success, VINF_SUCCESS will be returned and the buffer will contain
438 * details of a property notification. If no new notification is available
439 * which matches one of the specified patterns, the call will block until one
440 * is.
441 * If the last notification could not be found by timestamp, VWRN_NOT_FOUND
442 * will be returned and the oldest available notification will be returned.
443 * If a zero timestamp is specified, the call will always wait for a new
444 * notification to arrive.
445 * If the buffer supplied was not large enough to hold the notification,
446 * VERR_BUFFER_OVERFLOW will be returned and the size parameter will contain
447 * the size of the buffer needed.
448 *
449 * The protocol for a guest to obtain notifications is to call
450 * GET_NOTIFICATION in a loop. On the first call, the ingoing timestamp
451 * parameter should be set to zero. On subsequent calls, it should be set to
452 * the outgoing timestamp from the previous call.
453 */
454typedef struct GuestPropMsgGetNotification
455{
456 VBGLIOCHGCMCALL hdr;
457
458 /**
459 * A list of patterns to match the guest event name against, separated by
460 * vertical bars (|) (IN pointer)
461 * An empty string means match all.
462 */
463 HGCMFunctionParameter patterns;
464 /**
465 * The timestamp of the last change seen (IN uint64_t)
466 * This may be zero, in which case the oldest available change will be
467 * sent. If the service does not remember an event matching the
468 * timestamp, then VWRN_NOT_FOUND will be returned, and the guest should
469 * assume that it has missed a certain number of notifications.
470 *
471 * The timestamp of the change being notified of (OUT uint64_t)
472 * Undefined on failure.
473 */
474 HGCMFunctionParameter timestamp;
475
476 /**
477 * The returned data, if any, will be placed here. (OUT pointer)
478 * This call returns three null-terminated strings which will be placed
479 * one after another: name, value and flags. For a delete notification,
480 * value and flags will be empty strings. Undefined on failure.
481 */
482 HGCMFunctionParameter buffer;
483
484 /**
485 * On success, the size of the returned data. (OUT uint32_t)
486 * On buffer overflow, the size of the buffer needed to hold the data.
487 * Undefined on failure.
488 */
489 HGCMFunctionParameter size;
490} GuestPropMsgGetNotification;
491AssertCompileSize(GuestPropMsgGetNotification, 40 + 4 * (ARCH_BITS == 64 ? 16 : 12));
492
493
494#endif /* !___VBox_HostService_GuestPropertySvc_h */
495
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