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 *);
83 enum ctdb_eventscript_call call;
85 struct timeval timeout;
87 struct ctdb_monitor_script_status *scripts;
88 struct ctdb_script_list *script_list;
92 struct ctdb_monitor_script_status {
93 struct ctdb_monitor_script_status *next;
96 struct timeval finished;
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 == NULL) {
112 script = ctdb->current_monitor->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 /* starting a new monitor event */
127 static int32_t ctdb_control_event_script_init(struct ctdb_context *ctdb)
129 DEBUG(DEBUG_INFO, ("event script init called\n"));
131 if (ctdb->current_monitor == NULL) {
132 DEBUG(DEBUG_ERR,(__location__ " current_monitor_status_ctx is NULL when initing script\n"));
140 /* starting a new child to run a monitor event script */
141 static int32_t ctdb_control_event_script_start(struct ctdb_context *ctdb, const char *name)
143 struct ctdb_monitor_script_status *script;
145 DEBUG(DEBUG_INFO, ("event script start called : %s\n", name));
147 if (ctdb->current_monitor == NULL) {
148 DEBUG(DEBUG_ERR,(__location__ " current_monitor_status_ctx is NULL when starting script\n"));
152 script = talloc_zero(ctdb->current_monitor, struct ctdb_monitor_script_status);
153 if (script == NULL) {
154 DEBUG(DEBUG_ERR,(__location__ " Failed to talloc ctdb_monitor_script_status for script %s\n", name));
158 script->next = ctdb->current_monitor->scripts;
159 script->name = talloc_strdup(script, name);
160 CTDB_NO_MEMORY(ctdb, script->name);
161 script->start = timeval_current();
162 ctdb->current_monitor->scripts = script;
167 /* finished a child running a monitor event script */
168 static int32_t ctdb_control_event_script_stop(struct ctdb_context *ctdb, int res)
170 struct ctdb_monitor_script_status *script;
172 if (ctdb->current_monitor == NULL) {
173 DEBUG(DEBUG_ERR,(__location__ " current_monitor_status_ctx is NULL when script finished\n"));
177 script = ctdb->current_monitor->scripts;
178 if (script == NULL) {
179 DEBUG(DEBUG_ERR,(__location__ " script is NULL when the script had finished\n"));
183 script->finished = timeval_current();
184 script->status = res;
186 DEBUG(DEBUG_INFO, ("event script stop called for script:%s duration:%.1f status:%d\n", script->name, timeval_elapsed(&script->start), (int)res));
191 static struct ctdb_monitoring_wire *marshall_monitoring_scripts(TALLOC_CTX *mem_ctx, struct ctdb_monitoring_wire *monitoring_scripts, struct ctdb_monitor_script_status *script)
193 struct ctdb_monitoring_script_wire script_wire;
196 if (script == NULL) {
197 return monitoring_scripts;
199 monitoring_scripts = marshall_monitoring_scripts(mem_ctx, monitoring_scripts, script->next);
200 if (monitoring_scripts == NULL) {
204 bzero(&script_wire, sizeof(struct ctdb_monitoring_script_wire));
205 strncpy(script_wire.name, script->name, MAX_SCRIPT_NAME);
206 script_wire.start = script->start;
207 script_wire.finished = script->finished;
208 script_wire.status = script->status;
209 if (script->output != NULL) {
210 strncpy(script_wire.output, script->output, MAX_SCRIPT_OUTPUT);
213 size = talloc_get_size(monitoring_scripts);
214 monitoring_scripts = talloc_realloc_size(mem_ctx, monitoring_scripts, size + sizeof(struct ctdb_monitoring_script_wire));
215 if (monitoring_scripts == NULL) {
216 DEBUG(DEBUG_ERR,(__location__ " Failed to talloc_resize monitoring_scripts blob\n"));
220 memcpy(&monitoring_scripts->scripts[monitoring_scripts->num_scripts], &script_wire, sizeof(script_wire));
221 monitoring_scripts->num_scripts++;
223 return monitoring_scripts;
226 /* called when all event script child processes are done */
227 static int32_t ctdb_control_event_script_finished(struct ctdb_context *ctdb)
229 DEBUG(DEBUG_INFO, ("event script finished called\n"));
231 if (ctdb->current_monitor == NULL) {
232 DEBUG(DEBUG_ERR,(__location__ " script_status is NULL when monitoring event finished\n"));
236 talloc_free(ctdb->last_status);
237 ctdb->last_status = talloc_size(ctdb, offsetof(struct ctdb_monitoring_wire, scripts));
238 if (ctdb->last_status == NULL) {
239 DEBUG(DEBUG_ERR,(__location__ " failed to talloc last_status\n"));
243 ctdb->last_status->num_scripts = 0;
244 ctdb->last_status = marshall_monitoring_scripts(ctdb, ctdb->last_status, ctdb->current_monitor->scripts);
245 talloc_free(ctdb->current_monitor->scripts);
246 ctdb->current_monitor->scripts = NULL;
251 int32_t ctdb_control_get_event_script_status(struct ctdb_context *ctdb, TDB_DATA *outdata)
253 struct ctdb_monitoring_wire *monitoring_scripts = ctdb->last_status;
255 if (monitoring_scripts == NULL) {
256 DEBUG(DEBUG_ERR,(__location__ " last_monitor_status_ctx is NULL when reading status\n"));
260 outdata->dsize = talloc_get_size(monitoring_scripts);
261 outdata->dptr = (uint8_t *)monitoring_scripts;
266 struct ctdb_script_tree_item {
271 struct ctdb_script_list {
272 struct ctdb_script_list *next;
277 /* Return true if OK, otherwise set errno. */
278 static bool check_executable(const char *dir, const char *name)
283 full = talloc_asprintf(NULL, "%s/%s", dir, name);
287 if (stat(full, &st) != 0) {
288 DEBUG(DEBUG_ERR,("Could not stat event script %s: %s\n",
289 full, strerror(errno)));
294 if (!(st.st_mode & S_IXUSR)) {
295 DEBUG(DEBUG_INFO,("Event script %s is not executable. Ignoring this event script\n", full));
305 static struct ctdb_script_list *ctdb_get_script_list(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx)
311 struct ctdb_script_list *head, *tail, *new_item;
312 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
313 struct ctdb_script_tree_item *tree_item;
317 the service specific event scripts
319 if (stat(ctdb->event_script_dir, &st) != 0 &&
321 DEBUG(DEBUG_CRIT,("No event script directory found at '%s'\n", ctdb->event_script_dir));
322 talloc_free(tmp_ctx);
326 /* create a tree to store all the script names in */
327 tree = trbt_create(tmp_ctx, 0);
329 /* scan all directory entries and insert all valid scripts into the
332 dir = opendir(ctdb->event_script_dir);
334 DEBUG(DEBUG_CRIT,("Failed to open event script directory '%s'\n", ctdb->event_script_dir));
335 talloc_free(tmp_ctx);
340 while ((de=readdir(dir)) != NULL) {
344 namlen = strlen(de->d_name);
350 if (de->d_name[namlen-1] == '~') {
351 /* skip files emacs left behind */
355 if (de->d_name[2] != '.') {
359 if (sscanf(de->d_name, "%02u.", &num) != 1) {
363 tree_item = talloc(tree, struct ctdb_script_tree_item);
364 if (tree_item == NULL) {
365 DEBUG(DEBUG_ERR, (__location__ " Failed to allocate new tree item\n"));
366 talloc_free(tmp_ctx);
370 tree_item->error = 0;
371 if (!check_executable(ctdb->event_script_dir, de->d_name)) {
372 tree_item->error = errno;
375 tree_item->name = talloc_strdup(tree_item, de->d_name);
376 if (tree_item->name == NULL) {
377 DEBUG(DEBUG_ERR,(__location__ " Failed to allocate script name.\n"));
378 talloc_free(tmp_ctx);
382 /* store the event script in the tree */
383 trbt_insert32(tree, (num<<16)|count++, tree_item);
391 /* fetch the scripts from the tree one by one and add them to the linked
394 while ((tree_item=trbt_findfirstarray32(tree, 1)) != NULL) {
396 new_item = talloc(tmp_ctx, struct ctdb_script_list);
397 if (new_item == NULL) {
398 DEBUG(DEBUG_ERR, (__location__ " Failed to allocate new list item\n"));
399 talloc_free(tmp_ctx);
403 new_item->next = NULL;
404 new_item->name = talloc_steal(new_item, tree_item->name);
405 new_item->error = tree_item->error;
411 tail->next = new_item;
415 talloc_steal(mem_ctx, new_item);
417 /* remove this script from the tree */
418 talloc_free(tree_item);
421 talloc_free(tmp_ctx);
425 static int child_setup(struct ctdb_context *ctdb)
427 if (setpgid(0,0) != 0) {
429 DEBUG(DEBUG_ERR,("Failed to create process group for event scripts - %s\n",
434 signal(SIGTERM, sigterm);
438 static char *child_command_string(struct ctdb_context *ctdb,
441 const char *scriptname,
442 enum ctdb_eventscript_call call,
445 const char *str = from_user ? "CTDB_CALLED_BY_USER=1 " : "";
447 /* Allow a setting where we run the actual monitor event
448 from an external source and replace it with
449 a "status" event that just picks up the actual
450 status of the event asynchronously.
452 if ((ctdb->tunable.use_status_events_for_monitoring != 0)
453 && (call == CTDB_EVENT_MONITOR)
455 return talloc_asprintf(ctx, "%s%s/%s %s",
457 ctdb->event_script_dir,
458 scriptname, "status");
460 return talloc_asprintf(ctx, "%s%s/%s %s %s",
462 ctdb->event_script_dir,
463 scriptname, call_names[call], options);
467 static int child_run_one(struct ctdb_context *ctdb,
468 const char *scriptname, const char *cmdstr)
472 ret = system(cmdstr);
473 /* if the system() call was successful, translate ret into the
474 return code from the command
477 ret = WEXITSTATUS(ret);
482 /* 127 could mean it does not exist, 126 non-executable. */
483 if (ret == 127 || ret == 126) {
485 if (!check_executable(ctdb->event_script_dir, scriptname)) {
486 DEBUG(DEBUG_ERR,("Script %s returned status %u. Someone just deleted it?\n",
495 Actually run one event script
496 this function is called and run in the context of a forked child
497 which allows it to do blocking calls such as system()
499 static int child_run_script(struct ctdb_context *ctdb,
501 enum ctdb_eventscript_call call,
503 struct ctdb_script_list *current)
507 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
509 child_state.start = timeval_current();
510 ret = child_setup(ctdb);
514 cmdstr = child_command_string(ctdb, tmp_ctx, from_user,
515 current->name, call, options);
516 CTDB_NO_MEMORY(ctdb, cmdstr);
517 child_state.script_running = cmdstr;
519 DEBUG(DEBUG_INFO,("Executing event script %s\n",cmdstr));
521 if (current->error) {
522 ret = -current->error;
526 ret = child_run_one(ctdb, current->name, cmdstr);
528 talloc_free(tmp_ctx);
532 static void ctdb_event_script_handler(struct event_context *ev, struct fd_event *fde,
533 uint16_t flags, void *p);
535 static int fork_child_for_script(struct ctdb_context *ctdb,
536 struct ctdb_event_script_state *state)
540 if (!state->from_user && state->call == CTDB_EVENT_MONITOR) {
541 ctdb_control_event_script_start(ctdb, state->script_list->name);
546 DEBUG(DEBUG_ERR, (__location__ " pipe failed for child eventscript process\n"));
550 state->child = fork();
552 if (state->child == (pid_t)-1) {
554 DEBUG(DEBUG_ERR, (__location__ " fork failed for child eventscript process\n"));
560 if (state->child == 0) {
564 set_close_on_exec(state->fd[1]);
566 rt = child_run_script(ctdb, state->from_user, state->call, state->options, state->script_list);
567 /* We must be able to write PIPEBUF bytes at least; if this
568 somehow fails, the read above will be short. */
569 write(state->fd[1], &rt, sizeof(rt));
575 set_close_on_exec(state->fd[0]);
577 DEBUG(DEBUG_DEBUG, (__location__ " Created PIPE FD:%d to child eventscript process\n", state->fd[0]));
579 /* Set ourselves up to be called when that's done. */
580 event_add_fd(ctdb->ev, state, state->fd[0], EVENT_FD_READ|EVENT_FD_AUTOCLOSE,
581 ctdb_event_script_handler, state);
585 /* called when child is finished */
586 static void ctdb_event_script_handler(struct event_context *ev, struct fd_event *fde,
587 uint16_t flags, void *p)
589 struct ctdb_event_script_state *state =
590 talloc_get_type(p, struct ctdb_event_script_state);
591 struct ctdb_context *ctdb = state->ctdb;
594 r = read(state->fd[0], &state->cb_status, sizeof(state->cb_status));
596 state->cb_status = -errno;
597 } else if (r != sizeof(state->cb_status)) {
598 state->cb_status = -EIO;
601 if (!state->from_user && state->call == CTDB_EVENT_MONITOR) {
602 ctdb_control_event_script_stop(ctdb, state->cb_status);
605 /* don't stop just because it vanished or was disabled. */
606 if (state->cb_status == -ENOENT || state->cb_status == -ENOEXEC) {
607 state->cb_status = 0;
611 state->script_list = state->script_list->next;
613 /* Aborted or finished all scripts? We're done. */
614 if (state->cb_status != 0 || state->script_list == NULL) {
615 DEBUG(DEBUG_INFO,(__location__ " Eventscript %s %s finished with state %d\n",
616 call_names[state->call], state->options, state->cb_status));
618 if (!state->from_user && state->call == CTDB_EVENT_MONITOR) {
619 ctdb_control_event_script_finished(ctdb);
621 ctdb->event_script_timeouts = 0;
626 /* Forget about that old fd. */
630 state->cb_status = fork_child_for_script(ctdb, state);
631 if (state->cb_status != 0) {
632 if (!state->from_user && state->call == CTDB_EVENT_MONITOR) {
633 ctdb_control_event_script_finished(ctdb);
635 /* This calls the callback. */
640 /* called when child times out */
641 static void ctdb_event_script_timeout(struct event_context *ev, struct timed_event *te,
642 struct timeval t, void *p)
644 struct ctdb_event_script_state *state = talloc_get_type(p, struct ctdb_event_script_state);
645 struct ctdb_context *ctdb = state->ctdb;
647 DEBUG(DEBUG_ERR,("Event script timed out : %s %s count : %u pid : %d\n",
648 call_names[state->call], state->options, ctdb->event_script_timeouts, state->child));
650 state->cb_status = -ETIME;
652 if (kill(state->child, 0) != 0) {
653 DEBUG(DEBUG_ERR,("Event script child process already dead, errno %s(%d)\n", strerror(errno), errno));
657 if (state->call == CTDB_EVENT_MONITOR || state->call == CTDB_EVENT_STATUS) {
658 struct ctdb_monitor_script_status *script;
660 if (ctdb->current_monitor != NULL) {
661 script = ctdb->current_monitor->scripts;
662 if (script != NULL) {
663 script->status = state->cb_status;
666 ctdb_control_event_script_finished(ctdb);
674 destroy an event script: kill it if ->child != 0.
676 static int event_script_destructor(struct ctdb_event_script_state *state)
679 DEBUG(DEBUG_ERR,(__location__ " Sending SIGTERM to child pid:%d\n", state->child));
681 if (kill(state->child, SIGTERM) != 0) {
682 DEBUG(DEBUG_ERR,("Failed to kill child process for eventscript, errno %s(%d)\n", strerror(errno), errno));
686 /* If we were the current monitor, we no longer are. */
687 if (state->ctdb->current_monitor == state) {
688 state->ctdb->current_monitor = NULL;
691 /* This is allowed to free us; talloc will prevent double free anyway,
692 * but beware if you call this outside the destructor! */
693 if (state->callback) {
694 state->callback(state->ctdb, state->cb_status, state->private_data);
700 static unsigned int count_words(const char *options)
702 unsigned int words = 0;
704 options += strspn(options, " \t");
707 options += strcspn(options, " \t");
708 options += strspn(options, " \t");
713 static bool check_options(enum ctdb_eventscript_call call, const char *options)
716 /* These all take no arguments. */
717 case CTDB_EVENT_STARTUP:
718 case CTDB_EVENT_START_RECOVERY:
719 case CTDB_EVENT_RECOVERED:
720 case CTDB_EVENT_STOPPED:
721 case CTDB_EVENT_MONITOR:
722 case CTDB_EVENT_STATUS:
723 case CTDB_EVENT_SHUTDOWN:
724 case CTDB_EVENT_RELOAD:
725 return count_words(options) == 0;
727 case CTDB_EVENT_TAKE_IP: /* interface, IP address, netmask bits. */
728 case CTDB_EVENT_RELEASE_IP:
729 return count_words(options) == 3;
732 DEBUG(DEBUG_ERR,(__location__ "Unknown ctdb_eventscript_call %u\n", call));
738 run the event script in the background, calling the callback when
741 static int ctdb_event_script_callback_v(struct ctdb_context *ctdb,
742 void (*callback)(struct ctdb_context *, int, void *),
745 enum ctdb_eventscript_call call,
746 const char *fmt, va_list ap)
748 struct ctdb_event_script_state *state;
751 state = talloc(ctdb->event_script_ctx, struct ctdb_event_script_state);
752 CTDB_NO_MEMORY(ctdb, state);
755 state->callback = callback;
756 state->private_data = private_data;
757 state->from_user = from_user;
759 state->options = talloc_vasprintf(state, fmt, ap);
760 state->timeout = timeval_set(ctdb->tunable.script_timeout, 0);
761 state->scripts = NULL;
762 if (state->options == NULL) {
763 DEBUG(DEBUG_ERR, (__location__ " could not allocate state->options\n"));
767 if (!check_options(state->call, state->options)) {
768 DEBUG(DEBUG_ERR, ("Bad eventscript options '%s' for %s\n",
769 call_names[state->call], state->options));
774 if (ctdb->recovery_mode != CTDB_RECOVERY_NORMAL) {
775 /* we guarantee that only some specifically allowed event scripts are run
777 const enum ctdb_eventscript_call allowed_calls[] = {
778 CTDB_EVENT_START_RECOVERY, CTDB_EVENT_SHUTDOWN, CTDB_EVENT_RELEASE_IP, CTDB_EVENT_STOPPED };
780 for (i=0;i<ARRAY_SIZE(allowed_calls);i++) {
781 if (call == allowed_calls[i]) break;
783 if (i == ARRAY_SIZE(allowed_calls)) {
784 DEBUG(DEBUG_ERR,("Refusing to run event scripts call '%s' while in recovery\n",
791 /* Kill off any running monitor events to run this event. */
792 talloc_free(ctdb->current_monitor);
793 ctdb->current_monitor = NULL;
795 if (!from_user && (call == CTDB_EVENT_MONITOR || call == CTDB_EVENT_STATUS)) {
796 ctdb->current_monitor = state;
799 DEBUG(DEBUG_INFO,(__location__ " Starting eventscript %s %s\n",
800 call_names[state->call], state->options));
802 state->script_list = ctdb_get_script_list(ctdb, state);
803 if (state->script_list == NULL) {
808 if (!state->from_user && state->call == CTDB_EVENT_MONITOR) {
809 ctdb_control_event_script_init(ctdb);
812 ret = fork_child_for_script(ctdb, state);
818 talloc_set_destructor(state, event_script_destructor);
819 if (!timeval_is_zero(&state->timeout)) {
820 event_add_timed(ctdb->ev, state, timeval_current_ofs(state->timeout.tv_sec, state->timeout.tv_usec), ctdb_event_script_timeout, state);
822 DEBUG(DEBUG_ERR, (__location__ " eventscript %s %s called with no timeout\n",
823 call_names[state->call], state->options));
831 run the event script in the background, calling the callback when
834 int ctdb_event_script_callback(struct ctdb_context *ctdb,
836 void (*callback)(struct ctdb_context *, int, void *),
839 enum ctdb_eventscript_call call,
840 const char *fmt, ...)
846 ret = ctdb_event_script_callback_v(ctdb, callback, private_data, from_user, call, fmt, ap);
853 struct callback_status {
859 called when ctdb_event_script() finishes
861 static void event_script_callback(struct ctdb_context *ctdb, int status, void *private_data)
863 struct callback_status *s = (struct callback_status *)private_data;
869 run the event script, waiting for it to complete. Used when the caller
870 doesn't want to continue till the event script has finished.
872 int ctdb_event_script_args(struct ctdb_context *ctdb, enum ctdb_eventscript_call call,
873 const char *fmt, ...)
877 struct callback_status status;
880 ret = ctdb_event_script_callback_v(ctdb,
881 event_script_callback, &status, false, call, fmt, ap);
890 while (status.done == false && event_loop_once(ctdb->ev) == 0) /* noop */;
892 if (status.status == -ETIME) {
893 DEBUG(DEBUG_ERR, (__location__ " eventscript for '%s' timedout."
894 " Immediately banning ourself for %d seconds\n",
896 ctdb->tunable.recovery_ban_period));
900 return status.status;
903 int ctdb_event_script(struct ctdb_context *ctdb, enum ctdb_eventscript_call call)
905 /* GCC complains about empty format string, so use %s and "". */
906 return ctdb_event_script_args(ctdb, call, "%s", "");
909 struct eventscript_callback_state {
910 struct ctdb_req_control *c;
914 called when a forced eventscript run has finished
916 static void run_eventscripts_callback(struct ctdb_context *ctdb, int status,
919 struct eventscript_callback_state *state =
920 talloc_get_type(private_data, struct eventscript_callback_state);
922 ctdb_enable_monitoring(ctdb);
925 DEBUG(DEBUG_ERR,(__location__ " Failed to forcibly run eventscripts\n"));
928 ctdb_request_control_reply(ctdb, state->c, NULL, status, NULL);
929 /* This will free the struct ctdb_event_script_state we are in! */
935 /* Returns rest of string, or NULL if no match. */
936 static const char *get_call(const char *p, enum ctdb_eventscript_call *call)
940 /* Skip any initial whitespace. */
941 p += strspn(p, " \t");
943 /* See if we match any. */
944 for (*call = 0; *call < ARRAY_SIZE(call_names); (*call)++) {
945 len = strlen(call_names[*call]);
946 if (strncmp(p, call_names[*call], len) == 0) {
947 /* If end of string or whitespace, we're done. */
948 if (strcspn(p + len, " \t") == 0) {
957 A control to force running of the eventscripts from the ctdb client tool
959 int32_t ctdb_run_eventscripts(struct ctdb_context *ctdb,
960 struct ctdb_req_control *c,
961 TDB_DATA indata, bool *async_reply)
964 struct eventscript_callback_state *state;
966 enum ctdb_eventscript_call call;
968 /* Figure out what call they want. */
969 options = get_call((const char *)indata.dptr, &call);
971 DEBUG(DEBUG_ERR, (__location__ " Invalid forced \"%s\"\n", (const char *)indata.dptr));
975 if (ctdb->recovery_mode != CTDB_RECOVERY_NORMAL) {
976 DEBUG(DEBUG_ERR, (__location__ " Aborted running eventscript \"%s\" while in RECOVERY mode\n", indata.dptr));
980 state = talloc(ctdb->event_script_ctx, struct eventscript_callback_state);
981 CTDB_NO_MEMORY(ctdb, state);
983 state->c = talloc_steal(state, c);
985 DEBUG(DEBUG_NOTICE,("Forced running of eventscripts with arguments %s\n", indata.dptr));
987 ctdb_disable_monitoring(ctdb);
989 ret = ctdb_event_script_callback(ctdb,
990 state, run_eventscripts_callback, state,
991 true, call, "%s", options);
994 ctdb_enable_monitoring(ctdb);
995 DEBUG(DEBUG_ERR,(__location__ " Failed to run eventscripts with arguments %s\n", indata.dptr));
1000 /* tell ctdb_control.c that we will be replying asynchronously */
1001 *async_reply = true;
1008 int32_t ctdb_control_enable_script(struct ctdb_context *ctdb, TDB_DATA indata)
1013 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1015 script = (char *)indata.dptr;
1016 if (indata.dsize == 0) {
1017 DEBUG(DEBUG_ERR,(__location__ " No script specified.\n"));
1018 talloc_free(tmp_ctx);
1021 if (indata.dptr[indata.dsize - 1] != '\0') {
1022 DEBUG(DEBUG_ERR,(__location__ " String is not null terminated.\n"));
1023 talloc_free(tmp_ctx);
1026 if (index(script,'/') != NULL) {
1027 DEBUG(DEBUG_ERR,(__location__ " Script name contains '/'. Failed to enable script %s\n", script));
1028 talloc_free(tmp_ctx);
1033 if (stat(ctdb->event_script_dir, &st) != 0 &&
1035 DEBUG(DEBUG_CRIT,("No event script directory found at '%s'\n", ctdb->event_script_dir));
1036 talloc_free(tmp_ctx);
1041 filename = talloc_asprintf(tmp_ctx, "%s/%s", ctdb->event_script_dir, script);
1042 if (filename == NULL) {
1043 DEBUG(DEBUG_ERR,(__location__ " Failed to create script path\n"));
1044 talloc_free(tmp_ctx);
1048 if (stat(filename, &st) != 0) {
1049 DEBUG(DEBUG_ERR,("Could not stat event script %s. Failed to enable script.\n", filename));
1050 talloc_free(tmp_ctx);
1054 if (chmod(filename, st.st_mode | S_IXUSR) == -1) {
1055 DEBUG(DEBUG_ERR,("Could not chmod %s. Failed to enable script.\n", filename));
1056 talloc_free(tmp_ctx);
1060 talloc_free(tmp_ctx);
1064 int32_t ctdb_control_disable_script(struct ctdb_context *ctdb, TDB_DATA indata)
1069 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1071 script = (char *)indata.dptr;
1072 if (indata.dsize == 0) {
1073 DEBUG(DEBUG_ERR,(__location__ " No script specified.\n"));
1074 talloc_free(tmp_ctx);
1077 if (indata.dptr[indata.dsize - 1] != '\0') {
1078 DEBUG(DEBUG_ERR,(__location__ " String is not null terminated.\n"));
1079 talloc_free(tmp_ctx);
1082 if (index(script,'/') != NULL) {
1083 DEBUG(DEBUG_ERR,(__location__ " Script name contains '/'. Failed to disable script %s\n", script));
1084 talloc_free(tmp_ctx);
1089 if (stat(ctdb->event_script_dir, &st) != 0 &&
1091 DEBUG(DEBUG_CRIT,("No event script directory found at '%s'\n", ctdb->event_script_dir));
1092 talloc_free(tmp_ctx);
1097 filename = talloc_asprintf(tmp_ctx, "%s/%s", ctdb->event_script_dir, script);
1098 if (filename == NULL) {
1099 DEBUG(DEBUG_ERR,(__location__ " Failed to create script path\n"));
1100 talloc_free(tmp_ctx);
1104 if (stat(filename, &st) != 0) {
1105 DEBUG(DEBUG_ERR,("Could not stat event script %s. Failed to disable script.\n", filename));
1106 talloc_free(tmp_ctx);
1110 if (chmod(filename, st.st_mode & ~(S_IXUSR|S_IXGRP|S_IXOTH)) == -1) {
1111 DEBUG(DEBUG_ERR,("Could not chmod %s. Failed to disable script.\n", filename));
1112 talloc_free(tmp_ctx);
1116 talloc_free(tmp_ctx);