+static int wait_for_remove_process(void)
+{
+ int status, ret;
+
+ assert(remove_pid);
+ assert(
+ snapshot_removal_status == HS_PRE_RUNNING ||
+ snapshot_removal_status == HS_RUNNING ||
+ snapshot_removal_status == HS_POST_RUNNING
+ );
+ ret = wait_for_process(remove_pid, &status);
+ if (ret < 0)
+ return ret;
+ return handle_remove_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)create_pid);
+ ret = -E_INVOLUNTARY_EXIT;
+ snapshot_creation_status = HS_READY;
+ goto out;
+ }
+ es = WEXITSTATUS(status);
+ /*
+ * Restart rsync on non-fatal errors:
+ * 12: Error in rsync protocol data stream
+ * 13: Errors with program diagnostics
+ */
+ if (es == 12 || es == 13) {
+ DSS_WARNING_LOG("rsync process %d returned %d -- restarting\n",
+ (int)create_pid, es);
+ snapshot_creation_status = HS_NEEDS_RESTART;
+ next_snapshot_time = get_current_time() + 60;
+ ret = 1;
+ goto out;
+ }
+ if (es != 0 && es != 23 && es != 24) {
+ DSS_ERROR_LOG("rsync process %d returned %d\n", (int)create_pid, es);
+ ret = -E_BAD_EXIT_CODE;
+ snapshot_creation_status = HS_READY;
+ goto out;
+ }
+ ret = rename_incomplete_snapshot(current_snapshot_creation_time);
+ if (ret < 0)
+ goto out;
+ snapshot_creation_status = HS_SUCCESS;
+ free(name_of_reference_snapshot);
+ name_of_reference_snapshot = NULL;
+out:
+ create_process_stopped = 0;
+ return ret;
+}
+
+static int handle_pre_create_hook_exit(int status)
+{
+ int es, ret;
+ static int warn_count;
+
+ if (!WIFEXITED(status)) {
+ snapshot_creation_status = HS_READY;
+ ret = -E_INVOLUNTARY_EXIT;
+ goto out;
+ }
+ es = WEXITSTATUS(status);
+ if (es) {
+ if (!warn_count--) {
+ DSS_NOTICE_LOG("pre_create_hook %s returned %d\n",
+ conf.pre_create_hook_arg, es);
+ DSS_NOTICE_LOG("deferring snapshot creation...\n");
+ warn_count = 60; /* warn only once per hour */
+ }
+ next_snapshot_time = get_current_time() + 60;
+ snapshot_creation_status = HS_READY;
+ ret = 0;
+ goto out;
+ }
+ warn_count = 0;
+ snapshot_creation_status = HS_PRE_SUCCESS;
+ ret = 1;
+out:
+ return ret;
+}
+
+static int handle_sigchld(void)
+{
+ pid_t pid;
+ int status, ret = reap_child(&pid, &status);
+
+ if (ret <= 0)
+ return ret;
+
+ if (pid == create_pid) {
+ switch (snapshot_creation_status) {
+ case HS_PRE_RUNNING:
+ ret = handle_pre_create_hook_exit(status);
+ break;
+ case HS_RUNNING:
+ ret = handle_rsync_exit(status);
+ break;
+ case HS_POST_RUNNING:
+ snapshot_creation_status = HS_READY;
+ ret = 1;
+ break;
+ default:
+ DSS_EMERG_LOG("BUG: create can't die in status %d\n",
+ snapshot_creation_status);
+ return -E_BUG;
+ }
+ create_pid = 0;
+ return ret;
+ }
+ if (pid == remove_pid) {
+ ret = handle_remove_exit(status);
+ if (ret < 0)
+ return ret;
+ return ret;
+ }
+ DSS_EMERG_LOG("BUG: unknown process %d died\n", (int)pid);
+ return -E_BUG;
+}
+
+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;
+}
+
+/*
+ * Returns < 0 on errors, 0 if no config file is given and > 0 if the config
+ * file was read successfully.
+ */
+static int parse_config_file(int override)
+{
+ int ret, config_file_exists;
+ 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;
+ }
+
+ config_file_exists = !stat(config_file, &statbuf);
+ if (!config_file_exists && 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 (config_file_exists) {
+ struct cmdline_parser_params params = {
+ .override = override,
+ .initialize = 0,
+ .check_required = 1,
+ .check_ambiguity = 0,
+ .print_errors = 1
+ };
+ if (override) { /* invalidate all rsync options */
+ int i;
+
+ for (i = 0; i < conf.rsync_option_given; i++) {
+ free(conf.rsync_option_arg[i]);
+ conf.rsync_option_arg[i] = NULL;
+ }
+ conf.rsync_option_given = 0;
+ }
+ 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_DEBUG_LOG("loglevel: %d\n", conf.loglevel_arg);
+ ret = config_file_exists;
+out:
+ free(config_file);
+ if (ret < 0)
+ DSS_EMERG_LOG("%s\n", dss_strerror(-ret));
+ return ret;
+}
+
+static int change_to_dest_dir(void)
+{
+ DSS_INFO_LOG("changing cwd to %s\n", conf.dest_dir_arg);
+ return dss_chdir(conf.dest_dir_arg);
+}
+
+static int handle_sighup(void)
+{
+ int ret;
+
+ DSS_NOTICE_LOG("SIGHUP, re-reading config\n");
+ dump_dss_config("old");
+ ret = parse_config_file(1);
+ if (ret < 0)
+ return ret;
+ dump_dss_config("reloaded");
+ invalidate_next_snapshot_time();
+ return change_to_dest_dir();
+}
+
+static int handle_signal(void)