+ const char *msg = lls_strerror(-lopsub_ret);
+ if (*errctx)
+ DSS_ERROR_LOG(("%s: %s\n", *errctx, msg));
+ else
+ DSS_ERROR_LOG(("%s\n", msg));
+ free(*errctx);
+ *errctx = NULL;
+ return -E_LOPSUB;
+}
+
+static int parse_config_file(bool sighup, const struct lls_command *cmd)
+{
+ int ret, fd = -1;
+ char *config_file = get_config_file_name();
+ struct stat statbuf;
+ void *map;
+ size_t sz;
+ int cf_argc;
+ char **cf_argv, *errctx = NULL;
+ struct lls_parse_result *cf_lpr, *merged_lpr, *clpr;
+ const char *subcmd_name;
+
+ ret = open(config_file, O_RDONLY);
+ if (ret < 0) {
+ if (errno != ENOENT || OPT_GIVEN(DSS, CONFIG_FILE)) {
+ ret = -ERRNO_TO_DSS_ERROR(errno);
+ DSS_ERROR_LOG(("config file %s can not be opened\n",
+ config_file));
+ goto out;
+ }
+ /* no config file -- nothing to do */
+ ret = 0;
+ goto success;
+ }
+ fd = ret;
+ ret = fstat(fd, &statbuf);
+ if (ret < 0) {
+ ret = -ERRNO_TO_DSS_ERROR(errno);
+ DSS_ERROR_LOG(("failed to stat config file %s\n", config_file));
+ goto close_fd;
+ }
+ sz = statbuf.st_size;
+ if (sz == 0) { /* config file is empty -- nothing to do */
+ ret = 0;
+ goto success;
+ }
+ map = mmap(NULL, sz, PROT_READ, MAP_PRIVATE, fd, 0);
+ if (map == MAP_FAILED) {
+ ret = -ERRNO_TO_DSS_ERROR(errno);
+ DSS_ERROR_LOG(("failed to mmap config file %s\n",
+ config_file));
+ goto close_fd;
+ }
+ if (cmd == CMD_PTR(DSS))
+ subcmd_name = NULL;
+ else
+ subcmd_name = lls_command_name(cmd);
+ ret = lls_convert_config(map, sz, subcmd_name, &cf_argv, &errctx);
+ munmap(map, sz);
+ if (ret < 0) {
+ DSS_ERROR_LOG(("failed to convert config file %s\n",
+ config_file));
+ ret = lopsub_error(ret, &errctx);
+ goto close_fd;
+ }
+ cf_argc = ret;
+ ret = lls_parse(cf_argc, cf_argv, cmd, &cf_lpr, &errctx);
+ lls_free_argv(cf_argv);
+ if (ret < 0) {
+ ret = lopsub_error(ret, &errctx);
+ goto close_fd;
+ }
+ clpr = cmd == CMD_PTR(DSS)? cmdline_lpr : cmdline_sublpr;
+ if (sighup) /* config file overrides command line */
+ ret = lls_merge(cf_lpr, clpr, cmd, &merged_lpr, &errctx);
+ else /* command line options overrride config file options */
+ ret = lls_merge(clpr, cf_lpr, cmd, &merged_lpr, &errctx);
+ lls_free_parse_result(cf_lpr, cmd);
+ if (ret < 0) {
+ ret = lopsub_error(ret, &errctx);
+ goto close_fd;
+ }
+ ret = 1;
+success:
+ assert(ret >= 0);
+ DSS_DEBUG_LOG(("loglevel: %d\n", OPT_UINT32_VAL(DSS, LOGLEVEL)));
+ if (cmd != CMD_PTR(DSS)) {
+ if (ret > 0) {
+ if (sublpr != cmdline_sublpr)
+ lls_free_parse_result(sublpr, cmd);
+ sublpr = merged_lpr;
+ } else
+ sublpr = cmdline_sublpr;
+ } else {
+ if (ret > 0) {
+ if (lpr != cmdline_lpr)
+ lls_free_parse_result(lpr, cmd);
+ lpr = merged_lpr;
+ } else
+ lpr = cmdline_lpr;
+ }
+close_fd:
+ if (fd >= 0)
+ close(fd);
+out:
+ free(config_file);
+ if (ret < 0)
+ DSS_EMERG_LOG(("%s\n", dss_strerror(-ret)));
+ return ret;
+}
+
+static int handle_sighup(void)
+{
+ int ret;
+
+ DSS_NOTICE_LOG(("SIGHUP, re-reading config\n"));
+ dump_dss_config("old");
+ ret = parse_config_file(true /* SIGHUP */, CMD_PTR(DSS));
+ if (ret < 0)
+ return ret;
+ ret = parse_config_file(true /* SIGHUP */, CMD_PTR(RUN));
+ if (ret < 0)
+ return ret;
+ ret = check_config();
+ if (ret < 0)
+ return ret;
+ close_log(logfile);
+ logfile = NULL;
+ if (OPT_GIVEN(RUN, DAEMON) || daemonized) {
+ logfile = open_log(OPT_STRING_VAL(RUN, LOGFILE));
+ log_welcome(OPT_UINT32_VAL(DSS, LOGLEVEL));
+ daemonized = true;
+ }
+ dump_dss_config("reloaded");
+ invalidate_next_snapshot_time();
+ return 1;
+}
+
+static void kill_children(void)
+{
+ restart_create_process();
+ dss_kill(create_pid, SIGTERM, NULL);
+ dss_kill(remove_pid, SIGTERM, NULL);
+}
+
+static int handle_signal(void)
+{
+ int sig, ret = next_signal();
+
+ if (ret <= 0)
+ goto out;
+ sig = ret;
+ switch (sig) {
+ case SIGINT:
+ case SIGTERM:
+ return -E_SIGNAL;
+ case SIGHUP:
+ ret = handle_sighup();
+ break;
+ case SIGCHLD:
+ ret = handle_sigchld();
+ break;
+ }
+out:
+ if (ret < 0)
+ DSS_ERROR_LOG(("%s\n", dss_strerror(-ret)));
+ return ret;
+}
+
+/*
+ * We can not use rsync locally if the local user is different from the remote
+ * user or if the src dir is not on the local host (or both).
+ */
+static int use_rsync_locally(char *logname)
+{
+ const char *h = OPT_STRING_VAL(DSS, REMOTE_HOST);
+
+ if (strcmp(h, "localhost") && strcmp(h, "127.0.0.1"))
+ return 0;
+ if (OPT_GIVEN(DSS, REMOTE_USER) &&
+ strcmp(OPT_STRING_VAL(DSS, REMOTE_USER), logname))
+ return 0;
+ return 1;
+}
+
+static int rename_resume_snap(int64_t creation_time)
+{
+ struct snapshot_list sl;
+ struct snapshot *s = NULL;
+ char *new_name = incomplete_name(creation_time);
+ int ret;
+ const char *why;
+
+ sl.num_snapshots = 0;
+
+ ret = 0;
+ dss_get_snapshot_list(&sl);
+ /*
+ * Snapshot recycling: We first look at the newest snapshot. If this
+ * snapshot happens to be incomplete, the last rsync process was
+ * aborted and we reuse this one. Otherwise we look at snapshots which
+ * could be removed (outdated and redundant snapshots) as candidates
+ * for recycling. If no outdated/redundant snapshot exists, we check if
+ * there is an orphaned snapshot, which likely is useless anyway.
+ *
+ * Only if no existing snapshot is suitable for recycling, we bite the
+ * bullet and create a new one.
+ */
+ s = get_newest_snapshot(&sl);
+ if (!s) /* no snapshots at all */
+ goto out;
+ /* re-use last snapshot if it is incomplete */
+ why = "aborted";
+ if ((s->flags & SS_COMPLETE) == 0)
+ goto out;
+ why = "outdated";
+ s = find_outdated_snapshot(&sl);
+ if (s)
+ goto out;
+ why = "redundant";
+ s = find_redundant_snapshot(&sl);
+ if (s)
+ goto out;
+ why = "orphaned";
+ s = find_orphaned_snapshot(&sl);
+out:
+ if (s) {
+ DSS_NOTICE_LOG(("recycling %s snapshot %s\n", why, s->name));
+ ret = dss_rename(s->name, new_name);
+ }
+ if (ret >= 0)
+ DSS_NOTICE_LOG(("creating %s\n", new_name));
+ free(new_name);
+ free_snapshot_list(&sl);
+ return ret;
+}
+
+static void create_rsync_argv(char ***argv, int64_t *num)
+{
+ char *logname;
+ int i = 0, j, N;
+ struct snapshot_list sl;
+ static bool seeded;
+
+ dss_get_snapshot_list(&sl);
+ assert(!name_of_reference_snapshot);
+ name_of_reference_snapshot = name_of_newest_complete_snapshot(&sl);
+ free_snapshot_list(&sl);
+
+ /*
+ * We specify up to 6 arguments, one argument per given rsync option
+ * and one argument per given source dir. We also need space for the
+ * terminating NULL pointer.
+ */
+ N = OPT_GIVEN(DSS, RSYNC_OPTION) + OPT_GIVEN(DSS, SOURCE_DIR);
+ *argv = dss_malloc((7 + N) * sizeof(char *));
+ (*argv)[i++] = dss_strdup("rsync");
+ (*argv)[i++] = dss_strdup("-a");
+ (*argv)[i++] = dss_strdup("--delete");
+ if (!seeded) {
+ srandom((unsigned)time(NULL)); /* no need to be fancy here */
+ seeded = true;
+ }
+ if (1000 * (random() / (RAND_MAX + 1.0)) < OPT_UINT32_VAL(DSS, CHECKSUM)) {
+ DSS_NOTICE_LOG(("adding --checksum to rsync options\n"));
+ (*argv)[i++] = dss_strdup("--checksum");
+ }
+ for (j = 0; j < OPT_GIVEN(DSS, RSYNC_OPTION); j++)
+ (*argv)[i++] = dss_strdup(lls_string_val(j,
+ OPT_RESULT(DSS, RSYNC_OPTION)));
+ if (name_of_reference_snapshot) {
+ DSS_INFO_LOG(("using %s as reference\n", name_of_reference_snapshot));
+ (*argv)[i++] = make_message("--link-dest=../%s",
+ name_of_reference_snapshot);
+ } else
+ DSS_INFO_LOG(("no suitable reference snapshot found\n"));
+ logname = dss_logname();
+ if (use_rsync_locally(logname)) {
+ for (j = 0; j < OPT_GIVEN(DSS, SOURCE_DIR); j++)
+ (*argv)[i++] = dss_strdup(lls_string_val(j,
+ OPT_RESULT(DSS, SOURCE_DIR)));
+ } else {
+ /*
+ * dss-1.0 and earlier did not support multiple source
+ * directories. These versions appended a slash to the end of
+ * the source directory to make sure that only the contents of
+ * the single source directory, but not the directory itself,
+ * are copied to the destination. For multiple source
+ * directories, however, this is not a good idea because the
+ * source directories may well contain identical file names,
+ * which would then be copied to the same location on the
+ * destination, overwriting each other. Moreover, we want the
+ * directory on the destination match the source. To preserve
+ * the old behaviour, we thus have to special-case N=1.
+ */
+ for (j = 0; j < OPT_GIVEN(DSS, SOURCE_DIR); j++) {
+ (*argv)[i++] = make_message("%s@%s:%s%s",
+ OPT_GIVEN(DSS, REMOTE_USER)?
+ OPT_STRING_VAL(DSS, REMOTE_USER) : logname,
+ OPT_STRING_VAL(DSS, REMOTE_HOST),
+ lls_string_val(j, OPT_RESULT(DSS, SOURCE_DIR)),
+ N == 1? "/" : ""
+ );
+ }
+ }
+ free(logname);
+ *num = get_current_time();
+ (*argv)[i++] = incomplete_name(*num);
+ (*argv)[i++] = NULL;
+ for (j = 0; j < i; j++)
+ DSS_DEBUG_LOG(("argv[%d] = %s\n", j, (*argv)[j]));
+}
+
+static void free_rsync_argv(char **argv)
+{
+ int i;
+
+ if (!argv)