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