2009-07-28 18:46:53 +04:00
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <unistd.h>
# include <signal.h>
# include <sys/types.h>
/* Local includes */
2009-09-15 18:28:20 +04:00
# include <stdint.h>
2009-08-21 22:02:06 +04:00
# include <fence_virt.h>
2009-08-18 00:53:58 +04:00
# include <simpleconfig.h>
2010-01-14 21:38:37 +03:00
# include <static_map.h>
2009-07-28 18:46:53 +04:00
# include <server_plugin.h>
# include <debug.h>
2009-09-02 03:26:08 +04:00
# include <syslog.h>
2009-07-28 18:46:53 +04:00
2010-01-07 01:03:55 +03:00
/* configure.c */
int do_configure ( config_object_t * config , const char * filename ) ;
2010-01-15 02:45:47 +03:00
int daemon_init ( const char * prog , int nofork ) ;
int daemon_cleanup ( void ) ;
2010-01-07 01:03:55 +03:00
2009-07-28 18:46:53 +04:00
2009-11-05 21:07:06 +03:00
void
usage ( void )
{
printf ( " Usage: fence_virtd [options] \n " ) ;
printf ( " -F Do not daemonize. \n " ) ;
printf ( " -f <file> Use <file> as configuration file. \n " ) ;
printf ( " -d <level> Set debugging level to <level>. \n " ) ;
2010-01-07 01:03:55 +03:00
printf ( " -c Configuration mode. \n " ) ;
2010-12-08 21:52:59 +03:00
printf ( " -l List plugins. \n " ) ;
2009-11-05 21:07:06 +03:00
}
2010-01-15 02:45:47 +03:00
static int run = 1 ;
void
exit_handler ( int sig )
{
run = 0 ;
}
2009-07-28 18:46:53 +04:00
int
main ( int argc , char * * argv )
{
2009-09-02 02:54:17 +04:00
char val [ 4096 ] ;
2009-08-28 22:12:46 +04:00
char listener_name [ 80 ] ;
char backend_name [ 80 ] ;
2009-08-21 22:02:06 +04:00
const char * config_file = DEFAULT_CONFIG_FILE ;
2010-01-15 01:03:37 +03:00
config_object_t * config = NULL ;
map_object_t * map = NULL ;
2009-09-02 00:22:30 +04:00
const listener_plugin_t * lp ;
const backend_plugin_t * p ;
2009-08-28 22:12:46 +04:00
listener_context_t listener_ctx = NULL ;
backend_context_t backend_ctx = NULL ;
2010-01-13 01:32:13 +03:00
int debug_set = 0 , foreground = 0 , wait_for_backend = 0 ;
2010-01-07 01:03:55 +03:00
int opt , configure = 0 ;
2009-07-28 18:46:53 +04:00
2009-08-18 00:53:58 +04:00
config = sc_init ( ) ;
2010-01-14 21:38:37 +03:00
map = map_init ( ) ;
if ( ! config | | ! map ) {
perror ( " malloc " ) ;
return - 1 ;
}
2009-08-18 00:53:58 +04:00
2010-12-08 21:52:59 +03:00
while ( ( opt = getopt ( argc , argv , " Ff:d:cwlh " ) ) ! = EOF ) {
2009-08-18 00:53:58 +04:00
switch ( opt ) {
2009-09-02 03:26:08 +04:00
case ' F ' :
printf ( " Background mode disabled \n " ) ;
foreground = 1 ;
break ;
2009-08-18 00:53:58 +04:00
case ' f ' :
printf ( " Using %s \n " , optarg ) ;
2009-08-21 22:02:06 +04:00
config_file = optarg ;
2009-08-18 00:53:58 +04:00
break ;
case ' d ' :
2009-09-04 01:45:38 +04:00
debug_set = atoi ( optarg ) ;
2009-08-18 00:53:58 +04:00
break ;
2010-01-07 01:03:55 +03:00
case ' c ' :
configure = 1 ;
break ;
2010-01-13 01:32:13 +03:00
case ' w ' :
wait_for_backend = 1 ;
break ;
2010-12-08 21:52:59 +03:00
case ' l ' :
plugin_dump ( ) ;
return 0 ;
2009-11-05 21:07:06 +03:00
case ' h ' :
case ' ? ' :
usage ( ) ;
return 0 ;
2009-08-18 00:53:58 +04:00
default :
2009-09-02 03:26:08 +04:00
return - 1 ;
2009-08-18 00:53:58 +04:00
}
}
2010-01-07 01:03:55 +03:00
if ( configure ) {
return do_configure ( config , config_file ) ;
}
2009-08-21 22:02:06 +04:00
if ( sc_parse ( config , config_file ) ! = 0 ) {
printf ( " Failed to parse %s \n " , config_file ) ;
return - 1 ;
}
2009-08-18 00:53:58 +04:00
2009-09-04 01:45:38 +04:00
if ( debug_set ) {
snprintf ( val , sizeof ( val ) , " %d " , debug_set ) ;
sc_set ( config , " fence_virtd/@debug " , val ) ;
} else {
if ( sc_get ( config , " fence_virtd/@debug " , val , sizeof ( val ) ) = = 0 )
debug_set = atoi ( val ) ;
2009-08-18 00:53:58 +04:00
}
2009-09-04 01:45:38 +04:00
dset ( debug_set ) ;
2009-09-02 03:26:08 +04:00
if ( ! foreground ) {
if ( sc_get ( config , " fence_virtd/@foreground " ,
val , sizeof ( val ) ) = = 0 )
foreground = atoi ( val ) ;
}
2009-08-18 00:53:58 +04:00
2010-01-13 01:32:13 +03:00
if ( ! wait_for_backend ) {
if ( sc_get ( config , " fence_virtd/@wait_for_backend " ,
val , sizeof ( val ) ) = = 0 )
wait_for_backend = atoi ( val ) ;
}
2009-09-02 03:26:08 +04:00
if ( dget ( ) > 3 )
sc_dump ( config , stdout ) ;
2009-08-18 00:53:58 +04:00
2009-08-28 22:12:46 +04:00
if ( sc_get ( config , " fence_virtd/@backend " , backend_name ,
sizeof ( backend_name ) ) ) {
2009-08-18 00:53:58 +04:00
printf ( " Failed to determine backend. \n " ) ;
printf ( " %s \n " , val ) ;
return - 1 ;
}
2009-09-02 03:26:08 +04:00
dbg_printf ( 1 , " Backend plugin: %s \n " , backend_name ) ;
2009-08-28 22:12:46 +04:00
if ( sc_get ( config , " fence_virtd/@listener " , listener_name ,
sizeof ( listener_name ) ) ) {
printf ( " Failed to determine backend. \n " ) ;
printf ( " %s \n " , val ) ;
return - 1 ;
}
2009-09-02 03:26:08 +04:00
dbg_printf ( 1 , " Listener plugin: %s \n " , listener_name ) ;
2009-07-28 18:46:53 +04:00
2009-08-17 17:58:06 +04:00
# ifdef _MODULE
2009-09-02 02:54:17 +04:00
if ( sc_get ( config , " fence_virtd/@module_path " , val ,
sizeof ( val ) ) ) {
2009-09-15 23:17:14 +04:00
# ifdef MODULE_PATH
snprintf ( val , sizeof ( val ) , MODULE_PATH ) ;
# else
2009-09-02 02:54:17 +04:00
printf ( " Failed to determine module path. \n " ) ;
return - 1 ;
2009-09-15 23:17:14 +04:00
# endif
2009-08-17 17:58:06 +04:00
}
2009-09-02 02:54:17 +04:00
2009-09-02 03:26:08 +04:00
dbg_printf ( 1 , " Searching %s for plugins... \n " , val ) ;
2009-09-02 02:54:17 +04:00
opt = plugin_search ( val ) ;
if ( opt > 0 ) {
2009-09-02 03:26:08 +04:00
dbg_printf ( 1 , " %d plugins found \n " , opt ) ;
2009-09-02 02:54:17 +04:00
} else {
printf ( " No plugins found \n " ) ;
return 1 ;
2009-08-21 22:12:58 +04:00
}
2009-09-02 02:54:17 +04:00
2009-09-15 23:17:14 +04:00
# endif
2009-09-02 03:26:08 +04:00
if ( dget ( ) > 3 )
plugin_dump ( ) ;
2009-07-28 18:46:53 +04:00
2009-09-02 00:22:30 +04:00
lp = plugin_find_listener ( listener_name ) ;
if ( ! lp ) {
printf ( " Could not find listener \" %s \" \n " , listener_name ) ;
return 1 ;
}
p = plugin_find_backend ( backend_name ) ;
2009-08-12 21:20:50 +04:00
if ( ! p ) {
2009-09-02 02:18:20 +04:00
printf ( " Could not find backend \" %s \" \n " , backend_name ) ;
2009-08-21 22:12:58 +04:00
return 1 ;
2009-08-12 21:20:50 +04:00
}
2010-01-15 02:45:47 +03:00
daemon_init ( basename ( argv [ 0 ] ) , foreground ) ;
signal ( SIGINT , exit_handler ) ;
signal ( SIGTERM , exit_handler ) ;
signal ( SIGQUIT , exit_handler ) ;
2009-09-02 03:26:08 +04:00
2010-01-13 01:32:13 +03:00
while ( p - > init ( & backend_ctx , config ) < 0 ) {
if ( ! wait_for_backend ) {
if ( foreground ) {
printf ( " Backend plugin %s failed to initialize \n " ,
backend_name ) ;
}
syslog ( LOG_ERR ,
" Backend plugin %s failed to initialize \n " ,
2009-09-02 03:26:08 +04:00
backend_name ) ;
2010-01-13 01:32:13 +03:00
return 1 ;
2009-09-02 03:26:08 +04:00
}
2010-01-13 01:32:13 +03:00
sleep ( 5 ) ;
2009-07-28 18:46:53 +04:00
}
2010-01-14 21:38:37 +03:00
if ( map_load ( map , config ) < 0 ) {
2010-01-15 02:45:47 +03:00
syslog ( LOG_WARNING , " Failed to load static maps \n " ) ;
2010-01-14 21:38:37 +03:00
}
2009-07-28 18:46:53 +04:00
/* only client we have now is mcast (fence_xvm behavior) */
2010-01-14 21:38:37 +03:00
if ( lp - > init ( & listener_ctx , p - > callbacks , config , map ,
2009-08-28 22:12:46 +04:00
backend_ctx ) < 0 ) {
2009-09-02 03:26:08 +04:00
if ( foreground ) {
printf ( " Listener plugin %s failed to initialize \n " ,
listener_name ) ;
}
syslog ( LOG_ERR ,
" Listener plugin %s failed to initialize \n " ,
listener_name ) ;
2009-07-28 18:46:53 +04:00
return 1 ;
}
2010-01-15 02:45:47 +03:00
while ( run & & lp - > dispatch ( listener_ctx , NULL ) > = 0 ) ;
map_release ( map ) ;
sc_release ( config ) ;
2009-07-28 18:46:53 +04:00
2009-09-02 00:22:30 +04:00
lp - > cleanup ( listener_ctx ) ;
2009-08-28 22:12:46 +04:00
p - > cleanup ( backend_ctx ) ;
2009-07-28 18:46:53 +04:00
2010-01-15 02:45:47 +03:00
daemon_cleanup ( ) ;
2009-07-28 18:46:53 +04:00
return 0 ;
}