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 /* This is allowed to free us; talloc will prevent double free anyway,
649 * but beware if you call this outside the destructor!
650 * the callback hangs off a different context so we walk the list
651 * of "active" callbacks until we find the one state points to.
652 * if we cant find it it means the callback has been removed.
654 for (callback = state->ctdb->script_callbacks; callback != NULL; callback = callback->next) {
655 if (callback == state->callback) {
660 state->callback = NULL;
663 /* Make sure destructor doesn't free itself! */
664 talloc_steal(NULL, callback);
665 callback->fn(state->ctdb, status, callback->private_data);
666 talloc_free(callback);
672 static unsigned int count_words(const char *options)
674 unsigned int words = 0;
676 options += strspn(options, " \t");
679 options += strcspn(options, " \t");
680 options += strspn(options, " \t");
685 static bool check_options(enum ctdb_eventscript_call call, const char *options)
688 /* These all take no arguments. */
689 case CTDB_EVENT_INIT:
690 case CTDB_EVENT_SETUP:
691 case CTDB_EVENT_STARTUP:
692 case CTDB_EVENT_START_RECOVERY:
693 case CTDB_EVENT_RECOVERED:
694 case CTDB_EVENT_MONITOR:
695 case CTDB_EVENT_SHUTDOWN:
696 case CTDB_EVENT_IPREALLOCATED:
697 return count_words(options) == 0;
699 case CTDB_EVENT_TAKE_IP: /* interface, IP address, netmask bits. */
700 case CTDB_EVENT_RELEASE_IP:
701 return count_words(options) == 3;
703 case CTDB_EVENT_UPDATE_IP: /* old interface, new interface, IP address, netmask bits. */
704 return count_words(options) == 4;
707 DEBUG(DEBUG_ERR,(__location__ "Unknown ctdb_eventscript_call %u\n", call));
712 static int remove_callback(struct event_script_callback *callback)
714 DLIST_REMOVE(callback->ctdb->script_callbacks, callback);
719 run the event script in the background, calling the callback when
722 static int ctdb_event_script_callback_v(struct ctdb_context *ctdb,
724 void (*callback)(struct ctdb_context *, int, void *),
726 enum ctdb_eventscript_call call,
727 const char *fmt, va_list ap)
729 struct ctdb_event_script_state *state;
731 if (ctdb->recovery_mode != CTDB_RECOVERY_NORMAL) {
732 /* we guarantee that only some specifically allowed event scripts are run
734 const enum ctdb_eventscript_call allowed_calls[] = {
737 CTDB_EVENT_START_RECOVERY,
739 CTDB_EVENT_RELEASE_IP,
740 CTDB_EVENT_IPREALLOCATED,
743 for (i=0;i<ARRAY_SIZE(allowed_calls);i++) {
744 if (call == allowed_calls[i]) break;
746 if (i == ARRAY_SIZE(allowed_calls)) {
747 DEBUG(DEBUG_ERR,("Refusing to run event scripts call '%s' while in recovery\n",
748 ctdb_eventscript_call_names[call]));
753 /* Kill off any running monitor events to run this event. */
754 if (ctdb->current_monitor) {
755 struct ctdb_event_script_state *ms = talloc_get_type(ctdb->current_monitor, struct ctdb_event_script_state);
757 /* Cancel current monitor callback state only if monitoring
758 * context ctdb->monitor->monitor_context has not been freed */
759 if (ms->callback != NULL && !ctdb_stopped_monitoring(ctdb)) {
760 ms->callback->fn(ctdb, -ECANCELED, ms->callback->private_data);
761 talloc_free(ms->callback);
764 /* Discard script status so we don't save to last_status */
765 talloc_free(ctdb->current_monitor->scripts);
766 ctdb->current_monitor->scripts = NULL;
767 talloc_free(ctdb->current_monitor);
768 ctdb->current_monitor = NULL;
771 state = talloc(ctdb->event_script_ctx, struct ctdb_event_script_state);
772 CTDB_NO_MEMORY(ctdb, state);
774 /* The callback isn't done if the context is freed. */
775 state->callback = talloc(mem_ctx, struct event_script_callback);
776 CTDB_NO_MEMORY(ctdb, state->callback);
777 DLIST_ADD(ctdb->script_callbacks, state->callback);
778 talloc_set_destructor(state->callback, remove_callback);
779 state->callback->ctdb = ctdb;
780 state->callback->fn = callback;
781 state->callback->private_data = private_data;
785 state->options = talloc_vasprintf(state, fmt, ap);
786 state->timeout = timeval_set(ctdb->tunable.script_timeout, 0);
787 state->scripts = NULL;
788 if (state->options == NULL) {
789 DEBUG(DEBUG_ERR, (__location__ " could not allocate state->options\n"));
793 if (!check_options(state->call, state->options)) {
794 DEBUG(DEBUG_ERR, ("Bad eventscript options '%s' for %s\n",
795 ctdb_eventscript_call_names[state->call], state->options));
800 DEBUG(DEBUG_INFO,(__location__ " Starting eventscript %s %s\n",
801 ctdb_eventscript_call_names[state->call],
804 /* This is not a child of state, since we save it in destructor. */
805 state->scripts = ctdb_get_script_list(ctdb, ctdb);
806 if (state->scripts == NULL) {
813 if (call == CTDB_EVENT_MONITOR) {
814 ctdb->current_monitor = state;
817 talloc_set_destructor(state, event_script_destructor);
820 if (state->scripts->num_scripts == 0) {
825 state->scripts->scripts[0].status = fork_child_for_script(ctdb, state);
826 if (state->scripts->scripts[0].status != 0) {
827 /* Callback is called from destructor, with fail result. */
832 if (!timeval_is_zero(&state->timeout)) {
833 event_add_timed(ctdb->ev, state, timeval_current_ofs(state->timeout.tv_sec, state->timeout.tv_usec), ctdb_event_script_timeout, state);
835 DEBUG(DEBUG_ERR, (__location__ " eventscript %s %s called with no timeout\n",
836 ctdb_eventscript_call_names[state->call],
845 run the event script in the background, calling the callback when
846 finished. If mem_ctx is freed, callback will never be called.
848 int ctdb_event_script_callback(struct ctdb_context *ctdb,
850 void (*callback)(struct ctdb_context *, int, void *),
852 enum ctdb_eventscript_call call,
853 const char *fmt, ...)
859 ret = ctdb_event_script_callback_v(ctdb, mem_ctx, callback, private_data, call, fmt, ap);
866 struct callback_status {
872 called when ctdb_event_script() finishes
874 static void event_script_callback(struct ctdb_context *ctdb, int status, void *private_data)
876 struct callback_status *s = (struct callback_status *)private_data;
882 run the event script, waiting for it to complete. Used when the caller
883 doesn't want to continue till the event script has finished.
885 int ctdb_event_script_args(struct ctdb_context *ctdb, enum ctdb_eventscript_call call,
886 const char *fmt, ...)
890 struct callback_status status;
893 ret = ctdb_event_script_callback_v(ctdb, ctdb,
894 event_script_callback, &status, call, fmt, ap);
903 while (status.done == false && event_loop_once(ctdb->ev) == 0) /* noop */;
905 if (status.status == -ETIME) {
906 DEBUG(DEBUG_ERR, (__location__ " eventscript for '%s' timedout."
907 " Immediately banning ourself for %d seconds\n",
908 ctdb_eventscript_call_names[call],
909 ctdb->tunable.recovery_ban_period));
911 /* Don't ban self if CTDB is starting up or shutting down */
912 if (call != CTDB_EVENT_INIT && call != CTDB_EVENT_SHUTDOWN) {
917 return status.status;
920 int ctdb_event_script(struct ctdb_context *ctdb, enum ctdb_eventscript_call call)
922 /* GCC complains about empty format string, so use %s and "". */
923 return ctdb_event_script_args(ctdb, call, "%s", "");
926 struct eventscript_callback_state {
927 struct ctdb_req_control *c;
931 called when a forced eventscript run has finished
933 static void run_eventscripts_callback(struct ctdb_context *ctdb, int status,
936 struct eventscript_callback_state *state =
937 talloc_get_type(private_data, struct eventscript_callback_state);
939 ctdb_enable_monitoring(ctdb);
942 DEBUG(DEBUG_ERR,(__location__ " Failed to run eventscripts\n"));
945 ctdb_request_control_reply(ctdb, state->c, NULL, status, NULL);
946 /* This will free the struct ctdb_event_script_state we are in! */
952 /* Returns rest of string, or NULL if no match. */
953 static const char *get_call(const char *p, enum ctdb_eventscript_call *call)
957 /* Skip any initial whitespace. */
958 p += strspn(p, " \t");
960 /* See if we match any. */
961 for (*call = 0; *call < CTDB_EVENT_MAX; (*call)++) {
962 len = strlen(ctdb_eventscript_call_names[*call]);
963 if (strncmp(p, ctdb_eventscript_call_names[*call], len) == 0) {
964 /* If end of string or whitespace, we're done. */
965 if (strcspn(p + len, " \t") == 0) {
974 A control to force running of the eventscripts from the ctdb client tool
976 int32_t ctdb_run_eventscripts(struct ctdb_context *ctdb,
977 struct ctdb_req_control *c,
978 TDB_DATA indata, bool *async_reply)
981 struct eventscript_callback_state *state;
983 enum ctdb_eventscript_call call;
985 /* Figure out what call they want. */
986 options = get_call((const char *)indata.dptr, &call);
988 DEBUG(DEBUG_ERR, (__location__ " Invalid event name \"%s\"\n", (const char *)indata.dptr));
992 if (ctdb->recovery_mode != CTDB_RECOVERY_NORMAL) {
993 DEBUG(DEBUG_ERR, (__location__ " Aborted running eventscript \"%s\" while in RECOVERY mode\n", indata.dptr));
997 state = talloc(ctdb->event_script_ctx, struct eventscript_callback_state);
998 CTDB_NO_MEMORY(ctdb, state);
1000 state->c = talloc_steal(state, c);
1002 DEBUG(DEBUG_NOTICE,("Running eventscripts with arguments %s\n", indata.dptr));
1004 ctdb_disable_monitoring(ctdb);
1006 ret = ctdb_event_script_callback(ctdb,
1007 state, run_eventscripts_callback, state,
1008 call, "%s", options);
1011 ctdb_enable_monitoring(ctdb);
1012 DEBUG(DEBUG_ERR,(__location__ " Failed to run eventscripts with arguments %s\n", indata.dptr));
1017 /* tell ctdb_control.c that we will be replying asynchronously */
1018 *async_reply = true;
1025 int32_t ctdb_control_enable_script(struct ctdb_context *ctdb, TDB_DATA indata)
1030 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1032 script = (char *)indata.dptr;
1033 if (indata.dsize == 0) {
1034 DEBUG(DEBUG_ERR,(__location__ " No script specified.\n"));
1035 talloc_free(tmp_ctx);
1038 if (indata.dptr[indata.dsize - 1] != '\0') {
1039 DEBUG(DEBUG_ERR,(__location__ " String is not null terminated.\n"));
1040 talloc_free(tmp_ctx);
1043 if (index(script,'/') != NULL) {
1044 DEBUG(DEBUG_ERR,(__location__ " Script name contains '/'. Failed to enable script %s\n", script));
1045 talloc_free(tmp_ctx);
1050 if (stat(ctdb->event_script_dir, &st) != 0 &&
1052 DEBUG(DEBUG_CRIT,("No event script directory found at '%s'\n", ctdb->event_script_dir));
1053 talloc_free(tmp_ctx);
1058 filename = talloc_asprintf(tmp_ctx, "%s/%s", ctdb->event_script_dir, script);
1059 if (filename == NULL) {
1060 DEBUG(DEBUG_ERR,(__location__ " Failed to create script path\n"));
1061 talloc_free(tmp_ctx);
1065 if (stat(filename, &st) != 0) {
1066 DEBUG(DEBUG_ERR,("Could not stat event script %s. Failed to enable script.\n", filename));
1067 talloc_free(tmp_ctx);
1071 if (chmod(filename, st.st_mode | S_IXUSR) == -1) {
1072 DEBUG(DEBUG_ERR,("Could not chmod %s. Failed to enable script.\n", filename));
1073 talloc_free(tmp_ctx);
1077 talloc_free(tmp_ctx);
1081 int32_t ctdb_control_disable_script(struct ctdb_context *ctdb, TDB_DATA indata)
1086 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1088 script = (char *)indata.dptr;
1089 if (indata.dsize == 0) {
1090 DEBUG(DEBUG_ERR,(__location__ " No script specified.\n"));
1091 talloc_free(tmp_ctx);
1094 if (indata.dptr[indata.dsize - 1] != '\0') {
1095 DEBUG(DEBUG_ERR,(__location__ " String is not null terminated.\n"));
1096 talloc_free(tmp_ctx);
1099 if (index(script,'/') != NULL) {
1100 DEBUG(DEBUG_ERR,(__location__ " Script name contains '/'. Failed to disable script %s\n", script));
1101 talloc_free(tmp_ctx);
1106 if (stat(ctdb->event_script_dir, &st) != 0 &&
1108 DEBUG(DEBUG_CRIT,("No event script directory found at '%s'\n", ctdb->event_script_dir));
1109 talloc_free(tmp_ctx);
1114 filename = talloc_asprintf(tmp_ctx, "%s/%s", ctdb->event_script_dir, script);
1115 if (filename == NULL) {
1116 DEBUG(DEBUG_ERR,(__location__ " Failed to create script path\n"));
1117 talloc_free(tmp_ctx);
1121 if (stat(filename, &st) != 0) {
1122 DEBUG(DEBUG_ERR,("Could not stat event script %s. Failed to disable script.\n", filename));
1123 talloc_free(tmp_ctx);
1127 if (chmod(filename, st.st_mode & ~(S_IXUSR|S_IXGRP|S_IXOTH)) == -1) {
1128 DEBUG(DEBUG_ERR,("Could not chmod %s. Failed to disable script.\n", filename));
1129 talloc_free(tmp_ctx);
1133 talloc_free(tmp_ctx);