Ignore any scripts that timesout for most events, except startup.
[sahlberg/ctdb.git] / server / eventscript.c
index b507ea33e9a5d37b0cffb027d41a189b13286621..9cd95378c1a6e8b94082bf1371ad54ac12afb0e9 100644 (file)
@@ -32,19 +32,6 @@ static struct {
        const char *script_running;
 } child_state;
 
-static const char *call_names[] = {
-       "startup",
-       "startrecovery",
-       "recovered",
-       "takeip",
-       "releaseip",
-       "stopped",
-       "monitor",
-       "status",
-       "shutdown",
-       "reload"
-};
-
 static void ctdb_event_script_timeout(struct event_context *ev, struct timed_event *te, struct timeval t, void *p);
 
 /*
@@ -61,7 +48,8 @@ static void sigterm(int sig)
        t = time(NULL);
 
        strftime(tbuf, sizeof(tbuf)-1, "%Y%m%d%H%M%S",  localtime(&t));
-       sprintf(buf, "pstree -p >/tmp/ctdb.event.%s.%d", tbuf, getpid());
+       sprintf(buf, "{ pstree -p; cat /proc/locks; ls -li /var/ctdb/ /var/ctdb/persistent; }"
+               " >/tmp/ctdb.event.%s.%d", tbuf, getpid());
        system(buf);
 
        DEBUG(DEBUG_ERR,("Logged timedout eventscript : %s\n", buf));
@@ -76,7 +64,6 @@ struct ctdb_event_script_state {
        pid_t child;
        /* Warning: this can free us! */
        void (*callback)(struct ctdb_context *, int, void *);
-       int cb_status;
        int fd[2];
        void *private_data;
        bool from_user;
@@ -84,168 +71,54 @@ struct ctdb_event_script_state {
        const char *options;
        struct timeval timeout;
 
-       struct ctdb_script *scripts, *current;
+       unsigned int current;
+       struct ctdb_scripts_wire *scripts;
 };
 
-struct ctdb_script {
-       struct ctdb_script *next;
-       const char *name;
-       struct timeval start;
-       struct timeval finished;
-       int32_t status;
-       char *output;
-};
+static struct ctdb_script_wire *get_current_script(struct ctdb_event_script_state *state)
+{
+       return &state->scripts->scripts[state->current];
+}
 
 /* called from ctdb_logging when we have received output on STDERR from
  * one of the eventscripts
  */
 static void log_event_script_output(const char *str, uint16_t len, void *p)
 {
-       struct ctdb_script *script = talloc_get_type(p, struct ctdb_script);
-
-       script->output = talloc_asprintf_append(script->output, "%*.*s", len, len, str);
-}
-
-/* starting a new monitor event */
-static int32_t ctdb_control_event_script_init(struct ctdb_context *ctdb)
-{
-       DEBUG(DEBUG_INFO, ("event script init called\n"));
-
-       if (ctdb->current_monitor == NULL) {
-               DEBUG(DEBUG_ERR,(__location__ " current_monitor_status_ctx is NULL when initing script\n"));
-               return -1;
-       }
-
-       return 0;
-}
-
-
-/* starting a new child to run a monitor event script */
-static int32_t ctdb_control_event_script_start(struct ctdb_context *ctdb, const char *name)
-{
-       struct ctdb_script *script;
-
-       DEBUG(DEBUG_INFO, ("event script start called : %s\n", name));
-
-       if (ctdb->current_monitor == NULL) {
-               DEBUG(DEBUG_ERR,(__location__ " current_monitor_status_ctx is NULL when starting script\n"));
-               return -1;
-       }
-
-       script = ctdb->current_monitor->current;
-       if (script == NULL) {
-               DEBUG(DEBUG_ERR,(__location__ " current script for monitor is NULL for %s\n", name));
-               return -1;
-       }
-
-       script->start = timeval_current();
-       return 0;
-}
-
-/* finished a child running a monitor event script */
-static int32_t ctdb_control_event_script_stop(struct ctdb_context *ctdb, int res)
-{
-       struct ctdb_script *script;
-
-       if (ctdb->current_monitor == NULL) {
-               DEBUG(DEBUG_ERR,(__location__ " current_monitor_status_ctx is NULL when script finished\n"));
-               return -1;
-       }
-
-       script = ctdb->current_monitor->current;
-       if (script == NULL) {
-               DEBUG(DEBUG_ERR,(__location__ " script is NULL when the script had finished\n"));
-               return -1;
-       }
-
-       script->finished = timeval_current();
-       script->status   = res;
-
-       DEBUG(DEBUG_INFO, ("event script stop called for script:%s duration:%.1f status:%d\n", script->name, timeval_elapsed(&script->start), (int)res));
-
-       return 0;
-}
-
-static struct ctdb_monitoring_wire *marshall_monitoring_scripts(TALLOC_CTX *mem_ctx, struct ctdb_monitoring_wire *monitoring_scripts, struct ctdb_script *script, struct ctdb_script *terminus)
-{
-       struct ctdb_monitoring_script_wire script_wire;
-       size_t size;
+       struct ctdb_event_script_state *state
+               = talloc_get_type(p, struct ctdb_event_script_state);
+       struct ctdb_script_wire *current;
+       unsigned int slen, min;
 
-       if (script == terminus) {
-               return monitoring_scripts;
+       /* We may have been aborted to run something else.  Discard */
+       if (state->scripts == NULL) {
+               return;
        }
 
-       bzero(&script_wire, sizeof(struct ctdb_monitoring_script_wire));
-       strncpy(script_wire.name, script->name, MAX_SCRIPT_NAME);
-       script_wire.start    = script->start;
-       script_wire.finished = script->finished;
-       script_wire.status   = script->status;
-       if (script->output != NULL) {
-               strncpy(script_wire.output, script->output, MAX_SCRIPT_OUTPUT);
-       }
+       current = get_current_script(state);
 
-       size = talloc_get_size(monitoring_scripts);
-       monitoring_scripts = talloc_realloc_size(mem_ctx, monitoring_scripts, size + sizeof(struct ctdb_monitoring_script_wire));
-       if (monitoring_scripts == NULL) {
-               DEBUG(DEBUG_ERR,(__location__ " Failed to talloc_resize monitoring_scripts blob\n"));
-               return NULL;
-       }
+       /* Append, but don't overfill buffer.  It starts zero-filled. */
+       slen = strlen(current->output);
+       min = MIN(len, sizeof(current->output) - slen - 1);
 
-       memcpy(&monitoring_scripts->scripts[monitoring_scripts->num_scripts], &script_wire, sizeof(script_wire));
-       monitoring_scripts->num_scripts++;
-       
-       monitoring_scripts = marshall_monitoring_scripts(mem_ctx, monitoring_scripts, script->next, terminus);
-       if (monitoring_scripts == NULL) {
-               return NULL;
-       }
-
-       return monitoring_scripts;
+       memcpy(current->output + slen, str, min);
 }
 
-/* called when all event script child processes are done */
-static int32_t ctdb_control_event_script_finished(struct ctdb_context *ctdb)
+int32_t ctdb_control_get_event_script_status(struct ctdb_context *ctdb,
+                                            uint32_t call_type,
+                                            TDB_DATA *outdata)
 {
-       struct ctdb_script *terminus = NULL;
-
-       DEBUG(DEBUG_INFO, ("event script finished called\n"));
-
-       if (ctdb->current_monitor == NULL) {
-               DEBUG(DEBUG_ERR,(__location__ " script_status is NULL when monitoring event finished\n"));
+       if (call_type >= CTDB_EVENT_MAX) {
                return -1;
        }
 
-       talloc_free(ctdb->last_status);
-       ctdb->last_status = talloc_size(ctdb, offsetof(struct ctdb_monitoring_wire, scripts));
-       if (ctdb->last_status == NULL) {
-               DEBUG(DEBUG_ERR,(__location__ " failed to talloc last_status\n"));
-               return -1;
-       }
-
-       /* only report the finished ones */
-       if (ctdb->current_monitor->current != NULL) {
-               terminus = ctdb->current_monitor->current->next;
-       }
-
-       ctdb->last_status->num_scripts = 0;
-       ctdb->last_status = marshall_monitoring_scripts(ctdb, ctdb->last_status, ctdb->current_monitor->scripts, terminus);
-       talloc_free(ctdb->current_monitor->scripts);
-       ctdb->current_monitor->scripts = NULL;
-
-       return 0;
-}
-
-int32_t ctdb_control_get_event_script_status(struct ctdb_context *ctdb, TDB_DATA *outdata)
-{
-       struct ctdb_monitoring_wire *monitoring_scripts = ctdb->last_status;
-
-       if (monitoring_scripts == NULL) {
-               DEBUG(DEBUG_ERR,(__location__ " last_monitor_status_ctx is NULL when reading status\n"));
-               return -1;
+       if (ctdb->last_status[call_type] == NULL) {
+               /* If it's never been run, return nothing so they can tell. */
+               outdata->dsize = 0;
+       } else {
+               outdata->dsize = talloc_get_size(ctdb->last_status[call_type]);
+               outdata->dptr  = (uint8_t *)ctdb->last_status[call_type];
        }
-
-       outdata->dsize = talloc_get_size(monitoring_scripts);
-       outdata->dptr  = (uint8_t *)monitoring_scripts;
-
        return 0;
 }
 
@@ -272,7 +145,7 @@ static bool check_executable(const char *dir, const char *name)
        }
 
        if (!(st.st_mode & S_IXUSR)) {
-               DEBUG(DEBUG_INFO,("Event script %s is not executable. Ignoring this event script\n", full));
+               DEBUG(DEBUG_DEBUG,("Event script %s is not executable. Ignoring this event script\n", full));
                errno = ENOEXEC;
                talloc_free(full);
                return false;
@@ -282,13 +155,13 @@ static bool check_executable(const char *dir, const char *name)
        return true;
 }
 
-static struct ctdb_script *ctdb_get_script_list(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx)
+static struct ctdb_scripts_wire *ctdb_get_script_list(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx)
 {
        DIR *dir;
        struct dirent *de;
        struct stat st;
        trbt_tree_t *tree;
-       struct ctdb_script *head, *tail, *new_item;
+       struct ctdb_scripts_wire *scripts;
        TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
        struct ctdb_script_tree_item *tree_item;
        int count;
@@ -340,6 +213,12 @@ static struct ctdb_script *ctdb_get_script_list(struct ctdb_context *ctdb, TALLO
                        continue;
                }
 
+               if (strlen(de->d_name) > MAX_SCRIPT_NAME) {
+                       DEBUG(DEBUG_ERR,("Script name %s too long! %u chars max",
+                                        de->d_name, MAX_SCRIPT_NAME));
+                       continue;
+               }
+
                tree_item = talloc(tree, struct ctdb_script_tree_item);
                if (tree_item == NULL) {
                        DEBUG(DEBUG_ERR, (__location__ " Failed to allocate new tree item\n"));
@@ -364,48 +243,30 @@ static struct ctdb_script *ctdb_get_script_list(struct ctdb_context *ctdb, TALLO
        }
        closedir(dir);
 
+       /* Overallocates by one, but that's OK */
+       scripts = talloc_zero_size(tmp_ctx,
+                                  sizeof(*scripts)
+                                  + sizeof(scripts->scripts[0]) * count);
+       if (scripts == NULL) {
+               DEBUG(DEBUG_ERR, (__location__ " Failed to allocate scripts\n"));
+               talloc_free(tmp_ctx);
+               return NULL;
+       }
+       scripts->num_scripts = count;
 
-       head = NULL;
-       tail = NULL;
-
-       /* fetch the scripts from the tree one by one and add them to the linked
-          list
-        */
-       while ((tree_item=trbt_findfirstarray32(tree, 1)) != NULL) {
-
-               new_item = talloc(tmp_ctx, struct ctdb_script);
-               if (new_item == NULL) {
-                       DEBUG(DEBUG_ERR, (__location__ " Failed to allocate new list item\n"));
-                       talloc_free(tmp_ctx);
-                       return NULL;
-               }
-
-               new_item->next = NULL;
-               new_item->name = talloc_steal(new_item, tree_item->name);
-               new_item->status = -tree_item->error;
-               new_item->output = talloc_strdup(new_item, "");
-               if (new_item->output == NULL) {
-                       DEBUG(DEBUG_ERR, (__location__ " Failed to allocate new list item output\n"));
-                       talloc_free(tmp_ctx);
-                       return NULL;
-               }
-
-               if (head == NULL) {
-                       head = new_item;
-                       tail = new_item;
-               } else {
-                       tail->next = new_item;
-                       tail = new_item;
-               }
+       for (count = 0; count < scripts->num_scripts; count++) {
+               tree_item = trbt_findfirstarray32(tree, 1);
 
-               talloc_steal(mem_ctx, new_item);
+               strcpy(scripts->scripts[count].name, tree_item->name);
+               scripts->scripts[count].status = -tree_item->error;
 
                /* remove this script from the tree */
                talloc_free(tree_item);
-       }       
+       }
 
+       talloc_steal(mem_ctx, scripts);
        talloc_free(tmp_ctx);
-       return head;
+       return scripts;
 }
 
 static int child_setup(struct ctdb_context *ctdb)
@@ -446,7 +307,9 @@ static char *child_command_string(struct ctdb_context *ctdb,
                return talloc_asprintf(ctx, "%s%s/%s %s %s",
                                       str,
                                       ctdb->event_script_dir,
-                                      scriptname, call_names[call], options);
+                                      scriptname,
+                                      ctdb_eventscript_call_names[call],
+                                      options);
        }
 }
 
@@ -486,7 +349,7 @@ static int child_run_script(struct ctdb_context *ctdb,
                            bool from_user,
                            enum ctdb_eventscript_call call,
                            const char *options,
-                           struct ctdb_script *current)
+                           struct ctdb_script_wire *current)
 {
        char *cmdstr;
        int ret;
@@ -502,7 +365,7 @@ static int child_run_script(struct ctdb_context *ctdb,
        CTDB_NO_MEMORY(ctdb, cmdstr);
        child_state.script_running = cmdstr;
 
-       DEBUG(DEBUG_INFO,("Executing event script %s\n",cmdstr));
+       DEBUG(DEBUG_DEBUG,("Executing event script %s\n",cmdstr));
 
        if (current->status) {
                ret = current->status;
@@ -522,16 +385,9 @@ static int fork_child_for_script(struct ctdb_context *ctdb,
                                 struct ctdb_event_script_state *state)
 {
        int r;
-       void *mem_ctx = state;
-       void (*logfn)(const char *, uint16_t, void *) = NULL;
-
-       if (!state->from_user && state->call == CTDB_EVENT_MONITOR) {
-               ctdb_control_event_script_start(ctdb, state->current->name);
-               /* We need the logging destroyed after scripts, since it
-                * refers to them. */
-               mem_ctx = state->current->output;
-               logfn = log_event_script_output;
-       }
+       struct ctdb_script_wire *current = get_current_script(state);
+
+       current->start = timeval_current();
 
        r = pipe(state->fd);
        if (r != 0) {
@@ -539,8 +395,8 @@ static int fork_child_for_script(struct ctdb_context *ctdb,
                return -errno;
        }
 
-       if (!ctdb_fork_with_logging(mem_ctx, ctdb, logfn,
-                                   state->current, &state->child)) {
+       if (!ctdb_fork_with_logging(state, ctdb, log_event_script_output,
+                                   state, &state->child)) {
                r = -errno;
                close(state->fd[0]);
                close(state->fd[1]);
@@ -554,7 +410,7 @@ static int fork_child_for_script(struct ctdb_context *ctdb,
                close(state->fd[0]);
                set_close_on_exec(state->fd[1]);
 
-               rt = child_run_script(ctdb, state->from_user, state->call, state->options, state->current);
+               rt = child_run_script(ctdb, state->from_user, state->call, state->options, current);
                /* We must be able to write PIPEBUF bytes at least; if this
                   somehow fails, the read above will be short. */
                write(state->fd[1], &rt, sizeof(rt));
@@ -573,41 +429,64 @@ static int fork_child_for_script(struct ctdb_context *ctdb,
        return 0;
 }
 
+/*
+ Summarize status of this run of scripts.
+ */
+static int script_status(struct ctdb_scripts_wire *scripts)
+{
+       unsigned int i;
+
+       for (i = 0; i < scripts->num_scripts; i++) {
+               switch (scripts->scripts[i].status) {
+               case -ENOENT:
+               case -ENOEXEC:
+                       /* Disabled or missing; that's OK. */
+                       break;
+               case 0:
+                       /* No problem. */
+                       break;
+               default:
+                       return scripts->scripts[i].status;
+               }
+       }
+
+       /* All OK! */
+       return 0;
+}
+
 /* called when child is finished */
 static void ctdb_event_script_handler(struct event_context *ev, struct fd_event *fde, 
                                      uint16_t flags, void *p)
 {
        struct ctdb_event_script_state *state = 
                talloc_get_type(p, struct ctdb_event_script_state);
+       struct ctdb_script_wire *current = get_current_script(state);
        struct ctdb_context *ctdb = state->ctdb;
-       int r;
+       int r, status;
 
-       r = read(state->fd[0], &state->cb_status, sizeof(state->cb_status));
+       r = read(state->fd[0], &current->status, sizeof(current->status));
        if (r < 0) {
-               state->cb_status = -errno;
-       } else if (r != sizeof(state->cb_status)) {
-               state->cb_status = -EIO;
+               current->status = -errno;
+       } else if (r != sizeof(current->status)) {
+               current->status = -EIO;
        }
 
-       if (!state->from_user && state->call == CTDB_EVENT_MONITOR) {
-               ctdb_control_event_script_stop(ctdb, state->cb_status);
-       }
-
-       /* don't stop just because it vanished or was disabled. */
-       if (state->cb_status == -ENOENT || state->cb_status == -ENOEXEC) {
-               state->cb_status = 0;
+       current->finished = timeval_current();
+       /* valgrind gets overloaded if we run next script as it's still doing
+        * post-execution analysis, so kill finished child here. */
+       if (ctdb->valgrinding) {
+               kill(state->child, SIGKILL);
        }
 
        state->child = 0;
 
+       status = script_status(state->scripts);
+
        /* Aborted or finished all scripts?  We're done. */
-       if (state->cb_status != 0 || state->current->next == NULL) {
+       if (status != 0 || state->current+1 == state->scripts->num_scripts) {
                DEBUG(DEBUG_INFO,(__location__ " Eventscript %s %s finished with state %d\n",
-                                 call_names[state->call], state->options, state->cb_status));
+                                 ctdb_eventscript_call_names[state->call], state->options, status));
 
-               if (!state->from_user && state->call == CTDB_EVENT_MONITOR) {
-                       ctdb_control_event_script_finished(ctdb);
-               }
                ctdb->event_script_timeouts = 0;
                talloc_free(state);
                return;
@@ -617,12 +496,10 @@ static void ctdb_event_script_handler(struct event_context *ev, struct fd_event
        talloc_free(fde);
 
        /* Next script! */
-       state->current = state->current->next;
-       state->cb_status = fork_child_for_script(ctdb, state);
-       if (state->cb_status != 0) {
-               if (!state->from_user && state->call == CTDB_EVENT_MONITOR) {
-                       ctdb_control_event_script_finished(ctdb);
-               }
+       state->current++;
+       current++;
+       current->status = fork_child_for_script(ctdb, state);
+       if (current->status != 0) {
                /* This calls the callback. */
                talloc_free(state);
        }
@@ -634,30 +511,32 @@ static void ctdb_event_script_timeout(struct event_context *ev, struct timed_eve
 {
        struct ctdb_event_script_state *state = talloc_get_type(p, struct ctdb_event_script_state);
        struct ctdb_context *ctdb = state->ctdb;
+       struct ctdb_script_wire *current = get_current_script(state);
 
-       DEBUG(DEBUG_ERR,("Event script timed out : %s %s count : %u  pid : %d\n",
-                        call_names[state->call], state->options, ctdb->event_script_timeouts, state->child));
+       DEBUG(DEBUG_ERR,("Event script timed out : %s %s %s count : %u  pid : %d\n",
+                        current->name, ctdb_eventscript_call_names[state->call], state->options, ctdb->event_script_timeouts, state->child));
 
-       state->cb_status = -ETIME;
+       /* ignore timeouts for these events */
+       switch (state->call) {
+       case CTDB_EVENT_START_RECOVERY:
+       case CTDB_EVENT_RECOVERED:
+       case CTDB_EVENT_TAKE_IP:
+       case CTDB_EVENT_RELEASE_IP:
+       case CTDB_EVENT_STOPPED:
+       case CTDB_EVENT_MONITOR:
+       case CTDB_EVENT_STATUS:
+               state->scripts->scripts[state->current].status = 0;
+               DEBUG(DEBUG_ERR,("Ignoring hung script for %s call %d\n", state->options, state->call));
+               break;
+        default:
+               state->scripts->scripts[state->current].status = -ETIME;
+       }
 
        if (kill(state->child, 0) != 0) {
                DEBUG(DEBUG_ERR,("Event script child process already dead, errno %s(%d)\n", strerror(errno), errno));
                state->child = 0;
        }
 
-       if (state->call == CTDB_EVENT_MONITOR || state->call == CTDB_EVENT_STATUS) {
-               struct ctdb_script *script;
-
-               if (ctdb->current_monitor != NULL) {
-                       script = ctdb->current_monitor->current;
-                       if (script != NULL) {
-                               script->status = state->cb_status;
-                       }
-
-                       ctdb_control_event_script_finished(ctdb);
-               }
-       }
-
        talloc_free(state);
 }
 
@@ -666,6 +545,8 @@ static void ctdb_event_script_timeout(struct event_context *ev, struct timed_eve
  */
 static int event_script_destructor(struct ctdb_event_script_state *state)
 {
+       int status;
+
        if (state->child) {
                DEBUG(DEBUG_ERR,(__location__ " Sending SIGTERM to child pid:%d\n", state->child));
 
@@ -679,10 +560,27 @@ static int event_script_destructor(struct ctdb_event_script_state *state)
                state->ctdb->current_monitor = NULL;
        }
 
+       /* Save our scripts as the last executed status, if we have them.
+        * See ctdb_event_script_callback_v where we abort monitor event. */
+       if (state->scripts) {
+               talloc_free(state->ctdb->last_status[state->call]);
+               state->ctdb->last_status[state->call] = state->scripts;
+               if (state->current < state->ctdb->last_status[state->call]->num_scripts) {
+                       state->ctdb->last_status[state->call]->num_scripts = state->current+1;
+               }
+       }
+
+       /* Use last status as result, or "OK" if none. */
+       if (state->ctdb->last_status[state->call]) {
+               status = script_status(state->ctdb->last_status[state->call]);
+       } else {
+               status = 0;
+       }
+
        /* This is allowed to free us; talloc will prevent double free anyway,
         * but beware if you call this outside the destructor! */
        if (state->callback) {
-               state->callback(state->ctdb, state->cb_status, state->private_data);
+               state->callback(state->ctdb, status, state->private_data);
        }
 
        return 0;
@@ -705,6 +603,7 @@ static bool check_options(enum ctdb_eventscript_call call, const char *options)
 {
        switch (call) {
        /* These all take no arguments. */
+       case CTDB_EVENT_INIT:
        case CTDB_EVENT_STARTUP:
        case CTDB_EVENT_START_RECOVERY:
        case CTDB_EVENT_RECOVERED:
@@ -719,6 +618,9 @@ static bool check_options(enum ctdb_eventscript_call call, const char *options)
        case CTDB_EVENT_RELEASE_IP:
                return count_words(options) == 3;
 
+       case CTDB_EVENT_UPDATE_IP: /* old interface, new interface, IP address, netmask bits. */
+               return count_words(options) == 4;
+
        default:
                DEBUG(DEBUG_ERR,(__location__ "Unknown ctdb_eventscript_call %u\n", call));
                return false;
@@ -737,7 +639,6 @@ static int ctdb_event_script_callback_v(struct ctdb_context *ctdb,
                                        const char *fmt, va_list ap)
 {
        struct ctdb_event_script_state *state;
-       int ret;
 
        state = talloc(ctdb->event_script_ctx, struct ctdb_event_script_state);
        CTDB_NO_MEMORY(ctdb, state);
@@ -757,7 +658,7 @@ static int ctdb_event_script_callback_v(struct ctdb_context *ctdb,
        }
        if (!check_options(state->call, state->options)) {
                DEBUG(DEBUG_ERR, ("Bad eventscript options '%s' for %s\n",
-                                 call_names[state->call], state->options));
+                                 ctdb_eventscript_call_names[state->call], state->options));
                talloc_free(state);
                return -1;
        }
@@ -766,52 +667,70 @@ static int ctdb_event_script_callback_v(struct ctdb_context *ctdb,
                /* we guarantee that only some specifically allowed event scripts are run
                   while in recovery */
                const enum ctdb_eventscript_call allowed_calls[] = {
-                       CTDB_EVENT_START_RECOVERY, CTDB_EVENT_SHUTDOWN, CTDB_EVENT_RELEASE_IP, CTDB_EVENT_STOPPED };
+                       CTDB_EVENT_INIT,
+                       CTDB_EVENT_START_RECOVERY,
+                       CTDB_EVENT_SHUTDOWN,
+                       CTDB_EVENT_RELEASE_IP,
+                       CTDB_EVENT_STOPPED
+               };
                int i;
                for (i=0;i<ARRAY_SIZE(allowed_calls);i++) {
                        if (call == allowed_calls[i]) break;
                }
                if (i == ARRAY_SIZE(allowed_calls)) {
                        DEBUG(DEBUG_ERR,("Refusing to run event scripts call '%s' while in recovery\n",
-                                call_names[call]));
+                                ctdb_eventscript_call_names[call]));
                        talloc_free(state);
                        return -1;
                }
        }
 
        /* Kill off any running monitor events to run this event. */
-       talloc_free(ctdb->current_monitor);
-       ctdb->current_monitor = NULL;
-
-       if (!from_user && (call == CTDB_EVENT_MONITOR || call == CTDB_EVENT_STATUS)) {
-               ctdb->current_monitor = state;
+       if (ctdb->current_monitor) {
+               /* Discard script status so we don't save to last_status */
+               talloc_free(ctdb->current_monitor->scripts);
+               ctdb->current_monitor->scripts = NULL;
+               talloc_free(ctdb->current_monitor);
+               ctdb->current_monitor = NULL;
        }
 
        DEBUG(DEBUG_INFO,(__location__ " Starting eventscript %s %s\n",
-                         call_names[state->call], state->options));
+                         ctdb_eventscript_call_names[state->call],
+                         state->options));
 
-       state->current = state->scripts = ctdb_get_script_list(ctdb, state);
+       /* This is not a child of state, since we save it in destructor. */
+       state->scripts = ctdb_get_script_list(ctdb, ctdb);
        if (state->scripts == NULL) {
                talloc_free(state);
                return -1;
        }
+       state->current = 0;
 
-       if (!state->from_user && state->call == CTDB_EVENT_MONITOR) {
-               ctdb_control_event_script_init(ctdb);
+       if (!from_user && (call == CTDB_EVENT_MONITOR || call == CTDB_EVENT_STATUS)) {
+               ctdb->current_monitor = state;
        }
 
-       ret = fork_child_for_script(ctdb, state);
-       if (ret != 0) {
+       talloc_set_destructor(state, event_script_destructor);
+
+       /* Nothing to do? */
+       if (state->scripts->num_scripts == 0) {
                talloc_free(state);
-               return -1;
+               return 0;
        }
 
-       talloc_set_destructor(state, event_script_destructor);
+       state->scripts->scripts[0].status = fork_child_for_script(ctdb, state);
+       if (state->scripts->scripts[0].status != 0) {
+               /* Callback is called from destructor, with fail result. */
+               talloc_free(state);
+               return 0;
+       }
+
        if (!timeval_is_zero(&state->timeout)) {
                event_add_timed(ctdb->ev, state, timeval_current_ofs(state->timeout.tv_sec, state->timeout.tv_usec), ctdb_event_script_timeout, state);
        } else {
                DEBUG(DEBUG_ERR, (__location__ " eventscript %s %s called with no timeout\n",
-                                 call_names[state->call], state->options));
+                                 ctdb_eventscript_call_names[state->call],
+                                 state->options));
        }
 
        return 0;
@@ -883,7 +802,7 @@ int ctdb_event_script_args(struct ctdb_context *ctdb, enum ctdb_eventscript_call
        if (status.status == -ETIME) {
                DEBUG(DEBUG_ERR, (__location__ " eventscript for '%s' timedout."
                                  " Immediately banning ourself for %d seconds\n",
-                                 call_names[call],
+                                 ctdb_eventscript_call_names[call],
                                  ctdb->tunable.recovery_ban_period));
                ctdb_ban_self(ctdb);
        }
@@ -932,9 +851,9 @@ static const char *get_call(const char *p, enum ctdb_eventscript_call *call)
        p += strspn(p, " \t");
 
        /* See if we match any. */
-       for (*call = 0; *call < ARRAY_SIZE(call_names); (*call)++) {
-               len = strlen(call_names[*call]);
-               if (strncmp(p, call_names[*call], len) == 0) {
+       for (*call = 0; *call < CTDB_EVENT_MAX; (*call)++) {
+               len = strlen(ctdb_eventscript_call_names[*call]);
+               if (strncmp(p, ctdb_eventscript_call_names[*call], len) == 0) {
                        /* If end of string or whitespace, we're done. */
                        if (strcspn(p + len, " \t") == 0) {
                                return p + len;