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 struct ctdb_script_tree_item {
108 /* Return true if OK, otherwise set errno. */
109 static bool check_executable(const char *dir, const char *name)
114 full = talloc_asprintf(NULL, "%s/%s", dir, name);
118 if (stat(full, &st) != 0) {
119 DEBUG(DEBUG_ERR,("Could not stat event script %s: %s\n",
120 full, strerror(errno)));
125 if (!(st.st_mode & S_IXUSR)) {
126 DEBUG(DEBUG_DEBUG,("Event script %s is not executable. Ignoring this event script\n", full));
136 static struct ctdb_scripts_wire *ctdb_get_script_list(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx)
142 struct ctdb_scripts_wire *scripts;
143 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
144 struct ctdb_script_tree_item *tree_item;
148 the service specific event scripts
150 if (stat(ctdb->event_script_dir, &st) != 0 &&
152 DEBUG(DEBUG_CRIT,("No event script directory found at '%s'\n", ctdb->event_script_dir));
153 talloc_free(tmp_ctx);
157 /* create a tree to store all the script names in */
158 tree = trbt_create(tmp_ctx, 0);
160 /* scan all directory entries and insert all valid scripts into the
163 dir = opendir(ctdb->event_script_dir);
165 DEBUG(DEBUG_CRIT,("Failed to open event script directory '%s'\n", ctdb->event_script_dir));
166 talloc_free(tmp_ctx);
171 while ((de=readdir(dir)) != NULL) {
175 namlen = strlen(de->d_name);
181 if (de->d_name[namlen-1] == '~') {
182 /* skip files emacs left behind */
186 if (de->d_name[2] != '.') {
190 if (sscanf(de->d_name, "%02u.", &num) != 1) {
194 if (strlen(de->d_name) > MAX_SCRIPT_NAME) {
195 DEBUG(DEBUG_ERR,("Script name %s too long! %u chars max",
196 de->d_name, MAX_SCRIPT_NAME));
200 tree_item = talloc(tree, struct ctdb_script_tree_item);
201 if (tree_item == NULL) {
202 DEBUG(DEBUG_ERR, (__location__ " Failed to allocate new tree item\n"));
204 talloc_free(tmp_ctx);
208 tree_item->error = 0;
209 if (!check_executable(ctdb->event_script_dir, de->d_name)) {
210 tree_item->error = errno;
213 tree_item->name = talloc_strdup(tree_item, de->d_name);
214 if (tree_item->name == NULL) {
215 DEBUG(DEBUG_ERR,(__location__ " Failed to allocate script name.\n"));
217 talloc_free(tmp_ctx);
221 /* store the event script in the tree */
222 trbt_insert32(tree, (num<<16)|count++, tree_item);
226 /* Overallocates by one, but that's OK */
227 scripts = talloc_zero_size(tmp_ctx,
229 + sizeof(scripts->scripts[0]) * count);
230 if (scripts == NULL) {
231 DEBUG(DEBUG_ERR, (__location__ " Failed to allocate scripts\n"));
232 talloc_free(tmp_ctx);
235 scripts->num_scripts = count;
237 for (count = 0; count < scripts->num_scripts; count++) {
238 tree_item = trbt_findfirstarray32(tree, 1);
240 strcpy(scripts->scripts[count].name, tree_item->name);
241 scripts->scripts[count].status = -tree_item->error;
243 /* remove this script from the tree */
244 talloc_free(tree_item);
247 talloc_steal(mem_ctx, scripts);
248 talloc_free(tmp_ctx);
253 /* There cannot be more than 10 arguments to command helper. */
254 #define MAX_HELPER_ARGS (10)
256 static bool child_helper_args(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
257 enum ctdb_eventscript_call call,
259 struct ctdb_script_wire *current, int fd,
260 int *argc, const char ***argv)
264 char *t, *saveptr, *opt;
266 tmp = talloc_array(mem_ctx, const char *, 10+1);
267 if (tmp == NULL) goto failed;
269 tmp[0] = talloc_asprintf(tmp, "%d", fd);
270 tmp[1] = talloc_asprintf(tmp, "%s/%s", ctdb->event_script_dir, current->name);
271 tmp[2] = talloc_asprintf(tmp, "%s", ctdb_eventscript_call_names[call]);
274 /* Split options into individual arguments */
275 opt = talloc_strdup(mem_ctx, options);
280 t = strtok_r(opt, " ", &saveptr);
282 tmp[n++] = talloc_strdup(tmp, t);
283 if (n > MAX_HELPER_ARGS) {
286 t = strtok_r(NULL, " ", &saveptr);
289 for (i=0; i<n; i++) {
290 if (tmp[i] == NULL) {
295 /* Last argument should be NULL */
304 DEBUG(DEBUG_ERR, (__location__ " too many arguments '%s' to eventscript '%s'\n",
305 options, ctdb_eventscript_call_names[call]));
315 static void ctdb_event_script_handler(struct event_context *ev, struct fd_event *fde,
316 uint16_t flags, void *p);
318 static const char *helper_prog = NULL;
320 static int fork_child_for_script(struct ctdb_context *ctdb,
321 struct ctdb_event_script_state *state)
324 struct tevent_fd *fde;
325 struct ctdb_script_wire *current = get_current_script(state);
328 static const char *helper = BINDIR "/ctdb_event_helper";
330 if (helper_prog == NULL) {
331 const char *t = getenv("CTDB_EVENT_HELPER");
335 helper_prog = helper;
339 current->start = timeval_current();
343 DEBUG(DEBUG_ERR, (__location__ " pipe failed for child eventscript process\n"));
347 /* Arguments for helper */
348 if (!child_helper_args(state, ctdb, state->call, state->options, current,
349 state->fd[1], &argc, &argv)) {
350 DEBUG(DEBUG_ERR, (__location__ " failed to create arguments for eventscript helper\n"));
357 if (!ctdb_vfork_with_logging(state, ctdb, current->name,
358 helper_prog, argc, argv,
359 log_event_script_output,
360 state, &state->child)) {
371 set_close_on_exec(state->fd[0]);
373 /* Set ourselves up to be called when that's done. */
374 fde = event_add_fd(ctdb->ev, state, state->fd[0], EVENT_FD_READ,
375 ctdb_event_script_handler, state);
376 tevent_fd_set_auto_close(fde);
382 Summarize status of this run of scripts.
384 static int script_status(struct ctdb_scripts_wire *scripts)
388 for (i = 0; i < scripts->num_scripts; i++) {
389 switch (scripts->scripts[i].status) {
392 /* Disabled or missing; that's OK. */
398 return scripts->scripts[i].status;
406 /* called when child is finished */
407 static void ctdb_event_script_handler(struct event_context *ev, struct fd_event *fde,
408 uint16_t flags, void *p)
410 struct ctdb_event_script_state *state =
411 talloc_get_type(p, struct ctdb_event_script_state);
412 struct ctdb_script_wire *current = get_current_script(state);
413 struct ctdb_context *ctdb = state->ctdb;
417 DEBUG(DEBUG_ERR,("Eventscript finished but ctdb is NULL\n"));
421 r = read(state->fd[0], ¤t->status, sizeof(current->status));
423 current->status = -errno;
424 } else if (r != sizeof(current->status)) {
425 current->status = -EIO;
428 current->finished = timeval_current();
429 /* valgrind gets overloaded if we run next script as it's still doing
430 * post-execution analysis, so kill finished child here. */
431 if (ctdb->valgrinding) {
432 ctdb_kill(ctdb, state->child, SIGKILL);
437 status = script_status(state->scripts);
439 /* Aborted or finished all scripts? We're done. */
440 if (status != 0 || state->current+1 == state->scripts->num_scripts) {
441 DEBUG(DEBUG_INFO,(__location__ " Eventscript %s %s finished with state %d\n",
442 ctdb_eventscript_call_names[state->call], state->options, status));
444 ctdb->event_script_timeouts = 0;
449 /* Forget about that old fd. */
455 current->status = fork_child_for_script(ctdb, state);
456 if (current->status != 0) {
457 /* This calls the callback. */
462 struct debug_hung_script_state {
463 struct ctdb_context *ctdb;
465 enum ctdb_eventscript_call call;
468 static int debug_hung_script_state_destructor(struct debug_hung_script_state *state)
471 ctdb_kill(state->ctdb, state->child, SIGKILL);
476 static void debug_hung_script_timeout(struct tevent_context *ev, struct tevent_timer *te,
477 struct timeval t, void *p)
479 struct debug_hung_script_state *state =
480 talloc_get_type(p, struct debug_hung_script_state);
485 static void debug_hung_script_done(struct tevent_context *ev, struct tevent_fd *fde,
486 uint16_t flags, void *p)
488 struct debug_hung_script_state *state =
489 talloc_get_type(p, struct debug_hung_script_state);
494 static void ctdb_run_debug_hung_script(struct ctdb_context *ctdb, struct debug_hung_script_state *state)
497 const char * debug_hung_script = ETCDIR "/ctdb/debug-hung-script.sh";
499 struct tevent_timer *ttimer;
500 struct tevent_fd *tfd;
504 if (helper_prog == NULL) {
509 DEBUG(DEBUG_ERR,("Failed to create pipe fd for debug hung script\n"));
513 argv = talloc_array(state, const char *, 5);
515 argv[0] = talloc_asprintf(argv, "%d", fd[1]);
516 argv[1] = talloc_strdup(argv, debug_hung_script);
517 argv[2] = talloc_asprintf(argv, "%d", state->child);
518 argv[3] = talloc_strdup(argv, ctdb_eventscript_call_names[state->call]);
521 for (i=0; i<4; i++) {
522 if (argv[i] == NULL) {
531 if (!ctdb_vfork_with_logging(state, ctdb, "Hung-script",
532 helper_prog, 5, argv, NULL, NULL, &pid)) {
533 DEBUG(DEBUG_ERR,("Failed to fork a child to track hung event script\n"));
543 ttimer = tevent_add_timer(ctdb->ev, state,
544 timeval_current_ofs(ctdb->tunable.script_timeout, 0),
545 debug_hung_script_timeout, state);
546 if (ttimer == NULL) {
551 tfd = tevent_add_fd(ctdb->ev, state, fd[0], EVENT_FD_READ,
552 debug_hung_script_done, state);
558 tevent_fd_set_auto_close(tfd);
561 /* called when child times out */
562 static void ctdb_event_script_timeout(struct event_context *ev, struct timed_event *te,
563 struct timeval t, void *p)
565 struct ctdb_event_script_state *state = talloc_get_type(p, struct ctdb_event_script_state);
566 struct ctdb_context *ctdb = state->ctdb;
567 struct ctdb_script_wire *current = get_current_script(state);
568 struct debug_hung_script_state *debug_state;
570 DEBUG(DEBUG_ERR,("Event script '%s %s %s' timed out after %.1fs, count: %u, pid: %d\n",
571 current->name, ctdb_eventscript_call_names[state->call], state->options,
572 timeval_elapsed(¤t->start),
573 ctdb->event_script_timeouts, state->child));
575 /* ignore timeouts for these events */
576 switch (state->call) {
577 case CTDB_EVENT_START_RECOVERY:
578 case CTDB_EVENT_RECOVERED:
579 case CTDB_EVENT_TAKE_IP:
580 case CTDB_EVENT_RELEASE_IP:
581 state->scripts->scripts[state->current].status = 0;
582 DEBUG(DEBUG_ERR,("Ignoring hung script for %s call %d\n", state->options, state->call));
585 state->scripts->scripts[state->current].status = -ETIME;
588 debug_state = talloc_zero(ctdb, struct debug_hung_script_state);
589 if (debug_state == NULL) {
594 /* Save information useful for running debug hung script, so
595 * eventscript state can be freed.
597 debug_state->ctdb = ctdb;
598 debug_state->child = state->child;
599 debug_state->call = state->call;
601 /* This destructor will actually kill the hung event script */
602 talloc_set_destructor(debug_state, debug_hung_script_state_destructor);
607 ctdb_run_debug_hung_script(ctdb, debug_state);
611 destroy an event script: kill it if ->child != 0.
613 static int event_script_destructor(struct ctdb_event_script_state *state)
616 struct event_script_callback *callback;
619 DEBUG(DEBUG_ERR,(__location__ " Sending SIGTERM to child pid:%d\n", state->child));
621 if (ctdb_kill(state->ctdb, state->child, SIGTERM) != 0) {
622 DEBUG(DEBUG_ERR,("Failed to kill child process for eventscript, errno %s(%d)\n", strerror(errno), errno));
626 /* If we were the current monitor, we no longer are. */
627 if (state->ctdb->current_monitor == state) {
628 state->ctdb->current_monitor = NULL;
631 /* Save our scripts as the last executed status, if we have them.
632 * See ctdb_event_script_callback_v where we abort monitor event. */
633 if (state->scripts) {
634 talloc_free(state->ctdb->last_status[state->call]);
635 state->ctdb->last_status[state->call] = state->scripts;
636 if (state->current < state->ctdb->last_status[state->call]->num_scripts) {
637 state->ctdb->last_status[state->call]->num_scripts = state->current+1;
641 /* Use last status as result, or "OK" if none. */
642 if (state->ctdb->last_status[state->call]) {
643 status = script_status(state->ctdb->last_status[state->call]);
648 state->ctdb->active_events--;
649 if (state->ctdb->active_events < 0) {
650 ctdb_fatal(state->ctdb, "Active events < 0");
653 /* This is allowed to free us; talloc will prevent double free anyway,
654 * but beware if you call this outside the destructor!
655 * the callback hangs off a different context so we walk the list
656 * of "active" callbacks until we find the one state points to.
657 * if we cant find it it means the callback has been removed.
659 for (callback = state->ctdb->script_callbacks; callback != NULL; callback = callback->next) {
660 if (callback == state->callback) {
665 state->callback = NULL;
668 /* Make sure destructor doesn't free itself! */
669 talloc_steal(NULL, callback);
670 callback->fn(state->ctdb, status, callback->private_data);
671 talloc_free(callback);
677 static unsigned int count_words(const char *options)
679 unsigned int words = 0;
681 options += strspn(options, " \t");
684 options += strcspn(options, " \t");
685 options += strspn(options, " \t");
690 static bool check_options(enum ctdb_eventscript_call call, const char *options)
693 /* These all take no arguments. */
694 case CTDB_EVENT_INIT:
695 case CTDB_EVENT_SETUP:
696 case CTDB_EVENT_STARTUP:
697 case CTDB_EVENT_START_RECOVERY:
698 case CTDB_EVENT_RECOVERED:
699 case CTDB_EVENT_MONITOR:
700 case CTDB_EVENT_SHUTDOWN:
701 case CTDB_EVENT_IPREALLOCATED:
702 return count_words(options) == 0;
704 case CTDB_EVENT_TAKE_IP: /* interface, IP address, netmask bits. */
705 case CTDB_EVENT_RELEASE_IP:
706 return count_words(options) == 3;
708 case CTDB_EVENT_UPDATE_IP: /* old interface, new interface, IP address, netmask bits. */
709 return count_words(options) == 4;
712 DEBUG(DEBUG_ERR,(__location__ "Unknown ctdb_eventscript_call %u\n", call));
717 static int remove_callback(struct event_script_callback *callback)
719 DLIST_REMOVE(callback->ctdb->script_callbacks, callback);
724 run the event script in the background, calling the callback when
727 static int ctdb_event_script_callback_v(struct ctdb_context *ctdb,
729 void (*callback)(struct ctdb_context *, int, void *),
731 enum ctdb_eventscript_call call,
732 const char *fmt, va_list ap)
734 struct ctdb_event_script_state *state;
736 if (ctdb->recovery_mode != CTDB_RECOVERY_NORMAL) {
737 /* we guarantee that only some specifically allowed event scripts are run
739 const enum ctdb_eventscript_call allowed_calls[] = {
742 CTDB_EVENT_START_RECOVERY,
744 CTDB_EVENT_RELEASE_IP,
745 CTDB_EVENT_IPREALLOCATED,
748 for (i=0;i<ARRAY_SIZE(allowed_calls);i++) {
749 if (call == allowed_calls[i]) break;
751 if (i == ARRAY_SIZE(allowed_calls)) {
752 DEBUG(DEBUG_ERR,("Refusing to run event scripts call '%s' while in recovery\n",
753 ctdb_eventscript_call_names[call]));
758 /* Do not run new monitor events if some event is already running */
759 if (call == CTDB_EVENT_MONITOR && ctdb->active_events > 0) {
760 if (callback != NULL) {
761 callback(ctdb, -ECANCELED, private_data);
766 /* Kill off any running monitor events to run this event. */
767 if (ctdb->current_monitor) {
768 struct ctdb_event_script_state *ms = talloc_get_type(ctdb->current_monitor, struct ctdb_event_script_state);
770 /* Cancel current monitor callback state only if monitoring
771 * context ctdb->monitor->monitor_context has not been freed */
772 if (ms->callback != NULL && !ctdb_stopped_monitoring(ctdb)) {
773 ms->callback->fn(ctdb, -ECANCELED, ms->callback->private_data);
774 talloc_free(ms->callback);
777 /* Discard script status so we don't save to last_status */
778 talloc_free(ctdb->current_monitor->scripts);
779 ctdb->current_monitor->scripts = NULL;
780 talloc_free(ctdb->current_monitor);
781 ctdb->current_monitor = NULL;
784 state = talloc(ctdb->event_script_ctx, struct ctdb_event_script_state);
785 CTDB_NO_MEMORY(ctdb, state);
787 /* The callback isn't done if the context is freed. */
788 state->callback = talloc(mem_ctx, struct event_script_callback);
789 CTDB_NO_MEMORY(ctdb, state->callback);
790 DLIST_ADD(ctdb->script_callbacks, state->callback);
791 talloc_set_destructor(state->callback, remove_callback);
792 state->callback->ctdb = ctdb;
793 state->callback->fn = callback;
794 state->callback->private_data = private_data;
798 state->options = talloc_vasprintf(state, fmt, ap);
799 state->timeout = timeval_set(ctdb->tunable.script_timeout, 0);
800 state->scripts = NULL;
801 if (state->options == NULL) {
802 DEBUG(DEBUG_ERR, (__location__ " could not allocate state->options\n"));
806 if (!check_options(state->call, state->options)) {
807 DEBUG(DEBUG_ERR, ("Bad eventscript options '%s' for %s\n",
808 ctdb_eventscript_call_names[state->call], state->options));
813 DEBUG(DEBUG_INFO,(__location__ " Starting eventscript %s %s\n",
814 ctdb_eventscript_call_names[state->call],
817 /* This is not a child of state, since we save it in destructor. */
818 state->scripts = ctdb_get_script_list(ctdb, ctdb);
819 if (state->scripts == NULL) {
826 if (call == CTDB_EVENT_MONITOR) {
827 ctdb->current_monitor = state;
830 talloc_set_destructor(state, event_script_destructor);
832 ctdb->active_events++;
835 if (state->scripts->num_scripts == 0) {
840 state->scripts->scripts[0].status = fork_child_for_script(ctdb, state);
841 if (state->scripts->scripts[0].status != 0) {
842 /* Callback is called from destructor, with fail result. */
847 if (!timeval_is_zero(&state->timeout)) {
848 event_add_timed(ctdb->ev, state, timeval_current_ofs(state->timeout.tv_sec, state->timeout.tv_usec), ctdb_event_script_timeout, state);
850 DEBUG(DEBUG_ERR, (__location__ " eventscript %s %s called with no timeout\n",
851 ctdb_eventscript_call_names[state->call],
860 run the event script in the background, calling the callback when
861 finished. If mem_ctx is freed, callback will never be called.
863 int ctdb_event_script_callback(struct ctdb_context *ctdb,
865 void (*callback)(struct ctdb_context *, int, void *),
867 enum ctdb_eventscript_call call,
868 const char *fmt, ...)
874 ret = ctdb_event_script_callback_v(ctdb, mem_ctx, callback, private_data, call, fmt, ap);
881 struct callback_status {
887 called when ctdb_event_script() finishes
889 static void event_script_callback(struct ctdb_context *ctdb, int status, void *private_data)
891 struct callback_status *s = (struct callback_status *)private_data;
897 run the event script, waiting for it to complete. Used when the caller
898 doesn't want to continue till the event script has finished.
900 int ctdb_event_script_args(struct ctdb_context *ctdb, enum ctdb_eventscript_call call,
901 const char *fmt, ...)
905 struct callback_status status;
908 ret = ctdb_event_script_callback_v(ctdb, ctdb,
909 event_script_callback, &status, call, fmt, ap);
918 while (status.done == false && event_loop_once(ctdb->ev) == 0) /* noop */;
920 if (status.status == -ETIME) {
921 DEBUG(DEBUG_ERR, (__location__ " eventscript for '%s' timedout."
922 " Immediately banning ourself for %d seconds\n",
923 ctdb_eventscript_call_names[call],
924 ctdb->tunable.recovery_ban_period));
926 /* Don't ban self if CTDB is starting up or shutting down */
927 if (call != CTDB_EVENT_INIT && call != CTDB_EVENT_SHUTDOWN) {
932 return status.status;
935 int ctdb_event_script(struct ctdb_context *ctdb, enum ctdb_eventscript_call call)
937 /* GCC complains about empty format string, so use %s and "". */
938 return ctdb_event_script_args(ctdb, call, "%s", "");
941 struct eventscript_callback_state {
942 struct ctdb_req_control *c;
946 called when a forced eventscript run has finished
948 static void run_eventscripts_callback(struct ctdb_context *ctdb, int status,
951 struct eventscript_callback_state *state =
952 talloc_get_type(private_data, struct eventscript_callback_state);
954 ctdb_enable_monitoring(ctdb);
957 DEBUG(DEBUG_ERR,(__location__ " Failed to run eventscripts\n"));
960 ctdb_request_control_reply(ctdb, state->c, NULL, status, NULL);
961 /* This will free the struct ctdb_event_script_state we are in! */
967 /* Returns rest of string, or NULL if no match. */
968 static const char *get_call(const char *p, enum ctdb_eventscript_call *call)
972 /* Skip any initial whitespace. */
973 p += strspn(p, " \t");
975 /* See if we match any. */
976 for (*call = 0; *call < CTDB_EVENT_MAX; (*call)++) {
977 len = strlen(ctdb_eventscript_call_names[*call]);
978 if (strncmp(p, ctdb_eventscript_call_names[*call], len) == 0) {
979 /* If end of string or whitespace, we're done. */
980 if (strcspn(p + len, " \t") == 0) {
989 A control to force running of the eventscripts from the ctdb client tool
991 int32_t ctdb_run_eventscripts(struct ctdb_context *ctdb,
992 struct ctdb_req_control *c,
993 TDB_DATA indata, bool *async_reply)
996 struct eventscript_callback_state *state;
998 enum ctdb_eventscript_call call;
1000 /* Figure out what call they want. */
1001 options = get_call((const char *)indata.dptr, &call);
1003 DEBUG(DEBUG_ERR, (__location__ " Invalid event name \"%s\"\n", (const char *)indata.dptr));
1007 if (ctdb->recovery_mode != CTDB_RECOVERY_NORMAL) {
1008 DEBUG(DEBUG_ERR, (__location__ " Aborted running eventscript \"%s\" while in RECOVERY mode\n", indata.dptr));
1012 state = talloc(ctdb->event_script_ctx, struct eventscript_callback_state);
1013 CTDB_NO_MEMORY(ctdb, state);
1015 state->c = talloc_steal(state, c);
1017 DEBUG(DEBUG_NOTICE,("Running eventscripts with arguments %s\n", indata.dptr));
1019 ctdb_disable_monitoring(ctdb);
1021 ret = ctdb_event_script_callback(ctdb,
1022 state, run_eventscripts_callback, state,
1023 call, "%s", options);
1026 ctdb_enable_monitoring(ctdb);
1027 DEBUG(DEBUG_ERR,(__location__ " Failed to run eventscripts with arguments %s\n", indata.dptr));
1032 /* tell ctdb_control.c that we will be replying asynchronously */
1033 *async_reply = true;
1040 int32_t ctdb_control_enable_script(struct ctdb_context *ctdb, TDB_DATA indata)
1045 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1047 script = (char *)indata.dptr;
1048 if (indata.dsize == 0) {
1049 DEBUG(DEBUG_ERR,(__location__ " No script specified.\n"));
1050 talloc_free(tmp_ctx);
1053 if (indata.dptr[indata.dsize - 1] != '\0') {
1054 DEBUG(DEBUG_ERR,(__location__ " String is not null terminated.\n"));
1055 talloc_free(tmp_ctx);
1058 if (index(script,'/') != NULL) {
1059 DEBUG(DEBUG_ERR,(__location__ " Script name contains '/'. Failed to enable script %s\n", script));
1060 talloc_free(tmp_ctx);
1065 if (stat(ctdb->event_script_dir, &st) != 0 &&
1067 DEBUG(DEBUG_CRIT,("No event script directory found at '%s'\n", ctdb->event_script_dir));
1068 talloc_free(tmp_ctx);
1073 filename = talloc_asprintf(tmp_ctx, "%s/%s", ctdb->event_script_dir, script);
1074 if (filename == NULL) {
1075 DEBUG(DEBUG_ERR,(__location__ " Failed to create script path\n"));
1076 talloc_free(tmp_ctx);
1080 if (stat(filename, &st) != 0) {
1081 DEBUG(DEBUG_ERR,("Could not stat event script %s. Failed to enable script.\n", filename));
1082 talloc_free(tmp_ctx);
1086 if (chmod(filename, st.st_mode | S_IXUSR) == -1) {
1087 DEBUG(DEBUG_ERR,("Could not chmod %s. Failed to enable script.\n", filename));
1088 talloc_free(tmp_ctx);
1092 talloc_free(tmp_ctx);
1096 int32_t ctdb_control_disable_script(struct ctdb_context *ctdb, TDB_DATA indata)
1101 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1103 script = (char *)indata.dptr;
1104 if (indata.dsize == 0) {
1105 DEBUG(DEBUG_ERR,(__location__ " No script specified.\n"));
1106 talloc_free(tmp_ctx);
1109 if (indata.dptr[indata.dsize - 1] != '\0') {
1110 DEBUG(DEBUG_ERR,(__location__ " String is not null terminated.\n"));
1111 talloc_free(tmp_ctx);
1114 if (index(script,'/') != NULL) {
1115 DEBUG(DEBUG_ERR,(__location__ " Script name contains '/'. Failed to disable script %s\n", script));
1116 talloc_free(tmp_ctx);
1121 if (stat(ctdb->event_script_dir, &st) != 0 &&
1123 DEBUG(DEBUG_CRIT,("No event script directory found at '%s'\n", ctdb->event_script_dir));
1124 talloc_free(tmp_ctx);
1129 filename = talloc_asprintf(tmp_ctx, "%s/%s", ctdb->event_script_dir, script);
1130 if (filename == NULL) {
1131 DEBUG(DEBUG_ERR,(__location__ " Failed to create script path\n"));
1132 talloc_free(tmp_ctx);
1136 if (stat(filename, &st) != 0) {
1137 DEBUG(DEBUG_ERR,("Could not stat event script %s. Failed to disable script.\n", filename));
1138 talloc_free(tmp_ctx);
1142 if (chmod(filename, st.st_mode & ~(S_IXUSR|S_IXGRP|S_IXOTH)) == -1) {
1143 DEBUG(DEBUG_ERR,("Could not chmod %s. Failed to disable script.\n", filename));
1144 talloc_free(tmp_ctx);
1148 talloc_free(tmp_ctx);