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 >/tmp/ctdb.event.%s.%d", tbuf, getpid());
54 DEBUG(DEBUG_ERR,("Logged timedout eventscript : %s\n", buf));
56 /* all the child processes will be running in the same process group */
57 kill(-getpgrp(), SIGKILL);
61 struct ctdb_event_script_state {
62 struct ctdb_context *ctdb;
64 /* Warning: this can free us! */
65 void (*callback)(struct ctdb_context *, int, void *);
70 struct timeval timeout;
74 struct ctdb_monitor_script_status {
75 struct ctdb_monitor_script_status *next;
78 struct timeval finished;
85 struct ctdb_monitor_script_status_ctx {
86 struct ctdb_monitor_script_status *scripts;
89 /* called from ctdb_logging when we have received output on STDERR from
90 * one of the eventscripts
92 int ctdb_log_event_script_output(struct ctdb_context *ctdb, char *str, uint16_t len)
94 struct ctdb_monitor_script_status *script;
96 if (ctdb->current_monitor_status_ctx == NULL) {
100 script = ctdb->current_monitor_status_ctx->scripts;
101 if (script == NULL) {
105 if (script->output == NULL) {
106 script->output = talloc_asprintf(script, "%*.*s", len, len, str);
108 script->output = talloc_asprintf_append(script->output, "%*.*s", len, len, str);
114 /* called from the event script child process when we are starting a new
117 int32_t ctdb_control_event_script_init(struct ctdb_context *ctdb)
119 DEBUG(DEBUG_INFO, ("event script init called\n"));
121 if (ctdb->current_monitor_status_ctx == NULL) {
122 DEBUG(DEBUG_ERR,(__location__ " current_monitor_status_ctx is NULL when initing script\n"));
130 /* called from the event script child process when we are star running
133 int32_t ctdb_control_event_script_start(struct ctdb_context *ctdb, TDB_DATA indata)
135 const char *name = (const char *)indata.dptr;
136 struct ctdb_monitor_script_status *script;
138 DEBUG(DEBUG_INFO, ("event script start called : %s\n", name));
140 if (ctdb->current_monitor_status_ctx == NULL) {
141 DEBUG(DEBUG_ERR,(__location__ " current_monitor_status_ctx is NULL when starting script\n"));
145 script = talloc_zero(ctdb->current_monitor_status_ctx, struct ctdb_monitor_script_status);
146 if (script == NULL) {
147 DEBUG(DEBUG_ERR,(__location__ " Failed to talloc ctdb_monitor_script_status for script %s\n", name));
151 script->next = ctdb->current_monitor_status_ctx->scripts;
152 script->name = talloc_strdup(script, name);
153 CTDB_NO_MEMORY(ctdb, script->name);
154 script->start = timeval_current();
155 ctdb->current_monitor_status_ctx->scripts = script;
160 /* called from the event script child process when we have finished running
163 int32_t ctdb_control_event_script_stop(struct ctdb_context *ctdb, TDB_DATA indata)
165 int32_t res = *((int32_t *)indata.dptr);
166 struct ctdb_monitor_script_status *script;
168 if (ctdb->current_monitor_status_ctx == NULL) {
169 DEBUG(DEBUG_ERR,(__location__ " current_monitor_status_ctx is NULL when script finished\n"));
173 script = ctdb->current_monitor_status_ctx->scripts;
174 if (script == NULL) {
175 DEBUG(DEBUG_ERR,(__location__ " script is NULL when the script had finished\n"));
179 script->finished = timeval_current();
180 script->status = res;
182 DEBUG(DEBUG_INFO, ("event script stop called for script:%s duration:%.1f status:%d\n", script->name, timeval_elapsed(&script->start), (int)res));
187 /* called from the event script child process when we have a disabled script
189 int32_t ctdb_control_event_script_disabled(struct ctdb_context *ctdb, TDB_DATA indata)
191 const char *name = (const char *)indata.dptr;
192 struct ctdb_monitor_script_status *script;
194 DEBUG(DEBUG_INFO, ("event script disabed called for script %s\n", name));
196 if (ctdb->current_monitor_status_ctx == NULL) {
197 DEBUG(DEBUG_ERR,(__location__ " current_monitor_status_ctx is NULL when script finished\n"));
201 script = ctdb->current_monitor_status_ctx->scripts;
202 if (script == NULL) {
203 DEBUG(DEBUG_ERR,(__location__ " script is NULL when the script had finished\n"));
207 script->finished = timeval_current();
209 script->disabled = 1;
214 /* called from the event script child process when we have completed a
217 int32_t ctdb_control_event_script_finished(struct ctdb_context *ctdb)
219 DEBUG(DEBUG_INFO, ("event script finished called\n"));
221 if (ctdb->current_monitor_status_ctx == NULL) {
222 DEBUG(DEBUG_ERR,(__location__ " script_status is NULL when monitoring event finished\n"));
226 if (ctdb->last_monitor_status_ctx) {
227 talloc_free(ctdb->last_monitor_status_ctx);
228 ctdb->last_monitor_status_ctx = NULL;
230 ctdb->last_monitor_status_ctx = talloc_steal(ctdb, ctdb->current_monitor_status_ctx);
231 ctdb->current_monitor_status_ctx = NULL;
236 static struct ctdb_monitoring_wire *marshall_monitoring_scripts(TALLOC_CTX *mem_ctx, struct ctdb_monitoring_wire *monitoring_scripts, struct ctdb_monitor_script_status *script)
238 struct ctdb_monitoring_script_wire script_wire;
241 if (script == NULL) {
242 return monitoring_scripts;
244 monitoring_scripts = marshall_monitoring_scripts(mem_ctx, monitoring_scripts, script->next);
245 if (monitoring_scripts == NULL) {
249 bzero(&script_wire, sizeof(struct ctdb_monitoring_script_wire));
250 strncpy(script_wire.name, script->name, MAX_SCRIPT_NAME);
251 script_wire.start = script->start;
252 script_wire.finished = script->finished;
253 script_wire.disabled = script->disabled;
254 script_wire.status = script->status;
255 script_wire.timedout = script->timedout;
256 if (script->output != NULL) {
257 strncpy(script_wire.output, script->output, MAX_SCRIPT_OUTPUT);
260 size = talloc_get_size(monitoring_scripts);
261 monitoring_scripts = talloc_realloc_size(mem_ctx, monitoring_scripts, size + sizeof(struct ctdb_monitoring_script_wire));
262 if (monitoring_scripts == NULL) {
263 DEBUG(DEBUG_ERR,(__location__ " Failed to talloc_resize monitoring_scripts blob\n"));
267 memcpy(&monitoring_scripts->scripts[monitoring_scripts->num_scripts], &script_wire, sizeof(script_wire));
268 monitoring_scripts->num_scripts++;
270 return monitoring_scripts;
273 int32_t ctdb_control_get_event_script_status(struct ctdb_context *ctdb, TDB_DATA *outdata)
275 struct ctdb_monitor_script_status_ctx *script_status = talloc_get_type(ctdb->last_monitor_status_ctx, struct ctdb_monitor_script_status_ctx);
276 struct ctdb_monitoring_wire *monitoring_scripts;
278 if (script_status == NULL) {
279 DEBUG(DEBUG_ERR,(__location__ " last_monitor_status_ctx is NULL when reading status\n"));
283 monitoring_scripts = talloc_size(outdata, offsetof(struct ctdb_monitoring_wire, scripts));
284 if (monitoring_scripts == NULL) {
285 DEBUG(DEBUG_ERR,(__location__ " failed to talloc monitoring_scripts structure\n"));
289 monitoring_scripts->num_scripts = 0;
290 monitoring_scripts = marshall_monitoring_scripts(outdata, monitoring_scripts, script_status->scripts);
291 if (monitoring_scripts == NULL) {
292 DEBUG(DEBUG_ERR,(__location__ " Monitoring scritps is NULL. can not return data to client\n"));
296 outdata->dsize = talloc_get_size(monitoring_scripts);
297 outdata->dptr = (uint8_t *)monitoring_scripts;
302 struct ctdb_script_tree_item {
307 struct ctdb_script_list {
308 struct ctdb_script_list *next;
313 static struct ctdb_script_list *ctdb_get_script_list(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx)
319 struct ctdb_script_list *head, *tail, *new_item;
320 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
321 struct ctdb_script_tree_item *tree_item;
325 the service specific event scripts
327 if (stat(ctdb->event_script_dir, &st) != 0 &&
329 DEBUG(DEBUG_CRIT,("No event script directory found at '%s'\n", ctdb->event_script_dir));
330 talloc_free(tmp_ctx);
334 /* create a tree to store all the script names in */
335 tree = trbt_create(tmp_ctx, 0);
337 /* scan all directory entries and insert all valid scripts into the
340 dir = opendir(ctdb->event_script_dir);
342 DEBUG(DEBUG_CRIT,("Failed to open event script directory '%s'\n", ctdb->event_script_dir));
343 talloc_free(tmp_ctx);
348 while ((de=readdir(dir)) != NULL) {
353 namlen = strlen(de->d_name);
359 if (de->d_name[namlen-1] == '~') {
360 /* skip files emacs left behind */
364 if (de->d_name[2] != '.') {
368 if (sscanf(de->d_name, "%02u.", &num) != 1) {
372 /* Make sure the event script is executable */
373 str = talloc_asprintf(tree, "%s/%s", ctdb->event_script_dir, de->d_name);
374 if (stat(str, &st) != 0) {
375 DEBUG(DEBUG_ERR,("Could not stat event script %s. Ignoring this event script\n", str));
380 tree_item = talloc(tree, struct ctdb_script_tree_item);
381 if (tree_item == NULL) {
382 DEBUG(DEBUG_ERR, (__location__ " Failed to allocate new tree item\n"));
383 talloc_free(tmp_ctx);
387 tree_item->is_enabled = 1;
388 if (!(st.st_mode & S_IXUSR)) {
389 DEBUG(DEBUG_INFO,("Event script %s is not executable. Ignoring this event script\n", str));
390 tree_item->is_enabled = 0;
393 tree_item->name = talloc_strdup(tree_item, de->d_name);
394 if (tree_item->name == NULL) {
395 DEBUG(DEBUG_ERR,(__location__ " Failed to allocate script name.\n"));
396 talloc_free(tmp_ctx);
400 /* store the event script in the tree */
401 trbt_insert32(tree, (num<<16)|count++, tree_item);
409 /* fetch the scripts from the tree one by one and add them to the linked
412 while ((tree_item=trbt_findfirstarray32(tree, 1)) != NULL) {
414 new_item = talloc(tmp_ctx, struct ctdb_script_list);
415 if (new_item == NULL) {
416 DEBUG(DEBUG_ERR, (__location__ " Failed to allocate new list item\n"));
417 talloc_free(tmp_ctx);
421 new_item->next = NULL;
422 new_item->name = talloc_steal(new_item, tree_item->name);
423 new_item->is_enabled = tree_item->is_enabled;
429 tail->next = new_item;
433 talloc_steal(mem_ctx, new_item);
435 /* remove this script from the tree */
436 talloc_free(tree_item);
439 talloc_free(tmp_ctx);
446 Actually run the event script
447 this function is called and run in the context of a forked child
448 which allows it to do blocking calls such as system()
450 static int ctdb_run_event_script(struct ctdb_context *ctdb, const char *options)
454 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
455 struct ctdb_script_list *scripts, *current;
458 if (!strcmp(options, "monitor")) {
462 if (is_monitor == 1) {
463 /* This is running in the forked child process. At this stage
464 * we want to switch from being a ctdb daemon into being a
465 * client and connect to the real local daemon.
467 if (switch_from_server_to_client(ctdb) != 0) {
468 DEBUG(DEBUG_CRIT, (__location__ "ERROR: failed to switch eventscript child into client mode. shutting down.\n"));
472 if (ctdb_ctrl_event_script_init(ctdb) != 0) {
473 DEBUG(DEBUG_ERR,(__location__ " Failed to init event script monitoring\n"));
474 talloc_free(tmp_ctx);
479 if (ctdb->recovery_mode != CTDB_RECOVERY_NORMAL) {
480 /* we guarantee that only some specifically allowed event scripts are run
482 const char *allowed_scripts[] = {"startrecovery", "shutdown", "releaseip", "stopped" };
484 for (i=0;i<ARRAY_SIZE(allowed_scripts);i++) {
485 if (strncmp(options, allowed_scripts[i], strlen(allowed_scripts[i])) == 0) break;
487 if (i == ARRAY_SIZE(allowed_scripts)) {
488 DEBUG(DEBUG_ERR,("Refusing to run event scripts with option '%s' while in recovery\n",
490 talloc_free(tmp_ctx);
495 if (setpgid(0,0) != 0) {
496 DEBUG(DEBUG_ERR,("Failed to create process group for event scripts - %s\n",
498 talloc_free(tmp_ctx);
502 signal(SIGTERM, sigterm);
504 child_state.start = timeval_current();
505 child_state.script_running = "startup";
507 scripts = ctdb_get_script_list(ctdb, tmp_ctx);
509 /* fetch the scripts from the tree one by one and execute
512 for (current=scripts; current; current=current->next) {
513 /* Allow a setting where we run the actual monitor event
514 from an external source and replace it with
515 a "status" event that just picks up the actual
516 status of the event asynchronously.
518 if ((ctdb->tunable.use_status_events_for_monitoring != 0)
519 && (!strcmp(options, "monitor"))) {
520 cmdstr = talloc_asprintf(tmp_ctx, "%s/%s %s",
521 ctdb->event_script_dir,
522 current->name, "status");
524 cmdstr = talloc_asprintf(tmp_ctx, "%s/%s %s",
525 ctdb->event_script_dir,
526 current->name, options);
528 CTDB_NO_MEMORY(ctdb, cmdstr);
530 DEBUG(DEBUG_INFO,("Executing event script %s\n",cmdstr));
532 child_state.start = timeval_current();
533 child_state.script_running = cmdstr;
535 if (is_monitor == 1) {
536 if (ctdb_ctrl_event_script_start(ctdb, current->name) != 0) {
537 DEBUG(DEBUG_ERR,(__location__ " Failed to start event script monitoring\n"));
538 talloc_free(tmp_ctx);
542 if (!current->is_enabled) {
543 if (ctdb_ctrl_event_script_disabled(ctdb, current->name) != 0) {
544 DEBUG(DEBUG_ERR,(__location__ " Failed to report disabled eventscript\n"));
545 talloc_free(tmp_ctx);
552 if (!current->is_enabled) {
556 ret = system(cmdstr);
557 /* if the system() call was successful, translate ret into the
558 return code from the command
561 ret = WEXITSTATUS(ret);
565 DEBUG(DEBUG_ERR,("Script %s returned status 127. Someone just deleted it?\n", cmdstr));
568 if (is_monitor == 1) {
569 if (ctdb_ctrl_event_script_stop(ctdb, ret) != 0) {
570 DEBUG(DEBUG_ERR,(__location__ " Failed to stop event script monitoring\n"));
571 talloc_free(tmp_ctx);
576 /* return an error if the script failed */
578 DEBUG(DEBUG_ERR,("Event script %s failed with error %d\n", cmdstr, ret));
579 if (is_monitor == 1) {
580 if (ctdb_ctrl_event_script_finished(ctdb) != 0) {
581 DEBUG(DEBUG_ERR,(__location__ " Failed to finish event script monitoring\n"));
582 talloc_free(tmp_ctx);
587 talloc_free(tmp_ctx);
592 child_state.start = timeval_current();
593 child_state.script_running = "finished";
595 if (is_monitor == 1) {
596 if (ctdb_ctrl_event_script_finished(ctdb) != 0) {
597 DEBUG(DEBUG_ERR,(__location__ " Failed to finish event script monitoring\n"));
598 talloc_free(tmp_ctx);
603 talloc_free(tmp_ctx);
607 /* called when child is finished */
608 static void ctdb_event_script_handler(struct event_context *ev, struct fd_event *fde,
609 uint16_t flags, void *p)
611 struct ctdb_event_script_state *state =
612 talloc_get_type(p, struct ctdb_event_script_state);
613 struct ctdb_context *ctdb = state->ctdb;
615 if (read(state->fd[0], &state->cb_status, sizeof(state->cb_status)) !=
616 sizeof(state->cb_status)) {
617 state->cb_status = -2;
620 DEBUG(DEBUG_INFO,(__location__ " Eventscript %s finished with state %d\n", state->options, state->cb_status));
623 ctdb->event_script_timeouts = 0;
627 static void ctdb_ban_self(struct ctdb_context *ctdb, uint32_t ban_period)
630 struct ctdb_ban_time bantime;
632 bantime.pnn = ctdb->pnn;
633 bantime.time = ban_period;
635 data.dsize = sizeof(bantime);
636 data.dptr = (uint8_t *)&bantime;
638 ctdb_control_set_ban_state(ctdb, data);
642 /* called when child times out */
643 static void ctdb_event_script_timeout(struct event_context *ev, struct timed_event *te,
644 struct timeval t, void *p)
646 struct ctdb_event_script_state *state = talloc_get_type(p, struct ctdb_event_script_state);
647 struct ctdb_context *ctdb = state->ctdb;
649 DEBUG(DEBUG_ERR,("Event script timed out : %s count : %u pid : %d\n", state->options, ctdb->event_script_timeouts, state->child));
651 if (kill(state->child, 0) != 0) {
652 DEBUG(DEBUG_ERR,("Event script child process already dead, errno %s(%d)\n", strerror(errno), errno));
658 if (!strcmp(state->options, "monitor")) {
659 /* if it is a monitor event, we allow it to "hang" a few times
660 before we declare it a failure and ban ourself (and make
663 DEBUG(DEBUG_ERR, (__location__ " eventscript for monitor event timedout.\n"));
665 ctdb->event_script_timeouts++;
667 if (ctdb->event_script_timeouts > ctdb->tunable.script_ban_count) {
668 DEBUG(DEBUG_ERR, ("Maximum timeout count %u reached for eventscript. Making node unhealthy\n", ctdb->tunable.script_ban_count));
669 state->cb_status = -ETIME;
671 state->cb_status = 0;
673 } else if (!strcmp(state->options, "startup")) {
674 DEBUG(DEBUG_ERR, (__location__ " eventscript for startup event timedout.\n"));
675 state->cb_status = -1;
677 /* if it is not a monitor or a startup event we ban ourself
680 DEBUG(DEBUG_ERR, (__location__ " eventscript for NON-monitor/NON-startup event timedout. Immediately banning ourself for %d seconds\n", ctdb->tunable.recovery_ban_period));
682 ctdb_ban_self(ctdb, ctdb->tunable.recovery_ban_period);
684 state->cb_status = -1;
687 if (!strcmp(state->options, "monitor") || !strcmp(state->options, "status")) {
688 struct ctdb_monitor_script_status *script;
690 if (ctdb->current_monitor_status_ctx == NULL) {
695 script = ctdb->current_monitor_status_ctx->scripts;
696 if (script != NULL) {
697 script->timedout = 1;
700 if (ctdb->last_monitor_status_ctx) {
701 talloc_free(ctdb->last_monitor_status_ctx);
702 ctdb->last_monitor_status_ctx = NULL;
704 ctdb->last_monitor_status_ctx = talloc_steal(ctdb, ctdb->current_monitor_status_ctx);
705 ctdb->current_monitor_status_ctx = NULL;
712 destroy an event script: kill it if ->child != 0.
714 static int event_script_destructor(struct ctdb_event_script_state *state)
717 DEBUG(DEBUG_ERR,(__location__ " Sending SIGTERM to child pid:%d\n", state->child));
719 if (kill(state->child, SIGTERM) != 0) {
720 DEBUG(DEBUG_ERR,("Failed to kill child process for eventscript, errno %s(%d)\n", strerror(errno), errno));
724 /* This is allowed to free us; talloc will prevent double free anyway,
725 * but beware if you call this outside the destructor! */
726 if (state->callback) {
727 state->callback(state->ctdb, state->cb_status, state->private_data);
734 run the event script in the background, calling the callback when
737 static int ctdb_event_script_callback_v(struct ctdb_context *ctdb,
738 struct timeval timeout,
739 void (*callback)(struct ctdb_context *, int, void *),
741 const char *fmt, va_list ap)
744 struct ctdb_event_script_state *state;
747 if (!strcmp(fmt, "monitor") || !strcmp(fmt, "status")) {
748 /* if this was a "monitor" or a status event, we recycle the
749 context to start a new monitor event
751 if (ctdb->monitor_event_script_ctx != NULL) {
752 talloc_free(ctdb->monitor_event_script_ctx);
753 ctdb->monitor_event_script_ctx = NULL;
755 ctdb->monitor_event_script_ctx = talloc_new(ctdb);
756 mem_ctx = ctdb->monitor_event_script_ctx;
758 if (ctdb->current_monitor_status_ctx != NULL) {
759 talloc_free(ctdb->current_monitor_status_ctx);
760 ctdb->current_monitor_status_ctx = NULL;
763 ctdb->current_monitor_status_ctx = talloc(ctdb->monitor_event_script_ctx, struct ctdb_monitor_script_status_ctx);
764 ctdb->current_monitor_status_ctx->scripts = NULL;
766 /* any other script will first terminate any monitor event */
767 if (ctdb->monitor_event_script_ctx != NULL) {
768 talloc_free(ctdb->monitor_event_script_ctx);
769 ctdb->monitor_event_script_ctx = NULL;
771 /* and then use a context common for all non-monitor events */
772 if (ctdb->other_event_script_ctx != NULL) {
773 talloc_free(ctdb->other_event_script_ctx);
774 ctdb->other_event_script_ctx = NULL;
776 ctdb->other_event_script_ctx = talloc_new(ctdb);
777 mem_ctx = ctdb->other_event_script_ctx;
780 state = talloc(mem_ctx, struct ctdb_event_script_state);
781 CTDB_NO_MEMORY(ctdb, state);
784 state->callback = callback;
785 state->private_data = private_data;
786 state->options = talloc_vasprintf(state, fmt, ap);
787 state->timeout = timeout;
788 if (state->options == NULL) {
789 DEBUG(DEBUG_ERR, (__location__ " could not allocate state->options\n"));
794 DEBUG(DEBUG_INFO,(__location__ " Starting eventscript %s\n", state->options));
796 ret = pipe(state->fd);
802 state->child = fork();
804 if (state->child == (pid_t)-1) {
811 if (state->child == 0) {
815 set_close_on_exec(state->fd[1]);
817 rt = ctdb_run_event_script(ctdb, state->options);
818 /* We must be able to write PIPEBUF bytes at least; if this
819 somehow fails, the read above will be short. */
820 write(state->fd[1], &rt, sizeof(rt));
826 set_close_on_exec(state->fd[0]);
827 talloc_set_destructor(state, event_script_destructor);
829 DEBUG(DEBUG_DEBUG, (__location__ " Created PIPE FD:%d to child eventscript process\n", state->fd[0]));
831 event_add_fd(ctdb->ev, state, state->fd[0], EVENT_FD_READ|EVENT_FD_AUTOCLOSE,
832 ctdb_event_script_handler, state);
834 if (!timeval_is_zero(&state->timeout)) {
835 event_add_timed(ctdb->ev, state, timeval_current_ofs(state->timeout.tv_sec, state->timeout.tv_usec), ctdb_event_script_timeout, state);
837 DEBUG(DEBUG_ERR, (__location__ " eventscript %s called with no timeout\n", state->options));
845 run the event script in the background, calling the callback when
848 int ctdb_event_script_callback(struct ctdb_context *ctdb,
849 struct timeval timeout,
851 void (*callback)(struct ctdb_context *, int, void *),
853 const char *fmt, ...)
859 ret = ctdb_event_script_callback_v(ctdb, timeout, callback, private_data, fmt, ap);
866 struct callback_status {
872 called when ctdb_event_script() finishes
874 static void event_script_callback(struct ctdb_context *ctdb, int status, void *private_data)
876 struct callback_status *s = (struct callback_status *)private_data;
882 run the event script, waiting for it to complete. Used when the caller
883 doesn't want to continue till the event script has finished.
885 int ctdb_event_script(struct ctdb_context *ctdb, const char *fmt, ...)
889 struct callback_status status;
892 ret = ctdb_event_script_callback_v(ctdb,
893 timeval_set(ctdb->tunable.script_timeout, 0),
894 event_script_callback, &status, fmt, ap);
904 while (status.done == false && event_loop_once(ctdb->ev) == 0) /* noop */;
906 return status.status;
910 struct eventscript_callback_state {
911 struct ctdb_req_control *c;
915 called when a forced eventscript finishes
917 static void run_eventscripts_callback(struct ctdb_context *ctdb, int status,
920 struct eventscript_callback_state *state =
921 talloc_get_type(private_data, struct eventscript_callback_state);
923 ctdb_enable_monitoring(ctdb);
926 DEBUG(DEBUG_ERR,(__location__ " Failed to forcibly run eventscripts\n"));
929 ctdb_request_control_reply(ctdb, state->c, NULL, status, NULL);
930 /* This will free the struct ctdb_event_script_state we are in! */
937 A control to force running of the eventscripts from the ctdb client tool
939 int32_t ctdb_run_eventscripts(struct ctdb_context *ctdb,
940 struct ctdb_req_control *c,
941 TDB_DATA indata, bool *async_reply)
944 struct eventscript_callback_state *state;
946 if (ctdb->recovery_mode != CTDB_RECOVERY_NORMAL) {
947 DEBUG(DEBUG_ERR, (__location__ " Aborted running eventscript \"%s\" while in RECOVERY mode\n", indata.dptr));
951 state = talloc(ctdb->other_event_script_ctx, struct eventscript_callback_state);
952 CTDB_NO_MEMORY(ctdb, state);
954 state->c = talloc_steal(state, c);
956 DEBUG(DEBUG_NOTICE,("Forced running of eventscripts with arguments %s\n", indata.dptr));
958 ctdb_disable_monitoring(ctdb);
960 ret = ctdb_event_script_callback(ctdb,
961 timeval_set(ctdb->tunable.script_timeout, 0),
962 state, run_eventscripts_callback, state,
963 "%s", (const char *)indata.dptr);
966 ctdb_enable_monitoring(ctdb);
967 DEBUG(DEBUG_ERR,(__location__ " Failed to run eventscripts with arguments %s\n", indata.dptr));
972 /* tell ctdb_control.c that we will be replying asynchronously */
980 int32_t ctdb_control_enable_script(struct ctdb_context *ctdb, TDB_DATA indata)
985 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
987 script = (char *)indata.dptr;
988 if (indata.dsize == 0) {
989 DEBUG(DEBUG_ERR,(__location__ " No script specified.\n"));
990 talloc_free(tmp_ctx);
993 if (indata.dptr[indata.dsize - 1] != '\0') {
994 DEBUG(DEBUG_ERR,(__location__ " String is not null terminated.\n"));
995 talloc_free(tmp_ctx);
998 if (index(script,'/') != NULL) {
999 DEBUG(DEBUG_ERR,(__location__ " Script name contains '/'. Failed to enable script %s\n", script));
1000 talloc_free(tmp_ctx);
1005 if (stat(ctdb->event_script_dir, &st) != 0 &&
1007 DEBUG(DEBUG_CRIT,("No event script directory found at '%s'\n", ctdb->event_script_dir));
1008 talloc_free(tmp_ctx);
1013 filename = talloc_asprintf(tmp_ctx, "%s/%s", ctdb->event_script_dir, script);
1014 if (filename == NULL) {
1015 DEBUG(DEBUG_ERR,(__location__ " Failed to create script path\n"));
1016 talloc_free(tmp_ctx);
1020 if (stat(filename, &st) != 0) {
1021 DEBUG(DEBUG_ERR,("Could not stat event script %s. Failed to enable script.\n", filename));
1022 talloc_free(tmp_ctx);
1026 if (chmod(filename, st.st_mode | S_IXUSR) == -1) {
1027 DEBUG(DEBUG_ERR,("Could not chmod %s. Failed to enable script.\n", filename));
1028 talloc_free(tmp_ctx);
1032 talloc_free(tmp_ctx);
1036 int32_t ctdb_control_disable_script(struct ctdb_context *ctdb, TDB_DATA indata)
1041 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1043 script = (char *)indata.dptr;
1044 if (indata.dsize == 0) {
1045 DEBUG(DEBUG_ERR,(__location__ " No script specified.\n"));
1046 talloc_free(tmp_ctx);
1049 if (indata.dptr[indata.dsize - 1] != '\0') {
1050 DEBUG(DEBUG_ERR,(__location__ " String is not null terminated.\n"));
1051 talloc_free(tmp_ctx);
1054 if (index(script,'/') != NULL) {
1055 DEBUG(DEBUG_ERR,(__location__ " Script name contains '/'. Failed to disable script %s\n", script));
1056 talloc_free(tmp_ctx);
1061 if (stat(ctdb->event_script_dir, &st) != 0 &&
1063 DEBUG(DEBUG_CRIT,("No event script directory found at '%s'\n", ctdb->event_script_dir));
1064 talloc_free(tmp_ctx);
1069 filename = talloc_asprintf(tmp_ctx, "%s/%s", ctdb->event_script_dir, script);
1070 if (filename == NULL) {
1071 DEBUG(DEBUG_ERR,(__location__ " Failed to create script path\n"));
1072 talloc_free(tmp_ctx);
1076 if (stat(filename, &st) != 0) {
1077 DEBUG(DEBUG_ERR,("Could not stat event script %s. Failed to disable script.\n", filename));
1078 talloc_free(tmp_ctx);
1082 if (chmod(filename, st.st_mode & ~(S_IXUSR|S_IXGRP|S_IXOTH)) == -1) {
1083 DEBUG(DEBUG_ERR,("Could not chmod %s. Failed to disable script.\n", filename));
1084 talloc_free(tmp_ctx);
1088 talloc_free(tmp_ctx);