1 | /* $Id: VBoxCAPIGlue.c 96407 2022-08-22 17:43:14Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Glue code for dynamically linking to VBoxCAPI.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2008-2022 Oracle and/or its affiliates.
|
---|
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 | /*********************************************************************************************************************************
|
---|
33 | * Header Files *
|
---|
34 | *********************************************************************************************************************************/
|
---|
35 | /* NOTE: do NOT use any include files here which are only available in the
|
---|
36 | * VirtualBox tree, e.g. iprt. They are not available in the SDK, which is
|
---|
37 | * where this file will provided as source code and has to be compilable. */
|
---|
38 | #include "VBoxCAPIGlue.h"
|
---|
39 |
|
---|
40 | #include <stdio.h>
|
---|
41 | #include <string.h>
|
---|
42 | #include <stdlib.h>
|
---|
43 | #include <stdarg.h>
|
---|
44 | #ifdef WIN32
|
---|
45 | # define _INTPTR 2 /* on Windows stdint.h compares this in #if, causing warnings if not defined */
|
---|
46 | #endif /* WIN32 */
|
---|
47 | #include <stdint.h>
|
---|
48 | #ifndef WIN32
|
---|
49 | # include <dlfcn.h>
|
---|
50 | # include <pthread.h>
|
---|
51 | #else /* WIN32 */
|
---|
52 | # include <Windows.h>
|
---|
53 | #endif /* WIN32 */
|
---|
54 |
|
---|
55 |
|
---|
56 | /*********************************************************************************************************************************
|
---|
57 | * Defined Constants And Macros *
|
---|
58 | *********************************************************************************************************************************/
|
---|
59 | #if defined(__linux__) || defined(__linux_gnu__) || defined(__sun__) || defined(__FreeBSD__)
|
---|
60 | # define DYNLIB_NAME "VBoxXPCOMC.so"
|
---|
61 | #elif defined(__APPLE__)
|
---|
62 | # define DYNLIB_NAME "VBoxXPCOMC.dylib"
|
---|
63 | #elif defined(__OS2__)
|
---|
64 | # define DYNLIB_NAME "VBoxXPCOMC.dll"
|
---|
65 | #elif defined(WIN32)
|
---|
66 | # define DYNLIB_NAME "VBoxCAPI.dll"
|
---|
67 | #else
|
---|
68 | # error "Port me"
|
---|
69 | #endif
|
---|
70 |
|
---|
71 |
|
---|
72 | /*********************************************************************************************************************************
|
---|
73 | * Global Variables *
|
---|
74 | *********************************************************************************************************************************/
|
---|
75 | /** The so/dynsym/dll handle for VBoxCAPI. */
|
---|
76 | #ifndef WIN32
|
---|
77 | void *g_hVBoxCAPI = NULL;
|
---|
78 | #else /* WIN32 */
|
---|
79 | HMODULE g_hVBoxCAPI = NULL;
|
---|
80 | #endif /* WIN32 */
|
---|
81 | /** The last load error. */
|
---|
82 | char g_szVBoxErrMsg[256] = "";
|
---|
83 | /** Pointer to the VBOXCAPI function table. */
|
---|
84 | PCVBOXCAPI g_pVBoxFuncs = NULL;
|
---|
85 | /** Pointer to VBoxGetCAPIFunctions for the loaded VBoxCAPI so/dylib/dll. */
|
---|
86 | PFNVBOXGETCAPIFUNCTIONS g_pfnGetFunctions = NULL;
|
---|
87 |
|
---|
88 | typedef void FNDUMMY(void);
|
---|
89 | typedef FNDUMMY *PFNDUMMY;
|
---|
90 | /** Just a dummy global structure containing a bunch of
|
---|
91 | * function pointers to code which is wanted in the link. */
|
---|
92 | PFNDUMMY g_apfnVBoxCAPIGlue[] =
|
---|
93 | {
|
---|
94 | #ifndef WIN32
|
---|
95 | /* The following link dependency is for helping gdb as it gets hideously
|
---|
96 | * confused if the application doesn't drag in pthreads, but uses it. */
|
---|
97 | (PFNDUMMY)pthread_create,
|
---|
98 | #endif /* !WIN32 */
|
---|
99 | NULL
|
---|
100 | };
|
---|
101 |
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * Wrapper for setting g_szVBoxErrMsg. Can be an empty stub.
|
---|
105 | *
|
---|
106 | * @param fAlways When 0 the g_szVBoxErrMsg is only set if empty.
|
---|
107 | * @param pszFormat The format string.
|
---|
108 | * @param ... The arguments.
|
---|
109 | */
|
---|
110 | static void setErrMsg(int fAlways, const char *pszFormat, ...)
|
---|
111 | {
|
---|
112 | if ( fAlways
|
---|
113 | || !g_szVBoxErrMsg[0])
|
---|
114 | {
|
---|
115 | va_list va;
|
---|
116 | va_start(va, pszFormat);
|
---|
117 | vsnprintf(g_szVBoxErrMsg, sizeof(g_szVBoxErrMsg), pszFormat, va);
|
---|
118 | va_end(va);
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 |
|
---|
123 | /**
|
---|
124 | * Try load C API .so/dylib/dll from the specified location and resolve all
|
---|
125 | * the symbols we need. Tries both the new style and legacy name.
|
---|
126 | *
|
---|
127 | * @returns 0 on success, -1 on failure.
|
---|
128 | * @param pszHome The directory where to try load VBoxCAPI/VBoxXPCOMC
|
---|
129 | * from. Can be NULL.
|
---|
130 | * @param fSetAppHome Whether to set the VBOX_APP_HOME env.var. or not
|
---|
131 | * (boolean).
|
---|
132 | */
|
---|
133 | static int tryLoadLibrary(const char *pszHome, int fSetAppHome)
|
---|
134 | {
|
---|
135 | size_t cchHome = pszHome ? strlen(pszHome) : 0;
|
---|
136 | size_t cbBufNeeded;
|
---|
137 | char szName[4096];
|
---|
138 |
|
---|
139 | /*
|
---|
140 | * Construct the full name.
|
---|
141 | */
|
---|
142 | cbBufNeeded = cchHome + sizeof("/" DYNLIB_NAME);
|
---|
143 | if (cbBufNeeded > sizeof(szName))
|
---|
144 | {
|
---|
145 | setErrMsg(1, "path buffer too small: %u bytes needed",
|
---|
146 | (unsigned)cbBufNeeded);
|
---|
147 | return -1;
|
---|
148 | }
|
---|
149 | if (cchHome)
|
---|
150 | {
|
---|
151 | memcpy(szName, pszHome, cchHome);
|
---|
152 | szName[cchHome] = '/';
|
---|
153 | cchHome++;
|
---|
154 | }
|
---|
155 | memcpy(&szName[cchHome], DYNLIB_NAME, sizeof(DYNLIB_NAME));
|
---|
156 |
|
---|
157 | /*
|
---|
158 | * Try load it by that name, setting the VBOX_APP_HOME first (for now).
|
---|
159 | * Then resolve and call the function table getter.
|
---|
160 | */
|
---|
161 | if (fSetAppHome)
|
---|
162 | {
|
---|
163 | #ifndef WIN32
|
---|
164 | if (pszHome)
|
---|
165 | setenv("VBOX_APP_HOME", pszHome, 1 /* always override */);
|
---|
166 | else
|
---|
167 | unsetenv("VBOX_APP_HOME");
|
---|
168 | #endif /* !WIN32 */
|
---|
169 | }
|
---|
170 |
|
---|
171 | #ifndef WIN32
|
---|
172 | g_hVBoxCAPI = dlopen(szName, RTLD_NOW | RTLD_LOCAL);
|
---|
173 | #else /* WIN32 */
|
---|
174 | g_hVBoxCAPI = LoadLibraryExA(szName, NULL /* hFile */, 0 /* dwFlags */);
|
---|
175 | #endif /* WIN32 */
|
---|
176 | if (g_hVBoxCAPI)
|
---|
177 | {
|
---|
178 | PFNVBOXGETCAPIFUNCTIONS pfnGetFunctions;
|
---|
179 | #ifndef WIN32
|
---|
180 | pfnGetFunctions = (PFNVBOXGETCAPIFUNCTIONS)(uintptr_t)
|
---|
181 | dlsym(g_hVBoxCAPI, VBOX_GET_CAPI_FUNCTIONS_SYMBOL_NAME);
|
---|
182 | # ifdef VBOX_GET_XPCOM_FUNCTIONS_SYMBOL_NAME
|
---|
183 | if (!pfnGetFunctions)
|
---|
184 | pfnGetFunctions = (PFNVBOXGETCAPIFUNCTIONS)(uintptr_t)
|
---|
185 | dlsym(g_hVBoxCAPI, VBOX_GET_XPCOM_FUNCTIONS_SYMBOL_NAME);
|
---|
186 | # endif /* VBOX_GET_XPCOM_FUNCTIONS_SYMBOL_NAME */
|
---|
187 | #else /* WIN32 */
|
---|
188 | pfnGetFunctions = (PFNVBOXGETCAPIFUNCTIONS)
|
---|
189 | GetProcAddress(g_hVBoxCAPI, VBOX_GET_CAPI_FUNCTIONS_SYMBOL_NAME);
|
---|
190 | #endif /* WIN32 */
|
---|
191 | if (pfnGetFunctions)
|
---|
192 | {
|
---|
193 | g_pVBoxFuncs = pfnGetFunctions(VBOX_CAPI_VERSION);
|
---|
194 | if (g_pVBoxFuncs)
|
---|
195 | {
|
---|
196 | if ( ( VBOX_CAPI_MAJOR(g_pVBoxFuncs->uVersion)
|
---|
197 | == VBOX_CAPI_MAJOR(VBOX_CAPI_VERSION))
|
---|
198 | && ( VBOX_CAPI_MINOR(g_pVBoxFuncs->uVersion)
|
---|
199 | >= VBOX_CAPI_MINOR(VBOX_CAPI_VERSION)))
|
---|
200 | {
|
---|
201 | g_pfnGetFunctions = pfnGetFunctions;
|
---|
202 | return 0;
|
---|
203 | }
|
---|
204 | setErrMsg(1, "%.80s: pfnGetFunctions(%#x) returned incompatible version %#x",
|
---|
205 | szName, VBOX_CAPI_VERSION, g_pVBoxFuncs->uVersion);
|
---|
206 | g_pVBoxFuncs = NULL;
|
---|
207 | }
|
---|
208 | else
|
---|
209 | {
|
---|
210 | /* bail out */
|
---|
211 | setErrMsg(1, "%.80s: pfnGetFunctions(%#x) failed",
|
---|
212 | szName, VBOX_CAPI_VERSION);
|
---|
213 | }
|
---|
214 | }
|
---|
215 | else
|
---|
216 | {
|
---|
217 | #ifndef WIN32
|
---|
218 | setErrMsg(1, "dlsym(%.80s/%.32s): %.128s",
|
---|
219 | szName, VBOX_GET_CAPI_FUNCTIONS_SYMBOL_NAME, dlerror());
|
---|
220 | #else /* WIN32 */
|
---|
221 | setErrMsg(1, "GetProcAddress(%.80s/%.32s): %d",
|
---|
222 | szName, VBOX_GET_CAPI_FUNCTIONS_SYMBOL_NAME, GetLastError());
|
---|
223 | #endif /* WIN32 */
|
---|
224 | }
|
---|
225 |
|
---|
226 | #ifndef WIN32
|
---|
227 | dlclose(g_hVBoxCAPI);
|
---|
228 | #else /* WIN32 */
|
---|
229 | FreeLibrary(g_hVBoxCAPI);
|
---|
230 | #endif /* WIN32 */
|
---|
231 | g_hVBoxCAPI = NULL;
|
---|
232 | }
|
---|
233 | else
|
---|
234 | {
|
---|
235 | #ifndef WIN32
|
---|
236 | setErrMsg(0, "dlopen(%.80s): %.160s", szName, dlerror());
|
---|
237 | #else /* WIN32 */
|
---|
238 | setErrMsg(0, "LoadLibraryEx(%.80s): %d", szName, GetLastError());
|
---|
239 | #endif /* WIN32 */
|
---|
240 | }
|
---|
241 |
|
---|
242 | return -1;
|
---|
243 | }
|
---|
244 |
|
---|
245 |
|
---|
246 | /**
|
---|
247 | * Tries to locate and load VBoxCAPI.so/dylib/dll, resolving all the related
|
---|
248 | * function pointers.
|
---|
249 | *
|
---|
250 | * @returns 0 on success, -1 on failure.
|
---|
251 | *
|
---|
252 | * @remark This should be considered moved into a separate glue library since
|
---|
253 | * its its going to be pretty much the same for any user of VBoxCAPI
|
---|
254 | * and it will just cause trouble to have duplicate versions of this
|
---|
255 | * source code all around the place.
|
---|
256 | */
|
---|
257 | int VBoxCGlueInit(void)
|
---|
258 | {
|
---|
259 | const char *pszHome;
|
---|
260 |
|
---|
261 | memset(g_szVBoxErrMsg, 0, sizeof(g_szVBoxErrMsg));
|
---|
262 |
|
---|
263 | /*
|
---|
264 | * If the user specifies the location, try only that.
|
---|
265 | */
|
---|
266 | pszHome = getenv("VBOX_APP_HOME");
|
---|
267 | if (pszHome)
|
---|
268 | return tryLoadLibrary(pszHome, 0);
|
---|
269 |
|
---|
270 | /*
|
---|
271 | * Try the known standard locations.
|
---|
272 | */
|
---|
273 | #if defined(__gnu__linux__) || defined(__linux__)
|
---|
274 | if (tryLoadLibrary("/opt/VirtualBox", 1) == 0)
|
---|
275 | return 0;
|
---|
276 | if (tryLoadLibrary("/usr/lib/virtualbox", 1) == 0)
|
---|
277 | return 0;
|
---|
278 | #elif defined(__sun__)
|
---|
279 | if (tryLoadLibrary("/opt/VirtualBox/amd64", 1) == 0)
|
---|
280 | return 0;
|
---|
281 | if (tryLoadLibrary("/opt/VirtualBox/i386", 1) == 0)
|
---|
282 | return 0;
|
---|
283 | #elif defined(__APPLE__)
|
---|
284 | if (tryLoadLibrary("/Applications/VirtualBox.app/Contents/MacOS", 1) == 0)
|
---|
285 | return 0;
|
---|
286 | #elif defined(__FreeBSD__)
|
---|
287 | if (tryLoadLibrary("/usr/local/lib/virtualbox", 1) == 0)
|
---|
288 | return 0;
|
---|
289 | #elif defined(__OS2__)
|
---|
290 | if (tryLoadLibrary("C:/Apps/VirtualBox", 1) == 0)
|
---|
291 | return 0;
|
---|
292 | #elif defined(WIN32)
|
---|
293 | pszHome = getenv("ProgramFiles");
|
---|
294 | if (pszHome)
|
---|
295 | {
|
---|
296 | char szPath[4096];
|
---|
297 | size_t cb = sizeof(szPath);
|
---|
298 | char *tmp = szPath;
|
---|
299 | strncpy(tmp, pszHome, cb);
|
---|
300 | tmp[cb - 1] = '\0';
|
---|
301 | cb -= strlen(tmp);
|
---|
302 | tmp += strlen(tmp);
|
---|
303 | strncpy(tmp, "/Oracle/VirtualBox", cb);
|
---|
304 | tmp[cb - 1] = '\0';
|
---|
305 | if (tryLoadLibrary(szPath, 1) == 0)
|
---|
306 | return 0;
|
---|
307 | }
|
---|
308 | if (tryLoadLibrary("C:/Program Files/Oracle/VirtualBox", 1) == 0)
|
---|
309 | return 0;
|
---|
310 | #else
|
---|
311 | # error "port me"
|
---|
312 | #endif
|
---|
313 |
|
---|
314 | /*
|
---|
315 | * Finally try the dynamic linker search path.
|
---|
316 | */
|
---|
317 | if (tryLoadLibrary(NULL, 1) == 0)
|
---|
318 | return 0;
|
---|
319 |
|
---|
320 | /* No luck, return failure. */
|
---|
321 | return -1;
|
---|
322 | }
|
---|
323 |
|
---|
324 |
|
---|
325 | /**
|
---|
326 | * Terminate the C glue library.
|
---|
327 | */
|
---|
328 | void VBoxCGlueTerm(void)
|
---|
329 | {
|
---|
330 | if (g_hVBoxCAPI)
|
---|
331 | {
|
---|
332 | #if 0 /* VBoxRT.so doesn't like being reloaded. See @bugref{3725}. */
|
---|
333 | #ifndef WIN32
|
---|
334 | dlclose(g_hVBoxCAPI);
|
---|
335 | #else
|
---|
336 | FreeLibrary(g_hVBoxCAPI);
|
---|
337 | #endif
|
---|
338 | #endif
|
---|
339 | g_hVBoxCAPI = NULL;
|
---|
340 | }
|
---|
341 | g_pVBoxFuncs = NULL;
|
---|
342 | g_pfnGetFunctions = NULL;
|
---|
343 | memset(g_szVBoxErrMsg, 0, sizeof(g_szVBoxErrMsg));
|
---|
344 | }
|
---|
345 |
|
---|