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 /* derived from config file path */
72 /** The read end of the signal pipe */
73 static int signal_pipe;
74 /** Process id of current pre-create-hook/rsync/post-create-hook process. */
75 static pid_t create_pid;
76 /** Whether the pre-create-hook/rsync/post-create-hook is currently stopped. */
77 static int create_process_stopped;
78 /** How many times in a row the rsync command failed. */
79 static int num_consecutive_rsync_errors;
80 /** Process id of current pre-remove/rm/post-remove process. */
81 static pid_t remove_pid;
82 /** When the next snapshot is due. */
83 static int64_t next_snapshot_time;
84 /** When to try to remove something. */
85 static struct timeval next_removal_check;
86 /** Creation time of the snapshot currently being created. */
87 static int64_t current_snapshot_creation_time;
88 /* Set by the pre-rm hook, cleared by handle_remove_exit(). */
89 struct snapshot *snapshot_currently_being_removed;
90 /** Needed by the post-create hook. */
91 static char *path_to_last_complete_snapshot;
92 static char *name_of_reference_snapshot;
93 /** \sa \ref snap.h for details. */
94 enum hook_status snapshot_creation_status;
95 /** \sa \ref snap.h for details. */
96 enum hook_status snapshot_removal_status;
100 static const char *hook_status_description[] = {HOOK_STATUS_ARRAY};
102 /* may be called with ds == NULL. */
103 static int disk_space_low(struct disk_space *ds)
105 struct disk_space ds_struct;
109 int ret = get_disk_space(".", &ds_struct);
114 val = OPT_UINT32_VAL(DSS, MIN_FREE_MB);
116 if (ds->free_mb < val)
118 val = OPT_UINT32_VAL(DSS, MIN_FREE_PERCENT);
120 if (ds->percent_free < val)
122 val = OPT_UINT32_VAL(DSS, MIN_FREE_PERCENT_INODES);
124 if (ds->percent_free_inodes < val)
129 static void dump_dss_config(const char *msg)
131 const char dash[] = "-----------------------------";
134 FILE *log = logfile? logfile : stderr;
135 struct disk_space ds;
136 int64_t now = get_current_time();
138 if (OPT_UINT32_VAL(DSS, LOGLEVEL) > INFO)
141 fprintf(log, "%s <%s config> %s\n", dash, msg, dash);
142 fprintf(log, "\n*** disk space ***\n\n");
143 ret = get_disk_space(".", &ds);
145 DSS_INFO_LOG(("disk space low: %s\n", disk_space_low(&ds)?
149 DSS_ERROR_LOG(("can not get free disk space: %s\n",
150 dss_strerror(-ret)));
152 /* we continue on errors from get_disk_space */
154 fprintf(log, "\n*** non-default options ***\n\n");
155 lopsub_dump = lls_dump_parse_result(lpr, CMD_PTR(DSS), true);
156 fprintf(log, "%s", lopsub_dump);
158 fprintf(log, "\n*** non-default options for \"run\" ***\n\n");
159 lopsub_dump = lls_dump_parse_result(lpr, CMD_PTR(RUN), true);
160 fprintf(log, "%s", lopsub_dump);
162 fprintf(log, "\n*** internal state ***\n\n");
166 "snapshot_currently_being_removed: %s\n"
167 "path_to_last_complete_snapshot: %s\n"
168 "reference_snapshot: %s\n"
169 "snapshot_creation_status: %s\n"
170 "snapshot_removal_status: %s\n"
171 "num_consecutive_rsync_errors: %d\n"
174 logfile? OPT_STRING_VAL(RUN, LOGFILE) : "stderr",
175 snapshot_currently_being_removed?
176 snapshot_currently_being_removed->name : "(none)",
177 path_to_last_complete_snapshot?
178 path_to_last_complete_snapshot : "(none)",
179 name_of_reference_snapshot?
180 name_of_reference_snapshot : "(none)",
181 hook_status_description[snapshot_creation_status],
182 hook_status_description[snapshot_removal_status],
183 num_consecutive_rsync_errors
187 "create_pid: %" PRId32 "\n"
188 "create process is %sstopped\n"
191 create_process_stopped? "" : "not "
194 fprintf(log, "remove_pid: %" PRId32 "\n", remove_pid);
195 if (next_snapshot_time != 0)
196 fprintf(log, "next snapshot due in %" PRId64 " seconds\n",
197 next_snapshot_time - now);
198 if (current_snapshot_creation_time != 0)
199 fprintf(log, "current_snapshot_creation_time: %"
200 PRId64 " (%" PRId64 " seconds ago)\n",
201 current_snapshot_creation_time,
202 now - current_snapshot_creation_time
204 if (next_removal_check.tv_sec != 0) {
205 fprintf(log, "next removal check: %llu (%llu seconds ago)\n",
206 (long long unsigned)next_removal_check.tv_sec,
207 now - (long long unsigned)next_removal_check.tv_sec
211 fprintf(log, "%s </%s config> %s\n", dash, msg, dash);
214 static int loglevel = -1;
215 static const char *location_file = NULL;
216 static int location_line = -1;
217 static const char *location_func = NULL;
219 void dss_log_set_params(int ll, const char *file, int line, const char *func)
222 location_file = file;
223 location_line = line;
224 location_func = func;
228 * The log function of dss.
230 * \param ll Loglevel.
231 * \param fml Usual format string.
233 * All DSS_XXX_LOG() macros use this function.
235 __printf_1_2 void dss_log(const char* fmt,...)
242 int lpr_ll = lpr? OPT_UINT32_VAL(DSS, LOGLEVEL) : WARNING;
244 if (loglevel < lpr_ll)
246 outfd = logfile? logfile : stderr;
247 if (subcmd == CMD_PTR(RUN)) {
250 strftime(str, sizeof(str), "%b %d %H:%M:%S", tm);
251 fprintf(outfd, "%s ", str);
253 fprintf(outfd, "%i: ", loglevel);
255 if (subcmd == CMD_PTR(RUN))
256 #ifdef DSS_NO_FUNC_NAMES
257 fprintf(outfd, "%s:%d: ", location_file, location_line);
259 fprintf(outfd, "%s: ", location_func);
262 vfprintf(outfd, fmt, argp);
267 * Print a message either to stdout or to the log file.
269 static __printf_1_2 void dss_msg(const char* fmt,...)
271 FILE *outfd = logfile? logfile : stdout;
274 vfprintf(outfd, fmt, argp);
278 static void set_config_file_name(void)
281 if (OPT_GIVEN(DSS, CONFIG_FILE)) {
282 const char *arg = OPT_STRING_VAL(DSS, CONFIG_FILE);
283 config_file = realpath(arg, NULL);
285 DSS_EMERG_LOG(("could not resolve path %s: %s\n", arg,
290 char *home = get_homedir();
291 char *arg = make_message("%s/.dssrc", home);
293 config_file = realpath(arg, NULL);
299 DSS_DEBUG_LOG(("config file: %s\n", config_file));
300 ipc_key = super_fast_hash((uint8_t *)config_file,
301 strlen(config_file), 0) >> 1;
304 static int send_signal(int sig, bool wait)
307 int ret = get_dss_pid(ipc_key, &pid);
313 if (OPT_GIVEN(DSS, DRY_RUN)) {
314 dss_msg("%d\n", (int)pid);
317 DSS_NOTICE_LOG(("sending signal %d to pid %d\n", sig, (int)pid));
318 ret = kill(pid, sig);
320 return -ERRNO_TO_DSS_ERROR(errno);
324 ts.tv_sec = ms / 1000;
325 ts.tv_nsec = (ms % 1000) * 1000 * 1000;
326 ret = nanosleep(&ts, NULL);
328 return -ERRNO_TO_DSS_ERROR(errno);
332 return -ERRNO_TO_DSS_ERROR(errno);
337 return -E_KILL_TIMEOUT;
341 const char * const name;
346 * The table below was taken 2016 from proc/sig.c of procps-3.2.8. Copyright
347 * 1998-2003 by Albert Cahalan, GPLv2.
349 static const struct signal_info signal_table[] = {
350 {"ABRT", SIGABRT}, /* IOT */
353 {"CHLD", SIGCHLD}, /* CLD */
362 {"POLL", SIGPOLL}, /* IO */
371 {"STKFLT", SIGSTKFLT},
374 {"SYS", SIGSYS}, /* UNUSED */
383 {"VTALRM", SIGVTALRM},
389 #define SIGNAL_TABLE_SIZE (sizeof(signal_table) / sizeof(signal_table[0]))
394 static int com_kill(void)
396 bool w_given = OPT_GIVEN(KILL, WAIT);
397 const char *arg = OPT_STRING_VAL(KILL, SIGNAL);
400 if (*arg >= '0' && *arg <= '9') {
402 ret = dss_atoi64(arg, &val);
405 if (val < 0 || val > SIGRTMAX)
406 return -ERRNO_TO_DSS_ERROR(EINVAL);
407 return send_signal(val, w_given);
409 if (strncasecmp(arg, "sig", 3) == 0)
411 if (strcasecmp(arg, "CLD") == 0)
412 return send_signal(SIGCHLD, w_given);
413 if (strcasecmp(arg, "IOT") == 0)
414 return send_signal(SIGABRT, w_given);
415 for (i = 0; i < SIGNAL_TABLE_SIZE; i++)
416 if (strcasecmp(arg, signal_table[i].name) == 0)
417 return send_signal(signal_table[i].num, w_given);
418 DSS_ERROR_LOG(("invalid sigspec: %s\n", arg));
419 return -ERRNO_TO_DSS_ERROR(EINVAL);
421 EXPORT_CMD_HANDLER(kill);
423 static void dss_get_snapshot_list(struct snapshot_list *sl)
425 get_snapshot_list(sl, OPT_UINT32_VAL(DSS, UNIT_INTERVAL),
426 OPT_UINT32_VAL(DSS, NUM_INTERVALS));
429 static int64_t compute_next_snapshot_time(void)
431 int64_t x = 0, now = get_current_time(), unit_interval
432 = 24 * 3600 * OPT_UINT32_VAL(DSS, UNIT_INTERVAL), ret,
433 last_completion_time;
434 unsigned wanted = desired_number_of_snapshots(0,
435 OPT_UINT32_VAL(DSS, NUM_INTERVALS)),
438 struct snapshot *s = NULL;
439 struct snapshot_list sl;
441 dss_get_snapshot_list(&sl);
442 FOR_EACH_SNAPSHOT(s, i, &sl) {
443 if (!(s->flags & SS_COMPLETE))
446 x += s->completion_time - s->creation_time;
447 last_completion_time = s->completion_time;
452 if (num_complete == 0)
454 x /= num_complete; /* avg time to create one snapshot */
455 if (unit_interval < x * wanted) /* oops, no sleep at all */
457 ret = last_completion_time + unit_interval / wanted - x;
459 free_snapshot_list(&sl);
463 static inline void invalidate_next_snapshot_time(void)
465 next_snapshot_time = 0;
468 static inline int next_snapshot_time_is_valid(void)
470 return next_snapshot_time != 0;
473 static int next_snapshot_is_due(void)
475 int64_t now = get_current_time();
477 if (!next_snapshot_time_is_valid())
478 next_snapshot_time = compute_next_snapshot_time();
479 if (next_snapshot_time <= now) {
480 DSS_DEBUG_LOG(("next snapshot: now\n"));
483 DSS_DEBUG_LOG(("next snapshot due in %" PRId64 " seconds\n",
484 next_snapshot_time - now));
488 static void pre_create_hook(void)
490 assert(snapshot_creation_status == HS_READY);
491 /* make sure that the next snapshot time will be recomputed */
492 invalidate_next_snapshot_time();
493 DSS_DEBUG_LOG(("executing %s\n", OPT_STRING_VAL(DSS, PRE_CREATE_HOOK)));
494 dss_exec_cmdline_pid(&create_pid, OPT_STRING_VAL(DSS, PRE_CREATE_HOOK));
495 snapshot_creation_status = HS_PRE_RUNNING;
498 static void pre_remove_hook(struct snapshot *s, const char *why)
504 DSS_DEBUG_LOG(("%s snapshot %s\n", why, s->name));
505 assert(snapshot_removal_status == HS_READY);
506 assert(remove_pid == 0);
507 assert(!snapshot_currently_being_removed);
509 snapshot_currently_being_removed = dss_malloc(sizeof(struct snapshot));
510 *snapshot_currently_being_removed = *s;
511 snapshot_currently_being_removed->name = dss_strdup(s->name);
513 cmd = make_message("%s %s/%s", OPT_STRING_VAL(DSS, PRE_REMOVE_HOOK),
514 OPT_STRING_VAL(DSS, DEST_DIR), s->name);
515 DSS_DEBUG_LOG(("executing %s\n", cmd));
516 dss_exec_cmdline_pid(&remove_pid, cmd);
518 snapshot_removal_status = HS_PRE_RUNNING;
521 static int exec_rm(void)
523 struct snapshot *s = snapshot_currently_being_removed;
524 char *new_name = being_deleted_name(s);
533 assert(snapshot_removal_status == HS_PRE_SUCCESS);
534 assert(remove_pid == 0);
536 DSS_NOTICE_LOG(("removing %s (interval = %i)\n", s->name, s->interval));
537 ret = dss_rename(s->name, new_name);
540 dss_exec(&remove_pid, argv[0], argv);
541 snapshot_removal_status = HS_RUNNING;
547 static int snapshot_is_being_created(struct snapshot *s)
549 return s->creation_time == current_snapshot_creation_time;
552 static struct snapshot *find_orphaned_snapshot(struct snapshot_list *sl)
557 DSS_DEBUG_LOG(("looking for old incomplete snapshots\n"));
558 FOR_EACH_SNAPSHOT(s, i, sl) {
559 if (snapshot_is_being_created(s))
562 * We know that no rm is currently running, so if s is marked
563 * as being deleted, a previously started rm must have failed.
565 if (s->flags & SS_BEING_DELETED)
568 if (s->flags & SS_COMPLETE) /* good snapshot */
571 * This snapshot is incomplete and it is not the snapshot
572 * currently being created. However, we must not remove it if
573 * rsync is about to be restarted. As only the newest snapshot
574 * can be restarted, this snapshot is orphaned if it is not the
575 * newest snapshot or if we are not about to restart rsync.
577 if (get_newest_snapshot(sl) != s)
579 if (snapshot_creation_status != HS_NEEDS_RESTART)
582 /* no orphaned snapshots */
586 static int is_reference_snapshot(struct snapshot *s)
588 if (!name_of_reference_snapshot)
590 return strcmp(s->name, name_of_reference_snapshot)? 0 : 1;
593 static struct snapshot *find_redundant_snapshot(struct snapshot_list *sl)
597 unsigned missing = 0;
598 uint32_t N = OPT_UINT32_VAL(DSS, NUM_INTERVALS);
600 DSS_DEBUG_LOG(("looking for intervals containing too many snapshots\n"));
601 for (interval = N - 1; interval >= 0; interval--) {
602 unsigned keep = desired_number_of_snapshots(interval, N);
603 unsigned num = sl->interval_count[interval];
604 struct snapshot *victim = NULL, *prev = NULL;
605 int64_t score = LONG_MAX;
608 missing += keep - num;
609 if (keep + missing >= num)
611 /* redundant snapshot in this interval, pick snapshot with lowest score */
612 FOR_EACH_SNAPSHOT(s, i, sl) {
615 if (snapshot_is_being_created(s))
617 if (is_reference_snapshot(s))
619 if (s->interval > interval) {
623 if (s->interval < interval)
631 /* check if s is a better victim */
632 this_score = s->creation_time - prev->creation_time;
633 assert(this_score >= 0);
634 if (this_score < score) {
646 static struct snapshot *find_outdated_snapshot(struct snapshot_list *sl)
651 DSS_DEBUG_LOG(("looking for snapshots belonging to intervals >= %d\n",
652 OPT_UINT32_VAL(DSS, NUM_INTERVALS)));
653 FOR_EACH_SNAPSHOT(s, i, sl) {
654 if (snapshot_is_being_created(s))
656 if (is_reference_snapshot(s))
658 if (s->interval < OPT_UINT32_VAL(DSS, NUM_INTERVALS))
665 static struct snapshot *find_oldest_removable_snapshot(struct snapshot_list *sl)
668 struct snapshot *s, *ref = NULL;
670 DSS_DEBUG_LOG(("picking snapshot with earliest creation time\n"));
671 num_complete = num_complete_snapshots(sl);
672 if (num_complete <= OPT_UINT32_VAL(DSS, MIN_COMPLETE))
674 FOR_EACH_SNAPSHOT(s, i, sl) {
675 if (snapshot_is_being_created(s))
677 if (is_reference_snapshot(s)) { /* avoid this one */
684 DSS_WARNING_LOG(("removing reference snapshot %s\n", ref->name));
688 /* returns NULL <==> *reason is set to NULL */
689 static struct snapshot *find_removable_snapshot(struct snapshot_list *sl,
690 bool try_hard, char **reason)
692 struct snapshot *victim;
695 * Don't remove anything if there is free space and we have fewer
696 * snapshots than configured, plus one. This way there is always one
697 * snapshot that can be recycled.
699 if (!try_hard && sl->num_snapshots <=
700 1 << OPT_UINT32_VAL(DSS, NUM_INTERVALS))
702 victim = find_orphaned_snapshot(sl);
704 *reason = make_message("orphaned");
707 victim = find_outdated_snapshot(sl);
709 *reason = make_message("outdated");
712 if (!OPT_GIVEN(DSS, KEEP_REDUNDANT)) {
713 victim = find_redundant_snapshot(sl);
715 *reason = make_message("redundant");
721 DSS_WARNING_LOG(("nothing obvious to remove\n"));
722 victim = find_oldest_removable_snapshot(sl);
724 *reason = make_message("oldest");
732 static int rename_incomplete_snapshot(int64_t start)
739 * We don't want the dss_rename() below to fail with EEXIST because the
740 * last complete snapshot was created (and completed) in the same
741 * second as this one.
743 while ((now = get_current_time()) == start)
745 free(path_to_last_complete_snapshot);
746 ret = complete_name(start, now, &path_to_last_complete_snapshot);
749 old_name = incomplete_name(start);
750 ret = dss_rename(old_name, path_to_last_complete_snapshot);
752 DSS_NOTICE_LOG(("%s -> %s\n", old_name,
753 path_to_last_complete_snapshot));
758 static int try_to_free_disk_space(void)
761 struct snapshot_list sl;
762 struct snapshot *victim;
767 ret = disk_space_low(NULL);
770 low_disk_space = ret;
771 gettimeofday(&now, NULL);
772 if (tv_diff(&next_removal_check, &now, NULL) > 0)
774 if (!low_disk_space) {
775 if (snapshot_creation_status != HS_READY)
777 if (next_snapshot_is_due())
780 /* Idle or low disk space, look at existing snapshots. */
781 dss_get_snapshot_list(&sl);
782 victim = find_removable_snapshot(&sl, low_disk_space, &why);
784 pre_remove_hook(victim, why);
787 free_snapshot_list(&sl);
792 DSS_CRIT_LOG(("uhuhu: disk space low and nothing to remove\n"));
793 return -ERRNO_TO_DSS_ERROR(ENOSPC);
796 static void post_create_hook(void)
798 char *cmd = make_message("%s %s/%s",
799 OPT_STRING_VAL(DSS, POST_CREATE_HOOK),
800 OPT_STRING_VAL(DSS, DEST_DIR), path_to_last_complete_snapshot);
801 DSS_NOTICE_LOG(("executing %s\n", cmd));
802 dss_exec_cmdline_pid(&create_pid, cmd);
804 snapshot_creation_status = HS_POST_RUNNING;
807 static void post_remove_hook(void)
810 struct snapshot *s = snapshot_currently_being_removed;
814 cmd = make_message("%s %s/%s", OPT_STRING_VAL(DSS, POST_REMOVE_HOOK),
815 OPT_STRING_VAL(DSS, DEST_DIR), s->name);
816 DSS_NOTICE_LOG(("executing %s\n", cmd));
817 dss_exec_cmdline_pid(&remove_pid, cmd);
819 snapshot_removal_status = HS_POST_RUNNING;
822 static void dss_kill(pid_t pid, int sig, const char *msg)
824 const char *signame, *process_name;
829 case SIGTERM: signame = "TERM"; break;
830 case SIGSTOP: signame = "STOP"; break;
831 case SIGCONT: signame = "CONT"; break;
832 default: signame = "????";
835 if (pid == create_pid)
836 process_name = "create";
837 else if (pid == remove_pid)
838 process_name = "remove";
839 else process_name = "??????";
842 DSS_INFO_LOG(("%s\n", msg));
843 DSS_DEBUG_LOG(("sending signal %d (%s) to pid %d (%s process)\n",
844 sig, signame, (int)pid, process_name));
845 if (kill(pid, sig) >= 0)
847 DSS_INFO_LOG(("failed to send signal %d (%s) to pid %d (%s process)\n",
848 sig, signame, (int)pid, process_name));
851 static void stop_create_process(void)
853 if (create_process_stopped)
855 dss_kill(create_pid, SIGSTOP, "suspending create process");
856 create_process_stopped = 1;
859 static void restart_create_process(void)
861 if (!create_process_stopped)
863 dss_kill(create_pid, SIGCONT, "resuming create process");
864 create_process_stopped = 0;
868 * Print a log message about the exit status of a child.
870 static void log_termination_msg(pid_t pid, int status)
872 if (WIFEXITED(status))
873 DSS_INFO_LOG(("child %i exited. Exit status: %i\n", (int)pid,
874 WEXITSTATUS(status)));
875 else if (WIFSIGNALED(status))
876 DSS_NOTICE_LOG(("child %i was killed by signal %i\n", (int)pid,
879 DSS_WARNING_LOG(("child %i terminated abormally\n", (int)pid));
882 static int wait_for_process(pid_t pid, int *status)
886 DSS_DEBUG_LOG(("Waiting for process %d to terminate\n", (int)pid));
891 FD_SET(signal_pipe, &rfds);
892 ret = dss_select(signal_pipe + 1, &rfds, NULL, NULL);
898 if (ret == SIGCHLD) {
899 ret = waitpid(pid, status, 0);
902 if (errno != EINTR) { /* error */
903 ret = -ERRNO_TO_DSS_ERROR(errno);
907 /* SIGINT or SIGTERM */
908 dss_kill(pid, SIGTERM, "killing child process");
911 DSS_ERROR_LOG(("failed to wait for process %d\n", (int)pid));
913 log_termination_msg(pid, *status);
917 static void handle_pre_remove_exit(int status)
919 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
920 snapshot_removal_status = HS_READY;
921 gettimeofday(&next_removal_check, NULL);
922 next_removal_check.tv_sec += 60;
925 snapshot_removal_status = HS_PRE_SUCCESS;
928 static int handle_rm_exit(int status)
930 if (!WIFEXITED(status)) {
931 snapshot_removal_status = HS_READY;
932 return -E_INVOLUNTARY_EXIT;
934 if (WEXITSTATUS(status)) {
935 snapshot_removal_status = HS_READY;
936 return -E_BAD_EXIT_CODE;
938 snapshot_removal_status = HS_SUCCESS;
942 static void handle_post_remove_exit(void)
944 snapshot_removal_status = HS_READY;
947 static int handle_remove_exit(int status)
950 struct snapshot *s = snapshot_currently_being_removed;
953 switch (snapshot_removal_status) {
955 handle_pre_remove_exit(status);
959 ret = handle_rm_exit(status);
961 case HS_POST_RUNNING:
962 handle_post_remove_exit();
968 if (snapshot_removal_status == HS_READY) {
971 snapshot_currently_being_removed = NULL;
977 static int wait_for_remove_process(void)
983 snapshot_removal_status == HS_PRE_RUNNING ||
984 snapshot_removal_status == HS_RUNNING ||
985 snapshot_removal_status == HS_POST_RUNNING
987 ret = wait_for_process(remove_pid, &status);
990 return handle_remove_exit(status);
993 static int handle_rsync_exit(int status)
997 if (!WIFEXITED(status)) {
998 DSS_ERROR_LOG(("rsync process %d died involuntary\n", (int)create_pid));
999 ret = -E_INVOLUNTARY_EXIT;
1000 snapshot_creation_status = HS_READY;
1003 es = WEXITSTATUS(status);
1005 * Restart rsync on non-fatal errors:
1006 * 24: Partial transfer due to vanished source files
1008 if (es != 0 && es != 24) {
1009 DSS_WARNING_LOG(("rsync exit code %d, error count %d\n",
1010 es, ++num_consecutive_rsync_errors));
1011 if (!logfile) { /* called by com_run() */
1012 ret = -E_BAD_EXIT_CODE;
1015 if (num_consecutive_rsync_errors >
1016 OPT_UINT32_VAL(RUN, MAX_RSYNC_ERRORS)) {
1017 ret = -E_TOO_MANY_RSYNC_ERRORS;
1018 snapshot_creation_status = HS_READY;
1021 DSS_WARNING_LOG(("restarting rsync process\n"));
1022 snapshot_creation_status = HS_NEEDS_RESTART;
1023 next_snapshot_time = get_current_time() + 60;
1027 num_consecutive_rsync_errors = 0;
1028 ret = rename_incomplete_snapshot(current_snapshot_creation_time);
1031 snapshot_creation_status = HS_SUCCESS;
1032 free(name_of_reference_snapshot);
1033 name_of_reference_snapshot = NULL;
1035 create_process_stopped = 0;
1039 static int handle_pre_create_hook_exit(int status)
1042 static int warn_count;
1044 if (!WIFEXITED(status)) {
1045 snapshot_creation_status = HS_READY;
1046 ret = -E_INVOLUNTARY_EXIT;
1049 es = WEXITSTATUS(status);
1051 if (!warn_count--) {
1052 DSS_NOTICE_LOG(("pre_create_hook %s returned %d\n",
1053 OPT_STRING_VAL(DSS, PRE_CREATE_HOOK), es));
1054 DSS_NOTICE_LOG(("deferring snapshot creation...\n"));
1055 warn_count = 60; /* warn only once per hour */
1057 next_snapshot_time = get_current_time() + 60;
1058 snapshot_creation_status = HS_READY;
1063 snapshot_creation_status = HS_PRE_SUCCESS;
1069 static int handle_sigchld(void)
1072 int status, ret = reap_child(&pid, &status);
1077 if (pid == create_pid) {
1078 switch (snapshot_creation_status) {
1079 case HS_PRE_RUNNING:
1080 ret = handle_pre_create_hook_exit(status);
1083 ret = handle_rsync_exit(status);
1085 case HS_POST_RUNNING:
1086 snapshot_creation_status = HS_READY;
1090 DSS_EMERG_LOG(("BUG: create can't die in status %d\n",
1091 snapshot_creation_status));
1097 if (pid == remove_pid) {
1098 ret = handle_remove_exit(status);
1103 DSS_EMERG_LOG(("BUG: unknown process %d died\n", (int)pid));
1107 /* also checks if . is a mountpoint, if --mountpoint was given */
1108 static int change_to_dest_dir(void)
1111 const char *dd = OPT_STRING_VAL(DSS, DEST_DIR);
1112 struct stat dot, dotdot;
1114 DSS_INFO_LOG(("changing cwd to %s\n", dd));
1115 if (chdir(dd) < 0) {
1116 ret = -ERRNO_TO_DSS_ERROR(errno);
1117 DSS_ERROR_LOG(("could not change cwd to %s\n", dd));
1120 if (!OPT_GIVEN(DSS, MOUNTPOINT))
1122 if (stat(".", &dot) < 0) {
1123 ret = -ERRNO_TO_DSS_ERROR(errno);
1124 DSS_ERROR_LOG(("could not stat .\n"));
1127 if (stat("..", &dotdot) < 0) {
1128 ret = -ERRNO_TO_DSS_ERROR(errno);
1129 DSS_ERROR_LOG(("could not stat ..\n"));
1132 if (dot.st_dev == dotdot.st_dev && dot.st_ino != dotdot.st_ino) {
1133 DSS_ERROR_LOG(("mountpoint check failed for %s\n", dd));
1134 return -E_MOUNTPOINT;
1139 static int check_config(void)
1141 uint32_t unit_interval = OPT_UINT32_VAL(DSS, UNIT_INTERVAL);
1142 uint32_t num_intervals = OPT_UINT32_VAL(DSS, NUM_INTERVALS);
1144 if (unit_interval == 0) {
1145 DSS_ERROR_LOG(("bad unit interval: %i\n", unit_interval));
1146 return -E_INVALID_NUMBER;
1148 DSS_DEBUG_LOG(("unit interval: %i day(s)\n", unit_interval));
1150 if (num_intervals == 0 || num_intervals > 30) {
1151 DSS_ERROR_LOG(("bad number of intervals: %i\n", num_intervals));
1152 return -E_INVALID_NUMBER;
1154 if (subcmd == CMD_PTR(RUN) || subcmd == CMD_PTR(CREATE))
1155 if (!OPT_GIVEN(DSS, SOURCE_DIR)) {
1156 DSS_ERROR_LOG(("--source-dir required\n"));
1159 if (subcmd == CMD_PTR(RUN) || subcmd == CMD_PTR(CREATE)
1160 || subcmd == CMD_PTR(LS) || subcmd == CMD_PTR(PRUNE)) {
1161 if (!OPT_GIVEN(DSS, DEST_DIR)) {
1162 DSS_ERROR_LOG(("--dest-dir required\n"));
1166 DSS_DEBUG_LOG(("number of intervals: %i\n", num_intervals));
1170 static int lopsub_error(int lopsub_ret, char **errctx)
1172 const char *msg = lls_strerror(-lopsub_ret);
1174 DSS_ERROR_LOG(("%s: %s\n", *errctx, msg));
1176 DSS_ERROR_LOG(("%s\n", msg));
1182 static int parse_config_file(bool sighup, const struct lls_command *cmd)
1185 struct stat statbuf;
1189 char **cf_argv, *errctx = NULL;
1190 struct lls_parse_result *cf_lpr, *merged_lpr, *clpr;
1191 const char *subcmd_name;
1193 ret = open(config_file, O_RDONLY);
1195 if (errno != ENOENT || OPT_GIVEN(DSS, CONFIG_FILE)) {
1196 ret = -ERRNO_TO_DSS_ERROR(errno);
1197 DSS_ERROR_LOG(("config file %s can not be opened\n",
1201 /* no config file -- nothing to do */
1206 ret = fstat(fd, &statbuf);
1208 ret = -ERRNO_TO_DSS_ERROR(errno);
1209 DSS_ERROR_LOG(("failed to stat config file %s\n", config_file));
1212 sz = statbuf.st_size;
1213 if (sz == 0) { /* config file is empty -- nothing to do */
1217 map = mmap(NULL, sz, PROT_READ, MAP_PRIVATE, fd, 0);
1218 if (map == MAP_FAILED) {
1219 ret = -ERRNO_TO_DSS_ERROR(errno);
1220 DSS_ERROR_LOG(("failed to mmap config file %s\n",
1224 if (cmd == CMD_PTR(DSS))
1227 subcmd_name = lls_command_name(cmd);
1228 ret = lls_convert_config(map, sz, subcmd_name, &cf_argv, &errctx);
1231 DSS_ERROR_LOG(("failed to convert config file %s\n",
1233 ret = lopsub_error(ret, &errctx);
1237 ret = lls_parse(cf_argc, cf_argv, cmd, &cf_lpr, &errctx);
1238 lls_free_argv(cf_argv);
1240 ret = lopsub_error(ret, &errctx);
1243 clpr = cmd == CMD_PTR(DSS)? cmdline_lpr : cmdline_sublpr;
1244 if (sighup) /* config file overrides command line */
1245 ret = lls_merge(cf_lpr, clpr, cmd, &merged_lpr, &errctx);
1246 else /* command line options overrride config file options */
1247 ret = lls_merge(clpr, cf_lpr, cmd, &merged_lpr, &errctx);
1248 lls_free_parse_result(cf_lpr, cmd);
1250 ret = lopsub_error(ret, &errctx);
1256 DSS_DEBUG_LOG(("loglevel: %d\n", OPT_UINT32_VAL(DSS, LOGLEVEL)));
1257 if (cmd != CMD_PTR(DSS)) {
1259 if (sublpr != cmdline_sublpr)
1260 lls_free_parse_result(sublpr, cmd);
1261 sublpr = merged_lpr;
1263 sublpr = cmdline_sublpr;
1266 if (lpr != cmdline_lpr)
1267 lls_free_parse_result(lpr, cmd);
1279 static int handle_sighup(void)
1283 DSS_NOTICE_LOG(("SIGHUP, re-reading config\n"));
1284 dump_dss_config("old");
1285 ret = parse_config_file(true /* SIGHUP */, CMD_PTR(DSS));
1288 ret = parse_config_file(true /* SIGHUP */, CMD_PTR(RUN));
1291 ret = check_config();
1296 if (OPT_GIVEN(RUN, DAEMON) || daemonized) {
1297 logfile = open_log(OPT_STRING_VAL(RUN, LOGFILE));
1298 log_welcome(OPT_UINT32_VAL(DSS, LOGLEVEL));
1301 dump_dss_config("reloaded");
1302 invalidate_next_snapshot_time();
1306 static void kill_children(void)
1308 restart_create_process();
1309 dss_kill(create_pid, SIGTERM, NULL);
1310 dss_kill(remove_pid, SIGTERM, NULL);
1313 static int handle_signal(void)
1315 int sig, ret = next_signal();
1325 ret = handle_sighup();
1328 ret = handle_sigchld();
1333 DSS_ERROR_LOG(("%s\n", dss_strerror(-ret)));
1338 * We can not use rsync locally if the local user is different from the remote
1339 * user or if the src dir is not on the local host (or both).
1341 static int use_rsync_locally(char *logname)
1343 const char *h = OPT_STRING_VAL(DSS, REMOTE_HOST);
1345 if (strcmp(h, "localhost") && strcmp(h, "127.0.0.1"))
1347 if (OPT_GIVEN(DSS, REMOTE_USER) &&
1348 strcmp(OPT_STRING_VAL(DSS, REMOTE_USER), logname))
1353 static int rename_resume_snap(int64_t creation_time)
1355 struct snapshot_list sl;
1356 struct snapshot *s = NULL;
1357 char *new_name = incomplete_name(creation_time);
1361 sl.num_snapshots = 0;
1364 dss_get_snapshot_list(&sl);
1366 * Snapshot recycling: We first look at the newest snapshot. If this
1367 * snapshot happens to be incomplete, the last rsync process was
1368 * aborted and we reuse this one. Otherwise we look at snapshots which
1369 * could be removed (outdated and redundant snapshots) as candidates
1370 * for recycling. If no outdated/redundant snapshot exists, we check if
1371 * there is an orphaned snapshot, which likely is useless anyway.
1373 * Only if no existing snapshot is suitable for recycling, we bite the
1374 * bullet and create a new one.
1376 s = get_newest_snapshot(&sl);
1377 if (!s) /* no snapshots at all */
1379 /* re-use last snapshot if it is incomplete */
1381 if ((s->flags & SS_COMPLETE) == 0)
1384 s = find_outdated_snapshot(&sl);
1388 s = find_redundant_snapshot(&sl);
1392 s = find_orphaned_snapshot(&sl);
1395 DSS_NOTICE_LOG(("recycling %s snapshot %s\n", why, s->name));
1396 ret = dss_rename(s->name, new_name);
1399 DSS_NOTICE_LOG(("creating %s\n", new_name));
1401 free_snapshot_list(&sl);
1405 static void create_rsync_argv(char ***argv, int64_t *num)
1409 struct snapshot_list sl;
1412 dss_get_snapshot_list(&sl);
1413 assert(!name_of_reference_snapshot);
1414 name_of_reference_snapshot = name_of_newest_complete_snapshot(&sl);
1415 free_snapshot_list(&sl);
1418 * We specify up to 6 arguments, one argument per given rsync option
1419 * and one argument per given source dir. We also need space for the
1420 * terminating NULL pointer.
1422 N = OPT_GIVEN(DSS, RSYNC_OPTION) + OPT_GIVEN(DSS, SOURCE_DIR);
1423 *argv = dss_malloc((7 + N) * sizeof(char *));
1424 (*argv)[i++] = dss_strdup("rsync");
1425 (*argv)[i++] = dss_strdup("-a");
1426 (*argv)[i++] = dss_strdup("--delete");
1428 srandom((unsigned)time(NULL)); /* no need to be fancy here */
1431 if (1000 * (random() / (RAND_MAX + 1.0)) < OPT_UINT32_VAL(DSS, CHECKSUM)) {
1432 DSS_NOTICE_LOG(("adding --checksum to rsync options\n"));
1433 (*argv)[i++] = dss_strdup("--checksum");
1435 for (j = 0; j < OPT_GIVEN(DSS, RSYNC_OPTION); j++)
1436 (*argv)[i++] = dss_strdup(lls_string_val(j,
1437 OPT_RESULT(DSS, RSYNC_OPTION)));
1438 if (name_of_reference_snapshot) {
1439 DSS_INFO_LOG(("using %s as reference\n", name_of_reference_snapshot));
1440 (*argv)[i++] = make_message("--link-dest=../%s",
1441 name_of_reference_snapshot);
1443 DSS_INFO_LOG(("no suitable reference snapshot found\n"));
1444 logname = dss_logname();
1445 if (use_rsync_locally(logname)) {
1446 for (j = 0; j < OPT_GIVEN(DSS, SOURCE_DIR); j++)
1447 (*argv)[i++] = dss_strdup(lls_string_val(j,
1448 OPT_RESULT(DSS, SOURCE_DIR)));
1451 * dss-1.0 and earlier did not support multiple source
1452 * directories. These versions appended a slash to the end of
1453 * the source directory to make sure that only the contents of
1454 * the single source directory, but not the directory itself,
1455 * are copied to the destination. For multiple source
1456 * directories, however, this is not a good idea because the
1457 * source directories may well contain identical file names,
1458 * which would then be copied to the same location on the
1459 * destination, overwriting each other. Moreover, we want the
1460 * directory on the destination match the source. To preserve
1461 * the old behaviour, we thus have to special-case N=1.
1463 for (j = 0; j < OPT_GIVEN(DSS, SOURCE_DIR); j++) {
1464 (*argv)[i++] = make_message("%s@%s:%s%s",
1465 OPT_GIVEN(DSS, REMOTE_USER)?
1466 OPT_STRING_VAL(DSS, REMOTE_USER) : logname,
1467 OPT_STRING_VAL(DSS, REMOTE_HOST),
1468 lls_string_val(j, OPT_RESULT(DSS, SOURCE_DIR)),
1469 OPT_GIVEN(DSS, SOURCE_DIR) == 1? "/" : ""
1474 *num = get_current_time();
1475 (*argv)[i++] = incomplete_name(*num);
1476 (*argv)[i++] = NULL;
1477 for (j = 0; j < i; j++)
1478 DSS_DEBUG_LOG(("argv[%d] = %s\n", j, (*argv)[j]));
1481 static void free_rsync_argv(char **argv)
1487 for (i = 0; argv[i]; i++)
1492 static int create_snapshot(char **argv)
1497 ret = rename_resume_snap(current_snapshot_creation_time);
1500 dss_exec(&create_pid, argv[0], argv);
1501 snapshot_creation_status = HS_RUNNING;
1505 static int select_loop(void)
1508 /* check every 60 seconds for free disk space */
1510 char **rsync_argv = NULL;
1514 struct timeval *tvp;
1517 tvp = NULL; /* sleep until rm hook/process dies */
1518 else { /* sleep one minute */
1524 FD_SET(signal_pipe, &rfds);
1525 ret = dss_select(signal_pipe + 1, &rfds, NULL, tvp);
1528 if (FD_ISSET(signal_pipe, &rfds)) {
1529 ret = handle_signal();
1535 if (snapshot_removal_status == HS_PRE_SUCCESS) {
1541 if (snapshot_removal_status == HS_SUCCESS) {
1545 ret = try_to_free_disk_space();
1548 if (snapshot_removal_status != HS_READY) {
1549 stop_create_process();
1552 restart_create_process();
1553 switch (snapshot_creation_status) {
1555 if (!next_snapshot_is_due())
1559 case HS_PRE_RUNNING:
1561 case HS_POST_RUNNING:
1563 case HS_PRE_SUCCESS:
1564 if (!name_of_reference_snapshot) {
1565 free_rsync_argv(rsync_argv);
1566 create_rsync_argv(&rsync_argv, ¤t_snapshot_creation_time);
1568 ret = create_snapshot(rsync_argv);
1572 case HS_NEEDS_RESTART:
1573 if (!next_snapshot_is_due())
1575 ret = create_snapshot(rsync_argv);
1588 static void exit_hook(int exit_code)
1591 char **argv, *tmp = dss_strdup(OPT_STRING_VAL(DSS, EXIT_HOOK));
1592 unsigned n = split_args(tmp, &argv);
1595 argv = dss_realloc(argv, (n + 1) * sizeof(char *));
1596 argv[n - 1] = dss_strdup(dss_strerror(-exit_code));
1598 dss_exec(&pid, argv[0], argv);
1604 static void lock_dss_or_die(void)
1606 int ret = lock_dss(ipc_key);
1609 DSS_EMERG_LOG(("failed to lock: %s\n", dss_strerror(-ret)));
1614 static int com_run(void)
1619 if (OPT_GIVEN(DSS, DRY_RUN)) {
1620 DSS_ERROR_LOG(("dry run not supported by this command\n"));
1623 ret = get_dss_pid(ipc_key, &pid);
1625 DSS_ERROR_LOG(("pid %d\n", (int)pid));
1626 return -E_ALREADY_RUNNING;
1629 * Order is important here: Since daemon_init() forks, it would drop
1630 * the lock if it had been acquired already. Changing the cwd before
1631 * grabbing the lock causes stat(2) to fail in case a relative config
1632 * file path was given, which results in a different key ID for
1633 * locking. Therefore we must first daemonize, then lock, then change
1636 if (OPT_GIVEN(RUN, DAEMON)) {
1639 logfile = open_log(OPT_STRING_VAL(RUN, LOGFILE));
1642 ret = change_to_dest_dir();
1645 dump_dss_config("startup");
1646 ret = install_sighandler(SIGHUP);
1650 ret = write(fd, "\0", 1);
1652 DSS_ERROR_LOG(("write to daemon pipe returned %d\n",
1655 return -ERRNO_TO_DSS_ERROR(errno);
1659 ret = select_loop();
1660 if (ret >= 0) /* impossible */
1664 while (wait(NULL) >= 0 || errno != ECHILD)
1665 ; /* still have children to wait for */
1668 EXPORT_CMD_HANDLER(run);
1670 static int com_prune(void)
1673 struct snapshot_list sl;
1674 struct snapshot *victim;
1675 struct disk_space ds;
1680 ret = change_to_dest_dir();
1683 switch (OPT_UINT32_VAL(PRUNE, DISK_SPACE)) {
1684 case FDS_LOW: try_hard = true; break;
1685 case FDS_HIGH: try_hard = false; break;
1687 ret = get_disk_space(".", &ds);
1690 log_disk_space(&ds);
1691 try_hard = disk_space_low(&ds);
1693 dss_get_snapshot_list(&sl);
1694 victim = find_removable_snapshot(&sl, try_hard, &why);
1696 dss_msg("nothing to prune\n");
1700 if (OPT_GIVEN(DSS, DRY_RUN)) {
1701 dss_msg("picking %s snapshot %s (interval = %i)\n",
1702 why, victim->name, victim->interval);
1706 pre_remove_hook(victim, why);
1707 if (snapshot_removal_status == HS_PRE_RUNNING) {
1708 ret = wait_for_remove_process();
1711 ret = -E_HOOK_FAILED;
1712 if (snapshot_removal_status != HS_PRE_SUCCESS)
1718 ret = wait_for_remove_process();
1721 assert(snapshot_removal_status == HS_SUCCESS);
1723 assert(snapshot_removal_status == HS_POST_RUNNING);
1724 ret = wait_for_remove_process();
1728 free_snapshot_list(&sl);
1731 EXPORT_CMD_HANDLER(prune);
1733 static int com_create(void)
1739 ret = change_to_dest_dir();
1742 if (OPT_GIVEN(DSS, DRY_RUN)) {
1745 create_rsync_argv(&rsync_argv, ¤t_snapshot_creation_time);
1746 for (i = 0; rsync_argv[i]; i++) {
1748 msg = make_message("%s%s%s", tmp? tmp : "",
1749 tmp? " " : "", rsync_argv[i]);
1752 free_rsync_argv(rsync_argv);
1753 dss_msg("%s\n", msg);
1759 ret = wait_for_process(create_pid, &status);
1762 ret = handle_pre_create_hook_exit(status);
1763 if (ret <= 0) /* error, or pre-create failed */
1766 create_rsync_argv(&rsync_argv, ¤t_snapshot_creation_time);
1767 ret = create_snapshot(rsync_argv);
1770 ret = wait_for_process(create_pid, &status);
1773 ret = handle_rsync_exit(status);
1778 ret = wait_for_process(create_pid, &status);
1780 free_rsync_argv(rsync_argv);
1783 EXPORT_CMD_HANDLER(create);
1785 static int com_ls(void)
1788 struct snapshot_list sl;
1790 int64_t now = get_current_time();
1792 ret = change_to_dest_dir();
1795 dss_get_snapshot_list(&sl);
1796 FOR_EACH_SNAPSHOT(s, i, &sl) {
1798 if (s->flags & SS_COMPLETE)
1799 d = (s->completion_time - s->creation_time) / 60;
1801 d = (now - s->creation_time) / 60;
1802 dss_msg("%u\t%s\t%3" PRId64 ":%02" PRId64 "\n", s->interval,
1803 s->name, d / 60, d % 60);
1805 free_snapshot_list(&sl);
1808 EXPORT_CMD_HANDLER(ls);
1810 static int com_configtest(void)
1812 printf("Syntax Ok\n");
1815 EXPORT_CMD_HANDLER(configtest);
1817 static int setup_signal_handling(void)
1821 DSS_INFO_LOG(("setting up signal handlers\n"));
1822 signal_pipe = signal_init(); /* always successful */
1823 ret = install_sighandler(SIGINT);
1826 ret = install_sighandler(SIGTERM);
1829 return install_sighandler(SIGCHLD);
1832 const char *dss_version(void);
1833 static void handle_version_and_help(void)
1837 if (OPT_GIVEN(DSS, VERSION)) {
1838 printf("%s\n", dss_version());
1841 if (OPT_GIVEN(DSS, DETAILED_HELP))
1842 txt = lls_long_help(CMD_PTR(DSS));
1843 else if (OPT_GIVEN(DSS, HELP))
1844 txt = lls_short_help(CMD_PTR(DSS));
1848 printf("\nRun dss help for help on subcommands.\n");
1853 static void show_subcommand_summary(void)
1855 const struct lls_command *cmd;
1858 printf("Available subcommands:\n");
1859 for (i = 1; (cmd = lls_cmd(i, dss_suite)); i++) {
1860 const char *name = lls_command_name(cmd);
1861 const char *purpose = lls_purpose(cmd);
1862 printf("%-11s%s\n", name, purpose);
1864 printf("\nRun dss help <subcmd> for help on <subcmd>.\n");
1867 static int com_help(void)
1870 char *errctx, *help;
1872 const struct lls_command *cmd;
1874 ret = lls_check_arg_count(sublpr, 0, 1, &errctx);
1876 return lopsub_error(ret, &errctx);
1877 if (lls_num_inputs(sublpr) == 0) {
1878 show_subcommand_summary();
1881 arg = lls_input(0, sublpr);
1882 ret = lls_lookup_subcmd(arg, dss_suite, &errctx);
1884 return lopsub_error(ret, &errctx);
1885 cmd = lls_cmd(ret, dss_suite);
1886 if (OPT_GIVEN(HELP, LONG))
1887 help = lls_long_help(cmd);
1889 help = lls_short_help(cmd);
1892 if (!OPT_GIVEN(HELP, LONG))
1893 printf("\nRun dss -- help -l %s for long help.\n", arg);
1896 EXPORT_CMD_HANDLER(help);
1898 int main(int argc, char **argv)
1901 char *errctx = NULL;
1902 unsigned num_inputs;
1903 const struct dss_user_data *ud;
1905 ret = lls_parse(argc, argv, CMD_PTR(DSS), &cmdline_lpr, &errctx);
1907 ret = lopsub_error(ret, &errctx);
1911 set_config_file_name();
1912 ret = parse_config_file(false /* no SIGHUP */, CMD_PTR(DSS));
1915 handle_version_and_help();
1916 num_inputs = lls_num_inputs(lpr);
1917 if (num_inputs == 0) {
1918 show_subcommand_summary();
1922 ret = lls_lookup_subcmd(argv[argc - num_inputs], dss_suite, &errctx);
1924 ret = lopsub_error(ret, &errctx);
1927 subcmd = lls_cmd(ret, dss_suite);
1928 ret = lls_parse(num_inputs, argv + argc - num_inputs, subcmd,
1929 &cmdline_sublpr, &errctx);
1931 ret = lopsub_error(ret, &errctx);
1934 sublpr = cmdline_sublpr;
1935 ret = parse_config_file(false /* no SIGHUP */, subcmd);
1938 ret = check_config();
1941 ret = setup_signal_handling();
1944 ud = lls_user_data(subcmd);
1945 ret = ud->handler();
1950 DSS_ERROR_LOG(("%s\n", errctx));
1951 DSS_EMERG_LOG(("%s\n", dss_strerror(-ret)));
1954 lls_free_parse_result(lpr, CMD_PTR(DSS));
1955 if (lpr != cmdline_lpr)
1956 lls_free_parse_result(cmdline_lpr, CMD_PTR(DSS));
1957 lls_free_parse_result(sublpr, subcmd);
1958 if (sublpr != cmdline_sublpr)
1959 lls_free_parse_result(cmdline_sublpr, subcmd);
1961 exit(ret >= 0? EXIT_SUCCESS : EXIT_FAILURE);