VirtualBox

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

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

VBoxXPCOMCGlue.c/.h: MIT license since it requires static linking.

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

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