Fixes for various issues found by Coverity
[nivanova/samba-autobuild/.git] / ctdb / server / eventscript.c
index 752543fbe5e646bd7d611a4a51f239943523a3a0..10d426fba7f912264d1bd9ec645230a6f188a255 100644 (file)
@@ -34,8 +34,15 @@ static void ctdb_event_script_timeout(struct event_context *ev, struct timed_eve
  */
 static void sigterm(int sig)
 {
+       pid_t pid;
+
        /* all the child processes will be running in the same process group */
-       kill(-getpgrp(), SIGKILL);
+       pid = getpgrp();
+       if (pid == -1) {
+               kill(-getpid(), SIGKILL);
+       } else {
+               kill(-pid, SIGKILL);
+       }
        _exit(1);
 }
 
@@ -211,6 +218,7 @@ static struct ctdb_scripts_wire *ctdb_get_script_list(struct ctdb_context *ctdb,
                tree_item = talloc(tree, struct ctdb_script_tree_item);
                if (tree_item == NULL) {
                        DEBUG(DEBUG_ERR, (__location__ " Failed to allocate new tree item\n"));
+                       closedir(dir);
                        talloc_free(tmp_ctx);
                        return NULL;
                }
@@ -223,6 +231,7 @@ static struct ctdb_scripts_wire *ctdb_get_script_list(struct ctdb_context *ctdb,
                tree_item->name = talloc_strdup(tree_item, de->d_name);
                if (tree_item->name == NULL) {
                        DEBUG(DEBUG_ERR,(__location__ " Failed to allocate script name.\n"));
+                       closedir(dir);
                        talloc_free(tmp_ctx);
                        return NULL;
                }
@@ -400,6 +409,7 @@ static int fork_child_for_script(struct ctdb_context *ctdb,
                                              ctdb_eventscript_call_names[state->call]);
                close(state->fd[0]);
                set_close_on_exec(state->fd[1]);
+               ctdb_set_process_name("ctdb_eventscript");
 
                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
@@ -533,6 +543,7 @@ static void ctdb_run_debug_hung_script(struct ctdb_context *ctdb, struct ctdb_ev
        if (pid == 0) {
                char *buf;
 
+               ctdb_set_process_name("ctdb_debug_hung_script");
                if (getenv("CTDB_DEBUG_HUNG_SCRIPT") != NULL) {
                        debug_hung_script = getenv("CTDB_DEBUG_HUNG_SCRIPT");
                }
@@ -568,7 +579,6 @@ static void ctdb_event_script_timeout(struct event_context *ev, struct timed_eve
        case CTDB_EVENT_RECOVERED:
        case CTDB_EVENT_TAKE_IP:
        case CTDB_EVENT_RELEASE_IP:
-       case CTDB_EVENT_STOPPED:
        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));
@@ -666,7 +676,6 @@ static bool check_options(enum ctdb_eventscript_call call, const char *options)
        case CTDB_EVENT_STARTUP:
        case CTDB_EVENT_START_RECOVERY:
        case CTDB_EVENT_RECOVERED:
-       case CTDB_EVENT_STOPPED:
        case CTDB_EVENT_MONITOR:
        case CTDB_EVENT_STATUS:
        case CTDB_EVENT_SHUTDOWN:
@@ -717,7 +726,6 @@ static int ctdb_event_script_callback_v(struct ctdb_context *ctdb,
                        CTDB_EVENT_SHUTDOWN,
                        CTDB_EVENT_RELEASE_IP,
                        CTDB_EVENT_IPREALLOCATED,
-                       CTDB_EVENT_STOPPED
                };
                int i;
                for (i=0;i<ARRAY_SIZE(allowed_calls);i++) {
@@ -874,10 +882,10 @@ int ctdb_event_script_args(struct ctdb_context *ctdb, enum ctdb_eventscript_call
        va_start(ap, fmt);
        ret = ctdb_event_script_callback_v(ctdb, ctdb,
                        event_script_callback, &status, false, call, fmt, ap);
+       va_end(ap);
        if (ret != 0) {
                return ret;
        }
-       va_end(ap);
 
        status.status = -1;
        status.done = false;
@@ -889,7 +897,11 @@ int ctdb_event_script_args(struct ctdb_context *ctdb, enum ctdb_eventscript_call
                                  " Immediately banning ourself for %d seconds\n",
                                  ctdb_eventscript_call_names[call],
                                  ctdb->tunable.recovery_ban_period));
-               ctdb_ban_self(ctdb);
+
+               /* Don't ban self if CTDB is starting up or shutting down */
+               if (call != CTDB_EVENT_INIT && call != CTDB_EVENT_SHUTDOWN) {
+                       ctdb_ban_self(ctdb);
+               }
        }
 
        return status.status;