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 struct ctdb_event_script_state {
43 struct ctdb_context *ctdb;
45 /* Warning: this can free us! */
46 void (*callback)(struct ctdb_context *, int, void *);
50 enum ctdb_eventscript_call call;
52 struct timeval timeout;
55 struct ctdb_scripts_wire *scripts;
58 static struct ctdb_script_wire *get_current_script(struct ctdb_event_script_state *state)
60 return &state->scripts->scripts[state->current];
63 /* called from ctdb_logging when we have received output on STDERR from
64 * one of the eventscripts
66 static void log_event_script_output(const char *str, uint16_t len, void *p)
68 struct ctdb_event_script_state *state
69 = talloc_get_type(p, struct ctdb_event_script_state);
70 struct ctdb_script_wire *current;
71 unsigned int slen, min;
73 /* We may have been aborted to run something else. Discard */
74 if (state->scripts == NULL) {
78 current = get_current_script(state);
80 /* Append, but don't overfill buffer. It starts zero-filled. */
81 slen = strlen(current->output);
82 min = MIN(len, sizeof(current->output) - slen - 1);
84 memcpy(current->output + slen, str, min);
87 int32_t ctdb_control_get_event_script_status(struct ctdb_context *ctdb,
91 if (call_type >= CTDB_EVENT_MAX) {
95 if (ctdb->last_status[call_type] == NULL) {
96 /* If it's never been run, return nothing so they can tell. */
99 outdata->dsize = talloc_get_size(ctdb->last_status[call_type]);
100 outdata->dptr = (uint8_t *)ctdb->last_status[call_type];
105 struct ctdb_script_tree_item {
110 /* Return true if OK, otherwise set errno. */
111 static bool check_executable(const char *dir, const char *name)
116 full = talloc_asprintf(NULL, "%s/%s", dir, name);
120 if (stat(full, &st) != 0) {
121 DEBUG(DEBUG_ERR,("Could not stat event script %s: %s\n",
122 full, strerror(errno)));
127 if (!(st.st_mode & S_IXUSR)) {
128 DEBUG(DEBUG_DEBUG,("Event script %s is not executable. Ignoring this event script\n", full));
138 static struct ctdb_scripts_wire *ctdb_get_script_list(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx)
144 struct ctdb_scripts_wire *scripts;
145 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
146 struct ctdb_script_tree_item *tree_item;
150 the service specific event scripts
152 if (stat(ctdb->event_script_dir, &st) != 0 &&
154 DEBUG(DEBUG_CRIT,("No event script directory found at '%s'\n", ctdb->event_script_dir));
155 talloc_free(tmp_ctx);
159 /* create a tree to store all the script names in */
160 tree = trbt_create(tmp_ctx, 0);
162 /* scan all directory entries and insert all valid scripts into the
165 dir = opendir(ctdb->event_script_dir);
167 DEBUG(DEBUG_CRIT,("Failed to open event script directory '%s'\n", ctdb->event_script_dir));
168 talloc_free(tmp_ctx);
173 while ((de=readdir(dir)) != NULL) {
177 namlen = strlen(de->d_name);
183 if (de->d_name[namlen-1] == '~') {
184 /* skip files emacs left behind */
188 if (de->d_name[2] != '.') {
192 if (sscanf(de->d_name, "%02u.", &num) != 1) {
196 if (strlen(de->d_name) > MAX_SCRIPT_NAME) {
197 DEBUG(DEBUG_ERR,("Script name %s too long! %u chars max",
198 de->d_name, MAX_SCRIPT_NAME));
202 tree_item = talloc(tree, struct ctdb_script_tree_item);
203 if (tree_item == NULL) {
204 DEBUG(DEBUG_ERR, (__location__ " Failed to allocate new tree item\n"));
205 talloc_free(tmp_ctx);
209 tree_item->error = 0;
210 if (!check_executable(ctdb->event_script_dir, de->d_name)) {
211 tree_item->error = errno;
214 tree_item->name = talloc_strdup(tree_item, de->d_name);
215 if (tree_item->name == NULL) {
216 DEBUG(DEBUG_ERR,(__location__ " Failed to allocate script name.\n"));
217 talloc_free(tmp_ctx);
221 /* store the event script in the tree */
222 trbt_insert32(tree, (num<<16)|count++, tree_item);
226 /* Overallocates by one, but that's OK */
227 scripts = talloc_zero_size(tmp_ctx,
229 + sizeof(scripts->scripts[0]) * count);
230 if (scripts == NULL) {
231 DEBUG(DEBUG_ERR, (__location__ " Failed to allocate scripts\n"));
232 talloc_free(tmp_ctx);
235 scripts->num_scripts = count;
237 for (count = 0; count < scripts->num_scripts; count++) {
238 tree_item = trbt_findfirstarray32(tree, 1);
240 strcpy(scripts->scripts[count].name, tree_item->name);
241 scripts->scripts[count].status = -tree_item->error;
243 /* remove this script from the tree */
244 talloc_free(tree_item);
247 talloc_steal(mem_ctx, scripts);
248 talloc_free(tmp_ctx);
252 static int child_setup(struct ctdb_context *ctdb)
254 if (setpgid(0,0) != 0) {
256 DEBUG(DEBUG_ERR,("Failed to create process group for event scripts - %s\n",
261 signal(SIGTERM, sigterm);
265 static char *child_command_string(struct ctdb_context *ctdb,
268 const char *scriptname,
269 enum ctdb_eventscript_call call,
272 const char *str = from_user ? "CTDB_CALLED_BY_USER=1 " : "";
274 /* Allow a setting where we run the actual monitor event
275 from an external source and replace it with
276 a "status" event that just picks up the actual
277 status of the event asynchronously.
279 if ((ctdb->tunable.use_status_events_for_monitoring != 0)
280 && (call == CTDB_EVENT_MONITOR)
282 return talloc_asprintf(ctx, "%s%s/%s %s",
284 ctdb->event_script_dir,
285 scriptname, "status");
287 return talloc_asprintf(ctx, "%s%s/%s %s %s",
289 ctdb->event_script_dir,
291 ctdb_eventscript_call_names[call],
296 static int child_run_one(struct ctdb_context *ctdb,
297 const char *scriptname, const char *cmdstr)
301 ret = system(cmdstr);
302 /* if the system() call was successful, translate ret into the
303 return code from the command
306 ret = WEXITSTATUS(ret);
311 /* 127 could mean it does not exist, 126 non-executable. */
312 if (ret == 127 || ret == 126) {
314 if (!check_executable(ctdb->event_script_dir, scriptname)) {
315 DEBUG(DEBUG_ERR,("Script %s returned status %u. Someone just deleted it?\n",
324 Actually run one event script
325 this function is called and run in the context of a forked child
326 which allows it to do blocking calls such as system()
328 static int child_run_script(struct ctdb_context *ctdb,
330 enum ctdb_eventscript_call call,
332 struct ctdb_script_wire *current)
336 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
338 ret = child_setup(ctdb);
342 cmdstr = child_command_string(ctdb, tmp_ctx, from_user,
343 current->name, call, options);
344 CTDB_NO_MEMORY(ctdb, cmdstr);
346 DEBUG(DEBUG_DEBUG,("Executing event script %s\n",cmdstr));
348 if (current->status) {
349 ret = current->status;
353 ret = child_run_one(ctdb, current->name, cmdstr);
355 talloc_free(tmp_ctx);
359 static void ctdb_event_script_handler(struct event_context *ev, struct fd_event *fde,
360 uint16_t flags, void *p);
362 static int fork_child_for_script(struct ctdb_context *ctdb,
363 struct ctdb_event_script_state *state)
366 struct tevent_fd *fde;
367 struct ctdb_script_wire *current = get_current_script(state);
369 current->start = timeval_current();
373 DEBUG(DEBUG_ERR, (__location__ " pipe failed for child eventscript process\n"));
377 if (!ctdb_fork_with_logging(state, ctdb, log_event_script_output,
378 state, &state->child)) {
385 /* If we are the child, do the work. */
386 if (state->child == 0) {
390 set_close_on_exec(state->fd[1]);
392 rt = child_run_script(ctdb, state->from_user, state->call, state->options, current);
393 /* We must be able to write PIPEBUF bytes at least; if this
394 somehow fails, the read above will be short. */
395 write(state->fd[1], &rt, sizeof(rt));
401 set_close_on_exec(state->fd[0]);
403 DEBUG(DEBUG_DEBUG, (__location__ " Created PIPE FD:%d to child eventscript process\n", state->fd[0]));
405 /* Set ourselves up to be called when that's done. */
406 fde = event_add_fd(ctdb->ev, state, state->fd[0], EVENT_FD_READ,
407 ctdb_event_script_handler, state);
408 tevent_fd_set_auto_close(fde);
414 Summarize status of this run of scripts.
416 static int script_status(struct ctdb_scripts_wire *scripts)
420 for (i = 0; i < scripts->num_scripts; i++) {
421 switch (scripts->scripts[i].status) {
424 /* Disabled or missing; that's OK. */
430 return scripts->scripts[i].status;
438 /* called when child is finished */
439 static void ctdb_event_script_handler(struct event_context *ev, struct fd_event *fde,
440 uint16_t flags, void *p)
442 struct ctdb_event_script_state *state =
443 talloc_get_type(p, struct ctdb_event_script_state);
444 struct ctdb_script_wire *current = get_current_script(state);
445 struct ctdb_context *ctdb = state->ctdb;
448 r = read(state->fd[0], ¤t->status, sizeof(current->status));
450 current->status = -errno;
451 } else if (r != sizeof(current->status)) {
452 current->status = -EIO;
455 current->finished = timeval_current();
456 /* valgrind gets overloaded if we run next script as it's still doing
457 * post-execution analysis, so kill finished child here. */
458 if (ctdb->valgrinding) {
459 kill(state->child, SIGKILL);
464 status = script_status(state->scripts);
466 /* Aborted or finished all scripts? We're done. */
467 if (status != 0 || state->current+1 == state->scripts->num_scripts) {
468 DEBUG(DEBUG_INFO,(__location__ " Eventscript %s %s finished with state %d\n",
469 ctdb_eventscript_call_names[state->call], state->options, status));
471 ctdb->event_script_timeouts = 0;
476 /* Forget about that old fd. */
482 current->status = fork_child_for_script(ctdb, state);
483 if (current->status != 0) {
484 /* This calls the callback. */
489 static void debug_timeout(struct ctdb_event_script_state *state)
491 struct ctdb_script_wire *current = get_current_script(state);
495 char tbuf[100], buf[200];
497 cmd = child_command_string(state->ctdb, state,
498 state->from_user, current->name,
499 state->call, state->options);
500 CTDB_NO_MEMORY_VOID(state->ctdb, cmd);
502 DEBUG(DEBUG_ERR,("Timed out running script '%s' after %.1f seconds pid :%d\n",
503 cmd, timeval_elapsed(¤t->start), state->child));
507 strftime(tbuf, sizeof(tbuf)-1, "%Y%m%d%H%M%S", localtime(&t));
508 sprintf(buf, "{ pstree -p; cat /proc/locks; ls -li /var/ctdb/ /var/ctdb/persistent; }"
509 " >/tmp/ctdb.event.%s.%d", tbuf, getpid());
513 ctdb_reduce_priority(state->ctdb);
515 /* Now we can kill the child */
516 kill(state->child, SIGTERM);
520 DEBUG(DEBUG_ERR,("Fork for debug script failed : %s\n",
523 DEBUG(DEBUG_ERR,("Logged timedout eventscript : %s\n", buf));
524 /* Don't kill child until timeout done. */
529 /* called when child times out */
530 static void ctdb_event_script_timeout(struct event_context *ev, struct timed_event *te,
531 struct timeval t, void *p)
533 struct ctdb_event_script_state *state = talloc_get_type(p, struct ctdb_event_script_state);
534 struct ctdb_context *ctdb = state->ctdb;
535 struct ctdb_script_wire *current = get_current_script(state);
537 DEBUG(DEBUG_ERR,("Event script timed out : %s %s %s count : %u pid : %d\n",
538 current->name, ctdb_eventscript_call_names[state->call], state->options, ctdb->event_script_timeouts, state->child));
540 /* ignore timeouts for these events */
541 switch (state->call) {
542 case CTDB_EVENT_START_RECOVERY:
543 case CTDB_EVENT_RECOVERED:
544 case CTDB_EVENT_TAKE_IP:
545 case CTDB_EVENT_RELEASE_IP:
546 case CTDB_EVENT_STOPPED:
547 case CTDB_EVENT_MONITOR:
548 case CTDB_EVENT_STATUS:
549 state->scripts->scripts[state->current].status = 0;
550 DEBUG(DEBUG_ERR,("Ignoring hung script for %s call %d\n", state->options, state->call));
553 state->scripts->scripts[state->current].status = -ETIME;
554 debug_timeout(state);
561 destroy an event script: kill it if ->child != 0.
563 static int event_script_destructor(struct ctdb_event_script_state *state)
568 DEBUG(DEBUG_ERR,(__location__ " Sending SIGTERM to child pid:%d\n", state->child));
570 if (kill(state->child, SIGTERM) != 0) {
571 DEBUG(DEBUG_ERR,("Failed to kill child process for eventscript, errno %s(%d)\n", strerror(errno), errno));
575 /* If we were the current monitor, we no longer are. */
576 if (state->ctdb->current_monitor == state) {
577 state->ctdb->current_monitor = NULL;
580 /* Save our scripts as the last executed status, if we have them.
581 * See ctdb_event_script_callback_v where we abort monitor event. */
582 if (state->scripts) {
583 talloc_free(state->ctdb->last_status[state->call]);
584 state->ctdb->last_status[state->call] = state->scripts;
585 if (state->current < state->ctdb->last_status[state->call]->num_scripts) {
586 state->ctdb->last_status[state->call]->num_scripts = state->current+1;
590 /* Use last status as result, or "OK" if none. */
591 if (state->ctdb->last_status[state->call]) {
592 status = script_status(state->ctdb->last_status[state->call]);
597 /* This is allowed to free us; talloc will prevent double free anyway,
598 * but beware if you call this outside the destructor! */
599 if (state->callback) {
600 state->callback(state->ctdb, status, state->private_data);
606 static unsigned int count_words(const char *options)
608 unsigned int words = 0;
610 options += strspn(options, " \t");
613 options += strcspn(options, " \t");
614 options += strspn(options, " \t");
619 static bool check_options(enum ctdb_eventscript_call call, const char *options)
622 /* These all take no arguments. */
623 case CTDB_EVENT_INIT:
624 case CTDB_EVENT_SETUP:
625 case CTDB_EVENT_STARTUP:
626 case CTDB_EVENT_START_RECOVERY:
627 case CTDB_EVENT_RECOVERED:
628 case CTDB_EVENT_STOPPED:
629 case CTDB_EVENT_MONITOR:
630 case CTDB_EVENT_STATUS:
631 case CTDB_EVENT_SHUTDOWN:
632 case CTDB_EVENT_RELOAD:
633 return count_words(options) == 0;
635 case CTDB_EVENT_TAKE_IP: /* interface, IP address, netmask bits. */
636 case CTDB_EVENT_RELEASE_IP:
637 return count_words(options) == 3;
639 case CTDB_EVENT_UPDATE_IP: /* old interface, new interface, IP address, netmask bits. */
640 return count_words(options) == 4;
643 DEBUG(DEBUG_ERR,(__location__ "Unknown ctdb_eventscript_call %u\n", call));
649 run the event script in the background, calling the callback when
652 static int ctdb_event_script_callback_v(struct ctdb_context *ctdb,
653 void (*callback)(struct ctdb_context *, int, void *),
656 enum ctdb_eventscript_call call,
657 const char *fmt, va_list ap)
659 struct ctdb_event_script_state *state;
661 state = talloc(ctdb->event_script_ctx, struct ctdb_event_script_state);
662 CTDB_NO_MEMORY(ctdb, state);
665 state->callback = callback;
666 state->private_data = private_data;
667 state->from_user = from_user;
669 state->options = talloc_vasprintf(state, fmt, ap);
670 state->timeout = timeval_set(ctdb->tunable.script_timeout, 0);
671 state->scripts = NULL;
672 if (state->options == NULL) {
673 DEBUG(DEBUG_ERR, (__location__ " could not allocate state->options\n"));
677 if (!check_options(state->call, state->options)) {
678 DEBUG(DEBUG_ERR, ("Bad eventscript options '%s' for %s\n",
679 ctdb_eventscript_call_names[state->call], state->options));
684 if (ctdb->recovery_mode != CTDB_RECOVERY_NORMAL) {
685 /* we guarantee that only some specifically allowed event scripts are run
687 const enum ctdb_eventscript_call allowed_calls[] = {
690 CTDB_EVENT_START_RECOVERY,
692 CTDB_EVENT_RELEASE_IP,
696 for (i=0;i<ARRAY_SIZE(allowed_calls);i++) {
697 if (call == allowed_calls[i]) break;
699 if (i == ARRAY_SIZE(allowed_calls)) {
700 DEBUG(DEBUG_ERR,("Refusing to run event scripts call '%s' while in recovery\n",
701 ctdb_eventscript_call_names[call]));
707 /* Kill off any running monitor events to run this event. */
708 if (ctdb->current_monitor) {
709 /* Discard script status so we don't save to last_status */
710 talloc_free(ctdb->current_monitor->scripts);
711 ctdb->current_monitor->scripts = NULL;
712 talloc_free(ctdb->current_monitor);
713 ctdb->current_monitor = NULL;
716 DEBUG(DEBUG_INFO,(__location__ " Starting eventscript %s %s\n",
717 ctdb_eventscript_call_names[state->call],
720 /* This is not a child of state, since we save it in destructor. */
721 state->scripts = ctdb_get_script_list(ctdb, ctdb);
722 if (state->scripts == NULL) {
729 if (!from_user && (call == CTDB_EVENT_MONITOR || call == CTDB_EVENT_STATUS)) {
730 ctdb->current_monitor = state;
733 talloc_set_destructor(state, event_script_destructor);
736 if (state->scripts->num_scripts == 0) {
741 state->scripts->scripts[0].status = fork_child_for_script(ctdb, state);
742 if (state->scripts->scripts[0].status != 0) {
743 /* Callback is called from destructor, with fail result. */
748 if (!timeval_is_zero(&state->timeout)) {
749 event_add_timed(ctdb->ev, state, timeval_current_ofs(state->timeout.tv_sec, state->timeout.tv_usec), ctdb_event_script_timeout, state);
751 DEBUG(DEBUG_ERR, (__location__ " eventscript %s %s called with no timeout\n",
752 ctdb_eventscript_call_names[state->call],
761 run the event script in the background, calling the callback when
764 int ctdb_event_script_callback(struct ctdb_context *ctdb,
766 void (*callback)(struct ctdb_context *, int, void *),
769 enum ctdb_eventscript_call call,
770 const char *fmt, ...)
776 ret = ctdb_event_script_callback_v(ctdb, callback, private_data, from_user, call, fmt, ap);
783 struct callback_status {
789 called when ctdb_event_script() finishes
791 static void event_script_callback(struct ctdb_context *ctdb, int status, void *private_data)
793 struct callback_status *s = (struct callback_status *)private_data;
799 run the event script, waiting for it to complete. Used when the caller
800 doesn't want to continue till the event script has finished.
802 int ctdb_event_script_args(struct ctdb_context *ctdb, enum ctdb_eventscript_call call,
803 const char *fmt, ...)
807 struct callback_status status;
810 ret = ctdb_event_script_callback_v(ctdb,
811 event_script_callback, &status, false, call, fmt, ap);
820 while (status.done == false && event_loop_once(ctdb->ev) == 0) /* noop */;
822 if (status.status == -ETIME) {
823 DEBUG(DEBUG_ERR, (__location__ " eventscript for '%s' timedout."
824 " Immediately banning ourself for %d seconds\n",
825 ctdb_eventscript_call_names[call],
826 ctdb->tunable.recovery_ban_period));
830 return status.status;
833 int ctdb_event_script(struct ctdb_context *ctdb, enum ctdb_eventscript_call call)
835 /* GCC complains about empty format string, so use %s and "". */
836 return ctdb_event_script_args(ctdb, call, "%s", "");
839 struct eventscript_callback_state {
840 struct ctdb_req_control *c;
844 called when a forced eventscript run has finished
846 static void run_eventscripts_callback(struct ctdb_context *ctdb, int status,
849 struct eventscript_callback_state *state =
850 talloc_get_type(private_data, struct eventscript_callback_state);
852 ctdb_enable_monitoring(ctdb);
855 DEBUG(DEBUG_ERR,(__location__ " Failed to forcibly run eventscripts\n"));
858 ctdb_request_control_reply(ctdb, state->c, NULL, status, NULL);
859 /* This will free the struct ctdb_event_script_state we are in! */
865 /* Returns rest of string, or NULL if no match. */
866 static const char *get_call(const char *p, enum ctdb_eventscript_call *call)
870 /* Skip any initial whitespace. */
871 p += strspn(p, " \t");
873 /* See if we match any. */
874 for (*call = 0; *call < CTDB_EVENT_MAX; (*call)++) {
875 len = strlen(ctdb_eventscript_call_names[*call]);
876 if (strncmp(p, ctdb_eventscript_call_names[*call], len) == 0) {
877 /* If end of string or whitespace, we're done. */
878 if (strcspn(p + len, " \t") == 0) {
887 A control to force running of the eventscripts from the ctdb client tool
889 int32_t ctdb_run_eventscripts(struct ctdb_context *ctdb,
890 struct ctdb_req_control *c,
891 TDB_DATA indata, bool *async_reply)
894 struct eventscript_callback_state *state;
896 enum ctdb_eventscript_call call;
898 /* Figure out what call they want. */
899 options = get_call((const char *)indata.dptr, &call);
901 DEBUG(DEBUG_ERR, (__location__ " Invalid forced \"%s\"\n", (const char *)indata.dptr));
905 if (ctdb->recovery_mode != CTDB_RECOVERY_NORMAL) {
906 DEBUG(DEBUG_ERR, (__location__ " Aborted running eventscript \"%s\" while in RECOVERY mode\n", indata.dptr));
910 state = talloc(ctdb->event_script_ctx, struct eventscript_callback_state);
911 CTDB_NO_MEMORY(ctdb, state);
913 state->c = talloc_steal(state, c);
915 DEBUG(DEBUG_NOTICE,("Forced running of eventscripts with arguments %s\n", indata.dptr));
917 ctdb_disable_monitoring(ctdb);
919 ret = ctdb_event_script_callback(ctdb,
920 state, run_eventscripts_callback, state,
921 true, call, "%s", options);
924 ctdb_enable_monitoring(ctdb);
925 DEBUG(DEBUG_ERR,(__location__ " Failed to run eventscripts with arguments %s\n", indata.dptr));
930 /* tell ctdb_control.c that we will be replying asynchronously */
938 int32_t ctdb_control_enable_script(struct ctdb_context *ctdb, TDB_DATA indata)
943 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
945 script = (char *)indata.dptr;
946 if (indata.dsize == 0) {
947 DEBUG(DEBUG_ERR,(__location__ " No script specified.\n"));
948 talloc_free(tmp_ctx);
951 if (indata.dptr[indata.dsize - 1] != '\0') {
952 DEBUG(DEBUG_ERR,(__location__ " String is not null terminated.\n"));
953 talloc_free(tmp_ctx);
956 if (index(script,'/') != NULL) {
957 DEBUG(DEBUG_ERR,(__location__ " Script name contains '/'. Failed to enable script %s\n", script));
958 talloc_free(tmp_ctx);
963 if (stat(ctdb->event_script_dir, &st) != 0 &&
965 DEBUG(DEBUG_CRIT,("No event script directory found at '%s'\n", ctdb->event_script_dir));
966 talloc_free(tmp_ctx);
971 filename = talloc_asprintf(tmp_ctx, "%s/%s", ctdb->event_script_dir, script);
972 if (filename == NULL) {
973 DEBUG(DEBUG_ERR,(__location__ " Failed to create script path\n"));
974 talloc_free(tmp_ctx);
978 if (stat(filename, &st) != 0) {
979 DEBUG(DEBUG_ERR,("Could not stat event script %s. Failed to enable script.\n", filename));
980 talloc_free(tmp_ctx);
984 if (chmod(filename, st.st_mode | S_IXUSR) == -1) {
985 DEBUG(DEBUG_ERR,("Could not chmod %s. Failed to enable script.\n", filename));
986 talloc_free(tmp_ctx);
990 talloc_free(tmp_ctx);
994 int32_t ctdb_control_disable_script(struct ctdb_context *ctdb, TDB_DATA indata)
999 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1001 script = (char *)indata.dptr;
1002 if (indata.dsize == 0) {
1003 DEBUG(DEBUG_ERR,(__location__ " No script specified.\n"));
1004 talloc_free(tmp_ctx);
1007 if (indata.dptr[indata.dsize - 1] != '\0') {
1008 DEBUG(DEBUG_ERR,(__location__ " String is not null terminated.\n"));
1009 talloc_free(tmp_ctx);
1012 if (index(script,'/') != NULL) {
1013 DEBUG(DEBUG_ERR,(__location__ " Script name contains '/'. Failed to disable script %s\n", script));
1014 talloc_free(tmp_ctx);
1019 if (stat(ctdb->event_script_dir, &st) != 0 &&
1021 DEBUG(DEBUG_CRIT,("No event script directory found at '%s'\n", ctdb->event_script_dir));
1022 talloc_free(tmp_ctx);
1027 filename = talloc_asprintf(tmp_ctx, "%s/%s", ctdb->event_script_dir, script);
1028 if (filename == NULL) {
1029 DEBUG(DEBUG_ERR,(__location__ " Failed to create script path\n"));
1030 talloc_free(tmp_ctx);
1034 if (stat(filename, &st) != 0) {
1035 DEBUG(DEBUG_ERR,("Could not stat event script %s. Failed to disable script.\n", filename));
1036 talloc_free(tmp_ctx);
1040 if (chmod(filename, st.st_mode & ~(S_IXUSR|S_IXGRP|S_IXOTH)) == -1) {
1041 DEBUG(DEBUG_ERR,("Could not chmod %s. Failed to disable script.\n", filename));
1042 talloc_free(tmp_ctx);
1046 talloc_free(tmp_ctx);