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 void ctdb_event_script_timeout(struct event_context *ev, struct timed_event *te, struct timeval t, void *p);
38 ctdbd sends us a SIGTERM when we should time out the current script
40 static void sigterm(int sig)
42 char tbuf[100], buf[200];
45 DEBUG(DEBUG_ERR,("Timed out running script '%s' after %.1f seconds pid :%d\n",
46 child_state.script_running, timeval_elapsed(&child_state.start), getpid()));
50 strftime(tbuf, sizeof(tbuf)-1, "%Y%m%d%H%M%S", localtime(&t));
51 sprintf(buf, "{ pstree -p; cat /proc/locks; ls -li /var/ctdb/ /var/ctdb/persistent; }"
52 " >/tmp/ctdb.event.%s.%d", tbuf, getpid());
55 DEBUG(DEBUG_ERR,("Logged timedout eventscript : %s\n", buf));
57 /* all the child processes will be running in the same process group */
58 kill(-getpgrp(), SIGKILL);
62 struct ctdb_event_script_state {
63 struct ctdb_context *ctdb;
65 /* Warning: this can free us! */
66 void (*callback)(struct ctdb_context *, int, void *);
70 enum ctdb_eventscript_call call;
72 struct timeval timeout;
75 struct ctdb_scripts_wire *scripts;
78 static struct ctdb_script_wire *get_current_script(struct ctdb_event_script_state *state)
80 return &state->scripts->scripts[state->current];
83 /* called from ctdb_logging when we have received output on STDERR from
84 * one of the eventscripts
86 static void log_event_script_output(const char *str, uint16_t len, void *p)
88 struct ctdb_event_script_state *state
89 = talloc_get_type(p, struct ctdb_event_script_state);
90 struct ctdb_script_wire *current;
91 unsigned int slen, min;
93 /* We may have been aborted to run something else. Discard */
94 if (state->scripts == NULL) {
98 current = get_current_script(state);
100 /* Append, but don't overfill buffer. It starts zero-filled. */
101 slen = strlen(current->output);
102 min = MIN(len, sizeof(current->output) - slen - 1);
104 memcpy(current->output + slen, str, min);
107 int32_t ctdb_control_get_event_script_status(struct ctdb_context *ctdb,
111 if (call_type >= CTDB_EVENT_MAX) {
115 if (ctdb->last_status[call_type] == NULL) {
116 /* If it's never been run, return nothing so they can tell. */
119 outdata->dsize = talloc_get_size(ctdb->last_status[call_type]);
120 outdata->dptr = (uint8_t *)ctdb->last_status[call_type];
125 struct ctdb_script_tree_item {
130 /* Return true if OK, otherwise set errno. */
131 static bool check_executable(const char *dir, const char *name)
136 full = talloc_asprintf(NULL, "%s/%s", dir, name);
140 if (stat(full, &st) != 0) {
141 DEBUG(DEBUG_ERR,("Could not stat event script %s: %s\n",
142 full, strerror(errno)));
147 if (!(st.st_mode & S_IXUSR)) {
148 DEBUG(DEBUG_INFO,("Event script %s is not executable. Ignoring this event script\n", full));
158 static struct ctdb_scripts_wire *ctdb_get_script_list(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx)
164 struct ctdb_scripts_wire *scripts;
165 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
166 struct ctdb_script_tree_item *tree_item;
170 the service specific event scripts
172 if (stat(ctdb->event_script_dir, &st) != 0 &&
174 DEBUG(DEBUG_CRIT,("No event script directory found at '%s'\n", ctdb->event_script_dir));
175 talloc_free(tmp_ctx);
179 /* create a tree to store all the script names in */
180 tree = trbt_create(tmp_ctx, 0);
182 /* scan all directory entries and insert all valid scripts into the
185 dir = opendir(ctdb->event_script_dir);
187 DEBUG(DEBUG_CRIT,("Failed to open event script directory '%s'\n", ctdb->event_script_dir));
188 talloc_free(tmp_ctx);
193 while ((de=readdir(dir)) != NULL) {
197 namlen = strlen(de->d_name);
203 if (de->d_name[namlen-1] == '~') {
204 /* skip files emacs left behind */
208 if (de->d_name[2] != '.') {
212 if (sscanf(de->d_name, "%02u.", &num) != 1) {
216 if (strlen(de->d_name) > MAX_SCRIPT_NAME) {
217 DEBUG(DEBUG_ERR,("Script name %s too long! %u chars max",
218 de->d_name, MAX_SCRIPT_NAME));
222 tree_item = talloc(tree, struct ctdb_script_tree_item);
223 if (tree_item == NULL) {
224 DEBUG(DEBUG_ERR, (__location__ " Failed to allocate new tree item\n"));
225 talloc_free(tmp_ctx);
229 tree_item->error = 0;
230 if (!check_executable(ctdb->event_script_dir, de->d_name)) {
231 tree_item->error = errno;
234 tree_item->name = talloc_strdup(tree_item, de->d_name);
235 if (tree_item->name == NULL) {
236 DEBUG(DEBUG_ERR,(__location__ " Failed to allocate script name.\n"));
237 talloc_free(tmp_ctx);
241 /* store the event script in the tree */
242 trbt_insert32(tree, (num<<16)|count++, tree_item);
246 /* Overallocates by one, but that's OK */
247 scripts = talloc_zero_size(tmp_ctx,
249 + sizeof(scripts->scripts[0]) * count);
250 if (scripts == NULL) {
251 DEBUG(DEBUG_ERR, (__location__ " Failed to allocate scripts\n"));
252 talloc_free(tmp_ctx);
255 scripts->num_scripts = count;
257 for (count = 0; count < scripts->num_scripts; count++) {
258 tree_item = trbt_findfirstarray32(tree, 1);
260 strcpy(scripts->scripts[count].name, tree_item->name);
261 scripts->scripts[count].status = -tree_item->error;
263 /* remove this script from the tree */
264 talloc_free(tree_item);
267 talloc_steal(mem_ctx, scripts);
268 talloc_free(tmp_ctx);
272 static int child_setup(struct ctdb_context *ctdb)
274 if (setpgid(0,0) != 0) {
276 DEBUG(DEBUG_ERR,("Failed to create process group for event scripts - %s\n",
281 signal(SIGTERM, sigterm);
285 static char *child_command_string(struct ctdb_context *ctdb,
288 const char *scriptname,
289 enum ctdb_eventscript_call call,
292 const char *str = from_user ? "CTDB_CALLED_BY_USER=1 " : "";
294 /* Allow a setting where we run the actual monitor event
295 from an external source and replace it with
296 a "status" event that just picks up the actual
297 status of the event asynchronously.
299 if ((ctdb->tunable.use_status_events_for_monitoring != 0)
300 && (call == CTDB_EVENT_MONITOR)
302 return talloc_asprintf(ctx, "%s%s/%s %s",
304 ctdb->event_script_dir,
305 scriptname, "status");
307 return talloc_asprintf(ctx, "%s%s/%s %s %s",
309 ctdb->event_script_dir,
311 ctdb_eventscript_call_names[call],
316 static int child_run_one(struct ctdb_context *ctdb,
317 const char *scriptname, const char *cmdstr)
321 ret = system(cmdstr);
322 /* if the system() call was successful, translate ret into the
323 return code from the command
326 ret = WEXITSTATUS(ret);
331 /* 127 could mean it does not exist, 126 non-executable. */
332 if (ret == 127 || ret == 126) {
334 if (!check_executable(ctdb->event_script_dir, scriptname)) {
335 DEBUG(DEBUG_ERR,("Script %s returned status %u. Someone just deleted it?\n",
344 Actually run one event script
345 this function is called and run in the context of a forked child
346 which allows it to do blocking calls such as system()
348 static int child_run_script(struct ctdb_context *ctdb,
350 enum ctdb_eventscript_call call,
352 struct ctdb_script_wire *current)
356 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
358 child_state.start = timeval_current();
359 ret = child_setup(ctdb);
363 cmdstr = child_command_string(ctdb, tmp_ctx, from_user,
364 current->name, call, options);
365 CTDB_NO_MEMORY(ctdb, cmdstr);
366 child_state.script_running = cmdstr;
368 DEBUG(DEBUG_INFO,("Executing event script %s\n",cmdstr));
370 if (current->status) {
371 ret = current->status;
375 ret = child_run_one(ctdb, current->name, cmdstr);
377 talloc_free(tmp_ctx);
381 static void ctdb_event_script_handler(struct event_context *ev, struct fd_event *fde,
382 uint16_t flags, void *p);
384 static int fork_child_for_script(struct ctdb_context *ctdb,
385 struct ctdb_event_script_state *state)
388 struct ctdb_script_wire *current = get_current_script(state);
390 current->start = timeval_current();
394 DEBUG(DEBUG_ERR, (__location__ " pipe failed for child eventscript process\n"));
398 if (!ctdb_fork_with_logging(state, ctdb, log_event_script_output,
399 state, &state->child)) {
406 /* If we are the child, do the work. */
407 if (state->child == 0) {
411 set_close_on_exec(state->fd[1]);
413 rt = child_run_script(ctdb, state->from_user, state->call, state->options, current);
414 /* We must be able to write PIPEBUF bytes at least; if this
415 somehow fails, the read above will be short. */
416 write(state->fd[1], &rt, sizeof(rt));
422 set_close_on_exec(state->fd[0]);
424 DEBUG(DEBUG_DEBUG, (__location__ " Created PIPE FD:%d to child eventscript process\n", state->fd[0]));
426 /* Set ourselves up to be called when that's done. */
427 event_add_fd(ctdb->ev, state, state->fd[0], EVENT_FD_READ|EVENT_FD_AUTOCLOSE,
428 ctdb_event_script_handler, state);
433 Summarize status of this run of scripts.
435 static int script_status(struct ctdb_scripts_wire *scripts)
439 for (i = 0; i < scripts->num_scripts; i++) {
440 switch (scripts->scripts[i].status) {
443 /* Disabled or missing; that's OK. */
449 return scripts->scripts[i].status;
457 /* called when child is finished */
458 static void ctdb_event_script_handler(struct event_context *ev, struct fd_event *fde,
459 uint16_t flags, void *p)
461 struct ctdb_event_script_state *state =
462 talloc_get_type(p, struct ctdb_event_script_state);
463 struct ctdb_script_wire *current = get_current_script(state);
464 struct ctdb_context *ctdb = state->ctdb;
467 r = read(state->fd[0], ¤t->status, sizeof(current->status));
469 current->status = -errno;
470 } else if (r != sizeof(current->status)) {
471 current->status = -EIO;
474 current->finished = timeval_current();
475 /* valgrind gets overloaded if we run next script as it's still doing
476 * post-execution analysis, so kill finished child here. */
477 if (ctdb->valgrinding) {
478 kill(state->child, SIGKILL);
483 status = script_status(state->scripts);
485 /* Aborted or finished all scripts? We're done. */
486 if (status != 0 || state->current+1 == state->scripts->num_scripts) {
487 DEBUG(DEBUG_INFO,(__location__ " Eventscript %s %s finished with state %d\n",
488 ctdb_eventscript_call_names[state->call], state->options, status));
490 ctdb->event_script_timeouts = 0;
495 /* Forget about that old fd. */
501 current->status = fork_child_for_script(ctdb, state);
502 if (current->status != 0) {
503 /* This calls the callback. */
508 /* called when child times out */
509 static void ctdb_event_script_timeout(struct event_context *ev, struct timed_event *te,
510 struct timeval t, void *p)
512 struct ctdb_event_script_state *state = talloc_get_type(p, struct ctdb_event_script_state);
513 struct ctdb_context *ctdb = state->ctdb;
514 struct ctdb_script_wire *current = get_current_script(state);
516 DEBUG(DEBUG_ERR,("Event script timed out : %s %s %s count : %u pid : %d\n",
517 current->name, ctdb_eventscript_call_names[state->call], state->options, ctdb->event_script_timeouts, state->child));
519 state->scripts->scripts[state->current].status = -ETIME;
521 if (kill(state->child, 0) != 0) {
522 DEBUG(DEBUG_ERR,("Event script child process already dead, errno %s(%d)\n", strerror(errno), errno));
530 destroy an event script: kill it if ->child != 0.
532 static int event_script_destructor(struct ctdb_event_script_state *state)
537 DEBUG(DEBUG_ERR,(__location__ " Sending SIGTERM to child pid:%d\n", state->child));
539 if (kill(state->child, SIGTERM) != 0) {
540 DEBUG(DEBUG_ERR,("Failed to kill child process for eventscript, errno %s(%d)\n", strerror(errno), errno));
544 /* If we were the current monitor, we no longer are. */
545 if (state->ctdb->current_monitor == state) {
546 state->ctdb->current_monitor = NULL;
549 /* Save our scripts as the last executed status, if we have them.
550 * See ctdb_event_script_callback_v where we abort monitor event. */
551 if (state->scripts) {
552 talloc_free(state->ctdb->last_status[state->call]);
553 state->ctdb->last_status[state->call] = state->scripts;
554 if (state->current < state->ctdb->last_status[state->call]->num_scripts) {
555 state->ctdb->last_status[state->call]->num_scripts = state->current+1;
559 /* Use last status as result, or "OK" if none. */
560 if (state->ctdb->last_status[state->call]) {
561 status = script_status(state->ctdb->last_status[state->call]);
566 /* This is allowed to free us; talloc will prevent double free anyway,
567 * but beware if you call this outside the destructor! */
568 if (state->callback) {
569 state->callback(state->ctdb, status, state->private_data);
575 static unsigned int count_words(const char *options)
577 unsigned int words = 0;
579 options += strspn(options, " \t");
582 options += strcspn(options, " \t");
583 options += strspn(options, " \t");
588 static bool check_options(enum ctdb_eventscript_call call, const char *options)
591 /* These all take no arguments. */
592 case CTDB_EVENT_STARTUP:
593 case CTDB_EVENT_START_RECOVERY:
594 case CTDB_EVENT_RECOVERED:
595 case CTDB_EVENT_STOPPED:
596 case CTDB_EVENT_MONITOR:
597 case CTDB_EVENT_STATUS:
598 case CTDB_EVENT_SHUTDOWN:
599 case CTDB_EVENT_RELOAD:
600 return count_words(options) == 0;
602 case CTDB_EVENT_TAKE_IP: /* interface, IP address, netmask bits. */
603 case CTDB_EVENT_RELEASE_IP:
604 return count_words(options) == 3;
607 DEBUG(DEBUG_ERR,(__location__ "Unknown ctdb_eventscript_call %u\n", call));
613 run the event script in the background, calling the callback when
616 static int ctdb_event_script_callback_v(struct ctdb_context *ctdb,
617 void (*callback)(struct ctdb_context *, int, void *),
620 enum ctdb_eventscript_call call,
621 const char *fmt, va_list ap)
623 struct ctdb_event_script_state *state;
625 state = talloc(ctdb->event_script_ctx, struct ctdb_event_script_state);
626 CTDB_NO_MEMORY(ctdb, state);
629 state->callback = callback;
630 state->private_data = private_data;
631 state->from_user = from_user;
633 state->options = talloc_vasprintf(state, fmt, ap);
634 state->timeout = timeval_set(ctdb->tunable.script_timeout, 0);
635 state->scripts = NULL;
636 if (state->options == NULL) {
637 DEBUG(DEBUG_ERR, (__location__ " could not allocate state->options\n"));
641 if (!check_options(state->call, state->options)) {
642 DEBUG(DEBUG_ERR, ("Bad eventscript options '%s' for %s\n",
643 ctdb_eventscript_call_names[state->call], state->options));
648 if (ctdb->recovery_mode != CTDB_RECOVERY_NORMAL) {
649 /* we guarantee that only some specifically allowed event scripts are run
651 const enum ctdb_eventscript_call allowed_calls[] = {
652 CTDB_EVENT_START_RECOVERY, CTDB_EVENT_SHUTDOWN, CTDB_EVENT_RELEASE_IP, CTDB_EVENT_STOPPED };
654 for (i=0;i<ARRAY_SIZE(allowed_calls);i++) {
655 if (call == allowed_calls[i]) break;
657 if (i == ARRAY_SIZE(allowed_calls)) {
658 DEBUG(DEBUG_ERR,("Refusing to run event scripts call '%s' while in recovery\n",
659 ctdb_eventscript_call_names[call]));
665 /* Kill off any running monitor events to run this event. */
666 if (ctdb->current_monitor) {
667 /* Discard script status so we don't save to last_status */
668 talloc_free(ctdb->current_monitor->scripts);
669 ctdb->current_monitor->scripts = NULL;
670 talloc_free(ctdb->current_monitor);
671 ctdb->current_monitor = NULL;
674 DEBUG(DEBUG_INFO,(__location__ " Starting eventscript %s %s\n",
675 ctdb_eventscript_call_names[state->call],
678 /* This is not a child of state, since we save it in destructor. */
679 state->scripts = ctdb_get_script_list(ctdb, ctdb);
680 if (state->scripts == NULL) {
686 if (!from_user && (call == CTDB_EVENT_MONITOR || call == CTDB_EVENT_STATUS)) {
687 ctdb->current_monitor = state;
690 talloc_set_destructor(state, event_script_destructor);
693 if (state->scripts->num_scripts == 0) {
698 state->scripts->scripts[0].status = fork_child_for_script(ctdb, state);
699 if (state->scripts->scripts[0].status != 0) {
700 /* Callback is called from destructor, with fail result. */
705 if (!timeval_is_zero(&state->timeout)) {
706 event_add_timed(ctdb->ev, state, timeval_current_ofs(state->timeout.tv_sec, state->timeout.tv_usec), ctdb_event_script_timeout, state);
708 DEBUG(DEBUG_ERR, (__location__ " eventscript %s %s called with no timeout\n",
709 ctdb_eventscript_call_names[state->call],
718 run the event script in the background, calling the callback when
721 int ctdb_event_script_callback(struct ctdb_context *ctdb,
723 void (*callback)(struct ctdb_context *, int, void *),
726 enum ctdb_eventscript_call call,
727 const char *fmt, ...)
733 ret = ctdb_event_script_callback_v(ctdb, callback, private_data, from_user, call, fmt, ap);
740 struct callback_status {
746 called when ctdb_event_script() finishes
748 static void event_script_callback(struct ctdb_context *ctdb, int status, void *private_data)
750 struct callback_status *s = (struct callback_status *)private_data;
756 run the event script, waiting for it to complete. Used when the caller
757 doesn't want to continue till the event script has finished.
759 int ctdb_event_script_args(struct ctdb_context *ctdb, enum ctdb_eventscript_call call,
760 const char *fmt, ...)
764 struct callback_status status;
767 ret = ctdb_event_script_callback_v(ctdb,
768 event_script_callback, &status, false, call, fmt, ap);
777 while (status.done == false && event_loop_once(ctdb->ev) == 0) /* noop */;
779 if (status.status == -ETIME) {
780 DEBUG(DEBUG_ERR, (__location__ " eventscript for '%s' timedout."
781 " Immediately banning ourself for %d seconds\n",
782 ctdb_eventscript_call_names[call],
783 ctdb->tunable.recovery_ban_period));
787 return status.status;
790 int ctdb_event_script(struct ctdb_context *ctdb, enum ctdb_eventscript_call call)
792 /* GCC complains about empty format string, so use %s and "". */
793 return ctdb_event_script_args(ctdb, call, "%s", "");
796 struct eventscript_callback_state {
797 struct ctdb_req_control *c;
801 called when a forced eventscript run has finished
803 static void run_eventscripts_callback(struct ctdb_context *ctdb, int status,
806 struct eventscript_callback_state *state =
807 talloc_get_type(private_data, struct eventscript_callback_state);
809 ctdb_enable_monitoring(ctdb);
812 DEBUG(DEBUG_ERR,(__location__ " Failed to forcibly run eventscripts\n"));
815 ctdb_request_control_reply(ctdb, state->c, NULL, status, NULL);
816 /* This will free the struct ctdb_event_script_state we are in! */
822 /* Returns rest of string, or NULL if no match. */
823 static const char *get_call(const char *p, enum ctdb_eventscript_call *call)
827 /* Skip any initial whitespace. */
828 p += strspn(p, " \t");
830 /* See if we match any. */
831 for (*call = 0; *call < CTDB_EVENT_MAX; (*call)++) {
832 len = strlen(ctdb_eventscript_call_names[*call]);
833 if (strncmp(p, ctdb_eventscript_call_names[*call], len) == 0) {
834 /* If end of string or whitespace, we're done. */
835 if (strcspn(p + len, " \t") == 0) {
844 A control to force running of the eventscripts from the ctdb client tool
846 int32_t ctdb_run_eventscripts(struct ctdb_context *ctdb,
847 struct ctdb_req_control *c,
848 TDB_DATA indata, bool *async_reply)
851 struct eventscript_callback_state *state;
853 enum ctdb_eventscript_call call;
855 /* Figure out what call they want. */
856 options = get_call((const char *)indata.dptr, &call);
858 DEBUG(DEBUG_ERR, (__location__ " Invalid forced \"%s\"\n", (const char *)indata.dptr));
862 if (ctdb->recovery_mode != CTDB_RECOVERY_NORMAL) {
863 DEBUG(DEBUG_ERR, (__location__ " Aborted running eventscript \"%s\" while in RECOVERY mode\n", indata.dptr));
867 state = talloc(ctdb->event_script_ctx, struct eventscript_callback_state);
868 CTDB_NO_MEMORY(ctdb, state);
870 state->c = talloc_steal(state, c);
872 DEBUG(DEBUG_NOTICE,("Forced running of eventscripts with arguments %s\n", indata.dptr));
874 ctdb_disable_monitoring(ctdb);
876 ret = ctdb_event_script_callback(ctdb,
877 state, run_eventscripts_callback, state,
878 true, call, "%s", options);
881 ctdb_enable_monitoring(ctdb);
882 DEBUG(DEBUG_ERR,(__location__ " Failed to run eventscripts with arguments %s\n", indata.dptr));
887 /* tell ctdb_control.c that we will be replying asynchronously */
895 int32_t ctdb_control_enable_script(struct ctdb_context *ctdb, TDB_DATA indata)
900 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
902 script = (char *)indata.dptr;
903 if (indata.dsize == 0) {
904 DEBUG(DEBUG_ERR,(__location__ " No script specified.\n"));
905 talloc_free(tmp_ctx);
908 if (indata.dptr[indata.dsize - 1] != '\0') {
909 DEBUG(DEBUG_ERR,(__location__ " String is not null terminated.\n"));
910 talloc_free(tmp_ctx);
913 if (index(script,'/') != NULL) {
914 DEBUG(DEBUG_ERR,(__location__ " Script name contains '/'. Failed to enable script %s\n", script));
915 talloc_free(tmp_ctx);
920 if (stat(ctdb->event_script_dir, &st) != 0 &&
922 DEBUG(DEBUG_CRIT,("No event script directory found at '%s'\n", ctdb->event_script_dir));
923 talloc_free(tmp_ctx);
928 filename = talloc_asprintf(tmp_ctx, "%s/%s", ctdb->event_script_dir, script);
929 if (filename == NULL) {
930 DEBUG(DEBUG_ERR,(__location__ " Failed to create script path\n"));
931 talloc_free(tmp_ctx);
935 if (stat(filename, &st) != 0) {
936 DEBUG(DEBUG_ERR,("Could not stat event script %s. Failed to enable script.\n", filename));
937 talloc_free(tmp_ctx);
941 if (chmod(filename, st.st_mode | S_IXUSR) == -1) {
942 DEBUG(DEBUG_ERR,("Could not chmod %s. Failed to enable script.\n", filename));
943 talloc_free(tmp_ctx);
947 talloc_free(tmp_ctx);
951 int32_t ctdb_control_disable_script(struct ctdb_context *ctdb, TDB_DATA indata)
956 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
958 script = (char *)indata.dptr;
959 if (indata.dsize == 0) {
960 DEBUG(DEBUG_ERR,(__location__ " No script specified.\n"));
961 talloc_free(tmp_ctx);
964 if (indata.dptr[indata.dsize - 1] != '\0') {
965 DEBUG(DEBUG_ERR,(__location__ " String is not null terminated.\n"));
966 talloc_free(tmp_ctx);
969 if (index(script,'/') != NULL) {
970 DEBUG(DEBUG_ERR,(__location__ " Script name contains '/'. Failed to disable script %s\n", script));
971 talloc_free(tmp_ctx);
976 if (stat(ctdb->event_script_dir, &st) != 0 &&
978 DEBUG(DEBUG_CRIT,("No event script directory found at '%s'\n", ctdb->event_script_dir));
979 talloc_free(tmp_ctx);
984 filename = talloc_asprintf(tmp_ctx, "%s/%s", ctdb->event_script_dir, script);
985 if (filename == NULL) {
986 DEBUG(DEBUG_ERR,(__location__ " Failed to create script path\n"));
987 talloc_free(tmp_ctx);
991 if (stat(filename, &st) != 0) {
992 DEBUG(DEBUG_ERR,("Could not stat event script %s. Failed to disable script.\n", filename));
993 talloc_free(tmp_ctx);
997 if (chmod(filename, st.st_mode & ~(S_IXUSR|S_IXGRP|S_IXOTH)) == -1) {
998 DEBUG(DEBUG_ERR,("Could not chmod %s. Failed to disable script.\n", filename));
999 talloc_free(tmp_ctx);
1003 talloc_free(tmp_ctx);