VirtualBox

source: vbox/trunk/src/VBox/Additions/common/VBoxService/VBoxServiceUtils.cpp@ 29021

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

VBoxService: Introduced guest property cache (used by VM information thread). Not tested yet.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.2 KB
Line 
1/* $Id: VBoxServiceUtils.cpp 28967 2010-05-03 11:44:28Z vboxsync $ */
2/** @file
3 * VBoxServiceUtils - Some utility functions.
4 */
5
6/*
7 * Copyright (C) 2009-2010 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
18
19/*******************************************************************************
20* Header Files *
21*******************************************************************************/
22#ifdef RT_OS_WINDOWS
23# include <Windows.h>
24# include <iprt/param.h>
25# include <iprt/path.h>
26#endif
27#include <iprt/assert.h>
28#include <iprt/mem.h>
29#include <iprt/string.h>
30
31#include <VBox/VBoxGuestLib.h>
32#include "VBoxServiceInternal.h"
33
34#ifdef VBOX_WITH_GUEST_PROPS
35
36/**
37 * Reads a guest property.
38 *
39 * @returns VBox status code, fully bitched.
40 *
41 * @param u32ClientId The HGCM client ID for the guest property session.
42 * @param pszPropName The property name.
43 * @param ppszValue Where to return the value. This is always set
44 * to NULL. Free it using RTStrFree().
45 * @param ppszFlags Where to return the value flags. Free it
46 * using RTStrFree(). Optional.
47 * @param puTimestamp Where to return the timestamp. This is only set
48 * on success. Optional.
49 */
50int VBoxServiceReadProp(uint32_t u32ClientId, const char *pszPropName, char **ppszValue, char **ppszFlags, uint64_t *puTimestamp)
51{
52 uint32_t cbBuf = _1K;
53 void *pvBuf = NULL;
54 int rc;
55
56 *ppszValue = NULL;
57
58 for (unsigned cTries = 0; cTries < 10; cTries++)
59 {
60 /*
61 * (Re-)Allocate the buffer and try read the property.
62 */
63 RTMemFree(pvBuf);
64 pvBuf = RTMemAlloc(cbBuf);
65 if (!pvBuf)
66 {
67 VBoxServiceError("Guest Property: Failed to allocate %zu bytes\n", cbBuf);
68 rc = VERR_NO_MEMORY;
69 break;
70 }
71 char *pszValue;
72 char *pszFlags;
73 uint64_t uTimestamp;
74 rc = VbglR3GuestPropRead(u32ClientId, pszPropName,
75 pvBuf, cbBuf,
76 &pszValue, &uTimestamp, &pszFlags, NULL);
77 if (RT_FAILURE(rc))
78 {
79 if (rc == VERR_BUFFER_OVERFLOW)
80 {
81 /* try again with a bigger buffer. */
82 cbBuf *= 2;
83 continue;
84 }
85 if (rc == VERR_NOT_FOUND)
86 VBoxServiceVerbose(2, "Guest Property: %s not found\n", pszPropName);
87 else
88 VBoxServiceError("Guest Property: Failed to query \"%s\": %Rrc\n", pszPropName, rc);
89 break;
90 }
91
92 VBoxServiceVerbose(2, "Guest Property: Read \"%s\" = \"%s\", timestamp %RU64n\n",
93 pszPropName, pszValue, uTimestamp);
94 *ppszValue = RTStrDup(pszValue);
95 if (!*ppszValue)
96 {
97 VBoxServiceError("Guest Property: RTStrDup failed for \"%s\"\n", pszValue);
98 rc = VERR_NO_MEMORY;
99 break;
100 }
101
102 if (puTimestamp)
103 *puTimestamp = uTimestamp;
104 if (ppszFlags)
105 *ppszFlags = RTStrDup(pszFlags);
106 break; /* done */
107 }
108
109 RTMemFree(pvBuf);
110 return rc;
111}
112
113
114/**
115 * Reads a guest property as a 32-bit value.
116 *
117 * @returns VBox status code, fully bitched.
118 *
119 * @param u32ClientId The HGCM client ID for the guest property session.
120 * @param pszPropName The property name.
121 * @param pu32 Where to store the 32-bit value.
122 *
123 */
124int VBoxServiceReadPropUInt32(uint32_t u32ClientId, const char *pszPropName, uint32_t *pu32, uint32_t u32Min, uint32_t u32Max)
125{
126 char *pszValue;
127 int rc = VBoxServiceReadProp(u32ClientId, pszPropName, &pszValue,
128 NULL /* ppszFlags */, NULL /* puTimestamp */);
129 if (RT_SUCCESS(rc))
130 {
131 AssertPtr(pu32);
132 char *pszNext;
133 rc = RTStrToUInt32Ex(pszValue, &pszNext, 0, pu32);
134 if ( RT_SUCCESS(rc)
135 && (*pu32 < u32Min || *pu32 > u32Max))
136 {
137 rc = VBoxServiceError("The guest property value %s = %RU32 is out of range [%RU32..%RU32].\n",
138 pszPropName, *pu32, u32Min, u32Max);
139 }
140 RTStrFree(pszValue);
141 }
142 return rc;
143}
144
145
146/**
147 * Wrapper around VbglR3GuestPropWriteValue that does value formatting and
148 * logging.
149 *
150 * @returns VBox status code. Errors will be logged.
151 *
152 * @param u32ClientId The HGCM client ID for the guest property session.
153 * @param pszName The property name.
154 * @param pszValueFormat The property format string. If this is NULL then
155 * the property will be deleted (if possible).
156 * @param ... Format arguments.
157 */
158int VBoxServiceWritePropF(uint32_t u32ClientId, const char *pszName, const char *pszValueFormat, ...)
159{
160 AssertPtr(pszName);
161 int rc;
162 if (pszValueFormat != NULL)
163 {
164 va_list va;
165 va_start(va, pszValueFormat);
166 VBoxServiceVerbose(3, "Writing guest property \"%s\" = \"%N\"\n", pszName, pszValueFormat, &va);
167 va_end(va);
168
169 va_start(va, pszValueFormat);
170 rc = VbglR3GuestPropWriteValueV(u32ClientId, pszName, pszValueFormat, va);
171 va_end(va);
172
173 if (RT_FAILURE(rc))
174 VBoxServiceError("Error writing guest property \"%s\" (rc=%Rrc)\n", pszName, rc);
175 }
176 else
177 {
178 VBoxServiceVerbose(3, "Deleting guest property \"%s\"\n", pszName);
179 rc = VbglR3GuestPropWriteValue(u32ClientId, pszName, NULL);
180 if (RT_FAILURE(rc))
181 VBoxServiceError("Error deleting guest property \"%s\" (rc=%Rrc)\n", pszName, rc);
182 }
183 return rc;
184}
185#endif /* VBOX_WITH_GUEST_PROPS */
186#ifdef RT_OS_WINDOWS
187
188/**
189 * Helper for VBoxServiceGetFileVersion and attemps to read and parse
190 * FileVersion.
191 *
192 * @returns Success indicator.
193 */
194static bool VBoxServiceGetFileVersionOwn(LPSTR pVerData,
195 PDWORD pdwMajor,
196 PDWORD pdwMinor,
197 PDWORD pdwBuildNumber,
198 PDWORD pdwRevisionNumber)
199{
200 UINT cchStrValue = 0;
201 LPTSTR pStrValue = NULL;
202 if (!VerQueryValueA(pVerData, "\\StringFileInfo\\040904b0\\FileVersion", (LPVOID *)&pStrValue, &cchStrValue))
203 return false;
204
205 /** @todo r=bird: get rid of this. Avoid sscanf like the plague! */
206 if (sscanf(pStrValue, "%ld.%ld.%ld.%ld", pdwMajor, pdwMinor, pdwBuildNumber, pdwRevisionNumber) != 4)
207 return false;
208
209 return true;
210}
211
212
213/**
214 * Worker for VBoxServiceGetFileVersionString.
215 *
216 * @returns VBox status code.
217 * @param pszFilename ASCII & ANSI & UTF-8 compliant name.
218 */
219static int VBoxServiceGetFileVersion(const char *pszFilename,
220 PDWORD pdwMajor,
221 PDWORD pdwMinor,
222 PDWORD pdwBuildNumber,
223 PDWORD pdwRevisionNumber)
224{
225 int rc;
226
227 *pdwMajor = *pdwMinor = *pdwBuildNumber = *pdwRevisionNumber = 0;
228
229 /*
230 * Get the file version info.
231 */
232 DWORD dwHandleIgnored;
233 DWORD cbVerData = GetFileVersionInfoSizeA(pszFilename, &dwHandleIgnored);
234 if (cbVerData)
235 {
236 LPTSTR pVerData = (LPTSTR)RTMemTmpAllocZ(cbVerData);
237 if (pVerData)
238 {
239 if (GetFileVersionInfoA(pszFilename, dwHandleIgnored, cbVerData, pVerData))
240 {
241 /*
242 * Try query and parse the FileVersion string our selves first
243 * since this will give us the correct revision number when
244 * it goes beyond the range of an uint16_t / WORD.
245 */
246 if (VBoxServiceGetFileVersionOwn(pVerData, pdwMajor, pdwMinor, pdwBuildNumber, pdwRevisionNumber))
247 rc = VINF_SUCCESS;
248 else
249 {
250 /* Fall back on VS_FIXEDFILEINFO */
251 UINT cbFileInfoIgnored = 0;
252 VS_FIXEDFILEINFO *pFileInfo = NULL;
253 if (VerQueryValue(pVerData, "\\", (LPVOID *)&pFileInfo, &cbFileInfoIgnored))
254 {
255 *pdwMajor = HIWORD(pFileInfo->dwFileVersionMS);
256 *pdwMinor = LOWORD(pFileInfo->dwFileVersionMS);
257 *pdwBuildNumber = HIWORD(pFileInfo->dwFileVersionLS);
258 *pdwRevisionNumber = LOWORD(pFileInfo->dwFileVersionLS);
259 rc = VINF_SUCCESS;
260 }
261 else
262 {
263 rc = RTErrConvertFromWin32(GetLastError());
264 VBoxServiceVerbose(3, "No file version value for file \"%s\" available! (%d / rc=%Rrc)\n",
265 pszFilename, GetLastError(), rc);
266 }
267 }
268 }
269 else
270 {
271 rc = RTErrConvertFromWin32(GetLastError());
272 VBoxServiceVerbose(0, "GetFileVersionInfo(%s) -> %u / %Rrc\n", pszFilename, GetLastError(), rc);
273 }
274
275 RTMemTmpFree(pVerData);
276 }
277 else
278 {
279 VBoxServiceVerbose(0, "Failed to allocate %u byte for file version info for '%s'\n", cbVerData, pszFilename);
280 rc = VERR_NO_TMP_MEMORY;
281 }
282 }
283 else
284 {
285 rc = RTErrConvertFromWin32(GetLastError());
286 VBoxServiceVerbose(3, "GetFileVersionInfoSize(%s) -> %u / %Rrc\n", pszFilename, GetLastError(), rc);
287 }
288 return rc;
289}
290
291
292/**
293 * Gets a re-formatted version string from the VS_FIXEDFILEINFO table.
294 *
295 * @returns VBox status code. The output buffer is always valid and the status
296 * code can safely be ignored.
297 *
298 * @param pszPath The base path.
299 * @param pszFilaname The filename.
300 * @param pszVersion Where to return the version string.
301 * @param cbVersion The size of the version string buffer. This MUST be
302 * at least 2 bytes!
303 */
304int VBoxServiceGetFileVersionString(const char *pszPath, const char *pszFilename,
305 char *pszVersion, size_t cbVersion)
306{
307 /*
308 * We will ALWAYS return with a valid output buffer.
309 */
310 AssertReturn(cbVersion >= 2, VERR_BUFFER_OVERFLOW);
311 pszVersion[0] = '-';
312 pszVersion[1] = '\0';
313
314 /*
315 * Create the path and query the bits.
316 */
317 char szFullPath[RTPATH_MAX];
318 int rc = RTPathJoin(szFullPath, sizeof(szFullPath), pszPath, pszFilename);
319 if (RT_SUCCESS(rc))
320 {
321 DWORD dwMajor, dwMinor, dwBuild, dwRev;
322 rc = VBoxServiceGetFileVersion(szFullPath, &dwMajor, &dwMinor, &dwBuild, &dwRev);
323 if (RT_SUCCESS(rc))
324 RTStrPrintf(pszVersion, cbVersion, "%u.%u.%ur%u", dwMajor, dwMinor, dwBuild, dwRev);
325 }
326 return rc;
327}
328
329#endif /* RT_OS_WINDOWS */
330
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette