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/>.
21 #include "system/filesys.h"
22 #include "system/wait.h"
23 #include "system/dir.h"
24 #include "system/locale.h"
25 #include "../include/ctdb_private.h"
26 #include "lib/events/events.h"
27 #include "../common/rb_tree.h"
31 const char *script_running;
35 ctdbd sends us a SIGTERM when we should time out the current script
37 static void sigterm(int sig)
41 DEBUG(DEBUG_ERR,("Timed out running script '%s' after %.1f seconds pid :%d\n",
42 child_state.script_running, timeval_elapsed(&child_state.start), getpid()));
44 p = popen("pstree -p", "r");
46 DEBUG(DEBUG_ERR,("Failed popen to collect pstree for hung script\n"));
51 DEBUG(DEBUG_ERR,("PSTREE:\n"));
53 count=fread(buf, 1, 255, p);
64 DEBUG(DEBUG_ERR,("%s", buf));
66 DEBUG(DEBUG_ERR,("END OF PSTREE OUTPUT\n"));
70 /* all the child processes will be running in the same process group */
71 kill(-getpgrp(), SIGKILL);
75 struct ctdb_event_script_state {
76 struct ctdb_context *ctdb;
78 void (*callback)(struct ctdb_context *, int, void *);
85 struct ctdb_monitor_script_status {
86 struct ctdb_monitor_script_status *next;
89 struct timeval finished;
96 struct ctdb_monitoring_status {
98 struct timeval finished;
100 struct ctdb_monitor_script_status *scripts;
104 /* called from ctdb_logging when we have received output on STDERR from
105 * one of the eventscripts
107 int ctdb_log_event_script_output(struct ctdb_context *ctdb, char *str, uint16_t len)
109 struct ctdb_monitoring_status *monitoring_status =
110 talloc_get_type(ctdb->script_monitoring_ctx,
111 struct ctdb_monitoring_status);
112 struct ctdb_monitor_script_status *script;
114 if (monitoring_status == NULL) {
118 script = monitoring_status->scripts;
119 if (script == NULL) {
123 if (script->output == NULL) {
124 script->output = talloc_asprintf(script, "%*.*s", len, len, str);
126 script->output = talloc_asprintf_append(script->output, "%*.*s", len, len, str);
132 /* called from the event script child process when we are starting a new
135 int32_t ctdb_control_event_script_init(struct ctdb_context *ctdb)
137 struct ctdb_monitoring_status *monitoring_status;
139 DEBUG(DEBUG_INFO, ("event script init called\n"));
140 if (ctdb->script_monitoring_ctx != NULL) {
141 talloc_free(ctdb->script_monitoring_ctx);
142 ctdb->script_monitoring_ctx = NULL;
145 monitoring_status = talloc_zero(ctdb, struct ctdb_monitoring_status);
146 if (monitoring_status == NULL) {
147 DEBUG(DEBUG_ERR, (__location__ " ERROR: Failed to talloc script_monitoring context\n"));
151 ctdb->script_monitoring_ctx = monitoring_status;
152 monitoring_status->start = timeval_current();
158 /* called from the event script child process when we are star running
161 int32_t ctdb_control_event_script_start(struct ctdb_context *ctdb, TDB_DATA indata)
163 const char *name = (const char *)indata.dptr;
164 struct ctdb_monitoring_status *monitoring_status =
165 talloc_get_type(ctdb->script_monitoring_ctx,
166 struct ctdb_monitoring_status);
167 struct ctdb_monitor_script_status *script;
169 DEBUG(DEBUG_INFO, ("event script start called : %s\n", name));
171 if (monitoring_status == NULL) {
172 DEBUG(DEBUG_ERR,(__location__ " script_status is NULL when starting to run script %s\n", name));
176 script = talloc_zero(monitoring_status, struct ctdb_monitor_script_status);
177 if (script == NULL) {
178 DEBUG(DEBUG_ERR,(__location__ " Failed to talloc ctdb_monitor_script_status for script %s\n", name));
182 script->next = monitoring_status->scripts;
183 script->name = talloc_strdup(script, name);
184 CTDB_NO_MEMORY(ctdb, script->name);
185 script->start = timeval_current();
186 monitoring_status->scripts = script;
191 /* called from the event script child process when we have finished running
194 int32_t ctdb_control_event_script_stop(struct ctdb_context *ctdb, TDB_DATA indata)
196 int32_t res = *((int32_t *)indata.dptr);
197 struct ctdb_monitoring_status *monitoring_status =
198 talloc_get_type(ctdb->script_monitoring_ctx,
199 struct ctdb_monitoring_status);
200 struct ctdb_monitor_script_status *script;
202 DEBUG(DEBUG_INFO, ("event script stop called : %d\n", (int)res));
204 if (monitoring_status == NULL) {
205 DEBUG(DEBUG_ERR,(__location__ " script_status is NULL when script finished.\n"));
209 script = monitoring_status->scripts;
210 if (script == NULL) {
211 DEBUG(DEBUG_ERR,(__location__ " script is NULL when the script had finished\n"));
215 script->finished = timeval_current();
216 script->status = res;
221 /* called from the event script child process when we have a disabled script
223 int32_t ctdb_control_event_script_disabled(struct ctdb_context *ctdb, TDB_DATA indata)
225 const char *name = (const char *)indata.dptr;
226 struct ctdb_monitoring_status *monitoring_status =
227 talloc_get_type(ctdb->script_monitoring_ctx,
228 struct ctdb_monitoring_status);
229 struct ctdb_monitor_script_status *script;
231 DEBUG(DEBUG_INFO, ("event script disabed called for script %s\n", name));
233 if (monitoring_status == NULL) {
234 DEBUG(DEBUG_ERR,(__location__ " script_status is NULL when script finished.\n"));
238 script = monitoring_status->scripts;
239 if (script == NULL) {
240 DEBUG(DEBUG_ERR,(__location__ " script is NULL when the script had finished\n"));
244 script->finished = timeval_current();
246 script->disabled = 1;
251 /* called from the event script child process when we have completed a
254 int32_t ctdb_control_event_script_finished(struct ctdb_context *ctdb)
256 struct ctdb_monitoring_status *monitoring_status =
257 talloc_get_type(ctdb->script_monitoring_ctx,
258 struct ctdb_monitoring_status);
260 DEBUG(DEBUG_INFO, ("event script finished called\n"));
262 if (monitoring_status == NULL) {
263 DEBUG(DEBUG_ERR,(__location__ " script_status is NULL when monitoring event finished\n"));
267 monitoring_status->finished = timeval_current();
268 monitoring_status->status = MONITOR_SCRIPT_OK;
269 if (ctdb->last_monitoring_ctx) {
270 talloc_free(ctdb->last_monitoring_ctx);
272 ctdb->last_monitoring_ctx = ctdb->script_monitoring_ctx;
273 ctdb->script_monitoring_ctx = NULL;
278 static struct ctdb_monitoring_wire *marshall_monitoring_scripts(TALLOC_CTX *mem_ctx, struct ctdb_monitoring_wire *monitoring_scripts, struct ctdb_monitor_script_status *script)
280 struct ctdb_monitoring_script_wire script_wire;
283 if (script == NULL) {
284 return monitoring_scripts;
286 monitoring_scripts = marshall_monitoring_scripts(mem_ctx, monitoring_scripts, script->next);
287 if (monitoring_scripts == NULL) {
291 bzero(&script_wire, sizeof(struct ctdb_monitoring_script_wire));
292 strncpy(script_wire.name, script->name, MAX_SCRIPT_NAME);
293 script_wire.start = script->start;
294 script_wire.finished = script->finished;
295 script_wire.disabled = script->disabled;
296 script_wire.status = script->status;
297 script_wire.timedout = script->timedout;
298 if (script->output != NULL) {
299 strncpy(script_wire.output, script->output, MAX_SCRIPT_OUTPUT);
302 size = talloc_get_size(monitoring_scripts);
303 monitoring_scripts = talloc_realloc_size(mem_ctx, monitoring_scripts, size + sizeof(struct ctdb_monitoring_script_wire));
304 if (monitoring_scripts == NULL) {
305 DEBUG(DEBUG_ERR,(__location__ " Failed to talloc_resize monitoring_scripts blob\n"));
309 memcpy(&monitoring_scripts->scripts[monitoring_scripts->num_scripts], &script_wire, sizeof(script_wire));
310 monitoring_scripts->num_scripts++;
312 return monitoring_scripts;
315 int32_t ctdb_control_get_event_script_status(struct ctdb_context *ctdb, TDB_DATA *outdata)
317 struct ctdb_monitoring_status *monitoring_status =
318 talloc_get_type(ctdb->last_monitoring_ctx,
319 struct ctdb_monitoring_status);
320 struct ctdb_monitoring_wire *monitoring_scripts;
322 if (monitoring_status == NULL) {
323 DEBUG(DEBUG_ERR,(__location__ " last_monitor_ctx is NULL when reading status\n"));
327 monitoring_scripts = talloc_size(outdata, offsetof(struct ctdb_monitoring_wire, scripts));
328 if (monitoring_scripts == NULL) {
329 DEBUG(DEBUG_ERR,(__location__ " failed to talloc monitoring_scripts structure\n"));
333 monitoring_scripts->num_scripts = 0;
334 monitoring_scripts = marshall_monitoring_scripts(outdata, monitoring_scripts, monitoring_status->scripts);
335 if (monitoring_scripts == NULL) {
336 DEBUG(DEBUG_ERR,(__location__ " Monitoring scritps is NULL. can not return data to client\n"));
340 outdata->dsize = talloc_get_size(monitoring_scripts);
341 outdata->dptr = (uint8_t *)monitoring_scripts;
346 struct ctdb_script_tree_item {
351 struct ctdb_script_list {
352 struct ctdb_script_list *next;
357 static struct ctdb_script_list *ctdb_get_script_list(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx)
363 struct ctdb_script_list *head, *tail, *new_item;
364 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
365 struct ctdb_script_tree_item *tree_item;
369 the service specific event scripts
371 if (stat(ctdb->event_script_dir, &st) != 0 &&
373 DEBUG(DEBUG_CRIT,("No event script directory found at '%s'\n", ctdb->event_script_dir));
374 talloc_free(tmp_ctx);
378 /* create a tree to store all the script names in */
379 tree = trbt_create(tmp_ctx, 0);
381 /* scan all directory entries and insert all valid scripts into the
384 dir = opendir(ctdb->event_script_dir);
386 DEBUG(DEBUG_CRIT,("Failed to open event script directory '%s'\n", ctdb->event_script_dir));
387 talloc_free(tmp_ctx);
392 while ((de=readdir(dir)) != NULL) {
397 namlen = strlen(de->d_name);
403 if (de->d_name[namlen-1] == '~') {
404 /* skip files emacs left behind */
408 if (de->d_name[2] != '.') {
412 if (sscanf(de->d_name, "%02u.", &num) != 1) {
416 /* Make sure the event script is executable */
417 str = talloc_asprintf(tree, "%s/%s", ctdb->event_script_dir, de->d_name);
418 if (stat(str, &st) != 0) {
419 DEBUG(DEBUG_ERR,("Could not stat event script %s. Ignoring this event script\n", str));
424 tree_item = talloc(tree, struct ctdb_script_tree_item);
425 if (tree_item == NULL) {
426 DEBUG(DEBUG_ERR, (__location__ " Failed to allocate new tree item\n"));
427 talloc_free(tmp_ctx);
431 tree_item->is_enabled = 1;
432 if (!(st.st_mode & S_IXUSR)) {
433 DEBUG(DEBUG_INFO,("Event script %s is not executable. Ignoring this event script\n", str));
434 tree_item->is_enabled = 0;
437 tree_item->name = talloc_strdup(tree_item, de->d_name);
438 if (tree_item->name == NULL) {
439 DEBUG(DEBUG_ERR,(__location__ " Failed to allocate script name.\n"));
440 talloc_free(tmp_ctx);
444 /* store the event script in the tree */
445 trbt_insert32(tree, (num<<16)|count++, tree_item);
453 /* fetch the scripts from the tree one by one and add them to the linked
456 while ((tree_item=trbt_findfirstarray32(tree, 1)) != NULL) {
458 new_item = talloc(tmp_ctx, struct ctdb_script_list);
459 if (new_item == NULL) {
460 DEBUG(DEBUG_ERR, (__location__ " Failed to allocate new list item\n"));
461 talloc_free(tmp_ctx);
465 new_item->next = NULL;
466 new_item->name = talloc_steal(new_item, tree_item->name);
467 new_item->is_enabled = tree_item->is_enabled;
473 tail->next = new_item;
477 talloc_steal(mem_ctx, new_item);
479 /* remove this script from the tree */
480 talloc_free(tree_item);
483 talloc_free(tmp_ctx);
490 run the event script - varargs version
491 this function is called and run in the context of a forked child
492 which allows it to do blocking calls such as system()
494 static int ctdb_event_script_v(struct ctdb_context *ctdb, const char *options)
498 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
499 struct ctdb_script_list *scripts, *current;
502 if (!strcmp(options, "monitor")) {
506 if (is_monitor == 1) {
507 /* This is running in the forked child process. At this stage
508 * we want to switch from being a ctdb daemon into being a
509 * client and connect to the real local daemon.
511 if (switch_from_server_to_client(ctdb) != 0) {
512 DEBUG(DEBUG_CRIT, (__location__ "ERROR: failed to switch eventscript child into client mode. shutting down.\n"));
516 if (ctdb_ctrl_event_script_init(ctdb) != 0) {
517 DEBUG(DEBUG_ERR,(__location__ " Failed to init event script monitoring\n"));
518 talloc_free(tmp_ctx);
523 if (ctdb->recovery_mode != CTDB_RECOVERY_NORMAL) {
524 /* we guarantee that only some specifically allowed event scripts are run
526 const char *allowed_scripts[] = {"startrecovery", "shutdown", "releaseip", "stopped" };
528 for (i=0;i<ARRAY_SIZE(allowed_scripts);i++) {
529 if (strncmp(options, allowed_scripts[i], strlen(allowed_scripts[i])) == 0) break;
531 if (i == ARRAY_SIZE(allowed_scripts)) {
532 DEBUG(DEBUG_ERR,("Refusing to run event scripts with option '%s' while in recovery\n",
534 talloc_free(tmp_ctx);
539 if (setpgid(0,0) != 0) {
540 DEBUG(DEBUG_ERR,("Failed to create process group for event scripts - %s\n",
542 talloc_free(tmp_ctx);
546 signal(SIGTERM, sigterm);
548 child_state.start = timeval_current();
549 child_state.script_running = "startup";
551 scripts = ctdb_get_script_list(ctdb, tmp_ctx);
553 /* fetch the scripts from the tree one by one and execute
556 for (current=scripts; current; current=current->next) {
557 /* we dont run disabled scripts, we just report they are disabled */
558 cmdstr = talloc_asprintf(tmp_ctx, "%s/%s %s",
559 ctdb->event_script_dir,
560 current->name, options);
561 CTDB_NO_MEMORY(ctdb, cmdstr);
563 DEBUG(DEBUG_INFO,("Executing event script %s\n",cmdstr));
565 child_state.start = timeval_current();
566 child_state.script_running = cmdstr;
568 if (is_monitor == 1) {
569 if (ctdb_ctrl_event_script_start(ctdb, current->name) != 0) {
570 DEBUG(DEBUG_ERR,(__location__ " Failed to start event script monitoring\n"));
571 talloc_free(tmp_ctx);
575 if (!current->is_enabled) {
576 if (ctdb_ctrl_event_script_disabled(ctdb, current->name) != 0) {
577 DEBUG(DEBUG_ERR,(__location__ " Failed to report disabled eventscript\n"));
578 talloc_free(tmp_ctx);
585 if (!current->is_enabled) {
589 ret = system(cmdstr);
590 /* if the system() call was successful, translate ret into the
591 return code from the command
594 ret = WEXITSTATUS(ret);
596 if (is_monitor == 1) {
597 if (ctdb_ctrl_event_script_stop(ctdb, ret) != 0) {
598 DEBUG(DEBUG_ERR,(__location__ " Failed to stop event script monitoring\n"));
599 talloc_free(tmp_ctx);
604 /* return an error if the script failed */
606 DEBUG(DEBUG_ERR,("Event script %s failed with error %d\n", cmdstr, ret));
607 if (is_monitor == 1) {
608 if (ctdb_ctrl_event_script_finished(ctdb) != 0) {
609 DEBUG(DEBUG_ERR,(__location__ " Failed to finish event script monitoring\n"));
610 talloc_free(tmp_ctx);
615 talloc_free(tmp_ctx);
620 child_state.start = timeval_current();
621 child_state.script_running = "finished";
623 if (is_monitor == 1) {
624 if (ctdb_ctrl_event_script_finished(ctdb) != 0) {
625 DEBUG(DEBUG_ERR,(__location__ " Failed to finish event script monitoring\n"));
626 talloc_free(tmp_ctx);
631 talloc_free(tmp_ctx);
635 /* called when child is finished */
636 static void ctdb_event_script_handler(struct event_context *ev, struct fd_event *fde,
637 uint16_t flags, void *p)
639 struct ctdb_event_script_state *state =
640 talloc_get_type(p, struct ctdb_event_script_state);
641 void (*callback)(struct ctdb_context *, int, void *) = state->callback;
642 void *private_data = state->private_data;
643 struct ctdb_context *ctdb = state->ctdb;
646 read(state->fd[0], &rt, sizeof(rt));
648 DEBUG(DEBUG_INFO,(__location__ " Eventscript %s finished with state %d\n", state->options, rt));
650 talloc_set_destructor(state, NULL);
652 callback(ctdb, rt, private_data);
654 ctdb->event_script_timeouts = 0;
657 static void ctdb_ban_self(struct ctdb_context *ctdb, uint32_t ban_period)
660 struct ctdb_ban_time bantime;
662 bantime.pnn = ctdb->pnn;
663 bantime.time = ban_period;
665 data.dsize = sizeof(bantime);
666 data.dptr = (uint8_t *)&bantime;
668 ctdb_control_set_ban_state(ctdb, data);
672 /* called when child times out */
673 static void ctdb_event_script_timeout(struct event_context *ev, struct timed_event *te,
674 struct timeval t, void *p)
676 struct ctdb_event_script_state *state = talloc_get_type(p, struct ctdb_event_script_state);
677 void (*callback)(struct ctdb_context *, int, void *) = state->callback;
678 void *private_data = state->private_data;
679 struct ctdb_context *ctdb = state->ctdb;
681 struct ctdb_monitoring_status *monitoring_status =
682 talloc_get_type(ctdb->script_monitoring_ctx,
683 struct ctdb_monitoring_status);
685 DEBUG(DEBUG_ERR,("Event script timed out : %s count : %u pid : %d\n", state->options, ctdb->event_script_timeouts, state->child));
686 if (kill(state->child, 0) != 0) {
687 DEBUG(DEBUG_ERR,("Event script child process already dead, errno %s(%d)\n", strerror(errno), errno));
688 callback(ctdb, 0, private_data);
690 talloc_set_destructor(state, NULL);
695 options = talloc_strdup(ctdb, state->options);
696 CTDB_NO_MEMORY_VOID(ctdb, options);
699 if (!strcmp(options, "monitor")) {
700 /* if it is a monitor event, we allow it to "hang" a few times
701 before we declare it a failure and ban ourself (and make
704 DEBUG(DEBUG_ERR, (__location__ " eventscript for monitor event timedout.\n"));
706 ctdb->event_script_timeouts++;
707 if (ctdb->event_script_timeouts > ctdb->tunable.script_ban_count) {
708 if (ctdb->tunable.script_unhealthy_on_timeout != 0) {
709 DEBUG(DEBUG_ERR, ("Maximum timeout count %u reached for eventscript. Making node unhealthy\n", ctdb->tunable.script_ban_count));
710 callback(ctdb, -ETIME, private_data);
712 ctdb->event_script_timeouts = 0;
713 DEBUG(DEBUG_ERR, ("Maximum timeout count %u reached for eventscript. Banning self for %d seconds\n", ctdb->tunable.script_ban_count, ctdb->tunable.recovery_ban_period));
714 ctdb_ban_self(ctdb, ctdb->tunable.recovery_ban_period);
715 callback(ctdb, -1, private_data);
718 callback(ctdb, 0, private_data);
720 } else if (!strcmp(options, "startup")) {
721 DEBUG(DEBUG_ERR, (__location__ " eventscript for startup event timedout.\n"));
722 callback(ctdb, -1, private_data);
724 /* if it is not a monitor event we ban ourself immediately */
725 DEBUG(DEBUG_ERR, (__location__ " eventscript for NON-monitor/NON-startup event timedout. Immediately banning ourself for %d seconds\n", ctdb->tunable.recovery_ban_period));
726 ctdb_ban_self(ctdb, ctdb->tunable.recovery_ban_period);
727 callback(ctdb, -1, private_data);
730 if (monitoring_status != NULL) {
731 struct ctdb_monitor_script_status *script;
733 script = monitoring_status->scripts;
734 if (script != NULL) {
735 script->timedout = 1;
737 monitoring_status->status = MONITOR_SCRIPT_TIMEOUT;
738 if (ctdb->last_monitoring_ctx) {
739 talloc_free(ctdb->last_monitoring_ctx);
740 ctdb->last_monitoring_ctx = ctdb->script_monitoring_ctx;
741 ctdb->script_monitoring_ctx = NULL;
745 talloc_free(options);
749 destroy a running event script
751 static int event_script_destructor(struct ctdb_event_script_state *state)
753 DEBUG(DEBUG_ERR,(__location__ " Sending SIGTERM to child pid:%d\n", state->child));
755 if (kill(state->child, SIGTERM) != 0) {
756 DEBUG(DEBUG_ERR,("Failed to kill child process for eventscript, errno %s(%d)\n", strerror(errno), errno));
763 run the event script in the background, calling the callback when
766 static int ctdb_event_script_callback_v(struct ctdb_context *ctdb,
767 struct timeval timeout,
769 void (*callback)(struct ctdb_context *, int, void *),
771 const char *fmt, va_list ap)
773 struct ctdb_event_script_state *state;
776 state = talloc(mem_ctx, struct ctdb_event_script_state);
777 CTDB_NO_MEMORY(ctdb, state);
780 state->callback = callback;
781 state->private_data = private_data;
782 state->options = talloc_vasprintf(state, fmt, ap);
783 CTDB_NO_MEMORY(ctdb, state->options);
785 DEBUG(DEBUG_INFO,(__location__ " Starting eventscript %s\n", state->options));
787 ret = pipe(state->fd);
793 state->child = fork();
795 if (state->child == (pid_t)-1) {
802 if (state->child == 0) {
806 set_close_on_exec(state->fd[1]);
808 rt = ctdb_event_script_v(ctdb, state->options);
809 while ((ret = write(state->fd[1], &rt, sizeof(rt))) != sizeof(rt)) {
815 talloc_set_destructor(state, event_script_destructor);
818 set_close_on_exec(state->fd[0]);
820 DEBUG(DEBUG_DEBUG, (__location__ " Created PIPE FD:%d to child eventscript process\n", state->fd[0]));
822 event_add_fd(ctdb->ev, state, state->fd[0], EVENT_FD_READ|EVENT_FD_AUTOCLOSE,
823 ctdb_event_script_handler, state);
825 if (!timeval_is_zero(&timeout)) {
826 event_add_timed(ctdb->ev, state, timeout, ctdb_event_script_timeout, state);
828 DEBUG(DEBUG_ERR, (__location__ " eventscript %s called with no timeout\n", state->options));
836 run the event script in the background, calling the callback when
839 int ctdb_event_script_callback(struct ctdb_context *ctdb,
840 struct timeval timeout,
842 void (*callback)(struct ctdb_context *, int, void *),
844 const char *fmt, ...)
850 ret = ctdb_event_script_callback_v(ctdb, timeout, mem_ctx, callback, private_data, fmt, ap);
857 struct callback_status {
863 called when ctdb_event_script() finishes
865 static void event_script_callback(struct ctdb_context *ctdb, int status, void *private_data)
867 struct callback_status *s = (struct callback_status *)private_data;
873 run the event script, waiting for it to complete. Used when the caller doesn't want to
874 continue till the event script has finished.
876 int ctdb_event_script(struct ctdb_context *ctdb, const char *fmt, ...)
880 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
881 struct callback_status status;
884 ret = ctdb_event_script_callback_v(ctdb,
885 timeval_current_ofs(ctdb->tunable.script_timeout, 0),
886 tmp_ctx, event_script_callback, &status, fmt, ap);
890 talloc_free(tmp_ctx);
897 while (status.done == false && event_loop_once(ctdb->ev) == 0) /* noop */;
899 talloc_free(tmp_ctx);
901 return status.status;
905 struct eventscript_callback_state {
906 struct ctdb_req_control *c;
910 called when takeip event finishes
912 static void run_eventscripts_callback(struct ctdb_context *ctdb, int status,
915 struct eventscript_callback_state *state =
916 talloc_get_type(private_data, struct eventscript_callback_state);
918 ctdb_enable_monitoring(ctdb);
921 DEBUG(DEBUG_ERR,(__location__ " Failed to forcibly run eventscripts\n"));
922 ctdb_request_control_reply(ctdb, state->c, NULL, status, NULL);
927 /* the control succeeded */
928 ctdb_request_control_reply(ctdb, state->c, NULL, 0, NULL);
934 A control to force running of the eventscripts from the ctdb client tool
936 int32_t ctdb_run_eventscripts(struct ctdb_context *ctdb,
937 struct ctdb_req_control *c,
938 TDB_DATA indata, bool *async_reply)
941 struct eventscript_callback_state *state;
943 /* kill off any previous invokations of forced eventscripts */
944 if (ctdb->eventscripts_ctx) {
945 talloc_free(ctdb->eventscripts_ctx);
947 ctdb->eventscripts_ctx = talloc_new(ctdb);
948 CTDB_NO_MEMORY(ctdb, ctdb->eventscripts_ctx);
950 state = talloc(ctdb->eventscripts_ctx, struct eventscript_callback_state);
951 CTDB_NO_MEMORY(ctdb, state);
953 state->c = talloc_steal(state, c);
955 DEBUG(DEBUG_NOTICE,("Forced running of eventscripts with arguments %s\n", indata.dptr));
957 if (ctdb->recovery_mode != CTDB_RECOVERY_NORMAL) {
958 DEBUG(DEBUG_ERR, (__location__ " Aborted running eventscript \"%s\" while in RECOVERY mode\n", indata.dptr));
962 ctdb_disable_monitoring(ctdb);
964 ret = ctdb_event_script_callback(ctdb,
965 timeval_current_ofs(ctdb->tunable.script_timeout, 0),
966 state, run_eventscripts_callback, state,
967 (const char *)indata.dptr);
970 ctdb_enable_monitoring(ctdb);
971 DEBUG(DEBUG_ERR,(__location__ " Failed to run eventscripts with arguments %s\n", indata.dptr));
976 /* tell ctdb_control.c that we will be replying asynchronously */
984 int32_t ctdb_control_enable_script(struct ctdb_context *ctdb, TDB_DATA indata)
989 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
991 script = (char *)indata.dptr;
992 if (indata.dsize == 0) {
993 DEBUG(DEBUG_ERR,(__location__ " No script specified.\n"));
994 talloc_free(tmp_ctx);
997 if (indata.dptr[indata.dsize - 1] != '\0') {
998 DEBUG(DEBUG_ERR,(__location__ " String is not null terminated.\n"));
999 talloc_free(tmp_ctx);
1002 if (index(script,'/') != NULL) {
1003 DEBUG(DEBUG_ERR,(__location__ " Script name contains '/'. Failed to enable script %s\n", script));
1004 talloc_free(tmp_ctx);
1009 if (stat(ctdb->event_script_dir, &st) != 0 &&
1011 DEBUG(DEBUG_CRIT,("No event script directory found at '%s'\n", ctdb->event_script_dir));
1012 talloc_free(tmp_ctx);
1017 filename = talloc_asprintf(tmp_ctx, "%s/%s", ctdb->event_script_dir, script);
1018 if (filename == NULL) {
1019 DEBUG(DEBUG_ERR,(__location__ " Failed to create script path\n"));
1020 talloc_free(tmp_ctx);
1024 if (stat(filename, &st) != 0) {
1025 DEBUG(DEBUG_ERR,("Could not stat event script %s. Failed to enable script.\n", filename));
1026 talloc_free(tmp_ctx);
1030 if (chmod(filename, st.st_mode | S_IXUSR) == -1) {
1031 DEBUG(DEBUG_ERR,("Could not chmod %s. Failed to enable script.\n", filename));
1032 talloc_free(tmp_ctx);
1036 talloc_free(tmp_ctx);
1040 int32_t ctdb_control_disable_script(struct ctdb_context *ctdb, TDB_DATA indata)
1045 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1047 script = (char *)indata.dptr;
1048 if (indata.dsize == 0) {
1049 DEBUG(DEBUG_ERR,(__location__ " No script specified.\n"));
1050 talloc_free(tmp_ctx);
1053 if (indata.dptr[indata.dsize - 1] != '\0') {
1054 DEBUG(DEBUG_ERR,(__location__ " String is not null terminated.\n"));
1055 talloc_free(tmp_ctx);
1058 if (index(script,'/') != NULL) {
1059 DEBUG(DEBUG_ERR,(__location__ " Script name contains '/'. Failed to disable script %s\n", script));
1060 talloc_free(tmp_ctx);
1065 if (stat(ctdb->event_script_dir, &st) != 0 &&
1067 DEBUG(DEBUG_CRIT,("No event script directory found at '%s'\n", ctdb->event_script_dir));
1068 talloc_free(tmp_ctx);
1073 filename = talloc_asprintf(tmp_ctx, "%s/%s", ctdb->event_script_dir, script);
1074 if (filename == NULL) {
1075 DEBUG(DEBUG_ERR,(__location__ " Failed to create script path\n"));
1076 talloc_free(tmp_ctx);
1080 if (stat(filename, &st) != 0) {
1081 DEBUG(DEBUG_ERR,("Could not stat event script %s. Failed to disable script.\n", filename));
1082 talloc_free(tmp_ctx);
1086 if (chmod(filename, st.st_mode & ~(S_IXUSR|S_IXGRP|S_IXOTH)) == -1) {
1087 DEBUG(DEBUG_ERR,("Could not chmod %s. Failed to disable script.\n", filename));
1088 talloc_free(tmp_ctx);
1092 talloc_free(tmp_ctx);