]> git.ipfire.org Git - thirdparty/squid.git/blame - src/LoadableModule.cc
Convert loadable_modules to SBufList (#1738)
[thirdparty/squid.git] / src / LoadableModule.cc
CommitLineData
bbc27441 1/*
b8ae064d 2 * Copyright (C) 1996-2023 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
1c464a53
AJ
17LoadableModule::LoadableModule(const SBuf &aName):
18 theName(aName)
26ac0430 19{
26ac0430
AJ
20 // Initialise preloaded symbol lookup table.
21 LTDL_SET_PRELOADED_SYMBOLS();
22 if (lt_dlinit() != 0)
23 throw TexcHere("internal error: cannot initialize libtool module loader");
57afc994
AR
24}
25
26ac0430
AJ
26LoadableModule::~LoadableModule()
27{
28 if (loaded())
29 unload();
26ac0430 30 assert(lt_dlexit() == 0); // XXX: replace with a warning
57afc994
AR
31}
32
57d6aaab
AJ
33bool
34LoadableModule::loaded() const
26ac0430 35{
aee3523a 36 return theHandle != nullptr;
57afc994
AR
37}
38
57d6aaab 39void
8b082ed9 40LoadableModule::load()
26ac0430
AJ
41{
42 if (loaded())
43 throw TexcHere("internal error: reusing LoadableModule object");
57afc994 44
8b082ed9 45 theHandle = openModule();
57afc994 46
26ac0430
AJ
47 if (!loaded())
48 throw TexcHere(errorMsg());
57afc994
AR
49}
50
57d6aaab
AJ
51void
52LoadableModule::unload()
26ac0430
AJ
53{
54 if (!loaded())
55 throw TexcHere("internal error: unloading not loaded module");
57afc994 56
26ac0430
AJ
57 if (!closeModule())
58 throw TexcHere(errorMsg());
57afc994 59
aee3523a 60 theHandle = nullptr;
57afc994
AR
61}
62
57d6aaab 63void *
8b082ed9 64LoadableModule::openModule()
26ac0430 65{
1c464a53 66 return lt_dlopen(theName.c_str());
57afc994
AR
67}
68
57d6aaab
AJ
69bool
70LoadableModule::closeModule()
26ac0430 71{
26ac0430
AJ
72 // we cast to avoid including ltdl.h in LoadableModule.h
73 return lt_dlclose(static_cast<lt_dlhandle>(theHandle)) == 0;
57afc994
AR
74}
75
57d6aaab
AJ
76const char *
77LoadableModule::errorMsg()
26ac0430 78{
26ac0430 79 return lt_dlerror();
57afc994 80}
f53969cc 81