1 | /* $Revision: 18185 $ */
|
---|
2 | /** @file
|
---|
3 | * Glue code for dynamically linking to VBoxXPCOMC.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2008-2009 Sun Microsystems, Inc.
|
---|
8 | *
|
---|
9 | * This file is part of a free software library; you can redistribute
|
---|
10 | * it and/or modify it under the terms of the GNU Lesser General
|
---|
11 | * Public License version 2.1 as published by the Free Software
|
---|
12 | * Foundation Foundation and shipped in the the "COPYING" file with
|
---|
13 | * this library. The library is distributed in the hope that it will
|
---|
14 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
15 | *
|
---|
16 | * Sun LGPL Disclaimer: For the avoidance of doubt, except that if
|
---|
17 | * any license choice other than GPL or LGPL is available it will
|
---|
18 | * apply instead, Sun elects to use only the Lesser General Public
|
---|
19 | * License version 2.1 (LGPLv2) at this time for any software where
|
---|
20 | * a choice of LGPL license versions is made available with the
|
---|
21 | * language indicating that LGPLv2 or any later version may be used,
|
---|
22 | * or where a choice of which version of the LGPL is applied is
|
---|
23 | * otherwise unspecified.
|
---|
24 | *
|
---|
25 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
26 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
27 | * additional information or have any questions.
|
---|
28 | */
|
---|
29 |
|
---|
30 | /*******************************************************************************
|
---|
31 | * Header Files *
|
---|
32 | *******************************************************************************/
|
---|
33 | #ifndef VBOX_WITH_XPCOM /** @todo Find libvirt define to test for! */
|
---|
34 | # include <config.h>
|
---|
35 | # include "memory.h"
|
---|
36 | #endif /* !VBOX_WITH_XPCOM */
|
---|
37 |
|
---|
38 | #include <stdio.h>
|
---|
39 | #include <string.h>
|
---|
40 | #include <stdlib.h>
|
---|
41 | #include <dlfcn.h>
|
---|
42 |
|
---|
43 | #include "VBoxXPCOMCGlue.h"
|
---|
44 |
|
---|
45 |
|
---|
46 | /*******************************************************************************
|
---|
47 | * Defined Constants And Macros *
|
---|
48 | *******************************************************************************/
|
---|
49 | #if defined(__linux__) || defined(__linux_gnu__) || defined(__sun__)
|
---|
50 | # define DYNLIB_NAME "VBoxXPCOMC.so"
|
---|
51 | #elif defined(__APPLE__)
|
---|
52 | # define DYNLIB_NAME "VBoxXPCOMC.dylib"
|
---|
53 | #elif defined(_MSC_VER) || defined(__OS2__)
|
---|
54 | # define DYNLIB_NAME "VBoxXPCOMC.dll"
|
---|
55 | #else
|
---|
56 | # error "Port me"
|
---|
57 | #endif
|
---|
58 |
|
---|
59 | #ifdef VBOX_WITH_XPCOM /** @todo Find libvirt define to test for! */
|
---|
60 | # define VIR_ALLOC_N(a, b) ((a) = (char *)malloc(b))
|
---|
61 | # define VIR_FREE(name) (free(name))
|
---|
62 | #endif /* !VBOX_WITH_XPCOM */
|
---|
63 |
|
---|
64 |
|
---|
65 | /*******************************************************************************
|
---|
66 | * Global Variables *
|
---|
67 | *******************************************************************************/
|
---|
68 | /** The dlopen handle for VBoxXPCOMC. */
|
---|
69 | void *g_hVBoxXPCOMC = NULL;
|
---|
70 | /** The last load error. */
|
---|
71 | char g_szVBoxErrMsg[256];
|
---|
72 | /** Pointer to the VBoxXPCOMC function table. */
|
---|
73 | PCVBOXXPCOM g_pVBoxFuncs = NULL;
|
---|
74 | /** Pointer to VBoxGetXPCOMCFunctions for the loaded VBoxXPCOMC so/dylib/dll. */
|
---|
75 | PFNVBOXGETXPCOMCFUNCTIONS g_pfnGetFunctions = NULL;
|
---|
76 |
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Try load VBoxXPCOMC.so/dylib/dll from the specified location and resolve all
|
---|
80 | * the symbols we need.
|
---|
81 | *
|
---|
82 | * @returns 0 on success, -1 on failure.
|
---|
83 | * @param pszHome The director where to try load VBoxXPCOMC from. Can be NULL.
|
---|
84 | */
|
---|
85 | static int tryLoadOne(const char *pszHome)
|
---|
86 | {
|
---|
87 | size_t cchHome = pszHome ? strlen(pszHome) : 0;
|
---|
88 | size_t cbBuf;
|
---|
89 | char * pszBuf;
|
---|
90 | int rc = -1;
|
---|
91 |
|
---|
92 | /*
|
---|
93 | * Construct the full name.
|
---|
94 | */
|
---|
95 | cbBuf = cchHome + sizeof("/" DYNLIB_NAME);
|
---|
96 | if(VIR_ALLOC_N(pszBuf, cbBuf)) {;}
|
---|
97 | if (!pszBuf)
|
---|
98 | {
|
---|
99 | sprintf(g_szVBoxErrMsg, "malloc(%u) failed", (unsigned)cbBuf);
|
---|
100 | return -1;
|
---|
101 | }
|
---|
102 | if (pszHome)
|
---|
103 | {
|
---|
104 | memcpy(pszBuf, pszHome, cchHome);
|
---|
105 | pszBuf[cchHome] = '/';
|
---|
106 | cchHome++;
|
---|
107 | }
|
---|
108 | memcpy(&pszBuf[cchHome], DYNLIB_NAME, sizeof(DYNLIB_NAME));
|
---|
109 |
|
---|
110 | /*
|
---|
111 | * Try load it by that name, setting the VBOX_APP_HOME first (for now).
|
---|
112 | * Then resolve and call the function table getter.
|
---|
113 | */
|
---|
114 | setenv("VBOX_APP_HOME", pszHome, 0 /* no need to overwrite */);
|
---|
115 | g_hVBoxXPCOMC = dlopen(pszBuf, RTLD_NOW | RTLD_LOCAL);
|
---|
116 | if (g_hVBoxXPCOMC)
|
---|
117 | {
|
---|
118 | PFNVBOXGETXPCOMCFUNCTIONS pfnGetFunctions;
|
---|
119 | pfnGetFunctions = (PFNVBOXGETXPCOMCFUNCTIONS)
|
---|
120 | dlsym(g_hVBoxXPCOMC, VBOX_GET_XPCOMC_FUNCTIONS_SYMBOL_NAME);
|
---|
121 | if (pfnGetFunctions)
|
---|
122 | {
|
---|
123 | g_pVBoxFuncs = pfnGetFunctions(VBOX_XPCOMC_VERSION);
|
---|
124 | if (g_pVBoxFuncs)
|
---|
125 | {
|
---|
126 | g_pfnGetFunctions = pfnGetFunctions;
|
---|
127 | rc = 0;
|
---|
128 | }
|
---|
129 | else
|
---|
130 | sprintf(g_szVBoxErrMsg, "%.80s: pfnGetFunctions(%#x) failed",
|
---|
131 | pszBuf, VBOX_XPCOMC_VERSION);
|
---|
132 | }
|
---|
133 | else
|
---|
134 | sprintf(g_szVBoxErrMsg, "dlsym(%.80s/%.32s): %128s",
|
---|
135 | pszBuf, VBOX_GET_XPCOMC_FUNCTIONS_SYMBOL_NAME, dlerror());
|
---|
136 | if (rc != 0)
|
---|
137 | {
|
---|
138 | dlclose(g_hVBoxXPCOMC);
|
---|
139 | g_hVBoxXPCOMC = NULL;
|
---|
140 | }
|
---|
141 | }
|
---|
142 | else
|
---|
143 | sprintf(g_szVBoxErrMsg, "dlopen(%.80s): %128s", pszBuf, dlerror());
|
---|
144 | VIR_FREE(pszBuf);
|
---|
145 | return rc;
|
---|
146 | }
|
---|
147 |
|
---|
148 |
|
---|
149 | /**
|
---|
150 | * Tries to locate and load VBoxXPCOMC.so/dylib/dll, resolving all the related
|
---|
151 | * function pointers.
|
---|
152 | *
|
---|
153 | * @returns 0 on success, -1 on failure.
|
---|
154 | *
|
---|
155 | * @remark This should be considered moved into a separate glue library since
|
---|
156 | * its its going to be pretty much the same for any user of VBoxXPCOMC
|
---|
157 | * and it will just cause trouble to have duplicate versions of this
|
---|
158 | * source code all around the place.
|
---|
159 | */
|
---|
160 | int VBoxCGlueInit(void)
|
---|
161 | {
|
---|
162 | /*
|
---|
163 | * If the user specifies the location, try only that.
|
---|
164 | */
|
---|
165 | const char *pszHome = getenv("VBOX_APP_HOME");
|
---|
166 | if (pszHome)
|
---|
167 | return tryLoadOne(pszHome);
|
---|
168 |
|
---|
169 | /*
|
---|
170 | * Try the known standard locations.
|
---|
171 | */
|
---|
172 | #if defined(__gnu__linux__) || defined(__linux__)
|
---|
173 | if (tryLoadOne("/opt/VirtualBox") == 0)
|
---|
174 | return 0;
|
---|
175 | if (tryLoadOne("/usr/lib/virtualbox") == 0)
|
---|
176 | return 0;
|
---|
177 | #elif defined(__sun__)
|
---|
178 | if (tryLoadOne("/opt/VirtualBox/amd64") == 0)
|
---|
179 | return 0;
|
---|
180 | if (tryLoadOne("/opt/VirtualBox/i386") == 0)
|
---|
181 | return 0;
|
---|
182 | #elif defined(__APPLE__)
|
---|
183 | if (tryLoadOne("/Application/VirtualBox.app/Contents/MacOS") == 0)
|
---|
184 | return 0;
|
---|
185 | #else
|
---|
186 | # error "port me"
|
---|
187 | #endif
|
---|
188 |
|
---|
189 | /*
|
---|
190 | * Finally try the dynamic linker search path.
|
---|
191 | */
|
---|
192 | if (tryLoadOne(NULL) == 0)
|
---|
193 | return 0;
|
---|
194 |
|
---|
195 | /* No luck, return failure. */
|
---|
196 | return -1;
|
---|
197 | }
|
---|
198 |
|
---|
199 |
|
---|
200 | /**
|
---|
201 | * Terminate the C glue library.
|
---|
202 | */
|
---|
203 | void VBoxCGlueTerm(void)
|
---|
204 | {
|
---|
205 | if (g_hVBoxXPCOMC)
|
---|
206 | {
|
---|
207 | #if 0 /* VBoxRT.so doesn't like being reloaded. See @bugref{3725}. */
|
---|
208 | dlclose(g_hVBoxXPCOMC);
|
---|
209 | #endif
|
---|
210 | g_hVBoxXPCOMC = NULL;
|
---|
211 | }
|
---|
212 | g_pVBoxFuncs = NULL;
|
---|
213 | g_pfnGetFunctions = NULL;
|
---|
214 | memset(g_szVBoxErrMsg, 0, sizeof(g_szVBoxErrMsg));
|
---|
215 | }
|
---|
216 |
|
---|