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