The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
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: 2ee2a1e939201f98ddbf72cc0bad4405e0aff9b5 $
20 *
21 * @file dl.h
22 * @brief Wrappers around dlopen.
23 *
24 * @copyright 2016 The FreeRADIUS server project
25 */
26RCSIDH(dl_h, "$Id: 2ee2a1e939201f98ddbf72cc0bad4405e0aff9b5 $")
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
39#ifdef __cplusplus
40extern "C" {
41#endif
42
43#ifdef __APPLE__
44# define DL_EXTENSION ".dylib"
45#elif defined (WIN32)
46# define DL_EXTENSION ".dll"
47#else
48# define DL_EXTENSION ".so"
49#endif
50
51typedef struct dl_loader_s dl_loader_t;
52
53/** Module handle
54 *
55 * Contains module's dlhandle, and the functions it exports.
56 */
57typedef struct dl_s {
58 fr_rb_node_t node; //!< Entry in the rbtree module handles.
59
60 char const *name; //!< Name of the module e.g. sql.
61 void *handle; //!< Handle returned by dlopen.
62 dl_loader_t *loader; //!< Loader that owns this dl.
63
64 void *uctx; //!< API client's opaque data.
65 bool uctx_free; //!< Free opaque data on dl_t free (usually false).
66 bool in_tree; //!< Whether this dl is registered in the dl_tree.
68
69/** Callback to call when a module is first loaded
70 *
71 * @param[in] module being loaded.
72 * @param[in] symbol which, if present, will trigger this callback.
73 * @param[in] user_ctx passed to dl_loader_init_register.
74 * @return
75 * - 0 on success.
76 * - -1 on failure
77 */
78typedef int (*dl_onload_t)(dl_t const *module, void *symbol, void *user_ctx);
79
80
81/** Callback when a module is destroyed
82 *
83 * @param[in] module being loaded.
84 * @param[in] symbol which, if present, will trigger this callback.
85 * @param[in] user_ctx passed to dl_loader_init_register
86 */
87typedef void (*dl_unload_t)(dl_t const *module, void *symbol, void *user_ctx);
88
89/*
90 * Functions
91 */
92void *dl_open_by_sym(char const *sym_name, int flags);
93
95
97 unsigned int priority, char const *symbol,
98 dl_onload_t func, void *ctx);
99
101 char const *symbol, dl_onload_t func);
102
104 unsigned int priority, char const *symbol,
105 dl_unload_t func, void *ctx);
106
108 char const *symbol, dl_unload_t func);
109
111 void *uctx, bool uctx_free) CC_HINT(nonnull(1,2));
112
113int dl_free(dl_t const *dl);
114
115char const *dl_search_path(dl_loader_t *dl_loader) CC_HINT(nonnull);
116
117int dl_search_global_path_set(char const *lib_dir) CC_HINT(nonnull);
118
119int dl_search_path_set(dl_loader_t *dl_loader, char const *lib_dir) CC_HINT(nonnull);
120
121int dl_search_path_prepend(dl_loader_t *dl_loader, char const *lib_dir) CC_HINT(nonnull);
122
123int dl_search_path_append(dl_loader_t *dl_loader, char const *lib_dir) CC_HINT(nonnull);
124
126
127dl_loader_t *dl_loader_init(TALLOC_CTX *ctx, void *uctx, bool uctx_free, bool defer_symbol_init);
128
129bool dl_loader_set_static(dl_loader_t *dl_loader, bool do_static) CC_HINT(nonnull);
130
131void dl_loader_debug(FILE *fp, dl_loader_t *dl) CC_HINT(nonnull);
132#ifdef __cplusplus
133}
134#endif
static dl_loader_t * dl_loader
Definition fuzzer.c:42
static dl_t * dl
Definition fuzzer.c:41
#define RCSIDH(h, id)
Definition build.h:507
A dynamic loader.
Definition dl.c:81
bool in_tree
Whether this dl is registered in the dl_tree.
Definition dl.h:66
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:900
dl_loader_t * loader
Loader that owns this dl.
Definition dl.h:62
struct dl_s dl_t
Module handle.
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
void dl_loader_debug(FILE *fp, dl_loader_t *dl)
Called from a debugger to print information about a dl_loader.
Definition dl.c:977
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:858
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:78
int dl_search_global_path_set(char const *lib_dir)
Set the global library path.
Definition dl.c:783
bool uctx_free
Free opaque data on dl_t free (usually false).
Definition dl.h:65
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:965
void * handle
Handle returned by dlopen.
Definition dl.h:61
fr_rb_node_t node
Entry in the rbtree module handles.
Definition dl.h:58
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:828
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:64
char const * dl_search_path(dl_loader_t *dl_loader)
Return current library path.
Definition dl.c:744
int dl_search_path_set(dl_loader_t *dl_loader, char const *lib_dir)
Set the current library path.
Definition dl.c:803
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
void * dl_loader_uctx(dl_loader_t *dl_loader)
Retrieve the uctx from a dl_loader.
Definition dl.c:883
int dl_free(dl_t const *dl)
"free" a dl handle, possibly actually freeing it, and unloading the library
Definition dl.c:693
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
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:60
void(* dl_unload_t)(dl_t const *module, void *symbol, void *user_ctx)
Callback when a module is destroyed.
Definition dl.h:87
Module handle.
Definition dl.h:57
static char const * name
int nonnull(2, 5))