+ if (s->interval < conf.num_intervals_arg)
+ continue;
+ return s;
+ }
+ return NULL;
+}
+
+struct snapshot *find_oldest_removable_snapshot(struct snapshot_list *sl)
+{
+ int i;
+ struct snapshot *s;
+ FOR_EACH_SNAPSHOT(s, i, sl) {
+ if (snapshot_is_being_created(s))
+ continue;
+ if (is_reference_snapshot(s))
+ continue;
+ DSS_INFO_LOG("oldest removable snapshot: %s\n", s->name);
+ return s;
+ }
+ return NULL;
+}
+
+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(void)
+{
+ int ret;
+ struct snapshot_list sl;
+ struct snapshot *victim;
+ struct timeval now;
+ const char *why;
+ int low_disk_space;
+
+ ret = disk_space_low(NULL);
+ if (ret < 0)
+ return ret;
+ low_disk_space = ret;
+ gettimeofday(&now, NULL);
+ if (tv_diff(&next_removal_check, &now, NULL) > 0)
+ return 0;
+ if (!low_disk_space) {
+ if (conf.keep_redundant_given)
+ return 0;
+ if (snapshot_creation_status != HS_READY)
+ return 0;
+ if (next_snapshot_is_due())
+ return 0;
+ }
+ dss_get_snapshot_list(&sl);
+ ret = 0;
+ if (!low_disk_space && sl.num_snapshots <= 1)
+ goto out;
+ why = "outdated";
+ victim = find_outdated_snapshot(&sl);
+ if (victim)
+ goto remove;
+ why = "redundant";
+ victim = find_redundant_snapshot(&sl);
+ if (victim)
+ goto remove;
+ /* try harder only if disk space is low */
+ if (!low_disk_space)
+ goto out;
+ why = "orphaned";
+ victim = find_orphaned_snapshot(&sl);
+ if (victim)
+ goto remove;
+ DSS_WARNING_LOG("disk space low and nothing obvious to remove\n");
+ victim = find_oldest_removable_snapshot(&sl);
+ if (victim)
+ goto remove;
+ DSS_CRIT_LOG("uhuhu: disk space low and nothing to remove\n");
+ ret = -ERRNO_TO_DSS_ERROR(ENOSPC);
+ goto out;
+remove:
+ pre_remove_hook(victim, why);
+out:
+ free_snapshot_list(&sl);
+ return ret;
+}
+
+static void post_create_hook(void)
+{
+ char *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);
+ dss_exec_cmdline_pid(&create_pid, cmd);
+ free(cmd);
+ snapshot_creation_status = HS_POST_RUNNING;
+}
+
+static void post_remove_hook(void)
+{
+ char *cmd;
+ struct snapshot *s = snapshot_currently_being_removed;
+
+ assert(s);
+
+ cmd = make_message("%s %s/%s", conf.post_remove_hook_arg,
+ conf.dest_dir_arg, s->name);
+ DSS_NOTICE_LOG("executing %s\n", cmd);
+ dss_exec_cmdline_pid(&remove_pid, cmd);
+ free(cmd);
+ snapshot_removal_status = HS_POST_RUNNING;
+}
+
+static void dss_kill(pid_t pid, int sig, const char *msg)
+{
+ 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_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_process_stopped)
+ return;
+ dss_kill(create_pid, SIGSTOP, "suspending create process");
+ create_process_stopped = 1;
+}
+
+static void restart_create_process(void)
+{
+ if (!create_process_stopped)
+ return;
+ dss_kill(create_pid, SIGCONT, "resuming create process");
+ 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);