VirtualBox

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

Last change on this file since 21227 was 21227, checked in by vboxsync, 15 years ago

VBoxGuest.h/VMMDev.h/VBoxGuestLib.h usage cleanup.

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