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 .
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 .
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
*/
# include "includes.h"
2009-10-23 06:55:21 +04:00
# include <time.h>
2007-07-04 11:45:46 +04:00
# include "system/filesys.h"
# include "system/wait.h"
2007-08-20 05:10:30 +04:00
# include "system/dir.h"
# include "system/locale.h"
2007-07-04 11:45:46 +04:00
# include "../include/ctdb_private.h"
# include "lib/events/events.h"
2007-08-15 08:44:03 +04:00
# include "../common/rb_tree.h"
2007-08-20 05:10:30 +04:00
static struct {
struct timeval start ;
const char * script_running ;
} child_state ;
2009-10-28 08:11:54 +03:00
static void ctdb_event_script_timeout ( struct event_context * ev , struct timed_event * te , struct timeval t , void * p ) ;
2007-08-20 05:10:30 +04:00
/*
ctdbd sends us a SIGTERM when we should time out the current script
*/
static void sigterm ( int sig )
{
2009-10-23 06:55:21 +04:00
char tbuf [ 100 ] , buf [ 200 ] ;
time_t t ;
2009-10-14 07:14:28 +04:00
DEBUG ( DEBUG_ERR , ( " Timed out running script '%s' after %.1f seconds pid :%d \n " ,
child_state . script_running , timeval_elapsed ( & child_state . start ) , getpid ( ) ) ) ;
2009-10-23 06:55:21 +04:00
t = time ( NULL ) ;
2009-10-14 07:14:28 +04:00
2009-10-23 06:55:21 +04:00
strftime ( tbuf , sizeof ( tbuf ) - 1 , " %Y%m%d%H%M%S " , localtime ( & t ) ) ;
2009-12-07 18:02:36 +03:00
sprintf ( buf , " { pstree -p; cat /proc/locks; ls -li /var/ctdb/ /var/ctdb/persistent; } "
" >/tmp/ctdb.event.%s.%d " , tbuf , getpid ( ) ) ;
2009-10-23 06:55:21 +04:00
system ( buf ) ;
DEBUG ( DEBUG_ERR , ( " Logged timedout eventscript : %s \n " , buf ) ) ;
2009-10-14 07:14:28 +04:00
2007-08-20 05:10:30 +04:00
/* all the child processes will be running in the same process group */
kill ( - getpgrp ( ) , SIGKILL ) ;
2009-10-27 05:51:45 +03:00
_exit ( 1 ) ;
2007-08-20 05:10:30 +04:00
}
2007-07-04 11:45:46 +04:00
2008-06-13 06:18:00 +04:00
struct ctdb_event_script_state {
struct ctdb_context * ctdb ;
pid_t child ;
2009-11-24 03:36:53 +03:00
/* Warning: this can free us! */
2008-06-13 06:18:00 +04:00
void ( * callback ) ( struct ctdb_context * , int , void * ) ;
int fd [ 2 ] ;
void * private_data ;
2009-12-07 16:45:18 +03:00
bool from_user ;
2009-11-24 03:49:58 +03:00
enum ctdb_eventscript_call call ;
2008-06-13 06:18:00 +04:00
const char * options ;
2009-10-28 08:11:54 +03:00
struct timeval timeout ;
2009-12-07 16:44:01 +03:00
2009-12-08 05:17:05 +03:00
unsigned int current ;
2009-12-08 05:18:17 +03:00
struct ctdb_scripts_wire * scripts ;
2008-06-13 06:18:00 +04:00
} ;
2009-12-08 05:18:17 +03:00
static struct ctdb_script_wire * get_current_script ( struct ctdb_event_script_state * state )
2009-12-08 05:17:24 +03:00
{
return & state - > scripts - > scripts [ state - > current ] ;
}
2009-03-23 11:07:45 +03:00
/* called from ctdb_logging when we have received output on STDERR from
* one of the eventscripts
*/
2009-12-08 05:14:30 +03:00
static void log_event_script_output ( const char * str , uint16_t len , void * p )
2009-03-23 11:07:45 +03:00
{
2009-12-08 05:17:05 +03:00
struct ctdb_event_script_state * state
= talloc_get_type ( p , struct ctdb_event_script_state ) ;
2009-12-18 06:43:09 +03:00
struct ctdb_script_wire * current ;
2009-12-08 05:18:17 +03:00
unsigned int slen , min ;
2009-03-23 11:07:45 +03:00
2009-12-18 06:43:09 +03:00
/* We may have been aborted to run something else. Discard */
if ( state - > scripts = = NULL ) {
return ;
}
current = get_current_script ( state ) ;
2009-12-08 05:18:17 +03:00
/* Append, but don't overfill buffer. It starts zero-filled. */
slen = strlen ( current - > output ) ;
min = MIN ( len , sizeof ( current - > output ) - slen - 1 ) ;
2009-12-08 05:15:17 +03:00
2009-12-08 05:18:17 +03:00
memcpy ( current - > output + slen , str , min ) ;
2009-03-23 11:07:45 +03:00
}
2009-12-07 18:20:55 +03:00
int32_t ctdb_control_get_event_script_status ( struct ctdb_context * ctdb ,
uint32_t call_type ,
TDB_DATA * outdata )
2009-12-07 15:39:40 +03:00
{
2009-12-07 18:20:55 +03:00
if ( call_type > = CTDB_EVENT_MAX ) {
2009-03-23 11:07:45 +03:00
return - 1 ;
}
2009-12-07 18:20:55 +03:00
if ( ctdb - > last_status [ call_type ] = = NULL ) {
/* If it's never been run, return nothing so they can tell. */
outdata - > dsize = 0 ;
} else {
outdata - > dsize = talloc_get_size ( ctdb - > last_status [ call_type ] ) ;
outdata - > dptr = ( uint8_t * ) ctdb - > last_status [ call_type ] ;
}
2009-03-23 11:07:45 +03:00
return 0 ;
}
2009-08-13 07:04:08 +04:00
struct ctdb_script_tree_item {
const char * name ;
2009-12-07 15:42:19 +03:00
int error ;
2009-08-13 07:04:08 +04:00
} ;
2009-12-07 15:39:39 +03:00
/* Return true if OK, otherwise set errno. */
static bool check_executable ( const char * dir , const char * name )
{
char * full ;
struct stat st ;
full = talloc_asprintf ( NULL , " %s/%s " , dir , name ) ;
if ( ! full )
return false ;
if ( stat ( full , & st ) ! = 0 ) {
DEBUG ( DEBUG_ERR , ( " Could not stat event script %s: %s \n " ,
full , strerror ( errno ) ) ) ;
talloc_free ( full ) ;
return false ;
}
if ( ! ( st . st_mode & S_IXUSR ) ) {
2010-02-11 04:00:43 +03:00
DEBUG ( DEBUG_DEBUG , ( " Event script %s is not executable. Ignoring this event script \n " , full ) ) ;
2009-12-07 15:39:39 +03:00
errno = ENOEXEC ;
talloc_free ( full ) ;
return false ;
}
talloc_free ( full ) ;
return true ;
}
2009-12-08 05:18:17 +03:00
static struct ctdb_scripts_wire * ctdb_get_script_list ( struct ctdb_context * ctdb , TALLOC_CTX * mem_ctx )
2007-07-04 11:45:46 +04:00
{
2007-08-15 08:44:03 +04:00
DIR * dir ;
struct dirent * de ;
2009-08-13 07:04:08 +04:00
struct stat st ;
trbt_tree_t * tree ;
2009-12-08 05:18:17 +03:00
struct ctdb_scripts_wire * scripts ;
2009-08-13 07:04:08 +04:00
TALLOC_CTX * tmp_ctx = talloc_new ( ctdb ) ;
struct ctdb_script_tree_item * tree_item ;
2008-10-16 10:57:50 +04:00
int count ;
2007-08-20 05:10:30 +04:00
2007-08-15 08:44:03 +04:00
/*
the service specific event scripts
*/
2007-09-04 03:50:07 +04:00
if ( stat ( ctdb - > event_script_dir , & st ) ! = 0 & &
2007-07-04 11:45:46 +04:00
errno = = ENOENT ) {
2008-02-04 12:07:15 +03:00
DEBUG ( DEBUG_CRIT , ( " No event script directory found at '%s' \n " , ctdb - > event_script_dir ) ) ;
2007-08-15 08:44:03 +04:00
talloc_free ( tmp_ctx ) ;
2009-08-13 07:04:08 +04:00
return NULL ;
2007-08-15 08:44:03 +04:00
}
/* create a tree to store all the script names in */
tree = trbt_create ( tmp_ctx , 0 ) ;
/* scan all directory entries and insert all valid scripts into the
tree
*/
2007-09-04 03:50:07 +04:00
dir = opendir ( ctdb - > event_script_dir ) ;
2007-08-15 08:44:03 +04:00
if ( dir = = NULL ) {
2008-02-04 12:07:15 +03:00
DEBUG ( DEBUG_CRIT , ( " Failed to open event script directory '%s' \n " , ctdb - > event_script_dir ) ) ;
2007-08-15 08:44:03 +04:00
talloc_free ( tmp_ctx ) ;
2009-08-13 07:04:08 +04:00
return NULL ;
2007-07-04 11:45:46 +04:00
}
2008-10-16 10:57:50 +04:00
count = 0 ;
2007-08-15 08:44:03 +04:00
while ( ( de = readdir ( dir ) ) ! = NULL ) {
int namlen ;
2007-08-20 05:10:30 +04:00
unsigned num ;
2007-08-15 08:44:03 +04:00
namlen = strlen ( de - > d_name ) ;
2007-07-04 11:45:46 +04:00
2007-08-15 08:44:03 +04:00
if ( namlen < 3 ) {
continue ;
}
2007-07-04 11:45:46 +04:00
2007-08-15 08:44:03 +04:00
if ( de - > d_name [ namlen - 1 ] = = ' ~ ' ) {
/* skip files emacs left behind */
continue ;
}
2007-07-04 11:45:46 +04:00
2007-08-15 08:44:03 +04:00
if ( de - > d_name [ 2 ] ! = ' . ' ) {
continue ;
}
2007-08-20 05:10:30 +04:00
if ( sscanf ( de - > d_name , " %02u. " , & num ) ! = 1 ) {
2007-08-15 08:44:03 +04:00
continue ;
}
2007-08-21 03:22:14 +04:00
2009-12-08 05:18:17 +03:00
if ( strlen ( de - > d_name ) > MAX_SCRIPT_NAME ) {
DEBUG ( DEBUG_ERR , ( " Script name %s too long! %u chars max " ,
de - > d_name , MAX_SCRIPT_NAME ) ) ;
continue ;
}
2009-08-13 07:04:08 +04:00
tree_item = talloc ( tree , struct ctdb_script_tree_item ) ;
if ( tree_item = = NULL ) {
DEBUG ( DEBUG_ERR , ( __location__ " Failed to allocate new tree item \n " ) ) ;
talloc_free ( tmp_ctx ) ;
return NULL ;
}
2009-12-07 15:42:19 +03:00
tree_item - > error = 0 ;
2009-12-07 15:39:39 +03:00
if ( ! check_executable ( ctdb - > event_script_dir , de - > d_name ) ) {
2009-12-07 15:42:19 +03:00
tree_item - > error = errno ;
2007-08-21 03:22:14 +04:00
}
2009-08-13 07:04:08 +04:00
tree_item - > name = talloc_strdup ( tree_item , de - > d_name ) ;
if ( tree_item - > name = = NULL ) {
DEBUG ( DEBUG_ERR , ( __location__ " Failed to allocate script name. \n " ) ) ;
talloc_free ( tmp_ctx ) ;
return NULL ;
}
2008-10-16 10:57:50 +04:00
/* store the event script in the tree */
2009-08-13 07:04:08 +04:00
trbt_insert32 ( tree , ( num < < 16 ) | count + + , tree_item ) ;
2007-07-04 11:45:46 +04:00
}
2007-08-15 08:44:03 +04:00
closedir ( dir ) ;
2009-12-08 05:17:05 +03:00
/* Overallocates by one, but that's OK */
2009-12-08 05:18:17 +03:00
scripts = talloc_zero_size ( tmp_ctx ,
sizeof ( * scripts )
+ sizeof ( scripts - > scripts [ 0 ] ) * count ) ;
2009-12-08 05:17:05 +03:00
if ( scripts = = NULL ) {
DEBUG ( DEBUG_ERR , ( __location__ " Failed to allocate scripts \n " ) ) ;
talloc_free ( tmp_ctx ) ;
return NULL ;
}
scripts - > num_scripts = count ;
2009-08-13 07:04:08 +04:00
2009-12-08 05:17:05 +03:00
for ( count = 0 ; count < scripts - > num_scripts ; count + + ) {
tree_item = trbt_findfirstarray32 ( tree , 1 ) ;
2009-08-13 07:04:08 +04:00
2009-12-08 05:18:17 +03:00
strcpy ( scripts - > scripts [ count ] . name , tree_item - > name ) ;
2009-12-08 05:17:05 +03:00
scripts - > scripts [ count ] . status = - tree_item - > error ;
2009-12-08 05:18:17 +03:00
2009-08-13 07:04:08 +04:00
/* remove this script from the tree */
talloc_free ( tree_item ) ;
2009-12-08 05:17:05 +03:00
}
2009-08-13 07:04:08 +04:00
2009-12-08 05:17:05 +03:00
talloc_steal ( mem_ctx , scripts ) ;
2009-08-13 07:04:08 +04:00
talloc_free ( tmp_ctx ) ;
2009-12-08 05:17:05 +03:00
return scripts ;
2009-08-13 07:04:08 +04:00
}
2009-12-07 16:57:40 +03:00
static int child_setup ( struct ctdb_context * ctdb )
2009-08-13 07:04:08 +04:00
{
if ( setpgid ( 0 , 0 ) ! = 0 ) {
2009-12-07 16:25:03 +03:00
int ret = - errno ;
2009-08-13 07:04:08 +04:00
DEBUG ( DEBUG_ERR , ( " Failed to create process group for event scripts - %s \n " ,
strerror ( errno ) ) ) ;
2009-12-07 16:25:03 +03:00
return ret ;
2009-08-13 07:04:08 +04:00
}
signal ( SIGTERM , sigterm ) ;
2009-12-07 16:25:03 +03:00
return 0 ;
}
static char * child_command_string ( struct ctdb_context * ctdb ,
TALLOC_CTX * ctx ,
bool from_user ,
const char * scriptname ,
enum ctdb_eventscript_call call ,
const char * options )
{
const char * str = from_user ? " CTDB_CALLED_BY_USER=1 " : " " ;
/* Allow a setting where we run the actual monitor event
from an external source and replace it with
a " status " event that just picks up the actual
status of the event asynchronously .
*/
if ( ( ctdb - > tunable . use_status_events_for_monitoring ! = 0 )
& & ( call = = CTDB_EVENT_MONITOR )
& & ! from_user ) {
return talloc_asprintf ( ctx , " %s%s/%s %s " ,
str ,
ctdb - > event_script_dir ,
scriptname , " status " ) ;
} else {
return talloc_asprintf ( ctx , " %s%s/%s %s %s " ,
str ,
ctdb - > event_script_dir ,
2009-12-07 18:17:13 +03:00
scriptname ,
ctdb_eventscript_call_names [ call ] ,
options ) ;
2009-12-07 16:25:03 +03:00
}
}
static int child_run_one ( struct ctdb_context * ctdb ,
const char * scriptname , const char * cmdstr )
{
int ret ;
ret = system ( cmdstr ) ;
/* if the system() call was successful, translate ret into the
return code from the command
*/
if ( ret ! = - 1 ) {
ret = WEXITSTATUS ( ret ) ;
} else {
ret = - errno ;
}
/* 127 could mean it does not exist, 126 non-executable. */
if ( ret = = 127 | | ret = = 126 ) {
/* Re-check it... */
if ( ! check_executable ( ctdb - > event_script_dir , scriptname ) ) {
DEBUG ( DEBUG_ERR , ( " Script %s returned status %u. Someone just deleted it? \n " ,
cmdstr , ret ) ) ;
ret = - errno ;
}
}
return ret ;
}
/*
2009-12-07 16:51:25 +03:00
Actually run one event script
2009-12-07 16:25:03 +03:00
this function is called and run in the context of a forked child
which allows it to do blocking calls such as system ( )
*/
2009-12-07 16:51:25 +03:00
static int child_run_script ( struct ctdb_context * ctdb ,
bool from_user ,
enum ctdb_eventscript_call call ,
const char * options ,
2009-12-08 05:18:17 +03:00
struct ctdb_script_wire * current )
2009-12-07 16:25:03 +03:00
{
char * cmdstr ;
int ret ;
TALLOC_CTX * tmp_ctx = talloc_new ( ctdb ) ;
2009-12-07 16:57:40 +03:00
child_state . start = timeval_current ( ) ;
ret = child_setup ( ctdb ) ;
2009-12-07 16:25:03 +03:00
if ( ret ! = 0 )
goto out ;
2009-08-13 07:04:08 +04:00
2009-12-07 16:51:25 +03:00
cmdstr = child_command_string ( ctdb , tmp_ctx , from_user ,
current - > name , call , options ) ;
CTDB_NO_MEMORY ( ctdb , cmdstr ) ;
child_state . script_running = cmdstr ;
2009-08-13 07:04:08 +04:00
2010-02-11 04:00:43 +03:00
DEBUG ( DEBUG_DEBUG , ( " Executing event script %s \n " , cmdstr ) ) ;
2009-03-23 11:07:45 +03:00
2009-12-08 05:15:17 +03:00
if ( current - > status ) {
ret = current - > status ;
2009-12-07 16:51:25 +03:00
goto out ;
2007-08-15 08:44:03 +04:00
}
2007-08-20 05:10:30 +04:00
2009-12-07 16:51:25 +03:00
ret = child_run_one ( ctdb , current - > name , cmdstr ) ;
2009-12-07 15:45:56 +03:00
out :
2007-08-15 08:44:03 +04:00
talloc_free ( tmp_ctx ) ;
2009-12-07 15:43:12 +03:00
return ret ;
2007-07-04 11:45:46 +04:00
}
2009-12-07 16:52:55 +03:00
static void ctdb_event_script_handler ( struct event_context * ev , struct fd_event * fde ,
uint16_t flags , void * p ) ;
static int fork_child_for_script ( struct ctdb_context * ctdb ,
struct ctdb_event_script_state * state )
{
int r ;
2009-12-08 05:18:17 +03:00
struct ctdb_script_wire * current = get_current_script ( state ) ;
2009-12-07 16:52:55 +03:00
2009-12-08 05:17:05 +03:00
current - > start = timeval_current ( ) ;
2009-12-07 16:57:40 +03:00
2009-12-07 16:52:55 +03:00
r = pipe ( state - > fd ) ;
if ( r ! = 0 ) {
DEBUG ( DEBUG_ERR , ( __location__ " pipe failed for child eventscript process \n " ) ) ;
return - errno ;
}
2009-12-08 05:18:17 +03:00
if ( ! ctdb_fork_with_logging ( state , ctdb , log_event_script_output ,
2009-12-08 05:17:05 +03:00
state , & state - > child ) ) {
2009-12-07 16:52:55 +03:00
r = - errno ;
close ( state - > fd [ 0 ] ) ;
close ( state - > fd [ 1 ] ) ;
return r ;
}
2009-12-08 05:14:30 +03:00
/* If we are the child, do the work. */
2009-12-07 16:52:55 +03:00
if ( state - > child = = 0 ) {
int rt ;
close ( state - > fd [ 0 ] ) ;
set_close_on_exec ( state - > fd [ 1 ] ) ;
2009-12-08 05:17:05 +03:00
rt = child_run_script ( ctdb , state - > from_user , state - > call , state - > options , current ) ;
2009-12-07 16:52:55 +03:00
/* We must be able to write PIPEBUF bytes at least; if this
somehow fails , the read above will be short . */
write ( state - > fd [ 1 ] , & rt , sizeof ( rt ) ) ;
close ( state - > fd [ 1 ] ) ;
_exit ( rt ) ;
}
close ( state - > fd [ 1 ] ) ;
set_close_on_exec ( state - > fd [ 0 ] ) ;
DEBUG ( DEBUG_DEBUG , ( __location__ " Created PIPE FD:%d to child eventscript process \n " , state - > fd [ 0 ] ) ) ;
/* Set ourselves up to be called when that's done. */
event_add_fd ( ctdb - > ev , state , state - > fd [ 0 ] , EVENT_FD_READ | EVENT_FD_AUTOCLOSE ,
ctdb_event_script_handler , state ) ;
return 0 ;
}
2009-12-18 06:24:40 +03:00
/*
Summarize status of this run of scripts .
*/
static int script_status ( struct ctdb_scripts_wire * scripts )
{
unsigned int i ;
for ( i = 0 ; i < scripts - > num_scripts ; i + + ) {
switch ( scripts - > scripts [ i ] . status ) {
case - ENOENT :
case - ENOEXEC :
/* Disabled or missing; that's OK. */
break ;
case 0 :
/* No problem. */
break ;
default :
return scripts - > scripts [ i ] . status ;
}
}
/* All OK! */
return 0 ;
}
2007-07-04 11:45:46 +04:00
/* called when child is finished */
static void ctdb_event_script_handler ( struct event_context * ev , struct fd_event * fde ,
uint16_t flags , void * p )
{
struct ctdb_event_script_state * state =
talloc_get_type ( p , struct ctdb_event_script_state ) ;
2009-12-08 05:18:17 +03:00
struct ctdb_script_wire * current = get_current_script ( state ) ;
2007-07-04 11:45:46 +04:00
struct ctdb_context * ctdb = state - > ctdb ;
2009-12-18 06:24:40 +03:00
int r , status ;
2008-07-09 08:02:54 +04:00
2009-12-08 05:17:24 +03:00
r = read ( state - > fd [ 0 ] , & current - > status , sizeof ( current - > status ) ) ;
2009-12-07 15:45:56 +03:00
if ( r < 0 ) {
2009-12-08 05:17:24 +03:00
current - > status = - errno ;
} else if ( r ! = sizeof ( current - > status ) ) {
current - > status = - EIO ;
2009-12-07 16:57:40 +03:00
}
2009-12-08 05:17:24 +03:00
current - > finished = timeval_current ( ) ;
2009-12-15 12:56:16 +03:00
/* valgrind gets overloaded if we run next script as it's still doing
* post - execution analysis , so kill finished child here . */
2009-12-16 13:29:15 +03:00
if ( ctdb - > valgrinding ) {
kill ( state - > child , SIGKILL ) ;
}
2009-11-24 03:36:53 +03:00
state - > child = 0 ;
2009-12-07 16:51:25 +03:00
2009-12-18 06:24:40 +03:00
status = script_status ( state - > scripts ) ;
2009-12-07 16:51:25 +03:00
/* Aborted or finished all scripts? We're done. */
2009-12-18 06:24:40 +03:00
if ( status ! = 0 | | state - > current + 1 = = state - > scripts - > num_scripts ) {
2009-12-07 16:51:25 +03:00
DEBUG ( DEBUG_INFO , ( __location__ " Eventscript %s %s finished with state %d \n " ,
2009-12-18 06:24:40 +03:00
ctdb_eventscript_call_names [ state - > call ] , state - > options , status ) ) ;
2009-12-07 16:51:25 +03:00
ctdb - > event_script_timeouts = 0 ;
talloc_free ( state ) ;
return ;
}
/* Forget about that old fd. */
talloc_free ( fde ) ;
/* Next script! */
2009-12-08 05:17:05 +03:00
state - > current + + ;
2009-12-18 06:24:40 +03:00
current + + ;
current - > status = fork_child_for_script ( ctdb , state ) ;
if ( current - > status ! = 0 ) {
2009-12-07 16:52:55 +03:00
/* This calls the callback. */
talloc_free ( state ) ;
2009-12-07 16:51:25 +03:00
}
2008-06-13 07:18:06 +04:00
}
2007-07-04 11:45:46 +04:00
/* called when child times out */
static void ctdb_event_script_timeout ( struct event_context * ev , struct timed_event * te ,
struct timeval t , void * p )
{
struct ctdb_event_script_state * state = talloc_get_type ( p , struct ctdb_event_script_state ) ;
struct ctdb_context * ctdb = state - > ctdb ;
2009-12-18 06:24:40 +03:00
struct ctdb_script_wire * current = get_current_script ( state ) ;
2009-10-28 08:11:54 +03:00
2009-12-18 06:24:40 +03:00
DEBUG ( DEBUG_ERR , ( " Event script timed out : %s %s %s count : %u pid : %d \n " ,
current - > name , ctdb_eventscript_call_names [ state - > call ] , state - > options , ctdb - > event_script_timeouts , state - > child ) ) ;
2007-07-04 11:45:46 +04:00
2010-02-16 03:18:43 +03:00
/* ignore timeouts for these events */
switch ( state - > call ) {
case CTDB_EVENT_START_RECOVERY :
case CTDB_EVENT_RECOVERED :
case CTDB_EVENT_TAKE_IP :
case CTDB_EVENT_RELEASE_IP :
case CTDB_EVENT_STOPPED :
case CTDB_EVENT_MONITOR :
case CTDB_EVENT_STATUS :
state - > scripts - > scripts [ state - > current ] . status = 0 ;
DEBUG ( DEBUG_ERR , ( " Ignoring hung script for %s call %d \n " , state - > options , state - > call ) ) ;
break ;
default :
state - > scripts - > scripts [ state - > current ] . status = - ETIME ;
}
2009-12-07 16:18:57 +03:00
2009-10-14 07:14:28 +04:00
if ( kill ( state - > child , 0 ) ! = 0 ) {
DEBUG ( DEBUG_ERR , ( " Event script child process already dead, errno %s(%d) \n " , strerror ( errno ) , errno ) ) ;
2009-11-24 03:36:53 +03:00
state - > child = 0 ;
2008-06-13 07:18:06 +04:00
}
2008-07-07 14:38:59 +04:00
2009-11-03 22:01:00 +03:00
talloc_free ( state ) ;
2007-07-04 11:45:46 +04:00
}
/*
2009-11-24 03:36:53 +03:00
destroy an event script : kill it if - > child ! = 0.
2007-07-04 11:45:46 +04:00
*/
static int event_script_destructor ( struct ctdb_event_script_state * state )
{
2009-12-18 06:24:40 +03:00
int status ;
2009-11-24 03:36:53 +03:00
if ( state - > child ) {
DEBUG ( DEBUG_ERR , ( __location__ " Sending SIGTERM to child pid:%d \n " , state - > child ) ) ;
2009-10-14 07:14:28 +04:00
2009-11-24 03:36:53 +03:00
if ( kill ( state - > child , SIGTERM ) ! = 0 ) {
DEBUG ( DEBUG_ERR , ( " Failed to kill child process for eventscript, errno %s(%d) \n " , strerror ( errno ) , errno ) ) ;
}
2009-10-28 08:11:54 +03:00
}
2009-12-07 16:44:01 +03:00
/* If we were the current monitor, we no longer are. */
if ( state - > ctdb - > current_monitor = = state ) {
state - > ctdb - > current_monitor = NULL ;
}
2009-12-18 06:24:40 +03:00
/* Save our scripts as the last executed status, if we have them.
* See ctdb_event_script_callback_v where we abort monitor event . */
2009-12-10 12:55:33 +03:00
if ( state - > scripts ) {
talloc_free ( state - > ctdb - > last_status [ state - > call ] ) ;
state - > ctdb - > last_status [ state - > call ] = state - > scripts ;
if ( state - > current < state - > ctdb - > last_status [ state - > call ] - > num_scripts ) {
state - > ctdb - > last_status [ state - > call ] - > num_scripts = state - > current + 1 ;
}
2009-12-08 04:59:10 +03:00
}
2009-12-18 06:24:40 +03:00
/* Use last status as result, or "OK" if none. */
if ( state - > ctdb - > last_status [ state - > call ] ) {
status = script_status ( state - > ctdb - > last_status [ state - > call ] ) ;
} else {
status = 0 ;
}
2009-11-24 03:36:53 +03:00
/* This is allowed to free us; talloc will prevent double free anyway,
* but beware if you call this outside the destructor ! */
if ( state - > callback ) {
2009-12-18 06:24:40 +03:00
state - > callback ( state - > ctdb , status , state - > private_data ) ;
2009-10-14 07:14:28 +04:00
}
2007-07-04 11:45:46 +04:00
return 0 ;
}
2009-11-24 03:53:13 +03:00
static unsigned int count_words ( const char * options )
{
unsigned int words = 0 ;
options + = strspn ( options , " \t " ) ;
while ( * options ) {
words + + ;
options + = strcspn ( options , " \t " ) ;
options + = strspn ( options , " \t " ) ;
}
return words ;
}
static bool check_options ( enum ctdb_eventscript_call call , const char * options )
{
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_STOPPED :
case CTDB_EVENT_MONITOR :
case CTDB_EVENT_STATUS :
case CTDB_EVENT_SHUTDOWN :
2009-11-27 04:04:02 +03:00
case CTDB_EVENT_RELOAD :
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 :
DEBUG ( DEBUG_ERR , ( __location__ " Unknown ctdb_eventscript_call %u \n " , call ) ) ;
return false ;
}
}
2007-07-04 11:45:46 +04:00
/*
run the event script in the background , calling the callback when
finished
*/
2007-07-19 07:36:00 +04:00
static int ctdb_event_script_callback_v ( struct ctdb_context * ctdb ,
void ( * callback ) ( struct ctdb_context * , int , void * ) ,
void * private_data ,
2009-11-26 07:49:49 +03:00
bool from_user ,
2009-11-24 03:46:49 +03:00
enum ctdb_eventscript_call call ,
2007-07-19 07:36:00 +04:00
const char * fmt , va_list ap )
2007-07-04 11:45:46 +04:00
{
struct ctdb_event_script_state * state ;
2009-12-07 16:39:20 +03:00
state = talloc ( ctdb - > event_script_ctx , struct ctdb_event_script_state ) ;
2009-11-23 23:40:51 +03:00
CTDB_NO_MEMORY ( ctdb , state ) ;
2007-07-04 11:45:46 +04:00
state - > ctdb = ctdb ;
state - > callback = callback ;
state - > private_data = private_data ;
2009-12-07 16:45:18 +03:00
state - > from_user = from_user ;
2009-11-24 03:49:58 +03:00
state - > call = call ;
2008-06-13 06:18:00 +04:00
state - > options = talloc_vasprintf ( state , fmt , ap ) ;
2009-11-24 03:39:46 +03:00
state - > timeout = timeval_set ( ctdb - > tunable . script_timeout , 0 ) ;
2009-12-07 16:44:01 +03:00
state - > scripts = NULL ;
2009-10-28 08:11:54 +03:00
if ( state - > options = = NULL ) {
DEBUG ( DEBUG_ERR , ( __location__ " could not allocate state->options \n " ) ) ;
2009-10-28 09:35:15 +03:00
talloc_free ( state ) ;
2009-10-28 08:11:54 +03:00
return - 1 ;
}
2009-11-24 03:53:13 +03:00
if ( ! check_options ( state - > call , state - > options ) ) {
DEBUG ( DEBUG_ERR , ( " Bad eventscript options '%s' for %s \n " ,
2009-12-07 18:17:13 +03:00
ctdb_eventscript_call_names [ state - > call ] , state - > options ) ) ;
2009-11-24 03:53:13 +03:00
talloc_free ( state ) ;
return - 1 ;
}
2009-09-09 04:57:39 +04:00
2009-12-07 16:23:35 +03:00
if ( ctdb - > recovery_mode ! = CTDB_RECOVERY_NORMAL ) {
/* we guarantee that only some specifically allowed event scripts are run
while in recovery */
const enum ctdb_eventscript_call allowed_calls [ ] = {
2010-01-19 12:07:14 +03:00
CTDB_EVENT_INIT ,
2010-02-12 13:24:08 +03:00
CTDB_EVENT_SETUP ,
2010-01-19 12:07:14 +03:00
CTDB_EVENT_START_RECOVERY ,
CTDB_EVENT_SHUTDOWN ,
CTDB_EVENT_RELEASE_IP ,
CTDB_EVENT_STOPPED
} ;
2009-12-07 16:23:35 +03:00
int i ;
for ( i = 0 ; i < ARRAY_SIZE ( allowed_calls ) ; i + + ) {
if ( call = = allowed_calls [ i ] ) break ;
}
if ( i = = ARRAY_SIZE ( allowed_calls ) ) {
DEBUG ( DEBUG_ERR , ( " Refusing to run event scripts call '%s' while in recovery \n " ,
2009-12-07 18:17:13 +03:00
ctdb_eventscript_call_names [ call ] ) ) ;
2009-12-07 16:23:35 +03:00
talloc_free ( state ) ;
return - 1 ;
}
}
2009-12-07 16:39:20 +03:00
/* Kill off any running monitor events to run this event. */
2009-12-10 12:55:33 +03:00
if ( ctdb - > current_monitor ) {
/* Discard script status so we don't save to last_status */
talloc_free ( ctdb - > current_monitor - > scripts ) ;
ctdb - > current_monitor - > scripts = NULL ;
talloc_free ( ctdb - > current_monitor ) ;
ctdb - > current_monitor = NULL ;
}
2009-12-07 16:39:20 +03:00
2009-11-24 03:52:46 +03:00
DEBUG ( DEBUG_INFO , ( __location__ " Starting eventscript %s %s \n " ,
2009-12-07 18:17:13 +03:00
ctdb_eventscript_call_names [ state - > call ] ,
state - > options ) ) ;
2009-12-08 05:15:17 +03:00
2009-12-08 04:59:10 +03:00
/* This is not a child of state, since we save it in destructor. */
state - > scripts = ctdb_get_script_list ( ctdb , ctdb ) ;
2009-12-08 05:15:17 +03:00
if ( state - > scripts = = NULL ) {
2009-12-07 16:51:25 +03:00
talloc_free ( state ) ;
return - 1 ;
}
2009-12-08 05:17:05 +03:00
state - > current = 0 ;
2009-12-18 06:24:40 +03:00
if ( ! from_user & & ( call = = CTDB_EVENT_MONITOR | | call = = CTDB_EVENT_STATUS ) ) {
ctdb - > current_monitor = state ;
}
talloc_set_destructor ( state , event_script_destructor ) ;
2009-12-08 05:17:05 +03:00
/* Nothing to do? */
if ( state - > scripts - > num_scripts = = 0 ) {
talloc_free ( state ) ;
return 0 ;
}
2009-12-07 16:23:35 +03:00
2009-12-18 06:24:40 +03:00
state - > scripts - > scripts [ 0 ] . status = fork_child_for_script ( ctdb , state ) ;
if ( state - > scripts - > scripts [ 0 ] . status ! = 0 ) {
/* Callback is called from destructor, with fail result. */
talloc_free ( state ) ;
return 0 ;
}
2007-07-04 11:45:46 +04:00
2009-10-28 08:11:54 +03:00
if ( ! timeval_is_zero ( & state - > timeout ) ) {
2009-11-23 23:40:51 +03:00
event_add_timed ( ctdb - > ev , state , timeval_current_ofs ( state - > timeout . tv_sec , state - > timeout . tv_usec ) , ctdb_event_script_timeout , state ) ;
2008-06-13 06:18:00 +04:00
} else {
2009-11-24 03:52:46 +03:00
DEBUG ( DEBUG_ERR , ( __location__ " eventscript %s %s called with no timeout \n " ,
2009-12-07 18:17:13 +03:00
ctdb_eventscript_call_names [ state - > call ] ,
state - > options ) ) ;
2007-07-04 11:45:46 +04:00
}
return 0 ;
}
2007-07-19 07:36:00 +04:00
/*
run the event script in the background , calling the callback when
finished
*/
int ctdb_event_script_callback ( struct ctdb_context * ctdb ,
TALLOC_CTX * mem_ctx ,
void ( * callback ) ( struct ctdb_context * , int , void * ) ,
void * private_data ,
2009-11-26 07:49:49 +03:00
bool from_user ,
2009-11-24 03:46:49 +03:00
enum ctdb_eventscript_call call ,
2007-07-19 07:36:00 +04:00
const char * fmt , . . . )
{
va_list ap ;
int ret ;
va_start ( ap , fmt ) ;
2009-11-26 07:49:49 +03:00
ret = ctdb_event_script_callback_v ( ctdb , callback , private_data , from_user , call , fmt , ap ) ;
2007-07-19 07:36:00 +04:00
va_end ( ap ) ;
return ret ;
}
struct callback_status {
bool done ;
int status ;
} ;
/*
called when ctdb_event_script ( ) finishes
*/
static void event_script_callback ( struct ctdb_context * ctdb , int status , void * private_data )
{
struct callback_status * s = ( struct callback_status * ) private_data ;
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
*/
2009-11-24 03:46:49 +03:00
int ctdb_event_script_args ( struct ctdb_context * ctdb , enum ctdb_eventscript_call call ,
const char * fmt , . . . )
2007-07-19 07:36:00 +04:00
{
va_list ap ;
int ret ;
struct callback_status status ;
va_start ( ap , fmt ) ;
2009-11-24 03:46:49 +03:00
ret = ctdb_event_script_callback_v ( ctdb ,
2009-11-26 07:49:49 +03:00
event_script_callback , & status , false , call , fmt , ap ) ;
2007-07-19 07:36:00 +04:00
if ( ret ! = 0 ) {
return ret ;
}
2009-11-24 03:46:49 +03:00
va_end ( ap ) ;
2007-07-19 07:36:00 +04:00
status . status = - 1 ;
status . done = false ;
while ( status . done = = false & & event_loop_once ( ctdb - > ev ) = = 0 ) /* noop */ ;
2009-12-07 16:18:57 +03:00
if ( status . status = = - ETIME ) {
DEBUG ( DEBUG_ERR , ( __location__ " eventscript for '%s' timedout. "
" Immediately banning ourself for %d seconds \n " ,
2009-12-07 18:17:13 +03:00
ctdb_eventscript_call_names [ call ] ,
2009-12-07 16:18:57 +03:00
ctdb - > tunable . recovery_ban_period ) ) ;
ctdb_ban_self ( ctdb ) ;
}
2007-07-19 07:36:00 +04:00
return status . status ;
}
2008-04-02 04:13:30 +04:00
2009-11-24 03:46:49 +03:00
int ctdb_event_script ( struct ctdb_context * ctdb , enum ctdb_eventscript_call call )
{
/* GCC complains about empty format string, so use %s and "". */
return ctdb_event_script_args ( ctdb , call , " %s " , " " ) ;
}
2008-04-02 04:13:30 +04:00
struct eventscript_callback_state {
struct ctdb_req_control * c ;
} ;
/*
2009-11-19 03:03:51 +03:00
called when a forced eventscript run has finished
2008-04-02 04:13:30 +04:00
*/
static void run_eventscripts_callback ( struct ctdb_context * ctdb , int status ,
void * private_data )
{
struct eventscript_callback_state * state =
talloc_get_type ( private_data , struct eventscript_callback_state ) ;
ctdb_enable_monitoring ( ctdb ) ;
if ( status ! = 0 ) {
DEBUG ( DEBUG_ERR , ( __location__ " Failed to forcibly run eventscripts \n " ) ) ;
}
2009-11-23 23:40:51 +03:00
ctdb_request_control_reply ( ctdb , state - > c , NULL , status , NULL ) ;
2009-11-24 03:36:53 +03:00
/* This will free the struct ctdb_event_script_state we are in! */
2008-04-02 04:13:30 +04:00
talloc_free ( state ) ;
return ;
}
2009-11-23 23:40:51 +03:00
2009-11-25 03:32:29 +03:00
/* Returns rest of string, or NULL if no match. */
2009-11-24 03:54:22 +03:00
static const char * get_call ( const char * p , enum ctdb_eventscript_call * call )
{
unsigned int len ;
/* Skip any initial whitespace. */
p + = strspn ( p , " \t " ) ;
/* See if we match any. */
2009-12-07 18:17:13 +03:00
for ( * call = 0 ; * call < CTDB_EVENT_MAX ; ( * call ) + + ) {
len = strlen ( ctdb_eventscript_call_names [ * call ] ) ;
if ( strncmp ( p , ctdb_eventscript_call_names [ * call ] , len ) = = 0 ) {
2009-11-25 03:32:29 +03:00
/* If end of string or whitespace, we're done. */
if ( strcspn ( p + len , " \t " ) = = 0 ) {
return p + len ;
}
2009-11-24 03:54:22 +03:00
}
}
return NULL ;
}
2008-04-02 04:13:30 +04:00
/*
A control to force running of the eventscripts from the ctdb client tool
*/
int32_t ctdb_run_eventscripts ( struct ctdb_context * ctdb ,
struct ctdb_req_control * c ,
TDB_DATA indata , bool * async_reply )
{
int ret ;
struct eventscript_callback_state * state ;
2009-11-24 03:54:22 +03:00
const char * options ;
enum ctdb_eventscript_call call ;
/* Figure out what call they want. */
options = get_call ( ( const char * ) indata . dptr , & call ) ;
if ( ! options ) {
DEBUG ( DEBUG_ERR , ( __location__ " Invalid forced \" %s \" \n " , ( const char * ) indata . dptr ) ) ;
return - 1 ;
}
2008-04-02 04:13:30 +04:00
2009-11-23 23:40:51 +03:00
if ( ctdb - > recovery_mode ! = CTDB_RECOVERY_NORMAL ) {
DEBUG ( DEBUG_ERR , ( __location__ " Aborted running eventscript \" %s \" while in RECOVERY mode \n " , indata . dptr ) ) ;
return - 1 ;
2008-04-02 04:13:30 +04:00
}
2009-12-07 16:39:20 +03:00
state = talloc ( ctdb - > event_script_ctx , struct eventscript_callback_state ) ;
2008-04-02 04:13:30 +04:00
CTDB_NO_MEMORY ( ctdb , state ) ;
2008-07-11 04:33:46 +04:00
state - > c = talloc_steal ( state , c ) ;
2008-04-02 04:13:30 +04:00
DEBUG ( DEBUG_NOTICE , ( " Forced running of eventscripts with arguments %s \n " , indata . dptr ) ) ;
ctdb_disable_monitoring ( ctdb ) ;
ret = ctdb_event_script_callback ( ctdb ,
state , run_eventscripts_callback , state ,
2009-11-26 07:49:49 +03:00
true , call , " %s " , options ) ;
2008-04-02 04:13:30 +04:00
if ( ret ! = 0 ) {
ctdb_enable_monitoring ( ctdb ) ;
DEBUG ( DEBUG_ERR , ( __location__ " Failed to run eventscripts with arguments %s \n " , indata . dptr ) ) ;
talloc_free ( state ) ;
return - 1 ;
}
/* tell ctdb_control.c that we will be replying asynchronously */
* async_reply = true ;
return 0 ;
}
2009-08-13 07:04:08 +04:00
int32_t ctdb_control_enable_script ( struct ctdb_context * ctdb , TDB_DATA indata )
{
const char * script ;
struct stat st ;
char * filename ;
TALLOC_CTX * tmp_ctx = talloc_new ( ctdb ) ;
script = ( char * ) indata . dptr ;
if ( indata . dsize = = 0 ) {
DEBUG ( DEBUG_ERR , ( __location__ " No script specified. \n " ) ) ;
talloc_free ( tmp_ctx ) ;
return - 1 ;
}
if ( indata . dptr [ indata . dsize - 1 ] ! = ' \0 ' ) {
DEBUG ( DEBUG_ERR , ( __location__ " String is not null terminated. \n " ) ) ;
talloc_free ( tmp_ctx ) ;
return - 1 ;
}
if ( index ( script , ' / ' ) ! = NULL ) {
DEBUG ( DEBUG_ERR , ( __location__ " Script name contains '/'. Failed to enable script %s \n " , script ) ) ;
talloc_free ( tmp_ctx ) ;
return - 1 ;
}
if ( stat ( ctdb - > event_script_dir , & st ) ! = 0 & &
errno = = ENOENT ) {
DEBUG ( DEBUG_CRIT , ( " No event script directory found at '%s' \n " , ctdb - > event_script_dir ) ) ;
talloc_free ( tmp_ctx ) ;
return - 1 ;
}
filename = talloc_asprintf ( tmp_ctx , " %s/%s " , ctdb - > event_script_dir , script ) ;
if ( filename = = NULL ) {
DEBUG ( DEBUG_ERR , ( __location__ " Failed to create script path \n " ) ) ;
talloc_free ( tmp_ctx ) ;
return - 1 ;
}
if ( stat ( filename , & st ) ! = 0 ) {
DEBUG ( DEBUG_ERR , ( " Could not stat event script %s. Failed to enable script. \n " , filename ) ) ;
talloc_free ( tmp_ctx ) ;
return - 1 ;
}
if ( chmod ( filename , st . st_mode | S_IXUSR ) = = - 1 ) {
DEBUG ( DEBUG_ERR , ( " Could not chmod %s. Failed to enable script. \n " , filename ) ) ;
talloc_free ( tmp_ctx ) ;
return - 1 ;
}
talloc_free ( tmp_ctx ) ;
return 0 ;
}
int32_t ctdb_control_disable_script ( struct ctdb_context * ctdb , TDB_DATA indata )
{
const char * script ;
struct stat st ;
char * filename ;
TALLOC_CTX * tmp_ctx = talloc_new ( ctdb ) ;
script = ( char * ) indata . dptr ;
if ( indata . dsize = = 0 ) {
DEBUG ( DEBUG_ERR , ( __location__ " No script specified. \n " ) ) ;
talloc_free ( tmp_ctx ) ;
return - 1 ;
}
if ( indata . dptr [ indata . dsize - 1 ] ! = ' \0 ' ) {
DEBUG ( DEBUG_ERR , ( __location__ " String is not null terminated. \n " ) ) ;
talloc_free ( tmp_ctx ) ;
return - 1 ;
}
if ( index ( script , ' / ' ) ! = NULL ) {
DEBUG ( DEBUG_ERR , ( __location__ " Script name contains '/'. Failed to disable script %s \n " , script ) ) ;
talloc_free ( tmp_ctx ) ;
return - 1 ;
}
if ( stat ( ctdb - > event_script_dir , & st ) ! = 0 & &
errno = = ENOENT ) {
DEBUG ( DEBUG_CRIT , ( " No event script directory found at '%s' \n " , ctdb - > event_script_dir ) ) ;
talloc_free ( tmp_ctx ) ;
return - 1 ;
}
filename = talloc_asprintf ( tmp_ctx , " %s/%s " , ctdb - > event_script_dir , script ) ;
if ( filename = = NULL ) {
DEBUG ( DEBUG_ERR , ( __location__ " Failed to create script path \n " ) ) ;
talloc_free ( tmp_ctx ) ;
return - 1 ;
}
if ( stat ( filename , & st ) ! = 0 ) {
DEBUG ( DEBUG_ERR , ( " Could not stat event script %s. Failed to disable script. \n " , filename ) ) ;
talloc_free ( tmp_ctx ) ;
return - 1 ;
}
if ( chmod ( filename , st . st_mode & ~ ( S_IXUSR | S_IXGRP | S_IXOTH ) ) = = - 1 ) {
DEBUG ( DEBUG_ERR , ( " Could not chmod %s. Failed to disable script. \n " , filename ) ) ;
talloc_free ( tmp_ctx ) ;
return - 1 ;
}
talloc_free ( tmp_ctx ) ;
return 0 ;
}