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 const char *call_names[] = {
48 static void ctdb_event_script_timeout(struct event_context *ev, struct timed_event *te, struct timeval t, void *p);
51 ctdbd sends us a SIGTERM when we should time out the current script
53 static void sigterm(int sig)
55 char tbuf[100], buf[200];
58 DEBUG(DEBUG_ERR,("Timed out running script '%s' after %.1f seconds pid :%d\n",
59 child_state.script_running, timeval_elapsed(&child_state.start), getpid()));
63 strftime(tbuf, sizeof(tbuf)-1, "%Y%m%d%H%M%S", localtime(&t));
64 sprintf(buf, "pstree -p >/tmp/ctdb.event.%s.%d", tbuf, getpid());
67 DEBUG(DEBUG_ERR,("Logged timedout eventscript : %s\n", buf));
69 /* all the child processes will be running in the same process group */
70 kill(-getpgrp(), SIGKILL);
74 struct ctdb_event_script_state {
75 struct ctdb_context *ctdb;
77 /* Warning: this can free us! */
78 void (*callback)(struct ctdb_context *, int, void *);
82 enum ctdb_eventscript_call call;
84 struct timeval timeout;
88 struct ctdb_monitor_script_status {
89 struct ctdb_monitor_script_status *next;
92 struct timeval finished;
99 struct ctdb_monitor_script_status_ctx {
100 struct ctdb_monitor_script_status *scripts;
103 /* called from ctdb_logging when we have received output on STDERR from
104 * one of the eventscripts
106 int ctdb_log_event_script_output(struct ctdb_context *ctdb, char *str, uint16_t len)
108 struct ctdb_monitor_script_status *script;
110 if (ctdb->current_monitor_status_ctx == NULL) {
114 script = ctdb->current_monitor_status_ctx->scripts;
115 if (script == NULL) {
119 if (script->output == NULL) {
120 script->output = talloc_asprintf(script, "%*.*s", len, len, str);
122 script->output = talloc_asprintf_append(script->output, "%*.*s", len, len, str);
128 /* called from the event script child process when we are starting a new
131 int32_t ctdb_control_event_script_init(struct ctdb_context *ctdb)
133 DEBUG(DEBUG_INFO, ("event script init called\n"));
135 if (ctdb->current_monitor_status_ctx == NULL) {
136 DEBUG(DEBUG_ERR,(__location__ " current_monitor_status_ctx is NULL when initing script\n"));
144 /* called from the event script child process when we are star running
147 int32_t ctdb_control_event_script_start(struct ctdb_context *ctdb, TDB_DATA indata)
149 const char *name = (const char *)indata.dptr;
150 struct ctdb_monitor_script_status *script;
152 DEBUG(DEBUG_INFO, ("event script start called : %s\n", name));
154 if (ctdb->current_monitor_status_ctx == NULL) {
155 DEBUG(DEBUG_ERR,(__location__ " current_monitor_status_ctx is NULL when starting script\n"));
159 script = talloc_zero(ctdb->current_monitor_status_ctx, struct ctdb_monitor_script_status);
160 if (script == NULL) {
161 DEBUG(DEBUG_ERR,(__location__ " Failed to talloc ctdb_monitor_script_status for script %s\n", name));
165 script->next = ctdb->current_monitor_status_ctx->scripts;
166 script->name = talloc_strdup(script, name);
167 CTDB_NO_MEMORY(ctdb, script->name);
168 script->start = timeval_current();
169 ctdb->current_monitor_status_ctx->scripts = script;
174 /* called from the event script child process when we have finished running
177 int32_t ctdb_control_event_script_stop(struct ctdb_context *ctdb, TDB_DATA indata)
179 int32_t res = *((int32_t *)indata.dptr);
180 struct ctdb_monitor_script_status *script;
182 if (ctdb->current_monitor_status_ctx == NULL) {
183 DEBUG(DEBUG_ERR,(__location__ " current_monitor_status_ctx is NULL when script finished\n"));
187 script = ctdb->current_monitor_status_ctx->scripts;
188 if (script == NULL) {
189 DEBUG(DEBUG_ERR,(__location__ " script is NULL when the script had finished\n"));
193 script->finished = timeval_current();
194 script->status = res;
196 DEBUG(DEBUG_INFO, ("event script stop called for script:%s duration:%.1f status:%d\n", script->name, timeval_elapsed(&script->start), (int)res));
201 /* called from the event script child process when we have a disabled script
203 int32_t ctdb_control_event_script_disabled(struct ctdb_context *ctdb, TDB_DATA indata)
205 const char *name = (const char *)indata.dptr;
206 struct ctdb_monitor_script_status *script;
208 DEBUG(DEBUG_INFO, ("event script disabed called for script %s\n", name));
210 if (ctdb->current_monitor_status_ctx == NULL) {
211 DEBUG(DEBUG_ERR,(__location__ " current_monitor_status_ctx is NULL when script finished\n"));
215 script = ctdb->current_monitor_status_ctx->scripts;
216 if (script == NULL) {
217 DEBUG(DEBUG_ERR,(__location__ " script is NULL when the script had finished\n"));
221 script->finished = timeval_current();
223 script->disabled = 1;
228 /* called from the event script child process when we have completed a
231 int32_t ctdb_control_event_script_finished(struct ctdb_context *ctdb)
233 DEBUG(DEBUG_INFO, ("event script finished called\n"));
235 if (ctdb->current_monitor_status_ctx == NULL) {
236 DEBUG(DEBUG_ERR,(__location__ " script_status is NULL when monitoring event finished\n"));
240 if (ctdb->last_monitor_status_ctx) {
241 talloc_free(ctdb->last_monitor_status_ctx);
242 ctdb->last_monitor_status_ctx = NULL;
244 ctdb->last_monitor_status_ctx = talloc_steal(ctdb, ctdb->current_monitor_status_ctx);
245 ctdb->current_monitor_status_ctx = NULL;
250 static struct ctdb_monitoring_wire *marshall_monitoring_scripts(TALLOC_CTX *mem_ctx, struct ctdb_monitoring_wire *monitoring_scripts, struct ctdb_monitor_script_status *script)
252 struct ctdb_monitoring_script_wire script_wire;
255 if (script == NULL) {
256 return monitoring_scripts;
258 monitoring_scripts = marshall_monitoring_scripts(mem_ctx, monitoring_scripts, script->next);
259 if (monitoring_scripts == NULL) {
263 bzero(&script_wire, sizeof(struct ctdb_monitoring_script_wire));
264 strncpy(script_wire.name, script->name, MAX_SCRIPT_NAME);
265 script_wire.start = script->start;
266 script_wire.finished = script->finished;
267 script_wire.disabled = script->disabled;
268 script_wire.status = script->status;
269 script_wire.timedout = script->timedout;
270 if (script->output != NULL) {
271 strncpy(script_wire.output, script->output, MAX_SCRIPT_OUTPUT);
274 size = talloc_get_size(monitoring_scripts);
275 monitoring_scripts = talloc_realloc_size(mem_ctx, monitoring_scripts, size + sizeof(struct ctdb_monitoring_script_wire));
276 if (monitoring_scripts == NULL) {
277 DEBUG(DEBUG_ERR,(__location__ " Failed to talloc_resize monitoring_scripts blob\n"));
281 memcpy(&monitoring_scripts->scripts[monitoring_scripts->num_scripts], &script_wire, sizeof(script_wire));
282 monitoring_scripts->num_scripts++;
284 return monitoring_scripts;
287 int32_t ctdb_control_get_event_script_status(struct ctdb_context *ctdb, TDB_DATA *outdata)
289 struct ctdb_monitor_script_status_ctx *script_status = talloc_get_type(ctdb->last_monitor_status_ctx, struct ctdb_monitor_script_status_ctx);
290 struct ctdb_monitoring_wire *monitoring_scripts;
292 if (script_status == NULL) {
293 DEBUG(DEBUG_ERR,(__location__ " last_monitor_status_ctx is NULL when reading status\n"));
297 monitoring_scripts = talloc_size(outdata, offsetof(struct ctdb_monitoring_wire, scripts));
298 if (monitoring_scripts == NULL) {
299 DEBUG(DEBUG_ERR,(__location__ " failed to talloc monitoring_scripts structure\n"));
303 monitoring_scripts->num_scripts = 0;
304 monitoring_scripts = marshall_monitoring_scripts(outdata, monitoring_scripts, script_status->scripts);
305 if (monitoring_scripts == NULL) {
306 DEBUG(DEBUG_ERR,(__location__ " Monitoring scritps is NULL. can not return data to client\n"));
310 outdata->dsize = talloc_get_size(monitoring_scripts);
311 outdata->dptr = (uint8_t *)monitoring_scripts;
316 struct ctdb_script_tree_item {
321 struct ctdb_script_list {
322 struct ctdb_script_list *next;
327 static struct ctdb_script_list *ctdb_get_script_list(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx)
333 struct ctdb_script_list *head, *tail, *new_item;
334 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
335 struct ctdb_script_tree_item *tree_item;
339 the service specific event scripts
341 if (stat(ctdb->event_script_dir, &st) != 0 &&
343 DEBUG(DEBUG_CRIT,("No event script directory found at '%s'\n", ctdb->event_script_dir));
344 talloc_free(tmp_ctx);
348 /* create a tree to store all the script names in */
349 tree = trbt_create(tmp_ctx, 0);
351 /* scan all directory entries and insert all valid scripts into the
354 dir = opendir(ctdb->event_script_dir);
356 DEBUG(DEBUG_CRIT,("Failed to open event script directory '%s'\n", ctdb->event_script_dir));
357 talloc_free(tmp_ctx);
362 while ((de=readdir(dir)) != NULL) {
367 namlen = strlen(de->d_name);
373 if (de->d_name[namlen-1] == '~') {
374 /* skip files emacs left behind */
378 if (de->d_name[2] != '.') {
382 if (sscanf(de->d_name, "%02u.", &num) != 1) {
386 /* Make sure the event script is executable */
387 str = talloc_asprintf(tree, "%s/%s", ctdb->event_script_dir, de->d_name);
388 if (stat(str, &st) != 0) {
389 DEBUG(DEBUG_ERR,("Could not stat event script %s. Ignoring this event script\n", str));
394 tree_item = talloc(tree, struct ctdb_script_tree_item);
395 if (tree_item == NULL) {
396 DEBUG(DEBUG_ERR, (__location__ " Failed to allocate new tree item\n"));
397 talloc_free(tmp_ctx);
401 tree_item->is_enabled = 1;
402 if (!(st.st_mode & S_IXUSR)) {
403 DEBUG(DEBUG_INFO,("Event script %s is not executable. Ignoring this event script\n", str));
404 tree_item->is_enabled = 0;
407 tree_item->name = talloc_strdup(tree_item, de->d_name);
408 if (tree_item->name == NULL) {
409 DEBUG(DEBUG_ERR,(__location__ " Failed to allocate script name.\n"));
410 talloc_free(tmp_ctx);
414 /* store the event script in the tree */
415 trbt_insert32(tree, (num<<16)|count++, tree_item);
423 /* fetch the scripts from the tree one by one and add them to the linked
426 while ((tree_item=trbt_findfirstarray32(tree, 1)) != NULL) {
428 new_item = talloc(tmp_ctx, struct ctdb_script_list);
429 if (new_item == NULL) {
430 DEBUG(DEBUG_ERR, (__location__ " Failed to allocate new list item\n"));
431 talloc_free(tmp_ctx);
435 new_item->next = NULL;
436 new_item->name = talloc_steal(new_item, tree_item->name);
437 new_item->is_enabled = tree_item->is_enabled;
443 tail->next = new_item;
447 talloc_steal(mem_ctx, new_item);
449 /* remove this script from the tree */
450 talloc_free(tree_item);
453 talloc_free(tmp_ctx);
460 Actually run the event script
461 this function is called and run in the context of a forked child
462 which allows it to do blocking calls such as system()
464 static int ctdb_run_event_script(struct ctdb_context *ctdb,
466 enum ctdb_eventscript_call call,
471 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
472 struct ctdb_script_list *scripts, *current;
474 if (!from_user && call == CTDB_EVENT_MONITOR) {
475 /* This is running in the forked child process. At this stage
476 * we want to switch from being a ctdb daemon into being a
477 * client and connect to the real local daemon.
479 if (switch_from_server_to_client(ctdb) != 0) {
480 DEBUG(DEBUG_CRIT, (__location__ "ERROR: failed to switch eventscript child into client mode. shutting down.\n"));
484 if (ctdb_ctrl_event_script_init(ctdb) != 0) {
485 DEBUG(DEBUG_ERR,(__location__ " Failed to init event script monitoring\n"));
486 talloc_free(tmp_ctx);
491 if (ctdb->recovery_mode != CTDB_RECOVERY_NORMAL) {
492 /* we guarantee that only some specifically allowed event scripts are run
494 const enum ctdb_eventscript_call allowed_calls[] = {
495 CTDB_EVENT_START_RECOVERY, CTDB_EVENT_SHUTDOWN, CTDB_EVENT_RELEASE_IP, CTDB_EVENT_STOPPED };
497 for (i=0;i<ARRAY_SIZE(allowed_calls);i++) {
498 if (call == allowed_calls[i]) break;
500 if (i == ARRAY_SIZE(allowed_calls)) {
501 DEBUG(DEBUG_ERR,("Refusing to run event scripts call '%s' while in recovery\n",
503 talloc_free(tmp_ctx);
508 if (setpgid(0,0) != 0) {
509 DEBUG(DEBUG_ERR,("Failed to create process group for event scripts - %s\n",
511 talloc_free(tmp_ctx);
515 signal(SIGTERM, sigterm);
517 child_state.start = timeval_current();
518 child_state.script_running = "startup";
520 scripts = ctdb_get_script_list(ctdb, tmp_ctx);
522 /* fetch the scripts from the tree one by one and execute
525 for (current=scripts; current; current=current->next) {
526 const char *str = from_user ? "CTDB_CALLED_BY_USER=1 " : "";
528 /* Allow a setting where we run the actual monitor event
529 from an external source and replace it with
530 a "status" event that just picks up the actual
531 status of the event asynchronously.
533 if ((ctdb->tunable.use_status_events_for_monitoring != 0)
534 && (call == CTDB_EVENT_MONITOR)
536 cmdstr = talloc_asprintf(tmp_ctx, "%s%s/%s %s",
538 ctdb->event_script_dir,
539 current->name, "status");
541 cmdstr = talloc_asprintf(tmp_ctx, "%s%s/%s %s %s",
543 ctdb->event_script_dir,
544 current->name, call_names[call], options);
546 CTDB_NO_MEMORY(ctdb, cmdstr);
548 DEBUG(DEBUG_INFO,("Executing event script %s\n",cmdstr));
550 child_state.start = timeval_current();
551 child_state.script_running = cmdstr;
553 if (!from_user && call == CTDB_EVENT_MONITOR) {
554 if (ctdb_ctrl_event_script_start(ctdb, current->name) != 0) {
555 DEBUG(DEBUG_ERR,(__location__ " Failed to start event script monitoring\n"));
556 talloc_free(tmp_ctx);
560 if (!current->is_enabled) {
561 if (ctdb_ctrl_event_script_disabled(ctdb, current->name) != 0) {
562 DEBUG(DEBUG_ERR,(__location__ " Failed to report disabled eventscript\n"));
563 talloc_free(tmp_ctx);
570 if (!current->is_enabled) {
574 ret = system(cmdstr);
575 /* if the system() call was successful, translate ret into the
576 return code from the command
579 ret = WEXITSTATUS(ret);
583 DEBUG(DEBUG_ERR,("Script %s returned status 127. Someone just deleted it?\n", cmdstr));
586 if (!from_user && call == CTDB_EVENT_MONITOR) {
587 if (ctdb_ctrl_event_script_stop(ctdb, ret) != 0) {
588 DEBUG(DEBUG_ERR,(__location__ " Failed to stop event script monitoring\n"));
589 talloc_free(tmp_ctx);
594 /* return an error if the script failed */
596 DEBUG(DEBUG_ERR,("Event script %s failed with error %d\n", cmdstr, ret));
597 if (!from_user && call == CTDB_EVENT_MONITOR) {
598 if (ctdb_ctrl_event_script_finished(ctdb) != 0) {
599 DEBUG(DEBUG_ERR,(__location__ " Failed to finish event script monitoring\n"));
600 talloc_free(tmp_ctx);
605 talloc_free(tmp_ctx);
610 child_state.start = timeval_current();
611 child_state.script_running = "finished";
613 if (!from_user && call == CTDB_EVENT_MONITOR) {
614 if (ctdb_ctrl_event_script_finished(ctdb) != 0) {
615 DEBUG(DEBUG_ERR,(__location__ " Failed to finish event script monitoring\n"));
616 talloc_free(tmp_ctx);
621 talloc_free(tmp_ctx);
625 /* called when child is finished */
626 static void ctdb_event_script_handler(struct event_context *ev, struct fd_event *fde,
627 uint16_t flags, void *p)
629 struct ctdb_event_script_state *state =
630 talloc_get_type(p, struct ctdb_event_script_state);
631 struct ctdb_context *ctdb = state->ctdb;
633 if (read(state->fd[0], &state->cb_status, sizeof(state->cb_status)) !=
634 sizeof(state->cb_status)) {
635 state->cb_status = -2;
638 DEBUG(DEBUG_INFO,(__location__ " Eventscript %s %s finished with state %d\n",
639 call_names[state->call], state->options, state->cb_status));
642 ctdb->event_script_timeouts = 0;
646 static void ctdb_ban_self(struct ctdb_context *ctdb, uint32_t ban_period)
649 struct ctdb_ban_time bantime;
651 bantime.pnn = ctdb->pnn;
652 bantime.time = ban_period;
654 data.dsize = sizeof(bantime);
655 data.dptr = (uint8_t *)&bantime;
657 ctdb_control_set_ban_state(ctdb, data);
661 /* called when child times out */
662 static void ctdb_event_script_timeout(struct event_context *ev, struct timed_event *te,
663 struct timeval t, void *p)
665 struct ctdb_event_script_state *state = talloc_get_type(p, struct ctdb_event_script_state);
666 struct ctdb_context *ctdb = state->ctdb;
668 DEBUG(DEBUG_ERR,("Event script timed out : %s %s count : %u pid : %d\n",
669 call_names[state->call], state->options, ctdb->event_script_timeouts, state->child));
671 if (kill(state->child, 0) != 0) {
672 DEBUG(DEBUG_ERR,("Event script child process already dead, errno %s(%d)\n", strerror(errno), errno));
678 if (state->call == CTDB_EVENT_MONITOR) {
679 /* if it is a monitor event, we allow it to "hang" a few times
680 before we declare it a failure and ban ourself (and make
683 DEBUG(DEBUG_ERR, (__location__ " eventscript for monitor event timedout.\n"));
685 ctdb->event_script_timeouts++;
687 if (ctdb->event_script_timeouts > ctdb->tunable.script_ban_count) {
688 DEBUG(DEBUG_ERR, ("Maximum timeout count %u reached for eventscript. Making node unhealthy\n", ctdb->tunable.script_ban_count));
689 state->cb_status = -ETIME;
691 state->cb_status = 0;
693 } else if (state->call == CTDB_EVENT_STARTUP) {
694 DEBUG(DEBUG_ERR, (__location__ " eventscript for startup event timedout.\n"));
695 state->cb_status = -1;
697 /* if it is not a monitor or a startup event we ban ourself
700 DEBUG(DEBUG_ERR, (__location__ " eventscript for NON-monitor/NON-startup event timedout. Immediately banning ourself for %d seconds\n", ctdb->tunable.recovery_ban_period));
702 ctdb_ban_self(ctdb, ctdb->tunable.recovery_ban_period);
704 state->cb_status = -1;
707 if (state->call == CTDB_EVENT_MONITOR || state->call == CTDB_EVENT_STATUS) {
708 struct ctdb_monitor_script_status *script;
710 if (ctdb->current_monitor_status_ctx == NULL) {
715 script = ctdb->current_monitor_status_ctx->scripts;
716 if (script != NULL) {
717 script->timedout = 1;
720 if (ctdb->last_monitor_status_ctx) {
721 talloc_free(ctdb->last_monitor_status_ctx);
722 ctdb->last_monitor_status_ctx = NULL;
724 ctdb->last_monitor_status_ctx = ctdb->current_monitor_status_ctx;
725 ctdb->current_monitor_status_ctx = NULL;
732 destroy an event script: kill it if ->child != 0.
734 static int event_script_destructor(struct ctdb_event_script_state *state)
737 DEBUG(DEBUG_ERR,(__location__ " Sending SIGTERM to child pid:%d\n", state->child));
739 if (kill(state->child, SIGTERM) != 0) {
740 DEBUG(DEBUG_ERR,("Failed to kill child process for eventscript, errno %s(%d)\n", strerror(errno), errno));
744 /* This is allowed to free us; talloc will prevent double free anyway,
745 * but beware if you call this outside the destructor! */
746 if (state->callback) {
747 state->callback(state->ctdb, state->cb_status, state->private_data);
753 static unsigned int count_words(const char *options)
755 unsigned int words = 0;
757 options += strspn(options, " \t");
760 options += strcspn(options, " \t");
761 options += strspn(options, " \t");
766 static bool check_options(enum ctdb_eventscript_call call, const char *options)
769 /* These all take no arguments. */
770 case CTDB_EVENT_STARTUP:
771 case CTDB_EVENT_START_RECOVERY:
772 case CTDB_EVENT_RECOVERED:
773 case CTDB_EVENT_STOPPED:
774 case CTDB_EVENT_MONITOR:
775 case CTDB_EVENT_STATUS:
776 case CTDB_EVENT_SHUTDOWN:
777 return count_words(options) == 0;
779 case CTDB_EVENT_TAKE_IP: /* interface, IP address, netmask bits. */
780 case CTDB_EVENT_RELEASE_IP:
781 return count_words(options) == 3;
784 DEBUG(DEBUG_ERR,(__location__ "Unknown ctdb_eventscript_call %u\n", call));
790 run the event script in the background, calling the callback when
793 static int ctdb_event_script_callback_v(struct ctdb_context *ctdb,
794 void (*callback)(struct ctdb_context *, int, void *),
797 enum ctdb_eventscript_call call,
798 const char *fmt, va_list ap)
801 struct ctdb_event_script_state *state;
804 if (!from_user && (call == CTDB_EVENT_MONITOR || call == CTDB_EVENT_STATUS)) {
805 /* if this was a "monitor" or a status event, we recycle the
806 context to start a new monitor event
808 if (ctdb->monitor_event_script_ctx != NULL) {
809 talloc_free(ctdb->monitor_event_script_ctx);
810 ctdb->monitor_event_script_ctx = NULL;
812 ctdb->monitor_event_script_ctx = talloc_new(ctdb);
813 mem_ctx = ctdb->monitor_event_script_ctx;
815 if (ctdb->current_monitor_status_ctx != NULL) {
816 talloc_free(ctdb->current_monitor_status_ctx);
817 ctdb->current_monitor_status_ctx = NULL;
820 ctdb->current_monitor_status_ctx = talloc(ctdb, struct ctdb_monitor_script_status_ctx);
821 CTDB_NO_MEMORY(ctdb, ctdb->current_monitor_status_ctx);
822 ctdb->current_monitor_status_ctx->scripts = NULL;
824 /* any other script will first terminate any monitor event */
825 if (ctdb->monitor_event_script_ctx != NULL) {
826 talloc_free(ctdb->monitor_event_script_ctx);
827 ctdb->monitor_event_script_ctx = NULL;
829 /* and then use a context common for all non-monitor events */
830 if (ctdb->other_event_script_ctx == NULL) {
831 ctdb->other_event_script_ctx = talloc_new(ctdb);
833 mem_ctx = ctdb->other_event_script_ctx;
836 state = talloc(mem_ctx, struct ctdb_event_script_state);
837 CTDB_NO_MEMORY(ctdb, state);
840 state->callback = callback;
841 state->private_data = private_data;
843 state->options = talloc_vasprintf(state, fmt, ap);
844 state->timeout = timeval_set(ctdb->tunable.script_timeout, 0);
845 if (state->options == NULL) {
846 DEBUG(DEBUG_ERR, (__location__ " could not allocate state->options\n"));
850 if (!check_options(state->call, state->options)) {
851 DEBUG(DEBUG_ERR, ("Bad eventscript options '%s' for %s\n",
852 call_names[state->call], state->options));
857 DEBUG(DEBUG_INFO,(__location__ " Starting eventscript %s %s\n",
858 call_names[state->call], state->options));
860 ret = pipe(state->fd);
866 state->child = fork();
868 if (state->child == (pid_t)-1) {
875 if (state->child == 0) {
879 set_close_on_exec(state->fd[1]);
881 rt = ctdb_run_event_script(ctdb, from_user, state->call, state->options);
882 /* We must be able to write PIPEBUF bytes at least; if this
883 somehow fails, the read above will be short. */
884 write(state->fd[1], &rt, sizeof(rt));
890 set_close_on_exec(state->fd[0]);
891 talloc_set_destructor(state, event_script_destructor);
893 DEBUG(DEBUG_DEBUG, (__location__ " Created PIPE FD:%d to child eventscript process\n", state->fd[0]));
895 event_add_fd(ctdb->ev, state, state->fd[0], EVENT_FD_READ|EVENT_FD_AUTOCLOSE,
896 ctdb_event_script_handler, state);
898 if (!timeval_is_zero(&state->timeout)) {
899 event_add_timed(ctdb->ev, state, timeval_current_ofs(state->timeout.tv_sec, state->timeout.tv_usec), ctdb_event_script_timeout, state);
901 DEBUG(DEBUG_ERR, (__location__ " eventscript %s %s called with no timeout\n",
902 call_names[state->call], state->options));
910 run the event script in the background, calling the callback when
913 int ctdb_event_script_callback(struct ctdb_context *ctdb,
915 void (*callback)(struct ctdb_context *, int, void *),
918 enum ctdb_eventscript_call call,
919 const char *fmt, ...)
925 ret = ctdb_event_script_callback_v(ctdb, callback, private_data, from_user, call, fmt, ap);
932 struct callback_status {
938 called when ctdb_event_script() finishes
940 static void event_script_callback(struct ctdb_context *ctdb, int status, void *private_data)
942 struct callback_status *s = (struct callback_status *)private_data;
948 run the event script, waiting for it to complete. Used when the caller
949 doesn't want to continue till the event script has finished.
951 int ctdb_event_script_args(struct ctdb_context *ctdb, enum ctdb_eventscript_call call,
952 const char *fmt, ...)
956 struct callback_status status;
959 ret = ctdb_event_script_callback_v(ctdb,
960 event_script_callback, &status, false, call, fmt, ap);
969 while (status.done == false && event_loop_once(ctdb->ev) == 0) /* noop */;
971 return status.status;
974 int ctdb_event_script(struct ctdb_context *ctdb, enum ctdb_eventscript_call call)
976 /* GCC complains about empty format string, so use %s and "". */
977 return ctdb_event_script_args(ctdb, call, "%s", "");
980 struct eventscript_callback_state {
981 struct ctdb_req_control *c;
985 called when a forced eventscript finishes
987 static void run_eventscripts_callback(struct ctdb_context *ctdb, int status,
990 struct eventscript_callback_state *state =
991 talloc_get_type(private_data, struct eventscript_callback_state);
993 ctdb_enable_monitoring(ctdb);
996 DEBUG(DEBUG_ERR,(__location__ " Failed to forcibly run eventscripts\n"));
999 ctdb_request_control_reply(ctdb, state->c, NULL, status, NULL);
1000 /* This will free the struct ctdb_event_script_state we are in! */
1005 /* Returns rest of string, or NULL if no match. */
1006 static const char *get_call(const char *p, enum ctdb_eventscript_call *call)
1010 /* Skip any initial whitespace. */
1011 p += strspn(p, " \t");
1013 /* See if we match any. */
1014 for (*call = 0; *call < ARRAY_SIZE(call_names); (*call)++) {
1015 len = strlen(call_names[*call]);
1016 if (strncmp(p, call_names[*call], len) == 0) {
1017 /* If end of string or whitespace, we're done. */
1018 if (strcspn(p + len, " \t") == 0) {
1027 A control to force running of the eventscripts from the ctdb client tool
1029 int32_t ctdb_run_eventscripts(struct ctdb_context *ctdb,
1030 struct ctdb_req_control *c,
1031 TDB_DATA indata, bool *async_reply)
1034 struct eventscript_callback_state *state;
1035 const char *options;
1036 enum ctdb_eventscript_call call;
1038 /* Figure out what call they want. */
1039 options = get_call((const char *)indata.dptr, &call);
1041 DEBUG(DEBUG_ERR, (__location__ " Invalid forced \"%s\"\n", (const char *)indata.dptr));
1045 if (ctdb->recovery_mode != CTDB_RECOVERY_NORMAL) {
1046 DEBUG(DEBUG_ERR, (__location__ " Aborted running eventscript \"%s\" while in RECOVERY mode\n", indata.dptr));
1050 state = talloc(ctdb->other_event_script_ctx, struct eventscript_callback_state);
1051 CTDB_NO_MEMORY(ctdb, state);
1053 state->c = talloc_steal(state, c);
1055 DEBUG(DEBUG_NOTICE,("Forced running of eventscripts with arguments %s\n", indata.dptr));
1057 ctdb_disable_monitoring(ctdb);
1059 ret = ctdb_event_script_callback(ctdb,
1060 state, run_eventscripts_callback, state,
1061 true, call, "%s", options);
1064 ctdb_enable_monitoring(ctdb);
1065 DEBUG(DEBUG_ERR,(__location__ " Failed to run eventscripts with arguments %s\n", indata.dptr));
1070 /* tell ctdb_control.c that we will be replying asynchronously */
1071 *async_reply = true;
1078 int32_t ctdb_control_enable_script(struct ctdb_context *ctdb, TDB_DATA indata)
1083 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1085 script = (char *)indata.dptr;
1086 if (indata.dsize == 0) {
1087 DEBUG(DEBUG_ERR,(__location__ " No script specified.\n"));
1088 talloc_free(tmp_ctx);
1091 if (indata.dptr[indata.dsize - 1] != '\0') {
1092 DEBUG(DEBUG_ERR,(__location__ " String is not null terminated.\n"));
1093 talloc_free(tmp_ctx);
1096 if (index(script,'/') != NULL) {
1097 DEBUG(DEBUG_ERR,(__location__ " Script name contains '/'. Failed to enable script %s\n", script));
1098 talloc_free(tmp_ctx);
1103 if (stat(ctdb->event_script_dir, &st) != 0 &&
1105 DEBUG(DEBUG_CRIT,("No event script directory found at '%s'\n", ctdb->event_script_dir));
1106 talloc_free(tmp_ctx);
1111 filename = talloc_asprintf(tmp_ctx, "%s/%s", ctdb->event_script_dir, script);
1112 if (filename == NULL) {
1113 DEBUG(DEBUG_ERR,(__location__ " Failed to create script path\n"));
1114 talloc_free(tmp_ctx);
1118 if (stat(filename, &st) != 0) {
1119 DEBUG(DEBUG_ERR,("Could not stat event script %s. Failed to enable script.\n", filename));
1120 talloc_free(tmp_ctx);
1124 if (chmod(filename, st.st_mode | S_IXUSR) == -1) {
1125 DEBUG(DEBUG_ERR,("Could not chmod %s. Failed to enable script.\n", filename));
1126 talloc_free(tmp_ctx);
1130 talloc_free(tmp_ctx);
1134 int32_t ctdb_control_disable_script(struct ctdb_context *ctdb, TDB_DATA indata)
1139 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1141 script = (char *)indata.dptr;
1142 if (indata.dsize == 0) {
1143 DEBUG(DEBUG_ERR,(__location__ " No script specified.\n"));
1144 talloc_free(tmp_ctx);
1147 if (indata.dptr[indata.dsize - 1] != '\0') {
1148 DEBUG(DEBUG_ERR,(__location__ " String is not null terminated.\n"));
1149 talloc_free(tmp_ctx);
1152 if (index(script,'/') != NULL) {
1153 DEBUG(DEBUG_ERR,(__location__ " Script name contains '/'. Failed to disable script %s\n", script));
1154 talloc_free(tmp_ctx);
1159 if (stat(ctdb->event_script_dir, &st) != 0 &&
1161 DEBUG(DEBUG_CRIT,("No event script directory found at '%s'\n", ctdb->event_script_dir));
1162 talloc_free(tmp_ctx);
1167 filename = talloc_asprintf(tmp_ctx, "%s/%s", ctdb->event_script_dir, script);
1168 if (filename == NULL) {
1169 DEBUG(DEBUG_ERR,(__location__ " Failed to create script path\n"));
1170 talloc_free(tmp_ctx);
1174 if (stat(filename, &st) != 0) {
1175 DEBUG(DEBUG_ERR,("Could not stat event script %s. Failed to disable script.\n", filename));
1176 talloc_free(tmp_ctx);
1180 if (chmod(filename, st.st_mode & ~(S_IXUSR|S_IXGRP|S_IXOTH)) == -1) {
1181 DEBUG(DEBUG_ERR,("Could not chmod %s. Failed to disable script.\n", filename));
1182 talloc_free(tmp_ctx);
1186 talloc_free(tmp_ctx);