+ for (;;) {
+ fd_set rfds;
+ int low_disk_space;
+ struct timeval *tvp;
+
+ if (remove_pid)
+ tvp = NULL; /* sleep until rm hook/process dies */
+ else { /* sleep one minute */
+ tv.tv_sec = 60;
+ tv.tv_usec = 0;
+ tvp = &tv;
+ }
+ FD_ZERO(&rfds);
+ FD_SET(signal_pipe, &rfds);
+ DSS_DEBUG_LOG("tvp: %p, tv_sec : %lu\n", tvp, (long unsigned) tv.tv_sec);
+ ret = dss_select(signal_pipe + 1, &rfds, NULL, tvp);
+ if (ret < 0)
+ goto out;
+ if (FD_ISSET(signal_pipe, &rfds)) {
+ ret = handle_signal();
+ if (ret < 0)
+ goto out;
+ }
+ if (remove_pid)
+ continue;
+ if (snapshot_removal_status == HS_PRE_SUCCESS) {
+ ret = exec_rm();
+ if (ret < 0)
+ goto out;
+ continue;
+ }
+ if (snapshot_removal_status == HS_SUCCESS) {
+ ret = post_remove_hook();
+ if (ret < 0)
+ goto out;
+ continue;
+ }
+ ret = disk_space_low();
+ if (ret < 0)
+ goto out;
+ low_disk_space = ret;
+ ret = try_to_free_disk_space(low_disk_space);
+ if (ret < 0)
+ goto out;
+ if (snapshot_removal_status != HS_READY) {
+ stop_create_process();
+ continue;
+ }
+ restart_create_process();
+ switch (snapshot_creation_status) {
+ case HS_READY:
+ if (!next_snapshot_is_due())
+ continue;
+ ret = pre_create_hook();
+ if (ret < 0)
+ goto out;
+ continue;
+ case HS_PRE_RUNNING:
+ case HS_RUNNING:
+ case HS_POST_RUNNING:
+ continue;
+ case HS_PRE_SUCCESS:
+ free_rsync_argv(rsync_argv);
+ create_rsync_argv(&rsync_argv, ¤t_snapshot_creation_time);
+ /* fall through */
+ case HS_NEEDS_RESTART:
+ ret = create_snapshot(rsync_argv);
+ if (ret < 0)
+ goto out;
+ continue;
+ case HS_SUCCESS:
+ ret = post_create_hook();
+ if (ret < 0)
+ goto out;
+ continue;
+ }
+ }
+out:
+ return ret;
+}
+
+static void exit_hook(int exit_code)
+{
+ int fds[3] = {0, 0, 0};
+ char *argv[] = {conf.exit_hook_arg, dss_strerror(-exit_code), NULL};
+ pid_t pid;
+
+ if (!conf.exit_hook_given)
+ return;
+ DSS_NOTICE_LOG("executing %s %s\n", argv[0], argv[1]);
+ dss_exec(&pid, conf.exit_hook_arg, argv, fds);
+}
+
+static int com_run(void)
+{
+ int ret;
+
+ if (conf.dry_run_given) {
+ DSS_ERROR_LOG("dry_run not supported by this command\n");
+ return -E_SYNTAX;
+ }
+ ret = install_sighandler(SIGHUP);
+ if (ret < 0)
+ return ret;
+ ret = select_loop();
+ if (ret >= 0) /* impossible */
+ ret = -E_BUG;
+ exit_hook(ret);
+ return ret;
+}
+
+static int com_prune(void)
+{
+ int ret;
+ struct snapshot_list sl;
+ struct snapshot *victim;
+ struct disk_space ds;
+ const char *why;
+
+ ret = get_disk_space(".", &ds);
+ if (ret < 0)
+ return ret;
+ log_disk_space(&ds);
+ dss_get_snapshot_list(&sl);
+ why = "outdated";
+ victim = find_outdated_snapshot(&sl);
+ if (victim)
+ goto rm;
+ why = "redundant";
+ victim = find_redundant_snapshot(&sl);
+ if (victim)
+ goto rm;
+ ret = 0;
+ goto out;
+rm:
+ if (conf.dry_run_given) {
+ dss_msg("%s snapshot %s (interval = %i)\n",
+ why, victim->name, victim->interval);
+ ret = 0;