+ return 0;
+}
+
+static int remove_oldest_snapshot(struct snapshot_list *sl)
+{
+ struct snapshot *s = get_oldest_snapshot(sl);
+
+ if (!s) /* no snapshot found */
+ return 0;
+ DSS_INFO_LOG("oldest snapshot: %s\n", s->name);
+ if (s->creation_time == current_snapshot_creation_time)
+ return 0; /* do not remove the snapshot currently being created */
+ return remove_snapshot(s);
+}
+
+static int rename_incomplete_snapshot(int64_t start)
+{
+ char *old_name;
+ int ret;
+
+ free(path_to_last_complete_snapshot);
+ ret = complete_name(start, get_current_time(),
+ &path_to_last_complete_snapshot);
+ if (ret < 0)
+ return ret;
+ old_name = incomplete_name(start);
+ ret = dss_rename(old_name, path_to_last_complete_snapshot);
+ if (ret >= 0)
+ DSS_NOTICE_LOG("%s -> %s\n", old_name,
+ path_to_last_complete_snapshot);
+ free(old_name);
+ return ret;
+}
+
+static int try_to_free_disk_space(int low_disk_space)
+{
+ int ret;
+ struct snapshot_list sl;
+
+ if (!low_disk_space && conf.keep_redundant_given)
+ return 0;
+ dss_get_snapshot_list(&sl);
+ ret = remove_outdated_snapshot(&sl);
+ if (ret) /* error, or we are removing something */
+ goto out;
+ /* no outdated snapshot */
+ ret = remove_redundant_snapshot(&sl);
+ if (ret)
+ goto out;
+ ret = 0;
+ if (!low_disk_space)
+ goto out;
+ DSS_WARNING_LOG("disk space low and nothing obvious to remove\n");
+ ret = remove_oldest_snapshot(&sl);
+ if (ret)
+ goto out;
+ DSS_CRIT_LOG("uhuhu: not enough disk space for a single snapshot\n");
+ ret = -ERRNO_TO_DSS_ERROR(ENOSPC);
+out:
+ free_snapshot_list(&sl);
+ return ret;
+}
+
+static int pre_create_hook(void)
+{
+ int ret, fds[3] = {0, 0, 0};
+
+ if (!conf.pre_create_hook_given) {
+ snapshot_creation_status = SCS_PRE_HOOK_SUCCESS;
+ return 0;
+ }
+ DSS_NOTICE_LOG("executing %s\n", conf.pre_create_hook_arg);
+ ret = dss_exec_cmdline_pid(&pre_create_hook_pid,
+ conf.pre_create_hook_arg, fds);
+ if (ret < 0)
+ return ret;
+ 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", conf.post_create_hook_arg,
+ path_to_last_complete_snapshot);
+ DSS_NOTICE_LOG("executing %s\n", cmd);
+ ret = dss_exec_cmdline_pid(&post_create_hook_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_rsync_process(void)
+{
+ if (!rsync_pid || rsync_stopped)
+ return;
+ kill(SIGSTOP, rsync_pid);
+ rsync_stopped = 1;
+}
+
+static void restart_rsync_process(void)
+{
+ if (!rsync_pid || !rsync_stopped)
+ return;
+ kill (SIGCONT, rsync_pid);
+ rsync_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)rsync_pid);
+ ret = -E_INVOLUNTARY_EXIT;
+ snapshot_creation_status = SCS_READY;
+ compute_next_snapshot_time();
+ goto out;
+ }
+ es = WEXITSTATUS(status);
+ if (es != 0 && es != 23 && es != 24) {
+ DSS_ERROR_LOG("rsync process %d returned %d\n", (int)rsync_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:
+ rsync_pid = 0;
+ rsync_stopped = 0;
+ return ret;