1 | /* $Revision: 52846 $ */
|
---|
2 | /** @file
|
---|
3 | * Glue code for dynamically linking to VBoxCAPI.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2008-2014 Oracle Corporation
|
---|
8 | *
|
---|
9 | * Permission is hereby granted, free of charge, to any person
|
---|
10 | * obtaining a copy of this software and associated documentation
|
---|
11 | * files (the "Software"), to deal in the Software without
|
---|
12 | * restriction, including without limitation the rights to use,
|
---|
13 | * copy, modify, merge, publish, distribute, sublicense, and/or sell
|
---|
14 | * copies of the Software, and to permit persons to whom the
|
---|
15 | * Software is furnished to do so, subject to the following
|
---|
16 | * conditions:
|
---|
17 | *
|
---|
18 | * The above copyright notice and this permission notice shall be
|
---|
19 | * included in all copies or substantial portions of the Software.
|
---|
20 | *
|
---|
21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
---|
22 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
---|
23 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
---|
24 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
---|
25 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
---|
26 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
---|
27 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
---|
28 | * OTHER DEALINGS IN THE SOFTWARE.
|
---|
29 | */
|
---|
30 |
|
---|
31 | /*******************************************************************************
|
---|
32 | * Header Files *
|
---|
33 | *******************************************************************************/
|
---|
34 | #include "VBoxCAPIGlue.h"
|
---|
35 |
|
---|
36 | #include <stdio.h>
|
---|
37 | #include <string.h>
|
---|
38 | #include <stdlib.h>
|
---|
39 | #include <stdarg.h>
|
---|
40 | #include <stdint.h>
|
---|
41 | #ifndef WIN32
|
---|
42 | # include <dlfcn.h>
|
---|
43 | # include <pthread.h>
|
---|
44 | #else /* WIN32 */
|
---|
45 | # include <Windows.h>
|
---|
46 | #endif /* WIN32 */
|
---|
47 |
|
---|
48 |
|
---|
49 | /*******************************************************************************
|
---|
50 | * Defined Constants And Macros *
|
---|
51 | *******************************************************************************/
|
---|
52 | #if defined(__linux__) || defined(__linux_gnu__) || defined(__sun__) || defined(__FreeBSD__)
|
---|
53 | # define DYNLIB_NAME "VBoxXPCOMC.so"
|
---|
54 | #elif defined(__APPLE__)
|
---|
55 | # define DYNLIB_NAME "VBoxXPCOMC.dylib"
|
---|
56 | #elif defined(__OS2__)
|
---|
57 | # define DYNLIB_NAME "VBoxXPCOMC.dll"
|
---|
58 | #elif defined(WIN32)
|
---|
59 | # define DYNLIB_NAME "VBoxCAPI.dll"
|
---|
60 | #else
|
---|
61 | # error "Port me"
|
---|
62 | #endif
|
---|
63 |
|
---|
64 |
|
---|
65 | /*******************************************************************************
|
---|
66 | * Global Variables *
|
---|
67 | *******************************************************************************/
|
---|
68 | /** The so/dynsym/dll handle for VBoxCAPI. */
|
---|
69 | #ifndef WIN32
|
---|
70 | void *g_hVBoxCAPI = NULL;
|
---|
71 | #else /* WIN32 */
|
---|
72 | HMODULE g_hVBoxCAPI = NULL;
|
---|
73 | #endif /* WIN32 */
|
---|
74 | /** The last load error. */
|
---|
75 | char g_szVBoxErrMsg[256] = "";
|
---|
76 | /** Pointer to the VBOXCAPI function table. */
|
---|
77 | PCVBOXCAPI g_pVBoxFuncs = NULL;
|
---|
78 | /** Pointer to VBoxGetCAPIFunctions for the loaded VBoxCAPI so/dylib/dll. */
|
---|
79 | PFNVBOXGETCAPIFUNCTIONS g_pfnGetFunctions = NULL;
|
---|
80 |
|
---|
81 | typedef void FNDUMMY(void);
|
---|
82 | typedef FNDUMMY *PFNDUMMY;
|
---|
83 | /** Just a dummy global structure containing a bunch of
|
---|
84 | * function pointers to code which is wanted in the link. */
|
---|
85 | PFNDUMMY g_apfnVBoxCAPIGlue[] =
|
---|
86 | {
|
---|
87 | #ifndef WIN32
|
---|
88 | /* The following link dependency is for helping gdb as it gets hideously
|
---|
89 | * confused if the application doesn't drag in pthreads, but uses it. */
|
---|
90 | (PFNDUMMY)pthread_create,
|
---|
91 | #endif /* !WIN32 */
|
---|
92 | NULL
|
---|
93 | };
|
---|
94 |
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * Wrapper for setting g_szVBoxErrMsg. Can be an empty stub.
|
---|
98 | *
|
---|
99 | * @param fAlways When 0 the g_szVBoxErrMsg is only set if empty.
|
---|
100 | * @param pszFormat The format string.
|
---|
101 | * @param ... The arguments.
|
---|
102 | */
|
---|
103 | static void setErrMsg(int fAlways, const char *pszFormat, ...)
|
---|
104 | {
|
---|
105 | if ( fAlways
|
---|
106 | || !g_szVBoxErrMsg[0])
|
---|
107 | {
|
---|
108 | va_list va;
|
---|
109 | va_start(va, pszFormat);
|
---|
110 | vsnprintf(g_szVBoxErrMsg, sizeof(g_szVBoxErrMsg), pszFormat, va);
|
---|
111 | va_end(va);
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 |
|
---|
116 | /**
|
---|
117 | * Try load C API .so/dylib/dll from the specified location and resolve all
|
---|
118 | * the symbols we need. Tries both the new style and legacy name.
|
---|
119 | *
|
---|
120 | * @returns 0 on success, -1 on failure.
|
---|
121 | * @param pszHome The directory where to try load VBoxCAPI/VBoxXPCOMC
|
---|
122 | * from. Can be NULL.
|
---|
123 | * @param fSetAppHome Whether to set the VBOX_APP_HOME env.var. or not
|
---|
124 | * (boolean).
|
---|
125 | */
|
---|
126 | static int tryLoadLibrary(const char *pszHome, int fSetAppHome)
|
---|
127 | {
|
---|
128 | size_t cchHome = pszHome ? strlen(pszHome) : 0;
|
---|
129 | size_t cbBufNeeded;
|
---|
130 | char szName[4096];
|
---|
131 |
|
---|
132 | /*
|
---|
133 | * Construct the full name.
|
---|
134 | */
|
---|
135 | cbBufNeeded = cchHome + sizeof("/" DYNLIB_NAME);
|
---|
136 | if (cbBufNeeded > sizeof(szName))
|
---|
137 | {
|
---|
138 | setErrMsg(1, "path buffer too small: %u bytes needed",
|
---|
139 | (unsigned)cbBufNeeded);
|
---|
140 | return -1;
|
---|
141 | }
|
---|
142 | if (cchHome)
|
---|
143 | {
|
---|
144 | memcpy(szName, pszHome, cchHome);
|
---|
145 | szName[cchHome] = '/';
|
---|
146 | cchHome++;
|
---|
147 | }
|
---|
148 | memcpy(&szName[cchHome], DYNLIB_NAME, sizeof(DYNLIB_NAME));
|
---|
149 |
|
---|
150 | /*
|
---|
151 | * Try load it by that name, setting the VBOX_APP_HOME first (for now).
|
---|
152 | * Then resolve and call the function table getter.
|
---|
153 | */
|
---|
154 | if (fSetAppHome)
|
---|
155 | {
|
---|
156 | #ifndef WIN32
|
---|
157 | if (pszHome)
|
---|
158 | setenv("VBOX_APP_HOME", pszHome, 1 /* always override */);
|
---|
159 | else
|
---|
160 | unsetenv("VBOX_APP_HOME");
|
---|
161 | #endif /* !WIN32 */
|
---|
162 | }
|
---|
163 |
|
---|
164 | #ifndef WIN32
|
---|
165 | g_hVBoxCAPI = dlopen(szName, RTLD_NOW | RTLD_LOCAL);
|
---|
166 | if (g_hVBoxCAPI)
|
---|
167 | {
|
---|
168 | PFNVBOXGETCAPIFUNCTIONS pfnGetFunctions;
|
---|
169 | pfnGetFunctions = (PFNVBOXGETCAPIFUNCTIONS)(uintptr_t)
|
---|
170 | dlsym(g_hVBoxCAPI, VBOX_GET_CAPI_FUNCTIONS_SYMBOL_NAME);
|
---|
171 | #ifdef VBOX_GET_XPCOM_FUNCTIONS_SYMBOL_NAME
|
---|
172 | if (!pfnGetFunctions)
|
---|
173 | pfnGetFunctions = (PFNVBOXGETCAPIFUNCTIONS)(uintptr_t)
|
---|
174 | dlsym(g_hVBoxCAPI, VBOX_GET_XPCOM_FUNCTIONS_SYMBOL_NAME);
|
---|
175 | #endif /* VBOX_GET_XPCOM_FUNCTIONS_SYMBOL_NAME */
|
---|
176 | if (pfnGetFunctions)
|
---|
177 | {
|
---|
178 | g_pVBoxFuncs = pfnGetFunctions(VBOX_CAPI_VERSION);
|
---|
179 | if (g_pVBoxFuncs)
|
---|
180 | {
|
---|
181 | g_pfnGetFunctions = pfnGetFunctions;
|
---|
182 | return 0;
|
---|
183 | }
|
---|
184 |
|
---|
185 | /* bail out */
|
---|
186 | setErrMsg(1, "%.80s: pfnGetFunctions(%#x) failed",
|
---|
187 | szName, VBOX_CAPI_VERSION);
|
---|
188 | }
|
---|
189 | else
|
---|
190 | setErrMsg(1, "dlsym(%.80s/%.32s): %.128s",
|
---|
191 | szName, VBOX_GET_CAPI_FUNCTIONS_SYMBOL_NAME, dlerror());
|
---|
192 | dlclose(g_hVBoxCAPI);
|
---|
193 | g_hVBoxCAPI = NULL;
|
---|
194 | }
|
---|
195 | else
|
---|
196 | setErrMsg(0, "dlopen(%.80s): %.160s", szName, dlerror());
|
---|
197 | #else /* !WIN32 */
|
---|
198 | g_hVBoxCAPI = LoadLibraryExA(szName, NULL /* hFile */, 0 /* dwFlags */);
|
---|
199 | if (g_hVBoxCAPI)
|
---|
200 | {
|
---|
201 | PFNVBOXGETCAPIFUNCTIONS pfnGetFunctions;
|
---|
202 | pfnGetFunctions = (PFNVBOXGETCAPIFUNCTIONS)
|
---|
203 | GetProcAddress(g_hVBoxCAPI, VBOX_GET_CAPI_FUNCTIONS_SYMBOL_NAME);
|
---|
204 | if (pfnGetFunctions)
|
---|
205 | {
|
---|
206 | g_pVBoxFuncs = pfnGetFunctions(VBOX_CAPI_VERSION);
|
---|
207 | if (g_pVBoxFuncs)
|
---|
208 | {
|
---|
209 | g_pfnGetFunctions = pfnGetFunctions;
|
---|
210 | return 0;
|
---|
211 | }
|
---|
212 |
|
---|
213 | /* bail out */
|
---|
214 | setErrMsg(1, "%.80s: pfnGetFunctions(%#x) failed",
|
---|
215 | szName, VBOX_CAPI_VERSION);
|
---|
216 | }
|
---|
217 | else
|
---|
218 | setErrMsg(1, "GetProcAddress(%.80s/%.32s): %d",
|
---|
219 | szName, VBOX_GET_CAPI_FUNCTIONS_SYMBOL_NAME, GetLastError());
|
---|
220 | FreeLibrary(g_hVBoxCAPI);
|
---|
221 | g_hVBoxCAPI = NULL;
|
---|
222 | }
|
---|
223 | else
|
---|
224 | setErrMsg(0, "LoadLibraryEx(%.80s): %d", szName, GetLastError());
|
---|
225 | #endif /* !WIN32 */
|
---|
226 |
|
---|
227 | return -1;
|
---|
228 | }
|
---|
229 |
|
---|
230 |
|
---|
231 | /**
|
---|
232 | * Tries to locate and load VBoxCAPI.so/dylib/dll, resolving all the related
|
---|
233 | * function pointers.
|
---|
234 | *
|
---|
235 | * @returns 0 on success, -1 on failure.
|
---|
236 | *
|
---|
237 | * @remark This should be considered moved into a separate glue library since
|
---|
238 | * its its going to be pretty much the same for any user of VBoxCAPI
|
---|
239 | * and it will just cause trouble to have duplicate versions of this
|
---|
240 | * source code all around the place.
|
---|
241 | */
|
---|
242 | int VBoxCGlueInit(void)
|
---|
243 | {
|
---|
244 | const char *pszHome;
|
---|
245 |
|
---|
246 | memset(g_szVBoxErrMsg, 0, sizeof(g_szVBoxErrMsg));
|
---|
247 |
|
---|
248 | /*
|
---|
249 | * If the user specifies the location, try only that.
|
---|
250 | */
|
---|
251 | pszHome = getenv("VBOX_APP_HOME");
|
---|
252 | if (pszHome)
|
---|
253 | return tryLoadLibrary(pszHome, 0);
|
---|
254 |
|
---|
255 | /*
|
---|
256 | * Try the known standard locations.
|
---|
257 | */
|
---|
258 | #if defined(__gnu__linux__) || defined(__linux__)
|
---|
259 | if (tryLoadLibrary("/opt/VirtualBox", 1) == 0)
|
---|
260 | return 0;
|
---|
261 | if (tryLoadLibrary("/usr/lib/virtualbox", 1) == 0)
|
---|
262 | return 0;
|
---|
263 | #elif defined(__sun__)
|
---|
264 | if (tryLoadLibrary("/opt/VirtualBox/amd64", 1) == 0)
|
---|
265 | return 0;
|
---|
266 | if (tryLoadLibrary("/opt/VirtualBox/i386", 1) == 0)
|
---|
267 | return 0;
|
---|
268 | #elif defined(__APPLE__)
|
---|
269 | if (tryLoadLibrary("/Applications/VirtualBox.app/Contents/MacOS", 1) == 0)
|
---|
270 | return 0;
|
---|
271 | #elif defined(__FreeBSD__)
|
---|
272 | if (tryLoadLibrary("/usr/local/lib/virtualbox", 1) == 0)
|
---|
273 | return 0;
|
---|
274 | #elif defined(__OS2__)
|
---|
275 | if (tryLoadLibrary("C:/Apps/VirtualBox", 1) == 0)
|
---|
276 | return 0;
|
---|
277 | #elif defined(WIN32)
|
---|
278 | pszHome = getenv("ProgramFiles");
|
---|
279 | if (pszHome)
|
---|
280 | {
|
---|
281 | char szPath[4096];
|
---|
282 | size_t cb = sizeof(szPath);
|
---|
283 | char *tmp = szPath;
|
---|
284 | strncpy(tmp, pszHome, cb);
|
---|
285 | tmp[cb - 1] = '\0';
|
---|
286 | cb -= strlen(tmp);
|
---|
287 | tmp += strlen(tmp);
|
---|
288 | strncpy(tmp, "/Oracle/VirtualBox", cb);
|
---|
289 | tmp[cb - 1] = '\0';
|
---|
290 | if (tryLoadLibrary(szPath, 1) == 0)
|
---|
291 | return 0;
|
---|
292 | }
|
---|
293 | if (tryLoadLibrary("C:/Program Files/Oracle/VirtualBox", 1) == 0)
|
---|
294 | return 0;
|
---|
295 | #else
|
---|
296 | # error "port me"
|
---|
297 | #endif
|
---|
298 |
|
---|
299 | /*
|
---|
300 | * Finally try the dynamic linker search path.
|
---|
301 | */
|
---|
302 | if (tryLoadLibrary(NULL, 1) == 0)
|
---|
303 | return 0;
|
---|
304 |
|
---|
305 | /* No luck, return failure. */
|
---|
306 | return -1;
|
---|
307 | }
|
---|
308 |
|
---|
309 |
|
---|
310 | /**
|
---|
311 | * Terminate the C glue library.
|
---|
312 | */
|
---|
313 | void VBoxCGlueTerm(void)
|
---|
314 | {
|
---|
315 | if (g_hVBoxCAPI)
|
---|
316 | {
|
---|
317 | #if 0 /* VBoxRT.so doesn't like being reloaded. See @bugref{3725}. */
|
---|
318 | #ifndef WIN32
|
---|
319 | dlclose(g_hVBoxCAPI);
|
---|
320 | #else
|
---|
321 | FreeLibrary(g_hVBoxCAPI);
|
---|
322 | #endif
|
---|
323 | #endif
|
---|
324 | g_hVBoxCAPI = NULL;
|
---|
325 | }
|
---|
326 | g_pVBoxFuncs = NULL;
|
---|
327 | g_pfnGetFunctions = NULL;
|
---|
328 | memset(g_szVBoxErrMsg, 0, sizeof(g_szVBoxErrMsg));
|
---|
329 | }
|
---|
330 |
|
---|