ctdb-daemon: Replace ctdb_fork_with_logging with ctdb_vfork_with_logging (part 1)
[Samba.git] / ctdb / server / eventscript.c
blob2c5a61271512329c0a3531ee8e5500432c37b80b
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);
33 ctdbd sends us a SIGTERM when we should die.
35 static void sigterm(int sig)
37 pid_t pid;
39 /* all the child processes will be running in the same process group */
40 pid = getpgrp();
41 if (pid == -1) {
42 kill(-getpid(), SIGKILL);
43 } else {
44 kill(-pid, SIGKILL);
46 _exit(1);
49 /* This is attached to the event script state. */
50 struct event_script_callback {
51 struct event_script_callback *next, *prev;
52 struct ctdb_context *ctdb;
54 /* Warning: this can free us! */
55 void (*fn)(struct ctdb_context *, int, void *);
56 void *private_data;
60 struct ctdb_event_script_state {
61 struct ctdb_context *ctdb;
62 struct event_script_callback *callback;
63 pid_t child;
64 int fd[2];
65 enum ctdb_eventscript_call call;
66 const char *options;
67 struct timeval timeout;
69 unsigned int current;
70 struct ctdb_scripts_wire *scripts;
73 static struct ctdb_script_wire *get_current_script(struct ctdb_event_script_state *state)
75 return &state->scripts->scripts[state->current];
78 /* called from ctdb_logging when we have received output on STDERR from
79 * one of the eventscripts
81 static void log_event_script_output(const char *str, uint16_t len, void *p)
83 struct ctdb_event_script_state *state
84 = talloc_get_type(p, struct ctdb_event_script_state);
85 struct ctdb_script_wire *current;
86 unsigned int slen, min;
88 /* We may have been aborted to run something else. Discard */
89 if (state->scripts == NULL) {
90 return;
93 current = get_current_script(state);
95 /* Append, but don't overfill buffer. It starts zero-filled. */
96 slen = strlen(current->output);
97 min = MIN(len, sizeof(current->output) - slen - 1);
99 memcpy(current->output + slen, str, min);
102 int32_t ctdb_control_get_event_script_status(struct ctdb_context *ctdb,
103 uint32_t call_type,
104 TDB_DATA *outdata)
106 if (call_type >= CTDB_EVENT_MAX) {
107 return -1;
110 if (ctdb->last_status[call_type] == NULL) {
111 /* If it's never been run, return nothing so they can tell. */
112 outdata->dsize = 0;
113 } else {
114 outdata->dsize = talloc_get_size(ctdb->last_status[call_type]);
115 outdata->dptr = (uint8_t *)ctdb->last_status[call_type];
117 return 0;
120 struct ctdb_script_tree_item {
121 const char *name;
122 int error;
125 /* Return true if OK, otherwise set errno. */
126 static bool check_executable(const char *dir, const char *name)
128 char *full;
129 struct stat st;
131 full = talloc_asprintf(NULL, "%s/%s", dir, name);
132 if (!full)
133 return false;
135 if (stat(full, &st) != 0) {
136 DEBUG(DEBUG_ERR,("Could not stat event script %s: %s\n",
137 full, strerror(errno)));
138 talloc_free(full);
139 return false;
142 if (!(st.st_mode & S_IXUSR)) {
143 DEBUG(DEBUG_DEBUG,("Event script %s is not executable. Ignoring this event script\n", full));
144 errno = ENOEXEC;
145 talloc_free(full);
146 return false;
149 talloc_free(full);
150 return true;
153 static struct ctdb_scripts_wire *ctdb_get_script_list(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx)
155 DIR *dir;
156 struct dirent *de;
157 struct stat st;
158 trbt_tree_t *tree;
159 struct ctdb_scripts_wire *scripts;
160 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
161 struct ctdb_script_tree_item *tree_item;
162 int count;
165 the service specific event scripts
167 if (stat(ctdb->event_script_dir, &st) != 0 &&
168 errno == ENOENT) {
169 DEBUG(DEBUG_CRIT,("No event script directory found at '%s'\n", ctdb->event_script_dir));
170 talloc_free(tmp_ctx);
171 return NULL;
174 /* create a tree to store all the script names in */
175 tree = trbt_create(tmp_ctx, 0);
177 /* scan all directory entries and insert all valid scripts into the
178 tree
180 dir = opendir(ctdb->event_script_dir);
181 if (dir == NULL) {
182 DEBUG(DEBUG_CRIT,("Failed to open event script directory '%s'\n", ctdb->event_script_dir));
183 talloc_free(tmp_ctx);
184 return NULL;
187 count = 0;
188 while ((de=readdir(dir)) != NULL) {
189 int namlen;
190 unsigned num;
192 namlen = strlen(de->d_name);
194 if (namlen < 3) {
195 continue;
198 if (de->d_name[namlen-1] == '~') {
199 /* skip files emacs left behind */
200 continue;
203 if (de->d_name[2] != '.') {
204 continue;
207 if (sscanf(de->d_name, "%02u.", &num) != 1) {
208 continue;
211 if (strlen(de->d_name) > MAX_SCRIPT_NAME) {
212 DEBUG(DEBUG_ERR,("Script name %s too long! %u chars max",
213 de->d_name, MAX_SCRIPT_NAME));
214 continue;
217 tree_item = talloc(tree, struct ctdb_script_tree_item);
218 if (tree_item == NULL) {
219 DEBUG(DEBUG_ERR, (__location__ " Failed to allocate new tree item\n"));
220 closedir(dir);
221 talloc_free(tmp_ctx);
222 return NULL;
225 tree_item->error = 0;
226 if (!check_executable(ctdb->event_script_dir, de->d_name)) {
227 tree_item->error = errno;
230 tree_item->name = talloc_strdup(tree_item, de->d_name);
231 if (tree_item->name == NULL) {
232 DEBUG(DEBUG_ERR,(__location__ " Failed to allocate script name.\n"));
233 closedir(dir);
234 talloc_free(tmp_ctx);
235 return NULL;
238 /* store the event script in the tree */
239 trbt_insert32(tree, (num<<16)|count++, tree_item);
241 closedir(dir);
243 /* Overallocates by one, but that's OK */
244 scripts = talloc_zero_size(tmp_ctx,
245 sizeof(*scripts)
246 + sizeof(scripts->scripts[0]) * count);
247 if (scripts == NULL) {
248 DEBUG(DEBUG_ERR, (__location__ " Failed to allocate scripts\n"));
249 talloc_free(tmp_ctx);
250 return NULL;
252 scripts->num_scripts = count;
254 for (count = 0; count < scripts->num_scripts; count++) {
255 tree_item = trbt_findfirstarray32(tree, 1);
257 strcpy(scripts->scripts[count].name, tree_item->name);
258 scripts->scripts[count].status = -tree_item->error;
260 /* remove this script from the tree */
261 talloc_free(tree_item);
264 talloc_steal(mem_ctx, scripts);
265 talloc_free(tmp_ctx);
266 return scripts;
269 static int child_setup(struct ctdb_context *ctdb)
271 if (setpgid(0,0) != 0) {
272 int ret = -errno;
273 DEBUG(DEBUG_ERR,("Failed to create process group for event scripts - %s\n",
274 strerror(errno)));
275 return ret;
278 signal(SIGTERM, sigterm);
279 return 0;
282 static char *child_command_string(struct ctdb_context *ctdb,
283 TALLOC_CTX *ctx,
284 const char *scriptname,
285 enum ctdb_eventscript_call call,
286 const char *options)
288 return talloc_asprintf(ctx, "%s/%s %s %s",
289 ctdb->event_script_dir,
290 scriptname,
291 ctdb_eventscript_call_names[call],
292 options);
295 static int child_run_one(struct ctdb_context *ctdb,
296 const char *scriptname, const char *cmdstr)
298 int ret;
300 ret = system(cmdstr);
301 /* if the system() call was successful, translate ret into the
302 return code from the command
304 if (ret != -1) {
305 ret = WEXITSTATUS(ret);
306 } else {
307 ret = -errno;
310 /* 127 could mean it does not exist, 126 non-executable. */
311 if (ret == 127 || ret == 126) {
312 /* Re-check it... */
313 if (!check_executable(ctdb->event_script_dir, scriptname)) {
314 DEBUG(DEBUG_ERR,("Script %s returned status %u. Someone just deleted it?\n",
315 cmdstr, ret));
316 ret = -errno;
319 return ret;
323 Actually run one event script
324 this function is called and run in the context of a forked child
325 which allows it to do blocking calls such as system()
327 static int child_run_script(struct ctdb_context *ctdb,
328 enum ctdb_eventscript_call call,
329 const char *options,
330 struct ctdb_script_wire *current)
332 char *cmdstr;
333 int ret;
334 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
336 ret = child_setup(ctdb);
337 if (ret != 0)
338 goto out;
340 cmdstr = child_command_string(ctdb, tmp_ctx,
341 current->name, call, options);
342 CTDB_NO_MEMORY(ctdb, cmdstr);
344 DEBUG(DEBUG_DEBUG,("Executing event script %s\n",cmdstr));
346 if (current->status) {
347 ret = current->status;
348 goto out;
351 ret = child_run_one(ctdb, current->name, cmdstr);
352 out:
353 talloc_free(tmp_ctx);
354 return ret;
357 /* There cannot be more than 10 arguments to command helper. */
358 #define MAX_HELPER_ARGS (10)
360 static bool child_helper_args(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
361 enum ctdb_eventscript_call call,
362 const char *options,
363 struct ctdb_script_wire *current, int fd,
364 int *argc, const char ***argv)
366 const char **tmp;
367 int n, i;
368 char *t, *saveptr, *opt;
370 tmp = talloc_array(mem_ctx, const char *, 10+1);
371 if (tmp == NULL) goto failed;
373 tmp[0] = talloc_asprintf(tmp, "%d", fd);
374 tmp[1] = talloc_asprintf(tmp, "%s/%s", ctdb->event_script_dir, current->name);
375 tmp[2] = talloc_asprintf(tmp, "%s", ctdb_eventscript_call_names[call]);
376 n = 3;
378 /* Split options into individual arguments */
379 opt = talloc_strdup(mem_ctx, options);
380 if (opt == NULL) {
381 goto failed;
384 t = strtok_r(opt, " ", &saveptr);
385 while (t != NULL) {
386 tmp[n++] = talloc_strdup(tmp, t);
387 if (n > MAX_HELPER_ARGS) {
388 goto args_failed;
390 t = strtok_r(NULL, " ", &saveptr);
393 for (i=0; i<n; i++) {
394 if (tmp[i] == NULL) {
395 goto failed;
399 /* Last argument should be NULL */
400 tmp[n++] = NULL;
402 *argc = n;
403 *argv = tmp;
404 return true;
407 args_failed:
408 DEBUG(DEBUG_ERR, (__location__ " too many arguments '%s' to eventscript '%s'\n",
409 options, ctdb_eventscript_call_names[call]));
411 failed:
412 if (tmp) {
413 talloc_free(tmp);
415 return false;
419 static void ctdb_event_script_handler(struct event_context *ev, struct fd_event *fde,
420 uint16_t flags, void *p);
422 static const char *helper_prog = NULL;
424 static int fork_child_for_script(struct ctdb_context *ctdb,
425 struct ctdb_event_script_state *state)
427 int r;
428 struct tevent_fd *fde;
429 struct ctdb_script_wire *current = get_current_script(state);
430 int argc;
431 const char **argv;
432 static const char *helper = BINDIR "/ctdb_event_helper";
434 if (helper_prog == NULL) {
435 const char *t = getenv("CTDB_EVENT_HELPER");
436 if (t != NULL) {
437 helper_prog = t;
438 } else {
439 helper_prog = helper;
443 current->start = timeval_current();
445 r = pipe(state->fd);
446 if (r != 0) {
447 DEBUG(DEBUG_ERR, (__location__ " pipe failed for child eventscript process\n"));
448 return -errno;
451 /* Arguments for helper */
452 if (!child_helper_args(state, ctdb, state->call, state->options, current,
453 state->fd[1], &argc, &argv)) {
454 DEBUG(DEBUG_ERR, (__location__ " failed to create arguments for eventscript helper\n"));
455 r = -ENOMEM;
456 close(state->fd[0]);
457 close(state->fd[1]);
458 return r;
461 if (!ctdb_vfork_with_logging(state, ctdb, current->name,
462 helper_prog, argc, argv,
463 log_event_script_output,
464 state, &state->child)) {
465 talloc_free(argv);
466 r = -errno;
467 close(state->fd[0]);
468 close(state->fd[1]);
469 return r;
472 talloc_free(argv);
474 close(state->fd[1]);
475 set_close_on_exec(state->fd[0]);
477 /* Set ourselves up to be called when that's done. */
478 fde = event_add_fd(ctdb->ev, state, state->fd[0], EVENT_FD_READ,
479 ctdb_event_script_handler, state);
480 tevent_fd_set_auto_close(fde);
482 return 0;
486 Summarize status of this run of scripts.
488 static int script_status(struct ctdb_scripts_wire *scripts)
490 unsigned int i;
492 for (i = 0; i < scripts->num_scripts; i++) {
493 switch (scripts->scripts[i].status) {
494 case -ENOENT:
495 case -ENOEXEC:
496 /* Disabled or missing; that's OK. */
497 break;
498 case 0:
499 /* No problem. */
500 break;
501 default:
502 return scripts->scripts[i].status;
506 /* All OK! */
507 return 0;
510 /* called when child is finished */
511 static void ctdb_event_script_handler(struct event_context *ev, struct fd_event *fde,
512 uint16_t flags, void *p)
514 struct ctdb_event_script_state *state =
515 talloc_get_type(p, struct ctdb_event_script_state);
516 struct ctdb_script_wire *current = get_current_script(state);
517 struct ctdb_context *ctdb = state->ctdb;
518 int r, status;
520 if (ctdb == NULL) {
521 DEBUG(DEBUG_ERR,("Eventscript finished but ctdb is NULL\n"));
522 return;
525 r = read(state->fd[0], &current->status, sizeof(current->status));
526 if (r < 0) {
527 current->status = -errno;
528 } else if (r != sizeof(current->status)) {
529 current->status = -EIO;
532 current->finished = timeval_current();
533 /* valgrind gets overloaded if we run next script as it's still doing
534 * post-execution analysis, so kill finished child here. */
535 if (ctdb->valgrinding) {
536 ctdb_kill(ctdb, state->child, SIGKILL);
539 state->child = 0;
541 status = script_status(state->scripts);
543 /* Aborted or finished all scripts? We're done. */
544 if (status != 0 || state->current+1 == state->scripts->num_scripts) {
545 DEBUG(DEBUG_INFO,(__location__ " Eventscript %s %s finished with state %d\n",
546 ctdb_eventscript_call_names[state->call], state->options, status));
548 ctdb->event_script_timeouts = 0;
549 talloc_free(state);
550 return;
553 /* Forget about that old fd. */
554 talloc_free(fde);
556 /* Next script! */
557 state->current++;
558 current++;
559 current->status = fork_child_for_script(ctdb, state);
560 if (current->status != 0) {
561 /* This calls the callback. */
562 talloc_free(state);
566 struct debug_hung_script_state {
567 struct ctdb_context *ctdb;
568 pid_t child;
569 enum ctdb_eventscript_call call;
572 static int debug_hung_script_state_destructor(struct debug_hung_script_state *state)
574 if (state->child) {
575 ctdb_kill(state->ctdb, state->child, SIGKILL);
577 return 0;
580 static void debug_hung_script_timeout(struct tevent_context *ev, struct tevent_timer *te,
581 struct timeval t, void *p)
583 struct debug_hung_script_state *state =
584 talloc_get_type(p, struct debug_hung_script_state);
586 talloc_free(state);
589 static void debug_hung_script_done(struct tevent_context *ev, struct tevent_fd *fde,
590 uint16_t flags, void *p)
592 struct debug_hung_script_state *state =
593 talloc_get_type(p, struct debug_hung_script_state);
595 talloc_free(state);
598 static void ctdb_run_debug_hung_script(struct ctdb_context *ctdb, struct debug_hung_script_state *state)
600 pid_t pid;
601 const char * debug_hung_script = ETCDIR "/ctdb/debug-hung-script.sh";
602 int fd[2];
603 struct tevent_timer *ttimer;
604 struct tevent_fd *tfd;
606 if (pipe(fd) < 0) {
607 DEBUG(DEBUG_ERR,("Failed to create pipe fd for debug hung script\n"));
608 return;
611 if (!ctdb_fork_with_logging(ctdb, ctdb, "Hung script", NULL, NULL, &pid)) {
612 DEBUG(DEBUG_ERR,("Failed to fork a child process with logging to track hung event script\n"));
613 close(fd[0]);
614 close(fd[1]);
615 return;
617 if (pid == -1) {
618 DEBUG(DEBUG_ERR,("Fork for debug script failed : %s\n",
619 strerror(errno)));
620 close(fd[0]);
621 close(fd[1]);
622 return;
624 if (pid == 0) {
625 char *buf;
627 ctdb_set_process_name("ctdb_debug_hung_script");
628 if (getenv("CTDB_DEBUG_HUNG_SCRIPT") != NULL) {
629 debug_hung_script = getenv("CTDB_DEBUG_HUNG_SCRIPT");
632 close(fd[0]);
634 buf = talloc_asprintf(NULL, "%s %d %s",
635 debug_hung_script, state->child,
636 ctdb_eventscript_call_names[state->call]);
637 system(buf);
638 talloc_free(buf);
640 _exit(0);
643 close(fd[1]);
645 ttimer = tevent_add_timer(ctdb->ev, state,
646 timeval_current_ofs(ctdb->tunable.script_timeout, 0),
647 debug_hung_script_timeout, state);
648 if (ttimer == NULL) {
649 close(fd[0]);
650 return;
653 tfd = tevent_add_fd(ctdb->ev, state, fd[0], EVENT_FD_READ,
654 debug_hung_script_done, state);
655 if (tfd == NULL) {
656 talloc_free(ttimer);
657 close(fd[0]);
658 return;
660 tevent_fd_set_auto_close(tfd);
663 /* called when child times out */
664 static void ctdb_event_script_timeout(struct event_context *ev, struct timed_event *te,
665 struct timeval t, void *p)
667 struct ctdb_event_script_state *state = talloc_get_type(p, struct ctdb_event_script_state);
668 struct ctdb_context *ctdb = state->ctdb;
669 struct ctdb_script_wire *current = get_current_script(state);
670 struct debug_hung_script_state *debug_state;
672 DEBUG(DEBUG_ERR,("Event script '%s %s %s' timed out after %.1fs, count: %u, pid: %d\n",
673 current->name, ctdb_eventscript_call_names[state->call], state->options,
674 timeval_elapsed(&current->start),
675 ctdb->event_script_timeouts, state->child));
677 /* ignore timeouts for these events */
678 switch (state->call) {
679 case CTDB_EVENT_START_RECOVERY:
680 case CTDB_EVENT_RECOVERED:
681 case CTDB_EVENT_TAKE_IP:
682 case CTDB_EVENT_RELEASE_IP:
683 state->scripts->scripts[state->current].status = 0;
684 DEBUG(DEBUG_ERR,("Ignoring hung script for %s call %d\n", state->options, state->call));
685 break;
686 default:
687 state->scripts->scripts[state->current].status = -ETIME;
690 debug_state = talloc_zero(ctdb, struct debug_hung_script_state);
691 if (debug_state == NULL) {
692 talloc_free(state);
693 return;
696 /* Save information useful for running debug hung script, so
697 * eventscript state can be freed.
699 debug_state->ctdb = ctdb;
700 debug_state->child = state->child;
701 debug_state->call = state->call;
703 /* This destructor will actually kill the hung event script */
704 talloc_set_destructor(debug_state, debug_hung_script_state_destructor);
706 state->child = 0;
707 talloc_free(state);
709 ctdb_run_debug_hung_script(ctdb, debug_state);
713 destroy an event script: kill it if ->child != 0.
715 static int event_script_destructor(struct ctdb_event_script_state *state)
717 int status;
718 struct event_script_callback *callback;
720 if (state->child) {
721 DEBUG(DEBUG_ERR,(__location__ " Sending SIGTERM to child pid:%d\n", state->child));
723 if (ctdb_kill(state->ctdb, state->child, SIGTERM) != 0) {
724 DEBUG(DEBUG_ERR,("Failed to kill child process for eventscript, errno %s(%d)\n", strerror(errno), errno));
728 /* If we were the current monitor, we no longer are. */
729 if (state->ctdb->current_monitor == state) {
730 state->ctdb->current_monitor = NULL;
733 /* Save our scripts as the last executed status, if we have them.
734 * See ctdb_event_script_callback_v where we abort monitor event. */
735 if (state->scripts) {
736 talloc_free(state->ctdb->last_status[state->call]);
737 state->ctdb->last_status[state->call] = state->scripts;
738 if (state->current < state->ctdb->last_status[state->call]->num_scripts) {
739 state->ctdb->last_status[state->call]->num_scripts = state->current+1;
743 /* Use last status as result, or "OK" if none. */
744 if (state->ctdb->last_status[state->call]) {
745 status = script_status(state->ctdb->last_status[state->call]);
746 } else {
747 status = 0;
750 /* This is allowed to free us; talloc will prevent double free anyway,
751 * but beware if you call this outside the destructor!
752 * the callback hangs off a different context so we walk the list
753 * of "active" callbacks until we find the one state points to.
754 * if we cant find it it means the callback has been removed.
756 for (callback = state->ctdb->script_callbacks; callback != NULL; callback = callback->next) {
757 if (callback == state->callback) {
758 break;
762 state->callback = NULL;
764 if (callback) {
765 /* Make sure destructor doesn't free itself! */
766 talloc_steal(NULL, callback);
767 callback->fn(state->ctdb, status, callback->private_data);
768 talloc_free(callback);
771 return 0;
774 static unsigned int count_words(const char *options)
776 unsigned int words = 0;
778 options += strspn(options, " \t");
779 while (*options) {
780 words++;
781 options += strcspn(options, " \t");
782 options += strspn(options, " \t");
784 return words;
787 static bool check_options(enum ctdb_eventscript_call call, const char *options)
789 switch (call) {
790 /* These all take no arguments. */
791 case CTDB_EVENT_INIT:
792 case CTDB_EVENT_SETUP:
793 case CTDB_EVENT_STARTUP:
794 case CTDB_EVENT_START_RECOVERY:
795 case CTDB_EVENT_RECOVERED:
796 case CTDB_EVENT_MONITOR:
797 case CTDB_EVENT_SHUTDOWN:
798 case CTDB_EVENT_IPREALLOCATED:
799 return count_words(options) == 0;
801 case CTDB_EVENT_TAKE_IP: /* interface, IP address, netmask bits. */
802 case CTDB_EVENT_RELEASE_IP:
803 return count_words(options) == 3;
805 case CTDB_EVENT_UPDATE_IP: /* old interface, new interface, IP address, netmask bits. */
806 return count_words(options) == 4;
808 default:
809 DEBUG(DEBUG_ERR,(__location__ "Unknown ctdb_eventscript_call %u\n", call));
810 return false;
814 static int remove_callback(struct event_script_callback *callback)
816 DLIST_REMOVE(callback->ctdb->script_callbacks, callback);
817 return 0;
821 run the event script in the background, calling the callback when
822 finished
824 static int ctdb_event_script_callback_v(struct ctdb_context *ctdb,
825 const void *mem_ctx,
826 void (*callback)(struct ctdb_context *, int, void *),
827 void *private_data,
828 enum ctdb_eventscript_call call,
829 const char *fmt, va_list ap)
831 struct ctdb_event_script_state *state;
833 if (ctdb->recovery_mode != CTDB_RECOVERY_NORMAL) {
834 /* we guarantee that only some specifically allowed event scripts are run
835 while in recovery */
836 const enum ctdb_eventscript_call allowed_calls[] = {
837 CTDB_EVENT_INIT,
838 CTDB_EVENT_SETUP,
839 CTDB_EVENT_START_RECOVERY,
840 CTDB_EVENT_SHUTDOWN,
841 CTDB_EVENT_RELEASE_IP,
842 CTDB_EVENT_IPREALLOCATED,
844 int i;
845 for (i=0;i<ARRAY_SIZE(allowed_calls);i++) {
846 if (call == allowed_calls[i]) break;
848 if (i == ARRAY_SIZE(allowed_calls)) {
849 DEBUG(DEBUG_ERR,("Refusing to run event scripts call '%s' while in recovery\n",
850 ctdb_eventscript_call_names[call]));
851 return -1;
855 /* Kill off any running monitor events to run this event. */
856 if (ctdb->current_monitor) {
857 struct ctdb_event_script_state *ms = talloc_get_type(ctdb->current_monitor, struct ctdb_event_script_state);
859 /* Cancel current monitor callback state only if monitoring
860 * context ctdb->monitor->monitor_context has not been freed */
861 if (ms->callback != NULL && !ctdb_stopped_monitoring(ctdb)) {
862 ms->callback->fn(ctdb, -ECANCELED, ms->callback->private_data);
863 talloc_free(ms->callback);
866 /* Discard script status so we don't save to last_status */
867 talloc_free(ctdb->current_monitor->scripts);
868 ctdb->current_monitor->scripts = NULL;
869 talloc_free(ctdb->current_monitor);
870 ctdb->current_monitor = NULL;
873 state = talloc(ctdb->event_script_ctx, struct ctdb_event_script_state);
874 CTDB_NO_MEMORY(ctdb, state);
876 /* The callback isn't done if the context is freed. */
877 state->callback = talloc(mem_ctx, struct event_script_callback);
878 CTDB_NO_MEMORY(ctdb, state->callback);
879 DLIST_ADD(ctdb->script_callbacks, state->callback);
880 talloc_set_destructor(state->callback, remove_callback);
881 state->callback->ctdb = ctdb;
882 state->callback->fn = callback;
883 state->callback->private_data = private_data;
885 state->ctdb = ctdb;
886 state->call = call;
887 state->options = talloc_vasprintf(state, fmt, ap);
888 state->timeout = timeval_set(ctdb->tunable.script_timeout, 0);
889 state->scripts = NULL;
890 if (state->options == NULL) {
891 DEBUG(DEBUG_ERR, (__location__ " could not allocate state->options\n"));
892 talloc_free(state);
893 return -1;
895 if (!check_options(state->call, state->options)) {
896 DEBUG(DEBUG_ERR, ("Bad eventscript options '%s' for %s\n",
897 ctdb_eventscript_call_names[state->call], state->options));
898 talloc_free(state);
899 return -1;
902 DEBUG(DEBUG_INFO,(__location__ " Starting eventscript %s %s\n",
903 ctdb_eventscript_call_names[state->call],
904 state->options));
906 /* This is not a child of state, since we save it in destructor. */
907 state->scripts = ctdb_get_script_list(ctdb, ctdb);
908 if (state->scripts == NULL) {
909 talloc_free(state);
910 return -1;
912 state->current = 0;
913 state->child = 0;
915 if (call == CTDB_EVENT_MONITOR) {
916 ctdb->current_monitor = state;
919 talloc_set_destructor(state, event_script_destructor);
921 /* Nothing to do? */
922 if (state->scripts->num_scripts == 0) {
923 talloc_free(state);
924 return 0;
927 state->scripts->scripts[0].status = fork_child_for_script(ctdb, state);
928 if (state->scripts->scripts[0].status != 0) {
929 /* Callback is called from destructor, with fail result. */
930 talloc_free(state);
931 return 0;
934 if (!timeval_is_zero(&state->timeout)) {
935 event_add_timed(ctdb->ev, state, timeval_current_ofs(state->timeout.tv_sec, state->timeout.tv_usec), ctdb_event_script_timeout, state);
936 } else {
937 DEBUG(DEBUG_ERR, (__location__ " eventscript %s %s called with no timeout\n",
938 ctdb_eventscript_call_names[state->call],
939 state->options));
942 return 0;
947 run the event script in the background, calling the callback when
948 finished. If mem_ctx is freed, callback will never be called.
950 int ctdb_event_script_callback(struct ctdb_context *ctdb,
951 TALLOC_CTX *mem_ctx,
952 void (*callback)(struct ctdb_context *, int, void *),
953 void *private_data,
954 enum ctdb_eventscript_call call,
955 const char *fmt, ...)
957 va_list ap;
958 int ret;
960 va_start(ap, fmt);
961 ret = ctdb_event_script_callback_v(ctdb, mem_ctx, callback, private_data, call, fmt, ap);
962 va_end(ap);
964 return ret;
968 struct callback_status {
969 bool done;
970 int status;
974 called when ctdb_event_script() finishes
976 static void event_script_callback(struct ctdb_context *ctdb, int status, void *private_data)
978 struct callback_status *s = (struct callback_status *)private_data;
979 s->done = true;
980 s->status = status;
984 run the event script, waiting for it to complete. Used when the caller
985 doesn't want to continue till the event script has finished.
987 int ctdb_event_script_args(struct ctdb_context *ctdb, enum ctdb_eventscript_call call,
988 const char *fmt, ...)
990 va_list ap;
991 int ret;
992 struct callback_status status;
994 va_start(ap, fmt);
995 ret = ctdb_event_script_callback_v(ctdb, ctdb,
996 event_script_callback, &status, call, fmt, ap);
997 va_end(ap);
998 if (ret != 0) {
999 return ret;
1002 status.status = -1;
1003 status.done = false;
1005 while (status.done == false && event_loop_once(ctdb->ev) == 0) /* noop */;
1007 if (status.status == -ETIME) {
1008 DEBUG(DEBUG_ERR, (__location__ " eventscript for '%s' timedout."
1009 " Immediately banning ourself for %d seconds\n",
1010 ctdb_eventscript_call_names[call],
1011 ctdb->tunable.recovery_ban_period));
1013 /* Don't ban self if CTDB is starting up or shutting down */
1014 if (call != CTDB_EVENT_INIT && call != CTDB_EVENT_SHUTDOWN) {
1015 ctdb_ban_self(ctdb);
1019 return status.status;
1022 int ctdb_event_script(struct ctdb_context *ctdb, enum ctdb_eventscript_call call)
1024 /* GCC complains about empty format string, so use %s and "". */
1025 return ctdb_event_script_args(ctdb, call, "%s", "");
1028 struct eventscript_callback_state {
1029 struct ctdb_req_control *c;
1033 called when a forced eventscript run has finished
1035 static void run_eventscripts_callback(struct ctdb_context *ctdb, int status,
1036 void *private_data)
1038 struct eventscript_callback_state *state =
1039 talloc_get_type(private_data, struct eventscript_callback_state);
1041 ctdb_enable_monitoring(ctdb);
1043 if (status != 0) {
1044 DEBUG(DEBUG_ERR,(__location__ " Failed to run eventscripts\n"));
1047 ctdb_request_control_reply(ctdb, state->c, NULL, status, NULL);
1048 /* This will free the struct ctdb_event_script_state we are in! */
1049 talloc_free(state);
1050 return;
1054 /* Returns rest of string, or NULL if no match. */
1055 static const char *get_call(const char *p, enum ctdb_eventscript_call *call)
1057 unsigned int len;
1059 /* Skip any initial whitespace. */
1060 p += strspn(p, " \t");
1062 /* See if we match any. */
1063 for (*call = 0; *call < CTDB_EVENT_MAX; (*call)++) {
1064 len = strlen(ctdb_eventscript_call_names[*call]);
1065 if (strncmp(p, ctdb_eventscript_call_names[*call], len) == 0) {
1066 /* If end of string or whitespace, we're done. */
1067 if (strcspn(p + len, " \t") == 0) {
1068 return p + len;
1072 return NULL;
1076 A control to force running of the eventscripts from the ctdb client tool
1078 int32_t ctdb_run_eventscripts(struct ctdb_context *ctdb,
1079 struct ctdb_req_control *c,
1080 TDB_DATA indata, bool *async_reply)
1082 int ret;
1083 struct eventscript_callback_state *state;
1084 const char *options;
1085 enum ctdb_eventscript_call call;
1087 /* Figure out what call they want. */
1088 options = get_call((const char *)indata.dptr, &call);
1089 if (!options) {
1090 DEBUG(DEBUG_ERR, (__location__ " Invalid event name \"%s\"\n", (const char *)indata.dptr));
1091 return -1;
1094 if (ctdb->recovery_mode != CTDB_RECOVERY_NORMAL) {
1095 DEBUG(DEBUG_ERR, (__location__ " Aborted running eventscript \"%s\" while in RECOVERY mode\n", indata.dptr));
1096 return -1;
1099 state = talloc(ctdb->event_script_ctx, struct eventscript_callback_state);
1100 CTDB_NO_MEMORY(ctdb, state);
1102 state->c = talloc_steal(state, c);
1104 DEBUG(DEBUG_NOTICE,("Running eventscripts with arguments %s\n", indata.dptr));
1106 ctdb_disable_monitoring(ctdb);
1108 ret = ctdb_event_script_callback(ctdb,
1109 state, run_eventscripts_callback, state,
1110 call, "%s", options);
1112 if (ret != 0) {
1113 ctdb_enable_monitoring(ctdb);
1114 DEBUG(DEBUG_ERR,(__location__ " Failed to run eventscripts with arguments %s\n", indata.dptr));
1115 talloc_free(state);
1116 return -1;
1119 /* tell ctdb_control.c that we will be replying asynchronously */
1120 *async_reply = true;
1122 return 0;
1127 int32_t ctdb_control_enable_script(struct ctdb_context *ctdb, TDB_DATA indata)
1129 const char *script;
1130 struct stat st;
1131 char *filename;
1132 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1134 script = (char *)indata.dptr;
1135 if (indata.dsize == 0) {
1136 DEBUG(DEBUG_ERR,(__location__ " No script specified.\n"));
1137 talloc_free(tmp_ctx);
1138 return -1;
1140 if (indata.dptr[indata.dsize - 1] != '\0') {
1141 DEBUG(DEBUG_ERR,(__location__ " String is not null terminated.\n"));
1142 talloc_free(tmp_ctx);
1143 return -1;
1145 if (index(script,'/') != NULL) {
1146 DEBUG(DEBUG_ERR,(__location__ " Script name contains '/'. Failed to enable script %s\n", script));
1147 talloc_free(tmp_ctx);
1148 return -1;
1152 if (stat(ctdb->event_script_dir, &st) != 0 &&
1153 errno == ENOENT) {
1154 DEBUG(DEBUG_CRIT,("No event script directory found at '%s'\n", ctdb->event_script_dir));
1155 talloc_free(tmp_ctx);
1156 return -1;
1160 filename = talloc_asprintf(tmp_ctx, "%s/%s", ctdb->event_script_dir, script);
1161 if (filename == NULL) {
1162 DEBUG(DEBUG_ERR,(__location__ " Failed to create script path\n"));
1163 talloc_free(tmp_ctx);
1164 return -1;
1167 if (stat(filename, &st) != 0) {
1168 DEBUG(DEBUG_ERR,("Could not stat event script %s. Failed to enable script.\n", filename));
1169 talloc_free(tmp_ctx);
1170 return -1;
1173 if (chmod(filename, st.st_mode | S_IXUSR) == -1) {
1174 DEBUG(DEBUG_ERR,("Could not chmod %s. Failed to enable script.\n", filename));
1175 talloc_free(tmp_ctx);
1176 return -1;
1179 talloc_free(tmp_ctx);
1180 return 0;
1183 int32_t ctdb_control_disable_script(struct ctdb_context *ctdb, TDB_DATA indata)
1185 const char *script;
1186 struct stat st;
1187 char *filename;
1188 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1190 script = (char *)indata.dptr;
1191 if (indata.dsize == 0) {
1192 DEBUG(DEBUG_ERR,(__location__ " No script specified.\n"));
1193 talloc_free(tmp_ctx);
1194 return -1;
1196 if (indata.dptr[indata.dsize - 1] != '\0') {
1197 DEBUG(DEBUG_ERR,(__location__ " String is not null terminated.\n"));
1198 talloc_free(tmp_ctx);
1199 return -1;
1201 if (index(script,'/') != NULL) {
1202 DEBUG(DEBUG_ERR,(__location__ " Script name contains '/'. Failed to disable script %s\n", script));
1203 talloc_free(tmp_ctx);
1204 return -1;
1208 if (stat(ctdb->event_script_dir, &st) != 0 &&
1209 errno == ENOENT) {
1210 DEBUG(DEBUG_CRIT,("No event script directory found at '%s'\n", ctdb->event_script_dir));
1211 talloc_free(tmp_ctx);
1212 return -1;
1216 filename = talloc_asprintf(tmp_ctx, "%s/%s", ctdb->event_script_dir, script);
1217 if (filename == NULL) {
1218 DEBUG(DEBUG_ERR,(__location__ " Failed to create script path\n"));
1219 talloc_free(tmp_ctx);
1220 return -1;
1223 if (stat(filename, &st) != 0) {
1224 DEBUG(DEBUG_ERR,("Could not stat event script %s. Failed to disable script.\n", filename));
1225 talloc_free(tmp_ctx);
1226 return -1;
1229 if (chmod(filename, st.st_mode & ~(S_IXUSR|S_IXGRP|S_IXOTH)) == -1) {
1230 DEBUG(DEBUG_ERR,("Could not chmod %s. Failed to disable script.\n", filename));
1231 talloc_free(tmp_ctx);
1232 return -1;
1235 talloc_free(tmp_ctx);
1236 return 0;