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: 619b6a5e9bb7e92b75c07caefef4428b865be05f $
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: 619b6a5e9bb7e92b75c07caefef4428b865be05f $")
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 
61 
62 /** Stop people using different module/library/server versions together
63  *
64  */
65 #define MODULE_MAGIC_INIT RADIUSD_MAGIC_NUMBER
66 
67 typedef enum {
68  DL_MODULE_TYPE_MODULE = 0, //!< Standard loadable module.
69  DL_MODULE_TYPE_PROTO, //!< Protocol module.
70  DL_MODULE_TYPE_PROCESS, //!< protocol processor.
71  DL_MODULE_TYPE_SUBMODULE //!< Driver (or method in the case of EAP)
73 
74 /** Callback priorities
75  *
76  * The higher the priority, the earlier in callback gets called.
77  */
78 #define DL_PRIORITY_DICT 30 //!< Callback priority for dictionary autoloading
79 #define DL_PRIORITY_DICT_ATTR 29 //!< Callback priority for attribute resolution
80 #define DL_PRIORITY_DICT_ENUM 28 //!< Callback priority for enum resolution
81 #define DL_PRIORITY_LIB 20 //!< Callback priority for library config
82 #define DL_PRIORITY_BOOTSTRAP 10 //!< Callback priority for bootstrap callback
83 
85 
86 typedef struct {
89 
90 /** Module detach callback
91  *
92  * Is called just before the server exits, and after re-instantiation on HUP,
93  * to free the old module instance.
94  *
95  * Detach should close all handles associated with the module instance, and
96  * free any memory allocated during instantiate.
97  *
98  * @param[in] inst to free.
99  * @return
100  * - 0 on success.
101  * - -1 if detach failed.
102  */
103 typedef int (*module_detach_t)(module_detach_ctx_t const *inst);
104 
105 /** Callback to call when a module is first loaded
106  *
107  */
108 typedef int (*dl_module_onload_t)(void);
109 
110 
111 /** Callback when a module is destroyed
112  *
113  */
114 typedef void (*dl_module_unload_t)(void);
115 
116 /** Common fields for the interface struct modules export
117  *
118  */
119 #define DL_MODULE_COMMON \
120  struct { \
121  uint64_t magic; \
122  char const *name; \
123  size_t inst_size; \
124  char const *inst_type; \
125  conf_parser_t const *config; \
126  dl_module_onload_t onload; \
127  dl_module_unload_t unload; \
128  module_detach_t detach; \
129  }
130 
131 /** Fields common to all types of loadable modules
132  */
133 typedef struct {
136 
137 /** Module handle
138  *
139  * Contains module's dlhandle, and the functions it exports.
140  */
141 typedef struct dl_module_s dl_module_t;
142 struct dl_module_s {
143  dl_t * _CONST dl; //!< Dynamic loader handle.
144 
145  dl_module_t const * _CONST parent; //!< of this module.
146 
147  dl_module_type_t _CONST type; //!< of this module.
148 
149  dl_module_common_t const * _CONST common; //!< Symbol exported by the module, containing its public
150  //!< functions, name and behaviour control flags.
151 
152  CONF_SECTION * _CONST conf; //!< The module's global configuration
153  ///< (as opposed to the instance, configuration).
154  ///< May be NULL.
156 };
157 
158 /** A module/inst tuple
159  *
160  * Used to pass data back from dl_module_instance_parse_func
161  */
163  char const * _CONST name; //!< Instance name.
164  dl_module_t const * _CONST module; //!< Module
165  void * _CONST data; //!< Module instance's parsed configuration.
166  CONF_SECTION * _CONST conf; //!< Module's instance configuration.
167  dl_module_inst_t const * _CONST parent; //!< Parent module's instance (if any).
168  bool _CONST detached; //!< Whether the detach function has been called.
169 };
170 
172 extern size_t dl_module_type_prefix_len;
173 
174 dl_module_t const *dl_module(dl_module_t const *parent, char const *name, dl_module_type_t type);
175 
177 
179 
181 
182 char const *dl_module_instance_name_by_data(void const *data);
183 
184 void *dl_module_parent_data_by_child_data(void const *data);
185 
186 void *dl_module_instance_symbol(dl_module_inst_t const *instance, char const *sym_name);
187 
188 int dl_module_instance(TALLOC_CTX *ctx, dl_module_inst_t **out,
189  dl_module_inst_t const *parent,
190  dl_module_type_t type, char const *name, char const *inst_name);
191 
193 
195 
196 char const *dl_module_search_path(void);
197 
199 
200 dl_module_loader_t *dl_module_loader_init(char const *lib_dir);
201 
202 #undef _CONST
203 
204 #ifdef __cplusplus
205 }
206 #endif
#define RCSIDH(h, id)
Definition: build.h:445
A section grouping multiple CONF_PAIR.
Definition: cf_priv.h:89
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:52
Wrapper struct around dl_loader_t.
Definition: dl_module.c:46
char const * dl_module_search_path(void)
Definition: dl_module.c:657
bool _CONST in_tree
Definition: dl_module.h:155
dl_t *_CONST dl
Dynamic loader handle.
Definition: dl_module.h:143
char const * dl_module_inst_name_from_conf(CONF_SECTION *conf)
Avoid boilerplate when setting the module instance name.
Definition: dl_module.c:584
dl_module_t const *_CONST parent
of this module.
Definition: dl_module.h:145
dl_module_type_t _CONST type
of this module.
Definition: dl_module.h:147
dl_module_type_t
Definition: dl_module.h:67
@ DL_MODULE_TYPE_PROTO
Protocol module.
Definition: dl_module.h:69
@ DL_MODULE_TYPE_SUBMODULE
Driver (or method in the case of EAP)
Definition: dl_module.h:71
@ DL_MODULE_TYPE_MODULE
Standard loadable module.
Definition: dl_module.h:68
@ DL_MODULE_TYPE_PROCESS
protocol processor.
Definition: dl_module.h:70
dl_module_t const *_CONST module
Module.
Definition: dl_module.h:164
fr_table_num_sorted_t const dl_module_type_prefix[]
Name prefixes matching the types of loadable module.
Definition: dl_module.c:66
bool _CONST detached
Whether the detach function has been called.
Definition: dl_module.h:168
char const * dl_module_instance_name_by_data(void const *data)
Lookup instance name via instance data.
Definition: dl_module.c:235
int dl_module_instance(TALLOC_CTX *ctx, dl_module_inst_t **out, dl_module_inst_t const *parent, dl_module_type_t type, char const *name, char const *inst_name)
Load a module and parse its CONF_SECTION in one operation.
Definition: dl_module.c:552
int(* dl_module_onload_t)(void)
Callback to call when a module is first loaded.
Definition: dl_module.h:108
char const *_CONST name
Instance name.
Definition: dl_module.h:163
dl_module_inst_t const * dl_module_parent_instance(dl_module_inst_t const *child)
Lookup a module's parent.
Definition: dl_module.c:207
void * dl_module_parent_data_by_child_data(void const *data)
A convenience function for returning a parent's private data.
Definition: dl_module.c:252
void(* dl_module_unload_t)(void)
Callback when a module is destroyed.
Definition: dl_module.h:114
void *_CONST data
Module instance's parsed configuration.
Definition: dl_module.h:165
#define _CONST
Definition: dl_module.h:47
dl_module_inst_t const * dl_module_instance_by_data(void const *data)
Lookup a dl_module_inst_t via instance data.
Definition: dl_module.c:215
dl_module_loader_t * dl_module_loader_init(char const *lib_dir)
Initialise structures needed by the dynamic linker.
Definition: dl_module.c:706
void * dl_module_instance_symbol(dl_module_inst_t const *instance, char const *sym_name)
Retrieve a public symbol from a module using dlsym.
Definition: dl_module.c:526
CONF_SECTION *_CONST conf
The module's global configuration (as opposed to the instance, configuration).
Definition: dl_module.h:152
CONF_SECTION *_CONST conf
Module's instance configuration.
Definition: dl_module.h:166
dl_module_common_t const *_CONST common
Symbol exported by the module, containing its public functions, name and behaviour control flags.
Definition: dl_module.h:149
dl_loader_t * dl_loader_from_module_loader(dl_module_loader_t *dl_module_loader)
Definition: dl_module.c:662
dl_module_t const * dl_module(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:382
dl_module_inst_t const * inst
Definition: dl_module.h:87
size_t dl_module_type_prefix_len
Definition: dl_module.c:72
int(* module_detach_t)(module_detach_ctx_t const *inst)
Module detach callback.
Definition: dl_module.h:103
dl_module_inst_t const * dl_module_instance_by_cs(CONF_SECTION const *cs)
Lookup a dl_module_inst_t via a config section.
Definition: dl_module.c:227
int dl_module_conf_parse(dl_module_inst_t *dl_inst, CONF_SECTION *conf)
Definition: dl_module.c:594
dl_module_inst_t const *_CONST parent
Parent module's instance (if any).
Definition: dl_module.h:167
Fields common to all types of loadable modules.
Definition: dl_module.h:133
A module/inst tuple.
Definition: dl_module.h:162
static rs_t * conf
Definition: radsniff.c:53
static char const * name
eap_aka_sim_process_conf_t * inst
fr_aka_sim_id_type_t type
An element in a lexicographically sorted array of name to num mappings.
Definition: table.h:45
static fr_slen_t parent
Definition: pair.h:844
static fr_slen_t data
Definition: value.h:1259
static size_t char ** out
Definition: value.h:984