docs: change hard-coded default of nsupdate command to match lib/param
[Samba.git] / ctdb / server / eventscript.c
blobb85fed5944b618a6dd4c4c6240cb3fcb05f4ef5f
1 /*
2 event script handling
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/>.
20 #include "includes.h"
21 #include <time.h>
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 "../common/rb_tree.h"
28 #include "lib/util/dlinklist.h"
30 static void ctdb_event_script_timeout(struct event_context *ev, struct timed_event *te, struct timeval t, void *p);
32 /* This is attached to the event script state. */
33 struct event_script_callback {
34 struct event_script_callback *next, *prev;
35 struct ctdb_context *ctdb;
37 /* Warning: this can free us! */
38 void (*fn)(struct ctdb_context *, int, void *);
39 void *private_data;
43 struct ctdb_event_script_state {
44 struct ctdb_context *ctdb;
45 struct event_script_callback *callback;
46 pid_t child;
47 int fd[2];
48 enum ctdb_eventscript_call call;
49 const char *options;
50 struct timeval timeout;
52 unsigned int current;
53 struct ctdb_scripts_wire *scripts;
56 static struct ctdb_script_wire *get_current_script(struct ctdb_event_script_state *state)
58 return &state->scripts->scripts[state->current];
61 /* called from ctdb_logging when we have received output on STDERR from
62 * one of the eventscripts
64 static void log_event_script_output(const char *str, uint16_t len, void *p)
66 struct ctdb_event_script_state *state
67 = talloc_get_type(p, struct ctdb_event_script_state);
68 struct ctdb_script_wire *current;
69 unsigned int slen, min;
71 /* We may have been aborted to run something else. Discard */
72 if (state->scripts == NULL) {
73 return;
76 current = get_current_script(state);
78 /* Append, but don't overfill buffer. It starts zero-filled. */
79 slen = strlen(current->output);
80 min = MIN(len, sizeof(current->output) - slen - 1);
82 memcpy(current->output + slen, str, min);
85 int32_t ctdb_control_get_event_script_status(struct ctdb_context *ctdb,
86 uint32_t call_type,
87 TDB_DATA *outdata)
89 if (call_type >= CTDB_EVENT_MAX) {
90 return -1;
93 if (ctdb->last_status[call_type] == NULL) {
94 /* If it's never been run, return nothing so they can tell. */
95 outdata->dsize = 0;
96 } else {
97 outdata->dsize = talloc_get_size(ctdb->last_status[call_type]);
98 outdata->dptr = (uint8_t *)ctdb->last_status[call_type];
100 return 0;
103 /* To ignore directory entry return 0, else return non-zero */
104 static int script_filter(const struct dirent *de)
106 int namelen = strlen(de->d_name);
108 /* Ignore . and .. */
109 if (namelen < 3) {
110 return 0;
113 /* Skip temporary files left behind by emacs */
114 if (de->d_name[namelen-1] == '~') {
115 return 0;
118 /* Filename should start with [0-9][0-9]. */
119 if (!isdigit(de->d_name[0]) || !isdigit(de->d_name[1]) ||
120 de->d_name[2] != '.') {
121 return 0;
124 if (namelen > MAX_SCRIPT_NAME) {
125 return 0;
128 return 1;
131 /* Return true if OK, otherwise set errno. */
132 static bool check_executable(const char *dir, const char *name)
134 char *full;
135 struct stat st;
137 full = talloc_asprintf(NULL, "%s/%s", dir, name);
138 if (!full)
139 return false;
141 if (stat(full, &st) != 0) {
142 DEBUG(DEBUG_ERR,("Could not stat event script %s: %s\n",
143 full, strerror(errno)));
144 talloc_free(full);
145 return false;
148 if (!(st.st_mode & S_IXUSR)) {
149 DEBUG(DEBUG_DEBUG,("Event script %s is not executable. Ignoring this event script\n", full));
150 errno = ENOEXEC;
151 talloc_free(full);
152 return false;
155 talloc_free(full);
156 return true;
159 static struct ctdb_scripts_wire *ctdb_get_script_list(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx)
161 struct dirent **namelist;
162 struct ctdb_scripts_wire *scripts;
163 int count;
165 /* scan all directory entries and insert all valid scripts into the
166 tree
168 count = scandir(ctdb->event_script_dir, &namelist, script_filter, alphasort);
169 if (count == -1) {
170 DEBUG(DEBUG_CRIT, ("Failed to read event script directory '%s' - %s\n",
171 ctdb->event_script_dir, strerror(errno)));
172 return NULL;
175 /* Overallocates by one, but that's OK */
176 scripts = talloc_zero_size(mem_ctx,
177 sizeof(*scripts)
178 + sizeof(scripts->scripts[0]) * count);
179 if (scripts == NULL) {
180 DEBUG(DEBUG_ERR, (__location__ " Failed to allocate scripts\n"));
181 free(namelist);
182 return NULL;
184 scripts->num_scripts = count;
186 for (count = 0; count < scripts->num_scripts; count++) {
187 strcpy(scripts->scripts[count].name, namelist[count]->d_name);
188 scripts->scripts[count].status = 0;
189 if (!check_executable(ctdb->event_script_dir, namelist[count]->d_name)) {
190 scripts->scripts[count].status = -errno;
194 free(namelist);
195 return scripts;
199 /* There cannot be more than 10 arguments to command helper. */
200 #define MAX_HELPER_ARGS (10)
202 static bool child_helper_args(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
203 enum ctdb_eventscript_call call,
204 const char *options,
205 struct ctdb_script_wire *current, int fd,
206 int *argc, const char ***argv)
208 const char **tmp;
209 int n, i;
210 char *t, *saveptr, *opt;
212 tmp = talloc_array(mem_ctx, const char *, 10+1);
213 if (tmp == NULL) goto failed;
215 tmp[0] = talloc_asprintf(tmp, "%d", fd);
216 tmp[1] = talloc_asprintf(tmp, "%s/%s", ctdb->event_script_dir, current->name);
217 tmp[2] = talloc_asprintf(tmp, "%s", ctdb_eventscript_call_names[call]);
218 n = 3;
220 /* Split options into individual arguments */
221 opt = talloc_strdup(mem_ctx, options);
222 if (opt == NULL) {
223 goto failed;
226 t = strtok_r(opt, " ", &saveptr);
227 while (t != NULL) {
228 tmp[n++] = talloc_strdup(tmp, t);
229 if (n > MAX_HELPER_ARGS) {
230 goto args_failed;
232 t = strtok_r(NULL, " ", &saveptr);
235 for (i=0; i<n; i++) {
236 if (tmp[i] == NULL) {
237 goto failed;
241 /* Last argument should be NULL */
242 tmp[n++] = NULL;
244 *argc = n;
245 *argv = tmp;
246 return true;
249 args_failed:
250 DEBUG(DEBUG_ERR, (__location__ " too many arguments '%s' to eventscript '%s'\n",
251 options, ctdb_eventscript_call_names[call]));
253 failed:
254 if (tmp) {
255 talloc_free(tmp);
257 return false;
261 static void ctdb_event_script_handler(struct event_context *ev, struct fd_event *fde,
262 uint16_t flags, void *p);
264 static const char *helper_prog = NULL;
266 static int fork_child_for_script(struct ctdb_context *ctdb,
267 struct ctdb_event_script_state *state)
269 int r;
270 struct tevent_fd *fde;
271 struct ctdb_script_wire *current = get_current_script(state);
272 int argc;
273 const char **argv;
274 static const char *helper = BINDIR "/ctdb_event_helper";
276 if (helper_prog == NULL) {
277 const char *t = getenv("CTDB_EVENT_HELPER");
278 if (t != NULL) {
279 helper_prog = t;
280 } else {
281 helper_prog = helper;
285 current->start = timeval_current();
287 r = pipe(state->fd);
288 if (r != 0) {
289 DEBUG(DEBUG_ERR, (__location__ " pipe failed for child eventscript process\n"));
290 return -errno;
293 /* Arguments for helper */
294 if (!child_helper_args(state, ctdb, state->call, state->options, current,
295 state->fd[1], &argc, &argv)) {
296 DEBUG(DEBUG_ERR, (__location__ " failed to create arguments for eventscript helper\n"));
297 r = -ENOMEM;
298 close(state->fd[0]);
299 close(state->fd[1]);
300 return r;
303 if (!ctdb_vfork_with_logging(state, ctdb, current->name,
304 helper_prog, argc, argv,
305 log_event_script_output,
306 state, &state->child)) {
307 talloc_free(argv);
308 r = -errno;
309 close(state->fd[0]);
310 close(state->fd[1]);
311 return r;
314 talloc_free(argv);
316 close(state->fd[1]);
317 set_close_on_exec(state->fd[0]);
319 /* Set ourselves up to be called when that's done. */
320 fde = event_add_fd(ctdb->ev, state, state->fd[0], EVENT_FD_READ,
321 ctdb_event_script_handler, state);
322 tevent_fd_set_auto_close(fde);
324 return 0;
328 Summarize status of this run of scripts.
330 static int script_status(struct ctdb_scripts_wire *scripts)
332 unsigned int i;
334 for (i = 0; i < scripts->num_scripts; i++) {
335 switch (scripts->scripts[i].status) {
336 case -ENOENT:
337 case -ENOEXEC:
338 /* Disabled or missing; that's OK. */
339 break;
340 case 0:
341 /* No problem. */
342 break;
343 default:
344 return scripts->scripts[i].status;
348 /* All OK! */
349 return 0;
352 /* called when child is finished */
353 static void ctdb_event_script_handler(struct event_context *ev, struct fd_event *fde,
354 uint16_t flags, void *p)
356 struct ctdb_event_script_state *state =
357 talloc_get_type(p, struct ctdb_event_script_state);
358 struct ctdb_script_wire *current = get_current_script(state);
359 struct ctdb_context *ctdb = state->ctdb;
360 int r, status;
362 if (ctdb == NULL) {
363 DEBUG(DEBUG_ERR,("Eventscript finished but ctdb is NULL\n"));
364 return;
367 r = read(state->fd[0], &current->status, sizeof(current->status));
368 if (r < 0) {
369 current->status = -errno;
370 } else if (r != sizeof(current->status)) {
371 current->status = -EIO;
374 current->finished = timeval_current();
375 /* valgrind gets overloaded if we run next script as it's still doing
376 * post-execution analysis, so kill finished child here. */
377 if (ctdb->valgrinding) {
378 ctdb_kill(ctdb, state->child, SIGKILL);
381 state->child = 0;
383 status = script_status(state->scripts);
385 /* Aborted or finished all scripts? We're done. */
386 if (status != 0 || state->current+1 == state->scripts->num_scripts) {
387 DEBUG(DEBUG_INFO,(__location__ " Eventscript %s %s finished with state %d\n",
388 ctdb_eventscript_call_names[state->call], state->options, status));
390 ctdb->event_script_timeouts = 0;
391 talloc_free(state);
392 return;
395 /* Forget about that old fd. */
396 talloc_free(fde);
398 /* Next script! */
399 state->current++;
400 current++;
401 current->status = fork_child_for_script(ctdb, state);
402 if (current->status != 0) {
403 /* This calls the callback. */
404 talloc_free(state);
408 struct debug_hung_script_state {
409 struct ctdb_context *ctdb;
410 pid_t child;
411 enum ctdb_eventscript_call call;
414 static int debug_hung_script_state_destructor(struct debug_hung_script_state *state)
416 if (state->child) {
417 ctdb_kill(state->ctdb, state->child, SIGKILL);
419 return 0;
422 static void debug_hung_script_timeout(struct tevent_context *ev, struct tevent_timer *te,
423 struct timeval t, void *p)
425 struct debug_hung_script_state *state =
426 talloc_get_type(p, struct debug_hung_script_state);
428 talloc_free(state);
431 static void debug_hung_script_done(struct tevent_context *ev, struct tevent_fd *fde,
432 uint16_t flags, void *p)
434 struct debug_hung_script_state *state =
435 talloc_get_type(p, struct debug_hung_script_state);
437 talloc_free(state);
440 static void ctdb_run_debug_hung_script(struct ctdb_context *ctdb, struct debug_hung_script_state *state)
442 pid_t pid;
443 const char * debug_hung_script = ETCDIR "/ctdb/debug-hung-script.sh";
444 int fd[2];
445 struct tevent_timer *ttimer;
446 struct tevent_fd *tfd;
447 const char **argv;
448 int i;
450 if (helper_prog == NULL) {
451 return;
454 if (pipe(fd) < 0) {
455 DEBUG(DEBUG_ERR,("Failed to create pipe fd for debug hung script\n"));
456 return;
459 argv = talloc_array(state, const char *, 5);
461 argv[0] = talloc_asprintf(argv, "%d", fd[1]);
462 argv[1] = talloc_strdup(argv, debug_hung_script);
463 argv[2] = talloc_asprintf(argv, "%d", state->child);
464 argv[3] = talloc_strdup(argv, ctdb_eventscript_call_names[state->call]);
465 argv[4] = NULL;
467 for (i=0; i<4; i++) {
468 if (argv[i] == NULL) {
469 close(fd[0]);
470 close(fd[1]);
471 talloc_free(argv);
472 return;
477 if (!ctdb_vfork_with_logging(state, ctdb, "Hung-script",
478 helper_prog, 5, argv, NULL, NULL, &pid)) {
479 DEBUG(DEBUG_ERR,("Failed to fork a child to track hung event script\n"));
480 talloc_free(argv);
481 close(fd[0]);
482 close(fd[1]);
483 return;
486 talloc_free(argv);
487 close(fd[1]);
489 ttimer = tevent_add_timer(ctdb->ev, state,
490 timeval_current_ofs(ctdb->tunable.script_timeout, 0),
491 debug_hung_script_timeout, state);
492 if (ttimer == NULL) {
493 close(fd[0]);
494 return;
497 tfd = tevent_add_fd(ctdb->ev, state, fd[0], EVENT_FD_READ,
498 debug_hung_script_done, state);
499 if (tfd == NULL) {
500 talloc_free(ttimer);
501 close(fd[0]);
502 return;
504 tevent_fd_set_auto_close(tfd);
507 /* called when child times out */
508 static void ctdb_event_script_timeout(struct event_context *ev, struct timed_event *te,
509 struct timeval t, void *p)
511 struct ctdb_event_script_state *state = talloc_get_type(p, struct ctdb_event_script_state);
512 struct ctdb_context *ctdb = state->ctdb;
513 struct ctdb_script_wire *current = get_current_script(state);
514 struct debug_hung_script_state *debug_state;
516 DEBUG(DEBUG_ERR,("Event script '%s %s %s' timed out after %.1fs, count: %u, pid: %d\n",
517 current->name, ctdb_eventscript_call_names[state->call], state->options,
518 timeval_elapsed(&current->start),
519 ctdb->event_script_timeouts, state->child));
521 /* ignore timeouts for these events */
522 switch (state->call) {
523 case CTDB_EVENT_START_RECOVERY:
524 case CTDB_EVENT_RECOVERED:
525 case CTDB_EVENT_TAKE_IP:
526 case CTDB_EVENT_RELEASE_IP:
527 state->scripts->scripts[state->current].status = 0;
528 DEBUG(DEBUG_ERR,("Ignoring hung script for %s call %d\n", state->options, state->call));
529 break;
530 default:
531 state->scripts->scripts[state->current].status = -ETIME;
534 debug_state = talloc_zero(ctdb, struct debug_hung_script_state);
535 if (debug_state == NULL) {
536 talloc_free(state);
537 return;
540 /* Save information useful for running debug hung script, so
541 * eventscript state can be freed.
543 debug_state->ctdb = ctdb;
544 debug_state->child = state->child;
545 debug_state->call = state->call;
547 /* This destructor will actually kill the hung event script */
548 talloc_set_destructor(debug_state, debug_hung_script_state_destructor);
550 state->child = 0;
551 talloc_free(state);
553 ctdb_run_debug_hung_script(ctdb, debug_state);
557 destroy an event script: kill it if ->child != 0.
559 static int event_script_destructor(struct ctdb_event_script_state *state)
561 int status;
562 struct event_script_callback *callback;
564 if (state->child) {
565 DEBUG(DEBUG_ERR,(__location__ " Sending SIGTERM to child pid:%d\n", state->child));
567 if (ctdb_kill(state->ctdb, state->child, SIGTERM) != 0) {
568 DEBUG(DEBUG_ERR,("Failed to kill child process for eventscript, errno %s(%d)\n", strerror(errno), errno));
572 /* If we were the current monitor, we no longer are. */
573 if (state->ctdb->current_monitor == state) {
574 state->ctdb->current_monitor = NULL;
577 /* Save our scripts as the last executed status, if we have them.
578 * See ctdb_event_script_callback_v where we abort monitor event. */
579 if (state->scripts) {
580 talloc_free(state->ctdb->last_status[state->call]);
581 state->ctdb->last_status[state->call] = state->scripts;
582 if (state->current < state->ctdb->last_status[state->call]->num_scripts) {
583 state->ctdb->last_status[state->call]->num_scripts = state->current+1;
587 /* Use last status as result, or "OK" if none. */
588 if (state->ctdb->last_status[state->call]) {
589 status = script_status(state->ctdb->last_status[state->call]);
590 } else {
591 status = 0;
594 state->ctdb->active_events--;
595 if (state->ctdb->active_events < 0) {
596 ctdb_fatal(state->ctdb, "Active events < 0");
599 /* This is allowed to free us; talloc will prevent double free anyway,
600 * but beware if you call this outside the destructor!
601 * the callback hangs off a different context so we walk the list
602 * of "active" callbacks until we find the one state points to.
603 * if we cant find it it means the callback has been removed.
605 for (callback = state->ctdb->script_callbacks; callback != NULL; callback = callback->next) {
606 if (callback == state->callback) {
607 break;
611 state->callback = NULL;
613 if (callback) {
614 /* Make sure destructor doesn't free itself! */
615 talloc_steal(NULL, callback);
616 callback->fn(state->ctdb, status, callback->private_data);
617 talloc_free(callback);
620 return 0;
623 static unsigned int count_words(const char *options)
625 unsigned int words = 0;
627 options += strspn(options, " \t");
628 while (*options) {
629 words++;
630 options += strcspn(options, " \t");
631 options += strspn(options, " \t");
633 return words;
636 static bool check_options(enum ctdb_eventscript_call call, const char *options)
638 switch (call) {
639 /* These all take no arguments. */
640 case CTDB_EVENT_INIT:
641 case CTDB_EVENT_SETUP:
642 case CTDB_EVENT_STARTUP:
643 case CTDB_EVENT_START_RECOVERY:
644 case CTDB_EVENT_RECOVERED:
645 case CTDB_EVENT_MONITOR:
646 case CTDB_EVENT_SHUTDOWN:
647 case CTDB_EVENT_IPREALLOCATED:
648 return count_words(options) == 0;
650 case CTDB_EVENT_TAKE_IP: /* interface, IP address, netmask bits. */
651 case CTDB_EVENT_RELEASE_IP:
652 return count_words(options) == 3;
654 case CTDB_EVENT_UPDATE_IP: /* old interface, new interface, IP address, netmask bits. */
655 return count_words(options) == 4;
657 default:
658 DEBUG(DEBUG_ERR,(__location__ "Unknown ctdb_eventscript_call %u\n", call));
659 return false;
663 static int remove_callback(struct event_script_callback *callback)
665 DLIST_REMOVE(callback->ctdb->script_callbacks, callback);
666 return 0;
670 run the event script in the background, calling the callback when
671 finished
673 static int ctdb_event_script_callback_v(struct ctdb_context *ctdb,
674 const void *mem_ctx,
675 void (*callback)(struct ctdb_context *, int, void *),
676 void *private_data,
677 enum ctdb_eventscript_call call,
678 const char *fmt, va_list ap)
680 struct ctdb_event_script_state *state;
682 if (ctdb->recovery_mode != CTDB_RECOVERY_NORMAL) {
683 /* we guarantee that only some specifically allowed event scripts are run
684 while in recovery */
685 const enum ctdb_eventscript_call allowed_calls[] = {
686 CTDB_EVENT_INIT,
687 CTDB_EVENT_SETUP,
688 CTDB_EVENT_START_RECOVERY,
689 CTDB_EVENT_SHUTDOWN,
690 CTDB_EVENT_RELEASE_IP,
691 CTDB_EVENT_IPREALLOCATED,
693 int i;
694 for (i=0;i<ARRAY_SIZE(allowed_calls);i++) {
695 if (call == allowed_calls[i]) break;
697 if (i == ARRAY_SIZE(allowed_calls)) {
698 DEBUG(DEBUG_ERR,("Refusing to run event scripts call '%s' while in recovery\n",
699 ctdb_eventscript_call_names[call]));
700 return -1;
704 /* Do not run new monitor events if some event is already running */
705 if (call == CTDB_EVENT_MONITOR && ctdb->active_events > 0) {
706 if (callback != NULL) {
707 callback(ctdb, -ECANCELED, private_data);
709 return 0;
712 /* Kill off any running monitor events to run this event. */
713 if (ctdb->current_monitor) {
714 struct ctdb_event_script_state *ms = talloc_get_type(ctdb->current_monitor, struct ctdb_event_script_state);
716 /* Cancel current monitor callback state only if monitoring
717 * context ctdb->monitor->monitor_context has not been freed */
718 if (ms->callback != NULL && !ctdb_stopped_monitoring(ctdb)) {
719 ms->callback->fn(ctdb, -ECANCELED, ms->callback->private_data);
720 talloc_free(ms->callback);
723 /* Discard script status so we don't save to last_status */
724 talloc_free(ctdb->current_monitor->scripts);
725 ctdb->current_monitor->scripts = NULL;
726 talloc_free(ctdb->current_monitor);
727 ctdb->current_monitor = NULL;
730 state = talloc(ctdb->event_script_ctx, struct ctdb_event_script_state);
731 CTDB_NO_MEMORY(ctdb, state);
733 /* The callback isn't done if the context is freed. */
734 state->callback = talloc(mem_ctx, struct event_script_callback);
735 CTDB_NO_MEMORY(ctdb, state->callback);
736 DLIST_ADD(ctdb->script_callbacks, state->callback);
737 talloc_set_destructor(state->callback, remove_callback);
738 state->callback->ctdb = ctdb;
739 state->callback->fn = callback;
740 state->callback->private_data = private_data;
742 state->ctdb = ctdb;
743 state->call = call;
744 state->options = talloc_vasprintf(state, fmt, ap);
745 state->timeout = timeval_set(ctdb->tunable.script_timeout, 0);
746 state->scripts = NULL;
747 if (state->options == NULL) {
748 DEBUG(DEBUG_ERR, (__location__ " could not allocate state->options\n"));
749 talloc_free(state);
750 return -1;
752 if (!check_options(state->call, state->options)) {
753 DEBUG(DEBUG_ERR, ("Bad eventscript options '%s' for %s\n",
754 ctdb_eventscript_call_names[state->call], state->options));
755 talloc_free(state);
756 return -1;
759 DEBUG(DEBUG_INFO,(__location__ " Starting eventscript %s %s\n",
760 ctdb_eventscript_call_names[state->call],
761 state->options));
763 /* This is not a child of state, since we save it in destructor. */
764 state->scripts = ctdb_get_script_list(ctdb, ctdb);
765 if (state->scripts == NULL) {
766 talloc_free(state);
767 return -1;
769 state->current = 0;
770 state->child = 0;
772 if (call == CTDB_EVENT_MONITOR) {
773 ctdb->current_monitor = state;
776 talloc_set_destructor(state, event_script_destructor);
778 ctdb->active_events++;
780 /* Nothing to do? */
781 if (state->scripts->num_scripts == 0) {
782 talloc_free(state);
783 return 0;
786 state->scripts->scripts[0].status = fork_child_for_script(ctdb, state);
787 if (state->scripts->scripts[0].status != 0) {
788 /* Callback is called from destructor, with fail result. */
789 talloc_free(state);
790 return 0;
793 if (!timeval_is_zero(&state->timeout)) {
794 event_add_timed(ctdb->ev, state, timeval_current_ofs(state->timeout.tv_sec, state->timeout.tv_usec), ctdb_event_script_timeout, state);
795 } else {
796 DEBUG(DEBUG_ERR, (__location__ " eventscript %s %s called with no timeout\n",
797 ctdb_eventscript_call_names[state->call],
798 state->options));
801 return 0;
806 run the event script in the background, calling the callback when
807 finished. If mem_ctx is freed, callback will never be called.
809 int ctdb_event_script_callback(struct ctdb_context *ctdb,
810 TALLOC_CTX *mem_ctx,
811 void (*callback)(struct ctdb_context *, int, void *),
812 void *private_data,
813 enum ctdb_eventscript_call call,
814 const char *fmt, ...)
816 va_list ap;
817 int ret;
819 va_start(ap, fmt);
820 ret = ctdb_event_script_callback_v(ctdb, mem_ctx, callback, private_data, call, fmt, ap);
821 va_end(ap);
823 return ret;
827 struct callback_status {
828 bool done;
829 int status;
833 called when ctdb_event_script() finishes
835 static void event_script_callback(struct ctdb_context *ctdb, int status, void *private_data)
837 struct callback_status *s = (struct callback_status *)private_data;
838 s->done = true;
839 s->status = status;
843 run the event script, waiting for it to complete. Used when the caller
844 doesn't want to continue till the event script has finished.
846 int ctdb_event_script_args(struct ctdb_context *ctdb, enum ctdb_eventscript_call call,
847 const char *fmt, ...)
849 va_list ap;
850 int ret;
851 struct callback_status status;
853 va_start(ap, fmt);
854 ret = ctdb_event_script_callback_v(ctdb, ctdb,
855 event_script_callback, &status, call, fmt, ap);
856 va_end(ap);
857 if (ret != 0) {
858 return ret;
861 status.status = -1;
862 status.done = false;
864 while (status.done == false && event_loop_once(ctdb->ev) == 0) /* noop */;
866 if (status.status == -ETIME) {
867 DEBUG(DEBUG_ERR, (__location__ " eventscript for '%s' timedout."
868 " Immediately banning ourself for %d seconds\n",
869 ctdb_eventscript_call_names[call],
870 ctdb->tunable.recovery_ban_period));
872 /* Don't ban self if CTDB is starting up or shutting down */
873 if (call != CTDB_EVENT_INIT && call != CTDB_EVENT_SHUTDOWN) {
874 ctdb_ban_self(ctdb);
878 return status.status;
881 int ctdb_event_script(struct ctdb_context *ctdb, enum ctdb_eventscript_call call)
883 /* GCC complains about empty format string, so use %s and "". */
884 return ctdb_event_script_args(ctdb, call, "%s", "");
887 struct eventscript_callback_state {
888 struct ctdb_req_control *c;
892 called when a forced eventscript run has finished
894 static void run_eventscripts_callback(struct ctdb_context *ctdb, int status,
895 void *private_data)
897 struct eventscript_callback_state *state =
898 talloc_get_type(private_data, struct eventscript_callback_state);
900 ctdb_enable_monitoring(ctdb);
902 if (status != 0) {
903 DEBUG(DEBUG_ERR,(__location__ " Failed to run eventscripts\n"));
906 ctdb_request_control_reply(ctdb, state->c, NULL, status, NULL);
907 /* This will free the struct ctdb_event_script_state we are in! */
908 talloc_free(state);
909 return;
913 /* Returns rest of string, or NULL if no match. */
914 static const char *get_call(const char *p, enum ctdb_eventscript_call *call)
916 unsigned int len;
918 /* Skip any initial whitespace. */
919 p += strspn(p, " \t");
921 /* See if we match any. */
922 for (*call = 0; *call < CTDB_EVENT_MAX; (*call)++) {
923 len = strlen(ctdb_eventscript_call_names[*call]);
924 if (strncmp(p, ctdb_eventscript_call_names[*call], len) == 0) {
925 /* If end of string or whitespace, we're done. */
926 if (strcspn(p + len, " \t") == 0) {
927 return p + len;
931 return NULL;
935 A control to force running of the eventscripts from the ctdb client tool
937 int32_t ctdb_run_eventscripts(struct ctdb_context *ctdb,
938 struct ctdb_req_control *c,
939 TDB_DATA indata, bool *async_reply)
941 int ret;
942 struct eventscript_callback_state *state;
943 const char *options;
944 enum ctdb_eventscript_call call;
946 /* Figure out what call they want. */
947 options = get_call((const char *)indata.dptr, &call);
948 if (!options) {
949 DEBUG(DEBUG_ERR, (__location__ " Invalid event name \"%s\"\n", (const char *)indata.dptr));
950 return -1;
953 if (ctdb->recovery_mode != CTDB_RECOVERY_NORMAL) {
954 DEBUG(DEBUG_ERR, (__location__ " Aborted running eventscript \"%s\" while in RECOVERY mode\n", indata.dptr));
955 return -1;
958 state = talloc(ctdb->event_script_ctx, struct eventscript_callback_state);
959 CTDB_NO_MEMORY(ctdb, state);
961 state->c = talloc_steal(state, c);
963 DEBUG(DEBUG_NOTICE,("Running eventscripts with arguments %s\n", indata.dptr));
965 ctdb_disable_monitoring(ctdb);
967 ret = ctdb_event_script_callback(ctdb,
968 state, run_eventscripts_callback, state,
969 call, "%s", options);
971 if (ret != 0) {
972 ctdb_enable_monitoring(ctdb);
973 DEBUG(DEBUG_ERR,(__location__ " Failed to run eventscripts with arguments %s\n", indata.dptr));
974 talloc_free(state);
975 return -1;
978 /* tell ctdb_control.c that we will be replying asynchronously */
979 *async_reply = true;
981 return 0;
986 int32_t ctdb_control_enable_script(struct ctdb_context *ctdb, TDB_DATA indata)
988 const char *script;
989 struct stat st;
990 char *filename;
991 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
993 script = (char *)indata.dptr;
994 if (indata.dsize == 0) {
995 DEBUG(DEBUG_ERR,(__location__ " No script specified.\n"));
996 talloc_free(tmp_ctx);
997 return -1;
999 if (indata.dptr[indata.dsize - 1] != '\0') {
1000 DEBUG(DEBUG_ERR,(__location__ " String is not null terminated.\n"));
1001 talloc_free(tmp_ctx);
1002 return -1;
1004 if (index(script,'/') != NULL) {
1005 DEBUG(DEBUG_ERR,(__location__ " Script name contains '/'. Failed to enable script %s\n", script));
1006 talloc_free(tmp_ctx);
1007 return -1;
1011 if (stat(ctdb->event_script_dir, &st) != 0 &&
1012 errno == ENOENT) {
1013 DEBUG(DEBUG_CRIT,("No event script directory found at '%s'\n", ctdb->event_script_dir));
1014 talloc_free(tmp_ctx);
1015 return -1;
1019 filename = talloc_asprintf(tmp_ctx, "%s/%s", ctdb->event_script_dir, script);
1020 if (filename == NULL) {
1021 DEBUG(DEBUG_ERR,(__location__ " Failed to create script path\n"));
1022 talloc_free(tmp_ctx);
1023 return -1;
1026 if (stat(filename, &st) != 0) {
1027 DEBUG(DEBUG_ERR,("Could not stat event script %s. Failed to enable script.\n", filename));
1028 talloc_free(tmp_ctx);
1029 return -1;
1032 if (chmod(filename, st.st_mode | S_IXUSR) == -1) {
1033 DEBUG(DEBUG_ERR,("Could not chmod %s. Failed to enable script.\n", filename));
1034 talloc_free(tmp_ctx);
1035 return -1;
1038 talloc_free(tmp_ctx);
1039 return 0;
1042 int32_t ctdb_control_disable_script(struct ctdb_context *ctdb, TDB_DATA indata)
1044 const char *script;
1045 struct stat st;
1046 char *filename;
1047 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1049 script = (char *)indata.dptr;
1050 if (indata.dsize == 0) {
1051 DEBUG(DEBUG_ERR,(__location__ " No script specified.\n"));
1052 talloc_free(tmp_ctx);
1053 return -1;
1055 if (indata.dptr[indata.dsize - 1] != '\0') {
1056 DEBUG(DEBUG_ERR,(__location__ " String is not null terminated.\n"));
1057 talloc_free(tmp_ctx);
1058 return -1;
1060 if (index(script,'/') != NULL) {
1061 DEBUG(DEBUG_ERR,(__location__ " Script name contains '/'. Failed to disable script %s\n", script));
1062 talloc_free(tmp_ctx);
1063 return -1;
1067 if (stat(ctdb->event_script_dir, &st) != 0 &&
1068 errno == ENOENT) {
1069 DEBUG(DEBUG_CRIT,("No event script directory found at '%s'\n", ctdb->event_script_dir));
1070 talloc_free(tmp_ctx);
1071 return -1;
1075 filename = talloc_asprintf(tmp_ctx, "%s/%s", ctdb->event_script_dir, script);
1076 if (filename == NULL) {
1077 DEBUG(DEBUG_ERR,(__location__ " Failed to create script path\n"));
1078 talloc_free(tmp_ctx);
1079 return -1;
1082 if (stat(filename, &st) != 0) {
1083 DEBUG(DEBUG_ERR,("Could not stat event script %s. Failed to disable script.\n", filename));
1084 talloc_free(tmp_ctx);
1085 return -1;
1088 if (chmod(filename, st.st_mode & ~(S_IXUSR|S_IXGRP|S_IXOTH)) == -1) {
1089 DEBUG(DEBUG_ERR,("Could not chmod %s. Failed to disable script.\n", filename));
1090 talloc_free(tmp_ctx);
1091 return -1;
1094 talloc_free(tmp_ctx);
1095 return 0;