1 /* SPDX-License-Identifier: GPL-2.0 */
24 #include "gcc-compat.h"
38 #define CMD_PTR(_cname) lls_cmd(LSG_DSS_CMD_ ## _cname, dss_suite)
39 #define OPT_RESULT(_cname, _oname) (lls_opt_result(\
40 LSG_DSS_ ## _cname ## _OPT_ ## _oname, (CMD_PTR(_cname) == CMD_PTR(DSS))? lpr : sublpr))
41 #define OPT_GIVEN(_cname, _oname) (lls_opt_given(OPT_RESULT(_cname, _oname)))
42 #define OPT_STRING_VAL(_cname, _oname) (lls_string_val(0, \
43 OPT_RESULT(_cname, _oname)))
44 #define OPT_UINT32_VAL(_cname, _oname) (lls_uint32_val(0, \
45 OPT_RESULT(_cname, _oname)))
47 struct dss_user_data {int (*handler)(void);};
48 #define EXPORT_CMD_HANDLER(_cmd) const struct dss_user_data \
49 lsg_dss_com_ ## _cmd ## _user_data = { \
50 .handler = com_ ## _cmd \
54 * Command line and active options. We need to keep a copy of the parsed
55 * command line options for the SIGHUP case where we merge the command line
56 * options and the new config file options.
58 static struct lls_parse_result *cmdline_lpr, *lpr;
60 /** Parsed subcommand options. */
61 static struct lls_parse_result *cmdline_sublpr, *sublpr;
62 /* The executing subcommand (NULL at startup). */
63 static const struct lls_command *subcmd;
64 /** Wether daemon_init() was called. */
65 static bool daemonized;
66 /** Non-NULL if we log to a file. */
68 /* Realpath of the config file. */
69 static char *config_file;
70 /** The read end of the signal pipe */
71 static int signal_pipe;
72 /** Process id of current pre-create-hook/rsync/post-create-hook process. */
73 static pid_t create_pid;
74 /** Whether the pre-create-hook/rsync/post-create-hook is currently stopped. */
75 static int create_process_stopped;
76 /** How many times in a row the rsync command failed. */
77 static int num_consecutive_rsync_errors;
78 /** Process id of current pre-remove/rm/post-remove process. */
79 static pid_t remove_pid;
80 /** When the next snapshot is due. */
81 static int64_t next_snapshot_time;
82 /** When to try to remove something. */
83 static struct timeval next_removal_check;
84 /** Creation time of the snapshot currently being created. */
85 static int64_t current_snapshot_creation_time;
86 /* Set by the pre-rm hook, cleared by handle_remove_exit(). */
87 struct snapshot *snapshot_currently_being_removed;
88 /** Needed by the post-create hook. */
89 static char *path_to_last_complete_snapshot;
90 static char *name_of_reference_snapshot;
91 /** \sa \ref snap.h for details. */
92 enum hook_status snapshot_creation_status;
93 /** \sa \ref snap.h for details. */
94 enum hook_status snapshot_removal_status;
98 static const char *hook_status_description[] = {HOOK_STATUS_ARRAY};
100 /* may be called with ds == NULL. */
101 static int disk_space_low(struct disk_space *ds)
103 struct disk_space ds_struct;
107 int ret = get_disk_space(".", &ds_struct);
112 val = OPT_UINT32_VAL(DSS, MIN_FREE_MB);
114 if (ds->free_mb < val)
116 val = OPT_UINT32_VAL(DSS, MIN_FREE_PERCENT);
118 if (ds->percent_free < val)
120 val = OPT_UINT32_VAL(DSS, MIN_FREE_PERCENT_INODES);
122 if (ds->percent_free_inodes < val)
127 static void dump_dss_config(const char *msg)
129 const char dash[] = "-----------------------------";
132 FILE *log = logfile? logfile : stderr;
133 struct disk_space ds;
134 int64_t now = get_current_time();
136 if (OPT_UINT32_VAL(DSS, LOGLEVEL) > INFO)
139 fprintf(log, "%s <%s config> %s\n", dash, msg, dash);
140 fprintf(log, "\n*** disk space ***\n\n");
141 ret = get_disk_space(".", &ds);
143 DSS_INFO_LOG(("disk space low: %s\n", disk_space_low(&ds)?
147 DSS_ERROR_LOG(("can not get free disk space: %s\n",
148 dss_strerror(-ret)));
150 /* we continue on errors from get_disk_space */
152 fprintf(log, "\n*** non-default options ***\n\n");
153 lopsub_dump = lls_dump_parse_result(lpr, CMD_PTR(DSS), true);
154 fprintf(log, "%s", lopsub_dump);
156 fprintf(log, "\n*** non-default options for \"run\" ***\n\n");
157 lopsub_dump = lls_dump_parse_result(lpr, CMD_PTR(RUN), true);
158 fprintf(log, "%s", lopsub_dump);
160 fprintf(log, "\n*** internal state ***\n\n");
164 "snapshot_currently_being_removed: %s\n"
165 "path_to_last_complete_snapshot: %s\n"
166 "reference_snapshot: %s\n"
167 "snapshot_creation_status: %s\n"
168 "snapshot_removal_status: %s\n"
169 "num_consecutive_rsync_errors: %d\n"
172 logfile? OPT_STRING_VAL(RUN, LOGFILE) : "stderr",
173 snapshot_currently_being_removed?
174 snapshot_currently_being_removed->name : "(none)",
175 path_to_last_complete_snapshot?
176 path_to_last_complete_snapshot : "(none)",
177 name_of_reference_snapshot?
178 name_of_reference_snapshot : "(none)",
179 hook_status_description[snapshot_creation_status],
180 hook_status_description[snapshot_removal_status],
181 num_consecutive_rsync_errors
185 "create_pid: %" PRId32 "\n"
186 "create process is %sstopped\n"
189 create_process_stopped? "" : "not "
192 fprintf(log, "remove_pid: %" PRId32 "\n", remove_pid);
193 if (next_snapshot_time != 0)
194 fprintf(log, "next snapshot due in %" PRId64 " seconds\n",
195 next_snapshot_time - now);
196 if (current_snapshot_creation_time != 0)
197 fprintf(log, "current_snapshot_creation_time: %"
198 PRId64 " (%" PRId64 " seconds ago)\n",
199 current_snapshot_creation_time,
200 now - current_snapshot_creation_time
202 if (next_removal_check.tv_sec != 0) {
203 fprintf(log, "next removal check: %llu (%llu seconds ago)\n",
204 (long long unsigned)next_removal_check.tv_sec,
205 now - (long long unsigned)next_removal_check.tv_sec
209 fprintf(log, "%s </%s config> %s\n", dash, msg, dash);
212 static int loglevel = -1;
213 static const char *location_file = NULL;
214 static int location_line = -1;
215 static const char *location_func = NULL;
217 void dss_log_set_params(int ll, const char *file, int line, const char *func)
220 location_file = file;
221 location_line = line;
222 location_func = func;
226 * The log function of dss.
228 * \param ll Loglevel.
229 * \param fml Usual format string.
231 * All DSS_XXX_LOG() macros use this function.
233 __printf_1_2 void dss_log(const char* fmt,...)
240 int lpr_ll = lpr? OPT_UINT32_VAL(DSS, LOGLEVEL) : WARNING;
242 if (loglevel < lpr_ll)
244 outfd = logfile? logfile : stderr;
245 if (subcmd == CMD_PTR(RUN)) {
248 strftime(str, sizeof(str), "%b %d %H:%M:%S", tm);
249 fprintf(outfd, "%s ", str);
251 fprintf(outfd, "%i: ", loglevel);
253 if (subcmd == CMD_PTR(RUN))
254 #ifdef DSS_NO_FUNC_NAMES
255 fprintf(outfd, "%s:%d: ", location_file, location_line);
257 fprintf(outfd, "%s: ", location_func);
260 vfprintf(outfd, fmt, argp);
265 * Print a message either to stdout or to the log file.
267 static __printf_1_2 void dss_msg(const char* fmt,...)
269 FILE *outfd = logfile? logfile : stdout;
272 vfprintf(outfd, fmt, argp);
276 static void set_config_file_name(void)
279 if (OPT_GIVEN(DSS, CONFIG_FILE)) {
280 const char *arg = OPT_STRING_VAL(DSS, CONFIG_FILE);
281 config_file = realpath(arg, NULL);
283 DSS_EMERG_LOG(("could not resolve path %s: %s\n", arg,
288 char *home = get_homedir();
289 char *arg = make_message("%s/.dssrc", home);
291 config_file = realpath(arg, NULL);
297 DSS_DEBUG_LOG(("config file: %s\n", config_file));
300 static int send_signal(int sig, bool wait)
303 int ret = get_dss_pid(config_file, &pid);
309 if (OPT_GIVEN(DSS, DRY_RUN)) {
310 dss_msg("%d\n", (int)pid);
313 DSS_NOTICE_LOG(("sending signal %d to pid %d\n", sig, (int)pid));
314 ret = kill(pid, sig);
316 return -ERRNO_TO_DSS_ERROR(errno);
320 ts.tv_sec = ms / 1000;
321 ts.tv_nsec = (ms % 1000) * 1000 * 1000;
322 ret = nanosleep(&ts, NULL);
324 return -ERRNO_TO_DSS_ERROR(errno);
328 return -ERRNO_TO_DSS_ERROR(errno);
333 return -E_KILL_TIMEOUT;
337 const char * const name;
342 * The table below was taken 2016 from proc/sig.c of procps-3.2.8. Copyright
343 * 1998-2003 by Albert Cahalan, GPLv2.
345 static const struct signal_info signal_table[] = {
346 {"ABRT", SIGABRT}, /* IOT */
349 {"CHLD", SIGCHLD}, /* CLD */
358 {"POLL", SIGPOLL}, /* IO */
367 {"STKFLT", SIGSTKFLT},
370 {"SYS", SIGSYS}, /* UNUSED */
379 {"VTALRM", SIGVTALRM},
385 #define SIGNAL_TABLE_SIZE (sizeof(signal_table) / sizeof(signal_table[0]))
390 static int com_kill(void)
392 bool w_given = OPT_GIVEN(KILL, WAIT);
393 const char *arg = OPT_STRING_VAL(KILL, SIGNAL);
396 if (*arg >= '0' && *arg <= '9') {
398 ret = dss_atoi64(arg, &val);
401 if (val < 0 || val > SIGRTMAX)
402 return -ERRNO_TO_DSS_ERROR(EINVAL);
403 return send_signal(val, w_given);
405 if (strncasecmp(arg, "sig", 3) == 0)
407 if (strcasecmp(arg, "CLD") == 0)
408 return send_signal(SIGCHLD, w_given);
409 if (strcasecmp(arg, "IOT") == 0)
410 return send_signal(SIGABRT, w_given);
411 for (i = 0; i < SIGNAL_TABLE_SIZE; i++)
412 if (strcasecmp(arg, signal_table[i].name) == 0)
413 return send_signal(signal_table[i].num, w_given);
414 DSS_ERROR_LOG(("invalid sigspec: %s\n", arg));
415 return -ERRNO_TO_DSS_ERROR(EINVAL);
417 EXPORT_CMD_HANDLER(kill);
419 static void dss_get_snapshot_list(struct snapshot_list *sl)
421 get_snapshot_list(sl, OPT_UINT32_VAL(DSS, UNIT_INTERVAL),
422 OPT_UINT32_VAL(DSS, NUM_INTERVALS));
425 static int64_t compute_next_snapshot_time(void)
427 int64_t x = 0, now = get_current_time(), unit_interval
428 = 24 * 3600 * OPT_UINT32_VAL(DSS, UNIT_INTERVAL), ret,
429 last_completion_time;
430 unsigned wanted = desired_number_of_snapshots(0,
431 OPT_UINT32_VAL(DSS, NUM_INTERVALS)),
434 struct snapshot *s = NULL;
435 struct snapshot_list sl;
437 dss_get_snapshot_list(&sl);
438 FOR_EACH_SNAPSHOT(s, i, &sl) {
439 if (!(s->flags & SS_COMPLETE))
442 x += s->completion_time - s->creation_time;
443 last_completion_time = s->completion_time;
448 if (num_complete == 0)
450 x /= num_complete; /* avg time to create one snapshot */
451 if (unit_interval < x * wanted) /* oops, no sleep at all */
453 ret = last_completion_time + unit_interval / wanted - x;
455 free_snapshot_list(&sl);
459 static inline void invalidate_next_snapshot_time(void)
461 next_snapshot_time = 0;
464 static inline int next_snapshot_time_is_valid(void)
466 return next_snapshot_time != 0;
469 static int next_snapshot_is_due(void)
471 int64_t now = get_current_time();
473 if (!next_snapshot_time_is_valid())
474 next_snapshot_time = compute_next_snapshot_time();
475 if (next_snapshot_time <= now) {
476 DSS_DEBUG_LOG(("next snapshot: now\n"));
479 DSS_DEBUG_LOG(("next snapshot due in %" PRId64 " seconds\n",
480 next_snapshot_time - now));
484 static void pre_create_hook(void)
486 assert(snapshot_creation_status == HS_READY);
487 /* make sure that the next snapshot time will be recomputed */
488 invalidate_next_snapshot_time();
489 DSS_DEBUG_LOG(("executing %s\n", OPT_STRING_VAL(DSS, PRE_CREATE_HOOK)));
490 dss_exec_cmdline_pid(&create_pid, OPT_STRING_VAL(DSS, PRE_CREATE_HOOK));
491 snapshot_creation_status = HS_PRE_RUNNING;
494 static void pre_remove_hook(struct snapshot *s, const char *why)
500 DSS_DEBUG_LOG(("%s snapshot %s\n", why, s->name));
501 assert(snapshot_removal_status == HS_READY);
502 assert(remove_pid == 0);
503 assert(!snapshot_currently_being_removed);
505 snapshot_currently_being_removed = dss_malloc(sizeof(struct snapshot));
506 *snapshot_currently_being_removed = *s;
507 snapshot_currently_being_removed->name = dss_strdup(s->name);
509 cmd = make_message("%s %s/%s", OPT_STRING_VAL(DSS, PRE_REMOVE_HOOK),
510 OPT_STRING_VAL(DSS, DEST_DIR), s->name);
511 DSS_DEBUG_LOG(("executing %s\n", cmd));
512 dss_exec_cmdline_pid(&remove_pid, cmd);
514 snapshot_removal_status = HS_PRE_RUNNING;
517 static int exec_rm(void)
519 struct snapshot *s = snapshot_currently_being_removed;
520 char *new_name = being_deleted_name(s);
529 assert(snapshot_removal_status == HS_PRE_SUCCESS);
530 assert(remove_pid == 0);
532 DSS_NOTICE_LOG(("removing %s (interval = %i)\n", s->name, s->interval));
533 ret = dss_rename(s->name, new_name);
536 dss_exec(&remove_pid, argv[0], argv);
537 snapshot_removal_status = HS_RUNNING;
543 static int snapshot_is_being_created(struct snapshot *s)
545 return s->creation_time == current_snapshot_creation_time;
548 static struct snapshot *find_orphaned_snapshot(struct snapshot_list *sl)
553 DSS_DEBUG_LOG(("looking for old incomplete snapshots\n"));
554 FOR_EACH_SNAPSHOT(s, i, sl) {
555 if (snapshot_is_being_created(s))
558 * We know that no rm is currently running, so if s is marked
559 * as being deleted, a previously started rm must have failed.
561 if (s->flags & SS_BEING_DELETED)
564 if (s->flags & SS_COMPLETE) /* good snapshot */
567 * This snapshot is incomplete and it is not the snapshot
568 * currently being created. However, we must not remove it if
569 * rsync is about to be restarted. As only the newest snapshot
570 * can be restarted, this snapshot is orphaned if it is not the
571 * newest snapshot or if we are not about to restart rsync.
573 if (get_newest_snapshot(sl) != s)
575 if (snapshot_creation_status != HS_NEEDS_RESTART)
578 /* no orphaned snapshots */
582 static int is_reference_snapshot(struct snapshot *s)
584 if (!name_of_reference_snapshot)
586 return strcmp(s->name, name_of_reference_snapshot)? 0 : 1;
589 static struct snapshot *find_redundant_snapshot(struct snapshot_list *sl)
593 unsigned missing = 0;
594 uint32_t N = OPT_UINT32_VAL(DSS, NUM_INTERVALS);
596 DSS_DEBUG_LOG(("looking for intervals containing too many snapshots\n"));
597 for (interval = N - 1; interval >= 0; interval--) {
598 unsigned keep = desired_number_of_snapshots(interval, N);
599 unsigned num = sl->interval_count[interval];
600 struct snapshot *victim = NULL, *prev = NULL;
601 int64_t score = LONG_MAX;
604 missing += keep - num;
605 if (keep + missing >= num)
607 /* redundant snapshot in this interval, pick snapshot with lowest score */
608 FOR_EACH_SNAPSHOT(s, i, sl) {
611 if (snapshot_is_being_created(s))
613 if (is_reference_snapshot(s))
615 if (s->interval > interval) {
619 if (s->interval < interval)
627 /* check if s is a better victim */
628 this_score = s->creation_time - prev->creation_time;
629 assert(this_score >= 0);
630 if (this_score < score) {
642 static struct snapshot *find_outdated_snapshot(struct snapshot_list *sl)
647 DSS_DEBUG_LOG(("looking for snapshots belonging to intervals >= %d\n",
648 OPT_UINT32_VAL(DSS, NUM_INTERVALS)));
649 FOR_EACH_SNAPSHOT(s, i, sl) {
650 if (snapshot_is_being_created(s))
652 if (is_reference_snapshot(s))
654 if (s->interval < OPT_UINT32_VAL(DSS, NUM_INTERVALS))
661 static struct snapshot *find_oldest_removable_snapshot(struct snapshot_list *sl)
664 struct snapshot *s, *ref = NULL;
666 DSS_DEBUG_LOG(("picking snapshot with earliest creation time\n"));
667 num_complete = num_complete_snapshots(sl);
668 if (num_complete <= OPT_UINT32_VAL(DSS, MIN_COMPLETE))
670 FOR_EACH_SNAPSHOT(s, i, sl) {
671 if (snapshot_is_being_created(s))
673 if (is_reference_snapshot(s)) { /* avoid this one */
680 DSS_WARNING_LOG(("removing reference snapshot %s\n", ref->name));
684 /* returns NULL <==> *reason is set to NULL */
685 static struct snapshot *find_removable_snapshot(struct snapshot_list *sl,
686 bool try_hard, char **reason)
688 struct snapshot *victim;
691 * Don't remove anything if there is free space and we have fewer
692 * snapshots than configured, plus one. This way there is always one
693 * snapshot that can be recycled.
695 if (!try_hard && sl->num_snapshots <=
696 1 << OPT_UINT32_VAL(DSS, NUM_INTERVALS))
698 victim = find_orphaned_snapshot(sl);
700 *reason = make_message("orphaned");
703 victim = find_outdated_snapshot(sl);
705 *reason = make_message("outdated");
708 if (!OPT_GIVEN(DSS, KEEP_REDUNDANT)) {
709 victim = find_redundant_snapshot(sl);
711 *reason = make_message("redundant");
717 DSS_WARNING_LOG(("nothing obvious to remove\n"));
718 victim = find_oldest_removable_snapshot(sl);
720 *reason = make_message("oldest");
728 static int rename_incomplete_snapshot(int64_t start)
735 * We don't want the dss_rename() below to fail with EEXIST because the
736 * last complete snapshot was created (and completed) in the same
737 * second as this one.
739 while ((now = get_current_time()) == start)
741 free(path_to_last_complete_snapshot);
742 ret = complete_name(start, now, &path_to_last_complete_snapshot);
745 old_name = incomplete_name(start);
746 ret = dss_rename(old_name, path_to_last_complete_snapshot);
748 DSS_NOTICE_LOG(("%s -> %s\n", old_name,
749 path_to_last_complete_snapshot));
754 static int try_to_free_disk_space(void)
757 struct snapshot_list sl;
758 struct snapshot *victim;
763 ret = disk_space_low(NULL);
766 low_disk_space = ret;
767 gettimeofday(&now, NULL);
768 if (tv_diff(&next_removal_check, &now, NULL) > 0)
770 if (!low_disk_space) {
771 if (snapshot_creation_status != HS_READY)
773 if (next_snapshot_is_due())
776 /* Idle or low disk space, look at existing snapshots. */
777 dss_get_snapshot_list(&sl);
778 victim = find_removable_snapshot(&sl, low_disk_space, &why);
780 pre_remove_hook(victim, why);
783 free_snapshot_list(&sl);
788 DSS_CRIT_LOG(("uhuhu: disk space low and nothing to remove\n"));
789 return -ERRNO_TO_DSS_ERROR(ENOSPC);
792 static void post_create_hook(void)
794 char *cmd = make_message("%s %s/%s",
795 OPT_STRING_VAL(DSS, POST_CREATE_HOOK),
796 OPT_STRING_VAL(DSS, DEST_DIR), path_to_last_complete_snapshot);
797 DSS_NOTICE_LOG(("executing %s\n", cmd));
798 dss_exec_cmdline_pid(&create_pid, cmd);
800 snapshot_creation_status = HS_POST_RUNNING;
803 static void post_remove_hook(void)
806 struct snapshot *s = snapshot_currently_being_removed;
810 cmd = make_message("%s %s/%s", OPT_STRING_VAL(DSS, POST_REMOVE_HOOK),
811 OPT_STRING_VAL(DSS, DEST_DIR), s->name);
812 DSS_NOTICE_LOG(("executing %s\n", cmd));
813 dss_exec_cmdline_pid(&remove_pid, cmd);
815 snapshot_removal_status = HS_POST_RUNNING;
818 static void dss_kill(pid_t pid, int sig, const char *msg)
820 const char *signame, *process_name;
825 case SIGTERM: signame = "TERM"; break;
826 case SIGSTOP: signame = "STOP"; break;
827 case SIGCONT: signame = "CONT"; break;
828 default: signame = "????";
831 if (pid == create_pid)
832 process_name = "create";
833 else if (pid == remove_pid)
834 process_name = "remove";
835 else process_name = "??????";
838 DSS_INFO_LOG(("%s\n", msg));
839 DSS_DEBUG_LOG(("sending signal %d (%s) to pid %d (%s process)\n",
840 sig, signame, (int)pid, process_name));
841 if (kill(pid, sig) >= 0)
843 DSS_INFO_LOG(("failed to send signal %d (%s) to pid %d (%s process)\n",
844 sig, signame, (int)pid, process_name));
847 static void stop_create_process(void)
849 if (create_process_stopped)
851 dss_kill(create_pid, SIGSTOP, "suspending create process");
852 create_process_stopped = 1;
855 static void restart_create_process(void)
857 if (!create_process_stopped)
859 dss_kill(create_pid, SIGCONT, "resuming create process");
860 create_process_stopped = 0;
864 * Print a log message about the exit status of a child.
866 static void log_termination_msg(pid_t pid, int status)
868 if (WIFEXITED(status))
869 DSS_INFO_LOG(("child %i exited. Exit status: %i\n", (int)pid,
870 WEXITSTATUS(status)));
871 else if (WIFSIGNALED(status))
872 DSS_NOTICE_LOG(("child %i was killed by signal %i\n", (int)pid,
875 DSS_WARNING_LOG(("child %i terminated abormally\n", (int)pid));
878 static int wait_for_process(pid_t pid, int *status)
882 DSS_DEBUG_LOG(("Waiting for process %d to terminate\n", (int)pid));
887 FD_SET(signal_pipe, &rfds);
888 ret = dss_select(signal_pipe + 1, &rfds, NULL, NULL);
894 if (ret == SIGCHLD) {
895 ret = waitpid(pid, status, 0);
898 if (errno != EINTR) { /* error */
899 ret = -ERRNO_TO_DSS_ERROR(errno);
903 /* SIGINT or SIGTERM */
904 dss_kill(pid, SIGTERM, "killing child process");
907 DSS_ERROR_LOG(("failed to wait for process %d\n", (int)pid));
909 log_termination_msg(pid, *status);
913 static void handle_pre_remove_exit(int status)
915 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
916 snapshot_removal_status = HS_READY;
917 gettimeofday(&next_removal_check, NULL);
918 next_removal_check.tv_sec += 60;
921 snapshot_removal_status = HS_PRE_SUCCESS;
924 static int handle_rm_exit(int status)
926 if (!WIFEXITED(status)) {
927 snapshot_removal_status = HS_READY;
928 return -E_INVOLUNTARY_EXIT;
930 if (WEXITSTATUS(status)) {
931 snapshot_removal_status = HS_READY;
932 return -E_BAD_EXIT_CODE;
934 snapshot_removal_status = HS_SUCCESS;
938 static void handle_post_remove_exit(void)
940 snapshot_removal_status = HS_READY;
943 static int handle_remove_exit(int status)
946 struct snapshot *s = snapshot_currently_being_removed;
949 switch (snapshot_removal_status) {
951 handle_pre_remove_exit(status);
955 ret = handle_rm_exit(status);
957 case HS_POST_RUNNING:
958 handle_post_remove_exit();
964 if (snapshot_removal_status == HS_READY) {
967 snapshot_currently_being_removed = NULL;
973 static int wait_for_remove_process(void)
979 snapshot_removal_status == HS_PRE_RUNNING ||
980 snapshot_removal_status == HS_RUNNING ||
981 snapshot_removal_status == HS_POST_RUNNING
983 ret = wait_for_process(remove_pid, &status);
986 return handle_remove_exit(status);
989 static int handle_rsync_exit(int status)
993 if (!WIFEXITED(status)) {
994 DSS_ERROR_LOG(("rsync process %d died involuntary\n", (int)create_pid));
995 ret = -E_INVOLUNTARY_EXIT;
996 snapshot_creation_status = HS_READY;
999 es = WEXITSTATUS(status);
1001 * Restart rsync on non-fatal errors:
1002 * 24: Partial transfer due to vanished source files
1004 if (es != 0 && es != 24) {
1005 DSS_WARNING_LOG(("rsync exit code %d, error count %d\n",
1006 es, ++num_consecutive_rsync_errors));
1007 if (!logfile) { /* called by com_run() */
1008 ret = -E_BAD_EXIT_CODE;
1011 if (num_consecutive_rsync_errors >
1012 OPT_UINT32_VAL(RUN, MAX_RSYNC_ERRORS)) {
1013 ret = -E_TOO_MANY_RSYNC_ERRORS;
1014 snapshot_creation_status = HS_READY;
1017 DSS_WARNING_LOG(("restarting rsync process\n"));
1018 snapshot_creation_status = HS_NEEDS_RESTART;
1019 next_snapshot_time = get_current_time() + 60;
1023 num_consecutive_rsync_errors = 0;
1024 ret = rename_incomplete_snapshot(current_snapshot_creation_time);
1027 snapshot_creation_status = HS_SUCCESS;
1028 free(name_of_reference_snapshot);
1029 name_of_reference_snapshot = NULL;
1031 create_process_stopped = 0;
1035 static int handle_pre_create_hook_exit(int status)
1038 static int warn_count;
1040 if (!WIFEXITED(status)) {
1041 snapshot_creation_status = HS_READY;
1042 ret = -E_INVOLUNTARY_EXIT;
1045 es = WEXITSTATUS(status);
1047 if (!warn_count--) {
1048 DSS_NOTICE_LOG(("pre_create_hook %s returned %d\n",
1049 OPT_STRING_VAL(DSS, PRE_CREATE_HOOK), es));
1050 DSS_NOTICE_LOG(("deferring snapshot creation...\n"));
1051 warn_count = 60; /* warn only once per hour */
1053 next_snapshot_time = get_current_time() + 60;
1054 snapshot_creation_status = HS_READY;
1059 snapshot_creation_status = HS_PRE_SUCCESS;
1065 static int handle_sigchld(void)
1068 int status, ret = reap_child(&pid, &status);
1073 if (pid == create_pid) {
1074 switch (snapshot_creation_status) {
1075 case HS_PRE_RUNNING:
1076 ret = handle_pre_create_hook_exit(status);
1079 ret = handle_rsync_exit(status);
1081 case HS_POST_RUNNING:
1082 snapshot_creation_status = HS_READY;
1086 DSS_EMERG_LOG(("BUG: create can't die in status %d\n",
1087 snapshot_creation_status));
1093 if (pid == remove_pid) {
1094 ret = handle_remove_exit(status);
1099 DSS_EMERG_LOG(("BUG: unknown process %d died\n", (int)pid));
1103 /* also checks if . is a mountpoint, if --mountpoint was given */
1104 static int change_to_dest_dir(void)
1107 const char *dd = OPT_STRING_VAL(DSS, DEST_DIR);
1108 struct stat dot, dotdot;
1110 DSS_INFO_LOG(("changing cwd to %s\n", dd));
1111 if (chdir(dd) < 0) {
1112 ret = -ERRNO_TO_DSS_ERROR(errno);
1113 DSS_ERROR_LOG(("could not change cwd to %s\n", dd));
1116 if (!OPT_GIVEN(DSS, MOUNTPOINT))
1118 if (stat(".", &dot) < 0) {
1119 ret = -ERRNO_TO_DSS_ERROR(errno);
1120 DSS_ERROR_LOG(("could not stat .\n"));
1123 if (stat("..", &dotdot) < 0) {
1124 ret = -ERRNO_TO_DSS_ERROR(errno);
1125 DSS_ERROR_LOG(("could not stat ..\n"));
1128 if (dot.st_dev == dotdot.st_dev && dot.st_ino != dotdot.st_ino) {
1129 DSS_ERROR_LOG(("mountpoint check failed for %s\n", dd));
1130 return -E_MOUNTPOINT;
1135 static int check_config(void)
1137 uint32_t unit_interval = OPT_UINT32_VAL(DSS, UNIT_INTERVAL);
1138 uint32_t num_intervals = OPT_UINT32_VAL(DSS, NUM_INTERVALS);
1140 if (unit_interval == 0) {
1141 DSS_ERROR_LOG(("bad unit interval: %i\n", unit_interval));
1142 return -E_INVALID_NUMBER;
1144 DSS_DEBUG_LOG(("unit interval: %i day(s)\n", unit_interval));
1146 if (num_intervals == 0 || num_intervals > 30) {
1147 DSS_ERROR_LOG(("bad number of intervals: %i\n", num_intervals));
1148 return -E_INVALID_NUMBER;
1150 if (subcmd == CMD_PTR(RUN) || subcmd == CMD_PTR(CREATE))
1151 if (!OPT_GIVEN(DSS, SOURCE_DIR)) {
1152 DSS_ERROR_LOG(("--source-dir required\n"));
1155 if (subcmd == CMD_PTR(RUN) || subcmd == CMD_PTR(CREATE)
1156 || subcmd == CMD_PTR(LS) || subcmd == CMD_PTR(PRUNE)) {
1157 if (!OPT_GIVEN(DSS, DEST_DIR)) {
1158 DSS_ERROR_LOG(("--dest-dir required\n"));
1162 DSS_DEBUG_LOG(("number of intervals: %i\n", num_intervals));
1166 static int lopsub_error(int lopsub_ret, char **errctx)
1168 const char *msg = lls_strerror(-lopsub_ret);
1170 DSS_ERROR_LOG(("%s: %s\n", *errctx, msg));
1172 DSS_ERROR_LOG(("%s\n", msg));
1178 static int parse_config_file(bool sighup, const struct lls_command *cmd)
1181 struct stat statbuf;
1185 char **cf_argv, *errctx = NULL;
1186 struct lls_parse_result *cf_lpr, *merged_lpr, *clpr;
1187 const char *subcmd_name;
1189 ret = open(config_file, O_RDONLY);
1191 if (errno != ENOENT || OPT_GIVEN(DSS, CONFIG_FILE)) {
1192 ret = -ERRNO_TO_DSS_ERROR(errno);
1193 DSS_ERROR_LOG(("config file %s can not be opened\n",
1197 /* no config file -- nothing to do */
1202 ret = fstat(fd, &statbuf);
1204 ret = -ERRNO_TO_DSS_ERROR(errno);
1205 DSS_ERROR_LOG(("failed to stat config file %s\n", config_file));
1208 sz = statbuf.st_size;
1209 if (sz == 0) { /* config file is empty -- nothing to do */
1213 map = mmap(NULL, sz, PROT_READ, MAP_PRIVATE, fd, 0);
1214 if (map == MAP_FAILED) {
1215 ret = -ERRNO_TO_DSS_ERROR(errno);
1216 DSS_ERROR_LOG(("failed to mmap config file %s\n",
1220 if (cmd == CMD_PTR(DSS))
1223 subcmd_name = lls_command_name(cmd);
1224 ret = lls_convert_config(map, sz, subcmd_name, &cf_argv, &errctx);
1227 DSS_ERROR_LOG(("failed to convert config file %s\n",
1229 ret = lopsub_error(ret, &errctx);
1233 ret = lls_parse(cf_argc, cf_argv, cmd, &cf_lpr, &errctx);
1234 lls_free_argv(cf_argv);
1236 ret = lopsub_error(ret, &errctx);
1239 clpr = cmd == CMD_PTR(DSS)? cmdline_lpr : cmdline_sublpr;
1240 if (sighup) /* config file overrides command line */
1241 ret = lls_merge(cf_lpr, clpr, cmd, &merged_lpr, &errctx);
1242 else /* command line options overrride config file options */
1243 ret = lls_merge(clpr, cf_lpr, cmd, &merged_lpr, &errctx);
1244 lls_free_parse_result(cf_lpr, cmd);
1246 ret = lopsub_error(ret, &errctx);
1252 DSS_DEBUG_LOG(("loglevel: %d\n", OPT_UINT32_VAL(DSS, LOGLEVEL)));
1253 if (cmd != CMD_PTR(DSS)) {
1255 if (sublpr != cmdline_sublpr)
1256 lls_free_parse_result(sublpr, cmd);
1257 sublpr = merged_lpr;
1259 sublpr = cmdline_sublpr;
1262 if (lpr != cmdline_lpr)
1263 lls_free_parse_result(lpr, cmd);
1275 static int handle_sighup(void)
1279 DSS_NOTICE_LOG(("SIGHUP, re-reading config\n"));
1280 dump_dss_config("old");
1281 ret = parse_config_file(true /* SIGHUP */, CMD_PTR(DSS));
1284 ret = parse_config_file(true /* SIGHUP */, CMD_PTR(RUN));
1287 ret = check_config();
1292 if (OPT_GIVEN(RUN, DAEMON) || daemonized) {
1293 logfile = open_log(OPT_STRING_VAL(RUN, LOGFILE));
1294 log_welcome(OPT_UINT32_VAL(DSS, LOGLEVEL));
1297 dump_dss_config("reloaded");
1298 invalidate_next_snapshot_time();
1302 static void kill_children(void)
1304 restart_create_process();
1305 dss_kill(create_pid, SIGTERM, NULL);
1306 dss_kill(remove_pid, SIGTERM, NULL);
1309 static int handle_signal(void)
1311 int sig, ret = next_signal();
1321 ret = handle_sighup();
1324 ret = handle_sigchld();
1329 DSS_ERROR_LOG(("%s\n", dss_strerror(-ret)));
1334 * We can not use rsync locally if the local user is different from the remote
1335 * user or if the src dir is not on the local host (or both).
1337 static int use_rsync_locally(char *logname)
1339 const char *h = OPT_STRING_VAL(DSS, REMOTE_HOST);
1341 if (strcmp(h, "localhost") && strcmp(h, "127.0.0.1"))
1343 if (OPT_GIVEN(DSS, REMOTE_USER) &&
1344 strcmp(OPT_STRING_VAL(DSS, REMOTE_USER), logname))
1349 static int rename_resume_snap(int64_t creation_time)
1351 struct snapshot_list sl;
1352 struct snapshot *s = NULL;
1353 char *new_name = incomplete_name(creation_time);
1357 sl.num_snapshots = 0;
1360 dss_get_snapshot_list(&sl);
1362 * Snapshot recycling: We first look at the newest snapshot. If this
1363 * snapshot happens to be incomplete, the last rsync process was
1364 * aborted and we reuse this one. Otherwise we look at snapshots which
1365 * could be removed (outdated and redundant snapshots) as candidates
1366 * for recycling. If no outdated/redundant snapshot exists, we check if
1367 * there is an orphaned snapshot, which likely is useless anyway.
1369 * Only if no existing snapshot is suitable for recycling, we bite the
1370 * bullet and create a new one.
1372 s = get_newest_snapshot(&sl);
1373 if (!s) /* no snapshots at all */
1375 /* re-use last snapshot if it is incomplete */
1377 if ((s->flags & SS_COMPLETE) == 0)
1380 s = find_outdated_snapshot(&sl);
1384 s = find_redundant_snapshot(&sl);
1388 s = find_orphaned_snapshot(&sl);
1391 DSS_NOTICE_LOG(("recycling %s snapshot %s\n", why, s->name));
1392 ret = dss_rename(s->name, new_name);
1395 DSS_NOTICE_LOG(("creating %s\n", new_name));
1397 free_snapshot_list(&sl);
1401 static void create_rsync_argv(char ***argv, int64_t *num)
1405 struct snapshot_list sl;
1408 dss_get_snapshot_list(&sl);
1409 assert(!name_of_reference_snapshot);
1410 name_of_reference_snapshot = name_of_newest_complete_snapshot(&sl);
1411 free_snapshot_list(&sl);
1414 * We specify up to 6 arguments, one argument per given rsync option
1415 * and one argument per given source dir. We also need space for the
1416 * terminating NULL pointer.
1418 N = OPT_GIVEN(DSS, RSYNC_OPTION) + OPT_GIVEN(DSS, SOURCE_DIR);
1419 *argv = dss_malloc((7 + N) * sizeof(char *));
1420 (*argv)[i++] = dss_strdup("rsync");
1421 (*argv)[i++] = dss_strdup("-a");
1422 (*argv)[i++] = dss_strdup("--delete");
1424 srandom((unsigned)time(NULL)); /* no need to be fancy here */
1427 if (1000 * (random() / (RAND_MAX + 1.0)) < OPT_UINT32_VAL(DSS, CHECKSUM)) {
1428 DSS_NOTICE_LOG(("adding --checksum to rsync options\n"));
1429 (*argv)[i++] = dss_strdup("--checksum");
1431 for (j = 0; j < OPT_GIVEN(DSS, RSYNC_OPTION); j++)
1432 (*argv)[i++] = dss_strdup(lls_string_val(j,
1433 OPT_RESULT(DSS, RSYNC_OPTION)));
1434 if (name_of_reference_snapshot) {
1435 DSS_INFO_LOG(("using %s as reference\n", name_of_reference_snapshot));
1436 (*argv)[i++] = make_message("--link-dest=../%s",
1437 name_of_reference_snapshot);
1439 DSS_INFO_LOG(("no suitable reference snapshot found\n"));
1440 logname = dss_logname();
1441 if (use_rsync_locally(logname)) {
1442 for (j = 0; j < OPT_GIVEN(DSS, SOURCE_DIR); j++)
1443 (*argv)[i++] = dss_strdup(lls_string_val(j,
1444 OPT_RESULT(DSS, SOURCE_DIR)));
1447 * dss-1.0 and earlier did not support multiple source
1448 * directories. These versions appended a slash to the end of
1449 * the source directory to make sure that only the contents of
1450 * the single source directory, but not the directory itself,
1451 * are copied to the destination. For multiple source
1452 * directories, however, this is not a good idea because the
1453 * source directories may well contain identical file names,
1454 * which would then be copied to the same location on the
1455 * destination, overwriting each other. Moreover, we want the
1456 * directory on the destination match the source. To preserve
1457 * the old behaviour, we thus have to special-case N=1.
1459 for (j = 0; j < OPT_GIVEN(DSS, SOURCE_DIR); j++) {
1460 (*argv)[i++] = make_message("%s@%s:%s%s",
1461 OPT_GIVEN(DSS, REMOTE_USER)?
1462 OPT_STRING_VAL(DSS, REMOTE_USER) : logname,
1463 OPT_STRING_VAL(DSS, REMOTE_HOST),
1464 lls_string_val(j, OPT_RESULT(DSS, SOURCE_DIR)),
1465 OPT_GIVEN(DSS, SOURCE_DIR) == 1? "/" : ""
1470 *num = get_current_time();
1471 (*argv)[i++] = incomplete_name(*num);
1472 (*argv)[i++] = NULL;
1473 for (j = 0; j < i; j++)
1474 DSS_DEBUG_LOG(("argv[%d] = %s\n", j, (*argv)[j]));
1477 static void free_rsync_argv(char **argv)
1483 for (i = 0; argv[i]; i++)
1488 static int create_snapshot(char **argv)
1493 ret = rename_resume_snap(current_snapshot_creation_time);
1496 dss_exec(&create_pid, argv[0], argv);
1497 snapshot_creation_status = HS_RUNNING;
1501 static int select_loop(void)
1504 /* check every 60 seconds for free disk space */
1506 char **rsync_argv = NULL;
1510 struct timeval *tvp;
1513 tvp = NULL; /* sleep until rm hook/process dies */
1514 else { /* sleep one minute */
1520 FD_SET(signal_pipe, &rfds);
1521 ret = dss_select(signal_pipe + 1, &rfds, NULL, tvp);
1524 if (FD_ISSET(signal_pipe, &rfds)) {
1525 ret = handle_signal();
1531 if (snapshot_removal_status == HS_PRE_SUCCESS) {
1537 if (snapshot_removal_status == HS_SUCCESS) {
1541 ret = try_to_free_disk_space();
1544 if (snapshot_removal_status != HS_READY) {
1545 stop_create_process();
1548 restart_create_process();
1549 switch (snapshot_creation_status) {
1551 if (!next_snapshot_is_due())
1555 case HS_PRE_RUNNING:
1557 case HS_POST_RUNNING:
1559 case HS_PRE_SUCCESS:
1560 if (!name_of_reference_snapshot) {
1561 free_rsync_argv(rsync_argv);
1562 create_rsync_argv(&rsync_argv, ¤t_snapshot_creation_time);
1564 ret = create_snapshot(rsync_argv);
1568 case HS_NEEDS_RESTART:
1569 if (!next_snapshot_is_due())
1571 ret = create_snapshot(rsync_argv);
1584 static void exit_hook(int exit_code)
1587 char **argv, *tmp = dss_strdup(OPT_STRING_VAL(DSS, EXIT_HOOK));
1588 unsigned n = split_args(tmp, &argv);
1591 argv = dss_realloc(argv, (n + 1) * sizeof(char *));
1592 argv[n - 1] = dss_strdup(dss_strerror(-exit_code));
1594 dss_exec(&pid, argv[0], argv);
1600 static void lock_dss_or_die(void)
1602 int ret = lock_dss(config_file);
1605 DSS_EMERG_LOG(("failed to lock: %s\n", dss_strerror(-ret)));
1610 static int com_run(void)
1615 if (OPT_GIVEN(DSS, DRY_RUN)) {
1616 DSS_ERROR_LOG(("dry run not supported by this command\n"));
1619 ret = get_dss_pid(config_file, &pid);
1621 DSS_ERROR_LOG(("pid %d\n", (int)pid));
1622 return -E_ALREADY_RUNNING;
1625 * Order is important here: Since daemon_init() forks, it would drop
1626 * the lock if it had been acquired already. Changing the cwd before
1627 * grabbing the lock causes stat(2) to fail in case a relative config
1628 * file path was given, which results in a different key ID for
1629 * locking. Therefore we must first daemonize, then lock, then change
1632 if (OPT_GIVEN(RUN, DAEMON)) {
1635 logfile = open_log(OPT_STRING_VAL(RUN, LOGFILE));
1638 ret = change_to_dest_dir();
1641 dump_dss_config("startup");
1642 ret = install_sighandler(SIGHUP);
1646 ret = write(fd, "\0", 1);
1648 DSS_ERROR_LOG(("write to daemon pipe returned %d\n",
1651 return -ERRNO_TO_DSS_ERROR(errno);
1655 ret = select_loop();
1656 if (ret >= 0) /* impossible */
1660 while (wait(NULL) >= 0 || errno != ECHILD)
1661 ; /* still have children to wait for */
1664 EXPORT_CMD_HANDLER(run);
1666 static int com_prune(void)
1669 struct snapshot_list sl;
1670 struct snapshot *victim;
1671 struct disk_space ds;
1676 ret = change_to_dest_dir();
1679 switch (OPT_UINT32_VAL(PRUNE, DISK_SPACE)) {
1680 case FDS_LOW: try_hard = true; break;
1681 case FDS_HIGH: try_hard = false; break;
1683 ret = get_disk_space(".", &ds);
1686 log_disk_space(&ds);
1687 try_hard = disk_space_low(&ds);
1689 dss_get_snapshot_list(&sl);
1690 victim = find_removable_snapshot(&sl, try_hard, &why);
1692 dss_msg("nothing to prune\n");
1696 if (OPT_GIVEN(DSS, DRY_RUN)) {
1697 dss_msg("picking %s snapshot %s (interval = %i)\n",
1698 why, victim->name, victim->interval);
1702 pre_remove_hook(victim, why);
1703 if (snapshot_removal_status == HS_PRE_RUNNING) {
1704 ret = wait_for_remove_process();
1707 ret = -E_HOOK_FAILED;
1708 if (snapshot_removal_status != HS_PRE_SUCCESS)
1714 ret = wait_for_remove_process();
1717 assert(snapshot_removal_status == HS_SUCCESS);
1719 assert(snapshot_removal_status == HS_POST_RUNNING);
1720 ret = wait_for_remove_process();
1724 free_snapshot_list(&sl);
1727 EXPORT_CMD_HANDLER(prune);
1729 static int com_create(void)
1735 ret = change_to_dest_dir();
1738 if (OPT_GIVEN(DSS, DRY_RUN)) {
1741 create_rsync_argv(&rsync_argv, ¤t_snapshot_creation_time);
1742 for (i = 0; rsync_argv[i]; i++) {
1744 msg = make_message("%s%s%s", tmp? tmp : "",
1745 tmp? " " : "", rsync_argv[i]);
1748 free_rsync_argv(rsync_argv);
1749 dss_msg("%s\n", msg);
1755 ret = wait_for_process(create_pid, &status);
1758 ret = handle_pre_create_hook_exit(status);
1759 if (ret <= 0) /* error, or pre-create failed */
1762 create_rsync_argv(&rsync_argv, ¤t_snapshot_creation_time);
1763 ret = create_snapshot(rsync_argv);
1766 ret = wait_for_process(create_pid, &status);
1769 ret = handle_rsync_exit(status);
1774 ret = wait_for_process(create_pid, &status);
1776 free_rsync_argv(rsync_argv);
1779 EXPORT_CMD_HANDLER(create);
1781 static int com_ls(void)
1784 struct snapshot_list sl;
1786 int64_t now = get_current_time();
1788 ret = change_to_dest_dir();
1791 dss_get_snapshot_list(&sl);
1792 FOR_EACH_SNAPSHOT(s, i, &sl) {
1794 if (s->flags & SS_COMPLETE)
1795 d = (s->completion_time - s->creation_time) / 60;
1797 d = (now - s->creation_time) / 60;
1798 dss_msg("%u\t%s\t%3" PRId64 ":%02" PRId64 "\n", s->interval,
1799 s->name, d / 60, d % 60);
1801 free_snapshot_list(&sl);
1804 EXPORT_CMD_HANDLER(ls);
1806 static int com_configtest(void)
1808 printf("Syntax Ok\n");
1811 EXPORT_CMD_HANDLER(configtest);
1813 static int setup_signal_handling(void)
1817 DSS_INFO_LOG(("setting up signal handlers\n"));
1818 signal_pipe = signal_init(); /* always successful */
1819 ret = install_sighandler(SIGINT);
1822 ret = install_sighandler(SIGTERM);
1825 return install_sighandler(SIGCHLD);
1828 static void handle_version_and_help(void)
1832 if (OPT_GIVEN(DSS, DETAILED_HELP))
1833 txt = lls_long_help(CMD_PTR(DSS));
1834 else if (OPT_GIVEN(DSS, HELP))
1835 txt = lls_short_help(CMD_PTR(DSS));
1836 else if (OPT_GIVEN(DSS, VERSION))
1837 txt = make_message("%s\n", VERSION_STRING);
1845 static void show_subcommand_summary(void)
1847 const struct lls_command *cmd;
1850 printf("Available subcommands:\n");
1851 for (i = 1; (cmd = lls_cmd(i, dss_suite)); i++) {
1852 const char *name = lls_command_name(cmd);
1853 const char *purpose = lls_purpose(cmd);
1854 printf("%-11s%s\n", name, purpose);
1859 int main(int argc, char **argv)
1862 char *errctx = NULL;
1863 unsigned num_inputs;
1864 const struct dss_user_data *ud;
1866 ret = lls_parse(argc, argv, CMD_PTR(DSS), &cmdline_lpr, &errctx);
1868 ret = lopsub_error(ret, &errctx);
1872 set_config_file_name();
1873 ret = parse_config_file(false /* no SIGHUP */, CMD_PTR(DSS));
1876 handle_version_and_help();
1877 num_inputs = lls_num_inputs(lpr);
1878 if (num_inputs == 0)
1879 show_subcommand_summary();
1880 ret = lls_lookup_subcmd(argv[argc - num_inputs], dss_suite, &errctx);
1882 ret = lopsub_error(ret, &errctx);
1885 subcmd = lls_cmd(ret, dss_suite);
1886 ret = lls_parse(num_inputs, argv + argc - num_inputs, subcmd,
1887 &cmdline_sublpr, &errctx);
1889 ret = lopsub_error(ret, &errctx);
1892 sublpr = cmdline_sublpr;
1893 ret = parse_config_file(false /* no SIGHUP */, subcmd);
1896 ret = check_config();
1899 ret = setup_signal_handling();
1902 ud = lls_user_data(subcmd);
1903 ret = ud->handler();
1908 DSS_ERROR_LOG(("%s\n", errctx));
1909 DSS_EMERG_LOG(("%s\n", dss_strerror(-ret)));
1912 lls_free_parse_result(lpr, CMD_PTR(DSS));
1913 if (lpr != cmdline_lpr)
1914 lls_free_parse_result(cmdline_lpr, CMD_PTR(DSS));
1915 lls_free_parse_result(sublpr, subcmd);
1916 if (sublpr != cmdline_sublpr)
1917 lls_free_parse_result(cmdline_sublpr, subcmd);
1919 exit(ret >= 0? EXIT_SUCCESS : EXIT_FAILURE);