+ int ret;
+ struct snapshot_list sl;
+ struct snapshot *victim;
+ struct timeval now;
+ const char *why;
+ int low_disk_space;
+
+ ret = disk_space_low();
+ 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:
+ ret = pre_remove_hook(victim, why);
+out:
+ free_snapshot_list(&sl);
+ return ret;
+}
+
+static int post_create_hook(void)
+{
+ int ret, fds[3] = {0, 0, 0};
+ char *cmd;
+
+ 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);
+
+ 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 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)