The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Data Structures | Macros | Typedefs | Functions | Variables
dl.c File Reference

Wrappers around dlopen to manage loading shared objects at runtime. More...

#include <freeradius-devel/server/dl_module.h>
#include <freeradius-devel/server/log.h>
#include <freeradius-devel/util/atexit.h>
#include <freeradius-devel/util/debug.h>
#include <freeradius-devel/util/paths.h>
#include <freeradius-devel/util/syserror.h>
+ Include dependency graph for dl.c:

Go to the source code of this file.

Data Structures

struct  dl_loader_s
 A dynamic loader. More...
 
struct  dl_symbol_free_s
 
struct  dl_symbol_init_s
 

Macros

#define RTLD_LOCAL   (0)
 
#define RTLD_NOW   (0)
 
#define RUNNING_ON_VALGRIND   0
 

Typedefs

typedef struct dl_symbol_free_s dl_symbol_free_t
 Symbol dependent free callback. More...
 
typedef struct dl_symbol_init_s dl_symbol_init_t
 Symbol dependent initialisation callback. More...
 

Functions

static int _dl_free (dl_t *dl)
 Free a dl. More...
 
static int _dl_loader_free (dl_loader_t *dl_loader)
 
dl_tdl_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. More...
 
int dl_free (dl_t const *dl)
 "free" a dl handle, possibly actually freeing it, and unloading the library More...
 
static int8_t dl_handle_cmp (void const *one, void const *two)
 Compare the name of two dl_t. More...
 
void dl_loader_debug (dl_loader_t *dl)
 Called from a debugger to print information about a dl_loader. More...
 
dl_loader_tdl_loader_init (TALLOC_CTX *ctx, void *uctx, bool uctx_free, bool defer_symbol_init)
 Initialise structures needed by the dynamic linker. More...
 
bool dl_loader_set_static (dl_loader_t *dl_loader, bool do_static)
 Runtime override for doing static or dynamic module loading. More...
 
void * dl_loader_uctx (dl_loader_t *dl_loader)
 Retrieve the uctx from a dl_loader. More...
 
void * dl_open_by_sym (char const *sym_name, int flags)
 Utility function to dlopen the library containing a particular symbol. More...
 
int dl_search_global_path_set (char const *lib_dir)
 Set the global library path. More...
 
char const * dl_search_path (dl_loader_t *dl_loader)
 Return current library path. More...
 
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. More...
 
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. More...
 
int dl_search_path_set (dl_loader_t *dl_loader, char const *lib_dir)
 Set the current library path. More...
 
static int dl_symbol_free (dl_loader_t *dl_loader, dl_t const *dl)
 Walk over the registered init callbacks, searching for the symbols they depend on. More...
 
int dl_symbol_free_cb_register (dl_loader_t *dl_loader, unsigned int priority, char const *symbol, dl_unload_t func, void *uctx)
 Register a callback to execute when a dl with a particular symbol is unloaded. More...
 
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. More...
 
static int8_t dl_symbol_free_cmp (void const *one, void const *two)
 
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. More...
 
int dl_symbol_init_cb_register (dl_loader_t *dl_loader, unsigned int priority, char const *symbol, dl_onload_t func, void *uctx)
 Register a callback to execute when a dl with a particular symbol is first loaded. More...
 
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. More...
 
static int8_t dl_symbol_init_cmp (void const *one, void const *two)
 

Variables

static char * dl_global_libdir = NULL
 Global search path, prepended all dlopen paths. More...
 

Detailed Description

Wrappers around dlopen to manage loading shared objects at runtime.

Id
37717f9f82c82f4dbd6ede12de7e9f66a357e939

Definition in file dl.c.


Data Structure Documentation

◆ dl_loader_s

struct dl_loader_s

A dynamic loader.

Definition at line 81 of file dl.c.

+ Collaboration diagram for dl_loader_s:
Data Fields
bool defer_symbol_init Do not call dl_symbol_init in dl_loader_init.
bool do_dlclose dlclose modules when we're done with them.
bool do_static Do all symbol resolution using the special RTLD_DEFAULT handle, instead of attempting to load modules using dlopen().

This is useful when FreeRADIUS has been built as a monolithic binary.

char * lib_dir Where the libraries live.
fr_dlist_head_t sym_free Linked list of symbol free callbacks.
Note
Is linked list to retain insertion order. We don't expect huge numbers of callbacks so there shouldn't be efficiency issues.
fr_dlist_head_t sym_init Linked list of symbol init callbacks.
Note
Is linked list to retain insertion order. We don't expect huge numbers of callbacks so there shouldn't be efficiency issues.
fr_rb_tree_t * tree Tree of shared objects loaded.
void * uctx dl private extension data.
bool uctx_free Free uctx when dl_loader_t is freed.

◆ dl_symbol_free_s

struct dl_symbol_free_s

Definition at line 69 of file dl.c.

+ Collaboration diagram for dl_symbol_free_s:
Data Fields
fr_dlist_t entry Entry into the list of 'free' symbol callbacks.
dl_unload_t func to call when symbol is found in a dl's symbol table.
unsigned int priority Call priority.
char const * symbol to search for. May be NULL in which case func is always called.
void * uctx User data to pass to func.

◆ dl_symbol_init_s

struct dl_symbol_init_s

Definition at line 55 of file dl.c.

+ Collaboration diagram for dl_symbol_init_s:
Data Fields
fr_dlist_t entry Entry into the list of 'init' symbol callbacks.
dl_onload_t func to call when symbol is found in a dl's symbol table.
unsigned int priority Call priority.
char const * symbol to search for. May be NULL in which case func is always called.
void * uctx User data to pass to func.

Macro Definition Documentation

◆ RTLD_LOCAL

#define RTLD_LOCAL   (0)

Definition at line 47 of file dl.c.

◆ RTLD_NOW

#define RTLD_NOW   (0)

Definition at line 44 of file dl.c.

◆ RUNNING_ON_VALGRIND

#define RUNNING_ON_VALGRIND   0

Definition at line 40 of file dl.c.

Typedef Documentation

◆ dl_symbol_free_t

Symbol dependent free callback.

Call this function before the dl is unloaded.

Definition at line 1 of file dl.c.

◆ dl_symbol_init_t

Symbol dependent initialisation callback.

Call this function when the dl is loaded for the first time.

Definition at line 1 of file dl.c.

Function Documentation

◆ _dl_free()

static int _dl_free ( dl_t dl)
static

Free a dl.

Close dl's dlhandle, unloading it.

Parameters
[in]dlto close.
Returns
0.

Definition at line 437 of file dl.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _dl_loader_free()

static int _dl_loader_free ( dl_loader_t dl_loader)
static

Definition at line 685 of file dl.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ dl_by_name()

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.

Note
You must call dl_symbol_init when ready to call autoloader callbacks.
Parameters
[in]dl_loaderTree of dynamically loaded libraries, and callbacks.
[in]nameof library to load. May be a relative path.
[in]uctxData to store within the dl_t.
[in]uctx_freetalloc_free the passed in uctx data if this dl_t is freed.
Returns
  • A new dl_t on success, or a pointer to an existing one with the reference count increased.
  • NULL on error.

Definition at line 470 of file dl.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ dl_free()

int dl_free ( dl_t const *  dl)

"free" a dl handle, possibly actually freeing it, and unloading the library

This function should be used to explicitly free a dl.

Because dls are reference counted, it may not actually free the memory or unload the library, but it will reduce the reference count.

Returns
  • 0 if the dl was actually freed.
  • >0 the number of remaining references.

Definition at line 678 of file dl.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ dl_handle_cmp()

static int8_t dl_handle_cmp ( void const *  one,
void const *  two 
)
static

Compare the name of two dl_t.

Definition at line 168 of file dl.c.

+ Here is the caller graph for this function:

◆ dl_loader_debug()

void dl_loader_debug ( dl_loader_t dl)

Called from a debugger to print information about a dl_loader.

Definition at line 962 of file dl.c.

◆ dl_loader_init()

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.

Parameters
[in]ctxTo bind lifetime of dl_loader_t too.
[in]uctxAPI client opaque data to store in dl_loader_t.
[in]uctx_freeCall talloc_free() on uctx when the dl_loader_t is freed.
[in]defer_symbol_initIf true, it is up to the caller to call dl_symbol_init after calling dl_by_name. This prevents any of the registered callbacks from executing until dl_symbol_init is called explicitly.

Definition at line 885 of file dl.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ dl_loader_set_static()

bool dl_loader_set_static ( dl_loader_t dl_loader,
bool  do_static 
)

Runtime override for doing static or dynamic module loading.

Parameters
[in]dl_loaderto configure.
[in]do_staticIf true, all dlopen calls result in a reference to RTLD_DEFAULT being returned which allows all the dynamic loading infrastructure to worth correctly with a monolithic binary.
Returns
The previous value of do_static

Definition at line 950 of file dl.c.

◆ dl_loader_uctx()

void* dl_loader_uctx ( dl_loader_t dl_loader)

Retrieve the uctx from a dl_loader.

Definition at line 868 of file dl.c.

◆ dl_open_by_sym()

void* dl_open_by_sym ( char const *  sym_name,
int  flags 
)

Utility function to dlopen the library containing a particular symbol.

Note
Not really part of our 'dl' API, just a convenience function.
Parameters
[in]sym_nameto resolve.
[in]flagsto pass to dlopen.
Returns
  • NULL on error.
  • A new handle on success.

Definition at line 186 of file dl.c.

+ Here is the caller graph for this function:

◆ dl_search_global_path_set()

int dl_search_global_path_set ( char const *  lib_dir)

Set the global library path.

Parameters
[in]lib_dir":" separated list of paths to search for libraries in.
Returns
  • 0 on success.
  • -1 on failure.

Definition at line 768 of file dl.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ dl_search_path()

char const* dl_search_path ( dl_loader_t dl_loader)

Return current library path.

Definition at line 729 of file dl.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ dl_search_path_append()

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.

Parameters
[in]dl_loaderto add search path component for.
[in]lib_dirto add. Does not require a ":" prefix.
Returns
  • 0 on success.
  • -1 on failure.

Definition at line 843 of file dl.c.

◆ dl_search_path_prepend()

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.

Parameters
[in]dl_loaderto add search path component for.
[in]lib_dirto add. Does not require a ":" prefix.
Returns
  • 0 on success.
  • -1 on failure.

Definition at line 813 of file dl.c.

+ Here is the caller graph for this function:

◆ dl_search_path_set()

int dl_search_path_set ( dl_loader_t dl_loader,
char const *  lib_dir 
)

Set the current library path.

Parameters
[in]dl_loaderto add search path component for.
[in]lib_dirA ":" separated list of paths to search for libraries in.

Definition at line 788 of file dl.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ dl_symbol_free()

static int dl_symbol_free ( dl_loader_t dl_loader,
dl_t const *  dl 
)
static

Walk over the registered init callbacks, searching for the symbols they depend on.

Allows code outside of the dl API to register free functions that get executed depending on whether the dl exports a particular symbol.

This cuts down the amount of boilerplate code in 'mod_unload' functions.

Parameters
[in]dl_loaderTree of dynamically loaded libraries, and callbacks.
[in]dlto search for symbols in.

Definition at line 279 of file dl.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ dl_symbol_free_cb_register()

int dl_symbol_free_cb_register ( dl_loader_t dl_loader,
unsigned int  priority,
char const *  symbol,
dl_unload_t  func,
void *  uctx 
)

Register a callback to execute when a dl with a particular symbol is unloaded.

Note
Will replace ctx data for callbacks with the same symbol/func.
Parameters
[in]dl_loaderTree of dynamically loaded libraries, and callbacks.
[in]priorityExecution priority. Callbacks with a higher priority get called first.
[in]symbolthat determines whether func should be called. "<modname>_" is added as a prefix to the symbol. The prefix is added because some dls are loaded with RTLD_GLOBAL into the global symbol space, so the symbols they export must be unique. May be NULL to always call the function.
[in]functo register. Called then dl is unloaded.
[in]uctxto pass to func.
Returns
  • 0 on success (or already registered).
  • -1 on failure.

Definition at line 384 of file dl.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ dl_symbol_free_cb_unregister()

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.

Parameters
[in]dl_loaderTree of dynamically loaded libraries, and callbacks.
[in]symbolthe callback is attached to.
[in]functhe callback.

Definition at line 419 of file dl.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ dl_symbol_free_cmp()

static int8_t dl_symbol_free_cmp ( void const *  one,
void const *  two 
)
static

Definition at line 142 of file dl.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ dl_symbol_init()

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.

Allows code outside of the dl API to register initialisation functions that get executed depending on whether the dl exports a particular symbol.

This cuts down the amount of boilerplate code in 'mod_load' functions.

Parameters
[in]dl_loaderTree of dynamically loaded libraries, and callbacks.
[in]dlto search for symbols in.
Returns
  • 0 continue walking.
  • -1 error.

Definition at line 233 of file dl.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ dl_symbol_init_cb_register()

int dl_symbol_init_cb_register ( dl_loader_t dl_loader,
unsigned int  priority,
char const *  symbol,
dl_onload_t  func,
void *  uctx 
)

Register a callback to execute when a dl with a particular symbol is first loaded.

Note
Will replace ctx data for callbacks with the same symbol/func.
Parameters
[in]dl_loaderTree of dynamically loaded libraries, and callbacks.
[in]priorityExecution priority. Callbacks with a higher priority get called first.
[in]symbolthat determines whether func should be called. "<modname>_" is added as a prefix to the symbol. The prefix is added because some dls are loaded with RTLD_GLOBAL into the global symbol space, so the symbols they export must be unique. May be NULL to always call the function.
[in]functo register. Called when dl is loaded.
[in]uctxto pass to func.
Returns
  • 0 on success (or already registered).
  • -1 on failure.

Definition at line 321 of file dl.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ dl_symbol_init_cb_unregister()

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.

Parameters
[in]dl_loaderTree of dynamically loaded libraries, and callbacks.
[in]symbolthe callback is attached to.
[in]functhe callback.

Definition at line 355 of file dl.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ dl_symbol_init_cmp()

static int8_t dl_symbol_init_cmp ( void const *  one,
void const *  two 
)
static

Definition at line 119 of file dl.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ dl_global_libdir

char* dl_global_libdir = NULL
static

Global search path, prepended all dlopen paths.

Definition at line 117 of file dl.c.