VirtualBox

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

Last change on this file since 44824 was 44570, checked in by vboxsync, 12 years ago

Forward ported r83625 + r83626 (VBoxServiceVMInfo: Don't NULL LA client info, check for more pointers, VBoxServiceUtils: Pointer checks, don't leak property values if access is denied).

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