2002-04-08 20:04:50 +04:00
/*
* Copyright ( C ) 2002 Sistina Software ( UK ) Limited .
*
* This file is released under the LGPL .
*
*/
2002-11-18 17:01:16 +03:00
# include "lib.h"
2002-04-08 20:04:50 +04:00
# include "locking_types.h"
# include "defaults.h"
2002-11-18 17:01:16 +03:00
# include "sharedlib.h"
2002-04-08 20:04:50 +04:00
# include <dlfcn.h>
2002-11-18 17:01:16 +03:00
static void * _locking_lib = NULL ;
static void ( * _end_fn ) ( void ) = NULL ;
static int ( * _lock_fn ) ( struct cmd_context * cmd , const char * resource ,
int flags ) = NULL ;
static int ( * _init_fn ) ( int type , struct config_tree * cf ) = NULL ;
2002-04-08 20:04:50 +04:00
2002-11-18 17:01:16 +03:00
static int _lock_resource ( struct cmd_context * cmd , const char * resource ,
int flags )
2002-04-08 20:04:50 +04:00
{
2002-11-18 17:01:16 +03:00
if ( _lock_fn )
return _lock_fn ( cmd , resource , flags ) ;
2002-04-24 22:20:51 +04:00
else
return 0 ;
2002-04-08 20:04:50 +04:00
}
2002-11-18 17:01:16 +03:00
static void _fin_external_locking ( void )
2002-04-08 20:04:50 +04:00
{
2002-11-18 17:01:16 +03:00
if ( _end_fn )
_end_fn ( ) ;
2002-04-08 20:04:50 +04:00
2002-11-18 17:01:16 +03:00
dlclose ( _locking_lib ) ;
2002-04-08 20:04:50 +04:00
2002-11-18 17:01:16 +03:00
_locking_lib = NULL ;
_init_fn = NULL ;
_end_fn = NULL ;
_lock_fn = NULL ;
2002-04-08 20:04:50 +04:00
}
2002-11-18 17:01:16 +03:00
int init_external_locking ( struct locking_type * locking , struct config_tree * cf )
2002-04-08 20:04:50 +04:00
{
2002-11-18 17:01:16 +03:00
const char * libname ;
2002-04-08 20:04:50 +04:00
2002-11-18 17:01:16 +03:00
if ( _locking_lib ) {
2002-04-24 22:20:51 +04:00
log_error ( " External locking already initialised " ) ;
return 1 ;
2002-04-08 20:04:50 +04:00
}
2002-11-18 17:01:16 +03:00
locking - > lock_resource = _lock_resource ;
locking - > fin_locking = _fin_external_locking ;
2002-04-08 20:04:50 +04:00
2002-11-18 17:01:16 +03:00
libname = find_config_str ( cf - > root , " global/locking_library " , ' / ' ,
DEFAULT_LOCKING_LIB ) ;
2002-04-24 22:20:51 +04:00
2002-11-18 17:01:16 +03:00
if ( ! ( _locking_lib = load_shared_library ( cf , libname , " locking " ) ) ) {
stack ;
2002-04-24 22:20:51 +04:00
return 0 ;
2002-04-08 20:04:50 +04:00
}
/* Get the functions we need */
2002-11-18 17:01:16 +03:00
if ( ! ( _init_fn = dlsym ( _locking_lib , " init_locking " ) ) | |
! ( _lock_fn = dlsym ( _locking_lib , " lock_resource " ) ) | |
! ( _end_fn = dlsym ( _locking_lib , " end_locking " ) ) ) {
log_error ( " Shared library %s does not contain locking "
" functions " , libname ) ;
dlclose ( _locking_lib ) ;
_locking_lib = NULL ;
2002-04-24 22:20:51 +04:00
return 0 ;
2002-04-08 20:04:50 +04:00
}
2002-11-18 17:01:16 +03:00
log_verbose ( " Loaded external locking library %s " , libname ) ;
return _init_fn ( 2 , cf ) ;
2002-04-08 20:04:50 +04:00
}