VirtualBox

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

Last change on this file since 68643 was 62850, checked in by vboxsync, 8 years ago

GAs/common: warnings

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