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/tevent/tevent.h"
28 #include "../common/rb_tree.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)
37 /* all the child processes will be running in the same process group */
38 kill(-getpgrp(), SIGKILL);
42 /* This is attached to the event script state. */
43 struct event_script_callback {
44 struct ctdb_event_script_state *state;
46 /* Warning: this can free us! */
47 void (*fn)(struct ctdb_context *, int, void *);
52 struct ctdb_event_script_state {
53 struct ctdb_context *ctdb;
54 struct event_script_callback *callback;
58 enum ctdb_eventscript_call call;
60 struct timeval timeout;
63 struct ctdb_scripts_wire *scripts;
66 static struct ctdb_script_wire *get_current_script(struct ctdb_event_script_state *state)
68 return &state->scripts->scripts[state->current];
71 /* called from ctdb_logging when we have received output on STDERR from
72 * one of the eventscripts
74 static void log_event_script_output(const char *str, uint16_t len, void *p)
76 struct ctdb_event_script_state *state
77 = talloc_get_type(p, struct ctdb_event_script_state);
78 struct ctdb_script_wire *current;
79 unsigned int slen, min;
81 /* We may have been aborted to run something else. Discard */
82 if (state->scripts == NULL) {
86 current = get_current_script(state);
88 /* Append, but don't overfill buffer. It starts zero-filled. */
89 slen = strlen(current->output);
90 min = MIN(len, sizeof(current->output) - slen - 1);
92 memcpy(current->output + slen, str, min);
95 int32_t ctdb_control_get_event_script_status(struct ctdb_context *ctdb,
99 if (call_type >= CTDB_EVENT_MAX) {
103 if (ctdb->last_status[call_type] == NULL) {
104 /* If it's never been run, return nothing so they can tell. */
107 outdata->dsize = talloc_get_size(ctdb->last_status[call_type]);
108 outdata->dptr = (uint8_t *)ctdb->last_status[call_type];
113 struct ctdb_script_tree_item {
118 /* Return true if OK, otherwise set errno. */
119 static bool check_executable(const char *dir, const char *name)
124 full = talloc_asprintf(NULL, "%s/%s", dir, name);
128 if (stat(full, &st) != 0) {
129 DEBUG(DEBUG_ERR,("Could not stat event script %s: %s\n",
130 full, strerror(errno)));
135 if (!(st.st_mode & S_IXUSR)) {
136 DEBUG(DEBUG_DEBUG,("Event script %s is not executable. Ignoring this event script\n", full));
146 static struct ctdb_scripts_wire *ctdb_get_script_list(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx)
152 struct ctdb_scripts_wire *scripts;
153 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
154 struct ctdb_script_tree_item *tree_item;
158 the service specific event scripts
160 if (stat(ctdb->event_script_dir, &st) != 0 &&
162 DEBUG(DEBUG_CRIT,("No event script directory found at '%s'\n", ctdb->event_script_dir));
163 talloc_free(tmp_ctx);
167 /* create a tree to store all the script names in */
168 tree = trbt_create(tmp_ctx, 0);
170 /* scan all directory entries and insert all valid scripts into the
173 dir = opendir(ctdb->event_script_dir);
175 DEBUG(DEBUG_CRIT,("Failed to open event script directory '%s'\n", ctdb->event_script_dir));
176 talloc_free(tmp_ctx);
181 while ((de=readdir(dir)) != NULL) {
185 namlen = strlen(de->d_name);
191 if (de->d_name[namlen-1] == '~') {
192 /* skip files emacs left behind */
196 if (de->d_name[2] != '.') {
200 if (sscanf(de->d_name, "%02u.", &num) != 1) {
204 if (strlen(de->d_name) > MAX_SCRIPT_NAME) {
205 DEBUG(DEBUG_ERR,("Script name %s too long! %u chars max",
206 de->d_name, MAX_SCRIPT_NAME));
210 tree_item = talloc(tree, struct ctdb_script_tree_item);
211 if (tree_item == NULL) {
212 DEBUG(DEBUG_ERR, (__location__ " Failed to allocate new tree item\n"));
213 talloc_free(tmp_ctx);
217 tree_item->error = 0;
218 if (!check_executable(ctdb->event_script_dir, de->d_name)) {
219 tree_item->error = errno;
222 tree_item->name = talloc_strdup(tree_item, de->d_name);
223 if (tree_item->name == NULL) {
224 DEBUG(DEBUG_ERR,(__location__ " Failed to allocate script name.\n"));
225 talloc_free(tmp_ctx);
229 /* store the event script in the tree */
230 trbt_insert32(tree, (num<<16)|count++, tree_item);
234 /* Overallocates by one, but that's OK */
235 scripts = talloc_zero_size(tmp_ctx,
237 + sizeof(scripts->scripts[0]) * count);
238 if (scripts == NULL) {
239 DEBUG(DEBUG_ERR, (__location__ " Failed to allocate scripts\n"));
240 talloc_free(tmp_ctx);
243 scripts->num_scripts = count;
245 for (count = 0; count < scripts->num_scripts; count++) {
246 tree_item = trbt_findfirstarray32(tree, 1);
248 strcpy(scripts->scripts[count].name, tree_item->name);
249 scripts->scripts[count].status = -tree_item->error;
251 /* remove this script from the tree */
252 talloc_free(tree_item);
255 talloc_steal(mem_ctx, scripts);
256 talloc_free(tmp_ctx);
260 static int child_setup(struct ctdb_context *ctdb)
262 if (setpgid(0,0) != 0) {
264 DEBUG(DEBUG_ERR,("Failed to create process group for event scripts - %s\n",
269 signal(SIGTERM, sigterm);
273 static char *child_command_string(struct ctdb_context *ctdb,
276 const char *scriptname,
277 enum ctdb_eventscript_call call,
280 const char *str = from_user ? "CTDB_CALLED_BY_USER=1 " : "";
282 /* Allow a setting where we run the actual monitor event
283 from an external source and replace it with
284 a "status" event that just picks up the actual
285 status of the event asynchronously.
287 if ((ctdb->tunable.use_status_events_for_monitoring != 0)
288 && (call == CTDB_EVENT_MONITOR)
290 return talloc_asprintf(ctx, "%s%s/%s %s",
292 ctdb->event_script_dir,
293 scriptname, "status");
295 return talloc_asprintf(ctx, "%s%s/%s %s %s",
297 ctdb->event_script_dir,
299 ctdb_eventscript_call_names[call],
304 static int child_run_one(struct ctdb_context *ctdb,
305 const char *scriptname, const char *cmdstr)
309 ret = system(cmdstr);
310 /* if the system() call was successful, translate ret into the
311 return code from the command
314 ret = WEXITSTATUS(ret);
319 /* 127 could mean it does not exist, 126 non-executable. */
320 if (ret == 127 || ret == 126) {
322 if (!check_executable(ctdb->event_script_dir, scriptname)) {
323 DEBUG(DEBUG_ERR,("Script %s returned status %u. Someone just deleted it?\n",
332 Actually run one event script
333 this function is called and run in the context of a forked child
334 which allows it to do blocking calls such as system()
336 static int child_run_script(struct ctdb_context *ctdb,
338 enum ctdb_eventscript_call call,
340 struct ctdb_script_wire *current)
344 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
346 ret = child_setup(ctdb);
350 cmdstr = child_command_string(ctdb, tmp_ctx, from_user,
351 current->name, call, options);
352 CTDB_NO_MEMORY(ctdb, cmdstr);
354 DEBUG(DEBUG_DEBUG,("Executing event script %s\n",cmdstr));
356 if (current->status) {
357 ret = current->status;
361 ret = child_run_one(ctdb, current->name, cmdstr);
363 talloc_free(tmp_ctx);
367 static void ctdb_event_script_handler(struct event_context *ev, struct fd_event *fde,
368 uint16_t flags, void *p);
370 static int fork_child_for_script(struct ctdb_context *ctdb,
371 struct ctdb_event_script_state *state)
374 struct tevent_fd *fde;
375 struct ctdb_script_wire *current = get_current_script(state);
377 current->start = timeval_current();
381 DEBUG(DEBUG_ERR, (__location__ " pipe failed for child eventscript process\n"));
385 if (!ctdb_fork_with_logging(state, ctdb, current->name, log_event_script_output,
386 state, &state->child)) {
393 /* If we are the child, do the work. */
394 if (state->child == 0) {
397 debug_extra = talloc_asprintf(NULL, "eventscript-%s-%s:",
399 ctdb_eventscript_call_names[state->call]);
401 set_close_on_exec(state->fd[1]);
403 rt = child_run_script(ctdb, state->from_user, state->call, state->options, current);
404 /* We must be able to write PIPEBUF bytes at least; if this
405 somehow fails, the read above will be short. */
406 write(state->fd[1], &rt, sizeof(rt));
412 set_close_on_exec(state->fd[0]);
414 DEBUG(DEBUG_DEBUG, (__location__ " Created PIPE FD:%d to child eventscript process\n", state->fd[0]));
416 /* Set ourselves up to be called when that's done. */
417 fde = event_add_fd(ctdb->ev, state, state->fd[0], EVENT_FD_READ,
418 ctdb_event_script_handler, state);
419 tevent_fd_set_auto_close(fde);
425 Summarize status of this run of scripts.
427 static int script_status(struct ctdb_scripts_wire *scripts)
431 for (i = 0; i < scripts->num_scripts; i++) {
432 switch (scripts->scripts[i].status) {
435 /* Disabled or missing; that's OK. */
441 return scripts->scripts[i].status;
449 /* called when child is finished */
450 static void ctdb_event_script_handler(struct event_context *ev, struct fd_event *fde,
451 uint16_t flags, void *p)
453 struct ctdb_event_script_state *state =
454 talloc_get_type(p, struct ctdb_event_script_state);
455 struct ctdb_script_wire *current = get_current_script(state);
456 struct ctdb_context *ctdb = state->ctdb;
460 DEBUG(DEBUG_ERR,("Eventscript finished but ctdb is NULL\n"));
464 r = read(state->fd[0], ¤t->status, sizeof(current->status));
466 current->status = -errno;
467 } else if (r != sizeof(current->status)) {
468 current->status = -EIO;
471 current->finished = timeval_current();
472 /* valgrind gets overloaded if we run next script as it's still doing
473 * post-execution analysis, so kill finished child here. */
474 if (ctdb->valgrinding) {
475 kill(state->child, SIGKILL);
480 status = script_status(state->scripts);
482 /* Aborted or finished all scripts? We're done. */
483 if (status != 0 || state->current+1 == state->scripts->num_scripts) {
484 DEBUG(DEBUG_INFO,(__location__ " Eventscript %s %s finished with state %d\n",
485 ctdb_eventscript_call_names[state->call], state->options, status));
487 ctdb->event_script_timeouts = 0;
492 /* Forget about that old fd. */
498 current->status = fork_child_for_script(ctdb, state);
499 if (current->status != 0) {
500 /* This calls the callback. */
505 static void debug_timeout(struct ctdb_event_script_state *state)
507 struct ctdb_script_wire *current = get_current_script(state);
511 char tbuf[100], buf[200];
513 cmd = child_command_string(state->ctdb, state,
514 state->from_user, current->name,
515 state->call, state->options);
516 CTDB_NO_MEMORY_VOID(state->ctdb, cmd);
518 DEBUG(DEBUG_ERR,("Timed out running script '%s' after %.1f seconds pid :%d\n",
519 cmd, timeval_elapsed(¤t->start), state->child));
523 strftime(tbuf, sizeof(tbuf)-1, "%Y%m%d%H%M%S", localtime(&t));
524 sprintf(buf, "{ pstree -p; cat /proc/locks; ls -li /var/ctdb/ /var/ctdb/persistent; }"
525 " >/tmp/ctdb.event.%s.%d", tbuf, getpid());
527 pid = ctdb_fork(state->ctdb);
530 /* Now we can kill the child */
531 kill(state->child, SIGTERM);
535 DEBUG(DEBUG_ERR,("Fork for debug script failed : %s\n",
538 DEBUG(DEBUG_ERR,("Logged timedout eventscript : %s\n", buf));
539 /* Don't kill child until timeout done. */
544 /* called when child times out */
545 static void ctdb_event_script_timeout(struct event_context *ev, struct timed_event *te,
546 struct timeval t, void *p)
548 struct ctdb_event_script_state *state = talloc_get_type(p, struct ctdb_event_script_state);
549 struct ctdb_context *ctdb = state->ctdb;
550 struct ctdb_script_wire *current = get_current_script(state);
552 DEBUG(DEBUG_ERR,("Event script timed out : %s %s %s count : %u pid : %d\n",
553 current->name, ctdb_eventscript_call_names[state->call], state->options, ctdb->event_script_timeouts, state->child));
555 /* ignore timeouts for these events */
556 switch (state->call) {
557 case CTDB_EVENT_START_RECOVERY:
558 case CTDB_EVENT_RECOVERED:
559 case CTDB_EVENT_TAKE_IP:
560 case CTDB_EVENT_RELEASE_IP:
561 case CTDB_EVENT_STOPPED:
562 case CTDB_EVENT_MONITOR:
563 case CTDB_EVENT_STATUS:
564 state->scripts->scripts[state->current].status = 0;
565 DEBUG(DEBUG_ERR,("Ignoring hung script for %s call %d\n", state->options, state->call));
568 state->scripts->scripts[state->current].status = -ETIME;
569 debug_timeout(state);
576 destroy an event script: kill it if ->child != 0.
578 static int event_script_destructor(struct ctdb_event_script_state *state)
581 struct event_script_callback *callback;
584 DEBUG(DEBUG_ERR,(__location__ " Sending SIGTERM to child pid:%d\n", state->child));
586 if (kill(state->child, SIGTERM) != 0) {
587 DEBUG(DEBUG_ERR,("Failed to kill child process for eventscript, errno %s(%d)\n", strerror(errno), errno));
591 /* If we were the current monitor, we no longer are. */
592 if (state->ctdb->current_monitor == state) {
593 state->ctdb->current_monitor = NULL;
596 /* Save our scripts as the last executed status, if we have them.
597 * See ctdb_event_script_callback_v where we abort monitor event. */
598 if (state->scripts) {
599 talloc_free(state->ctdb->last_status[state->call]);
600 state->ctdb->last_status[state->call] = state->scripts;
601 if (state->current < state->ctdb->last_status[state->call]->num_scripts) {
602 state->ctdb->last_status[state->call]->num_scripts = state->current+1;
606 /* Use last status as result, or "OK" if none. */
607 if (state->ctdb->last_status[state->call]) {
608 status = script_status(state->ctdb->last_status[state->call]);
613 /* This is allowed to free us; talloc will prevent double free anyway,
614 * but beware if you call this outside the destructor! */
615 callback = state->callback;
618 /* Make sure destructor doesn't free itself! */
619 talloc_steal(NULL, callback);
620 callback->fn(state->ctdb, status, callback->private_data);
621 talloc_free(callback);
627 static unsigned int count_words(const char *options)
629 unsigned int words = 0;
631 options += strspn(options, " \t");
634 options += strcspn(options, " \t");
635 options += strspn(options, " \t");
640 static bool check_options(enum ctdb_eventscript_call call, const char *options)
643 /* These all take no arguments. */
644 case CTDB_EVENT_INIT:
645 case CTDB_EVENT_SETUP:
646 case CTDB_EVENT_STARTUP:
647 case CTDB_EVENT_START_RECOVERY:
648 case CTDB_EVENT_RECOVERED:
649 case CTDB_EVENT_STOPPED:
650 case CTDB_EVENT_MONITOR:
651 case CTDB_EVENT_STATUS:
652 case CTDB_EVENT_SHUTDOWN:
653 case CTDB_EVENT_RELOAD:
654 case CTDB_EVENT_IPREALLOCATED:
655 return count_words(options) == 0;
657 case CTDB_EVENT_TAKE_IP: /* interface, IP address, netmask bits. */
658 case CTDB_EVENT_RELEASE_IP:
659 return count_words(options) == 3;
661 case CTDB_EVENT_UPDATE_IP: /* old interface, new interface, IP address, netmask bits. */
662 return count_words(options) == 4;
665 DEBUG(DEBUG_ERR,(__location__ "Unknown ctdb_eventscript_call %u\n", call));
670 static int remove_callback(struct event_script_callback *callback)
672 /* Detach ourselves from the running script state */
673 callback->state->callback = NULL;
678 run the event script in the background, calling the callback when
681 static int ctdb_event_script_callback_v(struct ctdb_context *ctdb,
683 void (*callback)(struct ctdb_context *, int, void *),
686 enum ctdb_eventscript_call call,
687 const char *fmt, va_list ap)
689 struct ctdb_event_script_state *state;
691 state = talloc(ctdb->event_script_ctx, struct ctdb_event_script_state);
692 CTDB_NO_MEMORY(ctdb, state);
694 /* The callback isn't done if the context is freed. */
695 state->callback = talloc(mem_ctx, struct event_script_callback);
696 CTDB_NO_MEMORY(ctdb, state->callback);
697 talloc_set_destructor(state->callback, remove_callback);
698 state->callback->state = state;
699 state->callback->fn = callback;
700 state->callback->private_data = private_data;
703 state->from_user = from_user;
705 state->options = talloc_vasprintf(state, fmt, ap);
706 state->timeout = timeval_set(ctdb->tunable.script_timeout, 0);
707 state->scripts = NULL;
708 if (state->options == NULL) {
709 DEBUG(DEBUG_ERR, (__location__ " could not allocate state->options\n"));
713 if (!check_options(state->call, state->options)) {
714 DEBUG(DEBUG_ERR, ("Bad eventscript options '%s' for %s\n",
715 ctdb_eventscript_call_names[state->call], state->options));
720 if (ctdb->recovery_mode != CTDB_RECOVERY_NORMAL) {
721 /* we guarantee that only some specifically allowed event scripts are run
723 const enum ctdb_eventscript_call allowed_calls[] = {
726 CTDB_EVENT_START_RECOVERY,
728 CTDB_EVENT_RELEASE_IP,
732 for (i=0;i<ARRAY_SIZE(allowed_calls);i++) {
733 if (call == allowed_calls[i]) break;
735 if (i == ARRAY_SIZE(allowed_calls)) {
736 DEBUG(DEBUG_ERR,("Refusing to run event scripts call '%s' while in recovery\n",
737 ctdb_eventscript_call_names[call]));
743 /* Kill off any running monitor events to run this event. */
744 if (ctdb->current_monitor) {
745 /* Discard script status so we don't save to last_status */
746 talloc_free(ctdb->current_monitor->scripts);
747 ctdb->current_monitor->scripts = NULL;
748 talloc_free(ctdb->current_monitor);
749 ctdb->current_monitor = NULL;
752 DEBUG(DEBUG_INFO,(__location__ " Starting eventscript %s %s\n",
753 ctdb_eventscript_call_names[state->call],
756 /* This is not a child of state, since we save it in destructor. */
757 state->scripts = ctdb_get_script_list(ctdb, ctdb);
758 if (state->scripts == NULL) {
765 if (!from_user && (call == CTDB_EVENT_MONITOR || call == CTDB_EVENT_STATUS)) {
766 ctdb->current_monitor = state;
769 talloc_set_destructor(state, event_script_destructor);
772 if (state->scripts->num_scripts == 0) {
777 state->scripts->scripts[0].status = fork_child_for_script(ctdb, state);
778 if (state->scripts->scripts[0].status != 0) {
779 /* Callback is called from destructor, with fail result. */
784 if (!timeval_is_zero(&state->timeout)) {
785 event_add_timed(ctdb->ev, state, timeval_current_ofs(state->timeout.tv_sec, state->timeout.tv_usec), ctdb_event_script_timeout, state);
787 DEBUG(DEBUG_ERR, (__location__ " eventscript %s %s called with no timeout\n",
788 ctdb_eventscript_call_names[state->call],
797 run the event script in the background, calling the callback when
798 finished. If mem_ctx is freed, callback will never be called.
800 int ctdb_event_script_callback(struct ctdb_context *ctdb,
802 void (*callback)(struct ctdb_context *, int, void *),
805 enum ctdb_eventscript_call call,
806 const char *fmt, ...)
812 ret = ctdb_event_script_callback_v(ctdb, mem_ctx, callback, private_data, from_user, call, fmt, ap);
819 struct callback_status {
825 called when ctdb_event_script() finishes
827 static void event_script_callback(struct ctdb_context *ctdb, int status, void *private_data)
829 struct callback_status *s = (struct callback_status *)private_data;
835 run the event script, waiting for it to complete. Used when the caller
836 doesn't want to continue till the event script has finished.
838 int ctdb_event_script_args(struct ctdb_context *ctdb, enum ctdb_eventscript_call call,
839 const char *fmt, ...)
843 struct callback_status status;
846 ret = ctdb_event_script_callback_v(ctdb, ctdb,
847 event_script_callback, &status, false, call, fmt, ap);
856 while (status.done == false && event_loop_once(ctdb->ev) == 0) /* noop */;
858 if (status.status == -ETIME) {
859 DEBUG(DEBUG_ERR, (__location__ " eventscript for '%s' timedout."
860 " Immediately banning ourself for %d seconds\n",
861 ctdb_eventscript_call_names[call],
862 ctdb->tunable.recovery_ban_period));
866 return status.status;
869 int ctdb_event_script(struct ctdb_context *ctdb, enum ctdb_eventscript_call call)
871 /* GCC complains about empty format string, so use %s and "". */
872 return ctdb_event_script_args(ctdb, call, "%s", "");
875 struct eventscript_callback_state {
876 struct ctdb_req_control *c;
880 called when a forced eventscript run has finished
882 static void run_eventscripts_callback(struct ctdb_context *ctdb, int status,
885 struct eventscript_callback_state *state =
886 talloc_get_type(private_data, struct eventscript_callback_state);
888 ctdb_enable_monitoring(ctdb);
891 DEBUG(DEBUG_ERR,(__location__ " Failed to forcibly run eventscripts\n"));
894 ctdb_request_control_reply(ctdb, state->c, NULL, status, NULL);
895 /* This will free the struct ctdb_event_script_state we are in! */
901 /* Returns rest of string, or NULL if no match. */
902 static const char *get_call(const char *p, enum ctdb_eventscript_call *call)
906 /* Skip any initial whitespace. */
907 p += strspn(p, " \t");
909 /* See if we match any. */
910 for (*call = 0; *call < CTDB_EVENT_MAX; (*call)++) {
911 len = strlen(ctdb_eventscript_call_names[*call]);
912 if (strncmp(p, ctdb_eventscript_call_names[*call], len) == 0) {
913 /* If end of string or whitespace, we're done. */
914 if (strcspn(p + len, " \t") == 0) {
923 A control to force running of the eventscripts from the ctdb client tool
925 int32_t ctdb_run_eventscripts(struct ctdb_context *ctdb,
926 struct ctdb_req_control *c,
927 TDB_DATA indata, bool *async_reply)
930 struct eventscript_callback_state *state;
932 enum ctdb_eventscript_call call;
934 /* Figure out what call they want. */
935 options = get_call((const char *)indata.dptr, &call);
937 DEBUG(DEBUG_ERR, (__location__ " Invalid forced \"%s\"\n", (const char *)indata.dptr));
941 if (ctdb->recovery_mode != CTDB_RECOVERY_NORMAL) {
942 DEBUG(DEBUG_ERR, (__location__ " Aborted running eventscript \"%s\" while in RECOVERY mode\n", indata.dptr));
946 state = talloc(ctdb->event_script_ctx, struct eventscript_callback_state);
947 CTDB_NO_MEMORY(ctdb, state);
949 state->c = talloc_steal(state, c);
951 DEBUG(DEBUG_NOTICE,("Forced running of eventscripts with arguments %s\n", indata.dptr));
953 ctdb_disable_monitoring(ctdb);
955 ret = ctdb_event_script_callback(ctdb,
956 state, run_eventscripts_callback, state,
957 true, call, "%s", options);
960 ctdb_enable_monitoring(ctdb);
961 DEBUG(DEBUG_ERR,(__location__ " Failed to run eventscripts with arguments %s\n", indata.dptr));
966 /* tell ctdb_control.c that we will be replying asynchronously */
974 int32_t ctdb_control_enable_script(struct ctdb_context *ctdb, TDB_DATA indata)
979 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
981 script = (char *)indata.dptr;
982 if (indata.dsize == 0) {
983 DEBUG(DEBUG_ERR,(__location__ " No script specified.\n"));
984 talloc_free(tmp_ctx);
987 if (indata.dptr[indata.dsize - 1] != '\0') {
988 DEBUG(DEBUG_ERR,(__location__ " String is not null terminated.\n"));
989 talloc_free(tmp_ctx);
992 if (index(script,'/') != NULL) {
993 DEBUG(DEBUG_ERR,(__location__ " Script name contains '/'. Failed to enable script %s\n", script));
994 talloc_free(tmp_ctx);
999 if (stat(ctdb->event_script_dir, &st) != 0 &&
1001 DEBUG(DEBUG_CRIT,("No event script directory found at '%s'\n", ctdb->event_script_dir));
1002 talloc_free(tmp_ctx);
1007 filename = talloc_asprintf(tmp_ctx, "%s/%s", ctdb->event_script_dir, script);
1008 if (filename == NULL) {
1009 DEBUG(DEBUG_ERR,(__location__ " Failed to create script path\n"));
1010 talloc_free(tmp_ctx);
1014 if (stat(filename, &st) != 0) {
1015 DEBUG(DEBUG_ERR,("Could not stat event script %s. Failed to enable script.\n", filename));
1016 talloc_free(tmp_ctx);
1020 if (chmod(filename, st.st_mode | S_IXUSR) == -1) {
1021 DEBUG(DEBUG_ERR,("Could not chmod %s. Failed to enable script.\n", filename));
1022 talloc_free(tmp_ctx);
1026 talloc_free(tmp_ctx);
1030 int32_t ctdb_control_disable_script(struct ctdb_context *ctdb, TDB_DATA indata)
1035 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1037 script = (char *)indata.dptr;
1038 if (indata.dsize == 0) {
1039 DEBUG(DEBUG_ERR,(__location__ " No script specified.\n"));
1040 talloc_free(tmp_ctx);
1043 if (indata.dptr[indata.dsize - 1] != '\0') {
1044 DEBUG(DEBUG_ERR,(__location__ " String is not null terminated.\n"));
1045 talloc_free(tmp_ctx);
1048 if (index(script,'/') != NULL) {
1049 DEBUG(DEBUG_ERR,(__location__ " Script name contains '/'. Failed to disable script %s\n", script));
1050 talloc_free(tmp_ctx);
1055 if (stat(ctdb->event_script_dir, &st) != 0 &&
1057 DEBUG(DEBUG_CRIT,("No event script directory found at '%s'\n", ctdb->event_script_dir));
1058 talloc_free(tmp_ctx);
1063 filename = talloc_asprintf(tmp_ctx, "%s/%s", ctdb->event_script_dir, script);
1064 if (filename == NULL) {
1065 DEBUG(DEBUG_ERR,(__location__ " Failed to create script path\n"));
1066 talloc_free(tmp_ctx);
1070 if (stat(filename, &st) != 0) {
1071 DEBUG(DEBUG_ERR,("Could not stat event script %s. Failed to disable script.\n", filename));
1072 talloc_free(tmp_ctx);
1076 if (chmod(filename, st.st_mode & ~(S_IXUSR|S_IXGRP|S_IXOTH)) == -1) {
1077 DEBUG(DEBUG_ERR,("Could not chmod %s. Failed to disable script.\n", filename));
1078 talloc_free(tmp_ctx);
1082 talloc_free(tmp_ctx);