]> git.ipfire.org Git - thirdparty/squid.git/blob - src/LoadableModule.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / LoadableModule.cc
1 /*
2 * Copyright (C) 1996-2017 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9 #include "squid.h"
10 #include "base/TextException.h"
11 #include "libltdl/ltdl.h" /* generated file */
12 #include "LoadableModule.h"
13
14 // Note: We must use preprocessor instead of C ifs because if dlopen()
15 // is seen by the static linker, the linker will complain.
16
17 LoadableModule::LoadableModule(const String &aName): theName(aName), theHandle(0)
18 {
19 // Initialise preloaded symbol lookup table.
20 LTDL_SET_PRELOADED_SYMBOLS();
21 if (lt_dlinit() != 0)
22 throw TexcHere("internal error: cannot initialize libtool module loader");
23 }
24
25 LoadableModule::~LoadableModule()
26 {
27 if (loaded())
28 unload();
29 assert(lt_dlexit() == 0); // XXX: replace with a warning
30 }
31
32 bool
33 LoadableModule::loaded() const
34 {
35 return theHandle != 0;
36 }
37
38 void
39 LoadableModule::load(int mode)
40 {
41 if (loaded())
42 throw TexcHere("internal error: reusing LoadableModule object");
43
44 theHandle = openModule(mode);
45
46 if (!loaded())
47 throw TexcHere(errorMsg());
48 }
49
50 void
51 LoadableModule::unload()
52 {
53 if (!loaded())
54 throw TexcHere("internal error: unloading not loaded module");
55
56 if (!closeModule())
57 throw TexcHere(errorMsg());
58
59 theHandle = 0;
60 }
61
62 void *
63 LoadableModule::openModule(int mode)
64 {
65 return lt_dlopen(theName.termedBuf());
66 }
67
68 bool
69 LoadableModule::closeModule()
70 {
71 // we cast to avoid including ltdl.h in LoadableModule.h
72 return lt_dlclose(static_cast<lt_dlhandle>(theHandle)) == 0;
73 }
74
75 const char *
76 LoadableModule::errorMsg()
77 {
78 return lt_dlerror();
79 }
80