+int handle_pre_create_hook_exit(int status)
+{
+ int es, ret;
+
+ if (!WIFEXITED(status)) {
+ snapshot_creation_status = SCS_READY;
+ compute_next_snapshot_time();
+ ret = -E_INVOLUNTARY_EXIT;
+ goto out;
+ }
+ es = WEXITSTATUS(status);
+ if (es) {
+ snapshot_creation_status = SCS_READY;
+ compute_next_snapshot_time();
+ ret = -E_BAD_EXIT_CODE;
+ goto out;
+ }
+ snapshot_creation_status = SCS_PRE_HOOK_SUCCESS;
+ ret = 1;
+out:
+ pre_create_hook_pid = 0;
+ return ret;
+}
+
+int handle_sigchld()
+{
+ pid_t pid;
+ int status, ret = reap_child(&pid, &status);
+
+ if (ret <= 0)
+ return ret;
+ if (pid == rsync_pid)
+ return handle_rsync_exit(status);
+ if (pid == rm_pid)
+ return handle_rm_exit(status);
+ if (pid == pre_create_hook_pid)
+ return handle_pre_create_hook_exit(status);
+ if (pid == post_create_hook_pid) {
+ snapshot_creation_status = SCS_READY;
+ compute_next_snapshot_time();
+ return 1;
+ }
+ DSS_EMERG_LOG("BUG: unknown process %d died\n", (int)pid);
+ exit(EXIT_FAILURE);
+}
+
+void handle_signal(void)
+{
+ int sig, ret = next_signal();
+
+ if (ret <= 0)
+ goto out;
+ sig = ret;
+ switch (sig) {
+ case SIGINT:
+ case SIGTERM:
+ restart_rsync_process();
+ kill_process(rsync_pid);
+ kill_process(rm_pid);
+ exit(EXIT_FAILURE);
+ case SIGHUP:
+ handle_sighup();
+ ret = 1;
+ break;
+ case SIGCHLD:
+ ret = handle_sigchld();
+ break;
+ }
+out:
+ if (ret < 0)
+ DSS_ERROR_LOG("%s\n", dss_strerror(-ret));
+}
+
+int remove_oldest_snapshot(struct snapshot_list *sl)
+{
+ struct snapshot *s = oldest_snapshot(sl);
+
+ if (!s) /* no snapshot found */
+ return 0;
+ DSS_INFO_LOG("oldest snapshot: %s\n", s->name);
+ if (s->creation_time == current_snapshot_creation_time)
+ return 0; /* do not remove the snapshot currently being created */
+ return remove_snapshot(s);
+}
+
+/* TODO: Also consider number of inodes. */
+int disk_space_low(void)
+{
+ struct disk_space ds;
+ int ret = get_disk_space(".", &ds);
+
+ if (ret < 0)
+ return ret;
+ if (conf.min_free_mb_arg)
+ if (ds.free_mb < conf.min_free_mb_arg)
+ return 1;
+ if (conf.min_free_percent_arg)
+ if (ds.percent_free < conf.min_free_percent_arg)
+ return 1;
+ return 0;
+}
+
+int try_to_free_disk_space(int low_disk_space)
+{
+ int ret;
+ struct snapshot_list sl;
+
+ 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= -ENOSPC;
+out:
+ free_snapshot_list(&sl);
+ return ret;
+}
+
+int select_loop(void)
+{
+ int ret;
+ struct timeval tv = {.tv_sec = 0, .tv_usec = 0};
+
+ for (;;) {
+ fd_set rfds;
+ int low_disk_space;
+ char **rsync_argv;
+ struct timeval now, *tvp = &tv;
+
+ if (rsync_pid)
+ tv.tv_sec = 60; /* check every 60 seconds for free disk space */
+ else if (rm_pid)
+ tvp = NULL; /* sleep until rm process dies */
+ 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)
+ return ret;
+ if (FD_ISSET(signal_pipe, &rfds))
+ handle_signal();
+ if (rm_pid)
+ continue;
+ ret = disk_space_low();
+ if (ret < 0)
+ break;
+ low_disk_space = ret;
+ if (low_disk_space)
+ stop_rsync_process();
+ ret = try_to_free_disk_space(low_disk_space);
+ if (ret < 0)
+ break;
+ if (rm_pid)
+ continue;
+ restart_rsync_process();
+ gettimeofday(&now, NULL);
+ if (tv_diff(&next_snapshot_time, &now, &tv) > 0)
+ continue;
+ switch (snapshot_creation_status) {
+ case SCS_READY:
+ ret = pre_create_hook();
+ if (ret < 0)
+ goto out;
+ continue;
+ case SCS_PRE_HOOK_RUNNING:
+ continue;
+ case SCS_PRE_HOOK_SUCCESS:
+ create_rsync_argv(&rsync_argv, ¤t_snapshot_creation_time);
+ ret = create_snapshot(rsync_argv);
+ free_rsync_argv(rsync_argv);
+ if (ret < 0)
+ goto out;
+ continue;
+ case SCS_RSYNC_RUNNING:
+ continue;
+ case SCS_RSYNC_SUCCESS:
+ ret = post_create_hook();
+ if (ret < 0)
+ goto out;
+ continue;
+ case SCS_POST_HOOK_RUNNING:
+ continue;
+ }
+ }
+out:
+ return ret;
+}
+
+int com_run(void)