+ DSS_INFO_LOG(("oldest removable snapshot: %s\n", s->name));
+ return s;
+ }
+ assert(ref);
+ DSS_WARNING_LOG(("removing reference snapshot %s\n", ref->name));
+ return ref;
+}
+
+static int rename_incomplete_snapshot(int64_t start)
+{
+ char *old_name;
+ int ret;
+ int64_t now;
+
+ /*
+ * We don't want the dss_rename() below to fail with EEXIST because the
+ * last complete snapshot was created (and completed) in the same
+ * second as this one.
+ */
+ while ((now = get_current_time()) == start)
+ sleep(1);
+ free(path_to_last_complete_snapshot);
+ ret = complete_name(start, now, &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;
+ }
+ /*
+ * Idle and --keep_redundant not given, or low disk space. Look at
+ * existing snapshots.
+ */
+ dss_get_snapshot_list(&sl);
+ ret = 0;
+ /*
+ * Don't remove anything if there is free space and we have fewer
+ * snapshots than configured, plus one. This way there is always one
+ * snapshot that can be recycled.
+ */
+ if (!low_disk_space && sl.num_snapshots <= 1 << conf.num_intervals_arg)
+ goto out;
+ why = "outdated";
+ victim = find_outdated_snapshot(&sl);
+ if (victim)
+ goto remove;
+ why = "redundant";
+ victim = find_redundant_snapshot(&sl);
+ if (victim)
+ goto remove;
+ why = "orphaned";
+ victim = find_orphaned_snapshot(&sl);
+ if (victim)
+ goto remove;
+ /* try harder only if disk space is low */
+ if (!low_disk_space)
+ goto out;
+ 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);