The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
dl.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: 7ab8dc5798d5af68d350a761ceea9882bcd3e355 $
20  *
21  * @file dl.h
22  * @brief Wrappers around dlopen.
23  *
24  * @copyright 2016 The FreeRADIUS server project
25  */
26 RCSIDH(dl_h, "$Id: 7ab8dc5798d5af68d350a761ceea9882bcd3e355 $")
27 
28 #ifndef HAVE_DLFCN_H
29 # error FreeRADIUS needs a working dlopen()
30 #else
31 # include <dlfcn.h>
32 #endif
33 #include <freeradius-devel/util/dlist.h>
34 #include <freeradius-devel/util/rb.h>
35 #include <freeradius-devel/util/talloc.h>
36 #include <freeradius-devel/util/version.h>
37 
38 #include <stdbool.h>
39 
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43 
44 #ifdef __APPLE__
45 # define DL_EXTENSION ".dylib"
46 #elif defined (WIN32)
47 # define DL_EXTENSION ".dll"
48 #else
49 # define DL_EXTENSION ".so"
50 #endif
51 
52 typedef struct dl_loader_s dl_loader_t;
53 
54 /** Module handle
55  *
56  * Contains module's dlhandle, and the functions it exports.
57  */
58 typedef struct dl_s {
59  fr_rb_node_t node; //!< Entry in the rbtree module handles.
60 
61  char const *name; //!< Name of the module e.g. sql.
62  void *handle; //!< Handle returned by dlopen.
63  dl_loader_t *loader; //!< Loader that owns this dl.
64 
65  void *uctx; //!< API client's opaque data.
66  bool uctx_free; //!< Free opaque data on dl_t free (usually false).
67  bool in_tree; //!< Whether this dl is registered in the dl_tree.
68 } dl_t;
69 
70 /** Callback to call when a module is first loaded
71  *
72  * @param[in] module being loaded.
73  * @param[in] symbol which, if present, will trigger this callback.
74  * @param[in] user_ctx passed to dl_loader_init_register.
75  * @return
76  * - 0 on success.
77  * - -1 on failure
78  */
79 typedef int (*dl_onload_t)(dl_t const *module, void *symbol, void *user_ctx);
80 
81 
82 /** Callback when a module is destroyed
83  *
84  * @param[in] module being loaded.
85  * @param[in] symbol which, if present, will trigger this callback.
86  * @param[in] user_ctx passed to dl_loader_init_register
87  */
88 typedef void (*dl_unload_t)(dl_t const *module, void *symbol, void *user_ctx);
89 
90 /*
91  * Functions
92  */
93 void *dl_open_by_sym(char const *sym_name, int flags);
94 
96 
98  unsigned int priority, char const *symbol,
99  dl_onload_t func, void *ctx);
100 
102  char const *symbol, dl_onload_t func);
103 
105  unsigned int priority, char const *symbol,
106  dl_unload_t func, void *ctx);
107 
109  char const *symbol, dl_unload_t func);
110 
111 dl_t *dl_by_name(dl_loader_t *dl_loader, char const *name,
112  void *uctx, bool uctx_free) CC_HINT(nonnull(1,2));
113 
114 int dl_free(dl_t const *dl);
115 
116 char const *dl_search_path(dl_loader_t *dl_loader) CC_HINT(nonnull);
117 
118 int dl_search_global_path_set(char const *lib_dir) CC_HINT(nonnull);
119 
120 int dl_search_path_set(dl_loader_t *dl_loader, char const *lib_dir) CC_HINT(nonnull);
121 
122 int dl_search_path_prepend(dl_loader_t *dl_loader, char const *lib_dir) CC_HINT(nonnull);
123 
124 int dl_search_path_append(dl_loader_t *dl_loader, char const *lib_dir) CC_HINT(nonnull);
125 
127 
128 dl_loader_t *dl_loader_init(TALLOC_CTX *ctx, void *uctx, bool uctx_free, bool defer_symbol_init);
129 
130 bool dl_loader_set_static(dl_loader_t *dl_loader, bool do_static) CC_HINT(nonnull);
131 
133 #ifdef __cplusplus
134 }
135 #endif
static dl_loader_t * dl_loader
Definition: fuzzer.c:43
static dl_t * dl
Definition: fuzzer.c:42
#define RCSIDH(h, id)
Definition: build.h:482
fr_dcursor_eval_t void const * uctx
Definition: dcursor.h:546
A dynamic loader.
Definition: dl.c:81
bool in_tree
Whether this dl is registered in the dl_tree.
Definition: dl.h:67
dl_t * dl_by_name(dl_loader_t *dl_loader, char const *name, void *uctx, bool uctx_free))
Search for a dl's shared object in various locations.
Definition: dl.c:470
dl_loader_t * dl_loader_init(TALLOC_CTX *ctx, void *uctx, bool uctx_free, bool defer_symbol_init)
Initialise structures needed by the dynamic linker.
Definition: dl.c:885
dl_loader_t * loader
Loader that owns this dl.
Definition: dl.h:63
struct dl_s dl_t
Module handle.
int dl_search_path_append(dl_loader_t *dl_loader, char const *lib_dir)
Append a new search path component to the library search path.
Definition: dl.c:843
int(* dl_onload_t)(dl_t const *module, void *symbol, void *user_ctx)
Callback to call when a module is first loaded.
Definition: dl.h:79
int dl_search_global_path_set(char const *lib_dir)
Set the global library path.
Definition: dl.c:768
bool uctx_free
Free opaque data on dl_t free (usually false).
Definition: dl.h:66
bool dl_loader_set_static(dl_loader_t *dl_loader, bool do_static)
Runtime override for doing static or dynamic module loading.
Definition: dl.c:950
void * handle
Handle returned by dlopen.
Definition: dl.h:62
fr_rb_node_t node
Entry in the rbtree module handles.
Definition: dl.h:59
int dl_search_path_prepend(dl_loader_t *dl_loader, char const *lib_dir)
Append a new search path component to the library search path.
Definition: dl.c:813
void * dl_open_by_sym(char const *sym_name, int flags)
Utility function to dlopen the library containing a particular symbol.
Definition: dl.c:186
void * uctx
API client's opaque data.
Definition: dl.h:65
char const * dl_search_path(dl_loader_t *dl_loader)
Return current library path.
Definition: dl.c:729
int dl_search_path_set(dl_loader_t *dl_loader, char const *lib_dir)
Set the current library path.
Definition: dl.c:788
int dl_symbol_init_cb_register(dl_loader_t *dl_loader, unsigned int priority, char const *symbol, dl_onload_t func, void *ctx)
Register a callback to execute when a dl with a particular symbol is first loaded.
Definition: dl.c:321
int dl_free(dl_t const *dl)
"free" a dl handle, possibly actually freeing it, and unloading the library
Definition: dl.c:678
void * dl_loader_uctx(dl_loader_t *dl_loader)
Retrieve the uctx from a dl_loader.
Definition: dl.c:868
int dl_symbol_init(dl_loader_t *dl_loader, dl_t const *dl)
Walk over the registered init callbacks, searching for the symbols they depend on.
Definition: dl.c:233
void dl_symbol_free_cb_unregister(dl_loader_t *dl_loader, char const *symbol, dl_unload_t func)
Unregister an callback that was to be executed when a dl was unloaded.
Definition: dl.c:419
void dl_symbol_init_cb_unregister(dl_loader_t *dl_loader, char const *symbol, dl_onload_t func)
Unregister an callback that was to be executed when a dl was first loaded.
Definition: dl.c:355
void dl_loader_debug(dl_loader_t *dl)
Called from a debugger to print information about a dl_loader.
Definition: dl.c:962
int dl_symbol_free_cb_register(dl_loader_t *dl_loader, unsigned int priority, char const *symbol, dl_unload_t func, void *ctx)
Register a callback to execute when a dl with a particular symbol is unloaded.
Definition: dl.c:384
char const * name
Name of the module e.g. sql.
Definition: dl.h:61
void(* dl_unload_t)(dl_t const *module, void *symbol, void *user_ctx)
Callback when a module is destroyed.
Definition: dl.h:88
Module handle.
Definition: dl.h:58
static char const * name
int nonnull(2, 5))