VirtualBox

source: vbox/trunk/src/VBox/Main/cbinding/VBoxXPCOMCGlue.c@ 19028

Last change on this file since 19028 was 19028, checked in by vboxsync, 16 years ago

Main/cbindins,libvirt/*glue: Bought back g_szVBoxErrMsg and change the code to use vsnprintf as well as fixing a couple of the string length constraints. When compiling with libvirt, we won't set g_szVBoxErrMsg. Renamed szBuf to szName to make it easier to read.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.1 KB
Line 
1/* $Revision: 19028 $ */
2/** @file
3 * Glue code for dynamically linking to VBoxXPCOMC.
4 */
5
6/*
7 * Copyright (C) 2008-2009 Sun Microsystems, Inc.
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 <stdio.h>
35#include <string.h>
36#include <stdlib.h>
37#include <stdarg.h>
38#include <dlfcn.h>
39
40#include "VBoxXPCOMCGlue.h"
41
42
43/*******************************************************************************
44* Defined Constants And Macros *
45*******************************************************************************/
46#if defined(__linux__) || defined(__linux_gnu__) || defined(__sun__) || defined(__FreeBSD__)
47# define DYNLIB_NAME "VBoxXPCOMC.so"
48#elif defined(__APPLE__)
49# define DYNLIB_NAME "VBoxXPCOMC.dylib"
50#elif defined(_MSC_VER) || defined(__OS2__)
51# define DYNLIB_NAME "VBoxXPCOMC.dll"
52#else
53# error "Port me"
54#endif
55
56
57/*******************************************************************************
58* Global Variables *
59*******************************************************************************/
60/** The dlopen handle for VBoxXPCOMC. */
61void *g_hVBoxXPCOMC = NULL;
62/** The last load error. */
63char g_szVBoxErrMsg[256];
64/** Pointer to the VBoxXPCOMC function table. */
65PCVBOXXPCOM g_pVBoxFuncs = NULL;
66/** Pointer to VBoxGetXPCOMCFunctions for the loaded VBoxXPCOMC so/dylib/dll. */
67PFNVBOXGETXPCOMCFUNCTIONS g_pfnGetFunctions = NULL;
68
69
70/**
71 * Wrapper for setting g_szVBoxErrMsg. Can be an empty stub.
72 *
73 * @param pszFormat The format string.
74 * @param ... The arguments.
75 */
76static void setErrMsg(const char *pszFormat, ...)
77{
78 va_list va;
79 va_start(va, pszFormat);
80 vsnprintf(g_szVBoxErrMsg, sizeof(g_szVBoxErrMsg), pszFormat, va);
81 va_end(va);
82}
83
84
85/**
86 * Try load VBoxXPCOMC.so/dylib/dll from the specified location and resolve all
87 * the symbols we need.
88 *
89 * @returns 0 on success, -1 on failure.
90 * @param pszHome The director where to try load VBoxXPCOMC from. Can
91 * be NULL.
92 * @param fSetAppHome Whether to set the VBOX_APP_HOME env.var. or not
93 * (boolean).
94 */
95static int tryLoadOne(const char *pszHome, int fSetAppHome)
96{
97 size_t cchHome = pszHome ? strlen(pszHome) : 0;
98 size_t cbBufNeeded;
99 char szName[4096];
100 int rc = -1;
101
102 /*
103 * Construct the full name.
104 */
105 cbBufNeeded = cchHome + sizeof("/" DYNLIB_NAME);
106 if (cbBufNeeded > sizeof(szName))
107 {
108 setErrMsg("path buffer too small: %u bytes needed",
109 (unsigned)cbBufNeeded);
110 return -1;
111 }
112 if (cchHome)
113 {
114 memcpy(szName, pszHome, cchHome);
115 szName[cchHome] = '/';
116 cchHome++;
117 }
118 memcpy(&szName[cchHome], DYNLIB_NAME, sizeof(DYNLIB_NAME));
119
120 /*
121 * Try load it by that name, setting the VBOX_APP_HOME first (for now).
122 * Then resolve and call the function table getter.
123 */
124 if (fSetAppHome)
125 {
126 if (pszHome)
127 setenv("VBOX_APP_HOME", pszHome, 1 /* always override */);
128 else
129 unsetenv("VBOX_APP_HOME");
130 }
131 g_hVBoxXPCOMC = dlopen(szName, RTLD_NOW | RTLD_LOCAL);
132 if (g_hVBoxXPCOMC)
133 {
134 PFNVBOXGETXPCOMCFUNCTIONS pfnGetFunctions;
135 pfnGetFunctions = (PFNVBOXGETXPCOMCFUNCTIONS)
136 dlsym(g_hVBoxXPCOMC, VBOX_GET_XPCOMC_FUNCTIONS_SYMBOL_NAME);
137 if (pfnGetFunctions)
138 {
139 g_pVBoxFuncs = pfnGetFunctions(VBOX_XPCOMC_VERSION);
140 if (g_pVBoxFuncs)
141 {
142 g_pfnGetFunctions = pfnGetFunctions;
143 return 0;
144 }
145
146 /* bail out */
147 setErrMsg("%.80s: pfnGetFunctions(%#x) failed",
148 szName, VBOX_XPCOMC_VERSION);
149 }
150 else
151 setErrMsg("dlsym(%.80s/%.32s): %.128s",
152 szName, VBOX_GET_XPCOMC_FUNCTIONS_SYMBOL_NAME, dlerror());
153 dlclose(g_hVBoxXPCOMC);
154 g_hVBoxXPCOMC = NULL;
155 }
156 else
157 setErrMsg("dlopen(%.80s): %.160s", szName, dlerror());
158 return rc;
159}
160
161
162/**
163 * Tries to locate and load VBoxXPCOMC.so/dylib/dll, resolving all the related
164 * function pointers.
165 *
166 * @returns 0 on success, -1 on failure.
167 *
168 * @remark This should be considered moved into a separate glue library since
169 * its its going to be pretty much the same for any user of VBoxXPCOMC
170 * and it will just cause trouble to have duplicate versions of this
171 * source code all around the place.
172 */
173int VBoxCGlueInit(void)
174{
175 /*
176 * If the user specifies the location, try only that.
177 */
178 const char *pszHome = getenv("VBOX_APP_HOME");
179 if (pszHome)
180 return tryLoadOne(pszHome, 0);
181
182 /*
183 * Try the known standard locations.
184 */
185#if defined(__gnu__linux__) || defined(__linux__)
186 if (tryLoadOne("/opt/VirtualBox", 1) == 0)
187 return 0;
188 if (tryLoadOne("/usr/lib/virtualbox", 1) == 0)
189 return 0;
190#elif defined(__sun__)
191 if (tryLoadOne("/opt/VirtualBox/amd64", 1) == 0)
192 return 0;
193 if (tryLoadOne("/opt/VirtualBox/i386", 1) == 0)
194 return 0;
195#elif defined(__APPLE__)
196 if (tryLoadOne("/Application/VirtualBox.app/Contents/MacOS", 1) == 0)
197 return 0;
198#elif defined(__FreeBSD__)
199 if (tryLoadOne("/usr/local/lib/virtualbox", 1) == 0)
200 return 0;
201#else
202# error "port me"
203#endif
204
205 /*
206 * Finally try the dynamic linker search path.
207 */
208 if (tryLoadOne(NULL, 1) == 0)
209 return 0;
210
211 /* No luck, return failure. */
212 return -1;
213}
214
215
216/**
217 * Terminate the C glue library.
218 */
219void VBoxCGlueTerm(void)
220{
221 if (g_hVBoxXPCOMC)
222 {
223#if 0 /* VBoxRT.so doesn't like being reloaded. See @bugref{3725}. */
224 dlclose(g_hVBoxXPCOMC);
225#endif
226 g_hVBoxXPCOMC = NULL;
227 }
228 g_pVBoxFuncs = NULL;
229 g_pfnGetFunctions = NULL;
230 memset(g_szVBoxErrMsg, 0, sizeof(g_szVBoxErrMsg));
231}
232
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