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 "lib/events/events.h"
28 #include "../common/rb_tree.h"
32 const char *script_running;
35 static const char *call_names[] = {
48 static void ctdb_event_script_timeout(struct event_context *ev, struct timed_event *te, struct timeval t, void *p);
51 ctdbd sends us a SIGTERM when we should time out the current script
53 static void sigterm(int sig)
55 char tbuf[100], buf[200];
58 DEBUG(DEBUG_ERR,("Timed out running script '%s' after %.1f seconds pid :%d\n",
59 child_state.script_running, timeval_elapsed(&child_state.start), getpid()));
63 strftime(tbuf, sizeof(tbuf)-1, "%Y%m%d%H%M%S", localtime(&t));
64 sprintf(buf, "pstree -p >/tmp/ctdb.event.%s.%d", tbuf, getpid());
67 DEBUG(DEBUG_ERR,("Logged timedout eventscript : %s\n", buf));
69 /* all the child processes will be running in the same process group */
70 kill(-getpgrp(), SIGKILL);
74 struct ctdb_event_script_state {
75 struct ctdb_context *ctdb;
77 /* Warning: this can free us! */
78 void (*callback)(struct ctdb_context *, int, void *);
82 enum ctdb_eventscript_call call;
84 struct timeval timeout;
88 struct ctdb_monitor_script_status {
89 struct ctdb_monitor_script_status *next;
92 struct timeval finished;
97 struct ctdb_monitor_script_status_ctx {
98 struct ctdb_monitor_script_status *scripts;
101 /* called from ctdb_logging when we have received output on STDERR from
102 * one of the eventscripts
104 int ctdb_log_event_script_output(struct ctdb_context *ctdb, char *str, uint16_t len)
106 struct ctdb_monitor_script_status *script;
108 if (ctdb->current_monitor_status_ctx == NULL) {
112 script = ctdb->current_monitor_status_ctx->scripts;
113 if (script == NULL) {
117 if (script->output == NULL) {
118 script->output = talloc_asprintf(script, "%*.*s", len, len, str);
120 script->output = talloc_asprintf_append(script->output, "%*.*s", len, len, str);
126 /* called from the event script child process when we are starting a new
129 int32_t ctdb_control_event_script_init(struct ctdb_context *ctdb)
131 DEBUG(DEBUG_INFO, ("event script init called\n"));
133 if (ctdb->current_monitor_status_ctx == NULL) {
134 DEBUG(DEBUG_ERR,(__location__ " current_monitor_status_ctx is NULL when initing script\n"));
142 /* called from the event script child process when we are star running
145 int32_t ctdb_control_event_script_start(struct ctdb_context *ctdb, TDB_DATA indata)
147 const char *name = (const char *)indata.dptr;
148 struct ctdb_monitor_script_status *script;
150 DEBUG(DEBUG_INFO, ("event script start called : %s\n", name));
152 if (ctdb->current_monitor_status_ctx == NULL) {
153 DEBUG(DEBUG_ERR,(__location__ " current_monitor_status_ctx is NULL when starting script\n"));
157 script = talloc_zero(ctdb->current_monitor_status_ctx, struct ctdb_monitor_script_status);
158 if (script == NULL) {
159 DEBUG(DEBUG_ERR,(__location__ " Failed to talloc ctdb_monitor_script_status for script %s\n", name));
163 script->next = ctdb->current_monitor_status_ctx->scripts;
164 script->name = talloc_strdup(script, name);
165 CTDB_NO_MEMORY(ctdb, script->name);
166 script->start = timeval_current();
167 ctdb->current_monitor_status_ctx->scripts = script;
172 /* called from the event script child process when we have finished running
175 int32_t ctdb_control_event_script_stop(struct ctdb_context *ctdb, TDB_DATA indata)
177 int32_t res = *((int32_t *)indata.dptr);
178 struct ctdb_monitor_script_status *script;
180 if (ctdb->current_monitor_status_ctx == NULL) {
181 DEBUG(DEBUG_ERR,(__location__ " current_monitor_status_ctx is NULL when script finished\n"));
185 script = ctdb->current_monitor_status_ctx->scripts;
186 if (script == NULL) {
187 DEBUG(DEBUG_ERR,(__location__ " script is NULL when the script had finished\n"));
191 script->finished = timeval_current();
192 script->status = res;
194 DEBUG(DEBUG_INFO, ("event script stop called for script:%s duration:%.1f status:%d\n", script->name, timeval_elapsed(&script->start), (int)res));
199 static struct ctdb_monitoring_wire *marshall_monitoring_scripts(TALLOC_CTX *mem_ctx, struct ctdb_monitoring_wire *monitoring_scripts, struct ctdb_monitor_script_status *script)
201 struct ctdb_monitoring_script_wire script_wire;
204 if (script == NULL) {
205 return monitoring_scripts;
207 monitoring_scripts = marshall_monitoring_scripts(mem_ctx, monitoring_scripts, script->next);
208 if (monitoring_scripts == NULL) {
212 bzero(&script_wire, sizeof(struct ctdb_monitoring_script_wire));
213 strncpy(script_wire.name, script->name, MAX_SCRIPT_NAME);
214 script_wire.start = script->start;
215 script_wire.finished = script->finished;
216 script_wire.status = script->status;
217 if (script->output != NULL) {
218 strncpy(script_wire.output, script->output, MAX_SCRIPT_OUTPUT);
221 size = talloc_get_size(monitoring_scripts);
222 monitoring_scripts = talloc_realloc_size(mem_ctx, monitoring_scripts, size + sizeof(struct ctdb_monitoring_script_wire));
223 if (monitoring_scripts == NULL) {
224 DEBUG(DEBUG_ERR,(__location__ " Failed to talloc_resize monitoring_scripts blob\n"));
228 memcpy(&monitoring_scripts->scripts[monitoring_scripts->num_scripts], &script_wire, sizeof(script_wire));
229 monitoring_scripts->num_scripts++;
231 return monitoring_scripts;
234 /* called from the event script child process when we have completed a
237 int32_t ctdb_control_event_script_finished(struct ctdb_context *ctdb)
239 DEBUG(DEBUG_INFO, ("event script finished called\n"));
241 if (ctdb->current_monitor_status_ctx == NULL) {
242 DEBUG(DEBUG_ERR,(__location__ " script_status is NULL when monitoring event finished\n"));
246 talloc_free(ctdb->last_status);
247 ctdb->last_status = talloc_size(ctdb, offsetof(struct ctdb_monitoring_wire, scripts));
248 if (ctdb->last_status == NULL) {
249 DEBUG(DEBUG_ERR,(__location__ " failed to talloc last_status\n"));
253 ctdb->last_status->num_scripts = 0;
254 ctdb->last_status = marshall_monitoring_scripts(ctdb, ctdb->last_status, ctdb->current_monitor_status_ctx->scripts);
255 talloc_free(ctdb->current_monitor_status_ctx);
256 ctdb->current_monitor_status_ctx = NULL;
261 int32_t ctdb_control_get_event_script_status(struct ctdb_context *ctdb, TDB_DATA *outdata)
263 struct ctdb_monitoring_wire *monitoring_scripts = ctdb->last_status;
265 if (monitoring_scripts == NULL) {
266 DEBUG(DEBUG_ERR,(__location__ " last_monitor_status_ctx is NULL when reading status\n"));
270 outdata->dsize = talloc_get_size(monitoring_scripts);
271 outdata->dptr = (uint8_t *)monitoring_scripts;
276 struct ctdb_script_tree_item {
281 struct ctdb_script_list {
282 struct ctdb_script_list *next;
287 /* Return true if OK, otherwise set errno. */
288 static bool check_executable(const char *dir, const char *name)
293 full = talloc_asprintf(NULL, "%s/%s", dir, name);
297 if (stat(full, &st) != 0) {
298 DEBUG(DEBUG_ERR,("Could not stat event script %s: %s\n",
299 full, strerror(errno)));
304 if (!(st.st_mode & S_IXUSR)) {
305 DEBUG(DEBUG_INFO,("Event script %s is not executable. Ignoring this event script\n", full));
315 static struct ctdb_script_list *ctdb_get_script_list(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx)
321 struct ctdb_script_list *head, *tail, *new_item;
322 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
323 struct ctdb_script_tree_item *tree_item;
327 the service specific event scripts
329 if (stat(ctdb->event_script_dir, &st) != 0 &&
331 DEBUG(DEBUG_CRIT,("No event script directory found at '%s'\n", ctdb->event_script_dir));
332 talloc_free(tmp_ctx);
336 /* create a tree to store all the script names in */
337 tree = trbt_create(tmp_ctx, 0);
339 /* scan all directory entries and insert all valid scripts into the
342 dir = opendir(ctdb->event_script_dir);
344 DEBUG(DEBUG_CRIT,("Failed to open event script directory '%s'\n", ctdb->event_script_dir));
345 talloc_free(tmp_ctx);
350 while ((de=readdir(dir)) != NULL) {
354 namlen = strlen(de->d_name);
360 if (de->d_name[namlen-1] == '~') {
361 /* skip files emacs left behind */
365 if (de->d_name[2] != '.') {
369 if (sscanf(de->d_name, "%02u.", &num) != 1) {
373 tree_item = talloc(tree, struct ctdb_script_tree_item);
374 if (tree_item == NULL) {
375 DEBUG(DEBUG_ERR, (__location__ " Failed to allocate new tree item\n"));
376 talloc_free(tmp_ctx);
380 tree_item->error = 0;
381 if (!check_executable(ctdb->event_script_dir, de->d_name)) {
382 tree_item->error = errno;
385 tree_item->name = talloc_strdup(tree_item, de->d_name);
386 if (tree_item->name == NULL) {
387 DEBUG(DEBUG_ERR,(__location__ " Failed to allocate script name.\n"));
388 talloc_free(tmp_ctx);
392 /* store the event script in the tree */
393 trbt_insert32(tree, (num<<16)|count++, tree_item);
401 /* fetch the scripts from the tree one by one and add them to the linked
404 while ((tree_item=trbt_findfirstarray32(tree, 1)) != NULL) {
406 new_item = talloc(tmp_ctx, struct ctdb_script_list);
407 if (new_item == NULL) {
408 DEBUG(DEBUG_ERR, (__location__ " Failed to allocate new list item\n"));
409 talloc_free(tmp_ctx);
413 new_item->next = NULL;
414 new_item->name = talloc_steal(new_item, tree_item->name);
415 new_item->error = tree_item->error;
421 tail->next = new_item;
425 talloc_steal(mem_ctx, new_item);
427 /* remove this script from the tree */
428 talloc_free(tree_item);
431 talloc_free(tmp_ctx);
438 Actually run the event script
439 this function is called and run in the context of a forked child
440 which allows it to do blocking calls such as system()
442 static int ctdb_run_event_script(struct ctdb_context *ctdb,
444 enum ctdb_eventscript_call call,
449 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
450 struct ctdb_script_list *scripts, *current;
452 if (!from_user && call == CTDB_EVENT_MONITOR) {
453 /* This is running in the forked child process. At this stage
454 * we want to switch from being a ctdb daemon into being a
455 * client and connect to the real local daemon.
457 if (switch_from_server_to_client(ctdb) != 0) {
458 DEBUG(DEBUG_CRIT, (__location__ "ERROR: failed to switch eventscript child into client mode. shutting down.\n"));
462 if (ctdb_ctrl_event_script_init(ctdb) != 0) {
468 if (ctdb->recovery_mode != CTDB_RECOVERY_NORMAL) {
469 /* we guarantee that only some specifically allowed event scripts are run
471 const enum ctdb_eventscript_call allowed_calls[] = {
472 CTDB_EVENT_START_RECOVERY, CTDB_EVENT_SHUTDOWN, CTDB_EVENT_RELEASE_IP, CTDB_EVENT_STOPPED };
474 for (i=0;i<ARRAY_SIZE(allowed_calls);i++) {
475 if (call == allowed_calls[i]) break;
477 if (i == ARRAY_SIZE(allowed_calls)) {
478 DEBUG(DEBUG_ERR,("Refusing to run event scripts call '%s' while in recovery\n",
485 if (setpgid(0,0) != 0) {
487 DEBUG(DEBUG_ERR,("Failed to create process group for event scripts - %s\n",
492 signal(SIGTERM, sigterm);
494 child_state.start = timeval_current();
495 child_state.script_running = "startup";
497 scripts = ctdb_get_script_list(ctdb, tmp_ctx);
499 /* fetch the scripts from the tree one by one and execute
502 for (current=scripts; current; current=current->next) {
503 const char *str = from_user ? "CTDB_CALLED_BY_USER=1 " : "";
505 /* Allow a setting where we run the actual monitor event
506 from an external source and replace it with
507 a "status" event that just picks up the actual
508 status of the event asynchronously.
510 if ((ctdb->tunable.use_status_events_for_monitoring != 0)
511 && (call == CTDB_EVENT_MONITOR)
513 cmdstr = talloc_asprintf(tmp_ctx, "%s%s/%s %s",
515 ctdb->event_script_dir,
516 current->name, "status");
518 cmdstr = talloc_asprintf(tmp_ctx, "%s%s/%s %s %s",
520 ctdb->event_script_dir,
521 current->name, call_names[call], options);
523 CTDB_NO_MEMORY(ctdb, cmdstr);
525 DEBUG(DEBUG_INFO,("Executing event script %s\n",cmdstr));
527 child_state.start = timeval_current();
528 child_state.script_running = cmdstr;
530 if (!from_user && call == CTDB_EVENT_MONITOR) {
531 if (ctdb_ctrl_event_script_start(ctdb, current->name) != 0) {
536 if (current->error) {
537 if (ctdb_ctrl_event_script_stop(ctdb, -current->error) != 0) {
544 if (current->error) {
548 ret = system(cmdstr);
549 /* if the system() call was successful, translate ret into the
550 return code from the command
553 ret = WEXITSTATUS(ret);
558 /* 127 could mean it does not exist, 126 non-executable. */
559 if (ret == 127 || ret == 126) {
561 if (!check_executable(ctdb->event_script_dir,
563 DEBUG(DEBUG_ERR,("Script %s returned status %u. Someone just deleted it?\n",
569 if (!from_user && call == CTDB_EVENT_MONITOR) {
570 if (ctdb_ctrl_event_script_stop(ctdb, ret) != 0) {
576 /* now we've reported the per-script error, don't exit the loop
577 * just because it vanished or was disabled. */
578 if (ret == -ENOENT || ret == -ENOEXEC) {
582 /* return an error if the script failed */
588 child_state.start = timeval_current();
589 child_state.script_running = "finished";
591 if (!from_user && call == CTDB_EVENT_MONITOR) {
592 if (ctdb_ctrl_event_script_finished(ctdb) != 0) {
598 talloc_free(tmp_ctx);
602 /* called when child is finished */
603 static void ctdb_event_script_handler(struct event_context *ev, struct fd_event *fde,
604 uint16_t flags, void *p)
606 struct ctdb_event_script_state *state =
607 talloc_get_type(p, struct ctdb_event_script_state);
608 struct ctdb_context *ctdb = state->ctdb;
611 r = read(state->fd[0], &state->cb_status, sizeof(state->cb_status));
613 state->cb_status = -errno;
614 } else if (r != sizeof(state->cb_status)) {
615 state->cb_status = -EIO;
618 DEBUG(DEBUG_INFO,(__location__ " Eventscript %s %s finished with state %d\n",
619 call_names[state->call], state->options, state->cb_status));
622 ctdb->event_script_timeouts = 0;
626 /* called when child times out */
627 static void ctdb_event_script_timeout(struct event_context *ev, struct timed_event *te,
628 struct timeval t, void *p)
630 struct ctdb_event_script_state *state = talloc_get_type(p, struct ctdb_event_script_state);
631 struct ctdb_context *ctdb = state->ctdb;
633 DEBUG(DEBUG_ERR,("Event script timed out : %s %s count : %u pid : %d\n",
634 call_names[state->call], state->options, ctdb->event_script_timeouts, state->child));
636 state->cb_status = -ETIME;
638 if (kill(state->child, 0) != 0) {
639 DEBUG(DEBUG_ERR,("Event script child process already dead, errno %s(%d)\n", strerror(errno), errno));
643 if (state->call == CTDB_EVENT_MONITOR || state->call == CTDB_EVENT_STATUS) {
644 struct ctdb_monitor_script_status *script;
646 if (ctdb->current_monitor_status_ctx != NULL) {
647 script = ctdb->current_monitor_status_ctx->scripts;
648 if (script != NULL) {
649 script->status = state->cb_status;
652 ctdb_control_event_script_finished(ctdb);
660 destroy an event script: kill it if ->child != 0.
662 static int event_script_destructor(struct ctdb_event_script_state *state)
665 DEBUG(DEBUG_ERR,(__location__ " Sending SIGTERM to child pid:%d\n", state->child));
667 if (kill(state->child, SIGTERM) != 0) {
668 DEBUG(DEBUG_ERR,("Failed to kill child process for eventscript, errno %s(%d)\n", strerror(errno), errno));
672 /* This is allowed to free us; talloc will prevent double free anyway,
673 * but beware if you call this outside the destructor! */
674 if (state->callback) {
675 state->callback(state->ctdb, state->cb_status, state->private_data);
681 static unsigned int count_words(const char *options)
683 unsigned int words = 0;
685 options += strspn(options, " \t");
688 options += strcspn(options, " \t");
689 options += strspn(options, " \t");
694 static bool check_options(enum ctdb_eventscript_call call, const char *options)
697 /* These all take no arguments. */
698 case CTDB_EVENT_STARTUP:
699 case CTDB_EVENT_START_RECOVERY:
700 case CTDB_EVENT_RECOVERED:
701 case CTDB_EVENT_STOPPED:
702 case CTDB_EVENT_MONITOR:
703 case CTDB_EVENT_STATUS:
704 case CTDB_EVENT_SHUTDOWN:
705 case CTDB_EVENT_RELOAD:
706 return count_words(options) == 0;
708 case CTDB_EVENT_TAKE_IP: /* interface, IP address, netmask bits. */
709 case CTDB_EVENT_RELEASE_IP:
710 return count_words(options) == 3;
713 DEBUG(DEBUG_ERR,(__location__ "Unknown ctdb_eventscript_call %u\n", call));
719 run the event script in the background, calling the callback when
722 static int ctdb_event_script_callback_v(struct ctdb_context *ctdb,
723 void (*callback)(struct ctdb_context *, int, void *),
726 enum ctdb_eventscript_call call,
727 const char *fmt, va_list ap)
730 struct ctdb_event_script_state *state;
733 if (!from_user && (call == CTDB_EVENT_MONITOR || call == CTDB_EVENT_STATUS)) {
734 /* if this was a "monitor" or a status event, we recycle the
735 context to start a new monitor event
737 if (ctdb->monitor_event_script_ctx != NULL) {
738 talloc_free(ctdb->monitor_event_script_ctx);
739 ctdb->monitor_event_script_ctx = NULL;
741 ctdb->monitor_event_script_ctx = talloc_new(ctdb);
742 mem_ctx = ctdb->monitor_event_script_ctx;
744 if (ctdb->current_monitor_status_ctx != NULL) {
745 talloc_free(ctdb->current_monitor_status_ctx);
746 ctdb->current_monitor_status_ctx = NULL;
749 ctdb->current_monitor_status_ctx = talloc(ctdb, struct ctdb_monitor_script_status_ctx);
750 CTDB_NO_MEMORY(ctdb, ctdb->current_monitor_status_ctx);
751 ctdb->current_monitor_status_ctx->scripts = NULL;
753 /* any other script will first terminate any monitor event */
754 if (ctdb->monitor_event_script_ctx != NULL) {
755 talloc_free(ctdb->monitor_event_script_ctx);
756 ctdb->monitor_event_script_ctx = NULL;
758 /* and then use a context common for all non-monitor events */
759 if (ctdb->other_event_script_ctx == NULL) {
760 ctdb->other_event_script_ctx = talloc_new(ctdb);
762 mem_ctx = ctdb->other_event_script_ctx;
765 state = talloc(mem_ctx, struct ctdb_event_script_state);
766 CTDB_NO_MEMORY(ctdb, state);
769 state->callback = callback;
770 state->private_data = private_data;
772 state->options = talloc_vasprintf(state, fmt, ap);
773 state->timeout = timeval_set(ctdb->tunable.script_timeout, 0);
774 if (state->options == NULL) {
775 DEBUG(DEBUG_ERR, (__location__ " could not allocate state->options\n"));
779 if (!check_options(state->call, state->options)) {
780 DEBUG(DEBUG_ERR, ("Bad eventscript options '%s' for %s\n",
781 call_names[state->call], state->options));
786 DEBUG(DEBUG_INFO,(__location__ " Starting eventscript %s %s\n",
787 call_names[state->call], state->options));
789 ret = pipe(state->fd);
795 state->child = fork();
797 if (state->child == (pid_t)-1) {
804 if (state->child == 0) {
808 set_close_on_exec(state->fd[1]);
810 rt = ctdb_run_event_script(ctdb, from_user, state->call, state->options);
811 /* We must be able to write PIPEBUF bytes at least; if this
812 somehow fails, the read above will be short. */
813 write(state->fd[1], &rt, sizeof(rt));
819 set_close_on_exec(state->fd[0]);
820 talloc_set_destructor(state, event_script_destructor);
822 DEBUG(DEBUG_DEBUG, (__location__ " Created PIPE FD:%d to child eventscript process\n", state->fd[0]));
824 event_add_fd(ctdb->ev, state, state->fd[0], EVENT_FD_READ|EVENT_FD_AUTOCLOSE,
825 ctdb_event_script_handler, state);
827 if (!timeval_is_zero(&state->timeout)) {
828 event_add_timed(ctdb->ev, state, timeval_current_ofs(state->timeout.tv_sec, state->timeout.tv_usec), ctdb_event_script_timeout, state);
830 DEBUG(DEBUG_ERR, (__location__ " eventscript %s %s called with no timeout\n",
831 call_names[state->call], state->options));
839 run the event script in the background, calling the callback when
842 int ctdb_event_script_callback(struct ctdb_context *ctdb,
844 void (*callback)(struct ctdb_context *, int, void *),
847 enum ctdb_eventscript_call call,
848 const char *fmt, ...)
854 ret = ctdb_event_script_callback_v(ctdb, callback, private_data, from_user, call, fmt, ap);
861 struct callback_status {
867 called when ctdb_event_script() finishes
869 static void event_script_callback(struct ctdb_context *ctdb, int status, void *private_data)
871 struct callback_status *s = (struct callback_status *)private_data;
877 run the event script, waiting for it to complete. Used when the caller
878 doesn't want to continue till the event script has finished.
880 int ctdb_event_script_args(struct ctdb_context *ctdb, enum ctdb_eventscript_call call,
881 const char *fmt, ...)
885 struct callback_status status;
888 ret = ctdb_event_script_callback_v(ctdb,
889 event_script_callback, &status, false, call, fmt, ap);
898 while (status.done == false && event_loop_once(ctdb->ev) == 0) /* noop */;
900 if (status.status == -ETIME) {
901 DEBUG(DEBUG_ERR, (__location__ " eventscript for '%s' timedout."
902 " Immediately banning ourself for %d seconds\n",
904 ctdb->tunable.recovery_ban_period));
908 return status.status;
911 int ctdb_event_script(struct ctdb_context *ctdb, enum ctdb_eventscript_call call)
913 /* GCC complains about empty format string, so use %s and "". */
914 return ctdb_event_script_args(ctdb, call, "%s", "");
917 struct eventscript_callback_state {
918 struct ctdb_req_control *c;
922 called when a forced eventscript run has finished
924 static void run_eventscripts_callback(struct ctdb_context *ctdb, int status,
927 struct eventscript_callback_state *state =
928 talloc_get_type(private_data, struct eventscript_callback_state);
930 ctdb_enable_monitoring(ctdb);
933 DEBUG(DEBUG_ERR,(__location__ " Failed to forcibly run eventscripts\n"));
936 ctdb_request_control_reply(ctdb, state->c, NULL, status, NULL);
937 /* This will free the struct ctdb_event_script_state we are in! */
943 /* Returns rest of string, or NULL if no match. */
944 static const char *get_call(const char *p, enum ctdb_eventscript_call *call)
948 /* Skip any initial whitespace. */
949 p += strspn(p, " \t");
951 /* See if we match any. */
952 for (*call = 0; *call < ARRAY_SIZE(call_names); (*call)++) {
953 len = strlen(call_names[*call]);
954 if (strncmp(p, call_names[*call], len) == 0) {
955 /* If end of string or whitespace, we're done. */
956 if (strcspn(p + len, " \t") == 0) {
965 A control to force running of the eventscripts from the ctdb client tool
967 int32_t ctdb_run_eventscripts(struct ctdb_context *ctdb,
968 struct ctdb_req_control *c,
969 TDB_DATA indata, bool *async_reply)
972 struct eventscript_callback_state *state;
974 enum ctdb_eventscript_call call;
976 /* Figure out what call they want. */
977 options = get_call((const char *)indata.dptr, &call);
979 DEBUG(DEBUG_ERR, (__location__ " Invalid forced \"%s\"\n", (const char *)indata.dptr));
983 if (ctdb->recovery_mode != CTDB_RECOVERY_NORMAL) {
984 DEBUG(DEBUG_ERR, (__location__ " Aborted running eventscript \"%s\" while in RECOVERY mode\n", indata.dptr));
988 state = talloc(ctdb->other_event_script_ctx, struct eventscript_callback_state);
989 CTDB_NO_MEMORY(ctdb, state);
991 state->c = talloc_steal(state, c);
993 DEBUG(DEBUG_NOTICE,("Forced running of eventscripts with arguments %s\n", indata.dptr));
995 ctdb_disable_monitoring(ctdb);
997 ret = ctdb_event_script_callback(ctdb,
998 state, run_eventscripts_callback, state,
999 true, call, "%s", options);
1002 ctdb_enable_monitoring(ctdb);
1003 DEBUG(DEBUG_ERR,(__location__ " Failed to run eventscripts with arguments %s\n", indata.dptr));
1008 /* tell ctdb_control.c that we will be replying asynchronously */
1009 *async_reply = true;
1016 int32_t ctdb_control_enable_script(struct ctdb_context *ctdb, TDB_DATA indata)
1021 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1023 script = (char *)indata.dptr;
1024 if (indata.dsize == 0) {
1025 DEBUG(DEBUG_ERR,(__location__ " No script specified.\n"));
1026 talloc_free(tmp_ctx);
1029 if (indata.dptr[indata.dsize - 1] != '\0') {
1030 DEBUG(DEBUG_ERR,(__location__ " String is not null terminated.\n"));
1031 talloc_free(tmp_ctx);
1034 if (index(script,'/') != NULL) {
1035 DEBUG(DEBUG_ERR,(__location__ " Script name contains '/'. Failed to enable script %s\n", script));
1036 talloc_free(tmp_ctx);
1041 if (stat(ctdb->event_script_dir, &st) != 0 &&
1043 DEBUG(DEBUG_CRIT,("No event script directory found at '%s'\n", ctdb->event_script_dir));
1044 talloc_free(tmp_ctx);
1049 filename = talloc_asprintf(tmp_ctx, "%s/%s", ctdb->event_script_dir, script);
1050 if (filename == NULL) {
1051 DEBUG(DEBUG_ERR,(__location__ " Failed to create script path\n"));
1052 talloc_free(tmp_ctx);
1056 if (stat(filename, &st) != 0) {
1057 DEBUG(DEBUG_ERR,("Could not stat event script %s. Failed to enable script.\n", filename));
1058 talloc_free(tmp_ctx);
1062 if (chmod(filename, st.st_mode | S_IXUSR) == -1) {
1063 DEBUG(DEBUG_ERR,("Could not chmod %s. Failed to enable script.\n", filename));
1064 talloc_free(tmp_ctx);
1068 talloc_free(tmp_ctx);
1072 int32_t ctdb_control_disable_script(struct ctdb_context *ctdb, TDB_DATA indata)
1077 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1079 script = (char *)indata.dptr;
1080 if (indata.dsize == 0) {
1081 DEBUG(DEBUG_ERR,(__location__ " No script specified.\n"));
1082 talloc_free(tmp_ctx);
1085 if (indata.dptr[indata.dsize - 1] != '\0') {
1086 DEBUG(DEBUG_ERR,(__location__ " String is not null terminated.\n"));
1087 talloc_free(tmp_ctx);
1090 if (index(script,'/') != NULL) {
1091 DEBUG(DEBUG_ERR,(__location__ " Script name contains '/'. Failed to disable script %s\n", script));
1092 talloc_free(tmp_ctx);
1097 if (stat(ctdb->event_script_dir, &st) != 0 &&
1099 DEBUG(DEBUG_CRIT,("No event script directory found at '%s'\n", ctdb->event_script_dir));
1100 talloc_free(tmp_ctx);
1105 filename = talloc_asprintf(tmp_ctx, "%s/%s", ctdb->event_script_dir, script);
1106 if (filename == NULL) {
1107 DEBUG(DEBUG_ERR,(__location__ " Failed to create script path\n"));
1108 talloc_free(tmp_ctx);
1112 if (stat(filename, &st) != 0) {
1113 DEBUG(DEBUG_ERR,("Could not stat event script %s. Failed to disable script.\n", filename));
1114 talloc_free(tmp_ctx);
1118 if (chmod(filename, st.st_mode & ~(S_IXUSR|S_IXGRP|S_IXOTH)) == -1) {
1119 DEBUG(DEBUG_ERR,("Could not chmod %s. Failed to disable script.\n", filename));
1120 talloc_free(tmp_ctx);
1124 talloc_free(tmp_ctx);