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
);
32 /* This is attached to the event script state. */
33 struct event_script_callback
{
34 struct event_script_callback
*next
, *prev
;
35 struct ctdb_context
*ctdb
;
37 /* Warning: this can free us! */
38 void (*fn
)(struct ctdb_context
*, int, void *);
43 struct ctdb_event_script_state
{
44 struct ctdb_context
*ctdb
;
45 struct event_script_callback
*callback
;
48 enum ctdb_eventscript_call call
;
50 struct timeval timeout
;
53 struct ctdb_scripts_wire
*scripts
;
56 static struct ctdb_script_wire
*get_current_script(struct ctdb_event_script_state
*state
)
58 return &state
->scripts
->scripts
[state
->current
];
61 /* called from ctdb_logging when we have received output on STDERR from
62 * one of the eventscripts
64 static void log_event_script_output(const char *str
, uint16_t len
, void *p
)
66 struct ctdb_event_script_state
*state
67 = talloc_get_type(p
, struct ctdb_event_script_state
);
68 struct ctdb_script_wire
*current
;
69 unsigned int slen
, min
;
71 /* We may have been aborted to run something else. Discard */
72 if (state
->scripts
== NULL
) {
76 current
= get_current_script(state
);
78 /* Append, but don't overfill buffer. It starts zero-filled. */
79 slen
= strlen(current
->output
);
80 min
= MIN(len
, sizeof(current
->output
) - slen
- 1);
82 memcpy(current
->output
+ slen
, str
, min
);
85 int32_t ctdb_control_get_event_script_status(struct ctdb_context
*ctdb
,
89 if (call_type
>= CTDB_EVENT_MAX
) {
93 if (ctdb
->last_status
[call_type
] == NULL
) {
94 /* If it's never been run, return nothing so they can tell. */
97 outdata
->dsize
= talloc_get_size(ctdb
->last_status
[call_type
]);
98 outdata
->dptr
= (uint8_t *)ctdb
->last_status
[call_type
];
103 /* To ignore directory entry return 0, else return non-zero */
104 static int script_filter(const struct dirent
*de
)
106 int namelen
= strlen(de
->d_name
);
108 /* Ignore . and .. */
113 /* Skip temporary files left behind by emacs */
114 if (de
->d_name
[namelen
-1] == '~') {
118 /* Filename should start with [0-9][0-9]. */
119 if (!isdigit(de
->d_name
[0]) || !isdigit(de
->d_name
[1]) ||
120 de
->d_name
[2] != '.') {
124 if (namelen
> MAX_SCRIPT_NAME
) {
131 /* Return true if OK, otherwise set errno. */
132 static bool check_executable(const char *dir
, const char *name
)
137 full
= talloc_asprintf(NULL
, "%s/%s", dir
, name
);
141 if (stat(full
, &st
) != 0) {
142 DEBUG(DEBUG_ERR
,("Could not stat event script %s: %s\n",
143 full
, strerror(errno
)));
148 if (!(st
.st_mode
& S_IXUSR
)) {
149 DEBUG(DEBUG_DEBUG
,("Event script %s is not executable. Ignoring this event script\n", full
));
159 static struct ctdb_scripts_wire
*ctdb_get_script_list(struct ctdb_context
*ctdb
, TALLOC_CTX
*mem_ctx
)
161 struct dirent
**namelist
;
162 struct ctdb_scripts_wire
*scripts
;
165 /* scan all directory entries and insert all valid scripts into the
168 count
= scandir(ctdb
->event_script_dir
, &namelist
, script_filter
, alphasort
);
170 DEBUG(DEBUG_CRIT
, ("Failed to read event script directory '%s' - %s\n",
171 ctdb
->event_script_dir
, strerror(errno
)));
175 /* Overallocates by one, but that's OK */
176 scripts
= talloc_zero_size(mem_ctx
,
178 + sizeof(scripts
->scripts
[0]) * count
);
179 if (scripts
== NULL
) {
180 DEBUG(DEBUG_ERR
, (__location__
" Failed to allocate scripts\n"));
184 scripts
->num_scripts
= count
;
186 for (count
= 0; count
< scripts
->num_scripts
; count
++) {
187 strcpy(scripts
->scripts
[count
].name
, namelist
[count
]->d_name
);
188 scripts
->scripts
[count
].status
= 0;
189 if (!check_executable(ctdb
->event_script_dir
, namelist
[count
]->d_name
)) {
190 scripts
->scripts
[count
].status
= -errno
;
199 /* There cannot be more than 10 arguments to command helper. */
200 #define MAX_HELPER_ARGS (10)
202 static bool child_helper_args(TALLOC_CTX
*mem_ctx
, struct ctdb_context
*ctdb
,
203 enum ctdb_eventscript_call call
,
205 struct ctdb_script_wire
*current
, int fd
,
206 int *argc
, const char ***argv
)
210 char *t
, *saveptr
, *opt
;
212 tmp
= talloc_array(mem_ctx
, const char *, 10+1);
213 if (tmp
== NULL
) goto failed
;
215 tmp
[0] = talloc_asprintf(tmp
, "%d", fd
);
216 tmp
[1] = talloc_asprintf(tmp
, "%s/%s", ctdb
->event_script_dir
, current
->name
);
217 tmp
[2] = talloc_asprintf(tmp
, "%s", ctdb_eventscript_call_names
[call
]);
220 /* Split options into individual arguments */
221 opt
= talloc_strdup(mem_ctx
, options
);
226 t
= strtok_r(opt
, " ", &saveptr
);
228 tmp
[n
++] = talloc_strdup(tmp
, t
);
229 if (n
> MAX_HELPER_ARGS
) {
232 t
= strtok_r(NULL
, " ", &saveptr
);
235 for (i
=0; i
<n
; i
++) {
236 if (tmp
[i
] == NULL
) {
241 /* Last argument should be NULL */
250 DEBUG(DEBUG_ERR
, (__location__
" too many arguments '%s' to eventscript '%s'\n",
251 options
, ctdb_eventscript_call_names
[call
]));
261 static void ctdb_event_script_handler(struct event_context
*ev
, struct fd_event
*fde
,
262 uint16_t flags
, void *p
);
264 static const char *helper_prog
= NULL
;
266 static int fork_child_for_script(struct ctdb_context
*ctdb
,
267 struct ctdb_event_script_state
*state
)
270 struct tevent_fd
*fde
;
271 struct ctdb_script_wire
*current
= get_current_script(state
);
274 static const char *helper
= BINDIR
"/ctdb_event_helper";
276 if (helper_prog
== NULL
) {
277 const char *t
= getenv("CTDB_EVENT_HELPER");
281 helper_prog
= helper
;
285 current
->start
= timeval_current();
289 DEBUG(DEBUG_ERR
, (__location__
" pipe failed for child eventscript process\n"));
293 /* Arguments for helper */
294 if (!child_helper_args(state
, ctdb
, state
->call
, state
->options
, current
,
295 state
->fd
[1], &argc
, &argv
)) {
296 DEBUG(DEBUG_ERR
, (__location__
" failed to create arguments for eventscript helper\n"));
303 if (!ctdb_vfork_with_logging(state
, ctdb
, current
->name
,
304 helper_prog
, argc
, argv
,
305 log_event_script_output
,
306 state
, &state
->child
)) {
317 set_close_on_exec(state
->fd
[0]);
319 /* Set ourselves up to be called when that's done. */
320 fde
= event_add_fd(ctdb
->ev
, state
, state
->fd
[0], EVENT_FD_READ
,
321 ctdb_event_script_handler
, state
);
322 tevent_fd_set_auto_close(fde
);
328 Summarize status of this run of scripts.
330 static int script_status(struct ctdb_scripts_wire
*scripts
)
334 for (i
= 0; i
< scripts
->num_scripts
; i
++) {
335 switch (scripts
->scripts
[i
].status
) {
338 /* Disabled or missing; that's OK. */
344 return scripts
->scripts
[i
].status
;
352 /* called when child is finished */
353 static void ctdb_event_script_handler(struct event_context
*ev
, struct fd_event
*fde
,
354 uint16_t flags
, void *p
)
356 struct ctdb_event_script_state
*state
=
357 talloc_get_type(p
, struct ctdb_event_script_state
);
358 struct ctdb_script_wire
*current
= get_current_script(state
);
359 struct ctdb_context
*ctdb
= state
->ctdb
;
363 DEBUG(DEBUG_ERR
,("Eventscript finished but ctdb is NULL\n"));
367 r
= read(state
->fd
[0], ¤t
->status
, sizeof(current
->status
));
369 current
->status
= -errno
;
370 } else if (r
!= sizeof(current
->status
)) {
371 current
->status
= -EIO
;
374 current
->finished
= timeval_current();
375 /* valgrind gets overloaded if we run next script as it's still doing
376 * post-execution analysis, so kill finished child here. */
377 if (ctdb
->valgrinding
) {
378 ctdb_kill(ctdb
, state
->child
, SIGKILL
);
383 status
= script_status(state
->scripts
);
385 /* Aborted or finished all scripts? We're done. */
386 if (status
!= 0 || state
->current
+1 == state
->scripts
->num_scripts
) {
387 DEBUG(DEBUG_INFO
,(__location__
" Eventscript %s %s finished with state %d\n",
388 ctdb_eventscript_call_names
[state
->call
], state
->options
, status
));
390 ctdb
->event_script_timeouts
= 0;
395 /* Forget about that old fd. */
401 current
->status
= fork_child_for_script(ctdb
, state
);
402 if (current
->status
!= 0) {
403 /* This calls the callback. */
408 struct debug_hung_script_state
{
409 struct ctdb_context
*ctdb
;
411 enum ctdb_eventscript_call call
;
414 static int debug_hung_script_state_destructor(struct debug_hung_script_state
*state
)
417 ctdb_kill(state
->ctdb
, state
->child
, SIGKILL
);
422 static void debug_hung_script_timeout(struct tevent_context
*ev
, struct tevent_timer
*te
,
423 struct timeval t
, void *p
)
425 struct debug_hung_script_state
*state
=
426 talloc_get_type(p
, struct debug_hung_script_state
);
431 static void debug_hung_script_done(struct tevent_context
*ev
, struct tevent_fd
*fde
,
432 uint16_t flags
, void *p
)
434 struct debug_hung_script_state
*state
=
435 talloc_get_type(p
, struct debug_hung_script_state
);
440 static void ctdb_run_debug_hung_script(struct ctdb_context
*ctdb
, struct debug_hung_script_state
*state
)
443 const char * debug_hung_script
= ETCDIR
"/ctdb/debug-hung-script.sh";
445 struct tevent_timer
*ttimer
;
446 struct tevent_fd
*tfd
;
450 if (helper_prog
== NULL
) {
455 DEBUG(DEBUG_ERR
,("Failed to create pipe fd for debug hung script\n"));
459 if (getenv("CTDB_DEBUG_HUNG_SCRIPT") != NULL
) {
460 debug_hung_script
= getenv("CTDB_DEBUG_HUNG_SCRIPT");
463 argv
= talloc_array(state
, const char *, 5);
465 argv
[0] = talloc_asprintf(argv
, "%d", fd
[1]);
466 argv
[1] = talloc_strdup(argv
, debug_hung_script
);
467 argv
[2] = talloc_asprintf(argv
, "%d", state
->child
);
468 argv
[3] = talloc_strdup(argv
, ctdb_eventscript_call_names
[state
->call
]);
471 for (i
=0; i
<4; i
++) {
472 if (argv
[i
] == NULL
) {
481 if (!ctdb_vfork_with_logging(state
, ctdb
, "Hung-script",
482 helper_prog
, 5, argv
, NULL
, NULL
, &pid
)) {
483 DEBUG(DEBUG_ERR
,("Failed to fork a child to track hung event script\n"));
493 ttimer
= tevent_add_timer(ctdb
->ev
, state
,
494 timeval_current_ofs(ctdb
->tunable
.script_timeout
, 0),
495 debug_hung_script_timeout
, state
);
496 if (ttimer
== NULL
) {
501 tfd
= tevent_add_fd(ctdb
->ev
, state
, fd
[0], EVENT_FD_READ
,
502 debug_hung_script_done
, state
);
508 tevent_fd_set_auto_close(tfd
);
511 /* called when child times out */
512 static void ctdb_event_script_timeout(struct event_context
*ev
, struct timed_event
*te
,
513 struct timeval t
, void *p
)
515 struct ctdb_event_script_state
*state
= talloc_get_type(p
, struct ctdb_event_script_state
);
516 struct ctdb_context
*ctdb
= state
->ctdb
;
517 struct ctdb_script_wire
*current
= get_current_script(state
);
518 struct debug_hung_script_state
*debug_state
;
520 DEBUG(DEBUG_ERR
,("Event script '%s %s %s' timed out after %.1fs, count: %u, pid: %d\n",
521 current
->name
, ctdb_eventscript_call_names
[state
->call
], state
->options
,
522 timeval_elapsed(¤t
->start
),
523 ctdb
->event_script_timeouts
, state
->child
));
525 /* ignore timeouts for these events */
526 switch (state
->call
) {
527 case CTDB_EVENT_START_RECOVERY
:
528 case CTDB_EVENT_RECOVERED
:
529 case CTDB_EVENT_TAKE_IP
:
530 case CTDB_EVENT_RELEASE_IP
:
531 state
->scripts
->scripts
[state
->current
].status
= 0;
532 DEBUG(DEBUG_ERR
,("Ignoring hung script for %s call %d\n", state
->options
, state
->call
));
535 state
->scripts
->scripts
[state
->current
].status
= -ETIME
;
538 debug_state
= talloc_zero(ctdb
, struct debug_hung_script_state
);
539 if (debug_state
== NULL
) {
544 /* Save information useful for running debug hung script, so
545 * eventscript state can be freed.
547 debug_state
->ctdb
= ctdb
;
548 debug_state
->child
= state
->child
;
549 debug_state
->call
= state
->call
;
551 /* This destructor will actually kill the hung event script */
552 talloc_set_destructor(debug_state
, debug_hung_script_state_destructor
);
557 ctdb_run_debug_hung_script(ctdb
, debug_state
);
561 destroy an event script: kill it if ->child != 0.
563 static int event_script_destructor(struct ctdb_event_script_state
*state
)
566 struct event_script_callback
*callback
;
569 DEBUG(DEBUG_ERR
,(__location__
" Sending SIGTERM to child pid:%d\n", state
->child
));
571 if (ctdb_kill(state
->ctdb
, state
->child
, SIGTERM
) != 0) {
572 DEBUG(DEBUG_ERR
,("Failed to kill child process for eventscript, errno %s(%d)\n", strerror(errno
), errno
));
576 /* If we were the current monitor, we no longer are. */
577 if (state
->ctdb
->current_monitor
== state
) {
578 state
->ctdb
->current_monitor
= NULL
;
581 /* Save our scripts as the last executed status, if we have them.
582 * See ctdb_event_script_callback_v where we abort monitor event. */
583 if (state
->scripts
) {
584 talloc_free(state
->ctdb
->last_status
[state
->call
]);
585 state
->ctdb
->last_status
[state
->call
] = state
->scripts
;
586 if (state
->current
< state
->ctdb
->last_status
[state
->call
]->num_scripts
) {
587 state
->ctdb
->last_status
[state
->call
]->num_scripts
= state
->current
+1;
591 /* Use last status as result, or "OK" if none. */
592 if (state
->ctdb
->last_status
[state
->call
]) {
593 status
= script_status(state
->ctdb
->last_status
[state
->call
]);
598 state
->ctdb
->active_events
--;
599 if (state
->ctdb
->active_events
< 0) {
600 ctdb_fatal(state
->ctdb
, "Active events < 0");
603 /* This is allowed to free us; talloc will prevent double free anyway,
604 * but beware if you call this outside the destructor!
605 * the callback hangs off a different context so we walk the list
606 * of "active" callbacks until we find the one state points to.
607 * if we cant find it it means the callback has been removed.
609 for (callback
= state
->ctdb
->script_callbacks
; callback
!= NULL
; callback
= callback
->next
) {
610 if (callback
== state
->callback
) {
615 state
->callback
= NULL
;
618 /* Make sure destructor doesn't free itself! */
619 talloc_steal(NULL
, callback
);
620 callback
->fn(state
->ctdb
, status
, callback
->private_data
);
621 talloc_free(callback
);
627 static unsigned int count_words(const char *options
)
629 unsigned int words
= 0;
631 options
+= strspn(options
, " \t");
634 options
+= strcspn(options
, " \t");
635 options
+= strspn(options
, " \t");
640 static bool check_options(enum ctdb_eventscript_call call
, const char *options
)
643 /* These all take no arguments. */
644 case CTDB_EVENT_INIT
:
645 case CTDB_EVENT_SETUP
:
646 case CTDB_EVENT_STARTUP
:
647 case CTDB_EVENT_START_RECOVERY
:
648 case CTDB_EVENT_RECOVERED
:
649 case CTDB_EVENT_MONITOR
:
650 case CTDB_EVENT_SHUTDOWN
:
651 case CTDB_EVENT_IPREALLOCATED
:
652 return count_words(options
) == 0;
654 case CTDB_EVENT_TAKE_IP
: /* interface, IP address, netmask bits. */
655 case CTDB_EVENT_RELEASE_IP
:
656 return count_words(options
) == 3;
658 case CTDB_EVENT_UPDATE_IP
: /* old interface, new interface, IP address, netmask bits. */
659 return count_words(options
) == 4;
662 DEBUG(DEBUG_ERR
,(__location__
"Unknown ctdb_eventscript_call %u\n", call
));
667 static int remove_callback(struct event_script_callback
*callback
)
669 DLIST_REMOVE(callback
->ctdb
->script_callbacks
, callback
);
674 run the event script in the background, calling the callback when
677 static int ctdb_event_script_callback_v(struct ctdb_context
*ctdb
,
679 void (*callback
)(struct ctdb_context
*, int, void *),
681 enum ctdb_eventscript_call call
,
682 const char *fmt
, va_list ap
)
684 struct ctdb_event_script_state
*state
;
686 if (ctdb
->recovery_mode
!= CTDB_RECOVERY_NORMAL
) {
687 /* we guarantee that only some specifically allowed event scripts are run
689 const enum ctdb_eventscript_call allowed_calls
[] = {
692 CTDB_EVENT_START_RECOVERY
,
694 CTDB_EVENT_RELEASE_IP
,
695 CTDB_EVENT_IPREALLOCATED
,
698 for (i
=0;i
<ARRAY_SIZE(allowed_calls
);i
++) {
699 if (call
== allowed_calls
[i
]) break;
701 if (i
== ARRAY_SIZE(allowed_calls
)) {
702 DEBUG(DEBUG_ERR
,("Refusing to run event scripts call '%s' while in recovery\n",
703 ctdb_eventscript_call_names
[call
]));
708 /* Do not run new monitor events if some event is already running */
709 if (call
== CTDB_EVENT_MONITOR
&& ctdb
->active_events
> 0) {
710 if (callback
!= NULL
) {
711 callback(ctdb
, -ECANCELED
, private_data
);
716 /* Kill off any running monitor events to run this event. */
717 if (ctdb
->current_monitor
) {
718 struct ctdb_event_script_state
*ms
= talloc_get_type(ctdb
->current_monitor
, struct ctdb_event_script_state
);
720 /* Cancel current monitor callback state only if monitoring
721 * context ctdb->monitor->monitor_context has not been freed */
722 if (ms
->callback
!= NULL
&& !ctdb_stopped_monitoring(ctdb
)) {
723 ms
->callback
->fn(ctdb
, -ECANCELED
, ms
->callback
->private_data
);
724 talloc_free(ms
->callback
);
727 /* Discard script status so we don't save to last_status */
728 talloc_free(ctdb
->current_monitor
->scripts
);
729 ctdb
->current_monitor
->scripts
= NULL
;
730 talloc_free(ctdb
->current_monitor
);
731 ctdb
->current_monitor
= NULL
;
734 state
= talloc(ctdb
->event_script_ctx
, struct ctdb_event_script_state
);
735 CTDB_NO_MEMORY(ctdb
, state
);
737 /* The callback isn't done if the context is freed. */
738 state
->callback
= talloc(mem_ctx
, struct event_script_callback
);
739 CTDB_NO_MEMORY(ctdb
, state
->callback
);
740 DLIST_ADD(ctdb
->script_callbacks
, state
->callback
);
741 talloc_set_destructor(state
->callback
, remove_callback
);
742 state
->callback
->ctdb
= ctdb
;
743 state
->callback
->fn
= callback
;
744 state
->callback
->private_data
= private_data
;
748 state
->options
= talloc_vasprintf(state
, fmt
, ap
);
749 state
->timeout
= timeval_set(ctdb
->tunable
.script_timeout
, 0);
750 state
->scripts
= NULL
;
751 if (state
->options
== NULL
) {
752 DEBUG(DEBUG_ERR
, (__location__
" could not allocate state->options\n"));
756 if (!check_options(state
->call
, state
->options
)) {
757 DEBUG(DEBUG_ERR
, ("Bad eventscript options '%s' for %s\n",
758 ctdb_eventscript_call_names
[state
->call
], state
->options
));
763 DEBUG(DEBUG_INFO
,(__location__
" Starting eventscript %s %s\n",
764 ctdb_eventscript_call_names
[state
->call
],
767 /* This is not a child of state, since we save it in destructor. */
768 state
->scripts
= ctdb_get_script_list(ctdb
, ctdb
);
769 if (state
->scripts
== NULL
) {
776 if (call
== CTDB_EVENT_MONITOR
) {
777 ctdb
->current_monitor
= state
;
780 talloc_set_destructor(state
, event_script_destructor
);
782 ctdb
->active_events
++;
785 if (state
->scripts
->num_scripts
== 0) {
790 state
->scripts
->scripts
[0].status
= fork_child_for_script(ctdb
, state
);
791 if (state
->scripts
->scripts
[0].status
!= 0) {
792 /* Callback is called from destructor, with fail result. */
797 if (!timeval_is_zero(&state
->timeout
)) {
798 event_add_timed(ctdb
->ev
, state
, timeval_current_ofs(state
->timeout
.tv_sec
, state
->timeout
.tv_usec
), ctdb_event_script_timeout
, state
);
800 DEBUG(DEBUG_ERR
, (__location__
" eventscript %s %s called with no timeout\n",
801 ctdb_eventscript_call_names
[state
->call
],
810 run the event script in the background, calling the callback when
811 finished. If mem_ctx is freed, callback will never be called.
813 int ctdb_event_script_callback(struct ctdb_context
*ctdb
,
815 void (*callback
)(struct ctdb_context
*, int, void *),
817 enum ctdb_eventscript_call call
,
818 const char *fmt
, ...)
824 ret
= ctdb_event_script_callback_v(ctdb
, mem_ctx
, callback
, private_data
, call
, fmt
, ap
);
831 struct callback_status
{
837 called when ctdb_event_script() finishes
839 static void event_script_callback(struct ctdb_context
*ctdb
, int status
, void *private_data
)
841 struct callback_status
*s
= (struct callback_status
*)private_data
;
847 run the event script, waiting for it to complete. Used when the caller
848 doesn't want to continue till the event script has finished.
850 int ctdb_event_script_args(struct ctdb_context
*ctdb
, enum ctdb_eventscript_call call
,
851 const char *fmt
, ...)
855 struct callback_status status
;
858 ret
= ctdb_event_script_callback_v(ctdb
, ctdb
,
859 event_script_callback
, &status
, call
, fmt
, ap
);
868 while (status
.done
== false && event_loop_once(ctdb
->ev
) == 0) /* noop */;
870 if (status
.status
== -ETIME
) {
871 DEBUG(DEBUG_ERR
, (__location__
" eventscript for '%s' timedout."
872 " Immediately banning ourself for %d seconds\n",
873 ctdb_eventscript_call_names
[call
],
874 ctdb
->tunable
.recovery_ban_period
));
876 /* Don't ban self if CTDB is starting up or shutting down */
877 if (call
!= CTDB_EVENT_INIT
&& call
!= CTDB_EVENT_SHUTDOWN
) {
882 return status
.status
;
885 int ctdb_event_script(struct ctdb_context
*ctdb
, enum ctdb_eventscript_call call
)
887 /* GCC complains about empty format string, so use %s and "". */
888 return ctdb_event_script_args(ctdb
, call
, "%s", "");
891 struct eventscript_callback_state
{
892 struct ctdb_req_control
*c
;
896 called when a forced eventscript run has finished
898 static void run_eventscripts_callback(struct ctdb_context
*ctdb
, int status
,
901 struct eventscript_callback_state
*state
=
902 talloc_get_type(private_data
, struct eventscript_callback_state
);
905 DEBUG(DEBUG_ERR
,(__location__
" Failed to run eventscripts\n"));
908 ctdb_request_control_reply(ctdb
, state
->c
, NULL
, status
, NULL
);
909 /* This will free the struct ctdb_event_script_state we are in! */
915 /* Returns rest of string, or NULL if no match. */
916 static const char *get_call(const char *p
, enum ctdb_eventscript_call
*call
)
920 /* Skip any initial whitespace. */
921 p
+= strspn(p
, " \t");
923 /* See if we match any. */
924 for (*call
= 0; *call
< CTDB_EVENT_MAX
; (*call
)++) {
925 len
= strlen(ctdb_eventscript_call_names
[*call
]);
926 if (strncmp(p
, ctdb_eventscript_call_names
[*call
], len
) == 0) {
927 /* If end of string or whitespace, we're done. */
928 if (strcspn(p
+ len
, " \t") == 0) {
937 A control to force running of the eventscripts from the ctdb client tool
939 int32_t ctdb_run_eventscripts(struct ctdb_context
*ctdb
,
940 struct ctdb_req_control
*c
,
941 TDB_DATA indata
, bool *async_reply
)
944 struct eventscript_callback_state
*state
;
946 enum ctdb_eventscript_call call
;
948 /* Figure out what call they want. */
949 options
= get_call((const char *)indata
.dptr
, &call
);
951 DEBUG(DEBUG_ERR
, (__location__
" Invalid event name \"%s\"\n", (const char *)indata
.dptr
));
955 if (ctdb
->recovery_mode
!= CTDB_RECOVERY_NORMAL
) {
956 DEBUG(DEBUG_ERR
, (__location__
" Aborted running eventscript \"%s\" while in RECOVERY mode\n", indata
.dptr
));
960 state
= talloc(ctdb
->event_script_ctx
, struct eventscript_callback_state
);
961 CTDB_NO_MEMORY(ctdb
, state
);
963 state
->c
= talloc_steal(state
, c
);
965 DEBUG(DEBUG_NOTICE
,("Running eventscripts with arguments %s\n", indata
.dptr
));
967 ret
= ctdb_event_script_callback(ctdb
,
968 state
, run_eventscripts_callback
, state
,
969 call
, "%s", options
);
972 DEBUG(DEBUG_ERR
,(__location__
" Failed to run eventscripts with arguments %s\n", indata
.dptr
));
977 /* tell ctdb_control.c that we will be replying asynchronously */
985 int32_t ctdb_control_enable_script(struct ctdb_context
*ctdb
, TDB_DATA indata
)
990 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
992 script
= (char *)indata
.dptr
;
993 if (indata
.dsize
== 0) {
994 DEBUG(DEBUG_ERR
,(__location__
" No script specified.\n"));
995 talloc_free(tmp_ctx
);
998 if (indata
.dptr
[indata
.dsize
- 1] != '\0') {
999 DEBUG(DEBUG_ERR
,(__location__
" String is not null terminated.\n"));
1000 talloc_free(tmp_ctx
);
1003 if (index(script
,'/') != NULL
) {
1004 DEBUG(DEBUG_ERR
,(__location__
" Script name contains '/'. Failed to enable script %s\n", script
));
1005 talloc_free(tmp_ctx
);
1010 if (stat(ctdb
->event_script_dir
, &st
) != 0 &&
1012 DEBUG(DEBUG_CRIT
,("No event script directory found at '%s'\n", ctdb
->event_script_dir
));
1013 talloc_free(tmp_ctx
);
1018 filename
= talloc_asprintf(tmp_ctx
, "%s/%s", ctdb
->event_script_dir
, script
);
1019 if (filename
== NULL
) {
1020 DEBUG(DEBUG_ERR
,(__location__
" Failed to create script path\n"));
1021 talloc_free(tmp_ctx
);
1025 if (stat(filename
, &st
) != 0) {
1026 DEBUG(DEBUG_ERR
,("Could not stat event script %s. Failed to enable script.\n", filename
));
1027 talloc_free(tmp_ctx
);
1031 if (chmod(filename
, st
.st_mode
| S_IXUSR
) == -1) {
1032 DEBUG(DEBUG_ERR
,("Could not chmod %s. Failed to enable script.\n", filename
));
1033 talloc_free(tmp_ctx
);
1037 talloc_free(tmp_ctx
);
1041 int32_t ctdb_control_disable_script(struct ctdb_context
*ctdb
, TDB_DATA indata
)
1046 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
1048 script
= (char *)indata
.dptr
;
1049 if (indata
.dsize
== 0) {
1050 DEBUG(DEBUG_ERR
,(__location__
" No script specified.\n"));
1051 talloc_free(tmp_ctx
);
1054 if (indata
.dptr
[indata
.dsize
- 1] != '\0') {
1055 DEBUG(DEBUG_ERR
,(__location__
" String is not null terminated.\n"));
1056 talloc_free(tmp_ctx
);
1059 if (index(script
,'/') != NULL
) {
1060 DEBUG(DEBUG_ERR
,(__location__
" Script name contains '/'. Failed to disable script %s\n", script
));
1061 talloc_free(tmp_ctx
);
1066 if (stat(ctdb
->event_script_dir
, &st
) != 0 &&
1068 DEBUG(DEBUG_CRIT
,("No event script directory found at '%s'\n", ctdb
->event_script_dir
));
1069 talloc_free(tmp_ctx
);
1074 filename
= talloc_asprintf(tmp_ctx
, "%s/%s", ctdb
->event_script_dir
, script
);
1075 if (filename
== NULL
) {
1076 DEBUG(DEBUG_ERR
,(__location__
" Failed to create script path\n"));
1077 talloc_free(tmp_ctx
);
1081 if (stat(filename
, &st
) != 0) {
1082 DEBUG(DEBUG_ERR
,("Could not stat event script %s. Failed to disable script.\n", filename
));
1083 talloc_free(tmp_ctx
);
1087 if (chmod(filename
, st
.st_mode
& ~(S_IXUSR
|S_IXGRP
|S_IXOTH
)) == -1) {
1088 DEBUG(DEBUG_ERR
,("Could not chmod %s. Failed to disable script.\n", filename
));
1089 talloc_free(tmp_ctx
);
1093 talloc_free(tmp_ctx
);