ctdb-daemon: Initialise eventscript status earlier
[Samba.git] / ctdb / server / eventscript.c
blob62fab5f1ede51b47d5c2c3d6da62413749c03ed3
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;
42 struct ctdb_event_script_state {
43 struct ctdb_context *ctdb;
44 struct event_script_callback *callback;
45 pid_t child;
46 int fd[2];
47 enum ctdb_eventscript_call call;
48 const char *options;
49 struct timeval timeout;
51 unsigned int current;
52 struct ctdb_scripts_wire *scripts;
55 static struct ctdb_script_wire *get_current_script(struct ctdb_event_script_state *state)
57 return &state->scripts->scripts[state->current];
60 /* called from ctdb_logging when we have received output on STDERR from
61 * one of the eventscripts
63 static void log_event_script_output(const char *str, uint16_t len, void *p)
65 struct ctdb_event_script_state *state
66 = talloc_get_type(p, struct ctdb_event_script_state);
67 struct ctdb_script_wire *current;
68 unsigned int slen, min;
70 /* We may have been aborted to run something else. Discard */
71 if (state->scripts == NULL) {
72 return;
75 current = get_current_script(state);
77 /* Append, but don't overfill buffer. It starts zero-filled. */
78 slen = strlen(current->output);
79 min = MIN(len, sizeof(current->output) - slen - 1);
81 memcpy(current->output + slen, str, min);
84 int32_t ctdb_control_get_event_script_status(struct ctdb_context *ctdb,
85 uint32_t call_type,
86 TDB_DATA *outdata)
88 if (call_type >= CTDB_EVENT_MAX) {
89 return -1;
92 if (ctdb->last_status[call_type] == NULL) {
93 /* If it's never been run, return nothing so they can tell. */
94 outdata->dsize = 0;
95 } else {
96 outdata->dsize = talloc_get_size(ctdb->last_status[call_type]);
97 outdata->dptr = (uint8_t *)ctdb->last_status[call_type];
99 return 0;
102 /* To ignore directory entry return 0, else return non-zero */
103 static int script_filter(const struct dirent *de)
105 int namelen = strlen(de->d_name);
107 /* Ignore . and .. */
108 if (namelen < 3) {
109 return 0;
112 /* Skip temporary files left behind by emacs */
113 if (de->d_name[namelen-1] == '~') {
114 return 0;
117 /* Filename should start with [0-9][0-9]. */
118 if (!isdigit(de->d_name[0]) || !isdigit(de->d_name[1]) ||
119 de->d_name[2] != '.') {
120 return 0;
123 if (namelen > MAX_SCRIPT_NAME) {
124 return 0;
127 return 1;
130 /* Return true if OK, otherwise set errno. */
131 static bool check_executable(const char *dir, const char *name)
133 char *full;
134 struct stat st;
136 full = talloc_asprintf(NULL, "%s/%s", dir, name);
137 if (!full)
138 return false;
140 if (stat(full, &st) != 0) {
141 DEBUG(DEBUG_ERR,("Could not stat event script %s: %s\n",
142 full, strerror(errno)));
143 talloc_free(full);
144 return false;
147 if (!(st.st_mode & S_IXUSR)) {
148 DEBUG(DEBUG_DEBUG,("Event script %s is not executable. Ignoring this event script\n", full));
149 errno = ENOEXEC;
150 talloc_free(full);
151 return false;
154 talloc_free(full);
155 return true;
158 static struct ctdb_scripts_wire *ctdb_get_script_list(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx)
160 struct dirent **namelist;
161 struct ctdb_scripts_wire *scripts;
162 int i, count;
164 /* scan all directory entries and insert all valid scripts into the
165 tree
167 count = scandir(ctdb->event_script_dir, &namelist, script_filter, alphasort);
168 if (count == -1) {
169 DEBUG(DEBUG_CRIT, ("Failed to read event script directory '%s' - %s\n",
170 ctdb->event_script_dir, strerror(errno)));
171 return NULL;
174 /* Overallocates by one, but that's OK */
175 scripts = talloc_zero_size(mem_ctx,
176 sizeof(*scripts)
177 + sizeof(scripts->scripts[0]) * count);
178 if (scripts == NULL) {
179 DEBUG(DEBUG_ERR, (__location__ " Failed to allocate scripts\n"));
180 goto done;
182 scripts->num_scripts = count;
184 for (i = 0; i < count; i++) {
185 struct ctdb_script_wire *s = &scripts->scripts[i];
187 if (strlcpy(s->name, namelist[i]->d_name, sizeof(s->name)) >=
188 sizeof(s->name)) {
189 s->status = -ENAMETOOLONG;
190 continue;
193 s->status = 0;
194 if (!check_executable(ctdb->event_script_dir,
195 namelist[i]->d_name)) {
196 s->status = -errno;
200 done:
201 for (i=0; i<count; i++) {
202 free(namelist[i]);
204 free(namelist);
205 return scripts;
209 /* There cannot be more than 10 arguments to command helper. */
210 #define MAX_HELPER_ARGS (10)
212 static bool child_helper_args(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
213 enum ctdb_eventscript_call call,
214 const char *options,
215 struct ctdb_script_wire *current, int fd,
216 int *argc, const char ***argv)
218 const char **tmp;
219 int n, i;
220 char *t, *saveptr, *opt;
222 tmp = talloc_array(mem_ctx, const char *, 10+1);
223 if (tmp == NULL) goto failed;
225 tmp[0] = talloc_asprintf(tmp, "%d", fd);
226 tmp[1] = talloc_asprintf(tmp, "%s/%s", ctdb->event_script_dir, current->name);
227 tmp[2] = talloc_asprintf(tmp, "%s", ctdb_eventscript_call_names[call]);
228 n = 3;
230 /* Split options into individual arguments */
231 opt = talloc_strdup(mem_ctx, options);
232 if (opt == NULL) {
233 goto failed;
236 t = strtok_r(opt, " ", &saveptr);
237 while (t != NULL) {
238 tmp[n++] = talloc_strdup(tmp, t);
239 if (n > MAX_HELPER_ARGS) {
240 goto args_failed;
242 t = strtok_r(NULL, " ", &saveptr);
245 for (i=0; i<n; i++) {
246 if (tmp[i] == NULL) {
247 goto failed;
251 /* Last argument should be NULL */
252 tmp[n++] = NULL;
254 *argc = n;
255 *argv = tmp;
256 return true;
259 args_failed:
260 DEBUG(DEBUG_ERR, (__location__ " too many arguments '%s' to eventscript '%s'\n",
261 options, ctdb_eventscript_call_names[call]));
263 failed:
264 if (tmp) {
265 talloc_free(tmp);
267 return false;
271 static void ctdb_event_script_handler(struct event_context *ev, struct fd_event *fde,
272 uint16_t flags, void *p);
274 static char helper_prog[PATH_MAX+1] = "";
276 static int fork_child_for_script(struct ctdb_context *ctdb,
277 struct ctdb_event_script_state *state)
279 int r;
280 struct tevent_fd *fde;
281 struct ctdb_script_wire *current = get_current_script(state);
282 int argc;
283 const char **argv;
285 if (!ctdb_set_helper("event helper", helper_prog, sizeof(helper_prog),
286 "CTDB_EVENT_HELPER",
287 CTDB_HELPER_BINDIR, "ctdb_event_helper")) {
288 ctdb_die(ctdb, __location__
289 " Unable to set event helper\n");
292 current->start = timeval_current();
294 r = pipe(state->fd);
295 if (r != 0) {
296 DEBUG(DEBUG_ERR, (__location__ " pipe failed for child eventscript process\n"));
297 return -errno;
300 /* Arguments for helper */
301 if (!child_helper_args(state, ctdb, state->call, state->options, current,
302 state->fd[1], &argc, &argv)) {
303 DEBUG(DEBUG_ERR, (__location__ " failed to create arguments for eventscript helper\n"));
304 r = -ENOMEM;
305 close(state->fd[0]);
306 close(state->fd[1]);
307 return r;
310 if (!ctdb_vfork_with_logging(state, ctdb, current->name,
311 helper_prog, argc, argv,
312 log_event_script_output,
313 state, &state->child)) {
314 talloc_free(argv);
315 r = -errno;
316 close(state->fd[0]);
317 close(state->fd[1]);
318 return r;
321 talloc_free(argv);
323 close(state->fd[1]);
324 set_close_on_exec(state->fd[0]);
326 /* Set ourselves up to be called when that's done. */
327 fde = event_add_fd(ctdb->ev, state, state->fd[0], EVENT_FD_READ,
328 ctdb_event_script_handler, state);
329 tevent_fd_set_auto_close(fde);
331 return 0;
335 Summarize status of this run of scripts.
337 static int script_status(struct ctdb_scripts_wire *scripts)
339 unsigned int i;
341 for (i = 0; i < scripts->num_scripts; i++) {
342 switch (scripts->scripts[i].status) {
343 case -ENAMETOOLONG:
344 case -ENOENT:
345 case -ENOEXEC:
346 /* Disabled or missing; that's OK. */
347 break;
348 case 0:
349 /* No problem. */
350 break;
351 default:
352 return scripts->scripts[i].status;
356 /* All OK! */
357 return 0;
360 /* called when child is finished */
361 static void ctdb_event_script_handler(struct event_context *ev, struct fd_event *fde,
362 uint16_t flags, void *p)
364 struct ctdb_event_script_state *state =
365 talloc_get_type(p, struct ctdb_event_script_state);
366 struct ctdb_script_wire *current = get_current_script(state);
367 struct ctdb_context *ctdb = state->ctdb;
368 int r, status;
370 if (ctdb == NULL) {
371 DEBUG(DEBUG_ERR,("Eventscript finished but ctdb is NULL\n"));
372 return;
375 r = sys_read(state->fd[0], &current->status, sizeof(current->status));
376 if (r < 0) {
377 current->status = -errno;
378 } else if (r == 0) {
379 current->status = -EINTR;
380 } else if (r != sizeof(current->status)) {
381 current->status = -EIO;
384 current->finished = timeval_current();
385 /* valgrind gets overloaded if we run next script as it's still doing
386 * post-execution analysis, so kill finished child here. */
387 if (ctdb->valgrinding) {
388 ctdb_kill(ctdb, state->child, SIGKILL);
391 state->child = 0;
393 status = script_status(state->scripts);
395 /* Aborted or finished all scripts? We're done. */
396 if (status != 0 || state->current+1 == state->scripts->num_scripts) {
397 if (status != 0) {
398 DEBUG(DEBUG_INFO,
399 ("Eventscript %s %s finished with state %d\n",
400 ctdb_eventscript_call_names[state->call],
401 state->options, status));
404 ctdb->event_script_timeouts = 0;
405 talloc_free(state);
406 return;
409 /* Forget about that old fd. */
410 talloc_free(fde);
412 /* Next script! */
413 state->current++;
414 current++;
415 current->status = fork_child_for_script(ctdb, state);
416 if (current->status != 0) {
417 /* This calls the callback. */
418 talloc_free(state);
422 struct debug_hung_script_state {
423 struct ctdb_context *ctdb;
424 pid_t child;
425 enum ctdb_eventscript_call call;
428 static int debug_hung_script_state_destructor(struct debug_hung_script_state *state)
430 if (state->child) {
431 ctdb_kill(state->ctdb, state->child, SIGKILL);
433 return 0;
436 static void debug_hung_script_timeout(struct tevent_context *ev, struct tevent_timer *te,
437 struct timeval t, void *p)
439 struct debug_hung_script_state *state =
440 talloc_get_type(p, struct debug_hung_script_state);
442 talloc_free(state);
445 static void debug_hung_script_done(struct tevent_context *ev, struct tevent_fd *fde,
446 uint16_t flags, void *p)
448 struct debug_hung_script_state *state =
449 talloc_get_type(p, struct debug_hung_script_state);
451 talloc_free(state);
454 static void ctdb_run_debug_hung_script(struct ctdb_context *ctdb, struct debug_hung_script_state *state)
456 pid_t pid;
457 const char * debug_hung_script = CTDB_ETCDIR "/debug-hung-script.sh";
458 int fd[2];
459 struct tevent_timer *ttimer;
460 struct tevent_fd *tfd;
461 const char **argv;
462 int i;
464 if (pipe(fd) < 0) {
465 DEBUG(DEBUG_ERR,("Failed to create pipe fd for debug hung script\n"));
466 return;
469 if (getenv("CTDB_DEBUG_HUNG_SCRIPT") != NULL) {
470 debug_hung_script = getenv("CTDB_DEBUG_HUNG_SCRIPT");
473 argv = talloc_array(state, const char *, 5);
475 argv[0] = talloc_asprintf(argv, "%d", fd[1]);
476 argv[1] = talloc_strdup(argv, debug_hung_script);
477 argv[2] = talloc_asprintf(argv, "%d", state->child);
478 argv[3] = talloc_strdup(argv, ctdb_eventscript_call_names[state->call]);
479 argv[4] = NULL;
481 for (i=0; i<4; i++) {
482 if (argv[i] == NULL) {
483 close(fd[0]);
484 close(fd[1]);
485 talloc_free(argv);
486 return;
491 if (!ctdb_vfork_with_logging(state, ctdb, "Hung-script",
492 helper_prog, 5, argv, NULL, NULL, &pid)) {
493 DEBUG(DEBUG_ERR,("Failed to fork a child to track hung event script\n"));
494 talloc_free(argv);
495 close(fd[0]);
496 close(fd[1]);
497 return;
500 talloc_free(argv);
501 close(fd[1]);
503 ttimer = tevent_add_timer(ctdb->ev, state,
504 timeval_current_ofs(ctdb->tunable.script_timeout, 0),
505 debug_hung_script_timeout, state);
506 if (ttimer == NULL) {
507 close(fd[0]);
508 return;
511 tfd = tevent_add_fd(ctdb->ev, state, fd[0], EVENT_FD_READ,
512 debug_hung_script_done, state);
513 if (tfd == NULL) {
514 talloc_free(ttimer);
515 close(fd[0]);
516 return;
518 tevent_fd_set_auto_close(tfd);
521 /* called when child times out */
522 static void ctdb_event_script_timeout(struct event_context *ev, struct timed_event *te,
523 struct timeval t, void *p)
525 struct ctdb_event_script_state *state = talloc_get_type(p, struct ctdb_event_script_state);
526 struct ctdb_context *ctdb = state->ctdb;
527 struct ctdb_script_wire *current = get_current_script(state);
528 struct debug_hung_script_state *debug_state;
530 DEBUG(DEBUG_ERR,("Event script '%s %s %s' timed out after %.1fs, count: %u, pid: %d\n",
531 current->name, ctdb_eventscript_call_names[state->call], state->options,
532 timeval_elapsed(&current->start),
533 ctdb->event_script_timeouts, state->child));
535 /* ignore timeouts for these events */
536 switch (state->call) {
537 case CTDB_EVENT_START_RECOVERY:
538 case CTDB_EVENT_RECOVERED:
539 case CTDB_EVENT_TAKE_IP:
540 case CTDB_EVENT_RELEASE_IP:
541 state->scripts->scripts[state->current].status = 0;
542 DEBUG(DEBUG_ERR,("Ignoring hung script for %s call %d\n", state->options, state->call));
543 break;
544 default:
545 state->scripts->scripts[state->current].status = -ETIME;
548 debug_state = talloc_zero(ctdb, struct debug_hung_script_state);
549 if (debug_state == NULL) {
550 talloc_free(state);
551 return;
554 /* Save information useful for running debug hung script, so
555 * eventscript state can be freed.
557 debug_state->ctdb = ctdb;
558 debug_state->child = state->child;
559 debug_state->call = state->call;
561 /* This destructor will actually kill the hung event script */
562 talloc_set_destructor(debug_state, debug_hung_script_state_destructor);
564 state->child = 0;
565 talloc_free(state);
567 ctdb_run_debug_hung_script(ctdb, debug_state);
571 destroy an event script: kill it if ->child != 0.
573 static int event_script_destructor(struct ctdb_event_script_state *state)
575 int status;
576 struct event_script_callback *callback;
578 if (state->child) {
579 DEBUG(DEBUG_ERR,(__location__ " Sending SIGTERM to child pid:%d\n", state->child));
581 if (ctdb_kill(state->ctdb, state->child, SIGTERM) != 0) {
582 DEBUG(DEBUG_ERR,("Failed to kill child process for eventscript, errno %s(%d)\n", strerror(errno), errno));
586 /* If we were the current monitor, we no longer are. */
587 if (state->ctdb->current_monitor == state) {
588 state->ctdb->current_monitor = NULL;
591 /* Save our scripts as the last executed status, if we have them.
592 * See ctdb_event_script_callback_v where we abort monitor event. */
593 if (state->scripts) {
594 talloc_free(state->ctdb->last_status[state->call]);
595 state->ctdb->last_status[state->call] = state->scripts;
596 if (state->current < state->ctdb->last_status[state->call]->num_scripts) {
597 state->ctdb->last_status[state->call]->num_scripts = state->current+1;
601 /* Use last status as result, or "OK" if none. */
602 if (state->ctdb->last_status[state->call]) {
603 status = script_status(state->ctdb->last_status[state->call]);
604 } else {
605 status = 0;
608 state->ctdb->active_events--;
609 if (state->ctdb->active_events < 0) {
610 ctdb_fatal(state->ctdb, "Active events < 0");
613 /* This is allowed to free us; talloc will prevent double free anyway,
614 * but beware if you call this outside the destructor!
615 * the callback hangs off a different context so we walk the list
616 * of "active" callbacks until we find the one state points to.
617 * if we cant find it it means the callback has been removed.
619 for (callback = state->ctdb->script_callbacks; callback != NULL; callback = callback->next) {
620 if (callback == state->callback) {
621 break;
625 state->callback = NULL;
627 if (callback) {
628 /* Make sure destructor doesn't free itself! */
629 talloc_steal(NULL, callback);
630 callback->fn(state->ctdb, status, callback->private_data);
631 talloc_free(callback);
634 return 0;
637 static unsigned int count_words(const char *options)
639 unsigned int words = 0;
641 options += strspn(options, " \t");
642 while (*options) {
643 words++;
644 options += strcspn(options, " \t");
645 options += strspn(options, " \t");
647 return words;
650 static bool check_options(enum ctdb_eventscript_call call, const char *options)
652 switch (call) {
653 /* These all take no arguments. */
654 case CTDB_EVENT_INIT:
655 case CTDB_EVENT_SETUP:
656 case CTDB_EVENT_STARTUP:
657 case CTDB_EVENT_START_RECOVERY:
658 case CTDB_EVENT_RECOVERED:
659 case CTDB_EVENT_MONITOR:
660 case CTDB_EVENT_SHUTDOWN:
661 case CTDB_EVENT_IPREALLOCATED:
662 return count_words(options) == 0;
664 case CTDB_EVENT_TAKE_IP: /* interface, IP address, netmask bits. */
665 case CTDB_EVENT_RELEASE_IP:
666 return count_words(options) == 3;
668 case CTDB_EVENT_UPDATE_IP: /* old interface, new interface, IP address, netmask bits. */
669 return count_words(options) == 4;
671 default:
672 DEBUG(DEBUG_ERR,(__location__ "Unknown ctdb_eventscript_call %u\n", call));
673 return false;
677 static int remove_callback(struct event_script_callback *callback)
679 DLIST_REMOVE(callback->ctdb->script_callbacks, callback);
680 return 0;
684 run the event script in the background, calling the callback when
685 finished
687 static int ctdb_event_script_callback_v(struct ctdb_context *ctdb,
688 const void *mem_ctx,
689 void (*callback)(struct ctdb_context *, int, void *),
690 void *private_data,
691 enum ctdb_eventscript_call call,
692 const char *fmt, va_list ap)
694 struct ctdb_event_script_state *state;
696 if (ctdb->recovery_mode != CTDB_RECOVERY_NORMAL) {
697 /* we guarantee that only some specifically allowed event scripts are run
698 while in recovery */
699 const enum ctdb_eventscript_call allowed_calls[] = {
700 CTDB_EVENT_INIT,
701 CTDB_EVENT_SETUP,
702 CTDB_EVENT_START_RECOVERY,
703 CTDB_EVENT_SHUTDOWN,
704 CTDB_EVENT_RELEASE_IP,
705 CTDB_EVENT_IPREALLOCATED,
707 int i;
708 for (i=0;i<ARRAY_SIZE(allowed_calls);i++) {
709 if (call == allowed_calls[i]) break;
711 if (i == ARRAY_SIZE(allowed_calls)) {
712 DEBUG(DEBUG_ERR,("Refusing to run event scripts call '%s' while in recovery\n",
713 ctdb_eventscript_call_names[call]));
714 return -1;
718 /* Do not run new monitor events if some event is already running */
719 if (call == CTDB_EVENT_MONITOR && ctdb->active_events > 0) {
720 if (callback != NULL) {
721 callback(ctdb, -ECANCELED, private_data);
723 return 0;
726 /* Kill off any running monitor events to run this event. */
727 if (ctdb->current_monitor) {
728 struct ctdb_event_script_state *ms = talloc_get_type(ctdb->current_monitor, struct ctdb_event_script_state);
730 /* Cancel current monitor callback state only if monitoring
731 * context ctdb->monitor->monitor_context has not been freed */
732 if (ms->callback != NULL && !ctdb_stopped_monitoring(ctdb)) {
733 ms->callback->fn(ctdb, -ECANCELED, ms->callback->private_data);
734 talloc_free(ms->callback);
737 /* Discard script status so we don't save to last_status */
738 talloc_free(ctdb->current_monitor->scripts);
739 ctdb->current_monitor->scripts = NULL;
740 talloc_free(ctdb->current_monitor);
741 ctdb->current_monitor = NULL;
744 state = talloc(ctdb->event_script_ctx, struct ctdb_event_script_state);
745 CTDB_NO_MEMORY(ctdb, state);
747 /* The callback isn't done if the context is freed. */
748 state->callback = talloc(mem_ctx, struct event_script_callback);
749 CTDB_NO_MEMORY(ctdb, state->callback);
750 DLIST_ADD(ctdb->script_callbacks, state->callback);
751 talloc_set_destructor(state->callback, remove_callback);
752 state->callback->ctdb = ctdb;
753 state->callback->fn = callback;
754 state->callback->private_data = private_data;
756 state->ctdb = ctdb;
757 state->call = call;
758 state->options = talloc_vasprintf(state, fmt, ap);
759 state->timeout = timeval_set(ctdb->tunable.script_timeout, 0);
760 state->scripts = NULL;
761 if (state->options == NULL) {
762 DEBUG(DEBUG_ERR, (__location__ " could not allocate state->options\n"));
763 talloc_free(state);
764 return -1;
766 if (!check_options(state->call, state->options)) {
767 DEBUG(DEBUG_ERR, ("Bad eventscript options '%s' for '%s'\n",
768 state->options,
769 ctdb_eventscript_call_names[state->call]));
770 talloc_free(state);
771 return -1;
774 DEBUG(DEBUG_INFO,(__location__ " Starting eventscript %s %s\n",
775 ctdb_eventscript_call_names[state->call],
776 state->options));
778 /* This is not a child of state, since we save it in destructor. */
779 state->scripts = ctdb_get_script_list(ctdb, ctdb);
780 if (state->scripts == NULL) {
781 talloc_free(state);
782 return -1;
784 state->current = 0;
785 state->child = 0;
787 if (call == CTDB_EVENT_MONITOR) {
788 ctdb->current_monitor = state;
791 talloc_set_destructor(state, event_script_destructor);
793 ctdb->active_events++;
795 /* Nothing to do? */
796 if (state->scripts->num_scripts == 0) {
797 callback(ctdb, 0, private_data);
798 talloc_free(state);
799 return 0;
802 state->scripts->scripts[0].status = fork_child_for_script(ctdb, state);
803 if (state->scripts->scripts[0].status != 0) {
804 /* Callback is called from destructor, with fail result. */
805 talloc_free(state);
806 return 0;
809 if (!timeval_is_zero(&state->timeout)) {
810 event_add_timed(ctdb->ev, state, timeval_current_ofs(state->timeout.tv_sec, state->timeout.tv_usec), ctdb_event_script_timeout, state);
811 } else {
812 DEBUG(DEBUG_ERR, (__location__ " eventscript %s %s called with no timeout\n",
813 ctdb_eventscript_call_names[state->call],
814 state->options));
817 return 0;
822 run the event script in the background, calling the callback when
823 finished. If mem_ctx is freed, callback will never be called.
825 int ctdb_event_script_callback(struct ctdb_context *ctdb,
826 TALLOC_CTX *mem_ctx,
827 void (*callback)(struct ctdb_context *, int, void *),
828 void *private_data,
829 enum ctdb_eventscript_call call,
830 const char *fmt, ...)
832 va_list ap;
833 int ret;
835 va_start(ap, fmt);
836 ret = ctdb_event_script_callback_v(ctdb, mem_ctx, callback, private_data, call, fmt, ap);
837 va_end(ap);
839 return ret;
843 struct callback_status {
844 bool done;
845 int status;
849 called when ctdb_event_script() finishes
851 static void event_script_callback(struct ctdb_context *ctdb, int status, void *private_data)
853 struct callback_status *s = (struct callback_status *)private_data;
854 s->done = true;
855 s->status = status;
859 run the event script, waiting for it to complete. Used when the caller
860 doesn't want to continue till the event script has finished.
862 int ctdb_event_script_args(struct ctdb_context *ctdb, enum ctdb_eventscript_call call,
863 const char *fmt, ...)
865 va_list ap;
866 int ret;
867 struct callback_status status = {
868 .status = -1,
869 .done = false,
872 va_start(ap, fmt);
873 ret = ctdb_event_script_callback_v(ctdb, ctdb,
874 event_script_callback, &status, call, fmt, ap);
875 va_end(ap);
876 if (ret != 0) {
877 return ret;
880 while (status.done == false && event_loop_once(ctdb->ev) == 0) /* noop */;
882 if (status.status == -ETIME) {
883 DEBUG(DEBUG_ERR, (__location__ " eventscript for '%s' timedout."
884 " Immediately banning ourself for %d seconds\n",
885 ctdb_eventscript_call_names[call],
886 ctdb->tunable.recovery_ban_period));
888 /* Don't ban self if CTDB is starting up or shutting down */
889 if (call != CTDB_EVENT_INIT && call != CTDB_EVENT_SHUTDOWN) {
890 ctdb_ban_self(ctdb);
894 return status.status;
897 int ctdb_event_script(struct ctdb_context *ctdb, enum ctdb_eventscript_call call)
899 /* GCC complains about empty format string, so use %s and "". */
900 return ctdb_event_script_args(ctdb, call, "%s", "");
903 struct eventscript_callback_state {
904 struct ctdb_req_control *c;
908 called when a forced eventscript run has finished
910 static void run_eventscripts_callback(struct ctdb_context *ctdb, int status,
911 void *private_data)
913 struct eventscript_callback_state *state =
914 talloc_get_type(private_data, struct eventscript_callback_state);
916 if (status != 0) {
917 DEBUG(DEBUG_ERR,(__location__ " Failed to run eventscripts\n"));
920 ctdb_request_control_reply(ctdb, state->c, NULL, status, NULL);
921 /* This will free the struct ctdb_event_script_state we are in! */
922 talloc_free(state);
923 return;
927 /* Returns rest of string, or NULL if no match. */
928 static const char *get_call(const char *p, enum ctdb_eventscript_call *call)
930 unsigned int len;
932 /* Skip any initial whitespace. */
933 p += strspn(p, " \t");
935 /* See if we match any. */
936 for (*call = 0; *call < CTDB_EVENT_MAX; (*call)++) {
937 len = strlen(ctdb_eventscript_call_names[*call]);
938 if (strncmp(p, ctdb_eventscript_call_names[*call], len) == 0) {
939 /* If end of string or whitespace, we're done. */
940 if (strcspn(p + len, " \t") == 0) {
941 return p + len;
945 return NULL;
949 A control to force running of the eventscripts from the ctdb client tool
951 int32_t ctdb_run_eventscripts(struct ctdb_context *ctdb,
952 struct ctdb_req_control *c,
953 TDB_DATA indata, bool *async_reply)
955 int ret;
956 struct eventscript_callback_state *state;
957 const char *options;
958 enum ctdb_eventscript_call call;
960 /* Figure out what call they want. */
961 options = get_call((const char *)indata.dptr, &call);
962 if (!options) {
963 DEBUG(DEBUG_ERR, (__location__ " Invalid event name \"%s\"\n", (const char *)indata.dptr));
964 return -1;
967 if (ctdb->recovery_mode != CTDB_RECOVERY_NORMAL) {
968 DEBUG(DEBUG_ERR, (__location__ " Aborted running eventscript \"%s\" while in RECOVERY mode\n", indata.dptr));
969 return -1;
972 state = talloc(ctdb->event_script_ctx, struct eventscript_callback_state);
973 CTDB_NO_MEMORY(ctdb, state);
975 state->c = talloc_steal(state, c);
977 DEBUG(DEBUG_NOTICE,("Running eventscripts with arguments %s\n", indata.dptr));
979 ret = ctdb_event_script_callback(ctdb,
980 state, run_eventscripts_callback, state,
981 call, "%s", options);
983 if (ret != 0) {
984 DEBUG(DEBUG_ERR,(__location__ " Failed to run eventscripts with arguments %s\n", indata.dptr));
985 talloc_free(state);
986 return -1;
989 /* tell ctdb_control.c that we will be replying asynchronously */
990 *async_reply = true;
992 return 0;
997 int32_t ctdb_control_enable_script(struct ctdb_context *ctdb, TDB_DATA indata)
999 const char *script;
1000 struct stat st;
1001 char *filename;
1002 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1004 script = (char *)indata.dptr;
1005 if (indata.dsize == 0) {
1006 DEBUG(DEBUG_ERR,(__location__ " No script specified.\n"));
1007 talloc_free(tmp_ctx);
1008 return -1;
1010 if (indata.dptr[indata.dsize - 1] != '\0') {
1011 DEBUG(DEBUG_ERR,(__location__ " String is not null terminated.\n"));
1012 talloc_free(tmp_ctx);
1013 return -1;
1015 if (index(script,'/') != NULL) {
1016 DEBUG(DEBUG_ERR,(__location__ " Script name contains '/'. Failed to enable script %s\n", script));
1017 talloc_free(tmp_ctx);
1018 return -1;
1022 if (stat(ctdb->event_script_dir, &st) != 0 &&
1023 errno == ENOENT) {
1024 DEBUG(DEBUG_CRIT,("No event script directory found at '%s'\n", ctdb->event_script_dir));
1025 talloc_free(tmp_ctx);
1026 return -1;
1030 filename = talloc_asprintf(tmp_ctx, "%s/%s", ctdb->event_script_dir, script);
1031 if (filename == NULL) {
1032 DEBUG(DEBUG_ERR,(__location__ " Failed to create script path\n"));
1033 talloc_free(tmp_ctx);
1034 return -1;
1037 if (stat(filename, &st) != 0) {
1038 DEBUG(DEBUG_ERR,("Could not stat event script %s. Failed to enable script.\n", filename));
1039 talloc_free(tmp_ctx);
1040 return -1;
1043 if (chmod(filename, st.st_mode | S_IXUSR) == -1) {
1044 DEBUG(DEBUG_ERR,("Could not chmod %s. Failed to enable script.\n", filename));
1045 talloc_free(tmp_ctx);
1046 return -1;
1049 talloc_free(tmp_ctx);
1050 return 0;
1053 int32_t ctdb_control_disable_script(struct ctdb_context *ctdb, TDB_DATA indata)
1055 const char *script;
1056 struct stat st;
1057 char *filename;
1058 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1060 script = (char *)indata.dptr;
1061 if (indata.dsize == 0) {
1062 DEBUG(DEBUG_ERR,(__location__ " No script specified.\n"));
1063 talloc_free(tmp_ctx);
1064 return -1;
1066 if (indata.dptr[indata.dsize - 1] != '\0') {
1067 DEBUG(DEBUG_ERR,(__location__ " String is not null terminated.\n"));
1068 talloc_free(tmp_ctx);
1069 return -1;
1071 if (index(script,'/') != NULL) {
1072 DEBUG(DEBUG_ERR,(__location__ " Script name contains '/'. Failed to disable script %s\n", script));
1073 talloc_free(tmp_ctx);
1074 return -1;
1078 if (stat(ctdb->event_script_dir, &st) != 0 &&
1079 errno == ENOENT) {
1080 DEBUG(DEBUG_CRIT,("No event script directory found at '%s'\n", ctdb->event_script_dir));
1081 talloc_free(tmp_ctx);
1082 return -1;
1086 filename = talloc_asprintf(tmp_ctx, "%s/%s", ctdb->event_script_dir, script);
1087 if (filename == NULL) {
1088 DEBUG(DEBUG_ERR,(__location__ " Failed to create script path\n"));
1089 talloc_free(tmp_ctx);
1090 return -1;
1093 if (stat(filename, &st) != 0) {
1094 DEBUG(DEBUG_ERR,("Could not stat event script %s. Failed to disable script.\n", filename));
1095 talloc_free(tmp_ctx);
1096 return -1;
1099 if (chmod(filename, st.st_mode & ~(S_IXUSR|S_IXGRP|S_IXOTH)) == -1) {
1100 DEBUG(DEBUG_ERR,("Could not chmod %s. Failed to disable script.\n", filename));
1101 talloc_free(tmp_ctx);
1102 return -1;
1105 talloc_free(tmp_ctx);
1106 return 0;