VirtualBox

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

Last change on this file since 58029 was 58029, checked in by vboxsync, 9 years ago

VBoxService: Using prefix 'VGSvc', code style/width cleanups. No real changes.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.9 KB
Line 
1/* $Id: VBoxServiceUtils.cpp 58029 2015-10-05 20:50:18Z vboxsync $ */
2/** @file
3 * VBoxServiceUtils - Some utility functions.
4 */
5
6/*
7 * Copyright (C) 2009-2015 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
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;
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 VBoxServiceGetFileVersion 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 */
272static int vgsvcUtilGetFileVersion(const char *pszFilename, PDWORD pdwMajor, PDWORD pdwMinor, PDWORD pdwBuildNumber,
273 PDWORD pdwRevisionNumber)
274{
275 int rc;
276
277 *pdwMajor = *pdwMinor = *pdwBuildNumber = *pdwRevisionNumber = 0;
278
279 /*
280 * Get the file version info.
281 */
282 DWORD dwHandleIgnored;
283 DWORD cbVerData = GetFileVersionInfoSizeA(pszFilename, &dwHandleIgnored);
284 if (cbVerData)
285 {
286 LPTSTR pVerData = (LPTSTR)RTMemTmpAllocZ(cbVerData);
287 if (pVerData)
288 {
289 if (GetFileVersionInfoA(pszFilename, dwHandleIgnored, cbVerData, pVerData))
290 {
291 /*
292 * Try query and parse the FileVersion string our selves first
293 * since this will give us the correct revision number when
294 * it goes beyond the range of an uint16_t / WORD.
295 */
296 if (vgsvcUtilGetFileVersionOwn(pVerData, pdwMajor, pdwMinor, pdwBuildNumber, pdwRevisionNumber))
297 rc = VINF_SUCCESS;
298 else
299 {
300 /* Fall back on VS_FIXEDFILEINFO */
301 UINT cbFileInfoIgnored = 0;
302 VS_FIXEDFILEINFO *pFileInfo = NULL;
303 if (VerQueryValue(pVerData, "\\", (LPVOID *)&pFileInfo, &cbFileInfoIgnored))
304 {
305 *pdwMajor = HIWORD(pFileInfo->dwFileVersionMS);
306 *pdwMinor = LOWORD(pFileInfo->dwFileVersionMS);
307 *pdwBuildNumber = HIWORD(pFileInfo->dwFileVersionLS);
308 *pdwRevisionNumber = LOWORD(pFileInfo->dwFileVersionLS);
309 rc = VINF_SUCCESS;
310 }
311 else
312 {
313 rc = RTErrConvertFromWin32(GetLastError());
314 VGSvcVerbose(3, "No file version value for file '%s' available! (%d / rc=%Rrc)\n",
315 pszFilename, GetLastError(), rc);
316 }
317 }
318 }
319 else
320 {
321 rc = RTErrConvertFromWin32(GetLastError());
322 VGSvcVerbose(0, "GetFileVersionInfo(%s) -> %u / %Rrc\n", pszFilename, GetLastError(), rc);
323 }
324
325 RTMemTmpFree(pVerData);
326 }
327 else
328 {
329 VGSvcVerbose(0, "Failed to allocate %u byte for file version info for '%s'\n", cbVerData, pszFilename);
330 rc = VERR_NO_TMP_MEMORY;
331 }
332 }
333 else
334 {
335 rc = RTErrConvertFromWin32(GetLastError());
336 VGSvcVerbose(3, "GetFileVersionInfoSize(%s) -> %u / %Rrc\n", pszFilename, GetLastError(), rc);
337 }
338 return rc;
339}
340
341
342/**
343 * Gets a re-formatted version string from the VS_FIXEDFILEINFO table.
344 *
345 * @returns VBox status code. The output buffer is always valid and the status
346 * code can safely be ignored.
347 *
348 * @param pszPath The base path.
349 * @param pszFilaname The filename.
350 * @param pszVersion Where to return the version string.
351 * @param cbVersion The size of the version string buffer. This MUST be
352 * at least 2 bytes!
353 */
354int VGSvcUtilWinGetFileVersionString(const char *pszPath, const char *pszFilename, char *pszVersion, size_t cbVersion)
355{
356 /*
357 * We will ALWAYS return with a valid output buffer.
358 */
359 AssertReturn(cbVersion >= 2, VERR_BUFFER_OVERFLOW);
360 pszVersion[0] = '-';
361 pszVersion[1] = '\0';
362
363 /*
364 * Create the path and query the bits.
365 */
366 char szFullPath[RTPATH_MAX];
367 int rc = RTPathJoin(szFullPath, sizeof(szFullPath), pszPath, pszFilename);
368 if (RT_SUCCESS(rc))
369 {
370 DWORD dwMajor, dwMinor, dwBuild, dwRev;
371 rc = vgsvcUtilGetFileVersion(szFullPath, &dwMajor, &dwMinor, &dwBuild, &dwRev);
372 if (RT_SUCCESS(rc))
373 RTStrPrintf(pszVersion, cbVersion, "%u.%u.%ur%u", dwMajor, dwMinor, dwBuild, dwRev);
374 }
375 return rc;
376}
377
378#endif /* RT_OS_WINDOWS */
379
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