when tdb_chainunlock() fails, print the tdb error that occured
[metze/ctdb/wip.git] / server / eventscript.c
index f43877478d8007dfc8b2811438b7c316e164440d..8011269e26f625595841a5eae5302fa91e70bf57 100644 (file)
 #include "lib/events/events.h"
 #include "../common/rb_tree.h"
 
-static struct {
-       struct timeval start;
-       const char *script_running;
-} child_state;
-
 static void ctdb_event_script_timeout(struct event_context *ev, struct timed_event *te, struct timeval t, void *p);
 
 /*
-  ctdbd sends us a SIGTERM when we should time out the current script
+  ctdbd sends us a SIGTERM when we should die.
  */
 static void sigterm(int sig)
 {
-       char tbuf[100], buf[200];
-       time_t t;
-
-       DEBUG(DEBUG_ERR,("Timed out running script '%s' after %.1f seconds pid :%d\n", 
-                child_state.script_running, timeval_elapsed(&child_state.start), getpid()));
-
-       t = time(NULL);
-
-       strftime(tbuf, sizeof(tbuf)-1, "%Y%m%d%H%M%S",  localtime(&t));
-       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));
-
        /* all the child processes will be running in the same process group */
        kill(-getpgrp(), SIGKILL);
        _exit(1);
@@ -87,9 +67,16 @@ static void log_event_script_output(const char *str, uint16_t len, 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_script_wire *current;
        unsigned int slen, min;
 
+       /* We may have been aborted to run something else.  Discard */
+       if (state->scripts == NULL) {
+               return;
+       }
+
+       current = get_current_script(state);
+
        /* Append, but don't overfill buffer.  It starts zero-filled. */
        slen = strlen(current->output);
        min = MIN(len, sizeof(current->output) - slen - 1);
@@ -138,7 +125,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;
@@ -348,7 +335,6 @@ static int child_run_script(struct ctdb_context *ctdb,
        int ret;
        TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
 
-       child_state.start = timeval_current();
        ret = child_setup(ctdb);
        if (ret != 0)
                goto out;
@@ -356,9 +342,8 @@ static int child_run_script(struct ctdb_context *ctdb,
        cmdstr = child_command_string(ctdb, tmp_ctx, from_user,
                                      current->name, call, options);
        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;
@@ -498,6 +483,46 @@ static void ctdb_event_script_handler(struct event_context *ev, struct fd_event
        }
 }
 
+static void debug_timeout(struct ctdb_event_script_state *state)
+{
+       struct ctdb_script_wire *current = get_current_script(state);
+       char *cmd;
+       pid_t pid;
+       time_t t;
+       char tbuf[100], buf[200];
+
+       cmd = child_command_string(state->ctdb, state,
+                                  state->from_user, current->name,
+                                  state->call, state->options);
+       CTDB_NO_MEMORY_VOID(state->ctdb, cmd);
+
+       DEBUG(DEBUG_ERR,("Timed out running script '%s' after %.1f seconds pid :%d\n",
+                        cmd, timeval_elapsed(&current->start), state->child));
+       talloc_free(cmd);
+
+       t = time(NULL);
+       strftime(tbuf, sizeof(tbuf)-1, "%Y%m%d%H%M%S",  localtime(&t));
+       sprintf(buf, "{ pstree -p; cat /proc/locks; ls -li /var/ctdb/ /var/ctdb/persistent; }"
+                       " >/tmp/ctdb.event.%s.%d", tbuf, getpid());
+
+       pid = fork();
+       if (pid == 0) {
+               ctdb_reduce_priority(state->ctdb);
+               system(buf);
+               /* Now we can kill the child */
+               kill(state->child, SIGTERM);
+               exit(0);
+       }
+       if (pid == -1) {
+               DEBUG(DEBUG_ERR,("Fork for debug script failed : %s\n",
+                                strerror(errno)));
+       } else {
+               DEBUG(DEBUG_ERR,("Logged timedout eventscript : %s\n", buf));
+               /* Don't kill child until timeout done. */
+               state->child = 0;
+       }
+}
+
 /* called when child times out */
 static void ctdb_event_script_timeout(struct event_context *ev, struct timed_event *te, 
                                      struct timeval t, void *p)
@@ -509,11 +534,21 @@ static void ctdb_event_script_timeout(struct event_context *ev, struct timed_eve
        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->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;
+       /* 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;
+               debug_timeout(state);
        }
 
        talloc_free(state);
@@ -582,6 +617,8 @@ 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_SETUP:
        case CTDB_EVENT_STARTUP:
        case CTDB_EVENT_START_RECOVERY:
        case CTDB_EVENT_RECOVERED:
@@ -596,6 +633,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;
@@ -642,7 +682,13 @@ 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_SETUP,
+                       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;
@@ -664,10 +710,6 @@ static int ctdb_event_script_callback_v(struct ctdb_context *ctdb,
                ctdb->current_monitor = NULL;
        }
 
-       if (!from_user && (call == CTDB_EVENT_MONITOR || call == CTDB_EVENT_STATUS)) {
-               ctdb->current_monitor = state;
-       }
-
        DEBUG(DEBUG_INFO,(__location__ " Starting eventscript %s %s\n",
                          ctdb_eventscript_call_names[state->call],
                          state->options));
@@ -679,6 +721,12 @@ static int ctdb_event_script_callback_v(struct ctdb_context *ctdb,
                return -1;
        }
        state->current = 0;
+       state->child = 0;
+
+       if (!from_user && (call == CTDB_EVENT_MONITOR || call == CTDB_EVENT_STATUS)) {
+               ctdb->current_monitor = state;
+       }
+
        talloc_set_destructor(state, event_script_destructor);
 
        /* Nothing to do? */
@@ -687,12 +735,12 @@ static int ctdb_event_script_callback_v(struct ctdb_context *ctdb,
                return 0;
        }
 
-       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;
-       }
+       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);