Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / xpcom / glue / standalone / nsGlueLinkingDlopen.cpp
blob8f40c97e5a36962e998af662e90eda51c70482ac
1 /* -*- Mode: C++; tab-width: 6; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is Mozilla XPCOM.
17 * The Initial Developer of the Original Code is
18 * Benjamin Smedberg <benjamin@smedbergs.us>
20 * Portions created by the Initial Developer are Copyright (C) 2005
21 * the Initial Developer. All Rights Reserved.
23 * Contributor(s):
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #include "nsGlueLinking.h"
40 #include "nsXPCOMGlue.h"
42 #include <dlfcn.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <stdio.h>
47 #if defined(SUNOS4) || defined(NEXTSTEP) || \
48 (defined(OPENBSD) || defined(NETBSD)) && !defined(__ELF__)
49 #define LEADING_UNDERSCORE "_"
50 #else
51 #define LEADING_UNDERSCORE
52 #endif
54 struct DependentLib
56 void *libHandle;
57 DependentLib *next;
60 static DependentLib *sTop;
61 static void* sXULLibHandle;
63 static void
64 AppendDependentLib(void *libHandle)
66 DependentLib *d = new DependentLib;
67 if (!d)
68 return;
70 d->next = sTop;
71 d->libHandle = libHandle;
73 sTop = d;
76 static void
77 ReadDependentCB(const char *aDependentLib)
79 void *libHandle = dlopen(aDependentLib, RTLD_GLOBAL | RTLD_LAZY);
80 if (!libHandle)
81 return;
83 AppendDependentLib(libHandle);
86 GetFrozenFunctionsFunc
87 XPCOMGlueLoad(const char *xpcomFile)
89 char xpcomDir[MAXPATHLEN];
90 if (realpath(xpcomFile, xpcomDir)) {
91 char *lastSlash = strrchr(xpcomDir, '/');
92 if (lastSlash) {
93 *lastSlash = '\0';
95 XPCOMGlueLoadDependentLibs(xpcomDir, ReadDependentCB);
97 snprintf(lastSlash, MAXPATHLEN - strlen(xpcomDir), "/" XUL_DLL);
99 sXULLibHandle = dlopen(xpcomDir, RTLD_GLOBAL | RTLD_LAZY);
103 // RTLD_DEFAULT is not defined in non-GNU toolchains, and it is
104 // (void*) 0 in any case.
106 void *libHandle = nsnull;
108 if (xpcomFile[0] != '.' || xpcomFile[1] != '\0') {
109 libHandle = dlopen(xpcomFile, RTLD_GLOBAL | RTLD_LAZY);
110 if (libHandle) {
111 AppendDependentLib(libHandle);
115 GetFrozenFunctionsFunc sym =
116 (GetFrozenFunctionsFunc) dlsym(libHandle,
117 LEADING_UNDERSCORE "NS_GetFrozenFunctions");
119 if (!sym)
120 XPCOMGlueUnload();
122 return sym;
125 void
126 XPCOMGlueUnload()
128 while (sTop) {
129 dlclose(sTop->libHandle);
131 DependentLib *temp = sTop;
132 sTop = sTop->next;
134 delete temp;
137 if (sXULLibHandle) {
138 dlclose(sXULLibHandle);
139 sXULLibHandle = nsnull;
143 nsresult
144 XPCOMGlueLoadXULFunctions(const nsDynamicFunctionLoad *symbols)
146 // We don't null-check sXULLibHandle because this might work even
147 // if it is null (same as RTLD_DEFAULT)
149 nsresult rv = NS_OK;
150 while (symbols->functionName) {
151 char buffer[512];
152 snprintf(buffer, sizeof(buffer),
153 LEADING_UNDERSCORE "%s", symbols->functionName);
155 *symbols->function = (NSFuncPtr) dlsym(sXULLibHandle, buffer);
156 if (!*symbols->function)
157 rv = NS_ERROR_LOSS_OF_SIGNIFICANT_DATA;
159 ++symbols;
161 return rv;