The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
dl_module.h
Go to the documentation of this file.
1 #pragma once
2 /*
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2 of the License, or
6  * (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
16  */
17 
18 /**
19  * $Id: edfd9405472ea657c557139f2c8791fd1dc62b9c $
20  *
21  * @file dl_module.h
22  * @brief Wrappers around dlopen to manage loading modules at runtime.
23  *
24  * @copyright 2016-2019 The FreeRADIUS server project
25  */
26 RCSIDH(dl_module_h, "$Id: edfd9405472ea657c557139f2c8791fd1dc62b9c $")
27 
28 #ifndef HAVE_DLFCN_H
29 # error FreeRADIUS needs a working dlopen()
30 #else
31 # include <dlfcn.h>
32 #endif
33 
34 #include <freeradius-devel/server/cf_parse.h>
35 #include <freeradius-devel/util/dl.h>
36 #include <freeradius-devel/util/rb.h>
37 #include <freeradius-devel/util/version.h>
38 
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42 
43 #ifdef _CONST
44 # error _CONST can only be defined in the local header
45 #endif
46 #ifndef _DL_MODULE_PRIVATE
47 # define _CONST const
48 #else
49 # define _CONST
50 #endif
51 
52 #ifdef __APPLE__
53 # define DL_EXTENSION ".dylib"
54 #elif defined (WIN32)
55 # define DL_EXTENSION ".dll"
56 #else
57 # define DL_EXTENSION ".so"
58 #endif
59 
60 /** Stop people using different module/library/server versions together
61  *
62  */
63 #define MODULE_MAGIC_INIT RADIUSD_MAGIC_NUMBER
64 
65 typedef enum {
66  DL_MODULE_TYPE_MODULE = 0, //!< Standard loadable module.
67  DL_MODULE_TYPE_PROTO, //!< Protocol module.
68  DL_MODULE_TYPE_PROCESS, //!< protocol processor.
69  DL_MODULE_TYPE_SUBMODULE //!< Driver (or method in the case of EAP)
71 
72 /** Callback priorities
73  *
74  * The higher the priority, the earlier in callback gets called.
75  */
76 #define DL_PRIORITY_DICT 30 //!< Callback priority for dictionary autoloading
77 #define DL_PRIORITY_DICT_ATTR 29 //!< Callback priority for attribute resolution
78 #define DL_PRIORITY_DICT_ENUM 28 //!< Callback priority for enum resolution
79 #define DL_PRIORITY_LIB 20 //!< Callback priority for library config
80 #define DL_PRIORITY_BOOTSTRAP 10 //!< Callback priority for bootstrap callback
81 
83 
84 /** Callback to call when a module is first loaded
85  *
86  */
87 typedef int (*dl_module_onload_t)(void);
88 
89 /** Callback when a module is destroyed
90  *
91  */
92 typedef void (*dl_module_unload_t)(void);
93 
94 /** Common fields for the interface struct modules export
95  *
96  * These are just enough for the loader to be able to load and unload the module.
97  */
98 #define DL_MODULE_COMMON \
99  struct { \
100  uint64_t magic; \
101  char const *name; \
102  dl_module_onload_t onload; \
103  dl_module_unload_t unload; \
104  }
105 
106 /** Fields common to all types of loadable modules
107  */
108 typedef struct {
111 
112 /** Module handle
113  *
114  * Contains module's dlhandle, and the functions it exports.
115  */
116 typedef struct dl_module_s dl_module_t;
117 struct dl_module_s {
118  char const * _CONST name; //!< Name of the module. The name passed to dl_module_alloc.
119 
120  dl_module_loader_t * _CONST loader; //!< Loader that owns this dl.
121 
122  dl_t * _CONST dl; //!< Dynamic loader handle.
123 
124  dl_module_t const * _CONST parent; //!< of this module.
125 
126  dl_module_type_t _CONST type; //!< of this module.
127 
128  dl_module_common_t *exported; //!< Symbol exported by the module, containing its public
129  //!< functions, name and behaviour control flags.
130 
131  CONF_SECTION * _CONST conf; //!< The module's global configuration
132  ///< (as opposed to the instance, configuration).
133  ///< May be NULL.
134 
135  unsigned int refs; //!< Number of references to this module.
136  ///< This is maintained as a separate counter
137  ///< (instead of using talloc refs) because it needs
138  ///< to be thread safe.
139  ///< The talloc code accesses the chunk after calling
140  ///< the destructor, so we can't lock the loader mutex
141  ///< inside the destructor and expect things to work
142  ///< correctly.
144 };
145 
147 extern size_t dl_module_type_prefix_len;
148 
149 int dl_module_free(dl_module_t *dl_module);
150 
152 
153 char const *dl_module_search_path(void);
154 
156 
157 dl_module_loader_t *dl_module_loader_init(char const *lib_dir);
158 
159 #undef _CONST
160 
161 #ifdef __cplusplus
162 }
163 #endif
#define RCSIDH(h, id)
Definition: build.h:482
A section grouping multiple CONF_PAIR.
Definition: cf_priv.h:101
A dynamic loader.
Definition: dl.c:81
Module handle.
Definition: dl.h:58
static dl_module_loader_t * dl_module_loader
Definition: dl_module.c:53
Wrapper struct around dl_loader_t.
Definition: dl_module.c:47
char const * dl_module_search_path(void)
Definition: dl_module.c:483
bool _CONST in_tree
Definition: dl_module.h:143
dl_t *_CONST dl
Dynamic loader handle.
Definition: dl_module.h:122
dl_module_common_t * exported
Symbol exported by the module, containing its public functions, name and behaviour control flags.
Definition: dl_module.h:128
dl_module_t const *_CONST parent
of this module.
Definition: dl_module.h:124
dl_module_type_t _CONST type
of this module.
Definition: dl_module.h:126
dl_module_type_t
Definition: dl_module.h:65
@ DL_MODULE_TYPE_PROTO
Protocol module.
Definition: dl_module.h:67
@ DL_MODULE_TYPE_SUBMODULE
Driver (or method in the case of EAP)
Definition: dl_module.h:69
@ DL_MODULE_TYPE_MODULE
Standard loadable module.
Definition: dl_module.h:66
@ DL_MODULE_TYPE_PROCESS
protocol processor.
Definition: dl_module.h:68
char const *_CONST name
Name of the module. The name passed to dl_module_alloc.
Definition: dl_module.h:118
fr_table_num_sorted_t const dl_module_type_prefix[]
Name prefixes matching the types of loadable module.
Definition: dl_module.c:57
int(* dl_module_onload_t)(void)
Callback to call when a module is first loaded.
Definition: dl_module.h:87
void(* dl_module_unload_t)(void)
Callback when a module is destroyed.
Definition: dl_module.h:92
#define _CONST
Definition: dl_module.h:47
dl_module_loader_t * dl_module_loader_init(char const *lib_dir)
Initialise structures needed by the dynamic linker.
Definition: dl_module.c:532
dl_module_loader_t *_CONST loader
Loader that owns this dl.
Definition: dl_module.h:120
CONF_SECTION *_CONST conf
The module's global configuration (as opposed to the instance, configuration).
Definition: dl_module.h:131
dl_module_t * dl_module_alloc(dl_module_t const *parent, char const *name, dl_module_type_t type)
Load a module library using dlopen() or return a previously loaded module from the cache.
Definition: dl_module.c:315
dl_loader_t * dl_loader_from_module_loader(dl_module_loader_t *dl_module_loader)
Definition: dl_module.c:488
unsigned int refs
Number of references to this module.
Definition: dl_module.h:135
size_t dl_module_type_prefix_len
Definition: dl_module.c:63
int dl_module_free(dl_module_t *dl_module)
Free a dl_module (when there are no more references to it)
Definition: dl_module.c:281
Fields common to all types of loadable modules.
Definition: dl_module.h:108
static char const * name
fr_aka_sim_id_type_t type
An element in a lexicographically sorted array of name to num mappings.
Definition: table.h:49
static fr_slen_t parent
Definition: pair.h:851