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/>.
21 #include "system/filesys.h"
22 #include "system/network.h"
23 #include "system/wait.h"
24 #include "system/dir.h"
25 #include "system/locale.h"
26 #include "system/time.h"
31 #include "lib/util/dlinklist.h"
32 #include "lib/util/debug.h"
33 #include "lib/util/samba_util.h"
35 #include "ctdb_private.h"
36 #include "ctdb_logging.h"
38 #include "common/rb_tree.h"
39 #include "common/system.h"
40 #include "common/common.h"
43 static void ctdb_event_script_timeout(struct tevent_context *ev,
44 struct tevent_timer *te,
45 struct timeval t, void *p);
47 /* This is attached to the event script state. */
48 struct event_script_callback {
49 struct event_script_callback *next, *prev;
50 struct ctdb_context *ctdb;
52 /* Warning: this can free us! */
53 void (*fn)(struct ctdb_context *, int, void *);
57 struct ctdb_event_script_state {
58 struct ctdb_context *ctdb;
59 struct event_script_callback *callback;
62 enum ctdb_eventscript_call call;
64 struct timeval timeout;
67 struct ctdb_scripts_wire *scripts;
70 static struct ctdb_script_wire *get_current_script(struct ctdb_event_script_state *state)
72 return &state->scripts->scripts[state->current];
75 /* called from ctdb_logging when we have received output on STDERR from
76 * one of the eventscripts
78 static void log_event_script_output(const char *str, uint16_t len, void *p)
80 struct ctdb_event_script_state *state
81 = talloc_get_type(p, struct ctdb_event_script_state);
82 struct ctdb_script_wire *current;
83 unsigned int slen, min;
85 /* We may have been aborted to run something else. Discard */
86 if (state->scripts == NULL) {
90 current = get_current_script(state);
92 /* Append, but don't overfill buffer. It starts zero-filled. */
93 slen = strlen(current->output);
94 min = MIN(len, sizeof(current->output) - slen - 1);
96 memcpy(current->output + slen, str, min);
99 int32_t ctdb_control_get_event_script_status(struct ctdb_context *ctdb,
103 if (call_type >= CTDB_EVENT_MAX) {
107 if (ctdb->last_status[call_type] == NULL) {
108 /* If it's never been run, return nothing so they can tell. */
111 outdata->dsize = talloc_get_size(ctdb->last_status[call_type]);
112 outdata->dptr = (uint8_t *)ctdb->last_status[call_type];
117 /* To ignore directory entry return 0, else return non-zero */
118 static int script_filter(const struct dirent *de)
120 int namelen = strlen(de->d_name);
122 /* Ignore . and .. */
127 /* Skip temporary files left behind by emacs */
128 if (de->d_name[namelen-1] == '~') {
132 /* Filename should start with [0-9][0-9]. */
133 if (!isdigit(de->d_name[0]) || !isdigit(de->d_name[1]) ||
134 de->d_name[2] != '.') {
138 if (namelen > MAX_SCRIPT_NAME) {
145 /* Return true if OK, otherwise set errno. */
146 static bool check_executable(const char *dir, const char *name)
151 full = talloc_asprintf(NULL, "%s/%s", dir, name);
155 if (stat(full, &st) != 0) {
156 DEBUG(DEBUG_ERR,("Could not stat event script %s: %s\n",
157 full, strerror(errno)));
162 if (!(st.st_mode & S_IXUSR)) {
163 DEBUG(DEBUG_DEBUG,("Event script %s is not executable. Ignoring this event script\n", full));
173 static struct ctdb_scripts_wire *ctdb_get_script_list(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx)
175 struct dirent **namelist;
176 struct ctdb_scripts_wire *scripts;
179 /* scan all directory entries and insert all valid scripts into the
182 count = scandir(ctdb->event_script_dir, &namelist, script_filter, alphasort);
184 DEBUG(DEBUG_CRIT, ("Failed to read event script directory '%s' - %s\n",
185 ctdb->event_script_dir, strerror(errno)));
189 /* Overallocates by one, but that's OK */
190 scripts = talloc_zero_size(mem_ctx,
192 + sizeof(scripts->scripts[0]) * count);
193 if (scripts == NULL) {
194 DEBUG(DEBUG_ERR, (__location__ " Failed to allocate scripts\n"));
197 scripts->num_scripts = count;
199 for (i = 0; i < count; i++) {
200 struct ctdb_script_wire *s = &scripts->scripts[i];
202 if (strlcpy(s->name, namelist[i]->d_name, sizeof(s->name)) >=
204 s->status = -ENAMETOOLONG;
209 if (!check_executable(ctdb->event_script_dir,
210 namelist[i]->d_name)) {
216 for (i=0; i<count; i++) {
224 /* There cannot be more than 10 arguments to command helper. */
225 #define MAX_HELPER_ARGS (10)
227 static bool child_helper_args(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
228 enum ctdb_eventscript_call call,
230 struct ctdb_script_wire *current, int fd,
231 int *argc, const char ***argv)
235 char *t, *saveptr, *opt;
237 tmp = talloc_array(mem_ctx, const char *, 10+1);
238 if (tmp == NULL) goto failed;
240 tmp[0] = talloc_asprintf(tmp, "%d", fd);
241 tmp[1] = talloc_asprintf(tmp, "%s/%s", ctdb->event_script_dir, current->name);
242 tmp[2] = talloc_asprintf(tmp, "%s", ctdb_eventscript_call_names[call]);
245 /* Split options into individual arguments */
246 opt = talloc_strdup(mem_ctx, options);
251 t = strtok_r(opt, " ", &saveptr);
253 tmp[n++] = talloc_strdup(tmp, t);
254 if (n > MAX_HELPER_ARGS) {
257 t = strtok_r(NULL, " ", &saveptr);
260 for (i=0; i<n; i++) {
261 if (tmp[i] == NULL) {
266 /* Last argument should be NULL */
275 DEBUG(DEBUG_ERR, (__location__ " too many arguments '%s' to eventscript '%s'\n",
276 options, ctdb_eventscript_call_names[call]));
286 static void ctdb_event_script_handler(struct tevent_context *ev,
287 struct tevent_fd *fde,
288 uint16_t flags, void *p);
290 static char helper_prog[PATH_MAX+1] = "";
292 static int fork_child_for_script(struct ctdb_context *ctdb,
293 struct ctdb_event_script_state *state)
296 struct tevent_fd *fde;
297 struct ctdb_script_wire *current = get_current_script(state);
301 if (!ctdb_set_helper("event helper", helper_prog, sizeof(helper_prog),
303 CTDB_HELPER_BINDIR, "ctdb_event_helper")) {
304 ctdb_die(ctdb, __location__
305 " Unable to set event helper\n");
308 current->start = timeval_current();
312 DEBUG(DEBUG_ERR, (__location__ " pipe failed for child eventscript process\n"));
316 /* Arguments for helper */
317 if (!child_helper_args(state, ctdb, state->call, state->options, current,
318 state->fd[1], &argc, &argv)) {
319 DEBUG(DEBUG_ERR, (__location__ " failed to create arguments for eventscript helper\n"));
326 if (!ctdb_vfork_with_logging(state, ctdb, current->name,
327 helper_prog, argc, argv,
328 log_event_script_output,
329 state, &state->child)) {
340 set_close_on_exec(state->fd[0]);
342 /* Set ourselves up to be called when that's done. */
343 fde = tevent_add_fd(ctdb->ev, state, state->fd[0], TEVENT_FD_READ,
344 ctdb_event_script_handler, state);
345 tevent_fd_set_auto_close(fde);
351 Summarize status of this run of scripts.
353 static int script_status(struct ctdb_scripts_wire *scripts)
357 for (i = 0; i < scripts->num_scripts; i++) {
358 switch (scripts->scripts[i].status) {
362 /* Disabled or missing; that's OK. */
368 return scripts->scripts[i].status;
376 /* called when child is finished */
377 static void ctdb_event_script_handler(struct tevent_context *ev,
378 struct tevent_fd *fde,
379 uint16_t flags, void *p)
381 struct ctdb_event_script_state *state =
382 talloc_get_type(p, struct ctdb_event_script_state);
383 struct ctdb_script_wire *current = get_current_script(state);
384 struct ctdb_context *ctdb = state->ctdb;
388 DEBUG(DEBUG_ERR,("Eventscript finished but ctdb is NULL\n"));
392 r = sys_read(state->fd[0], ¤t->status, sizeof(current->status));
394 current->status = -errno;
396 current->status = -EINTR;
397 } else if (r != sizeof(current->status)) {
398 current->status = -EIO;
401 current->finished = timeval_current();
402 /* valgrind gets overloaded if we run next script as it's still doing
403 * post-execution analysis, so kill finished child here. */
404 if (ctdb->valgrinding) {
405 ctdb_kill(ctdb, state->child, SIGKILL);
410 status = script_status(state->scripts);
412 /* Aborted or finished all scripts? We're done. */
413 if (status != 0 || state->current+1 == state->scripts->num_scripts) {
416 ("Eventscript %s %s finished with state %d\n",
417 ctdb_eventscript_call_names[state->call],
418 state->options, status));
421 ctdb->event_script_timeouts = 0;
426 /* Forget about that old fd. */
432 current->status = fork_child_for_script(ctdb, state);
433 if (current->status != 0) {
434 /* This calls the callback. */
439 struct debug_hung_script_state {
440 struct ctdb_context *ctdb;
442 enum ctdb_eventscript_call call;
445 static int debug_hung_script_state_destructor(struct debug_hung_script_state *state)
448 ctdb_kill(state->ctdb, state->child, SIGKILL);
453 static void debug_hung_script_timeout(struct tevent_context *ev, struct tevent_timer *te,
454 struct timeval t, void *p)
456 struct debug_hung_script_state *state =
457 talloc_get_type(p, struct debug_hung_script_state);
462 static void debug_hung_script_done(struct tevent_context *ev, struct tevent_fd *fde,
463 uint16_t flags, void *p)
465 struct debug_hung_script_state *state =
466 talloc_get_type(p, struct debug_hung_script_state);
471 static void ctdb_run_debug_hung_script(struct ctdb_context *ctdb, struct debug_hung_script_state *state)
474 const char * debug_hung_script = CTDB_ETCDIR "/debug-hung-script.sh";
476 struct tevent_timer *ttimer;
477 struct tevent_fd *tfd;
482 DEBUG(DEBUG_ERR,("Failed to create pipe fd for debug hung script\n"));
486 if (getenv("CTDB_DEBUG_HUNG_SCRIPT") != NULL) {
487 debug_hung_script = getenv("CTDB_DEBUG_HUNG_SCRIPT");
490 argv = talloc_array(state, const char *, 5);
492 argv[0] = talloc_asprintf(argv, "%d", fd[1]);
493 argv[1] = talloc_strdup(argv, debug_hung_script);
494 argv[2] = talloc_asprintf(argv, "%d", state->child);
495 argv[3] = talloc_strdup(argv, ctdb_eventscript_call_names[state->call]);
498 for (i=0; i<4; i++) {
499 if (argv[i] == NULL) {
508 if (!ctdb_vfork_with_logging(state, ctdb, "Hung-script",
509 helper_prog, 5, argv, NULL, NULL, &pid)) {
510 DEBUG(DEBUG_ERR,("Failed to fork a child to track hung event script\n"));
520 ttimer = tevent_add_timer(ctdb->ev, state,
521 timeval_current_ofs(ctdb->tunable.script_timeout, 0),
522 debug_hung_script_timeout, state);
523 if (ttimer == NULL) {
528 tfd = tevent_add_fd(ctdb->ev, state, fd[0], TEVENT_FD_READ,
529 debug_hung_script_done, state);
535 tevent_fd_set_auto_close(tfd);
538 /* called when child times out */
539 static void ctdb_event_script_timeout(struct tevent_context *ev,
540 struct tevent_timer *te,
541 struct timeval t, void *p)
543 struct ctdb_event_script_state *state = talloc_get_type(p, struct ctdb_event_script_state);
544 struct ctdb_context *ctdb = state->ctdb;
545 struct ctdb_script_wire *current = get_current_script(state);
546 struct debug_hung_script_state *debug_state;
548 DEBUG(DEBUG_ERR,("Event script '%s %s %s' timed out after %.1fs, count: %u, pid: %d\n",
549 current->name, ctdb_eventscript_call_names[state->call], state->options,
550 timeval_elapsed(¤t->start),
551 ctdb->event_script_timeouts, state->child));
553 /* ignore timeouts for these events */
554 switch (state->call) {
555 case CTDB_EVENT_START_RECOVERY:
556 case CTDB_EVENT_RECOVERED:
557 case CTDB_EVENT_TAKE_IP:
558 case CTDB_EVENT_RELEASE_IP:
559 state->scripts->scripts[state->current].status = 0;
560 DEBUG(DEBUG_ERR,("Ignoring hung script for %s call %d\n", state->options, state->call));
563 state->scripts->scripts[state->current].status = -ETIME;
566 debug_state = talloc_zero(ctdb, struct debug_hung_script_state);
567 if (debug_state == NULL) {
572 /* Save information useful for running debug hung script, so
573 * eventscript state can be freed.
575 debug_state->ctdb = ctdb;
576 debug_state->child = state->child;
577 debug_state->call = state->call;
579 /* This destructor will actually kill the hung event script */
580 talloc_set_destructor(debug_state, debug_hung_script_state_destructor);
585 ctdb_run_debug_hung_script(ctdb, debug_state);
589 destroy an event script: kill it if ->child != 0.
591 static int event_script_destructor(struct ctdb_event_script_state *state)
594 struct event_script_callback *callback;
597 DEBUG(DEBUG_ERR,(__location__ " Sending SIGTERM to child pid:%d\n", state->child));
599 if (ctdb_kill(state->ctdb, state->child, SIGTERM) != 0) {
600 DEBUG(DEBUG_ERR,("Failed to kill child process for eventscript, errno %s(%d)\n", strerror(errno), errno));
604 /* If we were the current monitor, we no longer are. */
605 if (state->ctdb->current_monitor == state) {
606 state->ctdb->current_monitor = NULL;
609 /* Save our scripts as the last executed status, if we have them.
610 * See ctdb_event_script_callback_v where we abort monitor event. */
611 if (state->scripts) {
612 talloc_free(state->ctdb->last_status[state->call]);
613 state->ctdb->last_status[state->call] = state->scripts;
614 if (state->current < state->ctdb->last_status[state->call]->num_scripts) {
615 state->ctdb->last_status[state->call]->num_scripts = state->current+1;
619 /* Use last status as result, or "OK" if none. */
620 if (state->ctdb->last_status[state->call]) {
621 status = script_status(state->ctdb->last_status[state->call]);
626 state->ctdb->active_events--;
627 if (state->ctdb->active_events < 0) {
628 ctdb_fatal(state->ctdb, "Active events < 0");
631 /* This is allowed to free us; talloc will prevent double free anyway,
632 * but beware if you call this outside the destructor!
633 * the callback hangs off a different context so we walk the list
634 * of "active" callbacks until we find the one state points to.
635 * if we cant find it it means the callback has been removed.
637 for (callback = state->ctdb->script_callbacks; callback != NULL; callback = callback->next) {
638 if (callback == state->callback) {
643 state->callback = NULL;
646 /* Make sure destructor doesn't free itself! */
647 talloc_steal(NULL, callback);
648 callback->fn(state->ctdb, status, callback->private_data);
649 talloc_free(callback);
655 static unsigned int count_words(const char *options)
657 unsigned int words = 0;
659 options += strspn(options, " \t");
662 options += strcspn(options, " \t");
663 options += strspn(options, " \t");
668 static bool check_options(enum ctdb_eventscript_call call, const char *options)
671 /* These all take no arguments. */
672 case CTDB_EVENT_INIT:
673 case CTDB_EVENT_SETUP:
674 case CTDB_EVENT_STARTUP:
675 case CTDB_EVENT_START_RECOVERY:
676 case CTDB_EVENT_RECOVERED:
677 case CTDB_EVENT_MONITOR:
678 case CTDB_EVENT_SHUTDOWN:
679 case CTDB_EVENT_IPREALLOCATED:
680 return count_words(options) == 0;
682 case CTDB_EVENT_TAKE_IP: /* interface, IP address, netmask bits. */
683 case CTDB_EVENT_RELEASE_IP:
684 return count_words(options) == 3;
686 case CTDB_EVENT_UPDATE_IP: /* old interface, new interface, IP address, netmask bits. */
687 return count_words(options) == 4;
690 DEBUG(DEBUG_ERR,(__location__ "Unknown ctdb_eventscript_call %u\n", call));
695 static int remove_callback(struct event_script_callback *callback)
697 DLIST_REMOVE(callback->ctdb->script_callbacks, callback);
702 run the event script in the background, calling the callback when
705 static int ctdb_event_script_callback_v(struct ctdb_context *ctdb,
707 void (*callback)(struct ctdb_context *, int, void *),
709 enum ctdb_eventscript_call call,
710 const char *fmt, va_list ap)
712 struct ctdb_event_script_state *state;
714 if (ctdb->recovery_mode != CTDB_RECOVERY_NORMAL) {
715 /* we guarantee that only some specifically allowed event scripts are run
717 const enum ctdb_eventscript_call allowed_calls[] = {
720 CTDB_EVENT_START_RECOVERY,
722 CTDB_EVENT_RELEASE_IP,
723 CTDB_EVENT_IPREALLOCATED,
726 for (i=0;i<ARRAY_SIZE(allowed_calls);i++) {
727 if (call == allowed_calls[i]) break;
729 if (i == ARRAY_SIZE(allowed_calls)) {
730 DEBUG(DEBUG_ERR,("Refusing to run event scripts call '%s' while in recovery\n",
731 ctdb_eventscript_call_names[call]));
736 /* Do not run new monitor events if some event is already
737 * running, unless the running event is a monitor event, in
738 * which case running a new one should cancel the old one. */
739 if (call == CTDB_EVENT_MONITOR &&
740 ctdb->active_events > 0 &&
741 ctdb->current_monitor == NULL) {
742 if (callback != NULL) {
743 callback(ctdb, -ECANCELED, private_data);
748 /* Kill off any running monitor events to run this event. */
749 if (ctdb->current_monitor) {
750 struct ctdb_event_script_state *ms = talloc_get_type(ctdb->current_monitor, struct ctdb_event_script_state);
752 /* Cancel current monitor callback state only if monitoring
753 * context ctdb->monitor->monitor_context has not been freed */
754 if (ms->callback != NULL && !ctdb_stopped_monitoring(ctdb)) {
755 ms->callback->fn(ctdb, -ECANCELED, ms->callback->private_data);
756 talloc_free(ms->callback);
759 /* Discard script status so we don't save to last_status */
760 talloc_free(ctdb->current_monitor->scripts);
761 ctdb->current_monitor->scripts = NULL;
762 talloc_free(ctdb->current_monitor);
763 ctdb->current_monitor = NULL;
766 state = talloc(ctdb->event_script_ctx, struct ctdb_event_script_state);
767 CTDB_NO_MEMORY(ctdb, state);
769 /* The callback isn't done if the context is freed. */
770 state->callback = talloc(mem_ctx, struct event_script_callback);
771 CTDB_NO_MEMORY(ctdb, state->callback);
772 DLIST_ADD(ctdb->script_callbacks, state->callback);
773 talloc_set_destructor(state->callback, remove_callback);
774 state->callback->ctdb = ctdb;
775 state->callback->fn = callback;
776 state->callback->private_data = private_data;
780 state->options = talloc_vasprintf(state, fmt, ap);
781 state->timeout = timeval_set(ctdb->tunable.script_timeout, 0);
782 state->scripts = NULL;
783 if (state->options == NULL) {
784 DEBUG(DEBUG_ERR, (__location__ " could not allocate state->options\n"));
788 if (!check_options(state->call, state->options)) {
789 DEBUG(DEBUG_ERR, ("Bad eventscript options '%s' for '%s'\n",
791 ctdb_eventscript_call_names[state->call]));
796 DEBUG(DEBUG_INFO,(__location__ " Starting eventscript %s %s\n",
797 ctdb_eventscript_call_names[state->call],
800 /* This is not a child of state, since we save it in destructor. */
801 state->scripts = ctdb_get_script_list(ctdb, ctdb);
802 if (state->scripts == NULL) {
809 if (call == CTDB_EVENT_MONITOR) {
810 ctdb->current_monitor = state;
813 talloc_set_destructor(state, event_script_destructor);
815 ctdb->active_events++;
818 if (state->scripts->num_scripts == 0) {
819 callback(ctdb, 0, private_data);
824 state->scripts->scripts[0].status = fork_child_for_script(ctdb, state);
825 if (state->scripts->scripts[0].status != 0) {
826 /* Callback is called from destructor, with fail result. */
831 if (!timeval_is_zero(&state->timeout)) {
832 tevent_add_timer(ctdb->ev, state,
833 timeval_current_ofs(state->timeout.tv_sec,
834 state->timeout.tv_usec),
835 ctdb_event_script_timeout, state);
837 DEBUG(DEBUG_ERR, (__location__ " eventscript %s %s called with no timeout\n",
838 ctdb_eventscript_call_names[state->call],
847 run the event script in the background, calling the callback when
848 finished. If mem_ctx is freed, callback will never be called.
850 int ctdb_event_script_callback(struct ctdb_context *ctdb,
852 void (*callback)(struct ctdb_context *, int, void *),
854 enum ctdb_eventscript_call call,
855 const char *fmt, ...)
861 ret = ctdb_event_script_callback_v(ctdb, mem_ctx, callback, private_data, call, fmt, ap);
868 struct callback_status {
874 called when ctdb_event_script() finishes
876 static void event_script_callback(struct ctdb_context *ctdb, int status, void *private_data)
878 struct callback_status *s = (struct callback_status *)private_data;
884 run the event script, waiting for it to complete. Used when the caller
885 doesn't want to continue till the event script has finished.
887 int ctdb_event_script_args(struct ctdb_context *ctdb, enum ctdb_eventscript_call call,
888 const char *fmt, ...)
892 struct callback_status status = {
898 ret = ctdb_event_script_callback_v(ctdb, ctdb,
899 event_script_callback, &status, call, fmt, ap);
905 while (status.done == false && tevent_loop_once(ctdb->ev) == 0) /* noop */;
907 if (status.status == -ETIME) {
908 DEBUG(DEBUG_ERR, (__location__ " eventscript for '%s' timedout."
909 " Immediately banning ourself for %d seconds\n",
910 ctdb_eventscript_call_names[call],
911 ctdb->tunable.recovery_ban_period));
913 /* Don't ban self if CTDB is starting up or shutting down */
914 if (call != CTDB_EVENT_INIT && call != CTDB_EVENT_SHUTDOWN) {
919 return status.status;
922 int ctdb_event_script(struct ctdb_context *ctdb, enum ctdb_eventscript_call call)
924 /* GCC complains about empty format string, so use %s and "". */
925 return ctdb_event_script_args(ctdb, call, "%s", "");
928 struct eventscript_callback_state {
929 struct ctdb_req_control *c;
933 called when a forced eventscript run has finished
935 static void run_eventscripts_callback(struct ctdb_context *ctdb, int status,
938 const char *errmsg = NULL;
940 struct eventscript_callback_state *state =
941 talloc_get_type(private_data, struct eventscript_callback_state);
944 if (status == -ECANCELED) {
946 (__location__ " Eventscript cancelled\n"));
947 errmsg = "cancelled";
950 (__location__ " Failed to run eventscripts\n"));
954 ctdb_request_control_reply(ctdb, state->c, NULL, status, errmsg);
955 /* This will free the struct ctdb_event_script_state we are in! */
961 /* Returns rest of string, or NULL if no match. */
962 static const char *get_call(const char *p, enum ctdb_eventscript_call *call)
966 /* Skip any initial whitespace. */
967 p += strspn(p, " \t");
969 /* See if we match any. */
970 for (*call = 0; *call < CTDB_EVENT_MAX; (*call)++) {
971 len = strlen(ctdb_eventscript_call_names[*call]);
972 if (strncmp(p, ctdb_eventscript_call_names[*call], len) == 0) {
973 /* If end of string or whitespace, we're done. */
974 if (strcspn(p + len, " \t") == 0) {
983 A control to force running of the eventscripts from the ctdb client tool
985 int32_t ctdb_run_eventscripts(struct ctdb_context *ctdb,
986 struct ctdb_req_control *c,
987 TDB_DATA indata, bool *async_reply)
990 struct eventscript_callback_state *state;
992 enum ctdb_eventscript_call call;
994 /* Figure out what call they want. */
995 options = get_call((const char *)indata.dptr, &call);
997 DEBUG(DEBUG_ERR, (__location__ " Invalid event name \"%s\"\n", (const char *)indata.dptr));
1001 if (ctdb->recovery_mode != CTDB_RECOVERY_NORMAL) {
1002 DEBUG(DEBUG_ERR, (__location__ " Aborted running eventscript \"%s\" while in RECOVERY mode\n", indata.dptr));
1006 state = talloc(ctdb->event_script_ctx, struct eventscript_callback_state);
1007 CTDB_NO_MEMORY(ctdb, state);
1009 state->c = talloc_steal(state, c);
1011 DEBUG(DEBUG_NOTICE,("Running eventscripts with arguments %s\n", indata.dptr));
1013 ret = ctdb_event_script_callback(ctdb,
1014 ctdb, run_eventscripts_callback, state,
1015 call, "%s", options);
1018 DEBUG(DEBUG_ERR,(__location__ " Failed to run eventscripts with arguments %s\n", indata.dptr));
1023 /* tell ctdb_control.c that we will be replying asynchronously */
1024 *async_reply = true;
1031 int32_t ctdb_control_enable_script(struct ctdb_context *ctdb, TDB_DATA indata)
1036 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1038 script = (char *)indata.dptr;
1039 if (indata.dsize == 0) {
1040 DEBUG(DEBUG_ERR,(__location__ " No script specified.\n"));
1041 talloc_free(tmp_ctx);
1044 if (indata.dptr[indata.dsize - 1] != '\0') {
1045 DEBUG(DEBUG_ERR,(__location__ " String is not null terminated.\n"));
1046 talloc_free(tmp_ctx);
1049 if (index(script,'/') != NULL) {
1050 DEBUG(DEBUG_ERR,(__location__ " Script name contains '/'. Failed to enable script %s\n", script));
1051 talloc_free(tmp_ctx);
1056 if (stat(ctdb->event_script_dir, &st) != 0 &&
1058 DEBUG(DEBUG_CRIT,("No event script directory found at '%s'\n", ctdb->event_script_dir));
1059 talloc_free(tmp_ctx);
1064 filename = talloc_asprintf(tmp_ctx, "%s/%s", ctdb->event_script_dir, script);
1065 if (filename == NULL) {
1066 DEBUG(DEBUG_ERR,(__location__ " Failed to create script path\n"));
1067 talloc_free(tmp_ctx);
1071 if (stat(filename, &st) != 0) {
1072 DEBUG(DEBUG_ERR,("Could not stat event script %s. Failed to enable script.\n", filename));
1073 talloc_free(tmp_ctx);
1077 if (chmod(filename, st.st_mode | S_IXUSR) == -1) {
1078 DEBUG(DEBUG_ERR,("Could not chmod %s. Failed to enable script.\n", filename));
1079 talloc_free(tmp_ctx);
1083 talloc_free(tmp_ctx);
1087 int32_t ctdb_control_disable_script(struct ctdb_context *ctdb, TDB_DATA indata)
1092 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1094 script = (char *)indata.dptr;
1095 if (indata.dsize == 0) {
1096 DEBUG(DEBUG_ERR,(__location__ " No script specified.\n"));
1097 talloc_free(tmp_ctx);
1100 if (indata.dptr[indata.dsize - 1] != '\0') {
1101 DEBUG(DEBUG_ERR,(__location__ " String is not null terminated.\n"));
1102 talloc_free(tmp_ctx);
1105 if (index(script,'/') != NULL) {
1106 DEBUG(DEBUG_ERR,(__location__ " Script name contains '/'. Failed to disable script %s\n", script));
1107 talloc_free(tmp_ctx);
1112 if (stat(ctdb->event_script_dir, &st) != 0 &&
1114 DEBUG(DEBUG_CRIT,("No event script directory found at '%s'\n", ctdb->event_script_dir));
1115 talloc_free(tmp_ctx);
1120 filename = talloc_asprintf(tmp_ctx, "%s/%s", ctdb->event_script_dir, script);
1121 if (filename == NULL) {
1122 DEBUG(DEBUG_ERR,(__location__ " Failed to create script path\n"));
1123 talloc_free(tmp_ctx);
1127 if (stat(filename, &st) != 0) {
1128 DEBUG(DEBUG_ERR,("Could not stat event script %s. Failed to disable script.\n", filename));
1129 talloc_free(tmp_ctx);
1133 if (chmod(filename, st.st_mode & ~(S_IXUSR|S_IXGRP|S_IXOTH)) == -1) {
1134 DEBUG(DEBUG_ERR,("Could not chmod %s. Failed to disable script.\n", filename));
1135 talloc_free(tmp_ctx);
1139 talloc_free(tmp_ctx);