VirtualBox

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

最後變更 在這個檔案從17780是 17677,由 vboxsync 提交於 16 年 前

export pfnGetFunctions.

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 6.7 KB
 
1/* $Revision: 17677 $ */
2/** @file
3 * Glue code for dynamically linking to VBoxXPCOMC.
4 */
5
6/*
7 * Copyright (C) 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 "VBoxXPCOMCGlue.h"
35#include <stdio.h>
36#include <string.h>
37#include <stdlib.h>
38#include <dlfcn.h>
39
40
41/*******************************************************************************
42* Defined Constants And Macros *
43*******************************************************************************/
44#if defined(__linux__) || defined(__linux_gnu__) || defined(__sun__)
45# define DYNLIB_NAME "VBoxXPCOMC.so"
46#elif defined(__APPLE__)
47# define DYNLIB_NAME "VBoxXPCOMC.dylib"
48#elif defined(_MSC_VER) || defined(__OS2__)
49# define DYNLIB_NAME "VBoxXPCOMC.dll"
50#else
51# error "Port me"
52#endif
53
54
55/*******************************************************************************
56* Defined Constants And Macros *
57*******************************************************************************/
58/** The dlopen handle for VBoxXPCOMC. */
59void *g_hVBoxXPCOMC = NULL;
60/** The last load error. */
61char g_szVBoxErrMsg[256];
62/** Pointer to the VBoxXPCOMC function table. */
63PCVBOXXPCOM g_pVBoxFuncs = NULL;
64/** Pointer to VBoxGetXPCOMCFunctions for the loaded VBoxXPCOMC so/dylib/dll. */
65PFNVBOXGETXPCOMCFUNCTIONS g_pfnGetFunctions = NULL;
66
67
68/**
69 * Try load VBoxXPCOMC.so/dylib/dll from the specified location and resolve all
70 * the symbols we need.
71 *
72 * @returns 0 on success, -1 on failure.
73 * @param pszHome The director where to try load VBoxXPCOMC from. Can be NULL.
74 * @param pszMsgPrefix Error message prefix. NULL means no error messages.
75 */
76static int tryLoadOne(const char *pszHome, const char *pszMsgPrefix)
77{
78 size_t cchHome = pszHome ? strlen(pszHome) : 0;
79 size_t cbBuf;
80 char * pszBuf;
81 int rc = -1;
82
83 /*
84 * Construct the full name.
85 */
86 cbBuf = cchHome + sizeof("/" DYNLIB_NAME);
87 pszBuf = (char *)malloc(cbBuf);
88 if (!pszBuf)
89 {
90 sprintf(g_szVBoxErrMsg, "malloc(%u) failed", (unsigned)cbBuf);
91 if (pszMsgPrefix)
92 fprintf(stderr, "%s%s\n", pszMsgPrefix, g_szVBoxErrMsg);
93 return -1;
94 }
95 if (pszHome)
96 {
97 memcpy(pszBuf, pszHome, cchHome);
98 pszBuf[cchHome] = '/';
99 cchHome++;
100 }
101 memcpy(&pszBuf[cchHome], DYNLIB_NAME, sizeof(DYNLIB_NAME));
102
103 /*
104 * Try load it by that name, setting the VBOX_APP_HOME first (for now).
105 * Then resolve and call the function table getter.
106 */
107 setenv("VBOX_APP_HOME", pszHome, 0 /* no need to overwrite */);
108 g_hVBoxXPCOMC = dlopen(pszBuf, RTLD_NOW | RTLD_LOCAL);
109 if (g_hVBoxXPCOMC)
110 {
111 PFNVBOXGETXPCOMCFUNCTIONS pfnGetFunctions;
112 pfnGetFunctions = (PFNVBOXGETXPCOMCFUNCTIONS)
113 dlsym(g_hVBoxXPCOMC, VBOX_GET_XPCOMC_FUNCTIONS_SYMBOL_NAME);
114 if (pfnGetFunctions)
115 {
116 g_pVBoxFuncs = pfnGetFunctions(VBOX_XPCOMC_VERSION);
117 if (g_pVBoxFuncs)
118 {
119 g_pfnGetFunctions = pfnGetFunctions;
120 rc = 0;
121 }
122 else
123 sprintf(g_szVBoxErrMsg, "%.80s: pfnGetFunctions(%#x) failed",
124 pszBuf, VBOX_XPCOMC_VERSION);
125 }
126 else
127 sprintf(g_szVBoxErrMsg, "dlsym(%.80s/%.32s): %128s",
128 pszBuf, VBOX_GET_XPCOMC_FUNCTIONS_SYMBOL_NAME, dlerror());
129 if (rc != 0)
130 dlclose(g_hVBoxXPCOMC);
131 g_hVBoxXPCOMC = NULL;
132 }
133 else
134 sprintf(g_szVBoxErrMsg, "dlopen(%.80s): %128s", pszBuf, dlerror());
135 free(pszBuf);
136 return rc;
137}
138
139
140/**
141 * Tries to locate and load VBoxXPCOMC.so/dylib/dll, resolving all the related
142 * function pointers.
143 *
144 * @returns 0 on success, -1 on failure.
145 * @param pszMsgPrefix Error message prefix. NULL means no error messages.
146 *
147 * @remark This should be considered moved into a separate glue library since
148 * its its going to be pretty much the same for any user of VBoxXPCOMC
149 * and it will just cause trouble to have duplicate versions of this
150 * source code all around the place.
151 */
152int VBoxCGlueInit(const char *pszMsgPrefix)
153{
154 /*
155 * If the user specifies the location, try only that.
156 */
157 const char *pszHome = getenv("VBOX_APP_HOME");
158 if (pszHome)
159 return tryLoadOne(pszHome, pszMsgPrefix);
160
161 /*
162 * Try the known standard locations.
163 */
164#if defined(__gnu__linux__) || defined(__linux__)
165 if (tryLoadOne("/opt/VirtualBox", pszMsgPrefix) == 0)
166 return 0;
167 if (tryLoadOne("/usr/lib/virtualbox", pszMsgPrefix) == 0)
168 return 0;
169#elif defined(__sun__)
170 if (tryLoadOne("/opt/VirtualBox/amd64", pszMsgPrefix) == 0)
171 return 0;
172 if (tryLoadOne("/opt/VirtualBox/i386", pszMsgPrefix) == 0)
173 return 0;
174#elif defined(__APPLE__)
175 if (tryLoadOne("/Application/VirtualBox.app/Contents/MacOS", pszMsgPrefix) == 0)
176 return 0;
177#else
178# error "port me"
179#endif
180
181 /*
182 * Finally try the dynamic linker search path.
183 */
184 if (tryLoadOne(NULL, pszMsgPrefix) == 0)
185 return 0;
186
187 /* No luck, return failure. */
188 if (pszMsgPrefix)
189 fprintf(stderr, "%sFailed to locate VBoxXPCOMC\n", pszMsgPrefix);
190 return -1;
191}
192
193
194/**
195 * Terminate the C glue library.
196 */
197void VBoxCGlueTerm(void)
198{
199 if (g_hVBoxXPCOMC)
200 {
201 dlclose(g_hVBoxXPCOMC);
202 g_hVBoxXPCOMC = NULL;
203 }
204 g_pVBoxFuncs = NULL;
205 g_pfnGetFunctions = NULL;
206}
207
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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