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