VirtualBox

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

Last change on this file since 36382 was 36382, checked in by vboxsync, 14 years ago

hint

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