]>
Commit | Line | Data |
---|---|---|
bbc27441 | 1 | /* |
5b74111a | 2 | * Copyright (C) 1996-2018 The Squid Software Foundation and contributors |
bbc27441 AJ |
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 | ||
582c2af2 | 9 | #include "squid.h" |
3d93a84d | 10 | #include "base/TextException.h" |
57d6aaab | 11 | #include "libltdl/ltdl.h" /* generated file */ |
57afc994 AR |
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 | ||
26ac0430 AJ |
17 | LoadableModule::LoadableModule(const String &aName): theName(aName), theHandle(0) |
18 | { | |
26ac0430 AJ |
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"); | |
57afc994 AR |
23 | } |
24 | ||
26ac0430 AJ |
25 | LoadableModule::~LoadableModule() |
26 | { | |
27 | if (loaded()) | |
28 | unload(); | |
26ac0430 | 29 | assert(lt_dlexit() == 0); // XXX: replace with a warning |
57afc994 AR |
30 | } |
31 | ||
57d6aaab AJ |
32 | bool |
33 | LoadableModule::loaded() const | |
26ac0430 AJ |
34 | { |
35 | return theHandle != 0; | |
57afc994 AR |
36 | } |
37 | ||
57d6aaab AJ |
38 | void |
39 | LoadableModule::load(int mode) | |
26ac0430 AJ |
40 | { |
41 | if (loaded()) | |
42 | throw TexcHere("internal error: reusing LoadableModule object"); | |
57afc994 | 43 | |
26ac0430 | 44 | theHandle = openModule(mode); |
57afc994 | 45 | |
26ac0430 AJ |
46 | if (!loaded()) |
47 | throw TexcHere(errorMsg()); | |
57afc994 AR |
48 | } |
49 | ||
57d6aaab AJ |
50 | void |
51 | LoadableModule::unload() | |
26ac0430 AJ |
52 | { |
53 | if (!loaded()) | |
54 | throw TexcHere("internal error: unloading not loaded module"); | |
57afc994 | 55 | |
26ac0430 AJ |
56 | if (!closeModule()) |
57 | throw TexcHere(errorMsg()); | |
57afc994 | 58 | |
26ac0430 | 59 | theHandle = 0; |
57afc994 AR |
60 | } |
61 | ||
57d6aaab AJ |
62 | void * |
63 | LoadableModule::openModule(int mode) | |
26ac0430 | 64 | { |
b4f2886c | 65 | return lt_dlopen(theName.termedBuf()); |
57afc994 AR |
66 | } |
67 | ||
57d6aaab AJ |
68 | bool |
69 | LoadableModule::closeModule() | |
26ac0430 | 70 | { |
26ac0430 AJ |
71 | // we cast to avoid including ltdl.h in LoadableModule.h |
72 | return lt_dlclose(static_cast<lt_dlhandle>(theHandle)) == 0; | |
57afc994 AR |
73 | } |
74 | ||
57d6aaab AJ |
75 | const char * |
76 | LoadableModule::errorMsg() | |
26ac0430 | 77 | { |
26ac0430 | 78 | return lt_dlerror(); |
57afc994 | 79 | } |
f53969cc | 80 |