LoadableModule.cc
Go to the documentation of this file.
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 
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 
27 {
28  if (loaded())
29  unload();
30  assert(lt_dlexit() == 0); // XXX: replace with a warning
31 }
32 
33 bool
35 {
36  return theHandle != nullptr;
37 }
38 
39 void
41 {
42  if (loaded())
43  throw TexcHere("internal error: reusing LoadableModule object");
44 
46 
47  if (!loaded())
48  throw TexcHere(errorMsg());
49 }
50 
51 void
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 
63 void *
65 {
66  return lt_dlopen(theName.c_str());
67 }
68 
69 bool
71 {
72  // we cast to avoid including ltdl.h in LoadableModule.h
73  return lt_dlclose(static_cast<lt_dlhandle>(theHandle)) == 0;
74 }
75 
76 const char *
78 {
79  return lt_dlerror();
80 }
81 
Definition: SBuf.h:93
#define TexcHere(msg)
legacy convenience macro; it is not difficult to type Here() now
Definition: TextException.h:63
bool loaded() const
#define assert(EX)
Definition: assert.h:17
const char * c_str()
Definition: SBuf.cc:516
LoadableModule(const SBuf &aName)
const char * errorMsg()
void * openModule()

 

Introduction

Documentation

Support

Miscellaneous