1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
---|
2 | /* ***** BEGIN LICENSE BLOCK *****
|
---|
3 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
---|
4 | *
|
---|
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/
|
---|
9 | *
|
---|
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.
|
---|
14 | *
|
---|
15 | * The Original Code is mozilla.org code.
|
---|
16 | *
|
---|
17 | * The Initial Developer of the Original Code is
|
---|
18 | * Netscape Communications Corporation.
|
---|
19 | * Portions created by the Initial Developer are Copyright (C) 1998
|
---|
20 | * the Initial Developer. All Rights Reserved.
|
---|
21 | *
|
---|
22 | * Contributor(s):
|
---|
23 | *
|
---|
24 | * Alternatively, the contents of this file may be used under the terms of
|
---|
25 | * either of the GNU General Public License Version 2 or later (the "GPL"),
|
---|
26 | * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
---|
27 | * in which case the provisions of the GPL or the LGPL are applicable instead
|
---|
28 | * of those above. If you wish to allow use of your version of this file only
|
---|
29 | * under the terms of either the GPL or the LGPL, and not to allow others to
|
---|
30 | * use your version of this file under the terms of the MPL, indicate your
|
---|
31 | * decision by deleting the provisions above and replace them with the notice
|
---|
32 | * and other provisions required by the GPL or the LGPL. If you do not delete
|
---|
33 | * the provisions above, a recipient may use your version of this file under
|
---|
34 | * the terms of any one of the MPL, the GPL or the LGPL.
|
---|
35 | *
|
---|
36 | * ***** END LICENSE BLOCK ***** */
|
---|
37 |
|
---|
38 | /* nsDll
|
---|
39 | *
|
---|
40 | * Abstraction of a Dll. Stores modifiedTime and size for easy detection of
|
---|
41 | * change in dll.
|
---|
42 | *
|
---|
43 | * dp Suresh <[email protected]>
|
---|
44 | */
|
---|
45 |
|
---|
46 | #include "xcDll.h"
|
---|
47 | #include "nsDebug.h"
|
---|
48 | #include "nsIComponentManager.h"
|
---|
49 | #include "nsIComponentLoaderManager.h"
|
---|
50 | #include "nsIModule.h"
|
---|
51 | #include "nsILocalFile.h"
|
---|
52 | #include "nsDirectoryServiceDefs.h"
|
---|
53 | #include "nsDirectoryServiceUtils.h"
|
---|
54 | #include "nsCOMPtr.h"
|
---|
55 | #include "nsCRT.h"
|
---|
56 | #include "nsString.h"
|
---|
57 | #include "nsModule.h"
|
---|
58 | #ifdef DEBUG
|
---|
59 | #if defined(XP_MACOSX)
|
---|
60 | #include <signal.h>
|
---|
61 | #endif
|
---|
62 | #endif /* defined(DEBUG) */
|
---|
63 |
|
---|
64 | #include "nsTraceRefcntImpl.h"
|
---|
65 |
|
---|
66 | #include "nsNativeComponentLoader.h"
|
---|
67 | #include "nsMemory.h"
|
---|
68 |
|
---|
69 | nsDll::nsDll(nsIFile *dllSpec, nsNativeComponentLoader *loader)
|
---|
70 | : m_dllSpec(do_QueryInterface(dllSpec)),
|
---|
71 | m_hMod(NIL_RTLDRMOD),
|
---|
72 | m_moduleObject(NULL),
|
---|
73 | m_loader(loader),
|
---|
74 | m_markForUnload(PR_FALSE)
|
---|
75 | {
|
---|
76 | NS_ASSERTION(loader, "Null loader when creating a nsDLL");
|
---|
77 | }
|
---|
78 |
|
---|
79 | nsDll::~nsDll(void)
|
---|
80 | {
|
---|
81 | /** @todo r=aeichner Does this need fixing at all? */
|
---|
82 | //#if DEBUG_dougt
|
---|
83 | // The dll gets deleted when the dllStore is destroyed. This happens on
|
---|
84 | // app shutdown. At that point, unloading dlls can cause crashes if we have
|
---|
85 | // - dll dependencies
|
---|
86 | // - callbacks
|
---|
87 | // - static dtors
|
---|
88 | // Hence turn it back on after all the above have been removed.
|
---|
89 | #if defined(DEBUG) && defined(IPRT_WITH_GCC_SANITIZER)
|
---|
90 | /* Although this looks like it's what we should be doing here, we don't want to enable this for release builds (yet).
|
---|
91 | * Needs more testing and/or debugging first. See @bugref{10545c6}. */
|
---|
92 | Unload();
|
---|
93 | #endif
|
---|
94 | //#endif
|
---|
95 | }
|
---|
96 |
|
---|
97 | void
|
---|
98 | nsDll::GetDisplayPath(nsACString& aLeafName)
|
---|
99 | {
|
---|
100 | m_dllSpec->GetNativeLeafName(aLeafName);
|
---|
101 |
|
---|
102 | if (aLeafName.IsEmpty())
|
---|
103 | aLeafName.AssignLiteral("unknown!");
|
---|
104 | }
|
---|
105 |
|
---|
106 | PRBool
|
---|
107 | nsDll::HasChanged()
|
---|
108 | {
|
---|
109 | nsCOMPtr<nsIComponentLoaderManager> manager = do_QueryInterface(m_loader->mCompMgr);
|
---|
110 | if (!manager)
|
---|
111 | return PR_TRUE;
|
---|
112 |
|
---|
113 | // If mod date has changed, then dll has changed
|
---|
114 | PRInt64 currentDate;
|
---|
115 | nsresult rv = m_dllSpec->GetLastModifiedTime(¤tDate);
|
---|
116 | if (NS_FAILED(rv))
|
---|
117 | return PR_TRUE;
|
---|
118 | PRBool changed = PR_TRUE;
|
---|
119 | manager->HasFileChanged(m_dllSpec, nsnull, currentDate, &changed);
|
---|
120 | return changed;
|
---|
121 | }
|
---|
122 |
|
---|
123 | PRBool nsDll::Load(void)
|
---|
124 | {
|
---|
125 | /* Already loaded? Nothing to do. */
|
---|
126 | if (m_hMod != NIL_RTLDRMOD)
|
---|
127 | return PR_TRUE;
|
---|
128 |
|
---|
129 | if (m_dllSpec)
|
---|
130 | {
|
---|
131 | #ifdef NS_BUILD_REFCNT_LOGGING
|
---|
132 | nsTraceRefcntImpl::SetActivityIsLegal(PR_FALSE);
|
---|
133 | #endif
|
---|
134 |
|
---|
135 | // Load any library dependencies
|
---|
136 | // The Component Loader Manager may hold onto some extra data
|
---|
137 | // set by either the native component loader or the native
|
---|
138 | // component. We assume that this data is a space delimited
|
---|
139 | // listing of dependent libraries which are required to be
|
---|
140 | // loaded prior to us loading the given component. Once, the
|
---|
141 | // component is loaded into memory, we can release our hold
|
---|
142 | // on the dependent libraries with the assumption that the
|
---|
143 | // component library holds a reference via the OS so loader.
|
---|
144 | nsCOMPtr<nsIComponentLoaderManager> manager = do_QueryInterface(m_loader->mCompMgr);
|
---|
145 | if (!manager)
|
---|
146 | return PR_TRUE;
|
---|
147 |
|
---|
148 | nsXPIDLCString extraData;
|
---|
149 | manager->GetOptionalData(m_dllSpec, nsnull, getter_Copies(extraData));
|
---|
150 |
|
---|
151 | nsVoidArray dependentLibArray;
|
---|
152 |
|
---|
153 | // if there was any extra data, treat it as a listing of dependent libs
|
---|
154 | if (extraData != nsnull)
|
---|
155 | {
|
---|
156 | // all dependent libraries are suppose to be in the "gre" directory.
|
---|
157 | // note that the gre directory is the same as the "bin" directory,
|
---|
158 | // when there isn't a real "gre" found.
|
---|
159 |
|
---|
160 | nsXPIDLCString path;
|
---|
161 | nsCOMPtr<nsIFile> file;
|
---|
162 | NS_GetSpecialDirectory(NS_GRE_DIR, getter_AddRefs(file));
|
---|
163 |
|
---|
164 | if (!file)
|
---|
165 | return NS_ERROR_FAILURE;
|
---|
166 |
|
---|
167 | // we are talking about a file in the GRE dir. Lets append something
|
---|
168 | // stupid right now, so that later we can just set the leaf name.
|
---|
169 | file->AppendNative(NS_LITERAL_CSTRING("dummy"));
|
---|
170 |
|
---|
171 | char *buffer = (char *)nsMemory::Clone(extraData, strlen(extraData) + 1);
|
---|
172 | if (!buffer)
|
---|
173 | return NS_ERROR_OUT_OF_MEMORY;
|
---|
174 |
|
---|
175 | char* newStr;
|
---|
176 | char *token = nsCRT::strtok(buffer, " ", &newStr);
|
---|
177 | while (token!=nsnull)
|
---|
178 | {
|
---|
179 | nsCStringKey key(token);
|
---|
180 | if (m_loader->mLoadedDependentLibs.Get(&key)) {
|
---|
181 | token = nsCRT::strtok(newStr, " ", &newStr);
|
---|
182 | continue;
|
---|
183 | }
|
---|
184 |
|
---|
185 | m_loader->mLoadedDependentLibs.Put(&key, (void*)1);
|
---|
186 |
|
---|
187 | nsXPIDLCString libpath;
|
---|
188 | file->SetNativeLeafName(nsDependentCString(token));
|
---|
189 | file->GetNativePath(path);
|
---|
190 | if (!path)
|
---|
191 | return NS_ERROR_FAILURE;
|
---|
192 |
|
---|
193 | // Load this dependent library with the global flag and stash
|
---|
194 | // the result for later so that we can unload it.
|
---|
195 | const char *pszFilename = NULL;
|
---|
196 |
|
---|
197 | // if the depend library path starts with a / we are
|
---|
198 | // going to assume that it is a full path and should
|
---|
199 | // be loaded without prepending the gre diretory
|
---|
200 | // location. We could have short circuited the
|
---|
201 | // SetNativeLeafName above, but this is clearer and
|
---|
202 | // the common case is a relative path.
|
---|
203 | if (token[0] == '/')
|
---|
204 | pszFilename = token;
|
---|
205 | else
|
---|
206 | pszFilename = path;
|
---|
207 |
|
---|
208 | RTLDRMOD hMod = NIL_RTLDRMOD;
|
---|
209 | RTERRINFOSTATIC ErrInfo;
|
---|
210 | RTErrInfoInitStatic(&ErrInfo);
|
---|
211 |
|
---|
212 | int vrc = RTLdrLoadEx(pszFilename, &hMod, RTLDRLOAD_FLAGS_LOCAL, &ErrInfo.Core);
|
---|
213 | // if we couldn't load the dependent library. We did the best we
|
---|
214 | // can. Now just let us fail later if this really was a required
|
---|
215 | // dependency.
|
---|
216 | if (RT_SUCCESS(vrc))
|
---|
217 | dependentLibArray.AppendElement((void*)hMod);
|
---|
218 |
|
---|
219 | token = nsCRT::strtok(newStr, " ", &newStr);
|
---|
220 | }
|
---|
221 | nsMemory::Free(buffer);
|
---|
222 | }
|
---|
223 |
|
---|
224 | // load the component
|
---|
225 | nsCOMPtr<nsILocalFile> lf(do_QueryInterface(m_dllSpec));
|
---|
226 | NS_ASSERTION(lf, "nsIFile here must implement a nsILocalFile");
|
---|
227 | lf->Load(&m_hMod);
|
---|
228 |
|
---|
229 | // Unload any of library dependencies we loaded earlier. The assumption
|
---|
230 | // here is that the component will have a "internal" reference count to
|
---|
231 | // the dependency library we just loaded.
|
---|
232 | // XXX should we unload later - or even at all?
|
---|
233 | if (extraData != nsnull)
|
---|
234 | {
|
---|
235 | PRInt32 arrayCount = dependentLibArray.Count();
|
---|
236 | for (PRInt32 index = 0; index < arrayCount; index++)
|
---|
237 | RTLdrClose((RTLDRMOD)dependentLibArray.ElementAt(index));
|
---|
238 | }
|
---|
239 |
|
---|
240 | #ifdef NS_BUILD_REFCNT_LOGGING
|
---|
241 | nsTraceRefcntImpl::SetActivityIsLegal(PR_TRUE);
|
---|
242 | if (m_hMod != NIL_RTLDRMOD)
|
---|
243 | {
|
---|
244 | // Inform refcnt tracer of new library so that calls through the
|
---|
245 | // new library can be traced.
|
---|
246 | nsXPIDLCString displayPath;
|
---|
247 | GetDisplayPath(displayPath);
|
---|
248 | nsTraceRefcntImpl::LoadLibrarySymbols(displayPath.get(), m_hMod);
|
---|
249 | }
|
---|
250 | #endif
|
---|
251 | }
|
---|
252 |
|
---|
253 | return ((m_hMod == NIL_RTLDRMOD) ? PR_FALSE : PR_TRUE);
|
---|
254 | }
|
---|
255 |
|
---|
256 | PRBool nsDll::Unload(void)
|
---|
257 | {
|
---|
258 | if (m_hMod == NULL)
|
---|
259 | return PR_FALSE;
|
---|
260 |
|
---|
261 | // Shutdown the dll
|
---|
262 | Shutdown();
|
---|
263 |
|
---|
264 | #ifdef NS_BUILD_REFCNT_LOGGING
|
---|
265 | nsTraceRefcntImpl::SetActivityIsLegal(PR_FALSE);
|
---|
266 | #endif
|
---|
267 | int vrc = RTLdrClose(m_hMod);
|
---|
268 | #ifdef NS_BUILD_REFCNT_LOGGING
|
---|
269 | nsTraceRefcntImpl::SetActivityIsLegal(PR_TRUE);
|
---|
270 | #endif
|
---|
271 |
|
---|
272 | if (RT_SUCCESS(vrc))
|
---|
273 | {
|
---|
274 | m_hMod = NIL_RTLDRMOD;
|
---|
275 | return PR_TRUE;
|
---|
276 | }
|
---|
277 |
|
---|
278 | return PR_FALSE;
|
---|
279 | }
|
---|
280 |
|
---|
281 | void * nsDll::FindSymbol(const char *symbol)
|
---|
282 | {
|
---|
283 | if (symbol == NULL)
|
---|
284 | return NULL;
|
---|
285 |
|
---|
286 | // If not already loaded, load it now.
|
---|
287 | if (Load() != PR_TRUE)
|
---|
288 | return NULL;
|
---|
289 |
|
---|
290 | void *pvSym = NULL;
|
---|
291 | int vrc = RTLdrGetSymbol(m_hMod, symbol, &pvSym);
|
---|
292 | RT_NOREF(vrc);
|
---|
293 |
|
---|
294 | return pvSym;
|
---|
295 | }
|
---|
296 |
|
---|
297 |
|
---|
298 | // Component dll specific functions
|
---|
299 | nsresult nsDll::GetDllSpec(nsIFile **fsobj)
|
---|
300 | {
|
---|
301 | NS_ASSERTION(m_dllSpec, "m_dllSpec NULL");
|
---|
302 | NS_ASSERTION(fsobj, "xcDll::GetModule : Null argument" );
|
---|
303 |
|
---|
304 | *fsobj = m_dllSpec;
|
---|
305 | NS_ADDREF(*fsobj);
|
---|
306 | return NS_OK;
|
---|
307 | }
|
---|
308 |
|
---|
309 | nsresult nsDll::GetModule(nsISupports *servMgr, nsIModule **cobj)
|
---|
310 | {
|
---|
311 | // using the backpointer of the loader.
|
---|
312 | nsIComponentManager* compMgr = m_loader->mCompMgr;
|
---|
313 | NS_ASSERTION(compMgr, "Global Component Manager is null" );
|
---|
314 | if (!compMgr) return NS_ERROR_UNEXPECTED;
|
---|
315 |
|
---|
316 | NS_ASSERTION(cobj, "xcDll::GetModule : Null argument" );
|
---|
317 |
|
---|
318 | if (m_moduleObject)
|
---|
319 | {
|
---|
320 | NS_ADDREF(m_moduleObject);
|
---|
321 | *cobj = m_moduleObject;
|
---|
322 | return NS_OK;
|
---|
323 | }
|
---|
324 |
|
---|
325 | // If not already loaded, load it now.
|
---|
326 | if (Load() != PR_TRUE) return NS_ERROR_FAILURE;
|
---|
327 |
|
---|
328 | // We need a nsIFile for location
|
---|
329 | if (!m_dllSpec)
|
---|
330 | {
|
---|
331 | return NS_ERROR_FAILURE;
|
---|
332 | }
|
---|
333 |
|
---|
334 | nsGetModuleProc proc =
|
---|
335 | (nsGetModuleProc) FindSymbol(NS_GET_MODULE_SYMBOL);
|
---|
336 |
|
---|
337 | if (proc == NULL)
|
---|
338 | return NS_ERROR_FACTORY_NOT_LOADED;
|
---|
339 |
|
---|
340 | nsresult rv = (*proc) (compMgr, m_dllSpec, &m_moduleObject);
|
---|
341 | if (NS_SUCCEEDED(rv))
|
---|
342 | {
|
---|
343 | NS_ADDREF(m_moduleObject);
|
---|
344 | *cobj = m_moduleObject;
|
---|
345 | }
|
---|
346 | return rv;
|
---|
347 | }
|
---|
348 |
|
---|
349 | nsresult nsDll::Shutdown(void)
|
---|
350 | {
|
---|
351 | // Release the module object if we got one
|
---|
352 | nsrefcnt refcnt;
|
---|
353 | if (m_moduleObject)
|
---|
354 | {
|
---|
355 | NS_RELEASE2(m_moduleObject, refcnt);
|
---|
356 | NS_ASSERTION(refcnt == 0, "Dll moduleObject refcount non zero");
|
---|
357 | }
|
---|
358 |
|
---|
359 | return NS_OK;
|
---|
360 |
|
---|
361 | }
|
---|
362 |
|
---|