+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 (snapshot_is_being_created(s))
+ return 0;
+ return pre_remove_hook(s, "oldest");
+}
+
+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;
+ struct timeval now;
+
+ gettimeofday(&now, NULL);
+ if (tv_diff(&next_removal_check, &now, NULL) > 0)
+ return 0;
+ 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 post_create_hook(void)
+{
+ int ret, fds[3] = {0, 0, 0};
+ char *cmd;
+
+ if (!conf.post_create_hook_given) {
+ snapshot_creation_status = HS_READY;
+ 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 = HS_POST_RUNNING;
+ return ret;
+}
+
+static int post_remove_hook(void)
+{
+ int ret, fds[3] = {0, 0, 0};
+ char *cmd;
+ struct snapshot *s = snapshot_currently_being_removed;
+
+ assert(s);
+
+ if (!conf.post_remove_hook_given) {
+ snapshot_removal_status = HS_READY;
+ return 0;
+ }
+ cmd = make_message("%s %s/%s", conf.post_remove_hook_arg,
+ conf.dest_dir_arg, s->name);
+ DSS_NOTICE_LOG("executing %s\n", cmd);
+ ret = dss_exec_cmdline_pid(&remove_pid, cmd, fds);
+ free(cmd);
+ if (ret < 0)
+ return ret;
+ snapshot_removal_status = HS_POST_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 void handle_pre_remove_exit(int status)
+{
+ if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
+ snapshot_removal_status = HS_READY;
+ gettimeofday(&next_removal_check, NULL);
+ next_removal_check.tv_sec += 60;
+ return;
+ }
+ snapshot_removal_status = HS_PRE_SUCCESS;
+}
+
+static int handle_rm_exit(int status)
+{
+ if (!WIFEXITED(status)) {
+ snapshot_removal_status = HS_READY;
+ return -E_INVOLUNTARY_EXIT;
+ }
+ if (WEXITSTATUS(status)) {
+ snapshot_removal_status = HS_READY;
+ return -E_BAD_EXIT_CODE;
+ }
+ snapshot_removal_status = HS_SUCCESS;
+ return 1;
+}
+
+static void handle_post_remove_exit(void)
+{
+ snapshot_removal_status = HS_READY;
+}
+
+static int handle_remove_exit(int status)
+{
+ int ret;
+ struct snapshot *s = snapshot_currently_being_removed;
+
+ assert(s);
+ switch (snapshot_removal_status) {
+ case HS_PRE_RUNNING:
+ handle_pre_remove_exit(status);
+ ret = 1;
+ break;
+ case HS_RUNNING:
+ ret = handle_rm_exit(status);
+ break;
+ case HS_POST_RUNNING:
+ handle_post_remove_exit();
+ ret = 1;
+ break;
+ default:
+ ret = -E_BUG;
+ }
+ if (snapshot_removal_status == HS_READY) {
+ free(s->name);
+ free(s);
+ snapshot_currently_being_removed = NULL;
+ }
+ remove_pid = 0;
+ return ret;
+}
+
+static int wait_for_remove_process(void)
+{
+ int status, ret;
+
+ assert(remove_pid);
+ assert(
+ snapshot_removal_status == HS_PRE_RUNNING ||
+ snapshot_removal_status == HS_RUNNING ||
+ snapshot_removal_status == HS_POST_RUNNING
+ );
+ ret = wait_for_process(remove_pid, &status);
+ if (ret < 0)
+ return ret;
+ return handle_remove_exit(status);
+}
+
+static int handle_rsync_exit(int status)