lib: Remove unused parmlist code
[Samba.git] / ctdb / server / eventscript.c
blob49619b27063d97e1394f8416076231c31d5274d3
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
719 * running, unless the running event is a monitor event, in
720 * which case running a new one should cancel the old one. */
721 if (call == CTDB_EVENT_MONITOR &&
722 ctdb->active_events > 0 &&
723 ctdb->current_monitor == NULL) {
724 if (callback != NULL) {
725 callback(ctdb, -ECANCELED, private_data);
727 return 0;
730 /* Kill off any running monitor events to run this event. */
731 if (ctdb->current_monitor) {
732 struct ctdb_event_script_state *ms = talloc_get_type(ctdb->current_monitor, struct ctdb_event_script_state);
734 /* Cancel current monitor callback state only if monitoring
735 * context ctdb->monitor->monitor_context has not been freed */
736 if (ms->callback != NULL && !ctdb_stopped_monitoring(ctdb)) {
737 ms->callback->fn(ctdb, -ECANCELED, ms->callback->private_data);
738 talloc_free(ms->callback);
741 /* Discard script status so we don't save to last_status */
742 talloc_free(ctdb->current_monitor->scripts);
743 ctdb->current_monitor->scripts = NULL;
744 talloc_free(ctdb->current_monitor);
745 ctdb->current_monitor = NULL;
748 state = talloc(ctdb->event_script_ctx, struct ctdb_event_script_state);
749 CTDB_NO_MEMORY(ctdb, state);
751 /* The callback isn't done if the context is freed. */
752 state->callback = talloc(mem_ctx, struct event_script_callback);
753 CTDB_NO_MEMORY(ctdb, state->callback);
754 DLIST_ADD(ctdb->script_callbacks, state->callback);
755 talloc_set_destructor(state->callback, remove_callback);
756 state->callback->ctdb = ctdb;
757 state->callback->fn = callback;
758 state->callback->private_data = private_data;
760 state->ctdb = ctdb;
761 state->call = call;
762 state->options = talloc_vasprintf(state, fmt, ap);
763 state->timeout = timeval_set(ctdb->tunable.script_timeout, 0);
764 state->scripts = NULL;
765 if (state->options == NULL) {
766 DEBUG(DEBUG_ERR, (__location__ " could not allocate state->options\n"));
767 talloc_free(state);
768 return -1;
770 if (!check_options(state->call, state->options)) {
771 DEBUG(DEBUG_ERR, ("Bad eventscript options '%s' for '%s'\n",
772 state->options,
773 ctdb_eventscript_call_names[state->call]));
774 talloc_free(state);
775 return -1;
778 DEBUG(DEBUG_INFO,(__location__ " Starting eventscript %s %s\n",
779 ctdb_eventscript_call_names[state->call],
780 state->options));
782 /* This is not a child of state, since we save it in destructor. */
783 state->scripts = ctdb_get_script_list(ctdb, ctdb);
784 if (state->scripts == NULL) {
785 talloc_free(state);
786 return -1;
788 state->current = 0;
789 state->child = 0;
791 if (call == CTDB_EVENT_MONITOR) {
792 ctdb->current_monitor = state;
795 talloc_set_destructor(state, event_script_destructor);
797 ctdb->active_events++;
799 /* Nothing to do? */
800 if (state->scripts->num_scripts == 0) {
801 callback(ctdb, 0, private_data);
802 talloc_free(state);
803 return 0;
806 state->scripts->scripts[0].status = fork_child_for_script(ctdb, state);
807 if (state->scripts->scripts[0].status != 0) {
808 /* Callback is called from destructor, with fail result. */
809 talloc_free(state);
810 return 0;
813 if (!timeval_is_zero(&state->timeout)) {
814 event_add_timed(ctdb->ev, state, timeval_current_ofs(state->timeout.tv_sec, state->timeout.tv_usec), ctdb_event_script_timeout, state);
815 } else {
816 DEBUG(DEBUG_ERR, (__location__ " eventscript %s %s called with no timeout\n",
817 ctdb_eventscript_call_names[state->call],
818 state->options));
821 return 0;
826 run the event script in the background, calling the callback when
827 finished. If mem_ctx is freed, callback will never be called.
829 int ctdb_event_script_callback(struct ctdb_context *ctdb,
830 TALLOC_CTX *mem_ctx,
831 void (*callback)(struct ctdb_context *, int, void *),
832 void *private_data,
833 enum ctdb_eventscript_call call,
834 const char *fmt, ...)
836 va_list ap;
837 int ret;
839 va_start(ap, fmt);
840 ret = ctdb_event_script_callback_v(ctdb, mem_ctx, callback, private_data, call, fmt, ap);
841 va_end(ap);
843 return ret;
847 struct callback_status {
848 bool done;
849 int status;
853 called when ctdb_event_script() finishes
855 static void event_script_callback(struct ctdb_context *ctdb, int status, void *private_data)
857 struct callback_status *s = (struct callback_status *)private_data;
858 s->done = true;
859 s->status = status;
863 run the event script, waiting for it to complete. Used when the caller
864 doesn't want to continue till the event script has finished.
866 int ctdb_event_script_args(struct ctdb_context *ctdb, enum ctdb_eventscript_call call,
867 const char *fmt, ...)
869 va_list ap;
870 int ret;
871 struct callback_status status = {
872 .status = -1,
873 .done = false,
876 va_start(ap, fmt);
877 ret = ctdb_event_script_callback_v(ctdb, ctdb,
878 event_script_callback, &status, call, fmt, ap);
879 va_end(ap);
880 if (ret != 0) {
881 return ret;
884 while (status.done == false && event_loop_once(ctdb->ev) == 0) /* noop */;
886 if (status.status == -ETIME) {
887 DEBUG(DEBUG_ERR, (__location__ " eventscript for '%s' timedout."
888 " Immediately banning ourself for %d seconds\n",
889 ctdb_eventscript_call_names[call],
890 ctdb->tunable.recovery_ban_period));
892 /* Don't ban self if CTDB is starting up or shutting down */
893 if (call != CTDB_EVENT_INIT && call != CTDB_EVENT_SHUTDOWN) {
894 ctdb_ban_self(ctdb);
898 return status.status;
901 int ctdb_event_script(struct ctdb_context *ctdb, enum ctdb_eventscript_call call)
903 /* GCC complains about empty format string, so use %s and "". */
904 return ctdb_event_script_args(ctdb, call, "%s", "");
907 struct eventscript_callback_state {
908 struct ctdb_req_control *c;
912 called when a forced eventscript run has finished
914 static void run_eventscripts_callback(struct ctdb_context *ctdb, int status,
915 void *private_data)
917 const char *errmsg = NULL;
919 struct eventscript_callback_state *state =
920 talloc_get_type(private_data, struct eventscript_callback_state);
922 if (status != 0) {
923 if (status == -ECANCELED) {
924 DEBUG(DEBUG_WARNING,
925 (__location__ " Eventscript cancelled\n"));
926 errmsg = "cancelled";
927 } else {
928 DEBUG(DEBUG_ERR,
929 (__location__ " Failed to run eventscripts\n"));
933 ctdb_request_control_reply(ctdb, state->c, NULL, status, errmsg);
934 /* This will free the struct ctdb_event_script_state we are in! */
935 talloc_free(state);
936 return;
940 /* Returns rest of string, or NULL if no match. */
941 static const char *get_call(const char *p, enum ctdb_eventscript_call *call)
943 unsigned int len;
945 /* Skip any initial whitespace. */
946 p += strspn(p, " \t");
948 /* See if we match any. */
949 for (*call = 0; *call < CTDB_EVENT_MAX; (*call)++) {
950 len = strlen(ctdb_eventscript_call_names[*call]);
951 if (strncmp(p, ctdb_eventscript_call_names[*call], len) == 0) {
952 /* If end of string or whitespace, we're done. */
953 if (strcspn(p + len, " \t") == 0) {
954 return p + len;
958 return NULL;
962 A control to force running of the eventscripts from the ctdb client tool
964 int32_t ctdb_run_eventscripts(struct ctdb_context *ctdb,
965 struct ctdb_req_control *c,
966 TDB_DATA indata, bool *async_reply)
968 int ret;
969 struct eventscript_callback_state *state;
970 const char *options;
971 enum ctdb_eventscript_call call;
973 /* Figure out what call they want. */
974 options = get_call((const char *)indata.dptr, &call);
975 if (!options) {
976 DEBUG(DEBUG_ERR, (__location__ " Invalid event name \"%s\"\n", (const char *)indata.dptr));
977 return -1;
980 if (ctdb->recovery_mode != CTDB_RECOVERY_NORMAL) {
981 DEBUG(DEBUG_ERR, (__location__ " Aborted running eventscript \"%s\" while in RECOVERY mode\n", indata.dptr));
982 return -1;
985 state = talloc(ctdb->event_script_ctx, struct eventscript_callback_state);
986 CTDB_NO_MEMORY(ctdb, state);
988 state->c = talloc_steal(state, c);
990 DEBUG(DEBUG_NOTICE,("Running eventscripts with arguments %s\n", indata.dptr));
992 ret = ctdb_event_script_callback(ctdb,
993 ctdb, run_eventscripts_callback, state,
994 call, "%s", options);
996 if (ret != 0) {
997 DEBUG(DEBUG_ERR,(__location__ " Failed to run eventscripts with arguments %s\n", indata.dptr));
998 talloc_free(state);
999 return -1;
1002 /* tell ctdb_control.c that we will be replying asynchronously */
1003 *async_reply = true;
1005 return 0;
1010 int32_t ctdb_control_enable_script(struct ctdb_context *ctdb, TDB_DATA indata)
1012 const char *script;
1013 struct stat st;
1014 char *filename;
1015 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1017 script = (char *)indata.dptr;
1018 if (indata.dsize == 0) {
1019 DEBUG(DEBUG_ERR,(__location__ " No script specified.\n"));
1020 talloc_free(tmp_ctx);
1021 return -1;
1023 if (indata.dptr[indata.dsize - 1] != '\0') {
1024 DEBUG(DEBUG_ERR,(__location__ " String is not null terminated.\n"));
1025 talloc_free(tmp_ctx);
1026 return -1;
1028 if (index(script,'/') != NULL) {
1029 DEBUG(DEBUG_ERR,(__location__ " Script name contains '/'. Failed to enable script %s\n", script));
1030 talloc_free(tmp_ctx);
1031 return -1;
1035 if (stat(ctdb->event_script_dir, &st) != 0 &&
1036 errno == ENOENT) {
1037 DEBUG(DEBUG_CRIT,("No event script directory found at '%s'\n", ctdb->event_script_dir));
1038 talloc_free(tmp_ctx);
1039 return -1;
1043 filename = talloc_asprintf(tmp_ctx, "%s/%s", ctdb->event_script_dir, script);
1044 if (filename == NULL) {
1045 DEBUG(DEBUG_ERR,(__location__ " Failed to create script path\n"));
1046 talloc_free(tmp_ctx);
1047 return -1;
1050 if (stat(filename, &st) != 0) {
1051 DEBUG(DEBUG_ERR,("Could not stat event script %s. Failed to enable script.\n", filename));
1052 talloc_free(tmp_ctx);
1053 return -1;
1056 if (chmod(filename, st.st_mode | S_IXUSR) == -1) {
1057 DEBUG(DEBUG_ERR,("Could not chmod %s. Failed to enable script.\n", filename));
1058 talloc_free(tmp_ctx);
1059 return -1;
1062 talloc_free(tmp_ctx);
1063 return 0;
1066 int32_t ctdb_control_disable_script(struct ctdb_context *ctdb, TDB_DATA indata)
1068 const char *script;
1069 struct stat st;
1070 char *filename;
1071 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1073 script = (char *)indata.dptr;
1074 if (indata.dsize == 0) {
1075 DEBUG(DEBUG_ERR,(__location__ " No script specified.\n"));
1076 talloc_free(tmp_ctx);
1077 return -1;
1079 if (indata.dptr[indata.dsize - 1] != '\0') {
1080 DEBUG(DEBUG_ERR,(__location__ " String is not null terminated.\n"));
1081 talloc_free(tmp_ctx);
1082 return -1;
1084 if (index(script,'/') != NULL) {
1085 DEBUG(DEBUG_ERR,(__location__ " Script name contains '/'. Failed to disable script %s\n", script));
1086 talloc_free(tmp_ctx);
1087 return -1;
1091 if (stat(ctdb->event_script_dir, &st) != 0 &&
1092 errno == ENOENT) {
1093 DEBUG(DEBUG_CRIT,("No event script directory found at '%s'\n", ctdb->event_script_dir));
1094 talloc_free(tmp_ctx);
1095 return -1;
1099 filename = talloc_asprintf(tmp_ctx, "%s/%s", ctdb->event_script_dir, script);
1100 if (filename == NULL) {
1101 DEBUG(DEBUG_ERR,(__location__ " Failed to create script path\n"));
1102 talloc_free(tmp_ctx);
1103 return -1;
1106 if (stat(filename, &st) != 0) {
1107 DEBUG(DEBUG_ERR,("Could not stat event script %s. Failed to disable script.\n", filename));
1108 talloc_free(tmp_ctx);
1109 return -1;
1112 if (chmod(filename, st.st_mode & ~(S_IXUSR|S_IXGRP|S_IXOTH)) == -1) {
1113 DEBUG(DEBUG_ERR,("Could not chmod %s. Failed to disable script.\n", filename));
1114 talloc_free(tmp_ctx);
1115 return -1;
1118 talloc_free(tmp_ctx);
1119 return 0;