+static void restart_rsync_process(void)
+{
+ if (!rsync_pid || !rsync_stopped)
+ return;
+ kill (SIGCONT, rsync_pid);
+ rsync_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);
+ if (ret < 0)
+ break;
+ ret = next_signal();
+ if (!ret)
+ continue;
+ if (ret == SIGCHLD) {
+ ret = waitpid(pid, status, 0);
+ if (ret >= 0)
+ break;
+ if (errno != EINTR) { /* error */
+ ret = -ERRNO_TO_DSS_ERROR(errno);
+ break;
+ }
+ }
+ /* SIGINT or SIGTERM */
+ DSS_WARNING_LOG("sending SIGTERM to pid %d\n", (int)pid);
+ kill(pid, SIGTERM);
+ }
+ if (ret < 0)
+ DSS_ERROR_LOG("failed to wait for process %d\n", (int)pid);
+ else
+ log_termination_msg(pid, *status);
+ return ret;
+}
+
+static int handle_rm_exit(int status)
+{
+ rm_pid = 0;
+ if (!WIFEXITED(status))
+ return -E_INVOLUNTARY_EXIT;
+ if (WEXITSTATUS(status))
+ return -E_BAD_EXIT_CODE;
+ return 1;
+}
+
+static int wait_for_rm_process(void)
+{
+ int status, ret = wait_for_process(rm_pid, &status);
+
+ if (ret < 0)
+ return ret;
+ return handle_rm_exit(status);
+}
+
+static int handle_rsync_exit(int status)
+{
+ int es, ret;
+
+ if (!WIFEXITED(status)) {
+ DSS_ERROR_LOG("rsync process %d died involuntary\n", (int)rsync_pid);
+ ret = -E_INVOLUNTARY_EXIT;
+ snapshot_creation_status = SCS_READY;
+ compute_next_snapshot_time();
+ goto out;
+ }
+ es = WEXITSTATUS(status);
+ if (es != 0 && es != 23 && es != 24) {
+ DSS_ERROR_LOG("rsync process %d returned %d\n", (int)rsync_pid, es);
+ ret = -E_BAD_EXIT_CODE;
+ snapshot_creation_status = SCS_READY;
+ compute_next_snapshot_time();
+ goto out;
+ }
+ ret = rename_incomplete_snapshot(current_snapshot_creation_time);
+ if (ret < 0)
+ goto out;
+ snapshot_creation_status = SCS_RSYNC_SUCCESS;
+out:
+ rsync_pid = 0;
+ rsync_stopped = 0;
+ return ret;
+}
+
+static 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;
+}
+
+static int handle_sigchld(void)
+{
+ 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);
+}
+
+static int check_config(void)
+{
+ if (conf.unit_interval_arg <= 0) {
+ DSS_ERROR_LOG("bad unit interval: %i\n", conf.unit_interval_arg);
+ return -E_INVALID_NUMBER;
+ }
+ DSS_DEBUG_LOG("unit interval: %i day(s)\n", conf.unit_interval_arg);
+ if (conf.num_intervals_arg <= 0) {
+ DSS_ERROR_LOG("bad number of intervals %i\n", conf.num_intervals_arg);
+ return -E_INVALID_NUMBER;
+ }
+ DSS_DEBUG_LOG("number of intervals: %i\n", conf.num_intervals_arg);
+ return 1;
+}
+
+/* exits on errors */
+static void parse_config_file(int override)
+{
+ int ret;
+ char *config_file;
+ struct stat statbuf;
+ char *old_logfile_arg = NULL;
+ int old_daemon_given = 0;
+
+ if (conf.config_file_given)
+ config_file = dss_strdup(conf.config_file_arg);
+ else {
+ char *home = get_homedir();
+ config_file = make_message("%s/.dssrc", home);
+ free(home);
+ }
+ if (override) { /* SIGHUP */
+ if (conf.logfile_given)
+ old_logfile_arg = dss_strdup(conf.logfile_arg);
+ old_daemon_given = conf.daemon_given;
+ }
+
+ ret = stat(config_file, &statbuf);
+ if (ret && conf.config_file_given) {
+ ret = -ERRNO_TO_DSS_ERROR(errno);
+ DSS_ERROR_LOG("failed to stat config file %s\n", config_file);
+ goto out;
+ }
+ if (!ret) {
+ struct cmdline_parser_params params = {
+ .override = override,
+ .initialize = 0,
+ .check_required = 1,
+ .check_ambiguity = 0,
+ .print_errors = 1
+ };
+ cmdline_parser_config_file(config_file, &conf, ¶ms);
+ }
+ ret = check_config();
+ if (ret < 0)
+ goto out;
+ if (override) {
+ /* don't change daemon mode on SIGHUP */
+ conf.daemon_given = old_daemon_given;
+ close_log(logfile);
+ logfile = NULL;
+ if (conf.logfile_given)
+ free(old_logfile_arg);
+ else if (conf.daemon_given) { /* re-use old logfile */
+ conf.logfile_arg = old_logfile_arg;
+ conf.logfile_given = 1;
+ }
+ }
+ if (conf.logfile_given) {
+ logfile = open_log(conf.logfile_arg);
+ log_welcome(conf.loglevel_arg);
+ }
+ DSS_EMERG_LOG("loglevel: %d\n", conf.loglevel_arg);
+// cmdline_parser_dump(logfile? logfile : stdout, &conf);
+out:
+ free(config_file);
+ if (ret >= 0)
+ return;
+ DSS_EMERG_LOG("%s\n", dss_strerror(-ret));
+ exit(EXIT_FAILURE);
+}
+
+static void change_to_dest_dir(void)
+{
+ int ret;
+
+ DSS_INFO_LOG("changing cwd to %s\n", conf.dest_dir_arg);
+ ret = dss_chdir(conf.dest_dir_arg);
+ if (ret >= 0)
+ return;
+ DSS_EMERG_LOG("%s\n", dss_strerror(-ret));
+ exit(EXIT_FAILURE);
+}
+
+static void handle_sighup(void)
+{
+ DSS_NOTICE_LOG("SIGHUP\n");
+ parse_config_file(1);
+ change_to_dest_dir();
+}
+
+static void handle_signal(void)
+{
+ int sig, ret = next_signal();
+
+ if (ret <= 0)
+ goto out;