2015-03-13 17:01:25 +03:00
/*
2007-07-04 11:45:46 +04:00
event script handling
Copyright ( C ) Andrew Tridgell 2007
This program is free software ; you can redistribute it and / or modify
it under the terms of the GNU General Public License as published by
2007-07-10 09:29:31 +04:00
the Free Software Foundation ; either version 3 of the License , or
2007-07-04 11:45:46 +04:00
( at your option ) any later version .
2015-03-13 17:01:25 +03:00
2007-07-04 11:45:46 +04:00
This program is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU General Public License for more details .
2015-03-13 17:01:25 +03:00
2007-07-04 11:45:46 +04:00
You should have received a copy of the GNU General Public License
2007-07-10 09:29:31 +04:00
along with this program ; if not , see < http : //www.gnu.org/licenses/>.
2007-07-04 11:45:46 +04:00
*/
2015-10-26 08:50:46 +03:00
# include "replace.h"
2007-07-04 11:45:46 +04:00
# include "system/filesys.h"
2015-10-26 08:50:46 +03:00
# include "system/network.h"
2007-07-04 11:45:46 +04:00
# include "system/wait.h"
2007-08-20 05:10:30 +04:00
# include "system/dir.h"
# include "system/locale.h"
2015-10-26 08:50:46 +03:00
# include "system/time.h"
2016-08-27 10:26:28 +03:00
# include "system/dir.h"
2015-10-26 08:50:46 +03:00
# include <talloc.h>
# include <tevent.h>
2012-02-22 10:38:12 +04:00
# include "lib/util/dlinklist.h"
2015-10-26 08:50:46 +03:00
# include "lib/util/debug.h"
# include "lib/util/samba_util.h"
2016-11-29 04:55:06 +03:00
# include "lib/util/sys_rw.h"
2015-10-26 08:50:46 +03:00
# include "ctdb_private.h"
2015-10-23 06:17:34 +03:00
# include "common/common.h"
2015-11-11 07:41:10 +03:00
# include "common/logging.h"
2016-08-27 10:26:28 +03:00
# include "common/reqid.h"
# include "common/sock_io.h"
2018-06-21 09:38:01 +03:00
# include "common/path.h"
2016-08-27 10:26:28 +03:00
2018-06-21 10:16:07 +03:00
# include "protocol/protocol_util.h"
# include "event/event_protocol_api.h"
2016-08-27 10:26:28 +03:00
/*
* Setting up event daemon
*/
struct eventd_context {
struct tevent_context * ev ;
const char * path ;
const char * socket ;
/* server state */
pid_t eventd_pid ;
struct tevent_fd * eventd_fde ;
/* client state */
struct reqid_context * idr ;
struct sock_queue * queue ;
struct eventd_client_state * calls ;
} ;
static bool eventd_context_init ( TALLOC_CTX * mem_ctx ,
struct ctdb_context * ctdb ,
struct eventd_context * * out )
{
struct eventd_context * ectx ;
2018-06-21 09:38:01 +03:00
const char * eventd = CTDB_HELPER_BINDIR " /ctdb-eventd " ;
2016-08-27 10:26:28 +03:00
const char * value ;
int ret ;
ectx = talloc_zero ( mem_ctx , struct eventd_context ) ;
if ( ectx = = NULL ) {
return false ;
}
ectx - > ev = ctdb - > ev ;
value = getenv ( " CTDB_EVENTD " ) ;
if ( value ! = NULL ) {
eventd = value ;
}
ectx - > path = talloc_strdup ( ectx , eventd ) ;
if ( ectx - > path = = NULL ) {
talloc_free ( ectx ) ;
return false ;
}
2018-06-21 09:38:01 +03:00
ectx - > socket = path_socket ( ectx , " eventd " ) ;
2016-08-27 10:26:28 +03:00
if ( ectx - > socket = = NULL ) {
talloc_free ( ectx ) ;
return false ;
}
ret = reqid_init ( ectx , 1 , & ectx - > idr ) ;
if ( ret ! = 0 ) {
talloc_free ( ectx ) ;
return false ;
}
ectx - > eventd_pid = - 1 ;
* out = ectx ;
return true ;
}
2018-08-27 07:47:38 +03:00
struct eventd_startup_state {
bool done ;
int ret ;
int fd ;
} ;
static void eventd_startup_timeout_handler ( struct tevent_context * ev ,
struct tevent_timer * te ,
struct timeval t ,
void * private_data )
{
struct eventd_startup_state * state =
( struct eventd_startup_state * ) private_data ;
state - > done = true ;
state - > ret = ETIMEDOUT ;
}
static void eventd_startup_handler ( struct tevent_context * ev ,
struct tevent_fd * fde , uint16_t flags ,
void * private_data )
{
struct eventd_startup_state * state =
( struct eventd_startup_state * ) private_data ;
unsigned int data ;
ssize_t num_read ;
num_read = sys_read ( state - > fd , & data , sizeof ( data ) ) ;
if ( num_read = = sizeof ( data ) ) {
if ( data = = 0 ) {
state - > ret = 0 ;
} else {
state - > ret = EIO ;
}
} else if ( num_read = = 0 ) {
state - > ret = EPIPE ;
} else if ( num_read = = - 1 ) {
state - > ret = errno ;
} else {
state - > ret = EINVAL ;
}
state - > done = true ;
}
static int wait_for_daemon_startup ( struct tevent_context * ev ,
int fd )
{
TALLOC_CTX * mem_ctx ;
struct tevent_timer * timer ;
struct tevent_fd * fde ;
struct eventd_startup_state state = {
. done = false ,
. ret = 0 ,
. fd = fd ,
} ;
mem_ctx = talloc_new ( ev ) ;
if ( mem_ctx = = NULL ) {
return ENOMEM ;
}
timer = tevent_add_timer ( ev ,
mem_ctx ,
tevent_timeval_current_ofs ( 10 , 0 ) ,
eventd_startup_timeout_handler ,
& state ) ;
if ( timer = = NULL ) {
talloc_free ( mem_ctx ) ;
return ENOMEM ;
}
fde = tevent_add_fd ( ev ,
mem_ctx ,
fd ,
TEVENT_FD_READ ,
eventd_startup_handler ,
& state ) ;
if ( fde = = NULL ) {
talloc_free ( mem_ctx ) ;
return ENOMEM ;
}
while ( ! state . done ) {
tevent_loop_once ( ev ) ;
}
talloc_free ( mem_ctx ) ;
return state . ret ;
}
2016-08-27 10:26:28 +03:00
/*
* Start and stop event daemon
*/
static bool eventd_client_connect ( struct eventd_context * ectx ) ;
static void eventd_dead_handler ( struct tevent_context * ev ,
struct tevent_fd * fde , uint16_t flags ,
void * private_data ) ;
int ctdb_start_eventd ( struct ctdb_context * ctdb )
{
struct eventd_context * ectx ;
const char * * argv ;
int fd [ 2 ] ;
pid_t pid ;
2018-08-27 07:53:37 +03:00
int ret ;
2016-08-27 10:26:28 +03:00
bool status ;
if ( ctdb - > ectx = = NULL ) {
status = eventd_context_init ( ctdb , ctdb , & ctdb - > ectx ) ;
if ( ! status ) {
DEBUG ( DEBUG_ERR ,
( " Failed to initialize eventd context \n " ) ) ;
return - 1 ;
}
}
ectx = ctdb - > ectx ;
2017-01-30 06:34:12 +03:00
if ( ! sock_clean ( ectx - > socket ) ) {
2017-01-13 08:00:45 +03:00
return - 1 ;
}
2018-08-27 07:44:24 +03:00
ret = pipe ( fd ) ;
if ( ret ! = 0 ) {
return - 1 ;
}
2018-08-27 07:47:38 +03:00
argv = talloc_array ( ectx , const char * , 6 ) ;
2016-08-27 10:26:28 +03:00
if ( argv = = NULL ) {
2018-08-27 07:44:24 +03:00
close ( fd [ 0 ] ) ;
close ( fd [ 1 ] ) ;
2016-08-27 10:26:28 +03:00
return - 1 ;
}
argv [ 0 ] = ectx - > path ;
2018-06-21 09:38:01 +03:00
argv [ 1 ] = " -P " ;
argv [ 2 ] = talloc_asprintf ( argv , " %d " , ctdb - > ctdbd_pid ) ;
2018-08-27 07:47:38 +03:00
argv [ 3 ] = " -S " ;
argv [ 4 ] = talloc_asprintf ( argv , " %d " , fd [ 1 ] ) ;
argv [ 5 ] = NULL ;
2016-08-27 10:26:28 +03:00
2018-08-27 07:47:38 +03:00
if ( argv [ 2 ] = = NULL | | argv [ 4 ] = = NULL ) {
2018-08-27 07:44:24 +03:00
close ( fd [ 0 ] ) ;
close ( fd [ 1 ] ) ;
2016-08-27 10:26:28 +03:00
talloc_free ( argv ) ;
return - 1 ;
}
2018-09-03 09:12:16 +03:00
D_NOTICE ( " Starting event daemon %s %s %s %s %s \n " ,
argv [ 0 ] ,
argv [ 1 ] ,
argv [ 2 ] ,
argv [ 3 ] ,
argv [ 4 ] ) ;
2016-08-27 10:26:28 +03:00
pid = ctdb_fork ( ctdb ) ;
if ( pid = = - 1 ) {
close ( fd [ 0 ] ) ;
close ( fd [ 1 ] ) ;
2018-08-27 08:28:47 +03:00
talloc_free ( argv ) ;
2016-08-27 10:26:28 +03:00
return - 1 ;
}
if ( pid = = 0 ) {
close ( fd [ 0 ] ) ;
ret = execv ( argv [ 0 ] , discard_const ( argv ) ) ;
if ( ret = = - 1 ) {
_exit ( errno ) ;
}
_exit ( 0 ) ;
}
talloc_free ( argv ) ;
close ( fd [ 1 ] ) ;
2018-08-27 07:47:38 +03:00
ret = wait_for_daemon_startup ( ctdb - > ev , fd [ 0 ] ) ;
if ( ret ! = 0 ) {
ctdb_kill ( ctdb , pid , SIGKILL ) ;
close ( fd [ 0 ] ) ;
D_ERR ( " Failed to initialize event daemon (%d) \n " , ret ) ;
return - 1 ;
}
2016-08-27 10:26:28 +03:00
ectx - > eventd_fde = tevent_add_fd ( ctdb - > ev , ectx , fd [ 0 ] ,
TEVENT_FD_READ ,
eventd_dead_handler , ectx ) ;
if ( ectx - > eventd_fde = = NULL ) {
ctdb_kill ( ctdb , pid , SIGKILL ) ;
close ( fd [ 0 ] ) ;
return - 1 ;
}
tevent_fd_set_auto_close ( ectx - > eventd_fde ) ;
ectx - > eventd_pid = pid ;
2018-08-27 07:53:37 +03:00
status = eventd_client_connect ( ectx ) ;
2016-08-27 10:26:28 +03:00
if ( ! status ) {
2018-08-27 07:53:37 +03:00
DEBUG ( DEBUG_ERR , ( " Failed to connect to event daemon \n " ) ) ;
2016-08-27 10:26:28 +03:00
ctdb_stop_eventd ( ctdb ) ;
return - 1 ;
}
return 0 ;
}
static void eventd_dead_handler ( struct tevent_context * ev ,
struct tevent_fd * fde , uint16_t flags ,
void * private_data )
{
2018-10-11 03:26:06 +03:00
D_ERR ( " Eventd went away - exiting \n " ) ;
exit ( 1 ) ;
2016-08-27 10:26:28 +03:00
}
void ctdb_stop_eventd ( struct ctdb_context * ctdb )
{
struct eventd_context * ectx = ctdb - > ectx ;
if ( ectx = = NULL ) {
return ;
}
TALLOC_FREE ( ectx - > eventd_fde ) ;
if ( ectx - > eventd_pid ! = - 1 ) {
kill ( ectx - > eventd_pid , SIGTERM ) ;
ectx - > eventd_pid = - 1 ;
}
TALLOC_FREE ( ctdb - > ectx ) ;
}
/*
* Connect to event daemon
*/
struct eventd_client_state {
struct eventd_client_state * prev , * next ;
struct eventd_context * ectx ;
void ( * callback ) ( struct ctdb_event_reply * reply , void * private_data ) ;
void * private_data ;
uint32_t reqid ;
uint8_t * buf ;
size_t buflen ;
} ;
static void eventd_client_read ( uint8_t * buf , size_t buflen ,
void * private_data ) ;
static int eventd_client_state_destructor ( struct eventd_client_state * state ) ;
static bool eventd_client_connect ( struct eventd_context * ectx )
{
int fd ;
if ( ectx - > queue ! = NULL ) {
return true ;
}
fd = sock_connect ( ectx - > socket ) ;
if ( fd = = - 1 ) {
return false ;
}
ectx - > queue = sock_queue_setup ( ectx , ectx - > ev , fd ,
eventd_client_read , ectx ) ;
if ( ectx - > queue = = NULL ) {
close ( fd ) ;
return false ;
}
return true ;
}
static int eventd_client_write ( struct eventd_context * ectx ,
TALLOC_CTX * mem_ctx ,
struct ctdb_event_request * request ,
void ( * callback ) ( struct ctdb_event_reply * reply ,
void * private_data ) ,
void * private_data )
{
2018-10-17 09:19:06 +03:00
struct ctdb_event_header header = { 0 } ;
2016-08-27 10:26:28 +03:00
struct eventd_client_state * state ;
int ret ;
if ( ! eventd_client_connect ( ectx ) ) {
return - 1 ;
}
state = talloc_zero ( mem_ctx , struct eventd_client_state ) ;
if ( state = = NULL ) {
return - 1 ;
}
state - > ectx = ectx ;
state - > callback = callback ;
state - > private_data = private_data ;
state - > reqid = reqid_new ( ectx - > idr , state ) ;
if ( state - > reqid = = REQID_INVALID ) {
talloc_free ( state ) ;
return - 1 ;
}
talloc_set_destructor ( state , eventd_client_state_destructor ) ;
2018-06-21 10:16:07 +03:00
header . reqid = state - > reqid ;
2017-07-10 11:35:12 +03:00
2018-06-21 10:16:07 +03:00
state - > buflen = ctdb_event_request_len ( & header , request ) ;
2016-08-27 10:26:28 +03:00
state - > buf = talloc_size ( state , state - > buflen ) ;
if ( state - > buf = = NULL ) {
talloc_free ( state ) ;
return - 1 ;
}
2018-06-21 10:16:07 +03:00
ret = ctdb_event_request_push ( & header ,
request ,
state - > buf ,
& state - > buflen ) ;
2016-08-27 10:26:28 +03:00
if ( ret ! = 0 ) {
talloc_free ( state ) ;
return - 1 ;
}
ret = sock_queue_write ( ectx - > queue , state - > buf , state - > buflen ) ;
if ( ret ! = 0 ) {
talloc_free ( state ) ;
return - 1 ;
}
DLIST_ADD ( ectx - > calls , state ) ;
return 0 ;
}
static int eventd_client_state_destructor ( struct eventd_client_state * state )
{
struct eventd_context * ectx = state - > ectx ;
reqid_remove ( ectx - > idr , state - > reqid ) ;
DLIST_REMOVE ( ectx - > calls , state ) ;
return 0 ;
}
static void eventd_client_read ( uint8_t * buf , size_t buflen ,
void * private_data )
{
struct eventd_context * ectx = talloc_get_type_abort (
private_data , struct eventd_context ) ;
struct eventd_client_state * state ;
2018-06-21 10:16:07 +03:00
struct ctdb_event_header header ;
2016-08-27 10:26:28 +03:00
struct ctdb_event_reply * reply ;
int ret ;
if ( buf = = NULL ) {
/* connection lost */
TALLOC_FREE ( ectx - > queue ) ;
return ;
}
2018-06-21 10:16:07 +03:00
ret = ctdb_event_reply_pull ( buf , buflen , & header , ectx , & reply ) ;
2016-08-27 10:26:28 +03:00
if ( ret ! = 0 ) {
D_ERR ( " Invalid packet received, ret=%d \n " , ret ) ;
return ;
}
2018-06-21 10:16:07 +03:00
if ( buflen ! = header . length ) {
2016-08-27 10:26:28 +03:00
D_ERR ( " Packet size mismatch %zu != % " PRIu32 " \n " ,
2018-06-21 10:16:07 +03:00
buflen , header . length ) ;
2016-08-27 10:26:28 +03:00
talloc_free ( reply ) ;
return ;
}
2018-06-21 10:16:07 +03:00
state = reqid_find ( ectx - > idr , header . reqid ,
2016-08-27 10:26:28 +03:00
struct eventd_client_state ) ;
if ( state = = NULL ) {
talloc_free ( reply ) ;
return ;
}
2018-06-21 10:16:07 +03:00
if ( state - > reqid ! = header . reqid ) {
2016-08-27 10:26:28 +03:00
talloc_free ( reply ) ;
return ;
}
state = talloc_steal ( reply , state ) ;
state - > callback ( reply , state - > private_data ) ;
talloc_free ( reply ) ;
}
/*
* Run an event
*/
struct eventd_client_run_state {
struct eventd_context * ectx ;
void ( * callback ) ( int result , void * private_data ) ;
void * private_data ;
} ;
static void eventd_client_run_done ( struct ctdb_event_reply * reply ,
void * private_data ) ;
static int eventd_client_run ( struct eventd_context * ectx ,
TALLOC_CTX * mem_ctx ,
void ( * callback ) ( int result ,
void * private_data ) ,
void * private_data ,
enum ctdb_event event ,
const char * arg_str ,
uint32_t timeout )
{
struct eventd_client_run_state * state ;
struct ctdb_event_request request ;
struct ctdb_event_request_run rdata ;
int ret ;
state = talloc_zero ( mem_ctx , struct eventd_client_run_state ) ;
if ( state = = NULL ) {
return - 1 ;
}
state - > ectx = ectx ;
state - > callback = callback ;
state - > private_data = private_data ;
2018-06-21 10:16:07 +03:00
rdata . component = " legacy " ;
rdata . event = ctdb_event_to_string ( event ) ;
rdata . args = arg_str ;
2016-08-27 10:26:28 +03:00
rdata . timeout = timeout ;
2018-06-21 10:16:07 +03:00
rdata . flags = 0 ;
2016-08-27 10:26:28 +03:00
2018-06-21 10:16:07 +03:00
request . cmd = CTDB_EVENT_CMD_RUN ;
request . data . run = & rdata ;
2016-08-27 10:26:28 +03:00
ret = eventd_client_write ( ectx , state , & request ,
eventd_client_run_done , state ) ;
if ( ret ! = 0 ) {
talloc_free ( state ) ;
return ret ;
}
return 0 ;
}
static void eventd_client_run_done ( struct ctdb_event_reply * reply ,
void * private_data )
{
struct eventd_client_run_state * state = talloc_get_type_abort (
private_data , struct eventd_client_run_state ) ;
state = talloc_steal ( state - > ectx , state ) ;
2018-06-21 10:16:07 +03:00
state - > callback ( reply - > result , state - > private_data ) ;
2016-08-27 10:26:28 +03:00
talloc_free ( state ) ;
}
/*
* CTDB event script functions
*/
int ctdb_event_script_run ( struct ctdb_context * ctdb ,
TALLOC_CTX * mem_ctx ,
void ( * callback ) ( struct ctdb_context * ctdb ,
int result , void * private_data ) ,
void * private_data ,
enum ctdb_event event ,
const char * fmt , va_list ap )
PRINTF_ATTRIBUTE ( 6 , 0 ) ;
struct ctdb_event_script_run_state {
struct ctdb_context * ctdb ;
void ( * callback ) ( struct ctdb_context * ctdb , int result ,
void * private_data ) ;
void * private_data ;
enum ctdb_event event ;
} ;
static bool event_allowed_during_recovery ( enum ctdb_event event ) ;
static void ctdb_event_script_run_done ( int result , void * private_data ) ;
static bool check_options ( enum ctdb_event call , const char * options ) ;
int ctdb_event_script_run ( struct ctdb_context * ctdb ,
TALLOC_CTX * mem_ctx ,
void ( * callback ) ( struct ctdb_context * ctdb ,
int result , void * private_data ) ,
void * private_data ,
enum ctdb_event event ,
const char * fmt , va_list ap )
{
struct ctdb_event_script_run_state * state ;
char * arg_str ;
int ret ;
if ( ( ctdb - > recovery_mode ! = CTDB_RECOVERY_NORMAL ) & &
( ! event_allowed_during_recovery ( event ) ) ) {
DEBUG ( DEBUG_ERR ,
( " Refusing to run event '%s' while in recovery \n " ,
2024-02-10 07:34:03 +03:00
ctdb_event_to_string ( event ) ) ) ;
2018-10-10 05:35:00 +03:00
return - 1 ;
2016-08-27 10:26:28 +03:00
}
state = talloc_zero ( mem_ctx , struct ctdb_event_script_run_state ) ;
if ( state = = NULL ) {
return - 1 ;
}
state - > ctdb = ctdb ;
state - > callback = callback ;
state - > private_data = private_data ;
state - > event = event ;
if ( fmt ! = NULL ) {
arg_str = talloc_vasprintf ( state , fmt , ap ) ;
if ( arg_str = = NULL ) {
talloc_free ( state ) ;
return - 1 ;
}
} else {
arg_str = NULL ;
}
if ( ! check_options ( event , arg_str ) ) {
DEBUG ( DEBUG_ERR ,
( " Bad event script arguments '%s' for '%s' \n " ,
2024-02-10 07:34:03 +03:00
arg_str , ctdb_event_to_string ( event ) ) ) ;
2016-08-27 10:26:28 +03:00
talloc_free ( arg_str ) ;
return - 1 ;
}
ret = eventd_client_run ( ctdb - > ectx , state ,
ctdb_event_script_run_done , state ,
event , arg_str , ctdb - > tunable . script_timeout ) ;
if ( ret ! = 0 ) {
talloc_free ( state ) ;
return ret ;
}
DEBUG ( DEBUG_INFO ,
( __location__ " Running event %s with arguments %s \n " ,
2024-02-10 07:34:03 +03:00
ctdb_event_to_string ( event ) , arg_str ) ) ;
2016-08-27 10:26:28 +03:00
talloc_free ( arg_str ) ;
return 0 ;
}
static void ctdb_event_script_run_done ( int result , void * private_data )
{
struct ctdb_event_script_run_state * state = talloc_get_type_abort (
private_data , struct ctdb_event_script_run_state ) ;
2018-07-10 11:18:33 +03:00
if ( result = = ETIMEDOUT ) {
2016-08-27 10:26:28 +03:00
switch ( state - > event ) {
case CTDB_EVENT_START_RECOVERY :
case CTDB_EVENT_RECOVERED :
case CTDB_EVENT_TAKE_IP :
case CTDB_EVENT_RELEASE_IP :
DEBUG ( DEBUG_ERR ,
( " Ignoring hung script for %s event \n " ,
2024-02-10 07:34:03 +03:00
ctdb_event_to_string ( state - > event ) ) ) ;
2016-08-27 10:26:28 +03:00
result = 0 ;
break ;
default :
break ;
}
}
2016-09-16 13:06:07 +03:00
state = talloc_steal ( state - > ctdb , state ) ;
2016-08-27 10:26:28 +03:00
state - > callback ( state - > ctdb , result , state - > private_data ) ;
talloc_free ( state ) ;
}
2007-08-20 05:10:30 +04:00
2015-10-26 08:50:46 +03:00
2016-09-16 13:06:07 +03:00
static unsigned int count_words ( const char * options )
2014-01-16 06:05:58 +04:00
{
2016-09-16 13:06:07 +03:00
unsigned int words = 0 ;
2014-01-16 06:05:58 +04:00
2016-09-16 13:06:07 +03:00
if ( options = = NULL ) {
2014-01-16 06:05:58 +04:00
return 0 ;
}
2009-11-24 03:53:13 +03:00
options + = strspn ( options , " \t " ) ;
while ( * options ) {
words + + ;
options + = strcspn ( options , " \t " ) ;
options + = strspn ( options , " \t " ) ;
}
return words ;
}
2015-10-28 08:54:10 +03:00
static bool check_options ( enum ctdb_event call , const char * options )
2009-11-24 03:53:13 +03:00
{
switch ( call ) {
/* These all take no arguments. */
2010-01-19 12:07:14 +03:00
case CTDB_EVENT_INIT :
2010-02-12 13:24:08 +03:00
case CTDB_EVENT_SETUP :
2009-11-24 03:53:13 +03:00
case CTDB_EVENT_STARTUP :
case CTDB_EVENT_START_RECOVERY :
case CTDB_EVENT_RECOVERED :
case CTDB_EVENT_MONITOR :
case CTDB_EVENT_SHUTDOWN :
2010-08-30 12:08:38 +04:00
case CTDB_EVENT_IPREALLOCATED :
2023-10-25 14:29:33 +03:00
case CTDB_EVENT_START_IPREALLOCATE :
2009-11-24 03:53:13 +03:00
return count_words ( options ) = = 0 ;
case CTDB_EVENT_TAKE_IP : /* interface, IP address, netmask bits. */
case CTDB_EVENT_RELEASE_IP :
return count_words ( options ) = = 3 ;
2009-12-21 10:33:55 +03:00
case CTDB_EVENT_UPDATE_IP : /* old interface, new interface, IP address, netmask bits. */
return count_words ( options ) = = 4 ;
2009-11-24 03:53:13 +03:00
default :
2015-10-28 08:54:10 +03:00
DEBUG ( DEBUG_ERR , ( __location__ " Unknown ctdb_event %u \n " , call ) ) ;
2009-11-24 03:53:13 +03:00
return false ;
}
}
2016-09-16 11:44:37 +03:00
/* only specific events are allowed while in recovery */
static bool event_allowed_during_recovery ( enum ctdb_event event )
{
const enum ctdb_event allowed_events [ ] = {
CTDB_EVENT_INIT ,
CTDB_EVENT_SETUP ,
CTDB_EVENT_START_RECOVERY ,
CTDB_EVENT_SHUTDOWN ,
CTDB_EVENT_RELEASE_IP ,
CTDB_EVENT_IPREALLOCATED ,
2023-10-25 14:29:33 +03:00
CTDB_EVENT_START_IPREALLOCATE ,
2016-09-16 11:44:37 +03:00
} ;
2019-06-07 23:38:56 +03:00
size_t i ;
2016-09-16 11:44:37 +03:00
for ( i = 0 ; i < ARRAY_SIZE ( allowed_events ) ; i + + ) {
if ( event = = allowed_events [ i ] ) {
return true ;
}
}
return false ;
}
2007-07-19 07:36:00 +04:00
/*
2015-03-13 17:01:25 +03:00
run the event script in the background , calling the callback when
2011-07-25 12:26:06 +04:00
finished . If mem_ctx is freed , callback will never be called .
2007-07-19 07:36:00 +04:00
*/
2015-03-13 17:01:25 +03:00
int ctdb_event_script_callback ( struct ctdb_context * ctdb ,
2007-07-19 07:36:00 +04:00
TALLOC_CTX * mem_ctx ,
void ( * callback ) ( struct ctdb_context * , int , void * ) ,
void * private_data ,
2015-10-28 08:54:10 +03:00
enum ctdb_event call ,
2007-07-19 07:36:00 +04:00
const char * fmt , . . . )
{
va_list ap ;
int ret ;
va_start ( ap , fmt ) ;
2016-09-16 13:06:07 +03:00
ret = ctdb_event_script_run ( ctdb , mem_ctx , callback , private_data ,
call , fmt , ap ) ;
2007-07-19 07:36:00 +04:00
va_end ( ap ) ;
return ret ;
}
2016-09-16 13:06:07 +03:00
struct ctdb_event_script_args_state {
2007-07-19 07:36:00 +04:00
bool done ;
int status ;
} ;
2016-09-16 13:06:07 +03:00
static void ctdb_event_script_args_done ( struct ctdb_context * ctdb ,
int status , void * private_data )
2007-07-19 07:36:00 +04:00
{
2016-09-16 13:06:07 +03:00
struct ctdb_event_script_args_state * s =
( struct ctdb_event_script_args_state * ) private_data ;
2007-07-19 07:36:00 +04:00
s - > done = true ;
s - > status = status ;
}
/*
2009-11-23 23:40:51 +03:00
run the event script , waiting for it to complete . Used when the caller
doesn ' t want to continue till the event script has finished .
2007-07-19 07:36:00 +04:00
*/
2015-10-28 08:54:10 +03:00
int ctdb_event_script_args ( struct ctdb_context * ctdb , enum ctdb_event call ,
2009-11-24 03:46:49 +03:00
const char * fmt , . . . )
2007-07-19 07:36:00 +04:00
{
va_list ap ;
int ret ;
2016-09-16 13:06:07 +03:00
struct ctdb_event_script_args_state state = {
2015-03-18 12:46:46 +03:00
. status = - 1 ,
. done = false ,
} ;
2007-07-19 07:36:00 +04:00
va_start ( ap , fmt ) ;
2016-09-16 13:06:07 +03:00
ret = ctdb_event_script_run ( ctdb , ctdb ,
ctdb_event_script_args_done , & state ,
call , fmt , ap ) ;
2011-08-10 19:53:56 +04:00
va_end ( ap ) ;
2007-07-19 07:36:00 +04:00
if ( ret ! = 0 ) {
return ret ;
}
2016-09-16 13:06:07 +03:00
while ( ! state . done ) {
tevent_loop_once ( ctdb - > ev ) ;
}
2013-07-02 06:40:37 +04:00
2018-07-10 11:18:33 +03:00
if ( state . status = = ETIMEDOUT ) {
2013-07-02 06:40:37 +04:00
/* Don't ban self if CTDB is starting up or shutting down */
if ( call ! = CTDB_EVENT_INIT & & call ! = CTDB_EVENT_SHUTDOWN ) {
2016-09-16 13:06:07 +03:00
DEBUG ( DEBUG_ERR ,
( __location__ " eventscript for '%s' timed out. "
" Immediately banning ourself for %d seconds \n " ,
2024-02-10 07:34:03 +03:00
ctdb_event_to_string ( call ) ,
2016-09-16 13:06:07 +03:00
ctdb - > tunable . recovery_ban_period ) ) ;
2013-07-02 06:40:37 +04:00
ctdb_ban_self ( ctdb ) ;
}
2009-12-07 16:18:57 +03:00
}
2016-09-16 13:06:07 +03:00
return state . status ;
2007-07-19 07:36:00 +04:00
}
2008-04-02 04:13:30 +04:00
2015-10-28 08:54:10 +03:00
int ctdb_event_script ( struct ctdb_context * ctdb , enum ctdb_event call )
2009-11-24 03:46:49 +03:00
{
/* GCC complains about empty format string, so use %s and "". */
2016-09-16 13:06:07 +03:00
return ctdb_event_script_args ( ctdb , call , NULL ) ;
2009-11-24 03:46:49 +03:00
}
2021-09-30 14:10:33 +03:00
void ctdb_event_reopen_logs ( struct ctdb_context * ctdb )
{
if ( ctdb - > ectx - > eventd_pid > 0 ) {
kill ( ctdb - > ectx - > eventd_pid , SIGHUP ) ;
}
}