1 | /* $Id: VBoxGuestR3LibAdditions.cpp 30959 2010-07-21 13:22:08Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxGuestR3Lib - Ring-3 Support Library for VirtualBox guest additions, Additions Info.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2007-2010 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 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*******************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *******************************************************************************/
|
---|
31 | #include <iprt/mem.h>
|
---|
32 | #include <iprt/string.h>
|
---|
33 | #include <VBox/log.h>
|
---|
34 | #include <VBox/version.h>
|
---|
35 | #include "VBGLR3Internal.h"
|
---|
36 |
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * Fallback for vbglR3GetAdditionsVersion.
|
---|
40 | */
|
---|
41 | static int vbglR3GetAdditionsCompileTimeVersion(char **ppszVer, char **ppszRev)
|
---|
42 | {
|
---|
43 | if (ppszVer)
|
---|
44 | {
|
---|
45 | *ppszVer = RTStrDup(VBOX_VERSION_STRING);
|
---|
46 | if (!*ppszVer)
|
---|
47 | return VERR_NO_STR_MEMORY;
|
---|
48 | }
|
---|
49 |
|
---|
50 | if (ppszRev)
|
---|
51 | {
|
---|
52 | char szRev[64];
|
---|
53 | RTStrPrintf(szRev, sizeof(szRev), "%d", VBOX_SVN_REV);
|
---|
54 | *ppszRev = RTStrDup(szRev);
|
---|
55 | if (!*ppszRev)
|
---|
56 | {
|
---|
57 | if (ppszVer)
|
---|
58 | {
|
---|
59 | RTStrFree(*ppszVer);
|
---|
60 | *ppszVer = NULL;
|
---|
61 | }
|
---|
62 | return VERR_NO_STR_MEMORY;
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | return VINF_SUCCESS;
|
---|
67 | }
|
---|
68 |
|
---|
69 | #ifdef RT_OS_WINDOWS
|
---|
70 | /**
|
---|
71 | * Looks up the storage path handle (registry).
|
---|
72 | *
|
---|
73 | * @returns IPRT status value
|
---|
74 | * @param hKey Receives pointer of allocated version string. NULL is
|
---|
75 | * accepted. The returned pointer must be closed by
|
---|
76 | * vbglR3CloseAdditionsWinStoragePath().
|
---|
77 | */
|
---|
78 | static int vbglR3GetAdditionsWinStoragePath(PHKEY phKey)
|
---|
79 | {
|
---|
80 | /*
|
---|
81 | * Try get the *installed* version first.
|
---|
82 | */
|
---|
83 | LONG r;
|
---|
84 |
|
---|
85 | /* Check the built in vendor path first. */
|
---|
86 | char szPath[255];
|
---|
87 | RTStrPrintf(szPath, sizeof(szPath), "SOFTWARE\\%s\\VirtualBox Guest Additions", VBOX_VENDOR_SHORT);
|
---|
88 | r = RegOpenKeyEx(HKEY_LOCAL_MACHINE, szPath, 0, KEY_READ, phKey);
|
---|
89 | # ifdef RT_ARCH_AMD64
|
---|
90 | if (r != ERROR_SUCCESS)
|
---|
91 | {
|
---|
92 | /* Check Wow6432Node. */
|
---|
93 | RTStrPrintf(szPath, sizeof(szPath), "SOFTWARE\\Wow6432Node\\%s\\VirtualBox Guest Additions", VBOX_VENDOR_SHORT);
|
---|
94 | r = RegOpenKeyEx(HKEY_LOCAL_MACHINE, szPath, 0, KEY_READ, phKey);
|
---|
95 | }
|
---|
96 | # endif
|
---|
97 |
|
---|
98 | /* Check the "Sun" path first. */
|
---|
99 | if (r != ERROR_SUCCESS)
|
---|
100 | {
|
---|
101 | r = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Sun\\VirtualBox Guest Additions", 0, KEY_READ, phKey);
|
---|
102 | # ifdef RT_ARCH_AMD64
|
---|
103 | if (r != ERROR_SUCCESS)
|
---|
104 | {
|
---|
105 | /* Check Wow6432Node (for new entries). */
|
---|
106 | r = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Wow6432Node\\Sun\\VirtualBox Guest Additions", 0, KEY_READ, phKey);
|
---|
107 | }
|
---|
108 | # endif
|
---|
109 | }
|
---|
110 |
|
---|
111 | /* Still no luck? Then try the old "Sun xVM" paths ... */
|
---|
112 | if (r != ERROR_SUCCESS)
|
---|
113 | {
|
---|
114 | r = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Sun\\xVM VirtualBox Guest Additions", 0, KEY_READ, phKey);
|
---|
115 | # ifdef RT_ARCH_AMD64
|
---|
116 | if (r != ERROR_SUCCESS)
|
---|
117 | {
|
---|
118 | /* Check Wow6432Node (for new entries). */
|
---|
119 | r = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Wow6432Node\\Sun\\xVM VirtualBox Guest Additions", 0, KEY_READ, phKey);
|
---|
120 | }
|
---|
121 | # endif
|
---|
122 | }
|
---|
123 | return RTErrConvertFromWin32(r);
|
---|
124 | }
|
---|
125 |
|
---|
126 |
|
---|
127 | /**
|
---|
128 | * Closes the storage path handle (registry).
|
---|
129 | *
|
---|
130 | * @returns IPRT status value
|
---|
131 | * @param hKey Handle to close, retrieved by
|
---|
132 | * vbglR3GetAdditionsWinStoragePath().
|
---|
133 | */
|
---|
134 | static int vbglR3CloseAdditionsWinStoragePath(HKEY hKey)
|
---|
135 | {
|
---|
136 | return RTErrConvertFromWin32(RegCloseKey(hKey));
|
---|
137 | }
|
---|
138 |
|
---|
139 | #endif /* RT_OS_WINDOWS */
|
---|
140 |
|
---|
141 | /**
|
---|
142 | * Reports the Guest Additions status of a certain facility to the host.
|
---|
143 | *
|
---|
144 | * @returns IPRT status value
|
---|
145 | * @param enmFacility The facility to report the status on.
|
---|
146 | * @param enmStatus The new status of the facility.
|
---|
147 | * @param fReserved Reserved for future use (what?).
|
---|
148 | */
|
---|
149 | VBGLR3DECL(int) VbglR3ReportAdditionsStatus(VBoxGuestStatusFacility enmFacility,
|
---|
150 | VBoxGuestStatusCurrent enmStatusCurrent,
|
---|
151 | uint32_t fReserved)
|
---|
152 | {
|
---|
153 | VMMDevReportGuestStatus Report;
|
---|
154 | RT_ZERO(Report);
|
---|
155 | int rc = vmmdevInitRequest((VMMDevRequestHeader*)&Report, VMMDevReq_ReportGuestStatus);
|
---|
156 | if (RT_SUCCESS(rc))
|
---|
157 | {
|
---|
158 | Report.guestStatus.facility = enmFacility;
|
---|
159 | Report.guestStatus.status = enmStatusCurrent;
|
---|
160 | Report.guestStatus.flags = fReserved;
|
---|
161 |
|
---|
162 | rc = vbglR3GRPerform(&Report.header);
|
---|
163 | }
|
---|
164 | return rc;
|
---|
165 | }
|
---|
166 |
|
---|
167 | /**
|
---|
168 | * Retrieves the installed Guest Additions version and/or revision.
|
---|
169 | *
|
---|
170 | * @returns IPRT status value
|
---|
171 | * @param ppszVer Receives pointer of allocated version string. NULL is
|
---|
172 | * accepted. The returned pointer must be freed using
|
---|
173 | * RTStrFree().
|
---|
174 | * @param ppszRev Receives pointer of allocated revision string. NULL is
|
---|
175 | * accepted. The returned pointer must be freed using
|
---|
176 | * RTStrFree().
|
---|
177 | */
|
---|
178 | VBGLR3DECL(int) VbglR3GetAdditionsVersion(char **ppszVer, char **ppszRev)
|
---|
179 | {
|
---|
180 | #ifdef RT_OS_WINDOWS
|
---|
181 | HKEY hKey;
|
---|
182 | int rc = vbglR3GetAdditionsWinStoragePath(&hKey);
|
---|
183 | if (RT_SUCCESS(rc))
|
---|
184 | {
|
---|
185 | /* Version. */
|
---|
186 | LONG l;
|
---|
187 | DWORD dwType;
|
---|
188 | DWORD dwSize = 32;
|
---|
189 | char *pszTmp;
|
---|
190 | if (ppszVer)
|
---|
191 | {
|
---|
192 | pszTmp = (char*)RTMemAlloc(dwSize);
|
---|
193 | if (pszTmp)
|
---|
194 | {
|
---|
195 | l = RegQueryValueEx(hKey, "Version", NULL, &dwType, (BYTE*)(LPCTSTR)pszTmp, &dwSize);
|
---|
196 | if (l == ERROR_SUCCESS)
|
---|
197 | {
|
---|
198 | if (dwType == REG_SZ)
|
---|
199 | rc = RTStrDupEx(ppszVer, pszTmp);
|
---|
200 | else
|
---|
201 | rc = VERR_INVALID_PARAMETER;
|
---|
202 | }
|
---|
203 | else
|
---|
204 | {
|
---|
205 | rc = RTErrConvertFromWin32(l);
|
---|
206 | }
|
---|
207 | RTMemFree(pszTmp);
|
---|
208 | }
|
---|
209 | else
|
---|
210 | rc = VERR_NO_MEMORY;
|
---|
211 | }
|
---|
212 | /* Revision. */
|
---|
213 | if (ppszRev)
|
---|
214 | {
|
---|
215 | dwSize = 32; /* Reset */
|
---|
216 | pszTmp = (char*)RTMemAlloc(dwSize);
|
---|
217 | if (pszTmp)
|
---|
218 | {
|
---|
219 | l = RegQueryValueEx(hKey, "Revision", NULL, &dwType, (BYTE*)(LPCTSTR)pszTmp, &dwSize);
|
---|
220 | if (l == ERROR_SUCCESS)
|
---|
221 | {
|
---|
222 | if (dwType == REG_SZ)
|
---|
223 | rc = RTStrDupEx(ppszRev, pszTmp);
|
---|
224 | else
|
---|
225 | rc = VERR_INVALID_PARAMETER;
|
---|
226 | }
|
---|
227 | else
|
---|
228 | {
|
---|
229 | rc = RTErrConvertFromWin32(l);
|
---|
230 | }
|
---|
231 | RTMemFree(pszTmp);
|
---|
232 | }
|
---|
233 | else
|
---|
234 | rc = VERR_NO_MEMORY;
|
---|
235 |
|
---|
236 | if (RT_FAILURE(rc) && ppszVer)
|
---|
237 | {
|
---|
238 | RTStrFree(*ppszVer);
|
---|
239 | *ppszVer = NULL;
|
---|
240 | }
|
---|
241 | }
|
---|
242 | rc = vbglR3CloseAdditionsWinStoragePath(hKey);
|
---|
243 | }
|
---|
244 | else
|
---|
245 | {
|
---|
246 | /*
|
---|
247 | * No registry entries found, return the version string compiled
|
---|
248 | * into this binary.
|
---|
249 | */
|
---|
250 | rc = vbglR3GetAdditionsCompileTimeVersion(ppszVer, ppszRev);
|
---|
251 | }
|
---|
252 | return rc;
|
---|
253 |
|
---|
254 | #else /* !RT_OS_WINDOWS */
|
---|
255 | /*
|
---|
256 | * On non-Windows platforms just return the compile-time version string.
|
---|
257 | */
|
---|
258 | return vbglR3GetAdditionsCompileTimeVersion(ppszVer, ppszRev);
|
---|
259 | #endif /* !RT_OS_WINDOWS */
|
---|
260 | }
|
---|
261 |
|
---|
262 |
|
---|
263 | /**
|
---|
264 | * Retrieves the installation path of Guest Additions.
|
---|
265 | *
|
---|
266 | * @returns IPRT status value
|
---|
267 | * @param ppszPath Receives pointer of allocated installation path string.
|
---|
268 | * The returned pointer must be freed using
|
---|
269 | * RTStrFree().
|
---|
270 | */
|
---|
271 | VBGLR3DECL(int) VbglR3GetAdditionsInstallationPath(char **ppszPath)
|
---|
272 | {
|
---|
273 | int rc;
|
---|
274 | #ifdef RT_OS_WINDOWS
|
---|
275 | HKEY hKey;
|
---|
276 | rc = vbglR3GetAdditionsWinStoragePath(&hKey);
|
---|
277 | if (RT_SUCCESS(rc))
|
---|
278 | {
|
---|
279 | /* Installation directory. */
|
---|
280 | DWORD dwType;
|
---|
281 | DWORD dwSize = _MAX_PATH * sizeof(char);
|
---|
282 | char *pszTmp = (char*)RTMemAlloc(dwSize + 1);
|
---|
283 | if (pszTmp)
|
---|
284 | {
|
---|
285 | LONG l = RegQueryValueEx(hKey, "InstallDir", NULL, &dwType, (BYTE*)(LPCTSTR)pszTmp, &dwSize);
|
---|
286 | if ((l != ERROR_SUCCESS) && (l != ERROR_FILE_NOT_FOUND))
|
---|
287 | {
|
---|
288 | rc = RTErrConvertFromNtStatus(l);
|
---|
289 | }
|
---|
290 | else
|
---|
291 | {
|
---|
292 | if (dwType == REG_SZ)
|
---|
293 | rc = RTStrDupEx(ppszPath, pszTmp);
|
---|
294 | else
|
---|
295 | rc = VERR_INVALID_PARAMETER;
|
---|
296 | if (RT_SUCCESS(rc))
|
---|
297 | {
|
---|
298 | /* Flip slashes. */
|
---|
299 | for (char *pszTmp2 = ppszPath[0]; *pszTmp2; ++pszTmp2)
|
---|
300 | if (*pszTmp2 == '\\')
|
---|
301 | *pszTmp2 = '/';
|
---|
302 | }
|
---|
303 | }
|
---|
304 | RTMemFree(pszTmp);
|
---|
305 | }
|
---|
306 | else
|
---|
307 | rc = VERR_NO_MEMORY;
|
---|
308 | rc = vbglR3CloseAdditionsWinStoragePath(hKey);
|
---|
309 | }
|
---|
310 | #else
|
---|
311 | /** @todo implement me */
|
---|
312 | rc = VERR_NOT_IMPLEMENTED;
|
---|
313 | #endif
|
---|
314 | return rc;
|
---|
315 | }
|
---|
316 |
|
---|