4 Copyright (C) Andrew Tridgell 2007
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, see <http://www.gnu.org/licenses/>.
22 #include "system/filesys.h"
23 #include "system/wait.h"
24 #include "system/dir.h"
25 #include "system/locale.h"
26 #include "../include/ctdb_private.h"
27 #include "../common/rb_tree.h"
28 #include "lib/util/dlinklist.h"
30 static void ctdb_event_script_timeout(struct event_context *ev, struct timed_event *te, struct timeval t, void *p);
33 ctdbd sends us a SIGTERM when we should die.
35 static void sigterm(int sig)
39 /* all the child processes will be running in the same process group */
42 kill(-getpid(), SIGKILL);
49 /* This is attached to the event script state. */
50 struct event_script_callback {
51 struct event_script_callback *next, *prev;
52 struct ctdb_context *ctdb;
54 /* Warning: this can free us! */
55 void (*fn)(struct ctdb_context *, int, void *);
60 struct ctdb_event_script_state {
61 struct ctdb_context *ctdb;
62 struct event_script_callback *callback;
66 enum ctdb_eventscript_call call;
68 struct timeval timeout;
71 struct ctdb_scripts_wire *scripts;
74 static struct ctdb_script_wire *get_current_script(struct ctdb_event_script_state *state)
76 return &state->scripts->scripts[state->current];
79 /* called from ctdb_logging when we have received output on STDERR from
80 * one of the eventscripts
82 static void log_event_script_output(const char *str, uint16_t len, void *p)
84 struct ctdb_event_script_state *state
85 = talloc_get_type(p, struct ctdb_event_script_state);
86 struct ctdb_script_wire *current;
87 unsigned int slen, min;
89 /* We may have been aborted to run something else. Discard */
90 if (state->scripts == NULL) {
94 current = get_current_script(state);
96 /* Append, but don't overfill buffer. It starts zero-filled. */
97 slen = strlen(current->output);
98 min = MIN(len, sizeof(current->output) - slen - 1);
100 memcpy(current->output + slen, str, min);
103 int32_t ctdb_control_get_event_script_status(struct ctdb_context *ctdb,
107 if (call_type >= CTDB_EVENT_MAX) {
111 if (ctdb->last_status[call_type] == NULL) {
112 /* If it's never been run, return nothing so they can tell. */
115 outdata->dsize = talloc_get_size(ctdb->last_status[call_type]);
116 outdata->dptr = (uint8_t *)ctdb->last_status[call_type];
121 struct ctdb_script_tree_item {
126 /* Return true if OK, otherwise set errno. */
127 static bool check_executable(const char *dir, const char *name)
132 full = talloc_asprintf(NULL, "%s/%s", dir, name);
136 if (stat(full, &st) != 0) {
137 DEBUG(DEBUG_ERR,("Could not stat event script %s: %s\n",
138 full, strerror(errno)));
143 if (!(st.st_mode & S_IXUSR)) {
144 DEBUG(DEBUG_DEBUG,("Event script %s is not executable. Ignoring this event script\n", full));
154 static struct ctdb_scripts_wire *ctdb_get_script_list(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx)
160 struct ctdb_scripts_wire *scripts;
161 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
162 struct ctdb_script_tree_item *tree_item;
166 the service specific event scripts
168 if (stat(ctdb->event_script_dir, &st) != 0 &&
170 DEBUG(DEBUG_CRIT,("No event script directory found at '%s'\n", ctdb->event_script_dir));
171 talloc_free(tmp_ctx);
175 /* create a tree to store all the script names in */
176 tree = trbt_create(tmp_ctx, 0);
178 /* scan all directory entries and insert all valid scripts into the
181 dir = opendir(ctdb->event_script_dir);
183 DEBUG(DEBUG_CRIT,("Failed to open event script directory '%s'\n", ctdb->event_script_dir));
184 talloc_free(tmp_ctx);
189 while ((de=readdir(dir)) != NULL) {
193 namlen = strlen(de->d_name);
199 if (de->d_name[namlen-1] == '~') {
200 /* skip files emacs left behind */
204 if (de->d_name[2] != '.') {
208 if (sscanf(de->d_name, "%02u.", &num) != 1) {
212 if (strlen(de->d_name) > MAX_SCRIPT_NAME) {
213 DEBUG(DEBUG_ERR,("Script name %s too long! %u chars max",
214 de->d_name, MAX_SCRIPT_NAME));
218 tree_item = talloc(tree, struct ctdb_script_tree_item);
219 if (tree_item == NULL) {
220 DEBUG(DEBUG_ERR, (__location__ " Failed to allocate new tree item\n"));
222 talloc_free(tmp_ctx);
226 tree_item->error = 0;
227 if (!check_executable(ctdb->event_script_dir, de->d_name)) {
228 tree_item->error = errno;
231 tree_item->name = talloc_strdup(tree_item, de->d_name);
232 if (tree_item->name == NULL) {
233 DEBUG(DEBUG_ERR,(__location__ " Failed to allocate script name.\n"));
235 talloc_free(tmp_ctx);
239 /* store the event script in the tree */
240 trbt_insert32(tree, (num<<16)|count++, tree_item);
244 /* Overallocates by one, but that's OK */
245 scripts = talloc_zero_size(tmp_ctx,
247 + sizeof(scripts->scripts[0]) * count);
248 if (scripts == NULL) {
249 DEBUG(DEBUG_ERR, (__location__ " Failed to allocate scripts\n"));
250 talloc_free(tmp_ctx);
253 scripts->num_scripts = count;
255 for (count = 0; count < scripts->num_scripts; count++) {
256 tree_item = trbt_findfirstarray32(tree, 1);
258 strcpy(scripts->scripts[count].name, tree_item->name);
259 scripts->scripts[count].status = -tree_item->error;
261 /* remove this script from the tree */
262 talloc_free(tree_item);
265 talloc_steal(mem_ctx, scripts);
266 talloc_free(tmp_ctx);
270 static int child_setup(struct ctdb_context *ctdb)
272 if (setpgid(0,0) != 0) {
274 DEBUG(DEBUG_ERR,("Failed to create process group for event scripts - %s\n",
279 signal(SIGTERM, sigterm);
283 static char *child_command_string(struct ctdb_context *ctdb,
286 const char *scriptname,
287 enum ctdb_eventscript_call call,
290 const char *str = from_user ? "CTDB_CALLED_BY_USER=1 " : "";
292 /* Allow a setting where we run the actual monitor event
293 from an external source and replace it with
294 a "status" event that just picks up the actual
295 status of the event asynchronously.
297 if ((ctdb->tunable.use_status_events_for_monitoring != 0)
298 && (call == CTDB_EVENT_MONITOR)
300 return talloc_asprintf(ctx, "%s%s/%s %s",
302 ctdb->event_script_dir,
303 scriptname, "status");
305 return talloc_asprintf(ctx, "%s%s/%s %s %s",
307 ctdb->event_script_dir,
309 ctdb_eventscript_call_names[call],
314 static int child_run_one(struct ctdb_context *ctdb,
315 const char *scriptname, const char *cmdstr)
319 ret = system(cmdstr);
320 /* if the system() call was successful, translate ret into the
321 return code from the command
324 ret = WEXITSTATUS(ret);
329 /* 127 could mean it does not exist, 126 non-executable. */
330 if (ret == 127 || ret == 126) {
332 if (!check_executable(ctdb->event_script_dir, scriptname)) {
333 DEBUG(DEBUG_ERR,("Script %s returned status %u. Someone just deleted it?\n",
342 Actually run one event script
343 this function is called and run in the context of a forked child
344 which allows it to do blocking calls such as system()
346 static int child_run_script(struct ctdb_context *ctdb,
348 enum ctdb_eventscript_call call,
350 struct ctdb_script_wire *current)
354 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
356 ret = child_setup(ctdb);
360 cmdstr = child_command_string(ctdb, tmp_ctx, from_user,
361 current->name, call, options);
362 CTDB_NO_MEMORY(ctdb, cmdstr);
364 DEBUG(DEBUG_DEBUG,("Executing event script %s\n",cmdstr));
366 if (current->status) {
367 ret = current->status;
371 ret = child_run_one(ctdb, current->name, cmdstr);
373 talloc_free(tmp_ctx);
377 static void ctdb_event_script_handler(struct event_context *ev, struct fd_event *fde,
378 uint16_t flags, void *p);
380 static int fork_child_for_script(struct ctdb_context *ctdb,
381 struct ctdb_event_script_state *state)
384 struct tevent_fd *fde;
385 struct ctdb_script_wire *current = get_current_script(state);
387 current->start = timeval_current();
391 DEBUG(DEBUG_ERR, (__location__ " pipe failed for child eventscript process\n"));
395 if (!ctdb_fork_with_logging(state, ctdb, current->name, log_event_script_output,
396 state, &state->child)) {
403 /* If we are the child, do the work. */
404 if (state->child == 0) {
407 debug_extra = talloc_asprintf(NULL, "eventscript-%s-%s:",
409 ctdb_eventscript_call_names[state->call]);
411 set_close_on_exec(state->fd[1]);
412 ctdb_set_process_name("ctdb_eventscript");
414 rt = child_run_script(ctdb, state->from_user, state->call, state->options, current);
415 /* We must be able to write PIPEBUF bytes at least; if this
416 somehow fails, the read above will be short. */
417 write(state->fd[1], &rt, sizeof(rt));
423 set_close_on_exec(state->fd[0]);
425 DEBUG(DEBUG_DEBUG, (__location__ " Created PIPE FD:%d to child eventscript process\n", state->fd[0]));
427 /* Set ourselves up to be called when that's done. */
428 fde = event_add_fd(ctdb->ev, state, state->fd[0], EVENT_FD_READ,
429 ctdb_event_script_handler, state);
430 tevent_fd_set_auto_close(fde);
436 Summarize status of this run of scripts.
438 static int script_status(struct ctdb_scripts_wire *scripts)
442 for (i = 0; i < scripts->num_scripts; i++) {
443 switch (scripts->scripts[i].status) {
446 /* Disabled or missing; that's OK. */
452 return scripts->scripts[i].status;
460 /* called when child is finished */
461 static void ctdb_event_script_handler(struct event_context *ev, struct fd_event *fde,
462 uint16_t flags, void *p)
464 struct ctdb_event_script_state *state =
465 talloc_get_type(p, struct ctdb_event_script_state);
466 struct ctdb_script_wire *current = get_current_script(state);
467 struct ctdb_context *ctdb = state->ctdb;
471 DEBUG(DEBUG_ERR,("Eventscript finished but ctdb is NULL\n"));
475 r = read(state->fd[0], ¤t->status, sizeof(current->status));
477 current->status = -errno;
478 } else if (r != sizeof(current->status)) {
479 current->status = -EIO;
482 current->finished = timeval_current();
483 /* valgrind gets overloaded if we run next script as it's still doing
484 * post-execution analysis, so kill finished child here. */
485 if (ctdb->valgrinding) {
486 ctdb_kill(ctdb, state->child, SIGKILL);
491 status = script_status(state->scripts);
493 /* Aborted or finished all scripts? We're done. */
494 if (status != 0 || state->current+1 == state->scripts->num_scripts) {
495 DEBUG(DEBUG_INFO,(__location__ " Eventscript %s %s finished with state %d\n",
496 ctdb_eventscript_call_names[state->call], state->options, status));
498 ctdb->event_script_timeouts = 0;
503 /* Forget about that old fd. */
509 current->status = fork_child_for_script(ctdb, state);
510 if (current->status != 0) {
511 /* This calls the callback. */
516 struct debug_hung_script_state {
517 struct ctdb_context *ctdb;
519 enum ctdb_eventscript_call call;
522 static int debug_hung_script_state_destructor(struct debug_hung_script_state *state)
525 ctdb_kill(state->ctdb, state->child, SIGKILL);
530 static void debug_hung_script_timeout(struct tevent_context *ev, struct tevent_timer *te,
531 struct timeval t, void *p)
533 struct debug_hung_script_state *state =
534 talloc_get_type(p, struct debug_hung_script_state);
539 static void debug_hung_script_done(struct tevent_context *ev, struct tevent_fd *fde,
540 uint16_t flags, void *p)
542 struct debug_hung_script_state *state =
543 talloc_get_type(p, struct debug_hung_script_state);
548 static void ctdb_run_debug_hung_script(struct ctdb_context *ctdb, struct debug_hung_script_state *state)
551 const char * debug_hung_script = ETCDIR "/ctdb/debug-hung-script.sh";
553 struct tevent_timer *ttimer;
554 struct tevent_fd *tfd;
557 DEBUG(DEBUG_ERR,("Failed to create pipe fd for debug hung script\n"));
561 if (!ctdb_fork_with_logging(ctdb, ctdb, "Hung script", NULL, NULL, &pid)) {
562 DEBUG(DEBUG_ERR,("Failed to fork a child process with logging to track hung event script\n"));
568 DEBUG(DEBUG_ERR,("Fork for debug script failed : %s\n",
577 ctdb_set_process_name("ctdb_debug_hung_script");
578 if (getenv("CTDB_DEBUG_HUNG_SCRIPT") != NULL) {
579 debug_hung_script = getenv("CTDB_DEBUG_HUNG_SCRIPT");
584 buf = talloc_asprintf(NULL, "%s %d %s",
585 debug_hung_script, state->child,
586 ctdb_eventscript_call_names[state->call]);
595 ttimer = tevent_add_timer(ctdb->ev, state,
596 timeval_current_ofs(ctdb->tunable.script_timeout, 0),
597 debug_hung_script_timeout, state);
598 if (ttimer == NULL) {
603 tfd = tevent_add_fd(ctdb->ev, state, fd[0], EVENT_FD_READ,
604 debug_hung_script_done, state);
610 tevent_fd_set_auto_close(tfd);
613 /* called when child times out */
614 static void ctdb_event_script_timeout(struct event_context *ev, struct timed_event *te,
615 struct timeval t, void *p)
617 struct ctdb_event_script_state *state = talloc_get_type(p, struct ctdb_event_script_state);
618 struct ctdb_context *ctdb = state->ctdb;
619 struct ctdb_script_wire *current = get_current_script(state);
620 struct debug_hung_script_state *debug_state;
622 DEBUG(DEBUG_ERR,("Event script '%s %s %s' timed out after %.1fs, count: %u, pid: %d\n",
623 current->name, ctdb_eventscript_call_names[state->call], state->options,
624 timeval_elapsed(¤t->start),
625 ctdb->event_script_timeouts, state->child));
627 /* ignore timeouts for these events */
628 switch (state->call) {
629 case CTDB_EVENT_START_RECOVERY:
630 case CTDB_EVENT_RECOVERED:
631 case CTDB_EVENT_TAKE_IP:
632 case CTDB_EVENT_RELEASE_IP:
633 state->scripts->scripts[state->current].status = 0;
634 DEBUG(DEBUG_ERR,("Ignoring hung script for %s call %d\n", state->options, state->call));
637 state->scripts->scripts[state->current].status = -ETIME;
640 debug_state = talloc_zero(ctdb, struct debug_hung_script_state);
641 if (debug_state == NULL) {
646 /* Save information useful for running debug hung script, so
647 * eventscript state can be freed.
649 debug_state->ctdb = ctdb;
650 debug_state->child = state->child;
651 debug_state->call = state->call;
653 /* This destructor will actually kill the hung event script */
654 talloc_set_destructor(debug_state, debug_hung_script_state_destructor);
659 ctdb_run_debug_hung_script(ctdb, debug_state);
663 destroy an event script: kill it if ->child != 0.
665 static int event_script_destructor(struct ctdb_event_script_state *state)
668 struct event_script_callback *callback;
671 DEBUG(DEBUG_ERR,(__location__ " Sending SIGTERM to child pid:%d\n", state->child));
673 if (ctdb_kill(state->ctdb, state->child, SIGTERM) != 0) {
674 DEBUG(DEBUG_ERR,("Failed to kill child process for eventscript, errno %s(%d)\n", strerror(errno), errno));
678 /* If we were the current monitor, we no longer are. */
679 if (state->ctdb->current_monitor == state) {
680 state->ctdb->current_monitor = NULL;
683 /* Save our scripts as the last executed status, if we have them.
684 * See ctdb_event_script_callback_v where we abort monitor event. */
685 if (state->scripts) {
686 talloc_free(state->ctdb->last_status[state->call]);
687 state->ctdb->last_status[state->call] = state->scripts;
688 if (state->current < state->ctdb->last_status[state->call]->num_scripts) {
689 state->ctdb->last_status[state->call]->num_scripts = state->current+1;
693 /* Use last status as result, or "OK" if none. */
694 if (state->ctdb->last_status[state->call]) {
695 status = script_status(state->ctdb->last_status[state->call]);
700 /* This is allowed to free us; talloc will prevent double free anyway,
701 * but beware if you call this outside the destructor!
702 * the callback hangs off a different context so we walk the list
703 * of "active" callbacks until we find the one state points to.
704 * if we cant find it it means the callback has been removed.
706 for (callback = state->ctdb->script_callbacks; callback != NULL; callback = callback->next) {
707 if (callback == state->callback) {
712 state->callback = NULL;
715 /* Make sure destructor doesn't free itself! */
716 talloc_steal(NULL, callback);
717 callback->fn(state->ctdb, status, callback->private_data);
718 talloc_free(callback);
724 static unsigned int count_words(const char *options)
726 unsigned int words = 0;
728 options += strspn(options, " \t");
731 options += strcspn(options, " \t");
732 options += strspn(options, " \t");
737 static bool check_options(enum ctdb_eventscript_call call, const char *options)
740 /* These all take no arguments. */
741 case CTDB_EVENT_INIT:
742 case CTDB_EVENT_SETUP:
743 case CTDB_EVENT_STARTUP:
744 case CTDB_EVENT_START_RECOVERY:
745 case CTDB_EVENT_RECOVERED:
746 case CTDB_EVENT_MONITOR:
747 case CTDB_EVENT_SHUTDOWN:
748 case CTDB_EVENT_IPREALLOCATED:
749 return count_words(options) == 0;
751 case CTDB_EVENT_TAKE_IP: /* interface, IP address, netmask bits. */
752 case CTDB_EVENT_RELEASE_IP:
753 return count_words(options) == 3;
755 case CTDB_EVENT_UPDATE_IP: /* old interface, new interface, IP address, netmask bits. */
756 return count_words(options) == 4;
759 DEBUG(DEBUG_ERR,(__location__ "Unknown ctdb_eventscript_call %u\n", call));
764 static int remove_callback(struct event_script_callback *callback)
766 DLIST_REMOVE(callback->ctdb->script_callbacks, callback);
771 run the event script in the background, calling the callback when
774 static int ctdb_event_script_callback_v(struct ctdb_context *ctdb,
776 void (*callback)(struct ctdb_context *, int, void *),
779 enum ctdb_eventscript_call call,
780 const char *fmt, va_list ap)
782 struct ctdb_event_script_state *state;
784 if (ctdb->recovery_mode != CTDB_RECOVERY_NORMAL) {
785 /* we guarantee that only some specifically allowed event scripts are run
787 const enum ctdb_eventscript_call allowed_calls[] = {
790 CTDB_EVENT_START_RECOVERY,
792 CTDB_EVENT_RELEASE_IP,
793 CTDB_EVENT_IPREALLOCATED,
796 for (i=0;i<ARRAY_SIZE(allowed_calls);i++) {
797 if (call == allowed_calls[i]) break;
799 if (i == ARRAY_SIZE(allowed_calls)) {
800 DEBUG(DEBUG_ERR,("Refusing to run event scripts call '%s' while in recovery\n",
801 ctdb_eventscript_call_names[call]));
806 /* Kill off any running monitor events to run this event. */
807 if (ctdb->current_monitor) {
808 struct ctdb_event_script_state *ms = talloc_get_type(ctdb->current_monitor, struct ctdb_event_script_state);
810 /* Cancel current monitor callback state only if monitoring
811 * context ctdb->monitor->monitor_context has not been freed */
812 if (ms->callback != NULL && !ctdb_stopped_monitoring(ctdb)) {
813 ms->callback->fn(ctdb, -ECANCELED, ms->callback->private_data);
814 talloc_free(ms->callback);
817 /* Discard script status so we don't save to last_status */
818 talloc_free(ctdb->current_monitor->scripts);
819 ctdb->current_monitor->scripts = NULL;
820 talloc_free(ctdb->current_monitor);
821 ctdb->current_monitor = NULL;
824 state = talloc(ctdb->event_script_ctx, struct ctdb_event_script_state);
825 CTDB_NO_MEMORY(ctdb, state);
827 /* The callback isn't done if the context is freed. */
828 state->callback = talloc(mem_ctx, struct event_script_callback);
829 CTDB_NO_MEMORY(ctdb, state->callback);
830 DLIST_ADD(ctdb->script_callbacks, state->callback);
831 talloc_set_destructor(state->callback, remove_callback);
832 state->callback->ctdb = ctdb;
833 state->callback->fn = callback;
834 state->callback->private_data = private_data;
837 state->from_user = from_user;
839 state->options = talloc_vasprintf(state, fmt, ap);
840 state->timeout = timeval_set(ctdb->tunable.script_timeout, 0);
841 state->scripts = NULL;
842 if (state->options == NULL) {
843 DEBUG(DEBUG_ERR, (__location__ " could not allocate state->options\n"));
847 if (!check_options(state->call, state->options)) {
848 DEBUG(DEBUG_ERR, ("Bad eventscript options '%s' for %s\n",
849 ctdb_eventscript_call_names[state->call], state->options));
854 DEBUG(DEBUG_INFO,(__location__ " Starting eventscript %s %s\n",
855 ctdb_eventscript_call_names[state->call],
858 /* This is not a child of state, since we save it in destructor. */
859 state->scripts = ctdb_get_script_list(ctdb, ctdb);
860 if (state->scripts == NULL) {
867 if (!from_user && (call == CTDB_EVENT_MONITOR)) {
868 ctdb->current_monitor = state;
871 talloc_set_destructor(state, event_script_destructor);
874 if (state->scripts->num_scripts == 0) {
879 state->scripts->scripts[0].status = fork_child_for_script(ctdb, state);
880 if (state->scripts->scripts[0].status != 0) {
881 /* Callback is called from destructor, with fail result. */
886 if (!timeval_is_zero(&state->timeout)) {
887 event_add_timed(ctdb->ev, state, timeval_current_ofs(state->timeout.tv_sec, state->timeout.tv_usec), ctdb_event_script_timeout, state);
889 DEBUG(DEBUG_ERR, (__location__ " eventscript %s %s called with no timeout\n",
890 ctdb_eventscript_call_names[state->call],
899 run the event script in the background, calling the callback when
900 finished. If mem_ctx is freed, callback will never be called.
902 int ctdb_event_script_callback(struct ctdb_context *ctdb,
904 void (*callback)(struct ctdb_context *, int, void *),
907 enum ctdb_eventscript_call call,
908 const char *fmt, ...)
914 ret = ctdb_event_script_callback_v(ctdb, mem_ctx, callback, private_data, from_user, call, fmt, ap);
921 struct callback_status {
927 called when ctdb_event_script() finishes
929 static void event_script_callback(struct ctdb_context *ctdb, int status, void *private_data)
931 struct callback_status *s = (struct callback_status *)private_data;
937 run the event script, waiting for it to complete. Used when the caller
938 doesn't want to continue till the event script has finished.
940 int ctdb_event_script_args(struct ctdb_context *ctdb, enum ctdb_eventscript_call call,
941 const char *fmt, ...)
945 struct callback_status status;
948 ret = ctdb_event_script_callback_v(ctdb, ctdb,
949 event_script_callback, &status, false, call, fmt, ap);
958 while (status.done == false && event_loop_once(ctdb->ev) == 0) /* noop */;
960 if (status.status == -ETIME) {
961 DEBUG(DEBUG_ERR, (__location__ " eventscript for '%s' timedout."
962 " Immediately banning ourself for %d seconds\n",
963 ctdb_eventscript_call_names[call],
964 ctdb->tunable.recovery_ban_period));
966 /* Don't ban self if CTDB is starting up or shutting down */
967 if (call != CTDB_EVENT_INIT && call != CTDB_EVENT_SHUTDOWN) {
972 return status.status;
975 int ctdb_event_script(struct ctdb_context *ctdb, enum ctdb_eventscript_call call)
977 /* GCC complains about empty format string, so use %s and "". */
978 return ctdb_event_script_args(ctdb, call, "%s", "");
981 struct eventscript_callback_state {
982 struct ctdb_req_control *c;
986 called when a forced eventscript run has finished
988 static void run_eventscripts_callback(struct ctdb_context *ctdb, int status,
991 struct eventscript_callback_state *state =
992 talloc_get_type(private_data, struct eventscript_callback_state);
994 ctdb_enable_monitoring(ctdb);
997 DEBUG(DEBUG_ERR,(__location__ " Failed to run eventscripts\n"));
1000 ctdb_request_control_reply(ctdb, state->c, NULL, status, NULL);
1001 /* This will free the struct ctdb_event_script_state we are in! */
1007 /* Returns rest of string, or NULL if no match. */
1008 static const char *get_call(const char *p, enum ctdb_eventscript_call *call)
1012 /* Skip any initial whitespace. */
1013 p += strspn(p, " \t");
1015 /* See if we match any. */
1016 for (*call = 0; *call < CTDB_EVENT_MAX; (*call)++) {
1017 len = strlen(ctdb_eventscript_call_names[*call]);
1018 if (strncmp(p, ctdb_eventscript_call_names[*call], len) == 0) {
1019 /* If end of string or whitespace, we're done. */
1020 if (strcspn(p + len, " \t") == 0) {
1029 A control to force running of the eventscripts from the ctdb client tool
1031 int32_t ctdb_run_eventscripts(struct ctdb_context *ctdb,
1032 struct ctdb_req_control *c,
1033 TDB_DATA indata, bool *async_reply)
1036 struct eventscript_callback_state *state;
1037 const char *options;
1038 enum ctdb_eventscript_call call;
1040 /* Figure out what call they want. */
1041 options = get_call((const char *)indata.dptr, &call);
1043 DEBUG(DEBUG_ERR, (__location__ " Invalid event name \"%s\"\n", (const char *)indata.dptr));
1047 if (ctdb->recovery_mode != CTDB_RECOVERY_NORMAL) {
1048 DEBUG(DEBUG_ERR, (__location__ " Aborted running eventscript \"%s\" while in RECOVERY mode\n", indata.dptr));
1052 state = talloc(ctdb->event_script_ctx, struct eventscript_callback_state);
1053 CTDB_NO_MEMORY(ctdb, state);
1055 state->c = talloc_steal(state, c);
1057 DEBUG(DEBUG_NOTICE,("Running eventscripts with arguments %s\n", indata.dptr));
1059 ctdb_disable_monitoring(ctdb);
1061 ret = ctdb_event_script_callback(ctdb,
1062 state, run_eventscripts_callback, state,
1063 true, call, "%s", options);
1066 ctdb_enable_monitoring(ctdb);
1067 DEBUG(DEBUG_ERR,(__location__ " Failed to run eventscripts with arguments %s\n", indata.dptr));
1072 /* tell ctdb_control.c that we will be replying asynchronously */
1073 *async_reply = true;
1080 int32_t ctdb_control_enable_script(struct ctdb_context *ctdb, TDB_DATA indata)
1085 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1087 script = (char *)indata.dptr;
1088 if (indata.dsize == 0) {
1089 DEBUG(DEBUG_ERR,(__location__ " No script specified.\n"));
1090 talloc_free(tmp_ctx);
1093 if (indata.dptr[indata.dsize - 1] != '\0') {
1094 DEBUG(DEBUG_ERR,(__location__ " String is not null terminated.\n"));
1095 talloc_free(tmp_ctx);
1098 if (index(script,'/') != NULL) {
1099 DEBUG(DEBUG_ERR,(__location__ " Script name contains '/'. Failed to enable script %s\n", script));
1100 talloc_free(tmp_ctx);
1105 if (stat(ctdb->event_script_dir, &st) != 0 &&
1107 DEBUG(DEBUG_CRIT,("No event script directory found at '%s'\n", ctdb->event_script_dir));
1108 talloc_free(tmp_ctx);
1113 filename = talloc_asprintf(tmp_ctx, "%s/%s", ctdb->event_script_dir, script);
1114 if (filename == NULL) {
1115 DEBUG(DEBUG_ERR,(__location__ " Failed to create script path\n"));
1116 talloc_free(tmp_ctx);
1120 if (stat(filename, &st) != 0) {
1121 DEBUG(DEBUG_ERR,("Could not stat event script %s. Failed to enable script.\n", filename));
1122 talloc_free(tmp_ctx);
1126 if (chmod(filename, st.st_mode | S_IXUSR) == -1) {
1127 DEBUG(DEBUG_ERR,("Could not chmod %s. Failed to enable script.\n", filename));
1128 talloc_free(tmp_ctx);
1132 talloc_free(tmp_ctx);
1136 int32_t ctdb_control_disable_script(struct ctdb_context *ctdb, TDB_DATA indata)
1141 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1143 script = (char *)indata.dptr;
1144 if (indata.dsize == 0) {
1145 DEBUG(DEBUG_ERR,(__location__ " No script specified.\n"));
1146 talloc_free(tmp_ctx);
1149 if (indata.dptr[indata.dsize - 1] != '\0') {
1150 DEBUG(DEBUG_ERR,(__location__ " String is not null terminated.\n"));
1151 talloc_free(tmp_ctx);
1154 if (index(script,'/') != NULL) {
1155 DEBUG(DEBUG_ERR,(__location__ " Script name contains '/'. Failed to disable script %s\n", script));
1156 talloc_free(tmp_ctx);
1161 if (stat(ctdb->event_script_dir, &st) != 0 &&
1163 DEBUG(DEBUG_CRIT,("No event script directory found at '%s'\n", ctdb->event_script_dir));
1164 talloc_free(tmp_ctx);
1169 filename = talloc_asprintf(tmp_ctx, "%s/%s", ctdb->event_script_dir, script);
1170 if (filename == NULL) {
1171 DEBUG(DEBUG_ERR,(__location__ " Failed to create script path\n"));
1172 talloc_free(tmp_ctx);
1176 if (stat(filename, &st) != 0) {
1177 DEBUG(DEBUG_ERR,("Could not stat event script %s. Failed to disable script.\n", filename));
1178 talloc_free(tmp_ctx);
1182 if (chmod(filename, st.st_mode & ~(S_IXUSR|S_IXGRP|S_IXOTH)) == -1) {
1183 DEBUG(DEBUG_ERR,("Could not chmod %s. Failed to disable script.\n", filename));
1184 talloc_free(tmp_ctx);
1188 talloc_free(tmp_ctx);