4 Copyright (C) Andrew Tridgell 2007
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, see <http://www.gnu.org/licenses/>.
22 #include "system/filesys.h"
23 #include "system/wait.h"
24 #include "system/dir.h"
25 #include "system/locale.h"
26 #include "../include/ctdb_private.h"
27 #include "../common/rb_tree.h"
28 #include "lib/util/dlinklist.h"
30 static void ctdb_event_script_timeout(struct event_context
*ev
, struct timed_event
*te
, struct timeval t
, void *p
);
33 ctdbd sends us a SIGTERM when we should die.
35 static void sigterm(int sig
)
39 /* all the child processes will be running in the same process group */
42 kill(-getpid(), SIGKILL
);
49 /* This is attached to the event script state. */
50 struct event_script_callback
{
51 struct event_script_callback
*next
, *prev
;
52 struct ctdb_context
*ctdb
;
54 /* Warning: this can free us! */
55 void (*fn
)(struct ctdb_context
*, int, void *);
60 struct ctdb_event_script_state
{
61 struct ctdb_context
*ctdb
;
62 struct event_script_callback
*callback
;
65 enum ctdb_eventscript_call call
;
67 struct timeval timeout
;
70 struct ctdb_scripts_wire
*scripts
;
73 static struct ctdb_script_wire
*get_current_script(struct ctdb_event_script_state
*state
)
75 return &state
->scripts
->scripts
[state
->current
];
78 /* called from ctdb_logging when we have received output on STDERR from
79 * one of the eventscripts
81 static void log_event_script_output(const char *str
, uint16_t len
, void *p
)
83 struct ctdb_event_script_state
*state
84 = talloc_get_type(p
, struct ctdb_event_script_state
);
85 struct ctdb_script_wire
*current
;
86 unsigned int slen
, min
;
88 /* We may have been aborted to run something else. Discard */
89 if (state
->scripts
== NULL
) {
93 current
= get_current_script(state
);
95 /* Append, but don't overfill buffer. It starts zero-filled. */
96 slen
= strlen(current
->output
);
97 min
= MIN(len
, sizeof(current
->output
) - slen
- 1);
99 memcpy(current
->output
+ slen
, str
, min
);
102 int32_t ctdb_control_get_event_script_status(struct ctdb_context
*ctdb
,
106 if (call_type
>= CTDB_EVENT_MAX
) {
110 if (ctdb
->last_status
[call_type
] == NULL
) {
111 /* If it's never been run, return nothing so they can tell. */
114 outdata
->dsize
= talloc_get_size(ctdb
->last_status
[call_type
]);
115 outdata
->dptr
= (uint8_t *)ctdb
->last_status
[call_type
];
120 struct ctdb_script_tree_item
{
125 /* Return true if OK, otherwise set errno. */
126 static bool check_executable(const char *dir
, const char *name
)
131 full
= talloc_asprintf(NULL
, "%s/%s", dir
, name
);
135 if (stat(full
, &st
) != 0) {
136 DEBUG(DEBUG_ERR
,("Could not stat event script %s: %s\n",
137 full
, strerror(errno
)));
142 if (!(st
.st_mode
& S_IXUSR
)) {
143 DEBUG(DEBUG_DEBUG
,("Event script %s is not executable. Ignoring this event script\n", full
));
153 static struct ctdb_scripts_wire
*ctdb_get_script_list(struct ctdb_context
*ctdb
, TALLOC_CTX
*mem_ctx
)
159 struct ctdb_scripts_wire
*scripts
;
160 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
161 struct ctdb_script_tree_item
*tree_item
;
165 the service specific event scripts
167 if (stat(ctdb
->event_script_dir
, &st
) != 0 &&
169 DEBUG(DEBUG_CRIT
,("No event script directory found at '%s'\n", ctdb
->event_script_dir
));
170 talloc_free(tmp_ctx
);
174 /* create a tree to store all the script names in */
175 tree
= trbt_create(tmp_ctx
, 0);
177 /* scan all directory entries and insert all valid scripts into the
180 dir
= opendir(ctdb
->event_script_dir
);
182 DEBUG(DEBUG_CRIT
,("Failed to open event script directory '%s'\n", ctdb
->event_script_dir
));
183 talloc_free(tmp_ctx
);
188 while ((de
=readdir(dir
)) != NULL
) {
192 namlen
= strlen(de
->d_name
);
198 if (de
->d_name
[namlen
-1] == '~') {
199 /* skip files emacs left behind */
203 if (de
->d_name
[2] != '.') {
207 if (sscanf(de
->d_name
, "%02u.", &num
) != 1) {
211 if (strlen(de
->d_name
) > MAX_SCRIPT_NAME
) {
212 DEBUG(DEBUG_ERR
,("Script name %s too long! %u chars max",
213 de
->d_name
, MAX_SCRIPT_NAME
));
217 tree_item
= talloc(tree
, struct ctdb_script_tree_item
);
218 if (tree_item
== NULL
) {
219 DEBUG(DEBUG_ERR
, (__location__
" Failed to allocate new tree item\n"));
221 talloc_free(tmp_ctx
);
225 tree_item
->error
= 0;
226 if (!check_executable(ctdb
->event_script_dir
, de
->d_name
)) {
227 tree_item
->error
= errno
;
230 tree_item
->name
= talloc_strdup(tree_item
, de
->d_name
);
231 if (tree_item
->name
== NULL
) {
232 DEBUG(DEBUG_ERR
,(__location__
" Failed to allocate script name.\n"));
234 talloc_free(tmp_ctx
);
238 /* store the event script in the tree */
239 trbt_insert32(tree
, (num
<<16)|count
++, tree_item
);
243 /* Overallocates by one, but that's OK */
244 scripts
= talloc_zero_size(tmp_ctx
,
246 + sizeof(scripts
->scripts
[0]) * count
);
247 if (scripts
== NULL
) {
248 DEBUG(DEBUG_ERR
, (__location__
" Failed to allocate scripts\n"));
249 talloc_free(tmp_ctx
);
252 scripts
->num_scripts
= count
;
254 for (count
= 0; count
< scripts
->num_scripts
; count
++) {
255 tree_item
= trbt_findfirstarray32(tree
, 1);
257 strcpy(scripts
->scripts
[count
].name
, tree_item
->name
);
258 scripts
->scripts
[count
].status
= -tree_item
->error
;
260 /* remove this script from the tree */
261 talloc_free(tree_item
);
264 talloc_steal(mem_ctx
, scripts
);
265 talloc_free(tmp_ctx
);
269 static int child_setup(struct ctdb_context
*ctdb
)
271 if (setpgid(0,0) != 0) {
273 DEBUG(DEBUG_ERR
,("Failed to create process group for event scripts - %s\n",
278 signal(SIGTERM
, sigterm
);
282 static char *child_command_string(struct ctdb_context
*ctdb
,
284 const char *scriptname
,
285 enum ctdb_eventscript_call call
,
288 return talloc_asprintf(ctx
, "%s/%s %s %s",
289 ctdb
->event_script_dir
,
291 ctdb_eventscript_call_names
[call
],
295 static int child_run_one(struct ctdb_context
*ctdb
,
296 const char *scriptname
, const char *cmdstr
)
300 ret
= system(cmdstr
);
301 /* if the system() call was successful, translate ret into the
302 return code from the command
305 ret
= WEXITSTATUS(ret
);
310 /* 127 could mean it does not exist, 126 non-executable. */
311 if (ret
== 127 || ret
== 126) {
313 if (!check_executable(ctdb
->event_script_dir
, scriptname
)) {
314 DEBUG(DEBUG_ERR
,("Script %s returned status %u. Someone just deleted it?\n",
323 Actually run one event script
324 this function is called and run in the context of a forked child
325 which allows it to do blocking calls such as system()
327 static int child_run_script(struct ctdb_context
*ctdb
,
328 enum ctdb_eventscript_call call
,
330 struct ctdb_script_wire
*current
)
334 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
336 ret
= child_setup(ctdb
);
340 cmdstr
= child_command_string(ctdb
, tmp_ctx
,
341 current
->name
, call
, options
);
342 CTDB_NO_MEMORY(ctdb
, cmdstr
);
344 DEBUG(DEBUG_DEBUG
,("Executing event script %s\n",cmdstr
));
346 if (current
->status
) {
347 ret
= current
->status
;
351 ret
= child_run_one(ctdb
, current
->name
, cmdstr
);
353 talloc_free(tmp_ctx
);
357 /* There cannot be more than 10 arguments to command helper. */
358 #define MAX_HELPER_ARGS (10)
360 static bool child_helper_args(TALLOC_CTX
*mem_ctx
, struct ctdb_context
*ctdb
,
361 enum ctdb_eventscript_call call
,
363 struct ctdb_script_wire
*current
, int fd
,
364 int *argc
, const char ***argv
)
368 char *t
, *saveptr
, *opt
;
370 tmp
= talloc_array(mem_ctx
, const char *, 10+1);
371 if (tmp
== NULL
) goto failed
;
373 tmp
[0] = talloc_asprintf(tmp
, "%d", fd
);
374 tmp
[1] = talloc_asprintf(tmp
, "%s/%s", ctdb
->event_script_dir
, current
->name
);
375 tmp
[2] = talloc_asprintf(tmp
, "%s", ctdb_eventscript_call_names
[call
]);
378 /* Split options into individual arguments */
379 opt
= talloc_strdup(mem_ctx
, options
);
384 t
= strtok_r(opt
, " ", &saveptr
);
386 tmp
[n
++] = talloc_strdup(tmp
, t
);
387 if (n
> MAX_HELPER_ARGS
) {
390 t
= strtok_r(NULL
, " ", &saveptr
);
393 for (i
=0; i
<n
; i
++) {
394 if (tmp
[i
] == NULL
) {
399 /* Last argument should be NULL */
408 DEBUG(DEBUG_ERR
, (__location__
" too many arguments '%s' to eventscript '%s'\n",
409 options
, ctdb_eventscript_call_names
[call
]));
419 static void ctdb_event_script_handler(struct event_context
*ev
, struct fd_event
*fde
,
420 uint16_t flags
, void *p
);
422 static const char *helper_prog
= NULL
;
424 static int fork_child_for_script(struct ctdb_context
*ctdb
,
425 struct ctdb_event_script_state
*state
)
428 struct tevent_fd
*fde
;
429 struct ctdb_script_wire
*current
= get_current_script(state
);
432 static const char *helper
= BINDIR
"/ctdb_event_helper";
434 if (helper_prog
== NULL
) {
435 const char *t
= getenv("CTDB_EVENT_HELPER");
439 helper_prog
= helper
;
443 current
->start
= timeval_current();
447 DEBUG(DEBUG_ERR
, (__location__
" pipe failed for child eventscript process\n"));
451 /* Arguments for helper */
452 if (!child_helper_args(state
, ctdb
, state
->call
, state
->options
, current
,
453 state
->fd
[1], &argc
, &argv
)) {
454 DEBUG(DEBUG_ERR
, (__location__
" failed to create arguments for eventscript helper\n"));
461 if (!ctdb_vfork_with_logging(state
, ctdb
, current
->name
,
462 helper_prog
, argc
, argv
,
463 log_event_script_output
,
464 state
, &state
->child
)) {
475 set_close_on_exec(state
->fd
[0]);
477 /* Set ourselves up to be called when that's done. */
478 fde
= event_add_fd(ctdb
->ev
, state
, state
->fd
[0], EVENT_FD_READ
,
479 ctdb_event_script_handler
, state
);
480 tevent_fd_set_auto_close(fde
);
486 Summarize status of this run of scripts.
488 static int script_status(struct ctdb_scripts_wire
*scripts
)
492 for (i
= 0; i
< scripts
->num_scripts
; i
++) {
493 switch (scripts
->scripts
[i
].status
) {
496 /* Disabled or missing; that's OK. */
502 return scripts
->scripts
[i
].status
;
510 /* called when child is finished */
511 static void ctdb_event_script_handler(struct event_context
*ev
, struct fd_event
*fde
,
512 uint16_t flags
, void *p
)
514 struct ctdb_event_script_state
*state
=
515 talloc_get_type(p
, struct ctdb_event_script_state
);
516 struct ctdb_script_wire
*current
= get_current_script(state
);
517 struct ctdb_context
*ctdb
= state
->ctdb
;
521 DEBUG(DEBUG_ERR
,("Eventscript finished but ctdb is NULL\n"));
525 r
= read(state
->fd
[0], ¤t
->status
, sizeof(current
->status
));
527 current
->status
= -errno
;
528 } else if (r
!= sizeof(current
->status
)) {
529 current
->status
= -EIO
;
532 current
->finished
= timeval_current();
533 /* valgrind gets overloaded if we run next script as it's still doing
534 * post-execution analysis, so kill finished child here. */
535 if (ctdb
->valgrinding
) {
536 ctdb_kill(ctdb
, state
->child
, SIGKILL
);
541 status
= script_status(state
->scripts
);
543 /* Aborted or finished all scripts? We're done. */
544 if (status
!= 0 || state
->current
+1 == state
->scripts
->num_scripts
) {
545 DEBUG(DEBUG_INFO
,(__location__
" Eventscript %s %s finished with state %d\n",
546 ctdb_eventscript_call_names
[state
->call
], state
->options
, status
));
548 ctdb
->event_script_timeouts
= 0;
553 /* Forget about that old fd. */
559 current
->status
= fork_child_for_script(ctdb
, state
);
560 if (current
->status
!= 0) {
561 /* This calls the callback. */
566 struct debug_hung_script_state
{
567 struct ctdb_context
*ctdb
;
569 enum ctdb_eventscript_call call
;
572 static int debug_hung_script_state_destructor(struct debug_hung_script_state
*state
)
575 ctdb_kill(state
->ctdb
, state
->child
, SIGKILL
);
580 static void debug_hung_script_timeout(struct tevent_context
*ev
, struct tevent_timer
*te
,
581 struct timeval t
, void *p
)
583 struct debug_hung_script_state
*state
=
584 talloc_get_type(p
, struct debug_hung_script_state
);
589 static void debug_hung_script_done(struct tevent_context
*ev
, struct tevent_fd
*fde
,
590 uint16_t flags
, void *p
)
592 struct debug_hung_script_state
*state
=
593 talloc_get_type(p
, struct debug_hung_script_state
);
598 static void ctdb_run_debug_hung_script(struct ctdb_context
*ctdb
, struct debug_hung_script_state
*state
)
601 const char * debug_hung_script
= ETCDIR
"/ctdb/debug-hung-script.sh";
603 struct tevent_timer
*ttimer
;
604 struct tevent_fd
*tfd
;
607 DEBUG(DEBUG_ERR
,("Failed to create pipe fd for debug hung script\n"));
611 if (!ctdb_fork_with_logging(ctdb
, ctdb
, "Hung script", NULL
, NULL
, &pid
)) {
612 DEBUG(DEBUG_ERR
,("Failed to fork a child process with logging to track hung event script\n"));
618 DEBUG(DEBUG_ERR
,("Fork for debug script failed : %s\n",
627 ctdb_set_process_name("ctdb_debug_hung_script");
628 if (getenv("CTDB_DEBUG_HUNG_SCRIPT") != NULL
) {
629 debug_hung_script
= getenv("CTDB_DEBUG_HUNG_SCRIPT");
634 buf
= talloc_asprintf(NULL
, "%s %d %s",
635 debug_hung_script
, state
->child
,
636 ctdb_eventscript_call_names
[state
->call
]);
645 ttimer
= tevent_add_timer(ctdb
->ev
, state
,
646 timeval_current_ofs(ctdb
->tunable
.script_timeout
, 0),
647 debug_hung_script_timeout
, state
);
648 if (ttimer
== NULL
) {
653 tfd
= tevent_add_fd(ctdb
->ev
, state
, fd
[0], EVENT_FD_READ
,
654 debug_hung_script_done
, state
);
660 tevent_fd_set_auto_close(tfd
);
663 /* called when child times out */
664 static void ctdb_event_script_timeout(struct event_context
*ev
, struct timed_event
*te
,
665 struct timeval t
, void *p
)
667 struct ctdb_event_script_state
*state
= talloc_get_type(p
, struct ctdb_event_script_state
);
668 struct ctdb_context
*ctdb
= state
->ctdb
;
669 struct ctdb_script_wire
*current
= get_current_script(state
);
670 struct debug_hung_script_state
*debug_state
;
672 DEBUG(DEBUG_ERR
,("Event script '%s %s %s' timed out after %.1fs, count: %u, pid: %d\n",
673 current
->name
, ctdb_eventscript_call_names
[state
->call
], state
->options
,
674 timeval_elapsed(¤t
->start
),
675 ctdb
->event_script_timeouts
, state
->child
));
677 /* ignore timeouts for these events */
678 switch (state
->call
) {
679 case CTDB_EVENT_START_RECOVERY
:
680 case CTDB_EVENT_RECOVERED
:
681 case CTDB_EVENT_TAKE_IP
:
682 case CTDB_EVENT_RELEASE_IP
:
683 state
->scripts
->scripts
[state
->current
].status
= 0;
684 DEBUG(DEBUG_ERR
,("Ignoring hung script for %s call %d\n", state
->options
, state
->call
));
687 state
->scripts
->scripts
[state
->current
].status
= -ETIME
;
690 debug_state
= talloc_zero(ctdb
, struct debug_hung_script_state
);
691 if (debug_state
== NULL
) {
696 /* Save information useful for running debug hung script, so
697 * eventscript state can be freed.
699 debug_state
->ctdb
= ctdb
;
700 debug_state
->child
= state
->child
;
701 debug_state
->call
= state
->call
;
703 /* This destructor will actually kill the hung event script */
704 talloc_set_destructor(debug_state
, debug_hung_script_state_destructor
);
709 ctdb_run_debug_hung_script(ctdb
, debug_state
);
713 destroy an event script: kill it if ->child != 0.
715 static int event_script_destructor(struct ctdb_event_script_state
*state
)
718 struct event_script_callback
*callback
;
721 DEBUG(DEBUG_ERR
,(__location__
" Sending SIGTERM to child pid:%d\n", state
->child
));
723 if (ctdb_kill(state
->ctdb
, state
->child
, SIGTERM
) != 0) {
724 DEBUG(DEBUG_ERR
,("Failed to kill child process for eventscript, errno %s(%d)\n", strerror(errno
), errno
));
728 /* If we were the current monitor, we no longer are. */
729 if (state
->ctdb
->current_monitor
== state
) {
730 state
->ctdb
->current_monitor
= NULL
;
733 /* Save our scripts as the last executed status, if we have them.
734 * See ctdb_event_script_callback_v where we abort monitor event. */
735 if (state
->scripts
) {
736 talloc_free(state
->ctdb
->last_status
[state
->call
]);
737 state
->ctdb
->last_status
[state
->call
] = state
->scripts
;
738 if (state
->current
< state
->ctdb
->last_status
[state
->call
]->num_scripts
) {
739 state
->ctdb
->last_status
[state
->call
]->num_scripts
= state
->current
+1;
743 /* Use last status as result, or "OK" if none. */
744 if (state
->ctdb
->last_status
[state
->call
]) {
745 status
= script_status(state
->ctdb
->last_status
[state
->call
]);
750 /* This is allowed to free us; talloc will prevent double free anyway,
751 * but beware if you call this outside the destructor!
752 * the callback hangs off a different context so we walk the list
753 * of "active" callbacks until we find the one state points to.
754 * if we cant find it it means the callback has been removed.
756 for (callback
= state
->ctdb
->script_callbacks
; callback
!= NULL
; callback
= callback
->next
) {
757 if (callback
== state
->callback
) {
762 state
->callback
= NULL
;
765 /* Make sure destructor doesn't free itself! */
766 talloc_steal(NULL
, callback
);
767 callback
->fn(state
->ctdb
, status
, callback
->private_data
);
768 talloc_free(callback
);
774 static unsigned int count_words(const char *options
)
776 unsigned int words
= 0;
778 options
+= strspn(options
, " \t");
781 options
+= strcspn(options
, " \t");
782 options
+= strspn(options
, " \t");
787 static bool check_options(enum ctdb_eventscript_call call
, const char *options
)
790 /* These all take no arguments. */
791 case CTDB_EVENT_INIT
:
792 case CTDB_EVENT_SETUP
:
793 case CTDB_EVENT_STARTUP
:
794 case CTDB_EVENT_START_RECOVERY
:
795 case CTDB_EVENT_RECOVERED
:
796 case CTDB_EVENT_MONITOR
:
797 case CTDB_EVENT_SHUTDOWN
:
798 case CTDB_EVENT_IPREALLOCATED
:
799 return count_words(options
) == 0;
801 case CTDB_EVENT_TAKE_IP
: /* interface, IP address, netmask bits. */
802 case CTDB_EVENT_RELEASE_IP
:
803 return count_words(options
) == 3;
805 case CTDB_EVENT_UPDATE_IP
: /* old interface, new interface, IP address, netmask bits. */
806 return count_words(options
) == 4;
809 DEBUG(DEBUG_ERR
,(__location__
"Unknown ctdb_eventscript_call %u\n", call
));
814 static int remove_callback(struct event_script_callback
*callback
)
816 DLIST_REMOVE(callback
->ctdb
->script_callbacks
, callback
);
821 run the event script in the background, calling the callback when
824 static int ctdb_event_script_callback_v(struct ctdb_context
*ctdb
,
826 void (*callback
)(struct ctdb_context
*, int, void *),
828 enum ctdb_eventscript_call call
,
829 const char *fmt
, va_list ap
)
831 struct ctdb_event_script_state
*state
;
833 if (ctdb
->recovery_mode
!= CTDB_RECOVERY_NORMAL
) {
834 /* we guarantee that only some specifically allowed event scripts are run
836 const enum ctdb_eventscript_call allowed_calls
[] = {
839 CTDB_EVENT_START_RECOVERY
,
841 CTDB_EVENT_RELEASE_IP
,
842 CTDB_EVENT_IPREALLOCATED
,
845 for (i
=0;i
<ARRAY_SIZE(allowed_calls
);i
++) {
846 if (call
== allowed_calls
[i
]) break;
848 if (i
== ARRAY_SIZE(allowed_calls
)) {
849 DEBUG(DEBUG_ERR
,("Refusing to run event scripts call '%s' while in recovery\n",
850 ctdb_eventscript_call_names
[call
]));
855 /* Kill off any running monitor events to run this event. */
856 if (ctdb
->current_monitor
) {
857 struct ctdb_event_script_state
*ms
= talloc_get_type(ctdb
->current_monitor
, struct ctdb_event_script_state
);
859 /* Cancel current monitor callback state only if monitoring
860 * context ctdb->monitor->monitor_context has not been freed */
861 if (ms
->callback
!= NULL
&& !ctdb_stopped_monitoring(ctdb
)) {
862 ms
->callback
->fn(ctdb
, -ECANCELED
, ms
->callback
->private_data
);
863 talloc_free(ms
->callback
);
866 /* Discard script status so we don't save to last_status */
867 talloc_free(ctdb
->current_monitor
->scripts
);
868 ctdb
->current_monitor
->scripts
= NULL
;
869 talloc_free(ctdb
->current_monitor
);
870 ctdb
->current_monitor
= NULL
;
873 state
= talloc(ctdb
->event_script_ctx
, struct ctdb_event_script_state
);
874 CTDB_NO_MEMORY(ctdb
, state
);
876 /* The callback isn't done if the context is freed. */
877 state
->callback
= talloc(mem_ctx
, struct event_script_callback
);
878 CTDB_NO_MEMORY(ctdb
, state
->callback
);
879 DLIST_ADD(ctdb
->script_callbacks
, state
->callback
);
880 talloc_set_destructor(state
->callback
, remove_callback
);
881 state
->callback
->ctdb
= ctdb
;
882 state
->callback
->fn
= callback
;
883 state
->callback
->private_data
= private_data
;
887 state
->options
= talloc_vasprintf(state
, fmt
, ap
);
888 state
->timeout
= timeval_set(ctdb
->tunable
.script_timeout
, 0);
889 state
->scripts
= NULL
;
890 if (state
->options
== NULL
) {
891 DEBUG(DEBUG_ERR
, (__location__
" could not allocate state->options\n"));
895 if (!check_options(state
->call
, state
->options
)) {
896 DEBUG(DEBUG_ERR
, ("Bad eventscript options '%s' for %s\n",
897 ctdb_eventscript_call_names
[state
->call
], state
->options
));
902 DEBUG(DEBUG_INFO
,(__location__
" Starting eventscript %s %s\n",
903 ctdb_eventscript_call_names
[state
->call
],
906 /* This is not a child of state, since we save it in destructor. */
907 state
->scripts
= ctdb_get_script_list(ctdb
, ctdb
);
908 if (state
->scripts
== NULL
) {
915 if (call
== CTDB_EVENT_MONITOR
) {
916 ctdb
->current_monitor
= state
;
919 talloc_set_destructor(state
, event_script_destructor
);
922 if (state
->scripts
->num_scripts
== 0) {
927 state
->scripts
->scripts
[0].status
= fork_child_for_script(ctdb
, state
);
928 if (state
->scripts
->scripts
[0].status
!= 0) {
929 /* Callback is called from destructor, with fail result. */
934 if (!timeval_is_zero(&state
->timeout
)) {
935 event_add_timed(ctdb
->ev
, state
, timeval_current_ofs(state
->timeout
.tv_sec
, state
->timeout
.tv_usec
), ctdb_event_script_timeout
, state
);
937 DEBUG(DEBUG_ERR
, (__location__
" eventscript %s %s called with no timeout\n",
938 ctdb_eventscript_call_names
[state
->call
],
947 run the event script in the background, calling the callback when
948 finished. If mem_ctx is freed, callback will never be called.
950 int ctdb_event_script_callback(struct ctdb_context
*ctdb
,
952 void (*callback
)(struct ctdb_context
*, int, void *),
954 enum ctdb_eventscript_call call
,
955 const char *fmt
, ...)
961 ret
= ctdb_event_script_callback_v(ctdb
, mem_ctx
, callback
, private_data
, call
, fmt
, ap
);
968 struct callback_status
{
974 called when ctdb_event_script() finishes
976 static void event_script_callback(struct ctdb_context
*ctdb
, int status
, void *private_data
)
978 struct callback_status
*s
= (struct callback_status
*)private_data
;
984 run the event script, waiting for it to complete. Used when the caller
985 doesn't want to continue till the event script has finished.
987 int ctdb_event_script_args(struct ctdb_context
*ctdb
, enum ctdb_eventscript_call call
,
988 const char *fmt
, ...)
992 struct callback_status status
;
995 ret
= ctdb_event_script_callback_v(ctdb
, ctdb
,
996 event_script_callback
, &status
, call
, fmt
, ap
);
1003 status
.done
= false;
1005 while (status
.done
== false && event_loop_once(ctdb
->ev
) == 0) /* noop */;
1007 if (status
.status
== -ETIME
) {
1008 DEBUG(DEBUG_ERR
, (__location__
" eventscript for '%s' timedout."
1009 " Immediately banning ourself for %d seconds\n",
1010 ctdb_eventscript_call_names
[call
],
1011 ctdb
->tunable
.recovery_ban_period
));
1013 /* Don't ban self if CTDB is starting up or shutting down */
1014 if (call
!= CTDB_EVENT_INIT
&& call
!= CTDB_EVENT_SHUTDOWN
) {
1015 ctdb_ban_self(ctdb
);
1019 return status
.status
;
1022 int ctdb_event_script(struct ctdb_context
*ctdb
, enum ctdb_eventscript_call call
)
1024 /* GCC complains about empty format string, so use %s and "". */
1025 return ctdb_event_script_args(ctdb
, call
, "%s", "");
1028 struct eventscript_callback_state
{
1029 struct ctdb_req_control
*c
;
1033 called when a forced eventscript run has finished
1035 static void run_eventscripts_callback(struct ctdb_context
*ctdb
, int status
,
1038 struct eventscript_callback_state
*state
=
1039 talloc_get_type(private_data
, struct eventscript_callback_state
);
1041 ctdb_enable_monitoring(ctdb
);
1044 DEBUG(DEBUG_ERR
,(__location__
" Failed to run eventscripts\n"));
1047 ctdb_request_control_reply(ctdb
, state
->c
, NULL
, status
, NULL
);
1048 /* This will free the struct ctdb_event_script_state we are in! */
1054 /* Returns rest of string, or NULL if no match. */
1055 static const char *get_call(const char *p
, enum ctdb_eventscript_call
*call
)
1059 /* Skip any initial whitespace. */
1060 p
+= strspn(p
, " \t");
1062 /* See if we match any. */
1063 for (*call
= 0; *call
< CTDB_EVENT_MAX
; (*call
)++) {
1064 len
= strlen(ctdb_eventscript_call_names
[*call
]);
1065 if (strncmp(p
, ctdb_eventscript_call_names
[*call
], len
) == 0) {
1066 /* If end of string or whitespace, we're done. */
1067 if (strcspn(p
+ len
, " \t") == 0) {
1076 A control to force running of the eventscripts from the ctdb client tool
1078 int32_t ctdb_run_eventscripts(struct ctdb_context
*ctdb
,
1079 struct ctdb_req_control
*c
,
1080 TDB_DATA indata
, bool *async_reply
)
1083 struct eventscript_callback_state
*state
;
1084 const char *options
;
1085 enum ctdb_eventscript_call call
;
1087 /* Figure out what call they want. */
1088 options
= get_call((const char *)indata
.dptr
, &call
);
1090 DEBUG(DEBUG_ERR
, (__location__
" Invalid event name \"%s\"\n", (const char *)indata
.dptr
));
1094 if (ctdb
->recovery_mode
!= CTDB_RECOVERY_NORMAL
) {
1095 DEBUG(DEBUG_ERR
, (__location__
" Aborted running eventscript \"%s\" while in RECOVERY mode\n", indata
.dptr
));
1099 state
= talloc(ctdb
->event_script_ctx
, struct eventscript_callback_state
);
1100 CTDB_NO_MEMORY(ctdb
, state
);
1102 state
->c
= talloc_steal(state
, c
);
1104 DEBUG(DEBUG_NOTICE
,("Running eventscripts with arguments %s\n", indata
.dptr
));
1106 ctdb_disable_monitoring(ctdb
);
1108 ret
= ctdb_event_script_callback(ctdb
,
1109 state
, run_eventscripts_callback
, state
,
1110 call
, "%s", options
);
1113 ctdb_enable_monitoring(ctdb
);
1114 DEBUG(DEBUG_ERR
,(__location__
" Failed to run eventscripts with arguments %s\n", indata
.dptr
));
1119 /* tell ctdb_control.c that we will be replying asynchronously */
1120 *async_reply
= true;
1127 int32_t ctdb_control_enable_script(struct ctdb_context
*ctdb
, TDB_DATA indata
)
1132 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
1134 script
= (char *)indata
.dptr
;
1135 if (indata
.dsize
== 0) {
1136 DEBUG(DEBUG_ERR
,(__location__
" No script specified.\n"));
1137 talloc_free(tmp_ctx
);
1140 if (indata
.dptr
[indata
.dsize
- 1] != '\0') {
1141 DEBUG(DEBUG_ERR
,(__location__
" String is not null terminated.\n"));
1142 talloc_free(tmp_ctx
);
1145 if (index(script
,'/') != NULL
) {
1146 DEBUG(DEBUG_ERR
,(__location__
" Script name contains '/'. Failed to enable script %s\n", script
));
1147 talloc_free(tmp_ctx
);
1152 if (stat(ctdb
->event_script_dir
, &st
) != 0 &&
1154 DEBUG(DEBUG_CRIT
,("No event script directory found at '%s'\n", ctdb
->event_script_dir
));
1155 talloc_free(tmp_ctx
);
1160 filename
= talloc_asprintf(tmp_ctx
, "%s/%s", ctdb
->event_script_dir
, script
);
1161 if (filename
== NULL
) {
1162 DEBUG(DEBUG_ERR
,(__location__
" Failed to create script path\n"));
1163 talloc_free(tmp_ctx
);
1167 if (stat(filename
, &st
) != 0) {
1168 DEBUG(DEBUG_ERR
,("Could not stat event script %s. Failed to enable script.\n", filename
));
1169 talloc_free(tmp_ctx
);
1173 if (chmod(filename
, st
.st_mode
| S_IXUSR
) == -1) {
1174 DEBUG(DEBUG_ERR
,("Could not chmod %s. Failed to enable script.\n", filename
));
1175 talloc_free(tmp_ctx
);
1179 talloc_free(tmp_ctx
);
1183 int32_t ctdb_control_disable_script(struct ctdb_context
*ctdb
, TDB_DATA indata
)
1188 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
1190 script
= (char *)indata
.dptr
;
1191 if (indata
.dsize
== 0) {
1192 DEBUG(DEBUG_ERR
,(__location__
" No script specified.\n"));
1193 talloc_free(tmp_ctx
);
1196 if (indata
.dptr
[indata
.dsize
- 1] != '\0') {
1197 DEBUG(DEBUG_ERR
,(__location__
" String is not null terminated.\n"));
1198 talloc_free(tmp_ctx
);
1201 if (index(script
,'/') != NULL
) {
1202 DEBUG(DEBUG_ERR
,(__location__
" Script name contains '/'. Failed to disable script %s\n", script
));
1203 talloc_free(tmp_ctx
);
1208 if (stat(ctdb
->event_script_dir
, &st
) != 0 &&
1210 DEBUG(DEBUG_CRIT
,("No event script directory found at '%s'\n", ctdb
->event_script_dir
));
1211 talloc_free(tmp_ctx
);
1216 filename
= talloc_asprintf(tmp_ctx
, "%s/%s", ctdb
->event_script_dir
, script
);
1217 if (filename
== NULL
) {
1218 DEBUG(DEBUG_ERR
,(__location__
" Failed to create script path\n"));
1219 talloc_free(tmp_ctx
);
1223 if (stat(filename
, &st
) != 0) {
1224 DEBUG(DEBUG_ERR
,("Could not stat event script %s. Failed to disable script.\n", filename
));
1225 talloc_free(tmp_ctx
);
1229 if (chmod(filename
, st
.st_mode
& ~(S_IXUSR
|S_IXGRP
|S_IXOTH
)) == -1) {
1230 DEBUG(DEBUG_ERR
,("Could not chmod %s. Failed to disable script.\n", filename
));
1231 talloc_free(tmp_ctx
);
1235 talloc_free(tmp_ctx
);