VirtualBox

source: vbox/trunk/src/VBox/Main/cbinding/VBoxCAPIGlue.c@ 50183

Last change on this file since 50183 was 50183, checked in by vboxsync, 11 years ago

Main/cbinding: bring the C binding to a new functionality level, making them handle both COM and XPCOM based platforms, plus some xsl cleanup to eliminate the $dispatch case which was unused for many years (and will not be used again)

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette