1 | /* $Id: VBoxServiceUtils.cpp 47335 2013-07-23 10:53:37Z 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 | */
|
---|
50 | int 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 | */
|
---|
129 | int 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 IPRT status code, fully bitched.
|
---|
155 | * @param u32ClientId The HGCM client ID for the guest property session.
|
---|
156 | * @param pszPropName The property name.
|
---|
157 | * @param fReadOnly Whether or not this property needs to be read only
|
---|
158 | * by the guest side. Otherwise VERR_ACCESS_DENIED will
|
---|
159 | * be returned.
|
---|
160 | * @param ppszValue Where to return the value. This is always set
|
---|
161 | * to NULL. Free it using RTStrFree().
|
---|
162 | * @param ppszFlags Where to return the value flags. Free it
|
---|
163 | * using RTStrFree(). Optional.
|
---|
164 | * @param puTimestamp Where to return the timestamp. This is only set
|
---|
165 | * on success. Optional.
|
---|
166 | */
|
---|
167 | int VBoxServiceReadHostProp(uint32_t u32ClientId, const char *pszPropName, bool fReadOnly,
|
---|
168 | char **ppszValue, char **ppszFlags, uint64_t *puTimestamp)
|
---|
169 | {
|
---|
170 | AssertPtrReturn(ppszValue, VERR_INVALID_PARAMETER);
|
---|
171 |
|
---|
172 | char *pszValue = NULL;
|
---|
173 | char *pszFlags = NULL;
|
---|
174 | int rc = VBoxServiceReadProp(u32ClientId, pszPropName, &pszValue, &pszFlags, puTimestamp);
|
---|
175 | if (RT_SUCCESS(rc))
|
---|
176 | {
|
---|
177 | /* Check security bits. */
|
---|
178 | if ( fReadOnly /* Do we except a guest read-only property */
|
---|
179 | && !RTStrStr(pszFlags, "RDONLYGUEST"))
|
---|
180 | {
|
---|
181 | /* If we want a property which is read-only on the guest
|
---|
182 | * and it is *not* marked as such, deny access! */
|
---|
183 | rc = VERR_ACCESS_DENIED;
|
---|
184 | }
|
---|
185 |
|
---|
186 | if (RT_SUCCESS(rc))
|
---|
187 | {
|
---|
188 | *ppszValue = pszValue;
|
---|
189 |
|
---|
190 | if (ppszFlags)
|
---|
191 | *ppszFlags = pszFlags;
|
---|
192 | else if (pszFlags)
|
---|
193 | RTStrFree(pszFlags);
|
---|
194 | }
|
---|
195 | else
|
---|
196 | {
|
---|
197 | if (pszValue)
|
---|
198 | RTStrFree(pszValue);
|
---|
199 | if (pszFlags)
|
---|
200 | RTStrFree(pszFlags);
|
---|
201 | }
|
---|
202 | }
|
---|
203 |
|
---|
204 | return rc;
|
---|
205 | }
|
---|
206 |
|
---|
207 |
|
---|
208 | /**
|
---|
209 | * Wrapper around VbglR3GuestPropWriteValue that does value formatting and
|
---|
210 | * logging.
|
---|
211 | *
|
---|
212 | * @returns VBox status code. Errors will be logged.
|
---|
213 | *
|
---|
214 | * @param u32ClientId The HGCM client ID for the guest property session.
|
---|
215 | * @param pszName The property name.
|
---|
216 | * @param pszValueFormat The property format string. If this is NULL then
|
---|
217 | * the property will be deleted (if possible).
|
---|
218 | * @param ... Format arguments.
|
---|
219 | */
|
---|
220 | int VBoxServiceWritePropF(uint32_t u32ClientId, const char *pszName, const char *pszValueFormat, ...)
|
---|
221 | {
|
---|
222 | AssertPtr(pszName);
|
---|
223 | int rc;
|
---|
224 | if (pszValueFormat != NULL)
|
---|
225 | {
|
---|
226 | va_list va;
|
---|
227 | va_start(va, pszValueFormat);
|
---|
228 | VBoxServiceVerbose(3, "Writing guest property \"%s\" = \"%N\"\n", pszName, pszValueFormat, &va);
|
---|
229 | va_end(va);
|
---|
230 |
|
---|
231 | va_start(va, pszValueFormat);
|
---|
232 | rc = VbglR3GuestPropWriteValueV(u32ClientId, pszName, pszValueFormat, va);
|
---|
233 | va_end(va);
|
---|
234 |
|
---|
235 | if (RT_FAILURE(rc))
|
---|
236 | VBoxServiceError("Error writing guest property \"%s\" (rc=%Rrc)\n", pszName, rc);
|
---|
237 | }
|
---|
238 | else
|
---|
239 | {
|
---|
240 | VBoxServiceVerbose(3, "Deleting guest property \"%s\"\n", pszName);
|
---|
241 | rc = VbglR3GuestPropWriteValue(u32ClientId, pszName, NULL);
|
---|
242 | if (RT_FAILURE(rc))
|
---|
243 | VBoxServiceError("Error deleting guest property \"%s\" (rc=%Rrc)\n", pszName, rc);
|
---|
244 | }
|
---|
245 | return rc;
|
---|
246 | }
|
---|
247 |
|
---|
248 | #endif /* VBOX_WITH_GUEST_PROPS */
|
---|
249 | #ifdef RT_OS_WINDOWS
|
---|
250 |
|
---|
251 | /**
|
---|
252 | * Helper for VBoxServiceGetFileVersion and attempts to read and parse
|
---|
253 | * FileVersion.
|
---|
254 | *
|
---|
255 | * @returns Success indicator.
|
---|
256 | */
|
---|
257 | static bool VBoxServiceGetFileVersionOwn(LPSTR pVerData,
|
---|
258 | PDWORD pdwMajor,
|
---|
259 | PDWORD pdwMinor,
|
---|
260 | PDWORD pdwBuildNumber,
|
---|
261 | PDWORD pdwRevisionNumber)
|
---|
262 | {
|
---|
263 | UINT cchStrValue = 0;
|
---|
264 | LPTSTR pStrValue = NULL;
|
---|
265 | if (!VerQueryValueA(pVerData, "\\StringFileInfo\\040904b0\\FileVersion", (LPVOID *)&pStrValue, &cchStrValue))
|
---|
266 | return false;
|
---|
267 |
|
---|
268 | /** @todo r=bird: get rid of this. Avoid sscanf like the plague! */
|
---|
269 | if (sscanf(pStrValue, "%ld.%ld.%ld.%ld", pdwMajor, pdwMinor, pdwBuildNumber, pdwRevisionNumber) != 4)
|
---|
270 | return false;
|
---|
271 |
|
---|
272 | return true;
|
---|
273 | }
|
---|
274 |
|
---|
275 |
|
---|
276 | /**
|
---|
277 | * Worker for VBoxServiceGetFileVersionString.
|
---|
278 | *
|
---|
279 | * @returns VBox status code.
|
---|
280 | * @param pszFilename ASCII & ANSI & UTF-8 compliant name.
|
---|
281 | */
|
---|
282 | static int VBoxServiceGetFileVersion(const char *pszFilename,
|
---|
283 | PDWORD pdwMajor,
|
---|
284 | PDWORD pdwMinor,
|
---|
285 | PDWORD pdwBuildNumber,
|
---|
286 | PDWORD pdwRevisionNumber)
|
---|
287 | {
|
---|
288 | int rc;
|
---|
289 |
|
---|
290 | *pdwMajor = *pdwMinor = *pdwBuildNumber = *pdwRevisionNumber = 0;
|
---|
291 |
|
---|
292 | /*
|
---|
293 | * Get the file version info.
|
---|
294 | */
|
---|
295 | DWORD dwHandleIgnored;
|
---|
296 | DWORD cbVerData = GetFileVersionInfoSizeA(pszFilename, &dwHandleIgnored);
|
---|
297 | if (cbVerData)
|
---|
298 | {
|
---|
299 | LPTSTR pVerData = (LPTSTR)RTMemTmpAllocZ(cbVerData);
|
---|
300 | if (pVerData)
|
---|
301 | {
|
---|
302 | if (GetFileVersionInfoA(pszFilename, dwHandleIgnored, cbVerData, pVerData))
|
---|
303 | {
|
---|
304 | /*
|
---|
305 | * Try query and parse the FileVersion string our selves first
|
---|
306 | * since this will give us the correct revision number when
|
---|
307 | * it goes beyond the range of an uint16_t / WORD.
|
---|
308 | */
|
---|
309 | if (VBoxServiceGetFileVersionOwn(pVerData, pdwMajor, pdwMinor, pdwBuildNumber, pdwRevisionNumber))
|
---|
310 | rc = VINF_SUCCESS;
|
---|
311 | else
|
---|
312 | {
|
---|
313 | /* Fall back on VS_FIXEDFILEINFO */
|
---|
314 | UINT cbFileInfoIgnored = 0;
|
---|
315 | VS_FIXEDFILEINFO *pFileInfo = NULL;
|
---|
316 | if (VerQueryValue(pVerData, "\\", (LPVOID *)&pFileInfo, &cbFileInfoIgnored))
|
---|
317 | {
|
---|
318 | *pdwMajor = HIWORD(pFileInfo->dwFileVersionMS);
|
---|
319 | *pdwMinor = LOWORD(pFileInfo->dwFileVersionMS);
|
---|
320 | *pdwBuildNumber = HIWORD(pFileInfo->dwFileVersionLS);
|
---|
321 | *pdwRevisionNumber = LOWORD(pFileInfo->dwFileVersionLS);
|
---|
322 | rc = VINF_SUCCESS;
|
---|
323 | }
|
---|
324 | else
|
---|
325 | {
|
---|
326 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
327 | VBoxServiceVerbose(3, "No file version value for file \"%s\" available! (%d / rc=%Rrc)\n",
|
---|
328 | pszFilename, GetLastError(), rc);
|
---|
329 | }
|
---|
330 | }
|
---|
331 | }
|
---|
332 | else
|
---|
333 | {
|
---|
334 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
335 | VBoxServiceVerbose(0, "GetFileVersionInfo(%s) -> %u / %Rrc\n", pszFilename, GetLastError(), rc);
|
---|
336 | }
|
---|
337 |
|
---|
338 | RTMemTmpFree(pVerData);
|
---|
339 | }
|
---|
340 | else
|
---|
341 | {
|
---|
342 | VBoxServiceVerbose(0, "Failed to allocate %u byte for file version info for '%s'\n", cbVerData, pszFilename);
|
---|
343 | rc = VERR_NO_TMP_MEMORY;
|
---|
344 | }
|
---|
345 | }
|
---|
346 | else
|
---|
347 | {
|
---|
348 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
349 | VBoxServiceVerbose(3, "GetFileVersionInfoSize(%s) -> %u / %Rrc\n", pszFilename, GetLastError(), rc);
|
---|
350 | }
|
---|
351 | return rc;
|
---|
352 | }
|
---|
353 |
|
---|
354 |
|
---|
355 | /**
|
---|
356 | * Gets a re-formatted version string from the VS_FIXEDFILEINFO table.
|
---|
357 | *
|
---|
358 | * @returns VBox status code. The output buffer is always valid and the status
|
---|
359 | * code can safely be ignored.
|
---|
360 | *
|
---|
361 | * @param pszPath The base path.
|
---|
362 | * @param pszFilaname The filename.
|
---|
363 | * @param pszVersion Where to return the version string.
|
---|
364 | * @param cbVersion The size of the version string buffer. This MUST be
|
---|
365 | * at least 2 bytes!
|
---|
366 | */
|
---|
367 | int VBoxServiceGetFileVersionString(const char *pszPath, const char *pszFilename,
|
---|
368 | char *pszVersion, size_t cbVersion)
|
---|
369 | {
|
---|
370 | /*
|
---|
371 | * We will ALWAYS return with a valid output buffer.
|
---|
372 | */
|
---|
373 | AssertReturn(cbVersion >= 2, VERR_BUFFER_OVERFLOW);
|
---|
374 | pszVersion[0] = '-';
|
---|
375 | pszVersion[1] = '\0';
|
---|
376 |
|
---|
377 | /*
|
---|
378 | * Create the path and query the bits.
|
---|
379 | */
|
---|
380 | char szFullPath[RTPATH_MAX];
|
---|
381 | int rc = RTPathJoin(szFullPath, sizeof(szFullPath), pszPath, pszFilename);
|
---|
382 | if (RT_SUCCESS(rc))
|
---|
383 | {
|
---|
384 | DWORD dwMajor, dwMinor, dwBuild, dwRev;
|
---|
385 | rc = VBoxServiceGetFileVersion(szFullPath, &dwMajor, &dwMinor, &dwBuild, &dwRev);
|
---|
386 | if (RT_SUCCESS(rc))
|
---|
387 | RTStrPrintf(pszVersion, cbVersion, "%u.%u.%ur%u", dwMajor, dwMinor, dwBuild, dwRev);
|
---|
388 | }
|
---|
389 | return rc;
|
---|
390 | }
|
---|
391 |
|
---|
392 | #endif /* RT_OS_WINDOWS */
|
---|
393 |
|
---|