]> git.tuebingen.mpg.de Git - dss.git/blobdiff - dss.c
Unify sending of signals.
[dss.git] / dss.c
diff --git a/dss.c b/dss.c
index 68efc2b07cc457c4944013b6e3147fdc3f2adce1..9f601ceb8365552719b001caad98fdd3efef1b30 100644 (file)
--- a/dss.c
+++ b/dss.c
@@ -250,8 +250,7 @@ static int pre_remove_hook(struct snapshot *s, const char *why)
        cmd = make_message("%s %s/%s", conf.pre_remove_hook_arg,
                conf.dest_dir_arg, s->name);
        DSS_DEBUG_LOG("executing %s\n", cmd);
-       ret = dss_exec_cmdline_pid(&remove_pid,
-               conf.pre_remove_hook_arg, fds);
+       ret = dss_exec_cmdline_pid(&remove_pid, cmd, fds);
        free(cmd);
        if (ret < 0)
                return ret;
@@ -542,27 +541,48 @@ static int post_remove_hook(void)
        return ret;
 }
 
-static void kill_process(pid_t pid)
+static void dss_kill(pid_t pid, int sig, const char *msg)
 {
-       if (!pid)
+       const char *signame, *process_name;
+
+       if (pid == 0)
+               return;
+       switch (sig) {
+       case SIGTERM: signame = "TERM"; break;
+       case SIGSTOP: signame = "STOP"; break;
+       case SIGCONT: signame = "CONT"; break;
+       default: signame = "????";
+       }
+
+       if (pid == create_pid)
+               process_name = "create";
+       else if (pid == remove_pid)
+               process_name = "remove";
+       else process_name = "??????";
+
+       if (msg)
+               DSS_INFO_LOG("%s\n", msg);
+       DSS_DEBUG_LOG("sending signal %d (%s) to pid %d (%s process)\n",
+               sig, signame, (int)pid, process_name);
+       if (kill(pid, sig) >= 0)
                return;
-       DSS_WARNING_LOG("sending SIGTERM to pid %d\n", (int)pid);
-       kill(pid, SIGTERM);
+       DSS_INFO_LOG("failed to send signal %d (%s) to pid %d (%s process)\n",
+               sig, signame, (int)pid, process_name);
 }
 
 static void stop_create_process(void)
 {
-       if (!create_pid || create_process_stopped)
+       if (create_process_stopped)
                return;
-       kill(SIGSTOP, create_pid);
+       dss_kill(create_pid, SIGSTOP, "suspending create process");
        create_process_stopped = 1;
 }
 
 static void restart_create_process(void)
 {
-       if (!create_pid || !create_process_stopped)
+       if (!create_process_stopped)
                return;
-       kill (SIGCONT, create_pid);
+       dss_kill(create_pid, SIGCONT, "resuming create process");
        create_process_stopped = 0;
 }
 
@@ -607,8 +627,7 @@ static int wait_for_process(pid_t pid, int *status)
                        }
                }
                /* SIGINT or SIGTERM */
-               DSS_WARNING_LOG("sending SIGTERM to pid %d\n", (int)pid);
-               kill(pid, SIGTERM);
+               dss_kill(pid, SIGTERM, "killing child process");
        }
        if (ret < 0)
                DSS_ERROR_LOG("failed to wait for process %d\n", (int)pid);
@@ -778,18 +797,22 @@ static int handle_sigchld(void)
        if (pid == create_pid) {
                switch (snapshot_creation_status) {
                case HS_PRE_RUNNING:
-                       return handle_pre_create_hook_exit(status);
+                       ret = handle_pre_create_hook_exit(status);
+                       break;
                case HS_RUNNING:
-                       return handle_rsync_exit(status);
+                       ret = handle_rsync_exit(status);
+                       break;
                case HS_POST_RUNNING:
                        snapshot_creation_status = HS_READY;
-                       return 1;
+                       ret = 1;
+                       break;
                default:
                        DSS_EMERG_LOG("BUG: create can't die in status %d\n",
                                snapshot_creation_status);
                        return -E_BUG;
                }
                create_pid = 0;
+               return ret;
        }
        if (pid == remove_pid) {
                ret = handle_remove_exit(status);
@@ -933,8 +956,8 @@ static int handle_signal(void)
        case SIGINT:
        case SIGTERM:
                restart_create_process();
-               kill_process(create_pid);
-               kill_process(remove_pid);
+               dss_kill(create_pid, SIGTERM, NULL);
+               dss_kill(remove_pid, SIGTERM, NULL);
                ret = -E_SIGNAL;
                break;
        case SIGHUP:
@@ -1074,7 +1097,6 @@ static int select_loop(void)
                }
                FD_ZERO(&rfds);
                FD_SET(signal_pipe, &rfds);
-               DSS_DEBUG_LOG("tvp: %p, tv_sec : %lu\n", tvp, (long unsigned) tv.tv_sec);
                ret = dss_select(signal_pipe + 1, &rfds, NULL, tvp);
                if (ret < 0)
                        goto out;
@@ -1122,7 +1144,10 @@ static int select_loop(void)
                                free_rsync_argv(rsync_argv);
                                create_rsync_argv(&rsync_argv, &current_snapshot_creation_time);
                        }
-                       /* fall through */
+                       ret = create_snapshot(rsync_argv);
+                       if (ret < 0)
+                               goto out;
+                       continue;
                case HS_NEEDS_RESTART:
                        if (!next_snapshot_is_due())
                                continue;