+ snapshot_creation_status = SCS_PRE_HOOK_RUNNING;
+ return ret;
+}
+
+static int post_create_hook(void)
+{
+ int ret, fds[3] = {0, 0, 0};
+ char *cmd;
+
+ if (!conf.post_create_hook_given) {
+ snapshot_creation_status = SCS_READY;
+ compute_next_snapshot_time();
+ return 0;
+ }
+ cmd = make_message("%s %s/%s", conf.post_create_hook_arg,
+ conf.dest_dir_arg, path_to_last_complete_snapshot);
+ DSS_NOTICE_LOG("executing %s\n", cmd);
+ ret = dss_exec_cmdline_pid(&create_pid, cmd, fds);
+ free(cmd);
+ if (ret < 0)
+ return ret;
+ snapshot_creation_status = SCS_POST_HOOK_RUNNING;
+ return ret;
+}
+
+static void kill_process(pid_t pid)
+{
+ if (!pid)
+ return;
+ DSS_WARNING_LOG("sending SIGTERM to pid %d\n", (int)pid);
+ kill(pid, SIGTERM);
+}
+
+static void stop_create_process(void)
+{
+ if (!create_pid || create_process_stopped)
+ return;
+ kill(SIGSTOP, create_pid);
+ create_process_stopped = 1;
+}
+
+static void restart_create_process(void)
+{
+ if (!create_pid || !create_process_stopped)
+ return;
+ kill (SIGCONT, create_pid);
+ create_process_stopped = 0;
+}
+
+/**
+ * Print a log message about the exit status of a child.
+ */
+static void log_termination_msg(pid_t pid, int status)
+{
+ if (WIFEXITED(status))
+ DSS_INFO_LOG("child %i exited. Exit status: %i\n", (int)pid,
+ WEXITSTATUS(status));
+ else if (WIFSIGNALED(status))
+ DSS_NOTICE_LOG("child %i was killed by signal %i\n", (int)pid,
+ WTERMSIG(status));
+ else
+ DSS_WARNING_LOG("child %i terminated abormally\n", (int)pid);
+}
+
+static int wait_for_process(pid_t pid, int *status)
+{
+ int ret;
+
+ DSS_DEBUG_LOG("Waiting for process %d to terminate\n", (int)pid);
+ for (;;) {
+ fd_set rfds;
+
+ FD_ZERO(&rfds);
+ FD_SET(signal_pipe, &rfds);
+ ret = dss_select(signal_pipe + 1, &rfds, NULL, NULL);
+ if (ret < 0)
+ break;
+ ret = next_signal();
+ if (!ret)
+ continue;
+ if (ret == SIGCHLD) {
+ ret = waitpid(pid, status, 0);
+ if (ret >= 0)
+ break;
+ if (errno != EINTR) { /* error */
+ ret = -ERRNO_TO_DSS_ERROR(errno);
+ break;
+ }
+ }
+ /* SIGINT or SIGTERM */
+ DSS_WARNING_LOG("sending SIGTERM to pid %d\n", (int)pid);
+ kill(pid, SIGTERM);
+ }
+ if (ret < 0)
+ DSS_ERROR_LOG("failed to wait for process %d\n", (int)pid);
+ else
+ log_termination_msg(pid, *status);
+ return ret;
+}
+
+static int handle_rm_exit(int status)
+{
+ rm_pid = 0;
+ if (!WIFEXITED(status))
+ return -E_INVOLUNTARY_EXIT;
+ if (WEXITSTATUS(status))
+ return -E_BAD_EXIT_CODE;
+ return 1;
+}
+
+static int wait_for_rm_process(void)
+{
+ int status, ret = wait_for_process(rm_pid, &status);
+
+ if (ret < 0)
+ return ret;
+ return handle_rm_exit(status);
+}
+
+static int handle_rsync_exit(int status)
+{
+ int es, ret;
+
+ if (!WIFEXITED(status)) {
+ DSS_ERROR_LOG("rsync process %d died involuntary\n", (int)create_pid);
+ ret = -E_INVOLUNTARY_EXIT;
+ snapshot_creation_status = SCS_READY;
+ compute_next_snapshot_time();
+ goto out;
+ }
+ es = WEXITSTATUS(status);
+ if (es == 13) { /* Errors with program diagnostics */
+ DSS_WARNING_LOG("rsync process %d returned %d -- restarting\n",
+ (int)create_pid, es);
+ snapshot_creation_status = SCS_RSYNC_NEEDS_RESTART;
+ gettimeofday(&next_snapshot_time, NULL);
+ next_snapshot_time.tv_sec += 60;
+ ret = 1;
+ goto out;
+ }
+ if (es != 0 && es != 23 && es != 24) {
+ DSS_ERROR_LOG("rsync process %d returned %d\n", (int)create_pid, es);
+ ret = -E_BAD_EXIT_CODE;
+ snapshot_creation_status = SCS_READY;
+ compute_next_snapshot_time();
+ goto out;
+ }
+ ret = rename_incomplete_snapshot(current_snapshot_creation_time);
+ if (ret < 0)
+ goto out;
+ snapshot_creation_status = SCS_RSYNC_SUCCESS;
+out:
+ create_pid = 0;
+ create_process_stopped = 0;
+ return ret;
+}
+
+static int handle_pre_create_hook_exit(int status)
+{
+ int es, ret;
+
+ if (!WIFEXITED(status)) {
+ snapshot_creation_status = SCS_READY;
+ compute_next_snapshot_time();
+ ret = -E_INVOLUNTARY_EXIT;
+ goto out;